Deleted Added
full compact
ctl.c (267515) ctl.c (267537)
1/*-
2 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3 * Copyright (c) 2012 The FreeBSD Foundation
4 * All rights reserved.
5 *
6 * Portions of this software were developed by Edward Tomasz Napierala
7 * under sponsorship from the FreeBSD Foundation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
16 * substantially similar to the "NO WARRANTY" disclaimer below
17 * ("Disclaimer") and any redistribution must be conditioned upon
18 * including a substantially similar Disclaimer requirement for further
19 * binary redistribution.
20 *
21 * NO WARRANTY
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGES.
33 *
34 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.c#8 $
35 */
36/*
37 * CAM Target Layer, a SCSI device emulation subsystem.
38 *
39 * Author: Ken Merry <ken@FreeBSD.org>
40 */
41
42#define _CTL_C
43
44#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3 * Copyright (c) 2012 The FreeBSD Foundation
4 * All rights reserved.
5 *
6 * Portions of this software were developed by Edward Tomasz Napierala
7 * under sponsorship from the FreeBSD Foundation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
16 * substantially similar to the "NO WARRANTY" disclaimer below
17 * ("Disclaimer") and any redistribution must be conditioned upon
18 * including a substantially similar Disclaimer requirement for further
19 * binary redistribution.
20 *
21 * NO WARRANTY
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGES.
33 *
34 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.c#8 $
35 */
36/*
37 * CAM Target Layer, a SCSI device emulation subsystem.
38 *
39 * Author: Ken Merry <ken@FreeBSD.org>
40 */
41
42#define _CTL_C
43
44#include <sys/cdefs.h>
45__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl.c 267515 2014-06-15 18:16:51Z mav $");
45__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl.c 267537 2014-06-16 11:00:14Z mav $");
46
47#include <sys/param.h>
48#include <sys/systm.h>
49#include <sys/kernel.h>
50#include <sys/types.h>
51#include <sys/kthread.h>
52#include <sys/bio.h>
53#include <sys/fcntl.h>
54#include <sys/lock.h>
55#include <sys/module.h>
56#include <sys/mutex.h>
57#include <sys/condvar.h>
58#include <sys/malloc.h>
59#include <sys/conf.h>
60#include <sys/ioccom.h>
61#include <sys/queue.h>
62#include <sys/sbuf.h>
63#include <sys/smp.h>
64#include <sys/endian.h>
65#include <sys/sysctl.h>
66
67#include <cam/cam.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>
82
83struct ctl_softc *control_softc = NULL;
84
85/*
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.
90 */
91#define CTL_DONE_THREAD
92
93/*
94 * Use the serial number and device ID provided by the backend, rather than
95 * making up our own.
96 */
97#define CTL_USE_BACKEND_SN
98
99/*
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
102 * hardware.
103 */
104#define CTL_ALIGN_8B(target, source, type) \
105 if (((uint32_t)source & 0x7) != 0) \
106 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\
107 else \
108 target = (type)source;
109
110#define CTL_SIZE_8B(target, size) \
111 if ((size & 0x7) != 0) \
112 target = size + (0x8 - (size & 0x7)); \
113 else \
114 target = size;
115
116#define CTL_ALIGN_8B_MARGIN 16
117
118/*
119 * Template mode pages.
120 */
121
122/*
123 * Note that these are default values only. The actual values will be
124 * filled in when the user does a mode sense.
125 */
126static 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,
132 /* total_luns */ 26,
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,
136 0, 0, 0, 0, 0, 0}
137};
138
139static 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},
144 /*page_version*/ 0,
145 /* total_luns */ 0,
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,
149 0, 0, 0, 0, 0, 0}
150};
151
152static 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
158 0, //lock_active
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
162};
163
164static 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
169 0, //page_version
170 0, //lock_active
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
174};
175
176static 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 */
184};
185
186static 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 */
193};
194
195static 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},
208 /*flags*/ SFP_HSEC,
209 /*reserved*/ {0, 0, 0}
210};
211
212static 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},
224 /*flags*/ 0,
225 /*reserved*/ {0, 0, 0}
226};
227
228static 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,
239 /*reserved1*/ 0,
240 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
241 CTL_DEFAULT_ROTATION_RATE & 0xff},
242 /*reserved2*/ {0, 0}
243};
244
245static 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},
249 /*heads*/ 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},
254 /*rpl*/ 0,
255 /*rotational_offset*/ 0,
256 /*reserved1*/ 0,
257 /*rotation_rate*/ {0, 0},
258 /*reserved2*/ {0, 0}
259};
260
261static 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,
265 /*ret_priority*/ 0,
266 /*disable_pf_transfer_len*/ {0xff, 0xff},
267 /*min_prefetch*/ {0, 0},
268 /*max_prefetch*/ {0xff, 0xff},
269 /*max_pf_ceiling*/ {0xff, 0xff},
270 /*flags2*/ 0,
271 /*cache_segments*/ 0,
272 /*cache_seg_size*/ {0, 0},
273 /*reserved*/ 0,
274 /*non_cache_seg_size*/ {0, 0, 0}
275};
276
277static struct scsi_caching_page caching_page_changeable = {
278 /*page_code*/SMS_CACHING_PAGE,
279 /*page_length*/sizeof(struct scsi_caching_page) - 2,
280 /*flags1*/ 0,
281 /*ret_priority*/ 0,
282 /*disable_pf_transfer_len*/ {0, 0},
283 /*min_prefetch*/ {0, 0},
284 /*max_prefetch*/ {0, 0},
285 /*max_pf_ceiling*/ {0, 0},
286 /*flags2*/ 0,
287 /*cache_segments*/ 0,
288 /*cache_seg_size*/ {0, 0},
289 /*reserved*/ 0,
290 /*non_cache_seg_size*/ {0, 0, 0}
291};
292
293static struct scsi_control_page control_page_default = {
294 /*page_code*/SMS_CONTROL_MODE_PAGE,
295 /*page_length*/sizeof(struct scsi_control_page) - 2,
296 /*rlec*/0,
297 /*queue_flags*/0,
298 /*eca_and_aen*/0,
299 /*reserved*/0,
300 /*aen_holdoff_period*/{0, 0}
301};
302
303static struct scsi_control_page control_page_changeable = {
304 /*page_code*/SMS_CONTROL_MODE_PAGE,
305 /*page_length*/sizeof(struct scsi_control_page) - 2,
306 /*rlec*/SCP_DSENSE,
307 /*queue_flags*/0,
308 /*eca_and_aen*/0,
309 /*reserved*/0,
310 /*aen_holdoff_period*/{0, 0}
311};
312
313
314/*
315 * XXX KDM move these into the softc.
316 */
317static int rcv_sync_msg;
318static int persis_offset;
319static uint8_t ctl_pause_rtr;
320static int ctl_is_single = 1;
321static int index_to_aps_page;
322
323SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
324static int worker_threads = 1;
325TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads);
326SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
327 &worker_threads, 1, "Number of worker threads");
328static int verbose = 0;
329TUNABLE_INT("kern.cam.ctl.verbose", &verbose);
330SYSCTL_INT(_kern_cam_ctl, OID_AUTO, verbose, CTLFLAG_RWTUN,
331 &verbose, 0, "Show SCSI errors returned to initiator");
332
333/*
334 * Serial number (0x80), device id (0x83), supported pages (0x00),
335 * Block limits (0xB0) and Logical Block Provisioning (0xB2)
336 */
337#define SCSI_EVPD_NUM_SUPPORTED_PAGES 5
338
339static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
340 int param);
341static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
342static int ctl_init(void);
343void ctl_shutdown(void);
344static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
345static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
346static void ctl_ioctl_online(void *arg);
347static void ctl_ioctl_offline(void *arg);
348static int ctl_ioctl_targ_enable(void *arg, struct ctl_id targ_id);
349static int ctl_ioctl_targ_disable(void *arg, struct ctl_id targ_id);
350static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id);
351static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id);
352static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio);
353static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio, int have_lock);
354static int ctl_ioctl_submit_wait(union ctl_io *io);
355static void ctl_ioctl_datamove(union ctl_io *io);
356static void ctl_ioctl_done(union ctl_io *io);
357static void ctl_ioctl_hard_startstop_callback(void *arg,
358 struct cfi_metatask *metatask);
359static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask);
360static 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);
363static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
364 struct thread *td);
365uint32_t ctl_get_resindex(struct ctl_nexus *nexus);
366uint32_t ctl_port_idx(int port_num);
367#ifdef unused
368static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port,
369 uint32_t targ_target, uint32_t targ_lun,
370 int can_wait);
371static void ctl_kfree_io(union ctl_io *io);
372#endif /* unused */
373static 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);
375static int ctl_free_lun(struct ctl_lun *lun);
376static void ctl_create_lun(struct ctl_be_lun *be_lun);
377/**
378static void ctl_failover_change_pages(struct ctl_softc *softc,
379 struct ctl_scsiio *ctsio, int master);
380**/
381
382static int ctl_do_mode_select(union ctl_io *io);
383static 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);
389static void ctl_pro_preempt_other(struct ctl_lun *lun,
390 union ctl_ha_msg *msg);
391static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
392static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
393static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
394static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
395static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
396 int alloc_len);
397static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
398static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
399static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
400static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len);
401static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2);
402static ctl_action ctl_check_for_blockage(union ctl_io *pending_io,
403 union ctl_io *ooa_io);
404static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
405 union ctl_io *starting_io);
406static int ctl_check_blocked(struct ctl_lun *lun);
407static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc,
408 struct ctl_lun *lun,
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);
412static void ctl_failover(void);
413static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
414 struct ctl_scsiio *ctsio);
415static int ctl_scsiio(struct ctl_scsiio *ctsio);
416
417static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
418static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
419 ctl_ua_type ua_type);
420static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io,
421 ctl_ua_type ua_type);
422static int ctl_abort_task(union ctl_io *io);
423static void ctl_run_task_queue(struct ctl_softc *ctl_softc);
424#ifdef CTL_IO_DELAY
425static void ctl_datamove_timer_wakeup(void *arg);
426static void ctl_done_timer_wakeup(void *arg);
427#endif /* CTL_IO_DELAY */
428
429static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
430static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
431static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
432static void ctl_datamove_remote_write(union ctl_io *io);
433static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
434static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
435static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
436static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
437 ctl_ha_dt_cb callback);
438static void ctl_datamove_remote_read(union ctl_io *io);
439static void ctl_datamove_remote(union ctl_io *io);
440static int ctl_process_done(union ctl_io *io, int have_lock);
441static void ctl_work_thread(void *arg);
442
443/*
444 * Load the serialization table. This isn't very pretty, but is probably
445 * the easiest way to do it.
446 */
447#include "ctl_ser_table.c"
448
449/*
450 * We only need to define open, close and ioctl routines for this driver.
451 */
452static struct cdevsw ctl_cdevsw = {
453 .d_version = D_VERSION,
454 .d_flags = 0,
455 .d_open = ctl_open,
456 .d_close = ctl_close,
457 .d_ioctl = ctl_ioctl,
458 .d_name = "ctl",
459};
460
461
462MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
463
464static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
465
466static moduledata_t ctl_moduledata = {
467 "ctl",
468 ctl_module_event_handler,
469 NULL
470};
471
472DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
473MODULE_VERSION(ctl, 1);
474
475static void
476ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
477 union ctl_ha_msg *msg_info)
478{
479 struct ctl_scsiio *ctsio;
480
481 if (msg_info->hdr.original_sc == NULL) {
482 printf("%s: original_sc == NULL!\n", __func__);
483 /* XXX KDM now what? */
484 return;
485 }
486
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);
500 ctl_wakeup_thread();
501}
502
503static void
504ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
505 union ctl_ha_msg *msg_info)
506{
507 struct ctl_scsiio *ctsio;
508
509 if (msg_info->hdr.serializing_sc == NULL) {
510 printf("%s: serializing_sc == NULL!\n", __func__);
511 /* XXX KDM now what? */
512 return;
513 }
514
515 ctsio = &msg_info->hdr.serializing_sc->scsiio;
516#if 0
517 /*
518 * Attempt to catch the situation where an I/O has
519 * been freed, and we're using it again.
520 */
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__,
525 ctsio);
526 printf("%s: type %d msg %d cdb %x iptl: "
527 "%d:%d:%d:%d tag 0x%04x "
528 "flag %#x status %x\n",
529 __func__,
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 ==
538 CTL_IO_TASK) ?
539 tmp_io->taskio.tag_num :
540 tmp_io->scsiio.tag_num,
541 tmp_io->io_hdr.flags,
542 tmp_io->io_hdr.status);
543 }
544#endif
545 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
546 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, &ctsio->io_hdr, links);
547 ctl_wakeup_thread();
548}
549
550/*
551 * ISC (Inter Shelf Communication) event handler. Events from the HA
552 * subsystem come in here.
553 */
554static void
555ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
556{
557 struct ctl_softc *ctl_softc;
558 union ctl_io *io;
559 struct ctl_prio *presio;
560 ctl_ha_status isc_status;
561
562 ctl_softc = control_softc;
563 io = NULL;
564
565
566#if 0
567 printf("CTL: Isc Msg event %d\n", event);
568#endif
569 if (event == CTL_HA_EVT_MSG_RECV) {
570 union ctl_ha_msg msg_info;
571
572 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
573 sizeof(msg_info), /*wait*/ 0);
574#if 0
575 printf("CTL: msg_type %d\n", msg_info.msg_type);
576#endif
577 if (isc_status != 0) {
578 printf("Error receiving message, status = %d\n",
579 isc_status);
580 return;
581 }
582 mtx_lock(&ctl_softc->ctl_lock);
583
584 switch (msg_info.hdr.msg_type) {
585 case CTL_MSG_SERIALIZE:
586#if 0
587 printf("Serialize\n");
588#endif
589 io = ctl_alloc_io((void *)ctl_softc->othersc_pool);
590 if (io == NULL) {
591 printf("ctl_isc_event_handler: can't allocate "
592 "ctl_io!\n");
593 /* Bad Juju */
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){
602 }
603 goto bailout;
604 }
605 ctl_zero_io(io);
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;
610#if 0
611 printf("pOrig %x\n", (int)msg_info.original_sc);
612#endif
613 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
614 CTL_FLAG_IO_ACTIVE;
615 /*
616 * If we're in serialization-only mode, we don't
617 * want to go through full done processing. Thus
618 * the COPY flag.
619 *
620 * XXX KDM add another flag that is more specific.
621 */
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;
625#if 0
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);
631#endif
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,
635 CTL_MAX_CDBLEN);
636 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
637 struct ctl_cmd_entry *entry;
638 uint8_t opcode;
639
640 opcode = io->scsiio.cdb[0];
641 entry = &ctl_cmd_table[opcode];
642 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
643 io->io_hdr.flags |=
644 entry->flags & CTL_FLAG_DATA_MASK;
645 }
646 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
647 &io->io_hdr, links);
648 ctl_wakeup_thread();
649 break;
650
651 /* Performed on the Originating SC, XFER mode only */
652 case CTL_MSG_DATAMOVE: {
653 struct ctl_sg_entry *sgl;
654 int i, j;
655
656 io = msg_info.hdr.original_sc;
657 if (io == NULL) {
658 printf("%s: original_sc == NULL!\n", __func__);
659 /* XXX KDM do something here */
660 break;
661 }
662 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
663 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
664 /*
665 * Keep track of this, we need to send it back over
666 * when the datamove is complete.
667 */
668 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
669
670 if (msg_info.dt.sg_sequence == 0) {
671 /*
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
675 * lists.
676 */
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",
682 __func__,
683 msg_info.dt.kern_sg_entries,
684 sizeof(io->io_hdr.remote_sglist)/
685 sizeof(io->io_hdr.remote_sglist[0]));
686
687 /*
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.
692 */
693 break;
694 }
695 sgl = io->io_hdr.remote_sglist;
696 memset(sgl, 0,
697 sizeof(io->io_hdr.remote_sglist));
698
699 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
700
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;
713 /*
714 * Clear out per-DMA flags.
715 */
716 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK;
717 /*
718 * Add per-DMA flags that are set for this
719 * particular DMA request.
720 */
721 io->io_hdr.flags |= msg_info.dt.flags &
722 CTL_FLAG_RDMA_MASK;
723 } else
724 sgl = (struct ctl_sg_entry *)
725 io->scsiio.kern_data_ptr;
726
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;
732
733#if 0
734 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n",
735 __func__,
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);
739#endif
740 }
741#if 0
742 memcpy(&sgl[msg_info.dt.sent_sg_entries],
743 msg_info.dt.sg_list,
744 sizeof(*sgl) * msg_info.dt.cur_sg_entries);
745#endif
746
747 /*
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.
751 */
752 if (msg_info.dt.sg_last != 0) {
753 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
754 &io->io_hdr, links);
755 ctl_wakeup_thread();
756 }
757 break;
758 }
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",
763 __func__);
764 /* XXX KDM now what? */
765 break;
766 }
767 /*
768 * We grab the sense information here in case
769 * there was a failure, so we can return status
770 * back to the initiator.
771 */
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));
782
783 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
784 &io->io_hdr, links);
785 ctl_wakeup_thread();
786 break;
787 }
788
789 /* Preformed on Originating SC, SER_ONLY mode */
790 case CTL_MSG_R2R:
791 io = msg_info.hdr.original_sc;
792 if (io == NULL) {
793 printf("%s: Major Bummer\n", __func__);
794 mtx_unlock(&ctl_softc->ctl_lock);
795 return;
796 } else {
797#if 0
798 printf("pOrig %x\n",(int) ctsio);
799#endif
800 }
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,
804 &io->io_hdr, links);
805 ctl_wakeup_thread();
806 break;
807
808 /*
809 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
810 * mode.
811 * Performed on the Originating (i.e. secondary) SC in XFER
812 * mode
813 */
814 case CTL_MSG_FINISH_IO:
815 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER)
816 ctl_isc_handler_finish_xfer(ctl_softc,
817 &msg_info);
818 else
819 ctl_isc_handler_finish_ser_only(ctl_softc,
820 &msg_info);
821 break;
822
823 /* Preformed on Originating SC */
824 case CTL_MSG_BAD_JUJU:
825 io = msg_info.hdr.original_sc;
826 if (io == NULL) {
827 printf("%s: Bad JUJU!, original_sc is NULL!\n",
828 __func__);
829 break;
830 }
831 ctl_copy_sense_data(&msg_info, io);
832 /*
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.
836 */
837 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
838 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
839
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,
843 &io->io_hdr, links);
844 ctl_wakeup_thread();
845 break;
846
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 "
854 "ctl_io!\n");
855 /* Bad Juju */
856 /* should I just call the proper reset func
857 here??? */
858 mtx_unlock(&ctl_softc->ctl_lock);
859 goto bailout;
860 }
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;
868#ifdef CTL_TIME_IO
869 taskio->io_hdr.start_time = time_uptime;
870 getbintime(&taskio->io_hdr.start_bt);
871#if 0
872 cs_prof_gettime(&taskio->io_hdr.start_ticks);
873#endif
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;
878 ctl_wakeup_thread();
879 break;
880 }
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 "
887 "ctl_io!\n");
888 /* Bad Juju */
889 /* Need to set busy and send msg back */
890 mtx_unlock(&ctl_softc->ctl_lock);
891 goto bailout;
892 }
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);
898 ctl_wakeup_thread();
899 break;
900 case CTL_MSG_SYNC_FE:
901 rcv_sync_msg = 1;
902 break;
903 case CTL_MSG_APS_LOCK: {
904 // It's quicker to execute this then to
905 // queue it.
906 struct ctl_lun *lun;
907 struct ctl_page_index *page_index;
908 struct copan_aps_subpage *current_sp;
909 uint32_t targ_lun;
910
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);
914
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));
920
921 current_sp->lock_active = msg_info.aps.lock_flag;
922 break;
923 }
924 default:
925 printf("How did I get here?\n");
926 }
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",
931 param);
932 }
933 return;
934 } else if (event == CTL_HA_EVT_DISCONNECT) {
935 printf("CTL: Got a disconnect from Isc\n");
936 return;
937 } else {
938 printf("ctl_isc_event_handler: Unknown event %d\n", event);
939 return;
940 }
941
942bailout:
943 return;
944}
945
946static void
947ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
948{
949 struct scsi_sense_data *sense;
950
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;
956}
957
958static int
959ctl_init(void)
960{
961 struct ctl_softc *softc;
962 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool;
963 struct ctl_frontend *fe;
964 uint8_t sc_id =0;
965 int i, error, retval;
966 //int isc_retval;
967
968 retval = 0;
969 ctl_pause_rtr = 0;
970 rcv_sync_msg = 0;
971
972 control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
973 M_WAITOK | M_ZERO);
974 softc = control_softc;
975
976 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
977 "cam/ctl");
978
979 softc->dev->si_drv1 = softc;
980
981 /*
982 * By default, return a "bad LUN" peripheral qualifier for unknown
983 * LUNs. The user can override this default using the tunable or
984 * sysctl. See the comment in ctl_inquiry_std() for more details.
985 */
986 softc->inquiry_pq_no_lun = 1;
987 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun",
988 &softc->inquiry_pq_no_lun);
989 sysctl_ctx_init(&softc->sysctl_ctx);
990 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
991 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
992 CTLFLAG_RD, 0, "CAM Target Layer");
993
994 if (softc->sysctl_tree == NULL) {
995 printf("%s: unable to allocate sysctl tree\n", __func__);
996 destroy_dev(softc->dev);
997 free(control_softc, M_DEVBUF);
998 control_softc = NULL;
999 return (ENOMEM);
1000 }
1001
1002 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1003 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
1004 "inquiry_pq_no_lun", CTLFLAG_RW,
1005 &softc->inquiry_pq_no_lun, 0,
1006 "Report no lun possible for invalid LUNs");
1007
1008 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1009 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF);
1010 softc->open_count = 0;
1011
1012 /*
1013 * Default to actually sending a SYNCHRONIZE CACHE command down to
1014 * the drive.
1015 */
1016 softc->flags = CTL_FLAG_REAL_SYNC;
1017
1018 /*
1019 * In Copan's HA scheme, the "master" and "slave" roles are
1020 * figured out through the slot the controller is in. Although it
1021 * is an active/active system, someone has to be in charge.
1022 */
1023#ifdef NEEDTOPORT
1024 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id);
1025#endif
1026
1027 if (sc_id == 0) {
1028 softc->flags |= CTL_FLAG_MASTER_SHELF;
1029 persis_offset = 0;
1030 } else
1031 persis_offset = CTL_MAX_INITIATORS;
1032
1033 /*
1034 * XXX KDM need to figure out where we want to get our target ID
1035 * and WWID. Is it different on each port?
1036 */
1037 softc->target.id = 0;
1038 softc->target.wwid[0] = 0x12345678;
1039 softc->target.wwid[1] = 0x87654321;
1040 STAILQ_INIT(&softc->lun_list);
1041 STAILQ_INIT(&softc->pending_lun_queue);
1042 STAILQ_INIT(&softc->task_queue);
1043 STAILQ_INIT(&softc->incoming_queue);
1044 STAILQ_INIT(&softc->rtr_queue);
1045 STAILQ_INIT(&softc->done_queue);
1046 STAILQ_INIT(&softc->isc_queue);
1047 STAILQ_INIT(&softc->fe_list);
1048 STAILQ_INIT(&softc->be_list);
1049 STAILQ_INIT(&softc->io_pools);
1050
1051 /*
1052 * We don't bother calling these with ctl_lock held here, because,
1053 * in theory, no one else can try to do anything while we're in our
1054 * module init routine.
1055 */
1056 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL,
1057 &internal_pool)!= 0){
1058 printf("ctl: can't allocate %d entry internal pool, "
1059 "exiting\n", CTL_POOL_ENTRIES_INTERNAL);
1060 return (ENOMEM);
1061 }
1062
1063 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY,
1064 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) {
1065 printf("ctl: can't allocate %d entry emergency pool, "
1066 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY);
1067 ctl_pool_free(internal_pool);
1068 return (ENOMEM);
1069 }
1070
1071 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC,
1072 &other_pool) != 0)
1073 {
1074 printf("ctl: can't allocate %d entry other SC pool, "
1075 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1076 ctl_pool_free(internal_pool);
1077 ctl_pool_free(emergency_pool);
1078 return (ENOMEM);
1079 }
1080
1081 softc->internal_pool = internal_pool;
1082 softc->emergency_pool = emergency_pool;
1083 softc->othersc_pool = other_pool;
1084
1085 if (worker_threads > MAXCPU || worker_threads == 0) {
1086 printf("invalid kern.cam.ctl.worker_threads value; "
1087 "setting to 1");
1088 worker_threads = 1;
1089 } else if (worker_threads < 0) {
1090 if (mp_ncpus > 2) {
1091 /*
1092 * Using more than two worker threads actually hurts
1093 * performance due to lock contention.
1094 */
1095 worker_threads = 2;
1096 } else {
1097 worker_threads = 1;
1098 }
1099 }
1100
1101 for (i = 0; i < worker_threads; i++) {
1102 error = kproc_kthread_add(ctl_work_thread, softc,
1103 &softc->work_thread, NULL, 0, 0, "ctl", "work%d", i);
1104 if (error != 0) {
1105 printf("error creating CTL work thread!\n");
1106 ctl_pool_free(internal_pool);
1107 ctl_pool_free(emergency_pool);
1108 ctl_pool_free(other_pool);
1109 return (error);
1110 }
1111 }
1112 if (bootverbose)
1113 printf("ctl: CAM Target Layer loaded\n");
1114
1115 /*
1116 * Initialize the initiator and portname mappings
1117 */
1118 memset(softc->wwpn_iid, 0, sizeof(softc->wwpn_iid));
1119
1120 /*
1121 * Initialize the ioctl front end.
1122 */
1123 fe = &softc->ioctl_info.fe;
1124 sprintf(softc->ioctl_info.port_name, "CTL ioctl");
1125 fe->port_type = CTL_PORT_IOCTL;
1126 fe->num_requested_ctl_io = 100;
1127 fe->port_name = softc->ioctl_info.port_name;
1128 fe->port_online = ctl_ioctl_online;
1129 fe->port_offline = ctl_ioctl_offline;
1130 fe->onoff_arg = &softc->ioctl_info;
1131 fe->targ_enable = ctl_ioctl_targ_enable;
1132 fe->targ_disable = ctl_ioctl_targ_disable;
1133 fe->lun_enable = ctl_ioctl_lun_enable;
1134 fe->lun_disable = ctl_ioctl_lun_disable;
1135 fe->targ_lun_arg = &softc->ioctl_info;
1136 fe->fe_datamove = ctl_ioctl_datamove;
1137 fe->fe_done = ctl_ioctl_done;
1138 fe->max_targets = 15;
1139 fe->max_target_id = 15;
1140
1141 if (ctl_frontend_register(&softc->ioctl_info.fe,
1142 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) {
1143 printf("ctl: ioctl front end registration failed, will "
1144 "continue anyway\n");
1145 }
1146
1147#ifdef CTL_IO_DELAY
1148 if (sizeof(struct callout) > CTL_TIMER_BYTES) {
1149 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n",
1150 sizeof(struct callout), CTL_TIMER_BYTES);
1151 return (EINVAL);
1152 }
1153#endif /* CTL_IO_DELAY */
1154
1155 return (0);
1156}
1157
1158void
1159ctl_shutdown(void)
1160{
1161 struct ctl_softc *softc;
1162 struct ctl_lun *lun, *next_lun;
1163 struct ctl_io_pool *pool;
1164
1165 softc = (struct ctl_softc *)control_softc;
1166
1167 if (ctl_frontend_deregister(&softc->ioctl_info.fe) != 0)
1168 printf("ctl: ioctl front end deregistration failed\n");
1169
1170 mtx_lock(&softc->ctl_lock);
1171
1172 /*
1173 * Free up each LUN.
1174 */
1175 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1176 next_lun = STAILQ_NEXT(lun, links);
1177 ctl_free_lun(lun);
1178 }
1179
1180 mtx_unlock(&softc->ctl_lock);
1181
1182 /*
1183 * This will rip the rug out from under any FETDs or anyone else
1184 * that has a pool allocated. Since we increment our module
1185 * refcount any time someone outside the main CTL module allocates
1186 * a pool, we shouldn't have any problems here. The user won't be
1187 * able to unload the CTL module until client modules have
1188 * successfully unloaded.
1189 */
1190 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL)
1191 ctl_pool_free(pool);
1192
1193#if 0
1194 ctl_shutdown_thread(softc->work_thread);
1195#endif
1196
1197 mtx_destroy(&softc->pool_lock);
1198 mtx_destroy(&softc->ctl_lock);
1199
1200 destroy_dev(softc->dev);
1201
1202 sysctl_ctx_free(&softc->sysctl_ctx);
1203
1204 free(control_softc, M_DEVBUF);
1205 control_softc = NULL;
1206
1207 if (bootverbose)
1208 printf("ctl: CAM Target Layer unloaded\n");
1209}
1210
1211static int
1212ctl_module_event_handler(module_t mod, int what, void *arg)
1213{
1214
1215 switch (what) {
1216 case MOD_LOAD:
1217 return (ctl_init());
1218 case MOD_UNLOAD:
1219 return (EBUSY);
1220 default:
1221 return (EOPNOTSUPP);
1222 }
1223}
1224
1225/*
1226 * XXX KDM should we do some access checks here? Bump a reference count to
1227 * prevent a CTL module from being unloaded while someone has it open?
1228 */
1229static int
1230ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1231{
1232 return (0);
1233}
1234
1235static int
1236ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1237{
1238 return (0);
1239}
1240
1241int
1242ctl_port_enable(ctl_port_type port_type)
1243{
1244 struct ctl_softc *softc;
1245 struct ctl_frontend *fe;
1246
1247 if (ctl_is_single == 0) {
1248 union ctl_ha_msg msg_info;
1249 int isc_retval;
1250
1251#if 0
1252 printf("%s: HA mode, synchronizing frontend enable\n",
1253 __func__);
1254#endif
1255 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE;
1256 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1257 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) {
1258 printf("Sync msg send error retval %d\n", isc_retval);
1259 }
1260 if (!rcv_sync_msg) {
1261 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
1262 sizeof(msg_info), 1);
1263 }
1264#if 0
1265 printf("CTL:Frontend Enable\n");
1266 } else {
1267 printf("%s: single mode, skipping frontend synchronization\n",
1268 __func__);
1269#endif
1270 }
1271
1272 softc = control_softc;
1273
1274 STAILQ_FOREACH(fe, &softc->fe_list, links) {
1275 if (port_type & fe->port_type)
1276 {
1277#if 0
1278 printf("port %d\n", fe->targ_port);
1279#endif
1280 ctl_frontend_online(fe);
1281 }
1282 }
1283
1284 return (0);
1285}
1286
1287int
1288ctl_port_disable(ctl_port_type port_type)
1289{
1290 struct ctl_softc *softc;
1291 struct ctl_frontend *fe;
1292
1293 softc = control_softc;
1294
1295 STAILQ_FOREACH(fe, &softc->fe_list, links) {
1296 if (port_type & fe->port_type)
1297 ctl_frontend_offline(fe);
1298 }
1299
1300 return (0);
1301}
1302
1303/*
1304 * Returns 0 for success, 1 for failure.
1305 * Currently the only failure mode is if there aren't enough entries
1306 * allocated. So, in case of a failure, look at num_entries_dropped,
1307 * reallocate and try again.
1308 */
1309int
1310ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced,
1311 int *num_entries_filled, int *num_entries_dropped,
1312 ctl_port_type port_type, int no_virtual)
1313{
1314 struct ctl_softc *softc;
1315 struct ctl_frontend *fe;
1316 int entries_dropped, entries_filled;
1317 int retval;
1318 int i;
1319
1320 softc = control_softc;
1321
1322 retval = 0;
1323 entries_filled = 0;
1324 entries_dropped = 0;
1325
1326 i = 0;
1327 mtx_lock(&softc->ctl_lock);
1328 STAILQ_FOREACH(fe, &softc->fe_list, links) {
1329 struct ctl_port_entry *entry;
1330
1331 if ((fe->port_type & port_type) == 0)
1332 continue;
1333
1334 if ((no_virtual != 0)
1335 && (fe->virtual_port != 0))
1336 continue;
1337
1338 if (entries_filled >= num_entries_alloced) {
1339 entries_dropped++;
1340 continue;
1341 }
1342 entry = &entries[i];
1343
1344 entry->port_type = fe->port_type;
1345 strlcpy(entry->port_name, fe->port_name,
1346 sizeof(entry->port_name));
1347 entry->physical_port = fe->physical_port;
1348 entry->virtual_port = fe->virtual_port;
1349 entry->wwnn = fe->wwnn;
1350 entry->wwpn = fe->wwpn;
1351
1352 i++;
1353 entries_filled++;
1354 }
1355
1356 mtx_unlock(&softc->ctl_lock);
1357
1358 if (entries_dropped > 0)
1359 retval = 1;
1360
1361 *num_entries_dropped = entries_dropped;
1362 *num_entries_filled = entries_filled;
1363
1364 return (retval);
1365}
1366
1367static void
1368ctl_ioctl_online(void *arg)
1369{
1370 struct ctl_ioctl_info *ioctl_info;
1371
1372 ioctl_info = (struct ctl_ioctl_info *)arg;
1373
1374 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED;
1375}
1376
1377static void
1378ctl_ioctl_offline(void *arg)
1379{
1380 struct ctl_ioctl_info *ioctl_info;
1381
1382 ioctl_info = (struct ctl_ioctl_info *)arg;
1383
1384 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED;
1385}
1386
1387/*
1388 * Remove an initiator by port number and initiator ID.
1389 * Returns 0 for success, 1 for failure.
1390 */
1391int
1392ctl_remove_initiator(int32_t targ_port, uint32_t iid)
1393{
1394 struct ctl_softc *softc;
1395
1396 softc = control_softc;
1397
1398 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1399
1400 if ((targ_port < 0)
1401 || (targ_port > CTL_MAX_PORTS)) {
1402 printf("%s: invalid port number %d\n", __func__, targ_port);
1403 return (1);
1404 }
1405 if (iid > CTL_MAX_INIT_PER_PORT) {
1406 printf("%s: initiator ID %u > maximun %u!\n",
1407 __func__, iid, CTL_MAX_INIT_PER_PORT);
1408 return (1);
1409 }
1410
1411 mtx_lock(&softc->ctl_lock);
1412
1413 softc->wwpn_iid[targ_port][iid].in_use = 0;
1414
1415 mtx_unlock(&softc->ctl_lock);
1416
1417 return (0);
1418}
1419
1420/*
1421 * Add an initiator to the initiator map.
1422 * Returns 0 for success, 1 for failure.
1423 */
1424int
1425ctl_add_initiator(uint64_t wwpn, int32_t targ_port, uint32_t iid)
1426{
1427 struct ctl_softc *softc;
1428 int retval;
1429
1430 softc = control_softc;
1431
1432 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1433
1434 retval = 0;
1435
1436 if ((targ_port < 0)
1437 || (targ_port > CTL_MAX_PORTS)) {
1438 printf("%s: invalid port number %d\n", __func__, targ_port);
1439 return (1);
1440 }
1441 if (iid > CTL_MAX_INIT_PER_PORT) {
1442 printf("%s: WWPN %#jx initiator ID %u > maximun %u!\n",
1443 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1444 return (1);
1445 }
1446
1447 mtx_lock(&softc->ctl_lock);
1448
1449 if (softc->wwpn_iid[targ_port][iid].in_use != 0) {
1450 /*
1451 * We don't treat this as an error.
1452 */
1453 if (softc->wwpn_iid[targ_port][iid].wwpn == wwpn) {
1454 printf("%s: port %d iid %u WWPN %#jx arrived again?\n",
1455 __func__, targ_port, iid, (uintmax_t)wwpn);
1456 goto bailout;
1457 }
1458
1459 /*
1460 * This is an error, but what do we do about it? The
1461 * driver is telling us we have a new WWPN for this
1462 * initiator ID, so we pretty much need to use it.
1463 */
1464 printf("%s: port %d iid %u WWPN %#jx arrived, WWPN %#jx is "
1465 "still at that address\n", __func__, targ_port, iid,
1466 (uintmax_t)wwpn,
1467 (uintmax_t)softc->wwpn_iid[targ_port][iid].wwpn);
1468
1469 /*
1470 * XXX KDM clear have_ca and ua_pending on each LUN for
1471 * this initiator.
1472 */
1473 }
1474 softc->wwpn_iid[targ_port][iid].in_use = 1;
1475 softc->wwpn_iid[targ_port][iid].iid = iid;
1476 softc->wwpn_iid[targ_port][iid].wwpn = wwpn;
1477 softc->wwpn_iid[targ_port][iid].port = targ_port;
1478
1479bailout:
1480
1481 mtx_unlock(&softc->ctl_lock);
1482
1483 return (retval);
1484}
1485
1486/*
1487 * XXX KDM should we pretend to do something in the target/lun
1488 * enable/disable functions?
1489 */
1490static int
1491ctl_ioctl_targ_enable(void *arg, struct ctl_id targ_id)
1492{
1493 return (0);
1494}
1495
1496static int
1497ctl_ioctl_targ_disable(void *arg, struct ctl_id targ_id)
1498{
1499 return (0);
1500}
1501
1502static int
1503ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
1504{
1505 return (0);
1506}
1507
1508static int
1509ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
1510{
1511 return (0);
1512}
1513
1514/*
1515 * Data movement routine for the CTL ioctl frontend port.
1516 */
1517static int
1518ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio)
1519{
1520 struct ctl_sg_entry *ext_sglist, *kern_sglist;
1521 struct ctl_sg_entry ext_entry, kern_entry;
1522 int ext_sglen, ext_sg_entries, kern_sg_entries;
1523 int ext_sg_start, ext_offset;
1524 int len_to_copy, len_copied;
1525 int kern_watermark, ext_watermark;
1526 int ext_sglist_malloced;
1527 int i, j;
1528
1529 ext_sglist_malloced = 0;
1530 ext_sg_start = 0;
1531 ext_offset = 0;
1532
1533 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n"));
1534
1535 /*
1536 * If this flag is set, fake the data transfer.
1537 */
1538 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) {
1539 ctsio->ext_data_filled = ctsio->ext_data_len;
1540 goto bailout;
1541 }
1542
1543 /*
1544 * To simplify things here, if we have a single buffer, stick it in
1545 * a S/G entry and just make it a single entry S/G list.
1546 */
1547 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
1548 int len_seen;
1549
1550 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
1551
1552 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL,
1553 M_WAITOK);
1554 ext_sglist_malloced = 1;
1555 if (copyin(ctsio->ext_data_ptr, ext_sglist,
1556 ext_sglen) != 0) {
1557 ctl_set_internal_failure(ctsio,
1558 /*sks_valid*/ 0,
1559 /*retry_count*/ 0);
1560 goto bailout;
1561 }
1562 ext_sg_entries = ctsio->ext_sg_entries;
1563 len_seen = 0;
1564 for (i = 0; i < ext_sg_entries; i++) {
1565 if ((len_seen + ext_sglist[i].len) >=
1566 ctsio->ext_data_filled) {
1567 ext_sg_start = i;
1568 ext_offset = ctsio->ext_data_filled - len_seen;
1569 break;
1570 }
1571 len_seen += ext_sglist[i].len;
1572 }
1573 } else {
1574 ext_sglist = &ext_entry;
1575 ext_sglist->addr = ctsio->ext_data_ptr;
1576 ext_sglist->len = ctsio->ext_data_len;
1577 ext_sg_entries = 1;
1578 ext_sg_start = 0;
1579 ext_offset = ctsio->ext_data_filled;
1580 }
1581
1582 if (ctsio->kern_sg_entries > 0) {
1583 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
1584 kern_sg_entries = ctsio->kern_sg_entries;
1585 } else {
1586 kern_sglist = &kern_entry;
1587 kern_sglist->addr = ctsio->kern_data_ptr;
1588 kern_sglist->len = ctsio->kern_data_len;
1589 kern_sg_entries = 1;
1590 }
1591
1592
1593 kern_watermark = 0;
1594 ext_watermark = ext_offset;
1595 len_copied = 0;
1596 for (i = ext_sg_start, j = 0;
1597 i < ext_sg_entries && j < kern_sg_entries;) {
1598 uint8_t *ext_ptr, *kern_ptr;
1599
1600 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark,
1601 kern_sglist[j].len - kern_watermark);
1602
1603 ext_ptr = (uint8_t *)ext_sglist[i].addr;
1604 ext_ptr = ext_ptr + ext_watermark;
1605 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
1606 /*
1607 * XXX KDM fix this!
1608 */
1609 panic("need to implement bus address support");
1610#if 0
1611 kern_ptr = bus_to_virt(kern_sglist[j].addr);
1612#endif
1613 } else
1614 kern_ptr = (uint8_t *)kern_sglist[j].addr;
1615 kern_ptr = kern_ptr + kern_watermark;
1616
1617 kern_watermark += len_to_copy;
1618 ext_watermark += len_to_copy;
1619
1620 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
1621 CTL_FLAG_DATA_IN) {
1622 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1623 "bytes to user\n", len_to_copy));
1624 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1625 "to %p\n", kern_ptr, ext_ptr));
1626 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) {
1627 ctl_set_internal_failure(ctsio,
1628 /*sks_valid*/ 0,
1629 /*retry_count*/ 0);
1630 goto bailout;
1631 }
1632 } else {
1633 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1634 "bytes from user\n", len_to_copy));
1635 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1636 "to %p\n", ext_ptr, kern_ptr));
1637 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){
1638 ctl_set_internal_failure(ctsio,
1639 /*sks_valid*/ 0,
1640 /*retry_count*/0);
1641 goto bailout;
1642 }
1643 }
1644
1645 len_copied += len_to_copy;
1646
1647 if (ext_sglist[i].len == ext_watermark) {
1648 i++;
1649 ext_watermark = 0;
1650 }
1651
1652 if (kern_sglist[j].len == kern_watermark) {
1653 j++;
1654 kern_watermark = 0;
1655 }
1656 }
1657
1658 ctsio->ext_data_filled += len_copied;
1659
1660 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, "
1661 "kern_sg_entries: %d\n", ext_sg_entries,
1662 kern_sg_entries));
1663 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, "
1664 "kern_data_len = %d\n", ctsio->ext_data_len,
1665 ctsio->kern_data_len));
1666
1667
1668 /* XXX KDM set residual?? */
1669bailout:
1670
1671 if (ext_sglist_malloced != 0)
1672 free(ext_sglist, M_CTL);
1673
1674 return (CTL_RETVAL_COMPLETE);
1675}
1676
1677/*
1678 * Serialize a command that went down the "wrong" side, and so was sent to
1679 * this controller for execution. The logic is a little different than the
1680 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
1681 * sent back to the other side, but in the success case, we execute the
1682 * command on this side (XFER mode) or tell the other side to execute it
1683 * (SER_ONLY mode).
1684 */
1685static int
1686ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio, int have_lock)
1687{
1688 struct ctl_softc *ctl_softc;
1689 union ctl_ha_msg msg_info;
1690 struct ctl_lun *lun;
1691 int retval = 0;
1692 uint32_t targ_lun;
1693
1694 ctl_softc = control_softc;
1695 if (have_lock == 0)
1696 mtx_lock(&ctl_softc->ctl_lock);
1697
1698 targ_lun = ctsio->io_hdr.nexus.targ_lun;
1699 if (ctsio->io_hdr.nexus.lun_map_fn != NULL)
1700 targ_lun = ctsio->io_hdr.nexus.lun_map_fn(ctsio->io_hdr.nexus.lun_map_arg, targ_lun);
1701 lun = ctl_softc->ctl_luns[targ_lun];
1702 if (lun==NULL)
1703 {
1704 /*
1705 * Why isn't LUN defined? The other side wouldn't
1706 * send a cmd if the LUN is undefined.
1707 */
1708 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__);
1709
1710 /* "Logical unit not supported" */
1711 ctl_set_sense_data(&msg_info.scsi.sense_data,
1712 lun,
1713 /*sense_format*/SSD_TYPE_NONE,
1714 /*current_error*/ 1,
1715 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1716 /*asc*/ 0x25,
1717 /*ascq*/ 0x00,
1718 SSD_ELEM_NONE);
1719
1720 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1721 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1722 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1723 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1724 msg_info.hdr.serializing_sc = NULL;
1725 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1726 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1727 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1728 }
1729 if (have_lock == 0)
1730 mtx_unlock(&ctl_softc->ctl_lock);
1731 return(1);
1732
1733 }
1734
1735 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1736
1737 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
1738 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
1739 ooa_links))) {
1740 case CTL_ACTION_BLOCK:
1741 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
1742 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
1743 blocked_links);
1744 break;
1745 case CTL_ACTION_PASS:
1746 case CTL_ACTION_SKIP:
1747 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
1748 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
1749 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue,
1750 &ctsio->io_hdr, links);
1751 } else {
1752
1753 /* send msg back to other side */
1754 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1755 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
1756 msg_info.hdr.msg_type = CTL_MSG_R2R;
1757#if 0
1758 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc);
1759#endif
1760 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1761 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1762 }
1763 }
1764 break;
1765 case CTL_ACTION_OVERLAP:
1766 /* OVERLAPPED COMMANDS ATTEMPTED */
1767 ctl_set_sense_data(&msg_info.scsi.sense_data,
1768 lun,
1769 /*sense_format*/SSD_TYPE_NONE,
1770 /*current_error*/ 1,
1771 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1772 /*asc*/ 0x4E,
1773 /*ascq*/ 0x00,
1774 SSD_ELEM_NONE);
1775
1776 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1777 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1778 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1779 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1780 msg_info.hdr.serializing_sc = NULL;
1781 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1782#if 0
1783 printf("BAD JUJU:Major Bummer Overlap\n");
1784#endif
1785 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1786 retval = 1;
1787 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1788 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1789 }
1790 break;
1791 case CTL_ACTION_OVERLAP_TAG:
1792 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
1793 ctl_set_sense_data(&msg_info.scsi.sense_data,
1794 lun,
1795 /*sense_format*/SSD_TYPE_NONE,
1796 /*current_error*/ 1,
1797 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1798 /*asc*/ 0x4D,
1799 /*ascq*/ ctsio->tag_num & 0xff,
1800 SSD_ELEM_NONE);
1801
1802 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1803 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1804 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1805 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1806 msg_info.hdr.serializing_sc = NULL;
1807 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1808#if 0
1809 printf("BAD JUJU:Major Bummer Overlap Tag\n");
1810#endif
1811 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1812 retval = 1;
1813 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1814 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1815 }
1816 break;
1817 case CTL_ACTION_ERROR:
1818 default:
1819 /* "Internal target failure" */
1820 ctl_set_sense_data(&msg_info.scsi.sense_data,
1821 lun,
1822 /*sense_format*/SSD_TYPE_NONE,
1823 /*current_error*/ 1,
1824 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
1825 /*asc*/ 0x44,
1826 /*ascq*/ 0x00,
1827 SSD_ELEM_NONE);
1828
1829 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1830 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1831 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1832 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1833 msg_info.hdr.serializing_sc = NULL;
1834 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1835#if 0
1836 printf("BAD JUJU:Major Bummer HW Error\n");
1837#endif
1838 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1839 retval = 1;
1840 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1841 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1842 }
1843 break;
1844 }
1845 if (have_lock == 0)
1846 mtx_unlock(&ctl_softc->ctl_lock);
1847 return (retval);
1848}
1849
1850static int
1851ctl_ioctl_submit_wait(union ctl_io *io)
1852{
1853 struct ctl_fe_ioctl_params params;
1854 ctl_fe_ioctl_state last_state;
1855 int done, retval;
1856
1857 retval = 0;
1858
1859 bzero(&params, sizeof(params));
1860
1861 mtx_init(&params.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF);
1862 cv_init(&params.sem, "ctlioccv");
1863 params.state = CTL_IOCTL_INPROG;
1864 last_state = params.state;
1865
1866 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = &params;
1867
1868 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n"));
1869
1870 /* This shouldn't happen */
1871 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE)
1872 return (retval);
1873
1874 done = 0;
1875
1876 do {
1877 mtx_lock(&params.ioctl_mtx);
1878 /*
1879 * Check the state here, and don't sleep if the state has
1880 * already changed (i.e. wakeup has already occured, but we
1881 * weren't waiting yet).
1882 */
1883 if (params.state == last_state) {
1884 /* XXX KDM cv_wait_sig instead? */
1885 cv_wait(&params.sem, &params.ioctl_mtx);
1886 }
1887 last_state = params.state;
1888
1889 switch (params.state) {
1890 case CTL_IOCTL_INPROG:
1891 /* Why did we wake up? */
1892 /* XXX KDM error here? */
1893 mtx_unlock(&params.ioctl_mtx);
1894 break;
1895 case CTL_IOCTL_DATAMOVE:
1896 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n"));
1897
1898 /*
1899 * change last_state back to INPROG to avoid
1900 * deadlock on subsequent data moves.
1901 */
1902 params.state = last_state = CTL_IOCTL_INPROG;
1903
1904 mtx_unlock(&params.ioctl_mtx);
1905 ctl_ioctl_do_datamove(&io->scsiio);
1906 /*
1907 * Note that in some cases, most notably writes,
1908 * this will queue the I/O and call us back later.
1909 * In other cases, generally reads, this routine
1910 * will immediately call back and wake us up,
1911 * probably using our own context.
1912 */
1913 io->scsiio.be_move_done(io);
1914 break;
1915 case CTL_IOCTL_DONE:
1916 mtx_unlock(&params.ioctl_mtx);
1917 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n"));
1918 done = 1;
1919 break;
1920 default:
1921 mtx_unlock(&params.ioctl_mtx);
1922 /* XXX KDM error here? */
1923 break;
1924 }
1925 } while (done == 0);
1926
1927 mtx_destroy(&params.ioctl_mtx);
1928 cv_destroy(&params.sem);
1929
1930 return (CTL_RETVAL_COMPLETE);
1931}
1932
1933static void
1934ctl_ioctl_datamove(union ctl_io *io)
1935{
1936 struct ctl_fe_ioctl_params *params;
1937
1938 params = (struct ctl_fe_ioctl_params *)
1939 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1940
1941 mtx_lock(&params->ioctl_mtx);
1942 params->state = CTL_IOCTL_DATAMOVE;
1943 cv_broadcast(&params->sem);
1944 mtx_unlock(&params->ioctl_mtx);
1945}
1946
1947static void
1948ctl_ioctl_done(union ctl_io *io)
1949{
1950 struct ctl_fe_ioctl_params *params;
1951
1952 params = (struct ctl_fe_ioctl_params *)
1953 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1954
1955 mtx_lock(&params->ioctl_mtx);
1956 params->state = CTL_IOCTL_DONE;
1957 cv_broadcast(&params->sem);
1958 mtx_unlock(&params->ioctl_mtx);
1959}
1960
1961static void
1962ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask)
1963{
1964 struct ctl_fe_ioctl_startstop_info *sd_info;
1965
1966 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg;
1967
1968 sd_info->hs_info.status = metatask->status;
1969 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns;
1970 sd_info->hs_info.luns_complete =
1971 metatask->taskinfo.startstop.luns_complete;
1972 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed;
1973
1974 cv_broadcast(&sd_info->sem);
1975}
1976
1977static void
1978ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask)
1979{
1980 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info;
1981
1982 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg;
1983
1984 mtx_lock(fe_bbr_info->lock);
1985 fe_bbr_info->bbr_info->status = metatask->status;
1986 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
1987 fe_bbr_info->wakeup_done = 1;
1988 mtx_unlock(fe_bbr_info->lock);
1989
1990 cv_broadcast(&fe_bbr_info->sem);
1991}
1992
1993/*
1994 * Returns 0 for success, errno for failure.
1995 */
1996static int
1997ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
1998 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
1999{
2000 union ctl_io *io;
2001 int retval;
2002
2003 retval = 0;
2004
2005 mtx_assert(&control_softc->ctl_lock, MA_OWNED);
2006
2007 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2008 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2009 ooa_links)) {
2010 struct ctl_ooa_entry *entry;
2011
2012 /*
2013 * If we've got more than we can fit, just count the
2014 * remaining entries.
2015 */
2016 if (*cur_fill_num >= ooa_hdr->alloc_num)
2017 continue;
2018
2019 entry = &kern_entries[*cur_fill_num];
2020
2021 entry->tag_num = io->scsiio.tag_num;
2022 entry->lun_num = lun->lun;
2023#ifdef CTL_TIME_IO
2024 entry->start_bt = io->io_hdr.start_bt;
2025#endif
2026 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2027 entry->cdb_len = io->scsiio.cdb_len;
2028 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2029 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2030
2031 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2032 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2033
2034 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2035 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2036
2037 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2038 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2039
2040 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2041 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2042 }
2043
2044 return (retval);
2045}
2046
2047static void *
2048ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2049 size_t error_str_len)
2050{
2051 void *kptr;
2052
2053 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2054
2055 if (copyin(user_addr, kptr, len) != 0) {
2056 snprintf(error_str, error_str_len, "Error copying %d bytes "
2057 "from user address %p to kernel address %p", len,
2058 user_addr, kptr);
2059 free(kptr, M_CTL);
2060 return (NULL);
2061 }
2062
2063 return (kptr);
2064}
2065
2066static void
2067ctl_free_args(int num_be_args, struct ctl_be_arg *be_args)
2068{
2069 int i;
2070
2071 if (be_args == NULL)
2072 return;
2073
2074 for (i = 0; i < num_be_args; i++) {
2075 free(be_args[i].kname, M_CTL);
2076 free(be_args[i].kvalue, M_CTL);
2077 }
2078
2079 free(be_args, M_CTL);
2080}
2081
2082static struct ctl_be_arg *
2083ctl_copyin_args(int num_be_args, struct ctl_be_arg *be_args,
2084 char *error_str, size_t error_str_len)
2085{
2086 struct ctl_be_arg *args;
2087 int i;
2088
2089 args = ctl_copyin_alloc(be_args, num_be_args * sizeof(*be_args),
2090 error_str, error_str_len);
2091
2092 if (args == NULL)
2093 goto bailout;
2094
2095 for (i = 0; i < num_be_args; i++) {
2096 args[i].kname = NULL;
2097 args[i].kvalue = NULL;
2098 }
2099
2100 for (i = 0; i < num_be_args; i++) {
2101 uint8_t *tmpptr;
2102
2103 args[i].kname = ctl_copyin_alloc(args[i].name,
2104 args[i].namelen, error_str, error_str_len);
2105 if (args[i].kname == NULL)
2106 goto bailout;
2107
2108 if (args[i].kname[args[i].namelen - 1] != '\0') {
2109 snprintf(error_str, error_str_len, "Argument %d "
2110 "name is not NUL-terminated", i);
2111 goto bailout;
2112 }
2113
2114 args[i].kvalue = NULL;
2115
2116 tmpptr = ctl_copyin_alloc(args[i].value,
2117 args[i].vallen, error_str, error_str_len);
2118 if (tmpptr == NULL)
2119 goto bailout;
2120
2121 args[i].kvalue = tmpptr;
2122
2123 if ((args[i].flags & CTL_BEARG_ASCII)
2124 && (tmpptr[args[i].vallen - 1] != '\0')) {
2125 snprintf(error_str, error_str_len, "Argument %d "
2126 "value is not NUL-terminated", i);
2127 goto bailout;
2128 }
2129 }
2130
2131 return (args);
2132bailout:
2133
2134 ctl_free_args(num_be_args, args);
2135
2136 return (NULL);
2137}
2138
2139/*
2140 * Escape characters that are illegal or not recommended in XML.
2141 */
2142int
2143ctl_sbuf_printf_esc(struct sbuf *sb, char *str)
2144{
2145 int retval;
2146
2147 retval = 0;
2148
2149 for (; *str; str++) {
2150 switch (*str) {
2151 case '&':
2152 retval = sbuf_printf(sb, "&amp;");
2153 break;
2154 case '>':
2155 retval = sbuf_printf(sb, "&gt;");
2156 break;
2157 case '<':
2158 retval = sbuf_printf(sb, "&lt;");
2159 break;
2160 default:
2161 retval = sbuf_putc(sb, *str);
2162 break;
2163 }
2164
2165 if (retval != 0)
2166 break;
2167
2168 }
2169
2170 return (retval);
2171}
2172
2173static int
2174ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2175 struct thread *td)
2176{
2177 struct ctl_softc *softc;
2178 int retval;
2179
2180 softc = control_softc;
2181
2182 retval = 0;
2183
2184 switch (cmd) {
2185 case CTL_IO: {
2186 union ctl_io *io;
2187 void *pool_tmp;
2188
2189 /*
2190 * If we haven't been "enabled", don't allow any SCSI I/O
2191 * to this FETD.
2192 */
2193 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) {
2194 retval = -EPERM;
2195 break;
2196 }
2197
2198 io = ctl_alloc_io(softc->ioctl_info.fe.ctl_pool_ref);
2199 if (io == NULL) {
2200 printf("ctl_ioctl: can't allocate ctl_io!\n");
2201 retval = -ENOSPC;
2202 break;
2203 }
2204
2205 /*
2206 * Need to save the pool reference so it doesn't get
2207 * spammed by the user's ctl_io.
2208 */
2209 pool_tmp = io->io_hdr.pool;
2210
2211 memcpy(io, (void *)addr, sizeof(*io));
2212
2213 io->io_hdr.pool = pool_tmp;
2214 /*
2215 * No status yet, so make sure the status is set properly.
2216 */
2217 io->io_hdr.status = CTL_STATUS_NONE;
2218
2219 /*
2220 * The user sets the initiator ID, target and LUN IDs.
2221 */
2222 io->io_hdr.nexus.targ_port = softc->ioctl_info.fe.targ_port;
2223 io->io_hdr.flags |= CTL_FLAG_USER_REQ;
2224 if ((io->io_hdr.io_type == CTL_IO_SCSI)
2225 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED))
2226 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++;
2227
2228 retval = ctl_ioctl_submit_wait(io);
2229
2230 if (retval != 0) {
2231 ctl_free_io(io);
2232 break;
2233 }
2234
2235 memcpy((void *)addr, io, sizeof(*io));
2236
2237 /* return this to our pool */
2238 ctl_free_io(io);
2239
2240 break;
2241 }
2242 case CTL_ENABLE_PORT:
2243 case CTL_DISABLE_PORT:
2244 case CTL_SET_PORT_WWNS: {
2245 struct ctl_frontend *fe;
2246 struct ctl_port_entry *entry;
2247
2248 entry = (struct ctl_port_entry *)addr;
2249
2250 mtx_lock(&softc->ctl_lock);
2251 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2252 int action, done;
2253
2254 action = 0;
2255 done = 0;
2256
2257 if ((entry->port_type == CTL_PORT_NONE)
2258 && (entry->targ_port == fe->targ_port)) {
2259 /*
2260 * If the user only wants to enable or
2261 * disable or set WWNs on a specific port,
2262 * do the operation and we're done.
2263 */
2264 action = 1;
2265 done = 1;
2266 } else if (entry->port_type & fe->port_type) {
2267 /*
2268 * Compare the user's type mask with the
2269 * particular frontend type to see if we
2270 * have a match.
2271 */
2272 action = 1;
2273 done = 0;
2274
2275 /*
2276 * Make sure the user isn't trying to set
2277 * WWNs on multiple ports at the same time.
2278 */
2279 if (cmd == CTL_SET_PORT_WWNS) {
2280 printf("%s: Can't set WWNs on "
2281 "multiple ports\n", __func__);
2282 retval = EINVAL;
2283 break;
2284 }
2285 }
2286 if (action != 0) {
2287 /*
2288 * XXX KDM we have to drop the lock here,
2289 * because the online/offline operations
2290 * can potentially block. We need to
2291 * reference count the frontends so they
2292 * can't go away,
2293 */
2294 mtx_unlock(&softc->ctl_lock);
2295
2296 if (cmd == CTL_ENABLE_PORT) {
2297 struct ctl_lun *lun;
2298
2299 STAILQ_FOREACH(lun, &softc->lun_list,
2300 links) {
2301 fe->lun_enable(fe->targ_lun_arg,
2302 lun->target,
2303 lun->lun);
2304 }
2305
2306 ctl_frontend_online(fe);
2307 } else if (cmd == CTL_DISABLE_PORT) {
2308 struct ctl_lun *lun;
2309
2310 ctl_frontend_offline(fe);
2311
2312 STAILQ_FOREACH(lun, &softc->lun_list,
2313 links) {
2314 fe->lun_disable(
2315 fe->targ_lun_arg,
2316 lun->target,
2317 lun->lun);
2318 }
2319 }
2320
2321 mtx_lock(&softc->ctl_lock);
2322
2323 if (cmd == CTL_SET_PORT_WWNS)
2324 ctl_frontend_set_wwns(fe,
2325 (entry->flags & CTL_PORT_WWNN_VALID) ?
2326 1 : 0, entry->wwnn,
2327 (entry->flags & CTL_PORT_WWPN_VALID) ?
2328 1 : 0, entry->wwpn);
2329 }
2330 if (done != 0)
2331 break;
2332 }
2333 mtx_unlock(&softc->ctl_lock);
2334 break;
2335 }
2336 case CTL_GET_PORT_LIST: {
2337 struct ctl_frontend *fe;
2338 struct ctl_port_list *list;
2339 int i;
2340
2341 list = (struct ctl_port_list *)addr;
2342
2343 if (list->alloc_len != (list->alloc_num *
2344 sizeof(struct ctl_port_entry))) {
2345 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2346 "alloc_num %u * sizeof(struct ctl_port_entry) "
2347 "%zu\n", __func__, list->alloc_len,
2348 list->alloc_num, sizeof(struct ctl_port_entry));
2349 retval = EINVAL;
2350 break;
2351 }
2352 list->fill_len = 0;
2353 list->fill_num = 0;
2354 list->dropped_num = 0;
2355 i = 0;
2356 mtx_lock(&softc->ctl_lock);
2357 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2358 struct ctl_port_entry entry, *list_entry;
2359
2360 if (list->fill_num >= list->alloc_num) {
2361 list->dropped_num++;
2362 continue;
2363 }
2364
2365 entry.port_type = fe->port_type;
2366 strlcpy(entry.port_name, fe->port_name,
2367 sizeof(entry.port_name));
2368 entry.targ_port = fe->targ_port;
2369 entry.physical_port = fe->physical_port;
2370 entry.virtual_port = fe->virtual_port;
2371 entry.wwnn = fe->wwnn;
2372 entry.wwpn = fe->wwpn;
2373 if (fe->status & CTL_PORT_STATUS_ONLINE)
2374 entry.online = 1;
2375 else
2376 entry.online = 0;
2377
2378 list_entry = &list->entries[i];
2379
2380 retval = copyout(&entry, list_entry, sizeof(entry));
2381 if (retval != 0) {
2382 printf("%s: CTL_GET_PORT_LIST: copyout "
2383 "returned %d\n", __func__, retval);
2384 break;
2385 }
2386 i++;
2387 list->fill_num++;
2388 list->fill_len += sizeof(entry);
2389 }
2390 mtx_unlock(&softc->ctl_lock);
2391
2392 /*
2393 * If this is non-zero, we had a copyout fault, so there's
2394 * probably no point in attempting to set the status inside
2395 * the structure.
2396 */
2397 if (retval != 0)
2398 break;
2399
2400 if (list->dropped_num > 0)
2401 list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2402 else
2403 list->status = CTL_PORT_LIST_OK;
2404 break;
2405 }
2406 case CTL_DUMP_OOA: {
2407 struct ctl_lun *lun;
2408 union ctl_io *io;
2409 char printbuf[128];
2410 struct sbuf sb;
2411
2412 mtx_lock(&softc->ctl_lock);
2413 printf("Dumping OOA queues:\n");
2414 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2415 for (io = (union ctl_io *)TAILQ_FIRST(
2416 &lun->ooa_queue); io != NULL;
2417 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2418 ooa_links)) {
2419 sbuf_new(&sb, printbuf, sizeof(printbuf),
2420 SBUF_FIXEDLEN);
2421 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2422 (intmax_t)lun->lun,
2423 io->scsiio.tag_num,
2424 (io->io_hdr.flags &
2425 CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2426 (io->io_hdr.flags &
2427 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2428 (io->io_hdr.flags &
2429 CTL_FLAG_ABORT) ? " ABORT" : "",
2430 (io->io_hdr.flags &
2431 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2432 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2433 sbuf_finish(&sb);
2434 printf("%s\n", sbuf_data(&sb));
2435 }
2436 }
2437 printf("OOA queues dump done\n");
2438 mtx_unlock(&softc->ctl_lock);
2439 break;
2440 }
2441 case CTL_GET_OOA: {
2442 struct ctl_lun *lun;
2443 struct ctl_ooa *ooa_hdr;
2444 struct ctl_ooa_entry *entries;
2445 uint32_t cur_fill_num;
2446
2447 ooa_hdr = (struct ctl_ooa *)addr;
2448
2449 if ((ooa_hdr->alloc_len == 0)
2450 || (ooa_hdr->alloc_num == 0)) {
2451 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2452 "must be non-zero\n", __func__,
2453 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2454 retval = EINVAL;
2455 break;
2456 }
2457
2458 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2459 sizeof(struct ctl_ooa_entry))) {
2460 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2461 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2462 __func__, ooa_hdr->alloc_len,
2463 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2464 retval = EINVAL;
2465 break;
2466 }
2467
2468 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2469 if (entries == NULL) {
2470 printf("%s: could not allocate %d bytes for OOA "
2471 "dump\n", __func__, ooa_hdr->alloc_len);
2472 retval = ENOMEM;
2473 break;
2474 }
2475
2476 mtx_lock(&softc->ctl_lock);
2477 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2478 && ((ooa_hdr->lun_num > CTL_MAX_LUNS)
2479 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2480 mtx_unlock(&softc->ctl_lock);
2481 free(entries, M_CTL);
2482 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2483 __func__, (uintmax_t)ooa_hdr->lun_num);
2484 retval = EINVAL;
2485 break;
2486 }
2487
2488 cur_fill_num = 0;
2489
2490 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2491 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2492 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2493 ooa_hdr, entries);
2494 if (retval != 0)
2495 break;
2496 }
2497 if (retval != 0) {
2498 mtx_unlock(&softc->ctl_lock);
2499 free(entries, M_CTL);
2500 break;
2501 }
2502 } else {
2503 lun = softc->ctl_luns[ooa_hdr->lun_num];
2504
2505 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr,
2506 entries);
2507 }
2508 mtx_unlock(&softc->ctl_lock);
2509
2510 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2511 ooa_hdr->fill_len = ooa_hdr->fill_num *
2512 sizeof(struct ctl_ooa_entry);
2513 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2514 if (retval != 0) {
2515 printf("%s: error copying out %d bytes for OOA dump\n",
2516 __func__, ooa_hdr->fill_len);
2517 }
2518
2519 getbintime(&ooa_hdr->cur_bt);
2520
2521 if (cur_fill_num > ooa_hdr->alloc_num) {
2522 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2523 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2524 } else {
2525 ooa_hdr->dropped_num = 0;
2526 ooa_hdr->status = CTL_OOA_OK;
2527 }
2528
2529 free(entries, M_CTL);
2530 break;
2531 }
2532 case CTL_CHECK_OOA: {
2533 union ctl_io *io;
2534 struct ctl_lun *lun;
2535 struct ctl_ooa_info *ooa_info;
2536
2537
2538 ooa_info = (struct ctl_ooa_info *)addr;
2539
2540 if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2541 ooa_info->status = CTL_OOA_INVALID_LUN;
2542 break;
2543 }
2544 mtx_lock(&softc->ctl_lock);
2545 lun = softc->ctl_luns[ooa_info->lun_id];
2546 if (lun == NULL) {
2547 mtx_unlock(&softc->ctl_lock);
2548 ooa_info->status = CTL_OOA_INVALID_LUN;
2549 break;
2550 }
2551
2552 ooa_info->num_entries = 0;
2553 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2554 io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2555 &io->io_hdr, ooa_links)) {
2556 ooa_info->num_entries++;
2557 }
2558
2559 mtx_unlock(&softc->ctl_lock);
2560 ooa_info->status = CTL_OOA_SUCCESS;
2561
2562 break;
2563 }
2564 case CTL_HARD_START:
2565 case CTL_HARD_STOP: {
2566 struct ctl_fe_ioctl_startstop_info ss_info;
2567 struct cfi_metatask *metatask;
2568 struct mtx hs_mtx;
2569
2570 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF);
2571
2572 cv_init(&ss_info.sem, "hard start/stop cv" );
2573
2574 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2575 if (metatask == NULL) {
2576 retval = ENOMEM;
2577 mtx_destroy(&hs_mtx);
2578 break;
2579 }
2580
2581 if (cmd == CTL_HARD_START)
2582 metatask->tasktype = CFI_TASK_STARTUP;
2583 else
2584 metatask->tasktype = CFI_TASK_SHUTDOWN;
2585
2586 metatask->callback = ctl_ioctl_hard_startstop_callback;
2587 metatask->callback_arg = &ss_info;
2588
2589 cfi_action(metatask);
2590
2591 /* Wait for the callback */
2592 mtx_lock(&hs_mtx);
2593 cv_wait_sig(&ss_info.sem, &hs_mtx);
2594 mtx_unlock(&hs_mtx);
2595
2596 /*
2597 * All information has been copied from the metatask by the
2598 * time cv_broadcast() is called, so we free the metatask here.
2599 */
2600 cfi_free_metatask(metatask);
2601
2602 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info));
2603
2604 mtx_destroy(&hs_mtx);
2605 break;
2606 }
2607 case CTL_BBRREAD: {
2608 struct ctl_bbrread_info *bbr_info;
2609 struct ctl_fe_ioctl_bbrread_info fe_bbr_info;
2610 struct mtx bbr_mtx;
2611 struct cfi_metatask *metatask;
2612
2613 bbr_info = (struct ctl_bbrread_info *)addr;
2614
2615 bzero(&fe_bbr_info, sizeof(fe_bbr_info));
2616
2617 bzero(&bbr_mtx, sizeof(bbr_mtx));
2618 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF);
2619
2620 fe_bbr_info.bbr_info = bbr_info;
2621 fe_bbr_info.lock = &bbr_mtx;
2622
2623 cv_init(&fe_bbr_info.sem, "BBR read cv");
2624 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2625
2626 if (metatask == NULL) {
2627 mtx_destroy(&bbr_mtx);
2628 cv_destroy(&fe_bbr_info.sem);
2629 retval = ENOMEM;
2630 break;
2631 }
2632 metatask->tasktype = CFI_TASK_BBRREAD;
2633 metatask->callback = ctl_ioctl_bbrread_callback;
2634 metatask->callback_arg = &fe_bbr_info;
2635 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num;
2636 metatask->taskinfo.bbrread.lba = bbr_info->lba;
2637 metatask->taskinfo.bbrread.len = bbr_info->len;
2638
2639 cfi_action(metatask);
2640
2641 mtx_lock(&bbr_mtx);
2642 while (fe_bbr_info.wakeup_done == 0)
2643 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx);
2644 mtx_unlock(&bbr_mtx);
2645
2646 bbr_info->status = metatask->status;
2647 bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2648 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status;
2649 memcpy(&bbr_info->sense_data,
2650 &metatask->taskinfo.bbrread.sense_data,
2651 ctl_min(sizeof(bbr_info->sense_data),
2652 sizeof(metatask->taskinfo.bbrread.sense_data)));
2653
2654 cfi_free_metatask(metatask);
2655
2656 mtx_destroy(&bbr_mtx);
2657 cv_destroy(&fe_bbr_info.sem);
2658
2659 break;
2660 }
2661 case CTL_DELAY_IO: {
2662 struct ctl_io_delay_info *delay_info;
2663#ifdef CTL_IO_DELAY
2664 struct ctl_lun *lun;
2665#endif /* CTL_IO_DELAY */
2666
2667 delay_info = (struct ctl_io_delay_info *)addr;
2668
2669#ifdef CTL_IO_DELAY
2670 mtx_lock(&softc->ctl_lock);
2671
2672 if ((delay_info->lun_id > CTL_MAX_LUNS)
2673 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2674 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2675 } else {
2676 lun = softc->ctl_luns[delay_info->lun_id];
2677
2678 delay_info->status = CTL_DELAY_STATUS_OK;
2679
2680 switch (delay_info->delay_type) {
2681 case CTL_DELAY_TYPE_CONT:
2682 break;
2683 case CTL_DELAY_TYPE_ONESHOT:
2684 break;
2685 default:
2686 delay_info->status =
2687 CTL_DELAY_STATUS_INVALID_TYPE;
2688 break;
2689 }
2690
2691 switch (delay_info->delay_loc) {
2692 case CTL_DELAY_LOC_DATAMOVE:
2693 lun->delay_info.datamove_type =
2694 delay_info->delay_type;
2695 lun->delay_info.datamove_delay =
2696 delay_info->delay_secs;
2697 break;
2698 case CTL_DELAY_LOC_DONE:
2699 lun->delay_info.done_type =
2700 delay_info->delay_type;
2701 lun->delay_info.done_delay =
2702 delay_info->delay_secs;
2703 break;
2704 default:
2705 delay_info->status =
2706 CTL_DELAY_STATUS_INVALID_LOC;
2707 break;
2708 }
2709 }
2710
2711 mtx_unlock(&softc->ctl_lock);
2712#else
2713 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2714#endif /* CTL_IO_DELAY */
2715 break;
2716 }
2717 case CTL_REALSYNC_SET: {
2718 int *syncstate;
2719
2720 syncstate = (int *)addr;
2721
2722 mtx_lock(&softc->ctl_lock);
2723 switch (*syncstate) {
2724 case 0:
2725 softc->flags &= ~CTL_FLAG_REAL_SYNC;
2726 break;
2727 case 1:
2728 softc->flags |= CTL_FLAG_REAL_SYNC;
2729 break;
2730 default:
2731 retval = -EINVAL;
2732 break;
2733 }
2734 mtx_unlock(&softc->ctl_lock);
2735 break;
2736 }
2737 case CTL_REALSYNC_GET: {
2738 int *syncstate;
2739
2740 syncstate = (int*)addr;
2741
2742 mtx_lock(&softc->ctl_lock);
2743 if (softc->flags & CTL_FLAG_REAL_SYNC)
2744 *syncstate = 1;
2745 else
2746 *syncstate = 0;
2747 mtx_unlock(&softc->ctl_lock);
2748
2749 break;
2750 }
2751 case CTL_SETSYNC:
2752 case CTL_GETSYNC: {
2753 struct ctl_sync_info *sync_info;
2754 struct ctl_lun *lun;
2755
2756 sync_info = (struct ctl_sync_info *)addr;
2757
2758 mtx_lock(&softc->ctl_lock);
2759 lun = softc->ctl_luns[sync_info->lun_id];
2760 if (lun == NULL) {
2761 mtx_unlock(&softc->ctl_lock);
2762 sync_info->status = CTL_GS_SYNC_NO_LUN;
2763 }
2764 /*
2765 * Get or set the sync interval. We're not bounds checking
2766 * in the set case, hopefully the user won't do something
2767 * silly.
2768 */
2769 if (cmd == CTL_GETSYNC)
2770 sync_info->sync_interval = lun->sync_interval;
2771 else
2772 lun->sync_interval = sync_info->sync_interval;
2773
2774 mtx_unlock(&softc->ctl_lock);
2775
2776 sync_info->status = CTL_GS_SYNC_OK;
2777
2778 break;
2779 }
2780 case CTL_GETSTATS: {
2781 struct ctl_stats *stats;
2782 struct ctl_lun *lun;
2783 int i;
2784
2785 stats = (struct ctl_stats *)addr;
2786
2787 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2788 stats->alloc_len) {
2789 stats->status = CTL_SS_NEED_MORE_SPACE;
2790 stats->num_luns = softc->num_luns;
2791 break;
2792 }
2793 /*
2794 * XXX KDM no locking here. If the LUN list changes,
2795 * things can blow up.
2796 */
2797 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2798 i++, lun = STAILQ_NEXT(lun, links)) {
2799 retval = copyout(&lun->stats, &stats->lun_stats[i],
2800 sizeof(lun->stats));
2801 if (retval != 0)
2802 break;
2803 }
2804 stats->num_luns = softc->num_luns;
2805 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2806 softc->num_luns;
2807 stats->status = CTL_SS_OK;
2808#ifdef CTL_TIME_IO
2809 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2810#else
2811 stats->flags = CTL_STATS_FLAG_NONE;
2812#endif
2813 getnanouptime(&stats->timestamp);
2814 break;
2815 }
2816 case CTL_ERROR_INJECT: {
2817 struct ctl_error_desc *err_desc, *new_err_desc;
2818 struct ctl_lun *lun;
2819
2820 err_desc = (struct ctl_error_desc *)addr;
2821
2822 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2823 M_WAITOK | M_ZERO);
2824 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2825
2826 mtx_lock(&softc->ctl_lock);
2827 lun = softc->ctl_luns[err_desc->lun_id];
2828 if (lun == NULL) {
2829 mtx_unlock(&softc->ctl_lock);
2830 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2831 __func__, (uintmax_t)err_desc->lun_id);
2832 retval = EINVAL;
2833 break;
2834 }
2835
2836 /*
2837 * We could do some checking here to verify the validity
2838 * of the request, but given the complexity of error
2839 * injection requests, the checking logic would be fairly
2840 * complex.
2841 *
2842 * For now, if the request is invalid, it just won't get
2843 * executed and might get deleted.
2844 */
2845 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2846
2847 /*
2848 * XXX KDM check to make sure the serial number is unique,
2849 * in case we somehow manage to wrap. That shouldn't
2850 * happen for a very long time, but it's the right thing to
2851 * do.
2852 */
2853 new_err_desc->serial = lun->error_serial;
2854 err_desc->serial = lun->error_serial;
2855 lun->error_serial++;
2856
2857 mtx_unlock(&softc->ctl_lock);
2858 break;
2859 }
2860 case CTL_ERROR_INJECT_DELETE: {
2861 struct ctl_error_desc *delete_desc, *desc, *desc2;
2862 struct ctl_lun *lun;
2863 int delete_done;
2864
2865 delete_desc = (struct ctl_error_desc *)addr;
2866 delete_done = 0;
2867
2868 mtx_lock(&softc->ctl_lock);
2869 lun = softc->ctl_luns[delete_desc->lun_id];
2870 if (lun == NULL) {
2871 mtx_unlock(&softc->ctl_lock);
2872 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2873 __func__, (uintmax_t)delete_desc->lun_id);
2874 retval = EINVAL;
2875 break;
2876 }
2877 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2878 if (desc->serial != delete_desc->serial)
2879 continue;
2880
2881 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2882 links);
2883 free(desc, M_CTL);
2884 delete_done = 1;
2885 }
2886 mtx_unlock(&softc->ctl_lock);
2887 if (delete_done == 0) {
2888 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2889 "error serial %ju on LUN %u\n", __func__,
2890 delete_desc->serial, delete_desc->lun_id);
2891 retval = EINVAL;
2892 break;
2893 }
2894 break;
2895 }
2896 case CTL_DUMP_STRUCTS: {
2897 int i, j, k;
2898 struct ctl_frontend *fe;
2899
2900 printf("CTL IID to WWPN map start:\n");
2901 for (i = 0; i < CTL_MAX_PORTS; i++) {
2902 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2903 if (softc->wwpn_iid[i][j].in_use == 0)
2904 continue;
2905
2906 printf("port %d iid %u WWPN %#jx\n",
2907 softc->wwpn_iid[i][j].port,
2908 softc->wwpn_iid[i][j].iid,
2909 (uintmax_t)softc->wwpn_iid[i][j].wwpn);
2910 }
2911 }
2912 printf("CTL IID to WWPN map end\n");
2913 printf("CTL Persistent Reservation information start:\n");
2914 for (i = 0; i < CTL_MAX_LUNS; i++) {
2915 struct ctl_lun *lun;
2916
2917 lun = softc->ctl_luns[i];
2918
2919 if ((lun == NULL)
2920 || ((lun->flags & CTL_LUN_DISABLED) != 0))
2921 continue;
2922
2923 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) {
2924 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2925 if (lun->per_res[j+k].registered == 0)
2926 continue;
2927 printf("LUN %d port %d iid %d key "
2928 "%#jx\n", i, j, k,
2929 (uintmax_t)scsi_8btou64(
2930 lun->per_res[j+k].res_key.key));
2931 }
2932 }
2933 }
2934 printf("CTL Persistent Reservation information end\n");
2935 printf("CTL Frontends:\n");
2936 /*
2937 * XXX KDM calling this without a lock. We'd likely want
2938 * to drop the lock before calling the frontend's dump
2939 * routine anyway.
2940 */
2941 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2942 printf("Frontend %s Type %u pport %d vport %d WWNN "
2943 "%#jx WWPN %#jx\n", fe->port_name, fe->port_type,
2944 fe->physical_port, fe->virtual_port,
2945 (uintmax_t)fe->wwnn, (uintmax_t)fe->wwpn);
2946
2947 /*
2948 * Frontends are not required to support the dump
2949 * routine.
2950 */
2951 if (fe->fe_dump == NULL)
2952 continue;
2953
2954 fe->fe_dump();
2955 }
2956 printf("CTL Frontend information end\n");
2957 break;
2958 }
2959 case CTL_LUN_REQ: {
2960 struct ctl_lun_req *lun_req;
2961 struct ctl_backend_driver *backend;
2962
2963 lun_req = (struct ctl_lun_req *)addr;
2964
2965 backend = ctl_backend_find(lun_req->backend);
2966 if (backend == NULL) {
2967 lun_req->status = CTL_LUN_ERROR;
2968 snprintf(lun_req->error_str,
2969 sizeof(lun_req->error_str),
2970 "Backend \"%s\" not found.",
2971 lun_req->backend);
2972 break;
2973 }
2974 if (lun_req->num_be_args > 0) {
2975 lun_req->kern_be_args = ctl_copyin_args(
2976 lun_req->num_be_args,
2977 lun_req->be_args,
2978 lun_req->error_str,
2979 sizeof(lun_req->error_str));
2980 if (lun_req->kern_be_args == NULL) {
2981 lun_req->status = CTL_LUN_ERROR;
2982 break;
2983 }
2984 }
2985
2986 retval = backend->ioctl(dev, cmd, addr, flag, td);
2987
2988 if (lun_req->num_be_args > 0) {
2989 ctl_free_args(lun_req->num_be_args,
2990 lun_req->kern_be_args);
2991 }
2992 break;
2993 }
2994 case CTL_LUN_LIST: {
2995 struct sbuf *sb;
2996 struct ctl_lun *lun;
2997 struct ctl_lun_list *list;
2998 struct ctl_be_lun_option *opt;
2999
3000 list = (struct ctl_lun_list *)addr;
3001
3002 /*
3003 * Allocate a fixed length sbuf here, based on the length
3004 * of the user's buffer. We could allocate an auto-extending
3005 * buffer, and then tell the user how much larger our
3006 * amount of data is than his buffer, but that presents
3007 * some problems:
3008 *
3009 * 1. The sbuf(9) routines use a blocking malloc, and so
3010 * we can't hold a lock while calling them with an
3011 * auto-extending buffer.
3012 *
3013 * 2. There is not currently a LUN reference counting
3014 * mechanism, outside of outstanding transactions on
3015 * the LUN's OOA queue. So a LUN could go away on us
3016 * while we're getting the LUN number, backend-specific
3017 * information, etc. Thus, given the way things
3018 * currently work, we need to hold the CTL lock while
3019 * grabbing LUN information.
3020 *
3021 * So, from the user's standpoint, the best thing to do is
3022 * allocate what he thinks is a reasonable buffer length,
3023 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3024 * double the buffer length and try again. (And repeat
3025 * that until he succeeds.)
3026 */
3027 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3028 if (sb == NULL) {
3029 list->status = CTL_LUN_LIST_ERROR;
3030 snprintf(list->error_str, sizeof(list->error_str),
3031 "Unable to allocate %d bytes for LUN list",
3032 list->alloc_len);
3033 break;
3034 }
3035
3036 sbuf_printf(sb, "<ctllunlist>\n");
3037
3038 mtx_lock(&softc->ctl_lock);
3039
3040 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3041 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3042 (uintmax_t)lun->lun);
3043
3044 /*
3045 * Bail out as soon as we see that we've overfilled
3046 * the buffer.
3047 */
3048 if (retval != 0)
3049 break;
3050
3051 retval = sbuf_printf(sb, "<backend_type>%s"
3052 "</backend_type>\n",
3053 (lun->backend == NULL) ? "none" :
3054 lun->backend->name);
3055
3056 if (retval != 0)
3057 break;
3058
3059 retval = sbuf_printf(sb, "<lun_type>%d</lun_type>\n",
3060 lun->be_lun->lun_type);
3061
3062 if (retval != 0)
3063 break;
3064
3065 if (lun->backend == NULL) {
3066 retval = sbuf_printf(sb, "</lun>\n");
3067 if (retval != 0)
3068 break;
3069 continue;
3070 }
3071
3072 retval = sbuf_printf(sb, "<size>%ju</size>\n",
3073 (lun->be_lun->maxlba > 0) ?
3074 lun->be_lun->maxlba + 1 : 0);
3075
3076 if (retval != 0)
3077 break;
3078
3079 retval = sbuf_printf(sb, "<blocksize>%u</blocksize>\n",
3080 lun->be_lun->blocksize);
3081
3082 if (retval != 0)
3083 break;
3084
3085 retval = sbuf_printf(sb, "<serial_number>");
3086
3087 if (retval != 0)
3088 break;
3089
3090 retval = ctl_sbuf_printf_esc(sb,
3091 lun->be_lun->serial_num);
3092
3093 if (retval != 0)
3094 break;
3095
3096 retval = sbuf_printf(sb, "</serial_number>\n");
3097
3098 if (retval != 0)
3099 break;
3100
3101 retval = sbuf_printf(sb, "<device_id>");
3102
3103 if (retval != 0)
3104 break;
3105
3106 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id);
3107
3108 if (retval != 0)
3109 break;
3110
3111 retval = sbuf_printf(sb, "</device_id>\n");
3112
3113 if (retval != 0)
3114 break;
3115
3116 if (lun->backend->lun_info != NULL) {
3117 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3118 if (retval != 0)
3119 break;
3120 }
3121 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3122 retval = sbuf_printf(sb, "<%s>%s</%s>", opt->name, opt->value, opt->name);
3123 if (retval != 0)
3124 break;
3125 }
3126
3127 retval = sbuf_printf(sb, "</lun>\n");
3128
3129 if (retval != 0)
3130 break;
3131 }
3132 mtx_unlock(&softc->ctl_lock);
3133
3134 if ((retval != 0)
3135 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3136 retval = 0;
3137 sbuf_delete(sb);
3138 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3139 snprintf(list->error_str, sizeof(list->error_str),
3140 "Out of space, %d bytes is too small",
3141 list->alloc_len);
3142 break;
3143 }
3144
3145 sbuf_finish(sb);
3146
3147 retval = copyout(sbuf_data(sb), list->lun_xml,
3148 sbuf_len(sb) + 1);
3149
3150 list->fill_len = sbuf_len(sb) + 1;
3151 list->status = CTL_LUN_LIST_OK;
3152 sbuf_delete(sb);
3153 break;
3154 }
3155 case CTL_ISCSI: {
3156 struct ctl_iscsi *ci;
3157 struct ctl_frontend *fe;
3158
3159 ci = (struct ctl_iscsi *)addr;
3160
3161 mtx_lock(&softc->ctl_lock);
3162 STAILQ_FOREACH(fe, &softc->fe_list, links) {
3163 if (strcmp(fe->port_name, "iscsi") == 0)
3164 break;
3165 }
3166 mtx_unlock(&softc->ctl_lock);
3167
3168 if (fe == NULL) {
3169 ci->status = CTL_ISCSI_ERROR;
3170 snprintf(ci->error_str, sizeof(ci->error_str), "Backend \"iscsi\" not found.");
3171 break;
3172 }
3173
3174 retval = fe->ioctl(dev, cmd, addr, flag, td);
3175 break;
3176 }
3177 default: {
3178 /* XXX KDM should we fix this? */
3179#if 0
3180 struct ctl_backend_driver *backend;
3181 unsigned int type;
3182 int found;
3183
3184 found = 0;
3185
3186 /*
3187 * We encode the backend type as the ioctl type for backend
3188 * ioctls. So parse it out here, and then search for a
3189 * backend of this type.
3190 */
3191 type = _IOC_TYPE(cmd);
3192
3193 STAILQ_FOREACH(backend, &softc->be_list, links) {
3194 if (backend->type == type) {
3195 found = 1;
3196 break;
3197 }
3198 }
3199 if (found == 0) {
3200 printf("ctl: unknown ioctl command %#lx or backend "
3201 "%d\n", cmd, type);
3202 retval = -EINVAL;
3203 break;
3204 }
3205 retval = backend->ioctl(dev, cmd, addr, flag, td);
3206#endif
3207 retval = ENOTTY;
3208 break;
3209 }
3210 }
3211 return (retval);
3212}
3213
3214uint32_t
3215ctl_get_initindex(struct ctl_nexus *nexus)
3216{
3217 if (nexus->targ_port < CTL_MAX_PORTS)
3218 return (nexus->initid.id +
3219 (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3220 else
3221 return (nexus->initid.id +
3222 ((nexus->targ_port - CTL_MAX_PORTS) *
3223 CTL_MAX_INIT_PER_PORT));
3224}
3225
3226uint32_t
3227ctl_get_resindex(struct ctl_nexus *nexus)
3228{
3229 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3230}
3231
3232uint32_t
3233ctl_port_idx(int port_num)
3234{
3235 if (port_num < CTL_MAX_PORTS)
3236 return(port_num);
3237 else
3238 return(port_num - CTL_MAX_PORTS);
3239}
3240
3241/*
3242 * Note: This only works for bitmask sizes that are at least 32 bits, and
3243 * that are a power of 2.
3244 */
3245int
3246ctl_ffz(uint32_t *mask, uint32_t size)
3247{
3248 uint32_t num_chunks, num_pieces;
3249 int i, j;
3250
3251 num_chunks = (size >> 5);
3252 if (num_chunks == 0)
3253 num_chunks++;
3254 num_pieces = ctl_min((sizeof(uint32_t) * 8), size);
3255
3256 for (i = 0; i < num_chunks; i++) {
3257 for (j = 0; j < num_pieces; j++) {
3258 if ((mask[i] & (1 << j)) == 0)
3259 return ((i << 5) + j);
3260 }
3261 }
3262
3263 return (-1);
3264}
3265
3266int
3267ctl_set_mask(uint32_t *mask, uint32_t bit)
3268{
3269 uint32_t chunk, piece;
3270
3271 chunk = bit >> 5;
3272 piece = bit % (sizeof(uint32_t) * 8);
3273
3274 if ((mask[chunk] & (1 << piece)) != 0)
3275 return (-1);
3276 else
3277 mask[chunk] |= (1 << piece);
3278
3279 return (0);
3280}
3281
3282int
3283ctl_clear_mask(uint32_t *mask, uint32_t bit)
3284{
3285 uint32_t chunk, piece;
3286
3287 chunk = bit >> 5;
3288 piece = bit % (sizeof(uint32_t) * 8);
3289
3290 if ((mask[chunk] & (1 << piece)) == 0)
3291 return (-1);
3292 else
3293 mask[chunk] &= ~(1 << piece);
3294
3295 return (0);
3296}
3297
3298int
3299ctl_is_set(uint32_t *mask, uint32_t bit)
3300{
3301 uint32_t chunk, piece;
3302
3303 chunk = bit >> 5;
3304 piece = bit % (sizeof(uint32_t) * 8);
3305
3306 if ((mask[chunk] & (1 << piece)) == 0)
3307 return (0);
3308 else
3309 return (1);
3310}
3311
3312#ifdef unused
3313/*
3314 * The bus, target and lun are optional, they can be filled in later.
3315 * can_wait is used to determine whether we can wait on the malloc or not.
3316 */
3317union ctl_io*
3318ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target,
3319 uint32_t targ_lun, int can_wait)
3320{
3321 union ctl_io *io;
3322
3323 if (can_wait)
3324 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK);
3325 else
3326 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3327
3328 if (io != NULL) {
3329 io->io_hdr.io_type = io_type;
3330 io->io_hdr.targ_port = targ_port;
3331 /*
3332 * XXX KDM this needs to change/go away. We need to move
3333 * to a preallocated pool of ctl_scsiio structures.
3334 */
3335 io->io_hdr.nexus.targ_target.id = targ_target;
3336 io->io_hdr.nexus.targ_lun = targ_lun;
3337 }
3338
3339 return (io);
3340}
3341
3342void
3343ctl_kfree_io(union ctl_io *io)
3344{
3345 free(io, M_CTL);
3346}
3347#endif /* unused */
3348
3349/*
3350 * ctl_softc, pool_type, total_ctl_io are passed in.
3351 * npool is passed out.
3352 */
3353int
3354ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type,
3355 uint32_t total_ctl_io, struct ctl_io_pool **npool)
3356{
3357 uint32_t i;
3358 union ctl_io *cur_io, *next_io;
3359 struct ctl_io_pool *pool;
3360 int retval;
3361
3362 retval = 0;
3363
3364 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3365 M_NOWAIT | M_ZERO);
3366 if (pool == NULL) {
3367 retval = -ENOMEM;
3368 goto bailout;
3369 }
3370
3371 pool->type = pool_type;
3372 pool->ctl_softc = ctl_softc;
3373
3374 mtx_lock(&ctl_softc->pool_lock);
3375 pool->id = ctl_softc->cur_pool_id++;
3376 mtx_unlock(&ctl_softc->pool_lock);
3377
3378 pool->flags = CTL_POOL_FLAG_NONE;
3379 pool->refcount = 1; /* Reference for validity. */
3380 STAILQ_INIT(&pool->free_queue);
3381
3382 /*
3383 * XXX KDM other options here:
3384 * - allocate a page at a time
3385 * - allocate one big chunk of memory.
3386 * Page allocation might work well, but would take a little more
3387 * tracking.
3388 */
3389 for (i = 0; i < total_ctl_io; i++) {
3390 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTL,
3391 M_NOWAIT);
3392 if (cur_io == NULL) {
3393 retval = ENOMEM;
3394 break;
3395 }
3396 cur_io->io_hdr.pool = pool;
3397 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links);
3398 pool->total_ctl_io++;
3399 pool->free_ctl_io++;
3400 }
3401
3402 if (retval != 0) {
3403 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3404 cur_io != NULL; cur_io = next_io) {
3405 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr,
3406 links);
3407 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr,
3408 ctl_io_hdr, links);
3409 free(cur_io, M_CTL);
3410 }
3411
3412 free(pool, M_CTL);
3413 goto bailout;
3414 }
3415 mtx_lock(&ctl_softc->pool_lock);
3416 ctl_softc->num_pools++;
3417 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links);
3418 /*
3419 * Increment our usage count if this is an external consumer, so we
3420 * can't get unloaded until the external consumer (most likely a
3421 * FETD) unloads and frees his pool.
3422 *
3423 * XXX KDM will this increment the caller's module use count, or
3424 * mine?
3425 */
3426#if 0
3427 if ((pool_type != CTL_POOL_EMERGENCY)
3428 && (pool_type != CTL_POOL_INTERNAL)
3429 && (pool_type != CTL_POOL_IOCTL)
3430 && (pool_type != CTL_POOL_4OTHERSC))
3431 MOD_INC_USE_COUNT;
3432#endif
3433
3434 mtx_unlock(&ctl_softc->pool_lock);
3435
3436 *npool = pool;
3437
3438bailout:
3439
3440 return (retval);
3441}
3442
3443static int
3444ctl_pool_acquire(struct ctl_io_pool *pool)
3445{
3446
3447 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED);
3448
3449 if (pool->flags & CTL_POOL_FLAG_INVALID)
3450 return (-EINVAL);
3451
3452 pool->refcount++;
3453
3454 return (0);
3455}
3456
3457static void
3458ctl_pool_release(struct ctl_io_pool *pool)
3459{
3460 struct ctl_softc *ctl_softc = pool->ctl_softc;
3461 union ctl_io *io;
3462
3463 mtx_assert(&ctl_softc->pool_lock, MA_OWNED);
3464
3465 if (--pool->refcount != 0)
3466 return;
3467
3468 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) {
3469 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr,
3470 links);
3471 free(io, M_CTL);
3472 }
3473
3474 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links);
3475 ctl_softc->num_pools--;
3476
3477 /*
3478 * XXX KDM will this decrement the caller's usage count or mine?
3479 */
3480#if 0
3481 if ((pool->type != CTL_POOL_EMERGENCY)
3482 && (pool->type != CTL_POOL_INTERNAL)
3483 && (pool->type != CTL_POOL_IOCTL))
3484 MOD_DEC_USE_COUNT;
3485#endif
3486
3487 free(pool, M_CTL);
3488}
3489
3490void
3491ctl_pool_free(struct ctl_io_pool *pool)
3492{
3493 struct ctl_softc *ctl_softc;
3494
3495 if (pool == NULL)
3496 return;
3497
3498 ctl_softc = pool->ctl_softc;
3499 mtx_lock(&ctl_softc->pool_lock);
3500 pool->flags |= CTL_POOL_FLAG_INVALID;
3501 ctl_pool_release(pool);
3502 mtx_unlock(&ctl_softc->pool_lock);
3503}
3504
3505/*
3506 * This routine does not block (except for spinlocks of course).
3507 * It tries to allocate a ctl_io union from the caller's pool as quickly as
3508 * possible.
3509 */
3510union ctl_io *
3511ctl_alloc_io(void *pool_ref)
3512{
3513 union ctl_io *io;
3514 struct ctl_softc *ctl_softc;
3515 struct ctl_io_pool *pool, *npool;
3516 struct ctl_io_pool *emergency_pool;
3517
3518 pool = (struct ctl_io_pool *)pool_ref;
3519
3520 if (pool == NULL) {
3521 printf("%s: pool is NULL\n", __func__);
3522 return (NULL);
3523 }
3524
3525 emergency_pool = NULL;
3526
3527 ctl_softc = pool->ctl_softc;
3528
3529 mtx_lock(&ctl_softc->pool_lock);
3530 /*
3531 * First, try to get the io structure from the user's pool.
3532 */
3533 if (ctl_pool_acquire(pool) == 0) {
3534 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3535 if (io != NULL) {
3536 STAILQ_REMOVE_HEAD(&pool->free_queue, links);
3537 pool->total_allocated++;
3538 pool->free_ctl_io--;
3539 mtx_unlock(&ctl_softc->pool_lock);
3540 return (io);
3541 } else
3542 ctl_pool_release(pool);
3543 }
3544 /*
3545 * If he doesn't have any io structures left, search for an
3546 * emergency pool and grab one from there.
3547 */
3548 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) {
3549 if (npool->type != CTL_POOL_EMERGENCY)
3550 continue;
3551
3552 if (ctl_pool_acquire(npool) != 0)
3553 continue;
3554
3555 emergency_pool = npool;
3556
3557 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue);
3558 if (io != NULL) {
3559 STAILQ_REMOVE_HEAD(&npool->free_queue, links);
3560 npool->total_allocated++;
3561 npool->free_ctl_io--;
3562 mtx_unlock(&ctl_softc->pool_lock);
3563 return (io);
3564 } else
3565 ctl_pool_release(npool);
3566 }
3567
3568 /* Drop the spinlock before we malloc */
3569 mtx_unlock(&ctl_softc->pool_lock);
3570
3571 /*
3572 * The emergency pool (if it exists) didn't have one, so try an
3573 * atomic (i.e. nonblocking) malloc and see if we get lucky.
3574 */
3575 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3576 if (io != NULL) {
3577 /*
3578 * If the emergency pool exists but is empty, add this
3579 * ctl_io to its list when it gets freed.
3580 */
3581 if (emergency_pool != NULL) {
3582 mtx_lock(&ctl_softc->pool_lock);
3583 if (ctl_pool_acquire(emergency_pool) == 0) {
3584 io->io_hdr.pool = emergency_pool;
3585 emergency_pool->total_ctl_io++;
3586 /*
3587 * Need to bump this, otherwise
3588 * total_allocated and total_freed won't
3589 * match when we no longer have anything
3590 * outstanding.
3591 */
3592 emergency_pool->total_allocated++;
3593 }
3594 mtx_unlock(&ctl_softc->pool_lock);
3595 } else
3596 io->io_hdr.pool = NULL;
3597 }
3598
3599 return (io);
3600}
3601
3602void
3603ctl_free_io(union ctl_io *io)
3604{
3605 if (io == NULL)
3606 return;
3607
3608 /*
3609 * If this ctl_io has a pool, return it to that pool.
3610 */
3611 if (io->io_hdr.pool != NULL) {
3612 struct ctl_io_pool *pool;
3613#if 0
3614 struct ctl_softc *ctl_softc;
3615 union ctl_io *tmp_io;
3616 unsigned long xflags;
3617 int i;
3618
3619 ctl_softc = control_softc;
3620#endif
3621
3622 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3623
3624 mtx_lock(&pool->ctl_softc->pool_lock);
3625#if 0
3626 save_flags(xflags);
3627
3628 for (i = 0, tmp_io = (union ctl_io *)STAILQ_FIRST(
3629 &ctl_softc->task_queue); tmp_io != NULL; i++,
3630 tmp_io = (union ctl_io *)STAILQ_NEXT(&tmp_io->io_hdr,
3631 links)) {
3632 if (tmp_io == io) {
3633 printf("%s: %p is still on the task queue!\n",
3634 __func__, tmp_io);
3635 printf("%s: (%d): type %d "
3636 "msg %d cdb %x iptl: "
3637 "%d:%d:%d:%d tag 0x%04x "
3638 "flg %#lx\n",
3639 __func__, i,
3640 tmp_io->io_hdr.io_type,
3641 tmp_io->io_hdr.msg_type,
3642 tmp_io->scsiio.cdb[0],
3643 tmp_io->io_hdr.nexus.initid.id,
3644 tmp_io->io_hdr.nexus.targ_port,
3645 tmp_io->io_hdr.nexus.targ_target.id,
3646 tmp_io->io_hdr.nexus.targ_lun,
3647 (tmp_io->io_hdr.io_type ==
3648 CTL_IO_TASK) ?
3649 tmp_io->taskio.tag_num :
3650 tmp_io->scsiio.tag_num,
3651 xflags);
3652 panic("I/O still on the task queue!");
3653 }
3654 }
3655#endif
3656 io->io_hdr.io_type = 0xff;
3657 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links);
3658 pool->total_freed++;
3659 pool->free_ctl_io++;
3660 ctl_pool_release(pool);
3661 mtx_unlock(&pool->ctl_softc->pool_lock);
3662 } else {
3663 /*
3664 * Otherwise, just free it. We probably malloced it and
3665 * the emergency pool wasn't available.
3666 */
3667 free(io, M_CTL);
3668 }
3669
3670}
3671
3672void
3673ctl_zero_io(union ctl_io *io)
3674{
3675 void *pool_ref;
3676
3677 if (io == NULL)
3678 return;
3679
3680 /*
3681 * May need to preserve linked list pointers at some point too.
3682 */
3683 pool_ref = io->io_hdr.pool;
3684
3685 memset(io, 0, sizeof(*io));
3686
3687 io->io_hdr.pool = pool_ref;
3688}
3689
3690/*
3691 * This routine is currently used for internal copies of ctl_ios that need
3692 * to persist for some reason after we've already returned status to the
3693 * FETD. (Thus the flag set.)
3694 *
3695 * XXX XXX
3696 * Note that this makes a blind copy of all fields in the ctl_io, except
3697 * for the pool reference. This includes any memory that has been
3698 * allocated! That memory will no longer be valid after done has been
3699 * called, so this would be VERY DANGEROUS for command that actually does
3700 * any reads or writes. Right now (11/7/2005), this is only used for immediate
3701 * start and stop commands, which don't transfer any data, so this is not a
3702 * problem. If it is used for anything else, the caller would also need to
3703 * allocate data buffer space and this routine would need to be modified to
3704 * copy the data buffer(s) as well.
3705 */
3706void
3707ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3708{
3709 void *pool_ref;
3710
3711 if ((src == NULL)
3712 || (dest == NULL))
3713 return;
3714
3715 /*
3716 * May need to preserve linked list pointers at some point too.
3717 */
3718 pool_ref = dest->io_hdr.pool;
3719
3720 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest)));
3721
3722 dest->io_hdr.pool = pool_ref;
3723 /*
3724 * We need to know that this is an internal copy, and doesn't need
3725 * to get passed back to the FETD that allocated it.
3726 */
3727 dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3728}
3729
3730#ifdef NEEDTOPORT
3731static void
3732ctl_update_power_subpage(struct copan_power_subpage *page)
3733{
3734 int num_luns, num_partitions, config_type;
3735 struct ctl_softc *softc;
3736 cs_BOOL_t aor_present, shelf_50pct_power;
3737 cs_raidset_personality_t rs_type;
3738 int max_active_luns;
3739
3740 softc = control_softc;
3741
3742 /* subtract out the processor LUN */
3743 num_luns = softc->num_luns - 1;
3744 /*
3745 * Default to 7 LUNs active, which was the only number we allowed
3746 * in the past.
3747 */
3748 max_active_luns = 7;
3749
3750 num_partitions = config_GetRsPartitionInfo();
3751 config_type = config_GetConfigType();
3752 shelf_50pct_power = config_GetShelfPowerMode();
3753 aor_present = config_IsAorRsPresent();
3754
3755 rs_type = ddb_GetRsRaidType(1);
3756 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5)
3757 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) {
3758 EPRINT(0, "Unsupported RS type %d!", rs_type);
3759 }
3760
3761
3762 page->total_luns = num_luns;
3763
3764 switch (config_type) {
3765 case 40:
3766 /*
3767 * In a 40 drive configuration, it doesn't matter what DC
3768 * cards we have, whether we have AOR enabled or not,
3769 * partitioning or not, or what type of RAIDset we have.
3770 * In that scenario, we can power up every LUN we present
3771 * to the user.
3772 */
3773 max_active_luns = num_luns;
3774
3775 break;
3776 case 64:
3777 if (shelf_50pct_power == CS_FALSE) {
3778 /* 25% power */
3779 if (aor_present == CS_TRUE) {
3780 if (rs_type ==
3781 CS_RAIDSET_PERSONALITY_RAID5) {
3782 max_active_luns = 7;
3783 } else if (rs_type ==
3784 CS_RAIDSET_PERSONALITY_RAID1){
3785 max_active_luns = 14;
3786 } else {
3787 /* XXX KDM now what?? */
3788 }
3789 } else {
3790 if (rs_type ==
3791 CS_RAIDSET_PERSONALITY_RAID5) {
3792 max_active_luns = 8;
3793 } else if (rs_type ==
3794 CS_RAIDSET_PERSONALITY_RAID1){
3795 max_active_luns = 16;
3796 } else {
3797 /* XXX KDM now what?? */
3798 }
3799 }
3800 } else {
3801 /* 50% power */
3802 /*
3803 * With 50% power in a 64 drive configuration, we
3804 * can power all LUNs we present.
3805 */
3806 max_active_luns = num_luns;
3807 }
3808 break;
3809 case 112:
3810 if (shelf_50pct_power == CS_FALSE) {
3811 /* 25% power */
3812 if (aor_present == CS_TRUE) {
3813 if (rs_type ==
3814 CS_RAIDSET_PERSONALITY_RAID5) {
3815 max_active_luns = 7;
3816 } else if (rs_type ==
3817 CS_RAIDSET_PERSONALITY_RAID1){
3818 max_active_luns = 14;
3819 } else {
3820 /* XXX KDM now what?? */
3821 }
3822 } else {
3823 if (rs_type ==
3824 CS_RAIDSET_PERSONALITY_RAID5) {
3825 max_active_luns = 8;
3826 } else if (rs_type ==
3827 CS_RAIDSET_PERSONALITY_RAID1){
3828 max_active_luns = 16;
3829 } else {
3830 /* XXX KDM now what?? */
3831 }
3832 }
3833 } else {
3834 /* 50% power */
3835 if (aor_present == CS_TRUE) {
3836 if (rs_type ==
3837 CS_RAIDSET_PERSONALITY_RAID5) {
3838 max_active_luns = 14;
3839 } else if (rs_type ==
3840 CS_RAIDSET_PERSONALITY_RAID1){
3841 /*
3842 * We're assuming here that disk
3843 * caching is enabled, and so we're
3844 * able to power up half of each
3845 * LUN, and cache all writes.
3846 */
3847 max_active_luns = num_luns;
3848 } else {
3849 /* XXX KDM now what?? */
3850 }
3851 } else {
3852 if (rs_type ==
3853 CS_RAIDSET_PERSONALITY_RAID5) {
3854 max_active_luns = 15;
3855 } else if (rs_type ==
3856 CS_RAIDSET_PERSONALITY_RAID1){
3857 max_active_luns = 30;
3858 } else {
3859 /* XXX KDM now what?? */
3860 }
3861 }
3862 }
3863 break;
3864 default:
3865 /*
3866 * In this case, we have an unknown configuration, so we
3867 * just use the default from above.
3868 */
3869 break;
3870 }
3871
3872 page->max_active_luns = max_active_luns;
3873#if 0
3874 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__,
3875 page->total_luns, page->max_active_luns);
3876#endif
3877}
3878#endif /* NEEDTOPORT */
3879
3880/*
3881 * This routine could be used in the future to load default and/or saved
3882 * mode page parameters for a particuar lun.
3883 */
3884static int
3885ctl_init_page_index(struct ctl_lun *lun)
3886{
3887 int i;
3888 struct ctl_page_index *page_index;
3889 struct ctl_softc *softc;
3890
3891 memcpy(&lun->mode_pages.index, page_index_template,
3892 sizeof(page_index_template));
3893
3894 softc = lun->ctl_softc;
3895
3896 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3897
3898 page_index = &lun->mode_pages.index[i];
3899 /*
3900 * If this is a disk-only mode page, there's no point in
3901 * setting it up. For some pages, we have to have some
3902 * basic information about the disk in order to calculate the
3903 * mode page data.
3904 */
3905 if ((lun->be_lun->lun_type != T_DIRECT)
3906 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
3907 continue;
3908
3909 switch (page_index->page_code & SMPH_PC_MASK) {
3910 case SMS_FORMAT_DEVICE_PAGE: {
3911 struct scsi_format_page *format_page;
3912
3913 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3914 panic("subpage is incorrect!");
3915
3916 /*
3917 * Sectors per track are set above. Bytes per
3918 * sector need to be set here on a per-LUN basis.
3919 */
3920 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3921 &format_page_default,
3922 sizeof(format_page_default));
3923 memcpy(&lun->mode_pages.format_page[
3924 CTL_PAGE_CHANGEABLE], &format_page_changeable,
3925 sizeof(format_page_changeable));
3926 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3927 &format_page_default,
3928 sizeof(format_page_default));
3929 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3930 &format_page_default,
3931 sizeof(format_page_default));
3932
3933 format_page = &lun->mode_pages.format_page[
3934 CTL_PAGE_CURRENT];
3935 scsi_ulto2b(lun->be_lun->blocksize,
3936 format_page->bytes_per_sector);
3937
3938 format_page = &lun->mode_pages.format_page[
3939 CTL_PAGE_DEFAULT];
3940 scsi_ulto2b(lun->be_lun->blocksize,
3941 format_page->bytes_per_sector);
3942
3943 format_page = &lun->mode_pages.format_page[
3944 CTL_PAGE_SAVED];
3945 scsi_ulto2b(lun->be_lun->blocksize,
3946 format_page->bytes_per_sector);
3947
3948 page_index->page_data =
3949 (uint8_t *)lun->mode_pages.format_page;
3950 break;
3951 }
3952 case SMS_RIGID_DISK_PAGE: {
3953 struct scsi_rigid_disk_page *rigid_disk_page;
3954 uint32_t sectors_per_cylinder;
3955 uint64_t cylinders;
3956#ifndef __XSCALE__
3957 int shift;
3958#endif /* !__XSCALE__ */
3959
3960 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3961 panic("invalid subpage value %d",
3962 page_index->subpage);
3963
3964 /*
3965 * Rotation rate and sectors per track are set
3966 * above. We calculate the cylinders here based on
3967 * capacity. Due to the number of heads and
3968 * sectors per track we're using, smaller arrays
3969 * may turn out to have 0 cylinders. Linux and
3970 * FreeBSD don't pay attention to these mode pages
3971 * to figure out capacity, but Solaris does. It
3972 * seems to deal with 0 cylinders just fine, and
3973 * works out a fake geometry based on the capacity.
3974 */
3975 memcpy(&lun->mode_pages.rigid_disk_page[
3976 CTL_PAGE_CURRENT], &rigid_disk_page_default,
3977 sizeof(rigid_disk_page_default));
3978 memcpy(&lun->mode_pages.rigid_disk_page[
3979 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
3980 sizeof(rigid_disk_page_changeable));
3981 memcpy(&lun->mode_pages.rigid_disk_page[
3982 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
3983 sizeof(rigid_disk_page_default));
3984 memcpy(&lun->mode_pages.rigid_disk_page[
3985 CTL_PAGE_SAVED], &rigid_disk_page_default,
3986 sizeof(rigid_disk_page_default));
3987
3988 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
3989 CTL_DEFAULT_HEADS;
3990
3991 /*
3992 * The divide method here will be more accurate,
3993 * probably, but results in floating point being
3994 * used in the kernel on i386 (__udivdi3()). On the
3995 * XScale, though, __udivdi3() is implemented in
3996 * software.
3997 *
3998 * The shift method for cylinder calculation is
3999 * accurate if sectors_per_cylinder is a power of
4000 * 2. Otherwise it might be slightly off -- you
4001 * might have a bit of a truncation problem.
4002 */
4003#ifdef __XSCALE__
4004 cylinders = (lun->be_lun->maxlba + 1) /
4005 sectors_per_cylinder;
4006#else
4007 for (shift = 31; shift > 0; shift--) {
4008 if (sectors_per_cylinder & (1 << shift))
4009 break;
4010 }
4011 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4012#endif
4013
4014 /*
4015 * We've basically got 3 bytes, or 24 bits for the
4016 * cylinder size in the mode page. If we're over,
4017 * just round down to 2^24.
4018 */
4019 if (cylinders > 0xffffff)
4020 cylinders = 0xffffff;
4021
4022 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4023 CTL_PAGE_CURRENT];
4024 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4025
4026 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4027 CTL_PAGE_DEFAULT];
4028 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4029
4030 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4031 CTL_PAGE_SAVED];
4032 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4033
4034 page_index->page_data =
4035 (uint8_t *)lun->mode_pages.rigid_disk_page;
4036 break;
4037 }
4038 case SMS_CACHING_PAGE: {
4039
4040 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4041 panic("invalid subpage value %d",
4042 page_index->subpage);
4043 /*
4044 * Defaults should be okay here, no calculations
4045 * needed.
4046 */
4047 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4048 &caching_page_default,
4049 sizeof(caching_page_default));
4050 memcpy(&lun->mode_pages.caching_page[
4051 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4052 sizeof(caching_page_changeable));
4053 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4054 &caching_page_default,
4055 sizeof(caching_page_default));
4056 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4057 &caching_page_default,
4058 sizeof(caching_page_default));
4059 page_index->page_data =
4060 (uint8_t *)lun->mode_pages.caching_page;
4061 break;
4062 }
4063 case SMS_CONTROL_MODE_PAGE: {
4064
4065 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4066 panic("invalid subpage value %d",
4067 page_index->subpage);
4068
4069 /*
4070 * Defaults should be okay here, no calculations
4071 * needed.
4072 */
4073 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
4074 &control_page_default,
4075 sizeof(control_page_default));
4076 memcpy(&lun->mode_pages.control_page[
4077 CTL_PAGE_CHANGEABLE], &control_page_changeable,
4078 sizeof(control_page_changeable));
4079 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
4080 &control_page_default,
4081 sizeof(control_page_default));
4082 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
4083 &control_page_default,
4084 sizeof(control_page_default));
4085 page_index->page_data =
4086 (uint8_t *)lun->mode_pages.control_page;
4087 break;
4088
4089 }
4090 case SMS_VENDOR_SPECIFIC_PAGE:{
4091 switch (page_index->subpage) {
4092 case PWR_SUBPAGE_CODE: {
4093 struct copan_power_subpage *current_page,
4094 *saved_page;
4095
4096 memcpy(&lun->mode_pages.power_subpage[
4097 CTL_PAGE_CURRENT],
4098 &power_page_default,
4099 sizeof(power_page_default));
4100 memcpy(&lun->mode_pages.power_subpage[
4101 CTL_PAGE_CHANGEABLE],
4102 &power_page_changeable,
4103 sizeof(power_page_changeable));
4104 memcpy(&lun->mode_pages.power_subpage[
4105 CTL_PAGE_DEFAULT],
4106 &power_page_default,
4107 sizeof(power_page_default));
4108 memcpy(&lun->mode_pages.power_subpage[
4109 CTL_PAGE_SAVED],
4110 &power_page_default,
4111 sizeof(power_page_default));
4112 page_index->page_data =
4113 (uint8_t *)lun->mode_pages.power_subpage;
4114
4115 current_page = (struct copan_power_subpage *)
4116 (page_index->page_data +
4117 (page_index->page_len *
4118 CTL_PAGE_CURRENT));
4119 saved_page = (struct copan_power_subpage *)
4120 (page_index->page_data +
4121 (page_index->page_len *
4122 CTL_PAGE_SAVED));
4123 break;
4124 }
4125 case APS_SUBPAGE_CODE: {
4126 struct copan_aps_subpage *current_page,
4127 *saved_page;
4128
4129 // This gets set multiple times but
4130 // it should always be the same. It's
4131 // only done during init so who cares.
4132 index_to_aps_page = i;
4133
4134 memcpy(&lun->mode_pages.aps_subpage[
4135 CTL_PAGE_CURRENT],
4136 &aps_page_default,
4137 sizeof(aps_page_default));
4138 memcpy(&lun->mode_pages.aps_subpage[
4139 CTL_PAGE_CHANGEABLE],
4140 &aps_page_changeable,
4141 sizeof(aps_page_changeable));
4142 memcpy(&lun->mode_pages.aps_subpage[
4143 CTL_PAGE_DEFAULT],
4144 &aps_page_default,
4145 sizeof(aps_page_default));
4146 memcpy(&lun->mode_pages.aps_subpage[
4147 CTL_PAGE_SAVED],
4148 &aps_page_default,
4149 sizeof(aps_page_default));
4150 page_index->page_data =
4151 (uint8_t *)lun->mode_pages.aps_subpage;
4152
4153 current_page = (struct copan_aps_subpage *)
4154 (page_index->page_data +
4155 (page_index->page_len *
4156 CTL_PAGE_CURRENT));
4157 saved_page = (struct copan_aps_subpage *)
4158 (page_index->page_data +
4159 (page_index->page_len *
4160 CTL_PAGE_SAVED));
4161 break;
4162 }
4163 case DBGCNF_SUBPAGE_CODE: {
4164 struct copan_debugconf_subpage *current_page,
4165 *saved_page;
4166
4167 memcpy(&lun->mode_pages.debugconf_subpage[
4168 CTL_PAGE_CURRENT],
4169 &debugconf_page_default,
4170 sizeof(debugconf_page_default));
4171 memcpy(&lun->mode_pages.debugconf_subpage[
4172 CTL_PAGE_CHANGEABLE],
4173 &debugconf_page_changeable,
4174 sizeof(debugconf_page_changeable));
4175 memcpy(&lun->mode_pages.debugconf_subpage[
4176 CTL_PAGE_DEFAULT],
4177 &debugconf_page_default,
4178 sizeof(debugconf_page_default));
4179 memcpy(&lun->mode_pages.debugconf_subpage[
4180 CTL_PAGE_SAVED],
4181 &debugconf_page_default,
4182 sizeof(debugconf_page_default));
4183 page_index->page_data =
4184 (uint8_t *)lun->mode_pages.debugconf_subpage;
4185
4186 current_page = (struct copan_debugconf_subpage *)
4187 (page_index->page_data +
4188 (page_index->page_len *
4189 CTL_PAGE_CURRENT));
4190 saved_page = (struct copan_debugconf_subpage *)
4191 (page_index->page_data +
4192 (page_index->page_len *
4193 CTL_PAGE_SAVED));
4194 break;
4195 }
4196 default:
4197 panic("invalid subpage value %d",
4198 page_index->subpage);
4199 break;
4200 }
4201 break;
4202 }
4203 default:
4204 panic("invalid page value %d",
4205 page_index->page_code & SMPH_PC_MASK);
4206 break;
4207 }
4208 }
4209
4210 return (CTL_RETVAL_COMPLETE);
4211}
4212
4213/*
4214 * LUN allocation.
4215 *
4216 * Requirements:
4217 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4218 * wants us to allocate the LUN and he can block.
4219 * - ctl_softc is always set
4220 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4221 *
4222 * Returns 0 for success, non-zero (errno) for failure.
4223 */
4224static int
4225ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4226 struct ctl_be_lun *const be_lun, struct ctl_id target_id)
4227{
4228 struct ctl_lun *nlun, *lun;
4229 struct ctl_frontend *fe;
4230 int lun_number, i, lun_malloced;
4231
4232 if (be_lun == NULL)
4233 return (EINVAL);
4234
4235 /*
4236 * We currently only support Direct Access or Processor LUN types.
4237 */
4238 switch (be_lun->lun_type) {
4239 case T_DIRECT:
4240 break;
4241 case T_PROCESSOR:
4242 break;
4243 case T_SEQUENTIAL:
4244 case T_CHANGER:
4245 default:
4246 be_lun->lun_config_status(be_lun->be_lun,
4247 CTL_LUN_CONFIG_FAILURE);
4248 break;
4249 }
4250 if (ctl_lun == NULL) {
4251 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4252 lun_malloced = 1;
4253 } else {
4254 lun_malloced = 0;
4255 lun = ctl_lun;
4256 }
4257
4258 memset(lun, 0, sizeof(*lun));
4259 if (lun_malloced)
4260 lun->flags = CTL_LUN_MALLOCED;
4261
4262 mtx_lock(&ctl_softc->ctl_lock);
4263 /*
4264 * See if the caller requested a particular LUN number. If so, see
4265 * if it is available. Otherwise, allocate the first available LUN.
4266 */
4267 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4268 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4269 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4270 mtx_unlock(&ctl_softc->ctl_lock);
4271 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4272 printf("ctl: requested LUN ID %d is higher "
4273 "than CTL_MAX_LUNS - 1 (%d)\n",
4274 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4275 } else {
4276 /*
4277 * XXX KDM return an error, or just assign
4278 * another LUN ID in this case??
4279 */
4280 printf("ctl: requested LUN ID %d is already "
4281 "in use\n", be_lun->req_lun_id);
4282 }
4283 if (lun->flags & CTL_LUN_MALLOCED)
4284 free(lun, M_CTL);
4285 be_lun->lun_config_status(be_lun->be_lun,
4286 CTL_LUN_CONFIG_FAILURE);
4287 return (ENOSPC);
4288 }
4289 lun_number = be_lun->req_lun_id;
4290 } else {
4291 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS);
4292 if (lun_number == -1) {
4293 mtx_unlock(&ctl_softc->ctl_lock);
4294 printf("ctl: can't allocate LUN on target %ju, out of "
4295 "LUNs\n", (uintmax_t)target_id.id);
4296 if (lun->flags & CTL_LUN_MALLOCED)
4297 free(lun, M_CTL);
4298 be_lun->lun_config_status(be_lun->be_lun,
4299 CTL_LUN_CONFIG_FAILURE);
4300 return (ENOSPC);
4301 }
4302 }
4303 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4304
4305 lun->target = target_id;
4306 lun->lun = lun_number;
4307 lun->be_lun = be_lun;
4308 /*
4309 * The processor LUN is always enabled. Disk LUNs come on line
4310 * disabled, and must be enabled by the backend.
4311 */
4312 lun->flags |= CTL_LUN_DISABLED;
4313 lun->backend = be_lun->be;
4314 be_lun->ctl_lun = lun;
4315 be_lun->lun_id = lun_number;
4316 atomic_add_int(&be_lun->be->num_luns, 1);
4317 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4318 lun->flags |= CTL_LUN_STOPPED;
4319
4320 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4321 lun->flags |= CTL_LUN_INOPERABLE;
4322
4323 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4324 lun->flags |= CTL_LUN_PRIMARY_SC;
4325
4326 lun->ctl_softc = ctl_softc;
4327 TAILQ_INIT(&lun->ooa_queue);
4328 TAILQ_INIT(&lun->blocked_queue);
4329 STAILQ_INIT(&lun->error_list);
4330
4331 /*
4332 * Initialize the mode page index.
4333 */
4334 ctl_init_page_index(lun);
4335
4336 /*
4337 * Set the poweron UA for all initiators on this LUN only.
4338 */
4339 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4340 lun->pending_sense[i].ua_pending = CTL_UA_POWERON;
4341
4342 /*
4343 * Now, before we insert this lun on the lun list, set the lun
4344 * inventory changed UA for all other luns.
4345 */
4346 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4347 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4348 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE;
4349 }
4350 }
4351
4352 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4353
4354 ctl_softc->ctl_luns[lun_number] = lun;
4355
4356 ctl_softc->num_luns++;
4357
4358 /* Setup statistics gathering */
4359 lun->stats.device_type = be_lun->lun_type;
4360 lun->stats.lun_number = lun_number;
4361 if (lun->stats.device_type == T_DIRECT)
4362 lun->stats.blocksize = be_lun->blocksize;
4363 else
4364 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4365 for (i = 0;i < CTL_MAX_PORTS;i++)
4366 lun->stats.ports[i].targ_port = i;
4367
4368 mtx_unlock(&ctl_softc->ctl_lock);
4369
4370 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4371
4372 /*
4373 * Run through each registered FETD and bring it online if it isn't
4374 * already. Enable the target ID if it hasn't been enabled, and
4375 * enable this particular LUN.
4376 */
4377 STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) {
4378 int retval;
4379
4380 /*
4381 * XXX KDM this only works for ONE TARGET ID. We'll need
4382 * to do things differently if we go to a multiple target
4383 * ID scheme.
4384 */
4385 if ((fe->status & CTL_PORT_STATUS_TARG_ONLINE) == 0) {
4386
4387 retval = fe->targ_enable(fe->targ_lun_arg, target_id);
4388 if (retval != 0) {
4389 printf("ctl_alloc_lun: FETD %s port %d "
4390 "returned error %d for targ_enable on "
4391 "target %ju\n", fe->port_name,
4392 fe->targ_port, retval,
4393 (uintmax_t)target_id.id);
4394 } else
4395 fe->status |= CTL_PORT_STATUS_TARG_ONLINE;
4396 }
4397
4398 retval = fe->lun_enable(fe->targ_lun_arg, target_id,lun_number);
4399 if (retval != 0) {
4400 printf("ctl_alloc_lun: FETD %s port %d returned error "
4401 "%d for lun_enable on target %ju lun %d\n",
4402 fe->port_name, fe->targ_port, retval,
4403 (uintmax_t)target_id.id, lun_number);
4404 } else
4405 fe->status |= CTL_PORT_STATUS_LUN_ONLINE;
4406 }
4407 return (0);
4408}
4409
4410/*
4411 * Delete a LUN.
4412 * Assumptions:
4413 * - LUN has already been marked invalid and any pending I/O has been taken
4414 * care of.
4415 */
4416static int
4417ctl_free_lun(struct ctl_lun *lun)
4418{
4419 struct ctl_softc *softc;
4420#if 0
4421 struct ctl_frontend *fe;
4422#endif
4423 struct ctl_lun *nlun;
4424 union ctl_io *io, *next_io;
4425 int i;
4426
4427 softc = lun->ctl_softc;
4428
4429 mtx_assert(&softc->ctl_lock, MA_OWNED);
4430
4431 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4432
4433 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4434
4435 softc->ctl_luns[lun->lun] = NULL;
4436
4437 if (TAILQ_FIRST(&lun->ooa_queue) != NULL) {
4438 printf("ctl_free_lun: aieee!! freeing a LUN with "
4439 "outstanding I/O!!\n");
4440 }
4441
4442 /*
4443 * If we have anything pending on the RtR queue, remove it.
4444 */
4445 for (io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue); io != NULL;
4446 io = next_io) {
4447 uint32_t targ_lun;
4448
4449 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
4450 targ_lun = io->io_hdr.nexus.targ_lun;
4451 if (io->io_hdr.nexus.lun_map_fn != NULL)
4452 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
4453 if ((io->io_hdr.nexus.targ_target.id == lun->target.id)
4454 && (targ_lun == lun->lun))
4455 STAILQ_REMOVE(&softc->rtr_queue, &io->io_hdr,
4456 ctl_io_hdr, links);
4457 }
4458
4459 /*
4460 * Then remove everything from the blocked queue.
4461 */
4462 for (io = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); io != NULL;
4463 io = next_io) {
4464 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,blocked_links);
4465 TAILQ_REMOVE(&lun->blocked_queue, &io->io_hdr, blocked_links);
4466 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
4467 }
4468
4469 /*
4470 * Now clear out the OOA queue, and free all the I/O.
4471 * XXX KDM should we notify the FETD here? We probably need to
4472 * quiesce the LUN before deleting it.
4473 */
4474 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); io != NULL;
4475 io = next_io) {
4476 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, ooa_links);
4477 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
4478 ctl_free_io(io);
4479 }
4480
4481 softc->num_luns--;
4482
4483 /*
4484 * XXX KDM this scheme only works for a single target/multiple LUN
4485 * setup. It needs to be revamped for a multiple target scheme.
4486 *
4487 * XXX KDM this results in fe->lun_disable() getting called twice,
4488 * once when ctl_disable_lun() is called, and a second time here.
4489 * We really need to re-think the LUN disable semantics. There
4490 * should probably be several steps/levels to LUN removal:
4491 * - disable
4492 * - invalidate
4493 * - free
4494 *
4495 * Right now we only have a disable method when communicating to
4496 * the front end ports, at least for individual LUNs.
4497 */
4498#if 0
4499 STAILQ_FOREACH(fe, &softc->fe_list, links) {
4500 int retval;
4501
4502 retval = fe->lun_disable(fe->targ_lun_arg, lun->target,
4503 lun->lun);
4504 if (retval != 0) {
4505 printf("ctl_free_lun: FETD %s port %d returned error "
4506 "%d for lun_disable on target %ju lun %jd\n",
4507 fe->port_name, fe->targ_port, retval,
4508 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4509 }
4510
4511 if (STAILQ_FIRST(&softc->lun_list) == NULL) {
4512 fe->status &= ~CTL_PORT_STATUS_LUN_ONLINE;
4513
4514 retval = fe->targ_disable(fe->targ_lun_arg,lun->target);
4515 if (retval != 0) {
4516 printf("ctl_free_lun: FETD %s port %d "
4517 "returned error %d for targ_disable on "
4518 "target %ju\n", fe->port_name,
4519 fe->targ_port, retval,
4520 (uintmax_t)lun->target.id);
4521 } else
4522 fe->status &= ~CTL_PORT_STATUS_TARG_ONLINE;
4523
4524 if ((fe->status & CTL_PORT_STATUS_TARG_ONLINE) != 0)
4525 continue;
4526
4527#if 0
4528 fe->port_offline(fe->onoff_arg);
4529 fe->status &= ~CTL_PORT_STATUS_ONLINE;
4530#endif
4531 }
4532 }
4533#endif
4534
4535 /*
4536 * Tell the backend to free resources, if this LUN has a backend.
4537 */
4538 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4539 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4540
4541 if (lun->flags & CTL_LUN_MALLOCED)
4542 free(lun, M_CTL);
4543
4544 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4545 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4546 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE;
4547 }
4548 }
4549
4550 return (0);
4551}
4552
4553static void
4554ctl_create_lun(struct ctl_be_lun *be_lun)
4555{
4556 struct ctl_softc *ctl_softc;
4557
4558 ctl_softc = control_softc;
4559
4560 /*
4561 * ctl_alloc_lun() should handle all potential failure cases.
4562 */
4563 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target);
4564}
4565
4566int
4567ctl_add_lun(struct ctl_be_lun *be_lun)
4568{
4569 struct ctl_softc *ctl_softc;
4570
4571 ctl_softc = control_softc;
4572
4573 mtx_lock(&ctl_softc->ctl_lock);
4574 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links);
4575 mtx_unlock(&ctl_softc->ctl_lock);
4576
4577 ctl_wakeup_thread();
4578
4579 return (0);
4580}
4581
4582int
4583ctl_enable_lun(struct ctl_be_lun *be_lun)
4584{
4585 struct ctl_softc *ctl_softc;
4586 struct ctl_frontend *fe, *nfe;
4587 struct ctl_lun *lun;
4588 int retval;
4589
4590 ctl_softc = control_softc;
4591
4592 lun = (struct ctl_lun *)be_lun->ctl_lun;
4593
4594 mtx_lock(&ctl_softc->ctl_lock);
4595 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4596 /*
4597 * eh? Why did we get called if the LUN is already
4598 * enabled?
4599 */
4600 mtx_unlock(&ctl_softc->ctl_lock);
4601 return (0);
4602 }
4603 lun->flags &= ~CTL_LUN_DISABLED;
4604
4605 for (fe = STAILQ_FIRST(&ctl_softc->fe_list); fe != NULL; fe = nfe) {
4606 nfe = STAILQ_NEXT(fe, links);
4607
4608 /*
4609 * Drop the lock while we call the FETD's enable routine.
4610 * This can lead to a callback into CTL (at least in the
4611 * case of the internal initiator frontend.
4612 */
4613 mtx_unlock(&ctl_softc->ctl_lock);
4614 retval = fe->lun_enable(fe->targ_lun_arg, lun->target,lun->lun);
4615 mtx_lock(&ctl_softc->ctl_lock);
4616 if (retval != 0) {
4617 printf("%s: FETD %s port %d returned error "
4618 "%d for lun_enable on target %ju lun %jd\n",
4619 __func__, fe->port_name, fe->targ_port, retval,
4620 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4621 }
4622#if 0
4623 else {
4624 /* NOTE: TODO: why does lun enable affect port status? */
4625 fe->status |= CTL_PORT_STATUS_LUN_ONLINE;
4626 }
4627#endif
4628 }
4629
4630 mtx_unlock(&ctl_softc->ctl_lock);
4631
4632 return (0);
4633}
4634
4635int
4636ctl_disable_lun(struct ctl_be_lun *be_lun)
4637{
4638 struct ctl_softc *ctl_softc;
4639 struct ctl_frontend *fe;
4640 struct ctl_lun *lun;
4641 int retval;
4642
4643 ctl_softc = control_softc;
4644
4645 lun = (struct ctl_lun *)be_lun->ctl_lun;
4646
4647 mtx_lock(&ctl_softc->ctl_lock);
4648
4649 if (lun->flags & CTL_LUN_DISABLED) {
4650 mtx_unlock(&ctl_softc->ctl_lock);
4651 return (0);
4652 }
4653 lun->flags |= CTL_LUN_DISABLED;
4654
4655 STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) {
4656 mtx_unlock(&ctl_softc->ctl_lock);
4657 /*
4658 * Drop the lock before we call the frontend's disable
4659 * routine, to avoid lock order reversals.
4660 *
4661 * XXX KDM what happens if the frontend list changes while
4662 * we're traversing it? It's unlikely, but should be handled.
4663 */
4664 retval = fe->lun_disable(fe->targ_lun_arg, lun->target,
4665 lun->lun);
4666 mtx_lock(&ctl_softc->ctl_lock);
4667 if (retval != 0) {
4668 printf("ctl_alloc_lun: FETD %s port %d returned error "
4669 "%d for lun_disable on target %ju lun %jd\n",
4670 fe->port_name, fe->targ_port, retval,
4671 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4672 }
4673 }
4674
4675 mtx_unlock(&ctl_softc->ctl_lock);
4676
4677 return (0);
4678}
4679
4680int
4681ctl_start_lun(struct ctl_be_lun *be_lun)
4682{
4683 struct ctl_softc *ctl_softc;
4684 struct ctl_lun *lun;
4685
4686 ctl_softc = control_softc;
4687
4688 lun = (struct ctl_lun *)be_lun->ctl_lun;
4689
4690 mtx_lock(&ctl_softc->ctl_lock);
4691 lun->flags &= ~CTL_LUN_STOPPED;
4692 mtx_unlock(&ctl_softc->ctl_lock);
4693
4694 return (0);
4695}
4696
4697int
4698ctl_stop_lun(struct ctl_be_lun *be_lun)
4699{
4700 struct ctl_softc *ctl_softc;
4701 struct ctl_lun *lun;
4702
4703 ctl_softc = control_softc;
4704
4705 lun = (struct ctl_lun *)be_lun->ctl_lun;
4706
4707 mtx_lock(&ctl_softc->ctl_lock);
4708 lun->flags |= CTL_LUN_STOPPED;
4709 mtx_unlock(&ctl_softc->ctl_lock);
4710
4711 return (0);
4712}
4713
4714int
4715ctl_lun_offline(struct ctl_be_lun *be_lun)
4716{
4717 struct ctl_softc *ctl_softc;
4718 struct ctl_lun *lun;
4719
4720 ctl_softc = control_softc;
4721
4722 lun = (struct ctl_lun *)be_lun->ctl_lun;
4723
4724 mtx_lock(&ctl_softc->ctl_lock);
4725 lun->flags |= CTL_LUN_OFFLINE;
4726 mtx_unlock(&ctl_softc->ctl_lock);
4727
4728 return (0);
4729}
4730
4731int
4732ctl_lun_online(struct ctl_be_lun *be_lun)
4733{
4734 struct ctl_softc *ctl_softc;
4735 struct ctl_lun *lun;
4736
4737 ctl_softc = control_softc;
4738
4739 lun = (struct ctl_lun *)be_lun->ctl_lun;
4740
4741 mtx_lock(&ctl_softc->ctl_lock);
4742 lun->flags &= ~CTL_LUN_OFFLINE;
4743 mtx_unlock(&ctl_softc->ctl_lock);
4744
4745 return (0);
4746}
4747
4748int
4749ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4750{
4751 struct ctl_softc *ctl_softc;
4752 struct ctl_lun *lun;
4753
4754 ctl_softc = control_softc;
4755
4756 lun = (struct ctl_lun *)be_lun->ctl_lun;
4757
4758 mtx_lock(&ctl_softc->ctl_lock);
4759
4760 /*
4761 * The LUN needs to be disabled before it can be marked invalid.
4762 */
4763 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4764 mtx_unlock(&ctl_softc->ctl_lock);
4765 return (-1);
4766 }
4767 /*
4768 * Mark the LUN invalid.
4769 */
4770 lun->flags |= CTL_LUN_INVALID;
4771
4772 /*
4773 * If there is nothing in the OOA queue, go ahead and free the LUN.
4774 * If we have something in the OOA queue, we'll free it when the
4775 * last I/O completes.
4776 */
4777 if (TAILQ_FIRST(&lun->ooa_queue) == NULL)
4778 ctl_free_lun(lun);
4779 mtx_unlock(&ctl_softc->ctl_lock);
4780
4781 return (0);
4782}
4783
4784int
4785ctl_lun_inoperable(struct ctl_be_lun *be_lun)
4786{
4787 struct ctl_softc *ctl_softc;
4788 struct ctl_lun *lun;
4789
4790 ctl_softc = control_softc;
4791 lun = (struct ctl_lun *)be_lun->ctl_lun;
4792
4793 mtx_lock(&ctl_softc->ctl_lock);
4794 lun->flags |= CTL_LUN_INOPERABLE;
4795 mtx_unlock(&ctl_softc->ctl_lock);
4796
4797 return (0);
4798}
4799
4800int
4801ctl_lun_operable(struct ctl_be_lun *be_lun)
4802{
4803 struct ctl_softc *ctl_softc;
4804 struct ctl_lun *lun;
4805
4806 ctl_softc = control_softc;
4807 lun = (struct ctl_lun *)be_lun->ctl_lun;
4808
4809 mtx_lock(&ctl_softc->ctl_lock);
4810 lun->flags &= ~CTL_LUN_INOPERABLE;
4811 mtx_unlock(&ctl_softc->ctl_lock);
4812
4813 return (0);
4814}
4815
4816int
4817ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus,
4818 int lock)
4819{
4820 struct ctl_softc *softc;
4821 struct ctl_lun *lun;
4822 struct copan_aps_subpage *current_sp;
4823 struct ctl_page_index *page_index;
4824 int i;
4825
4826 softc = control_softc;
4827
4828 mtx_lock(&softc->ctl_lock);
4829
4830 lun = (struct ctl_lun *)be_lun->ctl_lun;
4831
4832 page_index = NULL;
4833 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4834 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
4835 APS_PAGE_CODE)
4836 continue;
4837
4838 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE)
4839 continue;
4840 page_index = &lun->mode_pages.index[i];
4841 }
4842
4843 if (page_index == NULL) {
4844 mtx_unlock(&softc->ctl_lock);
4845 printf("%s: APS subpage not found for lun %ju!\n", __func__,
4846 (uintmax_t)lun->lun);
4847 return (1);
4848 }
4849#if 0
4850 if ((softc->aps_locked_lun != 0)
4851 && (softc->aps_locked_lun != lun->lun)) {
4852 printf("%s: attempt to lock LUN %llu when %llu is already "
4853 "locked\n");
4854 mtx_unlock(&softc->ctl_lock);
4855 return (1);
4856 }
4857#endif
4858
4859 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
4860 (page_index->page_len * CTL_PAGE_CURRENT));
4861
4862 if (lock != 0) {
4863 current_sp->lock_active = APS_LOCK_ACTIVE;
4864 softc->aps_locked_lun = lun->lun;
4865 } else {
4866 current_sp->lock_active = 0;
4867 softc->aps_locked_lun = 0;
4868 }
4869
4870
4871 /*
4872 * If we're in HA mode, try to send the lock message to the other
4873 * side.
4874 */
4875 if (ctl_is_single == 0) {
4876 int isc_retval;
4877 union ctl_ha_msg lock_msg;
4878
4879 lock_msg.hdr.nexus = *nexus;
4880 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK;
4881 if (lock != 0)
4882 lock_msg.aps.lock_flag = 1;
4883 else
4884 lock_msg.aps.lock_flag = 0;
4885 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg,
4886 sizeof(lock_msg), 0);
4887 if (isc_retval > CTL_HA_STATUS_SUCCESS) {
4888 printf("%s: APS (lock=%d) error returned from "
4889 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval);
4890 mtx_unlock(&softc->ctl_lock);
4891 return (1);
4892 }
4893 }
4894
4895 mtx_unlock(&softc->ctl_lock);
4896
4897 return (0);
4898}
4899
4900void
4901ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4902{
4903 struct ctl_lun *lun;
4904 struct ctl_softc *softc;
4905 int i;
4906
4907 softc = control_softc;
4908
4909 mtx_lock(&softc->ctl_lock);
4910
4911 lun = (struct ctl_lun *)be_lun->ctl_lun;
4912
4913 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4914 lun->pending_sense[i].ua_pending |= CTL_UA_CAPACITY_CHANGED;
4915
4916 mtx_unlock(&softc->ctl_lock);
4917}
4918
4919/*
4920 * Backend "memory move is complete" callback for requests that never
4921 * make it down to say RAIDCore's configuration code.
4922 */
4923int
4924ctl_config_move_done(union ctl_io *io)
4925{
4926 int retval;
4927
4928 retval = CTL_RETVAL_COMPLETE;
4929
4930
4931 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
4932 /*
4933 * XXX KDM this shouldn't happen, but what if it does?
4934 */
4935 if (io->io_hdr.io_type != CTL_IO_SCSI)
4936 panic("I/O type isn't CTL_IO_SCSI!");
4937
4938 if ((io->io_hdr.port_status == 0)
4939 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
4940 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE))
4941 io->io_hdr.status = CTL_SUCCESS;
4942 else if ((io->io_hdr.port_status != 0)
4943 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
4944 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){
4945 /*
4946 * For hardware error sense keys, the sense key
4947 * specific value is defined to be a retry count,
4948 * but we use it to pass back an internal FETD
4949 * error code. XXX KDM Hopefully the FETD is only
4950 * using 16 bits for an error code, since that's
4951 * all the space we have in the sks field.
4952 */
4953 ctl_set_internal_failure(&io->scsiio,
4954 /*sks_valid*/ 1,
4955 /*retry_count*/
4956 io->io_hdr.port_status);
4957 free(io->scsiio.kern_data_ptr, M_CTL);
4958 ctl_done(io);
4959 goto bailout;
4960 }
4961
4962 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
4963 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
4964 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
4965 /*
4966 * XXX KDM just assuming a single pointer here, and not a
4967 * S/G list. If we start using S/G lists for config data,
4968 * we'll need to know how to clean them up here as well.
4969 */
4970 free(io->scsiio.kern_data_ptr, M_CTL);
4971 /* Hopefully the user has already set the status... */
4972 ctl_done(io);
4973 } else {
4974 /*
4975 * XXX KDM now we need to continue data movement. Some
4976 * options:
4977 * - call ctl_scsiio() again? We don't do this for data
4978 * writes, because for those at least we know ahead of
4979 * time where the write will go and how long it is. For
4980 * config writes, though, that information is largely
4981 * contained within the write itself, thus we need to
4982 * parse out the data again.
4983 *
4984 * - Call some other function once the data is in?
4985 */
4986
4987 /*
4988 * XXX KDM call ctl_scsiio() again for now, and check flag
4989 * bits to see whether we're allocated or not.
4990 */
4991 retval = ctl_scsiio(&io->scsiio);
4992 }
4993bailout:
4994 return (retval);
4995}
4996
4997/*
4998 * This gets called by a backend driver when it is done with a
46
47#include <sys/param.h>
48#include <sys/systm.h>
49#include <sys/kernel.h>
50#include <sys/types.h>
51#include <sys/kthread.h>
52#include <sys/bio.h>
53#include <sys/fcntl.h>
54#include <sys/lock.h>
55#include <sys/module.h>
56#include <sys/mutex.h>
57#include <sys/condvar.h>
58#include <sys/malloc.h>
59#include <sys/conf.h>
60#include <sys/ioccom.h>
61#include <sys/queue.h>
62#include <sys/sbuf.h>
63#include <sys/smp.h>
64#include <sys/endian.h>
65#include <sys/sysctl.h>
66
67#include <cam/cam.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>
82
83struct ctl_softc *control_softc = NULL;
84
85/*
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.
90 */
91#define CTL_DONE_THREAD
92
93/*
94 * Use the serial number and device ID provided by the backend, rather than
95 * making up our own.
96 */
97#define CTL_USE_BACKEND_SN
98
99/*
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
102 * hardware.
103 */
104#define CTL_ALIGN_8B(target, source, type) \
105 if (((uint32_t)source & 0x7) != 0) \
106 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\
107 else \
108 target = (type)source;
109
110#define CTL_SIZE_8B(target, size) \
111 if ((size & 0x7) != 0) \
112 target = size + (0x8 - (size & 0x7)); \
113 else \
114 target = size;
115
116#define CTL_ALIGN_8B_MARGIN 16
117
118/*
119 * Template mode pages.
120 */
121
122/*
123 * Note that these are default values only. The actual values will be
124 * filled in when the user does a mode sense.
125 */
126static 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,
132 /* total_luns */ 26,
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,
136 0, 0, 0, 0, 0, 0}
137};
138
139static 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},
144 /*page_version*/ 0,
145 /* total_luns */ 0,
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,
149 0, 0, 0, 0, 0, 0}
150};
151
152static 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
158 0, //lock_active
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
162};
163
164static 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
169 0, //page_version
170 0, //lock_active
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
174};
175
176static 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 */
184};
185
186static 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 */
193};
194
195static 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},
208 /*flags*/ SFP_HSEC,
209 /*reserved*/ {0, 0, 0}
210};
211
212static 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},
224 /*flags*/ 0,
225 /*reserved*/ {0, 0, 0}
226};
227
228static 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,
239 /*reserved1*/ 0,
240 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
241 CTL_DEFAULT_ROTATION_RATE & 0xff},
242 /*reserved2*/ {0, 0}
243};
244
245static 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},
249 /*heads*/ 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},
254 /*rpl*/ 0,
255 /*rotational_offset*/ 0,
256 /*reserved1*/ 0,
257 /*rotation_rate*/ {0, 0},
258 /*reserved2*/ {0, 0}
259};
260
261static 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,
265 /*ret_priority*/ 0,
266 /*disable_pf_transfer_len*/ {0xff, 0xff},
267 /*min_prefetch*/ {0, 0},
268 /*max_prefetch*/ {0xff, 0xff},
269 /*max_pf_ceiling*/ {0xff, 0xff},
270 /*flags2*/ 0,
271 /*cache_segments*/ 0,
272 /*cache_seg_size*/ {0, 0},
273 /*reserved*/ 0,
274 /*non_cache_seg_size*/ {0, 0, 0}
275};
276
277static struct scsi_caching_page caching_page_changeable = {
278 /*page_code*/SMS_CACHING_PAGE,
279 /*page_length*/sizeof(struct scsi_caching_page) - 2,
280 /*flags1*/ 0,
281 /*ret_priority*/ 0,
282 /*disable_pf_transfer_len*/ {0, 0},
283 /*min_prefetch*/ {0, 0},
284 /*max_prefetch*/ {0, 0},
285 /*max_pf_ceiling*/ {0, 0},
286 /*flags2*/ 0,
287 /*cache_segments*/ 0,
288 /*cache_seg_size*/ {0, 0},
289 /*reserved*/ 0,
290 /*non_cache_seg_size*/ {0, 0, 0}
291};
292
293static struct scsi_control_page control_page_default = {
294 /*page_code*/SMS_CONTROL_MODE_PAGE,
295 /*page_length*/sizeof(struct scsi_control_page) - 2,
296 /*rlec*/0,
297 /*queue_flags*/0,
298 /*eca_and_aen*/0,
299 /*reserved*/0,
300 /*aen_holdoff_period*/{0, 0}
301};
302
303static struct scsi_control_page control_page_changeable = {
304 /*page_code*/SMS_CONTROL_MODE_PAGE,
305 /*page_length*/sizeof(struct scsi_control_page) - 2,
306 /*rlec*/SCP_DSENSE,
307 /*queue_flags*/0,
308 /*eca_and_aen*/0,
309 /*reserved*/0,
310 /*aen_holdoff_period*/{0, 0}
311};
312
313
314/*
315 * XXX KDM move these into the softc.
316 */
317static int rcv_sync_msg;
318static int persis_offset;
319static uint8_t ctl_pause_rtr;
320static int ctl_is_single = 1;
321static int index_to_aps_page;
322
323SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
324static int worker_threads = 1;
325TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads);
326SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
327 &worker_threads, 1, "Number of worker threads");
328static int verbose = 0;
329TUNABLE_INT("kern.cam.ctl.verbose", &verbose);
330SYSCTL_INT(_kern_cam_ctl, OID_AUTO, verbose, CTLFLAG_RWTUN,
331 &verbose, 0, "Show SCSI errors returned to initiator");
332
333/*
334 * Serial number (0x80), device id (0x83), supported pages (0x00),
335 * Block limits (0xB0) and Logical Block Provisioning (0xB2)
336 */
337#define SCSI_EVPD_NUM_SUPPORTED_PAGES 5
338
339static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
340 int param);
341static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
342static int ctl_init(void);
343void ctl_shutdown(void);
344static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
345static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
346static void ctl_ioctl_online(void *arg);
347static void ctl_ioctl_offline(void *arg);
348static int ctl_ioctl_targ_enable(void *arg, struct ctl_id targ_id);
349static int ctl_ioctl_targ_disable(void *arg, struct ctl_id targ_id);
350static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id);
351static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id);
352static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio);
353static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio, int have_lock);
354static int ctl_ioctl_submit_wait(union ctl_io *io);
355static void ctl_ioctl_datamove(union ctl_io *io);
356static void ctl_ioctl_done(union ctl_io *io);
357static void ctl_ioctl_hard_startstop_callback(void *arg,
358 struct cfi_metatask *metatask);
359static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask);
360static 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);
363static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
364 struct thread *td);
365uint32_t ctl_get_resindex(struct ctl_nexus *nexus);
366uint32_t ctl_port_idx(int port_num);
367#ifdef unused
368static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port,
369 uint32_t targ_target, uint32_t targ_lun,
370 int can_wait);
371static void ctl_kfree_io(union ctl_io *io);
372#endif /* unused */
373static 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);
375static int ctl_free_lun(struct ctl_lun *lun);
376static void ctl_create_lun(struct ctl_be_lun *be_lun);
377/**
378static void ctl_failover_change_pages(struct ctl_softc *softc,
379 struct ctl_scsiio *ctsio, int master);
380**/
381
382static int ctl_do_mode_select(union ctl_io *io);
383static 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);
389static void ctl_pro_preempt_other(struct ctl_lun *lun,
390 union ctl_ha_msg *msg);
391static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
392static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
393static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
394static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
395static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
396 int alloc_len);
397static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
398static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
399static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
400static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len);
401static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2);
402static ctl_action ctl_check_for_blockage(union ctl_io *pending_io,
403 union ctl_io *ooa_io);
404static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
405 union ctl_io *starting_io);
406static int ctl_check_blocked(struct ctl_lun *lun);
407static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc,
408 struct ctl_lun *lun,
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);
412static void ctl_failover(void);
413static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
414 struct ctl_scsiio *ctsio);
415static int ctl_scsiio(struct ctl_scsiio *ctsio);
416
417static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
418static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
419 ctl_ua_type ua_type);
420static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io,
421 ctl_ua_type ua_type);
422static int ctl_abort_task(union ctl_io *io);
423static void ctl_run_task_queue(struct ctl_softc *ctl_softc);
424#ifdef CTL_IO_DELAY
425static void ctl_datamove_timer_wakeup(void *arg);
426static void ctl_done_timer_wakeup(void *arg);
427#endif /* CTL_IO_DELAY */
428
429static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
430static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
431static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
432static void ctl_datamove_remote_write(union ctl_io *io);
433static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
434static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
435static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
436static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
437 ctl_ha_dt_cb callback);
438static void ctl_datamove_remote_read(union ctl_io *io);
439static void ctl_datamove_remote(union ctl_io *io);
440static int ctl_process_done(union ctl_io *io, int have_lock);
441static void ctl_work_thread(void *arg);
442
443/*
444 * Load the serialization table. This isn't very pretty, but is probably
445 * the easiest way to do it.
446 */
447#include "ctl_ser_table.c"
448
449/*
450 * We only need to define open, close and ioctl routines for this driver.
451 */
452static struct cdevsw ctl_cdevsw = {
453 .d_version = D_VERSION,
454 .d_flags = 0,
455 .d_open = ctl_open,
456 .d_close = ctl_close,
457 .d_ioctl = ctl_ioctl,
458 .d_name = "ctl",
459};
460
461
462MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
463
464static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
465
466static moduledata_t ctl_moduledata = {
467 "ctl",
468 ctl_module_event_handler,
469 NULL
470};
471
472DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
473MODULE_VERSION(ctl, 1);
474
475static void
476ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
477 union ctl_ha_msg *msg_info)
478{
479 struct ctl_scsiio *ctsio;
480
481 if (msg_info->hdr.original_sc == NULL) {
482 printf("%s: original_sc == NULL!\n", __func__);
483 /* XXX KDM now what? */
484 return;
485 }
486
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);
500 ctl_wakeup_thread();
501}
502
503static void
504ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
505 union ctl_ha_msg *msg_info)
506{
507 struct ctl_scsiio *ctsio;
508
509 if (msg_info->hdr.serializing_sc == NULL) {
510 printf("%s: serializing_sc == NULL!\n", __func__);
511 /* XXX KDM now what? */
512 return;
513 }
514
515 ctsio = &msg_info->hdr.serializing_sc->scsiio;
516#if 0
517 /*
518 * Attempt to catch the situation where an I/O has
519 * been freed, and we're using it again.
520 */
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__,
525 ctsio);
526 printf("%s: type %d msg %d cdb %x iptl: "
527 "%d:%d:%d:%d tag 0x%04x "
528 "flag %#x status %x\n",
529 __func__,
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 ==
538 CTL_IO_TASK) ?
539 tmp_io->taskio.tag_num :
540 tmp_io->scsiio.tag_num,
541 tmp_io->io_hdr.flags,
542 tmp_io->io_hdr.status);
543 }
544#endif
545 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
546 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, &ctsio->io_hdr, links);
547 ctl_wakeup_thread();
548}
549
550/*
551 * ISC (Inter Shelf Communication) event handler. Events from the HA
552 * subsystem come in here.
553 */
554static void
555ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
556{
557 struct ctl_softc *ctl_softc;
558 union ctl_io *io;
559 struct ctl_prio *presio;
560 ctl_ha_status isc_status;
561
562 ctl_softc = control_softc;
563 io = NULL;
564
565
566#if 0
567 printf("CTL: Isc Msg event %d\n", event);
568#endif
569 if (event == CTL_HA_EVT_MSG_RECV) {
570 union ctl_ha_msg msg_info;
571
572 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
573 sizeof(msg_info), /*wait*/ 0);
574#if 0
575 printf("CTL: msg_type %d\n", msg_info.msg_type);
576#endif
577 if (isc_status != 0) {
578 printf("Error receiving message, status = %d\n",
579 isc_status);
580 return;
581 }
582 mtx_lock(&ctl_softc->ctl_lock);
583
584 switch (msg_info.hdr.msg_type) {
585 case CTL_MSG_SERIALIZE:
586#if 0
587 printf("Serialize\n");
588#endif
589 io = ctl_alloc_io((void *)ctl_softc->othersc_pool);
590 if (io == NULL) {
591 printf("ctl_isc_event_handler: can't allocate "
592 "ctl_io!\n");
593 /* Bad Juju */
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){
602 }
603 goto bailout;
604 }
605 ctl_zero_io(io);
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;
610#if 0
611 printf("pOrig %x\n", (int)msg_info.original_sc);
612#endif
613 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
614 CTL_FLAG_IO_ACTIVE;
615 /*
616 * If we're in serialization-only mode, we don't
617 * want to go through full done processing. Thus
618 * the COPY flag.
619 *
620 * XXX KDM add another flag that is more specific.
621 */
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;
625#if 0
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);
631#endif
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,
635 CTL_MAX_CDBLEN);
636 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
637 struct ctl_cmd_entry *entry;
638 uint8_t opcode;
639
640 opcode = io->scsiio.cdb[0];
641 entry = &ctl_cmd_table[opcode];
642 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
643 io->io_hdr.flags |=
644 entry->flags & CTL_FLAG_DATA_MASK;
645 }
646 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
647 &io->io_hdr, links);
648 ctl_wakeup_thread();
649 break;
650
651 /* Performed on the Originating SC, XFER mode only */
652 case CTL_MSG_DATAMOVE: {
653 struct ctl_sg_entry *sgl;
654 int i, j;
655
656 io = msg_info.hdr.original_sc;
657 if (io == NULL) {
658 printf("%s: original_sc == NULL!\n", __func__);
659 /* XXX KDM do something here */
660 break;
661 }
662 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
663 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
664 /*
665 * Keep track of this, we need to send it back over
666 * when the datamove is complete.
667 */
668 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
669
670 if (msg_info.dt.sg_sequence == 0) {
671 /*
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
675 * lists.
676 */
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",
682 __func__,
683 msg_info.dt.kern_sg_entries,
684 sizeof(io->io_hdr.remote_sglist)/
685 sizeof(io->io_hdr.remote_sglist[0]));
686
687 /*
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.
692 */
693 break;
694 }
695 sgl = io->io_hdr.remote_sglist;
696 memset(sgl, 0,
697 sizeof(io->io_hdr.remote_sglist));
698
699 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
700
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;
713 /*
714 * Clear out per-DMA flags.
715 */
716 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK;
717 /*
718 * Add per-DMA flags that are set for this
719 * particular DMA request.
720 */
721 io->io_hdr.flags |= msg_info.dt.flags &
722 CTL_FLAG_RDMA_MASK;
723 } else
724 sgl = (struct ctl_sg_entry *)
725 io->scsiio.kern_data_ptr;
726
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;
732
733#if 0
734 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n",
735 __func__,
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);
739#endif
740 }
741#if 0
742 memcpy(&sgl[msg_info.dt.sent_sg_entries],
743 msg_info.dt.sg_list,
744 sizeof(*sgl) * msg_info.dt.cur_sg_entries);
745#endif
746
747 /*
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.
751 */
752 if (msg_info.dt.sg_last != 0) {
753 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
754 &io->io_hdr, links);
755 ctl_wakeup_thread();
756 }
757 break;
758 }
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",
763 __func__);
764 /* XXX KDM now what? */
765 break;
766 }
767 /*
768 * We grab the sense information here in case
769 * there was a failure, so we can return status
770 * back to the initiator.
771 */
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));
782
783 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
784 &io->io_hdr, links);
785 ctl_wakeup_thread();
786 break;
787 }
788
789 /* Preformed on Originating SC, SER_ONLY mode */
790 case CTL_MSG_R2R:
791 io = msg_info.hdr.original_sc;
792 if (io == NULL) {
793 printf("%s: Major Bummer\n", __func__);
794 mtx_unlock(&ctl_softc->ctl_lock);
795 return;
796 } else {
797#if 0
798 printf("pOrig %x\n",(int) ctsio);
799#endif
800 }
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,
804 &io->io_hdr, links);
805 ctl_wakeup_thread();
806 break;
807
808 /*
809 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
810 * mode.
811 * Performed on the Originating (i.e. secondary) SC in XFER
812 * mode
813 */
814 case CTL_MSG_FINISH_IO:
815 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER)
816 ctl_isc_handler_finish_xfer(ctl_softc,
817 &msg_info);
818 else
819 ctl_isc_handler_finish_ser_only(ctl_softc,
820 &msg_info);
821 break;
822
823 /* Preformed on Originating SC */
824 case CTL_MSG_BAD_JUJU:
825 io = msg_info.hdr.original_sc;
826 if (io == NULL) {
827 printf("%s: Bad JUJU!, original_sc is NULL!\n",
828 __func__);
829 break;
830 }
831 ctl_copy_sense_data(&msg_info, io);
832 /*
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.
836 */
837 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
838 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
839
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,
843 &io->io_hdr, links);
844 ctl_wakeup_thread();
845 break;
846
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 "
854 "ctl_io!\n");
855 /* Bad Juju */
856 /* should I just call the proper reset func
857 here??? */
858 mtx_unlock(&ctl_softc->ctl_lock);
859 goto bailout;
860 }
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;
868#ifdef CTL_TIME_IO
869 taskio->io_hdr.start_time = time_uptime;
870 getbintime(&taskio->io_hdr.start_bt);
871#if 0
872 cs_prof_gettime(&taskio->io_hdr.start_ticks);
873#endif
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;
878 ctl_wakeup_thread();
879 break;
880 }
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 "
887 "ctl_io!\n");
888 /* Bad Juju */
889 /* Need to set busy and send msg back */
890 mtx_unlock(&ctl_softc->ctl_lock);
891 goto bailout;
892 }
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);
898 ctl_wakeup_thread();
899 break;
900 case CTL_MSG_SYNC_FE:
901 rcv_sync_msg = 1;
902 break;
903 case CTL_MSG_APS_LOCK: {
904 // It's quicker to execute this then to
905 // queue it.
906 struct ctl_lun *lun;
907 struct ctl_page_index *page_index;
908 struct copan_aps_subpage *current_sp;
909 uint32_t targ_lun;
910
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);
914
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));
920
921 current_sp->lock_active = msg_info.aps.lock_flag;
922 break;
923 }
924 default:
925 printf("How did I get here?\n");
926 }
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",
931 param);
932 }
933 return;
934 } else if (event == CTL_HA_EVT_DISCONNECT) {
935 printf("CTL: Got a disconnect from Isc\n");
936 return;
937 } else {
938 printf("ctl_isc_event_handler: Unknown event %d\n", event);
939 return;
940 }
941
942bailout:
943 return;
944}
945
946static void
947ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
948{
949 struct scsi_sense_data *sense;
950
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;
956}
957
958static int
959ctl_init(void)
960{
961 struct ctl_softc *softc;
962 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool;
963 struct ctl_frontend *fe;
964 uint8_t sc_id =0;
965 int i, error, retval;
966 //int isc_retval;
967
968 retval = 0;
969 ctl_pause_rtr = 0;
970 rcv_sync_msg = 0;
971
972 control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
973 M_WAITOK | M_ZERO);
974 softc = control_softc;
975
976 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
977 "cam/ctl");
978
979 softc->dev->si_drv1 = softc;
980
981 /*
982 * By default, return a "bad LUN" peripheral qualifier for unknown
983 * LUNs. The user can override this default using the tunable or
984 * sysctl. See the comment in ctl_inquiry_std() for more details.
985 */
986 softc->inquiry_pq_no_lun = 1;
987 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun",
988 &softc->inquiry_pq_no_lun);
989 sysctl_ctx_init(&softc->sysctl_ctx);
990 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
991 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
992 CTLFLAG_RD, 0, "CAM Target Layer");
993
994 if (softc->sysctl_tree == NULL) {
995 printf("%s: unable to allocate sysctl tree\n", __func__);
996 destroy_dev(softc->dev);
997 free(control_softc, M_DEVBUF);
998 control_softc = NULL;
999 return (ENOMEM);
1000 }
1001
1002 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1003 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
1004 "inquiry_pq_no_lun", CTLFLAG_RW,
1005 &softc->inquiry_pq_no_lun, 0,
1006 "Report no lun possible for invalid LUNs");
1007
1008 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1009 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF);
1010 softc->open_count = 0;
1011
1012 /*
1013 * Default to actually sending a SYNCHRONIZE CACHE command down to
1014 * the drive.
1015 */
1016 softc->flags = CTL_FLAG_REAL_SYNC;
1017
1018 /*
1019 * In Copan's HA scheme, the "master" and "slave" roles are
1020 * figured out through the slot the controller is in. Although it
1021 * is an active/active system, someone has to be in charge.
1022 */
1023#ifdef NEEDTOPORT
1024 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id);
1025#endif
1026
1027 if (sc_id == 0) {
1028 softc->flags |= CTL_FLAG_MASTER_SHELF;
1029 persis_offset = 0;
1030 } else
1031 persis_offset = CTL_MAX_INITIATORS;
1032
1033 /*
1034 * XXX KDM need to figure out where we want to get our target ID
1035 * and WWID. Is it different on each port?
1036 */
1037 softc->target.id = 0;
1038 softc->target.wwid[0] = 0x12345678;
1039 softc->target.wwid[1] = 0x87654321;
1040 STAILQ_INIT(&softc->lun_list);
1041 STAILQ_INIT(&softc->pending_lun_queue);
1042 STAILQ_INIT(&softc->task_queue);
1043 STAILQ_INIT(&softc->incoming_queue);
1044 STAILQ_INIT(&softc->rtr_queue);
1045 STAILQ_INIT(&softc->done_queue);
1046 STAILQ_INIT(&softc->isc_queue);
1047 STAILQ_INIT(&softc->fe_list);
1048 STAILQ_INIT(&softc->be_list);
1049 STAILQ_INIT(&softc->io_pools);
1050
1051 /*
1052 * We don't bother calling these with ctl_lock held here, because,
1053 * in theory, no one else can try to do anything while we're in our
1054 * module init routine.
1055 */
1056 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL,
1057 &internal_pool)!= 0){
1058 printf("ctl: can't allocate %d entry internal pool, "
1059 "exiting\n", CTL_POOL_ENTRIES_INTERNAL);
1060 return (ENOMEM);
1061 }
1062
1063 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY,
1064 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) {
1065 printf("ctl: can't allocate %d entry emergency pool, "
1066 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY);
1067 ctl_pool_free(internal_pool);
1068 return (ENOMEM);
1069 }
1070
1071 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC,
1072 &other_pool) != 0)
1073 {
1074 printf("ctl: can't allocate %d entry other SC pool, "
1075 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1076 ctl_pool_free(internal_pool);
1077 ctl_pool_free(emergency_pool);
1078 return (ENOMEM);
1079 }
1080
1081 softc->internal_pool = internal_pool;
1082 softc->emergency_pool = emergency_pool;
1083 softc->othersc_pool = other_pool;
1084
1085 if (worker_threads > MAXCPU || worker_threads == 0) {
1086 printf("invalid kern.cam.ctl.worker_threads value; "
1087 "setting to 1");
1088 worker_threads = 1;
1089 } else if (worker_threads < 0) {
1090 if (mp_ncpus > 2) {
1091 /*
1092 * Using more than two worker threads actually hurts
1093 * performance due to lock contention.
1094 */
1095 worker_threads = 2;
1096 } else {
1097 worker_threads = 1;
1098 }
1099 }
1100
1101 for (i = 0; i < worker_threads; i++) {
1102 error = kproc_kthread_add(ctl_work_thread, softc,
1103 &softc->work_thread, NULL, 0, 0, "ctl", "work%d", i);
1104 if (error != 0) {
1105 printf("error creating CTL work thread!\n");
1106 ctl_pool_free(internal_pool);
1107 ctl_pool_free(emergency_pool);
1108 ctl_pool_free(other_pool);
1109 return (error);
1110 }
1111 }
1112 if (bootverbose)
1113 printf("ctl: CAM Target Layer loaded\n");
1114
1115 /*
1116 * Initialize the initiator and portname mappings
1117 */
1118 memset(softc->wwpn_iid, 0, sizeof(softc->wwpn_iid));
1119
1120 /*
1121 * Initialize the ioctl front end.
1122 */
1123 fe = &softc->ioctl_info.fe;
1124 sprintf(softc->ioctl_info.port_name, "CTL ioctl");
1125 fe->port_type = CTL_PORT_IOCTL;
1126 fe->num_requested_ctl_io = 100;
1127 fe->port_name = softc->ioctl_info.port_name;
1128 fe->port_online = ctl_ioctl_online;
1129 fe->port_offline = ctl_ioctl_offline;
1130 fe->onoff_arg = &softc->ioctl_info;
1131 fe->targ_enable = ctl_ioctl_targ_enable;
1132 fe->targ_disable = ctl_ioctl_targ_disable;
1133 fe->lun_enable = ctl_ioctl_lun_enable;
1134 fe->lun_disable = ctl_ioctl_lun_disable;
1135 fe->targ_lun_arg = &softc->ioctl_info;
1136 fe->fe_datamove = ctl_ioctl_datamove;
1137 fe->fe_done = ctl_ioctl_done;
1138 fe->max_targets = 15;
1139 fe->max_target_id = 15;
1140
1141 if (ctl_frontend_register(&softc->ioctl_info.fe,
1142 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) {
1143 printf("ctl: ioctl front end registration failed, will "
1144 "continue anyway\n");
1145 }
1146
1147#ifdef CTL_IO_DELAY
1148 if (sizeof(struct callout) > CTL_TIMER_BYTES) {
1149 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n",
1150 sizeof(struct callout), CTL_TIMER_BYTES);
1151 return (EINVAL);
1152 }
1153#endif /* CTL_IO_DELAY */
1154
1155 return (0);
1156}
1157
1158void
1159ctl_shutdown(void)
1160{
1161 struct ctl_softc *softc;
1162 struct ctl_lun *lun, *next_lun;
1163 struct ctl_io_pool *pool;
1164
1165 softc = (struct ctl_softc *)control_softc;
1166
1167 if (ctl_frontend_deregister(&softc->ioctl_info.fe) != 0)
1168 printf("ctl: ioctl front end deregistration failed\n");
1169
1170 mtx_lock(&softc->ctl_lock);
1171
1172 /*
1173 * Free up each LUN.
1174 */
1175 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1176 next_lun = STAILQ_NEXT(lun, links);
1177 ctl_free_lun(lun);
1178 }
1179
1180 mtx_unlock(&softc->ctl_lock);
1181
1182 /*
1183 * This will rip the rug out from under any FETDs or anyone else
1184 * that has a pool allocated. Since we increment our module
1185 * refcount any time someone outside the main CTL module allocates
1186 * a pool, we shouldn't have any problems here. The user won't be
1187 * able to unload the CTL module until client modules have
1188 * successfully unloaded.
1189 */
1190 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL)
1191 ctl_pool_free(pool);
1192
1193#if 0
1194 ctl_shutdown_thread(softc->work_thread);
1195#endif
1196
1197 mtx_destroy(&softc->pool_lock);
1198 mtx_destroy(&softc->ctl_lock);
1199
1200 destroy_dev(softc->dev);
1201
1202 sysctl_ctx_free(&softc->sysctl_ctx);
1203
1204 free(control_softc, M_DEVBUF);
1205 control_softc = NULL;
1206
1207 if (bootverbose)
1208 printf("ctl: CAM Target Layer unloaded\n");
1209}
1210
1211static int
1212ctl_module_event_handler(module_t mod, int what, void *arg)
1213{
1214
1215 switch (what) {
1216 case MOD_LOAD:
1217 return (ctl_init());
1218 case MOD_UNLOAD:
1219 return (EBUSY);
1220 default:
1221 return (EOPNOTSUPP);
1222 }
1223}
1224
1225/*
1226 * XXX KDM should we do some access checks here? Bump a reference count to
1227 * prevent a CTL module from being unloaded while someone has it open?
1228 */
1229static int
1230ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1231{
1232 return (0);
1233}
1234
1235static int
1236ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1237{
1238 return (0);
1239}
1240
1241int
1242ctl_port_enable(ctl_port_type port_type)
1243{
1244 struct ctl_softc *softc;
1245 struct ctl_frontend *fe;
1246
1247 if (ctl_is_single == 0) {
1248 union ctl_ha_msg msg_info;
1249 int isc_retval;
1250
1251#if 0
1252 printf("%s: HA mode, synchronizing frontend enable\n",
1253 __func__);
1254#endif
1255 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE;
1256 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1257 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) {
1258 printf("Sync msg send error retval %d\n", isc_retval);
1259 }
1260 if (!rcv_sync_msg) {
1261 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
1262 sizeof(msg_info), 1);
1263 }
1264#if 0
1265 printf("CTL:Frontend Enable\n");
1266 } else {
1267 printf("%s: single mode, skipping frontend synchronization\n",
1268 __func__);
1269#endif
1270 }
1271
1272 softc = control_softc;
1273
1274 STAILQ_FOREACH(fe, &softc->fe_list, links) {
1275 if (port_type & fe->port_type)
1276 {
1277#if 0
1278 printf("port %d\n", fe->targ_port);
1279#endif
1280 ctl_frontend_online(fe);
1281 }
1282 }
1283
1284 return (0);
1285}
1286
1287int
1288ctl_port_disable(ctl_port_type port_type)
1289{
1290 struct ctl_softc *softc;
1291 struct ctl_frontend *fe;
1292
1293 softc = control_softc;
1294
1295 STAILQ_FOREACH(fe, &softc->fe_list, links) {
1296 if (port_type & fe->port_type)
1297 ctl_frontend_offline(fe);
1298 }
1299
1300 return (0);
1301}
1302
1303/*
1304 * Returns 0 for success, 1 for failure.
1305 * Currently the only failure mode is if there aren't enough entries
1306 * allocated. So, in case of a failure, look at num_entries_dropped,
1307 * reallocate and try again.
1308 */
1309int
1310ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced,
1311 int *num_entries_filled, int *num_entries_dropped,
1312 ctl_port_type port_type, int no_virtual)
1313{
1314 struct ctl_softc *softc;
1315 struct ctl_frontend *fe;
1316 int entries_dropped, entries_filled;
1317 int retval;
1318 int i;
1319
1320 softc = control_softc;
1321
1322 retval = 0;
1323 entries_filled = 0;
1324 entries_dropped = 0;
1325
1326 i = 0;
1327 mtx_lock(&softc->ctl_lock);
1328 STAILQ_FOREACH(fe, &softc->fe_list, links) {
1329 struct ctl_port_entry *entry;
1330
1331 if ((fe->port_type & port_type) == 0)
1332 continue;
1333
1334 if ((no_virtual != 0)
1335 && (fe->virtual_port != 0))
1336 continue;
1337
1338 if (entries_filled >= num_entries_alloced) {
1339 entries_dropped++;
1340 continue;
1341 }
1342 entry = &entries[i];
1343
1344 entry->port_type = fe->port_type;
1345 strlcpy(entry->port_name, fe->port_name,
1346 sizeof(entry->port_name));
1347 entry->physical_port = fe->physical_port;
1348 entry->virtual_port = fe->virtual_port;
1349 entry->wwnn = fe->wwnn;
1350 entry->wwpn = fe->wwpn;
1351
1352 i++;
1353 entries_filled++;
1354 }
1355
1356 mtx_unlock(&softc->ctl_lock);
1357
1358 if (entries_dropped > 0)
1359 retval = 1;
1360
1361 *num_entries_dropped = entries_dropped;
1362 *num_entries_filled = entries_filled;
1363
1364 return (retval);
1365}
1366
1367static void
1368ctl_ioctl_online(void *arg)
1369{
1370 struct ctl_ioctl_info *ioctl_info;
1371
1372 ioctl_info = (struct ctl_ioctl_info *)arg;
1373
1374 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED;
1375}
1376
1377static void
1378ctl_ioctl_offline(void *arg)
1379{
1380 struct ctl_ioctl_info *ioctl_info;
1381
1382 ioctl_info = (struct ctl_ioctl_info *)arg;
1383
1384 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED;
1385}
1386
1387/*
1388 * Remove an initiator by port number and initiator ID.
1389 * Returns 0 for success, 1 for failure.
1390 */
1391int
1392ctl_remove_initiator(int32_t targ_port, uint32_t iid)
1393{
1394 struct ctl_softc *softc;
1395
1396 softc = control_softc;
1397
1398 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1399
1400 if ((targ_port < 0)
1401 || (targ_port > CTL_MAX_PORTS)) {
1402 printf("%s: invalid port number %d\n", __func__, targ_port);
1403 return (1);
1404 }
1405 if (iid > CTL_MAX_INIT_PER_PORT) {
1406 printf("%s: initiator ID %u > maximun %u!\n",
1407 __func__, iid, CTL_MAX_INIT_PER_PORT);
1408 return (1);
1409 }
1410
1411 mtx_lock(&softc->ctl_lock);
1412
1413 softc->wwpn_iid[targ_port][iid].in_use = 0;
1414
1415 mtx_unlock(&softc->ctl_lock);
1416
1417 return (0);
1418}
1419
1420/*
1421 * Add an initiator to the initiator map.
1422 * Returns 0 for success, 1 for failure.
1423 */
1424int
1425ctl_add_initiator(uint64_t wwpn, int32_t targ_port, uint32_t iid)
1426{
1427 struct ctl_softc *softc;
1428 int retval;
1429
1430 softc = control_softc;
1431
1432 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1433
1434 retval = 0;
1435
1436 if ((targ_port < 0)
1437 || (targ_port > CTL_MAX_PORTS)) {
1438 printf("%s: invalid port number %d\n", __func__, targ_port);
1439 return (1);
1440 }
1441 if (iid > CTL_MAX_INIT_PER_PORT) {
1442 printf("%s: WWPN %#jx initiator ID %u > maximun %u!\n",
1443 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1444 return (1);
1445 }
1446
1447 mtx_lock(&softc->ctl_lock);
1448
1449 if (softc->wwpn_iid[targ_port][iid].in_use != 0) {
1450 /*
1451 * We don't treat this as an error.
1452 */
1453 if (softc->wwpn_iid[targ_port][iid].wwpn == wwpn) {
1454 printf("%s: port %d iid %u WWPN %#jx arrived again?\n",
1455 __func__, targ_port, iid, (uintmax_t)wwpn);
1456 goto bailout;
1457 }
1458
1459 /*
1460 * This is an error, but what do we do about it? The
1461 * driver is telling us we have a new WWPN for this
1462 * initiator ID, so we pretty much need to use it.
1463 */
1464 printf("%s: port %d iid %u WWPN %#jx arrived, WWPN %#jx is "
1465 "still at that address\n", __func__, targ_port, iid,
1466 (uintmax_t)wwpn,
1467 (uintmax_t)softc->wwpn_iid[targ_port][iid].wwpn);
1468
1469 /*
1470 * XXX KDM clear have_ca and ua_pending on each LUN for
1471 * this initiator.
1472 */
1473 }
1474 softc->wwpn_iid[targ_port][iid].in_use = 1;
1475 softc->wwpn_iid[targ_port][iid].iid = iid;
1476 softc->wwpn_iid[targ_port][iid].wwpn = wwpn;
1477 softc->wwpn_iid[targ_port][iid].port = targ_port;
1478
1479bailout:
1480
1481 mtx_unlock(&softc->ctl_lock);
1482
1483 return (retval);
1484}
1485
1486/*
1487 * XXX KDM should we pretend to do something in the target/lun
1488 * enable/disable functions?
1489 */
1490static int
1491ctl_ioctl_targ_enable(void *arg, struct ctl_id targ_id)
1492{
1493 return (0);
1494}
1495
1496static int
1497ctl_ioctl_targ_disable(void *arg, struct ctl_id targ_id)
1498{
1499 return (0);
1500}
1501
1502static int
1503ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
1504{
1505 return (0);
1506}
1507
1508static int
1509ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
1510{
1511 return (0);
1512}
1513
1514/*
1515 * Data movement routine for the CTL ioctl frontend port.
1516 */
1517static int
1518ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio)
1519{
1520 struct ctl_sg_entry *ext_sglist, *kern_sglist;
1521 struct ctl_sg_entry ext_entry, kern_entry;
1522 int ext_sglen, ext_sg_entries, kern_sg_entries;
1523 int ext_sg_start, ext_offset;
1524 int len_to_copy, len_copied;
1525 int kern_watermark, ext_watermark;
1526 int ext_sglist_malloced;
1527 int i, j;
1528
1529 ext_sglist_malloced = 0;
1530 ext_sg_start = 0;
1531 ext_offset = 0;
1532
1533 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n"));
1534
1535 /*
1536 * If this flag is set, fake the data transfer.
1537 */
1538 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) {
1539 ctsio->ext_data_filled = ctsio->ext_data_len;
1540 goto bailout;
1541 }
1542
1543 /*
1544 * To simplify things here, if we have a single buffer, stick it in
1545 * a S/G entry and just make it a single entry S/G list.
1546 */
1547 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
1548 int len_seen;
1549
1550 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
1551
1552 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL,
1553 M_WAITOK);
1554 ext_sglist_malloced = 1;
1555 if (copyin(ctsio->ext_data_ptr, ext_sglist,
1556 ext_sglen) != 0) {
1557 ctl_set_internal_failure(ctsio,
1558 /*sks_valid*/ 0,
1559 /*retry_count*/ 0);
1560 goto bailout;
1561 }
1562 ext_sg_entries = ctsio->ext_sg_entries;
1563 len_seen = 0;
1564 for (i = 0; i < ext_sg_entries; i++) {
1565 if ((len_seen + ext_sglist[i].len) >=
1566 ctsio->ext_data_filled) {
1567 ext_sg_start = i;
1568 ext_offset = ctsio->ext_data_filled - len_seen;
1569 break;
1570 }
1571 len_seen += ext_sglist[i].len;
1572 }
1573 } else {
1574 ext_sglist = &ext_entry;
1575 ext_sglist->addr = ctsio->ext_data_ptr;
1576 ext_sglist->len = ctsio->ext_data_len;
1577 ext_sg_entries = 1;
1578 ext_sg_start = 0;
1579 ext_offset = ctsio->ext_data_filled;
1580 }
1581
1582 if (ctsio->kern_sg_entries > 0) {
1583 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
1584 kern_sg_entries = ctsio->kern_sg_entries;
1585 } else {
1586 kern_sglist = &kern_entry;
1587 kern_sglist->addr = ctsio->kern_data_ptr;
1588 kern_sglist->len = ctsio->kern_data_len;
1589 kern_sg_entries = 1;
1590 }
1591
1592
1593 kern_watermark = 0;
1594 ext_watermark = ext_offset;
1595 len_copied = 0;
1596 for (i = ext_sg_start, j = 0;
1597 i < ext_sg_entries && j < kern_sg_entries;) {
1598 uint8_t *ext_ptr, *kern_ptr;
1599
1600 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark,
1601 kern_sglist[j].len - kern_watermark);
1602
1603 ext_ptr = (uint8_t *)ext_sglist[i].addr;
1604 ext_ptr = ext_ptr + ext_watermark;
1605 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
1606 /*
1607 * XXX KDM fix this!
1608 */
1609 panic("need to implement bus address support");
1610#if 0
1611 kern_ptr = bus_to_virt(kern_sglist[j].addr);
1612#endif
1613 } else
1614 kern_ptr = (uint8_t *)kern_sglist[j].addr;
1615 kern_ptr = kern_ptr + kern_watermark;
1616
1617 kern_watermark += len_to_copy;
1618 ext_watermark += len_to_copy;
1619
1620 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
1621 CTL_FLAG_DATA_IN) {
1622 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1623 "bytes to user\n", len_to_copy));
1624 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1625 "to %p\n", kern_ptr, ext_ptr));
1626 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) {
1627 ctl_set_internal_failure(ctsio,
1628 /*sks_valid*/ 0,
1629 /*retry_count*/ 0);
1630 goto bailout;
1631 }
1632 } else {
1633 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1634 "bytes from user\n", len_to_copy));
1635 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1636 "to %p\n", ext_ptr, kern_ptr));
1637 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){
1638 ctl_set_internal_failure(ctsio,
1639 /*sks_valid*/ 0,
1640 /*retry_count*/0);
1641 goto bailout;
1642 }
1643 }
1644
1645 len_copied += len_to_copy;
1646
1647 if (ext_sglist[i].len == ext_watermark) {
1648 i++;
1649 ext_watermark = 0;
1650 }
1651
1652 if (kern_sglist[j].len == kern_watermark) {
1653 j++;
1654 kern_watermark = 0;
1655 }
1656 }
1657
1658 ctsio->ext_data_filled += len_copied;
1659
1660 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, "
1661 "kern_sg_entries: %d\n", ext_sg_entries,
1662 kern_sg_entries));
1663 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, "
1664 "kern_data_len = %d\n", ctsio->ext_data_len,
1665 ctsio->kern_data_len));
1666
1667
1668 /* XXX KDM set residual?? */
1669bailout:
1670
1671 if (ext_sglist_malloced != 0)
1672 free(ext_sglist, M_CTL);
1673
1674 return (CTL_RETVAL_COMPLETE);
1675}
1676
1677/*
1678 * Serialize a command that went down the "wrong" side, and so was sent to
1679 * this controller for execution. The logic is a little different than the
1680 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
1681 * sent back to the other side, but in the success case, we execute the
1682 * command on this side (XFER mode) or tell the other side to execute it
1683 * (SER_ONLY mode).
1684 */
1685static int
1686ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio, int have_lock)
1687{
1688 struct ctl_softc *ctl_softc;
1689 union ctl_ha_msg msg_info;
1690 struct ctl_lun *lun;
1691 int retval = 0;
1692 uint32_t targ_lun;
1693
1694 ctl_softc = control_softc;
1695 if (have_lock == 0)
1696 mtx_lock(&ctl_softc->ctl_lock);
1697
1698 targ_lun = ctsio->io_hdr.nexus.targ_lun;
1699 if (ctsio->io_hdr.nexus.lun_map_fn != NULL)
1700 targ_lun = ctsio->io_hdr.nexus.lun_map_fn(ctsio->io_hdr.nexus.lun_map_arg, targ_lun);
1701 lun = ctl_softc->ctl_luns[targ_lun];
1702 if (lun==NULL)
1703 {
1704 /*
1705 * Why isn't LUN defined? The other side wouldn't
1706 * send a cmd if the LUN is undefined.
1707 */
1708 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__);
1709
1710 /* "Logical unit not supported" */
1711 ctl_set_sense_data(&msg_info.scsi.sense_data,
1712 lun,
1713 /*sense_format*/SSD_TYPE_NONE,
1714 /*current_error*/ 1,
1715 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1716 /*asc*/ 0x25,
1717 /*ascq*/ 0x00,
1718 SSD_ELEM_NONE);
1719
1720 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1721 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1722 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1723 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1724 msg_info.hdr.serializing_sc = NULL;
1725 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1726 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1727 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1728 }
1729 if (have_lock == 0)
1730 mtx_unlock(&ctl_softc->ctl_lock);
1731 return(1);
1732
1733 }
1734
1735 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1736
1737 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
1738 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
1739 ooa_links))) {
1740 case CTL_ACTION_BLOCK:
1741 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
1742 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
1743 blocked_links);
1744 break;
1745 case CTL_ACTION_PASS:
1746 case CTL_ACTION_SKIP:
1747 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
1748 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
1749 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue,
1750 &ctsio->io_hdr, links);
1751 } else {
1752
1753 /* send msg back to other side */
1754 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1755 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
1756 msg_info.hdr.msg_type = CTL_MSG_R2R;
1757#if 0
1758 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc);
1759#endif
1760 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1761 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1762 }
1763 }
1764 break;
1765 case CTL_ACTION_OVERLAP:
1766 /* OVERLAPPED COMMANDS ATTEMPTED */
1767 ctl_set_sense_data(&msg_info.scsi.sense_data,
1768 lun,
1769 /*sense_format*/SSD_TYPE_NONE,
1770 /*current_error*/ 1,
1771 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1772 /*asc*/ 0x4E,
1773 /*ascq*/ 0x00,
1774 SSD_ELEM_NONE);
1775
1776 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1777 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1778 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1779 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1780 msg_info.hdr.serializing_sc = NULL;
1781 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1782#if 0
1783 printf("BAD JUJU:Major Bummer Overlap\n");
1784#endif
1785 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1786 retval = 1;
1787 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1788 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1789 }
1790 break;
1791 case CTL_ACTION_OVERLAP_TAG:
1792 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
1793 ctl_set_sense_data(&msg_info.scsi.sense_data,
1794 lun,
1795 /*sense_format*/SSD_TYPE_NONE,
1796 /*current_error*/ 1,
1797 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1798 /*asc*/ 0x4D,
1799 /*ascq*/ ctsio->tag_num & 0xff,
1800 SSD_ELEM_NONE);
1801
1802 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1803 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1804 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1805 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1806 msg_info.hdr.serializing_sc = NULL;
1807 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1808#if 0
1809 printf("BAD JUJU:Major Bummer Overlap Tag\n");
1810#endif
1811 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1812 retval = 1;
1813 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1814 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1815 }
1816 break;
1817 case CTL_ACTION_ERROR:
1818 default:
1819 /* "Internal target failure" */
1820 ctl_set_sense_data(&msg_info.scsi.sense_data,
1821 lun,
1822 /*sense_format*/SSD_TYPE_NONE,
1823 /*current_error*/ 1,
1824 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
1825 /*asc*/ 0x44,
1826 /*ascq*/ 0x00,
1827 SSD_ELEM_NONE);
1828
1829 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1830 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1831 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1832 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1833 msg_info.hdr.serializing_sc = NULL;
1834 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1835#if 0
1836 printf("BAD JUJU:Major Bummer HW Error\n");
1837#endif
1838 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1839 retval = 1;
1840 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1841 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1842 }
1843 break;
1844 }
1845 if (have_lock == 0)
1846 mtx_unlock(&ctl_softc->ctl_lock);
1847 return (retval);
1848}
1849
1850static int
1851ctl_ioctl_submit_wait(union ctl_io *io)
1852{
1853 struct ctl_fe_ioctl_params params;
1854 ctl_fe_ioctl_state last_state;
1855 int done, retval;
1856
1857 retval = 0;
1858
1859 bzero(&params, sizeof(params));
1860
1861 mtx_init(&params.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF);
1862 cv_init(&params.sem, "ctlioccv");
1863 params.state = CTL_IOCTL_INPROG;
1864 last_state = params.state;
1865
1866 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = &params;
1867
1868 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n"));
1869
1870 /* This shouldn't happen */
1871 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE)
1872 return (retval);
1873
1874 done = 0;
1875
1876 do {
1877 mtx_lock(&params.ioctl_mtx);
1878 /*
1879 * Check the state here, and don't sleep if the state has
1880 * already changed (i.e. wakeup has already occured, but we
1881 * weren't waiting yet).
1882 */
1883 if (params.state == last_state) {
1884 /* XXX KDM cv_wait_sig instead? */
1885 cv_wait(&params.sem, &params.ioctl_mtx);
1886 }
1887 last_state = params.state;
1888
1889 switch (params.state) {
1890 case CTL_IOCTL_INPROG:
1891 /* Why did we wake up? */
1892 /* XXX KDM error here? */
1893 mtx_unlock(&params.ioctl_mtx);
1894 break;
1895 case CTL_IOCTL_DATAMOVE:
1896 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n"));
1897
1898 /*
1899 * change last_state back to INPROG to avoid
1900 * deadlock on subsequent data moves.
1901 */
1902 params.state = last_state = CTL_IOCTL_INPROG;
1903
1904 mtx_unlock(&params.ioctl_mtx);
1905 ctl_ioctl_do_datamove(&io->scsiio);
1906 /*
1907 * Note that in some cases, most notably writes,
1908 * this will queue the I/O and call us back later.
1909 * In other cases, generally reads, this routine
1910 * will immediately call back and wake us up,
1911 * probably using our own context.
1912 */
1913 io->scsiio.be_move_done(io);
1914 break;
1915 case CTL_IOCTL_DONE:
1916 mtx_unlock(&params.ioctl_mtx);
1917 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n"));
1918 done = 1;
1919 break;
1920 default:
1921 mtx_unlock(&params.ioctl_mtx);
1922 /* XXX KDM error here? */
1923 break;
1924 }
1925 } while (done == 0);
1926
1927 mtx_destroy(&params.ioctl_mtx);
1928 cv_destroy(&params.sem);
1929
1930 return (CTL_RETVAL_COMPLETE);
1931}
1932
1933static void
1934ctl_ioctl_datamove(union ctl_io *io)
1935{
1936 struct ctl_fe_ioctl_params *params;
1937
1938 params = (struct ctl_fe_ioctl_params *)
1939 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1940
1941 mtx_lock(&params->ioctl_mtx);
1942 params->state = CTL_IOCTL_DATAMOVE;
1943 cv_broadcast(&params->sem);
1944 mtx_unlock(&params->ioctl_mtx);
1945}
1946
1947static void
1948ctl_ioctl_done(union ctl_io *io)
1949{
1950 struct ctl_fe_ioctl_params *params;
1951
1952 params = (struct ctl_fe_ioctl_params *)
1953 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1954
1955 mtx_lock(&params->ioctl_mtx);
1956 params->state = CTL_IOCTL_DONE;
1957 cv_broadcast(&params->sem);
1958 mtx_unlock(&params->ioctl_mtx);
1959}
1960
1961static void
1962ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask)
1963{
1964 struct ctl_fe_ioctl_startstop_info *sd_info;
1965
1966 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg;
1967
1968 sd_info->hs_info.status = metatask->status;
1969 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns;
1970 sd_info->hs_info.luns_complete =
1971 metatask->taskinfo.startstop.luns_complete;
1972 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed;
1973
1974 cv_broadcast(&sd_info->sem);
1975}
1976
1977static void
1978ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask)
1979{
1980 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info;
1981
1982 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg;
1983
1984 mtx_lock(fe_bbr_info->lock);
1985 fe_bbr_info->bbr_info->status = metatask->status;
1986 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
1987 fe_bbr_info->wakeup_done = 1;
1988 mtx_unlock(fe_bbr_info->lock);
1989
1990 cv_broadcast(&fe_bbr_info->sem);
1991}
1992
1993/*
1994 * Returns 0 for success, errno for failure.
1995 */
1996static int
1997ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
1998 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
1999{
2000 union ctl_io *io;
2001 int retval;
2002
2003 retval = 0;
2004
2005 mtx_assert(&control_softc->ctl_lock, MA_OWNED);
2006
2007 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2008 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2009 ooa_links)) {
2010 struct ctl_ooa_entry *entry;
2011
2012 /*
2013 * If we've got more than we can fit, just count the
2014 * remaining entries.
2015 */
2016 if (*cur_fill_num >= ooa_hdr->alloc_num)
2017 continue;
2018
2019 entry = &kern_entries[*cur_fill_num];
2020
2021 entry->tag_num = io->scsiio.tag_num;
2022 entry->lun_num = lun->lun;
2023#ifdef CTL_TIME_IO
2024 entry->start_bt = io->io_hdr.start_bt;
2025#endif
2026 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2027 entry->cdb_len = io->scsiio.cdb_len;
2028 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2029 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2030
2031 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2032 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2033
2034 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2035 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2036
2037 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2038 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2039
2040 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2041 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2042 }
2043
2044 return (retval);
2045}
2046
2047static void *
2048ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2049 size_t error_str_len)
2050{
2051 void *kptr;
2052
2053 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2054
2055 if (copyin(user_addr, kptr, len) != 0) {
2056 snprintf(error_str, error_str_len, "Error copying %d bytes "
2057 "from user address %p to kernel address %p", len,
2058 user_addr, kptr);
2059 free(kptr, M_CTL);
2060 return (NULL);
2061 }
2062
2063 return (kptr);
2064}
2065
2066static void
2067ctl_free_args(int num_be_args, struct ctl_be_arg *be_args)
2068{
2069 int i;
2070
2071 if (be_args == NULL)
2072 return;
2073
2074 for (i = 0; i < num_be_args; i++) {
2075 free(be_args[i].kname, M_CTL);
2076 free(be_args[i].kvalue, M_CTL);
2077 }
2078
2079 free(be_args, M_CTL);
2080}
2081
2082static struct ctl_be_arg *
2083ctl_copyin_args(int num_be_args, struct ctl_be_arg *be_args,
2084 char *error_str, size_t error_str_len)
2085{
2086 struct ctl_be_arg *args;
2087 int i;
2088
2089 args = ctl_copyin_alloc(be_args, num_be_args * sizeof(*be_args),
2090 error_str, error_str_len);
2091
2092 if (args == NULL)
2093 goto bailout;
2094
2095 for (i = 0; i < num_be_args; i++) {
2096 args[i].kname = NULL;
2097 args[i].kvalue = NULL;
2098 }
2099
2100 for (i = 0; i < num_be_args; i++) {
2101 uint8_t *tmpptr;
2102
2103 args[i].kname = ctl_copyin_alloc(args[i].name,
2104 args[i].namelen, error_str, error_str_len);
2105 if (args[i].kname == NULL)
2106 goto bailout;
2107
2108 if (args[i].kname[args[i].namelen - 1] != '\0') {
2109 snprintf(error_str, error_str_len, "Argument %d "
2110 "name is not NUL-terminated", i);
2111 goto bailout;
2112 }
2113
2114 args[i].kvalue = NULL;
2115
2116 tmpptr = ctl_copyin_alloc(args[i].value,
2117 args[i].vallen, error_str, error_str_len);
2118 if (tmpptr == NULL)
2119 goto bailout;
2120
2121 args[i].kvalue = tmpptr;
2122
2123 if ((args[i].flags & CTL_BEARG_ASCII)
2124 && (tmpptr[args[i].vallen - 1] != '\0')) {
2125 snprintf(error_str, error_str_len, "Argument %d "
2126 "value is not NUL-terminated", i);
2127 goto bailout;
2128 }
2129 }
2130
2131 return (args);
2132bailout:
2133
2134 ctl_free_args(num_be_args, args);
2135
2136 return (NULL);
2137}
2138
2139/*
2140 * Escape characters that are illegal or not recommended in XML.
2141 */
2142int
2143ctl_sbuf_printf_esc(struct sbuf *sb, char *str)
2144{
2145 int retval;
2146
2147 retval = 0;
2148
2149 for (; *str; str++) {
2150 switch (*str) {
2151 case '&':
2152 retval = sbuf_printf(sb, "&amp;");
2153 break;
2154 case '>':
2155 retval = sbuf_printf(sb, "&gt;");
2156 break;
2157 case '<':
2158 retval = sbuf_printf(sb, "&lt;");
2159 break;
2160 default:
2161 retval = sbuf_putc(sb, *str);
2162 break;
2163 }
2164
2165 if (retval != 0)
2166 break;
2167
2168 }
2169
2170 return (retval);
2171}
2172
2173static int
2174ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2175 struct thread *td)
2176{
2177 struct ctl_softc *softc;
2178 int retval;
2179
2180 softc = control_softc;
2181
2182 retval = 0;
2183
2184 switch (cmd) {
2185 case CTL_IO: {
2186 union ctl_io *io;
2187 void *pool_tmp;
2188
2189 /*
2190 * If we haven't been "enabled", don't allow any SCSI I/O
2191 * to this FETD.
2192 */
2193 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) {
2194 retval = -EPERM;
2195 break;
2196 }
2197
2198 io = ctl_alloc_io(softc->ioctl_info.fe.ctl_pool_ref);
2199 if (io == NULL) {
2200 printf("ctl_ioctl: can't allocate ctl_io!\n");
2201 retval = -ENOSPC;
2202 break;
2203 }
2204
2205 /*
2206 * Need to save the pool reference so it doesn't get
2207 * spammed by the user's ctl_io.
2208 */
2209 pool_tmp = io->io_hdr.pool;
2210
2211 memcpy(io, (void *)addr, sizeof(*io));
2212
2213 io->io_hdr.pool = pool_tmp;
2214 /*
2215 * No status yet, so make sure the status is set properly.
2216 */
2217 io->io_hdr.status = CTL_STATUS_NONE;
2218
2219 /*
2220 * The user sets the initiator ID, target and LUN IDs.
2221 */
2222 io->io_hdr.nexus.targ_port = softc->ioctl_info.fe.targ_port;
2223 io->io_hdr.flags |= CTL_FLAG_USER_REQ;
2224 if ((io->io_hdr.io_type == CTL_IO_SCSI)
2225 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED))
2226 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++;
2227
2228 retval = ctl_ioctl_submit_wait(io);
2229
2230 if (retval != 0) {
2231 ctl_free_io(io);
2232 break;
2233 }
2234
2235 memcpy((void *)addr, io, sizeof(*io));
2236
2237 /* return this to our pool */
2238 ctl_free_io(io);
2239
2240 break;
2241 }
2242 case CTL_ENABLE_PORT:
2243 case CTL_DISABLE_PORT:
2244 case CTL_SET_PORT_WWNS: {
2245 struct ctl_frontend *fe;
2246 struct ctl_port_entry *entry;
2247
2248 entry = (struct ctl_port_entry *)addr;
2249
2250 mtx_lock(&softc->ctl_lock);
2251 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2252 int action, done;
2253
2254 action = 0;
2255 done = 0;
2256
2257 if ((entry->port_type == CTL_PORT_NONE)
2258 && (entry->targ_port == fe->targ_port)) {
2259 /*
2260 * If the user only wants to enable or
2261 * disable or set WWNs on a specific port,
2262 * do the operation and we're done.
2263 */
2264 action = 1;
2265 done = 1;
2266 } else if (entry->port_type & fe->port_type) {
2267 /*
2268 * Compare the user's type mask with the
2269 * particular frontend type to see if we
2270 * have a match.
2271 */
2272 action = 1;
2273 done = 0;
2274
2275 /*
2276 * Make sure the user isn't trying to set
2277 * WWNs on multiple ports at the same time.
2278 */
2279 if (cmd == CTL_SET_PORT_WWNS) {
2280 printf("%s: Can't set WWNs on "
2281 "multiple ports\n", __func__);
2282 retval = EINVAL;
2283 break;
2284 }
2285 }
2286 if (action != 0) {
2287 /*
2288 * XXX KDM we have to drop the lock here,
2289 * because the online/offline operations
2290 * can potentially block. We need to
2291 * reference count the frontends so they
2292 * can't go away,
2293 */
2294 mtx_unlock(&softc->ctl_lock);
2295
2296 if (cmd == CTL_ENABLE_PORT) {
2297 struct ctl_lun *lun;
2298
2299 STAILQ_FOREACH(lun, &softc->lun_list,
2300 links) {
2301 fe->lun_enable(fe->targ_lun_arg,
2302 lun->target,
2303 lun->lun);
2304 }
2305
2306 ctl_frontend_online(fe);
2307 } else if (cmd == CTL_DISABLE_PORT) {
2308 struct ctl_lun *lun;
2309
2310 ctl_frontend_offline(fe);
2311
2312 STAILQ_FOREACH(lun, &softc->lun_list,
2313 links) {
2314 fe->lun_disable(
2315 fe->targ_lun_arg,
2316 lun->target,
2317 lun->lun);
2318 }
2319 }
2320
2321 mtx_lock(&softc->ctl_lock);
2322
2323 if (cmd == CTL_SET_PORT_WWNS)
2324 ctl_frontend_set_wwns(fe,
2325 (entry->flags & CTL_PORT_WWNN_VALID) ?
2326 1 : 0, entry->wwnn,
2327 (entry->flags & CTL_PORT_WWPN_VALID) ?
2328 1 : 0, entry->wwpn);
2329 }
2330 if (done != 0)
2331 break;
2332 }
2333 mtx_unlock(&softc->ctl_lock);
2334 break;
2335 }
2336 case CTL_GET_PORT_LIST: {
2337 struct ctl_frontend *fe;
2338 struct ctl_port_list *list;
2339 int i;
2340
2341 list = (struct ctl_port_list *)addr;
2342
2343 if (list->alloc_len != (list->alloc_num *
2344 sizeof(struct ctl_port_entry))) {
2345 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2346 "alloc_num %u * sizeof(struct ctl_port_entry) "
2347 "%zu\n", __func__, list->alloc_len,
2348 list->alloc_num, sizeof(struct ctl_port_entry));
2349 retval = EINVAL;
2350 break;
2351 }
2352 list->fill_len = 0;
2353 list->fill_num = 0;
2354 list->dropped_num = 0;
2355 i = 0;
2356 mtx_lock(&softc->ctl_lock);
2357 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2358 struct ctl_port_entry entry, *list_entry;
2359
2360 if (list->fill_num >= list->alloc_num) {
2361 list->dropped_num++;
2362 continue;
2363 }
2364
2365 entry.port_type = fe->port_type;
2366 strlcpy(entry.port_name, fe->port_name,
2367 sizeof(entry.port_name));
2368 entry.targ_port = fe->targ_port;
2369 entry.physical_port = fe->physical_port;
2370 entry.virtual_port = fe->virtual_port;
2371 entry.wwnn = fe->wwnn;
2372 entry.wwpn = fe->wwpn;
2373 if (fe->status & CTL_PORT_STATUS_ONLINE)
2374 entry.online = 1;
2375 else
2376 entry.online = 0;
2377
2378 list_entry = &list->entries[i];
2379
2380 retval = copyout(&entry, list_entry, sizeof(entry));
2381 if (retval != 0) {
2382 printf("%s: CTL_GET_PORT_LIST: copyout "
2383 "returned %d\n", __func__, retval);
2384 break;
2385 }
2386 i++;
2387 list->fill_num++;
2388 list->fill_len += sizeof(entry);
2389 }
2390 mtx_unlock(&softc->ctl_lock);
2391
2392 /*
2393 * If this is non-zero, we had a copyout fault, so there's
2394 * probably no point in attempting to set the status inside
2395 * the structure.
2396 */
2397 if (retval != 0)
2398 break;
2399
2400 if (list->dropped_num > 0)
2401 list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2402 else
2403 list->status = CTL_PORT_LIST_OK;
2404 break;
2405 }
2406 case CTL_DUMP_OOA: {
2407 struct ctl_lun *lun;
2408 union ctl_io *io;
2409 char printbuf[128];
2410 struct sbuf sb;
2411
2412 mtx_lock(&softc->ctl_lock);
2413 printf("Dumping OOA queues:\n");
2414 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2415 for (io = (union ctl_io *)TAILQ_FIRST(
2416 &lun->ooa_queue); io != NULL;
2417 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2418 ooa_links)) {
2419 sbuf_new(&sb, printbuf, sizeof(printbuf),
2420 SBUF_FIXEDLEN);
2421 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2422 (intmax_t)lun->lun,
2423 io->scsiio.tag_num,
2424 (io->io_hdr.flags &
2425 CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2426 (io->io_hdr.flags &
2427 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2428 (io->io_hdr.flags &
2429 CTL_FLAG_ABORT) ? " ABORT" : "",
2430 (io->io_hdr.flags &
2431 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2432 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2433 sbuf_finish(&sb);
2434 printf("%s\n", sbuf_data(&sb));
2435 }
2436 }
2437 printf("OOA queues dump done\n");
2438 mtx_unlock(&softc->ctl_lock);
2439 break;
2440 }
2441 case CTL_GET_OOA: {
2442 struct ctl_lun *lun;
2443 struct ctl_ooa *ooa_hdr;
2444 struct ctl_ooa_entry *entries;
2445 uint32_t cur_fill_num;
2446
2447 ooa_hdr = (struct ctl_ooa *)addr;
2448
2449 if ((ooa_hdr->alloc_len == 0)
2450 || (ooa_hdr->alloc_num == 0)) {
2451 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2452 "must be non-zero\n", __func__,
2453 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2454 retval = EINVAL;
2455 break;
2456 }
2457
2458 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2459 sizeof(struct ctl_ooa_entry))) {
2460 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2461 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2462 __func__, ooa_hdr->alloc_len,
2463 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2464 retval = EINVAL;
2465 break;
2466 }
2467
2468 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2469 if (entries == NULL) {
2470 printf("%s: could not allocate %d bytes for OOA "
2471 "dump\n", __func__, ooa_hdr->alloc_len);
2472 retval = ENOMEM;
2473 break;
2474 }
2475
2476 mtx_lock(&softc->ctl_lock);
2477 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2478 && ((ooa_hdr->lun_num > CTL_MAX_LUNS)
2479 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2480 mtx_unlock(&softc->ctl_lock);
2481 free(entries, M_CTL);
2482 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2483 __func__, (uintmax_t)ooa_hdr->lun_num);
2484 retval = EINVAL;
2485 break;
2486 }
2487
2488 cur_fill_num = 0;
2489
2490 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2491 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2492 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2493 ooa_hdr, entries);
2494 if (retval != 0)
2495 break;
2496 }
2497 if (retval != 0) {
2498 mtx_unlock(&softc->ctl_lock);
2499 free(entries, M_CTL);
2500 break;
2501 }
2502 } else {
2503 lun = softc->ctl_luns[ooa_hdr->lun_num];
2504
2505 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr,
2506 entries);
2507 }
2508 mtx_unlock(&softc->ctl_lock);
2509
2510 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2511 ooa_hdr->fill_len = ooa_hdr->fill_num *
2512 sizeof(struct ctl_ooa_entry);
2513 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2514 if (retval != 0) {
2515 printf("%s: error copying out %d bytes for OOA dump\n",
2516 __func__, ooa_hdr->fill_len);
2517 }
2518
2519 getbintime(&ooa_hdr->cur_bt);
2520
2521 if (cur_fill_num > ooa_hdr->alloc_num) {
2522 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2523 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2524 } else {
2525 ooa_hdr->dropped_num = 0;
2526 ooa_hdr->status = CTL_OOA_OK;
2527 }
2528
2529 free(entries, M_CTL);
2530 break;
2531 }
2532 case CTL_CHECK_OOA: {
2533 union ctl_io *io;
2534 struct ctl_lun *lun;
2535 struct ctl_ooa_info *ooa_info;
2536
2537
2538 ooa_info = (struct ctl_ooa_info *)addr;
2539
2540 if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2541 ooa_info->status = CTL_OOA_INVALID_LUN;
2542 break;
2543 }
2544 mtx_lock(&softc->ctl_lock);
2545 lun = softc->ctl_luns[ooa_info->lun_id];
2546 if (lun == NULL) {
2547 mtx_unlock(&softc->ctl_lock);
2548 ooa_info->status = CTL_OOA_INVALID_LUN;
2549 break;
2550 }
2551
2552 ooa_info->num_entries = 0;
2553 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2554 io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2555 &io->io_hdr, ooa_links)) {
2556 ooa_info->num_entries++;
2557 }
2558
2559 mtx_unlock(&softc->ctl_lock);
2560 ooa_info->status = CTL_OOA_SUCCESS;
2561
2562 break;
2563 }
2564 case CTL_HARD_START:
2565 case CTL_HARD_STOP: {
2566 struct ctl_fe_ioctl_startstop_info ss_info;
2567 struct cfi_metatask *metatask;
2568 struct mtx hs_mtx;
2569
2570 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF);
2571
2572 cv_init(&ss_info.sem, "hard start/stop cv" );
2573
2574 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2575 if (metatask == NULL) {
2576 retval = ENOMEM;
2577 mtx_destroy(&hs_mtx);
2578 break;
2579 }
2580
2581 if (cmd == CTL_HARD_START)
2582 metatask->tasktype = CFI_TASK_STARTUP;
2583 else
2584 metatask->tasktype = CFI_TASK_SHUTDOWN;
2585
2586 metatask->callback = ctl_ioctl_hard_startstop_callback;
2587 metatask->callback_arg = &ss_info;
2588
2589 cfi_action(metatask);
2590
2591 /* Wait for the callback */
2592 mtx_lock(&hs_mtx);
2593 cv_wait_sig(&ss_info.sem, &hs_mtx);
2594 mtx_unlock(&hs_mtx);
2595
2596 /*
2597 * All information has been copied from the metatask by the
2598 * time cv_broadcast() is called, so we free the metatask here.
2599 */
2600 cfi_free_metatask(metatask);
2601
2602 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info));
2603
2604 mtx_destroy(&hs_mtx);
2605 break;
2606 }
2607 case CTL_BBRREAD: {
2608 struct ctl_bbrread_info *bbr_info;
2609 struct ctl_fe_ioctl_bbrread_info fe_bbr_info;
2610 struct mtx bbr_mtx;
2611 struct cfi_metatask *metatask;
2612
2613 bbr_info = (struct ctl_bbrread_info *)addr;
2614
2615 bzero(&fe_bbr_info, sizeof(fe_bbr_info));
2616
2617 bzero(&bbr_mtx, sizeof(bbr_mtx));
2618 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF);
2619
2620 fe_bbr_info.bbr_info = bbr_info;
2621 fe_bbr_info.lock = &bbr_mtx;
2622
2623 cv_init(&fe_bbr_info.sem, "BBR read cv");
2624 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2625
2626 if (metatask == NULL) {
2627 mtx_destroy(&bbr_mtx);
2628 cv_destroy(&fe_bbr_info.sem);
2629 retval = ENOMEM;
2630 break;
2631 }
2632 metatask->tasktype = CFI_TASK_BBRREAD;
2633 metatask->callback = ctl_ioctl_bbrread_callback;
2634 metatask->callback_arg = &fe_bbr_info;
2635 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num;
2636 metatask->taskinfo.bbrread.lba = bbr_info->lba;
2637 metatask->taskinfo.bbrread.len = bbr_info->len;
2638
2639 cfi_action(metatask);
2640
2641 mtx_lock(&bbr_mtx);
2642 while (fe_bbr_info.wakeup_done == 0)
2643 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx);
2644 mtx_unlock(&bbr_mtx);
2645
2646 bbr_info->status = metatask->status;
2647 bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2648 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status;
2649 memcpy(&bbr_info->sense_data,
2650 &metatask->taskinfo.bbrread.sense_data,
2651 ctl_min(sizeof(bbr_info->sense_data),
2652 sizeof(metatask->taskinfo.bbrread.sense_data)));
2653
2654 cfi_free_metatask(metatask);
2655
2656 mtx_destroy(&bbr_mtx);
2657 cv_destroy(&fe_bbr_info.sem);
2658
2659 break;
2660 }
2661 case CTL_DELAY_IO: {
2662 struct ctl_io_delay_info *delay_info;
2663#ifdef CTL_IO_DELAY
2664 struct ctl_lun *lun;
2665#endif /* CTL_IO_DELAY */
2666
2667 delay_info = (struct ctl_io_delay_info *)addr;
2668
2669#ifdef CTL_IO_DELAY
2670 mtx_lock(&softc->ctl_lock);
2671
2672 if ((delay_info->lun_id > CTL_MAX_LUNS)
2673 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2674 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2675 } else {
2676 lun = softc->ctl_luns[delay_info->lun_id];
2677
2678 delay_info->status = CTL_DELAY_STATUS_OK;
2679
2680 switch (delay_info->delay_type) {
2681 case CTL_DELAY_TYPE_CONT:
2682 break;
2683 case CTL_DELAY_TYPE_ONESHOT:
2684 break;
2685 default:
2686 delay_info->status =
2687 CTL_DELAY_STATUS_INVALID_TYPE;
2688 break;
2689 }
2690
2691 switch (delay_info->delay_loc) {
2692 case CTL_DELAY_LOC_DATAMOVE:
2693 lun->delay_info.datamove_type =
2694 delay_info->delay_type;
2695 lun->delay_info.datamove_delay =
2696 delay_info->delay_secs;
2697 break;
2698 case CTL_DELAY_LOC_DONE:
2699 lun->delay_info.done_type =
2700 delay_info->delay_type;
2701 lun->delay_info.done_delay =
2702 delay_info->delay_secs;
2703 break;
2704 default:
2705 delay_info->status =
2706 CTL_DELAY_STATUS_INVALID_LOC;
2707 break;
2708 }
2709 }
2710
2711 mtx_unlock(&softc->ctl_lock);
2712#else
2713 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2714#endif /* CTL_IO_DELAY */
2715 break;
2716 }
2717 case CTL_REALSYNC_SET: {
2718 int *syncstate;
2719
2720 syncstate = (int *)addr;
2721
2722 mtx_lock(&softc->ctl_lock);
2723 switch (*syncstate) {
2724 case 0:
2725 softc->flags &= ~CTL_FLAG_REAL_SYNC;
2726 break;
2727 case 1:
2728 softc->flags |= CTL_FLAG_REAL_SYNC;
2729 break;
2730 default:
2731 retval = -EINVAL;
2732 break;
2733 }
2734 mtx_unlock(&softc->ctl_lock);
2735 break;
2736 }
2737 case CTL_REALSYNC_GET: {
2738 int *syncstate;
2739
2740 syncstate = (int*)addr;
2741
2742 mtx_lock(&softc->ctl_lock);
2743 if (softc->flags & CTL_FLAG_REAL_SYNC)
2744 *syncstate = 1;
2745 else
2746 *syncstate = 0;
2747 mtx_unlock(&softc->ctl_lock);
2748
2749 break;
2750 }
2751 case CTL_SETSYNC:
2752 case CTL_GETSYNC: {
2753 struct ctl_sync_info *sync_info;
2754 struct ctl_lun *lun;
2755
2756 sync_info = (struct ctl_sync_info *)addr;
2757
2758 mtx_lock(&softc->ctl_lock);
2759 lun = softc->ctl_luns[sync_info->lun_id];
2760 if (lun == NULL) {
2761 mtx_unlock(&softc->ctl_lock);
2762 sync_info->status = CTL_GS_SYNC_NO_LUN;
2763 }
2764 /*
2765 * Get or set the sync interval. We're not bounds checking
2766 * in the set case, hopefully the user won't do something
2767 * silly.
2768 */
2769 if (cmd == CTL_GETSYNC)
2770 sync_info->sync_interval = lun->sync_interval;
2771 else
2772 lun->sync_interval = sync_info->sync_interval;
2773
2774 mtx_unlock(&softc->ctl_lock);
2775
2776 sync_info->status = CTL_GS_SYNC_OK;
2777
2778 break;
2779 }
2780 case CTL_GETSTATS: {
2781 struct ctl_stats *stats;
2782 struct ctl_lun *lun;
2783 int i;
2784
2785 stats = (struct ctl_stats *)addr;
2786
2787 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2788 stats->alloc_len) {
2789 stats->status = CTL_SS_NEED_MORE_SPACE;
2790 stats->num_luns = softc->num_luns;
2791 break;
2792 }
2793 /*
2794 * XXX KDM no locking here. If the LUN list changes,
2795 * things can blow up.
2796 */
2797 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2798 i++, lun = STAILQ_NEXT(lun, links)) {
2799 retval = copyout(&lun->stats, &stats->lun_stats[i],
2800 sizeof(lun->stats));
2801 if (retval != 0)
2802 break;
2803 }
2804 stats->num_luns = softc->num_luns;
2805 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2806 softc->num_luns;
2807 stats->status = CTL_SS_OK;
2808#ifdef CTL_TIME_IO
2809 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2810#else
2811 stats->flags = CTL_STATS_FLAG_NONE;
2812#endif
2813 getnanouptime(&stats->timestamp);
2814 break;
2815 }
2816 case CTL_ERROR_INJECT: {
2817 struct ctl_error_desc *err_desc, *new_err_desc;
2818 struct ctl_lun *lun;
2819
2820 err_desc = (struct ctl_error_desc *)addr;
2821
2822 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2823 M_WAITOK | M_ZERO);
2824 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2825
2826 mtx_lock(&softc->ctl_lock);
2827 lun = softc->ctl_luns[err_desc->lun_id];
2828 if (lun == NULL) {
2829 mtx_unlock(&softc->ctl_lock);
2830 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2831 __func__, (uintmax_t)err_desc->lun_id);
2832 retval = EINVAL;
2833 break;
2834 }
2835
2836 /*
2837 * We could do some checking here to verify the validity
2838 * of the request, but given the complexity of error
2839 * injection requests, the checking logic would be fairly
2840 * complex.
2841 *
2842 * For now, if the request is invalid, it just won't get
2843 * executed and might get deleted.
2844 */
2845 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2846
2847 /*
2848 * XXX KDM check to make sure the serial number is unique,
2849 * in case we somehow manage to wrap. That shouldn't
2850 * happen for a very long time, but it's the right thing to
2851 * do.
2852 */
2853 new_err_desc->serial = lun->error_serial;
2854 err_desc->serial = lun->error_serial;
2855 lun->error_serial++;
2856
2857 mtx_unlock(&softc->ctl_lock);
2858 break;
2859 }
2860 case CTL_ERROR_INJECT_DELETE: {
2861 struct ctl_error_desc *delete_desc, *desc, *desc2;
2862 struct ctl_lun *lun;
2863 int delete_done;
2864
2865 delete_desc = (struct ctl_error_desc *)addr;
2866 delete_done = 0;
2867
2868 mtx_lock(&softc->ctl_lock);
2869 lun = softc->ctl_luns[delete_desc->lun_id];
2870 if (lun == NULL) {
2871 mtx_unlock(&softc->ctl_lock);
2872 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2873 __func__, (uintmax_t)delete_desc->lun_id);
2874 retval = EINVAL;
2875 break;
2876 }
2877 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2878 if (desc->serial != delete_desc->serial)
2879 continue;
2880
2881 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2882 links);
2883 free(desc, M_CTL);
2884 delete_done = 1;
2885 }
2886 mtx_unlock(&softc->ctl_lock);
2887 if (delete_done == 0) {
2888 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2889 "error serial %ju on LUN %u\n", __func__,
2890 delete_desc->serial, delete_desc->lun_id);
2891 retval = EINVAL;
2892 break;
2893 }
2894 break;
2895 }
2896 case CTL_DUMP_STRUCTS: {
2897 int i, j, k;
2898 struct ctl_frontend *fe;
2899
2900 printf("CTL IID to WWPN map start:\n");
2901 for (i = 0; i < CTL_MAX_PORTS; i++) {
2902 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2903 if (softc->wwpn_iid[i][j].in_use == 0)
2904 continue;
2905
2906 printf("port %d iid %u WWPN %#jx\n",
2907 softc->wwpn_iid[i][j].port,
2908 softc->wwpn_iid[i][j].iid,
2909 (uintmax_t)softc->wwpn_iid[i][j].wwpn);
2910 }
2911 }
2912 printf("CTL IID to WWPN map end\n");
2913 printf("CTL Persistent Reservation information start:\n");
2914 for (i = 0; i < CTL_MAX_LUNS; i++) {
2915 struct ctl_lun *lun;
2916
2917 lun = softc->ctl_luns[i];
2918
2919 if ((lun == NULL)
2920 || ((lun->flags & CTL_LUN_DISABLED) != 0))
2921 continue;
2922
2923 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) {
2924 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2925 if (lun->per_res[j+k].registered == 0)
2926 continue;
2927 printf("LUN %d port %d iid %d key "
2928 "%#jx\n", i, j, k,
2929 (uintmax_t)scsi_8btou64(
2930 lun->per_res[j+k].res_key.key));
2931 }
2932 }
2933 }
2934 printf("CTL Persistent Reservation information end\n");
2935 printf("CTL Frontends:\n");
2936 /*
2937 * XXX KDM calling this without a lock. We'd likely want
2938 * to drop the lock before calling the frontend's dump
2939 * routine anyway.
2940 */
2941 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2942 printf("Frontend %s Type %u pport %d vport %d WWNN "
2943 "%#jx WWPN %#jx\n", fe->port_name, fe->port_type,
2944 fe->physical_port, fe->virtual_port,
2945 (uintmax_t)fe->wwnn, (uintmax_t)fe->wwpn);
2946
2947 /*
2948 * Frontends are not required to support the dump
2949 * routine.
2950 */
2951 if (fe->fe_dump == NULL)
2952 continue;
2953
2954 fe->fe_dump();
2955 }
2956 printf("CTL Frontend information end\n");
2957 break;
2958 }
2959 case CTL_LUN_REQ: {
2960 struct ctl_lun_req *lun_req;
2961 struct ctl_backend_driver *backend;
2962
2963 lun_req = (struct ctl_lun_req *)addr;
2964
2965 backend = ctl_backend_find(lun_req->backend);
2966 if (backend == NULL) {
2967 lun_req->status = CTL_LUN_ERROR;
2968 snprintf(lun_req->error_str,
2969 sizeof(lun_req->error_str),
2970 "Backend \"%s\" not found.",
2971 lun_req->backend);
2972 break;
2973 }
2974 if (lun_req->num_be_args > 0) {
2975 lun_req->kern_be_args = ctl_copyin_args(
2976 lun_req->num_be_args,
2977 lun_req->be_args,
2978 lun_req->error_str,
2979 sizeof(lun_req->error_str));
2980 if (lun_req->kern_be_args == NULL) {
2981 lun_req->status = CTL_LUN_ERROR;
2982 break;
2983 }
2984 }
2985
2986 retval = backend->ioctl(dev, cmd, addr, flag, td);
2987
2988 if (lun_req->num_be_args > 0) {
2989 ctl_free_args(lun_req->num_be_args,
2990 lun_req->kern_be_args);
2991 }
2992 break;
2993 }
2994 case CTL_LUN_LIST: {
2995 struct sbuf *sb;
2996 struct ctl_lun *lun;
2997 struct ctl_lun_list *list;
2998 struct ctl_be_lun_option *opt;
2999
3000 list = (struct ctl_lun_list *)addr;
3001
3002 /*
3003 * Allocate a fixed length sbuf here, based on the length
3004 * of the user's buffer. We could allocate an auto-extending
3005 * buffer, and then tell the user how much larger our
3006 * amount of data is than his buffer, but that presents
3007 * some problems:
3008 *
3009 * 1. The sbuf(9) routines use a blocking malloc, and so
3010 * we can't hold a lock while calling them with an
3011 * auto-extending buffer.
3012 *
3013 * 2. There is not currently a LUN reference counting
3014 * mechanism, outside of outstanding transactions on
3015 * the LUN's OOA queue. So a LUN could go away on us
3016 * while we're getting the LUN number, backend-specific
3017 * information, etc. Thus, given the way things
3018 * currently work, we need to hold the CTL lock while
3019 * grabbing LUN information.
3020 *
3021 * So, from the user's standpoint, the best thing to do is
3022 * allocate what he thinks is a reasonable buffer length,
3023 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3024 * double the buffer length and try again. (And repeat
3025 * that until he succeeds.)
3026 */
3027 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3028 if (sb == NULL) {
3029 list->status = CTL_LUN_LIST_ERROR;
3030 snprintf(list->error_str, sizeof(list->error_str),
3031 "Unable to allocate %d bytes for LUN list",
3032 list->alloc_len);
3033 break;
3034 }
3035
3036 sbuf_printf(sb, "<ctllunlist>\n");
3037
3038 mtx_lock(&softc->ctl_lock);
3039
3040 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3041 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3042 (uintmax_t)lun->lun);
3043
3044 /*
3045 * Bail out as soon as we see that we've overfilled
3046 * the buffer.
3047 */
3048 if (retval != 0)
3049 break;
3050
3051 retval = sbuf_printf(sb, "<backend_type>%s"
3052 "</backend_type>\n",
3053 (lun->backend == NULL) ? "none" :
3054 lun->backend->name);
3055
3056 if (retval != 0)
3057 break;
3058
3059 retval = sbuf_printf(sb, "<lun_type>%d</lun_type>\n",
3060 lun->be_lun->lun_type);
3061
3062 if (retval != 0)
3063 break;
3064
3065 if (lun->backend == NULL) {
3066 retval = sbuf_printf(sb, "</lun>\n");
3067 if (retval != 0)
3068 break;
3069 continue;
3070 }
3071
3072 retval = sbuf_printf(sb, "<size>%ju</size>\n",
3073 (lun->be_lun->maxlba > 0) ?
3074 lun->be_lun->maxlba + 1 : 0);
3075
3076 if (retval != 0)
3077 break;
3078
3079 retval = sbuf_printf(sb, "<blocksize>%u</blocksize>\n",
3080 lun->be_lun->blocksize);
3081
3082 if (retval != 0)
3083 break;
3084
3085 retval = sbuf_printf(sb, "<serial_number>");
3086
3087 if (retval != 0)
3088 break;
3089
3090 retval = ctl_sbuf_printf_esc(sb,
3091 lun->be_lun->serial_num);
3092
3093 if (retval != 0)
3094 break;
3095
3096 retval = sbuf_printf(sb, "</serial_number>\n");
3097
3098 if (retval != 0)
3099 break;
3100
3101 retval = sbuf_printf(sb, "<device_id>");
3102
3103 if (retval != 0)
3104 break;
3105
3106 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id);
3107
3108 if (retval != 0)
3109 break;
3110
3111 retval = sbuf_printf(sb, "</device_id>\n");
3112
3113 if (retval != 0)
3114 break;
3115
3116 if (lun->backend->lun_info != NULL) {
3117 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3118 if (retval != 0)
3119 break;
3120 }
3121 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3122 retval = sbuf_printf(sb, "<%s>%s</%s>", opt->name, opt->value, opt->name);
3123 if (retval != 0)
3124 break;
3125 }
3126
3127 retval = sbuf_printf(sb, "</lun>\n");
3128
3129 if (retval != 0)
3130 break;
3131 }
3132 mtx_unlock(&softc->ctl_lock);
3133
3134 if ((retval != 0)
3135 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3136 retval = 0;
3137 sbuf_delete(sb);
3138 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3139 snprintf(list->error_str, sizeof(list->error_str),
3140 "Out of space, %d bytes is too small",
3141 list->alloc_len);
3142 break;
3143 }
3144
3145 sbuf_finish(sb);
3146
3147 retval = copyout(sbuf_data(sb), list->lun_xml,
3148 sbuf_len(sb) + 1);
3149
3150 list->fill_len = sbuf_len(sb) + 1;
3151 list->status = CTL_LUN_LIST_OK;
3152 sbuf_delete(sb);
3153 break;
3154 }
3155 case CTL_ISCSI: {
3156 struct ctl_iscsi *ci;
3157 struct ctl_frontend *fe;
3158
3159 ci = (struct ctl_iscsi *)addr;
3160
3161 mtx_lock(&softc->ctl_lock);
3162 STAILQ_FOREACH(fe, &softc->fe_list, links) {
3163 if (strcmp(fe->port_name, "iscsi") == 0)
3164 break;
3165 }
3166 mtx_unlock(&softc->ctl_lock);
3167
3168 if (fe == NULL) {
3169 ci->status = CTL_ISCSI_ERROR;
3170 snprintf(ci->error_str, sizeof(ci->error_str), "Backend \"iscsi\" not found.");
3171 break;
3172 }
3173
3174 retval = fe->ioctl(dev, cmd, addr, flag, td);
3175 break;
3176 }
3177 default: {
3178 /* XXX KDM should we fix this? */
3179#if 0
3180 struct ctl_backend_driver *backend;
3181 unsigned int type;
3182 int found;
3183
3184 found = 0;
3185
3186 /*
3187 * We encode the backend type as the ioctl type for backend
3188 * ioctls. So parse it out here, and then search for a
3189 * backend of this type.
3190 */
3191 type = _IOC_TYPE(cmd);
3192
3193 STAILQ_FOREACH(backend, &softc->be_list, links) {
3194 if (backend->type == type) {
3195 found = 1;
3196 break;
3197 }
3198 }
3199 if (found == 0) {
3200 printf("ctl: unknown ioctl command %#lx or backend "
3201 "%d\n", cmd, type);
3202 retval = -EINVAL;
3203 break;
3204 }
3205 retval = backend->ioctl(dev, cmd, addr, flag, td);
3206#endif
3207 retval = ENOTTY;
3208 break;
3209 }
3210 }
3211 return (retval);
3212}
3213
3214uint32_t
3215ctl_get_initindex(struct ctl_nexus *nexus)
3216{
3217 if (nexus->targ_port < CTL_MAX_PORTS)
3218 return (nexus->initid.id +
3219 (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3220 else
3221 return (nexus->initid.id +
3222 ((nexus->targ_port - CTL_MAX_PORTS) *
3223 CTL_MAX_INIT_PER_PORT));
3224}
3225
3226uint32_t
3227ctl_get_resindex(struct ctl_nexus *nexus)
3228{
3229 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3230}
3231
3232uint32_t
3233ctl_port_idx(int port_num)
3234{
3235 if (port_num < CTL_MAX_PORTS)
3236 return(port_num);
3237 else
3238 return(port_num - CTL_MAX_PORTS);
3239}
3240
3241/*
3242 * Note: This only works for bitmask sizes that are at least 32 bits, and
3243 * that are a power of 2.
3244 */
3245int
3246ctl_ffz(uint32_t *mask, uint32_t size)
3247{
3248 uint32_t num_chunks, num_pieces;
3249 int i, j;
3250
3251 num_chunks = (size >> 5);
3252 if (num_chunks == 0)
3253 num_chunks++;
3254 num_pieces = ctl_min((sizeof(uint32_t) * 8), size);
3255
3256 for (i = 0; i < num_chunks; i++) {
3257 for (j = 0; j < num_pieces; j++) {
3258 if ((mask[i] & (1 << j)) == 0)
3259 return ((i << 5) + j);
3260 }
3261 }
3262
3263 return (-1);
3264}
3265
3266int
3267ctl_set_mask(uint32_t *mask, uint32_t bit)
3268{
3269 uint32_t chunk, piece;
3270
3271 chunk = bit >> 5;
3272 piece = bit % (sizeof(uint32_t) * 8);
3273
3274 if ((mask[chunk] & (1 << piece)) != 0)
3275 return (-1);
3276 else
3277 mask[chunk] |= (1 << piece);
3278
3279 return (0);
3280}
3281
3282int
3283ctl_clear_mask(uint32_t *mask, uint32_t bit)
3284{
3285 uint32_t chunk, piece;
3286
3287 chunk = bit >> 5;
3288 piece = bit % (sizeof(uint32_t) * 8);
3289
3290 if ((mask[chunk] & (1 << piece)) == 0)
3291 return (-1);
3292 else
3293 mask[chunk] &= ~(1 << piece);
3294
3295 return (0);
3296}
3297
3298int
3299ctl_is_set(uint32_t *mask, uint32_t bit)
3300{
3301 uint32_t chunk, piece;
3302
3303 chunk = bit >> 5;
3304 piece = bit % (sizeof(uint32_t) * 8);
3305
3306 if ((mask[chunk] & (1 << piece)) == 0)
3307 return (0);
3308 else
3309 return (1);
3310}
3311
3312#ifdef unused
3313/*
3314 * The bus, target and lun are optional, they can be filled in later.
3315 * can_wait is used to determine whether we can wait on the malloc or not.
3316 */
3317union ctl_io*
3318ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target,
3319 uint32_t targ_lun, int can_wait)
3320{
3321 union ctl_io *io;
3322
3323 if (can_wait)
3324 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK);
3325 else
3326 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3327
3328 if (io != NULL) {
3329 io->io_hdr.io_type = io_type;
3330 io->io_hdr.targ_port = targ_port;
3331 /*
3332 * XXX KDM this needs to change/go away. We need to move
3333 * to a preallocated pool of ctl_scsiio structures.
3334 */
3335 io->io_hdr.nexus.targ_target.id = targ_target;
3336 io->io_hdr.nexus.targ_lun = targ_lun;
3337 }
3338
3339 return (io);
3340}
3341
3342void
3343ctl_kfree_io(union ctl_io *io)
3344{
3345 free(io, M_CTL);
3346}
3347#endif /* unused */
3348
3349/*
3350 * ctl_softc, pool_type, total_ctl_io are passed in.
3351 * npool is passed out.
3352 */
3353int
3354ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type,
3355 uint32_t total_ctl_io, struct ctl_io_pool **npool)
3356{
3357 uint32_t i;
3358 union ctl_io *cur_io, *next_io;
3359 struct ctl_io_pool *pool;
3360 int retval;
3361
3362 retval = 0;
3363
3364 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3365 M_NOWAIT | M_ZERO);
3366 if (pool == NULL) {
3367 retval = -ENOMEM;
3368 goto bailout;
3369 }
3370
3371 pool->type = pool_type;
3372 pool->ctl_softc = ctl_softc;
3373
3374 mtx_lock(&ctl_softc->pool_lock);
3375 pool->id = ctl_softc->cur_pool_id++;
3376 mtx_unlock(&ctl_softc->pool_lock);
3377
3378 pool->flags = CTL_POOL_FLAG_NONE;
3379 pool->refcount = 1; /* Reference for validity. */
3380 STAILQ_INIT(&pool->free_queue);
3381
3382 /*
3383 * XXX KDM other options here:
3384 * - allocate a page at a time
3385 * - allocate one big chunk of memory.
3386 * Page allocation might work well, but would take a little more
3387 * tracking.
3388 */
3389 for (i = 0; i < total_ctl_io; i++) {
3390 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTL,
3391 M_NOWAIT);
3392 if (cur_io == NULL) {
3393 retval = ENOMEM;
3394 break;
3395 }
3396 cur_io->io_hdr.pool = pool;
3397 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links);
3398 pool->total_ctl_io++;
3399 pool->free_ctl_io++;
3400 }
3401
3402 if (retval != 0) {
3403 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3404 cur_io != NULL; cur_io = next_io) {
3405 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr,
3406 links);
3407 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr,
3408 ctl_io_hdr, links);
3409 free(cur_io, M_CTL);
3410 }
3411
3412 free(pool, M_CTL);
3413 goto bailout;
3414 }
3415 mtx_lock(&ctl_softc->pool_lock);
3416 ctl_softc->num_pools++;
3417 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links);
3418 /*
3419 * Increment our usage count if this is an external consumer, so we
3420 * can't get unloaded until the external consumer (most likely a
3421 * FETD) unloads and frees his pool.
3422 *
3423 * XXX KDM will this increment the caller's module use count, or
3424 * mine?
3425 */
3426#if 0
3427 if ((pool_type != CTL_POOL_EMERGENCY)
3428 && (pool_type != CTL_POOL_INTERNAL)
3429 && (pool_type != CTL_POOL_IOCTL)
3430 && (pool_type != CTL_POOL_4OTHERSC))
3431 MOD_INC_USE_COUNT;
3432#endif
3433
3434 mtx_unlock(&ctl_softc->pool_lock);
3435
3436 *npool = pool;
3437
3438bailout:
3439
3440 return (retval);
3441}
3442
3443static int
3444ctl_pool_acquire(struct ctl_io_pool *pool)
3445{
3446
3447 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED);
3448
3449 if (pool->flags & CTL_POOL_FLAG_INVALID)
3450 return (-EINVAL);
3451
3452 pool->refcount++;
3453
3454 return (0);
3455}
3456
3457static void
3458ctl_pool_release(struct ctl_io_pool *pool)
3459{
3460 struct ctl_softc *ctl_softc = pool->ctl_softc;
3461 union ctl_io *io;
3462
3463 mtx_assert(&ctl_softc->pool_lock, MA_OWNED);
3464
3465 if (--pool->refcount != 0)
3466 return;
3467
3468 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) {
3469 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr,
3470 links);
3471 free(io, M_CTL);
3472 }
3473
3474 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links);
3475 ctl_softc->num_pools--;
3476
3477 /*
3478 * XXX KDM will this decrement the caller's usage count or mine?
3479 */
3480#if 0
3481 if ((pool->type != CTL_POOL_EMERGENCY)
3482 && (pool->type != CTL_POOL_INTERNAL)
3483 && (pool->type != CTL_POOL_IOCTL))
3484 MOD_DEC_USE_COUNT;
3485#endif
3486
3487 free(pool, M_CTL);
3488}
3489
3490void
3491ctl_pool_free(struct ctl_io_pool *pool)
3492{
3493 struct ctl_softc *ctl_softc;
3494
3495 if (pool == NULL)
3496 return;
3497
3498 ctl_softc = pool->ctl_softc;
3499 mtx_lock(&ctl_softc->pool_lock);
3500 pool->flags |= CTL_POOL_FLAG_INVALID;
3501 ctl_pool_release(pool);
3502 mtx_unlock(&ctl_softc->pool_lock);
3503}
3504
3505/*
3506 * This routine does not block (except for spinlocks of course).
3507 * It tries to allocate a ctl_io union from the caller's pool as quickly as
3508 * possible.
3509 */
3510union ctl_io *
3511ctl_alloc_io(void *pool_ref)
3512{
3513 union ctl_io *io;
3514 struct ctl_softc *ctl_softc;
3515 struct ctl_io_pool *pool, *npool;
3516 struct ctl_io_pool *emergency_pool;
3517
3518 pool = (struct ctl_io_pool *)pool_ref;
3519
3520 if (pool == NULL) {
3521 printf("%s: pool is NULL\n", __func__);
3522 return (NULL);
3523 }
3524
3525 emergency_pool = NULL;
3526
3527 ctl_softc = pool->ctl_softc;
3528
3529 mtx_lock(&ctl_softc->pool_lock);
3530 /*
3531 * First, try to get the io structure from the user's pool.
3532 */
3533 if (ctl_pool_acquire(pool) == 0) {
3534 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3535 if (io != NULL) {
3536 STAILQ_REMOVE_HEAD(&pool->free_queue, links);
3537 pool->total_allocated++;
3538 pool->free_ctl_io--;
3539 mtx_unlock(&ctl_softc->pool_lock);
3540 return (io);
3541 } else
3542 ctl_pool_release(pool);
3543 }
3544 /*
3545 * If he doesn't have any io structures left, search for an
3546 * emergency pool and grab one from there.
3547 */
3548 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) {
3549 if (npool->type != CTL_POOL_EMERGENCY)
3550 continue;
3551
3552 if (ctl_pool_acquire(npool) != 0)
3553 continue;
3554
3555 emergency_pool = npool;
3556
3557 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue);
3558 if (io != NULL) {
3559 STAILQ_REMOVE_HEAD(&npool->free_queue, links);
3560 npool->total_allocated++;
3561 npool->free_ctl_io--;
3562 mtx_unlock(&ctl_softc->pool_lock);
3563 return (io);
3564 } else
3565 ctl_pool_release(npool);
3566 }
3567
3568 /* Drop the spinlock before we malloc */
3569 mtx_unlock(&ctl_softc->pool_lock);
3570
3571 /*
3572 * The emergency pool (if it exists) didn't have one, so try an
3573 * atomic (i.e. nonblocking) malloc and see if we get lucky.
3574 */
3575 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3576 if (io != NULL) {
3577 /*
3578 * If the emergency pool exists but is empty, add this
3579 * ctl_io to its list when it gets freed.
3580 */
3581 if (emergency_pool != NULL) {
3582 mtx_lock(&ctl_softc->pool_lock);
3583 if (ctl_pool_acquire(emergency_pool) == 0) {
3584 io->io_hdr.pool = emergency_pool;
3585 emergency_pool->total_ctl_io++;
3586 /*
3587 * Need to bump this, otherwise
3588 * total_allocated and total_freed won't
3589 * match when we no longer have anything
3590 * outstanding.
3591 */
3592 emergency_pool->total_allocated++;
3593 }
3594 mtx_unlock(&ctl_softc->pool_lock);
3595 } else
3596 io->io_hdr.pool = NULL;
3597 }
3598
3599 return (io);
3600}
3601
3602void
3603ctl_free_io(union ctl_io *io)
3604{
3605 if (io == NULL)
3606 return;
3607
3608 /*
3609 * If this ctl_io has a pool, return it to that pool.
3610 */
3611 if (io->io_hdr.pool != NULL) {
3612 struct ctl_io_pool *pool;
3613#if 0
3614 struct ctl_softc *ctl_softc;
3615 union ctl_io *tmp_io;
3616 unsigned long xflags;
3617 int i;
3618
3619 ctl_softc = control_softc;
3620#endif
3621
3622 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3623
3624 mtx_lock(&pool->ctl_softc->pool_lock);
3625#if 0
3626 save_flags(xflags);
3627
3628 for (i = 0, tmp_io = (union ctl_io *)STAILQ_FIRST(
3629 &ctl_softc->task_queue); tmp_io != NULL; i++,
3630 tmp_io = (union ctl_io *)STAILQ_NEXT(&tmp_io->io_hdr,
3631 links)) {
3632 if (tmp_io == io) {
3633 printf("%s: %p is still on the task queue!\n",
3634 __func__, tmp_io);
3635 printf("%s: (%d): type %d "
3636 "msg %d cdb %x iptl: "
3637 "%d:%d:%d:%d tag 0x%04x "
3638 "flg %#lx\n",
3639 __func__, i,
3640 tmp_io->io_hdr.io_type,
3641 tmp_io->io_hdr.msg_type,
3642 tmp_io->scsiio.cdb[0],
3643 tmp_io->io_hdr.nexus.initid.id,
3644 tmp_io->io_hdr.nexus.targ_port,
3645 tmp_io->io_hdr.nexus.targ_target.id,
3646 tmp_io->io_hdr.nexus.targ_lun,
3647 (tmp_io->io_hdr.io_type ==
3648 CTL_IO_TASK) ?
3649 tmp_io->taskio.tag_num :
3650 tmp_io->scsiio.tag_num,
3651 xflags);
3652 panic("I/O still on the task queue!");
3653 }
3654 }
3655#endif
3656 io->io_hdr.io_type = 0xff;
3657 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links);
3658 pool->total_freed++;
3659 pool->free_ctl_io++;
3660 ctl_pool_release(pool);
3661 mtx_unlock(&pool->ctl_softc->pool_lock);
3662 } else {
3663 /*
3664 * Otherwise, just free it. We probably malloced it and
3665 * the emergency pool wasn't available.
3666 */
3667 free(io, M_CTL);
3668 }
3669
3670}
3671
3672void
3673ctl_zero_io(union ctl_io *io)
3674{
3675 void *pool_ref;
3676
3677 if (io == NULL)
3678 return;
3679
3680 /*
3681 * May need to preserve linked list pointers at some point too.
3682 */
3683 pool_ref = io->io_hdr.pool;
3684
3685 memset(io, 0, sizeof(*io));
3686
3687 io->io_hdr.pool = pool_ref;
3688}
3689
3690/*
3691 * This routine is currently used for internal copies of ctl_ios that need
3692 * to persist for some reason after we've already returned status to the
3693 * FETD. (Thus the flag set.)
3694 *
3695 * XXX XXX
3696 * Note that this makes a blind copy of all fields in the ctl_io, except
3697 * for the pool reference. This includes any memory that has been
3698 * allocated! That memory will no longer be valid after done has been
3699 * called, so this would be VERY DANGEROUS for command that actually does
3700 * any reads or writes. Right now (11/7/2005), this is only used for immediate
3701 * start and stop commands, which don't transfer any data, so this is not a
3702 * problem. If it is used for anything else, the caller would also need to
3703 * allocate data buffer space and this routine would need to be modified to
3704 * copy the data buffer(s) as well.
3705 */
3706void
3707ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3708{
3709 void *pool_ref;
3710
3711 if ((src == NULL)
3712 || (dest == NULL))
3713 return;
3714
3715 /*
3716 * May need to preserve linked list pointers at some point too.
3717 */
3718 pool_ref = dest->io_hdr.pool;
3719
3720 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest)));
3721
3722 dest->io_hdr.pool = pool_ref;
3723 /*
3724 * We need to know that this is an internal copy, and doesn't need
3725 * to get passed back to the FETD that allocated it.
3726 */
3727 dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3728}
3729
3730#ifdef NEEDTOPORT
3731static void
3732ctl_update_power_subpage(struct copan_power_subpage *page)
3733{
3734 int num_luns, num_partitions, config_type;
3735 struct ctl_softc *softc;
3736 cs_BOOL_t aor_present, shelf_50pct_power;
3737 cs_raidset_personality_t rs_type;
3738 int max_active_luns;
3739
3740 softc = control_softc;
3741
3742 /* subtract out the processor LUN */
3743 num_luns = softc->num_luns - 1;
3744 /*
3745 * Default to 7 LUNs active, which was the only number we allowed
3746 * in the past.
3747 */
3748 max_active_luns = 7;
3749
3750 num_partitions = config_GetRsPartitionInfo();
3751 config_type = config_GetConfigType();
3752 shelf_50pct_power = config_GetShelfPowerMode();
3753 aor_present = config_IsAorRsPresent();
3754
3755 rs_type = ddb_GetRsRaidType(1);
3756 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5)
3757 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) {
3758 EPRINT(0, "Unsupported RS type %d!", rs_type);
3759 }
3760
3761
3762 page->total_luns = num_luns;
3763
3764 switch (config_type) {
3765 case 40:
3766 /*
3767 * In a 40 drive configuration, it doesn't matter what DC
3768 * cards we have, whether we have AOR enabled or not,
3769 * partitioning or not, or what type of RAIDset we have.
3770 * In that scenario, we can power up every LUN we present
3771 * to the user.
3772 */
3773 max_active_luns = num_luns;
3774
3775 break;
3776 case 64:
3777 if (shelf_50pct_power == CS_FALSE) {
3778 /* 25% power */
3779 if (aor_present == CS_TRUE) {
3780 if (rs_type ==
3781 CS_RAIDSET_PERSONALITY_RAID5) {
3782 max_active_luns = 7;
3783 } else if (rs_type ==
3784 CS_RAIDSET_PERSONALITY_RAID1){
3785 max_active_luns = 14;
3786 } else {
3787 /* XXX KDM now what?? */
3788 }
3789 } else {
3790 if (rs_type ==
3791 CS_RAIDSET_PERSONALITY_RAID5) {
3792 max_active_luns = 8;
3793 } else if (rs_type ==
3794 CS_RAIDSET_PERSONALITY_RAID1){
3795 max_active_luns = 16;
3796 } else {
3797 /* XXX KDM now what?? */
3798 }
3799 }
3800 } else {
3801 /* 50% power */
3802 /*
3803 * With 50% power in a 64 drive configuration, we
3804 * can power all LUNs we present.
3805 */
3806 max_active_luns = num_luns;
3807 }
3808 break;
3809 case 112:
3810 if (shelf_50pct_power == CS_FALSE) {
3811 /* 25% power */
3812 if (aor_present == CS_TRUE) {
3813 if (rs_type ==
3814 CS_RAIDSET_PERSONALITY_RAID5) {
3815 max_active_luns = 7;
3816 } else if (rs_type ==
3817 CS_RAIDSET_PERSONALITY_RAID1){
3818 max_active_luns = 14;
3819 } else {
3820 /* XXX KDM now what?? */
3821 }
3822 } else {
3823 if (rs_type ==
3824 CS_RAIDSET_PERSONALITY_RAID5) {
3825 max_active_luns = 8;
3826 } else if (rs_type ==
3827 CS_RAIDSET_PERSONALITY_RAID1){
3828 max_active_luns = 16;
3829 } else {
3830 /* XXX KDM now what?? */
3831 }
3832 }
3833 } else {
3834 /* 50% power */
3835 if (aor_present == CS_TRUE) {
3836 if (rs_type ==
3837 CS_RAIDSET_PERSONALITY_RAID5) {
3838 max_active_luns = 14;
3839 } else if (rs_type ==
3840 CS_RAIDSET_PERSONALITY_RAID1){
3841 /*
3842 * We're assuming here that disk
3843 * caching is enabled, and so we're
3844 * able to power up half of each
3845 * LUN, and cache all writes.
3846 */
3847 max_active_luns = num_luns;
3848 } else {
3849 /* XXX KDM now what?? */
3850 }
3851 } else {
3852 if (rs_type ==
3853 CS_RAIDSET_PERSONALITY_RAID5) {
3854 max_active_luns = 15;
3855 } else if (rs_type ==
3856 CS_RAIDSET_PERSONALITY_RAID1){
3857 max_active_luns = 30;
3858 } else {
3859 /* XXX KDM now what?? */
3860 }
3861 }
3862 }
3863 break;
3864 default:
3865 /*
3866 * In this case, we have an unknown configuration, so we
3867 * just use the default from above.
3868 */
3869 break;
3870 }
3871
3872 page->max_active_luns = max_active_luns;
3873#if 0
3874 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__,
3875 page->total_luns, page->max_active_luns);
3876#endif
3877}
3878#endif /* NEEDTOPORT */
3879
3880/*
3881 * This routine could be used in the future to load default and/or saved
3882 * mode page parameters for a particuar lun.
3883 */
3884static int
3885ctl_init_page_index(struct ctl_lun *lun)
3886{
3887 int i;
3888 struct ctl_page_index *page_index;
3889 struct ctl_softc *softc;
3890
3891 memcpy(&lun->mode_pages.index, page_index_template,
3892 sizeof(page_index_template));
3893
3894 softc = lun->ctl_softc;
3895
3896 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3897
3898 page_index = &lun->mode_pages.index[i];
3899 /*
3900 * If this is a disk-only mode page, there's no point in
3901 * setting it up. For some pages, we have to have some
3902 * basic information about the disk in order to calculate the
3903 * mode page data.
3904 */
3905 if ((lun->be_lun->lun_type != T_DIRECT)
3906 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
3907 continue;
3908
3909 switch (page_index->page_code & SMPH_PC_MASK) {
3910 case SMS_FORMAT_DEVICE_PAGE: {
3911 struct scsi_format_page *format_page;
3912
3913 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3914 panic("subpage is incorrect!");
3915
3916 /*
3917 * Sectors per track are set above. Bytes per
3918 * sector need to be set here on a per-LUN basis.
3919 */
3920 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3921 &format_page_default,
3922 sizeof(format_page_default));
3923 memcpy(&lun->mode_pages.format_page[
3924 CTL_PAGE_CHANGEABLE], &format_page_changeable,
3925 sizeof(format_page_changeable));
3926 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3927 &format_page_default,
3928 sizeof(format_page_default));
3929 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3930 &format_page_default,
3931 sizeof(format_page_default));
3932
3933 format_page = &lun->mode_pages.format_page[
3934 CTL_PAGE_CURRENT];
3935 scsi_ulto2b(lun->be_lun->blocksize,
3936 format_page->bytes_per_sector);
3937
3938 format_page = &lun->mode_pages.format_page[
3939 CTL_PAGE_DEFAULT];
3940 scsi_ulto2b(lun->be_lun->blocksize,
3941 format_page->bytes_per_sector);
3942
3943 format_page = &lun->mode_pages.format_page[
3944 CTL_PAGE_SAVED];
3945 scsi_ulto2b(lun->be_lun->blocksize,
3946 format_page->bytes_per_sector);
3947
3948 page_index->page_data =
3949 (uint8_t *)lun->mode_pages.format_page;
3950 break;
3951 }
3952 case SMS_RIGID_DISK_PAGE: {
3953 struct scsi_rigid_disk_page *rigid_disk_page;
3954 uint32_t sectors_per_cylinder;
3955 uint64_t cylinders;
3956#ifndef __XSCALE__
3957 int shift;
3958#endif /* !__XSCALE__ */
3959
3960 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3961 panic("invalid subpage value %d",
3962 page_index->subpage);
3963
3964 /*
3965 * Rotation rate and sectors per track are set
3966 * above. We calculate the cylinders here based on
3967 * capacity. Due to the number of heads and
3968 * sectors per track we're using, smaller arrays
3969 * may turn out to have 0 cylinders. Linux and
3970 * FreeBSD don't pay attention to these mode pages
3971 * to figure out capacity, but Solaris does. It
3972 * seems to deal with 0 cylinders just fine, and
3973 * works out a fake geometry based on the capacity.
3974 */
3975 memcpy(&lun->mode_pages.rigid_disk_page[
3976 CTL_PAGE_CURRENT], &rigid_disk_page_default,
3977 sizeof(rigid_disk_page_default));
3978 memcpy(&lun->mode_pages.rigid_disk_page[
3979 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
3980 sizeof(rigid_disk_page_changeable));
3981 memcpy(&lun->mode_pages.rigid_disk_page[
3982 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
3983 sizeof(rigid_disk_page_default));
3984 memcpy(&lun->mode_pages.rigid_disk_page[
3985 CTL_PAGE_SAVED], &rigid_disk_page_default,
3986 sizeof(rigid_disk_page_default));
3987
3988 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
3989 CTL_DEFAULT_HEADS;
3990
3991 /*
3992 * The divide method here will be more accurate,
3993 * probably, but results in floating point being
3994 * used in the kernel on i386 (__udivdi3()). On the
3995 * XScale, though, __udivdi3() is implemented in
3996 * software.
3997 *
3998 * The shift method for cylinder calculation is
3999 * accurate if sectors_per_cylinder is a power of
4000 * 2. Otherwise it might be slightly off -- you
4001 * might have a bit of a truncation problem.
4002 */
4003#ifdef __XSCALE__
4004 cylinders = (lun->be_lun->maxlba + 1) /
4005 sectors_per_cylinder;
4006#else
4007 for (shift = 31; shift > 0; shift--) {
4008 if (sectors_per_cylinder & (1 << shift))
4009 break;
4010 }
4011 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4012#endif
4013
4014 /*
4015 * We've basically got 3 bytes, or 24 bits for the
4016 * cylinder size in the mode page. If we're over,
4017 * just round down to 2^24.
4018 */
4019 if (cylinders > 0xffffff)
4020 cylinders = 0xffffff;
4021
4022 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4023 CTL_PAGE_CURRENT];
4024 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4025
4026 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4027 CTL_PAGE_DEFAULT];
4028 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4029
4030 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4031 CTL_PAGE_SAVED];
4032 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4033
4034 page_index->page_data =
4035 (uint8_t *)lun->mode_pages.rigid_disk_page;
4036 break;
4037 }
4038 case SMS_CACHING_PAGE: {
4039
4040 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4041 panic("invalid subpage value %d",
4042 page_index->subpage);
4043 /*
4044 * Defaults should be okay here, no calculations
4045 * needed.
4046 */
4047 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4048 &caching_page_default,
4049 sizeof(caching_page_default));
4050 memcpy(&lun->mode_pages.caching_page[
4051 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4052 sizeof(caching_page_changeable));
4053 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4054 &caching_page_default,
4055 sizeof(caching_page_default));
4056 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4057 &caching_page_default,
4058 sizeof(caching_page_default));
4059 page_index->page_data =
4060 (uint8_t *)lun->mode_pages.caching_page;
4061 break;
4062 }
4063 case SMS_CONTROL_MODE_PAGE: {
4064
4065 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4066 panic("invalid subpage value %d",
4067 page_index->subpage);
4068
4069 /*
4070 * Defaults should be okay here, no calculations
4071 * needed.
4072 */
4073 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
4074 &control_page_default,
4075 sizeof(control_page_default));
4076 memcpy(&lun->mode_pages.control_page[
4077 CTL_PAGE_CHANGEABLE], &control_page_changeable,
4078 sizeof(control_page_changeable));
4079 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
4080 &control_page_default,
4081 sizeof(control_page_default));
4082 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
4083 &control_page_default,
4084 sizeof(control_page_default));
4085 page_index->page_data =
4086 (uint8_t *)lun->mode_pages.control_page;
4087 break;
4088
4089 }
4090 case SMS_VENDOR_SPECIFIC_PAGE:{
4091 switch (page_index->subpage) {
4092 case PWR_SUBPAGE_CODE: {
4093 struct copan_power_subpage *current_page,
4094 *saved_page;
4095
4096 memcpy(&lun->mode_pages.power_subpage[
4097 CTL_PAGE_CURRENT],
4098 &power_page_default,
4099 sizeof(power_page_default));
4100 memcpy(&lun->mode_pages.power_subpage[
4101 CTL_PAGE_CHANGEABLE],
4102 &power_page_changeable,
4103 sizeof(power_page_changeable));
4104 memcpy(&lun->mode_pages.power_subpage[
4105 CTL_PAGE_DEFAULT],
4106 &power_page_default,
4107 sizeof(power_page_default));
4108 memcpy(&lun->mode_pages.power_subpage[
4109 CTL_PAGE_SAVED],
4110 &power_page_default,
4111 sizeof(power_page_default));
4112 page_index->page_data =
4113 (uint8_t *)lun->mode_pages.power_subpage;
4114
4115 current_page = (struct copan_power_subpage *)
4116 (page_index->page_data +
4117 (page_index->page_len *
4118 CTL_PAGE_CURRENT));
4119 saved_page = (struct copan_power_subpage *)
4120 (page_index->page_data +
4121 (page_index->page_len *
4122 CTL_PAGE_SAVED));
4123 break;
4124 }
4125 case APS_SUBPAGE_CODE: {
4126 struct copan_aps_subpage *current_page,
4127 *saved_page;
4128
4129 // This gets set multiple times but
4130 // it should always be the same. It's
4131 // only done during init so who cares.
4132 index_to_aps_page = i;
4133
4134 memcpy(&lun->mode_pages.aps_subpage[
4135 CTL_PAGE_CURRENT],
4136 &aps_page_default,
4137 sizeof(aps_page_default));
4138 memcpy(&lun->mode_pages.aps_subpage[
4139 CTL_PAGE_CHANGEABLE],
4140 &aps_page_changeable,
4141 sizeof(aps_page_changeable));
4142 memcpy(&lun->mode_pages.aps_subpage[
4143 CTL_PAGE_DEFAULT],
4144 &aps_page_default,
4145 sizeof(aps_page_default));
4146 memcpy(&lun->mode_pages.aps_subpage[
4147 CTL_PAGE_SAVED],
4148 &aps_page_default,
4149 sizeof(aps_page_default));
4150 page_index->page_data =
4151 (uint8_t *)lun->mode_pages.aps_subpage;
4152
4153 current_page = (struct copan_aps_subpage *)
4154 (page_index->page_data +
4155 (page_index->page_len *
4156 CTL_PAGE_CURRENT));
4157 saved_page = (struct copan_aps_subpage *)
4158 (page_index->page_data +
4159 (page_index->page_len *
4160 CTL_PAGE_SAVED));
4161 break;
4162 }
4163 case DBGCNF_SUBPAGE_CODE: {
4164 struct copan_debugconf_subpage *current_page,
4165 *saved_page;
4166
4167 memcpy(&lun->mode_pages.debugconf_subpage[
4168 CTL_PAGE_CURRENT],
4169 &debugconf_page_default,
4170 sizeof(debugconf_page_default));
4171 memcpy(&lun->mode_pages.debugconf_subpage[
4172 CTL_PAGE_CHANGEABLE],
4173 &debugconf_page_changeable,
4174 sizeof(debugconf_page_changeable));
4175 memcpy(&lun->mode_pages.debugconf_subpage[
4176 CTL_PAGE_DEFAULT],
4177 &debugconf_page_default,
4178 sizeof(debugconf_page_default));
4179 memcpy(&lun->mode_pages.debugconf_subpage[
4180 CTL_PAGE_SAVED],
4181 &debugconf_page_default,
4182 sizeof(debugconf_page_default));
4183 page_index->page_data =
4184 (uint8_t *)lun->mode_pages.debugconf_subpage;
4185
4186 current_page = (struct copan_debugconf_subpage *)
4187 (page_index->page_data +
4188 (page_index->page_len *
4189 CTL_PAGE_CURRENT));
4190 saved_page = (struct copan_debugconf_subpage *)
4191 (page_index->page_data +
4192 (page_index->page_len *
4193 CTL_PAGE_SAVED));
4194 break;
4195 }
4196 default:
4197 panic("invalid subpage value %d",
4198 page_index->subpage);
4199 break;
4200 }
4201 break;
4202 }
4203 default:
4204 panic("invalid page value %d",
4205 page_index->page_code & SMPH_PC_MASK);
4206 break;
4207 }
4208 }
4209
4210 return (CTL_RETVAL_COMPLETE);
4211}
4212
4213/*
4214 * LUN allocation.
4215 *
4216 * Requirements:
4217 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4218 * wants us to allocate the LUN and he can block.
4219 * - ctl_softc is always set
4220 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4221 *
4222 * Returns 0 for success, non-zero (errno) for failure.
4223 */
4224static int
4225ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4226 struct ctl_be_lun *const be_lun, struct ctl_id target_id)
4227{
4228 struct ctl_lun *nlun, *lun;
4229 struct ctl_frontend *fe;
4230 int lun_number, i, lun_malloced;
4231
4232 if (be_lun == NULL)
4233 return (EINVAL);
4234
4235 /*
4236 * We currently only support Direct Access or Processor LUN types.
4237 */
4238 switch (be_lun->lun_type) {
4239 case T_DIRECT:
4240 break;
4241 case T_PROCESSOR:
4242 break;
4243 case T_SEQUENTIAL:
4244 case T_CHANGER:
4245 default:
4246 be_lun->lun_config_status(be_lun->be_lun,
4247 CTL_LUN_CONFIG_FAILURE);
4248 break;
4249 }
4250 if (ctl_lun == NULL) {
4251 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4252 lun_malloced = 1;
4253 } else {
4254 lun_malloced = 0;
4255 lun = ctl_lun;
4256 }
4257
4258 memset(lun, 0, sizeof(*lun));
4259 if (lun_malloced)
4260 lun->flags = CTL_LUN_MALLOCED;
4261
4262 mtx_lock(&ctl_softc->ctl_lock);
4263 /*
4264 * See if the caller requested a particular LUN number. If so, see
4265 * if it is available. Otherwise, allocate the first available LUN.
4266 */
4267 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4268 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4269 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4270 mtx_unlock(&ctl_softc->ctl_lock);
4271 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4272 printf("ctl: requested LUN ID %d is higher "
4273 "than CTL_MAX_LUNS - 1 (%d)\n",
4274 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4275 } else {
4276 /*
4277 * XXX KDM return an error, or just assign
4278 * another LUN ID in this case??
4279 */
4280 printf("ctl: requested LUN ID %d is already "
4281 "in use\n", be_lun->req_lun_id);
4282 }
4283 if (lun->flags & CTL_LUN_MALLOCED)
4284 free(lun, M_CTL);
4285 be_lun->lun_config_status(be_lun->be_lun,
4286 CTL_LUN_CONFIG_FAILURE);
4287 return (ENOSPC);
4288 }
4289 lun_number = be_lun->req_lun_id;
4290 } else {
4291 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS);
4292 if (lun_number == -1) {
4293 mtx_unlock(&ctl_softc->ctl_lock);
4294 printf("ctl: can't allocate LUN on target %ju, out of "
4295 "LUNs\n", (uintmax_t)target_id.id);
4296 if (lun->flags & CTL_LUN_MALLOCED)
4297 free(lun, M_CTL);
4298 be_lun->lun_config_status(be_lun->be_lun,
4299 CTL_LUN_CONFIG_FAILURE);
4300 return (ENOSPC);
4301 }
4302 }
4303 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4304
4305 lun->target = target_id;
4306 lun->lun = lun_number;
4307 lun->be_lun = be_lun;
4308 /*
4309 * The processor LUN is always enabled. Disk LUNs come on line
4310 * disabled, and must be enabled by the backend.
4311 */
4312 lun->flags |= CTL_LUN_DISABLED;
4313 lun->backend = be_lun->be;
4314 be_lun->ctl_lun = lun;
4315 be_lun->lun_id = lun_number;
4316 atomic_add_int(&be_lun->be->num_luns, 1);
4317 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4318 lun->flags |= CTL_LUN_STOPPED;
4319
4320 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4321 lun->flags |= CTL_LUN_INOPERABLE;
4322
4323 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4324 lun->flags |= CTL_LUN_PRIMARY_SC;
4325
4326 lun->ctl_softc = ctl_softc;
4327 TAILQ_INIT(&lun->ooa_queue);
4328 TAILQ_INIT(&lun->blocked_queue);
4329 STAILQ_INIT(&lun->error_list);
4330
4331 /*
4332 * Initialize the mode page index.
4333 */
4334 ctl_init_page_index(lun);
4335
4336 /*
4337 * Set the poweron UA for all initiators on this LUN only.
4338 */
4339 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4340 lun->pending_sense[i].ua_pending = CTL_UA_POWERON;
4341
4342 /*
4343 * Now, before we insert this lun on the lun list, set the lun
4344 * inventory changed UA for all other luns.
4345 */
4346 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4347 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4348 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE;
4349 }
4350 }
4351
4352 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4353
4354 ctl_softc->ctl_luns[lun_number] = lun;
4355
4356 ctl_softc->num_luns++;
4357
4358 /* Setup statistics gathering */
4359 lun->stats.device_type = be_lun->lun_type;
4360 lun->stats.lun_number = lun_number;
4361 if (lun->stats.device_type == T_DIRECT)
4362 lun->stats.blocksize = be_lun->blocksize;
4363 else
4364 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4365 for (i = 0;i < CTL_MAX_PORTS;i++)
4366 lun->stats.ports[i].targ_port = i;
4367
4368 mtx_unlock(&ctl_softc->ctl_lock);
4369
4370 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4371
4372 /*
4373 * Run through each registered FETD and bring it online if it isn't
4374 * already. Enable the target ID if it hasn't been enabled, and
4375 * enable this particular LUN.
4376 */
4377 STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) {
4378 int retval;
4379
4380 /*
4381 * XXX KDM this only works for ONE TARGET ID. We'll need
4382 * to do things differently if we go to a multiple target
4383 * ID scheme.
4384 */
4385 if ((fe->status & CTL_PORT_STATUS_TARG_ONLINE) == 0) {
4386
4387 retval = fe->targ_enable(fe->targ_lun_arg, target_id);
4388 if (retval != 0) {
4389 printf("ctl_alloc_lun: FETD %s port %d "
4390 "returned error %d for targ_enable on "
4391 "target %ju\n", fe->port_name,
4392 fe->targ_port, retval,
4393 (uintmax_t)target_id.id);
4394 } else
4395 fe->status |= CTL_PORT_STATUS_TARG_ONLINE;
4396 }
4397
4398 retval = fe->lun_enable(fe->targ_lun_arg, target_id,lun_number);
4399 if (retval != 0) {
4400 printf("ctl_alloc_lun: FETD %s port %d returned error "
4401 "%d for lun_enable on target %ju lun %d\n",
4402 fe->port_name, fe->targ_port, retval,
4403 (uintmax_t)target_id.id, lun_number);
4404 } else
4405 fe->status |= CTL_PORT_STATUS_LUN_ONLINE;
4406 }
4407 return (0);
4408}
4409
4410/*
4411 * Delete a LUN.
4412 * Assumptions:
4413 * - LUN has already been marked invalid and any pending I/O has been taken
4414 * care of.
4415 */
4416static int
4417ctl_free_lun(struct ctl_lun *lun)
4418{
4419 struct ctl_softc *softc;
4420#if 0
4421 struct ctl_frontend *fe;
4422#endif
4423 struct ctl_lun *nlun;
4424 union ctl_io *io, *next_io;
4425 int i;
4426
4427 softc = lun->ctl_softc;
4428
4429 mtx_assert(&softc->ctl_lock, MA_OWNED);
4430
4431 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4432
4433 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4434
4435 softc->ctl_luns[lun->lun] = NULL;
4436
4437 if (TAILQ_FIRST(&lun->ooa_queue) != NULL) {
4438 printf("ctl_free_lun: aieee!! freeing a LUN with "
4439 "outstanding I/O!!\n");
4440 }
4441
4442 /*
4443 * If we have anything pending on the RtR queue, remove it.
4444 */
4445 for (io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue); io != NULL;
4446 io = next_io) {
4447 uint32_t targ_lun;
4448
4449 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
4450 targ_lun = io->io_hdr.nexus.targ_lun;
4451 if (io->io_hdr.nexus.lun_map_fn != NULL)
4452 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
4453 if ((io->io_hdr.nexus.targ_target.id == lun->target.id)
4454 && (targ_lun == lun->lun))
4455 STAILQ_REMOVE(&softc->rtr_queue, &io->io_hdr,
4456 ctl_io_hdr, links);
4457 }
4458
4459 /*
4460 * Then remove everything from the blocked queue.
4461 */
4462 for (io = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); io != NULL;
4463 io = next_io) {
4464 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,blocked_links);
4465 TAILQ_REMOVE(&lun->blocked_queue, &io->io_hdr, blocked_links);
4466 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
4467 }
4468
4469 /*
4470 * Now clear out the OOA queue, and free all the I/O.
4471 * XXX KDM should we notify the FETD here? We probably need to
4472 * quiesce the LUN before deleting it.
4473 */
4474 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); io != NULL;
4475 io = next_io) {
4476 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, ooa_links);
4477 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
4478 ctl_free_io(io);
4479 }
4480
4481 softc->num_luns--;
4482
4483 /*
4484 * XXX KDM this scheme only works for a single target/multiple LUN
4485 * setup. It needs to be revamped for a multiple target scheme.
4486 *
4487 * XXX KDM this results in fe->lun_disable() getting called twice,
4488 * once when ctl_disable_lun() is called, and a second time here.
4489 * We really need to re-think the LUN disable semantics. There
4490 * should probably be several steps/levels to LUN removal:
4491 * - disable
4492 * - invalidate
4493 * - free
4494 *
4495 * Right now we only have a disable method when communicating to
4496 * the front end ports, at least for individual LUNs.
4497 */
4498#if 0
4499 STAILQ_FOREACH(fe, &softc->fe_list, links) {
4500 int retval;
4501
4502 retval = fe->lun_disable(fe->targ_lun_arg, lun->target,
4503 lun->lun);
4504 if (retval != 0) {
4505 printf("ctl_free_lun: FETD %s port %d returned error "
4506 "%d for lun_disable on target %ju lun %jd\n",
4507 fe->port_name, fe->targ_port, retval,
4508 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4509 }
4510
4511 if (STAILQ_FIRST(&softc->lun_list) == NULL) {
4512 fe->status &= ~CTL_PORT_STATUS_LUN_ONLINE;
4513
4514 retval = fe->targ_disable(fe->targ_lun_arg,lun->target);
4515 if (retval != 0) {
4516 printf("ctl_free_lun: FETD %s port %d "
4517 "returned error %d for targ_disable on "
4518 "target %ju\n", fe->port_name,
4519 fe->targ_port, retval,
4520 (uintmax_t)lun->target.id);
4521 } else
4522 fe->status &= ~CTL_PORT_STATUS_TARG_ONLINE;
4523
4524 if ((fe->status & CTL_PORT_STATUS_TARG_ONLINE) != 0)
4525 continue;
4526
4527#if 0
4528 fe->port_offline(fe->onoff_arg);
4529 fe->status &= ~CTL_PORT_STATUS_ONLINE;
4530#endif
4531 }
4532 }
4533#endif
4534
4535 /*
4536 * Tell the backend to free resources, if this LUN has a backend.
4537 */
4538 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4539 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4540
4541 if (lun->flags & CTL_LUN_MALLOCED)
4542 free(lun, M_CTL);
4543
4544 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4545 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4546 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE;
4547 }
4548 }
4549
4550 return (0);
4551}
4552
4553static void
4554ctl_create_lun(struct ctl_be_lun *be_lun)
4555{
4556 struct ctl_softc *ctl_softc;
4557
4558 ctl_softc = control_softc;
4559
4560 /*
4561 * ctl_alloc_lun() should handle all potential failure cases.
4562 */
4563 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target);
4564}
4565
4566int
4567ctl_add_lun(struct ctl_be_lun *be_lun)
4568{
4569 struct ctl_softc *ctl_softc;
4570
4571 ctl_softc = control_softc;
4572
4573 mtx_lock(&ctl_softc->ctl_lock);
4574 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links);
4575 mtx_unlock(&ctl_softc->ctl_lock);
4576
4577 ctl_wakeup_thread();
4578
4579 return (0);
4580}
4581
4582int
4583ctl_enable_lun(struct ctl_be_lun *be_lun)
4584{
4585 struct ctl_softc *ctl_softc;
4586 struct ctl_frontend *fe, *nfe;
4587 struct ctl_lun *lun;
4588 int retval;
4589
4590 ctl_softc = control_softc;
4591
4592 lun = (struct ctl_lun *)be_lun->ctl_lun;
4593
4594 mtx_lock(&ctl_softc->ctl_lock);
4595 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4596 /*
4597 * eh? Why did we get called if the LUN is already
4598 * enabled?
4599 */
4600 mtx_unlock(&ctl_softc->ctl_lock);
4601 return (0);
4602 }
4603 lun->flags &= ~CTL_LUN_DISABLED;
4604
4605 for (fe = STAILQ_FIRST(&ctl_softc->fe_list); fe != NULL; fe = nfe) {
4606 nfe = STAILQ_NEXT(fe, links);
4607
4608 /*
4609 * Drop the lock while we call the FETD's enable routine.
4610 * This can lead to a callback into CTL (at least in the
4611 * case of the internal initiator frontend.
4612 */
4613 mtx_unlock(&ctl_softc->ctl_lock);
4614 retval = fe->lun_enable(fe->targ_lun_arg, lun->target,lun->lun);
4615 mtx_lock(&ctl_softc->ctl_lock);
4616 if (retval != 0) {
4617 printf("%s: FETD %s port %d returned error "
4618 "%d for lun_enable on target %ju lun %jd\n",
4619 __func__, fe->port_name, fe->targ_port, retval,
4620 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4621 }
4622#if 0
4623 else {
4624 /* NOTE: TODO: why does lun enable affect port status? */
4625 fe->status |= CTL_PORT_STATUS_LUN_ONLINE;
4626 }
4627#endif
4628 }
4629
4630 mtx_unlock(&ctl_softc->ctl_lock);
4631
4632 return (0);
4633}
4634
4635int
4636ctl_disable_lun(struct ctl_be_lun *be_lun)
4637{
4638 struct ctl_softc *ctl_softc;
4639 struct ctl_frontend *fe;
4640 struct ctl_lun *lun;
4641 int retval;
4642
4643 ctl_softc = control_softc;
4644
4645 lun = (struct ctl_lun *)be_lun->ctl_lun;
4646
4647 mtx_lock(&ctl_softc->ctl_lock);
4648
4649 if (lun->flags & CTL_LUN_DISABLED) {
4650 mtx_unlock(&ctl_softc->ctl_lock);
4651 return (0);
4652 }
4653 lun->flags |= CTL_LUN_DISABLED;
4654
4655 STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) {
4656 mtx_unlock(&ctl_softc->ctl_lock);
4657 /*
4658 * Drop the lock before we call the frontend's disable
4659 * routine, to avoid lock order reversals.
4660 *
4661 * XXX KDM what happens if the frontend list changes while
4662 * we're traversing it? It's unlikely, but should be handled.
4663 */
4664 retval = fe->lun_disable(fe->targ_lun_arg, lun->target,
4665 lun->lun);
4666 mtx_lock(&ctl_softc->ctl_lock);
4667 if (retval != 0) {
4668 printf("ctl_alloc_lun: FETD %s port %d returned error "
4669 "%d for lun_disable on target %ju lun %jd\n",
4670 fe->port_name, fe->targ_port, retval,
4671 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4672 }
4673 }
4674
4675 mtx_unlock(&ctl_softc->ctl_lock);
4676
4677 return (0);
4678}
4679
4680int
4681ctl_start_lun(struct ctl_be_lun *be_lun)
4682{
4683 struct ctl_softc *ctl_softc;
4684 struct ctl_lun *lun;
4685
4686 ctl_softc = control_softc;
4687
4688 lun = (struct ctl_lun *)be_lun->ctl_lun;
4689
4690 mtx_lock(&ctl_softc->ctl_lock);
4691 lun->flags &= ~CTL_LUN_STOPPED;
4692 mtx_unlock(&ctl_softc->ctl_lock);
4693
4694 return (0);
4695}
4696
4697int
4698ctl_stop_lun(struct ctl_be_lun *be_lun)
4699{
4700 struct ctl_softc *ctl_softc;
4701 struct ctl_lun *lun;
4702
4703 ctl_softc = control_softc;
4704
4705 lun = (struct ctl_lun *)be_lun->ctl_lun;
4706
4707 mtx_lock(&ctl_softc->ctl_lock);
4708 lun->flags |= CTL_LUN_STOPPED;
4709 mtx_unlock(&ctl_softc->ctl_lock);
4710
4711 return (0);
4712}
4713
4714int
4715ctl_lun_offline(struct ctl_be_lun *be_lun)
4716{
4717 struct ctl_softc *ctl_softc;
4718 struct ctl_lun *lun;
4719
4720 ctl_softc = control_softc;
4721
4722 lun = (struct ctl_lun *)be_lun->ctl_lun;
4723
4724 mtx_lock(&ctl_softc->ctl_lock);
4725 lun->flags |= CTL_LUN_OFFLINE;
4726 mtx_unlock(&ctl_softc->ctl_lock);
4727
4728 return (0);
4729}
4730
4731int
4732ctl_lun_online(struct ctl_be_lun *be_lun)
4733{
4734 struct ctl_softc *ctl_softc;
4735 struct ctl_lun *lun;
4736
4737 ctl_softc = control_softc;
4738
4739 lun = (struct ctl_lun *)be_lun->ctl_lun;
4740
4741 mtx_lock(&ctl_softc->ctl_lock);
4742 lun->flags &= ~CTL_LUN_OFFLINE;
4743 mtx_unlock(&ctl_softc->ctl_lock);
4744
4745 return (0);
4746}
4747
4748int
4749ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4750{
4751 struct ctl_softc *ctl_softc;
4752 struct ctl_lun *lun;
4753
4754 ctl_softc = control_softc;
4755
4756 lun = (struct ctl_lun *)be_lun->ctl_lun;
4757
4758 mtx_lock(&ctl_softc->ctl_lock);
4759
4760 /*
4761 * The LUN needs to be disabled before it can be marked invalid.
4762 */
4763 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4764 mtx_unlock(&ctl_softc->ctl_lock);
4765 return (-1);
4766 }
4767 /*
4768 * Mark the LUN invalid.
4769 */
4770 lun->flags |= CTL_LUN_INVALID;
4771
4772 /*
4773 * If there is nothing in the OOA queue, go ahead and free the LUN.
4774 * If we have something in the OOA queue, we'll free it when the
4775 * last I/O completes.
4776 */
4777 if (TAILQ_FIRST(&lun->ooa_queue) == NULL)
4778 ctl_free_lun(lun);
4779 mtx_unlock(&ctl_softc->ctl_lock);
4780
4781 return (0);
4782}
4783
4784int
4785ctl_lun_inoperable(struct ctl_be_lun *be_lun)
4786{
4787 struct ctl_softc *ctl_softc;
4788 struct ctl_lun *lun;
4789
4790 ctl_softc = control_softc;
4791 lun = (struct ctl_lun *)be_lun->ctl_lun;
4792
4793 mtx_lock(&ctl_softc->ctl_lock);
4794 lun->flags |= CTL_LUN_INOPERABLE;
4795 mtx_unlock(&ctl_softc->ctl_lock);
4796
4797 return (0);
4798}
4799
4800int
4801ctl_lun_operable(struct ctl_be_lun *be_lun)
4802{
4803 struct ctl_softc *ctl_softc;
4804 struct ctl_lun *lun;
4805
4806 ctl_softc = control_softc;
4807 lun = (struct ctl_lun *)be_lun->ctl_lun;
4808
4809 mtx_lock(&ctl_softc->ctl_lock);
4810 lun->flags &= ~CTL_LUN_INOPERABLE;
4811 mtx_unlock(&ctl_softc->ctl_lock);
4812
4813 return (0);
4814}
4815
4816int
4817ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus,
4818 int lock)
4819{
4820 struct ctl_softc *softc;
4821 struct ctl_lun *lun;
4822 struct copan_aps_subpage *current_sp;
4823 struct ctl_page_index *page_index;
4824 int i;
4825
4826 softc = control_softc;
4827
4828 mtx_lock(&softc->ctl_lock);
4829
4830 lun = (struct ctl_lun *)be_lun->ctl_lun;
4831
4832 page_index = NULL;
4833 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4834 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
4835 APS_PAGE_CODE)
4836 continue;
4837
4838 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE)
4839 continue;
4840 page_index = &lun->mode_pages.index[i];
4841 }
4842
4843 if (page_index == NULL) {
4844 mtx_unlock(&softc->ctl_lock);
4845 printf("%s: APS subpage not found for lun %ju!\n", __func__,
4846 (uintmax_t)lun->lun);
4847 return (1);
4848 }
4849#if 0
4850 if ((softc->aps_locked_lun != 0)
4851 && (softc->aps_locked_lun != lun->lun)) {
4852 printf("%s: attempt to lock LUN %llu when %llu is already "
4853 "locked\n");
4854 mtx_unlock(&softc->ctl_lock);
4855 return (1);
4856 }
4857#endif
4858
4859 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
4860 (page_index->page_len * CTL_PAGE_CURRENT));
4861
4862 if (lock != 0) {
4863 current_sp->lock_active = APS_LOCK_ACTIVE;
4864 softc->aps_locked_lun = lun->lun;
4865 } else {
4866 current_sp->lock_active = 0;
4867 softc->aps_locked_lun = 0;
4868 }
4869
4870
4871 /*
4872 * If we're in HA mode, try to send the lock message to the other
4873 * side.
4874 */
4875 if (ctl_is_single == 0) {
4876 int isc_retval;
4877 union ctl_ha_msg lock_msg;
4878
4879 lock_msg.hdr.nexus = *nexus;
4880 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK;
4881 if (lock != 0)
4882 lock_msg.aps.lock_flag = 1;
4883 else
4884 lock_msg.aps.lock_flag = 0;
4885 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg,
4886 sizeof(lock_msg), 0);
4887 if (isc_retval > CTL_HA_STATUS_SUCCESS) {
4888 printf("%s: APS (lock=%d) error returned from "
4889 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval);
4890 mtx_unlock(&softc->ctl_lock);
4891 return (1);
4892 }
4893 }
4894
4895 mtx_unlock(&softc->ctl_lock);
4896
4897 return (0);
4898}
4899
4900void
4901ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4902{
4903 struct ctl_lun *lun;
4904 struct ctl_softc *softc;
4905 int i;
4906
4907 softc = control_softc;
4908
4909 mtx_lock(&softc->ctl_lock);
4910
4911 lun = (struct ctl_lun *)be_lun->ctl_lun;
4912
4913 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4914 lun->pending_sense[i].ua_pending |= CTL_UA_CAPACITY_CHANGED;
4915
4916 mtx_unlock(&softc->ctl_lock);
4917}
4918
4919/*
4920 * Backend "memory move is complete" callback for requests that never
4921 * make it down to say RAIDCore's configuration code.
4922 */
4923int
4924ctl_config_move_done(union ctl_io *io)
4925{
4926 int retval;
4927
4928 retval = CTL_RETVAL_COMPLETE;
4929
4930
4931 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
4932 /*
4933 * XXX KDM this shouldn't happen, but what if it does?
4934 */
4935 if (io->io_hdr.io_type != CTL_IO_SCSI)
4936 panic("I/O type isn't CTL_IO_SCSI!");
4937
4938 if ((io->io_hdr.port_status == 0)
4939 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
4940 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE))
4941 io->io_hdr.status = CTL_SUCCESS;
4942 else if ((io->io_hdr.port_status != 0)
4943 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
4944 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){
4945 /*
4946 * For hardware error sense keys, the sense key
4947 * specific value is defined to be a retry count,
4948 * but we use it to pass back an internal FETD
4949 * error code. XXX KDM Hopefully the FETD is only
4950 * using 16 bits for an error code, since that's
4951 * all the space we have in the sks field.
4952 */
4953 ctl_set_internal_failure(&io->scsiio,
4954 /*sks_valid*/ 1,
4955 /*retry_count*/
4956 io->io_hdr.port_status);
4957 free(io->scsiio.kern_data_ptr, M_CTL);
4958 ctl_done(io);
4959 goto bailout;
4960 }
4961
4962 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
4963 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
4964 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
4965 /*
4966 * XXX KDM just assuming a single pointer here, and not a
4967 * S/G list. If we start using S/G lists for config data,
4968 * we'll need to know how to clean them up here as well.
4969 */
4970 free(io->scsiio.kern_data_ptr, M_CTL);
4971 /* Hopefully the user has already set the status... */
4972 ctl_done(io);
4973 } else {
4974 /*
4975 * XXX KDM now we need to continue data movement. Some
4976 * options:
4977 * - call ctl_scsiio() again? We don't do this for data
4978 * writes, because for those at least we know ahead of
4979 * time where the write will go and how long it is. For
4980 * config writes, though, that information is largely
4981 * contained within the write itself, thus we need to
4982 * parse out the data again.
4983 *
4984 * - Call some other function once the data is in?
4985 */
4986
4987 /*
4988 * XXX KDM call ctl_scsiio() again for now, and check flag
4989 * bits to see whether we're allocated or not.
4990 */
4991 retval = ctl_scsiio(&io->scsiio);
4992 }
4993bailout:
4994 return (retval);
4995}
4996
4997/*
4998 * This gets called by a backend driver when it is done with a
4999 * data_submit method.
5000 */
5001void
5002ctl_data_submit_done(union ctl_io *io)
5003{
5004 /*
5005 * If the IO_CONT flag is set, we need to call the supplied
5006 * function to continue processing the I/O, instead of completing
5007 * the I/O just yet.
5008 *
5009 * If there is an error, though, we don't want to keep processing.
5010 * Instead, just send status back to the initiator.
5011 */
5012 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT)
5013 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)
5014 || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) {
5015 io->scsiio.io_cont(io);
5016 return;
5017 }
5018 ctl_done(io);
5019}
5020
5021/*
5022 * This gets called by a backend driver when it is done with a
4999 * configuration write.
5000 */
5001void
5002ctl_config_write_done(union ctl_io *io)
5003{
5004 /*
5005 * If the IO_CONT flag is set, we need to call the supplied
5006 * function to continue processing the I/O, instead of completing
5007 * the I/O just yet.
5008 *
5009 * If there is an error, though, we don't want to keep processing.
5010 * Instead, just send status back to the initiator.
5011 */
5012 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT)
5013 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)
5014 || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) {
5015 io->scsiio.io_cont(io);
5016 return;
5017 }
5018 /*
5019 * Since a configuration write can be done for commands that actually
5020 * have data allocated, like write buffer, and commands that have
5021 * no data, like start/stop unit, we need to check here.
5022 */
5023 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
5024 free(io->scsiio.kern_data_ptr, M_CTL);
5025 ctl_done(io);
5026}
5027
5028/*
5029 * SCSI release command.
5030 */
5031int
5032ctl_scsi_release(struct ctl_scsiio *ctsio)
5033{
5034 int length, longid, thirdparty_id, resv_id;
5035 struct ctl_softc *ctl_softc;
5036 struct ctl_lun *lun;
5037
5038 length = 0;
5039 resv_id = 0;
5040
5041 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5042
5043 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5044 ctl_softc = control_softc;
5045
5046 switch (ctsio->cdb[0]) {
5047 case RELEASE: {
5048 struct scsi_release *cdb;
5049
5050 cdb = (struct scsi_release *)ctsio->cdb;
5051 if ((cdb->byte2 & 0x1f) != 0) {
5052 ctl_set_invalid_field(ctsio,
5053 /*sks_valid*/ 1,
5054 /*command*/ 1,
5055 /*field*/ 1,
5056 /*bit_valid*/ 0,
5057 /*bit*/ 0);
5058 ctl_done((union ctl_io *)ctsio);
5059 return (CTL_RETVAL_COMPLETE);
5060 }
5061 break;
5062 }
5063 case RELEASE_10: {
5064 struct scsi_release_10 *cdb;
5065
5066 cdb = (struct scsi_release_10 *)ctsio->cdb;
5067
5068 if ((cdb->byte2 & SR10_EXTENT) != 0) {
5069 ctl_set_invalid_field(ctsio,
5070 /*sks_valid*/ 1,
5071 /*command*/ 1,
5072 /*field*/ 1,
5073 /*bit_valid*/ 1,
5074 /*bit*/ 0);
5075 ctl_done((union ctl_io *)ctsio);
5076 return (CTL_RETVAL_COMPLETE);
5077
5078 }
5079
5080 if ((cdb->byte2 & SR10_3RDPTY) != 0) {
5081 ctl_set_invalid_field(ctsio,
5082 /*sks_valid*/ 1,
5083 /*command*/ 1,
5084 /*field*/ 1,
5085 /*bit_valid*/ 1,
5086 /*bit*/ 4);
5087 ctl_done((union ctl_io *)ctsio);
5088 return (CTL_RETVAL_COMPLETE);
5089 }
5090
5091 if (cdb->byte2 & SR10_LONGID)
5092 longid = 1;
5093 else
5094 thirdparty_id = cdb->thirdparty_id;
5095
5096 resv_id = cdb->resv_id;
5097 length = scsi_2btoul(cdb->length);
5098 break;
5099 }
5100 }
5101
5102
5103 /*
5104 * XXX KDM right now, we only support LUN reservation. We don't
5105 * support 3rd party reservations, or extent reservations, which
5106 * might actually need the parameter list. If we've gotten this
5107 * far, we've got a LUN reservation. Anything else got kicked out
5108 * above. So, according to SPC, ignore the length.
5109 */
5110 length = 0;
5111
5112 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5113 && (length > 0)) {
5114 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5115 ctsio->kern_data_len = length;
5116 ctsio->kern_total_len = length;
5117 ctsio->kern_data_resid = 0;
5118 ctsio->kern_rel_offset = 0;
5119 ctsio->kern_sg_entries = 0;
5120 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5121 ctsio->be_move_done = ctl_config_move_done;
5122 ctl_datamove((union ctl_io *)ctsio);
5123
5124 return (CTL_RETVAL_COMPLETE);
5125 }
5126
5127 if (length > 0)
5128 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5129
5130 mtx_lock(&ctl_softc->ctl_lock);
5131
5132 /*
5133 * According to SPC, it is not an error for an intiator to attempt
5134 * to release a reservation on a LUN that isn't reserved, or that
5135 * is reserved by another initiator. The reservation can only be
5136 * released, though, by the initiator who made it or by one of
5137 * several reset type events.
5138 */
5139 if (lun->flags & CTL_LUN_RESERVED) {
5140 if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id)
5141 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port)
5142 && (ctsio->io_hdr.nexus.targ_target.id ==
5143 lun->rsv_nexus.targ_target.id)) {
5144 lun->flags &= ~CTL_LUN_RESERVED;
5145 }
5146 }
5147
5148 ctsio->scsi_status = SCSI_STATUS_OK;
5149 ctsio->io_hdr.status = CTL_SUCCESS;
5150
5151 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5152 free(ctsio->kern_data_ptr, M_CTL);
5153 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5154 }
5155
5156 mtx_unlock(&ctl_softc->ctl_lock);
5157
5158 ctl_done((union ctl_io *)ctsio);
5159 return (CTL_RETVAL_COMPLETE);
5160}
5161
5162int
5163ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5164{
5165 int extent, thirdparty, longid;
5166 int resv_id, length;
5167 uint64_t thirdparty_id;
5168 struct ctl_softc *ctl_softc;
5169 struct ctl_lun *lun;
5170
5171 extent = 0;
5172 thirdparty = 0;
5173 longid = 0;
5174 resv_id = 0;
5175 length = 0;
5176 thirdparty_id = 0;
5177
5178 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5179
5180 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5181 ctl_softc = control_softc;
5182
5183 switch (ctsio->cdb[0]) {
5184 case RESERVE: {
5185 struct scsi_reserve *cdb;
5186
5187 cdb = (struct scsi_reserve *)ctsio->cdb;
5188 if ((cdb->byte2 & 0x1f) != 0) {
5189 ctl_set_invalid_field(ctsio,
5190 /*sks_valid*/ 1,
5191 /*command*/ 1,
5192 /*field*/ 1,
5193 /*bit_valid*/ 0,
5194 /*bit*/ 0);
5195 ctl_done((union ctl_io *)ctsio);
5196 return (CTL_RETVAL_COMPLETE);
5197 }
5198 resv_id = cdb->resv_id;
5199 length = scsi_2btoul(cdb->length);
5200 break;
5201 }
5202 case RESERVE_10: {
5203 struct scsi_reserve_10 *cdb;
5204
5205 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
5206
5207 if ((cdb->byte2 & SR10_EXTENT) != 0) {
5208 ctl_set_invalid_field(ctsio,
5209 /*sks_valid*/ 1,
5210 /*command*/ 1,
5211 /*field*/ 1,
5212 /*bit_valid*/ 1,
5213 /*bit*/ 0);
5214 ctl_done((union ctl_io *)ctsio);
5215 return (CTL_RETVAL_COMPLETE);
5216 }
5217 if ((cdb->byte2 & SR10_3RDPTY) != 0) {
5218 ctl_set_invalid_field(ctsio,
5219 /*sks_valid*/ 1,
5220 /*command*/ 1,
5221 /*field*/ 1,
5222 /*bit_valid*/ 1,
5223 /*bit*/ 4);
5224 ctl_done((union ctl_io *)ctsio);
5225 return (CTL_RETVAL_COMPLETE);
5226 }
5227 if (cdb->byte2 & SR10_LONGID)
5228 longid = 1;
5229 else
5230 thirdparty_id = cdb->thirdparty_id;
5231
5232 resv_id = cdb->resv_id;
5233 length = scsi_2btoul(cdb->length);
5234 break;
5235 }
5236 }
5237
5238 /*
5239 * XXX KDM right now, we only support LUN reservation. We don't
5240 * support 3rd party reservations, or extent reservations, which
5241 * might actually need the parameter list. If we've gotten this
5242 * far, we've got a LUN reservation. Anything else got kicked out
5243 * above. So, according to SPC, ignore the length.
5244 */
5245 length = 0;
5246
5247 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5248 && (length > 0)) {
5249 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5250 ctsio->kern_data_len = length;
5251 ctsio->kern_total_len = length;
5252 ctsio->kern_data_resid = 0;
5253 ctsio->kern_rel_offset = 0;
5254 ctsio->kern_sg_entries = 0;
5255 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5256 ctsio->be_move_done = ctl_config_move_done;
5257 ctl_datamove((union ctl_io *)ctsio);
5258
5259 return (CTL_RETVAL_COMPLETE);
5260 }
5261
5262 if (length > 0)
5263 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5264
5265 mtx_lock(&ctl_softc->ctl_lock);
5266 if (lun->flags & CTL_LUN_RESERVED) {
5267 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
5268 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
5269 || (ctsio->io_hdr.nexus.targ_target.id !=
5270 lun->rsv_nexus.targ_target.id)) {
5271 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
5272 ctsio->io_hdr.status = CTL_SCSI_ERROR;
5273 goto bailout;
5274 }
5275 }
5276
5277 lun->flags |= CTL_LUN_RESERVED;
5278 lun->rsv_nexus = ctsio->io_hdr.nexus;
5279
5280 ctsio->scsi_status = SCSI_STATUS_OK;
5281 ctsio->io_hdr.status = CTL_SUCCESS;
5282
5283bailout:
5284 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5285 free(ctsio->kern_data_ptr, M_CTL);
5286 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5287 }
5288
5289 mtx_unlock(&ctl_softc->ctl_lock);
5290
5291 ctl_done((union ctl_io *)ctsio);
5292 return (CTL_RETVAL_COMPLETE);
5293}
5294
5295int
5296ctl_start_stop(struct ctl_scsiio *ctsio)
5297{
5298 struct scsi_start_stop_unit *cdb;
5299 struct ctl_lun *lun;
5300 struct ctl_softc *ctl_softc;
5301 int retval;
5302
5303 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5304
5305 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5306 ctl_softc = control_softc;
5307 retval = 0;
5308
5309 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5310
5311 /*
5312 * XXX KDM
5313 * We don't support the immediate bit on a stop unit. In order to
5314 * do that, we would need to code up a way to know that a stop is
5315 * pending, and hold off any new commands until it completes, one
5316 * way or another. Then we could accept or reject those commands
5317 * depending on its status. We would almost need to do the reverse
5318 * of what we do below for an immediate start -- return the copy of
5319 * the ctl_io to the FETD with status to send to the host (and to
5320 * free the copy!) and then free the original I/O once the stop
5321 * actually completes. That way, the OOA queue mechanism can work
5322 * to block commands that shouldn't proceed. Another alternative
5323 * would be to put the copy in the queue in place of the original,
5324 * and return the original back to the caller. That could be
5325 * slightly safer..
5326 */
5327 if ((cdb->byte2 & SSS_IMMED)
5328 && ((cdb->how & SSS_START) == 0)) {
5329 ctl_set_invalid_field(ctsio,
5330 /*sks_valid*/ 1,
5331 /*command*/ 1,
5332 /*field*/ 1,
5333 /*bit_valid*/ 1,
5334 /*bit*/ 0);
5335 ctl_done((union ctl_io *)ctsio);
5336 return (CTL_RETVAL_COMPLETE);
5337 }
5338
5339 /*
5340 * We don't support the power conditions field. We need to check
5341 * this prior to checking the load/eject and start/stop bits.
5342 */
5343 if ((cdb->how & SSS_PC_MASK) != SSS_PC_START_VALID) {
5344 ctl_set_invalid_field(ctsio,
5345 /*sks_valid*/ 1,
5346 /*command*/ 1,
5347 /*field*/ 4,
5348 /*bit_valid*/ 1,
5349 /*bit*/ 4);
5350 ctl_done((union ctl_io *)ctsio);
5351 return (CTL_RETVAL_COMPLETE);
5352 }
5353
5354 /*
5355 * Media isn't removable, so we can't load or eject it.
5356 */
5357 if ((cdb->how & SSS_LOEJ) != 0) {
5358 ctl_set_invalid_field(ctsio,
5359 /*sks_valid*/ 1,
5360 /*command*/ 1,
5361 /*field*/ 4,
5362 /*bit_valid*/ 1,
5363 /*bit*/ 1);
5364 ctl_done((union ctl_io *)ctsio);
5365 return (CTL_RETVAL_COMPLETE);
5366 }
5367
5368 if ((lun->flags & CTL_LUN_PR_RESERVED)
5369 && ((cdb->how & SSS_START)==0)) {
5370 uint32_t residx;
5371
5372 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5373 if (!lun->per_res[residx].registered
5374 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5375
5376 ctl_set_reservation_conflict(ctsio);
5377 ctl_done((union ctl_io *)ctsio);
5378 return (CTL_RETVAL_COMPLETE);
5379 }
5380 }
5381
5382 /*
5383 * If there is no backend on this device, we can't start or stop
5384 * it. In theory we shouldn't get any start/stop commands in the
5385 * first place at this level if the LUN doesn't have a backend.
5386 * That should get stopped by the command decode code.
5387 */
5388 if (lun->backend == NULL) {
5389 ctl_set_invalid_opcode(ctsio);
5390 ctl_done((union ctl_io *)ctsio);
5391 return (CTL_RETVAL_COMPLETE);
5392 }
5393
5394 /*
5395 * XXX KDM Copan-specific offline behavior.
5396 * Figure out a reasonable way to port this?
5397 */
5398#ifdef NEEDTOPORT
5399 mtx_lock(&ctl_softc->ctl_lock);
5400
5401 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5402 && (lun->flags & CTL_LUN_OFFLINE)) {
5403 /*
5404 * If the LUN is offline, and the on/offline bit isn't set,
5405 * reject the start or stop. Otherwise, let it through.
5406 */
5407 mtx_unlock(&ctl_softc->ctl_lock);
5408 ctl_set_lun_not_ready(ctsio);
5409 ctl_done((union ctl_io *)ctsio);
5410 } else {
5411 mtx_unlock(&ctl_softc->ctl_lock);
5412#endif /* NEEDTOPORT */
5413 /*
5414 * This could be a start or a stop when we're online,
5415 * or a stop/offline or start/online. A start or stop when
5416 * we're offline is covered in the case above.
5417 */
5418 /*
5419 * In the non-immediate case, we send the request to
5420 * the backend and return status to the user when
5421 * it is done.
5422 *
5423 * In the immediate case, we allocate a new ctl_io
5424 * to hold a copy of the request, and send that to
5425 * the backend. We then set good status on the
5426 * user's request and return it immediately.
5427 */
5428 if (cdb->byte2 & SSS_IMMED) {
5429 union ctl_io *new_io;
5430
5431 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5432 if (new_io == NULL) {
5433 ctl_set_busy(ctsio);
5434 ctl_done((union ctl_io *)ctsio);
5435 } else {
5436 ctl_copy_io((union ctl_io *)ctsio,
5437 new_io);
5438 retval = lun->backend->config_write(new_io);
5439 ctl_set_success(ctsio);
5440 ctl_done((union ctl_io *)ctsio);
5441 }
5442 } else {
5443 retval = lun->backend->config_write(
5444 (union ctl_io *)ctsio);
5445 }
5446#ifdef NEEDTOPORT
5447 }
5448#endif
5449 return (retval);
5450}
5451
5452/*
5453 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5454 * we don't really do anything with the LBA and length fields if the user
5455 * passes them in. Instead we'll just flush out the cache for the entire
5456 * LUN.
5457 */
5458int
5459ctl_sync_cache(struct ctl_scsiio *ctsio)
5460{
5461 struct ctl_lun *lun;
5462 struct ctl_softc *ctl_softc;
5463 uint64_t starting_lba;
5464 uint32_t block_count;
5465 int reladr, immed;
5466 int retval;
5467
5468 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5469
5470 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5471 ctl_softc = control_softc;
5472 retval = 0;
5473 reladr = 0;
5474 immed = 0;
5475
5476 switch (ctsio->cdb[0]) {
5477 case SYNCHRONIZE_CACHE: {
5478 struct scsi_sync_cache *cdb;
5479 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5480
5481 if (cdb->byte2 & SSC_RELADR)
5482 reladr = 1;
5483
5484 if (cdb->byte2 & SSC_IMMED)
5485 immed = 1;
5486
5487 starting_lba = scsi_4btoul(cdb->begin_lba);
5488 block_count = scsi_2btoul(cdb->lb_count);
5489 break;
5490 }
5491 case SYNCHRONIZE_CACHE_16: {
5492 struct scsi_sync_cache_16 *cdb;
5493 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5494
5495 if (cdb->byte2 & SSC_RELADR)
5496 reladr = 1;
5497
5498 if (cdb->byte2 & SSC_IMMED)
5499 immed = 1;
5500
5501 starting_lba = scsi_8btou64(cdb->begin_lba);
5502 block_count = scsi_4btoul(cdb->lb_count);
5503 break;
5504 }
5505 default:
5506 ctl_set_invalid_opcode(ctsio);
5507 ctl_done((union ctl_io *)ctsio);
5508 goto bailout;
5509 break; /* NOTREACHED */
5510 }
5511
5512 if (immed) {
5513 /*
5514 * We don't support the immediate bit. Since it's in the
5515 * same place for the 10 and 16 byte SYNCHRONIZE CACHE
5516 * commands, we can just return the same error in either
5517 * case.
5518 */
5519 ctl_set_invalid_field(ctsio,
5520 /*sks_valid*/ 1,
5521 /*command*/ 1,
5522 /*field*/ 1,
5523 /*bit_valid*/ 1,
5524 /*bit*/ 1);
5525 ctl_done((union ctl_io *)ctsio);
5526 goto bailout;
5527 }
5528
5529 if (reladr) {
5530 /*
5531 * We don't support the reladr bit either. It can only be
5532 * used with linked commands, and we don't support linked
5533 * commands. Since the bit is in the same place for the
5534 * 10 and 16 byte SYNCHRONIZE CACHE * commands, we can
5535 * just return the same error in either case.
5536 */
5537 ctl_set_invalid_field(ctsio,
5538 /*sks_valid*/ 1,
5539 /*command*/ 1,
5540 /*field*/ 1,
5541 /*bit_valid*/ 1,
5542 /*bit*/ 0);
5543 ctl_done((union ctl_io *)ctsio);
5544 goto bailout;
5545 }
5546
5547 /*
5548 * We check the LBA and length, but don't do anything with them.
5549 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5550 * get flushed. This check will just help satisfy anyone who wants
5551 * to see an error for an out of range LBA.
5552 */
5553 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5554 ctl_set_lba_out_of_range(ctsio);
5555 ctl_done((union ctl_io *)ctsio);
5556 goto bailout;
5557 }
5558
5559 /*
5560 * If this LUN has no backend, we can't flush the cache anyway.
5561 */
5562 if (lun->backend == NULL) {
5563 ctl_set_invalid_opcode(ctsio);
5564 ctl_done((union ctl_io *)ctsio);
5565 goto bailout;
5566 }
5567
5568 /*
5569 * Check to see whether we're configured to send the SYNCHRONIZE
5570 * CACHE command directly to the back end.
5571 */
5572 mtx_lock(&ctl_softc->ctl_lock);
5573 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC)
5574 && (++(lun->sync_count) >= lun->sync_interval)) {
5575 lun->sync_count = 0;
5576 mtx_unlock(&ctl_softc->ctl_lock);
5577 retval = lun->backend->config_write((union ctl_io *)ctsio);
5578 } else {
5579 mtx_unlock(&ctl_softc->ctl_lock);
5580 ctl_set_success(ctsio);
5581 ctl_done((union ctl_io *)ctsio);
5582 }
5583
5584bailout:
5585
5586 return (retval);
5587}
5588
5589int
5590ctl_format(struct ctl_scsiio *ctsio)
5591{
5592 struct scsi_format *cdb;
5593 struct ctl_lun *lun;
5594 struct ctl_softc *ctl_softc;
5595 int length, defect_list_len;
5596
5597 CTL_DEBUG_PRINT(("ctl_format\n"));
5598
5599 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5600 ctl_softc = control_softc;
5601
5602 cdb = (struct scsi_format *)ctsio->cdb;
5603
5604 length = 0;
5605 if (cdb->byte2 & SF_FMTDATA) {
5606 if (cdb->byte2 & SF_LONGLIST)
5607 length = sizeof(struct scsi_format_header_long);
5608 else
5609 length = sizeof(struct scsi_format_header_short);
5610 }
5611
5612 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5613 && (length > 0)) {
5614 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5615 ctsio->kern_data_len = length;
5616 ctsio->kern_total_len = length;
5617 ctsio->kern_data_resid = 0;
5618 ctsio->kern_rel_offset = 0;
5619 ctsio->kern_sg_entries = 0;
5620 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5621 ctsio->be_move_done = ctl_config_move_done;
5622 ctl_datamove((union ctl_io *)ctsio);
5623
5624 return (CTL_RETVAL_COMPLETE);
5625 }
5626
5627 defect_list_len = 0;
5628
5629 if (cdb->byte2 & SF_FMTDATA) {
5630 if (cdb->byte2 & SF_LONGLIST) {
5631 struct scsi_format_header_long *header;
5632
5633 header = (struct scsi_format_header_long *)
5634 ctsio->kern_data_ptr;
5635
5636 defect_list_len = scsi_4btoul(header->defect_list_len);
5637 if (defect_list_len != 0) {
5638 ctl_set_invalid_field(ctsio,
5639 /*sks_valid*/ 1,
5640 /*command*/ 0,
5641 /*field*/ 2,
5642 /*bit_valid*/ 0,
5643 /*bit*/ 0);
5644 goto bailout;
5645 }
5646 } else {
5647 struct scsi_format_header_short *header;
5648
5649 header = (struct scsi_format_header_short *)
5650 ctsio->kern_data_ptr;
5651
5652 defect_list_len = scsi_2btoul(header->defect_list_len);
5653 if (defect_list_len != 0) {
5654 ctl_set_invalid_field(ctsio,
5655 /*sks_valid*/ 1,
5656 /*command*/ 0,
5657 /*field*/ 2,
5658 /*bit_valid*/ 0,
5659 /*bit*/ 0);
5660 goto bailout;
5661 }
5662 }
5663 }
5664
5665 /*
5666 * The format command will clear out the "Medium format corrupted"
5667 * status if set by the configuration code. That status is really
5668 * just a way to notify the host that we have lost the media, and
5669 * get them to issue a command that will basically make them think
5670 * they're blowing away the media.
5671 */
5672 mtx_lock(&ctl_softc->ctl_lock);
5673 lun->flags &= ~CTL_LUN_INOPERABLE;
5674 mtx_unlock(&ctl_softc->ctl_lock);
5675
5676 ctsio->scsi_status = SCSI_STATUS_OK;
5677 ctsio->io_hdr.status = CTL_SUCCESS;
5678bailout:
5679
5680 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5681 free(ctsio->kern_data_ptr, M_CTL);
5682 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5683 }
5684
5685 ctl_done((union ctl_io *)ctsio);
5686 return (CTL_RETVAL_COMPLETE);
5687}
5688
5689int
5690ctl_write_buffer(struct ctl_scsiio *ctsio)
5691{
5692 struct scsi_write_buffer *cdb;
5693 struct copan_page_header *header;
5694 struct ctl_lun *lun;
5695 struct ctl_softc *ctl_softc;
5696 int buffer_offset, len;
5697 int retval;
5698
5699 header = NULL;
5700
5701 retval = CTL_RETVAL_COMPLETE;
5702
5703 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5704
5705 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5706 ctl_softc = control_softc;
5707 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5708
5709 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5710 ctl_set_invalid_field(ctsio,
5711 /*sks_valid*/ 1,
5712 /*command*/ 1,
5713 /*field*/ 1,
5714 /*bit_valid*/ 1,
5715 /*bit*/ 4);
5716 ctl_done((union ctl_io *)ctsio);
5717 return (CTL_RETVAL_COMPLETE);
5718 }
5719 if (cdb->buffer_id != 0) {
5720 ctl_set_invalid_field(ctsio,
5721 /*sks_valid*/ 1,
5722 /*command*/ 1,
5723 /*field*/ 2,
5724 /*bit_valid*/ 0,
5725 /*bit*/ 0);
5726 ctl_done((union ctl_io *)ctsio);
5727 return (CTL_RETVAL_COMPLETE);
5728 }
5729
5730 len = scsi_3btoul(cdb->length);
5731 buffer_offset = scsi_3btoul(cdb->offset);
5732
5733 if (len > sizeof(lun->write_buffer)) {
5734 ctl_set_invalid_field(ctsio,
5735 /*sks_valid*/ 1,
5736 /*command*/ 1,
5737 /*field*/ 6,
5738 /*bit_valid*/ 0,
5739 /*bit*/ 0);
5740 ctl_done((union ctl_io *)ctsio);
5741 return (CTL_RETVAL_COMPLETE);
5742 }
5743
5744 if (buffer_offset != 0) {
5745 ctl_set_invalid_field(ctsio,
5746 /*sks_valid*/ 1,
5747 /*command*/ 1,
5748 /*field*/ 3,
5749 /*bit_valid*/ 0,
5750 /*bit*/ 0);
5751 ctl_done((union ctl_io *)ctsio);
5752 return (CTL_RETVAL_COMPLETE);
5753 }
5754
5755 /*
5756 * If we've got a kernel request that hasn't been malloced yet,
5757 * malloc it and tell the caller the data buffer is here.
5758 */
5759 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5760 ctsio->kern_data_ptr = lun->write_buffer;
5761 ctsio->kern_data_len = len;
5762 ctsio->kern_total_len = len;
5763 ctsio->kern_data_resid = 0;
5764 ctsio->kern_rel_offset = 0;
5765 ctsio->kern_sg_entries = 0;
5766 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5767 ctsio->be_move_done = ctl_config_move_done;
5768 ctl_datamove((union ctl_io *)ctsio);
5769
5770 return (CTL_RETVAL_COMPLETE);
5771 }
5772
5773 ctl_done((union ctl_io *)ctsio);
5774
5775 return (CTL_RETVAL_COMPLETE);
5776}
5777
5778int
5779ctl_write_same(struct ctl_scsiio *ctsio)
5780{
5781 struct ctl_lun *lun;
5782 struct ctl_lba_len_flags *lbalen;
5783 uint64_t lba;
5784 uint32_t num_blocks;
5785 int len, retval;
5786 uint8_t byte2;
5787
5788 retval = CTL_RETVAL_COMPLETE;
5789
5790 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5791
5792 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5793
5794 switch (ctsio->cdb[0]) {
5795 case WRITE_SAME_10: {
5796 struct scsi_write_same_10 *cdb;
5797
5798 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5799
5800 lba = scsi_4btoul(cdb->addr);
5801 num_blocks = scsi_2btoul(cdb->length);
5802 byte2 = cdb->byte2;
5803 break;
5804 }
5805 case WRITE_SAME_16: {
5806 struct scsi_write_same_16 *cdb;
5807
5808 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5809
5810 lba = scsi_8btou64(cdb->addr);
5811 num_blocks = scsi_4btoul(cdb->length);
5812 byte2 = cdb->byte2;
5813 break;
5814 }
5815 default:
5816 /*
5817 * We got a command we don't support. This shouldn't
5818 * happen, commands should be filtered out above us.
5819 */
5820 ctl_set_invalid_opcode(ctsio);
5821 ctl_done((union ctl_io *)ctsio);
5822
5823 return (CTL_RETVAL_COMPLETE);
5824 break; /* NOTREACHED */
5825 }
5826
5827 /*
5828 * The first check is to make sure we're in bounds, the second
5829 * check is to catch wrap-around problems. If the lba + num blocks
5830 * is less than the lba, then we've wrapped around and the block
5831 * range is invalid anyway.
5832 */
5833 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5834 || ((lba + num_blocks) < lba)) {
5835 ctl_set_lba_out_of_range(ctsio);
5836 ctl_done((union ctl_io *)ctsio);
5837 return (CTL_RETVAL_COMPLETE);
5838 }
5839
5840 /* Zero number of blocks means "to the last logical block" */
5841 if (num_blocks == 0) {
5842 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5843 ctl_set_invalid_field(ctsio,
5844 /*sks_valid*/ 0,
5845 /*command*/ 1,
5846 /*field*/ 0,
5847 /*bit_valid*/ 0,
5848 /*bit*/ 0);
5849 ctl_done((union ctl_io *)ctsio);
5850 return (CTL_RETVAL_COMPLETE);
5851 }
5852 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5853 }
5854
5855 len = lun->be_lun->blocksize;
5856
5857 /*
5858 * If we've got a kernel request that hasn't been malloced yet,
5859 * malloc it and tell the caller the data buffer is here.
5860 */
5861 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5862 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5863 ctsio->kern_data_len = len;
5864 ctsio->kern_total_len = len;
5865 ctsio->kern_data_resid = 0;
5866 ctsio->kern_rel_offset = 0;
5867 ctsio->kern_sg_entries = 0;
5868 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5869 ctsio->be_move_done = ctl_config_move_done;
5870 ctl_datamove((union ctl_io *)ctsio);
5871
5872 return (CTL_RETVAL_COMPLETE);
5873 }
5874
5875 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5876 lbalen->lba = lba;
5877 lbalen->len = num_blocks;
5878 lbalen->flags = byte2;
5879 retval = lun->backend->config_write((union ctl_io *)ctsio);
5880
5881 return (retval);
5882}
5883
5884int
5885ctl_unmap(struct ctl_scsiio *ctsio)
5886{
5887 struct ctl_lun *lun;
5888 struct scsi_unmap *cdb;
5889 struct ctl_ptr_len_flags *ptrlen;
5890 struct scsi_unmap_header *hdr;
5891 struct scsi_unmap_desc *buf, *end;
5892 uint64_t lba;
5893 uint32_t num_blocks;
5894 int len, retval;
5895 uint8_t byte2;
5896
5897 retval = CTL_RETVAL_COMPLETE;
5898
5899 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5900
5901 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5902 cdb = (struct scsi_unmap *)ctsio->cdb;
5903
5904 len = scsi_2btoul(cdb->length);
5905 byte2 = cdb->byte2;
5906
5907 /*
5908 * If we've got a kernel request that hasn't been malloced yet,
5909 * malloc it and tell the caller the data buffer is here.
5910 */
5911 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5912 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5913 ctsio->kern_data_len = len;
5914 ctsio->kern_total_len = len;
5915 ctsio->kern_data_resid = 0;
5916 ctsio->kern_rel_offset = 0;
5917 ctsio->kern_sg_entries = 0;
5918 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5919 ctsio->be_move_done = ctl_config_move_done;
5920 ctl_datamove((union ctl_io *)ctsio);
5921
5922 return (CTL_RETVAL_COMPLETE);
5923 }
5924
5925 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5926 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5927 if (len < sizeof (*hdr) ||
5928 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5929 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5930 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5931 ctl_set_invalid_field(ctsio,
5932 /*sks_valid*/ 0,
5933 /*command*/ 0,
5934 /*field*/ 0,
5935 /*bit_valid*/ 0,
5936 /*bit*/ 0);
5937 ctl_done((union ctl_io *)ctsio);
5938 return (CTL_RETVAL_COMPLETE);
5939 }
5940 len = scsi_2btoul(hdr->desc_length);
5941 buf = (struct scsi_unmap_desc *)(hdr + 1);
5942 end = buf + len / sizeof(*buf);
5943
5944 ptrlen = (struct ctl_ptr_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5945 ptrlen->ptr = (void *)buf;
5946 ptrlen->len = len;
5947 ptrlen->flags = byte2;
5948
5949 for (; buf < end; buf++) {
5950 lba = scsi_8btou64(buf->lba);
5951 num_blocks = scsi_4btoul(buf->length);
5952 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5953 || ((lba + num_blocks) < lba)) {
5954 ctl_set_lba_out_of_range(ctsio);
5955 ctl_done((union ctl_io *)ctsio);
5956 return (CTL_RETVAL_COMPLETE);
5957 }
5958 }
5959
5960 retval = lun->backend->config_write((union ctl_io *)ctsio);
5961
5962 return (retval);
5963}
5964
5965/*
5966 * Note that this function currently doesn't actually do anything inside
5967 * CTL to enforce things if the DQue bit is turned on.
5968 *
5969 * Also note that this function can't be used in the default case, because
5970 * the DQue bit isn't set in the changeable mask for the control mode page
5971 * anyway. This is just here as an example for how to implement a page
5972 * handler, and a placeholder in case we want to allow the user to turn
5973 * tagged queueing on and off.
5974 *
5975 * The D_SENSE bit handling is functional, however, and will turn
5976 * descriptor sense on and off for a given LUN.
5977 */
5978int
5979ctl_control_page_handler(struct ctl_scsiio *ctsio,
5980 struct ctl_page_index *page_index, uint8_t *page_ptr)
5981{
5982 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
5983 struct ctl_lun *lun;
5984 struct ctl_softc *softc;
5985 int set_ua;
5986 uint32_t initidx;
5987
5988 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5989 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5990 set_ua = 0;
5991
5992 user_cp = (struct scsi_control_page *)page_ptr;
5993 current_cp = (struct scsi_control_page *)
5994 (page_index->page_data + (page_index->page_len *
5995 CTL_PAGE_CURRENT));
5996 saved_cp = (struct scsi_control_page *)
5997 (page_index->page_data + (page_index->page_len *
5998 CTL_PAGE_SAVED));
5999
6000 softc = control_softc;
6001
6002 mtx_lock(&softc->ctl_lock);
6003 if (((current_cp->rlec & SCP_DSENSE) == 0)
6004 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
6005 /*
6006 * Descriptor sense is currently turned off and the user
6007 * wants to turn it on.
6008 */
6009 current_cp->rlec |= SCP_DSENSE;
6010 saved_cp->rlec |= SCP_DSENSE;
6011 lun->flags |= CTL_LUN_SENSE_DESC;
6012 set_ua = 1;
6013 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
6014 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
6015 /*
6016 * Descriptor sense is currently turned on, and the user
6017 * wants to turn it off.
6018 */
6019 current_cp->rlec &= ~SCP_DSENSE;
6020 saved_cp->rlec &= ~SCP_DSENSE;
6021 lun->flags &= ~CTL_LUN_SENSE_DESC;
6022 set_ua = 1;
6023 }
6024 if (current_cp->queue_flags & SCP_QUEUE_DQUE) {
6025 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
6026#ifdef NEEDTOPORT
6027 csevent_log(CSC_CTL | CSC_SHELF_SW |
6028 CTL_UNTAG_TO_UNTAG,
6029 csevent_LogType_Trace,
6030 csevent_Severity_Information,
6031 csevent_AlertLevel_Green,
6032 csevent_FRU_Firmware,
6033 csevent_FRU_Unknown,
6034 "Received untagged to untagged transition");
6035#endif /* NEEDTOPORT */
6036 } else {
6037#ifdef NEEDTOPORT
6038 csevent_log(CSC_CTL | CSC_SHELF_SW |
6039 CTL_UNTAG_TO_TAG,
6040 csevent_LogType_ConfigChange,
6041 csevent_Severity_Information,
6042 csevent_AlertLevel_Green,
6043 csevent_FRU_Firmware,
6044 csevent_FRU_Unknown,
6045 "Received untagged to tagged "
6046 "queueing transition");
6047#endif /* NEEDTOPORT */
6048
6049 current_cp->queue_flags &= ~SCP_QUEUE_DQUE;
6050 saved_cp->queue_flags &= ~SCP_QUEUE_DQUE;
6051 set_ua = 1;
6052 }
6053 } else {
6054 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
6055#ifdef NEEDTOPORT
6056 csevent_log(CSC_CTL | CSC_SHELF_SW |
6057 CTL_TAG_TO_UNTAG,
6058 csevent_LogType_ConfigChange,
6059 csevent_Severity_Warning,
6060 csevent_AlertLevel_Yellow,
6061 csevent_FRU_Firmware,
6062 csevent_FRU_Unknown,
6063 "Received tagged queueing to untagged "
6064 "transition");
6065#endif /* NEEDTOPORT */
6066
6067 current_cp->queue_flags |= SCP_QUEUE_DQUE;
6068 saved_cp->queue_flags |= SCP_QUEUE_DQUE;
6069 set_ua = 1;
6070 } else {
6071#ifdef NEEDTOPORT
6072 csevent_log(CSC_CTL | CSC_SHELF_SW |
6073 CTL_TAG_TO_TAG,
6074 csevent_LogType_Trace,
6075 csevent_Severity_Information,
6076 csevent_AlertLevel_Green,
6077 csevent_FRU_Firmware,
6078 csevent_FRU_Unknown,
6079 "Received tagged queueing to tagged "
6080 "queueing transition");
6081#endif /* NEEDTOPORT */
6082 }
6083 }
6084 if (set_ua != 0) {
6085 int i;
6086 /*
6087 * Let other initiators know that the mode
6088 * parameters for this LUN have changed.
6089 */
6090 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
6091 if (i == initidx)
6092 continue;
6093
6094 lun->pending_sense[i].ua_pending |=
6095 CTL_UA_MODE_CHANGE;
6096 }
6097 }
6098 mtx_unlock(&softc->ctl_lock);
6099
6100 return (0);
6101}
6102
6103int
6104ctl_power_sp_handler(struct ctl_scsiio *ctsio,
6105 struct ctl_page_index *page_index, uint8_t *page_ptr)
6106{
6107 return (0);
6108}
6109
6110int
6111ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio,
6112 struct ctl_page_index *page_index, int pc)
6113{
6114 struct copan_power_subpage *page;
6115
6116 page = (struct copan_power_subpage *)page_index->page_data +
6117 (page_index->page_len * pc);
6118
6119 switch (pc) {
6120 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6121 /*
6122 * We don't update the changable bits for this page.
6123 */
6124 break;
6125 case SMS_PAGE_CTRL_CURRENT >> 6:
6126 case SMS_PAGE_CTRL_DEFAULT >> 6:
6127 case SMS_PAGE_CTRL_SAVED >> 6:
6128#ifdef NEEDTOPORT
6129 ctl_update_power_subpage(page);
6130#endif
6131 break;
6132 default:
6133#ifdef NEEDTOPORT
6134 EPRINT(0, "Invalid PC %d!!", pc);
6135#endif
6136 break;
6137 }
6138 return (0);
6139}
6140
6141
6142int
6143ctl_aps_sp_handler(struct ctl_scsiio *ctsio,
6144 struct ctl_page_index *page_index, uint8_t *page_ptr)
6145{
6146 struct copan_aps_subpage *user_sp;
6147 struct copan_aps_subpage *current_sp;
6148 union ctl_modepage_info *modepage_info;
6149 struct ctl_softc *softc;
6150 struct ctl_lun *lun;
6151 int retval;
6152
6153 retval = CTL_RETVAL_COMPLETE;
6154 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
6155 (page_index->page_len * CTL_PAGE_CURRENT));
6156 softc = control_softc;
6157 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6158
6159 user_sp = (struct copan_aps_subpage *)page_ptr;
6160
6161 modepage_info = (union ctl_modepage_info *)
6162 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6163
6164 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK;
6165 modepage_info->header.subpage = page_index->subpage;
6166 modepage_info->aps.lock_active = user_sp->lock_active;
6167
6168 mtx_lock(&softc->ctl_lock);
6169
6170 /*
6171 * If there is a request to lock the LUN and another LUN is locked
6172 * this is an error. If the requested LUN is already locked ignore
6173 * the request. If no LUN is locked attempt to lock it.
6174 * if there is a request to unlock the LUN and the LUN is currently
6175 * locked attempt to unlock it. Otherwise ignore the request. i.e.
6176 * if another LUN is locked or no LUN is locked.
6177 */
6178 if (user_sp->lock_active & APS_LOCK_ACTIVE) {
6179 if (softc->aps_locked_lun == lun->lun) {
6180 /*
6181 * This LUN is already locked, so we're done.
6182 */
6183 retval = CTL_RETVAL_COMPLETE;
6184 } else if (softc->aps_locked_lun == 0) {
6185 /*
6186 * No one has the lock, pass the request to the
6187 * backend.
6188 */
6189 retval = lun->backend->config_write(
6190 (union ctl_io *)ctsio);
6191 } else {
6192 /*
6193 * Someone else has the lock, throw out the request.
6194 */
6195 ctl_set_already_locked(ctsio);
6196 free(ctsio->kern_data_ptr, M_CTL);
6197 ctl_done((union ctl_io *)ctsio);
6198
6199 /*
6200 * Set the return value so that ctl_do_mode_select()
6201 * won't try to complete the command. We already
6202 * completed it here.
6203 */
6204 retval = CTL_RETVAL_ERROR;
6205 }
6206 } else if (softc->aps_locked_lun == lun->lun) {
6207 /*
6208 * This LUN is locked, so pass the unlock request to the
6209 * backend.
6210 */
6211 retval = lun->backend->config_write((union ctl_io *)ctsio);
6212 }
6213 mtx_unlock(&softc->ctl_lock);
6214
6215 return (retval);
6216}
6217
6218int
6219ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
6220 struct ctl_page_index *page_index,
6221 uint8_t *page_ptr)
6222{
6223 uint8_t *c;
6224 int i;
6225
6226 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
6227 ctl_time_io_secs =
6228 (c[0] << 8) |
6229 (c[1] << 0) |
6230 0;
6231 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
6232 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
6233 printf("page data:");
6234 for (i=0; i<8; i++)
6235 printf(" %.2x",page_ptr[i]);
6236 printf("\n");
6237 return (0);
6238}
6239
6240int
6241ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
6242 struct ctl_page_index *page_index,
6243 int pc)
6244{
6245 struct copan_debugconf_subpage *page;
6246
6247 page = (struct copan_debugconf_subpage *)page_index->page_data +
6248 (page_index->page_len * pc);
6249
6250 switch (pc) {
6251 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6252 case SMS_PAGE_CTRL_DEFAULT >> 6:
6253 case SMS_PAGE_CTRL_SAVED >> 6:
6254 /*
6255 * We don't update the changable or default bits for this page.
6256 */
6257 break;
6258 case SMS_PAGE_CTRL_CURRENT >> 6:
6259 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
6260 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
6261 break;
6262 default:
6263#ifdef NEEDTOPORT
6264 EPRINT(0, "Invalid PC %d!!", pc);
6265#endif /* NEEDTOPORT */
6266 break;
6267 }
6268 return (0);
6269}
6270
6271
6272static int
6273ctl_do_mode_select(union ctl_io *io)
6274{
6275 struct scsi_mode_page_header *page_header;
6276 struct ctl_page_index *page_index;
6277 struct ctl_scsiio *ctsio;
6278 int control_dev, page_len;
6279 int page_len_offset, page_len_size;
6280 union ctl_modepage_info *modepage_info;
6281 struct ctl_lun *lun;
6282 int *len_left, *len_used;
6283 int retval, i;
6284
6285 ctsio = &io->scsiio;
6286 page_index = NULL;
6287 page_len = 0;
6288 retval = CTL_RETVAL_COMPLETE;
6289
6290 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6291
6292 if (lun->be_lun->lun_type != T_DIRECT)
6293 control_dev = 1;
6294 else
6295 control_dev = 0;
6296
6297 modepage_info = (union ctl_modepage_info *)
6298 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6299 len_left = &modepage_info->header.len_left;
6300 len_used = &modepage_info->header.len_used;
6301
6302do_next_page:
6303
6304 page_header = (struct scsi_mode_page_header *)
6305 (ctsio->kern_data_ptr + *len_used);
6306
6307 if (*len_left == 0) {
6308 free(ctsio->kern_data_ptr, M_CTL);
6309 ctl_set_success(ctsio);
6310 ctl_done((union ctl_io *)ctsio);
6311 return (CTL_RETVAL_COMPLETE);
6312 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6313
6314 free(ctsio->kern_data_ptr, M_CTL);
6315 ctl_set_param_len_error(ctsio);
6316 ctl_done((union ctl_io *)ctsio);
6317 return (CTL_RETVAL_COMPLETE);
6318
6319 } else if ((page_header->page_code & SMPH_SPF)
6320 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6321
6322 free(ctsio->kern_data_ptr, M_CTL);
6323 ctl_set_param_len_error(ctsio);
6324 ctl_done((union ctl_io *)ctsio);
6325 return (CTL_RETVAL_COMPLETE);
6326 }
6327
6328
6329 /*
6330 * XXX KDM should we do something with the block descriptor?
6331 */
6332 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6333
6334 if ((control_dev != 0)
6335 && (lun->mode_pages.index[i].page_flags &
6336 CTL_PAGE_FLAG_DISK_ONLY))
6337 continue;
6338
6339 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6340 (page_header->page_code & SMPH_PC_MASK))
6341 continue;
6342
6343 /*
6344 * If neither page has a subpage code, then we've got a
6345 * match.
6346 */
6347 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6348 && ((page_header->page_code & SMPH_SPF) == 0)) {
6349 page_index = &lun->mode_pages.index[i];
6350 page_len = page_header->page_length;
6351 break;
6352 }
6353
6354 /*
6355 * If both pages have subpages, then the subpage numbers
6356 * have to match.
6357 */
6358 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6359 && (page_header->page_code & SMPH_SPF)) {
6360 struct scsi_mode_page_header_sp *sph;
6361
6362 sph = (struct scsi_mode_page_header_sp *)page_header;
6363
6364 if (lun->mode_pages.index[i].subpage ==
6365 sph->subpage) {
6366 page_index = &lun->mode_pages.index[i];
6367 page_len = scsi_2btoul(sph->page_length);
6368 break;
6369 }
6370 }
6371 }
6372
6373 /*
6374 * If we couldn't find the page, or if we don't have a mode select
6375 * handler for it, send back an error to the user.
6376 */
6377 if ((page_index == NULL)
6378 || (page_index->select_handler == NULL)) {
6379 ctl_set_invalid_field(ctsio,
6380 /*sks_valid*/ 1,
6381 /*command*/ 0,
6382 /*field*/ *len_used,
6383 /*bit_valid*/ 0,
6384 /*bit*/ 0);
6385 free(ctsio->kern_data_ptr, M_CTL);
6386 ctl_done((union ctl_io *)ctsio);
6387 return (CTL_RETVAL_COMPLETE);
6388 }
6389
6390 if (page_index->page_code & SMPH_SPF) {
6391 page_len_offset = 2;
6392 page_len_size = 2;
6393 } else {
6394 page_len_size = 1;
6395 page_len_offset = 1;
6396 }
6397
6398 /*
6399 * If the length the initiator gives us isn't the one we specify in
6400 * the mode page header, or if they didn't specify enough data in
6401 * the CDB to avoid truncating this page, kick out the request.
6402 */
6403 if ((page_len != (page_index->page_len - page_len_offset -
6404 page_len_size))
6405 || (*len_left < page_index->page_len)) {
6406
6407
6408 ctl_set_invalid_field(ctsio,
6409 /*sks_valid*/ 1,
6410 /*command*/ 0,
6411 /*field*/ *len_used + page_len_offset,
6412 /*bit_valid*/ 0,
6413 /*bit*/ 0);
6414 free(ctsio->kern_data_ptr, M_CTL);
6415 ctl_done((union ctl_io *)ctsio);
6416 return (CTL_RETVAL_COMPLETE);
6417 }
6418
6419 /*
6420 * Run through the mode page, checking to make sure that the bits
6421 * the user changed are actually legal for him to change.
6422 */
6423 for (i = 0; i < page_index->page_len; i++) {
6424 uint8_t *user_byte, *change_mask, *current_byte;
6425 int bad_bit;
6426 int j;
6427
6428 user_byte = (uint8_t *)page_header + i;
6429 change_mask = page_index->page_data +
6430 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6431 current_byte = page_index->page_data +
6432 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6433
6434 /*
6435 * Check to see whether the user set any bits in this byte
6436 * that he is not allowed to set.
6437 */
6438 if ((*user_byte & ~(*change_mask)) ==
6439 (*current_byte & ~(*change_mask)))
6440 continue;
6441
6442 /*
6443 * Go through bit by bit to determine which one is illegal.
6444 */
6445 bad_bit = 0;
6446 for (j = 7; j >= 0; j--) {
6447 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6448 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6449 bad_bit = i;
6450 break;
6451 }
6452 }
6453 ctl_set_invalid_field(ctsio,
6454 /*sks_valid*/ 1,
6455 /*command*/ 0,
6456 /*field*/ *len_used + i,
6457 /*bit_valid*/ 1,
6458 /*bit*/ bad_bit);
6459 free(ctsio->kern_data_ptr, M_CTL);
6460 ctl_done((union ctl_io *)ctsio);
6461 return (CTL_RETVAL_COMPLETE);
6462 }
6463
6464 /*
6465 * Decrement these before we call the page handler, since we may
6466 * end up getting called back one way or another before the handler
6467 * returns to this context.
6468 */
6469 *len_left -= page_index->page_len;
6470 *len_used += page_index->page_len;
6471
6472 retval = page_index->select_handler(ctsio, page_index,
6473 (uint8_t *)page_header);
6474
6475 /*
6476 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6477 * wait until this queued command completes to finish processing
6478 * the mode page. If it returns anything other than
6479 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6480 * already set the sense information, freed the data pointer, and
6481 * completed the io for us.
6482 */
6483 if (retval != CTL_RETVAL_COMPLETE)
6484 goto bailout_no_done;
6485
6486 /*
6487 * If the initiator sent us more than one page, parse the next one.
6488 */
6489 if (*len_left > 0)
6490 goto do_next_page;
6491
6492 ctl_set_success(ctsio);
6493 free(ctsio->kern_data_ptr, M_CTL);
6494 ctl_done((union ctl_io *)ctsio);
6495
6496bailout_no_done:
6497
6498 return (CTL_RETVAL_COMPLETE);
6499
6500}
6501
6502int
6503ctl_mode_select(struct ctl_scsiio *ctsio)
6504{
6505 int param_len, pf, sp;
6506 int header_size, bd_len;
6507 int len_left, len_used;
6508 struct ctl_page_index *page_index;
6509 struct ctl_lun *lun;
6510 int control_dev, page_len;
6511 union ctl_modepage_info *modepage_info;
6512 int retval;
6513
6514 pf = 0;
6515 sp = 0;
6516 page_len = 0;
6517 len_used = 0;
6518 len_left = 0;
6519 retval = 0;
6520 bd_len = 0;
6521 page_index = NULL;
6522
6523 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6524
6525 if (lun->be_lun->lun_type != T_DIRECT)
6526 control_dev = 1;
6527 else
6528 control_dev = 0;
6529
6530 switch (ctsio->cdb[0]) {
6531 case MODE_SELECT_6: {
6532 struct scsi_mode_select_6 *cdb;
6533
6534 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6535
6536 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6537 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6538
6539 param_len = cdb->length;
6540 header_size = sizeof(struct scsi_mode_header_6);
6541 break;
6542 }
6543 case MODE_SELECT_10: {
6544 struct scsi_mode_select_10 *cdb;
6545
6546 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6547
6548 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6549 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6550
6551 param_len = scsi_2btoul(cdb->length);
6552 header_size = sizeof(struct scsi_mode_header_10);
6553 break;
6554 }
6555 default:
6556 ctl_set_invalid_opcode(ctsio);
6557 ctl_done((union ctl_io *)ctsio);
6558 return (CTL_RETVAL_COMPLETE);
6559 break; /* NOTREACHED */
6560 }
6561
6562 /*
6563 * From SPC-3:
6564 * "A parameter list length of zero indicates that the Data-Out Buffer
6565 * shall be empty. This condition shall not be considered as an error."
6566 */
6567 if (param_len == 0) {
6568 ctl_set_success(ctsio);
6569 ctl_done((union ctl_io *)ctsio);
6570 return (CTL_RETVAL_COMPLETE);
6571 }
6572
6573 /*
6574 * Since we'll hit this the first time through, prior to
6575 * allocation, we don't need to free a data buffer here.
6576 */
6577 if (param_len < header_size) {
6578 ctl_set_param_len_error(ctsio);
6579 ctl_done((union ctl_io *)ctsio);
6580 return (CTL_RETVAL_COMPLETE);
6581 }
6582
6583 /*
6584 * Allocate the data buffer and grab the user's data. In theory,
6585 * we shouldn't have to sanity check the parameter list length here
6586 * because the maximum size is 64K. We should be able to malloc
6587 * that much without too many problems.
6588 */
6589 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6590 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6591 ctsio->kern_data_len = param_len;
6592 ctsio->kern_total_len = param_len;
6593 ctsio->kern_data_resid = 0;
6594 ctsio->kern_rel_offset = 0;
6595 ctsio->kern_sg_entries = 0;
6596 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6597 ctsio->be_move_done = ctl_config_move_done;
6598 ctl_datamove((union ctl_io *)ctsio);
6599
6600 return (CTL_RETVAL_COMPLETE);
6601 }
6602
6603 switch (ctsio->cdb[0]) {
6604 case MODE_SELECT_6: {
6605 struct scsi_mode_header_6 *mh6;
6606
6607 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6608 bd_len = mh6->blk_desc_len;
6609 break;
6610 }
6611 case MODE_SELECT_10: {
6612 struct scsi_mode_header_10 *mh10;
6613
6614 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6615 bd_len = scsi_2btoul(mh10->blk_desc_len);
6616 break;
6617 }
6618 default:
6619 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6620 break;
6621 }
6622
6623 if (param_len < (header_size + bd_len)) {
6624 free(ctsio->kern_data_ptr, M_CTL);
6625 ctl_set_param_len_error(ctsio);
6626 ctl_done((union ctl_io *)ctsio);
6627 return (CTL_RETVAL_COMPLETE);
6628 }
6629
6630 /*
6631 * Set the IO_CONT flag, so that if this I/O gets passed to
6632 * ctl_config_write_done(), it'll get passed back to
6633 * ctl_do_mode_select() for further processing, or completion if
6634 * we're all done.
6635 */
6636 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6637 ctsio->io_cont = ctl_do_mode_select;
6638
6639 modepage_info = (union ctl_modepage_info *)
6640 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6641
6642 memset(modepage_info, 0, sizeof(*modepage_info));
6643
6644 len_left = param_len - header_size - bd_len;
6645 len_used = header_size + bd_len;
6646
6647 modepage_info->header.len_left = len_left;
6648 modepage_info->header.len_used = len_used;
6649
6650 return (ctl_do_mode_select((union ctl_io *)ctsio));
6651}
6652
6653int
6654ctl_mode_sense(struct ctl_scsiio *ctsio)
6655{
6656 struct ctl_lun *lun;
6657 int pc, page_code, dbd, llba, subpage;
6658 int alloc_len, page_len, header_len, total_len;
6659 struct scsi_mode_block_descr *block_desc;
6660 struct ctl_page_index *page_index;
6661 int control_dev;
6662
6663 dbd = 0;
6664 llba = 0;
6665 block_desc = NULL;
6666 page_index = NULL;
6667
6668 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6669
6670 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6671
6672 if (lun->be_lun->lun_type != T_DIRECT)
6673 control_dev = 1;
6674 else
6675 control_dev = 0;
6676
6677 switch (ctsio->cdb[0]) {
6678 case MODE_SENSE_6: {
6679 struct scsi_mode_sense_6 *cdb;
6680
6681 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6682
6683 header_len = sizeof(struct scsi_mode_hdr_6);
6684 if (cdb->byte2 & SMS_DBD)
6685 dbd = 1;
6686 else
6687 header_len += sizeof(struct scsi_mode_block_descr);
6688
6689 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6690 page_code = cdb->page & SMS_PAGE_CODE;
6691 subpage = cdb->subpage;
6692 alloc_len = cdb->length;
6693 break;
6694 }
6695 case MODE_SENSE_10: {
6696 struct scsi_mode_sense_10 *cdb;
6697
6698 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6699
6700 header_len = sizeof(struct scsi_mode_hdr_10);
6701
6702 if (cdb->byte2 & SMS_DBD)
6703 dbd = 1;
6704 else
6705 header_len += sizeof(struct scsi_mode_block_descr);
6706 if (cdb->byte2 & SMS10_LLBAA)
6707 llba = 1;
6708 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6709 page_code = cdb->page & SMS_PAGE_CODE;
6710 subpage = cdb->subpage;
6711 alloc_len = scsi_2btoul(cdb->length);
6712 break;
6713 }
6714 default:
6715 ctl_set_invalid_opcode(ctsio);
6716 ctl_done((union ctl_io *)ctsio);
6717 return (CTL_RETVAL_COMPLETE);
6718 break; /* NOTREACHED */
6719 }
6720
6721 /*
6722 * We have to make a first pass through to calculate the size of
6723 * the pages that match the user's query. Then we allocate enough
6724 * memory to hold it, and actually copy the data into the buffer.
6725 */
6726 switch (page_code) {
6727 case SMS_ALL_PAGES_PAGE: {
6728 int i;
6729
6730 page_len = 0;
6731
6732 /*
6733 * At the moment, values other than 0 and 0xff here are
6734 * reserved according to SPC-3.
6735 */
6736 if ((subpage != SMS_SUBPAGE_PAGE_0)
6737 && (subpage != SMS_SUBPAGE_ALL)) {
6738 ctl_set_invalid_field(ctsio,
6739 /*sks_valid*/ 1,
6740 /*command*/ 1,
6741 /*field*/ 3,
6742 /*bit_valid*/ 0,
6743 /*bit*/ 0);
6744 ctl_done((union ctl_io *)ctsio);
6745 return (CTL_RETVAL_COMPLETE);
6746 }
6747
6748 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6749 if ((control_dev != 0)
6750 && (lun->mode_pages.index[i].page_flags &
6751 CTL_PAGE_FLAG_DISK_ONLY))
6752 continue;
6753
6754 /*
6755 * We don't use this subpage if the user didn't
6756 * request all subpages.
6757 */
6758 if ((lun->mode_pages.index[i].subpage != 0)
6759 && (subpage == SMS_SUBPAGE_PAGE_0))
6760 continue;
6761
6762#if 0
6763 printf("found page %#x len %d\n",
6764 lun->mode_pages.index[i].page_code &
6765 SMPH_PC_MASK,
6766 lun->mode_pages.index[i].page_len);
6767#endif
6768 page_len += lun->mode_pages.index[i].page_len;
6769 }
6770 break;
6771 }
6772 default: {
6773 int i;
6774
6775 page_len = 0;
6776
6777 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6778 /* Look for the right page code */
6779 if ((lun->mode_pages.index[i].page_code &
6780 SMPH_PC_MASK) != page_code)
6781 continue;
6782
6783 /* Look for the right subpage or the subpage wildcard*/
6784 if ((lun->mode_pages.index[i].subpage != subpage)
6785 && (subpage != SMS_SUBPAGE_ALL))
6786 continue;
6787
6788 /* Make sure the page is supported for this dev type */
6789 if ((control_dev != 0)
6790 && (lun->mode_pages.index[i].page_flags &
6791 CTL_PAGE_FLAG_DISK_ONLY))
6792 continue;
6793
6794#if 0
6795 printf("found page %#x len %d\n",
6796 lun->mode_pages.index[i].page_code &
6797 SMPH_PC_MASK,
6798 lun->mode_pages.index[i].page_len);
6799#endif
6800
6801 page_len += lun->mode_pages.index[i].page_len;
6802 }
6803
6804 if (page_len == 0) {
6805 ctl_set_invalid_field(ctsio,
6806 /*sks_valid*/ 1,
6807 /*command*/ 1,
6808 /*field*/ 2,
6809 /*bit_valid*/ 1,
6810 /*bit*/ 5);
6811 ctl_done((union ctl_io *)ctsio);
6812 return (CTL_RETVAL_COMPLETE);
6813 }
6814 break;
6815 }
6816 }
6817
6818 total_len = header_len + page_len;
6819#if 0
6820 printf("header_len = %d, page_len = %d, total_len = %d\n",
6821 header_len, page_len, total_len);
6822#endif
6823
6824 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6825 ctsio->kern_sg_entries = 0;
6826 ctsio->kern_data_resid = 0;
6827 ctsio->kern_rel_offset = 0;
6828 if (total_len < alloc_len) {
6829 ctsio->residual = alloc_len - total_len;
6830 ctsio->kern_data_len = total_len;
6831 ctsio->kern_total_len = total_len;
6832 } else {
6833 ctsio->residual = 0;
6834 ctsio->kern_data_len = alloc_len;
6835 ctsio->kern_total_len = alloc_len;
6836 }
6837
6838 switch (ctsio->cdb[0]) {
6839 case MODE_SENSE_6: {
6840 struct scsi_mode_hdr_6 *header;
6841
6842 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6843
6844 header->datalen = ctl_min(total_len - 1, 254);
6845
6846 if (dbd)
6847 header->block_descr_len = 0;
6848 else
6849 header->block_descr_len =
6850 sizeof(struct scsi_mode_block_descr);
6851 block_desc = (struct scsi_mode_block_descr *)&header[1];
6852 break;
6853 }
6854 case MODE_SENSE_10: {
6855 struct scsi_mode_hdr_10 *header;
6856 int datalen;
6857
6858 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6859
6860 datalen = ctl_min(total_len - 2, 65533);
6861 scsi_ulto2b(datalen, header->datalen);
6862 if (dbd)
6863 scsi_ulto2b(0, header->block_descr_len);
6864 else
6865 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6866 header->block_descr_len);
6867 block_desc = (struct scsi_mode_block_descr *)&header[1];
6868 break;
6869 }
6870 default:
6871 panic("invalid CDB type %#x", ctsio->cdb[0]);
6872 break; /* NOTREACHED */
6873 }
6874
6875 /*
6876 * If we've got a disk, use its blocksize in the block
6877 * descriptor. Otherwise, just set it to 0.
6878 */
6879 if (dbd == 0) {
6880 if (control_dev != 0)
6881 scsi_ulto3b(lun->be_lun->blocksize,
6882 block_desc->block_len);
6883 else
6884 scsi_ulto3b(0, block_desc->block_len);
6885 }
6886
6887 switch (page_code) {
6888 case SMS_ALL_PAGES_PAGE: {
6889 int i, data_used;
6890
6891 data_used = header_len;
6892 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6893 struct ctl_page_index *page_index;
6894
6895 page_index = &lun->mode_pages.index[i];
6896
6897 if ((control_dev != 0)
6898 && (page_index->page_flags &
6899 CTL_PAGE_FLAG_DISK_ONLY))
6900 continue;
6901
6902 /*
6903 * We don't use this subpage if the user didn't
6904 * request all subpages. We already checked (above)
6905 * to make sure the user only specified a subpage
6906 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6907 */
6908 if ((page_index->subpage != 0)
6909 && (subpage == SMS_SUBPAGE_PAGE_0))
6910 continue;
6911
6912 /*
6913 * Call the handler, if it exists, to update the
6914 * page to the latest values.
6915 */
6916 if (page_index->sense_handler != NULL)
6917 page_index->sense_handler(ctsio, page_index,pc);
6918
6919 memcpy(ctsio->kern_data_ptr + data_used,
6920 page_index->page_data +
6921 (page_index->page_len * pc),
6922 page_index->page_len);
6923 data_used += page_index->page_len;
6924 }
6925 break;
6926 }
6927 default: {
6928 int i, data_used;
6929
6930 data_used = header_len;
6931
6932 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6933 struct ctl_page_index *page_index;
6934
6935 page_index = &lun->mode_pages.index[i];
6936
6937 /* Look for the right page code */
6938 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6939 continue;
6940
6941 /* Look for the right subpage or the subpage wildcard*/
6942 if ((page_index->subpage != subpage)
6943 && (subpage != SMS_SUBPAGE_ALL))
6944 continue;
6945
6946 /* Make sure the page is supported for this dev type */
6947 if ((control_dev != 0)
6948 && (page_index->page_flags &
6949 CTL_PAGE_FLAG_DISK_ONLY))
6950 continue;
6951
6952 /*
6953 * Call the handler, if it exists, to update the
6954 * page to the latest values.
6955 */
6956 if (page_index->sense_handler != NULL)
6957 page_index->sense_handler(ctsio, page_index,pc);
6958
6959 memcpy(ctsio->kern_data_ptr + data_used,
6960 page_index->page_data +
6961 (page_index->page_len * pc),
6962 page_index->page_len);
6963 data_used += page_index->page_len;
6964 }
6965 break;
6966 }
6967 }
6968
6969 ctsio->scsi_status = SCSI_STATUS_OK;
6970
6971 ctsio->be_move_done = ctl_config_move_done;
6972 ctl_datamove((union ctl_io *)ctsio);
6973
6974 return (CTL_RETVAL_COMPLETE);
6975}
6976
6977int
6978ctl_read_capacity(struct ctl_scsiio *ctsio)
6979{
6980 struct scsi_read_capacity *cdb;
6981 struct scsi_read_capacity_data *data;
6982 struct ctl_lun *lun;
6983 uint32_t lba;
6984
6985 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6986
6987 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6988
6989 lba = scsi_4btoul(cdb->addr);
6990 if (((cdb->pmi & SRC_PMI) == 0)
6991 && (lba != 0)) {
6992 ctl_set_invalid_field(/*ctsio*/ ctsio,
6993 /*sks_valid*/ 1,
6994 /*command*/ 1,
6995 /*field*/ 2,
6996 /*bit_valid*/ 0,
6997 /*bit*/ 0);
6998 ctl_done((union ctl_io *)ctsio);
6999 return (CTL_RETVAL_COMPLETE);
7000 }
7001
7002 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7003
7004 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7005 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
7006 ctsio->residual = 0;
7007 ctsio->kern_data_len = sizeof(*data);
7008 ctsio->kern_total_len = sizeof(*data);
7009 ctsio->kern_data_resid = 0;
7010 ctsio->kern_rel_offset = 0;
7011 ctsio->kern_sg_entries = 0;
7012
7013 /*
7014 * If the maximum LBA is greater than 0xfffffffe, the user must
7015 * issue a SERVICE ACTION IN (16) command, with the read capacity
7016 * serivce action set.
7017 */
7018 if (lun->be_lun->maxlba > 0xfffffffe)
7019 scsi_ulto4b(0xffffffff, data->addr);
7020 else
7021 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
7022
7023 /*
7024 * XXX KDM this may not be 512 bytes...
7025 */
7026 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7027
7028 ctsio->scsi_status = SCSI_STATUS_OK;
7029
7030 ctsio->be_move_done = ctl_config_move_done;
7031 ctl_datamove((union ctl_io *)ctsio);
7032
7033 return (CTL_RETVAL_COMPLETE);
7034}
7035
7036static int
7037ctl_read_capacity_16(struct ctl_scsiio *ctsio)
7038{
7039 struct scsi_read_capacity_16 *cdb;
7040 struct scsi_read_capacity_data_long *data;
7041 struct ctl_lun *lun;
7042 uint64_t lba;
7043 uint32_t alloc_len;
7044
7045 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7046
7047 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7048
7049 alloc_len = scsi_4btoul(cdb->alloc_len);
7050 lba = scsi_8btou64(cdb->addr);
7051
7052 if ((cdb->reladr & SRC16_PMI)
7053 && (lba != 0)) {
7054 ctl_set_invalid_field(/*ctsio*/ ctsio,
7055 /*sks_valid*/ 1,
7056 /*command*/ 1,
7057 /*field*/ 2,
7058 /*bit_valid*/ 0,
7059 /*bit*/ 0);
7060 ctl_done((union ctl_io *)ctsio);
7061 return (CTL_RETVAL_COMPLETE);
7062 }
7063
7064 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7065
7066 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7067 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7068
7069 if (sizeof(*data) < alloc_len) {
7070 ctsio->residual = alloc_len - sizeof(*data);
7071 ctsio->kern_data_len = sizeof(*data);
7072 ctsio->kern_total_len = sizeof(*data);
7073 } else {
7074 ctsio->residual = 0;
7075 ctsio->kern_data_len = alloc_len;
7076 ctsio->kern_total_len = alloc_len;
7077 }
7078 ctsio->kern_data_resid = 0;
7079 ctsio->kern_rel_offset = 0;
7080 ctsio->kern_sg_entries = 0;
7081
7082 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7083 /* XXX KDM this may not be 512 bytes... */
7084 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7085 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7086 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7087 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7088 data->lalba_lbp[0] |= SRC16_LBPME;
7089
7090 ctsio->scsi_status = SCSI_STATUS_OK;
7091
7092 ctsio->be_move_done = ctl_config_move_done;
7093 ctl_datamove((union ctl_io *)ctsio);
7094
7095 return (CTL_RETVAL_COMPLETE);
7096}
7097
7098int
7099ctl_service_action_in(struct ctl_scsiio *ctsio)
7100{
7101 struct scsi_service_action_in *cdb;
7102 int retval;
7103
7104 CTL_DEBUG_PRINT(("ctl_service_action_in\n"));
7105
7106 cdb = (struct scsi_service_action_in *)ctsio->cdb;
7107
7108 retval = CTL_RETVAL_COMPLETE;
7109
7110 switch (cdb->service_action) {
7111 case SRC16_SERVICE_ACTION:
7112 retval = ctl_read_capacity_16(ctsio);
7113 break;
7114 default:
7115 ctl_set_invalid_field(/*ctsio*/ ctsio,
7116 /*sks_valid*/ 1,
7117 /*command*/ 1,
7118 /*field*/ 1,
7119 /*bit_valid*/ 1,
7120 /*bit*/ 4);
7121 ctl_done((union ctl_io *)ctsio);
7122 break;
7123 }
7124
7125 return (retval);
7126}
7127
7128int
7129ctl_maintenance_in(struct ctl_scsiio *ctsio)
7130{
7131 struct scsi_maintenance_in *cdb;
7132 int retval;
7133 int alloc_len, total_len = 0;
7134 int num_target_port_groups, single;
7135 struct ctl_lun *lun;
7136 struct ctl_softc *softc;
7137 struct scsi_target_group_data *rtg_ptr;
7138 struct scsi_target_port_group_descriptor *tpg_desc_ptr1, *tpg_desc_ptr2;
7139 struct scsi_target_port_descriptor *tp_desc_ptr1_1, *tp_desc_ptr1_2,
7140 *tp_desc_ptr2_1, *tp_desc_ptr2_2;
7141
7142 CTL_DEBUG_PRINT(("ctl_maintenance_in\n"));
7143
7144 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7145 softc = control_softc;
7146 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7147
7148 retval = CTL_RETVAL_COMPLETE;
7149
7150 if ((cdb->byte2 & SERVICE_ACTION_MASK) != SA_RPRT_TRGT_GRP) {
7151 ctl_set_invalid_field(/*ctsio*/ ctsio,
7152 /*sks_valid*/ 1,
7153 /*command*/ 1,
7154 /*field*/ 1,
7155 /*bit_valid*/ 1,
7156 /*bit*/ 4);
7157 ctl_done((union ctl_io *)ctsio);
7158 return(retval);
7159 }
7160
7161 mtx_lock(&softc->ctl_lock);
7162 single = ctl_is_single;
7163 mtx_unlock(&softc->ctl_lock);
7164
7165 if (single)
7166 num_target_port_groups = NUM_TARGET_PORT_GROUPS - 1;
7167 else
7168 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
7169
7170 total_len = sizeof(struct scsi_target_group_data) +
7171 sizeof(struct scsi_target_port_group_descriptor) *
7172 num_target_port_groups +
7173 sizeof(struct scsi_target_port_descriptor) *
7174 NUM_PORTS_PER_GRP * num_target_port_groups;
7175
7176 alloc_len = scsi_4btoul(cdb->length);
7177
7178 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7179
7180 ctsio->kern_sg_entries = 0;
7181
7182 if (total_len < alloc_len) {
7183 ctsio->residual = alloc_len - total_len;
7184 ctsio->kern_data_len = total_len;
7185 ctsio->kern_total_len = total_len;
7186 } else {
7187 ctsio->residual = 0;
7188 ctsio->kern_data_len = alloc_len;
7189 ctsio->kern_total_len = alloc_len;
7190 }
7191 ctsio->kern_data_resid = 0;
7192 ctsio->kern_rel_offset = 0;
7193
7194 rtg_ptr = (struct scsi_target_group_data *)ctsio->kern_data_ptr;
7195
7196 tpg_desc_ptr1 = &rtg_ptr->groups[0];
7197 tp_desc_ptr1_1 = &tpg_desc_ptr1->descriptors[0];
7198 tp_desc_ptr1_2 = (struct scsi_target_port_descriptor *)
7199 &tp_desc_ptr1_1->desc_list[0];
7200
7201 if (single == 0) {
7202 tpg_desc_ptr2 = (struct scsi_target_port_group_descriptor *)
7203 &tp_desc_ptr1_2->desc_list[0];
7204 tp_desc_ptr2_1 = &tpg_desc_ptr2->descriptors[0];
7205 tp_desc_ptr2_2 = (struct scsi_target_port_descriptor *)
7206 &tp_desc_ptr2_1->desc_list[0];
7207 } else {
7208 tpg_desc_ptr2 = NULL;
7209 tp_desc_ptr2_1 = NULL;
7210 tp_desc_ptr2_2 = NULL;
7211 }
7212
7213 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7214 if (single == 0) {
7215 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) {
7216 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7217 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7218 tpg_desc_ptr2->pref_state =
7219 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7220 } else {
7221 tpg_desc_ptr1->pref_state =
7222 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7223 tpg_desc_ptr2->pref_state = TPG_PRIMARY;
7224 }
7225 } else {
7226 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7227 tpg_desc_ptr1->pref_state =
7228 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7229 tpg_desc_ptr2->pref_state = TPG_PRIMARY;
7230 } else {
7231 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7232 tpg_desc_ptr2->pref_state =
7233 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7234 }
7235 }
7236 } else {
7237 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7238 }
7239 tpg_desc_ptr1->support = 0;
7240 tpg_desc_ptr1->target_port_group[1] = 1;
7241 tpg_desc_ptr1->status = TPG_IMPLICIT;
7242 tpg_desc_ptr1->target_port_count= NUM_PORTS_PER_GRP;
7243
7244 if (single == 0) {
7245 tpg_desc_ptr2->support = 0;
7246 tpg_desc_ptr2->target_port_group[1] = 2;
7247 tpg_desc_ptr2->status = TPG_IMPLICIT;
7248 tpg_desc_ptr2->target_port_count = NUM_PORTS_PER_GRP;
7249
7250 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1;
7251 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2;
7252
7253 tp_desc_ptr2_1->relative_target_port_identifier[1] = 9;
7254 tp_desc_ptr2_2->relative_target_port_identifier[1] = 10;
7255 } else {
7256 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) {
7257 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1;
7258 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2;
7259 } else {
7260 tp_desc_ptr1_1->relative_target_port_identifier[1] = 9;
7261 tp_desc_ptr1_2->relative_target_port_identifier[1] = 10;
7262 }
7263 }
7264
7265 ctsio->be_move_done = ctl_config_move_done;
7266
7267 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7268 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7269 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7270 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7271 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7272
7273 ctl_datamove((union ctl_io *)ctsio);
7274 return(retval);
7275}
7276
7277int
7278ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7279{
7280 struct scsi_per_res_in *cdb;
7281 int alloc_len, total_len = 0;
7282 /* struct scsi_per_res_in_rsrv in_data; */
7283 struct ctl_lun *lun;
7284 struct ctl_softc *softc;
7285
7286 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7287
7288 softc = control_softc;
7289
7290 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7291
7292 alloc_len = scsi_2btoul(cdb->length);
7293
7294 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7295
7296retry:
7297 mtx_lock(&softc->ctl_lock);
7298 switch (cdb->action) {
7299 case SPRI_RK: /* read keys */
7300 total_len = sizeof(struct scsi_per_res_in_keys) +
7301 lun->pr_key_count *
7302 sizeof(struct scsi_per_res_key);
7303 break;
7304 case SPRI_RR: /* read reservation */
7305 if (lun->flags & CTL_LUN_PR_RESERVED)
7306 total_len = sizeof(struct scsi_per_res_in_rsrv);
7307 else
7308 total_len = sizeof(struct scsi_per_res_in_header);
7309 break;
7310 case SPRI_RC: /* report capabilities */
7311 total_len = sizeof(struct scsi_per_res_cap);
7312 break;
7313 case SPRI_RS: /* read full status */
7314 default:
7315 mtx_unlock(&softc->ctl_lock);
7316 ctl_set_invalid_field(ctsio,
7317 /*sks_valid*/ 1,
7318 /*command*/ 1,
7319 /*field*/ 1,
7320 /*bit_valid*/ 1,
7321 /*bit*/ 0);
7322 ctl_done((union ctl_io *)ctsio);
7323 return (CTL_RETVAL_COMPLETE);
7324 break; /* NOTREACHED */
7325 }
7326 mtx_unlock(&softc->ctl_lock);
7327
7328 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7329
7330 if (total_len < alloc_len) {
7331 ctsio->residual = alloc_len - total_len;
7332 ctsio->kern_data_len = total_len;
7333 ctsio->kern_total_len = total_len;
7334 } else {
7335 ctsio->residual = 0;
7336 ctsio->kern_data_len = alloc_len;
7337 ctsio->kern_total_len = alloc_len;
7338 }
7339
7340 ctsio->kern_data_resid = 0;
7341 ctsio->kern_rel_offset = 0;
7342 ctsio->kern_sg_entries = 0;
7343
7344 mtx_lock(&softc->ctl_lock);
7345 switch (cdb->action) {
7346 case SPRI_RK: { // read keys
7347 struct scsi_per_res_in_keys *res_keys;
7348 int i, key_count;
7349
7350 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7351
7352 /*
7353 * We had to drop the lock to allocate our buffer, which
7354 * leaves time for someone to come in with another
7355 * persistent reservation. (That is unlikely, though,
7356 * since this should be the only persistent reservation
7357 * command active right now.)
7358 */
7359 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7360 (lun->pr_key_count *
7361 sizeof(struct scsi_per_res_key)))){
7362 mtx_unlock(&softc->ctl_lock);
7363 free(ctsio->kern_data_ptr, M_CTL);
7364 printf("%s: reservation length changed, retrying\n",
7365 __func__);
7366 goto retry;
7367 }
7368
7369 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7370
7371 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7372 lun->pr_key_count, res_keys->header.length);
7373
7374 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7375 if (!lun->per_res[i].registered)
7376 continue;
7377
7378 /*
7379 * We used lun->pr_key_count to calculate the
7380 * size to allocate. If it turns out the number of
7381 * initiators with the registered flag set is
7382 * larger than that (i.e. they haven't been kept in
7383 * sync), we've got a problem.
7384 */
7385 if (key_count >= lun->pr_key_count) {
7386#ifdef NEEDTOPORT
7387 csevent_log(CSC_CTL | CSC_SHELF_SW |
7388 CTL_PR_ERROR,
7389 csevent_LogType_Fault,
7390 csevent_AlertLevel_Yellow,
7391 csevent_FRU_ShelfController,
7392 csevent_FRU_Firmware,
7393 csevent_FRU_Unknown,
7394 "registered keys %d >= key "
7395 "count %d", key_count,
7396 lun->pr_key_count);
7397#endif
7398 key_count++;
7399 continue;
7400 }
7401 memcpy(res_keys->keys[key_count].key,
7402 lun->per_res[i].res_key.key,
7403 ctl_min(sizeof(res_keys->keys[key_count].key),
7404 sizeof(lun->per_res[i].res_key)));
7405 key_count++;
7406 }
7407 break;
7408 }
7409 case SPRI_RR: { // read reservation
7410 struct scsi_per_res_in_rsrv *res;
7411 int tmp_len, header_only;
7412
7413 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7414
7415 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7416
7417 if (lun->flags & CTL_LUN_PR_RESERVED)
7418 {
7419 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7420 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7421 res->header.length);
7422 header_only = 0;
7423 } else {
7424 tmp_len = sizeof(struct scsi_per_res_in_header);
7425 scsi_ulto4b(0, res->header.length);
7426 header_only = 1;
7427 }
7428
7429 /*
7430 * We had to drop the lock to allocate our buffer, which
7431 * leaves time for someone to come in with another
7432 * persistent reservation. (That is unlikely, though,
7433 * since this should be the only persistent reservation
7434 * command active right now.)
7435 */
7436 if (tmp_len != total_len) {
7437 mtx_unlock(&softc->ctl_lock);
7438 free(ctsio->kern_data_ptr, M_CTL);
7439 printf("%s: reservation status changed, retrying\n",
7440 __func__);
7441 goto retry;
7442 }
7443
7444 /*
7445 * No reservation held, so we're done.
7446 */
7447 if (header_only != 0)
7448 break;
7449
7450 /*
7451 * If the registration is an All Registrants type, the key
7452 * is 0, since it doesn't really matter.
7453 */
7454 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7455 memcpy(res->data.reservation,
7456 &lun->per_res[lun->pr_res_idx].res_key,
7457 sizeof(struct scsi_per_res_key));
7458 }
7459 res->data.scopetype = lun->res_type;
7460 break;
7461 }
7462 case SPRI_RC: //report capabilities
7463 {
7464 struct scsi_per_res_cap *res_cap;
7465 uint16_t type_mask;
7466
7467 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7468 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7469 res_cap->flags2 |= SPRI_TMV;
7470 type_mask = SPRI_TM_WR_EX_AR |
7471 SPRI_TM_EX_AC_RO |
7472 SPRI_TM_WR_EX_RO |
7473 SPRI_TM_EX_AC |
7474 SPRI_TM_WR_EX |
7475 SPRI_TM_EX_AC_AR;
7476 scsi_ulto2b(type_mask, res_cap->type_mask);
7477 break;
7478 }
7479 case SPRI_RS: //read full status
7480 default:
7481 /*
7482 * This is a bug, because we just checked for this above,
7483 * and should have returned an error.
7484 */
7485 panic("Invalid PR type %x", cdb->action);
7486 break; /* NOTREACHED */
7487 }
7488 mtx_unlock(&softc->ctl_lock);
7489
7490 ctsio->be_move_done = ctl_config_move_done;
7491
7492 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7493 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7494 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7495 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7496 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7497
7498 ctl_datamove((union ctl_io *)ctsio);
7499
7500 return (CTL_RETVAL_COMPLETE);
7501}
7502
7503/*
7504 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7505 * it should return.
7506 */
7507static int
7508ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7509 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7510 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7511 struct scsi_per_res_out_parms* param)
7512{
7513 union ctl_ha_msg persis_io;
7514 int retval, i;
7515 int isc_retval;
7516
7517 retval = 0;
7518
7519 if (sa_res_key == 0) {
7520 mtx_lock(&softc->ctl_lock);
7521 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7522 /* validate scope and type */
7523 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7524 SPR_LU_SCOPE) {
7525 mtx_unlock(&softc->ctl_lock);
7526 ctl_set_invalid_field(/*ctsio*/ ctsio,
7527 /*sks_valid*/ 1,
7528 /*command*/ 1,
7529 /*field*/ 2,
7530 /*bit_valid*/ 1,
7531 /*bit*/ 4);
7532 ctl_done((union ctl_io *)ctsio);
7533 return (1);
7534 }
7535
7536 if (type>8 || type==2 || type==4 || type==0) {
7537 mtx_unlock(&softc->ctl_lock);
7538 ctl_set_invalid_field(/*ctsio*/ ctsio,
7539 /*sks_valid*/ 1,
7540 /*command*/ 1,
7541 /*field*/ 2,
7542 /*bit_valid*/ 1,
7543 /*bit*/ 0);
7544 ctl_done((union ctl_io *)ctsio);
7545 return (1);
7546 }
7547
7548 /* temporarily unregister this nexus */
7549 lun->per_res[residx].registered = 0;
7550
7551 /*
7552 * Unregister everybody else and build UA for
7553 * them
7554 */
7555 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7556 if (lun->per_res[i].registered == 0)
7557 continue;
7558
7559 if (!persis_offset
7560 && i <CTL_MAX_INITIATORS)
7561 lun->pending_sense[i].ua_pending |=
7562 CTL_UA_REG_PREEMPT;
7563 else if (persis_offset
7564 && i >= persis_offset)
7565 lun->pending_sense[i-persis_offset
7566 ].ua_pending |=
7567 CTL_UA_REG_PREEMPT;
7568 lun->per_res[i].registered = 0;
7569 memset(&lun->per_res[i].res_key, 0,
7570 sizeof(struct scsi_per_res_key));
7571 }
7572 lun->per_res[residx].registered = 1;
7573 lun->pr_key_count = 1;
7574 lun->res_type = type;
7575 if (lun->res_type != SPR_TYPE_WR_EX_AR
7576 && lun->res_type != SPR_TYPE_EX_AC_AR)
7577 lun->pr_res_idx = residx;
7578
7579 mtx_unlock(&softc->ctl_lock);
7580 /* send msg to other side */
7581 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7582 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7583 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7584 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7585 persis_io.pr.pr_info.res_type = type;
7586 memcpy(persis_io.pr.pr_info.sa_res_key,
7587 param->serv_act_res_key,
7588 sizeof(param->serv_act_res_key));
7589 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7590 &persis_io, sizeof(persis_io), 0)) >
7591 CTL_HA_STATUS_SUCCESS) {
7592 printf("CTL:Persis Out error returned "
7593 "from ctl_ha_msg_send %d\n",
7594 isc_retval);
7595 }
7596 } else {
7597 /* not all registrants */
7598 mtx_unlock(&softc->ctl_lock);
7599 free(ctsio->kern_data_ptr, M_CTL);
7600 ctl_set_invalid_field(ctsio,
7601 /*sks_valid*/ 1,
7602 /*command*/ 0,
7603 /*field*/ 8,
7604 /*bit_valid*/ 0,
7605 /*bit*/ 0);
7606 ctl_done((union ctl_io *)ctsio);
7607 return (1);
7608 }
7609 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7610 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7611 int found = 0;
7612
7613 mtx_lock(&softc->ctl_lock);
7614 if (res_key == sa_res_key) {
7615 /* special case */
7616 /*
7617 * The spec implies this is not good but doesn't
7618 * say what to do. There are two choices either
7619 * generate a res conflict or check condition
7620 * with illegal field in parameter data. Since
7621 * that is what is done when the sa_res_key is
7622 * zero I'll take that approach since this has
7623 * to do with the sa_res_key.
7624 */
7625 mtx_unlock(&softc->ctl_lock);
7626 free(ctsio->kern_data_ptr, M_CTL);
7627 ctl_set_invalid_field(ctsio,
7628 /*sks_valid*/ 1,
7629 /*command*/ 0,
7630 /*field*/ 8,
7631 /*bit_valid*/ 0,
7632 /*bit*/ 0);
7633 ctl_done((union ctl_io *)ctsio);
7634 return (1);
7635 }
7636
7637 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7638 if (lun->per_res[i].registered
7639 && memcmp(param->serv_act_res_key,
7640 lun->per_res[i].res_key.key,
7641 sizeof(struct scsi_per_res_key)) != 0)
7642 continue;
7643
7644 found = 1;
7645 lun->per_res[i].registered = 0;
7646 memset(&lun->per_res[i].res_key, 0,
7647 sizeof(struct scsi_per_res_key));
7648 lun->pr_key_count--;
7649
7650 if (!persis_offset
7651 && i < CTL_MAX_INITIATORS)
7652 lun->pending_sense[i].ua_pending |=
7653 CTL_UA_REG_PREEMPT;
7654 else if (persis_offset
7655 && i >= persis_offset)
7656 lun->pending_sense[i-persis_offset].ua_pending|=
7657 CTL_UA_REG_PREEMPT;
7658 }
7659 mtx_unlock(&softc->ctl_lock);
7660 if (!found) {
7661 free(ctsio->kern_data_ptr, M_CTL);
7662 ctl_set_reservation_conflict(ctsio);
7663 ctl_done((union ctl_io *)ctsio);
7664 return (CTL_RETVAL_COMPLETE);
7665 }
7666 /* send msg to other side */
7667 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7668 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7669 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7670 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7671 persis_io.pr.pr_info.res_type = type;
7672 memcpy(persis_io.pr.pr_info.sa_res_key,
7673 param->serv_act_res_key,
7674 sizeof(param->serv_act_res_key));
7675 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7676 &persis_io, sizeof(persis_io), 0)) >
7677 CTL_HA_STATUS_SUCCESS) {
7678 printf("CTL:Persis Out error returned from "
7679 "ctl_ha_msg_send %d\n", isc_retval);
7680 }
7681 } else {
7682 /* Reserved but not all registrants */
7683 /* sa_res_key is res holder */
7684 if (memcmp(param->serv_act_res_key,
7685 lun->per_res[lun->pr_res_idx].res_key.key,
7686 sizeof(struct scsi_per_res_key)) == 0) {
7687 /* validate scope and type */
7688 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7689 SPR_LU_SCOPE) {
7690 ctl_set_invalid_field(/*ctsio*/ ctsio,
7691 /*sks_valid*/ 1,
7692 /*command*/ 1,
7693 /*field*/ 2,
7694 /*bit_valid*/ 1,
7695 /*bit*/ 4);
7696 ctl_done((union ctl_io *)ctsio);
7697 return (1);
7698 }
7699
7700 if (type>8 || type==2 || type==4 || type==0) {
7701 ctl_set_invalid_field(/*ctsio*/ ctsio,
7702 /*sks_valid*/ 1,
7703 /*command*/ 1,
7704 /*field*/ 2,
7705 /*bit_valid*/ 1,
7706 /*bit*/ 0);
7707 ctl_done((union ctl_io *)ctsio);
7708 return (1);
7709 }
7710
7711 /*
7712 * Do the following:
7713 * if sa_res_key != res_key remove all
7714 * registrants w/sa_res_key and generate UA
7715 * for these registrants(Registrations
7716 * Preempted) if it wasn't an exclusive
7717 * reservation generate UA(Reservations
7718 * Preempted) for all other registered nexuses
7719 * if the type has changed. Establish the new
7720 * reservation and holder. If res_key and
7721 * sa_res_key are the same do the above
7722 * except don't unregister the res holder.
7723 */
7724
7725 /*
7726 * Temporarily unregister so it won't get
7727 * removed or UA generated
7728 */
7729 lun->per_res[residx].registered = 0;
7730 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7731 if (lun->per_res[i].registered == 0)
7732 continue;
7733
7734 if (memcmp(param->serv_act_res_key,
7735 lun->per_res[i].res_key.key,
7736 sizeof(struct scsi_per_res_key)) == 0) {
7737 lun->per_res[i].registered = 0;
7738 memset(&lun->per_res[i].res_key,
7739 0,
7740 sizeof(struct scsi_per_res_key));
7741 lun->pr_key_count--;
7742
7743 if (!persis_offset
7744 && i < CTL_MAX_INITIATORS)
7745 lun->pending_sense[i
7746 ].ua_pending |=
7747 CTL_UA_REG_PREEMPT;
7748 else if (persis_offset
7749 && i >= persis_offset)
7750 lun->pending_sense[
7751 i-persis_offset].ua_pending |=
7752 CTL_UA_REG_PREEMPT;
7753 } else if (type != lun->res_type
7754 && (lun->res_type == SPR_TYPE_WR_EX_RO
7755 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
7756 if (!persis_offset
7757 && i < CTL_MAX_INITIATORS)
7758 lun->pending_sense[i
7759 ].ua_pending |=
7760 CTL_UA_RES_RELEASE;
7761 else if (persis_offset
7762 && i >= persis_offset)
7763 lun->pending_sense[
7764 i-persis_offset
7765 ].ua_pending |=
7766 CTL_UA_RES_RELEASE;
7767 }
7768 }
7769 lun->per_res[residx].registered = 1;
7770 lun->res_type = type;
7771 if (lun->res_type != SPR_TYPE_WR_EX_AR
7772 && lun->res_type != SPR_TYPE_EX_AC_AR)
7773 lun->pr_res_idx = residx;
7774 else
7775 lun->pr_res_idx =
7776 CTL_PR_ALL_REGISTRANTS;
7777
7778 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7779 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7780 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7781 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7782 persis_io.pr.pr_info.res_type = type;
7783 memcpy(persis_io.pr.pr_info.sa_res_key,
7784 param->serv_act_res_key,
7785 sizeof(param->serv_act_res_key));
7786 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7787 &persis_io, sizeof(persis_io), 0)) >
7788 CTL_HA_STATUS_SUCCESS) {
7789 printf("CTL:Persis Out error returned "
7790 "from ctl_ha_msg_send %d\n",
7791 isc_retval);
7792 }
7793 } else {
7794 /*
7795 * sa_res_key is not the res holder just
7796 * remove registrants
7797 */
7798 int found=0;
7799 mtx_lock(&softc->ctl_lock);
7800
7801 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7802 if (memcmp(param->serv_act_res_key,
7803 lun->per_res[i].res_key.key,
7804 sizeof(struct scsi_per_res_key)) != 0)
7805 continue;
7806
7807 found = 1;
7808 lun->per_res[i].registered = 0;
7809 memset(&lun->per_res[i].res_key, 0,
7810 sizeof(struct scsi_per_res_key));
7811 lun->pr_key_count--;
7812
7813 if (!persis_offset
7814 && i < CTL_MAX_INITIATORS)
7815 lun->pending_sense[i].ua_pending |=
7816 CTL_UA_REG_PREEMPT;
7817 else if (persis_offset
7818 && i >= persis_offset)
7819 lun->pending_sense[
7820 i-persis_offset].ua_pending |=
7821 CTL_UA_REG_PREEMPT;
7822 }
7823
7824 if (!found) {
7825 mtx_unlock(&softc->ctl_lock);
7826 free(ctsio->kern_data_ptr, M_CTL);
7827 ctl_set_reservation_conflict(ctsio);
7828 ctl_done((union ctl_io *)ctsio);
7829 return (1);
7830 }
7831 mtx_unlock(&softc->ctl_lock);
7832 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7833 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7834 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7835 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7836 persis_io.pr.pr_info.res_type = type;
7837 memcpy(persis_io.pr.pr_info.sa_res_key,
7838 param->serv_act_res_key,
7839 sizeof(param->serv_act_res_key));
7840 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7841 &persis_io, sizeof(persis_io), 0)) >
7842 CTL_HA_STATUS_SUCCESS) {
7843 printf("CTL:Persis Out error returned "
7844 "from ctl_ha_msg_send %d\n",
7845 isc_retval);
7846 }
7847 }
7848 }
7849
7850 lun->PRGeneration++;
7851
7852 return (retval);
7853}
7854
7855static void
7856ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
7857{
7858 int i;
7859
7860 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7861 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
7862 || memcmp(&lun->per_res[lun->pr_res_idx].res_key,
7863 msg->pr.pr_info.sa_res_key,
7864 sizeof(struct scsi_per_res_key)) != 0) {
7865 uint64_t sa_res_key;
7866 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
7867
7868 if (sa_res_key == 0) {
7869 /* temporarily unregister this nexus */
7870 lun->per_res[msg->pr.pr_info.residx].registered = 0;
7871
7872 /*
7873 * Unregister everybody else and build UA for
7874 * them
7875 */
7876 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7877 if (lun->per_res[i].registered == 0)
7878 continue;
7879
7880 if (!persis_offset
7881 && i < CTL_MAX_INITIATORS)
7882 lun->pending_sense[i].ua_pending |=
7883 CTL_UA_REG_PREEMPT;
7884 else if (persis_offset && i >= persis_offset)
7885 lun->pending_sense[i -
7886 persis_offset].ua_pending |=
7887 CTL_UA_REG_PREEMPT;
7888 lun->per_res[i].registered = 0;
7889 memset(&lun->per_res[i].res_key, 0,
7890 sizeof(struct scsi_per_res_key));
7891 }
7892
7893 lun->per_res[msg->pr.pr_info.residx].registered = 1;
7894 lun->pr_key_count = 1;
7895 lun->res_type = msg->pr.pr_info.res_type;
7896 if (lun->res_type != SPR_TYPE_WR_EX_AR
7897 && lun->res_type != SPR_TYPE_EX_AC_AR)
7898 lun->pr_res_idx = msg->pr.pr_info.residx;
7899 } else {
7900 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7901 if (memcmp(msg->pr.pr_info.sa_res_key,
7902 lun->per_res[i].res_key.key,
7903 sizeof(struct scsi_per_res_key)) != 0)
7904 continue;
7905
7906 lun->per_res[i].registered = 0;
7907 memset(&lun->per_res[i].res_key, 0,
7908 sizeof(struct scsi_per_res_key));
7909 lun->pr_key_count--;
7910
7911 if (!persis_offset
7912 && i < persis_offset)
7913 lun->pending_sense[i].ua_pending |=
7914 CTL_UA_REG_PREEMPT;
7915 else if (persis_offset
7916 && i >= persis_offset)
7917 lun->pending_sense[i -
7918 persis_offset].ua_pending |=
7919 CTL_UA_REG_PREEMPT;
7920 }
7921 }
7922 } else {
7923 /*
7924 * Temporarily unregister so it won't get removed
7925 * or UA generated
7926 */
7927 lun->per_res[msg->pr.pr_info.residx].registered = 0;
7928 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7929 if (lun->per_res[i].registered == 0)
7930 continue;
7931
7932 if (memcmp(msg->pr.pr_info.sa_res_key,
7933 lun->per_res[i].res_key.key,
7934 sizeof(struct scsi_per_res_key)) == 0) {
7935 lun->per_res[i].registered = 0;
7936 memset(&lun->per_res[i].res_key, 0,
7937 sizeof(struct scsi_per_res_key));
7938 lun->pr_key_count--;
7939 if (!persis_offset
7940 && i < CTL_MAX_INITIATORS)
7941 lun->pending_sense[i].ua_pending |=
7942 CTL_UA_REG_PREEMPT;
7943 else if (persis_offset
7944 && i >= persis_offset)
7945 lun->pending_sense[i -
7946 persis_offset].ua_pending |=
7947 CTL_UA_REG_PREEMPT;
7948 } else if (msg->pr.pr_info.res_type != lun->res_type
7949 && (lun->res_type == SPR_TYPE_WR_EX_RO
7950 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
7951 if (!persis_offset
7952 && i < persis_offset)
7953 lun->pending_sense[i
7954 ].ua_pending |=
7955 CTL_UA_RES_RELEASE;
7956 else if (persis_offset
7957 && i >= persis_offset)
7958 lun->pending_sense[i -
7959 persis_offset].ua_pending |=
7960 CTL_UA_RES_RELEASE;
7961 }
7962 }
7963 lun->per_res[msg->pr.pr_info.residx].registered = 1;
7964 lun->res_type = msg->pr.pr_info.res_type;
7965 if (lun->res_type != SPR_TYPE_WR_EX_AR
7966 && lun->res_type != SPR_TYPE_EX_AC_AR)
7967 lun->pr_res_idx = msg->pr.pr_info.residx;
7968 else
7969 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
7970 }
7971 lun->PRGeneration++;
7972
7973}
7974
7975
7976int
7977ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
7978{
7979 int retval;
7980 int isc_retval;
7981 u_int32_t param_len;
7982 struct scsi_per_res_out *cdb;
7983 struct ctl_lun *lun;
7984 struct scsi_per_res_out_parms* param;
7985 struct ctl_softc *softc;
7986 uint32_t residx;
7987 uint64_t res_key, sa_res_key;
7988 uint8_t type;
7989 union ctl_ha_msg persis_io;
7990 int i;
7991
7992 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
7993
7994 retval = CTL_RETVAL_COMPLETE;
7995
7996 softc = control_softc;
7997
7998 cdb = (struct scsi_per_res_out *)ctsio->cdb;
7999 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8000
8001 /*
8002 * We only support whole-LUN scope. The scope & type are ignored for
8003 * register, register and ignore existing key and clear.
8004 * We sometimes ignore scope and type on preempts too!!
8005 * Verify reservation type here as well.
8006 */
8007 type = cdb->scope_type & SPR_TYPE_MASK;
8008 if ((cdb->action == SPRO_RESERVE)
8009 || (cdb->action == SPRO_RELEASE)) {
8010 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8011 ctl_set_invalid_field(/*ctsio*/ ctsio,
8012 /*sks_valid*/ 1,
8013 /*command*/ 1,
8014 /*field*/ 2,
8015 /*bit_valid*/ 1,
8016 /*bit*/ 4);
8017 ctl_done((union ctl_io *)ctsio);
8018 return (CTL_RETVAL_COMPLETE);
8019 }
8020
8021 if (type>8 || type==2 || type==4 || type==0) {
8022 ctl_set_invalid_field(/*ctsio*/ ctsio,
8023 /*sks_valid*/ 1,
8024 /*command*/ 1,
8025 /*field*/ 2,
8026 /*bit_valid*/ 1,
8027 /*bit*/ 0);
8028 ctl_done((union ctl_io *)ctsio);
8029 return (CTL_RETVAL_COMPLETE);
8030 }
8031 }
8032
8033 switch (cdb->action & SPRO_ACTION_MASK) {
8034 case SPRO_REGISTER:
8035 case SPRO_RESERVE:
8036 case SPRO_RELEASE:
8037 case SPRO_CLEAR:
8038 case SPRO_PREEMPT:
8039 case SPRO_REG_IGNO:
8040 break;
8041 case SPRO_REG_MOVE:
8042 case SPRO_PRE_ABO:
8043 default:
8044 ctl_set_invalid_field(/*ctsio*/ ctsio,
8045 /*sks_valid*/ 1,
8046 /*command*/ 1,
8047 /*field*/ 1,
8048 /*bit_valid*/ 1,
8049 /*bit*/ 0);
8050 ctl_done((union ctl_io *)ctsio);
8051 return (CTL_RETVAL_COMPLETE);
8052 break; /* NOTREACHED */
8053 }
8054
8055 param_len = scsi_4btoul(cdb->length);
8056
8057 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8058 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8059 ctsio->kern_data_len = param_len;
8060 ctsio->kern_total_len = param_len;
8061 ctsio->kern_data_resid = 0;
8062 ctsio->kern_rel_offset = 0;
8063 ctsio->kern_sg_entries = 0;
8064 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8065 ctsio->be_move_done = ctl_config_move_done;
8066 ctl_datamove((union ctl_io *)ctsio);
8067
8068 return (CTL_RETVAL_COMPLETE);
8069 }
8070
8071 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8072
8073 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8074 res_key = scsi_8btou64(param->res_key.key);
8075 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8076
8077 /*
8078 * Validate the reservation key here except for SPRO_REG_IGNO
8079 * This must be done for all other service actions
8080 */
8081 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8082 mtx_lock(&softc->ctl_lock);
8083 if (lun->per_res[residx].registered) {
8084 if (memcmp(param->res_key.key,
8085 lun->per_res[residx].res_key.key,
8086 ctl_min(sizeof(param->res_key),
8087 sizeof(lun->per_res[residx].res_key))) != 0) {
8088 /*
8089 * The current key passed in doesn't match
8090 * the one the initiator previously
8091 * registered.
8092 */
8093 mtx_unlock(&softc->ctl_lock);
8094 free(ctsio->kern_data_ptr, M_CTL);
8095 ctl_set_reservation_conflict(ctsio);
8096 ctl_done((union ctl_io *)ctsio);
8097 return (CTL_RETVAL_COMPLETE);
8098 }
8099 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8100 /*
8101 * We are not registered
8102 */
8103 mtx_unlock(&softc->ctl_lock);
8104 free(ctsio->kern_data_ptr, M_CTL);
8105 ctl_set_reservation_conflict(ctsio);
8106 ctl_done((union ctl_io *)ctsio);
8107 return (CTL_RETVAL_COMPLETE);
8108 } else if (res_key != 0) {
8109 /*
8110 * We are not registered and trying to register but
8111 * the register key isn't zero.
8112 */
8113 mtx_unlock(&softc->ctl_lock);
8114 free(ctsio->kern_data_ptr, M_CTL);
8115 ctl_set_reservation_conflict(ctsio);
8116 ctl_done((union ctl_io *)ctsio);
8117 return (CTL_RETVAL_COMPLETE);
8118 }
8119 mtx_unlock(&softc->ctl_lock);
8120 }
8121
8122 switch (cdb->action & SPRO_ACTION_MASK) {
8123 case SPRO_REGISTER:
8124 case SPRO_REG_IGNO: {
8125
8126#if 0
8127 printf("Registration received\n");
8128#endif
8129
8130 /*
8131 * We don't support any of these options, as we report in
8132 * the read capabilities request (see
8133 * ctl_persistent_reserve_in(), above).
8134 */
8135 if ((param->flags & SPR_SPEC_I_PT)
8136 || (param->flags & SPR_ALL_TG_PT)
8137 || (param->flags & SPR_APTPL)) {
8138 int bit_ptr;
8139
8140 if (param->flags & SPR_APTPL)
8141 bit_ptr = 0;
8142 else if (param->flags & SPR_ALL_TG_PT)
8143 bit_ptr = 2;
8144 else /* SPR_SPEC_I_PT */
8145 bit_ptr = 3;
8146
8147 free(ctsio->kern_data_ptr, M_CTL);
8148 ctl_set_invalid_field(ctsio,
8149 /*sks_valid*/ 1,
8150 /*command*/ 0,
8151 /*field*/ 20,
8152 /*bit_valid*/ 1,
8153 /*bit*/ bit_ptr);
8154 ctl_done((union ctl_io *)ctsio);
8155 return (CTL_RETVAL_COMPLETE);
8156 }
8157
8158 mtx_lock(&softc->ctl_lock);
8159
8160 /*
8161 * The initiator wants to clear the
8162 * key/unregister.
8163 */
8164 if (sa_res_key == 0) {
8165 if ((res_key == 0
8166 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8167 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8168 && !lun->per_res[residx].registered)) {
8169 mtx_unlock(&softc->ctl_lock);
8170 goto done;
8171 }
8172
8173 lun->per_res[residx].registered = 0;
8174 memset(&lun->per_res[residx].res_key,
8175 0, sizeof(lun->per_res[residx].res_key));
8176 lun->pr_key_count--;
8177
8178 if (residx == lun->pr_res_idx) {
8179 lun->flags &= ~CTL_LUN_PR_RESERVED;
8180 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8181
8182 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8183 || lun->res_type == SPR_TYPE_EX_AC_RO)
8184 && lun->pr_key_count) {
8185 /*
8186 * If the reservation is a registrants
8187 * only type we need to generate a UA
8188 * for other registered inits. The
8189 * sense code should be RESERVATIONS
8190 * RELEASED
8191 */
8192
8193 for (i = 0; i < CTL_MAX_INITIATORS;i++){
8194 if (lun->per_res[
8195 i+persis_offset].registered
8196 == 0)
8197 continue;
8198 lun->pending_sense[i
8199 ].ua_pending |=
8200 CTL_UA_RES_RELEASE;
8201 }
8202 }
8203 lun->res_type = 0;
8204 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8205 if (lun->pr_key_count==0) {
8206 lun->flags &= ~CTL_LUN_PR_RESERVED;
8207 lun->res_type = 0;
8208 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8209 }
8210 }
8211 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8212 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8213 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8214 persis_io.pr.pr_info.residx = residx;
8215 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8216 &persis_io, sizeof(persis_io), 0 )) >
8217 CTL_HA_STATUS_SUCCESS) {
8218 printf("CTL:Persis Out error returned from "
8219 "ctl_ha_msg_send %d\n", isc_retval);
8220 }
8221 mtx_unlock(&softc->ctl_lock);
8222 } else /* sa_res_key != 0 */ {
8223
8224 /*
8225 * If we aren't registered currently then increment
8226 * the key count and set the registered flag.
8227 */
8228 if (!lun->per_res[residx].registered) {
8229 lun->pr_key_count++;
8230 lun->per_res[residx].registered = 1;
8231 }
8232
8233 memcpy(&lun->per_res[residx].res_key,
8234 param->serv_act_res_key,
8235 ctl_min(sizeof(param->serv_act_res_key),
8236 sizeof(lun->per_res[residx].res_key)));
8237
8238 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8239 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8240 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8241 persis_io.pr.pr_info.residx = residx;
8242 memcpy(persis_io.pr.pr_info.sa_res_key,
8243 param->serv_act_res_key,
8244 sizeof(param->serv_act_res_key));
8245 mtx_unlock(&softc->ctl_lock);
8246 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8247 &persis_io, sizeof(persis_io), 0)) >
8248 CTL_HA_STATUS_SUCCESS) {
8249 printf("CTL:Persis Out error returned from "
8250 "ctl_ha_msg_send %d\n", isc_retval);
8251 }
8252 }
8253 lun->PRGeneration++;
8254
8255 break;
8256 }
8257 case SPRO_RESERVE:
8258#if 0
8259 printf("Reserve executed type %d\n", type);
8260#endif
8261 mtx_lock(&softc->ctl_lock);
8262 if (lun->flags & CTL_LUN_PR_RESERVED) {
8263 /*
8264 * if this isn't the reservation holder and it's
8265 * not a "all registrants" type or if the type is
8266 * different then we have a conflict
8267 */
8268 if ((lun->pr_res_idx != residx
8269 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8270 || lun->res_type != type) {
8271 mtx_unlock(&softc->ctl_lock);
8272 free(ctsio->kern_data_ptr, M_CTL);
8273 ctl_set_reservation_conflict(ctsio);
8274 ctl_done((union ctl_io *)ctsio);
8275 return (CTL_RETVAL_COMPLETE);
8276 }
8277 mtx_unlock(&softc->ctl_lock);
8278 } else /* create a reservation */ {
8279 /*
8280 * If it's not an "all registrants" type record
8281 * reservation holder
8282 */
8283 if (type != SPR_TYPE_WR_EX_AR
8284 && type != SPR_TYPE_EX_AC_AR)
8285 lun->pr_res_idx = residx; /* Res holder */
8286 else
8287 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8288
8289 lun->flags |= CTL_LUN_PR_RESERVED;
8290 lun->res_type = type;
8291
8292 mtx_unlock(&softc->ctl_lock);
8293
8294 /* send msg to other side */
8295 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8296 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8297 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8298 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8299 persis_io.pr.pr_info.res_type = type;
8300 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8301 &persis_io, sizeof(persis_io), 0)) >
8302 CTL_HA_STATUS_SUCCESS) {
8303 printf("CTL:Persis Out error returned from "
8304 "ctl_ha_msg_send %d\n", isc_retval);
8305 }
8306 }
8307 break;
8308
8309 case SPRO_RELEASE:
8310 mtx_lock(&softc->ctl_lock);
8311 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8312 /* No reservation exists return good status */
8313 mtx_unlock(&softc->ctl_lock);
8314 goto done;
8315 }
8316 /*
8317 * Is this nexus a reservation holder?
8318 */
8319 if (lun->pr_res_idx != residx
8320 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8321 /*
8322 * not a res holder return good status but
8323 * do nothing
8324 */
8325 mtx_unlock(&softc->ctl_lock);
8326 goto done;
8327 }
8328
8329 if (lun->res_type != type) {
8330 mtx_unlock(&softc->ctl_lock);
8331 free(ctsio->kern_data_ptr, M_CTL);
8332 ctl_set_illegal_pr_release(ctsio);
8333 ctl_done((union ctl_io *)ctsio);
8334 return (CTL_RETVAL_COMPLETE);
8335 }
8336
8337 /* okay to release */
8338 lun->flags &= ~CTL_LUN_PR_RESERVED;
8339 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8340 lun->res_type = 0;
8341
8342 /*
8343 * if this isn't an exclusive access
8344 * res generate UA for all other
8345 * registrants.
8346 */
8347 if (type != SPR_TYPE_EX_AC
8348 && type != SPR_TYPE_WR_EX) {
8349 /*
8350 * temporarily unregister so we don't generate UA
8351 */
8352 lun->per_res[residx].registered = 0;
8353
8354 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8355 if (lun->per_res[i+persis_offset].registered
8356 == 0)
8357 continue;
8358 lun->pending_sense[i].ua_pending |=
8359 CTL_UA_RES_RELEASE;
8360 }
8361
8362 lun->per_res[residx].registered = 1;
8363 }
8364 mtx_unlock(&softc->ctl_lock);
8365 /* Send msg to other side */
8366 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8367 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8368 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8369 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io,
8370 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8371 printf("CTL:Persis Out error returned from "
8372 "ctl_ha_msg_send %d\n", isc_retval);
8373 }
8374 break;
8375
8376 case SPRO_CLEAR:
8377 /* send msg to other side */
8378
8379 mtx_lock(&softc->ctl_lock);
8380 lun->flags &= ~CTL_LUN_PR_RESERVED;
8381 lun->res_type = 0;
8382 lun->pr_key_count = 0;
8383 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8384
8385
8386 memset(&lun->per_res[residx].res_key,
8387 0, sizeof(lun->per_res[residx].res_key));
8388 lun->per_res[residx].registered = 0;
8389
8390 for (i=0; i < 2*CTL_MAX_INITIATORS; i++)
8391 if (lun->per_res[i].registered) {
8392 if (!persis_offset && i < CTL_MAX_INITIATORS)
8393 lun->pending_sense[i].ua_pending |=
8394 CTL_UA_RES_PREEMPT;
8395 else if (persis_offset && i >= persis_offset)
8396 lun->pending_sense[i-persis_offset
8397 ].ua_pending |= CTL_UA_RES_PREEMPT;
8398
8399 memset(&lun->per_res[i].res_key,
8400 0, sizeof(struct scsi_per_res_key));
8401 lun->per_res[i].registered = 0;
8402 }
8403 lun->PRGeneration++;
8404 mtx_unlock(&softc->ctl_lock);
8405 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8406 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8407 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8408 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8409 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8410 printf("CTL:Persis Out error returned from "
8411 "ctl_ha_msg_send %d\n", isc_retval);
8412 }
8413 break;
8414
8415 case SPRO_PREEMPT: {
8416 int nretval;
8417
8418 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8419 residx, ctsio, cdb, param);
8420 if (nretval != 0)
8421 return (CTL_RETVAL_COMPLETE);
8422 break;
8423 }
8424 case SPRO_REG_MOVE:
8425 case SPRO_PRE_ABO:
8426 default:
8427 free(ctsio->kern_data_ptr, M_CTL);
8428 ctl_set_invalid_field(/*ctsio*/ ctsio,
8429 /*sks_valid*/ 1,
8430 /*command*/ 1,
8431 /*field*/ 1,
8432 /*bit_valid*/ 1,
8433 /*bit*/ 0);
8434 ctl_done((union ctl_io *)ctsio);
8435 return (CTL_RETVAL_COMPLETE);
8436 break; /* NOTREACHED */
8437 }
8438
8439done:
8440 free(ctsio->kern_data_ptr, M_CTL);
8441 ctl_set_success(ctsio);
8442 ctl_done((union ctl_io *)ctsio);
8443
8444 return (retval);
8445}
8446
8447/*
8448 * This routine is for handling a message from the other SC pertaining to
8449 * persistent reserve out. All the error checking will have been done
8450 * so only perorming the action need be done here to keep the two
8451 * in sync.
8452 */
8453static void
8454ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8455{
8456 struct ctl_lun *lun;
8457 struct ctl_softc *softc;
8458 int i;
8459 uint32_t targ_lun;
8460
8461 softc = control_softc;
8462
8463 mtx_lock(&softc->ctl_lock);
8464
8465 targ_lun = msg->hdr.nexus.targ_lun;
8466 if (msg->hdr.nexus.lun_map_fn != NULL)
8467 targ_lun = msg->hdr.nexus.lun_map_fn(msg->hdr.nexus.lun_map_arg, targ_lun);
8468 lun = softc->ctl_luns[targ_lun];
8469 switch(msg->pr.pr_info.action) {
8470 case CTL_PR_REG_KEY:
8471 if (!lun->per_res[msg->pr.pr_info.residx].registered) {
8472 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8473 lun->pr_key_count++;
8474 }
8475 lun->PRGeneration++;
8476 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key,
8477 msg->pr.pr_info.sa_res_key,
8478 sizeof(struct scsi_per_res_key));
8479 break;
8480
8481 case CTL_PR_UNREG_KEY:
8482 lun->per_res[msg->pr.pr_info.residx].registered = 0;
8483 memset(&lun->per_res[msg->pr.pr_info.residx].res_key,
8484 0, sizeof(struct scsi_per_res_key));
8485 lun->pr_key_count--;
8486
8487 /* XXX Need to see if the reservation has been released */
8488 /* if so do we need to generate UA? */
8489 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8490 lun->flags &= ~CTL_LUN_PR_RESERVED;
8491 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8492
8493 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8494 || lun->res_type == SPR_TYPE_EX_AC_RO)
8495 && lun->pr_key_count) {
8496 /*
8497 * If the reservation is a registrants
8498 * only type we need to generate a UA
8499 * for other registered inits. The
8500 * sense code should be RESERVATIONS
8501 * RELEASED
8502 */
8503
8504 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8505 if (lun->per_res[i+
8506 persis_offset].registered == 0)
8507 continue;
8508
8509 lun->pending_sense[i
8510 ].ua_pending |=
8511 CTL_UA_RES_RELEASE;
8512 }
8513 }
8514 lun->res_type = 0;
8515 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8516 if (lun->pr_key_count==0) {
8517 lun->flags &= ~CTL_LUN_PR_RESERVED;
8518 lun->res_type = 0;
8519 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8520 }
8521 }
8522 lun->PRGeneration++;
8523 break;
8524
8525 case CTL_PR_RESERVE:
8526 lun->flags |= CTL_LUN_PR_RESERVED;
8527 lun->res_type = msg->pr.pr_info.res_type;
8528 lun->pr_res_idx = msg->pr.pr_info.residx;
8529
8530 break;
8531
8532 case CTL_PR_RELEASE:
8533 /*
8534 * if this isn't an exclusive access res generate UA for all
8535 * other registrants.
8536 */
8537 if (lun->res_type != SPR_TYPE_EX_AC
8538 && lun->res_type != SPR_TYPE_WR_EX) {
8539 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8540 if (lun->per_res[i+persis_offset].registered)
8541 lun->pending_sense[i].ua_pending |=
8542 CTL_UA_RES_RELEASE;
8543 }
8544
8545 lun->flags &= ~CTL_LUN_PR_RESERVED;
8546 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8547 lun->res_type = 0;
8548 break;
8549
8550 case CTL_PR_PREEMPT:
8551 ctl_pro_preempt_other(lun, msg);
8552 break;
8553 case CTL_PR_CLEAR:
8554 lun->flags &= ~CTL_LUN_PR_RESERVED;
8555 lun->res_type = 0;
8556 lun->pr_key_count = 0;
8557 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8558
8559 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8560 if (lun->per_res[i].registered == 0)
8561 continue;
8562 if (!persis_offset
8563 && i < CTL_MAX_INITIATORS)
8564 lun->pending_sense[i].ua_pending |=
8565 CTL_UA_RES_PREEMPT;
8566 else if (persis_offset
8567 && i >= persis_offset)
8568 lun->pending_sense[i-persis_offset].ua_pending|=
8569 CTL_UA_RES_PREEMPT;
8570 memset(&lun->per_res[i].res_key, 0,
8571 sizeof(struct scsi_per_res_key));
8572 lun->per_res[i].registered = 0;
8573 }
8574 lun->PRGeneration++;
8575 break;
8576 }
8577
8578 mtx_unlock(&softc->ctl_lock);
8579}
8580
8581int
8582ctl_read_write(struct ctl_scsiio *ctsio)
8583{
8584 struct ctl_lun *lun;
5023 * configuration write.
5024 */
5025void
5026ctl_config_write_done(union ctl_io *io)
5027{
5028 /*
5029 * If the IO_CONT flag is set, we need to call the supplied
5030 * function to continue processing the I/O, instead of completing
5031 * the I/O just yet.
5032 *
5033 * If there is an error, though, we don't want to keep processing.
5034 * Instead, just send status back to the initiator.
5035 */
5036 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT)
5037 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)
5038 || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) {
5039 io->scsiio.io_cont(io);
5040 return;
5041 }
5042 /*
5043 * Since a configuration write can be done for commands that actually
5044 * have data allocated, like write buffer, and commands that have
5045 * no data, like start/stop unit, we need to check here.
5046 */
5047 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
5048 free(io->scsiio.kern_data_ptr, M_CTL);
5049 ctl_done(io);
5050}
5051
5052/*
5053 * SCSI release command.
5054 */
5055int
5056ctl_scsi_release(struct ctl_scsiio *ctsio)
5057{
5058 int length, longid, thirdparty_id, resv_id;
5059 struct ctl_softc *ctl_softc;
5060 struct ctl_lun *lun;
5061
5062 length = 0;
5063 resv_id = 0;
5064
5065 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5066
5067 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5068 ctl_softc = control_softc;
5069
5070 switch (ctsio->cdb[0]) {
5071 case RELEASE: {
5072 struct scsi_release *cdb;
5073
5074 cdb = (struct scsi_release *)ctsio->cdb;
5075 if ((cdb->byte2 & 0x1f) != 0) {
5076 ctl_set_invalid_field(ctsio,
5077 /*sks_valid*/ 1,
5078 /*command*/ 1,
5079 /*field*/ 1,
5080 /*bit_valid*/ 0,
5081 /*bit*/ 0);
5082 ctl_done((union ctl_io *)ctsio);
5083 return (CTL_RETVAL_COMPLETE);
5084 }
5085 break;
5086 }
5087 case RELEASE_10: {
5088 struct scsi_release_10 *cdb;
5089
5090 cdb = (struct scsi_release_10 *)ctsio->cdb;
5091
5092 if ((cdb->byte2 & SR10_EXTENT) != 0) {
5093 ctl_set_invalid_field(ctsio,
5094 /*sks_valid*/ 1,
5095 /*command*/ 1,
5096 /*field*/ 1,
5097 /*bit_valid*/ 1,
5098 /*bit*/ 0);
5099 ctl_done((union ctl_io *)ctsio);
5100 return (CTL_RETVAL_COMPLETE);
5101
5102 }
5103
5104 if ((cdb->byte2 & SR10_3RDPTY) != 0) {
5105 ctl_set_invalid_field(ctsio,
5106 /*sks_valid*/ 1,
5107 /*command*/ 1,
5108 /*field*/ 1,
5109 /*bit_valid*/ 1,
5110 /*bit*/ 4);
5111 ctl_done((union ctl_io *)ctsio);
5112 return (CTL_RETVAL_COMPLETE);
5113 }
5114
5115 if (cdb->byte2 & SR10_LONGID)
5116 longid = 1;
5117 else
5118 thirdparty_id = cdb->thirdparty_id;
5119
5120 resv_id = cdb->resv_id;
5121 length = scsi_2btoul(cdb->length);
5122 break;
5123 }
5124 }
5125
5126
5127 /*
5128 * XXX KDM right now, we only support LUN reservation. We don't
5129 * support 3rd party reservations, or extent reservations, which
5130 * might actually need the parameter list. If we've gotten this
5131 * far, we've got a LUN reservation. Anything else got kicked out
5132 * above. So, according to SPC, ignore the length.
5133 */
5134 length = 0;
5135
5136 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5137 && (length > 0)) {
5138 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5139 ctsio->kern_data_len = length;
5140 ctsio->kern_total_len = length;
5141 ctsio->kern_data_resid = 0;
5142 ctsio->kern_rel_offset = 0;
5143 ctsio->kern_sg_entries = 0;
5144 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5145 ctsio->be_move_done = ctl_config_move_done;
5146 ctl_datamove((union ctl_io *)ctsio);
5147
5148 return (CTL_RETVAL_COMPLETE);
5149 }
5150
5151 if (length > 0)
5152 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5153
5154 mtx_lock(&ctl_softc->ctl_lock);
5155
5156 /*
5157 * According to SPC, it is not an error for an intiator to attempt
5158 * to release a reservation on a LUN that isn't reserved, or that
5159 * is reserved by another initiator. The reservation can only be
5160 * released, though, by the initiator who made it or by one of
5161 * several reset type events.
5162 */
5163 if (lun->flags & CTL_LUN_RESERVED) {
5164 if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id)
5165 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port)
5166 && (ctsio->io_hdr.nexus.targ_target.id ==
5167 lun->rsv_nexus.targ_target.id)) {
5168 lun->flags &= ~CTL_LUN_RESERVED;
5169 }
5170 }
5171
5172 ctsio->scsi_status = SCSI_STATUS_OK;
5173 ctsio->io_hdr.status = CTL_SUCCESS;
5174
5175 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5176 free(ctsio->kern_data_ptr, M_CTL);
5177 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5178 }
5179
5180 mtx_unlock(&ctl_softc->ctl_lock);
5181
5182 ctl_done((union ctl_io *)ctsio);
5183 return (CTL_RETVAL_COMPLETE);
5184}
5185
5186int
5187ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5188{
5189 int extent, thirdparty, longid;
5190 int resv_id, length;
5191 uint64_t thirdparty_id;
5192 struct ctl_softc *ctl_softc;
5193 struct ctl_lun *lun;
5194
5195 extent = 0;
5196 thirdparty = 0;
5197 longid = 0;
5198 resv_id = 0;
5199 length = 0;
5200 thirdparty_id = 0;
5201
5202 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5203
5204 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5205 ctl_softc = control_softc;
5206
5207 switch (ctsio->cdb[0]) {
5208 case RESERVE: {
5209 struct scsi_reserve *cdb;
5210
5211 cdb = (struct scsi_reserve *)ctsio->cdb;
5212 if ((cdb->byte2 & 0x1f) != 0) {
5213 ctl_set_invalid_field(ctsio,
5214 /*sks_valid*/ 1,
5215 /*command*/ 1,
5216 /*field*/ 1,
5217 /*bit_valid*/ 0,
5218 /*bit*/ 0);
5219 ctl_done((union ctl_io *)ctsio);
5220 return (CTL_RETVAL_COMPLETE);
5221 }
5222 resv_id = cdb->resv_id;
5223 length = scsi_2btoul(cdb->length);
5224 break;
5225 }
5226 case RESERVE_10: {
5227 struct scsi_reserve_10 *cdb;
5228
5229 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
5230
5231 if ((cdb->byte2 & SR10_EXTENT) != 0) {
5232 ctl_set_invalid_field(ctsio,
5233 /*sks_valid*/ 1,
5234 /*command*/ 1,
5235 /*field*/ 1,
5236 /*bit_valid*/ 1,
5237 /*bit*/ 0);
5238 ctl_done((union ctl_io *)ctsio);
5239 return (CTL_RETVAL_COMPLETE);
5240 }
5241 if ((cdb->byte2 & SR10_3RDPTY) != 0) {
5242 ctl_set_invalid_field(ctsio,
5243 /*sks_valid*/ 1,
5244 /*command*/ 1,
5245 /*field*/ 1,
5246 /*bit_valid*/ 1,
5247 /*bit*/ 4);
5248 ctl_done((union ctl_io *)ctsio);
5249 return (CTL_RETVAL_COMPLETE);
5250 }
5251 if (cdb->byte2 & SR10_LONGID)
5252 longid = 1;
5253 else
5254 thirdparty_id = cdb->thirdparty_id;
5255
5256 resv_id = cdb->resv_id;
5257 length = scsi_2btoul(cdb->length);
5258 break;
5259 }
5260 }
5261
5262 /*
5263 * XXX KDM right now, we only support LUN reservation. We don't
5264 * support 3rd party reservations, or extent reservations, which
5265 * might actually need the parameter list. If we've gotten this
5266 * far, we've got a LUN reservation. Anything else got kicked out
5267 * above. So, according to SPC, ignore the length.
5268 */
5269 length = 0;
5270
5271 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5272 && (length > 0)) {
5273 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5274 ctsio->kern_data_len = length;
5275 ctsio->kern_total_len = length;
5276 ctsio->kern_data_resid = 0;
5277 ctsio->kern_rel_offset = 0;
5278 ctsio->kern_sg_entries = 0;
5279 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5280 ctsio->be_move_done = ctl_config_move_done;
5281 ctl_datamove((union ctl_io *)ctsio);
5282
5283 return (CTL_RETVAL_COMPLETE);
5284 }
5285
5286 if (length > 0)
5287 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5288
5289 mtx_lock(&ctl_softc->ctl_lock);
5290 if (lun->flags & CTL_LUN_RESERVED) {
5291 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
5292 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
5293 || (ctsio->io_hdr.nexus.targ_target.id !=
5294 lun->rsv_nexus.targ_target.id)) {
5295 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
5296 ctsio->io_hdr.status = CTL_SCSI_ERROR;
5297 goto bailout;
5298 }
5299 }
5300
5301 lun->flags |= CTL_LUN_RESERVED;
5302 lun->rsv_nexus = ctsio->io_hdr.nexus;
5303
5304 ctsio->scsi_status = SCSI_STATUS_OK;
5305 ctsio->io_hdr.status = CTL_SUCCESS;
5306
5307bailout:
5308 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5309 free(ctsio->kern_data_ptr, M_CTL);
5310 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5311 }
5312
5313 mtx_unlock(&ctl_softc->ctl_lock);
5314
5315 ctl_done((union ctl_io *)ctsio);
5316 return (CTL_RETVAL_COMPLETE);
5317}
5318
5319int
5320ctl_start_stop(struct ctl_scsiio *ctsio)
5321{
5322 struct scsi_start_stop_unit *cdb;
5323 struct ctl_lun *lun;
5324 struct ctl_softc *ctl_softc;
5325 int retval;
5326
5327 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5328
5329 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5330 ctl_softc = control_softc;
5331 retval = 0;
5332
5333 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5334
5335 /*
5336 * XXX KDM
5337 * We don't support the immediate bit on a stop unit. In order to
5338 * do that, we would need to code up a way to know that a stop is
5339 * pending, and hold off any new commands until it completes, one
5340 * way or another. Then we could accept or reject those commands
5341 * depending on its status. We would almost need to do the reverse
5342 * of what we do below for an immediate start -- return the copy of
5343 * the ctl_io to the FETD with status to send to the host (and to
5344 * free the copy!) and then free the original I/O once the stop
5345 * actually completes. That way, the OOA queue mechanism can work
5346 * to block commands that shouldn't proceed. Another alternative
5347 * would be to put the copy in the queue in place of the original,
5348 * and return the original back to the caller. That could be
5349 * slightly safer..
5350 */
5351 if ((cdb->byte2 & SSS_IMMED)
5352 && ((cdb->how & SSS_START) == 0)) {
5353 ctl_set_invalid_field(ctsio,
5354 /*sks_valid*/ 1,
5355 /*command*/ 1,
5356 /*field*/ 1,
5357 /*bit_valid*/ 1,
5358 /*bit*/ 0);
5359 ctl_done((union ctl_io *)ctsio);
5360 return (CTL_RETVAL_COMPLETE);
5361 }
5362
5363 /*
5364 * We don't support the power conditions field. We need to check
5365 * this prior to checking the load/eject and start/stop bits.
5366 */
5367 if ((cdb->how & SSS_PC_MASK) != SSS_PC_START_VALID) {
5368 ctl_set_invalid_field(ctsio,
5369 /*sks_valid*/ 1,
5370 /*command*/ 1,
5371 /*field*/ 4,
5372 /*bit_valid*/ 1,
5373 /*bit*/ 4);
5374 ctl_done((union ctl_io *)ctsio);
5375 return (CTL_RETVAL_COMPLETE);
5376 }
5377
5378 /*
5379 * Media isn't removable, so we can't load or eject it.
5380 */
5381 if ((cdb->how & SSS_LOEJ) != 0) {
5382 ctl_set_invalid_field(ctsio,
5383 /*sks_valid*/ 1,
5384 /*command*/ 1,
5385 /*field*/ 4,
5386 /*bit_valid*/ 1,
5387 /*bit*/ 1);
5388 ctl_done((union ctl_io *)ctsio);
5389 return (CTL_RETVAL_COMPLETE);
5390 }
5391
5392 if ((lun->flags & CTL_LUN_PR_RESERVED)
5393 && ((cdb->how & SSS_START)==0)) {
5394 uint32_t residx;
5395
5396 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5397 if (!lun->per_res[residx].registered
5398 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5399
5400 ctl_set_reservation_conflict(ctsio);
5401 ctl_done((union ctl_io *)ctsio);
5402 return (CTL_RETVAL_COMPLETE);
5403 }
5404 }
5405
5406 /*
5407 * If there is no backend on this device, we can't start or stop
5408 * it. In theory we shouldn't get any start/stop commands in the
5409 * first place at this level if the LUN doesn't have a backend.
5410 * That should get stopped by the command decode code.
5411 */
5412 if (lun->backend == NULL) {
5413 ctl_set_invalid_opcode(ctsio);
5414 ctl_done((union ctl_io *)ctsio);
5415 return (CTL_RETVAL_COMPLETE);
5416 }
5417
5418 /*
5419 * XXX KDM Copan-specific offline behavior.
5420 * Figure out a reasonable way to port this?
5421 */
5422#ifdef NEEDTOPORT
5423 mtx_lock(&ctl_softc->ctl_lock);
5424
5425 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5426 && (lun->flags & CTL_LUN_OFFLINE)) {
5427 /*
5428 * If the LUN is offline, and the on/offline bit isn't set,
5429 * reject the start or stop. Otherwise, let it through.
5430 */
5431 mtx_unlock(&ctl_softc->ctl_lock);
5432 ctl_set_lun_not_ready(ctsio);
5433 ctl_done((union ctl_io *)ctsio);
5434 } else {
5435 mtx_unlock(&ctl_softc->ctl_lock);
5436#endif /* NEEDTOPORT */
5437 /*
5438 * This could be a start or a stop when we're online,
5439 * or a stop/offline or start/online. A start or stop when
5440 * we're offline is covered in the case above.
5441 */
5442 /*
5443 * In the non-immediate case, we send the request to
5444 * the backend and return status to the user when
5445 * it is done.
5446 *
5447 * In the immediate case, we allocate a new ctl_io
5448 * to hold a copy of the request, and send that to
5449 * the backend. We then set good status on the
5450 * user's request and return it immediately.
5451 */
5452 if (cdb->byte2 & SSS_IMMED) {
5453 union ctl_io *new_io;
5454
5455 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5456 if (new_io == NULL) {
5457 ctl_set_busy(ctsio);
5458 ctl_done((union ctl_io *)ctsio);
5459 } else {
5460 ctl_copy_io((union ctl_io *)ctsio,
5461 new_io);
5462 retval = lun->backend->config_write(new_io);
5463 ctl_set_success(ctsio);
5464 ctl_done((union ctl_io *)ctsio);
5465 }
5466 } else {
5467 retval = lun->backend->config_write(
5468 (union ctl_io *)ctsio);
5469 }
5470#ifdef NEEDTOPORT
5471 }
5472#endif
5473 return (retval);
5474}
5475
5476/*
5477 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5478 * we don't really do anything with the LBA and length fields if the user
5479 * passes them in. Instead we'll just flush out the cache for the entire
5480 * LUN.
5481 */
5482int
5483ctl_sync_cache(struct ctl_scsiio *ctsio)
5484{
5485 struct ctl_lun *lun;
5486 struct ctl_softc *ctl_softc;
5487 uint64_t starting_lba;
5488 uint32_t block_count;
5489 int reladr, immed;
5490 int retval;
5491
5492 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5493
5494 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5495 ctl_softc = control_softc;
5496 retval = 0;
5497 reladr = 0;
5498 immed = 0;
5499
5500 switch (ctsio->cdb[0]) {
5501 case SYNCHRONIZE_CACHE: {
5502 struct scsi_sync_cache *cdb;
5503 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5504
5505 if (cdb->byte2 & SSC_RELADR)
5506 reladr = 1;
5507
5508 if (cdb->byte2 & SSC_IMMED)
5509 immed = 1;
5510
5511 starting_lba = scsi_4btoul(cdb->begin_lba);
5512 block_count = scsi_2btoul(cdb->lb_count);
5513 break;
5514 }
5515 case SYNCHRONIZE_CACHE_16: {
5516 struct scsi_sync_cache_16 *cdb;
5517 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5518
5519 if (cdb->byte2 & SSC_RELADR)
5520 reladr = 1;
5521
5522 if (cdb->byte2 & SSC_IMMED)
5523 immed = 1;
5524
5525 starting_lba = scsi_8btou64(cdb->begin_lba);
5526 block_count = scsi_4btoul(cdb->lb_count);
5527 break;
5528 }
5529 default:
5530 ctl_set_invalid_opcode(ctsio);
5531 ctl_done((union ctl_io *)ctsio);
5532 goto bailout;
5533 break; /* NOTREACHED */
5534 }
5535
5536 if (immed) {
5537 /*
5538 * We don't support the immediate bit. Since it's in the
5539 * same place for the 10 and 16 byte SYNCHRONIZE CACHE
5540 * commands, we can just return the same error in either
5541 * case.
5542 */
5543 ctl_set_invalid_field(ctsio,
5544 /*sks_valid*/ 1,
5545 /*command*/ 1,
5546 /*field*/ 1,
5547 /*bit_valid*/ 1,
5548 /*bit*/ 1);
5549 ctl_done((union ctl_io *)ctsio);
5550 goto bailout;
5551 }
5552
5553 if (reladr) {
5554 /*
5555 * We don't support the reladr bit either. It can only be
5556 * used with linked commands, and we don't support linked
5557 * commands. Since the bit is in the same place for the
5558 * 10 and 16 byte SYNCHRONIZE CACHE * commands, we can
5559 * just return the same error in either case.
5560 */
5561 ctl_set_invalid_field(ctsio,
5562 /*sks_valid*/ 1,
5563 /*command*/ 1,
5564 /*field*/ 1,
5565 /*bit_valid*/ 1,
5566 /*bit*/ 0);
5567 ctl_done((union ctl_io *)ctsio);
5568 goto bailout;
5569 }
5570
5571 /*
5572 * We check the LBA and length, but don't do anything with them.
5573 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5574 * get flushed. This check will just help satisfy anyone who wants
5575 * to see an error for an out of range LBA.
5576 */
5577 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5578 ctl_set_lba_out_of_range(ctsio);
5579 ctl_done((union ctl_io *)ctsio);
5580 goto bailout;
5581 }
5582
5583 /*
5584 * If this LUN has no backend, we can't flush the cache anyway.
5585 */
5586 if (lun->backend == NULL) {
5587 ctl_set_invalid_opcode(ctsio);
5588 ctl_done((union ctl_io *)ctsio);
5589 goto bailout;
5590 }
5591
5592 /*
5593 * Check to see whether we're configured to send the SYNCHRONIZE
5594 * CACHE command directly to the back end.
5595 */
5596 mtx_lock(&ctl_softc->ctl_lock);
5597 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC)
5598 && (++(lun->sync_count) >= lun->sync_interval)) {
5599 lun->sync_count = 0;
5600 mtx_unlock(&ctl_softc->ctl_lock);
5601 retval = lun->backend->config_write((union ctl_io *)ctsio);
5602 } else {
5603 mtx_unlock(&ctl_softc->ctl_lock);
5604 ctl_set_success(ctsio);
5605 ctl_done((union ctl_io *)ctsio);
5606 }
5607
5608bailout:
5609
5610 return (retval);
5611}
5612
5613int
5614ctl_format(struct ctl_scsiio *ctsio)
5615{
5616 struct scsi_format *cdb;
5617 struct ctl_lun *lun;
5618 struct ctl_softc *ctl_softc;
5619 int length, defect_list_len;
5620
5621 CTL_DEBUG_PRINT(("ctl_format\n"));
5622
5623 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5624 ctl_softc = control_softc;
5625
5626 cdb = (struct scsi_format *)ctsio->cdb;
5627
5628 length = 0;
5629 if (cdb->byte2 & SF_FMTDATA) {
5630 if (cdb->byte2 & SF_LONGLIST)
5631 length = sizeof(struct scsi_format_header_long);
5632 else
5633 length = sizeof(struct scsi_format_header_short);
5634 }
5635
5636 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5637 && (length > 0)) {
5638 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5639 ctsio->kern_data_len = length;
5640 ctsio->kern_total_len = length;
5641 ctsio->kern_data_resid = 0;
5642 ctsio->kern_rel_offset = 0;
5643 ctsio->kern_sg_entries = 0;
5644 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5645 ctsio->be_move_done = ctl_config_move_done;
5646 ctl_datamove((union ctl_io *)ctsio);
5647
5648 return (CTL_RETVAL_COMPLETE);
5649 }
5650
5651 defect_list_len = 0;
5652
5653 if (cdb->byte2 & SF_FMTDATA) {
5654 if (cdb->byte2 & SF_LONGLIST) {
5655 struct scsi_format_header_long *header;
5656
5657 header = (struct scsi_format_header_long *)
5658 ctsio->kern_data_ptr;
5659
5660 defect_list_len = scsi_4btoul(header->defect_list_len);
5661 if (defect_list_len != 0) {
5662 ctl_set_invalid_field(ctsio,
5663 /*sks_valid*/ 1,
5664 /*command*/ 0,
5665 /*field*/ 2,
5666 /*bit_valid*/ 0,
5667 /*bit*/ 0);
5668 goto bailout;
5669 }
5670 } else {
5671 struct scsi_format_header_short *header;
5672
5673 header = (struct scsi_format_header_short *)
5674 ctsio->kern_data_ptr;
5675
5676 defect_list_len = scsi_2btoul(header->defect_list_len);
5677 if (defect_list_len != 0) {
5678 ctl_set_invalid_field(ctsio,
5679 /*sks_valid*/ 1,
5680 /*command*/ 0,
5681 /*field*/ 2,
5682 /*bit_valid*/ 0,
5683 /*bit*/ 0);
5684 goto bailout;
5685 }
5686 }
5687 }
5688
5689 /*
5690 * The format command will clear out the "Medium format corrupted"
5691 * status if set by the configuration code. That status is really
5692 * just a way to notify the host that we have lost the media, and
5693 * get them to issue a command that will basically make them think
5694 * they're blowing away the media.
5695 */
5696 mtx_lock(&ctl_softc->ctl_lock);
5697 lun->flags &= ~CTL_LUN_INOPERABLE;
5698 mtx_unlock(&ctl_softc->ctl_lock);
5699
5700 ctsio->scsi_status = SCSI_STATUS_OK;
5701 ctsio->io_hdr.status = CTL_SUCCESS;
5702bailout:
5703
5704 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5705 free(ctsio->kern_data_ptr, M_CTL);
5706 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5707 }
5708
5709 ctl_done((union ctl_io *)ctsio);
5710 return (CTL_RETVAL_COMPLETE);
5711}
5712
5713int
5714ctl_write_buffer(struct ctl_scsiio *ctsio)
5715{
5716 struct scsi_write_buffer *cdb;
5717 struct copan_page_header *header;
5718 struct ctl_lun *lun;
5719 struct ctl_softc *ctl_softc;
5720 int buffer_offset, len;
5721 int retval;
5722
5723 header = NULL;
5724
5725 retval = CTL_RETVAL_COMPLETE;
5726
5727 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5728
5729 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5730 ctl_softc = control_softc;
5731 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5732
5733 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5734 ctl_set_invalid_field(ctsio,
5735 /*sks_valid*/ 1,
5736 /*command*/ 1,
5737 /*field*/ 1,
5738 /*bit_valid*/ 1,
5739 /*bit*/ 4);
5740 ctl_done((union ctl_io *)ctsio);
5741 return (CTL_RETVAL_COMPLETE);
5742 }
5743 if (cdb->buffer_id != 0) {
5744 ctl_set_invalid_field(ctsio,
5745 /*sks_valid*/ 1,
5746 /*command*/ 1,
5747 /*field*/ 2,
5748 /*bit_valid*/ 0,
5749 /*bit*/ 0);
5750 ctl_done((union ctl_io *)ctsio);
5751 return (CTL_RETVAL_COMPLETE);
5752 }
5753
5754 len = scsi_3btoul(cdb->length);
5755 buffer_offset = scsi_3btoul(cdb->offset);
5756
5757 if (len > sizeof(lun->write_buffer)) {
5758 ctl_set_invalid_field(ctsio,
5759 /*sks_valid*/ 1,
5760 /*command*/ 1,
5761 /*field*/ 6,
5762 /*bit_valid*/ 0,
5763 /*bit*/ 0);
5764 ctl_done((union ctl_io *)ctsio);
5765 return (CTL_RETVAL_COMPLETE);
5766 }
5767
5768 if (buffer_offset != 0) {
5769 ctl_set_invalid_field(ctsio,
5770 /*sks_valid*/ 1,
5771 /*command*/ 1,
5772 /*field*/ 3,
5773 /*bit_valid*/ 0,
5774 /*bit*/ 0);
5775 ctl_done((union ctl_io *)ctsio);
5776 return (CTL_RETVAL_COMPLETE);
5777 }
5778
5779 /*
5780 * If we've got a kernel request that hasn't been malloced yet,
5781 * malloc it and tell the caller the data buffer is here.
5782 */
5783 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5784 ctsio->kern_data_ptr = lun->write_buffer;
5785 ctsio->kern_data_len = len;
5786 ctsio->kern_total_len = len;
5787 ctsio->kern_data_resid = 0;
5788 ctsio->kern_rel_offset = 0;
5789 ctsio->kern_sg_entries = 0;
5790 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5791 ctsio->be_move_done = ctl_config_move_done;
5792 ctl_datamove((union ctl_io *)ctsio);
5793
5794 return (CTL_RETVAL_COMPLETE);
5795 }
5796
5797 ctl_done((union ctl_io *)ctsio);
5798
5799 return (CTL_RETVAL_COMPLETE);
5800}
5801
5802int
5803ctl_write_same(struct ctl_scsiio *ctsio)
5804{
5805 struct ctl_lun *lun;
5806 struct ctl_lba_len_flags *lbalen;
5807 uint64_t lba;
5808 uint32_t num_blocks;
5809 int len, retval;
5810 uint8_t byte2;
5811
5812 retval = CTL_RETVAL_COMPLETE;
5813
5814 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5815
5816 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5817
5818 switch (ctsio->cdb[0]) {
5819 case WRITE_SAME_10: {
5820 struct scsi_write_same_10 *cdb;
5821
5822 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5823
5824 lba = scsi_4btoul(cdb->addr);
5825 num_blocks = scsi_2btoul(cdb->length);
5826 byte2 = cdb->byte2;
5827 break;
5828 }
5829 case WRITE_SAME_16: {
5830 struct scsi_write_same_16 *cdb;
5831
5832 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5833
5834 lba = scsi_8btou64(cdb->addr);
5835 num_blocks = scsi_4btoul(cdb->length);
5836 byte2 = cdb->byte2;
5837 break;
5838 }
5839 default:
5840 /*
5841 * We got a command we don't support. This shouldn't
5842 * happen, commands should be filtered out above us.
5843 */
5844 ctl_set_invalid_opcode(ctsio);
5845 ctl_done((union ctl_io *)ctsio);
5846
5847 return (CTL_RETVAL_COMPLETE);
5848 break; /* NOTREACHED */
5849 }
5850
5851 /*
5852 * The first check is to make sure we're in bounds, the second
5853 * check is to catch wrap-around problems. If the lba + num blocks
5854 * is less than the lba, then we've wrapped around and the block
5855 * range is invalid anyway.
5856 */
5857 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5858 || ((lba + num_blocks) < lba)) {
5859 ctl_set_lba_out_of_range(ctsio);
5860 ctl_done((union ctl_io *)ctsio);
5861 return (CTL_RETVAL_COMPLETE);
5862 }
5863
5864 /* Zero number of blocks means "to the last logical block" */
5865 if (num_blocks == 0) {
5866 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5867 ctl_set_invalid_field(ctsio,
5868 /*sks_valid*/ 0,
5869 /*command*/ 1,
5870 /*field*/ 0,
5871 /*bit_valid*/ 0,
5872 /*bit*/ 0);
5873 ctl_done((union ctl_io *)ctsio);
5874 return (CTL_RETVAL_COMPLETE);
5875 }
5876 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5877 }
5878
5879 len = lun->be_lun->blocksize;
5880
5881 /*
5882 * If we've got a kernel request that hasn't been malloced yet,
5883 * malloc it and tell the caller the data buffer is here.
5884 */
5885 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5886 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5887 ctsio->kern_data_len = len;
5888 ctsio->kern_total_len = len;
5889 ctsio->kern_data_resid = 0;
5890 ctsio->kern_rel_offset = 0;
5891 ctsio->kern_sg_entries = 0;
5892 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5893 ctsio->be_move_done = ctl_config_move_done;
5894 ctl_datamove((union ctl_io *)ctsio);
5895
5896 return (CTL_RETVAL_COMPLETE);
5897 }
5898
5899 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5900 lbalen->lba = lba;
5901 lbalen->len = num_blocks;
5902 lbalen->flags = byte2;
5903 retval = lun->backend->config_write((union ctl_io *)ctsio);
5904
5905 return (retval);
5906}
5907
5908int
5909ctl_unmap(struct ctl_scsiio *ctsio)
5910{
5911 struct ctl_lun *lun;
5912 struct scsi_unmap *cdb;
5913 struct ctl_ptr_len_flags *ptrlen;
5914 struct scsi_unmap_header *hdr;
5915 struct scsi_unmap_desc *buf, *end;
5916 uint64_t lba;
5917 uint32_t num_blocks;
5918 int len, retval;
5919 uint8_t byte2;
5920
5921 retval = CTL_RETVAL_COMPLETE;
5922
5923 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5924
5925 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5926 cdb = (struct scsi_unmap *)ctsio->cdb;
5927
5928 len = scsi_2btoul(cdb->length);
5929 byte2 = cdb->byte2;
5930
5931 /*
5932 * If we've got a kernel request that hasn't been malloced yet,
5933 * malloc it and tell the caller the data buffer is here.
5934 */
5935 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5936 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5937 ctsio->kern_data_len = len;
5938 ctsio->kern_total_len = len;
5939 ctsio->kern_data_resid = 0;
5940 ctsio->kern_rel_offset = 0;
5941 ctsio->kern_sg_entries = 0;
5942 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5943 ctsio->be_move_done = ctl_config_move_done;
5944 ctl_datamove((union ctl_io *)ctsio);
5945
5946 return (CTL_RETVAL_COMPLETE);
5947 }
5948
5949 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5950 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5951 if (len < sizeof (*hdr) ||
5952 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5953 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5954 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5955 ctl_set_invalid_field(ctsio,
5956 /*sks_valid*/ 0,
5957 /*command*/ 0,
5958 /*field*/ 0,
5959 /*bit_valid*/ 0,
5960 /*bit*/ 0);
5961 ctl_done((union ctl_io *)ctsio);
5962 return (CTL_RETVAL_COMPLETE);
5963 }
5964 len = scsi_2btoul(hdr->desc_length);
5965 buf = (struct scsi_unmap_desc *)(hdr + 1);
5966 end = buf + len / sizeof(*buf);
5967
5968 ptrlen = (struct ctl_ptr_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5969 ptrlen->ptr = (void *)buf;
5970 ptrlen->len = len;
5971 ptrlen->flags = byte2;
5972
5973 for (; buf < end; buf++) {
5974 lba = scsi_8btou64(buf->lba);
5975 num_blocks = scsi_4btoul(buf->length);
5976 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5977 || ((lba + num_blocks) < lba)) {
5978 ctl_set_lba_out_of_range(ctsio);
5979 ctl_done((union ctl_io *)ctsio);
5980 return (CTL_RETVAL_COMPLETE);
5981 }
5982 }
5983
5984 retval = lun->backend->config_write((union ctl_io *)ctsio);
5985
5986 return (retval);
5987}
5988
5989/*
5990 * Note that this function currently doesn't actually do anything inside
5991 * CTL to enforce things if the DQue bit is turned on.
5992 *
5993 * Also note that this function can't be used in the default case, because
5994 * the DQue bit isn't set in the changeable mask for the control mode page
5995 * anyway. This is just here as an example for how to implement a page
5996 * handler, and a placeholder in case we want to allow the user to turn
5997 * tagged queueing on and off.
5998 *
5999 * The D_SENSE bit handling is functional, however, and will turn
6000 * descriptor sense on and off for a given LUN.
6001 */
6002int
6003ctl_control_page_handler(struct ctl_scsiio *ctsio,
6004 struct ctl_page_index *page_index, uint8_t *page_ptr)
6005{
6006 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
6007 struct ctl_lun *lun;
6008 struct ctl_softc *softc;
6009 int set_ua;
6010 uint32_t initidx;
6011
6012 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6013 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6014 set_ua = 0;
6015
6016 user_cp = (struct scsi_control_page *)page_ptr;
6017 current_cp = (struct scsi_control_page *)
6018 (page_index->page_data + (page_index->page_len *
6019 CTL_PAGE_CURRENT));
6020 saved_cp = (struct scsi_control_page *)
6021 (page_index->page_data + (page_index->page_len *
6022 CTL_PAGE_SAVED));
6023
6024 softc = control_softc;
6025
6026 mtx_lock(&softc->ctl_lock);
6027 if (((current_cp->rlec & SCP_DSENSE) == 0)
6028 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
6029 /*
6030 * Descriptor sense is currently turned off and the user
6031 * wants to turn it on.
6032 */
6033 current_cp->rlec |= SCP_DSENSE;
6034 saved_cp->rlec |= SCP_DSENSE;
6035 lun->flags |= CTL_LUN_SENSE_DESC;
6036 set_ua = 1;
6037 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
6038 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
6039 /*
6040 * Descriptor sense is currently turned on, and the user
6041 * wants to turn it off.
6042 */
6043 current_cp->rlec &= ~SCP_DSENSE;
6044 saved_cp->rlec &= ~SCP_DSENSE;
6045 lun->flags &= ~CTL_LUN_SENSE_DESC;
6046 set_ua = 1;
6047 }
6048 if (current_cp->queue_flags & SCP_QUEUE_DQUE) {
6049 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
6050#ifdef NEEDTOPORT
6051 csevent_log(CSC_CTL | CSC_SHELF_SW |
6052 CTL_UNTAG_TO_UNTAG,
6053 csevent_LogType_Trace,
6054 csevent_Severity_Information,
6055 csevent_AlertLevel_Green,
6056 csevent_FRU_Firmware,
6057 csevent_FRU_Unknown,
6058 "Received untagged to untagged transition");
6059#endif /* NEEDTOPORT */
6060 } else {
6061#ifdef NEEDTOPORT
6062 csevent_log(CSC_CTL | CSC_SHELF_SW |
6063 CTL_UNTAG_TO_TAG,
6064 csevent_LogType_ConfigChange,
6065 csevent_Severity_Information,
6066 csevent_AlertLevel_Green,
6067 csevent_FRU_Firmware,
6068 csevent_FRU_Unknown,
6069 "Received untagged to tagged "
6070 "queueing transition");
6071#endif /* NEEDTOPORT */
6072
6073 current_cp->queue_flags &= ~SCP_QUEUE_DQUE;
6074 saved_cp->queue_flags &= ~SCP_QUEUE_DQUE;
6075 set_ua = 1;
6076 }
6077 } else {
6078 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
6079#ifdef NEEDTOPORT
6080 csevent_log(CSC_CTL | CSC_SHELF_SW |
6081 CTL_TAG_TO_UNTAG,
6082 csevent_LogType_ConfigChange,
6083 csevent_Severity_Warning,
6084 csevent_AlertLevel_Yellow,
6085 csevent_FRU_Firmware,
6086 csevent_FRU_Unknown,
6087 "Received tagged queueing to untagged "
6088 "transition");
6089#endif /* NEEDTOPORT */
6090
6091 current_cp->queue_flags |= SCP_QUEUE_DQUE;
6092 saved_cp->queue_flags |= SCP_QUEUE_DQUE;
6093 set_ua = 1;
6094 } else {
6095#ifdef NEEDTOPORT
6096 csevent_log(CSC_CTL | CSC_SHELF_SW |
6097 CTL_TAG_TO_TAG,
6098 csevent_LogType_Trace,
6099 csevent_Severity_Information,
6100 csevent_AlertLevel_Green,
6101 csevent_FRU_Firmware,
6102 csevent_FRU_Unknown,
6103 "Received tagged queueing to tagged "
6104 "queueing transition");
6105#endif /* NEEDTOPORT */
6106 }
6107 }
6108 if (set_ua != 0) {
6109 int i;
6110 /*
6111 * Let other initiators know that the mode
6112 * parameters for this LUN have changed.
6113 */
6114 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
6115 if (i == initidx)
6116 continue;
6117
6118 lun->pending_sense[i].ua_pending |=
6119 CTL_UA_MODE_CHANGE;
6120 }
6121 }
6122 mtx_unlock(&softc->ctl_lock);
6123
6124 return (0);
6125}
6126
6127int
6128ctl_power_sp_handler(struct ctl_scsiio *ctsio,
6129 struct ctl_page_index *page_index, uint8_t *page_ptr)
6130{
6131 return (0);
6132}
6133
6134int
6135ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio,
6136 struct ctl_page_index *page_index, int pc)
6137{
6138 struct copan_power_subpage *page;
6139
6140 page = (struct copan_power_subpage *)page_index->page_data +
6141 (page_index->page_len * pc);
6142
6143 switch (pc) {
6144 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6145 /*
6146 * We don't update the changable bits for this page.
6147 */
6148 break;
6149 case SMS_PAGE_CTRL_CURRENT >> 6:
6150 case SMS_PAGE_CTRL_DEFAULT >> 6:
6151 case SMS_PAGE_CTRL_SAVED >> 6:
6152#ifdef NEEDTOPORT
6153 ctl_update_power_subpage(page);
6154#endif
6155 break;
6156 default:
6157#ifdef NEEDTOPORT
6158 EPRINT(0, "Invalid PC %d!!", pc);
6159#endif
6160 break;
6161 }
6162 return (0);
6163}
6164
6165
6166int
6167ctl_aps_sp_handler(struct ctl_scsiio *ctsio,
6168 struct ctl_page_index *page_index, uint8_t *page_ptr)
6169{
6170 struct copan_aps_subpage *user_sp;
6171 struct copan_aps_subpage *current_sp;
6172 union ctl_modepage_info *modepage_info;
6173 struct ctl_softc *softc;
6174 struct ctl_lun *lun;
6175 int retval;
6176
6177 retval = CTL_RETVAL_COMPLETE;
6178 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
6179 (page_index->page_len * CTL_PAGE_CURRENT));
6180 softc = control_softc;
6181 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6182
6183 user_sp = (struct copan_aps_subpage *)page_ptr;
6184
6185 modepage_info = (union ctl_modepage_info *)
6186 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6187
6188 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK;
6189 modepage_info->header.subpage = page_index->subpage;
6190 modepage_info->aps.lock_active = user_sp->lock_active;
6191
6192 mtx_lock(&softc->ctl_lock);
6193
6194 /*
6195 * If there is a request to lock the LUN and another LUN is locked
6196 * this is an error. If the requested LUN is already locked ignore
6197 * the request. If no LUN is locked attempt to lock it.
6198 * if there is a request to unlock the LUN and the LUN is currently
6199 * locked attempt to unlock it. Otherwise ignore the request. i.e.
6200 * if another LUN is locked or no LUN is locked.
6201 */
6202 if (user_sp->lock_active & APS_LOCK_ACTIVE) {
6203 if (softc->aps_locked_lun == lun->lun) {
6204 /*
6205 * This LUN is already locked, so we're done.
6206 */
6207 retval = CTL_RETVAL_COMPLETE;
6208 } else if (softc->aps_locked_lun == 0) {
6209 /*
6210 * No one has the lock, pass the request to the
6211 * backend.
6212 */
6213 retval = lun->backend->config_write(
6214 (union ctl_io *)ctsio);
6215 } else {
6216 /*
6217 * Someone else has the lock, throw out the request.
6218 */
6219 ctl_set_already_locked(ctsio);
6220 free(ctsio->kern_data_ptr, M_CTL);
6221 ctl_done((union ctl_io *)ctsio);
6222
6223 /*
6224 * Set the return value so that ctl_do_mode_select()
6225 * won't try to complete the command. We already
6226 * completed it here.
6227 */
6228 retval = CTL_RETVAL_ERROR;
6229 }
6230 } else if (softc->aps_locked_lun == lun->lun) {
6231 /*
6232 * This LUN is locked, so pass the unlock request to the
6233 * backend.
6234 */
6235 retval = lun->backend->config_write((union ctl_io *)ctsio);
6236 }
6237 mtx_unlock(&softc->ctl_lock);
6238
6239 return (retval);
6240}
6241
6242int
6243ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
6244 struct ctl_page_index *page_index,
6245 uint8_t *page_ptr)
6246{
6247 uint8_t *c;
6248 int i;
6249
6250 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
6251 ctl_time_io_secs =
6252 (c[0] << 8) |
6253 (c[1] << 0) |
6254 0;
6255 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
6256 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
6257 printf("page data:");
6258 for (i=0; i<8; i++)
6259 printf(" %.2x",page_ptr[i]);
6260 printf("\n");
6261 return (0);
6262}
6263
6264int
6265ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
6266 struct ctl_page_index *page_index,
6267 int pc)
6268{
6269 struct copan_debugconf_subpage *page;
6270
6271 page = (struct copan_debugconf_subpage *)page_index->page_data +
6272 (page_index->page_len * pc);
6273
6274 switch (pc) {
6275 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6276 case SMS_PAGE_CTRL_DEFAULT >> 6:
6277 case SMS_PAGE_CTRL_SAVED >> 6:
6278 /*
6279 * We don't update the changable or default bits for this page.
6280 */
6281 break;
6282 case SMS_PAGE_CTRL_CURRENT >> 6:
6283 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
6284 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
6285 break;
6286 default:
6287#ifdef NEEDTOPORT
6288 EPRINT(0, "Invalid PC %d!!", pc);
6289#endif /* NEEDTOPORT */
6290 break;
6291 }
6292 return (0);
6293}
6294
6295
6296static int
6297ctl_do_mode_select(union ctl_io *io)
6298{
6299 struct scsi_mode_page_header *page_header;
6300 struct ctl_page_index *page_index;
6301 struct ctl_scsiio *ctsio;
6302 int control_dev, page_len;
6303 int page_len_offset, page_len_size;
6304 union ctl_modepage_info *modepage_info;
6305 struct ctl_lun *lun;
6306 int *len_left, *len_used;
6307 int retval, i;
6308
6309 ctsio = &io->scsiio;
6310 page_index = NULL;
6311 page_len = 0;
6312 retval = CTL_RETVAL_COMPLETE;
6313
6314 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6315
6316 if (lun->be_lun->lun_type != T_DIRECT)
6317 control_dev = 1;
6318 else
6319 control_dev = 0;
6320
6321 modepage_info = (union ctl_modepage_info *)
6322 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6323 len_left = &modepage_info->header.len_left;
6324 len_used = &modepage_info->header.len_used;
6325
6326do_next_page:
6327
6328 page_header = (struct scsi_mode_page_header *)
6329 (ctsio->kern_data_ptr + *len_used);
6330
6331 if (*len_left == 0) {
6332 free(ctsio->kern_data_ptr, M_CTL);
6333 ctl_set_success(ctsio);
6334 ctl_done((union ctl_io *)ctsio);
6335 return (CTL_RETVAL_COMPLETE);
6336 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6337
6338 free(ctsio->kern_data_ptr, M_CTL);
6339 ctl_set_param_len_error(ctsio);
6340 ctl_done((union ctl_io *)ctsio);
6341 return (CTL_RETVAL_COMPLETE);
6342
6343 } else if ((page_header->page_code & SMPH_SPF)
6344 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6345
6346 free(ctsio->kern_data_ptr, M_CTL);
6347 ctl_set_param_len_error(ctsio);
6348 ctl_done((union ctl_io *)ctsio);
6349 return (CTL_RETVAL_COMPLETE);
6350 }
6351
6352
6353 /*
6354 * XXX KDM should we do something with the block descriptor?
6355 */
6356 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6357
6358 if ((control_dev != 0)
6359 && (lun->mode_pages.index[i].page_flags &
6360 CTL_PAGE_FLAG_DISK_ONLY))
6361 continue;
6362
6363 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6364 (page_header->page_code & SMPH_PC_MASK))
6365 continue;
6366
6367 /*
6368 * If neither page has a subpage code, then we've got a
6369 * match.
6370 */
6371 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6372 && ((page_header->page_code & SMPH_SPF) == 0)) {
6373 page_index = &lun->mode_pages.index[i];
6374 page_len = page_header->page_length;
6375 break;
6376 }
6377
6378 /*
6379 * If both pages have subpages, then the subpage numbers
6380 * have to match.
6381 */
6382 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6383 && (page_header->page_code & SMPH_SPF)) {
6384 struct scsi_mode_page_header_sp *sph;
6385
6386 sph = (struct scsi_mode_page_header_sp *)page_header;
6387
6388 if (lun->mode_pages.index[i].subpage ==
6389 sph->subpage) {
6390 page_index = &lun->mode_pages.index[i];
6391 page_len = scsi_2btoul(sph->page_length);
6392 break;
6393 }
6394 }
6395 }
6396
6397 /*
6398 * If we couldn't find the page, or if we don't have a mode select
6399 * handler for it, send back an error to the user.
6400 */
6401 if ((page_index == NULL)
6402 || (page_index->select_handler == NULL)) {
6403 ctl_set_invalid_field(ctsio,
6404 /*sks_valid*/ 1,
6405 /*command*/ 0,
6406 /*field*/ *len_used,
6407 /*bit_valid*/ 0,
6408 /*bit*/ 0);
6409 free(ctsio->kern_data_ptr, M_CTL);
6410 ctl_done((union ctl_io *)ctsio);
6411 return (CTL_RETVAL_COMPLETE);
6412 }
6413
6414 if (page_index->page_code & SMPH_SPF) {
6415 page_len_offset = 2;
6416 page_len_size = 2;
6417 } else {
6418 page_len_size = 1;
6419 page_len_offset = 1;
6420 }
6421
6422 /*
6423 * If the length the initiator gives us isn't the one we specify in
6424 * the mode page header, or if they didn't specify enough data in
6425 * the CDB to avoid truncating this page, kick out the request.
6426 */
6427 if ((page_len != (page_index->page_len - page_len_offset -
6428 page_len_size))
6429 || (*len_left < page_index->page_len)) {
6430
6431
6432 ctl_set_invalid_field(ctsio,
6433 /*sks_valid*/ 1,
6434 /*command*/ 0,
6435 /*field*/ *len_used + page_len_offset,
6436 /*bit_valid*/ 0,
6437 /*bit*/ 0);
6438 free(ctsio->kern_data_ptr, M_CTL);
6439 ctl_done((union ctl_io *)ctsio);
6440 return (CTL_RETVAL_COMPLETE);
6441 }
6442
6443 /*
6444 * Run through the mode page, checking to make sure that the bits
6445 * the user changed are actually legal for him to change.
6446 */
6447 for (i = 0; i < page_index->page_len; i++) {
6448 uint8_t *user_byte, *change_mask, *current_byte;
6449 int bad_bit;
6450 int j;
6451
6452 user_byte = (uint8_t *)page_header + i;
6453 change_mask = page_index->page_data +
6454 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6455 current_byte = page_index->page_data +
6456 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6457
6458 /*
6459 * Check to see whether the user set any bits in this byte
6460 * that he is not allowed to set.
6461 */
6462 if ((*user_byte & ~(*change_mask)) ==
6463 (*current_byte & ~(*change_mask)))
6464 continue;
6465
6466 /*
6467 * Go through bit by bit to determine which one is illegal.
6468 */
6469 bad_bit = 0;
6470 for (j = 7; j >= 0; j--) {
6471 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6472 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6473 bad_bit = i;
6474 break;
6475 }
6476 }
6477 ctl_set_invalid_field(ctsio,
6478 /*sks_valid*/ 1,
6479 /*command*/ 0,
6480 /*field*/ *len_used + i,
6481 /*bit_valid*/ 1,
6482 /*bit*/ bad_bit);
6483 free(ctsio->kern_data_ptr, M_CTL);
6484 ctl_done((union ctl_io *)ctsio);
6485 return (CTL_RETVAL_COMPLETE);
6486 }
6487
6488 /*
6489 * Decrement these before we call the page handler, since we may
6490 * end up getting called back one way or another before the handler
6491 * returns to this context.
6492 */
6493 *len_left -= page_index->page_len;
6494 *len_used += page_index->page_len;
6495
6496 retval = page_index->select_handler(ctsio, page_index,
6497 (uint8_t *)page_header);
6498
6499 /*
6500 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6501 * wait until this queued command completes to finish processing
6502 * the mode page. If it returns anything other than
6503 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6504 * already set the sense information, freed the data pointer, and
6505 * completed the io for us.
6506 */
6507 if (retval != CTL_RETVAL_COMPLETE)
6508 goto bailout_no_done;
6509
6510 /*
6511 * If the initiator sent us more than one page, parse the next one.
6512 */
6513 if (*len_left > 0)
6514 goto do_next_page;
6515
6516 ctl_set_success(ctsio);
6517 free(ctsio->kern_data_ptr, M_CTL);
6518 ctl_done((union ctl_io *)ctsio);
6519
6520bailout_no_done:
6521
6522 return (CTL_RETVAL_COMPLETE);
6523
6524}
6525
6526int
6527ctl_mode_select(struct ctl_scsiio *ctsio)
6528{
6529 int param_len, pf, sp;
6530 int header_size, bd_len;
6531 int len_left, len_used;
6532 struct ctl_page_index *page_index;
6533 struct ctl_lun *lun;
6534 int control_dev, page_len;
6535 union ctl_modepage_info *modepage_info;
6536 int retval;
6537
6538 pf = 0;
6539 sp = 0;
6540 page_len = 0;
6541 len_used = 0;
6542 len_left = 0;
6543 retval = 0;
6544 bd_len = 0;
6545 page_index = NULL;
6546
6547 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6548
6549 if (lun->be_lun->lun_type != T_DIRECT)
6550 control_dev = 1;
6551 else
6552 control_dev = 0;
6553
6554 switch (ctsio->cdb[0]) {
6555 case MODE_SELECT_6: {
6556 struct scsi_mode_select_6 *cdb;
6557
6558 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6559
6560 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6561 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6562
6563 param_len = cdb->length;
6564 header_size = sizeof(struct scsi_mode_header_6);
6565 break;
6566 }
6567 case MODE_SELECT_10: {
6568 struct scsi_mode_select_10 *cdb;
6569
6570 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6571
6572 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6573 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6574
6575 param_len = scsi_2btoul(cdb->length);
6576 header_size = sizeof(struct scsi_mode_header_10);
6577 break;
6578 }
6579 default:
6580 ctl_set_invalid_opcode(ctsio);
6581 ctl_done((union ctl_io *)ctsio);
6582 return (CTL_RETVAL_COMPLETE);
6583 break; /* NOTREACHED */
6584 }
6585
6586 /*
6587 * From SPC-3:
6588 * "A parameter list length of zero indicates that the Data-Out Buffer
6589 * shall be empty. This condition shall not be considered as an error."
6590 */
6591 if (param_len == 0) {
6592 ctl_set_success(ctsio);
6593 ctl_done((union ctl_io *)ctsio);
6594 return (CTL_RETVAL_COMPLETE);
6595 }
6596
6597 /*
6598 * Since we'll hit this the first time through, prior to
6599 * allocation, we don't need to free a data buffer here.
6600 */
6601 if (param_len < header_size) {
6602 ctl_set_param_len_error(ctsio);
6603 ctl_done((union ctl_io *)ctsio);
6604 return (CTL_RETVAL_COMPLETE);
6605 }
6606
6607 /*
6608 * Allocate the data buffer and grab the user's data. In theory,
6609 * we shouldn't have to sanity check the parameter list length here
6610 * because the maximum size is 64K. We should be able to malloc
6611 * that much without too many problems.
6612 */
6613 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6614 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6615 ctsio->kern_data_len = param_len;
6616 ctsio->kern_total_len = param_len;
6617 ctsio->kern_data_resid = 0;
6618 ctsio->kern_rel_offset = 0;
6619 ctsio->kern_sg_entries = 0;
6620 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6621 ctsio->be_move_done = ctl_config_move_done;
6622 ctl_datamove((union ctl_io *)ctsio);
6623
6624 return (CTL_RETVAL_COMPLETE);
6625 }
6626
6627 switch (ctsio->cdb[0]) {
6628 case MODE_SELECT_6: {
6629 struct scsi_mode_header_6 *mh6;
6630
6631 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6632 bd_len = mh6->blk_desc_len;
6633 break;
6634 }
6635 case MODE_SELECT_10: {
6636 struct scsi_mode_header_10 *mh10;
6637
6638 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6639 bd_len = scsi_2btoul(mh10->blk_desc_len);
6640 break;
6641 }
6642 default:
6643 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6644 break;
6645 }
6646
6647 if (param_len < (header_size + bd_len)) {
6648 free(ctsio->kern_data_ptr, M_CTL);
6649 ctl_set_param_len_error(ctsio);
6650 ctl_done((union ctl_io *)ctsio);
6651 return (CTL_RETVAL_COMPLETE);
6652 }
6653
6654 /*
6655 * Set the IO_CONT flag, so that if this I/O gets passed to
6656 * ctl_config_write_done(), it'll get passed back to
6657 * ctl_do_mode_select() for further processing, or completion if
6658 * we're all done.
6659 */
6660 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6661 ctsio->io_cont = ctl_do_mode_select;
6662
6663 modepage_info = (union ctl_modepage_info *)
6664 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6665
6666 memset(modepage_info, 0, sizeof(*modepage_info));
6667
6668 len_left = param_len - header_size - bd_len;
6669 len_used = header_size + bd_len;
6670
6671 modepage_info->header.len_left = len_left;
6672 modepage_info->header.len_used = len_used;
6673
6674 return (ctl_do_mode_select((union ctl_io *)ctsio));
6675}
6676
6677int
6678ctl_mode_sense(struct ctl_scsiio *ctsio)
6679{
6680 struct ctl_lun *lun;
6681 int pc, page_code, dbd, llba, subpage;
6682 int alloc_len, page_len, header_len, total_len;
6683 struct scsi_mode_block_descr *block_desc;
6684 struct ctl_page_index *page_index;
6685 int control_dev;
6686
6687 dbd = 0;
6688 llba = 0;
6689 block_desc = NULL;
6690 page_index = NULL;
6691
6692 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6693
6694 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6695
6696 if (lun->be_lun->lun_type != T_DIRECT)
6697 control_dev = 1;
6698 else
6699 control_dev = 0;
6700
6701 switch (ctsio->cdb[0]) {
6702 case MODE_SENSE_6: {
6703 struct scsi_mode_sense_6 *cdb;
6704
6705 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6706
6707 header_len = sizeof(struct scsi_mode_hdr_6);
6708 if (cdb->byte2 & SMS_DBD)
6709 dbd = 1;
6710 else
6711 header_len += sizeof(struct scsi_mode_block_descr);
6712
6713 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6714 page_code = cdb->page & SMS_PAGE_CODE;
6715 subpage = cdb->subpage;
6716 alloc_len = cdb->length;
6717 break;
6718 }
6719 case MODE_SENSE_10: {
6720 struct scsi_mode_sense_10 *cdb;
6721
6722 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6723
6724 header_len = sizeof(struct scsi_mode_hdr_10);
6725
6726 if (cdb->byte2 & SMS_DBD)
6727 dbd = 1;
6728 else
6729 header_len += sizeof(struct scsi_mode_block_descr);
6730 if (cdb->byte2 & SMS10_LLBAA)
6731 llba = 1;
6732 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6733 page_code = cdb->page & SMS_PAGE_CODE;
6734 subpage = cdb->subpage;
6735 alloc_len = scsi_2btoul(cdb->length);
6736 break;
6737 }
6738 default:
6739 ctl_set_invalid_opcode(ctsio);
6740 ctl_done((union ctl_io *)ctsio);
6741 return (CTL_RETVAL_COMPLETE);
6742 break; /* NOTREACHED */
6743 }
6744
6745 /*
6746 * We have to make a first pass through to calculate the size of
6747 * the pages that match the user's query. Then we allocate enough
6748 * memory to hold it, and actually copy the data into the buffer.
6749 */
6750 switch (page_code) {
6751 case SMS_ALL_PAGES_PAGE: {
6752 int i;
6753
6754 page_len = 0;
6755
6756 /*
6757 * At the moment, values other than 0 and 0xff here are
6758 * reserved according to SPC-3.
6759 */
6760 if ((subpage != SMS_SUBPAGE_PAGE_0)
6761 && (subpage != SMS_SUBPAGE_ALL)) {
6762 ctl_set_invalid_field(ctsio,
6763 /*sks_valid*/ 1,
6764 /*command*/ 1,
6765 /*field*/ 3,
6766 /*bit_valid*/ 0,
6767 /*bit*/ 0);
6768 ctl_done((union ctl_io *)ctsio);
6769 return (CTL_RETVAL_COMPLETE);
6770 }
6771
6772 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6773 if ((control_dev != 0)
6774 && (lun->mode_pages.index[i].page_flags &
6775 CTL_PAGE_FLAG_DISK_ONLY))
6776 continue;
6777
6778 /*
6779 * We don't use this subpage if the user didn't
6780 * request all subpages.
6781 */
6782 if ((lun->mode_pages.index[i].subpage != 0)
6783 && (subpage == SMS_SUBPAGE_PAGE_0))
6784 continue;
6785
6786#if 0
6787 printf("found page %#x len %d\n",
6788 lun->mode_pages.index[i].page_code &
6789 SMPH_PC_MASK,
6790 lun->mode_pages.index[i].page_len);
6791#endif
6792 page_len += lun->mode_pages.index[i].page_len;
6793 }
6794 break;
6795 }
6796 default: {
6797 int i;
6798
6799 page_len = 0;
6800
6801 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6802 /* Look for the right page code */
6803 if ((lun->mode_pages.index[i].page_code &
6804 SMPH_PC_MASK) != page_code)
6805 continue;
6806
6807 /* Look for the right subpage or the subpage wildcard*/
6808 if ((lun->mode_pages.index[i].subpage != subpage)
6809 && (subpage != SMS_SUBPAGE_ALL))
6810 continue;
6811
6812 /* Make sure the page is supported for this dev type */
6813 if ((control_dev != 0)
6814 && (lun->mode_pages.index[i].page_flags &
6815 CTL_PAGE_FLAG_DISK_ONLY))
6816 continue;
6817
6818#if 0
6819 printf("found page %#x len %d\n",
6820 lun->mode_pages.index[i].page_code &
6821 SMPH_PC_MASK,
6822 lun->mode_pages.index[i].page_len);
6823#endif
6824
6825 page_len += lun->mode_pages.index[i].page_len;
6826 }
6827
6828 if (page_len == 0) {
6829 ctl_set_invalid_field(ctsio,
6830 /*sks_valid*/ 1,
6831 /*command*/ 1,
6832 /*field*/ 2,
6833 /*bit_valid*/ 1,
6834 /*bit*/ 5);
6835 ctl_done((union ctl_io *)ctsio);
6836 return (CTL_RETVAL_COMPLETE);
6837 }
6838 break;
6839 }
6840 }
6841
6842 total_len = header_len + page_len;
6843#if 0
6844 printf("header_len = %d, page_len = %d, total_len = %d\n",
6845 header_len, page_len, total_len);
6846#endif
6847
6848 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6849 ctsio->kern_sg_entries = 0;
6850 ctsio->kern_data_resid = 0;
6851 ctsio->kern_rel_offset = 0;
6852 if (total_len < alloc_len) {
6853 ctsio->residual = alloc_len - total_len;
6854 ctsio->kern_data_len = total_len;
6855 ctsio->kern_total_len = total_len;
6856 } else {
6857 ctsio->residual = 0;
6858 ctsio->kern_data_len = alloc_len;
6859 ctsio->kern_total_len = alloc_len;
6860 }
6861
6862 switch (ctsio->cdb[0]) {
6863 case MODE_SENSE_6: {
6864 struct scsi_mode_hdr_6 *header;
6865
6866 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6867
6868 header->datalen = ctl_min(total_len - 1, 254);
6869
6870 if (dbd)
6871 header->block_descr_len = 0;
6872 else
6873 header->block_descr_len =
6874 sizeof(struct scsi_mode_block_descr);
6875 block_desc = (struct scsi_mode_block_descr *)&header[1];
6876 break;
6877 }
6878 case MODE_SENSE_10: {
6879 struct scsi_mode_hdr_10 *header;
6880 int datalen;
6881
6882 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6883
6884 datalen = ctl_min(total_len - 2, 65533);
6885 scsi_ulto2b(datalen, header->datalen);
6886 if (dbd)
6887 scsi_ulto2b(0, header->block_descr_len);
6888 else
6889 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6890 header->block_descr_len);
6891 block_desc = (struct scsi_mode_block_descr *)&header[1];
6892 break;
6893 }
6894 default:
6895 panic("invalid CDB type %#x", ctsio->cdb[0]);
6896 break; /* NOTREACHED */
6897 }
6898
6899 /*
6900 * If we've got a disk, use its blocksize in the block
6901 * descriptor. Otherwise, just set it to 0.
6902 */
6903 if (dbd == 0) {
6904 if (control_dev != 0)
6905 scsi_ulto3b(lun->be_lun->blocksize,
6906 block_desc->block_len);
6907 else
6908 scsi_ulto3b(0, block_desc->block_len);
6909 }
6910
6911 switch (page_code) {
6912 case SMS_ALL_PAGES_PAGE: {
6913 int i, data_used;
6914
6915 data_used = header_len;
6916 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6917 struct ctl_page_index *page_index;
6918
6919 page_index = &lun->mode_pages.index[i];
6920
6921 if ((control_dev != 0)
6922 && (page_index->page_flags &
6923 CTL_PAGE_FLAG_DISK_ONLY))
6924 continue;
6925
6926 /*
6927 * We don't use this subpage if the user didn't
6928 * request all subpages. We already checked (above)
6929 * to make sure the user only specified a subpage
6930 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6931 */
6932 if ((page_index->subpage != 0)
6933 && (subpage == SMS_SUBPAGE_PAGE_0))
6934 continue;
6935
6936 /*
6937 * Call the handler, if it exists, to update the
6938 * page to the latest values.
6939 */
6940 if (page_index->sense_handler != NULL)
6941 page_index->sense_handler(ctsio, page_index,pc);
6942
6943 memcpy(ctsio->kern_data_ptr + data_used,
6944 page_index->page_data +
6945 (page_index->page_len * pc),
6946 page_index->page_len);
6947 data_used += page_index->page_len;
6948 }
6949 break;
6950 }
6951 default: {
6952 int i, data_used;
6953
6954 data_used = header_len;
6955
6956 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6957 struct ctl_page_index *page_index;
6958
6959 page_index = &lun->mode_pages.index[i];
6960
6961 /* Look for the right page code */
6962 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6963 continue;
6964
6965 /* Look for the right subpage or the subpage wildcard*/
6966 if ((page_index->subpage != subpage)
6967 && (subpage != SMS_SUBPAGE_ALL))
6968 continue;
6969
6970 /* Make sure the page is supported for this dev type */
6971 if ((control_dev != 0)
6972 && (page_index->page_flags &
6973 CTL_PAGE_FLAG_DISK_ONLY))
6974 continue;
6975
6976 /*
6977 * Call the handler, if it exists, to update the
6978 * page to the latest values.
6979 */
6980 if (page_index->sense_handler != NULL)
6981 page_index->sense_handler(ctsio, page_index,pc);
6982
6983 memcpy(ctsio->kern_data_ptr + data_used,
6984 page_index->page_data +
6985 (page_index->page_len * pc),
6986 page_index->page_len);
6987 data_used += page_index->page_len;
6988 }
6989 break;
6990 }
6991 }
6992
6993 ctsio->scsi_status = SCSI_STATUS_OK;
6994
6995 ctsio->be_move_done = ctl_config_move_done;
6996 ctl_datamove((union ctl_io *)ctsio);
6997
6998 return (CTL_RETVAL_COMPLETE);
6999}
7000
7001int
7002ctl_read_capacity(struct ctl_scsiio *ctsio)
7003{
7004 struct scsi_read_capacity *cdb;
7005 struct scsi_read_capacity_data *data;
7006 struct ctl_lun *lun;
7007 uint32_t lba;
7008
7009 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
7010
7011 cdb = (struct scsi_read_capacity *)ctsio->cdb;
7012
7013 lba = scsi_4btoul(cdb->addr);
7014 if (((cdb->pmi & SRC_PMI) == 0)
7015 && (lba != 0)) {
7016 ctl_set_invalid_field(/*ctsio*/ ctsio,
7017 /*sks_valid*/ 1,
7018 /*command*/ 1,
7019 /*field*/ 2,
7020 /*bit_valid*/ 0,
7021 /*bit*/ 0);
7022 ctl_done((union ctl_io *)ctsio);
7023 return (CTL_RETVAL_COMPLETE);
7024 }
7025
7026 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7027
7028 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7029 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
7030 ctsio->residual = 0;
7031 ctsio->kern_data_len = sizeof(*data);
7032 ctsio->kern_total_len = sizeof(*data);
7033 ctsio->kern_data_resid = 0;
7034 ctsio->kern_rel_offset = 0;
7035 ctsio->kern_sg_entries = 0;
7036
7037 /*
7038 * If the maximum LBA is greater than 0xfffffffe, the user must
7039 * issue a SERVICE ACTION IN (16) command, with the read capacity
7040 * serivce action set.
7041 */
7042 if (lun->be_lun->maxlba > 0xfffffffe)
7043 scsi_ulto4b(0xffffffff, data->addr);
7044 else
7045 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
7046
7047 /*
7048 * XXX KDM this may not be 512 bytes...
7049 */
7050 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7051
7052 ctsio->scsi_status = SCSI_STATUS_OK;
7053
7054 ctsio->be_move_done = ctl_config_move_done;
7055 ctl_datamove((union ctl_io *)ctsio);
7056
7057 return (CTL_RETVAL_COMPLETE);
7058}
7059
7060static int
7061ctl_read_capacity_16(struct ctl_scsiio *ctsio)
7062{
7063 struct scsi_read_capacity_16 *cdb;
7064 struct scsi_read_capacity_data_long *data;
7065 struct ctl_lun *lun;
7066 uint64_t lba;
7067 uint32_t alloc_len;
7068
7069 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7070
7071 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7072
7073 alloc_len = scsi_4btoul(cdb->alloc_len);
7074 lba = scsi_8btou64(cdb->addr);
7075
7076 if ((cdb->reladr & SRC16_PMI)
7077 && (lba != 0)) {
7078 ctl_set_invalid_field(/*ctsio*/ ctsio,
7079 /*sks_valid*/ 1,
7080 /*command*/ 1,
7081 /*field*/ 2,
7082 /*bit_valid*/ 0,
7083 /*bit*/ 0);
7084 ctl_done((union ctl_io *)ctsio);
7085 return (CTL_RETVAL_COMPLETE);
7086 }
7087
7088 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7089
7090 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7091 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7092
7093 if (sizeof(*data) < alloc_len) {
7094 ctsio->residual = alloc_len - sizeof(*data);
7095 ctsio->kern_data_len = sizeof(*data);
7096 ctsio->kern_total_len = sizeof(*data);
7097 } else {
7098 ctsio->residual = 0;
7099 ctsio->kern_data_len = alloc_len;
7100 ctsio->kern_total_len = alloc_len;
7101 }
7102 ctsio->kern_data_resid = 0;
7103 ctsio->kern_rel_offset = 0;
7104 ctsio->kern_sg_entries = 0;
7105
7106 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7107 /* XXX KDM this may not be 512 bytes... */
7108 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7109 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7110 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7111 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7112 data->lalba_lbp[0] |= SRC16_LBPME;
7113
7114 ctsio->scsi_status = SCSI_STATUS_OK;
7115
7116 ctsio->be_move_done = ctl_config_move_done;
7117 ctl_datamove((union ctl_io *)ctsio);
7118
7119 return (CTL_RETVAL_COMPLETE);
7120}
7121
7122int
7123ctl_service_action_in(struct ctl_scsiio *ctsio)
7124{
7125 struct scsi_service_action_in *cdb;
7126 int retval;
7127
7128 CTL_DEBUG_PRINT(("ctl_service_action_in\n"));
7129
7130 cdb = (struct scsi_service_action_in *)ctsio->cdb;
7131
7132 retval = CTL_RETVAL_COMPLETE;
7133
7134 switch (cdb->service_action) {
7135 case SRC16_SERVICE_ACTION:
7136 retval = ctl_read_capacity_16(ctsio);
7137 break;
7138 default:
7139 ctl_set_invalid_field(/*ctsio*/ ctsio,
7140 /*sks_valid*/ 1,
7141 /*command*/ 1,
7142 /*field*/ 1,
7143 /*bit_valid*/ 1,
7144 /*bit*/ 4);
7145 ctl_done((union ctl_io *)ctsio);
7146 break;
7147 }
7148
7149 return (retval);
7150}
7151
7152int
7153ctl_maintenance_in(struct ctl_scsiio *ctsio)
7154{
7155 struct scsi_maintenance_in *cdb;
7156 int retval;
7157 int alloc_len, total_len = 0;
7158 int num_target_port_groups, single;
7159 struct ctl_lun *lun;
7160 struct ctl_softc *softc;
7161 struct scsi_target_group_data *rtg_ptr;
7162 struct scsi_target_port_group_descriptor *tpg_desc_ptr1, *tpg_desc_ptr2;
7163 struct scsi_target_port_descriptor *tp_desc_ptr1_1, *tp_desc_ptr1_2,
7164 *tp_desc_ptr2_1, *tp_desc_ptr2_2;
7165
7166 CTL_DEBUG_PRINT(("ctl_maintenance_in\n"));
7167
7168 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7169 softc = control_softc;
7170 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7171
7172 retval = CTL_RETVAL_COMPLETE;
7173
7174 if ((cdb->byte2 & SERVICE_ACTION_MASK) != SA_RPRT_TRGT_GRP) {
7175 ctl_set_invalid_field(/*ctsio*/ ctsio,
7176 /*sks_valid*/ 1,
7177 /*command*/ 1,
7178 /*field*/ 1,
7179 /*bit_valid*/ 1,
7180 /*bit*/ 4);
7181 ctl_done((union ctl_io *)ctsio);
7182 return(retval);
7183 }
7184
7185 mtx_lock(&softc->ctl_lock);
7186 single = ctl_is_single;
7187 mtx_unlock(&softc->ctl_lock);
7188
7189 if (single)
7190 num_target_port_groups = NUM_TARGET_PORT_GROUPS - 1;
7191 else
7192 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
7193
7194 total_len = sizeof(struct scsi_target_group_data) +
7195 sizeof(struct scsi_target_port_group_descriptor) *
7196 num_target_port_groups +
7197 sizeof(struct scsi_target_port_descriptor) *
7198 NUM_PORTS_PER_GRP * num_target_port_groups;
7199
7200 alloc_len = scsi_4btoul(cdb->length);
7201
7202 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7203
7204 ctsio->kern_sg_entries = 0;
7205
7206 if (total_len < alloc_len) {
7207 ctsio->residual = alloc_len - total_len;
7208 ctsio->kern_data_len = total_len;
7209 ctsio->kern_total_len = total_len;
7210 } else {
7211 ctsio->residual = 0;
7212 ctsio->kern_data_len = alloc_len;
7213 ctsio->kern_total_len = alloc_len;
7214 }
7215 ctsio->kern_data_resid = 0;
7216 ctsio->kern_rel_offset = 0;
7217
7218 rtg_ptr = (struct scsi_target_group_data *)ctsio->kern_data_ptr;
7219
7220 tpg_desc_ptr1 = &rtg_ptr->groups[0];
7221 tp_desc_ptr1_1 = &tpg_desc_ptr1->descriptors[0];
7222 tp_desc_ptr1_2 = (struct scsi_target_port_descriptor *)
7223 &tp_desc_ptr1_1->desc_list[0];
7224
7225 if (single == 0) {
7226 tpg_desc_ptr2 = (struct scsi_target_port_group_descriptor *)
7227 &tp_desc_ptr1_2->desc_list[0];
7228 tp_desc_ptr2_1 = &tpg_desc_ptr2->descriptors[0];
7229 tp_desc_ptr2_2 = (struct scsi_target_port_descriptor *)
7230 &tp_desc_ptr2_1->desc_list[0];
7231 } else {
7232 tpg_desc_ptr2 = NULL;
7233 tp_desc_ptr2_1 = NULL;
7234 tp_desc_ptr2_2 = NULL;
7235 }
7236
7237 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7238 if (single == 0) {
7239 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) {
7240 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7241 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7242 tpg_desc_ptr2->pref_state =
7243 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7244 } else {
7245 tpg_desc_ptr1->pref_state =
7246 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7247 tpg_desc_ptr2->pref_state = TPG_PRIMARY;
7248 }
7249 } else {
7250 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7251 tpg_desc_ptr1->pref_state =
7252 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7253 tpg_desc_ptr2->pref_state = TPG_PRIMARY;
7254 } else {
7255 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7256 tpg_desc_ptr2->pref_state =
7257 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7258 }
7259 }
7260 } else {
7261 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7262 }
7263 tpg_desc_ptr1->support = 0;
7264 tpg_desc_ptr1->target_port_group[1] = 1;
7265 tpg_desc_ptr1->status = TPG_IMPLICIT;
7266 tpg_desc_ptr1->target_port_count= NUM_PORTS_PER_GRP;
7267
7268 if (single == 0) {
7269 tpg_desc_ptr2->support = 0;
7270 tpg_desc_ptr2->target_port_group[1] = 2;
7271 tpg_desc_ptr2->status = TPG_IMPLICIT;
7272 tpg_desc_ptr2->target_port_count = NUM_PORTS_PER_GRP;
7273
7274 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1;
7275 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2;
7276
7277 tp_desc_ptr2_1->relative_target_port_identifier[1] = 9;
7278 tp_desc_ptr2_2->relative_target_port_identifier[1] = 10;
7279 } else {
7280 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) {
7281 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1;
7282 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2;
7283 } else {
7284 tp_desc_ptr1_1->relative_target_port_identifier[1] = 9;
7285 tp_desc_ptr1_2->relative_target_port_identifier[1] = 10;
7286 }
7287 }
7288
7289 ctsio->be_move_done = ctl_config_move_done;
7290
7291 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7292 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7293 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7294 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7295 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7296
7297 ctl_datamove((union ctl_io *)ctsio);
7298 return(retval);
7299}
7300
7301int
7302ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7303{
7304 struct scsi_per_res_in *cdb;
7305 int alloc_len, total_len = 0;
7306 /* struct scsi_per_res_in_rsrv in_data; */
7307 struct ctl_lun *lun;
7308 struct ctl_softc *softc;
7309
7310 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7311
7312 softc = control_softc;
7313
7314 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7315
7316 alloc_len = scsi_2btoul(cdb->length);
7317
7318 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7319
7320retry:
7321 mtx_lock(&softc->ctl_lock);
7322 switch (cdb->action) {
7323 case SPRI_RK: /* read keys */
7324 total_len = sizeof(struct scsi_per_res_in_keys) +
7325 lun->pr_key_count *
7326 sizeof(struct scsi_per_res_key);
7327 break;
7328 case SPRI_RR: /* read reservation */
7329 if (lun->flags & CTL_LUN_PR_RESERVED)
7330 total_len = sizeof(struct scsi_per_res_in_rsrv);
7331 else
7332 total_len = sizeof(struct scsi_per_res_in_header);
7333 break;
7334 case SPRI_RC: /* report capabilities */
7335 total_len = sizeof(struct scsi_per_res_cap);
7336 break;
7337 case SPRI_RS: /* read full status */
7338 default:
7339 mtx_unlock(&softc->ctl_lock);
7340 ctl_set_invalid_field(ctsio,
7341 /*sks_valid*/ 1,
7342 /*command*/ 1,
7343 /*field*/ 1,
7344 /*bit_valid*/ 1,
7345 /*bit*/ 0);
7346 ctl_done((union ctl_io *)ctsio);
7347 return (CTL_RETVAL_COMPLETE);
7348 break; /* NOTREACHED */
7349 }
7350 mtx_unlock(&softc->ctl_lock);
7351
7352 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7353
7354 if (total_len < alloc_len) {
7355 ctsio->residual = alloc_len - total_len;
7356 ctsio->kern_data_len = total_len;
7357 ctsio->kern_total_len = total_len;
7358 } else {
7359 ctsio->residual = 0;
7360 ctsio->kern_data_len = alloc_len;
7361 ctsio->kern_total_len = alloc_len;
7362 }
7363
7364 ctsio->kern_data_resid = 0;
7365 ctsio->kern_rel_offset = 0;
7366 ctsio->kern_sg_entries = 0;
7367
7368 mtx_lock(&softc->ctl_lock);
7369 switch (cdb->action) {
7370 case SPRI_RK: { // read keys
7371 struct scsi_per_res_in_keys *res_keys;
7372 int i, key_count;
7373
7374 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7375
7376 /*
7377 * We had to drop the lock to allocate our buffer, which
7378 * leaves time for someone to come in with another
7379 * persistent reservation. (That is unlikely, though,
7380 * since this should be the only persistent reservation
7381 * command active right now.)
7382 */
7383 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7384 (lun->pr_key_count *
7385 sizeof(struct scsi_per_res_key)))){
7386 mtx_unlock(&softc->ctl_lock);
7387 free(ctsio->kern_data_ptr, M_CTL);
7388 printf("%s: reservation length changed, retrying\n",
7389 __func__);
7390 goto retry;
7391 }
7392
7393 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7394
7395 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7396 lun->pr_key_count, res_keys->header.length);
7397
7398 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7399 if (!lun->per_res[i].registered)
7400 continue;
7401
7402 /*
7403 * We used lun->pr_key_count to calculate the
7404 * size to allocate. If it turns out the number of
7405 * initiators with the registered flag set is
7406 * larger than that (i.e. they haven't been kept in
7407 * sync), we've got a problem.
7408 */
7409 if (key_count >= lun->pr_key_count) {
7410#ifdef NEEDTOPORT
7411 csevent_log(CSC_CTL | CSC_SHELF_SW |
7412 CTL_PR_ERROR,
7413 csevent_LogType_Fault,
7414 csevent_AlertLevel_Yellow,
7415 csevent_FRU_ShelfController,
7416 csevent_FRU_Firmware,
7417 csevent_FRU_Unknown,
7418 "registered keys %d >= key "
7419 "count %d", key_count,
7420 lun->pr_key_count);
7421#endif
7422 key_count++;
7423 continue;
7424 }
7425 memcpy(res_keys->keys[key_count].key,
7426 lun->per_res[i].res_key.key,
7427 ctl_min(sizeof(res_keys->keys[key_count].key),
7428 sizeof(lun->per_res[i].res_key)));
7429 key_count++;
7430 }
7431 break;
7432 }
7433 case SPRI_RR: { // read reservation
7434 struct scsi_per_res_in_rsrv *res;
7435 int tmp_len, header_only;
7436
7437 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7438
7439 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7440
7441 if (lun->flags & CTL_LUN_PR_RESERVED)
7442 {
7443 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7444 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7445 res->header.length);
7446 header_only = 0;
7447 } else {
7448 tmp_len = sizeof(struct scsi_per_res_in_header);
7449 scsi_ulto4b(0, res->header.length);
7450 header_only = 1;
7451 }
7452
7453 /*
7454 * We had to drop the lock to allocate our buffer, which
7455 * leaves time for someone to come in with another
7456 * persistent reservation. (That is unlikely, though,
7457 * since this should be the only persistent reservation
7458 * command active right now.)
7459 */
7460 if (tmp_len != total_len) {
7461 mtx_unlock(&softc->ctl_lock);
7462 free(ctsio->kern_data_ptr, M_CTL);
7463 printf("%s: reservation status changed, retrying\n",
7464 __func__);
7465 goto retry;
7466 }
7467
7468 /*
7469 * No reservation held, so we're done.
7470 */
7471 if (header_only != 0)
7472 break;
7473
7474 /*
7475 * If the registration is an All Registrants type, the key
7476 * is 0, since it doesn't really matter.
7477 */
7478 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7479 memcpy(res->data.reservation,
7480 &lun->per_res[lun->pr_res_idx].res_key,
7481 sizeof(struct scsi_per_res_key));
7482 }
7483 res->data.scopetype = lun->res_type;
7484 break;
7485 }
7486 case SPRI_RC: //report capabilities
7487 {
7488 struct scsi_per_res_cap *res_cap;
7489 uint16_t type_mask;
7490
7491 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7492 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7493 res_cap->flags2 |= SPRI_TMV;
7494 type_mask = SPRI_TM_WR_EX_AR |
7495 SPRI_TM_EX_AC_RO |
7496 SPRI_TM_WR_EX_RO |
7497 SPRI_TM_EX_AC |
7498 SPRI_TM_WR_EX |
7499 SPRI_TM_EX_AC_AR;
7500 scsi_ulto2b(type_mask, res_cap->type_mask);
7501 break;
7502 }
7503 case SPRI_RS: //read full status
7504 default:
7505 /*
7506 * This is a bug, because we just checked for this above,
7507 * and should have returned an error.
7508 */
7509 panic("Invalid PR type %x", cdb->action);
7510 break; /* NOTREACHED */
7511 }
7512 mtx_unlock(&softc->ctl_lock);
7513
7514 ctsio->be_move_done = ctl_config_move_done;
7515
7516 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7517 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7518 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7519 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7520 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7521
7522 ctl_datamove((union ctl_io *)ctsio);
7523
7524 return (CTL_RETVAL_COMPLETE);
7525}
7526
7527/*
7528 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7529 * it should return.
7530 */
7531static int
7532ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7533 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7534 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7535 struct scsi_per_res_out_parms* param)
7536{
7537 union ctl_ha_msg persis_io;
7538 int retval, i;
7539 int isc_retval;
7540
7541 retval = 0;
7542
7543 if (sa_res_key == 0) {
7544 mtx_lock(&softc->ctl_lock);
7545 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7546 /* validate scope and type */
7547 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7548 SPR_LU_SCOPE) {
7549 mtx_unlock(&softc->ctl_lock);
7550 ctl_set_invalid_field(/*ctsio*/ ctsio,
7551 /*sks_valid*/ 1,
7552 /*command*/ 1,
7553 /*field*/ 2,
7554 /*bit_valid*/ 1,
7555 /*bit*/ 4);
7556 ctl_done((union ctl_io *)ctsio);
7557 return (1);
7558 }
7559
7560 if (type>8 || type==2 || type==4 || type==0) {
7561 mtx_unlock(&softc->ctl_lock);
7562 ctl_set_invalid_field(/*ctsio*/ ctsio,
7563 /*sks_valid*/ 1,
7564 /*command*/ 1,
7565 /*field*/ 2,
7566 /*bit_valid*/ 1,
7567 /*bit*/ 0);
7568 ctl_done((union ctl_io *)ctsio);
7569 return (1);
7570 }
7571
7572 /* temporarily unregister this nexus */
7573 lun->per_res[residx].registered = 0;
7574
7575 /*
7576 * Unregister everybody else and build UA for
7577 * them
7578 */
7579 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7580 if (lun->per_res[i].registered == 0)
7581 continue;
7582
7583 if (!persis_offset
7584 && i <CTL_MAX_INITIATORS)
7585 lun->pending_sense[i].ua_pending |=
7586 CTL_UA_REG_PREEMPT;
7587 else if (persis_offset
7588 && i >= persis_offset)
7589 lun->pending_sense[i-persis_offset
7590 ].ua_pending |=
7591 CTL_UA_REG_PREEMPT;
7592 lun->per_res[i].registered = 0;
7593 memset(&lun->per_res[i].res_key, 0,
7594 sizeof(struct scsi_per_res_key));
7595 }
7596 lun->per_res[residx].registered = 1;
7597 lun->pr_key_count = 1;
7598 lun->res_type = type;
7599 if (lun->res_type != SPR_TYPE_WR_EX_AR
7600 && lun->res_type != SPR_TYPE_EX_AC_AR)
7601 lun->pr_res_idx = residx;
7602
7603 mtx_unlock(&softc->ctl_lock);
7604 /* send msg to other side */
7605 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7606 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7607 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7608 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7609 persis_io.pr.pr_info.res_type = type;
7610 memcpy(persis_io.pr.pr_info.sa_res_key,
7611 param->serv_act_res_key,
7612 sizeof(param->serv_act_res_key));
7613 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7614 &persis_io, sizeof(persis_io), 0)) >
7615 CTL_HA_STATUS_SUCCESS) {
7616 printf("CTL:Persis Out error returned "
7617 "from ctl_ha_msg_send %d\n",
7618 isc_retval);
7619 }
7620 } else {
7621 /* not all registrants */
7622 mtx_unlock(&softc->ctl_lock);
7623 free(ctsio->kern_data_ptr, M_CTL);
7624 ctl_set_invalid_field(ctsio,
7625 /*sks_valid*/ 1,
7626 /*command*/ 0,
7627 /*field*/ 8,
7628 /*bit_valid*/ 0,
7629 /*bit*/ 0);
7630 ctl_done((union ctl_io *)ctsio);
7631 return (1);
7632 }
7633 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7634 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7635 int found = 0;
7636
7637 mtx_lock(&softc->ctl_lock);
7638 if (res_key == sa_res_key) {
7639 /* special case */
7640 /*
7641 * The spec implies this is not good but doesn't
7642 * say what to do. There are two choices either
7643 * generate a res conflict or check condition
7644 * with illegal field in parameter data. Since
7645 * that is what is done when the sa_res_key is
7646 * zero I'll take that approach since this has
7647 * to do with the sa_res_key.
7648 */
7649 mtx_unlock(&softc->ctl_lock);
7650 free(ctsio->kern_data_ptr, M_CTL);
7651 ctl_set_invalid_field(ctsio,
7652 /*sks_valid*/ 1,
7653 /*command*/ 0,
7654 /*field*/ 8,
7655 /*bit_valid*/ 0,
7656 /*bit*/ 0);
7657 ctl_done((union ctl_io *)ctsio);
7658 return (1);
7659 }
7660
7661 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7662 if (lun->per_res[i].registered
7663 && memcmp(param->serv_act_res_key,
7664 lun->per_res[i].res_key.key,
7665 sizeof(struct scsi_per_res_key)) != 0)
7666 continue;
7667
7668 found = 1;
7669 lun->per_res[i].registered = 0;
7670 memset(&lun->per_res[i].res_key, 0,
7671 sizeof(struct scsi_per_res_key));
7672 lun->pr_key_count--;
7673
7674 if (!persis_offset
7675 && i < CTL_MAX_INITIATORS)
7676 lun->pending_sense[i].ua_pending |=
7677 CTL_UA_REG_PREEMPT;
7678 else if (persis_offset
7679 && i >= persis_offset)
7680 lun->pending_sense[i-persis_offset].ua_pending|=
7681 CTL_UA_REG_PREEMPT;
7682 }
7683 mtx_unlock(&softc->ctl_lock);
7684 if (!found) {
7685 free(ctsio->kern_data_ptr, M_CTL);
7686 ctl_set_reservation_conflict(ctsio);
7687 ctl_done((union ctl_io *)ctsio);
7688 return (CTL_RETVAL_COMPLETE);
7689 }
7690 /* send msg to other side */
7691 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7692 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7693 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7694 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7695 persis_io.pr.pr_info.res_type = type;
7696 memcpy(persis_io.pr.pr_info.sa_res_key,
7697 param->serv_act_res_key,
7698 sizeof(param->serv_act_res_key));
7699 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7700 &persis_io, sizeof(persis_io), 0)) >
7701 CTL_HA_STATUS_SUCCESS) {
7702 printf("CTL:Persis Out error returned from "
7703 "ctl_ha_msg_send %d\n", isc_retval);
7704 }
7705 } else {
7706 /* Reserved but not all registrants */
7707 /* sa_res_key is res holder */
7708 if (memcmp(param->serv_act_res_key,
7709 lun->per_res[lun->pr_res_idx].res_key.key,
7710 sizeof(struct scsi_per_res_key)) == 0) {
7711 /* validate scope and type */
7712 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7713 SPR_LU_SCOPE) {
7714 ctl_set_invalid_field(/*ctsio*/ ctsio,
7715 /*sks_valid*/ 1,
7716 /*command*/ 1,
7717 /*field*/ 2,
7718 /*bit_valid*/ 1,
7719 /*bit*/ 4);
7720 ctl_done((union ctl_io *)ctsio);
7721 return (1);
7722 }
7723
7724 if (type>8 || type==2 || type==4 || type==0) {
7725 ctl_set_invalid_field(/*ctsio*/ ctsio,
7726 /*sks_valid*/ 1,
7727 /*command*/ 1,
7728 /*field*/ 2,
7729 /*bit_valid*/ 1,
7730 /*bit*/ 0);
7731 ctl_done((union ctl_io *)ctsio);
7732 return (1);
7733 }
7734
7735 /*
7736 * Do the following:
7737 * if sa_res_key != res_key remove all
7738 * registrants w/sa_res_key and generate UA
7739 * for these registrants(Registrations
7740 * Preempted) if it wasn't an exclusive
7741 * reservation generate UA(Reservations
7742 * Preempted) for all other registered nexuses
7743 * if the type has changed. Establish the new
7744 * reservation and holder. If res_key and
7745 * sa_res_key are the same do the above
7746 * except don't unregister the res holder.
7747 */
7748
7749 /*
7750 * Temporarily unregister so it won't get
7751 * removed or UA generated
7752 */
7753 lun->per_res[residx].registered = 0;
7754 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7755 if (lun->per_res[i].registered == 0)
7756 continue;
7757
7758 if (memcmp(param->serv_act_res_key,
7759 lun->per_res[i].res_key.key,
7760 sizeof(struct scsi_per_res_key)) == 0) {
7761 lun->per_res[i].registered = 0;
7762 memset(&lun->per_res[i].res_key,
7763 0,
7764 sizeof(struct scsi_per_res_key));
7765 lun->pr_key_count--;
7766
7767 if (!persis_offset
7768 && i < CTL_MAX_INITIATORS)
7769 lun->pending_sense[i
7770 ].ua_pending |=
7771 CTL_UA_REG_PREEMPT;
7772 else if (persis_offset
7773 && i >= persis_offset)
7774 lun->pending_sense[
7775 i-persis_offset].ua_pending |=
7776 CTL_UA_REG_PREEMPT;
7777 } else if (type != lun->res_type
7778 && (lun->res_type == SPR_TYPE_WR_EX_RO
7779 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
7780 if (!persis_offset
7781 && i < CTL_MAX_INITIATORS)
7782 lun->pending_sense[i
7783 ].ua_pending |=
7784 CTL_UA_RES_RELEASE;
7785 else if (persis_offset
7786 && i >= persis_offset)
7787 lun->pending_sense[
7788 i-persis_offset
7789 ].ua_pending |=
7790 CTL_UA_RES_RELEASE;
7791 }
7792 }
7793 lun->per_res[residx].registered = 1;
7794 lun->res_type = type;
7795 if (lun->res_type != SPR_TYPE_WR_EX_AR
7796 && lun->res_type != SPR_TYPE_EX_AC_AR)
7797 lun->pr_res_idx = residx;
7798 else
7799 lun->pr_res_idx =
7800 CTL_PR_ALL_REGISTRANTS;
7801
7802 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7803 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7804 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7805 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7806 persis_io.pr.pr_info.res_type = type;
7807 memcpy(persis_io.pr.pr_info.sa_res_key,
7808 param->serv_act_res_key,
7809 sizeof(param->serv_act_res_key));
7810 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7811 &persis_io, sizeof(persis_io), 0)) >
7812 CTL_HA_STATUS_SUCCESS) {
7813 printf("CTL:Persis Out error returned "
7814 "from ctl_ha_msg_send %d\n",
7815 isc_retval);
7816 }
7817 } else {
7818 /*
7819 * sa_res_key is not the res holder just
7820 * remove registrants
7821 */
7822 int found=0;
7823 mtx_lock(&softc->ctl_lock);
7824
7825 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7826 if (memcmp(param->serv_act_res_key,
7827 lun->per_res[i].res_key.key,
7828 sizeof(struct scsi_per_res_key)) != 0)
7829 continue;
7830
7831 found = 1;
7832 lun->per_res[i].registered = 0;
7833 memset(&lun->per_res[i].res_key, 0,
7834 sizeof(struct scsi_per_res_key));
7835 lun->pr_key_count--;
7836
7837 if (!persis_offset
7838 && i < CTL_MAX_INITIATORS)
7839 lun->pending_sense[i].ua_pending |=
7840 CTL_UA_REG_PREEMPT;
7841 else if (persis_offset
7842 && i >= persis_offset)
7843 lun->pending_sense[
7844 i-persis_offset].ua_pending |=
7845 CTL_UA_REG_PREEMPT;
7846 }
7847
7848 if (!found) {
7849 mtx_unlock(&softc->ctl_lock);
7850 free(ctsio->kern_data_ptr, M_CTL);
7851 ctl_set_reservation_conflict(ctsio);
7852 ctl_done((union ctl_io *)ctsio);
7853 return (1);
7854 }
7855 mtx_unlock(&softc->ctl_lock);
7856 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7857 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7858 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7859 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7860 persis_io.pr.pr_info.res_type = type;
7861 memcpy(persis_io.pr.pr_info.sa_res_key,
7862 param->serv_act_res_key,
7863 sizeof(param->serv_act_res_key));
7864 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7865 &persis_io, sizeof(persis_io), 0)) >
7866 CTL_HA_STATUS_SUCCESS) {
7867 printf("CTL:Persis Out error returned "
7868 "from ctl_ha_msg_send %d\n",
7869 isc_retval);
7870 }
7871 }
7872 }
7873
7874 lun->PRGeneration++;
7875
7876 return (retval);
7877}
7878
7879static void
7880ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
7881{
7882 int i;
7883
7884 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7885 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
7886 || memcmp(&lun->per_res[lun->pr_res_idx].res_key,
7887 msg->pr.pr_info.sa_res_key,
7888 sizeof(struct scsi_per_res_key)) != 0) {
7889 uint64_t sa_res_key;
7890 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
7891
7892 if (sa_res_key == 0) {
7893 /* temporarily unregister this nexus */
7894 lun->per_res[msg->pr.pr_info.residx].registered = 0;
7895
7896 /*
7897 * Unregister everybody else and build UA for
7898 * them
7899 */
7900 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7901 if (lun->per_res[i].registered == 0)
7902 continue;
7903
7904 if (!persis_offset
7905 && i < CTL_MAX_INITIATORS)
7906 lun->pending_sense[i].ua_pending |=
7907 CTL_UA_REG_PREEMPT;
7908 else if (persis_offset && i >= persis_offset)
7909 lun->pending_sense[i -
7910 persis_offset].ua_pending |=
7911 CTL_UA_REG_PREEMPT;
7912 lun->per_res[i].registered = 0;
7913 memset(&lun->per_res[i].res_key, 0,
7914 sizeof(struct scsi_per_res_key));
7915 }
7916
7917 lun->per_res[msg->pr.pr_info.residx].registered = 1;
7918 lun->pr_key_count = 1;
7919 lun->res_type = msg->pr.pr_info.res_type;
7920 if (lun->res_type != SPR_TYPE_WR_EX_AR
7921 && lun->res_type != SPR_TYPE_EX_AC_AR)
7922 lun->pr_res_idx = msg->pr.pr_info.residx;
7923 } else {
7924 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7925 if (memcmp(msg->pr.pr_info.sa_res_key,
7926 lun->per_res[i].res_key.key,
7927 sizeof(struct scsi_per_res_key)) != 0)
7928 continue;
7929
7930 lun->per_res[i].registered = 0;
7931 memset(&lun->per_res[i].res_key, 0,
7932 sizeof(struct scsi_per_res_key));
7933 lun->pr_key_count--;
7934
7935 if (!persis_offset
7936 && i < persis_offset)
7937 lun->pending_sense[i].ua_pending |=
7938 CTL_UA_REG_PREEMPT;
7939 else if (persis_offset
7940 && i >= persis_offset)
7941 lun->pending_sense[i -
7942 persis_offset].ua_pending |=
7943 CTL_UA_REG_PREEMPT;
7944 }
7945 }
7946 } else {
7947 /*
7948 * Temporarily unregister so it won't get removed
7949 * or UA generated
7950 */
7951 lun->per_res[msg->pr.pr_info.residx].registered = 0;
7952 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7953 if (lun->per_res[i].registered == 0)
7954 continue;
7955
7956 if (memcmp(msg->pr.pr_info.sa_res_key,
7957 lun->per_res[i].res_key.key,
7958 sizeof(struct scsi_per_res_key)) == 0) {
7959 lun->per_res[i].registered = 0;
7960 memset(&lun->per_res[i].res_key, 0,
7961 sizeof(struct scsi_per_res_key));
7962 lun->pr_key_count--;
7963 if (!persis_offset
7964 && i < CTL_MAX_INITIATORS)
7965 lun->pending_sense[i].ua_pending |=
7966 CTL_UA_REG_PREEMPT;
7967 else if (persis_offset
7968 && i >= persis_offset)
7969 lun->pending_sense[i -
7970 persis_offset].ua_pending |=
7971 CTL_UA_REG_PREEMPT;
7972 } else if (msg->pr.pr_info.res_type != lun->res_type
7973 && (lun->res_type == SPR_TYPE_WR_EX_RO
7974 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
7975 if (!persis_offset
7976 && i < persis_offset)
7977 lun->pending_sense[i
7978 ].ua_pending |=
7979 CTL_UA_RES_RELEASE;
7980 else if (persis_offset
7981 && i >= persis_offset)
7982 lun->pending_sense[i -
7983 persis_offset].ua_pending |=
7984 CTL_UA_RES_RELEASE;
7985 }
7986 }
7987 lun->per_res[msg->pr.pr_info.residx].registered = 1;
7988 lun->res_type = msg->pr.pr_info.res_type;
7989 if (lun->res_type != SPR_TYPE_WR_EX_AR
7990 && lun->res_type != SPR_TYPE_EX_AC_AR)
7991 lun->pr_res_idx = msg->pr.pr_info.residx;
7992 else
7993 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
7994 }
7995 lun->PRGeneration++;
7996
7997}
7998
7999
8000int
8001ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8002{
8003 int retval;
8004 int isc_retval;
8005 u_int32_t param_len;
8006 struct scsi_per_res_out *cdb;
8007 struct ctl_lun *lun;
8008 struct scsi_per_res_out_parms* param;
8009 struct ctl_softc *softc;
8010 uint32_t residx;
8011 uint64_t res_key, sa_res_key;
8012 uint8_t type;
8013 union ctl_ha_msg persis_io;
8014 int i;
8015
8016 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8017
8018 retval = CTL_RETVAL_COMPLETE;
8019
8020 softc = control_softc;
8021
8022 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8023 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8024
8025 /*
8026 * We only support whole-LUN scope. The scope & type are ignored for
8027 * register, register and ignore existing key and clear.
8028 * We sometimes ignore scope and type on preempts too!!
8029 * Verify reservation type here as well.
8030 */
8031 type = cdb->scope_type & SPR_TYPE_MASK;
8032 if ((cdb->action == SPRO_RESERVE)
8033 || (cdb->action == SPRO_RELEASE)) {
8034 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8035 ctl_set_invalid_field(/*ctsio*/ ctsio,
8036 /*sks_valid*/ 1,
8037 /*command*/ 1,
8038 /*field*/ 2,
8039 /*bit_valid*/ 1,
8040 /*bit*/ 4);
8041 ctl_done((union ctl_io *)ctsio);
8042 return (CTL_RETVAL_COMPLETE);
8043 }
8044
8045 if (type>8 || type==2 || type==4 || type==0) {
8046 ctl_set_invalid_field(/*ctsio*/ ctsio,
8047 /*sks_valid*/ 1,
8048 /*command*/ 1,
8049 /*field*/ 2,
8050 /*bit_valid*/ 1,
8051 /*bit*/ 0);
8052 ctl_done((union ctl_io *)ctsio);
8053 return (CTL_RETVAL_COMPLETE);
8054 }
8055 }
8056
8057 switch (cdb->action & SPRO_ACTION_MASK) {
8058 case SPRO_REGISTER:
8059 case SPRO_RESERVE:
8060 case SPRO_RELEASE:
8061 case SPRO_CLEAR:
8062 case SPRO_PREEMPT:
8063 case SPRO_REG_IGNO:
8064 break;
8065 case SPRO_REG_MOVE:
8066 case SPRO_PRE_ABO:
8067 default:
8068 ctl_set_invalid_field(/*ctsio*/ ctsio,
8069 /*sks_valid*/ 1,
8070 /*command*/ 1,
8071 /*field*/ 1,
8072 /*bit_valid*/ 1,
8073 /*bit*/ 0);
8074 ctl_done((union ctl_io *)ctsio);
8075 return (CTL_RETVAL_COMPLETE);
8076 break; /* NOTREACHED */
8077 }
8078
8079 param_len = scsi_4btoul(cdb->length);
8080
8081 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8082 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8083 ctsio->kern_data_len = param_len;
8084 ctsio->kern_total_len = param_len;
8085 ctsio->kern_data_resid = 0;
8086 ctsio->kern_rel_offset = 0;
8087 ctsio->kern_sg_entries = 0;
8088 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8089 ctsio->be_move_done = ctl_config_move_done;
8090 ctl_datamove((union ctl_io *)ctsio);
8091
8092 return (CTL_RETVAL_COMPLETE);
8093 }
8094
8095 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8096
8097 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8098 res_key = scsi_8btou64(param->res_key.key);
8099 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8100
8101 /*
8102 * Validate the reservation key here except for SPRO_REG_IGNO
8103 * This must be done for all other service actions
8104 */
8105 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8106 mtx_lock(&softc->ctl_lock);
8107 if (lun->per_res[residx].registered) {
8108 if (memcmp(param->res_key.key,
8109 lun->per_res[residx].res_key.key,
8110 ctl_min(sizeof(param->res_key),
8111 sizeof(lun->per_res[residx].res_key))) != 0) {
8112 /*
8113 * The current key passed in doesn't match
8114 * the one the initiator previously
8115 * registered.
8116 */
8117 mtx_unlock(&softc->ctl_lock);
8118 free(ctsio->kern_data_ptr, M_CTL);
8119 ctl_set_reservation_conflict(ctsio);
8120 ctl_done((union ctl_io *)ctsio);
8121 return (CTL_RETVAL_COMPLETE);
8122 }
8123 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8124 /*
8125 * We are not registered
8126 */
8127 mtx_unlock(&softc->ctl_lock);
8128 free(ctsio->kern_data_ptr, M_CTL);
8129 ctl_set_reservation_conflict(ctsio);
8130 ctl_done((union ctl_io *)ctsio);
8131 return (CTL_RETVAL_COMPLETE);
8132 } else if (res_key != 0) {
8133 /*
8134 * We are not registered and trying to register but
8135 * the register key isn't zero.
8136 */
8137 mtx_unlock(&softc->ctl_lock);
8138 free(ctsio->kern_data_ptr, M_CTL);
8139 ctl_set_reservation_conflict(ctsio);
8140 ctl_done((union ctl_io *)ctsio);
8141 return (CTL_RETVAL_COMPLETE);
8142 }
8143 mtx_unlock(&softc->ctl_lock);
8144 }
8145
8146 switch (cdb->action & SPRO_ACTION_MASK) {
8147 case SPRO_REGISTER:
8148 case SPRO_REG_IGNO: {
8149
8150#if 0
8151 printf("Registration received\n");
8152#endif
8153
8154 /*
8155 * We don't support any of these options, as we report in
8156 * the read capabilities request (see
8157 * ctl_persistent_reserve_in(), above).
8158 */
8159 if ((param->flags & SPR_SPEC_I_PT)
8160 || (param->flags & SPR_ALL_TG_PT)
8161 || (param->flags & SPR_APTPL)) {
8162 int bit_ptr;
8163
8164 if (param->flags & SPR_APTPL)
8165 bit_ptr = 0;
8166 else if (param->flags & SPR_ALL_TG_PT)
8167 bit_ptr = 2;
8168 else /* SPR_SPEC_I_PT */
8169 bit_ptr = 3;
8170
8171 free(ctsio->kern_data_ptr, M_CTL);
8172 ctl_set_invalid_field(ctsio,
8173 /*sks_valid*/ 1,
8174 /*command*/ 0,
8175 /*field*/ 20,
8176 /*bit_valid*/ 1,
8177 /*bit*/ bit_ptr);
8178 ctl_done((union ctl_io *)ctsio);
8179 return (CTL_RETVAL_COMPLETE);
8180 }
8181
8182 mtx_lock(&softc->ctl_lock);
8183
8184 /*
8185 * The initiator wants to clear the
8186 * key/unregister.
8187 */
8188 if (sa_res_key == 0) {
8189 if ((res_key == 0
8190 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8191 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8192 && !lun->per_res[residx].registered)) {
8193 mtx_unlock(&softc->ctl_lock);
8194 goto done;
8195 }
8196
8197 lun->per_res[residx].registered = 0;
8198 memset(&lun->per_res[residx].res_key,
8199 0, sizeof(lun->per_res[residx].res_key));
8200 lun->pr_key_count--;
8201
8202 if (residx == lun->pr_res_idx) {
8203 lun->flags &= ~CTL_LUN_PR_RESERVED;
8204 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8205
8206 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8207 || lun->res_type == SPR_TYPE_EX_AC_RO)
8208 && lun->pr_key_count) {
8209 /*
8210 * If the reservation is a registrants
8211 * only type we need to generate a UA
8212 * for other registered inits. The
8213 * sense code should be RESERVATIONS
8214 * RELEASED
8215 */
8216
8217 for (i = 0; i < CTL_MAX_INITIATORS;i++){
8218 if (lun->per_res[
8219 i+persis_offset].registered
8220 == 0)
8221 continue;
8222 lun->pending_sense[i
8223 ].ua_pending |=
8224 CTL_UA_RES_RELEASE;
8225 }
8226 }
8227 lun->res_type = 0;
8228 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8229 if (lun->pr_key_count==0) {
8230 lun->flags &= ~CTL_LUN_PR_RESERVED;
8231 lun->res_type = 0;
8232 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8233 }
8234 }
8235 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8236 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8237 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8238 persis_io.pr.pr_info.residx = residx;
8239 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8240 &persis_io, sizeof(persis_io), 0 )) >
8241 CTL_HA_STATUS_SUCCESS) {
8242 printf("CTL:Persis Out error returned from "
8243 "ctl_ha_msg_send %d\n", isc_retval);
8244 }
8245 mtx_unlock(&softc->ctl_lock);
8246 } else /* sa_res_key != 0 */ {
8247
8248 /*
8249 * If we aren't registered currently then increment
8250 * the key count and set the registered flag.
8251 */
8252 if (!lun->per_res[residx].registered) {
8253 lun->pr_key_count++;
8254 lun->per_res[residx].registered = 1;
8255 }
8256
8257 memcpy(&lun->per_res[residx].res_key,
8258 param->serv_act_res_key,
8259 ctl_min(sizeof(param->serv_act_res_key),
8260 sizeof(lun->per_res[residx].res_key)));
8261
8262 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8263 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8264 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8265 persis_io.pr.pr_info.residx = residx;
8266 memcpy(persis_io.pr.pr_info.sa_res_key,
8267 param->serv_act_res_key,
8268 sizeof(param->serv_act_res_key));
8269 mtx_unlock(&softc->ctl_lock);
8270 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8271 &persis_io, sizeof(persis_io), 0)) >
8272 CTL_HA_STATUS_SUCCESS) {
8273 printf("CTL:Persis Out error returned from "
8274 "ctl_ha_msg_send %d\n", isc_retval);
8275 }
8276 }
8277 lun->PRGeneration++;
8278
8279 break;
8280 }
8281 case SPRO_RESERVE:
8282#if 0
8283 printf("Reserve executed type %d\n", type);
8284#endif
8285 mtx_lock(&softc->ctl_lock);
8286 if (lun->flags & CTL_LUN_PR_RESERVED) {
8287 /*
8288 * if this isn't the reservation holder and it's
8289 * not a "all registrants" type or if the type is
8290 * different then we have a conflict
8291 */
8292 if ((lun->pr_res_idx != residx
8293 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8294 || lun->res_type != type) {
8295 mtx_unlock(&softc->ctl_lock);
8296 free(ctsio->kern_data_ptr, M_CTL);
8297 ctl_set_reservation_conflict(ctsio);
8298 ctl_done((union ctl_io *)ctsio);
8299 return (CTL_RETVAL_COMPLETE);
8300 }
8301 mtx_unlock(&softc->ctl_lock);
8302 } else /* create a reservation */ {
8303 /*
8304 * If it's not an "all registrants" type record
8305 * reservation holder
8306 */
8307 if (type != SPR_TYPE_WR_EX_AR
8308 && type != SPR_TYPE_EX_AC_AR)
8309 lun->pr_res_idx = residx; /* Res holder */
8310 else
8311 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8312
8313 lun->flags |= CTL_LUN_PR_RESERVED;
8314 lun->res_type = type;
8315
8316 mtx_unlock(&softc->ctl_lock);
8317
8318 /* send msg to other side */
8319 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8320 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8321 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8322 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8323 persis_io.pr.pr_info.res_type = type;
8324 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8325 &persis_io, sizeof(persis_io), 0)) >
8326 CTL_HA_STATUS_SUCCESS) {
8327 printf("CTL:Persis Out error returned from "
8328 "ctl_ha_msg_send %d\n", isc_retval);
8329 }
8330 }
8331 break;
8332
8333 case SPRO_RELEASE:
8334 mtx_lock(&softc->ctl_lock);
8335 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8336 /* No reservation exists return good status */
8337 mtx_unlock(&softc->ctl_lock);
8338 goto done;
8339 }
8340 /*
8341 * Is this nexus a reservation holder?
8342 */
8343 if (lun->pr_res_idx != residx
8344 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8345 /*
8346 * not a res holder return good status but
8347 * do nothing
8348 */
8349 mtx_unlock(&softc->ctl_lock);
8350 goto done;
8351 }
8352
8353 if (lun->res_type != type) {
8354 mtx_unlock(&softc->ctl_lock);
8355 free(ctsio->kern_data_ptr, M_CTL);
8356 ctl_set_illegal_pr_release(ctsio);
8357 ctl_done((union ctl_io *)ctsio);
8358 return (CTL_RETVAL_COMPLETE);
8359 }
8360
8361 /* okay to release */
8362 lun->flags &= ~CTL_LUN_PR_RESERVED;
8363 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8364 lun->res_type = 0;
8365
8366 /*
8367 * if this isn't an exclusive access
8368 * res generate UA for all other
8369 * registrants.
8370 */
8371 if (type != SPR_TYPE_EX_AC
8372 && type != SPR_TYPE_WR_EX) {
8373 /*
8374 * temporarily unregister so we don't generate UA
8375 */
8376 lun->per_res[residx].registered = 0;
8377
8378 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8379 if (lun->per_res[i+persis_offset].registered
8380 == 0)
8381 continue;
8382 lun->pending_sense[i].ua_pending |=
8383 CTL_UA_RES_RELEASE;
8384 }
8385
8386 lun->per_res[residx].registered = 1;
8387 }
8388 mtx_unlock(&softc->ctl_lock);
8389 /* Send msg to other side */
8390 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8391 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8392 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8393 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io,
8394 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8395 printf("CTL:Persis Out error returned from "
8396 "ctl_ha_msg_send %d\n", isc_retval);
8397 }
8398 break;
8399
8400 case SPRO_CLEAR:
8401 /* send msg to other side */
8402
8403 mtx_lock(&softc->ctl_lock);
8404 lun->flags &= ~CTL_LUN_PR_RESERVED;
8405 lun->res_type = 0;
8406 lun->pr_key_count = 0;
8407 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8408
8409
8410 memset(&lun->per_res[residx].res_key,
8411 0, sizeof(lun->per_res[residx].res_key));
8412 lun->per_res[residx].registered = 0;
8413
8414 for (i=0; i < 2*CTL_MAX_INITIATORS; i++)
8415 if (lun->per_res[i].registered) {
8416 if (!persis_offset && i < CTL_MAX_INITIATORS)
8417 lun->pending_sense[i].ua_pending |=
8418 CTL_UA_RES_PREEMPT;
8419 else if (persis_offset && i >= persis_offset)
8420 lun->pending_sense[i-persis_offset
8421 ].ua_pending |= CTL_UA_RES_PREEMPT;
8422
8423 memset(&lun->per_res[i].res_key,
8424 0, sizeof(struct scsi_per_res_key));
8425 lun->per_res[i].registered = 0;
8426 }
8427 lun->PRGeneration++;
8428 mtx_unlock(&softc->ctl_lock);
8429 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8430 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8431 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8432 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8433 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8434 printf("CTL:Persis Out error returned from "
8435 "ctl_ha_msg_send %d\n", isc_retval);
8436 }
8437 break;
8438
8439 case SPRO_PREEMPT: {
8440 int nretval;
8441
8442 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8443 residx, ctsio, cdb, param);
8444 if (nretval != 0)
8445 return (CTL_RETVAL_COMPLETE);
8446 break;
8447 }
8448 case SPRO_REG_MOVE:
8449 case SPRO_PRE_ABO:
8450 default:
8451 free(ctsio->kern_data_ptr, M_CTL);
8452 ctl_set_invalid_field(/*ctsio*/ ctsio,
8453 /*sks_valid*/ 1,
8454 /*command*/ 1,
8455 /*field*/ 1,
8456 /*bit_valid*/ 1,
8457 /*bit*/ 0);
8458 ctl_done((union ctl_io *)ctsio);
8459 return (CTL_RETVAL_COMPLETE);
8460 break; /* NOTREACHED */
8461 }
8462
8463done:
8464 free(ctsio->kern_data_ptr, M_CTL);
8465 ctl_set_success(ctsio);
8466 ctl_done((union ctl_io *)ctsio);
8467
8468 return (retval);
8469}
8470
8471/*
8472 * This routine is for handling a message from the other SC pertaining to
8473 * persistent reserve out. All the error checking will have been done
8474 * so only perorming the action need be done here to keep the two
8475 * in sync.
8476 */
8477static void
8478ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8479{
8480 struct ctl_lun *lun;
8481 struct ctl_softc *softc;
8482 int i;
8483 uint32_t targ_lun;
8484
8485 softc = control_softc;
8486
8487 mtx_lock(&softc->ctl_lock);
8488
8489 targ_lun = msg->hdr.nexus.targ_lun;
8490 if (msg->hdr.nexus.lun_map_fn != NULL)
8491 targ_lun = msg->hdr.nexus.lun_map_fn(msg->hdr.nexus.lun_map_arg, targ_lun);
8492 lun = softc->ctl_luns[targ_lun];
8493 switch(msg->pr.pr_info.action) {
8494 case CTL_PR_REG_KEY:
8495 if (!lun->per_res[msg->pr.pr_info.residx].registered) {
8496 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8497 lun->pr_key_count++;
8498 }
8499 lun->PRGeneration++;
8500 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key,
8501 msg->pr.pr_info.sa_res_key,
8502 sizeof(struct scsi_per_res_key));
8503 break;
8504
8505 case CTL_PR_UNREG_KEY:
8506 lun->per_res[msg->pr.pr_info.residx].registered = 0;
8507 memset(&lun->per_res[msg->pr.pr_info.residx].res_key,
8508 0, sizeof(struct scsi_per_res_key));
8509 lun->pr_key_count--;
8510
8511 /* XXX Need to see if the reservation has been released */
8512 /* if so do we need to generate UA? */
8513 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8514 lun->flags &= ~CTL_LUN_PR_RESERVED;
8515 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8516
8517 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8518 || lun->res_type == SPR_TYPE_EX_AC_RO)
8519 && lun->pr_key_count) {
8520 /*
8521 * If the reservation is a registrants
8522 * only type we need to generate a UA
8523 * for other registered inits. The
8524 * sense code should be RESERVATIONS
8525 * RELEASED
8526 */
8527
8528 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8529 if (lun->per_res[i+
8530 persis_offset].registered == 0)
8531 continue;
8532
8533 lun->pending_sense[i
8534 ].ua_pending |=
8535 CTL_UA_RES_RELEASE;
8536 }
8537 }
8538 lun->res_type = 0;
8539 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8540 if (lun->pr_key_count==0) {
8541 lun->flags &= ~CTL_LUN_PR_RESERVED;
8542 lun->res_type = 0;
8543 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8544 }
8545 }
8546 lun->PRGeneration++;
8547 break;
8548
8549 case CTL_PR_RESERVE:
8550 lun->flags |= CTL_LUN_PR_RESERVED;
8551 lun->res_type = msg->pr.pr_info.res_type;
8552 lun->pr_res_idx = msg->pr.pr_info.residx;
8553
8554 break;
8555
8556 case CTL_PR_RELEASE:
8557 /*
8558 * if this isn't an exclusive access res generate UA for all
8559 * other registrants.
8560 */
8561 if (lun->res_type != SPR_TYPE_EX_AC
8562 && lun->res_type != SPR_TYPE_WR_EX) {
8563 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8564 if (lun->per_res[i+persis_offset].registered)
8565 lun->pending_sense[i].ua_pending |=
8566 CTL_UA_RES_RELEASE;
8567 }
8568
8569 lun->flags &= ~CTL_LUN_PR_RESERVED;
8570 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8571 lun->res_type = 0;
8572 break;
8573
8574 case CTL_PR_PREEMPT:
8575 ctl_pro_preempt_other(lun, msg);
8576 break;
8577 case CTL_PR_CLEAR:
8578 lun->flags &= ~CTL_LUN_PR_RESERVED;
8579 lun->res_type = 0;
8580 lun->pr_key_count = 0;
8581 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8582
8583 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8584 if (lun->per_res[i].registered == 0)
8585 continue;
8586 if (!persis_offset
8587 && i < CTL_MAX_INITIATORS)
8588 lun->pending_sense[i].ua_pending |=
8589 CTL_UA_RES_PREEMPT;
8590 else if (persis_offset
8591 && i >= persis_offset)
8592 lun->pending_sense[i-persis_offset].ua_pending|=
8593 CTL_UA_RES_PREEMPT;
8594 memset(&lun->per_res[i].res_key, 0,
8595 sizeof(struct scsi_per_res_key));
8596 lun->per_res[i].registered = 0;
8597 }
8598 lun->PRGeneration++;
8599 break;
8600 }
8601
8602 mtx_unlock(&softc->ctl_lock);
8603}
8604
8605int
8606ctl_read_write(struct ctl_scsiio *ctsio)
8607{
8608 struct ctl_lun *lun;
8585 struct ctl_lba_len lbalen;
8609 struct ctl_lba_len_flags *lbalen;
8586 uint64_t lba;
8587 uint32_t num_blocks;
8588 int reladdr, fua, dpo, ebp;
8589 int retval;
8590 int isread;
8591
8592 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8593
8594 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8595
8596 reladdr = 0;
8597 fua = 0;
8598 dpo = 0;
8599 ebp = 0;
8600
8601 retval = CTL_RETVAL_COMPLETE;
8602
8603 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8604 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8605 if (lun->flags & CTL_LUN_PR_RESERVED && isread) {
8606 uint32_t residx;
8607
8608 /*
8609 * XXX KDM need a lock here.
8610 */
8611 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8612 if ((lun->res_type == SPR_TYPE_EX_AC
8613 && residx != lun->pr_res_idx)
8614 || ((lun->res_type == SPR_TYPE_EX_AC_RO
8615 || lun->res_type == SPR_TYPE_EX_AC_AR)
8616 && !lun->per_res[residx].registered)) {
8617 ctl_set_reservation_conflict(ctsio);
8618 ctl_done((union ctl_io *)ctsio);
8619 return (CTL_RETVAL_COMPLETE);
8620 }
8621 }
8622
8623 switch (ctsio->cdb[0]) {
8624 case READ_6:
8625 case WRITE_6: {
8626 struct scsi_rw_6 *cdb;
8627
8628 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8629
8630 lba = scsi_3btoul(cdb->addr);
8631 /* only 5 bits are valid in the most significant address byte */
8632 lba &= 0x1fffff;
8633 num_blocks = cdb->length;
8634 /*
8635 * This is correct according to SBC-2.
8636 */
8637 if (num_blocks == 0)
8638 num_blocks = 256;
8639 break;
8640 }
8641 case READ_10:
8642 case WRITE_10: {
8643 struct scsi_rw_10 *cdb;
8644
8645 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8646
8647 if (cdb->byte2 & SRW10_RELADDR)
8648 reladdr = 1;
8649 if (cdb->byte2 & SRW10_FUA)
8650 fua = 1;
8651 if (cdb->byte2 & SRW10_DPO)
8652 dpo = 1;
8653
8654 if ((cdb->opcode == WRITE_10)
8655 && (cdb->byte2 & SRW10_EBP))
8656 ebp = 1;
8657
8658 lba = scsi_4btoul(cdb->addr);
8659 num_blocks = scsi_2btoul(cdb->length);
8660 break;
8661 }
8662 case WRITE_VERIFY_10: {
8663 struct scsi_write_verify_10 *cdb;
8664
8665 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8666
8667 /*
8668 * XXX KDM we should do actual write verify support at some
8669 * point. This is obviously fake, we're just translating
8670 * things to a write. So we don't even bother checking the
8671 * BYTCHK field, since we don't do any verification. If
8672 * the user asks for it, we'll just pretend we did it.
8673 */
8674 if (cdb->byte2 & SWV_DPO)
8675 dpo = 1;
8676
8677 lba = scsi_4btoul(cdb->addr);
8678 num_blocks = scsi_2btoul(cdb->length);
8679 break;
8680 }
8681 case READ_12:
8682 case WRITE_12: {
8683 struct scsi_rw_12 *cdb;
8684
8685 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8686
8687 if (cdb->byte2 & SRW12_RELADDR)
8688 reladdr = 1;
8689 if (cdb->byte2 & SRW12_FUA)
8690 fua = 1;
8691 if (cdb->byte2 & SRW12_DPO)
8692 dpo = 1;
8693 lba = scsi_4btoul(cdb->addr);
8694 num_blocks = scsi_4btoul(cdb->length);
8695 break;
8696 }
8697 case WRITE_VERIFY_12: {
8698 struct scsi_write_verify_12 *cdb;
8699
8700 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8701
8702 if (cdb->byte2 & SWV_DPO)
8703 dpo = 1;
8704
8705 lba = scsi_4btoul(cdb->addr);
8706 num_blocks = scsi_4btoul(cdb->length);
8707
8708 break;
8709 }
8710 case READ_16:
8711 case WRITE_16: {
8712 struct scsi_rw_16 *cdb;
8713
8714 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8715
8716 if (cdb->byte2 & SRW12_RELADDR)
8717 reladdr = 1;
8718 if (cdb->byte2 & SRW12_FUA)
8719 fua = 1;
8720 if (cdb->byte2 & SRW12_DPO)
8721 dpo = 1;
8722
8723 lba = scsi_8btou64(cdb->addr);
8724 num_blocks = scsi_4btoul(cdb->length);
8725 break;
8726 }
8727 case WRITE_VERIFY_16: {
8728 struct scsi_write_verify_16 *cdb;
8729
8730 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8731
8732 if (cdb->byte2 & SWV_DPO)
8733 dpo = 1;
8734
8735 lba = scsi_8btou64(cdb->addr);
8736 num_blocks = scsi_4btoul(cdb->length);
8737 break;
8738 }
8739 default:
8740 /*
8741 * We got a command we don't support. This shouldn't
8742 * happen, commands should be filtered out above us.
8743 */
8744 ctl_set_invalid_opcode(ctsio);
8745 ctl_done((union ctl_io *)ctsio);
8746
8747 return (CTL_RETVAL_COMPLETE);
8748 break; /* NOTREACHED */
8749 }
8750
8751 /*
8752 * XXX KDM what do we do with the DPO and FUA bits? FUA might be
8753 * interesting for us, but if RAIDCore is in write-back mode,
8754 * getting it to do write-through for a particular transaction may
8755 * not be possible.
8756 */
8757 /*
8758 * We don't support relative addressing. That also requires
8759 * supporting linked commands, which we don't do.
8760 */
8761 if (reladdr != 0) {
8762 ctl_set_invalid_field(ctsio,
8763 /*sks_valid*/ 1,
8764 /*command*/ 1,
8765 /*field*/ 1,
8766 /*bit_valid*/ 1,
8767 /*bit*/ 0);
8768 ctl_done((union ctl_io *)ctsio);
8769 return (CTL_RETVAL_COMPLETE);
8770 }
8771
8772 /*
8773 * The first check is to make sure we're in bounds, the second
8774 * check is to catch wrap-around problems. If the lba + num blocks
8775 * is less than the lba, then we've wrapped around and the block
8776 * range is invalid anyway.
8777 */
8778 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8779 || ((lba + num_blocks) < lba)) {
8780 ctl_set_lba_out_of_range(ctsio);
8781 ctl_done((union ctl_io *)ctsio);
8782 return (CTL_RETVAL_COMPLETE);
8783 }
8784
8785 /*
8786 * According to SBC-3, a transfer length of 0 is not an error.
8787 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8788 * translates to 256 blocks for those commands.
8789 */
8790 if (num_blocks == 0) {
8791 ctl_set_success(ctsio);
8792 ctl_done((union ctl_io *)ctsio);
8793 return (CTL_RETVAL_COMPLETE);
8794 }
8795
8610 uint64_t lba;
8611 uint32_t num_blocks;
8612 int reladdr, fua, dpo, ebp;
8613 int retval;
8614 int isread;
8615
8616 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8617
8618 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8619
8620 reladdr = 0;
8621 fua = 0;
8622 dpo = 0;
8623 ebp = 0;
8624
8625 retval = CTL_RETVAL_COMPLETE;
8626
8627 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8628 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8629 if (lun->flags & CTL_LUN_PR_RESERVED && isread) {
8630 uint32_t residx;
8631
8632 /*
8633 * XXX KDM need a lock here.
8634 */
8635 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8636 if ((lun->res_type == SPR_TYPE_EX_AC
8637 && residx != lun->pr_res_idx)
8638 || ((lun->res_type == SPR_TYPE_EX_AC_RO
8639 || lun->res_type == SPR_TYPE_EX_AC_AR)
8640 && !lun->per_res[residx].registered)) {
8641 ctl_set_reservation_conflict(ctsio);
8642 ctl_done((union ctl_io *)ctsio);
8643 return (CTL_RETVAL_COMPLETE);
8644 }
8645 }
8646
8647 switch (ctsio->cdb[0]) {
8648 case READ_6:
8649 case WRITE_6: {
8650 struct scsi_rw_6 *cdb;
8651
8652 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8653
8654 lba = scsi_3btoul(cdb->addr);
8655 /* only 5 bits are valid in the most significant address byte */
8656 lba &= 0x1fffff;
8657 num_blocks = cdb->length;
8658 /*
8659 * This is correct according to SBC-2.
8660 */
8661 if (num_blocks == 0)
8662 num_blocks = 256;
8663 break;
8664 }
8665 case READ_10:
8666 case WRITE_10: {
8667 struct scsi_rw_10 *cdb;
8668
8669 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8670
8671 if (cdb->byte2 & SRW10_RELADDR)
8672 reladdr = 1;
8673 if (cdb->byte2 & SRW10_FUA)
8674 fua = 1;
8675 if (cdb->byte2 & SRW10_DPO)
8676 dpo = 1;
8677
8678 if ((cdb->opcode == WRITE_10)
8679 && (cdb->byte2 & SRW10_EBP))
8680 ebp = 1;
8681
8682 lba = scsi_4btoul(cdb->addr);
8683 num_blocks = scsi_2btoul(cdb->length);
8684 break;
8685 }
8686 case WRITE_VERIFY_10: {
8687 struct scsi_write_verify_10 *cdb;
8688
8689 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8690
8691 /*
8692 * XXX KDM we should do actual write verify support at some
8693 * point. This is obviously fake, we're just translating
8694 * things to a write. So we don't even bother checking the
8695 * BYTCHK field, since we don't do any verification. If
8696 * the user asks for it, we'll just pretend we did it.
8697 */
8698 if (cdb->byte2 & SWV_DPO)
8699 dpo = 1;
8700
8701 lba = scsi_4btoul(cdb->addr);
8702 num_blocks = scsi_2btoul(cdb->length);
8703 break;
8704 }
8705 case READ_12:
8706 case WRITE_12: {
8707 struct scsi_rw_12 *cdb;
8708
8709 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8710
8711 if (cdb->byte2 & SRW12_RELADDR)
8712 reladdr = 1;
8713 if (cdb->byte2 & SRW12_FUA)
8714 fua = 1;
8715 if (cdb->byte2 & SRW12_DPO)
8716 dpo = 1;
8717 lba = scsi_4btoul(cdb->addr);
8718 num_blocks = scsi_4btoul(cdb->length);
8719 break;
8720 }
8721 case WRITE_VERIFY_12: {
8722 struct scsi_write_verify_12 *cdb;
8723
8724 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8725
8726 if (cdb->byte2 & SWV_DPO)
8727 dpo = 1;
8728
8729 lba = scsi_4btoul(cdb->addr);
8730 num_blocks = scsi_4btoul(cdb->length);
8731
8732 break;
8733 }
8734 case READ_16:
8735 case WRITE_16: {
8736 struct scsi_rw_16 *cdb;
8737
8738 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8739
8740 if (cdb->byte2 & SRW12_RELADDR)
8741 reladdr = 1;
8742 if (cdb->byte2 & SRW12_FUA)
8743 fua = 1;
8744 if (cdb->byte2 & SRW12_DPO)
8745 dpo = 1;
8746
8747 lba = scsi_8btou64(cdb->addr);
8748 num_blocks = scsi_4btoul(cdb->length);
8749 break;
8750 }
8751 case WRITE_VERIFY_16: {
8752 struct scsi_write_verify_16 *cdb;
8753
8754 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8755
8756 if (cdb->byte2 & SWV_DPO)
8757 dpo = 1;
8758
8759 lba = scsi_8btou64(cdb->addr);
8760 num_blocks = scsi_4btoul(cdb->length);
8761 break;
8762 }
8763 default:
8764 /*
8765 * We got a command we don't support. This shouldn't
8766 * happen, commands should be filtered out above us.
8767 */
8768 ctl_set_invalid_opcode(ctsio);
8769 ctl_done((union ctl_io *)ctsio);
8770
8771 return (CTL_RETVAL_COMPLETE);
8772 break; /* NOTREACHED */
8773 }
8774
8775 /*
8776 * XXX KDM what do we do with the DPO and FUA bits? FUA might be
8777 * interesting for us, but if RAIDCore is in write-back mode,
8778 * getting it to do write-through for a particular transaction may
8779 * not be possible.
8780 */
8781 /*
8782 * We don't support relative addressing. That also requires
8783 * supporting linked commands, which we don't do.
8784 */
8785 if (reladdr != 0) {
8786 ctl_set_invalid_field(ctsio,
8787 /*sks_valid*/ 1,
8788 /*command*/ 1,
8789 /*field*/ 1,
8790 /*bit_valid*/ 1,
8791 /*bit*/ 0);
8792 ctl_done((union ctl_io *)ctsio);
8793 return (CTL_RETVAL_COMPLETE);
8794 }
8795
8796 /*
8797 * The first check is to make sure we're in bounds, the second
8798 * check is to catch wrap-around problems. If the lba + num blocks
8799 * is less than the lba, then we've wrapped around and the block
8800 * range is invalid anyway.
8801 */
8802 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8803 || ((lba + num_blocks) < lba)) {
8804 ctl_set_lba_out_of_range(ctsio);
8805 ctl_done((union ctl_io *)ctsio);
8806 return (CTL_RETVAL_COMPLETE);
8807 }
8808
8809 /*
8810 * According to SBC-3, a transfer length of 0 is not an error.
8811 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8812 * translates to 256 blocks for those commands.
8813 */
8814 if (num_blocks == 0) {
8815 ctl_set_success(ctsio);
8816 ctl_done((union ctl_io *)ctsio);
8817 return (CTL_RETVAL_COMPLETE);
8818 }
8819
8796 lbalen.lba = lba;
8797 lbalen.len = num_blocks;
8798 memcpy(ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, &lbalen,
8799 sizeof(lbalen));
8820 lbalen = (struct ctl_lba_len_flags *)
8821 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8822 lbalen->lba = lba;
8823 lbalen->len = num_blocks;
8824 lbalen->flags = isread ? CTL_LLF_READ : CTL_LLF_WRITE;
8800
8801 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8802 ctsio->kern_rel_offset = 0;
8803
8804 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8805
8806 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8807
8808 return (retval);
8809}
8810
8825
8826 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8827 ctsio->kern_rel_offset = 0;
8828
8829 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8830
8831 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8832
8833 return (retval);
8834}
8835
8836static int
8837ctl_cnw_cont(union ctl_io *io)
8838{
8839 struct ctl_scsiio *ctsio;
8840 struct ctl_lun *lun;
8841 struct ctl_lba_len_flags *lbalen;
8842 int retval;
8843
8844 ctsio = &io->scsiio;
8845 ctsio->io_hdr.status = CTL_STATUS_NONE;
8846 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8847 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8848 lbalen = (struct ctl_lba_len_flags *)
8849 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8850 lbalen->flags = CTL_LLF_WRITE;
8851
8852 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8853 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8854 return (retval);
8855}
8856
8811int
8857int
8858ctl_cnw(struct ctl_scsiio *ctsio)
8859{
8860 struct ctl_lun *lun;
8861 struct ctl_lba_len_flags *lbalen;
8862 uint64_t lba;
8863 uint32_t num_blocks;
8864 int fua, dpo;
8865 int retval;
8866
8867 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8868
8869 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8870
8871 fua = 0;
8872 dpo = 0;
8873
8874 retval = CTL_RETVAL_COMPLETE;
8875
8876 switch (ctsio->cdb[0]) {
8877 case COMPARE_AND_WRITE: {
8878 struct scsi_compare_and_write *cdb;
8879
8880 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8881
8882 if (cdb->byte2 & SRW10_FUA)
8883 fua = 1;
8884 if (cdb->byte2 & SRW10_DPO)
8885 dpo = 1;
8886 lba = scsi_8btou64(cdb->addr);
8887 num_blocks = cdb->length;
8888 break;
8889 }
8890 default:
8891 /*
8892 * We got a command we don't support. This shouldn't
8893 * happen, commands should be filtered out above us.
8894 */
8895 ctl_set_invalid_opcode(ctsio);
8896 ctl_done((union ctl_io *)ctsio);
8897
8898 return (CTL_RETVAL_COMPLETE);
8899 break; /* NOTREACHED */
8900 }
8901
8902 /*
8903 * XXX KDM what do we do with the DPO and FUA bits? FUA might be
8904 * interesting for us, but if RAIDCore is in write-back mode,
8905 * getting it to do write-through for a particular transaction may
8906 * not be possible.
8907 */
8908
8909 /*
8910 * The first check is to make sure we're in bounds, the second
8911 * check is to catch wrap-around problems. If the lba + num blocks
8912 * is less than the lba, then we've wrapped around and the block
8913 * range is invalid anyway.
8914 */
8915 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8916 || ((lba + num_blocks) < lba)) {
8917 ctl_set_lba_out_of_range(ctsio);
8918 ctl_done((union ctl_io *)ctsio);
8919 return (CTL_RETVAL_COMPLETE);
8920 }
8921
8922 /*
8923 * According to SBC-3, a transfer length of 0 is not an error.
8924 */
8925 if (num_blocks == 0) {
8926 ctl_set_success(ctsio);
8927 ctl_done((union ctl_io *)ctsio);
8928 return (CTL_RETVAL_COMPLETE);
8929 }
8930
8931 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8932 ctsio->kern_rel_offset = 0;
8933
8934 /*
8935 * Set the IO_CONT flag, so that if this I/O gets passed to
8936 * ctl_data_submit_done(), it'll get passed back to
8937 * ctl_ctl_cnw_cont() for further processing.
8938 */
8939 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8940 ctsio->io_cont = ctl_cnw_cont;
8941
8942 lbalen = (struct ctl_lba_len_flags *)
8943 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8944 lbalen->lba = lba;
8945 lbalen->len = num_blocks;
8946 lbalen->flags = CTL_LLF_COMPARE;
8947
8948 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8949 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8950 return (retval);
8951}
8952
8953int
8954ctl_verify(struct ctl_scsiio *ctsio)
8955{
8956 struct ctl_lun *lun;
8957 struct ctl_lba_len_flags *lbalen;
8958 uint64_t lba;
8959 uint32_t num_blocks;
8960 int bytchk, dpo;
8961 int retval;
8962
8963 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8964
8965 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
8966
8967 bytchk = 0;
8968 dpo = 0;
8969 retval = CTL_RETVAL_COMPLETE;
8970
8971 switch (ctsio->cdb[0]) {
8972 case VERIFY_10: {
8973 struct scsi_verify_10 *cdb;
8974
8975 cdb = (struct scsi_verify_10 *)ctsio->cdb;
8976 if (cdb->byte2 & SVFY_BYTCHK)
8977 bytchk = 1;
8978 if (cdb->byte2 & SVFY_DPO)
8979 dpo = 1;
8980 lba = scsi_4btoul(cdb->addr);
8981 num_blocks = scsi_2btoul(cdb->length);
8982 break;
8983 }
8984 case VERIFY_12: {
8985 struct scsi_verify_12 *cdb;
8986
8987 cdb = (struct scsi_verify_12 *)ctsio->cdb;
8988 if (cdb->byte2 & SVFY_BYTCHK)
8989 bytchk = 1;
8990 if (cdb->byte2 & SVFY_DPO)
8991 dpo = 1;
8992 lba = scsi_4btoul(cdb->addr);
8993 num_blocks = scsi_4btoul(cdb->length);
8994 break;
8995 }
8996 case VERIFY_16: {
8997 struct scsi_rw_16 *cdb;
8998
8999 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9000 if (cdb->byte2 & SVFY_BYTCHK)
9001 bytchk = 1;
9002 if (cdb->byte2 & SVFY_DPO)
9003 dpo = 1;
9004 lba = scsi_8btou64(cdb->addr);
9005 num_blocks = scsi_4btoul(cdb->length);
9006 break;
9007 }
9008 default:
9009 /*
9010 * We got a command we don't support. This shouldn't
9011 * happen, commands should be filtered out above us.
9012 */
9013 ctl_set_invalid_opcode(ctsio);
9014 ctl_done((union ctl_io *)ctsio);
9015 return (CTL_RETVAL_COMPLETE);
9016 }
9017
9018 /*
9019 * The first check is to make sure we're in bounds, the second
9020 * check is to catch wrap-around problems. If the lba + num blocks
9021 * is less than the lba, then we've wrapped around and the block
9022 * range is invalid anyway.
9023 */
9024 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9025 || ((lba + num_blocks) < lba)) {
9026 ctl_set_lba_out_of_range(ctsio);
9027 ctl_done((union ctl_io *)ctsio);
9028 return (CTL_RETVAL_COMPLETE);
9029 }
9030
9031 /*
9032 * According to SBC-3, a transfer length of 0 is not an error.
9033 */
9034 if (num_blocks == 0) {
9035 ctl_set_success(ctsio);
9036 ctl_done((union ctl_io *)ctsio);
9037 return (CTL_RETVAL_COMPLETE);
9038 }
9039
9040 lbalen = (struct ctl_lba_len_flags *)
9041 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9042 lbalen->lba = lba;
9043 lbalen->len = num_blocks;
9044 if (bytchk) {
9045 lbalen->flags = CTL_LLF_COMPARE;
9046 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9047 } else {
9048 lbalen->flags = CTL_LLF_VERIFY;
9049 ctsio->kern_total_len = 0;
9050 }
9051 ctsio->kern_rel_offset = 0;
9052
9053 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9054 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9055 return (retval);
9056}
9057
9058int
8812ctl_report_luns(struct ctl_scsiio *ctsio)
8813{
8814 struct scsi_report_luns *cdb;
8815 struct scsi_report_luns_data *lun_data;
8816 struct ctl_lun *lun, *request_lun;
8817 int num_luns, retval;
8818 uint32_t alloc_len, lun_datalen;
8819 int num_filled, well_known;
8820 uint32_t initidx, targ_lun_id, lun_id;
8821
8822 retval = CTL_RETVAL_COMPLETE;
8823 well_known = 0;
8824
8825 cdb = (struct scsi_report_luns *)ctsio->cdb;
8826
8827 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
8828
8829 mtx_lock(&control_softc->ctl_lock);
8830 num_luns = control_softc->num_luns;
8831 mtx_unlock(&control_softc->ctl_lock);
8832
8833 switch (cdb->select_report) {
8834 case RPL_REPORT_DEFAULT:
8835 case RPL_REPORT_ALL:
8836 break;
8837 case RPL_REPORT_WELLKNOWN:
8838 well_known = 1;
8839 num_luns = 0;
8840 break;
8841 default:
8842 ctl_set_invalid_field(ctsio,
8843 /*sks_valid*/ 1,
8844 /*command*/ 1,
8845 /*field*/ 2,
8846 /*bit_valid*/ 0,
8847 /*bit*/ 0);
8848 ctl_done((union ctl_io *)ctsio);
8849 return (retval);
8850 break; /* NOTREACHED */
8851 }
8852
8853 alloc_len = scsi_4btoul(cdb->length);
8854 /*
8855 * The initiator has to allocate at least 16 bytes for this request,
8856 * so he can at least get the header and the first LUN. Otherwise
8857 * we reject the request (per SPC-3 rev 14, section 6.21).
8858 */
8859 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
8860 sizeof(struct scsi_report_luns_lundata))) {
8861 ctl_set_invalid_field(ctsio,
8862 /*sks_valid*/ 1,
8863 /*command*/ 1,
8864 /*field*/ 6,
8865 /*bit_valid*/ 0,
8866 /*bit*/ 0);
8867 ctl_done((union ctl_io *)ctsio);
8868 return (retval);
8869 }
8870
8871 request_lun = (struct ctl_lun *)
8872 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8873
8874 lun_datalen = sizeof(*lun_data) +
8875 (num_luns * sizeof(struct scsi_report_luns_lundata));
8876
8877 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
8878 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
8879 ctsio->kern_sg_entries = 0;
8880
8881 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8882
8883 mtx_lock(&control_softc->ctl_lock);
8884 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
8885 lun_id = targ_lun_id;
8886 if (ctsio->io_hdr.nexus.lun_map_fn != NULL)
8887 lun_id = ctsio->io_hdr.nexus.lun_map_fn(ctsio->io_hdr.nexus.lun_map_arg, lun_id);
8888 if (lun_id >= CTL_MAX_LUNS)
8889 continue;
8890 lun = control_softc->ctl_luns[lun_id];
8891 if (lun == NULL)
8892 continue;
8893
8894 if (targ_lun_id <= 0xff) {
8895 /*
8896 * Peripheral addressing method, bus number 0.
8897 */
8898 lun_data->luns[num_filled].lundata[0] =
8899 RPL_LUNDATA_ATYP_PERIPH;
8900 lun_data->luns[num_filled].lundata[1] = targ_lun_id;
8901 num_filled++;
8902 } else if (targ_lun_id <= 0x3fff) {
8903 /*
8904 * Flat addressing method.
8905 */
8906 lun_data->luns[num_filled].lundata[0] =
8907 RPL_LUNDATA_ATYP_FLAT |
8908 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK);
8909#ifdef OLDCTLHEADERS
8910 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) |
8911 (targ_lun_id & SRLD_BUS_LUN_MASK);
8912#endif
8913 lun_data->luns[num_filled].lundata[1] =
8914#ifdef OLDCTLHEADERS
8915 targ_lun_id >> SRLD_BUS_LUN_BITS;
8916#endif
8917 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS;
8918 num_filled++;
8919 } else {
8920 printf("ctl_report_luns: bogus LUN number %jd, "
8921 "skipping\n", (intmax_t)targ_lun_id);
8922 }
8923 /*
8924 * According to SPC-3, rev 14 section 6.21:
8925 *
8926 * "The execution of a REPORT LUNS command to any valid and
8927 * installed logical unit shall clear the REPORTED LUNS DATA
8928 * HAS CHANGED unit attention condition for all logical
8929 * units of that target with respect to the requesting
8930 * initiator. A valid and installed logical unit is one
8931 * having a PERIPHERAL QUALIFIER of 000b in the standard
8932 * INQUIRY data (see 6.4.2)."
8933 *
8934 * If request_lun is NULL, the LUN this report luns command
8935 * was issued to is either disabled or doesn't exist. In that
8936 * case, we shouldn't clear any pending lun change unit
8937 * attention.
8938 */
8939 if (request_lun != NULL)
8940 lun->pending_sense[initidx].ua_pending &=
8941 ~CTL_UA_LUN_CHANGE;
8942 }
8943 mtx_unlock(&control_softc->ctl_lock);
8944
8945 /*
8946 * It's quite possible that we've returned fewer LUNs than we allocated
8947 * space for. Trim it.
8948 */
8949 lun_datalen = sizeof(*lun_data) +
8950 (num_filled * sizeof(struct scsi_report_luns_lundata));
8951
8952 if (lun_datalen < alloc_len) {
8953 ctsio->residual = alloc_len - lun_datalen;
8954 ctsio->kern_data_len = lun_datalen;
8955 ctsio->kern_total_len = lun_datalen;
8956 } else {
8957 ctsio->residual = 0;
8958 ctsio->kern_data_len = alloc_len;
8959 ctsio->kern_total_len = alloc_len;
8960 }
8961 ctsio->kern_data_resid = 0;
8962 ctsio->kern_rel_offset = 0;
8963 ctsio->kern_sg_entries = 0;
8964
8965 /*
8966 * We set this to the actual data length, regardless of how much
8967 * space we actually have to return results. If the user looks at
8968 * this value, he'll know whether or not he allocated enough space
8969 * and reissue the command if necessary. We don't support well
8970 * known logical units, so if the user asks for that, return none.
8971 */
8972 scsi_ulto4b(lun_datalen - 8, lun_data->length);
8973
8974 /*
8975 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
8976 * this request.
8977 */
8978 ctsio->scsi_status = SCSI_STATUS_OK;
8979
8980 ctsio->be_move_done = ctl_config_move_done;
8981 ctl_datamove((union ctl_io *)ctsio);
8982
8983 return (retval);
8984}
8985
8986int
8987ctl_request_sense(struct ctl_scsiio *ctsio)
8988{
8989 struct scsi_request_sense *cdb;
8990 struct scsi_sense_data *sense_ptr;
8991 struct ctl_lun *lun;
8992 uint32_t initidx;
8993 int have_error;
8994 scsi_sense_data_type sense_format;
8995
8996 cdb = (struct scsi_request_sense *)ctsio->cdb;
8997
8998 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8999
9000 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9001
9002 /*
9003 * Determine which sense format the user wants.
9004 */
9005 if (cdb->byte2 & SRS_DESC)
9006 sense_format = SSD_TYPE_DESC;
9007 else
9008 sense_format = SSD_TYPE_FIXED;
9009
9010 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9011 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9012 ctsio->kern_sg_entries = 0;
9013
9014 /*
9015 * struct scsi_sense_data, which is currently set to 256 bytes, is
9016 * larger than the largest allowed value for the length field in the
9017 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9018 */
9019 ctsio->residual = 0;
9020 ctsio->kern_data_len = cdb->length;
9021 ctsio->kern_total_len = cdb->length;
9022
9023 ctsio->kern_data_resid = 0;
9024 ctsio->kern_rel_offset = 0;
9025 ctsio->kern_sg_entries = 0;
9026
9027 /*
9028 * If we don't have a LUN, we don't have any pending sense.
9029 */
9030 if (lun == NULL)
9031 goto no_sense;
9032
9033 have_error = 0;
9034 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9035 /*
9036 * Check for pending sense, and then for pending unit attentions.
9037 * Pending sense gets returned first, then pending unit attentions.
9038 */
9039 mtx_lock(&lun->ctl_softc->ctl_lock);
9040 if (ctl_is_set(lun->have_ca, initidx)) {
9041 scsi_sense_data_type stored_format;
9042
9043 /*
9044 * Check to see which sense format was used for the stored
9045 * sense data.
9046 */
9047 stored_format = scsi_sense_type(
9048 &lun->pending_sense[initidx].sense);
9049
9050 /*
9051 * If the user requested a different sense format than the
9052 * one we stored, then we need to convert it to the other
9053 * format. If we're going from descriptor to fixed format
9054 * sense data, we may lose things in translation, depending
9055 * on what options were used.
9056 *
9057 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9058 * for some reason we'll just copy it out as-is.
9059 */
9060 if ((stored_format == SSD_TYPE_FIXED)
9061 && (sense_format == SSD_TYPE_DESC))
9062 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9063 &lun->pending_sense[initidx].sense,
9064 (struct scsi_sense_data_desc *)sense_ptr);
9065 else if ((stored_format == SSD_TYPE_DESC)
9066 && (sense_format == SSD_TYPE_FIXED))
9067 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9068 &lun->pending_sense[initidx].sense,
9069 (struct scsi_sense_data_fixed *)sense_ptr);
9070 else
9071 memcpy(sense_ptr, &lun->pending_sense[initidx].sense,
9072 ctl_min(sizeof(*sense_ptr),
9073 sizeof(lun->pending_sense[initidx].sense)));
9074
9075 ctl_clear_mask(lun->have_ca, initidx);
9076 have_error = 1;
9077 } else if (lun->pending_sense[initidx].ua_pending != CTL_UA_NONE) {
9078 ctl_ua_type ua_type;
9079
9080 ua_type = ctl_build_ua(lun->pending_sense[initidx].ua_pending,
9081 sense_ptr, sense_format);
9082 if (ua_type != CTL_UA_NONE) {
9083 have_error = 1;
9084 /* We're reporting this UA, so clear it */
9085 lun->pending_sense[initidx].ua_pending &= ~ua_type;
9086 }
9087 }
9088 mtx_unlock(&lun->ctl_softc->ctl_lock);
9089
9090 /*
9091 * We already have a pending error, return it.
9092 */
9093 if (have_error != 0) {
9094 /*
9095 * We report the SCSI status as OK, since the status of the
9096 * request sense command itself is OK.
9097 */
9098 ctsio->scsi_status = SCSI_STATUS_OK;
9099
9100 /*
9101 * We report 0 for the sense length, because we aren't doing
9102 * autosense in this case. We're reporting sense as
9103 * parameter data.
9104 */
9105 ctsio->sense_len = 0;
9106
9107 ctsio->be_move_done = ctl_config_move_done;
9108 ctl_datamove((union ctl_io *)ctsio);
9109
9110 return (CTL_RETVAL_COMPLETE);
9111 }
9112
9113no_sense:
9114
9115 /*
9116 * No sense information to report, so we report that everything is
9117 * okay.
9118 */
9119 ctl_set_sense_data(sense_ptr,
9120 lun,
9121 sense_format,
9122 /*current_error*/ 1,
9123 /*sense_key*/ SSD_KEY_NO_SENSE,
9124 /*asc*/ 0x00,
9125 /*ascq*/ 0x00,
9126 SSD_ELEM_NONE);
9127
9128 ctsio->scsi_status = SCSI_STATUS_OK;
9129
9130 /*
9131 * We report 0 for the sense length, because we aren't doing
9132 * autosense in this case. We're reporting sense as parameter data.
9133 */
9134 ctsio->sense_len = 0;
9135 ctsio->be_move_done = ctl_config_move_done;
9136 ctl_datamove((union ctl_io *)ctsio);
9137
9138 return (CTL_RETVAL_COMPLETE);
9139}
9140
9141int
9142ctl_tur(struct ctl_scsiio *ctsio)
9143{
9144 struct ctl_lun *lun;
9145
9146 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9147
9148 CTL_DEBUG_PRINT(("ctl_tur\n"));
9149
9150 if (lun == NULL)
9151 return (-EINVAL);
9152
9153 ctsio->scsi_status = SCSI_STATUS_OK;
9154 ctsio->io_hdr.status = CTL_SUCCESS;
9155
9156 ctl_done((union ctl_io *)ctsio);
9157
9158 return (CTL_RETVAL_COMPLETE);
9159}
9160
9161#ifdef notyet
9162static int
9163ctl_cmddt_inquiry(struct ctl_scsiio *ctsio)
9164{
9165
9166}
9167#endif
9168
9169static int
9170ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9171{
9172 struct scsi_vpd_supported_pages *pages;
9173 int sup_page_size;
9174 struct ctl_lun *lun;
9175
9176 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9177
9178 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9179 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9180 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9181 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9182 ctsio->kern_sg_entries = 0;
9183
9184 if (sup_page_size < alloc_len) {
9185 ctsio->residual = alloc_len - sup_page_size;
9186 ctsio->kern_data_len = sup_page_size;
9187 ctsio->kern_total_len = sup_page_size;
9188 } else {
9189 ctsio->residual = 0;
9190 ctsio->kern_data_len = alloc_len;
9191 ctsio->kern_total_len = alloc_len;
9192 }
9193 ctsio->kern_data_resid = 0;
9194 ctsio->kern_rel_offset = 0;
9195 ctsio->kern_sg_entries = 0;
9196
9197 /*
9198 * The control device is always connected. The disk device, on the
9199 * other hand, may not be online all the time. Need to change this
9200 * to figure out whether the disk device is actually online or not.
9201 */
9202 if (lun != NULL)
9203 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9204 lun->be_lun->lun_type;
9205 else
9206 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9207
9208 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES;
9209 /* Supported VPD pages */
9210 pages->page_list[0] = SVPD_SUPPORTED_PAGES;
9211 /* Serial Number */
9212 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER;
9213 /* Device Identification */
9214 pages->page_list[2] = SVPD_DEVICE_ID;
9215 /* Block limits */
9216 pages->page_list[3] = SVPD_BLOCK_LIMITS;
9217 /* Logical Block Provisioning */
9218 pages->page_list[4] = SVPD_LBP;
9219
9220 ctsio->scsi_status = SCSI_STATUS_OK;
9221
9222 ctsio->be_move_done = ctl_config_move_done;
9223 ctl_datamove((union ctl_io *)ctsio);
9224
9225 return (CTL_RETVAL_COMPLETE);
9226}
9227
9228static int
9229ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9230{
9231 struct scsi_vpd_unit_serial_number *sn_ptr;
9232 struct ctl_lun *lun;
9233#ifndef CTL_USE_BACKEND_SN
9234 char tmpstr[32];
9235#endif
9236
9237 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9238
9239 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO);
9240 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9241 ctsio->kern_sg_entries = 0;
9242
9243 if (sizeof(*sn_ptr) < alloc_len) {
9244 ctsio->residual = alloc_len - sizeof(*sn_ptr);
9245 ctsio->kern_data_len = sizeof(*sn_ptr);
9246 ctsio->kern_total_len = sizeof(*sn_ptr);
9247 } else {
9248 ctsio->residual = 0;
9249 ctsio->kern_data_len = alloc_len;
9250 ctsio->kern_total_len = alloc_len;
9251 }
9252 ctsio->kern_data_resid = 0;
9253 ctsio->kern_rel_offset = 0;
9254 ctsio->kern_sg_entries = 0;
9255
9256 /*
9257 * The control device is always connected. The disk device, on the
9258 * other hand, may not be online all the time. Need to change this
9259 * to figure out whether the disk device is actually online or not.
9260 */
9261 if (lun != NULL)
9262 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9263 lun->be_lun->lun_type;
9264 else
9265 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9266
9267 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9268 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN);
9269#ifdef CTL_USE_BACKEND_SN
9270 /*
9271 * If we don't have a LUN, we just leave the serial number as
9272 * all spaces.
9273 */
9274 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num));
9275 if (lun != NULL) {
9276 strncpy((char *)sn_ptr->serial_num,
9277 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9278 }
9279#else
9280 /*
9281 * Note that we're using a non-unique serial number here,
9282 */
9283 snprintf(tmpstr, sizeof(tmpstr), "MYSERIALNUMIS000");
9284 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num));
9285 strncpy(sn_ptr->serial_num, tmpstr, ctl_min(CTL_SN_LEN,
9286 ctl_min(sizeof(tmpstr), sizeof(*sn_ptr) - 4)));
9287#endif
9288 ctsio->scsi_status = SCSI_STATUS_OK;
9289
9290 ctsio->be_move_done = ctl_config_move_done;
9291 ctl_datamove((union ctl_io *)ctsio);
9292
9293 return (CTL_RETVAL_COMPLETE);
9294}
9295
9296
9297static int
9298ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9299{
9300 struct scsi_vpd_device_id *devid_ptr;
9301 struct scsi_vpd_id_descriptor *desc, *desc1;
9302 struct scsi_vpd_id_descriptor *desc2, *desc3; /* for types 4h and 5h */
9303 struct scsi_vpd_id_t10 *t10id;
9304 struct ctl_softc *ctl_softc;
9305 struct ctl_lun *lun;
9306 struct ctl_frontend *fe;
9307 char *val;
9308#ifndef CTL_USE_BACKEND_SN
9309 char tmpstr[32];
9310#endif /* CTL_USE_BACKEND_SN */
9311 int devid_len;
9312
9313 ctl_softc = control_softc;
9314
9315 mtx_lock(&ctl_softc->ctl_lock);
9316 fe = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
9317 mtx_unlock(&ctl_softc->ctl_lock);
9318
9319 if (fe->devid != NULL)
9320 return ((fe->devid)(ctsio, alloc_len));
9321
9322 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9323
9324 devid_len = sizeof(struct scsi_vpd_device_id) +
9325 sizeof(struct scsi_vpd_id_descriptor) +
9326 sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN +
9327 sizeof(struct scsi_vpd_id_descriptor) + CTL_WWPN_LEN +
9328 sizeof(struct scsi_vpd_id_descriptor) +
9329 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9330 sizeof(struct scsi_vpd_id_descriptor) +
9331 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9332
9333 ctsio->kern_data_ptr = malloc(devid_len, M_CTL, M_WAITOK | M_ZERO);
9334 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9335 ctsio->kern_sg_entries = 0;
9336
9337 if (devid_len < alloc_len) {
9338 ctsio->residual = alloc_len - devid_len;
9339 ctsio->kern_data_len = devid_len;
9340 ctsio->kern_total_len = devid_len;
9341 } else {
9342 ctsio->residual = 0;
9343 ctsio->kern_data_len = alloc_len;
9344 ctsio->kern_total_len = alloc_len;
9345 }
9346 ctsio->kern_data_resid = 0;
9347 ctsio->kern_rel_offset = 0;
9348 ctsio->kern_sg_entries = 0;
9349
9350 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9351 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
9352 desc1 = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9353 sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN);
9354 desc2 = (struct scsi_vpd_id_descriptor *)(&desc1->identifier[0] +
9355 CTL_WWPN_LEN);
9356 desc3 = (struct scsi_vpd_id_descriptor *)(&desc2->identifier[0] +
9357 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9358
9359 /*
9360 * The control device is always connected. The disk device, on the
9361 * other hand, may not be online all the time.
9362 */
9363 if (lun != NULL)
9364 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9365 lun->be_lun->lun_type;
9366 else
9367 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9368
9369 devid_ptr->page_code = SVPD_DEVICE_ID;
9370
9371 scsi_ulto2b(devid_len - 4, devid_ptr->length);
9372
9373 mtx_lock(&ctl_softc->ctl_lock);
9374
9375 /*
9376 * For Fibre channel,
9377 */
9378 if (fe->port_type == CTL_PORT_FC)
9379 {
9380 desc->proto_codeset = (SCSI_PROTO_FC << 4) |
9381 SVPD_ID_CODESET_ASCII;
9382 desc1->proto_codeset = (SCSI_PROTO_FC << 4) |
9383 SVPD_ID_CODESET_BINARY;
9384 }
9385 else
9386 {
9387 desc->proto_codeset = (SCSI_PROTO_SPI << 4) |
9388 SVPD_ID_CODESET_ASCII;
9389 desc1->proto_codeset = (SCSI_PROTO_SPI << 4) |
9390 SVPD_ID_CODESET_BINARY;
9391 }
9392 desc2->proto_codeset = desc3->proto_codeset = desc1->proto_codeset;
9393 mtx_unlock(&ctl_softc->ctl_lock);
9394
9395 /*
9396 * We're using a LUN association here. i.e., this device ID is a
9397 * per-LUN identifier.
9398 */
9399 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
9400 desc->length = sizeof(*t10id) + CTL_DEVID_LEN;
9401 if (lun == NULL || (val = ctl_get_opt(lun->be_lun, "vendor")) == NULL) {
9402 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
9403 } else {
9404 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
9405 strncpy(t10id->vendor, val,
9406 min(sizeof(t10id->vendor), strlen(val)));
9407 }
9408
9409 /*
9410 * desc1 is for the WWPN which is a port asscociation.
9411 */
9412 desc1->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | SVPD_ID_TYPE_NAA;
9413 desc1->length = CTL_WWPN_LEN;
9414 /* XXX Call Reggie's get_WWNN func here then add port # to the end */
9415 /* For testing just create the WWPN */
9416#if 0
9417 ddb_GetWWNN((char *)desc1->identifier);
9418
9419 /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */
9420 /* This is so Copancontrol will return something sane */
9421 if (ctsio->io_hdr.nexus.targ_port!=0 &&
9422 ctsio->io_hdr.nexus.targ_port!=8)
9423 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port-1;
9424 else
9425 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port;
9426#endif
9427
9428 be64enc(desc1->identifier, fe->wwpn);
9429
9430 /*
9431 * desc2 is for the Relative Target Port(type 4h) identifier
9432 */
9433 desc2->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT
9434 | SVPD_ID_TYPE_RELTARG;
9435 desc2->length = 4;
9436//#if 0
9437 /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */
9438 /* This is so Copancontrol will return something sane */
9439 if (ctsio->io_hdr.nexus.targ_port!=0 &&
9440 ctsio->io_hdr.nexus.targ_port!=8)
9441 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port - 1;
9442 else
9443 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port;
9444//#endif
9445
9446 /*
9447 * desc3 is for the Target Port Group(type 5h) identifier
9448 */
9449 desc3->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT
9450 | SVPD_ID_TYPE_TPORTGRP;
9451 desc3->length = 4;
9452 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS || ctl_is_single)
9453 desc3->identifier[3] = 1;
9454 else
9455 desc3->identifier[3] = 2;
9456
9457#ifdef CTL_USE_BACKEND_SN
9458 /*
9459 * If we've actually got a backend, copy the device id from the
9460 * per-LUN data. Otherwise, set it to all spaces.
9461 */
9462 if (lun != NULL) {
9463 /*
9464 * Copy the backend's LUN ID.
9465 */
9466 strncpy((char *)t10id->vendor_spec_id,
9467 (char *)lun->be_lun->device_id, CTL_DEVID_LEN);
9468 } else {
9469 /*
9470 * No backend, set this to spaces.
9471 */
9472 memset(t10id->vendor_spec_id, 0x20, CTL_DEVID_LEN);
9473 }
9474#else
9475 snprintf(tmpstr, sizeof(tmpstr), "MYDEVICEIDIS%4d",
9476 (lun != NULL) ? (int)lun->lun : 0);
9477 strncpy(t10id->vendor_spec_id, tmpstr, ctl_min(CTL_DEVID_LEN,
9478 sizeof(tmpstr)));
9479#endif
9480
9481 ctsio->scsi_status = SCSI_STATUS_OK;
9482
9483 ctsio->be_move_done = ctl_config_move_done;
9484 ctl_datamove((union ctl_io *)ctsio);
9485
9486 return (CTL_RETVAL_COMPLETE);
9487}
9488
9489static int
9490ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9491{
9492 struct scsi_vpd_block_limits *bl_ptr;
9493 struct ctl_lun *lun;
9494 int bs;
9495
9496 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9497 bs = lun->be_lun->blocksize;
9498
9499 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9500 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9501 ctsio->kern_sg_entries = 0;
9502
9503 if (sizeof(*bl_ptr) < alloc_len) {
9504 ctsio->residual = alloc_len - sizeof(*bl_ptr);
9505 ctsio->kern_data_len = sizeof(*bl_ptr);
9506 ctsio->kern_total_len = sizeof(*bl_ptr);
9507 } else {
9508 ctsio->residual = 0;
9509 ctsio->kern_data_len = alloc_len;
9510 ctsio->kern_total_len = alloc_len;
9511 }
9512 ctsio->kern_data_resid = 0;
9513 ctsio->kern_rel_offset = 0;
9514 ctsio->kern_sg_entries = 0;
9515
9516 /*
9517 * The control device is always connected. The disk device, on the
9518 * other hand, may not be online all the time. Need to change this
9519 * to figure out whether the disk device is actually online or not.
9520 */
9521 if (lun != NULL)
9522 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9523 lun->be_lun->lun_type;
9524 else
9525 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9526
9527 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9528 scsi_ulto2b(sizeof(*bl_ptr), bl_ptr->page_length);
9059ctl_report_luns(struct ctl_scsiio *ctsio)
9060{
9061 struct scsi_report_luns *cdb;
9062 struct scsi_report_luns_data *lun_data;
9063 struct ctl_lun *lun, *request_lun;
9064 int num_luns, retval;
9065 uint32_t alloc_len, lun_datalen;
9066 int num_filled, well_known;
9067 uint32_t initidx, targ_lun_id, lun_id;
9068
9069 retval = CTL_RETVAL_COMPLETE;
9070 well_known = 0;
9071
9072 cdb = (struct scsi_report_luns *)ctsio->cdb;
9073
9074 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9075
9076 mtx_lock(&control_softc->ctl_lock);
9077 num_luns = control_softc->num_luns;
9078 mtx_unlock(&control_softc->ctl_lock);
9079
9080 switch (cdb->select_report) {
9081 case RPL_REPORT_DEFAULT:
9082 case RPL_REPORT_ALL:
9083 break;
9084 case RPL_REPORT_WELLKNOWN:
9085 well_known = 1;
9086 num_luns = 0;
9087 break;
9088 default:
9089 ctl_set_invalid_field(ctsio,
9090 /*sks_valid*/ 1,
9091 /*command*/ 1,
9092 /*field*/ 2,
9093 /*bit_valid*/ 0,
9094 /*bit*/ 0);
9095 ctl_done((union ctl_io *)ctsio);
9096 return (retval);
9097 break; /* NOTREACHED */
9098 }
9099
9100 alloc_len = scsi_4btoul(cdb->length);
9101 /*
9102 * The initiator has to allocate at least 16 bytes for this request,
9103 * so he can at least get the header and the first LUN. Otherwise
9104 * we reject the request (per SPC-3 rev 14, section 6.21).
9105 */
9106 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9107 sizeof(struct scsi_report_luns_lundata))) {
9108 ctl_set_invalid_field(ctsio,
9109 /*sks_valid*/ 1,
9110 /*command*/ 1,
9111 /*field*/ 6,
9112 /*bit_valid*/ 0,
9113 /*bit*/ 0);
9114 ctl_done((union ctl_io *)ctsio);
9115 return (retval);
9116 }
9117
9118 request_lun = (struct ctl_lun *)
9119 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9120
9121 lun_datalen = sizeof(*lun_data) +
9122 (num_luns * sizeof(struct scsi_report_luns_lundata));
9123
9124 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9125 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9126 ctsio->kern_sg_entries = 0;
9127
9128 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9129
9130 mtx_lock(&control_softc->ctl_lock);
9131 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9132 lun_id = targ_lun_id;
9133 if (ctsio->io_hdr.nexus.lun_map_fn != NULL)
9134 lun_id = ctsio->io_hdr.nexus.lun_map_fn(ctsio->io_hdr.nexus.lun_map_arg, lun_id);
9135 if (lun_id >= CTL_MAX_LUNS)
9136 continue;
9137 lun = control_softc->ctl_luns[lun_id];
9138 if (lun == NULL)
9139 continue;
9140
9141 if (targ_lun_id <= 0xff) {
9142 /*
9143 * Peripheral addressing method, bus number 0.
9144 */
9145 lun_data->luns[num_filled].lundata[0] =
9146 RPL_LUNDATA_ATYP_PERIPH;
9147 lun_data->luns[num_filled].lundata[1] = targ_lun_id;
9148 num_filled++;
9149 } else if (targ_lun_id <= 0x3fff) {
9150 /*
9151 * Flat addressing method.
9152 */
9153 lun_data->luns[num_filled].lundata[0] =
9154 RPL_LUNDATA_ATYP_FLAT |
9155 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK);
9156#ifdef OLDCTLHEADERS
9157 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) |
9158 (targ_lun_id & SRLD_BUS_LUN_MASK);
9159#endif
9160 lun_data->luns[num_filled].lundata[1] =
9161#ifdef OLDCTLHEADERS
9162 targ_lun_id >> SRLD_BUS_LUN_BITS;
9163#endif
9164 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS;
9165 num_filled++;
9166 } else {
9167 printf("ctl_report_luns: bogus LUN number %jd, "
9168 "skipping\n", (intmax_t)targ_lun_id);
9169 }
9170 /*
9171 * According to SPC-3, rev 14 section 6.21:
9172 *
9173 * "The execution of a REPORT LUNS command to any valid and
9174 * installed logical unit shall clear the REPORTED LUNS DATA
9175 * HAS CHANGED unit attention condition for all logical
9176 * units of that target with respect to the requesting
9177 * initiator. A valid and installed logical unit is one
9178 * having a PERIPHERAL QUALIFIER of 000b in the standard
9179 * INQUIRY data (see 6.4.2)."
9180 *
9181 * If request_lun is NULL, the LUN this report luns command
9182 * was issued to is either disabled or doesn't exist. In that
9183 * case, we shouldn't clear any pending lun change unit
9184 * attention.
9185 */
9186 if (request_lun != NULL)
9187 lun->pending_sense[initidx].ua_pending &=
9188 ~CTL_UA_LUN_CHANGE;
9189 }
9190 mtx_unlock(&control_softc->ctl_lock);
9191
9192 /*
9193 * It's quite possible that we've returned fewer LUNs than we allocated
9194 * space for. Trim it.
9195 */
9196 lun_datalen = sizeof(*lun_data) +
9197 (num_filled * sizeof(struct scsi_report_luns_lundata));
9198
9199 if (lun_datalen < alloc_len) {
9200 ctsio->residual = alloc_len - lun_datalen;
9201 ctsio->kern_data_len = lun_datalen;
9202 ctsio->kern_total_len = lun_datalen;
9203 } else {
9204 ctsio->residual = 0;
9205 ctsio->kern_data_len = alloc_len;
9206 ctsio->kern_total_len = alloc_len;
9207 }
9208 ctsio->kern_data_resid = 0;
9209 ctsio->kern_rel_offset = 0;
9210 ctsio->kern_sg_entries = 0;
9211
9212 /*
9213 * We set this to the actual data length, regardless of how much
9214 * space we actually have to return results. If the user looks at
9215 * this value, he'll know whether or not he allocated enough space
9216 * and reissue the command if necessary. We don't support well
9217 * known logical units, so if the user asks for that, return none.
9218 */
9219 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9220
9221 /*
9222 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9223 * this request.
9224 */
9225 ctsio->scsi_status = SCSI_STATUS_OK;
9226
9227 ctsio->be_move_done = ctl_config_move_done;
9228 ctl_datamove((union ctl_io *)ctsio);
9229
9230 return (retval);
9231}
9232
9233int
9234ctl_request_sense(struct ctl_scsiio *ctsio)
9235{
9236 struct scsi_request_sense *cdb;
9237 struct scsi_sense_data *sense_ptr;
9238 struct ctl_lun *lun;
9239 uint32_t initidx;
9240 int have_error;
9241 scsi_sense_data_type sense_format;
9242
9243 cdb = (struct scsi_request_sense *)ctsio->cdb;
9244
9245 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9246
9247 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9248
9249 /*
9250 * Determine which sense format the user wants.
9251 */
9252 if (cdb->byte2 & SRS_DESC)
9253 sense_format = SSD_TYPE_DESC;
9254 else
9255 sense_format = SSD_TYPE_FIXED;
9256
9257 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9258 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9259 ctsio->kern_sg_entries = 0;
9260
9261 /*
9262 * struct scsi_sense_data, which is currently set to 256 bytes, is
9263 * larger than the largest allowed value for the length field in the
9264 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9265 */
9266 ctsio->residual = 0;
9267 ctsio->kern_data_len = cdb->length;
9268 ctsio->kern_total_len = cdb->length;
9269
9270 ctsio->kern_data_resid = 0;
9271 ctsio->kern_rel_offset = 0;
9272 ctsio->kern_sg_entries = 0;
9273
9274 /*
9275 * If we don't have a LUN, we don't have any pending sense.
9276 */
9277 if (lun == NULL)
9278 goto no_sense;
9279
9280 have_error = 0;
9281 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9282 /*
9283 * Check for pending sense, and then for pending unit attentions.
9284 * Pending sense gets returned first, then pending unit attentions.
9285 */
9286 mtx_lock(&lun->ctl_softc->ctl_lock);
9287 if (ctl_is_set(lun->have_ca, initidx)) {
9288 scsi_sense_data_type stored_format;
9289
9290 /*
9291 * Check to see which sense format was used for the stored
9292 * sense data.
9293 */
9294 stored_format = scsi_sense_type(
9295 &lun->pending_sense[initidx].sense);
9296
9297 /*
9298 * If the user requested a different sense format than the
9299 * one we stored, then we need to convert it to the other
9300 * format. If we're going from descriptor to fixed format
9301 * sense data, we may lose things in translation, depending
9302 * on what options were used.
9303 *
9304 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9305 * for some reason we'll just copy it out as-is.
9306 */
9307 if ((stored_format == SSD_TYPE_FIXED)
9308 && (sense_format == SSD_TYPE_DESC))
9309 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9310 &lun->pending_sense[initidx].sense,
9311 (struct scsi_sense_data_desc *)sense_ptr);
9312 else if ((stored_format == SSD_TYPE_DESC)
9313 && (sense_format == SSD_TYPE_FIXED))
9314 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9315 &lun->pending_sense[initidx].sense,
9316 (struct scsi_sense_data_fixed *)sense_ptr);
9317 else
9318 memcpy(sense_ptr, &lun->pending_sense[initidx].sense,
9319 ctl_min(sizeof(*sense_ptr),
9320 sizeof(lun->pending_sense[initidx].sense)));
9321
9322 ctl_clear_mask(lun->have_ca, initidx);
9323 have_error = 1;
9324 } else if (lun->pending_sense[initidx].ua_pending != CTL_UA_NONE) {
9325 ctl_ua_type ua_type;
9326
9327 ua_type = ctl_build_ua(lun->pending_sense[initidx].ua_pending,
9328 sense_ptr, sense_format);
9329 if (ua_type != CTL_UA_NONE) {
9330 have_error = 1;
9331 /* We're reporting this UA, so clear it */
9332 lun->pending_sense[initidx].ua_pending &= ~ua_type;
9333 }
9334 }
9335 mtx_unlock(&lun->ctl_softc->ctl_lock);
9336
9337 /*
9338 * We already have a pending error, return it.
9339 */
9340 if (have_error != 0) {
9341 /*
9342 * We report the SCSI status as OK, since the status of the
9343 * request sense command itself is OK.
9344 */
9345 ctsio->scsi_status = SCSI_STATUS_OK;
9346
9347 /*
9348 * We report 0 for the sense length, because we aren't doing
9349 * autosense in this case. We're reporting sense as
9350 * parameter data.
9351 */
9352 ctsio->sense_len = 0;
9353
9354 ctsio->be_move_done = ctl_config_move_done;
9355 ctl_datamove((union ctl_io *)ctsio);
9356
9357 return (CTL_RETVAL_COMPLETE);
9358 }
9359
9360no_sense:
9361
9362 /*
9363 * No sense information to report, so we report that everything is
9364 * okay.
9365 */
9366 ctl_set_sense_data(sense_ptr,
9367 lun,
9368 sense_format,
9369 /*current_error*/ 1,
9370 /*sense_key*/ SSD_KEY_NO_SENSE,
9371 /*asc*/ 0x00,
9372 /*ascq*/ 0x00,
9373 SSD_ELEM_NONE);
9374
9375 ctsio->scsi_status = SCSI_STATUS_OK;
9376
9377 /*
9378 * We report 0 for the sense length, because we aren't doing
9379 * autosense in this case. We're reporting sense as parameter data.
9380 */
9381 ctsio->sense_len = 0;
9382 ctsio->be_move_done = ctl_config_move_done;
9383 ctl_datamove((union ctl_io *)ctsio);
9384
9385 return (CTL_RETVAL_COMPLETE);
9386}
9387
9388int
9389ctl_tur(struct ctl_scsiio *ctsio)
9390{
9391 struct ctl_lun *lun;
9392
9393 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9394
9395 CTL_DEBUG_PRINT(("ctl_tur\n"));
9396
9397 if (lun == NULL)
9398 return (-EINVAL);
9399
9400 ctsio->scsi_status = SCSI_STATUS_OK;
9401 ctsio->io_hdr.status = CTL_SUCCESS;
9402
9403 ctl_done((union ctl_io *)ctsio);
9404
9405 return (CTL_RETVAL_COMPLETE);
9406}
9407
9408#ifdef notyet
9409static int
9410ctl_cmddt_inquiry(struct ctl_scsiio *ctsio)
9411{
9412
9413}
9414#endif
9415
9416static int
9417ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9418{
9419 struct scsi_vpd_supported_pages *pages;
9420 int sup_page_size;
9421 struct ctl_lun *lun;
9422
9423 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9424
9425 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9426 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9427 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9428 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9429 ctsio->kern_sg_entries = 0;
9430
9431 if (sup_page_size < alloc_len) {
9432 ctsio->residual = alloc_len - sup_page_size;
9433 ctsio->kern_data_len = sup_page_size;
9434 ctsio->kern_total_len = sup_page_size;
9435 } else {
9436 ctsio->residual = 0;
9437 ctsio->kern_data_len = alloc_len;
9438 ctsio->kern_total_len = alloc_len;
9439 }
9440 ctsio->kern_data_resid = 0;
9441 ctsio->kern_rel_offset = 0;
9442 ctsio->kern_sg_entries = 0;
9443
9444 /*
9445 * The control device is always connected. The disk device, on the
9446 * other hand, may not be online all the time. Need to change this
9447 * to figure out whether the disk device is actually online or not.
9448 */
9449 if (lun != NULL)
9450 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9451 lun->be_lun->lun_type;
9452 else
9453 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9454
9455 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES;
9456 /* Supported VPD pages */
9457 pages->page_list[0] = SVPD_SUPPORTED_PAGES;
9458 /* Serial Number */
9459 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER;
9460 /* Device Identification */
9461 pages->page_list[2] = SVPD_DEVICE_ID;
9462 /* Block limits */
9463 pages->page_list[3] = SVPD_BLOCK_LIMITS;
9464 /* Logical Block Provisioning */
9465 pages->page_list[4] = SVPD_LBP;
9466
9467 ctsio->scsi_status = SCSI_STATUS_OK;
9468
9469 ctsio->be_move_done = ctl_config_move_done;
9470 ctl_datamove((union ctl_io *)ctsio);
9471
9472 return (CTL_RETVAL_COMPLETE);
9473}
9474
9475static int
9476ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9477{
9478 struct scsi_vpd_unit_serial_number *sn_ptr;
9479 struct ctl_lun *lun;
9480#ifndef CTL_USE_BACKEND_SN
9481 char tmpstr[32];
9482#endif
9483
9484 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9485
9486 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO);
9487 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9488 ctsio->kern_sg_entries = 0;
9489
9490 if (sizeof(*sn_ptr) < alloc_len) {
9491 ctsio->residual = alloc_len - sizeof(*sn_ptr);
9492 ctsio->kern_data_len = sizeof(*sn_ptr);
9493 ctsio->kern_total_len = sizeof(*sn_ptr);
9494 } else {
9495 ctsio->residual = 0;
9496 ctsio->kern_data_len = alloc_len;
9497 ctsio->kern_total_len = alloc_len;
9498 }
9499 ctsio->kern_data_resid = 0;
9500 ctsio->kern_rel_offset = 0;
9501 ctsio->kern_sg_entries = 0;
9502
9503 /*
9504 * The control device is always connected. The disk device, on the
9505 * other hand, may not be online all the time. Need to change this
9506 * to figure out whether the disk device is actually online or not.
9507 */
9508 if (lun != NULL)
9509 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9510 lun->be_lun->lun_type;
9511 else
9512 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9513
9514 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9515 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN);
9516#ifdef CTL_USE_BACKEND_SN
9517 /*
9518 * If we don't have a LUN, we just leave the serial number as
9519 * all spaces.
9520 */
9521 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num));
9522 if (lun != NULL) {
9523 strncpy((char *)sn_ptr->serial_num,
9524 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9525 }
9526#else
9527 /*
9528 * Note that we're using a non-unique serial number here,
9529 */
9530 snprintf(tmpstr, sizeof(tmpstr), "MYSERIALNUMIS000");
9531 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num));
9532 strncpy(sn_ptr->serial_num, tmpstr, ctl_min(CTL_SN_LEN,
9533 ctl_min(sizeof(tmpstr), sizeof(*sn_ptr) - 4)));
9534#endif
9535 ctsio->scsi_status = SCSI_STATUS_OK;
9536
9537 ctsio->be_move_done = ctl_config_move_done;
9538 ctl_datamove((union ctl_io *)ctsio);
9539
9540 return (CTL_RETVAL_COMPLETE);
9541}
9542
9543
9544static int
9545ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9546{
9547 struct scsi_vpd_device_id *devid_ptr;
9548 struct scsi_vpd_id_descriptor *desc, *desc1;
9549 struct scsi_vpd_id_descriptor *desc2, *desc3; /* for types 4h and 5h */
9550 struct scsi_vpd_id_t10 *t10id;
9551 struct ctl_softc *ctl_softc;
9552 struct ctl_lun *lun;
9553 struct ctl_frontend *fe;
9554 char *val;
9555#ifndef CTL_USE_BACKEND_SN
9556 char tmpstr[32];
9557#endif /* CTL_USE_BACKEND_SN */
9558 int devid_len;
9559
9560 ctl_softc = control_softc;
9561
9562 mtx_lock(&ctl_softc->ctl_lock);
9563 fe = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
9564 mtx_unlock(&ctl_softc->ctl_lock);
9565
9566 if (fe->devid != NULL)
9567 return ((fe->devid)(ctsio, alloc_len));
9568
9569 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9570
9571 devid_len = sizeof(struct scsi_vpd_device_id) +
9572 sizeof(struct scsi_vpd_id_descriptor) +
9573 sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN +
9574 sizeof(struct scsi_vpd_id_descriptor) + CTL_WWPN_LEN +
9575 sizeof(struct scsi_vpd_id_descriptor) +
9576 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9577 sizeof(struct scsi_vpd_id_descriptor) +
9578 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9579
9580 ctsio->kern_data_ptr = malloc(devid_len, M_CTL, M_WAITOK | M_ZERO);
9581 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9582 ctsio->kern_sg_entries = 0;
9583
9584 if (devid_len < alloc_len) {
9585 ctsio->residual = alloc_len - devid_len;
9586 ctsio->kern_data_len = devid_len;
9587 ctsio->kern_total_len = devid_len;
9588 } else {
9589 ctsio->residual = 0;
9590 ctsio->kern_data_len = alloc_len;
9591 ctsio->kern_total_len = alloc_len;
9592 }
9593 ctsio->kern_data_resid = 0;
9594 ctsio->kern_rel_offset = 0;
9595 ctsio->kern_sg_entries = 0;
9596
9597 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9598 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
9599 desc1 = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9600 sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN);
9601 desc2 = (struct scsi_vpd_id_descriptor *)(&desc1->identifier[0] +
9602 CTL_WWPN_LEN);
9603 desc3 = (struct scsi_vpd_id_descriptor *)(&desc2->identifier[0] +
9604 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9605
9606 /*
9607 * The control device is always connected. The disk device, on the
9608 * other hand, may not be online all the time.
9609 */
9610 if (lun != NULL)
9611 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9612 lun->be_lun->lun_type;
9613 else
9614 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9615
9616 devid_ptr->page_code = SVPD_DEVICE_ID;
9617
9618 scsi_ulto2b(devid_len - 4, devid_ptr->length);
9619
9620 mtx_lock(&ctl_softc->ctl_lock);
9621
9622 /*
9623 * For Fibre channel,
9624 */
9625 if (fe->port_type == CTL_PORT_FC)
9626 {
9627 desc->proto_codeset = (SCSI_PROTO_FC << 4) |
9628 SVPD_ID_CODESET_ASCII;
9629 desc1->proto_codeset = (SCSI_PROTO_FC << 4) |
9630 SVPD_ID_CODESET_BINARY;
9631 }
9632 else
9633 {
9634 desc->proto_codeset = (SCSI_PROTO_SPI << 4) |
9635 SVPD_ID_CODESET_ASCII;
9636 desc1->proto_codeset = (SCSI_PROTO_SPI << 4) |
9637 SVPD_ID_CODESET_BINARY;
9638 }
9639 desc2->proto_codeset = desc3->proto_codeset = desc1->proto_codeset;
9640 mtx_unlock(&ctl_softc->ctl_lock);
9641
9642 /*
9643 * We're using a LUN association here. i.e., this device ID is a
9644 * per-LUN identifier.
9645 */
9646 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
9647 desc->length = sizeof(*t10id) + CTL_DEVID_LEN;
9648 if (lun == NULL || (val = ctl_get_opt(lun->be_lun, "vendor")) == NULL) {
9649 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
9650 } else {
9651 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
9652 strncpy(t10id->vendor, val,
9653 min(sizeof(t10id->vendor), strlen(val)));
9654 }
9655
9656 /*
9657 * desc1 is for the WWPN which is a port asscociation.
9658 */
9659 desc1->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | SVPD_ID_TYPE_NAA;
9660 desc1->length = CTL_WWPN_LEN;
9661 /* XXX Call Reggie's get_WWNN func here then add port # to the end */
9662 /* For testing just create the WWPN */
9663#if 0
9664 ddb_GetWWNN((char *)desc1->identifier);
9665
9666 /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */
9667 /* This is so Copancontrol will return something sane */
9668 if (ctsio->io_hdr.nexus.targ_port!=0 &&
9669 ctsio->io_hdr.nexus.targ_port!=8)
9670 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port-1;
9671 else
9672 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port;
9673#endif
9674
9675 be64enc(desc1->identifier, fe->wwpn);
9676
9677 /*
9678 * desc2 is for the Relative Target Port(type 4h) identifier
9679 */
9680 desc2->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT
9681 | SVPD_ID_TYPE_RELTARG;
9682 desc2->length = 4;
9683//#if 0
9684 /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */
9685 /* This is so Copancontrol will return something sane */
9686 if (ctsio->io_hdr.nexus.targ_port!=0 &&
9687 ctsio->io_hdr.nexus.targ_port!=8)
9688 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port - 1;
9689 else
9690 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port;
9691//#endif
9692
9693 /*
9694 * desc3 is for the Target Port Group(type 5h) identifier
9695 */
9696 desc3->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT
9697 | SVPD_ID_TYPE_TPORTGRP;
9698 desc3->length = 4;
9699 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS || ctl_is_single)
9700 desc3->identifier[3] = 1;
9701 else
9702 desc3->identifier[3] = 2;
9703
9704#ifdef CTL_USE_BACKEND_SN
9705 /*
9706 * If we've actually got a backend, copy the device id from the
9707 * per-LUN data. Otherwise, set it to all spaces.
9708 */
9709 if (lun != NULL) {
9710 /*
9711 * Copy the backend's LUN ID.
9712 */
9713 strncpy((char *)t10id->vendor_spec_id,
9714 (char *)lun->be_lun->device_id, CTL_DEVID_LEN);
9715 } else {
9716 /*
9717 * No backend, set this to spaces.
9718 */
9719 memset(t10id->vendor_spec_id, 0x20, CTL_DEVID_LEN);
9720 }
9721#else
9722 snprintf(tmpstr, sizeof(tmpstr), "MYDEVICEIDIS%4d",
9723 (lun != NULL) ? (int)lun->lun : 0);
9724 strncpy(t10id->vendor_spec_id, tmpstr, ctl_min(CTL_DEVID_LEN,
9725 sizeof(tmpstr)));
9726#endif
9727
9728 ctsio->scsi_status = SCSI_STATUS_OK;
9729
9730 ctsio->be_move_done = ctl_config_move_done;
9731 ctl_datamove((union ctl_io *)ctsio);
9732
9733 return (CTL_RETVAL_COMPLETE);
9734}
9735
9736static int
9737ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9738{
9739 struct scsi_vpd_block_limits *bl_ptr;
9740 struct ctl_lun *lun;
9741 int bs;
9742
9743 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9744 bs = lun->be_lun->blocksize;
9745
9746 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9747 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9748 ctsio->kern_sg_entries = 0;
9749
9750 if (sizeof(*bl_ptr) < alloc_len) {
9751 ctsio->residual = alloc_len - sizeof(*bl_ptr);
9752 ctsio->kern_data_len = sizeof(*bl_ptr);
9753 ctsio->kern_total_len = sizeof(*bl_ptr);
9754 } else {
9755 ctsio->residual = 0;
9756 ctsio->kern_data_len = alloc_len;
9757 ctsio->kern_total_len = alloc_len;
9758 }
9759 ctsio->kern_data_resid = 0;
9760 ctsio->kern_rel_offset = 0;
9761 ctsio->kern_sg_entries = 0;
9762
9763 /*
9764 * The control device is always connected. The disk device, on the
9765 * other hand, may not be online all the time. Need to change this
9766 * to figure out whether the disk device is actually online or not.
9767 */
9768 if (lun != NULL)
9769 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9770 lun->be_lun->lun_type;
9771 else
9772 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9773
9774 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9775 scsi_ulto2b(sizeof(*bl_ptr), bl_ptr->page_length);
9776 bl_ptr->max_cmp_write_len = 0xff;
9529 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9530 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len);
9531 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9532 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
9533 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
9534 }
9535 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
9536
9537 ctsio->scsi_status = SCSI_STATUS_OK;
9538 ctsio->be_move_done = ctl_config_move_done;
9539 ctl_datamove((union ctl_io *)ctsio);
9540
9541 return (CTL_RETVAL_COMPLETE);
9542}
9543
9544static int
9545ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9546{
9547 struct scsi_vpd_logical_block_prov *lbp_ptr;
9548 struct ctl_lun *lun;
9549 int bs;
9550
9551 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9552 bs = lun->be_lun->blocksize;
9553
9554 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
9555 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
9556 ctsio->kern_sg_entries = 0;
9557
9558 if (sizeof(*lbp_ptr) < alloc_len) {
9559 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
9560 ctsio->kern_data_len = sizeof(*lbp_ptr);
9561 ctsio->kern_total_len = sizeof(*lbp_ptr);
9562 } else {
9563 ctsio->residual = 0;
9564 ctsio->kern_data_len = alloc_len;
9565 ctsio->kern_total_len = alloc_len;
9566 }
9567 ctsio->kern_data_resid = 0;
9568 ctsio->kern_rel_offset = 0;
9569 ctsio->kern_sg_entries = 0;
9570
9571 /*
9572 * The control device is always connected. The disk device, on the
9573 * other hand, may not be online all the time. Need to change this
9574 * to figure out whether the disk device is actually online or not.
9575 */
9576 if (lun != NULL)
9577 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9578 lun->be_lun->lun_type;
9579 else
9580 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9581
9582 lbp_ptr->page_code = SVPD_LBP;
9583 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
9584 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | SVPD_LBP_WS10;
9585
9586 ctsio->scsi_status = SCSI_STATUS_OK;
9587 ctsio->be_move_done = ctl_config_move_done;
9588 ctl_datamove((union ctl_io *)ctsio);
9589
9590 return (CTL_RETVAL_COMPLETE);
9591}
9592
9593static int
9594ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
9595{
9596 struct scsi_inquiry *cdb;
9597 struct ctl_lun *lun;
9598 int alloc_len, retval;
9599
9600 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9601 cdb = (struct scsi_inquiry *)ctsio->cdb;
9602
9603 retval = CTL_RETVAL_COMPLETE;
9604
9605 alloc_len = scsi_2btoul(cdb->length);
9606
9607 switch (cdb->page_code) {
9608 case SVPD_SUPPORTED_PAGES:
9609 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
9610 break;
9611 case SVPD_UNIT_SERIAL_NUMBER:
9612 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
9613 break;
9614 case SVPD_DEVICE_ID:
9615 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
9616 break;
9617 case SVPD_BLOCK_LIMITS:
9618 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
9619 break;
9620 case SVPD_LBP:
9621 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
9622 break;
9623 default:
9624 ctl_set_invalid_field(ctsio,
9625 /*sks_valid*/ 1,
9626 /*command*/ 1,
9627 /*field*/ 2,
9628 /*bit_valid*/ 0,
9629 /*bit*/ 0);
9630 ctl_done((union ctl_io *)ctsio);
9631 retval = CTL_RETVAL_COMPLETE;
9632 break;
9633 }
9634
9635 return (retval);
9636}
9637
9638static int
9639ctl_inquiry_std(struct ctl_scsiio *ctsio)
9640{
9641 struct scsi_inquiry_data *inq_ptr;
9642 struct scsi_inquiry *cdb;
9643 struct ctl_softc *ctl_softc;
9644 struct ctl_lun *lun;
9645 char *val;
9646 uint32_t alloc_len;
9647 int is_fc;
9648
9649 ctl_softc = control_softc;
9650
9651 /*
9652 * Figure out whether we're talking to a Fibre Channel port or not.
9653 * We treat the ioctl front end, and any SCSI adapters, as packetized
9654 * SCSI front ends.
9655 */
9656 mtx_lock(&ctl_softc->ctl_lock);
9657 if (ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type !=
9658 CTL_PORT_FC)
9659 is_fc = 0;
9660 else
9661 is_fc = 1;
9662 mtx_unlock(&ctl_softc->ctl_lock);
9663
9664 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9665 cdb = (struct scsi_inquiry *)ctsio->cdb;
9666 alloc_len = scsi_2btoul(cdb->length);
9667
9668 /*
9669 * We malloc the full inquiry data size here and fill it
9670 * in. If the user only asks for less, we'll give him
9671 * that much.
9672 */
9673 ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK | M_ZERO);
9674 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
9675 ctsio->kern_sg_entries = 0;
9676 ctsio->kern_data_resid = 0;
9677 ctsio->kern_rel_offset = 0;
9678
9679 if (sizeof(*inq_ptr) < alloc_len) {
9680 ctsio->residual = alloc_len - sizeof(*inq_ptr);
9681 ctsio->kern_data_len = sizeof(*inq_ptr);
9682 ctsio->kern_total_len = sizeof(*inq_ptr);
9683 } else {
9684 ctsio->residual = 0;
9685 ctsio->kern_data_len = alloc_len;
9686 ctsio->kern_total_len = alloc_len;
9687 }
9688
9689 /*
9690 * If we have a LUN configured, report it as connected. Otherwise,
9691 * report that it is offline or no device is supported, depending
9692 * on the value of inquiry_pq_no_lun.
9693 *
9694 * According to the spec (SPC-4 r34), the peripheral qualifier
9695 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario:
9696 *
9697 * "A peripheral device having the specified peripheral device type
9698 * is not connected to this logical unit. However, the device
9699 * server is capable of supporting the specified peripheral device
9700 * type on this logical unit."
9701 *
9702 * According to the same spec, the peripheral qualifier
9703 * SID_QUAL_BAD_LU (011b) is used in this scenario:
9704 *
9705 * "The device server is not capable of supporting a peripheral
9706 * device on this logical unit. For this peripheral qualifier the
9707 * peripheral device type shall be set to 1Fh. All other peripheral
9708 * device type values are reserved for this peripheral qualifier."
9709 *
9710 * Given the text, it would seem that we probably want to report that
9711 * the LUN is offline here. There is no LUN connected, but we can
9712 * support a LUN at the given LUN number.
9713 *
9714 * In the real world, though, it sounds like things are a little
9715 * different:
9716 *
9717 * - Linux, when presented with a LUN with the offline peripheral
9718 * qualifier, will create an sg driver instance for it. So when
9719 * you attach it to CTL, you wind up with a ton of sg driver
9720 * instances. (One for every LUN that Linux bothered to probe.)
9721 * Linux does this despite the fact that it issues a REPORT LUNs
9722 * to LUN 0 to get the inventory of supported LUNs.
9723 *
9724 * - There is other anecdotal evidence (from Emulex folks) about
9725 * arrays that use the offline peripheral qualifier for LUNs that
9726 * are on the "passive" path in an active/passive array.
9727 *
9728 * So the solution is provide a hopefully reasonable default
9729 * (return bad/no LUN) and allow the user to change the behavior
9730 * with a tunable/sysctl variable.
9731 */
9732 if (lun != NULL)
9733 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9734 lun->be_lun->lun_type;
9735 else if (ctl_softc->inquiry_pq_no_lun == 0)
9736 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9737 else
9738 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
9739
9740 /* RMB in byte 2 is 0 */
9741 inq_ptr->version = SCSI_REV_SPC3;
9742
9743 /*
9744 * According to SAM-3, even if a device only supports a single
9745 * level of LUN addressing, it should still set the HISUP bit:
9746 *
9747 * 4.9.1 Logical unit numbers overview
9748 *
9749 * All logical unit number formats described in this standard are
9750 * hierarchical in structure even when only a single level in that
9751 * hierarchy is used. The HISUP bit shall be set to one in the
9752 * standard INQUIRY data (see SPC-2) when any logical unit number
9753 * format described in this standard is used. Non-hierarchical
9754 * formats are outside the scope of this standard.
9755 *
9756 * Therefore we set the HiSup bit here.
9757 *
9758 * The reponse format is 2, per SPC-3.
9759 */
9760 inq_ptr->response_format = SID_HiSup | 2;
9761
9762 inq_ptr->additional_length = sizeof(*inq_ptr) - 4;
9763 CTL_DEBUG_PRINT(("additional_length = %d\n",
9764 inq_ptr->additional_length));
9765
9766 inq_ptr->spc3_flags = SPC3_SID_TPGS_IMPLICIT;
9767 /* 16 bit addressing */
9768 if (is_fc == 0)
9769 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
9770 /* XXX set the SID_MultiP bit here if we're actually going to
9771 respond on multiple ports */
9772 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
9773
9774 /* 16 bit data bus, synchronous transfers */
9775 /* XXX these flags don't apply for FC */
9776 if (is_fc == 0)
9777 inq_ptr->flags = SID_WBus16 | SID_Sync;
9778 /*
9779 * XXX KDM do we want to support tagged queueing on the control
9780 * device at all?
9781 */
9782 if ((lun == NULL)
9783 || (lun->be_lun->lun_type != T_PROCESSOR))
9784 inq_ptr->flags |= SID_CmdQue;
9785 /*
9786 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
9787 * We have 8 bytes for the vendor name, and 16 bytes for the device
9788 * name and 4 bytes for the revision.
9789 */
9790 if (lun == NULL || (val = ctl_get_opt(lun->be_lun, "vendor")) == NULL) {
9791 strcpy(inq_ptr->vendor, CTL_VENDOR);
9792 } else {
9793 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
9794 strncpy(inq_ptr->vendor, val,
9795 min(sizeof(inq_ptr->vendor), strlen(val)));
9796 }
9797 if (lun == NULL) {
9798 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
9799 } else if ((val = ctl_get_opt(lun->be_lun, "product")) == NULL) {
9800 switch (lun->be_lun->lun_type) {
9801 case T_DIRECT:
9802 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
9803 break;
9804 case T_PROCESSOR:
9805 strcpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT);
9806 break;
9807 default:
9808 strcpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT);
9809 break;
9810 }
9811 } else {
9812 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
9813 strncpy(inq_ptr->product, val,
9814 min(sizeof(inq_ptr->product), strlen(val)));
9815 }
9816
9817 /*
9818 * XXX make this a macro somewhere so it automatically gets
9819 * incremented when we make changes.
9820 */
9821 if (lun == NULL || (val = ctl_get_opt(lun->be_lun, "revision")) == NULL) {
9822 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
9823 } else {
9824 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
9825 strncpy(inq_ptr->revision, val,
9826 min(sizeof(inq_ptr->revision), strlen(val)));
9827 }
9828
9829 /*
9830 * For parallel SCSI, we support double transition and single
9831 * transition clocking. We also support QAS (Quick Arbitration
9832 * and Selection) and Information Unit transfers on both the
9833 * control and array devices.
9834 */
9835 if (is_fc == 0)
9836 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
9837 SID_SPI_IUS;
9838
9839 /* SAM-3 */
9840 scsi_ulto2b(0x0060, inq_ptr->version1);
9841 /* SPC-3 (no version claimed) XXX should we claim a version? */
9842 scsi_ulto2b(0x0300, inq_ptr->version2);
9843 if (is_fc) {
9844 /* FCP-2 ANSI INCITS.350:2003 */
9845 scsi_ulto2b(0x0917, inq_ptr->version3);
9846 } else {
9847 /* SPI-4 ANSI INCITS.362:200x */
9848 scsi_ulto2b(0x0B56, inq_ptr->version3);
9849 }
9850
9851 if (lun == NULL) {
9852 /* SBC-2 (no version claimed) XXX should we claim a version? */
9853 scsi_ulto2b(0x0320, inq_ptr->version4);
9854 } else {
9855 switch (lun->be_lun->lun_type) {
9856 case T_DIRECT:
9857 /*
9858 * SBC-2 (no version claimed) XXX should we claim a
9859 * version?
9860 */
9861 scsi_ulto2b(0x0320, inq_ptr->version4);
9862 break;
9863 case T_PROCESSOR:
9864 default:
9865 break;
9866 }
9867 }
9868
9869 ctsio->scsi_status = SCSI_STATUS_OK;
9870 if (ctsio->kern_data_len > 0) {
9871 ctsio->be_move_done = ctl_config_move_done;
9872 ctl_datamove((union ctl_io *)ctsio);
9873 } else {
9874 ctsio->io_hdr.status = CTL_SUCCESS;
9875 ctl_done((union ctl_io *)ctsio);
9876 }
9877
9878 return (CTL_RETVAL_COMPLETE);
9879}
9880
9881int
9882ctl_inquiry(struct ctl_scsiio *ctsio)
9883{
9884 struct scsi_inquiry *cdb;
9885 int retval;
9886
9887 cdb = (struct scsi_inquiry *)ctsio->cdb;
9888
9889 retval = 0;
9890
9891 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
9892
9893 /*
9894 * Right now, we don't support the CmdDt inquiry information.
9895 * This would be nice to support in the future. When we do
9896 * support it, we should change this test so that it checks to make
9897 * sure SI_EVPD and SI_CMDDT aren't both set at the same time.
9898 */
9899#ifdef notyet
9900 if (((cdb->byte2 & SI_EVPD)
9901 && (cdb->byte2 & SI_CMDDT)))
9902#endif
9903 if (cdb->byte2 & SI_CMDDT) {
9904 /*
9905 * Point to the SI_CMDDT bit. We might change this
9906 * when we support SI_CMDDT, but since both bits would be
9907 * "wrong", this should probably just stay as-is then.
9908 */
9909 ctl_set_invalid_field(ctsio,
9910 /*sks_valid*/ 1,
9911 /*command*/ 1,
9912 /*field*/ 1,
9913 /*bit_valid*/ 1,
9914 /*bit*/ 1);
9915 ctl_done((union ctl_io *)ctsio);
9916 return (CTL_RETVAL_COMPLETE);
9917 }
9918 if (cdb->byte2 & SI_EVPD)
9919 retval = ctl_inquiry_evpd(ctsio);
9920#ifdef notyet
9921 else if (cdb->byte2 & SI_CMDDT)
9922 retval = ctl_inquiry_cmddt(ctsio);
9923#endif
9924 else
9925 retval = ctl_inquiry_std(ctsio);
9926
9927 return (retval);
9928}
9929
9930/*
9931 * For known CDB types, parse the LBA and length.
9932 */
9933static int
9934ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len)
9935{
9936 if (io->io_hdr.io_type != CTL_IO_SCSI)
9937 return (1);
9938
9939 switch (io->scsiio.cdb[0]) {
9777 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9778 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len);
9779 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9780 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
9781 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
9782 }
9783 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
9784
9785 ctsio->scsi_status = SCSI_STATUS_OK;
9786 ctsio->be_move_done = ctl_config_move_done;
9787 ctl_datamove((union ctl_io *)ctsio);
9788
9789 return (CTL_RETVAL_COMPLETE);
9790}
9791
9792static int
9793ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9794{
9795 struct scsi_vpd_logical_block_prov *lbp_ptr;
9796 struct ctl_lun *lun;
9797 int bs;
9798
9799 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9800 bs = lun->be_lun->blocksize;
9801
9802 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
9803 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
9804 ctsio->kern_sg_entries = 0;
9805
9806 if (sizeof(*lbp_ptr) < alloc_len) {
9807 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
9808 ctsio->kern_data_len = sizeof(*lbp_ptr);
9809 ctsio->kern_total_len = sizeof(*lbp_ptr);
9810 } else {
9811 ctsio->residual = 0;
9812 ctsio->kern_data_len = alloc_len;
9813 ctsio->kern_total_len = alloc_len;
9814 }
9815 ctsio->kern_data_resid = 0;
9816 ctsio->kern_rel_offset = 0;
9817 ctsio->kern_sg_entries = 0;
9818
9819 /*
9820 * The control device is always connected. The disk device, on the
9821 * other hand, may not be online all the time. Need to change this
9822 * to figure out whether the disk device is actually online or not.
9823 */
9824 if (lun != NULL)
9825 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9826 lun->be_lun->lun_type;
9827 else
9828 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9829
9830 lbp_ptr->page_code = SVPD_LBP;
9831 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
9832 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | SVPD_LBP_WS10;
9833
9834 ctsio->scsi_status = SCSI_STATUS_OK;
9835 ctsio->be_move_done = ctl_config_move_done;
9836 ctl_datamove((union ctl_io *)ctsio);
9837
9838 return (CTL_RETVAL_COMPLETE);
9839}
9840
9841static int
9842ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
9843{
9844 struct scsi_inquiry *cdb;
9845 struct ctl_lun *lun;
9846 int alloc_len, retval;
9847
9848 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9849 cdb = (struct scsi_inquiry *)ctsio->cdb;
9850
9851 retval = CTL_RETVAL_COMPLETE;
9852
9853 alloc_len = scsi_2btoul(cdb->length);
9854
9855 switch (cdb->page_code) {
9856 case SVPD_SUPPORTED_PAGES:
9857 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
9858 break;
9859 case SVPD_UNIT_SERIAL_NUMBER:
9860 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
9861 break;
9862 case SVPD_DEVICE_ID:
9863 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
9864 break;
9865 case SVPD_BLOCK_LIMITS:
9866 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
9867 break;
9868 case SVPD_LBP:
9869 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
9870 break;
9871 default:
9872 ctl_set_invalid_field(ctsio,
9873 /*sks_valid*/ 1,
9874 /*command*/ 1,
9875 /*field*/ 2,
9876 /*bit_valid*/ 0,
9877 /*bit*/ 0);
9878 ctl_done((union ctl_io *)ctsio);
9879 retval = CTL_RETVAL_COMPLETE;
9880 break;
9881 }
9882
9883 return (retval);
9884}
9885
9886static int
9887ctl_inquiry_std(struct ctl_scsiio *ctsio)
9888{
9889 struct scsi_inquiry_data *inq_ptr;
9890 struct scsi_inquiry *cdb;
9891 struct ctl_softc *ctl_softc;
9892 struct ctl_lun *lun;
9893 char *val;
9894 uint32_t alloc_len;
9895 int is_fc;
9896
9897 ctl_softc = control_softc;
9898
9899 /*
9900 * Figure out whether we're talking to a Fibre Channel port or not.
9901 * We treat the ioctl front end, and any SCSI adapters, as packetized
9902 * SCSI front ends.
9903 */
9904 mtx_lock(&ctl_softc->ctl_lock);
9905 if (ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type !=
9906 CTL_PORT_FC)
9907 is_fc = 0;
9908 else
9909 is_fc = 1;
9910 mtx_unlock(&ctl_softc->ctl_lock);
9911
9912 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9913 cdb = (struct scsi_inquiry *)ctsio->cdb;
9914 alloc_len = scsi_2btoul(cdb->length);
9915
9916 /*
9917 * We malloc the full inquiry data size here and fill it
9918 * in. If the user only asks for less, we'll give him
9919 * that much.
9920 */
9921 ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK | M_ZERO);
9922 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
9923 ctsio->kern_sg_entries = 0;
9924 ctsio->kern_data_resid = 0;
9925 ctsio->kern_rel_offset = 0;
9926
9927 if (sizeof(*inq_ptr) < alloc_len) {
9928 ctsio->residual = alloc_len - sizeof(*inq_ptr);
9929 ctsio->kern_data_len = sizeof(*inq_ptr);
9930 ctsio->kern_total_len = sizeof(*inq_ptr);
9931 } else {
9932 ctsio->residual = 0;
9933 ctsio->kern_data_len = alloc_len;
9934 ctsio->kern_total_len = alloc_len;
9935 }
9936
9937 /*
9938 * If we have a LUN configured, report it as connected. Otherwise,
9939 * report that it is offline or no device is supported, depending
9940 * on the value of inquiry_pq_no_lun.
9941 *
9942 * According to the spec (SPC-4 r34), the peripheral qualifier
9943 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario:
9944 *
9945 * "A peripheral device having the specified peripheral device type
9946 * is not connected to this logical unit. However, the device
9947 * server is capable of supporting the specified peripheral device
9948 * type on this logical unit."
9949 *
9950 * According to the same spec, the peripheral qualifier
9951 * SID_QUAL_BAD_LU (011b) is used in this scenario:
9952 *
9953 * "The device server is not capable of supporting a peripheral
9954 * device on this logical unit. For this peripheral qualifier the
9955 * peripheral device type shall be set to 1Fh. All other peripheral
9956 * device type values are reserved for this peripheral qualifier."
9957 *
9958 * Given the text, it would seem that we probably want to report that
9959 * the LUN is offline here. There is no LUN connected, but we can
9960 * support a LUN at the given LUN number.
9961 *
9962 * In the real world, though, it sounds like things are a little
9963 * different:
9964 *
9965 * - Linux, when presented with a LUN with the offline peripheral
9966 * qualifier, will create an sg driver instance for it. So when
9967 * you attach it to CTL, you wind up with a ton of sg driver
9968 * instances. (One for every LUN that Linux bothered to probe.)
9969 * Linux does this despite the fact that it issues a REPORT LUNs
9970 * to LUN 0 to get the inventory of supported LUNs.
9971 *
9972 * - There is other anecdotal evidence (from Emulex folks) about
9973 * arrays that use the offline peripheral qualifier for LUNs that
9974 * are on the "passive" path in an active/passive array.
9975 *
9976 * So the solution is provide a hopefully reasonable default
9977 * (return bad/no LUN) and allow the user to change the behavior
9978 * with a tunable/sysctl variable.
9979 */
9980 if (lun != NULL)
9981 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9982 lun->be_lun->lun_type;
9983 else if (ctl_softc->inquiry_pq_no_lun == 0)
9984 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9985 else
9986 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
9987
9988 /* RMB in byte 2 is 0 */
9989 inq_ptr->version = SCSI_REV_SPC3;
9990
9991 /*
9992 * According to SAM-3, even if a device only supports a single
9993 * level of LUN addressing, it should still set the HISUP bit:
9994 *
9995 * 4.9.1 Logical unit numbers overview
9996 *
9997 * All logical unit number formats described in this standard are
9998 * hierarchical in structure even when only a single level in that
9999 * hierarchy is used. The HISUP bit shall be set to one in the
10000 * standard INQUIRY data (see SPC-2) when any logical unit number
10001 * format described in this standard is used. Non-hierarchical
10002 * formats are outside the scope of this standard.
10003 *
10004 * Therefore we set the HiSup bit here.
10005 *
10006 * The reponse format is 2, per SPC-3.
10007 */
10008 inq_ptr->response_format = SID_HiSup | 2;
10009
10010 inq_ptr->additional_length = sizeof(*inq_ptr) - 4;
10011 CTL_DEBUG_PRINT(("additional_length = %d\n",
10012 inq_ptr->additional_length));
10013
10014 inq_ptr->spc3_flags = SPC3_SID_TPGS_IMPLICIT;
10015 /* 16 bit addressing */
10016 if (is_fc == 0)
10017 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10018 /* XXX set the SID_MultiP bit here if we're actually going to
10019 respond on multiple ports */
10020 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10021
10022 /* 16 bit data bus, synchronous transfers */
10023 /* XXX these flags don't apply for FC */
10024 if (is_fc == 0)
10025 inq_ptr->flags = SID_WBus16 | SID_Sync;
10026 /*
10027 * XXX KDM do we want to support tagged queueing on the control
10028 * device at all?
10029 */
10030 if ((lun == NULL)
10031 || (lun->be_lun->lun_type != T_PROCESSOR))
10032 inq_ptr->flags |= SID_CmdQue;
10033 /*
10034 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10035 * We have 8 bytes for the vendor name, and 16 bytes for the device
10036 * name and 4 bytes for the revision.
10037 */
10038 if (lun == NULL || (val = ctl_get_opt(lun->be_lun, "vendor")) == NULL) {
10039 strcpy(inq_ptr->vendor, CTL_VENDOR);
10040 } else {
10041 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10042 strncpy(inq_ptr->vendor, val,
10043 min(sizeof(inq_ptr->vendor), strlen(val)));
10044 }
10045 if (lun == NULL) {
10046 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
10047 } else if ((val = ctl_get_opt(lun->be_lun, "product")) == NULL) {
10048 switch (lun->be_lun->lun_type) {
10049 case T_DIRECT:
10050 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
10051 break;
10052 case T_PROCESSOR:
10053 strcpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT);
10054 break;
10055 default:
10056 strcpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT);
10057 break;
10058 }
10059 } else {
10060 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10061 strncpy(inq_ptr->product, val,
10062 min(sizeof(inq_ptr->product), strlen(val)));
10063 }
10064
10065 /*
10066 * XXX make this a macro somewhere so it automatically gets
10067 * incremented when we make changes.
10068 */
10069 if (lun == NULL || (val = ctl_get_opt(lun->be_lun, "revision")) == NULL) {
10070 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10071 } else {
10072 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10073 strncpy(inq_ptr->revision, val,
10074 min(sizeof(inq_ptr->revision), strlen(val)));
10075 }
10076
10077 /*
10078 * For parallel SCSI, we support double transition and single
10079 * transition clocking. We also support QAS (Quick Arbitration
10080 * and Selection) and Information Unit transfers on both the
10081 * control and array devices.
10082 */
10083 if (is_fc == 0)
10084 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10085 SID_SPI_IUS;
10086
10087 /* SAM-3 */
10088 scsi_ulto2b(0x0060, inq_ptr->version1);
10089 /* SPC-3 (no version claimed) XXX should we claim a version? */
10090 scsi_ulto2b(0x0300, inq_ptr->version2);
10091 if (is_fc) {
10092 /* FCP-2 ANSI INCITS.350:2003 */
10093 scsi_ulto2b(0x0917, inq_ptr->version3);
10094 } else {
10095 /* SPI-4 ANSI INCITS.362:200x */
10096 scsi_ulto2b(0x0B56, inq_ptr->version3);
10097 }
10098
10099 if (lun == NULL) {
10100 /* SBC-2 (no version claimed) XXX should we claim a version? */
10101 scsi_ulto2b(0x0320, inq_ptr->version4);
10102 } else {
10103 switch (lun->be_lun->lun_type) {
10104 case T_DIRECT:
10105 /*
10106 * SBC-2 (no version claimed) XXX should we claim a
10107 * version?
10108 */
10109 scsi_ulto2b(0x0320, inq_ptr->version4);
10110 break;
10111 case T_PROCESSOR:
10112 default:
10113 break;
10114 }
10115 }
10116
10117 ctsio->scsi_status = SCSI_STATUS_OK;
10118 if (ctsio->kern_data_len > 0) {
10119 ctsio->be_move_done = ctl_config_move_done;
10120 ctl_datamove((union ctl_io *)ctsio);
10121 } else {
10122 ctsio->io_hdr.status = CTL_SUCCESS;
10123 ctl_done((union ctl_io *)ctsio);
10124 }
10125
10126 return (CTL_RETVAL_COMPLETE);
10127}
10128
10129int
10130ctl_inquiry(struct ctl_scsiio *ctsio)
10131{
10132 struct scsi_inquiry *cdb;
10133 int retval;
10134
10135 cdb = (struct scsi_inquiry *)ctsio->cdb;
10136
10137 retval = 0;
10138
10139 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10140
10141 /*
10142 * Right now, we don't support the CmdDt inquiry information.
10143 * This would be nice to support in the future. When we do
10144 * support it, we should change this test so that it checks to make
10145 * sure SI_EVPD and SI_CMDDT aren't both set at the same time.
10146 */
10147#ifdef notyet
10148 if (((cdb->byte2 & SI_EVPD)
10149 && (cdb->byte2 & SI_CMDDT)))
10150#endif
10151 if (cdb->byte2 & SI_CMDDT) {
10152 /*
10153 * Point to the SI_CMDDT bit. We might change this
10154 * when we support SI_CMDDT, but since both bits would be
10155 * "wrong", this should probably just stay as-is then.
10156 */
10157 ctl_set_invalid_field(ctsio,
10158 /*sks_valid*/ 1,
10159 /*command*/ 1,
10160 /*field*/ 1,
10161 /*bit_valid*/ 1,
10162 /*bit*/ 1);
10163 ctl_done((union ctl_io *)ctsio);
10164 return (CTL_RETVAL_COMPLETE);
10165 }
10166 if (cdb->byte2 & SI_EVPD)
10167 retval = ctl_inquiry_evpd(ctsio);
10168#ifdef notyet
10169 else if (cdb->byte2 & SI_CMDDT)
10170 retval = ctl_inquiry_cmddt(ctsio);
10171#endif
10172 else
10173 retval = ctl_inquiry_std(ctsio);
10174
10175 return (retval);
10176}
10177
10178/*
10179 * For known CDB types, parse the LBA and length.
10180 */
10181static int
10182ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len)
10183{
10184 if (io->io_hdr.io_type != CTL_IO_SCSI)
10185 return (1);
10186
10187 switch (io->scsiio.cdb[0]) {
10188 case COMPARE_AND_WRITE: {
10189 struct scsi_compare_and_write *cdb;
10190
10191 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10192
10193 *lba = scsi_8btou64(cdb->addr);
10194 *len = cdb->length;
10195 break;
10196 }
9940 case READ_6:
9941 case WRITE_6: {
9942 struct scsi_rw_6 *cdb;
9943
9944 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
9945
9946 *lba = scsi_3btoul(cdb->addr);
9947 /* only 5 bits are valid in the most significant address byte */
9948 *lba &= 0x1fffff;
9949 *len = cdb->length;
9950 break;
9951 }
9952 case READ_10:
9953 case WRITE_10: {
9954 struct scsi_rw_10 *cdb;
9955
9956 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
9957
9958 *lba = scsi_4btoul(cdb->addr);
9959 *len = scsi_2btoul(cdb->length);
9960 break;
9961 }
9962 case WRITE_VERIFY_10: {
9963 struct scsi_write_verify_10 *cdb;
9964
9965 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
9966
9967 *lba = scsi_4btoul(cdb->addr);
9968 *len = scsi_2btoul(cdb->length);
9969 break;
9970 }
9971 case READ_12:
9972 case WRITE_12: {
9973 struct scsi_rw_12 *cdb;
9974
9975 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
9976
9977 *lba = scsi_4btoul(cdb->addr);
9978 *len = scsi_4btoul(cdb->length);
9979 break;
9980 }
9981 case WRITE_VERIFY_12: {
9982 struct scsi_write_verify_12 *cdb;
9983
9984 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
9985
9986 *lba = scsi_4btoul(cdb->addr);
9987 *len = scsi_4btoul(cdb->length);
9988 break;
9989 }
9990 case READ_16:
9991 case WRITE_16: {
9992 struct scsi_rw_16 *cdb;
9993
9994 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
9995
9996 *lba = scsi_8btou64(cdb->addr);
9997 *len = scsi_4btoul(cdb->length);
9998 break;
9999 }
10000 case WRITE_VERIFY_16: {
10001 struct scsi_write_verify_16 *cdb;
10002
10003 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10004
10005
10006 *lba = scsi_8btou64(cdb->addr);
10007 *len = scsi_4btoul(cdb->length);
10008 break;
10009 }
10010 case WRITE_SAME_10: {
10011 struct scsi_write_same_10 *cdb;
10012
10013 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10014
10015 *lba = scsi_4btoul(cdb->addr);
10016 *len = scsi_2btoul(cdb->length);
10017 break;
10018 }
10019 case WRITE_SAME_16: {
10020 struct scsi_write_same_16 *cdb;
10021
10022 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10023
10024 *lba = scsi_8btou64(cdb->addr);
10025 *len = scsi_4btoul(cdb->length);
10026 break;
10027 }
10197 case READ_6:
10198 case WRITE_6: {
10199 struct scsi_rw_6 *cdb;
10200
10201 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10202
10203 *lba = scsi_3btoul(cdb->addr);
10204 /* only 5 bits are valid in the most significant address byte */
10205 *lba &= 0x1fffff;
10206 *len = cdb->length;
10207 break;
10208 }
10209 case READ_10:
10210 case WRITE_10: {
10211 struct scsi_rw_10 *cdb;
10212
10213 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10214
10215 *lba = scsi_4btoul(cdb->addr);
10216 *len = scsi_2btoul(cdb->length);
10217 break;
10218 }
10219 case WRITE_VERIFY_10: {
10220 struct scsi_write_verify_10 *cdb;
10221
10222 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10223
10224 *lba = scsi_4btoul(cdb->addr);
10225 *len = scsi_2btoul(cdb->length);
10226 break;
10227 }
10228 case READ_12:
10229 case WRITE_12: {
10230 struct scsi_rw_12 *cdb;
10231
10232 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10233
10234 *lba = scsi_4btoul(cdb->addr);
10235 *len = scsi_4btoul(cdb->length);
10236 break;
10237 }
10238 case WRITE_VERIFY_12: {
10239 struct scsi_write_verify_12 *cdb;
10240
10241 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10242
10243 *lba = scsi_4btoul(cdb->addr);
10244 *len = scsi_4btoul(cdb->length);
10245 break;
10246 }
10247 case READ_16:
10248 case WRITE_16: {
10249 struct scsi_rw_16 *cdb;
10250
10251 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10252
10253 *lba = scsi_8btou64(cdb->addr);
10254 *len = scsi_4btoul(cdb->length);
10255 break;
10256 }
10257 case WRITE_VERIFY_16: {
10258 struct scsi_write_verify_16 *cdb;
10259
10260 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10261
10262
10263 *lba = scsi_8btou64(cdb->addr);
10264 *len = scsi_4btoul(cdb->length);
10265 break;
10266 }
10267 case WRITE_SAME_10: {
10268 struct scsi_write_same_10 *cdb;
10269
10270 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10271
10272 *lba = scsi_4btoul(cdb->addr);
10273 *len = scsi_2btoul(cdb->length);
10274 break;
10275 }
10276 case WRITE_SAME_16: {
10277 struct scsi_write_same_16 *cdb;
10278
10279 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10280
10281 *lba = scsi_8btou64(cdb->addr);
10282 *len = scsi_4btoul(cdb->length);
10283 break;
10284 }
10285 case VERIFY_10: {
10286 struct scsi_verify_10 *cdb;
10287
10288 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10289
10290 *lba = scsi_4btoul(cdb->addr);
10291 *len = scsi_2btoul(cdb->length);
10292 break;
10293 }
10294 case VERIFY_12: {
10295 struct scsi_verify_12 *cdb;
10296
10297 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10298
10299 *lba = scsi_4btoul(cdb->addr);
10300 *len = scsi_4btoul(cdb->length);
10301 break;
10302 }
10303 case VERIFY_16: {
10304 struct scsi_verify_16 *cdb;
10305
10306 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10307
10308 *lba = scsi_8btou64(cdb->addr);
10309 *len = scsi_4btoul(cdb->length);
10310 break;
10311 }
10028 default:
10029 return (1);
10030 break; /* NOTREACHED */
10031 }
10032
10033 return (0);
10034}
10035
10036static ctl_action
10037ctl_extent_check_lba(uint64_t lba1, uint32_t len1, uint64_t lba2, uint32_t len2)
10038{
10039 uint64_t endlba1, endlba2;
10040
10041 endlba1 = lba1 + len1 - 1;
10042 endlba2 = lba2 + len2 - 1;
10043
10044 if ((endlba1 < lba2)
10045 || (endlba2 < lba1))
10046 return (CTL_ACTION_PASS);
10047 else
10048 return (CTL_ACTION_BLOCK);
10049}
10050
10051static ctl_action
10052ctl_extent_check(union ctl_io *io1, union ctl_io *io2)
10053{
10054 uint64_t lba1, lba2;
10055 uint32_t len1, len2;
10056 int retval;
10057
10058 retval = ctl_get_lba_len(io1, &lba1, &len1);
10059 if (retval != 0)
10060 return (CTL_ACTION_ERROR);
10061
10062 retval = ctl_get_lba_len(io2, &lba2, &len2);
10063 if (retval != 0)
10064 return (CTL_ACTION_ERROR);
10065
10066 return (ctl_extent_check_lba(lba1, len1, lba2, len2));
10067}
10068
10069static ctl_action
10070ctl_check_for_blockage(union ctl_io *pending_io, union ctl_io *ooa_io)
10071{
10072 struct ctl_cmd_entry *pending_entry, *ooa_entry;
10073 ctl_serialize_action *serialize_row;
10074
10075 /*
10076 * The initiator attempted multiple untagged commands at the same
10077 * time. Can't do that.
10078 */
10079 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10080 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10081 && ((pending_io->io_hdr.nexus.targ_port ==
10082 ooa_io->io_hdr.nexus.targ_port)
10083 && (pending_io->io_hdr.nexus.initid.id ==
10084 ooa_io->io_hdr.nexus.initid.id))
10085 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10086 return (CTL_ACTION_OVERLAP);
10087
10088 /*
10089 * The initiator attempted to send multiple tagged commands with
10090 * the same ID. (It's fine if different initiators have the same
10091 * tag ID.)
10092 *
10093 * Even if all of those conditions are true, we don't kill the I/O
10094 * if the command ahead of us has been aborted. We won't end up
10095 * sending it to the FETD, and it's perfectly legal to resend a
10096 * command with the same tag number as long as the previous
10097 * instance of this tag number has been aborted somehow.
10098 */
10099 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10100 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10101 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10102 && ((pending_io->io_hdr.nexus.targ_port ==
10103 ooa_io->io_hdr.nexus.targ_port)
10104 && (pending_io->io_hdr.nexus.initid.id ==
10105 ooa_io->io_hdr.nexus.initid.id))
10106 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10107 return (CTL_ACTION_OVERLAP_TAG);
10108
10109 /*
10110 * If we get a head of queue tag, SAM-3 says that we should
10111 * immediately execute it.
10112 *
10113 * What happens if this command would normally block for some other
10114 * reason? e.g. a request sense with a head of queue tag
10115 * immediately after a write. Normally that would block, but this
10116 * will result in its getting executed immediately...
10117 *
10118 * We currently return "pass" instead of "skip", so we'll end up
10119 * going through the rest of the queue to check for overlapped tags.
10120 *
10121 * XXX KDM check for other types of blockage first??
10122 */
10123 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10124 return (CTL_ACTION_PASS);
10125
10126 /*
10127 * Ordered tags have to block until all items ahead of them
10128 * have completed. If we get called with an ordered tag, we always
10129 * block, if something else is ahead of us in the queue.
10130 */
10131 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10132 return (CTL_ACTION_BLOCK);
10133
10134 /*
10135 * Simple tags get blocked until all head of queue and ordered tags
10136 * ahead of them have completed. I'm lumping untagged commands in
10137 * with simple tags here. XXX KDM is that the right thing to do?
10138 */
10139 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10140 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10141 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10142 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10143 return (CTL_ACTION_BLOCK);
10144
10145 pending_entry = &ctl_cmd_table[pending_io->scsiio.cdb[0]];
10146 ooa_entry = &ctl_cmd_table[ooa_io->scsiio.cdb[0]];
10147
10148 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10149
10150 switch (serialize_row[pending_entry->seridx]) {
10151 case CTL_SER_BLOCK:
10152 return (CTL_ACTION_BLOCK);
10153 break; /* NOTREACHED */
10154 case CTL_SER_EXTENT:
10155 return (ctl_extent_check(pending_io, ooa_io));
10156 break; /* NOTREACHED */
10157 case CTL_SER_PASS:
10158 return (CTL_ACTION_PASS);
10159 break; /* NOTREACHED */
10160 case CTL_SER_SKIP:
10161 return (CTL_ACTION_SKIP);
10162 break;
10163 default:
10164 panic("invalid serialization value %d",
10165 serialize_row[pending_entry->seridx]);
10166 break; /* NOTREACHED */
10167 }
10168
10169 return (CTL_ACTION_ERROR);
10170}
10171
10172/*
10173 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10174 * Assumptions:
10175 * - pending_io is generally either incoming, or on the blocked queue
10176 * - starting I/O is the I/O we want to start the check with.
10177 */
10178static ctl_action
10179ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10180 union ctl_io *starting_io)
10181{
10182 union ctl_io *ooa_io;
10183 ctl_action action;
10184
10185 mtx_assert(&control_softc->ctl_lock, MA_OWNED);
10186
10187 /*
10188 * Run back along the OOA queue, starting with the current
10189 * blocked I/O and going through every I/O before it on the
10190 * queue. If starting_io is NULL, we'll just end up returning
10191 * CTL_ACTION_PASS.
10192 */
10193 for (ooa_io = starting_io; ooa_io != NULL;
10194 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10195 ooa_links)){
10196
10197 /*
10198 * This routine just checks to see whether
10199 * cur_blocked is blocked by ooa_io, which is ahead
10200 * of it in the queue. It doesn't queue/dequeue
10201 * cur_blocked.
10202 */
10203 action = ctl_check_for_blockage(pending_io, ooa_io);
10204 switch (action) {
10205 case CTL_ACTION_BLOCK:
10206 case CTL_ACTION_OVERLAP:
10207 case CTL_ACTION_OVERLAP_TAG:
10208 case CTL_ACTION_SKIP:
10209 case CTL_ACTION_ERROR:
10210 return (action);
10211 break; /* NOTREACHED */
10212 case CTL_ACTION_PASS:
10213 break;
10214 default:
10215 panic("invalid action %d", action);
10216 break; /* NOTREACHED */
10217 }
10218 }
10219
10220 return (CTL_ACTION_PASS);
10221}
10222
10223/*
10224 * Assumptions:
10225 * - An I/O has just completed, and has been removed from the per-LUN OOA
10226 * queue, so some items on the blocked queue may now be unblocked.
10227 */
10228static int
10229ctl_check_blocked(struct ctl_lun *lun)
10230{
10231 union ctl_io *cur_blocked, *next_blocked;
10232
10233 mtx_assert(&control_softc->ctl_lock, MA_OWNED);
10234
10235 /*
10236 * Run forward from the head of the blocked queue, checking each
10237 * entry against the I/Os prior to it on the OOA queue to see if
10238 * there is still any blockage.
10239 *
10240 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
10241 * with our removing a variable on it while it is traversing the
10242 * list.
10243 */
10244 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
10245 cur_blocked != NULL; cur_blocked = next_blocked) {
10246 union ctl_io *prev_ooa;
10247 ctl_action action;
10248
10249 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
10250 blocked_links);
10251
10252 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
10253 ctl_ooaq, ooa_links);
10254
10255 /*
10256 * If cur_blocked happens to be the first item in the OOA
10257 * queue now, prev_ooa will be NULL, and the action
10258 * returned will just be CTL_ACTION_PASS.
10259 */
10260 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
10261
10262 switch (action) {
10263 case CTL_ACTION_BLOCK:
10264 /* Nothing to do here, still blocked */
10265 break;
10266 case CTL_ACTION_OVERLAP:
10267 case CTL_ACTION_OVERLAP_TAG:
10268 /*
10269 * This shouldn't happen! In theory we've already
10270 * checked this command for overlap...
10271 */
10272 break;
10273 case CTL_ACTION_PASS:
10274 case CTL_ACTION_SKIP: {
10275 struct ctl_softc *softc;
10276 struct ctl_cmd_entry *entry;
10277 uint32_t initidx;
10278 uint8_t opcode;
10279 int isc_retval;
10280
10281 /*
10282 * The skip case shouldn't happen, this transaction
10283 * should have never made it onto the blocked queue.
10284 */
10285 /*
10286 * This I/O is no longer blocked, we can remove it
10287 * from the blocked queue. Since this is a TAILQ
10288 * (doubly linked list), we can do O(1) removals
10289 * from any place on the list.
10290 */
10291 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
10292 blocked_links);
10293 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10294
10295 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){
10296 /*
10297 * Need to send IO back to original side to
10298 * run
10299 */
10300 union ctl_ha_msg msg_info;
10301
10302 msg_info.hdr.original_sc =
10303 cur_blocked->io_hdr.original_sc;
10304 msg_info.hdr.serializing_sc = cur_blocked;
10305 msg_info.hdr.msg_type = CTL_MSG_R2R;
10306 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
10307 &msg_info, sizeof(msg_info), 0)) >
10308 CTL_HA_STATUS_SUCCESS) {
10309 printf("CTL:Check Blocked error from "
10310 "ctl_ha_msg_send %d\n",
10311 isc_retval);
10312 }
10313 break;
10314 }
10315 opcode = cur_blocked->scsiio.cdb[0];
10316 entry = &ctl_cmd_table[opcode];
10317 softc = control_softc;
10318
10319 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus);
10320
10321 /*
10322 * Check this I/O for LUN state changes that may
10323 * have happened while this command was blocked.
10324 * The LUN state may have been changed by a command
10325 * ahead of us in the queue, so we need to re-check
10326 * for any states that can be caused by SCSI
10327 * commands.
10328 */
10329 if (ctl_scsiio_lun_check(softc, lun, entry,
10330 &cur_blocked->scsiio) == 0) {
10331 cur_blocked->io_hdr.flags |=
10332 CTL_FLAG_IS_WAS_ON_RTR;
10333 STAILQ_INSERT_TAIL(&lun->ctl_softc->rtr_queue,
10334 &cur_blocked->io_hdr, links);
10335 /*
10336 * In the non CTL_DONE_THREAD case, we need
10337 * to wake up the work thread here. When
10338 * we're processing completed requests from
10339 * the work thread context, we'll pop back
10340 * around and end up pulling things off the
10341 * RtR queue. When we aren't processing
10342 * things from the work thread context,
10343 * though, we won't ever check the RtR queue.
10344 * So we need to wake up the thread to clear
10345 * things off the queue. Otherwise this
10346 * transaction will just sit on the RtR queue
10347 * until a new I/O comes in. (Which may or
10348 * may not happen...)
10349 */
10350#ifndef CTL_DONE_THREAD
10351 ctl_wakeup_thread();
10352#endif
10353 } else
10354 ctl_done_lock(cur_blocked, /*have_lock*/ 1);
10355 break;
10356 }
10357 default:
10358 /*
10359 * This probably shouldn't happen -- we shouldn't
10360 * get CTL_ACTION_ERROR, or anything else.
10361 */
10362 break;
10363 }
10364 }
10365
10366 return (CTL_RETVAL_COMPLETE);
10367}
10368
10369/*
10370 * This routine (with one exception) checks LUN flags that can be set by
10371 * commands ahead of us in the OOA queue. These flags have to be checked
10372 * when a command initially comes in, and when we pull a command off the
10373 * blocked queue and are preparing to execute it. The reason we have to
10374 * check these flags for commands on the blocked queue is that the LUN
10375 * state may have been changed by a command ahead of us while we're on the
10376 * blocked queue.
10377 *
10378 * Ordering is somewhat important with these checks, so please pay
10379 * careful attention to the placement of any new checks.
10380 */
10381static int
10382ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
10383 struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
10384{
10385 int retval;
10386
10387 retval = 0;
10388
10389 /*
10390 * If this shelf is a secondary shelf controller, we have to reject
10391 * any media access commands.
10392 */
10393#if 0
10394 /* No longer needed for HA */
10395 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0)
10396 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) {
10397 ctl_set_lun_standby(ctsio);
10398 retval = 1;
10399 goto bailout;
10400 }
10401#endif
10402
10403 /*
10404 * Check for a reservation conflict. If this command isn't allowed
10405 * even on reserved LUNs, and if this initiator isn't the one who
10406 * reserved us, reject the command with a reservation conflict.
10407 */
10408 if ((lun->flags & CTL_LUN_RESERVED)
10409 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
10410 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
10411 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
10412 || (ctsio->io_hdr.nexus.targ_target.id !=
10413 lun->rsv_nexus.targ_target.id)) {
10414 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
10415 ctsio->io_hdr.status = CTL_SCSI_ERROR;
10416 retval = 1;
10417 goto bailout;
10418 }
10419 }
10420
10421 if ( (lun->flags & CTL_LUN_PR_RESERVED)
10422 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) {
10423 uint32_t residx;
10424
10425 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
10426 /*
10427 * if we aren't registered or it's a res holder type
10428 * reservation and this isn't the res holder then set a
10429 * conflict.
10430 * NOTE: Commands which might be allowed on write exclusive
10431 * type reservations are checked in the particular command
10432 * for a conflict. Read and SSU are the only ones.
10433 */
10434 if (!lun->per_res[residx].registered
10435 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
10436 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
10437 ctsio->io_hdr.status = CTL_SCSI_ERROR;
10438 retval = 1;
10439 goto bailout;
10440 }
10441
10442 }
10443
10444 if ((lun->flags & CTL_LUN_OFFLINE)
10445 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) {
10446 ctl_set_lun_not_ready(ctsio);
10447 retval = 1;
10448 goto bailout;
10449 }
10450
10451 /*
10452 * If the LUN is stopped, see if this particular command is allowed
10453 * for a stopped lun. Otherwise, reject it with 0x04,0x02.
10454 */
10455 if ((lun->flags & CTL_LUN_STOPPED)
10456 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
10457 /* "Logical unit not ready, initializing cmd. required" */
10458 ctl_set_lun_stopped(ctsio);
10459 retval = 1;
10460 goto bailout;
10461 }
10462
10463 if ((lun->flags & CTL_LUN_INOPERABLE)
10464 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
10465 /* "Medium format corrupted" */
10466 ctl_set_medium_format_corrupted(ctsio);
10467 retval = 1;
10468 goto bailout;
10469 }
10470
10471bailout:
10472 return (retval);
10473
10474}
10475
10476static void
10477ctl_failover_io(union ctl_io *io, int have_lock)
10478{
10479 ctl_set_busy(&io->scsiio);
10480 ctl_done_lock(io, have_lock);
10481}
10482
10483static void
10484ctl_failover(void)
10485{
10486 struct ctl_lun *lun;
10487 struct ctl_softc *ctl_softc;
10488 union ctl_io *next_io, *pending_io;
10489 union ctl_io *io;
10490 int lun_idx;
10491 int i;
10492
10493 ctl_softc = control_softc;
10494
10495 mtx_lock(&ctl_softc->ctl_lock);
10496 /*
10497 * Remove any cmds from the other SC from the rtr queue. These
10498 * will obviously only be for LUNs for which we're the primary.
10499 * We can't send status or get/send data for these commands.
10500 * Since they haven't been executed yet, we can just remove them.
10501 * We'll either abort them or delete them below, depending on
10502 * which HA mode we're in.
10503 */
10504 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue);
10505 io != NULL; io = next_io) {
10506 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
10507 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
10508 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr,
10509 ctl_io_hdr, links);
10510 }
10511
10512 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) {
10513 lun = ctl_softc->ctl_luns[lun_idx];
10514 if (lun==NULL)
10515 continue;
10516
10517 /*
10518 * Processor LUNs are primary on both sides.
10519 * XXX will this always be true?
10520 */
10521 if (lun->be_lun->lun_type == T_PROCESSOR)
10522 continue;
10523
10524 if ((lun->flags & CTL_LUN_PRIMARY_SC)
10525 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
10526 printf("FAILOVER: primary lun %d\n", lun_idx);
10527 /*
10528 * Remove all commands from the other SC. First from the
10529 * blocked queue then from the ooa queue. Once we have
10530 * removed them. Call ctl_check_blocked to see if there
10531 * is anything that can run.
10532 */
10533 for (io = (union ctl_io *)TAILQ_FIRST(
10534 &lun->blocked_queue); io != NULL; io = next_io) {
10535
10536 next_io = (union ctl_io *)TAILQ_NEXT(
10537 &io->io_hdr, blocked_links);
10538
10539 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
10540 TAILQ_REMOVE(&lun->blocked_queue,
10541 &io->io_hdr,blocked_links);
10542 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10543 TAILQ_REMOVE(&lun->ooa_queue,
10544 &io->io_hdr, ooa_links);
10545
10546 ctl_free_io(io);
10547 }
10548 }
10549
10550 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
10551 io != NULL; io = next_io) {
10552
10553 next_io = (union ctl_io *)TAILQ_NEXT(
10554 &io->io_hdr, ooa_links);
10555
10556 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
10557
10558 TAILQ_REMOVE(&lun->ooa_queue,
10559 &io->io_hdr,
10560 ooa_links);
10561
10562 ctl_free_io(io);
10563 }
10564 }
10565 ctl_check_blocked(lun);
10566 } else if ((lun->flags & CTL_LUN_PRIMARY_SC)
10567 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
10568
10569 printf("FAILOVER: primary lun %d\n", lun_idx);
10570 /*
10571 * Abort all commands from the other SC. We can't
10572 * send status back for them now. These should get
10573 * cleaned up when they are completed or come out
10574 * for a datamove operation.
10575 */
10576 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
10577 io != NULL; io = next_io) {
10578 next_io = (union ctl_io *)TAILQ_NEXT(
10579 &io->io_hdr, ooa_links);
10580
10581 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
10582 io->io_hdr.flags |= CTL_FLAG_ABORT;
10583 }
10584 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
10585 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
10586
10587 printf("FAILOVER: secondary lun %d\n", lun_idx);
10588
10589 lun->flags |= CTL_LUN_PRIMARY_SC;
10590
10591 /*
10592 * We send all I/O that was sent to this controller
10593 * and redirected to the other side back with
10594 * busy status, and have the initiator retry it.
10595 * Figuring out how much data has been transferred,
10596 * etc. and picking up where we left off would be
10597 * very tricky.
10598 *
10599 * XXX KDM need to remove I/O from the blocked
10600 * queue as well!
10601 */
10602 for (pending_io = (union ctl_io *)TAILQ_FIRST(
10603 &lun->ooa_queue); pending_io != NULL;
10604 pending_io = next_io) {
10605
10606 next_io = (union ctl_io *)TAILQ_NEXT(
10607 &pending_io->io_hdr, ooa_links);
10608
10609 pending_io->io_hdr.flags &=
10610 ~CTL_FLAG_SENT_2OTHER_SC;
10611
10612 if (pending_io->io_hdr.flags &
10613 CTL_FLAG_IO_ACTIVE) {
10614 pending_io->io_hdr.flags |=
10615 CTL_FLAG_FAILOVER;
10616 } else {
10617 ctl_set_busy(&pending_io->scsiio);
10618 ctl_done_lock(pending_io,
10619 /*have_lock*/1);
10620 }
10621 }
10622
10623 /*
10624 * Build Unit Attention
10625 */
10626 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
10627 lun->pending_sense[i].ua_pending |=
10628 CTL_UA_ASYM_ACC_CHANGE;
10629 }
10630 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
10631 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
10632 printf("FAILOVER: secondary lun %d\n", lun_idx);
10633 /*
10634 * if the first io on the OOA is not on the RtR queue
10635 * add it.
10636 */
10637 lun->flags |= CTL_LUN_PRIMARY_SC;
10638
10639 pending_io = (union ctl_io *)TAILQ_FIRST(
10640 &lun->ooa_queue);
10641 if (pending_io==NULL) {
10642 printf("Nothing on OOA queue\n");
10643 continue;
10644 }
10645
10646 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
10647 if ((pending_io->io_hdr.flags &
10648 CTL_FLAG_IS_WAS_ON_RTR) == 0) {
10649 pending_io->io_hdr.flags |=
10650 CTL_FLAG_IS_WAS_ON_RTR;
10651 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue,
10652 &pending_io->io_hdr, links);
10653 }
10654#if 0
10655 else
10656 {
10657 printf("Tag 0x%04x is running\n",
10658 pending_io->scsiio.tag_num);
10659 }
10660#endif
10661
10662 next_io = (union ctl_io *)TAILQ_NEXT(
10663 &pending_io->io_hdr, ooa_links);
10664 for (pending_io=next_io; pending_io != NULL;
10665 pending_io = next_io) {
10666 pending_io->io_hdr.flags &=
10667 ~CTL_FLAG_SENT_2OTHER_SC;
10668 next_io = (union ctl_io *)TAILQ_NEXT(
10669 &pending_io->io_hdr, ooa_links);
10670 if (pending_io->io_hdr.flags &
10671 CTL_FLAG_IS_WAS_ON_RTR) {
10672#if 0
10673 printf("Tag 0x%04x is running\n",
10674 pending_io->scsiio.tag_num);
10675#endif
10676 continue;
10677 }
10678
10679 switch (ctl_check_ooa(lun, pending_io,
10680 (union ctl_io *)TAILQ_PREV(
10681 &pending_io->io_hdr, ctl_ooaq,
10682 ooa_links))) {
10683
10684 case CTL_ACTION_BLOCK:
10685 TAILQ_INSERT_TAIL(&lun->blocked_queue,
10686 &pending_io->io_hdr,
10687 blocked_links);
10688 pending_io->io_hdr.flags |=
10689 CTL_FLAG_BLOCKED;
10690 break;
10691 case CTL_ACTION_PASS:
10692 case CTL_ACTION_SKIP:
10693 pending_io->io_hdr.flags |=
10694 CTL_FLAG_IS_WAS_ON_RTR;
10695 STAILQ_INSERT_TAIL(
10696 &ctl_softc->rtr_queue,
10697 &pending_io->io_hdr, links);
10698 break;
10699 case CTL_ACTION_OVERLAP:
10700 ctl_set_overlapped_cmd(
10701 (struct ctl_scsiio *)pending_io);
10702 ctl_done_lock(pending_io,
10703 /*have_lock*/ 1);
10704 break;
10705 case CTL_ACTION_OVERLAP_TAG:
10706 ctl_set_overlapped_tag(
10707 (struct ctl_scsiio *)pending_io,
10708 pending_io->scsiio.tag_num & 0xff);
10709 ctl_done_lock(pending_io,
10710 /*have_lock*/ 1);
10711 break;
10712 case CTL_ACTION_ERROR:
10713 default:
10714 ctl_set_internal_failure(
10715 (struct ctl_scsiio *)pending_io,
10716 0, // sks_valid
10717 0); //retry count
10718 ctl_done_lock(pending_io,
10719 /*have_lock*/ 1);
10720 break;
10721 }
10722 }
10723
10724 /*
10725 * Build Unit Attention
10726 */
10727 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
10728 lun->pending_sense[i].ua_pending |=
10729 CTL_UA_ASYM_ACC_CHANGE;
10730 }
10731 } else {
10732 panic("Unhandled HA mode failover, LUN flags = %#x, "
10733 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode);
10734 }
10735 }
10736 ctl_pause_rtr = 0;
10737 mtx_unlock(&ctl_softc->ctl_lock);
10738}
10739
10740static int
10741ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio)
10742{
10743 struct ctl_lun *lun;
10744 struct ctl_cmd_entry *entry;
10745 uint8_t opcode;
10746 uint32_t initidx, targ_lun;
10747 int retval;
10748
10749 retval = 0;
10750
10751 lun = NULL;
10752
10753 opcode = ctsio->cdb[0];
10754
10755 mtx_lock(&ctl_softc->ctl_lock);
10756
10757 targ_lun = ctsio->io_hdr.nexus.targ_lun;
10758 if (ctsio->io_hdr.nexus.lun_map_fn != NULL)
10759 targ_lun = ctsio->io_hdr.nexus.lun_map_fn(ctsio->io_hdr.nexus.lun_map_arg, targ_lun);
10760 if ((targ_lun < CTL_MAX_LUNS)
10761 && (ctl_softc->ctl_luns[targ_lun] != NULL)) {
10762 lun = ctl_softc->ctl_luns[targ_lun];
10763 /*
10764 * If the LUN is invalid, pretend that it doesn't exist.
10765 * It will go away as soon as all pending I/O has been
10766 * completed.
10767 */
10768 if (lun->flags & CTL_LUN_DISABLED) {
10769 lun = NULL;
10770 } else {
10771 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
10772 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
10773 lun->be_lun;
10774 if (lun->be_lun->lun_type == T_PROCESSOR) {
10775 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV;
10776 }
10777 }
10778 } else {
10779 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
10780 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
10781 }
10782
10783 entry = &ctl_cmd_table[opcode];
10784
10785 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
10786 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
10787
10788 /*
10789 * Check to see whether we can send this command to LUNs that don't
10790 * exist. This should pretty much only be the case for inquiry
10791 * and request sense. Further checks, below, really require having
10792 * a LUN, so we can't really check the command anymore. Just put
10793 * it on the rtr queue.
10794 */
10795 if (lun == NULL) {
10796 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS)
10797 goto queue_rtr;
10798
10799 ctl_set_unsupported_lun(ctsio);
10800 mtx_unlock(&ctl_softc->ctl_lock);
10801 ctl_done((union ctl_io *)ctsio);
10802 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
10803 goto bailout;
10804 } else {
10805 /*
10806 * Every I/O goes into the OOA queue for a particular LUN, and
10807 * stays there until completion.
10808 */
10809 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
10810
10811 /*
10812 * Make sure we support this particular command on this LUN.
10813 * e.g., we don't support writes to the control LUN.
10814 */
10815 switch (lun->be_lun->lun_type) {
10816 case T_PROCESSOR:
10817 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
10818 && ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS)
10819 == 0)) {
10820 ctl_set_invalid_opcode(ctsio);
10821 mtx_unlock(&ctl_softc->ctl_lock);
10822 ctl_done((union ctl_io *)ctsio);
10823 goto bailout;
10824 }
10825 break;
10826 case T_DIRECT:
10827 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0)
10828 && ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS)
10829 == 0)){
10830 ctl_set_invalid_opcode(ctsio);
10831 mtx_unlock(&ctl_softc->ctl_lock);
10832 ctl_done((union ctl_io *)ctsio);
10833 goto bailout;
10834 }
10835 break;
10836 default:
10837 printf("Unsupported CTL LUN type %d\n",
10838 lun->be_lun->lun_type);
10839 panic("Unsupported CTL LUN type %d\n",
10840 lun->be_lun->lun_type);
10841 break; /* NOTREACHED */
10842 }
10843 }
10844
10845 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
10846
10847 /*
10848 * If we've got a request sense, it'll clear the contingent
10849 * allegiance condition. Otherwise, if we have a CA condition for
10850 * this initiator, clear it, because it sent down a command other
10851 * than request sense.
10852 */
10853 if ((opcode != REQUEST_SENSE)
10854 && (ctl_is_set(lun->have_ca, initidx)))
10855 ctl_clear_mask(lun->have_ca, initidx);
10856
10857 /*
10858 * If the command has this flag set, it handles its own unit
10859 * attention reporting, we shouldn't do anything. Otherwise we
10860 * check for any pending unit attentions, and send them back to the
10861 * initiator. We only do this when a command initially comes in,
10862 * not when we pull it off the blocked queue.
10863 *
10864 * According to SAM-3, section 5.3.2, the order that things get
10865 * presented back to the host is basically unit attentions caused
10866 * by some sort of reset event, busy status, reservation conflicts
10867 * or task set full, and finally any other status.
10868 *
10869 * One issue here is that some of the unit attentions we report
10870 * don't fall into the "reset" category (e.g. "reported luns data
10871 * has changed"). So reporting it here, before the reservation
10872 * check, may be technically wrong. I guess the only thing to do
10873 * would be to check for and report the reset events here, and then
10874 * check for the other unit attention types after we check for a
10875 * reservation conflict.
10876 *
10877 * XXX KDM need to fix this
10878 */
10879 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
10880 ctl_ua_type ua_type;
10881
10882 ua_type = lun->pending_sense[initidx].ua_pending;
10883 if (ua_type != CTL_UA_NONE) {
10884 scsi_sense_data_type sense_format;
10885
10886 if (lun != NULL)
10887 sense_format = (lun->flags &
10888 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC :
10889 SSD_TYPE_FIXED;
10890 else
10891 sense_format = SSD_TYPE_FIXED;
10892
10893 ua_type = ctl_build_ua(ua_type, &ctsio->sense_data,
10894 sense_format);
10895 if (ua_type != CTL_UA_NONE) {
10896 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
10897 ctsio->io_hdr.status = CTL_SCSI_ERROR |
10898 CTL_AUTOSENSE;
10899 ctsio->sense_len = SSD_FULL_SIZE;
10900 lun->pending_sense[initidx].ua_pending &=
10901 ~ua_type;
10902 mtx_unlock(&ctl_softc->ctl_lock);
10903 ctl_done((union ctl_io *)ctsio);
10904 goto bailout;
10905 }
10906 }
10907 }
10908
10909
10910 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) {
10911 mtx_unlock(&ctl_softc->ctl_lock);
10912 ctl_done((union ctl_io *)ctsio);
10913 goto bailout;
10914 }
10915
10916 /*
10917 * XXX CHD this is where we want to send IO to other side if
10918 * this LUN is secondary on this SC. We will need to make a copy
10919 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
10920 * the copy we send as FROM_OTHER.
10921 * We also need to stuff the address of the original IO so we can
10922 * find it easily. Something similar will need be done on the other
10923 * side so when we are done we can find the copy.
10924 */
10925 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
10926 union ctl_ha_msg msg_info;
10927 int isc_retval;
10928
10929 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
10930
10931 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
10932 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
10933#if 0
10934 printf("1. ctsio %p\n", ctsio);
10935#endif
10936 msg_info.hdr.serializing_sc = NULL;
10937 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
10938 msg_info.scsi.tag_num = ctsio->tag_num;
10939 msg_info.scsi.tag_type = ctsio->tag_type;
10940 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
10941
10942 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
10943
10944 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
10945 (void *)&msg_info, sizeof(msg_info), 0)) >
10946 CTL_HA_STATUS_SUCCESS) {
10947 printf("CTL:precheck, ctl_ha_msg_send returned %d\n",
10948 isc_retval);
10949 printf("CTL:opcode is %x\n",opcode);
10950 } else {
10951#if 0
10952 printf("CTL:Precheck sent msg, opcode is %x\n",opcode);
10953#endif
10954 }
10955
10956 /*
10957 * XXX KDM this I/O is off the incoming queue, but hasn't
10958 * been inserted on any other queue. We may need to come
10959 * up with a holding queue while we wait for serialization
10960 * so that we have an idea of what we're waiting for from
10961 * the other side.
10962 */
10963 goto bailout_unlock;
10964 }
10965
10966 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
10967 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
10968 ctl_ooaq, ooa_links))) {
10969 case CTL_ACTION_BLOCK:
10970 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
10971 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
10972 blocked_links);
10973 goto bailout_unlock;
10974 break; /* NOTREACHED */
10975 case CTL_ACTION_PASS:
10976 case CTL_ACTION_SKIP:
10977 goto queue_rtr;
10978 break; /* NOTREACHED */
10979 case CTL_ACTION_OVERLAP:
10980 ctl_set_overlapped_cmd(ctsio);
10981 mtx_unlock(&ctl_softc->ctl_lock);
10982 ctl_done((union ctl_io *)ctsio);
10983 goto bailout;
10984 break; /* NOTREACHED */
10985 case CTL_ACTION_OVERLAP_TAG:
10986 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
10987 mtx_unlock(&ctl_softc->ctl_lock);
10988 ctl_done((union ctl_io *)ctsio);
10989 goto bailout;
10990 break; /* NOTREACHED */
10991 case CTL_ACTION_ERROR:
10992 default:
10993 ctl_set_internal_failure(ctsio,
10994 /*sks_valid*/ 0,
10995 /*retry_count*/ 0);
10996 mtx_unlock(&ctl_softc->ctl_lock);
10997 ctl_done((union ctl_io *)ctsio);
10998 goto bailout;
10999 break; /* NOTREACHED */
11000 }
11001
11002 goto bailout_unlock;
11003
11004queue_rtr:
11005 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11006 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue, &ctsio->io_hdr, links);
11007
11008bailout_unlock:
11009 mtx_unlock(&ctl_softc->ctl_lock);
11010
11011bailout:
11012 return (retval);
11013}
11014
11015static int
11016ctl_scsiio(struct ctl_scsiio *ctsio)
11017{
11018 int retval;
11019 struct ctl_cmd_entry *entry;
11020
11021 retval = CTL_RETVAL_COMPLETE;
11022
11023 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11024
11025 entry = &ctl_cmd_table[ctsio->cdb[0]];
11026
11027 /*
11028 * If this I/O has been aborted, just send it straight to
11029 * ctl_done() without executing it.
11030 */
11031 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11032 ctl_done((union ctl_io *)ctsio);
11033 goto bailout;
11034 }
11035
11036 /*
11037 * All the checks should have been handled by ctl_scsiio_precheck().
11038 * We should be clear now to just execute the I/O.
11039 */
11040 retval = entry->execute(ctsio);
11041
11042bailout:
11043 return (retval);
11044}
11045
11046/*
11047 * Since we only implement one target right now, a bus reset simply resets
11048 * our single target.
11049 */
11050static int
11051ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io)
11052{
11053 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET));
11054}
11055
11056static int
11057ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
11058 ctl_ua_type ua_type)
11059{
11060 struct ctl_lun *lun;
11061 int retval;
11062
11063 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11064 union ctl_ha_msg msg_info;
11065
11066 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11067 msg_info.hdr.nexus = io->io_hdr.nexus;
11068 if (ua_type==CTL_UA_TARG_RESET)
11069 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11070 else
11071 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11072 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11073 msg_info.hdr.original_sc = NULL;
11074 msg_info.hdr.serializing_sc = NULL;
11075 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11076 (void *)&msg_info, sizeof(msg_info), 0)) {
11077 }
11078 }
11079 retval = 0;
11080
11081 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links)
11082 retval += ctl_lun_reset(lun, io, ua_type);
11083
11084 return (retval);
11085}
11086
11087/*
11088 * The LUN should always be set. The I/O is optional, and is used to
11089 * distinguish between I/Os sent by this initiator, and by other
11090 * initiators. We set unit attention for initiators other than this one.
11091 * SAM-3 is vague on this point. It does say that a unit attention should
11092 * be established for other initiators when a LUN is reset (see section
11093 * 5.7.3), but it doesn't specifically say that the unit attention should
11094 * be established for this particular initiator when a LUN is reset. Here
11095 * is the relevant text, from SAM-3 rev 8:
11096 *
11097 * 5.7.2 When a SCSI initiator port aborts its own tasks
11098 *
11099 * When a SCSI initiator port causes its own task(s) to be aborted, no
11100 * notification that the task(s) have been aborted shall be returned to
11101 * the SCSI initiator port other than the completion response for the
11102 * command or task management function action that caused the task(s) to
11103 * be aborted and notification(s) associated with related effects of the
11104 * action (e.g., a reset unit attention condition).
11105 *
11106 * XXX KDM for now, we're setting unit attention for all initiators.
11107 */
11108static int
11109ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11110{
11111 union ctl_io *xio;
11112#if 0
11113 uint32_t initindex;
11114#endif
11115 int i;
11116
11117 /*
11118 * Run through the OOA queue and abort each I/O.
11119 */
11120#if 0
11121 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
11122#endif
11123 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11124 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11125 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11126 }
11127
11128 /*
11129 * This version sets unit attention for every
11130 */
11131#if 0
11132 initindex = ctl_get_initindex(&io->io_hdr.nexus);
11133 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11134 if (initindex == i)
11135 continue;
11136 lun->pending_sense[i].ua_pending |= ua_type;
11137 }
11138#endif
11139
11140 /*
11141 * A reset (any kind, really) clears reservations established with
11142 * RESERVE/RELEASE. It does not clear reservations established
11143 * with PERSISTENT RESERVE OUT, but we don't support that at the
11144 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11145 * reservations made with the RESERVE/RELEASE commands, because
11146 * those commands are obsolete in SPC-3.
11147 */
11148 lun->flags &= ~CTL_LUN_RESERVED;
11149
11150 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11151 ctl_clear_mask(lun->have_ca, i);
11152 lun->pending_sense[i].ua_pending |= ua_type;
11153 }
11154
11155 return (0);
11156}
11157
11158static int
11159ctl_abort_task(union ctl_io *io)
11160{
11161 union ctl_io *xio;
11162 struct ctl_lun *lun;
11163 struct ctl_softc *ctl_softc;
11164#if 0
11165 struct sbuf sb;
11166 char printbuf[128];
11167#endif
11168 int found;
11169 uint32_t targ_lun;
11170
11171 ctl_softc = control_softc;
11172 found = 0;
11173
11174 /*
11175 * Look up the LUN.
11176 */
11177 targ_lun = io->io_hdr.nexus.targ_lun;
11178 if (io->io_hdr.nexus.lun_map_fn != NULL)
11179 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
11180 if ((targ_lun < CTL_MAX_LUNS)
11181 && (ctl_softc->ctl_luns[targ_lun] != NULL))
11182 lun = ctl_softc->ctl_luns[targ_lun];
11183 else
11184 goto bailout;
11185
11186#if 0
11187 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
11188 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
11189#endif
11190
11191 /*
11192 * Run through the OOA queue and attempt to find the given I/O.
11193 * The target port, initiator ID, tag type and tag number have to
11194 * match the values that we got from the initiator. If we have an
11195 * untagged command to abort, simply abort the first untagged command
11196 * we come to. We only allow one untagged command at a time of course.
11197 */
11198#if 0
11199 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
11200#endif
11201 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11202 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11203#if 0
11204 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
11205
11206 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
11207 lun->lun, xio->scsiio.tag_num,
11208 xio->scsiio.tag_type,
11209 (xio->io_hdr.blocked_links.tqe_prev
11210 == NULL) ? "" : " BLOCKED",
11211 (xio->io_hdr.flags &
11212 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
11213 (xio->io_hdr.flags &
11214 CTL_FLAG_ABORT) ? " ABORT" : "",
11215 (xio->io_hdr.flags &
11216 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
11217 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
11218 sbuf_finish(&sb);
11219 printf("%s\n", sbuf_data(&sb));
11220#endif
11221
11222 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port)
11223 && (xio->io_hdr.nexus.initid.id ==
11224 io->io_hdr.nexus.initid.id)) {
11225 /*
11226 * If the abort says that the task is untagged, the
11227 * task in the queue must be untagged. Otherwise,
11228 * we just check to see whether the tag numbers
11229 * match. This is because the QLogic firmware
11230 * doesn't pass back the tag type in an abort
11231 * request.
11232 */
11233#if 0
11234 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
11235 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
11236 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
11237#endif
11238 /*
11239 * XXX KDM we've got problems with FC, because it
11240 * doesn't send down a tag type with aborts. So we
11241 * can only really go by the tag number...
11242 * This may cause problems with parallel SCSI.
11243 * Need to figure that out!!
11244 */
11245 if (xio->scsiio.tag_num == io->taskio.tag_num) {
11246 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11247 found = 1;
11248 if ((io->io_hdr.flags &
11249 CTL_FLAG_FROM_OTHER_SC) == 0 &&
11250 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11251 union ctl_ha_msg msg_info;
11252
11253 io->io_hdr.flags |=
11254 CTL_FLAG_SENT_2OTHER_SC;
11255 msg_info.hdr.nexus = io->io_hdr.nexus;
11256 msg_info.task.task_action =
11257 CTL_TASK_ABORT_TASK;
11258 msg_info.task.tag_num =
11259 io->taskio.tag_num;
11260 msg_info.task.tag_type =
11261 io->taskio.tag_type;
11262 msg_info.hdr.msg_type =
11263 CTL_MSG_MANAGE_TASKS;
11264 msg_info.hdr.original_sc = NULL;
11265 msg_info.hdr.serializing_sc = NULL;
11266#if 0
11267 printf("Sent Abort to other side\n");
11268#endif
11269 if (CTL_HA_STATUS_SUCCESS !=
11270 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11271 (void *)&msg_info,
11272 sizeof(msg_info), 0)) {
11273 }
11274 }
11275#if 0
11276 printf("ctl_abort_task: found I/O to abort\n");
11277#endif
11278 break;
11279 }
11280 }
11281 }
11282
11283bailout:
11284
11285 if (found == 0) {
11286 /*
11287 * This isn't really an error. It's entirely possible for
11288 * the abort and command completion to cross on the wire.
11289 * This is more of an informative/diagnostic error.
11290 */
11291#if 0
11292 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
11293 "%d:%d:%d:%d tag %d type %d\n",
11294 io->io_hdr.nexus.initid.id,
11295 io->io_hdr.nexus.targ_port,
11296 io->io_hdr.nexus.targ_target.id,
11297 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
11298 io->taskio.tag_type);
11299#endif
11300 return (1);
11301 } else
11302 return (0);
11303}
11304
11305/*
11306 * This routine cannot block! It must be callable from an interrupt
11307 * handler as well as from the work thread.
11308 */
11309static void
11310ctl_run_task_queue(struct ctl_softc *ctl_softc)
11311{
11312 union ctl_io *io, *next_io;
11313
11314 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
11315
11316 CTL_DEBUG_PRINT(("ctl_run_task_queue\n"));
11317
11318 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->task_queue);
11319 io != NULL; io = next_io) {
11320 int retval;
11321 const char *task_desc;
11322
11323 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
11324
11325 retval = 0;
11326
11327 switch (io->io_hdr.io_type) {
11328 case CTL_IO_TASK: {
11329 task_desc = ctl_scsi_task_string(&io->taskio);
11330 if (task_desc != NULL) {
11331#ifdef NEEDTOPORT
11332 csevent_log(CSC_CTL | CSC_SHELF_SW |
11333 CTL_TASK_REPORT,
11334 csevent_LogType_Trace,
11335 csevent_Severity_Information,
11336 csevent_AlertLevel_Green,
11337 csevent_FRU_Firmware,
11338 csevent_FRU_Unknown,
11339 "CTL: received task: %s",task_desc);
11340#endif
11341 } else {
11342#ifdef NEEDTOPORT
11343 csevent_log(CSC_CTL | CSC_SHELF_SW |
11344 CTL_TASK_REPORT,
11345 csevent_LogType_Trace,
11346 csevent_Severity_Information,
11347 csevent_AlertLevel_Green,
11348 csevent_FRU_Firmware,
11349 csevent_FRU_Unknown,
11350 "CTL: received unknown task "
11351 "type: %d (%#x)",
11352 io->taskio.task_action,
11353 io->taskio.task_action);
11354#endif
11355 }
11356 switch (io->taskio.task_action) {
11357 case CTL_TASK_ABORT_TASK:
11358 retval = ctl_abort_task(io);
11359 break;
11360 case CTL_TASK_ABORT_TASK_SET:
11361 break;
11362 case CTL_TASK_CLEAR_ACA:
11363 break;
11364 case CTL_TASK_CLEAR_TASK_SET:
11365 break;
11366 case CTL_TASK_LUN_RESET: {
11367 struct ctl_lun *lun;
11368 uint32_t targ_lun;
11369 int retval;
11370
11371 targ_lun = io->io_hdr.nexus.targ_lun;
11372 if (io->io_hdr.nexus.lun_map_fn != NULL)
11373 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
11374
11375 if ((targ_lun < CTL_MAX_LUNS)
11376 && (ctl_softc->ctl_luns[targ_lun] != NULL))
11377 lun = ctl_softc->ctl_luns[targ_lun];
11378 else {
11379 retval = 1;
11380 break;
11381 }
11382
11383 if (!(io->io_hdr.flags &
11384 CTL_FLAG_FROM_OTHER_SC)) {
11385 union ctl_ha_msg msg_info;
11386
11387 io->io_hdr.flags |=
11388 CTL_FLAG_SENT_2OTHER_SC;
11389 msg_info.hdr.msg_type =
11390 CTL_MSG_MANAGE_TASKS;
11391 msg_info.hdr.nexus = io->io_hdr.nexus;
11392 msg_info.task.task_action =
11393 CTL_TASK_LUN_RESET;
11394 msg_info.hdr.original_sc = NULL;
11395 msg_info.hdr.serializing_sc = NULL;
11396 if (CTL_HA_STATUS_SUCCESS !=
11397 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11398 (void *)&msg_info,
11399 sizeof(msg_info), 0)) {
11400 }
11401 }
11402
11403 retval = ctl_lun_reset(lun, io,
11404 CTL_UA_LUN_RESET);
11405 break;
11406 }
11407 case CTL_TASK_TARGET_RESET:
11408 retval = ctl_target_reset(ctl_softc, io,
11409 CTL_UA_TARG_RESET);
11410 break;
11411 case CTL_TASK_BUS_RESET:
11412 retval = ctl_bus_reset(ctl_softc, io);
11413 break;
11414 case CTL_TASK_PORT_LOGIN:
11415 break;
11416 case CTL_TASK_PORT_LOGOUT:
11417 break;
11418 default:
11419 printf("ctl_run_task_queue: got unknown task "
11420 "management event %d\n",
11421 io->taskio.task_action);
11422 break;
11423 }
11424 if (retval == 0)
11425 io->io_hdr.status = CTL_SUCCESS;
11426 else
11427 io->io_hdr.status = CTL_ERROR;
11428
11429 STAILQ_REMOVE(&ctl_softc->task_queue, &io->io_hdr,
11430 ctl_io_hdr, links);
11431 /*
11432 * This will queue this I/O to the done queue, but the
11433 * work thread won't be able to process it until we
11434 * return and the lock is released.
11435 */
11436 ctl_done_lock(io, /*have_lock*/ 1);
11437 break;
11438 }
11439 default: {
11440
11441 printf("%s: invalid I/O type %d msg %d cdb %x"
11442 " iptl: %ju:%d:%ju:%d tag 0x%04x\n",
11443 __func__, io->io_hdr.io_type,
11444 io->io_hdr.msg_type, io->scsiio.cdb[0],
11445 (uintmax_t)io->io_hdr.nexus.initid.id,
11446 io->io_hdr.nexus.targ_port,
11447 (uintmax_t)io->io_hdr.nexus.targ_target.id,
11448 io->io_hdr.nexus.targ_lun /* XXX */,
11449 (io->io_hdr.io_type == CTL_IO_TASK) ?
11450 io->taskio.tag_num : io->scsiio.tag_num);
11451 STAILQ_REMOVE(&ctl_softc->task_queue, &io->io_hdr,
11452 ctl_io_hdr, links);
11453 ctl_free_io(io);
11454 break;
11455 }
11456 }
11457 }
11458
11459 ctl_softc->flags &= ~CTL_FLAG_TASK_PENDING;
11460}
11461
11462/*
11463 * For HA operation. Handle commands that come in from the other
11464 * controller.
11465 */
11466static void
11467ctl_handle_isc(union ctl_io *io)
11468{
11469 int free_io;
11470 struct ctl_lun *lun;
11471 struct ctl_softc *ctl_softc;
11472 uint32_t targ_lun;
11473
11474 ctl_softc = control_softc;
11475
11476 targ_lun = io->io_hdr.nexus.targ_lun;
11477 if (io->io_hdr.nexus.lun_map_fn != NULL)
11478 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
11479 lun = ctl_softc->ctl_luns[targ_lun];
11480
11481 switch (io->io_hdr.msg_type) {
11482 case CTL_MSG_SERIALIZE:
11483 free_io = ctl_serialize_other_sc_cmd(&io->scsiio,
11484 /*have_lock*/ 0);
11485 break;
11486 case CTL_MSG_R2R: {
11487 uint8_t opcode;
11488 struct ctl_cmd_entry *entry;
11489
11490 /*
11491 * This is only used in SER_ONLY mode.
11492 */
11493 free_io = 0;
11494 opcode = io->scsiio.cdb[0];
11495 entry = &ctl_cmd_table[opcode];
11496 mtx_lock(&ctl_softc->ctl_lock);
11497 if (ctl_scsiio_lun_check(ctl_softc, lun,
11498 entry, (struct ctl_scsiio *)io) != 0) {
11499 ctl_done_lock(io, /*have_lock*/ 1);
11500 mtx_unlock(&ctl_softc->ctl_lock);
11501 break;
11502 }
11503 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11504 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue,
11505 &io->io_hdr, links);
11506 mtx_unlock(&ctl_softc->ctl_lock);
11507 break;
11508 }
11509 case CTL_MSG_FINISH_IO:
11510 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
11511 free_io = 0;
11512 ctl_done_lock(io, /*have_lock*/ 0);
11513 } else {
11514 free_io = 1;
11515 mtx_lock(&ctl_softc->ctl_lock);
11516 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
11517 ooa_links);
11518 STAILQ_REMOVE(&ctl_softc->task_queue,
11519 &io->io_hdr, ctl_io_hdr, links);
11520 ctl_check_blocked(lun);
11521 mtx_unlock(&ctl_softc->ctl_lock);
11522 }
11523 break;
11524 case CTL_MSG_PERS_ACTION:
11525 ctl_hndl_per_res_out_on_other_sc(
11526 (union ctl_ha_msg *)&io->presio.pr_msg);
11527 free_io = 1;
11528 break;
11529 case CTL_MSG_BAD_JUJU:
11530 free_io = 0;
11531 ctl_done_lock(io, /*have_lock*/ 0);
11532 break;
11533 case CTL_MSG_DATAMOVE:
11534 /* Only used in XFER mode */
11535 free_io = 0;
11536 ctl_datamove_remote(io);
11537 break;
11538 case CTL_MSG_DATAMOVE_DONE:
11539 /* Only used in XFER mode */
11540 free_io = 0;
11541 io->scsiio.be_move_done(io);
11542 break;
11543 default:
11544 free_io = 1;
11545 printf("%s: Invalid message type %d\n",
11546 __func__, io->io_hdr.msg_type);
11547 break;
11548 }
11549 if (free_io)
11550 ctl_free_io(io);
11551
11552}
11553
11554
11555/*
11556 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
11557 * there is no match.
11558 */
11559static ctl_lun_error_pattern
11560ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
11561{
11562 struct ctl_cmd_entry *entry;
11563 ctl_lun_error_pattern filtered_pattern, pattern;
11564 uint8_t opcode;
11565
11566 pattern = desc->error_pattern;
11567
11568 /*
11569 * XXX KDM we need more data passed into this function to match a
11570 * custom pattern, and we actually need to implement custom pattern
11571 * matching.
11572 */
11573 if (pattern & CTL_LUN_PAT_CMD)
11574 return (CTL_LUN_PAT_CMD);
11575
11576 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
11577 return (CTL_LUN_PAT_ANY);
11578
11579 opcode = ctsio->cdb[0];
11580 entry = &ctl_cmd_table[opcode];
11581
11582 filtered_pattern = entry->pattern & pattern;
11583
11584 /*
11585 * If the user requested specific flags in the pattern (e.g.
11586 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
11587 * flags.
11588 *
11589 * If the user did not specify any flags, it doesn't matter whether
11590 * or not the command supports the flags.
11591 */
11592 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
11593 (pattern & ~CTL_LUN_PAT_MASK))
11594 return (CTL_LUN_PAT_NONE);
11595
11596 /*
11597 * If the user asked for a range check, see if the requested LBA
11598 * range overlaps with this command's LBA range.
11599 */
11600 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
11601 uint64_t lba1;
11602 uint32_t len1;
11603 ctl_action action;
11604 int retval;
11605
11606 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
11607 if (retval != 0)
11608 return (CTL_LUN_PAT_NONE);
11609
11610 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
11611 desc->lba_range.len);
11612 /*
11613 * A "pass" means that the LBA ranges don't overlap, so
11614 * this doesn't match the user's range criteria.
11615 */
11616 if (action == CTL_ACTION_PASS)
11617 return (CTL_LUN_PAT_NONE);
11618 }
11619
11620 return (filtered_pattern);
11621}
11622
11623static void
11624ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
11625{
11626 struct ctl_error_desc *desc, *desc2;
11627
11628 mtx_assert(&control_softc->ctl_lock, MA_OWNED);
11629
11630 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
11631 ctl_lun_error_pattern pattern;
11632 /*
11633 * Check to see whether this particular command matches
11634 * the pattern in the descriptor.
11635 */
11636 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
11637 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
11638 continue;
11639
11640 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
11641 case CTL_LUN_INJ_ABORTED:
11642 ctl_set_aborted(&io->scsiio);
11643 break;
11644 case CTL_LUN_INJ_MEDIUM_ERR:
11645 ctl_set_medium_error(&io->scsiio);
11646 break;
11647 case CTL_LUN_INJ_UA:
11648 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
11649 * OCCURRED */
11650 ctl_set_ua(&io->scsiio, 0x29, 0x00);
11651 break;
11652 case CTL_LUN_INJ_CUSTOM:
11653 /*
11654 * We're assuming the user knows what he is doing.
11655 * Just copy the sense information without doing
11656 * checks.
11657 */
11658 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
11659 ctl_min(sizeof(desc->custom_sense),
11660 sizeof(io->scsiio.sense_data)));
11661 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
11662 io->scsiio.sense_len = SSD_FULL_SIZE;
11663 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11664 break;
11665 case CTL_LUN_INJ_NONE:
11666 default:
11667 /*
11668 * If this is an error injection type we don't know
11669 * about, clear the continuous flag (if it is set)
11670 * so it will get deleted below.
11671 */
11672 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
11673 break;
11674 }
11675 /*
11676 * By default, each error injection action is a one-shot
11677 */
11678 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
11679 continue;
11680
11681 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
11682
11683 free(desc, M_CTL);
11684 }
11685}
11686
11687#ifdef CTL_IO_DELAY
11688static void
11689ctl_datamove_timer_wakeup(void *arg)
11690{
11691 union ctl_io *io;
11692
11693 io = (union ctl_io *)arg;
11694
11695 ctl_datamove(io);
11696}
11697#endif /* CTL_IO_DELAY */
11698
11699void
11700ctl_datamove(union ctl_io *io)
11701{
11702 void (*fe_datamove)(union ctl_io *io);
11703
11704 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
11705
11706 CTL_DEBUG_PRINT(("ctl_datamove\n"));
11707
11708#ifdef CTL_TIME_IO
11709 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
11710 char str[256];
11711 char path_str[64];
11712 struct sbuf sb;
11713
11714 ctl_scsi_path_string(io, path_str, sizeof(path_str));
11715 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
11716
11717 sbuf_cat(&sb, path_str);
11718 switch (io->io_hdr.io_type) {
11719 case CTL_IO_SCSI:
11720 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
11721 sbuf_printf(&sb, "\n");
11722 sbuf_cat(&sb, path_str);
11723 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
11724 io->scsiio.tag_num, io->scsiio.tag_type);
11725 break;
11726 case CTL_IO_TASK:
11727 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
11728 "Tag Type: %d\n", io->taskio.task_action,
11729 io->taskio.tag_num, io->taskio.tag_type);
11730 break;
11731 default:
11732 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
11733 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
11734 break;
11735 }
11736 sbuf_cat(&sb, path_str);
11737 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
11738 (intmax_t)time_uptime - io->io_hdr.start_time);
11739 sbuf_finish(&sb);
11740 printf("%s", sbuf_data(&sb));
11741 }
11742#endif /* CTL_TIME_IO */
11743
11744 mtx_lock(&control_softc->ctl_lock);
11745#ifdef CTL_IO_DELAY
11746 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
11747 struct ctl_lun *lun;
11748
11749 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
11750
11751 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
11752 } else {
11753 struct ctl_lun *lun;
11754
11755 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
11756 if ((lun != NULL)
11757 && (lun->delay_info.datamove_delay > 0)) {
11758 struct callout *callout;
11759
11760 callout = (struct callout *)&io->io_hdr.timer_bytes;
11761 callout_init(callout, /*mpsafe*/ 1);
11762 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
11763 callout_reset(callout,
11764 lun->delay_info.datamove_delay * hz,
11765 ctl_datamove_timer_wakeup, io);
11766 if (lun->delay_info.datamove_type ==
11767 CTL_DELAY_TYPE_ONESHOT)
11768 lun->delay_info.datamove_delay = 0;
11769 mtx_unlock(&control_softc->ctl_lock);
11770 return;
11771 }
11772 }
11773#endif
11774 /*
11775 * If we have any pending task management commands, process them
11776 * first. This is necessary to eliminate a race condition with the
11777 * FETD:
11778 *
11779 * - FETD submits a task management command, like an abort.
11780 * - Back end calls fe_datamove() to move the data for the aborted
11781 * command. The FETD can't really accept it, but if it did, it
11782 * would end up transmitting data for a command that the initiator
11783 * told us to abort.
11784 *
11785 * We close the race by processing all pending task management
11786 * commands here (we can't block!), and then check this I/O to see
11787 * if it has been aborted. If so, return it to the back end with
11788 * bad status, so the back end can say return an error to the back end
11789 * and then when the back end returns an error, we can return the
11790 * aborted command to the FETD, so it can clean up its resources.
11791 */
11792 if (control_softc->flags & CTL_FLAG_TASK_PENDING)
11793 ctl_run_task_queue(control_softc);
11794
11795 /*
11796 * This command has been aborted. Set the port status, so we fail
11797 * the data move.
11798 */
11799 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
11800 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n",
11801 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id,
11802 io->io_hdr.nexus.targ_port,
11803 (uintmax_t)io->io_hdr.nexus.targ_target.id,
11804 io->io_hdr.nexus.targ_lun);
11805 io->io_hdr.status = CTL_CMD_ABORTED;
11806 io->io_hdr.port_status = 31337;
11807 mtx_unlock(&control_softc->ctl_lock);
11808 /*
11809 * Note that the backend, in this case, will get the
11810 * callback in its context. In other cases it may get
11811 * called in the frontend's interrupt thread context.
11812 */
11813 io->scsiio.be_move_done(io);
11814 return;
11815 }
11816
11817 /*
11818 * If we're in XFER mode and this I/O is from the other shelf
11819 * controller, we need to send the DMA to the other side to
11820 * actually transfer the data to/from the host. In serialize only
11821 * mode the transfer happens below CTL and ctl_datamove() is only
11822 * called on the machine that originally received the I/O.
11823 */
11824 if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
11825 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11826 union ctl_ha_msg msg;
11827 uint32_t sg_entries_sent;
11828 int do_sg_copy;
11829 int i;
11830
11831 memset(&msg, 0, sizeof(msg));
11832 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
11833 msg.hdr.original_sc = io->io_hdr.original_sc;
11834 msg.hdr.serializing_sc = io;
11835 msg.hdr.nexus = io->io_hdr.nexus;
11836 msg.dt.flags = io->io_hdr.flags;
11837 /*
11838 * We convert everything into a S/G list here. We can't
11839 * pass by reference, only by value between controllers.
11840 * So we can't pass a pointer to the S/G list, only as many
11841 * S/G entries as we can fit in here. If it's possible for
11842 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
11843 * then we need to break this up into multiple transfers.
11844 */
11845 if (io->scsiio.kern_sg_entries == 0) {
11846 msg.dt.kern_sg_entries = 1;
11847 /*
11848 * If this is in cached memory, flush the cache
11849 * before we send the DMA request to the other
11850 * controller. We want to do this in either the
11851 * read or the write case. The read case is
11852 * straightforward. In the write case, we want to
11853 * make sure nothing is in the local cache that
11854 * could overwrite the DMAed data.
11855 */
11856 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
11857 /*
11858 * XXX KDM use bus_dmamap_sync() here.
11859 */
11860 }
11861
11862 /*
11863 * Convert to a physical address if this is a
11864 * virtual address.
11865 */
11866 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
11867 msg.dt.sg_list[0].addr =
11868 io->scsiio.kern_data_ptr;
11869 } else {
11870 /*
11871 * XXX KDM use busdma here!
11872 */
11873#if 0
11874 msg.dt.sg_list[0].addr = (void *)
11875 vtophys(io->scsiio.kern_data_ptr);
11876#endif
11877 }
11878
11879 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
11880 do_sg_copy = 0;
11881 } else {
11882 struct ctl_sg_entry *sgl;
11883
11884 do_sg_copy = 1;
11885 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
11886 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
11887 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
11888 /*
11889 * XXX KDM use bus_dmamap_sync() here.
11890 */
11891 }
11892 }
11893
11894 msg.dt.kern_data_len = io->scsiio.kern_data_len;
11895 msg.dt.kern_total_len = io->scsiio.kern_total_len;
11896 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
11897 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
11898 msg.dt.sg_sequence = 0;
11899
11900 /*
11901 * Loop until we've sent all of the S/G entries. On the
11902 * other end, we'll recompose these S/G entries into one
11903 * contiguous list before passing it to the
11904 */
11905 for (sg_entries_sent = 0; sg_entries_sent <
11906 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
11907 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/
11908 sizeof(msg.dt.sg_list[0])),
11909 msg.dt.kern_sg_entries - sg_entries_sent);
11910
11911 if (do_sg_copy != 0) {
11912 struct ctl_sg_entry *sgl;
11913 int j;
11914
11915 sgl = (struct ctl_sg_entry *)
11916 io->scsiio.kern_data_ptr;
11917 /*
11918 * If this is in cached memory, flush the cache
11919 * before we send the DMA request to the other
11920 * controller. We want to do this in either
11921 * the * read or the write case. The read
11922 * case is straightforward. In the write
11923 * case, we want to make sure nothing is
11924 * in the local cache that could overwrite
11925 * the DMAed data.
11926 */
11927
11928 for (i = sg_entries_sent, j = 0;
11929 i < msg.dt.cur_sg_entries; i++, j++) {
11930 if ((io->io_hdr.flags &
11931 CTL_FLAG_NO_DATASYNC) == 0) {
11932 /*
11933 * XXX KDM use bus_dmamap_sync()
11934 */
11935 }
11936 if ((io->io_hdr.flags &
11937 CTL_FLAG_BUS_ADDR) == 0) {
11938 /*
11939 * XXX KDM use busdma.
11940 */
11941#if 0
11942 msg.dt.sg_list[j].addr =(void *)
11943 vtophys(sgl[i].addr);
11944#endif
11945 } else {
11946 msg.dt.sg_list[j].addr =
11947 sgl[i].addr;
11948 }
11949 msg.dt.sg_list[j].len = sgl[i].len;
11950 }
11951 }
11952
11953 sg_entries_sent += msg.dt.cur_sg_entries;
11954 if (sg_entries_sent >= msg.dt.kern_sg_entries)
11955 msg.dt.sg_last = 1;
11956 else
11957 msg.dt.sg_last = 0;
11958
11959 /*
11960 * XXX KDM drop and reacquire the lock here?
11961 */
11962 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
11963 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
11964 /*
11965 * XXX do something here.
11966 */
11967 }
11968
11969 msg.dt.sent_sg_entries = sg_entries_sent;
11970 }
11971 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11972 if (io->io_hdr.flags & CTL_FLAG_FAILOVER)
11973 ctl_failover_io(io, /*have_lock*/ 1);
11974
11975 } else {
11976
11977 /*
11978 * Lookup the fe_datamove() function for this particular
11979 * front end.
11980 */
11981 fe_datamove =
11982 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
11983 mtx_unlock(&control_softc->ctl_lock);
11984
11985 fe_datamove(io);
11986 }
11987}
11988
11989static void
11990ctl_send_datamove_done(union ctl_io *io, int have_lock)
11991{
11992 union ctl_ha_msg msg;
11993 int isc_status;
11994
11995 memset(&msg, 0, sizeof(msg));
11996
11997 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
11998 msg.hdr.original_sc = io;
11999 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12000 msg.hdr.nexus = io->io_hdr.nexus;
12001 msg.hdr.status = io->io_hdr.status;
12002 msg.scsi.tag_num = io->scsiio.tag_num;
12003 msg.scsi.tag_type = io->scsiio.tag_type;
12004 msg.scsi.scsi_status = io->scsiio.scsi_status;
12005 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12006 sizeof(io->scsiio.sense_data));
12007 msg.scsi.sense_len = io->scsiio.sense_len;
12008 msg.scsi.sense_residual = io->scsiio.sense_residual;
12009 msg.scsi.fetd_status = io->io_hdr.port_status;
12010 msg.scsi.residual = io->scsiio.residual;
12011 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12012
12013 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12014 ctl_failover_io(io, /*have_lock*/ have_lock);
12015 return;
12016 }
12017
12018 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0);
12019 if (isc_status > CTL_HA_STATUS_SUCCESS) {
12020 /* XXX do something if this fails */
12021 }
12022
12023}
12024
12025/*
12026 * The DMA to the remote side is done, now we need to tell the other side
12027 * we're done so it can continue with its data movement.
12028 */
12029static void
12030ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12031{
12032 union ctl_io *io;
12033
12034 io = rq->context;
12035
12036 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12037 printf("%s: ISC DMA write failed with error %d", __func__,
12038 rq->ret);
12039 ctl_set_internal_failure(&io->scsiio,
12040 /*sks_valid*/ 1,
12041 /*retry_count*/ rq->ret);
12042 }
12043
12044 ctl_dt_req_free(rq);
12045
12046 /*
12047 * In this case, we had to malloc the memory locally. Free it.
12048 */
12049 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12050 int i;
12051 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12052 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12053 }
12054 /*
12055 * The data is in local and remote memory, so now we need to send
12056 * status (good or back) back to the other side.
12057 */
12058 ctl_send_datamove_done(io, /*have_lock*/ 0);
12059}
12060
12061/*
12062 * We've moved the data from the host/controller into local memory. Now we
12063 * need to push it over to the remote controller's memory.
12064 */
12065static int
12066ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12067{
12068 int retval;
12069
12070 retval = 0;
12071
12072 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12073 ctl_datamove_remote_write_cb);
12074
12075 return (retval);
12076}
12077
12078static void
12079ctl_datamove_remote_write(union ctl_io *io)
12080{
12081 int retval;
12082 void (*fe_datamove)(union ctl_io *io);
12083
12084 /*
12085 * - Get the data from the host/HBA into local memory.
12086 * - DMA memory from the local controller to the remote controller.
12087 * - Send status back to the remote controller.
12088 */
12089
12090 retval = ctl_datamove_remote_sgl_setup(io);
12091 if (retval != 0)
12092 return;
12093
12094 /* Switch the pointer over so the FETD knows what to do */
12095 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12096
12097 /*
12098 * Use a custom move done callback, since we need to send completion
12099 * back to the other controller, not to the backend on this side.
12100 */
12101 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12102
12103 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12104
12105 fe_datamove(io);
12106
12107 return;
12108
12109}
12110
12111static int
12112ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12113{
12114#if 0
12115 char str[256];
12116 char path_str[64];
12117 struct sbuf sb;
12118#endif
12119
12120 /*
12121 * In this case, we had to malloc the memory locally. Free it.
12122 */
12123 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12124 int i;
12125 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12126 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12127 }
12128
12129#if 0
12130 scsi_path_string(io, path_str, sizeof(path_str));
12131 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12132 sbuf_cat(&sb, path_str);
12133 scsi_command_string(&io->scsiio, NULL, &sb);
12134 sbuf_printf(&sb, "\n");
12135 sbuf_cat(&sb, path_str);
12136 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12137 io->scsiio.tag_num, io->scsiio.tag_type);
12138 sbuf_cat(&sb, path_str);
12139 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12140 io->io_hdr.flags, io->io_hdr.status);
12141 sbuf_finish(&sb);
12142 printk("%s", sbuf_data(&sb));
12143#endif
12144
12145
12146 /*
12147 * The read is done, now we need to send status (good or bad) back
12148 * to the other side.
12149 */
12150 ctl_send_datamove_done(io, /*have_lock*/ 0);
12151
12152 return (0);
12153}
12154
12155static void
12156ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12157{
12158 union ctl_io *io;
12159 void (*fe_datamove)(union ctl_io *io);
12160
12161 io = rq->context;
12162
12163 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12164 printf("%s: ISC DMA read failed with error %d", __func__,
12165 rq->ret);
12166 ctl_set_internal_failure(&io->scsiio,
12167 /*sks_valid*/ 1,
12168 /*retry_count*/ rq->ret);
12169 }
12170
12171 ctl_dt_req_free(rq);
12172
12173 /* Switch the pointer over so the FETD knows what to do */
12174 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12175
12176 /*
12177 * Use a custom move done callback, since we need to send completion
12178 * back to the other controller, not to the backend on this side.
12179 */
12180 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12181
12182 /* XXX KDM add checks like the ones in ctl_datamove? */
12183
12184 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12185
12186 fe_datamove(io);
12187}
12188
12189static int
12190ctl_datamove_remote_sgl_setup(union ctl_io *io)
12191{
12192 struct ctl_sg_entry *local_sglist, *remote_sglist;
12193 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist;
12194 struct ctl_softc *softc;
12195 int retval;
12196 int i;
12197
12198 retval = 0;
12199 softc = control_softc;
12200
12201 local_sglist = io->io_hdr.local_sglist;
12202 local_dma_sglist = io->io_hdr.local_dma_sglist;
12203 remote_sglist = io->io_hdr.remote_sglist;
12204 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
12205
12206 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) {
12207 for (i = 0; i < io->scsiio.kern_sg_entries; i++) {
12208 local_sglist[i].len = remote_sglist[i].len;
12209
12210 /*
12211 * XXX Detect the situation where the RS-level I/O
12212 * redirector on the other side has already read the
12213 * data off of the AOR RS on this side, and
12214 * transferred it to remote (mirror) memory on the
12215 * other side. Since we already have the data in
12216 * memory here, we just need to use it.
12217 *
12218 * XXX KDM this can probably be removed once we
12219 * get the cache device code in and take the
12220 * current AOR implementation out.
12221 */
12222#ifdef NEEDTOPORT
12223 if ((remote_sglist[i].addr >=
12224 (void *)vtophys(softc->mirr->addr))
12225 && (remote_sglist[i].addr <
12226 ((void *)vtophys(softc->mirr->addr) +
12227 CacheMirrorOffset))) {
12228 local_sglist[i].addr = remote_sglist[i].addr -
12229 CacheMirrorOffset;
12230 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12231 CTL_FLAG_DATA_IN)
12232 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE;
12233 } else {
12234 local_sglist[i].addr = remote_sglist[i].addr +
12235 CacheMirrorOffset;
12236 }
12237#endif
12238#if 0
12239 printf("%s: local %p, remote %p, len %d\n",
12240 __func__, local_sglist[i].addr,
12241 remote_sglist[i].addr, local_sglist[i].len);
12242#endif
12243 }
12244 } else {
12245 uint32_t len_to_go;
12246
12247 /*
12248 * In this case, we don't have automatically allocated
12249 * memory for this I/O on this controller. This typically
12250 * happens with internal CTL I/O -- e.g. inquiry, mode
12251 * sense, etc. Anything coming from RAIDCore will have
12252 * a mirror area available.
12253 */
12254 len_to_go = io->scsiio.kern_data_len;
12255
12256 /*
12257 * Clear the no datasync flag, we have to use malloced
12258 * buffers.
12259 */
12260 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC;
12261
12262 /*
12263 * The difficult thing here is that the size of the various
12264 * S/G segments may be different than the size from the
12265 * remote controller. That'll make it harder when DMAing
12266 * the data back to the other side.
12267 */
12268 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) /
12269 sizeof(io->io_hdr.remote_sglist[0])) &&
12270 (len_to_go > 0); i++) {
12271 local_sglist[i].len = ctl_min(len_to_go, 131072);
12272 CTL_SIZE_8B(local_dma_sglist[i].len,
12273 local_sglist[i].len);
12274 local_sglist[i].addr =
12275 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK);
12276
12277 local_dma_sglist[i].addr = local_sglist[i].addr;
12278
12279 if (local_sglist[i].addr == NULL) {
12280 int j;
12281
12282 printf("malloc failed for %zd bytes!",
12283 local_dma_sglist[i].len);
12284 for (j = 0; j < i; j++) {
12285 free(local_sglist[j].addr, M_CTL);
12286 }
12287 ctl_set_internal_failure(&io->scsiio,
12288 /*sks_valid*/ 1,
12289 /*retry_count*/ 4857);
12290 retval = 1;
12291 goto bailout_error;
12292
12293 }
12294 /* XXX KDM do we need a sync here? */
12295
12296 len_to_go -= local_sglist[i].len;
12297 }
12298 /*
12299 * Reset the number of S/G entries accordingly. The
12300 * original number of S/G entries is available in
12301 * rem_sg_entries.
12302 */
12303 io->scsiio.kern_sg_entries = i;
12304
12305#if 0
12306 printf("%s: kern_sg_entries = %d\n", __func__,
12307 io->scsiio.kern_sg_entries);
12308 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12309 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i,
12310 local_sglist[i].addr, local_sglist[i].len,
12311 local_dma_sglist[i].len);
12312#endif
12313 }
12314
12315
12316 return (retval);
12317
12318bailout_error:
12319
12320 ctl_send_datamove_done(io, /*have_lock*/ 0);
12321
12322 return (retval);
12323}
12324
12325static int
12326ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12327 ctl_ha_dt_cb callback)
12328{
12329 struct ctl_ha_dt_req *rq;
12330 struct ctl_sg_entry *remote_sglist, *local_sglist;
12331 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist;
12332 uint32_t local_used, remote_used, total_used;
12333 int retval;
12334 int i, j;
12335
12336 retval = 0;
12337
12338 rq = ctl_dt_req_alloc();
12339
12340 /*
12341 * If we failed to allocate the request, and if the DMA didn't fail
12342 * anyway, set busy status. This is just a resource allocation
12343 * failure.
12344 */
12345 if ((rq == NULL)
12346 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
12347 ctl_set_busy(&io->scsiio);
12348
12349 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
12350
12351 if (rq != NULL)
12352 ctl_dt_req_free(rq);
12353
12354 /*
12355 * The data move failed. We need to return status back
12356 * to the other controller. No point in trying to DMA
12357 * data to the remote controller.
12358 */
12359
12360 ctl_send_datamove_done(io, /*have_lock*/ 0);
12361
12362 retval = 1;
12363
12364 goto bailout;
12365 }
12366
12367 local_sglist = io->io_hdr.local_sglist;
12368 local_dma_sglist = io->io_hdr.local_dma_sglist;
12369 remote_sglist = io->io_hdr.remote_sglist;
12370 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
12371 local_used = 0;
12372 remote_used = 0;
12373 total_used = 0;
12374
12375 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) {
12376 rq->ret = CTL_HA_STATUS_SUCCESS;
12377 rq->context = io;
12378 callback(rq);
12379 goto bailout;
12380 }
12381
12382 /*
12383 * Pull/push the data over the wire from/to the other controller.
12384 * This takes into account the possibility that the local and
12385 * remote sglists may not be identical in terms of the size of
12386 * the elements and the number of elements.
12387 *
12388 * One fundamental assumption here is that the length allocated for
12389 * both the local and remote sglists is identical. Otherwise, we've
12390 * essentially got a coding error of some sort.
12391 */
12392 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12393 int isc_ret;
12394 uint32_t cur_len, dma_length;
12395 uint8_t *tmp_ptr;
12396
12397 rq->id = CTL_HA_DATA_CTL;
12398 rq->command = command;
12399 rq->context = io;
12400
12401 /*
12402 * Both pointers should be aligned. But it is possible
12403 * that the allocation length is not. They should both
12404 * also have enough slack left over at the end, though,
12405 * to round up to the next 8 byte boundary.
12406 */
12407 cur_len = ctl_min(local_sglist[i].len - local_used,
12408 remote_sglist[j].len - remote_used);
12409
12410 /*
12411 * In this case, we have a size issue and need to decrease
12412 * the size, except in the case where we actually have less
12413 * than 8 bytes left. In that case, we need to increase
12414 * the DMA length to get the last bit.
12415 */
12416 if ((cur_len & 0x7) != 0) {
12417 if (cur_len > 0x7) {
12418 cur_len = cur_len - (cur_len & 0x7);
12419 dma_length = cur_len;
12420 } else {
12421 CTL_SIZE_8B(dma_length, cur_len);
12422 }
12423
12424 } else
12425 dma_length = cur_len;
12426
12427 /*
12428 * If we had to allocate memory for this I/O, instead of using
12429 * the non-cached mirror memory, we'll need to flush the cache
12430 * before trying to DMA to the other controller.
12431 *
12432 * We could end up doing this multiple times for the same
12433 * segment if we have a larger local segment than remote
12434 * segment. That shouldn't be an issue.
12435 */
12436 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12437 /*
12438 * XXX KDM use bus_dmamap_sync() here.
12439 */
12440 }
12441
12442 rq->size = dma_length;
12443
12444 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12445 tmp_ptr += local_used;
12446
12447 /* Use physical addresses when talking to ISC hardware */
12448 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12449 /* XXX KDM use busdma */
12450#if 0
12451 rq->local = vtophys(tmp_ptr);
12452#endif
12453 } else
12454 rq->local = tmp_ptr;
12455
12456 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12457 tmp_ptr += remote_used;
12458 rq->remote = tmp_ptr;
12459
12460 rq->callback = NULL;
12461
12462 local_used += cur_len;
12463 if (local_used >= local_sglist[i].len) {
12464 i++;
12465 local_used = 0;
12466 }
12467
12468 remote_used += cur_len;
12469 if (remote_used >= remote_sglist[j].len) {
12470 j++;
12471 remote_used = 0;
12472 }
12473 total_used += cur_len;
12474
12475 if (total_used >= io->scsiio.kern_data_len)
12476 rq->callback = callback;
12477
12478 if ((rq->size & 0x7) != 0) {
12479 printf("%s: warning: size %d is not on 8b boundary\n",
12480 __func__, rq->size);
12481 }
12482 if (((uintptr_t)rq->local & 0x7) != 0) {
12483 printf("%s: warning: local %p not on 8b boundary\n",
12484 __func__, rq->local);
12485 }
12486 if (((uintptr_t)rq->remote & 0x7) != 0) {
12487 printf("%s: warning: remote %p not on 8b boundary\n",
12488 __func__, rq->local);
12489 }
12490#if 0
12491 printf("%s: %s: local %#x remote %#x size %d\n", __func__,
12492 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
12493 rq->local, rq->remote, rq->size);
12494#endif
12495
12496 isc_ret = ctl_dt_single(rq);
12497 if (isc_ret == CTL_HA_STATUS_WAIT)
12498 continue;
12499
12500 if (isc_ret == CTL_HA_STATUS_DISCONNECT) {
12501 rq->ret = CTL_HA_STATUS_SUCCESS;
12502 } else {
12503 rq->ret = isc_ret;
12504 }
12505 callback(rq);
12506 goto bailout;
12507 }
12508
12509bailout:
12510 return (retval);
12511
12512}
12513
12514static void
12515ctl_datamove_remote_read(union ctl_io *io)
12516{
12517 int retval;
12518 int i;
12519
12520 /*
12521 * This will send an error to the other controller in the case of a
12522 * failure.
12523 */
12524 retval = ctl_datamove_remote_sgl_setup(io);
12525 if (retval != 0)
12526 return;
12527
12528 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12529 ctl_datamove_remote_read_cb);
12530 if ((retval != 0)
12531 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) {
12532 /*
12533 * Make sure we free memory if there was an error.. The
12534 * ctl_datamove_remote_xfer() function will send the
12535 * datamove done message, or call the callback with an
12536 * error if there is a problem.
12537 */
12538 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12539 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12540 }
12541
12542 return;
12543}
12544
12545/*
12546 * Process a datamove request from the other controller. This is used for
12547 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12548 * first. Once that is complete, the data gets DMAed into the remote
12549 * controller's memory. For reads, we DMA from the remote controller's
12550 * memory into our memory first, and then move it out to the FETD.
12551 */
12552static void
12553ctl_datamove_remote(union ctl_io *io)
12554{
12555 struct ctl_softc *softc;
12556
12557 softc = control_softc;
12558
12559 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
12560
12561 /*
12562 * Note that we look for an aborted I/O here, but don't do some of
12563 * the other checks that ctl_datamove() normally does. We don't
12564 * need to run the task queue, because this I/O is on the ISC
12565 * queue, which is executed by the work thread after the task queue.
12566 * We don't need to run the datamove delay code, since that should
12567 * have been done if need be on the other controller.
12568 */
12569 mtx_lock(&softc->ctl_lock);
12570
12571 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12572
12573 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__,
12574 io->scsiio.tag_num, io->io_hdr.nexus.initid.id,
12575 io->io_hdr.nexus.targ_port,
12576 io->io_hdr.nexus.targ_target.id,
12577 io->io_hdr.nexus.targ_lun);
12578 io->io_hdr.status = CTL_CMD_ABORTED;
12579 io->io_hdr.port_status = 31338;
12580
12581 mtx_unlock(&softc->ctl_lock);
12582
12583 ctl_send_datamove_done(io, /*have_lock*/ 0);
12584
12585 return;
12586 }
12587
12588 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) {
12589 mtx_unlock(&softc->ctl_lock);
12590 ctl_datamove_remote_write(io);
12591 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){
12592 mtx_unlock(&softc->ctl_lock);
12593 ctl_datamove_remote_read(io);
12594 } else {
12595 union ctl_ha_msg msg;
12596 struct scsi_sense_data *sense;
12597 uint8_t sks[3];
12598 int retry_count;
12599
12600 memset(&msg, 0, sizeof(msg));
12601
12602 msg.hdr.msg_type = CTL_MSG_BAD_JUJU;
12603 msg.hdr.status = CTL_SCSI_ERROR;
12604 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
12605
12606 retry_count = 4243;
12607
12608 sense = &msg.scsi.sense_data;
12609 sks[0] = SSD_SCS_VALID;
12610 sks[1] = (retry_count >> 8) & 0xff;
12611 sks[2] = retry_count & 0xff;
12612
12613 /* "Internal target failure" */
12614 scsi_set_sense_data(sense,
12615 /*sense_format*/ SSD_TYPE_NONE,
12616 /*current_error*/ 1,
12617 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
12618 /*asc*/ 0x44,
12619 /*ascq*/ 0x00,
12620 /*type*/ SSD_ELEM_SKS,
12621 /*size*/ sizeof(sks),
12622 /*data*/ sks,
12623 SSD_ELEM_NONE);
12624
12625 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12626 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12627 ctl_failover_io(io, /*have_lock*/ 1);
12628 mtx_unlock(&softc->ctl_lock);
12629 return;
12630 }
12631
12632 mtx_unlock(&softc->ctl_lock);
12633
12634 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) >
12635 CTL_HA_STATUS_SUCCESS) {
12636 /* XXX KDM what to do if this fails? */
12637 }
12638 return;
12639 }
12640
12641}
12642
12643static int
12644ctl_process_done(union ctl_io *io, int have_lock)
12645{
12646 struct ctl_lun *lun;
12647 struct ctl_softc *ctl_softc;
12648 void (*fe_done)(union ctl_io *io);
12649 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port);
12650
12651 CTL_DEBUG_PRINT(("ctl_process_done\n"));
12652
12653 fe_done =
12654 control_softc->ctl_ports[targ_port]->fe_done;
12655
12656#ifdef CTL_TIME_IO
12657 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12658 char str[256];
12659 char path_str[64];
12660 struct sbuf sb;
12661
12662 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12663 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12664
12665 sbuf_cat(&sb, path_str);
12666 switch (io->io_hdr.io_type) {
12667 case CTL_IO_SCSI:
12668 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12669 sbuf_printf(&sb, "\n");
12670 sbuf_cat(&sb, path_str);
12671 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12672 io->scsiio.tag_num, io->scsiio.tag_type);
12673 break;
12674 case CTL_IO_TASK:
12675 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12676 "Tag Type: %d\n", io->taskio.task_action,
12677 io->taskio.tag_num, io->taskio.tag_type);
12678 break;
12679 default:
12680 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12681 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12682 break;
12683 }
12684 sbuf_cat(&sb, path_str);
12685 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12686 (intmax_t)time_uptime - io->io_hdr.start_time);
12687 sbuf_finish(&sb);
12688 printf("%s", sbuf_data(&sb));
12689 }
12690#endif /* CTL_TIME_IO */
12691
12692 switch (io->io_hdr.io_type) {
12693 case CTL_IO_SCSI:
12694 break;
12695 case CTL_IO_TASK:
12696 if (bootverbose || verbose > 0)
12697 ctl_io_error_print(io, NULL);
12698 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
12699 ctl_free_io(io);
12700 else
12701 fe_done(io);
12702 return (CTL_RETVAL_COMPLETE);
12703 break;
12704 default:
12705 printf("ctl_process_done: invalid io type %d\n",
12706 io->io_hdr.io_type);
12707 panic("ctl_process_done: invalid io type %d\n",
12708 io->io_hdr.io_type);
12709 break; /* NOTREACHED */
12710 }
12711
12712 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12713 if (lun == NULL) {
12714 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
12715 io->io_hdr.nexus.targ_lun));
12716 fe_done(io);
12717 goto bailout;
12718 }
12719 ctl_softc = lun->ctl_softc;
12720
12721 /*
12722 * Remove this from the OOA queue.
12723 */
12724 if (have_lock == 0)
12725 mtx_lock(&ctl_softc->ctl_lock);
12726
12727 /*
12728 * Check to see if we have any errors to inject here. We only
12729 * inject errors for commands that don't already have errors set.
12730 */
12731 if ((STAILQ_FIRST(&lun->error_list) != NULL)
12732 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))
12733 ctl_inject_error(lun, io);
12734
12735 /*
12736 * XXX KDM how do we treat commands that aren't completed
12737 * successfully?
12738 *
12739 * XXX KDM should we also track I/O latency?
12740 */
12741 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
12742 uint32_t blocksize;
12743#ifdef CTL_TIME_IO
12744 struct bintime cur_bt;
12745#endif
12746
12747 if ((lun->be_lun != NULL)
12748 && (lun->be_lun->blocksize != 0))
12749 blocksize = lun->be_lun->blocksize;
12750 else
12751 blocksize = 512;
12752
12753 switch (io->io_hdr.io_type) {
12754 case CTL_IO_SCSI: {
12755 int isread;
10312 default:
10313 return (1);
10314 break; /* NOTREACHED */
10315 }
10316
10317 return (0);
10318}
10319
10320static ctl_action
10321ctl_extent_check_lba(uint64_t lba1, uint32_t len1, uint64_t lba2, uint32_t len2)
10322{
10323 uint64_t endlba1, endlba2;
10324
10325 endlba1 = lba1 + len1 - 1;
10326 endlba2 = lba2 + len2 - 1;
10327
10328 if ((endlba1 < lba2)
10329 || (endlba2 < lba1))
10330 return (CTL_ACTION_PASS);
10331 else
10332 return (CTL_ACTION_BLOCK);
10333}
10334
10335static ctl_action
10336ctl_extent_check(union ctl_io *io1, union ctl_io *io2)
10337{
10338 uint64_t lba1, lba2;
10339 uint32_t len1, len2;
10340 int retval;
10341
10342 retval = ctl_get_lba_len(io1, &lba1, &len1);
10343 if (retval != 0)
10344 return (CTL_ACTION_ERROR);
10345
10346 retval = ctl_get_lba_len(io2, &lba2, &len2);
10347 if (retval != 0)
10348 return (CTL_ACTION_ERROR);
10349
10350 return (ctl_extent_check_lba(lba1, len1, lba2, len2));
10351}
10352
10353static ctl_action
10354ctl_check_for_blockage(union ctl_io *pending_io, union ctl_io *ooa_io)
10355{
10356 struct ctl_cmd_entry *pending_entry, *ooa_entry;
10357 ctl_serialize_action *serialize_row;
10358
10359 /*
10360 * The initiator attempted multiple untagged commands at the same
10361 * time. Can't do that.
10362 */
10363 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10364 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10365 && ((pending_io->io_hdr.nexus.targ_port ==
10366 ooa_io->io_hdr.nexus.targ_port)
10367 && (pending_io->io_hdr.nexus.initid.id ==
10368 ooa_io->io_hdr.nexus.initid.id))
10369 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10370 return (CTL_ACTION_OVERLAP);
10371
10372 /*
10373 * The initiator attempted to send multiple tagged commands with
10374 * the same ID. (It's fine if different initiators have the same
10375 * tag ID.)
10376 *
10377 * Even if all of those conditions are true, we don't kill the I/O
10378 * if the command ahead of us has been aborted. We won't end up
10379 * sending it to the FETD, and it's perfectly legal to resend a
10380 * command with the same tag number as long as the previous
10381 * instance of this tag number has been aborted somehow.
10382 */
10383 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10384 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10385 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10386 && ((pending_io->io_hdr.nexus.targ_port ==
10387 ooa_io->io_hdr.nexus.targ_port)
10388 && (pending_io->io_hdr.nexus.initid.id ==
10389 ooa_io->io_hdr.nexus.initid.id))
10390 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10391 return (CTL_ACTION_OVERLAP_TAG);
10392
10393 /*
10394 * If we get a head of queue tag, SAM-3 says that we should
10395 * immediately execute it.
10396 *
10397 * What happens if this command would normally block for some other
10398 * reason? e.g. a request sense with a head of queue tag
10399 * immediately after a write. Normally that would block, but this
10400 * will result in its getting executed immediately...
10401 *
10402 * We currently return "pass" instead of "skip", so we'll end up
10403 * going through the rest of the queue to check for overlapped tags.
10404 *
10405 * XXX KDM check for other types of blockage first??
10406 */
10407 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10408 return (CTL_ACTION_PASS);
10409
10410 /*
10411 * Ordered tags have to block until all items ahead of them
10412 * have completed. If we get called with an ordered tag, we always
10413 * block, if something else is ahead of us in the queue.
10414 */
10415 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10416 return (CTL_ACTION_BLOCK);
10417
10418 /*
10419 * Simple tags get blocked until all head of queue and ordered tags
10420 * ahead of them have completed. I'm lumping untagged commands in
10421 * with simple tags here. XXX KDM is that the right thing to do?
10422 */
10423 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10424 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10425 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10426 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10427 return (CTL_ACTION_BLOCK);
10428
10429 pending_entry = &ctl_cmd_table[pending_io->scsiio.cdb[0]];
10430 ooa_entry = &ctl_cmd_table[ooa_io->scsiio.cdb[0]];
10431
10432 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10433
10434 switch (serialize_row[pending_entry->seridx]) {
10435 case CTL_SER_BLOCK:
10436 return (CTL_ACTION_BLOCK);
10437 break; /* NOTREACHED */
10438 case CTL_SER_EXTENT:
10439 return (ctl_extent_check(pending_io, ooa_io));
10440 break; /* NOTREACHED */
10441 case CTL_SER_PASS:
10442 return (CTL_ACTION_PASS);
10443 break; /* NOTREACHED */
10444 case CTL_SER_SKIP:
10445 return (CTL_ACTION_SKIP);
10446 break;
10447 default:
10448 panic("invalid serialization value %d",
10449 serialize_row[pending_entry->seridx]);
10450 break; /* NOTREACHED */
10451 }
10452
10453 return (CTL_ACTION_ERROR);
10454}
10455
10456/*
10457 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10458 * Assumptions:
10459 * - pending_io is generally either incoming, or on the blocked queue
10460 * - starting I/O is the I/O we want to start the check with.
10461 */
10462static ctl_action
10463ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10464 union ctl_io *starting_io)
10465{
10466 union ctl_io *ooa_io;
10467 ctl_action action;
10468
10469 mtx_assert(&control_softc->ctl_lock, MA_OWNED);
10470
10471 /*
10472 * Run back along the OOA queue, starting with the current
10473 * blocked I/O and going through every I/O before it on the
10474 * queue. If starting_io is NULL, we'll just end up returning
10475 * CTL_ACTION_PASS.
10476 */
10477 for (ooa_io = starting_io; ooa_io != NULL;
10478 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10479 ooa_links)){
10480
10481 /*
10482 * This routine just checks to see whether
10483 * cur_blocked is blocked by ooa_io, which is ahead
10484 * of it in the queue. It doesn't queue/dequeue
10485 * cur_blocked.
10486 */
10487 action = ctl_check_for_blockage(pending_io, ooa_io);
10488 switch (action) {
10489 case CTL_ACTION_BLOCK:
10490 case CTL_ACTION_OVERLAP:
10491 case CTL_ACTION_OVERLAP_TAG:
10492 case CTL_ACTION_SKIP:
10493 case CTL_ACTION_ERROR:
10494 return (action);
10495 break; /* NOTREACHED */
10496 case CTL_ACTION_PASS:
10497 break;
10498 default:
10499 panic("invalid action %d", action);
10500 break; /* NOTREACHED */
10501 }
10502 }
10503
10504 return (CTL_ACTION_PASS);
10505}
10506
10507/*
10508 * Assumptions:
10509 * - An I/O has just completed, and has been removed from the per-LUN OOA
10510 * queue, so some items on the blocked queue may now be unblocked.
10511 */
10512static int
10513ctl_check_blocked(struct ctl_lun *lun)
10514{
10515 union ctl_io *cur_blocked, *next_blocked;
10516
10517 mtx_assert(&control_softc->ctl_lock, MA_OWNED);
10518
10519 /*
10520 * Run forward from the head of the blocked queue, checking each
10521 * entry against the I/Os prior to it on the OOA queue to see if
10522 * there is still any blockage.
10523 *
10524 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
10525 * with our removing a variable on it while it is traversing the
10526 * list.
10527 */
10528 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
10529 cur_blocked != NULL; cur_blocked = next_blocked) {
10530 union ctl_io *prev_ooa;
10531 ctl_action action;
10532
10533 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
10534 blocked_links);
10535
10536 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
10537 ctl_ooaq, ooa_links);
10538
10539 /*
10540 * If cur_blocked happens to be the first item in the OOA
10541 * queue now, prev_ooa will be NULL, and the action
10542 * returned will just be CTL_ACTION_PASS.
10543 */
10544 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
10545
10546 switch (action) {
10547 case CTL_ACTION_BLOCK:
10548 /* Nothing to do here, still blocked */
10549 break;
10550 case CTL_ACTION_OVERLAP:
10551 case CTL_ACTION_OVERLAP_TAG:
10552 /*
10553 * This shouldn't happen! In theory we've already
10554 * checked this command for overlap...
10555 */
10556 break;
10557 case CTL_ACTION_PASS:
10558 case CTL_ACTION_SKIP: {
10559 struct ctl_softc *softc;
10560 struct ctl_cmd_entry *entry;
10561 uint32_t initidx;
10562 uint8_t opcode;
10563 int isc_retval;
10564
10565 /*
10566 * The skip case shouldn't happen, this transaction
10567 * should have never made it onto the blocked queue.
10568 */
10569 /*
10570 * This I/O is no longer blocked, we can remove it
10571 * from the blocked queue. Since this is a TAILQ
10572 * (doubly linked list), we can do O(1) removals
10573 * from any place on the list.
10574 */
10575 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
10576 blocked_links);
10577 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10578
10579 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){
10580 /*
10581 * Need to send IO back to original side to
10582 * run
10583 */
10584 union ctl_ha_msg msg_info;
10585
10586 msg_info.hdr.original_sc =
10587 cur_blocked->io_hdr.original_sc;
10588 msg_info.hdr.serializing_sc = cur_blocked;
10589 msg_info.hdr.msg_type = CTL_MSG_R2R;
10590 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
10591 &msg_info, sizeof(msg_info), 0)) >
10592 CTL_HA_STATUS_SUCCESS) {
10593 printf("CTL:Check Blocked error from "
10594 "ctl_ha_msg_send %d\n",
10595 isc_retval);
10596 }
10597 break;
10598 }
10599 opcode = cur_blocked->scsiio.cdb[0];
10600 entry = &ctl_cmd_table[opcode];
10601 softc = control_softc;
10602
10603 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus);
10604
10605 /*
10606 * Check this I/O for LUN state changes that may
10607 * have happened while this command was blocked.
10608 * The LUN state may have been changed by a command
10609 * ahead of us in the queue, so we need to re-check
10610 * for any states that can be caused by SCSI
10611 * commands.
10612 */
10613 if (ctl_scsiio_lun_check(softc, lun, entry,
10614 &cur_blocked->scsiio) == 0) {
10615 cur_blocked->io_hdr.flags |=
10616 CTL_FLAG_IS_WAS_ON_RTR;
10617 STAILQ_INSERT_TAIL(&lun->ctl_softc->rtr_queue,
10618 &cur_blocked->io_hdr, links);
10619 /*
10620 * In the non CTL_DONE_THREAD case, we need
10621 * to wake up the work thread here. When
10622 * we're processing completed requests from
10623 * the work thread context, we'll pop back
10624 * around and end up pulling things off the
10625 * RtR queue. When we aren't processing
10626 * things from the work thread context,
10627 * though, we won't ever check the RtR queue.
10628 * So we need to wake up the thread to clear
10629 * things off the queue. Otherwise this
10630 * transaction will just sit on the RtR queue
10631 * until a new I/O comes in. (Which may or
10632 * may not happen...)
10633 */
10634#ifndef CTL_DONE_THREAD
10635 ctl_wakeup_thread();
10636#endif
10637 } else
10638 ctl_done_lock(cur_blocked, /*have_lock*/ 1);
10639 break;
10640 }
10641 default:
10642 /*
10643 * This probably shouldn't happen -- we shouldn't
10644 * get CTL_ACTION_ERROR, or anything else.
10645 */
10646 break;
10647 }
10648 }
10649
10650 return (CTL_RETVAL_COMPLETE);
10651}
10652
10653/*
10654 * This routine (with one exception) checks LUN flags that can be set by
10655 * commands ahead of us in the OOA queue. These flags have to be checked
10656 * when a command initially comes in, and when we pull a command off the
10657 * blocked queue and are preparing to execute it. The reason we have to
10658 * check these flags for commands on the blocked queue is that the LUN
10659 * state may have been changed by a command ahead of us while we're on the
10660 * blocked queue.
10661 *
10662 * Ordering is somewhat important with these checks, so please pay
10663 * careful attention to the placement of any new checks.
10664 */
10665static int
10666ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
10667 struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
10668{
10669 int retval;
10670
10671 retval = 0;
10672
10673 /*
10674 * If this shelf is a secondary shelf controller, we have to reject
10675 * any media access commands.
10676 */
10677#if 0
10678 /* No longer needed for HA */
10679 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0)
10680 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) {
10681 ctl_set_lun_standby(ctsio);
10682 retval = 1;
10683 goto bailout;
10684 }
10685#endif
10686
10687 /*
10688 * Check for a reservation conflict. If this command isn't allowed
10689 * even on reserved LUNs, and if this initiator isn't the one who
10690 * reserved us, reject the command with a reservation conflict.
10691 */
10692 if ((lun->flags & CTL_LUN_RESERVED)
10693 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
10694 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
10695 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
10696 || (ctsio->io_hdr.nexus.targ_target.id !=
10697 lun->rsv_nexus.targ_target.id)) {
10698 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
10699 ctsio->io_hdr.status = CTL_SCSI_ERROR;
10700 retval = 1;
10701 goto bailout;
10702 }
10703 }
10704
10705 if ( (lun->flags & CTL_LUN_PR_RESERVED)
10706 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) {
10707 uint32_t residx;
10708
10709 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
10710 /*
10711 * if we aren't registered or it's a res holder type
10712 * reservation and this isn't the res holder then set a
10713 * conflict.
10714 * NOTE: Commands which might be allowed on write exclusive
10715 * type reservations are checked in the particular command
10716 * for a conflict. Read and SSU are the only ones.
10717 */
10718 if (!lun->per_res[residx].registered
10719 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
10720 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
10721 ctsio->io_hdr.status = CTL_SCSI_ERROR;
10722 retval = 1;
10723 goto bailout;
10724 }
10725
10726 }
10727
10728 if ((lun->flags & CTL_LUN_OFFLINE)
10729 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) {
10730 ctl_set_lun_not_ready(ctsio);
10731 retval = 1;
10732 goto bailout;
10733 }
10734
10735 /*
10736 * If the LUN is stopped, see if this particular command is allowed
10737 * for a stopped lun. Otherwise, reject it with 0x04,0x02.
10738 */
10739 if ((lun->flags & CTL_LUN_STOPPED)
10740 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
10741 /* "Logical unit not ready, initializing cmd. required" */
10742 ctl_set_lun_stopped(ctsio);
10743 retval = 1;
10744 goto bailout;
10745 }
10746
10747 if ((lun->flags & CTL_LUN_INOPERABLE)
10748 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
10749 /* "Medium format corrupted" */
10750 ctl_set_medium_format_corrupted(ctsio);
10751 retval = 1;
10752 goto bailout;
10753 }
10754
10755bailout:
10756 return (retval);
10757
10758}
10759
10760static void
10761ctl_failover_io(union ctl_io *io, int have_lock)
10762{
10763 ctl_set_busy(&io->scsiio);
10764 ctl_done_lock(io, have_lock);
10765}
10766
10767static void
10768ctl_failover(void)
10769{
10770 struct ctl_lun *lun;
10771 struct ctl_softc *ctl_softc;
10772 union ctl_io *next_io, *pending_io;
10773 union ctl_io *io;
10774 int lun_idx;
10775 int i;
10776
10777 ctl_softc = control_softc;
10778
10779 mtx_lock(&ctl_softc->ctl_lock);
10780 /*
10781 * Remove any cmds from the other SC from the rtr queue. These
10782 * will obviously only be for LUNs for which we're the primary.
10783 * We can't send status or get/send data for these commands.
10784 * Since they haven't been executed yet, we can just remove them.
10785 * We'll either abort them or delete them below, depending on
10786 * which HA mode we're in.
10787 */
10788 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue);
10789 io != NULL; io = next_io) {
10790 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
10791 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
10792 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr,
10793 ctl_io_hdr, links);
10794 }
10795
10796 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) {
10797 lun = ctl_softc->ctl_luns[lun_idx];
10798 if (lun==NULL)
10799 continue;
10800
10801 /*
10802 * Processor LUNs are primary on both sides.
10803 * XXX will this always be true?
10804 */
10805 if (lun->be_lun->lun_type == T_PROCESSOR)
10806 continue;
10807
10808 if ((lun->flags & CTL_LUN_PRIMARY_SC)
10809 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
10810 printf("FAILOVER: primary lun %d\n", lun_idx);
10811 /*
10812 * Remove all commands from the other SC. First from the
10813 * blocked queue then from the ooa queue. Once we have
10814 * removed them. Call ctl_check_blocked to see if there
10815 * is anything that can run.
10816 */
10817 for (io = (union ctl_io *)TAILQ_FIRST(
10818 &lun->blocked_queue); io != NULL; io = next_io) {
10819
10820 next_io = (union ctl_io *)TAILQ_NEXT(
10821 &io->io_hdr, blocked_links);
10822
10823 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
10824 TAILQ_REMOVE(&lun->blocked_queue,
10825 &io->io_hdr,blocked_links);
10826 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10827 TAILQ_REMOVE(&lun->ooa_queue,
10828 &io->io_hdr, ooa_links);
10829
10830 ctl_free_io(io);
10831 }
10832 }
10833
10834 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
10835 io != NULL; io = next_io) {
10836
10837 next_io = (union ctl_io *)TAILQ_NEXT(
10838 &io->io_hdr, ooa_links);
10839
10840 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
10841
10842 TAILQ_REMOVE(&lun->ooa_queue,
10843 &io->io_hdr,
10844 ooa_links);
10845
10846 ctl_free_io(io);
10847 }
10848 }
10849 ctl_check_blocked(lun);
10850 } else if ((lun->flags & CTL_LUN_PRIMARY_SC)
10851 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
10852
10853 printf("FAILOVER: primary lun %d\n", lun_idx);
10854 /*
10855 * Abort all commands from the other SC. We can't
10856 * send status back for them now. These should get
10857 * cleaned up when they are completed or come out
10858 * for a datamove operation.
10859 */
10860 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
10861 io != NULL; io = next_io) {
10862 next_io = (union ctl_io *)TAILQ_NEXT(
10863 &io->io_hdr, ooa_links);
10864
10865 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
10866 io->io_hdr.flags |= CTL_FLAG_ABORT;
10867 }
10868 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
10869 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
10870
10871 printf("FAILOVER: secondary lun %d\n", lun_idx);
10872
10873 lun->flags |= CTL_LUN_PRIMARY_SC;
10874
10875 /*
10876 * We send all I/O that was sent to this controller
10877 * and redirected to the other side back with
10878 * busy status, and have the initiator retry it.
10879 * Figuring out how much data has been transferred,
10880 * etc. and picking up where we left off would be
10881 * very tricky.
10882 *
10883 * XXX KDM need to remove I/O from the blocked
10884 * queue as well!
10885 */
10886 for (pending_io = (union ctl_io *)TAILQ_FIRST(
10887 &lun->ooa_queue); pending_io != NULL;
10888 pending_io = next_io) {
10889
10890 next_io = (union ctl_io *)TAILQ_NEXT(
10891 &pending_io->io_hdr, ooa_links);
10892
10893 pending_io->io_hdr.flags &=
10894 ~CTL_FLAG_SENT_2OTHER_SC;
10895
10896 if (pending_io->io_hdr.flags &
10897 CTL_FLAG_IO_ACTIVE) {
10898 pending_io->io_hdr.flags |=
10899 CTL_FLAG_FAILOVER;
10900 } else {
10901 ctl_set_busy(&pending_io->scsiio);
10902 ctl_done_lock(pending_io,
10903 /*have_lock*/1);
10904 }
10905 }
10906
10907 /*
10908 * Build Unit Attention
10909 */
10910 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
10911 lun->pending_sense[i].ua_pending |=
10912 CTL_UA_ASYM_ACC_CHANGE;
10913 }
10914 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
10915 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
10916 printf("FAILOVER: secondary lun %d\n", lun_idx);
10917 /*
10918 * if the first io on the OOA is not on the RtR queue
10919 * add it.
10920 */
10921 lun->flags |= CTL_LUN_PRIMARY_SC;
10922
10923 pending_io = (union ctl_io *)TAILQ_FIRST(
10924 &lun->ooa_queue);
10925 if (pending_io==NULL) {
10926 printf("Nothing on OOA queue\n");
10927 continue;
10928 }
10929
10930 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
10931 if ((pending_io->io_hdr.flags &
10932 CTL_FLAG_IS_WAS_ON_RTR) == 0) {
10933 pending_io->io_hdr.flags |=
10934 CTL_FLAG_IS_WAS_ON_RTR;
10935 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue,
10936 &pending_io->io_hdr, links);
10937 }
10938#if 0
10939 else
10940 {
10941 printf("Tag 0x%04x is running\n",
10942 pending_io->scsiio.tag_num);
10943 }
10944#endif
10945
10946 next_io = (union ctl_io *)TAILQ_NEXT(
10947 &pending_io->io_hdr, ooa_links);
10948 for (pending_io=next_io; pending_io != NULL;
10949 pending_io = next_io) {
10950 pending_io->io_hdr.flags &=
10951 ~CTL_FLAG_SENT_2OTHER_SC;
10952 next_io = (union ctl_io *)TAILQ_NEXT(
10953 &pending_io->io_hdr, ooa_links);
10954 if (pending_io->io_hdr.flags &
10955 CTL_FLAG_IS_WAS_ON_RTR) {
10956#if 0
10957 printf("Tag 0x%04x is running\n",
10958 pending_io->scsiio.tag_num);
10959#endif
10960 continue;
10961 }
10962
10963 switch (ctl_check_ooa(lun, pending_io,
10964 (union ctl_io *)TAILQ_PREV(
10965 &pending_io->io_hdr, ctl_ooaq,
10966 ooa_links))) {
10967
10968 case CTL_ACTION_BLOCK:
10969 TAILQ_INSERT_TAIL(&lun->blocked_queue,
10970 &pending_io->io_hdr,
10971 blocked_links);
10972 pending_io->io_hdr.flags |=
10973 CTL_FLAG_BLOCKED;
10974 break;
10975 case CTL_ACTION_PASS:
10976 case CTL_ACTION_SKIP:
10977 pending_io->io_hdr.flags |=
10978 CTL_FLAG_IS_WAS_ON_RTR;
10979 STAILQ_INSERT_TAIL(
10980 &ctl_softc->rtr_queue,
10981 &pending_io->io_hdr, links);
10982 break;
10983 case CTL_ACTION_OVERLAP:
10984 ctl_set_overlapped_cmd(
10985 (struct ctl_scsiio *)pending_io);
10986 ctl_done_lock(pending_io,
10987 /*have_lock*/ 1);
10988 break;
10989 case CTL_ACTION_OVERLAP_TAG:
10990 ctl_set_overlapped_tag(
10991 (struct ctl_scsiio *)pending_io,
10992 pending_io->scsiio.tag_num & 0xff);
10993 ctl_done_lock(pending_io,
10994 /*have_lock*/ 1);
10995 break;
10996 case CTL_ACTION_ERROR:
10997 default:
10998 ctl_set_internal_failure(
10999 (struct ctl_scsiio *)pending_io,
11000 0, // sks_valid
11001 0); //retry count
11002 ctl_done_lock(pending_io,
11003 /*have_lock*/ 1);
11004 break;
11005 }
11006 }
11007
11008 /*
11009 * Build Unit Attention
11010 */
11011 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11012 lun->pending_sense[i].ua_pending |=
11013 CTL_UA_ASYM_ACC_CHANGE;
11014 }
11015 } else {
11016 panic("Unhandled HA mode failover, LUN flags = %#x, "
11017 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode);
11018 }
11019 }
11020 ctl_pause_rtr = 0;
11021 mtx_unlock(&ctl_softc->ctl_lock);
11022}
11023
11024static int
11025ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio)
11026{
11027 struct ctl_lun *lun;
11028 struct ctl_cmd_entry *entry;
11029 uint8_t opcode;
11030 uint32_t initidx, targ_lun;
11031 int retval;
11032
11033 retval = 0;
11034
11035 lun = NULL;
11036
11037 opcode = ctsio->cdb[0];
11038
11039 mtx_lock(&ctl_softc->ctl_lock);
11040
11041 targ_lun = ctsio->io_hdr.nexus.targ_lun;
11042 if (ctsio->io_hdr.nexus.lun_map_fn != NULL)
11043 targ_lun = ctsio->io_hdr.nexus.lun_map_fn(ctsio->io_hdr.nexus.lun_map_arg, targ_lun);
11044 if ((targ_lun < CTL_MAX_LUNS)
11045 && (ctl_softc->ctl_luns[targ_lun] != NULL)) {
11046 lun = ctl_softc->ctl_luns[targ_lun];
11047 /*
11048 * If the LUN is invalid, pretend that it doesn't exist.
11049 * It will go away as soon as all pending I/O has been
11050 * completed.
11051 */
11052 if (lun->flags & CTL_LUN_DISABLED) {
11053 lun = NULL;
11054 } else {
11055 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11056 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11057 lun->be_lun;
11058 if (lun->be_lun->lun_type == T_PROCESSOR) {
11059 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV;
11060 }
11061 }
11062 } else {
11063 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11064 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11065 }
11066
11067 entry = &ctl_cmd_table[opcode];
11068
11069 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11070 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11071
11072 /*
11073 * Check to see whether we can send this command to LUNs that don't
11074 * exist. This should pretty much only be the case for inquiry
11075 * and request sense. Further checks, below, really require having
11076 * a LUN, so we can't really check the command anymore. Just put
11077 * it on the rtr queue.
11078 */
11079 if (lun == NULL) {
11080 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS)
11081 goto queue_rtr;
11082
11083 ctl_set_unsupported_lun(ctsio);
11084 mtx_unlock(&ctl_softc->ctl_lock);
11085 ctl_done((union ctl_io *)ctsio);
11086 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11087 goto bailout;
11088 } else {
11089 /*
11090 * Every I/O goes into the OOA queue for a particular LUN, and
11091 * stays there until completion.
11092 */
11093 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
11094
11095 /*
11096 * Make sure we support this particular command on this LUN.
11097 * e.g., we don't support writes to the control LUN.
11098 */
11099 switch (lun->be_lun->lun_type) {
11100 case T_PROCESSOR:
11101 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11102 && ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS)
11103 == 0)) {
11104 ctl_set_invalid_opcode(ctsio);
11105 mtx_unlock(&ctl_softc->ctl_lock);
11106 ctl_done((union ctl_io *)ctsio);
11107 goto bailout;
11108 }
11109 break;
11110 case T_DIRECT:
11111 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0)
11112 && ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS)
11113 == 0)){
11114 ctl_set_invalid_opcode(ctsio);
11115 mtx_unlock(&ctl_softc->ctl_lock);
11116 ctl_done((union ctl_io *)ctsio);
11117 goto bailout;
11118 }
11119 break;
11120 default:
11121 printf("Unsupported CTL LUN type %d\n",
11122 lun->be_lun->lun_type);
11123 panic("Unsupported CTL LUN type %d\n",
11124 lun->be_lun->lun_type);
11125 break; /* NOTREACHED */
11126 }
11127 }
11128
11129 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11130
11131 /*
11132 * If we've got a request sense, it'll clear the contingent
11133 * allegiance condition. Otherwise, if we have a CA condition for
11134 * this initiator, clear it, because it sent down a command other
11135 * than request sense.
11136 */
11137 if ((opcode != REQUEST_SENSE)
11138 && (ctl_is_set(lun->have_ca, initidx)))
11139 ctl_clear_mask(lun->have_ca, initidx);
11140
11141 /*
11142 * If the command has this flag set, it handles its own unit
11143 * attention reporting, we shouldn't do anything. Otherwise we
11144 * check for any pending unit attentions, and send them back to the
11145 * initiator. We only do this when a command initially comes in,
11146 * not when we pull it off the blocked queue.
11147 *
11148 * According to SAM-3, section 5.3.2, the order that things get
11149 * presented back to the host is basically unit attentions caused
11150 * by some sort of reset event, busy status, reservation conflicts
11151 * or task set full, and finally any other status.
11152 *
11153 * One issue here is that some of the unit attentions we report
11154 * don't fall into the "reset" category (e.g. "reported luns data
11155 * has changed"). So reporting it here, before the reservation
11156 * check, may be technically wrong. I guess the only thing to do
11157 * would be to check for and report the reset events here, and then
11158 * check for the other unit attention types after we check for a
11159 * reservation conflict.
11160 *
11161 * XXX KDM need to fix this
11162 */
11163 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11164 ctl_ua_type ua_type;
11165
11166 ua_type = lun->pending_sense[initidx].ua_pending;
11167 if (ua_type != CTL_UA_NONE) {
11168 scsi_sense_data_type sense_format;
11169
11170 if (lun != NULL)
11171 sense_format = (lun->flags &
11172 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC :
11173 SSD_TYPE_FIXED;
11174 else
11175 sense_format = SSD_TYPE_FIXED;
11176
11177 ua_type = ctl_build_ua(ua_type, &ctsio->sense_data,
11178 sense_format);
11179 if (ua_type != CTL_UA_NONE) {
11180 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11181 ctsio->io_hdr.status = CTL_SCSI_ERROR |
11182 CTL_AUTOSENSE;
11183 ctsio->sense_len = SSD_FULL_SIZE;
11184 lun->pending_sense[initidx].ua_pending &=
11185 ~ua_type;
11186 mtx_unlock(&ctl_softc->ctl_lock);
11187 ctl_done((union ctl_io *)ctsio);
11188 goto bailout;
11189 }
11190 }
11191 }
11192
11193
11194 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) {
11195 mtx_unlock(&ctl_softc->ctl_lock);
11196 ctl_done((union ctl_io *)ctsio);
11197 goto bailout;
11198 }
11199
11200 /*
11201 * XXX CHD this is where we want to send IO to other side if
11202 * this LUN is secondary on this SC. We will need to make a copy
11203 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11204 * the copy we send as FROM_OTHER.
11205 * We also need to stuff the address of the original IO so we can
11206 * find it easily. Something similar will need be done on the other
11207 * side so when we are done we can find the copy.
11208 */
11209 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11210 union ctl_ha_msg msg_info;
11211 int isc_retval;
11212
11213 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11214
11215 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11216 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11217#if 0
11218 printf("1. ctsio %p\n", ctsio);
11219#endif
11220 msg_info.hdr.serializing_sc = NULL;
11221 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11222 msg_info.scsi.tag_num = ctsio->tag_num;
11223 msg_info.scsi.tag_type = ctsio->tag_type;
11224 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11225
11226 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11227
11228 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11229 (void *)&msg_info, sizeof(msg_info), 0)) >
11230 CTL_HA_STATUS_SUCCESS) {
11231 printf("CTL:precheck, ctl_ha_msg_send returned %d\n",
11232 isc_retval);
11233 printf("CTL:opcode is %x\n",opcode);
11234 } else {
11235#if 0
11236 printf("CTL:Precheck sent msg, opcode is %x\n",opcode);
11237#endif
11238 }
11239
11240 /*
11241 * XXX KDM this I/O is off the incoming queue, but hasn't
11242 * been inserted on any other queue. We may need to come
11243 * up with a holding queue while we wait for serialization
11244 * so that we have an idea of what we're waiting for from
11245 * the other side.
11246 */
11247 goto bailout_unlock;
11248 }
11249
11250 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11251 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11252 ctl_ooaq, ooa_links))) {
11253 case CTL_ACTION_BLOCK:
11254 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11255 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11256 blocked_links);
11257 goto bailout_unlock;
11258 break; /* NOTREACHED */
11259 case CTL_ACTION_PASS:
11260 case CTL_ACTION_SKIP:
11261 goto queue_rtr;
11262 break; /* NOTREACHED */
11263 case CTL_ACTION_OVERLAP:
11264 ctl_set_overlapped_cmd(ctsio);
11265 mtx_unlock(&ctl_softc->ctl_lock);
11266 ctl_done((union ctl_io *)ctsio);
11267 goto bailout;
11268 break; /* NOTREACHED */
11269 case CTL_ACTION_OVERLAP_TAG:
11270 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11271 mtx_unlock(&ctl_softc->ctl_lock);
11272 ctl_done((union ctl_io *)ctsio);
11273 goto bailout;
11274 break; /* NOTREACHED */
11275 case CTL_ACTION_ERROR:
11276 default:
11277 ctl_set_internal_failure(ctsio,
11278 /*sks_valid*/ 0,
11279 /*retry_count*/ 0);
11280 mtx_unlock(&ctl_softc->ctl_lock);
11281 ctl_done((union ctl_io *)ctsio);
11282 goto bailout;
11283 break; /* NOTREACHED */
11284 }
11285
11286 goto bailout_unlock;
11287
11288queue_rtr:
11289 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11290 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue, &ctsio->io_hdr, links);
11291
11292bailout_unlock:
11293 mtx_unlock(&ctl_softc->ctl_lock);
11294
11295bailout:
11296 return (retval);
11297}
11298
11299static int
11300ctl_scsiio(struct ctl_scsiio *ctsio)
11301{
11302 int retval;
11303 struct ctl_cmd_entry *entry;
11304
11305 retval = CTL_RETVAL_COMPLETE;
11306
11307 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11308
11309 entry = &ctl_cmd_table[ctsio->cdb[0]];
11310
11311 /*
11312 * If this I/O has been aborted, just send it straight to
11313 * ctl_done() without executing it.
11314 */
11315 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11316 ctl_done((union ctl_io *)ctsio);
11317 goto bailout;
11318 }
11319
11320 /*
11321 * All the checks should have been handled by ctl_scsiio_precheck().
11322 * We should be clear now to just execute the I/O.
11323 */
11324 retval = entry->execute(ctsio);
11325
11326bailout:
11327 return (retval);
11328}
11329
11330/*
11331 * Since we only implement one target right now, a bus reset simply resets
11332 * our single target.
11333 */
11334static int
11335ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io)
11336{
11337 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET));
11338}
11339
11340static int
11341ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
11342 ctl_ua_type ua_type)
11343{
11344 struct ctl_lun *lun;
11345 int retval;
11346
11347 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11348 union ctl_ha_msg msg_info;
11349
11350 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11351 msg_info.hdr.nexus = io->io_hdr.nexus;
11352 if (ua_type==CTL_UA_TARG_RESET)
11353 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11354 else
11355 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11356 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11357 msg_info.hdr.original_sc = NULL;
11358 msg_info.hdr.serializing_sc = NULL;
11359 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11360 (void *)&msg_info, sizeof(msg_info), 0)) {
11361 }
11362 }
11363 retval = 0;
11364
11365 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links)
11366 retval += ctl_lun_reset(lun, io, ua_type);
11367
11368 return (retval);
11369}
11370
11371/*
11372 * The LUN should always be set. The I/O is optional, and is used to
11373 * distinguish between I/Os sent by this initiator, and by other
11374 * initiators. We set unit attention for initiators other than this one.
11375 * SAM-3 is vague on this point. It does say that a unit attention should
11376 * be established for other initiators when a LUN is reset (see section
11377 * 5.7.3), but it doesn't specifically say that the unit attention should
11378 * be established for this particular initiator when a LUN is reset. Here
11379 * is the relevant text, from SAM-3 rev 8:
11380 *
11381 * 5.7.2 When a SCSI initiator port aborts its own tasks
11382 *
11383 * When a SCSI initiator port causes its own task(s) to be aborted, no
11384 * notification that the task(s) have been aborted shall be returned to
11385 * the SCSI initiator port other than the completion response for the
11386 * command or task management function action that caused the task(s) to
11387 * be aborted and notification(s) associated with related effects of the
11388 * action (e.g., a reset unit attention condition).
11389 *
11390 * XXX KDM for now, we're setting unit attention for all initiators.
11391 */
11392static int
11393ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11394{
11395 union ctl_io *xio;
11396#if 0
11397 uint32_t initindex;
11398#endif
11399 int i;
11400
11401 /*
11402 * Run through the OOA queue and abort each I/O.
11403 */
11404#if 0
11405 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
11406#endif
11407 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11408 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11409 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11410 }
11411
11412 /*
11413 * This version sets unit attention for every
11414 */
11415#if 0
11416 initindex = ctl_get_initindex(&io->io_hdr.nexus);
11417 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11418 if (initindex == i)
11419 continue;
11420 lun->pending_sense[i].ua_pending |= ua_type;
11421 }
11422#endif
11423
11424 /*
11425 * A reset (any kind, really) clears reservations established with
11426 * RESERVE/RELEASE. It does not clear reservations established
11427 * with PERSISTENT RESERVE OUT, but we don't support that at the
11428 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11429 * reservations made with the RESERVE/RELEASE commands, because
11430 * those commands are obsolete in SPC-3.
11431 */
11432 lun->flags &= ~CTL_LUN_RESERVED;
11433
11434 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11435 ctl_clear_mask(lun->have_ca, i);
11436 lun->pending_sense[i].ua_pending |= ua_type;
11437 }
11438
11439 return (0);
11440}
11441
11442static int
11443ctl_abort_task(union ctl_io *io)
11444{
11445 union ctl_io *xio;
11446 struct ctl_lun *lun;
11447 struct ctl_softc *ctl_softc;
11448#if 0
11449 struct sbuf sb;
11450 char printbuf[128];
11451#endif
11452 int found;
11453 uint32_t targ_lun;
11454
11455 ctl_softc = control_softc;
11456 found = 0;
11457
11458 /*
11459 * Look up the LUN.
11460 */
11461 targ_lun = io->io_hdr.nexus.targ_lun;
11462 if (io->io_hdr.nexus.lun_map_fn != NULL)
11463 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
11464 if ((targ_lun < CTL_MAX_LUNS)
11465 && (ctl_softc->ctl_luns[targ_lun] != NULL))
11466 lun = ctl_softc->ctl_luns[targ_lun];
11467 else
11468 goto bailout;
11469
11470#if 0
11471 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
11472 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
11473#endif
11474
11475 /*
11476 * Run through the OOA queue and attempt to find the given I/O.
11477 * The target port, initiator ID, tag type and tag number have to
11478 * match the values that we got from the initiator. If we have an
11479 * untagged command to abort, simply abort the first untagged command
11480 * we come to. We only allow one untagged command at a time of course.
11481 */
11482#if 0
11483 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
11484#endif
11485 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11486 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11487#if 0
11488 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
11489
11490 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
11491 lun->lun, xio->scsiio.tag_num,
11492 xio->scsiio.tag_type,
11493 (xio->io_hdr.blocked_links.tqe_prev
11494 == NULL) ? "" : " BLOCKED",
11495 (xio->io_hdr.flags &
11496 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
11497 (xio->io_hdr.flags &
11498 CTL_FLAG_ABORT) ? " ABORT" : "",
11499 (xio->io_hdr.flags &
11500 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
11501 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
11502 sbuf_finish(&sb);
11503 printf("%s\n", sbuf_data(&sb));
11504#endif
11505
11506 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port)
11507 && (xio->io_hdr.nexus.initid.id ==
11508 io->io_hdr.nexus.initid.id)) {
11509 /*
11510 * If the abort says that the task is untagged, the
11511 * task in the queue must be untagged. Otherwise,
11512 * we just check to see whether the tag numbers
11513 * match. This is because the QLogic firmware
11514 * doesn't pass back the tag type in an abort
11515 * request.
11516 */
11517#if 0
11518 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
11519 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
11520 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
11521#endif
11522 /*
11523 * XXX KDM we've got problems with FC, because it
11524 * doesn't send down a tag type with aborts. So we
11525 * can only really go by the tag number...
11526 * This may cause problems with parallel SCSI.
11527 * Need to figure that out!!
11528 */
11529 if (xio->scsiio.tag_num == io->taskio.tag_num) {
11530 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11531 found = 1;
11532 if ((io->io_hdr.flags &
11533 CTL_FLAG_FROM_OTHER_SC) == 0 &&
11534 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11535 union ctl_ha_msg msg_info;
11536
11537 io->io_hdr.flags |=
11538 CTL_FLAG_SENT_2OTHER_SC;
11539 msg_info.hdr.nexus = io->io_hdr.nexus;
11540 msg_info.task.task_action =
11541 CTL_TASK_ABORT_TASK;
11542 msg_info.task.tag_num =
11543 io->taskio.tag_num;
11544 msg_info.task.tag_type =
11545 io->taskio.tag_type;
11546 msg_info.hdr.msg_type =
11547 CTL_MSG_MANAGE_TASKS;
11548 msg_info.hdr.original_sc = NULL;
11549 msg_info.hdr.serializing_sc = NULL;
11550#if 0
11551 printf("Sent Abort to other side\n");
11552#endif
11553 if (CTL_HA_STATUS_SUCCESS !=
11554 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11555 (void *)&msg_info,
11556 sizeof(msg_info), 0)) {
11557 }
11558 }
11559#if 0
11560 printf("ctl_abort_task: found I/O to abort\n");
11561#endif
11562 break;
11563 }
11564 }
11565 }
11566
11567bailout:
11568
11569 if (found == 0) {
11570 /*
11571 * This isn't really an error. It's entirely possible for
11572 * the abort and command completion to cross on the wire.
11573 * This is more of an informative/diagnostic error.
11574 */
11575#if 0
11576 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
11577 "%d:%d:%d:%d tag %d type %d\n",
11578 io->io_hdr.nexus.initid.id,
11579 io->io_hdr.nexus.targ_port,
11580 io->io_hdr.nexus.targ_target.id,
11581 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
11582 io->taskio.tag_type);
11583#endif
11584 return (1);
11585 } else
11586 return (0);
11587}
11588
11589/*
11590 * This routine cannot block! It must be callable from an interrupt
11591 * handler as well as from the work thread.
11592 */
11593static void
11594ctl_run_task_queue(struct ctl_softc *ctl_softc)
11595{
11596 union ctl_io *io, *next_io;
11597
11598 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
11599
11600 CTL_DEBUG_PRINT(("ctl_run_task_queue\n"));
11601
11602 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->task_queue);
11603 io != NULL; io = next_io) {
11604 int retval;
11605 const char *task_desc;
11606
11607 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
11608
11609 retval = 0;
11610
11611 switch (io->io_hdr.io_type) {
11612 case CTL_IO_TASK: {
11613 task_desc = ctl_scsi_task_string(&io->taskio);
11614 if (task_desc != NULL) {
11615#ifdef NEEDTOPORT
11616 csevent_log(CSC_CTL | CSC_SHELF_SW |
11617 CTL_TASK_REPORT,
11618 csevent_LogType_Trace,
11619 csevent_Severity_Information,
11620 csevent_AlertLevel_Green,
11621 csevent_FRU_Firmware,
11622 csevent_FRU_Unknown,
11623 "CTL: received task: %s",task_desc);
11624#endif
11625 } else {
11626#ifdef NEEDTOPORT
11627 csevent_log(CSC_CTL | CSC_SHELF_SW |
11628 CTL_TASK_REPORT,
11629 csevent_LogType_Trace,
11630 csevent_Severity_Information,
11631 csevent_AlertLevel_Green,
11632 csevent_FRU_Firmware,
11633 csevent_FRU_Unknown,
11634 "CTL: received unknown task "
11635 "type: %d (%#x)",
11636 io->taskio.task_action,
11637 io->taskio.task_action);
11638#endif
11639 }
11640 switch (io->taskio.task_action) {
11641 case CTL_TASK_ABORT_TASK:
11642 retval = ctl_abort_task(io);
11643 break;
11644 case CTL_TASK_ABORT_TASK_SET:
11645 break;
11646 case CTL_TASK_CLEAR_ACA:
11647 break;
11648 case CTL_TASK_CLEAR_TASK_SET:
11649 break;
11650 case CTL_TASK_LUN_RESET: {
11651 struct ctl_lun *lun;
11652 uint32_t targ_lun;
11653 int retval;
11654
11655 targ_lun = io->io_hdr.nexus.targ_lun;
11656 if (io->io_hdr.nexus.lun_map_fn != NULL)
11657 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
11658
11659 if ((targ_lun < CTL_MAX_LUNS)
11660 && (ctl_softc->ctl_luns[targ_lun] != NULL))
11661 lun = ctl_softc->ctl_luns[targ_lun];
11662 else {
11663 retval = 1;
11664 break;
11665 }
11666
11667 if (!(io->io_hdr.flags &
11668 CTL_FLAG_FROM_OTHER_SC)) {
11669 union ctl_ha_msg msg_info;
11670
11671 io->io_hdr.flags |=
11672 CTL_FLAG_SENT_2OTHER_SC;
11673 msg_info.hdr.msg_type =
11674 CTL_MSG_MANAGE_TASKS;
11675 msg_info.hdr.nexus = io->io_hdr.nexus;
11676 msg_info.task.task_action =
11677 CTL_TASK_LUN_RESET;
11678 msg_info.hdr.original_sc = NULL;
11679 msg_info.hdr.serializing_sc = NULL;
11680 if (CTL_HA_STATUS_SUCCESS !=
11681 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11682 (void *)&msg_info,
11683 sizeof(msg_info), 0)) {
11684 }
11685 }
11686
11687 retval = ctl_lun_reset(lun, io,
11688 CTL_UA_LUN_RESET);
11689 break;
11690 }
11691 case CTL_TASK_TARGET_RESET:
11692 retval = ctl_target_reset(ctl_softc, io,
11693 CTL_UA_TARG_RESET);
11694 break;
11695 case CTL_TASK_BUS_RESET:
11696 retval = ctl_bus_reset(ctl_softc, io);
11697 break;
11698 case CTL_TASK_PORT_LOGIN:
11699 break;
11700 case CTL_TASK_PORT_LOGOUT:
11701 break;
11702 default:
11703 printf("ctl_run_task_queue: got unknown task "
11704 "management event %d\n",
11705 io->taskio.task_action);
11706 break;
11707 }
11708 if (retval == 0)
11709 io->io_hdr.status = CTL_SUCCESS;
11710 else
11711 io->io_hdr.status = CTL_ERROR;
11712
11713 STAILQ_REMOVE(&ctl_softc->task_queue, &io->io_hdr,
11714 ctl_io_hdr, links);
11715 /*
11716 * This will queue this I/O to the done queue, but the
11717 * work thread won't be able to process it until we
11718 * return and the lock is released.
11719 */
11720 ctl_done_lock(io, /*have_lock*/ 1);
11721 break;
11722 }
11723 default: {
11724
11725 printf("%s: invalid I/O type %d msg %d cdb %x"
11726 " iptl: %ju:%d:%ju:%d tag 0x%04x\n",
11727 __func__, io->io_hdr.io_type,
11728 io->io_hdr.msg_type, io->scsiio.cdb[0],
11729 (uintmax_t)io->io_hdr.nexus.initid.id,
11730 io->io_hdr.nexus.targ_port,
11731 (uintmax_t)io->io_hdr.nexus.targ_target.id,
11732 io->io_hdr.nexus.targ_lun /* XXX */,
11733 (io->io_hdr.io_type == CTL_IO_TASK) ?
11734 io->taskio.tag_num : io->scsiio.tag_num);
11735 STAILQ_REMOVE(&ctl_softc->task_queue, &io->io_hdr,
11736 ctl_io_hdr, links);
11737 ctl_free_io(io);
11738 break;
11739 }
11740 }
11741 }
11742
11743 ctl_softc->flags &= ~CTL_FLAG_TASK_PENDING;
11744}
11745
11746/*
11747 * For HA operation. Handle commands that come in from the other
11748 * controller.
11749 */
11750static void
11751ctl_handle_isc(union ctl_io *io)
11752{
11753 int free_io;
11754 struct ctl_lun *lun;
11755 struct ctl_softc *ctl_softc;
11756 uint32_t targ_lun;
11757
11758 ctl_softc = control_softc;
11759
11760 targ_lun = io->io_hdr.nexus.targ_lun;
11761 if (io->io_hdr.nexus.lun_map_fn != NULL)
11762 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
11763 lun = ctl_softc->ctl_luns[targ_lun];
11764
11765 switch (io->io_hdr.msg_type) {
11766 case CTL_MSG_SERIALIZE:
11767 free_io = ctl_serialize_other_sc_cmd(&io->scsiio,
11768 /*have_lock*/ 0);
11769 break;
11770 case CTL_MSG_R2R: {
11771 uint8_t opcode;
11772 struct ctl_cmd_entry *entry;
11773
11774 /*
11775 * This is only used in SER_ONLY mode.
11776 */
11777 free_io = 0;
11778 opcode = io->scsiio.cdb[0];
11779 entry = &ctl_cmd_table[opcode];
11780 mtx_lock(&ctl_softc->ctl_lock);
11781 if (ctl_scsiio_lun_check(ctl_softc, lun,
11782 entry, (struct ctl_scsiio *)io) != 0) {
11783 ctl_done_lock(io, /*have_lock*/ 1);
11784 mtx_unlock(&ctl_softc->ctl_lock);
11785 break;
11786 }
11787 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11788 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue,
11789 &io->io_hdr, links);
11790 mtx_unlock(&ctl_softc->ctl_lock);
11791 break;
11792 }
11793 case CTL_MSG_FINISH_IO:
11794 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
11795 free_io = 0;
11796 ctl_done_lock(io, /*have_lock*/ 0);
11797 } else {
11798 free_io = 1;
11799 mtx_lock(&ctl_softc->ctl_lock);
11800 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
11801 ooa_links);
11802 STAILQ_REMOVE(&ctl_softc->task_queue,
11803 &io->io_hdr, ctl_io_hdr, links);
11804 ctl_check_blocked(lun);
11805 mtx_unlock(&ctl_softc->ctl_lock);
11806 }
11807 break;
11808 case CTL_MSG_PERS_ACTION:
11809 ctl_hndl_per_res_out_on_other_sc(
11810 (union ctl_ha_msg *)&io->presio.pr_msg);
11811 free_io = 1;
11812 break;
11813 case CTL_MSG_BAD_JUJU:
11814 free_io = 0;
11815 ctl_done_lock(io, /*have_lock*/ 0);
11816 break;
11817 case CTL_MSG_DATAMOVE:
11818 /* Only used in XFER mode */
11819 free_io = 0;
11820 ctl_datamove_remote(io);
11821 break;
11822 case CTL_MSG_DATAMOVE_DONE:
11823 /* Only used in XFER mode */
11824 free_io = 0;
11825 io->scsiio.be_move_done(io);
11826 break;
11827 default:
11828 free_io = 1;
11829 printf("%s: Invalid message type %d\n",
11830 __func__, io->io_hdr.msg_type);
11831 break;
11832 }
11833 if (free_io)
11834 ctl_free_io(io);
11835
11836}
11837
11838
11839/*
11840 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
11841 * there is no match.
11842 */
11843static ctl_lun_error_pattern
11844ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
11845{
11846 struct ctl_cmd_entry *entry;
11847 ctl_lun_error_pattern filtered_pattern, pattern;
11848 uint8_t opcode;
11849
11850 pattern = desc->error_pattern;
11851
11852 /*
11853 * XXX KDM we need more data passed into this function to match a
11854 * custom pattern, and we actually need to implement custom pattern
11855 * matching.
11856 */
11857 if (pattern & CTL_LUN_PAT_CMD)
11858 return (CTL_LUN_PAT_CMD);
11859
11860 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
11861 return (CTL_LUN_PAT_ANY);
11862
11863 opcode = ctsio->cdb[0];
11864 entry = &ctl_cmd_table[opcode];
11865
11866 filtered_pattern = entry->pattern & pattern;
11867
11868 /*
11869 * If the user requested specific flags in the pattern (e.g.
11870 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
11871 * flags.
11872 *
11873 * If the user did not specify any flags, it doesn't matter whether
11874 * or not the command supports the flags.
11875 */
11876 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
11877 (pattern & ~CTL_LUN_PAT_MASK))
11878 return (CTL_LUN_PAT_NONE);
11879
11880 /*
11881 * If the user asked for a range check, see if the requested LBA
11882 * range overlaps with this command's LBA range.
11883 */
11884 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
11885 uint64_t lba1;
11886 uint32_t len1;
11887 ctl_action action;
11888 int retval;
11889
11890 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
11891 if (retval != 0)
11892 return (CTL_LUN_PAT_NONE);
11893
11894 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
11895 desc->lba_range.len);
11896 /*
11897 * A "pass" means that the LBA ranges don't overlap, so
11898 * this doesn't match the user's range criteria.
11899 */
11900 if (action == CTL_ACTION_PASS)
11901 return (CTL_LUN_PAT_NONE);
11902 }
11903
11904 return (filtered_pattern);
11905}
11906
11907static void
11908ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
11909{
11910 struct ctl_error_desc *desc, *desc2;
11911
11912 mtx_assert(&control_softc->ctl_lock, MA_OWNED);
11913
11914 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
11915 ctl_lun_error_pattern pattern;
11916 /*
11917 * Check to see whether this particular command matches
11918 * the pattern in the descriptor.
11919 */
11920 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
11921 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
11922 continue;
11923
11924 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
11925 case CTL_LUN_INJ_ABORTED:
11926 ctl_set_aborted(&io->scsiio);
11927 break;
11928 case CTL_LUN_INJ_MEDIUM_ERR:
11929 ctl_set_medium_error(&io->scsiio);
11930 break;
11931 case CTL_LUN_INJ_UA:
11932 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
11933 * OCCURRED */
11934 ctl_set_ua(&io->scsiio, 0x29, 0x00);
11935 break;
11936 case CTL_LUN_INJ_CUSTOM:
11937 /*
11938 * We're assuming the user knows what he is doing.
11939 * Just copy the sense information without doing
11940 * checks.
11941 */
11942 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
11943 ctl_min(sizeof(desc->custom_sense),
11944 sizeof(io->scsiio.sense_data)));
11945 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
11946 io->scsiio.sense_len = SSD_FULL_SIZE;
11947 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11948 break;
11949 case CTL_LUN_INJ_NONE:
11950 default:
11951 /*
11952 * If this is an error injection type we don't know
11953 * about, clear the continuous flag (if it is set)
11954 * so it will get deleted below.
11955 */
11956 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
11957 break;
11958 }
11959 /*
11960 * By default, each error injection action is a one-shot
11961 */
11962 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
11963 continue;
11964
11965 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
11966
11967 free(desc, M_CTL);
11968 }
11969}
11970
11971#ifdef CTL_IO_DELAY
11972static void
11973ctl_datamove_timer_wakeup(void *arg)
11974{
11975 union ctl_io *io;
11976
11977 io = (union ctl_io *)arg;
11978
11979 ctl_datamove(io);
11980}
11981#endif /* CTL_IO_DELAY */
11982
11983void
11984ctl_datamove(union ctl_io *io)
11985{
11986 void (*fe_datamove)(union ctl_io *io);
11987
11988 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
11989
11990 CTL_DEBUG_PRINT(("ctl_datamove\n"));
11991
11992#ifdef CTL_TIME_IO
11993 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
11994 char str[256];
11995 char path_str[64];
11996 struct sbuf sb;
11997
11998 ctl_scsi_path_string(io, path_str, sizeof(path_str));
11999 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12000
12001 sbuf_cat(&sb, path_str);
12002 switch (io->io_hdr.io_type) {
12003 case CTL_IO_SCSI:
12004 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12005 sbuf_printf(&sb, "\n");
12006 sbuf_cat(&sb, path_str);
12007 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12008 io->scsiio.tag_num, io->scsiio.tag_type);
12009 break;
12010 case CTL_IO_TASK:
12011 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12012 "Tag Type: %d\n", io->taskio.task_action,
12013 io->taskio.tag_num, io->taskio.tag_type);
12014 break;
12015 default:
12016 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12017 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12018 break;
12019 }
12020 sbuf_cat(&sb, path_str);
12021 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12022 (intmax_t)time_uptime - io->io_hdr.start_time);
12023 sbuf_finish(&sb);
12024 printf("%s", sbuf_data(&sb));
12025 }
12026#endif /* CTL_TIME_IO */
12027
12028 mtx_lock(&control_softc->ctl_lock);
12029#ifdef CTL_IO_DELAY
12030 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12031 struct ctl_lun *lun;
12032
12033 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12034
12035 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12036 } else {
12037 struct ctl_lun *lun;
12038
12039 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12040 if ((lun != NULL)
12041 && (lun->delay_info.datamove_delay > 0)) {
12042 struct callout *callout;
12043
12044 callout = (struct callout *)&io->io_hdr.timer_bytes;
12045 callout_init(callout, /*mpsafe*/ 1);
12046 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12047 callout_reset(callout,
12048 lun->delay_info.datamove_delay * hz,
12049 ctl_datamove_timer_wakeup, io);
12050 if (lun->delay_info.datamove_type ==
12051 CTL_DELAY_TYPE_ONESHOT)
12052 lun->delay_info.datamove_delay = 0;
12053 mtx_unlock(&control_softc->ctl_lock);
12054 return;
12055 }
12056 }
12057#endif
12058 /*
12059 * If we have any pending task management commands, process them
12060 * first. This is necessary to eliminate a race condition with the
12061 * FETD:
12062 *
12063 * - FETD submits a task management command, like an abort.
12064 * - Back end calls fe_datamove() to move the data for the aborted
12065 * command. The FETD can't really accept it, but if it did, it
12066 * would end up transmitting data for a command that the initiator
12067 * told us to abort.
12068 *
12069 * We close the race by processing all pending task management
12070 * commands here (we can't block!), and then check this I/O to see
12071 * if it has been aborted. If so, return it to the back end with
12072 * bad status, so the back end can say return an error to the back end
12073 * and then when the back end returns an error, we can return the
12074 * aborted command to the FETD, so it can clean up its resources.
12075 */
12076 if (control_softc->flags & CTL_FLAG_TASK_PENDING)
12077 ctl_run_task_queue(control_softc);
12078
12079 /*
12080 * This command has been aborted. Set the port status, so we fail
12081 * the data move.
12082 */
12083 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12084 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n",
12085 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id,
12086 io->io_hdr.nexus.targ_port,
12087 (uintmax_t)io->io_hdr.nexus.targ_target.id,
12088 io->io_hdr.nexus.targ_lun);
12089 io->io_hdr.status = CTL_CMD_ABORTED;
12090 io->io_hdr.port_status = 31337;
12091 mtx_unlock(&control_softc->ctl_lock);
12092 /*
12093 * Note that the backend, in this case, will get the
12094 * callback in its context. In other cases it may get
12095 * called in the frontend's interrupt thread context.
12096 */
12097 io->scsiio.be_move_done(io);
12098 return;
12099 }
12100
12101 /*
12102 * If we're in XFER mode and this I/O is from the other shelf
12103 * controller, we need to send the DMA to the other side to
12104 * actually transfer the data to/from the host. In serialize only
12105 * mode the transfer happens below CTL and ctl_datamove() is only
12106 * called on the machine that originally received the I/O.
12107 */
12108 if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
12109 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12110 union ctl_ha_msg msg;
12111 uint32_t sg_entries_sent;
12112 int do_sg_copy;
12113 int i;
12114
12115 memset(&msg, 0, sizeof(msg));
12116 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
12117 msg.hdr.original_sc = io->io_hdr.original_sc;
12118 msg.hdr.serializing_sc = io;
12119 msg.hdr.nexus = io->io_hdr.nexus;
12120 msg.dt.flags = io->io_hdr.flags;
12121 /*
12122 * We convert everything into a S/G list here. We can't
12123 * pass by reference, only by value between controllers.
12124 * So we can't pass a pointer to the S/G list, only as many
12125 * S/G entries as we can fit in here. If it's possible for
12126 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
12127 * then we need to break this up into multiple transfers.
12128 */
12129 if (io->scsiio.kern_sg_entries == 0) {
12130 msg.dt.kern_sg_entries = 1;
12131 /*
12132 * If this is in cached memory, flush the cache
12133 * before we send the DMA request to the other
12134 * controller. We want to do this in either the
12135 * read or the write case. The read case is
12136 * straightforward. In the write case, we want to
12137 * make sure nothing is in the local cache that
12138 * could overwrite the DMAed data.
12139 */
12140 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12141 /*
12142 * XXX KDM use bus_dmamap_sync() here.
12143 */
12144 }
12145
12146 /*
12147 * Convert to a physical address if this is a
12148 * virtual address.
12149 */
12150 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
12151 msg.dt.sg_list[0].addr =
12152 io->scsiio.kern_data_ptr;
12153 } else {
12154 /*
12155 * XXX KDM use busdma here!
12156 */
12157#if 0
12158 msg.dt.sg_list[0].addr = (void *)
12159 vtophys(io->scsiio.kern_data_ptr);
12160#endif
12161 }
12162
12163 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
12164 do_sg_copy = 0;
12165 } else {
12166 struct ctl_sg_entry *sgl;
12167
12168 do_sg_copy = 1;
12169 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
12170 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
12171 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12172 /*
12173 * XXX KDM use bus_dmamap_sync() here.
12174 */
12175 }
12176 }
12177
12178 msg.dt.kern_data_len = io->scsiio.kern_data_len;
12179 msg.dt.kern_total_len = io->scsiio.kern_total_len;
12180 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
12181 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
12182 msg.dt.sg_sequence = 0;
12183
12184 /*
12185 * Loop until we've sent all of the S/G entries. On the
12186 * other end, we'll recompose these S/G entries into one
12187 * contiguous list before passing it to the
12188 */
12189 for (sg_entries_sent = 0; sg_entries_sent <
12190 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
12191 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/
12192 sizeof(msg.dt.sg_list[0])),
12193 msg.dt.kern_sg_entries - sg_entries_sent);
12194
12195 if (do_sg_copy != 0) {
12196 struct ctl_sg_entry *sgl;
12197 int j;
12198
12199 sgl = (struct ctl_sg_entry *)
12200 io->scsiio.kern_data_ptr;
12201 /*
12202 * If this is in cached memory, flush the cache
12203 * before we send the DMA request to the other
12204 * controller. We want to do this in either
12205 * the * read or the write case. The read
12206 * case is straightforward. In the write
12207 * case, we want to make sure nothing is
12208 * in the local cache that could overwrite
12209 * the DMAed data.
12210 */
12211
12212 for (i = sg_entries_sent, j = 0;
12213 i < msg.dt.cur_sg_entries; i++, j++) {
12214 if ((io->io_hdr.flags &
12215 CTL_FLAG_NO_DATASYNC) == 0) {
12216 /*
12217 * XXX KDM use bus_dmamap_sync()
12218 */
12219 }
12220 if ((io->io_hdr.flags &
12221 CTL_FLAG_BUS_ADDR) == 0) {
12222 /*
12223 * XXX KDM use busdma.
12224 */
12225#if 0
12226 msg.dt.sg_list[j].addr =(void *)
12227 vtophys(sgl[i].addr);
12228#endif
12229 } else {
12230 msg.dt.sg_list[j].addr =
12231 sgl[i].addr;
12232 }
12233 msg.dt.sg_list[j].len = sgl[i].len;
12234 }
12235 }
12236
12237 sg_entries_sent += msg.dt.cur_sg_entries;
12238 if (sg_entries_sent >= msg.dt.kern_sg_entries)
12239 msg.dt.sg_last = 1;
12240 else
12241 msg.dt.sg_last = 0;
12242
12243 /*
12244 * XXX KDM drop and reacquire the lock here?
12245 */
12246 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12247 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
12248 /*
12249 * XXX do something here.
12250 */
12251 }
12252
12253 msg.dt.sent_sg_entries = sg_entries_sent;
12254 }
12255 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12256 if (io->io_hdr.flags & CTL_FLAG_FAILOVER)
12257 ctl_failover_io(io, /*have_lock*/ 1);
12258
12259 } else {
12260
12261 /*
12262 * Lookup the fe_datamove() function for this particular
12263 * front end.
12264 */
12265 fe_datamove =
12266 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12267 mtx_unlock(&control_softc->ctl_lock);
12268
12269 fe_datamove(io);
12270 }
12271}
12272
12273static void
12274ctl_send_datamove_done(union ctl_io *io, int have_lock)
12275{
12276 union ctl_ha_msg msg;
12277 int isc_status;
12278
12279 memset(&msg, 0, sizeof(msg));
12280
12281 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12282 msg.hdr.original_sc = io;
12283 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12284 msg.hdr.nexus = io->io_hdr.nexus;
12285 msg.hdr.status = io->io_hdr.status;
12286 msg.scsi.tag_num = io->scsiio.tag_num;
12287 msg.scsi.tag_type = io->scsiio.tag_type;
12288 msg.scsi.scsi_status = io->scsiio.scsi_status;
12289 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12290 sizeof(io->scsiio.sense_data));
12291 msg.scsi.sense_len = io->scsiio.sense_len;
12292 msg.scsi.sense_residual = io->scsiio.sense_residual;
12293 msg.scsi.fetd_status = io->io_hdr.port_status;
12294 msg.scsi.residual = io->scsiio.residual;
12295 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12296
12297 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12298 ctl_failover_io(io, /*have_lock*/ have_lock);
12299 return;
12300 }
12301
12302 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0);
12303 if (isc_status > CTL_HA_STATUS_SUCCESS) {
12304 /* XXX do something if this fails */
12305 }
12306
12307}
12308
12309/*
12310 * The DMA to the remote side is done, now we need to tell the other side
12311 * we're done so it can continue with its data movement.
12312 */
12313static void
12314ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12315{
12316 union ctl_io *io;
12317
12318 io = rq->context;
12319
12320 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12321 printf("%s: ISC DMA write failed with error %d", __func__,
12322 rq->ret);
12323 ctl_set_internal_failure(&io->scsiio,
12324 /*sks_valid*/ 1,
12325 /*retry_count*/ rq->ret);
12326 }
12327
12328 ctl_dt_req_free(rq);
12329
12330 /*
12331 * In this case, we had to malloc the memory locally. Free it.
12332 */
12333 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12334 int i;
12335 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12336 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12337 }
12338 /*
12339 * The data is in local and remote memory, so now we need to send
12340 * status (good or back) back to the other side.
12341 */
12342 ctl_send_datamove_done(io, /*have_lock*/ 0);
12343}
12344
12345/*
12346 * We've moved the data from the host/controller into local memory. Now we
12347 * need to push it over to the remote controller's memory.
12348 */
12349static int
12350ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12351{
12352 int retval;
12353
12354 retval = 0;
12355
12356 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12357 ctl_datamove_remote_write_cb);
12358
12359 return (retval);
12360}
12361
12362static void
12363ctl_datamove_remote_write(union ctl_io *io)
12364{
12365 int retval;
12366 void (*fe_datamove)(union ctl_io *io);
12367
12368 /*
12369 * - Get the data from the host/HBA into local memory.
12370 * - DMA memory from the local controller to the remote controller.
12371 * - Send status back to the remote controller.
12372 */
12373
12374 retval = ctl_datamove_remote_sgl_setup(io);
12375 if (retval != 0)
12376 return;
12377
12378 /* Switch the pointer over so the FETD knows what to do */
12379 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12380
12381 /*
12382 * Use a custom move done callback, since we need to send completion
12383 * back to the other controller, not to the backend on this side.
12384 */
12385 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12386
12387 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12388
12389 fe_datamove(io);
12390
12391 return;
12392
12393}
12394
12395static int
12396ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12397{
12398#if 0
12399 char str[256];
12400 char path_str[64];
12401 struct sbuf sb;
12402#endif
12403
12404 /*
12405 * In this case, we had to malloc the memory locally. Free it.
12406 */
12407 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12408 int i;
12409 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12410 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12411 }
12412
12413#if 0
12414 scsi_path_string(io, path_str, sizeof(path_str));
12415 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12416 sbuf_cat(&sb, path_str);
12417 scsi_command_string(&io->scsiio, NULL, &sb);
12418 sbuf_printf(&sb, "\n");
12419 sbuf_cat(&sb, path_str);
12420 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12421 io->scsiio.tag_num, io->scsiio.tag_type);
12422 sbuf_cat(&sb, path_str);
12423 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12424 io->io_hdr.flags, io->io_hdr.status);
12425 sbuf_finish(&sb);
12426 printk("%s", sbuf_data(&sb));
12427#endif
12428
12429
12430 /*
12431 * The read is done, now we need to send status (good or bad) back
12432 * to the other side.
12433 */
12434 ctl_send_datamove_done(io, /*have_lock*/ 0);
12435
12436 return (0);
12437}
12438
12439static void
12440ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12441{
12442 union ctl_io *io;
12443 void (*fe_datamove)(union ctl_io *io);
12444
12445 io = rq->context;
12446
12447 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12448 printf("%s: ISC DMA read failed with error %d", __func__,
12449 rq->ret);
12450 ctl_set_internal_failure(&io->scsiio,
12451 /*sks_valid*/ 1,
12452 /*retry_count*/ rq->ret);
12453 }
12454
12455 ctl_dt_req_free(rq);
12456
12457 /* Switch the pointer over so the FETD knows what to do */
12458 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12459
12460 /*
12461 * Use a custom move done callback, since we need to send completion
12462 * back to the other controller, not to the backend on this side.
12463 */
12464 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12465
12466 /* XXX KDM add checks like the ones in ctl_datamove? */
12467
12468 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12469
12470 fe_datamove(io);
12471}
12472
12473static int
12474ctl_datamove_remote_sgl_setup(union ctl_io *io)
12475{
12476 struct ctl_sg_entry *local_sglist, *remote_sglist;
12477 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist;
12478 struct ctl_softc *softc;
12479 int retval;
12480 int i;
12481
12482 retval = 0;
12483 softc = control_softc;
12484
12485 local_sglist = io->io_hdr.local_sglist;
12486 local_dma_sglist = io->io_hdr.local_dma_sglist;
12487 remote_sglist = io->io_hdr.remote_sglist;
12488 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
12489
12490 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) {
12491 for (i = 0; i < io->scsiio.kern_sg_entries; i++) {
12492 local_sglist[i].len = remote_sglist[i].len;
12493
12494 /*
12495 * XXX Detect the situation where the RS-level I/O
12496 * redirector on the other side has already read the
12497 * data off of the AOR RS on this side, and
12498 * transferred it to remote (mirror) memory on the
12499 * other side. Since we already have the data in
12500 * memory here, we just need to use it.
12501 *
12502 * XXX KDM this can probably be removed once we
12503 * get the cache device code in and take the
12504 * current AOR implementation out.
12505 */
12506#ifdef NEEDTOPORT
12507 if ((remote_sglist[i].addr >=
12508 (void *)vtophys(softc->mirr->addr))
12509 && (remote_sglist[i].addr <
12510 ((void *)vtophys(softc->mirr->addr) +
12511 CacheMirrorOffset))) {
12512 local_sglist[i].addr = remote_sglist[i].addr -
12513 CacheMirrorOffset;
12514 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12515 CTL_FLAG_DATA_IN)
12516 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE;
12517 } else {
12518 local_sglist[i].addr = remote_sglist[i].addr +
12519 CacheMirrorOffset;
12520 }
12521#endif
12522#if 0
12523 printf("%s: local %p, remote %p, len %d\n",
12524 __func__, local_sglist[i].addr,
12525 remote_sglist[i].addr, local_sglist[i].len);
12526#endif
12527 }
12528 } else {
12529 uint32_t len_to_go;
12530
12531 /*
12532 * In this case, we don't have automatically allocated
12533 * memory for this I/O on this controller. This typically
12534 * happens with internal CTL I/O -- e.g. inquiry, mode
12535 * sense, etc. Anything coming from RAIDCore will have
12536 * a mirror area available.
12537 */
12538 len_to_go = io->scsiio.kern_data_len;
12539
12540 /*
12541 * Clear the no datasync flag, we have to use malloced
12542 * buffers.
12543 */
12544 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC;
12545
12546 /*
12547 * The difficult thing here is that the size of the various
12548 * S/G segments may be different than the size from the
12549 * remote controller. That'll make it harder when DMAing
12550 * the data back to the other side.
12551 */
12552 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) /
12553 sizeof(io->io_hdr.remote_sglist[0])) &&
12554 (len_to_go > 0); i++) {
12555 local_sglist[i].len = ctl_min(len_to_go, 131072);
12556 CTL_SIZE_8B(local_dma_sglist[i].len,
12557 local_sglist[i].len);
12558 local_sglist[i].addr =
12559 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK);
12560
12561 local_dma_sglist[i].addr = local_sglist[i].addr;
12562
12563 if (local_sglist[i].addr == NULL) {
12564 int j;
12565
12566 printf("malloc failed for %zd bytes!",
12567 local_dma_sglist[i].len);
12568 for (j = 0; j < i; j++) {
12569 free(local_sglist[j].addr, M_CTL);
12570 }
12571 ctl_set_internal_failure(&io->scsiio,
12572 /*sks_valid*/ 1,
12573 /*retry_count*/ 4857);
12574 retval = 1;
12575 goto bailout_error;
12576
12577 }
12578 /* XXX KDM do we need a sync here? */
12579
12580 len_to_go -= local_sglist[i].len;
12581 }
12582 /*
12583 * Reset the number of S/G entries accordingly. The
12584 * original number of S/G entries is available in
12585 * rem_sg_entries.
12586 */
12587 io->scsiio.kern_sg_entries = i;
12588
12589#if 0
12590 printf("%s: kern_sg_entries = %d\n", __func__,
12591 io->scsiio.kern_sg_entries);
12592 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12593 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i,
12594 local_sglist[i].addr, local_sglist[i].len,
12595 local_dma_sglist[i].len);
12596#endif
12597 }
12598
12599
12600 return (retval);
12601
12602bailout_error:
12603
12604 ctl_send_datamove_done(io, /*have_lock*/ 0);
12605
12606 return (retval);
12607}
12608
12609static int
12610ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12611 ctl_ha_dt_cb callback)
12612{
12613 struct ctl_ha_dt_req *rq;
12614 struct ctl_sg_entry *remote_sglist, *local_sglist;
12615 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist;
12616 uint32_t local_used, remote_used, total_used;
12617 int retval;
12618 int i, j;
12619
12620 retval = 0;
12621
12622 rq = ctl_dt_req_alloc();
12623
12624 /*
12625 * If we failed to allocate the request, and if the DMA didn't fail
12626 * anyway, set busy status. This is just a resource allocation
12627 * failure.
12628 */
12629 if ((rq == NULL)
12630 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
12631 ctl_set_busy(&io->scsiio);
12632
12633 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
12634
12635 if (rq != NULL)
12636 ctl_dt_req_free(rq);
12637
12638 /*
12639 * The data move failed. We need to return status back
12640 * to the other controller. No point in trying to DMA
12641 * data to the remote controller.
12642 */
12643
12644 ctl_send_datamove_done(io, /*have_lock*/ 0);
12645
12646 retval = 1;
12647
12648 goto bailout;
12649 }
12650
12651 local_sglist = io->io_hdr.local_sglist;
12652 local_dma_sglist = io->io_hdr.local_dma_sglist;
12653 remote_sglist = io->io_hdr.remote_sglist;
12654 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
12655 local_used = 0;
12656 remote_used = 0;
12657 total_used = 0;
12658
12659 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) {
12660 rq->ret = CTL_HA_STATUS_SUCCESS;
12661 rq->context = io;
12662 callback(rq);
12663 goto bailout;
12664 }
12665
12666 /*
12667 * Pull/push the data over the wire from/to the other controller.
12668 * This takes into account the possibility that the local and
12669 * remote sglists may not be identical in terms of the size of
12670 * the elements and the number of elements.
12671 *
12672 * One fundamental assumption here is that the length allocated for
12673 * both the local and remote sglists is identical. Otherwise, we've
12674 * essentially got a coding error of some sort.
12675 */
12676 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12677 int isc_ret;
12678 uint32_t cur_len, dma_length;
12679 uint8_t *tmp_ptr;
12680
12681 rq->id = CTL_HA_DATA_CTL;
12682 rq->command = command;
12683 rq->context = io;
12684
12685 /*
12686 * Both pointers should be aligned. But it is possible
12687 * that the allocation length is not. They should both
12688 * also have enough slack left over at the end, though,
12689 * to round up to the next 8 byte boundary.
12690 */
12691 cur_len = ctl_min(local_sglist[i].len - local_used,
12692 remote_sglist[j].len - remote_used);
12693
12694 /*
12695 * In this case, we have a size issue and need to decrease
12696 * the size, except in the case where we actually have less
12697 * than 8 bytes left. In that case, we need to increase
12698 * the DMA length to get the last bit.
12699 */
12700 if ((cur_len & 0x7) != 0) {
12701 if (cur_len > 0x7) {
12702 cur_len = cur_len - (cur_len & 0x7);
12703 dma_length = cur_len;
12704 } else {
12705 CTL_SIZE_8B(dma_length, cur_len);
12706 }
12707
12708 } else
12709 dma_length = cur_len;
12710
12711 /*
12712 * If we had to allocate memory for this I/O, instead of using
12713 * the non-cached mirror memory, we'll need to flush the cache
12714 * before trying to DMA to the other controller.
12715 *
12716 * We could end up doing this multiple times for the same
12717 * segment if we have a larger local segment than remote
12718 * segment. That shouldn't be an issue.
12719 */
12720 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12721 /*
12722 * XXX KDM use bus_dmamap_sync() here.
12723 */
12724 }
12725
12726 rq->size = dma_length;
12727
12728 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12729 tmp_ptr += local_used;
12730
12731 /* Use physical addresses when talking to ISC hardware */
12732 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12733 /* XXX KDM use busdma */
12734#if 0
12735 rq->local = vtophys(tmp_ptr);
12736#endif
12737 } else
12738 rq->local = tmp_ptr;
12739
12740 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12741 tmp_ptr += remote_used;
12742 rq->remote = tmp_ptr;
12743
12744 rq->callback = NULL;
12745
12746 local_used += cur_len;
12747 if (local_used >= local_sglist[i].len) {
12748 i++;
12749 local_used = 0;
12750 }
12751
12752 remote_used += cur_len;
12753 if (remote_used >= remote_sglist[j].len) {
12754 j++;
12755 remote_used = 0;
12756 }
12757 total_used += cur_len;
12758
12759 if (total_used >= io->scsiio.kern_data_len)
12760 rq->callback = callback;
12761
12762 if ((rq->size & 0x7) != 0) {
12763 printf("%s: warning: size %d is not on 8b boundary\n",
12764 __func__, rq->size);
12765 }
12766 if (((uintptr_t)rq->local & 0x7) != 0) {
12767 printf("%s: warning: local %p not on 8b boundary\n",
12768 __func__, rq->local);
12769 }
12770 if (((uintptr_t)rq->remote & 0x7) != 0) {
12771 printf("%s: warning: remote %p not on 8b boundary\n",
12772 __func__, rq->local);
12773 }
12774#if 0
12775 printf("%s: %s: local %#x remote %#x size %d\n", __func__,
12776 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
12777 rq->local, rq->remote, rq->size);
12778#endif
12779
12780 isc_ret = ctl_dt_single(rq);
12781 if (isc_ret == CTL_HA_STATUS_WAIT)
12782 continue;
12783
12784 if (isc_ret == CTL_HA_STATUS_DISCONNECT) {
12785 rq->ret = CTL_HA_STATUS_SUCCESS;
12786 } else {
12787 rq->ret = isc_ret;
12788 }
12789 callback(rq);
12790 goto bailout;
12791 }
12792
12793bailout:
12794 return (retval);
12795
12796}
12797
12798static void
12799ctl_datamove_remote_read(union ctl_io *io)
12800{
12801 int retval;
12802 int i;
12803
12804 /*
12805 * This will send an error to the other controller in the case of a
12806 * failure.
12807 */
12808 retval = ctl_datamove_remote_sgl_setup(io);
12809 if (retval != 0)
12810 return;
12811
12812 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12813 ctl_datamove_remote_read_cb);
12814 if ((retval != 0)
12815 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) {
12816 /*
12817 * Make sure we free memory if there was an error.. The
12818 * ctl_datamove_remote_xfer() function will send the
12819 * datamove done message, or call the callback with an
12820 * error if there is a problem.
12821 */
12822 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12823 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12824 }
12825
12826 return;
12827}
12828
12829/*
12830 * Process a datamove request from the other controller. This is used for
12831 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12832 * first. Once that is complete, the data gets DMAed into the remote
12833 * controller's memory. For reads, we DMA from the remote controller's
12834 * memory into our memory first, and then move it out to the FETD.
12835 */
12836static void
12837ctl_datamove_remote(union ctl_io *io)
12838{
12839 struct ctl_softc *softc;
12840
12841 softc = control_softc;
12842
12843 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
12844
12845 /*
12846 * Note that we look for an aborted I/O here, but don't do some of
12847 * the other checks that ctl_datamove() normally does. We don't
12848 * need to run the task queue, because this I/O is on the ISC
12849 * queue, which is executed by the work thread after the task queue.
12850 * We don't need to run the datamove delay code, since that should
12851 * have been done if need be on the other controller.
12852 */
12853 mtx_lock(&softc->ctl_lock);
12854
12855 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12856
12857 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__,
12858 io->scsiio.tag_num, io->io_hdr.nexus.initid.id,
12859 io->io_hdr.nexus.targ_port,
12860 io->io_hdr.nexus.targ_target.id,
12861 io->io_hdr.nexus.targ_lun);
12862 io->io_hdr.status = CTL_CMD_ABORTED;
12863 io->io_hdr.port_status = 31338;
12864
12865 mtx_unlock(&softc->ctl_lock);
12866
12867 ctl_send_datamove_done(io, /*have_lock*/ 0);
12868
12869 return;
12870 }
12871
12872 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) {
12873 mtx_unlock(&softc->ctl_lock);
12874 ctl_datamove_remote_write(io);
12875 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){
12876 mtx_unlock(&softc->ctl_lock);
12877 ctl_datamove_remote_read(io);
12878 } else {
12879 union ctl_ha_msg msg;
12880 struct scsi_sense_data *sense;
12881 uint8_t sks[3];
12882 int retry_count;
12883
12884 memset(&msg, 0, sizeof(msg));
12885
12886 msg.hdr.msg_type = CTL_MSG_BAD_JUJU;
12887 msg.hdr.status = CTL_SCSI_ERROR;
12888 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
12889
12890 retry_count = 4243;
12891
12892 sense = &msg.scsi.sense_data;
12893 sks[0] = SSD_SCS_VALID;
12894 sks[1] = (retry_count >> 8) & 0xff;
12895 sks[2] = retry_count & 0xff;
12896
12897 /* "Internal target failure" */
12898 scsi_set_sense_data(sense,
12899 /*sense_format*/ SSD_TYPE_NONE,
12900 /*current_error*/ 1,
12901 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
12902 /*asc*/ 0x44,
12903 /*ascq*/ 0x00,
12904 /*type*/ SSD_ELEM_SKS,
12905 /*size*/ sizeof(sks),
12906 /*data*/ sks,
12907 SSD_ELEM_NONE);
12908
12909 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12910 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12911 ctl_failover_io(io, /*have_lock*/ 1);
12912 mtx_unlock(&softc->ctl_lock);
12913 return;
12914 }
12915
12916 mtx_unlock(&softc->ctl_lock);
12917
12918 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) >
12919 CTL_HA_STATUS_SUCCESS) {
12920 /* XXX KDM what to do if this fails? */
12921 }
12922 return;
12923 }
12924
12925}
12926
12927static int
12928ctl_process_done(union ctl_io *io, int have_lock)
12929{
12930 struct ctl_lun *lun;
12931 struct ctl_softc *ctl_softc;
12932 void (*fe_done)(union ctl_io *io);
12933 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port);
12934
12935 CTL_DEBUG_PRINT(("ctl_process_done\n"));
12936
12937 fe_done =
12938 control_softc->ctl_ports[targ_port]->fe_done;
12939
12940#ifdef CTL_TIME_IO
12941 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12942 char str[256];
12943 char path_str[64];
12944 struct sbuf sb;
12945
12946 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12947 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12948
12949 sbuf_cat(&sb, path_str);
12950 switch (io->io_hdr.io_type) {
12951 case CTL_IO_SCSI:
12952 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12953 sbuf_printf(&sb, "\n");
12954 sbuf_cat(&sb, path_str);
12955 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12956 io->scsiio.tag_num, io->scsiio.tag_type);
12957 break;
12958 case CTL_IO_TASK:
12959 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12960 "Tag Type: %d\n", io->taskio.task_action,
12961 io->taskio.tag_num, io->taskio.tag_type);
12962 break;
12963 default:
12964 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12965 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12966 break;
12967 }
12968 sbuf_cat(&sb, path_str);
12969 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12970 (intmax_t)time_uptime - io->io_hdr.start_time);
12971 sbuf_finish(&sb);
12972 printf("%s", sbuf_data(&sb));
12973 }
12974#endif /* CTL_TIME_IO */
12975
12976 switch (io->io_hdr.io_type) {
12977 case CTL_IO_SCSI:
12978 break;
12979 case CTL_IO_TASK:
12980 if (bootverbose || verbose > 0)
12981 ctl_io_error_print(io, NULL);
12982 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
12983 ctl_free_io(io);
12984 else
12985 fe_done(io);
12986 return (CTL_RETVAL_COMPLETE);
12987 break;
12988 default:
12989 printf("ctl_process_done: invalid io type %d\n",
12990 io->io_hdr.io_type);
12991 panic("ctl_process_done: invalid io type %d\n",
12992 io->io_hdr.io_type);
12993 break; /* NOTREACHED */
12994 }
12995
12996 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12997 if (lun == NULL) {
12998 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
12999 io->io_hdr.nexus.targ_lun));
13000 fe_done(io);
13001 goto bailout;
13002 }
13003 ctl_softc = lun->ctl_softc;
13004
13005 /*
13006 * Remove this from the OOA queue.
13007 */
13008 if (have_lock == 0)
13009 mtx_lock(&ctl_softc->ctl_lock);
13010
13011 /*
13012 * Check to see if we have any errors to inject here. We only
13013 * inject errors for commands that don't already have errors set.
13014 */
13015 if ((STAILQ_FIRST(&lun->error_list) != NULL)
13016 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))
13017 ctl_inject_error(lun, io);
13018
13019 /*
13020 * XXX KDM how do we treat commands that aren't completed
13021 * successfully?
13022 *
13023 * XXX KDM should we also track I/O latency?
13024 */
13025 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
13026 uint32_t blocksize;
13027#ifdef CTL_TIME_IO
13028 struct bintime cur_bt;
13029#endif
13030
13031 if ((lun->be_lun != NULL)
13032 && (lun->be_lun->blocksize != 0))
13033 blocksize = lun->be_lun->blocksize;
13034 else
13035 blocksize = 512;
13036
13037 switch (io->io_hdr.io_type) {
13038 case CTL_IO_SCSI: {
13039 int isread;
12756 struct ctl_lba_len *lbalen;
13040 struct ctl_lba_len_flags *lbalen;
12757
12758 isread = 0;
12759 switch (io->scsiio.cdb[0]) {
12760 case READ_6:
12761 case READ_10:
12762 case READ_12:
12763 case READ_16:
12764 isread = 1;
12765 /* FALLTHROUGH */
12766 case WRITE_6:
12767 case WRITE_10:
12768 case WRITE_12:
12769 case WRITE_16:
12770 case WRITE_VERIFY_10:
12771 case WRITE_VERIFY_12:
12772 case WRITE_VERIFY_16:
13041
13042 isread = 0;
13043 switch (io->scsiio.cdb[0]) {
13044 case READ_6:
13045 case READ_10:
13046 case READ_12:
13047 case READ_16:
13048 isread = 1;
13049 /* FALLTHROUGH */
13050 case WRITE_6:
13051 case WRITE_10:
13052 case WRITE_12:
13053 case WRITE_16:
13054 case WRITE_VERIFY_10:
13055 case WRITE_VERIFY_12:
13056 case WRITE_VERIFY_16:
12773 lbalen = (struct ctl_lba_len *)
13057 lbalen = (struct ctl_lba_len_flags *)
12774 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
12775
12776 if (isread) {
12777 lun->stats.ports[targ_port].bytes[CTL_STATS_READ] +=
12778 lbalen->len * blocksize;
12779 lun->stats.ports[targ_port].operations[CTL_STATS_READ]++;
12780
12781#ifdef CTL_TIME_IO
12782 bintime_add(
12783 &lun->stats.ports[targ_port].dma_time[CTL_STATS_READ],
12784 &io->io_hdr.dma_bt);
12785 lun->stats.ports[targ_port].num_dmas[CTL_STATS_READ] +=
12786 io->io_hdr.num_dmas;
12787 getbintime(&cur_bt);
12788 bintime_sub(&cur_bt,
12789 &io->io_hdr.start_bt);
12790
12791 bintime_add(
12792 &lun->stats.ports[targ_port].time[CTL_STATS_READ],
12793 &cur_bt);
12794
12795#if 0
12796 cs_prof_gettime(&cur_ticks);
12797 lun->stats.time[CTL_STATS_READ] +=
12798 cur_ticks -
12799 io->io_hdr.start_ticks;
12800#endif
12801#if 0
12802 lun->stats.time[CTL_STATS_READ] +=
12803 jiffies - io->io_hdr.start_time;
12804#endif
12805#endif /* CTL_TIME_IO */
12806 } else {
12807 lun->stats.ports[targ_port].bytes[CTL_STATS_WRITE] +=
12808 lbalen->len * blocksize;
12809 lun->stats.ports[targ_port].operations[
12810 CTL_STATS_WRITE]++;
12811
12812#ifdef CTL_TIME_IO
12813 bintime_add(
12814 &lun->stats.ports[targ_port].dma_time[CTL_STATS_WRITE],
12815 &io->io_hdr.dma_bt);
12816 lun->stats.ports[targ_port].num_dmas[CTL_STATS_WRITE] +=
12817 io->io_hdr.num_dmas;
12818 getbintime(&cur_bt);
12819 bintime_sub(&cur_bt,
12820 &io->io_hdr.start_bt);
12821
12822 bintime_add(
12823 &lun->stats.ports[targ_port].time[CTL_STATS_WRITE],
12824 &cur_bt);
12825#if 0
12826 cs_prof_gettime(&cur_ticks);
12827 lun->stats.ports[targ_port].time[CTL_STATS_WRITE] +=
12828 cur_ticks -
12829 io->io_hdr.start_ticks;
12830 lun->stats.ports[targ_port].time[CTL_STATS_WRITE] +=
12831 jiffies - io->io_hdr.start_time;
12832#endif
12833#endif /* CTL_TIME_IO */
12834 }
12835 break;
12836 default:
12837 lun->stats.ports[targ_port].operations[CTL_STATS_NO_IO]++;
12838
12839#ifdef CTL_TIME_IO
12840 bintime_add(
12841 &lun->stats.ports[targ_port].dma_time[CTL_STATS_NO_IO],
12842 &io->io_hdr.dma_bt);
12843 lun->stats.ports[targ_port].num_dmas[CTL_STATS_NO_IO] +=
12844 io->io_hdr.num_dmas;
12845 getbintime(&cur_bt);
12846 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
12847
12848 bintime_add(&lun->stats.ports[targ_port].time[CTL_STATS_NO_IO],
12849 &cur_bt);
12850
12851#if 0
12852 cs_prof_gettime(&cur_ticks);
12853 lun->stats.ports[targ_port].time[CTL_STATS_NO_IO] +=
12854 cur_ticks -
12855 io->io_hdr.start_ticks;
12856 lun->stats.ports[targ_port].time[CTL_STATS_NO_IO] +=
12857 jiffies - io->io_hdr.start_time;
12858#endif
12859#endif /* CTL_TIME_IO */
12860 break;
12861 }
12862 break;
12863 }
12864 default:
12865 break;
12866 }
12867 }
12868
12869 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12870
12871 /*
12872 * Run through the blocked queue on this LUN and see if anything
12873 * has become unblocked, now that this transaction is done.
12874 */
12875 ctl_check_blocked(lun);
12876
12877 /*
12878 * If the LUN has been invalidated, free it if there is nothing
12879 * left on its OOA queue.
12880 */
12881 if ((lun->flags & CTL_LUN_INVALID)
12882 && (TAILQ_FIRST(&lun->ooa_queue) == NULL))
12883 ctl_free_lun(lun);
12884
12885 /*
12886 * If this command has been aborted, make sure we set the status
12887 * properly. The FETD is responsible for freeing the I/O and doing
12888 * whatever it needs to do to clean up its state.
12889 */
12890 if (io->io_hdr.flags & CTL_FLAG_ABORT)
12891 io->io_hdr.status = CTL_CMD_ABORTED;
12892
12893 /*
12894 * We print out status for every task management command. For SCSI
12895 * commands, we filter out any unit attention errors; they happen
12896 * on every boot, and would clutter up the log. Note: task
12897 * management commands aren't printed here, they are printed above,
12898 * since they should never even make it down here.
12899 */
12900 switch (io->io_hdr.io_type) {
12901 case CTL_IO_SCSI: {
12902 int error_code, sense_key, asc, ascq;
12903
12904 sense_key = 0;
12905
12906 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR)
12907 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) {
12908 /*
12909 * Since this is just for printing, no need to
12910 * show errors here.
12911 */
12912 scsi_extract_sense_len(&io->scsiio.sense_data,
12913 io->scsiio.sense_len,
12914 &error_code,
12915 &sense_key,
12916 &asc,
12917 &ascq,
12918 /*show_errors*/ 0);
12919 }
12920
12921 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
12922 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR)
12923 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND)
12924 || (sense_key != SSD_KEY_UNIT_ATTENTION))) {
12925
12926 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){
12927 ctl_softc->skipped_prints++;
12928 if (have_lock == 0)
12929 mtx_unlock(&ctl_softc->ctl_lock);
12930 } else {
12931 uint32_t skipped_prints;
12932
12933 skipped_prints = ctl_softc->skipped_prints;
12934
12935 ctl_softc->skipped_prints = 0;
12936 ctl_softc->last_print_jiffies = time_uptime;
12937
12938 if (have_lock == 0)
12939 mtx_unlock(&ctl_softc->ctl_lock);
12940 if (skipped_prints > 0) {
12941#ifdef NEEDTOPORT
12942 csevent_log(CSC_CTL | CSC_SHELF_SW |
12943 CTL_ERROR_REPORT,
12944 csevent_LogType_Trace,
12945 csevent_Severity_Information,
12946 csevent_AlertLevel_Green,
12947 csevent_FRU_Firmware,
12948 csevent_FRU_Unknown,
12949 "High CTL error volume, %d prints "
12950 "skipped", skipped_prints);
12951#endif
12952 }
12953 if (bootverbose || verbose > 0)
12954 ctl_io_error_print(io, NULL);
12955 }
12956 } else {
12957 if (have_lock == 0)
12958 mtx_unlock(&ctl_softc->ctl_lock);
12959 }
12960 break;
12961 }
12962 case CTL_IO_TASK:
12963 if (have_lock == 0)
12964 mtx_unlock(&ctl_softc->ctl_lock);
12965 if (bootverbose || verbose > 0)
12966 ctl_io_error_print(io, NULL);
12967 break;
12968 default:
12969 if (have_lock == 0)
12970 mtx_unlock(&ctl_softc->ctl_lock);
12971 break;
12972 }
12973
12974 /*
12975 * Tell the FETD or the other shelf controller we're done with this
12976 * command. Note that only SCSI commands get to this point. Task
12977 * management commands are completed above.
12978 *
12979 * We only send status to the other controller if we're in XFER
12980 * mode. In SER_ONLY mode, the I/O is done on the controller that
12981 * received the I/O (from CTL's perspective), and so the status is
12982 * generated there.
12983 *
12984 * XXX KDM if we hold the lock here, we could cause a deadlock
12985 * if the frontend comes back in in this context to queue
12986 * something.
12987 */
12988 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER)
12989 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12990 union ctl_ha_msg msg;
12991
12992 memset(&msg, 0, sizeof(msg));
12993 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
12994 msg.hdr.original_sc = io->io_hdr.original_sc;
12995 msg.hdr.nexus = io->io_hdr.nexus;
12996 msg.hdr.status = io->io_hdr.status;
12997 msg.scsi.scsi_status = io->scsiio.scsi_status;
12998 msg.scsi.tag_num = io->scsiio.tag_num;
12999 msg.scsi.tag_type = io->scsiio.tag_type;
13000 msg.scsi.sense_len = io->scsiio.sense_len;
13001 msg.scsi.sense_residual = io->scsiio.sense_residual;
13002 msg.scsi.residual = io->scsiio.residual;
13003 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
13004 sizeof(io->scsiio.sense_data));
13005 /*
13006 * We copy this whether or not this is an I/O-related
13007 * command. Otherwise, we'd have to go and check to see
13008 * whether it's a read/write command, and it really isn't
13009 * worth it.
13010 */
13011 memcpy(&msg.scsi.lbalen,
13012 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
13013 sizeof(msg.scsi.lbalen));
13014
13015 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13016 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
13017 /* XXX do something here */
13018 }
13019
13020 ctl_free_io(io);
13021 } else
13022 fe_done(io);
13023
13024bailout:
13025
13026 return (CTL_RETVAL_COMPLETE);
13027}
13028
13029/*
13030 * Front end should call this if it doesn't do autosense. When the request
13031 * sense comes back in from the initiator, we'll dequeue this and send it.
13032 */
13033int
13034ctl_queue_sense(union ctl_io *io)
13035{
13036 struct ctl_lun *lun;
13037 struct ctl_softc *ctl_softc;
13038 uint32_t initidx, targ_lun;
13039
13040 ctl_softc = control_softc;
13041
13042 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13043
13044 /*
13045 * LUN lookup will likely move to the ctl_work_thread() once we
13046 * have our new queueing infrastructure (that doesn't put things on
13047 * a per-LUN queue initially). That is so that we can handle
13048 * things like an INQUIRY to a LUN that we don't have enabled. We
13049 * can't deal with that right now.
13050 */
13051 mtx_lock(&ctl_softc->ctl_lock);
13052
13053 /*
13054 * If we don't have a LUN for this, just toss the sense
13055 * information.
13056 */
13057 targ_lun = io->io_hdr.nexus.targ_lun;
13058 if (io->io_hdr.nexus.lun_map_fn != NULL)
13059 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
13060 if ((targ_lun < CTL_MAX_LUNS)
13061 && (ctl_softc->ctl_luns[targ_lun] != NULL))
13062 lun = ctl_softc->ctl_luns[targ_lun];
13063 else
13064 goto bailout;
13065
13066 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13067
13068 /*
13069 * Already have CA set for this LUN...toss the sense information.
13070 */
13071 if (ctl_is_set(lun->have_ca, initidx))
13072 goto bailout;
13073
13074 memcpy(&lun->pending_sense[initidx].sense, &io->scsiio.sense_data,
13075 ctl_min(sizeof(lun->pending_sense[initidx].sense),
13076 sizeof(io->scsiio.sense_data)));
13077 ctl_set_mask(lun->have_ca, initidx);
13078
13079bailout:
13080 mtx_unlock(&ctl_softc->ctl_lock);
13081
13082 ctl_free_io(io);
13083
13084 return (CTL_RETVAL_COMPLETE);
13085}
13086
13087/*
13088 * Primary command inlet from frontend ports. All SCSI and task I/O
13089 * requests must go through this function.
13090 */
13091int
13092ctl_queue(union ctl_io *io)
13093{
13094 struct ctl_softc *ctl_softc;
13095
13096 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13097
13098 ctl_softc = control_softc;
13099
13100#ifdef CTL_TIME_IO
13101 io->io_hdr.start_time = time_uptime;
13102 getbintime(&io->io_hdr.start_bt);
13103#endif /* CTL_TIME_IO */
13104
13105 mtx_lock(&ctl_softc->ctl_lock);
13106
13107 switch (io->io_hdr.io_type) {
13108 case CTL_IO_SCSI:
13109 STAILQ_INSERT_TAIL(&ctl_softc->incoming_queue, &io->io_hdr,
13110 links);
13111 break;
13112 case CTL_IO_TASK:
13113 STAILQ_INSERT_TAIL(&ctl_softc->task_queue, &io->io_hdr, links);
13114 /*
13115 * Set the task pending flag. This is necessary to close a
13116 * race condition with the FETD:
13117 *
13118 * - FETD submits a task management command, like an abort.
13119 * - Back end calls fe_datamove() to move the data for the
13120 * aborted command. The FETD can't really accept it, but
13121 * if it did, it would end up transmitting data for a
13122 * command that the initiator told us to abort.
13123 *
13124 * We close the race condition by setting the flag here,
13125 * and checking it in ctl_datamove(), before calling the
13126 * FETD's fe_datamove routine. If we've got a task
13127 * pending, we run the task queue and then check to see
13128 * whether our particular I/O has been aborted.
13129 */
13130 ctl_softc->flags |= CTL_FLAG_TASK_PENDING;
13131 break;
13132 default:
13133 mtx_unlock(&ctl_softc->ctl_lock);
13134 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13135 return (-EINVAL);
13136 break; /* NOTREACHED */
13137 }
13138 mtx_unlock(&ctl_softc->ctl_lock);
13139
13140 ctl_wakeup_thread();
13141
13142 return (CTL_RETVAL_COMPLETE);
13143}
13144
13145#ifdef CTL_IO_DELAY
13146static void
13147ctl_done_timer_wakeup(void *arg)
13148{
13149 union ctl_io *io;
13150
13151 io = (union ctl_io *)arg;
13152 ctl_done_lock(io, /*have_lock*/ 0);
13153}
13154#endif /* CTL_IO_DELAY */
13155
13156void
13157ctl_done_lock(union ctl_io *io, int have_lock)
13158{
13159 struct ctl_softc *ctl_softc;
13160#ifndef CTL_DONE_THREAD
13161 union ctl_io *xio;
13162#endif /* !CTL_DONE_THREAD */
13163
13164 ctl_softc = control_softc;
13165
13166 if (have_lock == 0)
13167 mtx_lock(&ctl_softc->ctl_lock);
13168
13169 /*
13170 * Enable this to catch duplicate completion issues.
13171 */
13172#if 0
13173 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13174 printf("%s: type %d msg %d cdb %x iptl: "
13175 "%d:%d:%d:%d tag 0x%04x "
13176 "flag %#x status %x\n",
13177 __func__,
13178 io->io_hdr.io_type,
13179 io->io_hdr.msg_type,
13180 io->scsiio.cdb[0],
13181 io->io_hdr.nexus.initid.id,
13182 io->io_hdr.nexus.targ_port,
13183 io->io_hdr.nexus.targ_target.id,
13184 io->io_hdr.nexus.targ_lun,
13185 (io->io_hdr.io_type ==
13186 CTL_IO_TASK) ?
13187 io->taskio.tag_num :
13188 io->scsiio.tag_num,
13189 io->io_hdr.flags,
13190 io->io_hdr.status);
13191 } else
13192 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13193#endif
13194
13195 /*
13196 * This is an internal copy of an I/O, and should not go through
13197 * the normal done processing logic.
13198 */
13199 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) {
13200 if (have_lock == 0)
13201 mtx_unlock(&ctl_softc->ctl_lock);
13202 return;
13203 }
13204
13205 /*
13206 * We need to send a msg to the serializing shelf to finish the IO
13207 * as well. We don't send a finish message to the other shelf if
13208 * this is a task management command. Task management commands
13209 * aren't serialized in the OOA queue, but rather just executed on
13210 * both shelf controllers for commands that originated on that
13211 * controller.
13212 */
13213 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)
13214 && (io->io_hdr.io_type != CTL_IO_TASK)) {
13215 union ctl_ha_msg msg_io;
13216
13217 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO;
13218 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc;
13219 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io,
13220 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) {
13221 }
13222 /* continue on to finish IO */
13223 }
13224#ifdef CTL_IO_DELAY
13225 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13226 struct ctl_lun *lun;
13227
13228 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13229
13230 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13231 } else {
13232 struct ctl_lun *lun;
13233
13234 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13235
13236 if ((lun != NULL)
13237 && (lun->delay_info.done_delay > 0)) {
13238 struct callout *callout;
13239
13240 callout = (struct callout *)&io->io_hdr.timer_bytes;
13241 callout_init(callout, /*mpsafe*/ 1);
13242 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13243 callout_reset(callout,
13244 lun->delay_info.done_delay * hz,
13245 ctl_done_timer_wakeup, io);
13246 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13247 lun->delay_info.done_delay = 0;
13248 if (have_lock == 0)
13249 mtx_unlock(&ctl_softc->ctl_lock);
13250 return;
13251 }
13252 }
13253#endif /* CTL_IO_DELAY */
13254
13255 STAILQ_INSERT_TAIL(&ctl_softc->done_queue, &io->io_hdr, links);
13256
13257#ifdef CTL_DONE_THREAD
13258 if (have_lock == 0)
13259 mtx_unlock(&ctl_softc->ctl_lock);
13260
13261 ctl_wakeup_thread();
13262#else /* CTL_DONE_THREAD */
13263 for (xio = (union ctl_io *)STAILQ_FIRST(&ctl_softc->done_queue);
13264 xio != NULL;
13265 xio =(union ctl_io *)STAILQ_FIRST(&ctl_softc->done_queue)) {
13266
13267 STAILQ_REMOVE_HEAD(&ctl_softc->done_queue, links);
13268
13269 ctl_process_done(xio, /*have_lock*/ 1);
13270 }
13271 if (have_lock == 0)
13272 mtx_unlock(&ctl_softc->ctl_lock);
13273#endif /* CTL_DONE_THREAD */
13274}
13275
13276void
13277ctl_done(union ctl_io *io)
13278{
13279 ctl_done_lock(io, /*have_lock*/ 0);
13280}
13281
13282int
13283ctl_isc(struct ctl_scsiio *ctsio)
13284{
13285 struct ctl_lun *lun;
13286 int retval;
13287
13288 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13289
13290 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0]));
13291
13292 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n"));
13293
13294 retval = lun->backend->data_submit((union ctl_io *)ctsio);
13295
13296 return (retval);
13297}
13298
13299
13300static void
13301ctl_work_thread(void *arg)
13302{
13303 struct ctl_softc *softc;
13304 union ctl_io *io;
13305 struct ctl_be_lun *be_lun;
13306 int retval;
13307
13308 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13309
13310 softc = (struct ctl_softc *)arg;
13311 if (softc == NULL)
13312 return;
13313
13314 mtx_lock(&softc->ctl_lock);
13315 for (;;) {
13316 retval = 0;
13317
13318 /*
13319 * We handle the queues in this order:
13320 * - task management
13321 * - ISC
13322 * - done queue (to free up resources, unblock other commands)
13323 * - RtR queue
13324 * - incoming queue
13325 *
13326 * If those queues are empty, we break out of the loop and
13327 * go to sleep.
13328 */
13329 io = (union ctl_io *)STAILQ_FIRST(&softc->task_queue);
13330 if (io != NULL) {
13331 ctl_run_task_queue(softc);
13332 continue;
13333 }
13334 io = (union ctl_io *)STAILQ_FIRST(&softc->isc_queue);
13335 if (io != NULL) {
13336 STAILQ_REMOVE_HEAD(&softc->isc_queue, links);
13337 ctl_handle_isc(io);
13338 continue;
13339 }
13340 io = (union ctl_io *)STAILQ_FIRST(&softc->done_queue);
13341 if (io != NULL) {
13342 STAILQ_REMOVE_HEAD(&softc->done_queue, links);
13343 /* clear any blocked commands, call fe_done */
13344 mtx_unlock(&softc->ctl_lock);
13345 /*
13346 * XXX KDM
13347 * Call this without a lock for now. This will
13348 * depend on whether there is any way the FETD can
13349 * sleep or deadlock if called with the CTL lock
13350 * held.
13351 */
13352 retval = ctl_process_done(io, /*have_lock*/ 0);
13353 mtx_lock(&softc->ctl_lock);
13354 continue;
13355 }
13356 if (!ctl_pause_rtr) {
13357 io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue);
13358 if (io != NULL) {
13359 STAILQ_REMOVE_HEAD(&softc->rtr_queue, links);
13360 mtx_unlock(&softc->ctl_lock);
13361 retval = ctl_scsiio(&io->scsiio);
13362 if (retval != CTL_RETVAL_COMPLETE)
13363 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13364 mtx_lock(&softc->ctl_lock);
13365 continue;
13366 }
13367 }
13368 io = (union ctl_io *)STAILQ_FIRST(&softc->incoming_queue);
13369 if (io != NULL) {
13370 STAILQ_REMOVE_HEAD(&softc->incoming_queue, links);
13371 mtx_unlock(&softc->ctl_lock);
13372 ctl_scsiio_precheck(softc, &io->scsiio);
13373 mtx_lock(&softc->ctl_lock);
13374 continue;
13375 }
13376 /*
13377 * We might want to move this to a separate thread, so that
13378 * configuration requests (in this case LUN creations)
13379 * won't impact the I/O path.
13380 */
13381 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13382 if (be_lun != NULL) {
13383 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13384 mtx_unlock(&softc->ctl_lock);
13385 ctl_create_lun(be_lun);
13386 mtx_lock(&softc->ctl_lock);
13387 continue;
13388 }
13389
13390 /* XXX KDM use the PDROP flag?? */
13391 /* Sleep until we have something to do. */
13392 mtx_sleep(softc, &softc->ctl_lock, PRIBIO, "-", 0);
13393
13394 /* Back to the top of the loop to see what woke us up. */
13395 continue;
13396 }
13397}
13398
13399void
13400ctl_wakeup_thread()
13401{
13402 struct ctl_softc *softc;
13403
13404 softc = control_softc;
13405
13406 wakeup_one(softc);
13407}
13408
13409/* Initialization and failover */
13410
13411void
13412ctl_init_isc_msg(void)
13413{
13414 printf("CTL: Still calling this thing\n");
13415}
13416
13417/*
13418 * Init component
13419 * Initializes component into configuration defined by bootMode
13420 * (see hasc-sv.c)
13421 * returns hasc_Status:
13422 * OK
13423 * ERROR - fatal error
13424 */
13425static ctl_ha_comp_status
13426ctl_isc_init(struct ctl_ha_component *c)
13427{
13428 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
13429
13430 c->status = ret;
13431 return ret;
13432}
13433
13434/* Start component
13435 * Starts component in state requested. If component starts successfully,
13436 * it must set its own state to the requestrd state
13437 * When requested state is HASC_STATE_HA, the component may refine it
13438 * by adding _SLAVE or _MASTER flags.
13439 * Currently allowed state transitions are:
13440 * UNKNOWN->HA - initial startup
13441 * UNKNOWN->SINGLE - initial startup when no parter detected
13442 * HA->SINGLE - failover
13443 * returns ctl_ha_comp_status:
13444 * OK - component successfully started in requested state
13445 * FAILED - could not start the requested state, failover may
13446 * be possible
13447 * ERROR - fatal error detected, no future startup possible
13448 */
13449static ctl_ha_comp_status
13450ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state)
13451{
13452 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
13453
13454 printf("%s: go\n", __func__);
13455
13456 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap)
13457 if (c->state == CTL_HA_STATE_UNKNOWN ) {
13458 ctl_is_single = 0;
13459 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
13460 != CTL_HA_STATUS_SUCCESS) {
13461 printf("ctl_isc_start: ctl_ha_msg_create failed.\n");
13462 ret = CTL_HA_COMP_STATUS_ERROR;
13463 }
13464 } else if (CTL_HA_STATE_IS_HA(c->state)
13465 && CTL_HA_STATE_IS_SINGLE(state)){
13466 // HA->SINGLE transition
13467 ctl_failover();
13468 ctl_is_single = 1;
13469 } else {
13470 printf("ctl_isc_start:Invalid state transition %X->%X\n",
13471 c->state, state);
13472 ret = CTL_HA_COMP_STATUS_ERROR;
13473 }
13474 if (CTL_HA_STATE_IS_SINGLE(state))
13475 ctl_is_single = 1;
13476
13477 c->state = state;
13478 c->status = ret;
13479 return ret;
13480}
13481
13482/*
13483 * Quiesce component
13484 * The component must clear any error conditions (set status to OK) and
13485 * prepare itself to another Start call
13486 * returns ctl_ha_comp_status:
13487 * OK
13488 * ERROR
13489 */
13490static ctl_ha_comp_status
13491ctl_isc_quiesce(struct ctl_ha_component *c)
13492{
13493 int ret = CTL_HA_COMP_STATUS_OK;
13494
13495 ctl_pause_rtr = 1;
13496 c->status = ret;
13497 return ret;
13498}
13499
13500struct ctl_ha_component ctl_ha_component_ctlisc =
13501{
13502 .name = "CTL ISC",
13503 .state = CTL_HA_STATE_UNKNOWN,
13504 .init = ctl_isc_init,
13505 .start = ctl_isc_start,
13506 .quiesce = ctl_isc_quiesce
13507};
13508
13509/*
13510 * vim: ts=8
13511 */
13058 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
13059
13060 if (isread) {
13061 lun->stats.ports[targ_port].bytes[CTL_STATS_READ] +=
13062 lbalen->len * blocksize;
13063 lun->stats.ports[targ_port].operations[CTL_STATS_READ]++;
13064
13065#ifdef CTL_TIME_IO
13066 bintime_add(
13067 &lun->stats.ports[targ_port].dma_time[CTL_STATS_READ],
13068 &io->io_hdr.dma_bt);
13069 lun->stats.ports[targ_port].num_dmas[CTL_STATS_READ] +=
13070 io->io_hdr.num_dmas;
13071 getbintime(&cur_bt);
13072 bintime_sub(&cur_bt,
13073 &io->io_hdr.start_bt);
13074
13075 bintime_add(
13076 &lun->stats.ports[targ_port].time[CTL_STATS_READ],
13077 &cur_bt);
13078
13079#if 0
13080 cs_prof_gettime(&cur_ticks);
13081 lun->stats.time[CTL_STATS_READ] +=
13082 cur_ticks -
13083 io->io_hdr.start_ticks;
13084#endif
13085#if 0
13086 lun->stats.time[CTL_STATS_READ] +=
13087 jiffies - io->io_hdr.start_time;
13088#endif
13089#endif /* CTL_TIME_IO */
13090 } else {
13091 lun->stats.ports[targ_port].bytes[CTL_STATS_WRITE] +=
13092 lbalen->len * blocksize;
13093 lun->stats.ports[targ_port].operations[
13094 CTL_STATS_WRITE]++;
13095
13096#ifdef CTL_TIME_IO
13097 bintime_add(
13098 &lun->stats.ports[targ_port].dma_time[CTL_STATS_WRITE],
13099 &io->io_hdr.dma_bt);
13100 lun->stats.ports[targ_port].num_dmas[CTL_STATS_WRITE] +=
13101 io->io_hdr.num_dmas;
13102 getbintime(&cur_bt);
13103 bintime_sub(&cur_bt,
13104 &io->io_hdr.start_bt);
13105
13106 bintime_add(
13107 &lun->stats.ports[targ_port].time[CTL_STATS_WRITE],
13108 &cur_bt);
13109#if 0
13110 cs_prof_gettime(&cur_ticks);
13111 lun->stats.ports[targ_port].time[CTL_STATS_WRITE] +=
13112 cur_ticks -
13113 io->io_hdr.start_ticks;
13114 lun->stats.ports[targ_port].time[CTL_STATS_WRITE] +=
13115 jiffies - io->io_hdr.start_time;
13116#endif
13117#endif /* CTL_TIME_IO */
13118 }
13119 break;
13120 default:
13121 lun->stats.ports[targ_port].operations[CTL_STATS_NO_IO]++;
13122
13123#ifdef CTL_TIME_IO
13124 bintime_add(
13125 &lun->stats.ports[targ_port].dma_time[CTL_STATS_NO_IO],
13126 &io->io_hdr.dma_bt);
13127 lun->stats.ports[targ_port].num_dmas[CTL_STATS_NO_IO] +=
13128 io->io_hdr.num_dmas;
13129 getbintime(&cur_bt);
13130 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13131
13132 bintime_add(&lun->stats.ports[targ_port].time[CTL_STATS_NO_IO],
13133 &cur_bt);
13134
13135#if 0
13136 cs_prof_gettime(&cur_ticks);
13137 lun->stats.ports[targ_port].time[CTL_STATS_NO_IO] +=
13138 cur_ticks -
13139 io->io_hdr.start_ticks;
13140 lun->stats.ports[targ_port].time[CTL_STATS_NO_IO] +=
13141 jiffies - io->io_hdr.start_time;
13142#endif
13143#endif /* CTL_TIME_IO */
13144 break;
13145 }
13146 break;
13147 }
13148 default:
13149 break;
13150 }
13151 }
13152
13153 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13154
13155 /*
13156 * Run through the blocked queue on this LUN and see if anything
13157 * has become unblocked, now that this transaction is done.
13158 */
13159 ctl_check_blocked(lun);
13160
13161 /*
13162 * If the LUN has been invalidated, free it if there is nothing
13163 * left on its OOA queue.
13164 */
13165 if ((lun->flags & CTL_LUN_INVALID)
13166 && (TAILQ_FIRST(&lun->ooa_queue) == NULL))
13167 ctl_free_lun(lun);
13168
13169 /*
13170 * If this command has been aborted, make sure we set the status
13171 * properly. The FETD is responsible for freeing the I/O and doing
13172 * whatever it needs to do to clean up its state.
13173 */
13174 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13175 io->io_hdr.status = CTL_CMD_ABORTED;
13176
13177 /*
13178 * We print out status for every task management command. For SCSI
13179 * commands, we filter out any unit attention errors; they happen
13180 * on every boot, and would clutter up the log. Note: task
13181 * management commands aren't printed here, they are printed above,
13182 * since they should never even make it down here.
13183 */
13184 switch (io->io_hdr.io_type) {
13185 case CTL_IO_SCSI: {
13186 int error_code, sense_key, asc, ascq;
13187
13188 sense_key = 0;
13189
13190 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR)
13191 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) {
13192 /*
13193 * Since this is just for printing, no need to
13194 * show errors here.
13195 */
13196 scsi_extract_sense_len(&io->scsiio.sense_data,
13197 io->scsiio.sense_len,
13198 &error_code,
13199 &sense_key,
13200 &asc,
13201 &ascq,
13202 /*show_errors*/ 0);
13203 }
13204
13205 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
13206 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR)
13207 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND)
13208 || (sense_key != SSD_KEY_UNIT_ATTENTION))) {
13209
13210 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){
13211 ctl_softc->skipped_prints++;
13212 if (have_lock == 0)
13213 mtx_unlock(&ctl_softc->ctl_lock);
13214 } else {
13215 uint32_t skipped_prints;
13216
13217 skipped_prints = ctl_softc->skipped_prints;
13218
13219 ctl_softc->skipped_prints = 0;
13220 ctl_softc->last_print_jiffies = time_uptime;
13221
13222 if (have_lock == 0)
13223 mtx_unlock(&ctl_softc->ctl_lock);
13224 if (skipped_prints > 0) {
13225#ifdef NEEDTOPORT
13226 csevent_log(CSC_CTL | CSC_SHELF_SW |
13227 CTL_ERROR_REPORT,
13228 csevent_LogType_Trace,
13229 csevent_Severity_Information,
13230 csevent_AlertLevel_Green,
13231 csevent_FRU_Firmware,
13232 csevent_FRU_Unknown,
13233 "High CTL error volume, %d prints "
13234 "skipped", skipped_prints);
13235#endif
13236 }
13237 if (bootverbose || verbose > 0)
13238 ctl_io_error_print(io, NULL);
13239 }
13240 } else {
13241 if (have_lock == 0)
13242 mtx_unlock(&ctl_softc->ctl_lock);
13243 }
13244 break;
13245 }
13246 case CTL_IO_TASK:
13247 if (have_lock == 0)
13248 mtx_unlock(&ctl_softc->ctl_lock);
13249 if (bootverbose || verbose > 0)
13250 ctl_io_error_print(io, NULL);
13251 break;
13252 default:
13253 if (have_lock == 0)
13254 mtx_unlock(&ctl_softc->ctl_lock);
13255 break;
13256 }
13257
13258 /*
13259 * Tell the FETD or the other shelf controller we're done with this
13260 * command. Note that only SCSI commands get to this point. Task
13261 * management commands are completed above.
13262 *
13263 * We only send status to the other controller if we're in XFER
13264 * mode. In SER_ONLY mode, the I/O is done on the controller that
13265 * received the I/O (from CTL's perspective), and so the status is
13266 * generated there.
13267 *
13268 * XXX KDM if we hold the lock here, we could cause a deadlock
13269 * if the frontend comes back in in this context to queue
13270 * something.
13271 */
13272 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER)
13273 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
13274 union ctl_ha_msg msg;
13275
13276 memset(&msg, 0, sizeof(msg));
13277 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13278 msg.hdr.original_sc = io->io_hdr.original_sc;
13279 msg.hdr.nexus = io->io_hdr.nexus;
13280 msg.hdr.status = io->io_hdr.status;
13281 msg.scsi.scsi_status = io->scsiio.scsi_status;
13282 msg.scsi.tag_num = io->scsiio.tag_num;
13283 msg.scsi.tag_type = io->scsiio.tag_type;
13284 msg.scsi.sense_len = io->scsiio.sense_len;
13285 msg.scsi.sense_residual = io->scsiio.sense_residual;
13286 msg.scsi.residual = io->scsiio.residual;
13287 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
13288 sizeof(io->scsiio.sense_data));
13289 /*
13290 * We copy this whether or not this is an I/O-related
13291 * command. Otherwise, we'd have to go and check to see
13292 * whether it's a read/write command, and it really isn't
13293 * worth it.
13294 */
13295 memcpy(&msg.scsi.lbalen,
13296 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
13297 sizeof(msg.scsi.lbalen));
13298
13299 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13300 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
13301 /* XXX do something here */
13302 }
13303
13304 ctl_free_io(io);
13305 } else
13306 fe_done(io);
13307
13308bailout:
13309
13310 return (CTL_RETVAL_COMPLETE);
13311}
13312
13313/*
13314 * Front end should call this if it doesn't do autosense. When the request
13315 * sense comes back in from the initiator, we'll dequeue this and send it.
13316 */
13317int
13318ctl_queue_sense(union ctl_io *io)
13319{
13320 struct ctl_lun *lun;
13321 struct ctl_softc *ctl_softc;
13322 uint32_t initidx, targ_lun;
13323
13324 ctl_softc = control_softc;
13325
13326 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13327
13328 /*
13329 * LUN lookup will likely move to the ctl_work_thread() once we
13330 * have our new queueing infrastructure (that doesn't put things on
13331 * a per-LUN queue initially). That is so that we can handle
13332 * things like an INQUIRY to a LUN that we don't have enabled. We
13333 * can't deal with that right now.
13334 */
13335 mtx_lock(&ctl_softc->ctl_lock);
13336
13337 /*
13338 * If we don't have a LUN for this, just toss the sense
13339 * information.
13340 */
13341 targ_lun = io->io_hdr.nexus.targ_lun;
13342 if (io->io_hdr.nexus.lun_map_fn != NULL)
13343 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
13344 if ((targ_lun < CTL_MAX_LUNS)
13345 && (ctl_softc->ctl_luns[targ_lun] != NULL))
13346 lun = ctl_softc->ctl_luns[targ_lun];
13347 else
13348 goto bailout;
13349
13350 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13351
13352 /*
13353 * Already have CA set for this LUN...toss the sense information.
13354 */
13355 if (ctl_is_set(lun->have_ca, initidx))
13356 goto bailout;
13357
13358 memcpy(&lun->pending_sense[initidx].sense, &io->scsiio.sense_data,
13359 ctl_min(sizeof(lun->pending_sense[initidx].sense),
13360 sizeof(io->scsiio.sense_data)));
13361 ctl_set_mask(lun->have_ca, initidx);
13362
13363bailout:
13364 mtx_unlock(&ctl_softc->ctl_lock);
13365
13366 ctl_free_io(io);
13367
13368 return (CTL_RETVAL_COMPLETE);
13369}
13370
13371/*
13372 * Primary command inlet from frontend ports. All SCSI and task I/O
13373 * requests must go through this function.
13374 */
13375int
13376ctl_queue(union ctl_io *io)
13377{
13378 struct ctl_softc *ctl_softc;
13379
13380 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13381
13382 ctl_softc = control_softc;
13383
13384#ifdef CTL_TIME_IO
13385 io->io_hdr.start_time = time_uptime;
13386 getbintime(&io->io_hdr.start_bt);
13387#endif /* CTL_TIME_IO */
13388
13389 mtx_lock(&ctl_softc->ctl_lock);
13390
13391 switch (io->io_hdr.io_type) {
13392 case CTL_IO_SCSI:
13393 STAILQ_INSERT_TAIL(&ctl_softc->incoming_queue, &io->io_hdr,
13394 links);
13395 break;
13396 case CTL_IO_TASK:
13397 STAILQ_INSERT_TAIL(&ctl_softc->task_queue, &io->io_hdr, links);
13398 /*
13399 * Set the task pending flag. This is necessary to close a
13400 * race condition with the FETD:
13401 *
13402 * - FETD submits a task management command, like an abort.
13403 * - Back end calls fe_datamove() to move the data for the
13404 * aborted command. The FETD can't really accept it, but
13405 * if it did, it would end up transmitting data for a
13406 * command that the initiator told us to abort.
13407 *
13408 * We close the race condition by setting the flag here,
13409 * and checking it in ctl_datamove(), before calling the
13410 * FETD's fe_datamove routine. If we've got a task
13411 * pending, we run the task queue and then check to see
13412 * whether our particular I/O has been aborted.
13413 */
13414 ctl_softc->flags |= CTL_FLAG_TASK_PENDING;
13415 break;
13416 default:
13417 mtx_unlock(&ctl_softc->ctl_lock);
13418 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13419 return (-EINVAL);
13420 break; /* NOTREACHED */
13421 }
13422 mtx_unlock(&ctl_softc->ctl_lock);
13423
13424 ctl_wakeup_thread();
13425
13426 return (CTL_RETVAL_COMPLETE);
13427}
13428
13429#ifdef CTL_IO_DELAY
13430static void
13431ctl_done_timer_wakeup(void *arg)
13432{
13433 union ctl_io *io;
13434
13435 io = (union ctl_io *)arg;
13436 ctl_done_lock(io, /*have_lock*/ 0);
13437}
13438#endif /* CTL_IO_DELAY */
13439
13440void
13441ctl_done_lock(union ctl_io *io, int have_lock)
13442{
13443 struct ctl_softc *ctl_softc;
13444#ifndef CTL_DONE_THREAD
13445 union ctl_io *xio;
13446#endif /* !CTL_DONE_THREAD */
13447
13448 ctl_softc = control_softc;
13449
13450 if (have_lock == 0)
13451 mtx_lock(&ctl_softc->ctl_lock);
13452
13453 /*
13454 * Enable this to catch duplicate completion issues.
13455 */
13456#if 0
13457 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13458 printf("%s: type %d msg %d cdb %x iptl: "
13459 "%d:%d:%d:%d tag 0x%04x "
13460 "flag %#x status %x\n",
13461 __func__,
13462 io->io_hdr.io_type,
13463 io->io_hdr.msg_type,
13464 io->scsiio.cdb[0],
13465 io->io_hdr.nexus.initid.id,
13466 io->io_hdr.nexus.targ_port,
13467 io->io_hdr.nexus.targ_target.id,
13468 io->io_hdr.nexus.targ_lun,
13469 (io->io_hdr.io_type ==
13470 CTL_IO_TASK) ?
13471 io->taskio.tag_num :
13472 io->scsiio.tag_num,
13473 io->io_hdr.flags,
13474 io->io_hdr.status);
13475 } else
13476 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13477#endif
13478
13479 /*
13480 * This is an internal copy of an I/O, and should not go through
13481 * the normal done processing logic.
13482 */
13483 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) {
13484 if (have_lock == 0)
13485 mtx_unlock(&ctl_softc->ctl_lock);
13486 return;
13487 }
13488
13489 /*
13490 * We need to send a msg to the serializing shelf to finish the IO
13491 * as well. We don't send a finish message to the other shelf if
13492 * this is a task management command. Task management commands
13493 * aren't serialized in the OOA queue, but rather just executed on
13494 * both shelf controllers for commands that originated on that
13495 * controller.
13496 */
13497 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)
13498 && (io->io_hdr.io_type != CTL_IO_TASK)) {
13499 union ctl_ha_msg msg_io;
13500
13501 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO;
13502 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc;
13503 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io,
13504 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) {
13505 }
13506 /* continue on to finish IO */
13507 }
13508#ifdef CTL_IO_DELAY
13509 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13510 struct ctl_lun *lun;
13511
13512 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13513
13514 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13515 } else {
13516 struct ctl_lun *lun;
13517
13518 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13519
13520 if ((lun != NULL)
13521 && (lun->delay_info.done_delay > 0)) {
13522 struct callout *callout;
13523
13524 callout = (struct callout *)&io->io_hdr.timer_bytes;
13525 callout_init(callout, /*mpsafe*/ 1);
13526 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13527 callout_reset(callout,
13528 lun->delay_info.done_delay * hz,
13529 ctl_done_timer_wakeup, io);
13530 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13531 lun->delay_info.done_delay = 0;
13532 if (have_lock == 0)
13533 mtx_unlock(&ctl_softc->ctl_lock);
13534 return;
13535 }
13536 }
13537#endif /* CTL_IO_DELAY */
13538
13539 STAILQ_INSERT_TAIL(&ctl_softc->done_queue, &io->io_hdr, links);
13540
13541#ifdef CTL_DONE_THREAD
13542 if (have_lock == 0)
13543 mtx_unlock(&ctl_softc->ctl_lock);
13544
13545 ctl_wakeup_thread();
13546#else /* CTL_DONE_THREAD */
13547 for (xio = (union ctl_io *)STAILQ_FIRST(&ctl_softc->done_queue);
13548 xio != NULL;
13549 xio =(union ctl_io *)STAILQ_FIRST(&ctl_softc->done_queue)) {
13550
13551 STAILQ_REMOVE_HEAD(&ctl_softc->done_queue, links);
13552
13553 ctl_process_done(xio, /*have_lock*/ 1);
13554 }
13555 if (have_lock == 0)
13556 mtx_unlock(&ctl_softc->ctl_lock);
13557#endif /* CTL_DONE_THREAD */
13558}
13559
13560void
13561ctl_done(union ctl_io *io)
13562{
13563 ctl_done_lock(io, /*have_lock*/ 0);
13564}
13565
13566int
13567ctl_isc(struct ctl_scsiio *ctsio)
13568{
13569 struct ctl_lun *lun;
13570 int retval;
13571
13572 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13573
13574 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0]));
13575
13576 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n"));
13577
13578 retval = lun->backend->data_submit((union ctl_io *)ctsio);
13579
13580 return (retval);
13581}
13582
13583
13584static void
13585ctl_work_thread(void *arg)
13586{
13587 struct ctl_softc *softc;
13588 union ctl_io *io;
13589 struct ctl_be_lun *be_lun;
13590 int retval;
13591
13592 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13593
13594 softc = (struct ctl_softc *)arg;
13595 if (softc == NULL)
13596 return;
13597
13598 mtx_lock(&softc->ctl_lock);
13599 for (;;) {
13600 retval = 0;
13601
13602 /*
13603 * We handle the queues in this order:
13604 * - task management
13605 * - ISC
13606 * - done queue (to free up resources, unblock other commands)
13607 * - RtR queue
13608 * - incoming queue
13609 *
13610 * If those queues are empty, we break out of the loop and
13611 * go to sleep.
13612 */
13613 io = (union ctl_io *)STAILQ_FIRST(&softc->task_queue);
13614 if (io != NULL) {
13615 ctl_run_task_queue(softc);
13616 continue;
13617 }
13618 io = (union ctl_io *)STAILQ_FIRST(&softc->isc_queue);
13619 if (io != NULL) {
13620 STAILQ_REMOVE_HEAD(&softc->isc_queue, links);
13621 ctl_handle_isc(io);
13622 continue;
13623 }
13624 io = (union ctl_io *)STAILQ_FIRST(&softc->done_queue);
13625 if (io != NULL) {
13626 STAILQ_REMOVE_HEAD(&softc->done_queue, links);
13627 /* clear any blocked commands, call fe_done */
13628 mtx_unlock(&softc->ctl_lock);
13629 /*
13630 * XXX KDM
13631 * Call this without a lock for now. This will
13632 * depend on whether there is any way the FETD can
13633 * sleep or deadlock if called with the CTL lock
13634 * held.
13635 */
13636 retval = ctl_process_done(io, /*have_lock*/ 0);
13637 mtx_lock(&softc->ctl_lock);
13638 continue;
13639 }
13640 if (!ctl_pause_rtr) {
13641 io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue);
13642 if (io != NULL) {
13643 STAILQ_REMOVE_HEAD(&softc->rtr_queue, links);
13644 mtx_unlock(&softc->ctl_lock);
13645 retval = ctl_scsiio(&io->scsiio);
13646 if (retval != CTL_RETVAL_COMPLETE)
13647 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13648 mtx_lock(&softc->ctl_lock);
13649 continue;
13650 }
13651 }
13652 io = (union ctl_io *)STAILQ_FIRST(&softc->incoming_queue);
13653 if (io != NULL) {
13654 STAILQ_REMOVE_HEAD(&softc->incoming_queue, links);
13655 mtx_unlock(&softc->ctl_lock);
13656 ctl_scsiio_precheck(softc, &io->scsiio);
13657 mtx_lock(&softc->ctl_lock);
13658 continue;
13659 }
13660 /*
13661 * We might want to move this to a separate thread, so that
13662 * configuration requests (in this case LUN creations)
13663 * won't impact the I/O path.
13664 */
13665 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13666 if (be_lun != NULL) {
13667 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13668 mtx_unlock(&softc->ctl_lock);
13669 ctl_create_lun(be_lun);
13670 mtx_lock(&softc->ctl_lock);
13671 continue;
13672 }
13673
13674 /* XXX KDM use the PDROP flag?? */
13675 /* Sleep until we have something to do. */
13676 mtx_sleep(softc, &softc->ctl_lock, PRIBIO, "-", 0);
13677
13678 /* Back to the top of the loop to see what woke us up. */
13679 continue;
13680 }
13681}
13682
13683void
13684ctl_wakeup_thread()
13685{
13686 struct ctl_softc *softc;
13687
13688 softc = control_softc;
13689
13690 wakeup_one(softc);
13691}
13692
13693/* Initialization and failover */
13694
13695void
13696ctl_init_isc_msg(void)
13697{
13698 printf("CTL: Still calling this thing\n");
13699}
13700
13701/*
13702 * Init component
13703 * Initializes component into configuration defined by bootMode
13704 * (see hasc-sv.c)
13705 * returns hasc_Status:
13706 * OK
13707 * ERROR - fatal error
13708 */
13709static ctl_ha_comp_status
13710ctl_isc_init(struct ctl_ha_component *c)
13711{
13712 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
13713
13714 c->status = ret;
13715 return ret;
13716}
13717
13718/* Start component
13719 * Starts component in state requested. If component starts successfully,
13720 * it must set its own state to the requestrd state
13721 * When requested state is HASC_STATE_HA, the component may refine it
13722 * by adding _SLAVE or _MASTER flags.
13723 * Currently allowed state transitions are:
13724 * UNKNOWN->HA - initial startup
13725 * UNKNOWN->SINGLE - initial startup when no parter detected
13726 * HA->SINGLE - failover
13727 * returns ctl_ha_comp_status:
13728 * OK - component successfully started in requested state
13729 * FAILED - could not start the requested state, failover may
13730 * be possible
13731 * ERROR - fatal error detected, no future startup possible
13732 */
13733static ctl_ha_comp_status
13734ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state)
13735{
13736 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
13737
13738 printf("%s: go\n", __func__);
13739
13740 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap)
13741 if (c->state == CTL_HA_STATE_UNKNOWN ) {
13742 ctl_is_single = 0;
13743 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
13744 != CTL_HA_STATUS_SUCCESS) {
13745 printf("ctl_isc_start: ctl_ha_msg_create failed.\n");
13746 ret = CTL_HA_COMP_STATUS_ERROR;
13747 }
13748 } else if (CTL_HA_STATE_IS_HA(c->state)
13749 && CTL_HA_STATE_IS_SINGLE(state)){
13750 // HA->SINGLE transition
13751 ctl_failover();
13752 ctl_is_single = 1;
13753 } else {
13754 printf("ctl_isc_start:Invalid state transition %X->%X\n",
13755 c->state, state);
13756 ret = CTL_HA_COMP_STATUS_ERROR;
13757 }
13758 if (CTL_HA_STATE_IS_SINGLE(state))
13759 ctl_is_single = 1;
13760
13761 c->state = state;
13762 c->status = ret;
13763 return ret;
13764}
13765
13766/*
13767 * Quiesce component
13768 * The component must clear any error conditions (set status to OK) and
13769 * prepare itself to another Start call
13770 * returns ctl_ha_comp_status:
13771 * OK
13772 * ERROR
13773 */
13774static ctl_ha_comp_status
13775ctl_isc_quiesce(struct ctl_ha_component *c)
13776{
13777 int ret = CTL_HA_COMP_STATUS_OK;
13778
13779 ctl_pause_rtr = 1;
13780 c->status = ret;
13781 return ret;
13782}
13783
13784struct ctl_ha_component ctl_ha_component_ctlisc =
13785{
13786 .name = "CTL ISC",
13787 .state = CTL_HA_STATE_UNKNOWN,
13788 .init = ctl_isc_init,
13789 .start = ctl_isc_start,
13790 .quiesce = ctl_isc_quiesce
13791};
13792
13793/*
13794 * vim: ts=8
13795 */