1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
5 * Copyright (c) 2012 The FreeBSD Foundation
6 * Copyright (c) 2014-2017 Alexander Motin <mav@FreeBSD.org>
7 * Copyright (c) 2017 Jakub Wojciech Klama <jceel@FreeBSD.org>
8 * Copyright (c) 2018 Marcelo Araujo <araujo@FreeBSD.org>
9 * All rights reserved.
10 *
11 * Portions of this software were developed by Edward Tomasz Napierala
12 * under sponsorship from the FreeBSD Foundation.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions, and the following disclaimer,
19 * without modification.
20 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
21 * substantially similar to the "NO WARRANTY" disclaimer below
22 * ("Disclaimer") and any redistribution must be conditioned upon
23 * including a substantially similar Disclaimer requirement for further
24 * binary redistribution.
25 *
26 * NO WARRANTY
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
35 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
36 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGES.
38 *
39 * $Id$
40 */
41 /*
42 * CAM Target Layer, a SCSI device emulation subsystem.
43 *
44 * Author: Ken Merry <ken@FreeBSD.org>
45 */
46
47 #include <sys/cdefs.h>
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/ctype.h>
51 #include <sys/kernel.h>
52 #include <sys/types.h>
53 #include <sys/kthread.h>
54 #include <sys/bio.h>
55 #include <sys/fcntl.h>
56 #include <sys/lock.h>
57 #include <sys/module.h>
58 #include <sys/mutex.h>
59 #include <sys/condvar.h>
60 #include <sys/malloc.h>
61 #include <sys/conf.h>
62 #include <sys/ioccom.h>
63 #include <sys/queue.h>
64 #include <sys/sbuf.h>
65 #include <sys/smp.h>
66 #include <sys/endian.h>
67 #include <sys/proc.h>
68 #include <sys/sched.h>
69 #include <sys/sysctl.h>
70 #include <sys/nv.h>
71 #include <sys/dnv.h>
72 #include <vm/uma.h>
73
74 #include <cam/cam.h>
75 #include <cam/scsi/scsi_all.h>
76 #include <cam/scsi/scsi_cd.h>
77 #include <cam/scsi/scsi_da.h>
78 #include <cam/ctl/ctl_io.h>
79 #include <cam/ctl/ctl.h>
80 #include <cam/ctl/ctl_frontend.h>
81 #include <cam/ctl/ctl_util.h>
82 #include <cam/ctl/ctl_backend.h>
83 #include <cam/ctl/ctl_ioctl.h>
84 #include <cam/ctl/ctl_ha.h>
85 #include <cam/ctl/ctl_private.h>
86 #include <cam/ctl/ctl_debug.h>
87 #include <cam/ctl/ctl_scsi_all.h>
88 #include <cam/ctl/ctl_error.h>
89
90 struct ctl_softc *control_softc = NULL;
91
92 /*
93 * Template mode pages.
94 */
95
96 /*
97 * Note that these are default values only. The actual values will be
98 * filled in when the user does a mode sense.
99 */
100 const static struct scsi_da_rw_recovery_page rw_er_page_default = {
101 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
102 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
103 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
104 /*read_retry_count*/0,
105 /*correction_span*/0,
106 /*head_offset_count*/0,
107 /*data_strobe_offset_cnt*/0,
108 /*byte8*/SMS_RWER_LBPERE,
109 /*write_retry_count*/0,
110 /*reserved2*/0,
111 /*recovery_time_limit*/{0, 0},
112 };
113
114 const static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
115 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
116 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
117 /*byte3*/SMS_RWER_PER,
118 /*read_retry_count*/0,
119 /*correction_span*/0,
120 /*head_offset_count*/0,
121 /*data_strobe_offset_cnt*/0,
122 /*byte8*/SMS_RWER_LBPERE,
123 /*write_retry_count*/0,
124 /*reserved2*/0,
125 /*recovery_time_limit*/{0, 0},
126 };
127
128 const static struct scsi_format_page format_page_default = {
129 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
130 /*page_length*/sizeof(struct scsi_format_page) - 2,
131 /*tracks_per_zone*/ {0, 0},
132 /*alt_sectors_per_zone*/ {0, 0},
133 /*alt_tracks_per_zone*/ {0, 0},
134 /*alt_tracks_per_lun*/ {0, 0},
135 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
136 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
137 /*bytes_per_sector*/ {0, 0},
138 /*interleave*/ {0, 0},
139 /*track_skew*/ {0, 0},
140 /*cylinder_skew*/ {0, 0},
141 /*flags*/ SFP_HSEC,
142 /*reserved*/ {0, 0, 0}
143 };
144
145 const static struct scsi_format_page format_page_changeable = {
146 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
147 /*page_length*/sizeof(struct scsi_format_page) - 2,
148 /*tracks_per_zone*/ {0, 0},
149 /*alt_sectors_per_zone*/ {0, 0},
150 /*alt_tracks_per_zone*/ {0, 0},
151 /*alt_tracks_per_lun*/ {0, 0},
152 /*sectors_per_track*/ {0, 0},
153 /*bytes_per_sector*/ {0, 0},
154 /*interleave*/ {0, 0},
155 /*track_skew*/ {0, 0},
156 /*cylinder_skew*/ {0, 0},
157 /*flags*/ 0,
158 /*reserved*/ {0, 0, 0}
159 };
160
161 const static struct scsi_rigid_disk_page rigid_disk_page_default = {
162 /*page_code*/SMS_RIGID_DISK_PAGE,
163 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
164 /*cylinders*/ {0, 0, 0},
165 /*heads*/ CTL_DEFAULT_HEADS,
166 /*start_write_precomp*/ {0, 0, 0},
167 /*start_reduced_current*/ {0, 0, 0},
168 /*step_rate*/ {0, 0},
169 /*landing_zone_cylinder*/ {0, 0, 0},
170 /*rpl*/ SRDP_RPL_DISABLED,
171 /*rotational_offset*/ 0,
172 /*reserved1*/ 0,
173 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
174 CTL_DEFAULT_ROTATION_RATE & 0xff},
175 /*reserved2*/ {0, 0}
176 };
177
178 const static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
179 /*page_code*/SMS_RIGID_DISK_PAGE,
180 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
181 /*cylinders*/ {0, 0, 0},
182 /*heads*/ 0,
183 /*start_write_precomp*/ {0, 0, 0},
184 /*start_reduced_current*/ {0, 0, 0},
185 /*step_rate*/ {0, 0},
186 /*landing_zone_cylinder*/ {0, 0, 0},
187 /*rpl*/ 0,
188 /*rotational_offset*/ 0,
189 /*reserved1*/ 0,
190 /*rotation_rate*/ {0, 0},
191 /*reserved2*/ {0, 0}
192 };
193
194 const static struct scsi_da_verify_recovery_page verify_er_page_default = {
195 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
196 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
197 /*byte3*/0,
198 /*read_retry_count*/0,
199 /*reserved*/{ 0, 0, 0, 0, 0, 0 },
200 /*recovery_time_limit*/{0, 0},
201 };
202
203 const static struct scsi_da_verify_recovery_page verify_er_page_changeable = {
204 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
205 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
206 /*byte3*/SMS_VER_PER,
207 /*read_retry_count*/0,
208 /*reserved*/{ 0, 0, 0, 0, 0, 0 },
209 /*recovery_time_limit*/{0, 0},
210 };
211
212 const static struct scsi_caching_page caching_page_default = {
213 /*page_code*/SMS_CACHING_PAGE,
214 /*page_length*/sizeof(struct scsi_caching_page) - 2,
215 /*flags1*/ SCP_DISC | SCP_WCE,
216 /*ret_priority*/ 0,
217 /*disable_pf_transfer_len*/ {0xff, 0xff},
218 /*min_prefetch*/ {0, 0},
219 /*max_prefetch*/ {0xff, 0xff},
220 /*max_pf_ceiling*/ {0xff, 0xff},
221 /*flags2*/ 0,
222 /*cache_segments*/ 0,
223 /*cache_seg_size*/ {0, 0},
224 /*reserved*/ 0,
225 /*non_cache_seg_size*/ {0, 0, 0}
226 };
227
228 const static struct scsi_caching_page caching_page_changeable = {
229 /*page_code*/SMS_CACHING_PAGE,
230 /*page_length*/sizeof(struct scsi_caching_page) - 2,
231 /*flags1*/ SCP_WCE | SCP_RCD,
232 /*ret_priority*/ 0,
233 /*disable_pf_transfer_len*/ {0, 0},
234 /*min_prefetch*/ {0, 0},
235 /*max_prefetch*/ {0, 0},
236 /*max_pf_ceiling*/ {0, 0},
237 /*flags2*/ 0,
238 /*cache_segments*/ 0,
239 /*cache_seg_size*/ {0, 0},
240 /*reserved*/ 0,
241 /*non_cache_seg_size*/ {0, 0, 0}
242 };
243
244 const static struct scsi_control_page control_page_default = {
245 /*page_code*/SMS_CONTROL_MODE_PAGE,
246 /*page_length*/sizeof(struct scsi_control_page) - 2,
247 /*rlec*/0,
248 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
249 /*eca_and_aen*/0,
250 /*flags4*/SCP_TAS,
251 /*aen_holdoff_period*/{0, 0},
252 /*busy_timeout_period*/{0, 0},
253 /*extended_selftest_completion_time*/{0, 0}
254 };
255
256 const static struct scsi_control_page control_page_changeable = {
257 /*page_code*/SMS_CONTROL_MODE_PAGE,
258 /*page_length*/sizeof(struct scsi_control_page) - 2,
259 /*rlec*/SCP_DSENSE,
260 /*queue_flags*/SCP_QUEUE_ALG_MASK | SCP_NUAR,
261 /*eca_and_aen*/SCP_SWP,
262 /*flags4*/0,
263 /*aen_holdoff_period*/{0, 0},
264 /*busy_timeout_period*/{0, 0},
265 /*extended_selftest_completion_time*/{0, 0}
266 };
267
268 #define CTL_CEM_LEN (sizeof(struct scsi_control_ext_page) - 4)
269
270 const static struct scsi_control_ext_page control_ext_page_default = {
271 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
272 /*subpage_code*/0x01,
273 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
274 /*flags*/0,
275 /*prio*/0,
276 /*max_sense*/0
277 };
278
279 const static struct scsi_control_ext_page control_ext_page_changeable = {
280 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
281 /*subpage_code*/0x01,
282 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
283 /*flags*/0,
284 /*prio*/0,
285 /*max_sense*/0xff
286 };
287
288 const static struct scsi_info_exceptions_page ie_page_default = {
289 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
290 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
291 /*info_flags*/SIEP_FLAGS_EWASC,
292 /*mrie*/SIEP_MRIE_NO,
293 /*interval_timer*/{0, 0, 0, 0},
294 /*report_count*/{0, 0, 0, 1}
295 };
296
297 const static struct scsi_info_exceptions_page ie_page_changeable = {
298 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
299 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
300 /*info_flags*/SIEP_FLAGS_EWASC | SIEP_FLAGS_DEXCPT | SIEP_FLAGS_TEST |
301 SIEP_FLAGS_LOGERR,
302 /*mrie*/0x0f,
303 /*interval_timer*/{0xff, 0xff, 0xff, 0xff},
304 /*report_count*/{0xff, 0xff, 0xff, 0xff}
305 };
306
307 #define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4)
308
309 const static struct ctl_logical_block_provisioning_page lbp_page_default = {{
310 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
311 /*subpage_code*/0x02,
312 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
313 /*flags*/0,
314 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
315 /*descr*/{}},
316 {{/*flags*/0,
317 /*resource*/0x01,
318 /*reserved*/{0, 0},
319 /*count*/{0, 0, 0, 0}},
320 {/*flags*/0,
321 /*resource*/0x02,
322 /*reserved*/{0, 0},
323 /*count*/{0, 0, 0, 0}},
324 {/*flags*/0,
325 /*resource*/0xf1,
326 /*reserved*/{0, 0},
327 /*count*/{0, 0, 0, 0}},
328 {/*flags*/0,
329 /*resource*/0xf2,
330 /*reserved*/{0, 0},
331 /*count*/{0, 0, 0, 0}}
332 }
333 };
334
335 const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{
336 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
337 /*subpage_code*/0x02,
338 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
339 /*flags*/SLBPP_SITUA,
340 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
341 /*descr*/{}},
342 {{/*flags*/0,
343 /*resource*/0,
344 /*reserved*/{0, 0},
345 /*count*/{0, 0, 0, 0}},
346 {/*flags*/0,
347 /*resource*/0,
348 /*reserved*/{0, 0},
349 /*count*/{0, 0, 0, 0}},
350 {/*flags*/0,
351 /*resource*/0,
352 /*reserved*/{0, 0},
353 /*count*/{0, 0, 0, 0}},
354 {/*flags*/0,
355 /*resource*/0,
356 /*reserved*/{0, 0},
357 /*count*/{0, 0, 0, 0}}
358 }
359 };
360
361 const static struct scsi_cddvd_capabilities_page cddvd_page_default = {
362 /*page_code*/SMS_CDDVD_CAPS_PAGE,
363 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
364 /*caps1*/0x3f,
365 /*caps2*/0x00,
366 /*caps3*/0xf0,
367 /*caps4*/0x00,
368 /*caps5*/0x29,
369 /*caps6*/0x00,
370 /*obsolete*/{0, 0},
371 /*nvol_levels*/{0, 0},
372 /*buffer_size*/{8, 0},
373 /*obsolete2*/{0, 0},
374 /*reserved*/0,
375 /*digital*/0,
376 /*obsolete3*/0,
377 /*copy_management*/0,
378 /*reserved2*/0,
379 /*rotation_control*/0,
380 /*cur_write_speed*/0,
381 /*num_speed_descr*/0,
382 };
383
384 const static struct scsi_cddvd_capabilities_page cddvd_page_changeable = {
385 /*page_code*/SMS_CDDVD_CAPS_PAGE,
386 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
387 /*caps1*/0,
388 /*caps2*/0,
389 /*caps3*/0,
390 /*caps4*/0,
391 /*caps5*/0,
392 /*caps6*/0,
393 /*obsolete*/{0, 0},
394 /*nvol_levels*/{0, 0},
395 /*buffer_size*/{0, 0},
396 /*obsolete2*/{0, 0},
397 /*reserved*/0,
398 /*digital*/0,
399 /*obsolete3*/0,
400 /*copy_management*/0,
401 /*reserved2*/0,
402 /*rotation_control*/0,
403 /*cur_write_speed*/0,
404 /*num_speed_descr*/0,
405 };
406
407 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
408 "CAM Target Layer");
409 static int worker_threads = -1;
410 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
411 &worker_threads, 1, "Number of worker threads");
412 static int ctl_debug = CTL_DEBUG_NONE;
413 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
414 &ctl_debug, 0, "Enabled debug flags");
415 static int ctl_lun_map_size = 1024;
416 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, lun_map_size, CTLFLAG_RWTUN,
417 &ctl_lun_map_size, 0, "Size of per-port LUN map (max LUN + 1)");
418 #ifdef CTL_TIME_IO
419 static int ctl_time_io_secs = CTL_TIME_IO_DEFAULT_SECS;
420 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, time_io_secs, CTLFLAG_RWTUN,
421 &ctl_time_io_secs, 0, "Log requests taking more seconds");
422 #endif
423
424 /*
425 * Maximum number of LUNs we support. MUST be a power of 2.
426 */
427 #define CTL_DEFAULT_MAX_LUNS 1024
428 static int ctl_max_luns = CTL_DEFAULT_MAX_LUNS;
429 TUNABLE_INT("kern.cam.ctl.max_luns", &ctl_max_luns);
430 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, max_luns, CTLFLAG_RDTUN,
431 &ctl_max_luns, CTL_DEFAULT_MAX_LUNS, "Maximum number of LUNs");
432
433 /*
434 * Maximum number of ports registered at one time.
435 */
436 #define CTL_DEFAULT_MAX_PORTS 256
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);
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->lun_num = lun->lun;
2493 #ifdef CTL_TIME_IO
2494 entry->start_bt = io->io_hdr.start_bt;
2495 #endif
2496 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2497 entry->cdb_len = io->scsiio.cdb_len;
2498 if (io->io_hdr.blocker != NULL)
2499 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2500
2501 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2502 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2503
2504 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2505 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2506
2507 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2508 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2509
2510 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2511 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2512
2513 if (io->io_hdr.flags & CTL_FLAG_STATUS_QUEUED)
2514 entry->cmd_flags |= CTL_OOACMD_FLAG_STATUS_QUEUED;
2515
2516 if (io->io_hdr.flags & CTL_FLAG_STATUS_SENT)
2517 entry->cmd_flags |= CTL_OOACMD_FLAG_STATUS_SENT;
2518 (*cur_fill_num)++;
2519 }
2520 mtx_unlock(&lun->lun_lock);
2521 }
2522
2523 /*
2524 * Escape characters that are illegal or not recommended in XML.
2525 */
2526 int
ctl_sbuf_printf_esc(struct sbuf * sb,char * str,int size)2527 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2528 {
2529 char *end = str + size;
2530 int retval;
2531
2532 retval = 0;
2533
2534 for (; *str && str < end; str++) {
2535 switch (*str) {
2536 case '&':
2537 retval = sbuf_printf(sb, "&");
2538 break;
2539 case '>':
2540 retval = sbuf_printf(sb, ">");
2541 break;
2542 case '<':
2543 retval = sbuf_printf(sb, "<");
2544 break;
2545 default:
2546 retval = sbuf_putc(sb, *str);
2547 break;
2548 }
2549
2550 if (retval != 0)
2551 break;
2552 }
2553
2554 return (retval);
2555 }
2556
2557 static void
ctl_id_sbuf(struct ctl_devid * id,struct sbuf * sb)2558 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2559 {
2560 struct scsi_vpd_id_descriptor *desc;
2561 int i;
2562
2563 if (id == NULL || id->len < 4)
2564 return;
2565 desc = (struct scsi_vpd_id_descriptor *)id->data;
2566 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2567 case SVPD_ID_TYPE_T10:
2568 sbuf_printf(sb, "t10.");
2569 break;
2570 case SVPD_ID_TYPE_EUI64:
2571 sbuf_printf(sb, "eui.");
2572 break;
2573 case SVPD_ID_TYPE_NAA:
2574 sbuf_printf(sb, "naa.");
2575 break;
2576 case SVPD_ID_TYPE_SCSI_NAME:
2577 break;
2578 }
2579 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2580 case SVPD_ID_CODESET_BINARY:
2581 for (i = 0; i < desc->length; i++)
2582 sbuf_printf(sb, "%02x", desc->identifier[i]);
2583 break;
2584 case SVPD_ID_CODESET_ASCII:
2585 sbuf_printf(sb, "%.*s", (int)desc->length,
2586 (char *)desc->identifier);
2587 break;
2588 case SVPD_ID_CODESET_UTF8:
2589 sbuf_printf(sb, "%s", (char *)desc->identifier);
2590 break;
2591 }
2592 }
2593
2594 static int
ctl_ioctl(struct cdev * dev,u_long cmd,caddr_t addr,int flag,struct thread * td)2595 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2596 struct thread *td)
2597 {
2598 struct ctl_softc *softc = dev->si_drv1;
2599 struct ctl_port *port;
2600 struct ctl_lun *lun;
2601 int retval;
2602
2603 retval = 0;
2604
2605 switch (cmd) {
2606 case CTL_IO:
2607 retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2608 break;
2609 case CTL_ENABLE_PORT:
2610 case CTL_DISABLE_PORT:
2611 case CTL_SET_PORT_WWNS: {
2612 struct ctl_port *port;
2613 struct ctl_port_entry *entry;
2614
2615 entry = (struct ctl_port_entry *)addr;
2616
2617 mtx_lock(&softc->ctl_lock);
2618 STAILQ_FOREACH(port, &softc->port_list, links) {
2619 int action, done;
2620
2621 if (port->targ_port < softc->port_min ||
2622 port->targ_port >= softc->port_max)
2623 continue;
2624
2625 action = 0;
2626 done = 0;
2627 if ((entry->port_type == CTL_PORT_NONE)
2628 && (entry->targ_port == port->targ_port)) {
2629 /*
2630 * If the user only wants to enable or
2631 * disable or set WWNs on a specific port,
2632 * do the operation and we're done.
2633 */
2634 action = 1;
2635 done = 1;
2636 } else if (entry->port_type & port->port_type) {
2637 /*
2638 * Compare the user's type mask with the
2639 * particular frontend type to see if we
2640 * have a match.
2641 */
2642 action = 1;
2643 done = 0;
2644
2645 /*
2646 * Make sure the user isn't trying to set
2647 * WWNs on multiple ports at the same time.
2648 */
2649 if (cmd == CTL_SET_PORT_WWNS) {
2650 printf("%s: Can't set WWNs on "
2651 "multiple ports\n", __func__);
2652 retval = EINVAL;
2653 break;
2654 }
2655 }
2656 if (action == 0)
2657 continue;
2658
2659 /*
2660 * XXX KDM we have to drop the lock here, because
2661 * the online/offline operations can potentially
2662 * block. We need to reference count the frontends
2663 * so they can't go away,
2664 */
2665 if (cmd == CTL_ENABLE_PORT) {
2666 mtx_unlock(&softc->ctl_lock);
2667 ctl_port_online(port);
2668 mtx_lock(&softc->ctl_lock);
2669 } else if (cmd == CTL_DISABLE_PORT) {
2670 mtx_unlock(&softc->ctl_lock);
2671 ctl_port_offline(port);
2672 mtx_lock(&softc->ctl_lock);
2673 } else if (cmd == CTL_SET_PORT_WWNS) {
2674 ctl_port_set_wwns(port,
2675 (entry->flags & CTL_PORT_WWNN_VALID) ?
2676 1 : 0, entry->wwnn,
2677 (entry->flags & CTL_PORT_WWPN_VALID) ?
2678 1 : 0, entry->wwpn);
2679 }
2680 if (done != 0)
2681 break;
2682 }
2683 mtx_unlock(&softc->ctl_lock);
2684 break;
2685 }
2686 case CTL_GET_OOA: {
2687 struct ctl_ooa *ooa_hdr;
2688 struct ctl_ooa_entry *entries;
2689 uint32_t cur_fill_num;
2690
2691 ooa_hdr = (struct ctl_ooa *)addr;
2692
2693 if ((ooa_hdr->alloc_len == 0)
2694 || (ooa_hdr->alloc_num == 0)) {
2695 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2696 "must be non-zero\n", __func__,
2697 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2698 retval = EINVAL;
2699 break;
2700 }
2701
2702 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2703 sizeof(struct ctl_ooa_entry))) {
2704 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2705 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2706 __func__, ooa_hdr->alloc_len,
2707 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2708 retval = EINVAL;
2709 break;
2710 }
2711
2712 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2713
2714 mtx_lock(&softc->ctl_lock);
2715 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 &&
2716 (ooa_hdr->lun_num >= ctl_max_luns ||
2717 softc->ctl_luns[ooa_hdr->lun_num] == NULL)) {
2718 mtx_unlock(&softc->ctl_lock);
2719 free(entries, M_CTL);
2720 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2721 __func__, (uintmax_t)ooa_hdr->lun_num);
2722 retval = EINVAL;
2723 break;
2724 }
2725
2726 cur_fill_num = 0;
2727
2728 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2729 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2730 ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2731 ooa_hdr, entries);
2732 }
2733 } else {
2734 lun = softc->ctl_luns[ooa_hdr->lun_num];
2735 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr,
2736 entries);
2737 }
2738 mtx_unlock(&softc->ctl_lock);
2739
2740 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2741 ooa_hdr->fill_len = ooa_hdr->fill_num *
2742 sizeof(struct ctl_ooa_entry);
2743 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2744 if (retval != 0) {
2745 printf("%s: error copying out %d bytes for OOA dump\n",
2746 __func__, ooa_hdr->fill_len);
2747 }
2748
2749 getbinuptime(&ooa_hdr->cur_bt);
2750
2751 if (cur_fill_num > ooa_hdr->alloc_num) {
2752 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2753 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2754 } else {
2755 ooa_hdr->dropped_num = 0;
2756 ooa_hdr->status = CTL_OOA_OK;
2757 }
2758
2759 free(entries, M_CTL);
2760 break;
2761 }
2762 case CTL_DELAY_IO: {
2763 struct ctl_io_delay_info *delay_info;
2764
2765 delay_info = (struct ctl_io_delay_info *)addr;
2766
2767 #ifdef CTL_IO_DELAY
2768 mtx_lock(&softc->ctl_lock);
2769 if (delay_info->lun_id >= ctl_max_luns ||
2770 (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) {
2771 mtx_unlock(&softc->ctl_lock);
2772 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2773 break;
2774 }
2775 mtx_lock(&lun->lun_lock);
2776 mtx_unlock(&softc->ctl_lock);
2777 delay_info->status = CTL_DELAY_STATUS_OK;
2778 switch (delay_info->delay_type) {
2779 case CTL_DELAY_TYPE_CONT:
2780 case CTL_DELAY_TYPE_ONESHOT:
2781 break;
2782 default:
2783 delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE;
2784 break;
2785 }
2786 switch (delay_info->delay_loc) {
2787 case CTL_DELAY_LOC_DATAMOVE:
2788 lun->delay_info.datamove_type = delay_info->delay_type;
2789 lun->delay_info.datamove_delay = delay_info->delay_secs;
2790 break;
2791 case CTL_DELAY_LOC_DONE:
2792 lun->delay_info.done_type = delay_info->delay_type;
2793 lun->delay_info.done_delay = delay_info->delay_secs;
2794 break;
2795 default:
2796 delay_info->status = CTL_DELAY_STATUS_INVALID_LOC;
2797 break;
2798 }
2799 mtx_unlock(&lun->lun_lock);
2800 #else
2801 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2802 #endif /* CTL_IO_DELAY */
2803 break;
2804 }
2805 case CTL_ERROR_INJECT: {
2806 struct ctl_error_desc *err_desc, *new_err_desc;
2807
2808 err_desc = (struct ctl_error_desc *)addr;
2809
2810 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2811 M_WAITOK | M_ZERO);
2812 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2813
2814 mtx_lock(&softc->ctl_lock);
2815 if (err_desc->lun_id >= ctl_max_luns ||
2816 (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) {
2817 mtx_unlock(&softc->ctl_lock);
2818 free(new_err_desc, M_CTL);
2819 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2820 __func__, (uintmax_t)err_desc->lun_id);
2821 retval = EINVAL;
2822 break;
2823 }
2824 mtx_lock(&lun->lun_lock);
2825 mtx_unlock(&softc->ctl_lock);
2826
2827 /*
2828 * We could do some checking here to verify the validity
2829 * of the request, but given the complexity of error
2830 * injection requests, the checking logic would be fairly
2831 * complex.
2832 *
2833 * For now, if the request is invalid, it just won't get
2834 * executed and might get deleted.
2835 */
2836 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2837
2838 /*
2839 * XXX KDM check to make sure the serial number is unique,
2840 * in case we somehow manage to wrap. That shouldn't
2841 * happen for a very long time, but it's the right thing to
2842 * do.
2843 */
2844 new_err_desc->serial = lun->error_serial;
2845 err_desc->serial = lun->error_serial;
2846 lun->error_serial++;
2847
2848 mtx_unlock(&lun->lun_lock);
2849 break;
2850 }
2851 case CTL_ERROR_INJECT_DELETE: {
2852 struct ctl_error_desc *delete_desc, *desc, *desc2;
2853 int delete_done;
2854
2855 delete_desc = (struct ctl_error_desc *)addr;
2856 delete_done = 0;
2857
2858 mtx_lock(&softc->ctl_lock);
2859 if (delete_desc->lun_id >= ctl_max_luns ||
2860 (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) {
2861 mtx_unlock(&softc->ctl_lock);
2862 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2863 __func__, (uintmax_t)delete_desc->lun_id);
2864 retval = EINVAL;
2865 break;
2866 }
2867 mtx_lock(&lun->lun_lock);
2868 mtx_unlock(&softc->ctl_lock);
2869 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2870 if (desc->serial != delete_desc->serial)
2871 continue;
2872
2873 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2874 links);
2875 free(desc, M_CTL);
2876 delete_done = 1;
2877 }
2878 mtx_unlock(&lun->lun_lock);
2879 if (delete_done == 0) {
2880 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2881 "error serial %ju on LUN %u\n", __func__,
2882 delete_desc->serial, delete_desc->lun_id);
2883 retval = EINVAL;
2884 break;
2885 }
2886 break;
2887 }
2888 case CTL_DUMP_STRUCTS: {
2889 int j, k;
2890 struct ctl_port *port;
2891 struct ctl_frontend *fe;
2892
2893 mtx_lock(&softc->ctl_lock);
2894 printf("CTL Persistent Reservation information start:\n");
2895 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2896 mtx_lock(&lun->lun_lock);
2897 if ((lun->flags & CTL_LUN_DISABLED) != 0) {
2898 mtx_unlock(&lun->lun_lock);
2899 continue;
2900 }
2901
2902 for (j = 0; j < ctl_max_ports; j++) {
2903 if (lun->pr_keys[j] == NULL)
2904 continue;
2905 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2906 if (lun->pr_keys[j][k] == 0)
2907 continue;
2908 printf(" LUN %ju port %d iid %d key "
2909 "%#jx\n", lun->lun, j, k,
2910 (uintmax_t)lun->pr_keys[j][k]);
2911 }
2912 }
2913 mtx_unlock(&lun->lun_lock);
2914 }
2915 printf("CTL Persistent Reservation information end\n");
2916 printf("CTL Ports:\n");
2917 STAILQ_FOREACH(port, &softc->port_list, links) {
2918 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2919 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
2920 port->frontend->name, port->port_type,
2921 port->physical_port, port->virtual_port,
2922 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2923 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2924 if (port->wwpn_iid[j].in_use == 0 &&
2925 port->wwpn_iid[j].wwpn == 0 &&
2926 port->wwpn_iid[j].name == NULL)
2927 continue;
2928
2929 printf(" iid %u use %d WWPN %#jx '%s'\n",
2930 j, port->wwpn_iid[j].in_use,
2931 (uintmax_t)port->wwpn_iid[j].wwpn,
2932 port->wwpn_iid[j].name);
2933 }
2934 }
2935 printf("CTL Port information end\n");
2936 mtx_unlock(&softc->ctl_lock);
2937 /*
2938 * XXX KDM calling this without a lock. We'd likely want
2939 * to drop the lock before calling the frontend's dump
2940 * routine anyway.
2941 */
2942 printf("CTL Frontends:\n");
2943 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2944 printf(" Frontend '%s'\n", fe->name);
2945 if (fe->fe_dump != NULL)
2946 fe->fe_dump();
2947 }
2948 printf("CTL Frontend information end\n");
2949 break;
2950 }
2951 case CTL_LUN_REQ: {
2952 struct ctl_lun_req *lun_req;
2953 struct ctl_backend_driver *backend;
2954 void *packed;
2955 nvlist_t *tmp_args_nvl;
2956 size_t packed_len;
2957
2958 lun_req = (struct ctl_lun_req *)addr;
2959 tmp_args_nvl = lun_req->args_nvl;
2960
2961 backend = ctl_backend_find(lun_req->backend);
2962 if (backend == NULL) {
2963 lun_req->status = CTL_LUN_ERROR;
2964 snprintf(lun_req->error_str,
2965 sizeof(lun_req->error_str),
2966 "Backend \"%s\" not found.",
2967 lun_req->backend);
2968 break;
2969 }
2970
2971 if (lun_req->args != NULL) {
2972 if (lun_req->args_len > CTL_MAX_ARGS_LEN) {
2973 lun_req->status = CTL_LUN_ERROR;
2974 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
2975 "Too big args.");
2976 break;
2977 }
2978 packed = malloc(lun_req->args_len, M_CTL, M_WAITOK);
2979 if (copyin(lun_req->args, packed, lun_req->args_len) != 0) {
2980 free(packed, M_CTL);
2981 lun_req->status = CTL_LUN_ERROR;
2982 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
2983 "Cannot copyin args.");
2984 break;
2985 }
2986 lun_req->args_nvl = nvlist_unpack(packed,
2987 lun_req->args_len, 0);
2988 free(packed, M_CTL);
2989
2990 if (lun_req->args_nvl == NULL) {
2991 lun_req->status = CTL_LUN_ERROR;
2992 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
2993 "Cannot unpack args nvlist.");
2994 break;
2995 }
2996 } else
2997 lun_req->args_nvl = nvlist_create(0);
2998
2999 lun_req->result_nvl = NULL;
3000 retval = backend->ioctl(dev, cmd, addr, flag, td);
3001 nvlist_destroy(lun_req->args_nvl);
3002 lun_req->args_nvl = tmp_args_nvl;
3003
3004 if (lun_req->result_nvl != NULL) {
3005 if (lun_req->result != NULL) {
3006 packed = nvlist_pack(lun_req->result_nvl,
3007 &packed_len);
3008 if (packed == NULL) {
3009 lun_req->status = CTL_LUN_ERROR;
3010 snprintf(lun_req->error_str,
3011 sizeof(lun_req->error_str),
3012 "Cannot pack result nvlist.");
3013 break;
3014 }
3015
3016 if (packed_len > lun_req->result_len) {
3017 lun_req->status = CTL_LUN_ERROR;
3018 snprintf(lun_req->error_str,
3019 sizeof(lun_req->error_str),
3020 "Result nvlist too large.");
3021 free(packed, M_NVLIST);
3022 break;
3023 }
3024
3025 if (copyout(packed, lun_req->result, packed_len)) {
3026 lun_req->status = CTL_LUN_ERROR;
3027 snprintf(lun_req->error_str,
3028 sizeof(lun_req->error_str),
3029 "Cannot copyout() the result.");
3030 free(packed, M_NVLIST);
3031 break;
3032 }
3033
3034 lun_req->result_len = packed_len;
3035 free(packed, M_NVLIST);
3036 }
3037
3038 nvlist_destroy(lun_req->result_nvl);
3039 }
3040 break;
3041 }
3042 case CTL_LUN_LIST: {
3043 struct sbuf *sb;
3044 struct ctl_lun_list *list;
3045 const char *name, *value;
3046 void *cookie;
3047 int type;
3048
3049 list = (struct ctl_lun_list *)addr;
3050
3051 /*
3052 * Allocate a fixed length sbuf here, based on the length
3053 * of the user's buffer. We could allocate an auto-extending
3054 * buffer, and then tell the user how much larger our
3055 * amount of data is than his buffer, but that presents
3056 * some problems:
3057 *
3058 * 1. The sbuf(9) routines use a blocking malloc, and so
3059 * we can't hold a lock while calling them with an
3060 * auto-extending buffer.
3061 *
3062 * 2. There is not currently a LUN reference counting
3063 * mechanism, outside of outstanding transactions on
3064 * the LUN's OOA queue. So a LUN could go away on us
3065 * while we're getting the LUN number, backend-specific
3066 * information, etc. Thus, given the way things
3067 * currently work, we need to hold the CTL lock while
3068 * grabbing LUN information.
3069 *
3070 * So, from the user's standpoint, the best thing to do is
3071 * allocate what he thinks is a reasonable buffer length,
3072 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3073 * double the buffer length and try again. (And repeat
3074 * that until he succeeds.)
3075 */
3076 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3077 if (sb == NULL) {
3078 list->status = CTL_LUN_LIST_ERROR;
3079 snprintf(list->error_str, sizeof(list->error_str),
3080 "Unable to allocate %d bytes for LUN list",
3081 list->alloc_len);
3082 break;
3083 }
3084
3085 sbuf_printf(sb, "<ctllunlist>\n");
3086
3087 mtx_lock(&softc->ctl_lock);
3088 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3089 mtx_lock(&lun->lun_lock);
3090 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3091 (uintmax_t)lun->lun);
3092
3093 /*
3094 * Bail out as soon as we see that we've overfilled
3095 * the buffer.
3096 */
3097 if (retval != 0)
3098 break;
3099
3100 retval = sbuf_printf(sb, "\t<backend_type>%s"
3101 "</backend_type>\n",
3102 (lun->backend == NULL) ? "none" :
3103 lun->backend->name);
3104
3105 if (retval != 0)
3106 break;
3107
3108 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3109 lun->be_lun->lun_type);
3110
3111 if (retval != 0)
3112 break;
3113
3114 if (lun->backend == NULL) {
3115 retval = sbuf_printf(sb, "</lun>\n");
3116 if (retval != 0)
3117 break;
3118 continue;
3119 }
3120
3121 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3122 (lun->be_lun->maxlba > 0) ?
3123 lun->be_lun->maxlba + 1 : 0);
3124
3125 if (retval != 0)
3126 break;
3127
3128 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3129 lun->be_lun->blocksize);
3130
3131 if (retval != 0)
3132 break;
3133
3134 retval = sbuf_printf(sb, "\t<serial_number>");
3135
3136 if (retval != 0)
3137 break;
3138
3139 retval = ctl_sbuf_printf_esc(sb,
3140 lun->be_lun->serial_num,
3141 sizeof(lun->be_lun->serial_num));
3142
3143 if (retval != 0)
3144 break;
3145
3146 retval = sbuf_printf(sb, "</serial_number>\n");
3147
3148 if (retval != 0)
3149 break;
3150
3151 retval = sbuf_printf(sb, "\t<device_id>");
3152
3153 if (retval != 0)
3154 break;
3155
3156 retval = ctl_sbuf_printf_esc(sb,
3157 lun->be_lun->device_id,
3158 sizeof(lun->be_lun->device_id));
3159
3160 if (retval != 0)
3161 break;
3162
3163 retval = sbuf_printf(sb, "</device_id>\n");
3164
3165 if (retval != 0)
3166 break;
3167
3168 if (lun->backend->lun_info != NULL) {
3169 retval = lun->backend->lun_info(lun->be_lun, sb);
3170 if (retval != 0)
3171 break;
3172 }
3173
3174 cookie = NULL;
3175 while ((name = nvlist_next(lun->be_lun->options, &type,
3176 &cookie)) != NULL) {
3177 sbuf_printf(sb, "\t<%s>", name);
3178
3179 if (type == NV_TYPE_STRING) {
3180 value = dnvlist_get_string(
3181 lun->be_lun->options, name, NULL);
3182 if (value != NULL)
3183 sbuf_printf(sb, "%s", value);
3184 }
3185
3186 sbuf_printf(sb, "</%s>\n", name);
3187 }
3188
3189 retval = sbuf_printf(sb, "</lun>\n");
3190
3191 if (retval != 0)
3192 break;
3193 mtx_unlock(&lun->lun_lock);
3194 }
3195 if (lun != NULL)
3196 mtx_unlock(&lun->lun_lock);
3197 mtx_unlock(&softc->ctl_lock);
3198
3199 if ((retval != 0)
3200 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3201 retval = 0;
3202 sbuf_delete(sb);
3203 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3204 snprintf(list->error_str, sizeof(list->error_str),
3205 "Out of space, %d bytes is too small",
3206 list->alloc_len);
3207 break;
3208 }
3209
3210 sbuf_finish(sb);
3211
3212 retval = copyout(sbuf_data(sb), list->lun_xml,
3213 sbuf_len(sb) + 1);
3214
3215 list->fill_len = sbuf_len(sb) + 1;
3216 list->status = CTL_LUN_LIST_OK;
3217 sbuf_delete(sb);
3218 break;
3219 }
3220 case CTL_ISCSI: {
3221 struct ctl_iscsi *ci;
3222 struct ctl_frontend *fe;
3223
3224 ci = (struct ctl_iscsi *)addr;
3225
3226 fe = ctl_frontend_find("iscsi");
3227 if (fe == NULL) {
3228 ci->status = CTL_ISCSI_ERROR;
3229 snprintf(ci->error_str, sizeof(ci->error_str),
3230 "Frontend \"iscsi\" not found.");
3231 break;
3232 }
3233
3234 retval = fe->ioctl(dev, cmd, addr, flag, td);
3235 break;
3236 }
3237 case CTL_PORT_REQ: {
3238 struct ctl_req *req;
3239 struct ctl_frontend *fe;
3240 void *packed;
3241 nvlist_t *tmp_args_nvl;
3242 size_t packed_len;
3243
3244 req = (struct ctl_req *)addr;
3245 tmp_args_nvl = req->args_nvl;
3246
3247 fe = ctl_frontend_find(req->driver);
3248 if (fe == NULL) {
3249 req->status = CTL_LUN_ERROR;
3250 snprintf(req->error_str, sizeof(req->error_str),
3251 "Frontend \"%s\" not found.", req->driver);
3252 break;
3253 }
3254
3255 if (req->args != NULL) {
3256 if (req->args_len > CTL_MAX_ARGS_LEN) {
3257 req->status = CTL_LUN_ERROR;
3258 snprintf(req->error_str, sizeof(req->error_str),
3259 "Too big args.");
3260 break;
3261 }
3262 packed = malloc(req->args_len, M_CTL, M_WAITOK);
3263 if (copyin(req->args, packed, req->args_len) != 0) {
3264 free(packed, M_CTL);
3265 req->status = CTL_LUN_ERROR;
3266 snprintf(req->error_str, sizeof(req->error_str),
3267 "Cannot copyin args.");
3268 break;
3269 }
3270 req->args_nvl = nvlist_unpack(packed,
3271 req->args_len, 0);
3272 free(packed, M_CTL);
3273
3274 if (req->args_nvl == NULL) {
3275 req->status = CTL_LUN_ERROR;
3276 snprintf(req->error_str, sizeof(req->error_str),
3277 "Cannot unpack args nvlist.");
3278 break;
3279 }
3280 } else
3281 req->args_nvl = nvlist_create(0);
3282
3283 req->result_nvl = NULL;
3284 if (fe->ioctl)
3285 retval = fe->ioctl(dev, cmd, addr, flag, td);
3286 else
3287 retval = ENODEV;
3288
3289 nvlist_destroy(req->args_nvl);
3290 req->args_nvl = tmp_args_nvl;
3291
3292 if (req->result_nvl != NULL) {
3293 if (req->result != NULL) {
3294 packed = nvlist_pack(req->result_nvl,
3295 &packed_len);
3296 if (packed == NULL) {
3297 req->status = CTL_LUN_ERROR;
3298 snprintf(req->error_str,
3299 sizeof(req->error_str),
3300 "Cannot pack result nvlist.");
3301 break;
3302 }
3303
3304 if (packed_len > req->result_len) {
3305 req->status = CTL_LUN_ERROR;
3306 snprintf(req->error_str,
3307 sizeof(req->error_str),
3308 "Result nvlist too large.");
3309 free(packed, M_NVLIST);
3310 break;
3311 }
3312
3313 if (copyout(packed, req->result, packed_len)) {
3314 req->status = CTL_LUN_ERROR;
3315 snprintf(req->error_str,
3316 sizeof(req->error_str),
3317 "Cannot copyout() the result.");
3318 free(packed, M_NVLIST);
3319 break;
3320 }
3321
3322 req->result_len = packed_len;
3323 free(packed, M_NVLIST);
3324 }
3325
3326 nvlist_destroy(req->result_nvl);
3327 }
3328 break;
3329 }
3330 case CTL_PORT_LIST: {
3331 struct sbuf *sb;
3332 struct ctl_port *port;
3333 struct ctl_lun_list *list;
3334 const char *name, *value;
3335 void *cookie;
3336 int j, type;
3337 uint32_t plun;
3338
3339 list = (struct ctl_lun_list *)addr;
3340
3341 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3342 if (sb == NULL) {
3343 list->status = CTL_LUN_LIST_ERROR;
3344 snprintf(list->error_str, sizeof(list->error_str),
3345 "Unable to allocate %d bytes for LUN list",
3346 list->alloc_len);
3347 break;
3348 }
3349
3350 sbuf_printf(sb, "<ctlportlist>\n");
3351
3352 mtx_lock(&softc->ctl_lock);
3353 STAILQ_FOREACH(port, &softc->port_list, links) {
3354 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3355 (uintmax_t)port->targ_port);
3356
3357 /*
3358 * Bail out as soon as we see that we've overfilled
3359 * the buffer.
3360 */
3361 if (retval != 0)
3362 break;
3363
3364 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3365 "</frontend_type>\n", port->frontend->name);
3366 if (retval != 0)
3367 break;
3368
3369 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3370 port->port_type);
3371 if (retval != 0)
3372 break;
3373
3374 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3375 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3376 if (retval != 0)
3377 break;
3378
3379 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3380 port->port_name);
3381 if (retval != 0)
3382 break;
3383
3384 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3385 port->physical_port);
3386 if (retval != 0)
3387 break;
3388
3389 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3390 port->virtual_port);
3391 if (retval != 0)
3392 break;
3393
3394 if (port->target_devid != NULL) {
3395 sbuf_printf(sb, "\t<target>");
3396 ctl_id_sbuf(port->target_devid, sb);
3397 sbuf_printf(sb, "</target>\n");
3398 }
3399
3400 if (port->port_devid != NULL) {
3401 sbuf_printf(sb, "\t<port>");
3402 ctl_id_sbuf(port->port_devid, sb);
3403 sbuf_printf(sb, "</port>\n");
3404 }
3405
3406 if (port->port_info != NULL) {
3407 retval = port->port_info(port->onoff_arg, sb);
3408 if (retval != 0)
3409 break;
3410 }
3411
3412 cookie = NULL;
3413 while ((name = nvlist_next(port->options, &type,
3414 &cookie)) != NULL) {
3415 sbuf_printf(sb, "\t<%s>", name);
3416
3417 if (type == NV_TYPE_STRING) {
3418 value = dnvlist_get_string(port->options,
3419 name, NULL);
3420 if (value != NULL)
3421 sbuf_printf(sb, "%s", value);
3422 }
3423
3424 sbuf_printf(sb, "</%s>\n", name);
3425 }
3426
3427 if (port->lun_map != NULL) {
3428 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3429 for (j = 0; j < port->lun_map_size; j++) {
3430 plun = ctl_lun_map_from_port(port, j);
3431 if (plun == UINT32_MAX)
3432 continue;
3433 sbuf_printf(sb,
3434 "\t<lun id=\"%u\">%u</lun>\n",
3435 j, plun);
3436 }
3437 }
3438
3439 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3440 if (port->wwpn_iid[j].in_use == 0 ||
3441 (port->wwpn_iid[j].wwpn == 0 &&
3442 port->wwpn_iid[j].name == NULL))
3443 continue;
3444
3445 if (port->wwpn_iid[j].name != NULL)
3446 retval = sbuf_printf(sb,
3447 "\t<initiator id=\"%u\">%s</initiator>\n",
3448 j, port->wwpn_iid[j].name);
3449 else
3450 retval = sbuf_printf(sb,
3451 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3452 j, port->wwpn_iid[j].wwpn);
3453 if (retval != 0)
3454 break;
3455 }
3456 if (retval != 0)
3457 break;
3458
3459 retval = sbuf_printf(sb, "</targ_port>\n");
3460 if (retval != 0)
3461 break;
3462 }
3463 mtx_unlock(&softc->ctl_lock);
3464
3465 if ((retval != 0)
3466 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3467 retval = 0;
3468 sbuf_delete(sb);
3469 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3470 snprintf(list->error_str, sizeof(list->error_str),
3471 "Out of space, %d bytes is too small",
3472 list->alloc_len);
3473 break;
3474 }
3475
3476 sbuf_finish(sb);
3477
3478 retval = copyout(sbuf_data(sb), list->lun_xml,
3479 sbuf_len(sb) + 1);
3480
3481 list->fill_len = sbuf_len(sb) + 1;
3482 list->status = CTL_LUN_LIST_OK;
3483 sbuf_delete(sb);
3484 break;
3485 }
3486 case CTL_LUN_MAP: {
3487 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3488 struct ctl_port *port;
3489
3490 mtx_lock(&softc->ctl_lock);
3491 if (lm->port < softc->port_min ||
3492 lm->port >= softc->port_max ||
3493 (port = softc->ctl_ports[lm->port]) == NULL) {
3494 mtx_unlock(&softc->ctl_lock);
3495 return (ENXIO);
3496 }
3497 if (port->status & CTL_PORT_STATUS_ONLINE) {
3498 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3499 if (ctl_lun_map_to_port(port, lun->lun) ==
3500 UINT32_MAX)
3501 continue;
3502 mtx_lock(&lun->lun_lock);
3503 ctl_est_ua_port(lun, lm->port, -1,
3504 CTL_UA_LUN_CHANGE);
3505 mtx_unlock(&lun->lun_lock);
3506 }
3507 }
3508 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3509 if (lm->plun != UINT32_MAX) {
3510 if (lm->lun == UINT32_MAX)
3511 retval = ctl_lun_map_unset(port, lm->plun);
3512 else if (lm->lun < ctl_max_luns &&
3513 softc->ctl_luns[lm->lun] != NULL)
3514 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3515 else
3516 return (ENXIO);
3517 } else {
3518 if (lm->lun == UINT32_MAX)
3519 retval = ctl_lun_map_deinit(port);
3520 else
3521 retval = ctl_lun_map_init(port);
3522 }
3523 if (port->status & CTL_PORT_STATUS_ONLINE)
3524 ctl_isc_announce_port(port);
3525 break;
3526 }
3527 case CTL_GET_LUN_STATS: {
3528 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3529 int i;
3530
3531 /*
3532 * XXX KDM no locking here. If the LUN list changes,
3533 * things can blow up.
3534 */
3535 i = 0;
3536 stats->status = CTL_SS_OK;
3537 stats->fill_len = 0;
3538 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3539 if (lun->lun < stats->first_item)
3540 continue;
3541 if (stats->fill_len + sizeof(lun->stats) >
3542 stats->alloc_len) {
3543 stats->status = CTL_SS_NEED_MORE_SPACE;
3544 break;
3545 }
3546 retval = copyout(&lun->stats, &stats->stats[i++],
3547 sizeof(lun->stats));
3548 if (retval != 0)
3549 break;
3550 stats->fill_len += sizeof(lun->stats);
3551 }
3552 stats->num_items = softc->num_luns;
3553 stats->flags = CTL_STATS_FLAG_NONE;
3554 #ifdef CTL_TIME_IO
3555 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3556 #endif
3557 getnanouptime(&stats->timestamp);
3558 break;
3559 }
3560 case CTL_GET_PORT_STATS: {
3561 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3562 int i;
3563
3564 /*
3565 * XXX KDM no locking here. If the LUN list changes,
3566 * things can blow up.
3567 */
3568 i = 0;
3569 stats->status = CTL_SS_OK;
3570 stats->fill_len = 0;
3571 STAILQ_FOREACH(port, &softc->port_list, links) {
3572 if (port->targ_port < stats->first_item)
3573 continue;
3574 if (stats->fill_len + sizeof(port->stats) >
3575 stats->alloc_len) {
3576 stats->status = CTL_SS_NEED_MORE_SPACE;
3577 break;
3578 }
3579 retval = copyout(&port->stats, &stats->stats[i++],
3580 sizeof(port->stats));
3581 if (retval != 0)
3582 break;
3583 stats->fill_len += sizeof(port->stats);
3584 }
3585 stats->num_items = softc->num_ports;
3586 stats->flags = CTL_STATS_FLAG_NONE;
3587 #ifdef CTL_TIME_IO
3588 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3589 #endif
3590 getnanouptime(&stats->timestamp);
3591 break;
3592 }
3593 default: {
3594 /* XXX KDM should we fix this? */
3595 #if 0
3596 struct ctl_backend_driver *backend;
3597 unsigned int type;
3598 int found;
3599
3600 found = 0;
3601
3602 /*
3603 * We encode the backend type as the ioctl type for backend
3604 * ioctls. So parse it out here, and then search for a
3605 * backend of this type.
3606 */
3607 type = _IOC_TYPE(cmd);
3608
3609 STAILQ_FOREACH(backend, &softc->be_list, links) {
3610 if (backend->type == type) {
3611 found = 1;
3612 break;
3613 }
3614 }
3615 if (found == 0) {
3616 printf("ctl: unknown ioctl command %#lx or backend "
3617 "%d\n", cmd, type);
3618 retval = EINVAL;
3619 break;
3620 }
3621 retval = backend->ioctl(dev, cmd, addr, flag, td);
3622 #endif
3623 retval = ENOTTY;
3624 break;
3625 }
3626 }
3627 return (retval);
3628 }
3629
3630 uint32_t
ctl_get_initindex(struct ctl_nexus * nexus)3631 ctl_get_initindex(struct ctl_nexus *nexus)
3632 {
3633 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3634 }
3635
3636 int
ctl_lun_map_init(struct ctl_port * port)3637 ctl_lun_map_init(struct ctl_port *port)
3638 {
3639 struct ctl_softc *softc = port->ctl_softc;
3640 struct ctl_lun *lun;
3641 int size = ctl_lun_map_size;
3642 uint32_t i;
3643
3644 if (port->lun_map == NULL || port->lun_map_size < size) {
3645 port->lun_map_size = 0;
3646 free(port->lun_map, M_CTL);
3647 port->lun_map = malloc(size * sizeof(uint32_t),
3648 M_CTL, M_NOWAIT);
3649 }
3650 if (port->lun_map == NULL)
3651 return (ENOMEM);
3652 for (i = 0; i < size; i++)
3653 port->lun_map[i] = UINT32_MAX;
3654 port->lun_map_size = size;
3655 if (port->status & CTL_PORT_STATUS_ONLINE) {
3656 if (port->lun_disable != NULL) {
3657 STAILQ_FOREACH(lun, &softc->lun_list, links)
3658 port->lun_disable(port->targ_lun_arg, lun->lun);
3659 }
3660 ctl_isc_announce_port(port);
3661 }
3662 return (0);
3663 }
3664
3665 int
ctl_lun_map_deinit(struct ctl_port * port)3666 ctl_lun_map_deinit(struct ctl_port *port)
3667 {
3668 struct ctl_softc *softc = port->ctl_softc;
3669 struct ctl_lun *lun;
3670
3671 if (port->lun_map == NULL)
3672 return (0);
3673 port->lun_map_size = 0;
3674 free(port->lun_map, M_CTL);
3675 port->lun_map = NULL;
3676 if (port->status & CTL_PORT_STATUS_ONLINE) {
3677 if (port->lun_enable != NULL) {
3678 STAILQ_FOREACH(lun, &softc->lun_list, links)
3679 port->lun_enable(port->targ_lun_arg, lun->lun);
3680 }
3681 ctl_isc_announce_port(port);
3682 }
3683 return (0);
3684 }
3685
3686 int
ctl_lun_map_set(struct ctl_port * port,uint32_t plun,uint32_t glun)3687 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3688 {
3689 int status;
3690 uint32_t old;
3691
3692 if (port->lun_map == NULL) {
3693 status = ctl_lun_map_init(port);
3694 if (status != 0)
3695 return (status);
3696 }
3697 if (plun >= port->lun_map_size)
3698 return (EINVAL);
3699 old = port->lun_map[plun];
3700 port->lun_map[plun] = glun;
3701 if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) {
3702 if (port->lun_enable != NULL)
3703 port->lun_enable(port->targ_lun_arg, plun);
3704 ctl_isc_announce_port(port);
3705 }
3706 return (0);
3707 }
3708
3709 int
ctl_lun_map_unset(struct ctl_port * port,uint32_t plun)3710 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3711 {
3712 uint32_t old;
3713
3714 if (port->lun_map == NULL || plun >= port->lun_map_size)
3715 return (0);
3716 old = port->lun_map[plun];
3717 port->lun_map[plun] = UINT32_MAX;
3718 if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) {
3719 if (port->lun_disable != NULL)
3720 port->lun_disable(port->targ_lun_arg, plun);
3721 ctl_isc_announce_port(port);
3722 }
3723 return (0);
3724 }
3725
3726 uint32_t
ctl_lun_map_from_port(struct ctl_port * port,uint32_t lun_id)3727 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3728 {
3729
3730 if (port == NULL)
3731 return (UINT32_MAX);
3732 if (port->lun_map == NULL)
3733 return (lun_id);
3734 if (lun_id > port->lun_map_size)
3735 return (UINT32_MAX);
3736 return (port->lun_map[lun_id]);
3737 }
3738
3739 uint32_t
ctl_lun_map_to_port(struct ctl_port * port,uint32_t lun_id)3740 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3741 {
3742 uint32_t i;
3743
3744 if (port == NULL)
3745 return (UINT32_MAX);
3746 if (port->lun_map == NULL)
3747 return (lun_id);
3748 for (i = 0; i < port->lun_map_size; i++) {
3749 if (port->lun_map[i] == lun_id)
3750 return (i);
3751 }
3752 return (UINT32_MAX);
3753 }
3754
3755 uint32_t
ctl_decode_lun(uint64_t encoded)3756 ctl_decode_lun(uint64_t encoded)
3757 {
3758 uint8_t lun[8];
3759 uint32_t result = 0xffffffff;
3760
3761 be64enc(lun, encoded);
3762 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) {
3763 case RPL_LUNDATA_ATYP_PERIPH:
3764 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 &&
3765 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0)
3766 result = lun[1];
3767 break;
3768 case RPL_LUNDATA_ATYP_FLAT:
3769 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 &&
3770 lun[6] == 0 && lun[7] == 0)
3771 result = ((lun[0] & 0x3f) << 8) + lun[1];
3772 break;
3773 case RPL_LUNDATA_ATYP_EXTLUN:
3774 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) {
3775 case 0x02:
3776 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) {
3777 case 0x00:
3778 result = lun[1];
3779 break;
3780 case 0x10:
3781 result = (lun[1] << 16) + (lun[2] << 8) +
3782 lun[3];
3783 break;
3784 case 0x20:
3785 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0)
3786 result = (lun[2] << 24) +
3787 (lun[3] << 16) + (lun[4] << 8) +
3788 lun[5];
3789 break;
3790 }
3791 break;
3792 case RPL_LUNDATA_EXT_EAM_NOT_SPEC:
3793 result = 0xffffffff;
3794 break;
3795 }
3796 break;
3797 }
3798 return (result);
3799 }
3800
3801 uint64_t
ctl_encode_lun(uint32_t decoded)3802 ctl_encode_lun(uint32_t decoded)
3803 {
3804 uint64_t l = decoded;
3805
3806 if (l <= 0xff)
3807 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48));
3808 if (l <= 0x3fff)
3809 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48));
3810 if (l <= 0xffffff)
3811 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) |
3812 (l << 32));
3813 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16));
3814 }
3815
3816 int
ctl_ffz(uint32_t * mask,uint32_t first,uint32_t last)3817 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3818 {
3819 int i;
3820
3821 for (i = first; i < last; i++) {
3822 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3823 return (i);
3824 }
3825 return (-1);
3826 }
3827
3828 int
ctl_set_mask(uint32_t * mask,uint32_t bit)3829 ctl_set_mask(uint32_t *mask, uint32_t bit)
3830 {
3831 uint32_t chunk, piece;
3832
3833 chunk = bit >> 5;
3834 piece = bit % (sizeof(uint32_t) * 8);
3835
3836 if ((mask[chunk] & (1 << piece)) != 0)
3837 return (-1);
3838 else
3839 mask[chunk] |= (1 << piece);
3840
3841 return (0);
3842 }
3843
3844 int
ctl_clear_mask(uint32_t * mask,uint32_t bit)3845 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3846 {
3847 uint32_t chunk, piece;
3848
3849 chunk = bit >> 5;
3850 piece = bit % (sizeof(uint32_t) * 8);
3851
3852 if ((mask[chunk] & (1 << piece)) == 0)
3853 return (-1);
3854 else
3855 mask[chunk] &= ~(1 << piece);
3856
3857 return (0);
3858 }
3859
3860 int
ctl_is_set(uint32_t * mask,uint32_t bit)3861 ctl_is_set(uint32_t *mask, uint32_t bit)
3862 {
3863 uint32_t chunk, piece;
3864
3865 chunk = bit >> 5;
3866 piece = bit % (sizeof(uint32_t) * 8);
3867
3868 if ((mask[chunk] & (1 << piece)) == 0)
3869 return (0);
3870 else
3871 return (1);
3872 }
3873
3874 static uint64_t
ctl_get_prkey(struct ctl_lun * lun,uint32_t residx)3875 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3876 {
3877 uint64_t *t;
3878
3879 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3880 if (t == NULL)
3881 return (0);
3882 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3883 }
3884
3885 static void
ctl_clr_prkey(struct ctl_lun * lun,uint32_t residx)3886 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3887 {
3888 uint64_t *t;
3889
3890 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3891 if (t == NULL)
3892 return;
3893 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3894 }
3895
3896 static void
ctl_alloc_prkey(struct ctl_lun * lun,uint32_t residx)3897 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3898 {
3899 uint64_t *p;
3900 u_int i;
3901
3902 i = residx/CTL_MAX_INIT_PER_PORT;
3903 if (lun->pr_keys[i] != NULL)
3904 return;
3905 mtx_unlock(&lun->lun_lock);
3906 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3907 M_WAITOK | M_ZERO);
3908 mtx_lock(&lun->lun_lock);
3909 if (lun->pr_keys[i] == NULL)
3910 lun->pr_keys[i] = p;
3911 else
3912 free(p, M_CTL);
3913 }
3914
3915 static void
ctl_set_prkey(struct ctl_lun * lun,uint32_t residx,uint64_t key)3916 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3917 {
3918 uint64_t *t;
3919
3920 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3921 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3922 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3923 }
3924
3925 /*
3926 * ctl_softc, pool_name, total_ctl_io are passed in.
3927 * npool is passed out.
3928 */
3929 int
ctl_pool_create(struct ctl_softc * ctl_softc,const char * pool_name,uint32_t total_ctl_io,void ** npool)3930 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3931 uint32_t total_ctl_io, void **npool)
3932 {
3933 struct ctl_io_pool *pool;
3934
3935 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3936 M_NOWAIT | M_ZERO);
3937 if (pool == NULL)
3938 return (ENOMEM);
3939
3940 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3941 pool->ctl_softc = ctl_softc;
3942 #ifdef IO_POOLS
3943 pool->zone = uma_zsecond_create(pool->name, NULL,
3944 NULL, NULL, NULL, ctl_softc->io_zone);
3945 /* uma_prealloc(pool->zone, total_ctl_io); */
3946 #else
3947 pool->zone = ctl_softc->io_zone;
3948 #endif
3949
3950 *npool = pool;
3951 return (0);
3952 }
3953
3954 void
ctl_pool_free(struct ctl_io_pool * pool)3955 ctl_pool_free(struct ctl_io_pool *pool)
3956 {
3957
3958 if (pool == NULL)
3959 return;
3960
3961 #ifdef IO_POOLS
3962 uma_zdestroy(pool->zone);
3963 #endif
3964 free(pool, M_CTL);
3965 }
3966
3967 union ctl_io *
ctl_alloc_io(void * pool_ref)3968 ctl_alloc_io(void *pool_ref)
3969 {
3970 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3971 union ctl_io *io;
3972
3973 io = uma_zalloc(pool->zone, M_WAITOK);
3974 if (io != NULL) {
3975 io->io_hdr.pool = pool_ref;
3976 CTL_SOFTC(io) = pool->ctl_softc;
3977 TAILQ_INIT(&io->io_hdr.blocked_queue);
3978 }
3979 return (io);
3980 }
3981
3982 union ctl_io *
ctl_alloc_io_nowait(void * pool_ref)3983 ctl_alloc_io_nowait(void *pool_ref)
3984 {
3985 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3986 union ctl_io *io;
3987
3988 io = uma_zalloc(pool->zone, M_NOWAIT);
3989 if (io != NULL) {
3990 io->io_hdr.pool = pool_ref;
3991 CTL_SOFTC(io) = pool->ctl_softc;
3992 TAILQ_INIT(&io->io_hdr.blocked_queue);
3993 }
3994 return (io);
3995 }
3996
3997 void
ctl_free_io(union ctl_io * io)3998 ctl_free_io(union ctl_io *io)
3999 {
4000 struct ctl_io_pool *pool;
4001
4002 if (io == NULL)
4003 return;
4004
4005 pool = (struct ctl_io_pool *)io->io_hdr.pool;
4006 uma_zfree(pool->zone, io);
4007 }
4008
4009 void
ctl_zero_io(union ctl_io * io)4010 ctl_zero_io(union ctl_io *io)
4011 {
4012 struct ctl_io_pool *pool;
4013
4014 if (io == NULL)
4015 return;
4016
4017 /*
4018 * May need to preserve linked list pointers at some point too.
4019 */
4020 pool = io->io_hdr.pool;
4021 memset(io, 0, sizeof(*io));
4022 io->io_hdr.pool = pool;
4023 CTL_SOFTC(io) = pool->ctl_softc;
4024 TAILQ_INIT(&io->io_hdr.blocked_queue);
4025 }
4026
4027 int
ctl_expand_number(const char * buf,uint64_t * num)4028 ctl_expand_number(const char *buf, uint64_t *num)
4029 {
4030 char *endptr;
4031 uint64_t number;
4032 unsigned shift;
4033
4034 number = strtoq(buf, &endptr, 0);
4035
4036 switch (tolower((unsigned char)*endptr)) {
4037 case 'e':
4038 shift = 60;
4039 break;
4040 case 'p':
4041 shift = 50;
4042 break;
4043 case 't':
4044 shift = 40;
4045 break;
4046 case 'g':
4047 shift = 30;
4048 break;
4049 case 'm':
4050 shift = 20;
4051 break;
4052 case 'k':
4053 shift = 10;
4054 break;
4055 case 'b':
4056 case '\0': /* No unit. */
4057 *num = number;
4058 return (0);
4059 default:
4060 /* Unrecognized unit. */
4061 return (-1);
4062 }
4063
4064 if ((number << shift) >> shift != number) {
4065 /* Overflow */
4066 return (-1);
4067 }
4068 *num = number << shift;
4069 return (0);
4070 }
4071
4072 /*
4073 * This routine could be used in the future to load default and/or saved
4074 * mode page parameters for a particuar lun.
4075 */
4076 static int
ctl_init_page_index(struct ctl_lun * lun)4077 ctl_init_page_index(struct ctl_lun *lun)
4078 {
4079 int i, page_code;
4080 struct ctl_page_index *page_index;
4081 const char *value;
4082 uint64_t ival;
4083
4084 memcpy(&lun->mode_pages.index, page_index_template,
4085 sizeof(page_index_template));
4086
4087 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4088 page_index = &lun->mode_pages.index[i];
4089 if (lun->be_lun->lun_type == T_DIRECT &&
4090 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4091 continue;
4092 if (lun->be_lun->lun_type == T_PROCESSOR &&
4093 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4094 continue;
4095 if (lun->be_lun->lun_type == T_CDROM &&
4096 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4097 continue;
4098
4099 page_code = page_index->page_code & SMPH_PC_MASK;
4100 switch (page_code) {
4101 case SMS_RW_ERROR_RECOVERY_PAGE: {
4102 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4103 ("subpage %#x for page %#x is incorrect!",
4104 page_index->subpage, page_code));
4105 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
4106 &rw_er_page_default,
4107 sizeof(rw_er_page_default));
4108 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
4109 &rw_er_page_changeable,
4110 sizeof(rw_er_page_changeable));
4111 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
4112 &rw_er_page_default,
4113 sizeof(rw_er_page_default));
4114 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
4115 &rw_er_page_default,
4116 sizeof(rw_er_page_default));
4117 page_index->page_data =
4118 (uint8_t *)lun->mode_pages.rw_er_page;
4119 break;
4120 }
4121 case SMS_FORMAT_DEVICE_PAGE: {
4122 struct scsi_format_page *format_page;
4123
4124 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4125 ("subpage %#x for page %#x is incorrect!",
4126 page_index->subpage, page_code));
4127
4128 /*
4129 * Sectors per track are set above. Bytes per
4130 * sector need to be set here on a per-LUN basis.
4131 */
4132 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
4133 &format_page_default,
4134 sizeof(format_page_default));
4135 memcpy(&lun->mode_pages.format_page[
4136 CTL_PAGE_CHANGEABLE], &format_page_changeable,
4137 sizeof(format_page_changeable));
4138 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
4139 &format_page_default,
4140 sizeof(format_page_default));
4141 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
4142 &format_page_default,
4143 sizeof(format_page_default));
4144
4145 format_page = &lun->mode_pages.format_page[
4146 CTL_PAGE_CURRENT];
4147 scsi_ulto2b(lun->be_lun->blocksize,
4148 format_page->bytes_per_sector);
4149
4150 format_page = &lun->mode_pages.format_page[
4151 CTL_PAGE_DEFAULT];
4152 scsi_ulto2b(lun->be_lun->blocksize,
4153 format_page->bytes_per_sector);
4154
4155 format_page = &lun->mode_pages.format_page[
4156 CTL_PAGE_SAVED];
4157 scsi_ulto2b(lun->be_lun->blocksize,
4158 format_page->bytes_per_sector);
4159
4160 page_index->page_data =
4161 (uint8_t *)lun->mode_pages.format_page;
4162 break;
4163 }
4164 case SMS_RIGID_DISK_PAGE: {
4165 struct scsi_rigid_disk_page *rigid_disk_page;
4166 uint32_t sectors_per_cylinder;
4167 uint64_t cylinders;
4168 #ifndef __XSCALE__
4169 int shift;
4170 #endif /* !__XSCALE__ */
4171
4172 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4173 ("subpage %#x for page %#x is incorrect!",
4174 page_index->subpage, page_code));
4175
4176 /*
4177 * Rotation rate and sectors per track are set
4178 * above. We calculate the cylinders here based on
4179 * capacity. Due to the number of heads and
4180 * sectors per track we're using, smaller arrays
4181 * may turn out to have 0 cylinders. Linux and
4182 * FreeBSD don't pay attention to these mode pages
4183 * to figure out capacity, but Solaris does. It
4184 * seems to deal with 0 cylinders just fine, and
4185 * works out a fake geometry based on the capacity.
4186 */
4187 memcpy(&lun->mode_pages.rigid_disk_page[
4188 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4189 sizeof(rigid_disk_page_default));
4190 memcpy(&lun->mode_pages.rigid_disk_page[
4191 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4192 sizeof(rigid_disk_page_changeable));
4193
4194 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4195 CTL_DEFAULT_HEADS;
4196
4197 /*
4198 * The divide method here will be more accurate,
4199 * probably, but results in floating point being
4200 * used in the kernel on i386 (__udivdi3()). On the
4201 * XScale, though, __udivdi3() is implemented in
4202 * software.
4203 *
4204 * The shift method for cylinder calculation is
4205 * accurate if sectors_per_cylinder is a power of
4206 * 2. Otherwise it might be slightly off -- you
4207 * might have a bit of a truncation problem.
4208 */
4209 #ifdef __XSCALE__
4210 cylinders = (lun->be_lun->maxlba + 1) /
4211 sectors_per_cylinder;
4212 #else
4213 for (shift = 31; shift > 0; shift--) {
4214 if (sectors_per_cylinder & (1 << shift))
4215 break;
4216 }
4217 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4218 #endif
4219
4220 /*
4221 * We've basically got 3 bytes, or 24 bits for the
4222 * cylinder size in the mode page. If we're over,
4223 * just round down to 2^24.
4224 */
4225 if (cylinders > 0xffffff)
4226 cylinders = 0xffffff;
4227
4228 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4229 CTL_PAGE_DEFAULT];
4230 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4231
4232 if ((value = dnvlist_get_string(lun->be_lun->options,
4233 "rpm", NULL)) != NULL) {
4234 scsi_ulto2b(strtol(value, NULL, 0),
4235 rigid_disk_page->rotation_rate);
4236 }
4237
4238 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4239 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4240 sizeof(rigid_disk_page_default));
4241 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4242 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4243 sizeof(rigid_disk_page_default));
4244
4245 page_index->page_data =
4246 (uint8_t *)lun->mode_pages.rigid_disk_page;
4247 break;
4248 }
4249 case SMS_VERIFY_ERROR_RECOVERY_PAGE: {
4250 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4251 ("subpage %#x for page %#x is incorrect!",
4252 page_index->subpage, page_code));
4253 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT],
4254 &verify_er_page_default,
4255 sizeof(verify_er_page_default));
4256 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE],
4257 &verify_er_page_changeable,
4258 sizeof(verify_er_page_changeable));
4259 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT],
4260 &verify_er_page_default,
4261 sizeof(verify_er_page_default));
4262 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED],
4263 &verify_er_page_default,
4264 sizeof(verify_er_page_default));
4265 page_index->page_data =
4266 (uint8_t *)lun->mode_pages.verify_er_page;
4267 break;
4268 }
4269 case SMS_CACHING_PAGE: {
4270 struct scsi_caching_page *caching_page;
4271
4272 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4273 ("subpage %#x for page %#x is incorrect!",
4274 page_index->subpage, page_code));
4275 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4276 &caching_page_default,
4277 sizeof(caching_page_default));
4278 memcpy(&lun->mode_pages.caching_page[
4279 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4280 sizeof(caching_page_changeable));
4281 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4282 &caching_page_default,
4283 sizeof(caching_page_default));
4284 caching_page = &lun->mode_pages.caching_page[
4285 CTL_PAGE_SAVED];
4286 value = dnvlist_get_string(lun->be_lun->options,
4287 "writecache", NULL);
4288 if (value != NULL && strcmp(value, "off") == 0)
4289 caching_page->flags1 &= ~SCP_WCE;
4290 value = dnvlist_get_string(lun->be_lun->options,
4291 "readcache", NULL);
4292 if (value != NULL && strcmp(value, "off") == 0)
4293 caching_page->flags1 |= SCP_RCD;
4294 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4295 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4296 sizeof(caching_page_default));
4297 page_index->page_data =
4298 (uint8_t *)lun->mode_pages.caching_page;
4299 break;
4300 }
4301 case SMS_CONTROL_MODE_PAGE: {
4302 switch (page_index->subpage) {
4303 case SMS_SUBPAGE_PAGE_0: {
4304 struct scsi_control_page *control_page;
4305
4306 memcpy(&lun->mode_pages.control_page[
4307 CTL_PAGE_DEFAULT],
4308 &control_page_default,
4309 sizeof(control_page_default));
4310 memcpy(&lun->mode_pages.control_page[
4311 CTL_PAGE_CHANGEABLE],
4312 &control_page_changeable,
4313 sizeof(control_page_changeable));
4314 memcpy(&lun->mode_pages.control_page[
4315 CTL_PAGE_SAVED],
4316 &control_page_default,
4317 sizeof(control_page_default));
4318 control_page = &lun->mode_pages.control_page[
4319 CTL_PAGE_SAVED];
4320 value = dnvlist_get_string(lun->be_lun->options,
4321 "reordering", NULL);
4322 if (value != NULL &&
4323 strcmp(value, "unrestricted") == 0) {
4324 control_page->queue_flags &=
4325 ~SCP_QUEUE_ALG_MASK;
4326 control_page->queue_flags |=
4327 SCP_QUEUE_ALG_UNRESTRICTED;
4328 }
4329 memcpy(&lun->mode_pages.control_page[
4330 CTL_PAGE_CURRENT],
4331 &lun->mode_pages.control_page[
4332 CTL_PAGE_SAVED],
4333 sizeof(control_page_default));
4334 page_index->page_data =
4335 (uint8_t *)lun->mode_pages.control_page;
4336 break;
4337 }
4338 case 0x01:
4339 memcpy(&lun->mode_pages.control_ext_page[
4340 CTL_PAGE_DEFAULT],
4341 &control_ext_page_default,
4342 sizeof(control_ext_page_default));
4343 memcpy(&lun->mode_pages.control_ext_page[
4344 CTL_PAGE_CHANGEABLE],
4345 &control_ext_page_changeable,
4346 sizeof(control_ext_page_changeable));
4347 memcpy(&lun->mode_pages.control_ext_page[
4348 CTL_PAGE_SAVED],
4349 &control_ext_page_default,
4350 sizeof(control_ext_page_default));
4351 memcpy(&lun->mode_pages.control_ext_page[
4352 CTL_PAGE_CURRENT],
4353 &lun->mode_pages.control_ext_page[
4354 CTL_PAGE_SAVED],
4355 sizeof(control_ext_page_default));
4356 page_index->page_data =
4357 (uint8_t *)lun->mode_pages.control_ext_page;
4358 break;
4359 default:
4360 panic("subpage %#x for page %#x is incorrect!",
4361 page_index->subpage, page_code);
4362 }
4363 break;
4364 }
4365 case SMS_INFO_EXCEPTIONS_PAGE: {
4366 switch (page_index->subpage) {
4367 case SMS_SUBPAGE_PAGE_0:
4368 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4369 &ie_page_default,
4370 sizeof(ie_page_default));
4371 memcpy(&lun->mode_pages.ie_page[
4372 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4373 sizeof(ie_page_changeable));
4374 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4375 &ie_page_default,
4376 sizeof(ie_page_default));
4377 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4378 &ie_page_default,
4379 sizeof(ie_page_default));
4380 page_index->page_data =
4381 (uint8_t *)lun->mode_pages.ie_page;
4382 break;
4383 case 0x02: {
4384 struct ctl_logical_block_provisioning_page *page;
4385
4386 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4387 &lbp_page_default,
4388 sizeof(lbp_page_default));
4389 memcpy(&lun->mode_pages.lbp_page[
4390 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4391 sizeof(lbp_page_changeable));
4392 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4393 &lbp_page_default,
4394 sizeof(lbp_page_default));
4395 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4396 value = dnvlist_get_string(lun->be_lun->options,
4397 "avail-threshold", NULL);
4398 if (value != NULL &&
4399 ctl_expand_number(value, &ival) == 0) {
4400 page->descr[0].flags |= SLBPPD_ENABLED |
4401 SLBPPD_ARMING_DEC;
4402 if (lun->be_lun->blocksize)
4403 ival /= lun->be_lun->blocksize;
4404 else
4405 ival /= 512;
4406 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4407 page->descr[0].count);
4408 }
4409 value = dnvlist_get_string(lun->be_lun->options,
4410 "used-threshold", NULL);
4411 if (value != NULL &&
4412 ctl_expand_number(value, &ival) == 0) {
4413 page->descr[1].flags |= SLBPPD_ENABLED |
4414 SLBPPD_ARMING_INC;
4415 if (lun->be_lun->blocksize)
4416 ival /= lun->be_lun->blocksize;
4417 else
4418 ival /= 512;
4419 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4420 page->descr[1].count);
4421 }
4422 value = dnvlist_get_string(lun->be_lun->options,
4423 "pool-avail-threshold", NULL);
4424 if (value != NULL &&
4425 ctl_expand_number(value, &ival) == 0) {
4426 page->descr[2].flags |= SLBPPD_ENABLED |
4427 SLBPPD_ARMING_DEC;
4428 if (lun->be_lun->blocksize)
4429 ival /= lun->be_lun->blocksize;
4430 else
4431 ival /= 512;
4432 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4433 page->descr[2].count);
4434 }
4435 value = dnvlist_get_string(lun->be_lun->options,
4436 "pool-used-threshold", NULL);
4437 if (value != NULL &&
4438 ctl_expand_number(value, &ival) == 0) {
4439 page->descr[3].flags |= SLBPPD_ENABLED |
4440 SLBPPD_ARMING_INC;
4441 if (lun->be_lun->blocksize)
4442 ival /= lun->be_lun->blocksize;
4443 else
4444 ival /= 512;
4445 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4446 page->descr[3].count);
4447 }
4448 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4449 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4450 sizeof(lbp_page_default));
4451 page_index->page_data =
4452 (uint8_t *)lun->mode_pages.lbp_page;
4453 break;
4454 }
4455 default:
4456 panic("subpage %#x for page %#x is incorrect!",
4457 page_index->subpage, page_code);
4458 }
4459 break;
4460 }
4461 case SMS_CDDVD_CAPS_PAGE:{
4462 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4463 ("subpage %#x for page %#x is incorrect!",
4464 page_index->subpage, page_code));
4465 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT],
4466 &cddvd_page_default,
4467 sizeof(cddvd_page_default));
4468 memcpy(&lun->mode_pages.cddvd_page[
4469 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable,
4470 sizeof(cddvd_page_changeable));
4471 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4472 &cddvd_page_default,
4473 sizeof(cddvd_page_default));
4474 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT],
4475 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4476 sizeof(cddvd_page_default));
4477 page_index->page_data =
4478 (uint8_t *)lun->mode_pages.cddvd_page;
4479 break;
4480 }
4481 default:
4482 panic("invalid page code value %#x", page_code);
4483 }
4484 }
4485
4486 return (CTL_RETVAL_COMPLETE);
4487 }
4488
4489 static int
ctl_init_log_page_index(struct ctl_lun * lun)4490 ctl_init_log_page_index(struct ctl_lun *lun)
4491 {
4492 struct ctl_page_index *page_index;
4493 int i, j, k, prev;
4494
4495 memcpy(&lun->log_pages.index, log_page_index_template,
4496 sizeof(log_page_index_template));
4497
4498 prev = -1;
4499 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4500 page_index = &lun->log_pages.index[i];
4501 if (lun->be_lun->lun_type == T_DIRECT &&
4502 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4503 continue;
4504 if (lun->be_lun->lun_type == T_PROCESSOR &&
4505 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4506 continue;
4507 if (lun->be_lun->lun_type == T_CDROM &&
4508 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4509 continue;
4510
4511 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4512 lun->backend->lun_attr == NULL)
4513 continue;
4514
4515 if (page_index->page_code != prev) {
4516 lun->log_pages.pages_page[j] = page_index->page_code;
4517 prev = page_index->page_code;
4518 j++;
4519 }
4520 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4521 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4522 k++;
4523 }
4524 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4525 lun->log_pages.index[0].page_len = j;
4526 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4527 lun->log_pages.index[1].page_len = k * 2;
4528 lun->log_pages.index[2].page_data = (uint8_t *)&lun->log_pages.temp_page;
4529 lun->log_pages.index[2].page_len = sizeof(lun->log_pages.temp_page);
4530 lun->log_pages.index[3].page_data = &lun->log_pages.lbp_page[0];
4531 lun->log_pages.index[3].page_len = 12*CTL_NUM_LBP_PARAMS;
4532 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.stat_page;
4533 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.stat_page);
4534 lun->log_pages.index[5].page_data = (uint8_t *)&lun->log_pages.ie_page;
4535 lun->log_pages.index[5].page_len = sizeof(lun->log_pages.ie_page);
4536
4537 return (CTL_RETVAL_COMPLETE);
4538 }
4539
4540 static int
hex2bin(const char * str,uint8_t * buf,int buf_size)4541 hex2bin(const char *str, uint8_t *buf, int buf_size)
4542 {
4543 int i;
4544 u_char c;
4545
4546 memset(buf, 0, buf_size);
4547 while (isspace(str[0]))
4548 str++;
4549 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4550 str += 2;
4551 buf_size *= 2;
4552 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4553 while (str[i] == '-') /* Skip dashes in UUIDs. */
4554 str++;
4555 c = str[i];
4556 if (isdigit(c))
4557 c -= '0';
4558 else if (isalpha(c))
4559 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4560 else
4561 break;
4562 if (c >= 16)
4563 break;
4564 if ((i & 1) == 0)
4565 buf[i / 2] |= (c << 4);
4566 else
4567 buf[i / 2] |= c;
4568 }
4569 return ((i + 1) / 2);
4570 }
4571
4572 /*
4573 * Add LUN.
4574 *
4575 * Returns 0 for success, non-zero (errno) for failure.
4576 */
4577 int
ctl_add_lun(struct ctl_be_lun * be_lun)4578 ctl_add_lun(struct ctl_be_lun *be_lun)
4579 {
4580 struct ctl_softc *ctl_softc = control_softc;
4581 struct ctl_lun *nlun, *lun;
4582 struct scsi_vpd_id_descriptor *desc;
4583 struct scsi_vpd_id_t10 *t10id;
4584 const char *eui, *naa, *scsiname, *uuid, *vendor, *value;
4585 int lun_number;
4586 int devidlen, idlen1, idlen2 = 0, len;
4587
4588 /*
4589 * We support only Direct Access, CD-ROM or Processor LUN types.
4590 */
4591 switch (be_lun->lun_type) {
4592 case T_DIRECT:
4593 case T_PROCESSOR:
4594 case T_CDROM:
4595 break;
4596 case T_SEQUENTIAL:
4597 case T_CHANGER:
4598 default:
4599 return (EINVAL);
4600 }
4601 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK | M_ZERO);
4602
4603 lun->pending_sense = malloc(sizeof(struct scsi_sense_data *) *
4604 ctl_max_ports, M_DEVBUF, M_WAITOK | M_ZERO);
4605 lun->pending_ua = malloc(sizeof(ctl_ua_type *) * ctl_max_ports,
4606 M_DEVBUF, M_WAITOK | M_ZERO);
4607 lun->pr_keys = malloc(sizeof(uint64_t *) * ctl_max_ports,
4608 M_DEVBUF, M_WAITOK | M_ZERO);
4609
4610 /* Generate LUN ID. */
4611 devidlen = max(CTL_DEVID_MIN_LEN,
4612 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4613 idlen1 = sizeof(*t10id) + devidlen;
4614 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4615 scsiname = dnvlist_get_string(be_lun->options, "scsiname", NULL);
4616 if (scsiname != NULL) {
4617 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4618 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4619 }
4620 eui = dnvlist_get_string(be_lun->options, "eui", NULL);
4621 if (eui != NULL) {
4622 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4623 }
4624 naa = dnvlist_get_string(be_lun->options, "naa", NULL);
4625 if (naa != NULL) {
4626 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4627 }
4628 uuid = dnvlist_get_string(be_lun->options, "uuid", NULL);
4629 if (uuid != NULL) {
4630 len += sizeof(struct scsi_vpd_id_descriptor) + 18;
4631 }
4632 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4633 M_CTL, M_WAITOK | M_ZERO);
4634 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4635 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4636 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4637 desc->length = idlen1;
4638 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4639 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4640 if ((vendor = dnvlist_get_string(be_lun->options, "vendor", NULL)) == NULL) {
4641 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4642 } else {
4643 strncpy(t10id->vendor, vendor,
4644 min(sizeof(t10id->vendor), strlen(vendor)));
4645 }
4646 strncpy((char *)t10id->vendor_spec_id,
4647 (char *)be_lun->device_id, devidlen);
4648 if (scsiname != NULL) {
4649 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4650 desc->length);
4651 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4652 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4653 SVPD_ID_TYPE_SCSI_NAME;
4654 desc->length = idlen2;
4655 strlcpy(desc->identifier, scsiname, idlen2);
4656 }
4657 if (eui != NULL) {
4658 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4659 desc->length);
4660 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4661 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4662 SVPD_ID_TYPE_EUI64;
4663 desc->length = hex2bin(eui, desc->identifier, 16);
4664 desc->length = desc->length > 12 ? 16 :
4665 (desc->length > 8 ? 12 : 8);
4666 len -= 16 - desc->length;
4667 }
4668 if (naa != NULL) {
4669 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4670 desc->length);
4671 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4672 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4673 SVPD_ID_TYPE_NAA;
4674 desc->length = hex2bin(naa, desc->identifier, 16);
4675 desc->length = desc->length > 8 ? 16 : 8;
4676 len -= 16 - desc->length;
4677 }
4678 if (uuid != NULL) {
4679 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4680 desc->length);
4681 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4682 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4683 SVPD_ID_TYPE_UUID;
4684 desc->identifier[0] = 0x10;
4685 hex2bin(uuid, &desc->identifier[2], 16);
4686 desc->length = 18;
4687 }
4688 lun->lun_devid->len = len;
4689
4690 mtx_lock(&ctl_softc->ctl_lock);
4691 /*
4692 * See if the caller requested a particular LUN number. If so, see
4693 * if it is available. Otherwise, allocate the first available LUN.
4694 */
4695 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4696 if ((be_lun->req_lun_id > (ctl_max_luns - 1))
4697 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4698 mtx_unlock(&ctl_softc->ctl_lock);
4699 if (be_lun->req_lun_id > (ctl_max_luns - 1)) {
4700 printf("ctl: requested LUN ID %d is higher "
4701 "than ctl_max_luns - 1 (%d)\n",
4702 be_lun->req_lun_id, ctl_max_luns - 1);
4703 } else {
4704 /*
4705 * XXX KDM return an error, or just assign
4706 * another LUN ID in this case??
4707 */
4708 printf("ctl: requested LUN ID %d is already "
4709 "in use\n", be_lun->req_lun_id);
4710 }
4711 fail:
4712 free(lun->lun_devid, M_CTL);
4713 free(lun, M_CTL);
4714 return (ENOSPC);
4715 }
4716 lun_number = be_lun->req_lun_id;
4717 } else {
4718 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, ctl_max_luns);
4719 if (lun_number == -1) {
4720 mtx_unlock(&ctl_softc->ctl_lock);
4721 printf("ctl: can't allocate LUN, out of LUNs\n");
4722 goto fail;
4723 }
4724 }
4725 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4726 mtx_unlock(&ctl_softc->ctl_lock);
4727
4728 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4729 lun->lun = lun_number;
4730 lun->be_lun = be_lun;
4731 /*
4732 * The processor LUN is always enabled. Disk LUNs come on line
4733 * disabled, and must be enabled by the backend.
4734 */
4735 lun->flags |= CTL_LUN_DISABLED;
4736 lun->backend = be_lun->be;
4737 be_lun->ctl_lun = lun;
4738 be_lun->lun_id = lun_number;
4739 if (be_lun->flags & CTL_LUN_FLAG_EJECTED)
4740 lun->flags |= CTL_LUN_EJECTED;
4741 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA)
4742 lun->flags |= CTL_LUN_NO_MEDIA;
4743 if (be_lun->flags & CTL_LUN_FLAG_STOPPED)
4744 lun->flags |= CTL_LUN_STOPPED;
4745
4746 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4747 lun->flags |= CTL_LUN_PRIMARY_SC;
4748
4749 value = dnvlist_get_string(be_lun->options, "removable", NULL);
4750 if (value != NULL) {
4751 if (strcmp(value, "on") == 0)
4752 lun->flags |= CTL_LUN_REMOVABLE;
4753 } else if (be_lun->lun_type == T_CDROM)
4754 lun->flags |= CTL_LUN_REMOVABLE;
4755
4756 lun->ctl_softc = ctl_softc;
4757 #ifdef CTL_TIME_IO
4758 lun->last_busy = getsbinuptime();
4759 #endif
4760 LIST_INIT(&lun->ooa_queue);
4761 STAILQ_INIT(&lun->error_list);
4762 lun->ie_reported = 1;
4763 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0);
4764 ctl_tpc_lun_init(lun);
4765 if (lun->flags & CTL_LUN_REMOVABLE) {
4766 lun->prevent = malloc((CTL_MAX_INITIATORS + 31) / 32 * 4,
4767 M_CTL, M_WAITOK);
4768 }
4769
4770 /*
4771 * Initialize the mode and log page index.
4772 */
4773 ctl_init_page_index(lun);
4774 ctl_init_log_page_index(lun);
4775
4776 /* Setup statistics gathering */
4777 lun->stats.item = lun_number;
4778
4779 /*
4780 * Now, before we insert this lun on the lun list, set the lun
4781 * inventory changed UA for all other luns.
4782 */
4783 mtx_lock(&ctl_softc->ctl_lock);
4784 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4785 mtx_lock(&nlun->lun_lock);
4786 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4787 mtx_unlock(&nlun->lun_lock);
4788 }
4789 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4790 ctl_softc->ctl_luns[lun_number] = lun;
4791 ctl_softc->num_luns++;
4792 mtx_unlock(&ctl_softc->ctl_lock);
4793
4794 /*
4795 * We successfully added the LUN, attempt to enable it.
4796 */
4797 if (ctl_enable_lun(lun) != 0) {
4798 printf("%s: ctl_enable_lun() failed!\n", __func__);
4799 mtx_lock(&ctl_softc->ctl_lock);
4800 STAILQ_REMOVE(&ctl_softc->lun_list, lun, ctl_lun, links);
4801 ctl_clear_mask(ctl_softc->ctl_lun_mask, lun_number);
4802 ctl_softc->ctl_luns[lun_number] = NULL;
4803 ctl_softc->num_luns--;
4804 mtx_unlock(&ctl_softc->ctl_lock);
4805 free(lun->lun_devid, M_CTL);
4806 free(lun, M_CTL);
4807 return (EIO);
4808 }
4809
4810 return (0);
4811 }
4812
4813 /*
4814 * Free LUN that has no active requests.
4815 */
4816 static int
ctl_free_lun(struct ctl_lun * lun)4817 ctl_free_lun(struct ctl_lun *lun)
4818 {
4819 struct ctl_softc *softc = lun->ctl_softc;
4820 struct ctl_lun *nlun;
4821 int i;
4822
4823 KASSERT(LIST_EMPTY(&lun->ooa_queue),
4824 ("Freeing a LUN %p with outstanding I/O!\n", lun));
4825
4826 mtx_lock(&softc->ctl_lock);
4827 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4828 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4829 softc->ctl_luns[lun->lun] = NULL;
4830 softc->num_luns--;
4831 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4832 mtx_lock(&nlun->lun_lock);
4833 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4834 mtx_unlock(&nlun->lun_lock);
4835 }
4836 mtx_unlock(&softc->ctl_lock);
4837
4838 /*
4839 * Tell the backend to free resources, if this LUN has a backend.
4840 */
4841 lun->be_lun->lun_shutdown(lun->be_lun);
4842
4843 lun->ie_reportcnt = UINT32_MAX;
4844 callout_drain(&lun->ie_callout);
4845 ctl_tpc_lun_shutdown(lun);
4846 mtx_destroy(&lun->lun_lock);
4847 free(lun->lun_devid, M_CTL);
4848 for (i = 0; i < ctl_max_ports; i++)
4849 free(lun->pending_ua[i], M_CTL);
4850 free(lun->pending_ua, M_DEVBUF);
4851 for (i = 0; i < ctl_max_ports; i++)
4852 free(lun->pr_keys[i], M_CTL);
4853 free(lun->pr_keys, M_DEVBUF);
4854 free(lun->write_buffer, M_CTL);
4855 free(lun->prevent, M_CTL);
4856 free(lun, M_CTL);
4857
4858 return (0);
4859 }
4860
4861 static int
ctl_enable_lun(struct ctl_lun * lun)4862 ctl_enable_lun(struct ctl_lun *lun)
4863 {
4864 struct ctl_softc *softc;
4865 struct ctl_port *port, *nport;
4866 int retval;
4867
4868 softc = lun->ctl_softc;
4869
4870 mtx_lock(&softc->ctl_lock);
4871 mtx_lock(&lun->lun_lock);
4872 KASSERT((lun->flags & CTL_LUN_DISABLED) != 0,
4873 ("%s: LUN not disabled", __func__));
4874 lun->flags &= ~CTL_LUN_DISABLED;
4875 mtx_unlock(&lun->lun_lock);
4876
4877 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) {
4878 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4879 port->lun_map != NULL || port->lun_enable == NULL)
4880 continue;
4881
4882 /*
4883 * Drop the lock while we call the FETD's enable routine.
4884 * This can lead to a callback into CTL (at least in the
4885 * case of the internal initiator frontend.
4886 */
4887 mtx_unlock(&softc->ctl_lock);
4888 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4889 mtx_lock(&softc->ctl_lock);
4890 if (retval != 0) {
4891 printf("%s: FETD %s port %d returned error "
4892 "%d for lun_enable on lun %jd\n",
4893 __func__, port->port_name, port->targ_port,
4894 retval, (intmax_t)lun->lun);
4895 }
4896 }
4897
4898 mtx_unlock(&softc->ctl_lock);
4899 ctl_isc_announce_lun(lun);
4900
4901 return (0);
4902 }
4903
4904 static int
ctl_disable_lun(struct ctl_lun * lun)4905 ctl_disable_lun(struct ctl_lun *lun)
4906 {
4907 struct ctl_softc *softc;
4908 struct ctl_port *port;
4909 int retval;
4910
4911 softc = lun->ctl_softc;
4912
4913 mtx_lock(&softc->ctl_lock);
4914 mtx_lock(&lun->lun_lock);
4915 KASSERT((lun->flags & CTL_LUN_DISABLED) == 0,
4916 ("%s: LUN not enabled", __func__));
4917 lun->flags |= CTL_LUN_DISABLED;
4918 mtx_unlock(&lun->lun_lock);
4919
4920 STAILQ_FOREACH(port, &softc->port_list, links) {
4921 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4922 port->lun_map != NULL || port->lun_disable == NULL)
4923 continue;
4924
4925 /*
4926 * Drop the lock before we call the frontend's disable
4927 * routine, to avoid lock order reversals.
4928 *
4929 * XXX KDM what happens if the frontend list changes while
4930 * we're traversing it? It's unlikely, but should be handled.
4931 */
4932 mtx_unlock(&softc->ctl_lock);
4933 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4934 mtx_lock(&softc->ctl_lock);
4935 if (retval != 0) {
4936 printf("%s: FETD %s port %d returned error "
4937 "%d for lun_disable on lun %jd\n",
4938 __func__, port->port_name, port->targ_port,
4939 retval, (intmax_t)lun->lun);
4940 }
4941 }
4942
4943 mtx_unlock(&softc->ctl_lock);
4944 ctl_isc_announce_lun(lun);
4945
4946 return (0);
4947 }
4948
4949 int
ctl_start_lun(struct ctl_be_lun * be_lun)4950 ctl_start_lun(struct ctl_be_lun *be_lun)
4951 {
4952 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4953
4954 mtx_lock(&lun->lun_lock);
4955 lun->flags &= ~CTL_LUN_STOPPED;
4956 mtx_unlock(&lun->lun_lock);
4957 return (0);
4958 }
4959
4960 int
ctl_stop_lun(struct ctl_be_lun * be_lun)4961 ctl_stop_lun(struct ctl_be_lun *be_lun)
4962 {
4963 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4964
4965 mtx_lock(&lun->lun_lock);
4966 lun->flags |= CTL_LUN_STOPPED;
4967 mtx_unlock(&lun->lun_lock);
4968 return (0);
4969 }
4970
4971 int
ctl_lun_no_media(struct ctl_be_lun * be_lun)4972 ctl_lun_no_media(struct ctl_be_lun *be_lun)
4973 {
4974 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4975
4976 mtx_lock(&lun->lun_lock);
4977 lun->flags |= CTL_LUN_NO_MEDIA;
4978 mtx_unlock(&lun->lun_lock);
4979 return (0);
4980 }
4981
4982 int
ctl_lun_has_media(struct ctl_be_lun * be_lun)4983 ctl_lun_has_media(struct ctl_be_lun *be_lun)
4984 {
4985 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4986 union ctl_ha_msg msg;
4987
4988 mtx_lock(&lun->lun_lock);
4989 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED);
4990 if (lun->flags & CTL_LUN_REMOVABLE)
4991 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE);
4992 mtx_unlock(&lun->lun_lock);
4993 if ((lun->flags & CTL_LUN_REMOVABLE) &&
4994 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4995 bzero(&msg.ua, sizeof(msg.ua));
4996 msg.hdr.msg_type = CTL_MSG_UA;
4997 msg.hdr.nexus.initid = -1;
4998 msg.hdr.nexus.targ_port = -1;
4999 msg.hdr.nexus.targ_lun = lun->lun;
5000 msg.hdr.nexus.targ_mapped_lun = lun->lun;
5001 msg.ua.ua_all = 1;
5002 msg.ua.ua_set = 1;
5003 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE;
5004 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
5005 M_WAITOK);
5006 }
5007 return (0);
5008 }
5009
5010 int
ctl_lun_ejected(struct ctl_be_lun * be_lun)5011 ctl_lun_ejected(struct ctl_be_lun *be_lun)
5012 {
5013 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5014
5015 mtx_lock(&lun->lun_lock);
5016 lun->flags |= CTL_LUN_EJECTED;
5017 mtx_unlock(&lun->lun_lock);
5018 return (0);
5019 }
5020
5021 int
ctl_lun_primary(struct ctl_be_lun * be_lun)5022 ctl_lun_primary(struct ctl_be_lun *be_lun)
5023 {
5024 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5025
5026 mtx_lock(&lun->lun_lock);
5027 lun->flags |= CTL_LUN_PRIMARY_SC;
5028 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
5029 mtx_unlock(&lun->lun_lock);
5030 ctl_isc_announce_lun(lun);
5031 return (0);
5032 }
5033
5034 int
ctl_lun_secondary(struct ctl_be_lun * be_lun)5035 ctl_lun_secondary(struct ctl_be_lun *be_lun)
5036 {
5037 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5038
5039 mtx_lock(&lun->lun_lock);
5040 lun->flags &= ~CTL_LUN_PRIMARY_SC;
5041 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
5042 mtx_unlock(&lun->lun_lock);
5043 ctl_isc_announce_lun(lun);
5044 return (0);
5045 }
5046
5047 /*
5048 * Remove LUN. If there are active requests, wait for completion.
5049 *
5050 * Returns 0 for success, non-zero (errno) for failure.
5051 * Completion is reported to backed via the lun_shutdown() method.
5052 */
5053 int
ctl_remove_lun(struct ctl_be_lun * be_lun)5054 ctl_remove_lun(struct ctl_be_lun *be_lun)
5055 {
5056 struct ctl_lun *lun;
5057
5058 lun = (struct ctl_lun *)be_lun->ctl_lun;
5059
5060 ctl_disable_lun(lun);
5061
5062 mtx_lock(&lun->lun_lock);
5063 lun->flags |= CTL_LUN_INVALID;
5064
5065 /*
5066 * If there is nothing in the OOA queue, go ahead and free the LUN.
5067 * If we have something in the OOA queue, we'll free it when the
5068 * last I/O completes.
5069 */
5070 if (LIST_EMPTY(&lun->ooa_queue)) {
5071 mtx_unlock(&lun->lun_lock);
5072 ctl_free_lun(lun);
5073 } else
5074 mtx_unlock(&lun->lun_lock);
5075
5076 return (0);
5077 }
5078
5079 void
ctl_lun_capacity_changed(struct ctl_be_lun * be_lun)5080 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
5081 {
5082 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5083 union ctl_ha_msg msg;
5084
5085 mtx_lock(&lun->lun_lock);
5086 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE);
5087 mtx_unlock(&lun->lun_lock);
5088 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
5089 /* Send msg to other side. */
5090 bzero(&msg.ua, sizeof(msg.ua));
5091 msg.hdr.msg_type = CTL_MSG_UA;
5092 msg.hdr.nexus.initid = -1;
5093 msg.hdr.nexus.targ_port = -1;
5094 msg.hdr.nexus.targ_lun = lun->lun;
5095 msg.hdr.nexus.targ_mapped_lun = lun->lun;
5096 msg.ua.ua_all = 1;
5097 msg.ua.ua_set = 1;
5098 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE;
5099 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
5100 M_WAITOK);
5101 }
5102 }
5103
5104 /*
5105 * Backend "memory move is complete" callback for requests that never
5106 * make it down to say RAIDCore's configuration code.
5107 */
5108 int
ctl_config_move_done(union ctl_io * io,bool samethr)5109 ctl_config_move_done(union ctl_io *io, bool samethr)
5110 {
5111 int retval;
5112
5113 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5114 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
5115 ("%s: unexpected I/O type %x", __func__, io->io_hdr.io_type));
5116
5117 if (ctl_debug & CTL_DEBUG_CDB_DATA)
5118 ctl_data_print(io);
5119 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
5120 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5121 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
5122 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5123 /*
5124 * XXX KDM just assuming a single pointer here, and not a
5125 * S/G list. If we start using S/G lists for config data,
5126 * we'll need to know how to clean them up here as well.
5127 */
5128 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5129 free(io->scsiio.kern_data_ptr, M_CTL);
5130 ctl_done(io);
5131 retval = CTL_RETVAL_COMPLETE;
5132 } else {
5133 /*
5134 * XXX KDM now we need to continue data movement. Some
5135 * options:
5136 * - call ctl_scsiio() again? We don't do this for data
5137 * writes, because for those at least we know ahead of
5138 * time where the write will go and how long it is. For
5139 * config writes, though, that information is largely
5140 * contained within the write itself, thus we need to
5141 * parse out the data again.
5142 *
5143 * - Call some other function once the data is in?
5144 */
5145
5146 /*
5147 * XXX KDM call ctl_scsiio() again for now, and check flag
5148 * bits to see whether we're allocated or not.
5149 */
5150 retval = ctl_scsiio(&io->scsiio);
5151 }
5152 return (retval);
5153 }
5154
5155 /*
5156 * This gets called by a backend driver when it is done with a
5157 * data_submit method.
5158 */
5159 void
ctl_data_submit_done(union ctl_io * io)5160 ctl_data_submit_done(union ctl_io *io)
5161 {
5162 /*
5163 * If the IO_CONT flag is set, we need to call the supplied
5164 * function to continue processing the I/O, instead of completing
5165 * the I/O just yet.
5166 *
5167 * If there is an error, though, we don't want to keep processing.
5168 * Instead, just send status back to the initiator.
5169 */
5170 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5171 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5172 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5173 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5174 io->scsiio.io_cont(io);
5175 return;
5176 }
5177 ctl_done(io);
5178 }
5179
5180 /*
5181 * This gets called by a backend driver when it is done with a
5182 * configuration write.
5183 */
5184 void
ctl_config_write_done(union ctl_io * io)5185 ctl_config_write_done(union ctl_io *io)
5186 {
5187 uint8_t *buf;
5188
5189 /*
5190 * If the IO_CONT flag is set, we need to call the supplied
5191 * function to continue processing the I/O, instead of completing
5192 * the I/O just yet.
5193 *
5194 * If there is an error, though, we don't want to keep processing.
5195 * Instead, just send status back to the initiator.
5196 */
5197 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5198 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5199 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5200 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5201 io->scsiio.io_cont(io);
5202 return;
5203 }
5204 /*
5205 * Since a configuration write can be done for commands that actually
5206 * have data allocated, like write buffer, and commands that have
5207 * no data, like start/stop unit, we need to check here.
5208 */
5209 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5210 buf = io->scsiio.kern_data_ptr;
5211 else
5212 buf = NULL;
5213 ctl_done(io);
5214 if (buf)
5215 free(buf, M_CTL);
5216 }
5217
5218 void
ctl_config_read_done(union ctl_io * io)5219 ctl_config_read_done(union ctl_io *io)
5220 {
5221 uint8_t *buf;
5222
5223 /*
5224 * If there is some error -- we are done, skip data transfer.
5225 */
5226 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5227 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5228 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5229 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5230 buf = io->scsiio.kern_data_ptr;
5231 else
5232 buf = NULL;
5233 ctl_done(io);
5234 if (buf)
5235 free(buf, M_CTL);
5236 return;
5237 }
5238
5239 /*
5240 * If the IO_CONT flag is set, we need to call the supplied
5241 * function to continue processing the I/O, instead of completing
5242 * the I/O just yet.
5243 */
5244 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5245 io->scsiio.io_cont(io);
5246 return;
5247 }
5248
5249 ctl_datamove(io);
5250 }
5251
5252 /*
5253 * SCSI release command.
5254 */
5255 int
ctl_scsi_release(struct ctl_scsiio * ctsio)5256 ctl_scsi_release(struct ctl_scsiio *ctsio)
5257 {
5258 struct ctl_lun *lun = CTL_LUN(ctsio);
5259 uint32_t residx;
5260
5261 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5262
5263 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5264
5265 /*
5266 * XXX KDM right now, we only support LUN reservation. We don't
5267 * support 3rd party reservations, or extent reservations, which
5268 * might actually need the parameter list. If we've gotten this
5269 * far, we've got a LUN reservation. Anything else got kicked out
5270 * above. So, according to SPC, ignore the length.
5271 */
5272
5273 mtx_lock(&lun->lun_lock);
5274
5275 /*
5276 * According to SPC, it is not an error for an intiator to attempt
5277 * to release a reservation on a LUN that isn't reserved, or that
5278 * is reserved by another initiator. The reservation can only be
5279 * released, though, by the initiator who made it or by one of
5280 * several reset type events.
5281 */
5282 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5283 lun->flags &= ~CTL_LUN_RESERVED;
5284
5285 mtx_unlock(&lun->lun_lock);
5286
5287 ctl_set_success(ctsio);
5288 ctl_done((union ctl_io *)ctsio);
5289 return (CTL_RETVAL_COMPLETE);
5290 }
5291
5292 int
ctl_scsi_reserve(struct ctl_scsiio * ctsio)5293 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5294 {
5295 struct ctl_lun *lun = CTL_LUN(ctsio);
5296 uint32_t residx;
5297
5298 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5299
5300 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5301
5302 /*
5303 * XXX KDM right now, we only support LUN reservation. We don't
5304 * support 3rd party reservations, or extent reservations, which
5305 * might actually need the parameter list. If we've gotten this
5306 * far, we've got a LUN reservation. Anything else got kicked out
5307 * above. So, according to SPC, ignore the length.
5308 */
5309
5310 mtx_lock(&lun->lun_lock);
5311 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5312 ctl_set_reservation_conflict(ctsio);
5313 goto bailout;
5314 }
5315
5316 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */
5317 if (lun->flags & CTL_LUN_PR_RESERVED) {
5318 ctl_set_success(ctsio);
5319 goto bailout;
5320 }
5321
5322 lun->flags |= CTL_LUN_RESERVED;
5323 lun->res_idx = residx;
5324 ctl_set_success(ctsio);
5325
5326 bailout:
5327 mtx_unlock(&lun->lun_lock);
5328 ctl_done((union ctl_io *)ctsio);
5329 return (CTL_RETVAL_COMPLETE);
5330 }
5331
5332 int
ctl_start_stop(struct ctl_scsiio * ctsio)5333 ctl_start_stop(struct ctl_scsiio *ctsio)
5334 {
5335 struct ctl_lun *lun = CTL_LUN(ctsio);
5336 struct scsi_start_stop_unit *cdb;
5337 int retval;
5338
5339 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5340
5341 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5342
5343 if ((cdb->how & SSS_PC_MASK) == 0) {
5344 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
5345 (cdb->how & SSS_START) == 0) {
5346 uint32_t residx;
5347
5348 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5349 if (ctl_get_prkey(lun, residx) == 0 ||
5350 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) {
5351 ctl_set_reservation_conflict(ctsio);
5352 ctl_done((union ctl_io *)ctsio);
5353 return (CTL_RETVAL_COMPLETE);
5354 }
5355 }
5356
5357 if ((cdb->how & SSS_LOEJ) &&
5358 (lun->flags & CTL_LUN_REMOVABLE) == 0) {
5359 ctl_set_invalid_field(ctsio,
5360 /*sks_valid*/ 1,
5361 /*command*/ 1,
5362 /*field*/ 4,
5363 /*bit_valid*/ 1,
5364 /*bit*/ 1);
5365 ctl_done((union ctl_io *)ctsio);
5366 return (CTL_RETVAL_COMPLETE);
5367 }
5368
5369 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) &&
5370 lun->prevent_count > 0) {
5371 /* "Medium removal prevented" */
5372 ctl_set_sense(ctsio, /*current_error*/ 1,
5373 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ?
5374 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST,
5375 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE);
5376 ctl_done((union ctl_io *)ctsio);
5377 return (CTL_RETVAL_COMPLETE);
5378 }
5379 }
5380
5381 retval = lun->backend->config_write((union ctl_io *)ctsio);
5382 return (retval);
5383 }
5384
5385 int
ctl_prevent_allow(struct ctl_scsiio * ctsio)5386 ctl_prevent_allow(struct ctl_scsiio *ctsio)
5387 {
5388 struct ctl_lun *lun = CTL_LUN(ctsio);
5389 struct scsi_prevent *cdb;
5390 int retval;
5391 uint32_t initidx;
5392
5393 CTL_DEBUG_PRINT(("ctl_prevent_allow\n"));
5394
5395 cdb = (struct scsi_prevent *)ctsio->cdb;
5396
5397 if ((lun->flags & CTL_LUN_REMOVABLE) == 0 || lun->prevent == NULL) {
5398 ctl_set_invalid_opcode(ctsio);
5399 ctl_done((union ctl_io *)ctsio);
5400 return (CTL_RETVAL_COMPLETE);
5401 }
5402
5403 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5404 mtx_lock(&lun->lun_lock);
5405 if ((cdb->how & PR_PREVENT) &&
5406 ctl_is_set(lun->prevent, initidx) == 0) {
5407 ctl_set_mask(lun->prevent, initidx);
5408 lun->prevent_count++;
5409 } else if ((cdb->how & PR_PREVENT) == 0 &&
5410 ctl_is_set(lun->prevent, initidx)) {
5411 ctl_clear_mask(lun->prevent, initidx);
5412 lun->prevent_count--;
5413 }
5414 mtx_unlock(&lun->lun_lock);
5415 retval = lun->backend->config_write((union ctl_io *)ctsio);
5416 return (retval);
5417 }
5418
5419 /*
5420 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5421 * we don't really do anything with the LBA and length fields if the user
5422 * passes them in. Instead we'll just flush out the cache for the entire
5423 * LUN.
5424 */
5425 int
ctl_sync_cache(struct ctl_scsiio * ctsio)5426 ctl_sync_cache(struct ctl_scsiio *ctsio)
5427 {
5428 struct ctl_lun *lun = CTL_LUN(ctsio);
5429 struct ctl_lba_len_flags *lbalen;
5430 uint64_t starting_lba;
5431 uint32_t block_count;
5432 int retval;
5433 uint8_t byte2;
5434
5435 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5436
5437 retval = 0;
5438
5439 switch (ctsio->cdb[0]) {
5440 case SYNCHRONIZE_CACHE: {
5441 struct scsi_sync_cache *cdb;
5442 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5443
5444 starting_lba = scsi_4btoul(cdb->begin_lba);
5445 block_count = scsi_2btoul(cdb->lb_count);
5446 byte2 = cdb->byte2;
5447 break;
5448 }
5449 case SYNCHRONIZE_CACHE_16: {
5450 struct scsi_sync_cache_16 *cdb;
5451 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5452
5453 starting_lba = scsi_8btou64(cdb->begin_lba);
5454 block_count = scsi_4btoul(cdb->lb_count);
5455 byte2 = cdb->byte2;
5456 break;
5457 }
5458 default:
5459 ctl_set_invalid_opcode(ctsio);
5460 ctl_done((union ctl_io *)ctsio);
5461 goto bailout;
5462 break; /* NOTREACHED */
5463 }
5464
5465 /*
5466 * We check the LBA and length, but don't do anything with them.
5467 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5468 * get flushed. This check will just help satisfy anyone who wants
5469 * to see an error for an out of range LBA.
5470 */
5471 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5472 ctl_set_lba_out_of_range(ctsio,
5473 MAX(starting_lba, lun->be_lun->maxlba + 1));
5474 ctl_done((union ctl_io *)ctsio);
5475 goto bailout;
5476 }
5477
5478 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5479 lbalen->lba = starting_lba;
5480 lbalen->len = block_count;
5481 lbalen->flags = byte2;
5482 retval = lun->backend->config_write((union ctl_io *)ctsio);
5483
5484 bailout:
5485 return (retval);
5486 }
5487
5488 int
ctl_format(struct ctl_scsiio * ctsio)5489 ctl_format(struct ctl_scsiio *ctsio)
5490 {
5491 struct scsi_format *cdb;
5492 int length, defect_list_len;
5493
5494 CTL_DEBUG_PRINT(("ctl_format\n"));
5495
5496 cdb = (struct scsi_format *)ctsio->cdb;
5497
5498 length = 0;
5499 if (cdb->byte2 & SF_FMTDATA) {
5500 if (cdb->byte2 & SF_LONGLIST)
5501 length = sizeof(struct scsi_format_header_long);
5502 else
5503 length = sizeof(struct scsi_format_header_short);
5504 }
5505
5506 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5507 && (length > 0)) {
5508 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5509 ctsio->kern_data_len = length;
5510 ctsio->kern_total_len = length;
5511 ctsio->kern_rel_offset = 0;
5512 ctsio->kern_sg_entries = 0;
5513 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5514 ctsio->be_move_done = ctl_config_move_done;
5515 ctl_datamove((union ctl_io *)ctsio);
5516
5517 return (CTL_RETVAL_COMPLETE);
5518 }
5519
5520 defect_list_len = 0;
5521
5522 if (cdb->byte2 & SF_FMTDATA) {
5523 if (cdb->byte2 & SF_LONGLIST) {
5524 struct scsi_format_header_long *header;
5525
5526 header = (struct scsi_format_header_long *)
5527 ctsio->kern_data_ptr;
5528
5529 defect_list_len = scsi_4btoul(header->defect_list_len);
5530 if (defect_list_len != 0) {
5531 ctl_set_invalid_field(ctsio,
5532 /*sks_valid*/ 1,
5533 /*command*/ 0,
5534 /*field*/ 2,
5535 /*bit_valid*/ 0,
5536 /*bit*/ 0);
5537 goto bailout;
5538 }
5539 } else {
5540 struct scsi_format_header_short *header;
5541
5542 header = (struct scsi_format_header_short *)
5543 ctsio->kern_data_ptr;
5544
5545 defect_list_len = scsi_2btoul(header->defect_list_len);
5546 if (defect_list_len != 0) {
5547 ctl_set_invalid_field(ctsio,
5548 /*sks_valid*/ 1,
5549 /*command*/ 0,
5550 /*field*/ 2,
5551 /*bit_valid*/ 0,
5552 /*bit*/ 0);
5553 goto bailout;
5554 }
5555 }
5556 }
5557
5558 ctl_set_success(ctsio);
5559 bailout:
5560
5561 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5562 free(ctsio->kern_data_ptr, M_CTL);
5563 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5564 }
5565
5566 ctl_done((union ctl_io *)ctsio);
5567 return (CTL_RETVAL_COMPLETE);
5568 }
5569
5570 int
ctl_read_buffer(struct ctl_scsiio * ctsio)5571 ctl_read_buffer(struct ctl_scsiio *ctsio)
5572 {
5573 struct ctl_lun *lun = CTL_LUN(ctsio);
5574 uint64_t buffer_offset;
5575 uint32_t len;
5576 uint8_t byte2;
5577 static uint8_t descr[4];
5578 static uint8_t echo_descr[4] = { 0 };
5579
5580 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5581
5582 switch (ctsio->cdb[0]) {
5583 case READ_BUFFER: {
5584 struct scsi_read_buffer *cdb;
5585
5586 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5587 buffer_offset = scsi_3btoul(cdb->offset);
5588 len = scsi_3btoul(cdb->length);
5589 byte2 = cdb->byte2;
5590 break;
5591 }
5592 case READ_BUFFER_16: {
5593 struct scsi_read_buffer_16 *cdb;
5594
5595 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb;
5596 buffer_offset = scsi_8btou64(cdb->offset);
5597 len = scsi_4btoul(cdb->length);
5598 byte2 = cdb->byte2;
5599 break;
5600 }
5601 default: /* This shouldn't happen. */
5602 ctl_set_invalid_opcode(ctsio);
5603 ctl_done((union ctl_io *)ctsio);
5604 return (CTL_RETVAL_COMPLETE);
5605 }
5606
5607 if (buffer_offset > CTL_WRITE_BUFFER_SIZE ||
5608 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5609 ctl_set_invalid_field(ctsio,
5610 /*sks_valid*/ 1,
5611 /*command*/ 1,
5612 /*field*/ 6,
5613 /*bit_valid*/ 0,
5614 /*bit*/ 0);
5615 ctl_done((union ctl_io *)ctsio);
5616 return (CTL_RETVAL_COMPLETE);
5617 }
5618
5619 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5620 descr[0] = 0;
5621 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5622 ctsio->kern_data_ptr = descr;
5623 len = min(len, sizeof(descr));
5624 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5625 ctsio->kern_data_ptr = echo_descr;
5626 len = min(len, sizeof(echo_descr));
5627 } else {
5628 if (lun->write_buffer == NULL) {
5629 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5630 M_CTL, M_WAITOK | M_ZERO);
5631 }
5632 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5633 }
5634 ctsio->kern_data_len = len;
5635 ctsio->kern_total_len = len;
5636 ctsio->kern_rel_offset = 0;
5637 ctsio->kern_sg_entries = 0;
5638 ctl_set_success(ctsio);
5639 ctsio->be_move_done = ctl_config_move_done;
5640 ctl_datamove((union ctl_io *)ctsio);
5641 return (CTL_RETVAL_COMPLETE);
5642 }
5643
5644 int
ctl_write_buffer(struct ctl_scsiio * ctsio)5645 ctl_write_buffer(struct ctl_scsiio *ctsio)
5646 {
5647 struct ctl_lun *lun = CTL_LUN(ctsio);
5648 struct scsi_write_buffer *cdb;
5649 int buffer_offset, len;
5650
5651 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5652
5653 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5654
5655 len = scsi_3btoul(cdb->length);
5656 buffer_offset = scsi_3btoul(cdb->offset);
5657
5658 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5659 ctl_set_invalid_field(ctsio,
5660 /*sks_valid*/ 1,
5661 /*command*/ 1,
5662 /*field*/ 6,
5663 /*bit_valid*/ 0,
5664 /*bit*/ 0);
5665 ctl_done((union ctl_io *)ctsio);
5666 return (CTL_RETVAL_COMPLETE);
5667 }
5668
5669 if (lun->write_buffer == NULL) {
5670 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5671 M_CTL, M_WAITOK | M_ZERO);
5672 }
5673
5674 /*
5675 * If this kernel request hasn't started yet, initialize the data
5676 * buffer to the correct region of the LUN's write buffer. Note that
5677 * this doesn't set CTL_FLAG_ALLOCATED since this points into a
5678 * persistent buffer belonging to the LUN rather than a buffer
5679 * dedicated to this request.
5680 */
5681 if (ctsio->kern_data_ptr == NULL) {
5682 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5683 ctsio->kern_data_len = len;
5684 ctsio->kern_total_len = len;
5685 ctsio->kern_rel_offset = 0;
5686 ctsio->kern_sg_entries = 0;
5687 ctsio->be_move_done = ctl_config_move_done;
5688 ctl_datamove((union ctl_io *)ctsio);
5689
5690 return (CTL_RETVAL_COMPLETE);
5691 }
5692
5693 ctl_set_success(ctsio);
5694 ctl_done((union ctl_io *)ctsio);
5695 return (CTL_RETVAL_COMPLETE);
5696 }
5697
5698 static int
ctl_write_same_cont(union ctl_io * io)5699 ctl_write_same_cont(union ctl_io *io)
5700 {
5701 struct ctl_lun *lun = CTL_LUN(io);
5702 struct ctl_scsiio *ctsio;
5703 struct ctl_lba_len_flags *lbalen;
5704 int retval;
5705
5706 ctsio = &io->scsiio;
5707 ctsio->io_hdr.status = CTL_STATUS_NONE;
5708 lbalen = (struct ctl_lba_len_flags *)
5709 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5710 lbalen->lba += lbalen->len;
5711 if ((lun->be_lun->maxlba + 1) - lbalen->lba <= UINT32_MAX) {
5712 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
5713 lbalen->len = (lun->be_lun->maxlba + 1) - lbalen->lba;
5714 }
5715
5716 CTL_DEBUG_PRINT(("ctl_write_same_cont: calling config_write()\n"));
5717 retval = lun->backend->config_write((union ctl_io *)ctsio);
5718 return (retval);
5719 }
5720
5721 int
ctl_write_same(struct ctl_scsiio * ctsio)5722 ctl_write_same(struct ctl_scsiio *ctsio)
5723 {
5724 struct ctl_lun *lun = CTL_LUN(ctsio);
5725 struct ctl_lba_len_flags *lbalen;
5726 const char *val;
5727 uint64_t lba, ival;
5728 uint32_t num_blocks;
5729 int len, retval;
5730 uint8_t byte2;
5731
5732 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5733
5734 switch (ctsio->cdb[0]) {
5735 case WRITE_SAME_10: {
5736 struct scsi_write_same_10 *cdb;
5737
5738 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5739
5740 lba = scsi_4btoul(cdb->addr);
5741 num_blocks = scsi_2btoul(cdb->length);
5742 byte2 = cdb->byte2;
5743 break;
5744 }
5745 case WRITE_SAME_16: {
5746 struct scsi_write_same_16 *cdb;
5747
5748 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5749
5750 lba = scsi_8btou64(cdb->addr);
5751 num_blocks = scsi_4btoul(cdb->length);
5752 byte2 = cdb->byte2;
5753 break;
5754 }
5755 default:
5756 /*
5757 * We got a command we don't support. This shouldn't
5758 * happen, commands should be filtered out above us.
5759 */
5760 ctl_set_invalid_opcode(ctsio);
5761 ctl_done((union ctl_io *)ctsio);
5762
5763 return (CTL_RETVAL_COMPLETE);
5764 break; /* NOTREACHED */
5765 }
5766
5767 /* ANCHOR flag can be used only together with UNMAP */
5768 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) {
5769 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5770 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5771 ctl_done((union ctl_io *)ctsio);
5772 return (CTL_RETVAL_COMPLETE);
5773 }
5774
5775 /*
5776 * The first check is to make sure we're in bounds, the second
5777 * check is to catch wrap-around problems. If the lba + num blocks
5778 * is less than the lba, then we've wrapped around and the block
5779 * range is invalid anyway.
5780 */
5781 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5782 || ((lba + num_blocks) < lba)) {
5783 ctl_set_lba_out_of_range(ctsio,
5784 MAX(lba, lun->be_lun->maxlba + 1));
5785 ctl_done((union ctl_io *)ctsio);
5786 return (CTL_RETVAL_COMPLETE);
5787 }
5788
5789 /* Zero number of blocks means "to the last logical block" */
5790 if (num_blocks == 0) {
5791 ival = UINT64_MAX;
5792 val = dnvlist_get_string(lun->be_lun->options,
5793 "write_same_max_lba", NULL);
5794 if (val != NULL)
5795 ctl_expand_number(val, &ival);
5796 if ((lun->be_lun->maxlba + 1) - lba > ival) {
5797 ctl_set_invalid_field(ctsio,
5798 /*sks_valid*/ 1, /*command*/ 1,
5799 /*field*/ ctsio->cdb[0] == WRITE_SAME_10 ? 7 : 10,
5800 /*bit_valid*/ 0, /*bit*/ 0);
5801 ctl_done((union ctl_io *)ctsio);
5802 return (CTL_RETVAL_COMPLETE);
5803 }
5804 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5805 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
5806 ctsio->io_cont = ctl_write_same_cont;
5807 num_blocks = 1 << 31;
5808 } else
5809 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5810 }
5811
5812 len = lun->be_lun->blocksize;
5813
5814 /*
5815 * If we've got a kernel request that hasn't been malloced yet,
5816 * malloc it and tell the caller the data buffer is here.
5817 */
5818 if ((byte2 & SWS_NDOB) == 0 &&
5819 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5820 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5821 ctsio->kern_data_len = len;
5822 ctsio->kern_total_len = len;
5823 ctsio->kern_rel_offset = 0;
5824 ctsio->kern_sg_entries = 0;
5825 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5826 ctsio->be_move_done = ctl_config_move_done;
5827 ctl_datamove((union ctl_io *)ctsio);
5828
5829 return (CTL_RETVAL_COMPLETE);
5830 }
5831
5832 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5833 lbalen->lba = lba;
5834 lbalen->len = num_blocks;
5835 lbalen->flags = byte2;
5836 retval = lun->backend->config_write((union ctl_io *)ctsio);
5837
5838 return (retval);
5839 }
5840
5841 int
ctl_unmap(struct ctl_scsiio * ctsio)5842 ctl_unmap(struct ctl_scsiio *ctsio)
5843 {
5844 struct ctl_lun *lun = CTL_LUN(ctsio);
5845 struct scsi_unmap *cdb;
5846 struct ctl_ptr_len_flags *ptrlen;
5847 struct scsi_unmap_header *hdr;
5848 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5849 uint64_t lba;
5850 uint32_t num_blocks;
5851 int len, retval;
5852 uint8_t byte2;
5853
5854 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5855
5856 cdb = (struct scsi_unmap *)ctsio->cdb;
5857 len = scsi_2btoul(cdb->length);
5858 byte2 = cdb->byte2;
5859
5860 /*
5861 * If we've got a kernel request that hasn't been malloced yet,
5862 * malloc it and tell the caller the data buffer is here.
5863 */
5864 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5865 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5866 ctsio->kern_data_len = len;
5867 ctsio->kern_total_len = len;
5868 ctsio->kern_rel_offset = 0;
5869 ctsio->kern_sg_entries = 0;
5870 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5871 ctsio->be_move_done = ctl_config_move_done;
5872 ctl_datamove((union ctl_io *)ctsio);
5873
5874 return (CTL_RETVAL_COMPLETE);
5875 }
5876
5877 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5878 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5879 if (len < sizeof (*hdr) ||
5880 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5881 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5882 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5883 ctl_set_invalid_field(ctsio,
5884 /*sks_valid*/ 0,
5885 /*command*/ 0,
5886 /*field*/ 0,
5887 /*bit_valid*/ 0,
5888 /*bit*/ 0);
5889 goto done;
5890 }
5891 len = scsi_2btoul(hdr->desc_length);
5892 buf = (struct scsi_unmap_desc *)(hdr + 1);
5893 end = buf + len / sizeof(*buf);
5894
5895 endnz = buf;
5896 for (range = buf; range < end; range++) {
5897 lba = scsi_8btou64(range->lba);
5898 num_blocks = scsi_4btoul(range->length);
5899 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5900 || ((lba + num_blocks) < lba)) {
5901 ctl_set_lba_out_of_range(ctsio,
5902 MAX(lba, lun->be_lun->maxlba + 1));
5903 ctl_done((union ctl_io *)ctsio);
5904 return (CTL_RETVAL_COMPLETE);
5905 }
5906 if (num_blocks != 0)
5907 endnz = range + 1;
5908 }
5909
5910 /*
5911 * Block backend can not handle zero last range.
5912 * Filter it out and return if there is nothing left.
5913 */
5914 len = (uint8_t *)endnz - (uint8_t *)buf;
5915 if (len == 0) {
5916 ctl_set_success(ctsio);
5917 goto done;
5918 }
5919
5920 mtx_lock(&lun->lun_lock);
5921 ptrlen = (struct ctl_ptr_len_flags *)
5922 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5923 ptrlen->ptr = (void *)buf;
5924 ptrlen->len = len;
5925 ptrlen->flags = byte2;
5926 ctl_try_unblock_others(lun, (union ctl_io *)ctsio, FALSE);
5927 mtx_unlock(&lun->lun_lock);
5928
5929 retval = lun->backend->config_write((union ctl_io *)ctsio);
5930 return (retval);
5931
5932 done:
5933 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5934 free(ctsio->kern_data_ptr, M_CTL);
5935 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5936 }
5937 ctl_done((union ctl_io *)ctsio);
5938 return (CTL_RETVAL_COMPLETE);
5939 }
5940
5941 int
ctl_default_page_handler(struct ctl_scsiio * ctsio,struct ctl_page_index * page_index,uint8_t * page_ptr)5942 ctl_default_page_handler(struct ctl_scsiio *ctsio,
5943 struct ctl_page_index *page_index, uint8_t *page_ptr)
5944 {
5945 struct ctl_lun *lun = CTL_LUN(ctsio);
5946 uint8_t *current_cp;
5947 int set_ua;
5948 uint32_t initidx;
5949
5950 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5951 set_ua = 0;
5952
5953 current_cp = (page_index->page_data + (page_index->page_len *
5954 CTL_PAGE_CURRENT));
5955
5956 mtx_lock(&lun->lun_lock);
5957 if (memcmp(current_cp, page_ptr, page_index->page_len)) {
5958 memcpy(current_cp, page_ptr, page_index->page_len);
5959 set_ua = 1;
5960 }
5961 if (set_ua != 0)
5962 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5963 mtx_unlock(&lun->lun_lock);
5964 if (set_ua) {
5965 ctl_isc_announce_mode(lun,
5966 ctl_get_initindex(&ctsio->io_hdr.nexus),
5967 page_index->page_code, page_index->subpage);
5968 }
5969 return (CTL_RETVAL_COMPLETE);
5970 }
5971
5972 static void
ctl_ie_timer(void * arg)5973 ctl_ie_timer(void *arg)
5974 {
5975 struct ctl_lun *lun = arg;
5976 uint64_t t;
5977
5978 if (lun->ie_asc == 0)
5979 return;
5980
5981 if (lun->MODE_IE.mrie == SIEP_MRIE_UA)
5982 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5983 else
5984 lun->ie_reported = 0;
5985
5986 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) {
5987 lun->ie_reportcnt++;
5988 t = scsi_4btoul(lun->MODE_IE.interval_timer);
5989 if (t == 0 || t == UINT32_MAX)
5990 t = 3000; /* 5 min */
5991 callout_schedule_sbt(&lun->ie_callout, SBT_1S / 10 * t,
5992 SBT_1S / 10, 0);
5993 }
5994 }
5995
5996 int
ctl_ie_page_handler(struct ctl_scsiio * ctsio,struct ctl_page_index * page_index,uint8_t * page_ptr)5997 ctl_ie_page_handler(struct ctl_scsiio *ctsio,
5998 struct ctl_page_index *page_index, uint8_t *page_ptr)
5999 {
6000 struct ctl_lun *lun = CTL_LUN(ctsio);
6001 struct scsi_info_exceptions_page *pg;
6002 uint64_t t;
6003
6004 (void)ctl_default_page_handler(ctsio, page_index, page_ptr);
6005
6006 pg = (struct scsi_info_exceptions_page *)page_ptr;
6007 mtx_lock(&lun->lun_lock);
6008 if (pg->info_flags & SIEP_FLAGS_TEST) {
6009 lun->ie_asc = 0x5d;
6010 lun->ie_ascq = 0xff;
6011 if (pg->mrie == SIEP_MRIE_UA) {
6012 ctl_est_ua_all(lun, -1, CTL_UA_IE);
6013 lun->ie_reported = 1;
6014 } else {
6015 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
6016 lun->ie_reported = -1;
6017 }
6018 lun->ie_reportcnt = 1;
6019 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) {
6020 lun->ie_reportcnt++;
6021 t = scsi_4btoul(pg->interval_timer);
6022 if (t == 0 || t == UINT32_MAX)
6023 t = 3000; /* 5 min */
6024 callout_reset_sbt(&lun->ie_callout, SBT_1S / 10 * t,
6025 SBT_1S / 10, ctl_ie_timer, lun, 0);
6026 }
6027 } else {
6028 lun->ie_asc = 0;
6029 lun->ie_ascq = 0;
6030 lun->ie_reported = 1;
6031 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
6032 lun->ie_reportcnt = UINT32_MAX;
6033 callout_stop(&lun->ie_callout);
6034 }
6035 mtx_unlock(&lun->lun_lock);
6036 return (CTL_RETVAL_COMPLETE);
6037 }
6038
6039 static int
ctl_do_mode_select(union ctl_io * io)6040 ctl_do_mode_select(union ctl_io *io)
6041 {
6042 struct ctl_lun *lun = CTL_LUN(io);
6043 struct scsi_mode_page_header *page_header;
6044 struct ctl_page_index *page_index;
6045 struct ctl_scsiio *ctsio;
6046 int page_len, page_len_offset, page_len_size;
6047 union ctl_modepage_info *modepage_info;
6048 uint16_t *len_left, *len_used;
6049 int retval, i;
6050
6051 ctsio = &io->scsiio;
6052 page_index = NULL;
6053 page_len = 0;
6054
6055 modepage_info = (union ctl_modepage_info *)
6056 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6057 len_left = &modepage_info->header.len_left;
6058 len_used = &modepage_info->header.len_used;
6059
6060 do_next_page:
6061
6062 page_header = (struct scsi_mode_page_header *)
6063 (ctsio->kern_data_ptr + *len_used);
6064
6065 if (*len_left == 0) {
6066 free(ctsio->kern_data_ptr, M_CTL);
6067 ctl_set_success(ctsio);
6068 ctl_done((union ctl_io *)ctsio);
6069 return (CTL_RETVAL_COMPLETE);
6070 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6071 free(ctsio->kern_data_ptr, M_CTL);
6072 ctl_set_param_len_error(ctsio);
6073 ctl_done((union ctl_io *)ctsio);
6074 return (CTL_RETVAL_COMPLETE);
6075
6076 } else if ((page_header->page_code & SMPH_SPF)
6077 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6078 free(ctsio->kern_data_ptr, M_CTL);
6079 ctl_set_param_len_error(ctsio);
6080 ctl_done((union ctl_io *)ctsio);
6081 return (CTL_RETVAL_COMPLETE);
6082 }
6083
6084 /*
6085 * XXX KDM should we do something with the block descriptor?
6086 */
6087 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6088 page_index = &lun->mode_pages.index[i];
6089 if (lun->be_lun->lun_type == T_DIRECT &&
6090 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6091 continue;
6092 if (lun->be_lun->lun_type == T_PROCESSOR &&
6093 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6094 continue;
6095 if (lun->be_lun->lun_type == T_CDROM &&
6096 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6097 continue;
6098
6099 if ((page_index->page_code & SMPH_PC_MASK) !=
6100 (page_header->page_code & SMPH_PC_MASK))
6101 continue;
6102
6103 /*
6104 * If neither page has a subpage code, then we've got a
6105 * match.
6106 */
6107 if (((page_index->page_code & SMPH_SPF) == 0)
6108 && ((page_header->page_code & SMPH_SPF) == 0)) {
6109 page_len = page_header->page_length;
6110 break;
6111 }
6112
6113 /*
6114 * If both pages have subpages, then the subpage numbers
6115 * have to match.
6116 */
6117 if ((page_index->page_code & SMPH_SPF)
6118 && (page_header->page_code & SMPH_SPF)) {
6119 struct scsi_mode_page_header_sp *sph;
6120
6121 sph = (struct scsi_mode_page_header_sp *)page_header;
6122 if (page_index->subpage == sph->subpage) {
6123 page_len = scsi_2btoul(sph->page_length);
6124 break;
6125 }
6126 }
6127 }
6128
6129 /*
6130 * If we couldn't find the page, or if we don't have a mode select
6131 * handler for it, send back an error to the user.
6132 */
6133 if ((i >= CTL_NUM_MODE_PAGES)
6134 || (page_index->select_handler == NULL)) {
6135 ctl_set_invalid_field(ctsio,
6136 /*sks_valid*/ 1,
6137 /*command*/ 0,
6138 /*field*/ *len_used,
6139 /*bit_valid*/ 0,
6140 /*bit*/ 0);
6141 free(ctsio->kern_data_ptr, M_CTL);
6142 ctl_done((union ctl_io *)ctsio);
6143 return (CTL_RETVAL_COMPLETE);
6144 }
6145
6146 if (page_index->page_code & SMPH_SPF) {
6147 page_len_offset = 2;
6148 page_len_size = 2;
6149 } else {
6150 page_len_size = 1;
6151 page_len_offset = 1;
6152 }
6153
6154 /*
6155 * If the length the initiator gives us isn't the one we specify in
6156 * the mode page header, or if they didn't specify enough data in
6157 * the CDB to avoid truncating this page, kick out the request.
6158 */
6159 if (page_len != page_index->page_len - page_len_offset - page_len_size) {
6160 ctl_set_invalid_field(ctsio,
6161 /*sks_valid*/ 1,
6162 /*command*/ 0,
6163 /*field*/ *len_used + page_len_offset,
6164 /*bit_valid*/ 0,
6165 /*bit*/ 0);
6166 free(ctsio->kern_data_ptr, M_CTL);
6167 ctl_done((union ctl_io *)ctsio);
6168 return (CTL_RETVAL_COMPLETE);
6169 }
6170 if (*len_left < page_index->page_len) {
6171 free(ctsio->kern_data_ptr, M_CTL);
6172 ctl_set_param_len_error(ctsio);
6173 ctl_done((union ctl_io *)ctsio);
6174 return (CTL_RETVAL_COMPLETE);
6175 }
6176
6177 /*
6178 * Run through the mode page, checking to make sure that the bits
6179 * the user changed are actually legal for him to change.
6180 */
6181 for (i = 0; i < page_index->page_len; i++) {
6182 uint8_t *user_byte, *change_mask, *current_byte;
6183 int bad_bit;
6184 int j;
6185
6186 user_byte = (uint8_t *)page_header + i;
6187 change_mask = page_index->page_data +
6188 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6189 current_byte = page_index->page_data +
6190 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6191
6192 /*
6193 * Check to see whether the user set any bits in this byte
6194 * that he is not allowed to set.
6195 */
6196 if ((*user_byte & ~(*change_mask)) ==
6197 (*current_byte & ~(*change_mask)))
6198 continue;
6199
6200 /*
6201 * Go through bit by bit to determine which one is illegal.
6202 */
6203 bad_bit = 0;
6204 for (j = 7; j >= 0; j--) {
6205 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6206 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6207 bad_bit = i;
6208 break;
6209 }
6210 }
6211 ctl_set_invalid_field(ctsio,
6212 /*sks_valid*/ 1,
6213 /*command*/ 0,
6214 /*field*/ *len_used + i,
6215 /*bit_valid*/ 1,
6216 /*bit*/ bad_bit);
6217 free(ctsio->kern_data_ptr, M_CTL);
6218 ctl_done((union ctl_io *)ctsio);
6219 return (CTL_RETVAL_COMPLETE);
6220 }
6221
6222 /*
6223 * Decrement these before we call the page handler, since we may
6224 * end up getting called back one way or another before the handler
6225 * returns to this context.
6226 */
6227 *len_left -= page_index->page_len;
6228 *len_used += page_index->page_len;
6229
6230 retval = page_index->select_handler(ctsio, page_index,
6231 (uint8_t *)page_header);
6232
6233 /*
6234 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6235 * wait until this queued command completes to finish processing
6236 * the mode page. If it returns anything other than
6237 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6238 * already set the sense information, freed the data pointer, and
6239 * completed the io for us.
6240 */
6241 if (retval != CTL_RETVAL_COMPLETE)
6242 goto bailout_no_done;
6243
6244 /*
6245 * If the initiator sent us more than one page, parse the next one.
6246 */
6247 if (*len_left > 0)
6248 goto do_next_page;
6249
6250 ctl_set_success(ctsio);
6251 free(ctsio->kern_data_ptr, M_CTL);
6252 ctl_done((union ctl_io *)ctsio);
6253
6254 bailout_no_done:
6255
6256 return (CTL_RETVAL_COMPLETE);
6257
6258 }
6259
6260 int
ctl_mode_select(struct ctl_scsiio * ctsio)6261 ctl_mode_select(struct ctl_scsiio *ctsio)
6262 {
6263 struct ctl_lun *lun = CTL_LUN(ctsio);
6264 union ctl_modepage_info *modepage_info;
6265 int bd_len, i, header_size, param_len, rtd;
6266 uint32_t initidx;
6267
6268 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6269 switch (ctsio->cdb[0]) {
6270 case MODE_SELECT_6: {
6271 struct scsi_mode_select_6 *cdb;
6272
6273 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6274
6275 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6276 param_len = cdb->length;
6277 header_size = sizeof(struct scsi_mode_header_6);
6278 break;
6279 }
6280 case MODE_SELECT_10: {
6281 struct scsi_mode_select_10 *cdb;
6282
6283 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6284
6285 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6286 param_len = scsi_2btoul(cdb->length);
6287 header_size = sizeof(struct scsi_mode_header_10);
6288 break;
6289 }
6290 default:
6291 ctl_set_invalid_opcode(ctsio);
6292 ctl_done((union ctl_io *)ctsio);
6293 return (CTL_RETVAL_COMPLETE);
6294 }
6295
6296 if (rtd) {
6297 if (param_len != 0) {
6298 ctl_set_invalid_field(ctsio, /*sks_valid*/ 0,
6299 /*command*/ 1, /*field*/ 0,
6300 /*bit_valid*/ 0, /*bit*/ 0);
6301 ctl_done((union ctl_io *)ctsio);
6302 return (CTL_RETVAL_COMPLETE);
6303 }
6304
6305 /* Revert to defaults. */
6306 ctl_init_page_index(lun);
6307 mtx_lock(&lun->lun_lock);
6308 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
6309 mtx_unlock(&lun->lun_lock);
6310 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6311 ctl_isc_announce_mode(lun, -1,
6312 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
6313 lun->mode_pages.index[i].subpage);
6314 }
6315 ctl_set_success(ctsio);
6316 ctl_done((union ctl_io *)ctsio);
6317 return (CTL_RETVAL_COMPLETE);
6318 }
6319
6320 /*
6321 * From SPC-3:
6322 * "A parameter list length of zero indicates that the Data-Out Buffer
6323 * shall be empty. This condition shall not be considered as an error."
6324 */
6325 if (param_len == 0) {
6326 ctl_set_success(ctsio);
6327 ctl_done((union ctl_io *)ctsio);
6328 return (CTL_RETVAL_COMPLETE);
6329 }
6330
6331 /*
6332 * Since we'll hit this the first time through, prior to
6333 * allocation, we don't need to free a data buffer here.
6334 */
6335 if (param_len < header_size) {
6336 ctl_set_param_len_error(ctsio);
6337 ctl_done((union ctl_io *)ctsio);
6338 return (CTL_RETVAL_COMPLETE);
6339 }
6340
6341 /*
6342 * Allocate the data buffer and grab the user's data. In theory,
6343 * we shouldn't have to sanity check the parameter list length here
6344 * because the maximum size is 64K. We should be able to malloc
6345 * that much without too many problems.
6346 */
6347 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6348 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6349 ctsio->kern_data_len = param_len;
6350 ctsio->kern_total_len = param_len;
6351 ctsio->kern_rel_offset = 0;
6352 ctsio->kern_sg_entries = 0;
6353 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6354 ctsio->be_move_done = ctl_config_move_done;
6355 ctl_datamove((union ctl_io *)ctsio);
6356
6357 return (CTL_RETVAL_COMPLETE);
6358 }
6359
6360 switch (ctsio->cdb[0]) {
6361 case MODE_SELECT_6: {
6362 struct scsi_mode_header_6 *mh6;
6363
6364 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6365 bd_len = mh6->blk_desc_len;
6366 break;
6367 }
6368 case MODE_SELECT_10: {
6369 struct scsi_mode_header_10 *mh10;
6370
6371 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6372 bd_len = scsi_2btoul(mh10->blk_desc_len);
6373 break;
6374 }
6375 default:
6376 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6377 }
6378
6379 if (param_len < (header_size + bd_len)) {
6380 free(ctsio->kern_data_ptr, M_CTL);
6381 ctl_set_param_len_error(ctsio);
6382 ctl_done((union ctl_io *)ctsio);
6383 return (CTL_RETVAL_COMPLETE);
6384 }
6385
6386 /*
6387 * Set the IO_CONT flag, so that if this I/O gets passed to
6388 * ctl_config_write_done(), it'll get passed back to
6389 * ctl_do_mode_select() for further processing, or completion if
6390 * we're all done.
6391 */
6392 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6393 ctsio->io_cont = ctl_do_mode_select;
6394
6395 modepage_info = (union ctl_modepage_info *)
6396 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6397 memset(modepage_info, 0, sizeof(*modepage_info));
6398 modepage_info->header.len_left = param_len - header_size - bd_len;
6399 modepage_info->header.len_used = header_size + bd_len;
6400
6401 return (ctl_do_mode_select((union ctl_io *)ctsio));
6402 }
6403
6404 int
ctl_mode_sense(struct ctl_scsiio * ctsio)6405 ctl_mode_sense(struct ctl_scsiio *ctsio)
6406 {
6407 struct ctl_lun *lun = CTL_LUN(ctsio);
6408 int pc, page_code, llba, subpage;
6409 int alloc_len, page_len, header_len, bd_len, total_len;
6410 void *block_desc;
6411 struct ctl_page_index *page_index;
6412
6413 llba = 0;
6414
6415 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6416
6417 switch (ctsio->cdb[0]) {
6418 case MODE_SENSE_6: {
6419 struct scsi_mode_sense_6 *cdb;
6420
6421 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6422
6423 header_len = sizeof(struct scsi_mode_hdr_6);
6424 if (cdb->byte2 & SMS_DBD)
6425 bd_len = 0;
6426 else
6427 bd_len = sizeof(struct scsi_mode_block_descr);
6428 header_len += bd_len;
6429
6430 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6431 page_code = cdb->page & SMS_PAGE_CODE;
6432 subpage = cdb->subpage;
6433 alloc_len = cdb->length;
6434 break;
6435 }
6436 case MODE_SENSE_10: {
6437 struct scsi_mode_sense_10 *cdb;
6438
6439 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6440
6441 header_len = sizeof(struct scsi_mode_hdr_10);
6442 if (cdb->byte2 & SMS_DBD) {
6443 bd_len = 0;
6444 } else if (lun->be_lun->lun_type == T_DIRECT) {
6445 if (cdb->byte2 & SMS10_LLBAA) {
6446 llba = 1;
6447 bd_len = sizeof(struct scsi_mode_block_descr_dlong);
6448 } else
6449 bd_len = sizeof(struct scsi_mode_block_descr_dshort);
6450 } else
6451 bd_len = sizeof(struct scsi_mode_block_descr);
6452 header_len += bd_len;
6453
6454 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6455 page_code = cdb->page & SMS_PAGE_CODE;
6456 subpage = cdb->subpage;
6457 alloc_len = scsi_2btoul(cdb->length);
6458 break;
6459 }
6460 default:
6461 ctl_set_invalid_opcode(ctsio);
6462 ctl_done((union ctl_io *)ctsio);
6463 return (CTL_RETVAL_COMPLETE);
6464 break; /* NOTREACHED */
6465 }
6466
6467 /*
6468 * We have to make a first pass through to calculate the size of
6469 * the pages that match the user's query. Then we allocate enough
6470 * memory to hold it, and actually copy the data into the buffer.
6471 */
6472 switch (page_code) {
6473 case SMS_ALL_PAGES_PAGE: {
6474 u_int i;
6475
6476 page_len = 0;
6477
6478 /*
6479 * At the moment, values other than 0 and 0xff here are
6480 * reserved according to SPC-3.
6481 */
6482 if ((subpage != SMS_SUBPAGE_PAGE_0)
6483 && (subpage != SMS_SUBPAGE_ALL)) {
6484 ctl_set_invalid_field(ctsio,
6485 /*sks_valid*/ 1,
6486 /*command*/ 1,
6487 /*field*/ 3,
6488 /*bit_valid*/ 0,
6489 /*bit*/ 0);
6490 ctl_done((union ctl_io *)ctsio);
6491 return (CTL_RETVAL_COMPLETE);
6492 }
6493
6494 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6495 page_index = &lun->mode_pages.index[i];
6496
6497 /* Make sure the page is supported for this dev type */
6498 if (lun->be_lun->lun_type == T_DIRECT &&
6499 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6500 continue;
6501 if (lun->be_lun->lun_type == T_PROCESSOR &&
6502 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6503 continue;
6504 if (lun->be_lun->lun_type == T_CDROM &&
6505 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6506 continue;
6507
6508 /*
6509 * We don't use this subpage if the user didn't
6510 * request all subpages.
6511 */
6512 if ((page_index->subpage != 0)
6513 && (subpage == SMS_SUBPAGE_PAGE_0))
6514 continue;
6515
6516 page_len += page_index->page_len;
6517 }
6518 break;
6519 }
6520 default: {
6521 u_int i;
6522
6523 page_len = 0;
6524
6525 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6526 page_index = &lun->mode_pages.index[i];
6527
6528 /* Make sure the page is supported for this dev type */
6529 if (lun->be_lun->lun_type == T_DIRECT &&
6530 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6531 continue;
6532 if (lun->be_lun->lun_type == T_PROCESSOR &&
6533 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6534 continue;
6535 if (lun->be_lun->lun_type == T_CDROM &&
6536 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6537 continue;
6538
6539 /* Look for the right page code */
6540 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6541 continue;
6542
6543 /* Look for the right subpage or the subpage wildcard*/
6544 if ((page_index->subpage != subpage)
6545 && (subpage != SMS_SUBPAGE_ALL))
6546 continue;
6547
6548 page_len += page_index->page_len;
6549 }
6550
6551 if (page_len == 0) {
6552 ctl_set_invalid_field(ctsio,
6553 /*sks_valid*/ 1,
6554 /*command*/ 1,
6555 /*field*/ 2,
6556 /*bit_valid*/ 1,
6557 /*bit*/ 5);
6558 ctl_done((union ctl_io *)ctsio);
6559 return (CTL_RETVAL_COMPLETE);
6560 }
6561 break;
6562 }
6563 }
6564
6565 total_len = header_len + page_len;
6566
6567 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6568 ctsio->kern_sg_entries = 0;
6569 ctsio->kern_rel_offset = 0;
6570 ctsio->kern_data_len = min(total_len, alloc_len);
6571 ctsio->kern_total_len = ctsio->kern_data_len;
6572
6573 switch (ctsio->cdb[0]) {
6574 case MODE_SENSE_6: {
6575 struct scsi_mode_hdr_6 *header;
6576
6577 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6578
6579 header->datalen = MIN(total_len - 1, 254);
6580 if (lun->be_lun->lun_type == T_DIRECT) {
6581 header->dev_specific = 0x10; /* DPOFUA */
6582 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6583 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6584 header->dev_specific |= 0x80; /* WP */
6585 }
6586 header->block_descr_len = bd_len;
6587 block_desc = &header[1];
6588 break;
6589 }
6590 case MODE_SENSE_10: {
6591 struct scsi_mode_hdr_10 *header;
6592 int datalen;
6593
6594 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6595
6596 datalen = MIN(total_len - 2, 65533);
6597 scsi_ulto2b(datalen, header->datalen);
6598 if (lun->be_lun->lun_type == T_DIRECT) {
6599 header->dev_specific = 0x10; /* DPOFUA */
6600 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6601 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6602 header->dev_specific |= 0x80; /* WP */
6603 }
6604 if (llba)
6605 header->flags |= SMH_LONGLBA;
6606 scsi_ulto2b(bd_len, header->block_descr_len);
6607 block_desc = &header[1];
6608 break;
6609 }
6610 default:
6611 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6612 }
6613
6614 /*
6615 * If we've got a disk, use its blocksize in the block
6616 * descriptor. Otherwise, just set it to 0.
6617 */
6618 if (bd_len > 0) {
6619 if (lun->be_lun->lun_type == T_DIRECT) {
6620 if (llba) {
6621 struct scsi_mode_block_descr_dlong *bd = block_desc;
6622 if (lun->be_lun->maxlba != 0)
6623 scsi_u64to8b(lun->be_lun->maxlba + 1,
6624 bd->num_blocks);
6625 scsi_ulto4b(lun->be_lun->blocksize,
6626 bd->block_len);
6627 } else {
6628 struct scsi_mode_block_descr_dshort *bd = block_desc;
6629 if (lun->be_lun->maxlba != 0)
6630 scsi_ulto4b(MIN(lun->be_lun->maxlba+1,
6631 UINT32_MAX), bd->num_blocks);
6632 scsi_ulto3b(lun->be_lun->blocksize,
6633 bd->block_len);
6634 }
6635 } else {
6636 struct scsi_mode_block_descr *bd = block_desc;
6637 scsi_ulto3b(0, bd->block_len);
6638 }
6639 }
6640
6641 switch (page_code) {
6642 case SMS_ALL_PAGES_PAGE: {
6643 int i, data_used;
6644
6645 data_used = header_len;
6646 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6647 struct ctl_page_index *page_index;
6648
6649 page_index = &lun->mode_pages.index[i];
6650 if (lun->be_lun->lun_type == T_DIRECT &&
6651 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6652 continue;
6653 if (lun->be_lun->lun_type == T_PROCESSOR &&
6654 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6655 continue;
6656 if (lun->be_lun->lun_type == T_CDROM &&
6657 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6658 continue;
6659
6660 /*
6661 * We don't use this subpage if the user didn't
6662 * request all subpages. We already checked (above)
6663 * to make sure the user only specified a subpage
6664 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6665 */
6666 if ((page_index->subpage != 0)
6667 && (subpage == SMS_SUBPAGE_PAGE_0))
6668 continue;
6669
6670 /*
6671 * Call the handler, if it exists, to update the
6672 * page to the latest values.
6673 */
6674 if (page_index->sense_handler != NULL)
6675 page_index->sense_handler(ctsio, page_index,pc);
6676
6677 memcpy(ctsio->kern_data_ptr + data_used,
6678 page_index->page_data +
6679 (page_index->page_len * pc),
6680 page_index->page_len);
6681 data_used += page_index->page_len;
6682 }
6683 break;
6684 }
6685 default: {
6686 int i, data_used;
6687
6688 data_used = header_len;
6689
6690 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6691 struct ctl_page_index *page_index;
6692
6693 page_index = &lun->mode_pages.index[i];
6694
6695 /* Look for the right page code */
6696 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6697 continue;
6698
6699 /* Look for the right subpage or the subpage wildcard*/
6700 if ((page_index->subpage != subpage)
6701 && (subpage != SMS_SUBPAGE_ALL))
6702 continue;
6703
6704 /* Make sure the page is supported for this dev type */
6705 if (lun->be_lun->lun_type == T_DIRECT &&
6706 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6707 continue;
6708 if (lun->be_lun->lun_type == T_PROCESSOR &&
6709 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6710 continue;
6711 if (lun->be_lun->lun_type == T_CDROM &&
6712 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6713 continue;
6714
6715 /*
6716 * Call the handler, if it exists, to update the
6717 * page to the latest values.
6718 */
6719 if (page_index->sense_handler != NULL)
6720 page_index->sense_handler(ctsio, page_index,pc);
6721
6722 memcpy(ctsio->kern_data_ptr + data_used,
6723 page_index->page_data +
6724 (page_index->page_len * pc),
6725 page_index->page_len);
6726 data_used += page_index->page_len;
6727 }
6728 break;
6729 }
6730 }
6731
6732 ctl_set_success(ctsio);
6733 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6734 ctsio->be_move_done = ctl_config_move_done;
6735 ctl_datamove((union ctl_io *)ctsio);
6736 return (CTL_RETVAL_COMPLETE);
6737 }
6738
6739 int
ctl_temp_log_sense_handler(struct ctl_scsiio * ctsio,struct ctl_page_index * page_index,int pc)6740 ctl_temp_log_sense_handler(struct ctl_scsiio *ctsio,
6741 struct ctl_page_index *page_index,
6742 int pc)
6743 {
6744 struct ctl_lun *lun = CTL_LUN(ctsio);
6745 struct scsi_log_temperature *data;
6746 const char *value;
6747
6748 data = (struct scsi_log_temperature *)page_index->page_data;
6749
6750 scsi_ulto2b(SLP_TEMPERATURE, data->hdr.param_code);
6751 data->hdr.param_control = SLP_LBIN;
6752 data->hdr.param_len = sizeof(struct scsi_log_temperature) -
6753 sizeof(struct scsi_log_param_header);
6754 if ((value = dnvlist_get_string(lun->be_lun->options, "temperature",
6755 NULL)) != NULL)
6756 data->temperature = strtol(value, NULL, 0);
6757 else
6758 data->temperature = 0xff;
6759 data++;
6760
6761 scsi_ulto2b(SLP_REFTEMPERATURE, data->hdr.param_code);
6762 data->hdr.param_control = SLP_LBIN;
6763 data->hdr.param_len = sizeof(struct scsi_log_temperature) -
6764 sizeof(struct scsi_log_param_header);
6765 if ((value = dnvlist_get_string(lun->be_lun->options, "reftemperature",
6766 NULL)) != NULL)
6767 data->temperature = strtol(value, NULL, 0);
6768 else
6769 data->temperature = 0xff;
6770 return (0);
6771 }
6772
6773 int
ctl_lbp_log_sense_handler(struct ctl_scsiio * ctsio,struct ctl_page_index * page_index,int pc)6774 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6775 struct ctl_page_index *page_index,
6776 int pc)
6777 {
6778 struct ctl_lun *lun = CTL_LUN(ctsio);
6779 struct scsi_log_param_header *phdr;
6780 uint8_t *data;
6781 uint64_t val;
6782
6783 data = page_index->page_data;
6784
6785 if (lun->backend->lun_attr != NULL &&
6786 (val = lun->backend->lun_attr(lun->be_lun, "blocksavail"))
6787 != UINT64_MAX) {
6788 phdr = (struct scsi_log_param_header *)data;
6789 scsi_ulto2b(0x0001, phdr->param_code);
6790 phdr->param_control = SLP_LBIN | SLP_LP;
6791 phdr->param_len = 8;
6792 data = (uint8_t *)(phdr + 1);
6793 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6794 data[4] = 0x02; /* per-pool */
6795 data += phdr->param_len;
6796 }
6797
6798 if (lun->backend->lun_attr != NULL &&
6799 (val = lun->backend->lun_attr(lun->be_lun, "blocksused"))
6800 != UINT64_MAX) {
6801 phdr = (struct scsi_log_param_header *)data;
6802 scsi_ulto2b(0x0002, phdr->param_code);
6803 phdr->param_control = SLP_LBIN | SLP_LP;
6804 phdr->param_len = 8;
6805 data = (uint8_t *)(phdr + 1);
6806 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6807 data[4] = 0x01; /* per-LUN */
6808 data += phdr->param_len;
6809 }
6810
6811 if (lun->backend->lun_attr != NULL &&
6812 (val = lun->backend->lun_attr(lun->be_lun, "poolblocksavail"))
6813 != UINT64_MAX) {
6814 phdr = (struct scsi_log_param_header *)data;
6815 scsi_ulto2b(0x00f1, phdr->param_code);
6816 phdr->param_control = SLP_LBIN | SLP_LP;
6817 phdr->param_len = 8;
6818 data = (uint8_t *)(phdr + 1);
6819 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6820 data[4] = 0x02; /* per-pool */
6821 data += phdr->param_len;
6822 }
6823
6824 if (lun->backend->lun_attr != NULL &&
6825 (val = lun->backend->lun_attr(lun->be_lun, "poolblocksused"))
6826 != UINT64_MAX) {
6827 phdr = (struct scsi_log_param_header *)data;
6828 scsi_ulto2b(0x00f2, phdr->param_code);
6829 phdr->param_control = SLP_LBIN | SLP_LP;
6830 phdr->param_len = 8;
6831 data = (uint8_t *)(phdr + 1);
6832 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6833 data[4] = 0x02; /* per-pool */
6834 data += phdr->param_len;
6835 }
6836
6837 page_index->page_len = data - page_index->page_data;
6838 return (0);
6839 }
6840
6841 int
ctl_sap_log_sense_handler(struct ctl_scsiio * ctsio,struct ctl_page_index * page_index,int pc)6842 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6843 struct ctl_page_index *page_index,
6844 int pc)
6845 {
6846 struct ctl_lun *lun = CTL_LUN(ctsio);
6847 struct stat_page *data;
6848 struct bintime *t;
6849
6850 data = (struct stat_page *)page_index->page_data;
6851
6852 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6853 data->sap.hdr.param_control = SLP_LBIN;
6854 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6855 sizeof(struct scsi_log_param_header);
6856 scsi_u64to8b(lun->stats.operations[CTL_STATS_READ],
6857 data->sap.read_num);
6858 scsi_u64to8b(lun->stats.operations[CTL_STATS_WRITE],
6859 data->sap.write_num);
6860 if (lun->be_lun->blocksize > 0) {
6861 scsi_u64to8b(lun->stats.bytes[CTL_STATS_WRITE] /
6862 lun->be_lun->blocksize, data->sap.recvieved_lba);
6863 scsi_u64to8b(lun->stats.bytes[CTL_STATS_READ] /
6864 lun->be_lun->blocksize, data->sap.transmitted_lba);
6865 }
6866 t = &lun->stats.time[CTL_STATS_READ];
6867 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6868 data->sap.read_int);
6869 t = &lun->stats.time[CTL_STATS_WRITE];
6870 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6871 data->sap.write_int);
6872 scsi_u64to8b(0, data->sap.weighted_num);
6873 scsi_u64to8b(0, data->sap.weighted_int);
6874 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6875 data->it.hdr.param_control = SLP_LBIN;
6876 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6877 sizeof(struct scsi_log_param_header);
6878 #ifdef CTL_TIME_IO
6879 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6880 #endif
6881 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6882 data->it.hdr.param_control = SLP_LBIN;
6883 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6884 sizeof(struct scsi_log_param_header);
6885 scsi_ulto4b(3, data->ti.exponent);
6886 scsi_ulto4b(1, data->ti.integer);
6887 return (0);
6888 }
6889
6890 int
ctl_ie_log_sense_handler(struct ctl_scsiio * ctsio,struct ctl_page_index * page_index,int pc)6891 ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio,
6892 struct ctl_page_index *page_index,
6893 int pc)
6894 {
6895 struct ctl_lun *lun = CTL_LUN(ctsio);
6896 struct scsi_log_informational_exceptions *data;
6897 const char *value;
6898
6899 data = (struct scsi_log_informational_exceptions *)page_index->page_data;
6900
6901 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code);
6902 data->hdr.param_control = SLP_LBIN;
6903 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) -
6904 sizeof(struct scsi_log_param_header);
6905 data->ie_asc = lun->ie_asc;
6906 data->ie_ascq = lun->ie_ascq;
6907 if ((value = dnvlist_get_string(lun->be_lun->options, "temperature",
6908 NULL)) != NULL)
6909 data->temperature = strtol(value, NULL, 0);
6910 else
6911 data->temperature = 0xff;
6912 return (0);
6913 }
6914
6915 int
ctl_log_sense(struct ctl_scsiio * ctsio)6916 ctl_log_sense(struct ctl_scsiio *ctsio)
6917 {
6918 struct ctl_lun *lun = CTL_LUN(ctsio);
6919 int i, pc, page_code, subpage;
6920 int alloc_len, total_len;
6921 struct ctl_page_index *page_index;
6922 struct scsi_log_sense *cdb;
6923 struct scsi_log_header *header;
6924
6925 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6926
6927 cdb = (struct scsi_log_sense *)ctsio->cdb;
6928 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6929 page_code = cdb->page & SLS_PAGE_CODE;
6930 subpage = cdb->subpage;
6931 alloc_len = scsi_2btoul(cdb->length);
6932
6933 page_index = NULL;
6934 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6935 page_index = &lun->log_pages.index[i];
6936
6937 /* Look for the right page code */
6938 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6939 continue;
6940
6941 /* Look for the right subpage or the subpage wildcard*/
6942 if (page_index->subpage != subpage)
6943 continue;
6944
6945 break;
6946 }
6947 if (i >= CTL_NUM_LOG_PAGES) {
6948 ctl_set_invalid_field(ctsio,
6949 /*sks_valid*/ 1,
6950 /*command*/ 1,
6951 /*field*/ 2,
6952 /*bit_valid*/ 0,
6953 /*bit*/ 0);
6954 ctl_done((union ctl_io *)ctsio);
6955 return (CTL_RETVAL_COMPLETE);
6956 }
6957
6958 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6959
6960 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6961 ctsio->kern_sg_entries = 0;
6962 ctsio->kern_rel_offset = 0;
6963 ctsio->kern_data_len = min(total_len, alloc_len);
6964 ctsio->kern_total_len = ctsio->kern_data_len;
6965
6966 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6967 header->page = page_index->page_code;
6968 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING)
6969 header->page |= SL_DS;
6970 if (page_index->subpage) {
6971 header->page |= SL_SPF;
6972 header->subpage = page_index->subpage;
6973 }
6974 scsi_ulto2b(page_index->page_len, header->datalen);
6975
6976 /*
6977 * Call the handler, if it exists, to update the
6978 * page to the latest values.
6979 */
6980 if (page_index->sense_handler != NULL)
6981 page_index->sense_handler(ctsio, page_index, pc);
6982
6983 memcpy(header + 1, page_index->page_data, page_index->page_len);
6984
6985 ctl_set_success(ctsio);
6986 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6987 ctsio->be_move_done = ctl_config_move_done;
6988 ctl_datamove((union ctl_io *)ctsio);
6989 return (CTL_RETVAL_COMPLETE);
6990 }
6991
6992 int
ctl_read_capacity(struct ctl_scsiio * ctsio)6993 ctl_read_capacity(struct ctl_scsiio *ctsio)
6994 {
6995 struct ctl_lun *lun = CTL_LUN(ctsio);
6996 struct scsi_read_capacity *cdb;
6997 struct scsi_read_capacity_data *data;
6998 uint32_t lba;
6999
7000 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
7001
7002 cdb = (struct scsi_read_capacity *)ctsio->cdb;
7003
7004 lba = scsi_4btoul(cdb->addr);
7005 if (((cdb->pmi & SRC_PMI) == 0)
7006 && (lba != 0)) {
7007 ctl_set_invalid_field(/*ctsio*/ ctsio,
7008 /*sks_valid*/ 1,
7009 /*command*/ 1,
7010 /*field*/ 2,
7011 /*bit_valid*/ 0,
7012 /*bit*/ 0);
7013 ctl_done((union ctl_io *)ctsio);
7014 return (CTL_RETVAL_COMPLETE);
7015 }
7016
7017 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7018 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
7019 ctsio->kern_data_len = sizeof(*data);
7020 ctsio->kern_total_len = sizeof(*data);
7021 ctsio->kern_rel_offset = 0;
7022 ctsio->kern_sg_entries = 0;
7023
7024 /*
7025 * If the maximum LBA is greater than 0xfffffffe, the user must
7026 * issue a SERVICE ACTION IN (16) command, with the read capacity
7027 * serivce action set.
7028 */
7029 if (lun->be_lun->maxlba > 0xfffffffe)
7030 scsi_ulto4b(0xffffffff, data->addr);
7031 else
7032 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
7033
7034 /*
7035 * XXX KDM this may not be 512 bytes...
7036 */
7037 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7038
7039 ctl_set_success(ctsio);
7040 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7041 ctsio->be_move_done = ctl_config_move_done;
7042 ctl_datamove((union ctl_io *)ctsio);
7043 return (CTL_RETVAL_COMPLETE);
7044 }
7045
7046 int
ctl_read_capacity_16(struct ctl_scsiio * ctsio)7047 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
7048 {
7049 struct ctl_lun *lun = CTL_LUN(ctsio);
7050 struct scsi_read_capacity_16 *cdb;
7051 struct scsi_read_capacity_data_long *data;
7052 uint64_t lba;
7053 uint32_t alloc_len;
7054
7055 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7056
7057 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7058
7059 alloc_len = scsi_4btoul(cdb->alloc_len);
7060 lba = scsi_8btou64(cdb->addr);
7061
7062 if ((cdb->reladr & SRC16_PMI)
7063 && (lba != 0)) {
7064 ctl_set_invalid_field(/*ctsio*/ ctsio,
7065 /*sks_valid*/ 1,
7066 /*command*/ 1,
7067 /*field*/ 2,
7068 /*bit_valid*/ 0,
7069 /*bit*/ 0);
7070 ctl_done((union ctl_io *)ctsio);
7071 return (CTL_RETVAL_COMPLETE);
7072 }
7073
7074 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7075 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7076 ctsio->kern_rel_offset = 0;
7077 ctsio->kern_sg_entries = 0;
7078 ctsio->kern_data_len = min(sizeof(*data), alloc_len);
7079 ctsio->kern_total_len = ctsio->kern_data_len;
7080
7081 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7082 /* XXX KDM this may not be 512 bytes... */
7083 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7084 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7085 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7086 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7087 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
7088
7089 ctl_set_success(ctsio);
7090 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7091 ctsio->be_move_done = ctl_config_move_done;
7092 ctl_datamove((union ctl_io *)ctsio);
7093 return (CTL_RETVAL_COMPLETE);
7094 }
7095
7096 int
ctl_get_lba_status(struct ctl_scsiio * ctsio)7097 ctl_get_lba_status(struct ctl_scsiio *ctsio)
7098 {
7099 struct ctl_lun *lun = CTL_LUN(ctsio);
7100 struct scsi_get_lba_status *cdb;
7101 struct scsi_get_lba_status_data *data;
7102 struct ctl_lba_len_flags *lbalen;
7103 uint64_t lba;
7104 uint32_t alloc_len, total_len;
7105 int retval;
7106
7107 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
7108
7109 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
7110 lba = scsi_8btou64(cdb->addr);
7111 alloc_len = scsi_4btoul(cdb->alloc_len);
7112
7113 if (lba > lun->be_lun->maxlba) {
7114 ctl_set_lba_out_of_range(ctsio, lba);
7115 ctl_done((union ctl_io *)ctsio);
7116 return (CTL_RETVAL_COMPLETE);
7117 }
7118
7119 total_len = sizeof(*data) + sizeof(data->descr[0]);
7120 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7121 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
7122 ctsio->kern_rel_offset = 0;
7123 ctsio->kern_sg_entries = 0;
7124 ctsio->kern_data_len = min(total_len, alloc_len);
7125 ctsio->kern_total_len = ctsio->kern_data_len;
7126
7127 /* Fill dummy data in case backend can't tell anything. */
7128 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
7129 scsi_u64to8b(lba, data->descr[0].addr);
7130 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
7131 data->descr[0].length);
7132 data->descr[0].status = 0; /* Mapped or unknown. */
7133
7134 ctl_set_success(ctsio);
7135 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7136 ctsio->be_move_done = ctl_config_move_done;
7137
7138 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7139 lbalen->lba = lba;
7140 lbalen->len = total_len;
7141 lbalen->flags = 0;
7142 retval = lun->backend->config_read((union ctl_io *)ctsio);
7143 return (retval);
7144 }
7145
7146 int
ctl_read_defect(struct ctl_scsiio * ctsio)7147 ctl_read_defect(struct ctl_scsiio *ctsio)
7148 {
7149 struct scsi_read_defect_data_10 *ccb10;
7150 struct scsi_read_defect_data_12 *ccb12;
7151 struct scsi_read_defect_data_hdr_10 *data10;
7152 struct scsi_read_defect_data_hdr_12 *data12;
7153 uint32_t alloc_len, data_len;
7154 uint8_t format;
7155
7156 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7157
7158 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7159 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7160 format = ccb10->format;
7161 alloc_len = scsi_2btoul(ccb10->alloc_length);
7162 data_len = sizeof(*data10);
7163 } else {
7164 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7165 format = ccb12->format;
7166 alloc_len = scsi_4btoul(ccb12->alloc_length);
7167 data_len = sizeof(*data12);
7168 }
7169 if (alloc_len == 0) {
7170 ctl_set_success(ctsio);
7171 ctl_done((union ctl_io *)ctsio);
7172 return (CTL_RETVAL_COMPLETE);
7173 }
7174
7175 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7176 ctsio->kern_rel_offset = 0;
7177 ctsio->kern_sg_entries = 0;
7178 ctsio->kern_data_len = min(data_len, alloc_len);
7179 ctsio->kern_total_len = ctsio->kern_data_len;
7180
7181 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7182 data10 = (struct scsi_read_defect_data_hdr_10 *)
7183 ctsio->kern_data_ptr;
7184 data10->format = format;
7185 scsi_ulto2b(0, data10->length);
7186 } else {
7187 data12 = (struct scsi_read_defect_data_hdr_12 *)
7188 ctsio->kern_data_ptr;
7189 data12->format = format;
7190 scsi_ulto2b(0, data12->generation);
7191 scsi_ulto4b(0, data12->length);
7192 }
7193
7194 ctl_set_success(ctsio);
7195 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7196 ctsio->be_move_done = ctl_config_move_done;
7197 ctl_datamove((union ctl_io *)ctsio);
7198 return (CTL_RETVAL_COMPLETE);
7199 }
7200
7201 int
ctl_report_ident_info(struct ctl_scsiio * ctsio)7202 ctl_report_ident_info(struct ctl_scsiio *ctsio)
7203 {
7204 struct ctl_lun *lun = CTL_LUN(ctsio);
7205 struct scsi_report_ident_info *cdb;
7206 struct scsi_report_ident_info_data *rii_ptr;
7207 struct scsi_report_ident_info_descr *riid_ptr;
7208 const char *oii, *otii;
7209 int retval, alloc_len, total_len = 0, len = 0;
7210
7211 CTL_DEBUG_PRINT(("ctl_report_ident_info\n"));
7212
7213 cdb = (struct scsi_report_ident_info *)ctsio->cdb;
7214 retval = CTL_RETVAL_COMPLETE;
7215
7216 total_len = sizeof(struct scsi_report_ident_info_data);
7217 switch (cdb->type) {
7218 case RII_LUII:
7219 oii = dnvlist_get_string(lun->be_lun->options,
7220 "ident_info", NULL);
7221 if (oii)
7222 len = strlen(oii); /* Approximately */
7223 break;
7224 case RII_LUTII:
7225 otii = dnvlist_get_string(lun->be_lun->options,
7226 "text_ident_info", NULL);
7227 if (otii)
7228 len = strlen(otii) + 1; /* NULL-terminated */
7229 break;
7230 case RII_IIS:
7231 len = 2 * sizeof(struct scsi_report_ident_info_descr);
7232 break;
7233 default:
7234 ctl_set_invalid_field(/*ctsio*/ ctsio,
7235 /*sks_valid*/ 1,
7236 /*command*/ 1,
7237 /*field*/ 11,
7238 /*bit_valid*/ 1,
7239 /*bit*/ 2);
7240 ctl_done((union ctl_io *)ctsio);
7241 return(retval);
7242 }
7243 total_len += len;
7244 alloc_len = scsi_4btoul(cdb->length);
7245
7246 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7247 ctsio->kern_sg_entries = 0;
7248 ctsio->kern_rel_offset = 0;
7249 ctsio->kern_data_len = min(total_len, alloc_len);
7250 ctsio->kern_total_len = ctsio->kern_data_len;
7251
7252 rii_ptr = (struct scsi_report_ident_info_data *)ctsio->kern_data_ptr;
7253 switch (cdb->type) {
7254 case RII_LUII:
7255 if (oii) {
7256 if (oii[0] == '0' && oii[1] == 'x')
7257 len = hex2bin(oii, (uint8_t *)(rii_ptr + 1), len);
7258 else
7259 strncpy((uint8_t *)(rii_ptr + 1), oii, len);
7260 }
7261 break;
7262 case RII_LUTII:
7263 if (otii)
7264 strlcpy((uint8_t *)(rii_ptr + 1), otii, len);
7265 break;
7266 case RII_IIS:
7267 riid_ptr = (struct scsi_report_ident_info_descr *)(rii_ptr + 1);
7268 riid_ptr->type = RII_LUII;
7269 scsi_ulto2b(0xffff, riid_ptr->length);
7270 riid_ptr++;
7271 riid_ptr->type = RII_LUTII;
7272 scsi_ulto2b(0xffff, riid_ptr->length);
7273 }
7274 scsi_ulto2b(len, rii_ptr->length);
7275
7276 ctl_set_success(ctsio);
7277 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7278 ctsio->be_move_done = ctl_config_move_done;
7279 ctl_datamove((union ctl_io *)ctsio);
7280 return(retval);
7281 }
7282
7283 int
ctl_report_tagret_port_groups(struct ctl_scsiio * ctsio)7284 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7285 {
7286 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7287 struct ctl_lun *lun = CTL_LUN(ctsio);
7288 struct scsi_maintenance_in *cdb;
7289 int retval;
7290 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os;
7291 int num_ha_groups, num_target_ports, shared_group;
7292 struct ctl_port *port;
7293 struct scsi_target_group_data *rtg_ptr;
7294 struct scsi_target_group_data_extended *rtg_ext_ptr;
7295 struct scsi_target_port_group_descriptor *tpg_desc;
7296
7297 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7298
7299 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7300 retval = CTL_RETVAL_COMPLETE;
7301
7302 switch (cdb->byte2 & STG_PDF_MASK) {
7303 case STG_PDF_LENGTH:
7304 ext = 0;
7305 break;
7306 case STG_PDF_EXTENDED:
7307 ext = 1;
7308 break;
7309 default:
7310 ctl_set_invalid_field(/*ctsio*/ ctsio,
7311 /*sks_valid*/ 1,
7312 /*command*/ 1,
7313 /*field*/ 2,
7314 /*bit_valid*/ 1,
7315 /*bit*/ 5);
7316 ctl_done((union ctl_io *)ctsio);
7317 return(retval);
7318 }
7319
7320 num_target_ports = 0;
7321 shared_group = (softc->is_single != 0);
7322 mtx_lock(&softc->ctl_lock);
7323 STAILQ_FOREACH(port, &softc->port_list, links) {
7324 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7325 continue;
7326 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7327 continue;
7328 num_target_ports++;
7329 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7330 shared_group = 1;
7331 }
7332 mtx_unlock(&softc->ctl_lock);
7333 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES;
7334
7335 if (ext)
7336 total_len = sizeof(struct scsi_target_group_data_extended);
7337 else
7338 total_len = sizeof(struct scsi_target_group_data);
7339 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7340 (shared_group + num_ha_groups) +
7341 sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7342
7343 alloc_len = scsi_4btoul(cdb->length);
7344
7345 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7346 ctsio->kern_sg_entries = 0;
7347 ctsio->kern_rel_offset = 0;
7348 ctsio->kern_data_len = min(total_len, alloc_len);
7349 ctsio->kern_total_len = ctsio->kern_data_len;
7350
7351 if (ext) {
7352 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7353 ctsio->kern_data_ptr;
7354 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7355 rtg_ext_ptr->format_type = 0x10;
7356 rtg_ext_ptr->implicit_transition_time = 0;
7357 tpg_desc = &rtg_ext_ptr->groups[0];
7358 } else {
7359 rtg_ptr = (struct scsi_target_group_data *)
7360 ctsio->kern_data_ptr;
7361 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7362 tpg_desc = &rtg_ptr->groups[0];
7363 }
7364
7365 mtx_lock(&softc->ctl_lock);
7366 pg = softc->port_min / softc->port_cnt;
7367 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) {
7368 /* Some shelf is known to be primary. */
7369 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7370 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7371 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7372 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7373 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7374 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7375 else
7376 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7377 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7378 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7379 } else {
7380 ts = os;
7381 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7382 }
7383 } else {
7384 /* No known primary shelf. */
7385 if (softc->ha_link == CTL_HA_LINK_OFFLINE) {
7386 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7387 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7388 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) {
7389 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7390 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7391 } else {
7392 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7393 }
7394 }
7395 if (shared_group) {
7396 tpg_desc->pref_state = ts;
7397 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7398 TPG_U_SUP | TPG_T_SUP;
7399 scsi_ulto2b(1, tpg_desc->target_port_group);
7400 tpg_desc->status = TPG_IMPLICIT;
7401 pc = 0;
7402 STAILQ_FOREACH(port, &softc->port_list, links) {
7403 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7404 continue;
7405 if (!softc->is_single &&
7406 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0)
7407 continue;
7408 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7409 continue;
7410 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7411 relative_target_port_identifier);
7412 pc++;
7413 }
7414 tpg_desc->target_port_count = pc;
7415 tpg_desc = (struct scsi_target_port_group_descriptor *)
7416 &tpg_desc->descriptors[pc];
7417 }
7418 for (g = 0; g < num_ha_groups; g++) {
7419 tpg_desc->pref_state = (g == pg) ? ts : os;
7420 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7421 TPG_U_SUP | TPG_T_SUP;
7422 scsi_ulto2b(2 + g, tpg_desc->target_port_group);
7423 tpg_desc->status = TPG_IMPLICIT;
7424 pc = 0;
7425 STAILQ_FOREACH(port, &softc->port_list, links) {
7426 if (port->targ_port < g * softc->port_cnt ||
7427 port->targ_port >= (g + 1) * softc->port_cnt)
7428 continue;
7429 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7430 continue;
7431 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7432 continue;
7433 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7434 continue;
7435 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7436 relative_target_port_identifier);
7437 pc++;
7438 }
7439 tpg_desc->target_port_count = pc;
7440 tpg_desc = (struct scsi_target_port_group_descriptor *)
7441 &tpg_desc->descriptors[pc];
7442 }
7443 mtx_unlock(&softc->ctl_lock);
7444
7445 ctl_set_success(ctsio);
7446 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7447 ctsio->be_move_done = ctl_config_move_done;
7448 ctl_datamove((union ctl_io *)ctsio);
7449 return(retval);
7450 }
7451
7452 int
ctl_report_supported_opcodes(struct ctl_scsiio * ctsio)7453 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7454 {
7455 struct ctl_lun *lun = CTL_LUN(ctsio);
7456 struct scsi_report_supported_opcodes *cdb;
7457 const struct ctl_cmd_entry *entry, *sentry;
7458 struct scsi_report_supported_opcodes_all *all;
7459 struct scsi_report_supported_opcodes_descr *descr;
7460 struct scsi_report_supported_opcodes_one *one;
7461 int retval;
7462 int alloc_len, total_len;
7463 int opcode, service_action, i, j, num;
7464
7465 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7466
7467 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7468 retval = CTL_RETVAL_COMPLETE;
7469
7470 opcode = cdb->requested_opcode;
7471 service_action = scsi_2btoul(cdb->requested_service_action);
7472 switch (cdb->options & RSO_OPTIONS_MASK) {
7473 case RSO_OPTIONS_ALL:
7474 num = 0;
7475 for (i = 0; i < 256; i++) {
7476 entry = &ctl_cmd_table[i];
7477 if (entry->flags & CTL_CMD_FLAG_SA5) {
7478 for (j = 0; j < 32; j++) {
7479 sentry = &((const struct ctl_cmd_entry *)
7480 entry->execute)[j];
7481 if (ctl_cmd_applicable(
7482 lun->be_lun->lun_type, sentry))
7483 num++;
7484 }
7485 } else {
7486 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7487 entry))
7488 num++;
7489 }
7490 }
7491 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7492 num * sizeof(struct scsi_report_supported_opcodes_descr);
7493 break;
7494 case RSO_OPTIONS_OC:
7495 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7496 goto invalid_options;
7497 }
7498 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7499 break;
7500 case RSO_OPTIONS_OC_SA:
7501 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0) {
7502 goto invalid_options;
7503 }
7504 /* FALLTHROUGH */
7505 case RSO_OPTIONS_OC_ASA:
7506 if (service_action >= 32) {
7507 ctl_set_invalid_field(/*ctsio*/ ctsio,
7508 /*sks_valid*/ 1,
7509 /*command*/ 1,
7510 /*field*/ 4,
7511 /*bit_valid*/ 0,
7512 /*bit*/ 0);
7513 ctl_done((union ctl_io *)ctsio);
7514 return (CTL_RETVAL_COMPLETE);
7515 }
7516 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7517 break;
7518 default:
7519 invalid_options:
7520 ctl_set_invalid_field(/*ctsio*/ ctsio,
7521 /*sks_valid*/ 1,
7522 /*command*/ 1,
7523 /*field*/ 2,
7524 /*bit_valid*/ 1,
7525 /*bit*/ 2);
7526 ctl_done((union ctl_io *)ctsio);
7527 return (CTL_RETVAL_COMPLETE);
7528 }
7529
7530 alloc_len = scsi_4btoul(cdb->length);
7531
7532 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7533 ctsio->kern_sg_entries = 0;
7534 ctsio->kern_rel_offset = 0;
7535 ctsio->kern_data_len = min(total_len, alloc_len);
7536 ctsio->kern_total_len = ctsio->kern_data_len;
7537
7538 switch (cdb->options & RSO_OPTIONS_MASK) {
7539 case RSO_OPTIONS_ALL:
7540 all = (struct scsi_report_supported_opcodes_all *)
7541 ctsio->kern_data_ptr;
7542 num = 0;
7543 for (i = 0; i < 256; i++) {
7544 entry = &ctl_cmd_table[i];
7545 if (entry->flags & CTL_CMD_FLAG_SA5) {
7546 for (j = 0; j < 32; j++) {
7547 sentry = &((const struct ctl_cmd_entry *)
7548 entry->execute)[j];
7549 if (!ctl_cmd_applicable(
7550 lun->be_lun->lun_type, sentry))
7551 continue;
7552 descr = &all->descr[num++];
7553 descr->opcode = i;
7554 scsi_ulto2b(j, descr->service_action);
7555 descr->flags = RSO_SERVACTV;
7556 scsi_ulto2b(sentry->length,
7557 descr->cdb_length);
7558 }
7559 } else {
7560 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7561 entry))
7562 continue;
7563 descr = &all->descr[num++];
7564 descr->opcode = i;
7565 scsi_ulto2b(0, descr->service_action);
7566 descr->flags = 0;
7567 scsi_ulto2b(entry->length, descr->cdb_length);
7568 }
7569 }
7570 scsi_ulto4b(
7571 num * sizeof(struct scsi_report_supported_opcodes_descr),
7572 all->length);
7573 break;
7574 case RSO_OPTIONS_OC:
7575 one = (struct scsi_report_supported_opcodes_one *)
7576 ctsio->kern_data_ptr;
7577 entry = &ctl_cmd_table[opcode];
7578 goto fill_one;
7579 case RSO_OPTIONS_OC_SA:
7580 one = (struct scsi_report_supported_opcodes_one *)
7581 ctsio->kern_data_ptr;
7582 entry = &ctl_cmd_table[opcode];
7583 entry = &((const struct ctl_cmd_entry *)
7584 entry->execute)[service_action];
7585 fill_one:
7586 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7587 one->support = 3;
7588 scsi_ulto2b(entry->length, one->cdb_length);
7589 one->cdb_usage[0] = opcode;
7590 memcpy(&one->cdb_usage[1], entry->usage,
7591 entry->length - 1);
7592 } else
7593 one->support = 1;
7594 break;
7595 case RSO_OPTIONS_OC_ASA:
7596 one = (struct scsi_report_supported_opcodes_one *)
7597 ctsio->kern_data_ptr;
7598 entry = &ctl_cmd_table[opcode];
7599 if (entry->flags & CTL_CMD_FLAG_SA5) {
7600 entry = &((const struct ctl_cmd_entry *)
7601 entry->execute)[service_action];
7602 } else if (service_action != 0) {
7603 one->support = 1;
7604 break;
7605 }
7606 goto fill_one;
7607 }
7608
7609 ctl_set_success(ctsio);
7610 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7611 ctsio->be_move_done = ctl_config_move_done;
7612 ctl_datamove((union ctl_io *)ctsio);
7613 return(retval);
7614 }
7615
7616 int
ctl_report_supported_tmf(struct ctl_scsiio * ctsio)7617 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7618 {
7619 struct scsi_report_supported_tmf *cdb;
7620 struct scsi_report_supported_tmf_ext_data *data;
7621 int retval;
7622 int alloc_len, total_len;
7623
7624 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7625
7626 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7627
7628 retval = CTL_RETVAL_COMPLETE;
7629
7630 if (cdb->options & RST_REPD)
7631 total_len = sizeof(struct scsi_report_supported_tmf_ext_data);
7632 else
7633 total_len = sizeof(struct scsi_report_supported_tmf_data);
7634 alloc_len = scsi_4btoul(cdb->length);
7635
7636 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7637 ctsio->kern_sg_entries = 0;
7638 ctsio->kern_rel_offset = 0;
7639 ctsio->kern_data_len = min(total_len, alloc_len);
7640 ctsio->kern_total_len = ctsio->kern_data_len;
7641
7642 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr;
7643 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7644 RST_TRS;
7645 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7646 data->length = total_len - 4;
7647
7648 ctl_set_success(ctsio);
7649 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7650 ctsio->be_move_done = ctl_config_move_done;
7651 ctl_datamove((union ctl_io *)ctsio);
7652 return (retval);
7653 }
7654
7655 int
ctl_report_timestamp(struct ctl_scsiio * ctsio)7656 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7657 {
7658 struct scsi_report_timestamp *cdb;
7659 struct scsi_report_timestamp_data *data;
7660 struct timeval tv;
7661 int64_t timestamp;
7662 int retval;
7663 int alloc_len, total_len;
7664
7665 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7666
7667 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7668
7669 retval = CTL_RETVAL_COMPLETE;
7670
7671 total_len = sizeof(struct scsi_report_timestamp_data);
7672 alloc_len = scsi_4btoul(cdb->length);
7673
7674 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7675 ctsio->kern_sg_entries = 0;
7676 ctsio->kern_rel_offset = 0;
7677 ctsio->kern_data_len = min(total_len, alloc_len);
7678 ctsio->kern_total_len = ctsio->kern_data_len;
7679
7680 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7681 scsi_ulto2b(sizeof(*data) - 2, data->length);
7682 data->origin = RTS_ORIG_OUTSIDE;
7683 getmicrotime(&tv);
7684 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7685 scsi_ulto4b(timestamp >> 16, data->timestamp);
7686 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7687
7688 ctl_set_success(ctsio);
7689 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7690 ctsio->be_move_done = ctl_config_move_done;
7691 ctl_datamove((union ctl_io *)ctsio);
7692 return (retval);
7693 }
7694
7695 int
ctl_persistent_reserve_in(struct ctl_scsiio * ctsio)7696 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7697 {
7698 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7699 struct ctl_lun *lun = CTL_LUN(ctsio);
7700 struct scsi_per_res_in *cdb;
7701 int alloc_len, total_len = 0;
7702 /* struct scsi_per_res_in_rsrv in_data; */
7703 uint64_t key;
7704
7705 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7706
7707 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7708
7709 alloc_len = scsi_2btoul(cdb->length);
7710
7711 retry:
7712 mtx_lock(&lun->lun_lock);
7713 switch (cdb->action) {
7714 case SPRI_RK: /* read keys */
7715 total_len = sizeof(struct scsi_per_res_in_keys) +
7716 lun->pr_key_count *
7717 sizeof(struct scsi_per_res_key);
7718 break;
7719 case SPRI_RR: /* read reservation */
7720 if (lun->flags & CTL_LUN_PR_RESERVED)
7721 total_len = sizeof(struct scsi_per_res_in_rsrv);
7722 else
7723 total_len = sizeof(struct scsi_per_res_in_header);
7724 break;
7725 case SPRI_RC: /* report capabilities */
7726 total_len = sizeof(struct scsi_per_res_cap);
7727 break;
7728 case SPRI_RS: /* read full status */
7729 total_len = sizeof(struct scsi_per_res_in_header) +
7730 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7731 lun->pr_key_count;
7732 break;
7733 default:
7734 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7735 }
7736 mtx_unlock(&lun->lun_lock);
7737
7738 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7739 ctsio->kern_rel_offset = 0;
7740 ctsio->kern_sg_entries = 0;
7741 ctsio->kern_data_len = min(total_len, alloc_len);
7742 ctsio->kern_total_len = ctsio->kern_data_len;
7743
7744 mtx_lock(&lun->lun_lock);
7745 switch (cdb->action) {
7746 case SPRI_RK: { // read keys
7747 struct scsi_per_res_in_keys *res_keys;
7748 int i, key_count;
7749
7750 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7751
7752 /*
7753 * We had to drop the lock to allocate our buffer, which
7754 * leaves time for someone to come in with another
7755 * persistent reservation. (That is unlikely, though,
7756 * since this should be the only persistent reservation
7757 * command active right now.)
7758 */
7759 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7760 (lun->pr_key_count *
7761 sizeof(struct scsi_per_res_key)))){
7762 mtx_unlock(&lun->lun_lock);
7763 free(ctsio->kern_data_ptr, M_CTL);
7764 printf("%s: reservation length changed, retrying\n",
7765 __func__);
7766 goto retry;
7767 }
7768
7769 scsi_ulto4b(lun->pr_generation, res_keys->header.generation);
7770
7771 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7772 lun->pr_key_count, res_keys->header.length);
7773
7774 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7775 if ((key = ctl_get_prkey(lun, i)) == 0)
7776 continue;
7777
7778 /*
7779 * We used lun->pr_key_count to calculate the
7780 * size to allocate. If it turns out the number of
7781 * initiators with the registered flag set is
7782 * larger than that (i.e. they haven't been kept in
7783 * sync), we've got a problem.
7784 */
7785 if (key_count >= lun->pr_key_count) {
7786 key_count++;
7787 continue;
7788 }
7789 scsi_u64to8b(key, res_keys->keys[key_count].key);
7790 key_count++;
7791 }
7792 break;
7793 }
7794 case SPRI_RR: { // read reservation
7795 struct scsi_per_res_in_rsrv *res;
7796 int tmp_len, header_only;
7797
7798 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7799
7800 scsi_ulto4b(lun->pr_generation, res->header.generation);
7801
7802 if (lun->flags & CTL_LUN_PR_RESERVED)
7803 {
7804 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7805 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7806 res->header.length);
7807 header_only = 0;
7808 } else {
7809 tmp_len = sizeof(struct scsi_per_res_in_header);
7810 scsi_ulto4b(0, res->header.length);
7811 header_only = 1;
7812 }
7813
7814 /*
7815 * We had to drop the lock to allocate our buffer, which
7816 * leaves time for someone to come in with another
7817 * persistent reservation. (That is unlikely, though,
7818 * since this should be the only persistent reservation
7819 * command active right now.)
7820 */
7821 if (tmp_len != total_len) {
7822 mtx_unlock(&lun->lun_lock);
7823 free(ctsio->kern_data_ptr, M_CTL);
7824 printf("%s: reservation status changed, retrying\n",
7825 __func__);
7826 goto retry;
7827 }
7828
7829 /*
7830 * No reservation held, so we're done.
7831 */
7832 if (header_only != 0)
7833 break;
7834
7835 /*
7836 * If the registration is an All Registrants type, the key
7837 * is 0, since it doesn't really matter.
7838 */
7839 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7840 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7841 res->data.reservation);
7842 }
7843 res->data.scopetype = lun->pr_res_type;
7844 break;
7845 }
7846 case SPRI_RC: //report capabilities
7847 {
7848 struct scsi_per_res_cap *res_cap;
7849 uint16_t type_mask;
7850
7851 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7852 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7853 res_cap->flags1 = SPRI_CRH;
7854 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5;
7855 type_mask = SPRI_TM_WR_EX_AR |
7856 SPRI_TM_EX_AC_RO |
7857 SPRI_TM_WR_EX_RO |
7858 SPRI_TM_EX_AC |
7859 SPRI_TM_WR_EX |
7860 SPRI_TM_EX_AC_AR;
7861 scsi_ulto2b(type_mask, res_cap->type_mask);
7862 break;
7863 }
7864 case SPRI_RS: { // read full status
7865 struct scsi_per_res_in_full *res_status;
7866 struct scsi_per_res_in_full_desc *res_desc;
7867 struct ctl_port *port;
7868 int i, len;
7869
7870 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7871
7872 /*
7873 * We had to drop the lock to allocate our buffer, which
7874 * leaves time for someone to come in with another
7875 * persistent reservation. (That is unlikely, though,
7876 * since this should be the only persistent reservation
7877 * command active right now.)
7878 */
7879 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7880 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7881 lun->pr_key_count)){
7882 mtx_unlock(&lun->lun_lock);
7883 free(ctsio->kern_data_ptr, M_CTL);
7884 printf("%s: reservation length changed, retrying\n",
7885 __func__);
7886 goto retry;
7887 }
7888
7889 scsi_ulto4b(lun->pr_generation, res_status->header.generation);
7890
7891 res_desc = &res_status->desc[0];
7892 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7893 if ((key = ctl_get_prkey(lun, i)) == 0)
7894 continue;
7895
7896 scsi_u64to8b(key, res_desc->res_key.key);
7897 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7898 (lun->pr_res_idx == i ||
7899 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7900 res_desc->flags = SPRI_FULL_R_HOLDER;
7901 res_desc->scopetype = lun->pr_res_type;
7902 }
7903 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7904 res_desc->rel_trgt_port_id);
7905 len = 0;
7906 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7907 if (port != NULL)
7908 len = ctl_create_iid(port,
7909 i % CTL_MAX_INIT_PER_PORT,
7910 res_desc->transport_id);
7911 scsi_ulto4b(len, res_desc->additional_length);
7912 res_desc = (struct scsi_per_res_in_full_desc *)
7913 &res_desc->transport_id[len];
7914 }
7915 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7916 res_status->header.length);
7917 break;
7918 }
7919 default:
7920 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7921 }
7922 mtx_unlock(&lun->lun_lock);
7923
7924 ctl_set_success(ctsio);
7925 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7926 ctsio->be_move_done = ctl_config_move_done;
7927 ctl_datamove((union ctl_io *)ctsio);
7928 return (CTL_RETVAL_COMPLETE);
7929 }
7930
7931 /*
7932 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7933 * it should return.
7934 */
7935 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)7936 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7937 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7938 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7939 struct scsi_per_res_out_parms* param)
7940 {
7941 union ctl_ha_msg persis_io;
7942 int i;
7943
7944 mtx_lock(&lun->lun_lock);
7945 if (sa_res_key == 0) {
7946 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7947 /* validate scope and type */
7948 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7949 SPR_LU_SCOPE) {
7950 mtx_unlock(&lun->lun_lock);
7951 ctl_set_invalid_field(/*ctsio*/ ctsio,
7952 /*sks_valid*/ 1,
7953 /*command*/ 1,
7954 /*field*/ 2,
7955 /*bit_valid*/ 1,
7956 /*bit*/ 4);
7957 ctl_done((union ctl_io *)ctsio);
7958 return (1);
7959 }
7960
7961 if (type>8 || type==2 || type==4 || type==0) {
7962 mtx_unlock(&lun->lun_lock);
7963 ctl_set_invalid_field(/*ctsio*/ ctsio,
7964 /*sks_valid*/ 1,
7965 /*command*/ 1,
7966 /*field*/ 2,
7967 /*bit_valid*/ 1,
7968 /*bit*/ 0);
7969 ctl_done((union ctl_io *)ctsio);
7970 return (1);
7971 }
7972
7973 /*
7974 * Unregister everybody else and build UA for
7975 * them
7976 */
7977 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7978 if (i == residx || ctl_get_prkey(lun, i) == 0)
7979 continue;
7980
7981 ctl_clr_prkey(lun, i);
7982 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7983 }
7984 lun->pr_key_count = 1;
7985 lun->pr_res_type = type;
7986 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7987 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7988 lun->pr_res_idx = residx;
7989 lun->pr_generation++;
7990 mtx_unlock(&lun->lun_lock);
7991
7992 /* send msg to other side */
7993 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7994 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7995 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7996 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7997 persis_io.pr.pr_info.res_type = type;
7998 memcpy(persis_io.pr.pr_info.sa_res_key,
7999 param->serv_act_res_key,
8000 sizeof(param->serv_act_res_key));
8001 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8002 sizeof(persis_io.pr), M_WAITOK);
8003 } else {
8004 /* not all registrants */
8005 mtx_unlock(&lun->lun_lock);
8006 free(ctsio->kern_data_ptr, M_CTL);
8007 ctl_set_invalid_field(ctsio,
8008 /*sks_valid*/ 1,
8009 /*command*/ 0,
8010 /*field*/ 8,
8011 /*bit_valid*/ 0,
8012 /*bit*/ 0);
8013 ctl_done((union ctl_io *)ctsio);
8014 return (1);
8015 }
8016 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8017 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
8018 int found = 0;
8019
8020 if (res_key == sa_res_key) {
8021 /* special case */
8022 /*
8023 * The spec implies this is not good but doesn't
8024 * say what to do. There are two choices either
8025 * generate a res conflict or check condition
8026 * with illegal field in parameter data. Since
8027 * that is what is done when the sa_res_key is
8028 * zero I'll take that approach since this has
8029 * to do with the sa_res_key.
8030 */
8031 mtx_unlock(&lun->lun_lock);
8032 free(ctsio->kern_data_ptr, M_CTL);
8033 ctl_set_invalid_field(ctsio,
8034 /*sks_valid*/ 1,
8035 /*command*/ 0,
8036 /*field*/ 8,
8037 /*bit_valid*/ 0,
8038 /*bit*/ 0);
8039 ctl_done((union ctl_io *)ctsio);
8040 return (1);
8041 }
8042
8043 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8044 if (ctl_get_prkey(lun, i) != sa_res_key)
8045 continue;
8046
8047 found = 1;
8048 ctl_clr_prkey(lun, i);
8049 lun->pr_key_count--;
8050 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8051 }
8052 if (!found) {
8053 mtx_unlock(&lun->lun_lock);
8054 free(ctsio->kern_data_ptr, M_CTL);
8055 ctl_set_reservation_conflict(ctsio);
8056 ctl_done((union ctl_io *)ctsio);
8057 return (CTL_RETVAL_COMPLETE);
8058 }
8059 lun->pr_generation++;
8060 mtx_unlock(&lun->lun_lock);
8061
8062 /* send msg to other side */
8063 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8064 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8065 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8066 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8067 persis_io.pr.pr_info.res_type = type;
8068 memcpy(persis_io.pr.pr_info.sa_res_key,
8069 param->serv_act_res_key,
8070 sizeof(param->serv_act_res_key));
8071 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8072 sizeof(persis_io.pr), M_WAITOK);
8073 } else {
8074 /* Reserved but not all registrants */
8075 /* sa_res_key is res holder */
8076 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
8077 /* validate scope and type */
8078 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
8079 SPR_LU_SCOPE) {
8080 mtx_unlock(&lun->lun_lock);
8081 ctl_set_invalid_field(/*ctsio*/ ctsio,
8082 /*sks_valid*/ 1,
8083 /*command*/ 1,
8084 /*field*/ 2,
8085 /*bit_valid*/ 1,
8086 /*bit*/ 4);
8087 ctl_done((union ctl_io *)ctsio);
8088 return (1);
8089 }
8090
8091 if (type>8 || type==2 || type==4 || type==0) {
8092 mtx_unlock(&lun->lun_lock);
8093 ctl_set_invalid_field(/*ctsio*/ ctsio,
8094 /*sks_valid*/ 1,
8095 /*command*/ 1,
8096 /*field*/ 2,
8097 /*bit_valid*/ 1,
8098 /*bit*/ 0);
8099 ctl_done((union ctl_io *)ctsio);
8100 return (1);
8101 }
8102
8103 /*
8104 * Do the following:
8105 * if sa_res_key != res_key remove all
8106 * registrants w/sa_res_key and generate UA
8107 * for these registrants(Registrations
8108 * Preempted) if it wasn't an exclusive
8109 * reservation generate UA(Reservations
8110 * Preempted) for all other registered nexuses
8111 * if the type has changed. Establish the new
8112 * reservation and holder. If res_key and
8113 * sa_res_key are the same do the above
8114 * except don't unregister the res holder.
8115 */
8116
8117 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8118 if (i == residx || ctl_get_prkey(lun, i) == 0)
8119 continue;
8120
8121 if (sa_res_key == ctl_get_prkey(lun, i)) {
8122 ctl_clr_prkey(lun, i);
8123 lun->pr_key_count--;
8124 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8125 } else if (type != lun->pr_res_type &&
8126 (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8127 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8128 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8129 }
8130 }
8131 lun->pr_res_type = type;
8132 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8133 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8134 lun->pr_res_idx = residx;
8135 else
8136 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8137 lun->pr_generation++;
8138 mtx_unlock(&lun->lun_lock);
8139
8140 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8141 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8142 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8143 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8144 persis_io.pr.pr_info.res_type = type;
8145 memcpy(persis_io.pr.pr_info.sa_res_key,
8146 param->serv_act_res_key,
8147 sizeof(param->serv_act_res_key));
8148 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8149 sizeof(persis_io.pr), M_WAITOK);
8150 } else {
8151 /*
8152 * sa_res_key is not the res holder just
8153 * remove registrants
8154 */
8155 int found=0;
8156
8157 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8158 if (sa_res_key != ctl_get_prkey(lun, i))
8159 continue;
8160
8161 found = 1;
8162 ctl_clr_prkey(lun, i);
8163 lun->pr_key_count--;
8164 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8165 }
8166
8167 if (!found) {
8168 mtx_unlock(&lun->lun_lock);
8169 free(ctsio->kern_data_ptr, M_CTL);
8170 ctl_set_reservation_conflict(ctsio);
8171 ctl_done((union ctl_io *)ctsio);
8172 return (1);
8173 }
8174 lun->pr_generation++;
8175 mtx_unlock(&lun->lun_lock);
8176
8177 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8178 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8179 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8180 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8181 persis_io.pr.pr_info.res_type = type;
8182 memcpy(persis_io.pr.pr_info.sa_res_key,
8183 param->serv_act_res_key,
8184 sizeof(param->serv_act_res_key));
8185 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8186 sizeof(persis_io.pr), M_WAITOK);
8187 }
8188 }
8189 return (0);
8190 }
8191
8192 static void
ctl_pro_preempt_other(struct ctl_lun * lun,union ctl_ha_msg * msg)8193 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8194 {
8195 uint64_t sa_res_key;
8196 int i;
8197
8198 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8199
8200 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8201 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8202 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8203 if (sa_res_key == 0) {
8204 /*
8205 * Unregister everybody else and build UA for
8206 * them
8207 */
8208 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8209 if (i == msg->pr.pr_info.residx ||
8210 ctl_get_prkey(lun, i) == 0)
8211 continue;
8212
8213 ctl_clr_prkey(lun, i);
8214 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8215 }
8216
8217 lun->pr_key_count = 1;
8218 lun->pr_res_type = msg->pr.pr_info.res_type;
8219 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8220 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8221 lun->pr_res_idx = msg->pr.pr_info.residx;
8222 } else {
8223 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8224 if (sa_res_key == ctl_get_prkey(lun, i))
8225 continue;
8226
8227 ctl_clr_prkey(lun, i);
8228 lun->pr_key_count--;
8229 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8230 }
8231 }
8232 } else {
8233 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8234 if (i == msg->pr.pr_info.residx ||
8235 ctl_get_prkey(lun, i) == 0)
8236 continue;
8237
8238 if (sa_res_key == ctl_get_prkey(lun, i)) {
8239 ctl_clr_prkey(lun, i);
8240 lun->pr_key_count--;
8241 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8242 } else if (msg->pr.pr_info.res_type != lun->pr_res_type
8243 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8244 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8245 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8246 }
8247 }
8248 lun->pr_res_type = msg->pr.pr_info.res_type;
8249 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8250 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8251 lun->pr_res_idx = msg->pr.pr_info.residx;
8252 else
8253 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8254 }
8255 lun->pr_generation++;
8256
8257 }
8258
8259 int
ctl_persistent_reserve_out(struct ctl_scsiio * ctsio)8260 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8261 {
8262 struct ctl_softc *softc = CTL_SOFTC(ctsio);
8263 struct ctl_lun *lun = CTL_LUN(ctsio);
8264 int retval;
8265 u_int32_t param_len;
8266 struct scsi_per_res_out *cdb;
8267 struct scsi_per_res_out_parms* param;
8268 uint32_t residx;
8269 uint64_t res_key, sa_res_key, key;
8270 uint8_t type;
8271 union ctl_ha_msg persis_io;
8272 int i;
8273
8274 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8275
8276 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8277 retval = CTL_RETVAL_COMPLETE;
8278
8279 /*
8280 * We only support whole-LUN scope. The scope & type are ignored for
8281 * register, register and ignore existing key and clear.
8282 * We sometimes ignore scope and type on preempts too!!
8283 * Verify reservation type here as well.
8284 */
8285 type = cdb->scope_type & SPR_TYPE_MASK;
8286 if ((cdb->action == SPRO_RESERVE)
8287 || (cdb->action == SPRO_RELEASE)) {
8288 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8289 ctl_set_invalid_field(/*ctsio*/ ctsio,
8290 /*sks_valid*/ 1,
8291 /*command*/ 1,
8292 /*field*/ 2,
8293 /*bit_valid*/ 1,
8294 /*bit*/ 4);
8295 ctl_done((union ctl_io *)ctsio);
8296 return (CTL_RETVAL_COMPLETE);
8297 }
8298
8299 if (type>8 || type==2 || type==4 || type==0) {
8300 ctl_set_invalid_field(/*ctsio*/ ctsio,
8301 /*sks_valid*/ 1,
8302 /*command*/ 1,
8303 /*field*/ 2,
8304 /*bit_valid*/ 1,
8305 /*bit*/ 0);
8306 ctl_done((union ctl_io *)ctsio);
8307 return (CTL_RETVAL_COMPLETE);
8308 }
8309 }
8310
8311 param_len = scsi_4btoul(cdb->length);
8312
8313 /* validate the parameter length */
8314 if (param_len != 24) {
8315 ctl_set_invalid_field(ctsio,
8316 /*sks_valid*/ 1,
8317 /*command*/ 1,
8318 /*field*/ 5,
8319 /*bit_valid*/ 1,
8320 /*bit*/ 0);
8321 ctl_done((union ctl_io *)ctsio);
8322 return (CTL_RETVAL_COMPLETE);
8323 }
8324
8325 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8326 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8327 ctsio->kern_data_len = param_len;
8328 ctsio->kern_total_len = param_len;
8329 ctsio->kern_rel_offset = 0;
8330 ctsio->kern_sg_entries = 0;
8331 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8332 ctsio->be_move_done = ctl_config_move_done;
8333 ctl_datamove((union ctl_io *)ctsio);
8334
8335 return (CTL_RETVAL_COMPLETE);
8336 }
8337
8338 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8339
8340 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8341 res_key = scsi_8btou64(param->res_key.key);
8342 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8343
8344 /*
8345 * Validate the reservation key here except for SPRO_REG_IGNO
8346 * This must be done for all other service actions
8347 */
8348 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8349 mtx_lock(&lun->lun_lock);
8350 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8351 if (res_key != key) {
8352 /*
8353 * The current key passed in doesn't match
8354 * the one the initiator previously
8355 * registered.
8356 */
8357 mtx_unlock(&lun->lun_lock);
8358 free(ctsio->kern_data_ptr, M_CTL);
8359 ctl_set_reservation_conflict(ctsio);
8360 ctl_done((union ctl_io *)ctsio);
8361 return (CTL_RETVAL_COMPLETE);
8362 }
8363 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8364 /*
8365 * We are not registered
8366 */
8367 mtx_unlock(&lun->lun_lock);
8368 free(ctsio->kern_data_ptr, M_CTL);
8369 ctl_set_reservation_conflict(ctsio);
8370 ctl_done((union ctl_io *)ctsio);
8371 return (CTL_RETVAL_COMPLETE);
8372 } else if (res_key != 0) {
8373 /*
8374 * We are not registered and trying to register but
8375 * the register key isn't zero.
8376 */
8377 mtx_unlock(&lun->lun_lock);
8378 free(ctsio->kern_data_ptr, M_CTL);
8379 ctl_set_reservation_conflict(ctsio);
8380 ctl_done((union ctl_io *)ctsio);
8381 return (CTL_RETVAL_COMPLETE);
8382 }
8383 mtx_unlock(&lun->lun_lock);
8384 }
8385
8386 switch (cdb->action & SPRO_ACTION_MASK) {
8387 case SPRO_REGISTER:
8388 case SPRO_REG_IGNO: {
8389 /*
8390 * We don't support any of these options, as we report in
8391 * the read capabilities request (see
8392 * ctl_persistent_reserve_in(), above).
8393 */
8394 if ((param->flags & SPR_SPEC_I_PT)
8395 || (param->flags & SPR_ALL_TG_PT)
8396 || (param->flags & SPR_APTPL)) {
8397 int bit_ptr;
8398
8399 if (param->flags & SPR_APTPL)
8400 bit_ptr = 0;
8401 else if (param->flags & SPR_ALL_TG_PT)
8402 bit_ptr = 2;
8403 else /* SPR_SPEC_I_PT */
8404 bit_ptr = 3;
8405
8406 free(ctsio->kern_data_ptr, M_CTL);
8407 ctl_set_invalid_field(ctsio,
8408 /*sks_valid*/ 1,
8409 /*command*/ 0,
8410 /*field*/ 20,
8411 /*bit_valid*/ 1,
8412 /*bit*/ bit_ptr);
8413 ctl_done((union ctl_io *)ctsio);
8414 return (CTL_RETVAL_COMPLETE);
8415 }
8416
8417 mtx_lock(&lun->lun_lock);
8418
8419 /*
8420 * The initiator wants to clear the
8421 * key/unregister.
8422 */
8423 if (sa_res_key == 0) {
8424 if ((res_key == 0
8425 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8426 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8427 && ctl_get_prkey(lun, residx) == 0)) {
8428 mtx_unlock(&lun->lun_lock);
8429 goto done;
8430 }
8431
8432 ctl_clr_prkey(lun, residx);
8433 lun->pr_key_count--;
8434
8435 if (residx == lun->pr_res_idx) {
8436 lun->flags &= ~CTL_LUN_PR_RESERVED;
8437 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8438
8439 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8440 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8441 lun->pr_key_count) {
8442 /*
8443 * If the reservation is a registrants
8444 * only type we need to generate a UA
8445 * for other registered inits. The
8446 * sense code should be RESERVATIONS
8447 * RELEASED
8448 */
8449
8450 for (i = softc->init_min; i < softc->init_max; i++){
8451 if (ctl_get_prkey(lun, i) == 0)
8452 continue;
8453 ctl_est_ua(lun, i,
8454 CTL_UA_RES_RELEASE);
8455 }
8456 }
8457 lun->pr_res_type = 0;
8458 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8459 if (lun->pr_key_count==0) {
8460 lun->flags &= ~CTL_LUN_PR_RESERVED;
8461 lun->pr_res_type = 0;
8462 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8463 }
8464 }
8465 lun->pr_generation++;
8466 mtx_unlock(&lun->lun_lock);
8467
8468 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8469 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8470 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8471 persis_io.pr.pr_info.residx = residx;
8472 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8473 sizeof(persis_io.pr), M_WAITOK);
8474 } else /* sa_res_key != 0 */ {
8475 /*
8476 * If we aren't registered currently then increment
8477 * the key count and set the registered flag.
8478 */
8479 ctl_alloc_prkey(lun, residx);
8480 if (ctl_get_prkey(lun, residx) == 0)
8481 lun->pr_key_count++;
8482 ctl_set_prkey(lun, residx, sa_res_key);
8483 lun->pr_generation++;
8484 mtx_unlock(&lun->lun_lock);
8485
8486 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8487 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8488 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8489 persis_io.pr.pr_info.residx = residx;
8490 memcpy(persis_io.pr.pr_info.sa_res_key,
8491 param->serv_act_res_key,
8492 sizeof(param->serv_act_res_key));
8493 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8494 sizeof(persis_io.pr), M_WAITOK);
8495 }
8496
8497 break;
8498 }
8499 case SPRO_RESERVE:
8500 mtx_lock(&lun->lun_lock);
8501 if (lun->flags & CTL_LUN_PR_RESERVED) {
8502 /*
8503 * if this isn't the reservation holder and it's
8504 * not a "all registrants" type or if the type is
8505 * different then we have a conflict
8506 */
8507 if ((lun->pr_res_idx != residx
8508 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8509 || lun->pr_res_type != type) {
8510 mtx_unlock(&lun->lun_lock);
8511 free(ctsio->kern_data_ptr, M_CTL);
8512 ctl_set_reservation_conflict(ctsio);
8513 ctl_done((union ctl_io *)ctsio);
8514 return (CTL_RETVAL_COMPLETE);
8515 }
8516 mtx_unlock(&lun->lun_lock);
8517 } else /* create a reservation */ {
8518 /*
8519 * If it's not an "all registrants" type record
8520 * reservation holder
8521 */
8522 if (type != SPR_TYPE_WR_EX_AR
8523 && type != SPR_TYPE_EX_AC_AR)
8524 lun->pr_res_idx = residx; /* Res holder */
8525 else
8526 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8527
8528 lun->flags |= CTL_LUN_PR_RESERVED;
8529 lun->pr_res_type = type;
8530
8531 mtx_unlock(&lun->lun_lock);
8532
8533 /* send msg to other side */
8534 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8535 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8536 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8537 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8538 persis_io.pr.pr_info.res_type = type;
8539 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8540 sizeof(persis_io.pr), M_WAITOK);
8541 }
8542 break;
8543
8544 case SPRO_RELEASE:
8545 mtx_lock(&lun->lun_lock);
8546 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8547 /* No reservation exists return good status */
8548 mtx_unlock(&lun->lun_lock);
8549 goto done;
8550 }
8551 /*
8552 * Is this nexus a reservation holder?
8553 */
8554 if (lun->pr_res_idx != residx
8555 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8556 /*
8557 * not a res holder return good status but
8558 * do nothing
8559 */
8560 mtx_unlock(&lun->lun_lock);
8561 goto done;
8562 }
8563
8564 if (lun->pr_res_type != type) {
8565 mtx_unlock(&lun->lun_lock);
8566 free(ctsio->kern_data_ptr, M_CTL);
8567 ctl_set_illegal_pr_release(ctsio);
8568 ctl_done((union ctl_io *)ctsio);
8569 return (CTL_RETVAL_COMPLETE);
8570 }
8571
8572 /* okay to release */
8573 lun->flags &= ~CTL_LUN_PR_RESERVED;
8574 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8575 lun->pr_res_type = 0;
8576
8577 /*
8578 * If this isn't an exclusive access reservation and NUAR
8579 * is not set, generate UA for all other registrants.
8580 */
8581 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX &&
8582 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8583 for (i = softc->init_min; i < softc->init_max; i++) {
8584 if (i == residx || ctl_get_prkey(lun, i) == 0)
8585 continue;
8586 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8587 }
8588 }
8589 mtx_unlock(&lun->lun_lock);
8590
8591 /* Send msg to other side */
8592 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8593 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8594 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8595 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8596 sizeof(persis_io.pr), M_WAITOK);
8597 break;
8598
8599 case SPRO_CLEAR:
8600 /* send msg to other side */
8601
8602 mtx_lock(&lun->lun_lock);
8603 lun->flags &= ~CTL_LUN_PR_RESERVED;
8604 lun->pr_res_type = 0;
8605 lun->pr_key_count = 0;
8606 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8607
8608 ctl_clr_prkey(lun, residx);
8609 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8610 if (ctl_get_prkey(lun, i) != 0) {
8611 ctl_clr_prkey(lun, i);
8612 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8613 }
8614 lun->pr_generation++;
8615 mtx_unlock(&lun->lun_lock);
8616
8617 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8618 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8619 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8620 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8621 sizeof(persis_io.pr), M_WAITOK);
8622 break;
8623
8624 case SPRO_PREEMPT:
8625 case SPRO_PRE_ABO: {
8626 int nretval;
8627
8628 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8629 residx, ctsio, cdb, param);
8630 if (nretval != 0)
8631 return (CTL_RETVAL_COMPLETE);
8632 break;
8633 }
8634 default:
8635 panic("%s: Invalid PR type %#x", __func__, cdb->action);
8636 }
8637
8638 done:
8639 free(ctsio->kern_data_ptr, M_CTL);
8640 ctl_set_success(ctsio);
8641 ctl_done((union ctl_io *)ctsio);
8642
8643 return (retval);
8644 }
8645
8646 /*
8647 * This routine is for handling a message from the other SC pertaining to
8648 * persistent reserve out. All the error checking will have been done
8649 * so only performing the action need be done here to keep the two
8650 * in sync.
8651 */
8652 static void
ctl_hndl_per_res_out_on_other_sc(union ctl_io * io)8653 ctl_hndl_per_res_out_on_other_sc(union ctl_io *io)
8654 {
8655 struct ctl_softc *softc = CTL_SOFTC(io);
8656 union ctl_ha_msg *msg = (union ctl_ha_msg *)&io->presio.pr_msg;
8657 struct ctl_lun *lun;
8658 int i;
8659 uint32_t residx, targ_lun;
8660
8661 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8662 mtx_lock(&softc->ctl_lock);
8663 if (targ_lun >= ctl_max_luns ||
8664 (lun = softc->ctl_luns[targ_lun]) == NULL) {
8665 mtx_unlock(&softc->ctl_lock);
8666 return;
8667 }
8668 mtx_lock(&lun->lun_lock);
8669 mtx_unlock(&softc->ctl_lock);
8670 if (lun->flags & CTL_LUN_DISABLED) {
8671 mtx_unlock(&lun->lun_lock);
8672 return;
8673 }
8674 residx = ctl_get_initindex(&msg->hdr.nexus);
8675 switch(msg->pr.pr_info.action) {
8676 case CTL_PR_REG_KEY:
8677 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8678 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8679 lun->pr_key_count++;
8680 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8681 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8682 lun->pr_generation++;
8683 break;
8684
8685 case CTL_PR_UNREG_KEY:
8686 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8687 lun->pr_key_count--;
8688
8689 /* XXX Need to see if the reservation has been released */
8690 /* if so do we need to generate UA? */
8691 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8692 lun->flags &= ~CTL_LUN_PR_RESERVED;
8693 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8694
8695 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8696 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8697 lun->pr_key_count) {
8698 /*
8699 * If the reservation is a registrants
8700 * only type we need to generate a UA
8701 * for other registered inits. The
8702 * sense code should be RESERVATIONS
8703 * RELEASED
8704 */
8705
8706 for (i = softc->init_min; i < softc->init_max; i++) {
8707 if (ctl_get_prkey(lun, i) == 0)
8708 continue;
8709
8710 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8711 }
8712 }
8713 lun->pr_res_type = 0;
8714 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8715 if (lun->pr_key_count==0) {
8716 lun->flags &= ~CTL_LUN_PR_RESERVED;
8717 lun->pr_res_type = 0;
8718 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8719 }
8720 }
8721 lun->pr_generation++;
8722 break;
8723
8724 case CTL_PR_RESERVE:
8725 lun->flags |= CTL_LUN_PR_RESERVED;
8726 lun->pr_res_type = msg->pr.pr_info.res_type;
8727 lun->pr_res_idx = msg->pr.pr_info.residx;
8728
8729 break;
8730
8731 case CTL_PR_RELEASE:
8732 /*
8733 * If this isn't an exclusive access reservation and NUAR
8734 * is not set, generate UA for all other registrants.
8735 */
8736 if (lun->pr_res_type != SPR_TYPE_EX_AC &&
8737 lun->pr_res_type != SPR_TYPE_WR_EX &&
8738 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8739 for (i = softc->init_min; i < softc->init_max; i++) {
8740 if (i == residx || ctl_get_prkey(lun, i) == 0)
8741 continue;
8742 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8743 }
8744 }
8745
8746 lun->flags &= ~CTL_LUN_PR_RESERVED;
8747 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8748 lun->pr_res_type = 0;
8749 break;
8750
8751 case CTL_PR_PREEMPT:
8752 ctl_pro_preempt_other(lun, msg);
8753 break;
8754 case CTL_PR_CLEAR:
8755 lun->flags &= ~CTL_LUN_PR_RESERVED;
8756 lun->pr_res_type = 0;
8757 lun->pr_key_count = 0;
8758 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8759
8760 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8761 if (ctl_get_prkey(lun, i) == 0)
8762 continue;
8763 ctl_clr_prkey(lun, i);
8764 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8765 }
8766 lun->pr_generation++;
8767 break;
8768 }
8769
8770 mtx_unlock(&lun->lun_lock);
8771 }
8772
8773 int
ctl_read_write(struct ctl_scsiio * ctsio)8774 ctl_read_write(struct ctl_scsiio *ctsio)
8775 {
8776 struct ctl_lun *lun = CTL_LUN(ctsio);
8777 struct ctl_lba_len_flags *lbalen;
8778 uint64_t lba;
8779 uint32_t num_blocks;
8780 int flags, retval;
8781 int isread;
8782
8783 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8784
8785 flags = 0;
8786 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8787 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8788 switch (ctsio->cdb[0]) {
8789 case READ_6:
8790 case WRITE_6: {
8791 struct scsi_rw_6 *cdb;
8792
8793 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8794
8795 lba = scsi_3btoul(cdb->addr);
8796 /* only 5 bits are valid in the most significant address byte */
8797 lba &= 0x1fffff;
8798 num_blocks = cdb->length;
8799 /*
8800 * This is correct according to SBC-2.
8801 */
8802 if (num_blocks == 0)
8803 num_blocks = 256;
8804 break;
8805 }
8806 case READ_10:
8807 case WRITE_10: {
8808 struct scsi_rw_10 *cdb;
8809
8810 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8811 if (cdb->byte2 & SRW10_FUA)
8812 flags |= CTL_LLF_FUA;
8813 if (cdb->byte2 & SRW10_DPO)
8814 flags |= CTL_LLF_DPO;
8815 lba = scsi_4btoul(cdb->addr);
8816 num_blocks = scsi_2btoul(cdb->length);
8817 break;
8818 }
8819 case WRITE_VERIFY_10: {
8820 struct scsi_write_verify_10 *cdb;
8821
8822 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8823 flags |= CTL_LLF_FUA;
8824 if (cdb->byte2 & SWV_DPO)
8825 flags |= CTL_LLF_DPO;
8826 lba = scsi_4btoul(cdb->addr);
8827 num_blocks = scsi_2btoul(cdb->length);
8828 break;
8829 }
8830 case READ_12:
8831 case WRITE_12: {
8832 struct scsi_rw_12 *cdb;
8833
8834 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8835 if (cdb->byte2 & SRW12_FUA)
8836 flags |= CTL_LLF_FUA;
8837 if (cdb->byte2 & SRW12_DPO)
8838 flags |= CTL_LLF_DPO;
8839 lba = scsi_4btoul(cdb->addr);
8840 num_blocks = scsi_4btoul(cdb->length);
8841 break;
8842 }
8843 case WRITE_VERIFY_12: {
8844 struct scsi_write_verify_12 *cdb;
8845
8846 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8847 flags |= CTL_LLF_FUA;
8848 if (cdb->byte2 & SWV_DPO)
8849 flags |= CTL_LLF_DPO;
8850 lba = scsi_4btoul(cdb->addr);
8851 num_blocks = scsi_4btoul(cdb->length);
8852 break;
8853 }
8854 case READ_16:
8855 case WRITE_16: {
8856 struct scsi_rw_16 *cdb;
8857
8858 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8859 if (cdb->byte2 & SRW12_FUA)
8860 flags |= CTL_LLF_FUA;
8861 if (cdb->byte2 & SRW12_DPO)
8862 flags |= CTL_LLF_DPO;
8863 lba = scsi_8btou64(cdb->addr);
8864 num_blocks = scsi_4btoul(cdb->length);
8865 break;
8866 }
8867 case WRITE_ATOMIC_16: {
8868 struct scsi_write_atomic_16 *cdb;
8869
8870 if (lun->be_lun->atomicblock == 0) {
8871 ctl_set_invalid_opcode(ctsio);
8872 ctl_done((union ctl_io *)ctsio);
8873 return (CTL_RETVAL_COMPLETE);
8874 }
8875
8876 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb;
8877 if (cdb->byte2 & SRW12_FUA)
8878 flags |= CTL_LLF_FUA;
8879 if (cdb->byte2 & SRW12_DPO)
8880 flags |= CTL_LLF_DPO;
8881 lba = scsi_8btou64(cdb->addr);
8882 num_blocks = scsi_2btoul(cdb->length);
8883 if (num_blocks > lun->be_lun->atomicblock) {
8884 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8885 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8886 /*bit*/ 0);
8887 ctl_done((union ctl_io *)ctsio);
8888 return (CTL_RETVAL_COMPLETE);
8889 }
8890 break;
8891 }
8892 case WRITE_VERIFY_16: {
8893 struct scsi_write_verify_16 *cdb;
8894
8895 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8896 flags |= CTL_LLF_FUA;
8897 if (cdb->byte2 & SWV_DPO)
8898 flags |= CTL_LLF_DPO;
8899 lba = scsi_8btou64(cdb->addr);
8900 num_blocks = scsi_4btoul(cdb->length);
8901 break;
8902 }
8903 default:
8904 /*
8905 * We got a command we don't support. This shouldn't
8906 * happen, commands should be filtered out above us.
8907 */
8908 ctl_set_invalid_opcode(ctsio);
8909 ctl_done((union ctl_io *)ctsio);
8910
8911 return (CTL_RETVAL_COMPLETE);
8912 break; /* NOTREACHED */
8913 }
8914
8915 /*
8916 * The first check is to make sure we're in bounds, the second
8917 * check is to catch wrap-around problems. If the lba + num blocks
8918 * is less than the lba, then we've wrapped around and the block
8919 * range is invalid anyway.
8920 */
8921 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8922 || ((lba + num_blocks) < lba)) {
8923 ctl_set_lba_out_of_range(ctsio,
8924 MAX(lba, lun->be_lun->maxlba + 1));
8925 ctl_done((union ctl_io *)ctsio);
8926 return (CTL_RETVAL_COMPLETE);
8927 }
8928
8929 /*
8930 * According to SBC-3, a transfer length of 0 is not an error.
8931 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8932 * translates to 256 blocks for those commands.
8933 */
8934 if (num_blocks == 0) {
8935 ctl_set_success(ctsio);
8936 ctl_done((union ctl_io *)ctsio);
8937 return (CTL_RETVAL_COMPLETE);
8938 }
8939
8940 /* Set FUA and/or DPO if caches are disabled. */
8941 if (isread) {
8942 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0)
8943 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8944 } else {
8945 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8946 flags |= CTL_LLF_FUA;
8947 }
8948
8949 lbalen = (struct ctl_lba_len_flags *)
8950 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8951 lbalen->lba = lba;
8952 lbalen->len = num_blocks;
8953 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8954
8955 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8956 ctsio->kern_rel_offset = 0;
8957
8958 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8959
8960 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8961 return (retval);
8962 }
8963
8964 static int
ctl_cnw_cont(union ctl_io * io)8965 ctl_cnw_cont(union ctl_io *io)
8966 {
8967 struct ctl_lun *lun = CTL_LUN(io);
8968 struct ctl_scsiio *ctsio;
8969 struct ctl_lba_len_flags *lbalen;
8970 int retval;
8971
8972 ctsio = &io->scsiio;
8973 ctsio->io_hdr.status = CTL_STATUS_NONE;
8974 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8975 lbalen = (struct ctl_lba_len_flags *)
8976 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8977 lbalen->flags &= ~CTL_LLF_COMPARE;
8978 lbalen->flags |= CTL_LLF_WRITE;
8979
8980 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8981 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8982 return (retval);
8983 }
8984
8985 int
ctl_cnw(struct ctl_scsiio * ctsio)8986 ctl_cnw(struct ctl_scsiio *ctsio)
8987 {
8988 struct ctl_lun *lun = CTL_LUN(ctsio);
8989 struct ctl_lba_len_flags *lbalen;
8990 uint64_t lba;
8991 uint32_t num_blocks;
8992 int flags, retval;
8993
8994 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8995
8996 flags = 0;
8997 switch (ctsio->cdb[0]) {
8998 case COMPARE_AND_WRITE: {
8999 struct scsi_compare_and_write *cdb;
9000
9001 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
9002 if (cdb->byte2 & SRW10_FUA)
9003 flags |= CTL_LLF_FUA;
9004 if (cdb->byte2 & SRW10_DPO)
9005 flags |= CTL_LLF_DPO;
9006 lba = scsi_8btou64(cdb->addr);
9007 num_blocks = cdb->length;
9008 break;
9009 }
9010 default:
9011 /*
9012 * We got a command we don't support. This shouldn't
9013 * happen, commands should be filtered out above us.
9014 */
9015 ctl_set_invalid_opcode(ctsio);
9016 ctl_done((union ctl_io *)ctsio);
9017
9018 return (CTL_RETVAL_COMPLETE);
9019 break; /* NOTREACHED */
9020 }
9021
9022 /*
9023 * The first check is to make sure we're in bounds, the second
9024 * check is to catch wrap-around problems. If the lba + num blocks
9025 * is less than the lba, then we've wrapped around and the block
9026 * range is invalid anyway.
9027 */
9028 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9029 || ((lba + num_blocks) < lba)) {
9030 ctl_set_lba_out_of_range(ctsio,
9031 MAX(lba, lun->be_lun->maxlba + 1));
9032 ctl_done((union ctl_io *)ctsio);
9033 return (CTL_RETVAL_COMPLETE);
9034 }
9035
9036 /*
9037 * According to SBC-3, a transfer length of 0 is not an error.
9038 */
9039 if (num_blocks == 0) {
9040 ctl_set_success(ctsio);
9041 ctl_done((union ctl_io *)ctsio);
9042 return (CTL_RETVAL_COMPLETE);
9043 }
9044
9045 /* Set FUA if write cache is disabled. */
9046 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
9047 flags |= CTL_LLF_FUA;
9048
9049 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
9050 ctsio->kern_rel_offset = 0;
9051
9052 /*
9053 * Set the IO_CONT flag, so that if this I/O gets passed to
9054 * ctl_data_submit_done(), it'll get passed back to
9055 * ctl_ctl_cnw_cont() for further processing.
9056 */
9057 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
9058 ctsio->io_cont = ctl_cnw_cont;
9059
9060 lbalen = (struct ctl_lba_len_flags *)
9061 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9062 lbalen->lba = lba;
9063 lbalen->len = num_blocks;
9064 lbalen->flags = CTL_LLF_COMPARE | flags;
9065
9066 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
9067 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9068 return (retval);
9069 }
9070
9071 int
ctl_verify(struct ctl_scsiio * ctsio)9072 ctl_verify(struct ctl_scsiio *ctsio)
9073 {
9074 struct ctl_lun *lun = CTL_LUN(ctsio);
9075 struct ctl_lba_len_flags *lbalen;
9076 uint64_t lba;
9077 uint32_t num_blocks;
9078 int bytchk, flags;
9079 int retval;
9080
9081 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
9082
9083 bytchk = 0;
9084 flags = CTL_LLF_FUA;
9085 switch (ctsio->cdb[0]) {
9086 case VERIFY_10: {
9087 struct scsi_verify_10 *cdb;
9088
9089 cdb = (struct scsi_verify_10 *)ctsio->cdb;
9090 if (cdb->byte2 & SVFY_BYTCHK)
9091 bytchk = 1;
9092 if (cdb->byte2 & SVFY_DPO)
9093 flags |= CTL_LLF_DPO;
9094 lba = scsi_4btoul(cdb->addr);
9095 num_blocks = scsi_2btoul(cdb->length);
9096 break;
9097 }
9098 case VERIFY_12: {
9099 struct scsi_verify_12 *cdb;
9100
9101 cdb = (struct scsi_verify_12 *)ctsio->cdb;
9102 if (cdb->byte2 & SVFY_BYTCHK)
9103 bytchk = 1;
9104 if (cdb->byte2 & SVFY_DPO)
9105 flags |= CTL_LLF_DPO;
9106 lba = scsi_4btoul(cdb->addr);
9107 num_blocks = scsi_4btoul(cdb->length);
9108 break;
9109 }
9110 case VERIFY_16: {
9111 struct scsi_rw_16 *cdb;
9112
9113 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9114 if (cdb->byte2 & SVFY_BYTCHK)
9115 bytchk = 1;
9116 if (cdb->byte2 & SVFY_DPO)
9117 flags |= CTL_LLF_DPO;
9118 lba = scsi_8btou64(cdb->addr);
9119 num_blocks = scsi_4btoul(cdb->length);
9120 break;
9121 }
9122 default:
9123 /*
9124 * We got a command we don't support. This shouldn't
9125 * happen, commands should be filtered out above us.
9126 */
9127 ctl_set_invalid_opcode(ctsio);
9128 ctl_done((union ctl_io *)ctsio);
9129 return (CTL_RETVAL_COMPLETE);
9130 }
9131
9132 /*
9133 * The first check is to make sure we're in bounds, the second
9134 * check is to catch wrap-around problems. If the lba + num blocks
9135 * is less than the lba, then we've wrapped around and the block
9136 * range is invalid anyway.
9137 */
9138 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9139 || ((lba + num_blocks) < lba)) {
9140 ctl_set_lba_out_of_range(ctsio,
9141 MAX(lba, lun->be_lun->maxlba + 1));
9142 ctl_done((union ctl_io *)ctsio);
9143 return (CTL_RETVAL_COMPLETE);
9144 }
9145
9146 /*
9147 * According to SBC-3, a transfer length of 0 is not an error.
9148 */
9149 if (num_blocks == 0) {
9150 ctl_set_success(ctsio);
9151 ctl_done((union ctl_io *)ctsio);
9152 return (CTL_RETVAL_COMPLETE);
9153 }
9154
9155 lbalen = (struct ctl_lba_len_flags *)
9156 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9157 lbalen->lba = lba;
9158 lbalen->len = num_blocks;
9159 if (bytchk) {
9160 lbalen->flags = CTL_LLF_COMPARE | flags;
9161 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9162 } else {
9163 lbalen->flags = CTL_LLF_VERIFY | flags;
9164 ctsio->kern_total_len = 0;
9165 }
9166 ctsio->kern_rel_offset = 0;
9167
9168 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9169 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9170 return (retval);
9171 }
9172
9173 int
ctl_report_luns(struct ctl_scsiio * ctsio)9174 ctl_report_luns(struct ctl_scsiio *ctsio)
9175 {
9176 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9177 struct ctl_port *port = CTL_PORT(ctsio);
9178 struct ctl_lun *lun, *request_lun = CTL_LUN(ctsio);
9179 struct scsi_report_luns *cdb;
9180 struct scsi_report_luns_data *lun_data;
9181 int num_filled, num_luns, num_port_luns, retval;
9182 uint32_t alloc_len, lun_datalen;
9183 uint32_t initidx, targ_lun_id, lun_id;
9184
9185 retval = CTL_RETVAL_COMPLETE;
9186 cdb = (struct scsi_report_luns *)ctsio->cdb;
9187
9188 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9189
9190 num_luns = 0;
9191 num_port_luns = port->lun_map ? port->lun_map_size : ctl_max_luns;
9192 mtx_lock(&softc->ctl_lock);
9193 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) {
9194 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX)
9195 num_luns++;
9196 }
9197 mtx_unlock(&softc->ctl_lock);
9198
9199 switch (cdb->select_report) {
9200 case RPL_REPORT_DEFAULT:
9201 case RPL_REPORT_ALL:
9202 case RPL_REPORT_NONSUBSID:
9203 break;
9204 case RPL_REPORT_WELLKNOWN:
9205 case RPL_REPORT_ADMIN:
9206 case RPL_REPORT_CONGLOM:
9207 num_luns = 0;
9208 break;
9209 default:
9210 ctl_set_invalid_field(ctsio,
9211 /*sks_valid*/ 1,
9212 /*command*/ 1,
9213 /*field*/ 2,
9214 /*bit_valid*/ 0,
9215 /*bit*/ 0);
9216 ctl_done((union ctl_io *)ctsio);
9217 return (retval);
9218 break; /* NOTREACHED */
9219 }
9220
9221 alloc_len = scsi_4btoul(cdb->length);
9222 /*
9223 * The initiator has to allocate at least 16 bytes for this request,
9224 * so he can at least get the header and the first LUN. Otherwise
9225 * we reject the request (per SPC-3 rev 14, section 6.21).
9226 */
9227 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9228 sizeof(struct scsi_report_luns_lundata))) {
9229 ctl_set_invalid_field(ctsio,
9230 /*sks_valid*/ 1,
9231 /*command*/ 1,
9232 /*field*/ 6,
9233 /*bit_valid*/ 0,
9234 /*bit*/ 0);
9235 ctl_done((union ctl_io *)ctsio);
9236 return (retval);
9237 }
9238
9239 lun_datalen = sizeof(*lun_data) +
9240 (num_luns * sizeof(struct scsi_report_luns_lundata));
9241
9242 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9243 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9244 ctsio->kern_sg_entries = 0;
9245
9246 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9247
9248 mtx_lock(&softc->ctl_lock);
9249 for (targ_lun_id = 0, num_filled = 0;
9250 targ_lun_id < num_port_luns && num_filled < num_luns;
9251 targ_lun_id++) {
9252 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9253 if (lun_id == UINT32_MAX)
9254 continue;
9255 lun = softc->ctl_luns[lun_id];
9256 if (lun == NULL)
9257 continue;
9258
9259 be64enc(lun_data->luns[num_filled++].lundata,
9260 ctl_encode_lun(targ_lun_id));
9261
9262 /*
9263 * According to SPC-3, rev 14 section 6.21:
9264 *
9265 * "The execution of a REPORT LUNS command to any valid and
9266 * installed logical unit shall clear the REPORTED LUNS DATA
9267 * HAS CHANGED unit attention condition for all logical
9268 * units of that target with respect to the requesting
9269 * initiator. A valid and installed logical unit is one
9270 * having a PERIPHERAL QUALIFIER of 000b in the standard
9271 * INQUIRY data (see 6.4.2)."
9272 *
9273 * If request_lun is NULL, the LUN this report luns command
9274 * was issued to is either disabled or doesn't exist. In that
9275 * case, we shouldn't clear any pending lun change unit
9276 * attention.
9277 */
9278 if (request_lun != NULL) {
9279 mtx_lock(&lun->lun_lock);
9280 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9281 mtx_unlock(&lun->lun_lock);
9282 }
9283 }
9284 mtx_unlock(&softc->ctl_lock);
9285
9286 /*
9287 * It's quite possible that we've returned fewer LUNs than we allocated
9288 * space for. Trim it.
9289 */
9290 lun_datalen = sizeof(*lun_data) +
9291 (num_filled * sizeof(struct scsi_report_luns_lundata));
9292 ctsio->kern_rel_offset = 0;
9293 ctsio->kern_sg_entries = 0;
9294 ctsio->kern_data_len = min(lun_datalen, alloc_len);
9295 ctsio->kern_total_len = ctsio->kern_data_len;
9296
9297 /*
9298 * We set this to the actual data length, regardless of how much
9299 * space we actually have to return results. If the user looks at
9300 * this value, he'll know whether or not he allocated enough space
9301 * and reissue the command if necessary. We don't support well
9302 * known logical units, so if the user asks for that, return none.
9303 */
9304 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9305
9306 /*
9307 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9308 * this request.
9309 */
9310 ctl_set_success(ctsio);
9311 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9312 ctsio->be_move_done = ctl_config_move_done;
9313 ctl_datamove((union ctl_io *)ctsio);
9314 return (retval);
9315 }
9316
9317 int
ctl_request_sense(struct ctl_scsiio * ctsio)9318 ctl_request_sense(struct ctl_scsiio *ctsio)
9319 {
9320 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9321 struct ctl_lun *lun = CTL_LUN(ctsio);
9322 struct scsi_request_sense *cdb;
9323 struct scsi_sense_data *sense_ptr, *ps;
9324 uint32_t initidx;
9325 int have_error;
9326 u_int sense_len = SSD_FULL_SIZE;
9327 scsi_sense_data_type sense_format;
9328 ctl_ua_type ua_type;
9329 uint8_t asc = 0, ascq = 0;
9330
9331 cdb = (struct scsi_request_sense *)ctsio->cdb;
9332
9333 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9334
9335 /*
9336 * Determine which sense format the user wants.
9337 */
9338 if (cdb->byte2 & SRS_DESC)
9339 sense_format = SSD_TYPE_DESC;
9340 else
9341 sense_format = SSD_TYPE_FIXED;
9342
9343 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9344 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9345 ctsio->kern_sg_entries = 0;
9346 ctsio->kern_rel_offset = 0;
9347 ctsio->kern_data_len = ctsio->kern_total_len =
9348 MIN(cdb->length, sizeof(*sense_ptr));
9349
9350 /*
9351 * If we don't have a LUN, we don't have any pending sense.
9352 */
9353 if (lun == NULL ||
9354 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
9355 softc->ha_link < CTL_HA_LINK_UNKNOWN)) {
9356 /* "Logical unit not supported" */
9357 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format,
9358 /*current_error*/ 1,
9359 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
9360 /*asc*/ 0x25,
9361 /*ascq*/ 0x00,
9362 SSD_ELEM_NONE);
9363 goto send;
9364 }
9365
9366 have_error = 0;
9367 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9368 /*
9369 * Check for pending sense, and then for pending unit attentions.
9370 * Pending sense gets returned first, then pending unit attentions.
9371 */
9372 mtx_lock(&lun->lun_lock);
9373 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
9374 if (ps != NULL)
9375 ps += initidx % CTL_MAX_INIT_PER_PORT;
9376 if (ps != NULL && ps->error_code != 0) {
9377 scsi_sense_data_type stored_format;
9378
9379 /*
9380 * Check to see which sense format was used for the stored
9381 * sense data.
9382 */
9383 stored_format = scsi_sense_type(ps);
9384
9385 /*
9386 * If the user requested a different sense format than the
9387 * one we stored, then we need to convert it to the other
9388 * format. If we're going from descriptor to fixed format
9389 * sense data, we may lose things in translation, depending
9390 * on what options were used.
9391 *
9392 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9393 * for some reason we'll just copy it out as-is.
9394 */
9395 if ((stored_format == SSD_TYPE_FIXED)
9396 && (sense_format == SSD_TYPE_DESC))
9397 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9398 ps, (struct scsi_sense_data_desc *)sense_ptr);
9399 else if ((stored_format == SSD_TYPE_DESC)
9400 && (sense_format == SSD_TYPE_FIXED))
9401 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9402 ps, (struct scsi_sense_data_fixed *)sense_ptr);
9403 else
9404 memcpy(sense_ptr, ps, sizeof(*sense_ptr));
9405
9406 ps->error_code = 0;
9407 have_error = 1;
9408 } else {
9409 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len,
9410 sense_format);
9411 if (ua_type != CTL_UA_NONE)
9412 have_error = 1;
9413 }
9414 if (have_error == 0) {
9415 /*
9416 * Report informational exception if have one and allowed.
9417 */
9418 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) {
9419 asc = lun->ie_asc;
9420 ascq = lun->ie_ascq;
9421 }
9422 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format,
9423 /*current_error*/ 1,
9424 /*sense_key*/ SSD_KEY_NO_SENSE,
9425 /*asc*/ asc,
9426 /*ascq*/ ascq,
9427 SSD_ELEM_NONE);
9428 }
9429 mtx_unlock(&lun->lun_lock);
9430
9431 send:
9432 /*
9433 * We report the SCSI status as OK, since the status of the command
9434 * itself is OK. We're reporting sense as parameter data.
9435 */
9436 ctl_set_success(ctsio);
9437 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9438 ctsio->be_move_done = ctl_config_move_done;
9439 ctl_datamove((union ctl_io *)ctsio);
9440 return (CTL_RETVAL_COMPLETE);
9441 }
9442
9443 int
ctl_tur(struct ctl_scsiio * ctsio)9444 ctl_tur(struct ctl_scsiio *ctsio)
9445 {
9446
9447 CTL_DEBUG_PRINT(("ctl_tur\n"));
9448
9449 ctl_set_success(ctsio);
9450 ctl_done((union ctl_io *)ctsio);
9451
9452 return (CTL_RETVAL_COMPLETE);
9453 }
9454
9455 /*
9456 * SCSI VPD page 0x00, the Supported VPD Pages page.
9457 */
9458 static int
ctl_inquiry_evpd_supported(struct ctl_scsiio * ctsio,int alloc_len)9459 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9460 {
9461 struct ctl_lun *lun = CTL_LUN(ctsio);
9462 struct scsi_vpd_supported_pages *pages;
9463 int sup_page_size;
9464 int p;
9465
9466 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9467 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9468 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9469 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9470 ctsio->kern_rel_offset = 0;
9471 ctsio->kern_sg_entries = 0;
9472 ctsio->kern_data_len = min(sup_page_size, alloc_len);
9473 ctsio->kern_total_len = ctsio->kern_data_len;
9474
9475 /*
9476 * The control device is always connected. The disk device, on the
9477 * other hand, may not be online all the time. Need to change this
9478 * to figure out whether the disk device is actually online or not.
9479 */
9480 if (lun != NULL)
9481 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9482 lun->be_lun->lun_type;
9483 else
9484 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9485
9486 p = 0;
9487 /* Supported VPD pages */
9488 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9489 /* Serial Number */
9490 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9491 /* Device Identification */
9492 pages->page_list[p++] = SVPD_DEVICE_ID;
9493 /* Extended INQUIRY Data */
9494 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9495 /* Mode Page Policy */
9496 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9497 /* SCSI Ports */
9498 pages->page_list[p++] = SVPD_SCSI_PORTS;
9499 /* Third-party Copy */
9500 pages->page_list[p++] = SVPD_SCSI_TPC;
9501 /* SCSI Feature Sets */
9502 pages->page_list[p++] = SVPD_SCSI_SFS;
9503 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9504 /* Block limits */
9505 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9506 /* Block Device Characteristics */
9507 pages->page_list[p++] = SVPD_BDC;
9508 /* Logical Block Provisioning */
9509 pages->page_list[p++] = SVPD_LBP;
9510 }
9511 pages->length = p;
9512
9513 ctl_set_success(ctsio);
9514 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9515 ctsio->be_move_done = ctl_config_move_done;
9516 ctl_datamove((union ctl_io *)ctsio);
9517 return (CTL_RETVAL_COMPLETE);
9518 }
9519
9520 /*
9521 * SCSI VPD page 0x80, the Unit Serial Number page.
9522 */
9523 static int
ctl_inquiry_evpd_serial(struct ctl_scsiio * ctsio,int alloc_len)9524 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9525 {
9526 struct ctl_lun *lun = CTL_LUN(ctsio);
9527 struct scsi_vpd_unit_serial_number *sn_ptr;
9528 int data_len;
9529
9530 data_len = 4 + CTL_SN_LEN;
9531 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9532 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9533 ctsio->kern_rel_offset = 0;
9534 ctsio->kern_sg_entries = 0;
9535 ctsio->kern_data_len = min(data_len, alloc_len);
9536 ctsio->kern_total_len = ctsio->kern_data_len;
9537
9538 /*
9539 * The control device is always connected. The disk device, on the
9540 * other hand, may not be online all the time. Need to change this
9541 * to figure out whether the disk device is actually online or not.
9542 */
9543 if (lun != NULL)
9544 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9545 lun->be_lun->lun_type;
9546 else
9547 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9548
9549 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9550 sn_ptr->length = CTL_SN_LEN;
9551 /*
9552 * If we don't have a LUN, we just leave the serial number as
9553 * all spaces.
9554 */
9555 if (lun != NULL) {
9556 strncpy((char *)sn_ptr->serial_num,
9557 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9558 } else
9559 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9560
9561 ctl_set_success(ctsio);
9562 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9563 ctsio->be_move_done = ctl_config_move_done;
9564 ctl_datamove((union ctl_io *)ctsio);
9565 return (CTL_RETVAL_COMPLETE);
9566 }
9567
9568 /*
9569 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9570 */
9571 static int
ctl_inquiry_evpd_eid(struct ctl_scsiio * ctsio,int alloc_len)9572 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9573 {
9574 struct ctl_lun *lun = CTL_LUN(ctsio);
9575 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9576 int data_len;
9577
9578 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9579 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9580 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9581 ctsio->kern_sg_entries = 0;
9582 ctsio->kern_rel_offset = 0;
9583 ctsio->kern_data_len = min(data_len, alloc_len);
9584 ctsio->kern_total_len = ctsio->kern_data_len;
9585
9586 /*
9587 * The control device is always connected. The disk device, on the
9588 * other hand, may not be online all the time.
9589 */
9590 if (lun != NULL)
9591 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9592 lun->be_lun->lun_type;
9593 else
9594 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9595 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9596 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9597 /*
9598 * We support head of queue, ordered and simple tags.
9599 */
9600 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9601 /*
9602 * Volatile cache supported.
9603 */
9604 eid_ptr->flags3 = SVPD_EID_V_SUP;
9605
9606 /*
9607 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9608 * attention for a particular IT nexus on all LUNs once we report
9609 * it to that nexus once. This bit is required as of SPC-4.
9610 */
9611 eid_ptr->flags4 = SVPD_EID_LUICLR;
9612
9613 /*
9614 * We support revert to defaults (RTD) bit in MODE SELECT.
9615 */
9616 eid_ptr->flags5 = SVPD_EID_RTD_SUP;
9617
9618 /*
9619 * XXX KDM in order to correctly answer this, we would need
9620 * information from the SIM to determine how much sense data it
9621 * can send. So this would really be a path inquiry field, most
9622 * likely. This can be set to a maximum of 252 according to SPC-4,
9623 * but the hardware may or may not be able to support that much.
9624 * 0 just means that the maximum sense data length is not reported.
9625 */
9626 eid_ptr->max_sense_length = 0;
9627
9628 ctl_set_success(ctsio);
9629 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9630 ctsio->be_move_done = ctl_config_move_done;
9631 ctl_datamove((union ctl_io *)ctsio);
9632 return (CTL_RETVAL_COMPLETE);
9633 }
9634
9635 static int
ctl_inquiry_evpd_mpp(struct ctl_scsiio * ctsio,int alloc_len)9636 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9637 {
9638 struct ctl_lun *lun = CTL_LUN(ctsio);
9639 struct scsi_vpd_mode_page_policy *mpp_ptr;
9640 int data_len;
9641
9642 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9643 sizeof(struct scsi_vpd_mode_page_policy_descr);
9644
9645 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9646 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9647 ctsio->kern_rel_offset = 0;
9648 ctsio->kern_sg_entries = 0;
9649 ctsio->kern_data_len = min(data_len, alloc_len);
9650 ctsio->kern_total_len = ctsio->kern_data_len;
9651
9652 /*
9653 * The control device is always connected. The disk device, on the
9654 * other hand, may not be online all the time.
9655 */
9656 if (lun != NULL)
9657 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9658 lun->be_lun->lun_type;
9659 else
9660 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9661 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9662 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9663 mpp_ptr->descr[0].page_code = 0x3f;
9664 mpp_ptr->descr[0].subpage_code = 0xff;
9665 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9666
9667 ctl_set_success(ctsio);
9668 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9669 ctsio->be_move_done = ctl_config_move_done;
9670 ctl_datamove((union ctl_io *)ctsio);
9671 return (CTL_RETVAL_COMPLETE);
9672 }
9673
9674 /*
9675 * SCSI VPD page 0x83, the Device Identification page.
9676 */
9677 static int
ctl_inquiry_evpd_devid(struct ctl_scsiio * ctsio,int alloc_len)9678 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9679 {
9680 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9681 struct ctl_port *port = CTL_PORT(ctsio);
9682 struct ctl_lun *lun = CTL_LUN(ctsio);
9683 struct scsi_vpd_device_id *devid_ptr;
9684 struct scsi_vpd_id_descriptor *desc;
9685 int data_len, g;
9686 uint8_t proto;
9687
9688 data_len = sizeof(struct scsi_vpd_device_id) +
9689 sizeof(struct scsi_vpd_id_descriptor) +
9690 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9691 sizeof(struct scsi_vpd_id_descriptor) +
9692 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9693 if (lun && lun->lun_devid)
9694 data_len += lun->lun_devid->len;
9695 if (port && port->port_devid)
9696 data_len += port->port_devid->len;
9697 if (port && port->target_devid)
9698 data_len += port->target_devid->len;
9699
9700 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9701 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9702 ctsio->kern_sg_entries = 0;
9703 ctsio->kern_rel_offset = 0;
9704 ctsio->kern_sg_entries = 0;
9705 ctsio->kern_data_len = min(data_len, alloc_len);
9706 ctsio->kern_total_len = ctsio->kern_data_len;
9707
9708 /*
9709 * The control device is always connected. The disk device, on the
9710 * other hand, may not be online all the time.
9711 */
9712 if (lun != NULL)
9713 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9714 lun->be_lun->lun_type;
9715 else
9716 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9717 devid_ptr->page_code = SVPD_DEVICE_ID;
9718 scsi_ulto2b(data_len - 4, devid_ptr->length);
9719
9720 if (port && port->port_type == CTL_PORT_FC)
9721 proto = SCSI_PROTO_FC << 4;
9722 else if (port && port->port_type == CTL_PORT_SAS)
9723 proto = SCSI_PROTO_SAS << 4;
9724 else if (port && port->port_type == CTL_PORT_ISCSI)
9725 proto = SCSI_PROTO_ISCSI << 4;
9726 else
9727 proto = SCSI_PROTO_SPI << 4;
9728 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9729
9730 /*
9731 * We're using a LUN association here. i.e., this device ID is a
9732 * per-LUN identifier.
9733 */
9734 if (lun && lun->lun_devid) {
9735 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9736 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9737 lun->lun_devid->len);
9738 }
9739
9740 /*
9741 * This is for the WWPN which is a port association.
9742 */
9743 if (port && port->port_devid) {
9744 memcpy(desc, port->port_devid->data, port->port_devid->len);
9745 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9746 port->port_devid->len);
9747 }
9748
9749 /*
9750 * This is for the Relative Target Port(type 4h) identifier
9751 */
9752 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9753 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9754 SVPD_ID_TYPE_RELTARG;
9755 desc->length = 4;
9756 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9757 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9758 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9759
9760 /*
9761 * This is for the Target Port Group(type 5h) identifier
9762 */
9763 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9764 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9765 SVPD_ID_TYPE_TPORTGRP;
9766 desc->length = 4;
9767 if (softc->is_single ||
9768 (port && port->status & CTL_PORT_STATUS_HA_SHARED))
9769 g = 1;
9770 else
9771 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt;
9772 scsi_ulto2b(g, &desc->identifier[2]);
9773 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9774 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9775
9776 /*
9777 * This is for the Target identifier
9778 */
9779 if (port && port->target_devid) {
9780 memcpy(desc, port->target_devid->data, port->target_devid->len);
9781 }
9782
9783 ctl_set_success(ctsio);
9784 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9785 ctsio->be_move_done = ctl_config_move_done;
9786 ctl_datamove((union ctl_io *)ctsio);
9787 return (CTL_RETVAL_COMPLETE);
9788 }
9789
9790 static int
ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio * ctsio,int alloc_len)9791 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9792 {
9793 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9794 struct ctl_lun *lun = CTL_LUN(ctsio);
9795 struct scsi_vpd_scsi_ports *sp;
9796 struct scsi_vpd_port_designation *pd;
9797 struct scsi_vpd_port_designation_cont *pdc;
9798 struct ctl_port *port;
9799 int data_len, num_target_ports, iid_len, id_len;
9800
9801 num_target_ports = 0;
9802 iid_len = 0;
9803 id_len = 0;
9804 mtx_lock(&softc->ctl_lock);
9805 STAILQ_FOREACH(port, &softc->port_list, links) {
9806 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9807 continue;
9808 if (lun != NULL &&
9809 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9810 continue;
9811 num_target_ports++;
9812 if (port->init_devid)
9813 iid_len += port->init_devid->len;
9814 if (port->port_devid)
9815 id_len += port->port_devid->len;
9816 }
9817 mtx_unlock(&softc->ctl_lock);
9818
9819 data_len = sizeof(struct scsi_vpd_scsi_ports) +
9820 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9821 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9822 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9823 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9824 ctsio->kern_sg_entries = 0;
9825 ctsio->kern_rel_offset = 0;
9826 ctsio->kern_sg_entries = 0;
9827 ctsio->kern_data_len = min(data_len, alloc_len);
9828 ctsio->kern_total_len = ctsio->kern_data_len;
9829
9830 /*
9831 * The control device is always connected. The disk device, on the
9832 * other hand, may not be online all the time. Need to change this
9833 * to figure out whether the disk device is actually online or not.
9834 */
9835 if (lun != NULL)
9836 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9837 lun->be_lun->lun_type;
9838 else
9839 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9840
9841 sp->page_code = SVPD_SCSI_PORTS;
9842 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9843 sp->page_length);
9844 pd = &sp->design[0];
9845
9846 mtx_lock(&softc->ctl_lock);
9847 STAILQ_FOREACH(port, &softc->port_list, links) {
9848 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9849 continue;
9850 if (lun != NULL &&
9851 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9852 continue;
9853 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9854 if (port->init_devid) {
9855 iid_len = port->init_devid->len;
9856 memcpy(pd->initiator_transportid,
9857 port->init_devid->data, port->init_devid->len);
9858 } else
9859 iid_len = 0;
9860 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9861 pdc = (struct scsi_vpd_port_designation_cont *)
9862 (&pd->initiator_transportid[iid_len]);
9863 if (port->port_devid) {
9864 id_len = port->port_devid->len;
9865 memcpy(pdc->target_port_descriptors,
9866 port->port_devid->data, port->port_devid->len);
9867 } else
9868 id_len = 0;
9869 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9870 pd = (struct scsi_vpd_port_designation *)
9871 ((uint8_t *)pdc->target_port_descriptors + id_len);
9872 }
9873 mtx_unlock(&softc->ctl_lock);
9874
9875 ctl_set_success(ctsio);
9876 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9877 ctsio->be_move_done = ctl_config_move_done;
9878 ctl_datamove((union ctl_io *)ctsio);
9879 return (CTL_RETVAL_COMPLETE);
9880 }
9881
9882 static int
ctl_inquiry_evpd_sfs(struct ctl_scsiio * ctsio,int alloc_len)9883 ctl_inquiry_evpd_sfs(struct ctl_scsiio *ctsio, int alloc_len)
9884 {
9885 struct ctl_lun *lun = CTL_LUN(ctsio);
9886 struct scsi_vpd_sfs *sfs_ptr;
9887 int sfs_page_size, n;
9888
9889 sfs_page_size = sizeof(*sfs_ptr) + 5 * 2;
9890 ctsio->kern_data_ptr = malloc(sfs_page_size, M_CTL, M_WAITOK | M_ZERO);
9891 sfs_ptr = (struct scsi_vpd_sfs *)ctsio->kern_data_ptr;
9892 ctsio->kern_sg_entries = 0;
9893 ctsio->kern_rel_offset = 0;
9894 ctsio->kern_sg_entries = 0;
9895 ctsio->kern_data_len = min(sfs_page_size, alloc_len);
9896 ctsio->kern_total_len = ctsio->kern_data_len;
9897
9898 /*
9899 * The control device is always connected. The disk device, on the
9900 * other hand, may not be online all the time. Need to change this
9901 * to figure out whether the disk device is actually online or not.
9902 */
9903 if (lun != NULL)
9904 sfs_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9905 lun->be_lun->lun_type;
9906 else
9907 sfs_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9908
9909 sfs_ptr->page_code = SVPD_SCSI_SFS;
9910 n = 0;
9911 /* Discovery 2016 */
9912 scsi_ulto2b(0x0001, &sfs_ptr->codes[2 * n++]);
9913 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9914 /* SBC Base 2016 */
9915 scsi_ulto2b(0x0101, &sfs_ptr->codes[2 * n++]);
9916 /* SBC Base 2010 */
9917 scsi_ulto2b(0x0102, &sfs_ptr->codes[2 * n++]);
9918 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9919 /* Basic Provisioning 2016 */
9920 scsi_ulto2b(0x0103, &sfs_ptr->codes[2 * n++]);
9921 }
9922 /* Drive Maintenance 2016 */
9923 //scsi_ulto2b(0x0104, &sfs_ptr->codes[2 * n++]);
9924 }
9925 scsi_ulto2b(4 + 2 * n, sfs_ptr->page_length);
9926
9927 ctl_set_success(ctsio);
9928 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9929 ctsio->be_move_done = ctl_config_move_done;
9930 ctl_datamove((union ctl_io *)ctsio);
9931 return (CTL_RETVAL_COMPLETE);
9932 }
9933
9934 static int
ctl_inquiry_evpd_block_limits(struct ctl_scsiio * ctsio,int alloc_len)9935 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9936 {
9937 struct ctl_lun *lun = CTL_LUN(ctsio);
9938 struct scsi_vpd_block_limits *bl_ptr;
9939 const char *val;
9940 uint64_t ival;
9941
9942 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9943 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9944 ctsio->kern_sg_entries = 0;
9945 ctsio->kern_rel_offset = 0;
9946 ctsio->kern_sg_entries = 0;
9947 ctsio->kern_data_len = min(sizeof(*bl_ptr), alloc_len);
9948 ctsio->kern_total_len = ctsio->kern_data_len;
9949
9950 /*
9951 * The control device is always connected. The disk device, on the
9952 * other hand, may not be online all the time. Need to change this
9953 * to figure out whether the disk device is actually online or not.
9954 */
9955 if (lun != NULL)
9956 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9957 lun->be_lun->lun_type;
9958 else
9959 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9960
9961 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9962 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9963 bl_ptr->max_cmp_write_len = 0xff;
9964 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9965 if (lun != NULL) {
9966 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9967 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9968 ival = 0xffffffff;
9969 val = dnvlist_get_string(lun->be_lun->options,
9970 "unmap_max_lba", NULL);
9971 if (val != NULL)
9972 ctl_expand_number(val, &ival);
9973 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt);
9974 ival = 0xffffffff;
9975 val = dnvlist_get_string(lun->be_lun->options,
9976 "unmap_max_descr", NULL);
9977 if (val != NULL)
9978 ctl_expand_number(val, &ival);
9979 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt);
9980 if (lun->be_lun->ublockexp != 0) {
9981 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9982 bl_ptr->opt_unmap_grain);
9983 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9984 bl_ptr->unmap_grain_align);
9985 }
9986 }
9987 scsi_ulto4b(lun->be_lun->atomicblock,
9988 bl_ptr->max_atomic_transfer_length);
9989 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9990 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9991 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary);
9992 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size);
9993 ival = UINT64_MAX;
9994 val = dnvlist_get_string(lun->be_lun->options,
9995 "write_same_max_lba", NULL);
9996 if (val != NULL)
9997 ctl_expand_number(val, &ival);
9998 scsi_u64to8b(ival, bl_ptr->max_write_same_length);
9999 if (lun->be_lun->maxlba + 1 > ival)
10000 bl_ptr->flags |= SVPD_BL_WSNZ;
10001 }
10002
10003 ctl_set_success(ctsio);
10004 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10005 ctsio->be_move_done = ctl_config_move_done;
10006 ctl_datamove((union ctl_io *)ctsio);
10007 return (CTL_RETVAL_COMPLETE);
10008 }
10009
10010 static int
ctl_inquiry_evpd_bdc(struct ctl_scsiio * ctsio,int alloc_len)10011 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
10012 {
10013 struct ctl_lun *lun = CTL_LUN(ctsio);
10014 struct scsi_vpd_block_device_characteristics *bdc_ptr;
10015 const char *value;
10016 u_int i;
10017
10018 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
10019 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
10020 ctsio->kern_sg_entries = 0;
10021 ctsio->kern_rel_offset = 0;
10022 ctsio->kern_data_len = min(sizeof(*bdc_ptr), alloc_len);
10023 ctsio->kern_total_len = ctsio->kern_data_len;
10024
10025 /*
10026 * The control device is always connected. The disk device, on the
10027 * other hand, may not be online all the time. Need to change this
10028 * to figure out whether the disk device is actually online or not.
10029 */
10030 if (lun != NULL)
10031 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10032 lun->be_lun->lun_type;
10033 else
10034 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10035 bdc_ptr->page_code = SVPD_BDC;
10036 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
10037 if (lun != NULL &&
10038 (value = dnvlist_get_string(lun->be_lun->options, "rpm", NULL)) != NULL)
10039 i = strtol(value, NULL, 0);
10040 else
10041 i = CTL_DEFAULT_ROTATION_RATE;
10042 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
10043 if (lun != NULL &&
10044 (value = dnvlist_get_string(lun->be_lun->options, "formfactor", NULL)) != NULL)
10045 i = strtol(value, NULL, 0);
10046 else
10047 i = 0;
10048 bdc_ptr->wab_wac_ff = (i & 0x0f);
10049 bdc_ptr->flags = SVPD_RBWZ | SVPD_FUAB | SVPD_VBULS;
10050
10051 ctl_set_success(ctsio);
10052 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10053 ctsio->be_move_done = ctl_config_move_done;
10054 ctl_datamove((union ctl_io *)ctsio);
10055 return (CTL_RETVAL_COMPLETE);
10056 }
10057
10058 static int
ctl_inquiry_evpd_lbp(struct ctl_scsiio * ctsio,int alloc_len)10059 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
10060 {
10061 struct ctl_lun *lun = CTL_LUN(ctsio);
10062 struct scsi_vpd_logical_block_prov *lbp_ptr;
10063 const char *value;
10064
10065 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
10066 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
10067 ctsio->kern_sg_entries = 0;
10068 ctsio->kern_rel_offset = 0;
10069 ctsio->kern_data_len = min(sizeof(*lbp_ptr), alloc_len);
10070 ctsio->kern_total_len = ctsio->kern_data_len;
10071
10072 /*
10073 * The control device is always connected. The disk device, on the
10074 * other hand, may not be online all the time. Need to change this
10075 * to figure out whether the disk device is actually online or not.
10076 */
10077 if (lun != NULL)
10078 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10079 lun->be_lun->lun_type;
10080 else
10081 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10082
10083 lbp_ptr->page_code = SVPD_LBP;
10084 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
10085 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
10086 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10087 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10088 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10089 value = dnvlist_get_string(lun->be_lun->options,
10090 "provisioning_type", NULL);
10091 if (value != NULL) {
10092 if (strcmp(value, "resource") == 0)
10093 lbp_ptr->prov_type = SVPD_LBP_RESOURCE;
10094 else if (strcmp(value, "thin") == 0)
10095 lbp_ptr->prov_type = SVPD_LBP_THIN;
10096 } else
10097 lbp_ptr->prov_type = SVPD_LBP_THIN;
10098 }
10099
10100 ctl_set_success(ctsio);
10101 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10102 ctsio->be_move_done = ctl_config_move_done;
10103 ctl_datamove((union ctl_io *)ctsio);
10104 return (CTL_RETVAL_COMPLETE);
10105 }
10106
10107 /*
10108 * INQUIRY with the EVPD bit set.
10109 */
10110 static int
ctl_inquiry_evpd(struct ctl_scsiio * ctsio)10111 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10112 {
10113 struct ctl_lun *lun = CTL_LUN(ctsio);
10114 struct scsi_inquiry *cdb;
10115 int alloc_len, retval;
10116
10117 cdb = (struct scsi_inquiry *)ctsio->cdb;
10118 alloc_len = scsi_2btoul(cdb->length);
10119
10120 switch (cdb->page_code) {
10121 case SVPD_SUPPORTED_PAGES:
10122 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10123 break;
10124 case SVPD_UNIT_SERIAL_NUMBER:
10125 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10126 break;
10127 case SVPD_DEVICE_ID:
10128 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10129 break;
10130 case SVPD_EXTENDED_INQUIRY_DATA:
10131 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10132 break;
10133 case SVPD_MODE_PAGE_POLICY:
10134 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10135 break;
10136 case SVPD_SCSI_PORTS:
10137 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10138 break;
10139 case SVPD_SCSI_TPC:
10140 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10141 break;
10142 case SVPD_SCSI_SFS:
10143 retval = ctl_inquiry_evpd_sfs(ctsio, alloc_len);
10144 break;
10145 case SVPD_BLOCK_LIMITS:
10146 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10147 goto err;
10148 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10149 break;
10150 case SVPD_BDC:
10151 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10152 goto err;
10153 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10154 break;
10155 case SVPD_LBP:
10156 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10157 goto err;
10158 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10159 break;
10160 default:
10161 err:
10162 ctl_set_invalid_field(ctsio,
10163 /*sks_valid*/ 1,
10164 /*command*/ 1,
10165 /*field*/ 2,
10166 /*bit_valid*/ 0,
10167 /*bit*/ 0);
10168 ctl_done((union ctl_io *)ctsio);
10169 retval = CTL_RETVAL_COMPLETE;
10170 break;
10171 }
10172
10173 return (retval);
10174 }
10175
10176 /*
10177 * Standard INQUIRY data.
10178 */
10179 static int
ctl_inquiry_std(struct ctl_scsiio * ctsio)10180 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10181 {
10182 struct ctl_softc *softc = CTL_SOFTC(ctsio);
10183 struct ctl_port *port = CTL_PORT(ctsio);
10184 struct ctl_lun *lun = CTL_LUN(ctsio);
10185 struct scsi_inquiry_data *inq_ptr;
10186 struct scsi_inquiry *cdb;
10187 const char *val;
10188 uint32_t alloc_len, data_len;
10189 ctl_port_type port_type;
10190
10191 port_type = port->port_type;
10192 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10193 port_type = CTL_PORT_SCSI;
10194
10195 cdb = (struct scsi_inquiry *)ctsio->cdb;
10196 alloc_len = scsi_2btoul(cdb->length);
10197
10198 /*
10199 * We malloc the full inquiry data size here and fill it
10200 * in. If the user only asks for less, we'll give him
10201 * that much.
10202 */
10203 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10204 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10205 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10206 ctsio->kern_sg_entries = 0;
10207 ctsio->kern_rel_offset = 0;
10208 ctsio->kern_data_len = min(data_len, alloc_len);
10209 ctsio->kern_total_len = ctsio->kern_data_len;
10210
10211 if (lun != NULL) {
10212 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
10213 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
10214 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10215 lun->be_lun->lun_type;
10216 } else {
10217 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
10218 lun->be_lun->lun_type;
10219 }
10220 if (lun->flags & CTL_LUN_REMOVABLE)
10221 inq_ptr->dev_qual2 |= SID_RMB;
10222 } else
10223 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10224
10225 /* RMB in byte 2 is 0 */
10226 inq_ptr->version = SCSI_REV_SPC5;
10227
10228 /*
10229 * According to SAM-3, even if a device only supports a single
10230 * level of LUN addressing, it should still set the HISUP bit:
10231 *
10232 * 4.9.1 Logical unit numbers overview
10233 *
10234 * All logical unit number formats described in this standard are
10235 * hierarchical in structure even when only a single level in that
10236 * hierarchy is used. The HISUP bit shall be set to one in the
10237 * standard INQUIRY data (see SPC-2) when any logical unit number
10238 * format described in this standard is used. Non-hierarchical
10239 * formats are outside the scope of this standard.
10240 *
10241 * Therefore we set the HiSup bit here.
10242 *
10243 * The response format is 2, per SPC-3.
10244 */
10245 inq_ptr->response_format = SID_HiSup | 2;
10246
10247 inq_ptr->additional_length = data_len -
10248 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10249 CTL_DEBUG_PRINT(("additional_length = %d\n",
10250 inq_ptr->additional_length));
10251
10252 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10253 if (port_type == CTL_PORT_SCSI)
10254 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10255 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10256 inq_ptr->flags = SID_CmdQue;
10257 if (port_type == CTL_PORT_SCSI)
10258 inq_ptr->flags |= SID_WBus16 | SID_Sync;
10259
10260 /*
10261 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10262 * We have 8 bytes for the vendor name, and 16 bytes for the device
10263 * name and 4 bytes for the revision.
10264 */
10265 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options,
10266 "vendor", NULL)) == NULL) {
10267 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10268 } else {
10269 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10270 strncpy(inq_ptr->vendor, val,
10271 min(sizeof(inq_ptr->vendor), strlen(val)));
10272 }
10273 if (lun == NULL) {
10274 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10275 sizeof(inq_ptr->product));
10276 } else if ((val = dnvlist_get_string(lun->be_lun->options, "product",
10277 NULL)) == NULL) {
10278 switch (lun->be_lun->lun_type) {
10279 case T_DIRECT:
10280 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10281 sizeof(inq_ptr->product));
10282 break;
10283 case T_PROCESSOR:
10284 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10285 sizeof(inq_ptr->product));
10286 break;
10287 case T_CDROM:
10288 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT,
10289 sizeof(inq_ptr->product));
10290 break;
10291 default:
10292 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10293 sizeof(inq_ptr->product));
10294 break;
10295 }
10296 } else {
10297 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10298 strncpy(inq_ptr->product, val,
10299 min(sizeof(inq_ptr->product), strlen(val)));
10300 }
10301
10302 /*
10303 * XXX make this a macro somewhere so it automatically gets
10304 * incremented when we make changes.
10305 */
10306 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options,
10307 "revision", NULL)) == NULL) {
10308 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10309 } else {
10310 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10311 strncpy(inq_ptr->revision, val,
10312 min(sizeof(inq_ptr->revision), strlen(val)));
10313 }
10314
10315 /*
10316 * For parallel SCSI, we support double transition and single
10317 * transition clocking. We also support QAS (Quick Arbitration
10318 * and Selection) and Information Unit transfers on both the
10319 * control and array devices.
10320 */
10321 if (port_type == CTL_PORT_SCSI)
10322 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10323 SID_SPI_IUS;
10324
10325 /* SAM-6 (no version claimed) */
10326 scsi_ulto2b(0x00C0, inq_ptr->version1);
10327 /* SPC-5 (no version claimed) */
10328 scsi_ulto2b(0x05C0, inq_ptr->version2);
10329 if (port_type == CTL_PORT_FC) {
10330 /* FCP-2 ANSI INCITS.350:2003 */
10331 scsi_ulto2b(0x0917, inq_ptr->version3);
10332 } else if (port_type == CTL_PORT_SCSI) {
10333 /* SPI-4 ANSI INCITS.362:200x */
10334 scsi_ulto2b(0x0B56, inq_ptr->version3);
10335 } else if (port_type == CTL_PORT_ISCSI) {
10336 /* iSCSI (no version claimed) */
10337 scsi_ulto2b(0x0960, inq_ptr->version3);
10338 } else if (port_type == CTL_PORT_SAS) {
10339 /* SAS (no version claimed) */
10340 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10341 } else if (port_type == CTL_PORT_UMASS) {
10342 /* USB Mass Storage Class Bulk-Only Transport, Revision 1.0 */
10343 scsi_ulto2b(0x1730, inq_ptr->version3);
10344 }
10345
10346 if (lun == NULL) {
10347 /* SBC-4 (no version claimed) */
10348 scsi_ulto2b(0x0600, inq_ptr->version4);
10349 } else {
10350 switch (lun->be_lun->lun_type) {
10351 case T_DIRECT:
10352 /* SBC-4 (no version claimed) */
10353 scsi_ulto2b(0x0600, inq_ptr->version4);
10354 break;
10355 case T_PROCESSOR:
10356 break;
10357 case T_CDROM:
10358 /* MMC-6 (no version claimed) */
10359 scsi_ulto2b(0x04E0, inq_ptr->version4);
10360 break;
10361 default:
10362 break;
10363 }
10364 }
10365
10366 ctl_set_success(ctsio);
10367 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10368 ctsio->be_move_done = ctl_config_move_done;
10369 ctl_datamove((union ctl_io *)ctsio);
10370 return (CTL_RETVAL_COMPLETE);
10371 }
10372
10373 int
ctl_inquiry(struct ctl_scsiio * ctsio)10374 ctl_inquiry(struct ctl_scsiio *ctsio)
10375 {
10376 struct scsi_inquiry *cdb;
10377 int retval;
10378
10379 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10380
10381 cdb = (struct scsi_inquiry *)ctsio->cdb;
10382 if (cdb->byte2 & SI_EVPD)
10383 retval = ctl_inquiry_evpd(ctsio);
10384 else if (cdb->page_code == 0)
10385 retval = ctl_inquiry_std(ctsio);
10386 else {
10387 ctl_set_invalid_field(ctsio,
10388 /*sks_valid*/ 1,
10389 /*command*/ 1,
10390 /*field*/ 2,
10391 /*bit_valid*/ 0,
10392 /*bit*/ 0);
10393 ctl_done((union ctl_io *)ctsio);
10394 return (CTL_RETVAL_COMPLETE);
10395 }
10396
10397 return (retval);
10398 }
10399
10400 int
ctl_get_config(struct ctl_scsiio * ctsio)10401 ctl_get_config(struct ctl_scsiio *ctsio)
10402 {
10403 struct ctl_lun *lun = CTL_LUN(ctsio);
10404 struct scsi_get_config_header *hdr;
10405 struct scsi_get_config_feature *feature;
10406 struct scsi_get_config *cdb;
10407 uint32_t alloc_len, data_len;
10408 int rt, starting;
10409
10410 cdb = (struct scsi_get_config *)ctsio->cdb;
10411 rt = (cdb->rt & SGC_RT_MASK);
10412 starting = scsi_2btoul(cdb->starting_feature);
10413 alloc_len = scsi_2btoul(cdb->length);
10414
10415 data_len = sizeof(struct scsi_get_config_header) +
10416 sizeof(struct scsi_get_config_feature) + 8 +
10417 sizeof(struct scsi_get_config_feature) + 8 +
10418 sizeof(struct scsi_get_config_feature) + 4 +
10419 sizeof(struct scsi_get_config_feature) + 4 +
10420 sizeof(struct scsi_get_config_feature) + 8 +
10421 sizeof(struct scsi_get_config_feature) +
10422 sizeof(struct scsi_get_config_feature) + 4 +
10423 sizeof(struct scsi_get_config_feature) + 4 +
10424 sizeof(struct scsi_get_config_feature) + 4 +
10425 sizeof(struct scsi_get_config_feature) + 4 +
10426 sizeof(struct scsi_get_config_feature) + 4 +
10427 sizeof(struct scsi_get_config_feature) + 4;
10428 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10429 ctsio->kern_sg_entries = 0;
10430 ctsio->kern_rel_offset = 0;
10431
10432 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr;
10433 if (lun->flags & CTL_LUN_NO_MEDIA)
10434 scsi_ulto2b(0x0000, hdr->current_profile);
10435 else
10436 scsi_ulto2b(0x0010, hdr->current_profile);
10437 feature = (struct scsi_get_config_feature *)(hdr + 1);
10438
10439 if (starting > 0x003b)
10440 goto done;
10441 if (starting > 0x003a)
10442 goto f3b;
10443 if (starting > 0x002b)
10444 goto f3a;
10445 if (starting > 0x002a)
10446 goto f2b;
10447 if (starting > 0x001f)
10448 goto f2a;
10449 if (starting > 0x001e)
10450 goto f1f;
10451 if (starting > 0x001d)
10452 goto f1e;
10453 if (starting > 0x0010)
10454 goto f1d;
10455 if (starting > 0x0003)
10456 goto f10;
10457 if (starting > 0x0002)
10458 goto f3;
10459 if (starting > 0x0001)
10460 goto f2;
10461 if (starting > 0x0000)
10462 goto f1;
10463
10464 /* Profile List */
10465 scsi_ulto2b(0x0000, feature->feature_code);
10466 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT;
10467 feature->add_length = 8;
10468 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */
10469 feature->feature_data[2] = 0x00;
10470 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */
10471 feature->feature_data[6] = 0x01;
10472 feature = (struct scsi_get_config_feature *)
10473 &feature->feature_data[feature->add_length];
10474
10475 f1: /* Core */
10476 scsi_ulto2b(0x0001, feature->feature_code);
10477 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10478 feature->add_length = 8;
10479 scsi_ulto4b(0x00000000, &feature->feature_data[0]);
10480 feature->feature_data[4] = 0x03;
10481 feature = (struct scsi_get_config_feature *)
10482 &feature->feature_data[feature->add_length];
10483
10484 f2: /* Morphing */
10485 scsi_ulto2b(0x0002, feature->feature_code);
10486 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10487 feature->add_length = 4;
10488 feature->feature_data[0] = 0x02;
10489 feature = (struct scsi_get_config_feature *)
10490 &feature->feature_data[feature->add_length];
10491
10492 f3: /* Removable Medium */
10493 scsi_ulto2b(0x0003, feature->feature_code);
10494 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10495 feature->add_length = 4;
10496 feature->feature_data[0] = 0x39;
10497 feature = (struct scsi_get_config_feature *)
10498 &feature->feature_data[feature->add_length];
10499
10500 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA))
10501 goto done;
10502
10503 f10: /* Random Read */
10504 scsi_ulto2b(0x0010, feature->feature_code);
10505 feature->flags = 0x00;
10506 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10507 feature->flags |= SGC_F_CURRENT;
10508 feature->add_length = 8;
10509 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]);
10510 scsi_ulto2b(1, &feature->feature_data[4]);
10511 feature->feature_data[6] = 0x00;
10512 feature = (struct scsi_get_config_feature *)
10513 &feature->feature_data[feature->add_length];
10514
10515 f1d: /* Multi-Read */
10516 scsi_ulto2b(0x001D, feature->feature_code);
10517 feature->flags = 0x00;
10518 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10519 feature->flags |= SGC_F_CURRENT;
10520 feature->add_length = 0;
10521 feature = (struct scsi_get_config_feature *)
10522 &feature->feature_data[feature->add_length];
10523
10524 f1e: /* CD Read */
10525 scsi_ulto2b(0x001E, feature->feature_code);
10526 feature->flags = 0x00;
10527 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10528 feature->flags |= SGC_F_CURRENT;
10529 feature->add_length = 4;
10530 feature->feature_data[0] = 0x00;
10531 feature = (struct scsi_get_config_feature *)
10532 &feature->feature_data[feature->add_length];
10533
10534 f1f: /* DVD Read */
10535 scsi_ulto2b(0x001F, feature->feature_code);
10536 feature->flags = 0x08;
10537 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10538 feature->flags |= SGC_F_CURRENT;
10539 feature->add_length = 4;
10540 feature->feature_data[0] = 0x01;
10541 feature->feature_data[2] = 0x03;
10542 feature = (struct scsi_get_config_feature *)
10543 &feature->feature_data[feature->add_length];
10544
10545 f2a: /* DVD+RW */
10546 scsi_ulto2b(0x002A, feature->feature_code);
10547 feature->flags = 0x04;
10548 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10549 feature->flags |= SGC_F_CURRENT;
10550 feature->add_length = 4;
10551 feature->feature_data[0] = 0x00;
10552 feature->feature_data[1] = 0x00;
10553 feature = (struct scsi_get_config_feature *)
10554 &feature->feature_data[feature->add_length];
10555
10556 f2b: /* DVD+R */
10557 scsi_ulto2b(0x002B, feature->feature_code);
10558 feature->flags = 0x00;
10559 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10560 feature->flags |= SGC_F_CURRENT;
10561 feature->add_length = 4;
10562 feature->feature_data[0] = 0x00;
10563 feature = (struct scsi_get_config_feature *)
10564 &feature->feature_data[feature->add_length];
10565
10566 f3a: /* DVD+RW Dual Layer */
10567 scsi_ulto2b(0x003A, feature->feature_code);
10568 feature->flags = 0x00;
10569 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10570 feature->flags |= SGC_F_CURRENT;
10571 feature->add_length = 4;
10572 feature->feature_data[0] = 0x00;
10573 feature->feature_data[1] = 0x00;
10574 feature = (struct scsi_get_config_feature *)
10575 &feature->feature_data[feature->add_length];
10576
10577 f3b: /* DVD+R Dual Layer */
10578 scsi_ulto2b(0x003B, feature->feature_code);
10579 feature->flags = 0x00;
10580 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10581 feature->flags |= SGC_F_CURRENT;
10582 feature->add_length = 4;
10583 feature->feature_data[0] = 0x00;
10584 feature = (struct scsi_get_config_feature *)
10585 &feature->feature_data[feature->add_length];
10586
10587 done:
10588 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10589 if (rt == SGC_RT_SPECIFIC && data_len > 4) {
10590 feature = (struct scsi_get_config_feature *)(hdr + 1);
10591 if (scsi_2btoul(feature->feature_code) == starting)
10592 feature = (struct scsi_get_config_feature *)
10593 &feature->feature_data[feature->add_length];
10594 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10595 }
10596 scsi_ulto4b(data_len - 4, hdr->data_length);
10597 ctsio->kern_data_len = min(data_len, alloc_len);
10598 ctsio->kern_total_len = ctsio->kern_data_len;
10599
10600 ctl_set_success(ctsio);
10601 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10602 ctsio->be_move_done = ctl_config_move_done;
10603 ctl_datamove((union ctl_io *)ctsio);
10604 return (CTL_RETVAL_COMPLETE);
10605 }
10606
10607 int
ctl_get_event_status(struct ctl_scsiio * ctsio)10608 ctl_get_event_status(struct ctl_scsiio *ctsio)
10609 {
10610 struct scsi_get_event_status_header *hdr;
10611 struct scsi_get_event_status *cdb;
10612 uint32_t alloc_len, data_len;
10613
10614 cdb = (struct scsi_get_event_status *)ctsio->cdb;
10615 if ((cdb->byte2 & SGESN_POLLED) == 0) {
10616 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
10617 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
10618 ctl_done((union ctl_io *)ctsio);
10619 return (CTL_RETVAL_COMPLETE);
10620 }
10621 alloc_len = scsi_2btoul(cdb->length);
10622
10623 data_len = sizeof(struct scsi_get_event_status_header);
10624 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10625 ctsio->kern_sg_entries = 0;
10626 ctsio->kern_rel_offset = 0;
10627 ctsio->kern_data_len = min(data_len, alloc_len);
10628 ctsio->kern_total_len = ctsio->kern_data_len;
10629
10630 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr;
10631 scsi_ulto2b(0, hdr->descr_length);
10632 hdr->nea_class = SGESN_NEA;
10633 hdr->supported_class = 0;
10634
10635 ctl_set_success(ctsio);
10636 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10637 ctsio->be_move_done = ctl_config_move_done;
10638 ctl_datamove((union ctl_io *)ctsio);
10639 return (CTL_RETVAL_COMPLETE);
10640 }
10641
10642 int
ctl_mechanism_status(struct ctl_scsiio * ctsio)10643 ctl_mechanism_status(struct ctl_scsiio *ctsio)
10644 {
10645 struct scsi_mechanism_status_header *hdr;
10646 struct scsi_mechanism_status *cdb;
10647 uint32_t alloc_len, data_len;
10648
10649 cdb = (struct scsi_mechanism_status *)ctsio->cdb;
10650 alloc_len = scsi_2btoul(cdb->length);
10651
10652 data_len = sizeof(struct scsi_mechanism_status_header);
10653 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10654 ctsio->kern_sg_entries = 0;
10655 ctsio->kern_rel_offset = 0;
10656 ctsio->kern_data_len = min(data_len, alloc_len);
10657 ctsio->kern_total_len = ctsio->kern_data_len;
10658
10659 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr;
10660 hdr->state1 = 0x00;
10661 hdr->state2 = 0xe0;
10662 scsi_ulto3b(0, hdr->lba);
10663 hdr->slots_num = 0;
10664 scsi_ulto2b(0, hdr->slots_length);
10665
10666 ctl_set_success(ctsio);
10667 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10668 ctsio->be_move_done = ctl_config_move_done;
10669 ctl_datamove((union ctl_io *)ctsio);
10670 return (CTL_RETVAL_COMPLETE);
10671 }
10672
10673 static void
ctl_ultomsf(uint32_t lba,uint8_t * buf)10674 ctl_ultomsf(uint32_t lba, uint8_t *buf)
10675 {
10676
10677 lba += 150;
10678 buf[0] = 0;
10679 buf[1] = bin2bcd((lba / 75) / 60);
10680 buf[2] = bin2bcd((lba / 75) % 60);
10681 buf[3] = bin2bcd(lba % 75);
10682 }
10683
10684 int
ctl_read_toc(struct ctl_scsiio * ctsio)10685 ctl_read_toc(struct ctl_scsiio *ctsio)
10686 {
10687 struct ctl_lun *lun = CTL_LUN(ctsio);
10688 struct scsi_read_toc_hdr *hdr;
10689 struct scsi_read_toc_type01_descr *descr;
10690 struct scsi_read_toc *cdb;
10691 uint32_t alloc_len, data_len;
10692 int format, msf;
10693
10694 cdb = (struct scsi_read_toc *)ctsio->cdb;
10695 msf = (cdb->byte2 & CD_MSF) != 0;
10696 format = cdb->format;
10697 alloc_len = scsi_2btoul(cdb->data_len);
10698
10699 data_len = sizeof(struct scsi_read_toc_hdr);
10700 if (format == 0)
10701 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr);
10702 else
10703 data_len += sizeof(struct scsi_read_toc_type01_descr);
10704 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10705 ctsio->kern_sg_entries = 0;
10706 ctsio->kern_rel_offset = 0;
10707 ctsio->kern_data_len = min(data_len, alloc_len);
10708 ctsio->kern_total_len = ctsio->kern_data_len;
10709
10710 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr;
10711 if (format == 0) {
10712 scsi_ulto2b(0x12, hdr->data_length);
10713 hdr->first = 1;
10714 hdr->last = 1;
10715 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10716 descr->addr_ctl = 0x14;
10717 descr->track_number = 1;
10718 if (msf)
10719 ctl_ultomsf(0, descr->track_start);
10720 else
10721 scsi_ulto4b(0, descr->track_start);
10722 descr++;
10723 descr->addr_ctl = 0x14;
10724 descr->track_number = 0xaa;
10725 if (msf)
10726 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start);
10727 else
10728 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start);
10729 } else {
10730 scsi_ulto2b(0x0a, hdr->data_length);
10731 hdr->first = 1;
10732 hdr->last = 1;
10733 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10734 descr->addr_ctl = 0x14;
10735 descr->track_number = 1;
10736 if (msf)
10737 ctl_ultomsf(0, descr->track_start);
10738 else
10739 scsi_ulto4b(0, descr->track_start);
10740 }
10741
10742 ctl_set_success(ctsio);
10743 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10744 ctsio->be_move_done = ctl_config_move_done;
10745 ctl_datamove((union ctl_io *)ctsio);
10746 return (CTL_RETVAL_COMPLETE);
10747 }
10748
10749 /*
10750 * For known CDB types, parse the LBA and length.
10751 */
10752 static int
ctl_get_lba_len(union ctl_io * io,uint64_t * lba,uint64_t * len)10753 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10754 {
10755
10756 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
10757 ("%s: unexpected I/O type %x", __func__, io->io_hdr.io_type));
10758
10759 switch (io->scsiio.cdb[0]) {
10760 case COMPARE_AND_WRITE: {
10761 struct scsi_compare_and_write *cdb;
10762
10763 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10764
10765 *lba = scsi_8btou64(cdb->addr);
10766 *len = cdb->length;
10767 break;
10768 }
10769 case READ_6:
10770 case WRITE_6: {
10771 struct scsi_rw_6 *cdb;
10772
10773 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10774
10775 *lba = scsi_3btoul(cdb->addr);
10776 /* only 5 bits are valid in the most significant address byte */
10777 *lba &= 0x1fffff;
10778 *len = cdb->length;
10779 break;
10780 }
10781 case READ_10:
10782 case WRITE_10: {
10783 struct scsi_rw_10 *cdb;
10784
10785 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10786
10787 *lba = scsi_4btoul(cdb->addr);
10788 *len = scsi_2btoul(cdb->length);
10789 break;
10790 }
10791 case WRITE_VERIFY_10: {
10792 struct scsi_write_verify_10 *cdb;
10793
10794 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10795
10796 *lba = scsi_4btoul(cdb->addr);
10797 *len = scsi_2btoul(cdb->length);
10798 break;
10799 }
10800 case READ_12:
10801 case WRITE_12: {
10802 struct scsi_rw_12 *cdb;
10803
10804 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10805
10806 *lba = scsi_4btoul(cdb->addr);
10807 *len = scsi_4btoul(cdb->length);
10808 break;
10809 }
10810 case WRITE_VERIFY_12: {
10811 struct scsi_write_verify_12 *cdb;
10812
10813 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10814
10815 *lba = scsi_4btoul(cdb->addr);
10816 *len = scsi_4btoul(cdb->length);
10817 break;
10818 }
10819 case READ_16:
10820 case WRITE_16: {
10821 struct scsi_rw_16 *cdb;
10822
10823 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10824
10825 *lba = scsi_8btou64(cdb->addr);
10826 *len = scsi_4btoul(cdb->length);
10827 break;
10828 }
10829 case WRITE_ATOMIC_16: {
10830 struct scsi_write_atomic_16 *cdb;
10831
10832 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb;
10833
10834 *lba = scsi_8btou64(cdb->addr);
10835 *len = scsi_2btoul(cdb->length);
10836 break;
10837 }
10838 case WRITE_VERIFY_16: {
10839 struct scsi_write_verify_16 *cdb;
10840
10841 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10842
10843 *lba = scsi_8btou64(cdb->addr);
10844 *len = scsi_4btoul(cdb->length);
10845 break;
10846 }
10847 case WRITE_SAME_10: {
10848 struct scsi_write_same_10 *cdb;
10849
10850 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10851
10852 *lba = scsi_4btoul(cdb->addr);
10853 *len = scsi_2btoul(cdb->length);
10854 break;
10855 }
10856 case WRITE_SAME_16: {
10857 struct scsi_write_same_16 *cdb;
10858
10859 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10860
10861 *lba = scsi_8btou64(cdb->addr);
10862 *len = scsi_4btoul(cdb->length);
10863 break;
10864 }
10865 case VERIFY_10: {
10866 struct scsi_verify_10 *cdb;
10867
10868 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10869
10870 *lba = scsi_4btoul(cdb->addr);
10871 *len = scsi_2btoul(cdb->length);
10872 break;
10873 }
10874 case VERIFY_12: {
10875 struct scsi_verify_12 *cdb;
10876
10877 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10878
10879 *lba = scsi_4btoul(cdb->addr);
10880 *len = scsi_4btoul(cdb->length);
10881 break;
10882 }
10883 case VERIFY_16: {
10884 struct scsi_verify_16 *cdb;
10885
10886 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10887
10888 *lba = scsi_8btou64(cdb->addr);
10889 *len = scsi_4btoul(cdb->length);
10890 break;
10891 }
10892 case UNMAP: {
10893 *lba = 0;
10894 *len = UINT64_MAX;
10895 break;
10896 }
10897 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10898 struct scsi_get_lba_status *cdb;
10899
10900 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10901 *lba = scsi_8btou64(cdb->addr);
10902 *len = UINT32_MAX;
10903 break;
10904 }
10905 default:
10906 *lba = 0;
10907 *len = UINT64_MAX;
10908 return (1);
10909 }
10910
10911 return (0);
10912 }
10913
10914 static ctl_action
ctl_extent_check_lba(uint64_t lba1,uint64_t len1,uint64_t lba2,uint64_t len2,bool seq)10915 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10916 bool seq)
10917 {
10918 uint64_t endlba1, endlba2;
10919
10920 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10921 endlba2 = lba2 + len2 - 1;
10922
10923 if ((endlba1 < lba2) || (endlba2 < lba1))
10924 return (CTL_ACTION_PASS);
10925 else
10926 return (CTL_ACTION_BLOCK);
10927 }
10928
10929 static int
ctl_extent_check_unmap(union ctl_io * io,uint64_t lba2,uint64_t len2)10930 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10931 {
10932 struct ctl_ptr_len_flags *ptrlen;
10933 struct scsi_unmap_desc *buf, *end, *range;
10934 uint64_t lba;
10935 uint32_t len;
10936
10937 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
10938 ("%s: unexpected I/O type %x", __func__, io->io_hdr.io_type));
10939
10940 /* If not UNMAP -- go other way. */
10941 if (io->scsiio.cdb[0] != UNMAP)
10942 return (CTL_ACTION_SKIP);
10943
10944 /* If UNMAP without data -- block and wait for data. */
10945 ptrlen = (struct ctl_ptr_len_flags *)
10946 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10947 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10948 ptrlen->ptr == NULL)
10949 return (CTL_ACTION_BLOCK);
10950
10951 /* UNMAP with data -- check for collision. */
10952 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10953 end = buf + ptrlen->len / sizeof(*buf);
10954 for (range = buf; range < end; range++) {
10955 lba = scsi_8btou64(range->lba);
10956 len = scsi_4btoul(range->length);
10957 if ((lba < lba2 + len2) && (lba + len > lba2))
10958 return (CTL_ACTION_BLOCK);
10959 }
10960 return (CTL_ACTION_PASS);
10961 }
10962
10963 static ctl_action
ctl_extent_check(union ctl_io * io1,union ctl_io * io2,bool seq)10964 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10965 {
10966 uint64_t lba1, lba2;
10967 uint64_t len1, len2;
10968 int retval;
10969
10970 retval = ctl_get_lba_len(io2, &lba2, &len2);
10971 KASSERT(retval == 0, ("ctl_get_lba_len() error"));
10972
10973 retval = ctl_extent_check_unmap(io1, lba2, len2);
10974 if (retval != CTL_ACTION_SKIP)
10975 return (retval);
10976
10977 retval = ctl_get_lba_len(io1, &lba1, &len1);
10978 KASSERT(retval == 0, ("ctl_get_lba_len() error"));
10979
10980 if (seq && (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE))
10981 seq = FALSE;
10982 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10983 }
10984
10985 static ctl_action
ctl_seq_check(union ctl_io * io1,union ctl_io * io2)10986 ctl_seq_check(union ctl_io *io1, union ctl_io *io2)
10987 {
10988 uint64_t lba1, lba2;
10989 uint64_t len1, len2;
10990 int retval;
10991
10992 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10993 return (CTL_ACTION_PASS);
10994 retval = ctl_get_lba_len(io1, &lba1, &len1);
10995 KASSERT(retval == 0, ("ctl_get_lba_len() error"));
10996 retval = ctl_get_lba_len(io2, &lba2, &len2);
10997 KASSERT(retval == 0, ("ctl_get_lba_len() error"));
10998
10999 if (lba1 + len1 == lba2)
11000 return (CTL_ACTION_BLOCK);
11001 return (CTL_ACTION_PASS);
11002 }
11003
11004 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)11005 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
11006 const uint8_t *serialize_row, union ctl_io *ooa_io)
11007 {
11008
11009 /*
11010 * The initiator attempted multiple untagged commands at the same
11011 * time. Can't do that.
11012 */
11013 if (__predict_false(pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11014 && __predict_false(ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11015 && ((pending_io->io_hdr.nexus.targ_port ==
11016 ooa_io->io_hdr.nexus.targ_port)
11017 && (pending_io->io_hdr.nexus.initid ==
11018 ooa_io->io_hdr.nexus.initid))
11019 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
11020 CTL_FLAG_STATUS_SENT)) == 0))
11021 return (CTL_ACTION_OVERLAP);
11022
11023 /*
11024 * The initiator attempted to send multiple tagged commands with
11025 * the same ID. (It's fine if different initiators have the same
11026 * tag ID.)
11027 *
11028 * Even if all of those conditions are true, we don't kill the I/O
11029 * if the command ahead of us has been aborted. We won't end up
11030 * sending it to the FETD, and it's perfectly legal to resend a
11031 * command with the same tag number as long as the previous
11032 * instance of this tag number has been aborted somehow.
11033 */
11034 if (__predict_true(pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11035 && __predict_true(ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11036 && __predict_false(pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
11037 && ((pending_io->io_hdr.nexus.targ_port ==
11038 ooa_io->io_hdr.nexus.targ_port)
11039 && (pending_io->io_hdr.nexus.initid ==
11040 ooa_io->io_hdr.nexus.initid))
11041 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
11042 CTL_FLAG_STATUS_SENT)) == 0))
11043 return (CTL_ACTION_OVERLAP_TAG);
11044
11045 /*
11046 * If we get a head of queue tag, SAM-3 says that we should
11047 * immediately execute it.
11048 *
11049 * What happens if this command would normally block for some other
11050 * reason? e.g. a request sense with a head of queue tag
11051 * immediately after a write. Normally that would block, but this
11052 * will result in its getting executed immediately...
11053 *
11054 * We currently return "pass" instead of "skip", so we'll end up
11055 * going through the rest of the queue to check for overlapped tags.
11056 *
11057 * XXX KDM check for other types of blockage first??
11058 */
11059 if (__predict_false(pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE))
11060 return (CTL_ACTION_PASS);
11061
11062 /*
11063 * Simple tags get blocked until all head of queue and ordered tags
11064 * ahead of them have completed. I'm lumping untagged commands in
11065 * with simple tags here. XXX KDM is that the right thing to do?
11066 */
11067 if (__predict_false(ooa_io->scsiio.tag_type == CTL_TAG_ORDERED) ||
11068 __predict_false(ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE))
11069 return (CTL_ACTION_BLOCK);
11070
11071 /* Unsupported command in OOA queue. */
11072 if (__predict_false(ooa_io->scsiio.seridx == CTL_SERIDX_INVLD))
11073 return (CTL_ACTION_PASS);
11074
11075 switch (serialize_row[ooa_io->scsiio.seridx]) {
11076 case CTL_SER_SEQ:
11077 if (lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
11078 return (ctl_seq_check(ooa_io, pending_io));
11079 /* FALLTHROUGH */
11080 case CTL_SER_PASS:
11081 return (CTL_ACTION_PASS);
11082 case CTL_SER_EXTENTOPT:
11083 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) ==
11084 SCP_QUEUE_ALG_UNRESTRICTED)
11085 return (CTL_ACTION_PASS);
11086 /* FALLTHROUGH */
11087 case CTL_SER_EXTENT:
11088 return (ctl_extent_check(ooa_io, pending_io,
11089 (lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
11090 case CTL_SER_BLOCKOPT:
11091 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) ==
11092 SCP_QUEUE_ALG_UNRESTRICTED)
11093 return (CTL_ACTION_PASS);
11094 /* FALLTHROUGH */
11095 case CTL_SER_BLOCK:
11096 return (CTL_ACTION_BLOCK);
11097 default:
11098 __assert_unreachable();
11099 }
11100 }
11101
11102 /*
11103 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
11104 * Assumptions:
11105 * - pending_io is generally either incoming, or on the blocked queue
11106 * - starting I/O is the I/O we want to start the check with.
11107 */
11108 static ctl_action
ctl_check_ooa(struct ctl_lun * lun,union ctl_io * pending_io,union ctl_io ** starting_io)11109 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
11110 union ctl_io **starting_io)
11111 {
11112 union ctl_io *ooa_io = *starting_io;
11113 const uint8_t *serialize_row;
11114 ctl_action action;
11115
11116 mtx_assert(&lun->lun_lock, MA_OWNED);
11117
11118 /*
11119 * Aborted commands are not going to be executed and may even
11120 * not report completion, so we don't care about their order.
11121 * Let them complete ASAP to clean the OOA queue.
11122 */
11123 if (__predict_false(pending_io->io_hdr.flags & CTL_FLAG_ABORT))
11124 return (CTL_ACTION_SKIP);
11125
11126 /*
11127 * Ordered tags have to block until all items ahead of them have
11128 * completed. If we get called with an ordered tag, we always
11129 * block, if something else is ahead of us in the queue.
11130 */
11131 if ((pending_io->scsiio.tag_type == CTL_TAG_ORDERED) &&
11132 (ooa_io != NULL))
11133 return (CTL_ACTION_BLOCK);
11134
11135 serialize_row = ctl_serialize_table[pending_io->scsiio.seridx];
11136
11137 /*
11138 * Run back along the OOA queue, starting with the current
11139 * blocked I/O and going through every I/O before it on the
11140 * queue. If starting_io is NULL, we'll just end up returning
11141 * CTL_ACTION_PASS.
11142 */
11143 for (; ooa_io != NULL;
11144 ooa_io = (union ctl_io *)LIST_NEXT(&ooa_io->io_hdr, ooa_links)) {
11145 action = ctl_check_for_blockage(lun, pending_io, serialize_row,
11146 ooa_io);
11147 if (action != CTL_ACTION_PASS) {
11148 *starting_io = ooa_io;
11149 return (action);
11150 }
11151 }
11152
11153 *starting_io = NULL;
11154 return (CTL_ACTION_PASS);
11155 }
11156
11157 /*
11158 * Try to unblock the specified I/O.
11159 *
11160 * skip parameter allows explicitly skip present blocker of the I/O,
11161 * starting from the previous one on OOA queue. It can be used when
11162 * we know for sure that the blocker I/O does no longer count.
11163 */
11164 static void
ctl_try_unblock_io(struct ctl_lun * lun,union ctl_io * io,bool skip)11165 ctl_try_unblock_io(struct ctl_lun *lun, union ctl_io *io, bool skip)
11166 {
11167 struct ctl_softc *softc = lun->ctl_softc;
11168 union ctl_io *bio, *obio;
11169 const struct ctl_cmd_entry *entry;
11170 union ctl_ha_msg msg_info;
11171 ctl_action action;
11172
11173 mtx_assert(&lun->lun_lock, MA_OWNED);
11174
11175 if (io->io_hdr.blocker == NULL)
11176 return;
11177
11178 obio = bio = io->io_hdr.blocker;
11179 if (skip)
11180 bio = (union ctl_io *)LIST_NEXT(&bio->io_hdr, ooa_links);
11181 action = ctl_check_ooa(lun, io, &bio);
11182 if (action == CTL_ACTION_BLOCK) {
11183 /* Still blocked, but may be by different I/O now. */
11184 if (bio != obio) {
11185 TAILQ_REMOVE(&obio->io_hdr.blocked_queue,
11186 &io->io_hdr, blocked_links);
11187 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue,
11188 &io->io_hdr, blocked_links);
11189 io->io_hdr.blocker = bio;
11190 }
11191 return;
11192 }
11193
11194 /* No longer blocked, one way or another. */
11195 TAILQ_REMOVE(&obio->io_hdr.blocked_queue, &io->io_hdr, blocked_links);
11196 io->io_hdr.blocker = NULL;
11197
11198 switch (action) {
11199 case CTL_ACTION_PASS:
11200 case CTL_ACTION_SKIP:
11201
11202 /* Serializing commands from the other SC retire there. */
11203 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) &&
11204 (softc->ha_mode != CTL_HA_MODE_XFER)) {
11205 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11206 msg_info.hdr.original_sc = io->io_hdr.remote_io;
11207 msg_info.hdr.serializing_sc = io;
11208 msg_info.hdr.msg_type = CTL_MSG_R2R;
11209 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11210 sizeof(msg_info.hdr), M_NOWAIT);
11211 break;
11212 }
11213
11214 /*
11215 * Check this I/O for LUN state changes that may have happened
11216 * while this command was blocked. The LUN state may have been
11217 * changed by a command ahead of us in the queue.
11218 */
11219 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
11220 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) {
11221 ctl_done(io);
11222 break;
11223 }
11224
11225 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11226 ctl_enqueue_rtr(io);
11227 break;
11228 default:
11229 __assert_unreachable();
11230 case CTL_ACTION_OVERLAP:
11231 ctl_set_overlapped_cmd(&io->scsiio);
11232 goto error;
11233 case CTL_ACTION_OVERLAP_TAG:
11234 ctl_set_overlapped_tag(&io->scsiio,
11235 io->scsiio.tag_num & 0xff);
11236 error:
11237 /* Serializing commands from the other SC are done here. */
11238 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) &&
11239 (softc->ha_mode != CTL_HA_MODE_XFER)) {
11240 ctl_try_unblock_others(lun, io, TRUE);
11241 LIST_REMOVE(&io->io_hdr, ooa_links);
11242
11243 ctl_copy_sense_data_back(io, &msg_info);
11244 msg_info.hdr.original_sc = io->io_hdr.remote_io;
11245 msg_info.hdr.serializing_sc = NULL;
11246 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
11247 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11248 sizeof(msg_info.scsi), M_WAITOK);
11249 ctl_free_io(io);
11250 break;
11251 }
11252
11253 ctl_done(io);
11254 break;
11255 }
11256 }
11257
11258 /*
11259 * Try to unblock I/Os blocked by the specified I/O.
11260 *
11261 * skip parameter allows explicitly skip the specified I/O as blocker,
11262 * starting from the previous one on the OOA queue. It can be used when
11263 * we know for sure that the specified I/O does no longer count (done).
11264 * It has to be still on OOA queue though so that we know where to start.
11265 */
11266 static void
ctl_try_unblock_others(struct ctl_lun * lun,union ctl_io * bio,bool skip)11267 ctl_try_unblock_others(struct ctl_lun *lun, union ctl_io *bio, bool skip)
11268 {
11269 union ctl_io *io, *next_io;
11270
11271 mtx_assert(&lun->lun_lock, MA_OWNED);
11272
11273 for (io = (union ctl_io *)TAILQ_FIRST(&bio->io_hdr.blocked_queue);
11274 io != NULL; io = next_io) {
11275 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, blocked_links);
11276
11277 KASSERT(io->io_hdr.blocker != NULL,
11278 ("I/O %p on blocked list without blocker", io));
11279 ctl_try_unblock_io(lun, io, skip);
11280 }
11281 KASSERT(!skip || TAILQ_EMPTY(&bio->io_hdr.blocked_queue),
11282 ("blocked_queue is not empty after skipping %p", bio));
11283 }
11284
11285 /*
11286 * This routine (with one exception) checks LUN flags that can be set by
11287 * commands ahead of us in the OOA queue. These flags have to be checked
11288 * when a command initially comes in, and when we pull a command off the
11289 * blocked queue and are preparing to execute it. The reason we have to
11290 * check these flags for commands on the blocked queue is that the LUN
11291 * state may have been changed by a command ahead of us while we're on the
11292 * blocked queue.
11293 *
11294 * Ordering is somewhat important with these checks, so please pay
11295 * careful attention to the placement of any new checks.
11296 */
11297 static int
ctl_scsiio_lun_check(struct ctl_lun * lun,const struct ctl_cmd_entry * entry,struct ctl_scsiio * ctsio)11298 ctl_scsiio_lun_check(struct ctl_lun *lun,
11299 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11300 {
11301 struct ctl_softc *softc = lun->ctl_softc;
11302 int retval;
11303 uint32_t residx;
11304
11305 retval = 0;
11306
11307 mtx_assert(&lun->lun_lock, MA_OWNED);
11308
11309 /*
11310 * If this shelf is a secondary shelf controller, we may have to
11311 * reject some commands disallowed by HA mode and link state.
11312 */
11313 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11314 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
11315 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11316 ctl_set_lun_unavail(ctsio);
11317 retval = 1;
11318 goto bailout;
11319 }
11320 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
11321 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11322 ctl_set_lun_transit(ctsio);
11323 retval = 1;
11324 goto bailout;
11325 }
11326 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
11327 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
11328 ctl_set_lun_standby(ctsio);
11329 retval = 1;
11330 goto bailout;
11331 }
11332
11333 /* The rest of checks are only done on executing side */
11334 if (softc->ha_mode == CTL_HA_MODE_XFER)
11335 goto bailout;
11336 }
11337
11338 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11339 if (lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
11340 ctl_set_hw_write_protected(ctsio);
11341 retval = 1;
11342 goto bailout;
11343 }
11344 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) {
11345 ctl_set_sense(ctsio, /*current_error*/ 1,
11346 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11347 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11348 retval = 1;
11349 goto bailout;
11350 }
11351 }
11352
11353 /*
11354 * Check for a reservation conflict. If this command isn't allowed
11355 * even on reserved LUNs, and if this initiator isn't the one who
11356 * reserved us, reject the command with a reservation conflict.
11357 */
11358 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11359 if ((lun->flags & CTL_LUN_RESERVED)
11360 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11361 if (lun->res_idx != residx) {
11362 ctl_set_reservation_conflict(ctsio);
11363 retval = 1;
11364 goto bailout;
11365 }
11366 }
11367
11368 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11369 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11370 /* No reservation or command is allowed. */;
11371 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11372 (lun->pr_res_type == SPR_TYPE_WR_EX ||
11373 lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
11374 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) {
11375 /* The command is allowed for Write Exclusive resv. */;
11376 } else {
11377 /*
11378 * if we aren't registered or it's a res holder type
11379 * reservation and this isn't the res holder then set a
11380 * conflict.
11381 */
11382 if (ctl_get_prkey(lun, residx) == 0 ||
11383 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) {
11384 ctl_set_reservation_conflict(ctsio);
11385 retval = 1;
11386 goto bailout;
11387 }
11388 }
11389
11390 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) {
11391 if (lun->flags & CTL_LUN_EJECTED)
11392 ctl_set_lun_ejected(ctsio);
11393 else if (lun->flags & CTL_LUN_NO_MEDIA) {
11394 if (lun->flags & CTL_LUN_REMOVABLE)
11395 ctl_set_lun_no_media(ctsio);
11396 else
11397 ctl_set_lun_int_reqd(ctsio);
11398 } else if (lun->flags & CTL_LUN_STOPPED)
11399 ctl_set_lun_stopped(ctsio);
11400 else
11401 goto bailout;
11402 retval = 1;
11403 goto bailout;
11404 }
11405
11406 bailout:
11407 return (retval);
11408 }
11409
11410 static void
ctl_failover_io(union ctl_io * io,int have_lock)11411 ctl_failover_io(union ctl_io *io, int have_lock)
11412 {
11413 ctl_set_busy(&io->scsiio);
11414 ctl_done(io);
11415 }
11416
11417 static void
ctl_failover_lun(union ctl_io * rio)11418 ctl_failover_lun(union ctl_io *rio)
11419 {
11420 struct ctl_softc *softc = CTL_SOFTC(rio);
11421 struct ctl_lun *lun;
11422 struct ctl_io_hdr *io, *next_io;
11423 uint32_t targ_lun;
11424
11425 targ_lun = rio->io_hdr.nexus.targ_mapped_lun;
11426 CTL_DEBUG_PRINT(("FAILOVER for lun %u\n", targ_lun));
11427
11428 /* Find and lock the LUN. */
11429 mtx_lock(&softc->ctl_lock);
11430 if (targ_lun > ctl_max_luns ||
11431 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11432 mtx_unlock(&softc->ctl_lock);
11433 return;
11434 }
11435 mtx_lock(&lun->lun_lock);
11436 mtx_unlock(&softc->ctl_lock);
11437 if (lun->flags & CTL_LUN_DISABLED) {
11438 mtx_unlock(&lun->lun_lock);
11439 return;
11440 }
11441
11442 if (softc->ha_mode == CTL_HA_MODE_XFER) {
11443 LIST_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11444 /* We are master */
11445 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11446 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11447 io->flags |= CTL_FLAG_ABORT |
11448 CTL_FLAG_FAILOVER;
11449 ctl_try_unblock_io(lun,
11450 (union ctl_io *)io, FALSE);
11451 } else { /* This can be only due to DATAMOVE */
11452 io->msg_type = CTL_MSG_DATAMOVE_DONE;
11453 io->flags &= ~CTL_FLAG_DMA_INPROG;
11454 io->flags |= CTL_FLAG_IO_ACTIVE;
11455 io->port_status = 31340;
11456 ctl_enqueue_isc((union ctl_io *)io);
11457 }
11458 } else
11459 /* We are slave */
11460 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11461 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11462 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11463 io->flags |= CTL_FLAG_FAILOVER;
11464 } else {
11465 ctl_set_busy(&((union ctl_io *)io)->
11466 scsiio);
11467 ctl_done((union ctl_io *)io);
11468 }
11469 }
11470 }
11471 } else { /* SERIALIZE modes */
11472 LIST_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11473 /* We are master */
11474 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11475 if (io->blocker != NULL) {
11476 TAILQ_REMOVE(&io->blocker->io_hdr.blocked_queue,
11477 io, blocked_links);
11478 io->blocker = NULL;
11479 }
11480 ctl_try_unblock_others(lun, (union ctl_io *)io,
11481 TRUE);
11482 LIST_REMOVE(io, ooa_links);
11483 ctl_free_io((union ctl_io *)io);
11484 } else
11485 /* We are slave */
11486 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11487 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11488 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
11489 ctl_set_busy(&((union ctl_io *)io)->
11490 scsiio);
11491 ctl_done((union ctl_io *)io);
11492 }
11493 }
11494 }
11495 }
11496 mtx_unlock(&lun->lun_lock);
11497 }
11498
11499 static void
ctl_scsiio_precheck(struct ctl_scsiio * ctsio)11500 ctl_scsiio_precheck(struct ctl_scsiio *ctsio)
11501 {
11502 struct ctl_softc *softc = CTL_SOFTC(ctsio);
11503 struct ctl_lun *lun;
11504 const struct ctl_cmd_entry *entry;
11505 union ctl_io *bio;
11506 uint32_t initidx, targ_lun;
11507
11508 lun = NULL;
11509 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11510 if (targ_lun < ctl_max_luns)
11511 lun = softc->ctl_luns[targ_lun];
11512 if (lun) {
11513 /*
11514 * If the LUN is invalid, pretend that it doesn't exist.
11515 * It will go away as soon as all pending I/O has been
11516 * completed.
11517 */
11518 mtx_lock(&lun->lun_lock);
11519 if (lun->flags & CTL_LUN_DISABLED) {
11520 mtx_unlock(&lun->lun_lock);
11521 lun = NULL;
11522 }
11523 }
11524 CTL_LUN(ctsio) = lun;
11525 if (lun) {
11526 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
11527
11528 /*
11529 * Every I/O goes into the OOA queue for a particular LUN,
11530 * and stays there until completion.
11531 */
11532 #ifdef CTL_TIME_IO
11533 if (LIST_EMPTY(&lun->ooa_queue))
11534 lun->idle_time += getsbinuptime() - lun->last_busy;
11535 #endif
11536 LIST_INSERT_HEAD(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
11537 }
11538
11539 /* Get command entry and return error if it is unsuppotyed. */
11540 entry = ctl_validate_command(ctsio);
11541 if (entry == NULL) {
11542 if (lun)
11543 mtx_unlock(&lun->lun_lock);
11544 return;
11545 }
11546
11547 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11548 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11549
11550 /*
11551 * Check to see whether we can send this command to LUNs that don't
11552 * exist. This should pretty much only be the case for inquiry
11553 * and request sense. Further checks, below, really require having
11554 * a LUN, so we can't really check the command anymore. Just put
11555 * it on the rtr queue.
11556 */
11557 if (lun == NULL) {
11558 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11559 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11560 ctl_enqueue_rtr((union ctl_io *)ctsio);
11561 return;
11562 }
11563
11564 ctl_set_unsupported_lun(ctsio);
11565 ctl_done((union ctl_io *)ctsio);
11566 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11567 return;
11568 } else {
11569 /*
11570 * Make sure we support this particular command on this LUN.
11571 * e.g., we don't support writes to the control LUN.
11572 */
11573 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11574 mtx_unlock(&lun->lun_lock);
11575 ctl_set_invalid_opcode(ctsio);
11576 ctl_done((union ctl_io *)ctsio);
11577 return;
11578 }
11579 }
11580
11581 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11582
11583 /*
11584 * If we've got a request sense, it'll clear the contingent
11585 * allegiance condition. Otherwise, if we have a CA condition for
11586 * this initiator, clear it, because it sent down a command other
11587 * than request sense.
11588 */
11589 if (ctsio->cdb[0] != REQUEST_SENSE) {
11590 struct scsi_sense_data *ps;
11591
11592 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
11593 if (ps != NULL)
11594 ps[initidx % CTL_MAX_INIT_PER_PORT].error_code = 0;
11595 }
11596
11597 /*
11598 * If the command has this flag set, it handles its own unit
11599 * attention reporting, we shouldn't do anything. Otherwise we
11600 * check for any pending unit attentions, and send them back to the
11601 * initiator. We only do this when a command initially comes in,
11602 * not when we pull it off the blocked queue.
11603 *
11604 * According to SAM-3, section 5.3.2, the order that things get
11605 * presented back to the host is basically unit attentions caused
11606 * by some sort of reset event, busy status, reservation conflicts
11607 * or task set full, and finally any other status.
11608 *
11609 * One issue here is that some of the unit attentions we report
11610 * don't fall into the "reset" category (e.g. "reported luns data
11611 * has changed"). So reporting it here, before the reservation
11612 * check, may be technically wrong. I guess the only thing to do
11613 * would be to check for and report the reset events here, and then
11614 * check for the other unit attention types after we check for a
11615 * reservation conflict.
11616 *
11617 * XXX KDM need to fix this
11618 */
11619 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11620 ctl_ua_type ua_type;
11621 u_int sense_len = 0;
11622
11623 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11624 &sense_len, SSD_TYPE_NONE);
11625 if (ua_type != CTL_UA_NONE) {
11626 mtx_unlock(&lun->lun_lock);
11627 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11628 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11629 ctsio->sense_len = sense_len;
11630 ctl_done((union ctl_io *)ctsio);
11631 return;
11632 }
11633 }
11634
11635 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11636 mtx_unlock(&lun->lun_lock);
11637 ctl_done((union ctl_io *)ctsio);
11638 return;
11639 }
11640
11641 /*
11642 * XXX CHD this is where we want to send IO to other side if
11643 * this LUN is secondary on this SC. We will need to make a copy
11644 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11645 * the copy we send as FROM_OTHER.
11646 * We also need to stuff the address of the original IO so we can
11647 * find it easily. Something similar will need be done on the other
11648 * side so when we are done we can find the copy.
11649 */
11650 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11651 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
11652 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
11653 union ctl_ha_msg msg_info;
11654 int isc_retval;
11655
11656 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11657 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11658 mtx_unlock(&lun->lun_lock);
11659
11660 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11661 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11662 msg_info.hdr.serializing_sc = NULL;
11663 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11664 msg_info.scsi.tag_num = ctsio->tag_num;
11665 msg_info.scsi.tag_type = ctsio->tag_type;
11666 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11667 msg_info.scsi.cdb_len = ctsio->cdb_len;
11668 msg_info.scsi.priority = ctsio->priority;
11669
11670 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11671 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11672 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11673 ctsio->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11674 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
11675 ctl_set_busy(ctsio);
11676 ctl_done((union ctl_io *)ctsio);
11677 return;
11678 }
11679 return;
11680 }
11681
11682 bio = (union ctl_io *)LIST_NEXT(&ctsio->io_hdr, ooa_links);
11683 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, &bio)) {
11684 case CTL_ACTION_PASS:
11685 case CTL_ACTION_SKIP:
11686 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11687 mtx_unlock(&lun->lun_lock);
11688 ctl_enqueue_rtr((union ctl_io *)ctsio);
11689 break;
11690 case CTL_ACTION_BLOCK:
11691 ctsio->io_hdr.blocker = bio;
11692 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctsio->io_hdr,
11693 blocked_links);
11694 mtx_unlock(&lun->lun_lock);
11695 break;
11696 case CTL_ACTION_OVERLAP:
11697 mtx_unlock(&lun->lun_lock);
11698 ctl_set_overlapped_cmd(ctsio);
11699 ctl_done((union ctl_io *)ctsio);
11700 break;
11701 case CTL_ACTION_OVERLAP_TAG:
11702 mtx_unlock(&lun->lun_lock);
11703 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11704 ctl_done((union ctl_io *)ctsio);
11705 break;
11706 default:
11707 __assert_unreachable();
11708 }
11709 }
11710
11711 const struct ctl_cmd_entry *
ctl_get_cmd_entry(struct ctl_scsiio * ctsio,int * sa)11712 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11713 {
11714 const struct ctl_cmd_entry *entry;
11715 int service_action;
11716
11717 entry = &ctl_cmd_table[ctsio->cdb[0]];
11718 if (sa)
11719 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11720 if (entry->flags & CTL_CMD_FLAG_SA5) {
11721 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11722 entry = &((const struct ctl_cmd_entry *)
11723 entry->execute)[service_action];
11724 }
11725 return (entry);
11726 }
11727
11728 const struct ctl_cmd_entry *
ctl_validate_command(struct ctl_scsiio * ctsio)11729 ctl_validate_command(struct ctl_scsiio *ctsio)
11730 {
11731 const struct ctl_cmd_entry *entry;
11732 int i, sa;
11733 uint8_t diff;
11734
11735 entry = ctl_get_cmd_entry(ctsio, &sa);
11736 ctsio->seridx = entry->seridx;
11737 if (entry->execute == NULL) {
11738 if (sa)
11739 ctl_set_invalid_field(ctsio,
11740 /*sks_valid*/ 1,
11741 /*command*/ 1,
11742 /*field*/ 1,
11743 /*bit_valid*/ 1,
11744 /*bit*/ 4);
11745 else
11746 ctl_set_invalid_opcode(ctsio);
11747 ctl_done((union ctl_io *)ctsio);
11748 return (NULL);
11749 }
11750 KASSERT(entry->length > 0,
11751 ("Not defined length for command 0x%02x/0x%02x",
11752 ctsio->cdb[0], ctsio->cdb[1]));
11753 for (i = 1; i < entry->length; i++) {
11754 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11755 if (diff == 0)
11756 continue;
11757 ctl_set_invalid_field(ctsio,
11758 /*sks_valid*/ 1,
11759 /*command*/ 1,
11760 /*field*/ i,
11761 /*bit_valid*/ 1,
11762 /*bit*/ fls(diff) - 1);
11763 ctl_done((union ctl_io *)ctsio);
11764 return (NULL);
11765 }
11766 return (entry);
11767 }
11768
11769 static int
ctl_cmd_applicable(uint8_t lun_type,const struct ctl_cmd_entry * entry)11770 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11771 {
11772
11773 switch (lun_type) {
11774 case T_DIRECT:
11775 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0)
11776 return (0);
11777 break;
11778 case T_PROCESSOR:
11779 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11780 return (0);
11781 break;
11782 case T_CDROM:
11783 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0)
11784 return (0);
11785 break;
11786 default:
11787 return (0);
11788 }
11789 return (1);
11790 }
11791
11792 static int
ctl_scsiio(struct ctl_scsiio * ctsio)11793 ctl_scsiio(struct ctl_scsiio *ctsio)
11794 {
11795 int retval;
11796 const struct ctl_cmd_entry *entry;
11797
11798 retval = CTL_RETVAL_COMPLETE;
11799
11800 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11801
11802 entry = ctl_get_cmd_entry(ctsio, NULL);
11803
11804 /*
11805 * If this I/O has been aborted, just send it straight to
11806 * ctl_done() without executing it.
11807 */
11808 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11809 ctl_done((union ctl_io *)ctsio);
11810 goto bailout;
11811 }
11812
11813 /*
11814 * All the checks should have been handled by ctl_scsiio_precheck().
11815 * We should be clear now to just execute the I/O.
11816 */
11817 retval = entry->execute(ctsio);
11818
11819 bailout:
11820 return (retval);
11821 }
11822
11823 static int
ctl_target_reset(union ctl_io * io)11824 ctl_target_reset(union ctl_io *io)
11825 {
11826 struct ctl_softc *softc = CTL_SOFTC(io);
11827 struct ctl_port *port = CTL_PORT(io);
11828 struct ctl_lun *lun;
11829 uint32_t initidx;
11830 ctl_ua_type ua_type;
11831
11832 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11833 union ctl_ha_msg msg_info;
11834
11835 msg_info.hdr.nexus = io->io_hdr.nexus;
11836 msg_info.task.task_action = io->taskio.task_action;
11837 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11838 msg_info.hdr.original_sc = NULL;
11839 msg_info.hdr.serializing_sc = NULL;
11840 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11841 sizeof(msg_info.task), M_WAITOK);
11842 }
11843
11844 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11845 if (io->taskio.task_action == CTL_TASK_TARGET_RESET)
11846 ua_type = CTL_UA_TARG_RESET;
11847 else
11848 ua_type = CTL_UA_BUS_RESET;
11849 mtx_lock(&softc->ctl_lock);
11850 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11851 if (port != NULL &&
11852 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
11853 continue;
11854 ctl_do_lun_reset(lun, initidx, ua_type);
11855 }
11856 mtx_unlock(&softc->ctl_lock);
11857 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11858 return (0);
11859 }
11860
11861 /*
11862 * The LUN should always be set. The I/O is optional, and is used to
11863 * distinguish between I/Os sent by this initiator, and by other
11864 * initiators. We set unit attention for initiators other than this one.
11865 * SAM-3 is vague on this point. It does say that a unit attention should
11866 * be established for other initiators when a LUN is reset (see section
11867 * 5.7.3), but it doesn't specifically say that the unit attention should
11868 * be established for this particular initiator when a LUN is reset. Here
11869 * is the relevant text, from SAM-3 rev 8:
11870 *
11871 * 5.7.2 When a SCSI initiator port aborts its own tasks
11872 *
11873 * When a SCSI initiator port causes its own task(s) to be aborted, no
11874 * notification that the task(s) have been aborted shall be returned to
11875 * the SCSI initiator port other than the completion response for the
11876 * command or task management function action that caused the task(s) to
11877 * be aborted and notification(s) associated with related effects of the
11878 * action (e.g., a reset unit attention condition).
11879 *
11880 * XXX KDM for now, we're setting unit attention for all initiators.
11881 */
11882 static void
ctl_do_lun_reset(struct ctl_lun * lun,uint32_t initidx,ctl_ua_type ua_type)11883 ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua_type)
11884 {
11885 struct ctl_io_hdr *xioh;
11886 int i;
11887
11888 mtx_lock(&lun->lun_lock);
11889 /* Abort tasks. */
11890 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) {
11891 xioh->flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11892 ctl_try_unblock_io(lun, (union ctl_io *)xioh, FALSE);
11893 }
11894 /* Clear CA. */
11895 for (i = 0; i < ctl_max_ports; i++) {
11896 free(lun->pending_sense[i], M_CTL);
11897 lun->pending_sense[i] = NULL;
11898 }
11899 /* Clear reservation. */
11900 lun->flags &= ~CTL_LUN_RESERVED;
11901 /* Clear prevent media removal. */
11902 if (lun->prevent) {
11903 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11904 ctl_clear_mask(lun->prevent, i);
11905 lun->prevent_count = 0;
11906 }
11907 /* Clear TPC status */
11908 ctl_tpc_lun_clear(lun, -1);
11909 /* Establish UA. */
11910 #if 0
11911 ctl_est_ua_all(lun, initidx, ua_type);
11912 #else
11913 ctl_est_ua_all(lun, -1, ua_type);
11914 #endif
11915 mtx_unlock(&lun->lun_lock);
11916 }
11917
11918 static int
ctl_lun_reset(union ctl_io * io)11919 ctl_lun_reset(union ctl_io *io)
11920 {
11921 struct ctl_softc *softc = CTL_SOFTC(io);
11922 struct ctl_lun *lun;
11923 uint32_t targ_lun, initidx;
11924
11925 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11926 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11927 mtx_lock(&softc->ctl_lock);
11928 if (targ_lun >= ctl_max_luns ||
11929 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11930 mtx_unlock(&softc->ctl_lock);
11931 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11932 return (1);
11933 }
11934 ctl_do_lun_reset(lun, initidx, CTL_UA_LUN_RESET);
11935 mtx_unlock(&softc->ctl_lock);
11936 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11937
11938 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
11939 union ctl_ha_msg msg_info;
11940
11941 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11942 msg_info.hdr.nexus = io->io_hdr.nexus;
11943 msg_info.task.task_action = CTL_TASK_LUN_RESET;
11944 msg_info.hdr.original_sc = NULL;
11945 msg_info.hdr.serializing_sc = NULL;
11946 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11947 sizeof(msg_info.task), M_WAITOK);
11948 }
11949 return (0);
11950 }
11951
11952 static void
ctl_abort_tasks_lun(struct ctl_lun * lun,uint32_t targ_port,uint32_t init_id,int other_sc)11953 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11954 int other_sc)
11955 {
11956 struct ctl_io_hdr *xioh;
11957
11958 mtx_assert(&lun->lun_lock, MA_OWNED);
11959
11960 /*
11961 * Run through the OOA queue and attempt to find the given I/O.
11962 * The target port, initiator ID, tag type and tag number have to
11963 * match the values that we got from the initiator. If we have an
11964 * untagged command to abort, simply abort the first untagged command
11965 * we come to. We only allow one untagged command at a time of course.
11966 */
11967 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) {
11968 union ctl_io *xio = (union ctl_io *)xioh;
11969 if ((targ_port == UINT32_MAX ||
11970 targ_port == xioh->nexus.targ_port) &&
11971 (init_id == UINT32_MAX ||
11972 init_id == xioh->nexus.initid)) {
11973 if (targ_port != xioh->nexus.targ_port ||
11974 init_id != xioh->nexus.initid)
11975 xioh->flags |= CTL_FLAG_ABORT_STATUS;
11976 xioh->flags |= CTL_FLAG_ABORT;
11977 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11978 union ctl_ha_msg msg_info;
11979
11980 msg_info.hdr.nexus = xioh->nexus;
11981 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11982 msg_info.task.tag_num = xio->scsiio.tag_num;
11983 msg_info.task.tag_type = xio->scsiio.tag_type;
11984 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11985 msg_info.hdr.original_sc = NULL;
11986 msg_info.hdr.serializing_sc = NULL;
11987 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11988 sizeof(msg_info.task), M_NOWAIT);
11989 }
11990 ctl_try_unblock_io(lun, xio, FALSE);
11991 }
11992 }
11993 }
11994
11995 static int
ctl_abort_task_set(union ctl_io * io)11996 ctl_abort_task_set(union ctl_io *io)
11997 {
11998 struct ctl_softc *softc = CTL_SOFTC(io);
11999 struct ctl_lun *lun;
12000 uint32_t targ_lun;
12001
12002 /*
12003 * Look up the LUN.
12004 */
12005 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12006 mtx_lock(&softc->ctl_lock);
12007 if (targ_lun >= ctl_max_luns ||
12008 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12009 mtx_unlock(&softc->ctl_lock);
12010 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12011 return (1);
12012 }
12013
12014 mtx_lock(&lun->lun_lock);
12015 mtx_unlock(&softc->ctl_lock);
12016 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
12017 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12018 io->io_hdr.nexus.initid,
12019 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12020 } else { /* CTL_TASK_CLEAR_TASK_SET */
12021 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
12022 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12023 }
12024 mtx_unlock(&lun->lun_lock);
12025 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12026 return (0);
12027 }
12028
12029 static void
ctl_i_t_nexus_loss(struct ctl_softc * softc,uint32_t initidx,ctl_ua_type ua_type)12030 ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx,
12031 ctl_ua_type ua_type)
12032 {
12033 struct ctl_lun *lun;
12034 struct scsi_sense_data *ps;
12035 uint32_t p, i;
12036
12037 p = initidx / CTL_MAX_INIT_PER_PORT;
12038 i = initidx % CTL_MAX_INIT_PER_PORT;
12039 mtx_lock(&softc->ctl_lock);
12040 STAILQ_FOREACH(lun, &softc->lun_list, links) {
12041 mtx_lock(&lun->lun_lock);
12042 /* Abort tasks. */
12043 ctl_abort_tasks_lun(lun, p, i, 1);
12044 /* Clear CA. */
12045 ps = lun->pending_sense[p];
12046 if (ps != NULL)
12047 ps[i].error_code = 0;
12048 /* Clear reservation. */
12049 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
12050 lun->flags &= ~CTL_LUN_RESERVED;
12051 /* Clear prevent media removal. */
12052 if (lun->prevent && ctl_is_set(lun->prevent, initidx)) {
12053 ctl_clear_mask(lun->prevent, initidx);
12054 lun->prevent_count--;
12055 }
12056 /* Clear TPC status */
12057 ctl_tpc_lun_clear(lun, initidx);
12058 /* Establish UA. */
12059 ctl_est_ua(lun, initidx, ua_type);
12060 mtx_unlock(&lun->lun_lock);
12061 }
12062 mtx_unlock(&softc->ctl_lock);
12063 }
12064
12065 static int
ctl_i_t_nexus_reset(union ctl_io * io)12066 ctl_i_t_nexus_reset(union ctl_io *io)
12067 {
12068 struct ctl_softc *softc = CTL_SOFTC(io);
12069 uint32_t initidx;
12070
12071 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12072 union ctl_ha_msg msg_info;
12073
12074 msg_info.hdr.nexus = io->io_hdr.nexus;
12075 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
12076 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12077 msg_info.hdr.original_sc = NULL;
12078 msg_info.hdr.serializing_sc = NULL;
12079 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12080 sizeof(msg_info.task), M_WAITOK);
12081 }
12082
12083 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12084 ctl_i_t_nexus_loss(softc, initidx, CTL_UA_I_T_NEXUS_LOSS);
12085 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12086 return (0);
12087 }
12088
12089 static int
ctl_abort_task(union ctl_io * io)12090 ctl_abort_task(union ctl_io *io)
12091 {
12092 struct ctl_softc *softc = CTL_SOFTC(io);
12093 struct ctl_io_hdr *xioh;
12094 struct ctl_lun *lun;
12095 uint32_t targ_lun;
12096
12097 /*
12098 * Look up the LUN.
12099 */
12100 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12101 mtx_lock(&softc->ctl_lock);
12102 if (targ_lun >= ctl_max_luns ||
12103 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12104 mtx_unlock(&softc->ctl_lock);
12105 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12106 return (1);
12107 }
12108
12109 mtx_lock(&lun->lun_lock);
12110 mtx_unlock(&softc->ctl_lock);
12111 /*
12112 * Run through the OOA queue and attempt to find the given I/O.
12113 * The target port, initiator ID, tag type and tag number have to
12114 * match the values that we got from the initiator. If we have an
12115 * untagged command to abort, simply abort the first untagged command
12116 * we come to. We only allow one untagged command at a time of course.
12117 */
12118 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) {
12119 union ctl_io *xio = (union ctl_io *)xioh;
12120 if ((xioh->nexus.targ_port != io->io_hdr.nexus.targ_port)
12121 || (xioh->nexus.initid != io->io_hdr.nexus.initid)
12122 || (xioh->flags & CTL_FLAG_ABORT))
12123 continue;
12124
12125 /*
12126 * If the abort says that the task is untagged, the
12127 * task in the queue must be untagged. Otherwise,
12128 * we just check to see whether the tag numbers
12129 * match. This is because the QLogic firmware
12130 * doesn't pass back the tag type in an abort
12131 * request.
12132 */
12133 #if 0
12134 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12135 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12136 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
12137 #else
12138 /*
12139 * XXX KDM we've got problems with FC, because it
12140 * doesn't send down a tag type with aborts. So we
12141 * can only really go by the tag number...
12142 * This may cause problems with parallel SCSI.
12143 * Need to figure that out!!
12144 */
12145 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12146 #endif
12147 xioh->flags |= CTL_FLAG_ABORT;
12148 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
12149 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12150 union ctl_ha_msg msg_info;
12151
12152 msg_info.hdr.nexus = io->io_hdr.nexus;
12153 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12154 msg_info.task.tag_num = io->taskio.tag_num;
12155 msg_info.task.tag_type = io->taskio.tag_type;
12156 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12157 msg_info.hdr.original_sc = NULL;
12158 msg_info.hdr.serializing_sc = NULL;
12159 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12160 sizeof(msg_info.task), M_NOWAIT);
12161 }
12162 ctl_try_unblock_io(lun, xio, FALSE);
12163 }
12164 }
12165 mtx_unlock(&lun->lun_lock);
12166 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12167 return (0);
12168 }
12169
12170 static int
12171 ctl_query_task(union ctl_io *io, int task_set)
12172 {
12173 struct ctl_softc *softc = CTL_SOFTC(io);
12174 struct ctl_io_hdr *xioh;
12175 struct ctl_lun *lun;
12176 int found = 0;
12177 uint32_t targ_lun;
12178
12179 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12180 mtx_lock(&softc->ctl_lock);
12181 if (targ_lun >= ctl_max_luns ||
12182 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12183 mtx_unlock(&softc->ctl_lock);
12184 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12185 return (1);
12186 }
12187 mtx_lock(&lun->lun_lock);
12188 mtx_unlock(&softc->ctl_lock);
12189 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) {
12190 union ctl_io *xio = (union ctl_io *)xioh;
12191 if ((xioh->nexus.targ_port != io->io_hdr.nexus.targ_port)
12192 || (xioh->nexus.initid != io->io_hdr.nexus.initid)
12193 || (xioh->flags & CTL_FLAG_ABORT))
12194 continue;
12195
12196 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
12197 found = 1;
12198 break;
12199 }
12200 }
12201 mtx_unlock(&lun->lun_lock);
12202 if (found)
12203 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12204 else
12205 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12206 return (0);
12207 }
12208
12209 static int
12210 ctl_query_async_event(union ctl_io *io)
12211 {
12212 struct ctl_softc *softc = CTL_SOFTC(io);
12213 struct ctl_lun *lun;
12214 ctl_ua_type ua;
12215 uint32_t targ_lun, initidx;
12216
12217 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12218 mtx_lock(&softc->ctl_lock);
12219 if (targ_lun >= ctl_max_luns ||
12220 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12221 mtx_unlock(&softc->ctl_lock);
12222 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12223 return (1);
12224 }
12225 mtx_lock(&lun->lun_lock);
12226 mtx_unlock(&softc->ctl_lock);
12227 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12228 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
12229 mtx_unlock(&lun->lun_lock);
12230 if (ua != CTL_UA_NONE)
12231 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12232 else
12233 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12234 return (0);
12235 }
12236
12237 static void
12238 ctl_run_task(union ctl_io *io)
12239 {
12240 int retval = 1;
12241
12242 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12243 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12244 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
12245 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
12246 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
12247 switch (io->taskio.task_action) {
12248 case CTL_TASK_ABORT_TASK:
12249 retval = ctl_abort_task(io);
12250 break;
12251 case CTL_TASK_ABORT_TASK_SET:
12252 case CTL_TASK_CLEAR_TASK_SET:
12253 retval = ctl_abort_task_set(io);
12254 break;
12255 case CTL_TASK_CLEAR_ACA:
12256 break;
12257 case CTL_TASK_I_T_NEXUS_RESET:
12258 retval = ctl_i_t_nexus_reset(io);
12259 break;
12260 case CTL_TASK_LUN_RESET:
12261 retval = ctl_lun_reset(io);
12262 break;
12263 case CTL_TASK_TARGET_RESET:
12264 case CTL_TASK_BUS_RESET:
12265 retval = ctl_target_reset(io);
12266 break;
12267 case CTL_TASK_PORT_LOGIN:
12268 break;
12269 case CTL_TASK_PORT_LOGOUT:
12270 break;
12271 case CTL_TASK_QUERY_TASK:
12272 retval = ctl_query_task(io, 0);
12273 break;
12274 case CTL_TASK_QUERY_TASK_SET:
12275 retval = ctl_query_task(io, 1);
12276 break;
12277 case CTL_TASK_QUERY_ASYNC_EVENT:
12278 retval = ctl_query_async_event(io);
12279 break;
12280 default:
12281 printf("%s: got unknown task management event %d\n",
12282 __func__, io->taskio.task_action);
12283 break;
12284 }
12285 if (retval == 0)
12286 io->io_hdr.status = CTL_SUCCESS;
12287 else
12288 io->io_hdr.status = CTL_ERROR;
12289 ctl_done(io);
12290 }
12291
12292 /*
12293 * For HA operation. Handle commands that come in from the other
12294 * controller.
12295 */
12296 static void
12297 ctl_handle_isc(union ctl_io *io)
12298 {
12299 struct ctl_softc *softc = CTL_SOFTC(io);
12300 struct ctl_lun *lun;
12301 const struct ctl_cmd_entry *entry;
12302 uint32_t targ_lun;
12303
12304 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12305 switch (io->io_hdr.msg_type) {
12306 case CTL_MSG_SERIALIZE:
12307 ctl_serialize_other_sc_cmd(&io->scsiio);
12308 break;
12309 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */
12310 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12311 if (targ_lun >= ctl_max_luns ||
12312 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12313 ctl_done(io);
12314 break;
12315 }
12316 mtx_lock(&lun->lun_lock);
12317 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) {
12318 mtx_unlock(&lun->lun_lock);
12319 ctl_done(io);
12320 break;
12321 }
12322 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12323 mtx_unlock(&lun->lun_lock);
12324 ctl_enqueue_rtr(io);
12325 break;
12326 case CTL_MSG_FINISH_IO:
12327 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12328 ctl_done(io);
12329 break;
12330 }
12331 if (targ_lun >= ctl_max_luns ||
12332 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12333 ctl_free_io(io);
12334 break;
12335 }
12336 mtx_lock(&lun->lun_lock);
12337 ctl_try_unblock_others(lun, io, TRUE);
12338 LIST_REMOVE(&io->io_hdr, ooa_links);
12339 mtx_unlock(&lun->lun_lock);
12340 ctl_free_io(io);
12341 break;
12342 case CTL_MSG_PERS_ACTION:
12343 ctl_hndl_per_res_out_on_other_sc(io);
12344 ctl_free_io(io);
12345 break;
12346 case CTL_MSG_BAD_JUJU:
12347 ctl_done(io);
12348 break;
12349 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */
12350 ctl_datamove_remote(io);
12351 break;
12352 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */
12353 ctl_datamove_done(io, false);
12354 break;
12355 case CTL_MSG_FAILOVER:
12356 ctl_failover_lun(io);
12357 ctl_free_io(io);
12358 break;
12359 default:
12360 printf("%s: Invalid message type %d\n",
12361 __func__, io->io_hdr.msg_type);
12362 ctl_free_io(io);
12363 break;
12364 }
12365
12366 }
12367
12368 /*
12369 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12370 * there is no match.
12371 */
12372 static ctl_lun_error_pattern
12373 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12374 {
12375 const struct ctl_cmd_entry *entry;
12376 ctl_lun_error_pattern filtered_pattern, pattern;
12377
12378 pattern = desc->error_pattern;
12379
12380 /*
12381 * XXX KDM we need more data passed into this function to match a
12382 * custom pattern, and we actually need to implement custom pattern
12383 * matching.
12384 */
12385 if (pattern & CTL_LUN_PAT_CMD)
12386 return (CTL_LUN_PAT_CMD);
12387
12388 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12389 return (CTL_LUN_PAT_ANY);
12390
12391 entry = ctl_get_cmd_entry(ctsio, NULL);
12392
12393 filtered_pattern = entry->pattern & pattern;
12394
12395 /*
12396 * If the user requested specific flags in the pattern (e.g.
12397 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12398 * flags.
12399 *
12400 * If the user did not specify any flags, it doesn't matter whether
12401 * or not the command supports the flags.
12402 */
12403 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12404 (pattern & ~CTL_LUN_PAT_MASK))
12405 return (CTL_LUN_PAT_NONE);
12406
12407 /*
12408 * If the user asked for a range check, see if the requested LBA
12409 * range overlaps with this command's LBA range.
12410 */
12411 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12412 uint64_t lba1;
12413 uint64_t len1;
12414 ctl_action action;
12415 int retval;
12416
12417 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12418 if (retval != 0)
12419 return (CTL_LUN_PAT_NONE);
12420
12421 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12422 desc->lba_range.len, FALSE);
12423 /*
12424 * A "pass" means that the LBA ranges don't overlap, so
12425 * this doesn't match the user's range criteria.
12426 */
12427 if (action == CTL_ACTION_PASS)
12428 return (CTL_LUN_PAT_NONE);
12429 }
12430
12431 return (filtered_pattern);
12432 }
12433
12434 static void
12435 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12436 {
12437 struct ctl_error_desc *desc, *desc2;
12438
12439 mtx_assert(&lun->lun_lock, MA_OWNED);
12440
12441 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12442 ctl_lun_error_pattern pattern;
12443 /*
12444 * Check to see whether this particular command matches
12445 * the pattern in the descriptor.
12446 */
12447 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12448 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12449 continue;
12450
12451 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12452 case CTL_LUN_INJ_ABORTED:
12453 ctl_set_aborted(&io->scsiio);
12454 break;
12455 case CTL_LUN_INJ_MEDIUM_ERR:
12456 ctl_set_medium_error(&io->scsiio,
12457 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
12458 CTL_FLAG_DATA_OUT);
12459 break;
12460 case CTL_LUN_INJ_UA:
12461 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12462 * OCCURRED */
12463 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12464 break;
12465 case CTL_LUN_INJ_CUSTOM:
12466 /*
12467 * We're assuming the user knows what he is doing.
12468 * Just copy the sense information without doing
12469 * checks.
12470 */
12471 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12472 MIN(sizeof(desc->custom_sense),
12473 sizeof(io->scsiio.sense_data)));
12474 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12475 io->scsiio.sense_len = SSD_FULL_SIZE;
12476 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12477 break;
12478 case CTL_LUN_INJ_NONE:
12479 default:
12480 /*
12481 * If this is an error injection type we don't know
12482 * about, clear the continuous flag (if it is set)
12483 * so it will get deleted below.
12484 */
12485 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12486 break;
12487 }
12488 /*
12489 * By default, each error injection action is a one-shot
12490 */
12491 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12492 continue;
12493
12494 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12495
12496 free(desc, M_CTL);
12497 }
12498 }
12499
12500 #ifdef CTL_IO_DELAY
12501 static void
12502 ctl_datamove_timer_wakeup(void *arg)
12503 {
12504 union ctl_io *io;
12505
12506 io = (union ctl_io *)arg;
12507
12508 ctl_datamove(io);
12509 }
12510 #endif /* CTL_IO_DELAY */
12511
12512 static void
12513 ctl_datamove_done_process(union ctl_io *io)
12514 {
12515 #ifdef CTL_TIME_IO
12516 struct bintime cur_bt;
12517 #endif
12518
12519 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
12520 ("%s: unexpected I/O type %x", __func__, io->io_hdr.io_type));
12521
12522 #ifdef CTL_TIME_IO
12523 getbinuptime(&cur_bt);
12524 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12525 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12526 #endif
12527 io->io_hdr.num_dmas++;
12528
12529 if ((io->io_hdr.port_status != 0) &&
12530 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
12531 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
12532 ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1,
12533 /*retry_count*/ io->io_hdr.port_status);
12534 } else if (io->scsiio.kern_data_resid != 0 &&
12535 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT &&
12536 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
12537 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
12538 ctl_set_invalid_field_ciu(&io->scsiio);
12539 } else if (ctl_debug & CTL_DEBUG_CDB_DATA)
12540 ctl_data_print(io);
12541 }
12542
12543 void
12544 ctl_datamove_done(union ctl_io *io, bool samethr)
12545 {
12546
12547 ctl_datamove_done_process(io);
12548 io->scsiio.be_move_done(io, samethr);
12549 }
12550
12551 void
12552 ctl_datamove(union ctl_io *io)
12553 {
12554 void (*fe_datamove)(union ctl_io *io);
12555
12556 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12557
12558 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12559
12560 /* No data transferred yet. Frontend must update this when done. */
12561 io->scsiio.kern_data_resid = io->scsiio.kern_data_len;
12562
12563 #ifdef CTL_TIME_IO
12564 getbinuptime(&io->io_hdr.dma_start_bt);
12565 #endif /* CTL_TIME_IO */
12566
12567 #ifdef CTL_IO_DELAY
12568 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12569 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12570 } else {
12571 struct ctl_lun *lun;
12572
12573 lun = CTL_LUN(io);
12574 if ((lun != NULL)
12575 && (lun->delay_info.datamove_delay > 0)) {
12576 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12577 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12578 callout_reset(&io->io_hdr.delay_callout,
12579 lun->delay_info.datamove_delay * hz,
12580 ctl_datamove_timer_wakeup, io);
12581 if (lun->delay_info.datamove_type ==
12582 CTL_DELAY_TYPE_ONESHOT)
12583 lun->delay_info.datamove_delay = 0;
12584 return;
12585 }
12586 }
12587 #endif
12588
12589 /*
12590 * This command has been aborted. Set the port status, so we fail
12591 * the data move.
12592 */
12593 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12594 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12595 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12596 io->io_hdr.nexus.targ_port,
12597 io->io_hdr.nexus.targ_lun);
12598 io->io_hdr.port_status = 31337;
12599 ctl_datamove_done_process(io);
12600 io->scsiio.be_move_done(io, true);
12601 return;
12602 }
12603
12604 /* Don't confuse frontend with zero length data move. */
12605 if (io->scsiio.kern_data_len == 0) {
12606 ctl_datamove_done_process(io);
12607 io->scsiio.be_move_done(io, true);
12608 return;
12609 }
12610
12611 fe_datamove = CTL_PORT(io)->fe_datamove;
12612 fe_datamove(io);
12613 }
12614
12615 static void
12616 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12617 {
12618 union ctl_ha_msg msg;
12619 #ifdef CTL_TIME_IO
12620 struct bintime cur_bt;
12621 #endif
12622
12623 memset(&msg, 0, sizeof(msg));
12624 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12625 msg.hdr.original_sc = io;
12626 msg.hdr.serializing_sc = io->io_hdr.remote_io;
12627 msg.hdr.nexus = io->io_hdr.nexus;
12628 msg.hdr.status = io->io_hdr.status;
12629 msg.scsi.kern_data_resid = io->scsiio.kern_data_resid;
12630 msg.scsi.tag_num = io->scsiio.tag_num;
12631 msg.scsi.tag_type = io->scsiio.tag_type;
12632 msg.scsi.scsi_status = io->scsiio.scsi_status;
12633 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12634 io->scsiio.sense_len);
12635 msg.scsi.sense_len = io->scsiio.sense_len;
12636 msg.scsi.port_status = io->io_hdr.port_status;
12637 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12638 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12639 ctl_failover_io(io, /*have_lock*/ have_lock);
12640 return;
12641 }
12642 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12643 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12644 msg.scsi.sense_len, M_WAITOK);
12645
12646 #ifdef CTL_TIME_IO
12647 getbinuptime(&cur_bt);
12648 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12649 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12650 #endif
12651 io->io_hdr.num_dmas++;
12652 }
12653
12654 /*
12655 * The DMA to the remote side is done, now we need to tell the other side
12656 * we're done so it can continue with its data movement.
12657 */
12658 static void
12659 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12660 {
12661 union ctl_io *io;
12662 uint32_t i;
12663
12664 io = rq->context;
12665
12666 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12667 printf("%s: ISC DMA write failed with error %d", __func__,
12668 rq->ret);
12669 ctl_set_internal_failure(&io->scsiio,
12670 /*sks_valid*/ 1,
12671 /*retry_count*/ rq->ret);
12672 }
12673
12674 ctl_dt_req_free(rq);
12675
12676 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12677 free(CTL_LSGLT(io)[i].addr, M_CTL);
12678 free(CTL_RSGL(io), M_CTL);
12679 CTL_RSGL(io) = NULL;
12680 CTL_LSGL(io) = NULL;
12681
12682 /*
12683 * The data is in local and remote memory, so now we need to send
12684 * status (good or back) back to the other side.
12685 */
12686 ctl_send_datamove_done(io, /*have_lock*/ 0);
12687 }
12688
12689 /*
12690 * We've moved the data from the host/controller into local memory. Now we
12691 * need to push it over to the remote controller's memory.
12692 */
12693 static int
12694 ctl_datamove_remote_dm_write_cb(union ctl_io *io, bool samethr)
12695 {
12696 int retval;
12697
12698 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12699 ctl_datamove_remote_write_cb);
12700 return (retval);
12701 }
12702
12703 static void
12704 ctl_datamove_remote_write(union ctl_io *io)
12705 {
12706 int retval;
12707 void (*fe_datamove)(union ctl_io *io);
12708
12709 /*
12710 * - Get the data from the host/HBA into local memory.
12711 * - DMA memory from the local controller to the remote controller.
12712 * - Send status back to the remote controller.
12713 */
12714
12715 retval = ctl_datamove_remote_sgl_setup(io);
12716 if (retval != 0)
12717 return;
12718
12719 /* Switch the pointer over so the FETD knows what to do */
12720 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io);
12721
12722 /*
12723 * Use a custom move done callback, since we need to send completion
12724 * back to the other controller, not to the backend on this side.
12725 */
12726 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12727
12728 fe_datamove = CTL_PORT(io)->fe_datamove;
12729 fe_datamove(io);
12730 }
12731
12732 static int
12733 ctl_datamove_remote_dm_read_cb(union ctl_io *io, bool samethr)
12734 {
12735 uint32_t i;
12736
12737 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12738 free(CTL_LSGLT(io)[i].addr, M_CTL);
12739 free(CTL_RSGL(io), M_CTL);
12740 CTL_RSGL(io) = NULL;
12741 CTL_LSGL(io) = NULL;
12742
12743 /*
12744 * The read is done, now we need to send status (good or bad) back
12745 * to the other side.
12746 */
12747 ctl_send_datamove_done(io, /*have_lock*/ 0);
12748
12749 return (0);
12750 }
12751
12752 static void
12753 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12754 {
12755 union ctl_io *io;
12756 void (*fe_datamove)(union ctl_io *io);
12757
12758 io = rq->context;
12759
12760 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12761 printf("%s: ISC DMA read failed with error %d\n", __func__,
12762 rq->ret);
12763 ctl_set_internal_failure(&io->scsiio,
12764 /*sks_valid*/ 1,
12765 /*retry_count*/ rq->ret);
12766 }
12767
12768 ctl_dt_req_free(rq);
12769
12770 /* Switch the pointer over so the FETD knows what to do */
12771 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io);
12772
12773 /*
12774 * Use a custom move done callback, since we need to send completion
12775 * back to the other controller, not to the backend on this side.
12776 */
12777 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12778
12779 /* XXX KDM add checks like the ones in ctl_datamove? */
12780
12781 fe_datamove = CTL_PORT(io)->fe_datamove;
12782 fe_datamove(io);
12783 }
12784
12785 static int
12786 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12787 {
12788 struct ctl_sg_entry *local_sglist;
12789 uint32_t len_to_go;
12790 int retval;
12791 int i;
12792
12793 retval = 0;
12794 local_sglist = CTL_LSGL(io);
12795 len_to_go = io->scsiio.kern_data_len;
12796
12797 /*
12798 * The difficult thing here is that the size of the various
12799 * S/G segments may be different than the size from the
12800 * remote controller. That'll make it harder when DMAing
12801 * the data back to the other side.
12802 */
12803 for (i = 0; len_to_go > 0; i++) {
12804 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12805 local_sglist[i].addr =
12806 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12807
12808 len_to_go -= local_sglist[i].len;
12809 }
12810 /*
12811 * Reset the number of S/G entries accordingly. The original
12812 * number of S/G entries is available in rem_sg_entries.
12813 */
12814 io->scsiio.kern_sg_entries = i;
12815
12816 return (retval);
12817 }
12818
12819 static int
12820 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12821 ctl_ha_dt_cb callback)
12822 {
12823 struct ctl_ha_dt_req *rq;
12824 struct ctl_sg_entry *remote_sglist, *local_sglist;
12825 uint32_t local_used, remote_used, total_used;
12826 int i, j, isc_ret;
12827
12828 rq = ctl_dt_req_alloc();
12829
12830 /*
12831 * If we failed to allocate the request, and if the DMA didn't fail
12832 * anyway, set busy status. This is just a resource allocation
12833 * failure.
12834 */
12835 if ((rq == NULL)
12836 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12837 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12838 ctl_set_busy(&io->scsiio);
12839
12840 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12841 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12842 if (rq != NULL)
12843 ctl_dt_req_free(rq);
12844
12845 /*
12846 * The data move failed. We need to return status back
12847 * to the other controller. No point in trying to DMA
12848 * data to the remote controller.
12849 */
12850
12851 ctl_send_datamove_done(io, /*have_lock*/ 0);
12852
12853 return (1);
12854 }
12855
12856 local_sglist = CTL_LSGL(io);
12857 remote_sglist = CTL_RSGL(io);
12858 local_used = 0;
12859 remote_used = 0;
12860 total_used = 0;
12861
12862 /*
12863 * Pull/push the data over the wire from/to the other controller.
12864 * This takes into account the possibility that the local and
12865 * remote sglists may not be identical in terms of the size of
12866 * the elements and the number of elements.
12867 *
12868 * One fundamental assumption here is that the length allocated for
12869 * both the local and remote sglists is identical. Otherwise, we've
12870 * essentially got a coding error of some sort.
12871 */
12872 isc_ret = CTL_HA_STATUS_SUCCESS;
12873 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12874 uint32_t cur_len;
12875 uint8_t *tmp_ptr;
12876
12877 rq->command = command;
12878 rq->context = io;
12879
12880 /*
12881 * Both pointers should be aligned. But it is possible
12882 * that the allocation length is not. They should both
12883 * also have enough slack left over at the end, though,
12884 * to round up to the next 8 byte boundary.
12885 */
12886 cur_len = MIN(local_sglist[i].len - local_used,
12887 remote_sglist[j].len - remote_used);
12888 rq->size = cur_len;
12889
12890 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12891 tmp_ptr += local_used;
12892
12893 #if 0
12894 /* Use physical addresses when talking to ISC hardware */
12895 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12896 /* XXX KDM use busdma */
12897 rq->local = vtophys(tmp_ptr);
12898 } else
12899 rq->local = tmp_ptr;
12900 #else
12901 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12902 ("HA does not support BUS_ADDR"));
12903 rq->local = tmp_ptr;
12904 #endif
12905
12906 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12907 tmp_ptr += remote_used;
12908 rq->remote = tmp_ptr;
12909
12910 rq->callback = NULL;
12911
12912 local_used += cur_len;
12913 if (local_used >= local_sglist[i].len) {
12914 i++;
12915 local_used = 0;
12916 }
12917
12918 remote_used += cur_len;
12919 if (remote_used >= remote_sglist[j].len) {
12920 j++;
12921 remote_used = 0;
12922 }
12923 total_used += cur_len;
12924
12925 if (total_used >= io->scsiio.kern_data_len)
12926 rq->callback = callback;
12927
12928 isc_ret = ctl_dt_single(rq);
12929 if (isc_ret > CTL_HA_STATUS_SUCCESS)
12930 break;
12931 }
12932 if (isc_ret != CTL_HA_STATUS_WAIT) {
12933 rq->ret = isc_ret;
12934 callback(rq);
12935 }
12936
12937 return (0);
12938 }
12939
12940 static void
12941 ctl_datamove_remote_read(union ctl_io *io)
12942 {
12943 int retval;
12944 uint32_t i;
12945
12946 /*
12947 * This will send an error to the other controller in the case of a
12948 * failure.
12949 */
12950 retval = ctl_datamove_remote_sgl_setup(io);
12951 if (retval != 0)
12952 return;
12953
12954 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12955 ctl_datamove_remote_read_cb);
12956 if (retval != 0) {
12957 /*
12958 * Make sure we free memory if there was an error.. The
12959 * ctl_datamove_remote_xfer() function will send the
12960 * datamove done message, or call the callback with an
12961 * error if there is a problem.
12962 */
12963 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12964 free(CTL_LSGLT(io)[i].addr, M_CTL);
12965 free(CTL_RSGL(io), M_CTL);
12966 CTL_RSGL(io) = NULL;
12967 CTL_LSGL(io) = NULL;
12968 }
12969 }
12970
12971 /*
12972 * Process a datamove request from the other controller. This is used for
12973 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12974 * first. Once that is complete, the data gets DMAed into the remote
12975 * controller's memory. For reads, we DMA from the remote controller's
12976 * memory into our memory first, and then move it out to the FETD.
12977 */
12978 static void
12979 ctl_datamove_remote(union ctl_io *io)
12980 {
12981
12982 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12983
12984 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12985 ctl_failover_io(io, /*have_lock*/ 0);
12986 return;
12987 }
12988
12989 /*
12990 * Note that we look for an aborted I/O here, but don't do some of
12991 * the other checks that ctl_datamove() normally does.
12992 * We don't need to run the datamove delay code, since that should
12993 * have been done if need be on the other controller.
12994 */
12995 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12996 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
12997 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12998 io->io_hdr.nexus.targ_port,
12999 io->io_hdr.nexus.targ_lun);
13000 io->io_hdr.port_status = 31338;
13001 ctl_send_datamove_done(io, /*have_lock*/ 0);
13002 return;
13003 }
13004
13005 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
13006 ctl_datamove_remote_write(io);
13007 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
13008 ctl_datamove_remote_read(io);
13009 else {
13010 io->io_hdr.port_status = 31339;
13011 ctl_send_datamove_done(io, /*have_lock*/ 0);
13012 }
13013 }
13014
13015 static void
13016 ctl_process_done(union ctl_io *io)
13017 {
13018 struct ctl_softc *softc = CTL_SOFTC(io);
13019 struct ctl_port *port = CTL_PORT(io);
13020 struct ctl_lun *lun = CTL_LUN(io);
13021 void (*fe_done)(union ctl_io *io);
13022 union ctl_ha_msg msg;
13023
13024 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13025 fe_done = port->fe_done;
13026
13027 #ifdef CTL_TIME_IO
13028 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13029 char str[256];
13030 char path_str[64];
13031 struct sbuf sb;
13032
13033 ctl_scsi_path_string(io, path_str, sizeof(path_str));
13034 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13035
13036 sbuf_cat(&sb, path_str);
13037 switch (io->io_hdr.io_type) {
13038 case CTL_IO_SCSI:
13039 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
13040 sbuf_printf(&sb, "\n");
13041 sbuf_cat(&sb, path_str);
13042 sbuf_printf(&sb, "Tag: 0x%04x/%d, Prio: %d\n",
13043 io->scsiio.tag_num, io->scsiio.tag_type,
13044 io->scsiio.priority);
13045 break;
13046 case CTL_IO_TASK:
13047 sbuf_printf(&sb, "Task Action: %d Tag: 0x%04x/%d\n",
13048 io->taskio.task_action,
13049 io->taskio.tag_num, io->taskio.tag_type);
13050 break;
13051 default:
13052 panic("%s: Invalid CTL I/O type %d\n",
13053 __func__, io->io_hdr.io_type);
13054 }
13055 sbuf_cat(&sb, path_str);
13056 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13057 (intmax_t)time_uptime - io->io_hdr.start_time);
13058 sbuf_finish(&sb);
13059 printf("%s", sbuf_data(&sb));
13060 }
13061 #endif /* CTL_TIME_IO */
13062
13063 switch (io->io_hdr.io_type) {
13064 case CTL_IO_SCSI:
13065 break;
13066 case CTL_IO_TASK:
13067 if (ctl_debug & CTL_DEBUG_INFO)
13068 ctl_io_error_print(io, NULL);
13069 fe_done(io);
13070 return;
13071 default:
13072 panic("%s: Invalid CTL I/O type %d\n",
13073 __func__, io->io_hdr.io_type);
13074 }
13075
13076 if (lun == NULL) {
13077 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13078 io->io_hdr.nexus.targ_mapped_lun));
13079 goto bailout;
13080 }
13081
13082 mtx_lock(&lun->lun_lock);
13083
13084 /*
13085 * Check to see if we have any informational exception and status
13086 * of this command can be modified to report it in form of either
13087 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field.
13088 */
13089 if (lun->ie_reported == 0 && lun->ie_asc != 0 &&
13090 io->io_hdr.status == CTL_SUCCESS &&
13091 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) {
13092 uint8_t mrie = lun->MODE_IE.mrie;
13093 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) ||
13094 (lun->MODE_VER.byte3 & SMS_VER_PER));
13095 if (((mrie == SIEP_MRIE_REC_COND && per) ||
13096 mrie == SIEP_MRIE_REC_UNCOND ||
13097 mrie == SIEP_MRIE_NO_SENSE) &&
13098 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags &
13099 CTL_CMD_FLAG_NO_SENSE) == 0) {
13100 ctl_set_sense(&io->scsiio,
13101 /*current_error*/ 1,
13102 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ?
13103 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR,
13104 /*asc*/ lun->ie_asc,
13105 /*ascq*/ lun->ie_ascq,
13106 SSD_ELEM_NONE);
13107 lun->ie_reported = 1;
13108 }
13109 } else if (lun->ie_reported < 0)
13110 lun->ie_reported = 0;
13111
13112 /*
13113 * Check to see if we have any errors to inject here. We only
13114 * inject errors for commands that don't already have errors set.
13115 */
13116 if (!STAILQ_EMPTY(&lun->error_list) &&
13117 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
13118 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
13119 ctl_inject_error(lun, io);
13120
13121 /*
13122 * XXX KDM how do we treat commands that aren't completed
13123 * successfully?
13124 *
13125 * XXX KDM should we also track I/O latency?
13126 */
13127 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13128 io->io_hdr.io_type == CTL_IO_SCSI) {
13129 int type;
13130 #ifdef CTL_TIME_IO
13131 struct bintime bt;
13132
13133 getbinuptime(&bt);
13134 bintime_sub(&bt, &io->io_hdr.start_bt);
13135 #endif
13136 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13137 CTL_FLAG_DATA_IN)
13138 type = CTL_STATS_READ;
13139 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13140 CTL_FLAG_DATA_OUT)
13141 type = CTL_STATS_WRITE;
13142 else
13143 type = CTL_STATS_NO_IO;
13144
13145 lun->stats.bytes[type] += io->scsiio.kern_total_len;
13146 lun->stats.operations[type] ++;
13147 lun->stats.dmas[type] += io->io_hdr.num_dmas;
13148 #ifdef CTL_TIME_IO
13149 bintime_add(&lun->stats.dma_time[type], &io->io_hdr.dma_bt);
13150 bintime_add(&lun->stats.time[type], &bt);
13151 #endif
13152
13153 mtx_lock(&port->port_lock);
13154 port->stats.bytes[type] += io->scsiio.kern_total_len;
13155 port->stats.operations[type] ++;
13156 port->stats.dmas[type] += io->io_hdr.num_dmas;
13157 #ifdef CTL_TIME_IO
13158 bintime_add(&port->stats.dma_time[type], &io->io_hdr.dma_bt);
13159 bintime_add(&port->stats.time[type], &bt);
13160 #endif
13161 mtx_unlock(&port->port_lock);
13162 }
13163
13164 /*
13165 * Run through the blocked queue of this I/O and see if anything
13166 * can be unblocked, now that this I/O is done and will be removed.
13167 * We need to do it before removal to have OOA position to start.
13168 */
13169 ctl_try_unblock_others(lun, io, TRUE);
13170
13171 /*
13172 * Remove this from the OOA queue.
13173 */
13174 LIST_REMOVE(&io->io_hdr, ooa_links);
13175 #ifdef CTL_TIME_IO
13176 if (LIST_EMPTY(&lun->ooa_queue))
13177 lun->last_busy = getsbinuptime();
13178 #endif
13179
13180 /*
13181 * If the LUN has been invalidated, free it if there is nothing
13182 * left on its OOA queue.
13183 */
13184 if ((lun->flags & CTL_LUN_INVALID)
13185 && LIST_EMPTY(&lun->ooa_queue)) {
13186 mtx_unlock(&lun->lun_lock);
13187 ctl_free_lun(lun);
13188 } else
13189 mtx_unlock(&lun->lun_lock);
13190
13191 bailout:
13192
13193 /*
13194 * If this command has been aborted, make sure we set the status
13195 * properly. The FETD is responsible for freeing the I/O and doing
13196 * whatever it needs to do to clean up its state.
13197 */
13198 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13199 ctl_set_task_aborted(&io->scsiio);
13200
13201 /*
13202 * If enabled, print command error status.
13203 */
13204 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
13205 (ctl_debug & CTL_DEBUG_INFO) != 0)
13206 ctl_io_error_print(io, NULL);
13207
13208 /*
13209 * Tell the FETD or the other shelf controller we're done with this
13210 * command. Note that only SCSI commands get to this point. Task
13211 * management commands are completed above.
13212 */
13213 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
13214 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
13215 memset(&msg, 0, sizeof(msg));
13216 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13217 msg.hdr.serializing_sc = io->io_hdr.remote_io;
13218 msg.hdr.nexus = io->io_hdr.nexus;
13219 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13220 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
13221 M_WAITOK);
13222 }
13223
13224 fe_done(io);
13225 }
13226
13227 /*
13228 * Front end should call this if it doesn't do autosense. When the request
13229 * sense comes back in from the initiator, we'll dequeue this and send it.
13230 */
13231 int
13232 ctl_queue_sense(union ctl_io *io)
13233 {
13234 struct ctl_softc *softc = CTL_SOFTC(io);
13235 struct ctl_port *port = CTL_PORT(io);
13236 struct ctl_lun *lun;
13237 struct scsi_sense_data *ps;
13238 uint32_t initidx, p, targ_lun;
13239
13240 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13241
13242 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13243
13244 /*
13245 * LUN lookup will likely move to the ctl_work_thread() once we
13246 * have our new queueing infrastructure (that doesn't put things on
13247 * a per-LUN queue initially). That is so that we can handle
13248 * things like an INQUIRY to a LUN that we don't have enabled. We
13249 * can't deal with that right now.
13250 * If we don't have a LUN for this, just toss the sense information.
13251 */
13252 mtx_lock(&softc->ctl_lock);
13253 if (targ_lun >= ctl_max_luns ||
13254 (lun = softc->ctl_luns[targ_lun]) == NULL) {
13255 mtx_unlock(&softc->ctl_lock);
13256 goto bailout;
13257 }
13258 mtx_lock(&lun->lun_lock);
13259 mtx_unlock(&softc->ctl_lock);
13260
13261 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13262 p = initidx / CTL_MAX_INIT_PER_PORT;
13263 if (lun->pending_sense[p] == NULL) {
13264 lun->pending_sense[p] = malloc(sizeof(*ps) * CTL_MAX_INIT_PER_PORT,
13265 M_CTL, M_NOWAIT | M_ZERO);
13266 }
13267 if ((ps = lun->pending_sense[p]) != NULL) {
13268 ps += initidx % CTL_MAX_INIT_PER_PORT;
13269 memset(ps, 0, sizeof(*ps));
13270 memcpy(ps, &io->scsiio.sense_data, io->scsiio.sense_len);
13271 }
13272 mtx_unlock(&lun->lun_lock);
13273
13274 bailout:
13275 ctl_free_io(io);
13276 return (CTL_RETVAL_COMPLETE);
13277 }
13278
13279 /*
13280 * Primary command inlet from frontend ports. All SCSI and task I/O
13281 * requests must go through this function.
13282 */
13283 int
13284 ctl_queue(union ctl_io *io)
13285 {
13286 struct ctl_port *port = CTL_PORT(io);
13287
13288 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13289
13290 #ifdef CTL_TIME_IO
13291 io->io_hdr.start_time = time_uptime;
13292 getbinuptime(&io->io_hdr.start_bt);
13293 #endif /* CTL_TIME_IO */
13294
13295 /* Map FE-specific LUN ID into global one. */
13296 io->io_hdr.nexus.targ_mapped_lun =
13297 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13298
13299 switch (io->io_hdr.io_type) {
13300 case CTL_IO_SCSI:
13301 case CTL_IO_TASK:
13302 if (ctl_debug & CTL_DEBUG_CDB)
13303 ctl_io_print(io);
13304 ctl_enqueue_incoming(io);
13305 break;
13306 default:
13307 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13308 return (EINVAL);
13309 }
13310
13311 return (CTL_RETVAL_COMPLETE);
13312 }
13313
13314 int
13315 ctl_run(union ctl_io *io)
13316 {
13317 struct ctl_port *port = CTL_PORT(io);
13318
13319 CTL_DEBUG_PRINT(("ctl_run cdb[0]=%02X\n", io->scsiio.cdb[0]));
13320
13321 #ifdef CTL_TIME_IO
13322 io->io_hdr.start_time = time_uptime;
13323 getbinuptime(&io->io_hdr.start_bt);
13324 #endif /* CTL_TIME_IO */
13325
13326 /* Map FE-specific LUN ID into global one. */
13327 io->io_hdr.nexus.targ_mapped_lun =
13328 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13329
13330 switch (io->io_hdr.io_type) {
13331 case CTL_IO_SCSI:
13332 if (ctl_debug & CTL_DEBUG_CDB)
13333 ctl_io_print(io);
13334 ctl_scsiio_precheck(&io->scsiio);
13335 break;
13336 case CTL_IO_TASK:
13337 if (ctl_debug & CTL_DEBUG_CDB)
13338 ctl_io_print(io);
13339 ctl_run_task(io);
13340 break;
13341 default:
13342 printf("ctl_run: unknown I/O type %d\n", io->io_hdr.io_type);
13343 return (EINVAL);
13344 }
13345
13346 return (CTL_RETVAL_COMPLETE);
13347 }
13348
13349 #ifdef CTL_IO_DELAY
13350 static void
13351 ctl_done_timer_wakeup(void *arg)
13352 {
13353 union ctl_io *io;
13354
13355 io = (union ctl_io *)arg;
13356 ctl_done(io);
13357 }
13358 #endif /* CTL_IO_DELAY */
13359
13360 void
13361 ctl_serseq_done(union ctl_io *io)
13362 {
13363 struct ctl_lun *lun = CTL_LUN(io);
13364
13365 /* This is racy, but should not be a problem. */
13366 if (!TAILQ_EMPTY(&io->io_hdr.blocked_queue)) {
13367 mtx_lock(&lun->lun_lock);
13368 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13369 ctl_try_unblock_others(lun, io, FALSE);
13370 mtx_unlock(&lun->lun_lock);
13371 } else
13372 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13373 }
13374
13375 void
13376 ctl_done(union ctl_io *io)
13377 {
13378
13379 /*
13380 * Enable this to catch duplicate completion issues.
13381 */
13382 #if 0
13383 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13384 printf("%s: type %d msg %d cdb %x iptl: "
13385 "%u:%u:%u tag 0x%04x "
13386 "flag %#x status %x\n",
13387 __func__,
13388 io->io_hdr.io_type,
13389 io->io_hdr.msg_type,
13390 io->scsiio.cdb[0],
13391 io->io_hdr.nexus.initid,
13392 io->io_hdr.nexus.targ_port,
13393 io->io_hdr.nexus.targ_lun,
13394 (io->io_hdr.io_type ==
13395 CTL_IO_TASK) ?
13396 io->taskio.tag_num :
13397 io->scsiio.tag_num,
13398 io->io_hdr.flags,
13399 io->io_hdr.status);
13400 } else
13401 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13402 #endif
13403
13404 /*
13405 * This is an internal copy of an I/O, and should not go through
13406 * the normal done processing logic.
13407 */
13408 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13409 return;
13410
13411 #ifdef CTL_IO_DELAY
13412 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13413 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13414 } else {
13415 struct ctl_lun *lun = CTL_LUN(io);
13416
13417 if ((lun != NULL)
13418 && (lun->delay_info.done_delay > 0)) {
13419 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13420 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13421 callout_reset(&io->io_hdr.delay_callout,
13422 lun->delay_info.done_delay * hz,
13423 ctl_done_timer_wakeup, io);
13424 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13425 lun->delay_info.done_delay = 0;
13426 return;
13427 }
13428 }
13429 #endif /* CTL_IO_DELAY */
13430
13431 ctl_enqueue_done(io);
13432 }
13433
13434 static void
13435 ctl_work_thread(void *arg)
13436 {
13437 struct ctl_thread *thr = (struct ctl_thread *)arg;
13438 struct ctl_softc *softc = thr->ctl_softc;
13439 union ctl_io *io;
13440 int retval;
13441
13442 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13443 thread_lock(curthread);
13444 sched_prio(curthread, PUSER - 1);
13445 thread_unlock(curthread);
13446
13447 while (!softc->shutdown) {
13448 /*
13449 * We handle the queues in this order:
13450 * - ISC
13451 * - done queue (to free up resources, unblock other commands)
13452 * - incoming queue
13453 * - RtR queue
13454 *
13455 * If those queues are empty, we break out of the loop and
13456 * go to sleep.
13457 */
13458 mtx_lock(&thr->queue_lock);
13459 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13460 if (io != NULL) {
13461 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13462 mtx_unlock(&thr->queue_lock);
13463 ctl_handle_isc(io);
13464 continue;
13465 }
13466 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13467 if (io != NULL) {
13468 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13469 /* clear any blocked commands, call fe_done */
13470 mtx_unlock(&thr->queue_lock);
13471 ctl_process_done(io);
13472 continue;
13473 }
13474 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13475 if (io != NULL) {
13476 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13477 mtx_unlock(&thr->queue_lock);
13478 if (io->io_hdr.io_type == CTL_IO_TASK)
13479 ctl_run_task(io);
13480 else
13481 ctl_scsiio_precheck(&io->scsiio);
13482 continue;
13483 }
13484 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13485 if (io != NULL) {
13486 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13487 mtx_unlock(&thr->queue_lock);
13488 retval = ctl_scsiio(&io->scsiio);
13489 if (retval != CTL_RETVAL_COMPLETE)
13490 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13491 continue;
13492 }
13493
13494 /* Sleep until we have something to do. */
13495 mtx_sleep(thr, &thr->queue_lock, PDROP, "-", 0);
13496 }
13497 thr->thread = NULL;
13498 kthread_exit();
13499 }
13500
13501 static void
13502 ctl_thresh_thread(void *arg)
13503 {
13504 struct ctl_softc *softc = (struct ctl_softc *)arg;
13505 struct ctl_lun *lun;
13506 struct ctl_logical_block_provisioning_page *page;
13507 const char *attr;
13508 union ctl_ha_msg msg;
13509 uint64_t thres, val;
13510 int i, e, set;
13511
13512 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13513 thread_lock(curthread);
13514 sched_prio(curthread, PUSER - 1);
13515 thread_unlock(curthread);
13516
13517 while (!softc->shutdown) {
13518 mtx_lock(&softc->ctl_lock);
13519 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13520 if ((lun->flags & CTL_LUN_DISABLED) ||
13521 (lun->flags & CTL_LUN_NO_MEDIA) ||
13522 lun->backend->lun_attr == NULL)
13523 continue;
13524 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13525 softc->ha_mode == CTL_HA_MODE_XFER)
13526 continue;
13527 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0)
13528 continue;
13529 e = 0;
13530 page = &lun->MODE_LBP;
13531 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13532 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13533 continue;
13534 thres = scsi_4btoul(page->descr[i].count);
13535 thres <<= CTL_LBP_EXPONENT;
13536 switch (page->descr[i].resource) {
13537 case 0x01:
13538 attr = "blocksavail";
13539 break;
13540 case 0x02:
13541 attr = "blocksused";
13542 break;
13543 case 0xf1:
13544 attr = "poolblocksavail";
13545 break;
13546 case 0xf2:
13547 attr = "poolblocksused";
13548 break;
13549 default:
13550 continue;
13551 }
13552 mtx_unlock(&softc->ctl_lock); // XXX
13553 val = lun->backend->lun_attr(lun->be_lun, attr);
13554 mtx_lock(&softc->ctl_lock);
13555 if (val == UINT64_MAX)
13556 continue;
13557 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13558 == SLBPPD_ARMING_INC)
13559 e = (val >= thres);
13560 else
13561 e = (val <= thres);
13562 if (e)
13563 break;
13564 }
13565 mtx_lock(&lun->lun_lock);
13566 if (e) {
13567 scsi_u64to8b((uint8_t *)&page->descr[i] -
13568 (uint8_t *)page, lun->ua_tpt_info);
13569 if (lun->lasttpt == 0 ||
13570 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13571 lun->lasttpt = time_uptime;
13572 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13573 set = 1;
13574 } else
13575 set = 0;
13576 } else {
13577 lun->lasttpt = 0;
13578 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13579 set = -1;
13580 }
13581 mtx_unlock(&lun->lun_lock);
13582 if (set != 0 &&
13583 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13584 /* Send msg to other side. */
13585 bzero(&msg.ua, sizeof(msg.ua));
13586 msg.hdr.msg_type = CTL_MSG_UA;
13587 msg.hdr.nexus.initid = -1;
13588 msg.hdr.nexus.targ_port = -1;
13589 msg.hdr.nexus.targ_lun = lun->lun;
13590 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13591 msg.ua.ua_all = 1;
13592 msg.ua.ua_set = (set > 0);
13593 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13594 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
13595 mtx_unlock(&softc->ctl_lock); // XXX
13596 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13597 sizeof(msg.ua), M_WAITOK);
13598 mtx_lock(&softc->ctl_lock);
13599 }
13600 }
13601 mtx_sleep(&softc->thresh_thread, &softc->ctl_lock,
13602 PDROP, "-", CTL_LBP_PERIOD * hz);
13603 }
13604 softc->thresh_thread = NULL;
13605 kthread_exit();
13606 }
13607
13608 static void
13609 ctl_enqueue_incoming(union ctl_io *io)
13610 {
13611 struct ctl_softc *softc = CTL_SOFTC(io);
13612 struct ctl_thread *thr;
13613 u_int idx;
13614
13615 idx = (io->io_hdr.nexus.targ_port * 127 +
13616 io->io_hdr.nexus.initid) % worker_threads;
13617 thr = &softc->threads[idx];
13618 mtx_lock(&thr->queue_lock);
13619 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13620 mtx_unlock(&thr->queue_lock);
13621 wakeup(thr);
13622 }
13623
13624 static void
13625 ctl_enqueue_rtr(union ctl_io *io)
13626 {
13627 struct ctl_softc *softc = CTL_SOFTC(io);
13628 struct ctl_thread *thr;
13629
13630 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13631 mtx_lock(&thr->queue_lock);
13632 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13633 mtx_unlock(&thr->queue_lock);
13634 wakeup(thr);
13635 }
13636
13637 static void
13638 ctl_enqueue_done(union ctl_io *io)
13639 {
13640 struct ctl_softc *softc = CTL_SOFTC(io);
13641 struct ctl_thread *thr;
13642
13643 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13644 mtx_lock(&thr->queue_lock);
13645 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13646 mtx_unlock(&thr->queue_lock);
13647 wakeup(thr);
13648 }
13649
13650 static void
13651 ctl_enqueue_isc(union ctl_io *io)
13652 {
13653 struct ctl_softc *softc = CTL_SOFTC(io);
13654 struct ctl_thread *thr;
13655
13656 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13657 mtx_lock(&thr->queue_lock);
13658 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13659 mtx_unlock(&thr->queue_lock);
13660 wakeup(thr);
13661 }
13662
13663 /*
13664 * vim: ts=8
13665 */
13666