1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
5 * Copyright (c) 2012 The FreeBSD Foundation
6 * Copyright (c) 2014-2017 Alexander Motin <mav@FreeBSD.org>
7 * Copyright (c) 2017 Jakub Wojciech Klama <jceel@FreeBSD.org>
8 * Copyright (c) 2018 Marcelo Araujo <araujo@FreeBSD.org>
9 * All rights reserved.
10 *
11 * Portions of this software were developed by Edward Tomasz Napierala
12 * under sponsorship from the FreeBSD Foundation.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions, and the following disclaimer,
19 * without modification.
20 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
21 * substantially similar to the "NO WARRANTY" disclaimer below
22 * ("Disclaimer") and any redistribution must be conditioned upon
23 * including a substantially similar Disclaimer requirement for further
24 * binary redistribution.
25 *
26 * NO WARRANTY
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
35 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
36 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGES.
38 *
39 * $Id$
40 */
41 /*
42 * CAM Target Layer, a SCSI device emulation subsystem.
43 *
44 * Author: Ken Merry <ken@FreeBSD.org>
45 */
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/ctype.h>
50 #include <sys/kernel.h>
51 #include <sys/types.h>
52 #include <sys/kthread.h>
53 #include <sys/bio.h>
54 #include <sys/fcntl.h>
55 #include <sys/lock.h>
56 #include <sys/module.h>
57 #include <sys/mutex.h>
58 #include <sys/condvar.h>
59 #include <sys/malloc.h>
60 #include <sys/conf.h>
61 #include <sys/ioccom.h>
62 #include <sys/queue.h>
63 #include <sys/sbuf.h>
64 #include <sys/smp.h>
65 #include <sys/endian.h>
66 #include <sys/proc.h>
67 #include <sys/sched.h>
68 #include <sys/sysctl.h>
69 #include <sys/nv.h>
70 #include <sys/dnv.h>
71 #include <vm/uma.h>
72
73 #include <cam/cam.h>
74 #include <cam/scsi/scsi_all.h>
75 #include <cam/scsi/scsi_cd.h>
76 #include <cam/scsi/scsi_da.h>
77 #include <cam/ctl/ctl_io.h>
78 #include <cam/ctl/ctl.h>
79 #include <cam/ctl/ctl_frontend.h>
80 #include <cam/ctl/ctl_util.h>
81 #include <cam/ctl/ctl_backend.h>
82 #include <cam/ctl/ctl_ioctl.h>
83 #include <cam/ctl/ctl_ha.h>
84 #include <cam/ctl/ctl_private.h>
85 #include <cam/ctl/ctl_debug.h>
86 #include <cam/ctl/ctl_nvme_all.h>
87 #include <cam/ctl/ctl_scsi_all.h>
88 #include <cam/ctl/ctl_error.h>
89
90 struct ctl_softc *control_softc = NULL;
91
92 /*
93 * Template mode pages.
94 */
95
96 /*
97 * Note that these are default values only. The actual values will be
98 * filled in when the user does a mode sense.
99 */
100 const static struct scsi_da_rw_recovery_page rw_er_page_default = {
101 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
102 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
103 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
104 /*read_retry_count*/0,
105 /*correction_span*/0,
106 /*head_offset_count*/0,
107 /*data_strobe_offset_cnt*/0,
108 /*byte8*/SMS_RWER_LBPERE,
109 /*write_retry_count*/0,
110 /*reserved2*/0,
111 /*recovery_time_limit*/{0, 0},
112 };
113
114 const static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
115 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
116 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
117 /*byte3*/SMS_RWER_PER,
118 /*read_retry_count*/0,
119 /*correction_span*/0,
120 /*head_offset_count*/0,
121 /*data_strobe_offset_cnt*/0,
122 /*byte8*/SMS_RWER_LBPERE,
123 /*write_retry_count*/0,
124 /*reserved2*/0,
125 /*recovery_time_limit*/{0, 0},
126 };
127
128 const static struct scsi_da_verify_recovery_page verify_er_page_default = {
129 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
130 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
131 /*byte3*/0,
132 /*read_retry_count*/0,
133 /*reserved*/{ 0, 0, 0, 0, 0, 0 },
134 /*recovery_time_limit*/{0, 0},
135 };
136
137 const static struct scsi_da_verify_recovery_page verify_er_page_changeable = {
138 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
139 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
140 /*byte3*/SMS_VER_PER,
141 /*read_retry_count*/0,
142 /*reserved*/{ 0, 0, 0, 0, 0, 0 },
143 /*recovery_time_limit*/{0, 0},
144 };
145
146 const static struct scsi_caching_page caching_page_default = {
147 /*page_code*/SMS_CACHING_PAGE,
148 /*page_length*/sizeof(struct scsi_caching_page) - 2,
149 /*flags1*/ SCP_DISC | SCP_WCE,
150 /*ret_priority*/ 0,
151 /*disable_pf_transfer_len*/ {0xff, 0xff},
152 /*min_prefetch*/ {0, 0},
153 /*max_prefetch*/ {0xff, 0xff},
154 /*max_pf_ceiling*/ {0xff, 0xff},
155 /*flags2*/ 0,
156 /*cache_segments*/ 0,
157 /*cache_seg_size*/ {0, 0},
158 /*reserved*/ 0,
159 /*non_cache_seg_size*/ {0, 0, 0}
160 };
161
162 const static struct scsi_caching_page caching_page_changeable = {
163 /*page_code*/SMS_CACHING_PAGE,
164 /*page_length*/sizeof(struct scsi_caching_page) - 2,
165 /*flags1*/ SCP_WCE | SCP_RCD,
166 /*ret_priority*/ 0,
167 /*disable_pf_transfer_len*/ {0, 0},
168 /*min_prefetch*/ {0, 0},
169 /*max_prefetch*/ {0, 0},
170 /*max_pf_ceiling*/ {0, 0},
171 /*flags2*/ 0,
172 /*cache_segments*/ 0,
173 /*cache_seg_size*/ {0, 0},
174 /*reserved*/ 0,
175 /*non_cache_seg_size*/ {0, 0, 0}
176 };
177
178 const static struct scsi_control_page control_page_default = {
179 /*page_code*/SMS_CONTROL_MODE_PAGE,
180 /*page_length*/sizeof(struct scsi_control_page) - 2,
181 /*rlec*/0,
182 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
183 /*eca_and_aen*/0,
184 /*flags4*/SCP_TAS,
185 /*aen_holdoff_period*/{0, 0},
186 /*busy_timeout_period*/{0, 0},
187 /*extended_selftest_completion_time*/{0, 0}
188 };
189
190 const static struct scsi_control_page control_page_changeable = {
191 /*page_code*/SMS_CONTROL_MODE_PAGE,
192 /*page_length*/sizeof(struct scsi_control_page) - 2,
193 /*rlec*/SCP_DSENSE,
194 /*queue_flags*/SCP_QUEUE_ALG_MASK | SCP_NUAR,
195 /*eca_and_aen*/SCP_SWP,
196 /*flags4*/0,
197 /*aen_holdoff_period*/{0, 0},
198 /*busy_timeout_period*/{0, 0},
199 /*extended_selftest_completion_time*/{0, 0}
200 };
201
202 #define CTL_CEM_LEN (sizeof(struct scsi_control_ext_page) - 4)
203
204 const static struct scsi_control_ext_page control_ext_page_default = {
205 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
206 /*subpage_code*/0x01,
207 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
208 /*flags*/0,
209 /*prio*/0,
210 /*max_sense*/0
211 };
212
213 const static struct scsi_control_ext_page control_ext_page_changeable = {
214 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
215 /*subpage_code*/0x01,
216 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
217 /*flags*/0,
218 /*prio*/0,
219 /*max_sense*/0xff
220 };
221
222 const static struct scsi_info_exceptions_page ie_page_default = {
223 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
224 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
225 /*info_flags*/SIEP_FLAGS_EWASC,
226 /*mrie*/SIEP_MRIE_NO,
227 /*interval_timer*/{0, 0, 0, 0},
228 /*report_count*/{0, 0, 0, 1}
229 };
230
231 const static struct scsi_info_exceptions_page ie_page_changeable = {
232 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
233 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
234 /*info_flags*/SIEP_FLAGS_EWASC | SIEP_FLAGS_DEXCPT | SIEP_FLAGS_TEST |
235 SIEP_FLAGS_LOGERR,
236 /*mrie*/0x0f,
237 /*interval_timer*/{0xff, 0xff, 0xff, 0xff},
238 /*report_count*/{0xff, 0xff, 0xff, 0xff}
239 };
240
241 #define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4)
242
243 const static struct ctl_logical_block_provisioning_page lbp_page_default = {{
244 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
245 /*subpage_code*/0x02,
246 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
247 /*flags*/0,
248 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
249 /*descr*/{}},
250 {{/*flags*/0,
251 /*resource*/0x01,
252 /*reserved*/{0, 0},
253 /*count*/{0, 0, 0, 0}},
254 {/*flags*/0,
255 /*resource*/0x02,
256 /*reserved*/{0, 0},
257 /*count*/{0, 0, 0, 0}},
258 {/*flags*/0,
259 /*resource*/0xf1,
260 /*reserved*/{0, 0},
261 /*count*/{0, 0, 0, 0}},
262 {/*flags*/0,
263 /*resource*/0xf2,
264 /*reserved*/{0, 0},
265 /*count*/{0, 0, 0, 0}}
266 }
267 };
268
269 const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{
270 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
271 /*subpage_code*/0x02,
272 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
273 /*flags*/SLBPP_SITUA,
274 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
275 /*descr*/{}},
276 {{/*flags*/0,
277 /*resource*/0,
278 /*reserved*/{0, 0},
279 /*count*/{0, 0, 0, 0}},
280 {/*flags*/0,
281 /*resource*/0,
282 /*reserved*/{0, 0},
283 /*count*/{0, 0, 0, 0}},
284 {/*flags*/0,
285 /*resource*/0,
286 /*reserved*/{0, 0},
287 /*count*/{0, 0, 0, 0}},
288 {/*flags*/0,
289 /*resource*/0,
290 /*reserved*/{0, 0},
291 /*count*/{0, 0, 0, 0}}
292 }
293 };
294
295 const static struct scsi_cddvd_capabilities_page cddvd_page_default = {
296 /*page_code*/SMS_CDDVD_CAPS_PAGE,
297 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
298 /*caps1*/0x3f,
299 /*caps2*/0x00,
300 /*caps3*/0xf0,
301 /*caps4*/0x00,
302 /*caps5*/0x29,
303 /*caps6*/0x00,
304 /*obsolete*/{0, 0},
305 /*nvol_levels*/{0, 0},
306 /*buffer_size*/{8, 0},
307 /*obsolete2*/{0, 0},
308 /*reserved*/0,
309 /*digital*/0,
310 /*obsolete3*/0,
311 /*copy_management*/0,
312 /*reserved2*/0,
313 /*rotation_control*/0,
314 /*cur_write_speed*/0,
315 /*num_speed_descr*/0,
316 };
317
318 const static struct scsi_cddvd_capabilities_page cddvd_page_changeable = {
319 /*page_code*/SMS_CDDVD_CAPS_PAGE,
320 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
321 /*caps1*/0,
322 /*caps2*/0,
323 /*caps3*/0,
324 /*caps4*/0,
325 /*caps5*/0,
326 /*caps6*/0,
327 /*obsolete*/{0, 0},
328 /*nvol_levels*/{0, 0},
329 /*buffer_size*/{0, 0},
330 /*obsolete2*/{0, 0},
331 /*reserved*/0,
332 /*digital*/0,
333 /*obsolete3*/0,
334 /*copy_management*/0,
335 /*reserved2*/0,
336 /*rotation_control*/0,
337 /*cur_write_speed*/0,
338 /*num_speed_descr*/0,
339 };
340
341 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
342 "CAM Target Layer");
343 static int worker_threads = -1;
344 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
345 &worker_threads, 1, "Number of worker threads");
346 static int ctl_debug = CTL_DEBUG_NONE;
347 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
348 &ctl_debug, 0, "Enabled debug flags");
349 static int ctl_lun_map_size = 1024;
350 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, lun_map_size, CTLFLAG_RWTUN,
351 &ctl_lun_map_size, 0, "Size of per-port LUN map (max LUN + 1)");
352 #ifdef CTL_TIME_IO
353 static int ctl_time_io_secs = CTL_TIME_IO_DEFAULT_SECS;
354 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, time_io_secs, CTLFLAG_RWTUN,
355 &ctl_time_io_secs, 0, "Log requests taking more seconds");
356 #endif
357
358 /*
359 * Maximum number of LUNs we support. MUST be a power of 2.
360 */
361 #define CTL_DEFAULT_MAX_LUNS 1024
362 static int ctl_max_luns = CTL_DEFAULT_MAX_LUNS;
363 TUNABLE_INT("kern.cam.ctl.max_luns", &ctl_max_luns);
364 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, max_luns, CTLFLAG_RDTUN,
365 &ctl_max_luns, CTL_DEFAULT_MAX_LUNS, "Maximum number of LUNs");
366
367 /*
368 * Maximum number of ports registered at one time.
369 */
370 #define CTL_DEFAULT_MAX_PORTS 1024
371 static int ctl_max_ports = CTL_DEFAULT_MAX_PORTS;
372 TUNABLE_INT("kern.cam.ctl.max_ports", &ctl_max_ports);
373 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, max_ports, CTLFLAG_RDTUN,
374 &ctl_max_ports, CTL_DEFAULT_MAX_LUNS, "Maximum number of ports");
375
376 /*
377 * Maximum number of initiators we support.
378 */
379 #define CTL_MAX_INITIATORS (CTL_MAX_INIT_PER_PORT * ctl_max_ports)
380
381 /*
382 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
383 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
384 * SCSI Ports (0x88), Third-party Copy (0x8F), SCSI Feature Sets (0x92),
385 * Block limits (0xB0), Block Device Characteristics (0xB1) and
386 * Logical Block Provisioning (0xB2)
387 */
388 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 11
389
390 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
391 int param);
392 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
393 static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest);
394 static int ctl_init(void);
395 static int ctl_shutdown(void);
396 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
397 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
398 static void ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
399 static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
400 struct ctl_ooa *ooa_hdr,
401 struct ctl_ooa_entry *kern_entries);
402 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
403 struct thread *td);
404 static int ctl_enable_lun(struct ctl_lun *lun);
405 static int ctl_disable_lun(struct ctl_lun *lun);
406 static int ctl_free_lun(struct ctl_lun *lun);
407
408 static int ctl_do_mode_select(union ctl_io *io);
409 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
410 uint64_t res_key, uint64_t sa_res_key,
411 uint8_t type, uint32_t residx,
412 struct ctl_scsiio *ctsio,
413 struct scsi_per_res_out *cdb,
414 struct scsi_per_res_out_parms* param);
415 static void ctl_pro_preempt_other(struct ctl_lun *lun,
416 union ctl_ha_msg *msg);
417 static void ctl_hndl_per_res_out_on_other_sc(union ctl_io *io);
418 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
419 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
420 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
421 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
422 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
423 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
424 int alloc_len);
425 static int ctl_inquiry_evpd_sfs(struct ctl_scsiio *ctsio, int alloc_len);
426 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
427 int alloc_len);
428 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
429 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
430 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
431 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
432 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
433 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2,
434 bool seq);
435 static ctl_action ctl_seq_check(union ctl_io *io1, union ctl_io *io2);
436 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
437 union ctl_io *pending_io, const uint8_t *serialize_row,
438 union ctl_io *ooa_io);
439 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
440 union ctl_io **starting_io);
441 static void ctl_try_unblock_io(struct ctl_lun *lun, union ctl_io *io,
442 bool skip);
443 static void ctl_try_unblock_others(struct ctl_lun *lun, union ctl_io *io,
444 bool skip);
445 static int ctl_scsiio_lun_check(struct ctl_lun *lun,
446 const struct ctl_cmd_entry *entry,
447 struct ctl_scsiio *ctsio);
448 static void ctl_failover_lun(union ctl_io *io);
449 static void ctl_scsiio_precheck(struct ctl_scsiio *ctsio);
450 static int ctl_scsiio(struct ctl_scsiio *ctsio);
451 static void ctl_nvmeio_precheck(struct ctl_nvmeio *ctnio);
452 static int ctl_nvmeio(struct ctl_nvmeio *ctnio);
453
454 static int ctl_target_reset(union ctl_io *io);
455 static void ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx,
456 ctl_ua_type ua_type);
457 static int ctl_lun_reset(union ctl_io *io);
458 static int ctl_abort_task(union ctl_io *io);
459 static int ctl_abort_task_set(union ctl_io *io);
460 static int ctl_query_task(union ctl_io *io, int task_set);
461 static void ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx,
462 ctl_ua_type ua_type);
463 static int ctl_i_t_nexus_reset(union ctl_io *io);
464 static int ctl_query_async_event(union ctl_io *io);
465 static void ctl_run_task(union ctl_io *io);
466 #ifdef CTL_IO_DELAY
467 static void ctl_datamove_timer_wakeup(void *arg);
468 static void ctl_done_timer_wakeup(void *arg);
469 #endif /* CTL_IO_DELAY */
470
471 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
472 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
473 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io, bool samethr);
474 static void ctl_datamove_remote_write(union ctl_io *io);
475 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io, bool samethr);
476 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
477 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
478 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
479 ctl_ha_dt_cb callback);
480 static void ctl_datamove_remote_read(union ctl_io *io);
481 static void ctl_datamove_remote(union ctl_io *io);
482 static void ctl_process_done(union ctl_io *io);
483 static void ctl_thresh_thread(void *arg);
484 static void ctl_work_thread(void *arg);
485 static void ctl_enqueue_incoming(union ctl_io *io);
486 static void ctl_enqueue_rtr(union ctl_io *io);
487 static void ctl_enqueue_done(union ctl_io *io);
488 static void ctl_enqueue_isc(union ctl_io *io);
489 static const struct ctl_cmd_entry *
490 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
491 static const struct ctl_cmd_entry *
492 ctl_validate_command(struct ctl_scsiio *ctsio);
493 static int ctl_cmd_applicable(uint8_t lun_type,
494 const struct ctl_cmd_entry *entry);
495 static int ctl_ha_init(void);
496 static int ctl_ha_shutdown(void);
497
498 static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx);
499 static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx);
500 static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx);
501 static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key);
502
503 /*
504 * Load the serialization table. This isn't very pretty, but is probably
505 * the easiest way to do it.
506 */
507 #include "ctl_ser_table.c"
508
509 /*
510 * We only need to define open, close and ioctl routines for this driver.
511 */
512 static struct cdevsw ctl_cdevsw = {
513 .d_version = D_VERSION,
514 .d_flags = 0,
515 .d_open = ctl_open,
516 .d_close = ctl_close,
517 .d_ioctl = ctl_ioctl,
518 .d_name = "ctl",
519 };
520
521 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
522
523 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
524
525 static moduledata_t ctl_moduledata = {
526 "ctl",
527 ctl_module_event_handler,
528 NULL
529 };
530
531 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
532 MODULE_VERSION(ctl, 1);
533
534 static void
ctl_be_move_done(union ctl_io * io,bool samethr)535 ctl_be_move_done(union ctl_io *io, bool samethr)
536 {
537 switch (io->io_hdr.io_type) {
538 case CTL_IO_SCSI:
539 io->scsiio.be_move_done(io, samethr);
540 break;
541 case CTL_IO_NVME:
542 case CTL_IO_NVME_ADMIN:
543 io->nvmeio.be_move_done(io, samethr);
544 break;
545 default:
546 __assert_unreachable();
547 }
548 }
549
550 static void
ctl_continue_io(union ctl_io * io)551 ctl_continue_io(union ctl_io *io)
552 {
553 switch (io->io_hdr.io_type) {
554 case CTL_IO_SCSI:
555 io->scsiio.io_cont(io);
556 break;
557 case CTL_IO_NVME:
558 case CTL_IO_NVME_ADMIN:
559 io->nvmeio.io_cont(io);
560 break;
561 default:
562 __assert_unreachable();
563 }
564 }
565
566 static struct ctl_frontend ha_frontend =
567 {
568 .name = "ha",
569 .init = ctl_ha_init,
570 .shutdown = ctl_ha_shutdown,
571 };
572
573 static int
ctl_ha_init(void)574 ctl_ha_init(void)
575 {
576 struct ctl_softc *softc = control_softc;
577
578 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
579 &softc->othersc_pool) != 0)
580 return (ENOMEM);
581 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
582 ctl_pool_free(softc->othersc_pool);
583 return (EIO);
584 }
585 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
586 != CTL_HA_STATUS_SUCCESS) {
587 ctl_ha_msg_destroy(softc);
588 ctl_pool_free(softc->othersc_pool);
589 return (EIO);
590 }
591 return (0);
592 };
593
594 static int
ctl_ha_shutdown(void)595 ctl_ha_shutdown(void)
596 {
597 struct ctl_softc *softc = control_softc;
598 struct ctl_port *port;
599
600 ctl_ha_msg_shutdown(softc);
601 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) != CTL_HA_STATUS_SUCCESS)
602 return (EIO);
603 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS)
604 return (EIO);
605 ctl_pool_free(softc->othersc_pool);
606 while ((port = STAILQ_FIRST(&ha_frontend.port_list)) != NULL) {
607 ctl_port_deregister(port);
608 free(port->port_name, M_CTL);
609 free(port, M_CTL);
610 }
611 return (0);
612 };
613
614 static void
ctl_ha_datamove(union ctl_io * io)615 ctl_ha_datamove(union ctl_io *io)
616 {
617 struct ctl_lun *lun = CTL_LUN(io);
618 struct ctl_sg_entry *sgl;
619 union ctl_ha_msg msg;
620 uint32_t sg_entries_sent;
621 int do_sg_copy, i, j;
622
623 CTL_IO_ASSERT(io, SCSI);
624
625 memset(&msg.dt, 0, sizeof(msg.dt));
626 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
627 msg.hdr.original_sc = io->io_hdr.remote_io;
628 msg.hdr.serializing_sc = io;
629 msg.hdr.nexus = io->io_hdr.nexus;
630 msg.hdr.status = io->io_hdr.status;
631 msg.dt.flags = io->io_hdr.flags;
632
633 /*
634 * We convert everything into a S/G list here. We can't
635 * pass by reference, only by value between controllers.
636 * So we can't pass a pointer to the S/G list, only as many
637 * S/G entries as we can fit in here. If it's possible for
638 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
639 * then we need to break this up into multiple transfers.
640 */
641 if (ctl_kern_sg_entries(io) == 0) {
642 msg.dt.kern_sg_entries = 1;
643 #if 0
644 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
645 msg.dt.sg_list[0].addr = ctl_kern_data_ptr(io);
646 } else {
647 /* XXX KDM use busdma here! */
648 msg.dt.sg_list[0].addr =
649 (void *)vtophys(ctl_kern_data_ptr(io));
650 }
651 #else
652 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
653 ("HA does not support BUS_ADDR"));
654 msg.dt.sg_list[0].addr = ctl_kern_data_ptr(io);
655 #endif
656 msg.dt.sg_list[0].len = ctl_kern_data_len(io);
657 do_sg_copy = 0;
658 } else {
659 msg.dt.kern_sg_entries = ctl_kern_sg_entries(io);
660 do_sg_copy = 1;
661 }
662
663 msg.dt.kern_data_len = ctl_kern_data_len(io);
664 msg.dt.kern_total_len = ctl_kern_total_len(io);
665 msg.dt.kern_data_resid = ctl_kern_data_resid(io);
666 msg.dt.kern_rel_offset = ctl_kern_rel_offset(io);
667 msg.dt.sg_sequence = 0;
668
669 /*
670 * Loop until we've sent all of the S/G entries. On the
671 * other end, we'll recompose these S/G entries into one
672 * contiguous list before processing.
673 */
674 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries;
675 msg.dt.sg_sequence++) {
676 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) /
677 sizeof(msg.dt.sg_list[0])),
678 msg.dt.kern_sg_entries - sg_entries_sent);
679 if (do_sg_copy != 0) {
680 sgl = (struct ctl_sg_entry *)ctl_kern_data_ptr(io);
681 for (i = sg_entries_sent, j = 0;
682 i < msg.dt.cur_sg_entries; i++, j++) {
683 #if 0
684 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
685 msg.dt.sg_list[j].addr = sgl[i].addr;
686 } else {
687 /* XXX KDM use busdma here! */
688 msg.dt.sg_list[j].addr =
689 (void *)vtophys(sgl[i].addr);
690 }
691 #else
692 KASSERT((io->io_hdr.flags &
693 CTL_FLAG_BUS_ADDR) == 0,
694 ("HA does not support BUS_ADDR"));
695 msg.dt.sg_list[j].addr = sgl[i].addr;
696 #endif
697 msg.dt.sg_list[j].len = sgl[i].len;
698 }
699 }
700
701 sg_entries_sent += msg.dt.cur_sg_entries;
702 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries);
703 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
704 sizeof(msg.dt) - sizeof(msg.dt.sg_list) +
705 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries,
706 M_WAITOK) > CTL_HA_STATUS_SUCCESS) {
707 io->io_hdr.port_status = 31341;
708 ctl_datamove_done(io, true);
709 return;
710 }
711 msg.dt.sent_sg_entries = sg_entries_sent;
712 }
713
714 /*
715 * Officially handover the request from us to peer.
716 * If failover has just happened, then we must return error.
717 * If failover happen just after, then it is not our problem.
718 */
719 if (lun)
720 mtx_lock(&lun->lun_lock);
721 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
722 if (lun)
723 mtx_unlock(&lun->lun_lock);
724 io->io_hdr.port_status = 31342;
725 ctl_datamove_done(io, true);
726 return;
727 }
728 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
729 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
730 if (lun)
731 mtx_unlock(&lun->lun_lock);
732 }
733
734 static void
ctl_ha_done(union ctl_io * io)735 ctl_ha_done(union ctl_io *io)
736 {
737 union ctl_ha_msg msg;
738
739 if (io->io_hdr.io_type == CTL_IO_SCSI) {
740 memset(&msg, 0, sizeof(msg));
741 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
742 msg.hdr.original_sc = io->io_hdr.remote_io;
743 msg.hdr.nexus = io->io_hdr.nexus;
744 msg.hdr.status = io->io_hdr.status;
745 msg.scsi.scsi_status = io->scsiio.scsi_status;
746 msg.scsi.tag_num = io->scsiio.tag_num;
747 msg.scsi.tag_type = io->scsiio.tag_type;
748 msg.scsi.sense_len = io->scsiio.sense_len;
749 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
750 io->scsiio.sense_len);
751 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
752 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
753 msg.scsi.sense_len, M_WAITOK);
754 }
755 ctl_free_io(io);
756 }
757
758 static void
ctl_isc_handler_finish_xfer(struct ctl_softc * ctl_softc,union ctl_ha_msg * msg_info)759 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
760 union ctl_ha_msg *msg_info)
761 {
762 struct ctl_scsiio *ctsio;
763
764 if (msg_info->hdr.original_sc == NULL) {
765 printf("%s: original_sc == NULL!\n", __func__);
766 /* XXX KDM now what? */
767 return;
768 }
769
770 ctsio = &msg_info->hdr.original_sc->scsiio;
771 ctsio->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
772 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
773 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
774 ctsio->io_hdr.status = msg_info->hdr.status;
775 ctsio->scsi_status = msg_info->scsi.scsi_status;
776 ctsio->sense_len = msg_info->scsi.sense_len;
777 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
778 msg_info->scsi.sense_len);
779 ctl_enqueue_isc((union ctl_io *)ctsio);
780 }
781
782 static void
ctl_isc_handler_finish_ser_only(struct ctl_softc * ctl_softc,union ctl_ha_msg * msg_info)783 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
784 union ctl_ha_msg *msg_info)
785 {
786 struct ctl_scsiio *ctsio;
787
788 if (msg_info->hdr.serializing_sc == NULL) {
789 printf("%s: serializing_sc == NULL!\n", __func__);
790 /* XXX KDM now what? */
791 return;
792 }
793
794 ctsio = &msg_info->hdr.serializing_sc->scsiio;
795 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
796 ctl_enqueue_isc((union ctl_io *)ctsio);
797 }
798
799 void
ctl_isc_announce_lun(struct ctl_lun * lun)800 ctl_isc_announce_lun(struct ctl_lun *lun)
801 {
802 struct ctl_softc *softc = lun->ctl_softc;
803 union ctl_ha_msg *msg;
804 struct ctl_ha_msg_lun_pr_key pr_key;
805 int i, k;
806
807 if (softc->ha_link != CTL_HA_LINK_ONLINE)
808 return;
809 mtx_lock(&lun->lun_lock);
810 i = sizeof(msg->lun);
811 if (lun->lun_devid)
812 i += lun->lun_devid->len;
813 i += sizeof(pr_key) * lun->pr_key_count;
814 alloc:
815 mtx_unlock(&lun->lun_lock);
816 msg = malloc(i, M_CTL, M_WAITOK);
817 mtx_lock(&lun->lun_lock);
818 k = sizeof(msg->lun);
819 if (lun->lun_devid)
820 k += lun->lun_devid->len;
821 k += sizeof(pr_key) * lun->pr_key_count;
822 if (i < k) {
823 free(msg, M_CTL);
824 i = k;
825 goto alloc;
826 }
827 bzero(&msg->lun, sizeof(msg->lun));
828 msg->hdr.msg_type = CTL_MSG_LUN_SYNC;
829 msg->hdr.nexus.targ_lun = lun->lun;
830 msg->hdr.nexus.targ_mapped_lun = lun->lun;
831 msg->lun.flags = lun->flags;
832 msg->lun.pr_generation = lun->pr_generation;
833 msg->lun.pr_res_idx = lun->pr_res_idx;
834 msg->lun.pr_res_type = lun->pr_res_type;
835 msg->lun.pr_key_count = lun->pr_key_count;
836 i = 0;
837 if (lun->lun_devid) {
838 msg->lun.lun_devid_len = lun->lun_devid->len;
839 memcpy(&msg->lun.data[i], lun->lun_devid->data,
840 msg->lun.lun_devid_len);
841 i += msg->lun.lun_devid_len;
842 }
843 for (k = 0; k < CTL_MAX_INITIATORS; k++) {
844 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0)
845 continue;
846 pr_key.pr_iid = k;
847 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key));
848 i += sizeof(pr_key);
849 }
850 mtx_unlock(&lun->lun_lock);
851 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->lun, sizeof(msg->lun) + i,
852 M_WAITOK);
853 free(msg, M_CTL);
854
855 if (lun->flags & CTL_LUN_PRIMARY_SC) {
856 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
857 ctl_isc_announce_mode(lun, -1,
858 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
859 lun->mode_pages.index[i].subpage);
860 }
861 }
862 }
863
864 void
ctl_isc_announce_port(struct ctl_port * port)865 ctl_isc_announce_port(struct ctl_port *port)
866 {
867 struct ctl_softc *softc = port->ctl_softc;
868 union ctl_ha_msg *msg;
869 int i;
870
871 if (port->targ_port < softc->port_min ||
872 port->targ_port >= softc->port_max ||
873 softc->ha_link != CTL_HA_LINK_ONLINE)
874 return;
875 i = sizeof(msg->port) + strlen(port->port_name) + 1;
876 if (port->lun_map)
877 i += port->lun_map_size * sizeof(uint32_t);
878 if (port->port_devid)
879 i += port->port_devid->len;
880 if (port->target_devid)
881 i += port->target_devid->len;
882 if (port->init_devid)
883 i += port->init_devid->len;
884 msg = malloc(i, M_CTL, M_WAITOK);
885 bzero(&msg->port, sizeof(msg->port));
886 msg->hdr.msg_type = CTL_MSG_PORT_SYNC;
887 msg->hdr.nexus.targ_port = port->targ_port;
888 msg->port.port_type = port->port_type;
889 msg->port.physical_port = port->physical_port;
890 msg->port.virtual_port = port->virtual_port;
891 msg->port.status = port->status;
892 i = 0;
893 msg->port.name_len = sprintf(&msg->port.data[i],
894 "%d:%s", softc->ha_id, port->port_name) + 1;
895 i += msg->port.name_len;
896 if (port->lun_map) {
897 msg->port.lun_map_len = port->lun_map_size * sizeof(uint32_t);
898 memcpy(&msg->port.data[i], port->lun_map,
899 msg->port.lun_map_len);
900 i += msg->port.lun_map_len;
901 }
902 if (port->port_devid) {
903 msg->port.port_devid_len = port->port_devid->len;
904 memcpy(&msg->port.data[i], port->port_devid->data,
905 msg->port.port_devid_len);
906 i += msg->port.port_devid_len;
907 }
908 if (port->target_devid) {
909 msg->port.target_devid_len = port->target_devid->len;
910 memcpy(&msg->port.data[i], port->target_devid->data,
911 msg->port.target_devid_len);
912 i += msg->port.target_devid_len;
913 }
914 if (port->init_devid) {
915 msg->port.init_devid_len = port->init_devid->len;
916 memcpy(&msg->port.data[i], port->init_devid->data,
917 msg->port.init_devid_len);
918 i += msg->port.init_devid_len;
919 }
920 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
921 M_WAITOK);
922 free(msg, M_CTL);
923 }
924
925 void
ctl_isc_announce_iid(struct ctl_port * port,int iid)926 ctl_isc_announce_iid(struct ctl_port *port, int iid)
927 {
928 struct ctl_softc *softc = port->ctl_softc;
929 union ctl_ha_msg *msg;
930 int i, l;
931
932 if (port->targ_port < softc->port_min ||
933 port->targ_port >= softc->port_max ||
934 softc->ha_link != CTL_HA_LINK_ONLINE)
935 return;
936 mtx_lock(&softc->ctl_lock);
937 i = sizeof(msg->iid);
938 l = 0;
939 if (port->wwpn_iid[iid].name)
940 l = strlen(port->wwpn_iid[iid].name) + 1;
941 i += l;
942 msg = malloc(i, M_CTL, M_NOWAIT);
943 if (msg == NULL) {
944 mtx_unlock(&softc->ctl_lock);
945 return;
946 }
947 bzero(&msg->iid, sizeof(msg->iid));
948 msg->hdr.msg_type = CTL_MSG_IID_SYNC;
949 msg->hdr.nexus.targ_port = port->targ_port;
950 msg->hdr.nexus.initid = iid;
951 msg->iid.in_use = port->wwpn_iid[iid].in_use;
952 msg->iid.name_len = l;
953 msg->iid.wwpn = port->wwpn_iid[iid].wwpn;
954 if (port->wwpn_iid[iid].name)
955 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l);
956 mtx_unlock(&softc->ctl_lock);
957 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT);
958 free(msg, M_CTL);
959 }
960
961 void
ctl_isc_announce_mode(struct ctl_lun * lun,uint32_t initidx,uint8_t page,uint8_t subpage)962 ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx,
963 uint8_t page, uint8_t subpage)
964 {
965 struct ctl_softc *softc = lun->ctl_softc;
966 union ctl_ha_msg *msg;
967 u_int i, l;
968
969 if (softc->ha_link != CTL_HA_LINK_ONLINE)
970 return;
971 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
972 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
973 page && lun->mode_pages.index[i].subpage == subpage)
974 break;
975 }
976 if (i == CTL_NUM_MODE_PAGES)
977 return;
978
979 /* Don't try to replicate pages not present on this device. */
980 if (lun->mode_pages.index[i].page_data == NULL)
981 return;
982
983 l = sizeof(msg->mode) + lun->mode_pages.index[i].page_len;
984 msg = malloc(l, M_CTL, M_WAITOK | M_ZERO);
985 msg->hdr.msg_type = CTL_MSG_MODE_SYNC;
986 msg->hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT;
987 msg->hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT;
988 msg->hdr.nexus.targ_lun = lun->lun;
989 msg->hdr.nexus.targ_mapped_lun = lun->lun;
990 msg->mode.page_code = page;
991 msg->mode.subpage = subpage;
992 msg->mode.page_len = lun->mode_pages.index[i].page_len;
993 memcpy(msg->mode.data, lun->mode_pages.index[i].page_data,
994 msg->mode.page_len);
995 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->mode, l, M_WAITOK);
996 free(msg, M_CTL);
997 }
998
999 static void
ctl_isc_ha_link_up(struct ctl_softc * softc)1000 ctl_isc_ha_link_up(struct ctl_softc *softc)
1001 {
1002 struct ctl_port *port;
1003 struct ctl_lun *lun;
1004 union ctl_ha_msg msg;
1005 int i;
1006
1007 /* Announce this node parameters to peer for validation. */
1008 msg.login.msg_type = CTL_MSG_LOGIN;
1009 msg.login.version = CTL_HA_VERSION;
1010 msg.login.ha_mode = softc->ha_mode;
1011 msg.login.ha_id = softc->ha_id;
1012 msg.login.max_luns = ctl_max_luns;
1013 msg.login.max_ports = ctl_max_ports;
1014 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT;
1015 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login),
1016 M_WAITOK);
1017
1018 STAILQ_FOREACH(port, &softc->port_list, links) {
1019 ctl_isc_announce_port(port);
1020 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1021 if (port->wwpn_iid[i].in_use)
1022 ctl_isc_announce_iid(port, i);
1023 }
1024 }
1025 STAILQ_FOREACH(lun, &softc->lun_list, links)
1026 ctl_isc_announce_lun(lun);
1027 }
1028
1029 static void
ctl_isc_ha_link_down(struct ctl_softc * softc)1030 ctl_isc_ha_link_down(struct ctl_softc *softc)
1031 {
1032 struct ctl_port *port;
1033 struct ctl_lun *lun;
1034 union ctl_io *io;
1035 int i;
1036
1037 mtx_lock(&softc->ctl_lock);
1038 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1039 mtx_lock(&lun->lun_lock);
1040 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) {
1041 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1042 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1043 }
1044 mtx_unlock(&lun->lun_lock);
1045
1046 mtx_unlock(&softc->ctl_lock);
1047 io = ctl_alloc_io(softc->othersc_pool);
1048 mtx_lock(&softc->ctl_lock);
1049 ctl_zero_io(io);
1050 io->io_hdr.msg_type = CTL_MSG_FAILOVER;
1051 io->io_hdr.nexus.targ_mapped_lun = lun->lun;
1052 ctl_enqueue_isc(io);
1053 }
1054
1055 STAILQ_FOREACH(port, &softc->port_list, links) {
1056 if (port->targ_port >= softc->port_min &&
1057 port->targ_port < softc->port_max)
1058 continue;
1059 port->status &= ~CTL_PORT_STATUS_ONLINE;
1060 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1061 port->wwpn_iid[i].in_use = 0;
1062 free(port->wwpn_iid[i].name, M_CTL);
1063 port->wwpn_iid[i].name = NULL;
1064 }
1065 }
1066 mtx_unlock(&softc->ctl_lock);
1067 }
1068
1069 static void
ctl_isc_ua(struct ctl_softc * softc,union ctl_ha_msg * msg,int len)1070 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1071 {
1072 struct ctl_lun *lun;
1073 uint32_t iid;
1074
1075 if (len < sizeof(msg->ua)) {
1076 printf("%s: Received truncated message %d < %zu\n",
1077 __func__, len, sizeof(msg->ua));
1078 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1079 return;
1080 }
1081
1082 mtx_lock(&softc->ctl_lock);
1083 if (msg->hdr.nexus.targ_mapped_lun >= ctl_max_luns ||
1084 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) == NULL) {
1085 mtx_unlock(&softc->ctl_lock);
1086 return;
1087 }
1088 mtx_lock(&lun->lun_lock);
1089 mtx_unlock(&softc->ctl_lock);
1090 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && msg->ua.ua_set)
1091 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8);
1092 iid = ctl_get_initindex(&msg->hdr.nexus);
1093 if (msg->ua.ua_all) {
1094 if (msg->ua.ua_set)
1095 ctl_est_ua_all(lun, iid, msg->ua.ua_type);
1096 else
1097 ctl_clr_ua_all(lun, iid, msg->ua.ua_type);
1098 } else {
1099 if (msg->ua.ua_set)
1100 ctl_est_ua(lun, iid, msg->ua.ua_type);
1101 else
1102 ctl_clr_ua(lun, iid, msg->ua.ua_type);
1103 }
1104 mtx_unlock(&lun->lun_lock);
1105 }
1106
1107 static void
ctl_isc_lun_sync(struct ctl_softc * softc,union ctl_ha_msg * msg,int len)1108 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1109 {
1110 struct ctl_lun *lun;
1111 struct ctl_ha_msg_lun_pr_key pr_key;
1112 int i, k;
1113 ctl_lun_flags oflags;
1114 uint32_t targ_lun;
1115
1116 if (len < offsetof(struct ctl_ha_msg_lun, data[0])) {
1117 printf("%s: Received truncated message %d < %zu\n",
1118 __func__, len, offsetof(struct ctl_ha_msg_lun, data[0]));
1119 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1120 return;
1121 }
1122 i = msg->lun.lun_devid_len + msg->lun.pr_key_count * sizeof(pr_key);
1123 if (len < offsetof(struct ctl_ha_msg_lun, data[i])) {
1124 printf("%s: Received truncated message data %d < %zu\n",
1125 __func__, len, offsetof(struct ctl_ha_msg_lun, data[i]));
1126 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1127 return;
1128 }
1129
1130 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1131 mtx_lock(&softc->ctl_lock);
1132 if (targ_lun >= ctl_max_luns ||
1133 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1134 mtx_unlock(&softc->ctl_lock);
1135 return;
1136 }
1137 mtx_lock(&lun->lun_lock);
1138 mtx_unlock(&softc->ctl_lock);
1139 if (lun->flags & CTL_LUN_DISABLED) {
1140 mtx_unlock(&lun->lun_lock);
1141 return;
1142 }
1143 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0;
1144 if (msg->lun.lun_devid_len != i || (i > 0 &&
1145 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) {
1146 mtx_unlock(&lun->lun_lock);
1147 printf("%s: Received conflicting HA LUN %d\n",
1148 __func__, targ_lun);
1149 return;
1150 } else {
1151 /* Record whether peer is primary. */
1152 oflags = lun->flags;
1153 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) &&
1154 (msg->lun.flags & CTL_LUN_DISABLED) == 0)
1155 lun->flags |= CTL_LUN_PEER_SC_PRIMARY;
1156 else
1157 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1158 if (oflags != lun->flags)
1159 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1160
1161 /* If peer is primary and we are not -- use data */
1162 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
1163 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) {
1164 lun->pr_generation = msg->lun.pr_generation;
1165 lun->pr_res_idx = msg->lun.pr_res_idx;
1166 lun->pr_res_type = msg->lun.pr_res_type;
1167 lun->pr_key_count = msg->lun.pr_key_count;
1168 for (k = 0; k < CTL_MAX_INITIATORS; k++)
1169 ctl_clr_prkey(lun, k);
1170 for (k = 0; k < msg->lun.pr_key_count; k++) {
1171 memcpy(&pr_key, &msg->lun.data[i],
1172 sizeof(pr_key));
1173 ctl_alloc_prkey(lun, pr_key.pr_iid);
1174 ctl_set_prkey(lun, pr_key.pr_iid,
1175 pr_key.pr_key);
1176 i += sizeof(pr_key);
1177 }
1178 }
1179
1180 mtx_unlock(&lun->lun_lock);
1181 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n",
1182 __func__, targ_lun,
1183 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ?
1184 "primary" : "secondary"));
1185
1186 /* If we are primary but peer doesn't know -- notify */
1187 if ((lun->flags & CTL_LUN_PRIMARY_SC) &&
1188 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0)
1189 ctl_isc_announce_lun(lun);
1190 }
1191 }
1192
1193 static void
ctl_isc_port_sync(struct ctl_softc * softc,union ctl_ha_msg * msg,int len)1194 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1195 {
1196 struct ctl_port *port;
1197 struct ctl_lun *lun;
1198 int i, new;
1199
1200 if (len < offsetof(struct ctl_ha_msg_port, data[0])) {
1201 printf("%s: Received truncated message %d < %zu\n",
1202 __func__, len, offsetof(struct ctl_ha_msg_port, data[0]));
1203 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1204 return;
1205 }
1206 i = msg->port.name_len + msg->port.lun_map_len +
1207 msg->port.port_devid_len + msg->port.target_devid_len +
1208 msg->port.init_devid_len;
1209 if (len < offsetof(struct ctl_ha_msg_port, data[i])) {
1210 printf("%s: Received truncated message data %d < %zu\n",
1211 __func__, len, offsetof(struct ctl_ha_msg_port, data[i]));
1212 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1213 return;
1214 }
1215
1216 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1217 if (port == NULL) {
1218 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__,
1219 msg->hdr.nexus.targ_port));
1220 new = 1;
1221 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO);
1222 port->frontend = &ha_frontend;
1223 port->targ_port = msg->hdr.nexus.targ_port;
1224 port->fe_datamove = ctl_ha_datamove;
1225 port->fe_done = ctl_ha_done;
1226 } else if (port->frontend == &ha_frontend) {
1227 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__,
1228 msg->hdr.nexus.targ_port));
1229 new = 0;
1230 } else {
1231 printf("%s: Received conflicting HA port %d\n",
1232 __func__, msg->hdr.nexus.targ_port);
1233 return;
1234 }
1235 port->port_type = msg->port.port_type;
1236 port->physical_port = msg->port.physical_port;
1237 port->virtual_port = msg->port.virtual_port;
1238 port->status = msg->port.status;
1239 i = 0;
1240 free(port->port_name, M_CTL);
1241 port->port_name = strndup(&msg->port.data[i], msg->port.name_len,
1242 M_CTL);
1243 i += msg->port.name_len;
1244 if (msg->port.lun_map_len != 0) {
1245 if (port->lun_map == NULL ||
1246 port->lun_map_size * sizeof(uint32_t) <
1247 msg->port.lun_map_len) {
1248 port->lun_map_size = 0;
1249 free(port->lun_map, M_CTL);
1250 port->lun_map = malloc(msg->port.lun_map_len,
1251 M_CTL, M_WAITOK);
1252 }
1253 memcpy(port->lun_map, &msg->port.data[i], msg->port.lun_map_len);
1254 port->lun_map_size = msg->port.lun_map_len / sizeof(uint32_t);
1255 i += msg->port.lun_map_len;
1256 } else {
1257 port->lun_map_size = 0;
1258 free(port->lun_map, M_CTL);
1259 port->lun_map = NULL;
1260 }
1261 if (msg->port.port_devid_len != 0) {
1262 if (port->port_devid == NULL ||
1263 port->port_devid->len < msg->port.port_devid_len) {
1264 free(port->port_devid, M_CTL);
1265 port->port_devid = malloc(sizeof(struct ctl_devid) +
1266 msg->port.port_devid_len, M_CTL, M_WAITOK);
1267 }
1268 memcpy(port->port_devid->data, &msg->port.data[i],
1269 msg->port.port_devid_len);
1270 port->port_devid->len = msg->port.port_devid_len;
1271 i += msg->port.port_devid_len;
1272 } else {
1273 free(port->port_devid, M_CTL);
1274 port->port_devid = NULL;
1275 }
1276 if (msg->port.target_devid_len != 0) {
1277 if (port->target_devid == NULL ||
1278 port->target_devid->len < msg->port.target_devid_len) {
1279 free(port->target_devid, M_CTL);
1280 port->target_devid = malloc(sizeof(struct ctl_devid) +
1281 msg->port.target_devid_len, M_CTL, M_WAITOK);
1282 }
1283 memcpy(port->target_devid->data, &msg->port.data[i],
1284 msg->port.target_devid_len);
1285 port->target_devid->len = msg->port.target_devid_len;
1286 i += msg->port.target_devid_len;
1287 } else {
1288 free(port->target_devid, M_CTL);
1289 port->target_devid = NULL;
1290 }
1291 if (msg->port.init_devid_len != 0) {
1292 if (port->init_devid == NULL ||
1293 port->init_devid->len < msg->port.init_devid_len) {
1294 free(port->init_devid, M_CTL);
1295 port->init_devid = malloc(sizeof(struct ctl_devid) +
1296 msg->port.init_devid_len, M_CTL, M_WAITOK);
1297 }
1298 memcpy(port->init_devid->data, &msg->port.data[i],
1299 msg->port.init_devid_len);
1300 port->init_devid->len = msg->port.init_devid_len;
1301 i += msg->port.init_devid_len;
1302 } else {
1303 free(port->init_devid, M_CTL);
1304 port->init_devid = NULL;
1305 }
1306 if (new) {
1307 if (ctl_port_register(port) != 0) {
1308 printf("%s: ctl_port_register() failed with error\n",
1309 __func__);
1310 }
1311 }
1312 mtx_lock(&softc->ctl_lock);
1313 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1314 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
1315 continue;
1316 mtx_lock(&lun->lun_lock);
1317 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
1318 mtx_unlock(&lun->lun_lock);
1319 }
1320 mtx_unlock(&softc->ctl_lock);
1321 }
1322
1323 static void
ctl_isc_iid_sync(struct ctl_softc * softc,union ctl_ha_msg * msg,int len)1324 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1325 {
1326 struct ctl_port *port;
1327 int i, iid;
1328
1329 if (len < offsetof(struct ctl_ha_msg_iid, data[0])) {
1330 printf("%s: Received truncated message %d < %zu\n",
1331 __func__, len, offsetof(struct ctl_ha_msg_iid, data[0]));
1332 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1333 return;
1334 }
1335 i = msg->iid.name_len;
1336 if (len < offsetof(struct ctl_ha_msg_iid, data[i])) {
1337 printf("%s: Received truncated message data %d < %zu\n",
1338 __func__, len, offsetof(struct ctl_ha_msg_iid, data[i]));
1339 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1340 return;
1341 }
1342
1343 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1344 if (port == NULL) {
1345 printf("%s: Received IID for unknown port %d\n",
1346 __func__, msg->hdr.nexus.targ_port);
1347 return;
1348 }
1349 iid = msg->hdr.nexus.initid;
1350 if (port->wwpn_iid[iid].in_use != 0 &&
1351 msg->iid.in_use == 0)
1352 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON);
1353 port->wwpn_iid[iid].in_use = msg->iid.in_use;
1354 port->wwpn_iid[iid].wwpn = msg->iid.wwpn;
1355 free(port->wwpn_iid[iid].name, M_CTL);
1356 if (msg->iid.name_len) {
1357 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0],
1358 msg->iid.name_len, M_CTL);
1359 } else
1360 port->wwpn_iid[iid].name = NULL;
1361 }
1362
1363 static void
ctl_isc_login(struct ctl_softc * softc,union ctl_ha_msg * msg,int len)1364 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1365 {
1366
1367 if (len < sizeof(msg->login)) {
1368 printf("%s: Received truncated message %d < %zu\n",
1369 __func__, len, sizeof(msg->login));
1370 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1371 return;
1372 }
1373
1374 if (msg->login.version != CTL_HA_VERSION) {
1375 printf("CTL HA peers have different versions %d != %d\n",
1376 msg->login.version, CTL_HA_VERSION);
1377 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1378 return;
1379 }
1380 if (msg->login.ha_mode != softc->ha_mode) {
1381 printf("CTL HA peers have different ha_mode %d != %d\n",
1382 msg->login.ha_mode, softc->ha_mode);
1383 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1384 return;
1385 }
1386 if (msg->login.ha_id == softc->ha_id) {
1387 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id);
1388 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1389 return;
1390 }
1391 if (msg->login.max_luns != ctl_max_luns ||
1392 msg->login.max_ports != ctl_max_ports ||
1393 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) {
1394 printf("CTL HA peers have different limits\n");
1395 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1396 return;
1397 }
1398 }
1399
1400 static void
ctl_isc_mode_sync(struct ctl_softc * softc,union ctl_ha_msg * msg,int len)1401 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1402 {
1403 struct ctl_lun *lun;
1404 u_int i;
1405 uint32_t initidx, targ_lun;
1406
1407 if (len < offsetof(struct ctl_ha_msg_mode, data[0])) {
1408 printf("%s: Received truncated message %d < %zu\n",
1409 __func__, len, offsetof(struct ctl_ha_msg_mode, data[0]));
1410 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1411 return;
1412 }
1413 i = msg->mode.page_len;
1414 if (len < offsetof(struct ctl_ha_msg_mode, data[i])) {
1415 printf("%s: Received truncated message data %d < %zu\n",
1416 __func__, len, offsetof(struct ctl_ha_msg_mode, data[i]));
1417 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1418 return;
1419 }
1420
1421 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1422 mtx_lock(&softc->ctl_lock);
1423 if (targ_lun >= ctl_max_luns ||
1424 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1425 mtx_unlock(&softc->ctl_lock);
1426 return;
1427 }
1428 mtx_lock(&lun->lun_lock);
1429 mtx_unlock(&softc->ctl_lock);
1430 if (lun->flags & CTL_LUN_DISABLED) {
1431 mtx_unlock(&lun->lun_lock);
1432 return;
1433 }
1434 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
1435 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
1436 msg->mode.page_code &&
1437 lun->mode_pages.index[i].subpage == msg->mode.subpage)
1438 break;
1439 }
1440 if (i == CTL_NUM_MODE_PAGES) {
1441 mtx_unlock(&lun->lun_lock);
1442 return;
1443 }
1444 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data,
1445 min(lun->mode_pages.index[i].page_len, msg->mode.page_len));
1446 initidx = ctl_get_initindex(&msg->hdr.nexus);
1447 if (initidx != -1)
1448 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
1449 mtx_unlock(&lun->lun_lock);
1450 }
1451
1452 /*
1453 * ISC (Inter Shelf Communication) event handler. Events from the HA
1454 * subsystem come in here.
1455 */
1456 static void
ctl_isc_event_handler(ctl_ha_channel channel,ctl_ha_event event,int param)1457 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
1458 {
1459 struct ctl_softc *softc = control_softc;
1460 union ctl_io *io;
1461 struct ctl_prio *presio;
1462 ctl_ha_status isc_status;
1463
1464 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event));
1465 if (event == CTL_HA_EVT_MSG_RECV) {
1466 union ctl_ha_msg *msg, msgbuf;
1467
1468 if (param > sizeof(msgbuf))
1469 msg = malloc(param, M_CTL, M_WAITOK);
1470 else
1471 msg = &msgbuf;
1472 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param,
1473 M_WAITOK);
1474 if (isc_status != CTL_HA_STATUS_SUCCESS) {
1475 printf("%s: Error receiving message: %d\n",
1476 __func__, isc_status);
1477 if (msg != &msgbuf)
1478 free(msg, M_CTL);
1479 return;
1480 }
1481
1482 CTL_DEBUG_PRINT(("CTL: msg_type %d len %d\n",
1483 msg->hdr.msg_type, param));
1484 switch (msg->hdr.msg_type) {
1485 case CTL_MSG_SERIALIZE:
1486 io = ctl_alloc_io(softc->othersc_pool);
1487 ctl_zero_io(io);
1488 // populate ctsio from msg
1489 io->io_hdr.io_type = CTL_IO_SCSI;
1490 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
1491 io->io_hdr.remote_io = msg->hdr.original_sc;
1492 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
1493 CTL_FLAG_IO_ACTIVE;
1494 /*
1495 * If we're in serialization-only mode, we don't
1496 * want to go through full done processing. Thus
1497 * the COPY flag.
1498 *
1499 * XXX KDM add another flag that is more specific.
1500 */
1501 if (softc->ha_mode != CTL_HA_MODE_XFER)
1502 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
1503 io->io_hdr.nexus = msg->hdr.nexus;
1504 io->scsiio.priority = msg->scsi.priority;
1505 io->scsiio.tag_num = msg->scsi.tag_num;
1506 io->scsiio.tag_type = msg->scsi.tag_type;
1507 #ifdef CTL_TIME_IO
1508 io->io_hdr.start_time = time_uptime;
1509 getbinuptime(&io->io_hdr.start_bt);
1510 #endif /* CTL_TIME_IO */
1511 io->scsiio.cdb_len = msg->scsi.cdb_len;
1512 memcpy(io->scsiio.cdb, msg->scsi.cdb,
1513 CTL_MAX_CDBLEN);
1514 if (softc->ha_mode == CTL_HA_MODE_XFER) {
1515 const struct ctl_cmd_entry *entry;
1516
1517 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
1518 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
1519 io->io_hdr.flags |=
1520 entry->flags & CTL_FLAG_DATA_MASK;
1521 }
1522 ctl_enqueue_isc(io);
1523 break;
1524
1525 /* Performed on the Originating SC, XFER mode only */
1526 case CTL_MSG_DATAMOVE: {
1527 struct ctl_sg_entry *sgl;
1528 int i, j;
1529
1530 io = msg->hdr.original_sc;
1531 if (io == NULL) {
1532 printf("%s: original_sc == NULL!\n", __func__);
1533 /* XXX KDM do something here */
1534 break;
1535 }
1536 CTL_IO_ASSERT(io, SCSI);
1537
1538 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
1539 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1540 /*
1541 * Keep track of this, we need to send it back over
1542 * when the datamove is complete.
1543 */
1544 io->io_hdr.remote_io = msg->hdr.serializing_sc;
1545 if (msg->hdr.status == CTL_SUCCESS)
1546 io->io_hdr.status = msg->hdr.status;
1547
1548 if (msg->dt.sg_sequence == 0) {
1549 #ifdef CTL_TIME_IO
1550 getbinuptime(&io->io_hdr.dma_start_bt);
1551 #endif
1552 i = msg->dt.kern_sg_entries +
1553 msg->dt.kern_data_len /
1554 CTL_HA_DATAMOVE_SEGMENT + 1;
1555 sgl = malloc(sizeof(*sgl) * i, M_CTL,
1556 M_WAITOK | M_ZERO);
1557 CTL_RSGL(io) = sgl;
1558 CTL_LSGL(io) = &sgl[msg->dt.kern_sg_entries];
1559
1560 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
1561
1562 io->scsiio.kern_sg_entries =
1563 msg->dt.kern_sg_entries;
1564 io->scsiio.rem_sg_entries =
1565 msg->dt.kern_sg_entries;
1566 io->scsiio.kern_data_len =
1567 msg->dt.kern_data_len;
1568 io->scsiio.kern_total_len =
1569 msg->dt.kern_total_len;
1570 io->scsiio.kern_data_resid =
1571 msg->dt.kern_data_resid;
1572 io->scsiio.kern_rel_offset =
1573 msg->dt.kern_rel_offset;
1574 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR;
1575 io->io_hdr.flags |= msg->dt.flags &
1576 CTL_FLAG_BUS_ADDR;
1577 } else
1578 sgl = (struct ctl_sg_entry *)
1579 io->scsiio.kern_data_ptr;
1580
1581 for (i = msg->dt.sent_sg_entries, j = 0;
1582 i < (msg->dt.sent_sg_entries +
1583 msg->dt.cur_sg_entries); i++, j++) {
1584 sgl[i].addr = msg->dt.sg_list[j].addr;
1585 sgl[i].len = msg->dt.sg_list[j].len;
1586 }
1587
1588 /*
1589 * If this is the last piece of the I/O, we've got
1590 * the full S/G list. Queue processing in the thread.
1591 * Otherwise wait for the next piece.
1592 */
1593 if (msg->dt.sg_last != 0)
1594 ctl_enqueue_isc(io);
1595 break;
1596 }
1597 /* Performed on the Serializing (primary) SC, XFER mode only */
1598 case CTL_MSG_DATAMOVE_DONE: {
1599 if (msg->hdr.serializing_sc == NULL) {
1600 printf("%s: serializing_sc == NULL!\n",
1601 __func__);
1602 /* XXX KDM now what? */
1603 break;
1604 }
1605 /*
1606 * We grab the sense information here in case
1607 * there was a failure, so we can return status
1608 * back to the initiator.
1609 */
1610 io = msg->hdr.serializing_sc;
1611 CTL_IO_ASSERT(io, SCSI);
1612
1613 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
1614 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1615 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1616 io->io_hdr.port_status = msg->scsi.port_status;
1617 io->scsiio.kern_data_resid = msg->scsi.kern_data_resid;
1618 if (msg->hdr.status != CTL_STATUS_NONE) {
1619 io->io_hdr.status = msg->hdr.status;
1620 io->scsiio.scsi_status = msg->scsi.scsi_status;
1621 io->scsiio.sense_len = msg->scsi.sense_len;
1622 memcpy(&io->scsiio.sense_data,
1623 &msg->scsi.sense_data,
1624 msg->scsi.sense_len);
1625 if (msg->hdr.status == CTL_SUCCESS)
1626 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1627 }
1628 ctl_enqueue_isc(io);
1629 break;
1630 }
1631
1632 /* Preformed on Originating SC, SER_ONLY mode */
1633 case CTL_MSG_R2R:
1634 io = msg->hdr.original_sc;
1635 if (io == NULL) {
1636 printf("%s: original_sc == NULL!\n",
1637 __func__);
1638 break;
1639 }
1640 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1641 io->io_hdr.msg_type = CTL_MSG_R2R;
1642 io->io_hdr.remote_io = msg->hdr.serializing_sc;
1643 ctl_enqueue_isc(io);
1644 break;
1645
1646 /*
1647 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
1648 * mode.
1649 * Performed on the Originating (i.e. secondary) SC in XFER
1650 * mode
1651 */
1652 case CTL_MSG_FINISH_IO:
1653 if (softc->ha_mode == CTL_HA_MODE_XFER)
1654 ctl_isc_handler_finish_xfer(softc, msg);
1655 else
1656 ctl_isc_handler_finish_ser_only(softc, msg);
1657 break;
1658
1659 /* Preformed on Originating SC */
1660 case CTL_MSG_BAD_JUJU:
1661 io = msg->hdr.original_sc;
1662 if (io == NULL) {
1663 printf("%s: Bad JUJU!, original_sc is NULL!\n",
1664 __func__);
1665 break;
1666 }
1667 ctl_copy_sense_data(msg, io);
1668 /*
1669 * IO should have already been cleaned up on other
1670 * SC so clear this flag so we won't send a message
1671 * back to finish the IO there.
1672 */
1673 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1674 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1675
1676 /* io = msg->hdr.serializing_sc; */
1677 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
1678 ctl_enqueue_isc(io);
1679 break;
1680
1681 /* Handle resets sent from the other side */
1682 case CTL_MSG_MANAGE_TASKS: {
1683 struct ctl_taskio *taskio;
1684 taskio = (struct ctl_taskio *)ctl_alloc_io(
1685 softc->othersc_pool);
1686 ctl_zero_io((union ctl_io *)taskio);
1687 taskio->io_hdr.io_type = CTL_IO_TASK;
1688 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1689 taskio->io_hdr.nexus = msg->hdr.nexus;
1690 taskio->task_action = msg->task.task_action;
1691 taskio->tag_num = msg->task.tag_num;
1692 taskio->tag_type = msg->task.tag_type;
1693 #ifdef CTL_TIME_IO
1694 taskio->io_hdr.start_time = time_uptime;
1695 getbinuptime(&taskio->io_hdr.start_bt);
1696 #endif /* CTL_TIME_IO */
1697 ctl_run_task((union ctl_io *)taskio);
1698 break;
1699 }
1700 /* Persistent Reserve action which needs attention */
1701 case CTL_MSG_PERS_ACTION:
1702 presio = (struct ctl_prio *)ctl_alloc_io(
1703 softc->othersc_pool);
1704 ctl_zero_io((union ctl_io *)presio);
1705 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
1706 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1707 presio->io_hdr.nexus = msg->hdr.nexus;
1708 presio->pr_msg = msg->pr;
1709 ctl_enqueue_isc((union ctl_io *)presio);
1710 break;
1711 case CTL_MSG_UA:
1712 ctl_isc_ua(softc, msg, param);
1713 break;
1714 case CTL_MSG_PORT_SYNC:
1715 ctl_isc_port_sync(softc, msg, param);
1716 break;
1717 case CTL_MSG_LUN_SYNC:
1718 ctl_isc_lun_sync(softc, msg, param);
1719 break;
1720 case CTL_MSG_IID_SYNC:
1721 ctl_isc_iid_sync(softc, msg, param);
1722 break;
1723 case CTL_MSG_LOGIN:
1724 ctl_isc_login(softc, msg, param);
1725 break;
1726 case CTL_MSG_MODE_SYNC:
1727 ctl_isc_mode_sync(softc, msg, param);
1728 break;
1729 default:
1730 printf("Received HA message of unknown type %d\n",
1731 msg->hdr.msg_type);
1732 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1733 break;
1734 }
1735 if (msg != &msgbuf)
1736 free(msg, M_CTL);
1737 } else if (event == CTL_HA_EVT_LINK_CHANGE) {
1738 printf("CTL: HA link status changed from %d to %d\n",
1739 softc->ha_link, param);
1740 if (param == softc->ha_link)
1741 return;
1742 if (softc->ha_link == CTL_HA_LINK_ONLINE) {
1743 softc->ha_link = param;
1744 ctl_isc_ha_link_down(softc);
1745 } else {
1746 softc->ha_link = param;
1747 if (softc->ha_link == CTL_HA_LINK_ONLINE)
1748 ctl_isc_ha_link_up(softc);
1749 }
1750 return;
1751 } else {
1752 printf("ctl_isc_event_handler: Unknown event %d\n", event);
1753 return;
1754 }
1755 }
1756
1757 static void
ctl_copy_sense_data(union ctl_ha_msg * src,union ctl_io * dest)1758 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
1759 {
1760
1761 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data,
1762 src->scsi.sense_len);
1763 dest->scsiio.scsi_status = src->scsi.scsi_status;
1764 dest->scsiio.sense_len = src->scsi.sense_len;
1765 dest->io_hdr.status = src->hdr.status;
1766 }
1767
1768 static void
ctl_copy_sense_data_back(union ctl_io * src,union ctl_ha_msg * dest)1769 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest)
1770 {
1771
1772 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data,
1773 src->scsiio.sense_len);
1774 dest->scsi.scsi_status = src->scsiio.scsi_status;
1775 dest->scsi.sense_len = src->scsiio.sense_len;
1776 dest->hdr.status = src->io_hdr.status;
1777 }
1778
1779 void
ctl_est_ua(struct ctl_lun * lun,uint32_t initidx,ctl_ua_type ua)1780 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1781 {
1782 struct ctl_softc *softc = lun->ctl_softc;
1783 ctl_ua_type *pu;
1784
1785 if (initidx < softc->init_min || initidx >= softc->init_max)
1786 return;
1787 mtx_assert(&lun->lun_lock, MA_OWNED);
1788 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1789 if (pu == NULL)
1790 return;
1791 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
1792 }
1793
1794 void
ctl_est_ua_port(struct ctl_lun * lun,int port,uint32_t except,ctl_ua_type ua)1795 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua)
1796 {
1797 int i;
1798
1799 mtx_assert(&lun->lun_lock, MA_OWNED);
1800 if (lun->pending_ua[port] == NULL)
1801 return;
1802 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1803 if (port * CTL_MAX_INIT_PER_PORT + i == except)
1804 continue;
1805 lun->pending_ua[port][i] |= ua;
1806 }
1807 }
1808
1809 void
ctl_est_ua_all(struct ctl_lun * lun,uint32_t except,ctl_ua_type ua)1810 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1811 {
1812 struct ctl_softc *softc = lun->ctl_softc;
1813 int i;
1814
1815 mtx_assert(&lun->lun_lock, MA_OWNED);
1816 for (i = softc->port_min; i < softc->port_max; i++)
1817 ctl_est_ua_port(lun, i, except, ua);
1818 }
1819
1820 void
ctl_clr_ua(struct ctl_lun * lun,uint32_t initidx,ctl_ua_type ua)1821 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1822 {
1823 struct ctl_softc *softc = lun->ctl_softc;
1824 ctl_ua_type *pu;
1825
1826 if (initidx < softc->init_min || initidx >= softc->init_max)
1827 return;
1828 mtx_assert(&lun->lun_lock, MA_OWNED);
1829 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1830 if (pu == NULL)
1831 return;
1832 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1833 }
1834
1835 void
ctl_clr_ua_all(struct ctl_lun * lun,uint32_t except,ctl_ua_type ua)1836 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1837 {
1838 struct ctl_softc *softc = lun->ctl_softc;
1839 int i, j;
1840
1841 mtx_assert(&lun->lun_lock, MA_OWNED);
1842 for (i = softc->port_min; i < softc->port_max; i++) {
1843 if (lun->pending_ua[i] == NULL)
1844 continue;
1845 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1846 if (i * CTL_MAX_INIT_PER_PORT + j == except)
1847 continue;
1848 lun->pending_ua[i][j] &= ~ua;
1849 }
1850 }
1851 }
1852
1853 void
ctl_clr_ua_allluns(struct ctl_softc * ctl_softc,uint32_t initidx,ctl_ua_type ua_type)1854 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
1855 ctl_ua_type ua_type)
1856 {
1857 struct ctl_lun *lun;
1858
1859 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
1860 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
1861 mtx_lock(&lun->lun_lock);
1862 ctl_clr_ua(lun, initidx, ua_type);
1863 mtx_unlock(&lun->lun_lock);
1864 }
1865 }
1866
1867 static int
ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)1868 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)
1869 {
1870 struct ctl_softc *softc = (struct ctl_softc *)arg1;
1871 struct ctl_lun *lun;
1872 struct ctl_lun_req ireq;
1873 int error, value;
1874
1875 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1;
1876 error = sysctl_handle_int(oidp, &value, 0, req);
1877 if ((error != 0) || (req->newptr == NULL))
1878 return (error);
1879
1880 mtx_lock(&softc->ctl_lock);
1881 if (value == 0)
1882 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1883 else
1884 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1885 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1886 mtx_unlock(&softc->ctl_lock);
1887 bzero(&ireq, sizeof(ireq));
1888 ireq.reqtype = CTL_LUNREQ_MODIFY;
1889 ireq.reqdata.modify.lun_id = lun->lun;
1890 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0,
1891 curthread);
1892 if (ireq.status != CTL_LUN_OK) {
1893 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n",
1894 __func__, ireq.status, ireq.error_str);
1895 }
1896 mtx_lock(&softc->ctl_lock);
1897 }
1898 mtx_unlock(&softc->ctl_lock);
1899 return (0);
1900 }
1901
1902 static int
ctl_init(void)1903 ctl_init(void)
1904 {
1905 struct make_dev_args args;
1906 struct ctl_softc *softc;
1907 int i, error;
1908
1909 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1910 M_WAITOK | M_ZERO);
1911
1912 make_dev_args_init(&args);
1913 args.mda_devsw = &ctl_cdevsw;
1914 args.mda_uid = UID_ROOT;
1915 args.mda_gid = GID_OPERATOR;
1916 args.mda_mode = 0600;
1917 args.mda_si_drv1 = softc;
1918 args.mda_si_drv2 = NULL;
1919 error = make_dev_s(&args, &softc->dev, "cam/ctl");
1920 if (error != 0) {
1921 free(softc, M_DEVBUF);
1922 control_softc = NULL;
1923 return (error);
1924 }
1925
1926 sysctl_ctx_init(&softc->sysctl_ctx);
1927 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1928 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1929 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "CAM Target Layer");
1930
1931 if (softc->sysctl_tree == NULL) {
1932 printf("%s: unable to allocate sysctl tree\n", __func__);
1933 destroy_dev(softc->dev);
1934 free(softc, M_DEVBUF);
1935 control_softc = NULL;
1936 return (ENOMEM);
1937 }
1938
1939 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1940 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1941 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1942 softc->flags = 0;
1943
1944 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1945 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0,
1946 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)");
1947
1948 if (ctl_max_luns <= 0 || powerof2(ctl_max_luns) == 0) {
1949 printf("Bad value %d for kern.cam.ctl.max_luns, must be a power of two, using %d\n",
1950 ctl_max_luns, CTL_DEFAULT_MAX_LUNS);
1951 ctl_max_luns = CTL_DEFAULT_MAX_LUNS;
1952 }
1953 softc->ctl_luns = malloc(sizeof(struct ctl_lun *) * ctl_max_luns,
1954 M_DEVBUF, M_WAITOK | M_ZERO);
1955 softc->ctl_lun_mask = malloc(sizeof(uint32_t) *
1956 ((ctl_max_luns + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO);
1957 if (ctl_max_ports <= 0 || powerof2(ctl_max_ports) == 0) {
1958 printf("Bad value %d for kern.cam.ctl.max_ports, must be a power of two, using %d\n",
1959 ctl_max_ports, CTL_DEFAULT_MAX_PORTS);
1960 ctl_max_ports = CTL_DEFAULT_MAX_PORTS;
1961 }
1962 softc->ctl_port_mask = malloc(sizeof(uint32_t) *
1963 ((ctl_max_ports + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO);
1964 softc->ctl_ports = malloc(sizeof(struct ctl_port *) * ctl_max_ports,
1965 M_DEVBUF, M_WAITOK | M_ZERO);
1966
1967 /*
1968 * In Copan's HA scheme, the "master" and "slave" roles are
1969 * figured out through the slot the controller is in. Although it
1970 * is an active/active system, someone has to be in charge.
1971 */
1972 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1973 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1974 "HA head ID (0 - no HA)");
1975 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) {
1976 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1977 softc->is_single = 1;
1978 softc->port_cnt = ctl_max_ports;
1979 softc->port_min = 0;
1980 } else {
1981 softc->port_cnt = ctl_max_ports / NUM_HA_SHELVES;
1982 softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
1983 }
1984 softc->port_max = softc->port_min + softc->port_cnt;
1985 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
1986 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
1987
1988 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1989 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
1990 "HA link state (0 - offline, 1 - unknown, 2 - online)");
1991
1992 STAILQ_INIT(&softc->lun_list);
1993 STAILQ_INIT(&softc->fe_list);
1994 STAILQ_INIT(&softc->port_list);
1995 STAILQ_INIT(&softc->be_list);
1996 ctl_tpc_init(softc);
1997
1998 if (worker_threads <= 0)
1999 worker_threads = max(1, mp_ncpus / 4);
2000 if (worker_threads > CTL_MAX_THREADS)
2001 worker_threads = CTL_MAX_THREADS;
2002
2003 for (i = 0; i < worker_threads; i++) {
2004 struct ctl_thread *thr = &softc->threads[i];
2005
2006 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
2007 thr->ctl_softc = softc;
2008 STAILQ_INIT(&thr->incoming_queue);
2009 STAILQ_INIT(&thr->rtr_queue);
2010 STAILQ_INIT(&thr->done_queue);
2011 STAILQ_INIT(&thr->isc_queue);
2012
2013 error = kproc_kthread_add(ctl_work_thread, thr,
2014 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
2015 if (error != 0) {
2016 printf("error creating CTL work thread!\n");
2017 return (error);
2018 }
2019 }
2020 error = kproc_kthread_add(ctl_thresh_thread, softc,
2021 &softc->ctl_proc, &softc->thresh_thread, 0, 0, "ctl", "thresh");
2022 if (error != 0) {
2023 printf("error creating CTL threshold thread!\n");
2024 return (error);
2025 }
2026
2027 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
2028 OID_AUTO, "ha_role",
2029 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
2030 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
2031
2032 if (softc->is_single == 0) {
2033 if (ctl_frontend_register(&ha_frontend) != 0)
2034 softc->is_single = 1;
2035 }
2036 return (0);
2037 }
2038
2039 static int
ctl_shutdown(void)2040 ctl_shutdown(void)
2041 {
2042 struct ctl_softc *softc = control_softc;
2043 int i;
2044
2045 if (softc->is_single == 0)
2046 ctl_frontend_deregister(&ha_frontend);
2047
2048 destroy_dev(softc->dev);
2049
2050 /* Shutdown CTL threads. */
2051 softc->shutdown = 1;
2052 for (i = 0; i < worker_threads; i++) {
2053 struct ctl_thread *thr = &softc->threads[i];
2054 while (thr->thread != NULL) {
2055 wakeup(thr);
2056 if (thr->thread != NULL)
2057 pause("CTL thr shutdown", 1);
2058 }
2059 mtx_destroy(&thr->queue_lock);
2060 }
2061 while (softc->thresh_thread != NULL) {
2062 wakeup(softc->thresh_thread);
2063 if (softc->thresh_thread != NULL)
2064 pause("CTL thr shutdown", 1);
2065 }
2066
2067 ctl_tpc_shutdown(softc);
2068 uma_zdestroy(softc->io_zone);
2069 mtx_destroy(&softc->ctl_lock);
2070
2071 free(softc->ctl_luns, M_DEVBUF);
2072 free(softc->ctl_lun_mask, M_DEVBUF);
2073 free(softc->ctl_port_mask, M_DEVBUF);
2074 free(softc->ctl_ports, M_DEVBUF);
2075
2076 sysctl_ctx_free(&softc->sysctl_ctx);
2077
2078 free(softc, M_DEVBUF);
2079 control_softc = NULL;
2080 return (0);
2081 }
2082
2083 static int
ctl_module_event_handler(module_t mod,int what,void * arg)2084 ctl_module_event_handler(module_t mod, int what, void *arg)
2085 {
2086
2087 switch (what) {
2088 case MOD_LOAD:
2089 return (ctl_init());
2090 case MOD_UNLOAD:
2091 return (ctl_shutdown());
2092 default:
2093 return (EOPNOTSUPP);
2094 }
2095 }
2096
2097 /*
2098 * XXX KDM should we do some access checks here? Bump a reference count to
2099 * prevent a CTL module from being unloaded while someone has it open?
2100 */
2101 static int
ctl_open(struct cdev * dev,int flags,int fmt,struct thread * td)2102 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
2103 {
2104 return (0);
2105 }
2106
2107 static int
ctl_close(struct cdev * dev,int flags,int fmt,struct thread * td)2108 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
2109 {
2110 return (0);
2111 }
2112
2113 /*
2114 * Remove an initiator by port number and initiator ID.
2115 * Returns 0 for success, -1 for failure.
2116 */
2117 int
ctl_remove_initiator(struct ctl_port * port,int iid)2118 ctl_remove_initiator(struct ctl_port *port, int iid)
2119 {
2120 struct ctl_softc *softc = port->ctl_softc;
2121 int last;
2122
2123 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2124
2125 if (iid > CTL_MAX_INIT_PER_PORT) {
2126 printf("%s: initiator ID %u > maximun %u!\n",
2127 __func__, iid, CTL_MAX_INIT_PER_PORT);
2128 return (-1);
2129 }
2130
2131 mtx_lock(&softc->ctl_lock);
2132 last = (--port->wwpn_iid[iid].in_use == 0);
2133 port->wwpn_iid[iid].last_use = time_uptime;
2134 mtx_unlock(&softc->ctl_lock);
2135 if (last)
2136 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON);
2137 ctl_isc_announce_iid(port, iid);
2138
2139 return (0);
2140 }
2141
2142 /*
2143 * Add an initiator to the initiator map.
2144 * Returns iid for success, < 0 for failure.
2145 */
2146 int
ctl_add_initiator(struct ctl_port * port,int iid,uint64_t wwpn,char * name)2147 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
2148 {
2149 struct ctl_softc *softc = port->ctl_softc;
2150 time_t best_time;
2151 int i, best;
2152
2153 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2154
2155 if (iid >= CTL_MAX_INIT_PER_PORT) {
2156 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
2157 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
2158 free(name, M_CTL);
2159 return (-1);
2160 }
2161
2162 mtx_lock(&softc->ctl_lock);
2163
2164 if (iid < 0 && (wwpn != 0 || name != NULL)) {
2165 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2166 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
2167 iid = i;
2168 break;
2169 }
2170 if (name != NULL && port->wwpn_iid[i].name != NULL &&
2171 strcmp(name, port->wwpn_iid[i].name) == 0) {
2172 iid = i;
2173 break;
2174 }
2175 }
2176 }
2177
2178 if (iid < 0) {
2179 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2180 if (port->wwpn_iid[i].in_use == 0 &&
2181 port->wwpn_iid[i].wwpn == 0 &&
2182 port->wwpn_iid[i].name == NULL) {
2183 iid = i;
2184 break;
2185 }
2186 }
2187 }
2188
2189 if (iid < 0) {
2190 best = -1;
2191 best_time = INT32_MAX;
2192 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2193 if (port->wwpn_iid[i].in_use == 0) {
2194 if (port->wwpn_iid[i].last_use < best_time) {
2195 best = i;
2196 best_time = port->wwpn_iid[i].last_use;
2197 }
2198 }
2199 }
2200 iid = best;
2201 }
2202
2203 if (iid < 0) {
2204 mtx_unlock(&softc->ctl_lock);
2205 free(name, M_CTL);
2206 return (-2);
2207 }
2208
2209 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
2210 /*
2211 * This is not an error yet.
2212 */
2213 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
2214 #if 0
2215 printf("%s: port %d iid %u WWPN %#jx arrived"
2216 " again\n", __func__, port->targ_port,
2217 iid, (uintmax_t)wwpn);
2218 #endif
2219 goto take;
2220 }
2221 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
2222 strcmp(name, port->wwpn_iid[iid].name) == 0) {
2223 #if 0
2224 printf("%s: port %d iid %u name '%s' arrived"
2225 " again\n", __func__, port->targ_port,
2226 iid, name);
2227 #endif
2228 goto take;
2229 }
2230
2231 /*
2232 * This is an error, but what do we do about it? The
2233 * driver is telling us we have a new WWPN for this
2234 * initiator ID, so we pretty much need to use it.
2235 */
2236 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
2237 " but WWPN %#jx '%s' is still at that address\n",
2238 __func__, port->targ_port, iid, wwpn, name,
2239 (uintmax_t)port->wwpn_iid[iid].wwpn,
2240 port->wwpn_iid[iid].name);
2241 }
2242 take:
2243 free(port->wwpn_iid[iid].name, M_CTL);
2244 port->wwpn_iid[iid].name = name;
2245 port->wwpn_iid[iid].wwpn = wwpn;
2246 port->wwpn_iid[iid].in_use++;
2247 mtx_unlock(&softc->ctl_lock);
2248 ctl_isc_announce_iid(port, iid);
2249
2250 return (iid);
2251 }
2252
2253 static int
ctl_create_iid(struct ctl_port * port,int iid,uint8_t * buf)2254 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
2255 {
2256 int len;
2257
2258 switch (port->port_type) {
2259 case CTL_PORT_FC:
2260 {
2261 struct scsi_transportid_fcp *id =
2262 (struct scsi_transportid_fcp *)buf;
2263 if (port->wwpn_iid[iid].wwpn == 0)
2264 return (0);
2265 memset(id, 0, sizeof(*id));
2266 id->format_protocol = SCSI_PROTO_FC;
2267 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
2268 return (sizeof(*id));
2269 }
2270 case CTL_PORT_ISCSI:
2271 {
2272 struct scsi_transportid_iscsi_port *id =
2273 (struct scsi_transportid_iscsi_port *)buf;
2274 if (port->wwpn_iid[iid].name == NULL)
2275 return (0);
2276 memset(id, 0, 256);
2277 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
2278 SCSI_PROTO_ISCSI;
2279 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
2280 len = roundup2(min(len, 252), 4);
2281 scsi_ulto2b(len, id->additional_length);
2282 return (sizeof(*id) + len);
2283 }
2284 case CTL_PORT_SAS:
2285 {
2286 struct scsi_transportid_sas *id =
2287 (struct scsi_transportid_sas *)buf;
2288 if (port->wwpn_iid[iid].wwpn == 0)
2289 return (0);
2290 memset(id, 0, sizeof(*id));
2291 id->format_protocol = SCSI_PROTO_SAS;
2292 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
2293 return (sizeof(*id));
2294 }
2295 default:
2296 {
2297 struct scsi_transportid_spi *id =
2298 (struct scsi_transportid_spi *)buf;
2299 memset(id, 0, sizeof(*id));
2300 id->format_protocol = SCSI_PROTO_SPI;
2301 scsi_ulto2b(iid, id->scsi_addr);
2302 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
2303 return (sizeof(*id));
2304 }
2305 }
2306 }
2307
2308 /*
2309 * Serialize a command that went down the "wrong" side, and so was sent to
2310 * this controller for execution. The logic is a little different than the
2311 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
2312 * sent back to the other side, but in the success case, we execute the
2313 * command on this side (XFER mode) or tell the other side to execute it
2314 * (SER_ONLY mode).
2315 */
2316 static void
ctl_serialize_other_sc_cmd(struct ctl_scsiio * ctsio)2317 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
2318 {
2319 struct ctl_softc *softc = CTL_SOFTC(ctsio);
2320 struct ctl_port *port = CTL_PORT(ctsio);
2321 union ctl_ha_msg msg_info;
2322 struct ctl_lun *lun;
2323 const struct ctl_cmd_entry *entry;
2324 union ctl_io *bio;
2325 uint32_t targ_lun;
2326
2327 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
2328
2329 /* Make sure that we know about this port. */
2330 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) {
2331 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2332 /*retry_count*/ 1);
2333 goto badjuju;
2334 }
2335
2336 /* Make sure that we know about this LUN. */
2337 mtx_lock(&softc->ctl_lock);
2338 if (targ_lun >= ctl_max_luns ||
2339 (lun = softc->ctl_luns[targ_lun]) == NULL) {
2340 mtx_unlock(&softc->ctl_lock);
2341
2342 /*
2343 * The other node would not send this request to us unless
2344 * received announce that we are primary node for this LUN.
2345 * If this LUN does not exist now, it is probably result of
2346 * a race, so respond to initiator in the most opaque way.
2347 */
2348 ctl_set_busy(ctsio);
2349 goto badjuju;
2350 }
2351 mtx_lock(&lun->lun_lock);
2352 mtx_unlock(&softc->ctl_lock);
2353
2354 /*
2355 * If the LUN is invalid, pretend that it doesn't exist.
2356 * It will go away as soon as all pending I/Os completed.
2357 */
2358 if (lun->flags & CTL_LUN_DISABLED) {
2359 mtx_unlock(&lun->lun_lock);
2360 ctl_set_busy(ctsio);
2361 goto badjuju;
2362 }
2363
2364 entry = ctl_get_cmd_entry(ctsio, NULL);
2365 ctsio->seridx = entry->seridx;
2366 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
2367 mtx_unlock(&lun->lun_lock);
2368 goto badjuju;
2369 }
2370
2371 CTL_LUN(ctsio) = lun;
2372 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
2373
2374 /*
2375 * Every I/O goes into the OOA queue for a
2376 * particular LUN, and stays there until completion.
2377 */
2378 #ifdef CTL_TIME_IO
2379 if (LIST_EMPTY(&lun->ooa_queue))
2380 lun->idle_time += getsbinuptime() - lun->last_busy;
2381 #endif
2382 LIST_INSERT_HEAD(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2383
2384 bio = (union ctl_io *)LIST_NEXT(&ctsio->io_hdr, ooa_links);
2385 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, &bio)) {
2386 case CTL_ACTION_PASS:
2387 case CTL_ACTION_SKIP:
2388 if (softc->ha_mode == CTL_HA_MODE_XFER) {
2389 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
2390 ctl_enqueue_rtr((union ctl_io *)ctsio);
2391 mtx_unlock(&lun->lun_lock);
2392 } else {
2393 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
2394 mtx_unlock(&lun->lun_lock);
2395
2396 /* send msg back to other side */
2397 msg_info.hdr.original_sc = ctsio->io_hdr.remote_io;
2398 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
2399 msg_info.hdr.msg_type = CTL_MSG_R2R;
2400 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2401 sizeof(msg_info.hdr), M_WAITOK);
2402 }
2403 break;
2404 case CTL_ACTION_BLOCK:
2405 ctsio->io_hdr.blocker = bio;
2406 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctsio->io_hdr,
2407 blocked_links);
2408 mtx_unlock(&lun->lun_lock);
2409 break;
2410 case CTL_ACTION_OVERLAP:
2411 LIST_REMOVE(&ctsio->io_hdr, ooa_links);
2412 mtx_unlock(&lun->lun_lock);
2413 ctl_set_overlapped_cmd(ctsio);
2414 goto badjuju;
2415 case CTL_ACTION_OVERLAP_TAG:
2416 LIST_REMOVE(&ctsio->io_hdr, ooa_links);
2417 mtx_unlock(&lun->lun_lock);
2418 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
2419 badjuju:
2420 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
2421 msg_info.hdr.original_sc = ctsio->io_hdr.remote_io;
2422 msg_info.hdr.serializing_sc = NULL;
2423 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
2424 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2425 sizeof(msg_info.scsi), M_WAITOK);
2426 ctl_free_io((union ctl_io *)ctsio);
2427 break;
2428 default:
2429 __assert_unreachable();
2430 }
2431 }
2432
2433 /*
2434 * Returns 0 for success, errno for failure.
2435 */
2436 static void
ctl_ioctl_fill_ooa(struct ctl_lun * lun,uint32_t * cur_fill_num,struct ctl_ooa * ooa_hdr,struct ctl_ooa_entry * kern_entries)2437 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2438 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2439 {
2440 struct ctl_io_hdr *ioh;
2441
2442 mtx_lock(&lun->lun_lock);
2443 ioh = LIST_FIRST(&lun->ooa_queue);
2444 if (ioh == NULL) {
2445 mtx_unlock(&lun->lun_lock);
2446 return;
2447 }
2448 while (LIST_NEXT(ioh, ooa_links) != NULL)
2449 ioh = LIST_NEXT(ioh, ooa_links);
2450 for ( ; ioh; ioh = LIST_PREV(ioh, &lun->ooa_queue, ctl_io_hdr, ooa_links)) {
2451 union ctl_io *io = (union ctl_io *)ioh;
2452 struct ctl_ooa_entry *entry;
2453
2454 CTL_IO_ASSERT(io, SCSI);
2455
2456 /*
2457 * If we've got more than we can fit, just count the
2458 * remaining entries.
2459 */
2460 if (*cur_fill_num >= ooa_hdr->alloc_num) {
2461 (*cur_fill_num)++;
2462 continue;
2463 }
2464
2465 entry = &kern_entries[*cur_fill_num];
2466
2467 entry->tag_num = io->scsiio.tag_num;
2468 entry->tag_type = io->scsiio.tag_type;
2469 entry->lun_num = lun->lun;
2470 #ifdef CTL_TIME_IO
2471 entry->start_bt = io->io_hdr.start_bt;
2472 #endif
2473 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2474 entry->cdb_len = io->scsiio.cdb_len;
2475 if (io->io_hdr.blocker != NULL)
2476 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2477
2478 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2479 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2480
2481 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2482 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2483
2484 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2485 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2486
2487 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2488 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2489
2490 if (io->io_hdr.flags & CTL_FLAG_STATUS_QUEUED)
2491 entry->cmd_flags |= CTL_OOACMD_FLAG_STATUS_QUEUED;
2492
2493 if (io->io_hdr.flags & CTL_FLAG_STATUS_SENT)
2494 entry->cmd_flags |= CTL_OOACMD_FLAG_STATUS_SENT;
2495 (*cur_fill_num)++;
2496 }
2497 mtx_unlock(&lun->lun_lock);
2498 }
2499
2500 /*
2501 * Escape characters that are illegal or not recommended in XML.
2502 */
2503 int
ctl_sbuf_printf_esc(struct sbuf * sb,char * str,int size)2504 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2505 {
2506 char *end = str + size;
2507 int retval;
2508
2509 retval = 0;
2510
2511 for (; *str && str < end; str++) {
2512 switch (*str) {
2513 case '&':
2514 retval = sbuf_cat(sb, "&");
2515 break;
2516 case '>':
2517 retval = sbuf_cat(sb, ">");
2518 break;
2519 case '<':
2520 retval = sbuf_cat(sb, "<");
2521 break;
2522 default:
2523 retval = sbuf_putc(sb, *str);
2524 break;
2525 }
2526
2527 if (retval != 0)
2528 break;
2529 }
2530
2531 return (retval);
2532 }
2533
2534 static void
ctl_id_sbuf(struct ctl_devid * id,struct sbuf * sb)2535 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2536 {
2537 struct scsi_vpd_id_descriptor *desc;
2538 int i;
2539
2540 if (id == NULL || id->len < 4)
2541 return;
2542 desc = (struct scsi_vpd_id_descriptor *)id->data;
2543 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2544 case SVPD_ID_TYPE_T10:
2545 sbuf_cat(sb, "t10.");
2546 break;
2547 case SVPD_ID_TYPE_EUI64:
2548 sbuf_cat(sb, "eui.");
2549 break;
2550 case SVPD_ID_TYPE_NAA:
2551 sbuf_cat(sb, "naa.");
2552 break;
2553 case SVPD_ID_TYPE_SCSI_NAME:
2554 break;
2555 }
2556 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2557 case SVPD_ID_CODESET_BINARY:
2558 for (i = 0; i < desc->length; i++)
2559 sbuf_printf(sb, "%02x", desc->identifier[i]);
2560 break;
2561 case SVPD_ID_CODESET_ASCII:
2562 sbuf_printf(sb, "%.*s", (int)desc->length,
2563 (char *)desc->identifier);
2564 break;
2565 case SVPD_ID_CODESET_UTF8:
2566 sbuf_cat(sb, (char *)desc->identifier);
2567 break;
2568 }
2569 }
2570
2571 static int
ctl_ioctl(struct cdev * dev,u_long cmd,caddr_t addr,int flag,struct thread * td)2572 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2573 struct thread *td)
2574 {
2575 struct ctl_softc *softc = dev->si_drv1;
2576 struct ctl_port *port;
2577 struct ctl_lun *lun;
2578 int retval;
2579
2580 retval = 0;
2581
2582 switch (cmd) {
2583 case CTL_IO:
2584 retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2585 break;
2586 case CTL_ENABLE_PORT:
2587 case CTL_DISABLE_PORT:
2588 case CTL_SET_PORT_WWNS: {
2589 struct ctl_port *port;
2590 struct ctl_port_entry *entry;
2591
2592 entry = (struct ctl_port_entry *)addr;
2593
2594 mtx_lock(&softc->ctl_lock);
2595 STAILQ_FOREACH(port, &softc->port_list, links) {
2596 int action, done;
2597
2598 if (port->targ_port < softc->port_min ||
2599 port->targ_port >= softc->port_max)
2600 continue;
2601
2602 action = 0;
2603 done = 0;
2604 if ((entry->port_type == CTL_PORT_NONE)
2605 && (entry->targ_port == port->targ_port)) {
2606 /*
2607 * If the user only wants to enable or
2608 * disable or set WWNs on a specific port,
2609 * do the operation and we're done.
2610 */
2611 action = 1;
2612 done = 1;
2613 } else if (entry->port_type & port->port_type) {
2614 /*
2615 * Compare the user's type mask with the
2616 * particular frontend type to see if we
2617 * have a match.
2618 */
2619 action = 1;
2620 done = 0;
2621
2622 /*
2623 * Make sure the user isn't trying to set
2624 * WWNs on multiple ports at the same time.
2625 */
2626 if (cmd == CTL_SET_PORT_WWNS) {
2627 printf("%s: Can't set WWNs on "
2628 "multiple ports\n", __func__);
2629 retval = EINVAL;
2630 break;
2631 }
2632 }
2633 if (action == 0)
2634 continue;
2635
2636 /*
2637 * XXX KDM we have to drop the lock here, because
2638 * the online/offline operations can potentially
2639 * block. We need to reference count the frontends
2640 * so they can't go away,
2641 */
2642 if (cmd == CTL_ENABLE_PORT) {
2643 mtx_unlock(&softc->ctl_lock);
2644 ctl_port_online(port);
2645 mtx_lock(&softc->ctl_lock);
2646 } else if (cmd == CTL_DISABLE_PORT) {
2647 mtx_unlock(&softc->ctl_lock);
2648 ctl_port_offline(port);
2649 mtx_lock(&softc->ctl_lock);
2650 } else if (cmd == CTL_SET_PORT_WWNS) {
2651 ctl_port_set_wwns(port,
2652 (entry->flags & CTL_PORT_WWNN_VALID) ?
2653 1 : 0, entry->wwnn,
2654 (entry->flags & CTL_PORT_WWPN_VALID) ?
2655 1 : 0, entry->wwpn);
2656 }
2657 if (done != 0)
2658 break;
2659 }
2660 mtx_unlock(&softc->ctl_lock);
2661 break;
2662 }
2663 case CTL_GET_OOA: {
2664 struct ctl_ooa *ooa_hdr;
2665 struct ctl_ooa_entry *entries;
2666 uint32_t cur_fill_num;
2667
2668 ooa_hdr = (struct ctl_ooa *)addr;
2669
2670 if ((ooa_hdr->alloc_len == 0)
2671 || (ooa_hdr->alloc_num == 0)) {
2672 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2673 "must be non-zero\n", __func__,
2674 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2675 retval = EINVAL;
2676 break;
2677 }
2678
2679 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2680 sizeof(struct ctl_ooa_entry))) {
2681 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2682 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2683 __func__, ooa_hdr->alloc_len,
2684 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2685 retval = EINVAL;
2686 break;
2687 }
2688
2689 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2690
2691 mtx_lock(&softc->ctl_lock);
2692 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 &&
2693 (ooa_hdr->lun_num >= ctl_max_luns ||
2694 softc->ctl_luns[ooa_hdr->lun_num] == NULL)) {
2695 mtx_unlock(&softc->ctl_lock);
2696 free(entries, M_CTL);
2697 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2698 __func__, (uintmax_t)ooa_hdr->lun_num);
2699 retval = EINVAL;
2700 break;
2701 }
2702
2703 cur_fill_num = 0;
2704
2705 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2706 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2707 ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2708 ooa_hdr, entries);
2709 }
2710 } else {
2711 lun = softc->ctl_luns[ooa_hdr->lun_num];
2712 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr,
2713 entries);
2714 }
2715 mtx_unlock(&softc->ctl_lock);
2716
2717 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2718 ooa_hdr->fill_len = ooa_hdr->fill_num *
2719 sizeof(struct ctl_ooa_entry);
2720 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2721 if (retval != 0) {
2722 printf("%s: error copying out %d bytes for OOA dump\n",
2723 __func__, ooa_hdr->fill_len);
2724 }
2725
2726 getbinuptime(&ooa_hdr->cur_bt);
2727
2728 if (cur_fill_num > ooa_hdr->alloc_num) {
2729 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2730 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2731 } else {
2732 ooa_hdr->dropped_num = 0;
2733 ooa_hdr->status = CTL_OOA_OK;
2734 }
2735
2736 free(entries, M_CTL);
2737 break;
2738 }
2739 case CTL_DELAY_IO: {
2740 struct ctl_io_delay_info *delay_info;
2741
2742 delay_info = (struct ctl_io_delay_info *)addr;
2743
2744 #ifdef CTL_IO_DELAY
2745 mtx_lock(&softc->ctl_lock);
2746 if (delay_info->lun_id >= ctl_max_luns ||
2747 (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) {
2748 mtx_unlock(&softc->ctl_lock);
2749 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2750 break;
2751 }
2752 mtx_lock(&lun->lun_lock);
2753 mtx_unlock(&softc->ctl_lock);
2754 delay_info->status = CTL_DELAY_STATUS_OK;
2755 switch (delay_info->delay_type) {
2756 case CTL_DELAY_TYPE_CONT:
2757 case CTL_DELAY_TYPE_ONESHOT:
2758 break;
2759 default:
2760 delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE;
2761 break;
2762 }
2763 switch (delay_info->delay_loc) {
2764 case CTL_DELAY_LOC_DATAMOVE:
2765 lun->delay_info.datamove_type = delay_info->delay_type;
2766 lun->delay_info.datamove_delay = delay_info->delay_secs;
2767 break;
2768 case CTL_DELAY_LOC_DONE:
2769 lun->delay_info.done_type = delay_info->delay_type;
2770 lun->delay_info.done_delay = delay_info->delay_secs;
2771 break;
2772 default:
2773 delay_info->status = CTL_DELAY_STATUS_INVALID_LOC;
2774 break;
2775 }
2776 mtx_unlock(&lun->lun_lock);
2777 #else
2778 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2779 #endif /* CTL_IO_DELAY */
2780 break;
2781 }
2782 case CTL_ERROR_INJECT: {
2783 struct ctl_error_desc *err_desc, *new_err_desc;
2784
2785 err_desc = (struct ctl_error_desc *)addr;
2786
2787 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2788 M_WAITOK | M_ZERO);
2789 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2790
2791 mtx_lock(&softc->ctl_lock);
2792 if (err_desc->lun_id >= ctl_max_luns ||
2793 (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) {
2794 mtx_unlock(&softc->ctl_lock);
2795 free(new_err_desc, M_CTL);
2796 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2797 __func__, (uintmax_t)err_desc->lun_id);
2798 retval = EINVAL;
2799 break;
2800 }
2801 mtx_lock(&lun->lun_lock);
2802 mtx_unlock(&softc->ctl_lock);
2803
2804 /*
2805 * We could do some checking here to verify the validity
2806 * of the request, but given the complexity of error
2807 * injection requests, the checking logic would be fairly
2808 * complex.
2809 *
2810 * For now, if the request is invalid, it just won't get
2811 * executed and might get deleted.
2812 */
2813 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2814
2815 /*
2816 * XXX KDM check to make sure the serial number is unique,
2817 * in case we somehow manage to wrap. That shouldn't
2818 * happen for a very long time, but it's the right thing to
2819 * do.
2820 */
2821 new_err_desc->serial = lun->error_serial;
2822 err_desc->serial = lun->error_serial;
2823 lun->error_serial++;
2824
2825 mtx_unlock(&lun->lun_lock);
2826 break;
2827 }
2828 case CTL_ERROR_INJECT_DELETE: {
2829 struct ctl_error_desc *delete_desc, *desc, *desc2;
2830 int delete_done;
2831
2832 delete_desc = (struct ctl_error_desc *)addr;
2833 delete_done = 0;
2834
2835 mtx_lock(&softc->ctl_lock);
2836 if (delete_desc->lun_id >= ctl_max_luns ||
2837 (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) {
2838 mtx_unlock(&softc->ctl_lock);
2839 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2840 __func__, (uintmax_t)delete_desc->lun_id);
2841 retval = EINVAL;
2842 break;
2843 }
2844 mtx_lock(&lun->lun_lock);
2845 mtx_unlock(&softc->ctl_lock);
2846 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2847 if (desc->serial != delete_desc->serial)
2848 continue;
2849
2850 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2851 links);
2852 free(desc, M_CTL);
2853 delete_done = 1;
2854 }
2855 mtx_unlock(&lun->lun_lock);
2856 if (delete_done == 0) {
2857 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2858 "error serial %ju on LUN %u\n", __func__,
2859 delete_desc->serial, delete_desc->lun_id);
2860 retval = EINVAL;
2861 break;
2862 }
2863 break;
2864 }
2865 case CTL_DUMP_STRUCTS: {
2866 int j, k;
2867 struct ctl_port *port;
2868 struct ctl_frontend *fe;
2869
2870 mtx_lock(&softc->ctl_lock);
2871 printf("CTL Persistent Reservation information start:\n");
2872 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2873 mtx_lock(&lun->lun_lock);
2874 if ((lun->flags & CTL_LUN_DISABLED) != 0) {
2875 mtx_unlock(&lun->lun_lock);
2876 continue;
2877 }
2878
2879 for (j = 0; j < ctl_max_ports; j++) {
2880 if (lun->pr_keys[j] == NULL)
2881 continue;
2882 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2883 if (lun->pr_keys[j][k] == 0)
2884 continue;
2885 printf(" LUN %ju port %d iid %d key "
2886 "%#jx\n", lun->lun, j, k,
2887 (uintmax_t)lun->pr_keys[j][k]);
2888 }
2889 }
2890 mtx_unlock(&lun->lun_lock);
2891 }
2892 printf("CTL Persistent Reservation information end\n");
2893 printf("CTL Ports:\n");
2894 STAILQ_FOREACH(port, &softc->port_list, links) {
2895 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2896 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
2897 port->frontend->name, port->port_type,
2898 port->physical_port, port->virtual_port,
2899 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2900 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2901 if (port->wwpn_iid[j].in_use == 0 &&
2902 port->wwpn_iid[j].wwpn == 0 &&
2903 port->wwpn_iid[j].name == NULL)
2904 continue;
2905
2906 printf(" iid %u use %d WWPN %#jx '%s'\n",
2907 j, port->wwpn_iid[j].in_use,
2908 (uintmax_t)port->wwpn_iid[j].wwpn,
2909 port->wwpn_iid[j].name);
2910 }
2911 }
2912 printf("CTL Port information end\n");
2913 mtx_unlock(&softc->ctl_lock);
2914 /*
2915 * XXX KDM calling this without a lock. We'd likely want
2916 * to drop the lock before calling the frontend's dump
2917 * routine anyway.
2918 */
2919 printf("CTL Frontends:\n");
2920 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2921 printf(" Frontend '%s'\n", fe->name);
2922 if (fe->fe_dump != NULL)
2923 fe->fe_dump();
2924 }
2925 printf("CTL Frontend information end\n");
2926 break;
2927 }
2928 case CTL_LUN_REQ: {
2929 struct ctl_lun_req *lun_req;
2930 struct ctl_backend_driver *backend;
2931 void *packed;
2932 nvlist_t *tmp_args_nvl;
2933 size_t packed_len;
2934
2935 lun_req = (struct ctl_lun_req *)addr;
2936 tmp_args_nvl = lun_req->args_nvl;
2937
2938 backend = ctl_backend_find(lun_req->backend);
2939 if (backend == NULL) {
2940 lun_req->status = CTL_LUN_ERROR;
2941 snprintf(lun_req->error_str,
2942 sizeof(lun_req->error_str),
2943 "Backend \"%s\" not found.",
2944 lun_req->backend);
2945 break;
2946 }
2947
2948 if (lun_req->args != NULL) {
2949 if (lun_req->args_len > CTL_MAX_ARGS_LEN) {
2950 lun_req->status = CTL_LUN_ERROR;
2951 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
2952 "Too big args.");
2953 break;
2954 }
2955 packed = malloc(lun_req->args_len, M_CTL, M_WAITOK);
2956 if (copyin(lun_req->args, packed, lun_req->args_len) != 0) {
2957 free(packed, M_CTL);
2958 lun_req->status = CTL_LUN_ERROR;
2959 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
2960 "Cannot copyin args.");
2961 break;
2962 }
2963 lun_req->args_nvl = nvlist_unpack(packed,
2964 lun_req->args_len, 0);
2965 free(packed, M_CTL);
2966
2967 if (lun_req->args_nvl == NULL) {
2968 lun_req->status = CTL_LUN_ERROR;
2969 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
2970 "Cannot unpack args nvlist.");
2971 break;
2972 }
2973 } else
2974 lun_req->args_nvl = nvlist_create(0);
2975
2976 lun_req->result_nvl = NULL;
2977 retval = backend->ioctl(dev, cmd, addr, flag, td);
2978 nvlist_destroy(lun_req->args_nvl);
2979 lun_req->args_nvl = tmp_args_nvl;
2980
2981 if (lun_req->result_nvl != NULL) {
2982 if (lun_req->result != NULL) {
2983 packed = nvlist_pack(lun_req->result_nvl,
2984 &packed_len);
2985 if (packed == NULL) {
2986 lun_req->status = CTL_LUN_ERROR;
2987 snprintf(lun_req->error_str,
2988 sizeof(lun_req->error_str),
2989 "Cannot pack result nvlist.");
2990 break;
2991 }
2992
2993 if (packed_len > lun_req->result_len) {
2994 lun_req->status = CTL_LUN_ERROR;
2995 snprintf(lun_req->error_str,
2996 sizeof(lun_req->error_str),
2997 "Result nvlist too large.");
2998 free(packed, M_NVLIST);
2999 break;
3000 }
3001
3002 if (copyout(packed, lun_req->result, packed_len)) {
3003 lun_req->status = CTL_LUN_ERROR;
3004 snprintf(lun_req->error_str,
3005 sizeof(lun_req->error_str),
3006 "Cannot copyout() the result.");
3007 free(packed, M_NVLIST);
3008 break;
3009 }
3010
3011 lun_req->result_len = packed_len;
3012 free(packed, M_NVLIST);
3013 }
3014
3015 nvlist_destroy(lun_req->result_nvl);
3016 }
3017 break;
3018 }
3019 case CTL_LUN_LIST: {
3020 struct sbuf *sb;
3021 struct ctl_lun_list *list;
3022 const char *name, *value;
3023 void *cookie;
3024 int type;
3025
3026 list = (struct ctl_lun_list *)addr;
3027
3028 /*
3029 * Allocate a fixed length sbuf here, based on the length
3030 * of the user's buffer. We could allocate an auto-extending
3031 * buffer, and then tell the user how much larger our
3032 * amount of data is than his buffer, but that presents
3033 * some problems:
3034 *
3035 * 1. The sbuf(9) routines use a blocking malloc, and so
3036 * we can't hold a lock while calling them with an
3037 * auto-extending buffer.
3038 *
3039 * 2. There is not currently a LUN reference counting
3040 * mechanism, outside of outstanding transactions on
3041 * the LUN's OOA queue. So a LUN could go away on us
3042 * while we're getting the LUN number, backend-specific
3043 * information, etc. Thus, given the way things
3044 * currently work, we need to hold the CTL lock while
3045 * grabbing LUN information.
3046 *
3047 * So, from the user's standpoint, the best thing to do is
3048 * allocate what he thinks is a reasonable buffer length,
3049 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3050 * double the buffer length and try again. (And repeat
3051 * that until he succeeds.)
3052 */
3053 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3054 if (sb == NULL) {
3055 list->status = CTL_LUN_LIST_ERROR;
3056 snprintf(list->error_str, sizeof(list->error_str),
3057 "Unable to allocate %d bytes for LUN list",
3058 list->alloc_len);
3059 break;
3060 }
3061
3062 sbuf_cat(sb, "<ctllunlist>\n");
3063
3064 mtx_lock(&softc->ctl_lock);
3065 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3066 mtx_lock(&lun->lun_lock);
3067 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3068 (uintmax_t)lun->lun);
3069
3070 /*
3071 * Bail out as soon as we see that we've overfilled
3072 * the buffer.
3073 */
3074 if (retval != 0)
3075 break;
3076
3077 retval = sbuf_printf(sb, "\t<backend_type>%s"
3078 "</backend_type>\n",
3079 (lun->backend == NULL) ? "none" :
3080 lun->backend->name);
3081
3082 if (retval != 0)
3083 break;
3084
3085 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3086 lun->be_lun->lun_type);
3087
3088 if (retval != 0)
3089 break;
3090
3091 if (lun->backend == NULL) {
3092 retval = sbuf_cat(sb, "</lun>\n");
3093 if (retval != 0)
3094 break;
3095 continue;
3096 }
3097
3098 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3099 (lun->be_lun->maxlba > 0) ?
3100 lun->be_lun->maxlba + 1 : 0);
3101
3102 if (retval != 0)
3103 break;
3104
3105 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3106 lun->be_lun->blocksize);
3107
3108 if (retval != 0)
3109 break;
3110
3111 retval = sbuf_cat(sb, "\t<serial_number>");
3112
3113 if (retval != 0)
3114 break;
3115
3116 retval = ctl_sbuf_printf_esc(sb,
3117 lun->be_lun->serial_num,
3118 sizeof(lun->be_lun->serial_num));
3119
3120 if (retval != 0)
3121 break;
3122
3123 retval = sbuf_cat(sb, "</serial_number>\n");
3124
3125 if (retval != 0)
3126 break;
3127
3128 retval = sbuf_cat(sb, "\t<device_id>");
3129
3130 if (retval != 0)
3131 break;
3132
3133 retval = ctl_sbuf_printf_esc(sb,
3134 lun->be_lun->device_id,
3135 sizeof(lun->be_lun->device_id));
3136
3137 if (retval != 0)
3138 break;
3139
3140 retval = sbuf_cat(sb, "</device_id>\n");
3141
3142 if (retval != 0)
3143 break;
3144
3145 if (lun->backend->lun_info != NULL) {
3146 retval = lun->backend->lun_info(lun->be_lun, sb);
3147 if (retval != 0)
3148 break;
3149 }
3150
3151 cookie = NULL;
3152 while ((name = nvlist_next(lun->be_lun->options, &type,
3153 &cookie)) != NULL) {
3154 sbuf_printf(sb, "\t<%s>", name);
3155
3156 if (type == NV_TYPE_STRING) {
3157 value = dnvlist_get_string(
3158 lun->be_lun->options, name, NULL);
3159 if (value != NULL)
3160 sbuf_cat(sb, value);
3161 }
3162
3163 sbuf_printf(sb, "</%s>\n", name);
3164 }
3165
3166 retval = sbuf_cat(sb, "</lun>\n");
3167
3168 if (retval != 0)
3169 break;
3170 mtx_unlock(&lun->lun_lock);
3171 }
3172 if (lun != NULL)
3173 mtx_unlock(&lun->lun_lock);
3174 mtx_unlock(&softc->ctl_lock);
3175
3176 if ((retval != 0)
3177 || ((retval = sbuf_cat(sb, "</ctllunlist>\n")) != 0)) {
3178 retval = 0;
3179 sbuf_delete(sb);
3180 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3181 snprintf(list->error_str, sizeof(list->error_str),
3182 "Out of space, %d bytes is too small",
3183 list->alloc_len);
3184 break;
3185 }
3186
3187 sbuf_finish(sb);
3188
3189 retval = copyout(sbuf_data(sb), list->lun_xml,
3190 sbuf_len(sb) + 1);
3191
3192 list->fill_len = sbuf_len(sb) + 1;
3193 list->status = CTL_LUN_LIST_OK;
3194 sbuf_delete(sb);
3195 break;
3196 }
3197 case CTL_ISCSI: {
3198 struct ctl_iscsi *ci;
3199 struct ctl_frontend *fe;
3200
3201 ci = (struct ctl_iscsi *)addr;
3202
3203 fe = ctl_frontend_find("iscsi");
3204 if (fe == NULL) {
3205 ci->status = CTL_ISCSI_ERROR;
3206 snprintf(ci->error_str, sizeof(ci->error_str),
3207 "Frontend \"iscsi\" not found.");
3208 break;
3209 }
3210
3211 retval = fe->ioctl(dev, cmd, addr, flag, td);
3212 break;
3213 }
3214 case CTL_NVMF: {
3215 struct ctl_nvmf *cn;
3216 struct ctl_frontend *fe;
3217
3218 cn = (struct ctl_nvmf *)addr;
3219
3220 fe = ctl_frontend_find("nvmf");
3221 if (fe == NULL) {
3222 cn->status = CTL_NVMF_ERROR;
3223 snprintf(cn->error_str, sizeof(cn->error_str),
3224 "Frontend \"nvmf\" not found.");
3225 break;
3226 }
3227
3228 retval = fe->ioctl(dev, cmd, addr, flag, td);
3229 break;
3230 }
3231 case CTL_PORT_REQ: {
3232 struct ctl_req *req;
3233 struct ctl_frontend *fe;
3234 void *packed;
3235 nvlist_t *tmp_args_nvl;
3236 size_t packed_len;
3237
3238 req = (struct ctl_req *)addr;
3239 tmp_args_nvl = req->args_nvl;
3240
3241 fe = ctl_frontend_find(req->driver);
3242 if (fe == NULL) {
3243 req->status = CTL_LUN_ERROR;
3244 snprintf(req->error_str, sizeof(req->error_str),
3245 "Frontend \"%s\" not found.", req->driver);
3246 break;
3247 }
3248
3249 if (req->args != NULL) {
3250 if (req->args_len > CTL_MAX_ARGS_LEN) {
3251 req->status = CTL_LUN_ERROR;
3252 snprintf(req->error_str, sizeof(req->error_str),
3253 "Too big args.");
3254 break;
3255 }
3256 packed = malloc(req->args_len, M_CTL, M_WAITOK);
3257 if (copyin(req->args, packed, req->args_len) != 0) {
3258 free(packed, M_CTL);
3259 req->status = CTL_LUN_ERROR;
3260 snprintf(req->error_str, sizeof(req->error_str),
3261 "Cannot copyin args.");
3262 break;
3263 }
3264 req->args_nvl = nvlist_unpack(packed,
3265 req->args_len, 0);
3266 free(packed, M_CTL);
3267
3268 if (req->args_nvl == NULL) {
3269 req->status = CTL_LUN_ERROR;
3270 snprintf(req->error_str, sizeof(req->error_str),
3271 "Cannot unpack args nvlist.");
3272 break;
3273 }
3274 } else
3275 req->args_nvl = nvlist_create(0);
3276
3277 req->result_nvl = NULL;
3278 if (fe->ioctl)
3279 retval = fe->ioctl(dev, cmd, addr, flag, td);
3280 else
3281 retval = ENODEV;
3282
3283 nvlist_destroy(req->args_nvl);
3284 req->args_nvl = tmp_args_nvl;
3285
3286 if (req->result_nvl != NULL) {
3287 if (req->result != NULL) {
3288 packed = nvlist_pack(req->result_nvl,
3289 &packed_len);
3290 if (packed == NULL) {
3291 req->status = CTL_LUN_ERROR;
3292 snprintf(req->error_str,
3293 sizeof(req->error_str),
3294 "Cannot pack result nvlist.");
3295 break;
3296 }
3297
3298 if (packed_len > req->result_len) {
3299 req->status = CTL_LUN_ERROR;
3300 snprintf(req->error_str,
3301 sizeof(req->error_str),
3302 "Result nvlist too large.");
3303 free(packed, M_NVLIST);
3304 break;
3305 }
3306
3307 if (copyout(packed, req->result, packed_len)) {
3308 req->status = CTL_LUN_ERROR;
3309 snprintf(req->error_str,
3310 sizeof(req->error_str),
3311 "Cannot copyout() the result.");
3312 free(packed, M_NVLIST);
3313 break;
3314 }
3315
3316 req->result_len = packed_len;
3317 free(packed, M_NVLIST);
3318 }
3319
3320 nvlist_destroy(req->result_nvl);
3321 }
3322 break;
3323 }
3324 case CTL_PORT_LIST: {
3325 struct sbuf *sb;
3326 struct ctl_port *port;
3327 struct ctl_lun_list *list;
3328 const char *name, *value;
3329 void *cookie;
3330 int j, type;
3331 uint32_t plun;
3332
3333 list = (struct ctl_lun_list *)addr;
3334
3335 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3336 if (sb == NULL) {
3337 list->status = CTL_LUN_LIST_ERROR;
3338 snprintf(list->error_str, sizeof(list->error_str),
3339 "Unable to allocate %d bytes for LUN list",
3340 list->alloc_len);
3341 break;
3342 }
3343
3344 sbuf_cat(sb, "<ctlportlist>\n");
3345
3346 mtx_lock(&softc->ctl_lock);
3347 STAILQ_FOREACH(port, &softc->port_list, links) {
3348 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3349 (uintmax_t)port->targ_port);
3350
3351 /*
3352 * Bail out as soon as we see that we've overfilled
3353 * the buffer.
3354 */
3355 if (retval != 0)
3356 break;
3357
3358 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3359 "</frontend_type>\n", port->frontend->name);
3360 if (retval != 0)
3361 break;
3362
3363 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3364 port->port_type);
3365 if (retval != 0)
3366 break;
3367
3368 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3369 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3370 if (retval != 0)
3371 break;
3372
3373 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3374 port->port_name);
3375 if (retval != 0)
3376 break;
3377
3378 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3379 port->physical_port);
3380 if (retval != 0)
3381 break;
3382
3383 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3384 port->virtual_port);
3385 if (retval != 0)
3386 break;
3387
3388 if (port->target_devid != NULL) {
3389 sbuf_cat(sb, "\t<target>");
3390 ctl_id_sbuf(port->target_devid, sb);
3391 sbuf_cat(sb, "</target>\n");
3392 }
3393
3394 if (port->port_devid != NULL) {
3395 sbuf_cat(sb, "\t<port>");
3396 ctl_id_sbuf(port->port_devid, sb);
3397 sbuf_cat(sb, "</port>\n");
3398 }
3399
3400 if (port->port_info != NULL) {
3401 retval = port->port_info(port->onoff_arg, sb);
3402 if (retval != 0)
3403 break;
3404 }
3405
3406 cookie = NULL;
3407 while ((name = nvlist_next(port->options, &type,
3408 &cookie)) != NULL) {
3409 sbuf_printf(sb, "\t<%s>", name);
3410
3411 if (type == NV_TYPE_STRING) {
3412 value = dnvlist_get_string(port->options,
3413 name, NULL);
3414 if (value != NULL)
3415 sbuf_printf(sb, "%s", value);
3416 }
3417
3418 sbuf_printf(sb, "</%s>\n", name);
3419 }
3420
3421 if (port->lun_map != NULL) {
3422 sbuf_cat(sb, "\t<lun_map>on</lun_map>\n");
3423 for (j = 0; j < port->lun_map_size; j++) {
3424 plun = ctl_lun_map_from_port(port, j);
3425 if (plun == UINT32_MAX)
3426 continue;
3427 sbuf_printf(sb,
3428 "\t<lun id=\"%u\">%u</lun>\n",
3429 j, plun);
3430 }
3431 }
3432
3433 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3434 if (port->wwpn_iid[j].in_use == 0 ||
3435 (port->wwpn_iid[j].wwpn == 0 &&
3436 port->wwpn_iid[j].name == NULL))
3437 continue;
3438
3439 if (port->wwpn_iid[j].name != NULL)
3440 retval = sbuf_printf(sb,
3441 "\t<initiator id=\"%u\">%s</initiator>\n",
3442 j, port->wwpn_iid[j].name);
3443 else
3444 retval = sbuf_printf(sb,
3445 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3446 j, port->wwpn_iid[j].wwpn);
3447 if (retval != 0)
3448 break;
3449 }
3450 if (retval != 0)
3451 break;
3452
3453 retval = sbuf_cat(sb, "</targ_port>\n");
3454 if (retval != 0)
3455 break;
3456 }
3457 mtx_unlock(&softc->ctl_lock);
3458
3459 if ((retval != 0)
3460 || ((retval = sbuf_cat(sb, "</ctlportlist>\n")) != 0)) {
3461 retval = 0;
3462 sbuf_delete(sb);
3463 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3464 snprintf(list->error_str, sizeof(list->error_str),
3465 "Out of space, %d bytes is too small",
3466 list->alloc_len);
3467 break;
3468 }
3469
3470 sbuf_finish(sb);
3471
3472 retval = copyout(sbuf_data(sb), list->lun_xml,
3473 sbuf_len(sb) + 1);
3474
3475 list->fill_len = sbuf_len(sb) + 1;
3476 list->status = CTL_LUN_LIST_OK;
3477 sbuf_delete(sb);
3478 break;
3479 }
3480 case CTL_LUN_MAP: {
3481 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3482 struct ctl_port *port;
3483
3484 mtx_lock(&softc->ctl_lock);
3485 if (lm->port < softc->port_min ||
3486 lm->port >= softc->port_max ||
3487 (port = softc->ctl_ports[lm->port]) == NULL) {
3488 mtx_unlock(&softc->ctl_lock);
3489 return (ENXIO);
3490 }
3491 if (port->status & CTL_PORT_STATUS_ONLINE) {
3492 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3493 if (ctl_lun_map_to_port(port, lun->lun) ==
3494 UINT32_MAX)
3495 continue;
3496 mtx_lock(&lun->lun_lock);
3497 ctl_est_ua_port(lun, lm->port, -1,
3498 CTL_UA_LUN_CHANGE);
3499 mtx_unlock(&lun->lun_lock);
3500 }
3501 }
3502 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3503 if (lm->plun != UINT32_MAX) {
3504 if (lm->lun == UINT32_MAX)
3505 retval = ctl_lun_map_unset(port, lm->plun);
3506 else if (lm->lun < ctl_max_luns &&
3507 softc->ctl_luns[lm->lun] != NULL)
3508 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3509 else
3510 return (ENXIO);
3511 } else {
3512 if (lm->lun == UINT32_MAX)
3513 retval = ctl_lun_map_deinit(port);
3514 else
3515 retval = ctl_lun_map_init(port);
3516 }
3517 if (port->status & CTL_PORT_STATUS_ONLINE)
3518 ctl_isc_announce_port(port);
3519 break;
3520 }
3521 case CTL_GET_LUN_STATS: {
3522 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3523 int i;
3524
3525 /*
3526 * XXX KDM no locking here. If the LUN list changes,
3527 * things can blow up.
3528 */
3529 i = 0;
3530 stats->status = CTL_SS_OK;
3531 stats->fill_len = 0;
3532 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3533 if (lun->lun < stats->first_item)
3534 continue;
3535 if (stats->fill_len + sizeof(lun->stats) >
3536 stats->alloc_len) {
3537 stats->status = CTL_SS_NEED_MORE_SPACE;
3538 break;
3539 }
3540 retval = copyout(&lun->stats, &stats->stats[i++],
3541 sizeof(lun->stats));
3542 if (retval != 0)
3543 break;
3544 stats->fill_len += sizeof(lun->stats);
3545 }
3546 stats->num_items = softc->num_luns;
3547 stats->flags = CTL_STATS_FLAG_NONE;
3548 #ifdef CTL_TIME_IO
3549 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3550 #endif
3551 getnanouptime(&stats->timestamp);
3552 break;
3553 }
3554 case CTL_GET_PORT_STATS: {
3555 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3556 int i;
3557
3558 /*
3559 * XXX KDM no locking here. If the LUN list changes,
3560 * things can blow up.
3561 */
3562 i = 0;
3563 stats->status = CTL_SS_OK;
3564 stats->fill_len = 0;
3565 STAILQ_FOREACH(port, &softc->port_list, links) {
3566 if (port->targ_port < stats->first_item)
3567 continue;
3568 if (stats->fill_len + sizeof(port->stats) >
3569 stats->alloc_len) {
3570 stats->status = CTL_SS_NEED_MORE_SPACE;
3571 break;
3572 }
3573 retval = copyout(&port->stats, &stats->stats[i++],
3574 sizeof(port->stats));
3575 if (retval != 0)
3576 break;
3577 stats->fill_len += sizeof(port->stats);
3578 }
3579 stats->num_items = softc->num_ports;
3580 stats->flags = CTL_STATS_FLAG_NONE;
3581 #ifdef CTL_TIME_IO
3582 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3583 #endif
3584 getnanouptime(&stats->timestamp);
3585 break;
3586 }
3587 default: {
3588 /* XXX KDM should we fix this? */
3589 #if 0
3590 struct ctl_backend_driver *backend;
3591 unsigned int type;
3592 int found;
3593
3594 found = 0;
3595
3596 /*
3597 * We encode the backend type as the ioctl type for backend
3598 * ioctls. So parse it out here, and then search for a
3599 * backend of this type.
3600 */
3601 type = _IOC_TYPE(cmd);
3602
3603 STAILQ_FOREACH(backend, &softc->be_list, links) {
3604 if (backend->type == type) {
3605 found = 1;
3606 break;
3607 }
3608 }
3609 if (found == 0) {
3610 printf("ctl: unknown ioctl command %#lx or backend "
3611 "%d\n", cmd, type);
3612 retval = EINVAL;
3613 break;
3614 }
3615 retval = backend->ioctl(dev, cmd, addr, flag, td);
3616 #endif
3617 retval = ENOTTY;
3618 break;
3619 }
3620 }
3621 return (retval);
3622 }
3623
3624 uint32_t
ctl_get_initindex(struct ctl_nexus * nexus)3625 ctl_get_initindex(struct ctl_nexus *nexus)
3626 {
3627 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3628 }
3629
3630 int
ctl_lun_map_init(struct ctl_port * port)3631 ctl_lun_map_init(struct ctl_port *port)
3632 {
3633 struct ctl_softc *softc = port->ctl_softc;
3634 struct ctl_lun *lun;
3635 int size = ctl_lun_map_size;
3636 uint32_t i;
3637
3638 if (port->lun_map == NULL || port->lun_map_size < size) {
3639 port->lun_map_size = 0;
3640 free(port->lun_map, M_CTL);
3641 port->lun_map = malloc(size * sizeof(uint32_t),
3642 M_CTL, M_NOWAIT);
3643 }
3644 if (port->lun_map == NULL)
3645 return (ENOMEM);
3646 for (i = 0; i < size; i++)
3647 port->lun_map[i] = UINT32_MAX;
3648 port->lun_map_size = size;
3649 if (port->status & CTL_PORT_STATUS_ONLINE) {
3650 if (port->lun_disable != NULL) {
3651 STAILQ_FOREACH(lun, &softc->lun_list, links)
3652 port->lun_disable(port->targ_lun_arg, lun->lun);
3653 }
3654 ctl_isc_announce_port(port);
3655 }
3656 return (0);
3657 }
3658
3659 int
ctl_lun_map_deinit(struct ctl_port * port)3660 ctl_lun_map_deinit(struct ctl_port *port)
3661 {
3662 struct ctl_softc *softc = port->ctl_softc;
3663 struct ctl_lun *lun;
3664
3665 if (port->lun_map == NULL)
3666 return (0);
3667 port->lun_map_size = 0;
3668 free(port->lun_map, M_CTL);
3669 port->lun_map = NULL;
3670 if (port->status & CTL_PORT_STATUS_ONLINE) {
3671 if (port->lun_enable != NULL) {
3672 STAILQ_FOREACH(lun, &softc->lun_list, links)
3673 port->lun_enable(port->targ_lun_arg, lun->lun);
3674 }
3675 ctl_isc_announce_port(port);
3676 }
3677 return (0);
3678 }
3679
3680 int
ctl_lun_map_set(struct ctl_port * port,uint32_t plun,uint32_t glun)3681 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3682 {
3683 int status;
3684 uint32_t old;
3685
3686 if (port->lun_map == NULL) {
3687 status = ctl_lun_map_init(port);
3688 if (status != 0)
3689 return (status);
3690 }
3691 if (plun >= port->lun_map_size)
3692 return (EINVAL);
3693 old = port->lun_map[plun];
3694 port->lun_map[plun] = glun;
3695 if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) {
3696 if (port->lun_enable != NULL)
3697 port->lun_enable(port->targ_lun_arg, plun);
3698 ctl_isc_announce_port(port);
3699 }
3700 return (0);
3701 }
3702
3703 int
ctl_lun_map_unset(struct ctl_port * port,uint32_t plun)3704 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3705 {
3706 uint32_t old;
3707
3708 if (port->lun_map == NULL || plun >= port->lun_map_size)
3709 return (0);
3710 old = port->lun_map[plun];
3711 port->lun_map[plun] = UINT32_MAX;
3712 if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) {
3713 if (port->lun_disable != NULL)
3714 port->lun_disable(port->targ_lun_arg, plun);
3715 ctl_isc_announce_port(port);
3716 }
3717 return (0);
3718 }
3719
3720 uint32_t
ctl_lun_map_from_port(struct ctl_port * port,uint32_t lun_id)3721 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3722 {
3723
3724 if (port == NULL)
3725 return (UINT32_MAX);
3726 if (port->lun_map == NULL)
3727 return (lun_id);
3728 if (lun_id > port->lun_map_size)
3729 return (UINT32_MAX);
3730 return (port->lun_map[lun_id]);
3731 }
3732
3733 uint32_t
ctl_lun_map_to_port(struct ctl_port * port,uint32_t lun_id)3734 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3735 {
3736 uint32_t i;
3737
3738 if (port == NULL)
3739 return (UINT32_MAX);
3740 if (port->lun_map == NULL)
3741 return (lun_id);
3742 for (i = 0; i < port->lun_map_size; i++) {
3743 if (port->lun_map[i] == lun_id)
3744 return (i);
3745 }
3746 return (UINT32_MAX);
3747 }
3748
3749 uint32_t
ctl_decode_lun(uint64_t encoded)3750 ctl_decode_lun(uint64_t encoded)
3751 {
3752 uint8_t lun[8];
3753 uint32_t result = 0xffffffff;
3754
3755 be64enc(lun, encoded);
3756 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) {
3757 case RPL_LUNDATA_ATYP_PERIPH:
3758 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 &&
3759 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0)
3760 result = lun[1];
3761 break;
3762 case RPL_LUNDATA_ATYP_FLAT:
3763 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 &&
3764 lun[6] == 0 && lun[7] == 0)
3765 result = ((lun[0] & 0x3f) << 8) + lun[1];
3766 break;
3767 case RPL_LUNDATA_ATYP_EXTLUN:
3768 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) {
3769 case 0x02:
3770 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) {
3771 case 0x00:
3772 result = lun[1];
3773 break;
3774 case 0x10:
3775 result = (lun[1] << 16) + (lun[2] << 8) +
3776 lun[3];
3777 break;
3778 case 0x20:
3779 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0)
3780 result = (lun[2] << 24) +
3781 (lun[3] << 16) + (lun[4] << 8) +
3782 lun[5];
3783 break;
3784 }
3785 break;
3786 case RPL_LUNDATA_EXT_EAM_NOT_SPEC:
3787 result = 0xffffffff;
3788 break;
3789 }
3790 break;
3791 }
3792 return (result);
3793 }
3794
3795 uint64_t
ctl_encode_lun(uint32_t decoded)3796 ctl_encode_lun(uint32_t decoded)
3797 {
3798 uint64_t l = decoded;
3799
3800 if (l <= 0xff)
3801 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48));
3802 if (l <= 0x3fff)
3803 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48));
3804 if (l <= 0xffffff)
3805 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) |
3806 (l << 32));
3807 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16));
3808 }
3809
3810 int
ctl_ffz(uint32_t * mask,uint32_t first,uint32_t last)3811 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3812 {
3813 int i;
3814
3815 for (i = first; i < last; i++) {
3816 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3817 return (i);
3818 }
3819 return (-1);
3820 }
3821
3822 int
ctl_set_mask(uint32_t * mask,uint32_t bit)3823 ctl_set_mask(uint32_t *mask, uint32_t bit)
3824 {
3825 uint32_t chunk, piece;
3826
3827 chunk = bit >> 5;
3828 piece = bit % (sizeof(uint32_t) * 8);
3829
3830 if ((mask[chunk] & (1 << piece)) != 0)
3831 return (-1);
3832 else
3833 mask[chunk] |= (1 << piece);
3834
3835 return (0);
3836 }
3837
3838 int
ctl_clear_mask(uint32_t * mask,uint32_t bit)3839 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3840 {
3841 uint32_t chunk, piece;
3842
3843 chunk = bit >> 5;
3844 piece = bit % (sizeof(uint32_t) * 8);
3845
3846 if ((mask[chunk] & (1 << piece)) == 0)
3847 return (-1);
3848 else
3849 mask[chunk] &= ~(1 << piece);
3850
3851 return (0);
3852 }
3853
3854 int
ctl_is_set(uint32_t * mask,uint32_t bit)3855 ctl_is_set(uint32_t *mask, uint32_t bit)
3856 {
3857 uint32_t chunk, piece;
3858
3859 chunk = bit >> 5;
3860 piece = bit % (sizeof(uint32_t) * 8);
3861
3862 if ((mask[chunk] & (1 << piece)) == 0)
3863 return (0);
3864 else
3865 return (1);
3866 }
3867
3868 static uint64_t
ctl_get_prkey(struct ctl_lun * lun,uint32_t residx)3869 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3870 {
3871 uint64_t *t;
3872
3873 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3874 if (t == NULL)
3875 return (0);
3876 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3877 }
3878
3879 static void
ctl_clr_prkey(struct ctl_lun * lun,uint32_t residx)3880 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3881 {
3882 uint64_t *t;
3883
3884 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3885 if (t == NULL)
3886 return;
3887 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3888 }
3889
3890 static void
ctl_alloc_prkey(struct ctl_lun * lun,uint32_t residx)3891 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3892 {
3893 uint64_t *p;
3894 u_int i;
3895
3896 i = residx/CTL_MAX_INIT_PER_PORT;
3897 if (lun->pr_keys[i] != NULL)
3898 return;
3899 mtx_unlock(&lun->lun_lock);
3900 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3901 M_WAITOK | M_ZERO);
3902 mtx_lock(&lun->lun_lock);
3903 if (lun->pr_keys[i] == NULL)
3904 lun->pr_keys[i] = p;
3905 else
3906 free(p, M_CTL);
3907 }
3908
3909 static void
ctl_set_prkey(struct ctl_lun * lun,uint32_t residx,uint64_t key)3910 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3911 {
3912 uint64_t *t;
3913
3914 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3915 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3916 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3917 }
3918
3919 /*
3920 * ctl_softc, pool_name, total_ctl_io are passed in.
3921 * npool is passed out.
3922 */
3923 int
ctl_pool_create(struct ctl_softc * ctl_softc,const char * pool_name,uint32_t total_ctl_io,void ** npool)3924 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3925 uint32_t total_ctl_io, void **npool)
3926 {
3927 struct ctl_io_pool *pool;
3928
3929 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3930 M_NOWAIT | M_ZERO);
3931 if (pool == NULL)
3932 return (ENOMEM);
3933
3934 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3935 pool->ctl_softc = ctl_softc;
3936 #ifdef IO_POOLS
3937 pool->zone = uma_zsecond_create(pool->name, NULL,
3938 NULL, NULL, NULL, ctl_softc->io_zone);
3939 /* uma_prealloc(pool->zone, total_ctl_io); */
3940 #else
3941 pool->zone = ctl_softc->io_zone;
3942 #endif
3943
3944 *npool = pool;
3945 return (0);
3946 }
3947
3948 void
ctl_pool_free(struct ctl_io_pool * pool)3949 ctl_pool_free(struct ctl_io_pool *pool)
3950 {
3951
3952 if (pool == NULL)
3953 return;
3954
3955 #ifdef IO_POOLS
3956 uma_zdestroy(pool->zone);
3957 #endif
3958 free(pool, M_CTL);
3959 }
3960
3961 union ctl_io *
ctl_alloc_io(void * pool_ref)3962 ctl_alloc_io(void *pool_ref)
3963 {
3964 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3965 union ctl_io *io;
3966
3967 io = uma_zalloc(pool->zone, M_WAITOK);
3968 if (io != NULL) {
3969 io->io_hdr.pool = pool_ref;
3970 CTL_SOFTC(io) = pool->ctl_softc;
3971 TAILQ_INIT(&io->io_hdr.blocked_queue);
3972 }
3973 return (io);
3974 }
3975
3976 union ctl_io *
ctl_alloc_io_nowait(void * pool_ref)3977 ctl_alloc_io_nowait(void *pool_ref)
3978 {
3979 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3980 union ctl_io *io;
3981
3982 io = uma_zalloc(pool->zone, M_NOWAIT);
3983 if (io != NULL) {
3984 io->io_hdr.pool = pool_ref;
3985 CTL_SOFTC(io) = pool->ctl_softc;
3986 TAILQ_INIT(&io->io_hdr.blocked_queue);
3987 }
3988 return (io);
3989 }
3990
3991 void
ctl_free_io(union ctl_io * io)3992 ctl_free_io(union ctl_io *io)
3993 {
3994 struct ctl_io_pool *pool;
3995
3996 if (io == NULL)
3997 return;
3998
3999 pool = (struct ctl_io_pool *)io->io_hdr.pool;
4000 uma_zfree(pool->zone, io);
4001 }
4002
4003 void
ctl_zero_io(union ctl_io * io)4004 ctl_zero_io(union ctl_io *io)
4005 {
4006 struct ctl_io_pool *pool;
4007
4008 if (io == NULL)
4009 return;
4010
4011 /*
4012 * May need to preserve linked list pointers at some point too.
4013 */
4014 pool = io->io_hdr.pool;
4015 memset(io, 0, sizeof(*io));
4016 io->io_hdr.pool = pool;
4017 CTL_SOFTC(io) = pool->ctl_softc;
4018 TAILQ_INIT(&io->io_hdr.blocked_queue);
4019 }
4020
4021 int
ctl_expand_number(const char * buf,uint64_t * num)4022 ctl_expand_number(const char *buf, uint64_t *num)
4023 {
4024 char *endptr;
4025 uint64_t number;
4026 unsigned shift;
4027
4028 number = strtoq(buf, &endptr, 0);
4029
4030 switch (tolower((unsigned char)*endptr)) {
4031 case 'e':
4032 shift = 60;
4033 break;
4034 case 'p':
4035 shift = 50;
4036 break;
4037 case 't':
4038 shift = 40;
4039 break;
4040 case 'g':
4041 shift = 30;
4042 break;
4043 case 'm':
4044 shift = 20;
4045 break;
4046 case 'k':
4047 shift = 10;
4048 break;
4049 case 'b':
4050 case '\0': /* No unit. */
4051 *num = number;
4052 return (0);
4053 default:
4054 /* Unrecognized unit. */
4055 return (-1);
4056 }
4057
4058 if ((number << shift) >> shift != number) {
4059 /* Overflow */
4060 return (-1);
4061 }
4062 *num = number << shift;
4063 return (0);
4064 }
4065
4066 /*
4067 * This routine could be used in the future to load default and/or saved
4068 * mode page parameters for a particuar lun.
4069 */
4070 static int
ctl_init_page_index(struct ctl_lun * lun)4071 ctl_init_page_index(struct ctl_lun *lun)
4072 {
4073 int i, page_code;
4074 struct ctl_page_index *page_index;
4075 const char *value;
4076 uint64_t ival;
4077
4078 memcpy(&lun->mode_pages.index, page_index_template,
4079 sizeof(page_index_template));
4080
4081 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4082 page_index = &lun->mode_pages.index[i];
4083 if (lun->be_lun->lun_type == T_DIRECT &&
4084 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4085 continue;
4086 if (lun->be_lun->lun_type == T_PROCESSOR &&
4087 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4088 continue;
4089 if (lun->be_lun->lun_type == T_CDROM &&
4090 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4091 continue;
4092
4093 page_code = page_index->page_code & SMPH_PC_MASK;
4094 switch (page_code) {
4095 case SMS_RW_ERROR_RECOVERY_PAGE: {
4096 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4097 ("subpage %#x for page %#x is incorrect!",
4098 page_index->subpage, page_code));
4099 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
4100 &rw_er_page_default,
4101 sizeof(rw_er_page_default));
4102 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
4103 &rw_er_page_changeable,
4104 sizeof(rw_er_page_changeable));
4105 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
4106 &rw_er_page_default,
4107 sizeof(rw_er_page_default));
4108 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
4109 &rw_er_page_default,
4110 sizeof(rw_er_page_default));
4111 page_index->page_data =
4112 (uint8_t *)lun->mode_pages.rw_er_page;
4113 break;
4114 }
4115 case SMS_VERIFY_ERROR_RECOVERY_PAGE: {
4116 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4117 ("subpage %#x for page %#x is incorrect!",
4118 page_index->subpage, page_code));
4119 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT],
4120 &verify_er_page_default,
4121 sizeof(verify_er_page_default));
4122 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE],
4123 &verify_er_page_changeable,
4124 sizeof(verify_er_page_changeable));
4125 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT],
4126 &verify_er_page_default,
4127 sizeof(verify_er_page_default));
4128 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED],
4129 &verify_er_page_default,
4130 sizeof(verify_er_page_default));
4131 page_index->page_data =
4132 (uint8_t *)lun->mode_pages.verify_er_page;
4133 break;
4134 }
4135 case SMS_CACHING_PAGE: {
4136 struct scsi_caching_page *caching_page;
4137
4138 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4139 ("subpage %#x for page %#x is incorrect!",
4140 page_index->subpage, page_code));
4141 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4142 &caching_page_default,
4143 sizeof(caching_page_default));
4144 memcpy(&lun->mode_pages.caching_page[
4145 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4146 sizeof(caching_page_changeable));
4147 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4148 &caching_page_default,
4149 sizeof(caching_page_default));
4150 caching_page = &lun->mode_pages.caching_page[
4151 CTL_PAGE_SAVED];
4152 value = dnvlist_get_string(lun->be_lun->options,
4153 "writecache", NULL);
4154 if (value != NULL && strcmp(value, "off") == 0)
4155 caching_page->flags1 &= ~SCP_WCE;
4156 value = dnvlist_get_string(lun->be_lun->options,
4157 "readcache", NULL);
4158 if (value != NULL && strcmp(value, "off") == 0)
4159 caching_page->flags1 |= SCP_RCD;
4160 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4161 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4162 sizeof(caching_page_default));
4163 page_index->page_data =
4164 (uint8_t *)lun->mode_pages.caching_page;
4165 break;
4166 }
4167 case SMS_CONTROL_MODE_PAGE: {
4168 switch (page_index->subpage) {
4169 case SMS_SUBPAGE_PAGE_0: {
4170 struct scsi_control_page *control_page;
4171
4172 memcpy(&lun->mode_pages.control_page[
4173 CTL_PAGE_DEFAULT],
4174 &control_page_default,
4175 sizeof(control_page_default));
4176 memcpy(&lun->mode_pages.control_page[
4177 CTL_PAGE_CHANGEABLE],
4178 &control_page_changeable,
4179 sizeof(control_page_changeable));
4180 memcpy(&lun->mode_pages.control_page[
4181 CTL_PAGE_SAVED],
4182 &control_page_default,
4183 sizeof(control_page_default));
4184 control_page = &lun->mode_pages.control_page[
4185 CTL_PAGE_SAVED];
4186 value = dnvlist_get_string(lun->be_lun->options,
4187 "reordering", NULL);
4188 if (value != NULL &&
4189 strcmp(value, "unrestricted") == 0) {
4190 control_page->queue_flags &=
4191 ~SCP_QUEUE_ALG_MASK;
4192 control_page->queue_flags |=
4193 SCP_QUEUE_ALG_UNRESTRICTED;
4194 }
4195 memcpy(&lun->mode_pages.control_page[
4196 CTL_PAGE_CURRENT],
4197 &lun->mode_pages.control_page[
4198 CTL_PAGE_SAVED],
4199 sizeof(control_page_default));
4200 page_index->page_data =
4201 (uint8_t *)lun->mode_pages.control_page;
4202 break;
4203 }
4204 case 0x01:
4205 memcpy(&lun->mode_pages.control_ext_page[
4206 CTL_PAGE_DEFAULT],
4207 &control_ext_page_default,
4208 sizeof(control_ext_page_default));
4209 memcpy(&lun->mode_pages.control_ext_page[
4210 CTL_PAGE_CHANGEABLE],
4211 &control_ext_page_changeable,
4212 sizeof(control_ext_page_changeable));
4213 memcpy(&lun->mode_pages.control_ext_page[
4214 CTL_PAGE_SAVED],
4215 &control_ext_page_default,
4216 sizeof(control_ext_page_default));
4217 memcpy(&lun->mode_pages.control_ext_page[
4218 CTL_PAGE_CURRENT],
4219 &lun->mode_pages.control_ext_page[
4220 CTL_PAGE_SAVED],
4221 sizeof(control_ext_page_default));
4222 page_index->page_data =
4223 (uint8_t *)lun->mode_pages.control_ext_page;
4224 break;
4225 default:
4226 panic("subpage %#x for page %#x is incorrect!",
4227 page_index->subpage, page_code);
4228 }
4229 break;
4230 }
4231 case SMS_INFO_EXCEPTIONS_PAGE: {
4232 switch (page_index->subpage) {
4233 case SMS_SUBPAGE_PAGE_0:
4234 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4235 &ie_page_default,
4236 sizeof(ie_page_default));
4237 memcpy(&lun->mode_pages.ie_page[
4238 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4239 sizeof(ie_page_changeable));
4240 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4241 &ie_page_default,
4242 sizeof(ie_page_default));
4243 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4244 &ie_page_default,
4245 sizeof(ie_page_default));
4246 page_index->page_data =
4247 (uint8_t *)lun->mode_pages.ie_page;
4248 break;
4249 case 0x02: {
4250 struct ctl_logical_block_provisioning_page *page;
4251
4252 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4253 &lbp_page_default,
4254 sizeof(lbp_page_default));
4255 memcpy(&lun->mode_pages.lbp_page[
4256 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4257 sizeof(lbp_page_changeable));
4258 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4259 &lbp_page_default,
4260 sizeof(lbp_page_default));
4261 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4262 value = dnvlist_get_string(lun->be_lun->options,
4263 "avail-threshold", NULL);
4264 if (value != NULL &&
4265 ctl_expand_number(value, &ival) == 0) {
4266 page->descr[0].flags |= SLBPPD_ENABLED |
4267 SLBPPD_ARMING_DEC;
4268 if (lun->be_lun->blocksize)
4269 ival /= lun->be_lun->blocksize;
4270 else
4271 ival /= 512;
4272 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4273 page->descr[0].count);
4274 }
4275 value = dnvlist_get_string(lun->be_lun->options,
4276 "used-threshold", NULL);
4277 if (value != NULL &&
4278 ctl_expand_number(value, &ival) == 0) {
4279 page->descr[1].flags |= SLBPPD_ENABLED |
4280 SLBPPD_ARMING_INC;
4281 if (lun->be_lun->blocksize)
4282 ival /= lun->be_lun->blocksize;
4283 else
4284 ival /= 512;
4285 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4286 page->descr[1].count);
4287 }
4288 value = dnvlist_get_string(lun->be_lun->options,
4289 "pool-avail-threshold", NULL);
4290 if (value != NULL &&
4291 ctl_expand_number(value, &ival) == 0) {
4292 page->descr[2].flags |= SLBPPD_ENABLED |
4293 SLBPPD_ARMING_DEC;
4294 if (lun->be_lun->blocksize)
4295 ival /= lun->be_lun->blocksize;
4296 else
4297 ival /= 512;
4298 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4299 page->descr[2].count);
4300 }
4301 value = dnvlist_get_string(lun->be_lun->options,
4302 "pool-used-threshold", NULL);
4303 if (value != NULL &&
4304 ctl_expand_number(value, &ival) == 0) {
4305 page->descr[3].flags |= SLBPPD_ENABLED |
4306 SLBPPD_ARMING_INC;
4307 if (lun->be_lun->blocksize)
4308 ival /= lun->be_lun->blocksize;
4309 else
4310 ival /= 512;
4311 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4312 page->descr[3].count);
4313 }
4314 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4315 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4316 sizeof(lbp_page_default));
4317 page_index->page_data =
4318 (uint8_t *)lun->mode_pages.lbp_page;
4319 break;
4320 }
4321 default:
4322 panic("subpage %#x for page %#x is incorrect!",
4323 page_index->subpage, page_code);
4324 }
4325 break;
4326 }
4327 case SMS_CDDVD_CAPS_PAGE:{
4328 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4329 ("subpage %#x for page %#x is incorrect!",
4330 page_index->subpage, page_code));
4331 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT],
4332 &cddvd_page_default,
4333 sizeof(cddvd_page_default));
4334 memcpy(&lun->mode_pages.cddvd_page[
4335 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable,
4336 sizeof(cddvd_page_changeable));
4337 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4338 &cddvd_page_default,
4339 sizeof(cddvd_page_default));
4340 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT],
4341 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4342 sizeof(cddvd_page_default));
4343 page_index->page_data =
4344 (uint8_t *)lun->mode_pages.cddvd_page;
4345 break;
4346 }
4347 default:
4348 panic("invalid page code value %#x", page_code);
4349 }
4350 }
4351
4352 return (CTL_RETVAL_COMPLETE);
4353 }
4354
4355 static int
ctl_init_log_page_index(struct ctl_lun * lun)4356 ctl_init_log_page_index(struct ctl_lun *lun)
4357 {
4358 struct ctl_page_index *page_index;
4359 int i, j, k, prev;
4360
4361 memcpy(&lun->log_pages.index, log_page_index_template,
4362 sizeof(log_page_index_template));
4363
4364 prev = -1;
4365 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4366 page_index = &lun->log_pages.index[i];
4367 if (lun->be_lun->lun_type == T_DIRECT &&
4368 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4369 continue;
4370 if (lun->be_lun->lun_type == T_PROCESSOR &&
4371 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4372 continue;
4373 if (lun->be_lun->lun_type == T_CDROM &&
4374 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4375 continue;
4376
4377 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4378 lun->backend->lun_attr == NULL)
4379 continue;
4380
4381 if (page_index->page_code != prev) {
4382 lun->log_pages.pages_page[j] = page_index->page_code;
4383 prev = page_index->page_code;
4384 j++;
4385 }
4386 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4387 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4388 k++;
4389 }
4390 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4391 lun->log_pages.index[0].page_len = j;
4392 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4393 lun->log_pages.index[1].page_len = k * 2;
4394 lun->log_pages.index[2].page_data = (uint8_t *)&lun->log_pages.temp_page;
4395 lun->log_pages.index[2].page_len = sizeof(lun->log_pages.temp_page);
4396 lun->log_pages.index[3].page_data = &lun->log_pages.lbp_page[0];
4397 lun->log_pages.index[3].page_len = 12*CTL_NUM_LBP_PARAMS;
4398 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.stat_page;
4399 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.stat_page);
4400 lun->log_pages.index[5].page_data = (uint8_t *)&lun->log_pages.ie_page;
4401 lun->log_pages.index[5].page_len = sizeof(lun->log_pages.ie_page);
4402
4403 return (CTL_RETVAL_COMPLETE);
4404 }
4405
4406 static int
hex2bin(const char * str,uint8_t * buf,int buf_size)4407 hex2bin(const char *str, uint8_t *buf, int buf_size)
4408 {
4409 int i;
4410 u_char c;
4411
4412 memset(buf, 0, buf_size);
4413 while (isspace(str[0]))
4414 str++;
4415 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4416 str += 2;
4417 buf_size *= 2;
4418 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4419 while (str[i] == '-') /* Skip dashes in UUIDs. */
4420 str++;
4421 c = str[i];
4422 if (isdigit(c))
4423 c -= '0';
4424 else if (isalpha(c))
4425 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4426 else
4427 break;
4428 if (c >= 16)
4429 break;
4430 if ((i & 1) == 0)
4431 buf[i / 2] |= (c << 4);
4432 else
4433 buf[i / 2] |= c;
4434 }
4435 return ((i + 1) / 2);
4436 }
4437
4438 /*
4439 * Add LUN.
4440 *
4441 * Returns 0 for success, non-zero (errno) for failure.
4442 */
4443 int
ctl_add_lun(struct ctl_be_lun * be_lun)4444 ctl_add_lun(struct ctl_be_lun *be_lun)
4445 {
4446 struct ctl_softc *ctl_softc = control_softc;
4447 struct ctl_lun *nlun, *lun;
4448 struct scsi_vpd_id_descriptor *desc;
4449 struct scsi_vpd_id_t10 *t10id;
4450 const char *eui, *naa, *scsiname, *uuid, *vendor, *value;
4451 int lun_number;
4452 int devidlen, idlen1, idlen2 = 0, len;
4453
4454 /*
4455 * We support only Direct Access, CD-ROM or Processor LUN types.
4456 */
4457 switch (be_lun->lun_type) {
4458 case T_DIRECT:
4459 case T_PROCESSOR:
4460 case T_CDROM:
4461 break;
4462 case T_SEQUENTIAL:
4463 case T_CHANGER:
4464 default:
4465 return (EINVAL);
4466 }
4467 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK | M_ZERO);
4468
4469 lun->pending_sense = malloc(sizeof(struct scsi_sense_data *) *
4470 ctl_max_ports, M_DEVBUF, M_WAITOK | M_ZERO);
4471 lun->pending_ua = malloc(sizeof(ctl_ua_type *) * ctl_max_ports,
4472 M_DEVBUF, M_WAITOK | M_ZERO);
4473 lun->pr_keys = malloc(sizeof(uint64_t *) * ctl_max_ports,
4474 M_DEVBUF, M_WAITOK | M_ZERO);
4475
4476 /* Generate LUN ID. */
4477 devidlen = max(CTL_DEVID_MIN_LEN,
4478 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4479 idlen1 = sizeof(*t10id) + devidlen;
4480 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4481 scsiname = dnvlist_get_string(be_lun->options, "scsiname", NULL);
4482 if (scsiname != NULL) {
4483 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4484 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4485 }
4486 eui = dnvlist_get_string(be_lun->options, "eui", NULL);
4487 if (eui != NULL) {
4488 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4489 }
4490 naa = dnvlist_get_string(be_lun->options, "naa", NULL);
4491 if (naa != NULL) {
4492 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4493 }
4494 uuid = dnvlist_get_string(be_lun->options, "uuid", NULL);
4495 if (uuid != NULL) {
4496 len += sizeof(struct scsi_vpd_id_descriptor) + 18;
4497 }
4498 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4499 M_CTL, M_WAITOK | M_ZERO);
4500 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4501 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4502 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4503 desc->length = idlen1;
4504 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4505 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4506 if ((vendor = dnvlist_get_string(be_lun->options, "vendor", NULL)) == NULL) {
4507 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4508 } else {
4509 strncpy(t10id->vendor, vendor,
4510 min(sizeof(t10id->vendor), strlen(vendor)));
4511 }
4512 strncpy((char *)t10id->vendor_spec_id,
4513 (char *)be_lun->device_id, devidlen);
4514 if (scsiname != NULL) {
4515 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4516 desc->length);
4517 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4518 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4519 SVPD_ID_TYPE_SCSI_NAME;
4520 desc->length = idlen2;
4521 strlcpy(desc->identifier, scsiname, idlen2);
4522 }
4523 if (eui != NULL) {
4524 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4525 desc->length);
4526 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4527 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4528 SVPD_ID_TYPE_EUI64;
4529 desc->length = hex2bin(eui, desc->identifier, 16);
4530 desc->length = desc->length > 12 ? 16 :
4531 (desc->length > 8 ? 12 : 8);
4532 len -= 16 - desc->length;
4533 }
4534 if (naa != NULL) {
4535 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4536 desc->length);
4537 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4538 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4539 SVPD_ID_TYPE_NAA;
4540 desc->length = hex2bin(naa, desc->identifier, 16);
4541 desc->length = desc->length > 8 ? 16 : 8;
4542 len -= 16 - desc->length;
4543 }
4544 if (uuid != NULL) {
4545 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4546 desc->length);
4547 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4548 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4549 SVPD_ID_TYPE_UUID;
4550 desc->identifier[0] = 0x10;
4551 hex2bin(uuid, &desc->identifier[2], 16);
4552 desc->length = 18;
4553 }
4554 lun->lun_devid->len = len;
4555
4556 mtx_lock(&ctl_softc->ctl_lock);
4557 /*
4558 * See if the caller requested a particular LUN number. If so, see
4559 * if it is available. Otherwise, allocate the first available LUN.
4560 */
4561 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4562 if ((be_lun->req_lun_id > (ctl_max_luns - 1))
4563 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4564 mtx_unlock(&ctl_softc->ctl_lock);
4565 if (be_lun->req_lun_id > (ctl_max_luns - 1)) {
4566 printf("ctl: requested LUN ID %d is higher "
4567 "than ctl_max_luns - 1 (%d)\n",
4568 be_lun->req_lun_id, ctl_max_luns - 1);
4569 } else {
4570 /*
4571 * XXX KDM return an error, or just assign
4572 * another LUN ID in this case??
4573 */
4574 printf("ctl: requested LUN ID %d is already "
4575 "in use\n", be_lun->req_lun_id);
4576 }
4577 fail:
4578 free(lun->lun_devid, M_CTL);
4579 free(lun, M_CTL);
4580 return (ENOSPC);
4581 }
4582 lun_number = be_lun->req_lun_id;
4583 } else {
4584 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, ctl_max_luns);
4585 if (lun_number == -1) {
4586 mtx_unlock(&ctl_softc->ctl_lock);
4587 printf("ctl: can't allocate LUN, out of LUNs\n");
4588 goto fail;
4589 }
4590 }
4591 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4592 mtx_unlock(&ctl_softc->ctl_lock);
4593
4594 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4595 lun->lun = lun_number;
4596 lun->be_lun = be_lun;
4597 /*
4598 * The processor LUN is always enabled. Disk LUNs come on line
4599 * disabled, and must be enabled by the backend.
4600 */
4601 lun->flags |= CTL_LUN_DISABLED;
4602 lun->backend = be_lun->be;
4603 be_lun->ctl_lun = lun;
4604 be_lun->lun_id = lun_number;
4605 if (be_lun->flags & CTL_LUN_FLAG_EJECTED)
4606 lun->flags |= CTL_LUN_EJECTED;
4607 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA)
4608 lun->flags |= CTL_LUN_NO_MEDIA;
4609 if (be_lun->flags & CTL_LUN_FLAG_STOPPED)
4610 lun->flags |= CTL_LUN_STOPPED;
4611
4612 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4613 lun->flags |= CTL_LUN_PRIMARY_SC;
4614
4615 value = dnvlist_get_string(be_lun->options, "removable", NULL);
4616 if (value != NULL) {
4617 if (strcmp(value, "on") == 0)
4618 lun->flags |= CTL_LUN_REMOVABLE;
4619 } else if (be_lun->lun_type == T_CDROM)
4620 lun->flags |= CTL_LUN_REMOVABLE;
4621
4622 lun->ctl_softc = ctl_softc;
4623 #ifdef CTL_TIME_IO
4624 lun->last_busy = getsbinuptime();
4625 #endif
4626 LIST_INIT(&lun->ooa_queue);
4627 STAILQ_INIT(&lun->error_list);
4628 lun->ie_reported = 1;
4629 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0);
4630 ctl_tpc_lun_init(lun);
4631 if (lun->flags & CTL_LUN_REMOVABLE) {
4632 lun->prevent = malloc((CTL_MAX_INITIATORS + 31) / 32 * 4,
4633 M_CTL, M_WAITOK | M_ZERO);
4634 }
4635
4636 /*
4637 * Initialize the mode and log page index.
4638 */
4639 ctl_init_page_index(lun);
4640 ctl_init_log_page_index(lun);
4641
4642 /* Setup statistics gathering */
4643 lun->stats.item = lun_number;
4644
4645 /*
4646 * Now, before we insert this lun on the lun list, set the lun
4647 * inventory changed UA for all other luns.
4648 */
4649 mtx_lock(&ctl_softc->ctl_lock);
4650 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4651 mtx_lock(&nlun->lun_lock);
4652 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4653 mtx_unlock(&nlun->lun_lock);
4654 }
4655 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4656 ctl_softc->ctl_luns[lun_number] = lun;
4657 ctl_softc->num_luns++;
4658 mtx_unlock(&ctl_softc->ctl_lock);
4659
4660 /*
4661 * We successfully added the LUN, attempt to enable it.
4662 */
4663 if (ctl_enable_lun(lun) != 0) {
4664 printf("%s: ctl_enable_lun() failed!\n", __func__);
4665 mtx_lock(&ctl_softc->ctl_lock);
4666 STAILQ_REMOVE(&ctl_softc->lun_list, lun, ctl_lun, links);
4667 ctl_clear_mask(ctl_softc->ctl_lun_mask, lun_number);
4668 ctl_softc->ctl_luns[lun_number] = NULL;
4669 ctl_softc->num_luns--;
4670 mtx_unlock(&ctl_softc->ctl_lock);
4671 free(lun->lun_devid, M_CTL);
4672 free(lun, M_CTL);
4673 return (EIO);
4674 }
4675
4676 return (0);
4677 }
4678
4679 /*
4680 * Free LUN that has no active requests.
4681 */
4682 static int
ctl_free_lun(struct ctl_lun * lun)4683 ctl_free_lun(struct ctl_lun *lun)
4684 {
4685 struct ctl_softc *softc = lun->ctl_softc;
4686 struct ctl_lun *nlun;
4687 int i;
4688
4689 KASSERT(LIST_EMPTY(&lun->ooa_queue),
4690 ("Freeing a LUN %p with outstanding I/O!\n", lun));
4691
4692 mtx_lock(&softc->ctl_lock);
4693 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4694 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4695 softc->ctl_luns[lun->lun] = NULL;
4696 softc->num_luns--;
4697 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4698 mtx_lock(&nlun->lun_lock);
4699 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4700 mtx_unlock(&nlun->lun_lock);
4701 }
4702 mtx_unlock(&softc->ctl_lock);
4703
4704 /*
4705 * Tell the backend to free resources, if this LUN has a backend.
4706 */
4707 lun->be_lun->lun_shutdown(lun->be_lun);
4708
4709 lun->ie_reportcnt = UINT32_MAX;
4710 callout_drain(&lun->ie_callout);
4711 ctl_tpc_lun_shutdown(lun);
4712 mtx_destroy(&lun->lun_lock);
4713 free(lun->lun_devid, M_CTL);
4714 for (i = 0; i < ctl_max_ports; i++)
4715 free(lun->pending_ua[i], M_CTL);
4716 free(lun->pending_ua, M_DEVBUF);
4717 for (i = 0; i < ctl_max_ports; i++)
4718 free(lun->pr_keys[i], M_CTL);
4719 free(lun->pr_keys, M_DEVBUF);
4720 free(lun->write_buffer, M_CTL);
4721 free(lun->prevent, M_CTL);
4722 free(lun, M_CTL);
4723
4724 return (0);
4725 }
4726
4727 static int
ctl_enable_lun(struct ctl_lun * lun)4728 ctl_enable_lun(struct ctl_lun *lun)
4729 {
4730 struct ctl_softc *softc;
4731 struct ctl_port *port, *nport;
4732 int retval;
4733
4734 softc = lun->ctl_softc;
4735
4736 mtx_lock(&softc->ctl_lock);
4737 mtx_lock(&lun->lun_lock);
4738 KASSERT((lun->flags & CTL_LUN_DISABLED) != 0,
4739 ("%s: LUN not disabled", __func__));
4740 lun->flags &= ~CTL_LUN_DISABLED;
4741 mtx_unlock(&lun->lun_lock);
4742
4743 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) {
4744 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4745 port->lun_map != NULL || port->lun_enable == NULL)
4746 continue;
4747
4748 /*
4749 * Drop the lock while we call the FETD's enable routine.
4750 * This can lead to a callback into CTL (at least in the
4751 * case of the internal initiator frontend.
4752 */
4753 mtx_unlock(&softc->ctl_lock);
4754 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4755 mtx_lock(&softc->ctl_lock);
4756 if (retval != 0) {
4757 printf("%s: FETD %s port %d returned error "
4758 "%d for lun_enable on lun %jd\n",
4759 __func__, port->port_name, port->targ_port,
4760 retval, (intmax_t)lun->lun);
4761 }
4762 }
4763
4764 mtx_unlock(&softc->ctl_lock);
4765 ctl_isc_announce_lun(lun);
4766
4767 return (0);
4768 }
4769
4770 static int
ctl_disable_lun(struct ctl_lun * lun)4771 ctl_disable_lun(struct ctl_lun *lun)
4772 {
4773 struct ctl_softc *softc;
4774 struct ctl_port *port;
4775 int retval;
4776
4777 softc = lun->ctl_softc;
4778
4779 mtx_lock(&softc->ctl_lock);
4780 mtx_lock(&lun->lun_lock);
4781 KASSERT((lun->flags & CTL_LUN_DISABLED) == 0,
4782 ("%s: LUN not enabled", __func__));
4783 lun->flags |= CTL_LUN_DISABLED;
4784 mtx_unlock(&lun->lun_lock);
4785
4786 STAILQ_FOREACH(port, &softc->port_list, links) {
4787 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4788 port->lun_map != NULL || port->lun_disable == NULL)
4789 continue;
4790
4791 /*
4792 * Drop the lock before we call the frontend's disable
4793 * routine, to avoid lock order reversals.
4794 *
4795 * XXX KDM what happens if the frontend list changes while
4796 * we're traversing it? It's unlikely, but should be handled.
4797 */
4798 mtx_unlock(&softc->ctl_lock);
4799 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4800 mtx_lock(&softc->ctl_lock);
4801 if (retval != 0) {
4802 printf("%s: FETD %s port %d returned error "
4803 "%d for lun_disable on lun %jd\n",
4804 __func__, port->port_name, port->targ_port,
4805 retval, (intmax_t)lun->lun);
4806 }
4807 }
4808
4809 mtx_unlock(&softc->ctl_lock);
4810 ctl_isc_announce_lun(lun);
4811
4812 return (0);
4813 }
4814
4815 int
ctl_start_lun(struct ctl_be_lun * be_lun)4816 ctl_start_lun(struct ctl_be_lun *be_lun)
4817 {
4818 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4819
4820 mtx_lock(&lun->lun_lock);
4821 lun->flags &= ~CTL_LUN_STOPPED;
4822 mtx_unlock(&lun->lun_lock);
4823 return (0);
4824 }
4825
4826 int
ctl_stop_lun(struct ctl_be_lun * be_lun)4827 ctl_stop_lun(struct ctl_be_lun *be_lun)
4828 {
4829 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4830
4831 mtx_lock(&lun->lun_lock);
4832 lun->flags |= CTL_LUN_STOPPED;
4833 mtx_unlock(&lun->lun_lock);
4834 return (0);
4835 }
4836
4837 int
ctl_lun_no_media(struct ctl_be_lun * be_lun)4838 ctl_lun_no_media(struct ctl_be_lun *be_lun)
4839 {
4840 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4841
4842 mtx_lock(&lun->lun_lock);
4843 lun->flags |= CTL_LUN_NO_MEDIA;
4844 mtx_unlock(&lun->lun_lock);
4845 return (0);
4846 }
4847
4848 int
ctl_lun_has_media(struct ctl_be_lun * be_lun)4849 ctl_lun_has_media(struct ctl_be_lun *be_lun)
4850 {
4851 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4852 union ctl_ha_msg msg;
4853
4854 mtx_lock(&lun->lun_lock);
4855 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED);
4856 if (lun->flags & CTL_LUN_REMOVABLE)
4857 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE);
4858 mtx_unlock(&lun->lun_lock);
4859 if ((lun->flags & CTL_LUN_REMOVABLE) &&
4860 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4861 bzero(&msg.ua, sizeof(msg.ua));
4862 msg.hdr.msg_type = CTL_MSG_UA;
4863 msg.hdr.nexus.initid = -1;
4864 msg.hdr.nexus.targ_port = -1;
4865 msg.hdr.nexus.targ_lun = lun->lun;
4866 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4867 msg.ua.ua_all = 1;
4868 msg.ua.ua_set = 1;
4869 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE;
4870 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4871 M_WAITOK);
4872 }
4873 return (0);
4874 }
4875
4876 int
ctl_lun_ejected(struct ctl_be_lun * be_lun)4877 ctl_lun_ejected(struct ctl_be_lun *be_lun)
4878 {
4879 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4880
4881 mtx_lock(&lun->lun_lock);
4882 lun->flags |= CTL_LUN_EJECTED;
4883 mtx_unlock(&lun->lun_lock);
4884 return (0);
4885 }
4886
4887 int
ctl_lun_primary(struct ctl_be_lun * be_lun)4888 ctl_lun_primary(struct ctl_be_lun *be_lun)
4889 {
4890 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4891
4892 mtx_lock(&lun->lun_lock);
4893 lun->flags |= CTL_LUN_PRIMARY_SC;
4894 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4895 mtx_unlock(&lun->lun_lock);
4896 ctl_isc_announce_lun(lun);
4897 return (0);
4898 }
4899
4900 int
ctl_lun_secondary(struct ctl_be_lun * be_lun)4901 ctl_lun_secondary(struct ctl_be_lun *be_lun)
4902 {
4903 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4904
4905 mtx_lock(&lun->lun_lock);
4906 lun->flags &= ~CTL_LUN_PRIMARY_SC;
4907 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4908 mtx_unlock(&lun->lun_lock);
4909 ctl_isc_announce_lun(lun);
4910 return (0);
4911 }
4912
4913 /*
4914 * Remove LUN. If there are active requests, wait for completion.
4915 *
4916 * Returns 0 for success, non-zero (errno) for failure.
4917 * Completion is reported to backed via the lun_shutdown() method.
4918 */
4919 int
ctl_remove_lun(struct ctl_be_lun * be_lun)4920 ctl_remove_lun(struct ctl_be_lun *be_lun)
4921 {
4922 struct ctl_lun *lun;
4923
4924 lun = (struct ctl_lun *)be_lun->ctl_lun;
4925
4926 ctl_disable_lun(lun);
4927
4928 mtx_lock(&lun->lun_lock);
4929 lun->flags |= CTL_LUN_INVALID;
4930
4931 /*
4932 * If there is nothing in the OOA queue, go ahead and free the LUN.
4933 * If we have something in the OOA queue, we'll free it when the
4934 * last I/O completes.
4935 */
4936 if (LIST_EMPTY(&lun->ooa_queue)) {
4937 mtx_unlock(&lun->lun_lock);
4938 ctl_free_lun(lun);
4939 } else
4940 mtx_unlock(&lun->lun_lock);
4941
4942 return (0);
4943 }
4944
4945 void
ctl_lun_capacity_changed(struct ctl_be_lun * be_lun)4946 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4947 {
4948 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4949 union ctl_ha_msg msg;
4950
4951 mtx_lock(&lun->lun_lock);
4952 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE);
4953 mtx_unlock(&lun->lun_lock);
4954 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4955 /* Send msg to other side. */
4956 bzero(&msg.ua, sizeof(msg.ua));
4957 msg.hdr.msg_type = CTL_MSG_UA;
4958 msg.hdr.nexus.initid = -1;
4959 msg.hdr.nexus.targ_port = -1;
4960 msg.hdr.nexus.targ_lun = lun->lun;
4961 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4962 msg.ua.ua_all = 1;
4963 msg.ua.ua_set = 1;
4964 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE;
4965 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4966 M_WAITOK);
4967 }
4968 }
4969
4970 void
ctl_lun_nsdata_ids(struct ctl_be_lun * be_lun,struct nvme_namespace_data * nsdata)4971 ctl_lun_nsdata_ids(struct ctl_be_lun *be_lun,
4972 struct nvme_namespace_data *nsdata)
4973 {
4974 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4975 struct scsi_vpd_id_descriptor *idd;
4976
4977 if (lun->lun_devid == NULL)
4978 return;
4979
4980 idd = scsi_get_devid_desc((struct scsi_vpd_id_descriptor *)
4981 lun->lun_devid->data, lun->lun_devid->len, scsi_devid_is_lun_naa);
4982 if (idd != NULL) {
4983 if (idd->length == 16) {
4984 memcpy(nsdata->nguid, idd->identifier, 16);
4985 return;
4986 }
4987 if (idd->length == 8) {
4988 memcpy(nsdata->eui64, idd->identifier, 8);
4989 return;
4990 }
4991 }
4992
4993 idd = scsi_get_devid_desc((struct scsi_vpd_id_descriptor *)
4994 lun->lun_devid->data, lun->lun_devid->len, scsi_devid_is_lun_eui64);
4995 if (idd != NULL) {
4996 if (idd->length == 8) {
4997 memcpy(nsdata->eui64, idd->identifier, 8);
4998 return;
4999 }
5000 }
5001 }
5002
5003 void
ctl_lun_nvme_ids(struct ctl_be_lun * be_lun,void * data)5004 ctl_lun_nvme_ids(struct ctl_be_lun *be_lun, void *data)
5005 {
5006 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5007 struct scsi_vpd_id_descriptor *naa, *eui64, *uuid;
5008 char *p;
5009
5010 memset(data, 0, 4096);
5011
5012 if (lun->lun_devid == NULL)
5013 return;
5014
5015 naa = scsi_get_devid_desc((struct scsi_vpd_id_descriptor *)
5016 lun->lun_devid->data, lun->lun_devid->len, scsi_devid_is_lun_naa);
5017 eui64 = scsi_get_devid_desc((struct scsi_vpd_id_descriptor *)
5018 lun->lun_devid->data, lun->lun_devid->len, scsi_devid_is_lun_eui64);
5019 uuid = scsi_get_devid_desc((struct scsi_vpd_id_descriptor *)
5020 lun->lun_devid->data, lun->lun_devid->len, scsi_devid_is_lun_uuid);
5021
5022 p = data;
5023
5024 /* EUI64 */
5025 if ((naa != NULL && naa->length == 8) || eui64 != NULL) {
5026 *p++ = 1;
5027 *p++ = 8;
5028 p += 2;
5029 if (naa != NULL && naa->length == 8)
5030 memcpy(p, naa->identifier, 8);
5031 else
5032 memcpy(p, eui64->identifier, 8);
5033 p += 8;
5034 }
5035
5036 /* NGUID */
5037 if (naa != NULL && naa->length == 16) {
5038 *p++ = 1;
5039 *p++ = 16;
5040 p += 2;
5041 memcpy(p, naa->identifier, 16);
5042 p += 16;
5043 }
5044
5045 /* UUID */
5046 if (uuid != NULL) {
5047 *p++ = 1;
5048 *p++ = uuid->length;
5049 p += 2;
5050 memcpy(p, uuid->identifier, uuid->length);
5051 p += uuid->length;
5052 }
5053 }
5054
5055 /*
5056 * Backend "memory move is complete" callback for requests that never
5057 * make it down to say RAIDCore's configuration code.
5058 */
5059 int
ctl_config_move_done(union ctl_io * io,bool samethr)5060 ctl_config_move_done(union ctl_io *io, bool samethr)
5061 {
5062 int retval;
5063
5064 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5065
5066 if (ctl_debug & CTL_DEBUG_CDB_DATA)
5067 ctl_data_print(io);
5068 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
5069 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5070 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
5071 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5072 /*
5073 * XXX KDM just assuming a single pointer here, and not a
5074 * S/G list. If we start using S/G lists for config data,
5075 * we'll need to know how to clean them up here as well.
5076 */
5077 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5078 free(ctl_kern_data_ptr(io), M_CTL);
5079 ctl_done(io);
5080 retval = CTL_RETVAL_COMPLETE;
5081 } else {
5082 /*
5083 * XXX KDM now we need to continue data movement. Some
5084 * options:
5085 * - call ctl_scsiio() again? We don't do this for data
5086 * writes, because for those at least we know ahead of
5087 * time where the write will go and how long it is. For
5088 * config writes, though, that information is largely
5089 * contained within the write itself, thus we need to
5090 * parse out the data again.
5091 *
5092 * - Call some other function once the data is in?
5093 */
5094
5095 /*
5096 * XXX KDM call ctl_scsiio() again for now, and check flag
5097 * bits to see whether we're allocated or not.
5098 */
5099 switch (io->io_hdr.io_type) {
5100 case CTL_IO_SCSI:
5101 retval = ctl_scsiio(&io->scsiio);
5102 break;
5103 case CTL_IO_NVME:
5104 case CTL_IO_NVME_ADMIN:
5105 retval = ctl_nvmeio(&io->nvmeio);
5106 break;
5107 default:
5108 __assert_unreachable();
5109 }
5110 }
5111 return (retval);
5112 }
5113
5114 /*
5115 * This gets called by a backend driver when it is done with a
5116 * data_submit method.
5117 */
5118 void
ctl_data_submit_done(union ctl_io * io)5119 ctl_data_submit_done(union ctl_io *io)
5120 {
5121 /*
5122 * If the IO_CONT flag is set, we need to call the supplied
5123 * function to continue processing the I/O, instead of completing
5124 * the I/O just yet.
5125 *
5126 * If there is an error, though, we don't want to keep processing.
5127 * Instead, just send status back to the initiator.
5128 */
5129 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5130 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5131 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5132 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5133 ctl_continue_io(io);
5134 return;
5135 }
5136 ctl_done(io);
5137 }
5138
5139 /*
5140 * This gets called by a backend driver when it is done with a
5141 * configuration write.
5142 */
5143 void
ctl_config_write_done(union ctl_io * io)5144 ctl_config_write_done(union ctl_io *io)
5145 {
5146 uint8_t *buf;
5147
5148 /*
5149 * If the IO_CONT flag is set, we need to call the supplied
5150 * function to continue processing the I/O, instead of completing
5151 * the I/O just yet.
5152 *
5153 * If there is an error, though, we don't want to keep processing.
5154 * Instead, just send status back to the initiator.
5155 */
5156 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5157 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5158 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5159 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5160 ctl_continue_io(io);
5161 return;
5162 }
5163 /*
5164 * Since a configuration write can be done for commands that actually
5165 * have data allocated, like write buffer, and commands that have
5166 * no data, like start/stop unit, we need to check here.
5167 */
5168 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5169 buf = ctl_kern_data_ptr(io);
5170 else
5171 buf = NULL;
5172 ctl_done(io);
5173 if (buf)
5174 free(buf, M_CTL);
5175 }
5176
5177 void
ctl_config_read_done(union ctl_io * io)5178 ctl_config_read_done(union ctl_io *io)
5179 {
5180 uint8_t *buf;
5181
5182 /*
5183 * If there is some error -- we are done, skip data transfer.
5184 */
5185 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5186 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5187 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5188 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5189 buf = ctl_kern_data_ptr(io);
5190 else
5191 buf = NULL;
5192 ctl_done(io);
5193 if (buf)
5194 free(buf, M_CTL);
5195 return;
5196 }
5197
5198 /*
5199 * If the IO_CONT flag is set, we need to call the supplied
5200 * function to continue processing the I/O, instead of completing
5201 * the I/O just yet.
5202 */
5203 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5204 ctl_continue_io(io);
5205 return;
5206 }
5207
5208 ctl_datamove(io);
5209 }
5210
5211 /*
5212 * SCSI release command.
5213 */
5214 int
ctl_scsi_release(struct ctl_scsiio * ctsio)5215 ctl_scsi_release(struct ctl_scsiio *ctsio)
5216 {
5217 struct ctl_lun *lun = CTL_LUN(ctsio);
5218 uint32_t residx;
5219
5220 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5221
5222 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5223
5224 /*
5225 * XXX KDM right now, we only support LUN reservation. We don't
5226 * support 3rd party reservations, or extent reservations, which
5227 * might actually need the parameter list. If we've gotten this
5228 * far, we've got a LUN reservation. Anything else got kicked out
5229 * above. So, according to SPC, ignore the length.
5230 */
5231
5232 mtx_lock(&lun->lun_lock);
5233
5234 /*
5235 * According to SPC, it is not an error for an intiator to attempt
5236 * to release a reservation on a LUN that isn't reserved, or that
5237 * is reserved by another initiator. The reservation can only be
5238 * released, though, by the initiator who made it or by one of
5239 * several reset type events.
5240 */
5241 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5242 lun->flags &= ~CTL_LUN_RESERVED;
5243
5244 mtx_unlock(&lun->lun_lock);
5245
5246 ctl_set_success(ctsio);
5247 ctl_done((union ctl_io *)ctsio);
5248 return (CTL_RETVAL_COMPLETE);
5249 }
5250
5251 int
ctl_scsi_reserve(struct ctl_scsiio * ctsio)5252 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5253 {
5254 struct ctl_lun *lun = CTL_LUN(ctsio);
5255 uint32_t residx;
5256
5257 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5258
5259 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5260
5261 /*
5262 * XXX KDM right now, we only support LUN reservation. We don't
5263 * support 3rd party reservations, or extent reservations, which
5264 * might actually need the parameter list. If we've gotten this
5265 * far, we've got a LUN reservation. Anything else got kicked out
5266 * above. So, according to SPC, ignore the length.
5267 */
5268
5269 mtx_lock(&lun->lun_lock);
5270 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5271 ctl_set_reservation_conflict(ctsio);
5272 goto bailout;
5273 }
5274
5275 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */
5276 if (lun->flags & CTL_LUN_PR_RESERVED) {
5277 ctl_set_success(ctsio);
5278 goto bailout;
5279 }
5280
5281 lun->flags |= CTL_LUN_RESERVED;
5282 lun->res_idx = residx;
5283 ctl_set_success(ctsio);
5284
5285 bailout:
5286 mtx_unlock(&lun->lun_lock);
5287 ctl_done((union ctl_io *)ctsio);
5288 return (CTL_RETVAL_COMPLETE);
5289 }
5290
5291 int
ctl_start_stop(struct ctl_scsiio * ctsio)5292 ctl_start_stop(struct ctl_scsiio *ctsio)
5293 {
5294 struct ctl_lun *lun = CTL_LUN(ctsio);
5295 struct scsi_start_stop_unit *cdb;
5296 int retval;
5297
5298 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5299
5300 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5301
5302 if ((cdb->how & SSS_PC_MASK) == 0) {
5303 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
5304 (cdb->how & SSS_START) == 0) {
5305 uint32_t residx;
5306
5307 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5308 if (ctl_get_prkey(lun, residx) == 0 ||
5309 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) {
5310 ctl_set_reservation_conflict(ctsio);
5311 ctl_done((union ctl_io *)ctsio);
5312 return (CTL_RETVAL_COMPLETE);
5313 }
5314 }
5315
5316 if ((cdb->how & SSS_LOEJ) &&
5317 (lun->flags & CTL_LUN_REMOVABLE) == 0) {
5318 ctl_set_invalid_field(ctsio,
5319 /*sks_valid*/ 1,
5320 /*command*/ 1,
5321 /*field*/ 4,
5322 /*bit_valid*/ 1,
5323 /*bit*/ 1);
5324 ctl_done((union ctl_io *)ctsio);
5325 return (CTL_RETVAL_COMPLETE);
5326 }
5327
5328 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) &&
5329 lun->prevent_count > 0) {
5330 /* "Medium removal prevented" */
5331 ctl_set_sense(ctsio, /*current_error*/ 1,
5332 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ?
5333 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST,
5334 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE);
5335 ctl_done((union ctl_io *)ctsio);
5336 return (CTL_RETVAL_COMPLETE);
5337 }
5338 }
5339
5340 retval = lun->backend->config_write((union ctl_io *)ctsio);
5341 return (retval);
5342 }
5343
5344 int
ctl_prevent_allow(struct ctl_scsiio * ctsio)5345 ctl_prevent_allow(struct ctl_scsiio *ctsio)
5346 {
5347 struct ctl_lun *lun = CTL_LUN(ctsio);
5348 struct scsi_prevent *cdb;
5349 int retval;
5350 uint32_t initidx;
5351
5352 CTL_DEBUG_PRINT(("ctl_prevent_allow\n"));
5353
5354 cdb = (struct scsi_prevent *)ctsio->cdb;
5355
5356 if ((lun->flags & CTL_LUN_REMOVABLE) == 0 || lun->prevent == NULL) {
5357 ctl_set_invalid_opcode(ctsio);
5358 ctl_done((union ctl_io *)ctsio);
5359 return (CTL_RETVAL_COMPLETE);
5360 }
5361
5362 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5363 mtx_lock(&lun->lun_lock);
5364 if ((cdb->how & PR_PREVENT) &&
5365 ctl_is_set(lun->prevent, initidx) == 0) {
5366 ctl_set_mask(lun->prevent, initidx);
5367 lun->prevent_count++;
5368 } else if ((cdb->how & PR_PREVENT) == 0 &&
5369 ctl_is_set(lun->prevent, initidx)) {
5370 ctl_clear_mask(lun->prevent, initidx);
5371 lun->prevent_count--;
5372 }
5373 mtx_unlock(&lun->lun_lock);
5374 retval = lun->backend->config_write((union ctl_io *)ctsio);
5375 return (retval);
5376 }
5377
5378 /*
5379 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5380 * we don't really do anything with the LBA and length fields if the user
5381 * passes them in. Instead we'll just flush out the cache for the entire
5382 * LUN.
5383 */
5384 int
ctl_sync_cache(struct ctl_scsiio * ctsio)5385 ctl_sync_cache(struct ctl_scsiio *ctsio)
5386 {
5387 struct ctl_lun *lun = CTL_LUN(ctsio);
5388 struct ctl_lba_len_flags *lbalen;
5389 uint64_t starting_lba;
5390 uint32_t block_count;
5391 int retval;
5392 uint8_t byte2;
5393
5394 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5395
5396 retval = 0;
5397
5398 switch (ctsio->cdb[0]) {
5399 case SYNCHRONIZE_CACHE: {
5400 struct scsi_sync_cache *cdb;
5401 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5402
5403 starting_lba = scsi_4btoul(cdb->begin_lba);
5404 block_count = scsi_2btoul(cdb->lb_count);
5405 byte2 = cdb->byte2;
5406 break;
5407 }
5408 case SYNCHRONIZE_CACHE_16: {
5409 struct scsi_sync_cache_16 *cdb;
5410 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5411
5412 starting_lba = scsi_8btou64(cdb->begin_lba);
5413 block_count = scsi_4btoul(cdb->lb_count);
5414 byte2 = cdb->byte2;
5415 break;
5416 }
5417 default:
5418 ctl_set_invalid_opcode(ctsio);
5419 ctl_done((union ctl_io *)ctsio);
5420 goto bailout;
5421 break; /* NOTREACHED */
5422 }
5423
5424 /*
5425 * We check the LBA and length, but don't do anything with them.
5426 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5427 * get flushed. This check will just help satisfy anyone who wants
5428 * to see an error for an out of range LBA.
5429 */
5430 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5431 ctl_set_lba_out_of_range(ctsio,
5432 MAX(starting_lba, lun->be_lun->maxlba + 1));
5433 ctl_done((union ctl_io *)ctsio);
5434 goto bailout;
5435 }
5436
5437 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5438 lbalen->lba = starting_lba;
5439 lbalen->len = block_count;
5440 lbalen->flags = byte2;
5441 retval = lun->backend->config_write((union ctl_io *)ctsio);
5442
5443 bailout:
5444 return (retval);
5445 }
5446
5447 int
ctl_format(struct ctl_scsiio * ctsio)5448 ctl_format(struct ctl_scsiio *ctsio)
5449 {
5450 struct scsi_format *cdb;
5451 int length, defect_list_len;
5452
5453 CTL_DEBUG_PRINT(("ctl_format\n"));
5454
5455 cdb = (struct scsi_format *)ctsio->cdb;
5456
5457 length = 0;
5458 if (cdb->byte2 & SF_FMTDATA) {
5459 if (cdb->byte2 & SF_LONGLIST)
5460 length = sizeof(struct scsi_format_header_long);
5461 else
5462 length = sizeof(struct scsi_format_header_short);
5463 }
5464
5465 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5466 && (length > 0)) {
5467 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5468 ctsio->kern_data_len = length;
5469 ctsio->kern_total_len = length;
5470 ctsio->kern_rel_offset = 0;
5471 ctsio->kern_sg_entries = 0;
5472 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5473 ctsio->be_move_done = ctl_config_move_done;
5474 ctl_datamove((union ctl_io *)ctsio);
5475
5476 return (CTL_RETVAL_COMPLETE);
5477 }
5478
5479 defect_list_len = 0;
5480
5481 if (cdb->byte2 & SF_FMTDATA) {
5482 if (cdb->byte2 & SF_LONGLIST) {
5483 struct scsi_format_header_long *header;
5484
5485 header = (struct scsi_format_header_long *)
5486 ctsio->kern_data_ptr;
5487
5488 defect_list_len = scsi_4btoul(header->defect_list_len);
5489 if (defect_list_len != 0) {
5490 ctl_set_invalid_field(ctsio,
5491 /*sks_valid*/ 1,
5492 /*command*/ 0,
5493 /*field*/ 2,
5494 /*bit_valid*/ 0,
5495 /*bit*/ 0);
5496 goto bailout;
5497 }
5498 } else {
5499 struct scsi_format_header_short *header;
5500
5501 header = (struct scsi_format_header_short *)
5502 ctsio->kern_data_ptr;
5503
5504 defect_list_len = scsi_2btoul(header->defect_list_len);
5505 if (defect_list_len != 0) {
5506 ctl_set_invalid_field(ctsio,
5507 /*sks_valid*/ 1,
5508 /*command*/ 0,
5509 /*field*/ 2,
5510 /*bit_valid*/ 0,
5511 /*bit*/ 0);
5512 goto bailout;
5513 }
5514 }
5515 }
5516
5517 ctl_set_success(ctsio);
5518 bailout:
5519
5520 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5521 free(ctsio->kern_data_ptr, M_CTL);
5522 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5523 }
5524
5525 ctl_done((union ctl_io *)ctsio);
5526 return (CTL_RETVAL_COMPLETE);
5527 }
5528
5529 int
ctl_read_buffer(struct ctl_scsiio * ctsio)5530 ctl_read_buffer(struct ctl_scsiio *ctsio)
5531 {
5532 struct ctl_lun *lun = CTL_LUN(ctsio);
5533 uint64_t buffer_offset;
5534 uint32_t len;
5535 uint8_t byte2;
5536 static uint8_t descr[4];
5537 static uint8_t echo_descr[4] = { 0 };
5538
5539 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5540
5541 switch (ctsio->cdb[0]) {
5542 case READ_BUFFER: {
5543 struct scsi_read_buffer *cdb;
5544
5545 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5546 buffer_offset = scsi_3btoul(cdb->offset);
5547 len = scsi_3btoul(cdb->length);
5548 byte2 = cdb->byte2;
5549 break;
5550 }
5551 case READ_BUFFER_16: {
5552 struct scsi_read_buffer_16 *cdb;
5553
5554 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb;
5555 buffer_offset = scsi_8btou64(cdb->offset);
5556 len = scsi_4btoul(cdb->length);
5557 byte2 = cdb->byte2;
5558 break;
5559 }
5560 default: /* This shouldn't happen. */
5561 ctl_set_invalid_opcode(ctsio);
5562 ctl_done((union ctl_io *)ctsio);
5563 return (CTL_RETVAL_COMPLETE);
5564 }
5565
5566 if (buffer_offset > CTL_WRITE_BUFFER_SIZE ||
5567 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5568 ctl_set_invalid_field(ctsio,
5569 /*sks_valid*/ 1,
5570 /*command*/ 1,
5571 /*field*/ 6,
5572 /*bit_valid*/ 0,
5573 /*bit*/ 0);
5574 ctl_done((union ctl_io *)ctsio);
5575 return (CTL_RETVAL_COMPLETE);
5576 }
5577
5578 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5579 descr[0] = 0;
5580 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5581 ctsio->kern_data_ptr = descr;
5582 len = min(len, sizeof(descr));
5583 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5584 ctsio->kern_data_ptr = echo_descr;
5585 len = min(len, sizeof(echo_descr));
5586 } else {
5587 if (lun->write_buffer == NULL) {
5588 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5589 M_CTL, M_WAITOK | M_ZERO);
5590 }
5591 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5592 }
5593 ctsio->kern_data_len = len;
5594 ctsio->kern_total_len = len;
5595 ctsio->kern_rel_offset = 0;
5596 ctsio->kern_sg_entries = 0;
5597 ctl_set_success(ctsio);
5598 ctsio->be_move_done = ctl_config_move_done;
5599 ctl_datamove((union ctl_io *)ctsio);
5600 return (CTL_RETVAL_COMPLETE);
5601 }
5602
5603 int
ctl_write_buffer(struct ctl_scsiio * ctsio)5604 ctl_write_buffer(struct ctl_scsiio *ctsio)
5605 {
5606 struct ctl_lun *lun = CTL_LUN(ctsio);
5607 struct scsi_write_buffer *cdb;
5608 int buffer_offset, len;
5609
5610 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5611
5612 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5613
5614 len = scsi_3btoul(cdb->length);
5615 buffer_offset = scsi_3btoul(cdb->offset);
5616
5617 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5618 ctl_set_invalid_field(ctsio,
5619 /*sks_valid*/ 1,
5620 /*command*/ 1,
5621 /*field*/ 6,
5622 /*bit_valid*/ 0,
5623 /*bit*/ 0);
5624 ctl_done((union ctl_io *)ctsio);
5625 return (CTL_RETVAL_COMPLETE);
5626 }
5627
5628 if (lun->write_buffer == NULL) {
5629 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5630 M_CTL, M_WAITOK | M_ZERO);
5631 }
5632
5633 /*
5634 * If this kernel request hasn't started yet, initialize the data
5635 * buffer to the correct region of the LUN's write buffer. Note that
5636 * this doesn't set CTL_FLAG_ALLOCATED since this points into a
5637 * persistent buffer belonging to the LUN rather than a buffer
5638 * dedicated to this request.
5639 */
5640 if (ctsio->kern_data_ptr == NULL) {
5641 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5642 ctsio->kern_data_len = len;
5643 ctsio->kern_total_len = len;
5644 ctsio->kern_rel_offset = 0;
5645 ctsio->kern_sg_entries = 0;
5646 ctsio->be_move_done = ctl_config_move_done;
5647 ctl_datamove((union ctl_io *)ctsio);
5648
5649 return (CTL_RETVAL_COMPLETE);
5650 }
5651
5652 ctl_set_success(ctsio);
5653 ctl_done((union ctl_io *)ctsio);
5654 return (CTL_RETVAL_COMPLETE);
5655 }
5656
5657 static int
ctl_write_same_cont(union ctl_io * io)5658 ctl_write_same_cont(union ctl_io *io)
5659 {
5660 struct ctl_lun *lun = CTL_LUN(io);
5661 struct ctl_scsiio *ctsio;
5662 struct ctl_lba_len_flags *lbalen;
5663 int retval;
5664
5665 CTL_IO_ASSERT(io, SCSI);
5666
5667 ctsio = &io->scsiio;
5668 ctsio->io_hdr.status = CTL_STATUS_NONE;
5669 lbalen = (struct ctl_lba_len_flags *)
5670 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5671 lbalen->lba += lbalen->len;
5672 if ((lun->be_lun->maxlba + 1) - lbalen->lba <= UINT32_MAX) {
5673 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
5674 lbalen->len = (lun->be_lun->maxlba + 1) - lbalen->lba;
5675 }
5676
5677 CTL_DEBUG_PRINT(("ctl_write_same_cont: calling config_write()\n"));
5678 retval = lun->backend->config_write((union ctl_io *)ctsio);
5679 return (retval);
5680 }
5681
5682 int
ctl_write_same(struct ctl_scsiio * ctsio)5683 ctl_write_same(struct ctl_scsiio *ctsio)
5684 {
5685 struct ctl_lun *lun = CTL_LUN(ctsio);
5686 struct ctl_lba_len_flags *lbalen;
5687 const char *val;
5688 uint64_t lba, ival;
5689 uint32_t num_blocks;
5690 int len, retval;
5691 uint8_t byte2;
5692
5693 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5694
5695 switch (ctsio->cdb[0]) {
5696 case WRITE_SAME_10: {
5697 struct scsi_write_same_10 *cdb;
5698
5699 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5700
5701 lba = scsi_4btoul(cdb->addr);
5702 num_blocks = scsi_2btoul(cdb->length);
5703 byte2 = cdb->byte2;
5704 break;
5705 }
5706 case WRITE_SAME_16: {
5707 struct scsi_write_same_16 *cdb;
5708
5709 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5710
5711 lba = scsi_8btou64(cdb->addr);
5712 num_blocks = scsi_4btoul(cdb->length);
5713 byte2 = cdb->byte2;
5714 break;
5715 }
5716 default:
5717 /*
5718 * We got a command we don't support. This shouldn't
5719 * happen, commands should be filtered out above us.
5720 */
5721 ctl_set_invalid_opcode(ctsio);
5722 ctl_done((union ctl_io *)ctsio);
5723
5724 return (CTL_RETVAL_COMPLETE);
5725 break; /* NOTREACHED */
5726 }
5727
5728 /* ANCHOR flag can be used only together with UNMAP */
5729 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) {
5730 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5731 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5732 ctl_done((union ctl_io *)ctsio);
5733 return (CTL_RETVAL_COMPLETE);
5734 }
5735
5736 /*
5737 * The first check is to make sure we're in bounds, the second
5738 * check is to catch wrap-around problems. If the lba + num blocks
5739 * is less than the lba, then we've wrapped around and the block
5740 * range is invalid anyway.
5741 */
5742 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5743 || ((lba + num_blocks) < lba)) {
5744 ctl_set_lba_out_of_range(ctsio,
5745 MAX(lba, lun->be_lun->maxlba + 1));
5746 ctl_done((union ctl_io *)ctsio);
5747 return (CTL_RETVAL_COMPLETE);
5748 }
5749
5750 /* Zero number of blocks means "to the last logical block" */
5751 if (num_blocks == 0) {
5752 ival = UINT64_MAX;
5753 val = dnvlist_get_string(lun->be_lun->options,
5754 "write_same_max_lba", NULL);
5755 if (val != NULL)
5756 ctl_expand_number(val, &ival);
5757 if ((lun->be_lun->maxlba + 1) - lba > ival) {
5758 ctl_set_invalid_field(ctsio,
5759 /*sks_valid*/ 1, /*command*/ 1,
5760 /*field*/ ctsio->cdb[0] == WRITE_SAME_10 ? 7 : 10,
5761 /*bit_valid*/ 0, /*bit*/ 0);
5762 ctl_done((union ctl_io *)ctsio);
5763 return (CTL_RETVAL_COMPLETE);
5764 }
5765 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5766 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
5767 ctsio->io_cont = ctl_write_same_cont;
5768 num_blocks = 1 << 31;
5769 } else
5770 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5771 }
5772
5773 len = lun->be_lun->blocksize;
5774
5775 /*
5776 * If we've got a kernel request that hasn't been malloced yet,
5777 * malloc it and tell the caller the data buffer is here.
5778 */
5779 if ((byte2 & SWS_NDOB) == 0 &&
5780 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5781 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5782 ctsio->kern_data_len = len;
5783 ctsio->kern_total_len = len;
5784 ctsio->kern_rel_offset = 0;
5785 ctsio->kern_sg_entries = 0;
5786 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5787 ctsio->be_move_done = ctl_config_move_done;
5788 ctl_datamove((union ctl_io *)ctsio);
5789
5790 return (CTL_RETVAL_COMPLETE);
5791 }
5792
5793 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5794 lbalen->lba = lba;
5795 lbalen->len = num_blocks;
5796 lbalen->flags = byte2;
5797 retval = lun->backend->config_write((union ctl_io *)ctsio);
5798
5799 return (retval);
5800 }
5801
5802 int
ctl_unmap(struct ctl_scsiio * ctsio)5803 ctl_unmap(struct ctl_scsiio *ctsio)
5804 {
5805 struct ctl_lun *lun = CTL_LUN(ctsio);
5806 struct scsi_unmap *cdb;
5807 struct ctl_ptr_len_flags *ptrlen;
5808 struct scsi_unmap_header *hdr;
5809 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5810 uint64_t lba;
5811 uint32_t num_blocks;
5812 int len, retval;
5813 uint8_t byte2;
5814
5815 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5816
5817 cdb = (struct scsi_unmap *)ctsio->cdb;
5818 len = scsi_2btoul(cdb->length);
5819 byte2 = cdb->byte2;
5820
5821 /*
5822 * If we've got a kernel request that hasn't been malloced yet,
5823 * malloc it and tell the caller the data buffer is here.
5824 */
5825 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5826 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5827 ctsio->kern_data_len = len;
5828 ctsio->kern_total_len = len;
5829 ctsio->kern_rel_offset = 0;
5830 ctsio->kern_sg_entries = 0;
5831 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5832 ctsio->be_move_done = ctl_config_move_done;
5833 ctl_datamove((union ctl_io *)ctsio);
5834
5835 return (CTL_RETVAL_COMPLETE);
5836 }
5837
5838 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5839 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5840 if (len < sizeof (*hdr) ||
5841 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5842 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5843 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5844 ctl_set_invalid_field(ctsio,
5845 /*sks_valid*/ 0,
5846 /*command*/ 0,
5847 /*field*/ 0,
5848 /*bit_valid*/ 0,
5849 /*bit*/ 0);
5850 goto done;
5851 }
5852 len = scsi_2btoul(hdr->desc_length);
5853 buf = (struct scsi_unmap_desc *)(hdr + 1);
5854 end = buf + len / sizeof(*buf);
5855
5856 endnz = buf;
5857 for (range = buf; range < end; range++) {
5858 lba = scsi_8btou64(range->lba);
5859 num_blocks = scsi_4btoul(range->length);
5860 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5861 || ((lba + num_blocks) < lba)) {
5862 ctl_set_lba_out_of_range(ctsio,
5863 MAX(lba, lun->be_lun->maxlba + 1));
5864 ctl_done((union ctl_io *)ctsio);
5865 return (CTL_RETVAL_COMPLETE);
5866 }
5867 if (num_blocks != 0)
5868 endnz = range + 1;
5869 }
5870
5871 /*
5872 * Block backend can not handle zero last range.
5873 * Filter it out and return if there is nothing left.
5874 */
5875 len = (uint8_t *)endnz - (uint8_t *)buf;
5876 if (len == 0) {
5877 ctl_set_success(ctsio);
5878 goto done;
5879 }
5880
5881 mtx_lock(&lun->lun_lock);
5882 ptrlen = (struct ctl_ptr_len_flags *)
5883 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5884 ptrlen->ptr = (void *)buf;
5885 ptrlen->len = len;
5886 ptrlen->flags = byte2;
5887 ctl_try_unblock_others(lun, (union ctl_io *)ctsio, FALSE);
5888 mtx_unlock(&lun->lun_lock);
5889
5890 retval = lun->backend->config_write((union ctl_io *)ctsio);
5891 return (retval);
5892
5893 done:
5894 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5895 free(ctsio->kern_data_ptr, M_CTL);
5896 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5897 }
5898 ctl_done((union ctl_io *)ctsio);
5899 return (CTL_RETVAL_COMPLETE);
5900 }
5901
5902 int
ctl_default_page_handler(struct ctl_scsiio * ctsio,struct ctl_page_index * page_index,uint8_t * page_ptr)5903 ctl_default_page_handler(struct ctl_scsiio *ctsio,
5904 struct ctl_page_index *page_index, uint8_t *page_ptr)
5905 {
5906 struct ctl_lun *lun = CTL_LUN(ctsio);
5907 uint8_t *current_cp;
5908 int set_ua;
5909 uint32_t initidx;
5910
5911 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5912 set_ua = 0;
5913
5914 current_cp = (page_index->page_data + (page_index->page_len *
5915 CTL_PAGE_CURRENT));
5916
5917 mtx_lock(&lun->lun_lock);
5918 if (memcmp(current_cp, page_ptr, page_index->page_len)) {
5919 memcpy(current_cp, page_ptr, page_index->page_len);
5920 set_ua = 1;
5921 }
5922 if (set_ua != 0)
5923 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5924 mtx_unlock(&lun->lun_lock);
5925 if (set_ua) {
5926 ctl_isc_announce_mode(lun,
5927 ctl_get_initindex(&ctsio->io_hdr.nexus),
5928 page_index->page_code, page_index->subpage);
5929 }
5930 return (CTL_RETVAL_COMPLETE);
5931 }
5932
5933 static void
ctl_ie_timer(void * arg)5934 ctl_ie_timer(void *arg)
5935 {
5936 struct ctl_lun *lun = arg;
5937 uint64_t t;
5938
5939 if (lun->ie_asc == 0)
5940 return;
5941
5942 if (lun->MODE_IE.mrie == SIEP_MRIE_UA)
5943 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5944 else
5945 lun->ie_reported = 0;
5946
5947 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) {
5948 lun->ie_reportcnt++;
5949 t = scsi_4btoul(lun->MODE_IE.interval_timer);
5950 if (t == 0 || t == UINT32_MAX)
5951 t = 3000; /* 5 min */
5952 callout_schedule_sbt(&lun->ie_callout, SBT_1S / 10 * t,
5953 SBT_1S / 10, 0);
5954 }
5955 }
5956
5957 int
ctl_ie_page_handler(struct ctl_scsiio * ctsio,struct ctl_page_index * page_index,uint8_t * page_ptr)5958 ctl_ie_page_handler(struct ctl_scsiio *ctsio,
5959 struct ctl_page_index *page_index, uint8_t *page_ptr)
5960 {
5961 struct ctl_lun *lun = CTL_LUN(ctsio);
5962 struct scsi_info_exceptions_page *pg;
5963 uint64_t t;
5964
5965 (void)ctl_default_page_handler(ctsio, page_index, page_ptr);
5966
5967 pg = (struct scsi_info_exceptions_page *)page_ptr;
5968 mtx_lock(&lun->lun_lock);
5969 if (pg->info_flags & SIEP_FLAGS_TEST) {
5970 lun->ie_asc = 0x5d;
5971 lun->ie_ascq = 0xff;
5972 if (pg->mrie == SIEP_MRIE_UA) {
5973 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5974 lun->ie_reported = 1;
5975 } else {
5976 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
5977 lun->ie_reported = -1;
5978 }
5979 lun->ie_reportcnt = 1;
5980 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) {
5981 lun->ie_reportcnt++;
5982 t = scsi_4btoul(pg->interval_timer);
5983 if (t == 0 || t == UINT32_MAX)
5984 t = 3000; /* 5 min */
5985 callout_reset_sbt(&lun->ie_callout, SBT_1S / 10 * t,
5986 SBT_1S / 10, ctl_ie_timer, lun, 0);
5987 }
5988 } else {
5989 lun->ie_asc = 0;
5990 lun->ie_ascq = 0;
5991 lun->ie_reported = 1;
5992 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
5993 lun->ie_reportcnt = UINT32_MAX;
5994 callout_stop(&lun->ie_callout);
5995 }
5996 mtx_unlock(&lun->lun_lock);
5997 return (CTL_RETVAL_COMPLETE);
5998 }
5999
6000 static int
ctl_do_mode_select(union ctl_io * io)6001 ctl_do_mode_select(union ctl_io *io)
6002 {
6003 struct ctl_lun *lun = CTL_LUN(io);
6004 struct scsi_mode_page_header *page_header;
6005 struct ctl_page_index *page_index;
6006 struct ctl_scsiio *ctsio;
6007 int page_len, page_len_offset, page_len_size;
6008 union ctl_modepage_info *modepage_info;
6009 uint16_t *len_left, *len_used;
6010 int retval, i;
6011
6012 CTL_IO_ASSERT(io, SCSI);
6013
6014 ctsio = &io->scsiio;
6015 page_index = NULL;
6016 page_len = 0;
6017
6018 modepage_info = (union ctl_modepage_info *)
6019 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6020 len_left = &modepage_info->header.len_left;
6021 len_used = &modepage_info->header.len_used;
6022
6023 do_next_page:
6024
6025 page_header = (struct scsi_mode_page_header *)
6026 (ctsio->kern_data_ptr + *len_used);
6027
6028 if (*len_left == 0) {
6029 free(ctsio->kern_data_ptr, M_CTL);
6030 ctl_set_success(ctsio);
6031 ctl_done((union ctl_io *)ctsio);
6032 return (CTL_RETVAL_COMPLETE);
6033 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6034 free(ctsio->kern_data_ptr, M_CTL);
6035 ctl_set_param_len_error(ctsio);
6036 ctl_done((union ctl_io *)ctsio);
6037 return (CTL_RETVAL_COMPLETE);
6038
6039 } else if ((page_header->page_code & SMPH_SPF)
6040 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6041 free(ctsio->kern_data_ptr, M_CTL);
6042 ctl_set_param_len_error(ctsio);
6043 ctl_done((union ctl_io *)ctsio);
6044 return (CTL_RETVAL_COMPLETE);
6045 }
6046
6047 /*
6048 * XXX KDM should we do something with the block descriptor?
6049 */
6050 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6051 page_index = &lun->mode_pages.index[i];
6052 if (lun->be_lun->lun_type == T_DIRECT &&
6053 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6054 continue;
6055 if (lun->be_lun->lun_type == T_PROCESSOR &&
6056 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6057 continue;
6058 if (lun->be_lun->lun_type == T_CDROM &&
6059 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6060 continue;
6061
6062 if ((page_index->page_code & SMPH_PC_MASK) !=
6063 (page_header->page_code & SMPH_PC_MASK))
6064 continue;
6065
6066 /*
6067 * If neither page has a subpage code, then we've got a
6068 * match.
6069 */
6070 if (((page_index->page_code & SMPH_SPF) == 0)
6071 && ((page_header->page_code & SMPH_SPF) == 0)) {
6072 page_len = page_header->page_length;
6073 break;
6074 }
6075
6076 /*
6077 * If both pages have subpages, then the subpage numbers
6078 * have to match.
6079 */
6080 if ((page_index->page_code & SMPH_SPF)
6081 && (page_header->page_code & SMPH_SPF)) {
6082 struct scsi_mode_page_header_sp *sph;
6083
6084 sph = (struct scsi_mode_page_header_sp *)page_header;
6085 if (page_index->subpage == sph->subpage) {
6086 page_len = scsi_2btoul(sph->page_length);
6087 break;
6088 }
6089 }
6090 }
6091
6092 /*
6093 * If we couldn't find the page, or if we don't have a mode select
6094 * handler for it, send back an error to the user.
6095 */
6096 if ((i >= CTL_NUM_MODE_PAGES)
6097 || (page_index->select_handler == NULL)) {
6098 ctl_set_invalid_field(ctsio,
6099 /*sks_valid*/ 1,
6100 /*command*/ 0,
6101 /*field*/ *len_used,
6102 /*bit_valid*/ 0,
6103 /*bit*/ 0);
6104 free(ctsio->kern_data_ptr, M_CTL);
6105 ctl_done((union ctl_io *)ctsio);
6106 return (CTL_RETVAL_COMPLETE);
6107 }
6108
6109 if (page_index->page_code & SMPH_SPF) {
6110 page_len_offset = 2;
6111 page_len_size = 2;
6112 } else {
6113 page_len_size = 1;
6114 page_len_offset = 1;
6115 }
6116
6117 /*
6118 * If the length the initiator gives us isn't the one we specify in
6119 * the mode page header, or if they didn't specify enough data in
6120 * the CDB to avoid truncating this page, kick out the request.
6121 */
6122 if (page_len != page_index->page_len - page_len_offset - page_len_size) {
6123 ctl_set_invalid_field(ctsio,
6124 /*sks_valid*/ 1,
6125 /*command*/ 0,
6126 /*field*/ *len_used + page_len_offset,
6127 /*bit_valid*/ 0,
6128 /*bit*/ 0);
6129 free(ctsio->kern_data_ptr, M_CTL);
6130 ctl_done((union ctl_io *)ctsio);
6131 return (CTL_RETVAL_COMPLETE);
6132 }
6133 if (*len_left < page_index->page_len) {
6134 free(ctsio->kern_data_ptr, M_CTL);
6135 ctl_set_param_len_error(ctsio);
6136 ctl_done((union ctl_io *)ctsio);
6137 return (CTL_RETVAL_COMPLETE);
6138 }
6139
6140 /*
6141 * Run through the mode page, checking to make sure that the bits
6142 * the user changed are actually legal for him to change.
6143 */
6144 for (i = 0; i < page_index->page_len; i++) {
6145 uint8_t *user_byte, *change_mask, *current_byte;
6146 int bad_bit;
6147 int j;
6148
6149 user_byte = (uint8_t *)page_header + i;
6150 change_mask = page_index->page_data +
6151 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6152 current_byte = page_index->page_data +
6153 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6154
6155 /*
6156 * Check to see whether the user set any bits in this byte
6157 * that he is not allowed to set.
6158 */
6159 if ((*user_byte & ~(*change_mask)) ==
6160 (*current_byte & ~(*change_mask)))
6161 continue;
6162
6163 /*
6164 * Go through bit by bit to determine which one is illegal.
6165 */
6166 bad_bit = 0;
6167 for (j = 7; j >= 0; j--) {
6168 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6169 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6170 bad_bit = i;
6171 break;
6172 }
6173 }
6174 ctl_set_invalid_field(ctsio,
6175 /*sks_valid*/ 1,
6176 /*command*/ 0,
6177 /*field*/ *len_used + i,
6178 /*bit_valid*/ 1,
6179 /*bit*/ bad_bit);
6180 free(ctsio->kern_data_ptr, M_CTL);
6181 ctl_done((union ctl_io *)ctsio);
6182 return (CTL_RETVAL_COMPLETE);
6183 }
6184
6185 /*
6186 * Decrement these before we call the page handler, since we may
6187 * end up getting called back one way or another before the handler
6188 * returns to this context.
6189 */
6190 *len_left -= page_index->page_len;
6191 *len_used += page_index->page_len;
6192
6193 retval = page_index->select_handler(ctsio, page_index,
6194 (uint8_t *)page_header);
6195
6196 /*
6197 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6198 * wait until this queued command completes to finish processing
6199 * the mode page. If it returns anything other than
6200 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6201 * already set the sense information, freed the data pointer, and
6202 * completed the io for us.
6203 */
6204 if (retval != CTL_RETVAL_COMPLETE)
6205 goto bailout_no_done;
6206
6207 /*
6208 * If the initiator sent us more than one page, parse the next one.
6209 */
6210 if (*len_left > 0)
6211 goto do_next_page;
6212
6213 ctl_set_success(ctsio);
6214 free(ctsio->kern_data_ptr, M_CTL);
6215 ctl_done((union ctl_io *)ctsio);
6216
6217 bailout_no_done:
6218
6219 return (CTL_RETVAL_COMPLETE);
6220
6221 }
6222
6223 int
ctl_mode_select(struct ctl_scsiio * ctsio)6224 ctl_mode_select(struct ctl_scsiio *ctsio)
6225 {
6226 struct ctl_lun *lun = CTL_LUN(ctsio);
6227 union ctl_modepage_info *modepage_info;
6228 int bd_len, i, header_size, param_len, rtd;
6229 uint32_t initidx;
6230
6231 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6232 switch (ctsio->cdb[0]) {
6233 case MODE_SELECT_6: {
6234 struct scsi_mode_select_6 *cdb;
6235
6236 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6237
6238 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6239 param_len = cdb->length;
6240 header_size = sizeof(struct scsi_mode_header_6);
6241 break;
6242 }
6243 case MODE_SELECT_10: {
6244 struct scsi_mode_select_10 *cdb;
6245
6246 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6247
6248 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6249 param_len = scsi_2btoul(cdb->length);
6250 header_size = sizeof(struct scsi_mode_header_10);
6251 break;
6252 }
6253 default:
6254 ctl_set_invalid_opcode(ctsio);
6255 ctl_done((union ctl_io *)ctsio);
6256 return (CTL_RETVAL_COMPLETE);
6257 }
6258
6259 if (rtd) {
6260 if (param_len != 0) {
6261 ctl_set_invalid_field(ctsio, /*sks_valid*/ 0,
6262 /*command*/ 1, /*field*/ 0,
6263 /*bit_valid*/ 0, /*bit*/ 0);
6264 ctl_done((union ctl_io *)ctsio);
6265 return (CTL_RETVAL_COMPLETE);
6266 }
6267
6268 /* Revert to defaults. */
6269 ctl_init_page_index(lun);
6270 mtx_lock(&lun->lun_lock);
6271 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
6272 mtx_unlock(&lun->lun_lock);
6273 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6274 ctl_isc_announce_mode(lun, -1,
6275 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
6276 lun->mode_pages.index[i].subpage);
6277 }
6278 ctl_set_success(ctsio);
6279 ctl_done((union ctl_io *)ctsio);
6280 return (CTL_RETVAL_COMPLETE);
6281 }
6282
6283 /*
6284 * From SPC-3:
6285 * "A parameter list length of zero indicates that the Data-Out Buffer
6286 * shall be empty. This condition shall not be considered as an error."
6287 */
6288 if (param_len == 0) {
6289 ctl_set_success(ctsio);
6290 ctl_done((union ctl_io *)ctsio);
6291 return (CTL_RETVAL_COMPLETE);
6292 }
6293
6294 /*
6295 * Since we'll hit this the first time through, prior to
6296 * allocation, we don't need to free a data buffer here.
6297 */
6298 if (param_len < header_size) {
6299 ctl_set_param_len_error(ctsio);
6300 ctl_done((union ctl_io *)ctsio);
6301 return (CTL_RETVAL_COMPLETE);
6302 }
6303
6304 /*
6305 * Allocate the data buffer and grab the user's data. In theory,
6306 * we shouldn't have to sanity check the parameter list length here
6307 * because the maximum size is 64K. We should be able to malloc
6308 * that much without too many problems.
6309 */
6310 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6311 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6312 ctsio->kern_data_len = param_len;
6313 ctsio->kern_total_len = param_len;
6314 ctsio->kern_rel_offset = 0;
6315 ctsio->kern_sg_entries = 0;
6316 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6317 ctsio->be_move_done = ctl_config_move_done;
6318 ctl_datamove((union ctl_io *)ctsio);
6319
6320 return (CTL_RETVAL_COMPLETE);
6321 }
6322
6323 switch (ctsio->cdb[0]) {
6324 case MODE_SELECT_6: {
6325 struct scsi_mode_header_6 *mh6;
6326
6327 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6328 bd_len = mh6->blk_desc_len;
6329 break;
6330 }
6331 case MODE_SELECT_10: {
6332 struct scsi_mode_header_10 *mh10;
6333
6334 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6335 bd_len = scsi_2btoul(mh10->blk_desc_len);
6336 break;
6337 }
6338 default:
6339 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6340 }
6341
6342 if (param_len < (header_size + bd_len)) {
6343 free(ctsio->kern_data_ptr, M_CTL);
6344 ctl_set_param_len_error(ctsio);
6345 ctl_done((union ctl_io *)ctsio);
6346 return (CTL_RETVAL_COMPLETE);
6347 }
6348
6349 /*
6350 * Set the IO_CONT flag, so that if this I/O gets passed to
6351 * ctl_config_write_done(), it'll get passed back to
6352 * ctl_do_mode_select() for further processing, or completion if
6353 * we're all done.
6354 */
6355 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6356 ctsio->io_cont = ctl_do_mode_select;
6357
6358 modepage_info = (union ctl_modepage_info *)
6359 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6360 memset(modepage_info, 0, sizeof(*modepage_info));
6361 modepage_info->header.len_left = param_len - header_size - bd_len;
6362 modepage_info->header.len_used = header_size + bd_len;
6363
6364 return (ctl_do_mode_select((union ctl_io *)ctsio));
6365 }
6366
6367 int
ctl_mode_sense(struct ctl_scsiio * ctsio)6368 ctl_mode_sense(struct ctl_scsiio *ctsio)
6369 {
6370 struct ctl_lun *lun = CTL_LUN(ctsio);
6371 int pc, page_code, llba, subpage;
6372 int alloc_len, page_len, header_len, bd_len, total_len;
6373 void *block_desc;
6374 struct ctl_page_index *page_index;
6375
6376 llba = 0;
6377
6378 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6379
6380 switch (ctsio->cdb[0]) {
6381 case MODE_SENSE_6: {
6382 struct scsi_mode_sense_6 *cdb;
6383
6384 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6385
6386 header_len = sizeof(struct scsi_mode_hdr_6);
6387 if (cdb->byte2 & SMS_DBD)
6388 bd_len = 0;
6389 else
6390 bd_len = sizeof(struct scsi_mode_block_descr);
6391 header_len += bd_len;
6392
6393 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6394 page_code = cdb->page & SMS_PAGE_CODE;
6395 subpage = cdb->subpage;
6396 alloc_len = cdb->length;
6397 break;
6398 }
6399 case MODE_SENSE_10: {
6400 struct scsi_mode_sense_10 *cdb;
6401
6402 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6403
6404 header_len = sizeof(struct scsi_mode_hdr_10);
6405 if (cdb->byte2 & SMS_DBD) {
6406 bd_len = 0;
6407 } else if (lun->be_lun->lun_type == T_DIRECT) {
6408 if (cdb->byte2 & SMS10_LLBAA) {
6409 llba = 1;
6410 bd_len = sizeof(struct scsi_mode_block_descr_dlong);
6411 } else
6412 bd_len = sizeof(struct scsi_mode_block_descr_dshort);
6413 } else
6414 bd_len = sizeof(struct scsi_mode_block_descr);
6415 header_len += bd_len;
6416
6417 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6418 page_code = cdb->page & SMS_PAGE_CODE;
6419 subpage = cdb->subpage;
6420 alloc_len = scsi_2btoul(cdb->length);
6421 break;
6422 }
6423 default:
6424 ctl_set_invalid_opcode(ctsio);
6425 ctl_done((union ctl_io *)ctsio);
6426 return (CTL_RETVAL_COMPLETE);
6427 break; /* NOTREACHED */
6428 }
6429
6430 /*
6431 * We have to make a first pass through to calculate the size of
6432 * the pages that match the user's query. Then we allocate enough
6433 * memory to hold it, and actually copy the data into the buffer.
6434 */
6435 switch (page_code) {
6436 case SMS_ALL_PAGES_PAGE: {
6437 u_int i;
6438
6439 page_len = 0;
6440
6441 /*
6442 * At the moment, values other than 0 and 0xff here are
6443 * reserved according to SPC-3.
6444 */
6445 if ((subpage != SMS_SUBPAGE_PAGE_0)
6446 && (subpage != SMS_SUBPAGE_ALL)) {
6447 ctl_set_invalid_field(ctsio,
6448 /*sks_valid*/ 1,
6449 /*command*/ 1,
6450 /*field*/ 3,
6451 /*bit_valid*/ 0,
6452 /*bit*/ 0);
6453 ctl_done((union ctl_io *)ctsio);
6454 return (CTL_RETVAL_COMPLETE);
6455 }
6456
6457 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6458 page_index = &lun->mode_pages.index[i];
6459
6460 /* Make sure the page is supported for this dev type */
6461 if (lun->be_lun->lun_type == T_DIRECT &&
6462 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6463 continue;
6464 if (lun->be_lun->lun_type == T_PROCESSOR &&
6465 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6466 continue;
6467 if (lun->be_lun->lun_type == T_CDROM &&
6468 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6469 continue;
6470
6471 /*
6472 * We don't use this subpage if the user didn't
6473 * request all subpages.
6474 */
6475 if ((page_index->subpage != 0)
6476 && (subpage == SMS_SUBPAGE_PAGE_0))
6477 continue;
6478
6479 page_len += page_index->page_len;
6480 }
6481 break;
6482 }
6483 default: {
6484 u_int i;
6485
6486 page_len = 0;
6487
6488 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6489 page_index = &lun->mode_pages.index[i];
6490
6491 /* Make sure the page is supported for this dev type */
6492 if (lun->be_lun->lun_type == T_DIRECT &&
6493 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6494 continue;
6495 if (lun->be_lun->lun_type == T_PROCESSOR &&
6496 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6497 continue;
6498 if (lun->be_lun->lun_type == T_CDROM &&
6499 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6500 continue;
6501
6502 /* Look for the right page code */
6503 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6504 continue;
6505
6506 /* Look for the right subpage or the subpage wildcard*/
6507 if ((page_index->subpage != subpage)
6508 && (subpage != SMS_SUBPAGE_ALL))
6509 continue;
6510
6511 page_len += page_index->page_len;
6512 }
6513
6514 if (page_len == 0) {
6515 ctl_set_invalid_field(ctsio,
6516 /*sks_valid*/ 1,
6517 /*command*/ 1,
6518 /*field*/ 2,
6519 /*bit_valid*/ 1,
6520 /*bit*/ 5);
6521 ctl_done((union ctl_io *)ctsio);
6522 return (CTL_RETVAL_COMPLETE);
6523 }
6524 break;
6525 }
6526 }
6527
6528 total_len = header_len + page_len;
6529
6530 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6531 ctsio->kern_sg_entries = 0;
6532 ctsio->kern_rel_offset = 0;
6533 ctsio->kern_data_len = min(total_len, alloc_len);
6534 ctsio->kern_total_len = ctsio->kern_data_len;
6535
6536 switch (ctsio->cdb[0]) {
6537 case MODE_SENSE_6: {
6538 struct scsi_mode_hdr_6 *header;
6539
6540 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6541
6542 header->datalen = MIN(total_len - 1, 254);
6543 if (lun->be_lun->lun_type == T_DIRECT) {
6544 header->dev_specific = 0x10; /* DPOFUA */
6545 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6546 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6547 header->dev_specific |= 0x80; /* WP */
6548 }
6549 header->block_descr_len = bd_len;
6550 block_desc = &header[1];
6551 break;
6552 }
6553 case MODE_SENSE_10: {
6554 struct scsi_mode_hdr_10 *header;
6555 int datalen;
6556
6557 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6558
6559 datalen = MIN(total_len - 2, 65533);
6560 scsi_ulto2b(datalen, header->datalen);
6561 if (lun->be_lun->lun_type == T_DIRECT) {
6562 header->dev_specific = 0x10; /* DPOFUA */
6563 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6564 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6565 header->dev_specific |= 0x80; /* WP */
6566 }
6567 if (llba)
6568 header->flags |= SMH_LONGLBA;
6569 scsi_ulto2b(bd_len, header->block_descr_len);
6570 block_desc = &header[1];
6571 break;
6572 }
6573 default:
6574 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6575 }
6576
6577 /*
6578 * If we've got a disk, use its blocksize in the block
6579 * descriptor. Otherwise, just set it to 0.
6580 */
6581 if (bd_len > 0) {
6582 if (lun->be_lun->lun_type == T_DIRECT) {
6583 if (llba) {
6584 struct scsi_mode_block_descr_dlong *bd = block_desc;
6585 if (lun->be_lun->maxlba != 0)
6586 scsi_u64to8b(lun->be_lun->maxlba + 1,
6587 bd->num_blocks);
6588 scsi_ulto4b(lun->be_lun->blocksize,
6589 bd->block_len);
6590 } else {
6591 struct scsi_mode_block_descr_dshort *bd = block_desc;
6592 if (lun->be_lun->maxlba != 0)
6593 scsi_ulto4b(MIN(lun->be_lun->maxlba+1,
6594 UINT32_MAX), bd->num_blocks);
6595 scsi_ulto3b(lun->be_lun->blocksize,
6596 bd->block_len);
6597 }
6598 } else {
6599 struct scsi_mode_block_descr *bd = block_desc;
6600 scsi_ulto3b(0, bd->block_len);
6601 }
6602 }
6603
6604 switch (page_code) {
6605 case SMS_ALL_PAGES_PAGE: {
6606 int i, data_used;
6607
6608 data_used = header_len;
6609 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6610 struct ctl_page_index *page_index;
6611
6612 page_index = &lun->mode_pages.index[i];
6613 if (lun->be_lun->lun_type == T_DIRECT &&
6614 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6615 continue;
6616 if (lun->be_lun->lun_type == T_PROCESSOR &&
6617 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6618 continue;
6619 if (lun->be_lun->lun_type == T_CDROM &&
6620 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6621 continue;
6622
6623 /*
6624 * We don't use this subpage if the user didn't
6625 * request all subpages. We already checked (above)
6626 * to make sure the user only specified a subpage
6627 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6628 */
6629 if ((page_index->subpage != 0)
6630 && (subpage == SMS_SUBPAGE_PAGE_0))
6631 continue;
6632
6633 /*
6634 * Call the handler, if it exists, to update the
6635 * page to the latest values.
6636 */
6637 if (page_index->sense_handler != NULL)
6638 page_index->sense_handler(ctsio, page_index,pc);
6639
6640 memcpy(ctsio->kern_data_ptr + data_used,
6641 page_index->page_data +
6642 (page_index->page_len * pc),
6643 page_index->page_len);
6644 data_used += page_index->page_len;
6645 }
6646 break;
6647 }
6648 default: {
6649 int i, data_used;
6650
6651 data_used = header_len;
6652
6653 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6654 struct ctl_page_index *page_index;
6655
6656 page_index = &lun->mode_pages.index[i];
6657
6658 /* Look for the right page code */
6659 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6660 continue;
6661
6662 /* Look for the right subpage or the subpage wildcard*/
6663 if ((page_index->subpage != subpage)
6664 && (subpage != SMS_SUBPAGE_ALL))
6665 continue;
6666
6667 /* Make sure the page is supported for this dev type */
6668 if (lun->be_lun->lun_type == T_DIRECT &&
6669 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6670 continue;
6671 if (lun->be_lun->lun_type == T_PROCESSOR &&
6672 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6673 continue;
6674 if (lun->be_lun->lun_type == T_CDROM &&
6675 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6676 continue;
6677
6678 /*
6679 * Call the handler, if it exists, to update the
6680 * page to the latest values.
6681 */
6682 if (page_index->sense_handler != NULL)
6683 page_index->sense_handler(ctsio, page_index,pc);
6684
6685 memcpy(ctsio->kern_data_ptr + data_used,
6686 page_index->page_data +
6687 (page_index->page_len * pc),
6688 page_index->page_len);
6689 data_used += page_index->page_len;
6690 }
6691 break;
6692 }
6693 }
6694
6695 ctl_set_success(ctsio);
6696 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6697 ctsio->be_move_done = ctl_config_move_done;
6698 ctl_datamove((union ctl_io *)ctsio);
6699 return (CTL_RETVAL_COMPLETE);
6700 }
6701
6702 int
ctl_temp_log_sense_handler(struct ctl_scsiio * ctsio,struct ctl_page_index * page_index,int pc)6703 ctl_temp_log_sense_handler(struct ctl_scsiio *ctsio,
6704 struct ctl_page_index *page_index,
6705 int pc)
6706 {
6707 struct ctl_lun *lun = CTL_LUN(ctsio);
6708 struct scsi_log_temperature *data;
6709 const char *value;
6710
6711 data = (struct scsi_log_temperature *)page_index->page_data;
6712
6713 scsi_ulto2b(SLP_TEMPERATURE, data->hdr.param_code);
6714 data->hdr.param_control = SLP_LBIN;
6715 data->hdr.param_len = sizeof(struct scsi_log_temperature) -
6716 sizeof(struct scsi_log_param_header);
6717 if ((value = dnvlist_get_string(lun->be_lun->options, "temperature",
6718 NULL)) != NULL)
6719 data->temperature = strtol(value, NULL, 0);
6720 else
6721 data->temperature = 0xff;
6722 data++;
6723
6724 scsi_ulto2b(SLP_REFTEMPERATURE, data->hdr.param_code);
6725 data->hdr.param_control = SLP_LBIN;
6726 data->hdr.param_len = sizeof(struct scsi_log_temperature) -
6727 sizeof(struct scsi_log_param_header);
6728 if ((value = dnvlist_get_string(lun->be_lun->options, "reftemperature",
6729 NULL)) != NULL)
6730 data->temperature = strtol(value, NULL, 0);
6731 else
6732 data->temperature = 0xff;
6733 return (0);
6734 }
6735
6736 int
ctl_lbp_log_sense_handler(struct ctl_scsiio * ctsio,struct ctl_page_index * page_index,int pc)6737 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6738 struct ctl_page_index *page_index,
6739 int pc)
6740 {
6741 struct ctl_lun *lun = CTL_LUN(ctsio);
6742 struct scsi_log_param_header *phdr;
6743 uint8_t *data;
6744 uint64_t val;
6745
6746 data = page_index->page_data;
6747
6748 if (lun->backend->lun_attr != NULL &&
6749 (val = lun->backend->lun_attr(lun->be_lun, "blocksavail"))
6750 != UINT64_MAX) {
6751 phdr = (struct scsi_log_param_header *)data;
6752 scsi_ulto2b(0x0001, phdr->param_code);
6753 phdr->param_control = SLP_LBIN | SLP_LP;
6754 phdr->param_len = 8;
6755 data = (uint8_t *)(phdr + 1);
6756 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6757 data[4] = 0x02; /* per-pool */
6758 data += phdr->param_len;
6759 }
6760
6761 if (lun->backend->lun_attr != NULL &&
6762 (val = lun->backend->lun_attr(lun->be_lun, "blocksused"))
6763 != UINT64_MAX) {
6764 phdr = (struct scsi_log_param_header *)data;
6765 scsi_ulto2b(0x0002, phdr->param_code);
6766 phdr->param_control = SLP_LBIN | SLP_LP;
6767 phdr->param_len = 8;
6768 data = (uint8_t *)(phdr + 1);
6769 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6770 data[4] = 0x01; /* per-LUN */
6771 data += phdr->param_len;
6772 }
6773
6774 if (lun->backend->lun_attr != NULL &&
6775 (val = lun->backend->lun_attr(lun->be_lun, "poolblocksavail"))
6776 != UINT64_MAX) {
6777 phdr = (struct scsi_log_param_header *)data;
6778 scsi_ulto2b(0x00f1, phdr->param_code);
6779 phdr->param_control = SLP_LBIN | SLP_LP;
6780 phdr->param_len = 8;
6781 data = (uint8_t *)(phdr + 1);
6782 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6783 data[4] = 0x02; /* per-pool */
6784 data += phdr->param_len;
6785 }
6786
6787 if (lun->backend->lun_attr != NULL &&
6788 (val = lun->backend->lun_attr(lun->be_lun, "poolblocksused"))
6789 != UINT64_MAX) {
6790 phdr = (struct scsi_log_param_header *)data;
6791 scsi_ulto2b(0x00f2, phdr->param_code);
6792 phdr->param_control = SLP_LBIN | SLP_LP;
6793 phdr->param_len = 8;
6794 data = (uint8_t *)(phdr + 1);
6795 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6796 data[4] = 0x02; /* per-pool */
6797 data += phdr->param_len;
6798 }
6799
6800 page_index->page_len = data - page_index->page_data;
6801 return (0);
6802 }
6803
6804 int
ctl_sap_log_sense_handler(struct ctl_scsiio * ctsio,struct ctl_page_index * page_index,int pc)6805 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6806 struct ctl_page_index *page_index,
6807 int pc)
6808 {
6809 struct ctl_lun *lun = CTL_LUN(ctsio);
6810 struct stat_page *data;
6811 struct bintime *t;
6812
6813 data = (struct stat_page *)page_index->page_data;
6814
6815 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6816 data->sap.hdr.param_control = SLP_LBIN;
6817 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6818 sizeof(struct scsi_log_param_header);
6819 scsi_u64to8b(lun->stats.operations[CTL_STATS_READ],
6820 data->sap.read_num);
6821 scsi_u64to8b(lun->stats.operations[CTL_STATS_WRITE],
6822 data->sap.write_num);
6823 if (lun->be_lun->blocksize > 0) {
6824 scsi_u64to8b(lun->stats.bytes[CTL_STATS_WRITE] /
6825 lun->be_lun->blocksize, data->sap.recvieved_lba);
6826 scsi_u64to8b(lun->stats.bytes[CTL_STATS_READ] /
6827 lun->be_lun->blocksize, data->sap.transmitted_lba);
6828 }
6829 t = &lun->stats.time[CTL_STATS_READ];
6830 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6831 data->sap.read_int);
6832 t = &lun->stats.time[CTL_STATS_WRITE];
6833 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6834 data->sap.write_int);
6835 scsi_u64to8b(0, data->sap.weighted_num);
6836 scsi_u64to8b(0, data->sap.weighted_int);
6837 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6838 data->it.hdr.param_control = SLP_LBIN;
6839 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6840 sizeof(struct scsi_log_param_header);
6841 #ifdef CTL_TIME_IO
6842 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6843 #endif
6844 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6845 data->it.hdr.param_control = SLP_LBIN;
6846 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6847 sizeof(struct scsi_log_param_header);
6848 scsi_ulto4b(3, data->ti.exponent);
6849 scsi_ulto4b(1, data->ti.integer);
6850 return (0);
6851 }
6852
6853 int
ctl_ie_log_sense_handler(struct ctl_scsiio * ctsio,struct ctl_page_index * page_index,int pc)6854 ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio,
6855 struct ctl_page_index *page_index,
6856 int pc)
6857 {
6858 struct ctl_lun *lun = CTL_LUN(ctsio);
6859 struct scsi_log_informational_exceptions *data;
6860 const char *value;
6861
6862 data = (struct scsi_log_informational_exceptions *)page_index->page_data;
6863
6864 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code);
6865 data->hdr.param_control = SLP_LBIN;
6866 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) -
6867 sizeof(struct scsi_log_param_header);
6868 data->ie_asc = lun->ie_asc;
6869 data->ie_ascq = lun->ie_ascq;
6870 if ((value = dnvlist_get_string(lun->be_lun->options, "temperature",
6871 NULL)) != NULL)
6872 data->temperature = strtol(value, NULL, 0);
6873 else
6874 data->temperature = 0xff;
6875 return (0);
6876 }
6877
6878 int
ctl_log_sense(struct ctl_scsiio * ctsio)6879 ctl_log_sense(struct ctl_scsiio *ctsio)
6880 {
6881 struct ctl_lun *lun = CTL_LUN(ctsio);
6882 int i, pc, page_code, subpage;
6883 int alloc_len, total_len;
6884 struct ctl_page_index *page_index;
6885 struct scsi_log_sense *cdb;
6886 struct scsi_log_header *header;
6887
6888 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6889
6890 cdb = (struct scsi_log_sense *)ctsio->cdb;
6891 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6892 page_code = cdb->page & SLS_PAGE_CODE;
6893 subpage = cdb->subpage;
6894 alloc_len = scsi_2btoul(cdb->length);
6895
6896 page_index = NULL;
6897 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6898 page_index = &lun->log_pages.index[i];
6899
6900 /* Look for the right page code */
6901 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6902 continue;
6903
6904 /* Look for the right subpage or the subpage wildcard*/
6905 if (page_index->subpage != subpage)
6906 continue;
6907
6908 break;
6909 }
6910 if (i >= CTL_NUM_LOG_PAGES) {
6911 ctl_set_invalid_field(ctsio,
6912 /*sks_valid*/ 1,
6913 /*command*/ 1,
6914 /*field*/ 2,
6915 /*bit_valid*/ 0,
6916 /*bit*/ 0);
6917 ctl_done((union ctl_io *)ctsio);
6918 return (CTL_RETVAL_COMPLETE);
6919 }
6920
6921 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6922
6923 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6924 ctsio->kern_sg_entries = 0;
6925 ctsio->kern_rel_offset = 0;
6926 ctsio->kern_data_len = min(total_len, alloc_len);
6927 ctsio->kern_total_len = ctsio->kern_data_len;
6928
6929 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6930 header->page = page_index->page_code;
6931 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING)
6932 header->page |= SL_DS;
6933 if (page_index->subpage) {
6934 header->page |= SL_SPF;
6935 header->subpage = page_index->subpage;
6936 }
6937 scsi_ulto2b(page_index->page_len, header->datalen);
6938
6939 /*
6940 * Call the handler, if it exists, to update the
6941 * page to the latest values.
6942 */
6943 if (page_index->sense_handler != NULL)
6944 page_index->sense_handler(ctsio, page_index, pc);
6945
6946 memcpy(header + 1, page_index->page_data, page_index->page_len);
6947
6948 ctl_set_success(ctsio);
6949 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6950 ctsio->be_move_done = ctl_config_move_done;
6951 ctl_datamove((union ctl_io *)ctsio);
6952 return (CTL_RETVAL_COMPLETE);
6953 }
6954
6955 int
ctl_read_capacity(struct ctl_scsiio * ctsio)6956 ctl_read_capacity(struct ctl_scsiio *ctsio)
6957 {
6958 struct ctl_lun *lun = CTL_LUN(ctsio);
6959 struct scsi_read_capacity *cdb;
6960 struct scsi_read_capacity_data *data;
6961 uint32_t lba;
6962
6963 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6964
6965 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6966
6967 lba = scsi_4btoul(cdb->addr);
6968 if (((cdb->pmi & SRC_PMI) == 0)
6969 && (lba != 0)) {
6970 ctl_set_invalid_field(/*ctsio*/ ctsio,
6971 /*sks_valid*/ 1,
6972 /*command*/ 1,
6973 /*field*/ 2,
6974 /*bit_valid*/ 0,
6975 /*bit*/ 0);
6976 ctl_done((union ctl_io *)ctsio);
6977 return (CTL_RETVAL_COMPLETE);
6978 }
6979
6980 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6981 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6982 ctsio->kern_data_len = sizeof(*data);
6983 ctsio->kern_total_len = sizeof(*data);
6984 ctsio->kern_rel_offset = 0;
6985 ctsio->kern_sg_entries = 0;
6986
6987 /*
6988 * If the maximum LBA is greater than 0xfffffffe, the user must
6989 * issue a SERVICE ACTION IN (16) command, with the read capacity
6990 * serivce action set.
6991 */
6992 if (lun->be_lun->maxlba > 0xfffffffe)
6993 scsi_ulto4b(0xffffffff, data->addr);
6994 else
6995 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6996
6997 /*
6998 * XXX KDM this may not be 512 bytes...
6999 */
7000 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7001
7002 ctl_set_success(ctsio);
7003 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7004 ctsio->be_move_done = ctl_config_move_done;
7005 ctl_datamove((union ctl_io *)ctsio);
7006 return (CTL_RETVAL_COMPLETE);
7007 }
7008
7009 int
ctl_read_capacity_16(struct ctl_scsiio * ctsio)7010 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
7011 {
7012 struct ctl_lun *lun = CTL_LUN(ctsio);
7013 struct scsi_read_capacity_16 *cdb;
7014 struct scsi_read_capacity_data_long *data;
7015 uint64_t lba;
7016 uint32_t alloc_len;
7017
7018 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7019
7020 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7021
7022 alloc_len = scsi_4btoul(cdb->alloc_len);
7023 lba = scsi_8btou64(cdb->addr);
7024
7025 if ((cdb->reladr & SRC16_PMI)
7026 && (lba != 0)) {
7027 ctl_set_invalid_field(/*ctsio*/ ctsio,
7028 /*sks_valid*/ 1,
7029 /*command*/ 1,
7030 /*field*/ 2,
7031 /*bit_valid*/ 0,
7032 /*bit*/ 0);
7033 ctl_done((union ctl_io *)ctsio);
7034 return (CTL_RETVAL_COMPLETE);
7035 }
7036
7037 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7038 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7039 ctsio->kern_rel_offset = 0;
7040 ctsio->kern_sg_entries = 0;
7041 ctsio->kern_data_len = min(sizeof(*data), alloc_len);
7042 ctsio->kern_total_len = ctsio->kern_data_len;
7043
7044 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7045 /* XXX KDM this may not be 512 bytes... */
7046 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7047 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7048 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7049 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7050 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
7051
7052 ctl_set_success(ctsio);
7053 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7054 ctsio->be_move_done = ctl_config_move_done;
7055 ctl_datamove((union ctl_io *)ctsio);
7056 return (CTL_RETVAL_COMPLETE);
7057 }
7058
7059 int
ctl_get_lba_status(struct ctl_scsiio * ctsio)7060 ctl_get_lba_status(struct ctl_scsiio *ctsio)
7061 {
7062 struct ctl_lun *lun = CTL_LUN(ctsio);
7063 struct scsi_get_lba_status *cdb;
7064 struct scsi_get_lba_status_data *data;
7065 struct ctl_lba_len_flags *lbalen;
7066 uint64_t lba;
7067 uint32_t alloc_len, total_len;
7068 int retval;
7069
7070 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
7071
7072 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
7073 lba = scsi_8btou64(cdb->addr);
7074 alloc_len = scsi_4btoul(cdb->alloc_len);
7075
7076 if (lba > lun->be_lun->maxlba) {
7077 ctl_set_lba_out_of_range(ctsio, lba);
7078 ctl_done((union ctl_io *)ctsio);
7079 return (CTL_RETVAL_COMPLETE);
7080 }
7081
7082 total_len = sizeof(*data) + sizeof(data->descr[0]);
7083 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7084 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
7085 ctsio->kern_rel_offset = 0;
7086 ctsio->kern_sg_entries = 0;
7087 ctsio->kern_data_len = min(total_len, alloc_len);
7088 ctsio->kern_total_len = ctsio->kern_data_len;
7089
7090 /* Fill dummy data in case backend can't tell anything. */
7091 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
7092 scsi_u64to8b(lba, data->descr[0].addr);
7093 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
7094 data->descr[0].length);
7095 data->descr[0].status = 0; /* Mapped or unknown. */
7096
7097 ctl_set_success(ctsio);
7098 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7099 ctsio->be_move_done = ctl_config_move_done;
7100
7101 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7102 lbalen->lba = lba;
7103 lbalen->len = total_len;
7104 lbalen->flags = 0;
7105 retval = lun->backend->config_read((union ctl_io *)ctsio);
7106 return (retval);
7107 }
7108
7109 int
ctl_read_defect(struct ctl_scsiio * ctsio)7110 ctl_read_defect(struct ctl_scsiio *ctsio)
7111 {
7112 struct scsi_read_defect_data_10 *ccb10;
7113 struct scsi_read_defect_data_12 *ccb12;
7114 struct scsi_read_defect_data_hdr_10 *data10;
7115 struct scsi_read_defect_data_hdr_12 *data12;
7116 uint32_t alloc_len, data_len;
7117 uint8_t format;
7118
7119 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7120
7121 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7122 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7123 format = ccb10->format;
7124 alloc_len = scsi_2btoul(ccb10->alloc_length);
7125 data_len = sizeof(*data10);
7126 } else {
7127 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7128 format = ccb12->format;
7129 alloc_len = scsi_4btoul(ccb12->alloc_length);
7130 data_len = sizeof(*data12);
7131 }
7132 if (alloc_len == 0) {
7133 ctl_set_success(ctsio);
7134 ctl_done((union ctl_io *)ctsio);
7135 return (CTL_RETVAL_COMPLETE);
7136 }
7137
7138 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7139 ctsio->kern_rel_offset = 0;
7140 ctsio->kern_sg_entries = 0;
7141 ctsio->kern_data_len = min(data_len, alloc_len);
7142 ctsio->kern_total_len = ctsio->kern_data_len;
7143
7144 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7145 data10 = (struct scsi_read_defect_data_hdr_10 *)
7146 ctsio->kern_data_ptr;
7147 data10->format = format;
7148 scsi_ulto2b(0, data10->length);
7149 } else {
7150 data12 = (struct scsi_read_defect_data_hdr_12 *)
7151 ctsio->kern_data_ptr;
7152 data12->format = format;
7153 scsi_ulto2b(0, data12->generation);
7154 scsi_ulto4b(0, data12->length);
7155 }
7156
7157 ctl_set_success(ctsio);
7158 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7159 ctsio->be_move_done = ctl_config_move_done;
7160 ctl_datamove((union ctl_io *)ctsio);
7161 return (CTL_RETVAL_COMPLETE);
7162 }
7163
7164 int
ctl_report_ident_info(struct ctl_scsiio * ctsio)7165 ctl_report_ident_info(struct ctl_scsiio *ctsio)
7166 {
7167 struct ctl_lun *lun = CTL_LUN(ctsio);
7168 struct scsi_report_ident_info *cdb;
7169 struct scsi_report_ident_info_data *rii_ptr;
7170 struct scsi_report_ident_info_descr *riid_ptr;
7171 const char *oii, *otii;
7172 int retval, alloc_len, total_len = 0, len = 0;
7173
7174 CTL_DEBUG_PRINT(("ctl_report_ident_info\n"));
7175
7176 cdb = (struct scsi_report_ident_info *)ctsio->cdb;
7177 retval = CTL_RETVAL_COMPLETE;
7178
7179 total_len = sizeof(struct scsi_report_ident_info_data);
7180 switch (cdb->type) {
7181 case RII_LUII:
7182 oii = dnvlist_get_string(lun->be_lun->options,
7183 "ident_info", NULL);
7184 if (oii)
7185 len = strlen(oii); /* Approximately */
7186 break;
7187 case RII_LUTII:
7188 otii = dnvlist_get_string(lun->be_lun->options,
7189 "text_ident_info", NULL);
7190 if (otii)
7191 len = strlen(otii) + 1; /* NULL-terminated */
7192 break;
7193 case RII_IIS:
7194 len = 2 * sizeof(struct scsi_report_ident_info_descr);
7195 break;
7196 default:
7197 ctl_set_invalid_field(/*ctsio*/ ctsio,
7198 /*sks_valid*/ 1,
7199 /*command*/ 1,
7200 /*field*/ 11,
7201 /*bit_valid*/ 1,
7202 /*bit*/ 2);
7203 ctl_done((union ctl_io *)ctsio);
7204 return(retval);
7205 }
7206 total_len += len;
7207 alloc_len = scsi_4btoul(cdb->length);
7208
7209 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7210 ctsio->kern_sg_entries = 0;
7211 ctsio->kern_rel_offset = 0;
7212 ctsio->kern_data_len = min(total_len, alloc_len);
7213 ctsio->kern_total_len = ctsio->kern_data_len;
7214
7215 rii_ptr = (struct scsi_report_ident_info_data *)ctsio->kern_data_ptr;
7216 switch (cdb->type) {
7217 case RII_LUII:
7218 if (oii) {
7219 if (oii[0] == '0' && oii[1] == 'x')
7220 len = hex2bin(oii, (uint8_t *)(rii_ptr + 1), len);
7221 else
7222 strncpy((uint8_t *)(rii_ptr + 1), oii, len);
7223 }
7224 break;
7225 case RII_LUTII:
7226 if (otii)
7227 strlcpy((uint8_t *)(rii_ptr + 1), otii, len);
7228 break;
7229 case RII_IIS:
7230 riid_ptr = (struct scsi_report_ident_info_descr *)(rii_ptr + 1);
7231 riid_ptr->type = RII_LUII;
7232 scsi_ulto2b(0xffff, riid_ptr->length);
7233 riid_ptr++;
7234 riid_ptr->type = RII_LUTII;
7235 scsi_ulto2b(0xffff, riid_ptr->length);
7236 }
7237 scsi_ulto2b(len, rii_ptr->length);
7238
7239 ctl_set_success(ctsio);
7240 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7241 ctsio->be_move_done = ctl_config_move_done;
7242 ctl_datamove((union ctl_io *)ctsio);
7243 return(retval);
7244 }
7245
7246 int
ctl_report_tagret_port_groups(struct ctl_scsiio * ctsio)7247 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7248 {
7249 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7250 struct ctl_lun *lun = CTL_LUN(ctsio);
7251 struct scsi_maintenance_in *cdb;
7252 int retval;
7253 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os;
7254 int num_ha_groups, num_target_ports, shared_group;
7255 struct ctl_port *port;
7256 struct scsi_target_group_data *rtg_ptr;
7257 struct scsi_target_group_data_extended *rtg_ext_ptr;
7258 struct scsi_target_port_group_descriptor *tpg_desc;
7259
7260 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7261
7262 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7263 retval = CTL_RETVAL_COMPLETE;
7264
7265 switch (cdb->byte2 & STG_PDF_MASK) {
7266 case STG_PDF_LENGTH:
7267 ext = 0;
7268 break;
7269 case STG_PDF_EXTENDED:
7270 ext = 1;
7271 break;
7272 default:
7273 ctl_set_invalid_field(/*ctsio*/ ctsio,
7274 /*sks_valid*/ 1,
7275 /*command*/ 1,
7276 /*field*/ 2,
7277 /*bit_valid*/ 1,
7278 /*bit*/ 5);
7279 ctl_done((union ctl_io *)ctsio);
7280 return(retval);
7281 }
7282
7283 num_target_ports = 0;
7284 shared_group = (softc->is_single != 0);
7285 mtx_lock(&softc->ctl_lock);
7286 STAILQ_FOREACH(port, &softc->port_list, links) {
7287 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7288 continue;
7289 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7290 continue;
7291 num_target_ports++;
7292 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7293 shared_group = 1;
7294 }
7295 mtx_unlock(&softc->ctl_lock);
7296 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES;
7297
7298 if (ext)
7299 total_len = sizeof(struct scsi_target_group_data_extended);
7300 else
7301 total_len = sizeof(struct scsi_target_group_data);
7302 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7303 (shared_group + num_ha_groups) +
7304 sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7305
7306 alloc_len = scsi_4btoul(cdb->length);
7307
7308 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7309 ctsio->kern_sg_entries = 0;
7310 ctsio->kern_rel_offset = 0;
7311 ctsio->kern_data_len = min(total_len, alloc_len);
7312 ctsio->kern_total_len = ctsio->kern_data_len;
7313
7314 if (ext) {
7315 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7316 ctsio->kern_data_ptr;
7317 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7318 rtg_ext_ptr->format_type = 0x10;
7319 rtg_ext_ptr->implicit_transition_time = 0;
7320 tpg_desc = &rtg_ext_ptr->groups[0];
7321 } else {
7322 rtg_ptr = (struct scsi_target_group_data *)
7323 ctsio->kern_data_ptr;
7324 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7325 tpg_desc = &rtg_ptr->groups[0];
7326 }
7327
7328 mtx_lock(&softc->ctl_lock);
7329 pg = softc->port_min / softc->port_cnt;
7330 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) {
7331 /* Some shelf is known to be primary. */
7332 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7333 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7334 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7335 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7336 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7337 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7338 else
7339 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7340 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7341 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7342 } else {
7343 ts = os;
7344 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7345 }
7346 } else {
7347 /* No known primary shelf. */
7348 if (softc->ha_link == CTL_HA_LINK_OFFLINE) {
7349 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7350 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7351 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) {
7352 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7353 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7354 } else {
7355 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7356 }
7357 }
7358 if (shared_group) {
7359 tpg_desc->pref_state = ts;
7360 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7361 TPG_U_SUP | TPG_T_SUP;
7362 scsi_ulto2b(1, tpg_desc->target_port_group);
7363 tpg_desc->status = TPG_IMPLICIT;
7364 pc = 0;
7365 STAILQ_FOREACH(port, &softc->port_list, links) {
7366 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7367 continue;
7368 if (!softc->is_single &&
7369 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0)
7370 continue;
7371 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7372 continue;
7373 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7374 relative_target_port_identifier);
7375 pc++;
7376 }
7377 tpg_desc->target_port_count = pc;
7378 tpg_desc = (struct scsi_target_port_group_descriptor *)
7379 &tpg_desc->descriptors[pc];
7380 }
7381 for (g = 0; g < num_ha_groups; g++) {
7382 tpg_desc->pref_state = (g == pg) ? ts : os;
7383 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7384 TPG_U_SUP | TPG_T_SUP;
7385 scsi_ulto2b(2 + g, tpg_desc->target_port_group);
7386 tpg_desc->status = TPG_IMPLICIT;
7387 pc = 0;
7388 STAILQ_FOREACH(port, &softc->port_list, links) {
7389 if (port->targ_port < g * softc->port_cnt ||
7390 port->targ_port >= (g + 1) * softc->port_cnt)
7391 continue;
7392 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7393 continue;
7394 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7395 continue;
7396 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7397 continue;
7398 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7399 relative_target_port_identifier);
7400 pc++;
7401 }
7402 tpg_desc->target_port_count = pc;
7403 tpg_desc = (struct scsi_target_port_group_descriptor *)
7404 &tpg_desc->descriptors[pc];
7405 }
7406 mtx_unlock(&softc->ctl_lock);
7407
7408 ctl_set_success(ctsio);
7409 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7410 ctsio->be_move_done = ctl_config_move_done;
7411 ctl_datamove((union ctl_io *)ctsio);
7412 return(retval);
7413 }
7414
7415 int
ctl_report_supported_opcodes(struct ctl_scsiio * ctsio)7416 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7417 {
7418 struct ctl_lun *lun = CTL_LUN(ctsio);
7419 struct scsi_report_supported_opcodes *cdb;
7420 const struct ctl_cmd_entry *entry, *sentry;
7421 struct scsi_report_supported_opcodes_all *all;
7422 struct scsi_report_supported_opcodes_descr *descr;
7423 struct scsi_report_supported_opcodes_one *one;
7424 int retval;
7425 int alloc_len, total_len;
7426 int opcode, service_action, i, j, num;
7427
7428 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7429
7430 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7431 retval = CTL_RETVAL_COMPLETE;
7432
7433 opcode = cdb->requested_opcode;
7434 service_action = scsi_2btoul(cdb->requested_service_action);
7435 switch (cdb->options & RSO_OPTIONS_MASK) {
7436 case RSO_OPTIONS_ALL:
7437 num = 0;
7438 for (i = 0; i < 256; i++) {
7439 entry = &ctl_cmd_table[i];
7440 if (entry->flags & CTL_CMD_FLAG_SA5) {
7441 for (j = 0; j < 32; j++) {
7442 sentry = &((const struct ctl_cmd_entry *)
7443 entry->execute)[j];
7444 if (ctl_cmd_applicable(
7445 lun->be_lun->lun_type, sentry))
7446 num++;
7447 }
7448 } else {
7449 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7450 entry))
7451 num++;
7452 }
7453 }
7454 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7455 num * sizeof(struct scsi_report_supported_opcodes_descr);
7456 break;
7457 case RSO_OPTIONS_OC:
7458 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7459 goto invalid_options;
7460 }
7461 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7462 break;
7463 case RSO_OPTIONS_OC_SA:
7464 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7465 service_action >= 32) {
7466 goto invalid_options;
7467 }
7468 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7469 break;
7470 case RSO_OPTIONS_OC_ASA:
7471 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) != 0 &&
7472 service_action >= 32) {
7473 goto invalid_options;
7474 }
7475 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7476 break;
7477 default:
7478 invalid_options:
7479 ctl_set_invalid_field(/*ctsio*/ ctsio,
7480 /*sks_valid*/ 1,
7481 /*command*/ 1,
7482 /*field*/ 2,
7483 /*bit_valid*/ 1,
7484 /*bit*/ 2);
7485 ctl_done((union ctl_io *)ctsio);
7486 return (CTL_RETVAL_COMPLETE);
7487 }
7488
7489 alloc_len = scsi_4btoul(cdb->length);
7490
7491 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7492 ctsio->kern_sg_entries = 0;
7493 ctsio->kern_rel_offset = 0;
7494 ctsio->kern_data_len = min(total_len, alloc_len);
7495 ctsio->kern_total_len = ctsio->kern_data_len;
7496
7497 switch (cdb->options & RSO_OPTIONS_MASK) {
7498 case RSO_OPTIONS_ALL:
7499 all = (struct scsi_report_supported_opcodes_all *)
7500 ctsio->kern_data_ptr;
7501 num = 0;
7502 for (i = 0; i < 256; i++) {
7503 entry = &ctl_cmd_table[i];
7504 if (entry->flags & CTL_CMD_FLAG_SA5) {
7505 for (j = 0; j < 32; j++) {
7506 sentry = &((const struct ctl_cmd_entry *)
7507 entry->execute)[j];
7508 if (!ctl_cmd_applicable(
7509 lun->be_lun->lun_type, sentry))
7510 continue;
7511 descr = &all->descr[num++];
7512 descr->opcode = i;
7513 scsi_ulto2b(j, descr->service_action);
7514 descr->flags = RSO_SERVACTV;
7515 scsi_ulto2b(sentry->length,
7516 descr->cdb_length);
7517 }
7518 } else {
7519 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7520 entry))
7521 continue;
7522 descr = &all->descr[num++];
7523 descr->opcode = i;
7524 scsi_ulto2b(0, descr->service_action);
7525 descr->flags = 0;
7526 scsi_ulto2b(entry->length, descr->cdb_length);
7527 }
7528 }
7529 scsi_ulto4b(
7530 num * sizeof(struct scsi_report_supported_opcodes_descr),
7531 all->length);
7532 break;
7533 case RSO_OPTIONS_OC:
7534 one = (struct scsi_report_supported_opcodes_one *)
7535 ctsio->kern_data_ptr;
7536 entry = &ctl_cmd_table[opcode];
7537 goto fill_one;
7538 case RSO_OPTIONS_OC_SA:
7539 one = (struct scsi_report_supported_opcodes_one *)
7540 ctsio->kern_data_ptr;
7541 entry = &ctl_cmd_table[opcode];
7542 entry = &((const struct ctl_cmd_entry *)
7543 entry->execute)[service_action];
7544 fill_one:
7545 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7546 one->support = 3;
7547 scsi_ulto2b(entry->length, one->cdb_length);
7548 one->cdb_usage[0] = opcode;
7549 memcpy(&one->cdb_usage[1], entry->usage,
7550 entry->length - 1);
7551 } else
7552 one->support = 1;
7553 break;
7554 case RSO_OPTIONS_OC_ASA:
7555 one = (struct scsi_report_supported_opcodes_one *)
7556 ctsio->kern_data_ptr;
7557 entry = &ctl_cmd_table[opcode];
7558 if (entry->flags & CTL_CMD_FLAG_SA5) {
7559 entry = &((const struct ctl_cmd_entry *)
7560 entry->execute)[service_action];
7561 } else if (service_action != 0) {
7562 one->support = 1;
7563 break;
7564 }
7565 goto fill_one;
7566 }
7567
7568 ctl_set_success(ctsio);
7569 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7570 ctsio->be_move_done = ctl_config_move_done;
7571 ctl_datamove((union ctl_io *)ctsio);
7572 return(retval);
7573 }
7574
7575 int
ctl_report_supported_tmf(struct ctl_scsiio * ctsio)7576 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7577 {
7578 struct scsi_report_supported_tmf *cdb;
7579 struct scsi_report_supported_tmf_ext_data *data;
7580 int retval;
7581 int alloc_len, total_len;
7582
7583 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7584
7585 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7586
7587 retval = CTL_RETVAL_COMPLETE;
7588
7589 if (cdb->options & RST_REPD)
7590 total_len = sizeof(struct scsi_report_supported_tmf_ext_data);
7591 else
7592 total_len = sizeof(struct scsi_report_supported_tmf_data);
7593 alloc_len = scsi_4btoul(cdb->length);
7594
7595 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7596 ctsio->kern_sg_entries = 0;
7597 ctsio->kern_rel_offset = 0;
7598 ctsio->kern_data_len = min(total_len, alloc_len);
7599 ctsio->kern_total_len = ctsio->kern_data_len;
7600
7601 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr;
7602 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7603 RST_TRS;
7604 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7605 data->length = total_len - 4;
7606
7607 ctl_set_success(ctsio);
7608 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7609 ctsio->be_move_done = ctl_config_move_done;
7610 ctl_datamove((union ctl_io *)ctsio);
7611 return (retval);
7612 }
7613
7614 int
ctl_report_timestamp(struct ctl_scsiio * ctsio)7615 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7616 {
7617 struct scsi_report_timestamp *cdb;
7618 struct scsi_report_timestamp_data *data;
7619 struct timeval tv;
7620 int64_t timestamp;
7621 int retval;
7622 int alloc_len, total_len;
7623
7624 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7625
7626 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7627
7628 retval = CTL_RETVAL_COMPLETE;
7629
7630 total_len = sizeof(struct scsi_report_timestamp_data);
7631 alloc_len = scsi_4btoul(cdb->length);
7632
7633 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7634 ctsio->kern_sg_entries = 0;
7635 ctsio->kern_rel_offset = 0;
7636 ctsio->kern_data_len = min(total_len, alloc_len);
7637 ctsio->kern_total_len = ctsio->kern_data_len;
7638
7639 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7640 scsi_ulto2b(sizeof(*data) - 2, data->length);
7641 data->origin = RTS_ORIG_OUTSIDE;
7642 getmicrotime(&tv);
7643 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7644 scsi_ulto4b(timestamp >> 16, data->timestamp);
7645 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7646
7647 ctl_set_success(ctsio);
7648 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7649 ctsio->be_move_done = ctl_config_move_done;
7650 ctl_datamove((union ctl_io *)ctsio);
7651 return (retval);
7652 }
7653
7654 int
ctl_persistent_reserve_in(struct ctl_scsiio * ctsio)7655 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7656 {
7657 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7658 struct ctl_lun *lun = CTL_LUN(ctsio);
7659 struct scsi_per_res_in *cdb;
7660 int alloc_len, total_len = 0;
7661 /* struct scsi_per_res_in_rsrv in_data; */
7662 uint64_t key;
7663
7664 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7665
7666 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7667
7668 alloc_len = scsi_2btoul(cdb->length);
7669
7670 retry:
7671 mtx_lock(&lun->lun_lock);
7672 switch (cdb->action) {
7673 case SPRI_RK: /* read keys */
7674 total_len = sizeof(struct scsi_per_res_in_keys) +
7675 lun->pr_key_count *
7676 sizeof(struct scsi_per_res_key);
7677 break;
7678 case SPRI_RR: /* read reservation */
7679 if (lun->flags & CTL_LUN_PR_RESERVED)
7680 total_len = sizeof(struct scsi_per_res_in_rsrv);
7681 else
7682 total_len = sizeof(struct scsi_per_res_in_header);
7683 break;
7684 case SPRI_RC: /* report capabilities */
7685 total_len = sizeof(struct scsi_per_res_cap);
7686 break;
7687 case SPRI_RS: /* read full status */
7688 total_len = sizeof(struct scsi_per_res_in_header) +
7689 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7690 lun->pr_key_count;
7691 break;
7692 default:
7693 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7694 }
7695 mtx_unlock(&lun->lun_lock);
7696
7697 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7698 ctsio->kern_rel_offset = 0;
7699 ctsio->kern_sg_entries = 0;
7700 ctsio->kern_data_len = min(total_len, alloc_len);
7701 ctsio->kern_total_len = ctsio->kern_data_len;
7702
7703 mtx_lock(&lun->lun_lock);
7704 switch (cdb->action) {
7705 case SPRI_RK: { // read keys
7706 struct scsi_per_res_in_keys *res_keys;
7707 int i, key_count;
7708
7709 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7710
7711 /*
7712 * We had to drop the lock to allocate our buffer, which
7713 * leaves time for someone to come in with another
7714 * persistent reservation. (That is unlikely, though,
7715 * since this should be the only persistent reservation
7716 * command active right now.)
7717 */
7718 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7719 (lun->pr_key_count *
7720 sizeof(struct scsi_per_res_key)))){
7721 mtx_unlock(&lun->lun_lock);
7722 free(ctsio->kern_data_ptr, M_CTL);
7723 printf("%s: reservation length changed, retrying\n",
7724 __func__);
7725 goto retry;
7726 }
7727
7728 scsi_ulto4b(lun->pr_generation, res_keys->header.generation);
7729
7730 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7731 lun->pr_key_count, res_keys->header.length);
7732
7733 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7734 if ((key = ctl_get_prkey(lun, i)) == 0)
7735 continue;
7736
7737 /*
7738 * We used lun->pr_key_count to calculate the
7739 * size to allocate. If it turns out the number of
7740 * initiators with the registered flag set is
7741 * larger than that (i.e. they haven't been kept in
7742 * sync), we've got a problem.
7743 */
7744 if (key_count >= lun->pr_key_count) {
7745 key_count++;
7746 continue;
7747 }
7748 scsi_u64to8b(key, res_keys->keys[key_count].key);
7749 key_count++;
7750 }
7751 break;
7752 }
7753 case SPRI_RR: { // read reservation
7754 struct scsi_per_res_in_rsrv *res;
7755 int tmp_len, header_only;
7756
7757 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7758
7759 scsi_ulto4b(lun->pr_generation, res->header.generation);
7760
7761 if (lun->flags & CTL_LUN_PR_RESERVED)
7762 {
7763 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7764 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7765 res->header.length);
7766 header_only = 0;
7767 } else {
7768 tmp_len = sizeof(struct scsi_per_res_in_header);
7769 scsi_ulto4b(0, res->header.length);
7770 header_only = 1;
7771 }
7772
7773 /*
7774 * We had to drop the lock to allocate our buffer, which
7775 * leaves time for someone to come in with another
7776 * persistent reservation. (That is unlikely, though,
7777 * since this should be the only persistent reservation
7778 * command active right now.)
7779 */
7780 if (tmp_len != total_len) {
7781 mtx_unlock(&lun->lun_lock);
7782 free(ctsio->kern_data_ptr, M_CTL);
7783 printf("%s: reservation status changed, retrying\n",
7784 __func__);
7785 goto retry;
7786 }
7787
7788 /*
7789 * No reservation held, so we're done.
7790 */
7791 if (header_only != 0)
7792 break;
7793
7794 /*
7795 * If the registration is an All Registrants type, the key
7796 * is 0, since it doesn't really matter.
7797 */
7798 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7799 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7800 res->data.reservation);
7801 }
7802 res->data.scopetype = lun->pr_res_type;
7803 break;
7804 }
7805 case SPRI_RC: //report capabilities
7806 {
7807 struct scsi_per_res_cap *res_cap;
7808 uint16_t type_mask;
7809
7810 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7811 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7812 res_cap->flags1 = SPRI_CRH;
7813 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5;
7814 type_mask = SPRI_TM_WR_EX_AR |
7815 SPRI_TM_EX_AC_RO |
7816 SPRI_TM_WR_EX_RO |
7817 SPRI_TM_EX_AC |
7818 SPRI_TM_WR_EX |
7819 SPRI_TM_EX_AC_AR;
7820 scsi_ulto2b(type_mask, res_cap->type_mask);
7821 break;
7822 }
7823 case SPRI_RS: { // read full status
7824 struct scsi_per_res_in_full *res_status;
7825 struct scsi_per_res_in_full_desc *res_desc;
7826 struct ctl_port *port;
7827 int i, len;
7828
7829 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7830
7831 /*
7832 * We had to drop the lock to allocate our buffer, which
7833 * leaves time for someone to come in with another
7834 * persistent reservation. (That is unlikely, though,
7835 * since this should be the only persistent reservation
7836 * command active right now.)
7837 */
7838 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7839 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7840 lun->pr_key_count)){
7841 mtx_unlock(&lun->lun_lock);
7842 free(ctsio->kern_data_ptr, M_CTL);
7843 printf("%s: reservation length changed, retrying\n",
7844 __func__);
7845 goto retry;
7846 }
7847
7848 scsi_ulto4b(lun->pr_generation, res_status->header.generation);
7849
7850 res_desc = &res_status->desc[0];
7851 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7852 if ((key = ctl_get_prkey(lun, i)) == 0)
7853 continue;
7854
7855 scsi_u64to8b(key, res_desc->res_key.key);
7856 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7857 (lun->pr_res_idx == i ||
7858 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7859 res_desc->flags = SPRI_FULL_R_HOLDER;
7860 res_desc->scopetype = lun->pr_res_type;
7861 }
7862 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7863 res_desc->rel_trgt_port_id);
7864 len = 0;
7865 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7866 if (port != NULL)
7867 len = ctl_create_iid(port,
7868 i % CTL_MAX_INIT_PER_PORT,
7869 res_desc->transport_id);
7870 scsi_ulto4b(len, res_desc->additional_length);
7871 res_desc = (struct scsi_per_res_in_full_desc *)
7872 &res_desc->transport_id[len];
7873 }
7874 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7875 res_status->header.length);
7876 break;
7877 }
7878 default:
7879 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7880 }
7881 mtx_unlock(&lun->lun_lock);
7882
7883 ctl_set_success(ctsio);
7884 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7885 ctsio->be_move_done = ctl_config_move_done;
7886 ctl_datamove((union ctl_io *)ctsio);
7887 return (CTL_RETVAL_COMPLETE);
7888 }
7889
7890 /*
7891 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7892 * it should return.
7893 */
7894 static int
ctl_pro_preempt(struct ctl_softc * softc,struct ctl_lun * lun,uint64_t res_key,uint64_t sa_res_key,uint8_t type,uint32_t residx,struct ctl_scsiio * ctsio,struct scsi_per_res_out * cdb,struct scsi_per_res_out_parms * param)7895 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7896 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7897 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7898 struct scsi_per_res_out_parms* param)
7899 {
7900 union ctl_ha_msg persis_io;
7901 int i;
7902
7903 mtx_lock(&lun->lun_lock);
7904 if (sa_res_key == 0) {
7905 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7906 /* validate scope and type */
7907 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7908 SPR_LU_SCOPE) {
7909 mtx_unlock(&lun->lun_lock);
7910 ctl_set_invalid_field(/*ctsio*/ ctsio,
7911 /*sks_valid*/ 1,
7912 /*command*/ 1,
7913 /*field*/ 2,
7914 /*bit_valid*/ 1,
7915 /*bit*/ 4);
7916 ctl_done((union ctl_io *)ctsio);
7917 return (1);
7918 }
7919
7920 if (type>8 || type==2 || type==4 || type==0) {
7921 mtx_unlock(&lun->lun_lock);
7922 ctl_set_invalid_field(/*ctsio*/ ctsio,
7923 /*sks_valid*/ 1,
7924 /*command*/ 1,
7925 /*field*/ 2,
7926 /*bit_valid*/ 1,
7927 /*bit*/ 0);
7928 ctl_done((union ctl_io *)ctsio);
7929 return (1);
7930 }
7931
7932 /*
7933 * Unregister everybody else and build UA for
7934 * them
7935 */
7936 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7937 if (i == residx || ctl_get_prkey(lun, i) == 0)
7938 continue;
7939
7940 ctl_clr_prkey(lun, i);
7941 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7942 }
7943 lun->pr_key_count = 1;
7944 lun->pr_res_type = type;
7945 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7946 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7947 lun->pr_res_idx = residx;
7948 lun->pr_generation++;
7949 mtx_unlock(&lun->lun_lock);
7950
7951 /* send msg to other side */
7952 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7953 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7954 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7955 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7956 persis_io.pr.pr_info.res_type = type;
7957 memcpy(persis_io.pr.pr_info.sa_res_key,
7958 param->serv_act_res_key,
7959 sizeof(param->serv_act_res_key));
7960 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7961 sizeof(persis_io.pr), M_WAITOK);
7962 } else {
7963 /* not all registrants */
7964 mtx_unlock(&lun->lun_lock);
7965 free(ctsio->kern_data_ptr, M_CTL);
7966 ctl_set_invalid_field(ctsio,
7967 /*sks_valid*/ 1,
7968 /*command*/ 0,
7969 /*field*/ 8,
7970 /*bit_valid*/ 0,
7971 /*bit*/ 0);
7972 ctl_done((union ctl_io *)ctsio);
7973 return (1);
7974 }
7975 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7976 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7977 int found = 0;
7978
7979 if (res_key == sa_res_key) {
7980 /* special case */
7981 /*
7982 * The spec implies this is not good but doesn't
7983 * say what to do. There are two choices either
7984 * generate a res conflict or check condition
7985 * with illegal field in parameter data. Since
7986 * that is what is done when the sa_res_key is
7987 * zero I'll take that approach since this has
7988 * to do with the sa_res_key.
7989 */
7990 mtx_unlock(&lun->lun_lock);
7991 free(ctsio->kern_data_ptr, M_CTL);
7992 ctl_set_invalid_field(ctsio,
7993 /*sks_valid*/ 1,
7994 /*command*/ 0,
7995 /*field*/ 8,
7996 /*bit_valid*/ 0,
7997 /*bit*/ 0);
7998 ctl_done((union ctl_io *)ctsio);
7999 return (1);
8000 }
8001
8002 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8003 if (ctl_get_prkey(lun, i) != sa_res_key)
8004 continue;
8005
8006 found = 1;
8007 ctl_clr_prkey(lun, i);
8008 lun->pr_key_count--;
8009 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8010 }
8011 if (!found) {
8012 mtx_unlock(&lun->lun_lock);
8013 free(ctsio->kern_data_ptr, M_CTL);
8014 ctl_set_reservation_conflict(ctsio);
8015 ctl_done((union ctl_io *)ctsio);
8016 return (CTL_RETVAL_COMPLETE);
8017 }
8018 lun->pr_generation++;
8019 mtx_unlock(&lun->lun_lock);
8020
8021 /* send msg to other side */
8022 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8023 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8024 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8025 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8026 persis_io.pr.pr_info.res_type = type;
8027 memcpy(persis_io.pr.pr_info.sa_res_key,
8028 param->serv_act_res_key,
8029 sizeof(param->serv_act_res_key));
8030 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8031 sizeof(persis_io.pr), M_WAITOK);
8032 } else {
8033 /* Reserved but not all registrants */
8034 /* sa_res_key is res holder */
8035 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
8036 /* validate scope and type */
8037 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
8038 SPR_LU_SCOPE) {
8039 mtx_unlock(&lun->lun_lock);
8040 ctl_set_invalid_field(/*ctsio*/ ctsio,
8041 /*sks_valid*/ 1,
8042 /*command*/ 1,
8043 /*field*/ 2,
8044 /*bit_valid*/ 1,
8045 /*bit*/ 4);
8046 ctl_done((union ctl_io *)ctsio);
8047 return (1);
8048 }
8049
8050 if (type>8 || type==2 || type==4 || type==0) {
8051 mtx_unlock(&lun->lun_lock);
8052 ctl_set_invalid_field(/*ctsio*/ ctsio,
8053 /*sks_valid*/ 1,
8054 /*command*/ 1,
8055 /*field*/ 2,
8056 /*bit_valid*/ 1,
8057 /*bit*/ 0);
8058 ctl_done((union ctl_io *)ctsio);
8059 return (1);
8060 }
8061
8062 /*
8063 * Do the following:
8064 * if sa_res_key != res_key remove all
8065 * registrants w/sa_res_key and generate UA
8066 * for these registrants(Registrations
8067 * Preempted) if it wasn't an exclusive
8068 * reservation generate UA(Reservations
8069 * Preempted) for all other registered nexuses
8070 * if the type has changed. Establish the new
8071 * reservation and holder. If res_key and
8072 * sa_res_key are the same do the above
8073 * except don't unregister the res holder.
8074 */
8075
8076 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8077 if (i == residx || ctl_get_prkey(lun, i) == 0)
8078 continue;
8079
8080 if (sa_res_key == ctl_get_prkey(lun, i)) {
8081 ctl_clr_prkey(lun, i);
8082 lun->pr_key_count--;
8083 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8084 } else if (type != lun->pr_res_type &&
8085 (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8086 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8087 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8088 }
8089 }
8090 lun->pr_res_type = type;
8091 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8092 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8093 lun->pr_res_idx = residx;
8094 else
8095 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8096 lun->pr_generation++;
8097 mtx_unlock(&lun->lun_lock);
8098
8099 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8100 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8101 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8102 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8103 persis_io.pr.pr_info.res_type = type;
8104 memcpy(persis_io.pr.pr_info.sa_res_key,
8105 param->serv_act_res_key,
8106 sizeof(param->serv_act_res_key));
8107 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8108 sizeof(persis_io.pr), M_WAITOK);
8109 } else {
8110 /*
8111 * sa_res_key is not the res holder just
8112 * remove registrants
8113 */
8114 int found=0;
8115
8116 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8117 if (sa_res_key != ctl_get_prkey(lun, i))
8118 continue;
8119
8120 found = 1;
8121 ctl_clr_prkey(lun, i);
8122 lun->pr_key_count--;
8123 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8124 }
8125
8126 if (!found) {
8127 mtx_unlock(&lun->lun_lock);
8128 free(ctsio->kern_data_ptr, M_CTL);
8129 ctl_set_reservation_conflict(ctsio);
8130 ctl_done((union ctl_io *)ctsio);
8131 return (1);
8132 }
8133 lun->pr_generation++;
8134 mtx_unlock(&lun->lun_lock);
8135
8136 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8137 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8138 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8139 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8140 persis_io.pr.pr_info.res_type = type;
8141 memcpy(persis_io.pr.pr_info.sa_res_key,
8142 param->serv_act_res_key,
8143 sizeof(param->serv_act_res_key));
8144 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8145 sizeof(persis_io.pr), M_WAITOK);
8146 }
8147 }
8148 return (0);
8149 }
8150
8151 static void
ctl_pro_preempt_other(struct ctl_lun * lun,union ctl_ha_msg * msg)8152 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8153 {
8154 uint64_t sa_res_key;
8155 int i;
8156
8157 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8158
8159 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8160 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8161 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8162 if (sa_res_key == 0) {
8163 /*
8164 * Unregister everybody else and build UA for
8165 * them
8166 */
8167 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8168 if (i == msg->pr.pr_info.residx ||
8169 ctl_get_prkey(lun, i) == 0)
8170 continue;
8171
8172 ctl_clr_prkey(lun, i);
8173 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8174 }
8175
8176 lun->pr_key_count = 1;
8177 lun->pr_res_type = msg->pr.pr_info.res_type;
8178 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8179 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8180 lun->pr_res_idx = msg->pr.pr_info.residx;
8181 } else {
8182 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8183 if (sa_res_key == ctl_get_prkey(lun, i))
8184 continue;
8185
8186 ctl_clr_prkey(lun, i);
8187 lun->pr_key_count--;
8188 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8189 }
8190 }
8191 } else {
8192 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8193 if (i == msg->pr.pr_info.residx ||
8194 ctl_get_prkey(lun, i) == 0)
8195 continue;
8196
8197 if (sa_res_key == ctl_get_prkey(lun, i)) {
8198 ctl_clr_prkey(lun, i);
8199 lun->pr_key_count--;
8200 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8201 } else if (msg->pr.pr_info.res_type != lun->pr_res_type
8202 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8203 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8204 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8205 }
8206 }
8207 lun->pr_res_type = msg->pr.pr_info.res_type;
8208 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8209 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8210 lun->pr_res_idx = msg->pr.pr_info.residx;
8211 else
8212 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8213 }
8214 lun->pr_generation++;
8215
8216 }
8217
8218 int
ctl_persistent_reserve_out(struct ctl_scsiio * ctsio)8219 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8220 {
8221 struct ctl_softc *softc = CTL_SOFTC(ctsio);
8222 struct ctl_lun *lun = CTL_LUN(ctsio);
8223 int retval;
8224 uint32_t param_len;
8225 struct scsi_per_res_out *cdb;
8226 struct scsi_per_res_out_parms* param;
8227 uint32_t residx;
8228 uint64_t res_key, sa_res_key, key;
8229 uint8_t type;
8230 union ctl_ha_msg persis_io;
8231 int i;
8232
8233 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8234
8235 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8236 retval = CTL_RETVAL_COMPLETE;
8237
8238 /*
8239 * We only support whole-LUN scope. The scope & type are ignored for
8240 * register, register and ignore existing key and clear.
8241 * We sometimes ignore scope and type on preempts too!!
8242 * Verify reservation type here as well.
8243 */
8244 type = cdb->scope_type & SPR_TYPE_MASK;
8245 if ((cdb->action == SPRO_RESERVE)
8246 || (cdb->action == SPRO_RELEASE)) {
8247 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8248 ctl_set_invalid_field(/*ctsio*/ ctsio,
8249 /*sks_valid*/ 1,
8250 /*command*/ 1,
8251 /*field*/ 2,
8252 /*bit_valid*/ 1,
8253 /*bit*/ 4);
8254 ctl_done((union ctl_io *)ctsio);
8255 return (CTL_RETVAL_COMPLETE);
8256 }
8257
8258 if (type>8 || type==2 || type==4 || type==0) {
8259 ctl_set_invalid_field(/*ctsio*/ ctsio,
8260 /*sks_valid*/ 1,
8261 /*command*/ 1,
8262 /*field*/ 2,
8263 /*bit_valid*/ 1,
8264 /*bit*/ 0);
8265 ctl_done((union ctl_io *)ctsio);
8266 return (CTL_RETVAL_COMPLETE);
8267 }
8268 }
8269
8270 param_len = scsi_4btoul(cdb->length);
8271
8272 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8273 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8274 ctsio->kern_data_len = param_len;
8275 ctsio->kern_total_len = param_len;
8276 ctsio->kern_rel_offset = 0;
8277 ctsio->kern_sg_entries = 0;
8278 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8279 ctsio->be_move_done = ctl_config_move_done;
8280 ctl_datamove((union ctl_io *)ctsio);
8281
8282 return (CTL_RETVAL_COMPLETE);
8283 }
8284
8285 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8286
8287 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8288 res_key = scsi_8btou64(param->res_key.key);
8289 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8290
8291 /*
8292 * Validate the reservation key here except for SPRO_REG_IGNO
8293 * This must be done for all other service actions
8294 */
8295 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8296 mtx_lock(&lun->lun_lock);
8297 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8298 if (res_key != key) {
8299 /*
8300 * The current key passed in doesn't match
8301 * the one the initiator previously
8302 * registered.
8303 */
8304 mtx_unlock(&lun->lun_lock);
8305 free(ctsio->kern_data_ptr, M_CTL);
8306 ctl_set_reservation_conflict(ctsio);
8307 ctl_done((union ctl_io *)ctsio);
8308 return (CTL_RETVAL_COMPLETE);
8309 }
8310 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8311 /*
8312 * We are not registered
8313 */
8314 mtx_unlock(&lun->lun_lock);
8315 free(ctsio->kern_data_ptr, M_CTL);
8316 ctl_set_reservation_conflict(ctsio);
8317 ctl_done((union ctl_io *)ctsio);
8318 return (CTL_RETVAL_COMPLETE);
8319 } else if (res_key != 0) {
8320 /*
8321 * We are not registered and trying to register but
8322 * the register key isn't zero.
8323 */
8324 mtx_unlock(&lun->lun_lock);
8325 free(ctsio->kern_data_ptr, M_CTL);
8326 ctl_set_reservation_conflict(ctsio);
8327 ctl_done((union ctl_io *)ctsio);
8328 return (CTL_RETVAL_COMPLETE);
8329 }
8330 mtx_unlock(&lun->lun_lock);
8331 }
8332
8333 switch (cdb->action & SPRO_ACTION_MASK) {
8334 case SPRO_REGISTER:
8335 case SPRO_REG_IGNO: {
8336 /*
8337 * We don't support any of these options, as we report in
8338 * the read capabilities request (see
8339 * ctl_persistent_reserve_in(), above).
8340 */
8341 if ((param->flags & SPR_SPEC_I_PT)
8342 || (param->flags & SPR_ALL_TG_PT)
8343 || (param->flags & SPR_APTPL)) {
8344 int bit_ptr;
8345
8346 if (param->flags & SPR_APTPL)
8347 bit_ptr = 0;
8348 else if (param->flags & SPR_ALL_TG_PT)
8349 bit_ptr = 2;
8350 else /* SPR_SPEC_I_PT */
8351 bit_ptr = 3;
8352
8353 free(ctsio->kern_data_ptr, M_CTL);
8354 ctl_set_invalid_field(ctsio,
8355 /*sks_valid*/ 1,
8356 /*command*/ 0,
8357 /*field*/ 20,
8358 /*bit_valid*/ 1,
8359 /*bit*/ bit_ptr);
8360 ctl_done((union ctl_io *)ctsio);
8361 return (CTL_RETVAL_COMPLETE);
8362 }
8363
8364 mtx_lock(&lun->lun_lock);
8365
8366 /*
8367 * The initiator wants to clear the
8368 * key/unregister.
8369 */
8370 if (sa_res_key == 0) {
8371 if ((res_key == 0
8372 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8373 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8374 && ctl_get_prkey(lun, residx) == 0)) {
8375 mtx_unlock(&lun->lun_lock);
8376 goto done;
8377 }
8378
8379 ctl_clr_prkey(lun, residx);
8380 lun->pr_key_count--;
8381
8382 if (residx == lun->pr_res_idx) {
8383 lun->flags &= ~CTL_LUN_PR_RESERVED;
8384 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8385
8386 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8387 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8388 lun->pr_key_count) {
8389 /*
8390 * If the reservation is a registrants
8391 * only type we need to generate a UA
8392 * for other registered inits. The
8393 * sense code should be RESERVATIONS
8394 * RELEASED
8395 */
8396
8397 for (i = softc->init_min; i < softc->init_max; i++){
8398 if (ctl_get_prkey(lun, i) == 0)
8399 continue;
8400 ctl_est_ua(lun, i,
8401 CTL_UA_RES_RELEASE);
8402 }
8403 }
8404 lun->pr_res_type = 0;
8405 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8406 if (lun->pr_key_count==0) {
8407 lun->flags &= ~CTL_LUN_PR_RESERVED;
8408 lun->pr_res_type = 0;
8409 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8410 }
8411 }
8412 lun->pr_generation++;
8413 mtx_unlock(&lun->lun_lock);
8414
8415 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8416 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8417 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8418 persis_io.pr.pr_info.residx = residx;
8419 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8420 sizeof(persis_io.pr), M_WAITOK);
8421 } else /* sa_res_key != 0 */ {
8422 /*
8423 * If we aren't registered currently then increment
8424 * the key count and set the registered flag.
8425 */
8426 ctl_alloc_prkey(lun, residx);
8427 if (ctl_get_prkey(lun, residx) == 0)
8428 lun->pr_key_count++;
8429 ctl_set_prkey(lun, residx, sa_res_key);
8430 lun->pr_generation++;
8431 mtx_unlock(&lun->lun_lock);
8432
8433 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8434 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8435 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8436 persis_io.pr.pr_info.residx = residx;
8437 memcpy(persis_io.pr.pr_info.sa_res_key,
8438 param->serv_act_res_key,
8439 sizeof(param->serv_act_res_key));
8440 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8441 sizeof(persis_io.pr), M_WAITOK);
8442 }
8443
8444 break;
8445 }
8446 case SPRO_RESERVE:
8447 mtx_lock(&lun->lun_lock);
8448 if (lun->flags & CTL_LUN_PR_RESERVED) {
8449 /*
8450 * if this isn't the reservation holder and it's
8451 * not a "all registrants" type or if the type is
8452 * different then we have a conflict
8453 */
8454 if ((lun->pr_res_idx != residx
8455 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8456 || lun->pr_res_type != type) {
8457 mtx_unlock(&lun->lun_lock);
8458 free(ctsio->kern_data_ptr, M_CTL);
8459 ctl_set_reservation_conflict(ctsio);
8460 ctl_done((union ctl_io *)ctsio);
8461 return (CTL_RETVAL_COMPLETE);
8462 }
8463 mtx_unlock(&lun->lun_lock);
8464 } else /* create a reservation */ {
8465 /*
8466 * If it's not an "all registrants" type record
8467 * reservation holder
8468 */
8469 if (type != SPR_TYPE_WR_EX_AR
8470 && type != SPR_TYPE_EX_AC_AR)
8471 lun->pr_res_idx = residx; /* Res holder */
8472 else
8473 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8474
8475 lun->flags |= CTL_LUN_PR_RESERVED;
8476 lun->pr_res_type = type;
8477
8478 mtx_unlock(&lun->lun_lock);
8479
8480 /* send msg to other side */
8481 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8482 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8483 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8484 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8485 persis_io.pr.pr_info.res_type = type;
8486 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8487 sizeof(persis_io.pr), M_WAITOK);
8488 }
8489 break;
8490
8491 case SPRO_RELEASE:
8492 mtx_lock(&lun->lun_lock);
8493 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8494 /* No reservation exists return good status */
8495 mtx_unlock(&lun->lun_lock);
8496 goto done;
8497 }
8498 /*
8499 * Is this nexus a reservation holder?
8500 */
8501 if (lun->pr_res_idx != residx
8502 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8503 /*
8504 * not a res holder return good status but
8505 * do nothing
8506 */
8507 mtx_unlock(&lun->lun_lock);
8508 goto done;
8509 }
8510
8511 if (lun->pr_res_type != type) {
8512 mtx_unlock(&lun->lun_lock);
8513 free(ctsio->kern_data_ptr, M_CTL);
8514 ctl_set_illegal_pr_release(ctsio);
8515 ctl_done((union ctl_io *)ctsio);
8516 return (CTL_RETVAL_COMPLETE);
8517 }
8518
8519 /* okay to release */
8520 lun->flags &= ~CTL_LUN_PR_RESERVED;
8521 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8522 lun->pr_res_type = 0;
8523
8524 /*
8525 * If this isn't an exclusive access reservation and NUAR
8526 * is not set, generate UA for all other registrants.
8527 */
8528 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX &&
8529 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8530 for (i = softc->init_min; i < softc->init_max; i++) {
8531 if (i == residx || ctl_get_prkey(lun, i) == 0)
8532 continue;
8533 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8534 }
8535 }
8536 mtx_unlock(&lun->lun_lock);
8537
8538 /* Send msg to other side */
8539 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8540 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8541 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8542 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8543 sizeof(persis_io.pr), M_WAITOK);
8544 break;
8545
8546 case SPRO_CLEAR:
8547 /* send msg to other side */
8548
8549 mtx_lock(&lun->lun_lock);
8550 lun->flags &= ~CTL_LUN_PR_RESERVED;
8551 lun->pr_res_type = 0;
8552 lun->pr_key_count = 0;
8553 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8554
8555 ctl_clr_prkey(lun, residx);
8556 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8557 if (ctl_get_prkey(lun, i) != 0) {
8558 ctl_clr_prkey(lun, i);
8559 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8560 }
8561 lun->pr_generation++;
8562 mtx_unlock(&lun->lun_lock);
8563
8564 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8565 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8566 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8567 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8568 sizeof(persis_io.pr), M_WAITOK);
8569 break;
8570
8571 case SPRO_PREEMPT:
8572 case SPRO_PRE_ABO: {
8573 int nretval;
8574
8575 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8576 residx, ctsio, cdb, param);
8577 if (nretval != 0)
8578 return (CTL_RETVAL_COMPLETE);
8579 break;
8580 }
8581 default:
8582 panic("%s: Invalid PR type %#x", __func__, cdb->action);
8583 }
8584
8585 done:
8586 free(ctsio->kern_data_ptr, M_CTL);
8587 ctl_set_success(ctsio);
8588 ctl_done((union ctl_io *)ctsio);
8589
8590 return (retval);
8591 }
8592
8593 /*
8594 * This routine is for handling a message from the other SC pertaining to
8595 * persistent reserve out. All the error checking will have been done
8596 * so only performing the action need be done here to keep the two
8597 * in sync.
8598 */
8599 static void
ctl_hndl_per_res_out_on_other_sc(union ctl_io * io)8600 ctl_hndl_per_res_out_on_other_sc(union ctl_io *io)
8601 {
8602 struct ctl_softc *softc = CTL_SOFTC(io);
8603 union ctl_ha_msg *msg = (union ctl_ha_msg *)&io->presio.pr_msg;
8604 struct ctl_lun *lun;
8605 int i;
8606 uint32_t residx, targ_lun;
8607
8608 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8609 mtx_lock(&softc->ctl_lock);
8610 if (targ_lun >= ctl_max_luns ||
8611 (lun = softc->ctl_luns[targ_lun]) == NULL) {
8612 mtx_unlock(&softc->ctl_lock);
8613 return;
8614 }
8615 mtx_lock(&lun->lun_lock);
8616 mtx_unlock(&softc->ctl_lock);
8617 if (lun->flags & CTL_LUN_DISABLED) {
8618 mtx_unlock(&lun->lun_lock);
8619 return;
8620 }
8621 residx = ctl_get_initindex(&msg->hdr.nexus);
8622 switch(msg->pr.pr_info.action) {
8623 case CTL_PR_REG_KEY:
8624 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8625 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8626 lun->pr_key_count++;
8627 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8628 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8629 lun->pr_generation++;
8630 break;
8631
8632 case CTL_PR_UNREG_KEY:
8633 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8634 lun->pr_key_count--;
8635
8636 /* XXX Need to see if the reservation has been released */
8637 /* if so do we need to generate UA? */
8638 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8639 lun->flags &= ~CTL_LUN_PR_RESERVED;
8640 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8641
8642 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8643 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8644 lun->pr_key_count) {
8645 /*
8646 * If the reservation is a registrants
8647 * only type we need to generate a UA
8648 * for other registered inits. The
8649 * sense code should be RESERVATIONS
8650 * RELEASED
8651 */
8652
8653 for (i = softc->init_min; i < softc->init_max; i++) {
8654 if (ctl_get_prkey(lun, i) == 0)
8655 continue;
8656
8657 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8658 }
8659 }
8660 lun->pr_res_type = 0;
8661 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8662 if (lun->pr_key_count==0) {
8663 lun->flags &= ~CTL_LUN_PR_RESERVED;
8664 lun->pr_res_type = 0;
8665 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8666 }
8667 }
8668 lun->pr_generation++;
8669 break;
8670
8671 case CTL_PR_RESERVE:
8672 lun->flags |= CTL_LUN_PR_RESERVED;
8673 lun->pr_res_type = msg->pr.pr_info.res_type;
8674 lun->pr_res_idx = msg->pr.pr_info.residx;
8675
8676 break;
8677
8678 case CTL_PR_RELEASE:
8679 /*
8680 * If this isn't an exclusive access reservation and NUAR
8681 * is not set, generate UA for all other registrants.
8682 */
8683 if (lun->pr_res_type != SPR_TYPE_EX_AC &&
8684 lun->pr_res_type != SPR_TYPE_WR_EX &&
8685 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8686 for (i = softc->init_min; i < softc->init_max; i++) {
8687 if (i == residx || ctl_get_prkey(lun, i) == 0)
8688 continue;
8689 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8690 }
8691 }
8692
8693 lun->flags &= ~CTL_LUN_PR_RESERVED;
8694 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8695 lun->pr_res_type = 0;
8696 break;
8697
8698 case CTL_PR_PREEMPT:
8699 ctl_pro_preempt_other(lun, msg);
8700 break;
8701 case CTL_PR_CLEAR:
8702 lun->flags &= ~CTL_LUN_PR_RESERVED;
8703 lun->pr_res_type = 0;
8704 lun->pr_key_count = 0;
8705 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8706
8707 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8708 if (ctl_get_prkey(lun, i) == 0)
8709 continue;
8710 ctl_clr_prkey(lun, i);
8711 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8712 }
8713 lun->pr_generation++;
8714 break;
8715 }
8716
8717 mtx_unlock(&lun->lun_lock);
8718 }
8719
8720 int
ctl_read_write(struct ctl_scsiio * ctsio)8721 ctl_read_write(struct ctl_scsiio *ctsio)
8722 {
8723 struct ctl_lun *lun = CTL_LUN(ctsio);
8724 struct ctl_lba_len_flags *lbalen;
8725 uint64_t lba;
8726 uint32_t num_blocks;
8727 int flags, retval;
8728 int isread;
8729
8730 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8731
8732 flags = 0;
8733 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8734 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8735 switch (ctsio->cdb[0]) {
8736 case READ_6:
8737 case WRITE_6: {
8738 struct scsi_rw_6 *cdb;
8739
8740 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8741
8742 lba = scsi_3btoul(cdb->addr);
8743 /* only 5 bits are valid in the most significant address byte */
8744 lba &= 0x1fffff;
8745 num_blocks = cdb->length;
8746 /*
8747 * This is correct according to SBC-2.
8748 */
8749 if (num_blocks == 0)
8750 num_blocks = 256;
8751 break;
8752 }
8753 case READ_10:
8754 case WRITE_10: {
8755 struct scsi_rw_10 *cdb;
8756
8757 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8758 if (cdb->byte2 & SRW10_FUA)
8759 flags |= CTL_LLF_FUA;
8760 if (cdb->byte2 & SRW10_DPO)
8761 flags |= CTL_LLF_DPO;
8762 lba = scsi_4btoul(cdb->addr);
8763 num_blocks = scsi_2btoul(cdb->length);
8764 break;
8765 }
8766 case WRITE_VERIFY_10: {
8767 struct scsi_write_verify_10 *cdb;
8768
8769 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8770 flags |= CTL_LLF_FUA;
8771 if (cdb->byte2 & SWV_DPO)
8772 flags |= CTL_LLF_DPO;
8773 lba = scsi_4btoul(cdb->addr);
8774 num_blocks = scsi_2btoul(cdb->length);
8775 break;
8776 }
8777 case READ_12:
8778 case WRITE_12: {
8779 struct scsi_rw_12 *cdb;
8780
8781 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8782 if (cdb->byte2 & SRW12_FUA)
8783 flags |= CTL_LLF_FUA;
8784 if (cdb->byte2 & SRW12_DPO)
8785 flags |= CTL_LLF_DPO;
8786 lba = scsi_4btoul(cdb->addr);
8787 num_blocks = scsi_4btoul(cdb->length);
8788 break;
8789 }
8790 case WRITE_VERIFY_12: {
8791 struct scsi_write_verify_12 *cdb;
8792
8793 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8794 flags |= CTL_LLF_FUA;
8795 if (cdb->byte2 & SWV_DPO)
8796 flags |= CTL_LLF_DPO;
8797 lba = scsi_4btoul(cdb->addr);
8798 num_blocks = scsi_4btoul(cdb->length);
8799 break;
8800 }
8801 case READ_16:
8802 case WRITE_16: {
8803 struct scsi_rw_16 *cdb;
8804
8805 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8806 if (cdb->byte2 & SRW12_FUA)
8807 flags |= CTL_LLF_FUA;
8808 if (cdb->byte2 & SRW12_DPO)
8809 flags |= CTL_LLF_DPO;
8810 lba = scsi_8btou64(cdb->addr);
8811 num_blocks = scsi_4btoul(cdb->length);
8812 break;
8813 }
8814 case WRITE_ATOMIC_16: {
8815 struct scsi_write_atomic_16 *cdb;
8816
8817 if (lun->be_lun->atomicblock == 0) {
8818 ctl_set_invalid_opcode(ctsio);
8819 ctl_done((union ctl_io *)ctsio);
8820 return (CTL_RETVAL_COMPLETE);
8821 }
8822
8823 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb;
8824 if (cdb->byte2 & SRW12_FUA)
8825 flags |= CTL_LLF_FUA;
8826 if (cdb->byte2 & SRW12_DPO)
8827 flags |= CTL_LLF_DPO;
8828 lba = scsi_8btou64(cdb->addr);
8829 num_blocks = scsi_2btoul(cdb->length);
8830 if (num_blocks > lun->be_lun->atomicblock) {
8831 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8832 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8833 /*bit*/ 0);
8834 ctl_done((union ctl_io *)ctsio);
8835 return (CTL_RETVAL_COMPLETE);
8836 }
8837 break;
8838 }
8839 case WRITE_VERIFY_16: {
8840 struct scsi_write_verify_16 *cdb;
8841
8842 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8843 flags |= CTL_LLF_FUA;
8844 if (cdb->byte2 & SWV_DPO)
8845 flags |= CTL_LLF_DPO;
8846 lba = scsi_8btou64(cdb->addr);
8847 num_blocks = scsi_4btoul(cdb->length);
8848 break;
8849 }
8850 default:
8851 /*
8852 * We got a command we don't support. This shouldn't
8853 * happen, commands should be filtered out above us.
8854 */
8855 ctl_set_invalid_opcode(ctsio);
8856 ctl_done((union ctl_io *)ctsio);
8857
8858 return (CTL_RETVAL_COMPLETE);
8859 break; /* NOTREACHED */
8860 }
8861
8862 /*
8863 * The first check is to make sure we're in bounds, the second
8864 * check is to catch wrap-around problems. If the lba + num blocks
8865 * is less than the lba, then we've wrapped around and the block
8866 * range is invalid anyway.
8867 */
8868 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8869 || ((lba + num_blocks) < lba)) {
8870 ctl_set_lba_out_of_range(ctsio,
8871 MAX(lba, lun->be_lun->maxlba + 1));
8872 ctl_done((union ctl_io *)ctsio);
8873 return (CTL_RETVAL_COMPLETE);
8874 }
8875
8876 /*
8877 * According to SBC-3, a transfer length of 0 is not an error.
8878 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8879 * translates to 256 blocks for those commands.
8880 */
8881 if (num_blocks == 0) {
8882 ctl_set_success(ctsio);
8883 ctl_done((union ctl_io *)ctsio);
8884 return (CTL_RETVAL_COMPLETE);
8885 }
8886
8887 /* Set FUA and/or DPO if caches are disabled. */
8888 if (isread) {
8889 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0)
8890 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8891 } else {
8892 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8893 flags |= CTL_LLF_FUA;
8894 }
8895
8896 lbalen = (struct ctl_lba_len_flags *)
8897 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8898 lbalen->lba = lba;
8899 lbalen->len = num_blocks;
8900 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8901
8902 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8903 ctsio->kern_rel_offset = 0;
8904
8905 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8906
8907 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8908 return (retval);
8909 }
8910
8911 static int
ctl_cnw_cont(union ctl_io * io)8912 ctl_cnw_cont(union ctl_io *io)
8913 {
8914 struct ctl_lun *lun = CTL_LUN(io);
8915 struct ctl_scsiio *ctsio;
8916 struct ctl_lba_len_flags *lbalen;
8917 int retval;
8918
8919 CTL_IO_ASSERT(io, SCSI);
8920
8921 ctsio = &io->scsiio;
8922 ctsio->io_hdr.status = CTL_STATUS_NONE;
8923 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8924 lbalen = (struct ctl_lba_len_flags *)
8925 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8926 lbalen->flags &= ~CTL_LLF_COMPARE;
8927 lbalen->flags |= CTL_LLF_WRITE;
8928
8929 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8930 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8931 return (retval);
8932 }
8933
8934 int
ctl_cnw(struct ctl_scsiio * ctsio)8935 ctl_cnw(struct ctl_scsiio *ctsio)
8936 {
8937 struct ctl_lun *lun = CTL_LUN(ctsio);
8938 struct ctl_lba_len_flags *lbalen;
8939 uint64_t lba;
8940 uint32_t num_blocks;
8941 int flags, retval;
8942
8943 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8944
8945 flags = 0;
8946 switch (ctsio->cdb[0]) {
8947 case COMPARE_AND_WRITE: {
8948 struct scsi_compare_and_write *cdb;
8949
8950 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8951 if (cdb->byte2 & SRW10_FUA)
8952 flags |= CTL_LLF_FUA;
8953 if (cdb->byte2 & SRW10_DPO)
8954 flags |= CTL_LLF_DPO;
8955 lba = scsi_8btou64(cdb->addr);
8956 num_blocks = cdb->length;
8957 break;
8958 }
8959 default:
8960 /*
8961 * We got a command we don't support. This shouldn't
8962 * happen, commands should be filtered out above us.
8963 */
8964 ctl_set_invalid_opcode(ctsio);
8965 ctl_done((union ctl_io *)ctsio);
8966
8967 return (CTL_RETVAL_COMPLETE);
8968 break; /* NOTREACHED */
8969 }
8970
8971 /*
8972 * The first check is to make sure we're in bounds, the second
8973 * check is to catch wrap-around problems. If the lba + num blocks
8974 * is less than the lba, then we've wrapped around and the block
8975 * range is invalid anyway.
8976 */
8977 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8978 || ((lba + num_blocks) < lba)) {
8979 ctl_set_lba_out_of_range(ctsio,
8980 MAX(lba, lun->be_lun->maxlba + 1));
8981 ctl_done((union ctl_io *)ctsio);
8982 return (CTL_RETVAL_COMPLETE);
8983 }
8984
8985 /*
8986 * According to SBC-3, a transfer length of 0 is not an error.
8987 */
8988 if (num_blocks == 0) {
8989 ctl_set_success(ctsio);
8990 ctl_done((union ctl_io *)ctsio);
8991 return (CTL_RETVAL_COMPLETE);
8992 }
8993
8994 /* Set FUA if write cache is disabled. */
8995 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8996 flags |= CTL_LLF_FUA;
8997
8998 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8999 ctsio->kern_rel_offset = 0;
9000
9001 /*
9002 * Set the IO_CONT flag, so that if this I/O gets passed to
9003 * ctl_data_submit_done(), it'll get passed back to
9004 * ctl_ctl_cnw_cont() for further processing.
9005 */
9006 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
9007 ctsio->io_cont = ctl_cnw_cont;
9008
9009 lbalen = (struct ctl_lba_len_flags *)
9010 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9011 lbalen->lba = lba;
9012 lbalen->len = num_blocks;
9013 lbalen->flags = CTL_LLF_COMPARE | flags;
9014
9015 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
9016 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9017 return (retval);
9018 }
9019
9020 int
ctl_verify(struct ctl_scsiio * ctsio)9021 ctl_verify(struct ctl_scsiio *ctsio)
9022 {
9023 struct ctl_lun *lun = CTL_LUN(ctsio);
9024 struct ctl_lba_len_flags *lbalen;
9025 uint64_t lba;
9026 uint32_t num_blocks;
9027 int bytchk, flags;
9028 int retval;
9029
9030 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
9031
9032 bytchk = 0;
9033 flags = CTL_LLF_FUA;
9034 switch (ctsio->cdb[0]) {
9035 case VERIFY_10: {
9036 struct scsi_verify_10 *cdb;
9037
9038 cdb = (struct scsi_verify_10 *)ctsio->cdb;
9039 if (cdb->byte2 & SVFY_BYTCHK)
9040 bytchk = 1;
9041 if (cdb->byte2 & SVFY_DPO)
9042 flags |= CTL_LLF_DPO;
9043 lba = scsi_4btoul(cdb->addr);
9044 num_blocks = scsi_2btoul(cdb->length);
9045 break;
9046 }
9047 case VERIFY_12: {
9048 struct scsi_verify_12 *cdb;
9049
9050 cdb = (struct scsi_verify_12 *)ctsio->cdb;
9051 if (cdb->byte2 & SVFY_BYTCHK)
9052 bytchk = 1;
9053 if (cdb->byte2 & SVFY_DPO)
9054 flags |= CTL_LLF_DPO;
9055 lba = scsi_4btoul(cdb->addr);
9056 num_blocks = scsi_4btoul(cdb->length);
9057 break;
9058 }
9059 case VERIFY_16: {
9060 struct scsi_rw_16 *cdb;
9061
9062 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9063 if (cdb->byte2 & SVFY_BYTCHK)
9064 bytchk = 1;
9065 if (cdb->byte2 & SVFY_DPO)
9066 flags |= CTL_LLF_DPO;
9067 lba = scsi_8btou64(cdb->addr);
9068 num_blocks = scsi_4btoul(cdb->length);
9069 break;
9070 }
9071 default:
9072 /*
9073 * We got a command we don't support. This shouldn't
9074 * happen, commands should be filtered out above us.
9075 */
9076 ctl_set_invalid_opcode(ctsio);
9077 ctl_done((union ctl_io *)ctsio);
9078 return (CTL_RETVAL_COMPLETE);
9079 }
9080
9081 /*
9082 * The first check is to make sure we're in bounds, the second
9083 * check is to catch wrap-around problems. If the lba + num blocks
9084 * is less than the lba, then we've wrapped around and the block
9085 * range is invalid anyway.
9086 */
9087 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9088 || ((lba + num_blocks) < lba)) {
9089 ctl_set_lba_out_of_range(ctsio,
9090 MAX(lba, lun->be_lun->maxlba + 1));
9091 ctl_done((union ctl_io *)ctsio);
9092 return (CTL_RETVAL_COMPLETE);
9093 }
9094
9095 /*
9096 * According to SBC-3, a transfer length of 0 is not an error.
9097 */
9098 if (num_blocks == 0) {
9099 ctl_set_success(ctsio);
9100 ctl_done((union ctl_io *)ctsio);
9101 return (CTL_RETVAL_COMPLETE);
9102 }
9103
9104 lbalen = (struct ctl_lba_len_flags *)
9105 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9106 lbalen->lba = lba;
9107 lbalen->len = num_blocks;
9108 if (bytchk) {
9109 lbalen->flags = CTL_LLF_COMPARE | flags;
9110 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9111 } else {
9112 lbalen->flags = CTL_LLF_VERIFY | flags;
9113 ctsio->kern_total_len = 0;
9114 }
9115 ctsio->kern_rel_offset = 0;
9116
9117 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9118 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9119 return (retval);
9120 }
9121
9122 int
ctl_report_luns(struct ctl_scsiio * ctsio)9123 ctl_report_luns(struct ctl_scsiio *ctsio)
9124 {
9125 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9126 struct ctl_port *port = CTL_PORT(ctsio);
9127 struct ctl_lun *lun, *request_lun = CTL_LUN(ctsio);
9128 struct scsi_report_luns *cdb;
9129 struct scsi_report_luns_data *lun_data;
9130 int num_filled, num_luns, num_port_luns, retval;
9131 uint32_t alloc_len, lun_datalen;
9132 uint32_t initidx, targ_lun_id, lun_id;
9133
9134 retval = CTL_RETVAL_COMPLETE;
9135 cdb = (struct scsi_report_luns *)ctsio->cdb;
9136
9137 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9138
9139 num_luns = 0;
9140 num_port_luns = port->lun_map ? port->lun_map_size : ctl_max_luns;
9141 mtx_lock(&softc->ctl_lock);
9142 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) {
9143 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX)
9144 num_luns++;
9145 }
9146 mtx_unlock(&softc->ctl_lock);
9147
9148 switch (cdb->select_report) {
9149 case RPL_REPORT_DEFAULT:
9150 case RPL_REPORT_ALL:
9151 case RPL_REPORT_NONSUBSID:
9152 break;
9153 case RPL_REPORT_WELLKNOWN:
9154 case RPL_REPORT_ADMIN:
9155 case RPL_REPORT_CONGLOM:
9156 num_luns = 0;
9157 break;
9158 default:
9159 ctl_set_invalid_field(ctsio,
9160 /*sks_valid*/ 1,
9161 /*command*/ 1,
9162 /*field*/ 2,
9163 /*bit_valid*/ 0,
9164 /*bit*/ 0);
9165 ctl_done((union ctl_io *)ctsio);
9166 return (retval);
9167 break; /* NOTREACHED */
9168 }
9169
9170 alloc_len = scsi_4btoul(cdb->length);
9171 /*
9172 * The initiator has to allocate at least 16 bytes for this request,
9173 * so he can at least get the header and the first LUN. Otherwise
9174 * we reject the request (per SPC-3 rev 14, section 6.21).
9175 */
9176 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9177 sizeof(struct scsi_report_luns_lundata))) {
9178 ctl_set_invalid_field(ctsio,
9179 /*sks_valid*/ 1,
9180 /*command*/ 1,
9181 /*field*/ 6,
9182 /*bit_valid*/ 0,
9183 /*bit*/ 0);
9184 ctl_done((union ctl_io *)ctsio);
9185 return (retval);
9186 }
9187
9188 lun_datalen = sizeof(*lun_data) +
9189 (num_luns * sizeof(struct scsi_report_luns_lundata));
9190
9191 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9192 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9193 ctsio->kern_sg_entries = 0;
9194
9195 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9196
9197 mtx_lock(&softc->ctl_lock);
9198 for (targ_lun_id = 0, num_filled = 0;
9199 targ_lun_id < num_port_luns && num_filled < num_luns;
9200 targ_lun_id++) {
9201 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9202 if (lun_id == UINT32_MAX)
9203 continue;
9204 lun = softc->ctl_luns[lun_id];
9205 if (lun == NULL)
9206 continue;
9207
9208 be64enc(lun_data->luns[num_filled++].lundata,
9209 ctl_encode_lun(targ_lun_id));
9210
9211 /*
9212 * According to SPC-3, rev 14 section 6.21:
9213 *
9214 * "The execution of a REPORT LUNS command to any valid and
9215 * installed logical unit shall clear the REPORTED LUNS DATA
9216 * HAS CHANGED unit attention condition for all logical
9217 * units of that target with respect to the requesting
9218 * initiator. A valid and installed logical unit is one
9219 * having a PERIPHERAL QUALIFIER of 000b in the standard
9220 * INQUIRY data (see 6.4.2)."
9221 *
9222 * If request_lun is NULL, the LUN this report luns command
9223 * was issued to is either disabled or doesn't exist. In that
9224 * case, we shouldn't clear any pending lun change unit
9225 * attention.
9226 */
9227 if (request_lun != NULL) {
9228 mtx_lock(&lun->lun_lock);
9229 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9230 mtx_unlock(&lun->lun_lock);
9231 }
9232 }
9233 mtx_unlock(&softc->ctl_lock);
9234
9235 /*
9236 * It's quite possible that we've returned fewer LUNs than we allocated
9237 * space for. Trim it.
9238 */
9239 lun_datalen = sizeof(*lun_data) +
9240 (num_filled * sizeof(struct scsi_report_luns_lundata));
9241 ctsio->kern_rel_offset = 0;
9242 ctsio->kern_sg_entries = 0;
9243 ctsio->kern_data_len = min(lun_datalen, alloc_len);
9244 ctsio->kern_total_len = ctsio->kern_data_len;
9245
9246 /*
9247 * We set this to the actual data length, regardless of how much
9248 * space we actually have to return results. If the user looks at
9249 * this value, he'll know whether or not he allocated enough space
9250 * and reissue the command if necessary. We don't support well
9251 * known logical units, so if the user asks for that, return none.
9252 */
9253 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9254
9255 /*
9256 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9257 * this request.
9258 */
9259 ctl_set_success(ctsio);
9260 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9261 ctsio->be_move_done = ctl_config_move_done;
9262 ctl_datamove((union ctl_io *)ctsio);
9263 return (retval);
9264 }
9265
9266 int
ctl_request_sense(struct ctl_scsiio * ctsio)9267 ctl_request_sense(struct ctl_scsiio *ctsio)
9268 {
9269 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9270 struct ctl_lun *lun = CTL_LUN(ctsio);
9271 struct scsi_request_sense *cdb;
9272 struct scsi_sense_data *sense_ptr, *ps;
9273 uint32_t initidx;
9274 int have_error;
9275 u_int sense_len = SSD_FULL_SIZE;
9276 scsi_sense_data_type sense_format;
9277 ctl_ua_type ua_type;
9278 uint8_t asc = 0, ascq = 0;
9279
9280 cdb = (struct scsi_request_sense *)ctsio->cdb;
9281
9282 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9283
9284 /*
9285 * Determine which sense format the user wants.
9286 */
9287 if (cdb->byte2 & SRS_DESC)
9288 sense_format = SSD_TYPE_DESC;
9289 else
9290 sense_format = SSD_TYPE_FIXED;
9291
9292 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9293 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9294 ctsio->kern_sg_entries = 0;
9295 ctsio->kern_rel_offset = 0;
9296 ctsio->kern_data_len = ctsio->kern_total_len =
9297 MIN(cdb->length, sizeof(*sense_ptr));
9298
9299 /*
9300 * If we don't have a LUN, we don't have any pending sense.
9301 */
9302 if (lun == NULL ||
9303 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
9304 softc->ha_link < CTL_HA_LINK_UNKNOWN)) {
9305 /* "Logical unit not supported" */
9306 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format,
9307 /*current_error*/ 1,
9308 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
9309 /*asc*/ 0x25,
9310 /*ascq*/ 0x00,
9311 SSD_ELEM_NONE);
9312 goto send;
9313 }
9314
9315 have_error = 0;
9316 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9317 /*
9318 * Check for pending sense, and then for pending unit attentions.
9319 * Pending sense gets returned first, then pending unit attentions.
9320 */
9321 mtx_lock(&lun->lun_lock);
9322 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
9323 if (ps != NULL)
9324 ps += initidx % CTL_MAX_INIT_PER_PORT;
9325 if (ps != NULL && ps->error_code != 0) {
9326 scsi_sense_data_type stored_format;
9327
9328 /*
9329 * Check to see which sense format was used for the stored
9330 * sense data.
9331 */
9332 stored_format = scsi_sense_type(ps);
9333
9334 /*
9335 * If the user requested a different sense format than the
9336 * one we stored, then we need to convert it to the other
9337 * format. If we're going from descriptor to fixed format
9338 * sense data, we may lose things in translation, depending
9339 * on what options were used.
9340 *
9341 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9342 * for some reason we'll just copy it out as-is.
9343 */
9344 if ((stored_format == SSD_TYPE_FIXED)
9345 && (sense_format == SSD_TYPE_DESC))
9346 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9347 ps, (struct scsi_sense_data_desc *)sense_ptr);
9348 else if ((stored_format == SSD_TYPE_DESC)
9349 && (sense_format == SSD_TYPE_FIXED))
9350 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9351 ps, (struct scsi_sense_data_fixed *)sense_ptr);
9352 else
9353 memcpy(sense_ptr, ps, sizeof(*sense_ptr));
9354
9355 ps->error_code = 0;
9356 have_error = 1;
9357 } else {
9358 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len,
9359 sense_format);
9360 if (ua_type != CTL_UA_NONE)
9361 have_error = 1;
9362 }
9363 if (have_error == 0) {
9364 /*
9365 * Report informational exception if have one and allowed.
9366 */
9367 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) {
9368 asc = lun->ie_asc;
9369 ascq = lun->ie_ascq;
9370 }
9371 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format,
9372 /*current_error*/ 1,
9373 /*sense_key*/ SSD_KEY_NO_SENSE,
9374 /*asc*/ asc,
9375 /*ascq*/ ascq,
9376 SSD_ELEM_NONE);
9377 }
9378 mtx_unlock(&lun->lun_lock);
9379
9380 send:
9381 /*
9382 * We report the SCSI status as OK, since the status of the command
9383 * itself is OK. We're reporting sense as parameter data.
9384 */
9385 ctl_set_success(ctsio);
9386 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9387 ctsio->be_move_done = ctl_config_move_done;
9388 ctl_datamove((union ctl_io *)ctsio);
9389 return (CTL_RETVAL_COMPLETE);
9390 }
9391
9392 int
ctl_tur(struct ctl_scsiio * ctsio)9393 ctl_tur(struct ctl_scsiio *ctsio)
9394 {
9395
9396 CTL_DEBUG_PRINT(("ctl_tur\n"));
9397
9398 ctl_set_success(ctsio);
9399 ctl_done((union ctl_io *)ctsio);
9400
9401 return (CTL_RETVAL_COMPLETE);
9402 }
9403
9404 /*
9405 * SCSI VPD page 0x00, the Supported VPD Pages page.
9406 */
9407 static int
ctl_inquiry_evpd_supported(struct ctl_scsiio * ctsio,int alloc_len)9408 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9409 {
9410 struct ctl_lun *lun = CTL_LUN(ctsio);
9411 struct scsi_vpd_supported_pages *pages;
9412 int sup_page_size;
9413 int p;
9414
9415 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9416 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9417 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9418 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9419 ctsio->kern_rel_offset = 0;
9420 ctsio->kern_sg_entries = 0;
9421 ctsio->kern_data_len = min(sup_page_size, alloc_len);
9422 ctsio->kern_total_len = ctsio->kern_data_len;
9423
9424 /*
9425 * The control device is always connected. The disk device, on the
9426 * other hand, may not be online all the time. Need to change this
9427 * to figure out whether the disk device is actually online or not.
9428 */
9429 if (lun != NULL)
9430 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9431 lun->be_lun->lun_type;
9432 else
9433 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9434
9435 p = 0;
9436 /* Supported VPD pages */
9437 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9438 /* Serial Number */
9439 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9440 /* Device Identification */
9441 pages->page_list[p++] = SVPD_DEVICE_ID;
9442 /* Extended INQUIRY Data */
9443 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9444 /* Mode Page Policy */
9445 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9446 /* SCSI Ports */
9447 pages->page_list[p++] = SVPD_SCSI_PORTS;
9448 /* Third-party Copy */
9449 pages->page_list[p++] = SVPD_SCSI_TPC;
9450 /* SCSI Feature Sets */
9451 pages->page_list[p++] = SVPD_SCSI_SFS;
9452 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9453 /* Block limits */
9454 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9455 /* Block Device Characteristics */
9456 pages->page_list[p++] = SVPD_BDC;
9457 /* Logical Block Provisioning */
9458 pages->page_list[p++] = SVPD_LBP;
9459 }
9460 pages->length = p;
9461
9462 ctl_set_success(ctsio);
9463 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9464 ctsio->be_move_done = ctl_config_move_done;
9465 ctl_datamove((union ctl_io *)ctsio);
9466 return (CTL_RETVAL_COMPLETE);
9467 }
9468
9469 /*
9470 * SCSI VPD page 0x80, the Unit Serial Number page.
9471 */
9472 static int
ctl_inquiry_evpd_serial(struct ctl_scsiio * ctsio,int alloc_len)9473 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9474 {
9475 struct ctl_lun *lun = CTL_LUN(ctsio);
9476 struct scsi_vpd_unit_serial_number *sn_ptr;
9477 int data_len;
9478
9479 data_len = 4 + CTL_SN_LEN;
9480 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9481 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9482 ctsio->kern_rel_offset = 0;
9483 ctsio->kern_sg_entries = 0;
9484 ctsio->kern_data_len = min(data_len, alloc_len);
9485 ctsio->kern_total_len = ctsio->kern_data_len;
9486
9487 /*
9488 * The control device is always connected. The disk device, on the
9489 * other hand, may not be online all the time. Need to change this
9490 * to figure out whether the disk device is actually online or not.
9491 */
9492 if (lun != NULL)
9493 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9494 lun->be_lun->lun_type;
9495 else
9496 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9497
9498 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9499 sn_ptr->length = CTL_SN_LEN;
9500 /*
9501 * If we don't have a LUN, we just leave the serial number as
9502 * all spaces.
9503 */
9504 if (lun != NULL) {
9505 strncpy((char *)sn_ptr->serial_num,
9506 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9507 } else
9508 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9509
9510 ctl_set_success(ctsio);
9511 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9512 ctsio->be_move_done = ctl_config_move_done;
9513 ctl_datamove((union ctl_io *)ctsio);
9514 return (CTL_RETVAL_COMPLETE);
9515 }
9516
9517 /*
9518 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9519 */
9520 static int
ctl_inquiry_evpd_eid(struct ctl_scsiio * ctsio,int alloc_len)9521 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9522 {
9523 struct ctl_lun *lun = CTL_LUN(ctsio);
9524 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9525 int data_len;
9526
9527 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9528 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9529 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9530 ctsio->kern_sg_entries = 0;
9531 ctsio->kern_rel_offset = 0;
9532 ctsio->kern_data_len = min(data_len, alloc_len);
9533 ctsio->kern_total_len = ctsio->kern_data_len;
9534
9535 /*
9536 * The control device is always connected. The disk device, on the
9537 * other hand, may not be online all the time.
9538 */
9539 if (lun != NULL)
9540 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9541 lun->be_lun->lun_type;
9542 else
9543 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9544 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9545 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9546 /*
9547 * We support head of queue, ordered and simple tags.
9548 */
9549 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9550 /*
9551 * Volatile cache supported.
9552 */
9553 eid_ptr->flags3 = SVPD_EID_V_SUP;
9554
9555 /*
9556 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9557 * attention for a particular IT nexus on all LUNs once we report
9558 * it to that nexus once. This bit is required as of SPC-4.
9559 */
9560 eid_ptr->flags4 = SVPD_EID_LUICLR;
9561
9562 /*
9563 * We support revert to defaults (RTD) bit in MODE SELECT.
9564 */
9565 eid_ptr->flags5 = SVPD_EID_RTD_SUP;
9566
9567 /*
9568 * XXX KDM in order to correctly answer this, we would need
9569 * information from the SIM to determine how much sense data it
9570 * can send. So this would really be a path inquiry field, most
9571 * likely. This can be set to a maximum of 252 according to SPC-4,
9572 * but the hardware may or may not be able to support that much.
9573 * 0 just means that the maximum sense data length is not reported.
9574 */
9575 eid_ptr->max_sense_length = 0;
9576
9577 ctl_set_success(ctsio);
9578 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9579 ctsio->be_move_done = ctl_config_move_done;
9580 ctl_datamove((union ctl_io *)ctsio);
9581 return (CTL_RETVAL_COMPLETE);
9582 }
9583
9584 static int
ctl_inquiry_evpd_mpp(struct ctl_scsiio * ctsio,int alloc_len)9585 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9586 {
9587 struct ctl_lun *lun = CTL_LUN(ctsio);
9588 struct scsi_vpd_mode_page_policy *mpp_ptr;
9589 int data_len;
9590
9591 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9592 sizeof(struct scsi_vpd_mode_page_policy_descr);
9593
9594 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9595 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9596 ctsio->kern_rel_offset = 0;
9597 ctsio->kern_sg_entries = 0;
9598 ctsio->kern_data_len = min(data_len, alloc_len);
9599 ctsio->kern_total_len = ctsio->kern_data_len;
9600
9601 /*
9602 * The control device is always connected. The disk device, on the
9603 * other hand, may not be online all the time.
9604 */
9605 if (lun != NULL)
9606 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9607 lun->be_lun->lun_type;
9608 else
9609 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9610 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9611 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9612 mpp_ptr->descr[0].page_code = 0x3f;
9613 mpp_ptr->descr[0].subpage_code = 0xff;
9614 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9615
9616 ctl_set_success(ctsio);
9617 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9618 ctsio->be_move_done = ctl_config_move_done;
9619 ctl_datamove((union ctl_io *)ctsio);
9620 return (CTL_RETVAL_COMPLETE);
9621 }
9622
9623 /*
9624 * SCSI VPD page 0x83, the Device Identification page.
9625 */
9626 static int
ctl_inquiry_evpd_devid(struct ctl_scsiio * ctsio,int alloc_len)9627 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9628 {
9629 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9630 struct ctl_port *port = CTL_PORT(ctsio);
9631 struct ctl_lun *lun = CTL_LUN(ctsio);
9632 struct scsi_vpd_device_id *devid_ptr;
9633 struct scsi_vpd_id_descriptor *desc;
9634 int data_len, g;
9635 uint8_t proto;
9636
9637 data_len = sizeof(struct scsi_vpd_device_id) +
9638 sizeof(struct scsi_vpd_id_descriptor) +
9639 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9640 sizeof(struct scsi_vpd_id_descriptor) +
9641 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9642 if (lun && lun->lun_devid)
9643 data_len += lun->lun_devid->len;
9644 if (port && port->port_devid)
9645 data_len += port->port_devid->len;
9646 if (port && port->target_devid)
9647 data_len += port->target_devid->len;
9648
9649 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9650 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9651 ctsio->kern_sg_entries = 0;
9652 ctsio->kern_rel_offset = 0;
9653 ctsio->kern_sg_entries = 0;
9654 ctsio->kern_data_len = min(data_len, alloc_len);
9655 ctsio->kern_total_len = ctsio->kern_data_len;
9656
9657 /*
9658 * The control device is always connected. The disk device, on the
9659 * other hand, may not be online all the time.
9660 */
9661 if (lun != NULL)
9662 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9663 lun->be_lun->lun_type;
9664 else
9665 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9666 devid_ptr->page_code = SVPD_DEVICE_ID;
9667 scsi_ulto2b(data_len - 4, devid_ptr->length);
9668
9669 if (port && port->port_type == CTL_PORT_FC)
9670 proto = SCSI_PROTO_FC << 4;
9671 else if (port && port->port_type == CTL_PORT_SAS)
9672 proto = SCSI_PROTO_SAS << 4;
9673 else if (port && port->port_type == CTL_PORT_ISCSI)
9674 proto = SCSI_PROTO_ISCSI << 4;
9675 else
9676 proto = SCSI_PROTO_SPI << 4;
9677 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9678
9679 /*
9680 * We're using a LUN association here. i.e., this device ID is a
9681 * per-LUN identifier.
9682 */
9683 if (lun && lun->lun_devid) {
9684 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9685 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9686 lun->lun_devid->len);
9687 }
9688
9689 /*
9690 * This is for the WWPN which is a port association.
9691 */
9692 if (port && port->port_devid) {
9693 memcpy(desc, port->port_devid->data, port->port_devid->len);
9694 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9695 port->port_devid->len);
9696 }
9697
9698 /*
9699 * This is for the Relative Target Port(type 4h) identifier
9700 */
9701 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9702 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9703 SVPD_ID_TYPE_RELTARG;
9704 desc->length = 4;
9705 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9706 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9707 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9708
9709 /*
9710 * This is for the Target Port Group(type 5h) identifier
9711 */
9712 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9713 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9714 SVPD_ID_TYPE_TPORTGRP;
9715 desc->length = 4;
9716 if (softc->is_single ||
9717 (port && port->status & CTL_PORT_STATUS_HA_SHARED))
9718 g = 1;
9719 else
9720 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt;
9721 scsi_ulto2b(g, &desc->identifier[2]);
9722 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9723 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9724
9725 /*
9726 * This is for the Target identifier
9727 */
9728 if (port && port->target_devid) {
9729 memcpy(desc, port->target_devid->data, port->target_devid->len);
9730 }
9731
9732 ctl_set_success(ctsio);
9733 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9734 ctsio->be_move_done = ctl_config_move_done;
9735 ctl_datamove((union ctl_io *)ctsio);
9736 return (CTL_RETVAL_COMPLETE);
9737 }
9738
9739 static int
ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio * ctsio,int alloc_len)9740 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9741 {
9742 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9743 struct ctl_lun *lun = CTL_LUN(ctsio);
9744 struct scsi_vpd_scsi_ports *sp;
9745 struct scsi_vpd_port_designation *pd;
9746 struct scsi_vpd_port_designation_cont *pdc;
9747 struct ctl_port *port;
9748 int data_len, num_target_ports, iid_len, id_len;
9749
9750 num_target_ports = 0;
9751 iid_len = 0;
9752 id_len = 0;
9753 mtx_lock(&softc->ctl_lock);
9754 STAILQ_FOREACH(port, &softc->port_list, links) {
9755 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9756 continue;
9757 if (lun != NULL &&
9758 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9759 continue;
9760 num_target_ports++;
9761 if (port->init_devid)
9762 iid_len += port->init_devid->len;
9763 if (port->port_devid)
9764 id_len += port->port_devid->len;
9765 }
9766 mtx_unlock(&softc->ctl_lock);
9767
9768 data_len = sizeof(struct scsi_vpd_scsi_ports) +
9769 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9770 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9771 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9772 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9773 ctsio->kern_sg_entries = 0;
9774 ctsio->kern_rel_offset = 0;
9775 ctsio->kern_sg_entries = 0;
9776 ctsio->kern_data_len = min(data_len, alloc_len);
9777 ctsio->kern_total_len = ctsio->kern_data_len;
9778
9779 /*
9780 * The control device is always connected. The disk device, on the
9781 * other hand, may not be online all the time. Need to change this
9782 * to figure out whether the disk device is actually online or not.
9783 */
9784 if (lun != NULL)
9785 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9786 lun->be_lun->lun_type;
9787 else
9788 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9789
9790 sp->page_code = SVPD_SCSI_PORTS;
9791 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9792 sp->page_length);
9793 pd = &sp->design[0];
9794
9795 mtx_lock(&softc->ctl_lock);
9796 STAILQ_FOREACH(port, &softc->port_list, links) {
9797 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9798 continue;
9799 if (lun != NULL &&
9800 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9801 continue;
9802 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9803 if (port->init_devid) {
9804 iid_len = port->init_devid->len;
9805 memcpy(pd->initiator_transportid,
9806 port->init_devid->data, port->init_devid->len);
9807 } else
9808 iid_len = 0;
9809 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9810 pdc = (struct scsi_vpd_port_designation_cont *)
9811 (&pd->initiator_transportid[iid_len]);
9812 if (port->port_devid) {
9813 id_len = port->port_devid->len;
9814 memcpy(pdc->target_port_descriptors,
9815 port->port_devid->data, port->port_devid->len);
9816 } else
9817 id_len = 0;
9818 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9819 pd = (struct scsi_vpd_port_designation *)
9820 ((uint8_t *)pdc->target_port_descriptors + id_len);
9821 }
9822 mtx_unlock(&softc->ctl_lock);
9823
9824 ctl_set_success(ctsio);
9825 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9826 ctsio->be_move_done = ctl_config_move_done;
9827 ctl_datamove((union ctl_io *)ctsio);
9828 return (CTL_RETVAL_COMPLETE);
9829 }
9830
9831 static int
ctl_inquiry_evpd_sfs(struct ctl_scsiio * ctsio,int alloc_len)9832 ctl_inquiry_evpd_sfs(struct ctl_scsiio *ctsio, int alloc_len)
9833 {
9834 struct ctl_lun *lun = CTL_LUN(ctsio);
9835 struct scsi_vpd_sfs *sfs_ptr;
9836 int sfs_page_size, n;
9837
9838 sfs_page_size = sizeof(*sfs_ptr) + 5 * 2;
9839 ctsio->kern_data_ptr = malloc(sfs_page_size, M_CTL, M_WAITOK | M_ZERO);
9840 sfs_ptr = (struct scsi_vpd_sfs *)ctsio->kern_data_ptr;
9841 ctsio->kern_sg_entries = 0;
9842 ctsio->kern_rel_offset = 0;
9843 ctsio->kern_sg_entries = 0;
9844 ctsio->kern_data_len = min(sfs_page_size, alloc_len);
9845 ctsio->kern_total_len = ctsio->kern_data_len;
9846
9847 /*
9848 * The control device is always connected. The disk device, on the
9849 * other hand, may not be online all the time. Need to change this
9850 * to figure out whether the disk device is actually online or not.
9851 */
9852 if (lun != NULL)
9853 sfs_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9854 lun->be_lun->lun_type;
9855 else
9856 sfs_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9857
9858 sfs_ptr->page_code = SVPD_SCSI_SFS;
9859 n = 0;
9860 /* Discovery 2016 */
9861 scsi_ulto2b(0x0001, &sfs_ptr->codes[2 * n++]);
9862 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9863 /* SBC Base 2016 */
9864 scsi_ulto2b(0x0101, &sfs_ptr->codes[2 * n++]);
9865 /* SBC Base 2010 */
9866 scsi_ulto2b(0x0102, &sfs_ptr->codes[2 * n++]);
9867 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9868 /* Basic Provisioning 2016 */
9869 scsi_ulto2b(0x0103, &sfs_ptr->codes[2 * n++]);
9870 }
9871 /* Drive Maintenance 2016 */
9872 //scsi_ulto2b(0x0104, &sfs_ptr->codes[2 * n++]);
9873 }
9874 scsi_ulto2b(4 + 2 * n, sfs_ptr->page_length);
9875
9876 ctl_set_success(ctsio);
9877 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9878 ctsio->be_move_done = ctl_config_move_done;
9879 ctl_datamove((union ctl_io *)ctsio);
9880 return (CTL_RETVAL_COMPLETE);
9881 }
9882
9883 static int
ctl_inquiry_evpd_block_limits(struct ctl_scsiio * ctsio,int alloc_len)9884 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9885 {
9886 struct ctl_lun *lun = CTL_LUN(ctsio);
9887 struct scsi_vpd_block_limits *bl_ptr;
9888 const char *val;
9889 uint64_t ival;
9890
9891 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9892 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9893 ctsio->kern_sg_entries = 0;
9894 ctsio->kern_rel_offset = 0;
9895 ctsio->kern_sg_entries = 0;
9896 ctsio->kern_data_len = min(sizeof(*bl_ptr), alloc_len);
9897 ctsio->kern_total_len = ctsio->kern_data_len;
9898
9899 /*
9900 * The control device is always connected. The disk device, on the
9901 * other hand, may not be online all the time. Need to change this
9902 * to figure out whether the disk device is actually online or not.
9903 */
9904 if (lun != NULL)
9905 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9906 lun->be_lun->lun_type;
9907 else
9908 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9909
9910 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9911 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9912 bl_ptr->max_cmp_write_len = 0xff;
9913 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9914 if (lun != NULL) {
9915 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9916 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9917 ival = 0xffffffff;
9918 val = dnvlist_get_string(lun->be_lun->options,
9919 "unmap_max_lba", NULL);
9920 if (val != NULL)
9921 ctl_expand_number(val, &ival);
9922 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt);
9923 ival = 0xffffffff;
9924 val = dnvlist_get_string(lun->be_lun->options,
9925 "unmap_max_descr", NULL);
9926 if (val != NULL)
9927 ctl_expand_number(val, &ival);
9928 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt);
9929 if (lun->be_lun->ublockexp != 0) {
9930 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9931 bl_ptr->opt_unmap_grain);
9932 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9933 bl_ptr->unmap_grain_align);
9934 }
9935 }
9936 scsi_ulto4b(lun->be_lun->atomicblock,
9937 bl_ptr->max_atomic_transfer_length);
9938 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9939 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9940 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary);
9941 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size);
9942 ival = UINT64_MAX;
9943 val = dnvlist_get_string(lun->be_lun->options,
9944 "write_same_max_lba", NULL);
9945 if (val != NULL)
9946 ctl_expand_number(val, &ival);
9947 scsi_u64to8b(ival, bl_ptr->max_write_same_length);
9948 if (lun->be_lun->maxlba + 1 > ival)
9949 bl_ptr->flags |= SVPD_BL_WSNZ;
9950 }
9951
9952 ctl_set_success(ctsio);
9953 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9954 ctsio->be_move_done = ctl_config_move_done;
9955 ctl_datamove((union ctl_io *)ctsio);
9956 return (CTL_RETVAL_COMPLETE);
9957 }
9958
9959 static int
ctl_inquiry_evpd_bdc(struct ctl_scsiio * ctsio,int alloc_len)9960 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
9961 {
9962 struct ctl_lun *lun = CTL_LUN(ctsio);
9963 struct scsi_vpd_block_device_characteristics *bdc_ptr;
9964 const char *value;
9965 u_int i;
9966
9967 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
9968 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
9969 ctsio->kern_sg_entries = 0;
9970 ctsio->kern_rel_offset = 0;
9971 ctsio->kern_data_len = min(sizeof(*bdc_ptr), alloc_len);
9972 ctsio->kern_total_len = ctsio->kern_data_len;
9973
9974 /*
9975 * The control device is always connected. The disk device, on the
9976 * other hand, may not be online all the time. Need to change this
9977 * to figure out whether the disk device is actually online or not.
9978 */
9979 if (lun != NULL)
9980 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9981 lun->be_lun->lun_type;
9982 else
9983 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9984 bdc_ptr->page_code = SVPD_BDC;
9985 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
9986 if (lun != NULL &&
9987 (value = dnvlist_get_string(lun->be_lun->options, "rpm", NULL)) != NULL)
9988 i = strtol(value, NULL, 0);
9989 else
9990 i = CTL_DEFAULT_ROTATION_RATE;
9991 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
9992 if (lun != NULL &&
9993 (value = dnvlist_get_string(lun->be_lun->options, "formfactor", NULL)) != NULL)
9994 i = strtol(value, NULL, 0);
9995 else
9996 i = 0;
9997 bdc_ptr->wab_wac_ff = (i & 0x0f);
9998 bdc_ptr->flags = SVPD_RBWZ | SVPD_FUAB | SVPD_VBULS;
9999
10000 ctl_set_success(ctsio);
10001 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10002 ctsio->be_move_done = ctl_config_move_done;
10003 ctl_datamove((union ctl_io *)ctsio);
10004 return (CTL_RETVAL_COMPLETE);
10005 }
10006
10007 static int
ctl_inquiry_evpd_lbp(struct ctl_scsiio * ctsio,int alloc_len)10008 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
10009 {
10010 struct ctl_lun *lun = CTL_LUN(ctsio);
10011 struct scsi_vpd_logical_block_prov *lbp_ptr;
10012 const char *value;
10013
10014 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
10015 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
10016 ctsio->kern_sg_entries = 0;
10017 ctsio->kern_rel_offset = 0;
10018 ctsio->kern_data_len = min(sizeof(*lbp_ptr), alloc_len);
10019 ctsio->kern_total_len = ctsio->kern_data_len;
10020
10021 /*
10022 * The control device is always connected. The disk device, on the
10023 * other hand, may not be online all the time. Need to change this
10024 * to figure out whether the disk device is actually online or not.
10025 */
10026 if (lun != NULL)
10027 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10028 lun->be_lun->lun_type;
10029 else
10030 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10031
10032 lbp_ptr->page_code = SVPD_LBP;
10033 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
10034 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
10035 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10036 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10037 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10038 value = dnvlist_get_string(lun->be_lun->options,
10039 "provisioning_type", NULL);
10040 if (value != NULL) {
10041 if (strcmp(value, "resource") == 0)
10042 lbp_ptr->prov_type = SVPD_LBP_RESOURCE;
10043 else if (strcmp(value, "thin") == 0)
10044 lbp_ptr->prov_type = SVPD_LBP_THIN;
10045 } else
10046 lbp_ptr->prov_type = SVPD_LBP_THIN;
10047 }
10048
10049 ctl_set_success(ctsio);
10050 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10051 ctsio->be_move_done = ctl_config_move_done;
10052 ctl_datamove((union ctl_io *)ctsio);
10053 return (CTL_RETVAL_COMPLETE);
10054 }
10055
10056 /*
10057 * INQUIRY with the EVPD bit set.
10058 */
10059 static int
ctl_inquiry_evpd(struct ctl_scsiio * ctsio)10060 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10061 {
10062 struct ctl_lun *lun = CTL_LUN(ctsio);
10063 struct scsi_inquiry *cdb;
10064 int alloc_len, retval;
10065
10066 cdb = (struct scsi_inquiry *)ctsio->cdb;
10067 alloc_len = scsi_2btoul(cdb->length);
10068
10069 switch (cdb->page_code) {
10070 case SVPD_SUPPORTED_PAGES:
10071 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10072 break;
10073 case SVPD_UNIT_SERIAL_NUMBER:
10074 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10075 break;
10076 case SVPD_DEVICE_ID:
10077 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10078 break;
10079 case SVPD_EXTENDED_INQUIRY_DATA:
10080 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10081 break;
10082 case SVPD_MODE_PAGE_POLICY:
10083 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10084 break;
10085 case SVPD_SCSI_PORTS:
10086 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10087 break;
10088 case SVPD_SCSI_TPC:
10089 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10090 break;
10091 case SVPD_SCSI_SFS:
10092 retval = ctl_inquiry_evpd_sfs(ctsio, alloc_len);
10093 break;
10094 case SVPD_BLOCK_LIMITS:
10095 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10096 goto err;
10097 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10098 break;
10099 case SVPD_BDC:
10100 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10101 goto err;
10102 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10103 break;
10104 case SVPD_LBP:
10105 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10106 goto err;
10107 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10108 break;
10109 default:
10110 err:
10111 ctl_set_invalid_field(ctsio,
10112 /*sks_valid*/ 1,
10113 /*command*/ 1,
10114 /*field*/ 2,
10115 /*bit_valid*/ 0,
10116 /*bit*/ 0);
10117 ctl_done((union ctl_io *)ctsio);
10118 retval = CTL_RETVAL_COMPLETE;
10119 break;
10120 }
10121
10122 return (retval);
10123 }
10124
10125 /*
10126 * Standard INQUIRY data.
10127 */
10128 static int
ctl_inquiry_std(struct ctl_scsiio * ctsio)10129 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10130 {
10131 struct ctl_softc *softc = CTL_SOFTC(ctsio);
10132 struct ctl_port *port = CTL_PORT(ctsio);
10133 struct ctl_lun *lun = CTL_LUN(ctsio);
10134 struct scsi_inquiry_data *inq_ptr;
10135 struct scsi_inquiry *cdb;
10136 const char *val;
10137 uint32_t alloc_len, data_len;
10138 ctl_port_type port_type;
10139
10140 port_type = port->port_type;
10141 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10142 port_type = CTL_PORT_SCSI;
10143
10144 cdb = (struct scsi_inquiry *)ctsio->cdb;
10145 alloc_len = scsi_2btoul(cdb->length);
10146
10147 /*
10148 * We malloc the full inquiry data size here and fill it
10149 * in. If the user only asks for less, we'll give him
10150 * that much.
10151 */
10152 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10153 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10154 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10155 ctsio->kern_sg_entries = 0;
10156 ctsio->kern_rel_offset = 0;
10157 ctsio->kern_data_len = min(data_len, alloc_len);
10158 ctsio->kern_total_len = ctsio->kern_data_len;
10159
10160 if (lun != NULL) {
10161 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
10162 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
10163 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10164 lun->be_lun->lun_type;
10165 } else {
10166 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
10167 lun->be_lun->lun_type;
10168 }
10169 if (lun->flags & CTL_LUN_REMOVABLE)
10170 inq_ptr->dev_qual2 |= SID_RMB;
10171 } else
10172 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10173
10174 /* RMB in byte 2 is 0 */
10175 inq_ptr->version = SCSI_REV_SPC5;
10176
10177 /*
10178 * According to SAM-3, even if a device only supports a single
10179 * level of LUN addressing, it should still set the HISUP bit:
10180 *
10181 * 4.9.1 Logical unit numbers overview
10182 *
10183 * All logical unit number formats described in this standard are
10184 * hierarchical in structure even when only a single level in that
10185 * hierarchy is used. The HISUP bit shall be set to one in the
10186 * standard INQUIRY data (see SPC-2) when any logical unit number
10187 * format described in this standard is used. Non-hierarchical
10188 * formats are outside the scope of this standard.
10189 *
10190 * Therefore we set the HiSup bit here.
10191 *
10192 * The response format is 2, per SPC-3.
10193 */
10194 inq_ptr->response_format = SID_HiSup | 2;
10195
10196 inq_ptr->additional_length = data_len -
10197 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10198 CTL_DEBUG_PRINT(("additional_length = %d\n",
10199 inq_ptr->additional_length));
10200
10201 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10202 if (port_type == CTL_PORT_SCSI)
10203 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10204 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10205 inq_ptr->flags = SID_CmdQue;
10206 if (port_type == CTL_PORT_SCSI)
10207 inq_ptr->flags |= SID_WBus16 | SID_Sync;
10208
10209 /*
10210 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10211 * We have 8 bytes for the vendor name, and 16 bytes for the device
10212 * name and 4 bytes for the revision.
10213 */
10214 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options,
10215 "vendor", NULL)) == NULL) {
10216 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10217 } else {
10218 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10219 strncpy(inq_ptr->vendor, val,
10220 min(sizeof(inq_ptr->vendor), strlen(val)));
10221 }
10222 if (lun == NULL) {
10223 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10224 sizeof(inq_ptr->product));
10225 } else if ((val = dnvlist_get_string(lun->be_lun->options, "product",
10226 NULL)) == NULL) {
10227 switch (lun->be_lun->lun_type) {
10228 case T_DIRECT:
10229 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10230 sizeof(inq_ptr->product));
10231 break;
10232 case T_PROCESSOR:
10233 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10234 sizeof(inq_ptr->product));
10235 break;
10236 case T_CDROM:
10237 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT,
10238 sizeof(inq_ptr->product));
10239 break;
10240 default:
10241 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10242 sizeof(inq_ptr->product));
10243 break;
10244 }
10245 } else {
10246 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10247 strncpy(inq_ptr->product, val,
10248 min(sizeof(inq_ptr->product), strlen(val)));
10249 }
10250
10251 /*
10252 * XXX make this a macro somewhere so it automatically gets
10253 * incremented when we make changes.
10254 */
10255 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options,
10256 "revision", NULL)) == NULL) {
10257 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10258 } else {
10259 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10260 strncpy(inq_ptr->revision, val,
10261 min(sizeof(inq_ptr->revision), strlen(val)));
10262 }
10263
10264 /*
10265 * For parallel SCSI, we support double transition and single
10266 * transition clocking. We also support QAS (Quick Arbitration
10267 * and Selection) and Information Unit transfers on both the
10268 * control and array devices.
10269 */
10270 if (port_type == CTL_PORT_SCSI)
10271 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10272 SID_SPI_IUS;
10273
10274 /* SAM-6 (no version claimed) */
10275 scsi_ulto2b(0x00C0, inq_ptr->version1);
10276 /* SPC-5 (no version claimed) */
10277 scsi_ulto2b(0x05C0, inq_ptr->version2);
10278 if (port_type == CTL_PORT_FC) {
10279 /* FCP-2 ANSI INCITS.350:2003 */
10280 scsi_ulto2b(0x0917, inq_ptr->version3);
10281 } else if (port_type == CTL_PORT_SCSI) {
10282 /* SPI-4 ANSI INCITS.362:200x */
10283 scsi_ulto2b(0x0B56, inq_ptr->version3);
10284 } else if (port_type == CTL_PORT_ISCSI) {
10285 /* iSCSI (no version claimed) */
10286 scsi_ulto2b(0x0960, inq_ptr->version3);
10287 } else if (port_type == CTL_PORT_SAS) {
10288 /* SAS (no version claimed) */
10289 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10290 } else if (port_type == CTL_PORT_UMASS) {
10291 /* USB Mass Storage Class Bulk-Only Transport, Revision 1.0 */
10292 scsi_ulto2b(0x1730, inq_ptr->version3);
10293 }
10294
10295 if (lun == NULL) {
10296 /* SBC-4 (no version claimed) */
10297 scsi_ulto2b(0x0600, inq_ptr->version4);
10298 } else {
10299 switch (lun->be_lun->lun_type) {
10300 case T_DIRECT:
10301 /* SBC-4 (no version claimed) */
10302 scsi_ulto2b(0x0600, inq_ptr->version4);
10303 break;
10304 case T_PROCESSOR:
10305 break;
10306 case T_CDROM:
10307 /* MMC-6 (no version claimed) */
10308 scsi_ulto2b(0x04E0, inq_ptr->version4);
10309 break;
10310 default:
10311 break;
10312 }
10313 }
10314
10315 ctl_set_success(ctsio);
10316 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10317 ctsio->be_move_done = ctl_config_move_done;
10318 ctl_datamove((union ctl_io *)ctsio);
10319 return (CTL_RETVAL_COMPLETE);
10320 }
10321
10322 int
ctl_inquiry(struct ctl_scsiio * ctsio)10323 ctl_inquiry(struct ctl_scsiio *ctsio)
10324 {
10325 struct scsi_inquiry *cdb;
10326 int retval;
10327
10328 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10329
10330 cdb = (struct scsi_inquiry *)ctsio->cdb;
10331 if (cdb->byte2 & SI_EVPD)
10332 retval = ctl_inquiry_evpd(ctsio);
10333 else if (cdb->page_code == 0)
10334 retval = ctl_inquiry_std(ctsio);
10335 else {
10336 ctl_set_invalid_field(ctsio,
10337 /*sks_valid*/ 1,
10338 /*command*/ 1,
10339 /*field*/ 2,
10340 /*bit_valid*/ 0,
10341 /*bit*/ 0);
10342 ctl_done((union ctl_io *)ctsio);
10343 return (CTL_RETVAL_COMPLETE);
10344 }
10345
10346 return (retval);
10347 }
10348
10349 int
ctl_get_config(struct ctl_scsiio * ctsio)10350 ctl_get_config(struct ctl_scsiio *ctsio)
10351 {
10352 struct ctl_lun *lun = CTL_LUN(ctsio);
10353 struct scsi_get_config_header *hdr;
10354 struct scsi_get_config_feature *feature;
10355 struct scsi_get_config *cdb;
10356 uint32_t alloc_len, data_len;
10357 int rt, starting;
10358
10359 cdb = (struct scsi_get_config *)ctsio->cdb;
10360 rt = (cdb->rt & SGC_RT_MASK);
10361 starting = scsi_2btoul(cdb->starting_feature);
10362 alloc_len = scsi_2btoul(cdb->length);
10363
10364 data_len = sizeof(struct scsi_get_config_header) +
10365 sizeof(struct scsi_get_config_feature) + 8 +
10366 sizeof(struct scsi_get_config_feature) + 8 +
10367 sizeof(struct scsi_get_config_feature) + 4 +
10368 sizeof(struct scsi_get_config_feature) + 4 +
10369 sizeof(struct scsi_get_config_feature) + 8 +
10370 sizeof(struct scsi_get_config_feature) +
10371 sizeof(struct scsi_get_config_feature) + 4 +
10372 sizeof(struct scsi_get_config_feature) + 4 +
10373 sizeof(struct scsi_get_config_feature) + 4 +
10374 sizeof(struct scsi_get_config_feature) + 4 +
10375 sizeof(struct scsi_get_config_feature) + 4 +
10376 sizeof(struct scsi_get_config_feature) + 4;
10377 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10378 ctsio->kern_sg_entries = 0;
10379 ctsio->kern_rel_offset = 0;
10380
10381 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr;
10382 if (lun->flags & CTL_LUN_NO_MEDIA)
10383 scsi_ulto2b(0x0000, hdr->current_profile);
10384 else
10385 scsi_ulto2b(0x0010, hdr->current_profile);
10386 feature = (struct scsi_get_config_feature *)(hdr + 1);
10387
10388 if (starting > 0x003b)
10389 goto done;
10390 if (starting > 0x003a)
10391 goto f3b;
10392 if (starting > 0x002b)
10393 goto f3a;
10394 if (starting > 0x002a)
10395 goto f2b;
10396 if (starting > 0x001f)
10397 goto f2a;
10398 if (starting > 0x001e)
10399 goto f1f;
10400 if (starting > 0x001d)
10401 goto f1e;
10402 if (starting > 0x0010)
10403 goto f1d;
10404 if (starting > 0x0003)
10405 goto f10;
10406 if (starting > 0x0002)
10407 goto f3;
10408 if (starting > 0x0001)
10409 goto f2;
10410 if (starting > 0x0000)
10411 goto f1;
10412
10413 /* Profile List */
10414 scsi_ulto2b(0x0000, feature->feature_code);
10415 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT;
10416 feature->add_length = 8;
10417 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */
10418 feature->feature_data[2] = 0x00;
10419 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */
10420 feature->feature_data[6] = 0x01;
10421 feature = (struct scsi_get_config_feature *)
10422 &feature->feature_data[feature->add_length];
10423
10424 f1: /* Core */
10425 scsi_ulto2b(0x0001, feature->feature_code);
10426 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10427 feature->add_length = 8;
10428 scsi_ulto4b(0x00000000, &feature->feature_data[0]);
10429 feature->feature_data[4] = 0x03;
10430 feature = (struct scsi_get_config_feature *)
10431 &feature->feature_data[feature->add_length];
10432
10433 f2: /* Morphing */
10434 scsi_ulto2b(0x0002, feature->feature_code);
10435 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10436 feature->add_length = 4;
10437 feature->feature_data[0] = 0x02;
10438 feature = (struct scsi_get_config_feature *)
10439 &feature->feature_data[feature->add_length];
10440
10441 f3: /* Removable Medium */
10442 scsi_ulto2b(0x0003, feature->feature_code);
10443 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10444 feature->add_length = 4;
10445 feature->feature_data[0] = 0x39;
10446 feature = (struct scsi_get_config_feature *)
10447 &feature->feature_data[feature->add_length];
10448
10449 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA))
10450 goto done;
10451
10452 f10: /* Random Read */
10453 scsi_ulto2b(0x0010, feature->feature_code);
10454 feature->flags = 0x00;
10455 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10456 feature->flags |= SGC_F_CURRENT;
10457 feature->add_length = 8;
10458 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]);
10459 scsi_ulto2b(1, &feature->feature_data[4]);
10460 feature->feature_data[6] = 0x00;
10461 feature = (struct scsi_get_config_feature *)
10462 &feature->feature_data[feature->add_length];
10463
10464 f1d: /* Multi-Read */
10465 scsi_ulto2b(0x001D, feature->feature_code);
10466 feature->flags = 0x00;
10467 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10468 feature->flags |= SGC_F_CURRENT;
10469 feature->add_length = 0;
10470 feature = (struct scsi_get_config_feature *)
10471 &feature->feature_data[feature->add_length];
10472
10473 f1e: /* CD Read */
10474 scsi_ulto2b(0x001E, feature->feature_code);
10475 feature->flags = 0x00;
10476 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10477 feature->flags |= SGC_F_CURRENT;
10478 feature->add_length = 4;
10479 feature->feature_data[0] = 0x00;
10480 feature = (struct scsi_get_config_feature *)
10481 &feature->feature_data[feature->add_length];
10482
10483 f1f: /* DVD Read */
10484 scsi_ulto2b(0x001F, feature->feature_code);
10485 feature->flags = 0x08;
10486 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10487 feature->flags |= SGC_F_CURRENT;
10488 feature->add_length = 4;
10489 feature->feature_data[0] = 0x01;
10490 feature->feature_data[2] = 0x03;
10491 feature = (struct scsi_get_config_feature *)
10492 &feature->feature_data[feature->add_length];
10493
10494 f2a: /* DVD+RW */
10495 scsi_ulto2b(0x002A, feature->feature_code);
10496 feature->flags = 0x04;
10497 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10498 feature->flags |= SGC_F_CURRENT;
10499 feature->add_length = 4;
10500 feature->feature_data[0] = 0x00;
10501 feature->feature_data[1] = 0x00;
10502 feature = (struct scsi_get_config_feature *)
10503 &feature->feature_data[feature->add_length];
10504
10505 f2b: /* DVD+R */
10506 scsi_ulto2b(0x002B, feature->feature_code);
10507 feature->flags = 0x00;
10508 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10509 feature->flags |= SGC_F_CURRENT;
10510 feature->add_length = 4;
10511 feature->feature_data[0] = 0x00;
10512 feature = (struct scsi_get_config_feature *)
10513 &feature->feature_data[feature->add_length];
10514
10515 f3a: /* DVD+RW Dual Layer */
10516 scsi_ulto2b(0x003A, feature->feature_code);
10517 feature->flags = 0x00;
10518 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10519 feature->flags |= SGC_F_CURRENT;
10520 feature->add_length = 4;
10521 feature->feature_data[0] = 0x00;
10522 feature->feature_data[1] = 0x00;
10523 feature = (struct scsi_get_config_feature *)
10524 &feature->feature_data[feature->add_length];
10525
10526 f3b: /* DVD+R Dual Layer */
10527 scsi_ulto2b(0x003B, feature->feature_code);
10528 feature->flags = 0x00;
10529 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10530 feature->flags |= SGC_F_CURRENT;
10531 feature->add_length = 4;
10532 feature->feature_data[0] = 0x00;
10533 feature = (struct scsi_get_config_feature *)
10534 &feature->feature_data[feature->add_length];
10535
10536 done:
10537 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10538 if (rt == SGC_RT_SPECIFIC && data_len > 4) {
10539 feature = (struct scsi_get_config_feature *)(hdr + 1);
10540 if (scsi_2btoul(feature->feature_code) == starting)
10541 feature = (struct scsi_get_config_feature *)
10542 &feature->feature_data[feature->add_length];
10543 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10544 }
10545 scsi_ulto4b(data_len - 4, hdr->data_length);
10546 ctsio->kern_data_len = min(data_len, alloc_len);
10547 ctsio->kern_total_len = ctsio->kern_data_len;
10548
10549 ctl_set_success(ctsio);
10550 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10551 ctsio->be_move_done = ctl_config_move_done;
10552 ctl_datamove((union ctl_io *)ctsio);
10553 return (CTL_RETVAL_COMPLETE);
10554 }
10555
10556 int
ctl_get_event_status(struct ctl_scsiio * ctsio)10557 ctl_get_event_status(struct ctl_scsiio *ctsio)
10558 {
10559 struct scsi_get_event_status_header *hdr;
10560 struct scsi_get_event_status *cdb;
10561 uint32_t alloc_len, data_len;
10562
10563 cdb = (struct scsi_get_event_status *)ctsio->cdb;
10564 if ((cdb->byte2 & SGESN_POLLED) == 0) {
10565 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
10566 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
10567 ctl_done((union ctl_io *)ctsio);
10568 return (CTL_RETVAL_COMPLETE);
10569 }
10570 alloc_len = scsi_2btoul(cdb->length);
10571
10572 data_len = sizeof(struct scsi_get_event_status_header);
10573 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10574 ctsio->kern_sg_entries = 0;
10575 ctsio->kern_rel_offset = 0;
10576 ctsio->kern_data_len = min(data_len, alloc_len);
10577 ctsio->kern_total_len = ctsio->kern_data_len;
10578
10579 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr;
10580 scsi_ulto2b(0, hdr->descr_length);
10581 hdr->nea_class = SGESN_NEA;
10582 hdr->supported_class = 0;
10583
10584 ctl_set_success(ctsio);
10585 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10586 ctsio->be_move_done = ctl_config_move_done;
10587 ctl_datamove((union ctl_io *)ctsio);
10588 return (CTL_RETVAL_COMPLETE);
10589 }
10590
10591 int
ctl_mechanism_status(struct ctl_scsiio * ctsio)10592 ctl_mechanism_status(struct ctl_scsiio *ctsio)
10593 {
10594 struct scsi_mechanism_status_header *hdr;
10595 struct scsi_mechanism_status *cdb;
10596 uint32_t alloc_len, data_len;
10597
10598 cdb = (struct scsi_mechanism_status *)ctsio->cdb;
10599 alloc_len = scsi_2btoul(cdb->length);
10600
10601 data_len = sizeof(struct scsi_mechanism_status_header);
10602 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10603 ctsio->kern_sg_entries = 0;
10604 ctsio->kern_rel_offset = 0;
10605 ctsio->kern_data_len = min(data_len, alloc_len);
10606 ctsio->kern_total_len = ctsio->kern_data_len;
10607
10608 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr;
10609 hdr->state1 = 0x00;
10610 hdr->state2 = 0xe0;
10611 scsi_ulto3b(0, hdr->lba);
10612 hdr->slots_num = 0;
10613 scsi_ulto2b(0, hdr->slots_length);
10614
10615 ctl_set_success(ctsio);
10616 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10617 ctsio->be_move_done = ctl_config_move_done;
10618 ctl_datamove((union ctl_io *)ctsio);
10619 return (CTL_RETVAL_COMPLETE);
10620 }
10621
10622 static void
ctl_ultomsf(uint32_t lba,uint8_t * buf)10623 ctl_ultomsf(uint32_t lba, uint8_t *buf)
10624 {
10625
10626 lba += 150;
10627 buf[0] = 0;
10628 buf[1] = bin2bcd((lba / 75) / 60);
10629 buf[2] = bin2bcd((lba / 75) % 60);
10630 buf[3] = bin2bcd(lba % 75);
10631 }
10632
10633 int
ctl_read_toc(struct ctl_scsiio * ctsio)10634 ctl_read_toc(struct ctl_scsiio *ctsio)
10635 {
10636 struct ctl_lun *lun = CTL_LUN(ctsio);
10637 struct scsi_read_toc_hdr *hdr;
10638 struct scsi_read_toc_type01_descr *descr;
10639 struct scsi_read_toc *cdb;
10640 uint32_t alloc_len, data_len;
10641 int format, msf;
10642
10643 cdb = (struct scsi_read_toc *)ctsio->cdb;
10644 msf = (cdb->byte2 & CD_MSF) != 0;
10645 format = cdb->format;
10646 alloc_len = scsi_2btoul(cdb->data_len);
10647
10648 data_len = sizeof(struct scsi_read_toc_hdr);
10649 if (format == 0)
10650 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr);
10651 else
10652 data_len += sizeof(struct scsi_read_toc_type01_descr);
10653 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10654 ctsio->kern_sg_entries = 0;
10655 ctsio->kern_rel_offset = 0;
10656 ctsio->kern_data_len = min(data_len, alloc_len);
10657 ctsio->kern_total_len = ctsio->kern_data_len;
10658
10659 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr;
10660 if (format == 0) {
10661 scsi_ulto2b(0x12, hdr->data_length);
10662 hdr->first = 1;
10663 hdr->last = 1;
10664 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10665 descr->addr_ctl = 0x14;
10666 descr->track_number = 1;
10667 if (msf)
10668 ctl_ultomsf(0, descr->track_start);
10669 else
10670 scsi_ulto4b(0, descr->track_start);
10671 descr++;
10672 descr->addr_ctl = 0x14;
10673 descr->track_number = 0xaa;
10674 if (msf)
10675 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start);
10676 else
10677 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start);
10678 } else {
10679 scsi_ulto2b(0x0a, hdr->data_length);
10680 hdr->first = 1;
10681 hdr->last = 1;
10682 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10683 descr->addr_ctl = 0x14;
10684 descr->track_number = 1;
10685 if (msf)
10686 ctl_ultomsf(0, descr->track_start);
10687 else
10688 scsi_ulto4b(0, descr->track_start);
10689 }
10690
10691 ctl_set_success(ctsio);
10692 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10693 ctsio->be_move_done = ctl_config_move_done;
10694 ctl_datamove((union ctl_io *)ctsio);
10695 return (CTL_RETVAL_COMPLETE);
10696 }
10697
10698 /*
10699 * For NVMe commands, parse the LBA and length.
10700 */
10701 static bool
ctl_nvme_get_lba_len(struct ctl_nvmeio * ctnio,uint64_t * lba,uint32_t * len)10702 ctl_nvme_get_lba_len(struct ctl_nvmeio *ctnio, uint64_t *lba, uint32_t *len)
10703 {
10704 CTL_IO_ASSERT(ctnio, NVME);
10705
10706 switch (ctnio->cmd.opc) {
10707 case NVME_OPC_WRITE:
10708 case NVME_OPC_READ:
10709 case NVME_OPC_WRITE_UNCORRECTABLE:
10710 case NVME_OPC_COMPARE:
10711 case NVME_OPC_WRITE_ZEROES:
10712 case NVME_OPC_VERIFY:
10713 *lba = (uint64_t)le32toh(ctnio->cmd.cdw11) << 32 |
10714 le32toh(ctnio->cmd.cdw10);
10715 *len = (le32toh(ctnio->cmd.cdw12) & 0xffff) + 1;
10716 return (true);
10717 default:
10718 *lba = 0;
10719 *len = 0;
10720 return (false);
10721 }
10722 }
10723
10724 static bool
ctl_nvme_fua(struct ctl_nvmeio * ctnio)10725 ctl_nvme_fua(struct ctl_nvmeio *ctnio)
10726 {
10727 return ((le32toh(ctnio->cmd.cdw12) & (1U << 30)) != 0);
10728 }
10729
10730 int
ctl_nvme_identify(struct ctl_nvmeio * ctnio)10731 ctl_nvme_identify(struct ctl_nvmeio *ctnio)
10732 {
10733 struct ctl_lun *lun = CTL_LUN(ctnio);
10734 size_t len;
10735 int retval;
10736 uint8_t cns;
10737
10738 CTL_DEBUG_PRINT(("ctl_nvme_identify\n"));
10739
10740 CTL_IO_ASSERT(ctnio, NVME_ADMIN);
10741 MPASS(ctnio->cmd.opc == NVME_OPC_IDENTIFY);
10742
10743 /*
10744 * The data buffer for Identify is always 4096 bytes, see
10745 * 5.51.1 in NVMe base specification 1.4.
10746 */
10747 len = 4096;
10748
10749 ctnio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
10750 ctnio->kern_data_len = len;
10751 ctnio->kern_total_len = len;
10752 ctnio->kern_rel_offset = 0;
10753 ctnio->kern_sg_entries = 0;
10754
10755 ctl_nvme_set_success(ctnio);
10756 ctnio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10757 ctnio->be_move_done = ctl_config_move_done;
10758
10759 /*
10760 * If we don't have a LUN, return an empty result for CNS == 0.
10761 */
10762 if (lun == NULL) {
10763 cns = le32toh(ctnio->cmd.cdw10) & 0xff;
10764 switch (cns) {
10765 case 0:
10766 memset(ctnio->kern_data_ptr, 0, len);
10767 ctl_datamove((union ctl_io *)ctnio);
10768 break;
10769 default:
10770 ctl_nvme_set_invalid_field(ctnio);
10771 break;
10772 }
10773 return (CTL_RETVAL_COMPLETE);
10774 }
10775
10776 retval = lun->backend->config_read((union ctl_io *)ctnio);
10777 return (retval);
10778 }
10779
10780 int
ctl_nvme_flush(struct ctl_nvmeio * ctnio)10781 ctl_nvme_flush(struct ctl_nvmeio *ctnio)
10782 {
10783 struct ctl_lun *lun = CTL_LUN(ctnio);
10784 int retval;
10785
10786 CTL_DEBUG_PRINT(("ctl_nvme_flush\n"));
10787
10788 CTL_IO_ASSERT(ctnio, NVME);
10789 MPASS(ctnio->cmd.opc == NVME_OPC_FLUSH);
10790
10791 /*
10792 * NVMe flushes always flush the entire namespace, not an LBA
10793 * range.
10794 */
10795 retval = lun->backend->config_write((union ctl_io *)ctnio);
10796
10797 return (retval);
10798 }
10799
10800 int
ctl_nvme_read_write(struct ctl_nvmeio * ctnio)10801 ctl_nvme_read_write(struct ctl_nvmeio *ctnio)
10802 {
10803 struct ctl_lun *lun = CTL_LUN(ctnio);
10804 struct ctl_lba_len_flags *lbalen;
10805 uint64_t lba;
10806 uint32_t num_blocks;
10807 int flags, retval;
10808 bool isread;
10809
10810 CTL_DEBUG_PRINT(("ctl_nvme_read_write: command: %#x\n",
10811 ctnio->cmd.opc));
10812
10813 CTL_IO_ASSERT(ctnio, NVME);
10814 MPASS(ctnio->cmd.opc == NVME_OPC_WRITE ||
10815 ctnio->cmd.opc == NVME_OPC_READ);
10816
10817 flags = 0;
10818 isread = ctnio->cmd.opc == NVME_OPC_READ;
10819 ctl_nvme_get_lba_len(ctnio, &lba, &num_blocks);
10820
10821 /*
10822 * The first check is to make sure we're in bounds, the second
10823 * check is to catch wrap-around problems. If the lba + num blocks
10824 * is less than the lba, then we've wrapped around and the block
10825 * range is invalid anyway.
10826 */
10827 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
10828 || ((lba + num_blocks) < lba)) {
10829 ctl_nvme_set_lba_out_of_range(ctnio);
10830 ctl_done((union ctl_io *)ctnio);
10831 return (CTL_RETVAL_COMPLETE);
10832 }
10833
10834 /*
10835 * Set FUA and/or DPO if caches are disabled.
10836 *
10837 * For a read this may not be quite correct for the block
10838 * backend as any earlier writes to the LBA range should be
10839 * flushed to backing store as part of the read.
10840 */
10841 if (ctl_nvme_fua(ctnio)) {
10842 flags |= CTL_LLF_FUA;
10843 if (isread)
10844 flags |= CTL_LLF_DPO;
10845 }
10846
10847 lbalen = (struct ctl_lba_len_flags *)
10848 &ctnio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10849 lbalen->lba = lba;
10850 lbalen->len = num_blocks;
10851 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
10852
10853 ctnio->kern_total_len = num_blocks * lun->be_lun->blocksize;
10854 ctnio->kern_rel_offset = 0;
10855
10856 CTL_DEBUG_PRINT(("ctl_nvme_read_write: calling data_submit()\n"));
10857
10858 retval = lun->backend->data_submit((union ctl_io *)ctnio);
10859 return (retval);
10860 }
10861
10862 int
ctl_nvme_write_uncorrectable(struct ctl_nvmeio * ctnio)10863 ctl_nvme_write_uncorrectable(struct ctl_nvmeio *ctnio)
10864 {
10865 struct ctl_lun *lun = CTL_LUN(ctnio);
10866 struct ctl_lba_len_flags *lbalen;
10867 uint64_t lba;
10868 uint32_t num_blocks;
10869 int retval;
10870
10871 CTL_DEBUG_PRINT(("ctl_nvme_write_uncorrectable\n"));
10872
10873 CTL_IO_ASSERT(ctnio, NVME);
10874 MPASS(ctnio->cmd.opc == NVME_OPC_WRITE_UNCORRECTABLE);
10875
10876 ctl_nvme_get_lba_len(ctnio, &lba, &num_blocks);
10877
10878 /*
10879 * The first check is to make sure we're in bounds, the second
10880 * check is to catch wrap-around problems. If the lba + num blocks
10881 * is less than the lba, then we've wrapped around and the block
10882 * range is invalid anyway.
10883 */
10884 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
10885 || ((lba + num_blocks) < lba)) {
10886 ctl_nvme_set_lba_out_of_range(ctnio);
10887 ctl_done((union ctl_io *)ctnio);
10888 return (CTL_RETVAL_COMPLETE);
10889 }
10890
10891 lbalen = (struct ctl_lba_len_flags *)
10892 &ctnio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10893 lbalen->lba = lba;
10894 lbalen->len = num_blocks;
10895 lbalen->flags = 0;
10896 retval = lun->backend->config_write((union ctl_io *)ctnio);
10897
10898 return (retval);
10899 }
10900
10901 int
ctl_nvme_compare(struct ctl_nvmeio * ctnio)10902 ctl_nvme_compare(struct ctl_nvmeio *ctnio)
10903 {
10904 struct ctl_lun *lun = CTL_LUN(ctnio);
10905 struct ctl_lba_len_flags *lbalen;
10906 uint64_t lba;
10907 uint32_t num_blocks;
10908 int flags;
10909 int retval;
10910
10911 CTL_DEBUG_PRINT(("ctl_nvme_compare\n"));
10912
10913 CTL_IO_ASSERT(ctnio, NVME);
10914 MPASS(ctnio->cmd.opc == NVME_OPC_COMPARE);
10915
10916 flags = 0;
10917 ctl_nvme_get_lba_len(ctnio, &lba, &num_blocks);
10918 if (ctl_nvme_fua(ctnio))
10919 flags |= CTL_LLF_FUA;
10920
10921 /*
10922 * The first check is to make sure we're in bounds, the second
10923 * check is to catch wrap-around problems. If the lba + num blocks
10924 * is less than the lba, then we've wrapped around and the block
10925 * range is invalid anyway.
10926 */
10927 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
10928 || ((lba + num_blocks) < lba)) {
10929 ctl_nvme_set_lba_out_of_range(ctnio);
10930 ctl_done((union ctl_io *)ctnio);
10931 return (CTL_RETVAL_COMPLETE);
10932 }
10933
10934 lbalen = (struct ctl_lba_len_flags *)
10935 &ctnio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10936 lbalen->lba = lba;
10937 lbalen->len = num_blocks;
10938 lbalen->flags = CTL_LLF_COMPARE | flags;
10939 ctnio->kern_total_len = num_blocks * lun->be_lun->blocksize;
10940 ctnio->kern_rel_offset = 0;
10941
10942 CTL_DEBUG_PRINT(("ctl_nvme_compare: calling data_submit()\n"));
10943 retval = lun->backend->data_submit((union ctl_io *)ctnio);
10944 return (retval);
10945 }
10946
10947 int
ctl_nvme_write_zeroes(struct ctl_nvmeio * ctnio)10948 ctl_nvme_write_zeroes(struct ctl_nvmeio *ctnio)
10949 {
10950 struct ctl_lun *lun = CTL_LUN(ctnio);
10951 struct ctl_lba_len_flags *lbalen;
10952 uint64_t lba;
10953 uint32_t num_blocks;
10954 int retval;
10955
10956 CTL_DEBUG_PRINT(("ctl_nvme_write_zeroes\n"));
10957
10958 CTL_IO_ASSERT(ctnio, NVME);
10959 MPASS(ctnio->cmd.opc == NVME_OPC_WRITE_ZEROES);
10960
10961 ctl_nvme_get_lba_len(ctnio, &lba, &num_blocks);
10962
10963 /*
10964 * The first check is to make sure we're in bounds, the second
10965 * check is to catch wrap-around problems. If the lba + num blocks
10966 * is less than the lba, then we've wrapped around and the block
10967 * range is invalid anyway.
10968 */
10969 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
10970 || ((lba + num_blocks) < lba)) {
10971 ctl_nvme_set_lba_out_of_range(ctnio);
10972 ctl_done((union ctl_io *)ctnio);
10973 return (CTL_RETVAL_COMPLETE);
10974 }
10975
10976 lbalen = (struct ctl_lba_len_flags *)
10977 &ctnio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10978 lbalen->lba = lba;
10979 lbalen->len = num_blocks;
10980 lbalen->flags = 0;
10981 retval = lun->backend->config_write((union ctl_io *)ctnio);
10982
10983 return (retval);
10984 }
10985
10986 int
ctl_nvme_dataset_management(struct ctl_nvmeio * ctnio)10987 ctl_nvme_dataset_management(struct ctl_nvmeio *ctnio)
10988 {
10989 struct ctl_lun *lun = CTL_LUN(ctnio);
10990 struct nvme_dsm_range *r;
10991 uint64_t lba;
10992 uint32_t len, num_blocks;
10993 u_int i, ranges;
10994 int retval;
10995
10996 CTL_DEBUG_PRINT(("ctl_nvme_dataset_management\n"));
10997
10998 CTL_IO_ASSERT(ctnio, NVME);
10999 MPASS(ctnio->cmd.opc == NVME_OPC_DATASET_MANAGEMENT);
11000
11001 ranges = le32toh(ctnio->cmd.cdw10) & 0xff;
11002 len = ranges * sizeof(struct nvme_dsm_range);
11003
11004 /*
11005 * If we've got a kernel request that hasn't been malloced yet,
11006 * malloc it and tell the caller the data buffer is here.
11007 */
11008 if ((ctnio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
11009 ctnio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
11010 ctnio->kern_data_len = len;
11011 ctnio->kern_total_len = len;
11012 ctnio->kern_rel_offset = 0;
11013 ctnio->kern_sg_entries = 0;
11014 ctnio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
11015 ctnio->be_move_done = ctl_config_move_done;
11016 ctl_datamove((union ctl_io *)ctnio);
11017
11018 return (CTL_RETVAL_COMPLETE);
11019 }
11020
11021 /*
11022 * Require a flat buffer of the correct size.
11023 */
11024 if (ctnio->kern_sg_entries > 0 ||
11025 ctnio->kern_total_len - ctnio->kern_data_resid != len)
11026 return (CTL_RETVAL_ERROR);
11027
11028 /*
11029 * Verify that none of the ranges are out of bounds.
11030 */
11031 r = (struct nvme_dsm_range *)ctnio->kern_data_ptr;
11032 for (i = 0; i < ranges; i++) {
11033 lba = le64toh(r[i].starting_lba);
11034 num_blocks = le32toh(r[i].length);
11035 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
11036 || ((lba + num_blocks) < lba)) {
11037 ctl_nvme_set_lba_out_of_range(ctnio);
11038 ctl_done((union ctl_io *)ctnio);
11039 return (CTL_RETVAL_COMPLETE);
11040 }
11041 }
11042
11043 CTL_DEBUG_PRINT(("ctl_nvme_dataset_management: calling config_write()\n"));
11044 retval = lun->backend->config_write((union ctl_io *)ctnio);
11045 return (retval);
11046 }
11047
11048 int
ctl_nvme_verify(struct ctl_nvmeio * ctnio)11049 ctl_nvme_verify(struct ctl_nvmeio *ctnio)
11050 {
11051 struct ctl_lun *lun = CTL_LUN(ctnio);
11052 struct ctl_lba_len_flags *lbalen;
11053 uint64_t lba;
11054 uint32_t num_blocks;
11055 int flags;
11056 int retval;
11057
11058 CTL_DEBUG_PRINT(("ctl_nvme_verify\n"));
11059
11060 CTL_IO_ASSERT(ctnio, NVME);
11061 MPASS(ctnio->cmd.opc == NVME_OPC_VERIFY);
11062
11063 flags = 0;
11064 ctl_nvme_get_lba_len(ctnio, &lba, &num_blocks);
11065 if (ctl_nvme_fua(ctnio))
11066 flags |= CTL_LLF_FUA;
11067
11068 /*
11069 * The first check is to make sure we're in bounds, the second
11070 * check is to catch wrap-around problems. If the lba + num blocks
11071 * is less than the lba, then we've wrapped around and the block
11072 * range is invalid anyway.
11073 */
11074 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
11075 || ((lba + num_blocks) < lba)) {
11076 ctl_nvme_set_lba_out_of_range(ctnio);
11077 ctl_done((union ctl_io *)ctnio);
11078 return (CTL_RETVAL_COMPLETE);
11079 }
11080
11081 lbalen = (struct ctl_lba_len_flags *)
11082 &ctnio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
11083 lbalen->lba = lba;
11084 lbalen->len = num_blocks;
11085 lbalen->flags = CTL_LLF_VERIFY | flags;
11086 ctnio->kern_total_len = 0;
11087 ctnio->kern_rel_offset = 0;
11088
11089 CTL_DEBUG_PRINT(("ctl_nvme_verify: calling data_submit()\n"));
11090 retval = lun->backend->data_submit((union ctl_io *)ctnio);
11091 return (retval);
11092 }
11093
11094 static const struct ctl_nvme_cmd_entry *
ctl_nvme_get_cmd_entry(struct ctl_nvmeio * ctnio)11095 ctl_nvme_get_cmd_entry(struct ctl_nvmeio *ctnio)
11096 {
11097 const struct ctl_nvme_cmd_entry *entry;
11098
11099 switch (ctnio->io_hdr.io_type) {
11100 case CTL_IO_NVME:
11101 entry = &nvme_nvm_cmd_table[ctnio->cmd.opc];
11102 break;
11103 case CTL_IO_NVME_ADMIN:
11104 entry = &nvme_admin_cmd_table[ctnio->cmd.opc];
11105 break;
11106 default:
11107 __assert_unreachable();
11108 }
11109 return (entry);
11110 }
11111
11112 static const struct ctl_nvme_cmd_entry *
ctl_nvme_validate_command(struct ctl_nvmeio * ctnio)11113 ctl_nvme_validate_command(struct ctl_nvmeio *ctnio)
11114 {
11115 const struct ctl_nvme_cmd_entry *entry;
11116
11117 entry = ctl_nvme_get_cmd_entry(ctnio);
11118 if (entry->execute == NULL) {
11119 ctl_nvme_set_invalid_opcode(ctnio);
11120 ctl_done((union ctl_io *)ctnio);
11121 return (NULL);
11122 }
11123
11124 /* Validate fused commands. */
11125 switch (NVMEV(NVME_CMD_FUSE, ctnio->cmd.fuse)) {
11126 case NVME_FUSE_NORMAL:
11127 break;
11128 case NVME_FUSE_FIRST:
11129 if (ctnio->io_hdr.io_type != CTL_IO_NVME ||
11130 ctnio->cmd.opc != NVME_OPC_COMPARE) {
11131 ctl_nvme_set_invalid_field(ctnio);
11132 ctl_done((union ctl_io *)ctnio);
11133 return (NULL);
11134 }
11135 break;
11136 case NVME_FUSE_SECOND:
11137 if (ctnio->io_hdr.io_type != CTL_IO_NVME ||
11138 ctnio->cmd.opc != NVME_OPC_COMPARE) {
11139 ctl_nvme_set_invalid_field(ctnio);
11140 ctl_done((union ctl_io *)ctnio);
11141 return (NULL);
11142 }
11143 break;
11144 default:
11145 ctl_nvme_set_invalid_field(ctnio);
11146 ctl_done((union ctl_io *)ctnio);
11147 return (NULL);
11148 }
11149
11150 return (entry);
11151 }
11152
11153 /*
11154 * This is a simpler version of ctl_scsiio_lun_check that fails
11155 * requests on a LUN without active media.
11156 *
11157 * Returns true if the command has been completed with an error.
11158 */
11159 static bool
ctl_nvmeio_lun_check(struct ctl_lun * lun,const struct ctl_nvme_cmd_entry * entry,struct ctl_nvmeio * ctnio)11160 ctl_nvmeio_lun_check(struct ctl_lun *lun,
11161 const struct ctl_nvme_cmd_entry *entry, struct ctl_nvmeio *ctnio)
11162 {
11163 mtx_assert(&lun->lun_lock, MA_OWNED);
11164
11165 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) {
11166 if ((lun->flags & (CTL_LUN_EJECTED | CTL_LUN_NO_MEDIA |
11167 CTL_LUN_STOPPED)) != 0) {
11168 ctl_nvme_set_namespace_not_ready(ctnio);
11169 return (true);
11170 }
11171 }
11172
11173 return (false);
11174 }
11175
11176 /*
11177 * Check for blockage against the OOA (Order Of Arrival) queue.
11178 * Assumptions:
11179 * - pending_io is generally either incoming, or on the blocked queue
11180 * - starting I/O is the I/O we want to start the check with.
11181 */
11182 static ctl_action
ctl_nvme_check_ooa(struct ctl_lun * lun,union ctl_io * pending_io,union ctl_io ** starting_io,union ctl_io ** aborted_io)11183 ctl_nvme_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
11184 union ctl_io **starting_io, union ctl_io **aborted_io)
11185 {
11186 union ctl_io *ooa_io = *starting_io;
11187
11188 CTL_IO_ASSERT(pending_io, NVME, NVME_ADMIN);
11189
11190 mtx_assert(&lun->lun_lock, MA_OWNED);
11191
11192 *aborted_io = NULL;
11193
11194 /*
11195 * Aborted commands are not going to be executed and may even
11196 * not report completion, so we don't care about their order.
11197 * Let them complete ASAP to clean the OOA queue.
11198 */
11199 if (__predict_false(pending_io->io_hdr.flags & CTL_FLAG_ABORT))
11200 return (CTL_ACTION_PASS);
11201
11202 /*
11203 * NVMe has rather simple command ordering requirements. In
11204 * particular, there is no requirement on the controller to
11205 * enforce a specific order for overlapping LBAs. The only
11206 * constraint is that fused operations (Compare and Write),
11207 * must be completed as a unit.
11208 *
11209 * To support fused operations, the following strategy is used:
11210 * - the first half of a fused command is not enqueued to rtr
11211 * until the second half is enqueued
11212 * - the second half of a fused command blocks on the first
11213 * half of a fuse command
11214 * - subsequent commands block on the second half of the
11215 * fused command
11216 */
11217
11218 /*
11219 * Is the previously submitted command the first half of a
11220 * fused operation?
11221 */
11222 if (ooa_io != NULL &&
11223 NVMEV(NVME_CMD_FUSE, ooa_io->nvmeio.cmd.fuse) == NVME_FUSE_FIRST) {
11224 /*
11225 * If this is the second half, enqueue the first half
11226 * and block the second half on the first half.
11227 */
11228 if (NVMEV(NVME_CMD_FUSE, pending_io->nvmeio.cmd.fuse) ==
11229 NVME_FUSE_SECOND) {
11230 /*
11231 * XXX: Do we need to wait for other rtr requests
11232 * to drain so this is truly atomic?
11233 */
11234 return (CTL_ACTION_FUSED);
11235 }
11236
11237 /* Abort the first half. */
11238 ctl_nvme_set_missing_fused_command(&ooa_io->nvmeio);
11239 *aborted_io = ooa_io;
11240 } else {
11241 switch (NVMEV(NVME_CMD_FUSE, pending_io->nvmeio.cmd.fuse)) {
11242 case NVME_FUSE_FIRST:
11243 /* First half, wait for the second half. */
11244 return (CTL_ACTION_SKIP);
11245 case NVME_FUSE_SECOND:
11246 /* Second half without a matching first half, abort. */
11247 ctl_nvme_set_missing_fused_command(&pending_io->nvmeio);
11248 *aborted_io = pending_io;
11249 return (CTL_ACTION_SKIP);
11250 }
11251 }
11252
11253 /*
11254 * Scan the OOA queue looking for the most recent second half
11255 * of a fused op.
11256 */
11257 for (; ooa_io != NULL;
11258 ooa_io = (union ctl_io *)LIST_NEXT(&ooa_io->io_hdr, ooa_links)) {
11259 if (NVMEV(NVME_CMD_FUSE, ooa_io->nvmeio.cmd.fuse) ==
11260 NVME_FUSE_SECOND) {
11261 *starting_io = ooa_io;
11262 return (CTL_ACTION_BLOCK);
11263 }
11264 }
11265
11266 *starting_io = NULL;
11267 return (CTL_ACTION_PASS);
11268 }
11269
11270 static void
ctl_nvmeio_precheck(struct ctl_nvmeio * ctnio)11271 ctl_nvmeio_precheck(struct ctl_nvmeio *ctnio)
11272 {
11273 struct ctl_softc *softc = CTL_SOFTC(ctnio);
11274 struct ctl_lun *lun;
11275 const struct ctl_nvme_cmd_entry *entry;
11276 union ctl_io *bio, *aborted_io;
11277 uint32_t targ_lun;
11278
11279 lun = NULL;
11280 targ_lun = ctnio->io_hdr.nexus.targ_mapped_lun;
11281 if (targ_lun < ctl_max_luns)
11282 lun = softc->ctl_luns[targ_lun];
11283 if (lun != NULL) {
11284 /*
11285 * If the LUN is invalid, pretend that it doesn't exist.
11286 * It will go away as soon as all pending I/O has been
11287 * completed.
11288 */
11289 mtx_lock(&lun->lun_lock);
11290 if (lun->flags & CTL_LUN_DISABLED) {
11291 mtx_unlock(&lun->lun_lock);
11292 lun = NULL;
11293 }
11294 }
11295 CTL_LUN(ctnio) = lun;
11296 if (lun != NULL) {
11297 CTL_BACKEND_LUN(ctnio) = lun->be_lun;
11298
11299 /*
11300 * Every I/O goes into the OOA queue for a particular LUN,
11301 * and stays there until completion.
11302 */
11303 #ifdef CTL_TIME_IO
11304 if (LIST_EMPTY(&lun->ooa_queue))
11305 lun->idle_time += getsbinuptime() - lun->last_busy;
11306 #endif
11307 LIST_INSERT_HEAD(&lun->ooa_queue, &ctnio->io_hdr, ooa_links);
11308 }
11309
11310 /* Get command entry and return error if it is unsupported. */
11311 entry = ctl_nvme_validate_command(ctnio);
11312 if (entry == NULL) {
11313 if (lun)
11314 mtx_unlock(&lun->lun_lock);
11315 return;
11316 }
11317
11318 ctnio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11319 ctnio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11320
11321 /* All NVMe commands other than IDENTIFY require a LUN. */
11322 if (lun == NULL) {
11323 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11324 ctnio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11325 ctl_enqueue_rtr((union ctl_io *)ctnio);
11326 return;
11327 }
11328
11329 ctl_nvme_set_invalid_namespace(ctnio);
11330 ctl_done((union ctl_io *)ctnio);
11331 CTL_DEBUG_PRINT(("ctl_nvmeio_precheck: bailing out due to invalid LUN\n"));
11332 return;
11333 } else {
11334 /*
11335 * NVMe namespaces can only be backed by T_DIRECT LUNs.
11336 */
11337 if (lun->be_lun->lun_type != T_DIRECT) {
11338 mtx_unlock(&lun->lun_lock);
11339 ctl_nvme_set_invalid_namespace(ctnio);
11340 ctl_done((union ctl_io *)ctnio);
11341 return;
11342 }
11343 }
11344
11345 if (ctl_nvmeio_lun_check(lun, entry, ctnio) != 0) {
11346 mtx_unlock(&lun->lun_lock);
11347 ctl_done((union ctl_io *)ctnio);
11348 return;
11349 }
11350
11351 bio = (union ctl_io *)LIST_NEXT(&ctnio->io_hdr, ooa_links);
11352 switch (ctl_nvme_check_ooa(lun, (union ctl_io *)ctnio, &bio,
11353 &aborted_io)) {
11354 case CTL_ACTION_PASS:
11355 ctnio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11356 mtx_unlock(&lun->lun_lock);
11357 ctl_enqueue_rtr((union ctl_io *)ctnio);
11358 break;
11359 case CTL_ACTION_FUSED:
11360 /* Block the second half on the first half. */
11361 ctnio->io_hdr.blocker = bio;
11362 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctnio->io_hdr,
11363 blocked_links);
11364
11365 /* Pass the first half. */
11366 bio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11367 mtx_unlock(&lun->lun_lock);
11368 ctl_enqueue_rtr(bio);
11369 break;
11370 case CTL_ACTION_SKIP:
11371 mtx_unlock(&lun->lun_lock);
11372 break;
11373 case CTL_ACTION_BLOCK:
11374 ctnio->io_hdr.blocker = bio;
11375 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctnio->io_hdr,
11376 blocked_links);
11377 mtx_unlock(&lun->lun_lock);
11378 break;
11379 default:
11380 __assert_unreachable();
11381 }
11382 if (aborted_io != NULL)
11383 ctl_done(aborted_io);
11384 }
11385
11386 static int
ctl_nvmeio(struct ctl_nvmeio * ctnio)11387 ctl_nvmeio(struct ctl_nvmeio *ctnio)
11388 {
11389 const struct ctl_nvme_cmd_entry *entry;
11390 int retval;
11391
11392 CTL_DEBUG_PRINT(("ctl_nvmeio %s opc=%02X\n",
11393 ctnio->io_hdr.io_type == CTL_IO_NVME ? "nvm" : "admin",
11394 ctnio->cmd.opc));
11395
11396 entry = ctl_nvme_get_cmd_entry(ctnio);
11397 MPASS(entry != NULL);
11398
11399 /*
11400 * If this I/O has been aborted, just send it straight to
11401 * ctl_done() without executing it.
11402 */
11403 if (ctnio->io_hdr.flags & CTL_FLAG_ABORT) {
11404 ctl_done((union ctl_io *)ctnio);
11405 return (CTL_RETVAL_COMPLETE);
11406 }
11407
11408 /*
11409 * All the checks should have been handled by ctl_nvmeio_precheck().
11410 * We should be clear now to just execute the I/O.
11411 */
11412 retval = entry->execute(ctnio);
11413
11414 return (retval);
11415 }
11416
11417 /*
11418 * For known CDB types, parse the LBA and length.
11419 */
11420 static int
ctl_get_lba_len(union ctl_io * io,uint64_t * lba,uint64_t * len)11421 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
11422 {
11423
11424 CTL_IO_ASSERT(io, SCSI);
11425
11426 switch (io->scsiio.cdb[0]) {
11427 case COMPARE_AND_WRITE: {
11428 struct scsi_compare_and_write *cdb;
11429
11430 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
11431
11432 *lba = scsi_8btou64(cdb->addr);
11433 *len = cdb->length;
11434 break;
11435 }
11436 case READ_6:
11437 case WRITE_6: {
11438 struct scsi_rw_6 *cdb;
11439
11440 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
11441
11442 *lba = scsi_3btoul(cdb->addr);
11443 /* only 5 bits are valid in the most significant address byte */
11444 *lba &= 0x1fffff;
11445 *len = cdb->length;
11446 break;
11447 }
11448 case READ_10:
11449 case WRITE_10: {
11450 struct scsi_rw_10 *cdb;
11451
11452 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
11453
11454 *lba = scsi_4btoul(cdb->addr);
11455 *len = scsi_2btoul(cdb->length);
11456 break;
11457 }
11458 case WRITE_VERIFY_10: {
11459 struct scsi_write_verify_10 *cdb;
11460
11461 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
11462
11463 *lba = scsi_4btoul(cdb->addr);
11464 *len = scsi_2btoul(cdb->length);
11465 break;
11466 }
11467 case READ_12:
11468 case WRITE_12: {
11469 struct scsi_rw_12 *cdb;
11470
11471 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
11472
11473 *lba = scsi_4btoul(cdb->addr);
11474 *len = scsi_4btoul(cdb->length);
11475 break;
11476 }
11477 case WRITE_VERIFY_12: {
11478 struct scsi_write_verify_12 *cdb;
11479
11480 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
11481
11482 *lba = scsi_4btoul(cdb->addr);
11483 *len = scsi_4btoul(cdb->length);
11484 break;
11485 }
11486 case READ_16:
11487 case WRITE_16: {
11488 struct scsi_rw_16 *cdb;
11489
11490 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
11491
11492 *lba = scsi_8btou64(cdb->addr);
11493 *len = scsi_4btoul(cdb->length);
11494 break;
11495 }
11496 case WRITE_ATOMIC_16: {
11497 struct scsi_write_atomic_16 *cdb;
11498
11499 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb;
11500
11501 *lba = scsi_8btou64(cdb->addr);
11502 *len = scsi_2btoul(cdb->length);
11503 break;
11504 }
11505 case WRITE_VERIFY_16: {
11506 struct scsi_write_verify_16 *cdb;
11507
11508 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
11509
11510 *lba = scsi_8btou64(cdb->addr);
11511 *len = scsi_4btoul(cdb->length);
11512 break;
11513 }
11514 case WRITE_SAME_10: {
11515 struct scsi_write_same_10 *cdb;
11516
11517 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
11518
11519 *lba = scsi_4btoul(cdb->addr);
11520 *len = scsi_2btoul(cdb->length);
11521 break;
11522 }
11523 case WRITE_SAME_16: {
11524 struct scsi_write_same_16 *cdb;
11525
11526 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
11527
11528 *lba = scsi_8btou64(cdb->addr);
11529 *len = scsi_4btoul(cdb->length);
11530 break;
11531 }
11532 case VERIFY_10: {
11533 struct scsi_verify_10 *cdb;
11534
11535 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
11536
11537 *lba = scsi_4btoul(cdb->addr);
11538 *len = scsi_2btoul(cdb->length);
11539 break;
11540 }
11541 case VERIFY_12: {
11542 struct scsi_verify_12 *cdb;
11543
11544 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
11545
11546 *lba = scsi_4btoul(cdb->addr);
11547 *len = scsi_4btoul(cdb->length);
11548 break;
11549 }
11550 case VERIFY_16: {
11551 struct scsi_verify_16 *cdb;
11552
11553 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
11554
11555 *lba = scsi_8btou64(cdb->addr);
11556 *len = scsi_4btoul(cdb->length);
11557 break;
11558 }
11559 case UNMAP: {
11560 *lba = 0;
11561 *len = UINT64_MAX;
11562 break;
11563 }
11564 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
11565 struct scsi_get_lba_status *cdb;
11566
11567 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
11568 *lba = scsi_8btou64(cdb->addr);
11569 *len = UINT32_MAX;
11570 break;
11571 }
11572 default:
11573 *lba = 0;
11574 *len = UINT64_MAX;
11575 return (1);
11576 }
11577
11578 return (0);
11579 }
11580
11581 static ctl_action
ctl_extent_check_lba(uint64_t lba1,uint64_t len1,uint64_t lba2,uint64_t len2,bool seq)11582 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
11583 bool seq)
11584 {
11585 uint64_t endlba1, endlba2;
11586
11587 endlba1 = lba1 + len1 - (seq ? 0 : 1);
11588 endlba2 = lba2 + len2 - 1;
11589
11590 if ((endlba1 < lba2) || (endlba2 < lba1))
11591 return (CTL_ACTION_PASS);
11592 else
11593 return (CTL_ACTION_BLOCK);
11594 }
11595
11596 static int
ctl_extent_check_unmap(union ctl_io * io,uint64_t lba2,uint64_t len2)11597 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
11598 {
11599 struct ctl_ptr_len_flags *ptrlen;
11600 struct scsi_unmap_desc *buf, *end, *range;
11601 uint64_t lba;
11602 uint32_t len;
11603
11604 CTL_IO_ASSERT(io, SCSI);
11605
11606 /* If not UNMAP -- go other way. */
11607 if (io->scsiio.cdb[0] != UNMAP)
11608 return (CTL_ACTION_SKIP);
11609
11610 /* If UNMAP without data -- block and wait for data. */
11611 ptrlen = (struct ctl_ptr_len_flags *)
11612 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
11613 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
11614 ptrlen->ptr == NULL)
11615 return (CTL_ACTION_BLOCK);
11616
11617 /* UNMAP with data -- check for collision. */
11618 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
11619 end = buf + ptrlen->len / sizeof(*buf);
11620 for (range = buf; range < end; range++) {
11621 lba = scsi_8btou64(range->lba);
11622 len = scsi_4btoul(range->length);
11623 if ((lba < lba2 + len2) && (lba + len > lba2))
11624 return (CTL_ACTION_BLOCK);
11625 }
11626 return (CTL_ACTION_PASS);
11627 }
11628
11629 static ctl_action
ctl_extent_check(union ctl_io * io1,union ctl_io * io2,bool seq)11630 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
11631 {
11632 uint64_t lba1, lba2;
11633 uint64_t len1, len2;
11634 int retval;
11635
11636 retval = ctl_get_lba_len(io2, &lba2, &len2);
11637 KASSERT(retval == 0, ("ctl_get_lba_len() error"));
11638
11639 retval = ctl_extent_check_unmap(io1, lba2, len2);
11640 if (retval != CTL_ACTION_SKIP)
11641 return (retval);
11642
11643 retval = ctl_get_lba_len(io1, &lba1, &len1);
11644 KASSERT(retval == 0, ("ctl_get_lba_len() error"));
11645
11646 if (seq && (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE))
11647 seq = FALSE;
11648 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
11649 }
11650
11651 static ctl_action
ctl_seq_check(union ctl_io * io1,union ctl_io * io2)11652 ctl_seq_check(union ctl_io *io1, union ctl_io *io2)
11653 {
11654 uint64_t lba1, lba2;
11655 uint64_t len1, len2;
11656 int retval __diagused;
11657
11658 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
11659 return (CTL_ACTION_PASS);
11660 retval = ctl_get_lba_len(io1, &lba1, &len1);
11661 KASSERT(retval == 0, ("ctl_get_lba_len() error"));
11662 retval = ctl_get_lba_len(io2, &lba2, &len2);
11663 KASSERT(retval == 0, ("ctl_get_lba_len() error"));
11664
11665 if (lba1 + len1 == lba2)
11666 return (CTL_ACTION_BLOCK);
11667 return (CTL_ACTION_PASS);
11668 }
11669
11670 static ctl_action
ctl_check_for_blockage(struct ctl_lun * lun,union ctl_io * pending_io,const uint8_t * serialize_row,union ctl_io * ooa_io)11671 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
11672 const uint8_t *serialize_row, union ctl_io *ooa_io)
11673 {
11674 CTL_IO_ASSERT(pending_io, SCSI);
11675 CTL_IO_ASSERT(ooa_io, SCSI);
11676
11677 /*
11678 * The initiator attempted multiple untagged commands at the same
11679 * time. Can't do that.
11680 */
11681 if (__predict_false(pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11682 && __predict_false(ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11683 && ((pending_io->io_hdr.nexus.targ_port ==
11684 ooa_io->io_hdr.nexus.targ_port)
11685 && (pending_io->io_hdr.nexus.initid ==
11686 ooa_io->io_hdr.nexus.initid))
11687 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
11688 CTL_FLAG_STATUS_SENT)) == 0))
11689 return (CTL_ACTION_OVERLAP);
11690
11691 /*
11692 * The initiator attempted to send multiple tagged commands with
11693 * the same ID. (It's fine if different initiators have the same
11694 * tag ID.)
11695 *
11696 * Even if all of those conditions are true, we don't kill the I/O
11697 * if the command ahead of us has been aborted. We won't end up
11698 * sending it to the FETD, and it's perfectly legal to resend a
11699 * command with the same tag number as long as the previous
11700 * instance of this tag number has been aborted somehow.
11701 */
11702 if (__predict_true(pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11703 && __predict_true(ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11704 && __predict_false(pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
11705 && ((pending_io->io_hdr.nexus.targ_port ==
11706 ooa_io->io_hdr.nexus.targ_port)
11707 && (pending_io->io_hdr.nexus.initid ==
11708 ooa_io->io_hdr.nexus.initid))
11709 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
11710 CTL_FLAG_STATUS_SENT)) == 0))
11711 return (CTL_ACTION_OVERLAP_TAG);
11712
11713 /*
11714 * If we get a head of queue tag, SAM-3 says that we should
11715 * immediately execute it.
11716 *
11717 * What happens if this command would normally block for some other
11718 * reason? e.g. a request sense with a head of queue tag
11719 * immediately after a write. Normally that would block, but this
11720 * will result in its getting executed immediately...
11721 *
11722 * We currently return "pass" instead of "skip", so we'll end up
11723 * going through the rest of the queue to check for overlapped tags.
11724 *
11725 * XXX KDM check for other types of blockage first??
11726 */
11727 if (__predict_false(pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE))
11728 return (CTL_ACTION_PASS);
11729
11730 /*
11731 * Simple tags get blocked until all head of queue and ordered tags
11732 * ahead of them have completed. I'm lumping untagged commands in
11733 * with simple tags here. XXX KDM is that the right thing to do?
11734 */
11735 if (__predict_false(ooa_io->scsiio.tag_type == CTL_TAG_ORDERED) ||
11736 __predict_false(ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE))
11737 return (CTL_ACTION_BLOCK);
11738
11739 /* Unsupported command in OOA queue. */
11740 if (__predict_false(ooa_io->scsiio.seridx == CTL_SERIDX_INVLD))
11741 return (CTL_ACTION_PASS);
11742
11743 switch (serialize_row[ooa_io->scsiio.seridx]) {
11744 case CTL_SER_SEQ:
11745 if (lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
11746 return (ctl_seq_check(ooa_io, pending_io));
11747 /* FALLTHROUGH */
11748 case CTL_SER_PASS:
11749 return (CTL_ACTION_PASS);
11750 case CTL_SER_EXTENTOPT:
11751 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) ==
11752 SCP_QUEUE_ALG_UNRESTRICTED)
11753 return (CTL_ACTION_PASS);
11754 /* FALLTHROUGH */
11755 case CTL_SER_EXTENT:
11756 return (ctl_extent_check(ooa_io, pending_io,
11757 (lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
11758 case CTL_SER_BLOCKOPT:
11759 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) ==
11760 SCP_QUEUE_ALG_UNRESTRICTED)
11761 return (CTL_ACTION_PASS);
11762 /* FALLTHROUGH */
11763 case CTL_SER_BLOCK:
11764 return (CTL_ACTION_BLOCK);
11765 default:
11766 __assert_unreachable();
11767 }
11768 }
11769
11770 /*
11771 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
11772 * Assumptions:
11773 * - pending_io is generally either incoming, or on the blocked queue
11774 * - starting I/O is the I/O we want to start the check with.
11775 */
11776 static ctl_action
ctl_check_ooa(struct ctl_lun * lun,union ctl_io * pending_io,union ctl_io ** starting_io)11777 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
11778 union ctl_io **starting_io)
11779 {
11780 union ctl_io *ooa_io = *starting_io;
11781 const uint8_t *serialize_row;
11782 ctl_action action;
11783
11784 CTL_IO_ASSERT(pending_io, SCSI);
11785
11786 mtx_assert(&lun->lun_lock, MA_OWNED);
11787
11788 /*
11789 * Aborted commands are not going to be executed and may even
11790 * not report completion, so we don't care about their order.
11791 * Let them complete ASAP to clean the OOA queue.
11792 */
11793 if (__predict_false(pending_io->io_hdr.flags & CTL_FLAG_ABORT))
11794 return (CTL_ACTION_SKIP);
11795
11796 /*
11797 * Ordered tags have to block until all items ahead of them have
11798 * completed. If we get called with an ordered tag, we always
11799 * block, if something else is ahead of us in the queue.
11800 */
11801 if ((pending_io->scsiio.tag_type == CTL_TAG_ORDERED) &&
11802 (ooa_io != NULL))
11803 return (CTL_ACTION_BLOCK);
11804
11805 serialize_row = ctl_serialize_table[pending_io->scsiio.seridx];
11806
11807 /*
11808 * Run back along the OOA queue, starting with the current
11809 * blocked I/O and going through every I/O before it on the
11810 * queue. If starting_io is NULL, we'll just end up returning
11811 * CTL_ACTION_PASS.
11812 */
11813 for (; ooa_io != NULL;
11814 ooa_io = (union ctl_io *)LIST_NEXT(&ooa_io->io_hdr, ooa_links)) {
11815 action = ctl_check_for_blockage(lun, pending_io, serialize_row,
11816 ooa_io);
11817 if (action != CTL_ACTION_PASS) {
11818 *starting_io = ooa_io;
11819 return (action);
11820 }
11821 }
11822
11823 *starting_io = NULL;
11824 return (CTL_ACTION_PASS);
11825 }
11826
11827 /*
11828 * Try to unblock the specified I/O.
11829 *
11830 * skip parameter allows explicitly skip present blocker of the I/O,
11831 * starting from the previous one on OOA queue. It can be used when
11832 * we know for sure that the blocker I/O does no longer count.
11833 */
11834 static void
ctl_scsi_try_unblock_io(struct ctl_lun * lun,union ctl_io * io,bool skip)11835 ctl_scsi_try_unblock_io(struct ctl_lun *lun, union ctl_io *io, bool skip)
11836 {
11837 struct ctl_softc *softc = lun->ctl_softc;
11838 union ctl_io *bio, *obio;
11839 const struct ctl_cmd_entry *entry;
11840 union ctl_ha_msg msg_info;
11841 ctl_action action;
11842
11843 CTL_IO_ASSERT(io, SCSI);
11844
11845 mtx_assert(&lun->lun_lock, MA_OWNED);
11846
11847 if (io->io_hdr.blocker == NULL)
11848 return;
11849
11850 obio = bio = io->io_hdr.blocker;
11851 if (skip)
11852 bio = (union ctl_io *)LIST_NEXT(&bio->io_hdr, ooa_links);
11853 action = ctl_check_ooa(lun, io, &bio);
11854 if (action == CTL_ACTION_BLOCK) {
11855 /* Still blocked, but may be by different I/O now. */
11856 if (bio != obio) {
11857 TAILQ_REMOVE(&obio->io_hdr.blocked_queue,
11858 &io->io_hdr, blocked_links);
11859 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue,
11860 &io->io_hdr, blocked_links);
11861 io->io_hdr.blocker = bio;
11862 }
11863 return;
11864 }
11865
11866 /* No longer blocked, one way or another. */
11867 TAILQ_REMOVE(&obio->io_hdr.blocked_queue, &io->io_hdr, blocked_links);
11868 io->io_hdr.blocker = NULL;
11869
11870 switch (action) {
11871 case CTL_ACTION_PASS:
11872 case CTL_ACTION_SKIP:
11873
11874 /* Serializing commands from the other SC retire there. */
11875 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) &&
11876 (softc->ha_mode != CTL_HA_MODE_XFER)) {
11877 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11878 msg_info.hdr.original_sc = io->io_hdr.remote_io;
11879 msg_info.hdr.serializing_sc = io;
11880 msg_info.hdr.msg_type = CTL_MSG_R2R;
11881 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11882 sizeof(msg_info.hdr), M_NOWAIT);
11883 break;
11884 }
11885
11886 /*
11887 * Check this I/O for LUN state changes that may have happened
11888 * while this command was blocked. The LUN state may have been
11889 * changed by a command ahead of us in the queue.
11890 */
11891 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
11892 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) {
11893 ctl_done(io);
11894 break;
11895 }
11896
11897 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11898 ctl_enqueue_rtr(io);
11899 break;
11900 default:
11901 __assert_unreachable();
11902 case CTL_ACTION_OVERLAP:
11903 ctl_set_overlapped_cmd(&io->scsiio);
11904 goto error;
11905 case CTL_ACTION_OVERLAP_TAG:
11906 ctl_set_overlapped_tag(&io->scsiio,
11907 io->scsiio.tag_num & 0xff);
11908 error:
11909 /* Serializing commands from the other SC are done here. */
11910 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) &&
11911 (softc->ha_mode != CTL_HA_MODE_XFER)) {
11912 ctl_try_unblock_others(lun, io, TRUE);
11913 LIST_REMOVE(&io->io_hdr, ooa_links);
11914
11915 ctl_copy_sense_data_back(io, &msg_info);
11916 msg_info.hdr.original_sc = io->io_hdr.remote_io;
11917 msg_info.hdr.serializing_sc = NULL;
11918 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
11919 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11920 sizeof(msg_info.scsi), M_WAITOK);
11921 ctl_free_io(io);
11922 break;
11923 }
11924
11925 ctl_done(io);
11926 break;
11927 }
11928 }
11929
11930 static void
ctl_nvme_try_unblock_io(struct ctl_lun * lun,union ctl_io * io,bool skip)11931 ctl_nvme_try_unblock_io(struct ctl_lun *lun, union ctl_io *io, bool skip)
11932 {
11933 union ctl_io *bio;
11934 const struct ctl_nvme_cmd_entry *entry;
11935
11936 CTL_IO_ASSERT(io, NVME, NVME_ADMIN);
11937
11938 mtx_assert(&lun->lun_lock, MA_OWNED);
11939
11940 if (io->io_hdr.blocker == NULL)
11941 return;
11942
11943 /*
11944 * If this is the second half of a fused operation, it should
11945 * be the only io on the blocked list. If the first half
11946 * failed, complete the second half with an appropriate error.
11947 */
11948 bio = io->io_hdr.blocker;
11949 if (NVMEV(NVME_CMD_FUSE, io->nvmeio.cmd.fuse) == NVME_FUSE_SECOND) {
11950 MPASS(io ==
11951 (union ctl_io *)TAILQ_FIRST(&bio->io_hdr.blocked_queue));
11952 MPASS(TAILQ_NEXT(&io->io_hdr, blocked_links) == NULL);
11953
11954 TAILQ_REMOVE(&bio->io_hdr.blocked_queue, &io->io_hdr,
11955 blocked_links);
11956 if (bio->io_hdr.status != CTL_SUCCESS) {
11957 ctl_nvme_set_failed_fused_command(&io->nvmeio);
11958 ctl_done(io);
11959 return;
11960 }
11961 } else {
11962 /*
11963 * This must be a command that was blocked on the
11964 * second half of a fused operation.
11965 */
11966 MPASS(NVMEV(NVME_CMD_FUSE, bio->nvmeio.cmd.fuse) ==
11967 NVME_FUSE_SECOND);
11968 TAILQ_REMOVE(&bio->io_hdr.blocked_queue, &io->io_hdr,
11969 blocked_links);
11970 }
11971
11972 entry = ctl_nvme_get_cmd_entry(&io->nvmeio);
11973 if (ctl_nvmeio_lun_check(lun, entry, &io->nvmeio) != 0) {
11974 ctl_done(io);
11975 return;
11976 }
11977
11978 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11979 ctl_enqueue_rtr(io);
11980 }
11981
11982 static void
ctl_try_unblock_io(struct ctl_lun * lun,union ctl_io * io,bool skip)11983 ctl_try_unblock_io(struct ctl_lun *lun, union ctl_io *io, bool skip)
11984 {
11985 switch (io->io_hdr.io_type) {
11986 case CTL_IO_SCSI:
11987 return (ctl_scsi_try_unblock_io(lun, io, skip));
11988 case CTL_IO_NVME:
11989 case CTL_IO_NVME_ADMIN:
11990 return (ctl_nvme_try_unblock_io(lun, io, skip));
11991 default:
11992 __assert_unreachable();
11993 }
11994 }
11995
11996 /*
11997 * Try to unblock I/Os blocked by the specified I/O.
11998 *
11999 * skip parameter allows explicitly skip the specified I/O as blocker,
12000 * starting from the previous one on the OOA queue. It can be used when
12001 * we know for sure that the specified I/O does no longer count (done).
12002 * It has to be still on OOA queue though so that we know where to start.
12003 */
12004 static void
ctl_try_unblock_others(struct ctl_lun * lun,union ctl_io * bio,bool skip)12005 ctl_try_unblock_others(struct ctl_lun *lun, union ctl_io *bio, bool skip)
12006 {
12007 union ctl_io *io, *next_io;
12008
12009 mtx_assert(&lun->lun_lock, MA_OWNED);
12010
12011 for (io = (union ctl_io *)TAILQ_FIRST(&bio->io_hdr.blocked_queue);
12012 io != NULL; io = next_io) {
12013 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, blocked_links);
12014
12015 KASSERT(io->io_hdr.blocker != NULL,
12016 ("I/O %p on blocked list without blocker", io));
12017 ctl_try_unblock_io(lun, io, skip);
12018 }
12019 KASSERT(!skip || TAILQ_EMPTY(&bio->io_hdr.blocked_queue),
12020 ("blocked_queue is not empty after skipping %p", bio));
12021 }
12022
12023 /*
12024 * This routine (with one exception) checks LUN flags that can be set by
12025 * commands ahead of us in the OOA queue. These flags have to be checked
12026 * when a command initially comes in, and when we pull a command off the
12027 * blocked queue and are preparing to execute it. The reason we have to
12028 * check these flags for commands on the blocked queue is that the LUN
12029 * state may have been changed by a command ahead of us while we're on the
12030 * blocked queue.
12031 *
12032 * Ordering is somewhat important with these checks, so please pay
12033 * careful attention to the placement of any new checks.
12034 */
12035 static int
ctl_scsiio_lun_check(struct ctl_lun * lun,const struct ctl_cmd_entry * entry,struct ctl_scsiio * ctsio)12036 ctl_scsiio_lun_check(struct ctl_lun *lun,
12037 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
12038 {
12039 struct ctl_softc *softc = lun->ctl_softc;
12040 int retval;
12041 uint32_t residx;
12042
12043 retval = 0;
12044
12045 mtx_assert(&lun->lun_lock, MA_OWNED);
12046
12047 /*
12048 * If this shelf is a secondary shelf controller, we may have to
12049 * reject some commands disallowed by HA mode and link state.
12050 */
12051 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
12052 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
12053 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
12054 ctl_set_lun_unavail(ctsio);
12055 retval = 1;
12056 goto bailout;
12057 }
12058 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
12059 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
12060 ctl_set_lun_transit(ctsio);
12061 retval = 1;
12062 goto bailout;
12063 }
12064 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
12065 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
12066 ctl_set_lun_standby(ctsio);
12067 retval = 1;
12068 goto bailout;
12069 }
12070
12071 /* The rest of checks are only done on executing side */
12072 if (softc->ha_mode == CTL_HA_MODE_XFER)
12073 goto bailout;
12074 }
12075
12076 if (entry->pattern & CTL_LUN_PAT_WRITE) {
12077 if (lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
12078 ctl_set_hw_write_protected(ctsio);
12079 retval = 1;
12080 goto bailout;
12081 }
12082 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) {
12083 ctl_set_sense(ctsio, /*current_error*/ 1,
12084 /*sense_key*/ SSD_KEY_DATA_PROTECT,
12085 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
12086 retval = 1;
12087 goto bailout;
12088 }
12089 }
12090
12091 /*
12092 * Check for a reservation conflict. If this command isn't allowed
12093 * even on reserved LUNs, and if this initiator isn't the one who
12094 * reserved us, reject the command with a reservation conflict.
12095 */
12096 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
12097 if ((lun->flags & CTL_LUN_RESERVED)
12098 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
12099 if (lun->res_idx != residx) {
12100 ctl_set_reservation_conflict(ctsio);
12101 retval = 1;
12102 goto bailout;
12103 }
12104 }
12105
12106 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
12107 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
12108 /* No reservation or command is allowed. */;
12109 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
12110 (lun->pr_res_type == SPR_TYPE_WR_EX ||
12111 lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
12112 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) {
12113 /* The command is allowed for Write Exclusive resv. */;
12114 } else {
12115 /*
12116 * if we aren't registered or it's a res holder type
12117 * reservation and this isn't the res holder then set a
12118 * conflict.
12119 */
12120 if (ctl_get_prkey(lun, residx) == 0 ||
12121 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) {
12122 ctl_set_reservation_conflict(ctsio);
12123 retval = 1;
12124 goto bailout;
12125 }
12126 }
12127
12128 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) {
12129 if (lun->flags & CTL_LUN_EJECTED)
12130 ctl_set_lun_ejected(ctsio);
12131 else if (lun->flags & CTL_LUN_NO_MEDIA) {
12132 if (lun->flags & CTL_LUN_REMOVABLE)
12133 ctl_set_lun_no_media(ctsio);
12134 else
12135 ctl_set_lun_int_reqd(ctsio);
12136 } else if (lun->flags & CTL_LUN_STOPPED)
12137 ctl_set_lun_stopped(ctsio);
12138 else
12139 goto bailout;
12140 retval = 1;
12141 goto bailout;
12142 }
12143
12144 bailout:
12145 return (retval);
12146 }
12147
12148 static void
ctl_failover_io(union ctl_io * io,int have_lock)12149 ctl_failover_io(union ctl_io *io, int have_lock)
12150 {
12151 CTL_IO_ASSERT(io, SCSI);
12152
12153 ctl_set_busy(&io->scsiio);
12154 ctl_done(io);
12155 }
12156
12157 static void
ctl_failover_lun(union ctl_io * rio)12158 ctl_failover_lun(union ctl_io *rio)
12159 {
12160 struct ctl_softc *softc = CTL_SOFTC(rio);
12161 struct ctl_lun *lun;
12162 struct ctl_io_hdr *io, *next_io;
12163 uint32_t targ_lun;
12164
12165 targ_lun = rio->io_hdr.nexus.targ_mapped_lun;
12166 CTL_DEBUG_PRINT(("FAILOVER for lun %u\n", targ_lun));
12167
12168 /* Find and lock the LUN. */
12169 mtx_lock(&softc->ctl_lock);
12170 if (targ_lun > ctl_max_luns ||
12171 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12172 mtx_unlock(&softc->ctl_lock);
12173 return;
12174 }
12175 mtx_lock(&lun->lun_lock);
12176 mtx_unlock(&softc->ctl_lock);
12177 if (lun->flags & CTL_LUN_DISABLED) {
12178 mtx_unlock(&lun->lun_lock);
12179 return;
12180 }
12181
12182 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12183 LIST_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
12184 /* We are master */
12185 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
12186 if (io->flags & CTL_FLAG_IO_ACTIVE) {
12187 io->flags |= CTL_FLAG_ABORT |
12188 CTL_FLAG_FAILOVER;
12189 ctl_try_unblock_io(lun,
12190 (union ctl_io *)io, FALSE);
12191 } else { /* This can be only due to DATAMOVE */
12192 io->msg_type = CTL_MSG_DATAMOVE_DONE;
12193 io->flags &= ~CTL_FLAG_DMA_INPROG;
12194 io->flags |= CTL_FLAG_IO_ACTIVE;
12195 io->port_status = 31340;
12196 ctl_enqueue_isc((union ctl_io *)io);
12197 }
12198 } else
12199 /* We are slave */
12200 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
12201 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
12202 if (io->flags & CTL_FLAG_IO_ACTIVE) {
12203 io->flags |= CTL_FLAG_FAILOVER;
12204 } else {
12205 ctl_set_busy(&((union ctl_io *)io)->
12206 scsiio);
12207 ctl_done((union ctl_io *)io);
12208 }
12209 }
12210 }
12211 } else { /* SERIALIZE modes */
12212 LIST_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
12213 /* We are master */
12214 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
12215 if (io->blocker != NULL) {
12216 TAILQ_REMOVE(&io->blocker->io_hdr.blocked_queue,
12217 io, blocked_links);
12218 io->blocker = NULL;
12219 }
12220 ctl_try_unblock_others(lun, (union ctl_io *)io,
12221 TRUE);
12222 LIST_REMOVE(io, ooa_links);
12223 ctl_free_io((union ctl_io *)io);
12224 } else
12225 /* We are slave */
12226 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
12227 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
12228 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
12229 ctl_set_busy(&((union ctl_io *)io)->
12230 scsiio);
12231 ctl_done((union ctl_io *)io);
12232 }
12233 }
12234 }
12235 }
12236 mtx_unlock(&lun->lun_lock);
12237 }
12238
12239 static void
ctl_scsiio_precheck(struct ctl_scsiio * ctsio)12240 ctl_scsiio_precheck(struct ctl_scsiio *ctsio)
12241 {
12242 struct ctl_softc *softc = CTL_SOFTC(ctsio);
12243 struct ctl_lun *lun;
12244 const struct ctl_cmd_entry *entry;
12245 union ctl_io *bio;
12246 uint32_t initidx, targ_lun;
12247
12248 lun = NULL;
12249 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
12250 if (targ_lun < ctl_max_luns)
12251 lun = softc->ctl_luns[targ_lun];
12252 if (lun) {
12253 /*
12254 * If the LUN is invalid, pretend that it doesn't exist.
12255 * It will go away as soon as all pending I/O has been
12256 * completed.
12257 */
12258 mtx_lock(&lun->lun_lock);
12259 if (lun->flags & CTL_LUN_DISABLED) {
12260 mtx_unlock(&lun->lun_lock);
12261 lun = NULL;
12262 }
12263 }
12264 CTL_LUN(ctsio) = lun;
12265 if (lun) {
12266 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
12267
12268 /*
12269 * Every I/O goes into the OOA queue for a particular LUN,
12270 * and stays there until completion.
12271 */
12272 #ifdef CTL_TIME_IO
12273 if (LIST_EMPTY(&lun->ooa_queue))
12274 lun->idle_time += getsbinuptime() - lun->last_busy;
12275 #endif
12276 LIST_INSERT_HEAD(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
12277 }
12278
12279 /* Get command entry and return error if it is unsuppotyed. */
12280 entry = ctl_validate_command(ctsio);
12281 if (entry == NULL) {
12282 if (lun)
12283 mtx_unlock(&lun->lun_lock);
12284 return;
12285 }
12286
12287 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
12288 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
12289
12290 /*
12291 * Check to see whether we can send this command to LUNs that don't
12292 * exist. This should pretty much only be the case for inquiry
12293 * and request sense. Further checks, below, really require having
12294 * a LUN, so we can't really check the command anymore. Just put
12295 * it on the rtr queue.
12296 */
12297 if (lun == NULL) {
12298 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
12299 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12300 ctl_enqueue_rtr((union ctl_io *)ctsio);
12301 return;
12302 }
12303
12304 ctl_set_unsupported_lun(ctsio);
12305 ctl_done((union ctl_io *)ctsio);
12306 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
12307 return;
12308 } else {
12309 /*
12310 * Make sure we support this particular command on this LUN.
12311 * e.g., we don't support writes to the control LUN.
12312 */
12313 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
12314 mtx_unlock(&lun->lun_lock);
12315 ctl_set_invalid_opcode(ctsio);
12316 ctl_done((union ctl_io *)ctsio);
12317 return;
12318 }
12319 }
12320
12321 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
12322
12323 /*
12324 * If we've got a request sense, it'll clear the contingent
12325 * allegiance condition. Otherwise, if we have a CA condition for
12326 * this initiator, clear it, because it sent down a command other
12327 * than request sense.
12328 */
12329 if (ctsio->cdb[0] != REQUEST_SENSE) {
12330 struct scsi_sense_data *ps;
12331
12332 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
12333 if (ps != NULL)
12334 ps[initidx % CTL_MAX_INIT_PER_PORT].error_code = 0;
12335 }
12336
12337 /*
12338 * If the command has this flag set, it handles its own unit
12339 * attention reporting, we shouldn't do anything. Otherwise we
12340 * check for any pending unit attentions, and send them back to the
12341 * initiator. We only do this when a command initially comes in,
12342 * not when we pull it off the blocked queue.
12343 *
12344 * According to SAM-3, section 5.3.2, the order that things get
12345 * presented back to the host is basically unit attentions caused
12346 * by some sort of reset event, busy status, reservation conflicts
12347 * or task set full, and finally any other status.
12348 *
12349 * One issue here is that some of the unit attentions we report
12350 * don't fall into the "reset" category (e.g. "reported luns data
12351 * has changed"). So reporting it here, before the reservation
12352 * check, may be technically wrong. I guess the only thing to do
12353 * would be to check for and report the reset events here, and then
12354 * check for the other unit attention types after we check for a
12355 * reservation conflict.
12356 *
12357 * XXX KDM need to fix this
12358 */
12359 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
12360 ctl_ua_type ua_type;
12361 u_int sense_len = 0;
12362
12363 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
12364 &sense_len, SSD_TYPE_NONE);
12365 if (ua_type != CTL_UA_NONE) {
12366 mtx_unlock(&lun->lun_lock);
12367 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
12368 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12369 ctsio->sense_len = sense_len;
12370 ctl_done((union ctl_io *)ctsio);
12371 return;
12372 }
12373 }
12374
12375 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
12376 mtx_unlock(&lun->lun_lock);
12377 ctl_done((union ctl_io *)ctsio);
12378 return;
12379 }
12380
12381 /*
12382 * XXX CHD this is where we want to send IO to other side if
12383 * this LUN is secondary on this SC. We will need to make a copy
12384 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
12385 * the copy we send as FROM_OTHER.
12386 * We also need to stuff the address of the original IO so we can
12387 * find it easily. Something similar will need be done on the other
12388 * side so when we are done we can find the copy.
12389 */
12390 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
12391 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
12392 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
12393 union ctl_ha_msg msg_info;
12394 int isc_retval;
12395
12396 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
12397 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12398 mtx_unlock(&lun->lun_lock);
12399
12400 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
12401 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
12402 msg_info.hdr.serializing_sc = NULL;
12403 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
12404 msg_info.scsi.tag_num = ctsio->tag_num;
12405 msg_info.scsi.tag_type = ctsio->tag_type;
12406 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
12407 msg_info.scsi.cdb_len = ctsio->cdb_len;
12408 msg_info.scsi.priority = ctsio->priority;
12409
12410 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12411 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
12412 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
12413 ctsio->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
12414 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
12415 ctl_set_busy(ctsio);
12416 ctl_done((union ctl_io *)ctsio);
12417 return;
12418 }
12419 return;
12420 }
12421
12422 bio = (union ctl_io *)LIST_NEXT(&ctsio->io_hdr, ooa_links);
12423 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, &bio)) {
12424 case CTL_ACTION_PASS:
12425 case CTL_ACTION_SKIP:
12426 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12427 mtx_unlock(&lun->lun_lock);
12428 ctl_enqueue_rtr((union ctl_io *)ctsio);
12429 break;
12430 case CTL_ACTION_BLOCK:
12431 ctsio->io_hdr.blocker = bio;
12432 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctsio->io_hdr,
12433 blocked_links);
12434 mtx_unlock(&lun->lun_lock);
12435 break;
12436 case CTL_ACTION_OVERLAP:
12437 mtx_unlock(&lun->lun_lock);
12438 ctl_set_overlapped_cmd(ctsio);
12439 ctl_done((union ctl_io *)ctsio);
12440 break;
12441 case CTL_ACTION_OVERLAP_TAG:
12442 mtx_unlock(&lun->lun_lock);
12443 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
12444 ctl_done((union ctl_io *)ctsio);
12445 break;
12446 default:
12447 __assert_unreachable();
12448 }
12449 }
12450
12451 const struct ctl_cmd_entry *
ctl_get_cmd_entry(struct ctl_scsiio * ctsio,int * sa)12452 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
12453 {
12454 const struct ctl_cmd_entry *entry;
12455 int service_action;
12456
12457 entry = &ctl_cmd_table[ctsio->cdb[0]];
12458 if (sa)
12459 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
12460 if (entry->flags & CTL_CMD_FLAG_SA5) {
12461 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
12462 entry = &((const struct ctl_cmd_entry *)
12463 entry->execute)[service_action];
12464 }
12465 return (entry);
12466 }
12467
12468 const struct ctl_cmd_entry *
ctl_validate_command(struct ctl_scsiio * ctsio)12469 ctl_validate_command(struct ctl_scsiio *ctsio)
12470 {
12471 const struct ctl_cmd_entry *entry;
12472 int i, sa;
12473 uint8_t diff;
12474
12475 entry = ctl_get_cmd_entry(ctsio, &sa);
12476 ctsio->seridx = entry->seridx;
12477 if (entry->execute == NULL) {
12478 if (sa)
12479 ctl_set_invalid_field(ctsio,
12480 /*sks_valid*/ 1,
12481 /*command*/ 1,
12482 /*field*/ 1,
12483 /*bit_valid*/ 1,
12484 /*bit*/ 4);
12485 else
12486 ctl_set_invalid_opcode(ctsio);
12487 ctl_done((union ctl_io *)ctsio);
12488 return (NULL);
12489 }
12490 KASSERT(entry->length > 0,
12491 ("Not defined length for command 0x%02x/0x%02x",
12492 ctsio->cdb[0], ctsio->cdb[1]));
12493 for (i = 1; i < entry->length; i++) {
12494 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
12495 if (diff == 0)
12496 continue;
12497 ctl_set_invalid_field(ctsio,
12498 /*sks_valid*/ 1,
12499 /*command*/ 1,
12500 /*field*/ i,
12501 /*bit_valid*/ 1,
12502 /*bit*/ fls(diff) - 1);
12503 ctl_done((union ctl_io *)ctsio);
12504 return (NULL);
12505 }
12506 return (entry);
12507 }
12508
12509 static int
ctl_cmd_applicable(uint8_t lun_type,const struct ctl_cmd_entry * entry)12510 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
12511 {
12512
12513 switch (lun_type) {
12514 case T_DIRECT:
12515 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0)
12516 return (0);
12517 break;
12518 case T_PROCESSOR:
12519 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
12520 return (0);
12521 break;
12522 case T_CDROM:
12523 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0)
12524 return (0);
12525 break;
12526 default:
12527 return (0);
12528 }
12529 return (1);
12530 }
12531
12532 static int
ctl_scsiio(struct ctl_scsiio * ctsio)12533 ctl_scsiio(struct ctl_scsiio *ctsio)
12534 {
12535 int retval;
12536 const struct ctl_cmd_entry *entry;
12537
12538 retval = CTL_RETVAL_COMPLETE;
12539
12540 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
12541
12542 entry = ctl_get_cmd_entry(ctsio, NULL);
12543
12544 /*
12545 * If this I/O has been aborted, just send it straight to
12546 * ctl_done() without executing it.
12547 */
12548 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
12549 ctl_done((union ctl_io *)ctsio);
12550 goto bailout;
12551 }
12552
12553 /*
12554 * All the checks should have been handled by ctl_scsiio_precheck().
12555 * We should be clear now to just execute the I/O.
12556 */
12557 retval = entry->execute(ctsio);
12558
12559 bailout:
12560 return (retval);
12561 }
12562
12563 static int
ctl_target_reset(union ctl_io * io)12564 ctl_target_reset(union ctl_io *io)
12565 {
12566 struct ctl_softc *softc = CTL_SOFTC(io);
12567 struct ctl_port *port = CTL_PORT(io);
12568 struct ctl_lun *lun;
12569 uint32_t initidx;
12570 ctl_ua_type ua_type;
12571
12572 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12573 union ctl_ha_msg msg_info;
12574
12575 msg_info.hdr.nexus = io->io_hdr.nexus;
12576 msg_info.task.task_action = io->taskio.task_action;
12577 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12578 msg_info.hdr.original_sc = NULL;
12579 msg_info.hdr.serializing_sc = NULL;
12580 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12581 sizeof(msg_info.task), M_WAITOK);
12582 }
12583
12584 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12585 if (io->taskio.task_action == CTL_TASK_TARGET_RESET)
12586 ua_type = CTL_UA_TARG_RESET;
12587 else
12588 ua_type = CTL_UA_BUS_RESET;
12589 mtx_lock(&softc->ctl_lock);
12590 STAILQ_FOREACH(lun, &softc->lun_list, links) {
12591 if (port != NULL &&
12592 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
12593 continue;
12594 ctl_do_lun_reset(lun, initidx, ua_type);
12595 }
12596 mtx_unlock(&softc->ctl_lock);
12597 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12598 return (0);
12599 }
12600
12601 /*
12602 * The LUN should always be set. The I/O is optional, and is used to
12603 * distinguish between I/Os sent by this initiator, and by other
12604 * initiators. We set unit attention for initiators other than this one.
12605 * SAM-3 is vague on this point. It does say that a unit attention should
12606 * be established for other initiators when a LUN is reset (see section
12607 * 5.7.3), but it doesn't specifically say that the unit attention should
12608 * be established for this particular initiator when a LUN is reset. Here
12609 * is the relevant text, from SAM-3 rev 8:
12610 *
12611 * 5.7.2 When a SCSI initiator port aborts its own tasks
12612 *
12613 * When a SCSI initiator port causes its own task(s) to be aborted, no
12614 * notification that the task(s) have been aborted shall be returned to
12615 * the SCSI initiator port other than the completion response for the
12616 * command or task management function action that caused the task(s) to
12617 * be aborted and notification(s) associated with related effects of the
12618 * action (e.g., a reset unit attention condition).
12619 *
12620 * XXX KDM for now, we're setting unit attention for all initiators.
12621 */
12622 static void
ctl_do_lun_reset(struct ctl_lun * lun,uint32_t initidx,ctl_ua_type ua_type)12623 ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua_type)
12624 {
12625 struct ctl_io_hdr *xioh;
12626 int i;
12627
12628 mtx_lock(&lun->lun_lock);
12629 /* Abort tasks. */
12630 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) {
12631 xioh->flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
12632 ctl_try_unblock_io(lun, (union ctl_io *)xioh, FALSE);
12633 }
12634 /* Clear CA. */
12635 for (i = 0; i < ctl_max_ports; i++) {
12636 free(lun->pending_sense[i], M_CTL);
12637 lun->pending_sense[i] = NULL;
12638 }
12639 /* Clear reservation. */
12640 lun->flags &= ~CTL_LUN_RESERVED;
12641 /* Clear prevent media removal. */
12642 if (lun->prevent) {
12643 for (i = 0; i < CTL_MAX_INITIATORS; i++)
12644 ctl_clear_mask(lun->prevent, i);
12645 lun->prevent_count = 0;
12646 }
12647 /* Clear TPC status */
12648 ctl_tpc_lun_clear(lun, -1);
12649 /* Establish UA. */
12650 #if 0
12651 ctl_est_ua_all(lun, initidx, ua_type);
12652 #else
12653 ctl_est_ua_all(lun, -1, ua_type);
12654 #endif
12655 mtx_unlock(&lun->lun_lock);
12656 }
12657
12658 static int
ctl_lun_reset(union ctl_io * io)12659 ctl_lun_reset(union ctl_io *io)
12660 {
12661 struct ctl_softc *softc = CTL_SOFTC(io);
12662 struct ctl_lun *lun;
12663 uint32_t targ_lun, initidx;
12664
12665 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12666 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12667 mtx_lock(&softc->ctl_lock);
12668 if (targ_lun >= ctl_max_luns ||
12669 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12670 mtx_unlock(&softc->ctl_lock);
12671 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12672 return (1);
12673 }
12674 ctl_do_lun_reset(lun, initidx, CTL_UA_LUN_RESET);
12675 mtx_unlock(&softc->ctl_lock);
12676 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12677
12678 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
12679 union ctl_ha_msg msg_info;
12680
12681 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12682 msg_info.hdr.nexus = io->io_hdr.nexus;
12683 msg_info.task.task_action = CTL_TASK_LUN_RESET;
12684 msg_info.hdr.original_sc = NULL;
12685 msg_info.hdr.serializing_sc = NULL;
12686 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12687 sizeof(msg_info.task), M_WAITOK);
12688 }
12689 return (0);
12690 }
12691
12692 static void
ctl_abort_tasks_lun(struct ctl_lun * lun,uint32_t targ_port,uint32_t init_id,int other_sc)12693 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
12694 int other_sc)
12695 {
12696 struct ctl_io_hdr *xioh;
12697
12698 mtx_assert(&lun->lun_lock, MA_OWNED);
12699
12700 /*
12701 * Run through the OOA queue and attempt to find the given I/O.
12702 * The target port, initiator ID, tag type and tag number have to
12703 * match the values that we got from the initiator. If we have an
12704 * untagged command to abort, simply abort the first untagged command
12705 * we come to. We only allow one untagged command at a time of course.
12706 */
12707 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) {
12708 union ctl_io *xio = (union ctl_io *)xioh;
12709
12710 if ((targ_port == UINT32_MAX ||
12711 targ_port == xioh->nexus.targ_port) &&
12712 (init_id == UINT32_MAX ||
12713 init_id == xioh->nexus.initid)) {
12714 if (targ_port != xioh->nexus.targ_port ||
12715 init_id != xioh->nexus.initid)
12716 xioh->flags |= CTL_FLAG_ABORT_STATUS;
12717 xioh->flags |= CTL_FLAG_ABORT;
12718 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12719 union ctl_ha_msg msg_info;
12720
12721 CTL_IO_ASSERT(xio, SCSI);
12722 msg_info.hdr.nexus = xioh->nexus;
12723 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12724 msg_info.task.tag_num = xio->scsiio.tag_num;
12725 msg_info.task.tag_type = xio->scsiio.tag_type;
12726 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12727 msg_info.hdr.original_sc = NULL;
12728 msg_info.hdr.serializing_sc = NULL;
12729 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12730 sizeof(msg_info.task), M_NOWAIT);
12731 }
12732 ctl_try_unblock_io(lun, xio, FALSE);
12733 }
12734 }
12735 }
12736
12737 static int
ctl_abort_task_set(union ctl_io * io)12738 ctl_abort_task_set(union ctl_io *io)
12739 {
12740 struct ctl_softc *softc = CTL_SOFTC(io);
12741 struct ctl_lun *lun;
12742 uint32_t targ_lun;
12743
12744 /*
12745 * Look up the LUN.
12746 */
12747 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12748 mtx_lock(&softc->ctl_lock);
12749 if (targ_lun >= ctl_max_luns ||
12750 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12751 mtx_unlock(&softc->ctl_lock);
12752 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12753 return (1);
12754 }
12755
12756 mtx_lock(&lun->lun_lock);
12757 mtx_unlock(&softc->ctl_lock);
12758 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
12759 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12760 io->io_hdr.nexus.initid,
12761 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12762 } else { /* CTL_TASK_CLEAR_TASK_SET */
12763 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
12764 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12765 }
12766 mtx_unlock(&lun->lun_lock);
12767 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12768 return (0);
12769 }
12770
12771 static void
ctl_i_t_nexus_loss(struct ctl_softc * softc,uint32_t initidx,ctl_ua_type ua_type)12772 ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx,
12773 ctl_ua_type ua_type)
12774 {
12775 struct ctl_lun *lun;
12776 struct scsi_sense_data *ps;
12777 uint32_t p, i;
12778
12779 p = initidx / CTL_MAX_INIT_PER_PORT;
12780 i = initidx % CTL_MAX_INIT_PER_PORT;
12781 mtx_lock(&softc->ctl_lock);
12782 STAILQ_FOREACH(lun, &softc->lun_list, links) {
12783 mtx_lock(&lun->lun_lock);
12784 /* Abort tasks. */
12785 ctl_abort_tasks_lun(lun, p, i, 1);
12786 /* Clear CA. */
12787 ps = lun->pending_sense[p];
12788 if (ps != NULL)
12789 ps[i].error_code = 0;
12790 /* Clear reservation. */
12791 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
12792 lun->flags &= ~CTL_LUN_RESERVED;
12793 /* Clear prevent media removal. */
12794 if (lun->prevent && ctl_is_set(lun->prevent, initidx)) {
12795 ctl_clear_mask(lun->prevent, initidx);
12796 lun->prevent_count--;
12797 }
12798 /* Clear TPC status */
12799 ctl_tpc_lun_clear(lun, initidx);
12800 /* Establish UA. */
12801 ctl_est_ua(lun, initidx, ua_type);
12802 mtx_unlock(&lun->lun_lock);
12803 }
12804 mtx_unlock(&softc->ctl_lock);
12805 }
12806
12807 static int
ctl_i_t_nexus_reset(union ctl_io * io)12808 ctl_i_t_nexus_reset(union ctl_io *io)
12809 {
12810 struct ctl_softc *softc = CTL_SOFTC(io);
12811 uint32_t initidx;
12812
12813 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12814 union ctl_ha_msg msg_info;
12815
12816 msg_info.hdr.nexus = io->io_hdr.nexus;
12817 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
12818 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12819 msg_info.hdr.original_sc = NULL;
12820 msg_info.hdr.serializing_sc = NULL;
12821 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12822 sizeof(msg_info.task), M_WAITOK);
12823 }
12824
12825 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12826 ctl_i_t_nexus_loss(softc, initidx, CTL_UA_I_T_NEXUS_LOSS);
12827 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12828 return (0);
12829 }
12830
12831 static int
ctl_abort_task(union ctl_io * io)12832 ctl_abort_task(union ctl_io *io)
12833 {
12834 struct ctl_softc *softc = CTL_SOFTC(io);
12835 struct ctl_io_hdr *xioh;
12836 struct ctl_lun *lun;
12837 uint32_t targ_lun;
12838
12839 /*
12840 * Look up the LUN.
12841 */
12842 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12843 mtx_lock(&softc->ctl_lock);
12844 if (targ_lun >= ctl_max_luns ||
12845 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12846 mtx_unlock(&softc->ctl_lock);
12847 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12848 return (1);
12849 }
12850
12851 mtx_lock(&lun->lun_lock);
12852 mtx_unlock(&softc->ctl_lock);
12853 /*
12854 * Run through the OOA queue and attempt to find the given I/O.
12855 * The target port, initiator ID, tag type and tag number have to
12856 * match the values that we got from the initiator. If we have an
12857 * untagged command to abort, simply abort the first untagged command
12858 * we come to. We only allow one untagged command at a time of course.
12859 */
12860 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) {
12861 union ctl_io *xio = (union ctl_io *)xioh;
12862
12863 CTL_IO_ASSERT(xio, SCSI);
12864 if ((xioh->nexus.targ_port != io->io_hdr.nexus.targ_port)
12865 || (xioh->nexus.initid != io->io_hdr.nexus.initid)
12866 || (xioh->flags & CTL_FLAG_ABORT))
12867 continue;
12868
12869 /*
12870 * If the abort says that the task is untagged, the
12871 * task in the queue must be untagged. Otherwise,
12872 * we just check to see whether the tag numbers
12873 * match. This is because the QLogic firmware
12874 * doesn't pass back the tag type in an abort
12875 * request.
12876 */
12877 #if 0
12878 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12879 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12880 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
12881 #else
12882 /*
12883 * XXX KDM we've got problems with FC, because it
12884 * doesn't send down a tag type with aborts. So we
12885 * can only really go by the tag number...
12886 * This may cause problems with parallel SCSI.
12887 * Need to figure that out!!
12888 */
12889 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12890 #endif
12891 xioh->flags |= CTL_FLAG_ABORT;
12892 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
12893 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12894 union ctl_ha_msg msg_info;
12895
12896 msg_info.hdr.nexus = io->io_hdr.nexus;
12897 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12898 msg_info.task.tag_num = io->taskio.tag_num;
12899 msg_info.task.tag_type = io->taskio.tag_type;
12900 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12901 msg_info.hdr.original_sc = NULL;
12902 msg_info.hdr.serializing_sc = NULL;
12903 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12904 sizeof(msg_info.task), M_NOWAIT);
12905 }
12906 ctl_try_unblock_io(lun, xio, FALSE);
12907 }
12908 }
12909 mtx_unlock(&lun->lun_lock);
12910 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12911 return (0);
12912 }
12913
12914 static int
12915 ctl_query_task(union ctl_io *io, int task_set)
12916 {
12917 struct ctl_softc *softc = CTL_SOFTC(io);
12918 struct ctl_io_hdr *xioh;
12919 struct ctl_lun *lun;
12920 int found = 0;
12921 uint32_t targ_lun;
12922
12923 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12924 mtx_lock(&softc->ctl_lock);
12925 if (targ_lun >= ctl_max_luns ||
12926 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12927 mtx_unlock(&softc->ctl_lock);
12928 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12929 return (1);
12930 }
12931 mtx_lock(&lun->lun_lock);
12932 mtx_unlock(&softc->ctl_lock);
12933 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) {
12934 union ctl_io *xio = (union ctl_io *)xioh;
12935
12936 CTL_IO_ASSERT(xio, SCSI);
12937 if ((xioh->nexus.targ_port != io->io_hdr.nexus.targ_port)
12938 || (xioh->nexus.initid != io->io_hdr.nexus.initid)
12939 || (xioh->flags & CTL_FLAG_ABORT))
12940 continue;
12941
12942 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
12943 found = 1;
12944 break;
12945 }
12946 }
12947 mtx_unlock(&lun->lun_lock);
12948 if (found)
12949 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12950 else
12951 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12952 return (0);
12953 }
12954
12955 static int
12956 ctl_query_async_event(union ctl_io *io)
12957 {
12958 struct ctl_softc *softc = CTL_SOFTC(io);
12959 struct ctl_lun *lun;
12960 ctl_ua_type ua;
12961 uint32_t targ_lun, initidx;
12962
12963 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12964 mtx_lock(&softc->ctl_lock);
12965 if (targ_lun >= ctl_max_luns ||
12966 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12967 mtx_unlock(&softc->ctl_lock);
12968 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12969 return (1);
12970 }
12971 mtx_lock(&lun->lun_lock);
12972 mtx_unlock(&softc->ctl_lock);
12973 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12974 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
12975 mtx_unlock(&lun->lun_lock);
12976 if (ua != CTL_UA_NONE)
12977 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12978 else
12979 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12980 return (0);
12981 }
12982
12983 static void
12984 ctl_run_task(union ctl_io *io)
12985 {
12986 int retval = 1;
12987
12988 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12989 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12990 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
12991 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
12992 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
12993 switch (io->taskio.task_action) {
12994 case CTL_TASK_ABORT_TASK:
12995 retval = ctl_abort_task(io);
12996 break;
12997 case CTL_TASK_ABORT_TASK_SET:
12998 case CTL_TASK_CLEAR_TASK_SET:
12999 retval = ctl_abort_task_set(io);
13000 break;
13001 case CTL_TASK_CLEAR_ACA:
13002 break;
13003 case CTL_TASK_I_T_NEXUS_RESET:
13004 retval = ctl_i_t_nexus_reset(io);
13005 break;
13006 case CTL_TASK_LUN_RESET:
13007 retval = ctl_lun_reset(io);
13008 break;
13009 case CTL_TASK_TARGET_RESET:
13010 case CTL_TASK_BUS_RESET:
13011 retval = ctl_target_reset(io);
13012 break;
13013 case CTL_TASK_PORT_LOGIN:
13014 break;
13015 case CTL_TASK_PORT_LOGOUT:
13016 break;
13017 case CTL_TASK_QUERY_TASK:
13018 retval = ctl_query_task(io, 0);
13019 break;
13020 case CTL_TASK_QUERY_TASK_SET:
13021 retval = ctl_query_task(io, 1);
13022 break;
13023 case CTL_TASK_QUERY_ASYNC_EVENT:
13024 retval = ctl_query_async_event(io);
13025 break;
13026 default:
13027 printf("%s: got unknown task management event %d\n",
13028 __func__, io->taskio.task_action);
13029 break;
13030 }
13031 if (retval == 0)
13032 io->io_hdr.status = CTL_SUCCESS;
13033 else
13034 io->io_hdr.status = CTL_ERROR;
13035 ctl_done(io);
13036 }
13037
13038 /*
13039 * For HA operation. Handle commands that come in from the other
13040 * controller.
13041 */
13042 static void
13043 ctl_handle_isc(union ctl_io *io)
13044 {
13045 struct ctl_softc *softc = CTL_SOFTC(io);
13046 struct ctl_lun *lun;
13047 const struct ctl_cmd_entry *entry;
13048 uint32_t targ_lun;
13049
13050 CTL_IO_ASSERT(io, SCSI);
13051
13052 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
13053 switch (io->io_hdr.msg_type) {
13054 case CTL_MSG_SERIALIZE:
13055 ctl_serialize_other_sc_cmd(&io->scsiio);
13056 break;
13057 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */
13058 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
13059 if (targ_lun >= ctl_max_luns ||
13060 (lun = softc->ctl_luns[targ_lun]) == NULL) {
13061 ctl_done(io);
13062 break;
13063 }
13064 mtx_lock(&lun->lun_lock);
13065 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) {
13066 mtx_unlock(&lun->lun_lock);
13067 ctl_done(io);
13068 break;
13069 }
13070 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
13071 mtx_unlock(&lun->lun_lock);
13072 ctl_enqueue_rtr(io);
13073 break;
13074 case CTL_MSG_FINISH_IO:
13075 if (softc->ha_mode == CTL_HA_MODE_XFER) {
13076 ctl_done(io);
13077 break;
13078 }
13079 if (targ_lun >= ctl_max_luns ||
13080 (lun = softc->ctl_luns[targ_lun]) == NULL) {
13081 ctl_free_io(io);
13082 break;
13083 }
13084 mtx_lock(&lun->lun_lock);
13085 ctl_try_unblock_others(lun, io, TRUE);
13086 LIST_REMOVE(&io->io_hdr, ooa_links);
13087 mtx_unlock(&lun->lun_lock);
13088 ctl_free_io(io);
13089 break;
13090 case CTL_MSG_PERS_ACTION:
13091 ctl_hndl_per_res_out_on_other_sc(io);
13092 ctl_free_io(io);
13093 break;
13094 case CTL_MSG_BAD_JUJU:
13095 ctl_done(io);
13096 break;
13097 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */
13098 ctl_datamove_remote(io);
13099 break;
13100 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */
13101 ctl_datamove_done(io, false);
13102 break;
13103 case CTL_MSG_FAILOVER:
13104 ctl_failover_lun(io);
13105 ctl_free_io(io);
13106 break;
13107 default:
13108 printf("%s: Invalid message type %d\n",
13109 __func__, io->io_hdr.msg_type);
13110 ctl_free_io(io);
13111 break;
13112 }
13113
13114 }
13115
13116 /*
13117 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
13118 * there is no match.
13119 */
13120 static ctl_lun_error_pattern
13121 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
13122 {
13123 const struct ctl_cmd_entry *entry;
13124 ctl_lun_error_pattern filtered_pattern, pattern;
13125
13126 pattern = desc->error_pattern;
13127
13128 /*
13129 * XXX KDM we need more data passed into this function to match a
13130 * custom pattern, and we actually need to implement custom pattern
13131 * matching.
13132 */
13133 if (pattern & CTL_LUN_PAT_CMD)
13134 return (CTL_LUN_PAT_CMD);
13135
13136 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
13137 return (CTL_LUN_PAT_ANY);
13138
13139 entry = ctl_get_cmd_entry(ctsio, NULL);
13140
13141 filtered_pattern = entry->pattern & pattern;
13142
13143 /*
13144 * If the user requested specific flags in the pattern (e.g.
13145 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
13146 * flags.
13147 *
13148 * If the user did not specify any flags, it doesn't matter whether
13149 * or not the command supports the flags.
13150 */
13151 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
13152 (pattern & ~CTL_LUN_PAT_MASK))
13153 return (CTL_LUN_PAT_NONE);
13154
13155 /*
13156 * If the user asked for a range check, see if the requested LBA
13157 * range overlaps with this command's LBA range.
13158 */
13159 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
13160 uint64_t lba1;
13161 uint64_t len1;
13162 ctl_action action;
13163 int retval;
13164
13165 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
13166 if (retval != 0)
13167 return (CTL_LUN_PAT_NONE);
13168
13169 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
13170 desc->lba_range.len, FALSE);
13171 /*
13172 * A "pass" means that the LBA ranges don't overlap, so
13173 * this doesn't match the user's range criteria.
13174 */
13175 if (action == CTL_ACTION_PASS)
13176 return (CTL_LUN_PAT_NONE);
13177 }
13178
13179 return (filtered_pattern);
13180 }
13181
13182 static void
13183 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
13184 {
13185 struct ctl_error_desc *desc, *desc2;
13186
13187 CTL_IO_ASSERT(io, SCSI);
13188
13189 mtx_assert(&lun->lun_lock, MA_OWNED);
13190
13191 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
13192 ctl_lun_error_pattern pattern;
13193 /*
13194 * Check to see whether this particular command matches
13195 * the pattern in the descriptor.
13196 */
13197 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
13198 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
13199 continue;
13200
13201 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
13202 case CTL_LUN_INJ_ABORTED:
13203 ctl_set_aborted(&io->scsiio);
13204 break;
13205 case CTL_LUN_INJ_MEDIUM_ERR:
13206 ctl_set_medium_error(&io->scsiio,
13207 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
13208 CTL_FLAG_DATA_OUT);
13209 break;
13210 case CTL_LUN_INJ_UA:
13211 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
13212 * OCCURRED */
13213 ctl_set_ua(&io->scsiio, 0x29, 0x00);
13214 break;
13215 case CTL_LUN_INJ_CUSTOM:
13216 /*
13217 * We're assuming the user knows what he is doing.
13218 * Just copy the sense information without doing
13219 * checks.
13220 */
13221 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
13222 MIN(sizeof(desc->custom_sense),
13223 sizeof(io->scsiio.sense_data)));
13224 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
13225 io->scsiio.sense_len = SSD_FULL_SIZE;
13226 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
13227 break;
13228 case CTL_LUN_INJ_NONE:
13229 default:
13230 /*
13231 * If this is an error injection type we don't know
13232 * about, clear the continuous flag (if it is set)
13233 * so it will get deleted below.
13234 */
13235 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
13236 break;
13237 }
13238 /*
13239 * By default, each error injection action is a one-shot
13240 */
13241 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
13242 continue;
13243
13244 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
13245
13246 free(desc, M_CTL);
13247 }
13248 }
13249
13250 #ifdef CTL_IO_DELAY
13251 static void
13252 ctl_datamove_timer_wakeup(void *arg)
13253 {
13254 union ctl_io *io;
13255
13256 io = (union ctl_io *)arg;
13257
13258 ctl_datamove(io);
13259 }
13260 #endif /* CTL_IO_DELAY */
13261
13262 static void
13263 ctl_datamove_done_process(union ctl_io *io)
13264 {
13265 #ifdef CTL_TIME_IO
13266 struct bintime cur_bt;
13267
13268 getbinuptime(&cur_bt);
13269 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
13270 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
13271 #endif
13272 io->io_hdr.num_dmas++;
13273
13274 if ((io->io_hdr.port_status != 0) &&
13275 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
13276 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
13277 switch (io->io_hdr.io_type) {
13278 case CTL_IO_SCSI:
13279 ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1,
13280 /*retry_count*/ io->io_hdr.port_status);
13281 break;
13282 case CTL_IO_NVME:
13283 case CTL_IO_NVME_ADMIN:
13284 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13285 ctl_nvme_set_command_aborted(&io->nvmeio);
13286 else
13287 ctl_nvme_set_data_transfer_error(&io->nvmeio);
13288 break;
13289 default:
13290 __assert_unreachable();
13291 }
13292 } else if (ctl_kern_data_resid(io) != 0 &&
13293 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT &&
13294 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
13295 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
13296 switch (io->io_hdr.io_type) {
13297 case CTL_IO_SCSI:
13298 ctl_set_invalid_field_ciu(&io->scsiio);
13299 break;
13300 case CTL_IO_NVME:
13301 case CTL_IO_NVME_ADMIN:
13302 ctl_nvme_set_data_transfer_error(&io->nvmeio);
13303 break;
13304 default:
13305 __assert_unreachable();
13306 }
13307 } else if (ctl_debug & CTL_DEBUG_CDB_DATA)
13308 ctl_data_print(io);
13309 }
13310
13311 void
13312 ctl_datamove_done(union ctl_io *io, bool samethr)
13313 {
13314
13315 ctl_datamove_done_process(io);
13316 ctl_be_move_done(io, samethr);
13317 }
13318
13319 void
13320 ctl_datamove(union ctl_io *io)
13321 {
13322 void (*fe_datamove)(union ctl_io *io);
13323
13324 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
13325
13326 CTL_DEBUG_PRINT(("ctl_datamove\n"));
13327
13328 /* No data transferred yet. Frontend must update this when done. */
13329 ctl_set_kern_data_resid(io, ctl_kern_data_len(io));
13330
13331 #ifdef CTL_TIME_IO
13332 getbinuptime(&io->io_hdr.dma_start_bt);
13333 #endif /* CTL_TIME_IO */
13334
13335 #ifdef CTL_IO_DELAY
13336 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13337 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13338 } else {
13339 struct ctl_lun *lun;
13340
13341 lun = CTL_LUN(io);
13342 if ((lun != NULL)
13343 && (lun->delay_info.datamove_delay > 0)) {
13344 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13345 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13346 callout_reset(&io->io_hdr.delay_callout,
13347 lun->delay_info.datamove_delay * hz,
13348 ctl_datamove_timer_wakeup, io);
13349 if (lun->delay_info.datamove_type ==
13350 CTL_DELAY_TYPE_ONESHOT)
13351 lun->delay_info.datamove_delay = 0;
13352 return;
13353 }
13354 }
13355 #endif
13356
13357 /*
13358 * This command has been aborted. Set the port status, so we fail
13359 * the data move.
13360 */
13361 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13362 switch (io->io_hdr.io_type) {
13363 case CTL_IO_SCSI:
13364 printf("ctl_datamove: tag 0x%jx on (%u:%u:%u) aborted\n",
13365 io->scsiio.tag_num, io->io_hdr.nexus.initid,
13366 io->io_hdr.nexus.targ_port,
13367 io->io_hdr.nexus.targ_lun);
13368 break;
13369 case CTL_IO_NVME:
13370 case CTL_IO_NVME_ADMIN:
13371 printf("ctl_datamove: CID 0x%x on (%u:%u:%u) aborted\n",
13372 le16toh(io->nvmeio.cmd.cid),
13373 io->io_hdr.nexus.initid, io->io_hdr.nexus.targ_port,
13374 io->io_hdr.nexus.targ_lun);
13375 break;
13376 default:
13377 __assert_unreachable();
13378 }
13379 io->io_hdr.port_status = 31337;
13380 ctl_datamove_done_process(io);
13381 ctl_be_move_done(io, true);
13382 return;
13383 }
13384
13385 /* Don't confuse frontend with zero length data move. */
13386 if (ctl_kern_data_len(io) == 0) {
13387 ctl_datamove_done_process(io);
13388 ctl_be_move_done(io, true);
13389 return;
13390 }
13391
13392 fe_datamove = CTL_PORT(io)->fe_datamove;
13393 fe_datamove(io);
13394 }
13395
13396 static void
13397 ctl_send_datamove_done(union ctl_io *io, int have_lock)
13398 {
13399 union ctl_ha_msg msg;
13400 #ifdef CTL_TIME_IO
13401 struct bintime cur_bt;
13402 #endif
13403
13404 CTL_IO_ASSERT(io, SCSI);
13405
13406 memset(&msg, 0, sizeof(msg));
13407 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
13408 msg.hdr.original_sc = io;
13409 msg.hdr.serializing_sc = io->io_hdr.remote_io;
13410 msg.hdr.nexus = io->io_hdr.nexus;
13411 msg.hdr.status = io->io_hdr.status;
13412 msg.scsi.kern_data_resid = io->scsiio.kern_data_resid;
13413 msg.scsi.tag_num = io->scsiio.tag_num;
13414 msg.scsi.tag_type = io->scsiio.tag_type;
13415 msg.scsi.scsi_status = io->scsiio.scsi_status;
13416 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
13417 io->scsiio.sense_len);
13418 msg.scsi.sense_len = io->scsiio.sense_len;
13419 msg.scsi.port_status = io->io_hdr.port_status;
13420 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
13421 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13422 ctl_failover_io(io, /*have_lock*/ have_lock);
13423 return;
13424 }
13425 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13426 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
13427 msg.scsi.sense_len, M_WAITOK);
13428
13429 #ifdef CTL_TIME_IO
13430 getbinuptime(&cur_bt);
13431 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
13432 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
13433 #endif
13434 io->io_hdr.num_dmas++;
13435 }
13436
13437 /*
13438 * The DMA to the remote side is done, now we need to tell the other side
13439 * we're done so it can continue with its data movement.
13440 */
13441 static void
13442 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
13443 {
13444 union ctl_io *io;
13445 uint32_t i;
13446
13447 io = rq->context;
13448 CTL_IO_ASSERT(io, SCSI);
13449
13450 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
13451 printf("%s: ISC DMA write failed with error %d", __func__,
13452 rq->ret);
13453 ctl_set_internal_failure(&io->scsiio,
13454 /*sks_valid*/ 1,
13455 /*retry_count*/ rq->ret);
13456 }
13457
13458 ctl_dt_req_free(rq);
13459
13460 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13461 free(CTL_LSGLT(io)[i].addr, M_CTL);
13462 free(CTL_RSGL(io), M_CTL);
13463 CTL_RSGL(io) = NULL;
13464 CTL_LSGL(io) = NULL;
13465
13466 /*
13467 * The data is in local and remote memory, so now we need to send
13468 * status (good or back) back to the other side.
13469 */
13470 ctl_send_datamove_done(io, /*have_lock*/ 0);
13471 }
13472
13473 /*
13474 * We've moved the data from the host/controller into local memory. Now we
13475 * need to push it over to the remote controller's memory.
13476 */
13477 static int
13478 ctl_datamove_remote_dm_write_cb(union ctl_io *io, bool samethr)
13479 {
13480 int retval;
13481
13482 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
13483 ctl_datamove_remote_write_cb);
13484 return (retval);
13485 }
13486
13487 static void
13488 ctl_datamove_remote_write(union ctl_io *io)
13489 {
13490 int retval;
13491 void (*fe_datamove)(union ctl_io *io);
13492
13493 CTL_IO_ASSERT(io, SCSI);
13494
13495 /*
13496 * - Get the data from the host/HBA into local memory.
13497 * - DMA memory from the local controller to the remote controller.
13498 * - Send status back to the remote controller.
13499 */
13500
13501 retval = ctl_datamove_remote_sgl_setup(io);
13502 if (retval != 0)
13503 return;
13504
13505 /* Switch the pointer over so the FETD knows what to do */
13506 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io);
13507
13508 /*
13509 * Use a custom move done callback, since we need to send completion
13510 * back to the other controller, not to the backend on this side.
13511 */
13512 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
13513
13514 fe_datamove = CTL_PORT(io)->fe_datamove;
13515 fe_datamove(io);
13516 }
13517
13518 static int
13519 ctl_datamove_remote_dm_read_cb(union ctl_io *io, bool samethr)
13520 {
13521 uint32_t i;
13522
13523 CTL_IO_ASSERT(io, SCSI);
13524
13525 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13526 free(CTL_LSGLT(io)[i].addr, M_CTL);
13527 free(CTL_RSGL(io), M_CTL);
13528 CTL_RSGL(io) = NULL;
13529 CTL_LSGL(io) = NULL;
13530
13531 /*
13532 * The read is done, now we need to send status (good or bad) back
13533 * to the other side.
13534 */
13535 ctl_send_datamove_done(io, /*have_lock*/ 0);
13536
13537 return (0);
13538 }
13539
13540 static void
13541 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
13542 {
13543 union ctl_io *io;
13544 void (*fe_datamove)(union ctl_io *io);
13545
13546 io = rq->context;
13547 CTL_IO_ASSERT(io, SCSI);
13548
13549 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
13550 printf("%s: ISC DMA read failed with error %d\n", __func__,
13551 rq->ret);
13552 ctl_set_internal_failure(&io->scsiio,
13553 /*sks_valid*/ 1,
13554 /*retry_count*/ rq->ret);
13555 }
13556
13557 ctl_dt_req_free(rq);
13558
13559 /* Switch the pointer over so the FETD knows what to do */
13560 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io);
13561
13562 /*
13563 * Use a custom move done callback, since we need to send completion
13564 * back to the other controller, not to the backend on this side.
13565 */
13566 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
13567
13568 /* XXX KDM add checks like the ones in ctl_datamove? */
13569
13570 fe_datamove = CTL_PORT(io)->fe_datamove;
13571 fe_datamove(io);
13572 }
13573
13574 static int
13575 ctl_datamove_remote_sgl_setup(union ctl_io *io)
13576 {
13577 struct ctl_sg_entry *local_sglist;
13578 uint32_t len_to_go;
13579 int retval;
13580 int i;
13581
13582 CTL_IO_ASSERT(io, SCSI);
13583
13584 retval = 0;
13585 local_sglist = CTL_LSGL(io);
13586 len_to_go = io->scsiio.kern_data_len;
13587
13588 /*
13589 * The difficult thing here is that the size of the various
13590 * S/G segments may be different than the size from the
13591 * remote controller. That'll make it harder when DMAing
13592 * the data back to the other side.
13593 */
13594 for (i = 0; len_to_go > 0; i++) {
13595 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
13596 local_sglist[i].addr =
13597 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
13598
13599 len_to_go -= local_sglist[i].len;
13600 }
13601 /*
13602 * Reset the number of S/G entries accordingly. The original
13603 * number of S/G entries is available in rem_sg_entries.
13604 */
13605 io->scsiio.kern_sg_entries = i;
13606
13607 return (retval);
13608 }
13609
13610 static int
13611 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
13612 ctl_ha_dt_cb callback)
13613 {
13614 struct ctl_ha_dt_req *rq;
13615 struct ctl_sg_entry *remote_sglist, *local_sglist;
13616 uint32_t local_used, remote_used, total_used;
13617 int i, j, isc_ret;
13618
13619 rq = ctl_dt_req_alloc();
13620
13621 CTL_IO_ASSERT(io, SCSI);
13622
13623 /*
13624 * If we failed to allocate the request, and if the DMA didn't fail
13625 * anyway, set busy status. This is just a resource allocation
13626 * failure.
13627 */
13628 if ((rq == NULL)
13629 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
13630 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
13631 ctl_set_busy(&io->scsiio);
13632
13633 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
13634 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
13635 if (rq != NULL)
13636 ctl_dt_req_free(rq);
13637
13638 /*
13639 * The data move failed. We need to return status back
13640 * to the other controller. No point in trying to DMA
13641 * data to the remote controller.
13642 */
13643
13644 ctl_send_datamove_done(io, /*have_lock*/ 0);
13645
13646 return (1);
13647 }
13648
13649 local_sglist = CTL_LSGL(io);
13650 remote_sglist = CTL_RSGL(io);
13651 local_used = 0;
13652 remote_used = 0;
13653 total_used = 0;
13654
13655 /*
13656 * Pull/push the data over the wire from/to the other controller.
13657 * This takes into account the possibility that the local and
13658 * remote sglists may not be identical in terms of the size of
13659 * the elements and the number of elements.
13660 *
13661 * One fundamental assumption here is that the length allocated for
13662 * both the local and remote sglists is identical. Otherwise, we've
13663 * essentially got a coding error of some sort.
13664 */
13665 isc_ret = CTL_HA_STATUS_SUCCESS;
13666 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
13667 uint32_t cur_len;
13668 uint8_t *tmp_ptr;
13669
13670 rq->command = command;
13671 rq->context = io;
13672
13673 /*
13674 * Both pointers should be aligned. But it is possible
13675 * that the allocation length is not. They should both
13676 * also have enough slack left over at the end, though,
13677 * to round up to the next 8 byte boundary.
13678 */
13679 cur_len = MIN(local_sglist[i].len - local_used,
13680 remote_sglist[j].len - remote_used);
13681 rq->size = cur_len;
13682
13683 tmp_ptr = (uint8_t *)local_sglist[i].addr;
13684 tmp_ptr += local_used;
13685
13686 #if 0
13687 /* Use physical addresses when talking to ISC hardware */
13688 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
13689 /* XXX KDM use busdma */
13690 rq->local = vtophys(tmp_ptr);
13691 } else
13692 rq->local = tmp_ptr;
13693 #else
13694 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
13695 ("HA does not support BUS_ADDR"));
13696 rq->local = tmp_ptr;
13697 #endif
13698
13699 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
13700 tmp_ptr += remote_used;
13701 rq->remote = tmp_ptr;
13702
13703 rq->callback = NULL;
13704
13705 local_used += cur_len;
13706 if (local_used >= local_sglist[i].len) {
13707 i++;
13708 local_used = 0;
13709 }
13710
13711 remote_used += cur_len;
13712 if (remote_used >= remote_sglist[j].len) {
13713 j++;
13714 remote_used = 0;
13715 }
13716 total_used += cur_len;
13717
13718 if (total_used >= io->scsiio.kern_data_len)
13719 rq->callback = callback;
13720
13721 isc_ret = ctl_dt_single(rq);
13722 if (isc_ret > CTL_HA_STATUS_SUCCESS)
13723 break;
13724 }
13725 if (isc_ret != CTL_HA_STATUS_WAIT) {
13726 rq->ret = isc_ret;
13727 callback(rq);
13728 }
13729
13730 return (0);
13731 }
13732
13733 static void
13734 ctl_datamove_remote_read(union ctl_io *io)
13735 {
13736 int retval;
13737 uint32_t i;
13738
13739 /*
13740 * This will send an error to the other controller in the case of a
13741 * failure.
13742 */
13743 retval = ctl_datamove_remote_sgl_setup(io);
13744 if (retval != 0)
13745 return;
13746
13747 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
13748 ctl_datamove_remote_read_cb);
13749 if (retval != 0) {
13750 /*
13751 * Make sure we free memory if there was an error.. The
13752 * ctl_datamove_remote_xfer() function will send the
13753 * datamove done message, or call the callback with an
13754 * error if there is a problem.
13755 */
13756 for (i = 0; i < ctl_kern_sg_entries(io); i++)
13757 free(CTL_LSGLT(io)[i].addr, M_CTL);
13758 free(CTL_RSGL(io), M_CTL);
13759 CTL_RSGL(io) = NULL;
13760 CTL_LSGL(io) = NULL;
13761 }
13762 }
13763
13764 /*
13765 * Process a datamove request from the other controller. This is used for
13766 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
13767 * first. Once that is complete, the data gets DMAed into the remote
13768 * controller's memory. For reads, we DMA from the remote controller's
13769 * memory into our memory first, and then move it out to the FETD.
13770 */
13771 static void
13772 ctl_datamove_remote(union ctl_io *io)
13773 {
13774 CTL_IO_ASSERT(io, SCSI);
13775
13776 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
13777
13778 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13779 ctl_failover_io(io, /*have_lock*/ 0);
13780 return;
13781 }
13782
13783 /*
13784 * Note that we look for an aborted I/O here, but don't do some of
13785 * the other checks that ctl_datamove() normally does.
13786 * We don't need to run the datamove delay code, since that should
13787 * have been done if need be on the other controller.
13788 */
13789 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13790 printf("%s: tag 0x%jx on (%u:%u:%u) aborted\n", __func__,
13791 io->scsiio.tag_num, io->io_hdr.nexus.initid,
13792 io->io_hdr.nexus.targ_port,
13793 io->io_hdr.nexus.targ_lun);
13794 io->io_hdr.port_status = 31338;
13795 ctl_send_datamove_done(io, /*have_lock*/ 0);
13796 return;
13797 }
13798
13799 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
13800 ctl_datamove_remote_write(io);
13801 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
13802 ctl_datamove_remote_read(io);
13803 else {
13804 io->io_hdr.port_status = 31339;
13805 ctl_send_datamove_done(io, /*have_lock*/ 0);
13806 }
13807 }
13808
13809 static void
13810 ctl_process_done(union ctl_io *io)
13811 {
13812 struct ctl_softc *softc = CTL_SOFTC(io);
13813 struct ctl_port *port = CTL_PORT(io);
13814 struct ctl_lun *lun = CTL_LUN(io);
13815 void (*fe_done)(union ctl_io *io);
13816 union ctl_ha_msg msg;
13817
13818 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13819 fe_done = port->fe_done;
13820
13821 #ifdef CTL_TIME_IO
13822 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13823 char str[256];
13824 char path_str[64];
13825 struct sbuf sb;
13826
13827 ctl_scsi_path_string(&io->io_hdr, path_str, sizeof(path_str));
13828 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13829
13830 ctl_io_sbuf(io, &sb);
13831 sbuf_cat(&sb, path_str);
13832 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13833 (intmax_t)time_uptime - io->io_hdr.start_time);
13834 sbuf_finish(&sb);
13835 printf("%s", sbuf_data(&sb));
13836 }
13837 #endif /* CTL_TIME_IO */
13838
13839 switch (io->io_hdr.io_type) {
13840 case CTL_IO_SCSI:
13841 case CTL_IO_NVME:
13842 case CTL_IO_NVME_ADMIN:
13843 break;
13844 case CTL_IO_TASK:
13845 if (ctl_debug & CTL_DEBUG_INFO)
13846 ctl_io_error_print(io, NULL);
13847 fe_done(io);
13848 return;
13849 default:
13850 panic("%s: Invalid CTL I/O type %d\n",
13851 __func__, io->io_hdr.io_type);
13852 }
13853
13854 if (lun == NULL) {
13855 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13856 io->io_hdr.nexus.targ_mapped_lun));
13857 goto bailout;
13858 }
13859
13860 mtx_lock(&lun->lun_lock);
13861
13862 /*
13863 * Check to see if we have any informational exception and status
13864 * of this command can be modified to report it in form of either
13865 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field.
13866 */
13867 if (lun->ie_reported == 0 && lun->ie_asc != 0 &&
13868 io->io_hdr.status == CTL_SUCCESS &&
13869 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) {
13870 uint8_t mrie = lun->MODE_IE.mrie;
13871 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) ||
13872 (lun->MODE_VER.byte3 & SMS_VER_PER));
13873
13874 CTL_IO_ASSERT(io, SCSI);
13875 if (((mrie == SIEP_MRIE_REC_COND && per) ||
13876 mrie == SIEP_MRIE_REC_UNCOND ||
13877 mrie == SIEP_MRIE_NO_SENSE) &&
13878 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags &
13879 CTL_CMD_FLAG_NO_SENSE) == 0) {
13880 ctl_set_sense(&io->scsiio,
13881 /*current_error*/ 1,
13882 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ?
13883 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR,
13884 /*asc*/ lun->ie_asc,
13885 /*ascq*/ lun->ie_ascq,
13886 SSD_ELEM_NONE);
13887 lun->ie_reported = 1;
13888 }
13889 } else if (lun->ie_reported < 0)
13890 lun->ie_reported = 0;
13891
13892 /*
13893 * Check to see if we have any errors to inject here. We only
13894 * inject errors for commands that don't already have errors set.
13895 */
13896 if (!STAILQ_EMPTY(&lun->error_list) &&
13897 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
13898 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
13899 ctl_inject_error(lun, io);
13900
13901 /*
13902 * XXX KDM how do we treat commands that aren't completed
13903 * successfully?
13904 *
13905 * XXX KDM should we also track I/O latency?
13906 */
13907 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13908 (io->io_hdr.io_type == CTL_IO_SCSI ||
13909 io->io_hdr.io_type == CTL_IO_NVME ||
13910 io->io_hdr.io_type == CTL_IO_NVME_ADMIN)) {
13911 int type;
13912 #ifdef CTL_TIME_IO
13913 struct bintime bt;
13914
13915 getbinuptime(&bt);
13916 bintime_sub(&bt, &io->io_hdr.start_bt);
13917 #endif
13918 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13919 CTL_FLAG_DATA_IN)
13920 type = CTL_STATS_READ;
13921 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13922 CTL_FLAG_DATA_OUT)
13923 type = CTL_STATS_WRITE;
13924 else
13925 type = CTL_STATS_NO_IO;
13926
13927 lun->stats.bytes[type] += ctl_kern_total_len(io);
13928 lun->stats.operations[type] ++;
13929 lun->stats.dmas[type] += io->io_hdr.num_dmas;
13930 #ifdef CTL_TIME_IO
13931 bintime_add(&lun->stats.dma_time[type], &io->io_hdr.dma_bt);
13932 bintime_add(&lun->stats.time[type], &bt);
13933 #endif
13934
13935 mtx_lock(&port->port_lock);
13936 port->stats.bytes[type] += ctl_kern_total_len(io);
13937 port->stats.operations[type] ++;
13938 port->stats.dmas[type] += io->io_hdr.num_dmas;
13939 #ifdef CTL_TIME_IO
13940 bintime_add(&port->stats.dma_time[type], &io->io_hdr.dma_bt);
13941 bintime_add(&port->stats.time[type], &bt);
13942 #endif
13943 mtx_unlock(&port->port_lock);
13944 }
13945
13946 /*
13947 * Run through the blocked queue of this I/O and see if anything
13948 * can be unblocked, now that this I/O is done and will be removed.
13949 * We need to do it before removal to have OOA position to start.
13950 */
13951 ctl_try_unblock_others(lun, io, TRUE);
13952
13953 /*
13954 * Remove this from the OOA queue.
13955 */
13956 LIST_REMOVE(&io->io_hdr, ooa_links);
13957 #ifdef CTL_TIME_IO
13958 if (LIST_EMPTY(&lun->ooa_queue))
13959 lun->last_busy = getsbinuptime();
13960 #endif
13961
13962 /*
13963 * If the LUN has been invalidated, free it if there is nothing
13964 * left on its OOA queue.
13965 */
13966 if ((lun->flags & CTL_LUN_INVALID)
13967 && LIST_EMPTY(&lun->ooa_queue)) {
13968 mtx_unlock(&lun->lun_lock);
13969 ctl_free_lun(lun);
13970 } else
13971 mtx_unlock(&lun->lun_lock);
13972
13973 bailout:
13974
13975 /*
13976 * If this command has been aborted, make sure we set the status
13977 * properly. The FETD is responsible for freeing the I/O and doing
13978 * whatever it needs to do to clean up its state.
13979 */
13980 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13981 switch (io->io_hdr.io_type) {
13982 case CTL_IO_SCSI:
13983 ctl_set_task_aborted(&io->scsiio);
13984 break;
13985 case CTL_IO_NVME:
13986 case CTL_IO_NVME_ADMIN:
13987 ctl_nvme_set_command_aborted(&io->nvmeio);
13988 break;
13989 default:
13990 __assert_unreachable();
13991 }
13992 }
13993
13994 /*
13995 * If enabled, print command error status.
13996 */
13997 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
13998 (ctl_debug & CTL_DEBUG_INFO) != 0)
13999 ctl_io_error_print(io, NULL);
14000
14001 /*
14002 * Tell the FETD or the other shelf controller we're done with this
14003 * command. Note that only SCSI commands get to this point. Task
14004 * management commands are completed above.
14005 */
14006 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
14007 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
14008 memset(&msg, 0, sizeof(msg));
14009 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
14010 msg.hdr.serializing_sc = io->io_hdr.remote_io;
14011 msg.hdr.nexus = io->io_hdr.nexus;
14012 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
14013 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
14014 M_WAITOK);
14015 }
14016
14017 fe_done(io);
14018 }
14019
14020 /*
14021 * Front end should call this if it doesn't do autosense. When the request
14022 * sense comes back in from the initiator, we'll dequeue this and send it.
14023 */
14024 int
14025 ctl_queue_sense(union ctl_io *io)
14026 {
14027 struct ctl_softc *softc = CTL_SOFTC(io);
14028 struct ctl_port *port = CTL_PORT(io);
14029 struct ctl_lun *lun;
14030 struct scsi_sense_data *ps;
14031 uint32_t initidx, p, targ_lun;
14032
14033 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
14034 CTL_IO_ASSERT(io, SCSI);
14035
14036 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
14037
14038 /*
14039 * LUN lookup will likely move to the ctl_work_thread() once we
14040 * have our new queueing infrastructure (that doesn't put things on
14041 * a per-LUN queue initially). That is so that we can handle
14042 * things like an INQUIRY to a LUN that we don't have enabled. We
14043 * can't deal with that right now.
14044 * If we don't have a LUN for this, just toss the sense information.
14045 */
14046 mtx_lock(&softc->ctl_lock);
14047 if (targ_lun >= ctl_max_luns ||
14048 (lun = softc->ctl_luns[targ_lun]) == NULL) {
14049 mtx_unlock(&softc->ctl_lock);
14050 goto bailout;
14051 }
14052 mtx_lock(&lun->lun_lock);
14053 mtx_unlock(&softc->ctl_lock);
14054
14055 initidx = ctl_get_initindex(&io->io_hdr.nexus);
14056 p = initidx / CTL_MAX_INIT_PER_PORT;
14057 if (lun->pending_sense[p] == NULL) {
14058 lun->pending_sense[p] = malloc(sizeof(*ps) * CTL_MAX_INIT_PER_PORT,
14059 M_CTL, M_NOWAIT | M_ZERO);
14060 }
14061 if ((ps = lun->pending_sense[p]) != NULL) {
14062 ps += initidx % CTL_MAX_INIT_PER_PORT;
14063 memset(ps, 0, sizeof(*ps));
14064 memcpy(ps, &io->scsiio.sense_data, io->scsiio.sense_len);
14065 }
14066 mtx_unlock(&lun->lun_lock);
14067
14068 bailout:
14069 ctl_free_io(io);
14070 return (CTL_RETVAL_COMPLETE);
14071 }
14072
14073 /*
14074 * Primary command inlet from frontend ports. All SCSI and task I/O
14075 * requests must go through this function.
14076 */
14077 int
14078 ctl_queue(union ctl_io *io)
14079 {
14080 struct ctl_port *port = CTL_PORT(io);
14081
14082 switch (io->io_hdr.io_type) {
14083 case CTL_IO_SCSI:
14084 case CTL_IO_TASK:
14085 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
14086 break;
14087 case CTL_IO_NVME:
14088 CTL_DEBUG_PRINT(("ctl_queue nvme nvm cmd=%02X\n",
14089 io->nvmeio.cmd.opc));
14090 break;
14091 case CTL_IO_NVME_ADMIN:
14092 CTL_DEBUG_PRINT(("ctl_queue nvme admin cmd=%02X\n",
14093 io->nvmeio.cmd.opc));
14094 break;
14095 default:
14096 break;
14097 }
14098
14099 #ifdef CTL_TIME_IO
14100 io->io_hdr.start_time = time_uptime;
14101 getbinuptime(&io->io_hdr.start_bt);
14102 #endif /* CTL_TIME_IO */
14103
14104 /* Map FE-specific LUN ID into global one. */
14105 io->io_hdr.nexus.targ_mapped_lun =
14106 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
14107
14108 switch (io->io_hdr.io_type) {
14109 case CTL_IO_SCSI:
14110 case CTL_IO_TASK:
14111 case CTL_IO_NVME:
14112 case CTL_IO_NVME_ADMIN:
14113 if (ctl_debug & CTL_DEBUG_CDB)
14114 ctl_io_print(io);
14115 ctl_enqueue_incoming(io);
14116 break;
14117 default:
14118 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
14119 return (EINVAL);
14120 }
14121
14122 return (CTL_RETVAL_COMPLETE);
14123 }
14124
14125 int
14126 ctl_run(union ctl_io *io)
14127 {
14128 struct ctl_port *port = CTL_PORT(io);
14129
14130 CTL_DEBUG_PRINT(("ctl_run cdb[0]=%02X\n", io->scsiio.cdb[0]));
14131
14132 #ifdef CTL_TIME_IO
14133 io->io_hdr.start_time = time_uptime;
14134 getbinuptime(&io->io_hdr.start_bt);
14135 #endif /* CTL_TIME_IO */
14136
14137 /* Map FE-specific LUN ID into global one. */
14138 io->io_hdr.nexus.targ_mapped_lun =
14139 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
14140
14141 switch (io->io_hdr.io_type) {
14142 case CTL_IO_SCSI:
14143 if (ctl_debug & CTL_DEBUG_CDB)
14144 ctl_io_print(io);
14145 ctl_scsiio_precheck(&io->scsiio);
14146 break;
14147 case CTL_IO_TASK:
14148 if (ctl_debug & CTL_DEBUG_CDB)
14149 ctl_io_print(io);
14150 ctl_run_task(io);
14151 break;
14152 case CTL_IO_NVME:
14153 case CTL_IO_NVME_ADMIN:
14154 if (ctl_debug & CTL_DEBUG_CDB)
14155 ctl_io_print(io);
14156 ctl_nvmeio_precheck(&io->nvmeio);
14157 break;
14158 default:
14159 printf("ctl_run: unknown I/O type %d\n", io->io_hdr.io_type);
14160 return (EINVAL);
14161 }
14162
14163 return (CTL_RETVAL_COMPLETE);
14164 }
14165
14166 #ifdef CTL_IO_DELAY
14167 static void
14168 ctl_done_timer_wakeup(void *arg)
14169 {
14170 union ctl_io *io;
14171
14172 io = (union ctl_io *)arg;
14173 ctl_done(io);
14174 }
14175 #endif /* CTL_IO_DELAY */
14176
14177 void
14178 ctl_serseq_done(union ctl_io *io)
14179 {
14180 struct ctl_lun *lun = CTL_LUN(io);
14181
14182 /* This is racy, but should not be a problem. */
14183 if (!TAILQ_EMPTY(&io->io_hdr.blocked_queue)) {
14184 mtx_lock(&lun->lun_lock);
14185 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
14186 ctl_try_unblock_others(lun, io, FALSE);
14187 mtx_unlock(&lun->lun_lock);
14188 } else
14189 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
14190 }
14191
14192 void
14193 ctl_done(union ctl_io *io)
14194 {
14195
14196 /*
14197 * Enable this to catch duplicate completion issues.
14198 */
14199 #if 0
14200 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
14201 switch (io->io_hdr.io_type) {
14202 case CTL_IO_SCSI:
14203 case CTL_IO_TASK:
14204 printf("%s: type %d msg %d cdb %x iptl: "
14205 "%u:%u:%u tag 0x%04lx "
14206 "flag %#x status %x\n",
14207 __func__,
14208 io->io_hdr.io_type,
14209 io->io_hdr.msg_type,
14210 io->scsiio.cdb[0],
14211 io->io_hdr.nexus.initid,
14212 io->io_hdr.nexus.targ_port,
14213 io->io_hdr.nexus.targ_lun,
14214 (io->io_hdr.io_type == CTL_IO_TASK) ?
14215 io->taskio.tag_num :
14216 io->scsiio.tag_num,
14217 io->io_hdr.flags,
14218 io->io_hdr.status);
14219 break;
14220 case CTL_IO_NVME:
14221 case CTL_IO_NVME_ADMIN:
14222 printf("%s: type %d msg %d opc %x iptl: "
14223 "%u:%u:%u cid 0x%04x "
14224 "flag %#x status %x\n",
14225 __func__,
14226 io->io_hdr.io_type,
14227 io->io_hdr.msg_type,
14228 io->nvmeio.cmd.opc,
14229 io->io_hdr.nexus.initid,
14230 io->io_hdr.nexus.targ_port,
14231 io->io_hdr.nexus.targ_lun,
14232 io->nvmeio.cmd.cid,
14233 io->io_hdr.flags,
14234 io->io_hdr.status);
14235 break;
14236 default:
14237 printf("%s: type %d msg %d iptl: "
14238 "%u:%u:%u flag %#x status %x\n",
14239 __func__,
14240 io->io_hdr.io_type,
14241 io->io_hdr.msg_type,
14242 io->io_hdr.nexus.initid,
14243 io->io_hdr.nexus.targ_port,
14244 io->io_hdr.nexus.targ_lun,
14245 io->io_hdr.flags,
14246 io->io_hdr.status);
14247 break;
14248 }
14249 } else
14250 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
14251 #endif
14252
14253 /*
14254 * This is an internal copy of an I/O, and should not go through
14255 * the normal done processing logic.
14256 */
14257 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
14258 return;
14259
14260 #ifdef CTL_IO_DELAY
14261 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
14262 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
14263 } else {
14264 struct ctl_lun *lun = CTL_LUN(io);
14265
14266 if ((lun != NULL)
14267 && (lun->delay_info.done_delay > 0)) {
14268 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
14269 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
14270 callout_reset(&io->io_hdr.delay_callout,
14271 lun->delay_info.done_delay * hz,
14272 ctl_done_timer_wakeup, io);
14273 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
14274 lun->delay_info.done_delay = 0;
14275 return;
14276 }
14277 }
14278 #endif /* CTL_IO_DELAY */
14279
14280 ctl_enqueue_done(io);
14281 }
14282
14283 static void
14284 ctl_work_thread(void *arg)
14285 {
14286 struct ctl_thread *thr = (struct ctl_thread *)arg;
14287 struct ctl_softc *softc = thr->ctl_softc;
14288 union ctl_io *io;
14289 int retval;
14290
14291 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
14292 thread_lock(curthread);
14293 sched_prio(curthread, PUSER - 1);
14294 thread_unlock(curthread);
14295
14296 while (!softc->shutdown) {
14297 /*
14298 * We handle the queues in this order:
14299 * - ISC
14300 * - done queue (to free up resources, unblock other commands)
14301 * - incoming queue
14302 * - RtR queue
14303 *
14304 * If those queues are empty, we break out of the loop and
14305 * go to sleep.
14306 */
14307 mtx_lock(&thr->queue_lock);
14308 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
14309 if (io != NULL) {
14310 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
14311 mtx_unlock(&thr->queue_lock);
14312 ctl_handle_isc(io);
14313 continue;
14314 }
14315 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
14316 if (io != NULL) {
14317 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
14318 /* clear any blocked commands, call fe_done */
14319 mtx_unlock(&thr->queue_lock);
14320 ctl_process_done(io);
14321 continue;
14322 }
14323 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
14324 if (io != NULL) {
14325 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
14326 mtx_unlock(&thr->queue_lock);
14327 switch (io->io_hdr.io_type) {
14328 case CTL_IO_TASK:
14329 ctl_run_task(io);
14330 break;
14331 case CTL_IO_SCSI:
14332 ctl_scsiio_precheck(&io->scsiio);
14333 break;
14334 case CTL_IO_NVME:
14335 case CTL_IO_NVME_ADMIN:
14336 ctl_nvmeio_precheck(&io->nvmeio);
14337 break;
14338 default:
14339 __assert_unreachable();
14340 }
14341 continue;
14342 }
14343 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
14344 if (io != NULL) {
14345 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
14346 mtx_unlock(&thr->queue_lock);
14347 switch (io->io_hdr.io_type) {
14348 case CTL_IO_SCSI:
14349 retval = ctl_scsiio(&io->scsiio);
14350 if (retval != CTL_RETVAL_COMPLETE)
14351 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
14352 break;
14353 case CTL_IO_NVME:
14354 case CTL_IO_NVME_ADMIN:
14355 retval = ctl_nvmeio(&io->nvmeio);
14356 if (retval != CTL_RETVAL_COMPLETE)
14357 CTL_DEBUG_PRINT(("ctl_nvmeio failed\n"));
14358 break;
14359 default:
14360 __assert_unreachable();
14361 }
14362 continue;
14363 }
14364
14365 /* Sleep until we have something to do. */
14366 mtx_sleep(thr, &thr->queue_lock, PDROP, "-", 0);
14367 }
14368 thr->thread = NULL;
14369 kthread_exit();
14370 }
14371
14372 static void
14373 ctl_thresh_thread(void *arg)
14374 {
14375 struct ctl_softc *softc = (struct ctl_softc *)arg;
14376 struct ctl_lun *lun;
14377 struct ctl_logical_block_provisioning_page *page;
14378 const char *attr;
14379 union ctl_ha_msg msg;
14380 uint64_t thres, val;
14381 int i, e, set;
14382
14383 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
14384 thread_lock(curthread);
14385 sched_prio(curthread, PUSER - 1);
14386 thread_unlock(curthread);
14387
14388 while (!softc->shutdown) {
14389 mtx_lock(&softc->ctl_lock);
14390 STAILQ_FOREACH(lun, &softc->lun_list, links) {
14391 if ((lun->flags & CTL_LUN_DISABLED) ||
14392 (lun->flags & CTL_LUN_NO_MEDIA) ||
14393 lun->backend->lun_attr == NULL)
14394 continue;
14395 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
14396 softc->ha_mode == CTL_HA_MODE_XFER)
14397 continue;
14398 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0)
14399 continue;
14400 e = 0;
14401 page = &lun->MODE_LBP;
14402 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
14403 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
14404 continue;
14405 thres = scsi_4btoul(page->descr[i].count);
14406 thres <<= CTL_LBP_EXPONENT;
14407 switch (page->descr[i].resource) {
14408 case 0x01:
14409 attr = "blocksavail";
14410 break;
14411 case 0x02:
14412 attr = "blocksused";
14413 break;
14414 case 0xf1:
14415 attr = "poolblocksavail";
14416 break;
14417 case 0xf2:
14418 attr = "poolblocksused";
14419 break;
14420 default:
14421 continue;
14422 }
14423 mtx_unlock(&softc->ctl_lock); // XXX
14424 val = lun->backend->lun_attr(lun->be_lun, attr);
14425 mtx_lock(&softc->ctl_lock);
14426 if (val == UINT64_MAX)
14427 continue;
14428 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
14429 == SLBPPD_ARMING_INC)
14430 e = (val >= thres);
14431 else
14432 e = (val <= thres);
14433 if (e)
14434 break;
14435 }
14436 mtx_lock(&lun->lun_lock);
14437 if (e) {
14438 scsi_u64to8b((uint8_t *)&page->descr[i] -
14439 (uint8_t *)page, lun->ua_tpt_info);
14440 if (lun->lasttpt == 0 ||
14441 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
14442 lun->lasttpt = time_uptime;
14443 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
14444 set = 1;
14445 } else
14446 set = 0;
14447 } else {
14448 lun->lasttpt = 0;
14449 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
14450 set = -1;
14451 }
14452 mtx_unlock(&lun->lun_lock);
14453 if (set != 0 &&
14454 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
14455 /* Send msg to other side. */
14456 bzero(&msg.ua, sizeof(msg.ua));
14457 msg.hdr.msg_type = CTL_MSG_UA;
14458 msg.hdr.nexus.initid = -1;
14459 msg.hdr.nexus.targ_port = -1;
14460 msg.hdr.nexus.targ_lun = lun->lun;
14461 msg.hdr.nexus.targ_mapped_lun = lun->lun;
14462 msg.ua.ua_all = 1;
14463 msg.ua.ua_set = (set > 0);
14464 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
14465 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
14466 mtx_unlock(&softc->ctl_lock); // XXX
14467 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
14468 sizeof(msg.ua), M_WAITOK);
14469 mtx_lock(&softc->ctl_lock);
14470 }
14471 }
14472 mtx_sleep(&softc->thresh_thread, &softc->ctl_lock,
14473 PDROP, "-", CTL_LBP_PERIOD * hz);
14474 }
14475 softc->thresh_thread = NULL;
14476 kthread_exit();
14477 }
14478
14479 static void
14480 ctl_enqueue_incoming(union ctl_io *io)
14481 {
14482 struct ctl_softc *softc = CTL_SOFTC(io);
14483 struct ctl_thread *thr;
14484 u_int idx;
14485
14486 idx = (io->io_hdr.nexus.targ_port * 127 +
14487 io->io_hdr.nexus.initid) % worker_threads;
14488 thr = &softc->threads[idx];
14489 mtx_lock(&thr->queue_lock);
14490 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
14491 mtx_unlock(&thr->queue_lock);
14492 wakeup(thr);
14493 }
14494
14495 static void
14496 ctl_enqueue_rtr(union ctl_io *io)
14497 {
14498 struct ctl_softc *softc = CTL_SOFTC(io);
14499 struct ctl_thread *thr;
14500
14501 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14502 mtx_lock(&thr->queue_lock);
14503 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
14504 mtx_unlock(&thr->queue_lock);
14505 wakeup(thr);
14506 }
14507
14508 static void
14509 ctl_enqueue_done(union ctl_io *io)
14510 {
14511 struct ctl_softc *softc = CTL_SOFTC(io);
14512 struct ctl_thread *thr;
14513
14514 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14515 mtx_lock(&thr->queue_lock);
14516 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
14517 mtx_unlock(&thr->queue_lock);
14518 wakeup(thr);
14519 }
14520
14521 static void
14522 ctl_enqueue_isc(union ctl_io *io)
14523 {
14524 struct ctl_softc *softc = CTL_SOFTC(io);
14525 struct ctl_thread *thr;
14526
14527 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14528 mtx_lock(&thr->queue_lock);
14529 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
14530 mtx_unlock(&thr->queue_lock);
14531 wakeup(thr);
14532 }
14533
14534 /*
14535 * vim: ts=8
14536 */
14537