Deleted Added
sdiff udiff text old ( 312582 ) new ( 312834 )
full compact
1/*-
2 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3 * Copyright (c) 2012 The FreeBSD Foundation
4 * Copyright (c) 2015 Alexander Motin <mav@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Portions of this software were developed by Edward Tomasz Napierala
8 * under sponsorship from the FreeBSD Foundation.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification.
16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17 * substantially similar to the "NO WARRANTY" disclaimer below
18 * ("Disclaimer") and any redistribution must be conditioned upon
19 * including a substantially similar Disclaimer requirement for further
20 * binary redistribution.
21 *
22 * NO WARRANTY
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGES.
34 *
35 * $Id$
36 */
37/*
38 * CAM Target Layer, a SCSI device emulation subsystem.
39 *
40 * Author: Ken Merry <ken@FreeBSD.org>
41 */
42
43#define _CTL_C
44
45#include <sys/cdefs.h>
46__FBSDID("$FreeBSD: stable/11/sys/cam/ctl/ctl.c 312834 2017-01-26 20:49:19Z mav $");
47
48#include <sys/param.h>
49#include <sys/systm.h>
50#include <sys/ctype.h>
51#include <sys/kernel.h>
52#include <sys/types.h>
53#include <sys/kthread.h>
54#include <sys/bio.h>
55#include <sys/fcntl.h>
56#include <sys/lock.h>
57#include <sys/module.h>
58#include <sys/mutex.h>
59#include <sys/condvar.h>
60#include <sys/malloc.h>
61#include <sys/conf.h>
62#include <sys/ioccom.h>
63#include <sys/queue.h>
64#include <sys/sbuf.h>
65#include <sys/smp.h>
66#include <sys/endian.h>
67#include <sys/sysctl.h>
68#include <vm/uma.h>
69
70#include <cam/cam.h>
71#include <cam/scsi/scsi_all.h>
72#include <cam/scsi/scsi_cd.h>
73#include <cam/scsi/scsi_da.h>
74#include <cam/ctl/ctl_io.h>
75#include <cam/ctl/ctl.h>
76#include <cam/ctl/ctl_frontend.h>
77#include <cam/ctl/ctl_util.h>
78#include <cam/ctl/ctl_backend.h>
79#include <cam/ctl/ctl_ioctl.h>
80#include <cam/ctl/ctl_ha.h>
81#include <cam/ctl/ctl_private.h>
82#include <cam/ctl/ctl_debug.h>
83#include <cam/ctl/ctl_scsi_all.h>
84#include <cam/ctl/ctl_error.h>
85
86struct ctl_softc *control_softc = NULL;
87
88/*
89 * Template mode pages.
90 */
91
92/*
93 * Note that these are default values only. The actual values will be
94 * filled in when the user does a mode sense.
95 */
96const static struct scsi_da_rw_recovery_page rw_er_page_default = {
97 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
98 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
99 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
100 /*read_retry_count*/0,
101 /*correction_span*/0,
102 /*head_offset_count*/0,
103 /*data_strobe_offset_cnt*/0,
104 /*byte8*/SMS_RWER_LBPERE,
105 /*write_retry_count*/0,
106 /*reserved2*/0,
107 /*recovery_time_limit*/{0, 0},
108};
109
110const static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
111 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
112 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
113 /*byte3*/SMS_RWER_PER,
114 /*read_retry_count*/0,
115 /*correction_span*/0,
116 /*head_offset_count*/0,
117 /*data_strobe_offset_cnt*/0,
118 /*byte8*/SMS_RWER_LBPERE,
119 /*write_retry_count*/0,
120 /*reserved2*/0,
121 /*recovery_time_limit*/{0, 0},
122};
123
124const static struct scsi_format_page format_page_default = {
125 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
126 /*page_length*/sizeof(struct scsi_format_page) - 2,
127 /*tracks_per_zone*/ {0, 0},
128 /*alt_sectors_per_zone*/ {0, 0},
129 /*alt_tracks_per_zone*/ {0, 0},
130 /*alt_tracks_per_lun*/ {0, 0},
131 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
132 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
133 /*bytes_per_sector*/ {0, 0},
134 /*interleave*/ {0, 0},
135 /*track_skew*/ {0, 0},
136 /*cylinder_skew*/ {0, 0},
137 /*flags*/ SFP_HSEC,
138 /*reserved*/ {0, 0, 0}
139};
140
141const static struct scsi_format_page format_page_changeable = {
142 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
143 /*page_length*/sizeof(struct scsi_format_page) - 2,
144 /*tracks_per_zone*/ {0, 0},
145 /*alt_sectors_per_zone*/ {0, 0},
146 /*alt_tracks_per_zone*/ {0, 0},
147 /*alt_tracks_per_lun*/ {0, 0},
148 /*sectors_per_track*/ {0, 0},
149 /*bytes_per_sector*/ {0, 0},
150 /*interleave*/ {0, 0},
151 /*track_skew*/ {0, 0},
152 /*cylinder_skew*/ {0, 0},
153 /*flags*/ 0,
154 /*reserved*/ {0, 0, 0}
155};
156
157const static struct scsi_rigid_disk_page rigid_disk_page_default = {
158 /*page_code*/SMS_RIGID_DISK_PAGE,
159 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
160 /*cylinders*/ {0, 0, 0},
161 /*heads*/ CTL_DEFAULT_HEADS,
162 /*start_write_precomp*/ {0, 0, 0},
163 /*start_reduced_current*/ {0, 0, 0},
164 /*step_rate*/ {0, 0},
165 /*landing_zone_cylinder*/ {0, 0, 0},
166 /*rpl*/ SRDP_RPL_DISABLED,
167 /*rotational_offset*/ 0,
168 /*reserved1*/ 0,
169 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
170 CTL_DEFAULT_ROTATION_RATE & 0xff},
171 /*reserved2*/ {0, 0}
172};
173
174const static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
175 /*page_code*/SMS_RIGID_DISK_PAGE,
176 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
177 /*cylinders*/ {0, 0, 0},
178 /*heads*/ 0,
179 /*start_write_precomp*/ {0, 0, 0},
180 /*start_reduced_current*/ {0, 0, 0},
181 /*step_rate*/ {0, 0},
182 /*landing_zone_cylinder*/ {0, 0, 0},
183 /*rpl*/ 0,
184 /*rotational_offset*/ 0,
185 /*reserved1*/ 0,
186 /*rotation_rate*/ {0, 0},
187 /*reserved2*/ {0, 0}
188};
189
190const static struct scsi_da_verify_recovery_page verify_er_page_default = {
191 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
192 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
193 /*byte3*/0,
194 /*read_retry_count*/0,
195 /*reserved*/{ 0, 0, 0, 0, 0, 0 },
196 /*recovery_time_limit*/{0, 0},
197};
198
199const static struct scsi_da_verify_recovery_page verify_er_page_changeable = {
200 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
201 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
202 /*byte3*/SMS_VER_PER,
203 /*read_retry_count*/0,
204 /*reserved*/{ 0, 0, 0, 0, 0, 0 },
205 /*recovery_time_limit*/{0, 0},
206};
207
208const static struct scsi_caching_page caching_page_default = {
209 /*page_code*/SMS_CACHING_PAGE,
210 /*page_length*/sizeof(struct scsi_caching_page) - 2,
211 /*flags1*/ SCP_DISC | SCP_WCE,
212 /*ret_priority*/ 0,
213 /*disable_pf_transfer_len*/ {0xff, 0xff},
214 /*min_prefetch*/ {0, 0},
215 /*max_prefetch*/ {0xff, 0xff},
216 /*max_pf_ceiling*/ {0xff, 0xff},
217 /*flags2*/ 0,
218 /*cache_segments*/ 0,
219 /*cache_seg_size*/ {0, 0},
220 /*reserved*/ 0,
221 /*non_cache_seg_size*/ {0, 0, 0}
222};
223
224const static struct scsi_caching_page caching_page_changeable = {
225 /*page_code*/SMS_CACHING_PAGE,
226 /*page_length*/sizeof(struct scsi_caching_page) - 2,
227 /*flags1*/ SCP_WCE | SCP_RCD,
228 /*ret_priority*/ 0,
229 /*disable_pf_transfer_len*/ {0, 0},
230 /*min_prefetch*/ {0, 0},
231 /*max_prefetch*/ {0, 0},
232 /*max_pf_ceiling*/ {0, 0},
233 /*flags2*/ 0,
234 /*cache_segments*/ 0,
235 /*cache_seg_size*/ {0, 0},
236 /*reserved*/ 0,
237 /*non_cache_seg_size*/ {0, 0, 0}
238};
239
240const static struct scsi_control_page control_page_default = {
241 /*page_code*/SMS_CONTROL_MODE_PAGE,
242 /*page_length*/sizeof(struct scsi_control_page) - 2,
243 /*rlec*/0,
244 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
245 /*eca_and_aen*/0,
246 /*flags4*/SCP_TAS,
247 /*aen_holdoff_period*/{0, 0},
248 /*busy_timeout_period*/{0, 0},
249 /*extended_selftest_completion_time*/{0, 0}
250};
251
252const static struct scsi_control_page control_page_changeable = {
253 /*page_code*/SMS_CONTROL_MODE_PAGE,
254 /*page_length*/sizeof(struct scsi_control_page) - 2,
255 /*rlec*/SCP_DSENSE,
256 /*queue_flags*/SCP_QUEUE_ALG_MASK | SCP_NUAR,
257 /*eca_and_aen*/SCP_SWP,
258 /*flags4*/0,
259 /*aen_holdoff_period*/{0, 0},
260 /*busy_timeout_period*/{0, 0},
261 /*extended_selftest_completion_time*/{0, 0}
262};
263
264#define CTL_CEM_LEN (sizeof(struct scsi_control_ext_page) - 4)
265
266const static struct scsi_control_ext_page control_ext_page_default = {
267 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
268 /*subpage_code*/0x01,
269 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
270 /*flags*/0,
271 /*prio*/0,
272 /*max_sense*/0
273};
274
275const static struct scsi_control_ext_page control_ext_page_changeable = {
276 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
277 /*subpage_code*/0x01,
278 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
279 /*flags*/0,
280 /*prio*/0,
281 /*max_sense*/0xff
282};
283
284const static struct scsi_info_exceptions_page ie_page_default = {
285 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
286 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
287 /*info_flags*/SIEP_FLAGS_EWASC,
288 /*mrie*/SIEP_MRIE_NO,
289 /*interval_timer*/{0, 0, 0, 0},
290 /*report_count*/{0, 0, 0, 1}
291};
292
293const static struct scsi_info_exceptions_page ie_page_changeable = {
294 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
295 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
296 /*info_flags*/SIEP_FLAGS_EWASC | SIEP_FLAGS_DEXCPT | SIEP_FLAGS_TEST |
297 SIEP_FLAGS_LOGERR,
298 /*mrie*/0x0f,
299 /*interval_timer*/{0xff, 0xff, 0xff, 0xff},
300 /*report_count*/{0xff, 0xff, 0xff, 0xff}
301};
302
303#define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4)
304
305const static struct ctl_logical_block_provisioning_page lbp_page_default = {{
306 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
307 /*subpage_code*/0x02,
308 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
309 /*flags*/0,
310 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
311 /*descr*/{}},
312 {{/*flags*/0,
313 /*resource*/0x01,
314 /*reserved*/{0, 0},
315 /*count*/{0, 0, 0, 0}},
316 {/*flags*/0,
317 /*resource*/0x02,
318 /*reserved*/{0, 0},
319 /*count*/{0, 0, 0, 0}},
320 {/*flags*/0,
321 /*resource*/0xf1,
322 /*reserved*/{0, 0},
323 /*count*/{0, 0, 0, 0}},
324 {/*flags*/0,
325 /*resource*/0xf2,
326 /*reserved*/{0, 0},
327 /*count*/{0, 0, 0, 0}}
328 }
329};
330
331const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{
332 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
333 /*subpage_code*/0x02,
334 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
335 /*flags*/SLBPP_SITUA,
336 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
337 /*descr*/{}},
338 {{/*flags*/0,
339 /*resource*/0,
340 /*reserved*/{0, 0},
341 /*count*/{0, 0, 0, 0}},
342 {/*flags*/0,
343 /*resource*/0,
344 /*reserved*/{0, 0},
345 /*count*/{0, 0, 0, 0}},
346 {/*flags*/0,
347 /*resource*/0,
348 /*reserved*/{0, 0},
349 /*count*/{0, 0, 0, 0}},
350 {/*flags*/0,
351 /*resource*/0,
352 /*reserved*/{0, 0},
353 /*count*/{0, 0, 0, 0}}
354 }
355};
356
357const static struct scsi_cddvd_capabilities_page cddvd_page_default = {
358 /*page_code*/SMS_CDDVD_CAPS_PAGE,
359 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
360 /*caps1*/0x3f,
361 /*caps2*/0x00,
362 /*caps3*/0xf0,
363 /*caps4*/0x00,
364 /*caps5*/0x29,
365 /*caps6*/0x00,
366 /*obsolete*/{0, 0},
367 /*nvol_levels*/{0, 0},
368 /*buffer_size*/{8, 0},
369 /*obsolete2*/{0, 0},
370 /*reserved*/0,
371 /*digital*/0,
372 /*obsolete3*/0,
373 /*copy_management*/0,
374 /*reserved2*/0,
375 /*rotation_control*/0,
376 /*cur_write_speed*/0,
377 /*num_speed_descr*/0,
378};
379
380const static struct scsi_cddvd_capabilities_page cddvd_page_changeable = {
381 /*page_code*/SMS_CDDVD_CAPS_PAGE,
382 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
383 /*caps1*/0,
384 /*caps2*/0,
385 /*caps3*/0,
386 /*caps4*/0,
387 /*caps5*/0,
388 /*caps6*/0,
389 /*obsolete*/{0, 0},
390 /*nvol_levels*/{0, 0},
391 /*buffer_size*/{0, 0},
392 /*obsolete2*/{0, 0},
393 /*reserved*/0,
394 /*digital*/0,
395 /*obsolete3*/0,
396 /*copy_management*/0,
397 /*reserved2*/0,
398 /*rotation_control*/0,
399 /*cur_write_speed*/0,
400 /*num_speed_descr*/0,
401};
402
403SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
404static int worker_threads = -1;
405SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
406 &worker_threads, 1, "Number of worker threads");
407static int ctl_debug = CTL_DEBUG_NONE;
408SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
409 &ctl_debug, 0, "Enabled debug flags");
410static int ctl_lun_map_size = 1024;
411SYSCTL_INT(_kern_cam_ctl, OID_AUTO, lun_map_size, CTLFLAG_RWTUN,
412 &ctl_lun_map_size, 0, "Size of per-port LUN map (max LUN + 1)");
413
414/*
415 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
416 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
417 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
418 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
419 */
420#define SCSI_EVPD_NUM_SUPPORTED_PAGES 10
421
422static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
423 int param);
424static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
425static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest);
426static int ctl_init(void);
427void ctl_shutdown(void);
428static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
429static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
430static void ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
431static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
432 struct ctl_ooa *ooa_hdr,
433 struct ctl_ooa_entry *kern_entries);
434static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
435 struct thread *td);
436static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
437 struct ctl_be_lun *be_lun);
438static int ctl_free_lun(struct ctl_lun *lun);
439static void ctl_create_lun(struct ctl_be_lun *be_lun);
440
441static int ctl_do_mode_select(union ctl_io *io);
442static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
443 uint64_t res_key, uint64_t sa_res_key,
444 uint8_t type, uint32_t residx,
445 struct ctl_scsiio *ctsio,
446 struct scsi_per_res_out *cdb,
447 struct scsi_per_res_out_parms* param);
448static void ctl_pro_preempt_other(struct ctl_lun *lun,
449 union ctl_ha_msg *msg);
450static void ctl_hndl_per_res_out_on_other_sc(union ctl_io *io);
451static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
452static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
453static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
454static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
455static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
456static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
457 int alloc_len);
458static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
459 int alloc_len);
460static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
461static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
462static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
463static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
464static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
465static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2,
466 bool seq);
467static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2);
468static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
469 union ctl_io *pending_io, union ctl_io *ooa_io);
470static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
471 union ctl_io *starting_io);
472static int ctl_check_blocked(struct ctl_lun *lun);
473static int ctl_scsiio_lun_check(struct ctl_lun *lun,
474 const struct ctl_cmd_entry *entry,
475 struct ctl_scsiio *ctsio);
476static void ctl_failover_lun(union ctl_io *io);
477static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
478 struct ctl_scsiio *ctsio);
479static int ctl_scsiio(struct ctl_scsiio *ctsio);
480
481static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
482static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
483 ctl_ua_type ua_type);
484static int ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io,
485 ctl_ua_type ua_type);
486static int ctl_lun_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
487static int ctl_abort_task(union ctl_io *io);
488static int ctl_abort_task_set(union ctl_io *io);
489static int ctl_query_task(union ctl_io *io, int task_set);
490static int ctl_i_t_nexus_reset(union ctl_io *io);
491static int ctl_query_async_event(union ctl_io *io);
492static void ctl_run_task(union ctl_io *io);
493#ifdef CTL_IO_DELAY
494static void ctl_datamove_timer_wakeup(void *arg);
495static void ctl_done_timer_wakeup(void *arg);
496#endif /* CTL_IO_DELAY */
497
498static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
499static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
500static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
501static void ctl_datamove_remote_write(union ctl_io *io);
502static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
503static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
504static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
505static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
506 ctl_ha_dt_cb callback);
507static void ctl_datamove_remote_read(union ctl_io *io);
508static void ctl_datamove_remote(union ctl_io *io);
509static void ctl_process_done(union ctl_io *io);
510static void ctl_lun_thread(void *arg);
511static void ctl_thresh_thread(void *arg);
512static void ctl_work_thread(void *arg);
513static void ctl_enqueue_incoming(union ctl_io *io);
514static void ctl_enqueue_rtr(union ctl_io *io);
515static void ctl_enqueue_done(union ctl_io *io);
516static void ctl_enqueue_isc(union ctl_io *io);
517static const struct ctl_cmd_entry *
518 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
519static const struct ctl_cmd_entry *
520 ctl_validate_command(struct ctl_scsiio *ctsio);
521static int ctl_cmd_applicable(uint8_t lun_type,
522 const struct ctl_cmd_entry *entry);
523
524static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx);
525static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx);
526static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx);
527static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key);
528
529/*
530 * Load the serialization table. This isn't very pretty, but is probably
531 * the easiest way to do it.
532 */
533#include "ctl_ser_table.c"
534
535/*
536 * We only need to define open, close and ioctl routines for this driver.
537 */
538static struct cdevsw ctl_cdevsw = {
539 .d_version = D_VERSION,
540 .d_flags = 0,
541 .d_open = ctl_open,
542 .d_close = ctl_close,
543 .d_ioctl = ctl_ioctl,
544 .d_name = "ctl",
545};
546
547
548MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
549
550static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
551
552static moduledata_t ctl_moduledata = {
553 "ctl",
554 ctl_module_event_handler,
555 NULL
556};
557
558DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
559MODULE_VERSION(ctl, 1);
560
561static struct ctl_frontend ha_frontend =
562{
563 .name = "ha",
564};
565
566static void
567ctl_ha_datamove(union ctl_io *io)
568{
569 struct ctl_lun *lun = CTL_LUN(io);
570 struct ctl_sg_entry *sgl;
571 union ctl_ha_msg msg;
572 uint32_t sg_entries_sent;
573 int do_sg_copy, i, j;
574
575 memset(&msg.dt, 0, sizeof(msg.dt));
576 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
577 msg.hdr.original_sc = io->io_hdr.original_sc;
578 msg.hdr.serializing_sc = io;
579 msg.hdr.nexus = io->io_hdr.nexus;
580 msg.hdr.status = io->io_hdr.status;
581 msg.dt.flags = io->io_hdr.flags;
582
583 /*
584 * We convert everything into a S/G list here. We can't
585 * pass by reference, only by value between controllers.
586 * So we can't pass a pointer to the S/G list, only as many
587 * S/G entries as we can fit in here. If it's possible for
588 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
589 * then we need to break this up into multiple transfers.
590 */
591 if (io->scsiio.kern_sg_entries == 0) {
592 msg.dt.kern_sg_entries = 1;
593#if 0
594 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
595 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
596 } else {
597 /* XXX KDM use busdma here! */
598 msg.dt.sg_list[0].addr =
599 (void *)vtophys(io->scsiio.kern_data_ptr);
600 }
601#else
602 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
603 ("HA does not support BUS_ADDR"));
604 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
605#endif
606 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
607 do_sg_copy = 0;
608 } else {
609 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
610 do_sg_copy = 1;
611 }
612
613 msg.dt.kern_data_len = io->scsiio.kern_data_len;
614 msg.dt.kern_total_len = io->scsiio.kern_total_len;
615 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
616 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
617 msg.dt.sg_sequence = 0;
618
619 /*
620 * Loop until we've sent all of the S/G entries. On the
621 * other end, we'll recompose these S/G entries into one
622 * contiguous list before processing.
623 */
624 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries;
625 msg.dt.sg_sequence++) {
626 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) /
627 sizeof(msg.dt.sg_list[0])),
628 msg.dt.kern_sg_entries - sg_entries_sent);
629 if (do_sg_copy != 0) {
630 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
631 for (i = sg_entries_sent, j = 0;
632 i < msg.dt.cur_sg_entries; i++, j++) {
633#if 0
634 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
635 msg.dt.sg_list[j].addr = sgl[i].addr;
636 } else {
637 /* XXX KDM use busdma here! */
638 msg.dt.sg_list[j].addr =
639 (void *)vtophys(sgl[i].addr);
640 }
641#else
642 KASSERT((io->io_hdr.flags &
643 CTL_FLAG_BUS_ADDR) == 0,
644 ("HA does not support BUS_ADDR"));
645 msg.dt.sg_list[j].addr = sgl[i].addr;
646#endif
647 msg.dt.sg_list[j].len = sgl[i].len;
648 }
649 }
650
651 sg_entries_sent += msg.dt.cur_sg_entries;
652 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries);
653 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
654 sizeof(msg.dt) - sizeof(msg.dt.sg_list) +
655 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries,
656 M_WAITOK) > CTL_HA_STATUS_SUCCESS) {
657 io->io_hdr.port_status = 31341;
658 io->scsiio.be_move_done(io);
659 return;
660 }
661 msg.dt.sent_sg_entries = sg_entries_sent;
662 }
663
664 /*
665 * Officially handover the request from us to peer.
666 * If failover has just happened, then we must return error.
667 * If failover happen just after, then it is not our problem.
668 */
669 if (lun)
670 mtx_lock(&lun->lun_lock);
671 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
672 if (lun)
673 mtx_unlock(&lun->lun_lock);
674 io->io_hdr.port_status = 31342;
675 io->scsiio.be_move_done(io);
676 return;
677 }
678 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
679 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
680 if (lun)
681 mtx_unlock(&lun->lun_lock);
682}
683
684static void
685ctl_ha_done(union ctl_io *io)
686{
687 union ctl_ha_msg msg;
688
689 if (io->io_hdr.io_type == CTL_IO_SCSI) {
690 memset(&msg, 0, sizeof(msg));
691 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
692 msg.hdr.original_sc = io->io_hdr.original_sc;
693 msg.hdr.nexus = io->io_hdr.nexus;
694 msg.hdr.status = io->io_hdr.status;
695 msg.scsi.scsi_status = io->scsiio.scsi_status;
696 msg.scsi.tag_num = io->scsiio.tag_num;
697 msg.scsi.tag_type = io->scsiio.tag_type;
698 msg.scsi.sense_len = io->scsiio.sense_len;
699 msg.scsi.sense_residual = io->scsiio.sense_residual;
700 msg.scsi.residual = io->scsiio.residual;
701 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
702 io->scsiio.sense_len);
703 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
704 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
705 msg.scsi.sense_len, M_WAITOK);
706 }
707 ctl_free_io(io);
708}
709
710static void
711ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
712 union ctl_ha_msg *msg_info)
713{
714 struct ctl_scsiio *ctsio;
715
716 if (msg_info->hdr.original_sc == NULL) {
717 printf("%s: original_sc == NULL!\n", __func__);
718 /* XXX KDM now what? */
719 return;
720 }
721
722 ctsio = &msg_info->hdr.original_sc->scsiio;
723 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
724 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
725 ctsio->io_hdr.status = msg_info->hdr.status;
726 ctsio->scsi_status = msg_info->scsi.scsi_status;
727 ctsio->sense_len = msg_info->scsi.sense_len;
728 ctsio->sense_residual = msg_info->scsi.sense_residual;
729 ctsio->residual = msg_info->scsi.residual;
730 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
731 msg_info->scsi.sense_len);
732 ctl_enqueue_isc((union ctl_io *)ctsio);
733}
734
735static void
736ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
737 union ctl_ha_msg *msg_info)
738{
739 struct ctl_scsiio *ctsio;
740
741 if (msg_info->hdr.serializing_sc == NULL) {
742 printf("%s: serializing_sc == NULL!\n", __func__);
743 /* XXX KDM now what? */
744 return;
745 }
746
747 ctsio = &msg_info->hdr.serializing_sc->scsiio;
748 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
749 ctl_enqueue_isc((union ctl_io *)ctsio);
750}
751
752void
753ctl_isc_announce_lun(struct ctl_lun *lun)
754{
755 struct ctl_softc *softc = lun->ctl_softc;
756 union ctl_ha_msg *msg;
757 struct ctl_ha_msg_lun_pr_key pr_key;
758 int i, k;
759
760 if (softc->ha_link != CTL_HA_LINK_ONLINE)
761 return;
762 mtx_lock(&lun->lun_lock);
763 i = sizeof(msg->lun);
764 if (lun->lun_devid)
765 i += lun->lun_devid->len;
766 i += sizeof(pr_key) * lun->pr_key_count;
767alloc:
768 mtx_unlock(&lun->lun_lock);
769 msg = malloc(i, M_CTL, M_WAITOK);
770 mtx_lock(&lun->lun_lock);
771 k = sizeof(msg->lun);
772 if (lun->lun_devid)
773 k += lun->lun_devid->len;
774 k += sizeof(pr_key) * lun->pr_key_count;
775 if (i < k) {
776 free(msg, M_CTL);
777 i = k;
778 goto alloc;
779 }
780 bzero(&msg->lun, sizeof(msg->lun));
781 msg->hdr.msg_type = CTL_MSG_LUN_SYNC;
782 msg->hdr.nexus.targ_lun = lun->lun;
783 msg->hdr.nexus.targ_mapped_lun = lun->lun;
784 msg->lun.flags = lun->flags;
785 msg->lun.pr_generation = lun->pr_generation;
786 msg->lun.pr_res_idx = lun->pr_res_idx;
787 msg->lun.pr_res_type = lun->pr_res_type;
788 msg->lun.pr_key_count = lun->pr_key_count;
789 i = 0;
790 if (lun->lun_devid) {
791 msg->lun.lun_devid_len = lun->lun_devid->len;
792 memcpy(&msg->lun.data[i], lun->lun_devid->data,
793 msg->lun.lun_devid_len);
794 i += msg->lun.lun_devid_len;
795 }
796 for (k = 0; k < CTL_MAX_INITIATORS; k++) {
797 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0)
798 continue;
799 pr_key.pr_iid = k;
800 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key));
801 i += sizeof(pr_key);
802 }
803 mtx_unlock(&lun->lun_lock);
804 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
805 M_WAITOK);
806 free(msg, M_CTL);
807
808 if (lun->flags & CTL_LUN_PRIMARY_SC) {
809 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
810 ctl_isc_announce_mode(lun, -1,
811 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
812 lun->mode_pages.index[i].subpage);
813 }
814 }
815}
816
817void
818ctl_isc_announce_port(struct ctl_port *port)
819{
820 struct ctl_softc *softc = port->ctl_softc;
821 union ctl_ha_msg *msg;
822 int i;
823
824 if (port->targ_port < softc->port_min ||
825 port->targ_port >= softc->port_max ||
826 softc->ha_link != CTL_HA_LINK_ONLINE)
827 return;
828 i = sizeof(msg->port) + strlen(port->port_name) + 1;
829 if (port->lun_map)
830 i += port->lun_map_size * sizeof(uint32_t);
831 if (port->port_devid)
832 i += port->port_devid->len;
833 if (port->target_devid)
834 i += port->target_devid->len;
835 if (port->init_devid)
836 i += port->init_devid->len;
837 msg = malloc(i, M_CTL, M_WAITOK);
838 bzero(&msg->port, sizeof(msg->port));
839 msg->hdr.msg_type = CTL_MSG_PORT_SYNC;
840 msg->hdr.nexus.targ_port = port->targ_port;
841 msg->port.port_type = port->port_type;
842 msg->port.physical_port = port->physical_port;
843 msg->port.virtual_port = port->virtual_port;
844 msg->port.status = port->status;
845 i = 0;
846 msg->port.name_len = sprintf(&msg->port.data[i],
847 "%d:%s", softc->ha_id, port->port_name) + 1;
848 i += msg->port.name_len;
849 if (port->lun_map) {
850 msg->port.lun_map_len = port->lun_map_size * sizeof(uint32_t);
851 memcpy(&msg->port.data[i], port->lun_map,
852 msg->port.lun_map_len);
853 i += msg->port.lun_map_len;
854 }
855 if (port->port_devid) {
856 msg->port.port_devid_len = port->port_devid->len;
857 memcpy(&msg->port.data[i], port->port_devid->data,
858 msg->port.port_devid_len);
859 i += msg->port.port_devid_len;
860 }
861 if (port->target_devid) {
862 msg->port.target_devid_len = port->target_devid->len;
863 memcpy(&msg->port.data[i], port->target_devid->data,
864 msg->port.target_devid_len);
865 i += msg->port.target_devid_len;
866 }
867 if (port->init_devid) {
868 msg->port.init_devid_len = port->init_devid->len;
869 memcpy(&msg->port.data[i], port->init_devid->data,
870 msg->port.init_devid_len);
871 i += msg->port.init_devid_len;
872 }
873 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
874 M_WAITOK);
875 free(msg, M_CTL);
876}
877
878void
879ctl_isc_announce_iid(struct ctl_port *port, int iid)
880{
881 struct ctl_softc *softc = port->ctl_softc;
882 union ctl_ha_msg *msg;
883 int i, l;
884
885 if (port->targ_port < softc->port_min ||
886 port->targ_port >= softc->port_max ||
887 softc->ha_link != CTL_HA_LINK_ONLINE)
888 return;
889 mtx_lock(&softc->ctl_lock);
890 i = sizeof(msg->iid);
891 l = 0;
892 if (port->wwpn_iid[iid].name)
893 l = strlen(port->wwpn_iid[iid].name) + 1;
894 i += l;
895 msg = malloc(i, M_CTL, M_NOWAIT);
896 if (msg == NULL) {
897 mtx_unlock(&softc->ctl_lock);
898 return;
899 }
900 bzero(&msg->iid, sizeof(msg->iid));
901 msg->hdr.msg_type = CTL_MSG_IID_SYNC;
902 msg->hdr.nexus.targ_port = port->targ_port;
903 msg->hdr.nexus.initid = iid;
904 msg->iid.in_use = port->wwpn_iid[iid].in_use;
905 msg->iid.name_len = l;
906 msg->iid.wwpn = port->wwpn_iid[iid].wwpn;
907 if (port->wwpn_iid[iid].name)
908 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l);
909 mtx_unlock(&softc->ctl_lock);
910 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT);
911 free(msg, M_CTL);
912}
913
914void
915ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx,
916 uint8_t page, uint8_t subpage)
917{
918 struct ctl_softc *softc = lun->ctl_softc;
919 union ctl_ha_msg msg;
920 u_int i;
921
922 if (softc->ha_link != CTL_HA_LINK_ONLINE)
923 return;
924 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
925 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
926 page && lun->mode_pages.index[i].subpage == subpage)
927 break;
928 }
929 if (i == CTL_NUM_MODE_PAGES)
930 return;
931
932 /* Don't try to replicate pages not present on this device. */
933 if (lun->mode_pages.index[i].page_data == NULL)
934 return;
935
936 bzero(&msg.mode, sizeof(msg.mode));
937 msg.hdr.msg_type = CTL_MSG_MODE_SYNC;
938 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT;
939 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT;
940 msg.hdr.nexus.targ_lun = lun->lun;
941 msg.hdr.nexus.targ_mapped_lun = lun->lun;
942 msg.mode.page_code = page;
943 msg.mode.subpage = subpage;
944 msg.mode.page_len = lun->mode_pages.index[i].page_len;
945 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data,
946 msg.mode.page_len);
947 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode),
948 M_WAITOK);
949}
950
951static void
952ctl_isc_ha_link_up(struct ctl_softc *softc)
953{
954 struct ctl_port *port;
955 struct ctl_lun *lun;
956 union ctl_ha_msg msg;
957 int i;
958
959 /* Announce this node parameters to peer for validation. */
960 msg.login.msg_type = CTL_MSG_LOGIN;
961 msg.login.version = CTL_HA_VERSION;
962 msg.login.ha_mode = softc->ha_mode;
963 msg.login.ha_id = softc->ha_id;
964 msg.login.max_luns = CTL_MAX_LUNS;
965 msg.login.max_ports = CTL_MAX_PORTS;
966 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT;
967 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login),
968 M_WAITOK);
969
970 STAILQ_FOREACH(port, &softc->port_list, links) {
971 ctl_isc_announce_port(port);
972 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
973 if (port->wwpn_iid[i].in_use)
974 ctl_isc_announce_iid(port, i);
975 }
976 }
977 STAILQ_FOREACH(lun, &softc->lun_list, links)
978 ctl_isc_announce_lun(lun);
979}
980
981static void
982ctl_isc_ha_link_down(struct ctl_softc *softc)
983{
984 struct ctl_port *port;
985 struct ctl_lun *lun;
986 union ctl_io *io;
987 int i;
988
989 mtx_lock(&softc->ctl_lock);
990 STAILQ_FOREACH(lun, &softc->lun_list, links) {
991 mtx_lock(&lun->lun_lock);
992 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) {
993 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
994 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
995 }
996 mtx_unlock(&lun->lun_lock);
997
998 mtx_unlock(&softc->ctl_lock);
999 io = ctl_alloc_io(softc->othersc_pool);
1000 mtx_lock(&softc->ctl_lock);
1001 ctl_zero_io(io);
1002 io->io_hdr.msg_type = CTL_MSG_FAILOVER;
1003 io->io_hdr.nexus.targ_mapped_lun = lun->lun;
1004 ctl_enqueue_isc(io);
1005 }
1006
1007 STAILQ_FOREACH(port, &softc->port_list, links) {
1008 if (port->targ_port >= softc->port_min &&
1009 port->targ_port < softc->port_max)
1010 continue;
1011 port->status &= ~CTL_PORT_STATUS_ONLINE;
1012 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1013 port->wwpn_iid[i].in_use = 0;
1014 free(port->wwpn_iid[i].name, M_CTL);
1015 port->wwpn_iid[i].name = NULL;
1016 }
1017 }
1018 mtx_unlock(&softc->ctl_lock);
1019}
1020
1021static void
1022ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1023{
1024 struct ctl_lun *lun;
1025 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus);
1026
1027 mtx_lock(&softc->ctl_lock);
1028 if (msg->hdr.nexus.targ_mapped_lun >= CTL_MAX_LUNS ||
1029 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) == NULL) {
1030 mtx_unlock(&softc->ctl_lock);
1031 return;
1032 }
1033 mtx_lock(&lun->lun_lock);
1034 mtx_unlock(&softc->ctl_lock);
1035 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && msg->ua.ua_set)
1036 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8);
1037 if (msg->ua.ua_all) {
1038 if (msg->ua.ua_set)
1039 ctl_est_ua_all(lun, iid, msg->ua.ua_type);
1040 else
1041 ctl_clr_ua_all(lun, iid, msg->ua.ua_type);
1042 } else {
1043 if (msg->ua.ua_set)
1044 ctl_est_ua(lun, iid, msg->ua.ua_type);
1045 else
1046 ctl_clr_ua(lun, iid, msg->ua.ua_type);
1047 }
1048 mtx_unlock(&lun->lun_lock);
1049}
1050
1051static void
1052ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1053{
1054 struct ctl_lun *lun;
1055 struct ctl_ha_msg_lun_pr_key pr_key;
1056 int i, k;
1057 ctl_lun_flags oflags;
1058 uint32_t targ_lun;
1059
1060 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1061 mtx_lock(&softc->ctl_lock);
1062 if (targ_lun >= CTL_MAX_LUNS ||
1063 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1064 mtx_unlock(&softc->ctl_lock);
1065 return;
1066 }
1067 mtx_lock(&lun->lun_lock);
1068 mtx_unlock(&softc->ctl_lock);
1069 if (lun->flags & CTL_LUN_DISABLED) {
1070 mtx_unlock(&lun->lun_lock);
1071 return;
1072 }
1073 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0;
1074 if (msg->lun.lun_devid_len != i || (i > 0 &&
1075 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) {
1076 mtx_unlock(&lun->lun_lock);
1077 printf("%s: Received conflicting HA LUN %d\n",
1078 __func__, targ_lun);
1079 return;
1080 } else {
1081 /* Record whether peer is primary. */
1082 oflags = lun->flags;
1083 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) &&
1084 (msg->lun.flags & CTL_LUN_DISABLED) == 0)
1085 lun->flags |= CTL_LUN_PEER_SC_PRIMARY;
1086 else
1087 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1088 if (oflags != lun->flags)
1089 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1090
1091 /* If peer is primary and we are not -- use data */
1092 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
1093 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) {
1094 lun->pr_generation = msg->lun.pr_generation;
1095 lun->pr_res_idx = msg->lun.pr_res_idx;
1096 lun->pr_res_type = msg->lun.pr_res_type;
1097 lun->pr_key_count = msg->lun.pr_key_count;
1098 for (k = 0; k < CTL_MAX_INITIATORS; k++)
1099 ctl_clr_prkey(lun, k);
1100 for (k = 0; k < msg->lun.pr_key_count; k++) {
1101 memcpy(&pr_key, &msg->lun.data[i],
1102 sizeof(pr_key));
1103 ctl_alloc_prkey(lun, pr_key.pr_iid);
1104 ctl_set_prkey(lun, pr_key.pr_iid,
1105 pr_key.pr_key);
1106 i += sizeof(pr_key);
1107 }
1108 }
1109
1110 mtx_unlock(&lun->lun_lock);
1111 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n",
1112 __func__, targ_lun,
1113 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ?
1114 "primary" : "secondary"));
1115
1116 /* If we are primary but peer doesn't know -- notify */
1117 if ((lun->flags & CTL_LUN_PRIMARY_SC) &&
1118 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0)
1119 ctl_isc_announce_lun(lun);
1120 }
1121}
1122
1123static void
1124ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1125{
1126 struct ctl_port *port;
1127 struct ctl_lun *lun;
1128 int i, new;
1129
1130 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1131 if (port == NULL) {
1132 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__,
1133 msg->hdr.nexus.targ_port));
1134 new = 1;
1135 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO);
1136 port->frontend = &ha_frontend;
1137 port->targ_port = msg->hdr.nexus.targ_port;
1138 port->fe_datamove = ctl_ha_datamove;
1139 port->fe_done = ctl_ha_done;
1140 } else if (port->frontend == &ha_frontend) {
1141 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__,
1142 msg->hdr.nexus.targ_port));
1143 new = 0;
1144 } else {
1145 printf("%s: Received conflicting HA port %d\n",
1146 __func__, msg->hdr.nexus.targ_port);
1147 return;
1148 }
1149 port->port_type = msg->port.port_type;
1150 port->physical_port = msg->port.physical_port;
1151 port->virtual_port = msg->port.virtual_port;
1152 port->status = msg->port.status;
1153 i = 0;
1154 free(port->port_name, M_CTL);
1155 port->port_name = strndup(&msg->port.data[i], msg->port.name_len,
1156 M_CTL);
1157 i += msg->port.name_len;
1158 if (msg->port.lun_map_len != 0) {
1159 if (port->lun_map == NULL ||
1160 port->lun_map_size * sizeof(uint32_t) <
1161 msg->port.lun_map_len) {
1162 port->lun_map_size = 0;
1163 free(port->lun_map, M_CTL);
1164 port->lun_map = malloc(msg->port.lun_map_len,
1165 M_CTL, M_WAITOK);
1166 }
1167 memcpy(port->lun_map, &msg->port.data[i], msg->port.lun_map_len);
1168 port->lun_map_size = msg->port.lun_map_len / sizeof(uint32_t);
1169 i += msg->port.lun_map_len;
1170 } else {
1171 port->lun_map_size = 0;
1172 free(port->lun_map, M_CTL);
1173 port->lun_map = NULL;
1174 }
1175 if (msg->port.port_devid_len != 0) {
1176 if (port->port_devid == NULL ||
1177 port->port_devid->len < msg->port.port_devid_len) {
1178 free(port->port_devid, M_CTL);
1179 port->port_devid = malloc(sizeof(struct ctl_devid) +
1180 msg->port.port_devid_len, M_CTL, M_WAITOK);
1181 }
1182 memcpy(port->port_devid->data, &msg->port.data[i],
1183 msg->port.port_devid_len);
1184 port->port_devid->len = msg->port.port_devid_len;
1185 i += msg->port.port_devid_len;
1186 } else {
1187 free(port->port_devid, M_CTL);
1188 port->port_devid = NULL;
1189 }
1190 if (msg->port.target_devid_len != 0) {
1191 if (port->target_devid == NULL ||
1192 port->target_devid->len < msg->port.target_devid_len) {
1193 free(port->target_devid, M_CTL);
1194 port->target_devid = malloc(sizeof(struct ctl_devid) +
1195 msg->port.target_devid_len, M_CTL, M_WAITOK);
1196 }
1197 memcpy(port->target_devid->data, &msg->port.data[i],
1198 msg->port.target_devid_len);
1199 port->target_devid->len = msg->port.target_devid_len;
1200 i += msg->port.target_devid_len;
1201 } else {
1202 free(port->target_devid, M_CTL);
1203 port->target_devid = NULL;
1204 }
1205 if (msg->port.init_devid_len != 0) {
1206 if (port->init_devid == NULL ||
1207 port->init_devid->len < msg->port.init_devid_len) {
1208 free(port->init_devid, M_CTL);
1209 port->init_devid = malloc(sizeof(struct ctl_devid) +
1210 msg->port.init_devid_len, M_CTL, M_WAITOK);
1211 }
1212 memcpy(port->init_devid->data, &msg->port.data[i],
1213 msg->port.init_devid_len);
1214 port->init_devid->len = msg->port.init_devid_len;
1215 i += msg->port.init_devid_len;
1216 } else {
1217 free(port->init_devid, M_CTL);
1218 port->init_devid = NULL;
1219 }
1220 if (new) {
1221 if (ctl_port_register(port) != 0) {
1222 printf("%s: ctl_port_register() failed with error\n",
1223 __func__);
1224 }
1225 }
1226 mtx_lock(&softc->ctl_lock);
1227 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1228 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
1229 continue;
1230 mtx_lock(&lun->lun_lock);
1231 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
1232 mtx_unlock(&lun->lun_lock);
1233 }
1234 mtx_unlock(&softc->ctl_lock);
1235}
1236
1237static void
1238ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1239{
1240 struct ctl_port *port;
1241 int iid;
1242
1243 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1244 if (port == NULL) {
1245 printf("%s: Received IID for unknown port %d\n",
1246 __func__, msg->hdr.nexus.targ_port);
1247 return;
1248 }
1249 iid = msg->hdr.nexus.initid;
1250 port->wwpn_iid[iid].in_use = msg->iid.in_use;
1251 port->wwpn_iid[iid].wwpn = msg->iid.wwpn;
1252 free(port->wwpn_iid[iid].name, M_CTL);
1253 if (msg->iid.name_len) {
1254 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0],
1255 msg->iid.name_len, M_CTL);
1256 } else
1257 port->wwpn_iid[iid].name = NULL;
1258}
1259
1260static void
1261ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1262{
1263
1264 if (msg->login.version != CTL_HA_VERSION) {
1265 printf("CTL HA peers have different versions %d != %d\n",
1266 msg->login.version, CTL_HA_VERSION);
1267 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1268 return;
1269 }
1270 if (msg->login.ha_mode != softc->ha_mode) {
1271 printf("CTL HA peers have different ha_mode %d != %d\n",
1272 msg->login.ha_mode, softc->ha_mode);
1273 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1274 return;
1275 }
1276 if (msg->login.ha_id == softc->ha_id) {
1277 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id);
1278 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1279 return;
1280 }
1281 if (msg->login.max_luns != CTL_MAX_LUNS ||
1282 msg->login.max_ports != CTL_MAX_PORTS ||
1283 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) {
1284 printf("CTL HA peers have different limits\n");
1285 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1286 return;
1287 }
1288}
1289
1290static void
1291ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1292{
1293 struct ctl_lun *lun;
1294 u_int i;
1295 uint32_t initidx, targ_lun;
1296
1297 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1298 mtx_lock(&softc->ctl_lock);
1299 if (targ_lun >= CTL_MAX_LUNS ||
1300 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1301 mtx_unlock(&softc->ctl_lock);
1302 return;
1303 }
1304 mtx_lock(&lun->lun_lock);
1305 mtx_unlock(&softc->ctl_lock);
1306 if (lun->flags & CTL_LUN_DISABLED) {
1307 mtx_unlock(&lun->lun_lock);
1308 return;
1309 }
1310 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
1311 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
1312 msg->mode.page_code &&
1313 lun->mode_pages.index[i].subpage == msg->mode.subpage)
1314 break;
1315 }
1316 if (i == CTL_NUM_MODE_PAGES) {
1317 mtx_unlock(&lun->lun_lock);
1318 return;
1319 }
1320 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data,
1321 lun->mode_pages.index[i].page_len);
1322 initidx = ctl_get_initindex(&msg->hdr.nexus);
1323 if (initidx != -1)
1324 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
1325 mtx_unlock(&lun->lun_lock);
1326}
1327
1328/*
1329 * ISC (Inter Shelf Communication) event handler. Events from the HA
1330 * subsystem come in here.
1331 */
1332static void
1333ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
1334{
1335 struct ctl_softc *softc = control_softc;
1336 union ctl_io *io;
1337 struct ctl_prio *presio;
1338 ctl_ha_status isc_status;
1339
1340 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event));
1341 if (event == CTL_HA_EVT_MSG_RECV) {
1342 union ctl_ha_msg *msg, msgbuf;
1343
1344 if (param > sizeof(msgbuf))
1345 msg = malloc(param, M_CTL, M_WAITOK);
1346 else
1347 msg = &msgbuf;
1348 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param,
1349 M_WAITOK);
1350 if (isc_status != CTL_HA_STATUS_SUCCESS) {
1351 printf("%s: Error receiving message: %d\n",
1352 __func__, isc_status);
1353 if (msg != &msgbuf)
1354 free(msg, M_CTL);
1355 return;
1356 }
1357
1358 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type));
1359 switch (msg->hdr.msg_type) {
1360 case CTL_MSG_SERIALIZE:
1361 io = ctl_alloc_io(softc->othersc_pool);
1362 ctl_zero_io(io);
1363 // populate ctsio from msg
1364 io->io_hdr.io_type = CTL_IO_SCSI;
1365 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
1366 io->io_hdr.original_sc = msg->hdr.original_sc;
1367 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
1368 CTL_FLAG_IO_ACTIVE;
1369 /*
1370 * If we're in serialization-only mode, we don't
1371 * want to go through full done processing. Thus
1372 * the COPY flag.
1373 *
1374 * XXX KDM add another flag that is more specific.
1375 */
1376 if (softc->ha_mode != CTL_HA_MODE_XFER)
1377 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
1378 io->io_hdr.nexus = msg->hdr.nexus;
1379#if 0
1380 printf("port %u, iid %u, lun %u\n",
1381 io->io_hdr.nexus.targ_port,
1382 io->io_hdr.nexus.initid,
1383 io->io_hdr.nexus.targ_lun);
1384#endif
1385 io->scsiio.tag_num = msg->scsi.tag_num;
1386 io->scsiio.tag_type = msg->scsi.tag_type;
1387#ifdef CTL_TIME_IO
1388 io->io_hdr.start_time = time_uptime;
1389 getbinuptime(&io->io_hdr.start_bt);
1390#endif /* CTL_TIME_IO */
1391 io->scsiio.cdb_len = msg->scsi.cdb_len;
1392 memcpy(io->scsiio.cdb, msg->scsi.cdb,
1393 CTL_MAX_CDBLEN);
1394 if (softc->ha_mode == CTL_HA_MODE_XFER) {
1395 const struct ctl_cmd_entry *entry;
1396
1397 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
1398 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
1399 io->io_hdr.flags |=
1400 entry->flags & CTL_FLAG_DATA_MASK;
1401 }
1402 ctl_enqueue_isc(io);
1403 break;
1404
1405 /* Performed on the Originating SC, XFER mode only */
1406 case CTL_MSG_DATAMOVE: {
1407 struct ctl_sg_entry *sgl;
1408 int i, j;
1409
1410 io = msg->hdr.original_sc;
1411 if (io == NULL) {
1412 printf("%s: original_sc == NULL!\n", __func__);
1413 /* XXX KDM do something here */
1414 break;
1415 }
1416 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
1417 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1418 /*
1419 * Keep track of this, we need to send it back over
1420 * when the datamove is complete.
1421 */
1422 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1423 if (msg->hdr.status == CTL_SUCCESS)
1424 io->io_hdr.status = msg->hdr.status;
1425
1426 if (msg->dt.sg_sequence == 0) {
1427#ifdef CTL_TIME_IO
1428 getbinuptime(&io->io_hdr.dma_start_bt);
1429#endif
1430 i = msg->dt.kern_sg_entries +
1431 msg->dt.kern_data_len /
1432 CTL_HA_DATAMOVE_SEGMENT + 1;
1433 sgl = malloc(sizeof(*sgl) * i, M_CTL,
1434 M_WAITOK | M_ZERO);
1435 io->io_hdr.remote_sglist = sgl;
1436 io->io_hdr.local_sglist =
1437 &sgl[msg->dt.kern_sg_entries];
1438
1439 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
1440
1441 io->scsiio.kern_sg_entries =
1442 msg->dt.kern_sg_entries;
1443 io->scsiio.rem_sg_entries =
1444 msg->dt.kern_sg_entries;
1445 io->scsiio.kern_data_len =
1446 msg->dt.kern_data_len;
1447 io->scsiio.kern_total_len =
1448 msg->dt.kern_total_len;
1449 io->scsiio.kern_data_resid =
1450 msg->dt.kern_data_resid;
1451 io->scsiio.kern_rel_offset =
1452 msg->dt.kern_rel_offset;
1453 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR;
1454 io->io_hdr.flags |= msg->dt.flags &
1455 CTL_FLAG_BUS_ADDR;
1456 } else
1457 sgl = (struct ctl_sg_entry *)
1458 io->scsiio.kern_data_ptr;
1459
1460 for (i = msg->dt.sent_sg_entries, j = 0;
1461 i < (msg->dt.sent_sg_entries +
1462 msg->dt.cur_sg_entries); i++, j++) {
1463 sgl[i].addr = msg->dt.sg_list[j].addr;
1464 sgl[i].len = msg->dt.sg_list[j].len;
1465
1466#if 0
1467 printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n",
1468 __func__, sgl[i].addr, sgl[i].len, j, i);
1469#endif
1470 }
1471
1472 /*
1473 * If this is the last piece of the I/O, we've got
1474 * the full S/G list. Queue processing in the thread.
1475 * Otherwise wait for the next piece.
1476 */
1477 if (msg->dt.sg_last != 0)
1478 ctl_enqueue_isc(io);
1479 break;
1480 }
1481 /* Performed on the Serializing (primary) SC, XFER mode only */
1482 case CTL_MSG_DATAMOVE_DONE: {
1483 if (msg->hdr.serializing_sc == NULL) {
1484 printf("%s: serializing_sc == NULL!\n",
1485 __func__);
1486 /* XXX KDM now what? */
1487 break;
1488 }
1489 /*
1490 * We grab the sense information here in case
1491 * there was a failure, so we can return status
1492 * back to the initiator.
1493 */
1494 io = msg->hdr.serializing_sc;
1495 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
1496 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1497 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1498 io->io_hdr.port_status = msg->scsi.fetd_status;
1499 io->scsiio.residual = msg->scsi.residual;
1500 if (msg->hdr.status != CTL_STATUS_NONE) {
1501 io->io_hdr.status = msg->hdr.status;
1502 io->scsiio.scsi_status = msg->scsi.scsi_status;
1503 io->scsiio.sense_len = msg->scsi.sense_len;
1504 io->scsiio.sense_residual =msg->scsi.sense_residual;
1505 memcpy(&io->scsiio.sense_data,
1506 &msg->scsi.sense_data,
1507 msg->scsi.sense_len);
1508 if (msg->hdr.status == CTL_SUCCESS)
1509 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1510 }
1511 ctl_enqueue_isc(io);
1512 break;
1513 }
1514
1515 /* Preformed on Originating SC, SER_ONLY mode */
1516 case CTL_MSG_R2R:
1517 io = msg->hdr.original_sc;
1518 if (io == NULL) {
1519 printf("%s: original_sc == NULL!\n",
1520 __func__);
1521 break;
1522 }
1523 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1524 io->io_hdr.msg_type = CTL_MSG_R2R;
1525 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1526 ctl_enqueue_isc(io);
1527 break;
1528
1529 /*
1530 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
1531 * mode.
1532 * Performed on the Originating (i.e. secondary) SC in XFER
1533 * mode
1534 */
1535 case CTL_MSG_FINISH_IO:
1536 if (softc->ha_mode == CTL_HA_MODE_XFER)
1537 ctl_isc_handler_finish_xfer(softc, msg);
1538 else
1539 ctl_isc_handler_finish_ser_only(softc, msg);
1540 break;
1541
1542 /* Preformed on Originating SC */
1543 case CTL_MSG_BAD_JUJU:
1544 io = msg->hdr.original_sc;
1545 if (io == NULL) {
1546 printf("%s: Bad JUJU!, original_sc is NULL!\n",
1547 __func__);
1548 break;
1549 }
1550 ctl_copy_sense_data(msg, io);
1551 /*
1552 * IO should have already been cleaned up on other
1553 * SC so clear this flag so we won't send a message
1554 * back to finish the IO there.
1555 */
1556 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1557 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1558
1559 /* io = msg->hdr.serializing_sc; */
1560 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
1561 ctl_enqueue_isc(io);
1562 break;
1563
1564 /* Handle resets sent from the other side */
1565 case CTL_MSG_MANAGE_TASKS: {
1566 struct ctl_taskio *taskio;
1567 taskio = (struct ctl_taskio *)ctl_alloc_io(
1568 softc->othersc_pool);
1569 ctl_zero_io((union ctl_io *)taskio);
1570 taskio->io_hdr.io_type = CTL_IO_TASK;
1571 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1572 taskio->io_hdr.nexus = msg->hdr.nexus;
1573 taskio->task_action = msg->task.task_action;
1574 taskio->tag_num = msg->task.tag_num;
1575 taskio->tag_type = msg->task.tag_type;
1576#ifdef CTL_TIME_IO
1577 taskio->io_hdr.start_time = time_uptime;
1578 getbinuptime(&taskio->io_hdr.start_bt);
1579#endif /* CTL_TIME_IO */
1580 ctl_run_task((union ctl_io *)taskio);
1581 break;
1582 }
1583 /* Persistent Reserve action which needs attention */
1584 case CTL_MSG_PERS_ACTION:
1585 presio = (struct ctl_prio *)ctl_alloc_io(
1586 softc->othersc_pool);
1587 ctl_zero_io((union ctl_io *)presio);
1588 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
1589 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1590 presio->io_hdr.nexus = msg->hdr.nexus;
1591 presio->pr_msg = msg->pr;
1592 ctl_enqueue_isc((union ctl_io *)presio);
1593 break;
1594 case CTL_MSG_UA:
1595 ctl_isc_ua(softc, msg, param);
1596 break;
1597 case CTL_MSG_PORT_SYNC:
1598 ctl_isc_port_sync(softc, msg, param);
1599 break;
1600 case CTL_MSG_LUN_SYNC:
1601 ctl_isc_lun_sync(softc, msg, param);
1602 break;
1603 case CTL_MSG_IID_SYNC:
1604 ctl_isc_iid_sync(softc, msg, param);
1605 break;
1606 case CTL_MSG_LOGIN:
1607 ctl_isc_login(softc, msg, param);
1608 break;
1609 case CTL_MSG_MODE_SYNC:
1610 ctl_isc_mode_sync(softc, msg, param);
1611 break;
1612 default:
1613 printf("Received HA message of unknown type %d\n",
1614 msg->hdr.msg_type);
1615 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1616 break;
1617 }
1618 if (msg != &msgbuf)
1619 free(msg, M_CTL);
1620 } else if (event == CTL_HA_EVT_LINK_CHANGE) {
1621 printf("CTL: HA link status changed from %d to %d\n",
1622 softc->ha_link, param);
1623 if (param == softc->ha_link)
1624 return;
1625 if (softc->ha_link == CTL_HA_LINK_ONLINE) {
1626 softc->ha_link = param;
1627 ctl_isc_ha_link_down(softc);
1628 } else {
1629 softc->ha_link = param;
1630 if (softc->ha_link == CTL_HA_LINK_ONLINE)
1631 ctl_isc_ha_link_up(softc);
1632 }
1633 return;
1634 } else {
1635 printf("ctl_isc_event_handler: Unknown event %d\n", event);
1636 return;
1637 }
1638}
1639
1640static void
1641ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
1642{
1643
1644 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data,
1645 src->scsi.sense_len);
1646 dest->scsiio.scsi_status = src->scsi.scsi_status;
1647 dest->scsiio.sense_len = src->scsi.sense_len;
1648 dest->io_hdr.status = src->hdr.status;
1649}
1650
1651static void
1652ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest)
1653{
1654
1655 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data,
1656 src->scsiio.sense_len);
1657 dest->scsi.scsi_status = src->scsiio.scsi_status;
1658 dest->scsi.sense_len = src->scsiio.sense_len;
1659 dest->hdr.status = src->io_hdr.status;
1660}
1661
1662void
1663ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1664{
1665 struct ctl_softc *softc = lun->ctl_softc;
1666 ctl_ua_type *pu;
1667
1668 if (initidx < softc->init_min || initidx >= softc->init_max)
1669 return;
1670 mtx_assert(&lun->lun_lock, MA_OWNED);
1671 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1672 if (pu == NULL)
1673 return;
1674 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
1675}
1676
1677void
1678ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua)
1679{
1680 int i;
1681
1682 mtx_assert(&lun->lun_lock, MA_OWNED);
1683 if (lun->pending_ua[port] == NULL)
1684 return;
1685 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1686 if (port * CTL_MAX_INIT_PER_PORT + i == except)
1687 continue;
1688 lun->pending_ua[port][i] |= ua;
1689 }
1690}
1691
1692void
1693ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1694{
1695 struct ctl_softc *softc = lun->ctl_softc;
1696 int i;
1697
1698 mtx_assert(&lun->lun_lock, MA_OWNED);
1699 for (i = softc->port_min; i < softc->port_max; i++)
1700 ctl_est_ua_port(lun, i, except, ua);
1701}
1702
1703void
1704ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1705{
1706 struct ctl_softc *softc = lun->ctl_softc;
1707 ctl_ua_type *pu;
1708
1709 if (initidx < softc->init_min || initidx >= softc->init_max)
1710 return;
1711 mtx_assert(&lun->lun_lock, MA_OWNED);
1712 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1713 if (pu == NULL)
1714 return;
1715 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1716}
1717
1718void
1719ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1720{
1721 struct ctl_softc *softc = lun->ctl_softc;
1722 int i, j;
1723
1724 mtx_assert(&lun->lun_lock, MA_OWNED);
1725 for (i = softc->port_min; i < softc->port_max; i++) {
1726 if (lun->pending_ua[i] == NULL)
1727 continue;
1728 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1729 if (i * CTL_MAX_INIT_PER_PORT + j == except)
1730 continue;
1731 lun->pending_ua[i][j] &= ~ua;
1732 }
1733 }
1734}
1735
1736void
1737ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
1738 ctl_ua_type ua_type)
1739{
1740 struct ctl_lun *lun;
1741
1742 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
1743 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
1744 mtx_lock(&lun->lun_lock);
1745 ctl_clr_ua(lun, initidx, ua_type);
1746 mtx_unlock(&lun->lun_lock);
1747 }
1748}
1749
1750static int
1751ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)
1752{
1753 struct ctl_softc *softc = (struct ctl_softc *)arg1;
1754 struct ctl_lun *lun;
1755 struct ctl_lun_req ireq;
1756 int error, value;
1757
1758 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1;
1759 error = sysctl_handle_int(oidp, &value, 0, req);
1760 if ((error != 0) || (req->newptr == NULL))
1761 return (error);
1762
1763 mtx_lock(&softc->ctl_lock);
1764 if (value == 0)
1765 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1766 else
1767 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1768 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1769 mtx_unlock(&softc->ctl_lock);
1770 bzero(&ireq, sizeof(ireq));
1771 ireq.reqtype = CTL_LUNREQ_MODIFY;
1772 ireq.reqdata.modify.lun_id = lun->lun;
1773 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0,
1774 curthread);
1775 if (ireq.status != CTL_LUN_OK) {
1776 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n",
1777 __func__, ireq.status, ireq.error_str);
1778 }
1779 mtx_lock(&softc->ctl_lock);
1780 }
1781 mtx_unlock(&softc->ctl_lock);
1782 return (0);
1783}
1784
1785static int
1786ctl_init(void)
1787{
1788 struct make_dev_args args;
1789 struct ctl_softc *softc;
1790 void *other_pool;
1791 int i, error;
1792
1793 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1794 M_WAITOK | M_ZERO);
1795
1796 make_dev_args_init(&args);
1797 args.mda_devsw = &ctl_cdevsw;
1798 args.mda_uid = UID_ROOT;
1799 args.mda_gid = GID_OPERATOR;
1800 args.mda_mode = 0600;
1801 args.mda_si_drv1 = softc;
1802 error = make_dev_s(&args, &softc->dev, "cam/ctl");
1803 if (error != 0) {
1804 free(softc, M_DEVBUF);
1805 control_softc = NULL;
1806 return (error);
1807 }
1808
1809 sysctl_ctx_init(&softc->sysctl_ctx);
1810 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1811 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1812 CTLFLAG_RD, 0, "CAM Target Layer");
1813
1814 if (softc->sysctl_tree == NULL) {
1815 printf("%s: unable to allocate sysctl tree\n", __func__);
1816 destroy_dev(softc->dev);
1817 free(softc, M_DEVBUF);
1818 control_softc = NULL;
1819 return (ENOMEM);
1820 }
1821
1822 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1823 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1824 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1825 softc->flags = 0;
1826
1827 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1828 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0,
1829 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)");
1830
1831 /*
1832 * In Copan's HA scheme, the "master" and "slave" roles are
1833 * figured out through the slot the controller is in. Although it
1834 * is an active/active system, someone has to be in charge.
1835 */
1836 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1837 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1838 "HA head ID (0 - no HA)");
1839 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) {
1840 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1841 softc->is_single = 1;
1842 softc->port_cnt = CTL_MAX_PORTS;
1843 softc->port_min = 0;
1844 } else {
1845 softc->port_cnt = CTL_MAX_PORTS / NUM_HA_SHELVES;
1846 softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
1847 }
1848 softc->port_max = softc->port_min + softc->port_cnt;
1849 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
1850 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
1851
1852 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1853 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
1854 "HA link state (0 - offline, 1 - unknown, 2 - online)");
1855
1856 STAILQ_INIT(&softc->lun_list);
1857 STAILQ_INIT(&softc->pending_lun_queue);
1858 STAILQ_INIT(&softc->fe_list);
1859 STAILQ_INIT(&softc->port_list);
1860 STAILQ_INIT(&softc->be_list);
1861 ctl_tpc_init(softc);
1862
1863 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
1864 &other_pool) != 0)
1865 {
1866 printf("ctl: can't allocate %d entry other SC pool, "
1867 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1868 return (ENOMEM);
1869 }
1870 softc->othersc_pool = other_pool;
1871
1872 if (worker_threads <= 0)
1873 worker_threads = max(1, mp_ncpus / 4);
1874 if (worker_threads > CTL_MAX_THREADS)
1875 worker_threads = CTL_MAX_THREADS;
1876
1877 for (i = 0; i < worker_threads; i++) {
1878 struct ctl_thread *thr = &softc->threads[i];
1879
1880 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1881 thr->ctl_softc = softc;
1882 STAILQ_INIT(&thr->incoming_queue);
1883 STAILQ_INIT(&thr->rtr_queue);
1884 STAILQ_INIT(&thr->done_queue);
1885 STAILQ_INIT(&thr->isc_queue);
1886
1887 error = kproc_kthread_add(ctl_work_thread, thr,
1888 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1889 if (error != 0) {
1890 printf("error creating CTL work thread!\n");
1891 ctl_pool_free(other_pool);
1892 return (error);
1893 }
1894 }
1895 error = kproc_kthread_add(ctl_lun_thread, softc,
1896 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1897 if (error != 0) {
1898 printf("error creating CTL lun thread!\n");
1899 ctl_pool_free(other_pool);
1900 return (error);
1901 }
1902 error = kproc_kthread_add(ctl_thresh_thread, softc,
1903 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh");
1904 if (error != 0) {
1905 printf("error creating CTL threshold thread!\n");
1906 ctl_pool_free(other_pool);
1907 return (error);
1908 }
1909
1910 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1911 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN,
1912 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
1913
1914 if (softc->is_single == 0) {
1915 ctl_frontend_register(&ha_frontend);
1916 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
1917 printf("ctl_init: ctl_ha_msg_init failed.\n");
1918 softc->is_single = 1;
1919 } else
1920 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
1921 != CTL_HA_STATUS_SUCCESS) {
1922 printf("ctl_init: ctl_ha_msg_register failed.\n");
1923 softc->is_single = 1;
1924 }
1925 }
1926 return (0);
1927}
1928
1929void
1930ctl_shutdown(void)
1931{
1932 struct ctl_softc *softc = control_softc;
1933 struct ctl_lun *lun, *next_lun;
1934
1935 if (softc->is_single == 0) {
1936 ctl_ha_msg_shutdown(softc);
1937 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL)
1938 != CTL_HA_STATUS_SUCCESS)
1939 printf("%s: ctl_ha_msg_deregister failed.\n", __func__);
1940 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS)
1941 printf("%s: ctl_ha_msg_destroy failed.\n", __func__);
1942 ctl_frontend_deregister(&ha_frontend);
1943 }
1944
1945 mtx_lock(&softc->ctl_lock);
1946
1947 STAILQ_FOREACH_SAFE(lun, &softc->lun_list, links, next_lun)
1948 ctl_free_lun(lun);
1949
1950 mtx_unlock(&softc->ctl_lock);
1951
1952#if 0
1953 ctl_shutdown_thread(softc->work_thread);
1954 mtx_destroy(&softc->queue_lock);
1955#endif
1956
1957 ctl_tpc_shutdown(softc);
1958 uma_zdestroy(softc->io_zone);
1959 mtx_destroy(&softc->ctl_lock);
1960
1961 destroy_dev(softc->dev);
1962
1963 sysctl_ctx_free(&softc->sysctl_ctx);
1964
1965 free(softc, M_DEVBUF);
1966 control_softc = NULL;
1967}
1968
1969static int
1970ctl_module_event_handler(module_t mod, int what, void *arg)
1971{
1972
1973 switch (what) {
1974 case MOD_LOAD:
1975 return (ctl_init());
1976 case MOD_UNLOAD:
1977 return (EBUSY);
1978 default:
1979 return (EOPNOTSUPP);
1980 }
1981}
1982
1983/*
1984 * XXX KDM should we do some access checks here? Bump a reference count to
1985 * prevent a CTL module from being unloaded while someone has it open?
1986 */
1987static int
1988ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1989{
1990 return (0);
1991}
1992
1993static int
1994ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1995{
1996 return (0);
1997}
1998
1999/*
2000 * Remove an initiator by port number and initiator ID.
2001 * Returns 0 for success, -1 for failure.
2002 */
2003int
2004ctl_remove_initiator(struct ctl_port *port, int iid)
2005{
2006 struct ctl_softc *softc = port->ctl_softc;
2007
2008 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2009
2010 if (iid > CTL_MAX_INIT_PER_PORT) {
2011 printf("%s: initiator ID %u > maximun %u!\n",
2012 __func__, iid, CTL_MAX_INIT_PER_PORT);
2013 return (-1);
2014 }
2015
2016 mtx_lock(&softc->ctl_lock);
2017 port->wwpn_iid[iid].in_use--;
2018 port->wwpn_iid[iid].last_use = time_uptime;
2019 mtx_unlock(&softc->ctl_lock);
2020 ctl_isc_announce_iid(port, iid);
2021
2022 return (0);
2023}
2024
2025/*
2026 * Add an initiator to the initiator map.
2027 * Returns iid for success, < 0 for failure.
2028 */
2029int
2030ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
2031{
2032 struct ctl_softc *softc = port->ctl_softc;
2033 time_t best_time;
2034 int i, best;
2035
2036 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2037
2038 if (iid >= CTL_MAX_INIT_PER_PORT) {
2039 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
2040 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
2041 free(name, M_CTL);
2042 return (-1);
2043 }
2044
2045 mtx_lock(&softc->ctl_lock);
2046
2047 if (iid < 0 && (wwpn != 0 || name != NULL)) {
2048 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2049 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
2050 iid = i;
2051 break;
2052 }
2053 if (name != NULL && port->wwpn_iid[i].name != NULL &&
2054 strcmp(name, port->wwpn_iid[i].name) == 0) {
2055 iid = i;
2056 break;
2057 }
2058 }
2059 }
2060
2061 if (iid < 0) {
2062 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2063 if (port->wwpn_iid[i].in_use == 0 &&
2064 port->wwpn_iid[i].wwpn == 0 &&
2065 port->wwpn_iid[i].name == NULL) {
2066 iid = i;
2067 break;
2068 }
2069 }
2070 }
2071
2072 if (iid < 0) {
2073 best = -1;
2074 best_time = INT32_MAX;
2075 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2076 if (port->wwpn_iid[i].in_use == 0) {
2077 if (port->wwpn_iid[i].last_use < best_time) {
2078 best = i;
2079 best_time = port->wwpn_iid[i].last_use;
2080 }
2081 }
2082 }
2083 iid = best;
2084 }
2085
2086 if (iid < 0) {
2087 mtx_unlock(&softc->ctl_lock);
2088 free(name, M_CTL);
2089 return (-2);
2090 }
2091
2092 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
2093 /*
2094 * This is not an error yet.
2095 */
2096 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
2097#if 0
2098 printf("%s: port %d iid %u WWPN %#jx arrived"
2099 " again\n", __func__, port->targ_port,
2100 iid, (uintmax_t)wwpn);
2101#endif
2102 goto take;
2103 }
2104 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
2105 strcmp(name, port->wwpn_iid[iid].name) == 0) {
2106#if 0
2107 printf("%s: port %d iid %u name '%s' arrived"
2108 " again\n", __func__, port->targ_port,
2109 iid, name);
2110#endif
2111 goto take;
2112 }
2113
2114 /*
2115 * This is an error, but what do we do about it? The
2116 * driver is telling us we have a new WWPN for this
2117 * initiator ID, so we pretty much need to use it.
2118 */
2119 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
2120 " but WWPN %#jx '%s' is still at that address\n",
2121 __func__, port->targ_port, iid, wwpn, name,
2122 (uintmax_t)port->wwpn_iid[iid].wwpn,
2123 port->wwpn_iid[iid].name);
2124
2125 /*
2126 * XXX KDM clear have_ca and ua_pending on each LUN for
2127 * this initiator.
2128 */
2129 }
2130take:
2131 free(port->wwpn_iid[iid].name, M_CTL);
2132 port->wwpn_iid[iid].name = name;
2133 port->wwpn_iid[iid].wwpn = wwpn;
2134 port->wwpn_iid[iid].in_use++;
2135 mtx_unlock(&softc->ctl_lock);
2136 ctl_isc_announce_iid(port, iid);
2137
2138 return (iid);
2139}
2140
2141static int
2142ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
2143{
2144 int len;
2145
2146 switch (port->port_type) {
2147 case CTL_PORT_FC:
2148 {
2149 struct scsi_transportid_fcp *id =
2150 (struct scsi_transportid_fcp *)buf;
2151 if (port->wwpn_iid[iid].wwpn == 0)
2152 return (0);
2153 memset(id, 0, sizeof(*id));
2154 id->format_protocol = SCSI_PROTO_FC;
2155 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
2156 return (sizeof(*id));
2157 }
2158 case CTL_PORT_ISCSI:
2159 {
2160 struct scsi_transportid_iscsi_port *id =
2161 (struct scsi_transportid_iscsi_port *)buf;
2162 if (port->wwpn_iid[iid].name == NULL)
2163 return (0);
2164 memset(id, 0, 256);
2165 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
2166 SCSI_PROTO_ISCSI;
2167 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
2168 len = roundup2(min(len, 252), 4);
2169 scsi_ulto2b(len, id->additional_length);
2170 return (sizeof(*id) + len);
2171 }
2172 case CTL_PORT_SAS:
2173 {
2174 struct scsi_transportid_sas *id =
2175 (struct scsi_transportid_sas *)buf;
2176 if (port->wwpn_iid[iid].wwpn == 0)
2177 return (0);
2178 memset(id, 0, sizeof(*id));
2179 id->format_protocol = SCSI_PROTO_SAS;
2180 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
2181 return (sizeof(*id));
2182 }
2183 default:
2184 {
2185 struct scsi_transportid_spi *id =
2186 (struct scsi_transportid_spi *)buf;
2187 memset(id, 0, sizeof(*id));
2188 id->format_protocol = SCSI_PROTO_SPI;
2189 scsi_ulto2b(iid, id->scsi_addr);
2190 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
2191 return (sizeof(*id));
2192 }
2193 }
2194}
2195
2196/*
2197 * Serialize a command that went down the "wrong" side, and so was sent to
2198 * this controller for execution. The logic is a little different than the
2199 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
2200 * sent back to the other side, but in the success case, we execute the
2201 * command on this side (XFER mode) or tell the other side to execute it
2202 * (SER_ONLY mode).
2203 */
2204static void
2205ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
2206{
2207 struct ctl_softc *softc = CTL_SOFTC(ctsio);
2208 struct ctl_port *port = CTL_PORT(ctsio);
2209 union ctl_ha_msg msg_info;
2210 struct ctl_lun *lun;
2211 const struct ctl_cmd_entry *entry;
2212 uint32_t targ_lun;
2213
2214 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
2215
2216 /* Make sure that we know about this port. */
2217 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) {
2218 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2219 /*retry_count*/ 1);
2220 goto badjuju;
2221 }
2222
2223 /* Make sure that we know about this LUN. */
2224 mtx_lock(&softc->ctl_lock);
2225 if (targ_lun >= CTL_MAX_LUNS ||
2226 (lun = softc->ctl_luns[targ_lun]) == NULL) {
2227 mtx_unlock(&softc->ctl_lock);
2228
2229 /*
2230 * The other node would not send this request to us unless
2231 * received announce that we are primary node for this LUN.
2232 * If this LUN does not exist now, it is probably result of
2233 * a race, so respond to initiator in the most opaque way.
2234 */
2235 ctl_set_busy(ctsio);
2236 goto badjuju;
2237 }
2238 mtx_lock(&lun->lun_lock);
2239 mtx_unlock(&softc->ctl_lock);
2240
2241 /*
2242 * If the LUN is invalid, pretend that it doesn't exist.
2243 * It will go away as soon as all pending I/Os completed.
2244 */
2245 if (lun->flags & CTL_LUN_DISABLED) {
2246 mtx_unlock(&lun->lun_lock);
2247 ctl_set_busy(ctsio);
2248 goto badjuju;
2249 }
2250
2251 entry = ctl_get_cmd_entry(ctsio, NULL);
2252 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
2253 mtx_unlock(&lun->lun_lock);
2254 goto badjuju;
2255 }
2256
2257 CTL_LUN(ctsio) = lun;
2258 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
2259
2260 /*
2261 * Every I/O goes into the OOA queue for a
2262 * particular LUN, and stays there until completion.
2263 */
2264#ifdef CTL_TIME_IO
2265 if (TAILQ_EMPTY(&lun->ooa_queue))
2266 lun->idle_time += getsbinuptime() - lun->last_busy;
2267#endif
2268 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2269
2270 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
2271 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
2272 ooa_links))) {
2273 case CTL_ACTION_BLOCK:
2274 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
2275 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
2276 blocked_links);
2277 mtx_unlock(&lun->lun_lock);
2278 break;
2279 case CTL_ACTION_PASS:
2280 case CTL_ACTION_SKIP:
2281 if (softc->ha_mode == CTL_HA_MODE_XFER) {
2282 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
2283 ctl_enqueue_rtr((union ctl_io *)ctsio);
2284 mtx_unlock(&lun->lun_lock);
2285 } else {
2286 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
2287 mtx_unlock(&lun->lun_lock);
2288
2289 /* send msg back to other side */
2290 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2291 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
2292 msg_info.hdr.msg_type = CTL_MSG_R2R;
2293 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2294 sizeof(msg_info.hdr), M_WAITOK);
2295 }
2296 break;
2297 case CTL_ACTION_OVERLAP:
2298 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2299 mtx_unlock(&lun->lun_lock);
2300 ctl_set_overlapped_cmd(ctsio);
2301 goto badjuju;
2302 case CTL_ACTION_OVERLAP_TAG:
2303 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2304 mtx_unlock(&lun->lun_lock);
2305 ctl_set_overlapped_tag(ctsio, ctsio->tag_num);
2306 goto badjuju;
2307 case CTL_ACTION_ERROR:
2308 default:
2309 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2310 mtx_unlock(&lun->lun_lock);
2311
2312 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2313 /*retry_count*/ 0);
2314badjuju:
2315 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
2316 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2317 msg_info.hdr.serializing_sc = NULL;
2318 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
2319 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2320 sizeof(msg_info.scsi), M_WAITOK);
2321 ctl_free_io((union ctl_io *)ctsio);
2322 break;
2323 }
2324}
2325
2326/*
2327 * Returns 0 for success, errno for failure.
2328 */
2329static void
2330ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2331 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2332{
2333 union ctl_io *io;
2334
2335 mtx_lock(&lun->lun_lock);
2336 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2337 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2338 ooa_links)) {
2339 struct ctl_ooa_entry *entry;
2340
2341 /*
2342 * If we've got more than we can fit, just count the
2343 * remaining entries.
2344 */
2345 if (*cur_fill_num >= ooa_hdr->alloc_num)
2346 continue;
2347
2348 entry = &kern_entries[*cur_fill_num];
2349
2350 entry->tag_num = io->scsiio.tag_num;
2351 entry->lun_num = lun->lun;
2352#ifdef CTL_TIME_IO
2353 entry->start_bt = io->io_hdr.start_bt;
2354#endif
2355 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2356 entry->cdb_len = io->scsiio.cdb_len;
2357 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2358 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2359
2360 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2361 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2362
2363 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2364 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2365
2366 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2367 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2368
2369 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2370 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2371 }
2372 mtx_unlock(&lun->lun_lock);
2373}
2374
2375static void *
2376ctl_copyin_alloc(void *user_addr, unsigned int len, char *error_str,
2377 size_t error_str_len)
2378{
2379 void *kptr;
2380
2381 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2382
2383 if (copyin(user_addr, kptr, len) != 0) {
2384 snprintf(error_str, error_str_len, "Error copying %d bytes "
2385 "from user address %p to kernel address %p", len,
2386 user_addr, kptr);
2387 free(kptr, M_CTL);
2388 return (NULL);
2389 }
2390
2391 return (kptr);
2392}
2393
2394static void
2395ctl_free_args(int num_args, struct ctl_be_arg *args)
2396{
2397 int i;
2398
2399 if (args == NULL)
2400 return;
2401
2402 for (i = 0; i < num_args; i++) {
2403 free(args[i].kname, M_CTL);
2404 free(args[i].kvalue, M_CTL);
2405 }
2406
2407 free(args, M_CTL);
2408}
2409
2410static struct ctl_be_arg *
2411ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2412 char *error_str, size_t error_str_len)
2413{
2414 struct ctl_be_arg *args;
2415 int i;
2416
2417 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2418 error_str, error_str_len);
2419
2420 if (args == NULL)
2421 goto bailout;
2422
2423 for (i = 0; i < num_args; i++) {
2424 args[i].kname = NULL;
2425 args[i].kvalue = NULL;
2426 }
2427
2428 for (i = 0; i < num_args; i++) {
2429 uint8_t *tmpptr;
2430
2431 if (args[i].namelen == 0) {
2432 snprintf(error_str, error_str_len, "Argument %d "
2433 "name length is zero", i);
2434 goto bailout;
2435 }
2436
2437 args[i].kname = ctl_copyin_alloc(args[i].name,
2438 args[i].namelen, error_str, error_str_len);
2439 if (args[i].kname == NULL)
2440 goto bailout;
2441
2442 if (args[i].kname[args[i].namelen - 1] != '\0') {
2443 snprintf(error_str, error_str_len, "Argument %d "
2444 "name is not NUL-terminated", i);
2445 goto bailout;
2446 }
2447
2448 if (args[i].flags & CTL_BEARG_RD) {
2449 if (args[i].vallen == 0) {
2450 snprintf(error_str, error_str_len, "Argument %d "
2451 "value length is zero", i);
2452 goto bailout;
2453 }
2454
2455 tmpptr = ctl_copyin_alloc(args[i].value,
2456 args[i].vallen, error_str, error_str_len);
2457 if (tmpptr == NULL)
2458 goto bailout;
2459
2460 if ((args[i].flags & CTL_BEARG_ASCII)
2461 && (tmpptr[args[i].vallen - 1] != '\0')) {
2462 snprintf(error_str, error_str_len, "Argument "
2463 "%d value is not NUL-terminated", i);
2464 free(tmpptr, M_CTL);
2465 goto bailout;
2466 }
2467 args[i].kvalue = tmpptr;
2468 } else {
2469 args[i].kvalue = malloc(args[i].vallen,
2470 M_CTL, M_WAITOK | M_ZERO);
2471 }
2472 }
2473
2474 return (args);
2475bailout:
2476
2477 ctl_free_args(num_args, args);
2478
2479 return (NULL);
2480}
2481
2482static void
2483ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2484{
2485 int i;
2486
2487 for (i = 0; i < num_args; i++) {
2488 if (args[i].flags & CTL_BEARG_WR)
2489 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2490 }
2491}
2492
2493/*
2494 * Escape characters that are illegal or not recommended in XML.
2495 */
2496int
2497ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2498{
2499 char *end = str + size;
2500 int retval;
2501
2502 retval = 0;
2503
2504 for (; *str && str < end; str++) {
2505 switch (*str) {
2506 case '&':
2507 retval = sbuf_printf(sb, "&amp;");
2508 break;
2509 case '>':
2510 retval = sbuf_printf(sb, "&gt;");
2511 break;
2512 case '<':
2513 retval = sbuf_printf(sb, "&lt;");
2514 break;
2515 default:
2516 retval = sbuf_putc(sb, *str);
2517 break;
2518 }
2519
2520 if (retval != 0)
2521 break;
2522
2523 }
2524
2525 return (retval);
2526}
2527
2528static void
2529ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2530{
2531 struct scsi_vpd_id_descriptor *desc;
2532 int i;
2533
2534 if (id == NULL || id->len < 4)
2535 return;
2536 desc = (struct scsi_vpd_id_descriptor *)id->data;
2537 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2538 case SVPD_ID_TYPE_T10:
2539 sbuf_printf(sb, "t10.");
2540 break;
2541 case SVPD_ID_TYPE_EUI64:
2542 sbuf_printf(sb, "eui.");
2543 break;
2544 case SVPD_ID_TYPE_NAA:
2545 sbuf_printf(sb, "naa.");
2546 break;
2547 case SVPD_ID_TYPE_SCSI_NAME:
2548 break;
2549 }
2550 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2551 case SVPD_ID_CODESET_BINARY:
2552 for (i = 0; i < desc->length; i++)
2553 sbuf_printf(sb, "%02x", desc->identifier[i]);
2554 break;
2555 case SVPD_ID_CODESET_ASCII:
2556 sbuf_printf(sb, "%.*s", (int)desc->length,
2557 (char *)desc->identifier);
2558 break;
2559 case SVPD_ID_CODESET_UTF8:
2560 sbuf_printf(sb, "%s", (char *)desc->identifier);
2561 break;
2562 }
2563}
2564
2565static int
2566ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2567 struct thread *td)
2568{
2569 struct ctl_softc *softc = dev->si_drv1;
2570 struct ctl_lun *lun;
2571 int retval;
2572
2573 retval = 0;
2574
2575 switch (cmd) {
2576 case CTL_IO:
2577 retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2578 break;
2579 case CTL_ENABLE_PORT:
2580 case CTL_DISABLE_PORT:
2581 case CTL_SET_PORT_WWNS: {
2582 struct ctl_port *port;
2583 struct ctl_port_entry *entry;
2584
2585 entry = (struct ctl_port_entry *)addr;
2586
2587 mtx_lock(&softc->ctl_lock);
2588 STAILQ_FOREACH(port, &softc->port_list, links) {
2589 int action, done;
2590
2591 if (port->targ_port < softc->port_min ||
2592 port->targ_port >= softc->port_max)
2593 continue;
2594
2595 action = 0;
2596 done = 0;
2597 if ((entry->port_type == CTL_PORT_NONE)
2598 && (entry->targ_port == port->targ_port)) {
2599 /*
2600 * If the user only wants to enable or
2601 * disable or set WWNs on a specific port,
2602 * do the operation and we're done.
2603 */
2604 action = 1;
2605 done = 1;
2606 } else if (entry->port_type & port->port_type) {
2607 /*
2608 * Compare the user's type mask with the
2609 * particular frontend type to see if we
2610 * have a match.
2611 */
2612 action = 1;
2613 done = 0;
2614
2615 /*
2616 * Make sure the user isn't trying to set
2617 * WWNs on multiple ports at the same time.
2618 */
2619 if (cmd == CTL_SET_PORT_WWNS) {
2620 printf("%s: Can't set WWNs on "
2621 "multiple ports\n", __func__);
2622 retval = EINVAL;
2623 break;
2624 }
2625 }
2626 if (action == 0)
2627 continue;
2628
2629 /*
2630 * XXX KDM we have to drop the lock here, because
2631 * the online/offline operations can potentially
2632 * block. We need to reference count the frontends
2633 * so they can't go away,
2634 */
2635 if (cmd == CTL_ENABLE_PORT) {
2636 mtx_unlock(&softc->ctl_lock);
2637 ctl_port_online(port);
2638 mtx_lock(&softc->ctl_lock);
2639 } else if (cmd == CTL_DISABLE_PORT) {
2640 mtx_unlock(&softc->ctl_lock);
2641 ctl_port_offline(port);
2642 mtx_lock(&softc->ctl_lock);
2643 } else if (cmd == CTL_SET_PORT_WWNS) {
2644 ctl_port_set_wwns(port,
2645 (entry->flags & CTL_PORT_WWNN_VALID) ?
2646 1 : 0, entry->wwnn,
2647 (entry->flags & CTL_PORT_WWPN_VALID) ?
2648 1 : 0, entry->wwpn);
2649 }
2650 if (done != 0)
2651 break;
2652 }
2653 mtx_unlock(&softc->ctl_lock);
2654 break;
2655 }
2656 case CTL_GET_OOA: {
2657 struct ctl_ooa *ooa_hdr;
2658 struct ctl_ooa_entry *entries;
2659 uint32_t cur_fill_num;
2660
2661 ooa_hdr = (struct ctl_ooa *)addr;
2662
2663 if ((ooa_hdr->alloc_len == 0)
2664 || (ooa_hdr->alloc_num == 0)) {
2665 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2666 "must be non-zero\n", __func__,
2667 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2668 retval = EINVAL;
2669 break;
2670 }
2671
2672 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2673 sizeof(struct ctl_ooa_entry))) {
2674 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2675 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2676 __func__, ooa_hdr->alloc_len,
2677 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2678 retval = EINVAL;
2679 break;
2680 }
2681
2682 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2683 if (entries == NULL) {
2684 printf("%s: could not allocate %d bytes for OOA "
2685 "dump\n", __func__, ooa_hdr->alloc_len);
2686 retval = ENOMEM;
2687 break;
2688 }
2689
2690 mtx_lock(&softc->ctl_lock);
2691 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 &&
2692 (ooa_hdr->lun_num >= CTL_MAX_LUNS ||
2693 softc->ctl_luns[ooa_hdr->lun_num] == NULL)) {
2694 mtx_unlock(&softc->ctl_lock);
2695 free(entries, M_CTL);
2696 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2697 __func__, (uintmax_t)ooa_hdr->lun_num);
2698 retval = EINVAL;
2699 break;
2700 }
2701
2702 cur_fill_num = 0;
2703
2704 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2705 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2706 ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2707 ooa_hdr, entries);
2708 }
2709 } else {
2710 lun = softc->ctl_luns[ooa_hdr->lun_num];
2711 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr,
2712 entries);
2713 }
2714 mtx_unlock(&softc->ctl_lock);
2715
2716 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2717 ooa_hdr->fill_len = ooa_hdr->fill_num *
2718 sizeof(struct ctl_ooa_entry);
2719 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2720 if (retval != 0) {
2721 printf("%s: error copying out %d bytes for OOA dump\n",
2722 __func__, ooa_hdr->fill_len);
2723 }
2724
2725 getbinuptime(&ooa_hdr->cur_bt);
2726
2727 if (cur_fill_num > ooa_hdr->alloc_num) {
2728 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2729 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2730 } else {
2731 ooa_hdr->dropped_num = 0;
2732 ooa_hdr->status = CTL_OOA_OK;
2733 }
2734
2735 free(entries, M_CTL);
2736 break;
2737 }
2738 case CTL_DELAY_IO: {
2739 struct ctl_io_delay_info *delay_info;
2740
2741 delay_info = (struct ctl_io_delay_info *)addr;
2742
2743#ifdef CTL_IO_DELAY
2744 mtx_lock(&softc->ctl_lock);
2745 if (delay_info->lun_id >= CTL_MAX_LUNS ||
2746 (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) {
2747 mtx_unlock(&softc->ctl_lock);
2748 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2749 break;
2750 }
2751 mtx_lock(&lun->lun_lock);
2752 mtx_unlock(&softc->ctl_lock);
2753 delay_info->status = CTL_DELAY_STATUS_OK;
2754 switch (delay_info->delay_type) {
2755 case CTL_DELAY_TYPE_CONT:
2756 case CTL_DELAY_TYPE_ONESHOT:
2757 break;
2758 default:
2759 delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE;
2760 break;
2761 }
2762 switch (delay_info->delay_loc) {
2763 case CTL_DELAY_LOC_DATAMOVE:
2764 lun->delay_info.datamove_type = delay_info->delay_type;
2765 lun->delay_info.datamove_delay = delay_info->delay_secs;
2766 break;
2767 case CTL_DELAY_LOC_DONE:
2768 lun->delay_info.done_type = delay_info->delay_type;
2769 lun->delay_info.done_delay = delay_info->delay_secs;
2770 break;
2771 default:
2772 delay_info->status = CTL_DELAY_STATUS_INVALID_LOC;
2773 break;
2774 }
2775 mtx_unlock(&lun->lun_lock);
2776#else
2777 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2778#endif /* CTL_IO_DELAY */
2779 break;
2780 }
2781 case CTL_GETSTATS: {
2782 struct ctl_stats *stats;
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 i = 0;
2798 STAILQ_FOREACH(lun, &softc->lun_list, 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
2819 err_desc = (struct ctl_error_desc *)addr;
2820
2821 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2822 M_WAITOK | M_ZERO);
2823 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2824
2825 mtx_lock(&softc->ctl_lock);
2826 if (err_desc->lun_id >= CTL_MAX_LUNS ||
2827 (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) {
2828 mtx_unlock(&softc->ctl_lock);
2829 free(new_err_desc, M_CTL);
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 mtx_lock(&lun->lun_lock);
2836 mtx_unlock(&softc->ctl_lock);
2837
2838 /*
2839 * We could do some checking here to verify the validity
2840 * of the request, but given the complexity of error
2841 * injection requests, the checking logic would be fairly
2842 * complex.
2843 *
2844 * For now, if the request is invalid, it just won't get
2845 * executed and might get deleted.
2846 */
2847 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2848
2849 /*
2850 * XXX KDM check to make sure the serial number is unique,
2851 * in case we somehow manage to wrap. That shouldn't
2852 * happen for a very long time, but it's the right thing to
2853 * do.
2854 */
2855 new_err_desc->serial = lun->error_serial;
2856 err_desc->serial = lun->error_serial;
2857 lun->error_serial++;
2858
2859 mtx_unlock(&lun->lun_lock);
2860 break;
2861 }
2862 case CTL_ERROR_INJECT_DELETE: {
2863 struct ctl_error_desc *delete_desc, *desc, *desc2;
2864 int delete_done;
2865
2866 delete_desc = (struct ctl_error_desc *)addr;
2867 delete_done = 0;
2868
2869 mtx_lock(&softc->ctl_lock);
2870 if (delete_desc->lun_id >= CTL_MAX_LUNS ||
2871 (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) {
2872 mtx_unlock(&softc->ctl_lock);
2873 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2874 __func__, (uintmax_t)delete_desc->lun_id);
2875 retval = EINVAL;
2876 break;
2877 }
2878 mtx_lock(&lun->lun_lock);
2879 mtx_unlock(&softc->ctl_lock);
2880 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2881 if (desc->serial != delete_desc->serial)
2882 continue;
2883
2884 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2885 links);
2886 free(desc, M_CTL);
2887 delete_done = 1;
2888 }
2889 mtx_unlock(&lun->lun_lock);
2890 if (delete_done == 0) {
2891 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2892 "error serial %ju on LUN %u\n", __func__,
2893 delete_desc->serial, delete_desc->lun_id);
2894 retval = EINVAL;
2895 break;
2896 }
2897 break;
2898 }
2899 case CTL_DUMP_STRUCTS: {
2900 int j, k;
2901 struct ctl_port *port;
2902 struct ctl_frontend *fe;
2903
2904 mtx_lock(&softc->ctl_lock);
2905 printf("CTL Persistent Reservation information start:\n");
2906 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2907 mtx_lock(&lun->lun_lock);
2908 if ((lun->flags & CTL_LUN_DISABLED) != 0) {
2909 mtx_unlock(&lun->lun_lock);
2910 continue;
2911 }
2912
2913 for (j = 0; j < CTL_MAX_PORTS; j++) {
2914 if (lun->pr_keys[j] == NULL)
2915 continue;
2916 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2917 if (lun->pr_keys[j][k] == 0)
2918 continue;
2919 printf(" LUN %ju port %d iid %d key "
2920 "%#jx\n", lun->lun, j, k,
2921 (uintmax_t)lun->pr_keys[j][k]);
2922 }
2923 }
2924 mtx_unlock(&lun->lun_lock);
2925 }
2926 printf("CTL Persistent Reservation information end\n");
2927 printf("CTL Ports:\n");
2928 STAILQ_FOREACH(port, &softc->port_list, links) {
2929 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2930 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
2931 port->frontend->name, port->port_type,
2932 port->physical_port, port->virtual_port,
2933 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2934 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2935 if (port->wwpn_iid[j].in_use == 0 &&
2936 port->wwpn_iid[j].wwpn == 0 &&
2937 port->wwpn_iid[j].name == NULL)
2938 continue;
2939
2940 printf(" iid %u use %d WWPN %#jx '%s'\n",
2941 j, port->wwpn_iid[j].in_use,
2942 (uintmax_t)port->wwpn_iid[j].wwpn,
2943 port->wwpn_iid[j].name);
2944 }
2945 }
2946 printf("CTL Port information end\n");
2947 mtx_unlock(&softc->ctl_lock);
2948 /*
2949 * XXX KDM calling this without a lock. We'd likely want
2950 * to drop the lock before calling the frontend's dump
2951 * routine anyway.
2952 */
2953 printf("CTL Frontends:\n");
2954 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2955 printf(" Frontend '%s'\n", fe->name);
2956 if (fe->fe_dump != NULL)
2957 fe->fe_dump();
2958 }
2959 printf("CTL Frontend information end\n");
2960 break;
2961 }
2962 case CTL_LUN_REQ: {
2963 struct ctl_lun_req *lun_req;
2964 struct ctl_backend_driver *backend;
2965
2966 lun_req = (struct ctl_lun_req *)addr;
2967
2968 backend = ctl_backend_find(lun_req->backend);
2969 if (backend == NULL) {
2970 lun_req->status = CTL_LUN_ERROR;
2971 snprintf(lun_req->error_str,
2972 sizeof(lun_req->error_str),
2973 "Backend \"%s\" not found.",
2974 lun_req->backend);
2975 break;
2976 }
2977 if (lun_req->num_be_args > 0) {
2978 lun_req->kern_be_args = ctl_copyin_args(
2979 lun_req->num_be_args,
2980 lun_req->be_args,
2981 lun_req->error_str,
2982 sizeof(lun_req->error_str));
2983 if (lun_req->kern_be_args == NULL) {
2984 lun_req->status = CTL_LUN_ERROR;
2985 break;
2986 }
2987 }
2988
2989 retval = backend->ioctl(dev, cmd, addr, flag, td);
2990
2991 if (lun_req->num_be_args > 0) {
2992 ctl_copyout_args(lun_req->num_be_args,
2993 lun_req->kern_be_args);
2994 ctl_free_args(lun_req->num_be_args,
2995 lun_req->kern_be_args);
2996 }
2997 break;
2998 }
2999 case CTL_LUN_LIST: {
3000 struct sbuf *sb;
3001 struct ctl_lun_list *list;
3002 struct ctl_option *opt;
3003
3004 list = (struct ctl_lun_list *)addr;
3005
3006 /*
3007 * Allocate a fixed length sbuf here, based on the length
3008 * of the user's buffer. We could allocate an auto-extending
3009 * buffer, and then tell the user how much larger our
3010 * amount of data is than his buffer, but that presents
3011 * some problems:
3012 *
3013 * 1. The sbuf(9) routines use a blocking malloc, and so
3014 * we can't hold a lock while calling them with an
3015 * auto-extending buffer.
3016 *
3017 * 2. There is not currently a LUN reference counting
3018 * mechanism, outside of outstanding transactions on
3019 * the LUN's OOA queue. So a LUN could go away on us
3020 * while we're getting the LUN number, backend-specific
3021 * information, etc. Thus, given the way things
3022 * currently work, we need to hold the CTL lock while
3023 * grabbing LUN information.
3024 *
3025 * So, from the user's standpoint, the best thing to do is
3026 * allocate what he thinks is a reasonable buffer length,
3027 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3028 * double the buffer length and try again. (And repeat
3029 * that until he succeeds.)
3030 */
3031 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3032 if (sb == NULL) {
3033 list->status = CTL_LUN_LIST_ERROR;
3034 snprintf(list->error_str, sizeof(list->error_str),
3035 "Unable to allocate %d bytes for LUN list",
3036 list->alloc_len);
3037 break;
3038 }
3039
3040 sbuf_printf(sb, "<ctllunlist>\n");
3041
3042 mtx_lock(&softc->ctl_lock);
3043 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3044 mtx_lock(&lun->lun_lock);
3045 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3046 (uintmax_t)lun->lun);
3047
3048 /*
3049 * Bail out as soon as we see that we've overfilled
3050 * the buffer.
3051 */
3052 if (retval != 0)
3053 break;
3054
3055 retval = sbuf_printf(sb, "\t<backend_type>%s"
3056 "</backend_type>\n",
3057 (lun->backend == NULL) ? "none" :
3058 lun->backend->name);
3059
3060 if (retval != 0)
3061 break;
3062
3063 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3064 lun->be_lun->lun_type);
3065
3066 if (retval != 0)
3067 break;
3068
3069 if (lun->backend == NULL) {
3070 retval = sbuf_printf(sb, "</lun>\n");
3071 if (retval != 0)
3072 break;
3073 continue;
3074 }
3075
3076 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3077 (lun->be_lun->maxlba > 0) ?
3078 lun->be_lun->maxlba + 1 : 0);
3079
3080 if (retval != 0)
3081 break;
3082
3083 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3084 lun->be_lun->blocksize);
3085
3086 if (retval != 0)
3087 break;
3088
3089 retval = sbuf_printf(sb, "\t<serial_number>");
3090
3091 if (retval != 0)
3092 break;
3093
3094 retval = ctl_sbuf_printf_esc(sb,
3095 lun->be_lun->serial_num,
3096 sizeof(lun->be_lun->serial_num));
3097
3098 if (retval != 0)
3099 break;
3100
3101 retval = sbuf_printf(sb, "</serial_number>\n");
3102
3103 if (retval != 0)
3104 break;
3105
3106 retval = sbuf_printf(sb, "\t<device_id>");
3107
3108 if (retval != 0)
3109 break;
3110
3111 retval = ctl_sbuf_printf_esc(sb,
3112 lun->be_lun->device_id,
3113 sizeof(lun->be_lun->device_id));
3114
3115 if (retval != 0)
3116 break;
3117
3118 retval = sbuf_printf(sb, "</device_id>\n");
3119
3120 if (retval != 0)
3121 break;
3122
3123 if (lun->backend->lun_info != NULL) {
3124 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3125 if (retval != 0)
3126 break;
3127 }
3128 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3129 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3130 opt->name, opt->value, opt->name);
3131 if (retval != 0)
3132 break;
3133 }
3134
3135 retval = sbuf_printf(sb, "</lun>\n");
3136
3137 if (retval != 0)
3138 break;
3139 mtx_unlock(&lun->lun_lock);
3140 }
3141 if (lun != NULL)
3142 mtx_unlock(&lun->lun_lock);
3143 mtx_unlock(&softc->ctl_lock);
3144
3145 if ((retval != 0)
3146 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3147 retval = 0;
3148 sbuf_delete(sb);
3149 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3150 snprintf(list->error_str, sizeof(list->error_str),
3151 "Out of space, %d bytes is too small",
3152 list->alloc_len);
3153 break;
3154 }
3155
3156 sbuf_finish(sb);
3157
3158 retval = copyout(sbuf_data(sb), list->lun_xml,
3159 sbuf_len(sb) + 1);
3160
3161 list->fill_len = sbuf_len(sb) + 1;
3162 list->status = CTL_LUN_LIST_OK;
3163 sbuf_delete(sb);
3164 break;
3165 }
3166 case CTL_ISCSI: {
3167 struct ctl_iscsi *ci;
3168 struct ctl_frontend *fe;
3169
3170 ci = (struct ctl_iscsi *)addr;
3171
3172 fe = ctl_frontend_find("iscsi");
3173 if (fe == NULL) {
3174 ci->status = CTL_ISCSI_ERROR;
3175 snprintf(ci->error_str, sizeof(ci->error_str),
3176 "Frontend \"iscsi\" not found.");
3177 break;
3178 }
3179
3180 retval = fe->ioctl(dev, cmd, addr, flag, td);
3181 break;
3182 }
3183 case CTL_PORT_REQ: {
3184 struct ctl_req *req;
3185 struct ctl_frontend *fe;
3186
3187 req = (struct ctl_req *)addr;
3188
3189 fe = ctl_frontend_find(req->driver);
3190 if (fe == NULL) {
3191 req->status = CTL_LUN_ERROR;
3192 snprintf(req->error_str, sizeof(req->error_str),
3193 "Frontend \"%s\" not found.", req->driver);
3194 break;
3195 }
3196 if (req->num_args > 0) {
3197 req->kern_args = ctl_copyin_args(req->num_args,
3198 req->args, req->error_str, sizeof(req->error_str));
3199 if (req->kern_args == NULL) {
3200 req->status = CTL_LUN_ERROR;
3201 break;
3202 }
3203 }
3204
3205 if (fe->ioctl)
3206 retval = fe->ioctl(dev, cmd, addr, flag, td);
3207 else
3208 retval = ENODEV;
3209
3210 if (req->num_args > 0) {
3211 ctl_copyout_args(req->num_args, req->kern_args);
3212 ctl_free_args(req->num_args, req->kern_args);
3213 }
3214 break;
3215 }
3216 case CTL_PORT_LIST: {
3217 struct sbuf *sb;
3218 struct ctl_port *port;
3219 struct ctl_lun_list *list;
3220 struct ctl_option *opt;
3221 int j;
3222 uint32_t plun;
3223
3224 list = (struct ctl_lun_list *)addr;
3225
3226 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3227 if (sb == NULL) {
3228 list->status = CTL_LUN_LIST_ERROR;
3229 snprintf(list->error_str, sizeof(list->error_str),
3230 "Unable to allocate %d bytes for LUN list",
3231 list->alloc_len);
3232 break;
3233 }
3234
3235 sbuf_printf(sb, "<ctlportlist>\n");
3236
3237 mtx_lock(&softc->ctl_lock);
3238 STAILQ_FOREACH(port, &softc->port_list, links) {
3239 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3240 (uintmax_t)port->targ_port);
3241
3242 /*
3243 * Bail out as soon as we see that we've overfilled
3244 * the buffer.
3245 */
3246 if (retval != 0)
3247 break;
3248
3249 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3250 "</frontend_type>\n", port->frontend->name);
3251 if (retval != 0)
3252 break;
3253
3254 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3255 port->port_type);
3256 if (retval != 0)
3257 break;
3258
3259 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3260 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3261 if (retval != 0)
3262 break;
3263
3264 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3265 port->port_name);
3266 if (retval != 0)
3267 break;
3268
3269 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3270 port->physical_port);
3271 if (retval != 0)
3272 break;
3273
3274 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3275 port->virtual_port);
3276 if (retval != 0)
3277 break;
3278
3279 if (port->target_devid != NULL) {
3280 sbuf_printf(sb, "\t<target>");
3281 ctl_id_sbuf(port->target_devid, sb);
3282 sbuf_printf(sb, "</target>\n");
3283 }
3284
3285 if (port->port_devid != NULL) {
3286 sbuf_printf(sb, "\t<port>");
3287 ctl_id_sbuf(port->port_devid, sb);
3288 sbuf_printf(sb, "</port>\n");
3289 }
3290
3291 if (port->port_info != NULL) {
3292 retval = port->port_info(port->onoff_arg, sb);
3293 if (retval != 0)
3294 break;
3295 }
3296 STAILQ_FOREACH(opt, &port->options, links) {
3297 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3298 opt->name, opt->value, opt->name);
3299 if (retval != 0)
3300 break;
3301 }
3302
3303 if (port->lun_map != NULL) {
3304 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3305 for (j = 0; j < port->lun_map_size; j++) {
3306 plun = ctl_lun_map_from_port(port, j);
3307 if (plun == UINT32_MAX)
3308 continue;
3309 sbuf_printf(sb,
3310 "\t<lun id=\"%u\">%u</lun>\n",
3311 j, plun);
3312 }
3313 }
3314
3315 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3316 if (port->wwpn_iid[j].in_use == 0 ||
3317 (port->wwpn_iid[j].wwpn == 0 &&
3318 port->wwpn_iid[j].name == NULL))
3319 continue;
3320
3321 if (port->wwpn_iid[j].name != NULL)
3322 retval = sbuf_printf(sb,
3323 "\t<initiator id=\"%u\">%s</initiator>\n",
3324 j, port->wwpn_iid[j].name);
3325 else
3326 retval = sbuf_printf(sb,
3327 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3328 j, port->wwpn_iid[j].wwpn);
3329 if (retval != 0)
3330 break;
3331 }
3332 if (retval != 0)
3333 break;
3334
3335 retval = sbuf_printf(sb, "</targ_port>\n");
3336 if (retval != 0)
3337 break;
3338 }
3339 mtx_unlock(&softc->ctl_lock);
3340
3341 if ((retval != 0)
3342 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3343 retval = 0;
3344 sbuf_delete(sb);
3345 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3346 snprintf(list->error_str, sizeof(list->error_str),
3347 "Out of space, %d bytes is too small",
3348 list->alloc_len);
3349 break;
3350 }
3351
3352 sbuf_finish(sb);
3353
3354 retval = copyout(sbuf_data(sb), list->lun_xml,
3355 sbuf_len(sb) + 1);
3356
3357 list->fill_len = sbuf_len(sb) + 1;
3358 list->status = CTL_LUN_LIST_OK;
3359 sbuf_delete(sb);
3360 break;
3361 }
3362 case CTL_LUN_MAP: {
3363 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3364 struct ctl_port *port;
3365
3366 mtx_lock(&softc->ctl_lock);
3367 if (lm->port < softc->port_min ||
3368 lm->port >= softc->port_max ||
3369 (port = softc->ctl_ports[lm->port]) == NULL) {
3370 mtx_unlock(&softc->ctl_lock);
3371 return (ENXIO);
3372 }
3373 if (port->status & CTL_PORT_STATUS_ONLINE) {
3374 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3375 if (ctl_lun_map_to_port(port, lun->lun) ==
3376 UINT32_MAX)
3377 continue;
3378 mtx_lock(&lun->lun_lock);
3379 ctl_est_ua_port(lun, lm->port, -1,
3380 CTL_UA_LUN_CHANGE);
3381 mtx_unlock(&lun->lun_lock);
3382 }
3383 }
3384 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3385 if (lm->plun != UINT32_MAX) {
3386 if (lm->lun == UINT32_MAX)
3387 retval = ctl_lun_map_unset(port, lm->plun);
3388 else if (lm->lun < CTL_MAX_LUNS &&
3389 softc->ctl_luns[lm->lun] != NULL)
3390 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3391 else
3392 return (ENXIO);
3393 } else {
3394 if (lm->lun == UINT32_MAX)
3395 retval = ctl_lun_map_deinit(port);
3396 else
3397 retval = ctl_lun_map_init(port);
3398 }
3399 if (port->status & CTL_PORT_STATUS_ONLINE)
3400 ctl_isc_announce_port(port);
3401 break;
3402 }
3403 default: {
3404 /* XXX KDM should we fix this? */
3405#if 0
3406 struct ctl_backend_driver *backend;
3407 unsigned int type;
3408 int found;
3409
3410 found = 0;
3411
3412 /*
3413 * We encode the backend type as the ioctl type for backend
3414 * ioctls. So parse it out here, and then search for a
3415 * backend of this type.
3416 */
3417 type = _IOC_TYPE(cmd);
3418
3419 STAILQ_FOREACH(backend, &softc->be_list, links) {
3420 if (backend->type == type) {
3421 found = 1;
3422 break;
3423 }
3424 }
3425 if (found == 0) {
3426 printf("ctl: unknown ioctl command %#lx or backend "
3427 "%d\n", cmd, type);
3428 retval = EINVAL;
3429 break;
3430 }
3431 retval = backend->ioctl(dev, cmd, addr, flag, td);
3432#endif
3433 retval = ENOTTY;
3434 break;
3435 }
3436 }
3437 return (retval);
3438}
3439
3440uint32_t
3441ctl_get_initindex(struct ctl_nexus *nexus)
3442{
3443 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3444}
3445
3446int
3447ctl_lun_map_init(struct ctl_port *port)
3448{
3449 struct ctl_softc *softc = port->ctl_softc;
3450 struct ctl_lun *lun;
3451 int size = ctl_lun_map_size;
3452 uint32_t i;
3453
3454 if (port->lun_map == NULL || port->lun_map_size < size) {
3455 port->lun_map_size = 0;
3456 free(port->lun_map, M_CTL);
3457 port->lun_map = malloc(size * sizeof(uint32_t),
3458 M_CTL, M_NOWAIT);
3459 }
3460 if (port->lun_map == NULL)
3461 return (ENOMEM);
3462 for (i = 0; i < size; i++)
3463 port->lun_map[i] = UINT32_MAX;
3464 port->lun_map_size = size;
3465 if (port->status & CTL_PORT_STATUS_ONLINE) {
3466 if (port->lun_disable != NULL) {
3467 STAILQ_FOREACH(lun, &softc->lun_list, links)
3468 port->lun_disable(port->targ_lun_arg, lun->lun);
3469 }
3470 ctl_isc_announce_port(port);
3471 }
3472 return (0);
3473}
3474
3475int
3476ctl_lun_map_deinit(struct ctl_port *port)
3477{
3478 struct ctl_softc *softc = port->ctl_softc;
3479 struct ctl_lun *lun;
3480
3481 if (port->lun_map == NULL)
3482 return (0);
3483 port->lun_map_size = 0;
3484 free(port->lun_map, M_CTL);
3485 port->lun_map = NULL;
3486 if (port->status & CTL_PORT_STATUS_ONLINE) {
3487 if (port->lun_enable != NULL) {
3488 STAILQ_FOREACH(lun, &softc->lun_list, links)
3489 port->lun_enable(port->targ_lun_arg, lun->lun);
3490 }
3491 ctl_isc_announce_port(port);
3492 }
3493 return (0);
3494}
3495
3496int
3497ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3498{
3499 int status;
3500 uint32_t old;
3501
3502 if (port->lun_map == NULL) {
3503 status = ctl_lun_map_init(port);
3504 if (status != 0)
3505 return (status);
3506 }
3507 if (plun >= port->lun_map_size)
3508 return (EINVAL);
3509 old = port->lun_map[plun];
3510 port->lun_map[plun] = glun;
3511 if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) {
3512 if (port->lun_enable != NULL)
3513 port->lun_enable(port->targ_lun_arg, plun);
3514 ctl_isc_announce_port(port);
3515 }
3516 return (0);
3517}
3518
3519int
3520ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3521{
3522 uint32_t old;
3523
3524 if (port->lun_map == NULL || plun >= port->lun_map_size)
3525 return (0);
3526 old = port->lun_map[plun];
3527 port->lun_map[plun] = UINT32_MAX;
3528 if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) {
3529 if (port->lun_disable != NULL)
3530 port->lun_disable(port->targ_lun_arg, plun);
3531 ctl_isc_announce_port(port);
3532 }
3533 return (0);
3534}
3535
3536uint32_t
3537ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3538{
3539
3540 if (port == NULL)
3541 return (UINT32_MAX);
3542 if (port->lun_map == NULL)
3543 return (lun_id);
3544 if (lun_id > port->lun_map_size)
3545 return (UINT32_MAX);
3546 return (port->lun_map[lun_id]);
3547}
3548
3549uint32_t
3550ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3551{
3552 uint32_t i;
3553
3554 if (port == NULL)
3555 return (UINT32_MAX);
3556 if (port->lun_map == NULL)
3557 return (lun_id);
3558 for (i = 0; i < port->lun_map_size; i++) {
3559 if (port->lun_map[i] == lun_id)
3560 return (i);
3561 }
3562 return (UINT32_MAX);
3563}
3564
3565uint32_t
3566ctl_decode_lun(uint64_t encoded)
3567{
3568 uint8_t lun[8];
3569 uint32_t result = 0xffffffff;
3570
3571 be64enc(lun, encoded);
3572 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) {
3573 case RPL_LUNDATA_ATYP_PERIPH:
3574 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 &&
3575 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0)
3576 result = lun[1];
3577 break;
3578 case RPL_LUNDATA_ATYP_FLAT:
3579 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 &&
3580 lun[6] == 0 && lun[7] == 0)
3581 result = ((lun[0] & 0x3f) << 8) + lun[1];
3582 break;
3583 case RPL_LUNDATA_ATYP_EXTLUN:
3584 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) {
3585 case 0x02:
3586 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) {
3587 case 0x00:
3588 result = lun[1];
3589 break;
3590 case 0x10:
3591 result = (lun[1] << 16) + (lun[2] << 8) +
3592 lun[3];
3593 break;
3594 case 0x20:
3595 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0)
3596 result = (lun[2] << 24) +
3597 (lun[3] << 16) + (lun[4] << 8) +
3598 lun[5];
3599 break;
3600 }
3601 break;
3602 case RPL_LUNDATA_EXT_EAM_NOT_SPEC:
3603 result = 0xffffffff;
3604 break;
3605 }
3606 break;
3607 }
3608 return (result);
3609}
3610
3611uint64_t
3612ctl_encode_lun(uint32_t decoded)
3613{
3614 uint64_t l = decoded;
3615
3616 if (l <= 0xff)
3617 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48));
3618 if (l <= 0x3fff)
3619 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48));
3620 if (l <= 0xffffff)
3621 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) |
3622 (l << 32));
3623 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16));
3624}
3625
3626int
3627ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3628{
3629 int i;
3630
3631 for (i = first; i < last; i++) {
3632 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3633 return (i);
3634 }
3635 return (-1);
3636}
3637
3638int
3639ctl_set_mask(uint32_t *mask, uint32_t bit)
3640{
3641 uint32_t chunk, piece;
3642
3643 chunk = bit >> 5;
3644 piece = bit % (sizeof(uint32_t) * 8);
3645
3646 if ((mask[chunk] & (1 << piece)) != 0)
3647 return (-1);
3648 else
3649 mask[chunk] |= (1 << piece);
3650
3651 return (0);
3652}
3653
3654int
3655ctl_clear_mask(uint32_t *mask, uint32_t bit)
3656{
3657 uint32_t chunk, piece;
3658
3659 chunk = bit >> 5;
3660 piece = bit % (sizeof(uint32_t) * 8);
3661
3662 if ((mask[chunk] & (1 << piece)) == 0)
3663 return (-1);
3664 else
3665 mask[chunk] &= ~(1 << piece);
3666
3667 return (0);
3668}
3669
3670int
3671ctl_is_set(uint32_t *mask, uint32_t bit)
3672{
3673 uint32_t chunk, piece;
3674
3675 chunk = bit >> 5;
3676 piece = bit % (sizeof(uint32_t) * 8);
3677
3678 if ((mask[chunk] & (1 << piece)) == 0)
3679 return (0);
3680 else
3681 return (1);
3682}
3683
3684static uint64_t
3685ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3686{
3687 uint64_t *t;
3688
3689 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3690 if (t == NULL)
3691 return (0);
3692 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3693}
3694
3695static void
3696ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3697{
3698 uint64_t *t;
3699
3700 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3701 if (t == NULL)
3702 return;
3703 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3704}
3705
3706static void
3707ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3708{
3709 uint64_t *p;
3710 u_int i;
3711
3712 i = residx/CTL_MAX_INIT_PER_PORT;
3713 if (lun->pr_keys[i] != NULL)
3714 return;
3715 mtx_unlock(&lun->lun_lock);
3716 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3717 M_WAITOK | M_ZERO);
3718 mtx_lock(&lun->lun_lock);
3719 if (lun->pr_keys[i] == NULL)
3720 lun->pr_keys[i] = p;
3721 else
3722 free(p, M_CTL);
3723}
3724
3725static void
3726ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3727{
3728 uint64_t *t;
3729
3730 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3731 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3732 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3733}
3734
3735/*
3736 * ctl_softc, pool_name, total_ctl_io are passed in.
3737 * npool is passed out.
3738 */
3739int
3740ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3741 uint32_t total_ctl_io, void **npool)
3742{
3743 struct ctl_io_pool *pool;
3744
3745 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3746 M_NOWAIT | M_ZERO);
3747 if (pool == NULL)
3748 return (ENOMEM);
3749
3750 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3751 pool->ctl_softc = ctl_softc;
3752#ifdef IO_POOLS
3753 pool->zone = uma_zsecond_create(pool->name, NULL,
3754 NULL, NULL, NULL, ctl_softc->io_zone);
3755 /* uma_prealloc(pool->zone, total_ctl_io); */
3756#else
3757 pool->zone = ctl_softc->io_zone;
3758#endif
3759
3760 *npool = pool;
3761 return (0);
3762}
3763
3764void
3765ctl_pool_free(struct ctl_io_pool *pool)
3766{
3767
3768 if (pool == NULL)
3769 return;
3770
3771#ifdef IO_POOLS
3772 uma_zdestroy(pool->zone);
3773#endif
3774 free(pool, M_CTL);
3775}
3776
3777union ctl_io *
3778ctl_alloc_io(void *pool_ref)
3779{
3780 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3781 union ctl_io *io;
3782
3783 io = uma_zalloc(pool->zone, M_WAITOK);
3784 if (io != NULL) {
3785 io->io_hdr.pool = pool_ref;
3786 CTL_SOFTC(io) = pool->ctl_softc;
3787 }
3788 return (io);
3789}
3790
3791union ctl_io *
3792ctl_alloc_io_nowait(void *pool_ref)
3793{
3794 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3795 union ctl_io *io;
3796
3797 io = uma_zalloc(pool->zone, M_NOWAIT);
3798 if (io != NULL) {
3799 io->io_hdr.pool = pool_ref;
3800 CTL_SOFTC(io) = pool->ctl_softc;
3801 }
3802 return (io);
3803}
3804
3805void
3806ctl_free_io(union ctl_io *io)
3807{
3808 struct ctl_io_pool *pool;
3809
3810 if (io == NULL)
3811 return;
3812
3813 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3814 uma_zfree(pool->zone, io);
3815}
3816
3817void
3818ctl_zero_io(union ctl_io *io)
3819{
3820 struct ctl_io_pool *pool;
3821
3822 if (io == NULL)
3823 return;
3824
3825 /*
3826 * May need to preserve linked list pointers at some point too.
3827 */
3828 pool = io->io_hdr.pool;
3829 memset(io, 0, sizeof(*io));
3830 io->io_hdr.pool = pool;
3831 CTL_SOFTC(io) = pool->ctl_softc;
3832}
3833
3834int
3835ctl_expand_number(const char *buf, uint64_t *num)
3836{
3837 char *endptr;
3838 uint64_t number;
3839 unsigned shift;
3840
3841 number = strtoq(buf, &endptr, 0);
3842
3843 switch (tolower((unsigned char)*endptr)) {
3844 case 'e':
3845 shift = 60;
3846 break;
3847 case 'p':
3848 shift = 50;
3849 break;
3850 case 't':
3851 shift = 40;
3852 break;
3853 case 'g':
3854 shift = 30;
3855 break;
3856 case 'm':
3857 shift = 20;
3858 break;
3859 case 'k':
3860 shift = 10;
3861 break;
3862 case 'b':
3863 case '\0': /* No unit. */
3864 *num = number;
3865 return (0);
3866 default:
3867 /* Unrecognized unit. */
3868 return (-1);
3869 }
3870
3871 if ((number << shift) >> shift != number) {
3872 /* Overflow */
3873 return (-1);
3874 }
3875 *num = number << shift;
3876 return (0);
3877}
3878
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, page_code;
3888 struct ctl_page_index *page_index;
3889 const char *value;
3890 uint64_t ival;
3891
3892 memcpy(&lun->mode_pages.index, page_index_template,
3893 sizeof(page_index_template));
3894
3895 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3896
3897 page_index = &lun->mode_pages.index[i];
3898 if (lun->be_lun->lun_type == T_DIRECT &&
3899 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
3900 continue;
3901 if (lun->be_lun->lun_type == T_PROCESSOR &&
3902 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
3903 continue;
3904 if (lun->be_lun->lun_type == T_CDROM &&
3905 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
3906 continue;
3907
3908 page_code = page_index->page_code & SMPH_PC_MASK;
3909 switch (page_code) {
3910 case SMS_RW_ERROR_RECOVERY_PAGE: {
3911 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
3912 ("subpage %#x for page %#x is incorrect!",
3913 page_index->subpage, page_code));
3914 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
3915 &rw_er_page_default,
3916 sizeof(rw_er_page_default));
3917 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
3918 &rw_er_page_changeable,
3919 sizeof(rw_er_page_changeable));
3920 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
3921 &rw_er_page_default,
3922 sizeof(rw_er_page_default));
3923 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
3924 &rw_er_page_default,
3925 sizeof(rw_er_page_default));
3926 page_index->page_data =
3927 (uint8_t *)lun->mode_pages.rw_er_page;
3928 break;
3929 }
3930 case SMS_FORMAT_DEVICE_PAGE: {
3931 struct scsi_format_page *format_page;
3932
3933 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
3934 ("subpage %#x for page %#x is incorrect!",
3935 page_index->subpage, page_code));
3936
3937 /*
3938 * Sectors per track are set above. Bytes per
3939 * sector need to be set here on a per-LUN basis.
3940 */
3941 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3942 &format_page_default,
3943 sizeof(format_page_default));
3944 memcpy(&lun->mode_pages.format_page[
3945 CTL_PAGE_CHANGEABLE], &format_page_changeable,
3946 sizeof(format_page_changeable));
3947 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3948 &format_page_default,
3949 sizeof(format_page_default));
3950 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3951 &format_page_default,
3952 sizeof(format_page_default));
3953
3954 format_page = &lun->mode_pages.format_page[
3955 CTL_PAGE_CURRENT];
3956 scsi_ulto2b(lun->be_lun->blocksize,
3957 format_page->bytes_per_sector);
3958
3959 format_page = &lun->mode_pages.format_page[
3960 CTL_PAGE_DEFAULT];
3961 scsi_ulto2b(lun->be_lun->blocksize,
3962 format_page->bytes_per_sector);
3963
3964 format_page = &lun->mode_pages.format_page[
3965 CTL_PAGE_SAVED];
3966 scsi_ulto2b(lun->be_lun->blocksize,
3967 format_page->bytes_per_sector);
3968
3969 page_index->page_data =
3970 (uint8_t *)lun->mode_pages.format_page;
3971 break;
3972 }
3973 case SMS_RIGID_DISK_PAGE: {
3974 struct scsi_rigid_disk_page *rigid_disk_page;
3975 uint32_t sectors_per_cylinder;
3976 uint64_t cylinders;
3977#ifndef __XSCALE__
3978 int shift;
3979#endif /* !__XSCALE__ */
3980
3981 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
3982 ("subpage %#x for page %#x is incorrect!",
3983 page_index->subpage, page_code));
3984
3985 /*
3986 * Rotation rate and sectors per track are set
3987 * above. We calculate the cylinders here based on
3988 * capacity. Due to the number of heads and
3989 * sectors per track we're using, smaller arrays
3990 * may turn out to have 0 cylinders. Linux and
3991 * FreeBSD don't pay attention to these mode pages
3992 * to figure out capacity, but Solaris does. It
3993 * seems to deal with 0 cylinders just fine, and
3994 * works out a fake geometry based on the capacity.
3995 */
3996 memcpy(&lun->mode_pages.rigid_disk_page[
3997 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
3998 sizeof(rigid_disk_page_default));
3999 memcpy(&lun->mode_pages.rigid_disk_page[
4000 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4001 sizeof(rigid_disk_page_changeable));
4002
4003 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4004 CTL_DEFAULT_HEADS;
4005
4006 /*
4007 * The divide method here will be more accurate,
4008 * probably, but results in floating point being
4009 * used in the kernel on i386 (__udivdi3()). On the
4010 * XScale, though, __udivdi3() is implemented in
4011 * software.
4012 *
4013 * The shift method for cylinder calculation is
4014 * accurate if sectors_per_cylinder is a power of
4015 * 2. Otherwise it might be slightly off -- you
4016 * might have a bit of a truncation problem.
4017 */
4018#ifdef __XSCALE__
4019 cylinders = (lun->be_lun->maxlba + 1) /
4020 sectors_per_cylinder;
4021#else
4022 for (shift = 31; shift > 0; shift--) {
4023 if (sectors_per_cylinder & (1 << shift))
4024 break;
4025 }
4026 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4027#endif
4028
4029 /*
4030 * We've basically got 3 bytes, or 24 bits for the
4031 * cylinder size in the mode page. If we're over,
4032 * just round down to 2^24.
4033 */
4034 if (cylinders > 0xffffff)
4035 cylinders = 0xffffff;
4036
4037 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4038 CTL_PAGE_DEFAULT];
4039 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4040
4041 if ((value = ctl_get_opt(&lun->be_lun->options,
4042 "rpm")) != NULL) {
4043 scsi_ulto2b(strtol(value, NULL, 0),
4044 rigid_disk_page->rotation_rate);
4045 }
4046
4047 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4048 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4049 sizeof(rigid_disk_page_default));
4050 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4051 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4052 sizeof(rigid_disk_page_default));
4053
4054 page_index->page_data =
4055 (uint8_t *)lun->mode_pages.rigid_disk_page;
4056 break;
4057 }
4058 case SMS_VERIFY_ERROR_RECOVERY_PAGE: {
4059 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4060 ("subpage %#x for page %#x is incorrect!",
4061 page_index->subpage, page_code));
4062 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT],
4063 &verify_er_page_default,
4064 sizeof(verify_er_page_default));
4065 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE],
4066 &verify_er_page_changeable,
4067 sizeof(verify_er_page_changeable));
4068 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT],
4069 &verify_er_page_default,
4070 sizeof(verify_er_page_default));
4071 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED],
4072 &verify_er_page_default,
4073 sizeof(verify_er_page_default));
4074 page_index->page_data =
4075 (uint8_t *)lun->mode_pages.verify_er_page;
4076 break;
4077 }
4078 case SMS_CACHING_PAGE: {
4079 struct scsi_caching_page *caching_page;
4080
4081 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4082 ("subpage %#x for page %#x is incorrect!",
4083 page_index->subpage, page_code));
4084 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4085 &caching_page_default,
4086 sizeof(caching_page_default));
4087 memcpy(&lun->mode_pages.caching_page[
4088 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4089 sizeof(caching_page_changeable));
4090 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4091 &caching_page_default,
4092 sizeof(caching_page_default));
4093 caching_page = &lun->mode_pages.caching_page[
4094 CTL_PAGE_SAVED];
4095 value = ctl_get_opt(&lun->be_lun->options, "writecache");
4096 if (value != NULL && strcmp(value, "off") == 0)
4097 caching_page->flags1 &= ~SCP_WCE;
4098 value = ctl_get_opt(&lun->be_lun->options, "readcache");
4099 if (value != NULL && strcmp(value, "off") == 0)
4100 caching_page->flags1 |= SCP_RCD;
4101 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4102 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4103 sizeof(caching_page_default));
4104 page_index->page_data =
4105 (uint8_t *)lun->mode_pages.caching_page;
4106 break;
4107 }
4108 case SMS_CONTROL_MODE_PAGE: {
4109 switch (page_index->subpage) {
4110 case SMS_SUBPAGE_PAGE_0: {
4111 struct scsi_control_page *control_page;
4112
4113 memcpy(&lun->mode_pages.control_page[
4114 CTL_PAGE_DEFAULT],
4115 &control_page_default,
4116 sizeof(control_page_default));
4117 memcpy(&lun->mode_pages.control_page[
4118 CTL_PAGE_CHANGEABLE],
4119 &control_page_changeable,
4120 sizeof(control_page_changeable));
4121 memcpy(&lun->mode_pages.control_page[
4122 CTL_PAGE_SAVED],
4123 &control_page_default,
4124 sizeof(control_page_default));
4125 control_page = &lun->mode_pages.control_page[
4126 CTL_PAGE_SAVED];
4127 value = ctl_get_opt(&lun->be_lun->options,
4128 "reordering");
4129 if (value != NULL &&
4130 strcmp(value, "unrestricted") == 0) {
4131 control_page->queue_flags &=
4132 ~SCP_QUEUE_ALG_MASK;
4133 control_page->queue_flags |=
4134 SCP_QUEUE_ALG_UNRESTRICTED;
4135 }
4136 memcpy(&lun->mode_pages.control_page[
4137 CTL_PAGE_CURRENT],
4138 &lun->mode_pages.control_page[
4139 CTL_PAGE_SAVED],
4140 sizeof(control_page_default));
4141 page_index->page_data =
4142 (uint8_t *)lun->mode_pages.control_page;
4143 break;
4144 }
4145 case 0x01:
4146 memcpy(&lun->mode_pages.control_ext_page[
4147 CTL_PAGE_DEFAULT],
4148 &control_ext_page_default,
4149 sizeof(control_ext_page_default));
4150 memcpy(&lun->mode_pages.control_ext_page[
4151 CTL_PAGE_CHANGEABLE],
4152 &control_ext_page_changeable,
4153 sizeof(control_ext_page_changeable));
4154 memcpy(&lun->mode_pages.control_ext_page[
4155 CTL_PAGE_SAVED],
4156 &control_ext_page_default,
4157 sizeof(control_ext_page_default));
4158 memcpy(&lun->mode_pages.control_ext_page[
4159 CTL_PAGE_CURRENT],
4160 &lun->mode_pages.control_ext_page[
4161 CTL_PAGE_SAVED],
4162 sizeof(control_ext_page_default));
4163 page_index->page_data =
4164 (uint8_t *)lun->mode_pages.control_ext_page;
4165 break;
4166 default:
4167 panic("subpage %#x for page %#x is incorrect!",
4168 page_index->subpage, page_code);
4169 }
4170 break;
4171 }
4172 case SMS_INFO_EXCEPTIONS_PAGE: {
4173 switch (page_index->subpage) {
4174 case SMS_SUBPAGE_PAGE_0:
4175 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4176 &ie_page_default,
4177 sizeof(ie_page_default));
4178 memcpy(&lun->mode_pages.ie_page[
4179 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4180 sizeof(ie_page_changeable));
4181 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4182 &ie_page_default,
4183 sizeof(ie_page_default));
4184 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4185 &ie_page_default,
4186 sizeof(ie_page_default));
4187 page_index->page_data =
4188 (uint8_t *)lun->mode_pages.ie_page;
4189 break;
4190 case 0x02: {
4191 struct ctl_logical_block_provisioning_page *page;
4192
4193 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4194 &lbp_page_default,
4195 sizeof(lbp_page_default));
4196 memcpy(&lun->mode_pages.lbp_page[
4197 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4198 sizeof(lbp_page_changeable));
4199 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4200 &lbp_page_default,
4201 sizeof(lbp_page_default));
4202 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4203 value = ctl_get_opt(&lun->be_lun->options,
4204 "avail-threshold");
4205 if (value != NULL &&
4206 ctl_expand_number(value, &ival) == 0) {
4207 page->descr[0].flags |= SLBPPD_ENABLED |
4208 SLBPPD_ARMING_DEC;
4209 if (lun->be_lun->blocksize)
4210 ival /= lun->be_lun->blocksize;
4211 else
4212 ival /= 512;
4213 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4214 page->descr[0].count);
4215 }
4216 value = ctl_get_opt(&lun->be_lun->options,
4217 "used-threshold");
4218 if (value != NULL &&
4219 ctl_expand_number(value, &ival) == 0) {
4220 page->descr[1].flags |= SLBPPD_ENABLED |
4221 SLBPPD_ARMING_INC;
4222 if (lun->be_lun->blocksize)
4223 ival /= lun->be_lun->blocksize;
4224 else
4225 ival /= 512;
4226 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4227 page->descr[1].count);
4228 }
4229 value = ctl_get_opt(&lun->be_lun->options,
4230 "pool-avail-threshold");
4231 if (value != NULL &&
4232 ctl_expand_number(value, &ival) == 0) {
4233 page->descr[2].flags |= SLBPPD_ENABLED |
4234 SLBPPD_ARMING_DEC;
4235 if (lun->be_lun->blocksize)
4236 ival /= lun->be_lun->blocksize;
4237 else
4238 ival /= 512;
4239 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4240 page->descr[2].count);
4241 }
4242 value = ctl_get_opt(&lun->be_lun->options,
4243 "pool-used-threshold");
4244 if (value != NULL &&
4245 ctl_expand_number(value, &ival) == 0) {
4246 page->descr[3].flags |= SLBPPD_ENABLED |
4247 SLBPPD_ARMING_INC;
4248 if (lun->be_lun->blocksize)
4249 ival /= lun->be_lun->blocksize;
4250 else
4251 ival /= 512;
4252 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4253 page->descr[3].count);
4254 }
4255 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4256 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4257 sizeof(lbp_page_default));
4258 page_index->page_data =
4259 (uint8_t *)lun->mode_pages.lbp_page;
4260 break;
4261 }
4262 default:
4263 panic("subpage %#x for page %#x is incorrect!",
4264 page_index->subpage, page_code);
4265 }
4266 break;
4267 }
4268 case SMS_CDDVD_CAPS_PAGE:{
4269 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4270 ("subpage %#x for page %#x is incorrect!",
4271 page_index->subpage, page_code));
4272 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT],
4273 &cddvd_page_default,
4274 sizeof(cddvd_page_default));
4275 memcpy(&lun->mode_pages.cddvd_page[
4276 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable,
4277 sizeof(cddvd_page_changeable));
4278 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4279 &cddvd_page_default,
4280 sizeof(cddvd_page_default));
4281 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT],
4282 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4283 sizeof(cddvd_page_default));
4284 page_index->page_data =
4285 (uint8_t *)lun->mode_pages.cddvd_page;
4286 break;
4287 }
4288 default:
4289 panic("invalid page code value %#x", page_code);
4290 }
4291 }
4292
4293 return (CTL_RETVAL_COMPLETE);
4294}
4295
4296static int
4297ctl_init_log_page_index(struct ctl_lun *lun)
4298{
4299 struct ctl_page_index *page_index;
4300 int i, j, k, prev;
4301
4302 memcpy(&lun->log_pages.index, log_page_index_template,
4303 sizeof(log_page_index_template));
4304
4305 prev = -1;
4306 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4307
4308 page_index = &lun->log_pages.index[i];
4309 if (lun->be_lun->lun_type == T_DIRECT &&
4310 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4311 continue;
4312 if (lun->be_lun->lun_type == T_PROCESSOR &&
4313 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4314 continue;
4315 if (lun->be_lun->lun_type == T_CDROM &&
4316 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4317 continue;
4318
4319 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4320 lun->backend->lun_attr == NULL)
4321 continue;
4322
4323 if (page_index->page_code != prev) {
4324 lun->log_pages.pages_page[j] = page_index->page_code;
4325 prev = page_index->page_code;
4326 j++;
4327 }
4328 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4329 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4330 k++;
4331 }
4332 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4333 lun->log_pages.index[0].page_len = j;
4334 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4335 lun->log_pages.index[1].page_len = k * 2;
4336 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4337 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4338 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page;
4339 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page);
4340 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.ie_page;
4341 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.ie_page);
4342
4343 return (CTL_RETVAL_COMPLETE);
4344}
4345
4346static int
4347hex2bin(const char *str, uint8_t *buf, int buf_size)
4348{
4349 int i;
4350 u_char c;
4351
4352 memset(buf, 0, buf_size);
4353 while (isspace(str[0]))
4354 str++;
4355 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4356 str += 2;
4357 buf_size *= 2;
4358 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4359 while (str[i] == '-') /* Skip dashes in UUIDs. */
4360 str++;
4361 c = str[i];
4362 if (isdigit(c))
4363 c -= '0';
4364 else if (isalpha(c))
4365 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4366 else
4367 break;
4368 if (c >= 16)
4369 break;
4370 if ((i & 1) == 0)
4371 buf[i / 2] |= (c << 4);
4372 else
4373 buf[i / 2] |= c;
4374 }
4375 return ((i + 1) / 2);
4376}
4377
4378/*
4379 * LUN allocation.
4380 *
4381 * Requirements:
4382 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4383 * wants us to allocate the LUN and he can block.
4384 * - ctl_softc is always set
4385 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4386 *
4387 * Returns 0 for success, non-zero (errno) for failure.
4388 */
4389static int
4390ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4391 struct ctl_be_lun *const be_lun)
4392{
4393 struct ctl_lun *nlun, *lun;
4394 struct scsi_vpd_id_descriptor *desc;
4395 struct scsi_vpd_id_t10 *t10id;
4396 const char *eui, *naa, *scsiname, *uuid, *vendor, *value;
4397 int lun_number, i, lun_malloced;
4398 int devidlen, idlen1, idlen2 = 0, len;
4399
4400 if (be_lun == NULL)
4401 return (EINVAL);
4402
4403 /*
4404 * We currently only support Direct Access or Processor LUN types.
4405 */
4406 switch (be_lun->lun_type) {
4407 case T_DIRECT:
4408 case T_PROCESSOR:
4409 case T_CDROM:
4410 break;
4411 case T_SEQUENTIAL:
4412 case T_CHANGER:
4413 default:
4414 be_lun->lun_config_status(be_lun->be_lun,
4415 CTL_LUN_CONFIG_FAILURE);
4416 break;
4417 }
4418 if (ctl_lun == NULL) {
4419 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4420 lun_malloced = 1;
4421 } else {
4422 lun_malloced = 0;
4423 lun = ctl_lun;
4424 }
4425
4426 memset(lun, 0, sizeof(*lun));
4427 if (lun_malloced)
4428 lun->flags = CTL_LUN_MALLOCED;
4429
4430 /* Generate LUN ID. */
4431 devidlen = max(CTL_DEVID_MIN_LEN,
4432 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4433 idlen1 = sizeof(*t10id) + devidlen;
4434 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4435 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4436 if (scsiname != NULL) {
4437 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4438 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4439 }
4440 eui = ctl_get_opt(&be_lun->options, "eui");
4441 if (eui != NULL) {
4442 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4443 }
4444 naa = ctl_get_opt(&be_lun->options, "naa");
4445 if (naa != NULL) {
4446 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4447 }
4448 uuid = ctl_get_opt(&be_lun->options, "uuid");
4449 if (uuid != NULL) {
4450 len += sizeof(struct scsi_vpd_id_descriptor) + 18;
4451 }
4452 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4453 M_CTL, M_WAITOK | M_ZERO);
4454 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4455 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4456 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4457 desc->length = idlen1;
4458 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4459 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4460 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4461 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4462 } else {
4463 strncpy(t10id->vendor, vendor,
4464 min(sizeof(t10id->vendor), strlen(vendor)));
4465 }
4466 strncpy((char *)t10id->vendor_spec_id,
4467 (char *)be_lun->device_id, devidlen);
4468 if (scsiname != NULL) {
4469 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4470 desc->length);
4471 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4472 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4473 SVPD_ID_TYPE_SCSI_NAME;
4474 desc->length = idlen2;
4475 strlcpy(desc->identifier, scsiname, idlen2);
4476 }
4477 if (eui != NULL) {
4478 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4479 desc->length);
4480 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4481 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4482 SVPD_ID_TYPE_EUI64;
4483 desc->length = hex2bin(eui, desc->identifier, 16);
4484 desc->length = desc->length > 12 ? 16 :
4485 (desc->length > 8 ? 12 : 8);
4486 len -= 16 - desc->length;
4487 }
4488 if (naa != NULL) {
4489 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4490 desc->length);
4491 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4492 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4493 SVPD_ID_TYPE_NAA;
4494 desc->length = hex2bin(naa, desc->identifier, 16);
4495 desc->length = desc->length > 8 ? 16 : 8;
4496 len -= 16 - desc->length;
4497 }
4498 if (uuid != NULL) {
4499 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4500 desc->length);
4501 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4502 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4503 SVPD_ID_TYPE_UUID;
4504 desc->identifier[0] = 0x10;
4505 hex2bin(uuid, &desc->identifier[2], 16);
4506 desc->length = 18;
4507 }
4508 lun->lun_devid->len = len;
4509
4510 mtx_lock(&ctl_softc->ctl_lock);
4511 /*
4512 * See if the caller requested a particular LUN number. If so, see
4513 * if it is available. Otherwise, allocate the first available LUN.
4514 */
4515 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4516 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4517 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4518 mtx_unlock(&ctl_softc->ctl_lock);
4519 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4520 printf("ctl: requested LUN ID %d is higher "
4521 "than CTL_MAX_LUNS - 1 (%d)\n",
4522 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4523 } else {
4524 /*
4525 * XXX KDM return an error, or just assign
4526 * another LUN ID in this case??
4527 */
4528 printf("ctl: requested LUN ID %d is already "
4529 "in use\n", be_lun->req_lun_id);
4530 }
4531 if (lun->flags & CTL_LUN_MALLOCED)
4532 free(lun, M_CTL);
4533 be_lun->lun_config_status(be_lun->be_lun,
4534 CTL_LUN_CONFIG_FAILURE);
4535 return (ENOSPC);
4536 }
4537 lun_number = be_lun->req_lun_id;
4538 } else {
4539 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS);
4540 if (lun_number == -1) {
4541 mtx_unlock(&ctl_softc->ctl_lock);
4542 printf("ctl: can't allocate LUN, out of LUNs\n");
4543 if (lun->flags & CTL_LUN_MALLOCED)
4544 free(lun, M_CTL);
4545 be_lun->lun_config_status(be_lun->be_lun,
4546 CTL_LUN_CONFIG_FAILURE);
4547 return (ENOSPC);
4548 }
4549 }
4550 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4551
4552 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4553 lun->lun = lun_number;
4554 lun->be_lun = be_lun;
4555 /*
4556 * The processor LUN is always enabled. Disk LUNs come on line
4557 * disabled, and must be enabled by the backend.
4558 */
4559 lun->flags |= CTL_LUN_DISABLED;
4560 lun->backend = be_lun->be;
4561 be_lun->ctl_lun = lun;
4562 be_lun->lun_id = lun_number;
4563 atomic_add_int(&be_lun->be->num_luns, 1);
4564 if (be_lun->flags & CTL_LUN_FLAG_EJECTED)
4565 lun->flags |= CTL_LUN_EJECTED;
4566 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA)
4567 lun->flags |= CTL_LUN_NO_MEDIA;
4568 if (be_lun->flags & CTL_LUN_FLAG_STOPPED)
4569 lun->flags |= CTL_LUN_STOPPED;
4570
4571 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4572 lun->flags |= CTL_LUN_PRIMARY_SC;
4573
4574 value = ctl_get_opt(&be_lun->options, "removable");
4575 if (value != NULL) {
4576 if (strcmp(value, "on") == 0)
4577 lun->flags |= CTL_LUN_REMOVABLE;
4578 } else if (be_lun->lun_type == T_CDROM)
4579 lun->flags |= CTL_LUN_REMOVABLE;
4580
4581 lun->ctl_softc = ctl_softc;
4582#ifdef CTL_TIME_IO
4583 lun->last_busy = getsbinuptime();
4584#endif
4585 TAILQ_INIT(&lun->ooa_queue);
4586 TAILQ_INIT(&lun->blocked_queue);
4587 STAILQ_INIT(&lun->error_list);
4588 lun->ie_reported = 1;
4589 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0);
4590 ctl_tpc_lun_init(lun);
4591
4592 /*
4593 * Initialize the mode and log page index.
4594 */
4595 ctl_init_page_index(lun);
4596 ctl_init_log_page_index(lun);
4597
4598 /*
4599 * Now, before we insert this lun on the lun list, set the lun
4600 * inventory changed UA for all other luns.
4601 */
4602 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4603 mtx_lock(&nlun->lun_lock);
4604 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4605 mtx_unlock(&nlun->lun_lock);
4606 }
4607
4608 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4609
4610 ctl_softc->ctl_luns[lun_number] = lun;
4611
4612 ctl_softc->num_luns++;
4613
4614 /* Setup statistics gathering */
4615 lun->stats.device_type = be_lun->lun_type;
4616 lun->stats.lun_number = lun_number;
4617 lun->stats.blocksize = be_lun->blocksize;
4618 if (be_lun->blocksize == 0)
4619 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4620 for (i = 0;i < CTL_MAX_PORTS;i++)
4621 lun->stats.ports[i].targ_port = i;
4622
4623 mtx_unlock(&ctl_softc->ctl_lock);
4624
4625 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4626 return (0);
4627}
4628
4629/*
4630 * Delete a LUN.
4631 * Assumptions:
4632 * - LUN has already been marked invalid and any pending I/O has been taken
4633 * care of.
4634 */
4635static int
4636ctl_free_lun(struct ctl_lun *lun)
4637{
4638 struct ctl_softc *softc = lun->ctl_softc;
4639 struct ctl_lun *nlun;
4640 int i;
4641
4642 mtx_assert(&softc->ctl_lock, MA_OWNED);
4643
4644 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4645
4646 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4647
4648 softc->ctl_luns[lun->lun] = NULL;
4649
4650 if (!TAILQ_EMPTY(&lun->ooa_queue))
4651 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4652
4653 softc->num_luns--;
4654
4655 /*
4656 * Tell the backend to free resources, if this LUN has a backend.
4657 */
4658 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4659 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4660
4661 lun->ie_reportcnt = UINT32_MAX;
4662 callout_drain(&lun->ie_callout);
4663
4664 ctl_tpc_lun_shutdown(lun);
4665 mtx_destroy(&lun->lun_lock);
4666 free(lun->lun_devid, M_CTL);
4667 for (i = 0; i < CTL_MAX_PORTS; i++)
4668 free(lun->pending_ua[i], M_CTL);
4669 for (i = 0; i < CTL_MAX_PORTS; i++)
4670 free(lun->pr_keys[i], M_CTL);
4671 free(lun->write_buffer, M_CTL);
4672 if (lun->flags & CTL_LUN_MALLOCED)
4673 free(lun, M_CTL);
4674
4675 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4676 mtx_lock(&nlun->lun_lock);
4677 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4678 mtx_unlock(&nlun->lun_lock);
4679 }
4680
4681 return (0);
4682}
4683
4684static void
4685ctl_create_lun(struct ctl_be_lun *be_lun)
4686{
4687
4688 /*
4689 * ctl_alloc_lun() should handle all potential failure cases.
4690 */
4691 ctl_alloc_lun(control_softc, NULL, be_lun);
4692}
4693
4694int
4695ctl_add_lun(struct ctl_be_lun *be_lun)
4696{
4697 struct ctl_softc *softc = control_softc;
4698
4699 mtx_lock(&softc->ctl_lock);
4700 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4701 mtx_unlock(&softc->ctl_lock);
4702 wakeup(&softc->pending_lun_queue);
4703
4704 return (0);
4705}
4706
4707int
4708ctl_enable_lun(struct ctl_be_lun *be_lun)
4709{
4710 struct ctl_softc *softc;
4711 struct ctl_port *port, *nport;
4712 struct ctl_lun *lun;
4713 int retval;
4714
4715 lun = (struct ctl_lun *)be_lun->ctl_lun;
4716 softc = lun->ctl_softc;
4717
4718 mtx_lock(&softc->ctl_lock);
4719 mtx_lock(&lun->lun_lock);
4720 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4721 /*
4722 * eh? Why did we get called if the LUN is already
4723 * enabled?
4724 */
4725 mtx_unlock(&lun->lun_lock);
4726 mtx_unlock(&softc->ctl_lock);
4727 return (0);
4728 }
4729 lun->flags &= ~CTL_LUN_DISABLED;
4730 mtx_unlock(&lun->lun_lock);
4731
4732 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) {
4733 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4734 port->lun_map != NULL || port->lun_enable == NULL)
4735 continue;
4736
4737 /*
4738 * Drop the lock while we call the FETD's enable routine.
4739 * This can lead to a callback into CTL (at least in the
4740 * case of the internal initiator frontend.
4741 */
4742 mtx_unlock(&softc->ctl_lock);
4743 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4744 mtx_lock(&softc->ctl_lock);
4745 if (retval != 0) {
4746 printf("%s: FETD %s port %d returned error "
4747 "%d for lun_enable on lun %jd\n",
4748 __func__, port->port_name, port->targ_port,
4749 retval, (intmax_t)lun->lun);
4750 }
4751 }
4752
4753 mtx_unlock(&softc->ctl_lock);
4754 ctl_isc_announce_lun(lun);
4755
4756 return (0);
4757}
4758
4759int
4760ctl_disable_lun(struct ctl_be_lun *be_lun)
4761{
4762 struct ctl_softc *softc;
4763 struct ctl_port *port;
4764 struct ctl_lun *lun;
4765 int retval;
4766
4767 lun = (struct ctl_lun *)be_lun->ctl_lun;
4768 softc = lun->ctl_softc;
4769
4770 mtx_lock(&softc->ctl_lock);
4771 mtx_lock(&lun->lun_lock);
4772 if (lun->flags & CTL_LUN_DISABLED) {
4773 mtx_unlock(&lun->lun_lock);
4774 mtx_unlock(&softc->ctl_lock);
4775 return (0);
4776 }
4777 lun->flags |= CTL_LUN_DISABLED;
4778 mtx_unlock(&lun->lun_lock);
4779
4780 STAILQ_FOREACH(port, &softc->port_list, links) {
4781 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4782 port->lun_map != NULL || port->lun_disable == NULL)
4783 continue;
4784
4785 /*
4786 * Drop the lock before we call the frontend's disable
4787 * routine, to avoid lock order reversals.
4788 *
4789 * XXX KDM what happens if the frontend list changes while
4790 * we're traversing it? It's unlikely, but should be handled.
4791 */
4792 mtx_unlock(&softc->ctl_lock);
4793 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4794 mtx_lock(&softc->ctl_lock);
4795 if (retval != 0) {
4796 printf("%s: FETD %s port %d returned error "
4797 "%d for lun_disable on lun %jd\n",
4798 __func__, port->port_name, port->targ_port,
4799 retval, (intmax_t)lun->lun);
4800 }
4801 }
4802
4803 mtx_unlock(&softc->ctl_lock);
4804 ctl_isc_announce_lun(lun);
4805
4806 return (0);
4807}
4808
4809int
4810ctl_start_lun(struct ctl_be_lun *be_lun)
4811{
4812 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4813
4814 mtx_lock(&lun->lun_lock);
4815 lun->flags &= ~CTL_LUN_STOPPED;
4816 mtx_unlock(&lun->lun_lock);
4817 return (0);
4818}
4819
4820int
4821ctl_stop_lun(struct ctl_be_lun *be_lun)
4822{
4823 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4824
4825 mtx_lock(&lun->lun_lock);
4826 lun->flags |= CTL_LUN_STOPPED;
4827 mtx_unlock(&lun->lun_lock);
4828 return (0);
4829}
4830
4831int
4832ctl_lun_no_media(struct ctl_be_lun *be_lun)
4833{
4834 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4835
4836 mtx_lock(&lun->lun_lock);
4837 lun->flags |= CTL_LUN_NO_MEDIA;
4838 mtx_unlock(&lun->lun_lock);
4839 return (0);
4840}
4841
4842int
4843ctl_lun_has_media(struct ctl_be_lun *be_lun)
4844{
4845 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4846 union ctl_ha_msg msg;
4847
4848 mtx_lock(&lun->lun_lock);
4849 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED);
4850 if (lun->flags & CTL_LUN_REMOVABLE)
4851 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE);
4852 mtx_unlock(&lun->lun_lock);
4853 if ((lun->flags & CTL_LUN_REMOVABLE) &&
4854 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4855 bzero(&msg.ua, sizeof(msg.ua));
4856 msg.hdr.msg_type = CTL_MSG_UA;
4857 msg.hdr.nexus.initid = -1;
4858 msg.hdr.nexus.targ_port = -1;
4859 msg.hdr.nexus.targ_lun = lun->lun;
4860 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4861 msg.ua.ua_all = 1;
4862 msg.ua.ua_set = 1;
4863 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE;
4864 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4865 M_WAITOK);
4866 }
4867 return (0);
4868}
4869
4870int
4871ctl_lun_ejected(struct ctl_be_lun *be_lun)
4872{
4873 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4874
4875 mtx_lock(&lun->lun_lock);
4876 lun->flags |= CTL_LUN_EJECTED;
4877 mtx_unlock(&lun->lun_lock);
4878 return (0);
4879}
4880
4881int
4882ctl_lun_primary(struct ctl_be_lun *be_lun)
4883{
4884 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4885
4886 mtx_lock(&lun->lun_lock);
4887 lun->flags |= CTL_LUN_PRIMARY_SC;
4888 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4889 mtx_unlock(&lun->lun_lock);
4890 ctl_isc_announce_lun(lun);
4891 return (0);
4892}
4893
4894int
4895ctl_lun_secondary(struct ctl_be_lun *be_lun)
4896{
4897 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4898
4899 mtx_lock(&lun->lun_lock);
4900 lun->flags &= ~CTL_LUN_PRIMARY_SC;
4901 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4902 mtx_unlock(&lun->lun_lock);
4903 ctl_isc_announce_lun(lun);
4904 return (0);
4905}
4906
4907int
4908ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4909{
4910 struct ctl_softc *softc;
4911 struct ctl_lun *lun;
4912
4913 lun = (struct ctl_lun *)be_lun->ctl_lun;
4914 softc = lun->ctl_softc;
4915
4916 mtx_lock(&lun->lun_lock);
4917
4918 /*
4919 * The LUN needs to be disabled before it can be marked invalid.
4920 */
4921 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4922 mtx_unlock(&lun->lun_lock);
4923 return (-1);
4924 }
4925 /*
4926 * Mark the LUN invalid.
4927 */
4928 lun->flags |= CTL_LUN_INVALID;
4929
4930 /*
4931 * If there is nothing in the OOA queue, go ahead and free the LUN.
4932 * If we have something in the OOA queue, we'll free it when the
4933 * last I/O completes.
4934 */
4935 if (TAILQ_EMPTY(&lun->ooa_queue)) {
4936 mtx_unlock(&lun->lun_lock);
4937 mtx_lock(&softc->ctl_lock);
4938 ctl_free_lun(lun);
4939 mtx_unlock(&softc->ctl_lock);
4940 } else
4941 mtx_unlock(&lun->lun_lock);
4942
4943 return (0);
4944}
4945
4946void
4947ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4948{
4949 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4950 union ctl_ha_msg msg;
4951
4952 mtx_lock(&lun->lun_lock);
4953 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE);
4954 mtx_unlock(&lun->lun_lock);
4955 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4956 /* Send msg to other side. */
4957 bzero(&msg.ua, sizeof(msg.ua));
4958 msg.hdr.msg_type = CTL_MSG_UA;
4959 msg.hdr.nexus.initid = -1;
4960 msg.hdr.nexus.targ_port = -1;
4961 msg.hdr.nexus.targ_lun = lun->lun;
4962 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4963 msg.ua.ua_all = 1;
4964 msg.ua.ua_set = 1;
4965 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE;
4966 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4967 M_WAITOK);
4968 }
4969}
4970
4971/*
4972 * Backend "memory move is complete" callback for requests that never
4973 * make it down to say RAIDCore's configuration code.
4974 */
4975int
4976ctl_config_move_done(union ctl_io *io)
4977{
4978 int retval;
4979
4980 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
4981 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
4982 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
4983
4984 if ((io->io_hdr.port_status != 0) &&
4985 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4986 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4987 /*
4988 * For hardware error sense keys, the sense key
4989 * specific value is defined to be a retry count,
4990 * but we use it to pass back an internal FETD
4991 * error code. XXX KDM Hopefully the FETD is only
4992 * using 16 bits for an error code, since that's
4993 * all the space we have in the sks field.
4994 */
4995 ctl_set_internal_failure(&io->scsiio,
4996 /*sks_valid*/ 1,
4997 /*retry_count*/
4998 io->io_hdr.port_status);
4999 }
5000
5001 if (ctl_debug & CTL_DEBUG_CDB_DATA)
5002 ctl_data_print(io);
5003 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
5004 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5005 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
5006 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5007 /*
5008 * XXX KDM just assuming a single pointer here, and not a
5009 * S/G list. If we start using S/G lists for config data,
5010 * we'll need to know how to clean them up here as well.
5011 */
5012 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5013 free(io->scsiio.kern_data_ptr, M_CTL);
5014 ctl_done(io);
5015 retval = CTL_RETVAL_COMPLETE;
5016 } else {
5017 /*
5018 * XXX KDM now we need to continue data movement. Some
5019 * options:
5020 * - call ctl_scsiio() again? We don't do this for data
5021 * writes, because for those at least we know ahead of
5022 * time where the write will go and how long it is. For
5023 * config writes, though, that information is largely
5024 * contained within the write itself, thus we need to
5025 * parse out the data again.
5026 *
5027 * - Call some other function once the data is in?
5028 */
5029
5030 /*
5031 * XXX KDM call ctl_scsiio() again for now, and check flag
5032 * bits to see whether we're allocated or not.
5033 */
5034 retval = ctl_scsiio(&io->scsiio);
5035 }
5036 return (retval);
5037}
5038
5039/*
5040 * This gets called by a backend driver when it is done with a
5041 * data_submit method.
5042 */
5043void
5044ctl_data_submit_done(union ctl_io *io)
5045{
5046 /*
5047 * If the IO_CONT flag is set, we need to call the supplied
5048 * function to continue processing the I/O, instead of completing
5049 * the I/O just yet.
5050 *
5051 * If there is an error, though, we don't want to keep processing.
5052 * Instead, just send status back to the initiator.
5053 */
5054 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5055 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5056 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5057 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5058 io->scsiio.io_cont(io);
5059 return;
5060 }
5061 ctl_done(io);
5062}
5063
5064/*
5065 * This gets called by a backend driver when it is done with a
5066 * configuration write.
5067 */
5068void
5069ctl_config_write_done(union ctl_io *io)
5070{
5071 uint8_t *buf;
5072
5073 /*
5074 * If the IO_CONT flag is set, we need to call the supplied
5075 * function to continue processing the I/O, instead of completing
5076 * the I/O just yet.
5077 *
5078 * If there is an error, though, we don't want to keep processing.
5079 * Instead, just send status back to the initiator.
5080 */
5081 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5082 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5083 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5084 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5085 io->scsiio.io_cont(io);
5086 return;
5087 }
5088 /*
5089 * Since a configuration write can be done for commands that actually
5090 * have data allocated, like write buffer, and commands that have
5091 * no data, like start/stop unit, we need to check here.
5092 */
5093 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5094 buf = io->scsiio.kern_data_ptr;
5095 else
5096 buf = NULL;
5097 ctl_done(io);
5098 if (buf)
5099 free(buf, M_CTL);
5100}
5101
5102void
5103ctl_config_read_done(union ctl_io *io)
5104{
5105 uint8_t *buf;
5106
5107 /*
5108 * If there is some error -- we are done, skip data transfer.
5109 */
5110 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5111 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5112 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5113 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5114 buf = io->scsiio.kern_data_ptr;
5115 else
5116 buf = NULL;
5117 ctl_done(io);
5118 if (buf)
5119 free(buf, M_CTL);
5120 return;
5121 }
5122
5123 /*
5124 * If the IO_CONT flag is set, we need to call the supplied
5125 * function to continue processing the I/O, instead of completing
5126 * the I/O just yet.
5127 */
5128 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5129 io->scsiio.io_cont(io);
5130 return;
5131 }
5132
5133 ctl_datamove(io);
5134}
5135
5136/*
5137 * SCSI release command.
5138 */
5139int
5140ctl_scsi_release(struct ctl_scsiio *ctsio)
5141{
5142 struct ctl_lun *lun = CTL_LUN(ctsio);
5143 uint32_t residx;
5144
5145 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5146
5147 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5148
5149 /*
5150 * XXX KDM right now, we only support LUN reservation. We don't
5151 * support 3rd party reservations, or extent reservations, which
5152 * might actually need the parameter list. If we've gotten this
5153 * far, we've got a LUN reservation. Anything else got kicked out
5154 * above. So, according to SPC, ignore the length.
5155 */
5156
5157 mtx_lock(&lun->lun_lock);
5158
5159 /*
5160 * According to SPC, it is not an error for an intiator to attempt
5161 * to release a reservation on a LUN that isn't reserved, or that
5162 * is reserved by another initiator. The reservation can only be
5163 * released, though, by the initiator who made it or by one of
5164 * several reset type events.
5165 */
5166 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5167 lun->flags &= ~CTL_LUN_RESERVED;
5168
5169 mtx_unlock(&lun->lun_lock);
5170
5171 ctl_set_success(ctsio);
5172 ctl_done((union ctl_io *)ctsio);
5173 return (CTL_RETVAL_COMPLETE);
5174}
5175
5176int
5177ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5178{
5179 struct ctl_lun *lun = CTL_LUN(ctsio);
5180 uint32_t residx;
5181
5182 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5183
5184 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5185
5186 /*
5187 * XXX KDM right now, we only support LUN reservation. We don't
5188 * support 3rd party reservations, or extent reservations, which
5189 * might actually need the parameter list. If we've gotten this
5190 * far, we've got a LUN reservation. Anything else got kicked out
5191 * above. So, according to SPC, ignore the length.
5192 */
5193
5194 mtx_lock(&lun->lun_lock);
5195 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5196 ctl_set_reservation_conflict(ctsio);
5197 goto bailout;
5198 }
5199
5200 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */
5201 if (lun->flags & CTL_LUN_PR_RESERVED) {
5202 ctl_set_success(ctsio);
5203 goto bailout;
5204 }
5205
5206 lun->flags |= CTL_LUN_RESERVED;
5207 lun->res_idx = residx;
5208 ctl_set_success(ctsio);
5209
5210bailout:
5211 mtx_unlock(&lun->lun_lock);
5212 ctl_done((union ctl_io *)ctsio);
5213 return (CTL_RETVAL_COMPLETE);
5214}
5215
5216int
5217ctl_start_stop(struct ctl_scsiio *ctsio)
5218{
5219 struct ctl_lun *lun = CTL_LUN(ctsio);
5220 struct scsi_start_stop_unit *cdb;
5221 int retval;
5222
5223 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5224
5225 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5226
5227 if ((cdb->how & SSS_PC_MASK) == 0) {
5228 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
5229 (cdb->how & SSS_START) == 0) {
5230 uint32_t residx;
5231
5232 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5233 if (ctl_get_prkey(lun, residx) == 0 ||
5234 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) {
5235
5236 ctl_set_reservation_conflict(ctsio);
5237 ctl_done((union ctl_io *)ctsio);
5238 return (CTL_RETVAL_COMPLETE);
5239 }
5240 }
5241
5242 if ((cdb->how & SSS_LOEJ) &&
5243 (lun->flags & CTL_LUN_REMOVABLE) == 0) {
5244 ctl_set_invalid_field(ctsio,
5245 /*sks_valid*/ 1,
5246 /*command*/ 1,
5247 /*field*/ 4,
5248 /*bit_valid*/ 1,
5249 /*bit*/ 1);
5250 ctl_done((union ctl_io *)ctsio);
5251 return (CTL_RETVAL_COMPLETE);
5252 }
5253
5254 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) &&
5255 lun->prevent_count > 0) {
5256 /* "Medium removal prevented" */
5257 ctl_set_sense(ctsio, /*current_error*/ 1,
5258 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ?
5259 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST,
5260 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE);
5261 ctl_done((union ctl_io *)ctsio);
5262 return (CTL_RETVAL_COMPLETE);
5263 }
5264 }
5265
5266 retval = lun->backend->config_write((union ctl_io *)ctsio);
5267 return (retval);
5268}
5269
5270int
5271ctl_prevent_allow(struct ctl_scsiio *ctsio)
5272{
5273 struct ctl_lun *lun = CTL_LUN(ctsio);
5274 struct scsi_prevent *cdb;
5275 int retval;
5276 uint32_t initidx;
5277
5278 CTL_DEBUG_PRINT(("ctl_prevent_allow\n"));
5279
5280 cdb = (struct scsi_prevent *)ctsio->cdb;
5281
5282 if ((lun->flags & CTL_LUN_REMOVABLE) == 0) {
5283 ctl_set_invalid_opcode(ctsio);
5284 ctl_done((union ctl_io *)ctsio);
5285 return (CTL_RETVAL_COMPLETE);
5286 }
5287
5288 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5289 mtx_lock(&lun->lun_lock);
5290 if ((cdb->how & PR_PREVENT) &&
5291 ctl_is_set(lun->prevent, initidx) == 0) {
5292 ctl_set_mask(lun->prevent, initidx);
5293 lun->prevent_count++;
5294 } else if ((cdb->how & PR_PREVENT) == 0 &&
5295 ctl_is_set(lun->prevent, initidx)) {
5296 ctl_clear_mask(lun->prevent, initidx);
5297 lun->prevent_count--;
5298 }
5299 mtx_unlock(&lun->lun_lock);
5300 retval = lun->backend->config_write((union ctl_io *)ctsio);
5301 return (retval);
5302}
5303
5304/*
5305 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5306 * we don't really do anything with the LBA and length fields if the user
5307 * passes them in. Instead we'll just flush out the cache for the entire
5308 * LUN.
5309 */
5310int
5311ctl_sync_cache(struct ctl_scsiio *ctsio)
5312{
5313 struct ctl_lun *lun = CTL_LUN(ctsio);
5314 struct ctl_lba_len_flags *lbalen;
5315 uint64_t starting_lba;
5316 uint32_t block_count;
5317 int retval;
5318 uint8_t byte2;
5319
5320 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5321
5322 retval = 0;
5323
5324 switch (ctsio->cdb[0]) {
5325 case SYNCHRONIZE_CACHE: {
5326 struct scsi_sync_cache *cdb;
5327 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5328
5329 starting_lba = scsi_4btoul(cdb->begin_lba);
5330 block_count = scsi_2btoul(cdb->lb_count);
5331 byte2 = cdb->byte2;
5332 break;
5333 }
5334 case SYNCHRONIZE_CACHE_16: {
5335 struct scsi_sync_cache_16 *cdb;
5336 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5337
5338 starting_lba = scsi_8btou64(cdb->begin_lba);
5339 block_count = scsi_4btoul(cdb->lb_count);
5340 byte2 = cdb->byte2;
5341 break;
5342 }
5343 default:
5344 ctl_set_invalid_opcode(ctsio);
5345 ctl_done((union ctl_io *)ctsio);
5346 goto bailout;
5347 break; /* NOTREACHED */
5348 }
5349
5350 /*
5351 * We check the LBA and length, but don't do anything with them.
5352 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5353 * get flushed. This check will just help satisfy anyone who wants
5354 * to see an error for an out of range LBA.
5355 */
5356 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5357 ctl_set_lba_out_of_range(ctsio,
5358 MAX(starting_lba, lun->be_lun->maxlba + 1));
5359 ctl_done((union ctl_io *)ctsio);
5360 goto bailout;
5361 }
5362
5363 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5364 lbalen->lba = starting_lba;
5365 lbalen->len = block_count;
5366 lbalen->flags = byte2;
5367 retval = lun->backend->config_write((union ctl_io *)ctsio);
5368
5369bailout:
5370 return (retval);
5371}
5372
5373int
5374ctl_format(struct ctl_scsiio *ctsio)
5375{
5376 struct scsi_format *cdb;
5377 int length, defect_list_len;
5378
5379 CTL_DEBUG_PRINT(("ctl_format\n"));
5380
5381 cdb = (struct scsi_format *)ctsio->cdb;
5382
5383 length = 0;
5384 if (cdb->byte2 & SF_FMTDATA) {
5385 if (cdb->byte2 & SF_LONGLIST)
5386 length = sizeof(struct scsi_format_header_long);
5387 else
5388 length = sizeof(struct scsi_format_header_short);
5389 }
5390
5391 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5392 && (length > 0)) {
5393 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5394 ctsio->kern_data_len = length;
5395 ctsio->kern_total_len = length;
5396 ctsio->kern_data_resid = 0;
5397 ctsio->kern_rel_offset = 0;
5398 ctsio->kern_sg_entries = 0;
5399 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5400 ctsio->be_move_done = ctl_config_move_done;
5401 ctl_datamove((union ctl_io *)ctsio);
5402
5403 return (CTL_RETVAL_COMPLETE);
5404 }
5405
5406 defect_list_len = 0;
5407
5408 if (cdb->byte2 & SF_FMTDATA) {
5409 if (cdb->byte2 & SF_LONGLIST) {
5410 struct scsi_format_header_long *header;
5411
5412 header = (struct scsi_format_header_long *)
5413 ctsio->kern_data_ptr;
5414
5415 defect_list_len = scsi_4btoul(header->defect_list_len);
5416 if (defect_list_len != 0) {
5417 ctl_set_invalid_field(ctsio,
5418 /*sks_valid*/ 1,
5419 /*command*/ 0,
5420 /*field*/ 2,
5421 /*bit_valid*/ 0,
5422 /*bit*/ 0);
5423 goto bailout;
5424 }
5425 } else {
5426 struct scsi_format_header_short *header;
5427
5428 header = (struct scsi_format_header_short *)
5429 ctsio->kern_data_ptr;
5430
5431 defect_list_len = scsi_2btoul(header->defect_list_len);
5432 if (defect_list_len != 0) {
5433 ctl_set_invalid_field(ctsio,
5434 /*sks_valid*/ 1,
5435 /*command*/ 0,
5436 /*field*/ 2,
5437 /*bit_valid*/ 0,
5438 /*bit*/ 0);
5439 goto bailout;
5440 }
5441 }
5442 }
5443
5444 ctl_set_success(ctsio);
5445bailout:
5446
5447 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5448 free(ctsio->kern_data_ptr, M_CTL);
5449 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5450 }
5451
5452 ctl_done((union ctl_io *)ctsio);
5453 return (CTL_RETVAL_COMPLETE);
5454}
5455
5456int
5457ctl_read_buffer(struct ctl_scsiio *ctsio)
5458{
5459 struct ctl_lun *lun = CTL_LUN(ctsio);
5460 uint64_t buffer_offset;
5461 uint32_t len;
5462 uint8_t byte2;
5463 static uint8_t descr[4];
5464 static uint8_t echo_descr[4] = { 0 };
5465
5466 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5467
5468 switch (ctsio->cdb[0]) {
5469 case READ_BUFFER: {
5470 struct scsi_read_buffer *cdb;
5471
5472 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5473 buffer_offset = scsi_3btoul(cdb->offset);
5474 len = scsi_3btoul(cdb->length);
5475 byte2 = cdb->byte2;
5476 break;
5477 }
5478 case READ_BUFFER_16: {
5479 struct scsi_read_buffer_16 *cdb;
5480
5481 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb;
5482 buffer_offset = scsi_8btou64(cdb->offset);
5483 len = scsi_4btoul(cdb->length);
5484 byte2 = cdb->byte2;
5485 break;
5486 }
5487 default: /* This shouldn't happen. */
5488 ctl_set_invalid_opcode(ctsio);
5489 ctl_done((union ctl_io *)ctsio);
5490 return (CTL_RETVAL_COMPLETE);
5491 }
5492
5493 if (buffer_offset > CTL_WRITE_BUFFER_SIZE ||
5494 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5495 ctl_set_invalid_field(ctsio,
5496 /*sks_valid*/ 1,
5497 /*command*/ 1,
5498 /*field*/ 6,
5499 /*bit_valid*/ 0,
5500 /*bit*/ 0);
5501 ctl_done((union ctl_io *)ctsio);
5502 return (CTL_RETVAL_COMPLETE);
5503 }
5504
5505 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5506 descr[0] = 0;
5507 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5508 ctsio->kern_data_ptr = descr;
5509 len = min(len, sizeof(descr));
5510 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5511 ctsio->kern_data_ptr = echo_descr;
5512 len = min(len, sizeof(echo_descr));
5513 } else {
5514 if (lun->write_buffer == NULL) {
5515 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5516 M_CTL, M_WAITOK);
5517 }
5518 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5519 }
5520 ctsio->kern_data_len = len;
5521 ctsio->kern_total_len = len;
5522 ctsio->kern_data_resid = 0;
5523 ctsio->kern_rel_offset = 0;
5524 ctsio->kern_sg_entries = 0;
5525 ctl_set_success(ctsio);
5526 ctsio->be_move_done = ctl_config_move_done;
5527 ctl_datamove((union ctl_io *)ctsio);
5528 return (CTL_RETVAL_COMPLETE);
5529}
5530
5531int
5532ctl_write_buffer(struct ctl_scsiio *ctsio)
5533{
5534 struct ctl_lun *lun = CTL_LUN(ctsio);
5535 struct scsi_write_buffer *cdb;
5536 int buffer_offset, len;
5537
5538 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5539
5540 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5541
5542 len = scsi_3btoul(cdb->length);
5543 buffer_offset = scsi_3btoul(cdb->offset);
5544
5545 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5546 ctl_set_invalid_field(ctsio,
5547 /*sks_valid*/ 1,
5548 /*command*/ 1,
5549 /*field*/ 6,
5550 /*bit_valid*/ 0,
5551 /*bit*/ 0);
5552 ctl_done((union ctl_io *)ctsio);
5553 return (CTL_RETVAL_COMPLETE);
5554 }
5555
5556 /*
5557 * If we've got a kernel request that hasn't been malloced yet,
5558 * malloc it and tell the caller the data buffer is here.
5559 */
5560 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5561 if (lun->write_buffer == NULL) {
5562 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5563 M_CTL, M_WAITOK);
5564 }
5565 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5566 ctsio->kern_data_len = len;
5567 ctsio->kern_total_len = len;
5568 ctsio->kern_data_resid = 0;
5569 ctsio->kern_rel_offset = 0;
5570 ctsio->kern_sg_entries = 0;
5571 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5572 ctsio->be_move_done = ctl_config_move_done;
5573 ctl_datamove((union ctl_io *)ctsio);
5574
5575 return (CTL_RETVAL_COMPLETE);
5576 }
5577
5578 ctl_set_success(ctsio);
5579 ctl_done((union ctl_io *)ctsio);
5580 return (CTL_RETVAL_COMPLETE);
5581}
5582
5583int
5584ctl_write_same(struct ctl_scsiio *ctsio)
5585{
5586 struct ctl_lun *lun = CTL_LUN(ctsio);
5587 struct ctl_lba_len_flags *lbalen;
5588 uint64_t lba;
5589 uint32_t num_blocks;
5590 int len, retval;
5591 uint8_t byte2;
5592
5593 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5594
5595 switch (ctsio->cdb[0]) {
5596 case WRITE_SAME_10: {
5597 struct scsi_write_same_10 *cdb;
5598
5599 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5600
5601 lba = scsi_4btoul(cdb->addr);
5602 num_blocks = scsi_2btoul(cdb->length);
5603 byte2 = cdb->byte2;
5604 break;
5605 }
5606 case WRITE_SAME_16: {
5607 struct scsi_write_same_16 *cdb;
5608
5609 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5610
5611 lba = scsi_8btou64(cdb->addr);
5612 num_blocks = scsi_4btoul(cdb->length);
5613 byte2 = cdb->byte2;
5614 break;
5615 }
5616 default:
5617 /*
5618 * We got a command we don't support. This shouldn't
5619 * happen, commands should be filtered out above us.
5620 */
5621 ctl_set_invalid_opcode(ctsio);
5622 ctl_done((union ctl_io *)ctsio);
5623
5624 return (CTL_RETVAL_COMPLETE);
5625 break; /* NOTREACHED */
5626 }
5627
5628 /* ANCHOR flag can be used only together with UNMAP */
5629 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) {
5630 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5631 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5632 ctl_done((union ctl_io *)ctsio);
5633 return (CTL_RETVAL_COMPLETE);
5634 }
5635
5636 /*
5637 * The first check is to make sure we're in bounds, the second
5638 * check is to catch wrap-around problems. If the lba + num blocks
5639 * is less than the lba, then we've wrapped around and the block
5640 * range is invalid anyway.
5641 */
5642 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5643 || ((lba + num_blocks) < lba)) {
5644 ctl_set_lba_out_of_range(ctsio,
5645 MAX(lba, lun->be_lun->maxlba + 1));
5646 ctl_done((union ctl_io *)ctsio);
5647 return (CTL_RETVAL_COMPLETE);
5648 }
5649
5650 /* Zero number of blocks means "to the last logical block" */
5651 if (num_blocks == 0) {
5652 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5653 ctl_set_invalid_field(ctsio,
5654 /*sks_valid*/ 0,
5655 /*command*/ 1,
5656 /*field*/ 0,
5657 /*bit_valid*/ 0,
5658 /*bit*/ 0);
5659 ctl_done((union ctl_io *)ctsio);
5660 return (CTL_RETVAL_COMPLETE);
5661 }
5662 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5663 }
5664
5665 len = lun->be_lun->blocksize;
5666
5667 /*
5668 * If we've got a kernel request that hasn't been malloced yet,
5669 * malloc it and tell the caller the data buffer is here.
5670 */
5671 if ((byte2 & SWS_NDOB) == 0 &&
5672 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5673 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5674 ctsio->kern_data_len = len;
5675 ctsio->kern_total_len = len;
5676 ctsio->kern_data_resid = 0;
5677 ctsio->kern_rel_offset = 0;
5678 ctsio->kern_sg_entries = 0;
5679 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5680 ctsio->be_move_done = ctl_config_move_done;
5681 ctl_datamove((union ctl_io *)ctsio);
5682
5683 return (CTL_RETVAL_COMPLETE);
5684 }
5685
5686 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5687 lbalen->lba = lba;
5688 lbalen->len = num_blocks;
5689 lbalen->flags = byte2;
5690 retval = lun->backend->config_write((union ctl_io *)ctsio);
5691
5692 return (retval);
5693}
5694
5695int
5696ctl_unmap(struct ctl_scsiio *ctsio)
5697{
5698 struct ctl_lun *lun = CTL_LUN(ctsio);
5699 struct scsi_unmap *cdb;
5700 struct ctl_ptr_len_flags *ptrlen;
5701 struct scsi_unmap_header *hdr;
5702 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5703 uint64_t lba;
5704 uint32_t num_blocks;
5705 int len, retval;
5706 uint8_t byte2;
5707
5708 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5709
5710 cdb = (struct scsi_unmap *)ctsio->cdb;
5711 len = scsi_2btoul(cdb->length);
5712 byte2 = cdb->byte2;
5713
5714 /*
5715 * If we've got a kernel request that hasn't been malloced yet,
5716 * malloc it and tell the caller the data buffer is here.
5717 */
5718 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5719 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5720 ctsio->kern_data_len = len;
5721 ctsio->kern_total_len = len;
5722 ctsio->kern_data_resid = 0;
5723 ctsio->kern_rel_offset = 0;
5724 ctsio->kern_sg_entries = 0;
5725 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5726 ctsio->be_move_done = ctl_config_move_done;
5727 ctl_datamove((union ctl_io *)ctsio);
5728
5729 return (CTL_RETVAL_COMPLETE);
5730 }
5731
5732 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5733 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5734 if (len < sizeof (*hdr) ||
5735 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5736 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5737 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5738 ctl_set_invalid_field(ctsio,
5739 /*sks_valid*/ 0,
5740 /*command*/ 0,
5741 /*field*/ 0,
5742 /*bit_valid*/ 0,
5743 /*bit*/ 0);
5744 goto done;
5745 }
5746 len = scsi_2btoul(hdr->desc_length);
5747 buf = (struct scsi_unmap_desc *)(hdr + 1);
5748 end = buf + len / sizeof(*buf);
5749
5750 endnz = buf;
5751 for (range = buf; range < end; range++) {
5752 lba = scsi_8btou64(range->lba);
5753 num_blocks = scsi_4btoul(range->length);
5754 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5755 || ((lba + num_blocks) < lba)) {
5756 ctl_set_lba_out_of_range(ctsio,
5757 MAX(lba, lun->be_lun->maxlba + 1));
5758 ctl_done((union ctl_io *)ctsio);
5759 return (CTL_RETVAL_COMPLETE);
5760 }
5761 if (num_blocks != 0)
5762 endnz = range + 1;
5763 }
5764
5765 /*
5766 * Block backend can not handle zero last range.
5767 * Filter it out and return if there is nothing left.
5768 */
5769 len = (uint8_t *)endnz - (uint8_t *)buf;
5770 if (len == 0) {
5771 ctl_set_success(ctsio);
5772 goto done;
5773 }
5774
5775 mtx_lock(&lun->lun_lock);
5776 ptrlen = (struct ctl_ptr_len_flags *)
5777 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5778 ptrlen->ptr = (void *)buf;
5779 ptrlen->len = len;
5780 ptrlen->flags = byte2;
5781 ctl_check_blocked(lun);
5782 mtx_unlock(&lun->lun_lock);
5783
5784 retval = lun->backend->config_write((union ctl_io *)ctsio);
5785 return (retval);
5786
5787done:
5788 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5789 free(ctsio->kern_data_ptr, M_CTL);
5790 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5791 }
5792 ctl_done((union ctl_io *)ctsio);
5793 return (CTL_RETVAL_COMPLETE);
5794}
5795
5796int
5797ctl_default_page_handler(struct ctl_scsiio *ctsio,
5798 struct ctl_page_index *page_index, uint8_t *page_ptr)
5799{
5800 struct ctl_lun *lun = CTL_LUN(ctsio);
5801 uint8_t *current_cp;
5802 int set_ua;
5803 uint32_t initidx;
5804
5805 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5806 set_ua = 0;
5807
5808 current_cp = (page_index->page_data + (page_index->page_len *
5809 CTL_PAGE_CURRENT));
5810
5811 mtx_lock(&lun->lun_lock);
5812 if (memcmp(current_cp, page_ptr, page_index->page_len)) {
5813 memcpy(current_cp, page_ptr, page_index->page_len);
5814 set_ua = 1;
5815 }
5816 if (set_ua != 0)
5817 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5818 mtx_unlock(&lun->lun_lock);
5819 if (set_ua) {
5820 ctl_isc_announce_mode(lun,
5821 ctl_get_initindex(&ctsio->io_hdr.nexus),
5822 page_index->page_code, page_index->subpage);
5823 }
5824 return (CTL_RETVAL_COMPLETE);
5825}
5826
5827static void
5828ctl_ie_timer(void *arg)
5829{
5830 struct ctl_lun *lun = arg;
5831 uint64_t t;
5832
5833 if (lun->ie_asc == 0)
5834 return;
5835
5836 if (lun->MODE_IE.mrie == SIEP_MRIE_UA)
5837 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5838 else
5839 lun->ie_reported = 0;
5840
5841 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) {
5842 lun->ie_reportcnt++;
5843 t = scsi_4btoul(lun->MODE_IE.interval_timer);
5844 if (t == 0 || t == UINT32_MAX)
5845 t = 3000; /* 5 min */
5846 callout_schedule(&lun->ie_callout, t * hz / 10);
5847 }
5848}
5849
5850int
5851ctl_ie_page_handler(struct ctl_scsiio *ctsio,
5852 struct ctl_page_index *page_index, uint8_t *page_ptr)
5853{
5854 struct ctl_lun *lun = CTL_LUN(ctsio);
5855 struct scsi_info_exceptions_page *pg;
5856 uint64_t t;
5857
5858 (void)ctl_default_page_handler(ctsio, page_index, page_ptr);
5859
5860 pg = (struct scsi_info_exceptions_page *)page_ptr;
5861 mtx_lock(&lun->lun_lock);
5862 if (pg->info_flags & SIEP_FLAGS_TEST) {
5863 lun->ie_asc = 0x5d;
5864 lun->ie_ascq = 0xff;
5865 if (pg->mrie == SIEP_MRIE_UA) {
5866 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5867 lun->ie_reported = 1;
5868 } else {
5869 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
5870 lun->ie_reported = -1;
5871 }
5872 lun->ie_reportcnt = 1;
5873 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) {
5874 lun->ie_reportcnt++;
5875 t = scsi_4btoul(pg->interval_timer);
5876 if (t == 0 || t == UINT32_MAX)
5877 t = 3000; /* 5 min */
5878 callout_reset(&lun->ie_callout, t * hz / 10,
5879 ctl_ie_timer, lun);
5880 }
5881 } else {
5882 lun->ie_asc = 0;
5883 lun->ie_ascq = 0;
5884 lun->ie_reported = 1;
5885 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
5886 lun->ie_reportcnt = UINT32_MAX;
5887 callout_stop(&lun->ie_callout);
5888 }
5889 mtx_unlock(&lun->lun_lock);
5890 return (CTL_RETVAL_COMPLETE);
5891}
5892
5893static int
5894ctl_do_mode_select(union ctl_io *io)
5895{
5896 struct ctl_lun *lun = CTL_LUN(io);
5897 struct scsi_mode_page_header *page_header;
5898 struct ctl_page_index *page_index;
5899 struct ctl_scsiio *ctsio;
5900 int page_len, page_len_offset, page_len_size;
5901 union ctl_modepage_info *modepage_info;
5902 uint16_t *len_left, *len_used;
5903 int retval, i;
5904
5905 ctsio = &io->scsiio;
5906 page_index = NULL;
5907 page_len = 0;
5908
5909 modepage_info = (union ctl_modepage_info *)
5910 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
5911 len_left = &modepage_info->header.len_left;
5912 len_used = &modepage_info->header.len_used;
5913
5914do_next_page:
5915
5916 page_header = (struct scsi_mode_page_header *)
5917 (ctsio->kern_data_ptr + *len_used);
5918
5919 if (*len_left == 0) {
5920 free(ctsio->kern_data_ptr, M_CTL);
5921 ctl_set_success(ctsio);
5922 ctl_done((union ctl_io *)ctsio);
5923 return (CTL_RETVAL_COMPLETE);
5924 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
5925
5926 free(ctsio->kern_data_ptr, M_CTL);
5927 ctl_set_param_len_error(ctsio);
5928 ctl_done((union ctl_io *)ctsio);
5929 return (CTL_RETVAL_COMPLETE);
5930
5931 } else if ((page_header->page_code & SMPH_SPF)
5932 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
5933
5934 free(ctsio->kern_data_ptr, M_CTL);
5935 ctl_set_param_len_error(ctsio);
5936 ctl_done((union ctl_io *)ctsio);
5937 return (CTL_RETVAL_COMPLETE);
5938 }
5939
5940
5941 /*
5942 * XXX KDM should we do something with the block descriptor?
5943 */
5944 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
5945 page_index = &lun->mode_pages.index[i];
5946 if (lun->be_lun->lun_type == T_DIRECT &&
5947 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
5948 continue;
5949 if (lun->be_lun->lun_type == T_PROCESSOR &&
5950 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
5951 continue;
5952 if (lun->be_lun->lun_type == T_CDROM &&
5953 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
5954 continue;
5955
5956 if ((page_index->page_code & SMPH_PC_MASK) !=
5957 (page_header->page_code & SMPH_PC_MASK))
5958 continue;
5959
5960 /*
5961 * If neither page has a subpage code, then we've got a
5962 * match.
5963 */
5964 if (((page_index->page_code & SMPH_SPF) == 0)
5965 && ((page_header->page_code & SMPH_SPF) == 0)) {
5966 page_len = page_header->page_length;
5967 break;
5968 }
5969
5970 /*
5971 * If both pages have subpages, then the subpage numbers
5972 * have to match.
5973 */
5974 if ((page_index->page_code & SMPH_SPF)
5975 && (page_header->page_code & SMPH_SPF)) {
5976 struct scsi_mode_page_header_sp *sph;
5977
5978 sph = (struct scsi_mode_page_header_sp *)page_header;
5979 if (page_index->subpage == sph->subpage) {
5980 page_len = scsi_2btoul(sph->page_length);
5981 break;
5982 }
5983 }
5984 }
5985
5986 /*
5987 * If we couldn't find the page, or if we don't have a mode select
5988 * handler for it, send back an error to the user.
5989 */
5990 if ((i >= CTL_NUM_MODE_PAGES)
5991 || (page_index->select_handler == NULL)) {
5992 ctl_set_invalid_field(ctsio,
5993 /*sks_valid*/ 1,
5994 /*command*/ 0,
5995 /*field*/ *len_used,
5996 /*bit_valid*/ 0,
5997 /*bit*/ 0);
5998 free(ctsio->kern_data_ptr, M_CTL);
5999 ctl_done((union ctl_io *)ctsio);
6000 return (CTL_RETVAL_COMPLETE);
6001 }
6002
6003 if (page_index->page_code & SMPH_SPF) {
6004 page_len_offset = 2;
6005 page_len_size = 2;
6006 } else {
6007 page_len_size = 1;
6008 page_len_offset = 1;
6009 }
6010
6011 /*
6012 * If the length the initiator gives us isn't the one we specify in
6013 * the mode page header, or if they didn't specify enough data in
6014 * the CDB to avoid truncating this page, kick out the request.
6015 */
6016 if (page_len != page_index->page_len - page_len_offset - page_len_size) {
6017 ctl_set_invalid_field(ctsio,
6018 /*sks_valid*/ 1,
6019 /*command*/ 0,
6020 /*field*/ *len_used + page_len_offset,
6021 /*bit_valid*/ 0,
6022 /*bit*/ 0);
6023 free(ctsio->kern_data_ptr, M_CTL);
6024 ctl_done((union ctl_io *)ctsio);
6025 return (CTL_RETVAL_COMPLETE);
6026 }
6027 if (*len_left < page_index->page_len) {
6028 free(ctsio->kern_data_ptr, M_CTL);
6029 ctl_set_param_len_error(ctsio);
6030 ctl_done((union ctl_io *)ctsio);
6031 return (CTL_RETVAL_COMPLETE);
6032 }
6033
6034 /*
6035 * Run through the mode page, checking to make sure that the bits
6036 * the user changed are actually legal for him to change.
6037 */
6038 for (i = 0; i < page_index->page_len; i++) {
6039 uint8_t *user_byte, *change_mask, *current_byte;
6040 int bad_bit;
6041 int j;
6042
6043 user_byte = (uint8_t *)page_header + i;
6044 change_mask = page_index->page_data +
6045 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6046 current_byte = page_index->page_data +
6047 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6048
6049 /*
6050 * Check to see whether the user set any bits in this byte
6051 * that he is not allowed to set.
6052 */
6053 if ((*user_byte & ~(*change_mask)) ==
6054 (*current_byte & ~(*change_mask)))
6055 continue;
6056
6057 /*
6058 * Go through bit by bit to determine which one is illegal.
6059 */
6060 bad_bit = 0;
6061 for (j = 7; j >= 0; j--) {
6062 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6063 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6064 bad_bit = i;
6065 break;
6066 }
6067 }
6068 ctl_set_invalid_field(ctsio,
6069 /*sks_valid*/ 1,
6070 /*command*/ 0,
6071 /*field*/ *len_used + i,
6072 /*bit_valid*/ 1,
6073 /*bit*/ bad_bit);
6074 free(ctsio->kern_data_ptr, M_CTL);
6075 ctl_done((union ctl_io *)ctsio);
6076 return (CTL_RETVAL_COMPLETE);
6077 }
6078
6079 /*
6080 * Decrement these before we call the page handler, since we may
6081 * end up getting called back one way or another before the handler
6082 * returns to this context.
6083 */
6084 *len_left -= page_index->page_len;
6085 *len_used += page_index->page_len;
6086
6087 retval = page_index->select_handler(ctsio, page_index,
6088 (uint8_t *)page_header);
6089
6090 /*
6091 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6092 * wait until this queued command completes to finish processing
6093 * the mode page. If it returns anything other than
6094 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6095 * already set the sense information, freed the data pointer, and
6096 * completed the io for us.
6097 */
6098 if (retval != CTL_RETVAL_COMPLETE)
6099 goto bailout_no_done;
6100
6101 /*
6102 * If the initiator sent us more than one page, parse the next one.
6103 */
6104 if (*len_left > 0)
6105 goto do_next_page;
6106
6107 ctl_set_success(ctsio);
6108 free(ctsio->kern_data_ptr, M_CTL);
6109 ctl_done((union ctl_io *)ctsio);
6110
6111bailout_no_done:
6112
6113 return (CTL_RETVAL_COMPLETE);
6114
6115}
6116
6117int
6118ctl_mode_select(struct ctl_scsiio *ctsio)
6119{
6120 struct ctl_lun *lun = CTL_LUN(ctsio);
6121 union ctl_modepage_info *modepage_info;
6122 int bd_len, i, header_size, param_len, pf, rtd, sp;
6123 uint32_t initidx;
6124
6125 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6126 switch (ctsio->cdb[0]) {
6127 case MODE_SELECT_6: {
6128 struct scsi_mode_select_6 *cdb;
6129
6130 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6131
6132 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6133 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6134 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6135 param_len = cdb->length;
6136 header_size = sizeof(struct scsi_mode_header_6);
6137 break;
6138 }
6139 case MODE_SELECT_10: {
6140 struct scsi_mode_select_10 *cdb;
6141
6142 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6143
6144 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6145 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6146 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6147 param_len = scsi_2btoul(cdb->length);
6148 header_size = sizeof(struct scsi_mode_header_10);
6149 break;
6150 }
6151 default:
6152 ctl_set_invalid_opcode(ctsio);
6153 ctl_done((union ctl_io *)ctsio);
6154 return (CTL_RETVAL_COMPLETE);
6155 }
6156
6157 if (rtd) {
6158 if (param_len != 0) {
6159 ctl_set_invalid_field(ctsio, /*sks_valid*/ 0,
6160 /*command*/ 1, /*field*/ 0,
6161 /*bit_valid*/ 0, /*bit*/ 0);
6162 ctl_done((union ctl_io *)ctsio);
6163 return (CTL_RETVAL_COMPLETE);
6164 }
6165
6166 /* Revert to defaults. */
6167 ctl_init_page_index(lun);
6168 mtx_lock(&lun->lun_lock);
6169 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
6170 mtx_unlock(&lun->lun_lock);
6171 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6172 ctl_isc_announce_mode(lun, -1,
6173 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
6174 lun->mode_pages.index[i].subpage);
6175 }
6176 ctl_set_success(ctsio);
6177 ctl_done((union ctl_io *)ctsio);
6178 return (CTL_RETVAL_COMPLETE);
6179 }
6180
6181 /*
6182 * From SPC-3:
6183 * "A parameter list length of zero indicates that the Data-Out Buffer
6184 * shall be empty. This condition shall not be considered as an error."
6185 */
6186 if (param_len == 0) {
6187 ctl_set_success(ctsio);
6188 ctl_done((union ctl_io *)ctsio);
6189 return (CTL_RETVAL_COMPLETE);
6190 }
6191
6192 /*
6193 * Since we'll hit this the first time through, prior to
6194 * allocation, we don't need to free a data buffer here.
6195 */
6196 if (param_len < header_size) {
6197 ctl_set_param_len_error(ctsio);
6198 ctl_done((union ctl_io *)ctsio);
6199 return (CTL_RETVAL_COMPLETE);
6200 }
6201
6202 /*
6203 * Allocate the data buffer and grab the user's data. In theory,
6204 * we shouldn't have to sanity check the parameter list length here
6205 * because the maximum size is 64K. We should be able to malloc
6206 * that much without too many problems.
6207 */
6208 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6209 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6210 ctsio->kern_data_len = param_len;
6211 ctsio->kern_total_len = param_len;
6212 ctsio->kern_data_resid = 0;
6213 ctsio->kern_rel_offset = 0;
6214 ctsio->kern_sg_entries = 0;
6215 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6216 ctsio->be_move_done = ctl_config_move_done;
6217 ctl_datamove((union ctl_io *)ctsio);
6218
6219 return (CTL_RETVAL_COMPLETE);
6220 }
6221
6222 switch (ctsio->cdb[0]) {
6223 case MODE_SELECT_6: {
6224 struct scsi_mode_header_6 *mh6;
6225
6226 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6227 bd_len = mh6->blk_desc_len;
6228 break;
6229 }
6230 case MODE_SELECT_10: {
6231 struct scsi_mode_header_10 *mh10;
6232
6233 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6234 bd_len = scsi_2btoul(mh10->blk_desc_len);
6235 break;
6236 }
6237 default:
6238 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6239 }
6240
6241 if (param_len < (header_size + bd_len)) {
6242 free(ctsio->kern_data_ptr, M_CTL);
6243 ctl_set_param_len_error(ctsio);
6244 ctl_done((union ctl_io *)ctsio);
6245 return (CTL_RETVAL_COMPLETE);
6246 }
6247
6248 /*
6249 * Set the IO_CONT flag, so that if this I/O gets passed to
6250 * ctl_config_write_done(), it'll get passed back to
6251 * ctl_do_mode_select() for further processing, or completion if
6252 * we're all done.
6253 */
6254 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6255 ctsio->io_cont = ctl_do_mode_select;
6256
6257 modepage_info = (union ctl_modepage_info *)
6258 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6259 memset(modepage_info, 0, sizeof(*modepage_info));
6260 modepage_info->header.len_left = param_len - header_size - bd_len;
6261 modepage_info->header.len_used = header_size + bd_len;
6262
6263 return (ctl_do_mode_select((union ctl_io *)ctsio));
6264}
6265
6266int
6267ctl_mode_sense(struct ctl_scsiio *ctsio)
6268{
6269 struct ctl_lun *lun = CTL_LUN(ctsio);
6270 int pc, page_code, dbd, llba, subpage;
6271 int alloc_len, page_len, header_len, total_len;
6272 struct scsi_mode_block_descr *block_desc;
6273 struct ctl_page_index *page_index;
6274
6275 dbd = 0;
6276 llba = 0;
6277 block_desc = NULL;
6278
6279 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6280
6281 switch (ctsio->cdb[0]) {
6282 case MODE_SENSE_6: {
6283 struct scsi_mode_sense_6 *cdb;
6284
6285 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6286
6287 header_len = sizeof(struct scsi_mode_hdr_6);
6288 if (cdb->byte2 & SMS_DBD)
6289 dbd = 1;
6290 else
6291 header_len += sizeof(struct scsi_mode_block_descr);
6292
6293 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6294 page_code = cdb->page & SMS_PAGE_CODE;
6295 subpage = cdb->subpage;
6296 alloc_len = cdb->length;
6297 break;
6298 }
6299 case MODE_SENSE_10: {
6300 struct scsi_mode_sense_10 *cdb;
6301
6302 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6303
6304 header_len = sizeof(struct scsi_mode_hdr_10);
6305
6306 if (cdb->byte2 & SMS_DBD)
6307 dbd = 1;
6308 else
6309 header_len += sizeof(struct scsi_mode_block_descr);
6310 if (cdb->byte2 & SMS10_LLBAA)
6311 llba = 1;
6312 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6313 page_code = cdb->page & SMS_PAGE_CODE;
6314 subpage = cdb->subpage;
6315 alloc_len = scsi_2btoul(cdb->length);
6316 break;
6317 }
6318 default:
6319 ctl_set_invalid_opcode(ctsio);
6320 ctl_done((union ctl_io *)ctsio);
6321 return (CTL_RETVAL_COMPLETE);
6322 break; /* NOTREACHED */
6323 }
6324
6325 /*
6326 * We have to make a first pass through to calculate the size of
6327 * the pages that match the user's query. Then we allocate enough
6328 * memory to hold it, and actually copy the data into the buffer.
6329 */
6330 switch (page_code) {
6331 case SMS_ALL_PAGES_PAGE: {
6332 u_int i;
6333
6334 page_len = 0;
6335
6336 /*
6337 * At the moment, values other than 0 and 0xff here are
6338 * reserved according to SPC-3.
6339 */
6340 if ((subpage != SMS_SUBPAGE_PAGE_0)
6341 && (subpage != SMS_SUBPAGE_ALL)) {
6342 ctl_set_invalid_field(ctsio,
6343 /*sks_valid*/ 1,
6344 /*command*/ 1,
6345 /*field*/ 3,
6346 /*bit_valid*/ 0,
6347 /*bit*/ 0);
6348 ctl_done((union ctl_io *)ctsio);
6349 return (CTL_RETVAL_COMPLETE);
6350 }
6351
6352 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6353 page_index = &lun->mode_pages.index[i];
6354
6355 /* Make sure the page is supported for this dev type */
6356 if (lun->be_lun->lun_type == T_DIRECT &&
6357 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6358 continue;
6359 if (lun->be_lun->lun_type == T_PROCESSOR &&
6360 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6361 continue;
6362 if (lun->be_lun->lun_type == T_CDROM &&
6363 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6364 continue;
6365
6366 /*
6367 * We don't use this subpage if the user didn't
6368 * request all subpages.
6369 */
6370 if ((page_index->subpage != 0)
6371 && (subpage == SMS_SUBPAGE_PAGE_0))
6372 continue;
6373
6374#if 0
6375 printf("found page %#x len %d\n",
6376 page_index->page_code & SMPH_PC_MASK,
6377 page_index->page_len);
6378#endif
6379 page_len += page_index->page_len;
6380 }
6381 break;
6382 }
6383 default: {
6384 u_int i;
6385
6386 page_len = 0;
6387
6388 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6389 page_index = &lun->mode_pages.index[i];
6390
6391 /* Make sure the page is supported for this dev type */
6392 if (lun->be_lun->lun_type == T_DIRECT &&
6393 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6394 continue;
6395 if (lun->be_lun->lun_type == T_PROCESSOR &&
6396 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6397 continue;
6398 if (lun->be_lun->lun_type == T_CDROM &&
6399 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6400 continue;
6401
6402 /* Look for the right page code */
6403 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6404 continue;
6405
6406 /* Look for the right subpage or the subpage wildcard*/
6407 if ((page_index->subpage != subpage)
6408 && (subpage != SMS_SUBPAGE_ALL))
6409 continue;
6410
6411#if 0
6412 printf("found page %#x len %d\n",
6413 page_index->page_code & SMPH_PC_MASK,
6414 page_index->page_len);
6415#endif
6416
6417 page_len += page_index->page_len;
6418 }
6419
6420 if (page_len == 0) {
6421 ctl_set_invalid_field(ctsio,
6422 /*sks_valid*/ 1,
6423 /*command*/ 1,
6424 /*field*/ 2,
6425 /*bit_valid*/ 1,
6426 /*bit*/ 5);
6427 ctl_done((union ctl_io *)ctsio);
6428 return (CTL_RETVAL_COMPLETE);
6429 }
6430 break;
6431 }
6432 }
6433
6434 total_len = header_len + page_len;
6435#if 0
6436 printf("header_len = %d, page_len = %d, total_len = %d\n",
6437 header_len, page_len, total_len);
6438#endif
6439
6440 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6441 ctsio->kern_sg_entries = 0;
6442 ctsio->kern_data_resid = 0;
6443 ctsio->kern_rel_offset = 0;
6444 if (total_len < alloc_len) {
6445 ctsio->residual = alloc_len - total_len;
6446 ctsio->kern_data_len = total_len;
6447 ctsio->kern_total_len = total_len;
6448 } else {
6449 ctsio->residual = 0;
6450 ctsio->kern_data_len = alloc_len;
6451 ctsio->kern_total_len = alloc_len;
6452 }
6453
6454 switch (ctsio->cdb[0]) {
6455 case MODE_SENSE_6: {
6456 struct scsi_mode_hdr_6 *header;
6457
6458 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6459
6460 header->datalen = MIN(total_len - 1, 254);
6461 if (lun->be_lun->lun_type == T_DIRECT) {
6462 header->dev_specific = 0x10; /* DPOFUA */
6463 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6464 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6465 header->dev_specific |= 0x80; /* WP */
6466 }
6467 if (dbd)
6468 header->block_descr_len = 0;
6469 else
6470 header->block_descr_len =
6471 sizeof(struct scsi_mode_block_descr);
6472 block_desc = (struct scsi_mode_block_descr *)&header[1];
6473 break;
6474 }
6475 case MODE_SENSE_10: {
6476 struct scsi_mode_hdr_10 *header;
6477 int datalen;
6478
6479 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6480
6481 datalen = MIN(total_len - 2, 65533);
6482 scsi_ulto2b(datalen, header->datalen);
6483 if (lun->be_lun->lun_type == T_DIRECT) {
6484 header->dev_specific = 0x10; /* DPOFUA */
6485 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6486 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6487 header->dev_specific |= 0x80; /* WP */
6488 }
6489 if (dbd)
6490 scsi_ulto2b(0, header->block_descr_len);
6491 else
6492 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6493 header->block_descr_len);
6494 block_desc = (struct scsi_mode_block_descr *)&header[1];
6495 break;
6496 }
6497 default:
6498 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6499 }
6500
6501 /*
6502 * If we've got a disk, use its blocksize in the block
6503 * descriptor. Otherwise, just set it to 0.
6504 */
6505 if (dbd == 0) {
6506 if (lun->be_lun->lun_type == T_DIRECT)
6507 scsi_ulto3b(lun->be_lun->blocksize,
6508 block_desc->block_len);
6509 else
6510 scsi_ulto3b(0, block_desc->block_len);
6511 }
6512
6513 switch (page_code) {
6514 case SMS_ALL_PAGES_PAGE: {
6515 int i, data_used;
6516
6517 data_used = header_len;
6518 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6519 struct ctl_page_index *page_index;
6520
6521 page_index = &lun->mode_pages.index[i];
6522 if (lun->be_lun->lun_type == T_DIRECT &&
6523 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6524 continue;
6525 if (lun->be_lun->lun_type == T_PROCESSOR &&
6526 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6527 continue;
6528 if (lun->be_lun->lun_type == T_CDROM &&
6529 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6530 continue;
6531
6532 /*
6533 * We don't use this subpage if the user didn't
6534 * request all subpages. We already checked (above)
6535 * to make sure the user only specified a subpage
6536 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6537 */
6538 if ((page_index->subpage != 0)
6539 && (subpage == SMS_SUBPAGE_PAGE_0))
6540 continue;
6541
6542 /*
6543 * Call the handler, if it exists, to update the
6544 * page to the latest values.
6545 */
6546 if (page_index->sense_handler != NULL)
6547 page_index->sense_handler(ctsio, page_index,pc);
6548
6549 memcpy(ctsio->kern_data_ptr + data_used,
6550 page_index->page_data +
6551 (page_index->page_len * pc),
6552 page_index->page_len);
6553 data_used += page_index->page_len;
6554 }
6555 break;
6556 }
6557 default: {
6558 int i, data_used;
6559
6560 data_used = header_len;
6561
6562 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6563 struct ctl_page_index *page_index;
6564
6565 page_index = &lun->mode_pages.index[i];
6566
6567 /* Look for the right page code */
6568 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6569 continue;
6570
6571 /* Look for the right subpage or the subpage wildcard*/
6572 if ((page_index->subpage != subpage)
6573 && (subpage != SMS_SUBPAGE_ALL))
6574 continue;
6575
6576 /* Make sure the page is supported for this dev type */
6577 if (lun->be_lun->lun_type == T_DIRECT &&
6578 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6579 continue;
6580 if (lun->be_lun->lun_type == T_PROCESSOR &&
6581 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6582 continue;
6583 if (lun->be_lun->lun_type == T_CDROM &&
6584 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6585 continue;
6586
6587 /*
6588 * Call the handler, if it exists, to update the
6589 * page to the latest values.
6590 */
6591 if (page_index->sense_handler != NULL)
6592 page_index->sense_handler(ctsio, page_index,pc);
6593
6594 memcpy(ctsio->kern_data_ptr + data_used,
6595 page_index->page_data +
6596 (page_index->page_len * pc),
6597 page_index->page_len);
6598 data_used += page_index->page_len;
6599 }
6600 break;
6601 }
6602 }
6603
6604 ctl_set_success(ctsio);
6605 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6606 ctsio->be_move_done = ctl_config_move_done;
6607 ctl_datamove((union ctl_io *)ctsio);
6608 return (CTL_RETVAL_COMPLETE);
6609}
6610
6611int
6612ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6613 struct ctl_page_index *page_index,
6614 int pc)
6615{
6616 struct ctl_lun *lun = CTL_LUN(ctsio);
6617 struct scsi_log_param_header *phdr;
6618 uint8_t *data;
6619 uint64_t val;
6620
6621 data = page_index->page_data;
6622
6623 if (lun->backend->lun_attr != NULL &&
6624 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6625 != UINT64_MAX) {
6626 phdr = (struct scsi_log_param_header *)data;
6627 scsi_ulto2b(0x0001, phdr->param_code);
6628 phdr->param_control = SLP_LBIN | SLP_LP;
6629 phdr->param_len = 8;
6630 data = (uint8_t *)(phdr + 1);
6631 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6632 data[4] = 0x02; /* per-pool */
6633 data += phdr->param_len;
6634 }
6635
6636 if (lun->backend->lun_attr != NULL &&
6637 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6638 != UINT64_MAX) {
6639 phdr = (struct scsi_log_param_header *)data;
6640 scsi_ulto2b(0x0002, phdr->param_code);
6641 phdr->param_control = SLP_LBIN | SLP_LP;
6642 phdr->param_len = 8;
6643 data = (uint8_t *)(phdr + 1);
6644 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6645 data[4] = 0x01; /* per-LUN */
6646 data += phdr->param_len;
6647 }
6648
6649 if (lun->backend->lun_attr != NULL &&
6650 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6651 != UINT64_MAX) {
6652 phdr = (struct scsi_log_param_header *)data;
6653 scsi_ulto2b(0x00f1, phdr->param_code);
6654 phdr->param_control = SLP_LBIN | SLP_LP;
6655 phdr->param_len = 8;
6656 data = (uint8_t *)(phdr + 1);
6657 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6658 data[4] = 0x02; /* per-pool */
6659 data += phdr->param_len;
6660 }
6661
6662 if (lun->backend->lun_attr != NULL &&
6663 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6664 != UINT64_MAX) {
6665 phdr = (struct scsi_log_param_header *)data;
6666 scsi_ulto2b(0x00f2, phdr->param_code);
6667 phdr->param_control = SLP_LBIN | SLP_LP;
6668 phdr->param_len = 8;
6669 data = (uint8_t *)(phdr + 1);
6670 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6671 data[4] = 0x02; /* per-pool */
6672 data += phdr->param_len;
6673 }
6674
6675 page_index->page_len = data - page_index->page_data;
6676 return (0);
6677}
6678
6679int
6680ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6681 struct ctl_page_index *page_index,
6682 int pc)
6683{
6684 struct ctl_lun *lun = CTL_LUN(ctsio);
6685 struct stat_page *data;
6686 uint64_t rn, wn, rb, wb;
6687 struct bintime rt, wt;
6688 int i;
6689
6690 data = (struct stat_page *)page_index->page_data;
6691
6692 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6693 data->sap.hdr.param_control = SLP_LBIN;
6694 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6695 sizeof(struct scsi_log_param_header);
6696 rn = wn = rb = wb = 0;
6697 bintime_clear(&rt);
6698 bintime_clear(&wt);
6699 for (i = 0; i < CTL_MAX_PORTS; i++) {
6700 rn += lun->stats.ports[i].operations[CTL_STATS_READ];
6701 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE];
6702 rb += lun->stats.ports[i].bytes[CTL_STATS_READ];
6703 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE];
6704 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]);
6705 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]);
6706 }
6707 scsi_u64to8b(rn, data->sap.read_num);
6708 scsi_u64to8b(wn, data->sap.write_num);
6709 if (lun->stats.blocksize > 0) {
6710 scsi_u64to8b(wb / lun->stats.blocksize,
6711 data->sap.recvieved_lba);
6712 scsi_u64to8b(rb / lun->stats.blocksize,
6713 data->sap.transmitted_lba);
6714 }
6715 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000),
6716 data->sap.read_int);
6717 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000),
6718 data->sap.write_int);
6719 scsi_u64to8b(0, data->sap.weighted_num);
6720 scsi_u64to8b(0, data->sap.weighted_int);
6721 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6722 data->it.hdr.param_control = SLP_LBIN;
6723 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6724 sizeof(struct scsi_log_param_header);
6725#ifdef CTL_TIME_IO
6726 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6727#endif
6728 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6729 data->it.hdr.param_control = SLP_LBIN;
6730 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6731 sizeof(struct scsi_log_param_header);
6732 scsi_ulto4b(3, data->ti.exponent);
6733 scsi_ulto4b(1, data->ti.integer);
6734 return (0);
6735}
6736
6737int
6738ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio,
6739 struct ctl_page_index *page_index,
6740 int pc)
6741{
6742 struct ctl_lun *lun = CTL_LUN(ctsio);
6743 struct scsi_log_informational_exceptions *data;
6744
6745 data = (struct scsi_log_informational_exceptions *)page_index->page_data;
6746
6747 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code);
6748 data->hdr.param_control = SLP_LBIN;
6749 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) -
6750 sizeof(struct scsi_log_param_header);
6751 data->ie_asc = lun->ie_asc;
6752 data->ie_ascq = lun->ie_ascq;
6753 data->temperature = 0xff;
6754 return (0);
6755}
6756
6757int
6758ctl_log_sense(struct ctl_scsiio *ctsio)
6759{
6760 struct ctl_lun *lun = CTL_LUN(ctsio);
6761 int i, pc, page_code, subpage;
6762 int alloc_len, total_len;
6763 struct ctl_page_index *page_index;
6764 struct scsi_log_sense *cdb;
6765 struct scsi_log_header *header;
6766
6767 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6768
6769 cdb = (struct scsi_log_sense *)ctsio->cdb;
6770 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6771 page_code = cdb->page & SLS_PAGE_CODE;
6772 subpage = cdb->subpage;
6773 alloc_len = scsi_2btoul(cdb->length);
6774
6775 page_index = NULL;
6776 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6777 page_index = &lun->log_pages.index[i];
6778
6779 /* Look for the right page code */
6780 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6781 continue;
6782
6783 /* Look for the right subpage or the subpage wildcard*/
6784 if (page_index->subpage != subpage)
6785 continue;
6786
6787 break;
6788 }
6789 if (i >= CTL_NUM_LOG_PAGES) {
6790 ctl_set_invalid_field(ctsio,
6791 /*sks_valid*/ 1,
6792 /*command*/ 1,
6793 /*field*/ 2,
6794 /*bit_valid*/ 0,
6795 /*bit*/ 0);
6796 ctl_done((union ctl_io *)ctsio);
6797 return (CTL_RETVAL_COMPLETE);
6798 }
6799
6800 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6801
6802 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6803 ctsio->kern_sg_entries = 0;
6804 ctsio->kern_data_resid = 0;
6805 ctsio->kern_rel_offset = 0;
6806 if (total_len < alloc_len) {
6807 ctsio->residual = alloc_len - total_len;
6808 ctsio->kern_data_len = total_len;
6809 ctsio->kern_total_len = total_len;
6810 } else {
6811 ctsio->residual = 0;
6812 ctsio->kern_data_len = alloc_len;
6813 ctsio->kern_total_len = alloc_len;
6814 }
6815
6816 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6817 header->page = page_index->page_code;
6818 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING)
6819 header->page |= SL_DS;
6820 if (page_index->subpage) {
6821 header->page |= SL_SPF;
6822 header->subpage = page_index->subpage;
6823 }
6824 scsi_ulto2b(page_index->page_len, header->datalen);
6825
6826 /*
6827 * Call the handler, if it exists, to update the
6828 * page to the latest values.
6829 */
6830 if (page_index->sense_handler != NULL)
6831 page_index->sense_handler(ctsio, page_index, pc);
6832
6833 memcpy(header + 1, page_index->page_data, page_index->page_len);
6834
6835 ctl_set_success(ctsio);
6836 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6837 ctsio->be_move_done = ctl_config_move_done;
6838 ctl_datamove((union ctl_io *)ctsio);
6839 return (CTL_RETVAL_COMPLETE);
6840}
6841
6842int
6843ctl_read_capacity(struct ctl_scsiio *ctsio)
6844{
6845 struct ctl_lun *lun = CTL_LUN(ctsio);
6846 struct scsi_read_capacity *cdb;
6847 struct scsi_read_capacity_data *data;
6848 uint32_t lba;
6849
6850 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6851
6852 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6853
6854 lba = scsi_4btoul(cdb->addr);
6855 if (((cdb->pmi & SRC_PMI) == 0)
6856 && (lba != 0)) {
6857 ctl_set_invalid_field(/*ctsio*/ ctsio,
6858 /*sks_valid*/ 1,
6859 /*command*/ 1,
6860 /*field*/ 2,
6861 /*bit_valid*/ 0,
6862 /*bit*/ 0);
6863 ctl_done((union ctl_io *)ctsio);
6864 return (CTL_RETVAL_COMPLETE);
6865 }
6866
6867 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6868 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6869 ctsio->residual = 0;
6870 ctsio->kern_data_len = sizeof(*data);
6871 ctsio->kern_total_len = sizeof(*data);
6872 ctsio->kern_data_resid = 0;
6873 ctsio->kern_rel_offset = 0;
6874 ctsio->kern_sg_entries = 0;
6875
6876 /*
6877 * If the maximum LBA is greater than 0xfffffffe, the user must
6878 * issue a SERVICE ACTION IN (16) command, with the read capacity
6879 * serivce action set.
6880 */
6881 if (lun->be_lun->maxlba > 0xfffffffe)
6882 scsi_ulto4b(0xffffffff, data->addr);
6883 else
6884 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6885
6886 /*
6887 * XXX KDM this may not be 512 bytes...
6888 */
6889 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6890
6891 ctl_set_success(ctsio);
6892 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6893 ctsio->be_move_done = ctl_config_move_done;
6894 ctl_datamove((union ctl_io *)ctsio);
6895 return (CTL_RETVAL_COMPLETE);
6896}
6897
6898int
6899ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6900{
6901 struct ctl_lun *lun = CTL_LUN(ctsio);
6902 struct scsi_read_capacity_16 *cdb;
6903 struct scsi_read_capacity_data_long *data;
6904 uint64_t lba;
6905 uint32_t alloc_len;
6906
6907 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6908
6909 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6910
6911 alloc_len = scsi_4btoul(cdb->alloc_len);
6912 lba = scsi_8btou64(cdb->addr);
6913
6914 if ((cdb->reladr & SRC16_PMI)
6915 && (lba != 0)) {
6916 ctl_set_invalid_field(/*ctsio*/ ctsio,
6917 /*sks_valid*/ 1,
6918 /*command*/ 1,
6919 /*field*/ 2,
6920 /*bit_valid*/ 0,
6921 /*bit*/ 0);
6922 ctl_done((union ctl_io *)ctsio);
6923 return (CTL_RETVAL_COMPLETE);
6924 }
6925
6926 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6927 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6928
6929 if (sizeof(*data) < alloc_len) {
6930 ctsio->residual = alloc_len - sizeof(*data);
6931 ctsio->kern_data_len = sizeof(*data);
6932 ctsio->kern_total_len = sizeof(*data);
6933 } else {
6934 ctsio->residual = 0;
6935 ctsio->kern_data_len = alloc_len;
6936 ctsio->kern_total_len = alloc_len;
6937 }
6938 ctsio->kern_data_resid = 0;
6939 ctsio->kern_rel_offset = 0;
6940 ctsio->kern_sg_entries = 0;
6941
6942 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
6943 /* XXX KDM this may not be 512 bytes... */
6944 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6945 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
6946 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
6947 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
6948 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
6949
6950 ctl_set_success(ctsio);
6951 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6952 ctsio->be_move_done = ctl_config_move_done;
6953 ctl_datamove((union ctl_io *)ctsio);
6954 return (CTL_RETVAL_COMPLETE);
6955}
6956
6957int
6958ctl_get_lba_status(struct ctl_scsiio *ctsio)
6959{
6960 struct ctl_lun *lun = CTL_LUN(ctsio);
6961 struct scsi_get_lba_status *cdb;
6962 struct scsi_get_lba_status_data *data;
6963 struct ctl_lba_len_flags *lbalen;
6964 uint64_t lba;
6965 uint32_t alloc_len, total_len;
6966 int retval;
6967
6968 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
6969
6970 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
6971 lba = scsi_8btou64(cdb->addr);
6972 alloc_len = scsi_4btoul(cdb->alloc_len);
6973
6974 if (lba > lun->be_lun->maxlba) {
6975 ctl_set_lba_out_of_range(ctsio, lba);
6976 ctl_done((union ctl_io *)ctsio);
6977 return (CTL_RETVAL_COMPLETE);
6978 }
6979
6980 total_len = sizeof(*data) + sizeof(data->descr[0]);
6981 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6982 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
6983
6984 if (total_len < alloc_len) {
6985 ctsio->residual = alloc_len - total_len;
6986 ctsio->kern_data_len = total_len;
6987 ctsio->kern_total_len = total_len;
6988 } else {
6989 ctsio->residual = 0;
6990 ctsio->kern_data_len = alloc_len;
6991 ctsio->kern_total_len = alloc_len;
6992 }
6993 ctsio->kern_data_resid = 0;
6994 ctsio->kern_rel_offset = 0;
6995 ctsio->kern_sg_entries = 0;
6996
6997 /* Fill dummy data in case backend can't tell anything. */
6998 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
6999 scsi_u64to8b(lba, data->descr[0].addr);
7000 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
7001 data->descr[0].length);
7002 data->descr[0].status = 0; /* Mapped or unknown. */
7003
7004 ctl_set_success(ctsio);
7005 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7006 ctsio->be_move_done = ctl_config_move_done;
7007
7008 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7009 lbalen->lba = lba;
7010 lbalen->len = total_len;
7011 lbalen->flags = 0;
7012 retval = lun->backend->config_read((union ctl_io *)ctsio);
7013 return (CTL_RETVAL_COMPLETE);
7014}
7015
7016int
7017ctl_read_defect(struct ctl_scsiio *ctsio)
7018{
7019 struct scsi_read_defect_data_10 *ccb10;
7020 struct scsi_read_defect_data_12 *ccb12;
7021 struct scsi_read_defect_data_hdr_10 *data10;
7022 struct scsi_read_defect_data_hdr_12 *data12;
7023 uint32_t alloc_len, data_len;
7024 uint8_t format;
7025
7026 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7027
7028 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7029 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7030 format = ccb10->format;
7031 alloc_len = scsi_2btoul(ccb10->alloc_length);
7032 data_len = sizeof(*data10);
7033 } else {
7034 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7035 format = ccb12->format;
7036 alloc_len = scsi_4btoul(ccb12->alloc_length);
7037 data_len = sizeof(*data12);
7038 }
7039 if (alloc_len == 0) {
7040 ctl_set_success(ctsio);
7041 ctl_done((union ctl_io *)ctsio);
7042 return (CTL_RETVAL_COMPLETE);
7043 }
7044
7045 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7046 if (data_len < alloc_len) {
7047 ctsio->residual = alloc_len - data_len;
7048 ctsio->kern_data_len = data_len;
7049 ctsio->kern_total_len = data_len;
7050 } else {
7051 ctsio->residual = 0;
7052 ctsio->kern_data_len = alloc_len;
7053 ctsio->kern_total_len = alloc_len;
7054 }
7055 ctsio->kern_data_resid = 0;
7056 ctsio->kern_rel_offset = 0;
7057 ctsio->kern_sg_entries = 0;
7058
7059 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7060 data10 = (struct scsi_read_defect_data_hdr_10 *)
7061 ctsio->kern_data_ptr;
7062 data10->format = format;
7063 scsi_ulto2b(0, data10->length);
7064 } else {
7065 data12 = (struct scsi_read_defect_data_hdr_12 *)
7066 ctsio->kern_data_ptr;
7067 data12->format = format;
7068 scsi_ulto2b(0, data12->generation);
7069 scsi_ulto4b(0, data12->length);
7070 }
7071
7072 ctl_set_success(ctsio);
7073 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7074 ctsio->be_move_done = ctl_config_move_done;
7075 ctl_datamove((union ctl_io *)ctsio);
7076 return (CTL_RETVAL_COMPLETE);
7077}
7078
7079int
7080ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7081{
7082 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7083 struct ctl_lun *lun = CTL_LUN(ctsio);
7084 struct scsi_maintenance_in *cdb;
7085 int retval;
7086 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os;
7087 int num_ha_groups, num_target_ports, shared_group;
7088 struct ctl_port *port;
7089 struct scsi_target_group_data *rtg_ptr;
7090 struct scsi_target_group_data_extended *rtg_ext_ptr;
7091 struct scsi_target_port_group_descriptor *tpg_desc;
7092
7093 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7094
7095 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7096 retval = CTL_RETVAL_COMPLETE;
7097
7098 switch (cdb->byte2 & STG_PDF_MASK) {
7099 case STG_PDF_LENGTH:
7100 ext = 0;
7101 break;
7102 case STG_PDF_EXTENDED:
7103 ext = 1;
7104 break;
7105 default:
7106 ctl_set_invalid_field(/*ctsio*/ ctsio,
7107 /*sks_valid*/ 1,
7108 /*command*/ 1,
7109 /*field*/ 2,
7110 /*bit_valid*/ 1,
7111 /*bit*/ 5);
7112 ctl_done((union ctl_io *)ctsio);
7113 return(retval);
7114 }
7115
7116 num_target_ports = 0;
7117 shared_group = (softc->is_single != 0);
7118 mtx_lock(&softc->ctl_lock);
7119 STAILQ_FOREACH(port, &softc->port_list, links) {
7120 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7121 continue;
7122 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7123 continue;
7124 num_target_ports++;
7125 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7126 shared_group = 1;
7127 }
7128 mtx_unlock(&softc->ctl_lock);
7129 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES;
7130
7131 if (ext)
7132 total_len = sizeof(struct scsi_target_group_data_extended);
7133 else
7134 total_len = sizeof(struct scsi_target_group_data);
7135 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7136 (shared_group + num_ha_groups) +
7137 sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7138
7139 alloc_len = scsi_4btoul(cdb->length);
7140
7141 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7142
7143 ctsio->kern_sg_entries = 0;
7144
7145 if (total_len < alloc_len) {
7146 ctsio->residual = alloc_len - total_len;
7147 ctsio->kern_data_len = total_len;
7148 ctsio->kern_total_len = total_len;
7149 } else {
7150 ctsio->residual = 0;
7151 ctsio->kern_data_len = alloc_len;
7152 ctsio->kern_total_len = alloc_len;
7153 }
7154 ctsio->kern_data_resid = 0;
7155 ctsio->kern_rel_offset = 0;
7156
7157 if (ext) {
7158 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7159 ctsio->kern_data_ptr;
7160 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7161 rtg_ext_ptr->format_type = 0x10;
7162 rtg_ext_ptr->implicit_transition_time = 0;
7163 tpg_desc = &rtg_ext_ptr->groups[0];
7164 } else {
7165 rtg_ptr = (struct scsi_target_group_data *)
7166 ctsio->kern_data_ptr;
7167 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7168 tpg_desc = &rtg_ptr->groups[0];
7169 }
7170
7171 mtx_lock(&softc->ctl_lock);
7172 pg = softc->port_min / softc->port_cnt;
7173 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) {
7174 /* Some shelf is known to be primary. */
7175 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7176 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7177 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7178 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7179 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7180 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7181 else
7182 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7183 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7184 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7185 } else {
7186 ts = os;
7187 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7188 }
7189 } else {
7190 /* No known primary shelf. */
7191 if (softc->ha_link == CTL_HA_LINK_OFFLINE) {
7192 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7193 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7194 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) {
7195 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7196 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7197 } else {
7198 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7199 }
7200 }
7201 if (shared_group) {
7202 tpg_desc->pref_state = ts;
7203 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7204 TPG_U_SUP | TPG_T_SUP;
7205 scsi_ulto2b(1, tpg_desc->target_port_group);
7206 tpg_desc->status = TPG_IMPLICIT;
7207 pc = 0;
7208 STAILQ_FOREACH(port, &softc->port_list, links) {
7209 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7210 continue;
7211 if (!softc->is_single &&
7212 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0)
7213 continue;
7214 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7215 continue;
7216 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7217 relative_target_port_identifier);
7218 pc++;
7219 }
7220 tpg_desc->target_port_count = pc;
7221 tpg_desc = (struct scsi_target_port_group_descriptor *)
7222 &tpg_desc->descriptors[pc];
7223 }
7224 for (g = 0; g < num_ha_groups; g++) {
7225 tpg_desc->pref_state = (g == pg) ? ts : os;
7226 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7227 TPG_U_SUP | TPG_T_SUP;
7228 scsi_ulto2b(2 + g, tpg_desc->target_port_group);
7229 tpg_desc->status = TPG_IMPLICIT;
7230 pc = 0;
7231 STAILQ_FOREACH(port, &softc->port_list, links) {
7232 if (port->targ_port < g * softc->port_cnt ||
7233 port->targ_port >= (g + 1) * softc->port_cnt)
7234 continue;
7235 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7236 continue;
7237 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7238 continue;
7239 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7240 continue;
7241 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7242 relative_target_port_identifier);
7243 pc++;
7244 }
7245 tpg_desc->target_port_count = pc;
7246 tpg_desc = (struct scsi_target_port_group_descriptor *)
7247 &tpg_desc->descriptors[pc];
7248 }
7249 mtx_unlock(&softc->ctl_lock);
7250
7251 ctl_set_success(ctsio);
7252 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7253 ctsio->be_move_done = ctl_config_move_done;
7254 ctl_datamove((union ctl_io *)ctsio);
7255 return(retval);
7256}
7257
7258int
7259ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7260{
7261 struct ctl_lun *lun = CTL_LUN(ctsio);
7262 struct scsi_report_supported_opcodes *cdb;
7263 const struct ctl_cmd_entry *entry, *sentry;
7264 struct scsi_report_supported_opcodes_all *all;
7265 struct scsi_report_supported_opcodes_descr *descr;
7266 struct scsi_report_supported_opcodes_one *one;
7267 int retval;
7268 int alloc_len, total_len;
7269 int opcode, service_action, i, j, num;
7270
7271 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7272
7273 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7274 retval = CTL_RETVAL_COMPLETE;
7275
7276 opcode = cdb->requested_opcode;
7277 service_action = scsi_2btoul(cdb->requested_service_action);
7278 switch (cdb->options & RSO_OPTIONS_MASK) {
7279 case RSO_OPTIONS_ALL:
7280 num = 0;
7281 for (i = 0; i < 256; i++) {
7282 entry = &ctl_cmd_table[i];
7283 if (entry->flags & CTL_CMD_FLAG_SA5) {
7284 for (j = 0; j < 32; j++) {
7285 sentry = &((const struct ctl_cmd_entry *)
7286 entry->execute)[j];
7287 if (ctl_cmd_applicable(
7288 lun->be_lun->lun_type, sentry))
7289 num++;
7290 }
7291 } else {
7292 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7293 entry))
7294 num++;
7295 }
7296 }
7297 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7298 num * sizeof(struct scsi_report_supported_opcodes_descr);
7299 break;
7300 case RSO_OPTIONS_OC:
7301 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7302 ctl_set_invalid_field(/*ctsio*/ ctsio,
7303 /*sks_valid*/ 1,
7304 /*command*/ 1,
7305 /*field*/ 2,
7306 /*bit_valid*/ 1,
7307 /*bit*/ 2);
7308 ctl_done((union ctl_io *)ctsio);
7309 return (CTL_RETVAL_COMPLETE);
7310 }
7311 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7312 break;
7313 case RSO_OPTIONS_OC_SA:
7314 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7315 service_action >= 32) {
7316 ctl_set_invalid_field(/*ctsio*/ ctsio,
7317 /*sks_valid*/ 1,
7318 /*command*/ 1,
7319 /*field*/ 2,
7320 /*bit_valid*/ 1,
7321 /*bit*/ 2);
7322 ctl_done((union ctl_io *)ctsio);
7323 return (CTL_RETVAL_COMPLETE);
7324 }
7325 /* FALLTHROUGH */
7326 case RSO_OPTIONS_OC_ASA:
7327 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7328 break;
7329 default:
7330 ctl_set_invalid_field(/*ctsio*/ ctsio,
7331 /*sks_valid*/ 1,
7332 /*command*/ 1,
7333 /*field*/ 2,
7334 /*bit_valid*/ 1,
7335 /*bit*/ 2);
7336 ctl_done((union ctl_io *)ctsio);
7337 return (CTL_RETVAL_COMPLETE);
7338 }
7339
7340 alloc_len = scsi_4btoul(cdb->length);
7341
7342 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7343
7344 ctsio->kern_sg_entries = 0;
7345
7346 if (total_len < alloc_len) {
7347 ctsio->residual = alloc_len - total_len;
7348 ctsio->kern_data_len = total_len;
7349 ctsio->kern_total_len = total_len;
7350 } else {
7351 ctsio->residual = 0;
7352 ctsio->kern_data_len = alloc_len;
7353 ctsio->kern_total_len = alloc_len;
7354 }
7355 ctsio->kern_data_resid = 0;
7356 ctsio->kern_rel_offset = 0;
7357
7358 switch (cdb->options & RSO_OPTIONS_MASK) {
7359 case RSO_OPTIONS_ALL:
7360 all = (struct scsi_report_supported_opcodes_all *)
7361 ctsio->kern_data_ptr;
7362 num = 0;
7363 for (i = 0; i < 256; i++) {
7364 entry = &ctl_cmd_table[i];
7365 if (entry->flags & CTL_CMD_FLAG_SA5) {
7366 for (j = 0; j < 32; j++) {
7367 sentry = &((const struct ctl_cmd_entry *)
7368 entry->execute)[j];
7369 if (!ctl_cmd_applicable(
7370 lun->be_lun->lun_type, sentry))
7371 continue;
7372 descr = &all->descr[num++];
7373 descr->opcode = i;
7374 scsi_ulto2b(j, descr->service_action);
7375 descr->flags = RSO_SERVACTV;
7376 scsi_ulto2b(sentry->length,
7377 descr->cdb_length);
7378 }
7379 } else {
7380 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7381 entry))
7382 continue;
7383 descr = &all->descr[num++];
7384 descr->opcode = i;
7385 scsi_ulto2b(0, descr->service_action);
7386 descr->flags = 0;
7387 scsi_ulto2b(entry->length, descr->cdb_length);
7388 }
7389 }
7390 scsi_ulto4b(
7391 num * sizeof(struct scsi_report_supported_opcodes_descr),
7392 all->length);
7393 break;
7394 case RSO_OPTIONS_OC:
7395 one = (struct scsi_report_supported_opcodes_one *)
7396 ctsio->kern_data_ptr;
7397 entry = &ctl_cmd_table[opcode];
7398 goto fill_one;
7399 case RSO_OPTIONS_OC_SA:
7400 one = (struct scsi_report_supported_opcodes_one *)
7401 ctsio->kern_data_ptr;
7402 entry = &ctl_cmd_table[opcode];
7403 entry = &((const struct ctl_cmd_entry *)
7404 entry->execute)[service_action];
7405fill_one:
7406 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7407 one->support = 3;
7408 scsi_ulto2b(entry->length, one->cdb_length);
7409 one->cdb_usage[0] = opcode;
7410 memcpy(&one->cdb_usage[1], entry->usage,
7411 entry->length - 1);
7412 } else
7413 one->support = 1;
7414 break;
7415 case RSO_OPTIONS_OC_ASA:
7416 one = (struct scsi_report_supported_opcodes_one *)
7417 ctsio->kern_data_ptr;
7418 entry = &ctl_cmd_table[opcode];
7419 if (entry->flags & CTL_CMD_FLAG_SA5) {
7420 entry = &((const struct ctl_cmd_entry *)
7421 entry->execute)[service_action];
7422 } else if (service_action != 0) {
7423 one->support = 1;
7424 break;
7425 }
7426 goto fill_one;
7427 }
7428
7429 ctl_set_success(ctsio);
7430 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7431 ctsio->be_move_done = ctl_config_move_done;
7432 ctl_datamove((union ctl_io *)ctsio);
7433 return(retval);
7434}
7435
7436int
7437ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7438{
7439 struct scsi_report_supported_tmf *cdb;
7440 struct scsi_report_supported_tmf_ext_data *data;
7441 int retval;
7442 int alloc_len, total_len;
7443
7444 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7445
7446 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7447
7448 retval = CTL_RETVAL_COMPLETE;
7449
7450 if (cdb->options & RST_REPD)
7451 total_len = sizeof(struct scsi_report_supported_tmf_ext_data);
7452 else
7453 total_len = sizeof(struct scsi_report_supported_tmf_data);
7454 alloc_len = scsi_4btoul(cdb->length);
7455
7456 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7457
7458 ctsio->kern_sg_entries = 0;
7459
7460 if (total_len < alloc_len) {
7461 ctsio->residual = alloc_len - total_len;
7462 ctsio->kern_data_len = total_len;
7463 ctsio->kern_total_len = total_len;
7464 } else {
7465 ctsio->residual = 0;
7466 ctsio->kern_data_len = alloc_len;
7467 ctsio->kern_total_len = alloc_len;
7468 }
7469 ctsio->kern_data_resid = 0;
7470 ctsio->kern_rel_offset = 0;
7471
7472 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr;
7473 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7474 RST_TRS;
7475 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7476 data->length = total_len - 4;
7477
7478 ctl_set_success(ctsio);
7479 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7480 ctsio->be_move_done = ctl_config_move_done;
7481 ctl_datamove((union ctl_io *)ctsio);
7482 return (retval);
7483}
7484
7485int
7486ctl_report_timestamp(struct ctl_scsiio *ctsio)
7487{
7488 struct scsi_report_timestamp *cdb;
7489 struct scsi_report_timestamp_data *data;
7490 struct timeval tv;
7491 int64_t timestamp;
7492 int retval;
7493 int alloc_len, total_len;
7494
7495 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7496
7497 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7498
7499 retval = CTL_RETVAL_COMPLETE;
7500
7501 total_len = sizeof(struct scsi_report_timestamp_data);
7502 alloc_len = scsi_4btoul(cdb->length);
7503
7504 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7505
7506 ctsio->kern_sg_entries = 0;
7507
7508 if (total_len < alloc_len) {
7509 ctsio->residual = alloc_len - total_len;
7510 ctsio->kern_data_len = total_len;
7511 ctsio->kern_total_len = total_len;
7512 } else {
7513 ctsio->residual = 0;
7514 ctsio->kern_data_len = alloc_len;
7515 ctsio->kern_total_len = alloc_len;
7516 }
7517 ctsio->kern_data_resid = 0;
7518 ctsio->kern_rel_offset = 0;
7519
7520 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7521 scsi_ulto2b(sizeof(*data) - 2, data->length);
7522 data->origin = RTS_ORIG_OUTSIDE;
7523 getmicrotime(&tv);
7524 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7525 scsi_ulto4b(timestamp >> 16, data->timestamp);
7526 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7527
7528 ctl_set_success(ctsio);
7529 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7530 ctsio->be_move_done = ctl_config_move_done;
7531 ctl_datamove((union ctl_io *)ctsio);
7532 return (retval);
7533}
7534
7535int
7536ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7537{
7538 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7539 struct ctl_lun *lun = CTL_LUN(ctsio);
7540 struct scsi_per_res_in *cdb;
7541 int alloc_len, total_len = 0;
7542 /* struct scsi_per_res_in_rsrv in_data; */
7543 uint64_t key;
7544
7545 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7546
7547 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7548
7549 alloc_len = scsi_2btoul(cdb->length);
7550
7551retry:
7552 mtx_lock(&lun->lun_lock);
7553 switch (cdb->action) {
7554 case SPRI_RK: /* read keys */
7555 total_len = sizeof(struct scsi_per_res_in_keys) +
7556 lun->pr_key_count *
7557 sizeof(struct scsi_per_res_key);
7558 break;
7559 case SPRI_RR: /* read reservation */
7560 if (lun->flags & CTL_LUN_PR_RESERVED)
7561 total_len = sizeof(struct scsi_per_res_in_rsrv);
7562 else
7563 total_len = sizeof(struct scsi_per_res_in_header);
7564 break;
7565 case SPRI_RC: /* report capabilities */
7566 total_len = sizeof(struct scsi_per_res_cap);
7567 break;
7568 case SPRI_RS: /* read full status */
7569 total_len = sizeof(struct scsi_per_res_in_header) +
7570 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7571 lun->pr_key_count;
7572 break;
7573 default:
7574 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7575 }
7576 mtx_unlock(&lun->lun_lock);
7577
7578 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7579
7580 if (total_len < alloc_len) {
7581 ctsio->residual = alloc_len - total_len;
7582 ctsio->kern_data_len = total_len;
7583 ctsio->kern_total_len = total_len;
7584 } else {
7585 ctsio->residual = 0;
7586 ctsio->kern_data_len = alloc_len;
7587 ctsio->kern_total_len = alloc_len;
7588 }
7589
7590 ctsio->kern_data_resid = 0;
7591 ctsio->kern_rel_offset = 0;
7592 ctsio->kern_sg_entries = 0;
7593
7594 mtx_lock(&lun->lun_lock);
7595 switch (cdb->action) {
7596 case SPRI_RK: { // read keys
7597 struct scsi_per_res_in_keys *res_keys;
7598 int i, key_count;
7599
7600 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7601
7602 /*
7603 * We had to drop the lock to allocate our buffer, which
7604 * leaves time for someone to come in with another
7605 * persistent reservation. (That is unlikely, though,
7606 * since this should be the only persistent reservation
7607 * command active right now.)
7608 */
7609 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7610 (lun->pr_key_count *
7611 sizeof(struct scsi_per_res_key)))){
7612 mtx_unlock(&lun->lun_lock);
7613 free(ctsio->kern_data_ptr, M_CTL);
7614 printf("%s: reservation length changed, retrying\n",
7615 __func__);
7616 goto retry;
7617 }
7618
7619 scsi_ulto4b(lun->pr_generation, res_keys->header.generation);
7620
7621 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7622 lun->pr_key_count, res_keys->header.length);
7623
7624 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7625 if ((key = ctl_get_prkey(lun, i)) == 0)
7626 continue;
7627
7628 /*
7629 * We used lun->pr_key_count to calculate the
7630 * size to allocate. If it turns out the number of
7631 * initiators with the registered flag set is
7632 * larger than that (i.e. they haven't been kept in
7633 * sync), we've got a problem.
7634 */
7635 if (key_count >= lun->pr_key_count) {
7636 key_count++;
7637 continue;
7638 }
7639 scsi_u64to8b(key, res_keys->keys[key_count].key);
7640 key_count++;
7641 }
7642 break;
7643 }
7644 case SPRI_RR: { // read reservation
7645 struct scsi_per_res_in_rsrv *res;
7646 int tmp_len, header_only;
7647
7648 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7649
7650 scsi_ulto4b(lun->pr_generation, res->header.generation);
7651
7652 if (lun->flags & CTL_LUN_PR_RESERVED)
7653 {
7654 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7655 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7656 res->header.length);
7657 header_only = 0;
7658 } else {
7659 tmp_len = sizeof(struct scsi_per_res_in_header);
7660 scsi_ulto4b(0, res->header.length);
7661 header_only = 1;
7662 }
7663
7664 /*
7665 * We had to drop the lock to allocate our buffer, which
7666 * leaves time for someone to come in with another
7667 * persistent reservation. (That is unlikely, though,
7668 * since this should be the only persistent reservation
7669 * command active right now.)
7670 */
7671 if (tmp_len != total_len) {
7672 mtx_unlock(&lun->lun_lock);
7673 free(ctsio->kern_data_ptr, M_CTL);
7674 printf("%s: reservation status changed, retrying\n",
7675 __func__);
7676 goto retry;
7677 }
7678
7679 /*
7680 * No reservation held, so we're done.
7681 */
7682 if (header_only != 0)
7683 break;
7684
7685 /*
7686 * If the registration is an All Registrants type, the key
7687 * is 0, since it doesn't really matter.
7688 */
7689 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7690 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7691 res->data.reservation);
7692 }
7693 res->data.scopetype = lun->pr_res_type;
7694 break;
7695 }
7696 case SPRI_RC: //report capabilities
7697 {
7698 struct scsi_per_res_cap *res_cap;
7699 uint16_t type_mask;
7700
7701 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7702 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7703 res_cap->flags1 = SPRI_CRH;
7704 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5;
7705 type_mask = SPRI_TM_WR_EX_AR |
7706 SPRI_TM_EX_AC_RO |
7707 SPRI_TM_WR_EX_RO |
7708 SPRI_TM_EX_AC |
7709 SPRI_TM_WR_EX |
7710 SPRI_TM_EX_AC_AR;
7711 scsi_ulto2b(type_mask, res_cap->type_mask);
7712 break;
7713 }
7714 case SPRI_RS: { // read full status
7715 struct scsi_per_res_in_full *res_status;
7716 struct scsi_per_res_in_full_desc *res_desc;
7717 struct ctl_port *port;
7718 int i, len;
7719
7720 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7721
7722 /*
7723 * We had to drop the lock to allocate our buffer, which
7724 * leaves time for someone to come in with another
7725 * persistent reservation. (That is unlikely, though,
7726 * since this should be the only persistent reservation
7727 * command active right now.)
7728 */
7729 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7730 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7731 lun->pr_key_count)){
7732 mtx_unlock(&lun->lun_lock);
7733 free(ctsio->kern_data_ptr, M_CTL);
7734 printf("%s: reservation length changed, retrying\n",
7735 __func__);
7736 goto retry;
7737 }
7738
7739 scsi_ulto4b(lun->pr_generation, res_status->header.generation);
7740
7741 res_desc = &res_status->desc[0];
7742 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7743 if ((key = ctl_get_prkey(lun, i)) == 0)
7744 continue;
7745
7746 scsi_u64to8b(key, res_desc->res_key.key);
7747 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7748 (lun->pr_res_idx == i ||
7749 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7750 res_desc->flags = SPRI_FULL_R_HOLDER;
7751 res_desc->scopetype = lun->pr_res_type;
7752 }
7753 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7754 res_desc->rel_trgt_port_id);
7755 len = 0;
7756 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7757 if (port != NULL)
7758 len = ctl_create_iid(port,
7759 i % CTL_MAX_INIT_PER_PORT,
7760 res_desc->transport_id);
7761 scsi_ulto4b(len, res_desc->additional_length);
7762 res_desc = (struct scsi_per_res_in_full_desc *)
7763 &res_desc->transport_id[len];
7764 }
7765 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7766 res_status->header.length);
7767 break;
7768 }
7769 default:
7770 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7771 }
7772 mtx_unlock(&lun->lun_lock);
7773
7774 ctl_set_success(ctsio);
7775 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7776 ctsio->be_move_done = ctl_config_move_done;
7777 ctl_datamove((union ctl_io *)ctsio);
7778 return (CTL_RETVAL_COMPLETE);
7779}
7780
7781/*
7782 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7783 * it should return.
7784 */
7785static int
7786ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7787 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7788 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7789 struct scsi_per_res_out_parms* param)
7790{
7791 union ctl_ha_msg persis_io;
7792 int i;
7793
7794 mtx_lock(&lun->lun_lock);
7795 if (sa_res_key == 0) {
7796 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7797 /* validate scope and type */
7798 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7799 SPR_LU_SCOPE) {
7800 mtx_unlock(&lun->lun_lock);
7801 ctl_set_invalid_field(/*ctsio*/ ctsio,
7802 /*sks_valid*/ 1,
7803 /*command*/ 1,
7804 /*field*/ 2,
7805 /*bit_valid*/ 1,
7806 /*bit*/ 4);
7807 ctl_done((union ctl_io *)ctsio);
7808 return (1);
7809 }
7810
7811 if (type>8 || type==2 || type==4 || type==0) {
7812 mtx_unlock(&lun->lun_lock);
7813 ctl_set_invalid_field(/*ctsio*/ ctsio,
7814 /*sks_valid*/ 1,
7815 /*command*/ 1,
7816 /*field*/ 2,
7817 /*bit_valid*/ 1,
7818 /*bit*/ 0);
7819 ctl_done((union ctl_io *)ctsio);
7820 return (1);
7821 }
7822
7823 /*
7824 * Unregister everybody else and build UA for
7825 * them
7826 */
7827 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7828 if (i == residx || ctl_get_prkey(lun, i) == 0)
7829 continue;
7830
7831 ctl_clr_prkey(lun, i);
7832 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7833 }
7834 lun->pr_key_count = 1;
7835 lun->pr_res_type = type;
7836 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7837 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7838 lun->pr_res_idx = residx;
7839 lun->pr_generation++;
7840 mtx_unlock(&lun->lun_lock);
7841
7842 /* send msg to other side */
7843 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7844 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7845 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7846 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7847 persis_io.pr.pr_info.res_type = type;
7848 memcpy(persis_io.pr.pr_info.sa_res_key,
7849 param->serv_act_res_key,
7850 sizeof(param->serv_act_res_key));
7851 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7852 sizeof(persis_io.pr), M_WAITOK);
7853 } else {
7854 /* not all registrants */
7855 mtx_unlock(&lun->lun_lock);
7856 free(ctsio->kern_data_ptr, M_CTL);
7857 ctl_set_invalid_field(ctsio,
7858 /*sks_valid*/ 1,
7859 /*command*/ 0,
7860 /*field*/ 8,
7861 /*bit_valid*/ 0,
7862 /*bit*/ 0);
7863 ctl_done((union ctl_io *)ctsio);
7864 return (1);
7865 }
7866 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7867 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7868 int found = 0;
7869
7870 if (res_key == sa_res_key) {
7871 /* special case */
7872 /*
7873 * The spec implies this is not good but doesn't
7874 * say what to do. There are two choices either
7875 * generate a res conflict or check condition
7876 * with illegal field in parameter data. Since
7877 * that is what is done when the sa_res_key is
7878 * zero I'll take that approach since this has
7879 * to do with the sa_res_key.
7880 */
7881 mtx_unlock(&lun->lun_lock);
7882 free(ctsio->kern_data_ptr, M_CTL);
7883 ctl_set_invalid_field(ctsio,
7884 /*sks_valid*/ 1,
7885 /*command*/ 0,
7886 /*field*/ 8,
7887 /*bit_valid*/ 0,
7888 /*bit*/ 0);
7889 ctl_done((union ctl_io *)ctsio);
7890 return (1);
7891 }
7892
7893 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7894 if (ctl_get_prkey(lun, i) != sa_res_key)
7895 continue;
7896
7897 found = 1;
7898 ctl_clr_prkey(lun, i);
7899 lun->pr_key_count--;
7900 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7901 }
7902 if (!found) {
7903 mtx_unlock(&lun->lun_lock);
7904 free(ctsio->kern_data_ptr, M_CTL);
7905 ctl_set_reservation_conflict(ctsio);
7906 ctl_done((union ctl_io *)ctsio);
7907 return (CTL_RETVAL_COMPLETE);
7908 }
7909 lun->pr_generation++;
7910 mtx_unlock(&lun->lun_lock);
7911
7912 /* send msg to other side */
7913 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7914 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7915 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7916 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7917 persis_io.pr.pr_info.res_type = type;
7918 memcpy(persis_io.pr.pr_info.sa_res_key,
7919 param->serv_act_res_key,
7920 sizeof(param->serv_act_res_key));
7921 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7922 sizeof(persis_io.pr), M_WAITOK);
7923 } else {
7924 /* Reserved but not all registrants */
7925 /* sa_res_key is res holder */
7926 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
7927 /* validate scope and type */
7928 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7929 SPR_LU_SCOPE) {
7930 mtx_unlock(&lun->lun_lock);
7931 ctl_set_invalid_field(/*ctsio*/ ctsio,
7932 /*sks_valid*/ 1,
7933 /*command*/ 1,
7934 /*field*/ 2,
7935 /*bit_valid*/ 1,
7936 /*bit*/ 4);
7937 ctl_done((union ctl_io *)ctsio);
7938 return (1);
7939 }
7940
7941 if (type>8 || type==2 || type==4 || type==0) {
7942 mtx_unlock(&lun->lun_lock);
7943 ctl_set_invalid_field(/*ctsio*/ ctsio,
7944 /*sks_valid*/ 1,
7945 /*command*/ 1,
7946 /*field*/ 2,
7947 /*bit_valid*/ 1,
7948 /*bit*/ 0);
7949 ctl_done((union ctl_io *)ctsio);
7950 return (1);
7951 }
7952
7953 /*
7954 * Do the following:
7955 * if sa_res_key != res_key remove all
7956 * registrants w/sa_res_key and generate UA
7957 * for these registrants(Registrations
7958 * Preempted) if it wasn't an exclusive
7959 * reservation generate UA(Reservations
7960 * Preempted) for all other registered nexuses
7961 * if the type has changed. Establish the new
7962 * reservation and holder. If res_key and
7963 * sa_res_key are the same do the above
7964 * except don't unregister the res holder.
7965 */
7966
7967 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7968 if (i == residx || ctl_get_prkey(lun, i) == 0)
7969 continue;
7970
7971 if (sa_res_key == ctl_get_prkey(lun, i)) {
7972 ctl_clr_prkey(lun, i);
7973 lun->pr_key_count--;
7974 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7975 } else if (type != lun->pr_res_type &&
7976 (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
7977 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
7978 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
7979 }
7980 }
7981 lun->pr_res_type = type;
7982 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7983 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7984 lun->pr_res_idx = residx;
7985 else
7986 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
7987 lun->pr_generation++;
7988 mtx_unlock(&lun->lun_lock);
7989
7990 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7991 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7992 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7993 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7994 persis_io.pr.pr_info.res_type = type;
7995 memcpy(persis_io.pr.pr_info.sa_res_key,
7996 param->serv_act_res_key,
7997 sizeof(param->serv_act_res_key));
7998 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7999 sizeof(persis_io.pr), M_WAITOK);
8000 } else {
8001 /*
8002 * sa_res_key is not the res holder just
8003 * remove registrants
8004 */
8005 int found=0;
8006
8007 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8008 if (sa_res_key != ctl_get_prkey(lun, i))
8009 continue;
8010
8011 found = 1;
8012 ctl_clr_prkey(lun, i);
8013 lun->pr_key_count--;
8014 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8015 }
8016
8017 if (!found) {
8018 mtx_unlock(&lun->lun_lock);
8019 free(ctsio->kern_data_ptr, M_CTL);
8020 ctl_set_reservation_conflict(ctsio);
8021 ctl_done((union ctl_io *)ctsio);
8022 return (1);
8023 }
8024 lun->pr_generation++;
8025 mtx_unlock(&lun->lun_lock);
8026
8027 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8028 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8029 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8030 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8031 persis_io.pr.pr_info.res_type = type;
8032 memcpy(persis_io.pr.pr_info.sa_res_key,
8033 param->serv_act_res_key,
8034 sizeof(param->serv_act_res_key));
8035 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8036 sizeof(persis_io.pr), M_WAITOK);
8037 }
8038 }
8039 return (0);
8040}
8041
8042static void
8043ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8044{
8045 uint64_t sa_res_key;
8046 int i;
8047
8048 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8049
8050 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8051 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8052 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8053 if (sa_res_key == 0) {
8054 /*
8055 * Unregister everybody else and build UA for
8056 * them
8057 */
8058 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8059 if (i == msg->pr.pr_info.residx ||
8060 ctl_get_prkey(lun, i) == 0)
8061 continue;
8062
8063 ctl_clr_prkey(lun, i);
8064 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8065 }
8066
8067 lun->pr_key_count = 1;
8068 lun->pr_res_type = msg->pr.pr_info.res_type;
8069 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8070 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8071 lun->pr_res_idx = msg->pr.pr_info.residx;
8072 } else {
8073 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8074 if (sa_res_key == ctl_get_prkey(lun, i))
8075 continue;
8076
8077 ctl_clr_prkey(lun, i);
8078 lun->pr_key_count--;
8079 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8080 }
8081 }
8082 } else {
8083 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8084 if (i == msg->pr.pr_info.residx ||
8085 ctl_get_prkey(lun, i) == 0)
8086 continue;
8087
8088 if (sa_res_key == ctl_get_prkey(lun, i)) {
8089 ctl_clr_prkey(lun, i);
8090 lun->pr_key_count--;
8091 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8092 } else if (msg->pr.pr_info.res_type != lun->pr_res_type
8093 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8094 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8095 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8096 }
8097 }
8098 lun->pr_res_type = msg->pr.pr_info.res_type;
8099 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8100 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8101 lun->pr_res_idx = msg->pr.pr_info.residx;
8102 else
8103 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8104 }
8105 lun->pr_generation++;
8106
8107}
8108
8109
8110int
8111ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8112{
8113 struct ctl_softc *softc = CTL_SOFTC(ctsio);
8114 struct ctl_lun *lun = CTL_LUN(ctsio);
8115 int retval;
8116 u_int32_t param_len;
8117 struct scsi_per_res_out *cdb;
8118 struct scsi_per_res_out_parms* param;
8119 uint32_t residx;
8120 uint64_t res_key, sa_res_key, key;
8121 uint8_t type;
8122 union ctl_ha_msg persis_io;
8123 int i;
8124
8125 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8126
8127 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8128 retval = CTL_RETVAL_COMPLETE;
8129
8130 /*
8131 * We only support whole-LUN scope. The scope & type are ignored for
8132 * register, register and ignore existing key and clear.
8133 * We sometimes ignore scope and type on preempts too!!
8134 * Verify reservation type here as well.
8135 */
8136 type = cdb->scope_type & SPR_TYPE_MASK;
8137 if ((cdb->action == SPRO_RESERVE)
8138 || (cdb->action == SPRO_RELEASE)) {
8139 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8140 ctl_set_invalid_field(/*ctsio*/ ctsio,
8141 /*sks_valid*/ 1,
8142 /*command*/ 1,
8143 /*field*/ 2,
8144 /*bit_valid*/ 1,
8145 /*bit*/ 4);
8146 ctl_done((union ctl_io *)ctsio);
8147 return (CTL_RETVAL_COMPLETE);
8148 }
8149
8150 if (type>8 || type==2 || type==4 || type==0) {
8151 ctl_set_invalid_field(/*ctsio*/ ctsio,
8152 /*sks_valid*/ 1,
8153 /*command*/ 1,
8154 /*field*/ 2,
8155 /*bit_valid*/ 1,
8156 /*bit*/ 0);
8157 ctl_done((union ctl_io *)ctsio);
8158 return (CTL_RETVAL_COMPLETE);
8159 }
8160 }
8161
8162 param_len = scsi_4btoul(cdb->length);
8163
8164 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8165 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8166 ctsio->kern_data_len = param_len;
8167 ctsio->kern_total_len = param_len;
8168 ctsio->kern_data_resid = 0;
8169 ctsio->kern_rel_offset = 0;
8170 ctsio->kern_sg_entries = 0;
8171 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8172 ctsio->be_move_done = ctl_config_move_done;
8173 ctl_datamove((union ctl_io *)ctsio);
8174
8175 return (CTL_RETVAL_COMPLETE);
8176 }
8177
8178 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8179
8180 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8181 res_key = scsi_8btou64(param->res_key.key);
8182 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8183
8184 /*
8185 * Validate the reservation key here except for SPRO_REG_IGNO
8186 * This must be done for all other service actions
8187 */
8188 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8189 mtx_lock(&lun->lun_lock);
8190 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8191 if (res_key != key) {
8192 /*
8193 * The current key passed in doesn't match
8194 * the one the initiator previously
8195 * registered.
8196 */
8197 mtx_unlock(&lun->lun_lock);
8198 free(ctsio->kern_data_ptr, M_CTL);
8199 ctl_set_reservation_conflict(ctsio);
8200 ctl_done((union ctl_io *)ctsio);
8201 return (CTL_RETVAL_COMPLETE);
8202 }
8203 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8204 /*
8205 * We are not registered
8206 */
8207 mtx_unlock(&lun->lun_lock);
8208 free(ctsio->kern_data_ptr, M_CTL);
8209 ctl_set_reservation_conflict(ctsio);
8210 ctl_done((union ctl_io *)ctsio);
8211 return (CTL_RETVAL_COMPLETE);
8212 } else if (res_key != 0) {
8213 /*
8214 * We are not registered and trying to register but
8215 * the register key isn't zero.
8216 */
8217 mtx_unlock(&lun->lun_lock);
8218 free(ctsio->kern_data_ptr, M_CTL);
8219 ctl_set_reservation_conflict(ctsio);
8220 ctl_done((union ctl_io *)ctsio);
8221 return (CTL_RETVAL_COMPLETE);
8222 }
8223 mtx_unlock(&lun->lun_lock);
8224 }
8225
8226 switch (cdb->action & SPRO_ACTION_MASK) {
8227 case SPRO_REGISTER:
8228 case SPRO_REG_IGNO: {
8229
8230#if 0
8231 printf("Registration received\n");
8232#endif
8233
8234 /*
8235 * We don't support any of these options, as we report in
8236 * the read capabilities request (see
8237 * ctl_persistent_reserve_in(), above).
8238 */
8239 if ((param->flags & SPR_SPEC_I_PT)
8240 || (param->flags & SPR_ALL_TG_PT)
8241 || (param->flags & SPR_APTPL)) {
8242 int bit_ptr;
8243
8244 if (param->flags & SPR_APTPL)
8245 bit_ptr = 0;
8246 else if (param->flags & SPR_ALL_TG_PT)
8247 bit_ptr = 2;
8248 else /* SPR_SPEC_I_PT */
8249 bit_ptr = 3;
8250
8251 free(ctsio->kern_data_ptr, M_CTL);
8252 ctl_set_invalid_field(ctsio,
8253 /*sks_valid*/ 1,
8254 /*command*/ 0,
8255 /*field*/ 20,
8256 /*bit_valid*/ 1,
8257 /*bit*/ bit_ptr);
8258 ctl_done((union ctl_io *)ctsio);
8259 return (CTL_RETVAL_COMPLETE);
8260 }
8261
8262 mtx_lock(&lun->lun_lock);
8263
8264 /*
8265 * The initiator wants to clear the
8266 * key/unregister.
8267 */
8268 if (sa_res_key == 0) {
8269 if ((res_key == 0
8270 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8271 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8272 && ctl_get_prkey(lun, residx) == 0)) {
8273 mtx_unlock(&lun->lun_lock);
8274 goto done;
8275 }
8276
8277 ctl_clr_prkey(lun, residx);
8278 lun->pr_key_count--;
8279
8280 if (residx == lun->pr_res_idx) {
8281 lun->flags &= ~CTL_LUN_PR_RESERVED;
8282 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8283
8284 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8285 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8286 lun->pr_key_count) {
8287 /*
8288 * If the reservation is a registrants
8289 * only type we need to generate a UA
8290 * for other registered inits. The
8291 * sense code should be RESERVATIONS
8292 * RELEASED
8293 */
8294
8295 for (i = softc->init_min; i < softc->init_max; i++){
8296 if (ctl_get_prkey(lun, i) == 0)
8297 continue;
8298 ctl_est_ua(lun, i,
8299 CTL_UA_RES_RELEASE);
8300 }
8301 }
8302 lun->pr_res_type = 0;
8303 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8304 if (lun->pr_key_count==0) {
8305 lun->flags &= ~CTL_LUN_PR_RESERVED;
8306 lun->pr_res_type = 0;
8307 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8308 }
8309 }
8310 lun->pr_generation++;
8311 mtx_unlock(&lun->lun_lock);
8312
8313 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8314 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8315 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8316 persis_io.pr.pr_info.residx = residx;
8317 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8318 sizeof(persis_io.pr), M_WAITOK);
8319 } else /* sa_res_key != 0 */ {
8320
8321 /*
8322 * If we aren't registered currently then increment
8323 * the key count and set the registered flag.
8324 */
8325 ctl_alloc_prkey(lun, residx);
8326 if (ctl_get_prkey(lun, residx) == 0)
8327 lun->pr_key_count++;
8328 ctl_set_prkey(lun, residx, sa_res_key);
8329 lun->pr_generation++;
8330 mtx_unlock(&lun->lun_lock);
8331
8332 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8333 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8334 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8335 persis_io.pr.pr_info.residx = residx;
8336 memcpy(persis_io.pr.pr_info.sa_res_key,
8337 param->serv_act_res_key,
8338 sizeof(param->serv_act_res_key));
8339 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8340 sizeof(persis_io.pr), M_WAITOK);
8341 }
8342
8343 break;
8344 }
8345 case SPRO_RESERVE:
8346#if 0
8347 printf("Reserve executed type %d\n", type);
8348#endif
8349 mtx_lock(&lun->lun_lock);
8350 if (lun->flags & CTL_LUN_PR_RESERVED) {
8351 /*
8352 * if this isn't the reservation holder and it's
8353 * not a "all registrants" type or if the type is
8354 * different then we have a conflict
8355 */
8356 if ((lun->pr_res_idx != residx
8357 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8358 || lun->pr_res_type != type) {
8359 mtx_unlock(&lun->lun_lock);
8360 free(ctsio->kern_data_ptr, M_CTL);
8361 ctl_set_reservation_conflict(ctsio);
8362 ctl_done((union ctl_io *)ctsio);
8363 return (CTL_RETVAL_COMPLETE);
8364 }
8365 mtx_unlock(&lun->lun_lock);
8366 } else /* create a reservation */ {
8367 /*
8368 * If it's not an "all registrants" type record
8369 * reservation holder
8370 */
8371 if (type != SPR_TYPE_WR_EX_AR
8372 && type != SPR_TYPE_EX_AC_AR)
8373 lun->pr_res_idx = residx; /* Res holder */
8374 else
8375 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8376
8377 lun->flags |= CTL_LUN_PR_RESERVED;
8378 lun->pr_res_type = type;
8379
8380 mtx_unlock(&lun->lun_lock);
8381
8382 /* send msg to other side */
8383 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8384 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8385 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8386 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8387 persis_io.pr.pr_info.res_type = type;
8388 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8389 sizeof(persis_io.pr), M_WAITOK);
8390 }
8391 break;
8392
8393 case SPRO_RELEASE:
8394 mtx_lock(&lun->lun_lock);
8395 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8396 /* No reservation exists return good status */
8397 mtx_unlock(&lun->lun_lock);
8398 goto done;
8399 }
8400 /*
8401 * Is this nexus a reservation holder?
8402 */
8403 if (lun->pr_res_idx != residx
8404 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8405 /*
8406 * not a res holder return good status but
8407 * do nothing
8408 */
8409 mtx_unlock(&lun->lun_lock);
8410 goto done;
8411 }
8412
8413 if (lun->pr_res_type != type) {
8414 mtx_unlock(&lun->lun_lock);
8415 free(ctsio->kern_data_ptr, M_CTL);
8416 ctl_set_illegal_pr_release(ctsio);
8417 ctl_done((union ctl_io *)ctsio);
8418 return (CTL_RETVAL_COMPLETE);
8419 }
8420
8421 /* okay to release */
8422 lun->flags &= ~CTL_LUN_PR_RESERVED;
8423 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8424 lun->pr_res_type = 0;
8425
8426 /*
8427 * If this isn't an exclusive access reservation and NUAR
8428 * is not set, generate UA for all other registrants.
8429 */
8430 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX &&
8431 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8432 for (i = softc->init_min; i < softc->init_max; i++) {
8433 if (i == residx || ctl_get_prkey(lun, i) == 0)
8434 continue;
8435 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8436 }
8437 }
8438 mtx_unlock(&lun->lun_lock);
8439
8440 /* Send msg to other side */
8441 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8442 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8443 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8444 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8445 sizeof(persis_io.pr), M_WAITOK);
8446 break;
8447
8448 case SPRO_CLEAR:
8449 /* send msg to other side */
8450
8451 mtx_lock(&lun->lun_lock);
8452 lun->flags &= ~CTL_LUN_PR_RESERVED;
8453 lun->pr_res_type = 0;
8454 lun->pr_key_count = 0;
8455 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8456
8457 ctl_clr_prkey(lun, residx);
8458 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8459 if (ctl_get_prkey(lun, i) != 0) {
8460 ctl_clr_prkey(lun, i);
8461 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8462 }
8463 lun->pr_generation++;
8464 mtx_unlock(&lun->lun_lock);
8465
8466 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8467 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8468 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8469 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8470 sizeof(persis_io.pr), M_WAITOK);
8471 break;
8472
8473 case SPRO_PREEMPT:
8474 case SPRO_PRE_ABO: {
8475 int nretval;
8476
8477 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8478 residx, ctsio, cdb, param);
8479 if (nretval != 0)
8480 return (CTL_RETVAL_COMPLETE);
8481 break;
8482 }
8483 default:
8484 panic("%s: Invalid PR type %#x", __func__, cdb->action);
8485 }
8486
8487done:
8488 free(ctsio->kern_data_ptr, M_CTL);
8489 ctl_set_success(ctsio);
8490 ctl_done((union ctl_io *)ctsio);
8491
8492 return (retval);
8493}
8494
8495/*
8496 * This routine is for handling a message from the other SC pertaining to
8497 * persistent reserve out. All the error checking will have been done
8498 * so only perorming the action need be done here to keep the two
8499 * in sync.
8500 */
8501static void
8502ctl_hndl_per_res_out_on_other_sc(union ctl_io *io)
8503{
8504 struct ctl_softc *softc = CTL_SOFTC(io);
8505 union ctl_ha_msg *msg = (union ctl_ha_msg *)&io->presio.pr_msg;
8506 struct ctl_lun *lun;
8507 int i;
8508 uint32_t residx, targ_lun;
8509
8510 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8511 mtx_lock(&softc->ctl_lock);
8512 if (targ_lun >= CTL_MAX_LUNS ||
8513 (lun = softc->ctl_luns[targ_lun]) == NULL) {
8514 mtx_unlock(&softc->ctl_lock);
8515 return;
8516 }
8517 mtx_lock(&lun->lun_lock);
8518 mtx_unlock(&softc->ctl_lock);
8519 if (lun->flags & CTL_LUN_DISABLED) {
8520 mtx_unlock(&lun->lun_lock);
8521 return;
8522 }
8523 residx = ctl_get_initindex(&msg->hdr.nexus);
8524 switch(msg->pr.pr_info.action) {
8525 case CTL_PR_REG_KEY:
8526 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8527 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8528 lun->pr_key_count++;
8529 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8530 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8531 lun->pr_generation++;
8532 break;
8533
8534 case CTL_PR_UNREG_KEY:
8535 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8536 lun->pr_key_count--;
8537
8538 /* XXX Need to see if the reservation has been released */
8539 /* if so do we need to generate UA? */
8540 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8541 lun->flags &= ~CTL_LUN_PR_RESERVED;
8542 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8543
8544 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8545 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8546 lun->pr_key_count) {
8547 /*
8548 * If the reservation is a registrants
8549 * only type we need to generate a UA
8550 * for other registered inits. The
8551 * sense code should be RESERVATIONS
8552 * RELEASED
8553 */
8554
8555 for (i = softc->init_min; i < softc->init_max; i++) {
8556 if (ctl_get_prkey(lun, i) == 0)
8557 continue;
8558
8559 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8560 }
8561 }
8562 lun->pr_res_type = 0;
8563 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8564 if (lun->pr_key_count==0) {
8565 lun->flags &= ~CTL_LUN_PR_RESERVED;
8566 lun->pr_res_type = 0;
8567 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8568 }
8569 }
8570 lun->pr_generation++;
8571 break;
8572
8573 case CTL_PR_RESERVE:
8574 lun->flags |= CTL_LUN_PR_RESERVED;
8575 lun->pr_res_type = msg->pr.pr_info.res_type;
8576 lun->pr_res_idx = msg->pr.pr_info.residx;
8577
8578 break;
8579
8580 case CTL_PR_RELEASE:
8581 /*
8582 * If this isn't an exclusive access reservation and NUAR
8583 * is not set, generate UA for all other registrants.
8584 */
8585 if (lun->pr_res_type != SPR_TYPE_EX_AC &&
8586 lun->pr_res_type != SPR_TYPE_WR_EX &&
8587 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8588 for (i = softc->init_min; i < softc->init_max; i++)
8589 if (i == residx || ctl_get_prkey(lun, i) == 0)
8590 continue;
8591 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8592 }
8593
8594 lun->flags &= ~CTL_LUN_PR_RESERVED;
8595 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8596 lun->pr_res_type = 0;
8597 break;
8598
8599 case CTL_PR_PREEMPT:
8600 ctl_pro_preempt_other(lun, msg);
8601 break;
8602 case CTL_PR_CLEAR:
8603 lun->flags &= ~CTL_LUN_PR_RESERVED;
8604 lun->pr_res_type = 0;
8605 lun->pr_key_count = 0;
8606 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8607
8608 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8609 if (ctl_get_prkey(lun, i) == 0)
8610 continue;
8611 ctl_clr_prkey(lun, i);
8612 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8613 }
8614 lun->pr_generation++;
8615 break;
8616 }
8617
8618 mtx_unlock(&lun->lun_lock);
8619}
8620
8621int
8622ctl_read_write(struct ctl_scsiio *ctsio)
8623{
8624 struct ctl_lun *lun = CTL_LUN(ctsio);
8625 struct ctl_lba_len_flags *lbalen;
8626 uint64_t lba;
8627 uint32_t num_blocks;
8628 int flags, retval;
8629 int isread;
8630
8631 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8632
8633 flags = 0;
8634 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8635 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8636 switch (ctsio->cdb[0]) {
8637 case READ_6:
8638 case WRITE_6: {
8639 struct scsi_rw_6 *cdb;
8640
8641 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8642
8643 lba = scsi_3btoul(cdb->addr);
8644 /* only 5 bits are valid in the most significant address byte */
8645 lba &= 0x1fffff;
8646 num_blocks = cdb->length;
8647 /*
8648 * This is correct according to SBC-2.
8649 */
8650 if (num_blocks == 0)
8651 num_blocks = 256;
8652 break;
8653 }
8654 case READ_10:
8655 case WRITE_10: {
8656 struct scsi_rw_10 *cdb;
8657
8658 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8659 if (cdb->byte2 & SRW10_FUA)
8660 flags |= CTL_LLF_FUA;
8661 if (cdb->byte2 & SRW10_DPO)
8662 flags |= CTL_LLF_DPO;
8663 lba = scsi_4btoul(cdb->addr);
8664 num_blocks = scsi_2btoul(cdb->length);
8665 break;
8666 }
8667 case WRITE_VERIFY_10: {
8668 struct scsi_write_verify_10 *cdb;
8669
8670 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8671 flags |= CTL_LLF_FUA;
8672 if (cdb->byte2 & SWV_DPO)
8673 flags |= CTL_LLF_DPO;
8674 lba = scsi_4btoul(cdb->addr);
8675 num_blocks = scsi_2btoul(cdb->length);
8676 break;
8677 }
8678 case READ_12:
8679 case WRITE_12: {
8680 struct scsi_rw_12 *cdb;
8681
8682 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8683 if (cdb->byte2 & SRW12_FUA)
8684 flags |= CTL_LLF_FUA;
8685 if (cdb->byte2 & SRW12_DPO)
8686 flags |= CTL_LLF_DPO;
8687 lba = scsi_4btoul(cdb->addr);
8688 num_blocks = scsi_4btoul(cdb->length);
8689 break;
8690 }
8691 case WRITE_VERIFY_12: {
8692 struct scsi_write_verify_12 *cdb;
8693
8694 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8695 flags |= CTL_LLF_FUA;
8696 if (cdb->byte2 & SWV_DPO)
8697 flags |= CTL_LLF_DPO;
8698 lba = scsi_4btoul(cdb->addr);
8699 num_blocks = scsi_4btoul(cdb->length);
8700 break;
8701 }
8702 case READ_16:
8703 case WRITE_16: {
8704 struct scsi_rw_16 *cdb;
8705
8706 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8707 if (cdb->byte2 & SRW12_FUA)
8708 flags |= CTL_LLF_FUA;
8709 if (cdb->byte2 & SRW12_DPO)
8710 flags |= CTL_LLF_DPO;
8711 lba = scsi_8btou64(cdb->addr);
8712 num_blocks = scsi_4btoul(cdb->length);
8713 break;
8714 }
8715 case WRITE_ATOMIC_16: {
8716 struct scsi_write_atomic_16 *cdb;
8717
8718 if (lun->be_lun->atomicblock == 0) {
8719 ctl_set_invalid_opcode(ctsio);
8720 ctl_done((union ctl_io *)ctsio);
8721 return (CTL_RETVAL_COMPLETE);
8722 }
8723
8724 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb;
8725 if (cdb->byte2 & SRW12_FUA)
8726 flags |= CTL_LLF_FUA;
8727 if (cdb->byte2 & SRW12_DPO)
8728 flags |= CTL_LLF_DPO;
8729 lba = scsi_8btou64(cdb->addr);
8730 num_blocks = scsi_2btoul(cdb->length);
8731 if (num_blocks > lun->be_lun->atomicblock) {
8732 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8733 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8734 /*bit*/ 0);
8735 ctl_done((union ctl_io *)ctsio);
8736 return (CTL_RETVAL_COMPLETE);
8737 }
8738 break;
8739 }
8740 case WRITE_VERIFY_16: {
8741 struct scsi_write_verify_16 *cdb;
8742
8743 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8744 flags |= CTL_LLF_FUA;
8745 if (cdb->byte2 & SWV_DPO)
8746 flags |= CTL_LLF_DPO;
8747 lba = scsi_8btou64(cdb->addr);
8748 num_blocks = scsi_4btoul(cdb->length);
8749 break;
8750 }
8751 default:
8752 /*
8753 * We got a command we don't support. This shouldn't
8754 * happen, commands should be filtered out above us.
8755 */
8756 ctl_set_invalid_opcode(ctsio);
8757 ctl_done((union ctl_io *)ctsio);
8758
8759 return (CTL_RETVAL_COMPLETE);
8760 break; /* NOTREACHED */
8761 }
8762
8763 /*
8764 * The first check is to make sure we're in bounds, the second
8765 * check is to catch wrap-around problems. If the lba + num blocks
8766 * is less than the lba, then we've wrapped around and the block
8767 * range is invalid anyway.
8768 */
8769 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8770 || ((lba + num_blocks) < lba)) {
8771 ctl_set_lba_out_of_range(ctsio,
8772 MAX(lba, lun->be_lun->maxlba + 1));
8773 ctl_done((union ctl_io *)ctsio);
8774 return (CTL_RETVAL_COMPLETE);
8775 }
8776
8777 /*
8778 * According to SBC-3, a transfer length of 0 is not an error.
8779 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8780 * translates to 256 blocks for those commands.
8781 */
8782 if (num_blocks == 0) {
8783 ctl_set_success(ctsio);
8784 ctl_done((union ctl_io *)ctsio);
8785 return (CTL_RETVAL_COMPLETE);
8786 }
8787
8788 /* Set FUA and/or DPO if caches are disabled. */
8789 if (isread) {
8790 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0)
8791 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8792 } else {
8793 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8794 flags |= CTL_LLF_FUA;
8795 }
8796
8797 lbalen = (struct ctl_lba_len_flags *)
8798 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8799 lbalen->lba = lba;
8800 lbalen->len = num_blocks;
8801 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8802
8803 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8804 ctsio->kern_rel_offset = 0;
8805
8806 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8807
8808 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8809 return (retval);
8810}
8811
8812static int
8813ctl_cnw_cont(union ctl_io *io)
8814{
8815 struct ctl_lun *lun = CTL_LUN(io);
8816 struct ctl_scsiio *ctsio;
8817 struct ctl_lba_len_flags *lbalen;
8818 int retval;
8819
8820 ctsio = &io->scsiio;
8821 ctsio->io_hdr.status = CTL_STATUS_NONE;
8822 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8823 lbalen = (struct ctl_lba_len_flags *)
8824 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8825 lbalen->flags &= ~CTL_LLF_COMPARE;
8826 lbalen->flags |= CTL_LLF_WRITE;
8827
8828 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8829 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8830 return (retval);
8831}
8832
8833int
8834ctl_cnw(struct ctl_scsiio *ctsio)
8835{
8836 struct ctl_lun *lun = CTL_LUN(ctsio);
8837 struct ctl_lba_len_flags *lbalen;
8838 uint64_t lba;
8839 uint32_t num_blocks;
8840 int flags, retval;
8841
8842 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8843
8844 flags = 0;
8845 switch (ctsio->cdb[0]) {
8846 case COMPARE_AND_WRITE: {
8847 struct scsi_compare_and_write *cdb;
8848
8849 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8850 if (cdb->byte2 & SRW10_FUA)
8851 flags |= CTL_LLF_FUA;
8852 if (cdb->byte2 & SRW10_DPO)
8853 flags |= CTL_LLF_DPO;
8854 lba = scsi_8btou64(cdb->addr);
8855 num_blocks = cdb->length;
8856 break;
8857 }
8858 default:
8859 /*
8860 * We got a command we don't support. This shouldn't
8861 * happen, commands should be filtered out above us.
8862 */
8863 ctl_set_invalid_opcode(ctsio);
8864 ctl_done((union ctl_io *)ctsio);
8865
8866 return (CTL_RETVAL_COMPLETE);
8867 break; /* NOTREACHED */
8868 }
8869
8870 /*
8871 * The first check is to make sure we're in bounds, the second
8872 * check is to catch wrap-around problems. If the lba + num blocks
8873 * is less than the lba, then we've wrapped around and the block
8874 * range is invalid anyway.
8875 */
8876 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8877 || ((lba + num_blocks) < lba)) {
8878 ctl_set_lba_out_of_range(ctsio,
8879 MAX(lba, lun->be_lun->maxlba + 1));
8880 ctl_done((union ctl_io *)ctsio);
8881 return (CTL_RETVAL_COMPLETE);
8882 }
8883
8884 /*
8885 * According to SBC-3, a transfer length of 0 is not an error.
8886 */
8887 if (num_blocks == 0) {
8888 ctl_set_success(ctsio);
8889 ctl_done((union ctl_io *)ctsio);
8890 return (CTL_RETVAL_COMPLETE);
8891 }
8892
8893 /* Set FUA if write cache is disabled. */
8894 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8895 flags |= CTL_LLF_FUA;
8896
8897 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8898 ctsio->kern_rel_offset = 0;
8899
8900 /*
8901 * Set the IO_CONT flag, so that if this I/O gets passed to
8902 * ctl_data_submit_done(), it'll get passed back to
8903 * ctl_ctl_cnw_cont() for further processing.
8904 */
8905 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8906 ctsio->io_cont = ctl_cnw_cont;
8907
8908 lbalen = (struct ctl_lba_len_flags *)
8909 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8910 lbalen->lba = lba;
8911 lbalen->len = num_blocks;
8912 lbalen->flags = CTL_LLF_COMPARE | flags;
8913
8914 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8915 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8916 return (retval);
8917}
8918
8919int
8920ctl_verify(struct ctl_scsiio *ctsio)
8921{
8922 struct ctl_lun *lun = CTL_LUN(ctsio);
8923 struct ctl_lba_len_flags *lbalen;
8924 uint64_t lba;
8925 uint32_t num_blocks;
8926 int bytchk, flags;
8927 int retval;
8928
8929 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
8930
8931 bytchk = 0;
8932 flags = CTL_LLF_FUA;
8933 switch (ctsio->cdb[0]) {
8934 case VERIFY_10: {
8935 struct scsi_verify_10 *cdb;
8936
8937 cdb = (struct scsi_verify_10 *)ctsio->cdb;
8938 if (cdb->byte2 & SVFY_BYTCHK)
8939 bytchk = 1;
8940 if (cdb->byte2 & SVFY_DPO)
8941 flags |= CTL_LLF_DPO;
8942 lba = scsi_4btoul(cdb->addr);
8943 num_blocks = scsi_2btoul(cdb->length);
8944 break;
8945 }
8946 case VERIFY_12: {
8947 struct scsi_verify_12 *cdb;
8948
8949 cdb = (struct scsi_verify_12 *)ctsio->cdb;
8950 if (cdb->byte2 & SVFY_BYTCHK)
8951 bytchk = 1;
8952 if (cdb->byte2 & SVFY_DPO)
8953 flags |= CTL_LLF_DPO;
8954 lba = scsi_4btoul(cdb->addr);
8955 num_blocks = scsi_4btoul(cdb->length);
8956 break;
8957 }
8958 case VERIFY_16: {
8959 struct scsi_rw_16 *cdb;
8960
8961 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8962 if (cdb->byte2 & SVFY_BYTCHK)
8963 bytchk = 1;
8964 if (cdb->byte2 & SVFY_DPO)
8965 flags |= CTL_LLF_DPO;
8966 lba = scsi_8btou64(cdb->addr);
8967 num_blocks = scsi_4btoul(cdb->length);
8968 break;
8969 }
8970 default:
8971 /*
8972 * We got a command we don't support. This shouldn't
8973 * happen, commands should be filtered out above us.
8974 */
8975 ctl_set_invalid_opcode(ctsio);
8976 ctl_done((union ctl_io *)ctsio);
8977 return (CTL_RETVAL_COMPLETE);
8978 }
8979
8980 /*
8981 * The first check is to make sure we're in bounds, the second
8982 * check is to catch wrap-around problems. If the lba + num blocks
8983 * is less than the lba, then we've wrapped around and the block
8984 * range is invalid anyway.
8985 */
8986 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8987 || ((lba + num_blocks) < lba)) {
8988 ctl_set_lba_out_of_range(ctsio,
8989 MAX(lba, lun->be_lun->maxlba + 1));
8990 ctl_done((union ctl_io *)ctsio);
8991 return (CTL_RETVAL_COMPLETE);
8992 }
8993
8994 /*
8995 * According to SBC-3, a transfer length of 0 is not an error.
8996 */
8997 if (num_blocks == 0) {
8998 ctl_set_success(ctsio);
8999 ctl_done((union ctl_io *)ctsio);
9000 return (CTL_RETVAL_COMPLETE);
9001 }
9002
9003 lbalen = (struct ctl_lba_len_flags *)
9004 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9005 lbalen->lba = lba;
9006 lbalen->len = num_blocks;
9007 if (bytchk) {
9008 lbalen->flags = CTL_LLF_COMPARE | flags;
9009 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9010 } else {
9011 lbalen->flags = CTL_LLF_VERIFY | flags;
9012 ctsio->kern_total_len = 0;
9013 }
9014 ctsio->kern_rel_offset = 0;
9015
9016 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9017 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9018 return (retval);
9019}
9020
9021int
9022ctl_report_luns(struct ctl_scsiio *ctsio)
9023{
9024 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9025 struct ctl_port *port = CTL_PORT(ctsio);
9026 struct ctl_lun *lun, *request_lun = CTL_LUN(ctsio);
9027 struct scsi_report_luns *cdb;
9028 struct scsi_report_luns_data *lun_data;
9029 int num_filled, num_luns, num_port_luns, retval;
9030 uint32_t alloc_len, lun_datalen;
9031 uint32_t initidx, targ_lun_id, lun_id;
9032
9033 retval = CTL_RETVAL_COMPLETE;
9034 cdb = (struct scsi_report_luns *)ctsio->cdb;
9035
9036 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9037
9038 num_luns = 0;
9039 num_port_luns = port->lun_map ? port->lun_map_size : CTL_MAX_LUNS;
9040 mtx_lock(&softc->ctl_lock);
9041 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) {
9042 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX)
9043 num_luns++;
9044 }
9045 mtx_unlock(&softc->ctl_lock);
9046
9047 switch (cdb->select_report) {
9048 case RPL_REPORT_DEFAULT:
9049 case RPL_REPORT_ALL:
9050 case RPL_REPORT_NONSUBSID:
9051 break;
9052 case RPL_REPORT_WELLKNOWN:
9053 case RPL_REPORT_ADMIN:
9054 case RPL_REPORT_CONGLOM:
9055 num_luns = 0;
9056 break;
9057 default:
9058 ctl_set_invalid_field(ctsio,
9059 /*sks_valid*/ 1,
9060 /*command*/ 1,
9061 /*field*/ 2,
9062 /*bit_valid*/ 0,
9063 /*bit*/ 0);
9064 ctl_done((union ctl_io *)ctsio);
9065 return (retval);
9066 break; /* NOTREACHED */
9067 }
9068
9069 alloc_len = scsi_4btoul(cdb->length);
9070 /*
9071 * The initiator has to allocate at least 16 bytes for this request,
9072 * so he can at least get the header and the first LUN. Otherwise
9073 * we reject the request (per SPC-3 rev 14, section 6.21).
9074 */
9075 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9076 sizeof(struct scsi_report_luns_lundata))) {
9077 ctl_set_invalid_field(ctsio,
9078 /*sks_valid*/ 1,
9079 /*command*/ 1,
9080 /*field*/ 6,
9081 /*bit_valid*/ 0,
9082 /*bit*/ 0);
9083 ctl_done((union ctl_io *)ctsio);
9084 return (retval);
9085 }
9086
9087 lun_datalen = sizeof(*lun_data) +
9088 (num_luns * sizeof(struct scsi_report_luns_lundata));
9089
9090 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9091 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9092 ctsio->kern_sg_entries = 0;
9093
9094 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9095
9096 mtx_lock(&softc->ctl_lock);
9097 for (targ_lun_id = 0, num_filled = 0;
9098 targ_lun_id < num_port_luns && num_filled < num_luns;
9099 targ_lun_id++) {
9100 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9101 if (lun_id == UINT32_MAX)
9102 continue;
9103 lun = softc->ctl_luns[lun_id];
9104 if (lun == NULL)
9105 continue;
9106
9107 be64enc(lun_data->luns[num_filled++].lundata,
9108 ctl_encode_lun(targ_lun_id));
9109
9110 /*
9111 * According to SPC-3, rev 14 section 6.21:
9112 *
9113 * "The execution of a REPORT LUNS command to any valid and
9114 * installed logical unit shall clear the REPORTED LUNS DATA
9115 * HAS CHANGED unit attention condition for all logical
9116 * units of that target with respect to the requesting
9117 * initiator. A valid and installed logical unit is one
9118 * having a PERIPHERAL QUALIFIER of 000b in the standard
9119 * INQUIRY data (see 6.4.2)."
9120 *
9121 * If request_lun is NULL, the LUN this report luns command
9122 * was issued to is either disabled or doesn't exist. In that
9123 * case, we shouldn't clear any pending lun change unit
9124 * attention.
9125 */
9126 if (request_lun != NULL) {
9127 mtx_lock(&lun->lun_lock);
9128 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9129 mtx_unlock(&lun->lun_lock);
9130 }
9131 }
9132 mtx_unlock(&softc->ctl_lock);
9133
9134 /*
9135 * It's quite possible that we've returned fewer LUNs than we allocated
9136 * space for. Trim it.
9137 */
9138 lun_datalen = sizeof(*lun_data) +
9139 (num_filled * sizeof(struct scsi_report_luns_lundata));
9140
9141 if (lun_datalen < alloc_len) {
9142 ctsio->residual = alloc_len - lun_datalen;
9143 ctsio->kern_data_len = lun_datalen;
9144 ctsio->kern_total_len = lun_datalen;
9145 } else {
9146 ctsio->residual = 0;
9147 ctsio->kern_data_len = alloc_len;
9148 ctsio->kern_total_len = alloc_len;
9149 }
9150 ctsio->kern_data_resid = 0;
9151 ctsio->kern_rel_offset = 0;
9152 ctsio->kern_sg_entries = 0;
9153
9154 /*
9155 * We set this to the actual data length, regardless of how much
9156 * space we actually have to return results. If the user looks at
9157 * this value, he'll know whether or not he allocated enough space
9158 * and reissue the command if necessary. We don't support well
9159 * known logical units, so if the user asks for that, return none.
9160 */
9161 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9162
9163 /*
9164 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9165 * this request.
9166 */
9167 ctl_set_success(ctsio);
9168 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9169 ctsio->be_move_done = ctl_config_move_done;
9170 ctl_datamove((union ctl_io *)ctsio);
9171 return (retval);
9172}
9173
9174int
9175ctl_request_sense(struct ctl_scsiio *ctsio)
9176{
9177 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9178 struct ctl_lun *lun = CTL_LUN(ctsio);
9179 struct scsi_request_sense *cdb;
9180 struct scsi_sense_data *sense_ptr;
9181 uint32_t initidx;
9182 int have_error;
9183 u_int sense_len = SSD_FULL_SIZE;
9184 scsi_sense_data_type sense_format;
9185 ctl_ua_type ua_type;
9186 uint8_t asc = 0, ascq = 0;
9187
9188 cdb = (struct scsi_request_sense *)ctsio->cdb;
9189
9190 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9191
9192 /*
9193 * Determine which sense format the user wants.
9194 */
9195 if (cdb->byte2 & SRS_DESC)
9196 sense_format = SSD_TYPE_DESC;
9197 else
9198 sense_format = SSD_TYPE_FIXED;
9199
9200 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9201 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9202 ctsio->kern_sg_entries = 0;
9203
9204 /*
9205 * struct scsi_sense_data, which is currently set to 256 bytes, is
9206 * larger than the largest allowed value for the length field in the
9207 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9208 */
9209 ctsio->residual = 0;
9210 ctsio->kern_data_len = cdb->length;
9211 ctsio->kern_total_len = cdb->length;
9212
9213 ctsio->kern_data_resid = 0;
9214 ctsio->kern_rel_offset = 0;
9215 ctsio->kern_sg_entries = 0;
9216
9217 /*
9218 * If we don't have a LUN, we don't have any pending sense.
9219 */
9220 if (lun == NULL ||
9221 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
9222 softc->ha_link < CTL_HA_LINK_UNKNOWN)) {
9223 /* "Logical unit not supported" */
9224 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format,
9225 /*current_error*/ 1,
9226 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
9227 /*asc*/ 0x25,
9228 /*ascq*/ 0x00,
9229 SSD_ELEM_NONE);
9230 goto send;
9231 }
9232
9233 have_error = 0;
9234 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9235 /*
9236 * Check for pending sense, and then for pending unit attentions.
9237 * Pending sense gets returned first, then pending unit attentions.
9238 */
9239 mtx_lock(&lun->lun_lock);
9240#ifdef CTL_WITH_CA
9241 if (ctl_is_set(lun->have_ca, initidx)) {
9242 scsi_sense_data_type stored_format;
9243
9244 /*
9245 * Check to see which sense format was used for the stored
9246 * sense data.
9247 */
9248 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9249
9250 /*
9251 * If the user requested a different sense format than the
9252 * one we stored, then we need to convert it to the other
9253 * format. If we're going from descriptor to fixed format
9254 * sense data, we may lose things in translation, depending
9255 * on what options were used.
9256 *
9257 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9258 * for some reason we'll just copy it out as-is.
9259 */
9260 if ((stored_format == SSD_TYPE_FIXED)
9261 && (sense_format == SSD_TYPE_DESC))
9262 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9263 &lun->pending_sense[initidx],
9264 (struct scsi_sense_data_desc *)sense_ptr);
9265 else if ((stored_format == SSD_TYPE_DESC)
9266 && (sense_format == SSD_TYPE_FIXED))
9267 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9268 &lun->pending_sense[initidx],
9269 (struct scsi_sense_data_fixed *)sense_ptr);
9270 else
9271 memcpy(sense_ptr, &lun->pending_sense[initidx],
9272 MIN(sizeof(*sense_ptr),
9273 sizeof(lun->pending_sense[initidx])));
9274
9275 ctl_clear_mask(lun->have_ca, initidx);
9276 have_error = 1;
9277 } else
9278#endif
9279 if (have_error == 0) {
9280 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len,
9281 sense_format);
9282 if (ua_type != CTL_UA_NONE)
9283 have_error = 1;
9284 }
9285 if (have_error == 0) {
9286 /*
9287 * Report informational exception if have one and allowed.
9288 */
9289 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) {
9290 asc = lun->ie_asc;
9291 ascq = lun->ie_ascq;
9292 }
9293 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format,
9294 /*current_error*/ 1,
9295 /*sense_key*/ SSD_KEY_NO_SENSE,
9296 /*asc*/ asc,
9297 /*ascq*/ ascq,
9298 SSD_ELEM_NONE);
9299 }
9300 mtx_unlock(&lun->lun_lock);
9301
9302send:
9303 /*
9304 * We report the SCSI status as OK, since the status of the command
9305 * itself is OK. We're reporting sense as parameter data.
9306 */
9307 ctl_set_success(ctsio);
9308 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9309 ctsio->be_move_done = ctl_config_move_done;
9310 ctl_datamove((union ctl_io *)ctsio);
9311 return (CTL_RETVAL_COMPLETE);
9312}
9313
9314int
9315ctl_tur(struct ctl_scsiio *ctsio)
9316{
9317
9318 CTL_DEBUG_PRINT(("ctl_tur\n"));
9319
9320 ctl_set_success(ctsio);
9321 ctl_done((union ctl_io *)ctsio);
9322
9323 return (CTL_RETVAL_COMPLETE);
9324}
9325
9326/*
9327 * SCSI VPD page 0x00, the Supported VPD Pages page.
9328 */
9329static int
9330ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9331{
9332 struct ctl_lun *lun = CTL_LUN(ctsio);
9333 struct scsi_vpd_supported_pages *pages;
9334 int sup_page_size;
9335 int p;
9336
9337 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9338 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9339 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9340 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9341 ctsio->kern_sg_entries = 0;
9342
9343 if (sup_page_size < alloc_len) {
9344 ctsio->residual = alloc_len - sup_page_size;
9345 ctsio->kern_data_len = sup_page_size;
9346 ctsio->kern_total_len = sup_page_size;
9347 } else {
9348 ctsio->residual = 0;
9349 ctsio->kern_data_len = alloc_len;
9350 ctsio->kern_total_len = alloc_len;
9351 }
9352 ctsio->kern_data_resid = 0;
9353 ctsio->kern_rel_offset = 0;
9354 ctsio->kern_sg_entries = 0;
9355
9356 /*
9357 * The control device is always connected. The disk device, on the
9358 * other hand, may not be online all the time. Need to change this
9359 * to figure out whether the disk device is actually online or not.
9360 */
9361 if (lun != NULL)
9362 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9363 lun->be_lun->lun_type;
9364 else
9365 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9366
9367 p = 0;
9368 /* Supported VPD pages */
9369 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9370 /* Serial Number */
9371 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9372 /* Device Identification */
9373 pages->page_list[p++] = SVPD_DEVICE_ID;
9374 /* Extended INQUIRY Data */
9375 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9376 /* Mode Page Policy */
9377 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9378 /* SCSI Ports */
9379 pages->page_list[p++] = SVPD_SCSI_PORTS;
9380 /* Third-party Copy */
9381 pages->page_list[p++] = SVPD_SCSI_TPC;
9382 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9383 /* Block limits */
9384 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9385 /* Block Device Characteristics */
9386 pages->page_list[p++] = SVPD_BDC;
9387 /* Logical Block Provisioning */
9388 pages->page_list[p++] = SVPD_LBP;
9389 }
9390 pages->length = p;
9391
9392 ctl_set_success(ctsio);
9393 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9394 ctsio->be_move_done = ctl_config_move_done;
9395 ctl_datamove((union ctl_io *)ctsio);
9396 return (CTL_RETVAL_COMPLETE);
9397}
9398
9399/*
9400 * SCSI VPD page 0x80, the Unit Serial Number page.
9401 */
9402static int
9403ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9404{
9405 struct ctl_lun *lun = CTL_LUN(ctsio);
9406 struct scsi_vpd_unit_serial_number *sn_ptr;
9407 int data_len;
9408
9409 data_len = 4 + CTL_SN_LEN;
9410 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9411 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9412 if (data_len < alloc_len) {
9413 ctsio->residual = alloc_len - data_len;
9414 ctsio->kern_data_len = data_len;
9415 ctsio->kern_total_len = data_len;
9416 } else {
9417 ctsio->residual = 0;
9418 ctsio->kern_data_len = alloc_len;
9419 ctsio->kern_total_len = alloc_len;
9420 }
9421 ctsio->kern_data_resid = 0;
9422 ctsio->kern_rel_offset = 0;
9423 ctsio->kern_sg_entries = 0;
9424
9425 /*
9426 * The control device is always connected. The disk device, on the
9427 * other hand, may not be online all the time. Need to change this
9428 * to figure out whether the disk device is actually online or not.
9429 */
9430 if (lun != NULL)
9431 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9432 lun->be_lun->lun_type;
9433 else
9434 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9435
9436 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9437 sn_ptr->length = CTL_SN_LEN;
9438 /*
9439 * If we don't have a LUN, we just leave the serial number as
9440 * all spaces.
9441 */
9442 if (lun != NULL) {
9443 strncpy((char *)sn_ptr->serial_num,
9444 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9445 } else
9446 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9447
9448 ctl_set_success(ctsio);
9449 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9450 ctsio->be_move_done = ctl_config_move_done;
9451 ctl_datamove((union ctl_io *)ctsio);
9452 return (CTL_RETVAL_COMPLETE);
9453}
9454
9455
9456/*
9457 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9458 */
9459static int
9460ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9461{
9462 struct ctl_lun *lun = CTL_LUN(ctsio);
9463 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9464 int data_len;
9465
9466 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9467 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9468 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9469 ctsio->kern_sg_entries = 0;
9470
9471 if (data_len < alloc_len) {
9472 ctsio->residual = alloc_len - data_len;
9473 ctsio->kern_data_len = data_len;
9474 ctsio->kern_total_len = data_len;
9475 } else {
9476 ctsio->residual = 0;
9477 ctsio->kern_data_len = alloc_len;
9478 ctsio->kern_total_len = alloc_len;
9479 }
9480 ctsio->kern_data_resid = 0;
9481 ctsio->kern_rel_offset = 0;
9482 ctsio->kern_sg_entries = 0;
9483
9484 /*
9485 * The control device is always connected. The disk device, on the
9486 * other hand, may not be online all the time.
9487 */
9488 if (lun != NULL)
9489 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9490 lun->be_lun->lun_type;
9491 else
9492 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9493 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9494 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9495 /*
9496 * We support head of queue, ordered and simple tags.
9497 */
9498 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9499 /*
9500 * Volatile cache supported.
9501 */
9502 eid_ptr->flags3 = SVPD_EID_V_SUP;
9503
9504 /*
9505 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9506 * attention for a particular IT nexus on all LUNs once we report
9507 * it to that nexus once. This bit is required as of SPC-4.
9508 */
9509 eid_ptr->flags4 = SVPD_EID_LUICLR;
9510
9511 /*
9512 * We support revert to defaults (RTD) bit in MODE SELECT.
9513 */
9514 eid_ptr->flags5 = SVPD_EID_RTD_SUP;
9515
9516 /*
9517 * XXX KDM in order to correctly answer this, we would need
9518 * information from the SIM to determine how much sense data it
9519 * can send. So this would really be a path inquiry field, most
9520 * likely. This can be set to a maximum of 252 according to SPC-4,
9521 * but the hardware may or may not be able to support that much.
9522 * 0 just means that the maximum sense data length is not reported.
9523 */
9524 eid_ptr->max_sense_length = 0;
9525
9526 ctl_set_success(ctsio);
9527 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9528 ctsio->be_move_done = ctl_config_move_done;
9529 ctl_datamove((union ctl_io *)ctsio);
9530 return (CTL_RETVAL_COMPLETE);
9531}
9532
9533static int
9534ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9535{
9536 struct ctl_lun *lun = CTL_LUN(ctsio);
9537 struct scsi_vpd_mode_page_policy *mpp_ptr;
9538 int data_len;
9539
9540 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9541 sizeof(struct scsi_vpd_mode_page_policy_descr);
9542
9543 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9544 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9545 ctsio->kern_sg_entries = 0;
9546
9547 if (data_len < alloc_len) {
9548 ctsio->residual = alloc_len - data_len;
9549 ctsio->kern_data_len = data_len;
9550 ctsio->kern_total_len = data_len;
9551 } else {
9552 ctsio->residual = 0;
9553 ctsio->kern_data_len = alloc_len;
9554 ctsio->kern_total_len = alloc_len;
9555 }
9556 ctsio->kern_data_resid = 0;
9557 ctsio->kern_rel_offset = 0;
9558 ctsio->kern_sg_entries = 0;
9559
9560 /*
9561 * The control device is always connected. The disk device, on the
9562 * other hand, may not be online all the time.
9563 */
9564 if (lun != NULL)
9565 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9566 lun->be_lun->lun_type;
9567 else
9568 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9569 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9570 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9571 mpp_ptr->descr[0].page_code = 0x3f;
9572 mpp_ptr->descr[0].subpage_code = 0xff;
9573 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9574
9575 ctl_set_success(ctsio);
9576 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9577 ctsio->be_move_done = ctl_config_move_done;
9578 ctl_datamove((union ctl_io *)ctsio);
9579 return (CTL_RETVAL_COMPLETE);
9580}
9581
9582/*
9583 * SCSI VPD page 0x83, the Device Identification page.
9584 */
9585static int
9586ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9587{
9588 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9589 struct ctl_port *port = CTL_PORT(ctsio);
9590 struct ctl_lun *lun = CTL_LUN(ctsio);
9591 struct scsi_vpd_device_id *devid_ptr;
9592 struct scsi_vpd_id_descriptor *desc;
9593 int data_len, g;
9594 uint8_t proto;
9595
9596 data_len = sizeof(struct scsi_vpd_device_id) +
9597 sizeof(struct scsi_vpd_id_descriptor) +
9598 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9599 sizeof(struct scsi_vpd_id_descriptor) +
9600 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9601 if (lun && lun->lun_devid)
9602 data_len += lun->lun_devid->len;
9603 if (port && port->port_devid)
9604 data_len += port->port_devid->len;
9605 if (port && port->target_devid)
9606 data_len += port->target_devid->len;
9607
9608 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9609 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9610 ctsio->kern_sg_entries = 0;
9611
9612 if (data_len < alloc_len) {
9613 ctsio->residual = alloc_len - data_len;
9614 ctsio->kern_data_len = data_len;
9615 ctsio->kern_total_len = data_len;
9616 } else {
9617 ctsio->residual = 0;
9618 ctsio->kern_data_len = alloc_len;
9619 ctsio->kern_total_len = alloc_len;
9620 }
9621 ctsio->kern_data_resid = 0;
9622 ctsio->kern_rel_offset = 0;
9623 ctsio->kern_sg_entries = 0;
9624
9625 /*
9626 * The control device is always connected. The disk device, on the
9627 * other hand, may not be online all the time.
9628 */
9629 if (lun != NULL)
9630 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9631 lun->be_lun->lun_type;
9632 else
9633 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9634 devid_ptr->page_code = SVPD_DEVICE_ID;
9635 scsi_ulto2b(data_len - 4, devid_ptr->length);
9636
9637 if (port && port->port_type == CTL_PORT_FC)
9638 proto = SCSI_PROTO_FC << 4;
9639 else if (port && port->port_type == CTL_PORT_ISCSI)
9640 proto = SCSI_PROTO_ISCSI << 4;
9641 else
9642 proto = SCSI_PROTO_SPI << 4;
9643 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9644
9645 /*
9646 * We're using a LUN association here. i.e., this device ID is a
9647 * per-LUN identifier.
9648 */
9649 if (lun && lun->lun_devid) {
9650 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9651 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9652 lun->lun_devid->len);
9653 }
9654
9655 /*
9656 * This is for the WWPN which is a port association.
9657 */
9658 if (port && port->port_devid) {
9659 memcpy(desc, port->port_devid->data, port->port_devid->len);
9660 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9661 port->port_devid->len);
9662 }
9663
9664 /*
9665 * This is for the Relative Target Port(type 4h) identifier
9666 */
9667 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9668 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9669 SVPD_ID_TYPE_RELTARG;
9670 desc->length = 4;
9671 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9672 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9673 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9674
9675 /*
9676 * This is for the Target Port Group(type 5h) identifier
9677 */
9678 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9679 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9680 SVPD_ID_TYPE_TPORTGRP;
9681 desc->length = 4;
9682 if (softc->is_single ||
9683 (port && port->status & CTL_PORT_STATUS_HA_SHARED))
9684 g = 1;
9685 else
9686 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt;
9687 scsi_ulto2b(g, &desc->identifier[2]);
9688 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9689 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9690
9691 /*
9692 * This is for the Target identifier
9693 */
9694 if (port && port->target_devid) {
9695 memcpy(desc, port->target_devid->data, port->target_devid->len);
9696 }
9697
9698 ctl_set_success(ctsio);
9699 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9700 ctsio->be_move_done = ctl_config_move_done;
9701 ctl_datamove((union ctl_io *)ctsio);
9702 return (CTL_RETVAL_COMPLETE);
9703}
9704
9705static int
9706ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9707{
9708 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9709 struct ctl_lun *lun = CTL_LUN(ctsio);
9710 struct scsi_vpd_scsi_ports *sp;
9711 struct scsi_vpd_port_designation *pd;
9712 struct scsi_vpd_port_designation_cont *pdc;
9713 struct ctl_port *port;
9714 int data_len, num_target_ports, iid_len, id_len;
9715
9716 num_target_ports = 0;
9717 iid_len = 0;
9718 id_len = 0;
9719 mtx_lock(&softc->ctl_lock);
9720 STAILQ_FOREACH(port, &softc->port_list, links) {
9721 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9722 continue;
9723 if (lun != NULL &&
9724 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9725 continue;
9726 num_target_ports++;
9727 if (port->init_devid)
9728 iid_len += port->init_devid->len;
9729 if (port->port_devid)
9730 id_len += port->port_devid->len;
9731 }
9732 mtx_unlock(&softc->ctl_lock);
9733
9734 data_len = sizeof(struct scsi_vpd_scsi_ports) +
9735 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9736 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9737 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9738 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9739 ctsio->kern_sg_entries = 0;
9740
9741 if (data_len < alloc_len) {
9742 ctsio->residual = alloc_len - data_len;
9743 ctsio->kern_data_len = data_len;
9744 ctsio->kern_total_len = data_len;
9745 } else {
9746 ctsio->residual = 0;
9747 ctsio->kern_data_len = alloc_len;
9748 ctsio->kern_total_len = alloc_len;
9749 }
9750 ctsio->kern_data_resid = 0;
9751 ctsio->kern_rel_offset = 0;
9752 ctsio->kern_sg_entries = 0;
9753
9754 /*
9755 * The control device is always connected. The disk device, on the
9756 * other hand, may not be online all the time. Need to change this
9757 * to figure out whether the disk device is actually online or not.
9758 */
9759 if (lun != NULL)
9760 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9761 lun->be_lun->lun_type;
9762 else
9763 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9764
9765 sp->page_code = SVPD_SCSI_PORTS;
9766 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9767 sp->page_length);
9768 pd = &sp->design[0];
9769
9770 mtx_lock(&softc->ctl_lock);
9771 STAILQ_FOREACH(port, &softc->port_list, links) {
9772 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9773 continue;
9774 if (lun != NULL &&
9775 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9776 continue;
9777 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9778 if (port->init_devid) {
9779 iid_len = port->init_devid->len;
9780 memcpy(pd->initiator_transportid,
9781 port->init_devid->data, port->init_devid->len);
9782 } else
9783 iid_len = 0;
9784 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9785 pdc = (struct scsi_vpd_port_designation_cont *)
9786 (&pd->initiator_transportid[iid_len]);
9787 if (port->port_devid) {
9788 id_len = port->port_devid->len;
9789 memcpy(pdc->target_port_descriptors,
9790 port->port_devid->data, port->port_devid->len);
9791 } else
9792 id_len = 0;
9793 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9794 pd = (struct scsi_vpd_port_designation *)
9795 ((uint8_t *)pdc->target_port_descriptors + id_len);
9796 }
9797 mtx_unlock(&softc->ctl_lock);
9798
9799 ctl_set_success(ctsio);
9800 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9801 ctsio->be_move_done = ctl_config_move_done;
9802 ctl_datamove((union ctl_io *)ctsio);
9803 return (CTL_RETVAL_COMPLETE);
9804}
9805
9806static int
9807ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9808{
9809 struct ctl_lun *lun = CTL_LUN(ctsio);
9810 struct scsi_vpd_block_limits *bl_ptr;
9811 uint64_t ival;
9812
9813 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9814 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9815 ctsio->kern_sg_entries = 0;
9816
9817 if (sizeof(*bl_ptr) < alloc_len) {
9818 ctsio->residual = alloc_len - sizeof(*bl_ptr);
9819 ctsio->kern_data_len = sizeof(*bl_ptr);
9820 ctsio->kern_total_len = sizeof(*bl_ptr);
9821 } else {
9822 ctsio->residual = 0;
9823 ctsio->kern_data_len = alloc_len;
9824 ctsio->kern_total_len = alloc_len;
9825 }
9826 ctsio->kern_data_resid = 0;
9827 ctsio->kern_rel_offset = 0;
9828 ctsio->kern_sg_entries = 0;
9829
9830 /*
9831 * The control device is always connected. The disk device, on the
9832 * other hand, may not be online all the time. Need to change this
9833 * to figure out whether the disk device is actually online or not.
9834 */
9835 if (lun != NULL)
9836 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9837 lun->be_lun->lun_type;
9838 else
9839 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9840
9841 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9842 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9843 bl_ptr->max_cmp_write_len = 0xff;
9844 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9845 if (lun != NULL) {
9846 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9847 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9848 ival = 0xffffffff;
9849 ctl_get_opt_number(&lun->be_lun->options,
9850 "unmap_max_lba", &ival);
9851 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt);
9852 ival = 0xffffffff;
9853 ctl_get_opt_number(&lun->be_lun->options,
9854 "unmap_max_descr", &ival);
9855 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt);
9856 if (lun->be_lun->ublockexp != 0) {
9857 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9858 bl_ptr->opt_unmap_grain);
9859 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9860 bl_ptr->unmap_grain_align);
9861 }
9862 }
9863 scsi_ulto4b(lun->be_lun->atomicblock,
9864 bl_ptr->max_atomic_transfer_length);
9865 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9866 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9867 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary);
9868 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size);
9869 ival = UINT64_MAX;
9870 ctl_get_opt_number(&lun->be_lun->options, "write_same_max_lba", &ival);
9871 scsi_u64to8b(ival, bl_ptr->max_write_same_length);
9872 }
9873
9874 ctl_set_success(ctsio);
9875 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9876 ctsio->be_move_done = ctl_config_move_done;
9877 ctl_datamove((union ctl_io *)ctsio);
9878 return (CTL_RETVAL_COMPLETE);
9879}
9880
9881static int
9882ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
9883{
9884 struct ctl_lun *lun = CTL_LUN(ctsio);
9885 struct scsi_vpd_block_device_characteristics *bdc_ptr;
9886 const char *value;
9887 u_int i;
9888
9889 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
9890 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
9891 ctsio->kern_sg_entries = 0;
9892
9893 if (sizeof(*bdc_ptr) < alloc_len) {
9894 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
9895 ctsio->kern_data_len = sizeof(*bdc_ptr);
9896 ctsio->kern_total_len = sizeof(*bdc_ptr);
9897 } else {
9898 ctsio->residual = 0;
9899 ctsio->kern_data_len = alloc_len;
9900 ctsio->kern_total_len = alloc_len;
9901 }
9902 ctsio->kern_data_resid = 0;
9903 ctsio->kern_rel_offset = 0;
9904 ctsio->kern_sg_entries = 0;
9905
9906 /*
9907 * The control device is always connected. The disk device, on the
9908 * other hand, may not be online all the time. Need to change this
9909 * to figure out whether the disk device is actually online or not.
9910 */
9911 if (lun != NULL)
9912 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9913 lun->be_lun->lun_type;
9914 else
9915 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9916 bdc_ptr->page_code = SVPD_BDC;
9917 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
9918 if (lun != NULL &&
9919 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
9920 i = strtol(value, NULL, 0);
9921 else
9922 i = CTL_DEFAULT_ROTATION_RATE;
9923 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
9924 if (lun != NULL &&
9925 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
9926 i = strtol(value, NULL, 0);
9927 else
9928 i = 0;
9929 bdc_ptr->wab_wac_ff = (i & 0x0f);
9930 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
9931
9932 ctl_set_success(ctsio);
9933 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9934 ctsio->be_move_done = ctl_config_move_done;
9935 ctl_datamove((union ctl_io *)ctsio);
9936 return (CTL_RETVAL_COMPLETE);
9937}
9938
9939static int
9940ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9941{
9942 struct ctl_lun *lun = CTL_LUN(ctsio);
9943 struct scsi_vpd_logical_block_prov *lbp_ptr;
9944 const char *value;
9945
9946 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
9947 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
9948 ctsio->kern_sg_entries = 0;
9949
9950 if (sizeof(*lbp_ptr) < alloc_len) {
9951 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
9952 ctsio->kern_data_len = sizeof(*lbp_ptr);
9953 ctsio->kern_total_len = sizeof(*lbp_ptr);
9954 } else {
9955 ctsio->residual = 0;
9956 ctsio->kern_data_len = alloc_len;
9957 ctsio->kern_total_len = alloc_len;
9958 }
9959 ctsio->kern_data_resid = 0;
9960 ctsio->kern_rel_offset = 0;
9961 ctsio->kern_sg_entries = 0;
9962
9963 /*
9964 * The control device is always connected. The disk device, on the
9965 * other hand, may not be online all the time. Need to change this
9966 * to figure out whether the disk device is actually online or not.
9967 */
9968 if (lun != NULL)
9969 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9970 lun->be_lun->lun_type;
9971 else
9972 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9973
9974 lbp_ptr->page_code = SVPD_LBP;
9975 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
9976 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
9977 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9978 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
9979 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
9980 value = ctl_get_opt(&lun->be_lun->options, "provisioning_type");
9981 if (value != NULL) {
9982 if (strcmp(value, "resource") == 0)
9983 lbp_ptr->prov_type = SVPD_LBP_RESOURCE;
9984 else if (strcmp(value, "thin") == 0)
9985 lbp_ptr->prov_type = SVPD_LBP_THIN;
9986 } else
9987 lbp_ptr->prov_type = SVPD_LBP_THIN;
9988 }
9989
9990 ctl_set_success(ctsio);
9991 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9992 ctsio->be_move_done = ctl_config_move_done;
9993 ctl_datamove((union ctl_io *)ctsio);
9994 return (CTL_RETVAL_COMPLETE);
9995}
9996
9997/*
9998 * INQUIRY with the EVPD bit set.
9999 */
10000static int
10001ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10002{
10003 struct ctl_lun *lun = CTL_LUN(ctsio);
10004 struct scsi_inquiry *cdb;
10005 int alloc_len, retval;
10006
10007 cdb = (struct scsi_inquiry *)ctsio->cdb;
10008 alloc_len = scsi_2btoul(cdb->length);
10009
10010 switch (cdb->page_code) {
10011 case SVPD_SUPPORTED_PAGES:
10012 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10013 break;
10014 case SVPD_UNIT_SERIAL_NUMBER:
10015 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10016 break;
10017 case SVPD_DEVICE_ID:
10018 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10019 break;
10020 case SVPD_EXTENDED_INQUIRY_DATA:
10021 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10022 break;
10023 case SVPD_MODE_PAGE_POLICY:
10024 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10025 break;
10026 case SVPD_SCSI_PORTS:
10027 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10028 break;
10029 case SVPD_SCSI_TPC:
10030 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10031 break;
10032 case SVPD_BLOCK_LIMITS:
10033 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10034 goto err;
10035 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10036 break;
10037 case SVPD_BDC:
10038 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10039 goto err;
10040 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10041 break;
10042 case SVPD_LBP:
10043 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10044 goto err;
10045 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10046 break;
10047 default:
10048err:
10049 ctl_set_invalid_field(ctsio,
10050 /*sks_valid*/ 1,
10051 /*command*/ 1,
10052 /*field*/ 2,
10053 /*bit_valid*/ 0,
10054 /*bit*/ 0);
10055 ctl_done((union ctl_io *)ctsio);
10056 retval = CTL_RETVAL_COMPLETE;
10057 break;
10058 }
10059
10060 return (retval);
10061}
10062
10063/*
10064 * Standard INQUIRY data.
10065 */
10066static int
10067ctl_inquiry_std(struct ctl_scsiio *ctsio)
10068{
10069 struct ctl_softc *softc = CTL_SOFTC(ctsio);
10070 struct ctl_port *port = CTL_PORT(ctsio);
10071 struct ctl_lun *lun = CTL_LUN(ctsio);
10072 struct scsi_inquiry_data *inq_ptr;
10073 struct scsi_inquiry *cdb;
10074 char *val;
10075 uint32_t alloc_len, data_len;
10076 ctl_port_type port_type;
10077
10078 port_type = port->port_type;
10079 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10080 port_type = CTL_PORT_SCSI;
10081
10082 cdb = (struct scsi_inquiry *)ctsio->cdb;
10083 alloc_len = scsi_2btoul(cdb->length);
10084
10085 /*
10086 * We malloc the full inquiry data size here and fill it
10087 * in. If the user only asks for less, we'll give him
10088 * that much.
10089 */
10090 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10091 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10092 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10093 ctsio->kern_sg_entries = 0;
10094 ctsio->kern_data_resid = 0;
10095 ctsio->kern_rel_offset = 0;
10096
10097 if (data_len < alloc_len) {
10098 ctsio->residual = alloc_len - data_len;
10099 ctsio->kern_data_len = data_len;
10100 ctsio->kern_total_len = data_len;
10101 } else {
10102 ctsio->residual = 0;
10103 ctsio->kern_data_len = alloc_len;
10104 ctsio->kern_total_len = alloc_len;
10105 }
10106
10107 if (lun != NULL) {
10108 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
10109 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
10110 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10111 lun->be_lun->lun_type;
10112 } else {
10113 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
10114 lun->be_lun->lun_type;
10115 }
10116 if (lun->flags & CTL_LUN_REMOVABLE)
10117 inq_ptr->dev_qual2 |= SID_RMB;
10118 } else
10119 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10120
10121 /* RMB in byte 2 is 0 */
10122 inq_ptr->version = SCSI_REV_SPC5;
10123
10124 /*
10125 * According to SAM-3, even if a device only supports a single
10126 * level of LUN addressing, it should still set the HISUP bit:
10127 *
10128 * 4.9.1 Logical unit numbers overview
10129 *
10130 * All logical unit number formats described in this standard are
10131 * hierarchical in structure even when only a single level in that
10132 * hierarchy is used. The HISUP bit shall be set to one in the
10133 * standard INQUIRY data (see SPC-2) when any logical unit number
10134 * format described in this standard is used. Non-hierarchical
10135 * formats are outside the scope of this standard.
10136 *
10137 * Therefore we set the HiSup bit here.
10138 *
10139 * The response format is 2, per SPC-3.
10140 */
10141 inq_ptr->response_format = SID_HiSup | 2;
10142
10143 inq_ptr->additional_length = data_len -
10144 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10145 CTL_DEBUG_PRINT(("additional_length = %d\n",
10146 inq_ptr->additional_length));
10147
10148 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10149 if (port_type == CTL_PORT_SCSI)
10150 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10151 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10152 inq_ptr->flags = SID_CmdQue;
10153 if (port_type == CTL_PORT_SCSI)
10154 inq_ptr->flags |= SID_WBus16 | SID_Sync;
10155
10156 /*
10157 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10158 * We have 8 bytes for the vendor name, and 16 bytes for the device
10159 * name and 4 bytes for the revision.
10160 */
10161 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10162 "vendor")) == NULL) {
10163 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10164 } else {
10165 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10166 strncpy(inq_ptr->vendor, val,
10167 min(sizeof(inq_ptr->vendor), strlen(val)));
10168 }
10169 if (lun == NULL) {
10170 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10171 sizeof(inq_ptr->product));
10172 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10173 switch (lun->be_lun->lun_type) {
10174 case T_DIRECT:
10175 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10176 sizeof(inq_ptr->product));
10177 break;
10178 case T_PROCESSOR:
10179 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10180 sizeof(inq_ptr->product));
10181 break;
10182 case T_CDROM:
10183 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT,
10184 sizeof(inq_ptr->product));
10185 break;
10186 default:
10187 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10188 sizeof(inq_ptr->product));
10189 break;
10190 }
10191 } else {
10192 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10193 strncpy(inq_ptr->product, val,
10194 min(sizeof(inq_ptr->product), strlen(val)));
10195 }
10196
10197 /*
10198 * XXX make this a macro somewhere so it automatically gets
10199 * incremented when we make changes.
10200 */
10201 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10202 "revision")) == NULL) {
10203 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10204 } else {
10205 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10206 strncpy(inq_ptr->revision, val,
10207 min(sizeof(inq_ptr->revision), strlen(val)));
10208 }
10209
10210 /*
10211 * For parallel SCSI, we support double transition and single
10212 * transition clocking. We also support QAS (Quick Arbitration
10213 * and Selection) and Information Unit transfers on both the
10214 * control and array devices.
10215 */
10216 if (port_type == CTL_PORT_SCSI)
10217 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10218 SID_SPI_IUS;
10219
10220 /* SAM-6 (no version claimed) */
10221 scsi_ulto2b(0x00C0, inq_ptr->version1);
10222 /* SPC-5 (no version claimed) */
10223 scsi_ulto2b(0x05C0, inq_ptr->version2);
10224 if (port_type == CTL_PORT_FC) {
10225 /* FCP-2 ANSI INCITS.350:2003 */
10226 scsi_ulto2b(0x0917, inq_ptr->version3);
10227 } else if (port_type == CTL_PORT_SCSI) {
10228 /* SPI-4 ANSI INCITS.362:200x */
10229 scsi_ulto2b(0x0B56, inq_ptr->version3);
10230 } else if (port_type == CTL_PORT_ISCSI) {
10231 /* iSCSI (no version claimed) */
10232 scsi_ulto2b(0x0960, inq_ptr->version3);
10233 } else if (port_type == CTL_PORT_SAS) {
10234 /* SAS (no version claimed) */
10235 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10236 }
10237
10238 if (lun == NULL) {
10239 /* SBC-4 (no version claimed) */
10240 scsi_ulto2b(0x0600, inq_ptr->version4);
10241 } else {
10242 switch (lun->be_lun->lun_type) {
10243 case T_DIRECT:
10244 /* SBC-4 (no version claimed) */
10245 scsi_ulto2b(0x0600, inq_ptr->version4);
10246 break;
10247 case T_PROCESSOR:
10248 break;
10249 case T_CDROM:
10250 /* MMC-6 (no version claimed) */
10251 scsi_ulto2b(0x04E0, inq_ptr->version4);
10252 break;
10253 default:
10254 break;
10255 }
10256 }
10257
10258 ctl_set_success(ctsio);
10259 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10260 ctsio->be_move_done = ctl_config_move_done;
10261 ctl_datamove((union ctl_io *)ctsio);
10262 return (CTL_RETVAL_COMPLETE);
10263}
10264
10265int
10266ctl_inquiry(struct ctl_scsiio *ctsio)
10267{
10268 struct scsi_inquiry *cdb;
10269 int retval;
10270
10271 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10272
10273 cdb = (struct scsi_inquiry *)ctsio->cdb;
10274 if (cdb->byte2 & SI_EVPD)
10275 retval = ctl_inquiry_evpd(ctsio);
10276 else if (cdb->page_code == 0)
10277 retval = ctl_inquiry_std(ctsio);
10278 else {
10279 ctl_set_invalid_field(ctsio,
10280 /*sks_valid*/ 1,
10281 /*command*/ 1,
10282 /*field*/ 2,
10283 /*bit_valid*/ 0,
10284 /*bit*/ 0);
10285 ctl_done((union ctl_io *)ctsio);
10286 return (CTL_RETVAL_COMPLETE);
10287 }
10288
10289 return (retval);
10290}
10291
10292int
10293ctl_get_config(struct ctl_scsiio *ctsio)
10294{
10295 struct ctl_lun *lun = CTL_LUN(ctsio);
10296 struct scsi_get_config_header *hdr;
10297 struct scsi_get_config_feature *feature;
10298 struct scsi_get_config *cdb;
10299 uint32_t alloc_len, data_len;
10300 int rt, starting;
10301
10302 cdb = (struct scsi_get_config *)ctsio->cdb;
10303 rt = (cdb->rt & SGC_RT_MASK);
10304 starting = scsi_2btoul(cdb->starting_feature);
10305 alloc_len = scsi_2btoul(cdb->length);
10306
10307 data_len = sizeof(struct scsi_get_config_header) +
10308 sizeof(struct scsi_get_config_feature) + 8 +
10309 sizeof(struct scsi_get_config_feature) + 8 +
10310 sizeof(struct scsi_get_config_feature) + 4 +
10311 sizeof(struct scsi_get_config_feature) + 4 +
10312 sizeof(struct scsi_get_config_feature) + 8 +
10313 sizeof(struct scsi_get_config_feature) +
10314 sizeof(struct scsi_get_config_feature) + 4 +
10315 sizeof(struct scsi_get_config_feature) + 4 +
10316 sizeof(struct scsi_get_config_feature) + 4 +
10317 sizeof(struct scsi_get_config_feature) + 4 +
10318 sizeof(struct scsi_get_config_feature) + 4 +
10319 sizeof(struct scsi_get_config_feature) + 4;
10320 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10321 ctsio->kern_sg_entries = 0;
10322 ctsio->kern_data_resid = 0;
10323 ctsio->kern_rel_offset = 0;
10324
10325 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr;
10326 if (lun->flags & CTL_LUN_NO_MEDIA)
10327 scsi_ulto2b(0x0000, hdr->current_profile);
10328 else
10329 scsi_ulto2b(0x0010, hdr->current_profile);
10330 feature = (struct scsi_get_config_feature *)(hdr + 1);
10331
10332 if (starting > 0x003b)
10333 goto done;
10334 if (starting > 0x003a)
10335 goto f3b;
10336 if (starting > 0x002b)
10337 goto f3a;
10338 if (starting > 0x002a)
10339 goto f2b;
10340 if (starting > 0x001f)
10341 goto f2a;
10342 if (starting > 0x001e)
10343 goto f1f;
10344 if (starting > 0x001d)
10345 goto f1e;
10346 if (starting > 0x0010)
10347 goto f1d;
10348 if (starting > 0x0003)
10349 goto f10;
10350 if (starting > 0x0002)
10351 goto f3;
10352 if (starting > 0x0001)
10353 goto f2;
10354 if (starting > 0x0000)
10355 goto f1;
10356
10357 /* Profile List */
10358 scsi_ulto2b(0x0000, feature->feature_code);
10359 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT;
10360 feature->add_length = 8;
10361 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */
10362 feature->feature_data[2] = 0x00;
10363 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */
10364 feature->feature_data[6] = 0x01;
10365 feature = (struct scsi_get_config_feature *)
10366 &feature->feature_data[feature->add_length];
10367
10368f1: /* Core */
10369 scsi_ulto2b(0x0001, feature->feature_code);
10370 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10371 feature->add_length = 8;
10372 scsi_ulto4b(0x00000000, &feature->feature_data[0]);
10373 feature->feature_data[4] = 0x03;
10374 feature = (struct scsi_get_config_feature *)
10375 &feature->feature_data[feature->add_length];
10376
10377f2: /* Morphing */
10378 scsi_ulto2b(0x0002, feature->feature_code);
10379 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10380 feature->add_length = 4;
10381 feature->feature_data[0] = 0x02;
10382 feature = (struct scsi_get_config_feature *)
10383 &feature->feature_data[feature->add_length];
10384
10385f3: /* Removable Medium */
10386 scsi_ulto2b(0x0003, feature->feature_code);
10387 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10388 feature->add_length = 4;
10389 feature->feature_data[0] = 0x39;
10390 feature = (struct scsi_get_config_feature *)
10391 &feature->feature_data[feature->add_length];
10392
10393 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA))
10394 goto done;
10395
10396f10: /* Random Read */
10397 scsi_ulto2b(0x0010, feature->feature_code);
10398 feature->flags = 0x00;
10399 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10400 feature->flags |= SGC_F_CURRENT;
10401 feature->add_length = 8;
10402 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]);
10403 scsi_ulto2b(1, &feature->feature_data[4]);
10404 feature->feature_data[6] = 0x00;
10405 feature = (struct scsi_get_config_feature *)
10406 &feature->feature_data[feature->add_length];
10407
10408f1d: /* Multi-Read */
10409 scsi_ulto2b(0x001D, feature->feature_code);
10410 feature->flags = 0x00;
10411 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10412 feature->flags |= SGC_F_CURRENT;
10413 feature->add_length = 0;
10414 feature = (struct scsi_get_config_feature *)
10415 &feature->feature_data[feature->add_length];
10416
10417f1e: /* CD Read */
10418 scsi_ulto2b(0x001E, feature->feature_code);
10419 feature->flags = 0x00;
10420 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10421 feature->flags |= SGC_F_CURRENT;
10422 feature->add_length = 4;
10423 feature->feature_data[0] = 0x00;
10424 feature = (struct scsi_get_config_feature *)
10425 &feature->feature_data[feature->add_length];
10426
10427f1f: /* DVD Read */
10428 scsi_ulto2b(0x001F, feature->feature_code);
10429 feature->flags = 0x08;
10430 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10431 feature->flags |= SGC_F_CURRENT;
10432 feature->add_length = 4;
10433 feature->feature_data[0] = 0x01;
10434 feature->feature_data[2] = 0x03;
10435 feature = (struct scsi_get_config_feature *)
10436 &feature->feature_data[feature->add_length];
10437
10438f2a: /* DVD+RW */
10439 scsi_ulto2b(0x002A, feature->feature_code);
10440 feature->flags = 0x04;
10441 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10442 feature->flags |= SGC_F_CURRENT;
10443 feature->add_length = 4;
10444 feature->feature_data[0] = 0x00;
10445 feature->feature_data[1] = 0x00;
10446 feature = (struct scsi_get_config_feature *)
10447 &feature->feature_data[feature->add_length];
10448
10449f2b: /* DVD+R */
10450 scsi_ulto2b(0x002B, feature->feature_code);
10451 feature->flags = 0x00;
10452 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10453 feature->flags |= SGC_F_CURRENT;
10454 feature->add_length = 4;
10455 feature->feature_data[0] = 0x00;
10456 feature = (struct scsi_get_config_feature *)
10457 &feature->feature_data[feature->add_length];
10458
10459f3a: /* DVD+RW Dual Layer */
10460 scsi_ulto2b(0x003A, feature->feature_code);
10461 feature->flags = 0x00;
10462 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10463 feature->flags |= SGC_F_CURRENT;
10464 feature->add_length = 4;
10465 feature->feature_data[0] = 0x00;
10466 feature->feature_data[1] = 0x00;
10467 feature = (struct scsi_get_config_feature *)
10468 &feature->feature_data[feature->add_length];
10469
10470f3b: /* DVD+R Dual Layer */
10471 scsi_ulto2b(0x003B, feature->feature_code);
10472 feature->flags = 0x00;
10473 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10474 feature->flags |= SGC_F_CURRENT;
10475 feature->add_length = 4;
10476 feature->feature_data[0] = 0x00;
10477 feature = (struct scsi_get_config_feature *)
10478 &feature->feature_data[feature->add_length];
10479
10480done:
10481 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10482 if (rt == SGC_RT_SPECIFIC && data_len > 4) {
10483 feature = (struct scsi_get_config_feature *)(hdr + 1);
10484 if (scsi_2btoul(feature->feature_code) == starting)
10485 feature = (struct scsi_get_config_feature *)
10486 &feature->feature_data[feature->add_length];
10487 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10488 }
10489 scsi_ulto4b(data_len - 4, hdr->data_length);
10490 if (data_len < alloc_len) {
10491 ctsio->residual = alloc_len - data_len;
10492 ctsio->kern_data_len = data_len;
10493 ctsio->kern_total_len = data_len;
10494 } else {
10495 ctsio->residual = 0;
10496 ctsio->kern_data_len = alloc_len;
10497 ctsio->kern_total_len = alloc_len;
10498 }
10499
10500 ctl_set_success(ctsio);
10501 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10502 ctsio->be_move_done = ctl_config_move_done;
10503 ctl_datamove((union ctl_io *)ctsio);
10504 return (CTL_RETVAL_COMPLETE);
10505}
10506
10507int
10508ctl_get_event_status(struct ctl_scsiio *ctsio)
10509{
10510 struct scsi_get_event_status_header *hdr;
10511 struct scsi_get_event_status *cdb;
10512 uint32_t alloc_len, data_len;
10513 int notif_class;
10514
10515 cdb = (struct scsi_get_event_status *)ctsio->cdb;
10516 if ((cdb->byte2 & SGESN_POLLED) == 0) {
10517 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
10518 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
10519 ctl_done((union ctl_io *)ctsio);
10520 return (CTL_RETVAL_COMPLETE);
10521 }
10522 notif_class = cdb->notif_class;
10523 alloc_len = scsi_2btoul(cdb->length);
10524
10525 data_len = sizeof(struct scsi_get_event_status_header);
10526 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10527 ctsio->kern_sg_entries = 0;
10528 ctsio->kern_data_resid = 0;
10529 ctsio->kern_rel_offset = 0;
10530
10531 if (data_len < alloc_len) {
10532 ctsio->residual = alloc_len - data_len;
10533 ctsio->kern_data_len = data_len;
10534 ctsio->kern_total_len = data_len;
10535 } else {
10536 ctsio->residual = 0;
10537 ctsio->kern_data_len = alloc_len;
10538 ctsio->kern_total_len = alloc_len;
10539 }
10540
10541 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr;
10542 scsi_ulto2b(0, hdr->descr_length);
10543 hdr->nea_class = SGESN_NEA;
10544 hdr->supported_class = 0;
10545
10546 ctl_set_success(ctsio);
10547 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10548 ctsio->be_move_done = ctl_config_move_done;
10549 ctl_datamove((union ctl_io *)ctsio);
10550 return (CTL_RETVAL_COMPLETE);
10551}
10552
10553int
10554ctl_mechanism_status(struct ctl_scsiio *ctsio)
10555{
10556 struct scsi_mechanism_status_header *hdr;
10557 struct scsi_mechanism_status *cdb;
10558 uint32_t alloc_len, data_len;
10559
10560 cdb = (struct scsi_mechanism_status *)ctsio->cdb;
10561 alloc_len = scsi_2btoul(cdb->length);
10562
10563 data_len = sizeof(struct scsi_mechanism_status_header);
10564 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10565 ctsio->kern_sg_entries = 0;
10566 ctsio->kern_data_resid = 0;
10567 ctsio->kern_rel_offset = 0;
10568
10569 if (data_len < alloc_len) {
10570 ctsio->residual = alloc_len - data_len;
10571 ctsio->kern_data_len = data_len;
10572 ctsio->kern_total_len = data_len;
10573 } else {
10574 ctsio->residual = 0;
10575 ctsio->kern_data_len = alloc_len;
10576 ctsio->kern_total_len = alloc_len;
10577 }
10578
10579 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr;
10580 hdr->state1 = 0x00;
10581 hdr->state2 = 0xe0;
10582 scsi_ulto3b(0, hdr->lba);
10583 hdr->slots_num = 0;
10584 scsi_ulto2b(0, hdr->slots_length);
10585
10586 ctl_set_success(ctsio);
10587 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10588 ctsio->be_move_done = ctl_config_move_done;
10589 ctl_datamove((union ctl_io *)ctsio);
10590 return (CTL_RETVAL_COMPLETE);
10591}
10592
10593static void
10594ctl_ultomsf(uint32_t lba, uint8_t *buf)
10595{
10596
10597 lba += 150;
10598 buf[0] = 0;
10599 buf[1] = bin2bcd((lba / 75) / 60);
10600 buf[2] = bin2bcd((lba / 75) % 60);
10601 buf[3] = bin2bcd(lba % 75);
10602}
10603
10604int
10605ctl_read_toc(struct ctl_scsiio *ctsio)
10606{
10607 struct ctl_lun *lun = CTL_LUN(ctsio);
10608 struct scsi_read_toc_hdr *hdr;
10609 struct scsi_read_toc_type01_descr *descr;
10610 struct scsi_read_toc *cdb;
10611 uint32_t alloc_len, data_len;
10612 int format, msf;
10613
10614 cdb = (struct scsi_read_toc *)ctsio->cdb;
10615 msf = (cdb->byte2 & CD_MSF) != 0;
10616 format = cdb->format;
10617 alloc_len = scsi_2btoul(cdb->data_len);
10618
10619 data_len = sizeof(struct scsi_read_toc_hdr);
10620 if (format == 0)
10621 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr);
10622 else
10623 data_len += sizeof(struct scsi_read_toc_type01_descr);
10624 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10625 ctsio->kern_sg_entries = 0;
10626 ctsio->kern_data_resid = 0;
10627 ctsio->kern_rel_offset = 0;
10628
10629 if (data_len < alloc_len) {
10630 ctsio->residual = alloc_len - data_len;
10631 ctsio->kern_data_len = data_len;
10632 ctsio->kern_total_len = data_len;
10633 } else {
10634 ctsio->residual = 0;
10635 ctsio->kern_data_len = alloc_len;
10636 ctsio->kern_total_len = alloc_len;
10637 }
10638
10639 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr;
10640 if (format == 0) {
10641 scsi_ulto2b(0x12, hdr->data_length);
10642 hdr->first = 1;
10643 hdr->last = 1;
10644 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10645 descr->addr_ctl = 0x14;
10646 descr->track_number = 1;
10647 if (msf)
10648 ctl_ultomsf(0, descr->track_start);
10649 else
10650 scsi_ulto4b(0, descr->track_start);
10651 descr++;
10652 descr->addr_ctl = 0x14;
10653 descr->track_number = 0xaa;
10654 if (msf)
10655 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start);
10656 else
10657 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start);
10658 } else {
10659 scsi_ulto2b(0x0a, hdr->data_length);
10660 hdr->first = 1;
10661 hdr->last = 1;
10662 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10663 descr->addr_ctl = 0x14;
10664 descr->track_number = 1;
10665 if (msf)
10666 ctl_ultomsf(0, descr->track_start);
10667 else
10668 scsi_ulto4b(0, descr->track_start);
10669 }
10670
10671 ctl_set_success(ctsio);
10672 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10673 ctsio->be_move_done = ctl_config_move_done;
10674 ctl_datamove((union ctl_io *)ctsio);
10675 return (CTL_RETVAL_COMPLETE);
10676}
10677
10678/*
10679 * For known CDB types, parse the LBA and length.
10680 */
10681static int
10682ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10683{
10684 if (io->io_hdr.io_type != CTL_IO_SCSI)
10685 return (1);
10686
10687 switch (io->scsiio.cdb[0]) {
10688 case COMPARE_AND_WRITE: {
10689 struct scsi_compare_and_write *cdb;
10690
10691 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10692
10693 *lba = scsi_8btou64(cdb->addr);
10694 *len = cdb->length;
10695 break;
10696 }
10697 case READ_6:
10698 case WRITE_6: {
10699 struct scsi_rw_6 *cdb;
10700
10701 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10702
10703 *lba = scsi_3btoul(cdb->addr);
10704 /* only 5 bits are valid in the most significant address byte */
10705 *lba &= 0x1fffff;
10706 *len = cdb->length;
10707 break;
10708 }
10709 case READ_10:
10710 case WRITE_10: {
10711 struct scsi_rw_10 *cdb;
10712
10713 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10714
10715 *lba = scsi_4btoul(cdb->addr);
10716 *len = scsi_2btoul(cdb->length);
10717 break;
10718 }
10719 case WRITE_VERIFY_10: {
10720 struct scsi_write_verify_10 *cdb;
10721
10722 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10723
10724 *lba = scsi_4btoul(cdb->addr);
10725 *len = scsi_2btoul(cdb->length);
10726 break;
10727 }
10728 case READ_12:
10729 case WRITE_12: {
10730 struct scsi_rw_12 *cdb;
10731
10732 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10733
10734 *lba = scsi_4btoul(cdb->addr);
10735 *len = scsi_4btoul(cdb->length);
10736 break;
10737 }
10738 case WRITE_VERIFY_12: {
10739 struct scsi_write_verify_12 *cdb;
10740
10741 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10742
10743 *lba = scsi_4btoul(cdb->addr);
10744 *len = scsi_4btoul(cdb->length);
10745 break;
10746 }
10747 case READ_16:
10748 case WRITE_16: {
10749 struct scsi_rw_16 *cdb;
10750
10751 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10752
10753 *lba = scsi_8btou64(cdb->addr);
10754 *len = scsi_4btoul(cdb->length);
10755 break;
10756 }
10757 case WRITE_ATOMIC_16: {
10758 struct scsi_write_atomic_16 *cdb;
10759
10760 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb;
10761
10762 *lba = scsi_8btou64(cdb->addr);
10763 *len = scsi_2btoul(cdb->length);
10764 break;
10765 }
10766 case WRITE_VERIFY_16: {
10767 struct scsi_write_verify_16 *cdb;
10768
10769 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10770
10771 *lba = scsi_8btou64(cdb->addr);
10772 *len = scsi_4btoul(cdb->length);
10773 break;
10774 }
10775 case WRITE_SAME_10: {
10776 struct scsi_write_same_10 *cdb;
10777
10778 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10779
10780 *lba = scsi_4btoul(cdb->addr);
10781 *len = scsi_2btoul(cdb->length);
10782 break;
10783 }
10784 case WRITE_SAME_16: {
10785 struct scsi_write_same_16 *cdb;
10786
10787 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10788
10789 *lba = scsi_8btou64(cdb->addr);
10790 *len = scsi_4btoul(cdb->length);
10791 break;
10792 }
10793 case VERIFY_10: {
10794 struct scsi_verify_10 *cdb;
10795
10796 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10797
10798 *lba = scsi_4btoul(cdb->addr);
10799 *len = scsi_2btoul(cdb->length);
10800 break;
10801 }
10802 case VERIFY_12: {
10803 struct scsi_verify_12 *cdb;
10804
10805 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10806
10807 *lba = scsi_4btoul(cdb->addr);
10808 *len = scsi_4btoul(cdb->length);
10809 break;
10810 }
10811 case VERIFY_16: {
10812 struct scsi_verify_16 *cdb;
10813
10814 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10815
10816 *lba = scsi_8btou64(cdb->addr);
10817 *len = scsi_4btoul(cdb->length);
10818 break;
10819 }
10820 case UNMAP: {
10821 *lba = 0;
10822 *len = UINT64_MAX;
10823 break;
10824 }
10825 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10826 struct scsi_get_lba_status *cdb;
10827
10828 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10829 *lba = scsi_8btou64(cdb->addr);
10830 *len = UINT32_MAX;
10831 break;
10832 }
10833 default:
10834 return (1);
10835 break; /* NOTREACHED */
10836 }
10837
10838 return (0);
10839}
10840
10841static ctl_action
10842ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10843 bool seq)
10844{
10845 uint64_t endlba1, endlba2;
10846
10847 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10848 endlba2 = lba2 + len2 - 1;
10849
10850 if ((endlba1 < lba2) || (endlba2 < lba1))
10851 return (CTL_ACTION_PASS);
10852 else
10853 return (CTL_ACTION_BLOCK);
10854}
10855
10856static int
10857ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10858{
10859 struct ctl_ptr_len_flags *ptrlen;
10860 struct scsi_unmap_desc *buf, *end, *range;
10861 uint64_t lba;
10862 uint32_t len;
10863
10864 /* If not UNMAP -- go other way. */
10865 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10866 io->scsiio.cdb[0] != UNMAP)
10867 return (CTL_ACTION_ERROR);
10868
10869 /* If UNMAP without data -- block and wait for data. */
10870 ptrlen = (struct ctl_ptr_len_flags *)
10871 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10872 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10873 ptrlen->ptr == NULL)
10874 return (CTL_ACTION_BLOCK);
10875
10876 /* UNMAP with data -- check for collision. */
10877 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10878 end = buf + ptrlen->len / sizeof(*buf);
10879 for (range = buf; range < end; range++) {
10880 lba = scsi_8btou64(range->lba);
10881 len = scsi_4btoul(range->length);
10882 if ((lba < lba2 + len2) && (lba + len > lba2))
10883 return (CTL_ACTION_BLOCK);
10884 }
10885 return (CTL_ACTION_PASS);
10886}
10887
10888static ctl_action
10889ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10890{
10891 uint64_t lba1, lba2;
10892 uint64_t len1, len2;
10893 int retval;
10894
10895 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10896 return (CTL_ACTION_ERROR);
10897
10898 retval = ctl_extent_check_unmap(io1, lba2, len2);
10899 if (retval != CTL_ACTION_ERROR)
10900 return (retval);
10901
10902 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10903 return (CTL_ACTION_ERROR);
10904
10905 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10906 seq = FALSE;
10907 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10908}
10909
10910static ctl_action
10911ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
10912{
10913 uint64_t lba1, lba2;
10914 uint64_t len1, len2;
10915
10916 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10917 return (CTL_ACTION_PASS);
10918 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10919 return (CTL_ACTION_ERROR);
10920 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10921 return (CTL_ACTION_ERROR);
10922
10923 if (lba1 + len1 == lba2)
10924 return (CTL_ACTION_BLOCK);
10925 return (CTL_ACTION_PASS);
10926}
10927
10928static ctl_action
10929ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10930 union ctl_io *ooa_io)
10931{
10932 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10933 const ctl_serialize_action *serialize_row;
10934
10935 /*
10936 * The initiator attempted multiple untagged commands at the same
10937 * time. Can't do that.
10938 */
10939 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10940 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10941 && ((pending_io->io_hdr.nexus.targ_port ==
10942 ooa_io->io_hdr.nexus.targ_port)
10943 && (pending_io->io_hdr.nexus.initid ==
10944 ooa_io->io_hdr.nexus.initid))
10945 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10946 CTL_FLAG_STATUS_SENT)) == 0))
10947 return (CTL_ACTION_OVERLAP);
10948
10949 /*
10950 * The initiator attempted to send multiple tagged commands with
10951 * the same ID. (It's fine if different initiators have the same
10952 * tag ID.)
10953 *
10954 * Even if all of those conditions are true, we don't kill the I/O
10955 * if the command ahead of us has been aborted. We won't end up
10956 * sending it to the FETD, and it's perfectly legal to resend a
10957 * command with the same tag number as long as the previous
10958 * instance of this tag number has been aborted somehow.
10959 */
10960 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10961 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10962 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10963 && ((pending_io->io_hdr.nexus.targ_port ==
10964 ooa_io->io_hdr.nexus.targ_port)
10965 && (pending_io->io_hdr.nexus.initid ==
10966 ooa_io->io_hdr.nexus.initid))
10967 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10968 CTL_FLAG_STATUS_SENT)) == 0))
10969 return (CTL_ACTION_OVERLAP_TAG);
10970
10971 /*
10972 * If we get a head of queue tag, SAM-3 says that we should
10973 * immediately execute it.
10974 *
10975 * What happens if this command would normally block for some other
10976 * reason? e.g. a request sense with a head of queue tag
10977 * immediately after a write. Normally that would block, but this
10978 * will result in its getting executed immediately...
10979 *
10980 * We currently return "pass" instead of "skip", so we'll end up
10981 * going through the rest of the queue to check for overlapped tags.
10982 *
10983 * XXX KDM check for other types of blockage first??
10984 */
10985 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10986 return (CTL_ACTION_PASS);
10987
10988 /*
10989 * Ordered tags have to block until all items ahead of them
10990 * have completed. If we get called with an ordered tag, we always
10991 * block, if something else is ahead of us in the queue.
10992 */
10993 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10994 return (CTL_ACTION_BLOCK);
10995
10996 /*
10997 * Simple tags get blocked until all head of queue and ordered tags
10998 * ahead of them have completed. I'm lumping untagged commands in
10999 * with simple tags here. XXX KDM is that the right thing to do?
11000 */
11001 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11002 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
11003 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
11004 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
11005 return (CTL_ACTION_BLOCK);
11006
11007 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
11008 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT,
11009 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p",
11010 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0],
11011 pending_io->scsiio.cdb[1], pending_io));
11012 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
11013 if (ooa_entry->seridx == CTL_SERIDX_INVLD)
11014 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */
11015 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT,
11016 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p",
11017 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0],
11018 ooa_io->scsiio.cdb[1], ooa_io));
11019
11020 serialize_row = ctl_serialize_table[ooa_entry->seridx];
11021
11022 switch (serialize_row[pending_entry->seridx]) {
11023 case CTL_SER_BLOCK:
11024 return (CTL_ACTION_BLOCK);
11025 case CTL_SER_EXTENT:
11026 return (ctl_extent_check(ooa_io, pending_io,
11027 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
11028 case CTL_SER_EXTENTOPT:
11029 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
11030 SCP_QUEUE_ALG_UNRESTRICTED)
11031 return (ctl_extent_check(ooa_io, pending_io,
11032 (lun->be_lun &&
11033 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
11034 return (CTL_ACTION_PASS);
11035 case CTL_SER_EXTENTSEQ:
11036 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
11037 return (ctl_extent_check_seq(ooa_io, pending_io));
11038 return (CTL_ACTION_PASS);
11039 case CTL_SER_PASS:
11040 return (CTL_ACTION_PASS);
11041 case CTL_SER_BLOCKOPT:
11042 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
11043 SCP_QUEUE_ALG_UNRESTRICTED)
11044 return (CTL_ACTION_BLOCK);
11045 return (CTL_ACTION_PASS);
11046 case CTL_SER_SKIP:
11047 return (CTL_ACTION_SKIP);
11048 default:
11049 panic("%s: Invalid serialization value %d for %d => %d",
11050 __func__, serialize_row[pending_entry->seridx],
11051 pending_entry->seridx, ooa_entry->seridx);
11052 }
11053
11054 return (CTL_ACTION_ERROR);
11055}
11056
11057/*
11058 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
11059 * Assumptions:
11060 * - pending_io is generally either incoming, or on the blocked queue
11061 * - starting I/O is the I/O we want to start the check with.
11062 */
11063static ctl_action
11064ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
11065 union ctl_io *starting_io)
11066{
11067 union ctl_io *ooa_io;
11068 ctl_action action;
11069
11070 mtx_assert(&lun->lun_lock, MA_OWNED);
11071
11072 /*
11073 * Run back along the OOA queue, starting with the current
11074 * blocked I/O and going through every I/O before it on the
11075 * queue. If starting_io is NULL, we'll just end up returning
11076 * CTL_ACTION_PASS.
11077 */
11078 for (ooa_io = starting_io; ooa_io != NULL;
11079 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
11080 ooa_links)){
11081
11082 /*
11083 * This routine just checks to see whether
11084 * cur_blocked is blocked by ooa_io, which is ahead
11085 * of it in the queue. It doesn't queue/dequeue
11086 * cur_blocked.
11087 */
11088 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
11089 switch (action) {
11090 case CTL_ACTION_BLOCK:
11091 case CTL_ACTION_OVERLAP:
11092 case CTL_ACTION_OVERLAP_TAG:
11093 case CTL_ACTION_SKIP:
11094 case CTL_ACTION_ERROR:
11095 return (action);
11096 break; /* NOTREACHED */
11097 case CTL_ACTION_PASS:
11098 break;
11099 default:
11100 panic("%s: Invalid action %d\n", __func__, action);
11101 }
11102 }
11103
11104 return (CTL_ACTION_PASS);
11105}
11106
11107/*
11108 * Assumptions:
11109 * - An I/O has just completed, and has been removed from the per-LUN OOA
11110 * queue, so some items on the blocked queue may now be unblocked.
11111 */
11112static int
11113ctl_check_blocked(struct ctl_lun *lun)
11114{
11115 struct ctl_softc *softc = lun->ctl_softc;
11116 union ctl_io *cur_blocked, *next_blocked;
11117
11118 mtx_assert(&lun->lun_lock, MA_OWNED);
11119
11120 /*
11121 * Run forward from the head of the blocked queue, checking each
11122 * entry against the I/Os prior to it on the OOA queue to see if
11123 * there is still any blockage.
11124 *
11125 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
11126 * with our removing a variable on it while it is traversing the
11127 * list.
11128 */
11129 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
11130 cur_blocked != NULL; cur_blocked = next_blocked) {
11131 union ctl_io *prev_ooa;
11132 ctl_action action;
11133
11134 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
11135 blocked_links);
11136
11137 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
11138 ctl_ooaq, ooa_links);
11139
11140 /*
11141 * If cur_blocked happens to be the first item in the OOA
11142 * queue now, prev_ooa will be NULL, and the action
11143 * returned will just be CTL_ACTION_PASS.
11144 */
11145 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
11146
11147 switch (action) {
11148 case CTL_ACTION_BLOCK:
11149 /* Nothing to do here, still blocked */
11150 break;
11151 case CTL_ACTION_OVERLAP:
11152 case CTL_ACTION_OVERLAP_TAG:
11153 /*
11154 * This shouldn't happen! In theory we've already
11155 * checked this command for overlap...
11156 */
11157 break;
11158 case CTL_ACTION_PASS:
11159 case CTL_ACTION_SKIP: {
11160 const struct ctl_cmd_entry *entry;
11161
11162 /*
11163 * The skip case shouldn't happen, this transaction
11164 * should have never made it onto the blocked queue.
11165 */
11166 /*
11167 * This I/O is no longer blocked, we can remove it
11168 * from the blocked queue. Since this is a TAILQ
11169 * (doubly linked list), we can do O(1) removals
11170 * from any place on the list.
11171 */
11172 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
11173 blocked_links);
11174 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11175
11176 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
11177 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){
11178 /*
11179 * Need to send IO back to original side to
11180 * run
11181 */
11182 union ctl_ha_msg msg_info;
11183
11184 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11185 msg_info.hdr.original_sc =
11186 cur_blocked->io_hdr.original_sc;
11187 msg_info.hdr.serializing_sc = cur_blocked;
11188 msg_info.hdr.msg_type = CTL_MSG_R2R;
11189 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11190 sizeof(msg_info.hdr), M_NOWAIT);
11191 break;
11192 }
11193 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
11194
11195 /*
11196 * Check this I/O for LUN state changes that may
11197 * have happened while this command was blocked.
11198 * The LUN state may have been changed by a command
11199 * ahead of us in the queue, so we need to re-check
11200 * for any states that can be caused by SCSI
11201 * commands.
11202 */
11203 if (ctl_scsiio_lun_check(lun, entry,
11204 &cur_blocked->scsiio) == 0) {
11205 cur_blocked->io_hdr.flags |=
11206 CTL_FLAG_IS_WAS_ON_RTR;
11207 ctl_enqueue_rtr(cur_blocked);
11208 } else
11209 ctl_done(cur_blocked);
11210 break;
11211 }
11212 default:
11213 /*
11214 * This probably shouldn't happen -- we shouldn't
11215 * get CTL_ACTION_ERROR, or anything else.
11216 */
11217 break;
11218 }
11219 }
11220
11221 return (CTL_RETVAL_COMPLETE);
11222}
11223
11224/*
11225 * This routine (with one exception) checks LUN flags that can be set by
11226 * commands ahead of us in the OOA queue. These flags have to be checked
11227 * when a command initially comes in, and when we pull a command off the
11228 * blocked queue and are preparing to execute it. The reason we have to
11229 * check these flags for commands on the blocked queue is that the LUN
11230 * state may have been changed by a command ahead of us while we're on the
11231 * blocked queue.
11232 *
11233 * Ordering is somewhat important with these checks, so please pay
11234 * careful attention to the placement of any new checks.
11235 */
11236static int
11237ctl_scsiio_lun_check(struct ctl_lun *lun,
11238 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11239{
11240 struct ctl_softc *softc = lun->ctl_softc;
11241 int retval;
11242 uint32_t residx;
11243
11244 retval = 0;
11245
11246 mtx_assert(&lun->lun_lock, MA_OWNED);
11247
11248 /*
11249 * If this shelf is a secondary shelf controller, we may have to
11250 * reject some commands disallowed by HA mode and link state.
11251 */
11252 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11253 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
11254 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11255 ctl_set_lun_unavail(ctsio);
11256 retval = 1;
11257 goto bailout;
11258 }
11259 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
11260 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11261 ctl_set_lun_transit(ctsio);
11262 retval = 1;
11263 goto bailout;
11264 }
11265 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
11266 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
11267 ctl_set_lun_standby(ctsio);
11268 retval = 1;
11269 goto bailout;
11270 }
11271
11272 /* The rest of checks are only done on executing side */
11273 if (softc->ha_mode == CTL_HA_MODE_XFER)
11274 goto bailout;
11275 }
11276
11277 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11278 if (lun->be_lun &&
11279 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
11280 ctl_set_hw_write_protected(ctsio);
11281 retval = 1;
11282 goto bailout;
11283 }
11284 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) {
11285 ctl_set_sense(ctsio, /*current_error*/ 1,
11286 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11287 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11288 retval = 1;
11289 goto bailout;
11290 }
11291 }
11292
11293 /*
11294 * Check for a reservation conflict. If this command isn't allowed
11295 * even on reserved LUNs, and if this initiator isn't the one who
11296 * reserved us, reject the command with a reservation conflict.
11297 */
11298 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11299 if ((lun->flags & CTL_LUN_RESERVED)
11300 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11301 if (lun->res_idx != residx) {
11302 ctl_set_reservation_conflict(ctsio);
11303 retval = 1;
11304 goto bailout;
11305 }
11306 }
11307
11308 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11309 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11310 /* No reservation or command is allowed. */;
11311 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11312 (lun->pr_res_type == SPR_TYPE_WR_EX ||
11313 lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
11314 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) {
11315 /* The command is allowed for Write Exclusive resv. */;
11316 } else {
11317 /*
11318 * if we aren't registered or it's a res holder type
11319 * reservation and this isn't the res holder then set a
11320 * conflict.
11321 */
11322 if (ctl_get_prkey(lun, residx) == 0 ||
11323 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) {
11324 ctl_set_reservation_conflict(ctsio);
11325 retval = 1;
11326 goto bailout;
11327 }
11328 }
11329
11330 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) {
11331 if (lun->flags & CTL_LUN_EJECTED)
11332 ctl_set_lun_ejected(ctsio);
11333 else if (lun->flags & CTL_LUN_NO_MEDIA) {
11334 if (lun->flags & CTL_LUN_REMOVABLE)
11335 ctl_set_lun_no_media(ctsio);
11336 else
11337 ctl_set_lun_int_reqd(ctsio);
11338 } else if (lun->flags & CTL_LUN_STOPPED)
11339 ctl_set_lun_stopped(ctsio);
11340 else
11341 goto bailout;
11342 retval = 1;
11343 goto bailout;
11344 }
11345
11346bailout:
11347 return (retval);
11348}
11349
11350static void
11351ctl_failover_io(union ctl_io *io, int have_lock)
11352{
11353 ctl_set_busy(&io->scsiio);
11354 ctl_done(io);
11355}
11356
11357static void
11358ctl_failover_lun(union ctl_io *rio)
11359{
11360 struct ctl_softc *softc = CTL_SOFTC(rio);
11361 struct ctl_lun *lun;
11362 struct ctl_io_hdr *io, *next_io;
11363 uint32_t targ_lun;
11364
11365 targ_lun = rio->io_hdr.nexus.targ_mapped_lun;
11366 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun));
11367
11368 /* Find and lock the LUN. */
11369 mtx_lock(&softc->ctl_lock);
11370 if (targ_lun > CTL_MAX_LUNS ||
11371 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11372 mtx_unlock(&softc->ctl_lock);
11373 return;
11374 }
11375 mtx_lock(&lun->lun_lock);
11376 mtx_unlock(&softc->ctl_lock);
11377 if (lun->flags & CTL_LUN_DISABLED) {
11378 mtx_unlock(&lun->lun_lock);
11379 return;
11380 }
11381
11382 if (softc->ha_mode == CTL_HA_MODE_XFER) {
11383 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11384 /* We are master */
11385 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11386 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11387 io->flags |= CTL_FLAG_ABORT;
11388 io->flags |= CTL_FLAG_FAILOVER;
11389 } else { /* This can be only due to DATAMOVE */
11390 io->msg_type = CTL_MSG_DATAMOVE_DONE;
11391 io->flags &= ~CTL_FLAG_DMA_INPROG;
11392 io->flags |= CTL_FLAG_IO_ACTIVE;
11393 io->port_status = 31340;
11394 ctl_enqueue_isc((union ctl_io *)io);
11395 }
11396 }
11397 /* We are slave */
11398 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11399 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11400 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11401 io->flags |= CTL_FLAG_FAILOVER;
11402 } else {
11403 ctl_set_busy(&((union ctl_io *)io)->
11404 scsiio);
11405 ctl_done((union ctl_io *)io);
11406 }
11407 }
11408 }
11409 } else { /* SERIALIZE modes */
11410 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links,
11411 next_io) {
11412 /* We are master */
11413 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11414 TAILQ_REMOVE(&lun->blocked_queue, io,
11415 blocked_links);
11416 io->flags &= ~CTL_FLAG_BLOCKED;
11417 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11418 ctl_free_io((union ctl_io *)io);
11419 }
11420 }
11421 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11422 /* We are master */
11423 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11424 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11425 ctl_free_io((union ctl_io *)io);
11426 }
11427 /* We are slave */
11428 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11429 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11430 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
11431 ctl_set_busy(&((union ctl_io *)io)->
11432 scsiio);
11433 ctl_done((union ctl_io *)io);
11434 }
11435 }
11436 }
11437 ctl_check_blocked(lun);
11438 }
11439 mtx_unlock(&lun->lun_lock);
11440}
11441
11442static int
11443ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11444{
11445 struct ctl_lun *lun;
11446 const struct ctl_cmd_entry *entry;
11447 uint32_t initidx, targ_lun;
11448 int retval = 0;
11449
11450 lun = NULL;
11451 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11452 if (targ_lun < CTL_MAX_LUNS)
11453 lun = softc->ctl_luns[targ_lun];
11454 if (lun) {
11455 /*
11456 * If the LUN is invalid, pretend that it doesn't exist.
11457 * It will go away as soon as all pending I/O has been
11458 * completed.
11459 */
11460 mtx_lock(&lun->lun_lock);
11461 if (lun->flags & CTL_LUN_DISABLED) {
11462 mtx_unlock(&lun->lun_lock);
11463 lun = NULL;
11464 }
11465 }
11466 CTL_LUN(ctsio) = lun;
11467 if (lun) {
11468 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
11469
11470 /*
11471 * Every I/O goes into the OOA queue for a particular LUN,
11472 * and stays there until completion.
11473 */
11474#ifdef CTL_TIME_IO
11475 if (TAILQ_EMPTY(&lun->ooa_queue))
11476 lun->idle_time += getsbinuptime() - lun->last_busy;
11477#endif
11478 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
11479 }
11480
11481 /* Get command entry and return error if it is unsuppotyed. */
11482 entry = ctl_validate_command(ctsio);
11483 if (entry == NULL) {
11484 if (lun)
11485 mtx_unlock(&lun->lun_lock);
11486 return (retval);
11487 }
11488
11489 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11490 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11491
11492 /*
11493 * Check to see whether we can send this command to LUNs that don't
11494 * exist. This should pretty much only be the case for inquiry
11495 * and request sense. Further checks, below, really require having
11496 * a LUN, so we can't really check the command anymore. Just put
11497 * it on the rtr queue.
11498 */
11499 if (lun == NULL) {
11500 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11501 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11502 ctl_enqueue_rtr((union ctl_io *)ctsio);
11503 return (retval);
11504 }
11505
11506 ctl_set_unsupported_lun(ctsio);
11507 ctl_done((union ctl_io *)ctsio);
11508 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11509 return (retval);
11510 } else {
11511 /*
11512 * Make sure we support this particular command on this LUN.
11513 * e.g., we don't support writes to the control LUN.
11514 */
11515 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11516 mtx_unlock(&lun->lun_lock);
11517 ctl_set_invalid_opcode(ctsio);
11518 ctl_done((union ctl_io *)ctsio);
11519 return (retval);
11520 }
11521 }
11522
11523 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11524
11525#ifdef CTL_WITH_CA
11526 /*
11527 * If we've got a request sense, it'll clear the contingent
11528 * allegiance condition. Otherwise, if we have a CA condition for
11529 * this initiator, clear it, because it sent down a command other
11530 * than request sense.
11531 */
11532 if ((ctsio->cdb[0] != REQUEST_SENSE)
11533 && (ctl_is_set(lun->have_ca, initidx)))
11534 ctl_clear_mask(lun->have_ca, initidx);
11535#endif
11536
11537 /*
11538 * If the command has this flag set, it handles its own unit
11539 * attention reporting, we shouldn't do anything. Otherwise we
11540 * check for any pending unit attentions, and send them back to the
11541 * initiator. We only do this when a command initially comes in,
11542 * not when we pull it off the blocked queue.
11543 *
11544 * According to SAM-3, section 5.3.2, the order that things get
11545 * presented back to the host is basically unit attentions caused
11546 * by some sort of reset event, busy status, reservation conflicts
11547 * or task set full, and finally any other status.
11548 *
11549 * One issue here is that some of the unit attentions we report
11550 * don't fall into the "reset" category (e.g. "reported luns data
11551 * has changed"). So reporting it here, before the reservation
11552 * check, may be technically wrong. I guess the only thing to do
11553 * would be to check for and report the reset events here, and then
11554 * check for the other unit attention types after we check for a
11555 * reservation conflict.
11556 *
11557 * XXX KDM need to fix this
11558 */
11559 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11560 ctl_ua_type ua_type;
11561 u_int sense_len = 0;
11562
11563 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11564 &sense_len, SSD_TYPE_NONE);
11565 if (ua_type != CTL_UA_NONE) {
11566 mtx_unlock(&lun->lun_lock);
11567 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11568 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11569 ctsio->sense_len = sense_len;
11570 ctl_done((union ctl_io *)ctsio);
11571 return (retval);
11572 }
11573 }
11574
11575
11576 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11577 mtx_unlock(&lun->lun_lock);
11578 ctl_done((union ctl_io *)ctsio);
11579 return (retval);
11580 }
11581
11582 /*
11583 * XXX CHD this is where we want to send IO to other side if
11584 * this LUN is secondary on this SC. We will need to make a copy
11585 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11586 * the copy we send as FROM_OTHER.
11587 * We also need to stuff the address of the original IO so we can
11588 * find it easily. Something similar will need be done on the other
11589 * side so when we are done we can find the copy.
11590 */
11591 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11592 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
11593 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
11594 union ctl_ha_msg msg_info;
11595 int isc_retval;
11596
11597 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11598 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11599 mtx_unlock(&lun->lun_lock);
11600
11601 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11602 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11603 msg_info.hdr.serializing_sc = NULL;
11604 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11605 msg_info.scsi.tag_num = ctsio->tag_num;
11606 msg_info.scsi.tag_type = ctsio->tag_type;
11607 msg_info.scsi.cdb_len = ctsio->cdb_len;
11608 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11609
11610 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11611 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11612 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11613 ctl_set_busy(ctsio);
11614 ctl_done((union ctl_io *)ctsio);
11615 return (retval);
11616 }
11617 return (retval);
11618 }
11619
11620 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11621 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11622 ctl_ooaq, ooa_links))) {
11623 case CTL_ACTION_BLOCK:
11624 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11625 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11626 blocked_links);
11627 mtx_unlock(&lun->lun_lock);
11628 return (retval);
11629 case CTL_ACTION_PASS:
11630 case CTL_ACTION_SKIP:
11631 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11632 mtx_unlock(&lun->lun_lock);
11633 ctl_enqueue_rtr((union ctl_io *)ctsio);
11634 break;
11635 case CTL_ACTION_OVERLAP:
11636 mtx_unlock(&lun->lun_lock);
11637 ctl_set_overlapped_cmd(ctsio);
11638 ctl_done((union ctl_io *)ctsio);
11639 break;
11640 case CTL_ACTION_OVERLAP_TAG:
11641 mtx_unlock(&lun->lun_lock);
11642 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11643 ctl_done((union ctl_io *)ctsio);
11644 break;
11645 case CTL_ACTION_ERROR:
11646 default:
11647 mtx_unlock(&lun->lun_lock);
11648 ctl_set_internal_failure(ctsio,
11649 /*sks_valid*/ 0,
11650 /*retry_count*/ 0);
11651 ctl_done((union ctl_io *)ctsio);
11652 break;
11653 }
11654 return (retval);
11655}
11656
11657const struct ctl_cmd_entry *
11658ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11659{
11660 const struct ctl_cmd_entry *entry;
11661 int service_action;
11662
11663 entry = &ctl_cmd_table[ctsio->cdb[0]];
11664 if (sa)
11665 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11666 if (entry->flags & CTL_CMD_FLAG_SA5) {
11667 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11668 entry = &((const struct ctl_cmd_entry *)
11669 entry->execute)[service_action];
11670 }
11671 return (entry);
11672}
11673
11674const struct ctl_cmd_entry *
11675ctl_validate_command(struct ctl_scsiio *ctsio)
11676{
11677 const struct ctl_cmd_entry *entry;
11678 int i, sa;
11679 uint8_t diff;
11680
11681 entry = ctl_get_cmd_entry(ctsio, &sa);
11682 if (entry->execute == NULL) {
11683 if (sa)
11684 ctl_set_invalid_field(ctsio,
11685 /*sks_valid*/ 1,
11686 /*command*/ 1,
11687 /*field*/ 1,
11688 /*bit_valid*/ 1,
11689 /*bit*/ 4);
11690 else
11691 ctl_set_invalid_opcode(ctsio);
11692 ctl_done((union ctl_io *)ctsio);
11693 return (NULL);
11694 }
11695 KASSERT(entry->length > 0,
11696 ("Not defined length for command 0x%02x/0x%02x",
11697 ctsio->cdb[0], ctsio->cdb[1]));
11698 for (i = 1; i < entry->length; i++) {
11699 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11700 if (diff == 0)
11701 continue;
11702 ctl_set_invalid_field(ctsio,
11703 /*sks_valid*/ 1,
11704 /*command*/ 1,
11705 /*field*/ i,
11706 /*bit_valid*/ 1,
11707 /*bit*/ fls(diff) - 1);
11708 ctl_done((union ctl_io *)ctsio);
11709 return (NULL);
11710 }
11711 return (entry);
11712}
11713
11714static int
11715ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11716{
11717
11718 switch (lun_type) {
11719 case T_DIRECT:
11720 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0)
11721 return (0);
11722 break;
11723 case T_PROCESSOR:
11724 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11725 return (0);
11726 break;
11727 case T_CDROM:
11728 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0)
11729 return (0);
11730 break;
11731 default:
11732 return (0);
11733 }
11734 return (1);
11735}
11736
11737static int
11738ctl_scsiio(struct ctl_scsiio *ctsio)
11739{
11740 int retval;
11741 const struct ctl_cmd_entry *entry;
11742
11743 retval = CTL_RETVAL_COMPLETE;
11744
11745 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11746
11747 entry = ctl_get_cmd_entry(ctsio, NULL);
11748
11749 /*
11750 * If this I/O has been aborted, just send it straight to
11751 * ctl_done() without executing it.
11752 */
11753 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11754 ctl_done((union ctl_io *)ctsio);
11755 goto bailout;
11756 }
11757
11758 /*
11759 * All the checks should have been handled by ctl_scsiio_precheck().
11760 * We should be clear now to just execute the I/O.
11761 */
11762 retval = entry->execute(ctsio);
11763
11764bailout:
11765 return (retval);
11766}
11767
11768/*
11769 * Since we only implement one target right now, a bus reset simply resets
11770 * our single target.
11771 */
11772static int
11773ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io)
11774{
11775 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET));
11776}
11777
11778static int
11779ctl_target_reset(struct ctl_softc *softc, union ctl_io *io,
11780 ctl_ua_type ua_type)
11781{
11782 struct ctl_port *port = CTL_PORT(io);
11783 struct ctl_lun *lun;
11784 int retval;
11785
11786 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11787 union ctl_ha_msg msg_info;
11788
11789 msg_info.hdr.nexus = io->io_hdr.nexus;
11790 if (ua_type==CTL_UA_TARG_RESET)
11791 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11792 else
11793 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11794 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11795 msg_info.hdr.original_sc = NULL;
11796 msg_info.hdr.serializing_sc = NULL;
11797 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11798 sizeof(msg_info.task), M_WAITOK);
11799 }
11800 retval = 0;
11801
11802 mtx_lock(&softc->ctl_lock);
11803 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11804 if (port != NULL &&
11805 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
11806 continue;
11807 retval += ctl_do_lun_reset(lun, io, ua_type);
11808 }
11809 mtx_unlock(&softc->ctl_lock);
11810 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11811 return (retval);
11812}
11813
11814/*
11815 * The LUN should always be set. The I/O is optional, and is used to
11816 * distinguish between I/Os sent by this initiator, and by other
11817 * initiators. We set unit attention for initiators other than this one.
11818 * SAM-3 is vague on this point. It does say that a unit attention should
11819 * be established for other initiators when a LUN is reset (see section
11820 * 5.7.3), but it doesn't specifically say that the unit attention should
11821 * be established for this particular initiator when a LUN is reset. Here
11822 * is the relevant text, from SAM-3 rev 8:
11823 *
11824 * 5.7.2 When a SCSI initiator port aborts its own tasks
11825 *
11826 * When a SCSI initiator port causes its own task(s) to be aborted, no
11827 * notification that the task(s) have been aborted shall be returned to
11828 * the SCSI initiator port other than the completion response for the
11829 * command or task management function action that caused the task(s) to
11830 * be aborted and notification(s) associated with related effects of the
11831 * action (e.g., a reset unit attention condition).
11832 *
11833 * XXX KDM for now, we're setting unit attention for all initiators.
11834 */
11835static int
11836ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11837{
11838 union ctl_io *xio;
11839#if 0
11840 uint32_t initidx;
11841#endif
11842 int i;
11843
11844 mtx_lock(&lun->lun_lock);
11845 /*
11846 * Run through the OOA queue and abort each I/O.
11847 */
11848 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11849 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11850 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11851 }
11852
11853 /*
11854 * This version sets unit attention for every
11855 */
11856#if 0
11857 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11858 ctl_est_ua_all(lun, initidx, ua_type);
11859#else
11860 ctl_est_ua_all(lun, -1, ua_type);
11861#endif
11862
11863 /*
11864 * A reset (any kind, really) clears reservations established with
11865 * RESERVE/RELEASE. It does not clear reservations established
11866 * with PERSISTENT RESERVE OUT, but we don't support that at the
11867 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11868 * reservations made with the RESERVE/RELEASE commands, because
11869 * those commands are obsolete in SPC-3.
11870 */
11871 lun->flags &= ~CTL_LUN_RESERVED;
11872
11873#ifdef CTL_WITH_CA
11874 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11875 ctl_clear_mask(lun->have_ca, i);
11876#endif
11877 lun->prevent_count = 0;
11878 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11879 ctl_clear_mask(lun->prevent, i);
11880 mtx_unlock(&lun->lun_lock);
11881
11882 return (0);
11883}
11884
11885static int
11886ctl_lun_reset(struct ctl_softc *softc, union ctl_io *io)
11887{
11888 struct ctl_lun *lun;
11889 uint32_t targ_lun;
11890 int retval;
11891
11892 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11893 mtx_lock(&softc->ctl_lock);
11894 if (targ_lun >= CTL_MAX_LUNS ||
11895 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11896 mtx_unlock(&softc->ctl_lock);
11897 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11898 return (1);
11899 }
11900 retval = ctl_do_lun_reset(lun, io, CTL_UA_LUN_RESET);
11901 mtx_unlock(&softc->ctl_lock);
11902 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11903
11904 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
11905 union ctl_ha_msg msg_info;
11906
11907 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11908 msg_info.hdr.nexus = io->io_hdr.nexus;
11909 msg_info.task.task_action = CTL_TASK_LUN_RESET;
11910 msg_info.hdr.original_sc = NULL;
11911 msg_info.hdr.serializing_sc = NULL;
11912 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11913 sizeof(msg_info.task), M_WAITOK);
11914 }
11915 return (retval);
11916}
11917
11918static void
11919ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11920 int other_sc)
11921{
11922 union ctl_io *xio;
11923
11924 mtx_assert(&lun->lun_lock, MA_OWNED);
11925
11926 /*
11927 * Run through the OOA queue and attempt to find the given I/O.
11928 * The target port, initiator ID, tag type and tag number have to
11929 * match the values that we got from the initiator. If we have an
11930 * untagged command to abort, simply abort the first untagged command
11931 * we come to. We only allow one untagged command at a time of course.
11932 */
11933 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11934 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11935
11936 if ((targ_port == UINT32_MAX ||
11937 targ_port == xio->io_hdr.nexus.targ_port) &&
11938 (init_id == UINT32_MAX ||
11939 init_id == xio->io_hdr.nexus.initid)) {
11940 if (targ_port != xio->io_hdr.nexus.targ_port ||
11941 init_id != xio->io_hdr.nexus.initid)
11942 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
11943 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11944 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11945 union ctl_ha_msg msg_info;
11946
11947 msg_info.hdr.nexus = xio->io_hdr.nexus;
11948 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11949 msg_info.task.tag_num = xio->scsiio.tag_num;
11950 msg_info.task.tag_type = xio->scsiio.tag_type;
11951 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11952 msg_info.hdr.original_sc = NULL;
11953 msg_info.hdr.serializing_sc = NULL;
11954 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11955 sizeof(msg_info.task), M_NOWAIT);
11956 }
11957 }
11958 }
11959}
11960
11961static int
11962ctl_abort_task_set(union ctl_io *io)
11963{
11964 struct ctl_softc *softc = CTL_SOFTC(io);
11965 struct ctl_lun *lun;
11966 uint32_t targ_lun;
11967
11968 /*
11969 * Look up the LUN.
11970 */
11971 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11972 mtx_lock(&softc->ctl_lock);
11973 if (targ_lun >= CTL_MAX_LUNS ||
11974 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11975 mtx_unlock(&softc->ctl_lock);
11976 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11977 return (1);
11978 }
11979
11980 mtx_lock(&lun->lun_lock);
11981 mtx_unlock(&softc->ctl_lock);
11982 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
11983 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11984 io->io_hdr.nexus.initid,
11985 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11986 } else { /* CTL_TASK_CLEAR_TASK_SET */
11987 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
11988 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11989 }
11990 mtx_unlock(&lun->lun_lock);
11991 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11992 return (0);
11993}
11994
11995static int
11996ctl_i_t_nexus_reset(union ctl_io *io)
11997{
11998 struct ctl_softc *softc = CTL_SOFTC(io);
11999 struct ctl_lun *lun;
12000 uint32_t initidx;
12001
12002 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12003 union ctl_ha_msg msg_info;
12004
12005 msg_info.hdr.nexus = io->io_hdr.nexus;
12006 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
12007 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12008 msg_info.hdr.original_sc = NULL;
12009 msg_info.hdr.serializing_sc = NULL;
12010 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12011 sizeof(msg_info.task), M_WAITOK);
12012 }
12013
12014 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12015 mtx_lock(&softc->ctl_lock);
12016 STAILQ_FOREACH(lun, &softc->lun_list, links) {
12017 mtx_lock(&lun->lun_lock);
12018 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12019 io->io_hdr.nexus.initid, 1);
12020#ifdef CTL_WITH_CA
12021 ctl_clear_mask(lun->have_ca, initidx);
12022#endif
12023 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
12024 lun->flags &= ~CTL_LUN_RESERVED;
12025 if (ctl_is_set(lun->prevent, initidx)) {
12026 ctl_clear_mask(lun->prevent, initidx);
12027 lun->prevent_count--;
12028 }
12029 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS);
12030 mtx_unlock(&lun->lun_lock);
12031 }
12032 mtx_unlock(&softc->ctl_lock);
12033 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12034 return (0);
12035}
12036
12037static int
12038ctl_abort_task(union ctl_io *io)
12039{
12040 struct ctl_softc *softc = CTL_SOFTC(io);
12041 union ctl_io *xio;
12042 struct ctl_lun *lun;
12043#if 0
12044 struct sbuf sb;
12045 char printbuf[128];
12046#endif
12047 int found;
12048 uint32_t targ_lun;
12049
12050 found = 0;
12051
12052 /*
12053 * Look up the LUN.
12054 */
12055 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12056 mtx_lock(&softc->ctl_lock);
12057 if (targ_lun >= CTL_MAX_LUNS ||
12058 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12059 mtx_unlock(&softc->ctl_lock);
12060 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12061 return (1);
12062 }
12063
12064#if 0
12065 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
12066 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
12067#endif
12068
12069 mtx_lock(&lun->lun_lock);
12070 mtx_unlock(&softc->ctl_lock);
12071 /*
12072 * Run through the OOA queue and attempt to find the given I/O.
12073 * The target port, initiator ID, tag type and tag number have to
12074 * match the values that we got from the initiator. If we have an
12075 * untagged command to abort, simply abort the first untagged command
12076 * we come to. We only allow one untagged command at a time of course.
12077 */
12078 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12079 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12080#if 0
12081 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
12082
12083 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
12084 lun->lun, xio->scsiio.tag_num,
12085 xio->scsiio.tag_type,
12086 (xio->io_hdr.blocked_links.tqe_prev
12087 == NULL) ? "" : " BLOCKED",
12088 (xio->io_hdr.flags &
12089 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
12090 (xio->io_hdr.flags &
12091 CTL_FLAG_ABORT) ? " ABORT" : "",
12092 (xio->io_hdr.flags &
12093 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
12094 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
12095 sbuf_finish(&sb);
12096 printf("%s\n", sbuf_data(&sb));
12097#endif
12098
12099 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12100 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12101 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12102 continue;
12103
12104 /*
12105 * If the abort says that the task is untagged, the
12106 * task in the queue must be untagged. Otherwise,
12107 * we just check to see whether the tag numbers
12108 * match. This is because the QLogic firmware
12109 * doesn't pass back the tag type in an abort
12110 * request.
12111 */
12112#if 0
12113 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12114 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12115 || (xio->scsiio.tag_num == io->taskio.tag_num))
12116#endif
12117 /*
12118 * XXX KDM we've got problems with FC, because it
12119 * doesn't send down a tag type with aborts. So we
12120 * can only really go by the tag number...
12121 * This may cause problems with parallel SCSI.
12122 * Need to figure that out!!
12123 */
12124 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12125 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12126 found = 1;
12127 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
12128 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12129 union ctl_ha_msg msg_info;
12130
12131 msg_info.hdr.nexus = io->io_hdr.nexus;
12132 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12133 msg_info.task.tag_num = io->taskio.tag_num;
12134 msg_info.task.tag_type = io->taskio.tag_type;
12135 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12136 msg_info.hdr.original_sc = NULL;
12137 msg_info.hdr.serializing_sc = NULL;
12138#if 0
12139 printf("Sent Abort to other side\n");
12140#endif
12141 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12142 sizeof(msg_info.task), M_NOWAIT);
12143 }
12144#if 0
12145 printf("ctl_abort_task: found I/O to abort\n");
12146#endif
12147 }
12148 }
12149 mtx_unlock(&lun->lun_lock);
12150
12151 if (found == 0) {
12152 /*
12153 * This isn't really an error. It's entirely possible for
12154 * the abort and command completion to cross on the wire.
12155 * This is more of an informative/diagnostic error.
12156 */
12157#if 0
12158 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12159 "%u:%u:%u tag %d type %d\n",
12160 io->io_hdr.nexus.initid,
12161 io->io_hdr.nexus.targ_port,
12162 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12163 io->taskio.tag_type);
12164#endif
12165 }
12166 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12167 return (0);
12168}
12169
12170static int
12171ctl_query_task(union ctl_io *io, int task_set)
12172{
12173 struct ctl_softc *softc = CTL_SOFTC(io);
12174 union ctl_io *xio;
12175 struct ctl_lun *lun;
12176 int found = 0;
12177 uint32_t targ_lun;
12178
12179 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12180 mtx_lock(&softc->ctl_lock);
12181 if (targ_lun >= CTL_MAX_LUNS ||
12182 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12183 mtx_unlock(&softc->ctl_lock);
12184 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12185 return (1);
12186 }
12187 mtx_lock(&lun->lun_lock);
12188 mtx_unlock(&softc->ctl_lock);
12189 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12190 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12191
12192 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12193 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12194 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12195 continue;
12196
12197 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
12198 found = 1;
12199 break;
12200 }
12201 }
12202 mtx_unlock(&lun->lun_lock);
12203 if (found)
12204 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12205 else
12206 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12207 return (0);
12208}
12209
12210static int
12211ctl_query_async_event(union ctl_io *io)
12212{
12213 struct ctl_softc *softc = CTL_SOFTC(io);
12214 struct ctl_lun *lun;
12215 ctl_ua_type ua;
12216 uint32_t targ_lun, initidx;
12217
12218 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12219 mtx_lock(&softc->ctl_lock);
12220 if (targ_lun >= CTL_MAX_LUNS ||
12221 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12222 mtx_unlock(&softc->ctl_lock);
12223 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12224 return (1);
12225 }
12226 mtx_lock(&lun->lun_lock);
12227 mtx_unlock(&softc->ctl_lock);
12228 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12229 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
12230 mtx_unlock(&lun->lun_lock);
12231 if (ua != CTL_UA_NONE)
12232 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12233 else
12234 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12235 return (0);
12236}
12237
12238static void
12239ctl_run_task(union ctl_io *io)
12240{
12241 struct ctl_softc *softc = CTL_SOFTC(io);
12242 int retval = 1;
12243
12244 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12245 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12246 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
12247 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
12248 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
12249 switch (io->taskio.task_action) {
12250 case CTL_TASK_ABORT_TASK:
12251 retval = ctl_abort_task(io);
12252 break;
12253 case CTL_TASK_ABORT_TASK_SET:
12254 case CTL_TASK_CLEAR_TASK_SET:
12255 retval = ctl_abort_task_set(io);
12256 break;
12257 case CTL_TASK_CLEAR_ACA:
12258 break;
12259 case CTL_TASK_I_T_NEXUS_RESET:
12260 retval = ctl_i_t_nexus_reset(io);
12261 break;
12262 case CTL_TASK_LUN_RESET:
12263 retval = ctl_lun_reset(softc, io);
12264 break;
12265 case CTL_TASK_TARGET_RESET:
12266 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET);
12267 break;
12268 case CTL_TASK_BUS_RESET:
12269 retval = ctl_bus_reset(softc, io);
12270 break;
12271 case CTL_TASK_PORT_LOGIN:
12272 break;
12273 case CTL_TASK_PORT_LOGOUT:
12274 break;
12275 case CTL_TASK_QUERY_TASK:
12276 retval = ctl_query_task(io, 0);
12277 break;
12278 case CTL_TASK_QUERY_TASK_SET:
12279 retval = ctl_query_task(io, 1);
12280 break;
12281 case CTL_TASK_QUERY_ASYNC_EVENT:
12282 retval = ctl_query_async_event(io);
12283 break;
12284 default:
12285 printf("%s: got unknown task management event %d\n",
12286 __func__, io->taskio.task_action);
12287 break;
12288 }
12289 if (retval == 0)
12290 io->io_hdr.status = CTL_SUCCESS;
12291 else
12292 io->io_hdr.status = CTL_ERROR;
12293 ctl_done(io);
12294}
12295
12296/*
12297 * For HA operation. Handle commands that come in from the other
12298 * controller.
12299 */
12300static void
12301ctl_handle_isc(union ctl_io *io)
12302{
12303 struct ctl_softc *softc = CTL_SOFTC(io);
12304 struct ctl_lun *lun;
12305 const struct ctl_cmd_entry *entry;
12306 uint32_t targ_lun;
12307
12308 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12309 switch (io->io_hdr.msg_type) {
12310 case CTL_MSG_SERIALIZE:
12311 ctl_serialize_other_sc_cmd(&io->scsiio);
12312 break;
12313 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */
12314 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12315 if (targ_lun >= CTL_MAX_LUNS ||
12316 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12317 ctl_done(io);
12318 break;
12319 }
12320 mtx_lock(&lun->lun_lock);
12321 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) {
12322 mtx_unlock(&lun->lun_lock);
12323 ctl_done(io);
12324 break;
12325 }
12326 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12327 mtx_unlock(&lun->lun_lock);
12328 ctl_enqueue_rtr(io);
12329 break;
12330 case CTL_MSG_FINISH_IO:
12331 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12332 ctl_done(io);
12333 break;
12334 }
12335 if (targ_lun >= CTL_MAX_LUNS ||
12336 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12337 ctl_free_io(io);
12338 break;
12339 }
12340 mtx_lock(&lun->lun_lock);
12341 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12342 ctl_check_blocked(lun);
12343 mtx_unlock(&lun->lun_lock);
12344 ctl_free_io(io);
12345 break;
12346 case CTL_MSG_PERS_ACTION:
12347 ctl_hndl_per_res_out_on_other_sc(io);
12348 ctl_free_io(io);
12349 break;
12350 case CTL_MSG_BAD_JUJU:
12351 ctl_done(io);
12352 break;
12353 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */
12354 ctl_datamove_remote(io);
12355 break;
12356 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */
12357 io->scsiio.be_move_done(io);
12358 break;
12359 case CTL_MSG_FAILOVER:
12360 ctl_failover_lun(io);
12361 ctl_free_io(io);
12362 break;
12363 default:
12364 printf("%s: Invalid message type %d\n",
12365 __func__, io->io_hdr.msg_type);
12366 ctl_free_io(io);
12367 break;
12368 }
12369
12370}
12371
12372
12373/*
12374 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12375 * there is no match.
12376 */
12377static ctl_lun_error_pattern
12378ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12379{
12380 const struct ctl_cmd_entry *entry;
12381 ctl_lun_error_pattern filtered_pattern, pattern;
12382
12383 pattern = desc->error_pattern;
12384
12385 /*
12386 * XXX KDM we need more data passed into this function to match a
12387 * custom pattern, and we actually need to implement custom pattern
12388 * matching.
12389 */
12390 if (pattern & CTL_LUN_PAT_CMD)
12391 return (CTL_LUN_PAT_CMD);
12392
12393 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12394 return (CTL_LUN_PAT_ANY);
12395
12396 entry = ctl_get_cmd_entry(ctsio, NULL);
12397
12398 filtered_pattern = entry->pattern & pattern;
12399
12400 /*
12401 * If the user requested specific flags in the pattern (e.g.
12402 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12403 * flags.
12404 *
12405 * If the user did not specify any flags, it doesn't matter whether
12406 * or not the command supports the flags.
12407 */
12408 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12409 (pattern & ~CTL_LUN_PAT_MASK))
12410 return (CTL_LUN_PAT_NONE);
12411
12412 /*
12413 * If the user asked for a range check, see if the requested LBA
12414 * range overlaps with this command's LBA range.
12415 */
12416 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12417 uint64_t lba1;
12418 uint64_t len1;
12419 ctl_action action;
12420 int retval;
12421
12422 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12423 if (retval != 0)
12424 return (CTL_LUN_PAT_NONE);
12425
12426 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12427 desc->lba_range.len, FALSE);
12428 /*
12429 * A "pass" means that the LBA ranges don't overlap, so
12430 * this doesn't match the user's range criteria.
12431 */
12432 if (action == CTL_ACTION_PASS)
12433 return (CTL_LUN_PAT_NONE);
12434 }
12435
12436 return (filtered_pattern);
12437}
12438
12439static void
12440ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12441{
12442 struct ctl_error_desc *desc, *desc2;
12443
12444 mtx_assert(&lun->lun_lock, MA_OWNED);
12445
12446 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12447 ctl_lun_error_pattern pattern;
12448 /*
12449 * Check to see whether this particular command matches
12450 * the pattern in the descriptor.
12451 */
12452 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12453 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12454 continue;
12455
12456 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12457 case CTL_LUN_INJ_ABORTED:
12458 ctl_set_aborted(&io->scsiio);
12459 break;
12460 case CTL_LUN_INJ_MEDIUM_ERR:
12461 ctl_set_medium_error(&io->scsiio,
12462 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
12463 CTL_FLAG_DATA_OUT);
12464 break;
12465 case CTL_LUN_INJ_UA:
12466 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12467 * OCCURRED */
12468 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12469 break;
12470 case CTL_LUN_INJ_CUSTOM:
12471 /*
12472 * We're assuming the user knows what he is doing.
12473 * Just copy the sense information without doing
12474 * checks.
12475 */
12476 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12477 MIN(sizeof(desc->custom_sense),
12478 sizeof(io->scsiio.sense_data)));
12479 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12480 io->scsiio.sense_len = SSD_FULL_SIZE;
12481 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12482 break;
12483 case CTL_LUN_INJ_NONE:
12484 default:
12485 /*
12486 * If this is an error injection type we don't know
12487 * about, clear the continuous flag (if it is set)
12488 * so it will get deleted below.
12489 */
12490 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12491 break;
12492 }
12493 /*
12494 * By default, each error injection action is a one-shot
12495 */
12496 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12497 continue;
12498
12499 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12500
12501 free(desc, M_CTL);
12502 }
12503}
12504
12505#ifdef CTL_IO_DELAY
12506static void
12507ctl_datamove_timer_wakeup(void *arg)
12508{
12509 union ctl_io *io;
12510
12511 io = (union ctl_io *)arg;
12512
12513 ctl_datamove(io);
12514}
12515#endif /* CTL_IO_DELAY */
12516
12517void
12518ctl_datamove(union ctl_io *io)
12519{
12520 void (*fe_datamove)(union ctl_io *io);
12521
12522 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12523
12524 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12525
12526#ifdef CTL_TIME_IO
12527 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12528 char str[256];
12529 char path_str[64];
12530 struct sbuf sb;
12531
12532 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12533 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12534
12535 sbuf_cat(&sb, path_str);
12536 switch (io->io_hdr.io_type) {
12537 case CTL_IO_SCSI:
12538 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12539 sbuf_printf(&sb, "\n");
12540 sbuf_cat(&sb, path_str);
12541 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12542 io->scsiio.tag_num, io->scsiio.tag_type);
12543 break;
12544 case CTL_IO_TASK:
12545 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12546 "Tag Type: %d\n", io->taskio.task_action,
12547 io->taskio.tag_num, io->taskio.tag_type);
12548 break;
12549 default:
12550 panic("%s: Invalid CTL I/O type %d\n",
12551 __func__, io->io_hdr.io_type);
12552 }
12553 sbuf_cat(&sb, path_str);
12554 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12555 (intmax_t)time_uptime - io->io_hdr.start_time);
12556 sbuf_finish(&sb);
12557 printf("%s", sbuf_data(&sb));
12558 }
12559#endif /* CTL_TIME_IO */
12560
12561#ifdef CTL_IO_DELAY
12562 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12563 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12564 } else {
12565 if ((lun != NULL)
12566 && (lun->delay_info.datamove_delay > 0)) {
12567
12568 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12569 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12570 callout_reset(&io->io_hdr.delay_callout,
12571 lun->delay_info.datamove_delay * hz,
12572 ctl_datamove_timer_wakeup, io);
12573 if (lun->delay_info.datamove_type ==
12574 CTL_DELAY_TYPE_ONESHOT)
12575 lun->delay_info.datamove_delay = 0;
12576 return;
12577 }
12578 }
12579#endif
12580
12581 /*
12582 * This command has been aborted. Set the port status, so we fail
12583 * the data move.
12584 */
12585 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12586 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12587 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12588 io->io_hdr.nexus.targ_port,
12589 io->io_hdr.nexus.targ_lun);
12590 io->io_hdr.port_status = 31337;
12591 /*
12592 * Note that the backend, in this case, will get the
12593 * callback in its context. In other cases it may get
12594 * called in the frontend's interrupt thread context.
12595 */
12596 io->scsiio.be_move_done(io);
12597 return;
12598 }
12599
12600 /* Don't confuse frontend with zero length data move. */
12601 if (io->scsiio.kern_data_len == 0) {
12602 io->scsiio.be_move_done(io);
12603 return;
12604 }
12605
12606 fe_datamove = CTL_PORT(io)->fe_datamove;
12607 fe_datamove(io);
12608}
12609
12610static void
12611ctl_send_datamove_done(union ctl_io *io, int have_lock)
12612{
12613 union ctl_ha_msg msg;
12614#ifdef CTL_TIME_IO
12615 struct bintime cur_bt;
12616#endif
12617
12618 memset(&msg, 0, sizeof(msg));
12619 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12620 msg.hdr.original_sc = io;
12621 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12622 msg.hdr.nexus = io->io_hdr.nexus;
12623 msg.hdr.status = io->io_hdr.status;
12624 msg.scsi.tag_num = io->scsiio.tag_num;
12625 msg.scsi.tag_type = io->scsiio.tag_type;
12626 msg.scsi.scsi_status = io->scsiio.scsi_status;
12627 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12628 io->scsiio.sense_len);
12629 msg.scsi.sense_len = io->scsiio.sense_len;
12630 msg.scsi.sense_residual = io->scsiio.sense_residual;
12631 msg.scsi.fetd_status = io->io_hdr.port_status;
12632 msg.scsi.residual = io->scsiio.residual;
12633 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12634 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12635 ctl_failover_io(io, /*have_lock*/ have_lock);
12636 return;
12637 }
12638 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12639 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12640 msg.scsi.sense_len, M_WAITOK);
12641
12642#ifdef CTL_TIME_IO
12643 getbinuptime(&cur_bt);
12644 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12645 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12646#endif
12647 io->io_hdr.num_dmas++;
12648}
12649
12650/*
12651 * The DMA to the remote side is done, now we need to tell the other side
12652 * we're done so it can continue with its data movement.
12653 */
12654static void
12655ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12656{
12657 union ctl_io *io;
12658 uint32_t i;
12659
12660 io = rq->context;
12661
12662 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12663 printf("%s: ISC DMA write failed with error %d", __func__,
12664 rq->ret);
12665 ctl_set_internal_failure(&io->scsiio,
12666 /*sks_valid*/ 1,
12667 /*retry_count*/ rq->ret);
12668 }
12669
12670 ctl_dt_req_free(rq);
12671
12672 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12673 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12674 free(io->io_hdr.remote_sglist, M_CTL);
12675 io->io_hdr.remote_sglist = NULL;
12676 io->io_hdr.local_sglist = NULL;
12677
12678 /*
12679 * The data is in local and remote memory, so now we need to send
12680 * status (good or back) back to the other side.
12681 */
12682 ctl_send_datamove_done(io, /*have_lock*/ 0);
12683}
12684
12685/*
12686 * We've moved the data from the host/controller into local memory. Now we
12687 * need to push it over to the remote controller's memory.
12688 */
12689static int
12690ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12691{
12692 int retval;
12693
12694 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12695 ctl_datamove_remote_write_cb);
12696 return (retval);
12697}
12698
12699static void
12700ctl_datamove_remote_write(union ctl_io *io)
12701{
12702 int retval;
12703 void (*fe_datamove)(union ctl_io *io);
12704
12705 /*
12706 * - Get the data from the host/HBA into local memory.
12707 * - DMA memory from the local controller to the remote controller.
12708 * - Send status back to the remote controller.
12709 */
12710
12711 retval = ctl_datamove_remote_sgl_setup(io);
12712 if (retval != 0)
12713 return;
12714
12715 /* Switch the pointer over so the FETD knows what to do */
12716 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12717
12718 /*
12719 * Use a custom move done callback, since we need to send completion
12720 * back to the other controller, not to the backend on this side.
12721 */
12722 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12723
12724 fe_datamove = CTL_PORT(io)->fe_datamove;
12725 fe_datamove(io);
12726}
12727
12728static int
12729ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12730{
12731#if 0
12732 char str[256];
12733 char path_str[64];
12734 struct sbuf sb;
12735#endif
12736 uint32_t i;
12737
12738 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12739 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12740 free(io->io_hdr.remote_sglist, M_CTL);
12741 io->io_hdr.remote_sglist = NULL;
12742 io->io_hdr.local_sglist = NULL;
12743
12744#if 0
12745 scsi_path_string(io, path_str, sizeof(path_str));
12746 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12747 sbuf_cat(&sb, path_str);
12748 scsi_command_string(&io->scsiio, NULL, &sb);
12749 sbuf_printf(&sb, "\n");
12750 sbuf_cat(&sb, path_str);
12751 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12752 io->scsiio.tag_num, io->scsiio.tag_type);
12753 sbuf_cat(&sb, path_str);
12754 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12755 io->io_hdr.flags, io->io_hdr.status);
12756 sbuf_finish(&sb);
12757 printk("%s", sbuf_data(&sb));
12758#endif
12759
12760
12761 /*
12762 * The read is done, now we need to send status (good or bad) back
12763 * to the other side.
12764 */
12765 ctl_send_datamove_done(io, /*have_lock*/ 0);
12766
12767 return (0);
12768}
12769
12770static void
12771ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12772{
12773 union ctl_io *io;
12774 void (*fe_datamove)(union ctl_io *io);
12775
12776 io = rq->context;
12777
12778 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12779 printf("%s: ISC DMA read failed with error %d\n", __func__,
12780 rq->ret);
12781 ctl_set_internal_failure(&io->scsiio,
12782 /*sks_valid*/ 1,
12783 /*retry_count*/ rq->ret);
12784 }
12785
12786 ctl_dt_req_free(rq);
12787
12788 /* Switch the pointer over so the FETD knows what to do */
12789 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12790
12791 /*
12792 * Use a custom move done callback, since we need to send completion
12793 * back to the other controller, not to the backend on this side.
12794 */
12795 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12796
12797 /* XXX KDM add checks like the ones in ctl_datamove? */
12798
12799 fe_datamove = CTL_PORT(io)->fe_datamove;
12800 fe_datamove(io);
12801}
12802
12803static int
12804ctl_datamove_remote_sgl_setup(union ctl_io *io)
12805{
12806 struct ctl_sg_entry *local_sglist;
12807 uint32_t len_to_go;
12808 int retval;
12809 int i;
12810
12811 retval = 0;
12812 local_sglist = io->io_hdr.local_sglist;
12813 len_to_go = io->scsiio.kern_data_len;
12814
12815 /*
12816 * The difficult thing here is that the size of the various
12817 * S/G segments may be different than the size from the
12818 * remote controller. That'll make it harder when DMAing
12819 * the data back to the other side.
12820 */
12821 for (i = 0; len_to_go > 0; i++) {
12822 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12823 local_sglist[i].addr =
12824 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12825
12826 len_to_go -= local_sglist[i].len;
12827 }
12828 /*
12829 * Reset the number of S/G entries accordingly. The original
12830 * number of S/G entries is available in rem_sg_entries.
12831 */
12832 io->scsiio.kern_sg_entries = i;
12833
12834#if 0
12835 printf("%s: kern_sg_entries = %d\n", __func__,
12836 io->scsiio.kern_sg_entries);
12837 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12838 printf("%s: sg[%d] = %p, %lu\n", __func__, i,
12839 local_sglist[i].addr, local_sglist[i].len);
12840#endif
12841
12842 return (retval);
12843}
12844
12845static int
12846ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12847 ctl_ha_dt_cb callback)
12848{
12849 struct ctl_ha_dt_req *rq;
12850 struct ctl_sg_entry *remote_sglist, *local_sglist;
12851 uint32_t local_used, remote_used, total_used;
12852 int i, j, isc_ret;
12853
12854 rq = ctl_dt_req_alloc();
12855
12856 /*
12857 * If we failed to allocate the request, and if the DMA didn't fail
12858 * anyway, set busy status. This is just a resource allocation
12859 * failure.
12860 */
12861 if ((rq == NULL)
12862 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12863 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12864 ctl_set_busy(&io->scsiio);
12865
12866 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12867 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12868
12869 if (rq != NULL)
12870 ctl_dt_req_free(rq);
12871
12872 /*
12873 * The data move failed. We need to return status back
12874 * to the other controller. No point in trying to DMA
12875 * data to the remote controller.
12876 */
12877
12878 ctl_send_datamove_done(io, /*have_lock*/ 0);
12879
12880 return (1);
12881 }
12882
12883 local_sglist = io->io_hdr.local_sglist;
12884 remote_sglist = io->io_hdr.remote_sglist;
12885 local_used = 0;
12886 remote_used = 0;
12887 total_used = 0;
12888
12889 /*
12890 * Pull/push the data over the wire from/to the other controller.
12891 * This takes into account the possibility that the local and
12892 * remote sglists may not be identical in terms of the size of
12893 * the elements and the number of elements.
12894 *
12895 * One fundamental assumption here is that the length allocated for
12896 * both the local and remote sglists is identical. Otherwise, we've
12897 * essentially got a coding error of some sort.
12898 */
12899 isc_ret = CTL_HA_STATUS_SUCCESS;
12900 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12901 uint32_t cur_len;
12902 uint8_t *tmp_ptr;
12903
12904 rq->command = command;
12905 rq->context = io;
12906
12907 /*
12908 * Both pointers should be aligned. But it is possible
12909 * that the allocation length is not. They should both
12910 * also have enough slack left over at the end, though,
12911 * to round up to the next 8 byte boundary.
12912 */
12913 cur_len = MIN(local_sglist[i].len - local_used,
12914 remote_sglist[j].len - remote_used);
12915 rq->size = cur_len;
12916
12917 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12918 tmp_ptr += local_used;
12919
12920#if 0
12921 /* Use physical addresses when talking to ISC hardware */
12922 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12923 /* XXX KDM use busdma */
12924 rq->local = vtophys(tmp_ptr);
12925 } else
12926 rq->local = tmp_ptr;
12927#else
12928 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12929 ("HA does not support BUS_ADDR"));
12930 rq->local = tmp_ptr;
12931#endif
12932
12933 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12934 tmp_ptr += remote_used;
12935 rq->remote = tmp_ptr;
12936
12937 rq->callback = NULL;
12938
12939 local_used += cur_len;
12940 if (local_used >= local_sglist[i].len) {
12941 i++;
12942 local_used = 0;
12943 }
12944
12945 remote_used += cur_len;
12946 if (remote_used >= remote_sglist[j].len) {
12947 j++;
12948 remote_used = 0;
12949 }
12950 total_used += cur_len;
12951
12952 if (total_used >= io->scsiio.kern_data_len)
12953 rq->callback = callback;
12954
12955#if 0
12956 printf("%s: %s: local %p remote %p size %d\n", __func__,
12957 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
12958 rq->local, rq->remote, rq->size);
12959#endif
12960
12961 isc_ret = ctl_dt_single(rq);
12962 if (isc_ret > CTL_HA_STATUS_SUCCESS)
12963 break;
12964 }
12965 if (isc_ret != CTL_HA_STATUS_WAIT) {
12966 rq->ret = isc_ret;
12967 callback(rq);
12968 }
12969
12970 return (0);
12971}
12972
12973static void
12974ctl_datamove_remote_read(union ctl_io *io)
12975{
12976 int retval;
12977 uint32_t i;
12978
12979 /*
12980 * This will send an error to the other controller in the case of a
12981 * failure.
12982 */
12983 retval = ctl_datamove_remote_sgl_setup(io);
12984 if (retval != 0)
12985 return;
12986
12987 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12988 ctl_datamove_remote_read_cb);
12989 if (retval != 0) {
12990 /*
12991 * Make sure we free memory if there was an error.. The
12992 * ctl_datamove_remote_xfer() function will send the
12993 * datamove done message, or call the callback with an
12994 * error if there is a problem.
12995 */
12996 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12997 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12998 free(io->io_hdr.remote_sglist, M_CTL);
12999 io->io_hdr.remote_sglist = NULL;
13000 io->io_hdr.local_sglist = NULL;
13001 }
13002}
13003
13004/*
13005 * Process a datamove request from the other controller. This is used for
13006 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
13007 * first. Once that is complete, the data gets DMAed into the remote
13008 * controller's memory. For reads, we DMA from the remote controller's
13009 * memory into our memory first, and then move it out to the FETD.
13010 */
13011static void
13012ctl_datamove_remote(union ctl_io *io)
13013{
13014
13015 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
13016
13017 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13018 ctl_failover_io(io, /*have_lock*/ 0);
13019 return;
13020 }
13021
13022 /*
13023 * Note that we look for an aborted I/O here, but don't do some of
13024 * the other checks that ctl_datamove() normally does.
13025 * We don't need to run the datamove delay code, since that should
13026 * have been done if need be on the other controller.
13027 */
13028 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13029 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
13030 io->scsiio.tag_num, io->io_hdr.nexus.initid,
13031 io->io_hdr.nexus.targ_port,
13032 io->io_hdr.nexus.targ_lun);
13033 io->io_hdr.port_status = 31338;
13034 ctl_send_datamove_done(io, /*have_lock*/ 0);
13035 return;
13036 }
13037
13038 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
13039 ctl_datamove_remote_write(io);
13040 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
13041 ctl_datamove_remote_read(io);
13042 else {
13043 io->io_hdr.port_status = 31339;
13044 ctl_send_datamove_done(io, /*have_lock*/ 0);
13045 }
13046}
13047
13048static void
13049ctl_process_done(union ctl_io *io)
13050{
13051 struct ctl_softc *softc = CTL_SOFTC(io);
13052 struct ctl_lun *lun = CTL_LUN(io);
13053 void (*fe_done)(union ctl_io *io);
13054 union ctl_ha_msg msg;
13055 uint32_t targ_port = io->io_hdr.nexus.targ_port;
13056
13057 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13058 fe_done = softc->ctl_ports[targ_port]->fe_done;
13059
13060#ifdef CTL_TIME_IO
13061 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13062 char str[256];
13063 char path_str[64];
13064 struct sbuf sb;
13065
13066 ctl_scsi_path_string(io, path_str, sizeof(path_str));
13067 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13068
13069 sbuf_cat(&sb, path_str);
13070 switch (io->io_hdr.io_type) {
13071 case CTL_IO_SCSI:
13072 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
13073 sbuf_printf(&sb, "\n");
13074 sbuf_cat(&sb, path_str);
13075 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13076 io->scsiio.tag_num, io->scsiio.tag_type);
13077 break;
13078 case CTL_IO_TASK:
13079 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
13080 "Tag Type: %d\n", io->taskio.task_action,
13081 io->taskio.tag_num, io->taskio.tag_type);
13082 break;
13083 default:
13084 panic("%s: Invalid CTL I/O type %d\n",
13085 __func__, io->io_hdr.io_type);
13086 }
13087 sbuf_cat(&sb, path_str);
13088 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13089 (intmax_t)time_uptime - io->io_hdr.start_time);
13090 sbuf_finish(&sb);
13091 printf("%s", sbuf_data(&sb));
13092 }
13093#endif /* CTL_TIME_IO */
13094
13095 switch (io->io_hdr.io_type) {
13096 case CTL_IO_SCSI:
13097 break;
13098 case CTL_IO_TASK:
13099 if (ctl_debug & CTL_DEBUG_INFO)
13100 ctl_io_error_print(io, NULL);
13101 fe_done(io);
13102 return;
13103 default:
13104 panic("%s: Invalid CTL I/O type %d\n",
13105 __func__, io->io_hdr.io_type);
13106 }
13107
13108 if (lun == NULL) {
13109 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13110 io->io_hdr.nexus.targ_mapped_lun));
13111 goto bailout;
13112 }
13113
13114 mtx_lock(&lun->lun_lock);
13115
13116 /*
13117 * Check to see if we have any informational exception and status
13118 * of this command can be modified to report it in form of either
13119 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field.
13120 */
13121 if (lun->ie_reported == 0 && lun->ie_asc != 0 &&
13122 io->io_hdr.status == CTL_SUCCESS &&
13123 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) {
13124 uint8_t mrie = lun->MODE_IE.mrie;
13125 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) ||
13126 (lun->MODE_VER.byte3 & SMS_VER_PER));
13127 if (((mrie == SIEP_MRIE_REC_COND && per) ||
13128 mrie == SIEP_MRIE_REC_UNCOND ||
13129 mrie == SIEP_MRIE_NO_SENSE) &&
13130 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags &
13131 CTL_CMD_FLAG_NO_SENSE) == 0) {
13132 ctl_set_sense(&io->scsiio,
13133 /*current_error*/ 1,
13134 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ?
13135 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR,
13136 /*asc*/ lun->ie_asc,
13137 /*ascq*/ lun->ie_ascq,
13138 SSD_ELEM_NONE);
13139 lun->ie_reported = 1;
13140 }
13141 } else if (lun->ie_reported < 0)
13142 lun->ie_reported = 0;
13143
13144 /*
13145 * Check to see if we have any errors to inject here. We only
13146 * inject errors for commands that don't already have errors set.
13147 */
13148 if (!STAILQ_EMPTY(&lun->error_list) &&
13149 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
13150 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
13151 ctl_inject_error(lun, io);
13152
13153 /*
13154 * XXX KDM how do we treat commands that aren't completed
13155 * successfully?
13156 *
13157 * XXX KDM should we also track I/O latency?
13158 */
13159 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13160 io->io_hdr.io_type == CTL_IO_SCSI) {
13161#ifdef CTL_TIME_IO
13162 struct bintime cur_bt;
13163#endif
13164 int type;
13165
13166 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13167 CTL_FLAG_DATA_IN)
13168 type = CTL_STATS_READ;
13169 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13170 CTL_FLAG_DATA_OUT)
13171 type = CTL_STATS_WRITE;
13172 else
13173 type = CTL_STATS_NO_IO;
13174
13175 lun->stats.ports[targ_port].bytes[type] +=
13176 io->scsiio.kern_total_len;
13177 lun->stats.ports[targ_port].operations[type]++;
13178#ifdef CTL_TIME_IO
13179 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13180 &io->io_hdr.dma_bt);
13181 getbinuptime(&cur_bt);
13182 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13183 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13184#endif
13185 lun->stats.ports[targ_port].num_dmas[type] +=
13186 io->io_hdr.num_dmas;
13187 }
13188
13189 /*
13190 * Remove this from the OOA queue.
13191 */
13192 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13193#ifdef CTL_TIME_IO
13194 if (TAILQ_EMPTY(&lun->ooa_queue))
13195 lun->last_busy = getsbinuptime();
13196#endif
13197
13198 /*
13199 * Run through the blocked queue on this LUN and see if anything
13200 * has become unblocked, now that this transaction is done.
13201 */
13202 ctl_check_blocked(lun);
13203
13204 /*
13205 * If the LUN has been invalidated, free it if there is nothing
13206 * left on its OOA queue.
13207 */
13208 if ((lun->flags & CTL_LUN_INVALID)
13209 && TAILQ_EMPTY(&lun->ooa_queue)) {
13210 mtx_unlock(&lun->lun_lock);
13211 mtx_lock(&softc->ctl_lock);
13212 ctl_free_lun(lun);
13213 mtx_unlock(&softc->ctl_lock);
13214 } else
13215 mtx_unlock(&lun->lun_lock);
13216
13217bailout:
13218
13219 /*
13220 * If this command has been aborted, make sure we set the status
13221 * properly. The FETD is responsible for freeing the I/O and doing
13222 * whatever it needs to do to clean up its state.
13223 */
13224 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13225 ctl_set_task_aborted(&io->scsiio);
13226
13227 /*
13228 * If enabled, print command error status.
13229 */
13230 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
13231 (ctl_debug & CTL_DEBUG_INFO) != 0)
13232 ctl_io_error_print(io, NULL);
13233
13234 /*
13235 * Tell the FETD or the other shelf controller we're done with this
13236 * command. Note that only SCSI commands get to this point. Task
13237 * management commands are completed above.
13238 */
13239 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
13240 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
13241 memset(&msg, 0, sizeof(msg));
13242 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13243 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
13244 msg.hdr.nexus = io->io_hdr.nexus;
13245 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13246 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
13247 M_WAITOK);
13248 }
13249
13250 fe_done(io);
13251}
13252
13253#ifdef CTL_WITH_CA
13254/*
13255 * Front end should call this if it doesn't do autosense. When the request
13256 * sense comes back in from the initiator, we'll dequeue this and send it.
13257 */
13258int
13259ctl_queue_sense(union ctl_io *io)
13260{
13261 struct ctl_softc *softc = CTL_SOFTC(io);
13262 struct ctl_port *port = CTL_PORT(io);
13263 struct ctl_lun *lun;
13264 uint32_t initidx, targ_lun;
13265
13266 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13267
13268 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13269
13270 /*
13271 * LUN lookup will likely move to the ctl_work_thread() once we
13272 * have our new queueing infrastructure (that doesn't put things on
13273 * a per-LUN queue initially). That is so that we can handle
13274 * things like an INQUIRY to a LUN that we don't have enabled. We
13275 * can't deal with that right now.
13276 * If we don't have a LUN for this, just toss the sense information.
13277 */
13278 mtx_lock(&softc->ctl_lock);
13279 if (targ_lun >= CTL_MAX_LUNS ||
13280 (lun = softc->ctl_luns[targ_lun]) == NULL) {
13281 mtx_unlock(&softc->ctl_lock);
13282 goto bailout;
13283 }
13284 mtx_lock(&lun->lun_lock);
13285 mtx_unlock(&softc->ctl_lock);
13286
13287 /*
13288 * Already have CA set for this LUN...toss the sense information.
13289 */
13290 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13291 if (ctl_is_set(lun->have_ca, initidx)) {
13292 mtx_unlock(&lun->lun_lock);
13293 goto bailout;
13294 }
13295
13296 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13297 MIN(sizeof(lun->pending_sense[initidx]),
13298 sizeof(io->scsiio.sense_data)));
13299 ctl_set_mask(lun->have_ca, initidx);
13300 mtx_unlock(&lun->lun_lock);
13301
13302bailout:
13303 ctl_free_io(io);
13304 return (CTL_RETVAL_COMPLETE);
13305}
13306#endif
13307
13308/*
13309 * Primary command inlet from frontend ports. All SCSI and task I/O
13310 * requests must go through this function.
13311 */
13312int
13313ctl_queue(union ctl_io *io)
13314{
13315 struct ctl_port *port = CTL_PORT(io);
13316
13317 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13318
13319#ifdef CTL_TIME_IO
13320 io->io_hdr.start_time = time_uptime;
13321 getbinuptime(&io->io_hdr.start_bt);
13322#endif /* CTL_TIME_IO */
13323
13324 /* Map FE-specific LUN ID into global one. */
13325 io->io_hdr.nexus.targ_mapped_lun =
13326 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13327
13328 switch (io->io_hdr.io_type) {
13329 case CTL_IO_SCSI:
13330 case CTL_IO_TASK:
13331 if (ctl_debug & CTL_DEBUG_CDB)
13332 ctl_io_print(io);
13333 ctl_enqueue_incoming(io);
13334 break;
13335 default:
13336 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13337 return (EINVAL);
13338 }
13339
13340 return (CTL_RETVAL_COMPLETE);
13341}
13342
13343#ifdef CTL_IO_DELAY
13344static void
13345ctl_done_timer_wakeup(void *arg)
13346{
13347 union ctl_io *io;
13348
13349 io = (union ctl_io *)arg;
13350 ctl_done(io);
13351}
13352#endif /* CTL_IO_DELAY */
13353
13354void
13355ctl_serseq_done(union ctl_io *io)
13356{
13357 struct ctl_lun *lun = CTL_LUN(io);;
13358
13359 if (lun->be_lun == NULL ||
13360 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF)
13361 return;
13362 mtx_lock(&lun->lun_lock);
13363 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13364 ctl_check_blocked(lun);
13365 mtx_unlock(&lun->lun_lock);
13366}
13367
13368void
13369ctl_done(union ctl_io *io)
13370{
13371
13372 /*
13373 * Enable this to catch duplicate completion issues.
13374 */
13375#if 0
13376 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13377 printf("%s: type %d msg %d cdb %x iptl: "
13378 "%u:%u:%u tag 0x%04x "
13379 "flag %#x status %x\n",
13380 __func__,
13381 io->io_hdr.io_type,
13382 io->io_hdr.msg_type,
13383 io->scsiio.cdb[0],
13384 io->io_hdr.nexus.initid,
13385 io->io_hdr.nexus.targ_port,
13386 io->io_hdr.nexus.targ_lun,
13387 (io->io_hdr.io_type ==
13388 CTL_IO_TASK) ?
13389 io->taskio.tag_num :
13390 io->scsiio.tag_num,
13391 io->io_hdr.flags,
13392 io->io_hdr.status);
13393 } else
13394 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13395#endif
13396
13397 /*
13398 * This is an internal copy of an I/O, and should not go through
13399 * the normal done processing logic.
13400 */
13401 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13402 return;
13403
13404#ifdef CTL_IO_DELAY
13405 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13406 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13407 } else {
13408 struct ctl_lun *lun = CTL_LUN(io);
13409
13410 if ((lun != NULL)
13411 && (lun->delay_info.done_delay > 0)) {
13412
13413 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13414 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13415 callout_reset(&io->io_hdr.delay_callout,
13416 lun->delay_info.done_delay * hz,
13417 ctl_done_timer_wakeup, io);
13418 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13419 lun->delay_info.done_delay = 0;
13420 return;
13421 }
13422 }
13423#endif /* CTL_IO_DELAY */
13424
13425 ctl_enqueue_done(io);
13426}
13427
13428static void
13429ctl_work_thread(void *arg)
13430{
13431 struct ctl_thread *thr = (struct ctl_thread *)arg;
13432 struct ctl_softc *softc = thr->ctl_softc;
13433 union ctl_io *io;
13434 int retval;
13435
13436 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13437
13438 for (;;) {
13439 /*
13440 * We handle the queues in this order:
13441 * - ISC
13442 * - done queue (to free up resources, unblock other commands)
13443 * - RtR queue
13444 * - incoming queue
13445 *
13446 * If those queues are empty, we break out of the loop and
13447 * go to sleep.
13448 */
13449 mtx_lock(&thr->queue_lock);
13450 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13451 if (io != NULL) {
13452 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13453 mtx_unlock(&thr->queue_lock);
13454 ctl_handle_isc(io);
13455 continue;
13456 }
13457 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13458 if (io != NULL) {
13459 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13460 /* clear any blocked commands, call fe_done */
13461 mtx_unlock(&thr->queue_lock);
13462 ctl_process_done(io);
13463 continue;
13464 }
13465 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13466 if (io != NULL) {
13467 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13468 mtx_unlock(&thr->queue_lock);
13469 if (io->io_hdr.io_type == CTL_IO_TASK)
13470 ctl_run_task(io);
13471 else
13472 ctl_scsiio_precheck(softc, &io->scsiio);
13473 continue;
13474 }
13475 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13476 if (io != NULL) {
13477 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13478 mtx_unlock(&thr->queue_lock);
13479 retval = ctl_scsiio(&io->scsiio);
13480 if (retval != CTL_RETVAL_COMPLETE)
13481 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13482 continue;
13483 }
13484
13485 /* Sleep until we have something to do. */
13486 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13487 }
13488}
13489
13490static void
13491ctl_lun_thread(void *arg)
13492{
13493 struct ctl_softc *softc = (struct ctl_softc *)arg;
13494 struct ctl_be_lun *be_lun;
13495
13496 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13497
13498 for (;;) {
13499 mtx_lock(&softc->ctl_lock);
13500 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13501 if (be_lun != NULL) {
13502 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13503 mtx_unlock(&softc->ctl_lock);
13504 ctl_create_lun(be_lun);
13505 continue;
13506 }
13507
13508 /* Sleep until we have something to do. */
13509 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13510 PDROP | PRIBIO, "-", 0);
13511 }
13512}
13513
13514static void
13515ctl_thresh_thread(void *arg)
13516{
13517 struct ctl_softc *softc = (struct ctl_softc *)arg;
13518 struct ctl_lun *lun;
13519 struct ctl_logical_block_provisioning_page *page;
13520 const char *attr;
13521 union ctl_ha_msg msg;
13522 uint64_t thres, val;
13523 int i, e, set;
13524
13525 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13526
13527 for (;;) {
13528 mtx_lock(&softc->ctl_lock);
13529 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13530 if ((lun->flags & CTL_LUN_DISABLED) ||
13531 (lun->flags & CTL_LUN_NO_MEDIA) ||
13532 lun->backend->lun_attr == NULL)
13533 continue;
13534 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13535 softc->ha_mode == CTL_HA_MODE_XFER)
13536 continue;
13537 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0)
13538 continue;
13539 e = 0;
13540 page = &lun->MODE_LBP;
13541 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13542 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13543 continue;
13544 thres = scsi_4btoul(page->descr[i].count);
13545 thres <<= CTL_LBP_EXPONENT;
13546 switch (page->descr[i].resource) {
13547 case 0x01:
13548 attr = "blocksavail";
13549 break;
13550 case 0x02:
13551 attr = "blocksused";
13552 break;
13553 case 0xf1:
13554 attr = "poolblocksavail";
13555 break;
13556 case 0xf2:
13557 attr = "poolblocksused";
13558 break;
13559 default:
13560 continue;
13561 }
13562 mtx_unlock(&softc->ctl_lock); // XXX
13563 val = lun->backend->lun_attr(
13564 lun->be_lun->be_lun, attr);
13565 mtx_lock(&softc->ctl_lock);
13566 if (val == UINT64_MAX)
13567 continue;
13568 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13569 == SLBPPD_ARMING_INC)
13570 e = (val >= thres);
13571 else
13572 e = (val <= thres);
13573 if (e)
13574 break;
13575 }
13576 mtx_lock(&lun->lun_lock);
13577 if (e) {
13578 scsi_u64to8b((uint8_t *)&page->descr[i] -
13579 (uint8_t *)page, lun->ua_tpt_info);
13580 if (lun->lasttpt == 0 ||
13581 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13582 lun->lasttpt = time_uptime;
13583 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13584 set = 1;
13585 } else
13586 set = 0;
13587 } else {
13588 lun->lasttpt = 0;
13589 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13590 set = -1;
13591 }
13592 mtx_unlock(&lun->lun_lock);
13593 if (set != 0 &&
13594 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13595 /* Send msg to other side. */
13596 bzero(&msg.ua, sizeof(msg.ua));
13597 msg.hdr.msg_type = CTL_MSG_UA;
13598 msg.hdr.nexus.initid = -1;
13599 msg.hdr.nexus.targ_port = -1;
13600 msg.hdr.nexus.targ_lun = lun->lun;
13601 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13602 msg.ua.ua_all = 1;
13603 msg.ua.ua_set = (set > 0);
13604 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13605 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
13606 mtx_unlock(&softc->ctl_lock); // XXX
13607 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13608 sizeof(msg.ua), M_WAITOK);
13609 mtx_lock(&softc->ctl_lock);
13610 }
13611 }
13612 mtx_unlock(&softc->ctl_lock);
13613 pause("-", CTL_LBP_PERIOD * hz);
13614 }
13615}
13616
13617static void
13618ctl_enqueue_incoming(union ctl_io *io)
13619{
13620 struct ctl_softc *softc = CTL_SOFTC(io);
13621 struct ctl_thread *thr;
13622 u_int idx;
13623
13624 idx = (io->io_hdr.nexus.targ_port * 127 +
13625 io->io_hdr.nexus.initid) % worker_threads;
13626 thr = &softc->threads[idx];
13627 mtx_lock(&thr->queue_lock);
13628 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13629 mtx_unlock(&thr->queue_lock);
13630 wakeup(thr);
13631}
13632
13633static void
13634ctl_enqueue_rtr(union ctl_io *io)
13635{
13636 struct ctl_softc *softc = CTL_SOFTC(io);
13637 struct ctl_thread *thr;
13638
13639 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13640 mtx_lock(&thr->queue_lock);
13641 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13642 mtx_unlock(&thr->queue_lock);
13643 wakeup(thr);
13644}
13645
13646static void
13647ctl_enqueue_done(union ctl_io *io)
13648{
13649 struct ctl_softc *softc = CTL_SOFTC(io);
13650 struct ctl_thread *thr;
13651
13652 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13653 mtx_lock(&thr->queue_lock);
13654 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13655 mtx_unlock(&thr->queue_lock);
13656 wakeup(thr);
13657}
13658
13659static void
13660ctl_enqueue_isc(union ctl_io *io)
13661{
13662 struct ctl_softc *softc = CTL_SOFTC(io);
13663 struct ctl_thread *thr;
13664
13665 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13666 mtx_lock(&thr->queue_lock);
13667 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13668 mtx_unlock(&thr->queue_lock);
13669 wakeup(thr);
13670}
13671
13672/*
13673 * vim: ts=8
13674 */