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 <[email protected]>
7 * Copyright (c) 2017 Jakub Wojciech Klama <[email protected]>
8 * Copyright (c) 2018 Marcelo Araujo <[email protected]>
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 <[email protected]>
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 ctl_set_invalid_field(/*ctsio*/ ctsio,
7498 /*sks_valid*/ 1,
7499 /*command*/ 1,
7500 /*field*/ 2,
7501 /*bit_valid*/ 1,
7502 /*bit*/ 2);
7503 ctl_done((union ctl_io *)ctsio);
7504 return (CTL_RETVAL_COMPLETE);
7505 }
7506 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7507 break;
7508 case RSO_OPTIONS_OC_SA:
7509 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7510 service_action >= 32) {
7511 goto invalid;
7512 }
7513 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7514 break;
7515 case RSO_OPTIONS_OC_ASA:
7516 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) != 0 &&
7517 service_action >= 32) {
7518 goto invalid;
7519 }
7520 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7521 break;
7522 default:
7523 invalid:
7524 ctl_set_invalid_field(/*ctsio*/ ctsio,
7525 /*sks_valid*/ 1,
7526 /*command*/ 1,
7527 /*field*/ 2,
7528 /*bit_valid*/ 1,
7529 /*bit*/ 2);
7530 ctl_done((union ctl_io *)ctsio);
7531 return (CTL_RETVAL_COMPLETE);
7532 }
7533
7534 alloc_len = scsi_4btoul(cdb->length);
7535
7536 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7537 ctsio->kern_sg_entries = 0;
7538 ctsio->kern_rel_offset = 0;
7539 ctsio->kern_data_len = min(total_len, alloc_len);
7540 ctsio->kern_total_len = ctsio->kern_data_len;
7541
7542 switch (cdb->options & RSO_OPTIONS_MASK) {
7543 case RSO_OPTIONS_ALL:
7544 all = (struct scsi_report_supported_opcodes_all *)
7545 ctsio->kern_data_ptr;
7546 num = 0;
7547 for (i = 0; i < 256; i++) {
7548 entry = &ctl_cmd_table[i];
7549 if (entry->flags & CTL_CMD_FLAG_SA5) {
7550 for (j = 0; j < 32; j++) {
7551 sentry = &((const struct ctl_cmd_entry *)
7552 entry->execute)[j];
7553 if (!ctl_cmd_applicable(
7554 lun->be_lun->lun_type, sentry))
7555 continue;
7556 descr = &all->descr[num++];
7557 descr->opcode = i;
7558 scsi_ulto2b(j, descr->service_action);
7559 descr->flags = RSO_SERVACTV;
7560 scsi_ulto2b(sentry->length,
7561 descr->cdb_length);
7562 }
7563 } else {
7564 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7565 entry))
7566 continue;
7567 descr = &all->descr[num++];
7568 descr->opcode = i;
7569 scsi_ulto2b(0, descr->service_action);
7570 descr->flags = 0;
7571 scsi_ulto2b(entry->length, descr->cdb_length);
7572 }
7573 }
7574 scsi_ulto4b(
7575 num * sizeof(struct scsi_report_supported_opcodes_descr),
7576 all->length);
7577 break;
7578 case RSO_OPTIONS_OC:
7579 one = (struct scsi_report_supported_opcodes_one *)
7580 ctsio->kern_data_ptr;
7581 entry = &ctl_cmd_table[opcode];
7582 goto fill_one;
7583 case RSO_OPTIONS_OC_SA:
7584 one = (struct scsi_report_supported_opcodes_one *)
7585 ctsio->kern_data_ptr;
7586 entry = &ctl_cmd_table[opcode];
7587 entry = &((const struct ctl_cmd_entry *)
7588 entry->execute)[service_action];
7589 fill_one:
7590 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7591 one->support = 3;
7592 scsi_ulto2b(entry->length, one->cdb_length);
7593 one->cdb_usage[0] = opcode;
7594 memcpy(&one->cdb_usage[1], entry->usage,
7595 entry->length - 1);
7596 } else
7597 one->support = 1;
7598 break;
7599 case RSO_OPTIONS_OC_ASA:
7600 one = (struct scsi_report_supported_opcodes_one *)
7601 ctsio->kern_data_ptr;
7602 entry = &ctl_cmd_table[opcode];
7603 if (entry->flags & CTL_CMD_FLAG_SA5) {
7604 entry = &((const struct ctl_cmd_entry *)
7605 entry->execute)[service_action];
7606 } else if (service_action != 0) {
7607 one->support = 1;
7608 break;
7609 }
7610 goto fill_one;
7611 }
7612
7613 ctl_set_success(ctsio);
7614 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7615 ctsio->be_move_done = ctl_config_move_done;
7616 ctl_datamove((union ctl_io *)ctsio);
7617 return(retval);
7618 }
7619
7620 int
ctl_report_supported_tmf(struct ctl_scsiio * ctsio)7621 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7622 {
7623 struct scsi_report_supported_tmf *cdb;
7624 struct scsi_report_supported_tmf_ext_data *data;
7625 int retval;
7626 int alloc_len, total_len;
7627
7628 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7629
7630 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7631
7632 retval = CTL_RETVAL_COMPLETE;
7633
7634 if (cdb->options & RST_REPD)
7635 total_len = sizeof(struct scsi_report_supported_tmf_ext_data);
7636 else
7637 total_len = sizeof(struct scsi_report_supported_tmf_data);
7638 alloc_len = scsi_4btoul(cdb->length);
7639
7640 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7641 ctsio->kern_sg_entries = 0;
7642 ctsio->kern_rel_offset = 0;
7643 ctsio->kern_data_len = min(total_len, alloc_len);
7644 ctsio->kern_total_len = ctsio->kern_data_len;
7645
7646 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr;
7647 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7648 RST_TRS;
7649 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7650 data->length = total_len - 4;
7651
7652 ctl_set_success(ctsio);
7653 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7654 ctsio->be_move_done = ctl_config_move_done;
7655 ctl_datamove((union ctl_io *)ctsio);
7656 return (retval);
7657 }
7658
7659 int
ctl_report_timestamp(struct ctl_scsiio * ctsio)7660 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7661 {
7662 struct scsi_report_timestamp *cdb;
7663 struct scsi_report_timestamp_data *data;
7664 struct timeval tv;
7665 int64_t timestamp;
7666 int retval;
7667 int alloc_len, total_len;
7668
7669 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7670
7671 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7672
7673 retval = CTL_RETVAL_COMPLETE;
7674
7675 total_len = sizeof(struct scsi_report_timestamp_data);
7676 alloc_len = scsi_4btoul(cdb->length);
7677
7678 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7679 ctsio->kern_sg_entries = 0;
7680 ctsio->kern_rel_offset = 0;
7681 ctsio->kern_data_len = min(total_len, alloc_len);
7682 ctsio->kern_total_len = ctsio->kern_data_len;
7683
7684 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7685 scsi_ulto2b(sizeof(*data) - 2, data->length);
7686 data->origin = RTS_ORIG_OUTSIDE;
7687 getmicrotime(&tv);
7688 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7689 scsi_ulto4b(timestamp >> 16, data->timestamp);
7690 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7691
7692 ctl_set_success(ctsio);
7693 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7694 ctsio->be_move_done = ctl_config_move_done;
7695 ctl_datamove((union ctl_io *)ctsio);
7696 return (retval);
7697 }
7698
7699 int
ctl_persistent_reserve_in(struct ctl_scsiio * ctsio)7700 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7701 {
7702 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7703 struct ctl_lun *lun = CTL_LUN(ctsio);
7704 struct scsi_per_res_in *cdb;
7705 int alloc_len, total_len = 0;
7706 /* struct scsi_per_res_in_rsrv in_data; */
7707 uint64_t key;
7708
7709 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7710
7711 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7712
7713 alloc_len = scsi_2btoul(cdb->length);
7714
7715 retry:
7716 mtx_lock(&lun->lun_lock);
7717 switch (cdb->action) {
7718 case SPRI_RK: /* read keys */
7719 total_len = sizeof(struct scsi_per_res_in_keys) +
7720 lun->pr_key_count *
7721 sizeof(struct scsi_per_res_key);
7722 break;
7723 case SPRI_RR: /* read reservation */
7724 if (lun->flags & CTL_LUN_PR_RESERVED)
7725 total_len = sizeof(struct scsi_per_res_in_rsrv);
7726 else
7727 total_len = sizeof(struct scsi_per_res_in_header);
7728 break;
7729 case SPRI_RC: /* report capabilities */
7730 total_len = sizeof(struct scsi_per_res_cap);
7731 break;
7732 case SPRI_RS: /* read full status */
7733 total_len = sizeof(struct scsi_per_res_in_header) +
7734 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7735 lun->pr_key_count;
7736 break;
7737 default:
7738 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7739 }
7740 mtx_unlock(&lun->lun_lock);
7741
7742 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7743 ctsio->kern_rel_offset = 0;
7744 ctsio->kern_sg_entries = 0;
7745 ctsio->kern_data_len = min(total_len, alloc_len);
7746 ctsio->kern_total_len = ctsio->kern_data_len;
7747
7748 mtx_lock(&lun->lun_lock);
7749 switch (cdb->action) {
7750 case SPRI_RK: { // read keys
7751 struct scsi_per_res_in_keys *res_keys;
7752 int i, key_count;
7753
7754 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7755
7756 /*
7757 * We had to drop the lock to allocate our buffer, which
7758 * leaves time for someone to come in with another
7759 * persistent reservation. (That is unlikely, though,
7760 * since this should be the only persistent reservation
7761 * command active right now.)
7762 */
7763 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7764 (lun->pr_key_count *
7765 sizeof(struct scsi_per_res_key)))){
7766 mtx_unlock(&lun->lun_lock);
7767 free(ctsio->kern_data_ptr, M_CTL);
7768 printf("%s: reservation length changed, retrying\n",
7769 __func__);
7770 goto retry;
7771 }
7772
7773 scsi_ulto4b(lun->pr_generation, res_keys->header.generation);
7774
7775 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7776 lun->pr_key_count, res_keys->header.length);
7777
7778 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7779 if ((key = ctl_get_prkey(lun, i)) == 0)
7780 continue;
7781
7782 /*
7783 * We used lun->pr_key_count to calculate the
7784 * size to allocate. If it turns out the number of
7785 * initiators with the registered flag set is
7786 * larger than that (i.e. they haven't been kept in
7787 * sync), we've got a problem.
7788 */
7789 if (key_count >= lun->pr_key_count) {
7790 key_count++;
7791 continue;
7792 }
7793 scsi_u64to8b(key, res_keys->keys[key_count].key);
7794 key_count++;
7795 }
7796 break;
7797 }
7798 case SPRI_RR: { // read reservation
7799 struct scsi_per_res_in_rsrv *res;
7800 int tmp_len, header_only;
7801
7802 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7803
7804 scsi_ulto4b(lun->pr_generation, res->header.generation);
7805
7806 if (lun->flags & CTL_LUN_PR_RESERVED)
7807 {
7808 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7809 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7810 res->header.length);
7811 header_only = 0;
7812 } else {
7813 tmp_len = sizeof(struct scsi_per_res_in_header);
7814 scsi_ulto4b(0, res->header.length);
7815 header_only = 1;
7816 }
7817
7818 /*
7819 * We had to drop the lock to allocate our buffer, which
7820 * leaves time for someone to come in with another
7821 * persistent reservation. (That is unlikely, though,
7822 * since this should be the only persistent reservation
7823 * command active right now.)
7824 */
7825 if (tmp_len != total_len) {
7826 mtx_unlock(&lun->lun_lock);
7827 free(ctsio->kern_data_ptr, M_CTL);
7828 printf("%s: reservation status changed, retrying\n",
7829 __func__);
7830 goto retry;
7831 }
7832
7833 /*
7834 * No reservation held, so we're done.
7835 */
7836 if (header_only != 0)
7837 break;
7838
7839 /*
7840 * If the registration is an All Registrants type, the key
7841 * is 0, since it doesn't really matter.
7842 */
7843 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7844 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7845 res->data.reservation);
7846 }
7847 res->data.scopetype = lun->pr_res_type;
7848 break;
7849 }
7850 case SPRI_RC: //report capabilities
7851 {
7852 struct scsi_per_res_cap *res_cap;
7853 uint16_t type_mask;
7854
7855 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7856 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7857 res_cap->flags1 = SPRI_CRH;
7858 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5;
7859 type_mask = SPRI_TM_WR_EX_AR |
7860 SPRI_TM_EX_AC_RO |
7861 SPRI_TM_WR_EX_RO |
7862 SPRI_TM_EX_AC |
7863 SPRI_TM_WR_EX |
7864 SPRI_TM_EX_AC_AR;
7865 scsi_ulto2b(type_mask, res_cap->type_mask);
7866 break;
7867 }
7868 case SPRI_RS: { // read full status
7869 struct scsi_per_res_in_full *res_status;
7870 struct scsi_per_res_in_full_desc *res_desc;
7871 struct ctl_port *port;
7872 int i, len;
7873
7874 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7875
7876 /*
7877 * We had to drop the lock to allocate our buffer, which
7878 * leaves time for someone to come in with another
7879 * persistent reservation. (That is unlikely, though,
7880 * since this should be the only persistent reservation
7881 * command active right now.)
7882 */
7883 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7884 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7885 lun->pr_key_count)){
7886 mtx_unlock(&lun->lun_lock);
7887 free(ctsio->kern_data_ptr, M_CTL);
7888 printf("%s: reservation length changed, retrying\n",
7889 __func__);
7890 goto retry;
7891 }
7892
7893 scsi_ulto4b(lun->pr_generation, res_status->header.generation);
7894
7895 res_desc = &res_status->desc[0];
7896 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7897 if ((key = ctl_get_prkey(lun, i)) == 0)
7898 continue;
7899
7900 scsi_u64to8b(key, res_desc->res_key.key);
7901 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7902 (lun->pr_res_idx == i ||
7903 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7904 res_desc->flags = SPRI_FULL_R_HOLDER;
7905 res_desc->scopetype = lun->pr_res_type;
7906 }
7907 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7908 res_desc->rel_trgt_port_id);
7909 len = 0;
7910 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7911 if (port != NULL)
7912 len = ctl_create_iid(port,
7913 i % CTL_MAX_INIT_PER_PORT,
7914 res_desc->transport_id);
7915 scsi_ulto4b(len, res_desc->additional_length);
7916 res_desc = (struct scsi_per_res_in_full_desc *)
7917 &res_desc->transport_id[len];
7918 }
7919 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7920 res_status->header.length);
7921 break;
7922 }
7923 default:
7924 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7925 }
7926 mtx_unlock(&lun->lun_lock);
7927
7928 ctl_set_success(ctsio);
7929 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7930 ctsio->be_move_done = ctl_config_move_done;
7931 ctl_datamove((union ctl_io *)ctsio);
7932 return (CTL_RETVAL_COMPLETE);
7933 }
7934
7935 /*
7936 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7937 * it should return.
7938 */
7939 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)7940 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7941 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7942 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7943 struct scsi_per_res_out_parms* param)
7944 {
7945 union ctl_ha_msg persis_io;
7946 int i;
7947
7948 mtx_lock(&lun->lun_lock);
7949 if (sa_res_key == 0) {
7950 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7951 /* validate scope and type */
7952 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7953 SPR_LU_SCOPE) {
7954 mtx_unlock(&lun->lun_lock);
7955 ctl_set_invalid_field(/*ctsio*/ ctsio,
7956 /*sks_valid*/ 1,
7957 /*command*/ 1,
7958 /*field*/ 2,
7959 /*bit_valid*/ 1,
7960 /*bit*/ 4);
7961 ctl_done((union ctl_io *)ctsio);
7962 return (1);
7963 }
7964
7965 if (type>8 || type==2 || type==4 || type==0) {
7966 mtx_unlock(&lun->lun_lock);
7967 ctl_set_invalid_field(/*ctsio*/ ctsio,
7968 /*sks_valid*/ 1,
7969 /*command*/ 1,
7970 /*field*/ 2,
7971 /*bit_valid*/ 1,
7972 /*bit*/ 0);
7973 ctl_done((union ctl_io *)ctsio);
7974 return (1);
7975 }
7976
7977 /*
7978 * Unregister everybody else and build UA for
7979 * them
7980 */
7981 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7982 if (i == residx || ctl_get_prkey(lun, i) == 0)
7983 continue;
7984
7985 ctl_clr_prkey(lun, i);
7986 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7987 }
7988 lun->pr_key_count = 1;
7989 lun->pr_res_type = type;
7990 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7991 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7992 lun->pr_res_idx = residx;
7993 lun->pr_generation++;
7994 mtx_unlock(&lun->lun_lock);
7995
7996 /* send msg to other side */
7997 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7998 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7999 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8000 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8001 persis_io.pr.pr_info.res_type = type;
8002 memcpy(persis_io.pr.pr_info.sa_res_key,
8003 param->serv_act_res_key,
8004 sizeof(param->serv_act_res_key));
8005 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8006 sizeof(persis_io.pr), M_WAITOK);
8007 } else {
8008 /* not all registrants */
8009 mtx_unlock(&lun->lun_lock);
8010 free(ctsio->kern_data_ptr, M_CTL);
8011 ctl_set_invalid_field(ctsio,
8012 /*sks_valid*/ 1,
8013 /*command*/ 0,
8014 /*field*/ 8,
8015 /*bit_valid*/ 0,
8016 /*bit*/ 0);
8017 ctl_done((union ctl_io *)ctsio);
8018 return (1);
8019 }
8020 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8021 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
8022 int found = 0;
8023
8024 if (res_key == sa_res_key) {
8025 /* special case */
8026 /*
8027 * The spec implies this is not good but doesn't
8028 * say what to do. There are two choices either
8029 * generate a res conflict or check condition
8030 * with illegal field in parameter data. Since
8031 * that is what is done when the sa_res_key is
8032 * zero I'll take that approach since this has
8033 * to do with the sa_res_key.
8034 */
8035 mtx_unlock(&lun->lun_lock);
8036 free(ctsio->kern_data_ptr, M_CTL);
8037 ctl_set_invalid_field(ctsio,
8038 /*sks_valid*/ 1,
8039 /*command*/ 0,
8040 /*field*/ 8,
8041 /*bit_valid*/ 0,
8042 /*bit*/ 0);
8043 ctl_done((union ctl_io *)ctsio);
8044 return (1);
8045 }
8046
8047 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8048 if (ctl_get_prkey(lun, i) != sa_res_key)
8049 continue;
8050
8051 found = 1;
8052 ctl_clr_prkey(lun, i);
8053 lun->pr_key_count--;
8054 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8055 }
8056 if (!found) {
8057 mtx_unlock(&lun->lun_lock);
8058 free(ctsio->kern_data_ptr, M_CTL);
8059 ctl_set_reservation_conflict(ctsio);
8060 ctl_done((union ctl_io *)ctsio);
8061 return (CTL_RETVAL_COMPLETE);
8062 }
8063 lun->pr_generation++;
8064 mtx_unlock(&lun->lun_lock);
8065
8066 /* send msg to other side */
8067 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8068 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8069 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8070 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8071 persis_io.pr.pr_info.res_type = type;
8072 memcpy(persis_io.pr.pr_info.sa_res_key,
8073 param->serv_act_res_key,
8074 sizeof(param->serv_act_res_key));
8075 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8076 sizeof(persis_io.pr), M_WAITOK);
8077 } else {
8078 /* Reserved but not all registrants */
8079 /* sa_res_key is res holder */
8080 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
8081 /* validate scope and type */
8082 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
8083 SPR_LU_SCOPE) {
8084 mtx_unlock(&lun->lun_lock);
8085 ctl_set_invalid_field(/*ctsio*/ ctsio,
8086 /*sks_valid*/ 1,
8087 /*command*/ 1,
8088 /*field*/ 2,
8089 /*bit_valid*/ 1,
8090 /*bit*/ 4);
8091 ctl_done((union ctl_io *)ctsio);
8092 return (1);
8093 }
8094
8095 if (type>8 || type==2 || type==4 || type==0) {
8096 mtx_unlock(&lun->lun_lock);
8097 ctl_set_invalid_field(/*ctsio*/ ctsio,
8098 /*sks_valid*/ 1,
8099 /*command*/ 1,
8100 /*field*/ 2,
8101 /*bit_valid*/ 1,
8102 /*bit*/ 0);
8103 ctl_done((union ctl_io *)ctsio);
8104 return (1);
8105 }
8106
8107 /*
8108 * Do the following:
8109 * if sa_res_key != res_key remove all
8110 * registrants w/sa_res_key and generate UA
8111 * for these registrants(Registrations
8112 * Preempted) if it wasn't an exclusive
8113 * reservation generate UA(Reservations
8114 * Preempted) for all other registered nexuses
8115 * if the type has changed. Establish the new
8116 * reservation and holder. If res_key and
8117 * sa_res_key are the same do the above
8118 * except don't unregister the res holder.
8119 */
8120
8121 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8122 if (i == residx || ctl_get_prkey(lun, i) == 0)
8123 continue;
8124
8125 if (sa_res_key == ctl_get_prkey(lun, i)) {
8126 ctl_clr_prkey(lun, i);
8127 lun->pr_key_count--;
8128 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8129 } else if (type != lun->pr_res_type &&
8130 (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8131 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8132 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8133 }
8134 }
8135 lun->pr_res_type = type;
8136 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8137 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8138 lun->pr_res_idx = residx;
8139 else
8140 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8141 lun->pr_generation++;
8142 mtx_unlock(&lun->lun_lock);
8143
8144 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8145 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8146 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8147 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8148 persis_io.pr.pr_info.res_type = type;
8149 memcpy(persis_io.pr.pr_info.sa_res_key,
8150 param->serv_act_res_key,
8151 sizeof(param->serv_act_res_key));
8152 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8153 sizeof(persis_io.pr), M_WAITOK);
8154 } else {
8155 /*
8156 * sa_res_key is not the res holder just
8157 * remove registrants
8158 */
8159 int found=0;
8160
8161 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8162 if (sa_res_key != ctl_get_prkey(lun, i))
8163 continue;
8164
8165 found = 1;
8166 ctl_clr_prkey(lun, i);
8167 lun->pr_key_count--;
8168 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8169 }
8170
8171 if (!found) {
8172 mtx_unlock(&lun->lun_lock);
8173 free(ctsio->kern_data_ptr, M_CTL);
8174 ctl_set_reservation_conflict(ctsio);
8175 ctl_done((union ctl_io *)ctsio);
8176 return (1);
8177 }
8178 lun->pr_generation++;
8179 mtx_unlock(&lun->lun_lock);
8180
8181 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8182 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8183 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8184 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8185 persis_io.pr.pr_info.res_type = type;
8186 memcpy(persis_io.pr.pr_info.sa_res_key,
8187 param->serv_act_res_key,
8188 sizeof(param->serv_act_res_key));
8189 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8190 sizeof(persis_io.pr), M_WAITOK);
8191 }
8192 }
8193 return (0);
8194 }
8195
8196 static void
ctl_pro_preempt_other(struct ctl_lun * lun,union ctl_ha_msg * msg)8197 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8198 {
8199 uint64_t sa_res_key;
8200 int i;
8201
8202 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8203
8204 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8205 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8206 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8207 if (sa_res_key == 0) {
8208 /*
8209 * Unregister everybody else and build UA for
8210 * them
8211 */
8212 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8213 if (i == msg->pr.pr_info.residx ||
8214 ctl_get_prkey(lun, i) == 0)
8215 continue;
8216
8217 ctl_clr_prkey(lun, i);
8218 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8219 }
8220
8221 lun->pr_key_count = 1;
8222 lun->pr_res_type = msg->pr.pr_info.res_type;
8223 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8224 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8225 lun->pr_res_idx = msg->pr.pr_info.residx;
8226 } else {
8227 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8228 if (sa_res_key == ctl_get_prkey(lun, i))
8229 continue;
8230
8231 ctl_clr_prkey(lun, i);
8232 lun->pr_key_count--;
8233 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8234 }
8235 }
8236 } else {
8237 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8238 if (i == msg->pr.pr_info.residx ||
8239 ctl_get_prkey(lun, i) == 0)
8240 continue;
8241
8242 if (sa_res_key == ctl_get_prkey(lun, i)) {
8243 ctl_clr_prkey(lun, i);
8244 lun->pr_key_count--;
8245 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8246 } else if (msg->pr.pr_info.res_type != lun->pr_res_type
8247 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8248 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8249 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8250 }
8251 }
8252 lun->pr_res_type = msg->pr.pr_info.res_type;
8253 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8254 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8255 lun->pr_res_idx = msg->pr.pr_info.residx;
8256 else
8257 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8258 }
8259 lun->pr_generation++;
8260
8261 }
8262
8263 int
ctl_persistent_reserve_out(struct ctl_scsiio * ctsio)8264 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8265 {
8266 struct ctl_softc *softc = CTL_SOFTC(ctsio);
8267 struct ctl_lun *lun = CTL_LUN(ctsio);
8268 int retval;
8269 uint32_t param_len;
8270 struct scsi_per_res_out *cdb;
8271 struct scsi_per_res_out_parms* param;
8272 uint32_t residx;
8273 uint64_t res_key, sa_res_key, key;
8274 uint8_t type;
8275 union ctl_ha_msg persis_io;
8276 int i;
8277
8278 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8279
8280 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8281 retval = CTL_RETVAL_COMPLETE;
8282
8283 /*
8284 * We only support whole-LUN scope. The scope & type are ignored for
8285 * register, register and ignore existing key and clear.
8286 * We sometimes ignore scope and type on preempts too!!
8287 * Verify reservation type here as well.
8288 */
8289 type = cdb->scope_type & SPR_TYPE_MASK;
8290 if ((cdb->action == SPRO_RESERVE)
8291 || (cdb->action == SPRO_RELEASE)) {
8292 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8293 ctl_set_invalid_field(/*ctsio*/ ctsio,
8294 /*sks_valid*/ 1,
8295 /*command*/ 1,
8296 /*field*/ 2,
8297 /*bit_valid*/ 1,
8298 /*bit*/ 4);
8299 ctl_done((union ctl_io *)ctsio);
8300 return (CTL_RETVAL_COMPLETE);
8301 }
8302
8303 if (type>8 || type==2 || type==4 || type==0) {
8304 ctl_set_invalid_field(/*ctsio*/ ctsio,
8305 /*sks_valid*/ 1,
8306 /*command*/ 1,
8307 /*field*/ 2,
8308 /*bit_valid*/ 1,
8309 /*bit*/ 0);
8310 ctl_done((union ctl_io *)ctsio);
8311 return (CTL_RETVAL_COMPLETE);
8312 }
8313 }
8314
8315 param_len = scsi_4btoul(cdb->length);
8316
8317 /* validate the parameter length */
8318 if (param_len != 24) {
8319 ctl_set_invalid_field(ctsio,
8320 /*sks_valid*/ 1,
8321 /*command*/ 1,
8322 /*field*/ 5,
8323 /*bit_valid*/ 1,
8324 /*bit*/ 0);
8325 ctl_done((union ctl_io *)ctsio);
8326 return (CTL_RETVAL_COMPLETE);
8327 }
8328
8329 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8330 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8331 ctsio->kern_data_len = param_len;
8332 ctsio->kern_total_len = param_len;
8333 ctsio->kern_rel_offset = 0;
8334 ctsio->kern_sg_entries = 0;
8335 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8336 ctsio->be_move_done = ctl_config_move_done;
8337 ctl_datamove((union ctl_io *)ctsio);
8338
8339 return (CTL_RETVAL_COMPLETE);
8340 }
8341
8342 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8343
8344 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8345 res_key = scsi_8btou64(param->res_key.key);
8346 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8347
8348 /*
8349 * Validate the reservation key here except for SPRO_REG_IGNO
8350 * This must be done for all other service actions
8351 */
8352 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8353 mtx_lock(&lun->lun_lock);
8354 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8355 if (res_key != key) {
8356 /*
8357 * The current key passed in doesn't match
8358 * the one the initiator previously
8359 * registered.
8360 */
8361 mtx_unlock(&lun->lun_lock);
8362 free(ctsio->kern_data_ptr, M_CTL);
8363 ctl_set_reservation_conflict(ctsio);
8364 ctl_done((union ctl_io *)ctsio);
8365 return (CTL_RETVAL_COMPLETE);
8366 }
8367 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8368 /*
8369 * We are not registered
8370 */
8371 mtx_unlock(&lun->lun_lock);
8372 free(ctsio->kern_data_ptr, M_CTL);
8373 ctl_set_reservation_conflict(ctsio);
8374 ctl_done((union ctl_io *)ctsio);
8375 return (CTL_RETVAL_COMPLETE);
8376 } else if (res_key != 0) {
8377 /*
8378 * We are not registered and trying to register but
8379 * the register key isn't zero.
8380 */
8381 mtx_unlock(&lun->lun_lock);
8382 free(ctsio->kern_data_ptr, M_CTL);
8383 ctl_set_reservation_conflict(ctsio);
8384 ctl_done((union ctl_io *)ctsio);
8385 return (CTL_RETVAL_COMPLETE);
8386 }
8387 mtx_unlock(&lun->lun_lock);
8388 }
8389
8390 switch (cdb->action & SPRO_ACTION_MASK) {
8391 case SPRO_REGISTER:
8392 case SPRO_REG_IGNO: {
8393 /*
8394 * We don't support any of these options, as we report in
8395 * the read capabilities request (see
8396 * ctl_persistent_reserve_in(), above).
8397 */
8398 if ((param->flags & SPR_SPEC_I_PT)
8399 || (param->flags & SPR_ALL_TG_PT)
8400 || (param->flags & SPR_APTPL)) {
8401 int bit_ptr;
8402
8403 if (param->flags & SPR_APTPL)
8404 bit_ptr = 0;
8405 else if (param->flags & SPR_ALL_TG_PT)
8406 bit_ptr = 2;
8407 else /* SPR_SPEC_I_PT */
8408 bit_ptr = 3;
8409
8410 free(ctsio->kern_data_ptr, M_CTL);
8411 ctl_set_invalid_field(ctsio,
8412 /*sks_valid*/ 1,
8413 /*command*/ 0,
8414 /*field*/ 20,
8415 /*bit_valid*/ 1,
8416 /*bit*/ bit_ptr);
8417 ctl_done((union ctl_io *)ctsio);
8418 return (CTL_RETVAL_COMPLETE);
8419 }
8420
8421 mtx_lock(&lun->lun_lock);
8422
8423 /*
8424 * The initiator wants to clear the
8425 * key/unregister.
8426 */
8427 if (sa_res_key == 0) {
8428 if ((res_key == 0
8429 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8430 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8431 && ctl_get_prkey(lun, residx) == 0)) {
8432 mtx_unlock(&lun->lun_lock);
8433 goto done;
8434 }
8435
8436 ctl_clr_prkey(lun, residx);
8437 lun->pr_key_count--;
8438
8439 if (residx == lun->pr_res_idx) {
8440 lun->flags &= ~CTL_LUN_PR_RESERVED;
8441 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8442
8443 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8444 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8445 lun->pr_key_count) {
8446 /*
8447 * If the reservation is a registrants
8448 * only type we need to generate a UA
8449 * for other registered inits. The
8450 * sense code should be RESERVATIONS
8451 * RELEASED
8452 */
8453
8454 for (i = softc->init_min; i < softc->init_max; i++){
8455 if (ctl_get_prkey(lun, i) == 0)
8456 continue;
8457 ctl_est_ua(lun, i,
8458 CTL_UA_RES_RELEASE);
8459 }
8460 }
8461 lun->pr_res_type = 0;
8462 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8463 if (lun->pr_key_count==0) {
8464 lun->flags &= ~CTL_LUN_PR_RESERVED;
8465 lun->pr_res_type = 0;
8466 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8467 }
8468 }
8469 lun->pr_generation++;
8470 mtx_unlock(&lun->lun_lock);
8471
8472 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8473 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8474 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8475 persis_io.pr.pr_info.residx = residx;
8476 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8477 sizeof(persis_io.pr), M_WAITOK);
8478 } else /* sa_res_key != 0 */ {
8479 /*
8480 * If we aren't registered currently then increment
8481 * the key count and set the registered flag.
8482 */
8483 ctl_alloc_prkey(lun, residx);
8484 if (ctl_get_prkey(lun, residx) == 0)
8485 lun->pr_key_count++;
8486 ctl_set_prkey(lun, residx, sa_res_key);
8487 lun->pr_generation++;
8488 mtx_unlock(&lun->lun_lock);
8489
8490 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8491 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8492 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8493 persis_io.pr.pr_info.residx = residx;
8494 memcpy(persis_io.pr.pr_info.sa_res_key,
8495 param->serv_act_res_key,
8496 sizeof(param->serv_act_res_key));
8497 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8498 sizeof(persis_io.pr), M_WAITOK);
8499 }
8500
8501 break;
8502 }
8503 case SPRO_RESERVE:
8504 mtx_lock(&lun->lun_lock);
8505 if (lun->flags & CTL_LUN_PR_RESERVED) {
8506 /*
8507 * if this isn't the reservation holder and it's
8508 * not a "all registrants" type or if the type is
8509 * different then we have a conflict
8510 */
8511 if ((lun->pr_res_idx != residx
8512 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8513 || lun->pr_res_type != type) {
8514 mtx_unlock(&lun->lun_lock);
8515 free(ctsio->kern_data_ptr, M_CTL);
8516 ctl_set_reservation_conflict(ctsio);
8517 ctl_done((union ctl_io *)ctsio);
8518 return (CTL_RETVAL_COMPLETE);
8519 }
8520 mtx_unlock(&lun->lun_lock);
8521 } else /* create a reservation */ {
8522 /*
8523 * If it's not an "all registrants" type record
8524 * reservation holder
8525 */
8526 if (type != SPR_TYPE_WR_EX_AR
8527 && type != SPR_TYPE_EX_AC_AR)
8528 lun->pr_res_idx = residx; /* Res holder */
8529 else
8530 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8531
8532 lun->flags |= CTL_LUN_PR_RESERVED;
8533 lun->pr_res_type = type;
8534
8535 mtx_unlock(&lun->lun_lock);
8536
8537 /* send msg to other side */
8538 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8539 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8540 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8541 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8542 persis_io.pr.pr_info.res_type = type;
8543 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8544 sizeof(persis_io.pr), M_WAITOK);
8545 }
8546 break;
8547
8548 case SPRO_RELEASE:
8549 mtx_lock(&lun->lun_lock);
8550 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8551 /* No reservation exists return good status */
8552 mtx_unlock(&lun->lun_lock);
8553 goto done;
8554 }
8555 /*
8556 * Is this nexus a reservation holder?
8557 */
8558 if (lun->pr_res_idx != residx
8559 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8560 /*
8561 * not a res holder return good status but
8562 * do nothing
8563 */
8564 mtx_unlock(&lun->lun_lock);
8565 goto done;
8566 }
8567
8568 if (lun->pr_res_type != type) {
8569 mtx_unlock(&lun->lun_lock);
8570 free(ctsio->kern_data_ptr, M_CTL);
8571 ctl_set_illegal_pr_release(ctsio);
8572 ctl_done((union ctl_io *)ctsio);
8573 return (CTL_RETVAL_COMPLETE);
8574 }
8575
8576 /* okay to release */
8577 lun->flags &= ~CTL_LUN_PR_RESERVED;
8578 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8579 lun->pr_res_type = 0;
8580
8581 /*
8582 * If this isn't an exclusive access reservation and NUAR
8583 * is not set, generate UA for all other registrants.
8584 */
8585 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX &&
8586 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8587 for (i = softc->init_min; i < softc->init_max; i++) {
8588 if (i == residx || ctl_get_prkey(lun, i) == 0)
8589 continue;
8590 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8591 }
8592 }
8593 mtx_unlock(&lun->lun_lock);
8594
8595 /* Send msg to other side */
8596 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8597 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8598 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8599 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8600 sizeof(persis_io.pr), M_WAITOK);
8601 break;
8602
8603 case SPRO_CLEAR:
8604 /* send msg to other side */
8605
8606 mtx_lock(&lun->lun_lock);
8607 lun->flags &= ~CTL_LUN_PR_RESERVED;
8608 lun->pr_res_type = 0;
8609 lun->pr_key_count = 0;
8610 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8611
8612 ctl_clr_prkey(lun, residx);
8613 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8614 if (ctl_get_prkey(lun, i) != 0) {
8615 ctl_clr_prkey(lun, i);
8616 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8617 }
8618 lun->pr_generation++;
8619 mtx_unlock(&lun->lun_lock);
8620
8621 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8622 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8623 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8624 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8625 sizeof(persis_io.pr), M_WAITOK);
8626 break;
8627
8628 case SPRO_PREEMPT:
8629 case SPRO_PRE_ABO: {
8630 int nretval;
8631
8632 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8633 residx, ctsio, cdb, param);
8634 if (nretval != 0)
8635 return (CTL_RETVAL_COMPLETE);
8636 break;
8637 }
8638 default:
8639 panic("%s: Invalid PR type %#x", __func__, cdb->action);
8640 }
8641
8642 done:
8643 free(ctsio->kern_data_ptr, M_CTL);
8644 ctl_set_success(ctsio);
8645 ctl_done((union ctl_io *)ctsio);
8646
8647 return (retval);
8648 }
8649
8650 /*
8651 * This routine is for handling a message from the other SC pertaining to
8652 * persistent reserve out. All the error checking will have been done
8653 * so only performing the action need be done here to keep the two
8654 * in sync.
8655 */
8656 static void
ctl_hndl_per_res_out_on_other_sc(union ctl_io * io)8657 ctl_hndl_per_res_out_on_other_sc(union ctl_io *io)
8658 {
8659 struct ctl_softc *softc = CTL_SOFTC(io);
8660 union ctl_ha_msg *msg = (union ctl_ha_msg *)&io->presio.pr_msg;
8661 struct ctl_lun *lun;
8662 int i;
8663 uint32_t residx, targ_lun;
8664
8665 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8666 mtx_lock(&softc->ctl_lock);
8667 if (targ_lun >= ctl_max_luns ||
8668 (lun = softc->ctl_luns[targ_lun]) == NULL) {
8669 mtx_unlock(&softc->ctl_lock);
8670 return;
8671 }
8672 mtx_lock(&lun->lun_lock);
8673 mtx_unlock(&softc->ctl_lock);
8674 if (lun->flags & CTL_LUN_DISABLED) {
8675 mtx_unlock(&lun->lun_lock);
8676 return;
8677 }
8678 residx = ctl_get_initindex(&msg->hdr.nexus);
8679 switch(msg->pr.pr_info.action) {
8680 case CTL_PR_REG_KEY:
8681 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8682 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8683 lun->pr_key_count++;
8684 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8685 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8686 lun->pr_generation++;
8687 break;
8688
8689 case CTL_PR_UNREG_KEY:
8690 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8691 lun->pr_key_count--;
8692
8693 /* XXX Need to see if the reservation has been released */
8694 /* if so do we need to generate UA? */
8695 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8696 lun->flags &= ~CTL_LUN_PR_RESERVED;
8697 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8698
8699 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8700 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8701 lun->pr_key_count) {
8702 /*
8703 * If the reservation is a registrants
8704 * only type we need to generate a UA
8705 * for other registered inits. The
8706 * sense code should be RESERVATIONS
8707 * RELEASED
8708 */
8709
8710 for (i = softc->init_min; i < softc->init_max; i++) {
8711 if (ctl_get_prkey(lun, i) == 0)
8712 continue;
8713
8714 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8715 }
8716 }
8717 lun->pr_res_type = 0;
8718 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8719 if (lun->pr_key_count==0) {
8720 lun->flags &= ~CTL_LUN_PR_RESERVED;
8721 lun->pr_res_type = 0;
8722 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8723 }
8724 }
8725 lun->pr_generation++;
8726 break;
8727
8728 case CTL_PR_RESERVE:
8729 lun->flags |= CTL_LUN_PR_RESERVED;
8730 lun->pr_res_type = msg->pr.pr_info.res_type;
8731 lun->pr_res_idx = msg->pr.pr_info.residx;
8732
8733 break;
8734
8735 case CTL_PR_RELEASE:
8736 /*
8737 * If this isn't an exclusive access reservation and NUAR
8738 * is not set, generate UA for all other registrants.
8739 */
8740 if (lun->pr_res_type != SPR_TYPE_EX_AC &&
8741 lun->pr_res_type != SPR_TYPE_WR_EX &&
8742 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8743 for (i = softc->init_min; i < softc->init_max; i++) {
8744 if (i == residx || ctl_get_prkey(lun, i) == 0)
8745 continue;
8746 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8747 }
8748 }
8749
8750 lun->flags &= ~CTL_LUN_PR_RESERVED;
8751 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8752 lun->pr_res_type = 0;
8753 break;
8754
8755 case CTL_PR_PREEMPT:
8756 ctl_pro_preempt_other(lun, msg);
8757 break;
8758 case CTL_PR_CLEAR:
8759 lun->flags &= ~CTL_LUN_PR_RESERVED;
8760 lun->pr_res_type = 0;
8761 lun->pr_key_count = 0;
8762 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8763
8764 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8765 if (ctl_get_prkey(lun, i) == 0)
8766 continue;
8767 ctl_clr_prkey(lun, i);
8768 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8769 }
8770 lun->pr_generation++;
8771 break;
8772 }
8773
8774 mtx_unlock(&lun->lun_lock);
8775 }
8776
8777 int
ctl_read_write(struct ctl_scsiio * ctsio)8778 ctl_read_write(struct ctl_scsiio *ctsio)
8779 {
8780 struct ctl_lun *lun = CTL_LUN(ctsio);
8781 struct ctl_lba_len_flags *lbalen;
8782 uint64_t lba;
8783 uint32_t num_blocks;
8784 int flags, retval;
8785 int isread;
8786
8787 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8788
8789 flags = 0;
8790 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8791 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8792 switch (ctsio->cdb[0]) {
8793 case READ_6:
8794 case WRITE_6: {
8795 struct scsi_rw_6 *cdb;
8796
8797 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8798
8799 lba = scsi_3btoul(cdb->addr);
8800 /* only 5 bits are valid in the most significant address byte */
8801 lba &= 0x1fffff;
8802 num_blocks = cdb->length;
8803 /*
8804 * This is correct according to SBC-2.
8805 */
8806 if (num_blocks == 0)
8807 num_blocks = 256;
8808 break;
8809 }
8810 case READ_10:
8811 case WRITE_10: {
8812 struct scsi_rw_10 *cdb;
8813
8814 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8815 if (cdb->byte2 & SRW10_FUA)
8816 flags |= CTL_LLF_FUA;
8817 if (cdb->byte2 & SRW10_DPO)
8818 flags |= CTL_LLF_DPO;
8819 lba = scsi_4btoul(cdb->addr);
8820 num_blocks = scsi_2btoul(cdb->length);
8821 break;
8822 }
8823 case WRITE_VERIFY_10: {
8824 struct scsi_write_verify_10 *cdb;
8825
8826 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8827 flags |= CTL_LLF_FUA;
8828 if (cdb->byte2 & SWV_DPO)
8829 flags |= CTL_LLF_DPO;
8830 lba = scsi_4btoul(cdb->addr);
8831 num_blocks = scsi_2btoul(cdb->length);
8832 break;
8833 }
8834 case READ_12:
8835 case WRITE_12: {
8836 struct scsi_rw_12 *cdb;
8837
8838 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8839 if (cdb->byte2 & SRW12_FUA)
8840 flags |= CTL_LLF_FUA;
8841 if (cdb->byte2 & SRW12_DPO)
8842 flags |= CTL_LLF_DPO;
8843 lba = scsi_4btoul(cdb->addr);
8844 num_blocks = scsi_4btoul(cdb->length);
8845 break;
8846 }
8847 case WRITE_VERIFY_12: {
8848 struct scsi_write_verify_12 *cdb;
8849
8850 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8851 flags |= CTL_LLF_FUA;
8852 if (cdb->byte2 & SWV_DPO)
8853 flags |= CTL_LLF_DPO;
8854 lba = scsi_4btoul(cdb->addr);
8855 num_blocks = scsi_4btoul(cdb->length);
8856 break;
8857 }
8858 case READ_16:
8859 case WRITE_16: {
8860 struct scsi_rw_16 *cdb;
8861
8862 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8863 if (cdb->byte2 & SRW12_FUA)
8864 flags |= CTL_LLF_FUA;
8865 if (cdb->byte2 & SRW12_DPO)
8866 flags |= CTL_LLF_DPO;
8867 lba = scsi_8btou64(cdb->addr);
8868 num_blocks = scsi_4btoul(cdb->length);
8869 break;
8870 }
8871 case WRITE_ATOMIC_16: {
8872 struct scsi_write_atomic_16 *cdb;
8873
8874 if (lun->be_lun->atomicblock == 0) {
8875 ctl_set_invalid_opcode(ctsio);
8876 ctl_done((union ctl_io *)ctsio);
8877 return (CTL_RETVAL_COMPLETE);
8878 }
8879
8880 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb;
8881 if (cdb->byte2 & SRW12_FUA)
8882 flags |= CTL_LLF_FUA;
8883 if (cdb->byte2 & SRW12_DPO)
8884 flags |= CTL_LLF_DPO;
8885 lba = scsi_8btou64(cdb->addr);
8886 num_blocks = scsi_2btoul(cdb->length);
8887 if (num_blocks > lun->be_lun->atomicblock) {
8888 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8889 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8890 /*bit*/ 0);
8891 ctl_done((union ctl_io *)ctsio);
8892 return (CTL_RETVAL_COMPLETE);
8893 }
8894 break;
8895 }
8896 case WRITE_VERIFY_16: {
8897 struct scsi_write_verify_16 *cdb;
8898
8899 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8900 flags |= CTL_LLF_FUA;
8901 if (cdb->byte2 & SWV_DPO)
8902 flags |= CTL_LLF_DPO;
8903 lba = scsi_8btou64(cdb->addr);
8904 num_blocks = scsi_4btoul(cdb->length);
8905 break;
8906 }
8907 default:
8908 /*
8909 * We got a command we don't support. This shouldn't
8910 * happen, commands should be filtered out above us.
8911 */
8912 ctl_set_invalid_opcode(ctsio);
8913 ctl_done((union ctl_io *)ctsio);
8914
8915 return (CTL_RETVAL_COMPLETE);
8916 break; /* NOTREACHED */
8917 }
8918
8919 /*
8920 * The first check is to make sure we're in bounds, the second
8921 * check is to catch wrap-around problems. If the lba + num blocks
8922 * is less than the lba, then we've wrapped around and the block
8923 * range is invalid anyway.
8924 */
8925 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8926 || ((lba + num_blocks) < lba)) {
8927 ctl_set_lba_out_of_range(ctsio,
8928 MAX(lba, lun->be_lun->maxlba + 1));
8929 ctl_done((union ctl_io *)ctsio);
8930 return (CTL_RETVAL_COMPLETE);
8931 }
8932
8933 /*
8934 * According to SBC-3, a transfer length of 0 is not an error.
8935 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8936 * translates to 256 blocks for those commands.
8937 */
8938 if (num_blocks == 0) {
8939 ctl_set_success(ctsio);
8940 ctl_done((union ctl_io *)ctsio);
8941 return (CTL_RETVAL_COMPLETE);
8942 }
8943
8944 /* Set FUA and/or DPO if caches are disabled. */
8945 if (isread) {
8946 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0)
8947 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8948 } else {
8949 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8950 flags |= CTL_LLF_FUA;
8951 }
8952
8953 lbalen = (struct ctl_lba_len_flags *)
8954 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8955 lbalen->lba = lba;
8956 lbalen->len = num_blocks;
8957 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8958
8959 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8960 ctsio->kern_rel_offset = 0;
8961
8962 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8963
8964 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8965 return (retval);
8966 }
8967
8968 static int
ctl_cnw_cont(union ctl_io * io)8969 ctl_cnw_cont(union ctl_io *io)
8970 {
8971 struct ctl_lun *lun = CTL_LUN(io);
8972 struct ctl_scsiio *ctsio;
8973 struct ctl_lba_len_flags *lbalen;
8974 int retval;
8975
8976 ctsio = &io->scsiio;
8977 ctsio->io_hdr.status = CTL_STATUS_NONE;
8978 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8979 lbalen = (struct ctl_lba_len_flags *)
8980 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8981 lbalen->flags &= ~CTL_LLF_COMPARE;
8982 lbalen->flags |= CTL_LLF_WRITE;
8983
8984 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8985 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8986 return (retval);
8987 }
8988
8989 int
ctl_cnw(struct ctl_scsiio * ctsio)8990 ctl_cnw(struct ctl_scsiio *ctsio)
8991 {
8992 struct ctl_lun *lun = CTL_LUN(ctsio);
8993 struct ctl_lba_len_flags *lbalen;
8994 uint64_t lba;
8995 uint32_t num_blocks;
8996 int flags, retval;
8997
8998 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8999
9000 flags = 0;
9001 switch (ctsio->cdb[0]) {
9002 case COMPARE_AND_WRITE: {
9003 struct scsi_compare_and_write *cdb;
9004
9005 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
9006 if (cdb->byte2 & SRW10_FUA)
9007 flags |= CTL_LLF_FUA;
9008 if (cdb->byte2 & SRW10_DPO)
9009 flags |= CTL_LLF_DPO;
9010 lba = scsi_8btou64(cdb->addr);
9011 num_blocks = cdb->length;
9012 break;
9013 }
9014 default:
9015 /*
9016 * We got a command we don't support. This shouldn't
9017 * happen, commands should be filtered out above us.
9018 */
9019 ctl_set_invalid_opcode(ctsio);
9020 ctl_done((union ctl_io *)ctsio);
9021
9022 return (CTL_RETVAL_COMPLETE);
9023 break; /* NOTREACHED */
9024 }
9025
9026 /*
9027 * The first check is to make sure we're in bounds, the second
9028 * check is to catch wrap-around problems. If the lba + num blocks
9029 * is less than the lba, then we've wrapped around and the block
9030 * range is invalid anyway.
9031 */
9032 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9033 || ((lba + num_blocks) < lba)) {
9034 ctl_set_lba_out_of_range(ctsio,
9035 MAX(lba, lun->be_lun->maxlba + 1));
9036 ctl_done((union ctl_io *)ctsio);
9037 return (CTL_RETVAL_COMPLETE);
9038 }
9039
9040 /*
9041 * According to SBC-3, a transfer length of 0 is not an error.
9042 */
9043 if (num_blocks == 0) {
9044 ctl_set_success(ctsio);
9045 ctl_done((union ctl_io *)ctsio);
9046 return (CTL_RETVAL_COMPLETE);
9047 }
9048
9049 /* Set FUA if write cache is disabled. */
9050 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
9051 flags |= CTL_LLF_FUA;
9052
9053 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
9054 ctsio->kern_rel_offset = 0;
9055
9056 /*
9057 * Set the IO_CONT flag, so that if this I/O gets passed to
9058 * ctl_data_submit_done(), it'll get passed back to
9059 * ctl_ctl_cnw_cont() for further processing.
9060 */
9061 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
9062 ctsio->io_cont = ctl_cnw_cont;
9063
9064 lbalen = (struct ctl_lba_len_flags *)
9065 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9066 lbalen->lba = lba;
9067 lbalen->len = num_blocks;
9068 lbalen->flags = CTL_LLF_COMPARE | flags;
9069
9070 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
9071 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9072 return (retval);
9073 }
9074
9075 int
ctl_verify(struct ctl_scsiio * ctsio)9076 ctl_verify(struct ctl_scsiio *ctsio)
9077 {
9078 struct ctl_lun *lun = CTL_LUN(ctsio);
9079 struct ctl_lba_len_flags *lbalen;
9080 uint64_t lba;
9081 uint32_t num_blocks;
9082 int bytchk, flags;
9083 int retval;
9084
9085 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
9086
9087 bytchk = 0;
9088 flags = CTL_LLF_FUA;
9089 switch (ctsio->cdb[0]) {
9090 case VERIFY_10: {
9091 struct scsi_verify_10 *cdb;
9092
9093 cdb = (struct scsi_verify_10 *)ctsio->cdb;
9094 if (cdb->byte2 & SVFY_BYTCHK)
9095 bytchk = 1;
9096 if (cdb->byte2 & SVFY_DPO)
9097 flags |= CTL_LLF_DPO;
9098 lba = scsi_4btoul(cdb->addr);
9099 num_blocks = scsi_2btoul(cdb->length);
9100 break;
9101 }
9102 case VERIFY_12: {
9103 struct scsi_verify_12 *cdb;
9104
9105 cdb = (struct scsi_verify_12 *)ctsio->cdb;
9106 if (cdb->byte2 & SVFY_BYTCHK)
9107 bytchk = 1;
9108 if (cdb->byte2 & SVFY_DPO)
9109 flags |= CTL_LLF_DPO;
9110 lba = scsi_4btoul(cdb->addr);
9111 num_blocks = scsi_4btoul(cdb->length);
9112 break;
9113 }
9114 case VERIFY_16: {
9115 struct scsi_rw_16 *cdb;
9116
9117 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9118 if (cdb->byte2 & SVFY_BYTCHK)
9119 bytchk = 1;
9120 if (cdb->byte2 & SVFY_DPO)
9121 flags |= CTL_LLF_DPO;
9122 lba = scsi_8btou64(cdb->addr);
9123 num_blocks = scsi_4btoul(cdb->length);
9124 break;
9125 }
9126 default:
9127 /*
9128 * We got a command we don't support. This shouldn't
9129 * happen, commands should be filtered out above us.
9130 */
9131 ctl_set_invalid_opcode(ctsio);
9132 ctl_done((union ctl_io *)ctsio);
9133 return (CTL_RETVAL_COMPLETE);
9134 }
9135
9136 /*
9137 * The first check is to make sure we're in bounds, the second
9138 * check is to catch wrap-around problems. If the lba + num blocks
9139 * is less than the lba, then we've wrapped around and the block
9140 * range is invalid anyway.
9141 */
9142 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9143 || ((lba + num_blocks) < lba)) {
9144 ctl_set_lba_out_of_range(ctsio,
9145 MAX(lba, lun->be_lun->maxlba + 1));
9146 ctl_done((union ctl_io *)ctsio);
9147 return (CTL_RETVAL_COMPLETE);
9148 }
9149
9150 /*
9151 * According to SBC-3, a transfer length of 0 is not an error.
9152 */
9153 if (num_blocks == 0) {
9154 ctl_set_success(ctsio);
9155 ctl_done((union ctl_io *)ctsio);
9156 return (CTL_RETVAL_COMPLETE);
9157 }
9158
9159 lbalen = (struct ctl_lba_len_flags *)
9160 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9161 lbalen->lba = lba;
9162 lbalen->len = num_blocks;
9163 if (bytchk) {
9164 lbalen->flags = CTL_LLF_COMPARE | flags;
9165 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9166 } else {
9167 lbalen->flags = CTL_LLF_VERIFY | flags;
9168 ctsio->kern_total_len = 0;
9169 }
9170 ctsio->kern_rel_offset = 0;
9171
9172 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9173 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9174 return (retval);
9175 }
9176
9177 int
ctl_report_luns(struct ctl_scsiio * ctsio)9178 ctl_report_luns(struct ctl_scsiio *ctsio)
9179 {
9180 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9181 struct ctl_port *port = CTL_PORT(ctsio);
9182 struct ctl_lun *lun, *request_lun = CTL_LUN(ctsio);
9183 struct scsi_report_luns *cdb;
9184 struct scsi_report_luns_data *lun_data;
9185 int num_filled, num_luns, num_port_luns, retval;
9186 uint32_t alloc_len, lun_datalen;
9187 uint32_t initidx, targ_lun_id, lun_id;
9188
9189 retval = CTL_RETVAL_COMPLETE;
9190 cdb = (struct scsi_report_luns *)ctsio->cdb;
9191
9192 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9193
9194 num_luns = 0;
9195 num_port_luns = port->lun_map ? port->lun_map_size : ctl_max_luns;
9196 mtx_lock(&softc->ctl_lock);
9197 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) {
9198 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX)
9199 num_luns++;
9200 }
9201 mtx_unlock(&softc->ctl_lock);
9202
9203 switch (cdb->select_report) {
9204 case RPL_REPORT_DEFAULT:
9205 case RPL_REPORT_ALL:
9206 case RPL_REPORT_NONSUBSID:
9207 break;
9208 case RPL_REPORT_WELLKNOWN:
9209 case RPL_REPORT_ADMIN:
9210 case RPL_REPORT_CONGLOM:
9211 num_luns = 0;
9212 break;
9213 default:
9214 ctl_set_invalid_field(ctsio,
9215 /*sks_valid*/ 1,
9216 /*command*/ 1,
9217 /*field*/ 2,
9218 /*bit_valid*/ 0,
9219 /*bit*/ 0);
9220 ctl_done((union ctl_io *)ctsio);
9221 return (retval);
9222 break; /* NOTREACHED */
9223 }
9224
9225 alloc_len = scsi_4btoul(cdb->length);
9226 /*
9227 * The initiator has to allocate at least 16 bytes for this request,
9228 * so he can at least get the header and the first LUN. Otherwise
9229 * we reject the request (per SPC-3 rev 14, section 6.21).
9230 */
9231 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9232 sizeof(struct scsi_report_luns_lundata))) {
9233 ctl_set_invalid_field(ctsio,
9234 /*sks_valid*/ 1,
9235 /*command*/ 1,
9236 /*field*/ 6,
9237 /*bit_valid*/ 0,
9238 /*bit*/ 0);
9239 ctl_done((union ctl_io *)ctsio);
9240 return (retval);
9241 }
9242
9243 lun_datalen = sizeof(*lun_data) +
9244 (num_luns * sizeof(struct scsi_report_luns_lundata));
9245
9246 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9247 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9248 ctsio->kern_sg_entries = 0;
9249
9250 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9251
9252 mtx_lock(&softc->ctl_lock);
9253 for (targ_lun_id = 0, num_filled = 0;
9254 targ_lun_id < num_port_luns && num_filled < num_luns;
9255 targ_lun_id++) {
9256 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9257 if (lun_id == UINT32_MAX)
9258 continue;
9259 lun = softc->ctl_luns[lun_id];
9260 if (lun == NULL)
9261 continue;
9262
9263 be64enc(lun_data->luns[num_filled++].lundata,
9264 ctl_encode_lun(targ_lun_id));
9265
9266 /*
9267 * According to SPC-3, rev 14 section 6.21:
9268 *
9269 * "The execution of a REPORT LUNS command to any valid and
9270 * installed logical unit shall clear the REPORTED LUNS DATA
9271 * HAS CHANGED unit attention condition for all logical
9272 * units of that target with respect to the requesting
9273 * initiator. A valid and installed logical unit is one
9274 * having a PERIPHERAL QUALIFIER of 000b in the standard
9275 * INQUIRY data (see 6.4.2)."
9276 *
9277 * If request_lun is NULL, the LUN this report luns command
9278 * was issued to is either disabled or doesn't exist. In that
9279 * case, we shouldn't clear any pending lun change unit
9280 * attention.
9281 */
9282 if (request_lun != NULL) {
9283 mtx_lock(&lun->lun_lock);
9284 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9285 mtx_unlock(&lun->lun_lock);
9286 }
9287 }
9288 mtx_unlock(&softc->ctl_lock);
9289
9290 /*
9291 * It's quite possible that we've returned fewer LUNs than we allocated
9292 * space for. Trim it.
9293 */
9294 lun_datalen = sizeof(*lun_data) +
9295 (num_filled * sizeof(struct scsi_report_luns_lundata));
9296 ctsio->kern_rel_offset = 0;
9297 ctsio->kern_sg_entries = 0;
9298 ctsio->kern_data_len = min(lun_datalen, alloc_len);
9299 ctsio->kern_total_len = ctsio->kern_data_len;
9300
9301 /*
9302 * We set this to the actual data length, regardless of how much
9303 * space we actually have to return results. If the user looks at
9304 * this value, he'll know whether or not he allocated enough space
9305 * and reissue the command if necessary. We don't support well
9306 * known logical units, so if the user asks for that, return none.
9307 */
9308 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9309
9310 /*
9311 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9312 * this request.
9313 */
9314 ctl_set_success(ctsio);
9315 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9316 ctsio->be_move_done = ctl_config_move_done;
9317 ctl_datamove((union ctl_io *)ctsio);
9318 return (retval);
9319 }
9320
9321 int
ctl_request_sense(struct ctl_scsiio * ctsio)9322 ctl_request_sense(struct ctl_scsiio *ctsio)
9323 {
9324 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9325 struct ctl_lun *lun = CTL_LUN(ctsio);
9326 struct scsi_request_sense *cdb;
9327 struct scsi_sense_data *sense_ptr, *ps;
9328 uint32_t initidx;
9329 int have_error;
9330 u_int sense_len = SSD_FULL_SIZE;
9331 scsi_sense_data_type sense_format;
9332 ctl_ua_type ua_type;
9333 uint8_t asc = 0, ascq = 0;
9334
9335 cdb = (struct scsi_request_sense *)ctsio->cdb;
9336
9337 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9338
9339 /*
9340 * Determine which sense format the user wants.
9341 */
9342 if (cdb->byte2 & SRS_DESC)
9343 sense_format = SSD_TYPE_DESC;
9344 else
9345 sense_format = SSD_TYPE_FIXED;
9346
9347 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9348 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9349 ctsio->kern_sg_entries = 0;
9350 ctsio->kern_rel_offset = 0;
9351 ctsio->kern_data_len = ctsio->kern_total_len =
9352 MIN(cdb->length, sizeof(*sense_ptr));
9353
9354 /*
9355 * If we don't have a LUN, we don't have any pending sense.
9356 */
9357 if (lun == NULL ||
9358 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
9359 softc->ha_link < CTL_HA_LINK_UNKNOWN)) {
9360 /* "Logical unit not supported" */
9361 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format,
9362 /*current_error*/ 1,
9363 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
9364 /*asc*/ 0x25,
9365 /*ascq*/ 0x00,
9366 SSD_ELEM_NONE);
9367 goto send;
9368 }
9369
9370 have_error = 0;
9371 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9372 /*
9373 * Check for pending sense, and then for pending unit attentions.
9374 * Pending sense gets returned first, then pending unit attentions.
9375 */
9376 mtx_lock(&lun->lun_lock);
9377 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
9378 if (ps != NULL)
9379 ps += initidx % CTL_MAX_INIT_PER_PORT;
9380 if (ps != NULL && ps->error_code != 0) {
9381 scsi_sense_data_type stored_format;
9382
9383 /*
9384 * Check to see which sense format was used for the stored
9385 * sense data.
9386 */
9387 stored_format = scsi_sense_type(ps);
9388
9389 /*
9390 * If the user requested a different sense format than the
9391 * one we stored, then we need to convert it to the other
9392 * format. If we're going from descriptor to fixed format
9393 * sense data, we may lose things in translation, depending
9394 * on what options were used.
9395 *
9396 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9397 * for some reason we'll just copy it out as-is.
9398 */
9399 if ((stored_format == SSD_TYPE_FIXED)
9400 && (sense_format == SSD_TYPE_DESC))
9401 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9402 ps, (struct scsi_sense_data_desc *)sense_ptr);
9403 else if ((stored_format == SSD_TYPE_DESC)
9404 && (sense_format == SSD_TYPE_FIXED))
9405 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9406 ps, (struct scsi_sense_data_fixed *)sense_ptr);
9407 else
9408 memcpy(sense_ptr, ps, sizeof(*sense_ptr));
9409
9410 ps->error_code = 0;
9411 have_error = 1;
9412 } else {
9413 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len,
9414 sense_format);
9415 if (ua_type != CTL_UA_NONE)
9416 have_error = 1;
9417 }
9418 if (have_error == 0) {
9419 /*
9420 * Report informational exception if have one and allowed.
9421 */
9422 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) {
9423 asc = lun->ie_asc;
9424 ascq = lun->ie_ascq;
9425 }
9426 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format,
9427 /*current_error*/ 1,
9428 /*sense_key*/ SSD_KEY_NO_SENSE,
9429 /*asc*/ asc,
9430 /*ascq*/ ascq,
9431 SSD_ELEM_NONE);
9432 }
9433 mtx_unlock(&lun->lun_lock);
9434
9435 send:
9436 /*
9437 * We report the SCSI status as OK, since the status of the command
9438 * itself is OK. We're reporting sense as parameter data.
9439 */
9440 ctl_set_success(ctsio);
9441 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9442 ctsio->be_move_done = ctl_config_move_done;
9443 ctl_datamove((union ctl_io *)ctsio);
9444 return (CTL_RETVAL_COMPLETE);
9445 }
9446
9447 int
ctl_tur(struct ctl_scsiio * ctsio)9448 ctl_tur(struct ctl_scsiio *ctsio)
9449 {
9450
9451 CTL_DEBUG_PRINT(("ctl_tur\n"));
9452
9453 ctl_set_success(ctsio);
9454 ctl_done((union ctl_io *)ctsio);
9455
9456 return (CTL_RETVAL_COMPLETE);
9457 }
9458
9459 /*
9460 * SCSI VPD page 0x00, the Supported VPD Pages page.
9461 */
9462 static int
ctl_inquiry_evpd_supported(struct ctl_scsiio * ctsio,int alloc_len)9463 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9464 {
9465 struct ctl_lun *lun = CTL_LUN(ctsio);
9466 struct scsi_vpd_supported_pages *pages;
9467 int sup_page_size;
9468 int p;
9469
9470 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9471 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9472 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9473 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9474 ctsio->kern_rel_offset = 0;
9475 ctsio->kern_sg_entries = 0;
9476 ctsio->kern_data_len = min(sup_page_size, alloc_len);
9477 ctsio->kern_total_len = ctsio->kern_data_len;
9478
9479 /*
9480 * The control device is always connected. The disk device, on the
9481 * other hand, may not be online all the time. Need to change this
9482 * to figure out whether the disk device is actually online or not.
9483 */
9484 if (lun != NULL)
9485 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9486 lun->be_lun->lun_type;
9487 else
9488 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9489
9490 p = 0;
9491 /* Supported VPD pages */
9492 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9493 /* Serial Number */
9494 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9495 /* Device Identification */
9496 pages->page_list[p++] = SVPD_DEVICE_ID;
9497 /* Extended INQUIRY Data */
9498 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9499 /* Mode Page Policy */
9500 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9501 /* SCSI Ports */
9502 pages->page_list[p++] = SVPD_SCSI_PORTS;
9503 /* Third-party Copy */
9504 pages->page_list[p++] = SVPD_SCSI_TPC;
9505 /* SCSI Feature Sets */
9506 pages->page_list[p++] = SVPD_SCSI_SFS;
9507 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9508 /* Block limits */
9509 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9510 /* Block Device Characteristics */
9511 pages->page_list[p++] = SVPD_BDC;
9512 /* Logical Block Provisioning */
9513 pages->page_list[p++] = SVPD_LBP;
9514 }
9515 pages->length = p;
9516
9517 ctl_set_success(ctsio);
9518 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9519 ctsio->be_move_done = ctl_config_move_done;
9520 ctl_datamove((union ctl_io *)ctsio);
9521 return (CTL_RETVAL_COMPLETE);
9522 }
9523
9524 /*
9525 * SCSI VPD page 0x80, the Unit Serial Number page.
9526 */
9527 static int
ctl_inquiry_evpd_serial(struct ctl_scsiio * ctsio,int alloc_len)9528 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9529 {
9530 struct ctl_lun *lun = CTL_LUN(ctsio);
9531 struct scsi_vpd_unit_serial_number *sn_ptr;
9532 int data_len;
9533
9534 data_len = 4 + CTL_SN_LEN;
9535 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9536 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9537 ctsio->kern_rel_offset = 0;
9538 ctsio->kern_sg_entries = 0;
9539 ctsio->kern_data_len = min(data_len, alloc_len);
9540 ctsio->kern_total_len = ctsio->kern_data_len;
9541
9542 /*
9543 * The control device is always connected. The disk device, on the
9544 * other hand, may not be online all the time. Need to change this
9545 * to figure out whether the disk device is actually online or not.
9546 */
9547 if (lun != NULL)
9548 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9549 lun->be_lun->lun_type;
9550 else
9551 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9552
9553 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9554 sn_ptr->length = CTL_SN_LEN;
9555 /*
9556 * If we don't have a LUN, we just leave the serial number as
9557 * all spaces.
9558 */
9559 if (lun != NULL) {
9560 strncpy((char *)sn_ptr->serial_num,
9561 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9562 } else
9563 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9564
9565 ctl_set_success(ctsio);
9566 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9567 ctsio->be_move_done = ctl_config_move_done;
9568 ctl_datamove((union ctl_io *)ctsio);
9569 return (CTL_RETVAL_COMPLETE);
9570 }
9571
9572 /*
9573 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9574 */
9575 static int
ctl_inquiry_evpd_eid(struct ctl_scsiio * ctsio,int alloc_len)9576 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9577 {
9578 struct ctl_lun *lun = CTL_LUN(ctsio);
9579 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9580 int data_len;
9581
9582 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9583 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9584 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9585 ctsio->kern_sg_entries = 0;
9586 ctsio->kern_rel_offset = 0;
9587 ctsio->kern_data_len = min(data_len, alloc_len);
9588 ctsio->kern_total_len = ctsio->kern_data_len;
9589
9590 /*
9591 * The control device is always connected. The disk device, on the
9592 * other hand, may not be online all the time.
9593 */
9594 if (lun != NULL)
9595 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9596 lun->be_lun->lun_type;
9597 else
9598 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9599 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9600 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9601 /*
9602 * We support head of queue, ordered and simple tags.
9603 */
9604 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9605 /*
9606 * Volatile cache supported.
9607 */
9608 eid_ptr->flags3 = SVPD_EID_V_SUP;
9609
9610 /*
9611 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9612 * attention for a particular IT nexus on all LUNs once we report
9613 * it to that nexus once. This bit is required as of SPC-4.
9614 */
9615 eid_ptr->flags4 = SVPD_EID_LUICLR;
9616
9617 /*
9618 * We support revert to defaults (RTD) bit in MODE SELECT.
9619 */
9620 eid_ptr->flags5 = SVPD_EID_RTD_SUP;
9621
9622 /*
9623 * XXX KDM in order to correctly answer this, we would need
9624 * information from the SIM to determine how much sense data it
9625 * can send. So this would really be a path inquiry field, most
9626 * likely. This can be set to a maximum of 252 according to SPC-4,
9627 * but the hardware may or may not be able to support that much.
9628 * 0 just means that the maximum sense data length is not reported.
9629 */
9630 eid_ptr->max_sense_length = 0;
9631
9632 ctl_set_success(ctsio);
9633 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9634 ctsio->be_move_done = ctl_config_move_done;
9635 ctl_datamove((union ctl_io *)ctsio);
9636 return (CTL_RETVAL_COMPLETE);
9637 }
9638
9639 static int
ctl_inquiry_evpd_mpp(struct ctl_scsiio * ctsio,int alloc_len)9640 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9641 {
9642 struct ctl_lun *lun = CTL_LUN(ctsio);
9643 struct scsi_vpd_mode_page_policy *mpp_ptr;
9644 int data_len;
9645
9646 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9647 sizeof(struct scsi_vpd_mode_page_policy_descr);
9648
9649 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9650 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9651 ctsio->kern_rel_offset = 0;
9652 ctsio->kern_sg_entries = 0;
9653 ctsio->kern_data_len = min(data_len, alloc_len);
9654 ctsio->kern_total_len = ctsio->kern_data_len;
9655
9656 /*
9657 * The control device is always connected. The disk device, on the
9658 * other hand, may not be online all the time.
9659 */
9660 if (lun != NULL)
9661 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9662 lun->be_lun->lun_type;
9663 else
9664 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9665 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9666 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9667 mpp_ptr->descr[0].page_code = 0x3f;
9668 mpp_ptr->descr[0].subpage_code = 0xff;
9669 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9670
9671 ctl_set_success(ctsio);
9672 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9673 ctsio->be_move_done = ctl_config_move_done;
9674 ctl_datamove((union ctl_io *)ctsio);
9675 return (CTL_RETVAL_COMPLETE);
9676 }
9677
9678 /*
9679 * SCSI VPD page 0x83, the Device Identification page.
9680 */
9681 static int
ctl_inquiry_evpd_devid(struct ctl_scsiio * ctsio,int alloc_len)9682 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9683 {
9684 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9685 struct ctl_port *port = CTL_PORT(ctsio);
9686 struct ctl_lun *lun = CTL_LUN(ctsio);
9687 struct scsi_vpd_device_id *devid_ptr;
9688 struct scsi_vpd_id_descriptor *desc;
9689 int data_len, g;
9690 uint8_t proto;
9691
9692 data_len = sizeof(struct scsi_vpd_device_id) +
9693 sizeof(struct scsi_vpd_id_descriptor) +
9694 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9695 sizeof(struct scsi_vpd_id_descriptor) +
9696 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9697 if (lun && lun->lun_devid)
9698 data_len += lun->lun_devid->len;
9699 if (port && port->port_devid)
9700 data_len += port->port_devid->len;
9701 if (port && port->target_devid)
9702 data_len += port->target_devid->len;
9703
9704 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9705 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9706 ctsio->kern_sg_entries = 0;
9707 ctsio->kern_rel_offset = 0;
9708 ctsio->kern_sg_entries = 0;
9709 ctsio->kern_data_len = min(data_len, alloc_len);
9710 ctsio->kern_total_len = ctsio->kern_data_len;
9711
9712 /*
9713 * The control device is always connected. The disk device, on the
9714 * other hand, may not be online all the time.
9715 */
9716 if (lun != NULL)
9717 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9718 lun->be_lun->lun_type;
9719 else
9720 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9721 devid_ptr->page_code = SVPD_DEVICE_ID;
9722 scsi_ulto2b(data_len - 4, devid_ptr->length);
9723
9724 if (port && port->port_type == CTL_PORT_FC)
9725 proto = SCSI_PROTO_FC << 4;
9726 else if (port && port->port_type == CTL_PORT_SAS)
9727 proto = SCSI_PROTO_SAS << 4;
9728 else if (port && port->port_type == CTL_PORT_ISCSI)
9729 proto = SCSI_PROTO_ISCSI << 4;
9730 else
9731 proto = SCSI_PROTO_SPI << 4;
9732 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9733
9734 /*
9735 * We're using a LUN association here. i.e., this device ID is a
9736 * per-LUN identifier.
9737 */
9738 if (lun && lun->lun_devid) {
9739 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9740 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9741 lun->lun_devid->len);
9742 }
9743
9744 /*
9745 * This is for the WWPN which is a port association.
9746 */
9747 if (port && port->port_devid) {
9748 memcpy(desc, port->port_devid->data, port->port_devid->len);
9749 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9750 port->port_devid->len);
9751 }
9752
9753 /*
9754 * This is for the Relative Target Port(type 4h) identifier
9755 */
9756 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9757 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9758 SVPD_ID_TYPE_RELTARG;
9759 desc->length = 4;
9760 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9761 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9762 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9763
9764 /*
9765 * This is for the Target Port Group(type 5h) identifier
9766 */
9767 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9768 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9769 SVPD_ID_TYPE_TPORTGRP;
9770 desc->length = 4;
9771 if (softc->is_single ||
9772 (port && port->status & CTL_PORT_STATUS_HA_SHARED))
9773 g = 1;
9774 else
9775 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt;
9776 scsi_ulto2b(g, &desc->identifier[2]);
9777 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9778 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9779
9780 /*
9781 * This is for the Target identifier
9782 */
9783 if (port && port->target_devid) {
9784 memcpy(desc, port->target_devid->data, port->target_devid->len);
9785 }
9786
9787 ctl_set_success(ctsio);
9788 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9789 ctsio->be_move_done = ctl_config_move_done;
9790 ctl_datamove((union ctl_io *)ctsio);
9791 return (CTL_RETVAL_COMPLETE);
9792 }
9793
9794 static int
ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio * ctsio,int alloc_len)9795 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9796 {
9797 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9798 struct ctl_lun *lun = CTL_LUN(ctsio);
9799 struct scsi_vpd_scsi_ports *sp;
9800 struct scsi_vpd_port_designation *pd;
9801 struct scsi_vpd_port_designation_cont *pdc;
9802 struct ctl_port *port;
9803 int data_len, num_target_ports, iid_len, id_len;
9804
9805 num_target_ports = 0;
9806 iid_len = 0;
9807 id_len = 0;
9808 mtx_lock(&softc->ctl_lock);
9809 STAILQ_FOREACH(port, &softc->port_list, links) {
9810 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9811 continue;
9812 if (lun != NULL &&
9813 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9814 continue;
9815 num_target_ports++;
9816 if (port->init_devid)
9817 iid_len += port->init_devid->len;
9818 if (port->port_devid)
9819 id_len += port->port_devid->len;
9820 }
9821 mtx_unlock(&softc->ctl_lock);
9822
9823 data_len = sizeof(struct scsi_vpd_scsi_ports) +
9824 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9825 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9826 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9827 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9828 ctsio->kern_sg_entries = 0;
9829 ctsio->kern_rel_offset = 0;
9830 ctsio->kern_sg_entries = 0;
9831 ctsio->kern_data_len = min(data_len, alloc_len);
9832 ctsio->kern_total_len = ctsio->kern_data_len;
9833
9834 /*
9835 * The control device is always connected. The disk device, on the
9836 * other hand, may not be online all the time. Need to change this
9837 * to figure out whether the disk device is actually online or not.
9838 */
9839 if (lun != NULL)
9840 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9841 lun->be_lun->lun_type;
9842 else
9843 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9844
9845 sp->page_code = SVPD_SCSI_PORTS;
9846 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9847 sp->page_length);
9848 pd = &sp->design[0];
9849
9850 mtx_lock(&softc->ctl_lock);
9851 STAILQ_FOREACH(port, &softc->port_list, links) {
9852 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9853 continue;
9854 if (lun != NULL &&
9855 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9856 continue;
9857 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9858 if (port->init_devid) {
9859 iid_len = port->init_devid->len;
9860 memcpy(pd->initiator_transportid,
9861 port->init_devid->data, port->init_devid->len);
9862 } else
9863 iid_len = 0;
9864 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9865 pdc = (struct scsi_vpd_port_designation_cont *)
9866 (&pd->initiator_transportid[iid_len]);
9867 if (port->port_devid) {
9868 id_len = port->port_devid->len;
9869 memcpy(pdc->target_port_descriptors,
9870 port->port_devid->data, port->port_devid->len);
9871 } else
9872 id_len = 0;
9873 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9874 pd = (struct scsi_vpd_port_designation *)
9875 ((uint8_t *)pdc->target_port_descriptors + id_len);
9876 }
9877 mtx_unlock(&softc->ctl_lock);
9878
9879 ctl_set_success(ctsio);
9880 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9881 ctsio->be_move_done = ctl_config_move_done;
9882 ctl_datamove((union ctl_io *)ctsio);
9883 return (CTL_RETVAL_COMPLETE);
9884 }
9885
9886 static int
ctl_inquiry_evpd_sfs(struct ctl_scsiio * ctsio,int alloc_len)9887 ctl_inquiry_evpd_sfs(struct ctl_scsiio *ctsio, int alloc_len)
9888 {
9889 struct ctl_lun *lun = CTL_LUN(ctsio);
9890 struct scsi_vpd_sfs *sfs_ptr;
9891 int sfs_page_size, n;
9892
9893 sfs_page_size = sizeof(*sfs_ptr) + 5 * 2;
9894 ctsio->kern_data_ptr = malloc(sfs_page_size, M_CTL, M_WAITOK | M_ZERO);
9895 sfs_ptr = (struct scsi_vpd_sfs *)ctsio->kern_data_ptr;
9896 ctsio->kern_sg_entries = 0;
9897 ctsio->kern_rel_offset = 0;
9898 ctsio->kern_sg_entries = 0;
9899 ctsio->kern_data_len = min(sfs_page_size, alloc_len);
9900 ctsio->kern_total_len = ctsio->kern_data_len;
9901
9902 /*
9903 * The control device is always connected. The disk device, on the
9904 * other hand, may not be online all the time. Need to change this
9905 * to figure out whether the disk device is actually online or not.
9906 */
9907 if (lun != NULL)
9908 sfs_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9909 lun->be_lun->lun_type;
9910 else
9911 sfs_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9912
9913 sfs_ptr->page_code = SVPD_SCSI_SFS;
9914 n = 0;
9915 /* Discovery 2016 */
9916 scsi_ulto2b(0x0001, &sfs_ptr->codes[2 * n++]);
9917 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9918 /* SBC Base 2016 */
9919 scsi_ulto2b(0x0101, &sfs_ptr->codes[2 * n++]);
9920 /* SBC Base 2010 */
9921 scsi_ulto2b(0x0102, &sfs_ptr->codes[2 * n++]);
9922 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9923 /* Basic Provisioning 2016 */
9924 scsi_ulto2b(0x0103, &sfs_ptr->codes[2 * n++]);
9925 }
9926 /* Drive Maintenance 2016 */
9927 //scsi_ulto2b(0x0104, &sfs_ptr->codes[2 * n++]);
9928 }
9929 scsi_ulto2b(4 + 2 * n, sfs_ptr->page_length);
9930
9931 ctl_set_success(ctsio);
9932 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9933 ctsio->be_move_done = ctl_config_move_done;
9934 ctl_datamove((union ctl_io *)ctsio);
9935 return (CTL_RETVAL_COMPLETE);
9936 }
9937
9938 static int
ctl_inquiry_evpd_block_limits(struct ctl_scsiio * ctsio,int alloc_len)9939 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9940 {
9941 struct ctl_lun *lun = CTL_LUN(ctsio);
9942 struct scsi_vpd_block_limits *bl_ptr;
9943 const char *val;
9944 uint64_t ival;
9945
9946 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9947 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9948 ctsio->kern_sg_entries = 0;
9949 ctsio->kern_rel_offset = 0;
9950 ctsio->kern_sg_entries = 0;
9951 ctsio->kern_data_len = min(sizeof(*bl_ptr), alloc_len);
9952 ctsio->kern_total_len = ctsio->kern_data_len;
9953
9954 /*
9955 * The control device is always connected. The disk device, on the
9956 * other hand, may not be online all the time. Need to change this
9957 * to figure out whether the disk device is actually online or not.
9958 */
9959 if (lun != NULL)
9960 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9961 lun->be_lun->lun_type;
9962 else
9963 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9964
9965 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9966 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9967 bl_ptr->max_cmp_write_len = 0xff;
9968 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9969 if (lun != NULL) {
9970 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9971 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9972 ival = 0xffffffff;
9973 val = dnvlist_get_string(lun->be_lun->options,
9974 "unmap_max_lba", NULL);
9975 if (val != NULL)
9976 ctl_expand_number(val, &ival);
9977 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt);
9978 ival = 0xffffffff;
9979 val = dnvlist_get_string(lun->be_lun->options,
9980 "unmap_max_descr", NULL);
9981 if (val != NULL)
9982 ctl_expand_number(val, &ival);
9983 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt);
9984 if (lun->be_lun->ublockexp != 0) {
9985 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9986 bl_ptr->opt_unmap_grain);
9987 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9988 bl_ptr->unmap_grain_align);
9989 }
9990 }
9991 scsi_ulto4b(lun->be_lun->atomicblock,
9992 bl_ptr->max_atomic_transfer_length);
9993 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9994 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9995 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary);
9996 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size);
9997 ival = UINT64_MAX;
9998 val = dnvlist_get_string(lun->be_lun->options,
9999 "write_same_max_lba", NULL);
10000 if (val != NULL)
10001 ctl_expand_number(val, &ival);
10002 scsi_u64to8b(ival, bl_ptr->max_write_same_length);
10003 if (lun->be_lun->maxlba + 1 > ival)
10004 bl_ptr->flags |= SVPD_BL_WSNZ;
10005 }
10006
10007 ctl_set_success(ctsio);
10008 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10009 ctsio->be_move_done = ctl_config_move_done;
10010 ctl_datamove((union ctl_io *)ctsio);
10011 return (CTL_RETVAL_COMPLETE);
10012 }
10013
10014 static int
ctl_inquiry_evpd_bdc(struct ctl_scsiio * ctsio,int alloc_len)10015 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
10016 {
10017 struct ctl_lun *lun = CTL_LUN(ctsio);
10018 struct scsi_vpd_block_device_characteristics *bdc_ptr;
10019 const char *value;
10020 u_int i;
10021
10022 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
10023 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
10024 ctsio->kern_sg_entries = 0;
10025 ctsio->kern_rel_offset = 0;
10026 ctsio->kern_data_len = min(sizeof(*bdc_ptr), alloc_len);
10027 ctsio->kern_total_len = ctsio->kern_data_len;
10028
10029 /*
10030 * The control device is always connected. The disk device, on the
10031 * other hand, may not be online all the time. Need to change this
10032 * to figure out whether the disk device is actually online or not.
10033 */
10034 if (lun != NULL)
10035 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10036 lun->be_lun->lun_type;
10037 else
10038 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10039 bdc_ptr->page_code = SVPD_BDC;
10040 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
10041 if (lun != NULL &&
10042 (value = dnvlist_get_string(lun->be_lun->options, "rpm", NULL)) != NULL)
10043 i = strtol(value, NULL, 0);
10044 else
10045 i = CTL_DEFAULT_ROTATION_RATE;
10046 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
10047 if (lun != NULL &&
10048 (value = dnvlist_get_string(lun->be_lun->options, "formfactor", NULL)) != NULL)
10049 i = strtol(value, NULL, 0);
10050 else
10051 i = 0;
10052 bdc_ptr->wab_wac_ff = (i & 0x0f);
10053 bdc_ptr->flags = SVPD_RBWZ | SVPD_FUAB | SVPD_VBULS;
10054
10055 ctl_set_success(ctsio);
10056 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10057 ctsio->be_move_done = ctl_config_move_done;
10058 ctl_datamove((union ctl_io *)ctsio);
10059 return (CTL_RETVAL_COMPLETE);
10060 }
10061
10062 static int
ctl_inquiry_evpd_lbp(struct ctl_scsiio * ctsio,int alloc_len)10063 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
10064 {
10065 struct ctl_lun *lun = CTL_LUN(ctsio);
10066 struct scsi_vpd_logical_block_prov *lbp_ptr;
10067 const char *value;
10068
10069 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
10070 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
10071 ctsio->kern_sg_entries = 0;
10072 ctsio->kern_rel_offset = 0;
10073 ctsio->kern_data_len = min(sizeof(*lbp_ptr), alloc_len);
10074 ctsio->kern_total_len = ctsio->kern_data_len;
10075
10076 /*
10077 * The control device is always connected. The disk device, on the
10078 * other hand, may not be online all the time. Need to change this
10079 * to figure out whether the disk device is actually online or not.
10080 */
10081 if (lun != NULL)
10082 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10083 lun->be_lun->lun_type;
10084 else
10085 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10086
10087 lbp_ptr->page_code = SVPD_LBP;
10088 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
10089 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
10090 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10091 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10092 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10093 value = dnvlist_get_string(lun->be_lun->options,
10094 "provisioning_type", NULL);
10095 if (value != NULL) {
10096 if (strcmp(value, "resource") == 0)
10097 lbp_ptr->prov_type = SVPD_LBP_RESOURCE;
10098 else if (strcmp(value, "thin") == 0)
10099 lbp_ptr->prov_type = SVPD_LBP_THIN;
10100 } else
10101 lbp_ptr->prov_type = SVPD_LBP_THIN;
10102 }
10103
10104 ctl_set_success(ctsio);
10105 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10106 ctsio->be_move_done = ctl_config_move_done;
10107 ctl_datamove((union ctl_io *)ctsio);
10108 return (CTL_RETVAL_COMPLETE);
10109 }
10110
10111 /*
10112 * INQUIRY with the EVPD bit set.
10113 */
10114 static int
ctl_inquiry_evpd(struct ctl_scsiio * ctsio)10115 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10116 {
10117 struct ctl_lun *lun = CTL_LUN(ctsio);
10118 struct scsi_inquiry *cdb;
10119 int alloc_len, retval;
10120
10121 cdb = (struct scsi_inquiry *)ctsio->cdb;
10122 alloc_len = scsi_2btoul(cdb->length);
10123
10124 switch (cdb->page_code) {
10125 case SVPD_SUPPORTED_PAGES:
10126 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10127 break;
10128 case SVPD_UNIT_SERIAL_NUMBER:
10129 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10130 break;
10131 case SVPD_DEVICE_ID:
10132 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10133 break;
10134 case SVPD_EXTENDED_INQUIRY_DATA:
10135 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10136 break;
10137 case SVPD_MODE_PAGE_POLICY:
10138 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10139 break;
10140 case SVPD_SCSI_PORTS:
10141 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10142 break;
10143 case SVPD_SCSI_TPC:
10144 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10145 break;
10146 case SVPD_SCSI_SFS:
10147 retval = ctl_inquiry_evpd_sfs(ctsio, alloc_len);
10148 break;
10149 case SVPD_BLOCK_LIMITS:
10150 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10151 goto err;
10152 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10153 break;
10154 case SVPD_BDC:
10155 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10156 goto err;
10157 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10158 break;
10159 case SVPD_LBP:
10160 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10161 goto err;
10162 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10163 break;
10164 default:
10165 err:
10166 ctl_set_invalid_field(ctsio,
10167 /*sks_valid*/ 1,
10168 /*command*/ 1,
10169 /*field*/ 2,
10170 /*bit_valid*/ 0,
10171 /*bit*/ 0);
10172 ctl_done((union ctl_io *)ctsio);
10173 retval = CTL_RETVAL_COMPLETE;
10174 break;
10175 }
10176
10177 return (retval);
10178 }
10179
10180 /*
10181 * Standard INQUIRY data.
10182 */
10183 static int
ctl_inquiry_std(struct ctl_scsiio * ctsio)10184 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10185 {
10186 struct ctl_softc *softc = CTL_SOFTC(ctsio);
10187 struct ctl_port *port = CTL_PORT(ctsio);
10188 struct ctl_lun *lun = CTL_LUN(ctsio);
10189 struct scsi_inquiry_data *inq_ptr;
10190 struct scsi_inquiry *cdb;
10191 const char *val;
10192 uint32_t alloc_len, data_len;
10193 ctl_port_type port_type;
10194
10195 port_type = port->port_type;
10196 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10197 port_type = CTL_PORT_SCSI;
10198
10199 cdb = (struct scsi_inquiry *)ctsio->cdb;
10200 alloc_len = scsi_2btoul(cdb->length);
10201
10202 /*
10203 * We malloc the full inquiry data size here and fill it
10204 * in. If the user only asks for less, we'll give him
10205 * that much.
10206 */
10207 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10208 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10209 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10210 ctsio->kern_sg_entries = 0;
10211 ctsio->kern_rel_offset = 0;
10212 ctsio->kern_data_len = min(data_len, alloc_len);
10213 ctsio->kern_total_len = ctsio->kern_data_len;
10214
10215 if (lun != NULL) {
10216 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
10217 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
10218 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10219 lun->be_lun->lun_type;
10220 } else {
10221 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
10222 lun->be_lun->lun_type;
10223 }
10224 if (lun->flags & CTL_LUN_REMOVABLE)
10225 inq_ptr->dev_qual2 |= SID_RMB;
10226 } else
10227 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10228
10229 /* RMB in byte 2 is 0 */
10230 inq_ptr->version = SCSI_REV_SPC5;
10231
10232 /*
10233 * According to SAM-3, even if a device only supports a single
10234 * level of LUN addressing, it should still set the HISUP bit:
10235 *
10236 * 4.9.1 Logical unit numbers overview
10237 *
10238 * All logical unit number formats described in this standard are
10239 * hierarchical in structure even when only a single level in that
10240 * hierarchy is used. The HISUP bit shall be set to one in the
10241 * standard INQUIRY data (see SPC-2) when any logical unit number
10242 * format described in this standard is used. Non-hierarchical
10243 * formats are outside the scope of this standard.
10244 *
10245 * Therefore we set the HiSup bit here.
10246 *
10247 * The response format is 2, per SPC-3.
10248 */
10249 inq_ptr->response_format = SID_HiSup | 2;
10250
10251 inq_ptr->additional_length = data_len -
10252 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10253 CTL_DEBUG_PRINT(("additional_length = %d\n",
10254 inq_ptr->additional_length));
10255
10256 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10257 if (port_type == CTL_PORT_SCSI)
10258 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10259 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10260 inq_ptr->flags = SID_CmdQue;
10261 if (port_type == CTL_PORT_SCSI)
10262 inq_ptr->flags |= SID_WBus16 | SID_Sync;
10263
10264 /*
10265 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10266 * We have 8 bytes for the vendor name, and 16 bytes for the device
10267 * name and 4 bytes for the revision.
10268 */
10269 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options,
10270 "vendor", NULL)) == NULL) {
10271 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10272 } else {
10273 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10274 strncpy(inq_ptr->vendor, val,
10275 min(sizeof(inq_ptr->vendor), strlen(val)));
10276 }
10277 if (lun == NULL) {
10278 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10279 sizeof(inq_ptr->product));
10280 } else if ((val = dnvlist_get_string(lun->be_lun->options, "product",
10281 NULL)) == NULL) {
10282 switch (lun->be_lun->lun_type) {
10283 case T_DIRECT:
10284 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10285 sizeof(inq_ptr->product));
10286 break;
10287 case T_PROCESSOR:
10288 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10289 sizeof(inq_ptr->product));
10290 break;
10291 case T_CDROM:
10292 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT,
10293 sizeof(inq_ptr->product));
10294 break;
10295 default:
10296 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10297 sizeof(inq_ptr->product));
10298 break;
10299 }
10300 } else {
10301 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10302 strncpy(inq_ptr->product, val,
10303 min(sizeof(inq_ptr->product), strlen(val)));
10304 }
10305
10306 /*
10307 * XXX make this a macro somewhere so it automatically gets
10308 * incremented when we make changes.
10309 */
10310 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options,
10311 "revision", NULL)) == NULL) {
10312 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10313 } else {
10314 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10315 strncpy(inq_ptr->revision, val,
10316 min(sizeof(inq_ptr->revision), strlen(val)));
10317 }
10318
10319 /*
10320 * For parallel SCSI, we support double transition and single
10321 * transition clocking. We also support QAS (Quick Arbitration
10322 * and Selection) and Information Unit transfers on both the
10323 * control and array devices.
10324 */
10325 if (port_type == CTL_PORT_SCSI)
10326 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10327 SID_SPI_IUS;
10328
10329 /* SAM-6 (no version claimed) */
10330 scsi_ulto2b(0x00C0, inq_ptr->version1);
10331 /* SPC-5 (no version claimed) */
10332 scsi_ulto2b(0x05C0, inq_ptr->version2);
10333 if (port_type == CTL_PORT_FC) {
10334 /* FCP-2 ANSI INCITS.350:2003 */
10335 scsi_ulto2b(0x0917, inq_ptr->version3);
10336 } else if (port_type == CTL_PORT_SCSI) {
10337 /* SPI-4 ANSI INCITS.362:200x */
10338 scsi_ulto2b(0x0B56, inq_ptr->version3);
10339 } else if (port_type == CTL_PORT_ISCSI) {
10340 /* iSCSI (no version claimed) */
10341 scsi_ulto2b(0x0960, inq_ptr->version3);
10342 } else if (port_type == CTL_PORT_SAS) {
10343 /* SAS (no version claimed) */
10344 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10345 } else if (port_type == CTL_PORT_UMASS) {
10346 /* USB Mass Storage Class Bulk-Only Transport, Revision 1.0 */
10347 scsi_ulto2b(0x1730, inq_ptr->version3);
10348 }
10349
10350 if (lun == NULL) {
10351 /* SBC-4 (no version claimed) */
10352 scsi_ulto2b(0x0600, inq_ptr->version4);
10353 } else {
10354 switch (lun->be_lun->lun_type) {
10355 case T_DIRECT:
10356 /* SBC-4 (no version claimed) */
10357 scsi_ulto2b(0x0600, inq_ptr->version4);
10358 break;
10359 case T_PROCESSOR:
10360 break;
10361 case T_CDROM:
10362 /* MMC-6 (no version claimed) */
10363 scsi_ulto2b(0x04E0, inq_ptr->version4);
10364 break;
10365 default:
10366 break;
10367 }
10368 }
10369
10370 ctl_set_success(ctsio);
10371 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10372 ctsio->be_move_done = ctl_config_move_done;
10373 ctl_datamove((union ctl_io *)ctsio);
10374 return (CTL_RETVAL_COMPLETE);
10375 }
10376
10377 int
ctl_inquiry(struct ctl_scsiio * ctsio)10378 ctl_inquiry(struct ctl_scsiio *ctsio)
10379 {
10380 struct scsi_inquiry *cdb;
10381 int retval;
10382
10383 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10384
10385 cdb = (struct scsi_inquiry *)ctsio->cdb;
10386 if (cdb->byte2 & SI_EVPD)
10387 retval = ctl_inquiry_evpd(ctsio);
10388 else if (cdb->page_code == 0)
10389 retval = ctl_inquiry_std(ctsio);
10390 else {
10391 ctl_set_invalid_field(ctsio,
10392 /*sks_valid*/ 1,
10393 /*command*/ 1,
10394 /*field*/ 2,
10395 /*bit_valid*/ 0,
10396 /*bit*/ 0);
10397 ctl_done((union ctl_io *)ctsio);
10398 return (CTL_RETVAL_COMPLETE);
10399 }
10400
10401 return (retval);
10402 }
10403
10404 int
ctl_get_config(struct ctl_scsiio * ctsio)10405 ctl_get_config(struct ctl_scsiio *ctsio)
10406 {
10407 struct ctl_lun *lun = CTL_LUN(ctsio);
10408 struct scsi_get_config_header *hdr;
10409 struct scsi_get_config_feature *feature;
10410 struct scsi_get_config *cdb;
10411 uint32_t alloc_len, data_len;
10412 int rt, starting;
10413
10414 cdb = (struct scsi_get_config *)ctsio->cdb;
10415 rt = (cdb->rt & SGC_RT_MASK);
10416 starting = scsi_2btoul(cdb->starting_feature);
10417 alloc_len = scsi_2btoul(cdb->length);
10418
10419 data_len = sizeof(struct scsi_get_config_header) +
10420 sizeof(struct scsi_get_config_feature) + 8 +
10421 sizeof(struct scsi_get_config_feature) + 8 +
10422 sizeof(struct scsi_get_config_feature) + 4 +
10423 sizeof(struct scsi_get_config_feature) + 4 +
10424 sizeof(struct scsi_get_config_feature) + 8 +
10425 sizeof(struct scsi_get_config_feature) +
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 sizeof(struct scsi_get_config_feature) + 4 +
10430 sizeof(struct scsi_get_config_feature) + 4 +
10431 sizeof(struct scsi_get_config_feature) + 4;
10432 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10433 ctsio->kern_sg_entries = 0;
10434 ctsio->kern_rel_offset = 0;
10435
10436 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr;
10437 if (lun->flags & CTL_LUN_NO_MEDIA)
10438 scsi_ulto2b(0x0000, hdr->current_profile);
10439 else
10440 scsi_ulto2b(0x0010, hdr->current_profile);
10441 feature = (struct scsi_get_config_feature *)(hdr + 1);
10442
10443 if (starting > 0x003b)
10444 goto done;
10445 if (starting > 0x003a)
10446 goto f3b;
10447 if (starting > 0x002b)
10448 goto f3a;
10449 if (starting > 0x002a)
10450 goto f2b;
10451 if (starting > 0x001f)
10452 goto f2a;
10453 if (starting > 0x001e)
10454 goto f1f;
10455 if (starting > 0x001d)
10456 goto f1e;
10457 if (starting > 0x0010)
10458 goto f1d;
10459 if (starting > 0x0003)
10460 goto f10;
10461 if (starting > 0x0002)
10462 goto f3;
10463 if (starting > 0x0001)
10464 goto f2;
10465 if (starting > 0x0000)
10466 goto f1;
10467
10468 /* Profile List */
10469 scsi_ulto2b(0x0000, feature->feature_code);
10470 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT;
10471 feature->add_length = 8;
10472 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */
10473 feature->feature_data[2] = 0x00;
10474 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */
10475 feature->feature_data[6] = 0x01;
10476 feature = (struct scsi_get_config_feature *)
10477 &feature->feature_data[feature->add_length];
10478
10479 f1: /* Core */
10480 scsi_ulto2b(0x0001, feature->feature_code);
10481 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10482 feature->add_length = 8;
10483 scsi_ulto4b(0x00000000, &feature->feature_data[0]);
10484 feature->feature_data[4] = 0x03;
10485 feature = (struct scsi_get_config_feature *)
10486 &feature->feature_data[feature->add_length];
10487
10488 f2: /* Morphing */
10489 scsi_ulto2b(0x0002, feature->feature_code);
10490 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10491 feature->add_length = 4;
10492 feature->feature_data[0] = 0x02;
10493 feature = (struct scsi_get_config_feature *)
10494 &feature->feature_data[feature->add_length];
10495
10496 f3: /* Removable Medium */
10497 scsi_ulto2b(0x0003, feature->feature_code);
10498 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10499 feature->add_length = 4;
10500 feature->feature_data[0] = 0x39;
10501 feature = (struct scsi_get_config_feature *)
10502 &feature->feature_data[feature->add_length];
10503
10504 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA))
10505 goto done;
10506
10507 f10: /* Random Read */
10508 scsi_ulto2b(0x0010, feature->feature_code);
10509 feature->flags = 0x00;
10510 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10511 feature->flags |= SGC_F_CURRENT;
10512 feature->add_length = 8;
10513 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]);
10514 scsi_ulto2b(1, &feature->feature_data[4]);
10515 feature->feature_data[6] = 0x00;
10516 feature = (struct scsi_get_config_feature *)
10517 &feature->feature_data[feature->add_length];
10518
10519 f1d: /* Multi-Read */
10520 scsi_ulto2b(0x001D, feature->feature_code);
10521 feature->flags = 0x00;
10522 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10523 feature->flags |= SGC_F_CURRENT;
10524 feature->add_length = 0;
10525 feature = (struct scsi_get_config_feature *)
10526 &feature->feature_data[feature->add_length];
10527
10528 f1e: /* CD Read */
10529 scsi_ulto2b(0x001E, feature->feature_code);
10530 feature->flags = 0x00;
10531 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10532 feature->flags |= SGC_F_CURRENT;
10533 feature->add_length = 4;
10534 feature->feature_data[0] = 0x00;
10535 feature = (struct scsi_get_config_feature *)
10536 &feature->feature_data[feature->add_length];
10537
10538 f1f: /* DVD Read */
10539 scsi_ulto2b(0x001F, feature->feature_code);
10540 feature->flags = 0x08;
10541 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10542 feature->flags |= SGC_F_CURRENT;
10543 feature->add_length = 4;
10544 feature->feature_data[0] = 0x01;
10545 feature->feature_data[2] = 0x03;
10546 feature = (struct scsi_get_config_feature *)
10547 &feature->feature_data[feature->add_length];
10548
10549 f2a: /* DVD+RW */
10550 scsi_ulto2b(0x002A, feature->feature_code);
10551 feature->flags = 0x04;
10552 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10553 feature->flags |= SGC_F_CURRENT;
10554 feature->add_length = 4;
10555 feature->feature_data[0] = 0x00;
10556 feature->feature_data[1] = 0x00;
10557 feature = (struct scsi_get_config_feature *)
10558 &feature->feature_data[feature->add_length];
10559
10560 f2b: /* DVD+R */
10561 scsi_ulto2b(0x002B, feature->feature_code);
10562 feature->flags = 0x00;
10563 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10564 feature->flags |= SGC_F_CURRENT;
10565 feature->add_length = 4;
10566 feature->feature_data[0] = 0x00;
10567 feature = (struct scsi_get_config_feature *)
10568 &feature->feature_data[feature->add_length];
10569
10570 f3a: /* DVD+RW Dual Layer */
10571 scsi_ulto2b(0x003A, feature->feature_code);
10572 feature->flags = 0x00;
10573 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10574 feature->flags |= SGC_F_CURRENT;
10575 feature->add_length = 4;
10576 feature->feature_data[0] = 0x00;
10577 feature->feature_data[1] = 0x00;
10578 feature = (struct scsi_get_config_feature *)
10579 &feature->feature_data[feature->add_length];
10580
10581 f3b: /* DVD+R Dual Layer */
10582 scsi_ulto2b(0x003B, feature->feature_code);
10583 feature->flags = 0x00;
10584 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10585 feature->flags |= SGC_F_CURRENT;
10586 feature->add_length = 4;
10587 feature->feature_data[0] = 0x00;
10588 feature = (struct scsi_get_config_feature *)
10589 &feature->feature_data[feature->add_length];
10590
10591 done:
10592 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10593 if (rt == SGC_RT_SPECIFIC && data_len > 4) {
10594 feature = (struct scsi_get_config_feature *)(hdr + 1);
10595 if (scsi_2btoul(feature->feature_code) == starting)
10596 feature = (struct scsi_get_config_feature *)
10597 &feature->feature_data[feature->add_length];
10598 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10599 }
10600 scsi_ulto4b(data_len - 4, hdr->data_length);
10601 ctsio->kern_data_len = min(data_len, alloc_len);
10602 ctsio->kern_total_len = ctsio->kern_data_len;
10603
10604 ctl_set_success(ctsio);
10605 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10606 ctsio->be_move_done = ctl_config_move_done;
10607 ctl_datamove((union ctl_io *)ctsio);
10608 return (CTL_RETVAL_COMPLETE);
10609 }
10610
10611 int
ctl_get_event_status(struct ctl_scsiio * ctsio)10612 ctl_get_event_status(struct ctl_scsiio *ctsio)
10613 {
10614 struct scsi_get_event_status_header *hdr;
10615 struct scsi_get_event_status *cdb;
10616 uint32_t alloc_len, data_len;
10617
10618 cdb = (struct scsi_get_event_status *)ctsio->cdb;
10619 if ((cdb->byte2 & SGESN_POLLED) == 0) {
10620 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
10621 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
10622 ctl_done((union ctl_io *)ctsio);
10623 return (CTL_RETVAL_COMPLETE);
10624 }
10625 alloc_len = scsi_2btoul(cdb->length);
10626
10627 data_len = sizeof(struct scsi_get_event_status_header);
10628 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10629 ctsio->kern_sg_entries = 0;
10630 ctsio->kern_rel_offset = 0;
10631 ctsio->kern_data_len = min(data_len, alloc_len);
10632 ctsio->kern_total_len = ctsio->kern_data_len;
10633
10634 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr;
10635 scsi_ulto2b(0, hdr->descr_length);
10636 hdr->nea_class = SGESN_NEA;
10637 hdr->supported_class = 0;
10638
10639 ctl_set_success(ctsio);
10640 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10641 ctsio->be_move_done = ctl_config_move_done;
10642 ctl_datamove((union ctl_io *)ctsio);
10643 return (CTL_RETVAL_COMPLETE);
10644 }
10645
10646 int
ctl_mechanism_status(struct ctl_scsiio * ctsio)10647 ctl_mechanism_status(struct ctl_scsiio *ctsio)
10648 {
10649 struct scsi_mechanism_status_header *hdr;
10650 struct scsi_mechanism_status *cdb;
10651 uint32_t alloc_len, data_len;
10652
10653 cdb = (struct scsi_mechanism_status *)ctsio->cdb;
10654 alloc_len = scsi_2btoul(cdb->length);
10655
10656 data_len = sizeof(struct scsi_mechanism_status_header);
10657 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10658 ctsio->kern_sg_entries = 0;
10659 ctsio->kern_rel_offset = 0;
10660 ctsio->kern_data_len = min(data_len, alloc_len);
10661 ctsio->kern_total_len = ctsio->kern_data_len;
10662
10663 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr;
10664 hdr->state1 = 0x00;
10665 hdr->state2 = 0xe0;
10666 scsi_ulto3b(0, hdr->lba);
10667 hdr->slots_num = 0;
10668 scsi_ulto2b(0, hdr->slots_length);
10669
10670 ctl_set_success(ctsio);
10671 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10672 ctsio->be_move_done = ctl_config_move_done;
10673 ctl_datamove((union ctl_io *)ctsio);
10674 return (CTL_RETVAL_COMPLETE);
10675 }
10676
10677 static void
ctl_ultomsf(uint32_t lba,uint8_t * buf)10678 ctl_ultomsf(uint32_t lba, uint8_t *buf)
10679 {
10680
10681 lba += 150;
10682 buf[0] = 0;
10683 buf[1] = bin2bcd((lba / 75) / 60);
10684 buf[2] = bin2bcd((lba / 75) % 60);
10685 buf[3] = bin2bcd(lba % 75);
10686 }
10687
10688 int
ctl_read_toc(struct ctl_scsiio * ctsio)10689 ctl_read_toc(struct ctl_scsiio *ctsio)
10690 {
10691 struct ctl_lun *lun = CTL_LUN(ctsio);
10692 struct scsi_read_toc_hdr *hdr;
10693 struct scsi_read_toc_type01_descr *descr;
10694 struct scsi_read_toc *cdb;
10695 uint32_t alloc_len, data_len;
10696 int format, msf;
10697
10698 cdb = (struct scsi_read_toc *)ctsio->cdb;
10699 msf = (cdb->byte2 & CD_MSF) != 0;
10700 format = cdb->format;
10701 alloc_len = scsi_2btoul(cdb->data_len);
10702
10703 data_len = sizeof(struct scsi_read_toc_hdr);
10704 if (format == 0)
10705 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr);
10706 else
10707 data_len += sizeof(struct scsi_read_toc_type01_descr);
10708 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10709 ctsio->kern_sg_entries = 0;
10710 ctsio->kern_rel_offset = 0;
10711 ctsio->kern_data_len = min(data_len, alloc_len);
10712 ctsio->kern_total_len = ctsio->kern_data_len;
10713
10714 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr;
10715 if (format == 0) {
10716 scsi_ulto2b(0x12, hdr->data_length);
10717 hdr->first = 1;
10718 hdr->last = 1;
10719 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10720 descr->addr_ctl = 0x14;
10721 descr->track_number = 1;
10722 if (msf)
10723 ctl_ultomsf(0, descr->track_start);
10724 else
10725 scsi_ulto4b(0, descr->track_start);
10726 descr++;
10727 descr->addr_ctl = 0x14;
10728 descr->track_number = 0xaa;
10729 if (msf)
10730 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start);
10731 else
10732 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start);
10733 } else {
10734 scsi_ulto2b(0x0a, hdr->data_length);
10735 hdr->first = 1;
10736 hdr->last = 1;
10737 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10738 descr->addr_ctl = 0x14;
10739 descr->track_number = 1;
10740 if (msf)
10741 ctl_ultomsf(0, descr->track_start);
10742 else
10743 scsi_ulto4b(0, descr->track_start);
10744 }
10745
10746 ctl_set_success(ctsio);
10747 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10748 ctsio->be_move_done = ctl_config_move_done;
10749 ctl_datamove((union ctl_io *)ctsio);
10750 return (CTL_RETVAL_COMPLETE);
10751 }
10752
10753 /*
10754 * For known CDB types, parse the LBA and length.
10755 */
10756 static int
ctl_get_lba_len(union ctl_io * io,uint64_t * lba,uint64_t * len)10757 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10758 {
10759
10760 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
10761 ("%s: unexpected I/O type %x", __func__, io->io_hdr.io_type));
10762
10763 switch (io->scsiio.cdb[0]) {
10764 case COMPARE_AND_WRITE: {
10765 struct scsi_compare_and_write *cdb;
10766
10767 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10768
10769 *lba = scsi_8btou64(cdb->addr);
10770 *len = cdb->length;
10771 break;
10772 }
10773 case READ_6:
10774 case WRITE_6: {
10775 struct scsi_rw_6 *cdb;
10776
10777 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10778
10779 *lba = scsi_3btoul(cdb->addr);
10780 /* only 5 bits are valid in the most significant address byte */
10781 *lba &= 0x1fffff;
10782 *len = cdb->length;
10783 break;
10784 }
10785 case READ_10:
10786 case WRITE_10: {
10787 struct scsi_rw_10 *cdb;
10788
10789 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10790
10791 *lba = scsi_4btoul(cdb->addr);
10792 *len = scsi_2btoul(cdb->length);
10793 break;
10794 }
10795 case WRITE_VERIFY_10: {
10796 struct scsi_write_verify_10 *cdb;
10797
10798 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10799
10800 *lba = scsi_4btoul(cdb->addr);
10801 *len = scsi_2btoul(cdb->length);
10802 break;
10803 }
10804 case READ_12:
10805 case WRITE_12: {
10806 struct scsi_rw_12 *cdb;
10807
10808 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10809
10810 *lba = scsi_4btoul(cdb->addr);
10811 *len = scsi_4btoul(cdb->length);
10812 break;
10813 }
10814 case WRITE_VERIFY_12: {
10815 struct scsi_write_verify_12 *cdb;
10816
10817 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10818
10819 *lba = scsi_4btoul(cdb->addr);
10820 *len = scsi_4btoul(cdb->length);
10821 break;
10822 }
10823 case READ_16:
10824 case WRITE_16: {
10825 struct scsi_rw_16 *cdb;
10826
10827 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10828
10829 *lba = scsi_8btou64(cdb->addr);
10830 *len = scsi_4btoul(cdb->length);
10831 break;
10832 }
10833 case WRITE_ATOMIC_16: {
10834 struct scsi_write_atomic_16 *cdb;
10835
10836 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb;
10837
10838 *lba = scsi_8btou64(cdb->addr);
10839 *len = scsi_2btoul(cdb->length);
10840 break;
10841 }
10842 case WRITE_VERIFY_16: {
10843 struct scsi_write_verify_16 *cdb;
10844
10845 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10846
10847 *lba = scsi_8btou64(cdb->addr);
10848 *len = scsi_4btoul(cdb->length);
10849 break;
10850 }
10851 case WRITE_SAME_10: {
10852 struct scsi_write_same_10 *cdb;
10853
10854 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10855
10856 *lba = scsi_4btoul(cdb->addr);
10857 *len = scsi_2btoul(cdb->length);
10858 break;
10859 }
10860 case WRITE_SAME_16: {
10861 struct scsi_write_same_16 *cdb;
10862
10863 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10864
10865 *lba = scsi_8btou64(cdb->addr);
10866 *len = scsi_4btoul(cdb->length);
10867 break;
10868 }
10869 case VERIFY_10: {
10870 struct scsi_verify_10 *cdb;
10871
10872 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10873
10874 *lba = scsi_4btoul(cdb->addr);
10875 *len = scsi_2btoul(cdb->length);
10876 break;
10877 }
10878 case VERIFY_12: {
10879 struct scsi_verify_12 *cdb;
10880
10881 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10882
10883 *lba = scsi_4btoul(cdb->addr);
10884 *len = scsi_4btoul(cdb->length);
10885 break;
10886 }
10887 case VERIFY_16: {
10888 struct scsi_verify_16 *cdb;
10889
10890 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10891
10892 *lba = scsi_8btou64(cdb->addr);
10893 *len = scsi_4btoul(cdb->length);
10894 break;
10895 }
10896 case UNMAP: {
10897 *lba = 0;
10898 *len = UINT64_MAX;
10899 break;
10900 }
10901 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10902 struct scsi_get_lba_status *cdb;
10903
10904 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10905 *lba = scsi_8btou64(cdb->addr);
10906 *len = UINT32_MAX;
10907 break;
10908 }
10909 default:
10910 *lba = 0;
10911 *len = UINT64_MAX;
10912 return (1);
10913 }
10914
10915 return (0);
10916 }
10917
10918 static ctl_action
ctl_extent_check_lba(uint64_t lba1,uint64_t len1,uint64_t lba2,uint64_t len2,bool seq)10919 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10920 bool seq)
10921 {
10922 uint64_t endlba1, endlba2;
10923
10924 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10925 endlba2 = lba2 + len2 - 1;
10926
10927 if ((endlba1 < lba2) || (endlba2 < lba1))
10928 return (CTL_ACTION_PASS);
10929 else
10930 return (CTL_ACTION_BLOCK);
10931 }
10932
10933 static int
ctl_extent_check_unmap(union ctl_io * io,uint64_t lba2,uint64_t len2)10934 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10935 {
10936 struct ctl_ptr_len_flags *ptrlen;
10937 struct scsi_unmap_desc *buf, *end, *range;
10938 uint64_t lba;
10939 uint32_t len;
10940
10941 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
10942 ("%s: unexpected I/O type %x", __func__, io->io_hdr.io_type));
10943
10944 /* If not UNMAP -- go other way. */
10945 if (io->scsiio.cdb[0] != UNMAP)
10946 return (CTL_ACTION_SKIP);
10947
10948 /* If UNMAP without data -- block and wait for data. */
10949 ptrlen = (struct ctl_ptr_len_flags *)
10950 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10951 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10952 ptrlen->ptr == NULL)
10953 return (CTL_ACTION_BLOCK);
10954
10955 /* UNMAP with data -- check for collision. */
10956 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10957 end = buf + ptrlen->len / sizeof(*buf);
10958 for (range = buf; range < end; range++) {
10959 lba = scsi_8btou64(range->lba);
10960 len = scsi_4btoul(range->length);
10961 if ((lba < lba2 + len2) && (lba + len > lba2))
10962 return (CTL_ACTION_BLOCK);
10963 }
10964 return (CTL_ACTION_PASS);
10965 }
10966
10967 static ctl_action
ctl_extent_check(union ctl_io * io1,union ctl_io * io2,bool seq)10968 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10969 {
10970 uint64_t lba1, lba2;
10971 uint64_t len1, len2;
10972 int retval;
10973
10974 retval = ctl_get_lba_len(io2, &lba2, &len2);
10975 KASSERT(retval == 0, ("ctl_get_lba_len() error"));
10976
10977 retval = ctl_extent_check_unmap(io1, lba2, len2);
10978 if (retval != CTL_ACTION_SKIP)
10979 return (retval);
10980
10981 retval = ctl_get_lba_len(io1, &lba1, &len1);
10982 KASSERT(retval == 0, ("ctl_get_lba_len() error"));
10983
10984 if (seq && (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE))
10985 seq = FALSE;
10986 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10987 }
10988
10989 static ctl_action
ctl_seq_check(union ctl_io * io1,union ctl_io * io2)10990 ctl_seq_check(union ctl_io *io1, union ctl_io *io2)
10991 {
10992 uint64_t lba1, lba2;
10993 uint64_t len1, len2;
10994 int retval __diagused;
10995
10996 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10997 return (CTL_ACTION_PASS);
10998 retval = ctl_get_lba_len(io1, &lba1, &len1);
10999 KASSERT(retval == 0, ("ctl_get_lba_len() error"));
11000 retval = ctl_get_lba_len(io2, &lba2, &len2);
11001 KASSERT(retval == 0, ("ctl_get_lba_len() error"));
11002
11003 if (lba1 + len1 == lba2)
11004 return (CTL_ACTION_BLOCK);
11005 return (CTL_ACTION_PASS);
11006 }
11007
11008 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)11009 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
11010 const uint8_t *serialize_row, union ctl_io *ooa_io)
11011 {
11012
11013 /*
11014 * The initiator attempted multiple untagged commands at the same
11015 * time. Can't do that.
11016 */
11017 if (__predict_false(pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11018 && __predict_false(ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11019 && ((pending_io->io_hdr.nexus.targ_port ==
11020 ooa_io->io_hdr.nexus.targ_port)
11021 && (pending_io->io_hdr.nexus.initid ==
11022 ooa_io->io_hdr.nexus.initid))
11023 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
11024 CTL_FLAG_STATUS_SENT)) == 0))
11025 return (CTL_ACTION_OVERLAP);
11026
11027 /*
11028 * The initiator attempted to send multiple tagged commands with
11029 * the same ID. (It's fine if different initiators have the same
11030 * tag ID.)
11031 *
11032 * Even if all of those conditions are true, we don't kill the I/O
11033 * if the command ahead of us has been aborted. We won't end up
11034 * sending it to the FETD, and it's perfectly legal to resend a
11035 * command with the same tag number as long as the previous
11036 * instance of this tag number has been aborted somehow.
11037 */
11038 if (__predict_true(pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11039 && __predict_true(ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11040 && __predict_false(pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
11041 && ((pending_io->io_hdr.nexus.targ_port ==
11042 ooa_io->io_hdr.nexus.targ_port)
11043 && (pending_io->io_hdr.nexus.initid ==
11044 ooa_io->io_hdr.nexus.initid))
11045 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
11046 CTL_FLAG_STATUS_SENT)) == 0))
11047 return (CTL_ACTION_OVERLAP_TAG);
11048
11049 /*
11050 * If we get a head of queue tag, SAM-3 says that we should
11051 * immediately execute it.
11052 *
11053 * What happens if this command would normally block for some other
11054 * reason? e.g. a request sense with a head of queue tag
11055 * immediately after a write. Normally that would block, but this
11056 * will result in its getting executed immediately...
11057 *
11058 * We currently return "pass" instead of "skip", so we'll end up
11059 * going through the rest of the queue to check for overlapped tags.
11060 *
11061 * XXX KDM check for other types of blockage first??
11062 */
11063 if (__predict_false(pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE))
11064 return (CTL_ACTION_PASS);
11065
11066 /*
11067 * Simple tags get blocked until all head of queue and ordered tags
11068 * ahead of them have completed. I'm lumping untagged commands in
11069 * with simple tags here. XXX KDM is that the right thing to do?
11070 */
11071 if (__predict_false(ooa_io->scsiio.tag_type == CTL_TAG_ORDERED) ||
11072 __predict_false(ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE))
11073 return (CTL_ACTION_BLOCK);
11074
11075 /* Unsupported command in OOA queue. */
11076 if (__predict_false(ooa_io->scsiio.seridx == CTL_SERIDX_INVLD))
11077 return (CTL_ACTION_PASS);
11078
11079 switch (serialize_row[ooa_io->scsiio.seridx]) {
11080 case CTL_SER_SEQ:
11081 if (lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
11082 return (ctl_seq_check(ooa_io, pending_io));
11083 /* FALLTHROUGH */
11084 case CTL_SER_PASS:
11085 return (CTL_ACTION_PASS);
11086 case CTL_SER_EXTENTOPT:
11087 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) ==
11088 SCP_QUEUE_ALG_UNRESTRICTED)
11089 return (CTL_ACTION_PASS);
11090 /* FALLTHROUGH */
11091 case CTL_SER_EXTENT:
11092 return (ctl_extent_check(ooa_io, pending_io,
11093 (lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
11094 case CTL_SER_BLOCKOPT:
11095 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) ==
11096 SCP_QUEUE_ALG_UNRESTRICTED)
11097 return (CTL_ACTION_PASS);
11098 /* FALLTHROUGH */
11099 case CTL_SER_BLOCK:
11100 return (CTL_ACTION_BLOCK);
11101 default:
11102 __assert_unreachable();
11103 }
11104 }
11105
11106 /*
11107 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
11108 * Assumptions:
11109 * - pending_io is generally either incoming, or on the blocked queue
11110 * - starting I/O is the I/O we want to start the check with.
11111 */
11112 static ctl_action
ctl_check_ooa(struct ctl_lun * lun,union ctl_io * pending_io,union ctl_io ** starting_io)11113 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
11114 union ctl_io **starting_io)
11115 {
11116 union ctl_io *ooa_io = *starting_io;
11117 const uint8_t *serialize_row;
11118 ctl_action action;
11119
11120 mtx_assert(&lun->lun_lock, MA_OWNED);
11121
11122 /*
11123 * Aborted commands are not going to be executed and may even
11124 * not report completion, so we don't care about their order.
11125 * Let them complete ASAP to clean the OOA queue.
11126 */
11127 if (__predict_false(pending_io->io_hdr.flags & CTL_FLAG_ABORT))
11128 return (CTL_ACTION_SKIP);
11129
11130 /*
11131 * Ordered tags have to block until all items ahead of them have
11132 * completed. If we get called with an ordered tag, we always
11133 * block, if something else is ahead of us in the queue.
11134 */
11135 if ((pending_io->scsiio.tag_type == CTL_TAG_ORDERED) &&
11136 (ooa_io != NULL))
11137 return (CTL_ACTION_BLOCK);
11138
11139 serialize_row = ctl_serialize_table[pending_io->scsiio.seridx];
11140
11141 /*
11142 * Run back along the OOA queue, starting with the current
11143 * blocked I/O and going through every I/O before it on the
11144 * queue. If starting_io is NULL, we'll just end up returning
11145 * CTL_ACTION_PASS.
11146 */
11147 for (; ooa_io != NULL;
11148 ooa_io = (union ctl_io *)LIST_NEXT(&ooa_io->io_hdr, ooa_links)) {
11149 action = ctl_check_for_blockage(lun, pending_io, serialize_row,
11150 ooa_io);
11151 if (action != CTL_ACTION_PASS) {
11152 *starting_io = ooa_io;
11153 return (action);
11154 }
11155 }
11156
11157 *starting_io = NULL;
11158 return (CTL_ACTION_PASS);
11159 }
11160
11161 /*
11162 * Try to unblock the specified I/O.
11163 *
11164 * skip parameter allows explicitly skip present blocker of the I/O,
11165 * starting from the previous one on OOA queue. It can be used when
11166 * we know for sure that the blocker I/O does no longer count.
11167 */
11168 static void
ctl_try_unblock_io(struct ctl_lun * lun,union ctl_io * io,bool skip)11169 ctl_try_unblock_io(struct ctl_lun *lun, union ctl_io *io, bool skip)
11170 {
11171 struct ctl_softc *softc = lun->ctl_softc;
11172 union ctl_io *bio, *obio;
11173 const struct ctl_cmd_entry *entry;
11174 union ctl_ha_msg msg_info;
11175 ctl_action action;
11176
11177 mtx_assert(&lun->lun_lock, MA_OWNED);
11178
11179 if (io->io_hdr.blocker == NULL)
11180 return;
11181
11182 obio = bio = io->io_hdr.blocker;
11183 if (skip)
11184 bio = (union ctl_io *)LIST_NEXT(&bio->io_hdr, ooa_links);
11185 action = ctl_check_ooa(lun, io, &bio);
11186 if (action == CTL_ACTION_BLOCK) {
11187 /* Still blocked, but may be by different I/O now. */
11188 if (bio != obio) {
11189 TAILQ_REMOVE(&obio->io_hdr.blocked_queue,
11190 &io->io_hdr, blocked_links);
11191 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue,
11192 &io->io_hdr, blocked_links);
11193 io->io_hdr.blocker = bio;
11194 }
11195 return;
11196 }
11197
11198 /* No longer blocked, one way or another. */
11199 TAILQ_REMOVE(&obio->io_hdr.blocked_queue, &io->io_hdr, blocked_links);
11200 io->io_hdr.blocker = NULL;
11201
11202 switch (action) {
11203 case CTL_ACTION_PASS:
11204 case CTL_ACTION_SKIP:
11205
11206 /* Serializing commands from the other SC retire there. */
11207 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) &&
11208 (softc->ha_mode != CTL_HA_MODE_XFER)) {
11209 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11210 msg_info.hdr.original_sc = io->io_hdr.remote_io;
11211 msg_info.hdr.serializing_sc = io;
11212 msg_info.hdr.msg_type = CTL_MSG_R2R;
11213 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11214 sizeof(msg_info.hdr), M_NOWAIT);
11215 break;
11216 }
11217
11218 /*
11219 * Check this I/O for LUN state changes that may have happened
11220 * while this command was blocked. The LUN state may have been
11221 * changed by a command ahead of us in the queue.
11222 */
11223 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
11224 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) {
11225 ctl_done(io);
11226 break;
11227 }
11228
11229 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11230 ctl_enqueue_rtr(io);
11231 break;
11232 default:
11233 __assert_unreachable();
11234 case CTL_ACTION_OVERLAP:
11235 ctl_set_overlapped_cmd(&io->scsiio);
11236 goto error;
11237 case CTL_ACTION_OVERLAP_TAG:
11238 ctl_set_overlapped_tag(&io->scsiio,
11239 io->scsiio.tag_num & 0xff);
11240 error:
11241 /* Serializing commands from the other SC are done here. */
11242 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) &&
11243 (softc->ha_mode != CTL_HA_MODE_XFER)) {
11244 ctl_try_unblock_others(lun, io, TRUE);
11245 LIST_REMOVE(&io->io_hdr, ooa_links);
11246
11247 ctl_copy_sense_data_back(io, &msg_info);
11248 msg_info.hdr.original_sc = io->io_hdr.remote_io;
11249 msg_info.hdr.serializing_sc = NULL;
11250 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
11251 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11252 sizeof(msg_info.scsi), M_WAITOK);
11253 ctl_free_io(io);
11254 break;
11255 }
11256
11257 ctl_done(io);
11258 break;
11259 }
11260 }
11261
11262 /*
11263 * Try to unblock I/Os blocked by the specified I/O.
11264 *
11265 * skip parameter allows explicitly skip the specified I/O as blocker,
11266 * starting from the previous one on the OOA queue. It can be used when
11267 * we know for sure that the specified I/O does no longer count (done).
11268 * It has to be still on OOA queue though so that we know where to start.
11269 */
11270 static void
ctl_try_unblock_others(struct ctl_lun * lun,union ctl_io * bio,bool skip)11271 ctl_try_unblock_others(struct ctl_lun *lun, union ctl_io *bio, bool skip)
11272 {
11273 union ctl_io *io, *next_io;
11274
11275 mtx_assert(&lun->lun_lock, MA_OWNED);
11276
11277 for (io = (union ctl_io *)TAILQ_FIRST(&bio->io_hdr.blocked_queue);
11278 io != NULL; io = next_io) {
11279 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, blocked_links);
11280
11281 KASSERT(io->io_hdr.blocker != NULL,
11282 ("I/O %p on blocked list without blocker", io));
11283 ctl_try_unblock_io(lun, io, skip);
11284 }
11285 KASSERT(!skip || TAILQ_EMPTY(&bio->io_hdr.blocked_queue),
11286 ("blocked_queue is not empty after skipping %p", bio));
11287 }
11288
11289 /*
11290 * This routine (with one exception) checks LUN flags that can be set by
11291 * commands ahead of us in the OOA queue. These flags have to be checked
11292 * when a command initially comes in, and when we pull a command off the
11293 * blocked queue and are preparing to execute it. The reason we have to
11294 * check these flags for commands on the blocked queue is that the LUN
11295 * state may have been changed by a command ahead of us while we're on the
11296 * blocked queue.
11297 *
11298 * Ordering is somewhat important with these checks, so please pay
11299 * careful attention to the placement of any new checks.
11300 */
11301 static int
ctl_scsiio_lun_check(struct ctl_lun * lun,const struct ctl_cmd_entry * entry,struct ctl_scsiio * ctsio)11302 ctl_scsiio_lun_check(struct ctl_lun *lun,
11303 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11304 {
11305 struct ctl_softc *softc = lun->ctl_softc;
11306 int retval;
11307 uint32_t residx;
11308
11309 retval = 0;
11310
11311 mtx_assert(&lun->lun_lock, MA_OWNED);
11312
11313 /*
11314 * If this shelf is a secondary shelf controller, we may have to
11315 * reject some commands disallowed by HA mode and link state.
11316 */
11317 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11318 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
11319 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11320 ctl_set_lun_unavail(ctsio);
11321 retval = 1;
11322 goto bailout;
11323 }
11324 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
11325 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11326 ctl_set_lun_transit(ctsio);
11327 retval = 1;
11328 goto bailout;
11329 }
11330 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
11331 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
11332 ctl_set_lun_standby(ctsio);
11333 retval = 1;
11334 goto bailout;
11335 }
11336
11337 /* The rest of checks are only done on executing side */
11338 if (softc->ha_mode == CTL_HA_MODE_XFER)
11339 goto bailout;
11340 }
11341
11342 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11343 if (lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
11344 ctl_set_hw_write_protected(ctsio);
11345 retval = 1;
11346 goto bailout;
11347 }
11348 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) {
11349 ctl_set_sense(ctsio, /*current_error*/ 1,
11350 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11351 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11352 retval = 1;
11353 goto bailout;
11354 }
11355 }
11356
11357 /*
11358 * Check for a reservation conflict. If this command isn't allowed
11359 * even on reserved LUNs, and if this initiator isn't the one who
11360 * reserved us, reject the command with a reservation conflict.
11361 */
11362 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11363 if ((lun->flags & CTL_LUN_RESERVED)
11364 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11365 if (lun->res_idx != residx) {
11366 ctl_set_reservation_conflict(ctsio);
11367 retval = 1;
11368 goto bailout;
11369 }
11370 }
11371
11372 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11373 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11374 /* No reservation or command is allowed. */;
11375 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11376 (lun->pr_res_type == SPR_TYPE_WR_EX ||
11377 lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
11378 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) {
11379 /* The command is allowed for Write Exclusive resv. */;
11380 } else {
11381 /*
11382 * if we aren't registered or it's a res holder type
11383 * reservation and this isn't the res holder then set a
11384 * conflict.
11385 */
11386 if (ctl_get_prkey(lun, residx) == 0 ||
11387 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) {
11388 ctl_set_reservation_conflict(ctsio);
11389 retval = 1;
11390 goto bailout;
11391 }
11392 }
11393
11394 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) {
11395 if (lun->flags & CTL_LUN_EJECTED)
11396 ctl_set_lun_ejected(ctsio);
11397 else if (lun->flags & CTL_LUN_NO_MEDIA) {
11398 if (lun->flags & CTL_LUN_REMOVABLE)
11399 ctl_set_lun_no_media(ctsio);
11400 else
11401 ctl_set_lun_int_reqd(ctsio);
11402 } else if (lun->flags & CTL_LUN_STOPPED)
11403 ctl_set_lun_stopped(ctsio);
11404 else
11405 goto bailout;
11406 retval = 1;
11407 goto bailout;
11408 }
11409
11410 bailout:
11411 return (retval);
11412 }
11413
11414 static void
ctl_failover_io(union ctl_io * io,int have_lock)11415 ctl_failover_io(union ctl_io *io, int have_lock)
11416 {
11417 ctl_set_busy(&io->scsiio);
11418 ctl_done(io);
11419 }
11420
11421 static void
ctl_failover_lun(union ctl_io * rio)11422 ctl_failover_lun(union ctl_io *rio)
11423 {
11424 struct ctl_softc *softc = CTL_SOFTC(rio);
11425 struct ctl_lun *lun;
11426 struct ctl_io_hdr *io, *next_io;
11427 uint32_t targ_lun;
11428
11429 targ_lun = rio->io_hdr.nexus.targ_mapped_lun;
11430 CTL_DEBUG_PRINT(("FAILOVER for lun %u\n", targ_lun));
11431
11432 /* Find and lock the LUN. */
11433 mtx_lock(&softc->ctl_lock);
11434 if (targ_lun > ctl_max_luns ||
11435 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11436 mtx_unlock(&softc->ctl_lock);
11437 return;
11438 }
11439 mtx_lock(&lun->lun_lock);
11440 mtx_unlock(&softc->ctl_lock);
11441 if (lun->flags & CTL_LUN_DISABLED) {
11442 mtx_unlock(&lun->lun_lock);
11443 return;
11444 }
11445
11446 if (softc->ha_mode == CTL_HA_MODE_XFER) {
11447 LIST_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11448 /* We are master */
11449 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11450 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11451 io->flags |= CTL_FLAG_ABORT |
11452 CTL_FLAG_FAILOVER;
11453 ctl_try_unblock_io(lun,
11454 (union ctl_io *)io, FALSE);
11455 } else { /* This can be only due to DATAMOVE */
11456 io->msg_type = CTL_MSG_DATAMOVE_DONE;
11457 io->flags &= ~CTL_FLAG_DMA_INPROG;
11458 io->flags |= CTL_FLAG_IO_ACTIVE;
11459 io->port_status = 31340;
11460 ctl_enqueue_isc((union ctl_io *)io);
11461 }
11462 } else
11463 /* We are slave */
11464 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11465 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11466 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11467 io->flags |= CTL_FLAG_FAILOVER;
11468 } else {
11469 ctl_set_busy(&((union ctl_io *)io)->
11470 scsiio);
11471 ctl_done((union ctl_io *)io);
11472 }
11473 }
11474 }
11475 } else { /* SERIALIZE modes */
11476 LIST_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11477 /* We are master */
11478 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11479 if (io->blocker != NULL) {
11480 TAILQ_REMOVE(&io->blocker->io_hdr.blocked_queue,
11481 io, blocked_links);
11482 io->blocker = NULL;
11483 }
11484 ctl_try_unblock_others(lun, (union ctl_io *)io,
11485 TRUE);
11486 LIST_REMOVE(io, ooa_links);
11487 ctl_free_io((union ctl_io *)io);
11488 } else
11489 /* We are slave */
11490 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11491 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11492 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
11493 ctl_set_busy(&((union ctl_io *)io)->
11494 scsiio);
11495 ctl_done((union ctl_io *)io);
11496 }
11497 }
11498 }
11499 }
11500 mtx_unlock(&lun->lun_lock);
11501 }
11502
11503 static void
ctl_scsiio_precheck(struct ctl_scsiio * ctsio)11504 ctl_scsiio_precheck(struct ctl_scsiio *ctsio)
11505 {
11506 struct ctl_softc *softc = CTL_SOFTC(ctsio);
11507 struct ctl_lun *lun;
11508 const struct ctl_cmd_entry *entry;
11509 union ctl_io *bio;
11510 uint32_t initidx, targ_lun;
11511
11512 lun = NULL;
11513 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11514 if (targ_lun < ctl_max_luns)
11515 lun = softc->ctl_luns[targ_lun];
11516 if (lun) {
11517 /*
11518 * If the LUN is invalid, pretend that it doesn't exist.
11519 * It will go away as soon as all pending I/O has been
11520 * completed.
11521 */
11522 mtx_lock(&lun->lun_lock);
11523 if (lun->flags & CTL_LUN_DISABLED) {
11524 mtx_unlock(&lun->lun_lock);
11525 lun = NULL;
11526 }
11527 }
11528 CTL_LUN(ctsio) = lun;
11529 if (lun) {
11530 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
11531
11532 /*
11533 * Every I/O goes into the OOA queue for a particular LUN,
11534 * and stays there until completion.
11535 */
11536 #ifdef CTL_TIME_IO
11537 if (LIST_EMPTY(&lun->ooa_queue))
11538 lun->idle_time += getsbinuptime() - lun->last_busy;
11539 #endif
11540 LIST_INSERT_HEAD(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
11541 }
11542
11543 /* Get command entry and return error if it is unsuppotyed. */
11544 entry = ctl_validate_command(ctsio);
11545 if (entry == NULL) {
11546 if (lun)
11547 mtx_unlock(&lun->lun_lock);
11548 return;
11549 }
11550
11551 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11552 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11553
11554 /*
11555 * Check to see whether we can send this command to LUNs that don't
11556 * exist. This should pretty much only be the case for inquiry
11557 * and request sense. Further checks, below, really require having
11558 * a LUN, so we can't really check the command anymore. Just put
11559 * it on the rtr queue.
11560 */
11561 if (lun == NULL) {
11562 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11563 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11564 ctl_enqueue_rtr((union ctl_io *)ctsio);
11565 return;
11566 }
11567
11568 ctl_set_unsupported_lun(ctsio);
11569 ctl_done((union ctl_io *)ctsio);
11570 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11571 return;
11572 } else {
11573 /*
11574 * Make sure we support this particular command on this LUN.
11575 * e.g., we don't support writes to the control LUN.
11576 */
11577 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11578 mtx_unlock(&lun->lun_lock);
11579 ctl_set_invalid_opcode(ctsio);
11580 ctl_done((union ctl_io *)ctsio);
11581 return;
11582 }
11583 }
11584
11585 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11586
11587 /*
11588 * If we've got a request sense, it'll clear the contingent
11589 * allegiance condition. Otherwise, if we have a CA condition for
11590 * this initiator, clear it, because it sent down a command other
11591 * than request sense.
11592 */
11593 if (ctsio->cdb[0] != REQUEST_SENSE) {
11594 struct scsi_sense_data *ps;
11595
11596 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
11597 if (ps != NULL)
11598 ps[initidx % CTL_MAX_INIT_PER_PORT].error_code = 0;
11599 }
11600
11601 /*
11602 * If the command has this flag set, it handles its own unit
11603 * attention reporting, we shouldn't do anything. Otherwise we
11604 * check for any pending unit attentions, and send them back to the
11605 * initiator. We only do this when a command initially comes in,
11606 * not when we pull it off the blocked queue.
11607 *
11608 * According to SAM-3, section 5.3.2, the order that things get
11609 * presented back to the host is basically unit attentions caused
11610 * by some sort of reset event, busy status, reservation conflicts
11611 * or task set full, and finally any other status.
11612 *
11613 * One issue here is that some of the unit attentions we report
11614 * don't fall into the "reset" category (e.g. "reported luns data
11615 * has changed"). So reporting it here, before the reservation
11616 * check, may be technically wrong. I guess the only thing to do
11617 * would be to check for and report the reset events here, and then
11618 * check for the other unit attention types after we check for a
11619 * reservation conflict.
11620 *
11621 * XXX KDM need to fix this
11622 */
11623 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11624 ctl_ua_type ua_type;
11625 u_int sense_len = 0;
11626
11627 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11628 &sense_len, SSD_TYPE_NONE);
11629 if (ua_type != CTL_UA_NONE) {
11630 mtx_unlock(&lun->lun_lock);
11631 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11632 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11633 ctsio->sense_len = sense_len;
11634 ctl_done((union ctl_io *)ctsio);
11635 return;
11636 }
11637 }
11638
11639 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11640 mtx_unlock(&lun->lun_lock);
11641 ctl_done((union ctl_io *)ctsio);
11642 return;
11643 }
11644
11645 /*
11646 * XXX CHD this is where we want to send IO to other side if
11647 * this LUN is secondary on this SC. We will need to make a copy
11648 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11649 * the copy we send as FROM_OTHER.
11650 * We also need to stuff the address of the original IO so we can
11651 * find it easily. Something similar will need be done on the other
11652 * side so when we are done we can find the copy.
11653 */
11654 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11655 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
11656 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
11657 union ctl_ha_msg msg_info;
11658 int isc_retval;
11659
11660 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11661 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11662 mtx_unlock(&lun->lun_lock);
11663
11664 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11665 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11666 msg_info.hdr.serializing_sc = NULL;
11667 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11668 msg_info.scsi.tag_num = ctsio->tag_num;
11669 msg_info.scsi.tag_type = ctsio->tag_type;
11670 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11671 msg_info.scsi.cdb_len = ctsio->cdb_len;
11672 msg_info.scsi.priority = ctsio->priority;
11673
11674 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11675 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11676 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11677 ctsio->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11678 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
11679 ctl_set_busy(ctsio);
11680 ctl_done((union ctl_io *)ctsio);
11681 return;
11682 }
11683 return;
11684 }
11685
11686 bio = (union ctl_io *)LIST_NEXT(&ctsio->io_hdr, ooa_links);
11687 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, &bio)) {
11688 case CTL_ACTION_PASS:
11689 case CTL_ACTION_SKIP:
11690 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11691 mtx_unlock(&lun->lun_lock);
11692 ctl_enqueue_rtr((union ctl_io *)ctsio);
11693 break;
11694 case CTL_ACTION_BLOCK:
11695 ctsio->io_hdr.blocker = bio;
11696 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctsio->io_hdr,
11697 blocked_links);
11698 mtx_unlock(&lun->lun_lock);
11699 break;
11700 case CTL_ACTION_OVERLAP:
11701 mtx_unlock(&lun->lun_lock);
11702 ctl_set_overlapped_cmd(ctsio);
11703 ctl_done((union ctl_io *)ctsio);
11704 break;
11705 case CTL_ACTION_OVERLAP_TAG:
11706 mtx_unlock(&lun->lun_lock);
11707 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11708 ctl_done((union ctl_io *)ctsio);
11709 break;
11710 default:
11711 __assert_unreachable();
11712 }
11713 }
11714
11715 const struct ctl_cmd_entry *
ctl_get_cmd_entry(struct ctl_scsiio * ctsio,int * sa)11716 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11717 {
11718 const struct ctl_cmd_entry *entry;
11719 int service_action;
11720
11721 entry = &ctl_cmd_table[ctsio->cdb[0]];
11722 if (sa)
11723 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11724 if (entry->flags & CTL_CMD_FLAG_SA5) {
11725 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11726 entry = &((const struct ctl_cmd_entry *)
11727 entry->execute)[service_action];
11728 }
11729 return (entry);
11730 }
11731
11732 const struct ctl_cmd_entry *
ctl_validate_command(struct ctl_scsiio * ctsio)11733 ctl_validate_command(struct ctl_scsiio *ctsio)
11734 {
11735 const struct ctl_cmd_entry *entry;
11736 int i, sa;
11737 uint8_t diff;
11738
11739 entry = ctl_get_cmd_entry(ctsio, &sa);
11740 ctsio->seridx = entry->seridx;
11741 if (entry->execute == NULL) {
11742 if (sa)
11743 ctl_set_invalid_field(ctsio,
11744 /*sks_valid*/ 1,
11745 /*command*/ 1,
11746 /*field*/ 1,
11747 /*bit_valid*/ 1,
11748 /*bit*/ 4);
11749 else
11750 ctl_set_invalid_opcode(ctsio);
11751 ctl_done((union ctl_io *)ctsio);
11752 return (NULL);
11753 }
11754 KASSERT(entry->length > 0,
11755 ("Not defined length for command 0x%02x/0x%02x",
11756 ctsio->cdb[0], ctsio->cdb[1]));
11757 for (i = 1; i < entry->length; i++) {
11758 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11759 if (diff == 0)
11760 continue;
11761 ctl_set_invalid_field(ctsio,
11762 /*sks_valid*/ 1,
11763 /*command*/ 1,
11764 /*field*/ i,
11765 /*bit_valid*/ 1,
11766 /*bit*/ fls(diff) - 1);
11767 ctl_done((union ctl_io *)ctsio);
11768 return (NULL);
11769 }
11770 return (entry);
11771 }
11772
11773 static int
ctl_cmd_applicable(uint8_t lun_type,const struct ctl_cmd_entry * entry)11774 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11775 {
11776
11777 switch (lun_type) {
11778 case T_DIRECT:
11779 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0)
11780 return (0);
11781 break;
11782 case T_PROCESSOR:
11783 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11784 return (0);
11785 break;
11786 case T_CDROM:
11787 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0)
11788 return (0);
11789 break;
11790 default:
11791 return (0);
11792 }
11793 return (1);
11794 }
11795
11796 static int
ctl_scsiio(struct ctl_scsiio * ctsio)11797 ctl_scsiio(struct ctl_scsiio *ctsio)
11798 {
11799 int retval;
11800 const struct ctl_cmd_entry *entry;
11801
11802 retval = CTL_RETVAL_COMPLETE;
11803
11804 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11805
11806 entry = ctl_get_cmd_entry(ctsio, NULL);
11807
11808 /*
11809 * If this I/O has been aborted, just send it straight to
11810 * ctl_done() without executing it.
11811 */
11812 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11813 ctl_done((union ctl_io *)ctsio);
11814 goto bailout;
11815 }
11816
11817 /*
11818 * All the checks should have been handled by ctl_scsiio_precheck().
11819 * We should be clear now to just execute the I/O.
11820 */
11821 retval = entry->execute(ctsio);
11822
11823 bailout:
11824 return (retval);
11825 }
11826
11827 static int
ctl_target_reset(union ctl_io * io)11828 ctl_target_reset(union ctl_io *io)
11829 {
11830 struct ctl_softc *softc = CTL_SOFTC(io);
11831 struct ctl_port *port = CTL_PORT(io);
11832 struct ctl_lun *lun;
11833 uint32_t initidx;
11834 ctl_ua_type ua_type;
11835
11836 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11837 union ctl_ha_msg msg_info;
11838
11839 msg_info.hdr.nexus = io->io_hdr.nexus;
11840 msg_info.task.task_action = io->taskio.task_action;
11841 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11842 msg_info.hdr.original_sc = NULL;
11843 msg_info.hdr.serializing_sc = NULL;
11844 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11845 sizeof(msg_info.task), M_WAITOK);
11846 }
11847
11848 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11849 if (io->taskio.task_action == CTL_TASK_TARGET_RESET)
11850 ua_type = CTL_UA_TARG_RESET;
11851 else
11852 ua_type = CTL_UA_BUS_RESET;
11853 mtx_lock(&softc->ctl_lock);
11854 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11855 if (port != NULL &&
11856 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
11857 continue;
11858 ctl_do_lun_reset(lun, initidx, ua_type);
11859 }
11860 mtx_unlock(&softc->ctl_lock);
11861 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11862 return (0);
11863 }
11864
11865 /*
11866 * The LUN should always be set. The I/O is optional, and is used to
11867 * distinguish between I/Os sent by this initiator, and by other
11868 * initiators. We set unit attention for initiators other than this one.
11869 * SAM-3 is vague on this point. It does say that a unit attention should
11870 * be established for other initiators when a LUN is reset (see section
11871 * 5.7.3), but it doesn't specifically say that the unit attention should
11872 * be established for this particular initiator when a LUN is reset. Here
11873 * is the relevant text, from SAM-3 rev 8:
11874 *
11875 * 5.7.2 When a SCSI initiator port aborts its own tasks
11876 *
11877 * When a SCSI initiator port causes its own task(s) to be aborted, no
11878 * notification that the task(s) have been aborted shall be returned to
11879 * the SCSI initiator port other than the completion response for the
11880 * command or task management function action that caused the task(s) to
11881 * be aborted and notification(s) associated with related effects of the
11882 * action (e.g., a reset unit attention condition).
11883 *
11884 * XXX KDM for now, we're setting unit attention for all initiators.
11885 */
11886 static void
ctl_do_lun_reset(struct ctl_lun * lun,uint32_t initidx,ctl_ua_type ua_type)11887 ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua_type)
11888 {
11889 struct ctl_io_hdr *xioh;
11890 int i;
11891
11892 mtx_lock(&lun->lun_lock);
11893 /* Abort tasks. */
11894 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) {
11895 xioh->flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11896 ctl_try_unblock_io(lun, (union ctl_io *)xioh, FALSE);
11897 }
11898 /* Clear CA. */
11899 for (i = 0; i < ctl_max_ports; i++) {
11900 free(lun->pending_sense[i], M_CTL);
11901 lun->pending_sense[i] = NULL;
11902 }
11903 /* Clear reservation. */
11904 lun->flags &= ~CTL_LUN_RESERVED;
11905 /* Clear prevent media removal. */
11906 if (lun->prevent) {
11907 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11908 ctl_clear_mask(lun->prevent, i);
11909 lun->prevent_count = 0;
11910 }
11911 /* Clear TPC status */
11912 ctl_tpc_lun_clear(lun, -1);
11913 /* Establish UA. */
11914 #if 0
11915 ctl_est_ua_all(lun, initidx, ua_type);
11916 #else
11917 ctl_est_ua_all(lun, -1, ua_type);
11918 #endif
11919 mtx_unlock(&lun->lun_lock);
11920 }
11921
11922 static int
ctl_lun_reset(union ctl_io * io)11923 ctl_lun_reset(union ctl_io *io)
11924 {
11925 struct ctl_softc *softc = CTL_SOFTC(io);
11926 struct ctl_lun *lun;
11927 uint32_t targ_lun, initidx;
11928
11929 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11930 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11931 mtx_lock(&softc->ctl_lock);
11932 if (targ_lun >= ctl_max_luns ||
11933 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11934 mtx_unlock(&softc->ctl_lock);
11935 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11936 return (1);
11937 }
11938 ctl_do_lun_reset(lun, initidx, CTL_UA_LUN_RESET);
11939 mtx_unlock(&softc->ctl_lock);
11940 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11941
11942 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
11943 union ctl_ha_msg msg_info;
11944
11945 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11946 msg_info.hdr.nexus = io->io_hdr.nexus;
11947 msg_info.task.task_action = CTL_TASK_LUN_RESET;
11948 msg_info.hdr.original_sc = NULL;
11949 msg_info.hdr.serializing_sc = NULL;
11950 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11951 sizeof(msg_info.task), M_WAITOK);
11952 }
11953 return (0);
11954 }
11955
11956 static void
ctl_abort_tasks_lun(struct ctl_lun * lun,uint32_t targ_port,uint32_t init_id,int other_sc)11957 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11958 int other_sc)
11959 {
11960 struct ctl_io_hdr *xioh;
11961
11962 mtx_assert(&lun->lun_lock, MA_OWNED);
11963
11964 /*
11965 * Run through the OOA queue and attempt to find the given I/O.
11966 * The target port, initiator ID, tag type and tag number have to
11967 * match the values that we got from the initiator. If we have an
11968 * untagged command to abort, simply abort the first untagged command
11969 * we come to. We only allow one untagged command at a time of course.
11970 */
11971 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) {
11972 union ctl_io *xio = (union ctl_io *)xioh;
11973 if ((targ_port == UINT32_MAX ||
11974 targ_port == xioh->nexus.targ_port) &&
11975 (init_id == UINT32_MAX ||
11976 init_id == xioh->nexus.initid)) {
11977 if (targ_port != xioh->nexus.targ_port ||
11978 init_id != xioh->nexus.initid)
11979 xioh->flags |= CTL_FLAG_ABORT_STATUS;
11980 xioh->flags |= CTL_FLAG_ABORT;
11981 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11982 union ctl_ha_msg msg_info;
11983
11984 msg_info.hdr.nexus = xioh->nexus;
11985 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11986 msg_info.task.tag_num = xio->scsiio.tag_num;
11987 msg_info.task.tag_type = xio->scsiio.tag_type;
11988 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11989 msg_info.hdr.original_sc = NULL;
11990 msg_info.hdr.serializing_sc = NULL;
11991 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11992 sizeof(msg_info.task), M_NOWAIT);
11993 }
11994 ctl_try_unblock_io(lun, xio, FALSE);
11995 }
11996 }
11997 }
11998
11999 static int
ctl_abort_task_set(union ctl_io * io)12000 ctl_abort_task_set(union ctl_io *io)
12001 {
12002 struct ctl_softc *softc = CTL_SOFTC(io);
12003 struct ctl_lun *lun;
12004 uint32_t targ_lun;
12005
12006 /*
12007 * Look up the LUN.
12008 */
12009 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12010 mtx_lock(&softc->ctl_lock);
12011 if (targ_lun >= ctl_max_luns ||
12012 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12013 mtx_unlock(&softc->ctl_lock);
12014 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12015 return (1);
12016 }
12017
12018 mtx_lock(&lun->lun_lock);
12019 mtx_unlock(&softc->ctl_lock);
12020 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
12021 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12022 io->io_hdr.nexus.initid,
12023 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12024 } else { /* CTL_TASK_CLEAR_TASK_SET */
12025 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
12026 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12027 }
12028 mtx_unlock(&lun->lun_lock);
12029 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12030 return (0);
12031 }
12032
12033 static void
ctl_i_t_nexus_loss(struct ctl_softc * softc,uint32_t initidx,ctl_ua_type ua_type)12034 ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx,
12035 ctl_ua_type ua_type)
12036 {
12037 struct ctl_lun *lun;
12038 struct scsi_sense_data *ps;
12039 uint32_t p, i;
12040
12041 p = initidx / CTL_MAX_INIT_PER_PORT;
12042 i = initidx % CTL_MAX_INIT_PER_PORT;
12043 mtx_lock(&softc->ctl_lock);
12044 STAILQ_FOREACH(lun, &softc->lun_list, links) {
12045 mtx_lock(&lun->lun_lock);
12046 /* Abort tasks. */
12047 ctl_abort_tasks_lun(lun, p, i, 1);
12048 /* Clear CA. */
12049 ps = lun->pending_sense[p];
12050 if (ps != NULL)
12051 ps[i].error_code = 0;
12052 /* Clear reservation. */
12053 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
12054 lun->flags &= ~CTL_LUN_RESERVED;
12055 /* Clear prevent media removal. */
12056 if (lun->prevent && ctl_is_set(lun->prevent, initidx)) {
12057 ctl_clear_mask(lun->prevent, initidx);
12058 lun->prevent_count--;
12059 }
12060 /* Clear TPC status */
12061 ctl_tpc_lun_clear(lun, initidx);
12062 /* Establish UA. */
12063 ctl_est_ua(lun, initidx, ua_type);
12064 mtx_unlock(&lun->lun_lock);
12065 }
12066 mtx_unlock(&softc->ctl_lock);
12067 }
12068
12069 static int
ctl_i_t_nexus_reset(union ctl_io * io)12070 ctl_i_t_nexus_reset(union ctl_io *io)
12071 {
12072 struct ctl_softc *softc = CTL_SOFTC(io);
12073 uint32_t initidx;
12074
12075 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12076 union ctl_ha_msg msg_info;
12077
12078 msg_info.hdr.nexus = io->io_hdr.nexus;
12079 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
12080 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12081 msg_info.hdr.original_sc = NULL;
12082 msg_info.hdr.serializing_sc = NULL;
12083 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12084 sizeof(msg_info.task), M_WAITOK);
12085 }
12086
12087 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12088 ctl_i_t_nexus_loss(softc, initidx, CTL_UA_I_T_NEXUS_LOSS);
12089 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12090 return (0);
12091 }
12092
12093 static int
ctl_abort_task(union ctl_io * io)12094 ctl_abort_task(union ctl_io *io)
12095 {
12096 struct ctl_softc *softc = CTL_SOFTC(io);
12097 struct ctl_io_hdr *xioh;
12098 struct ctl_lun *lun;
12099 uint32_t targ_lun;
12100
12101 /*
12102 * Look up the LUN.
12103 */
12104 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12105 mtx_lock(&softc->ctl_lock);
12106 if (targ_lun >= ctl_max_luns ||
12107 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12108 mtx_unlock(&softc->ctl_lock);
12109 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12110 return (1);
12111 }
12112
12113 mtx_lock(&lun->lun_lock);
12114 mtx_unlock(&softc->ctl_lock);
12115 /*
12116 * Run through the OOA queue and attempt to find the given I/O.
12117 * The target port, initiator ID, tag type and tag number have to
12118 * match the values that we got from the initiator. If we have an
12119 * untagged command to abort, simply abort the first untagged command
12120 * we come to. We only allow one untagged command at a time of course.
12121 */
12122 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) {
12123 union ctl_io *xio = (union ctl_io *)xioh;
12124 if ((xioh->nexus.targ_port != io->io_hdr.nexus.targ_port)
12125 || (xioh->nexus.initid != io->io_hdr.nexus.initid)
12126 || (xioh->flags & CTL_FLAG_ABORT))
12127 continue;
12128
12129 /*
12130 * If the abort says that the task is untagged, the
12131 * task in the queue must be untagged. Otherwise,
12132 * we just check to see whether the tag numbers
12133 * match. This is because the QLogic firmware
12134 * doesn't pass back the tag type in an abort
12135 * request.
12136 */
12137 #if 0
12138 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12139 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12140 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
12141 #else
12142 /*
12143 * XXX KDM we've got problems with FC, because it
12144 * doesn't send down a tag type with aborts. So we
12145 * can only really go by the tag number...
12146 * This may cause problems with parallel SCSI.
12147 * Need to figure that out!!
12148 */
12149 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12150 #endif
12151 xioh->flags |= CTL_FLAG_ABORT;
12152 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
12153 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12154 union ctl_ha_msg msg_info;
12155
12156 msg_info.hdr.nexus = io->io_hdr.nexus;
12157 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12158 msg_info.task.tag_num = io->taskio.tag_num;
12159 msg_info.task.tag_type = io->taskio.tag_type;
12160 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12161 msg_info.hdr.original_sc = NULL;
12162 msg_info.hdr.serializing_sc = NULL;
12163 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12164 sizeof(msg_info.task), M_NOWAIT);
12165 }
12166 ctl_try_unblock_io(lun, xio, FALSE);
12167 }
12168 }
12169 mtx_unlock(&lun->lun_lock);
12170 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12171 return (0);
12172 }
12173
12174 static int
12175 ctl_query_task(union ctl_io *io, int task_set)
12176 {
12177 struct ctl_softc *softc = CTL_SOFTC(io);
12178 struct ctl_io_hdr *xioh;
12179 struct ctl_lun *lun;
12180 int found = 0;
12181 uint32_t targ_lun;
12182
12183 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12184 mtx_lock(&softc->ctl_lock);
12185 if (targ_lun >= ctl_max_luns ||
12186 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12187 mtx_unlock(&softc->ctl_lock);
12188 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12189 return (1);
12190 }
12191 mtx_lock(&lun->lun_lock);
12192 mtx_unlock(&softc->ctl_lock);
12193 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) {
12194 union ctl_io *xio = (union ctl_io *)xioh;
12195 if ((xioh->nexus.targ_port != io->io_hdr.nexus.targ_port)
12196 || (xioh->nexus.initid != io->io_hdr.nexus.initid)
12197 || (xioh->flags & CTL_FLAG_ABORT))
12198 continue;
12199
12200 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
12201 found = 1;
12202 break;
12203 }
12204 }
12205 mtx_unlock(&lun->lun_lock);
12206 if (found)
12207 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12208 else
12209 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12210 return (0);
12211 }
12212
12213 static int
12214 ctl_query_async_event(union ctl_io *io)
12215 {
12216 struct ctl_softc *softc = CTL_SOFTC(io);
12217 struct ctl_lun *lun;
12218 ctl_ua_type ua;
12219 uint32_t targ_lun, initidx;
12220
12221 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12222 mtx_lock(&softc->ctl_lock);
12223 if (targ_lun >= ctl_max_luns ||
12224 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12225 mtx_unlock(&softc->ctl_lock);
12226 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12227 return (1);
12228 }
12229 mtx_lock(&lun->lun_lock);
12230 mtx_unlock(&softc->ctl_lock);
12231 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12232 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
12233 mtx_unlock(&lun->lun_lock);
12234 if (ua != CTL_UA_NONE)
12235 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12236 else
12237 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12238 return (0);
12239 }
12240
12241 static void
12242 ctl_run_task(union ctl_io *io)
12243 {
12244 int retval = 1;
12245
12246 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12247 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12248 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
12249 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
12250 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
12251 switch (io->taskio.task_action) {
12252 case CTL_TASK_ABORT_TASK:
12253 retval = ctl_abort_task(io);
12254 break;
12255 case CTL_TASK_ABORT_TASK_SET:
12256 case CTL_TASK_CLEAR_TASK_SET:
12257 retval = ctl_abort_task_set(io);
12258 break;
12259 case CTL_TASK_CLEAR_ACA:
12260 break;
12261 case CTL_TASK_I_T_NEXUS_RESET:
12262 retval = ctl_i_t_nexus_reset(io);
12263 break;
12264 case CTL_TASK_LUN_RESET:
12265 retval = ctl_lun_reset(io);
12266 break;
12267 case CTL_TASK_TARGET_RESET:
12268 case CTL_TASK_BUS_RESET:
12269 retval = ctl_target_reset(io);
12270 break;
12271 case CTL_TASK_PORT_LOGIN:
12272 break;
12273 case CTL_TASK_PORT_LOGOUT:
12274 break;
12275 case CTL_TASK_QUERY_TASK:
12276 retval = ctl_query_task(io, 0);
12277 break;
12278 case CTL_TASK_QUERY_TASK_SET:
12279 retval = ctl_query_task(io, 1);
12280 break;
12281 case CTL_TASK_QUERY_ASYNC_EVENT:
12282 retval = ctl_query_async_event(io);
12283 break;
12284 default:
12285 printf("%s: got unknown task management event %d\n",
12286 __func__, io->taskio.task_action);
12287 break;
12288 }
12289 if (retval == 0)
12290 io->io_hdr.status = CTL_SUCCESS;
12291 else
12292 io->io_hdr.status = CTL_ERROR;
12293 ctl_done(io);
12294 }
12295
12296 /*
12297 * For HA operation. Handle commands that come in from the other
12298 * controller.
12299 */
12300 static void
12301 ctl_handle_isc(union ctl_io *io)
12302 {
12303 struct ctl_softc *softc = CTL_SOFTC(io);
12304 struct ctl_lun *lun;
12305 const struct ctl_cmd_entry *entry;
12306 uint32_t targ_lun;
12307
12308 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12309 switch (io->io_hdr.msg_type) {
12310 case CTL_MSG_SERIALIZE:
12311 ctl_serialize_other_sc_cmd(&io->scsiio);
12312 break;
12313 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */
12314 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12315 if (targ_lun >= ctl_max_luns ||
12316 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12317 ctl_done(io);
12318 break;
12319 }
12320 mtx_lock(&lun->lun_lock);
12321 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) {
12322 mtx_unlock(&lun->lun_lock);
12323 ctl_done(io);
12324 break;
12325 }
12326 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12327 mtx_unlock(&lun->lun_lock);
12328 ctl_enqueue_rtr(io);
12329 break;
12330 case CTL_MSG_FINISH_IO:
12331 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12332 ctl_done(io);
12333 break;
12334 }
12335 if (targ_lun >= ctl_max_luns ||
12336 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12337 ctl_free_io(io);
12338 break;
12339 }
12340 mtx_lock(&lun->lun_lock);
12341 ctl_try_unblock_others(lun, io, TRUE);
12342 LIST_REMOVE(&io->io_hdr, ooa_links);
12343 mtx_unlock(&lun->lun_lock);
12344 ctl_free_io(io);
12345 break;
12346 case CTL_MSG_PERS_ACTION:
12347 ctl_hndl_per_res_out_on_other_sc(io);
12348 ctl_free_io(io);
12349 break;
12350 case CTL_MSG_BAD_JUJU:
12351 ctl_done(io);
12352 break;
12353 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */
12354 ctl_datamove_remote(io);
12355 break;
12356 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */
12357 ctl_datamove_done(io, false);
12358 break;
12359 case CTL_MSG_FAILOVER:
12360 ctl_failover_lun(io);
12361 ctl_free_io(io);
12362 break;
12363 default:
12364 printf("%s: Invalid message type %d\n",
12365 __func__, io->io_hdr.msg_type);
12366 ctl_free_io(io);
12367 break;
12368 }
12369
12370 }
12371
12372 /*
12373 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12374 * there is no match.
12375 */
12376 static ctl_lun_error_pattern
12377 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12378 {
12379 const struct ctl_cmd_entry *entry;
12380 ctl_lun_error_pattern filtered_pattern, pattern;
12381
12382 pattern = desc->error_pattern;
12383
12384 /*
12385 * XXX KDM we need more data passed into this function to match a
12386 * custom pattern, and we actually need to implement custom pattern
12387 * matching.
12388 */
12389 if (pattern & CTL_LUN_PAT_CMD)
12390 return (CTL_LUN_PAT_CMD);
12391
12392 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12393 return (CTL_LUN_PAT_ANY);
12394
12395 entry = ctl_get_cmd_entry(ctsio, NULL);
12396
12397 filtered_pattern = entry->pattern & pattern;
12398
12399 /*
12400 * If the user requested specific flags in the pattern (e.g.
12401 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12402 * flags.
12403 *
12404 * If the user did not specify any flags, it doesn't matter whether
12405 * or not the command supports the flags.
12406 */
12407 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12408 (pattern & ~CTL_LUN_PAT_MASK))
12409 return (CTL_LUN_PAT_NONE);
12410
12411 /*
12412 * If the user asked for a range check, see if the requested LBA
12413 * range overlaps with this command's LBA range.
12414 */
12415 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12416 uint64_t lba1;
12417 uint64_t len1;
12418 ctl_action action;
12419 int retval;
12420
12421 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12422 if (retval != 0)
12423 return (CTL_LUN_PAT_NONE);
12424
12425 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12426 desc->lba_range.len, FALSE);
12427 /*
12428 * A "pass" means that the LBA ranges don't overlap, so
12429 * this doesn't match the user's range criteria.
12430 */
12431 if (action == CTL_ACTION_PASS)
12432 return (CTL_LUN_PAT_NONE);
12433 }
12434
12435 return (filtered_pattern);
12436 }
12437
12438 static void
12439 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12440 {
12441 struct ctl_error_desc *desc, *desc2;
12442
12443 mtx_assert(&lun->lun_lock, MA_OWNED);
12444
12445 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12446 ctl_lun_error_pattern pattern;
12447 /*
12448 * Check to see whether this particular command matches
12449 * the pattern in the descriptor.
12450 */
12451 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12452 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12453 continue;
12454
12455 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12456 case CTL_LUN_INJ_ABORTED:
12457 ctl_set_aborted(&io->scsiio);
12458 break;
12459 case CTL_LUN_INJ_MEDIUM_ERR:
12460 ctl_set_medium_error(&io->scsiio,
12461 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
12462 CTL_FLAG_DATA_OUT);
12463 break;
12464 case CTL_LUN_INJ_UA:
12465 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12466 * OCCURRED */
12467 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12468 break;
12469 case CTL_LUN_INJ_CUSTOM:
12470 /*
12471 * We're assuming the user knows what he is doing.
12472 * Just copy the sense information without doing
12473 * checks.
12474 */
12475 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12476 MIN(sizeof(desc->custom_sense),
12477 sizeof(io->scsiio.sense_data)));
12478 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12479 io->scsiio.sense_len = SSD_FULL_SIZE;
12480 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12481 break;
12482 case CTL_LUN_INJ_NONE:
12483 default:
12484 /*
12485 * If this is an error injection type we don't know
12486 * about, clear the continuous flag (if it is set)
12487 * so it will get deleted below.
12488 */
12489 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12490 break;
12491 }
12492 /*
12493 * By default, each error injection action is a one-shot
12494 */
12495 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12496 continue;
12497
12498 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12499
12500 free(desc, M_CTL);
12501 }
12502 }
12503
12504 #ifdef CTL_IO_DELAY
12505 static void
12506 ctl_datamove_timer_wakeup(void *arg)
12507 {
12508 union ctl_io *io;
12509
12510 io = (union ctl_io *)arg;
12511
12512 ctl_datamove(io);
12513 }
12514 #endif /* CTL_IO_DELAY */
12515
12516 static void
12517 ctl_datamove_done_process(union ctl_io *io)
12518 {
12519 #ifdef CTL_TIME_IO
12520 struct bintime cur_bt;
12521 #endif
12522
12523 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
12524 ("%s: unexpected I/O type %x", __func__, io->io_hdr.io_type));
12525
12526 #ifdef CTL_TIME_IO
12527 getbinuptime(&cur_bt);
12528 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12529 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12530 #endif
12531 io->io_hdr.num_dmas++;
12532
12533 if ((io->io_hdr.port_status != 0) &&
12534 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
12535 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
12536 ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1,
12537 /*retry_count*/ io->io_hdr.port_status);
12538 } else if (io->scsiio.kern_data_resid != 0 &&
12539 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT &&
12540 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
12541 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
12542 ctl_set_invalid_field_ciu(&io->scsiio);
12543 } else if (ctl_debug & CTL_DEBUG_CDB_DATA)
12544 ctl_data_print(io);
12545 }
12546
12547 void
12548 ctl_datamove_done(union ctl_io *io, bool samethr)
12549 {
12550
12551 ctl_datamove_done_process(io);
12552 io->scsiio.be_move_done(io, samethr);
12553 }
12554
12555 void
12556 ctl_datamove(union ctl_io *io)
12557 {
12558 void (*fe_datamove)(union ctl_io *io);
12559
12560 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12561
12562 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12563
12564 /* No data transferred yet. Frontend must update this when done. */
12565 io->scsiio.kern_data_resid = io->scsiio.kern_data_len;
12566
12567 #ifdef CTL_TIME_IO
12568 getbinuptime(&io->io_hdr.dma_start_bt);
12569 #endif /* CTL_TIME_IO */
12570
12571 #ifdef CTL_IO_DELAY
12572 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12573 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12574 } else {
12575 struct ctl_lun *lun;
12576
12577 lun = CTL_LUN(io);
12578 if ((lun != NULL)
12579 && (lun->delay_info.datamove_delay > 0)) {
12580 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12581 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12582 callout_reset(&io->io_hdr.delay_callout,
12583 lun->delay_info.datamove_delay * hz,
12584 ctl_datamove_timer_wakeup, io);
12585 if (lun->delay_info.datamove_type ==
12586 CTL_DELAY_TYPE_ONESHOT)
12587 lun->delay_info.datamove_delay = 0;
12588 return;
12589 }
12590 }
12591 #endif
12592
12593 /*
12594 * This command has been aborted. Set the port status, so we fail
12595 * the data move.
12596 */
12597 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12598 printf("ctl_datamove: tag 0x%jx on (%u:%u:%u) aborted\n",
12599 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12600 io->io_hdr.nexus.targ_port,
12601 io->io_hdr.nexus.targ_lun);
12602 io->io_hdr.port_status = 31337;
12603 ctl_datamove_done_process(io);
12604 io->scsiio.be_move_done(io, true);
12605 return;
12606 }
12607
12608 /* Don't confuse frontend with zero length data move. */
12609 if (io->scsiio.kern_data_len == 0) {
12610 ctl_datamove_done_process(io);
12611 io->scsiio.be_move_done(io, true);
12612 return;
12613 }
12614
12615 fe_datamove = CTL_PORT(io)->fe_datamove;
12616 fe_datamove(io);
12617 }
12618
12619 static void
12620 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12621 {
12622 union ctl_ha_msg msg;
12623 #ifdef CTL_TIME_IO
12624 struct bintime cur_bt;
12625 #endif
12626
12627 memset(&msg, 0, sizeof(msg));
12628 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12629 msg.hdr.original_sc = io;
12630 msg.hdr.serializing_sc = io->io_hdr.remote_io;
12631 msg.hdr.nexus = io->io_hdr.nexus;
12632 msg.hdr.status = io->io_hdr.status;
12633 msg.scsi.kern_data_resid = io->scsiio.kern_data_resid;
12634 msg.scsi.tag_num = io->scsiio.tag_num;
12635 msg.scsi.tag_type = io->scsiio.tag_type;
12636 msg.scsi.scsi_status = io->scsiio.scsi_status;
12637 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12638 io->scsiio.sense_len);
12639 msg.scsi.sense_len = io->scsiio.sense_len;
12640 msg.scsi.port_status = io->io_hdr.port_status;
12641 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12642 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12643 ctl_failover_io(io, /*have_lock*/ have_lock);
12644 return;
12645 }
12646 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12647 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12648 msg.scsi.sense_len, M_WAITOK);
12649
12650 #ifdef CTL_TIME_IO
12651 getbinuptime(&cur_bt);
12652 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12653 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12654 #endif
12655 io->io_hdr.num_dmas++;
12656 }
12657
12658 /*
12659 * The DMA to the remote side is done, now we need to tell the other side
12660 * we're done so it can continue with its data movement.
12661 */
12662 static void
12663 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12664 {
12665 union ctl_io *io;
12666 uint32_t i;
12667
12668 io = rq->context;
12669
12670 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12671 printf("%s: ISC DMA write failed with error %d", __func__,
12672 rq->ret);
12673 ctl_set_internal_failure(&io->scsiio,
12674 /*sks_valid*/ 1,
12675 /*retry_count*/ rq->ret);
12676 }
12677
12678 ctl_dt_req_free(rq);
12679
12680 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12681 free(CTL_LSGLT(io)[i].addr, M_CTL);
12682 free(CTL_RSGL(io), M_CTL);
12683 CTL_RSGL(io) = NULL;
12684 CTL_LSGL(io) = NULL;
12685
12686 /*
12687 * The data is in local and remote memory, so now we need to send
12688 * status (good or back) back to the other side.
12689 */
12690 ctl_send_datamove_done(io, /*have_lock*/ 0);
12691 }
12692
12693 /*
12694 * We've moved the data from the host/controller into local memory. Now we
12695 * need to push it over to the remote controller's memory.
12696 */
12697 static int
12698 ctl_datamove_remote_dm_write_cb(union ctl_io *io, bool samethr)
12699 {
12700 int retval;
12701
12702 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12703 ctl_datamove_remote_write_cb);
12704 return (retval);
12705 }
12706
12707 static void
12708 ctl_datamove_remote_write(union ctl_io *io)
12709 {
12710 int retval;
12711 void (*fe_datamove)(union ctl_io *io);
12712
12713 /*
12714 * - Get the data from the host/HBA into local memory.
12715 * - DMA memory from the local controller to the remote controller.
12716 * - Send status back to the remote controller.
12717 */
12718
12719 retval = ctl_datamove_remote_sgl_setup(io);
12720 if (retval != 0)
12721 return;
12722
12723 /* Switch the pointer over so the FETD knows what to do */
12724 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io);
12725
12726 /*
12727 * Use a custom move done callback, since we need to send completion
12728 * back to the other controller, not to the backend on this side.
12729 */
12730 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12731
12732 fe_datamove = CTL_PORT(io)->fe_datamove;
12733 fe_datamove(io);
12734 }
12735
12736 static int
12737 ctl_datamove_remote_dm_read_cb(union ctl_io *io, bool samethr)
12738 {
12739 uint32_t i;
12740
12741 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12742 free(CTL_LSGLT(io)[i].addr, M_CTL);
12743 free(CTL_RSGL(io), M_CTL);
12744 CTL_RSGL(io) = NULL;
12745 CTL_LSGL(io) = NULL;
12746
12747 /*
12748 * The read is done, now we need to send status (good or bad) back
12749 * to the other side.
12750 */
12751 ctl_send_datamove_done(io, /*have_lock*/ 0);
12752
12753 return (0);
12754 }
12755
12756 static void
12757 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12758 {
12759 union ctl_io *io;
12760 void (*fe_datamove)(union ctl_io *io);
12761
12762 io = rq->context;
12763
12764 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12765 printf("%s: ISC DMA read failed with error %d\n", __func__,
12766 rq->ret);
12767 ctl_set_internal_failure(&io->scsiio,
12768 /*sks_valid*/ 1,
12769 /*retry_count*/ rq->ret);
12770 }
12771
12772 ctl_dt_req_free(rq);
12773
12774 /* Switch the pointer over so the FETD knows what to do */
12775 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io);
12776
12777 /*
12778 * Use a custom move done callback, since we need to send completion
12779 * back to the other controller, not to the backend on this side.
12780 */
12781 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12782
12783 /* XXX KDM add checks like the ones in ctl_datamove? */
12784
12785 fe_datamove = CTL_PORT(io)->fe_datamove;
12786 fe_datamove(io);
12787 }
12788
12789 static int
12790 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12791 {
12792 struct ctl_sg_entry *local_sglist;
12793 uint32_t len_to_go;
12794 int retval;
12795 int i;
12796
12797 retval = 0;
12798 local_sglist = CTL_LSGL(io);
12799 len_to_go = io->scsiio.kern_data_len;
12800
12801 /*
12802 * The difficult thing here is that the size of the various
12803 * S/G segments may be different than the size from the
12804 * remote controller. That'll make it harder when DMAing
12805 * the data back to the other side.
12806 */
12807 for (i = 0; len_to_go > 0; i++) {
12808 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12809 local_sglist[i].addr =
12810 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12811
12812 len_to_go -= local_sglist[i].len;
12813 }
12814 /*
12815 * Reset the number of S/G entries accordingly. The original
12816 * number of S/G entries is available in rem_sg_entries.
12817 */
12818 io->scsiio.kern_sg_entries = i;
12819
12820 return (retval);
12821 }
12822
12823 static int
12824 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12825 ctl_ha_dt_cb callback)
12826 {
12827 struct ctl_ha_dt_req *rq;
12828 struct ctl_sg_entry *remote_sglist, *local_sglist;
12829 uint32_t local_used, remote_used, total_used;
12830 int i, j, isc_ret;
12831
12832 rq = ctl_dt_req_alloc();
12833
12834 /*
12835 * If we failed to allocate the request, and if the DMA didn't fail
12836 * anyway, set busy status. This is just a resource allocation
12837 * failure.
12838 */
12839 if ((rq == NULL)
12840 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12841 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12842 ctl_set_busy(&io->scsiio);
12843
12844 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12845 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12846 if (rq != NULL)
12847 ctl_dt_req_free(rq);
12848
12849 /*
12850 * The data move failed. We need to return status back
12851 * to the other controller. No point in trying to DMA
12852 * data to the remote controller.
12853 */
12854
12855 ctl_send_datamove_done(io, /*have_lock*/ 0);
12856
12857 return (1);
12858 }
12859
12860 local_sglist = CTL_LSGL(io);
12861 remote_sglist = CTL_RSGL(io);
12862 local_used = 0;
12863 remote_used = 0;
12864 total_used = 0;
12865
12866 /*
12867 * Pull/push the data over the wire from/to the other controller.
12868 * This takes into account the possibility that the local and
12869 * remote sglists may not be identical in terms of the size of
12870 * the elements and the number of elements.
12871 *
12872 * One fundamental assumption here is that the length allocated for
12873 * both the local and remote sglists is identical. Otherwise, we've
12874 * essentially got a coding error of some sort.
12875 */
12876 isc_ret = CTL_HA_STATUS_SUCCESS;
12877 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12878 uint32_t cur_len;
12879 uint8_t *tmp_ptr;
12880
12881 rq->command = command;
12882 rq->context = io;
12883
12884 /*
12885 * Both pointers should be aligned. But it is possible
12886 * that the allocation length is not. They should both
12887 * also have enough slack left over at the end, though,
12888 * to round up to the next 8 byte boundary.
12889 */
12890 cur_len = MIN(local_sglist[i].len - local_used,
12891 remote_sglist[j].len - remote_used);
12892 rq->size = cur_len;
12893
12894 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12895 tmp_ptr += local_used;
12896
12897 #if 0
12898 /* Use physical addresses when talking to ISC hardware */
12899 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12900 /* XXX KDM use busdma */
12901 rq->local = vtophys(tmp_ptr);
12902 } else
12903 rq->local = tmp_ptr;
12904 #else
12905 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12906 ("HA does not support BUS_ADDR"));
12907 rq->local = tmp_ptr;
12908 #endif
12909
12910 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12911 tmp_ptr += remote_used;
12912 rq->remote = tmp_ptr;
12913
12914 rq->callback = NULL;
12915
12916 local_used += cur_len;
12917 if (local_used >= local_sglist[i].len) {
12918 i++;
12919 local_used = 0;
12920 }
12921
12922 remote_used += cur_len;
12923 if (remote_used >= remote_sglist[j].len) {
12924 j++;
12925 remote_used = 0;
12926 }
12927 total_used += cur_len;
12928
12929 if (total_used >= io->scsiio.kern_data_len)
12930 rq->callback = callback;
12931
12932 isc_ret = ctl_dt_single(rq);
12933 if (isc_ret > CTL_HA_STATUS_SUCCESS)
12934 break;
12935 }
12936 if (isc_ret != CTL_HA_STATUS_WAIT) {
12937 rq->ret = isc_ret;
12938 callback(rq);
12939 }
12940
12941 return (0);
12942 }
12943
12944 static void
12945 ctl_datamove_remote_read(union ctl_io *io)
12946 {
12947 int retval;
12948 uint32_t i;
12949
12950 /*
12951 * This will send an error to the other controller in the case of a
12952 * failure.
12953 */
12954 retval = ctl_datamove_remote_sgl_setup(io);
12955 if (retval != 0)
12956 return;
12957
12958 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12959 ctl_datamove_remote_read_cb);
12960 if (retval != 0) {
12961 /*
12962 * Make sure we free memory if there was an error.. The
12963 * ctl_datamove_remote_xfer() function will send the
12964 * datamove done message, or call the callback with an
12965 * error if there is a problem.
12966 */
12967 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12968 free(CTL_LSGLT(io)[i].addr, M_CTL);
12969 free(CTL_RSGL(io), M_CTL);
12970 CTL_RSGL(io) = NULL;
12971 CTL_LSGL(io) = NULL;
12972 }
12973 }
12974
12975 /*
12976 * Process a datamove request from the other controller. This is used for
12977 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12978 * first. Once that is complete, the data gets DMAed into the remote
12979 * controller's memory. For reads, we DMA from the remote controller's
12980 * memory into our memory first, and then move it out to the FETD.
12981 */
12982 static void
12983 ctl_datamove_remote(union ctl_io *io)
12984 {
12985
12986 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12987
12988 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12989 ctl_failover_io(io, /*have_lock*/ 0);
12990 return;
12991 }
12992
12993 /*
12994 * Note that we look for an aborted I/O here, but don't do some of
12995 * the other checks that ctl_datamove() normally does.
12996 * We don't need to run the datamove delay code, since that should
12997 * have been done if need be on the other controller.
12998 */
12999 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13000 printf("%s: tag 0x%jx on (%u:%u:%u) aborted\n", __func__,
13001 io->scsiio.tag_num, io->io_hdr.nexus.initid,
13002 io->io_hdr.nexus.targ_port,
13003 io->io_hdr.nexus.targ_lun);
13004 io->io_hdr.port_status = 31338;
13005 ctl_send_datamove_done(io, /*have_lock*/ 0);
13006 return;
13007 }
13008
13009 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
13010 ctl_datamove_remote_write(io);
13011 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
13012 ctl_datamove_remote_read(io);
13013 else {
13014 io->io_hdr.port_status = 31339;
13015 ctl_send_datamove_done(io, /*have_lock*/ 0);
13016 }
13017 }
13018
13019 static void
13020 ctl_process_done(union ctl_io *io)
13021 {
13022 struct ctl_softc *softc = CTL_SOFTC(io);
13023 struct ctl_port *port = CTL_PORT(io);
13024 struct ctl_lun *lun = CTL_LUN(io);
13025 void (*fe_done)(union ctl_io *io);
13026 union ctl_ha_msg msg;
13027
13028 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13029 fe_done = port->fe_done;
13030
13031 #ifdef CTL_TIME_IO
13032 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13033 char str[256];
13034 char path_str[64];
13035 struct sbuf sb;
13036
13037 ctl_scsi_path_string(io, path_str, sizeof(path_str));
13038 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13039
13040 ctl_io_sbuf(io, &sb);
13041 sbuf_cat(&sb, path_str);
13042 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13043 (intmax_t)time_uptime - io->io_hdr.start_time);
13044 sbuf_finish(&sb);
13045 printf("%s", sbuf_data(&sb));
13046 }
13047 #endif /* CTL_TIME_IO */
13048
13049 switch (io->io_hdr.io_type) {
13050 case CTL_IO_SCSI:
13051 break;
13052 case CTL_IO_TASK:
13053 if (ctl_debug & CTL_DEBUG_INFO)
13054 ctl_io_error_print(io, NULL);
13055 fe_done(io);
13056 return;
13057 default:
13058 panic("%s: Invalid CTL I/O type %d\n",
13059 __func__, io->io_hdr.io_type);
13060 }
13061
13062 if (lun == NULL) {
13063 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13064 io->io_hdr.nexus.targ_mapped_lun));
13065 goto bailout;
13066 }
13067
13068 mtx_lock(&lun->lun_lock);
13069
13070 /*
13071 * Check to see if we have any informational exception and status
13072 * of this command can be modified to report it in form of either
13073 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field.
13074 */
13075 if (lun->ie_reported == 0 && lun->ie_asc != 0 &&
13076 io->io_hdr.status == CTL_SUCCESS &&
13077 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) {
13078 uint8_t mrie = lun->MODE_IE.mrie;
13079 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) ||
13080 (lun->MODE_VER.byte3 & SMS_VER_PER));
13081 if (((mrie == SIEP_MRIE_REC_COND && per) ||
13082 mrie == SIEP_MRIE_REC_UNCOND ||
13083 mrie == SIEP_MRIE_NO_SENSE) &&
13084 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags &
13085 CTL_CMD_FLAG_NO_SENSE) == 0) {
13086 ctl_set_sense(&io->scsiio,
13087 /*current_error*/ 1,
13088 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ?
13089 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR,
13090 /*asc*/ lun->ie_asc,
13091 /*ascq*/ lun->ie_ascq,
13092 SSD_ELEM_NONE);
13093 lun->ie_reported = 1;
13094 }
13095 } else if (lun->ie_reported < 0)
13096 lun->ie_reported = 0;
13097
13098 /*
13099 * Check to see if we have any errors to inject here. We only
13100 * inject errors for commands that don't already have errors set.
13101 */
13102 if (!STAILQ_EMPTY(&lun->error_list) &&
13103 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
13104 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
13105 ctl_inject_error(lun, io);
13106
13107 /*
13108 * XXX KDM how do we treat commands that aren't completed
13109 * successfully?
13110 *
13111 * XXX KDM should we also track I/O latency?
13112 */
13113 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13114 io->io_hdr.io_type == CTL_IO_SCSI) {
13115 int type;
13116 #ifdef CTL_TIME_IO
13117 struct bintime bt;
13118
13119 getbinuptime(&bt);
13120 bintime_sub(&bt, &io->io_hdr.start_bt);
13121 #endif
13122 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13123 CTL_FLAG_DATA_IN)
13124 type = CTL_STATS_READ;
13125 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13126 CTL_FLAG_DATA_OUT)
13127 type = CTL_STATS_WRITE;
13128 else
13129 type = CTL_STATS_NO_IO;
13130
13131 lun->stats.bytes[type] += io->scsiio.kern_total_len;
13132 lun->stats.operations[type] ++;
13133 lun->stats.dmas[type] += io->io_hdr.num_dmas;
13134 #ifdef CTL_TIME_IO
13135 bintime_add(&lun->stats.dma_time[type], &io->io_hdr.dma_bt);
13136 bintime_add(&lun->stats.time[type], &bt);
13137 #endif
13138
13139 mtx_lock(&port->port_lock);
13140 port->stats.bytes[type] += io->scsiio.kern_total_len;
13141 port->stats.operations[type] ++;
13142 port->stats.dmas[type] += io->io_hdr.num_dmas;
13143 #ifdef CTL_TIME_IO
13144 bintime_add(&port->stats.dma_time[type], &io->io_hdr.dma_bt);
13145 bintime_add(&port->stats.time[type], &bt);
13146 #endif
13147 mtx_unlock(&port->port_lock);
13148 }
13149
13150 /*
13151 * Run through the blocked queue of this I/O and see if anything
13152 * can be unblocked, now that this I/O is done and will be removed.
13153 * We need to do it before removal to have OOA position to start.
13154 */
13155 ctl_try_unblock_others(lun, io, TRUE);
13156
13157 /*
13158 * Remove this from the OOA queue.
13159 */
13160 LIST_REMOVE(&io->io_hdr, ooa_links);
13161 #ifdef CTL_TIME_IO
13162 if (LIST_EMPTY(&lun->ooa_queue))
13163 lun->last_busy = getsbinuptime();
13164 #endif
13165
13166 /*
13167 * If the LUN has been invalidated, free it if there is nothing
13168 * left on its OOA queue.
13169 */
13170 if ((lun->flags & CTL_LUN_INVALID)
13171 && LIST_EMPTY(&lun->ooa_queue)) {
13172 mtx_unlock(&lun->lun_lock);
13173 ctl_free_lun(lun);
13174 } else
13175 mtx_unlock(&lun->lun_lock);
13176
13177 bailout:
13178
13179 /*
13180 * If this command has been aborted, make sure we set the status
13181 * properly. The FETD is responsible for freeing the I/O and doing
13182 * whatever it needs to do to clean up its state.
13183 */
13184 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13185 ctl_set_task_aborted(&io->scsiio);
13186
13187 /*
13188 * If enabled, print command error status.
13189 */
13190 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
13191 (ctl_debug & CTL_DEBUG_INFO) != 0)
13192 ctl_io_error_print(io, NULL);
13193
13194 /*
13195 * Tell the FETD or the other shelf controller we're done with this
13196 * command. Note that only SCSI commands get to this point. Task
13197 * management commands are completed above.
13198 */
13199 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
13200 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
13201 memset(&msg, 0, sizeof(msg));
13202 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13203 msg.hdr.serializing_sc = io->io_hdr.remote_io;
13204 msg.hdr.nexus = io->io_hdr.nexus;
13205 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13206 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
13207 M_WAITOK);
13208 }
13209
13210 fe_done(io);
13211 }
13212
13213 /*
13214 * Front end should call this if it doesn't do autosense. When the request
13215 * sense comes back in from the initiator, we'll dequeue this and send it.
13216 */
13217 int
13218 ctl_queue_sense(union ctl_io *io)
13219 {
13220 struct ctl_softc *softc = CTL_SOFTC(io);
13221 struct ctl_port *port = CTL_PORT(io);
13222 struct ctl_lun *lun;
13223 struct scsi_sense_data *ps;
13224 uint32_t initidx, p, targ_lun;
13225
13226 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13227
13228 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13229
13230 /*
13231 * LUN lookup will likely move to the ctl_work_thread() once we
13232 * have our new queueing infrastructure (that doesn't put things on
13233 * a per-LUN queue initially). That is so that we can handle
13234 * things like an INQUIRY to a LUN that we don't have enabled. We
13235 * can't deal with that right now.
13236 * If we don't have a LUN for this, just toss the sense information.
13237 */
13238 mtx_lock(&softc->ctl_lock);
13239 if (targ_lun >= ctl_max_luns ||
13240 (lun = softc->ctl_luns[targ_lun]) == NULL) {
13241 mtx_unlock(&softc->ctl_lock);
13242 goto bailout;
13243 }
13244 mtx_lock(&lun->lun_lock);
13245 mtx_unlock(&softc->ctl_lock);
13246
13247 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13248 p = initidx / CTL_MAX_INIT_PER_PORT;
13249 if (lun->pending_sense[p] == NULL) {
13250 lun->pending_sense[p] = malloc(sizeof(*ps) * CTL_MAX_INIT_PER_PORT,
13251 M_CTL, M_NOWAIT | M_ZERO);
13252 }
13253 if ((ps = lun->pending_sense[p]) != NULL) {
13254 ps += initidx % CTL_MAX_INIT_PER_PORT;
13255 memset(ps, 0, sizeof(*ps));
13256 memcpy(ps, &io->scsiio.sense_data, io->scsiio.sense_len);
13257 }
13258 mtx_unlock(&lun->lun_lock);
13259
13260 bailout:
13261 ctl_free_io(io);
13262 return (CTL_RETVAL_COMPLETE);
13263 }
13264
13265 /*
13266 * Primary command inlet from frontend ports. All SCSI and task I/O
13267 * requests must go through this function.
13268 */
13269 int
13270 ctl_queue(union ctl_io *io)
13271 {
13272 struct ctl_port *port = CTL_PORT(io);
13273
13274 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13275
13276 #ifdef CTL_TIME_IO
13277 io->io_hdr.start_time = time_uptime;
13278 getbinuptime(&io->io_hdr.start_bt);
13279 #endif /* CTL_TIME_IO */
13280
13281 /* Map FE-specific LUN ID into global one. */
13282 io->io_hdr.nexus.targ_mapped_lun =
13283 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13284
13285 switch (io->io_hdr.io_type) {
13286 case CTL_IO_SCSI:
13287 case CTL_IO_TASK:
13288 if (ctl_debug & CTL_DEBUG_CDB)
13289 ctl_io_print(io);
13290 ctl_enqueue_incoming(io);
13291 break;
13292 default:
13293 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13294 return (EINVAL);
13295 }
13296
13297 return (CTL_RETVAL_COMPLETE);
13298 }
13299
13300 int
13301 ctl_run(union ctl_io *io)
13302 {
13303 struct ctl_port *port = CTL_PORT(io);
13304
13305 CTL_DEBUG_PRINT(("ctl_run cdb[0]=%02X\n", io->scsiio.cdb[0]));
13306
13307 #ifdef CTL_TIME_IO
13308 io->io_hdr.start_time = time_uptime;
13309 getbinuptime(&io->io_hdr.start_bt);
13310 #endif /* CTL_TIME_IO */
13311
13312 /* Map FE-specific LUN ID into global one. */
13313 io->io_hdr.nexus.targ_mapped_lun =
13314 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13315
13316 switch (io->io_hdr.io_type) {
13317 case CTL_IO_SCSI:
13318 if (ctl_debug & CTL_DEBUG_CDB)
13319 ctl_io_print(io);
13320 ctl_scsiio_precheck(&io->scsiio);
13321 break;
13322 case CTL_IO_TASK:
13323 if (ctl_debug & CTL_DEBUG_CDB)
13324 ctl_io_print(io);
13325 ctl_run_task(io);
13326 break;
13327 default:
13328 printf("ctl_run: unknown I/O type %d\n", io->io_hdr.io_type);
13329 return (EINVAL);
13330 }
13331
13332 return (CTL_RETVAL_COMPLETE);
13333 }
13334
13335 #ifdef CTL_IO_DELAY
13336 static void
13337 ctl_done_timer_wakeup(void *arg)
13338 {
13339 union ctl_io *io;
13340
13341 io = (union ctl_io *)arg;
13342 ctl_done(io);
13343 }
13344 #endif /* CTL_IO_DELAY */
13345
13346 void
13347 ctl_serseq_done(union ctl_io *io)
13348 {
13349 struct ctl_lun *lun = CTL_LUN(io);
13350
13351 /* This is racy, but should not be a problem. */
13352 if (!TAILQ_EMPTY(&io->io_hdr.blocked_queue)) {
13353 mtx_lock(&lun->lun_lock);
13354 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13355 ctl_try_unblock_others(lun, io, FALSE);
13356 mtx_unlock(&lun->lun_lock);
13357 } else
13358 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13359 }
13360
13361 void
13362 ctl_done(union ctl_io *io)
13363 {
13364
13365 /*
13366 * Enable this to catch duplicate completion issues.
13367 */
13368 #if 0
13369 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13370 printf("%s: type %d msg %d cdb %x iptl: "
13371 "%u:%u:%u tag 0x%04x "
13372 "flag %#x status %x\n",
13373 __func__,
13374 io->io_hdr.io_type,
13375 io->io_hdr.msg_type,
13376 io->scsiio.cdb[0],
13377 io->io_hdr.nexus.initid,
13378 io->io_hdr.nexus.targ_port,
13379 io->io_hdr.nexus.targ_lun,
13380 (io->io_hdr.io_type ==
13381 CTL_IO_TASK) ?
13382 io->taskio.tag_num :
13383 io->scsiio.tag_num,
13384 io->io_hdr.flags,
13385 io->io_hdr.status);
13386 } else
13387 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13388 #endif
13389
13390 /*
13391 * This is an internal copy of an I/O, and should not go through
13392 * the normal done processing logic.
13393 */
13394 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13395 return;
13396
13397 #ifdef CTL_IO_DELAY
13398 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13399 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13400 } else {
13401 struct ctl_lun *lun = CTL_LUN(io);
13402
13403 if ((lun != NULL)
13404 && (lun->delay_info.done_delay > 0)) {
13405 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13406 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13407 callout_reset(&io->io_hdr.delay_callout,
13408 lun->delay_info.done_delay * hz,
13409 ctl_done_timer_wakeup, io);
13410 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13411 lun->delay_info.done_delay = 0;
13412 return;
13413 }
13414 }
13415 #endif /* CTL_IO_DELAY */
13416
13417 ctl_enqueue_done(io);
13418 }
13419
13420 static void
13421 ctl_work_thread(void *arg)
13422 {
13423 struct ctl_thread *thr = (struct ctl_thread *)arg;
13424 struct ctl_softc *softc = thr->ctl_softc;
13425 union ctl_io *io;
13426 int retval;
13427
13428 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13429 thread_lock(curthread);
13430 sched_prio(curthread, PUSER - 1);
13431 thread_unlock(curthread);
13432
13433 while (!softc->shutdown) {
13434 /*
13435 * We handle the queues in this order:
13436 * - ISC
13437 * - done queue (to free up resources, unblock other commands)
13438 * - incoming queue
13439 * - RtR queue
13440 *
13441 * If those queues are empty, we break out of the loop and
13442 * go to sleep.
13443 */
13444 mtx_lock(&thr->queue_lock);
13445 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13446 if (io != NULL) {
13447 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13448 mtx_unlock(&thr->queue_lock);
13449 ctl_handle_isc(io);
13450 continue;
13451 }
13452 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13453 if (io != NULL) {
13454 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13455 /* clear any blocked commands, call fe_done */
13456 mtx_unlock(&thr->queue_lock);
13457 ctl_process_done(io);
13458 continue;
13459 }
13460 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13461 if (io != NULL) {
13462 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13463 mtx_unlock(&thr->queue_lock);
13464 if (io->io_hdr.io_type == CTL_IO_TASK)
13465 ctl_run_task(io);
13466 else
13467 ctl_scsiio_precheck(&io->scsiio);
13468 continue;
13469 }
13470 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13471 if (io != NULL) {
13472 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13473 mtx_unlock(&thr->queue_lock);
13474 retval = ctl_scsiio(&io->scsiio);
13475 if (retval != CTL_RETVAL_COMPLETE)
13476 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13477 continue;
13478 }
13479
13480 /* Sleep until we have something to do. */
13481 mtx_sleep(thr, &thr->queue_lock, PDROP, "-", 0);
13482 }
13483 thr->thread = NULL;
13484 kthread_exit();
13485 }
13486
13487 static void
13488 ctl_thresh_thread(void *arg)
13489 {
13490 struct ctl_softc *softc = (struct ctl_softc *)arg;
13491 struct ctl_lun *lun;
13492 struct ctl_logical_block_provisioning_page *page;
13493 const char *attr;
13494 union ctl_ha_msg msg;
13495 uint64_t thres, val;
13496 int i, e, set;
13497
13498 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13499 thread_lock(curthread);
13500 sched_prio(curthread, PUSER - 1);
13501 thread_unlock(curthread);
13502
13503 while (!softc->shutdown) {
13504 mtx_lock(&softc->ctl_lock);
13505 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13506 if ((lun->flags & CTL_LUN_DISABLED) ||
13507 (lun->flags & CTL_LUN_NO_MEDIA) ||
13508 lun->backend->lun_attr == NULL)
13509 continue;
13510 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13511 softc->ha_mode == CTL_HA_MODE_XFER)
13512 continue;
13513 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0)
13514 continue;
13515 e = 0;
13516 page = &lun->MODE_LBP;
13517 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13518 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13519 continue;
13520 thres = scsi_4btoul(page->descr[i].count);
13521 thres <<= CTL_LBP_EXPONENT;
13522 switch (page->descr[i].resource) {
13523 case 0x01:
13524 attr = "blocksavail";
13525 break;
13526 case 0x02:
13527 attr = "blocksused";
13528 break;
13529 case 0xf1:
13530 attr = "poolblocksavail";
13531 break;
13532 case 0xf2:
13533 attr = "poolblocksused";
13534 break;
13535 default:
13536 continue;
13537 }
13538 mtx_unlock(&softc->ctl_lock); // XXX
13539 val = lun->backend->lun_attr(lun->be_lun, attr);
13540 mtx_lock(&softc->ctl_lock);
13541 if (val == UINT64_MAX)
13542 continue;
13543 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13544 == SLBPPD_ARMING_INC)
13545 e = (val >= thres);
13546 else
13547 e = (val <= thres);
13548 if (e)
13549 break;
13550 }
13551 mtx_lock(&lun->lun_lock);
13552 if (e) {
13553 scsi_u64to8b((uint8_t *)&page->descr[i] -
13554 (uint8_t *)page, lun->ua_tpt_info);
13555 if (lun->lasttpt == 0 ||
13556 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13557 lun->lasttpt = time_uptime;
13558 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13559 set = 1;
13560 } else
13561 set = 0;
13562 } else {
13563 lun->lasttpt = 0;
13564 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13565 set = -1;
13566 }
13567 mtx_unlock(&lun->lun_lock);
13568 if (set != 0 &&
13569 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13570 /* Send msg to other side. */
13571 bzero(&msg.ua, sizeof(msg.ua));
13572 msg.hdr.msg_type = CTL_MSG_UA;
13573 msg.hdr.nexus.initid = -1;
13574 msg.hdr.nexus.targ_port = -1;
13575 msg.hdr.nexus.targ_lun = lun->lun;
13576 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13577 msg.ua.ua_all = 1;
13578 msg.ua.ua_set = (set > 0);
13579 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13580 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
13581 mtx_unlock(&softc->ctl_lock); // XXX
13582 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13583 sizeof(msg.ua), M_WAITOK);
13584 mtx_lock(&softc->ctl_lock);
13585 }
13586 }
13587 mtx_sleep(&softc->thresh_thread, &softc->ctl_lock,
13588 PDROP, "-", CTL_LBP_PERIOD * hz);
13589 }
13590 softc->thresh_thread = NULL;
13591 kthread_exit();
13592 }
13593
13594 static void
13595 ctl_enqueue_incoming(union ctl_io *io)
13596 {
13597 struct ctl_softc *softc = CTL_SOFTC(io);
13598 struct ctl_thread *thr;
13599 u_int idx;
13600
13601 idx = (io->io_hdr.nexus.targ_port * 127 +
13602 io->io_hdr.nexus.initid) % worker_threads;
13603 thr = &softc->threads[idx];
13604 mtx_lock(&thr->queue_lock);
13605 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13606 mtx_unlock(&thr->queue_lock);
13607 wakeup(thr);
13608 }
13609
13610 static void
13611 ctl_enqueue_rtr(union ctl_io *io)
13612 {
13613 struct ctl_softc *softc = CTL_SOFTC(io);
13614 struct ctl_thread *thr;
13615
13616 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13617 mtx_lock(&thr->queue_lock);
13618 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13619 mtx_unlock(&thr->queue_lock);
13620 wakeup(thr);
13621 }
13622
13623 static void
13624 ctl_enqueue_done(union ctl_io *io)
13625 {
13626 struct ctl_softc *softc = CTL_SOFTC(io);
13627 struct ctl_thread *thr;
13628
13629 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13630 mtx_lock(&thr->queue_lock);
13631 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13632 mtx_unlock(&thr->queue_lock);
13633 wakeup(thr);
13634 }
13635
13636 static void
13637 ctl_enqueue_isc(union ctl_io *io)
13638 {
13639 struct ctl_softc *softc = CTL_SOFTC(io);
13640 struct ctl_thread *thr;
13641
13642 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13643 mtx_lock(&thr->queue_lock);
13644 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13645 mtx_unlock(&thr->queue_lock);
13646 wakeup(thr);
13647 }
13648
13649 /*
13650 * vim: ts=8
13651 */
13652