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/cdefs.h>
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/ctype.h>
51 #include <sys/kernel.h>
52 #include <sys/types.h>
53 #include <sys/kthread.h>
54 #include <sys/bio.h>
55 #include <sys/fcntl.h>
56 #include <sys/lock.h>
57 #include <sys/module.h>
58 #include <sys/mutex.h>
59 #include <sys/condvar.h>
60 #include <sys/malloc.h>
61 #include <sys/conf.h>
62 #include <sys/ioccom.h>
63 #include <sys/queue.h>
64 #include <sys/sbuf.h>
65 #include <sys/smp.h>
66 #include <sys/endian.h>
67 #include <sys/proc.h>
68 #include <sys/sched.h>
69 #include <sys/sysctl.h>
70 #include <sys/nv.h>
71 #include <sys/dnv.h>
72 #include <vm/uma.h>
73
74 #include <cam/cam.h>
75 #include <cam/scsi/scsi_all.h>
76 #include <cam/scsi/scsi_cd.h>
77 #include <cam/scsi/scsi_da.h>
78 #include <cam/ctl/ctl_io.h>
79 #include <cam/ctl/ctl.h>
80 #include <cam/ctl/ctl_frontend.h>
81 #include <cam/ctl/ctl_util.h>
82 #include <cam/ctl/ctl_backend.h>
83 #include <cam/ctl/ctl_ioctl.h>
84 #include <cam/ctl/ctl_ha.h>
85 #include <cam/ctl/ctl_private.h>
86 #include <cam/ctl/ctl_debug.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_format_page format_page_default = {
129 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
130 /*page_length*/sizeof(struct scsi_format_page) - 2,
131 /*tracks_per_zone*/ {0, 0},
132 /*alt_sectors_per_zone*/ {0, 0},
133 /*alt_tracks_per_zone*/ {0, 0},
134 /*alt_tracks_per_lun*/ {0, 0},
135 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
136 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
137 /*bytes_per_sector*/ {0, 0},
138 /*interleave*/ {0, 0},
139 /*track_skew*/ {0, 0},
140 /*cylinder_skew*/ {0, 0},
141 /*flags*/ SFP_HSEC,
142 /*reserved*/ {0, 0, 0}
143 };
144
145 const static struct scsi_format_page format_page_changeable = {
146 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
147 /*page_length*/sizeof(struct scsi_format_page) - 2,
148 /*tracks_per_zone*/ {0, 0},
149 /*alt_sectors_per_zone*/ {0, 0},
150 /*alt_tracks_per_zone*/ {0, 0},
151 /*alt_tracks_per_lun*/ {0, 0},
152 /*sectors_per_track*/ {0, 0},
153 /*bytes_per_sector*/ {0, 0},
154 /*interleave*/ {0, 0},
155 /*track_skew*/ {0, 0},
156 /*cylinder_skew*/ {0, 0},
157 /*flags*/ 0,
158 /*reserved*/ {0, 0, 0}
159 };
160
161 const static struct scsi_rigid_disk_page rigid_disk_page_default = {
162 /*page_code*/SMS_RIGID_DISK_PAGE,
163 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
164 /*cylinders*/ {0, 0, 0},
165 /*heads*/ CTL_DEFAULT_HEADS,
166 /*start_write_precomp*/ {0, 0, 0},
167 /*start_reduced_current*/ {0, 0, 0},
168 /*step_rate*/ {0, 0},
169 /*landing_zone_cylinder*/ {0, 0, 0},
170 /*rpl*/ SRDP_RPL_DISABLED,
171 /*rotational_offset*/ 0,
172 /*reserved1*/ 0,
173 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
174 CTL_DEFAULT_ROTATION_RATE & 0xff},
175 /*reserved2*/ {0, 0}
176 };
177
178 const static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
179 /*page_code*/SMS_RIGID_DISK_PAGE,
180 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
181 /*cylinders*/ {0, 0, 0},
182 /*heads*/ 0,
183 /*start_write_precomp*/ {0, 0, 0},
184 /*start_reduced_current*/ {0, 0, 0},
185 /*step_rate*/ {0, 0},
186 /*landing_zone_cylinder*/ {0, 0, 0},
187 /*rpl*/ 0,
188 /*rotational_offset*/ 0,
189 /*reserved1*/ 0,
190 /*rotation_rate*/ {0, 0},
191 /*reserved2*/ {0, 0}
192 };
193
194 const static struct scsi_da_verify_recovery_page verify_er_page_default = {
195 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
196 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
197 /*byte3*/0,
198 /*read_retry_count*/0,
199 /*reserved*/{ 0, 0, 0, 0, 0, 0 },
200 /*recovery_time_limit*/{0, 0},
201 };
202
203 const static struct scsi_da_verify_recovery_page verify_er_page_changeable = {
204 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
205 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
206 /*byte3*/SMS_VER_PER,
207 /*read_retry_count*/0,
208 /*reserved*/{ 0, 0, 0, 0, 0, 0 },
209 /*recovery_time_limit*/{0, 0},
210 };
211
212 const static struct scsi_caching_page caching_page_default = {
213 /*page_code*/SMS_CACHING_PAGE,
214 /*page_length*/sizeof(struct scsi_caching_page) - 2,
215 /*flags1*/ SCP_DISC | SCP_WCE,
216 /*ret_priority*/ 0,
217 /*disable_pf_transfer_len*/ {0xff, 0xff},
218 /*min_prefetch*/ {0, 0},
219 /*max_prefetch*/ {0xff, 0xff},
220 /*max_pf_ceiling*/ {0xff, 0xff},
221 /*flags2*/ 0,
222 /*cache_segments*/ 0,
223 /*cache_seg_size*/ {0, 0},
224 /*reserved*/ 0,
225 /*non_cache_seg_size*/ {0, 0, 0}
226 };
227
228 const static struct scsi_caching_page caching_page_changeable = {
229 /*page_code*/SMS_CACHING_PAGE,
230 /*page_length*/sizeof(struct scsi_caching_page) - 2,
231 /*flags1*/ SCP_WCE | SCP_RCD,
232 /*ret_priority*/ 0,
233 /*disable_pf_transfer_len*/ {0, 0},
234 /*min_prefetch*/ {0, 0},
235 /*max_prefetch*/ {0, 0},
236 /*max_pf_ceiling*/ {0, 0},
237 /*flags2*/ 0,
238 /*cache_segments*/ 0,
239 /*cache_seg_size*/ {0, 0},
240 /*reserved*/ 0,
241 /*non_cache_seg_size*/ {0, 0, 0}
242 };
243
244 const static struct scsi_control_page control_page_default = {
245 /*page_code*/SMS_CONTROL_MODE_PAGE,
246 /*page_length*/sizeof(struct scsi_control_page) - 2,
247 /*rlec*/0,
248 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
249 /*eca_and_aen*/0,
250 /*flags4*/SCP_TAS,
251 /*aen_holdoff_period*/{0, 0},
252 /*busy_timeout_period*/{0, 0},
253 /*extended_selftest_completion_time*/{0, 0}
254 };
255
256 const static struct scsi_control_page control_page_changeable = {
257 /*page_code*/SMS_CONTROL_MODE_PAGE,
258 /*page_length*/sizeof(struct scsi_control_page) - 2,
259 /*rlec*/SCP_DSENSE,
260 /*queue_flags*/SCP_QUEUE_ALG_MASK | SCP_NUAR,
261 /*eca_and_aen*/SCP_SWP,
262 /*flags4*/0,
263 /*aen_holdoff_period*/{0, 0},
264 /*busy_timeout_period*/{0, 0},
265 /*extended_selftest_completion_time*/{0, 0}
266 };
267
268 #define CTL_CEM_LEN (sizeof(struct scsi_control_ext_page) - 4)
269
270 const static struct scsi_control_ext_page control_ext_page_default = {
271 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
272 /*subpage_code*/0x01,
273 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
274 /*flags*/0,
275 /*prio*/0,
276 /*max_sense*/0
277 };
278
279 const static struct scsi_control_ext_page control_ext_page_changeable = {
280 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
281 /*subpage_code*/0x01,
282 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
283 /*flags*/0,
284 /*prio*/0,
285 /*max_sense*/0xff
286 };
287
288 const static struct scsi_info_exceptions_page ie_page_default = {
289 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
290 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
291 /*info_flags*/SIEP_FLAGS_EWASC,
292 /*mrie*/SIEP_MRIE_NO,
293 /*interval_timer*/{0, 0, 0, 0},
294 /*report_count*/{0, 0, 0, 1}
295 };
296
297 const static struct scsi_info_exceptions_page ie_page_changeable = {
298 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
299 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
300 /*info_flags*/SIEP_FLAGS_EWASC | SIEP_FLAGS_DEXCPT | SIEP_FLAGS_TEST |
301 SIEP_FLAGS_LOGERR,
302 /*mrie*/0x0f,
303 /*interval_timer*/{0xff, 0xff, 0xff, 0xff},
304 /*report_count*/{0xff, 0xff, 0xff, 0xff}
305 };
306
307 #define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4)
308
309 const static struct ctl_logical_block_provisioning_page lbp_page_default = {{
310 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
311 /*subpage_code*/0x02,
312 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
313 /*flags*/0,
314 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
315 /*descr*/{}},
316 {{/*flags*/0,
317 /*resource*/0x01,
318 /*reserved*/{0, 0},
319 /*count*/{0, 0, 0, 0}},
320 {/*flags*/0,
321 /*resource*/0x02,
322 /*reserved*/{0, 0},
323 /*count*/{0, 0, 0, 0}},
324 {/*flags*/0,
325 /*resource*/0xf1,
326 /*reserved*/{0, 0},
327 /*count*/{0, 0, 0, 0}},
328 {/*flags*/0,
329 /*resource*/0xf2,
330 /*reserved*/{0, 0},
331 /*count*/{0, 0, 0, 0}}
332 }
333 };
334
335 const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{
336 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
337 /*subpage_code*/0x02,
338 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
339 /*flags*/SLBPP_SITUA,
340 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
341 /*descr*/{}},
342 {{/*flags*/0,
343 /*resource*/0,
344 /*reserved*/{0, 0},
345 /*count*/{0, 0, 0, 0}},
346 {/*flags*/0,
347 /*resource*/0,
348 /*reserved*/{0, 0},
349 /*count*/{0, 0, 0, 0}},
350 {/*flags*/0,
351 /*resource*/0,
352 /*reserved*/{0, 0},
353 /*count*/{0, 0, 0, 0}},
354 {/*flags*/0,
355 /*resource*/0,
356 /*reserved*/{0, 0},
357 /*count*/{0, 0, 0, 0}}
358 }
359 };
360
361 const static struct scsi_cddvd_capabilities_page cddvd_page_default = {
362 /*page_code*/SMS_CDDVD_CAPS_PAGE,
363 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
364 /*caps1*/0x3f,
365 /*caps2*/0x00,
366 /*caps3*/0xf0,
367 /*caps4*/0x00,
368 /*caps5*/0x29,
369 /*caps6*/0x00,
370 /*obsolete*/{0, 0},
371 /*nvol_levels*/{0, 0},
372 /*buffer_size*/{8, 0},
373 /*obsolete2*/{0, 0},
374 /*reserved*/0,
375 /*digital*/0,
376 /*obsolete3*/0,
377 /*copy_management*/0,
378 /*reserved2*/0,
379 /*rotation_control*/0,
380 /*cur_write_speed*/0,
381 /*num_speed_descr*/0,
382 };
383
384 const static struct scsi_cddvd_capabilities_page cddvd_page_changeable = {
385 /*page_code*/SMS_CDDVD_CAPS_PAGE,
386 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
387 /*caps1*/0,
388 /*caps2*/0,
389 /*caps3*/0,
390 /*caps4*/0,
391 /*caps5*/0,
392 /*caps6*/0,
393 /*obsolete*/{0, 0},
394 /*nvol_levels*/{0, 0},
395 /*buffer_size*/{0, 0},
396 /*obsolete2*/{0, 0},
397 /*reserved*/0,
398 /*digital*/0,
399 /*obsolete3*/0,
400 /*copy_management*/0,
401 /*reserved2*/0,
402 /*rotation_control*/0,
403 /*cur_write_speed*/0,
404 /*num_speed_descr*/0,
405 };
406
407 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
408 "CAM Target Layer");
409 static int worker_threads = -1;
410 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
411 &worker_threads, 1, "Number of worker threads");
412 static int ctl_debug = CTL_DEBUG_NONE;
413 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
414 &ctl_debug, 0, "Enabled debug flags");
415 static int ctl_lun_map_size = 1024;
416 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, lun_map_size, CTLFLAG_RWTUN,
417 &ctl_lun_map_size, 0, "Size of per-port LUN map (max LUN + 1)");
418 #ifdef CTL_TIME_IO
419 static int ctl_time_io_secs = CTL_TIME_IO_DEFAULT_SECS;
420 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, time_io_secs, CTLFLAG_RWTUN,
421 &ctl_time_io_secs, 0, "Log requests taking more seconds");
422 #endif
423
424 /*
425 * Maximum number of LUNs we support. MUST be a power of 2.
426 */
427 #define CTL_DEFAULT_MAX_LUNS 1024
428 static int ctl_max_luns = CTL_DEFAULT_MAX_LUNS;
429 TUNABLE_INT("kern.cam.ctl.max_luns", &ctl_max_luns);
430 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, max_luns, CTLFLAG_RDTUN,
431 &ctl_max_luns, CTL_DEFAULT_MAX_LUNS, "Maximum number of LUNs");
432
433 /*
434 * Maximum number of ports registered at one time.
435 */
436 #define CTL_DEFAULT_MAX_PORTS 1024
437 static int ctl_max_ports = CTL_DEFAULT_MAX_PORTS;
438 TUNABLE_INT("kern.cam.ctl.max_ports", &ctl_max_ports);
439 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, max_ports, CTLFLAG_RDTUN,
440 &ctl_max_ports, CTL_DEFAULT_MAX_LUNS, "Maximum number of ports");
441
442 /*
443 * Maximum number of initiators we support.
444 */
445 #define CTL_MAX_INITIATORS (CTL_MAX_INIT_PER_PORT * ctl_max_ports)
446
447 /*
448 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
449 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
450 * SCSI Ports (0x88), Third-party Copy (0x8F), SCSI Feature Sets (0x92),
451 * Block limits (0xB0), Block Device Characteristics (0xB1) and
452 * Logical Block Provisioning (0xB2)
453 */
454 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 11
455
456 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
457 int param);
458 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
459 static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest);
460 static int ctl_init(void);
461 static int ctl_shutdown(void);
462 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
463 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
464 static void ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
465 static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
466 struct ctl_ooa *ooa_hdr,
467 struct ctl_ooa_entry *kern_entries);
468 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
469 struct thread *td);
470 static int ctl_enable_lun(struct ctl_lun *lun);
471 static int ctl_disable_lun(struct ctl_lun *lun);
472 static int ctl_free_lun(struct ctl_lun *lun);
473
474 static int ctl_do_mode_select(union ctl_io *io);
475 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
476 uint64_t res_key, uint64_t sa_res_key,
477 uint8_t type, uint32_t residx,
478 struct ctl_scsiio *ctsio,
479 struct scsi_per_res_out *cdb,
480 struct scsi_per_res_out_parms* param);
481 static void ctl_pro_preempt_other(struct ctl_lun *lun,
482 union ctl_ha_msg *msg);
483 static void ctl_hndl_per_res_out_on_other_sc(union ctl_io *io);
484 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
485 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
486 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
487 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
488 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
489 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
490 int alloc_len);
491 static int ctl_inquiry_evpd_sfs(struct ctl_scsiio *ctsio, int alloc_len);
492 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
493 int alloc_len);
494 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
495 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
496 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
497 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
498 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
499 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2,
500 bool seq);
501 static ctl_action ctl_seq_check(union ctl_io *io1, union ctl_io *io2);
502 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
503 union ctl_io *pending_io, const uint8_t *serialize_row,
504 union ctl_io *ooa_io);
505 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
506 union ctl_io **starting_io);
507 static void ctl_try_unblock_io(struct ctl_lun *lun, union ctl_io *io,
508 bool skip);
509 static void ctl_try_unblock_others(struct ctl_lun *lun, union ctl_io *io,
510 bool skip);
511 static int ctl_scsiio_lun_check(struct ctl_lun *lun,
512 const struct ctl_cmd_entry *entry,
513 struct ctl_scsiio *ctsio);
514 static void ctl_failover_lun(union ctl_io *io);
515 static void ctl_scsiio_precheck(struct ctl_scsiio *ctsio);
516 static int ctl_scsiio(struct ctl_scsiio *ctsio);
517
518 static int ctl_target_reset(union ctl_io *io);
519 static void ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx,
520 ctl_ua_type ua_type);
521 static int ctl_lun_reset(union ctl_io *io);
522 static int ctl_abort_task(union ctl_io *io);
523 static int ctl_abort_task_set(union ctl_io *io);
524 static int ctl_query_task(union ctl_io *io, int task_set);
525 static void ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx,
526 ctl_ua_type ua_type);
527 static int ctl_i_t_nexus_reset(union ctl_io *io);
528 static int ctl_query_async_event(union ctl_io *io);
529 static void ctl_run_task(union ctl_io *io);
530 #ifdef CTL_IO_DELAY
531 static void ctl_datamove_timer_wakeup(void *arg);
532 static void ctl_done_timer_wakeup(void *arg);
533 #endif /* CTL_IO_DELAY */
534
535 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
536 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
537 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io, bool samethr);
538 static void ctl_datamove_remote_write(union ctl_io *io);
539 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io, bool samethr);
540 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
541 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
542 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
543 ctl_ha_dt_cb callback);
544 static void ctl_datamove_remote_read(union ctl_io *io);
545 static void ctl_datamove_remote(union ctl_io *io);
546 static void ctl_process_done(union ctl_io *io);
547 static void ctl_thresh_thread(void *arg);
548 static void ctl_work_thread(void *arg);
549 static void ctl_enqueue_incoming(union ctl_io *io);
550 static void ctl_enqueue_rtr(union ctl_io *io);
551 static void ctl_enqueue_done(union ctl_io *io);
552 static void ctl_enqueue_isc(union ctl_io *io);
553 static const struct ctl_cmd_entry *
554 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
555 static const struct ctl_cmd_entry *
556 ctl_validate_command(struct ctl_scsiio *ctsio);
557 static int ctl_cmd_applicable(uint8_t lun_type,
558 const struct ctl_cmd_entry *entry);
559 static int ctl_ha_init(void);
560 static int ctl_ha_shutdown(void);
561
562 static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx);
563 static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx);
564 static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx);
565 static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key);
566
567 /*
568 * Load the serialization table. This isn't very pretty, but is probably
569 * the easiest way to do it.
570 */
571 #include "ctl_ser_table.c"
572
573 /*
574 * We only need to define open, close and ioctl routines for this driver.
575 */
576 static struct cdevsw ctl_cdevsw = {
577 .d_version = D_VERSION,
578 .d_flags = 0,
579 .d_open = ctl_open,
580 .d_close = ctl_close,
581 .d_ioctl = ctl_ioctl,
582 .d_name = "ctl",
583 };
584
585 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
586
587 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
588
589 static moduledata_t ctl_moduledata = {
590 "ctl",
591 ctl_module_event_handler,
592 NULL
593 };
594
595 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
596 MODULE_VERSION(ctl, 1);
597
598 static struct ctl_frontend ha_frontend =
599 {
600 .name = "ha",
601 .init = ctl_ha_init,
602 .shutdown = ctl_ha_shutdown,
603 };
604
605 static int
ctl_ha_init(void)606 ctl_ha_init(void)
607 {
608 struct ctl_softc *softc = control_softc;
609
610 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
611 &softc->othersc_pool) != 0)
612 return (ENOMEM);
613 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
614 ctl_pool_free(softc->othersc_pool);
615 return (EIO);
616 }
617 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
618 != CTL_HA_STATUS_SUCCESS) {
619 ctl_ha_msg_destroy(softc);
620 ctl_pool_free(softc->othersc_pool);
621 return (EIO);
622 }
623 return (0);
624 };
625
626 static int
ctl_ha_shutdown(void)627 ctl_ha_shutdown(void)
628 {
629 struct ctl_softc *softc = control_softc;
630 struct ctl_port *port;
631
632 ctl_ha_msg_shutdown(softc);
633 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) != CTL_HA_STATUS_SUCCESS)
634 return (EIO);
635 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS)
636 return (EIO);
637 ctl_pool_free(softc->othersc_pool);
638 while ((port = STAILQ_FIRST(&ha_frontend.port_list)) != NULL) {
639 ctl_port_deregister(port);
640 free(port->port_name, M_CTL);
641 free(port, M_CTL);
642 }
643 return (0);
644 };
645
646 static void
ctl_ha_datamove(union ctl_io * io)647 ctl_ha_datamove(union ctl_io *io)
648 {
649 struct ctl_lun *lun = CTL_LUN(io);
650 struct ctl_sg_entry *sgl;
651 union ctl_ha_msg msg;
652 uint32_t sg_entries_sent;
653 int do_sg_copy, i, j;
654
655 memset(&msg.dt, 0, sizeof(msg.dt));
656 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
657 msg.hdr.original_sc = io->io_hdr.remote_io;
658 msg.hdr.serializing_sc = io;
659 msg.hdr.nexus = io->io_hdr.nexus;
660 msg.hdr.status = io->io_hdr.status;
661 msg.dt.flags = io->io_hdr.flags;
662
663 /*
664 * We convert everything into a S/G list here. We can't
665 * pass by reference, only by value between controllers.
666 * So we can't pass a pointer to the S/G list, only as many
667 * S/G entries as we can fit in here. If it's possible for
668 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
669 * then we need to break this up into multiple transfers.
670 */
671 if (io->scsiio.kern_sg_entries == 0) {
672 msg.dt.kern_sg_entries = 1;
673 #if 0
674 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
675 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
676 } else {
677 /* XXX KDM use busdma here! */
678 msg.dt.sg_list[0].addr =
679 (void *)vtophys(io->scsiio.kern_data_ptr);
680 }
681 #else
682 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
683 ("HA does not support BUS_ADDR"));
684 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
685 #endif
686 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
687 do_sg_copy = 0;
688 } else {
689 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
690 do_sg_copy = 1;
691 }
692
693 msg.dt.kern_data_len = io->scsiio.kern_data_len;
694 msg.dt.kern_total_len = io->scsiio.kern_total_len;
695 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
696 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
697 msg.dt.sg_sequence = 0;
698
699 /*
700 * Loop until we've sent all of the S/G entries. On the
701 * other end, we'll recompose these S/G entries into one
702 * contiguous list before processing.
703 */
704 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries;
705 msg.dt.sg_sequence++) {
706 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) /
707 sizeof(msg.dt.sg_list[0])),
708 msg.dt.kern_sg_entries - sg_entries_sent);
709 if (do_sg_copy != 0) {
710 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
711 for (i = sg_entries_sent, j = 0;
712 i < msg.dt.cur_sg_entries; i++, j++) {
713 #if 0
714 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
715 msg.dt.sg_list[j].addr = sgl[i].addr;
716 } else {
717 /* XXX KDM use busdma here! */
718 msg.dt.sg_list[j].addr =
719 (void *)vtophys(sgl[i].addr);
720 }
721 #else
722 KASSERT((io->io_hdr.flags &
723 CTL_FLAG_BUS_ADDR) == 0,
724 ("HA does not support BUS_ADDR"));
725 msg.dt.sg_list[j].addr = sgl[i].addr;
726 #endif
727 msg.dt.sg_list[j].len = sgl[i].len;
728 }
729 }
730
731 sg_entries_sent += msg.dt.cur_sg_entries;
732 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries);
733 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
734 sizeof(msg.dt) - sizeof(msg.dt.sg_list) +
735 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries,
736 M_WAITOK) > CTL_HA_STATUS_SUCCESS) {
737 io->io_hdr.port_status = 31341;
738 ctl_datamove_done(io, true);
739 return;
740 }
741 msg.dt.sent_sg_entries = sg_entries_sent;
742 }
743
744 /*
745 * Officially handover the request from us to peer.
746 * If failover has just happened, then we must return error.
747 * If failover happen just after, then it is not our problem.
748 */
749 if (lun)
750 mtx_lock(&lun->lun_lock);
751 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
752 if (lun)
753 mtx_unlock(&lun->lun_lock);
754 io->io_hdr.port_status = 31342;
755 ctl_datamove_done(io, true);
756 return;
757 }
758 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
759 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
760 if (lun)
761 mtx_unlock(&lun->lun_lock);
762 }
763
764 static void
ctl_ha_done(union ctl_io * io)765 ctl_ha_done(union ctl_io *io)
766 {
767 union ctl_ha_msg msg;
768
769 if (io->io_hdr.io_type == CTL_IO_SCSI) {
770 memset(&msg, 0, sizeof(msg));
771 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
772 msg.hdr.original_sc = io->io_hdr.remote_io;
773 msg.hdr.nexus = io->io_hdr.nexus;
774 msg.hdr.status = io->io_hdr.status;
775 msg.scsi.scsi_status = io->scsiio.scsi_status;
776 msg.scsi.tag_num = io->scsiio.tag_num;
777 msg.scsi.tag_type = io->scsiio.tag_type;
778 msg.scsi.sense_len = io->scsiio.sense_len;
779 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
780 io->scsiio.sense_len);
781 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
782 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
783 msg.scsi.sense_len, M_WAITOK);
784 }
785 ctl_free_io(io);
786 }
787
788 static void
ctl_isc_handler_finish_xfer(struct ctl_softc * ctl_softc,union ctl_ha_msg * msg_info)789 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
790 union ctl_ha_msg *msg_info)
791 {
792 struct ctl_scsiio *ctsio;
793
794 if (msg_info->hdr.original_sc == NULL) {
795 printf("%s: original_sc == NULL!\n", __func__);
796 /* XXX KDM now what? */
797 return;
798 }
799
800 ctsio = &msg_info->hdr.original_sc->scsiio;
801 ctsio->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
802 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
803 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
804 ctsio->io_hdr.status = msg_info->hdr.status;
805 ctsio->scsi_status = msg_info->scsi.scsi_status;
806 ctsio->sense_len = msg_info->scsi.sense_len;
807 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
808 msg_info->scsi.sense_len);
809 ctl_enqueue_isc((union ctl_io *)ctsio);
810 }
811
812 static void
ctl_isc_handler_finish_ser_only(struct ctl_softc * ctl_softc,union ctl_ha_msg * msg_info)813 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
814 union ctl_ha_msg *msg_info)
815 {
816 struct ctl_scsiio *ctsio;
817
818 if (msg_info->hdr.serializing_sc == NULL) {
819 printf("%s: serializing_sc == NULL!\n", __func__);
820 /* XXX KDM now what? */
821 return;
822 }
823
824 ctsio = &msg_info->hdr.serializing_sc->scsiio;
825 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
826 ctl_enqueue_isc((union ctl_io *)ctsio);
827 }
828
829 void
ctl_isc_announce_lun(struct ctl_lun * lun)830 ctl_isc_announce_lun(struct ctl_lun *lun)
831 {
832 struct ctl_softc *softc = lun->ctl_softc;
833 union ctl_ha_msg *msg;
834 struct ctl_ha_msg_lun_pr_key pr_key;
835 int i, k;
836
837 if (softc->ha_link != CTL_HA_LINK_ONLINE)
838 return;
839 mtx_lock(&lun->lun_lock);
840 i = sizeof(msg->lun);
841 if (lun->lun_devid)
842 i += lun->lun_devid->len;
843 i += sizeof(pr_key) * lun->pr_key_count;
844 alloc:
845 mtx_unlock(&lun->lun_lock);
846 msg = malloc(i, M_CTL, M_WAITOK);
847 mtx_lock(&lun->lun_lock);
848 k = sizeof(msg->lun);
849 if (lun->lun_devid)
850 k += lun->lun_devid->len;
851 k += sizeof(pr_key) * lun->pr_key_count;
852 if (i < k) {
853 free(msg, M_CTL);
854 i = k;
855 goto alloc;
856 }
857 bzero(&msg->lun, sizeof(msg->lun));
858 msg->hdr.msg_type = CTL_MSG_LUN_SYNC;
859 msg->hdr.nexus.targ_lun = lun->lun;
860 msg->hdr.nexus.targ_mapped_lun = lun->lun;
861 msg->lun.flags = lun->flags;
862 msg->lun.pr_generation = lun->pr_generation;
863 msg->lun.pr_res_idx = lun->pr_res_idx;
864 msg->lun.pr_res_type = lun->pr_res_type;
865 msg->lun.pr_key_count = lun->pr_key_count;
866 i = 0;
867 if (lun->lun_devid) {
868 msg->lun.lun_devid_len = lun->lun_devid->len;
869 memcpy(&msg->lun.data[i], lun->lun_devid->data,
870 msg->lun.lun_devid_len);
871 i += msg->lun.lun_devid_len;
872 }
873 for (k = 0; k < CTL_MAX_INITIATORS; k++) {
874 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0)
875 continue;
876 pr_key.pr_iid = k;
877 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key));
878 i += sizeof(pr_key);
879 }
880 mtx_unlock(&lun->lun_lock);
881 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->lun, sizeof(msg->lun) + i,
882 M_WAITOK);
883 free(msg, M_CTL);
884
885 if (lun->flags & CTL_LUN_PRIMARY_SC) {
886 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
887 ctl_isc_announce_mode(lun, -1,
888 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
889 lun->mode_pages.index[i].subpage);
890 }
891 }
892 }
893
894 void
ctl_isc_announce_port(struct ctl_port * port)895 ctl_isc_announce_port(struct ctl_port *port)
896 {
897 struct ctl_softc *softc = port->ctl_softc;
898 union ctl_ha_msg *msg;
899 int i;
900
901 if (port->targ_port < softc->port_min ||
902 port->targ_port >= softc->port_max ||
903 softc->ha_link != CTL_HA_LINK_ONLINE)
904 return;
905 i = sizeof(msg->port) + strlen(port->port_name) + 1;
906 if (port->lun_map)
907 i += port->lun_map_size * sizeof(uint32_t);
908 if (port->port_devid)
909 i += port->port_devid->len;
910 if (port->target_devid)
911 i += port->target_devid->len;
912 if (port->init_devid)
913 i += port->init_devid->len;
914 msg = malloc(i, M_CTL, M_WAITOK);
915 bzero(&msg->port, sizeof(msg->port));
916 msg->hdr.msg_type = CTL_MSG_PORT_SYNC;
917 msg->hdr.nexus.targ_port = port->targ_port;
918 msg->port.port_type = port->port_type;
919 msg->port.physical_port = port->physical_port;
920 msg->port.virtual_port = port->virtual_port;
921 msg->port.status = port->status;
922 i = 0;
923 msg->port.name_len = sprintf(&msg->port.data[i],
924 "%d:%s", softc->ha_id, port->port_name) + 1;
925 i += msg->port.name_len;
926 if (port->lun_map) {
927 msg->port.lun_map_len = port->lun_map_size * sizeof(uint32_t);
928 memcpy(&msg->port.data[i], port->lun_map,
929 msg->port.lun_map_len);
930 i += msg->port.lun_map_len;
931 }
932 if (port->port_devid) {
933 msg->port.port_devid_len = port->port_devid->len;
934 memcpy(&msg->port.data[i], port->port_devid->data,
935 msg->port.port_devid_len);
936 i += msg->port.port_devid_len;
937 }
938 if (port->target_devid) {
939 msg->port.target_devid_len = port->target_devid->len;
940 memcpy(&msg->port.data[i], port->target_devid->data,
941 msg->port.target_devid_len);
942 i += msg->port.target_devid_len;
943 }
944 if (port->init_devid) {
945 msg->port.init_devid_len = port->init_devid->len;
946 memcpy(&msg->port.data[i], port->init_devid->data,
947 msg->port.init_devid_len);
948 i += msg->port.init_devid_len;
949 }
950 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
951 M_WAITOK);
952 free(msg, M_CTL);
953 }
954
955 void
ctl_isc_announce_iid(struct ctl_port * port,int iid)956 ctl_isc_announce_iid(struct ctl_port *port, int iid)
957 {
958 struct ctl_softc *softc = port->ctl_softc;
959 union ctl_ha_msg *msg;
960 int i, l;
961
962 if (port->targ_port < softc->port_min ||
963 port->targ_port >= softc->port_max ||
964 softc->ha_link != CTL_HA_LINK_ONLINE)
965 return;
966 mtx_lock(&softc->ctl_lock);
967 i = sizeof(msg->iid);
968 l = 0;
969 if (port->wwpn_iid[iid].name)
970 l = strlen(port->wwpn_iid[iid].name) + 1;
971 i += l;
972 msg = malloc(i, M_CTL, M_NOWAIT);
973 if (msg == NULL) {
974 mtx_unlock(&softc->ctl_lock);
975 return;
976 }
977 bzero(&msg->iid, sizeof(msg->iid));
978 msg->hdr.msg_type = CTL_MSG_IID_SYNC;
979 msg->hdr.nexus.targ_port = port->targ_port;
980 msg->hdr.nexus.initid = iid;
981 msg->iid.in_use = port->wwpn_iid[iid].in_use;
982 msg->iid.name_len = l;
983 msg->iid.wwpn = port->wwpn_iid[iid].wwpn;
984 if (port->wwpn_iid[iid].name)
985 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l);
986 mtx_unlock(&softc->ctl_lock);
987 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT);
988 free(msg, M_CTL);
989 }
990
991 void
ctl_isc_announce_mode(struct ctl_lun * lun,uint32_t initidx,uint8_t page,uint8_t subpage)992 ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx,
993 uint8_t page, uint8_t subpage)
994 {
995 struct ctl_softc *softc = lun->ctl_softc;
996 union ctl_ha_msg *msg;
997 u_int i, l;
998
999 if (softc->ha_link != CTL_HA_LINK_ONLINE)
1000 return;
1001 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
1002 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
1003 page && lun->mode_pages.index[i].subpage == subpage)
1004 break;
1005 }
1006 if (i == CTL_NUM_MODE_PAGES)
1007 return;
1008
1009 /* Don't try to replicate pages not present on this device. */
1010 if (lun->mode_pages.index[i].page_data == NULL)
1011 return;
1012
1013 l = sizeof(msg->mode) + lun->mode_pages.index[i].page_len;
1014 msg = malloc(l, M_CTL, M_WAITOK | M_ZERO);
1015 msg->hdr.msg_type = CTL_MSG_MODE_SYNC;
1016 msg->hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT;
1017 msg->hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT;
1018 msg->hdr.nexus.targ_lun = lun->lun;
1019 msg->hdr.nexus.targ_mapped_lun = lun->lun;
1020 msg->mode.page_code = page;
1021 msg->mode.subpage = subpage;
1022 msg->mode.page_len = lun->mode_pages.index[i].page_len;
1023 memcpy(msg->mode.data, lun->mode_pages.index[i].page_data,
1024 msg->mode.page_len);
1025 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->mode, l, M_WAITOK);
1026 free(msg, M_CTL);
1027 }
1028
1029 static void
ctl_isc_ha_link_up(struct ctl_softc * softc)1030 ctl_isc_ha_link_up(struct ctl_softc *softc)
1031 {
1032 struct ctl_port *port;
1033 struct ctl_lun *lun;
1034 union ctl_ha_msg msg;
1035 int i;
1036
1037 /* Announce this node parameters to peer for validation. */
1038 msg.login.msg_type = CTL_MSG_LOGIN;
1039 msg.login.version = CTL_HA_VERSION;
1040 msg.login.ha_mode = softc->ha_mode;
1041 msg.login.ha_id = softc->ha_id;
1042 msg.login.max_luns = ctl_max_luns;
1043 msg.login.max_ports = ctl_max_ports;
1044 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT;
1045 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login),
1046 M_WAITOK);
1047
1048 STAILQ_FOREACH(port, &softc->port_list, links) {
1049 ctl_isc_announce_port(port);
1050 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1051 if (port->wwpn_iid[i].in_use)
1052 ctl_isc_announce_iid(port, i);
1053 }
1054 }
1055 STAILQ_FOREACH(lun, &softc->lun_list, links)
1056 ctl_isc_announce_lun(lun);
1057 }
1058
1059 static void
ctl_isc_ha_link_down(struct ctl_softc * softc)1060 ctl_isc_ha_link_down(struct ctl_softc *softc)
1061 {
1062 struct ctl_port *port;
1063 struct ctl_lun *lun;
1064 union ctl_io *io;
1065 int i;
1066
1067 mtx_lock(&softc->ctl_lock);
1068 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1069 mtx_lock(&lun->lun_lock);
1070 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) {
1071 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1072 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1073 }
1074 mtx_unlock(&lun->lun_lock);
1075
1076 mtx_unlock(&softc->ctl_lock);
1077 io = ctl_alloc_io(softc->othersc_pool);
1078 mtx_lock(&softc->ctl_lock);
1079 ctl_zero_io(io);
1080 io->io_hdr.msg_type = CTL_MSG_FAILOVER;
1081 io->io_hdr.nexus.targ_mapped_lun = lun->lun;
1082 ctl_enqueue_isc(io);
1083 }
1084
1085 STAILQ_FOREACH(port, &softc->port_list, links) {
1086 if (port->targ_port >= softc->port_min &&
1087 port->targ_port < softc->port_max)
1088 continue;
1089 port->status &= ~CTL_PORT_STATUS_ONLINE;
1090 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1091 port->wwpn_iid[i].in_use = 0;
1092 free(port->wwpn_iid[i].name, M_CTL);
1093 port->wwpn_iid[i].name = NULL;
1094 }
1095 }
1096 mtx_unlock(&softc->ctl_lock);
1097 }
1098
1099 static void
ctl_isc_ua(struct ctl_softc * softc,union ctl_ha_msg * msg,int len)1100 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1101 {
1102 struct ctl_lun *lun;
1103 uint32_t iid;
1104
1105 if (len < sizeof(msg->ua)) {
1106 printf("%s: Received truncated message %d < %zu\n",
1107 __func__, len, sizeof(msg->ua));
1108 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1109 return;
1110 }
1111
1112 mtx_lock(&softc->ctl_lock);
1113 if (msg->hdr.nexus.targ_mapped_lun >= ctl_max_luns ||
1114 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) == NULL) {
1115 mtx_unlock(&softc->ctl_lock);
1116 return;
1117 }
1118 mtx_lock(&lun->lun_lock);
1119 mtx_unlock(&softc->ctl_lock);
1120 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && msg->ua.ua_set)
1121 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8);
1122 iid = ctl_get_initindex(&msg->hdr.nexus);
1123 if (msg->ua.ua_all) {
1124 if (msg->ua.ua_set)
1125 ctl_est_ua_all(lun, iid, msg->ua.ua_type);
1126 else
1127 ctl_clr_ua_all(lun, iid, msg->ua.ua_type);
1128 } else {
1129 if (msg->ua.ua_set)
1130 ctl_est_ua(lun, iid, msg->ua.ua_type);
1131 else
1132 ctl_clr_ua(lun, iid, msg->ua.ua_type);
1133 }
1134 mtx_unlock(&lun->lun_lock);
1135 }
1136
1137 static void
ctl_isc_lun_sync(struct ctl_softc * softc,union ctl_ha_msg * msg,int len)1138 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1139 {
1140 struct ctl_lun *lun;
1141 struct ctl_ha_msg_lun_pr_key pr_key;
1142 int i, k;
1143 ctl_lun_flags oflags;
1144 uint32_t targ_lun;
1145
1146 if (len < offsetof(struct ctl_ha_msg_lun, data[0])) {
1147 printf("%s: Received truncated message %d < %zu\n",
1148 __func__, len, offsetof(struct ctl_ha_msg_lun, data[0]));
1149 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1150 return;
1151 }
1152 i = msg->lun.lun_devid_len + msg->lun.pr_key_count * sizeof(pr_key);
1153 if (len < offsetof(struct ctl_ha_msg_lun, data[i])) {
1154 printf("%s: Received truncated message data %d < %zu\n",
1155 __func__, len, offsetof(struct ctl_ha_msg_lun, data[i]));
1156 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1157 return;
1158 }
1159
1160 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1161 mtx_lock(&softc->ctl_lock);
1162 if (targ_lun >= ctl_max_luns ||
1163 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1164 mtx_unlock(&softc->ctl_lock);
1165 return;
1166 }
1167 mtx_lock(&lun->lun_lock);
1168 mtx_unlock(&softc->ctl_lock);
1169 if (lun->flags & CTL_LUN_DISABLED) {
1170 mtx_unlock(&lun->lun_lock);
1171 return;
1172 }
1173 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0;
1174 if (msg->lun.lun_devid_len != i || (i > 0 &&
1175 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) {
1176 mtx_unlock(&lun->lun_lock);
1177 printf("%s: Received conflicting HA LUN %d\n",
1178 __func__, targ_lun);
1179 return;
1180 } else {
1181 /* Record whether peer is primary. */
1182 oflags = lun->flags;
1183 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) &&
1184 (msg->lun.flags & CTL_LUN_DISABLED) == 0)
1185 lun->flags |= CTL_LUN_PEER_SC_PRIMARY;
1186 else
1187 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1188 if (oflags != lun->flags)
1189 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1190
1191 /* If peer is primary and we are not -- use data */
1192 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
1193 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) {
1194 lun->pr_generation = msg->lun.pr_generation;
1195 lun->pr_res_idx = msg->lun.pr_res_idx;
1196 lun->pr_res_type = msg->lun.pr_res_type;
1197 lun->pr_key_count = msg->lun.pr_key_count;
1198 for (k = 0; k < CTL_MAX_INITIATORS; k++)
1199 ctl_clr_prkey(lun, k);
1200 for (k = 0; k < msg->lun.pr_key_count; k++) {
1201 memcpy(&pr_key, &msg->lun.data[i],
1202 sizeof(pr_key));
1203 ctl_alloc_prkey(lun, pr_key.pr_iid);
1204 ctl_set_prkey(lun, pr_key.pr_iid,
1205 pr_key.pr_key);
1206 i += sizeof(pr_key);
1207 }
1208 }
1209
1210 mtx_unlock(&lun->lun_lock);
1211 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n",
1212 __func__, targ_lun,
1213 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ?
1214 "primary" : "secondary"));
1215
1216 /* If we are primary but peer doesn't know -- notify */
1217 if ((lun->flags & CTL_LUN_PRIMARY_SC) &&
1218 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0)
1219 ctl_isc_announce_lun(lun);
1220 }
1221 }
1222
1223 static void
ctl_isc_port_sync(struct ctl_softc * softc,union ctl_ha_msg * msg,int len)1224 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1225 {
1226 struct ctl_port *port;
1227 struct ctl_lun *lun;
1228 int i, new;
1229
1230 if (len < offsetof(struct ctl_ha_msg_port, data[0])) {
1231 printf("%s: Received truncated message %d < %zu\n",
1232 __func__, len, offsetof(struct ctl_ha_msg_port, data[0]));
1233 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1234 return;
1235 }
1236 i = msg->port.name_len + msg->port.lun_map_len +
1237 msg->port.port_devid_len + msg->port.target_devid_len +
1238 msg->port.init_devid_len;
1239 if (len < offsetof(struct ctl_ha_msg_port, data[i])) {
1240 printf("%s: Received truncated message data %d < %zu\n",
1241 __func__, len, offsetof(struct ctl_ha_msg_port, data[i]));
1242 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1243 return;
1244 }
1245
1246 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1247 if (port == NULL) {
1248 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__,
1249 msg->hdr.nexus.targ_port));
1250 new = 1;
1251 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO);
1252 port->frontend = &ha_frontend;
1253 port->targ_port = msg->hdr.nexus.targ_port;
1254 port->fe_datamove = ctl_ha_datamove;
1255 port->fe_done = ctl_ha_done;
1256 } else if (port->frontend == &ha_frontend) {
1257 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__,
1258 msg->hdr.nexus.targ_port));
1259 new = 0;
1260 } else {
1261 printf("%s: Received conflicting HA port %d\n",
1262 __func__, msg->hdr.nexus.targ_port);
1263 return;
1264 }
1265 port->port_type = msg->port.port_type;
1266 port->physical_port = msg->port.physical_port;
1267 port->virtual_port = msg->port.virtual_port;
1268 port->status = msg->port.status;
1269 i = 0;
1270 free(port->port_name, M_CTL);
1271 port->port_name = strndup(&msg->port.data[i], msg->port.name_len,
1272 M_CTL);
1273 i += msg->port.name_len;
1274 if (msg->port.lun_map_len != 0) {
1275 if (port->lun_map == NULL ||
1276 port->lun_map_size * sizeof(uint32_t) <
1277 msg->port.lun_map_len) {
1278 port->lun_map_size = 0;
1279 free(port->lun_map, M_CTL);
1280 port->lun_map = malloc(msg->port.lun_map_len,
1281 M_CTL, M_WAITOK);
1282 }
1283 memcpy(port->lun_map, &msg->port.data[i], msg->port.lun_map_len);
1284 port->lun_map_size = msg->port.lun_map_len / sizeof(uint32_t);
1285 i += msg->port.lun_map_len;
1286 } else {
1287 port->lun_map_size = 0;
1288 free(port->lun_map, M_CTL);
1289 port->lun_map = NULL;
1290 }
1291 if (msg->port.port_devid_len != 0) {
1292 if (port->port_devid == NULL ||
1293 port->port_devid->len < msg->port.port_devid_len) {
1294 free(port->port_devid, M_CTL);
1295 port->port_devid = malloc(sizeof(struct ctl_devid) +
1296 msg->port.port_devid_len, M_CTL, M_WAITOK);
1297 }
1298 memcpy(port->port_devid->data, &msg->port.data[i],
1299 msg->port.port_devid_len);
1300 port->port_devid->len = msg->port.port_devid_len;
1301 i += msg->port.port_devid_len;
1302 } else {
1303 free(port->port_devid, M_CTL);
1304 port->port_devid = NULL;
1305 }
1306 if (msg->port.target_devid_len != 0) {
1307 if (port->target_devid == NULL ||
1308 port->target_devid->len < msg->port.target_devid_len) {
1309 free(port->target_devid, M_CTL);
1310 port->target_devid = malloc(sizeof(struct ctl_devid) +
1311 msg->port.target_devid_len, M_CTL, M_WAITOK);
1312 }
1313 memcpy(port->target_devid->data, &msg->port.data[i],
1314 msg->port.target_devid_len);
1315 port->target_devid->len = msg->port.target_devid_len;
1316 i += msg->port.target_devid_len;
1317 } else {
1318 free(port->target_devid, M_CTL);
1319 port->target_devid = NULL;
1320 }
1321 if (msg->port.init_devid_len != 0) {
1322 if (port->init_devid == NULL ||
1323 port->init_devid->len < msg->port.init_devid_len) {
1324 free(port->init_devid, M_CTL);
1325 port->init_devid = malloc(sizeof(struct ctl_devid) +
1326 msg->port.init_devid_len, M_CTL, M_WAITOK);
1327 }
1328 memcpy(port->init_devid->data, &msg->port.data[i],
1329 msg->port.init_devid_len);
1330 port->init_devid->len = msg->port.init_devid_len;
1331 i += msg->port.init_devid_len;
1332 } else {
1333 free(port->init_devid, M_CTL);
1334 port->init_devid = NULL;
1335 }
1336 if (new) {
1337 if (ctl_port_register(port) != 0) {
1338 printf("%s: ctl_port_register() failed with error\n",
1339 __func__);
1340 }
1341 }
1342 mtx_lock(&softc->ctl_lock);
1343 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1344 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
1345 continue;
1346 mtx_lock(&lun->lun_lock);
1347 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
1348 mtx_unlock(&lun->lun_lock);
1349 }
1350 mtx_unlock(&softc->ctl_lock);
1351 }
1352
1353 static void
ctl_isc_iid_sync(struct ctl_softc * softc,union ctl_ha_msg * msg,int len)1354 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1355 {
1356 struct ctl_port *port;
1357 int i, iid;
1358
1359 if (len < offsetof(struct ctl_ha_msg_iid, data[0])) {
1360 printf("%s: Received truncated message %d < %zu\n",
1361 __func__, len, offsetof(struct ctl_ha_msg_iid, data[0]));
1362 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1363 return;
1364 }
1365 i = msg->iid.name_len;
1366 if (len < offsetof(struct ctl_ha_msg_iid, data[i])) {
1367 printf("%s: Received truncated message data %d < %zu\n",
1368 __func__, len, offsetof(struct ctl_ha_msg_iid, data[i]));
1369 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1370 return;
1371 }
1372
1373 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1374 if (port == NULL) {
1375 printf("%s: Received IID for unknown port %d\n",
1376 __func__, msg->hdr.nexus.targ_port);
1377 return;
1378 }
1379 iid = msg->hdr.nexus.initid;
1380 if (port->wwpn_iid[iid].in_use != 0 &&
1381 msg->iid.in_use == 0)
1382 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON);
1383 port->wwpn_iid[iid].in_use = msg->iid.in_use;
1384 port->wwpn_iid[iid].wwpn = msg->iid.wwpn;
1385 free(port->wwpn_iid[iid].name, M_CTL);
1386 if (msg->iid.name_len) {
1387 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0],
1388 msg->iid.name_len, M_CTL);
1389 } else
1390 port->wwpn_iid[iid].name = NULL;
1391 }
1392
1393 static void
ctl_isc_login(struct ctl_softc * softc,union ctl_ha_msg * msg,int len)1394 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1395 {
1396
1397 if (len < sizeof(msg->login)) {
1398 printf("%s: Received truncated message %d < %zu\n",
1399 __func__, len, sizeof(msg->login));
1400 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1401 return;
1402 }
1403
1404 if (msg->login.version != CTL_HA_VERSION) {
1405 printf("CTL HA peers have different versions %d != %d\n",
1406 msg->login.version, CTL_HA_VERSION);
1407 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1408 return;
1409 }
1410 if (msg->login.ha_mode != softc->ha_mode) {
1411 printf("CTL HA peers have different ha_mode %d != %d\n",
1412 msg->login.ha_mode, softc->ha_mode);
1413 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1414 return;
1415 }
1416 if (msg->login.ha_id == softc->ha_id) {
1417 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id);
1418 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1419 return;
1420 }
1421 if (msg->login.max_luns != ctl_max_luns ||
1422 msg->login.max_ports != ctl_max_ports ||
1423 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) {
1424 printf("CTL HA peers have different limits\n");
1425 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1426 return;
1427 }
1428 }
1429
1430 static void
ctl_isc_mode_sync(struct ctl_softc * softc,union ctl_ha_msg * msg,int len)1431 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1432 {
1433 struct ctl_lun *lun;
1434 u_int i;
1435 uint32_t initidx, targ_lun;
1436
1437 if (len < offsetof(struct ctl_ha_msg_mode, data[0])) {
1438 printf("%s: Received truncated message %d < %zu\n",
1439 __func__, len, offsetof(struct ctl_ha_msg_mode, data[0]));
1440 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1441 return;
1442 }
1443 i = msg->mode.page_len;
1444 if (len < offsetof(struct ctl_ha_msg_mode, data[i])) {
1445 printf("%s: Received truncated message data %d < %zu\n",
1446 __func__, len, offsetof(struct ctl_ha_msg_mode, data[i]));
1447 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1448 return;
1449 }
1450
1451 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1452 mtx_lock(&softc->ctl_lock);
1453 if (targ_lun >= ctl_max_luns ||
1454 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1455 mtx_unlock(&softc->ctl_lock);
1456 return;
1457 }
1458 mtx_lock(&lun->lun_lock);
1459 mtx_unlock(&softc->ctl_lock);
1460 if (lun->flags & CTL_LUN_DISABLED) {
1461 mtx_unlock(&lun->lun_lock);
1462 return;
1463 }
1464 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
1465 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
1466 msg->mode.page_code &&
1467 lun->mode_pages.index[i].subpage == msg->mode.subpage)
1468 break;
1469 }
1470 if (i == CTL_NUM_MODE_PAGES) {
1471 mtx_unlock(&lun->lun_lock);
1472 return;
1473 }
1474 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data,
1475 min(lun->mode_pages.index[i].page_len, msg->mode.page_len));
1476 initidx = ctl_get_initindex(&msg->hdr.nexus);
1477 if (initidx != -1)
1478 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
1479 mtx_unlock(&lun->lun_lock);
1480 }
1481
1482 /*
1483 * ISC (Inter Shelf Communication) event handler. Events from the HA
1484 * subsystem come in here.
1485 */
1486 static void
ctl_isc_event_handler(ctl_ha_channel channel,ctl_ha_event event,int param)1487 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
1488 {
1489 struct ctl_softc *softc = control_softc;
1490 union ctl_io *io;
1491 struct ctl_prio *presio;
1492 ctl_ha_status isc_status;
1493
1494 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event));
1495 if (event == CTL_HA_EVT_MSG_RECV) {
1496 union ctl_ha_msg *msg, msgbuf;
1497
1498 if (param > sizeof(msgbuf))
1499 msg = malloc(param, M_CTL, M_WAITOK);
1500 else
1501 msg = &msgbuf;
1502 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param,
1503 M_WAITOK);
1504 if (isc_status != CTL_HA_STATUS_SUCCESS) {
1505 printf("%s: Error receiving message: %d\n",
1506 __func__, isc_status);
1507 if (msg != &msgbuf)
1508 free(msg, M_CTL);
1509 return;
1510 }
1511
1512 CTL_DEBUG_PRINT(("CTL: msg_type %d len %d\n",
1513 msg->hdr.msg_type, param));
1514 switch (msg->hdr.msg_type) {
1515 case CTL_MSG_SERIALIZE:
1516 io = ctl_alloc_io(softc->othersc_pool);
1517 ctl_zero_io(io);
1518 // populate ctsio from msg
1519 io->io_hdr.io_type = CTL_IO_SCSI;
1520 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
1521 io->io_hdr.remote_io = msg->hdr.original_sc;
1522 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
1523 CTL_FLAG_IO_ACTIVE;
1524 /*
1525 * If we're in serialization-only mode, we don't
1526 * want to go through full done processing. Thus
1527 * the COPY flag.
1528 *
1529 * XXX KDM add another flag that is more specific.
1530 */
1531 if (softc->ha_mode != CTL_HA_MODE_XFER)
1532 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
1533 io->io_hdr.nexus = msg->hdr.nexus;
1534 io->scsiio.priority = msg->scsi.priority;
1535 io->scsiio.tag_num = msg->scsi.tag_num;
1536 io->scsiio.tag_type = msg->scsi.tag_type;
1537 #ifdef CTL_TIME_IO
1538 io->io_hdr.start_time = time_uptime;
1539 getbinuptime(&io->io_hdr.start_bt);
1540 #endif /* CTL_TIME_IO */
1541 io->scsiio.cdb_len = msg->scsi.cdb_len;
1542 memcpy(io->scsiio.cdb, msg->scsi.cdb,
1543 CTL_MAX_CDBLEN);
1544 if (softc->ha_mode == CTL_HA_MODE_XFER) {
1545 const struct ctl_cmd_entry *entry;
1546
1547 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
1548 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
1549 io->io_hdr.flags |=
1550 entry->flags & CTL_FLAG_DATA_MASK;
1551 }
1552 ctl_enqueue_isc(io);
1553 break;
1554
1555 /* Performed on the Originating SC, XFER mode only */
1556 case CTL_MSG_DATAMOVE: {
1557 struct ctl_sg_entry *sgl;
1558 int i, j;
1559
1560 io = msg->hdr.original_sc;
1561 if (io == NULL) {
1562 printf("%s: original_sc == NULL!\n", __func__);
1563 /* XXX KDM do something here */
1564 break;
1565 }
1566 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
1567 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1568 /*
1569 * Keep track of this, we need to send it back over
1570 * when the datamove is complete.
1571 */
1572 io->io_hdr.remote_io = msg->hdr.serializing_sc;
1573 if (msg->hdr.status == CTL_SUCCESS)
1574 io->io_hdr.status = msg->hdr.status;
1575
1576 if (msg->dt.sg_sequence == 0) {
1577 #ifdef CTL_TIME_IO
1578 getbinuptime(&io->io_hdr.dma_start_bt);
1579 #endif
1580 i = msg->dt.kern_sg_entries +
1581 msg->dt.kern_data_len /
1582 CTL_HA_DATAMOVE_SEGMENT + 1;
1583 sgl = malloc(sizeof(*sgl) * i, M_CTL,
1584 M_WAITOK | M_ZERO);
1585 CTL_RSGL(io) = sgl;
1586 CTL_LSGL(io) = &sgl[msg->dt.kern_sg_entries];
1587
1588 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
1589
1590 io->scsiio.kern_sg_entries =
1591 msg->dt.kern_sg_entries;
1592 io->scsiio.rem_sg_entries =
1593 msg->dt.kern_sg_entries;
1594 io->scsiio.kern_data_len =
1595 msg->dt.kern_data_len;
1596 io->scsiio.kern_total_len =
1597 msg->dt.kern_total_len;
1598 io->scsiio.kern_data_resid =
1599 msg->dt.kern_data_resid;
1600 io->scsiio.kern_rel_offset =
1601 msg->dt.kern_rel_offset;
1602 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR;
1603 io->io_hdr.flags |= msg->dt.flags &
1604 CTL_FLAG_BUS_ADDR;
1605 } else
1606 sgl = (struct ctl_sg_entry *)
1607 io->scsiio.kern_data_ptr;
1608
1609 for (i = msg->dt.sent_sg_entries, j = 0;
1610 i < (msg->dt.sent_sg_entries +
1611 msg->dt.cur_sg_entries); i++, j++) {
1612 sgl[i].addr = msg->dt.sg_list[j].addr;
1613 sgl[i].len = msg->dt.sg_list[j].len;
1614 }
1615
1616 /*
1617 * If this is the last piece of the I/O, we've got
1618 * the full S/G list. Queue processing in the thread.
1619 * Otherwise wait for the next piece.
1620 */
1621 if (msg->dt.sg_last != 0)
1622 ctl_enqueue_isc(io);
1623 break;
1624 }
1625 /* Performed on the Serializing (primary) SC, XFER mode only */
1626 case CTL_MSG_DATAMOVE_DONE: {
1627 if (msg->hdr.serializing_sc == NULL) {
1628 printf("%s: serializing_sc == NULL!\n",
1629 __func__);
1630 /* XXX KDM now what? */
1631 break;
1632 }
1633 /*
1634 * We grab the sense information here in case
1635 * there was a failure, so we can return status
1636 * back to the initiator.
1637 */
1638 io = msg->hdr.serializing_sc;
1639 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
1640 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1641 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1642 io->io_hdr.port_status = msg->scsi.port_status;
1643 io->scsiio.kern_data_resid = msg->scsi.kern_data_resid;
1644 if (msg->hdr.status != CTL_STATUS_NONE) {
1645 io->io_hdr.status = msg->hdr.status;
1646 io->scsiio.scsi_status = msg->scsi.scsi_status;
1647 io->scsiio.sense_len = msg->scsi.sense_len;
1648 memcpy(&io->scsiio.sense_data,
1649 &msg->scsi.sense_data,
1650 msg->scsi.sense_len);
1651 if (msg->hdr.status == CTL_SUCCESS)
1652 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1653 }
1654 ctl_enqueue_isc(io);
1655 break;
1656 }
1657
1658 /* Preformed on Originating SC, SER_ONLY mode */
1659 case CTL_MSG_R2R:
1660 io = msg->hdr.original_sc;
1661 if (io == NULL) {
1662 printf("%s: original_sc == NULL!\n",
1663 __func__);
1664 break;
1665 }
1666 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1667 io->io_hdr.msg_type = CTL_MSG_R2R;
1668 io->io_hdr.remote_io = msg->hdr.serializing_sc;
1669 ctl_enqueue_isc(io);
1670 break;
1671
1672 /*
1673 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
1674 * mode.
1675 * Performed on the Originating (i.e. secondary) SC in XFER
1676 * mode
1677 */
1678 case CTL_MSG_FINISH_IO:
1679 if (softc->ha_mode == CTL_HA_MODE_XFER)
1680 ctl_isc_handler_finish_xfer(softc, msg);
1681 else
1682 ctl_isc_handler_finish_ser_only(softc, msg);
1683 break;
1684
1685 /* Preformed on Originating SC */
1686 case CTL_MSG_BAD_JUJU:
1687 io = msg->hdr.original_sc;
1688 if (io == NULL) {
1689 printf("%s: Bad JUJU!, original_sc is NULL!\n",
1690 __func__);
1691 break;
1692 }
1693 ctl_copy_sense_data(msg, io);
1694 /*
1695 * IO should have already been cleaned up on other
1696 * SC so clear this flag so we won't send a message
1697 * back to finish the IO there.
1698 */
1699 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1700 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1701
1702 /* io = msg->hdr.serializing_sc; */
1703 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
1704 ctl_enqueue_isc(io);
1705 break;
1706
1707 /* Handle resets sent from the other side */
1708 case CTL_MSG_MANAGE_TASKS: {
1709 struct ctl_taskio *taskio;
1710 taskio = (struct ctl_taskio *)ctl_alloc_io(
1711 softc->othersc_pool);
1712 ctl_zero_io((union ctl_io *)taskio);
1713 taskio->io_hdr.io_type = CTL_IO_TASK;
1714 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1715 taskio->io_hdr.nexus = msg->hdr.nexus;
1716 taskio->task_action = msg->task.task_action;
1717 taskio->tag_num = msg->task.tag_num;
1718 taskio->tag_type = msg->task.tag_type;
1719 #ifdef CTL_TIME_IO
1720 taskio->io_hdr.start_time = time_uptime;
1721 getbinuptime(&taskio->io_hdr.start_bt);
1722 #endif /* CTL_TIME_IO */
1723 ctl_run_task((union ctl_io *)taskio);
1724 break;
1725 }
1726 /* Persistent Reserve action which needs attention */
1727 case CTL_MSG_PERS_ACTION:
1728 presio = (struct ctl_prio *)ctl_alloc_io(
1729 softc->othersc_pool);
1730 ctl_zero_io((union ctl_io *)presio);
1731 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
1732 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1733 presio->io_hdr.nexus = msg->hdr.nexus;
1734 presio->pr_msg = msg->pr;
1735 ctl_enqueue_isc((union ctl_io *)presio);
1736 break;
1737 case CTL_MSG_UA:
1738 ctl_isc_ua(softc, msg, param);
1739 break;
1740 case CTL_MSG_PORT_SYNC:
1741 ctl_isc_port_sync(softc, msg, param);
1742 break;
1743 case CTL_MSG_LUN_SYNC:
1744 ctl_isc_lun_sync(softc, msg, param);
1745 break;
1746 case CTL_MSG_IID_SYNC:
1747 ctl_isc_iid_sync(softc, msg, param);
1748 break;
1749 case CTL_MSG_LOGIN:
1750 ctl_isc_login(softc, msg, param);
1751 break;
1752 case CTL_MSG_MODE_SYNC:
1753 ctl_isc_mode_sync(softc, msg, param);
1754 break;
1755 default:
1756 printf("Received HA message of unknown type %d\n",
1757 msg->hdr.msg_type);
1758 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1759 break;
1760 }
1761 if (msg != &msgbuf)
1762 free(msg, M_CTL);
1763 } else if (event == CTL_HA_EVT_LINK_CHANGE) {
1764 printf("CTL: HA link status changed from %d to %d\n",
1765 softc->ha_link, param);
1766 if (param == softc->ha_link)
1767 return;
1768 if (softc->ha_link == CTL_HA_LINK_ONLINE) {
1769 softc->ha_link = param;
1770 ctl_isc_ha_link_down(softc);
1771 } else {
1772 softc->ha_link = param;
1773 if (softc->ha_link == CTL_HA_LINK_ONLINE)
1774 ctl_isc_ha_link_up(softc);
1775 }
1776 return;
1777 } else {
1778 printf("ctl_isc_event_handler: Unknown event %d\n", event);
1779 return;
1780 }
1781 }
1782
1783 static void
ctl_copy_sense_data(union ctl_ha_msg * src,union ctl_io * dest)1784 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
1785 {
1786
1787 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data,
1788 src->scsi.sense_len);
1789 dest->scsiio.scsi_status = src->scsi.scsi_status;
1790 dest->scsiio.sense_len = src->scsi.sense_len;
1791 dest->io_hdr.status = src->hdr.status;
1792 }
1793
1794 static void
ctl_copy_sense_data_back(union ctl_io * src,union ctl_ha_msg * dest)1795 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest)
1796 {
1797
1798 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data,
1799 src->scsiio.sense_len);
1800 dest->scsi.scsi_status = src->scsiio.scsi_status;
1801 dest->scsi.sense_len = src->scsiio.sense_len;
1802 dest->hdr.status = src->io_hdr.status;
1803 }
1804
1805 void
ctl_est_ua(struct ctl_lun * lun,uint32_t initidx,ctl_ua_type ua)1806 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1807 {
1808 struct ctl_softc *softc = lun->ctl_softc;
1809 ctl_ua_type *pu;
1810
1811 if (initidx < softc->init_min || initidx >= softc->init_max)
1812 return;
1813 mtx_assert(&lun->lun_lock, MA_OWNED);
1814 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1815 if (pu == NULL)
1816 return;
1817 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
1818 }
1819
1820 void
ctl_est_ua_port(struct ctl_lun * lun,int port,uint32_t except,ctl_ua_type ua)1821 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua)
1822 {
1823 int i;
1824
1825 mtx_assert(&lun->lun_lock, MA_OWNED);
1826 if (lun->pending_ua[port] == NULL)
1827 return;
1828 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1829 if (port * CTL_MAX_INIT_PER_PORT + i == except)
1830 continue;
1831 lun->pending_ua[port][i] |= ua;
1832 }
1833 }
1834
1835 void
ctl_est_ua_all(struct ctl_lun * lun,uint32_t except,ctl_ua_type ua)1836 ctl_est_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;
1840
1841 mtx_assert(&lun->lun_lock, MA_OWNED);
1842 for (i = softc->port_min; i < softc->port_max; i++)
1843 ctl_est_ua_port(lun, i, except, ua);
1844 }
1845
1846 void
ctl_clr_ua(struct ctl_lun * lun,uint32_t initidx,ctl_ua_type ua)1847 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1848 {
1849 struct ctl_softc *softc = lun->ctl_softc;
1850 ctl_ua_type *pu;
1851
1852 if (initidx < softc->init_min || initidx >= softc->init_max)
1853 return;
1854 mtx_assert(&lun->lun_lock, MA_OWNED);
1855 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1856 if (pu == NULL)
1857 return;
1858 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1859 }
1860
1861 void
ctl_clr_ua_all(struct ctl_lun * lun,uint32_t except,ctl_ua_type ua)1862 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1863 {
1864 struct ctl_softc *softc = lun->ctl_softc;
1865 int i, j;
1866
1867 mtx_assert(&lun->lun_lock, MA_OWNED);
1868 for (i = softc->port_min; i < softc->port_max; i++) {
1869 if (lun->pending_ua[i] == NULL)
1870 continue;
1871 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1872 if (i * CTL_MAX_INIT_PER_PORT + j == except)
1873 continue;
1874 lun->pending_ua[i][j] &= ~ua;
1875 }
1876 }
1877 }
1878
1879 void
ctl_clr_ua_allluns(struct ctl_softc * ctl_softc,uint32_t initidx,ctl_ua_type ua_type)1880 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
1881 ctl_ua_type ua_type)
1882 {
1883 struct ctl_lun *lun;
1884
1885 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
1886 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
1887 mtx_lock(&lun->lun_lock);
1888 ctl_clr_ua(lun, initidx, ua_type);
1889 mtx_unlock(&lun->lun_lock);
1890 }
1891 }
1892
1893 static int
ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)1894 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)
1895 {
1896 struct ctl_softc *softc = (struct ctl_softc *)arg1;
1897 struct ctl_lun *lun;
1898 struct ctl_lun_req ireq;
1899 int error, value;
1900
1901 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1;
1902 error = sysctl_handle_int(oidp, &value, 0, req);
1903 if ((error != 0) || (req->newptr == NULL))
1904 return (error);
1905
1906 mtx_lock(&softc->ctl_lock);
1907 if (value == 0)
1908 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1909 else
1910 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1911 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1912 mtx_unlock(&softc->ctl_lock);
1913 bzero(&ireq, sizeof(ireq));
1914 ireq.reqtype = CTL_LUNREQ_MODIFY;
1915 ireq.reqdata.modify.lun_id = lun->lun;
1916 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0,
1917 curthread);
1918 if (ireq.status != CTL_LUN_OK) {
1919 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n",
1920 __func__, ireq.status, ireq.error_str);
1921 }
1922 mtx_lock(&softc->ctl_lock);
1923 }
1924 mtx_unlock(&softc->ctl_lock);
1925 return (0);
1926 }
1927
1928 static int
ctl_init(void)1929 ctl_init(void)
1930 {
1931 struct make_dev_args args;
1932 struct ctl_softc *softc;
1933 int i, error;
1934
1935 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1936 M_WAITOK | M_ZERO);
1937
1938 make_dev_args_init(&args);
1939 args.mda_devsw = &ctl_cdevsw;
1940 args.mda_uid = UID_ROOT;
1941 args.mda_gid = GID_OPERATOR;
1942 args.mda_mode = 0600;
1943 args.mda_si_drv1 = softc;
1944 args.mda_si_drv2 = NULL;
1945 error = make_dev_s(&args, &softc->dev, "cam/ctl");
1946 if (error != 0) {
1947 free(softc, M_DEVBUF);
1948 control_softc = NULL;
1949 return (error);
1950 }
1951
1952 sysctl_ctx_init(&softc->sysctl_ctx);
1953 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1954 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1955 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "CAM Target Layer");
1956
1957 if (softc->sysctl_tree == NULL) {
1958 printf("%s: unable to allocate sysctl tree\n", __func__);
1959 destroy_dev(softc->dev);
1960 free(softc, M_DEVBUF);
1961 control_softc = NULL;
1962 return (ENOMEM);
1963 }
1964
1965 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1966 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1967 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1968 softc->flags = 0;
1969
1970 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1971 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0,
1972 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)");
1973
1974 if (ctl_max_luns <= 0 || powerof2(ctl_max_luns) == 0) {
1975 printf("Bad value %d for kern.cam.ctl.max_luns, must be a power of two, using %d\n",
1976 ctl_max_luns, CTL_DEFAULT_MAX_LUNS);
1977 ctl_max_luns = CTL_DEFAULT_MAX_LUNS;
1978 }
1979 softc->ctl_luns = malloc(sizeof(struct ctl_lun *) * ctl_max_luns,
1980 M_DEVBUF, M_WAITOK | M_ZERO);
1981 softc->ctl_lun_mask = malloc(sizeof(uint32_t) *
1982 ((ctl_max_luns + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO);
1983 if (ctl_max_ports <= 0 || powerof2(ctl_max_ports) == 0) {
1984 printf("Bad value %d for kern.cam.ctl.max_ports, must be a power of two, using %d\n",
1985 ctl_max_ports, CTL_DEFAULT_MAX_PORTS);
1986 ctl_max_ports = CTL_DEFAULT_MAX_PORTS;
1987 }
1988 softc->ctl_port_mask = malloc(sizeof(uint32_t) *
1989 ((ctl_max_ports + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO);
1990 softc->ctl_ports = malloc(sizeof(struct ctl_port *) * ctl_max_ports,
1991 M_DEVBUF, M_WAITOK | M_ZERO);
1992
1993 /*
1994 * In Copan's HA scheme, the "master" and "slave" roles are
1995 * figured out through the slot the controller is in. Although it
1996 * is an active/active system, someone has to be in charge.
1997 */
1998 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1999 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
2000 "HA head ID (0 - no HA)");
2001 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) {
2002 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
2003 softc->is_single = 1;
2004 softc->port_cnt = ctl_max_ports;
2005 softc->port_min = 0;
2006 } else {
2007 softc->port_cnt = ctl_max_ports / NUM_HA_SHELVES;
2008 softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
2009 }
2010 softc->port_max = softc->port_min + softc->port_cnt;
2011 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
2012 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
2013
2014 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2015 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
2016 "HA link state (0 - offline, 1 - unknown, 2 - online)");
2017
2018 STAILQ_INIT(&softc->lun_list);
2019 STAILQ_INIT(&softc->fe_list);
2020 STAILQ_INIT(&softc->port_list);
2021 STAILQ_INIT(&softc->be_list);
2022 ctl_tpc_init(softc);
2023
2024 if (worker_threads <= 0)
2025 worker_threads = max(1, mp_ncpus / 4);
2026 if (worker_threads > CTL_MAX_THREADS)
2027 worker_threads = CTL_MAX_THREADS;
2028
2029 for (i = 0; i < worker_threads; i++) {
2030 struct ctl_thread *thr = &softc->threads[i];
2031
2032 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
2033 thr->ctl_softc = softc;
2034 STAILQ_INIT(&thr->incoming_queue);
2035 STAILQ_INIT(&thr->rtr_queue);
2036 STAILQ_INIT(&thr->done_queue);
2037 STAILQ_INIT(&thr->isc_queue);
2038
2039 error = kproc_kthread_add(ctl_work_thread, thr,
2040 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
2041 if (error != 0) {
2042 printf("error creating CTL work thread!\n");
2043 return (error);
2044 }
2045 }
2046 error = kproc_kthread_add(ctl_thresh_thread, softc,
2047 &softc->ctl_proc, &softc->thresh_thread, 0, 0, "ctl", "thresh");
2048 if (error != 0) {
2049 printf("error creating CTL threshold thread!\n");
2050 return (error);
2051 }
2052
2053 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
2054 OID_AUTO, "ha_role",
2055 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
2056 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
2057
2058 if (softc->is_single == 0) {
2059 if (ctl_frontend_register(&ha_frontend) != 0)
2060 softc->is_single = 1;
2061 }
2062 return (0);
2063 }
2064
2065 static int
ctl_shutdown(void)2066 ctl_shutdown(void)
2067 {
2068 struct ctl_softc *softc = control_softc;
2069 int i;
2070
2071 if (softc->is_single == 0)
2072 ctl_frontend_deregister(&ha_frontend);
2073
2074 destroy_dev(softc->dev);
2075
2076 /* Shutdown CTL threads. */
2077 softc->shutdown = 1;
2078 for (i = 0; i < worker_threads; i++) {
2079 struct ctl_thread *thr = &softc->threads[i];
2080 while (thr->thread != NULL) {
2081 wakeup(thr);
2082 if (thr->thread != NULL)
2083 pause("CTL thr shutdown", 1);
2084 }
2085 mtx_destroy(&thr->queue_lock);
2086 }
2087 while (softc->thresh_thread != NULL) {
2088 wakeup(softc->thresh_thread);
2089 if (softc->thresh_thread != NULL)
2090 pause("CTL thr shutdown", 1);
2091 }
2092
2093 ctl_tpc_shutdown(softc);
2094 uma_zdestroy(softc->io_zone);
2095 mtx_destroy(&softc->ctl_lock);
2096
2097 free(softc->ctl_luns, M_DEVBUF);
2098 free(softc->ctl_lun_mask, M_DEVBUF);
2099 free(softc->ctl_port_mask, M_DEVBUF);
2100 free(softc->ctl_ports, M_DEVBUF);
2101
2102 sysctl_ctx_free(&softc->sysctl_ctx);
2103
2104 free(softc, M_DEVBUF);
2105 control_softc = NULL;
2106 return (0);
2107 }
2108
2109 static int
ctl_module_event_handler(module_t mod,int what,void * arg)2110 ctl_module_event_handler(module_t mod, int what, void *arg)
2111 {
2112
2113 switch (what) {
2114 case MOD_LOAD:
2115 return (ctl_init());
2116 case MOD_UNLOAD:
2117 return (ctl_shutdown());
2118 default:
2119 return (EOPNOTSUPP);
2120 }
2121 }
2122
2123 /*
2124 * XXX KDM should we do some access checks here? Bump a reference count to
2125 * prevent a CTL module from being unloaded while someone has it open?
2126 */
2127 static int
ctl_open(struct cdev * dev,int flags,int fmt,struct thread * td)2128 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
2129 {
2130 return (0);
2131 }
2132
2133 static int
ctl_close(struct cdev * dev,int flags,int fmt,struct thread * td)2134 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
2135 {
2136 return (0);
2137 }
2138
2139 /*
2140 * Remove an initiator by port number and initiator ID.
2141 * Returns 0 for success, -1 for failure.
2142 */
2143 int
ctl_remove_initiator(struct ctl_port * port,int iid)2144 ctl_remove_initiator(struct ctl_port *port, int iid)
2145 {
2146 struct ctl_softc *softc = port->ctl_softc;
2147 int last;
2148
2149 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2150
2151 if (iid > CTL_MAX_INIT_PER_PORT) {
2152 printf("%s: initiator ID %u > maximun %u!\n",
2153 __func__, iid, CTL_MAX_INIT_PER_PORT);
2154 return (-1);
2155 }
2156
2157 mtx_lock(&softc->ctl_lock);
2158 last = (--port->wwpn_iid[iid].in_use == 0);
2159 port->wwpn_iid[iid].last_use = time_uptime;
2160 mtx_unlock(&softc->ctl_lock);
2161 if (last)
2162 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON);
2163 ctl_isc_announce_iid(port, iid);
2164
2165 return (0);
2166 }
2167
2168 /*
2169 * Add an initiator to the initiator map.
2170 * Returns iid for success, < 0 for failure.
2171 */
2172 int
ctl_add_initiator(struct ctl_port * port,int iid,uint64_t wwpn,char * name)2173 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
2174 {
2175 struct ctl_softc *softc = port->ctl_softc;
2176 time_t best_time;
2177 int i, best;
2178
2179 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2180
2181 if (iid >= CTL_MAX_INIT_PER_PORT) {
2182 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
2183 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
2184 free(name, M_CTL);
2185 return (-1);
2186 }
2187
2188 mtx_lock(&softc->ctl_lock);
2189
2190 if (iid < 0 && (wwpn != 0 || name != NULL)) {
2191 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2192 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
2193 iid = i;
2194 break;
2195 }
2196 if (name != NULL && port->wwpn_iid[i].name != NULL &&
2197 strcmp(name, port->wwpn_iid[i].name) == 0) {
2198 iid = i;
2199 break;
2200 }
2201 }
2202 }
2203
2204 if (iid < 0) {
2205 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2206 if (port->wwpn_iid[i].in_use == 0 &&
2207 port->wwpn_iid[i].wwpn == 0 &&
2208 port->wwpn_iid[i].name == NULL) {
2209 iid = i;
2210 break;
2211 }
2212 }
2213 }
2214
2215 if (iid < 0) {
2216 best = -1;
2217 best_time = INT32_MAX;
2218 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2219 if (port->wwpn_iid[i].in_use == 0) {
2220 if (port->wwpn_iid[i].last_use < best_time) {
2221 best = i;
2222 best_time = port->wwpn_iid[i].last_use;
2223 }
2224 }
2225 }
2226 iid = best;
2227 }
2228
2229 if (iid < 0) {
2230 mtx_unlock(&softc->ctl_lock);
2231 free(name, M_CTL);
2232 return (-2);
2233 }
2234
2235 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
2236 /*
2237 * This is not an error yet.
2238 */
2239 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
2240 #if 0
2241 printf("%s: port %d iid %u WWPN %#jx arrived"
2242 " again\n", __func__, port->targ_port,
2243 iid, (uintmax_t)wwpn);
2244 #endif
2245 goto take;
2246 }
2247 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
2248 strcmp(name, port->wwpn_iid[iid].name) == 0) {
2249 #if 0
2250 printf("%s: port %d iid %u name '%s' arrived"
2251 " again\n", __func__, port->targ_port,
2252 iid, name);
2253 #endif
2254 goto take;
2255 }
2256
2257 /*
2258 * This is an error, but what do we do about it? The
2259 * driver is telling us we have a new WWPN for this
2260 * initiator ID, so we pretty much need to use it.
2261 */
2262 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
2263 " but WWPN %#jx '%s' is still at that address\n",
2264 __func__, port->targ_port, iid, wwpn, name,
2265 (uintmax_t)port->wwpn_iid[iid].wwpn,
2266 port->wwpn_iid[iid].name);
2267 }
2268 take:
2269 free(port->wwpn_iid[iid].name, M_CTL);
2270 port->wwpn_iid[iid].name = name;
2271 port->wwpn_iid[iid].wwpn = wwpn;
2272 port->wwpn_iid[iid].in_use++;
2273 mtx_unlock(&softc->ctl_lock);
2274 ctl_isc_announce_iid(port, iid);
2275
2276 return (iid);
2277 }
2278
2279 static int
ctl_create_iid(struct ctl_port * port,int iid,uint8_t * buf)2280 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
2281 {
2282 int len;
2283
2284 switch (port->port_type) {
2285 case CTL_PORT_FC:
2286 {
2287 struct scsi_transportid_fcp *id =
2288 (struct scsi_transportid_fcp *)buf;
2289 if (port->wwpn_iid[iid].wwpn == 0)
2290 return (0);
2291 memset(id, 0, sizeof(*id));
2292 id->format_protocol = SCSI_PROTO_FC;
2293 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
2294 return (sizeof(*id));
2295 }
2296 case CTL_PORT_ISCSI:
2297 {
2298 struct scsi_transportid_iscsi_port *id =
2299 (struct scsi_transportid_iscsi_port *)buf;
2300 if (port->wwpn_iid[iid].name == NULL)
2301 return (0);
2302 memset(id, 0, 256);
2303 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
2304 SCSI_PROTO_ISCSI;
2305 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
2306 len = roundup2(min(len, 252), 4);
2307 scsi_ulto2b(len, id->additional_length);
2308 return (sizeof(*id) + len);
2309 }
2310 case CTL_PORT_SAS:
2311 {
2312 struct scsi_transportid_sas *id =
2313 (struct scsi_transportid_sas *)buf;
2314 if (port->wwpn_iid[iid].wwpn == 0)
2315 return (0);
2316 memset(id, 0, sizeof(*id));
2317 id->format_protocol = SCSI_PROTO_SAS;
2318 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
2319 return (sizeof(*id));
2320 }
2321 default:
2322 {
2323 struct scsi_transportid_spi *id =
2324 (struct scsi_transportid_spi *)buf;
2325 memset(id, 0, sizeof(*id));
2326 id->format_protocol = SCSI_PROTO_SPI;
2327 scsi_ulto2b(iid, id->scsi_addr);
2328 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
2329 return (sizeof(*id));
2330 }
2331 }
2332 }
2333
2334 /*
2335 * Serialize a command that went down the "wrong" side, and so was sent to
2336 * this controller for execution. The logic is a little different than the
2337 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
2338 * sent back to the other side, but in the success case, we execute the
2339 * command on this side (XFER mode) or tell the other side to execute it
2340 * (SER_ONLY mode).
2341 */
2342 static void
ctl_serialize_other_sc_cmd(struct ctl_scsiio * ctsio)2343 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
2344 {
2345 struct ctl_softc *softc = CTL_SOFTC(ctsio);
2346 struct ctl_port *port = CTL_PORT(ctsio);
2347 union ctl_ha_msg msg_info;
2348 struct ctl_lun *lun;
2349 const struct ctl_cmd_entry *entry;
2350 union ctl_io *bio;
2351 uint32_t targ_lun;
2352
2353 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
2354
2355 /* Make sure that we know about this port. */
2356 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) {
2357 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2358 /*retry_count*/ 1);
2359 goto badjuju;
2360 }
2361
2362 /* Make sure that we know about this LUN. */
2363 mtx_lock(&softc->ctl_lock);
2364 if (targ_lun >= ctl_max_luns ||
2365 (lun = softc->ctl_luns[targ_lun]) == NULL) {
2366 mtx_unlock(&softc->ctl_lock);
2367
2368 /*
2369 * The other node would not send this request to us unless
2370 * received announce that we are primary node for this LUN.
2371 * If this LUN does not exist now, it is probably result of
2372 * a race, so respond to initiator in the most opaque way.
2373 */
2374 ctl_set_busy(ctsio);
2375 goto badjuju;
2376 }
2377 mtx_lock(&lun->lun_lock);
2378 mtx_unlock(&softc->ctl_lock);
2379
2380 /*
2381 * If the LUN is invalid, pretend that it doesn't exist.
2382 * It will go away as soon as all pending I/Os completed.
2383 */
2384 if (lun->flags & CTL_LUN_DISABLED) {
2385 mtx_unlock(&lun->lun_lock);
2386 ctl_set_busy(ctsio);
2387 goto badjuju;
2388 }
2389
2390 entry = ctl_get_cmd_entry(ctsio, NULL);
2391 ctsio->seridx = entry->seridx;
2392 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
2393 mtx_unlock(&lun->lun_lock);
2394 goto badjuju;
2395 }
2396
2397 CTL_LUN(ctsio) = lun;
2398 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
2399
2400 /*
2401 * Every I/O goes into the OOA queue for a
2402 * particular LUN, and stays there until completion.
2403 */
2404 #ifdef CTL_TIME_IO
2405 if (LIST_EMPTY(&lun->ooa_queue))
2406 lun->idle_time += getsbinuptime() - lun->last_busy;
2407 #endif
2408 LIST_INSERT_HEAD(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2409
2410 bio = (union ctl_io *)LIST_NEXT(&ctsio->io_hdr, ooa_links);
2411 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, &bio)) {
2412 case CTL_ACTION_PASS:
2413 case CTL_ACTION_SKIP:
2414 if (softc->ha_mode == CTL_HA_MODE_XFER) {
2415 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
2416 ctl_enqueue_rtr((union ctl_io *)ctsio);
2417 mtx_unlock(&lun->lun_lock);
2418 } else {
2419 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
2420 mtx_unlock(&lun->lun_lock);
2421
2422 /* send msg back to other side */
2423 msg_info.hdr.original_sc = ctsio->io_hdr.remote_io;
2424 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
2425 msg_info.hdr.msg_type = CTL_MSG_R2R;
2426 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2427 sizeof(msg_info.hdr), M_WAITOK);
2428 }
2429 break;
2430 case CTL_ACTION_BLOCK:
2431 ctsio->io_hdr.blocker = bio;
2432 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctsio->io_hdr,
2433 blocked_links);
2434 mtx_unlock(&lun->lun_lock);
2435 break;
2436 case CTL_ACTION_OVERLAP:
2437 LIST_REMOVE(&ctsio->io_hdr, ooa_links);
2438 mtx_unlock(&lun->lun_lock);
2439 ctl_set_overlapped_cmd(ctsio);
2440 goto badjuju;
2441 case CTL_ACTION_OVERLAP_TAG:
2442 LIST_REMOVE(&ctsio->io_hdr, ooa_links);
2443 mtx_unlock(&lun->lun_lock);
2444 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
2445 badjuju:
2446 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
2447 msg_info.hdr.original_sc = ctsio->io_hdr.remote_io;
2448 msg_info.hdr.serializing_sc = NULL;
2449 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
2450 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2451 sizeof(msg_info.scsi), M_WAITOK);
2452 ctl_free_io((union ctl_io *)ctsio);
2453 break;
2454 default:
2455 __assert_unreachable();
2456 }
2457 }
2458
2459 /*
2460 * Returns 0 for success, errno for failure.
2461 */
2462 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)2463 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2464 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2465 {
2466 struct ctl_io_hdr *ioh;
2467
2468 mtx_lock(&lun->lun_lock);
2469 ioh = LIST_FIRST(&lun->ooa_queue);
2470 if (ioh == NULL) {
2471 mtx_unlock(&lun->lun_lock);
2472 return;
2473 }
2474 while (LIST_NEXT(ioh, ooa_links) != NULL)
2475 ioh = LIST_NEXT(ioh, ooa_links);
2476 for ( ; ioh; ioh = LIST_PREV(ioh, &lun->ooa_queue, ctl_io_hdr, ooa_links)) {
2477 union ctl_io *io = (union ctl_io *)ioh;
2478 struct ctl_ooa_entry *entry;
2479
2480 /*
2481 * If we've got more than we can fit, just count the
2482 * remaining entries.
2483 */
2484 if (*cur_fill_num >= ooa_hdr->alloc_num) {
2485 (*cur_fill_num)++;
2486 continue;
2487 }
2488
2489 entry = &kern_entries[*cur_fill_num];
2490
2491 entry->tag_num = io->scsiio.tag_num;
2492 entry->tag_type = io->scsiio.tag_type;
2493 entry->lun_num = lun->lun;
2494 #ifdef CTL_TIME_IO
2495 entry->start_bt = io->io_hdr.start_bt;
2496 #endif
2497 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2498 entry->cdb_len = io->scsiio.cdb_len;
2499 if (io->io_hdr.blocker != NULL)
2500 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2501
2502 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2503 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2504
2505 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2506 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2507
2508 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2509 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2510
2511 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2512 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2513
2514 if (io->io_hdr.flags & CTL_FLAG_STATUS_QUEUED)
2515 entry->cmd_flags |= CTL_OOACMD_FLAG_STATUS_QUEUED;
2516
2517 if (io->io_hdr.flags & CTL_FLAG_STATUS_SENT)
2518 entry->cmd_flags |= CTL_OOACMD_FLAG_STATUS_SENT;
2519 (*cur_fill_num)++;
2520 }
2521 mtx_unlock(&lun->lun_lock);
2522 }
2523
2524 /*
2525 * Escape characters that are illegal or not recommended in XML.
2526 */
2527 int
ctl_sbuf_printf_esc(struct sbuf * sb,char * str,int size)2528 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2529 {
2530 char *end = str + size;
2531 int retval;
2532
2533 retval = 0;
2534
2535 for (; *str && str < end; str++) {
2536 switch (*str) {
2537 case '&':
2538 retval = sbuf_printf(sb, "&");
2539 break;
2540 case '>':
2541 retval = sbuf_printf(sb, ">");
2542 break;
2543 case '<':
2544 retval = sbuf_printf(sb, "<");
2545 break;
2546 default:
2547 retval = sbuf_putc(sb, *str);
2548 break;
2549 }
2550
2551 if (retval != 0)
2552 break;
2553 }
2554
2555 return (retval);
2556 }
2557
2558 static void
ctl_id_sbuf(struct ctl_devid * id,struct sbuf * sb)2559 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2560 {
2561 struct scsi_vpd_id_descriptor *desc;
2562 int i;
2563
2564 if (id == NULL || id->len < 4)
2565 return;
2566 desc = (struct scsi_vpd_id_descriptor *)id->data;
2567 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2568 case SVPD_ID_TYPE_T10:
2569 sbuf_printf(sb, "t10.");
2570 break;
2571 case SVPD_ID_TYPE_EUI64:
2572 sbuf_printf(sb, "eui.");
2573 break;
2574 case SVPD_ID_TYPE_NAA:
2575 sbuf_printf(sb, "naa.");
2576 break;
2577 case SVPD_ID_TYPE_SCSI_NAME:
2578 break;
2579 }
2580 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2581 case SVPD_ID_CODESET_BINARY:
2582 for (i = 0; i < desc->length; i++)
2583 sbuf_printf(sb, "%02x", desc->identifier[i]);
2584 break;
2585 case SVPD_ID_CODESET_ASCII:
2586 sbuf_printf(sb, "%.*s", (int)desc->length,
2587 (char *)desc->identifier);
2588 break;
2589 case SVPD_ID_CODESET_UTF8:
2590 sbuf_printf(sb, "%s", (char *)desc->identifier);
2591 break;
2592 }
2593 }
2594
2595 static int
ctl_ioctl(struct cdev * dev,u_long cmd,caddr_t addr,int flag,struct thread * td)2596 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2597 struct thread *td)
2598 {
2599 struct ctl_softc *softc = dev->si_drv1;
2600 struct ctl_port *port;
2601 struct ctl_lun *lun;
2602 int retval;
2603
2604 retval = 0;
2605
2606 switch (cmd) {
2607 case CTL_IO:
2608 retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2609 break;
2610 case CTL_ENABLE_PORT:
2611 case CTL_DISABLE_PORT:
2612 case CTL_SET_PORT_WWNS: {
2613 struct ctl_port *port;
2614 struct ctl_port_entry *entry;
2615
2616 entry = (struct ctl_port_entry *)addr;
2617
2618 mtx_lock(&softc->ctl_lock);
2619 STAILQ_FOREACH(port, &softc->port_list, links) {
2620 int action, done;
2621
2622 if (port->targ_port < softc->port_min ||
2623 port->targ_port >= softc->port_max)
2624 continue;
2625
2626 action = 0;
2627 done = 0;
2628 if ((entry->port_type == CTL_PORT_NONE)
2629 && (entry->targ_port == port->targ_port)) {
2630 /*
2631 * If the user only wants to enable or
2632 * disable or set WWNs on a specific port,
2633 * do the operation and we're done.
2634 */
2635 action = 1;
2636 done = 1;
2637 } else if (entry->port_type & port->port_type) {
2638 /*
2639 * Compare the user's type mask with the
2640 * particular frontend type to see if we
2641 * have a match.
2642 */
2643 action = 1;
2644 done = 0;
2645
2646 /*
2647 * Make sure the user isn't trying to set
2648 * WWNs on multiple ports at the same time.
2649 */
2650 if (cmd == CTL_SET_PORT_WWNS) {
2651 printf("%s: Can't set WWNs on "
2652 "multiple ports\n", __func__);
2653 retval = EINVAL;
2654 break;
2655 }
2656 }
2657 if (action == 0)
2658 continue;
2659
2660 /*
2661 * XXX KDM we have to drop the lock here, because
2662 * the online/offline operations can potentially
2663 * block. We need to reference count the frontends
2664 * so they can't go away,
2665 */
2666 if (cmd == CTL_ENABLE_PORT) {
2667 mtx_unlock(&softc->ctl_lock);
2668 ctl_port_online(port);
2669 mtx_lock(&softc->ctl_lock);
2670 } else if (cmd == CTL_DISABLE_PORT) {
2671 mtx_unlock(&softc->ctl_lock);
2672 ctl_port_offline(port);
2673 mtx_lock(&softc->ctl_lock);
2674 } else if (cmd == CTL_SET_PORT_WWNS) {
2675 ctl_port_set_wwns(port,
2676 (entry->flags & CTL_PORT_WWNN_VALID) ?
2677 1 : 0, entry->wwnn,
2678 (entry->flags & CTL_PORT_WWPN_VALID) ?
2679 1 : 0, entry->wwpn);
2680 }
2681 if (done != 0)
2682 break;
2683 }
2684 mtx_unlock(&softc->ctl_lock);
2685 break;
2686 }
2687 case CTL_GET_OOA: {
2688 struct ctl_ooa *ooa_hdr;
2689 struct ctl_ooa_entry *entries;
2690 uint32_t cur_fill_num;
2691
2692 ooa_hdr = (struct ctl_ooa *)addr;
2693
2694 if ((ooa_hdr->alloc_len == 0)
2695 || (ooa_hdr->alloc_num == 0)) {
2696 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2697 "must be non-zero\n", __func__,
2698 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2699 retval = EINVAL;
2700 break;
2701 }
2702
2703 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2704 sizeof(struct ctl_ooa_entry))) {
2705 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2706 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2707 __func__, ooa_hdr->alloc_len,
2708 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2709 retval = EINVAL;
2710 break;
2711 }
2712
2713 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2714
2715 mtx_lock(&softc->ctl_lock);
2716 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 &&
2717 (ooa_hdr->lun_num >= ctl_max_luns ||
2718 softc->ctl_luns[ooa_hdr->lun_num] == NULL)) {
2719 mtx_unlock(&softc->ctl_lock);
2720 free(entries, M_CTL);
2721 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2722 __func__, (uintmax_t)ooa_hdr->lun_num);
2723 retval = EINVAL;
2724 break;
2725 }
2726
2727 cur_fill_num = 0;
2728
2729 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2730 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2731 ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2732 ooa_hdr, entries);
2733 }
2734 } else {
2735 lun = softc->ctl_luns[ooa_hdr->lun_num];
2736 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr,
2737 entries);
2738 }
2739 mtx_unlock(&softc->ctl_lock);
2740
2741 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2742 ooa_hdr->fill_len = ooa_hdr->fill_num *
2743 sizeof(struct ctl_ooa_entry);
2744 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2745 if (retval != 0) {
2746 printf("%s: error copying out %d bytes for OOA dump\n",
2747 __func__, ooa_hdr->fill_len);
2748 }
2749
2750 getbinuptime(&ooa_hdr->cur_bt);
2751
2752 if (cur_fill_num > ooa_hdr->alloc_num) {
2753 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2754 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2755 } else {
2756 ooa_hdr->dropped_num = 0;
2757 ooa_hdr->status = CTL_OOA_OK;
2758 }
2759
2760 free(entries, M_CTL);
2761 break;
2762 }
2763 case CTL_DELAY_IO: {
2764 struct ctl_io_delay_info *delay_info;
2765
2766 delay_info = (struct ctl_io_delay_info *)addr;
2767
2768 #ifdef CTL_IO_DELAY
2769 mtx_lock(&softc->ctl_lock);
2770 if (delay_info->lun_id >= ctl_max_luns ||
2771 (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) {
2772 mtx_unlock(&softc->ctl_lock);
2773 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2774 break;
2775 }
2776 mtx_lock(&lun->lun_lock);
2777 mtx_unlock(&softc->ctl_lock);
2778 delay_info->status = CTL_DELAY_STATUS_OK;
2779 switch (delay_info->delay_type) {
2780 case CTL_DELAY_TYPE_CONT:
2781 case CTL_DELAY_TYPE_ONESHOT:
2782 break;
2783 default:
2784 delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE;
2785 break;
2786 }
2787 switch (delay_info->delay_loc) {
2788 case CTL_DELAY_LOC_DATAMOVE:
2789 lun->delay_info.datamove_type = delay_info->delay_type;
2790 lun->delay_info.datamove_delay = delay_info->delay_secs;
2791 break;
2792 case CTL_DELAY_LOC_DONE:
2793 lun->delay_info.done_type = delay_info->delay_type;
2794 lun->delay_info.done_delay = delay_info->delay_secs;
2795 break;
2796 default:
2797 delay_info->status = CTL_DELAY_STATUS_INVALID_LOC;
2798 break;
2799 }
2800 mtx_unlock(&lun->lun_lock);
2801 #else
2802 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2803 #endif /* CTL_IO_DELAY */
2804 break;
2805 }
2806 case CTL_ERROR_INJECT: {
2807 struct ctl_error_desc *err_desc, *new_err_desc;
2808
2809 err_desc = (struct ctl_error_desc *)addr;
2810
2811 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2812 M_WAITOK | M_ZERO);
2813 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2814
2815 mtx_lock(&softc->ctl_lock);
2816 if (err_desc->lun_id >= ctl_max_luns ||
2817 (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) {
2818 mtx_unlock(&softc->ctl_lock);
2819 free(new_err_desc, M_CTL);
2820 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2821 __func__, (uintmax_t)err_desc->lun_id);
2822 retval = EINVAL;
2823 break;
2824 }
2825 mtx_lock(&lun->lun_lock);
2826 mtx_unlock(&softc->ctl_lock);
2827
2828 /*
2829 * We could do some checking here to verify the validity
2830 * of the request, but given the complexity of error
2831 * injection requests, the checking logic would be fairly
2832 * complex.
2833 *
2834 * For now, if the request is invalid, it just won't get
2835 * executed and might get deleted.
2836 */
2837 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2838
2839 /*
2840 * XXX KDM check to make sure the serial number is unique,
2841 * in case we somehow manage to wrap. That shouldn't
2842 * happen for a very long time, but it's the right thing to
2843 * do.
2844 */
2845 new_err_desc->serial = lun->error_serial;
2846 err_desc->serial = lun->error_serial;
2847 lun->error_serial++;
2848
2849 mtx_unlock(&lun->lun_lock);
2850 break;
2851 }
2852 case CTL_ERROR_INJECT_DELETE: {
2853 struct ctl_error_desc *delete_desc, *desc, *desc2;
2854 int delete_done;
2855
2856 delete_desc = (struct ctl_error_desc *)addr;
2857 delete_done = 0;
2858
2859 mtx_lock(&softc->ctl_lock);
2860 if (delete_desc->lun_id >= ctl_max_luns ||
2861 (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) {
2862 mtx_unlock(&softc->ctl_lock);
2863 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2864 __func__, (uintmax_t)delete_desc->lun_id);
2865 retval = EINVAL;
2866 break;
2867 }
2868 mtx_lock(&lun->lun_lock);
2869 mtx_unlock(&softc->ctl_lock);
2870 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2871 if (desc->serial != delete_desc->serial)
2872 continue;
2873
2874 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2875 links);
2876 free(desc, M_CTL);
2877 delete_done = 1;
2878 }
2879 mtx_unlock(&lun->lun_lock);
2880 if (delete_done == 0) {
2881 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2882 "error serial %ju on LUN %u\n", __func__,
2883 delete_desc->serial, delete_desc->lun_id);
2884 retval = EINVAL;
2885 break;
2886 }
2887 break;
2888 }
2889 case CTL_DUMP_STRUCTS: {
2890 int j, k;
2891 struct ctl_port *port;
2892 struct ctl_frontend *fe;
2893
2894 mtx_lock(&softc->ctl_lock);
2895 printf("CTL Persistent Reservation information start:\n");
2896 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2897 mtx_lock(&lun->lun_lock);
2898 if ((lun->flags & CTL_LUN_DISABLED) != 0) {
2899 mtx_unlock(&lun->lun_lock);
2900 continue;
2901 }
2902
2903 for (j = 0; j < ctl_max_ports; j++) {
2904 if (lun->pr_keys[j] == NULL)
2905 continue;
2906 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2907 if (lun->pr_keys[j][k] == 0)
2908 continue;
2909 printf(" LUN %ju port %d iid %d key "
2910 "%#jx\n", lun->lun, j, k,
2911 (uintmax_t)lun->pr_keys[j][k]);
2912 }
2913 }
2914 mtx_unlock(&lun->lun_lock);
2915 }
2916 printf("CTL Persistent Reservation information end\n");
2917 printf("CTL Ports:\n");
2918 STAILQ_FOREACH(port, &softc->port_list, links) {
2919 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2920 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
2921 port->frontend->name, port->port_type,
2922 port->physical_port, port->virtual_port,
2923 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2924 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2925 if (port->wwpn_iid[j].in_use == 0 &&
2926 port->wwpn_iid[j].wwpn == 0 &&
2927 port->wwpn_iid[j].name == NULL)
2928 continue;
2929
2930 printf(" iid %u use %d WWPN %#jx '%s'\n",
2931 j, port->wwpn_iid[j].in_use,
2932 (uintmax_t)port->wwpn_iid[j].wwpn,
2933 port->wwpn_iid[j].name);
2934 }
2935 }
2936 printf("CTL Port information end\n");
2937 mtx_unlock(&softc->ctl_lock);
2938 /*
2939 * XXX KDM calling this without a lock. We'd likely want
2940 * to drop the lock before calling the frontend's dump
2941 * routine anyway.
2942 */
2943 printf("CTL Frontends:\n");
2944 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2945 printf(" Frontend '%s'\n", fe->name);
2946 if (fe->fe_dump != NULL)
2947 fe->fe_dump();
2948 }
2949 printf("CTL Frontend information end\n");
2950 break;
2951 }
2952 case CTL_LUN_REQ: {
2953 struct ctl_lun_req *lun_req;
2954 struct ctl_backend_driver *backend;
2955 void *packed;
2956 nvlist_t *tmp_args_nvl;
2957 size_t packed_len;
2958
2959 lun_req = (struct ctl_lun_req *)addr;
2960 tmp_args_nvl = lun_req->args_nvl;
2961
2962 backend = ctl_backend_find(lun_req->backend);
2963 if (backend == NULL) {
2964 lun_req->status = CTL_LUN_ERROR;
2965 snprintf(lun_req->error_str,
2966 sizeof(lun_req->error_str),
2967 "Backend \"%s\" not found.",
2968 lun_req->backend);
2969 break;
2970 }
2971
2972 if (lun_req->args != NULL) {
2973 if (lun_req->args_len > CTL_MAX_ARGS_LEN) {
2974 lun_req->status = CTL_LUN_ERROR;
2975 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
2976 "Too big args.");
2977 break;
2978 }
2979 packed = malloc(lun_req->args_len, M_CTL, M_WAITOK);
2980 if (copyin(lun_req->args, packed, lun_req->args_len) != 0) {
2981 free(packed, M_CTL);
2982 lun_req->status = CTL_LUN_ERROR;
2983 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
2984 "Cannot copyin args.");
2985 break;
2986 }
2987 lun_req->args_nvl = nvlist_unpack(packed,
2988 lun_req->args_len, 0);
2989 free(packed, M_CTL);
2990
2991 if (lun_req->args_nvl == NULL) {
2992 lun_req->status = CTL_LUN_ERROR;
2993 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
2994 "Cannot unpack args nvlist.");
2995 break;
2996 }
2997 } else
2998 lun_req->args_nvl = nvlist_create(0);
2999
3000 lun_req->result_nvl = NULL;
3001 retval = backend->ioctl(dev, cmd, addr, flag, td);
3002 nvlist_destroy(lun_req->args_nvl);
3003 lun_req->args_nvl = tmp_args_nvl;
3004
3005 if (lun_req->result_nvl != NULL) {
3006 if (lun_req->result != NULL) {
3007 packed = nvlist_pack(lun_req->result_nvl,
3008 &packed_len);
3009 if (packed == NULL) {
3010 lun_req->status = CTL_LUN_ERROR;
3011 snprintf(lun_req->error_str,
3012 sizeof(lun_req->error_str),
3013 "Cannot pack result nvlist.");
3014 break;
3015 }
3016
3017 if (packed_len > lun_req->result_len) {
3018 lun_req->status = CTL_LUN_ERROR;
3019 snprintf(lun_req->error_str,
3020 sizeof(lun_req->error_str),
3021 "Result nvlist too large.");
3022 free(packed, M_NVLIST);
3023 break;
3024 }
3025
3026 if (copyout(packed, lun_req->result, packed_len)) {
3027 lun_req->status = CTL_LUN_ERROR;
3028 snprintf(lun_req->error_str,
3029 sizeof(lun_req->error_str),
3030 "Cannot copyout() the result.");
3031 free(packed, M_NVLIST);
3032 break;
3033 }
3034
3035 lun_req->result_len = packed_len;
3036 free(packed, M_NVLIST);
3037 }
3038
3039 nvlist_destroy(lun_req->result_nvl);
3040 }
3041 break;
3042 }
3043 case CTL_LUN_LIST: {
3044 struct sbuf *sb;
3045 struct ctl_lun_list *list;
3046 const char *name, *value;
3047 void *cookie;
3048 int type;
3049
3050 list = (struct ctl_lun_list *)addr;
3051
3052 /*
3053 * Allocate a fixed length sbuf here, based on the length
3054 * of the user's buffer. We could allocate an auto-extending
3055 * buffer, and then tell the user how much larger our
3056 * amount of data is than his buffer, but that presents
3057 * some problems:
3058 *
3059 * 1. The sbuf(9) routines use a blocking malloc, and so
3060 * we can't hold a lock while calling them with an
3061 * auto-extending buffer.
3062 *
3063 * 2. There is not currently a LUN reference counting
3064 * mechanism, outside of outstanding transactions on
3065 * the LUN's OOA queue. So a LUN could go away on us
3066 * while we're getting the LUN number, backend-specific
3067 * information, etc. Thus, given the way things
3068 * currently work, we need to hold the CTL lock while
3069 * grabbing LUN information.
3070 *
3071 * So, from the user's standpoint, the best thing to do is
3072 * allocate what he thinks is a reasonable buffer length,
3073 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3074 * double the buffer length and try again. (And repeat
3075 * that until he succeeds.)
3076 */
3077 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3078 if (sb == NULL) {
3079 list->status = CTL_LUN_LIST_ERROR;
3080 snprintf(list->error_str, sizeof(list->error_str),
3081 "Unable to allocate %d bytes for LUN list",
3082 list->alloc_len);
3083 break;
3084 }
3085
3086 sbuf_printf(sb, "<ctllunlist>\n");
3087
3088 mtx_lock(&softc->ctl_lock);
3089 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3090 mtx_lock(&lun->lun_lock);
3091 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3092 (uintmax_t)lun->lun);
3093
3094 /*
3095 * Bail out as soon as we see that we've overfilled
3096 * the buffer.
3097 */
3098 if (retval != 0)
3099 break;
3100
3101 retval = sbuf_printf(sb, "\t<backend_type>%s"
3102 "</backend_type>\n",
3103 (lun->backend == NULL) ? "none" :
3104 lun->backend->name);
3105
3106 if (retval != 0)
3107 break;
3108
3109 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3110 lun->be_lun->lun_type);
3111
3112 if (retval != 0)
3113 break;
3114
3115 if (lun->backend == NULL) {
3116 retval = sbuf_printf(sb, "</lun>\n");
3117 if (retval != 0)
3118 break;
3119 continue;
3120 }
3121
3122 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3123 (lun->be_lun->maxlba > 0) ?
3124 lun->be_lun->maxlba + 1 : 0);
3125
3126 if (retval != 0)
3127 break;
3128
3129 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3130 lun->be_lun->blocksize);
3131
3132 if (retval != 0)
3133 break;
3134
3135 retval = sbuf_printf(sb, "\t<serial_number>");
3136
3137 if (retval != 0)
3138 break;
3139
3140 retval = ctl_sbuf_printf_esc(sb,
3141 lun->be_lun->serial_num,
3142 sizeof(lun->be_lun->serial_num));
3143
3144 if (retval != 0)
3145 break;
3146
3147 retval = sbuf_printf(sb, "</serial_number>\n");
3148
3149 if (retval != 0)
3150 break;
3151
3152 retval = sbuf_printf(sb, "\t<device_id>");
3153
3154 if (retval != 0)
3155 break;
3156
3157 retval = ctl_sbuf_printf_esc(sb,
3158 lun->be_lun->device_id,
3159 sizeof(lun->be_lun->device_id));
3160
3161 if (retval != 0)
3162 break;
3163
3164 retval = sbuf_printf(sb, "</device_id>\n");
3165
3166 if (retval != 0)
3167 break;
3168
3169 if (lun->backend->lun_info != NULL) {
3170 retval = lun->backend->lun_info(lun->be_lun, sb);
3171 if (retval != 0)
3172 break;
3173 }
3174
3175 cookie = NULL;
3176 while ((name = nvlist_next(lun->be_lun->options, &type,
3177 &cookie)) != NULL) {
3178 sbuf_printf(sb, "\t<%s>", name);
3179
3180 if (type == NV_TYPE_STRING) {
3181 value = dnvlist_get_string(
3182 lun->be_lun->options, name, NULL);
3183 if (value != NULL)
3184 sbuf_printf(sb, "%s", value);
3185 }
3186
3187 sbuf_printf(sb, "</%s>\n", name);
3188 }
3189
3190 retval = sbuf_printf(sb, "</lun>\n");
3191
3192 if (retval != 0)
3193 break;
3194 mtx_unlock(&lun->lun_lock);
3195 }
3196 if (lun != NULL)
3197 mtx_unlock(&lun->lun_lock);
3198 mtx_unlock(&softc->ctl_lock);
3199
3200 if ((retval != 0)
3201 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3202 retval = 0;
3203 sbuf_delete(sb);
3204 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3205 snprintf(list->error_str, sizeof(list->error_str),
3206 "Out of space, %d bytes is too small",
3207 list->alloc_len);
3208 break;
3209 }
3210
3211 sbuf_finish(sb);
3212
3213 retval = copyout(sbuf_data(sb), list->lun_xml,
3214 sbuf_len(sb) + 1);
3215
3216 list->fill_len = sbuf_len(sb) + 1;
3217 list->status = CTL_LUN_LIST_OK;
3218 sbuf_delete(sb);
3219 break;
3220 }
3221 case CTL_ISCSI: {
3222 struct ctl_iscsi *ci;
3223 struct ctl_frontend *fe;
3224
3225 ci = (struct ctl_iscsi *)addr;
3226
3227 fe = ctl_frontend_find("iscsi");
3228 if (fe == NULL) {
3229 ci->status = CTL_ISCSI_ERROR;
3230 snprintf(ci->error_str, sizeof(ci->error_str),
3231 "Frontend \"iscsi\" not found.");
3232 break;
3233 }
3234
3235 retval = fe->ioctl(dev, cmd, addr, flag, td);
3236 break;
3237 }
3238 case CTL_PORT_REQ: {
3239 struct ctl_req *req;
3240 struct ctl_frontend *fe;
3241 void *packed;
3242 nvlist_t *tmp_args_nvl;
3243 size_t packed_len;
3244
3245 req = (struct ctl_req *)addr;
3246 tmp_args_nvl = req->args_nvl;
3247
3248 fe = ctl_frontend_find(req->driver);
3249 if (fe == NULL) {
3250 req->status = CTL_LUN_ERROR;
3251 snprintf(req->error_str, sizeof(req->error_str),
3252 "Frontend \"%s\" not found.", req->driver);
3253 break;
3254 }
3255
3256 if (req->args != NULL) {
3257 if (req->args_len > CTL_MAX_ARGS_LEN) {
3258 req->status = CTL_LUN_ERROR;
3259 snprintf(req->error_str, sizeof(req->error_str),
3260 "Too big args.");
3261 break;
3262 }
3263 packed = malloc(req->args_len, M_CTL, M_WAITOK);
3264 if (copyin(req->args, packed, req->args_len) != 0) {
3265 free(packed, M_CTL);
3266 req->status = CTL_LUN_ERROR;
3267 snprintf(req->error_str, sizeof(req->error_str),
3268 "Cannot copyin args.");
3269 break;
3270 }
3271 req->args_nvl = nvlist_unpack(packed,
3272 req->args_len, 0);
3273 free(packed, M_CTL);
3274
3275 if (req->args_nvl == NULL) {
3276 req->status = CTL_LUN_ERROR;
3277 snprintf(req->error_str, sizeof(req->error_str),
3278 "Cannot unpack args nvlist.");
3279 break;
3280 }
3281 } else
3282 req->args_nvl = nvlist_create(0);
3283
3284 req->result_nvl = NULL;
3285 if (fe->ioctl)
3286 retval = fe->ioctl(dev, cmd, addr, flag, td);
3287 else
3288 retval = ENODEV;
3289
3290 nvlist_destroy(req->args_nvl);
3291 req->args_nvl = tmp_args_nvl;
3292
3293 if (req->result_nvl != NULL) {
3294 if (req->result != NULL) {
3295 packed = nvlist_pack(req->result_nvl,
3296 &packed_len);
3297 if (packed == NULL) {
3298 req->status = CTL_LUN_ERROR;
3299 snprintf(req->error_str,
3300 sizeof(req->error_str),
3301 "Cannot pack result nvlist.");
3302 break;
3303 }
3304
3305 if (packed_len > req->result_len) {
3306 req->status = CTL_LUN_ERROR;
3307 snprintf(req->error_str,
3308 sizeof(req->error_str),
3309 "Result nvlist too large.");
3310 free(packed, M_NVLIST);
3311 break;
3312 }
3313
3314 if (copyout(packed, req->result, packed_len)) {
3315 req->status = CTL_LUN_ERROR;
3316 snprintf(req->error_str,
3317 sizeof(req->error_str),
3318 "Cannot copyout() the result.");
3319 free(packed, M_NVLIST);
3320 break;
3321 }
3322
3323 req->result_len = packed_len;
3324 free(packed, M_NVLIST);
3325 }
3326
3327 nvlist_destroy(req->result_nvl);
3328 }
3329 break;
3330 }
3331 case CTL_PORT_LIST: {
3332 struct sbuf *sb;
3333 struct ctl_port *port;
3334 struct ctl_lun_list *list;
3335 const char *name, *value;
3336 void *cookie;
3337 int j, type;
3338 uint32_t plun;
3339
3340 list = (struct ctl_lun_list *)addr;
3341
3342 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3343 if (sb == NULL) {
3344 list->status = CTL_LUN_LIST_ERROR;
3345 snprintf(list->error_str, sizeof(list->error_str),
3346 "Unable to allocate %d bytes for LUN list",
3347 list->alloc_len);
3348 break;
3349 }
3350
3351 sbuf_printf(sb, "<ctlportlist>\n");
3352
3353 mtx_lock(&softc->ctl_lock);
3354 STAILQ_FOREACH(port, &softc->port_list, links) {
3355 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3356 (uintmax_t)port->targ_port);
3357
3358 /*
3359 * Bail out as soon as we see that we've overfilled
3360 * the buffer.
3361 */
3362 if (retval != 0)
3363 break;
3364
3365 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3366 "</frontend_type>\n", port->frontend->name);
3367 if (retval != 0)
3368 break;
3369
3370 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3371 port->port_type);
3372 if (retval != 0)
3373 break;
3374
3375 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3376 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3377 if (retval != 0)
3378 break;
3379
3380 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3381 port->port_name);
3382 if (retval != 0)
3383 break;
3384
3385 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3386 port->physical_port);
3387 if (retval != 0)
3388 break;
3389
3390 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3391 port->virtual_port);
3392 if (retval != 0)
3393 break;
3394
3395 if (port->target_devid != NULL) {
3396 sbuf_printf(sb, "\t<target>");
3397 ctl_id_sbuf(port->target_devid, sb);
3398 sbuf_printf(sb, "</target>\n");
3399 }
3400
3401 if (port->port_devid != NULL) {
3402 sbuf_printf(sb, "\t<port>");
3403 ctl_id_sbuf(port->port_devid, sb);
3404 sbuf_printf(sb, "</port>\n");
3405 }
3406
3407 if (port->port_info != NULL) {
3408 retval = port->port_info(port->onoff_arg, sb);
3409 if (retval != 0)
3410 break;
3411 }
3412
3413 cookie = NULL;
3414 while ((name = nvlist_next(port->options, &type,
3415 &cookie)) != NULL) {
3416 sbuf_printf(sb, "\t<%s>", name);
3417
3418 if (type == NV_TYPE_STRING) {
3419 value = dnvlist_get_string(port->options,
3420 name, NULL);
3421 if (value != NULL)
3422 sbuf_printf(sb, "%s", value);
3423 }
3424
3425 sbuf_printf(sb, "</%s>\n", name);
3426 }
3427
3428 if (port->lun_map != NULL) {
3429 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3430 for (j = 0; j < port->lun_map_size; j++) {
3431 plun = ctl_lun_map_from_port(port, j);
3432 if (plun == UINT32_MAX)
3433 continue;
3434 sbuf_printf(sb,
3435 "\t<lun id=\"%u\">%u</lun>\n",
3436 j, plun);
3437 }
3438 }
3439
3440 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3441 if (port->wwpn_iid[j].in_use == 0 ||
3442 (port->wwpn_iid[j].wwpn == 0 &&
3443 port->wwpn_iid[j].name == NULL))
3444 continue;
3445
3446 if (port->wwpn_iid[j].name != NULL)
3447 retval = sbuf_printf(sb,
3448 "\t<initiator id=\"%u\">%s</initiator>\n",
3449 j, port->wwpn_iid[j].name);
3450 else
3451 retval = sbuf_printf(sb,
3452 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3453 j, port->wwpn_iid[j].wwpn);
3454 if (retval != 0)
3455 break;
3456 }
3457 if (retval != 0)
3458 break;
3459
3460 retval = sbuf_printf(sb, "</targ_port>\n");
3461 if (retval != 0)
3462 break;
3463 }
3464 mtx_unlock(&softc->ctl_lock);
3465
3466 if ((retval != 0)
3467 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3468 retval = 0;
3469 sbuf_delete(sb);
3470 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3471 snprintf(list->error_str, sizeof(list->error_str),
3472 "Out of space, %d bytes is too small",
3473 list->alloc_len);
3474 break;
3475 }
3476
3477 sbuf_finish(sb);
3478
3479 retval = copyout(sbuf_data(sb), list->lun_xml,
3480 sbuf_len(sb) + 1);
3481
3482 list->fill_len = sbuf_len(sb) + 1;
3483 list->status = CTL_LUN_LIST_OK;
3484 sbuf_delete(sb);
3485 break;
3486 }
3487 case CTL_LUN_MAP: {
3488 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3489 struct ctl_port *port;
3490
3491 mtx_lock(&softc->ctl_lock);
3492 if (lm->port < softc->port_min ||
3493 lm->port >= softc->port_max ||
3494 (port = softc->ctl_ports[lm->port]) == NULL) {
3495 mtx_unlock(&softc->ctl_lock);
3496 return (ENXIO);
3497 }
3498 if (port->status & CTL_PORT_STATUS_ONLINE) {
3499 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3500 if (ctl_lun_map_to_port(port, lun->lun) ==
3501 UINT32_MAX)
3502 continue;
3503 mtx_lock(&lun->lun_lock);
3504 ctl_est_ua_port(lun, lm->port, -1,
3505 CTL_UA_LUN_CHANGE);
3506 mtx_unlock(&lun->lun_lock);
3507 }
3508 }
3509 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3510 if (lm->plun != UINT32_MAX) {
3511 if (lm->lun == UINT32_MAX)
3512 retval = ctl_lun_map_unset(port, lm->plun);
3513 else if (lm->lun < ctl_max_luns &&
3514 softc->ctl_luns[lm->lun] != NULL)
3515 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3516 else
3517 return (ENXIO);
3518 } else {
3519 if (lm->lun == UINT32_MAX)
3520 retval = ctl_lun_map_deinit(port);
3521 else
3522 retval = ctl_lun_map_init(port);
3523 }
3524 if (port->status & CTL_PORT_STATUS_ONLINE)
3525 ctl_isc_announce_port(port);
3526 break;
3527 }
3528 case CTL_GET_LUN_STATS: {
3529 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3530 int i;
3531
3532 /*
3533 * XXX KDM no locking here. If the LUN list changes,
3534 * things can blow up.
3535 */
3536 i = 0;
3537 stats->status = CTL_SS_OK;
3538 stats->fill_len = 0;
3539 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3540 if (lun->lun < stats->first_item)
3541 continue;
3542 if (stats->fill_len + sizeof(lun->stats) >
3543 stats->alloc_len) {
3544 stats->status = CTL_SS_NEED_MORE_SPACE;
3545 break;
3546 }
3547 retval = copyout(&lun->stats, &stats->stats[i++],
3548 sizeof(lun->stats));
3549 if (retval != 0)
3550 break;
3551 stats->fill_len += sizeof(lun->stats);
3552 }
3553 stats->num_items = softc->num_luns;
3554 stats->flags = CTL_STATS_FLAG_NONE;
3555 #ifdef CTL_TIME_IO
3556 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3557 #endif
3558 getnanouptime(&stats->timestamp);
3559 break;
3560 }
3561 case CTL_GET_PORT_STATS: {
3562 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3563 int i;
3564
3565 /*
3566 * XXX KDM no locking here. If the LUN list changes,
3567 * things can blow up.
3568 */
3569 i = 0;
3570 stats->status = CTL_SS_OK;
3571 stats->fill_len = 0;
3572 STAILQ_FOREACH(port, &softc->port_list, links) {
3573 if (port->targ_port < stats->first_item)
3574 continue;
3575 if (stats->fill_len + sizeof(port->stats) >
3576 stats->alloc_len) {
3577 stats->status = CTL_SS_NEED_MORE_SPACE;
3578 break;
3579 }
3580 retval = copyout(&port->stats, &stats->stats[i++],
3581 sizeof(port->stats));
3582 if (retval != 0)
3583 break;
3584 stats->fill_len += sizeof(port->stats);
3585 }
3586 stats->num_items = softc->num_ports;
3587 stats->flags = CTL_STATS_FLAG_NONE;
3588 #ifdef CTL_TIME_IO
3589 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3590 #endif
3591 getnanouptime(&stats->timestamp);
3592 break;
3593 }
3594 default: {
3595 /* XXX KDM should we fix this? */
3596 #if 0
3597 struct ctl_backend_driver *backend;
3598 unsigned int type;
3599 int found;
3600
3601 found = 0;
3602
3603 /*
3604 * We encode the backend type as the ioctl type for backend
3605 * ioctls. So parse it out here, and then search for a
3606 * backend of this type.
3607 */
3608 type = _IOC_TYPE(cmd);
3609
3610 STAILQ_FOREACH(backend, &softc->be_list, links) {
3611 if (backend->type == type) {
3612 found = 1;
3613 break;
3614 }
3615 }
3616 if (found == 0) {
3617 printf("ctl: unknown ioctl command %#lx or backend "
3618 "%d\n", cmd, type);
3619 retval = EINVAL;
3620 break;
3621 }
3622 retval = backend->ioctl(dev, cmd, addr, flag, td);
3623 #endif
3624 retval = ENOTTY;
3625 break;
3626 }
3627 }
3628 return (retval);
3629 }
3630
3631 uint32_t
ctl_get_initindex(struct ctl_nexus * nexus)3632 ctl_get_initindex(struct ctl_nexus *nexus)
3633 {
3634 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3635 }
3636
3637 int
ctl_lun_map_init(struct ctl_port * port)3638 ctl_lun_map_init(struct ctl_port *port)
3639 {
3640 struct ctl_softc *softc = port->ctl_softc;
3641 struct ctl_lun *lun;
3642 int size = ctl_lun_map_size;
3643 uint32_t i;
3644
3645 if (port->lun_map == NULL || port->lun_map_size < size) {
3646 port->lun_map_size = 0;
3647 free(port->lun_map, M_CTL);
3648 port->lun_map = malloc(size * sizeof(uint32_t),
3649 M_CTL, M_NOWAIT);
3650 }
3651 if (port->lun_map == NULL)
3652 return (ENOMEM);
3653 for (i = 0; i < size; i++)
3654 port->lun_map[i] = UINT32_MAX;
3655 port->lun_map_size = size;
3656 if (port->status & CTL_PORT_STATUS_ONLINE) {
3657 if (port->lun_disable != NULL) {
3658 STAILQ_FOREACH(lun, &softc->lun_list, links)
3659 port->lun_disable(port->targ_lun_arg, lun->lun);
3660 }
3661 ctl_isc_announce_port(port);
3662 }
3663 return (0);
3664 }
3665
3666 int
ctl_lun_map_deinit(struct ctl_port * port)3667 ctl_lun_map_deinit(struct ctl_port *port)
3668 {
3669 struct ctl_softc *softc = port->ctl_softc;
3670 struct ctl_lun *lun;
3671
3672 if (port->lun_map == NULL)
3673 return (0);
3674 port->lun_map_size = 0;
3675 free(port->lun_map, M_CTL);
3676 port->lun_map = NULL;
3677 if (port->status & CTL_PORT_STATUS_ONLINE) {
3678 if (port->lun_enable != NULL) {
3679 STAILQ_FOREACH(lun, &softc->lun_list, links)
3680 port->lun_enable(port->targ_lun_arg, lun->lun);
3681 }
3682 ctl_isc_announce_port(port);
3683 }
3684 return (0);
3685 }
3686
3687 int
ctl_lun_map_set(struct ctl_port * port,uint32_t plun,uint32_t glun)3688 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3689 {
3690 int status;
3691 uint32_t old;
3692
3693 if (port->lun_map == NULL) {
3694 status = ctl_lun_map_init(port);
3695 if (status != 0)
3696 return (status);
3697 }
3698 if (plun >= port->lun_map_size)
3699 return (EINVAL);
3700 old = port->lun_map[plun];
3701 port->lun_map[plun] = glun;
3702 if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) {
3703 if (port->lun_enable != NULL)
3704 port->lun_enable(port->targ_lun_arg, plun);
3705 ctl_isc_announce_port(port);
3706 }
3707 return (0);
3708 }
3709
3710 int
ctl_lun_map_unset(struct ctl_port * port,uint32_t plun)3711 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3712 {
3713 uint32_t old;
3714
3715 if (port->lun_map == NULL || plun >= port->lun_map_size)
3716 return (0);
3717 old = port->lun_map[plun];
3718 port->lun_map[plun] = UINT32_MAX;
3719 if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) {
3720 if (port->lun_disable != NULL)
3721 port->lun_disable(port->targ_lun_arg, plun);
3722 ctl_isc_announce_port(port);
3723 }
3724 return (0);
3725 }
3726
3727 uint32_t
ctl_lun_map_from_port(struct ctl_port * port,uint32_t lun_id)3728 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3729 {
3730
3731 if (port == NULL)
3732 return (UINT32_MAX);
3733 if (port->lun_map == NULL)
3734 return (lun_id);
3735 if (lun_id > port->lun_map_size)
3736 return (UINT32_MAX);
3737 return (port->lun_map[lun_id]);
3738 }
3739
3740 uint32_t
ctl_lun_map_to_port(struct ctl_port * port,uint32_t lun_id)3741 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3742 {
3743 uint32_t i;
3744
3745 if (port == NULL)
3746 return (UINT32_MAX);
3747 if (port->lun_map == NULL)
3748 return (lun_id);
3749 for (i = 0; i < port->lun_map_size; i++) {
3750 if (port->lun_map[i] == lun_id)
3751 return (i);
3752 }
3753 return (UINT32_MAX);
3754 }
3755
3756 uint32_t
ctl_decode_lun(uint64_t encoded)3757 ctl_decode_lun(uint64_t encoded)
3758 {
3759 uint8_t lun[8];
3760 uint32_t result = 0xffffffff;
3761
3762 be64enc(lun, encoded);
3763 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) {
3764 case RPL_LUNDATA_ATYP_PERIPH:
3765 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 &&
3766 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0)
3767 result = lun[1];
3768 break;
3769 case RPL_LUNDATA_ATYP_FLAT:
3770 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 &&
3771 lun[6] == 0 && lun[7] == 0)
3772 result = ((lun[0] & 0x3f) << 8) + lun[1];
3773 break;
3774 case RPL_LUNDATA_ATYP_EXTLUN:
3775 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) {
3776 case 0x02:
3777 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) {
3778 case 0x00:
3779 result = lun[1];
3780 break;
3781 case 0x10:
3782 result = (lun[1] << 16) + (lun[2] << 8) +
3783 lun[3];
3784 break;
3785 case 0x20:
3786 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0)
3787 result = (lun[2] << 24) +
3788 (lun[3] << 16) + (lun[4] << 8) +
3789 lun[5];
3790 break;
3791 }
3792 break;
3793 case RPL_LUNDATA_EXT_EAM_NOT_SPEC:
3794 result = 0xffffffff;
3795 break;
3796 }
3797 break;
3798 }
3799 return (result);
3800 }
3801
3802 uint64_t
ctl_encode_lun(uint32_t decoded)3803 ctl_encode_lun(uint32_t decoded)
3804 {
3805 uint64_t l = decoded;
3806
3807 if (l <= 0xff)
3808 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48));
3809 if (l <= 0x3fff)
3810 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48));
3811 if (l <= 0xffffff)
3812 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) |
3813 (l << 32));
3814 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16));
3815 }
3816
3817 int
ctl_ffz(uint32_t * mask,uint32_t first,uint32_t last)3818 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3819 {
3820 int i;
3821
3822 for (i = first; i < last; i++) {
3823 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3824 return (i);
3825 }
3826 return (-1);
3827 }
3828
3829 int
ctl_set_mask(uint32_t * mask,uint32_t bit)3830 ctl_set_mask(uint32_t *mask, uint32_t bit)
3831 {
3832 uint32_t chunk, piece;
3833
3834 chunk = bit >> 5;
3835 piece = bit % (sizeof(uint32_t) * 8);
3836
3837 if ((mask[chunk] & (1 << piece)) != 0)
3838 return (-1);
3839 else
3840 mask[chunk] |= (1 << piece);
3841
3842 return (0);
3843 }
3844
3845 int
ctl_clear_mask(uint32_t * mask,uint32_t bit)3846 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3847 {
3848 uint32_t chunk, piece;
3849
3850 chunk = bit >> 5;
3851 piece = bit % (sizeof(uint32_t) * 8);
3852
3853 if ((mask[chunk] & (1 << piece)) == 0)
3854 return (-1);
3855 else
3856 mask[chunk] &= ~(1 << piece);
3857
3858 return (0);
3859 }
3860
3861 int
ctl_is_set(uint32_t * mask,uint32_t bit)3862 ctl_is_set(uint32_t *mask, uint32_t bit)
3863 {
3864 uint32_t chunk, piece;
3865
3866 chunk = bit >> 5;
3867 piece = bit % (sizeof(uint32_t) * 8);
3868
3869 if ((mask[chunk] & (1 << piece)) == 0)
3870 return (0);
3871 else
3872 return (1);
3873 }
3874
3875 static uint64_t
ctl_get_prkey(struct ctl_lun * lun,uint32_t residx)3876 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3877 {
3878 uint64_t *t;
3879
3880 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3881 if (t == NULL)
3882 return (0);
3883 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3884 }
3885
3886 static void
ctl_clr_prkey(struct ctl_lun * lun,uint32_t residx)3887 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3888 {
3889 uint64_t *t;
3890
3891 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3892 if (t == NULL)
3893 return;
3894 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3895 }
3896
3897 static void
ctl_alloc_prkey(struct ctl_lun * lun,uint32_t residx)3898 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3899 {
3900 uint64_t *p;
3901 u_int i;
3902
3903 i = residx/CTL_MAX_INIT_PER_PORT;
3904 if (lun->pr_keys[i] != NULL)
3905 return;
3906 mtx_unlock(&lun->lun_lock);
3907 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3908 M_WAITOK | M_ZERO);
3909 mtx_lock(&lun->lun_lock);
3910 if (lun->pr_keys[i] == NULL)
3911 lun->pr_keys[i] = p;
3912 else
3913 free(p, M_CTL);
3914 }
3915
3916 static void
ctl_set_prkey(struct ctl_lun * lun,uint32_t residx,uint64_t key)3917 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3918 {
3919 uint64_t *t;
3920
3921 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3922 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3923 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3924 }
3925
3926 /*
3927 * ctl_softc, pool_name, total_ctl_io are passed in.
3928 * npool is passed out.
3929 */
3930 int
ctl_pool_create(struct ctl_softc * ctl_softc,const char * pool_name,uint32_t total_ctl_io,void ** npool)3931 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3932 uint32_t total_ctl_io, void **npool)
3933 {
3934 struct ctl_io_pool *pool;
3935
3936 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3937 M_NOWAIT | M_ZERO);
3938 if (pool == NULL)
3939 return (ENOMEM);
3940
3941 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3942 pool->ctl_softc = ctl_softc;
3943 #ifdef IO_POOLS
3944 pool->zone = uma_zsecond_create(pool->name, NULL,
3945 NULL, NULL, NULL, ctl_softc->io_zone);
3946 /* uma_prealloc(pool->zone, total_ctl_io); */
3947 #else
3948 pool->zone = ctl_softc->io_zone;
3949 #endif
3950
3951 *npool = pool;
3952 return (0);
3953 }
3954
3955 void
ctl_pool_free(struct ctl_io_pool * pool)3956 ctl_pool_free(struct ctl_io_pool *pool)
3957 {
3958
3959 if (pool == NULL)
3960 return;
3961
3962 #ifdef IO_POOLS
3963 uma_zdestroy(pool->zone);
3964 #endif
3965 free(pool, M_CTL);
3966 }
3967
3968 union ctl_io *
ctl_alloc_io(void * pool_ref)3969 ctl_alloc_io(void *pool_ref)
3970 {
3971 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3972 union ctl_io *io;
3973
3974 io = uma_zalloc(pool->zone, M_WAITOK);
3975 if (io != NULL) {
3976 io->io_hdr.pool = pool_ref;
3977 CTL_SOFTC(io) = pool->ctl_softc;
3978 TAILQ_INIT(&io->io_hdr.blocked_queue);
3979 }
3980 return (io);
3981 }
3982
3983 union ctl_io *
ctl_alloc_io_nowait(void * pool_ref)3984 ctl_alloc_io_nowait(void *pool_ref)
3985 {
3986 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3987 union ctl_io *io;
3988
3989 io = uma_zalloc(pool->zone, M_NOWAIT);
3990 if (io != NULL) {
3991 io->io_hdr.pool = pool_ref;
3992 CTL_SOFTC(io) = pool->ctl_softc;
3993 TAILQ_INIT(&io->io_hdr.blocked_queue);
3994 }
3995 return (io);
3996 }
3997
3998 void
ctl_free_io(union ctl_io * io)3999 ctl_free_io(union ctl_io *io)
4000 {
4001 struct ctl_io_pool *pool;
4002
4003 if (io == NULL)
4004 return;
4005
4006 pool = (struct ctl_io_pool *)io->io_hdr.pool;
4007 uma_zfree(pool->zone, io);
4008 }
4009
4010 void
ctl_zero_io(union ctl_io * io)4011 ctl_zero_io(union ctl_io *io)
4012 {
4013 struct ctl_io_pool *pool;
4014
4015 if (io == NULL)
4016 return;
4017
4018 /*
4019 * May need to preserve linked list pointers at some point too.
4020 */
4021 pool = io->io_hdr.pool;
4022 memset(io, 0, sizeof(*io));
4023 io->io_hdr.pool = pool;
4024 CTL_SOFTC(io) = pool->ctl_softc;
4025 TAILQ_INIT(&io->io_hdr.blocked_queue);
4026 }
4027
4028 int
ctl_expand_number(const char * buf,uint64_t * num)4029 ctl_expand_number(const char *buf, uint64_t *num)
4030 {
4031 char *endptr;
4032 uint64_t number;
4033 unsigned shift;
4034
4035 number = strtoq(buf, &endptr, 0);
4036
4037 switch (tolower((unsigned char)*endptr)) {
4038 case 'e':
4039 shift = 60;
4040 break;
4041 case 'p':
4042 shift = 50;
4043 break;
4044 case 't':
4045 shift = 40;
4046 break;
4047 case 'g':
4048 shift = 30;
4049 break;
4050 case 'm':
4051 shift = 20;
4052 break;
4053 case 'k':
4054 shift = 10;
4055 break;
4056 case 'b':
4057 case '\0': /* No unit. */
4058 *num = number;
4059 return (0);
4060 default:
4061 /* Unrecognized unit. */
4062 return (-1);
4063 }
4064
4065 if ((number << shift) >> shift != number) {
4066 /* Overflow */
4067 return (-1);
4068 }
4069 *num = number << shift;
4070 return (0);
4071 }
4072
4073 /*
4074 * This routine could be used in the future to load default and/or saved
4075 * mode page parameters for a particuar lun.
4076 */
4077 static int
ctl_init_page_index(struct ctl_lun * lun)4078 ctl_init_page_index(struct ctl_lun *lun)
4079 {
4080 int i, page_code;
4081 struct ctl_page_index *page_index;
4082 const char *value;
4083 uint64_t ival;
4084
4085 memcpy(&lun->mode_pages.index, page_index_template,
4086 sizeof(page_index_template));
4087
4088 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4089 page_index = &lun->mode_pages.index[i];
4090 if (lun->be_lun->lun_type == T_DIRECT &&
4091 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4092 continue;
4093 if (lun->be_lun->lun_type == T_PROCESSOR &&
4094 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4095 continue;
4096 if (lun->be_lun->lun_type == T_CDROM &&
4097 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4098 continue;
4099
4100 page_code = page_index->page_code & SMPH_PC_MASK;
4101 switch (page_code) {
4102 case SMS_RW_ERROR_RECOVERY_PAGE: {
4103 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4104 ("subpage %#x for page %#x is incorrect!",
4105 page_index->subpage, page_code));
4106 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
4107 &rw_er_page_default,
4108 sizeof(rw_er_page_default));
4109 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
4110 &rw_er_page_changeable,
4111 sizeof(rw_er_page_changeable));
4112 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
4113 &rw_er_page_default,
4114 sizeof(rw_er_page_default));
4115 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
4116 &rw_er_page_default,
4117 sizeof(rw_er_page_default));
4118 page_index->page_data =
4119 (uint8_t *)lun->mode_pages.rw_er_page;
4120 break;
4121 }
4122 case SMS_FORMAT_DEVICE_PAGE: {
4123 struct scsi_format_page *format_page;
4124
4125 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4126 ("subpage %#x for page %#x is incorrect!",
4127 page_index->subpage, page_code));
4128
4129 /*
4130 * Sectors per track are set above. Bytes per
4131 * sector need to be set here on a per-LUN basis.
4132 */
4133 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
4134 &format_page_default,
4135 sizeof(format_page_default));
4136 memcpy(&lun->mode_pages.format_page[
4137 CTL_PAGE_CHANGEABLE], &format_page_changeable,
4138 sizeof(format_page_changeable));
4139 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
4140 &format_page_default,
4141 sizeof(format_page_default));
4142 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
4143 &format_page_default,
4144 sizeof(format_page_default));
4145
4146 format_page = &lun->mode_pages.format_page[
4147 CTL_PAGE_CURRENT];
4148 scsi_ulto2b(lun->be_lun->blocksize,
4149 format_page->bytes_per_sector);
4150
4151 format_page = &lun->mode_pages.format_page[
4152 CTL_PAGE_DEFAULT];
4153 scsi_ulto2b(lun->be_lun->blocksize,
4154 format_page->bytes_per_sector);
4155
4156 format_page = &lun->mode_pages.format_page[
4157 CTL_PAGE_SAVED];
4158 scsi_ulto2b(lun->be_lun->blocksize,
4159 format_page->bytes_per_sector);
4160
4161 page_index->page_data =
4162 (uint8_t *)lun->mode_pages.format_page;
4163 break;
4164 }
4165 case SMS_RIGID_DISK_PAGE: {
4166 struct scsi_rigid_disk_page *rigid_disk_page;
4167 uint32_t sectors_per_cylinder;
4168 uint64_t cylinders;
4169 #ifndef __XSCALE__
4170 int shift;
4171 #endif /* !__XSCALE__ */
4172
4173 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4174 ("subpage %#x for page %#x is incorrect!",
4175 page_index->subpage, page_code));
4176
4177 /*
4178 * Rotation rate and sectors per track are set
4179 * above. We calculate the cylinders here based on
4180 * capacity. Due to the number of heads and
4181 * sectors per track we're using, smaller arrays
4182 * may turn out to have 0 cylinders. Linux and
4183 * FreeBSD don't pay attention to these mode pages
4184 * to figure out capacity, but Solaris does. It
4185 * seems to deal with 0 cylinders just fine, and
4186 * works out a fake geometry based on the capacity.
4187 */
4188 memcpy(&lun->mode_pages.rigid_disk_page[
4189 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4190 sizeof(rigid_disk_page_default));
4191 memcpy(&lun->mode_pages.rigid_disk_page[
4192 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4193 sizeof(rigid_disk_page_changeable));
4194
4195 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4196 CTL_DEFAULT_HEADS;
4197
4198 /*
4199 * The divide method here will be more accurate,
4200 * probably, but results in floating point being
4201 * used in the kernel on i386 (__udivdi3()). On the
4202 * XScale, though, __udivdi3() is implemented in
4203 * software.
4204 *
4205 * The shift method for cylinder calculation is
4206 * accurate if sectors_per_cylinder is a power of
4207 * 2. Otherwise it might be slightly off -- you
4208 * might have a bit of a truncation problem.
4209 */
4210 #ifdef __XSCALE__
4211 cylinders = (lun->be_lun->maxlba + 1) /
4212 sectors_per_cylinder;
4213 #else
4214 for (shift = 31; shift > 0; shift--) {
4215 if (sectors_per_cylinder & (1 << shift))
4216 break;
4217 }
4218 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4219 #endif
4220
4221 /*
4222 * We've basically got 3 bytes, or 24 bits for the
4223 * cylinder size in the mode page. If we're over,
4224 * just round down to 2^24.
4225 */
4226 if (cylinders > 0xffffff)
4227 cylinders = 0xffffff;
4228
4229 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4230 CTL_PAGE_DEFAULT];
4231 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4232
4233 if ((value = dnvlist_get_string(lun->be_lun->options,
4234 "rpm", NULL)) != NULL) {
4235 scsi_ulto2b(strtol(value, NULL, 0),
4236 rigid_disk_page->rotation_rate);
4237 }
4238
4239 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4240 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4241 sizeof(rigid_disk_page_default));
4242 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4243 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4244 sizeof(rigid_disk_page_default));
4245
4246 page_index->page_data =
4247 (uint8_t *)lun->mode_pages.rigid_disk_page;
4248 break;
4249 }
4250 case SMS_VERIFY_ERROR_RECOVERY_PAGE: {
4251 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4252 ("subpage %#x for page %#x is incorrect!",
4253 page_index->subpage, page_code));
4254 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT],
4255 &verify_er_page_default,
4256 sizeof(verify_er_page_default));
4257 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE],
4258 &verify_er_page_changeable,
4259 sizeof(verify_er_page_changeable));
4260 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT],
4261 &verify_er_page_default,
4262 sizeof(verify_er_page_default));
4263 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED],
4264 &verify_er_page_default,
4265 sizeof(verify_er_page_default));
4266 page_index->page_data =
4267 (uint8_t *)lun->mode_pages.verify_er_page;
4268 break;
4269 }
4270 case SMS_CACHING_PAGE: {
4271 struct scsi_caching_page *caching_page;
4272
4273 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4274 ("subpage %#x for page %#x is incorrect!",
4275 page_index->subpage, page_code));
4276 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4277 &caching_page_default,
4278 sizeof(caching_page_default));
4279 memcpy(&lun->mode_pages.caching_page[
4280 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4281 sizeof(caching_page_changeable));
4282 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4283 &caching_page_default,
4284 sizeof(caching_page_default));
4285 caching_page = &lun->mode_pages.caching_page[
4286 CTL_PAGE_SAVED];
4287 value = dnvlist_get_string(lun->be_lun->options,
4288 "writecache", NULL);
4289 if (value != NULL && strcmp(value, "off") == 0)
4290 caching_page->flags1 &= ~SCP_WCE;
4291 value = dnvlist_get_string(lun->be_lun->options,
4292 "readcache", NULL);
4293 if (value != NULL && strcmp(value, "off") == 0)
4294 caching_page->flags1 |= SCP_RCD;
4295 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4296 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4297 sizeof(caching_page_default));
4298 page_index->page_data =
4299 (uint8_t *)lun->mode_pages.caching_page;
4300 break;
4301 }
4302 case SMS_CONTROL_MODE_PAGE: {
4303 switch (page_index->subpage) {
4304 case SMS_SUBPAGE_PAGE_0: {
4305 struct scsi_control_page *control_page;
4306
4307 memcpy(&lun->mode_pages.control_page[
4308 CTL_PAGE_DEFAULT],
4309 &control_page_default,
4310 sizeof(control_page_default));
4311 memcpy(&lun->mode_pages.control_page[
4312 CTL_PAGE_CHANGEABLE],
4313 &control_page_changeable,
4314 sizeof(control_page_changeable));
4315 memcpy(&lun->mode_pages.control_page[
4316 CTL_PAGE_SAVED],
4317 &control_page_default,
4318 sizeof(control_page_default));
4319 control_page = &lun->mode_pages.control_page[
4320 CTL_PAGE_SAVED];
4321 value = dnvlist_get_string(lun->be_lun->options,
4322 "reordering", NULL);
4323 if (value != NULL &&
4324 strcmp(value, "unrestricted") == 0) {
4325 control_page->queue_flags &=
4326 ~SCP_QUEUE_ALG_MASK;
4327 control_page->queue_flags |=
4328 SCP_QUEUE_ALG_UNRESTRICTED;
4329 }
4330 memcpy(&lun->mode_pages.control_page[
4331 CTL_PAGE_CURRENT],
4332 &lun->mode_pages.control_page[
4333 CTL_PAGE_SAVED],
4334 sizeof(control_page_default));
4335 page_index->page_data =
4336 (uint8_t *)lun->mode_pages.control_page;
4337 break;
4338 }
4339 case 0x01:
4340 memcpy(&lun->mode_pages.control_ext_page[
4341 CTL_PAGE_DEFAULT],
4342 &control_ext_page_default,
4343 sizeof(control_ext_page_default));
4344 memcpy(&lun->mode_pages.control_ext_page[
4345 CTL_PAGE_CHANGEABLE],
4346 &control_ext_page_changeable,
4347 sizeof(control_ext_page_changeable));
4348 memcpy(&lun->mode_pages.control_ext_page[
4349 CTL_PAGE_SAVED],
4350 &control_ext_page_default,
4351 sizeof(control_ext_page_default));
4352 memcpy(&lun->mode_pages.control_ext_page[
4353 CTL_PAGE_CURRENT],
4354 &lun->mode_pages.control_ext_page[
4355 CTL_PAGE_SAVED],
4356 sizeof(control_ext_page_default));
4357 page_index->page_data =
4358 (uint8_t *)lun->mode_pages.control_ext_page;
4359 break;
4360 default:
4361 panic("subpage %#x for page %#x is incorrect!",
4362 page_index->subpage, page_code);
4363 }
4364 break;
4365 }
4366 case SMS_INFO_EXCEPTIONS_PAGE: {
4367 switch (page_index->subpage) {
4368 case SMS_SUBPAGE_PAGE_0:
4369 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4370 &ie_page_default,
4371 sizeof(ie_page_default));
4372 memcpy(&lun->mode_pages.ie_page[
4373 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4374 sizeof(ie_page_changeable));
4375 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4376 &ie_page_default,
4377 sizeof(ie_page_default));
4378 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4379 &ie_page_default,
4380 sizeof(ie_page_default));
4381 page_index->page_data =
4382 (uint8_t *)lun->mode_pages.ie_page;
4383 break;
4384 case 0x02: {
4385 struct ctl_logical_block_provisioning_page *page;
4386
4387 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4388 &lbp_page_default,
4389 sizeof(lbp_page_default));
4390 memcpy(&lun->mode_pages.lbp_page[
4391 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4392 sizeof(lbp_page_changeable));
4393 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4394 &lbp_page_default,
4395 sizeof(lbp_page_default));
4396 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4397 value = dnvlist_get_string(lun->be_lun->options,
4398 "avail-threshold", NULL);
4399 if (value != NULL &&
4400 ctl_expand_number(value, &ival) == 0) {
4401 page->descr[0].flags |= SLBPPD_ENABLED |
4402 SLBPPD_ARMING_DEC;
4403 if (lun->be_lun->blocksize)
4404 ival /= lun->be_lun->blocksize;
4405 else
4406 ival /= 512;
4407 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4408 page->descr[0].count);
4409 }
4410 value = dnvlist_get_string(lun->be_lun->options,
4411 "used-threshold", NULL);
4412 if (value != NULL &&
4413 ctl_expand_number(value, &ival) == 0) {
4414 page->descr[1].flags |= SLBPPD_ENABLED |
4415 SLBPPD_ARMING_INC;
4416 if (lun->be_lun->blocksize)
4417 ival /= lun->be_lun->blocksize;
4418 else
4419 ival /= 512;
4420 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4421 page->descr[1].count);
4422 }
4423 value = dnvlist_get_string(lun->be_lun->options,
4424 "pool-avail-threshold", NULL);
4425 if (value != NULL &&
4426 ctl_expand_number(value, &ival) == 0) {
4427 page->descr[2].flags |= SLBPPD_ENABLED |
4428 SLBPPD_ARMING_DEC;
4429 if (lun->be_lun->blocksize)
4430 ival /= lun->be_lun->blocksize;
4431 else
4432 ival /= 512;
4433 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4434 page->descr[2].count);
4435 }
4436 value = dnvlist_get_string(lun->be_lun->options,
4437 "pool-used-threshold", NULL);
4438 if (value != NULL &&
4439 ctl_expand_number(value, &ival) == 0) {
4440 page->descr[3].flags |= SLBPPD_ENABLED |
4441 SLBPPD_ARMING_INC;
4442 if (lun->be_lun->blocksize)
4443 ival /= lun->be_lun->blocksize;
4444 else
4445 ival /= 512;
4446 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4447 page->descr[3].count);
4448 }
4449 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4450 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4451 sizeof(lbp_page_default));
4452 page_index->page_data =
4453 (uint8_t *)lun->mode_pages.lbp_page;
4454 break;
4455 }
4456 default:
4457 panic("subpage %#x for page %#x is incorrect!",
4458 page_index->subpage, page_code);
4459 }
4460 break;
4461 }
4462 case SMS_CDDVD_CAPS_PAGE:{
4463 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4464 ("subpage %#x for page %#x is incorrect!",
4465 page_index->subpage, page_code));
4466 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT],
4467 &cddvd_page_default,
4468 sizeof(cddvd_page_default));
4469 memcpy(&lun->mode_pages.cddvd_page[
4470 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable,
4471 sizeof(cddvd_page_changeable));
4472 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4473 &cddvd_page_default,
4474 sizeof(cddvd_page_default));
4475 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT],
4476 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4477 sizeof(cddvd_page_default));
4478 page_index->page_data =
4479 (uint8_t *)lun->mode_pages.cddvd_page;
4480 break;
4481 }
4482 default:
4483 panic("invalid page code value %#x", page_code);
4484 }
4485 }
4486
4487 return (CTL_RETVAL_COMPLETE);
4488 }
4489
4490 static int
ctl_init_log_page_index(struct ctl_lun * lun)4491 ctl_init_log_page_index(struct ctl_lun *lun)
4492 {
4493 struct ctl_page_index *page_index;
4494 int i, j, k, prev;
4495
4496 memcpy(&lun->log_pages.index, log_page_index_template,
4497 sizeof(log_page_index_template));
4498
4499 prev = -1;
4500 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4501 page_index = &lun->log_pages.index[i];
4502 if (lun->be_lun->lun_type == T_DIRECT &&
4503 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4504 continue;
4505 if (lun->be_lun->lun_type == T_PROCESSOR &&
4506 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4507 continue;
4508 if (lun->be_lun->lun_type == T_CDROM &&
4509 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4510 continue;
4511
4512 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4513 lun->backend->lun_attr == NULL)
4514 continue;
4515
4516 if (page_index->page_code != prev) {
4517 lun->log_pages.pages_page[j] = page_index->page_code;
4518 prev = page_index->page_code;
4519 j++;
4520 }
4521 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4522 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4523 k++;
4524 }
4525 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4526 lun->log_pages.index[0].page_len = j;
4527 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4528 lun->log_pages.index[1].page_len = k * 2;
4529 lun->log_pages.index[2].page_data = (uint8_t *)&lun->log_pages.temp_page;
4530 lun->log_pages.index[2].page_len = sizeof(lun->log_pages.temp_page);
4531 lun->log_pages.index[3].page_data = &lun->log_pages.lbp_page[0];
4532 lun->log_pages.index[3].page_len = 12*CTL_NUM_LBP_PARAMS;
4533 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.stat_page;
4534 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.stat_page);
4535 lun->log_pages.index[5].page_data = (uint8_t *)&lun->log_pages.ie_page;
4536 lun->log_pages.index[5].page_len = sizeof(lun->log_pages.ie_page);
4537
4538 return (CTL_RETVAL_COMPLETE);
4539 }
4540
4541 static int
hex2bin(const char * str,uint8_t * buf,int buf_size)4542 hex2bin(const char *str, uint8_t *buf, int buf_size)
4543 {
4544 int i;
4545 u_char c;
4546
4547 memset(buf, 0, buf_size);
4548 while (isspace(str[0]))
4549 str++;
4550 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4551 str += 2;
4552 buf_size *= 2;
4553 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4554 while (str[i] == '-') /* Skip dashes in UUIDs. */
4555 str++;
4556 c = str[i];
4557 if (isdigit(c))
4558 c -= '0';
4559 else if (isalpha(c))
4560 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4561 else
4562 break;
4563 if (c >= 16)
4564 break;
4565 if ((i & 1) == 0)
4566 buf[i / 2] |= (c << 4);
4567 else
4568 buf[i / 2] |= c;
4569 }
4570 return ((i + 1) / 2);
4571 }
4572
4573 /*
4574 * Add LUN.
4575 *
4576 * Returns 0 for success, non-zero (errno) for failure.
4577 */
4578 int
ctl_add_lun(struct ctl_be_lun * be_lun)4579 ctl_add_lun(struct ctl_be_lun *be_lun)
4580 {
4581 struct ctl_softc *ctl_softc = control_softc;
4582 struct ctl_lun *nlun, *lun;
4583 struct scsi_vpd_id_descriptor *desc;
4584 struct scsi_vpd_id_t10 *t10id;
4585 const char *eui, *naa, *scsiname, *uuid, *vendor, *value;
4586 int lun_number;
4587 int devidlen, idlen1, idlen2 = 0, len;
4588
4589 /*
4590 * We support only Direct Access, CD-ROM or Processor LUN types.
4591 */
4592 switch (be_lun->lun_type) {
4593 case T_DIRECT:
4594 case T_PROCESSOR:
4595 case T_CDROM:
4596 break;
4597 case T_SEQUENTIAL:
4598 case T_CHANGER:
4599 default:
4600 return (EINVAL);
4601 }
4602 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK | M_ZERO);
4603
4604 lun->pending_sense = malloc(sizeof(struct scsi_sense_data *) *
4605 ctl_max_ports, M_DEVBUF, M_WAITOK | M_ZERO);
4606 lun->pending_ua = malloc(sizeof(ctl_ua_type *) * ctl_max_ports,
4607 M_DEVBUF, M_WAITOK | M_ZERO);
4608 lun->pr_keys = malloc(sizeof(uint64_t *) * ctl_max_ports,
4609 M_DEVBUF, M_WAITOK | M_ZERO);
4610
4611 /* Generate LUN ID. */
4612 devidlen = max(CTL_DEVID_MIN_LEN,
4613 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4614 idlen1 = sizeof(*t10id) + devidlen;
4615 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4616 scsiname = dnvlist_get_string(be_lun->options, "scsiname", NULL);
4617 if (scsiname != NULL) {
4618 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4619 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4620 }
4621 eui = dnvlist_get_string(be_lun->options, "eui", NULL);
4622 if (eui != NULL) {
4623 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4624 }
4625 naa = dnvlist_get_string(be_lun->options, "naa", NULL);
4626 if (naa != NULL) {
4627 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4628 }
4629 uuid = dnvlist_get_string(be_lun->options, "uuid", NULL);
4630 if (uuid != NULL) {
4631 len += sizeof(struct scsi_vpd_id_descriptor) + 18;
4632 }
4633 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4634 M_CTL, M_WAITOK | M_ZERO);
4635 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4636 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4637 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4638 desc->length = idlen1;
4639 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4640 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4641 if ((vendor = dnvlist_get_string(be_lun->options, "vendor", NULL)) == NULL) {
4642 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4643 } else {
4644 strncpy(t10id->vendor, vendor,
4645 min(sizeof(t10id->vendor), strlen(vendor)));
4646 }
4647 strncpy((char *)t10id->vendor_spec_id,
4648 (char *)be_lun->device_id, devidlen);
4649 if (scsiname != NULL) {
4650 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4651 desc->length);
4652 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4653 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4654 SVPD_ID_TYPE_SCSI_NAME;
4655 desc->length = idlen2;
4656 strlcpy(desc->identifier, scsiname, idlen2);
4657 }
4658 if (eui != NULL) {
4659 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4660 desc->length);
4661 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4662 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4663 SVPD_ID_TYPE_EUI64;
4664 desc->length = hex2bin(eui, desc->identifier, 16);
4665 desc->length = desc->length > 12 ? 16 :
4666 (desc->length > 8 ? 12 : 8);
4667 len -= 16 - desc->length;
4668 }
4669 if (naa != NULL) {
4670 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4671 desc->length);
4672 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4673 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4674 SVPD_ID_TYPE_NAA;
4675 desc->length = hex2bin(naa, desc->identifier, 16);
4676 desc->length = desc->length > 8 ? 16 : 8;
4677 len -= 16 - desc->length;
4678 }
4679 if (uuid != NULL) {
4680 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4681 desc->length);
4682 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4683 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4684 SVPD_ID_TYPE_UUID;
4685 desc->identifier[0] = 0x10;
4686 hex2bin(uuid, &desc->identifier[2], 16);
4687 desc->length = 18;
4688 }
4689 lun->lun_devid->len = len;
4690
4691 mtx_lock(&ctl_softc->ctl_lock);
4692 /*
4693 * See if the caller requested a particular LUN number. If so, see
4694 * if it is available. Otherwise, allocate the first available LUN.
4695 */
4696 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4697 if ((be_lun->req_lun_id > (ctl_max_luns - 1))
4698 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4699 mtx_unlock(&ctl_softc->ctl_lock);
4700 if (be_lun->req_lun_id > (ctl_max_luns - 1)) {
4701 printf("ctl: requested LUN ID %d is higher "
4702 "than ctl_max_luns - 1 (%d)\n",
4703 be_lun->req_lun_id, ctl_max_luns - 1);
4704 } else {
4705 /*
4706 * XXX KDM return an error, or just assign
4707 * another LUN ID in this case??
4708 */
4709 printf("ctl: requested LUN ID %d is already "
4710 "in use\n", be_lun->req_lun_id);
4711 }
4712 fail:
4713 free(lun->lun_devid, M_CTL);
4714 free(lun, M_CTL);
4715 return (ENOSPC);
4716 }
4717 lun_number = be_lun->req_lun_id;
4718 } else {
4719 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, ctl_max_luns);
4720 if (lun_number == -1) {
4721 mtx_unlock(&ctl_softc->ctl_lock);
4722 printf("ctl: can't allocate LUN, out of LUNs\n");
4723 goto fail;
4724 }
4725 }
4726 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4727 mtx_unlock(&ctl_softc->ctl_lock);
4728
4729 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4730 lun->lun = lun_number;
4731 lun->be_lun = be_lun;
4732 /*
4733 * The processor LUN is always enabled. Disk LUNs come on line
4734 * disabled, and must be enabled by the backend.
4735 */
4736 lun->flags |= CTL_LUN_DISABLED;
4737 lun->backend = be_lun->be;
4738 be_lun->ctl_lun = lun;
4739 be_lun->lun_id = lun_number;
4740 if (be_lun->flags & CTL_LUN_FLAG_EJECTED)
4741 lun->flags |= CTL_LUN_EJECTED;
4742 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA)
4743 lun->flags |= CTL_LUN_NO_MEDIA;
4744 if (be_lun->flags & CTL_LUN_FLAG_STOPPED)
4745 lun->flags |= CTL_LUN_STOPPED;
4746
4747 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4748 lun->flags |= CTL_LUN_PRIMARY_SC;
4749
4750 value = dnvlist_get_string(be_lun->options, "removable", NULL);
4751 if (value != NULL) {
4752 if (strcmp(value, "on") == 0)
4753 lun->flags |= CTL_LUN_REMOVABLE;
4754 } else if (be_lun->lun_type == T_CDROM)
4755 lun->flags |= CTL_LUN_REMOVABLE;
4756
4757 lun->ctl_softc = ctl_softc;
4758 #ifdef CTL_TIME_IO
4759 lun->last_busy = getsbinuptime();
4760 #endif
4761 LIST_INIT(&lun->ooa_queue);
4762 STAILQ_INIT(&lun->error_list);
4763 lun->ie_reported = 1;
4764 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0);
4765 ctl_tpc_lun_init(lun);
4766 if (lun->flags & CTL_LUN_REMOVABLE) {
4767 lun->prevent = malloc((CTL_MAX_INITIATORS + 31) / 32 * 4,
4768 M_CTL, M_WAITOK | M_ZERO);
4769 }
4770
4771 /*
4772 * Initialize the mode and log page index.
4773 */
4774 ctl_init_page_index(lun);
4775 ctl_init_log_page_index(lun);
4776
4777 /* Setup statistics gathering */
4778 lun->stats.item = lun_number;
4779
4780 /*
4781 * Now, before we insert this lun on the lun list, set the lun
4782 * inventory changed UA for all other luns.
4783 */
4784 mtx_lock(&ctl_softc->ctl_lock);
4785 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4786 mtx_lock(&nlun->lun_lock);
4787 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4788 mtx_unlock(&nlun->lun_lock);
4789 }
4790 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4791 ctl_softc->ctl_luns[lun_number] = lun;
4792 ctl_softc->num_luns++;
4793 mtx_unlock(&ctl_softc->ctl_lock);
4794
4795 /*
4796 * We successfully added the LUN, attempt to enable it.
4797 */
4798 if (ctl_enable_lun(lun) != 0) {
4799 printf("%s: ctl_enable_lun() failed!\n", __func__);
4800 mtx_lock(&ctl_softc->ctl_lock);
4801 STAILQ_REMOVE(&ctl_softc->lun_list, lun, ctl_lun, links);
4802 ctl_clear_mask(ctl_softc->ctl_lun_mask, lun_number);
4803 ctl_softc->ctl_luns[lun_number] = NULL;
4804 ctl_softc->num_luns--;
4805 mtx_unlock(&ctl_softc->ctl_lock);
4806 free(lun->lun_devid, M_CTL);
4807 free(lun, M_CTL);
4808 return (EIO);
4809 }
4810
4811 return (0);
4812 }
4813
4814 /*
4815 * Free LUN that has no active requests.
4816 */
4817 static int
ctl_free_lun(struct ctl_lun * lun)4818 ctl_free_lun(struct ctl_lun *lun)
4819 {
4820 struct ctl_softc *softc = lun->ctl_softc;
4821 struct ctl_lun *nlun;
4822 int i;
4823
4824 KASSERT(LIST_EMPTY(&lun->ooa_queue),
4825 ("Freeing a LUN %p with outstanding I/O!\n", lun));
4826
4827 mtx_lock(&softc->ctl_lock);
4828 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4829 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4830 softc->ctl_luns[lun->lun] = NULL;
4831 softc->num_luns--;
4832 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4833 mtx_lock(&nlun->lun_lock);
4834 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4835 mtx_unlock(&nlun->lun_lock);
4836 }
4837 mtx_unlock(&softc->ctl_lock);
4838
4839 /*
4840 * Tell the backend to free resources, if this LUN has a backend.
4841 */
4842 lun->be_lun->lun_shutdown(lun->be_lun);
4843
4844 lun->ie_reportcnt = UINT32_MAX;
4845 callout_drain(&lun->ie_callout);
4846 ctl_tpc_lun_shutdown(lun);
4847 mtx_destroy(&lun->lun_lock);
4848 free(lun->lun_devid, M_CTL);
4849 for (i = 0; i < ctl_max_ports; i++)
4850 free(lun->pending_ua[i], M_CTL);
4851 free(lun->pending_ua, M_DEVBUF);
4852 for (i = 0; i < ctl_max_ports; i++)
4853 free(lun->pr_keys[i], M_CTL);
4854 free(lun->pr_keys, M_DEVBUF);
4855 free(lun->write_buffer, M_CTL);
4856 free(lun->prevent, M_CTL);
4857 free(lun, M_CTL);
4858
4859 return (0);
4860 }
4861
4862 static int
ctl_enable_lun(struct ctl_lun * lun)4863 ctl_enable_lun(struct ctl_lun *lun)
4864 {
4865 struct ctl_softc *softc;
4866 struct ctl_port *port, *nport;
4867 int retval;
4868
4869 softc = lun->ctl_softc;
4870
4871 mtx_lock(&softc->ctl_lock);
4872 mtx_lock(&lun->lun_lock);
4873 KASSERT((lun->flags & CTL_LUN_DISABLED) != 0,
4874 ("%s: LUN not disabled", __func__));
4875 lun->flags &= ~CTL_LUN_DISABLED;
4876 mtx_unlock(&lun->lun_lock);
4877
4878 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) {
4879 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4880 port->lun_map != NULL || port->lun_enable == NULL)
4881 continue;
4882
4883 /*
4884 * Drop the lock while we call the FETD's enable routine.
4885 * This can lead to a callback into CTL (at least in the
4886 * case of the internal initiator frontend.
4887 */
4888 mtx_unlock(&softc->ctl_lock);
4889 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4890 mtx_lock(&softc->ctl_lock);
4891 if (retval != 0) {
4892 printf("%s: FETD %s port %d returned error "
4893 "%d for lun_enable on lun %jd\n",
4894 __func__, port->port_name, port->targ_port,
4895 retval, (intmax_t)lun->lun);
4896 }
4897 }
4898
4899 mtx_unlock(&softc->ctl_lock);
4900 ctl_isc_announce_lun(lun);
4901
4902 return (0);
4903 }
4904
4905 static int
ctl_disable_lun(struct ctl_lun * lun)4906 ctl_disable_lun(struct ctl_lun *lun)
4907 {
4908 struct ctl_softc *softc;
4909 struct ctl_port *port;
4910 int retval;
4911
4912 softc = lun->ctl_softc;
4913
4914 mtx_lock(&softc->ctl_lock);
4915 mtx_lock(&lun->lun_lock);
4916 KASSERT((lun->flags & CTL_LUN_DISABLED) == 0,
4917 ("%s: LUN not enabled", __func__));
4918 lun->flags |= CTL_LUN_DISABLED;
4919 mtx_unlock(&lun->lun_lock);
4920
4921 STAILQ_FOREACH(port, &softc->port_list, links) {
4922 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4923 port->lun_map != NULL || port->lun_disable == NULL)
4924 continue;
4925
4926 /*
4927 * Drop the lock before we call the frontend's disable
4928 * routine, to avoid lock order reversals.
4929 *
4930 * XXX KDM what happens if the frontend list changes while
4931 * we're traversing it? It's unlikely, but should be handled.
4932 */
4933 mtx_unlock(&softc->ctl_lock);
4934 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4935 mtx_lock(&softc->ctl_lock);
4936 if (retval != 0) {
4937 printf("%s: FETD %s port %d returned error "
4938 "%d for lun_disable on lun %jd\n",
4939 __func__, port->port_name, port->targ_port,
4940 retval, (intmax_t)lun->lun);
4941 }
4942 }
4943
4944 mtx_unlock(&softc->ctl_lock);
4945 ctl_isc_announce_lun(lun);
4946
4947 return (0);
4948 }
4949
4950 int
ctl_start_lun(struct ctl_be_lun * be_lun)4951 ctl_start_lun(struct ctl_be_lun *be_lun)
4952 {
4953 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4954
4955 mtx_lock(&lun->lun_lock);
4956 lun->flags &= ~CTL_LUN_STOPPED;
4957 mtx_unlock(&lun->lun_lock);
4958 return (0);
4959 }
4960
4961 int
ctl_stop_lun(struct ctl_be_lun * be_lun)4962 ctl_stop_lun(struct ctl_be_lun *be_lun)
4963 {
4964 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4965
4966 mtx_lock(&lun->lun_lock);
4967 lun->flags |= CTL_LUN_STOPPED;
4968 mtx_unlock(&lun->lun_lock);
4969 return (0);
4970 }
4971
4972 int
ctl_lun_no_media(struct ctl_be_lun * be_lun)4973 ctl_lun_no_media(struct ctl_be_lun *be_lun)
4974 {
4975 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4976
4977 mtx_lock(&lun->lun_lock);
4978 lun->flags |= CTL_LUN_NO_MEDIA;
4979 mtx_unlock(&lun->lun_lock);
4980 return (0);
4981 }
4982
4983 int
ctl_lun_has_media(struct ctl_be_lun * be_lun)4984 ctl_lun_has_media(struct ctl_be_lun *be_lun)
4985 {
4986 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4987 union ctl_ha_msg msg;
4988
4989 mtx_lock(&lun->lun_lock);
4990 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED);
4991 if (lun->flags & CTL_LUN_REMOVABLE)
4992 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE);
4993 mtx_unlock(&lun->lun_lock);
4994 if ((lun->flags & CTL_LUN_REMOVABLE) &&
4995 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4996 bzero(&msg.ua, sizeof(msg.ua));
4997 msg.hdr.msg_type = CTL_MSG_UA;
4998 msg.hdr.nexus.initid = -1;
4999 msg.hdr.nexus.targ_port = -1;
5000 msg.hdr.nexus.targ_lun = lun->lun;
5001 msg.hdr.nexus.targ_mapped_lun = lun->lun;
5002 msg.ua.ua_all = 1;
5003 msg.ua.ua_set = 1;
5004 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE;
5005 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
5006 M_WAITOK);
5007 }
5008 return (0);
5009 }
5010
5011 int
ctl_lun_ejected(struct ctl_be_lun * be_lun)5012 ctl_lun_ejected(struct ctl_be_lun *be_lun)
5013 {
5014 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5015
5016 mtx_lock(&lun->lun_lock);
5017 lun->flags |= CTL_LUN_EJECTED;
5018 mtx_unlock(&lun->lun_lock);
5019 return (0);
5020 }
5021
5022 int
ctl_lun_primary(struct ctl_be_lun * be_lun)5023 ctl_lun_primary(struct ctl_be_lun *be_lun)
5024 {
5025 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5026
5027 mtx_lock(&lun->lun_lock);
5028 lun->flags |= CTL_LUN_PRIMARY_SC;
5029 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
5030 mtx_unlock(&lun->lun_lock);
5031 ctl_isc_announce_lun(lun);
5032 return (0);
5033 }
5034
5035 int
ctl_lun_secondary(struct ctl_be_lun * be_lun)5036 ctl_lun_secondary(struct ctl_be_lun *be_lun)
5037 {
5038 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5039
5040 mtx_lock(&lun->lun_lock);
5041 lun->flags &= ~CTL_LUN_PRIMARY_SC;
5042 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
5043 mtx_unlock(&lun->lun_lock);
5044 ctl_isc_announce_lun(lun);
5045 return (0);
5046 }
5047
5048 /*
5049 * Remove LUN. If there are active requests, wait for completion.
5050 *
5051 * Returns 0 for success, non-zero (errno) for failure.
5052 * Completion is reported to backed via the lun_shutdown() method.
5053 */
5054 int
ctl_remove_lun(struct ctl_be_lun * be_lun)5055 ctl_remove_lun(struct ctl_be_lun *be_lun)
5056 {
5057 struct ctl_lun *lun;
5058
5059 lun = (struct ctl_lun *)be_lun->ctl_lun;
5060
5061 ctl_disable_lun(lun);
5062
5063 mtx_lock(&lun->lun_lock);
5064 lun->flags |= CTL_LUN_INVALID;
5065
5066 /*
5067 * If there is nothing in the OOA queue, go ahead and free the LUN.
5068 * If we have something in the OOA queue, we'll free it when the
5069 * last I/O completes.
5070 */
5071 if (LIST_EMPTY(&lun->ooa_queue)) {
5072 mtx_unlock(&lun->lun_lock);
5073 ctl_free_lun(lun);
5074 } else
5075 mtx_unlock(&lun->lun_lock);
5076
5077 return (0);
5078 }
5079
5080 void
ctl_lun_capacity_changed(struct ctl_be_lun * be_lun)5081 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
5082 {
5083 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5084 union ctl_ha_msg msg;
5085
5086 mtx_lock(&lun->lun_lock);
5087 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE);
5088 mtx_unlock(&lun->lun_lock);
5089 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
5090 /* Send msg to other side. */
5091 bzero(&msg.ua, sizeof(msg.ua));
5092 msg.hdr.msg_type = CTL_MSG_UA;
5093 msg.hdr.nexus.initid = -1;
5094 msg.hdr.nexus.targ_port = -1;
5095 msg.hdr.nexus.targ_lun = lun->lun;
5096 msg.hdr.nexus.targ_mapped_lun = lun->lun;
5097 msg.ua.ua_all = 1;
5098 msg.ua.ua_set = 1;
5099 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE;
5100 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
5101 M_WAITOK);
5102 }
5103 }
5104
5105 /*
5106 * Backend "memory move is complete" callback for requests that never
5107 * make it down to say RAIDCore's configuration code.
5108 */
5109 int
ctl_config_move_done(union ctl_io * io,bool samethr)5110 ctl_config_move_done(union ctl_io *io, bool samethr)
5111 {
5112 int retval;
5113
5114 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5115 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
5116 ("%s: unexpected I/O type %x", __func__, io->io_hdr.io_type));
5117
5118 if (ctl_debug & CTL_DEBUG_CDB_DATA)
5119 ctl_data_print(io);
5120 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
5121 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5122 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
5123 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5124 /*
5125 * XXX KDM just assuming a single pointer here, and not a
5126 * S/G list. If we start using S/G lists for config data,
5127 * we'll need to know how to clean them up here as well.
5128 */
5129 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5130 free(io->scsiio.kern_data_ptr, M_CTL);
5131 ctl_done(io);
5132 retval = CTL_RETVAL_COMPLETE;
5133 } else {
5134 /*
5135 * XXX KDM now we need to continue data movement. Some
5136 * options:
5137 * - call ctl_scsiio() again? We don't do this for data
5138 * writes, because for those at least we know ahead of
5139 * time where the write will go and how long it is. For
5140 * config writes, though, that information is largely
5141 * contained within the write itself, thus we need to
5142 * parse out the data again.
5143 *
5144 * - Call some other function once the data is in?
5145 */
5146
5147 /*
5148 * XXX KDM call ctl_scsiio() again for now, and check flag
5149 * bits to see whether we're allocated or not.
5150 */
5151 retval = ctl_scsiio(&io->scsiio);
5152 }
5153 return (retval);
5154 }
5155
5156 /*
5157 * This gets called by a backend driver when it is done with a
5158 * data_submit method.
5159 */
5160 void
ctl_data_submit_done(union ctl_io * io)5161 ctl_data_submit_done(union ctl_io *io)
5162 {
5163 /*
5164 * If the IO_CONT flag is set, we need to call the supplied
5165 * function to continue processing the I/O, instead of completing
5166 * the I/O just yet.
5167 *
5168 * If there is an error, though, we don't want to keep processing.
5169 * Instead, just send status back to the initiator.
5170 */
5171 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5172 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5173 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5174 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5175 io->scsiio.io_cont(io);
5176 return;
5177 }
5178 ctl_done(io);
5179 }
5180
5181 /*
5182 * This gets called by a backend driver when it is done with a
5183 * configuration write.
5184 */
5185 void
ctl_config_write_done(union ctl_io * io)5186 ctl_config_write_done(union ctl_io *io)
5187 {
5188 uint8_t *buf;
5189
5190 /*
5191 * If the IO_CONT flag is set, we need to call the supplied
5192 * function to continue processing the I/O, instead of completing
5193 * the I/O just yet.
5194 *
5195 * If there is an error, though, we don't want to keep processing.
5196 * Instead, just send status back to the initiator.
5197 */
5198 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5199 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5200 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5201 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5202 io->scsiio.io_cont(io);
5203 return;
5204 }
5205 /*
5206 * Since a configuration write can be done for commands that actually
5207 * have data allocated, like write buffer, and commands that have
5208 * no data, like start/stop unit, we need to check here.
5209 */
5210 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5211 buf = io->scsiio.kern_data_ptr;
5212 else
5213 buf = NULL;
5214 ctl_done(io);
5215 if (buf)
5216 free(buf, M_CTL);
5217 }
5218
5219 void
ctl_config_read_done(union ctl_io * io)5220 ctl_config_read_done(union ctl_io *io)
5221 {
5222 uint8_t *buf;
5223
5224 /*
5225 * If there is some error -- we are done, skip data transfer.
5226 */
5227 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5228 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5229 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5230 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5231 buf = io->scsiio.kern_data_ptr;
5232 else
5233 buf = NULL;
5234 ctl_done(io);
5235 if (buf)
5236 free(buf, M_CTL);
5237 return;
5238 }
5239
5240 /*
5241 * If the IO_CONT flag is set, we need to call the supplied
5242 * function to continue processing the I/O, instead of completing
5243 * the I/O just yet.
5244 */
5245 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5246 io->scsiio.io_cont(io);
5247 return;
5248 }
5249
5250 ctl_datamove(io);
5251 }
5252
5253 /*
5254 * SCSI release command.
5255 */
5256 int
ctl_scsi_release(struct ctl_scsiio * ctsio)5257 ctl_scsi_release(struct ctl_scsiio *ctsio)
5258 {
5259 struct ctl_lun *lun = CTL_LUN(ctsio);
5260 uint32_t residx;
5261
5262 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5263
5264 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5265
5266 /*
5267 * XXX KDM right now, we only support LUN reservation. We don't
5268 * support 3rd party reservations, or extent reservations, which
5269 * might actually need the parameter list. If we've gotten this
5270 * far, we've got a LUN reservation. Anything else got kicked out
5271 * above. So, according to SPC, ignore the length.
5272 */
5273
5274 mtx_lock(&lun->lun_lock);
5275
5276 /*
5277 * According to SPC, it is not an error for an intiator to attempt
5278 * to release a reservation on a LUN that isn't reserved, or that
5279 * is reserved by another initiator. The reservation can only be
5280 * released, though, by the initiator who made it or by one of
5281 * several reset type events.
5282 */
5283 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5284 lun->flags &= ~CTL_LUN_RESERVED;
5285
5286 mtx_unlock(&lun->lun_lock);
5287
5288 ctl_set_success(ctsio);
5289 ctl_done((union ctl_io *)ctsio);
5290 return (CTL_RETVAL_COMPLETE);
5291 }
5292
5293 int
ctl_scsi_reserve(struct ctl_scsiio * ctsio)5294 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5295 {
5296 struct ctl_lun *lun = CTL_LUN(ctsio);
5297 uint32_t residx;
5298
5299 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5300
5301 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5302
5303 /*
5304 * XXX KDM right now, we only support LUN reservation. We don't
5305 * support 3rd party reservations, or extent reservations, which
5306 * might actually need the parameter list. If we've gotten this
5307 * far, we've got a LUN reservation. Anything else got kicked out
5308 * above. So, according to SPC, ignore the length.
5309 */
5310
5311 mtx_lock(&lun->lun_lock);
5312 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5313 ctl_set_reservation_conflict(ctsio);
5314 goto bailout;
5315 }
5316
5317 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */
5318 if (lun->flags & CTL_LUN_PR_RESERVED) {
5319 ctl_set_success(ctsio);
5320 goto bailout;
5321 }
5322
5323 lun->flags |= CTL_LUN_RESERVED;
5324 lun->res_idx = residx;
5325 ctl_set_success(ctsio);
5326
5327 bailout:
5328 mtx_unlock(&lun->lun_lock);
5329 ctl_done((union ctl_io *)ctsio);
5330 return (CTL_RETVAL_COMPLETE);
5331 }
5332
5333 int
ctl_start_stop(struct ctl_scsiio * ctsio)5334 ctl_start_stop(struct ctl_scsiio *ctsio)
5335 {
5336 struct ctl_lun *lun = CTL_LUN(ctsio);
5337 struct scsi_start_stop_unit *cdb;
5338 int retval;
5339
5340 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5341
5342 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5343
5344 if ((cdb->how & SSS_PC_MASK) == 0) {
5345 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
5346 (cdb->how & SSS_START) == 0) {
5347 uint32_t residx;
5348
5349 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5350 if (ctl_get_prkey(lun, residx) == 0 ||
5351 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) {
5352 ctl_set_reservation_conflict(ctsio);
5353 ctl_done((union ctl_io *)ctsio);
5354 return (CTL_RETVAL_COMPLETE);
5355 }
5356 }
5357
5358 if ((cdb->how & SSS_LOEJ) &&
5359 (lun->flags & CTL_LUN_REMOVABLE) == 0) {
5360 ctl_set_invalid_field(ctsio,
5361 /*sks_valid*/ 1,
5362 /*command*/ 1,
5363 /*field*/ 4,
5364 /*bit_valid*/ 1,
5365 /*bit*/ 1);
5366 ctl_done((union ctl_io *)ctsio);
5367 return (CTL_RETVAL_COMPLETE);
5368 }
5369
5370 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) &&
5371 lun->prevent_count > 0) {
5372 /* "Medium removal prevented" */
5373 ctl_set_sense(ctsio, /*current_error*/ 1,
5374 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ?
5375 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST,
5376 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE);
5377 ctl_done((union ctl_io *)ctsio);
5378 return (CTL_RETVAL_COMPLETE);
5379 }
5380 }
5381
5382 retval = lun->backend->config_write((union ctl_io *)ctsio);
5383 return (retval);
5384 }
5385
5386 int
ctl_prevent_allow(struct ctl_scsiio * ctsio)5387 ctl_prevent_allow(struct ctl_scsiio *ctsio)
5388 {
5389 struct ctl_lun *lun = CTL_LUN(ctsio);
5390 struct scsi_prevent *cdb;
5391 int retval;
5392 uint32_t initidx;
5393
5394 CTL_DEBUG_PRINT(("ctl_prevent_allow\n"));
5395
5396 cdb = (struct scsi_prevent *)ctsio->cdb;
5397
5398 if ((lun->flags & CTL_LUN_REMOVABLE) == 0 || lun->prevent == NULL) {
5399 ctl_set_invalid_opcode(ctsio);
5400 ctl_done((union ctl_io *)ctsio);
5401 return (CTL_RETVAL_COMPLETE);
5402 }
5403
5404 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5405 mtx_lock(&lun->lun_lock);
5406 if ((cdb->how & PR_PREVENT) &&
5407 ctl_is_set(lun->prevent, initidx) == 0) {
5408 ctl_set_mask(lun->prevent, initidx);
5409 lun->prevent_count++;
5410 } else if ((cdb->how & PR_PREVENT) == 0 &&
5411 ctl_is_set(lun->prevent, initidx)) {
5412 ctl_clear_mask(lun->prevent, initidx);
5413 lun->prevent_count--;
5414 }
5415 mtx_unlock(&lun->lun_lock);
5416 retval = lun->backend->config_write((union ctl_io *)ctsio);
5417 return (retval);
5418 }
5419
5420 /*
5421 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5422 * we don't really do anything with the LBA and length fields if the user
5423 * passes them in. Instead we'll just flush out the cache for the entire
5424 * LUN.
5425 */
5426 int
ctl_sync_cache(struct ctl_scsiio * ctsio)5427 ctl_sync_cache(struct ctl_scsiio *ctsio)
5428 {
5429 struct ctl_lun *lun = CTL_LUN(ctsio);
5430 struct ctl_lba_len_flags *lbalen;
5431 uint64_t starting_lba;
5432 uint32_t block_count;
5433 int retval;
5434 uint8_t byte2;
5435
5436 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5437
5438 retval = 0;
5439
5440 switch (ctsio->cdb[0]) {
5441 case SYNCHRONIZE_CACHE: {
5442 struct scsi_sync_cache *cdb;
5443 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5444
5445 starting_lba = scsi_4btoul(cdb->begin_lba);
5446 block_count = scsi_2btoul(cdb->lb_count);
5447 byte2 = cdb->byte2;
5448 break;
5449 }
5450 case SYNCHRONIZE_CACHE_16: {
5451 struct scsi_sync_cache_16 *cdb;
5452 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5453
5454 starting_lba = scsi_8btou64(cdb->begin_lba);
5455 block_count = scsi_4btoul(cdb->lb_count);
5456 byte2 = cdb->byte2;
5457 break;
5458 }
5459 default:
5460 ctl_set_invalid_opcode(ctsio);
5461 ctl_done((union ctl_io *)ctsio);
5462 goto bailout;
5463 break; /* NOTREACHED */
5464 }
5465
5466 /*
5467 * We check the LBA and length, but don't do anything with them.
5468 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5469 * get flushed. This check will just help satisfy anyone who wants
5470 * to see an error for an out of range LBA.
5471 */
5472 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5473 ctl_set_lba_out_of_range(ctsio,
5474 MAX(starting_lba, lun->be_lun->maxlba + 1));
5475 ctl_done((union ctl_io *)ctsio);
5476 goto bailout;
5477 }
5478
5479 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5480 lbalen->lba = starting_lba;
5481 lbalen->len = block_count;
5482 lbalen->flags = byte2;
5483 retval = lun->backend->config_write((union ctl_io *)ctsio);
5484
5485 bailout:
5486 return (retval);
5487 }
5488
5489 int
ctl_format(struct ctl_scsiio * ctsio)5490 ctl_format(struct ctl_scsiio *ctsio)
5491 {
5492 struct scsi_format *cdb;
5493 int length, defect_list_len;
5494
5495 CTL_DEBUG_PRINT(("ctl_format\n"));
5496
5497 cdb = (struct scsi_format *)ctsio->cdb;
5498
5499 length = 0;
5500 if (cdb->byte2 & SF_FMTDATA) {
5501 if (cdb->byte2 & SF_LONGLIST)
5502 length = sizeof(struct scsi_format_header_long);
5503 else
5504 length = sizeof(struct scsi_format_header_short);
5505 }
5506
5507 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5508 && (length > 0)) {
5509 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5510 ctsio->kern_data_len = length;
5511 ctsio->kern_total_len = length;
5512 ctsio->kern_rel_offset = 0;
5513 ctsio->kern_sg_entries = 0;
5514 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5515 ctsio->be_move_done = ctl_config_move_done;
5516 ctl_datamove((union ctl_io *)ctsio);
5517
5518 return (CTL_RETVAL_COMPLETE);
5519 }
5520
5521 defect_list_len = 0;
5522
5523 if (cdb->byte2 & SF_FMTDATA) {
5524 if (cdb->byte2 & SF_LONGLIST) {
5525 struct scsi_format_header_long *header;
5526
5527 header = (struct scsi_format_header_long *)
5528 ctsio->kern_data_ptr;
5529
5530 defect_list_len = scsi_4btoul(header->defect_list_len);
5531 if (defect_list_len != 0) {
5532 ctl_set_invalid_field(ctsio,
5533 /*sks_valid*/ 1,
5534 /*command*/ 0,
5535 /*field*/ 2,
5536 /*bit_valid*/ 0,
5537 /*bit*/ 0);
5538 goto bailout;
5539 }
5540 } else {
5541 struct scsi_format_header_short *header;
5542
5543 header = (struct scsi_format_header_short *)
5544 ctsio->kern_data_ptr;
5545
5546 defect_list_len = scsi_2btoul(header->defect_list_len);
5547 if (defect_list_len != 0) {
5548 ctl_set_invalid_field(ctsio,
5549 /*sks_valid*/ 1,
5550 /*command*/ 0,
5551 /*field*/ 2,
5552 /*bit_valid*/ 0,
5553 /*bit*/ 0);
5554 goto bailout;
5555 }
5556 }
5557 }
5558
5559 ctl_set_success(ctsio);
5560 bailout:
5561
5562 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5563 free(ctsio->kern_data_ptr, M_CTL);
5564 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5565 }
5566
5567 ctl_done((union ctl_io *)ctsio);
5568 return (CTL_RETVAL_COMPLETE);
5569 }
5570
5571 int
ctl_read_buffer(struct ctl_scsiio * ctsio)5572 ctl_read_buffer(struct ctl_scsiio *ctsio)
5573 {
5574 struct ctl_lun *lun = CTL_LUN(ctsio);
5575 uint64_t buffer_offset;
5576 uint32_t len;
5577 uint8_t byte2;
5578 static uint8_t descr[4];
5579 static uint8_t echo_descr[4] = { 0 };
5580
5581 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5582
5583 switch (ctsio->cdb[0]) {
5584 case READ_BUFFER: {
5585 struct scsi_read_buffer *cdb;
5586
5587 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5588 buffer_offset = scsi_3btoul(cdb->offset);
5589 len = scsi_3btoul(cdb->length);
5590 byte2 = cdb->byte2;
5591 break;
5592 }
5593 case READ_BUFFER_16: {
5594 struct scsi_read_buffer_16 *cdb;
5595
5596 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb;
5597 buffer_offset = scsi_8btou64(cdb->offset);
5598 len = scsi_4btoul(cdb->length);
5599 byte2 = cdb->byte2;
5600 break;
5601 }
5602 default: /* This shouldn't happen. */
5603 ctl_set_invalid_opcode(ctsio);
5604 ctl_done((union ctl_io *)ctsio);
5605 return (CTL_RETVAL_COMPLETE);
5606 }
5607
5608 if (buffer_offset > CTL_WRITE_BUFFER_SIZE ||
5609 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5610 ctl_set_invalid_field(ctsio,
5611 /*sks_valid*/ 1,
5612 /*command*/ 1,
5613 /*field*/ 6,
5614 /*bit_valid*/ 0,
5615 /*bit*/ 0);
5616 ctl_done((union ctl_io *)ctsio);
5617 return (CTL_RETVAL_COMPLETE);
5618 }
5619
5620 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5621 descr[0] = 0;
5622 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5623 ctsio->kern_data_ptr = descr;
5624 len = min(len, sizeof(descr));
5625 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5626 ctsio->kern_data_ptr = echo_descr;
5627 len = min(len, sizeof(echo_descr));
5628 } else {
5629 if (lun->write_buffer == NULL) {
5630 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5631 M_CTL, M_WAITOK | M_ZERO);
5632 }
5633 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5634 }
5635 ctsio->kern_data_len = len;
5636 ctsio->kern_total_len = len;
5637 ctsio->kern_rel_offset = 0;
5638 ctsio->kern_sg_entries = 0;
5639 ctl_set_success(ctsio);
5640 ctsio->be_move_done = ctl_config_move_done;
5641 ctl_datamove((union ctl_io *)ctsio);
5642 return (CTL_RETVAL_COMPLETE);
5643 }
5644
5645 int
ctl_write_buffer(struct ctl_scsiio * ctsio)5646 ctl_write_buffer(struct ctl_scsiio *ctsio)
5647 {
5648 struct ctl_lun *lun = CTL_LUN(ctsio);
5649 struct scsi_write_buffer *cdb;
5650 int buffer_offset, len;
5651
5652 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5653
5654 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5655
5656 len = scsi_3btoul(cdb->length);
5657 buffer_offset = scsi_3btoul(cdb->offset);
5658
5659 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5660 ctl_set_invalid_field(ctsio,
5661 /*sks_valid*/ 1,
5662 /*command*/ 1,
5663 /*field*/ 6,
5664 /*bit_valid*/ 0,
5665 /*bit*/ 0);
5666 ctl_done((union ctl_io *)ctsio);
5667 return (CTL_RETVAL_COMPLETE);
5668 }
5669
5670 if (lun->write_buffer == NULL) {
5671 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5672 M_CTL, M_WAITOK | M_ZERO);
5673 }
5674
5675 /*
5676 * If this kernel request hasn't started yet, initialize the data
5677 * buffer to the correct region of the LUN's write buffer. Note that
5678 * this doesn't set CTL_FLAG_ALLOCATED since this points into a
5679 * persistent buffer belonging to the LUN rather than a buffer
5680 * dedicated to this request.
5681 */
5682 if (ctsio->kern_data_ptr == NULL) {
5683 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5684 ctsio->kern_data_len = len;
5685 ctsio->kern_total_len = len;
5686 ctsio->kern_rel_offset = 0;
5687 ctsio->kern_sg_entries = 0;
5688 ctsio->be_move_done = ctl_config_move_done;
5689 ctl_datamove((union ctl_io *)ctsio);
5690
5691 return (CTL_RETVAL_COMPLETE);
5692 }
5693
5694 ctl_set_success(ctsio);
5695 ctl_done((union ctl_io *)ctsio);
5696 return (CTL_RETVAL_COMPLETE);
5697 }
5698
5699 static int
ctl_write_same_cont(union ctl_io * io)5700 ctl_write_same_cont(union ctl_io *io)
5701 {
5702 struct ctl_lun *lun = CTL_LUN(io);
5703 struct ctl_scsiio *ctsio;
5704 struct ctl_lba_len_flags *lbalen;
5705 int retval;
5706
5707 ctsio = &io->scsiio;
5708 ctsio->io_hdr.status = CTL_STATUS_NONE;
5709 lbalen = (struct ctl_lba_len_flags *)
5710 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5711 lbalen->lba += lbalen->len;
5712 if ((lun->be_lun->maxlba + 1) - lbalen->lba <= UINT32_MAX) {
5713 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
5714 lbalen->len = (lun->be_lun->maxlba + 1) - lbalen->lba;
5715 }
5716
5717 CTL_DEBUG_PRINT(("ctl_write_same_cont: calling config_write()\n"));
5718 retval = lun->backend->config_write((union ctl_io *)ctsio);
5719 return (retval);
5720 }
5721
5722 int
ctl_write_same(struct ctl_scsiio * ctsio)5723 ctl_write_same(struct ctl_scsiio *ctsio)
5724 {
5725 struct ctl_lun *lun = CTL_LUN(ctsio);
5726 struct ctl_lba_len_flags *lbalen;
5727 const char *val;
5728 uint64_t lba, ival;
5729 uint32_t num_blocks;
5730 int len, retval;
5731 uint8_t byte2;
5732
5733 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5734
5735 switch (ctsio->cdb[0]) {
5736 case WRITE_SAME_10: {
5737 struct scsi_write_same_10 *cdb;
5738
5739 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5740
5741 lba = scsi_4btoul(cdb->addr);
5742 num_blocks = scsi_2btoul(cdb->length);
5743 byte2 = cdb->byte2;
5744 break;
5745 }
5746 case WRITE_SAME_16: {
5747 struct scsi_write_same_16 *cdb;
5748
5749 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5750
5751 lba = scsi_8btou64(cdb->addr);
5752 num_blocks = scsi_4btoul(cdb->length);
5753 byte2 = cdb->byte2;
5754 break;
5755 }
5756 default:
5757 /*
5758 * We got a command we don't support. This shouldn't
5759 * happen, commands should be filtered out above us.
5760 */
5761 ctl_set_invalid_opcode(ctsio);
5762 ctl_done((union ctl_io *)ctsio);
5763
5764 return (CTL_RETVAL_COMPLETE);
5765 break; /* NOTREACHED */
5766 }
5767
5768 /* ANCHOR flag can be used only together with UNMAP */
5769 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) {
5770 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5771 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5772 ctl_done((union ctl_io *)ctsio);
5773 return (CTL_RETVAL_COMPLETE);
5774 }
5775
5776 /*
5777 * The first check is to make sure we're in bounds, the second
5778 * check is to catch wrap-around problems. If the lba + num blocks
5779 * is less than the lba, then we've wrapped around and the block
5780 * range is invalid anyway.
5781 */
5782 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5783 || ((lba + num_blocks) < lba)) {
5784 ctl_set_lba_out_of_range(ctsio,
5785 MAX(lba, lun->be_lun->maxlba + 1));
5786 ctl_done((union ctl_io *)ctsio);
5787 return (CTL_RETVAL_COMPLETE);
5788 }
5789
5790 /* Zero number of blocks means "to the last logical block" */
5791 if (num_blocks == 0) {
5792 ival = UINT64_MAX;
5793 val = dnvlist_get_string(lun->be_lun->options,
5794 "write_same_max_lba", NULL);
5795 if (val != NULL)
5796 ctl_expand_number(val, &ival);
5797 if ((lun->be_lun->maxlba + 1) - lba > ival) {
5798 ctl_set_invalid_field(ctsio,
5799 /*sks_valid*/ 1, /*command*/ 1,
5800 /*field*/ ctsio->cdb[0] == WRITE_SAME_10 ? 7 : 10,
5801 /*bit_valid*/ 0, /*bit*/ 0);
5802 ctl_done((union ctl_io *)ctsio);
5803 return (CTL_RETVAL_COMPLETE);
5804 }
5805 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5806 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
5807 ctsio->io_cont = ctl_write_same_cont;
5808 num_blocks = 1 << 31;
5809 } else
5810 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5811 }
5812
5813 len = lun->be_lun->blocksize;
5814
5815 /*
5816 * If we've got a kernel request that hasn't been malloced yet,
5817 * malloc it and tell the caller the data buffer is here.
5818 */
5819 if ((byte2 & SWS_NDOB) == 0 &&
5820 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5821 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5822 ctsio->kern_data_len = len;
5823 ctsio->kern_total_len = len;
5824 ctsio->kern_rel_offset = 0;
5825 ctsio->kern_sg_entries = 0;
5826 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5827 ctsio->be_move_done = ctl_config_move_done;
5828 ctl_datamove((union ctl_io *)ctsio);
5829
5830 return (CTL_RETVAL_COMPLETE);
5831 }
5832
5833 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5834 lbalen->lba = lba;
5835 lbalen->len = num_blocks;
5836 lbalen->flags = byte2;
5837 retval = lun->backend->config_write((union ctl_io *)ctsio);
5838
5839 return (retval);
5840 }
5841
5842 int
ctl_unmap(struct ctl_scsiio * ctsio)5843 ctl_unmap(struct ctl_scsiio *ctsio)
5844 {
5845 struct ctl_lun *lun = CTL_LUN(ctsio);
5846 struct scsi_unmap *cdb;
5847 struct ctl_ptr_len_flags *ptrlen;
5848 struct scsi_unmap_header *hdr;
5849 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5850 uint64_t lba;
5851 uint32_t num_blocks;
5852 int len, retval;
5853 uint8_t byte2;
5854
5855 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5856
5857 cdb = (struct scsi_unmap *)ctsio->cdb;
5858 len = scsi_2btoul(cdb->length);
5859 byte2 = cdb->byte2;
5860
5861 /*
5862 * If we've got a kernel request that hasn't been malloced yet,
5863 * malloc it and tell the caller the data buffer is here.
5864 */
5865 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5866 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5867 ctsio->kern_data_len = len;
5868 ctsio->kern_total_len = len;
5869 ctsio->kern_rel_offset = 0;
5870 ctsio->kern_sg_entries = 0;
5871 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5872 ctsio->be_move_done = ctl_config_move_done;
5873 ctl_datamove((union ctl_io *)ctsio);
5874
5875 return (CTL_RETVAL_COMPLETE);
5876 }
5877
5878 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5879 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5880 if (len < sizeof (*hdr) ||
5881 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5882 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5883 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5884 ctl_set_invalid_field(ctsio,
5885 /*sks_valid*/ 0,
5886 /*command*/ 0,
5887 /*field*/ 0,
5888 /*bit_valid*/ 0,
5889 /*bit*/ 0);
5890 goto done;
5891 }
5892 len = scsi_2btoul(hdr->desc_length);
5893 buf = (struct scsi_unmap_desc *)(hdr + 1);
5894 end = buf + len / sizeof(*buf);
5895
5896 endnz = buf;
5897 for (range = buf; range < end; range++) {
5898 lba = scsi_8btou64(range->lba);
5899 num_blocks = scsi_4btoul(range->length);
5900 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5901 || ((lba + num_blocks) < lba)) {
5902 ctl_set_lba_out_of_range(ctsio,
5903 MAX(lba, lun->be_lun->maxlba + 1));
5904 ctl_done((union ctl_io *)ctsio);
5905 return (CTL_RETVAL_COMPLETE);
5906 }
5907 if (num_blocks != 0)
5908 endnz = range + 1;
5909 }
5910
5911 /*
5912 * Block backend can not handle zero last range.
5913 * Filter it out and return if there is nothing left.
5914 */
5915 len = (uint8_t *)endnz - (uint8_t *)buf;
5916 if (len == 0) {
5917 ctl_set_success(ctsio);
5918 goto done;
5919 }
5920
5921 mtx_lock(&lun->lun_lock);
5922 ptrlen = (struct ctl_ptr_len_flags *)
5923 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5924 ptrlen->ptr = (void *)buf;
5925 ptrlen->len = len;
5926 ptrlen->flags = byte2;
5927 ctl_try_unblock_others(lun, (union ctl_io *)ctsio, FALSE);
5928 mtx_unlock(&lun->lun_lock);
5929
5930 retval = lun->backend->config_write((union ctl_io *)ctsio);
5931 return (retval);
5932
5933 done:
5934 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5935 free(ctsio->kern_data_ptr, M_CTL);
5936 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5937 }
5938 ctl_done((union ctl_io *)ctsio);
5939 return (CTL_RETVAL_COMPLETE);
5940 }
5941
5942 int
ctl_default_page_handler(struct ctl_scsiio * ctsio,struct ctl_page_index * page_index,uint8_t * page_ptr)5943 ctl_default_page_handler(struct ctl_scsiio *ctsio,
5944 struct ctl_page_index *page_index, uint8_t *page_ptr)
5945 {
5946 struct ctl_lun *lun = CTL_LUN(ctsio);
5947 uint8_t *current_cp;
5948 int set_ua;
5949 uint32_t initidx;
5950
5951 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5952 set_ua = 0;
5953
5954 current_cp = (page_index->page_data + (page_index->page_len *
5955 CTL_PAGE_CURRENT));
5956
5957 mtx_lock(&lun->lun_lock);
5958 if (memcmp(current_cp, page_ptr, page_index->page_len)) {
5959 memcpy(current_cp, page_ptr, page_index->page_len);
5960 set_ua = 1;
5961 }
5962 if (set_ua != 0)
5963 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5964 mtx_unlock(&lun->lun_lock);
5965 if (set_ua) {
5966 ctl_isc_announce_mode(lun,
5967 ctl_get_initindex(&ctsio->io_hdr.nexus),
5968 page_index->page_code, page_index->subpage);
5969 }
5970 return (CTL_RETVAL_COMPLETE);
5971 }
5972
5973 static void
ctl_ie_timer(void * arg)5974 ctl_ie_timer(void *arg)
5975 {
5976 struct ctl_lun *lun = arg;
5977 uint64_t t;
5978
5979 if (lun->ie_asc == 0)
5980 return;
5981
5982 if (lun->MODE_IE.mrie == SIEP_MRIE_UA)
5983 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5984 else
5985 lun->ie_reported = 0;
5986
5987 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) {
5988 lun->ie_reportcnt++;
5989 t = scsi_4btoul(lun->MODE_IE.interval_timer);
5990 if (t == 0 || t == UINT32_MAX)
5991 t = 3000; /* 5 min */
5992 callout_schedule_sbt(&lun->ie_callout, SBT_1S / 10 * t,
5993 SBT_1S / 10, 0);
5994 }
5995 }
5996
5997 int
ctl_ie_page_handler(struct ctl_scsiio * ctsio,struct ctl_page_index * page_index,uint8_t * page_ptr)5998 ctl_ie_page_handler(struct ctl_scsiio *ctsio,
5999 struct ctl_page_index *page_index, uint8_t *page_ptr)
6000 {
6001 struct ctl_lun *lun = CTL_LUN(ctsio);
6002 struct scsi_info_exceptions_page *pg;
6003 uint64_t t;
6004
6005 (void)ctl_default_page_handler(ctsio, page_index, page_ptr);
6006
6007 pg = (struct scsi_info_exceptions_page *)page_ptr;
6008 mtx_lock(&lun->lun_lock);
6009 if (pg->info_flags & SIEP_FLAGS_TEST) {
6010 lun->ie_asc = 0x5d;
6011 lun->ie_ascq = 0xff;
6012 if (pg->mrie == SIEP_MRIE_UA) {
6013 ctl_est_ua_all(lun, -1, CTL_UA_IE);
6014 lun->ie_reported = 1;
6015 } else {
6016 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
6017 lun->ie_reported = -1;
6018 }
6019 lun->ie_reportcnt = 1;
6020 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) {
6021 lun->ie_reportcnt++;
6022 t = scsi_4btoul(pg->interval_timer);
6023 if (t == 0 || t == UINT32_MAX)
6024 t = 3000; /* 5 min */
6025 callout_reset_sbt(&lun->ie_callout, SBT_1S / 10 * t,
6026 SBT_1S / 10, ctl_ie_timer, lun, 0);
6027 }
6028 } else {
6029 lun->ie_asc = 0;
6030 lun->ie_ascq = 0;
6031 lun->ie_reported = 1;
6032 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
6033 lun->ie_reportcnt = UINT32_MAX;
6034 callout_stop(&lun->ie_callout);
6035 }
6036 mtx_unlock(&lun->lun_lock);
6037 return (CTL_RETVAL_COMPLETE);
6038 }
6039
6040 static int
ctl_do_mode_select(union ctl_io * io)6041 ctl_do_mode_select(union ctl_io *io)
6042 {
6043 struct ctl_lun *lun = CTL_LUN(io);
6044 struct scsi_mode_page_header *page_header;
6045 struct ctl_page_index *page_index;
6046 struct ctl_scsiio *ctsio;
6047 int page_len, page_len_offset, page_len_size;
6048 union ctl_modepage_info *modepage_info;
6049 uint16_t *len_left, *len_used;
6050 int retval, i;
6051
6052 ctsio = &io->scsiio;
6053 page_index = NULL;
6054 page_len = 0;
6055
6056 modepage_info = (union ctl_modepage_info *)
6057 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6058 len_left = &modepage_info->header.len_left;
6059 len_used = &modepage_info->header.len_used;
6060
6061 do_next_page:
6062
6063 page_header = (struct scsi_mode_page_header *)
6064 (ctsio->kern_data_ptr + *len_used);
6065
6066 if (*len_left == 0) {
6067 free(ctsio->kern_data_ptr, M_CTL);
6068 ctl_set_success(ctsio);
6069 ctl_done((union ctl_io *)ctsio);
6070 return (CTL_RETVAL_COMPLETE);
6071 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6072 free(ctsio->kern_data_ptr, M_CTL);
6073 ctl_set_param_len_error(ctsio);
6074 ctl_done((union ctl_io *)ctsio);
6075 return (CTL_RETVAL_COMPLETE);
6076
6077 } else if ((page_header->page_code & SMPH_SPF)
6078 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6079 free(ctsio->kern_data_ptr, M_CTL);
6080 ctl_set_param_len_error(ctsio);
6081 ctl_done((union ctl_io *)ctsio);
6082 return (CTL_RETVAL_COMPLETE);
6083 }
6084
6085 /*
6086 * XXX KDM should we do something with the block descriptor?
6087 */
6088 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6089 page_index = &lun->mode_pages.index[i];
6090 if (lun->be_lun->lun_type == T_DIRECT &&
6091 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6092 continue;
6093 if (lun->be_lun->lun_type == T_PROCESSOR &&
6094 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6095 continue;
6096 if (lun->be_lun->lun_type == T_CDROM &&
6097 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6098 continue;
6099
6100 if ((page_index->page_code & SMPH_PC_MASK) !=
6101 (page_header->page_code & SMPH_PC_MASK))
6102 continue;
6103
6104 /*
6105 * If neither page has a subpage code, then we've got a
6106 * match.
6107 */
6108 if (((page_index->page_code & SMPH_SPF) == 0)
6109 && ((page_header->page_code & SMPH_SPF) == 0)) {
6110 page_len = page_header->page_length;
6111 break;
6112 }
6113
6114 /*
6115 * If both pages have subpages, then the subpage numbers
6116 * have to match.
6117 */
6118 if ((page_index->page_code & SMPH_SPF)
6119 && (page_header->page_code & SMPH_SPF)) {
6120 struct scsi_mode_page_header_sp *sph;
6121
6122 sph = (struct scsi_mode_page_header_sp *)page_header;
6123 if (page_index->subpage == sph->subpage) {
6124 page_len = scsi_2btoul(sph->page_length);
6125 break;
6126 }
6127 }
6128 }
6129
6130 /*
6131 * If we couldn't find the page, or if we don't have a mode select
6132 * handler for it, send back an error to the user.
6133 */
6134 if ((i >= CTL_NUM_MODE_PAGES)
6135 || (page_index->select_handler == NULL)) {
6136 ctl_set_invalid_field(ctsio,
6137 /*sks_valid*/ 1,
6138 /*command*/ 0,
6139 /*field*/ *len_used,
6140 /*bit_valid*/ 0,
6141 /*bit*/ 0);
6142 free(ctsio->kern_data_ptr, M_CTL);
6143 ctl_done((union ctl_io *)ctsio);
6144 return (CTL_RETVAL_COMPLETE);
6145 }
6146
6147 if (page_index->page_code & SMPH_SPF) {
6148 page_len_offset = 2;
6149 page_len_size = 2;
6150 } else {
6151 page_len_size = 1;
6152 page_len_offset = 1;
6153 }
6154
6155 /*
6156 * If the length the initiator gives us isn't the one we specify in
6157 * the mode page header, or if they didn't specify enough data in
6158 * the CDB to avoid truncating this page, kick out the request.
6159 */
6160 if (page_len != page_index->page_len - page_len_offset - page_len_size) {
6161 ctl_set_invalid_field(ctsio,
6162 /*sks_valid*/ 1,
6163 /*command*/ 0,
6164 /*field*/ *len_used + page_len_offset,
6165 /*bit_valid*/ 0,
6166 /*bit*/ 0);
6167 free(ctsio->kern_data_ptr, M_CTL);
6168 ctl_done((union ctl_io *)ctsio);
6169 return (CTL_RETVAL_COMPLETE);
6170 }
6171 if (*len_left < page_index->page_len) {
6172 free(ctsio->kern_data_ptr, M_CTL);
6173 ctl_set_param_len_error(ctsio);
6174 ctl_done((union ctl_io *)ctsio);
6175 return (CTL_RETVAL_COMPLETE);
6176 }
6177
6178 /*
6179 * Run through the mode page, checking to make sure that the bits
6180 * the user changed are actually legal for him to change.
6181 */
6182 for (i = 0; i < page_index->page_len; i++) {
6183 uint8_t *user_byte, *change_mask, *current_byte;
6184 int bad_bit;
6185 int j;
6186
6187 user_byte = (uint8_t *)page_header + i;
6188 change_mask = page_index->page_data +
6189 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6190 current_byte = page_index->page_data +
6191 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6192
6193 /*
6194 * Check to see whether the user set any bits in this byte
6195 * that he is not allowed to set.
6196 */
6197 if ((*user_byte & ~(*change_mask)) ==
6198 (*current_byte & ~(*change_mask)))
6199 continue;
6200
6201 /*
6202 * Go through bit by bit to determine which one is illegal.
6203 */
6204 bad_bit = 0;
6205 for (j = 7; j >= 0; j--) {
6206 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6207 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6208 bad_bit = i;
6209 break;
6210 }
6211 }
6212 ctl_set_invalid_field(ctsio,
6213 /*sks_valid*/ 1,
6214 /*command*/ 0,
6215 /*field*/ *len_used + i,
6216 /*bit_valid*/ 1,
6217 /*bit*/ bad_bit);
6218 free(ctsio->kern_data_ptr, M_CTL);
6219 ctl_done((union ctl_io *)ctsio);
6220 return (CTL_RETVAL_COMPLETE);
6221 }
6222
6223 /*
6224 * Decrement these before we call the page handler, since we may
6225 * end up getting called back one way or another before the handler
6226 * returns to this context.
6227 */
6228 *len_left -= page_index->page_len;
6229 *len_used += page_index->page_len;
6230
6231 retval = page_index->select_handler(ctsio, page_index,
6232 (uint8_t *)page_header);
6233
6234 /*
6235 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6236 * wait until this queued command completes to finish processing
6237 * the mode page. If it returns anything other than
6238 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6239 * already set the sense information, freed the data pointer, and
6240 * completed the io for us.
6241 */
6242 if (retval != CTL_RETVAL_COMPLETE)
6243 goto bailout_no_done;
6244
6245 /*
6246 * If the initiator sent us more than one page, parse the next one.
6247 */
6248 if (*len_left > 0)
6249 goto do_next_page;
6250
6251 ctl_set_success(ctsio);
6252 free(ctsio->kern_data_ptr, M_CTL);
6253 ctl_done((union ctl_io *)ctsio);
6254
6255 bailout_no_done:
6256
6257 return (CTL_RETVAL_COMPLETE);
6258
6259 }
6260
6261 int
ctl_mode_select(struct ctl_scsiio * ctsio)6262 ctl_mode_select(struct ctl_scsiio *ctsio)
6263 {
6264 struct ctl_lun *lun = CTL_LUN(ctsio);
6265 union ctl_modepage_info *modepage_info;
6266 int bd_len, i, header_size, param_len, rtd;
6267 uint32_t initidx;
6268
6269 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6270 switch (ctsio->cdb[0]) {
6271 case MODE_SELECT_6: {
6272 struct scsi_mode_select_6 *cdb;
6273
6274 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6275
6276 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6277 param_len = cdb->length;
6278 header_size = sizeof(struct scsi_mode_header_6);
6279 break;
6280 }
6281 case MODE_SELECT_10: {
6282 struct scsi_mode_select_10 *cdb;
6283
6284 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6285
6286 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6287 param_len = scsi_2btoul(cdb->length);
6288 header_size = sizeof(struct scsi_mode_header_10);
6289 break;
6290 }
6291 default:
6292 ctl_set_invalid_opcode(ctsio);
6293 ctl_done((union ctl_io *)ctsio);
6294 return (CTL_RETVAL_COMPLETE);
6295 }
6296
6297 if (rtd) {
6298 if (param_len != 0) {
6299 ctl_set_invalid_field(ctsio, /*sks_valid*/ 0,
6300 /*command*/ 1, /*field*/ 0,
6301 /*bit_valid*/ 0, /*bit*/ 0);
6302 ctl_done((union ctl_io *)ctsio);
6303 return (CTL_RETVAL_COMPLETE);
6304 }
6305
6306 /* Revert to defaults. */
6307 ctl_init_page_index(lun);
6308 mtx_lock(&lun->lun_lock);
6309 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
6310 mtx_unlock(&lun->lun_lock);
6311 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6312 ctl_isc_announce_mode(lun, -1,
6313 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
6314 lun->mode_pages.index[i].subpage);
6315 }
6316 ctl_set_success(ctsio);
6317 ctl_done((union ctl_io *)ctsio);
6318 return (CTL_RETVAL_COMPLETE);
6319 }
6320
6321 /*
6322 * From SPC-3:
6323 * "A parameter list length of zero indicates that the Data-Out Buffer
6324 * shall be empty. This condition shall not be considered as an error."
6325 */
6326 if (param_len == 0) {
6327 ctl_set_success(ctsio);
6328 ctl_done((union ctl_io *)ctsio);
6329 return (CTL_RETVAL_COMPLETE);
6330 }
6331
6332 /*
6333 * Since we'll hit this the first time through, prior to
6334 * allocation, we don't need to free a data buffer here.
6335 */
6336 if (param_len < header_size) {
6337 ctl_set_param_len_error(ctsio);
6338 ctl_done((union ctl_io *)ctsio);
6339 return (CTL_RETVAL_COMPLETE);
6340 }
6341
6342 /*
6343 * Allocate the data buffer and grab the user's data. In theory,
6344 * we shouldn't have to sanity check the parameter list length here
6345 * because the maximum size is 64K. We should be able to malloc
6346 * that much without too many problems.
6347 */
6348 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6349 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6350 ctsio->kern_data_len = param_len;
6351 ctsio->kern_total_len = param_len;
6352 ctsio->kern_rel_offset = 0;
6353 ctsio->kern_sg_entries = 0;
6354 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6355 ctsio->be_move_done = ctl_config_move_done;
6356 ctl_datamove((union ctl_io *)ctsio);
6357
6358 return (CTL_RETVAL_COMPLETE);
6359 }
6360
6361 switch (ctsio->cdb[0]) {
6362 case MODE_SELECT_6: {
6363 struct scsi_mode_header_6 *mh6;
6364
6365 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6366 bd_len = mh6->blk_desc_len;
6367 break;
6368 }
6369 case MODE_SELECT_10: {
6370 struct scsi_mode_header_10 *mh10;
6371
6372 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6373 bd_len = scsi_2btoul(mh10->blk_desc_len);
6374 break;
6375 }
6376 default:
6377 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6378 }
6379
6380 if (param_len < (header_size + bd_len)) {
6381 free(ctsio->kern_data_ptr, M_CTL);
6382 ctl_set_param_len_error(ctsio);
6383 ctl_done((union ctl_io *)ctsio);
6384 return (CTL_RETVAL_COMPLETE);
6385 }
6386
6387 /*
6388 * Set the IO_CONT flag, so that if this I/O gets passed to
6389 * ctl_config_write_done(), it'll get passed back to
6390 * ctl_do_mode_select() for further processing, or completion if
6391 * we're all done.
6392 */
6393 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6394 ctsio->io_cont = ctl_do_mode_select;
6395
6396 modepage_info = (union ctl_modepage_info *)
6397 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6398 memset(modepage_info, 0, sizeof(*modepage_info));
6399 modepage_info->header.len_left = param_len - header_size - bd_len;
6400 modepage_info->header.len_used = header_size + bd_len;
6401
6402 return (ctl_do_mode_select((union ctl_io *)ctsio));
6403 }
6404
6405 int
ctl_mode_sense(struct ctl_scsiio * ctsio)6406 ctl_mode_sense(struct ctl_scsiio *ctsio)
6407 {
6408 struct ctl_lun *lun = CTL_LUN(ctsio);
6409 int pc, page_code, llba, subpage;
6410 int alloc_len, page_len, header_len, bd_len, total_len;
6411 void *block_desc;
6412 struct ctl_page_index *page_index;
6413
6414 llba = 0;
6415
6416 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6417
6418 switch (ctsio->cdb[0]) {
6419 case MODE_SENSE_6: {
6420 struct scsi_mode_sense_6 *cdb;
6421
6422 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6423
6424 header_len = sizeof(struct scsi_mode_hdr_6);
6425 if (cdb->byte2 & SMS_DBD)
6426 bd_len = 0;
6427 else
6428 bd_len = sizeof(struct scsi_mode_block_descr);
6429 header_len += bd_len;
6430
6431 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6432 page_code = cdb->page & SMS_PAGE_CODE;
6433 subpage = cdb->subpage;
6434 alloc_len = cdb->length;
6435 break;
6436 }
6437 case MODE_SENSE_10: {
6438 struct scsi_mode_sense_10 *cdb;
6439
6440 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6441
6442 header_len = sizeof(struct scsi_mode_hdr_10);
6443 if (cdb->byte2 & SMS_DBD) {
6444 bd_len = 0;
6445 } else if (lun->be_lun->lun_type == T_DIRECT) {
6446 if (cdb->byte2 & SMS10_LLBAA) {
6447 llba = 1;
6448 bd_len = sizeof(struct scsi_mode_block_descr_dlong);
6449 } else
6450 bd_len = sizeof(struct scsi_mode_block_descr_dshort);
6451 } else
6452 bd_len = sizeof(struct scsi_mode_block_descr);
6453 header_len += bd_len;
6454
6455 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6456 page_code = cdb->page & SMS_PAGE_CODE;
6457 subpage = cdb->subpage;
6458 alloc_len = scsi_2btoul(cdb->length);
6459 break;
6460 }
6461 default:
6462 ctl_set_invalid_opcode(ctsio);
6463 ctl_done((union ctl_io *)ctsio);
6464 return (CTL_RETVAL_COMPLETE);
6465 break; /* NOTREACHED */
6466 }
6467
6468 /*
6469 * We have to make a first pass through to calculate the size of
6470 * the pages that match the user's query. Then we allocate enough
6471 * memory to hold it, and actually copy the data into the buffer.
6472 */
6473 switch (page_code) {
6474 case SMS_ALL_PAGES_PAGE: {
6475 u_int i;
6476
6477 page_len = 0;
6478
6479 /*
6480 * At the moment, values other than 0 and 0xff here are
6481 * reserved according to SPC-3.
6482 */
6483 if ((subpage != SMS_SUBPAGE_PAGE_0)
6484 && (subpage != SMS_SUBPAGE_ALL)) {
6485 ctl_set_invalid_field(ctsio,
6486 /*sks_valid*/ 1,
6487 /*command*/ 1,
6488 /*field*/ 3,
6489 /*bit_valid*/ 0,
6490 /*bit*/ 0);
6491 ctl_done((union ctl_io *)ctsio);
6492 return (CTL_RETVAL_COMPLETE);
6493 }
6494
6495 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6496 page_index = &lun->mode_pages.index[i];
6497
6498 /* Make sure the page is supported for this dev type */
6499 if (lun->be_lun->lun_type == T_DIRECT &&
6500 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6501 continue;
6502 if (lun->be_lun->lun_type == T_PROCESSOR &&
6503 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6504 continue;
6505 if (lun->be_lun->lun_type == T_CDROM &&
6506 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6507 continue;
6508
6509 /*
6510 * We don't use this subpage if the user didn't
6511 * request all subpages.
6512 */
6513 if ((page_index->subpage != 0)
6514 && (subpage == SMS_SUBPAGE_PAGE_0))
6515 continue;
6516
6517 page_len += page_index->page_len;
6518 }
6519 break;
6520 }
6521 default: {
6522 u_int i;
6523
6524 page_len = 0;
6525
6526 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6527 page_index = &lun->mode_pages.index[i];
6528
6529 /* Make sure the page is supported for this dev type */
6530 if (lun->be_lun->lun_type == T_DIRECT &&
6531 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6532 continue;
6533 if (lun->be_lun->lun_type == T_PROCESSOR &&
6534 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6535 continue;
6536 if (lun->be_lun->lun_type == T_CDROM &&
6537 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6538 continue;
6539
6540 /* Look for the right page code */
6541 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6542 continue;
6543
6544 /* Look for the right subpage or the subpage wildcard*/
6545 if ((page_index->subpage != subpage)
6546 && (subpage != SMS_SUBPAGE_ALL))
6547 continue;
6548
6549 page_len += page_index->page_len;
6550 }
6551
6552 if (page_len == 0) {
6553 ctl_set_invalid_field(ctsio,
6554 /*sks_valid*/ 1,
6555 /*command*/ 1,
6556 /*field*/ 2,
6557 /*bit_valid*/ 1,
6558 /*bit*/ 5);
6559 ctl_done((union ctl_io *)ctsio);
6560 return (CTL_RETVAL_COMPLETE);
6561 }
6562 break;
6563 }
6564 }
6565
6566 total_len = header_len + page_len;
6567
6568 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6569 ctsio->kern_sg_entries = 0;
6570 ctsio->kern_rel_offset = 0;
6571 ctsio->kern_data_len = min(total_len, alloc_len);
6572 ctsio->kern_total_len = ctsio->kern_data_len;
6573
6574 switch (ctsio->cdb[0]) {
6575 case MODE_SENSE_6: {
6576 struct scsi_mode_hdr_6 *header;
6577
6578 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6579
6580 header->datalen = MIN(total_len - 1, 254);
6581 if (lun->be_lun->lun_type == T_DIRECT) {
6582 header->dev_specific = 0x10; /* DPOFUA */
6583 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6584 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6585 header->dev_specific |= 0x80; /* WP */
6586 }
6587 header->block_descr_len = bd_len;
6588 block_desc = &header[1];
6589 break;
6590 }
6591 case MODE_SENSE_10: {
6592 struct scsi_mode_hdr_10 *header;
6593 int datalen;
6594
6595 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6596
6597 datalen = MIN(total_len - 2, 65533);
6598 scsi_ulto2b(datalen, header->datalen);
6599 if (lun->be_lun->lun_type == T_DIRECT) {
6600 header->dev_specific = 0x10; /* DPOFUA */
6601 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6602 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6603 header->dev_specific |= 0x80; /* WP */
6604 }
6605 if (llba)
6606 header->flags |= SMH_LONGLBA;
6607 scsi_ulto2b(bd_len, header->block_descr_len);
6608 block_desc = &header[1];
6609 break;
6610 }
6611 default:
6612 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6613 }
6614
6615 /*
6616 * If we've got a disk, use its blocksize in the block
6617 * descriptor. Otherwise, just set it to 0.
6618 */
6619 if (bd_len > 0) {
6620 if (lun->be_lun->lun_type == T_DIRECT) {
6621 if (llba) {
6622 struct scsi_mode_block_descr_dlong *bd = block_desc;
6623 if (lun->be_lun->maxlba != 0)
6624 scsi_u64to8b(lun->be_lun->maxlba + 1,
6625 bd->num_blocks);
6626 scsi_ulto4b(lun->be_lun->blocksize,
6627 bd->block_len);
6628 } else {
6629 struct scsi_mode_block_descr_dshort *bd = block_desc;
6630 if (lun->be_lun->maxlba != 0)
6631 scsi_ulto4b(MIN(lun->be_lun->maxlba+1,
6632 UINT32_MAX), bd->num_blocks);
6633 scsi_ulto3b(lun->be_lun->blocksize,
6634 bd->block_len);
6635 }
6636 } else {
6637 struct scsi_mode_block_descr *bd = block_desc;
6638 scsi_ulto3b(0, bd->block_len);
6639 }
6640 }
6641
6642 switch (page_code) {
6643 case SMS_ALL_PAGES_PAGE: {
6644 int i, data_used;
6645
6646 data_used = header_len;
6647 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6648 struct ctl_page_index *page_index;
6649
6650 page_index = &lun->mode_pages.index[i];
6651 if (lun->be_lun->lun_type == T_DIRECT &&
6652 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6653 continue;
6654 if (lun->be_lun->lun_type == T_PROCESSOR &&
6655 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6656 continue;
6657 if (lun->be_lun->lun_type == T_CDROM &&
6658 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6659 continue;
6660
6661 /*
6662 * We don't use this subpage if the user didn't
6663 * request all subpages. We already checked (above)
6664 * to make sure the user only specified a subpage
6665 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6666 */
6667 if ((page_index->subpage != 0)
6668 && (subpage == SMS_SUBPAGE_PAGE_0))
6669 continue;
6670
6671 /*
6672 * Call the handler, if it exists, to update the
6673 * page to the latest values.
6674 */
6675 if (page_index->sense_handler != NULL)
6676 page_index->sense_handler(ctsio, page_index,pc);
6677
6678 memcpy(ctsio->kern_data_ptr + data_used,
6679 page_index->page_data +
6680 (page_index->page_len * pc),
6681 page_index->page_len);
6682 data_used += page_index->page_len;
6683 }
6684 break;
6685 }
6686 default: {
6687 int i, data_used;
6688
6689 data_used = header_len;
6690
6691 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6692 struct ctl_page_index *page_index;
6693
6694 page_index = &lun->mode_pages.index[i];
6695
6696 /* Look for the right page code */
6697 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6698 continue;
6699
6700 /* Look for the right subpage or the subpage wildcard*/
6701 if ((page_index->subpage != subpage)
6702 && (subpage != SMS_SUBPAGE_ALL))
6703 continue;
6704
6705 /* Make sure the page is supported for this dev type */
6706 if (lun->be_lun->lun_type == T_DIRECT &&
6707 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6708 continue;
6709 if (lun->be_lun->lun_type == T_PROCESSOR &&
6710 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6711 continue;
6712 if (lun->be_lun->lun_type == T_CDROM &&
6713 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6714 continue;
6715
6716 /*
6717 * Call the handler, if it exists, to update the
6718 * page to the latest values.
6719 */
6720 if (page_index->sense_handler != NULL)
6721 page_index->sense_handler(ctsio, page_index,pc);
6722
6723 memcpy(ctsio->kern_data_ptr + data_used,
6724 page_index->page_data +
6725 (page_index->page_len * pc),
6726 page_index->page_len);
6727 data_used += page_index->page_len;
6728 }
6729 break;
6730 }
6731 }
6732
6733 ctl_set_success(ctsio);
6734 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6735 ctsio->be_move_done = ctl_config_move_done;
6736 ctl_datamove((union ctl_io *)ctsio);
6737 return (CTL_RETVAL_COMPLETE);
6738 }
6739
6740 int
ctl_temp_log_sense_handler(struct ctl_scsiio * ctsio,struct ctl_page_index * page_index,int pc)6741 ctl_temp_log_sense_handler(struct ctl_scsiio *ctsio,
6742 struct ctl_page_index *page_index,
6743 int pc)
6744 {
6745 struct ctl_lun *lun = CTL_LUN(ctsio);
6746 struct scsi_log_temperature *data;
6747 const char *value;
6748
6749 data = (struct scsi_log_temperature *)page_index->page_data;
6750
6751 scsi_ulto2b(SLP_TEMPERATURE, data->hdr.param_code);
6752 data->hdr.param_control = SLP_LBIN;
6753 data->hdr.param_len = sizeof(struct scsi_log_temperature) -
6754 sizeof(struct scsi_log_param_header);
6755 if ((value = dnvlist_get_string(lun->be_lun->options, "temperature",
6756 NULL)) != NULL)
6757 data->temperature = strtol(value, NULL, 0);
6758 else
6759 data->temperature = 0xff;
6760 data++;
6761
6762 scsi_ulto2b(SLP_REFTEMPERATURE, data->hdr.param_code);
6763 data->hdr.param_control = SLP_LBIN;
6764 data->hdr.param_len = sizeof(struct scsi_log_temperature) -
6765 sizeof(struct scsi_log_param_header);
6766 if ((value = dnvlist_get_string(lun->be_lun->options, "reftemperature",
6767 NULL)) != NULL)
6768 data->temperature = strtol(value, NULL, 0);
6769 else
6770 data->temperature = 0xff;
6771 return (0);
6772 }
6773
6774 int
ctl_lbp_log_sense_handler(struct ctl_scsiio * ctsio,struct ctl_page_index * page_index,int pc)6775 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6776 struct ctl_page_index *page_index,
6777 int pc)
6778 {
6779 struct ctl_lun *lun = CTL_LUN(ctsio);
6780 struct scsi_log_param_header *phdr;
6781 uint8_t *data;
6782 uint64_t val;
6783
6784 data = page_index->page_data;
6785
6786 if (lun->backend->lun_attr != NULL &&
6787 (val = lun->backend->lun_attr(lun->be_lun, "blocksavail"))
6788 != UINT64_MAX) {
6789 phdr = (struct scsi_log_param_header *)data;
6790 scsi_ulto2b(0x0001, phdr->param_code);
6791 phdr->param_control = SLP_LBIN | SLP_LP;
6792 phdr->param_len = 8;
6793 data = (uint8_t *)(phdr + 1);
6794 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6795 data[4] = 0x02; /* per-pool */
6796 data += phdr->param_len;
6797 }
6798
6799 if (lun->backend->lun_attr != NULL &&
6800 (val = lun->backend->lun_attr(lun->be_lun, "blocksused"))
6801 != UINT64_MAX) {
6802 phdr = (struct scsi_log_param_header *)data;
6803 scsi_ulto2b(0x0002, phdr->param_code);
6804 phdr->param_control = SLP_LBIN | SLP_LP;
6805 phdr->param_len = 8;
6806 data = (uint8_t *)(phdr + 1);
6807 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6808 data[4] = 0x01; /* per-LUN */
6809 data += phdr->param_len;
6810 }
6811
6812 if (lun->backend->lun_attr != NULL &&
6813 (val = lun->backend->lun_attr(lun->be_lun, "poolblocksavail"))
6814 != UINT64_MAX) {
6815 phdr = (struct scsi_log_param_header *)data;
6816 scsi_ulto2b(0x00f1, phdr->param_code);
6817 phdr->param_control = SLP_LBIN | SLP_LP;
6818 phdr->param_len = 8;
6819 data = (uint8_t *)(phdr + 1);
6820 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6821 data[4] = 0x02; /* per-pool */
6822 data += phdr->param_len;
6823 }
6824
6825 if (lun->backend->lun_attr != NULL &&
6826 (val = lun->backend->lun_attr(lun->be_lun, "poolblocksused"))
6827 != UINT64_MAX) {
6828 phdr = (struct scsi_log_param_header *)data;
6829 scsi_ulto2b(0x00f2, phdr->param_code);
6830 phdr->param_control = SLP_LBIN | SLP_LP;
6831 phdr->param_len = 8;
6832 data = (uint8_t *)(phdr + 1);
6833 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6834 data[4] = 0x02; /* per-pool */
6835 data += phdr->param_len;
6836 }
6837
6838 page_index->page_len = data - page_index->page_data;
6839 return (0);
6840 }
6841
6842 int
ctl_sap_log_sense_handler(struct ctl_scsiio * ctsio,struct ctl_page_index * page_index,int pc)6843 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6844 struct ctl_page_index *page_index,
6845 int pc)
6846 {
6847 struct ctl_lun *lun = CTL_LUN(ctsio);
6848 struct stat_page *data;
6849 struct bintime *t;
6850
6851 data = (struct stat_page *)page_index->page_data;
6852
6853 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6854 data->sap.hdr.param_control = SLP_LBIN;
6855 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6856 sizeof(struct scsi_log_param_header);
6857 scsi_u64to8b(lun->stats.operations[CTL_STATS_READ],
6858 data->sap.read_num);
6859 scsi_u64to8b(lun->stats.operations[CTL_STATS_WRITE],
6860 data->sap.write_num);
6861 if (lun->be_lun->blocksize > 0) {
6862 scsi_u64to8b(lun->stats.bytes[CTL_STATS_WRITE] /
6863 lun->be_lun->blocksize, data->sap.recvieved_lba);
6864 scsi_u64to8b(lun->stats.bytes[CTL_STATS_READ] /
6865 lun->be_lun->blocksize, data->sap.transmitted_lba);
6866 }
6867 t = &lun->stats.time[CTL_STATS_READ];
6868 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6869 data->sap.read_int);
6870 t = &lun->stats.time[CTL_STATS_WRITE];
6871 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6872 data->sap.write_int);
6873 scsi_u64to8b(0, data->sap.weighted_num);
6874 scsi_u64to8b(0, data->sap.weighted_int);
6875 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6876 data->it.hdr.param_control = SLP_LBIN;
6877 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6878 sizeof(struct scsi_log_param_header);
6879 #ifdef CTL_TIME_IO
6880 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6881 #endif
6882 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6883 data->it.hdr.param_control = SLP_LBIN;
6884 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6885 sizeof(struct scsi_log_param_header);
6886 scsi_ulto4b(3, data->ti.exponent);
6887 scsi_ulto4b(1, data->ti.integer);
6888 return (0);
6889 }
6890
6891 int
ctl_ie_log_sense_handler(struct ctl_scsiio * ctsio,struct ctl_page_index * page_index,int pc)6892 ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio,
6893 struct ctl_page_index *page_index,
6894 int pc)
6895 {
6896 struct ctl_lun *lun = CTL_LUN(ctsio);
6897 struct scsi_log_informational_exceptions *data;
6898 const char *value;
6899
6900 data = (struct scsi_log_informational_exceptions *)page_index->page_data;
6901
6902 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code);
6903 data->hdr.param_control = SLP_LBIN;
6904 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) -
6905 sizeof(struct scsi_log_param_header);
6906 data->ie_asc = lun->ie_asc;
6907 data->ie_ascq = lun->ie_ascq;
6908 if ((value = dnvlist_get_string(lun->be_lun->options, "temperature",
6909 NULL)) != NULL)
6910 data->temperature = strtol(value, NULL, 0);
6911 else
6912 data->temperature = 0xff;
6913 return (0);
6914 }
6915
6916 int
ctl_log_sense(struct ctl_scsiio * ctsio)6917 ctl_log_sense(struct ctl_scsiio *ctsio)
6918 {
6919 struct ctl_lun *lun = CTL_LUN(ctsio);
6920 int i, pc, page_code, subpage;
6921 int alloc_len, total_len;
6922 struct ctl_page_index *page_index;
6923 struct scsi_log_sense *cdb;
6924 struct scsi_log_header *header;
6925
6926 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6927
6928 cdb = (struct scsi_log_sense *)ctsio->cdb;
6929 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6930 page_code = cdb->page & SLS_PAGE_CODE;
6931 subpage = cdb->subpage;
6932 alloc_len = scsi_2btoul(cdb->length);
6933
6934 page_index = NULL;
6935 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6936 page_index = &lun->log_pages.index[i];
6937
6938 /* Look for the right page code */
6939 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6940 continue;
6941
6942 /* Look for the right subpage or the subpage wildcard*/
6943 if (page_index->subpage != subpage)
6944 continue;
6945
6946 break;
6947 }
6948 if (i >= CTL_NUM_LOG_PAGES) {
6949 ctl_set_invalid_field(ctsio,
6950 /*sks_valid*/ 1,
6951 /*command*/ 1,
6952 /*field*/ 2,
6953 /*bit_valid*/ 0,
6954 /*bit*/ 0);
6955 ctl_done((union ctl_io *)ctsio);
6956 return (CTL_RETVAL_COMPLETE);
6957 }
6958
6959 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6960
6961 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6962 ctsio->kern_sg_entries = 0;
6963 ctsio->kern_rel_offset = 0;
6964 ctsio->kern_data_len = min(total_len, alloc_len);
6965 ctsio->kern_total_len = ctsio->kern_data_len;
6966
6967 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6968 header->page = page_index->page_code;
6969 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING)
6970 header->page |= SL_DS;
6971 if (page_index->subpage) {
6972 header->page |= SL_SPF;
6973 header->subpage = page_index->subpage;
6974 }
6975 scsi_ulto2b(page_index->page_len, header->datalen);
6976
6977 /*
6978 * Call the handler, if it exists, to update the
6979 * page to the latest values.
6980 */
6981 if (page_index->sense_handler != NULL)
6982 page_index->sense_handler(ctsio, page_index, pc);
6983
6984 memcpy(header + 1, page_index->page_data, page_index->page_len);
6985
6986 ctl_set_success(ctsio);
6987 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6988 ctsio->be_move_done = ctl_config_move_done;
6989 ctl_datamove((union ctl_io *)ctsio);
6990 return (CTL_RETVAL_COMPLETE);
6991 }
6992
6993 int
ctl_read_capacity(struct ctl_scsiio * ctsio)6994 ctl_read_capacity(struct ctl_scsiio *ctsio)
6995 {
6996 struct ctl_lun *lun = CTL_LUN(ctsio);
6997 struct scsi_read_capacity *cdb;
6998 struct scsi_read_capacity_data *data;
6999 uint32_t lba;
7000
7001 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
7002
7003 cdb = (struct scsi_read_capacity *)ctsio->cdb;
7004
7005 lba = scsi_4btoul(cdb->addr);
7006 if (((cdb->pmi & SRC_PMI) == 0)
7007 && (lba != 0)) {
7008 ctl_set_invalid_field(/*ctsio*/ ctsio,
7009 /*sks_valid*/ 1,
7010 /*command*/ 1,
7011 /*field*/ 2,
7012 /*bit_valid*/ 0,
7013 /*bit*/ 0);
7014 ctl_done((union ctl_io *)ctsio);
7015 return (CTL_RETVAL_COMPLETE);
7016 }
7017
7018 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7019 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
7020 ctsio->kern_data_len = sizeof(*data);
7021 ctsio->kern_total_len = sizeof(*data);
7022 ctsio->kern_rel_offset = 0;
7023 ctsio->kern_sg_entries = 0;
7024
7025 /*
7026 * If the maximum LBA is greater than 0xfffffffe, the user must
7027 * issue a SERVICE ACTION IN (16) command, with the read capacity
7028 * serivce action set.
7029 */
7030 if (lun->be_lun->maxlba > 0xfffffffe)
7031 scsi_ulto4b(0xffffffff, data->addr);
7032 else
7033 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
7034
7035 /*
7036 * XXX KDM this may not be 512 bytes...
7037 */
7038 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7039
7040 ctl_set_success(ctsio);
7041 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7042 ctsio->be_move_done = ctl_config_move_done;
7043 ctl_datamove((union ctl_io *)ctsio);
7044 return (CTL_RETVAL_COMPLETE);
7045 }
7046
7047 int
ctl_read_capacity_16(struct ctl_scsiio * ctsio)7048 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
7049 {
7050 struct ctl_lun *lun = CTL_LUN(ctsio);
7051 struct scsi_read_capacity_16 *cdb;
7052 struct scsi_read_capacity_data_long *data;
7053 uint64_t lba;
7054 uint32_t alloc_len;
7055
7056 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7057
7058 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7059
7060 alloc_len = scsi_4btoul(cdb->alloc_len);
7061 lba = scsi_8btou64(cdb->addr);
7062
7063 if ((cdb->reladr & SRC16_PMI)
7064 && (lba != 0)) {
7065 ctl_set_invalid_field(/*ctsio*/ ctsio,
7066 /*sks_valid*/ 1,
7067 /*command*/ 1,
7068 /*field*/ 2,
7069 /*bit_valid*/ 0,
7070 /*bit*/ 0);
7071 ctl_done((union ctl_io *)ctsio);
7072 return (CTL_RETVAL_COMPLETE);
7073 }
7074
7075 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7076 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7077 ctsio->kern_rel_offset = 0;
7078 ctsio->kern_sg_entries = 0;
7079 ctsio->kern_data_len = min(sizeof(*data), alloc_len);
7080 ctsio->kern_total_len = ctsio->kern_data_len;
7081
7082 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7083 /* XXX KDM this may not be 512 bytes... */
7084 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7085 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7086 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7087 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7088 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
7089
7090 ctl_set_success(ctsio);
7091 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7092 ctsio->be_move_done = ctl_config_move_done;
7093 ctl_datamove((union ctl_io *)ctsio);
7094 return (CTL_RETVAL_COMPLETE);
7095 }
7096
7097 int
ctl_get_lba_status(struct ctl_scsiio * ctsio)7098 ctl_get_lba_status(struct ctl_scsiio *ctsio)
7099 {
7100 struct ctl_lun *lun = CTL_LUN(ctsio);
7101 struct scsi_get_lba_status *cdb;
7102 struct scsi_get_lba_status_data *data;
7103 struct ctl_lba_len_flags *lbalen;
7104 uint64_t lba;
7105 uint32_t alloc_len, total_len;
7106 int retval;
7107
7108 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
7109
7110 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
7111 lba = scsi_8btou64(cdb->addr);
7112 alloc_len = scsi_4btoul(cdb->alloc_len);
7113
7114 if (lba > lun->be_lun->maxlba) {
7115 ctl_set_lba_out_of_range(ctsio, lba);
7116 ctl_done((union ctl_io *)ctsio);
7117 return (CTL_RETVAL_COMPLETE);
7118 }
7119
7120 total_len = sizeof(*data) + sizeof(data->descr[0]);
7121 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7122 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
7123 ctsio->kern_rel_offset = 0;
7124 ctsio->kern_sg_entries = 0;
7125 ctsio->kern_data_len = min(total_len, alloc_len);
7126 ctsio->kern_total_len = ctsio->kern_data_len;
7127
7128 /* Fill dummy data in case backend can't tell anything. */
7129 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
7130 scsi_u64to8b(lba, data->descr[0].addr);
7131 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
7132 data->descr[0].length);
7133 data->descr[0].status = 0; /* Mapped or unknown. */
7134
7135 ctl_set_success(ctsio);
7136 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7137 ctsio->be_move_done = ctl_config_move_done;
7138
7139 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7140 lbalen->lba = lba;
7141 lbalen->len = total_len;
7142 lbalen->flags = 0;
7143 retval = lun->backend->config_read((union ctl_io *)ctsio);
7144 return (retval);
7145 }
7146
7147 int
ctl_read_defect(struct ctl_scsiio * ctsio)7148 ctl_read_defect(struct ctl_scsiio *ctsio)
7149 {
7150 struct scsi_read_defect_data_10 *ccb10;
7151 struct scsi_read_defect_data_12 *ccb12;
7152 struct scsi_read_defect_data_hdr_10 *data10;
7153 struct scsi_read_defect_data_hdr_12 *data12;
7154 uint32_t alloc_len, data_len;
7155 uint8_t format;
7156
7157 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7158
7159 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7160 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7161 format = ccb10->format;
7162 alloc_len = scsi_2btoul(ccb10->alloc_length);
7163 data_len = sizeof(*data10);
7164 } else {
7165 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7166 format = ccb12->format;
7167 alloc_len = scsi_4btoul(ccb12->alloc_length);
7168 data_len = sizeof(*data12);
7169 }
7170 if (alloc_len == 0) {
7171 ctl_set_success(ctsio);
7172 ctl_done((union ctl_io *)ctsio);
7173 return (CTL_RETVAL_COMPLETE);
7174 }
7175
7176 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7177 ctsio->kern_rel_offset = 0;
7178 ctsio->kern_sg_entries = 0;
7179 ctsio->kern_data_len = min(data_len, alloc_len);
7180 ctsio->kern_total_len = ctsio->kern_data_len;
7181
7182 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7183 data10 = (struct scsi_read_defect_data_hdr_10 *)
7184 ctsio->kern_data_ptr;
7185 data10->format = format;
7186 scsi_ulto2b(0, data10->length);
7187 } else {
7188 data12 = (struct scsi_read_defect_data_hdr_12 *)
7189 ctsio->kern_data_ptr;
7190 data12->format = format;
7191 scsi_ulto2b(0, data12->generation);
7192 scsi_ulto4b(0, data12->length);
7193 }
7194
7195 ctl_set_success(ctsio);
7196 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7197 ctsio->be_move_done = ctl_config_move_done;
7198 ctl_datamove((union ctl_io *)ctsio);
7199 return (CTL_RETVAL_COMPLETE);
7200 }
7201
7202 int
ctl_report_ident_info(struct ctl_scsiio * ctsio)7203 ctl_report_ident_info(struct ctl_scsiio *ctsio)
7204 {
7205 struct ctl_lun *lun = CTL_LUN(ctsio);
7206 struct scsi_report_ident_info *cdb;
7207 struct scsi_report_ident_info_data *rii_ptr;
7208 struct scsi_report_ident_info_descr *riid_ptr;
7209 const char *oii, *otii;
7210 int retval, alloc_len, total_len = 0, len = 0;
7211
7212 CTL_DEBUG_PRINT(("ctl_report_ident_info\n"));
7213
7214 cdb = (struct scsi_report_ident_info *)ctsio->cdb;
7215 retval = CTL_RETVAL_COMPLETE;
7216
7217 total_len = sizeof(struct scsi_report_ident_info_data);
7218 switch (cdb->type) {
7219 case RII_LUII:
7220 oii = dnvlist_get_string(lun->be_lun->options,
7221 "ident_info", NULL);
7222 if (oii)
7223 len = strlen(oii); /* Approximately */
7224 break;
7225 case RII_LUTII:
7226 otii = dnvlist_get_string(lun->be_lun->options,
7227 "text_ident_info", NULL);
7228 if (otii)
7229 len = strlen(otii) + 1; /* NULL-terminated */
7230 break;
7231 case RII_IIS:
7232 len = 2 * sizeof(struct scsi_report_ident_info_descr);
7233 break;
7234 default:
7235 ctl_set_invalid_field(/*ctsio*/ ctsio,
7236 /*sks_valid*/ 1,
7237 /*command*/ 1,
7238 /*field*/ 11,
7239 /*bit_valid*/ 1,
7240 /*bit*/ 2);
7241 ctl_done((union ctl_io *)ctsio);
7242 return(retval);
7243 }
7244 total_len += len;
7245 alloc_len = scsi_4btoul(cdb->length);
7246
7247 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7248 ctsio->kern_sg_entries = 0;
7249 ctsio->kern_rel_offset = 0;
7250 ctsio->kern_data_len = min(total_len, alloc_len);
7251 ctsio->kern_total_len = ctsio->kern_data_len;
7252
7253 rii_ptr = (struct scsi_report_ident_info_data *)ctsio->kern_data_ptr;
7254 switch (cdb->type) {
7255 case RII_LUII:
7256 if (oii) {
7257 if (oii[0] == '0' && oii[1] == 'x')
7258 len = hex2bin(oii, (uint8_t *)(rii_ptr + 1), len);
7259 else
7260 strncpy((uint8_t *)(rii_ptr + 1), oii, len);
7261 }
7262 break;
7263 case RII_LUTII:
7264 if (otii)
7265 strlcpy((uint8_t *)(rii_ptr + 1), otii, len);
7266 break;
7267 case RII_IIS:
7268 riid_ptr = (struct scsi_report_ident_info_descr *)(rii_ptr + 1);
7269 riid_ptr->type = RII_LUII;
7270 scsi_ulto2b(0xffff, riid_ptr->length);
7271 riid_ptr++;
7272 riid_ptr->type = RII_LUTII;
7273 scsi_ulto2b(0xffff, riid_ptr->length);
7274 }
7275 scsi_ulto2b(len, rii_ptr->length);
7276
7277 ctl_set_success(ctsio);
7278 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7279 ctsio->be_move_done = ctl_config_move_done;
7280 ctl_datamove((union ctl_io *)ctsio);
7281 return(retval);
7282 }
7283
7284 int
ctl_report_tagret_port_groups(struct ctl_scsiio * ctsio)7285 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7286 {
7287 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7288 struct ctl_lun *lun = CTL_LUN(ctsio);
7289 struct scsi_maintenance_in *cdb;
7290 int retval;
7291 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os;
7292 int num_ha_groups, num_target_ports, shared_group;
7293 struct ctl_port *port;
7294 struct scsi_target_group_data *rtg_ptr;
7295 struct scsi_target_group_data_extended *rtg_ext_ptr;
7296 struct scsi_target_port_group_descriptor *tpg_desc;
7297
7298 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7299
7300 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7301 retval = CTL_RETVAL_COMPLETE;
7302
7303 switch (cdb->byte2 & STG_PDF_MASK) {
7304 case STG_PDF_LENGTH:
7305 ext = 0;
7306 break;
7307 case STG_PDF_EXTENDED:
7308 ext = 1;
7309 break;
7310 default:
7311 ctl_set_invalid_field(/*ctsio*/ ctsio,
7312 /*sks_valid*/ 1,
7313 /*command*/ 1,
7314 /*field*/ 2,
7315 /*bit_valid*/ 1,
7316 /*bit*/ 5);
7317 ctl_done((union ctl_io *)ctsio);
7318 return(retval);
7319 }
7320
7321 num_target_ports = 0;
7322 shared_group = (softc->is_single != 0);
7323 mtx_lock(&softc->ctl_lock);
7324 STAILQ_FOREACH(port, &softc->port_list, links) {
7325 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7326 continue;
7327 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7328 continue;
7329 num_target_ports++;
7330 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7331 shared_group = 1;
7332 }
7333 mtx_unlock(&softc->ctl_lock);
7334 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES;
7335
7336 if (ext)
7337 total_len = sizeof(struct scsi_target_group_data_extended);
7338 else
7339 total_len = sizeof(struct scsi_target_group_data);
7340 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7341 (shared_group + num_ha_groups) +
7342 sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7343
7344 alloc_len = scsi_4btoul(cdb->length);
7345
7346 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7347 ctsio->kern_sg_entries = 0;
7348 ctsio->kern_rel_offset = 0;
7349 ctsio->kern_data_len = min(total_len, alloc_len);
7350 ctsio->kern_total_len = ctsio->kern_data_len;
7351
7352 if (ext) {
7353 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7354 ctsio->kern_data_ptr;
7355 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7356 rtg_ext_ptr->format_type = 0x10;
7357 rtg_ext_ptr->implicit_transition_time = 0;
7358 tpg_desc = &rtg_ext_ptr->groups[0];
7359 } else {
7360 rtg_ptr = (struct scsi_target_group_data *)
7361 ctsio->kern_data_ptr;
7362 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7363 tpg_desc = &rtg_ptr->groups[0];
7364 }
7365
7366 mtx_lock(&softc->ctl_lock);
7367 pg = softc->port_min / softc->port_cnt;
7368 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) {
7369 /* Some shelf is known to be primary. */
7370 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7371 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7372 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7373 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7374 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7375 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7376 else
7377 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7378 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7379 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7380 } else {
7381 ts = os;
7382 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7383 }
7384 } else {
7385 /* No known primary shelf. */
7386 if (softc->ha_link == CTL_HA_LINK_OFFLINE) {
7387 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7388 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7389 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) {
7390 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7391 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7392 } else {
7393 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7394 }
7395 }
7396 if (shared_group) {
7397 tpg_desc->pref_state = ts;
7398 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7399 TPG_U_SUP | TPG_T_SUP;
7400 scsi_ulto2b(1, tpg_desc->target_port_group);
7401 tpg_desc->status = TPG_IMPLICIT;
7402 pc = 0;
7403 STAILQ_FOREACH(port, &softc->port_list, links) {
7404 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7405 continue;
7406 if (!softc->is_single &&
7407 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0)
7408 continue;
7409 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7410 continue;
7411 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7412 relative_target_port_identifier);
7413 pc++;
7414 }
7415 tpg_desc->target_port_count = pc;
7416 tpg_desc = (struct scsi_target_port_group_descriptor *)
7417 &tpg_desc->descriptors[pc];
7418 }
7419 for (g = 0; g < num_ha_groups; g++) {
7420 tpg_desc->pref_state = (g == pg) ? ts : os;
7421 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7422 TPG_U_SUP | TPG_T_SUP;
7423 scsi_ulto2b(2 + g, tpg_desc->target_port_group);
7424 tpg_desc->status = TPG_IMPLICIT;
7425 pc = 0;
7426 STAILQ_FOREACH(port, &softc->port_list, links) {
7427 if (port->targ_port < g * softc->port_cnt ||
7428 port->targ_port >= (g + 1) * softc->port_cnt)
7429 continue;
7430 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7431 continue;
7432 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7433 continue;
7434 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7435 continue;
7436 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7437 relative_target_port_identifier);
7438 pc++;
7439 }
7440 tpg_desc->target_port_count = pc;
7441 tpg_desc = (struct scsi_target_port_group_descriptor *)
7442 &tpg_desc->descriptors[pc];
7443 }
7444 mtx_unlock(&softc->ctl_lock);
7445
7446 ctl_set_success(ctsio);
7447 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7448 ctsio->be_move_done = ctl_config_move_done;
7449 ctl_datamove((union ctl_io *)ctsio);
7450 return(retval);
7451 }
7452
7453 int
ctl_report_supported_opcodes(struct ctl_scsiio * ctsio)7454 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7455 {
7456 struct ctl_lun *lun = CTL_LUN(ctsio);
7457 struct scsi_report_supported_opcodes *cdb;
7458 const struct ctl_cmd_entry *entry, *sentry;
7459 struct scsi_report_supported_opcodes_all *all;
7460 struct scsi_report_supported_opcodes_descr *descr;
7461 struct scsi_report_supported_opcodes_one *one;
7462 int retval;
7463 int alloc_len, total_len;
7464 int opcode, service_action, i, j, num;
7465
7466 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7467
7468 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7469 retval = CTL_RETVAL_COMPLETE;
7470
7471 opcode = cdb->requested_opcode;
7472 service_action = scsi_2btoul(cdb->requested_service_action);
7473 switch (cdb->options & RSO_OPTIONS_MASK) {
7474 case RSO_OPTIONS_ALL:
7475 num = 0;
7476 for (i = 0; i < 256; i++) {
7477 entry = &ctl_cmd_table[i];
7478 if (entry->flags & CTL_CMD_FLAG_SA5) {
7479 for (j = 0; j < 32; j++) {
7480 sentry = &((const struct ctl_cmd_entry *)
7481 entry->execute)[j];
7482 if (ctl_cmd_applicable(
7483 lun->be_lun->lun_type, sentry))
7484 num++;
7485 }
7486 } else {
7487 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7488 entry))
7489 num++;
7490 }
7491 }
7492 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7493 num * sizeof(struct scsi_report_supported_opcodes_descr);
7494 break;
7495 case RSO_OPTIONS_OC:
7496 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7497 goto invalid_options;
7498 }
7499 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7500 break;
7501 case RSO_OPTIONS_OC_SA:
7502 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0) {
7503 goto invalid_options;
7504 }
7505 /* FALLTHROUGH */
7506 case RSO_OPTIONS_OC_ASA:
7507 if (service_action >= 32) {
7508 ctl_set_invalid_field(/*ctsio*/ ctsio,
7509 /*sks_valid*/ 1,
7510 /*command*/ 1,
7511 /*field*/ 4,
7512 /*bit_valid*/ 0,
7513 /*bit*/ 0);
7514 ctl_done((union ctl_io *)ctsio);
7515 return (CTL_RETVAL_COMPLETE);
7516 }
7517 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7518 break;
7519 default:
7520 invalid_options:
7521 ctl_set_invalid_field(/*ctsio*/ ctsio,
7522 /*sks_valid*/ 1,
7523 /*command*/ 1,
7524 /*field*/ 2,
7525 /*bit_valid*/ 1,
7526 /*bit*/ 2);
7527 ctl_done((union ctl_io *)ctsio);
7528 return (CTL_RETVAL_COMPLETE);
7529 }
7530
7531 alloc_len = scsi_4btoul(cdb->length);
7532
7533 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7534 ctsio->kern_sg_entries = 0;
7535 ctsio->kern_rel_offset = 0;
7536 ctsio->kern_data_len = min(total_len, alloc_len);
7537 ctsio->kern_total_len = ctsio->kern_data_len;
7538
7539 switch (cdb->options & RSO_OPTIONS_MASK) {
7540 case RSO_OPTIONS_ALL:
7541 all = (struct scsi_report_supported_opcodes_all *)
7542 ctsio->kern_data_ptr;
7543 num = 0;
7544 for (i = 0; i < 256; i++) {
7545 entry = &ctl_cmd_table[i];
7546 if (entry->flags & CTL_CMD_FLAG_SA5) {
7547 for (j = 0; j < 32; j++) {
7548 sentry = &((const struct ctl_cmd_entry *)
7549 entry->execute)[j];
7550 if (!ctl_cmd_applicable(
7551 lun->be_lun->lun_type, sentry))
7552 continue;
7553 descr = &all->descr[num++];
7554 descr->opcode = i;
7555 scsi_ulto2b(j, descr->service_action);
7556 descr->flags = RSO_SERVACTV;
7557 scsi_ulto2b(sentry->length,
7558 descr->cdb_length);
7559 }
7560 } else {
7561 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7562 entry))
7563 continue;
7564 descr = &all->descr[num++];
7565 descr->opcode = i;
7566 scsi_ulto2b(0, descr->service_action);
7567 descr->flags = 0;
7568 scsi_ulto2b(entry->length, descr->cdb_length);
7569 }
7570 }
7571 scsi_ulto4b(
7572 num * sizeof(struct scsi_report_supported_opcodes_descr),
7573 all->length);
7574 break;
7575 case RSO_OPTIONS_OC:
7576 one = (struct scsi_report_supported_opcodes_one *)
7577 ctsio->kern_data_ptr;
7578 entry = &ctl_cmd_table[opcode];
7579 goto fill_one;
7580 case RSO_OPTIONS_OC_SA:
7581 one = (struct scsi_report_supported_opcodes_one *)
7582 ctsio->kern_data_ptr;
7583 entry = &ctl_cmd_table[opcode];
7584 entry = &((const struct ctl_cmd_entry *)
7585 entry->execute)[service_action];
7586 fill_one:
7587 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7588 one->support = 3;
7589 scsi_ulto2b(entry->length, one->cdb_length);
7590 one->cdb_usage[0] = opcode;
7591 memcpy(&one->cdb_usage[1], entry->usage,
7592 entry->length - 1);
7593 } else
7594 one->support = 1;
7595 break;
7596 case RSO_OPTIONS_OC_ASA:
7597 one = (struct scsi_report_supported_opcodes_one *)
7598 ctsio->kern_data_ptr;
7599 entry = &ctl_cmd_table[opcode];
7600 if (entry->flags & CTL_CMD_FLAG_SA5) {
7601 entry = &((const struct ctl_cmd_entry *)
7602 entry->execute)[service_action];
7603 } else if (service_action != 0) {
7604 one->support = 1;
7605 break;
7606 }
7607 goto fill_one;
7608 }
7609
7610 ctl_set_success(ctsio);
7611 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7612 ctsio->be_move_done = ctl_config_move_done;
7613 ctl_datamove((union ctl_io *)ctsio);
7614 return(retval);
7615 }
7616
7617 int
ctl_report_supported_tmf(struct ctl_scsiio * ctsio)7618 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7619 {
7620 struct scsi_report_supported_tmf *cdb;
7621 struct scsi_report_supported_tmf_ext_data *data;
7622 int retval;
7623 int alloc_len, total_len;
7624
7625 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7626
7627 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7628
7629 retval = CTL_RETVAL_COMPLETE;
7630
7631 if (cdb->options & RST_REPD)
7632 total_len = sizeof(struct scsi_report_supported_tmf_ext_data);
7633 else
7634 total_len = sizeof(struct scsi_report_supported_tmf_data);
7635 alloc_len = scsi_4btoul(cdb->length);
7636
7637 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7638 ctsio->kern_sg_entries = 0;
7639 ctsio->kern_rel_offset = 0;
7640 ctsio->kern_data_len = min(total_len, alloc_len);
7641 ctsio->kern_total_len = ctsio->kern_data_len;
7642
7643 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr;
7644 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7645 RST_TRS;
7646 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7647 data->length = total_len - 4;
7648
7649 ctl_set_success(ctsio);
7650 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7651 ctsio->be_move_done = ctl_config_move_done;
7652 ctl_datamove((union ctl_io *)ctsio);
7653 return (retval);
7654 }
7655
7656 int
ctl_report_timestamp(struct ctl_scsiio * ctsio)7657 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7658 {
7659 struct scsi_report_timestamp *cdb;
7660 struct scsi_report_timestamp_data *data;
7661 struct timeval tv;
7662 int64_t timestamp;
7663 int retval;
7664 int alloc_len, total_len;
7665
7666 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7667
7668 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7669
7670 retval = CTL_RETVAL_COMPLETE;
7671
7672 total_len = sizeof(struct scsi_report_timestamp_data);
7673 alloc_len = scsi_4btoul(cdb->length);
7674
7675 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7676 ctsio->kern_sg_entries = 0;
7677 ctsio->kern_rel_offset = 0;
7678 ctsio->kern_data_len = min(total_len, alloc_len);
7679 ctsio->kern_total_len = ctsio->kern_data_len;
7680
7681 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7682 scsi_ulto2b(sizeof(*data) - 2, data->length);
7683 data->origin = RTS_ORIG_OUTSIDE;
7684 getmicrotime(&tv);
7685 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7686 scsi_ulto4b(timestamp >> 16, data->timestamp);
7687 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7688
7689 ctl_set_success(ctsio);
7690 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7691 ctsio->be_move_done = ctl_config_move_done;
7692 ctl_datamove((union ctl_io *)ctsio);
7693 return (retval);
7694 }
7695
7696 int
ctl_persistent_reserve_in(struct ctl_scsiio * ctsio)7697 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7698 {
7699 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7700 struct ctl_lun *lun = CTL_LUN(ctsio);
7701 struct scsi_per_res_in *cdb;
7702 int alloc_len, total_len = 0;
7703 /* struct scsi_per_res_in_rsrv in_data; */
7704 uint64_t key;
7705
7706 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7707
7708 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7709
7710 alloc_len = scsi_2btoul(cdb->length);
7711
7712 retry:
7713 mtx_lock(&lun->lun_lock);
7714 switch (cdb->action) {
7715 case SPRI_RK: /* read keys */
7716 total_len = sizeof(struct scsi_per_res_in_keys) +
7717 lun->pr_key_count *
7718 sizeof(struct scsi_per_res_key);
7719 break;
7720 case SPRI_RR: /* read reservation */
7721 if (lun->flags & CTL_LUN_PR_RESERVED)
7722 total_len = sizeof(struct scsi_per_res_in_rsrv);
7723 else
7724 total_len = sizeof(struct scsi_per_res_in_header);
7725 break;
7726 case SPRI_RC: /* report capabilities */
7727 total_len = sizeof(struct scsi_per_res_cap);
7728 break;
7729 case SPRI_RS: /* read full status */
7730 total_len = sizeof(struct scsi_per_res_in_header) +
7731 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7732 lun->pr_key_count;
7733 break;
7734 default:
7735 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7736 }
7737 mtx_unlock(&lun->lun_lock);
7738
7739 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7740 ctsio->kern_rel_offset = 0;
7741 ctsio->kern_sg_entries = 0;
7742 ctsio->kern_data_len = min(total_len, alloc_len);
7743 ctsio->kern_total_len = ctsio->kern_data_len;
7744
7745 mtx_lock(&lun->lun_lock);
7746 switch (cdb->action) {
7747 case SPRI_RK: { // read keys
7748 struct scsi_per_res_in_keys *res_keys;
7749 int i, key_count;
7750
7751 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7752
7753 /*
7754 * We had to drop the lock to allocate our buffer, which
7755 * leaves time for someone to come in with another
7756 * persistent reservation. (That is unlikely, though,
7757 * since this should be the only persistent reservation
7758 * command active right now.)
7759 */
7760 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7761 (lun->pr_key_count *
7762 sizeof(struct scsi_per_res_key)))){
7763 mtx_unlock(&lun->lun_lock);
7764 free(ctsio->kern_data_ptr, M_CTL);
7765 printf("%s: reservation length changed, retrying\n",
7766 __func__);
7767 goto retry;
7768 }
7769
7770 scsi_ulto4b(lun->pr_generation, res_keys->header.generation);
7771
7772 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7773 lun->pr_key_count, res_keys->header.length);
7774
7775 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7776 if ((key = ctl_get_prkey(lun, i)) == 0)
7777 continue;
7778
7779 /*
7780 * We used lun->pr_key_count to calculate the
7781 * size to allocate. If it turns out the number of
7782 * initiators with the registered flag set is
7783 * larger than that (i.e. they haven't been kept in
7784 * sync), we've got a problem.
7785 */
7786 if (key_count >= lun->pr_key_count) {
7787 key_count++;
7788 continue;
7789 }
7790 scsi_u64to8b(key, res_keys->keys[key_count].key);
7791 key_count++;
7792 }
7793 break;
7794 }
7795 case SPRI_RR: { // read reservation
7796 struct scsi_per_res_in_rsrv *res;
7797 int tmp_len, header_only;
7798
7799 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7800
7801 scsi_ulto4b(lun->pr_generation, res->header.generation);
7802
7803 if (lun->flags & CTL_LUN_PR_RESERVED)
7804 {
7805 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7806 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7807 res->header.length);
7808 header_only = 0;
7809 } else {
7810 tmp_len = sizeof(struct scsi_per_res_in_header);
7811 scsi_ulto4b(0, res->header.length);
7812 header_only = 1;
7813 }
7814
7815 /*
7816 * We had to drop the lock to allocate our buffer, which
7817 * leaves time for someone to come in with another
7818 * persistent reservation. (That is unlikely, though,
7819 * since this should be the only persistent reservation
7820 * command active right now.)
7821 */
7822 if (tmp_len != total_len) {
7823 mtx_unlock(&lun->lun_lock);
7824 free(ctsio->kern_data_ptr, M_CTL);
7825 printf("%s: reservation status changed, retrying\n",
7826 __func__);
7827 goto retry;
7828 }
7829
7830 /*
7831 * No reservation held, so we're done.
7832 */
7833 if (header_only != 0)
7834 break;
7835
7836 /*
7837 * If the registration is an All Registrants type, the key
7838 * is 0, since it doesn't really matter.
7839 */
7840 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7841 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7842 res->data.reservation);
7843 }
7844 res->data.scopetype = lun->pr_res_type;
7845 break;
7846 }
7847 case SPRI_RC: //report capabilities
7848 {
7849 struct scsi_per_res_cap *res_cap;
7850 uint16_t type_mask;
7851
7852 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7853 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7854 res_cap->flags1 = SPRI_CRH;
7855 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5;
7856 type_mask = SPRI_TM_WR_EX_AR |
7857 SPRI_TM_EX_AC_RO |
7858 SPRI_TM_WR_EX_RO |
7859 SPRI_TM_EX_AC |
7860 SPRI_TM_WR_EX |
7861 SPRI_TM_EX_AC_AR;
7862 scsi_ulto2b(type_mask, res_cap->type_mask);
7863 break;
7864 }
7865 case SPRI_RS: { // read full status
7866 struct scsi_per_res_in_full *res_status;
7867 struct scsi_per_res_in_full_desc *res_desc;
7868 struct ctl_port *port;
7869 int i, len;
7870
7871 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7872
7873 /*
7874 * We had to drop the lock to allocate our buffer, which
7875 * leaves time for someone to come in with another
7876 * persistent reservation. (That is unlikely, though,
7877 * since this should be the only persistent reservation
7878 * command active right now.)
7879 */
7880 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7881 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7882 lun->pr_key_count)){
7883 mtx_unlock(&lun->lun_lock);
7884 free(ctsio->kern_data_ptr, M_CTL);
7885 printf("%s: reservation length changed, retrying\n",
7886 __func__);
7887 goto retry;
7888 }
7889
7890 scsi_ulto4b(lun->pr_generation, res_status->header.generation);
7891
7892 res_desc = &res_status->desc[0];
7893 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7894 if ((key = ctl_get_prkey(lun, i)) == 0)
7895 continue;
7896
7897 scsi_u64to8b(key, res_desc->res_key.key);
7898 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7899 (lun->pr_res_idx == i ||
7900 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7901 res_desc->flags = SPRI_FULL_R_HOLDER;
7902 res_desc->scopetype = lun->pr_res_type;
7903 }
7904 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7905 res_desc->rel_trgt_port_id);
7906 len = 0;
7907 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7908 if (port != NULL)
7909 len = ctl_create_iid(port,
7910 i % CTL_MAX_INIT_PER_PORT,
7911 res_desc->transport_id);
7912 scsi_ulto4b(len, res_desc->additional_length);
7913 res_desc = (struct scsi_per_res_in_full_desc *)
7914 &res_desc->transport_id[len];
7915 }
7916 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7917 res_status->header.length);
7918 break;
7919 }
7920 default:
7921 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7922 }
7923 mtx_unlock(&lun->lun_lock);
7924
7925 ctl_set_success(ctsio);
7926 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7927 ctsio->be_move_done = ctl_config_move_done;
7928 ctl_datamove((union ctl_io *)ctsio);
7929 return (CTL_RETVAL_COMPLETE);
7930 }
7931
7932 /*
7933 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7934 * it should return.
7935 */
7936 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)7937 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7938 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7939 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7940 struct scsi_per_res_out_parms* param)
7941 {
7942 union ctl_ha_msg persis_io;
7943 int i;
7944
7945 mtx_lock(&lun->lun_lock);
7946 if (sa_res_key == 0) {
7947 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7948 /* validate scope and type */
7949 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7950 SPR_LU_SCOPE) {
7951 mtx_unlock(&lun->lun_lock);
7952 ctl_set_invalid_field(/*ctsio*/ ctsio,
7953 /*sks_valid*/ 1,
7954 /*command*/ 1,
7955 /*field*/ 2,
7956 /*bit_valid*/ 1,
7957 /*bit*/ 4);
7958 ctl_done((union ctl_io *)ctsio);
7959 return (1);
7960 }
7961
7962 if (type>8 || type==2 || type==4 || type==0) {
7963 mtx_unlock(&lun->lun_lock);
7964 ctl_set_invalid_field(/*ctsio*/ ctsio,
7965 /*sks_valid*/ 1,
7966 /*command*/ 1,
7967 /*field*/ 2,
7968 /*bit_valid*/ 1,
7969 /*bit*/ 0);
7970 ctl_done((union ctl_io *)ctsio);
7971 return (1);
7972 }
7973
7974 /*
7975 * Unregister everybody else and build UA for
7976 * them
7977 */
7978 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7979 if (i == residx || ctl_get_prkey(lun, i) == 0)
7980 continue;
7981
7982 ctl_clr_prkey(lun, i);
7983 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7984 }
7985 lun->pr_key_count = 1;
7986 lun->pr_res_type = type;
7987 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7988 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7989 lun->pr_res_idx = residx;
7990 lun->pr_generation++;
7991 mtx_unlock(&lun->lun_lock);
7992
7993 /* send msg to other side */
7994 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7995 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7996 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7997 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7998 persis_io.pr.pr_info.res_type = type;
7999 memcpy(persis_io.pr.pr_info.sa_res_key,
8000 param->serv_act_res_key,
8001 sizeof(param->serv_act_res_key));
8002 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8003 sizeof(persis_io.pr), M_WAITOK);
8004 } else {
8005 /* not all registrants */
8006 mtx_unlock(&lun->lun_lock);
8007 free(ctsio->kern_data_ptr, M_CTL);
8008 ctl_set_invalid_field(ctsio,
8009 /*sks_valid*/ 1,
8010 /*command*/ 0,
8011 /*field*/ 8,
8012 /*bit_valid*/ 0,
8013 /*bit*/ 0);
8014 ctl_done((union ctl_io *)ctsio);
8015 return (1);
8016 }
8017 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8018 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
8019 int found = 0;
8020
8021 if (res_key == sa_res_key) {
8022 /* special case */
8023 /*
8024 * The spec implies this is not good but doesn't
8025 * say what to do. There are two choices either
8026 * generate a res conflict or check condition
8027 * with illegal field in parameter data. Since
8028 * that is what is done when the sa_res_key is
8029 * zero I'll take that approach since this has
8030 * to do with the sa_res_key.
8031 */
8032 mtx_unlock(&lun->lun_lock);
8033 free(ctsio->kern_data_ptr, M_CTL);
8034 ctl_set_invalid_field(ctsio,
8035 /*sks_valid*/ 1,
8036 /*command*/ 0,
8037 /*field*/ 8,
8038 /*bit_valid*/ 0,
8039 /*bit*/ 0);
8040 ctl_done((union ctl_io *)ctsio);
8041 return (1);
8042 }
8043
8044 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8045 if (ctl_get_prkey(lun, i) != sa_res_key)
8046 continue;
8047
8048 found = 1;
8049 ctl_clr_prkey(lun, i);
8050 lun->pr_key_count--;
8051 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8052 }
8053 if (!found) {
8054 mtx_unlock(&lun->lun_lock);
8055 free(ctsio->kern_data_ptr, M_CTL);
8056 ctl_set_reservation_conflict(ctsio);
8057 ctl_done((union ctl_io *)ctsio);
8058 return (CTL_RETVAL_COMPLETE);
8059 }
8060 lun->pr_generation++;
8061 mtx_unlock(&lun->lun_lock);
8062
8063 /* send msg to other side */
8064 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8065 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8066 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8067 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8068 persis_io.pr.pr_info.res_type = type;
8069 memcpy(persis_io.pr.pr_info.sa_res_key,
8070 param->serv_act_res_key,
8071 sizeof(param->serv_act_res_key));
8072 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8073 sizeof(persis_io.pr), M_WAITOK);
8074 } else {
8075 /* Reserved but not all registrants */
8076 /* sa_res_key is res holder */
8077 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
8078 /* validate scope and type */
8079 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
8080 SPR_LU_SCOPE) {
8081 mtx_unlock(&lun->lun_lock);
8082 ctl_set_invalid_field(/*ctsio*/ ctsio,
8083 /*sks_valid*/ 1,
8084 /*command*/ 1,
8085 /*field*/ 2,
8086 /*bit_valid*/ 1,
8087 /*bit*/ 4);
8088 ctl_done((union ctl_io *)ctsio);
8089 return (1);
8090 }
8091
8092 if (type>8 || type==2 || type==4 || type==0) {
8093 mtx_unlock(&lun->lun_lock);
8094 ctl_set_invalid_field(/*ctsio*/ ctsio,
8095 /*sks_valid*/ 1,
8096 /*command*/ 1,
8097 /*field*/ 2,
8098 /*bit_valid*/ 1,
8099 /*bit*/ 0);
8100 ctl_done((union ctl_io *)ctsio);
8101 return (1);
8102 }
8103
8104 /*
8105 * Do the following:
8106 * if sa_res_key != res_key remove all
8107 * registrants w/sa_res_key and generate UA
8108 * for these registrants(Registrations
8109 * Preempted) if it wasn't an exclusive
8110 * reservation generate UA(Reservations
8111 * Preempted) for all other registered nexuses
8112 * if the type has changed. Establish the new
8113 * reservation and holder. If res_key and
8114 * sa_res_key are the same do the above
8115 * except don't unregister the res holder.
8116 */
8117
8118 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8119 if (i == residx || ctl_get_prkey(lun, i) == 0)
8120 continue;
8121
8122 if (sa_res_key == ctl_get_prkey(lun, i)) {
8123 ctl_clr_prkey(lun, i);
8124 lun->pr_key_count--;
8125 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8126 } else if (type != lun->pr_res_type &&
8127 (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8128 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8129 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8130 }
8131 }
8132 lun->pr_res_type = type;
8133 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8134 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8135 lun->pr_res_idx = residx;
8136 else
8137 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8138 lun->pr_generation++;
8139 mtx_unlock(&lun->lun_lock);
8140
8141 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8142 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8143 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8144 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8145 persis_io.pr.pr_info.res_type = type;
8146 memcpy(persis_io.pr.pr_info.sa_res_key,
8147 param->serv_act_res_key,
8148 sizeof(param->serv_act_res_key));
8149 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8150 sizeof(persis_io.pr), M_WAITOK);
8151 } else {
8152 /*
8153 * sa_res_key is not the res holder just
8154 * remove registrants
8155 */
8156 int found=0;
8157
8158 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8159 if (sa_res_key != ctl_get_prkey(lun, i))
8160 continue;
8161
8162 found = 1;
8163 ctl_clr_prkey(lun, i);
8164 lun->pr_key_count--;
8165 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8166 }
8167
8168 if (!found) {
8169 mtx_unlock(&lun->lun_lock);
8170 free(ctsio->kern_data_ptr, M_CTL);
8171 ctl_set_reservation_conflict(ctsio);
8172 ctl_done((union ctl_io *)ctsio);
8173 return (1);
8174 }
8175 lun->pr_generation++;
8176 mtx_unlock(&lun->lun_lock);
8177
8178 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8179 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8180 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8181 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8182 persis_io.pr.pr_info.res_type = type;
8183 memcpy(persis_io.pr.pr_info.sa_res_key,
8184 param->serv_act_res_key,
8185 sizeof(param->serv_act_res_key));
8186 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8187 sizeof(persis_io.pr), M_WAITOK);
8188 }
8189 }
8190 return (0);
8191 }
8192
8193 static void
ctl_pro_preempt_other(struct ctl_lun * lun,union ctl_ha_msg * msg)8194 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8195 {
8196 uint64_t sa_res_key;
8197 int i;
8198
8199 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8200
8201 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8202 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8203 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8204 if (sa_res_key == 0) {
8205 /*
8206 * Unregister everybody else and build UA for
8207 * them
8208 */
8209 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8210 if (i == msg->pr.pr_info.residx ||
8211 ctl_get_prkey(lun, i) == 0)
8212 continue;
8213
8214 ctl_clr_prkey(lun, i);
8215 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8216 }
8217
8218 lun->pr_key_count = 1;
8219 lun->pr_res_type = msg->pr.pr_info.res_type;
8220 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8221 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8222 lun->pr_res_idx = msg->pr.pr_info.residx;
8223 } else {
8224 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8225 if (sa_res_key == ctl_get_prkey(lun, i))
8226 continue;
8227
8228 ctl_clr_prkey(lun, i);
8229 lun->pr_key_count--;
8230 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8231 }
8232 }
8233 } else {
8234 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8235 if (i == msg->pr.pr_info.residx ||
8236 ctl_get_prkey(lun, i) == 0)
8237 continue;
8238
8239 if (sa_res_key == ctl_get_prkey(lun, i)) {
8240 ctl_clr_prkey(lun, i);
8241 lun->pr_key_count--;
8242 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8243 } else if (msg->pr.pr_info.res_type != lun->pr_res_type
8244 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8245 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8246 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8247 }
8248 }
8249 lun->pr_res_type = msg->pr.pr_info.res_type;
8250 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8251 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8252 lun->pr_res_idx = msg->pr.pr_info.residx;
8253 else
8254 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8255 }
8256 lun->pr_generation++;
8257
8258 }
8259
8260 int
ctl_persistent_reserve_out(struct ctl_scsiio * ctsio)8261 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8262 {
8263 struct ctl_softc *softc = CTL_SOFTC(ctsio);
8264 struct ctl_lun *lun = CTL_LUN(ctsio);
8265 int retval;
8266 uint32_t param_len;
8267 struct scsi_per_res_out *cdb;
8268 struct scsi_per_res_out_parms* param;
8269 uint32_t residx;
8270 uint64_t res_key, sa_res_key, key;
8271 uint8_t type;
8272 union ctl_ha_msg persis_io;
8273 int i;
8274
8275 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8276
8277 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8278 retval = CTL_RETVAL_COMPLETE;
8279
8280 /*
8281 * We only support whole-LUN scope. The scope & type are ignored for
8282 * register, register and ignore existing key and clear.
8283 * We sometimes ignore scope and type on preempts too!!
8284 * Verify reservation type here as well.
8285 */
8286 type = cdb->scope_type & SPR_TYPE_MASK;
8287 if ((cdb->action == SPRO_RESERVE)
8288 || (cdb->action == SPRO_RELEASE)) {
8289 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8290 ctl_set_invalid_field(/*ctsio*/ ctsio,
8291 /*sks_valid*/ 1,
8292 /*command*/ 1,
8293 /*field*/ 2,
8294 /*bit_valid*/ 1,
8295 /*bit*/ 4);
8296 ctl_done((union ctl_io *)ctsio);
8297 return (CTL_RETVAL_COMPLETE);
8298 }
8299
8300 if (type>8 || type==2 || type==4 || type==0) {
8301 ctl_set_invalid_field(/*ctsio*/ ctsio,
8302 /*sks_valid*/ 1,
8303 /*command*/ 1,
8304 /*field*/ 2,
8305 /*bit_valid*/ 1,
8306 /*bit*/ 0);
8307 ctl_done((union ctl_io *)ctsio);
8308 return (CTL_RETVAL_COMPLETE);
8309 }
8310 }
8311
8312 param_len = scsi_4btoul(cdb->length);
8313
8314 /* validate the parameter length */
8315 if (param_len != 24) {
8316 ctl_set_invalid_field(ctsio,
8317 /*sks_valid*/ 1,
8318 /*command*/ 1,
8319 /*field*/ 5,
8320 /*bit_valid*/ 1,
8321 /*bit*/ 0);
8322 ctl_done((union ctl_io *)ctsio);
8323 return (CTL_RETVAL_COMPLETE);
8324 }
8325
8326 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8327 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8328 ctsio->kern_data_len = param_len;
8329 ctsio->kern_total_len = param_len;
8330 ctsio->kern_rel_offset = 0;
8331 ctsio->kern_sg_entries = 0;
8332 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8333 ctsio->be_move_done = ctl_config_move_done;
8334 ctl_datamove((union ctl_io *)ctsio);
8335
8336 return (CTL_RETVAL_COMPLETE);
8337 }
8338
8339 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8340
8341 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8342 res_key = scsi_8btou64(param->res_key.key);
8343 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8344
8345 /*
8346 * Validate the reservation key here except for SPRO_REG_IGNO
8347 * This must be done for all other service actions
8348 */
8349 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8350 mtx_lock(&lun->lun_lock);
8351 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8352 if (res_key != key) {
8353 /*
8354 * The current key passed in doesn't match
8355 * the one the initiator previously
8356 * registered.
8357 */
8358 mtx_unlock(&lun->lun_lock);
8359 free(ctsio->kern_data_ptr, M_CTL);
8360 ctl_set_reservation_conflict(ctsio);
8361 ctl_done((union ctl_io *)ctsio);
8362 return (CTL_RETVAL_COMPLETE);
8363 }
8364 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8365 /*
8366 * We are not registered
8367 */
8368 mtx_unlock(&lun->lun_lock);
8369 free(ctsio->kern_data_ptr, M_CTL);
8370 ctl_set_reservation_conflict(ctsio);
8371 ctl_done((union ctl_io *)ctsio);
8372 return (CTL_RETVAL_COMPLETE);
8373 } else if (res_key != 0) {
8374 /*
8375 * We are not registered and trying to register but
8376 * the register key isn't zero.
8377 */
8378 mtx_unlock(&lun->lun_lock);
8379 free(ctsio->kern_data_ptr, M_CTL);
8380 ctl_set_reservation_conflict(ctsio);
8381 ctl_done((union ctl_io *)ctsio);
8382 return (CTL_RETVAL_COMPLETE);
8383 }
8384 mtx_unlock(&lun->lun_lock);
8385 }
8386
8387 switch (cdb->action & SPRO_ACTION_MASK) {
8388 case SPRO_REGISTER:
8389 case SPRO_REG_IGNO: {
8390 /*
8391 * We don't support any of these options, as we report in
8392 * the read capabilities request (see
8393 * ctl_persistent_reserve_in(), above).
8394 */
8395 if ((param->flags & SPR_SPEC_I_PT)
8396 || (param->flags & SPR_ALL_TG_PT)
8397 || (param->flags & SPR_APTPL)) {
8398 int bit_ptr;
8399
8400 if (param->flags & SPR_APTPL)
8401 bit_ptr = 0;
8402 else if (param->flags & SPR_ALL_TG_PT)
8403 bit_ptr = 2;
8404 else /* SPR_SPEC_I_PT */
8405 bit_ptr = 3;
8406
8407 free(ctsio->kern_data_ptr, M_CTL);
8408 ctl_set_invalid_field(ctsio,
8409 /*sks_valid*/ 1,
8410 /*command*/ 0,
8411 /*field*/ 20,
8412 /*bit_valid*/ 1,
8413 /*bit*/ bit_ptr);
8414 ctl_done((union ctl_io *)ctsio);
8415 return (CTL_RETVAL_COMPLETE);
8416 }
8417
8418 mtx_lock(&lun->lun_lock);
8419
8420 /*
8421 * The initiator wants to clear the
8422 * key/unregister.
8423 */
8424 if (sa_res_key == 0) {
8425 if ((res_key == 0
8426 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8427 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8428 && ctl_get_prkey(lun, residx) == 0)) {
8429 mtx_unlock(&lun->lun_lock);
8430 goto done;
8431 }
8432
8433 ctl_clr_prkey(lun, residx);
8434 lun->pr_key_count--;
8435
8436 if (residx == lun->pr_res_idx) {
8437 lun->flags &= ~CTL_LUN_PR_RESERVED;
8438 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8439
8440 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8441 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8442 lun->pr_key_count) {
8443 /*
8444 * If the reservation is a registrants
8445 * only type we need to generate a UA
8446 * for other registered inits. The
8447 * sense code should be RESERVATIONS
8448 * RELEASED
8449 */
8450
8451 for (i = softc->init_min; i < softc->init_max; i++){
8452 if (ctl_get_prkey(lun, i) == 0)
8453 continue;
8454 ctl_est_ua(lun, i,
8455 CTL_UA_RES_RELEASE);
8456 }
8457 }
8458 lun->pr_res_type = 0;
8459 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8460 if (lun->pr_key_count==0) {
8461 lun->flags &= ~CTL_LUN_PR_RESERVED;
8462 lun->pr_res_type = 0;
8463 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8464 }
8465 }
8466 lun->pr_generation++;
8467 mtx_unlock(&lun->lun_lock);
8468
8469 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8470 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8471 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8472 persis_io.pr.pr_info.residx = residx;
8473 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8474 sizeof(persis_io.pr), M_WAITOK);
8475 } else /* sa_res_key != 0 */ {
8476 /*
8477 * If we aren't registered currently then increment
8478 * the key count and set the registered flag.
8479 */
8480 ctl_alloc_prkey(lun, residx);
8481 if (ctl_get_prkey(lun, residx) == 0)
8482 lun->pr_key_count++;
8483 ctl_set_prkey(lun, residx, sa_res_key);
8484 lun->pr_generation++;
8485 mtx_unlock(&lun->lun_lock);
8486
8487 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8488 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8489 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8490 persis_io.pr.pr_info.residx = residx;
8491 memcpy(persis_io.pr.pr_info.sa_res_key,
8492 param->serv_act_res_key,
8493 sizeof(param->serv_act_res_key));
8494 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8495 sizeof(persis_io.pr), M_WAITOK);
8496 }
8497
8498 break;
8499 }
8500 case SPRO_RESERVE:
8501 mtx_lock(&lun->lun_lock);
8502 if (lun->flags & CTL_LUN_PR_RESERVED) {
8503 /*
8504 * if this isn't the reservation holder and it's
8505 * not a "all registrants" type or if the type is
8506 * different then we have a conflict
8507 */
8508 if ((lun->pr_res_idx != residx
8509 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8510 || lun->pr_res_type != type) {
8511 mtx_unlock(&lun->lun_lock);
8512 free(ctsio->kern_data_ptr, M_CTL);
8513 ctl_set_reservation_conflict(ctsio);
8514 ctl_done((union ctl_io *)ctsio);
8515 return (CTL_RETVAL_COMPLETE);
8516 }
8517 mtx_unlock(&lun->lun_lock);
8518 } else /* create a reservation */ {
8519 /*
8520 * If it's not an "all registrants" type record
8521 * reservation holder
8522 */
8523 if (type != SPR_TYPE_WR_EX_AR
8524 && type != SPR_TYPE_EX_AC_AR)
8525 lun->pr_res_idx = residx; /* Res holder */
8526 else
8527 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8528
8529 lun->flags |= CTL_LUN_PR_RESERVED;
8530 lun->pr_res_type = type;
8531
8532 mtx_unlock(&lun->lun_lock);
8533
8534 /* send msg to other side */
8535 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8536 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8537 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8538 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8539 persis_io.pr.pr_info.res_type = type;
8540 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8541 sizeof(persis_io.pr), M_WAITOK);
8542 }
8543 break;
8544
8545 case SPRO_RELEASE:
8546 mtx_lock(&lun->lun_lock);
8547 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8548 /* No reservation exists return good status */
8549 mtx_unlock(&lun->lun_lock);
8550 goto done;
8551 }
8552 /*
8553 * Is this nexus a reservation holder?
8554 */
8555 if (lun->pr_res_idx != residx
8556 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8557 /*
8558 * not a res holder return good status but
8559 * do nothing
8560 */
8561 mtx_unlock(&lun->lun_lock);
8562 goto done;
8563 }
8564
8565 if (lun->pr_res_type != type) {
8566 mtx_unlock(&lun->lun_lock);
8567 free(ctsio->kern_data_ptr, M_CTL);
8568 ctl_set_illegal_pr_release(ctsio);
8569 ctl_done((union ctl_io *)ctsio);
8570 return (CTL_RETVAL_COMPLETE);
8571 }
8572
8573 /* okay to release */
8574 lun->flags &= ~CTL_LUN_PR_RESERVED;
8575 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8576 lun->pr_res_type = 0;
8577
8578 /*
8579 * If this isn't an exclusive access reservation and NUAR
8580 * is not set, generate UA for all other registrants.
8581 */
8582 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX &&
8583 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8584 for (i = softc->init_min; i < softc->init_max; i++) {
8585 if (i == residx || ctl_get_prkey(lun, i) == 0)
8586 continue;
8587 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8588 }
8589 }
8590 mtx_unlock(&lun->lun_lock);
8591
8592 /* Send msg to other side */
8593 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8594 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8595 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8596 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8597 sizeof(persis_io.pr), M_WAITOK);
8598 break;
8599
8600 case SPRO_CLEAR:
8601 /* send msg to other side */
8602
8603 mtx_lock(&lun->lun_lock);
8604 lun->flags &= ~CTL_LUN_PR_RESERVED;
8605 lun->pr_res_type = 0;
8606 lun->pr_key_count = 0;
8607 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8608
8609 ctl_clr_prkey(lun, residx);
8610 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8611 if (ctl_get_prkey(lun, i) != 0) {
8612 ctl_clr_prkey(lun, i);
8613 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8614 }
8615 lun->pr_generation++;
8616 mtx_unlock(&lun->lun_lock);
8617
8618 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8619 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8620 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8621 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8622 sizeof(persis_io.pr), M_WAITOK);
8623 break;
8624
8625 case SPRO_PREEMPT:
8626 case SPRO_PRE_ABO: {
8627 int nretval;
8628
8629 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8630 residx, ctsio, cdb, param);
8631 if (nretval != 0)
8632 return (CTL_RETVAL_COMPLETE);
8633 break;
8634 }
8635 default:
8636 panic("%s: Invalid PR type %#x", __func__, cdb->action);
8637 }
8638
8639 done:
8640 free(ctsio->kern_data_ptr, M_CTL);
8641 ctl_set_success(ctsio);
8642 ctl_done((union ctl_io *)ctsio);
8643
8644 return (retval);
8645 }
8646
8647 /*
8648 * This routine is for handling a message from the other SC pertaining to
8649 * persistent reserve out. All the error checking will have been done
8650 * so only performing the action need be done here to keep the two
8651 * in sync.
8652 */
8653 static void
ctl_hndl_per_res_out_on_other_sc(union ctl_io * io)8654 ctl_hndl_per_res_out_on_other_sc(union ctl_io *io)
8655 {
8656 struct ctl_softc *softc = CTL_SOFTC(io);
8657 union ctl_ha_msg *msg = (union ctl_ha_msg *)&io->presio.pr_msg;
8658 struct ctl_lun *lun;
8659 int i;
8660 uint32_t residx, targ_lun;
8661
8662 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8663 mtx_lock(&softc->ctl_lock);
8664 if (targ_lun >= ctl_max_luns ||
8665 (lun = softc->ctl_luns[targ_lun]) == NULL) {
8666 mtx_unlock(&softc->ctl_lock);
8667 return;
8668 }
8669 mtx_lock(&lun->lun_lock);
8670 mtx_unlock(&softc->ctl_lock);
8671 if (lun->flags & CTL_LUN_DISABLED) {
8672 mtx_unlock(&lun->lun_lock);
8673 return;
8674 }
8675 residx = ctl_get_initindex(&msg->hdr.nexus);
8676 switch(msg->pr.pr_info.action) {
8677 case CTL_PR_REG_KEY:
8678 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8679 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8680 lun->pr_key_count++;
8681 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8682 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8683 lun->pr_generation++;
8684 break;
8685
8686 case CTL_PR_UNREG_KEY:
8687 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8688 lun->pr_key_count--;
8689
8690 /* XXX Need to see if the reservation has been released */
8691 /* if so do we need to generate UA? */
8692 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8693 lun->flags &= ~CTL_LUN_PR_RESERVED;
8694 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8695
8696 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8697 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8698 lun->pr_key_count) {
8699 /*
8700 * If the reservation is a registrants
8701 * only type we need to generate a UA
8702 * for other registered inits. The
8703 * sense code should be RESERVATIONS
8704 * RELEASED
8705 */
8706
8707 for (i = softc->init_min; i < softc->init_max; i++) {
8708 if (ctl_get_prkey(lun, i) == 0)
8709 continue;
8710
8711 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8712 }
8713 }
8714 lun->pr_res_type = 0;
8715 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8716 if (lun->pr_key_count==0) {
8717 lun->flags &= ~CTL_LUN_PR_RESERVED;
8718 lun->pr_res_type = 0;
8719 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8720 }
8721 }
8722 lun->pr_generation++;
8723 break;
8724
8725 case CTL_PR_RESERVE:
8726 lun->flags |= CTL_LUN_PR_RESERVED;
8727 lun->pr_res_type = msg->pr.pr_info.res_type;
8728 lun->pr_res_idx = msg->pr.pr_info.residx;
8729
8730 break;
8731
8732 case CTL_PR_RELEASE:
8733 /*
8734 * If this isn't an exclusive access reservation and NUAR
8735 * is not set, generate UA for all other registrants.
8736 */
8737 if (lun->pr_res_type != SPR_TYPE_EX_AC &&
8738 lun->pr_res_type != SPR_TYPE_WR_EX &&
8739 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8740 for (i = softc->init_min; i < softc->init_max; i++) {
8741 if (i == residx || ctl_get_prkey(lun, i) == 0)
8742 continue;
8743 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8744 }
8745 }
8746
8747 lun->flags &= ~CTL_LUN_PR_RESERVED;
8748 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8749 lun->pr_res_type = 0;
8750 break;
8751
8752 case CTL_PR_PREEMPT:
8753 ctl_pro_preempt_other(lun, msg);
8754 break;
8755 case CTL_PR_CLEAR:
8756 lun->flags &= ~CTL_LUN_PR_RESERVED;
8757 lun->pr_res_type = 0;
8758 lun->pr_key_count = 0;
8759 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8760
8761 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8762 if (ctl_get_prkey(lun, i) == 0)
8763 continue;
8764 ctl_clr_prkey(lun, i);
8765 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8766 }
8767 lun->pr_generation++;
8768 break;
8769 }
8770
8771 mtx_unlock(&lun->lun_lock);
8772 }
8773
8774 int
ctl_read_write(struct ctl_scsiio * ctsio)8775 ctl_read_write(struct ctl_scsiio *ctsio)
8776 {
8777 struct ctl_lun *lun = CTL_LUN(ctsio);
8778 struct ctl_lba_len_flags *lbalen;
8779 uint64_t lba;
8780 uint32_t num_blocks;
8781 int flags, retval;
8782 int isread;
8783
8784 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8785
8786 flags = 0;
8787 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8788 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8789 switch (ctsio->cdb[0]) {
8790 case READ_6:
8791 case WRITE_6: {
8792 struct scsi_rw_6 *cdb;
8793
8794 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8795
8796 lba = scsi_3btoul(cdb->addr);
8797 /* only 5 bits are valid in the most significant address byte */
8798 lba &= 0x1fffff;
8799 num_blocks = cdb->length;
8800 /*
8801 * This is correct according to SBC-2.
8802 */
8803 if (num_blocks == 0)
8804 num_blocks = 256;
8805 break;
8806 }
8807 case READ_10:
8808 case WRITE_10: {
8809 struct scsi_rw_10 *cdb;
8810
8811 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8812 if (cdb->byte2 & SRW10_FUA)
8813 flags |= CTL_LLF_FUA;
8814 if (cdb->byte2 & SRW10_DPO)
8815 flags |= CTL_LLF_DPO;
8816 lba = scsi_4btoul(cdb->addr);
8817 num_blocks = scsi_2btoul(cdb->length);
8818 break;
8819 }
8820 case WRITE_VERIFY_10: {
8821 struct scsi_write_verify_10 *cdb;
8822
8823 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8824 flags |= CTL_LLF_FUA;
8825 if (cdb->byte2 & SWV_DPO)
8826 flags |= CTL_LLF_DPO;
8827 lba = scsi_4btoul(cdb->addr);
8828 num_blocks = scsi_2btoul(cdb->length);
8829 break;
8830 }
8831 case READ_12:
8832 case WRITE_12: {
8833 struct scsi_rw_12 *cdb;
8834
8835 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8836 if (cdb->byte2 & SRW12_FUA)
8837 flags |= CTL_LLF_FUA;
8838 if (cdb->byte2 & SRW12_DPO)
8839 flags |= CTL_LLF_DPO;
8840 lba = scsi_4btoul(cdb->addr);
8841 num_blocks = scsi_4btoul(cdb->length);
8842 break;
8843 }
8844 case WRITE_VERIFY_12: {
8845 struct scsi_write_verify_12 *cdb;
8846
8847 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8848 flags |= CTL_LLF_FUA;
8849 if (cdb->byte2 & SWV_DPO)
8850 flags |= CTL_LLF_DPO;
8851 lba = scsi_4btoul(cdb->addr);
8852 num_blocks = scsi_4btoul(cdb->length);
8853 break;
8854 }
8855 case READ_16:
8856 case WRITE_16: {
8857 struct scsi_rw_16 *cdb;
8858
8859 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8860 if (cdb->byte2 & SRW12_FUA)
8861 flags |= CTL_LLF_FUA;
8862 if (cdb->byte2 & SRW12_DPO)
8863 flags |= CTL_LLF_DPO;
8864 lba = scsi_8btou64(cdb->addr);
8865 num_blocks = scsi_4btoul(cdb->length);
8866 break;
8867 }
8868 case WRITE_ATOMIC_16: {
8869 struct scsi_write_atomic_16 *cdb;
8870
8871 if (lun->be_lun->atomicblock == 0) {
8872 ctl_set_invalid_opcode(ctsio);
8873 ctl_done((union ctl_io *)ctsio);
8874 return (CTL_RETVAL_COMPLETE);
8875 }
8876
8877 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb;
8878 if (cdb->byte2 & SRW12_FUA)
8879 flags |= CTL_LLF_FUA;
8880 if (cdb->byte2 & SRW12_DPO)
8881 flags |= CTL_LLF_DPO;
8882 lba = scsi_8btou64(cdb->addr);
8883 num_blocks = scsi_2btoul(cdb->length);
8884 if (num_blocks > lun->be_lun->atomicblock) {
8885 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8886 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8887 /*bit*/ 0);
8888 ctl_done((union ctl_io *)ctsio);
8889 return (CTL_RETVAL_COMPLETE);
8890 }
8891 break;
8892 }
8893 case WRITE_VERIFY_16: {
8894 struct scsi_write_verify_16 *cdb;
8895
8896 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8897 flags |= CTL_LLF_FUA;
8898 if (cdb->byte2 & SWV_DPO)
8899 flags |= CTL_LLF_DPO;
8900 lba = scsi_8btou64(cdb->addr);
8901 num_blocks = scsi_4btoul(cdb->length);
8902 break;
8903 }
8904 default:
8905 /*
8906 * We got a command we don't support. This shouldn't
8907 * happen, commands should be filtered out above us.
8908 */
8909 ctl_set_invalid_opcode(ctsio);
8910 ctl_done((union ctl_io *)ctsio);
8911
8912 return (CTL_RETVAL_COMPLETE);
8913 break; /* NOTREACHED */
8914 }
8915
8916 /*
8917 * The first check is to make sure we're in bounds, the second
8918 * check is to catch wrap-around problems. If the lba + num blocks
8919 * is less than the lba, then we've wrapped around and the block
8920 * range is invalid anyway.
8921 */
8922 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8923 || ((lba + num_blocks) < lba)) {
8924 ctl_set_lba_out_of_range(ctsio,
8925 MAX(lba, lun->be_lun->maxlba + 1));
8926 ctl_done((union ctl_io *)ctsio);
8927 return (CTL_RETVAL_COMPLETE);
8928 }
8929
8930 /*
8931 * According to SBC-3, a transfer length of 0 is not an error.
8932 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8933 * translates to 256 blocks for those commands.
8934 */
8935 if (num_blocks == 0) {
8936 ctl_set_success(ctsio);
8937 ctl_done((union ctl_io *)ctsio);
8938 return (CTL_RETVAL_COMPLETE);
8939 }
8940
8941 /* Set FUA and/or DPO if caches are disabled. */
8942 if (isread) {
8943 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0)
8944 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8945 } else {
8946 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8947 flags |= CTL_LLF_FUA;
8948 }
8949
8950 lbalen = (struct ctl_lba_len_flags *)
8951 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8952 lbalen->lba = lba;
8953 lbalen->len = num_blocks;
8954 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8955
8956 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8957 ctsio->kern_rel_offset = 0;
8958
8959 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8960
8961 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8962 return (retval);
8963 }
8964
8965 static int
ctl_cnw_cont(union ctl_io * io)8966 ctl_cnw_cont(union ctl_io *io)
8967 {
8968 struct ctl_lun *lun = CTL_LUN(io);
8969 struct ctl_scsiio *ctsio;
8970 struct ctl_lba_len_flags *lbalen;
8971 int retval;
8972
8973 ctsio = &io->scsiio;
8974 ctsio->io_hdr.status = CTL_STATUS_NONE;
8975 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8976 lbalen = (struct ctl_lba_len_flags *)
8977 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8978 lbalen->flags &= ~CTL_LLF_COMPARE;
8979 lbalen->flags |= CTL_LLF_WRITE;
8980
8981 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8982 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8983 return (retval);
8984 }
8985
8986 int
ctl_cnw(struct ctl_scsiio * ctsio)8987 ctl_cnw(struct ctl_scsiio *ctsio)
8988 {
8989 struct ctl_lun *lun = CTL_LUN(ctsio);
8990 struct ctl_lba_len_flags *lbalen;
8991 uint64_t lba;
8992 uint32_t num_blocks;
8993 int flags, retval;
8994
8995 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8996
8997 flags = 0;
8998 switch (ctsio->cdb[0]) {
8999 case COMPARE_AND_WRITE: {
9000 struct scsi_compare_and_write *cdb;
9001
9002 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
9003 if (cdb->byte2 & SRW10_FUA)
9004 flags |= CTL_LLF_FUA;
9005 if (cdb->byte2 & SRW10_DPO)
9006 flags |= CTL_LLF_DPO;
9007 lba = scsi_8btou64(cdb->addr);
9008 num_blocks = cdb->length;
9009 break;
9010 }
9011 default:
9012 /*
9013 * We got a command we don't support. This shouldn't
9014 * happen, commands should be filtered out above us.
9015 */
9016 ctl_set_invalid_opcode(ctsio);
9017 ctl_done((union ctl_io *)ctsio);
9018
9019 return (CTL_RETVAL_COMPLETE);
9020 break; /* NOTREACHED */
9021 }
9022
9023 /*
9024 * The first check is to make sure we're in bounds, the second
9025 * check is to catch wrap-around problems. If the lba + num blocks
9026 * is less than the lba, then we've wrapped around and the block
9027 * range is invalid anyway.
9028 */
9029 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9030 || ((lba + num_blocks) < lba)) {
9031 ctl_set_lba_out_of_range(ctsio,
9032 MAX(lba, lun->be_lun->maxlba + 1));
9033 ctl_done((union ctl_io *)ctsio);
9034 return (CTL_RETVAL_COMPLETE);
9035 }
9036
9037 /*
9038 * According to SBC-3, a transfer length of 0 is not an error.
9039 */
9040 if (num_blocks == 0) {
9041 ctl_set_success(ctsio);
9042 ctl_done((union ctl_io *)ctsio);
9043 return (CTL_RETVAL_COMPLETE);
9044 }
9045
9046 /* Set FUA if write cache is disabled. */
9047 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
9048 flags |= CTL_LLF_FUA;
9049
9050 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
9051 ctsio->kern_rel_offset = 0;
9052
9053 /*
9054 * Set the IO_CONT flag, so that if this I/O gets passed to
9055 * ctl_data_submit_done(), it'll get passed back to
9056 * ctl_ctl_cnw_cont() for further processing.
9057 */
9058 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
9059 ctsio->io_cont = ctl_cnw_cont;
9060
9061 lbalen = (struct ctl_lba_len_flags *)
9062 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9063 lbalen->lba = lba;
9064 lbalen->len = num_blocks;
9065 lbalen->flags = CTL_LLF_COMPARE | flags;
9066
9067 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
9068 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9069 return (retval);
9070 }
9071
9072 int
ctl_verify(struct ctl_scsiio * ctsio)9073 ctl_verify(struct ctl_scsiio *ctsio)
9074 {
9075 struct ctl_lun *lun = CTL_LUN(ctsio);
9076 struct ctl_lba_len_flags *lbalen;
9077 uint64_t lba;
9078 uint32_t num_blocks;
9079 int bytchk, flags;
9080 int retval;
9081
9082 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
9083
9084 bytchk = 0;
9085 flags = CTL_LLF_FUA;
9086 switch (ctsio->cdb[0]) {
9087 case VERIFY_10: {
9088 struct scsi_verify_10 *cdb;
9089
9090 cdb = (struct scsi_verify_10 *)ctsio->cdb;
9091 if (cdb->byte2 & SVFY_BYTCHK)
9092 bytchk = 1;
9093 if (cdb->byte2 & SVFY_DPO)
9094 flags |= CTL_LLF_DPO;
9095 lba = scsi_4btoul(cdb->addr);
9096 num_blocks = scsi_2btoul(cdb->length);
9097 break;
9098 }
9099 case VERIFY_12: {
9100 struct scsi_verify_12 *cdb;
9101
9102 cdb = (struct scsi_verify_12 *)ctsio->cdb;
9103 if (cdb->byte2 & SVFY_BYTCHK)
9104 bytchk = 1;
9105 if (cdb->byte2 & SVFY_DPO)
9106 flags |= CTL_LLF_DPO;
9107 lba = scsi_4btoul(cdb->addr);
9108 num_blocks = scsi_4btoul(cdb->length);
9109 break;
9110 }
9111 case VERIFY_16: {
9112 struct scsi_rw_16 *cdb;
9113
9114 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9115 if (cdb->byte2 & SVFY_BYTCHK)
9116 bytchk = 1;
9117 if (cdb->byte2 & SVFY_DPO)
9118 flags |= CTL_LLF_DPO;
9119 lba = scsi_8btou64(cdb->addr);
9120 num_blocks = scsi_4btoul(cdb->length);
9121 break;
9122 }
9123 default:
9124 /*
9125 * We got a command we don't support. This shouldn't
9126 * happen, commands should be filtered out above us.
9127 */
9128 ctl_set_invalid_opcode(ctsio);
9129 ctl_done((union ctl_io *)ctsio);
9130 return (CTL_RETVAL_COMPLETE);
9131 }
9132
9133 /*
9134 * The first check is to make sure we're in bounds, the second
9135 * check is to catch wrap-around problems. If the lba + num blocks
9136 * is less than the lba, then we've wrapped around and the block
9137 * range is invalid anyway.
9138 */
9139 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9140 || ((lba + num_blocks) < lba)) {
9141 ctl_set_lba_out_of_range(ctsio,
9142 MAX(lba, lun->be_lun->maxlba + 1));
9143 ctl_done((union ctl_io *)ctsio);
9144 return (CTL_RETVAL_COMPLETE);
9145 }
9146
9147 /*
9148 * According to SBC-3, a transfer length of 0 is not an error.
9149 */
9150 if (num_blocks == 0) {
9151 ctl_set_success(ctsio);
9152 ctl_done((union ctl_io *)ctsio);
9153 return (CTL_RETVAL_COMPLETE);
9154 }
9155
9156 lbalen = (struct ctl_lba_len_flags *)
9157 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9158 lbalen->lba = lba;
9159 lbalen->len = num_blocks;
9160 if (bytchk) {
9161 lbalen->flags = CTL_LLF_COMPARE | flags;
9162 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9163 } else {
9164 lbalen->flags = CTL_LLF_VERIFY | flags;
9165 ctsio->kern_total_len = 0;
9166 }
9167 ctsio->kern_rel_offset = 0;
9168
9169 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9170 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9171 return (retval);
9172 }
9173
9174 int
ctl_report_luns(struct ctl_scsiio * ctsio)9175 ctl_report_luns(struct ctl_scsiio *ctsio)
9176 {
9177 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9178 struct ctl_port *port = CTL_PORT(ctsio);
9179 struct ctl_lun *lun, *request_lun = CTL_LUN(ctsio);
9180 struct scsi_report_luns *cdb;
9181 struct scsi_report_luns_data *lun_data;
9182 int num_filled, num_luns, num_port_luns, retval;
9183 uint32_t alloc_len, lun_datalen;
9184 uint32_t initidx, targ_lun_id, lun_id;
9185
9186 retval = CTL_RETVAL_COMPLETE;
9187 cdb = (struct scsi_report_luns *)ctsio->cdb;
9188
9189 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9190
9191 num_luns = 0;
9192 num_port_luns = port->lun_map ? port->lun_map_size : ctl_max_luns;
9193 mtx_lock(&softc->ctl_lock);
9194 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) {
9195 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX)
9196 num_luns++;
9197 }
9198 mtx_unlock(&softc->ctl_lock);
9199
9200 switch (cdb->select_report) {
9201 case RPL_REPORT_DEFAULT:
9202 case RPL_REPORT_ALL:
9203 case RPL_REPORT_NONSUBSID:
9204 break;
9205 case RPL_REPORT_WELLKNOWN:
9206 case RPL_REPORT_ADMIN:
9207 case RPL_REPORT_CONGLOM:
9208 num_luns = 0;
9209 break;
9210 default:
9211 ctl_set_invalid_field(ctsio,
9212 /*sks_valid*/ 1,
9213 /*command*/ 1,
9214 /*field*/ 2,
9215 /*bit_valid*/ 0,
9216 /*bit*/ 0);
9217 ctl_done((union ctl_io *)ctsio);
9218 return (retval);
9219 break; /* NOTREACHED */
9220 }
9221
9222 alloc_len = scsi_4btoul(cdb->length);
9223 /*
9224 * The initiator has to allocate at least 16 bytes for this request,
9225 * so he can at least get the header and the first LUN. Otherwise
9226 * we reject the request (per SPC-3 rev 14, section 6.21).
9227 */
9228 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9229 sizeof(struct scsi_report_luns_lundata))) {
9230 ctl_set_invalid_field(ctsio,
9231 /*sks_valid*/ 1,
9232 /*command*/ 1,
9233 /*field*/ 6,
9234 /*bit_valid*/ 0,
9235 /*bit*/ 0);
9236 ctl_done((union ctl_io *)ctsio);
9237 return (retval);
9238 }
9239
9240 lun_datalen = sizeof(*lun_data) +
9241 (num_luns * sizeof(struct scsi_report_luns_lundata));
9242
9243 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9244 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9245 ctsio->kern_sg_entries = 0;
9246
9247 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9248
9249 mtx_lock(&softc->ctl_lock);
9250 for (targ_lun_id = 0, num_filled = 0;
9251 targ_lun_id < num_port_luns && num_filled < num_luns;
9252 targ_lun_id++) {
9253 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9254 if (lun_id == UINT32_MAX)
9255 continue;
9256 lun = softc->ctl_luns[lun_id];
9257 if (lun == NULL)
9258 continue;
9259
9260 be64enc(lun_data->luns[num_filled++].lundata,
9261 ctl_encode_lun(targ_lun_id));
9262
9263 /*
9264 * According to SPC-3, rev 14 section 6.21:
9265 *
9266 * "The execution of a REPORT LUNS command to any valid and
9267 * installed logical unit shall clear the REPORTED LUNS DATA
9268 * HAS CHANGED unit attention condition for all logical
9269 * units of that target with respect to the requesting
9270 * initiator. A valid and installed logical unit is one
9271 * having a PERIPHERAL QUALIFIER of 000b in the standard
9272 * INQUIRY data (see 6.4.2)."
9273 *
9274 * If request_lun is NULL, the LUN this report luns command
9275 * was issued to is either disabled or doesn't exist. In that
9276 * case, we shouldn't clear any pending lun change unit
9277 * attention.
9278 */
9279 if (request_lun != NULL) {
9280 mtx_lock(&lun->lun_lock);
9281 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9282 mtx_unlock(&lun->lun_lock);
9283 }
9284 }
9285 mtx_unlock(&softc->ctl_lock);
9286
9287 /*
9288 * It's quite possible that we've returned fewer LUNs than we allocated
9289 * space for. Trim it.
9290 */
9291 lun_datalen = sizeof(*lun_data) +
9292 (num_filled * sizeof(struct scsi_report_luns_lundata));
9293 ctsio->kern_rel_offset = 0;
9294 ctsio->kern_sg_entries = 0;
9295 ctsio->kern_data_len = min(lun_datalen, alloc_len);
9296 ctsio->kern_total_len = ctsio->kern_data_len;
9297
9298 /*
9299 * We set this to the actual data length, regardless of how much
9300 * space we actually have to return results. If the user looks at
9301 * this value, he'll know whether or not he allocated enough space
9302 * and reissue the command if necessary. We don't support well
9303 * known logical units, so if the user asks for that, return none.
9304 */
9305 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9306
9307 /*
9308 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9309 * this request.
9310 */
9311 ctl_set_success(ctsio);
9312 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9313 ctsio->be_move_done = ctl_config_move_done;
9314 ctl_datamove((union ctl_io *)ctsio);
9315 return (retval);
9316 }
9317
9318 int
ctl_request_sense(struct ctl_scsiio * ctsio)9319 ctl_request_sense(struct ctl_scsiio *ctsio)
9320 {
9321 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9322 struct ctl_lun *lun = CTL_LUN(ctsio);
9323 struct scsi_request_sense *cdb;
9324 struct scsi_sense_data *sense_ptr, *ps;
9325 uint32_t initidx;
9326 int have_error;
9327 u_int sense_len = SSD_FULL_SIZE;
9328 scsi_sense_data_type sense_format;
9329 ctl_ua_type ua_type;
9330 uint8_t asc = 0, ascq = 0;
9331
9332 cdb = (struct scsi_request_sense *)ctsio->cdb;
9333
9334 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9335
9336 /*
9337 * Determine which sense format the user wants.
9338 */
9339 if (cdb->byte2 & SRS_DESC)
9340 sense_format = SSD_TYPE_DESC;
9341 else
9342 sense_format = SSD_TYPE_FIXED;
9343
9344 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9345 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9346 ctsio->kern_sg_entries = 0;
9347 ctsio->kern_rel_offset = 0;
9348 ctsio->kern_data_len = ctsio->kern_total_len =
9349 MIN(cdb->length, sizeof(*sense_ptr));
9350
9351 /*
9352 * If we don't have a LUN, we don't have any pending sense.
9353 */
9354 if (lun == NULL ||
9355 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
9356 softc->ha_link < CTL_HA_LINK_UNKNOWN)) {
9357 /* "Logical unit not supported" */
9358 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format,
9359 /*current_error*/ 1,
9360 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
9361 /*asc*/ 0x25,
9362 /*ascq*/ 0x00,
9363 SSD_ELEM_NONE);
9364 goto send;
9365 }
9366
9367 have_error = 0;
9368 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9369 /*
9370 * Check for pending sense, and then for pending unit attentions.
9371 * Pending sense gets returned first, then pending unit attentions.
9372 */
9373 mtx_lock(&lun->lun_lock);
9374 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
9375 if (ps != NULL)
9376 ps += initidx % CTL_MAX_INIT_PER_PORT;
9377 if (ps != NULL && ps->error_code != 0) {
9378 scsi_sense_data_type stored_format;
9379
9380 /*
9381 * Check to see which sense format was used for the stored
9382 * sense data.
9383 */
9384 stored_format = scsi_sense_type(ps);
9385
9386 /*
9387 * If the user requested a different sense format than the
9388 * one we stored, then we need to convert it to the other
9389 * format. If we're going from descriptor to fixed format
9390 * sense data, we may lose things in translation, depending
9391 * on what options were used.
9392 *
9393 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9394 * for some reason we'll just copy it out as-is.
9395 */
9396 if ((stored_format == SSD_TYPE_FIXED)
9397 && (sense_format == SSD_TYPE_DESC))
9398 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9399 ps, (struct scsi_sense_data_desc *)sense_ptr);
9400 else if ((stored_format == SSD_TYPE_DESC)
9401 && (sense_format == SSD_TYPE_FIXED))
9402 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9403 ps, (struct scsi_sense_data_fixed *)sense_ptr);
9404 else
9405 memcpy(sense_ptr, ps, sizeof(*sense_ptr));
9406
9407 ps->error_code = 0;
9408 have_error = 1;
9409 } else {
9410 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len,
9411 sense_format);
9412 if (ua_type != CTL_UA_NONE)
9413 have_error = 1;
9414 }
9415 if (have_error == 0) {
9416 /*
9417 * Report informational exception if have one and allowed.
9418 */
9419 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) {
9420 asc = lun->ie_asc;
9421 ascq = lun->ie_ascq;
9422 }
9423 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format,
9424 /*current_error*/ 1,
9425 /*sense_key*/ SSD_KEY_NO_SENSE,
9426 /*asc*/ asc,
9427 /*ascq*/ ascq,
9428 SSD_ELEM_NONE);
9429 }
9430 mtx_unlock(&lun->lun_lock);
9431
9432 send:
9433 /*
9434 * We report the SCSI status as OK, since the status of the command
9435 * itself is OK. We're reporting sense as parameter data.
9436 */
9437 ctl_set_success(ctsio);
9438 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9439 ctsio->be_move_done = ctl_config_move_done;
9440 ctl_datamove((union ctl_io *)ctsio);
9441 return (CTL_RETVAL_COMPLETE);
9442 }
9443
9444 int
ctl_tur(struct ctl_scsiio * ctsio)9445 ctl_tur(struct ctl_scsiio *ctsio)
9446 {
9447
9448 CTL_DEBUG_PRINT(("ctl_tur\n"));
9449
9450 ctl_set_success(ctsio);
9451 ctl_done((union ctl_io *)ctsio);
9452
9453 return (CTL_RETVAL_COMPLETE);
9454 }
9455
9456 /*
9457 * SCSI VPD page 0x00, the Supported VPD Pages page.
9458 */
9459 static int
ctl_inquiry_evpd_supported(struct ctl_scsiio * ctsio,int alloc_len)9460 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9461 {
9462 struct ctl_lun *lun = CTL_LUN(ctsio);
9463 struct scsi_vpd_supported_pages *pages;
9464 int sup_page_size;
9465 int p;
9466
9467 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9468 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9469 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9470 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9471 ctsio->kern_rel_offset = 0;
9472 ctsio->kern_sg_entries = 0;
9473 ctsio->kern_data_len = min(sup_page_size, alloc_len);
9474 ctsio->kern_total_len = ctsio->kern_data_len;
9475
9476 /*
9477 * The control device is always connected. The disk device, on the
9478 * other hand, may not be online all the time. Need to change this
9479 * to figure out whether the disk device is actually online or not.
9480 */
9481 if (lun != NULL)
9482 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9483 lun->be_lun->lun_type;
9484 else
9485 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9486
9487 p = 0;
9488 /* Supported VPD pages */
9489 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9490 /* Serial Number */
9491 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9492 /* Device Identification */
9493 pages->page_list[p++] = SVPD_DEVICE_ID;
9494 /* Extended INQUIRY Data */
9495 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9496 /* Mode Page Policy */
9497 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9498 /* SCSI Ports */
9499 pages->page_list[p++] = SVPD_SCSI_PORTS;
9500 /* Third-party Copy */
9501 pages->page_list[p++] = SVPD_SCSI_TPC;
9502 /* SCSI Feature Sets */
9503 pages->page_list[p++] = SVPD_SCSI_SFS;
9504 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9505 /* Block limits */
9506 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9507 /* Block Device Characteristics */
9508 pages->page_list[p++] = SVPD_BDC;
9509 /* Logical Block Provisioning */
9510 pages->page_list[p++] = SVPD_LBP;
9511 }
9512 pages->length = p;
9513
9514 ctl_set_success(ctsio);
9515 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9516 ctsio->be_move_done = ctl_config_move_done;
9517 ctl_datamove((union ctl_io *)ctsio);
9518 return (CTL_RETVAL_COMPLETE);
9519 }
9520
9521 /*
9522 * SCSI VPD page 0x80, the Unit Serial Number page.
9523 */
9524 static int
ctl_inquiry_evpd_serial(struct ctl_scsiio * ctsio,int alloc_len)9525 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9526 {
9527 struct ctl_lun *lun = CTL_LUN(ctsio);
9528 struct scsi_vpd_unit_serial_number *sn_ptr;
9529 int data_len;
9530
9531 data_len = 4 + CTL_SN_LEN;
9532 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9533 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9534 ctsio->kern_rel_offset = 0;
9535 ctsio->kern_sg_entries = 0;
9536 ctsio->kern_data_len = min(data_len, alloc_len);
9537 ctsio->kern_total_len = ctsio->kern_data_len;
9538
9539 /*
9540 * The control device is always connected. The disk device, on the
9541 * other hand, may not be online all the time. Need to change this
9542 * to figure out whether the disk device is actually online or not.
9543 */
9544 if (lun != NULL)
9545 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9546 lun->be_lun->lun_type;
9547 else
9548 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9549
9550 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9551 sn_ptr->length = CTL_SN_LEN;
9552 /*
9553 * If we don't have a LUN, we just leave the serial number as
9554 * all spaces.
9555 */
9556 if (lun != NULL) {
9557 strncpy((char *)sn_ptr->serial_num,
9558 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9559 } else
9560 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9561
9562 ctl_set_success(ctsio);
9563 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9564 ctsio->be_move_done = ctl_config_move_done;
9565 ctl_datamove((union ctl_io *)ctsio);
9566 return (CTL_RETVAL_COMPLETE);
9567 }
9568
9569 /*
9570 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9571 */
9572 static int
ctl_inquiry_evpd_eid(struct ctl_scsiio * ctsio,int alloc_len)9573 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9574 {
9575 struct ctl_lun *lun = CTL_LUN(ctsio);
9576 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9577 int data_len;
9578
9579 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9580 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9581 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9582 ctsio->kern_sg_entries = 0;
9583 ctsio->kern_rel_offset = 0;
9584 ctsio->kern_data_len = min(data_len, alloc_len);
9585 ctsio->kern_total_len = ctsio->kern_data_len;
9586
9587 /*
9588 * The control device is always connected. The disk device, on the
9589 * other hand, may not be online all the time.
9590 */
9591 if (lun != NULL)
9592 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9593 lun->be_lun->lun_type;
9594 else
9595 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9596 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9597 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9598 /*
9599 * We support head of queue, ordered and simple tags.
9600 */
9601 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9602 /*
9603 * Volatile cache supported.
9604 */
9605 eid_ptr->flags3 = SVPD_EID_V_SUP;
9606
9607 /*
9608 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9609 * attention for a particular IT nexus on all LUNs once we report
9610 * it to that nexus once. This bit is required as of SPC-4.
9611 */
9612 eid_ptr->flags4 = SVPD_EID_LUICLR;
9613
9614 /*
9615 * We support revert to defaults (RTD) bit in MODE SELECT.
9616 */
9617 eid_ptr->flags5 = SVPD_EID_RTD_SUP;
9618
9619 /*
9620 * XXX KDM in order to correctly answer this, we would need
9621 * information from the SIM to determine how much sense data it
9622 * can send. So this would really be a path inquiry field, most
9623 * likely. This can be set to a maximum of 252 according to SPC-4,
9624 * but the hardware may or may not be able to support that much.
9625 * 0 just means that the maximum sense data length is not reported.
9626 */
9627 eid_ptr->max_sense_length = 0;
9628
9629 ctl_set_success(ctsio);
9630 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9631 ctsio->be_move_done = ctl_config_move_done;
9632 ctl_datamove((union ctl_io *)ctsio);
9633 return (CTL_RETVAL_COMPLETE);
9634 }
9635
9636 static int
ctl_inquiry_evpd_mpp(struct ctl_scsiio * ctsio,int alloc_len)9637 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9638 {
9639 struct ctl_lun *lun = CTL_LUN(ctsio);
9640 struct scsi_vpd_mode_page_policy *mpp_ptr;
9641 int data_len;
9642
9643 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9644 sizeof(struct scsi_vpd_mode_page_policy_descr);
9645
9646 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9647 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9648 ctsio->kern_rel_offset = 0;
9649 ctsio->kern_sg_entries = 0;
9650 ctsio->kern_data_len = min(data_len, alloc_len);
9651 ctsio->kern_total_len = ctsio->kern_data_len;
9652
9653 /*
9654 * The control device is always connected. The disk device, on the
9655 * other hand, may not be online all the time.
9656 */
9657 if (lun != NULL)
9658 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9659 lun->be_lun->lun_type;
9660 else
9661 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9662 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9663 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9664 mpp_ptr->descr[0].page_code = 0x3f;
9665 mpp_ptr->descr[0].subpage_code = 0xff;
9666 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9667
9668 ctl_set_success(ctsio);
9669 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9670 ctsio->be_move_done = ctl_config_move_done;
9671 ctl_datamove((union ctl_io *)ctsio);
9672 return (CTL_RETVAL_COMPLETE);
9673 }
9674
9675 /*
9676 * SCSI VPD page 0x83, the Device Identification page.
9677 */
9678 static int
ctl_inquiry_evpd_devid(struct ctl_scsiio * ctsio,int alloc_len)9679 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9680 {
9681 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9682 struct ctl_port *port = CTL_PORT(ctsio);
9683 struct ctl_lun *lun = CTL_LUN(ctsio);
9684 struct scsi_vpd_device_id *devid_ptr;
9685 struct scsi_vpd_id_descriptor *desc;
9686 int data_len, g;
9687 uint8_t proto;
9688
9689 data_len = sizeof(struct scsi_vpd_device_id) +
9690 sizeof(struct scsi_vpd_id_descriptor) +
9691 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9692 sizeof(struct scsi_vpd_id_descriptor) +
9693 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9694 if (lun && lun->lun_devid)
9695 data_len += lun->lun_devid->len;
9696 if (port && port->port_devid)
9697 data_len += port->port_devid->len;
9698 if (port && port->target_devid)
9699 data_len += port->target_devid->len;
9700
9701 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9702 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9703 ctsio->kern_sg_entries = 0;
9704 ctsio->kern_rel_offset = 0;
9705 ctsio->kern_sg_entries = 0;
9706 ctsio->kern_data_len = min(data_len, alloc_len);
9707 ctsio->kern_total_len = ctsio->kern_data_len;
9708
9709 /*
9710 * The control device is always connected. The disk device, on the
9711 * other hand, may not be online all the time.
9712 */
9713 if (lun != NULL)
9714 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9715 lun->be_lun->lun_type;
9716 else
9717 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9718 devid_ptr->page_code = SVPD_DEVICE_ID;
9719 scsi_ulto2b(data_len - 4, devid_ptr->length);
9720
9721 if (port && port->port_type == CTL_PORT_FC)
9722 proto = SCSI_PROTO_FC << 4;
9723 else if (port && port->port_type == CTL_PORT_SAS)
9724 proto = SCSI_PROTO_SAS << 4;
9725 else if (port && port->port_type == CTL_PORT_ISCSI)
9726 proto = SCSI_PROTO_ISCSI << 4;
9727 else
9728 proto = SCSI_PROTO_SPI << 4;
9729 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9730
9731 /*
9732 * We're using a LUN association here. i.e., this device ID is a
9733 * per-LUN identifier.
9734 */
9735 if (lun && lun->lun_devid) {
9736 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9737 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9738 lun->lun_devid->len);
9739 }
9740
9741 /*
9742 * This is for the WWPN which is a port association.
9743 */
9744 if (port && port->port_devid) {
9745 memcpy(desc, port->port_devid->data, port->port_devid->len);
9746 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9747 port->port_devid->len);
9748 }
9749
9750 /*
9751 * This is for the Relative Target Port(type 4h) identifier
9752 */
9753 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9754 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9755 SVPD_ID_TYPE_RELTARG;
9756 desc->length = 4;
9757 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9758 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9759 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9760
9761 /*
9762 * This is for the Target Port Group(type 5h) identifier
9763 */
9764 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9765 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9766 SVPD_ID_TYPE_TPORTGRP;
9767 desc->length = 4;
9768 if (softc->is_single ||
9769 (port && port->status & CTL_PORT_STATUS_HA_SHARED))
9770 g = 1;
9771 else
9772 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt;
9773 scsi_ulto2b(g, &desc->identifier[2]);
9774 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9775 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9776
9777 /*
9778 * This is for the Target identifier
9779 */
9780 if (port && port->target_devid) {
9781 memcpy(desc, port->target_devid->data, port->target_devid->len);
9782 }
9783
9784 ctl_set_success(ctsio);
9785 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9786 ctsio->be_move_done = ctl_config_move_done;
9787 ctl_datamove((union ctl_io *)ctsio);
9788 return (CTL_RETVAL_COMPLETE);
9789 }
9790
9791 static int
ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio * ctsio,int alloc_len)9792 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9793 {
9794 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9795 struct ctl_lun *lun = CTL_LUN(ctsio);
9796 struct scsi_vpd_scsi_ports *sp;
9797 struct scsi_vpd_port_designation *pd;
9798 struct scsi_vpd_port_designation_cont *pdc;
9799 struct ctl_port *port;
9800 int data_len, num_target_ports, iid_len, id_len;
9801
9802 num_target_ports = 0;
9803 iid_len = 0;
9804 id_len = 0;
9805 mtx_lock(&softc->ctl_lock);
9806 STAILQ_FOREACH(port, &softc->port_list, links) {
9807 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9808 continue;
9809 if (lun != NULL &&
9810 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9811 continue;
9812 num_target_ports++;
9813 if (port->init_devid)
9814 iid_len += port->init_devid->len;
9815 if (port->port_devid)
9816 id_len += port->port_devid->len;
9817 }
9818 mtx_unlock(&softc->ctl_lock);
9819
9820 data_len = sizeof(struct scsi_vpd_scsi_ports) +
9821 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9822 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9823 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9824 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9825 ctsio->kern_sg_entries = 0;
9826 ctsio->kern_rel_offset = 0;
9827 ctsio->kern_sg_entries = 0;
9828 ctsio->kern_data_len = min(data_len, alloc_len);
9829 ctsio->kern_total_len = ctsio->kern_data_len;
9830
9831 /*
9832 * The control device is always connected. The disk device, on the
9833 * other hand, may not be online all the time. Need to change this
9834 * to figure out whether the disk device is actually online or not.
9835 */
9836 if (lun != NULL)
9837 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9838 lun->be_lun->lun_type;
9839 else
9840 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9841
9842 sp->page_code = SVPD_SCSI_PORTS;
9843 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9844 sp->page_length);
9845 pd = &sp->design[0];
9846
9847 mtx_lock(&softc->ctl_lock);
9848 STAILQ_FOREACH(port, &softc->port_list, links) {
9849 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9850 continue;
9851 if (lun != NULL &&
9852 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9853 continue;
9854 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9855 if (port->init_devid) {
9856 iid_len = port->init_devid->len;
9857 memcpy(pd->initiator_transportid,
9858 port->init_devid->data, port->init_devid->len);
9859 } else
9860 iid_len = 0;
9861 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9862 pdc = (struct scsi_vpd_port_designation_cont *)
9863 (&pd->initiator_transportid[iid_len]);
9864 if (port->port_devid) {
9865 id_len = port->port_devid->len;
9866 memcpy(pdc->target_port_descriptors,
9867 port->port_devid->data, port->port_devid->len);
9868 } else
9869 id_len = 0;
9870 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9871 pd = (struct scsi_vpd_port_designation *)
9872 ((uint8_t *)pdc->target_port_descriptors + id_len);
9873 }
9874 mtx_unlock(&softc->ctl_lock);
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_sfs(struct ctl_scsiio * ctsio,int alloc_len)9884 ctl_inquiry_evpd_sfs(struct ctl_scsiio *ctsio, int alloc_len)
9885 {
9886 struct ctl_lun *lun = CTL_LUN(ctsio);
9887 struct scsi_vpd_sfs *sfs_ptr;
9888 int sfs_page_size, n;
9889
9890 sfs_page_size = sizeof(*sfs_ptr) + 5 * 2;
9891 ctsio->kern_data_ptr = malloc(sfs_page_size, M_CTL, M_WAITOK | M_ZERO);
9892 sfs_ptr = (struct scsi_vpd_sfs *)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(sfs_page_size, 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 sfs_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9906 lun->be_lun->lun_type;
9907 else
9908 sfs_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9909
9910 sfs_ptr->page_code = SVPD_SCSI_SFS;
9911 n = 0;
9912 /* Discovery 2016 */
9913 scsi_ulto2b(0x0001, &sfs_ptr->codes[2 * n++]);
9914 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9915 /* SBC Base 2016 */
9916 scsi_ulto2b(0x0101, &sfs_ptr->codes[2 * n++]);
9917 /* SBC Base 2010 */
9918 scsi_ulto2b(0x0102, &sfs_ptr->codes[2 * n++]);
9919 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9920 /* Basic Provisioning 2016 */
9921 scsi_ulto2b(0x0103, &sfs_ptr->codes[2 * n++]);
9922 }
9923 /* Drive Maintenance 2016 */
9924 //scsi_ulto2b(0x0104, &sfs_ptr->codes[2 * n++]);
9925 }
9926 scsi_ulto2b(4 + 2 * n, sfs_ptr->page_length);
9927
9928 ctl_set_success(ctsio);
9929 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9930 ctsio->be_move_done = ctl_config_move_done;
9931 ctl_datamove((union ctl_io *)ctsio);
9932 return (CTL_RETVAL_COMPLETE);
9933 }
9934
9935 static int
ctl_inquiry_evpd_block_limits(struct ctl_scsiio * ctsio,int alloc_len)9936 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9937 {
9938 struct ctl_lun *lun = CTL_LUN(ctsio);
9939 struct scsi_vpd_block_limits *bl_ptr;
9940 const char *val;
9941 uint64_t ival;
9942
9943 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9944 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9945 ctsio->kern_sg_entries = 0;
9946 ctsio->kern_rel_offset = 0;
9947 ctsio->kern_sg_entries = 0;
9948 ctsio->kern_data_len = min(sizeof(*bl_ptr), alloc_len);
9949 ctsio->kern_total_len = ctsio->kern_data_len;
9950
9951 /*
9952 * The control device is always connected. The disk device, on the
9953 * other hand, may not be online all the time. Need to change this
9954 * to figure out whether the disk device is actually online or not.
9955 */
9956 if (lun != NULL)
9957 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9958 lun->be_lun->lun_type;
9959 else
9960 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9961
9962 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9963 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9964 bl_ptr->max_cmp_write_len = 0xff;
9965 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9966 if (lun != NULL) {
9967 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9968 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9969 ival = 0xffffffff;
9970 val = dnvlist_get_string(lun->be_lun->options,
9971 "unmap_max_lba", NULL);
9972 if (val != NULL)
9973 ctl_expand_number(val, &ival);
9974 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt);
9975 ival = 0xffffffff;
9976 val = dnvlist_get_string(lun->be_lun->options,
9977 "unmap_max_descr", NULL);
9978 if (val != NULL)
9979 ctl_expand_number(val, &ival);
9980 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt);
9981 if (lun->be_lun->ublockexp != 0) {
9982 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9983 bl_ptr->opt_unmap_grain);
9984 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9985 bl_ptr->unmap_grain_align);
9986 }
9987 }
9988 scsi_ulto4b(lun->be_lun->atomicblock,
9989 bl_ptr->max_atomic_transfer_length);
9990 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9991 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9992 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary);
9993 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size);
9994 ival = UINT64_MAX;
9995 val = dnvlist_get_string(lun->be_lun->options,
9996 "write_same_max_lba", NULL);
9997 if (val != NULL)
9998 ctl_expand_number(val, &ival);
9999 scsi_u64to8b(ival, bl_ptr->max_write_same_length);
10000 if (lun->be_lun->maxlba + 1 > ival)
10001 bl_ptr->flags |= SVPD_BL_WSNZ;
10002 }
10003
10004 ctl_set_success(ctsio);
10005 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10006 ctsio->be_move_done = ctl_config_move_done;
10007 ctl_datamove((union ctl_io *)ctsio);
10008 return (CTL_RETVAL_COMPLETE);
10009 }
10010
10011 static int
ctl_inquiry_evpd_bdc(struct ctl_scsiio * ctsio,int alloc_len)10012 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
10013 {
10014 struct ctl_lun *lun = CTL_LUN(ctsio);
10015 struct scsi_vpd_block_device_characteristics *bdc_ptr;
10016 const char *value;
10017 u_int i;
10018
10019 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
10020 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
10021 ctsio->kern_sg_entries = 0;
10022 ctsio->kern_rel_offset = 0;
10023 ctsio->kern_data_len = min(sizeof(*bdc_ptr), alloc_len);
10024 ctsio->kern_total_len = ctsio->kern_data_len;
10025
10026 /*
10027 * The control device is always connected. The disk device, on the
10028 * other hand, may not be online all the time. Need to change this
10029 * to figure out whether the disk device is actually online or not.
10030 */
10031 if (lun != NULL)
10032 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10033 lun->be_lun->lun_type;
10034 else
10035 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10036 bdc_ptr->page_code = SVPD_BDC;
10037 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
10038 if (lun != NULL &&
10039 (value = dnvlist_get_string(lun->be_lun->options, "rpm", NULL)) != NULL)
10040 i = strtol(value, NULL, 0);
10041 else
10042 i = CTL_DEFAULT_ROTATION_RATE;
10043 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
10044 if (lun != NULL &&
10045 (value = dnvlist_get_string(lun->be_lun->options, "formfactor", NULL)) != NULL)
10046 i = strtol(value, NULL, 0);
10047 else
10048 i = 0;
10049 bdc_ptr->wab_wac_ff = (i & 0x0f);
10050 bdc_ptr->flags = SVPD_RBWZ | SVPD_FUAB | SVPD_VBULS;
10051
10052 ctl_set_success(ctsio);
10053 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10054 ctsio->be_move_done = ctl_config_move_done;
10055 ctl_datamove((union ctl_io *)ctsio);
10056 return (CTL_RETVAL_COMPLETE);
10057 }
10058
10059 static int
ctl_inquiry_evpd_lbp(struct ctl_scsiio * ctsio,int alloc_len)10060 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
10061 {
10062 struct ctl_lun *lun = CTL_LUN(ctsio);
10063 struct scsi_vpd_logical_block_prov *lbp_ptr;
10064 const char *value;
10065
10066 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
10067 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
10068 ctsio->kern_sg_entries = 0;
10069 ctsio->kern_rel_offset = 0;
10070 ctsio->kern_data_len = min(sizeof(*lbp_ptr), alloc_len);
10071 ctsio->kern_total_len = ctsio->kern_data_len;
10072
10073 /*
10074 * The control device is always connected. The disk device, on the
10075 * other hand, may not be online all the time. Need to change this
10076 * to figure out whether the disk device is actually online or not.
10077 */
10078 if (lun != NULL)
10079 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10080 lun->be_lun->lun_type;
10081 else
10082 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10083
10084 lbp_ptr->page_code = SVPD_LBP;
10085 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
10086 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
10087 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10088 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10089 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10090 value = dnvlist_get_string(lun->be_lun->options,
10091 "provisioning_type", NULL);
10092 if (value != NULL) {
10093 if (strcmp(value, "resource") == 0)
10094 lbp_ptr->prov_type = SVPD_LBP_RESOURCE;
10095 else if (strcmp(value, "thin") == 0)
10096 lbp_ptr->prov_type = SVPD_LBP_THIN;
10097 } else
10098 lbp_ptr->prov_type = SVPD_LBP_THIN;
10099 }
10100
10101 ctl_set_success(ctsio);
10102 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10103 ctsio->be_move_done = ctl_config_move_done;
10104 ctl_datamove((union ctl_io *)ctsio);
10105 return (CTL_RETVAL_COMPLETE);
10106 }
10107
10108 /*
10109 * INQUIRY with the EVPD bit set.
10110 */
10111 static int
ctl_inquiry_evpd(struct ctl_scsiio * ctsio)10112 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10113 {
10114 struct ctl_lun *lun = CTL_LUN(ctsio);
10115 struct scsi_inquiry *cdb;
10116 int alloc_len, retval;
10117
10118 cdb = (struct scsi_inquiry *)ctsio->cdb;
10119 alloc_len = scsi_2btoul(cdb->length);
10120
10121 switch (cdb->page_code) {
10122 case SVPD_SUPPORTED_PAGES:
10123 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10124 break;
10125 case SVPD_UNIT_SERIAL_NUMBER:
10126 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10127 break;
10128 case SVPD_DEVICE_ID:
10129 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10130 break;
10131 case SVPD_EXTENDED_INQUIRY_DATA:
10132 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10133 break;
10134 case SVPD_MODE_PAGE_POLICY:
10135 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10136 break;
10137 case SVPD_SCSI_PORTS:
10138 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10139 break;
10140 case SVPD_SCSI_TPC:
10141 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10142 break;
10143 case SVPD_SCSI_SFS:
10144 retval = ctl_inquiry_evpd_sfs(ctsio, alloc_len);
10145 break;
10146 case SVPD_BLOCK_LIMITS:
10147 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10148 goto err;
10149 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10150 break;
10151 case SVPD_BDC:
10152 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10153 goto err;
10154 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10155 break;
10156 case SVPD_LBP:
10157 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10158 goto err;
10159 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10160 break;
10161 default:
10162 err:
10163 ctl_set_invalid_field(ctsio,
10164 /*sks_valid*/ 1,
10165 /*command*/ 1,
10166 /*field*/ 2,
10167 /*bit_valid*/ 0,
10168 /*bit*/ 0);
10169 ctl_done((union ctl_io *)ctsio);
10170 retval = CTL_RETVAL_COMPLETE;
10171 break;
10172 }
10173
10174 return (retval);
10175 }
10176
10177 /*
10178 * Standard INQUIRY data.
10179 */
10180 static int
ctl_inquiry_std(struct ctl_scsiio * ctsio)10181 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10182 {
10183 struct ctl_softc *softc = CTL_SOFTC(ctsio);
10184 struct ctl_port *port = CTL_PORT(ctsio);
10185 struct ctl_lun *lun = CTL_LUN(ctsio);
10186 struct scsi_inquiry_data *inq_ptr;
10187 struct scsi_inquiry *cdb;
10188 const char *val;
10189 uint32_t alloc_len, data_len;
10190 ctl_port_type port_type;
10191
10192 port_type = port->port_type;
10193 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10194 port_type = CTL_PORT_SCSI;
10195
10196 cdb = (struct scsi_inquiry *)ctsio->cdb;
10197 alloc_len = scsi_2btoul(cdb->length);
10198
10199 /*
10200 * We malloc the full inquiry data size here and fill it
10201 * in. If the user only asks for less, we'll give him
10202 * that much.
10203 */
10204 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10205 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10206 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10207 ctsio->kern_sg_entries = 0;
10208 ctsio->kern_rel_offset = 0;
10209 ctsio->kern_data_len = min(data_len, alloc_len);
10210 ctsio->kern_total_len = ctsio->kern_data_len;
10211
10212 if (lun != NULL) {
10213 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
10214 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
10215 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10216 lun->be_lun->lun_type;
10217 } else {
10218 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
10219 lun->be_lun->lun_type;
10220 }
10221 if (lun->flags & CTL_LUN_REMOVABLE)
10222 inq_ptr->dev_qual2 |= SID_RMB;
10223 } else
10224 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10225
10226 /* RMB in byte 2 is 0 */
10227 inq_ptr->version = SCSI_REV_SPC5;
10228
10229 /*
10230 * According to SAM-3, even if a device only supports a single
10231 * level of LUN addressing, it should still set the HISUP bit:
10232 *
10233 * 4.9.1 Logical unit numbers overview
10234 *
10235 * All logical unit number formats described in this standard are
10236 * hierarchical in structure even when only a single level in that
10237 * hierarchy is used. The HISUP bit shall be set to one in the
10238 * standard INQUIRY data (see SPC-2) when any logical unit number
10239 * format described in this standard is used. Non-hierarchical
10240 * formats are outside the scope of this standard.
10241 *
10242 * Therefore we set the HiSup bit here.
10243 *
10244 * The response format is 2, per SPC-3.
10245 */
10246 inq_ptr->response_format = SID_HiSup | 2;
10247
10248 inq_ptr->additional_length = data_len -
10249 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10250 CTL_DEBUG_PRINT(("additional_length = %d\n",
10251 inq_ptr->additional_length));
10252
10253 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10254 if (port_type == CTL_PORT_SCSI)
10255 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10256 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10257 inq_ptr->flags = SID_CmdQue;
10258 if (port_type == CTL_PORT_SCSI)
10259 inq_ptr->flags |= SID_WBus16 | SID_Sync;
10260
10261 /*
10262 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10263 * We have 8 bytes for the vendor name, and 16 bytes for the device
10264 * name and 4 bytes for the revision.
10265 */
10266 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options,
10267 "vendor", NULL)) == NULL) {
10268 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10269 } else {
10270 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10271 strncpy(inq_ptr->vendor, val,
10272 min(sizeof(inq_ptr->vendor), strlen(val)));
10273 }
10274 if (lun == NULL) {
10275 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10276 sizeof(inq_ptr->product));
10277 } else if ((val = dnvlist_get_string(lun->be_lun->options, "product",
10278 NULL)) == NULL) {
10279 switch (lun->be_lun->lun_type) {
10280 case T_DIRECT:
10281 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10282 sizeof(inq_ptr->product));
10283 break;
10284 case T_PROCESSOR:
10285 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10286 sizeof(inq_ptr->product));
10287 break;
10288 case T_CDROM:
10289 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT,
10290 sizeof(inq_ptr->product));
10291 break;
10292 default:
10293 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10294 sizeof(inq_ptr->product));
10295 break;
10296 }
10297 } else {
10298 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10299 strncpy(inq_ptr->product, val,
10300 min(sizeof(inq_ptr->product), strlen(val)));
10301 }
10302
10303 /*
10304 * XXX make this a macro somewhere so it automatically gets
10305 * incremented when we make changes.
10306 */
10307 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options,
10308 "revision", NULL)) == NULL) {
10309 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10310 } else {
10311 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10312 strncpy(inq_ptr->revision, val,
10313 min(sizeof(inq_ptr->revision), strlen(val)));
10314 }
10315
10316 /*
10317 * For parallel SCSI, we support double transition and single
10318 * transition clocking. We also support QAS (Quick Arbitration
10319 * and Selection) and Information Unit transfers on both the
10320 * control and array devices.
10321 */
10322 if (port_type == CTL_PORT_SCSI)
10323 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10324 SID_SPI_IUS;
10325
10326 /* SAM-6 (no version claimed) */
10327 scsi_ulto2b(0x00C0, inq_ptr->version1);
10328 /* SPC-5 (no version claimed) */
10329 scsi_ulto2b(0x05C0, inq_ptr->version2);
10330 if (port_type == CTL_PORT_FC) {
10331 /* FCP-2 ANSI INCITS.350:2003 */
10332 scsi_ulto2b(0x0917, inq_ptr->version3);
10333 } else if (port_type == CTL_PORT_SCSI) {
10334 /* SPI-4 ANSI INCITS.362:200x */
10335 scsi_ulto2b(0x0B56, inq_ptr->version3);
10336 } else if (port_type == CTL_PORT_ISCSI) {
10337 /* iSCSI (no version claimed) */
10338 scsi_ulto2b(0x0960, inq_ptr->version3);
10339 } else if (port_type == CTL_PORT_SAS) {
10340 /* SAS (no version claimed) */
10341 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10342 } else if (port_type == CTL_PORT_UMASS) {
10343 /* USB Mass Storage Class Bulk-Only Transport, Revision 1.0 */
10344 scsi_ulto2b(0x1730, inq_ptr->version3);
10345 }
10346
10347 if (lun == NULL) {
10348 /* SBC-4 (no version claimed) */
10349 scsi_ulto2b(0x0600, inq_ptr->version4);
10350 } else {
10351 switch (lun->be_lun->lun_type) {
10352 case T_DIRECT:
10353 /* SBC-4 (no version claimed) */
10354 scsi_ulto2b(0x0600, inq_ptr->version4);
10355 break;
10356 case T_PROCESSOR:
10357 break;
10358 case T_CDROM:
10359 /* MMC-6 (no version claimed) */
10360 scsi_ulto2b(0x04E0, inq_ptr->version4);
10361 break;
10362 default:
10363 break;
10364 }
10365 }
10366
10367 ctl_set_success(ctsio);
10368 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10369 ctsio->be_move_done = ctl_config_move_done;
10370 ctl_datamove((union ctl_io *)ctsio);
10371 return (CTL_RETVAL_COMPLETE);
10372 }
10373
10374 int
ctl_inquiry(struct ctl_scsiio * ctsio)10375 ctl_inquiry(struct ctl_scsiio *ctsio)
10376 {
10377 struct scsi_inquiry *cdb;
10378 int retval;
10379
10380 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10381
10382 cdb = (struct scsi_inquiry *)ctsio->cdb;
10383 if (cdb->byte2 & SI_EVPD)
10384 retval = ctl_inquiry_evpd(ctsio);
10385 else if (cdb->page_code == 0)
10386 retval = ctl_inquiry_std(ctsio);
10387 else {
10388 ctl_set_invalid_field(ctsio,
10389 /*sks_valid*/ 1,
10390 /*command*/ 1,
10391 /*field*/ 2,
10392 /*bit_valid*/ 0,
10393 /*bit*/ 0);
10394 ctl_done((union ctl_io *)ctsio);
10395 return (CTL_RETVAL_COMPLETE);
10396 }
10397
10398 return (retval);
10399 }
10400
10401 int
ctl_get_config(struct ctl_scsiio * ctsio)10402 ctl_get_config(struct ctl_scsiio *ctsio)
10403 {
10404 struct ctl_lun *lun = CTL_LUN(ctsio);
10405 struct scsi_get_config_header *hdr;
10406 struct scsi_get_config_feature *feature;
10407 struct scsi_get_config *cdb;
10408 uint32_t alloc_len, data_len;
10409 int rt, starting;
10410
10411 cdb = (struct scsi_get_config *)ctsio->cdb;
10412 rt = (cdb->rt & SGC_RT_MASK);
10413 starting = scsi_2btoul(cdb->starting_feature);
10414 alloc_len = scsi_2btoul(cdb->length);
10415
10416 data_len = sizeof(struct scsi_get_config_header) +
10417 sizeof(struct scsi_get_config_feature) + 8 +
10418 sizeof(struct scsi_get_config_feature) + 8 +
10419 sizeof(struct scsi_get_config_feature) + 4 +
10420 sizeof(struct scsi_get_config_feature) + 4 +
10421 sizeof(struct scsi_get_config_feature) + 8 +
10422 sizeof(struct scsi_get_config_feature) +
10423 sizeof(struct scsi_get_config_feature) + 4 +
10424 sizeof(struct scsi_get_config_feature) + 4 +
10425 sizeof(struct scsi_get_config_feature) + 4 +
10426 sizeof(struct scsi_get_config_feature) + 4 +
10427 sizeof(struct scsi_get_config_feature) + 4 +
10428 sizeof(struct scsi_get_config_feature) + 4;
10429 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10430 ctsio->kern_sg_entries = 0;
10431 ctsio->kern_rel_offset = 0;
10432
10433 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr;
10434 if (lun->flags & CTL_LUN_NO_MEDIA)
10435 scsi_ulto2b(0x0000, hdr->current_profile);
10436 else
10437 scsi_ulto2b(0x0010, hdr->current_profile);
10438 feature = (struct scsi_get_config_feature *)(hdr + 1);
10439
10440 if (starting > 0x003b)
10441 goto done;
10442 if (starting > 0x003a)
10443 goto f3b;
10444 if (starting > 0x002b)
10445 goto f3a;
10446 if (starting > 0x002a)
10447 goto f2b;
10448 if (starting > 0x001f)
10449 goto f2a;
10450 if (starting > 0x001e)
10451 goto f1f;
10452 if (starting > 0x001d)
10453 goto f1e;
10454 if (starting > 0x0010)
10455 goto f1d;
10456 if (starting > 0x0003)
10457 goto f10;
10458 if (starting > 0x0002)
10459 goto f3;
10460 if (starting > 0x0001)
10461 goto f2;
10462 if (starting > 0x0000)
10463 goto f1;
10464
10465 /* Profile List */
10466 scsi_ulto2b(0x0000, feature->feature_code);
10467 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT;
10468 feature->add_length = 8;
10469 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */
10470 feature->feature_data[2] = 0x00;
10471 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */
10472 feature->feature_data[6] = 0x01;
10473 feature = (struct scsi_get_config_feature *)
10474 &feature->feature_data[feature->add_length];
10475
10476 f1: /* Core */
10477 scsi_ulto2b(0x0001, feature->feature_code);
10478 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10479 feature->add_length = 8;
10480 scsi_ulto4b(0x00000000, &feature->feature_data[0]);
10481 feature->feature_data[4] = 0x03;
10482 feature = (struct scsi_get_config_feature *)
10483 &feature->feature_data[feature->add_length];
10484
10485 f2: /* Morphing */
10486 scsi_ulto2b(0x0002, feature->feature_code);
10487 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10488 feature->add_length = 4;
10489 feature->feature_data[0] = 0x02;
10490 feature = (struct scsi_get_config_feature *)
10491 &feature->feature_data[feature->add_length];
10492
10493 f3: /* Removable Medium */
10494 scsi_ulto2b(0x0003, feature->feature_code);
10495 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10496 feature->add_length = 4;
10497 feature->feature_data[0] = 0x39;
10498 feature = (struct scsi_get_config_feature *)
10499 &feature->feature_data[feature->add_length];
10500
10501 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA))
10502 goto done;
10503
10504 f10: /* Random Read */
10505 scsi_ulto2b(0x0010, feature->feature_code);
10506 feature->flags = 0x00;
10507 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10508 feature->flags |= SGC_F_CURRENT;
10509 feature->add_length = 8;
10510 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]);
10511 scsi_ulto2b(1, &feature->feature_data[4]);
10512 feature->feature_data[6] = 0x00;
10513 feature = (struct scsi_get_config_feature *)
10514 &feature->feature_data[feature->add_length];
10515
10516 f1d: /* Multi-Read */
10517 scsi_ulto2b(0x001D, feature->feature_code);
10518 feature->flags = 0x00;
10519 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10520 feature->flags |= SGC_F_CURRENT;
10521 feature->add_length = 0;
10522 feature = (struct scsi_get_config_feature *)
10523 &feature->feature_data[feature->add_length];
10524
10525 f1e: /* CD Read */
10526 scsi_ulto2b(0x001E, feature->feature_code);
10527 feature->flags = 0x00;
10528 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10529 feature->flags |= SGC_F_CURRENT;
10530 feature->add_length = 4;
10531 feature->feature_data[0] = 0x00;
10532 feature = (struct scsi_get_config_feature *)
10533 &feature->feature_data[feature->add_length];
10534
10535 f1f: /* DVD Read */
10536 scsi_ulto2b(0x001F, feature->feature_code);
10537 feature->flags = 0x08;
10538 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10539 feature->flags |= SGC_F_CURRENT;
10540 feature->add_length = 4;
10541 feature->feature_data[0] = 0x01;
10542 feature->feature_data[2] = 0x03;
10543 feature = (struct scsi_get_config_feature *)
10544 &feature->feature_data[feature->add_length];
10545
10546 f2a: /* DVD+RW */
10547 scsi_ulto2b(0x002A, feature->feature_code);
10548 feature->flags = 0x04;
10549 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10550 feature->flags |= SGC_F_CURRENT;
10551 feature->add_length = 4;
10552 feature->feature_data[0] = 0x00;
10553 feature->feature_data[1] = 0x00;
10554 feature = (struct scsi_get_config_feature *)
10555 &feature->feature_data[feature->add_length];
10556
10557 f2b: /* DVD+R */
10558 scsi_ulto2b(0x002B, feature->feature_code);
10559 feature->flags = 0x00;
10560 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10561 feature->flags |= SGC_F_CURRENT;
10562 feature->add_length = 4;
10563 feature->feature_data[0] = 0x00;
10564 feature = (struct scsi_get_config_feature *)
10565 &feature->feature_data[feature->add_length];
10566
10567 f3a: /* DVD+RW Dual Layer */
10568 scsi_ulto2b(0x003A, feature->feature_code);
10569 feature->flags = 0x00;
10570 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10571 feature->flags |= SGC_F_CURRENT;
10572 feature->add_length = 4;
10573 feature->feature_data[0] = 0x00;
10574 feature->feature_data[1] = 0x00;
10575 feature = (struct scsi_get_config_feature *)
10576 &feature->feature_data[feature->add_length];
10577
10578 f3b: /* DVD+R Dual Layer */
10579 scsi_ulto2b(0x003B, feature->feature_code);
10580 feature->flags = 0x00;
10581 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10582 feature->flags |= SGC_F_CURRENT;
10583 feature->add_length = 4;
10584 feature->feature_data[0] = 0x00;
10585 feature = (struct scsi_get_config_feature *)
10586 &feature->feature_data[feature->add_length];
10587
10588 done:
10589 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10590 if (rt == SGC_RT_SPECIFIC && data_len > 4) {
10591 feature = (struct scsi_get_config_feature *)(hdr + 1);
10592 if (scsi_2btoul(feature->feature_code) == starting)
10593 feature = (struct scsi_get_config_feature *)
10594 &feature->feature_data[feature->add_length];
10595 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10596 }
10597 scsi_ulto4b(data_len - 4, hdr->data_length);
10598 ctsio->kern_data_len = min(data_len, alloc_len);
10599 ctsio->kern_total_len = ctsio->kern_data_len;
10600
10601 ctl_set_success(ctsio);
10602 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10603 ctsio->be_move_done = ctl_config_move_done;
10604 ctl_datamove((union ctl_io *)ctsio);
10605 return (CTL_RETVAL_COMPLETE);
10606 }
10607
10608 int
ctl_get_event_status(struct ctl_scsiio * ctsio)10609 ctl_get_event_status(struct ctl_scsiio *ctsio)
10610 {
10611 struct scsi_get_event_status_header *hdr;
10612 struct scsi_get_event_status *cdb;
10613 uint32_t alloc_len, data_len;
10614
10615 cdb = (struct scsi_get_event_status *)ctsio->cdb;
10616 if ((cdb->byte2 & SGESN_POLLED) == 0) {
10617 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
10618 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
10619 ctl_done((union ctl_io *)ctsio);
10620 return (CTL_RETVAL_COMPLETE);
10621 }
10622 alloc_len = scsi_2btoul(cdb->length);
10623
10624 data_len = sizeof(struct scsi_get_event_status_header);
10625 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10626 ctsio->kern_sg_entries = 0;
10627 ctsio->kern_rel_offset = 0;
10628 ctsio->kern_data_len = min(data_len, alloc_len);
10629 ctsio->kern_total_len = ctsio->kern_data_len;
10630
10631 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr;
10632 scsi_ulto2b(0, hdr->descr_length);
10633 hdr->nea_class = SGESN_NEA;
10634 hdr->supported_class = 0;
10635
10636 ctl_set_success(ctsio);
10637 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10638 ctsio->be_move_done = ctl_config_move_done;
10639 ctl_datamove((union ctl_io *)ctsio);
10640 return (CTL_RETVAL_COMPLETE);
10641 }
10642
10643 int
ctl_mechanism_status(struct ctl_scsiio * ctsio)10644 ctl_mechanism_status(struct ctl_scsiio *ctsio)
10645 {
10646 struct scsi_mechanism_status_header *hdr;
10647 struct scsi_mechanism_status *cdb;
10648 uint32_t alloc_len, data_len;
10649
10650 cdb = (struct scsi_mechanism_status *)ctsio->cdb;
10651 alloc_len = scsi_2btoul(cdb->length);
10652
10653 data_len = sizeof(struct scsi_mechanism_status_header);
10654 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10655 ctsio->kern_sg_entries = 0;
10656 ctsio->kern_rel_offset = 0;
10657 ctsio->kern_data_len = min(data_len, alloc_len);
10658 ctsio->kern_total_len = ctsio->kern_data_len;
10659
10660 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr;
10661 hdr->state1 = 0x00;
10662 hdr->state2 = 0xe0;
10663 scsi_ulto3b(0, hdr->lba);
10664 hdr->slots_num = 0;
10665 scsi_ulto2b(0, hdr->slots_length);
10666
10667 ctl_set_success(ctsio);
10668 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10669 ctsio->be_move_done = ctl_config_move_done;
10670 ctl_datamove((union ctl_io *)ctsio);
10671 return (CTL_RETVAL_COMPLETE);
10672 }
10673
10674 static void
ctl_ultomsf(uint32_t lba,uint8_t * buf)10675 ctl_ultomsf(uint32_t lba, uint8_t *buf)
10676 {
10677
10678 lba += 150;
10679 buf[0] = 0;
10680 buf[1] = bin2bcd((lba / 75) / 60);
10681 buf[2] = bin2bcd((lba / 75) % 60);
10682 buf[3] = bin2bcd(lba % 75);
10683 }
10684
10685 int
ctl_read_toc(struct ctl_scsiio * ctsio)10686 ctl_read_toc(struct ctl_scsiio *ctsio)
10687 {
10688 struct ctl_lun *lun = CTL_LUN(ctsio);
10689 struct scsi_read_toc_hdr *hdr;
10690 struct scsi_read_toc_type01_descr *descr;
10691 struct scsi_read_toc *cdb;
10692 uint32_t alloc_len, data_len;
10693 int format, msf;
10694
10695 cdb = (struct scsi_read_toc *)ctsio->cdb;
10696 msf = (cdb->byte2 & CD_MSF) != 0;
10697 format = cdb->format;
10698 alloc_len = scsi_2btoul(cdb->data_len);
10699
10700 data_len = sizeof(struct scsi_read_toc_hdr);
10701 if (format == 0)
10702 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr);
10703 else
10704 data_len += sizeof(struct scsi_read_toc_type01_descr);
10705 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10706 ctsio->kern_sg_entries = 0;
10707 ctsio->kern_rel_offset = 0;
10708 ctsio->kern_data_len = min(data_len, alloc_len);
10709 ctsio->kern_total_len = ctsio->kern_data_len;
10710
10711 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr;
10712 if (format == 0) {
10713 scsi_ulto2b(0x12, hdr->data_length);
10714 hdr->first = 1;
10715 hdr->last = 1;
10716 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10717 descr->addr_ctl = 0x14;
10718 descr->track_number = 1;
10719 if (msf)
10720 ctl_ultomsf(0, descr->track_start);
10721 else
10722 scsi_ulto4b(0, descr->track_start);
10723 descr++;
10724 descr->addr_ctl = 0x14;
10725 descr->track_number = 0xaa;
10726 if (msf)
10727 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start);
10728 else
10729 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start);
10730 } else {
10731 scsi_ulto2b(0x0a, hdr->data_length);
10732 hdr->first = 1;
10733 hdr->last = 1;
10734 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10735 descr->addr_ctl = 0x14;
10736 descr->track_number = 1;
10737 if (msf)
10738 ctl_ultomsf(0, descr->track_start);
10739 else
10740 scsi_ulto4b(0, descr->track_start);
10741 }
10742
10743 ctl_set_success(ctsio);
10744 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10745 ctsio->be_move_done = ctl_config_move_done;
10746 ctl_datamove((union ctl_io *)ctsio);
10747 return (CTL_RETVAL_COMPLETE);
10748 }
10749
10750 /*
10751 * For known CDB types, parse the LBA and length.
10752 */
10753 static int
ctl_get_lba_len(union ctl_io * io,uint64_t * lba,uint64_t * len)10754 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10755 {
10756
10757 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
10758 ("%s: unexpected I/O type %x", __func__, io->io_hdr.io_type));
10759
10760 switch (io->scsiio.cdb[0]) {
10761 case COMPARE_AND_WRITE: {
10762 struct scsi_compare_and_write *cdb;
10763
10764 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10765
10766 *lba = scsi_8btou64(cdb->addr);
10767 *len = cdb->length;
10768 break;
10769 }
10770 case READ_6:
10771 case WRITE_6: {
10772 struct scsi_rw_6 *cdb;
10773
10774 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10775
10776 *lba = scsi_3btoul(cdb->addr);
10777 /* only 5 bits are valid in the most significant address byte */
10778 *lba &= 0x1fffff;
10779 *len = cdb->length;
10780 break;
10781 }
10782 case READ_10:
10783 case WRITE_10: {
10784 struct scsi_rw_10 *cdb;
10785
10786 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10787
10788 *lba = scsi_4btoul(cdb->addr);
10789 *len = scsi_2btoul(cdb->length);
10790 break;
10791 }
10792 case WRITE_VERIFY_10: {
10793 struct scsi_write_verify_10 *cdb;
10794
10795 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10796
10797 *lba = scsi_4btoul(cdb->addr);
10798 *len = scsi_2btoul(cdb->length);
10799 break;
10800 }
10801 case READ_12:
10802 case WRITE_12: {
10803 struct scsi_rw_12 *cdb;
10804
10805 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10806
10807 *lba = scsi_4btoul(cdb->addr);
10808 *len = scsi_4btoul(cdb->length);
10809 break;
10810 }
10811 case WRITE_VERIFY_12: {
10812 struct scsi_write_verify_12 *cdb;
10813
10814 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10815
10816 *lba = scsi_4btoul(cdb->addr);
10817 *len = scsi_4btoul(cdb->length);
10818 break;
10819 }
10820 case READ_16:
10821 case WRITE_16: {
10822 struct scsi_rw_16 *cdb;
10823
10824 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10825
10826 *lba = scsi_8btou64(cdb->addr);
10827 *len = scsi_4btoul(cdb->length);
10828 break;
10829 }
10830 case WRITE_ATOMIC_16: {
10831 struct scsi_write_atomic_16 *cdb;
10832
10833 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb;
10834
10835 *lba = scsi_8btou64(cdb->addr);
10836 *len = scsi_2btoul(cdb->length);
10837 break;
10838 }
10839 case WRITE_VERIFY_16: {
10840 struct scsi_write_verify_16 *cdb;
10841
10842 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10843
10844 *lba = scsi_8btou64(cdb->addr);
10845 *len = scsi_4btoul(cdb->length);
10846 break;
10847 }
10848 case WRITE_SAME_10: {
10849 struct scsi_write_same_10 *cdb;
10850
10851 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10852
10853 *lba = scsi_4btoul(cdb->addr);
10854 *len = scsi_2btoul(cdb->length);
10855 break;
10856 }
10857 case WRITE_SAME_16: {
10858 struct scsi_write_same_16 *cdb;
10859
10860 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10861
10862 *lba = scsi_8btou64(cdb->addr);
10863 *len = scsi_4btoul(cdb->length);
10864 break;
10865 }
10866 case VERIFY_10: {
10867 struct scsi_verify_10 *cdb;
10868
10869 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10870
10871 *lba = scsi_4btoul(cdb->addr);
10872 *len = scsi_2btoul(cdb->length);
10873 break;
10874 }
10875 case VERIFY_12: {
10876 struct scsi_verify_12 *cdb;
10877
10878 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10879
10880 *lba = scsi_4btoul(cdb->addr);
10881 *len = scsi_4btoul(cdb->length);
10882 break;
10883 }
10884 case VERIFY_16: {
10885 struct scsi_verify_16 *cdb;
10886
10887 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10888
10889 *lba = scsi_8btou64(cdb->addr);
10890 *len = scsi_4btoul(cdb->length);
10891 break;
10892 }
10893 case UNMAP: {
10894 *lba = 0;
10895 *len = UINT64_MAX;
10896 break;
10897 }
10898 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10899 struct scsi_get_lba_status *cdb;
10900
10901 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10902 *lba = scsi_8btou64(cdb->addr);
10903 *len = UINT32_MAX;
10904 break;
10905 }
10906 default:
10907 *lba = 0;
10908 *len = UINT64_MAX;
10909 return (1);
10910 }
10911
10912 return (0);
10913 }
10914
10915 static ctl_action
ctl_extent_check_lba(uint64_t lba1,uint64_t len1,uint64_t lba2,uint64_t len2,bool seq)10916 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10917 bool seq)
10918 {
10919 uint64_t endlba1, endlba2;
10920
10921 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10922 endlba2 = lba2 + len2 - 1;
10923
10924 if ((endlba1 < lba2) || (endlba2 < lba1))
10925 return (CTL_ACTION_PASS);
10926 else
10927 return (CTL_ACTION_BLOCK);
10928 }
10929
10930 static int
ctl_extent_check_unmap(union ctl_io * io,uint64_t lba2,uint64_t len2)10931 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10932 {
10933 struct ctl_ptr_len_flags *ptrlen;
10934 struct scsi_unmap_desc *buf, *end, *range;
10935 uint64_t lba;
10936 uint32_t len;
10937
10938 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
10939 ("%s: unexpected I/O type %x", __func__, io->io_hdr.io_type));
10940
10941 /* If not UNMAP -- go other way. */
10942 if (io->scsiio.cdb[0] != UNMAP)
10943 return (CTL_ACTION_SKIP);
10944
10945 /* If UNMAP without data -- block and wait for data. */
10946 ptrlen = (struct ctl_ptr_len_flags *)
10947 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10948 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10949 ptrlen->ptr == NULL)
10950 return (CTL_ACTION_BLOCK);
10951
10952 /* UNMAP with data -- check for collision. */
10953 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10954 end = buf + ptrlen->len / sizeof(*buf);
10955 for (range = buf; range < end; range++) {
10956 lba = scsi_8btou64(range->lba);
10957 len = scsi_4btoul(range->length);
10958 if ((lba < lba2 + len2) && (lba + len > lba2))
10959 return (CTL_ACTION_BLOCK);
10960 }
10961 return (CTL_ACTION_PASS);
10962 }
10963
10964 static ctl_action
ctl_extent_check(union ctl_io * io1,union ctl_io * io2,bool seq)10965 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10966 {
10967 uint64_t lba1, lba2;
10968 uint64_t len1, len2;
10969 int retval;
10970
10971 retval = ctl_get_lba_len(io2, &lba2, &len2);
10972 KASSERT(retval == 0, ("ctl_get_lba_len() error"));
10973
10974 retval = ctl_extent_check_unmap(io1, lba2, len2);
10975 if (retval != CTL_ACTION_SKIP)
10976 return (retval);
10977
10978 retval = ctl_get_lba_len(io1, &lba1, &len1);
10979 KASSERT(retval == 0, ("ctl_get_lba_len() error"));
10980
10981 if (seq && (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE))
10982 seq = FALSE;
10983 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10984 }
10985
10986 static ctl_action
ctl_seq_check(union ctl_io * io1,union ctl_io * io2)10987 ctl_seq_check(union ctl_io *io1, union ctl_io *io2)
10988 {
10989 uint64_t lba1, lba2;
10990 uint64_t len1, len2;
10991 int retval __diagused;
10992
10993 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10994 return (CTL_ACTION_PASS);
10995 retval = ctl_get_lba_len(io1, &lba1, &len1);
10996 KASSERT(retval == 0, ("ctl_get_lba_len() error"));
10997 retval = ctl_get_lba_len(io2, &lba2, &len2);
10998 KASSERT(retval == 0, ("ctl_get_lba_len() error"));
10999
11000 if (lba1 + len1 == lba2)
11001 return (CTL_ACTION_BLOCK);
11002 return (CTL_ACTION_PASS);
11003 }
11004
11005 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)11006 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
11007 const uint8_t *serialize_row, union ctl_io *ooa_io)
11008 {
11009
11010 /*
11011 * The initiator attempted multiple untagged commands at the same
11012 * time. Can't do that.
11013 */
11014 if (__predict_false(pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11015 && __predict_false(ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11016 && ((pending_io->io_hdr.nexus.targ_port ==
11017 ooa_io->io_hdr.nexus.targ_port)
11018 && (pending_io->io_hdr.nexus.initid ==
11019 ooa_io->io_hdr.nexus.initid))
11020 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
11021 CTL_FLAG_STATUS_SENT)) == 0))
11022 return (CTL_ACTION_OVERLAP);
11023
11024 /*
11025 * The initiator attempted to send multiple tagged commands with
11026 * the same ID. (It's fine if different initiators have the same
11027 * tag ID.)
11028 *
11029 * Even if all of those conditions are true, we don't kill the I/O
11030 * if the command ahead of us has been aborted. We won't end up
11031 * sending it to the FETD, and it's perfectly legal to resend a
11032 * command with the same tag number as long as the previous
11033 * instance of this tag number has been aborted somehow.
11034 */
11035 if (__predict_true(pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11036 && __predict_true(ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11037 && __predict_false(pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
11038 && ((pending_io->io_hdr.nexus.targ_port ==
11039 ooa_io->io_hdr.nexus.targ_port)
11040 && (pending_io->io_hdr.nexus.initid ==
11041 ooa_io->io_hdr.nexus.initid))
11042 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
11043 CTL_FLAG_STATUS_SENT)) == 0))
11044 return (CTL_ACTION_OVERLAP_TAG);
11045
11046 /*
11047 * If we get a head of queue tag, SAM-3 says that we should
11048 * immediately execute it.
11049 *
11050 * What happens if this command would normally block for some other
11051 * reason? e.g. a request sense with a head of queue tag
11052 * immediately after a write. Normally that would block, but this
11053 * will result in its getting executed immediately...
11054 *
11055 * We currently return "pass" instead of "skip", so we'll end up
11056 * going through the rest of the queue to check for overlapped tags.
11057 *
11058 * XXX KDM check for other types of blockage first??
11059 */
11060 if (__predict_false(pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE))
11061 return (CTL_ACTION_PASS);
11062
11063 /*
11064 * Simple tags get blocked until all head of queue and ordered tags
11065 * ahead of them have completed. I'm lumping untagged commands in
11066 * with simple tags here. XXX KDM is that the right thing to do?
11067 */
11068 if (__predict_false(ooa_io->scsiio.tag_type == CTL_TAG_ORDERED) ||
11069 __predict_false(ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE))
11070 return (CTL_ACTION_BLOCK);
11071
11072 /* Unsupported command in OOA queue. */
11073 if (__predict_false(ooa_io->scsiio.seridx == CTL_SERIDX_INVLD))
11074 return (CTL_ACTION_PASS);
11075
11076 switch (serialize_row[ooa_io->scsiio.seridx]) {
11077 case CTL_SER_SEQ:
11078 if (lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
11079 return (ctl_seq_check(ooa_io, pending_io));
11080 /* FALLTHROUGH */
11081 case CTL_SER_PASS:
11082 return (CTL_ACTION_PASS);
11083 case CTL_SER_EXTENTOPT:
11084 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) ==
11085 SCP_QUEUE_ALG_UNRESTRICTED)
11086 return (CTL_ACTION_PASS);
11087 /* FALLTHROUGH */
11088 case CTL_SER_EXTENT:
11089 return (ctl_extent_check(ooa_io, pending_io,
11090 (lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
11091 case CTL_SER_BLOCKOPT:
11092 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) ==
11093 SCP_QUEUE_ALG_UNRESTRICTED)
11094 return (CTL_ACTION_PASS);
11095 /* FALLTHROUGH */
11096 case CTL_SER_BLOCK:
11097 return (CTL_ACTION_BLOCK);
11098 default:
11099 __assert_unreachable();
11100 }
11101 }
11102
11103 /*
11104 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
11105 * Assumptions:
11106 * - pending_io is generally either incoming, or on the blocked queue
11107 * - starting I/O is the I/O we want to start the check with.
11108 */
11109 static ctl_action
ctl_check_ooa(struct ctl_lun * lun,union ctl_io * pending_io,union ctl_io ** starting_io)11110 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
11111 union ctl_io **starting_io)
11112 {
11113 union ctl_io *ooa_io = *starting_io;
11114 const uint8_t *serialize_row;
11115 ctl_action action;
11116
11117 mtx_assert(&lun->lun_lock, MA_OWNED);
11118
11119 /*
11120 * Aborted commands are not going to be executed and may even
11121 * not report completion, so we don't care about their order.
11122 * Let them complete ASAP to clean the OOA queue.
11123 */
11124 if (__predict_false(pending_io->io_hdr.flags & CTL_FLAG_ABORT))
11125 return (CTL_ACTION_SKIP);
11126
11127 /*
11128 * Ordered tags have to block until all items ahead of them have
11129 * completed. If we get called with an ordered tag, we always
11130 * block, if something else is ahead of us in the queue.
11131 */
11132 if ((pending_io->scsiio.tag_type == CTL_TAG_ORDERED) &&
11133 (ooa_io != NULL))
11134 return (CTL_ACTION_BLOCK);
11135
11136 serialize_row = ctl_serialize_table[pending_io->scsiio.seridx];
11137
11138 /*
11139 * Run back along the OOA queue, starting with the current
11140 * blocked I/O and going through every I/O before it on the
11141 * queue. If starting_io is NULL, we'll just end up returning
11142 * CTL_ACTION_PASS.
11143 */
11144 for (; ooa_io != NULL;
11145 ooa_io = (union ctl_io *)LIST_NEXT(&ooa_io->io_hdr, ooa_links)) {
11146 action = ctl_check_for_blockage(lun, pending_io, serialize_row,
11147 ooa_io);
11148 if (action != CTL_ACTION_PASS) {
11149 *starting_io = ooa_io;
11150 return (action);
11151 }
11152 }
11153
11154 *starting_io = NULL;
11155 return (CTL_ACTION_PASS);
11156 }
11157
11158 /*
11159 * Try to unblock the specified I/O.
11160 *
11161 * skip parameter allows explicitly skip present blocker of the I/O,
11162 * starting from the previous one on OOA queue. It can be used when
11163 * we know for sure that the blocker I/O does no longer count.
11164 */
11165 static void
ctl_try_unblock_io(struct ctl_lun * lun,union ctl_io * io,bool skip)11166 ctl_try_unblock_io(struct ctl_lun *lun, union ctl_io *io, bool skip)
11167 {
11168 struct ctl_softc *softc = lun->ctl_softc;
11169 union ctl_io *bio, *obio;
11170 const struct ctl_cmd_entry *entry;
11171 union ctl_ha_msg msg_info;
11172 ctl_action action;
11173
11174 mtx_assert(&lun->lun_lock, MA_OWNED);
11175
11176 if (io->io_hdr.blocker == NULL)
11177 return;
11178
11179 obio = bio = io->io_hdr.blocker;
11180 if (skip)
11181 bio = (union ctl_io *)LIST_NEXT(&bio->io_hdr, ooa_links);
11182 action = ctl_check_ooa(lun, io, &bio);
11183 if (action == CTL_ACTION_BLOCK) {
11184 /* Still blocked, but may be by different I/O now. */
11185 if (bio != obio) {
11186 TAILQ_REMOVE(&obio->io_hdr.blocked_queue,
11187 &io->io_hdr, blocked_links);
11188 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue,
11189 &io->io_hdr, blocked_links);
11190 io->io_hdr.blocker = bio;
11191 }
11192 return;
11193 }
11194
11195 /* No longer blocked, one way or another. */
11196 TAILQ_REMOVE(&obio->io_hdr.blocked_queue, &io->io_hdr, blocked_links);
11197 io->io_hdr.blocker = NULL;
11198
11199 switch (action) {
11200 case CTL_ACTION_PASS:
11201 case CTL_ACTION_SKIP:
11202
11203 /* Serializing commands from the other SC retire there. */
11204 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) &&
11205 (softc->ha_mode != CTL_HA_MODE_XFER)) {
11206 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11207 msg_info.hdr.original_sc = io->io_hdr.remote_io;
11208 msg_info.hdr.serializing_sc = io;
11209 msg_info.hdr.msg_type = CTL_MSG_R2R;
11210 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11211 sizeof(msg_info.hdr), M_NOWAIT);
11212 break;
11213 }
11214
11215 /*
11216 * Check this I/O for LUN state changes that may have happened
11217 * while this command was blocked. The LUN state may have been
11218 * changed by a command ahead of us in the queue.
11219 */
11220 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
11221 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) {
11222 ctl_done(io);
11223 break;
11224 }
11225
11226 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11227 ctl_enqueue_rtr(io);
11228 break;
11229 default:
11230 __assert_unreachable();
11231 case CTL_ACTION_OVERLAP:
11232 ctl_set_overlapped_cmd(&io->scsiio);
11233 goto error;
11234 case CTL_ACTION_OVERLAP_TAG:
11235 ctl_set_overlapped_tag(&io->scsiio,
11236 io->scsiio.tag_num & 0xff);
11237 error:
11238 /* Serializing commands from the other SC are done here. */
11239 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) &&
11240 (softc->ha_mode != CTL_HA_MODE_XFER)) {
11241 ctl_try_unblock_others(lun, io, TRUE);
11242 LIST_REMOVE(&io->io_hdr, ooa_links);
11243
11244 ctl_copy_sense_data_back(io, &msg_info);
11245 msg_info.hdr.original_sc = io->io_hdr.remote_io;
11246 msg_info.hdr.serializing_sc = NULL;
11247 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
11248 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11249 sizeof(msg_info.scsi), M_WAITOK);
11250 ctl_free_io(io);
11251 break;
11252 }
11253
11254 ctl_done(io);
11255 break;
11256 }
11257 }
11258
11259 /*
11260 * Try to unblock I/Os blocked by the specified I/O.
11261 *
11262 * skip parameter allows explicitly skip the specified I/O as blocker,
11263 * starting from the previous one on the OOA queue. It can be used when
11264 * we know for sure that the specified I/O does no longer count (done).
11265 * It has to be still on OOA queue though so that we know where to start.
11266 */
11267 static void
ctl_try_unblock_others(struct ctl_lun * lun,union ctl_io * bio,bool skip)11268 ctl_try_unblock_others(struct ctl_lun *lun, union ctl_io *bio, bool skip)
11269 {
11270 union ctl_io *io, *next_io;
11271
11272 mtx_assert(&lun->lun_lock, MA_OWNED);
11273
11274 for (io = (union ctl_io *)TAILQ_FIRST(&bio->io_hdr.blocked_queue);
11275 io != NULL; io = next_io) {
11276 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, blocked_links);
11277
11278 KASSERT(io->io_hdr.blocker != NULL,
11279 ("I/O %p on blocked list without blocker", io));
11280 ctl_try_unblock_io(lun, io, skip);
11281 }
11282 KASSERT(!skip || TAILQ_EMPTY(&bio->io_hdr.blocked_queue),
11283 ("blocked_queue is not empty after skipping %p", bio));
11284 }
11285
11286 /*
11287 * This routine (with one exception) checks LUN flags that can be set by
11288 * commands ahead of us in the OOA queue. These flags have to be checked
11289 * when a command initially comes in, and when we pull a command off the
11290 * blocked queue and are preparing to execute it. The reason we have to
11291 * check these flags for commands on the blocked queue is that the LUN
11292 * state may have been changed by a command ahead of us while we're on the
11293 * blocked queue.
11294 *
11295 * Ordering is somewhat important with these checks, so please pay
11296 * careful attention to the placement of any new checks.
11297 */
11298 static int
ctl_scsiio_lun_check(struct ctl_lun * lun,const struct ctl_cmd_entry * entry,struct ctl_scsiio * ctsio)11299 ctl_scsiio_lun_check(struct ctl_lun *lun,
11300 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11301 {
11302 struct ctl_softc *softc = lun->ctl_softc;
11303 int retval;
11304 uint32_t residx;
11305
11306 retval = 0;
11307
11308 mtx_assert(&lun->lun_lock, MA_OWNED);
11309
11310 /*
11311 * If this shelf is a secondary shelf controller, we may have to
11312 * reject some commands disallowed by HA mode and link state.
11313 */
11314 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11315 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
11316 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11317 ctl_set_lun_unavail(ctsio);
11318 retval = 1;
11319 goto bailout;
11320 }
11321 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
11322 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11323 ctl_set_lun_transit(ctsio);
11324 retval = 1;
11325 goto bailout;
11326 }
11327 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
11328 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
11329 ctl_set_lun_standby(ctsio);
11330 retval = 1;
11331 goto bailout;
11332 }
11333
11334 /* The rest of checks are only done on executing side */
11335 if (softc->ha_mode == CTL_HA_MODE_XFER)
11336 goto bailout;
11337 }
11338
11339 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11340 if (lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
11341 ctl_set_hw_write_protected(ctsio);
11342 retval = 1;
11343 goto bailout;
11344 }
11345 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) {
11346 ctl_set_sense(ctsio, /*current_error*/ 1,
11347 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11348 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11349 retval = 1;
11350 goto bailout;
11351 }
11352 }
11353
11354 /*
11355 * Check for a reservation conflict. If this command isn't allowed
11356 * even on reserved LUNs, and if this initiator isn't the one who
11357 * reserved us, reject the command with a reservation conflict.
11358 */
11359 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11360 if ((lun->flags & CTL_LUN_RESERVED)
11361 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11362 if (lun->res_idx != residx) {
11363 ctl_set_reservation_conflict(ctsio);
11364 retval = 1;
11365 goto bailout;
11366 }
11367 }
11368
11369 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11370 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11371 /* No reservation or command is allowed. */;
11372 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11373 (lun->pr_res_type == SPR_TYPE_WR_EX ||
11374 lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
11375 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) {
11376 /* The command is allowed for Write Exclusive resv. */;
11377 } else {
11378 /*
11379 * if we aren't registered or it's a res holder type
11380 * reservation and this isn't the res holder then set a
11381 * conflict.
11382 */
11383 if (ctl_get_prkey(lun, residx) == 0 ||
11384 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) {
11385 ctl_set_reservation_conflict(ctsio);
11386 retval = 1;
11387 goto bailout;
11388 }
11389 }
11390
11391 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) {
11392 if (lun->flags & CTL_LUN_EJECTED)
11393 ctl_set_lun_ejected(ctsio);
11394 else if (lun->flags & CTL_LUN_NO_MEDIA) {
11395 if (lun->flags & CTL_LUN_REMOVABLE)
11396 ctl_set_lun_no_media(ctsio);
11397 else
11398 ctl_set_lun_int_reqd(ctsio);
11399 } else if (lun->flags & CTL_LUN_STOPPED)
11400 ctl_set_lun_stopped(ctsio);
11401 else
11402 goto bailout;
11403 retval = 1;
11404 goto bailout;
11405 }
11406
11407 bailout:
11408 return (retval);
11409 }
11410
11411 static void
ctl_failover_io(union ctl_io * io,int have_lock)11412 ctl_failover_io(union ctl_io *io, int have_lock)
11413 {
11414 ctl_set_busy(&io->scsiio);
11415 ctl_done(io);
11416 }
11417
11418 static void
ctl_failover_lun(union ctl_io * rio)11419 ctl_failover_lun(union ctl_io *rio)
11420 {
11421 struct ctl_softc *softc = CTL_SOFTC(rio);
11422 struct ctl_lun *lun;
11423 struct ctl_io_hdr *io, *next_io;
11424 uint32_t targ_lun;
11425
11426 targ_lun = rio->io_hdr.nexus.targ_mapped_lun;
11427 CTL_DEBUG_PRINT(("FAILOVER for lun %u\n", targ_lun));
11428
11429 /* Find and lock the LUN. */
11430 mtx_lock(&softc->ctl_lock);
11431 if (targ_lun > ctl_max_luns ||
11432 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11433 mtx_unlock(&softc->ctl_lock);
11434 return;
11435 }
11436 mtx_lock(&lun->lun_lock);
11437 mtx_unlock(&softc->ctl_lock);
11438 if (lun->flags & CTL_LUN_DISABLED) {
11439 mtx_unlock(&lun->lun_lock);
11440 return;
11441 }
11442
11443 if (softc->ha_mode == CTL_HA_MODE_XFER) {
11444 LIST_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11445 /* We are master */
11446 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11447 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11448 io->flags |= CTL_FLAG_ABORT |
11449 CTL_FLAG_FAILOVER;
11450 ctl_try_unblock_io(lun,
11451 (union ctl_io *)io, FALSE);
11452 } else { /* This can be only due to DATAMOVE */
11453 io->msg_type = CTL_MSG_DATAMOVE_DONE;
11454 io->flags &= ~CTL_FLAG_DMA_INPROG;
11455 io->flags |= CTL_FLAG_IO_ACTIVE;
11456 io->port_status = 31340;
11457 ctl_enqueue_isc((union ctl_io *)io);
11458 }
11459 } else
11460 /* We are slave */
11461 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11462 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11463 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11464 io->flags |= CTL_FLAG_FAILOVER;
11465 } else {
11466 ctl_set_busy(&((union ctl_io *)io)->
11467 scsiio);
11468 ctl_done((union ctl_io *)io);
11469 }
11470 }
11471 }
11472 } else { /* SERIALIZE modes */
11473 LIST_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11474 /* We are master */
11475 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11476 if (io->blocker != NULL) {
11477 TAILQ_REMOVE(&io->blocker->io_hdr.blocked_queue,
11478 io, blocked_links);
11479 io->blocker = NULL;
11480 }
11481 ctl_try_unblock_others(lun, (union ctl_io *)io,
11482 TRUE);
11483 LIST_REMOVE(io, ooa_links);
11484 ctl_free_io((union ctl_io *)io);
11485 } else
11486 /* We are slave */
11487 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11488 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11489 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
11490 ctl_set_busy(&((union ctl_io *)io)->
11491 scsiio);
11492 ctl_done((union ctl_io *)io);
11493 }
11494 }
11495 }
11496 }
11497 mtx_unlock(&lun->lun_lock);
11498 }
11499
11500 static void
ctl_scsiio_precheck(struct ctl_scsiio * ctsio)11501 ctl_scsiio_precheck(struct ctl_scsiio *ctsio)
11502 {
11503 struct ctl_softc *softc = CTL_SOFTC(ctsio);
11504 struct ctl_lun *lun;
11505 const struct ctl_cmd_entry *entry;
11506 union ctl_io *bio;
11507 uint32_t initidx, targ_lun;
11508
11509 lun = NULL;
11510 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11511 if (targ_lun < ctl_max_luns)
11512 lun = softc->ctl_luns[targ_lun];
11513 if (lun) {
11514 /*
11515 * If the LUN is invalid, pretend that it doesn't exist.
11516 * It will go away as soon as all pending I/O has been
11517 * completed.
11518 */
11519 mtx_lock(&lun->lun_lock);
11520 if (lun->flags & CTL_LUN_DISABLED) {
11521 mtx_unlock(&lun->lun_lock);
11522 lun = NULL;
11523 }
11524 }
11525 CTL_LUN(ctsio) = lun;
11526 if (lun) {
11527 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
11528
11529 /*
11530 * Every I/O goes into the OOA queue for a particular LUN,
11531 * and stays there until completion.
11532 */
11533 #ifdef CTL_TIME_IO
11534 if (LIST_EMPTY(&lun->ooa_queue))
11535 lun->idle_time += getsbinuptime() - lun->last_busy;
11536 #endif
11537 LIST_INSERT_HEAD(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
11538 }
11539
11540 /* Get command entry and return error if it is unsuppotyed. */
11541 entry = ctl_validate_command(ctsio);
11542 if (entry == NULL) {
11543 if (lun)
11544 mtx_unlock(&lun->lun_lock);
11545 return;
11546 }
11547
11548 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11549 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11550
11551 /*
11552 * Check to see whether we can send this command to LUNs that don't
11553 * exist. This should pretty much only be the case for inquiry
11554 * and request sense. Further checks, below, really require having
11555 * a LUN, so we can't really check the command anymore. Just put
11556 * it on the rtr queue.
11557 */
11558 if (lun == NULL) {
11559 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11560 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11561 ctl_enqueue_rtr((union ctl_io *)ctsio);
11562 return;
11563 }
11564
11565 ctl_set_unsupported_lun(ctsio);
11566 ctl_done((union ctl_io *)ctsio);
11567 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11568 return;
11569 } else {
11570 /*
11571 * Make sure we support this particular command on this LUN.
11572 * e.g., we don't support writes to the control LUN.
11573 */
11574 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11575 mtx_unlock(&lun->lun_lock);
11576 ctl_set_invalid_opcode(ctsio);
11577 ctl_done((union ctl_io *)ctsio);
11578 return;
11579 }
11580 }
11581
11582 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11583
11584 /*
11585 * If we've got a request sense, it'll clear the contingent
11586 * allegiance condition. Otherwise, if we have a CA condition for
11587 * this initiator, clear it, because it sent down a command other
11588 * than request sense.
11589 */
11590 if (ctsio->cdb[0] != REQUEST_SENSE) {
11591 struct scsi_sense_data *ps;
11592
11593 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
11594 if (ps != NULL)
11595 ps[initidx % CTL_MAX_INIT_PER_PORT].error_code = 0;
11596 }
11597
11598 /*
11599 * If the command has this flag set, it handles its own unit
11600 * attention reporting, we shouldn't do anything. Otherwise we
11601 * check for any pending unit attentions, and send them back to the
11602 * initiator. We only do this when a command initially comes in,
11603 * not when we pull it off the blocked queue.
11604 *
11605 * According to SAM-3, section 5.3.2, the order that things get
11606 * presented back to the host is basically unit attentions caused
11607 * by some sort of reset event, busy status, reservation conflicts
11608 * or task set full, and finally any other status.
11609 *
11610 * One issue here is that some of the unit attentions we report
11611 * don't fall into the "reset" category (e.g. "reported luns data
11612 * has changed"). So reporting it here, before the reservation
11613 * check, may be technically wrong. I guess the only thing to do
11614 * would be to check for and report the reset events here, and then
11615 * check for the other unit attention types after we check for a
11616 * reservation conflict.
11617 *
11618 * XXX KDM need to fix this
11619 */
11620 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11621 ctl_ua_type ua_type;
11622 u_int sense_len = 0;
11623
11624 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11625 &sense_len, SSD_TYPE_NONE);
11626 if (ua_type != CTL_UA_NONE) {
11627 mtx_unlock(&lun->lun_lock);
11628 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11629 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11630 ctsio->sense_len = sense_len;
11631 ctl_done((union ctl_io *)ctsio);
11632 return;
11633 }
11634 }
11635
11636 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11637 mtx_unlock(&lun->lun_lock);
11638 ctl_done((union ctl_io *)ctsio);
11639 return;
11640 }
11641
11642 /*
11643 * XXX CHD this is where we want to send IO to other side if
11644 * this LUN is secondary on this SC. We will need to make a copy
11645 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11646 * the copy we send as FROM_OTHER.
11647 * We also need to stuff the address of the original IO so we can
11648 * find it easily. Something similar will need be done on the other
11649 * side so when we are done we can find the copy.
11650 */
11651 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11652 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
11653 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
11654 union ctl_ha_msg msg_info;
11655 int isc_retval;
11656
11657 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11658 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11659 mtx_unlock(&lun->lun_lock);
11660
11661 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11662 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11663 msg_info.hdr.serializing_sc = NULL;
11664 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11665 msg_info.scsi.tag_num = ctsio->tag_num;
11666 msg_info.scsi.tag_type = ctsio->tag_type;
11667 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11668 msg_info.scsi.cdb_len = ctsio->cdb_len;
11669 msg_info.scsi.priority = ctsio->priority;
11670
11671 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11672 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11673 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11674 ctsio->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11675 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
11676 ctl_set_busy(ctsio);
11677 ctl_done((union ctl_io *)ctsio);
11678 return;
11679 }
11680 return;
11681 }
11682
11683 bio = (union ctl_io *)LIST_NEXT(&ctsio->io_hdr, ooa_links);
11684 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, &bio)) {
11685 case CTL_ACTION_PASS:
11686 case CTL_ACTION_SKIP:
11687 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11688 mtx_unlock(&lun->lun_lock);
11689 ctl_enqueue_rtr((union ctl_io *)ctsio);
11690 break;
11691 case CTL_ACTION_BLOCK:
11692 ctsio->io_hdr.blocker = bio;
11693 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctsio->io_hdr,
11694 blocked_links);
11695 mtx_unlock(&lun->lun_lock);
11696 break;
11697 case CTL_ACTION_OVERLAP:
11698 mtx_unlock(&lun->lun_lock);
11699 ctl_set_overlapped_cmd(ctsio);
11700 ctl_done((union ctl_io *)ctsio);
11701 break;
11702 case CTL_ACTION_OVERLAP_TAG:
11703 mtx_unlock(&lun->lun_lock);
11704 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11705 ctl_done((union ctl_io *)ctsio);
11706 break;
11707 default:
11708 __assert_unreachable();
11709 }
11710 }
11711
11712 const struct ctl_cmd_entry *
ctl_get_cmd_entry(struct ctl_scsiio * ctsio,int * sa)11713 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11714 {
11715 const struct ctl_cmd_entry *entry;
11716 int service_action;
11717
11718 entry = &ctl_cmd_table[ctsio->cdb[0]];
11719 if (sa)
11720 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11721 if (entry->flags & CTL_CMD_FLAG_SA5) {
11722 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11723 entry = &((const struct ctl_cmd_entry *)
11724 entry->execute)[service_action];
11725 }
11726 return (entry);
11727 }
11728
11729 const struct ctl_cmd_entry *
ctl_validate_command(struct ctl_scsiio * ctsio)11730 ctl_validate_command(struct ctl_scsiio *ctsio)
11731 {
11732 const struct ctl_cmd_entry *entry;
11733 int i, sa;
11734 uint8_t diff;
11735
11736 entry = ctl_get_cmd_entry(ctsio, &sa);
11737 ctsio->seridx = entry->seridx;
11738 if (entry->execute == NULL) {
11739 if (sa)
11740 ctl_set_invalid_field(ctsio,
11741 /*sks_valid*/ 1,
11742 /*command*/ 1,
11743 /*field*/ 1,
11744 /*bit_valid*/ 1,
11745 /*bit*/ 4);
11746 else
11747 ctl_set_invalid_opcode(ctsio);
11748 ctl_done((union ctl_io *)ctsio);
11749 return (NULL);
11750 }
11751 KASSERT(entry->length > 0,
11752 ("Not defined length for command 0x%02x/0x%02x",
11753 ctsio->cdb[0], ctsio->cdb[1]));
11754 for (i = 1; i < entry->length; i++) {
11755 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11756 if (diff == 0)
11757 continue;
11758 ctl_set_invalid_field(ctsio,
11759 /*sks_valid*/ 1,
11760 /*command*/ 1,
11761 /*field*/ i,
11762 /*bit_valid*/ 1,
11763 /*bit*/ fls(diff) - 1);
11764 ctl_done((union ctl_io *)ctsio);
11765 return (NULL);
11766 }
11767 return (entry);
11768 }
11769
11770 static int
ctl_cmd_applicable(uint8_t lun_type,const struct ctl_cmd_entry * entry)11771 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11772 {
11773
11774 switch (lun_type) {
11775 case T_DIRECT:
11776 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0)
11777 return (0);
11778 break;
11779 case T_PROCESSOR:
11780 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11781 return (0);
11782 break;
11783 case T_CDROM:
11784 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0)
11785 return (0);
11786 break;
11787 default:
11788 return (0);
11789 }
11790 return (1);
11791 }
11792
11793 static int
ctl_scsiio(struct ctl_scsiio * ctsio)11794 ctl_scsiio(struct ctl_scsiio *ctsio)
11795 {
11796 int retval;
11797 const struct ctl_cmd_entry *entry;
11798
11799 retval = CTL_RETVAL_COMPLETE;
11800
11801 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11802
11803 entry = ctl_get_cmd_entry(ctsio, NULL);
11804
11805 /*
11806 * If this I/O has been aborted, just send it straight to
11807 * ctl_done() without executing it.
11808 */
11809 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11810 ctl_done((union ctl_io *)ctsio);
11811 goto bailout;
11812 }
11813
11814 /*
11815 * All the checks should have been handled by ctl_scsiio_precheck().
11816 * We should be clear now to just execute the I/O.
11817 */
11818 retval = entry->execute(ctsio);
11819
11820 bailout:
11821 return (retval);
11822 }
11823
11824 static int
ctl_target_reset(union ctl_io * io)11825 ctl_target_reset(union ctl_io *io)
11826 {
11827 struct ctl_softc *softc = CTL_SOFTC(io);
11828 struct ctl_port *port = CTL_PORT(io);
11829 struct ctl_lun *lun;
11830 uint32_t initidx;
11831 ctl_ua_type ua_type;
11832
11833 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11834 union ctl_ha_msg msg_info;
11835
11836 msg_info.hdr.nexus = io->io_hdr.nexus;
11837 msg_info.task.task_action = io->taskio.task_action;
11838 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11839 msg_info.hdr.original_sc = NULL;
11840 msg_info.hdr.serializing_sc = NULL;
11841 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11842 sizeof(msg_info.task), M_WAITOK);
11843 }
11844
11845 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11846 if (io->taskio.task_action == CTL_TASK_TARGET_RESET)
11847 ua_type = CTL_UA_TARG_RESET;
11848 else
11849 ua_type = CTL_UA_BUS_RESET;
11850 mtx_lock(&softc->ctl_lock);
11851 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11852 if (port != NULL &&
11853 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
11854 continue;
11855 ctl_do_lun_reset(lun, initidx, ua_type);
11856 }
11857 mtx_unlock(&softc->ctl_lock);
11858 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11859 return (0);
11860 }
11861
11862 /*
11863 * The LUN should always be set. The I/O is optional, and is used to
11864 * distinguish between I/Os sent by this initiator, and by other
11865 * initiators. We set unit attention for initiators other than this one.
11866 * SAM-3 is vague on this point. It does say that a unit attention should
11867 * be established for other initiators when a LUN is reset (see section
11868 * 5.7.3), but it doesn't specifically say that the unit attention should
11869 * be established for this particular initiator when a LUN is reset. Here
11870 * is the relevant text, from SAM-3 rev 8:
11871 *
11872 * 5.7.2 When a SCSI initiator port aborts its own tasks
11873 *
11874 * When a SCSI initiator port causes its own task(s) to be aborted, no
11875 * notification that the task(s) have been aborted shall be returned to
11876 * the SCSI initiator port other than the completion response for the
11877 * command or task management function action that caused the task(s) to
11878 * be aborted and notification(s) associated with related effects of the
11879 * action (e.g., a reset unit attention condition).
11880 *
11881 * XXX KDM for now, we're setting unit attention for all initiators.
11882 */
11883 static void
ctl_do_lun_reset(struct ctl_lun * lun,uint32_t initidx,ctl_ua_type ua_type)11884 ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua_type)
11885 {
11886 struct ctl_io_hdr *xioh;
11887 int i;
11888
11889 mtx_lock(&lun->lun_lock);
11890 /* Abort tasks. */
11891 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) {
11892 xioh->flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11893 ctl_try_unblock_io(lun, (union ctl_io *)xioh, FALSE);
11894 }
11895 /* Clear CA. */
11896 for (i = 0; i < ctl_max_ports; i++) {
11897 free(lun->pending_sense[i], M_CTL);
11898 lun->pending_sense[i] = NULL;
11899 }
11900 /* Clear reservation. */
11901 lun->flags &= ~CTL_LUN_RESERVED;
11902 /* Clear prevent media removal. */
11903 if (lun->prevent) {
11904 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11905 ctl_clear_mask(lun->prevent, i);
11906 lun->prevent_count = 0;
11907 }
11908 /* Clear TPC status */
11909 ctl_tpc_lun_clear(lun, -1);
11910 /* Establish UA. */
11911 #if 0
11912 ctl_est_ua_all(lun, initidx, ua_type);
11913 #else
11914 ctl_est_ua_all(lun, -1, ua_type);
11915 #endif
11916 mtx_unlock(&lun->lun_lock);
11917 }
11918
11919 static int
ctl_lun_reset(union ctl_io * io)11920 ctl_lun_reset(union ctl_io *io)
11921 {
11922 struct ctl_softc *softc = CTL_SOFTC(io);
11923 struct ctl_lun *lun;
11924 uint32_t targ_lun, initidx;
11925
11926 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11927 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11928 mtx_lock(&softc->ctl_lock);
11929 if (targ_lun >= ctl_max_luns ||
11930 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11931 mtx_unlock(&softc->ctl_lock);
11932 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11933 return (1);
11934 }
11935 ctl_do_lun_reset(lun, initidx, CTL_UA_LUN_RESET);
11936 mtx_unlock(&softc->ctl_lock);
11937 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11938
11939 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
11940 union ctl_ha_msg msg_info;
11941
11942 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11943 msg_info.hdr.nexus = io->io_hdr.nexus;
11944 msg_info.task.task_action = CTL_TASK_LUN_RESET;
11945 msg_info.hdr.original_sc = NULL;
11946 msg_info.hdr.serializing_sc = NULL;
11947 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11948 sizeof(msg_info.task), M_WAITOK);
11949 }
11950 return (0);
11951 }
11952
11953 static void
ctl_abort_tasks_lun(struct ctl_lun * lun,uint32_t targ_port,uint32_t init_id,int other_sc)11954 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11955 int other_sc)
11956 {
11957 struct ctl_io_hdr *xioh;
11958
11959 mtx_assert(&lun->lun_lock, MA_OWNED);
11960
11961 /*
11962 * Run through the OOA queue and attempt to find the given I/O.
11963 * The target port, initiator ID, tag type and tag number have to
11964 * match the values that we got from the initiator. If we have an
11965 * untagged command to abort, simply abort the first untagged command
11966 * we come to. We only allow one untagged command at a time of course.
11967 */
11968 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) {
11969 union ctl_io *xio = (union ctl_io *)xioh;
11970 if ((targ_port == UINT32_MAX ||
11971 targ_port == xioh->nexus.targ_port) &&
11972 (init_id == UINT32_MAX ||
11973 init_id == xioh->nexus.initid)) {
11974 if (targ_port != xioh->nexus.targ_port ||
11975 init_id != xioh->nexus.initid)
11976 xioh->flags |= CTL_FLAG_ABORT_STATUS;
11977 xioh->flags |= CTL_FLAG_ABORT;
11978 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11979 union ctl_ha_msg msg_info;
11980
11981 msg_info.hdr.nexus = xioh->nexus;
11982 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11983 msg_info.task.tag_num = xio->scsiio.tag_num;
11984 msg_info.task.tag_type = xio->scsiio.tag_type;
11985 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11986 msg_info.hdr.original_sc = NULL;
11987 msg_info.hdr.serializing_sc = NULL;
11988 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11989 sizeof(msg_info.task), M_NOWAIT);
11990 }
11991 ctl_try_unblock_io(lun, xio, FALSE);
11992 }
11993 }
11994 }
11995
11996 static int
ctl_abort_task_set(union ctl_io * io)11997 ctl_abort_task_set(union ctl_io *io)
11998 {
11999 struct ctl_softc *softc = CTL_SOFTC(io);
12000 struct ctl_lun *lun;
12001 uint32_t targ_lun;
12002
12003 /*
12004 * Look up the LUN.
12005 */
12006 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12007 mtx_lock(&softc->ctl_lock);
12008 if (targ_lun >= ctl_max_luns ||
12009 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12010 mtx_unlock(&softc->ctl_lock);
12011 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12012 return (1);
12013 }
12014
12015 mtx_lock(&lun->lun_lock);
12016 mtx_unlock(&softc->ctl_lock);
12017 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
12018 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12019 io->io_hdr.nexus.initid,
12020 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12021 } else { /* CTL_TASK_CLEAR_TASK_SET */
12022 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
12023 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12024 }
12025 mtx_unlock(&lun->lun_lock);
12026 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12027 return (0);
12028 }
12029
12030 static void
ctl_i_t_nexus_loss(struct ctl_softc * softc,uint32_t initidx,ctl_ua_type ua_type)12031 ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx,
12032 ctl_ua_type ua_type)
12033 {
12034 struct ctl_lun *lun;
12035 struct scsi_sense_data *ps;
12036 uint32_t p, i;
12037
12038 p = initidx / CTL_MAX_INIT_PER_PORT;
12039 i = initidx % CTL_MAX_INIT_PER_PORT;
12040 mtx_lock(&softc->ctl_lock);
12041 STAILQ_FOREACH(lun, &softc->lun_list, links) {
12042 mtx_lock(&lun->lun_lock);
12043 /* Abort tasks. */
12044 ctl_abort_tasks_lun(lun, p, i, 1);
12045 /* Clear CA. */
12046 ps = lun->pending_sense[p];
12047 if (ps != NULL)
12048 ps[i].error_code = 0;
12049 /* Clear reservation. */
12050 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
12051 lun->flags &= ~CTL_LUN_RESERVED;
12052 /* Clear prevent media removal. */
12053 if (lun->prevent && ctl_is_set(lun->prevent, initidx)) {
12054 ctl_clear_mask(lun->prevent, initidx);
12055 lun->prevent_count--;
12056 }
12057 /* Clear TPC status */
12058 ctl_tpc_lun_clear(lun, initidx);
12059 /* Establish UA. */
12060 ctl_est_ua(lun, initidx, ua_type);
12061 mtx_unlock(&lun->lun_lock);
12062 }
12063 mtx_unlock(&softc->ctl_lock);
12064 }
12065
12066 static int
ctl_i_t_nexus_reset(union ctl_io * io)12067 ctl_i_t_nexus_reset(union ctl_io *io)
12068 {
12069 struct ctl_softc *softc = CTL_SOFTC(io);
12070 uint32_t initidx;
12071
12072 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12073 union ctl_ha_msg msg_info;
12074
12075 msg_info.hdr.nexus = io->io_hdr.nexus;
12076 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
12077 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12078 msg_info.hdr.original_sc = NULL;
12079 msg_info.hdr.serializing_sc = NULL;
12080 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12081 sizeof(msg_info.task), M_WAITOK);
12082 }
12083
12084 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12085 ctl_i_t_nexus_loss(softc, initidx, CTL_UA_I_T_NEXUS_LOSS);
12086 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12087 return (0);
12088 }
12089
12090 static int
ctl_abort_task(union ctl_io * io)12091 ctl_abort_task(union ctl_io *io)
12092 {
12093 struct ctl_softc *softc = CTL_SOFTC(io);
12094 struct ctl_io_hdr *xioh;
12095 struct ctl_lun *lun;
12096 uint32_t targ_lun;
12097
12098 /*
12099 * Look up the LUN.
12100 */
12101 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12102 mtx_lock(&softc->ctl_lock);
12103 if (targ_lun >= ctl_max_luns ||
12104 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12105 mtx_unlock(&softc->ctl_lock);
12106 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12107 return (1);
12108 }
12109
12110 mtx_lock(&lun->lun_lock);
12111 mtx_unlock(&softc->ctl_lock);
12112 /*
12113 * Run through the OOA queue and attempt to find the given I/O.
12114 * The target port, initiator ID, tag type and tag number have to
12115 * match the values that we got from the initiator. If we have an
12116 * untagged command to abort, simply abort the first untagged command
12117 * we come to. We only allow one untagged command at a time of course.
12118 */
12119 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) {
12120 union ctl_io *xio = (union ctl_io *)xioh;
12121 if ((xioh->nexus.targ_port != io->io_hdr.nexus.targ_port)
12122 || (xioh->nexus.initid != io->io_hdr.nexus.initid)
12123 || (xioh->flags & CTL_FLAG_ABORT))
12124 continue;
12125
12126 /*
12127 * If the abort says that the task is untagged, the
12128 * task in the queue must be untagged. Otherwise,
12129 * we just check to see whether the tag numbers
12130 * match. This is because the QLogic firmware
12131 * doesn't pass back the tag type in an abort
12132 * request.
12133 */
12134 #if 0
12135 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12136 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12137 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
12138 #else
12139 /*
12140 * XXX KDM we've got problems with FC, because it
12141 * doesn't send down a tag type with aborts. So we
12142 * can only really go by the tag number...
12143 * This may cause problems with parallel SCSI.
12144 * Need to figure that out!!
12145 */
12146 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12147 #endif
12148 xioh->flags |= CTL_FLAG_ABORT;
12149 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
12150 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12151 union ctl_ha_msg msg_info;
12152
12153 msg_info.hdr.nexus = io->io_hdr.nexus;
12154 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12155 msg_info.task.tag_num = io->taskio.tag_num;
12156 msg_info.task.tag_type = io->taskio.tag_type;
12157 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12158 msg_info.hdr.original_sc = NULL;
12159 msg_info.hdr.serializing_sc = NULL;
12160 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12161 sizeof(msg_info.task), M_NOWAIT);
12162 }
12163 ctl_try_unblock_io(lun, xio, FALSE);
12164 }
12165 }
12166 mtx_unlock(&lun->lun_lock);
12167 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12168 return (0);
12169 }
12170
12171 static int
12172 ctl_query_task(union ctl_io *io, int task_set)
12173 {
12174 struct ctl_softc *softc = CTL_SOFTC(io);
12175 struct ctl_io_hdr *xioh;
12176 struct ctl_lun *lun;
12177 int found = 0;
12178 uint32_t targ_lun;
12179
12180 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12181 mtx_lock(&softc->ctl_lock);
12182 if (targ_lun >= ctl_max_luns ||
12183 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12184 mtx_unlock(&softc->ctl_lock);
12185 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12186 return (1);
12187 }
12188 mtx_lock(&lun->lun_lock);
12189 mtx_unlock(&softc->ctl_lock);
12190 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) {
12191 union ctl_io *xio = (union ctl_io *)xioh;
12192 if ((xioh->nexus.targ_port != io->io_hdr.nexus.targ_port)
12193 || (xioh->nexus.initid != io->io_hdr.nexus.initid)
12194 || (xioh->flags & CTL_FLAG_ABORT))
12195 continue;
12196
12197 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
12198 found = 1;
12199 break;
12200 }
12201 }
12202 mtx_unlock(&lun->lun_lock);
12203 if (found)
12204 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12205 else
12206 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12207 return (0);
12208 }
12209
12210 static int
12211 ctl_query_async_event(union ctl_io *io)
12212 {
12213 struct ctl_softc *softc = CTL_SOFTC(io);
12214 struct ctl_lun *lun;
12215 ctl_ua_type ua;
12216 uint32_t targ_lun, initidx;
12217
12218 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12219 mtx_lock(&softc->ctl_lock);
12220 if (targ_lun >= ctl_max_luns ||
12221 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12222 mtx_unlock(&softc->ctl_lock);
12223 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12224 return (1);
12225 }
12226 mtx_lock(&lun->lun_lock);
12227 mtx_unlock(&softc->ctl_lock);
12228 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12229 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
12230 mtx_unlock(&lun->lun_lock);
12231 if (ua != CTL_UA_NONE)
12232 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12233 else
12234 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12235 return (0);
12236 }
12237
12238 static void
12239 ctl_run_task(union ctl_io *io)
12240 {
12241 int retval = 1;
12242
12243 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12244 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12245 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
12246 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
12247 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
12248 switch (io->taskio.task_action) {
12249 case CTL_TASK_ABORT_TASK:
12250 retval = ctl_abort_task(io);
12251 break;
12252 case CTL_TASK_ABORT_TASK_SET:
12253 case CTL_TASK_CLEAR_TASK_SET:
12254 retval = ctl_abort_task_set(io);
12255 break;
12256 case CTL_TASK_CLEAR_ACA:
12257 break;
12258 case CTL_TASK_I_T_NEXUS_RESET:
12259 retval = ctl_i_t_nexus_reset(io);
12260 break;
12261 case CTL_TASK_LUN_RESET:
12262 retval = ctl_lun_reset(io);
12263 break;
12264 case CTL_TASK_TARGET_RESET:
12265 case CTL_TASK_BUS_RESET:
12266 retval = ctl_target_reset(io);
12267 break;
12268 case CTL_TASK_PORT_LOGIN:
12269 break;
12270 case CTL_TASK_PORT_LOGOUT:
12271 break;
12272 case CTL_TASK_QUERY_TASK:
12273 retval = ctl_query_task(io, 0);
12274 break;
12275 case CTL_TASK_QUERY_TASK_SET:
12276 retval = ctl_query_task(io, 1);
12277 break;
12278 case CTL_TASK_QUERY_ASYNC_EVENT:
12279 retval = ctl_query_async_event(io);
12280 break;
12281 default:
12282 printf("%s: got unknown task management event %d\n",
12283 __func__, io->taskio.task_action);
12284 break;
12285 }
12286 if (retval == 0)
12287 io->io_hdr.status = CTL_SUCCESS;
12288 else
12289 io->io_hdr.status = CTL_ERROR;
12290 ctl_done(io);
12291 }
12292
12293 /*
12294 * For HA operation. Handle commands that come in from the other
12295 * controller.
12296 */
12297 static void
12298 ctl_handle_isc(union ctl_io *io)
12299 {
12300 struct ctl_softc *softc = CTL_SOFTC(io);
12301 struct ctl_lun *lun;
12302 const struct ctl_cmd_entry *entry;
12303 uint32_t targ_lun;
12304
12305 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12306 switch (io->io_hdr.msg_type) {
12307 case CTL_MSG_SERIALIZE:
12308 ctl_serialize_other_sc_cmd(&io->scsiio);
12309 break;
12310 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */
12311 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12312 if (targ_lun >= ctl_max_luns ||
12313 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12314 ctl_done(io);
12315 break;
12316 }
12317 mtx_lock(&lun->lun_lock);
12318 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) {
12319 mtx_unlock(&lun->lun_lock);
12320 ctl_done(io);
12321 break;
12322 }
12323 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12324 mtx_unlock(&lun->lun_lock);
12325 ctl_enqueue_rtr(io);
12326 break;
12327 case CTL_MSG_FINISH_IO:
12328 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12329 ctl_done(io);
12330 break;
12331 }
12332 if (targ_lun >= ctl_max_luns ||
12333 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12334 ctl_free_io(io);
12335 break;
12336 }
12337 mtx_lock(&lun->lun_lock);
12338 ctl_try_unblock_others(lun, io, TRUE);
12339 LIST_REMOVE(&io->io_hdr, ooa_links);
12340 mtx_unlock(&lun->lun_lock);
12341 ctl_free_io(io);
12342 break;
12343 case CTL_MSG_PERS_ACTION:
12344 ctl_hndl_per_res_out_on_other_sc(io);
12345 ctl_free_io(io);
12346 break;
12347 case CTL_MSG_BAD_JUJU:
12348 ctl_done(io);
12349 break;
12350 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */
12351 ctl_datamove_remote(io);
12352 break;
12353 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */
12354 ctl_datamove_done(io, false);
12355 break;
12356 case CTL_MSG_FAILOVER:
12357 ctl_failover_lun(io);
12358 ctl_free_io(io);
12359 break;
12360 default:
12361 printf("%s: Invalid message type %d\n",
12362 __func__, io->io_hdr.msg_type);
12363 ctl_free_io(io);
12364 break;
12365 }
12366
12367 }
12368
12369 /*
12370 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12371 * there is no match.
12372 */
12373 static ctl_lun_error_pattern
12374 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12375 {
12376 const struct ctl_cmd_entry *entry;
12377 ctl_lun_error_pattern filtered_pattern, pattern;
12378
12379 pattern = desc->error_pattern;
12380
12381 /*
12382 * XXX KDM we need more data passed into this function to match a
12383 * custom pattern, and we actually need to implement custom pattern
12384 * matching.
12385 */
12386 if (pattern & CTL_LUN_PAT_CMD)
12387 return (CTL_LUN_PAT_CMD);
12388
12389 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12390 return (CTL_LUN_PAT_ANY);
12391
12392 entry = ctl_get_cmd_entry(ctsio, NULL);
12393
12394 filtered_pattern = entry->pattern & pattern;
12395
12396 /*
12397 * If the user requested specific flags in the pattern (e.g.
12398 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12399 * flags.
12400 *
12401 * If the user did not specify any flags, it doesn't matter whether
12402 * or not the command supports the flags.
12403 */
12404 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12405 (pattern & ~CTL_LUN_PAT_MASK))
12406 return (CTL_LUN_PAT_NONE);
12407
12408 /*
12409 * If the user asked for a range check, see if the requested LBA
12410 * range overlaps with this command's LBA range.
12411 */
12412 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12413 uint64_t lba1;
12414 uint64_t len1;
12415 ctl_action action;
12416 int retval;
12417
12418 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12419 if (retval != 0)
12420 return (CTL_LUN_PAT_NONE);
12421
12422 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12423 desc->lba_range.len, FALSE);
12424 /*
12425 * A "pass" means that the LBA ranges don't overlap, so
12426 * this doesn't match the user's range criteria.
12427 */
12428 if (action == CTL_ACTION_PASS)
12429 return (CTL_LUN_PAT_NONE);
12430 }
12431
12432 return (filtered_pattern);
12433 }
12434
12435 static void
12436 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12437 {
12438 struct ctl_error_desc *desc, *desc2;
12439
12440 mtx_assert(&lun->lun_lock, MA_OWNED);
12441
12442 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12443 ctl_lun_error_pattern pattern;
12444 /*
12445 * Check to see whether this particular command matches
12446 * the pattern in the descriptor.
12447 */
12448 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12449 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12450 continue;
12451
12452 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12453 case CTL_LUN_INJ_ABORTED:
12454 ctl_set_aborted(&io->scsiio);
12455 break;
12456 case CTL_LUN_INJ_MEDIUM_ERR:
12457 ctl_set_medium_error(&io->scsiio,
12458 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
12459 CTL_FLAG_DATA_OUT);
12460 break;
12461 case CTL_LUN_INJ_UA:
12462 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12463 * OCCURRED */
12464 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12465 break;
12466 case CTL_LUN_INJ_CUSTOM:
12467 /*
12468 * We're assuming the user knows what he is doing.
12469 * Just copy the sense information without doing
12470 * checks.
12471 */
12472 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12473 MIN(sizeof(desc->custom_sense),
12474 sizeof(io->scsiio.sense_data)));
12475 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12476 io->scsiio.sense_len = SSD_FULL_SIZE;
12477 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12478 break;
12479 case CTL_LUN_INJ_NONE:
12480 default:
12481 /*
12482 * If this is an error injection type we don't know
12483 * about, clear the continuous flag (if it is set)
12484 * so it will get deleted below.
12485 */
12486 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12487 break;
12488 }
12489 /*
12490 * By default, each error injection action is a one-shot
12491 */
12492 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12493 continue;
12494
12495 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12496
12497 free(desc, M_CTL);
12498 }
12499 }
12500
12501 #ifdef CTL_IO_DELAY
12502 static void
12503 ctl_datamove_timer_wakeup(void *arg)
12504 {
12505 union ctl_io *io;
12506
12507 io = (union ctl_io *)arg;
12508
12509 ctl_datamove(io);
12510 }
12511 #endif /* CTL_IO_DELAY */
12512
12513 static void
12514 ctl_datamove_done_process(union ctl_io *io)
12515 {
12516 #ifdef CTL_TIME_IO
12517 struct bintime cur_bt;
12518 #endif
12519
12520 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
12521 ("%s: unexpected I/O type %x", __func__, io->io_hdr.io_type));
12522
12523 #ifdef CTL_TIME_IO
12524 getbinuptime(&cur_bt);
12525 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12526 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12527 #endif
12528 io->io_hdr.num_dmas++;
12529
12530 if ((io->io_hdr.port_status != 0) &&
12531 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
12532 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
12533 ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1,
12534 /*retry_count*/ io->io_hdr.port_status);
12535 } else if (io->scsiio.kern_data_resid != 0 &&
12536 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT &&
12537 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
12538 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
12539 ctl_set_invalid_field_ciu(&io->scsiio);
12540 } else if (ctl_debug & CTL_DEBUG_CDB_DATA)
12541 ctl_data_print(io);
12542 }
12543
12544 void
12545 ctl_datamove_done(union ctl_io *io, bool samethr)
12546 {
12547
12548 ctl_datamove_done_process(io);
12549 io->scsiio.be_move_done(io, samethr);
12550 }
12551
12552 void
12553 ctl_datamove(union ctl_io *io)
12554 {
12555 void (*fe_datamove)(union ctl_io *io);
12556
12557 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12558
12559 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12560
12561 /* No data transferred yet. Frontend must update this when done. */
12562 io->scsiio.kern_data_resid = io->scsiio.kern_data_len;
12563
12564 #ifdef CTL_TIME_IO
12565 getbinuptime(&io->io_hdr.dma_start_bt);
12566 #endif /* CTL_TIME_IO */
12567
12568 #ifdef CTL_IO_DELAY
12569 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12570 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12571 } else {
12572 struct ctl_lun *lun;
12573
12574 lun = CTL_LUN(io);
12575 if ((lun != NULL)
12576 && (lun->delay_info.datamove_delay > 0)) {
12577 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12578 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12579 callout_reset(&io->io_hdr.delay_callout,
12580 lun->delay_info.datamove_delay * hz,
12581 ctl_datamove_timer_wakeup, io);
12582 if (lun->delay_info.datamove_type ==
12583 CTL_DELAY_TYPE_ONESHOT)
12584 lun->delay_info.datamove_delay = 0;
12585 return;
12586 }
12587 }
12588 #endif
12589
12590 /*
12591 * This command has been aborted. Set the port status, so we fail
12592 * the data move.
12593 */
12594 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12595 printf("ctl_datamove: tag 0x%jx on (%u:%u:%u) aborted\n",
12596 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12597 io->io_hdr.nexus.targ_port,
12598 io->io_hdr.nexus.targ_lun);
12599 io->io_hdr.port_status = 31337;
12600 ctl_datamove_done_process(io);
12601 io->scsiio.be_move_done(io, true);
12602 return;
12603 }
12604
12605 /* Don't confuse frontend with zero length data move. */
12606 if (io->scsiio.kern_data_len == 0) {
12607 ctl_datamove_done_process(io);
12608 io->scsiio.be_move_done(io, true);
12609 return;
12610 }
12611
12612 fe_datamove = CTL_PORT(io)->fe_datamove;
12613 fe_datamove(io);
12614 }
12615
12616 static void
12617 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12618 {
12619 union ctl_ha_msg msg;
12620 #ifdef CTL_TIME_IO
12621 struct bintime cur_bt;
12622 #endif
12623
12624 memset(&msg, 0, sizeof(msg));
12625 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12626 msg.hdr.original_sc = io;
12627 msg.hdr.serializing_sc = io->io_hdr.remote_io;
12628 msg.hdr.nexus = io->io_hdr.nexus;
12629 msg.hdr.status = io->io_hdr.status;
12630 msg.scsi.kern_data_resid = io->scsiio.kern_data_resid;
12631 msg.scsi.tag_num = io->scsiio.tag_num;
12632 msg.scsi.tag_type = io->scsiio.tag_type;
12633 msg.scsi.scsi_status = io->scsiio.scsi_status;
12634 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12635 io->scsiio.sense_len);
12636 msg.scsi.sense_len = io->scsiio.sense_len;
12637 msg.scsi.port_status = io->io_hdr.port_status;
12638 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12639 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12640 ctl_failover_io(io, /*have_lock*/ have_lock);
12641 return;
12642 }
12643 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12644 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12645 msg.scsi.sense_len, M_WAITOK);
12646
12647 #ifdef CTL_TIME_IO
12648 getbinuptime(&cur_bt);
12649 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12650 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12651 #endif
12652 io->io_hdr.num_dmas++;
12653 }
12654
12655 /*
12656 * The DMA to the remote side is done, now we need to tell the other side
12657 * we're done so it can continue with its data movement.
12658 */
12659 static void
12660 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12661 {
12662 union ctl_io *io;
12663 uint32_t i;
12664
12665 io = rq->context;
12666
12667 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12668 printf("%s: ISC DMA write failed with error %d", __func__,
12669 rq->ret);
12670 ctl_set_internal_failure(&io->scsiio,
12671 /*sks_valid*/ 1,
12672 /*retry_count*/ rq->ret);
12673 }
12674
12675 ctl_dt_req_free(rq);
12676
12677 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12678 free(CTL_LSGLT(io)[i].addr, M_CTL);
12679 free(CTL_RSGL(io), M_CTL);
12680 CTL_RSGL(io) = NULL;
12681 CTL_LSGL(io) = NULL;
12682
12683 /*
12684 * The data is in local and remote memory, so now we need to send
12685 * status (good or back) back to the other side.
12686 */
12687 ctl_send_datamove_done(io, /*have_lock*/ 0);
12688 }
12689
12690 /*
12691 * We've moved the data from the host/controller into local memory. Now we
12692 * need to push it over to the remote controller's memory.
12693 */
12694 static int
12695 ctl_datamove_remote_dm_write_cb(union ctl_io *io, bool samethr)
12696 {
12697 int retval;
12698
12699 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12700 ctl_datamove_remote_write_cb);
12701 return (retval);
12702 }
12703
12704 static void
12705 ctl_datamove_remote_write(union ctl_io *io)
12706 {
12707 int retval;
12708 void (*fe_datamove)(union ctl_io *io);
12709
12710 /*
12711 * - Get the data from the host/HBA into local memory.
12712 * - DMA memory from the local controller to the remote controller.
12713 * - Send status back to the remote controller.
12714 */
12715
12716 retval = ctl_datamove_remote_sgl_setup(io);
12717 if (retval != 0)
12718 return;
12719
12720 /* Switch the pointer over so the FETD knows what to do */
12721 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io);
12722
12723 /*
12724 * Use a custom move done callback, since we need to send completion
12725 * back to the other controller, not to the backend on this side.
12726 */
12727 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12728
12729 fe_datamove = CTL_PORT(io)->fe_datamove;
12730 fe_datamove(io);
12731 }
12732
12733 static int
12734 ctl_datamove_remote_dm_read_cb(union ctl_io *io, bool samethr)
12735 {
12736 uint32_t i;
12737
12738 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12739 free(CTL_LSGLT(io)[i].addr, M_CTL);
12740 free(CTL_RSGL(io), M_CTL);
12741 CTL_RSGL(io) = NULL;
12742 CTL_LSGL(io) = NULL;
12743
12744 /*
12745 * The read is done, now we need to send status (good or bad) back
12746 * to the other side.
12747 */
12748 ctl_send_datamove_done(io, /*have_lock*/ 0);
12749
12750 return (0);
12751 }
12752
12753 static void
12754 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12755 {
12756 union ctl_io *io;
12757 void (*fe_datamove)(union ctl_io *io);
12758
12759 io = rq->context;
12760
12761 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12762 printf("%s: ISC DMA read failed with error %d\n", __func__,
12763 rq->ret);
12764 ctl_set_internal_failure(&io->scsiio,
12765 /*sks_valid*/ 1,
12766 /*retry_count*/ rq->ret);
12767 }
12768
12769 ctl_dt_req_free(rq);
12770
12771 /* Switch the pointer over so the FETD knows what to do */
12772 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io);
12773
12774 /*
12775 * Use a custom move done callback, since we need to send completion
12776 * back to the other controller, not to the backend on this side.
12777 */
12778 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12779
12780 /* XXX KDM add checks like the ones in ctl_datamove? */
12781
12782 fe_datamove = CTL_PORT(io)->fe_datamove;
12783 fe_datamove(io);
12784 }
12785
12786 static int
12787 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12788 {
12789 struct ctl_sg_entry *local_sglist;
12790 uint32_t len_to_go;
12791 int retval;
12792 int i;
12793
12794 retval = 0;
12795 local_sglist = CTL_LSGL(io);
12796 len_to_go = io->scsiio.kern_data_len;
12797
12798 /*
12799 * The difficult thing here is that the size of the various
12800 * S/G segments may be different than the size from the
12801 * remote controller. That'll make it harder when DMAing
12802 * the data back to the other side.
12803 */
12804 for (i = 0; len_to_go > 0; i++) {
12805 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12806 local_sglist[i].addr =
12807 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12808
12809 len_to_go -= local_sglist[i].len;
12810 }
12811 /*
12812 * Reset the number of S/G entries accordingly. The original
12813 * number of S/G entries is available in rem_sg_entries.
12814 */
12815 io->scsiio.kern_sg_entries = i;
12816
12817 return (retval);
12818 }
12819
12820 static int
12821 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12822 ctl_ha_dt_cb callback)
12823 {
12824 struct ctl_ha_dt_req *rq;
12825 struct ctl_sg_entry *remote_sglist, *local_sglist;
12826 uint32_t local_used, remote_used, total_used;
12827 int i, j, isc_ret;
12828
12829 rq = ctl_dt_req_alloc();
12830
12831 /*
12832 * If we failed to allocate the request, and if the DMA didn't fail
12833 * anyway, set busy status. This is just a resource allocation
12834 * failure.
12835 */
12836 if ((rq == NULL)
12837 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12838 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12839 ctl_set_busy(&io->scsiio);
12840
12841 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12842 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12843 if (rq != NULL)
12844 ctl_dt_req_free(rq);
12845
12846 /*
12847 * The data move failed. We need to return status back
12848 * to the other controller. No point in trying to DMA
12849 * data to the remote controller.
12850 */
12851
12852 ctl_send_datamove_done(io, /*have_lock*/ 0);
12853
12854 return (1);
12855 }
12856
12857 local_sglist = CTL_LSGL(io);
12858 remote_sglist = CTL_RSGL(io);
12859 local_used = 0;
12860 remote_used = 0;
12861 total_used = 0;
12862
12863 /*
12864 * Pull/push the data over the wire from/to the other controller.
12865 * This takes into account the possibility that the local and
12866 * remote sglists may not be identical in terms of the size of
12867 * the elements and the number of elements.
12868 *
12869 * One fundamental assumption here is that the length allocated for
12870 * both the local and remote sglists is identical. Otherwise, we've
12871 * essentially got a coding error of some sort.
12872 */
12873 isc_ret = CTL_HA_STATUS_SUCCESS;
12874 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12875 uint32_t cur_len;
12876 uint8_t *tmp_ptr;
12877
12878 rq->command = command;
12879 rq->context = io;
12880
12881 /*
12882 * Both pointers should be aligned. But it is possible
12883 * that the allocation length is not. They should both
12884 * also have enough slack left over at the end, though,
12885 * to round up to the next 8 byte boundary.
12886 */
12887 cur_len = MIN(local_sglist[i].len - local_used,
12888 remote_sglist[j].len - remote_used);
12889 rq->size = cur_len;
12890
12891 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12892 tmp_ptr += local_used;
12893
12894 #if 0
12895 /* Use physical addresses when talking to ISC hardware */
12896 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12897 /* XXX KDM use busdma */
12898 rq->local = vtophys(tmp_ptr);
12899 } else
12900 rq->local = tmp_ptr;
12901 #else
12902 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12903 ("HA does not support BUS_ADDR"));
12904 rq->local = tmp_ptr;
12905 #endif
12906
12907 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12908 tmp_ptr += remote_used;
12909 rq->remote = tmp_ptr;
12910
12911 rq->callback = NULL;
12912
12913 local_used += cur_len;
12914 if (local_used >= local_sglist[i].len) {
12915 i++;
12916 local_used = 0;
12917 }
12918
12919 remote_used += cur_len;
12920 if (remote_used >= remote_sglist[j].len) {
12921 j++;
12922 remote_used = 0;
12923 }
12924 total_used += cur_len;
12925
12926 if (total_used >= io->scsiio.kern_data_len)
12927 rq->callback = callback;
12928
12929 isc_ret = ctl_dt_single(rq);
12930 if (isc_ret > CTL_HA_STATUS_SUCCESS)
12931 break;
12932 }
12933 if (isc_ret != CTL_HA_STATUS_WAIT) {
12934 rq->ret = isc_ret;
12935 callback(rq);
12936 }
12937
12938 return (0);
12939 }
12940
12941 static void
12942 ctl_datamove_remote_read(union ctl_io *io)
12943 {
12944 int retval;
12945 uint32_t i;
12946
12947 /*
12948 * This will send an error to the other controller in the case of a
12949 * failure.
12950 */
12951 retval = ctl_datamove_remote_sgl_setup(io);
12952 if (retval != 0)
12953 return;
12954
12955 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12956 ctl_datamove_remote_read_cb);
12957 if (retval != 0) {
12958 /*
12959 * Make sure we free memory if there was an error.. The
12960 * ctl_datamove_remote_xfer() function will send the
12961 * datamove done message, or call the callback with an
12962 * error if there is a problem.
12963 */
12964 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12965 free(CTL_LSGLT(io)[i].addr, M_CTL);
12966 free(CTL_RSGL(io), M_CTL);
12967 CTL_RSGL(io) = NULL;
12968 CTL_LSGL(io) = NULL;
12969 }
12970 }
12971
12972 /*
12973 * Process a datamove request from the other controller. This is used for
12974 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12975 * first. Once that is complete, the data gets DMAed into the remote
12976 * controller's memory. For reads, we DMA from the remote controller's
12977 * memory into our memory first, and then move it out to the FETD.
12978 */
12979 static void
12980 ctl_datamove_remote(union ctl_io *io)
12981 {
12982
12983 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12984
12985 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12986 ctl_failover_io(io, /*have_lock*/ 0);
12987 return;
12988 }
12989
12990 /*
12991 * Note that we look for an aborted I/O here, but don't do some of
12992 * the other checks that ctl_datamove() normally does.
12993 * We don't need to run the datamove delay code, since that should
12994 * have been done if need be on the other controller.
12995 */
12996 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12997 printf("%s: tag 0x%jx on (%u:%u:%u) aborted\n", __func__,
12998 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12999 io->io_hdr.nexus.targ_port,
13000 io->io_hdr.nexus.targ_lun);
13001 io->io_hdr.port_status = 31338;
13002 ctl_send_datamove_done(io, /*have_lock*/ 0);
13003 return;
13004 }
13005
13006 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
13007 ctl_datamove_remote_write(io);
13008 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
13009 ctl_datamove_remote_read(io);
13010 else {
13011 io->io_hdr.port_status = 31339;
13012 ctl_send_datamove_done(io, /*have_lock*/ 0);
13013 }
13014 }
13015
13016 static void
13017 ctl_process_done(union ctl_io *io)
13018 {
13019 struct ctl_softc *softc = CTL_SOFTC(io);
13020 struct ctl_port *port = CTL_PORT(io);
13021 struct ctl_lun *lun = CTL_LUN(io);
13022 void (*fe_done)(union ctl_io *io);
13023 union ctl_ha_msg msg;
13024
13025 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13026 fe_done = port->fe_done;
13027
13028 #ifdef CTL_TIME_IO
13029 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13030 char str[256];
13031 char path_str[64];
13032 struct sbuf sb;
13033
13034 ctl_scsi_path_string(io, path_str, sizeof(path_str));
13035 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13036
13037 ctl_io_sbuf(io, &sb);
13038 sbuf_cat(&sb, path_str);
13039 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13040 (intmax_t)time_uptime - io->io_hdr.start_time);
13041 sbuf_finish(&sb);
13042 printf("%s", sbuf_data(&sb));
13043 }
13044 #endif /* CTL_TIME_IO */
13045
13046 switch (io->io_hdr.io_type) {
13047 case CTL_IO_SCSI:
13048 break;
13049 case CTL_IO_TASK:
13050 if (ctl_debug & CTL_DEBUG_INFO)
13051 ctl_io_error_print(io, NULL);
13052 fe_done(io);
13053 return;
13054 default:
13055 panic("%s: Invalid CTL I/O type %d\n",
13056 __func__, io->io_hdr.io_type);
13057 }
13058
13059 if (lun == NULL) {
13060 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13061 io->io_hdr.nexus.targ_mapped_lun));
13062 goto bailout;
13063 }
13064
13065 mtx_lock(&lun->lun_lock);
13066
13067 /*
13068 * Check to see if we have any informational exception and status
13069 * of this command can be modified to report it in form of either
13070 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field.
13071 */
13072 if (lun->ie_reported == 0 && lun->ie_asc != 0 &&
13073 io->io_hdr.status == CTL_SUCCESS &&
13074 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) {
13075 uint8_t mrie = lun->MODE_IE.mrie;
13076 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) ||
13077 (lun->MODE_VER.byte3 & SMS_VER_PER));
13078 if (((mrie == SIEP_MRIE_REC_COND && per) ||
13079 mrie == SIEP_MRIE_REC_UNCOND ||
13080 mrie == SIEP_MRIE_NO_SENSE) &&
13081 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags &
13082 CTL_CMD_FLAG_NO_SENSE) == 0) {
13083 ctl_set_sense(&io->scsiio,
13084 /*current_error*/ 1,
13085 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ?
13086 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR,
13087 /*asc*/ lun->ie_asc,
13088 /*ascq*/ lun->ie_ascq,
13089 SSD_ELEM_NONE);
13090 lun->ie_reported = 1;
13091 }
13092 } else if (lun->ie_reported < 0)
13093 lun->ie_reported = 0;
13094
13095 /*
13096 * Check to see if we have any errors to inject here. We only
13097 * inject errors for commands that don't already have errors set.
13098 */
13099 if (!STAILQ_EMPTY(&lun->error_list) &&
13100 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
13101 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
13102 ctl_inject_error(lun, io);
13103
13104 /*
13105 * XXX KDM how do we treat commands that aren't completed
13106 * successfully?
13107 *
13108 * XXX KDM should we also track I/O latency?
13109 */
13110 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13111 io->io_hdr.io_type == CTL_IO_SCSI) {
13112 int type;
13113 #ifdef CTL_TIME_IO
13114 struct bintime bt;
13115
13116 getbinuptime(&bt);
13117 bintime_sub(&bt, &io->io_hdr.start_bt);
13118 #endif
13119 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13120 CTL_FLAG_DATA_IN)
13121 type = CTL_STATS_READ;
13122 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13123 CTL_FLAG_DATA_OUT)
13124 type = CTL_STATS_WRITE;
13125 else
13126 type = CTL_STATS_NO_IO;
13127
13128 lun->stats.bytes[type] += io->scsiio.kern_total_len;
13129 lun->stats.operations[type] ++;
13130 lun->stats.dmas[type] += io->io_hdr.num_dmas;
13131 #ifdef CTL_TIME_IO
13132 bintime_add(&lun->stats.dma_time[type], &io->io_hdr.dma_bt);
13133 bintime_add(&lun->stats.time[type], &bt);
13134 #endif
13135
13136 mtx_lock(&port->port_lock);
13137 port->stats.bytes[type] += io->scsiio.kern_total_len;
13138 port->stats.operations[type] ++;
13139 port->stats.dmas[type] += io->io_hdr.num_dmas;
13140 #ifdef CTL_TIME_IO
13141 bintime_add(&port->stats.dma_time[type], &io->io_hdr.dma_bt);
13142 bintime_add(&port->stats.time[type], &bt);
13143 #endif
13144 mtx_unlock(&port->port_lock);
13145 }
13146
13147 /*
13148 * Run through the blocked queue of this I/O and see if anything
13149 * can be unblocked, now that this I/O is done and will be removed.
13150 * We need to do it before removal to have OOA position to start.
13151 */
13152 ctl_try_unblock_others(lun, io, TRUE);
13153
13154 /*
13155 * Remove this from the OOA queue.
13156 */
13157 LIST_REMOVE(&io->io_hdr, ooa_links);
13158 #ifdef CTL_TIME_IO
13159 if (LIST_EMPTY(&lun->ooa_queue))
13160 lun->last_busy = getsbinuptime();
13161 #endif
13162
13163 /*
13164 * If the LUN has been invalidated, free it if there is nothing
13165 * left on its OOA queue.
13166 */
13167 if ((lun->flags & CTL_LUN_INVALID)
13168 && LIST_EMPTY(&lun->ooa_queue)) {
13169 mtx_unlock(&lun->lun_lock);
13170 ctl_free_lun(lun);
13171 } else
13172 mtx_unlock(&lun->lun_lock);
13173
13174 bailout:
13175
13176 /*
13177 * If this command has been aborted, make sure we set the status
13178 * properly. The FETD is responsible for freeing the I/O and doing
13179 * whatever it needs to do to clean up its state.
13180 */
13181 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13182 ctl_set_task_aborted(&io->scsiio);
13183
13184 /*
13185 * If enabled, print command error status.
13186 */
13187 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
13188 (ctl_debug & CTL_DEBUG_INFO) != 0)
13189 ctl_io_error_print(io, NULL);
13190
13191 /*
13192 * Tell the FETD or the other shelf controller we're done with this
13193 * command. Note that only SCSI commands get to this point. Task
13194 * management commands are completed above.
13195 */
13196 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
13197 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
13198 memset(&msg, 0, sizeof(msg));
13199 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13200 msg.hdr.serializing_sc = io->io_hdr.remote_io;
13201 msg.hdr.nexus = io->io_hdr.nexus;
13202 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13203 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
13204 M_WAITOK);
13205 }
13206
13207 fe_done(io);
13208 }
13209
13210 /*
13211 * Front end should call this if it doesn't do autosense. When the request
13212 * sense comes back in from the initiator, we'll dequeue this and send it.
13213 */
13214 int
13215 ctl_queue_sense(union ctl_io *io)
13216 {
13217 struct ctl_softc *softc = CTL_SOFTC(io);
13218 struct ctl_port *port = CTL_PORT(io);
13219 struct ctl_lun *lun;
13220 struct scsi_sense_data *ps;
13221 uint32_t initidx, p, targ_lun;
13222
13223 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13224
13225 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13226
13227 /*
13228 * LUN lookup will likely move to the ctl_work_thread() once we
13229 * have our new queueing infrastructure (that doesn't put things on
13230 * a per-LUN queue initially). That is so that we can handle
13231 * things like an INQUIRY to a LUN that we don't have enabled. We
13232 * can't deal with that right now.
13233 * If we don't have a LUN for this, just toss the sense information.
13234 */
13235 mtx_lock(&softc->ctl_lock);
13236 if (targ_lun >= ctl_max_luns ||
13237 (lun = softc->ctl_luns[targ_lun]) == NULL) {
13238 mtx_unlock(&softc->ctl_lock);
13239 goto bailout;
13240 }
13241 mtx_lock(&lun->lun_lock);
13242 mtx_unlock(&softc->ctl_lock);
13243
13244 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13245 p = initidx / CTL_MAX_INIT_PER_PORT;
13246 if (lun->pending_sense[p] == NULL) {
13247 lun->pending_sense[p] = malloc(sizeof(*ps) * CTL_MAX_INIT_PER_PORT,
13248 M_CTL, M_NOWAIT | M_ZERO);
13249 }
13250 if ((ps = lun->pending_sense[p]) != NULL) {
13251 ps += initidx % CTL_MAX_INIT_PER_PORT;
13252 memset(ps, 0, sizeof(*ps));
13253 memcpy(ps, &io->scsiio.sense_data, io->scsiio.sense_len);
13254 }
13255 mtx_unlock(&lun->lun_lock);
13256
13257 bailout:
13258 ctl_free_io(io);
13259 return (CTL_RETVAL_COMPLETE);
13260 }
13261
13262 /*
13263 * Primary command inlet from frontend ports. All SCSI and task I/O
13264 * requests must go through this function.
13265 */
13266 int
13267 ctl_queue(union ctl_io *io)
13268 {
13269 struct ctl_port *port = CTL_PORT(io);
13270
13271 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13272
13273 #ifdef CTL_TIME_IO
13274 io->io_hdr.start_time = time_uptime;
13275 getbinuptime(&io->io_hdr.start_bt);
13276 #endif /* CTL_TIME_IO */
13277
13278 /* Map FE-specific LUN ID into global one. */
13279 io->io_hdr.nexus.targ_mapped_lun =
13280 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13281
13282 switch (io->io_hdr.io_type) {
13283 case CTL_IO_SCSI:
13284 case CTL_IO_TASK:
13285 if (ctl_debug & CTL_DEBUG_CDB)
13286 ctl_io_print(io);
13287 ctl_enqueue_incoming(io);
13288 break;
13289 default:
13290 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13291 return (EINVAL);
13292 }
13293
13294 return (CTL_RETVAL_COMPLETE);
13295 }
13296
13297 int
13298 ctl_run(union ctl_io *io)
13299 {
13300 struct ctl_port *port = CTL_PORT(io);
13301
13302 CTL_DEBUG_PRINT(("ctl_run cdb[0]=%02X\n", io->scsiio.cdb[0]));
13303
13304 #ifdef CTL_TIME_IO
13305 io->io_hdr.start_time = time_uptime;
13306 getbinuptime(&io->io_hdr.start_bt);
13307 #endif /* CTL_TIME_IO */
13308
13309 /* Map FE-specific LUN ID into global one. */
13310 io->io_hdr.nexus.targ_mapped_lun =
13311 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13312
13313 switch (io->io_hdr.io_type) {
13314 case CTL_IO_SCSI:
13315 if (ctl_debug & CTL_DEBUG_CDB)
13316 ctl_io_print(io);
13317 ctl_scsiio_precheck(&io->scsiio);
13318 break;
13319 case CTL_IO_TASK:
13320 if (ctl_debug & CTL_DEBUG_CDB)
13321 ctl_io_print(io);
13322 ctl_run_task(io);
13323 break;
13324 default:
13325 printf("ctl_run: unknown I/O type %d\n", io->io_hdr.io_type);
13326 return (EINVAL);
13327 }
13328
13329 return (CTL_RETVAL_COMPLETE);
13330 }
13331
13332 #ifdef CTL_IO_DELAY
13333 static void
13334 ctl_done_timer_wakeup(void *arg)
13335 {
13336 union ctl_io *io;
13337
13338 io = (union ctl_io *)arg;
13339 ctl_done(io);
13340 }
13341 #endif /* CTL_IO_DELAY */
13342
13343 void
13344 ctl_serseq_done(union ctl_io *io)
13345 {
13346 struct ctl_lun *lun = CTL_LUN(io);
13347
13348 /* This is racy, but should not be a problem. */
13349 if (!TAILQ_EMPTY(&io->io_hdr.blocked_queue)) {
13350 mtx_lock(&lun->lun_lock);
13351 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13352 ctl_try_unblock_others(lun, io, FALSE);
13353 mtx_unlock(&lun->lun_lock);
13354 } else
13355 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13356 }
13357
13358 void
13359 ctl_done(union ctl_io *io)
13360 {
13361
13362 /*
13363 * Enable this to catch duplicate completion issues.
13364 */
13365 #if 0
13366 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13367 printf("%s: type %d msg %d cdb %x iptl: "
13368 "%u:%u:%u tag 0x%04x "
13369 "flag %#x status %x\n",
13370 __func__,
13371 io->io_hdr.io_type,
13372 io->io_hdr.msg_type,
13373 io->scsiio.cdb[0],
13374 io->io_hdr.nexus.initid,
13375 io->io_hdr.nexus.targ_port,
13376 io->io_hdr.nexus.targ_lun,
13377 (io->io_hdr.io_type ==
13378 CTL_IO_TASK) ?
13379 io->taskio.tag_num :
13380 io->scsiio.tag_num,
13381 io->io_hdr.flags,
13382 io->io_hdr.status);
13383 } else
13384 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13385 #endif
13386
13387 /*
13388 * This is an internal copy of an I/O, and should not go through
13389 * the normal done processing logic.
13390 */
13391 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13392 return;
13393
13394 #ifdef CTL_IO_DELAY
13395 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13396 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13397 } else {
13398 struct ctl_lun *lun = CTL_LUN(io);
13399
13400 if ((lun != NULL)
13401 && (lun->delay_info.done_delay > 0)) {
13402 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13403 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13404 callout_reset(&io->io_hdr.delay_callout,
13405 lun->delay_info.done_delay * hz,
13406 ctl_done_timer_wakeup, io);
13407 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13408 lun->delay_info.done_delay = 0;
13409 return;
13410 }
13411 }
13412 #endif /* CTL_IO_DELAY */
13413
13414 ctl_enqueue_done(io);
13415 }
13416
13417 static void
13418 ctl_work_thread(void *arg)
13419 {
13420 struct ctl_thread *thr = (struct ctl_thread *)arg;
13421 struct ctl_softc *softc = thr->ctl_softc;
13422 union ctl_io *io;
13423 int retval;
13424
13425 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13426 thread_lock(curthread);
13427 sched_prio(curthread, PUSER - 1);
13428 thread_unlock(curthread);
13429
13430 while (!softc->shutdown) {
13431 /*
13432 * We handle the queues in this order:
13433 * - ISC
13434 * - done queue (to free up resources, unblock other commands)
13435 * - incoming queue
13436 * - RtR queue
13437 *
13438 * If those queues are empty, we break out of the loop and
13439 * go to sleep.
13440 */
13441 mtx_lock(&thr->queue_lock);
13442 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13443 if (io != NULL) {
13444 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13445 mtx_unlock(&thr->queue_lock);
13446 ctl_handle_isc(io);
13447 continue;
13448 }
13449 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13450 if (io != NULL) {
13451 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13452 /* clear any blocked commands, call fe_done */
13453 mtx_unlock(&thr->queue_lock);
13454 ctl_process_done(io);
13455 continue;
13456 }
13457 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13458 if (io != NULL) {
13459 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13460 mtx_unlock(&thr->queue_lock);
13461 if (io->io_hdr.io_type == CTL_IO_TASK)
13462 ctl_run_task(io);
13463 else
13464 ctl_scsiio_precheck(&io->scsiio);
13465 continue;
13466 }
13467 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13468 if (io != NULL) {
13469 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13470 mtx_unlock(&thr->queue_lock);
13471 retval = ctl_scsiio(&io->scsiio);
13472 if (retval != CTL_RETVAL_COMPLETE)
13473 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13474 continue;
13475 }
13476
13477 /* Sleep until we have something to do. */
13478 mtx_sleep(thr, &thr->queue_lock, PDROP, "-", 0);
13479 }
13480 thr->thread = NULL;
13481 kthread_exit();
13482 }
13483
13484 static void
13485 ctl_thresh_thread(void *arg)
13486 {
13487 struct ctl_softc *softc = (struct ctl_softc *)arg;
13488 struct ctl_lun *lun;
13489 struct ctl_logical_block_provisioning_page *page;
13490 const char *attr;
13491 union ctl_ha_msg msg;
13492 uint64_t thres, val;
13493 int i, e, set;
13494
13495 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13496 thread_lock(curthread);
13497 sched_prio(curthread, PUSER - 1);
13498 thread_unlock(curthread);
13499
13500 while (!softc->shutdown) {
13501 mtx_lock(&softc->ctl_lock);
13502 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13503 if ((lun->flags & CTL_LUN_DISABLED) ||
13504 (lun->flags & CTL_LUN_NO_MEDIA) ||
13505 lun->backend->lun_attr == NULL)
13506 continue;
13507 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13508 softc->ha_mode == CTL_HA_MODE_XFER)
13509 continue;
13510 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0)
13511 continue;
13512 e = 0;
13513 page = &lun->MODE_LBP;
13514 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13515 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13516 continue;
13517 thres = scsi_4btoul(page->descr[i].count);
13518 thres <<= CTL_LBP_EXPONENT;
13519 switch (page->descr[i].resource) {
13520 case 0x01:
13521 attr = "blocksavail";
13522 break;
13523 case 0x02:
13524 attr = "blocksused";
13525 break;
13526 case 0xf1:
13527 attr = "poolblocksavail";
13528 break;
13529 case 0xf2:
13530 attr = "poolblocksused";
13531 break;
13532 default:
13533 continue;
13534 }
13535 mtx_unlock(&softc->ctl_lock); // XXX
13536 val = lun->backend->lun_attr(lun->be_lun, attr);
13537 mtx_lock(&softc->ctl_lock);
13538 if (val == UINT64_MAX)
13539 continue;
13540 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13541 == SLBPPD_ARMING_INC)
13542 e = (val >= thres);
13543 else
13544 e = (val <= thres);
13545 if (e)
13546 break;
13547 }
13548 mtx_lock(&lun->lun_lock);
13549 if (e) {
13550 scsi_u64to8b((uint8_t *)&page->descr[i] -
13551 (uint8_t *)page, lun->ua_tpt_info);
13552 if (lun->lasttpt == 0 ||
13553 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13554 lun->lasttpt = time_uptime;
13555 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13556 set = 1;
13557 } else
13558 set = 0;
13559 } else {
13560 lun->lasttpt = 0;
13561 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13562 set = -1;
13563 }
13564 mtx_unlock(&lun->lun_lock);
13565 if (set != 0 &&
13566 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13567 /* Send msg to other side. */
13568 bzero(&msg.ua, sizeof(msg.ua));
13569 msg.hdr.msg_type = CTL_MSG_UA;
13570 msg.hdr.nexus.initid = -1;
13571 msg.hdr.nexus.targ_port = -1;
13572 msg.hdr.nexus.targ_lun = lun->lun;
13573 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13574 msg.ua.ua_all = 1;
13575 msg.ua.ua_set = (set > 0);
13576 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13577 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
13578 mtx_unlock(&softc->ctl_lock); // XXX
13579 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13580 sizeof(msg.ua), M_WAITOK);
13581 mtx_lock(&softc->ctl_lock);
13582 }
13583 }
13584 mtx_sleep(&softc->thresh_thread, &softc->ctl_lock,
13585 PDROP, "-", CTL_LBP_PERIOD * hz);
13586 }
13587 softc->thresh_thread = NULL;
13588 kthread_exit();
13589 }
13590
13591 static void
13592 ctl_enqueue_incoming(union ctl_io *io)
13593 {
13594 struct ctl_softc *softc = CTL_SOFTC(io);
13595 struct ctl_thread *thr;
13596 u_int idx;
13597
13598 idx = (io->io_hdr.nexus.targ_port * 127 +
13599 io->io_hdr.nexus.initid) % worker_threads;
13600 thr = &softc->threads[idx];
13601 mtx_lock(&thr->queue_lock);
13602 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13603 mtx_unlock(&thr->queue_lock);
13604 wakeup(thr);
13605 }
13606
13607 static void
13608 ctl_enqueue_rtr(union ctl_io *io)
13609 {
13610 struct ctl_softc *softc = CTL_SOFTC(io);
13611 struct ctl_thread *thr;
13612
13613 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13614 mtx_lock(&thr->queue_lock);
13615 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13616 mtx_unlock(&thr->queue_lock);
13617 wakeup(thr);
13618 }
13619
13620 static void
13621 ctl_enqueue_done(union ctl_io *io)
13622 {
13623 struct ctl_softc *softc = CTL_SOFTC(io);
13624 struct ctl_thread *thr;
13625
13626 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13627 mtx_lock(&thr->queue_lock);
13628 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13629 mtx_unlock(&thr->queue_lock);
13630 wakeup(thr);
13631 }
13632
13633 static void
13634 ctl_enqueue_isc(union ctl_io *io)
13635 {
13636 struct ctl_softc *softc = CTL_SOFTC(io);
13637 struct ctl_thread *thr;
13638
13639 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13640 mtx_lock(&thr->queue_lock);
13641 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13642 mtx_unlock(&thr->queue_lock);
13643 wakeup(thr);
13644 }
13645
13646 /*
13647 * vim: ts=8
13648 */
13649