1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
5 * Copyright (c) 2012 The FreeBSD Foundation
6 * Copyright (c) 2014-2017 Alexander Motin <[email protected]>
7 * Copyright (c) 2017 Jakub Wojciech Klama <[email protected]>
8 * Copyright (c) 2018 Marcelo Araujo <[email protected]>
9 * All rights reserved.
10 *
11 * Portions of this software were developed by Edward Tomasz Napierala
12 * under sponsorship from the FreeBSD Foundation.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions, and the following disclaimer,
19 * without modification.
20 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
21 * substantially similar to the "NO WARRANTY" disclaimer below
22 * ("Disclaimer") and any redistribution must be conditioned upon
23 * including a substantially similar Disclaimer requirement for further
24 * binary redistribution.
25 *
26 * NO WARRANTY
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
35 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
36 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGES.
38 *
39 * $Id$
40 */
41 /*
42 * CAM Target Layer, a SCSI device emulation subsystem.
43 *
44 * Author: Ken Merry <[email protected]>
45 */
46
47 #include <sys/cdefs.h>
48 __FBSDID("$FreeBSD$");
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/ctype.h>
53 #include <sys/kernel.h>
54 #include <sys/types.h>
55 #include <sys/kthread.h>
56 #include <sys/bio.h>
57 #include <sys/fcntl.h>
58 #include <sys/lock.h>
59 #include <sys/module.h>
60 #include <sys/mutex.h>
61 #include <sys/condvar.h>
62 #include <sys/malloc.h>
63 #include <sys/conf.h>
64 #include <sys/ioccom.h>
65 #include <sys/queue.h>
66 #include <sys/sbuf.h>
67 #include <sys/smp.h>
68 #include <sys/endian.h>
69 #include <sys/proc.h>
70 #include <sys/sched.h>
71 #include <sys/sysctl.h>
72 #include <sys/nv.h>
73 #include <sys/dnv.h>
74 #include <vm/uma.h>
75
76 #include <cam/cam.h>
77 #include <cam/scsi/scsi_all.h>
78 #include <cam/scsi/scsi_cd.h>
79 #include <cam/scsi/scsi_da.h>
80 #include <cam/ctl/ctl_io.h>
81 #include <cam/ctl/ctl.h>
82 #include <cam/ctl/ctl_frontend.h>
83 #include <cam/ctl/ctl_util.h>
84 #include <cam/ctl/ctl_backend.h>
85 #include <cam/ctl/ctl_ioctl.h>
86 #include <cam/ctl/ctl_ha.h>
87 #include <cam/ctl/ctl_private.h>
88 #include <cam/ctl/ctl_debug.h>
89 #include <cam/ctl/ctl_scsi_all.h>
90 #include <cam/ctl/ctl_error.h>
91
92 struct ctl_softc *control_softc = NULL;
93
94 /*
95 * Template mode pages.
96 */
97
98 /*
99 * Note that these are default values only. The actual values will be
100 * filled in when the user does a mode sense.
101 */
102 const static struct scsi_da_rw_recovery_page rw_er_page_default = {
103 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
104 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
105 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
106 /*read_retry_count*/0,
107 /*correction_span*/0,
108 /*head_offset_count*/0,
109 /*data_strobe_offset_cnt*/0,
110 /*byte8*/SMS_RWER_LBPERE,
111 /*write_retry_count*/0,
112 /*reserved2*/0,
113 /*recovery_time_limit*/{0, 0},
114 };
115
116 const static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
117 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
118 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
119 /*byte3*/SMS_RWER_PER,
120 /*read_retry_count*/0,
121 /*correction_span*/0,
122 /*head_offset_count*/0,
123 /*data_strobe_offset_cnt*/0,
124 /*byte8*/SMS_RWER_LBPERE,
125 /*write_retry_count*/0,
126 /*reserved2*/0,
127 /*recovery_time_limit*/{0, 0},
128 };
129
130 const static struct scsi_format_page format_page_default = {
131 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
132 /*page_length*/sizeof(struct scsi_format_page) - 2,
133 /*tracks_per_zone*/ {0, 0},
134 /*alt_sectors_per_zone*/ {0, 0},
135 /*alt_tracks_per_zone*/ {0, 0},
136 /*alt_tracks_per_lun*/ {0, 0},
137 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
138 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
139 /*bytes_per_sector*/ {0, 0},
140 /*interleave*/ {0, 0},
141 /*track_skew*/ {0, 0},
142 /*cylinder_skew*/ {0, 0},
143 /*flags*/ SFP_HSEC,
144 /*reserved*/ {0, 0, 0}
145 };
146
147 const static struct scsi_format_page format_page_changeable = {
148 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
149 /*page_length*/sizeof(struct scsi_format_page) - 2,
150 /*tracks_per_zone*/ {0, 0},
151 /*alt_sectors_per_zone*/ {0, 0},
152 /*alt_tracks_per_zone*/ {0, 0},
153 /*alt_tracks_per_lun*/ {0, 0},
154 /*sectors_per_track*/ {0, 0},
155 /*bytes_per_sector*/ {0, 0},
156 /*interleave*/ {0, 0},
157 /*track_skew*/ {0, 0},
158 /*cylinder_skew*/ {0, 0},
159 /*flags*/ 0,
160 /*reserved*/ {0, 0, 0}
161 };
162
163 const static struct scsi_rigid_disk_page rigid_disk_page_default = {
164 /*page_code*/SMS_RIGID_DISK_PAGE,
165 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
166 /*cylinders*/ {0, 0, 0},
167 /*heads*/ CTL_DEFAULT_HEADS,
168 /*start_write_precomp*/ {0, 0, 0},
169 /*start_reduced_current*/ {0, 0, 0},
170 /*step_rate*/ {0, 0},
171 /*landing_zone_cylinder*/ {0, 0, 0},
172 /*rpl*/ SRDP_RPL_DISABLED,
173 /*rotational_offset*/ 0,
174 /*reserved1*/ 0,
175 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
176 CTL_DEFAULT_ROTATION_RATE & 0xff},
177 /*reserved2*/ {0, 0}
178 };
179
180 const static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
181 /*page_code*/SMS_RIGID_DISK_PAGE,
182 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
183 /*cylinders*/ {0, 0, 0},
184 /*heads*/ 0,
185 /*start_write_precomp*/ {0, 0, 0},
186 /*start_reduced_current*/ {0, 0, 0},
187 /*step_rate*/ {0, 0},
188 /*landing_zone_cylinder*/ {0, 0, 0},
189 /*rpl*/ 0,
190 /*rotational_offset*/ 0,
191 /*reserved1*/ 0,
192 /*rotation_rate*/ {0, 0},
193 /*reserved2*/ {0, 0}
194 };
195
196 const static struct scsi_da_verify_recovery_page verify_er_page_default = {
197 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
198 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
199 /*byte3*/0,
200 /*read_retry_count*/0,
201 /*reserved*/{ 0, 0, 0, 0, 0, 0 },
202 /*recovery_time_limit*/{0, 0},
203 };
204
205 const static struct scsi_da_verify_recovery_page verify_er_page_changeable = {
206 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
207 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
208 /*byte3*/SMS_VER_PER,
209 /*read_retry_count*/0,
210 /*reserved*/{ 0, 0, 0, 0, 0, 0 },
211 /*recovery_time_limit*/{0, 0},
212 };
213
214 const static struct scsi_caching_page caching_page_default = {
215 /*page_code*/SMS_CACHING_PAGE,
216 /*page_length*/sizeof(struct scsi_caching_page) - 2,
217 /*flags1*/ SCP_DISC | SCP_WCE,
218 /*ret_priority*/ 0,
219 /*disable_pf_transfer_len*/ {0xff, 0xff},
220 /*min_prefetch*/ {0, 0},
221 /*max_prefetch*/ {0xff, 0xff},
222 /*max_pf_ceiling*/ {0xff, 0xff},
223 /*flags2*/ 0,
224 /*cache_segments*/ 0,
225 /*cache_seg_size*/ {0, 0},
226 /*reserved*/ 0,
227 /*non_cache_seg_size*/ {0, 0, 0}
228 };
229
230 const static struct scsi_caching_page caching_page_changeable = {
231 /*page_code*/SMS_CACHING_PAGE,
232 /*page_length*/sizeof(struct scsi_caching_page) - 2,
233 /*flags1*/ SCP_WCE | SCP_RCD,
234 /*ret_priority*/ 0,
235 /*disable_pf_transfer_len*/ {0, 0},
236 /*min_prefetch*/ {0, 0},
237 /*max_prefetch*/ {0, 0},
238 /*max_pf_ceiling*/ {0, 0},
239 /*flags2*/ 0,
240 /*cache_segments*/ 0,
241 /*cache_seg_size*/ {0, 0},
242 /*reserved*/ 0,
243 /*non_cache_seg_size*/ {0, 0, 0}
244 };
245
246 const static struct scsi_control_page control_page_default = {
247 /*page_code*/SMS_CONTROL_MODE_PAGE,
248 /*page_length*/sizeof(struct scsi_control_page) - 2,
249 /*rlec*/0,
250 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
251 /*eca_and_aen*/0,
252 /*flags4*/SCP_TAS,
253 /*aen_holdoff_period*/{0, 0},
254 /*busy_timeout_period*/{0, 0},
255 /*extended_selftest_completion_time*/{0, 0}
256 };
257
258 const static struct scsi_control_page control_page_changeable = {
259 /*page_code*/SMS_CONTROL_MODE_PAGE,
260 /*page_length*/sizeof(struct scsi_control_page) - 2,
261 /*rlec*/SCP_DSENSE,
262 /*queue_flags*/SCP_QUEUE_ALG_MASK | SCP_NUAR,
263 /*eca_and_aen*/SCP_SWP,
264 /*flags4*/0,
265 /*aen_holdoff_period*/{0, 0},
266 /*busy_timeout_period*/{0, 0},
267 /*extended_selftest_completion_time*/{0, 0}
268 };
269
270 #define CTL_CEM_LEN (sizeof(struct scsi_control_ext_page) - 4)
271
272 const static struct scsi_control_ext_page control_ext_page_default = {
273 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
274 /*subpage_code*/0x01,
275 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
276 /*flags*/0,
277 /*prio*/0,
278 /*max_sense*/0
279 };
280
281 const static struct scsi_control_ext_page control_ext_page_changeable = {
282 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
283 /*subpage_code*/0x01,
284 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
285 /*flags*/0,
286 /*prio*/0,
287 /*max_sense*/0xff
288 };
289
290 const static struct scsi_info_exceptions_page ie_page_default = {
291 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
292 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
293 /*info_flags*/SIEP_FLAGS_EWASC,
294 /*mrie*/SIEP_MRIE_NO,
295 /*interval_timer*/{0, 0, 0, 0},
296 /*report_count*/{0, 0, 0, 1}
297 };
298
299 const static struct scsi_info_exceptions_page ie_page_changeable = {
300 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
301 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
302 /*info_flags*/SIEP_FLAGS_EWASC | SIEP_FLAGS_DEXCPT | SIEP_FLAGS_TEST |
303 SIEP_FLAGS_LOGERR,
304 /*mrie*/0x0f,
305 /*interval_timer*/{0xff, 0xff, 0xff, 0xff},
306 /*report_count*/{0xff, 0xff, 0xff, 0xff}
307 };
308
309 #define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4)
310
311 const static struct ctl_logical_block_provisioning_page lbp_page_default = {{
312 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
313 /*subpage_code*/0x02,
314 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
315 /*flags*/0,
316 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
317 /*descr*/{}},
318 {{/*flags*/0,
319 /*resource*/0x01,
320 /*reserved*/{0, 0},
321 /*count*/{0, 0, 0, 0}},
322 {/*flags*/0,
323 /*resource*/0x02,
324 /*reserved*/{0, 0},
325 /*count*/{0, 0, 0, 0}},
326 {/*flags*/0,
327 /*resource*/0xf1,
328 /*reserved*/{0, 0},
329 /*count*/{0, 0, 0, 0}},
330 {/*flags*/0,
331 /*resource*/0xf2,
332 /*reserved*/{0, 0},
333 /*count*/{0, 0, 0, 0}}
334 }
335 };
336
337 const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{
338 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
339 /*subpage_code*/0x02,
340 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
341 /*flags*/SLBPP_SITUA,
342 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
343 /*descr*/{}},
344 {{/*flags*/0,
345 /*resource*/0,
346 /*reserved*/{0, 0},
347 /*count*/{0, 0, 0, 0}},
348 {/*flags*/0,
349 /*resource*/0,
350 /*reserved*/{0, 0},
351 /*count*/{0, 0, 0, 0}},
352 {/*flags*/0,
353 /*resource*/0,
354 /*reserved*/{0, 0},
355 /*count*/{0, 0, 0, 0}},
356 {/*flags*/0,
357 /*resource*/0,
358 /*reserved*/{0, 0},
359 /*count*/{0, 0, 0, 0}}
360 }
361 };
362
363 const static struct scsi_cddvd_capabilities_page cddvd_page_default = {
364 /*page_code*/SMS_CDDVD_CAPS_PAGE,
365 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
366 /*caps1*/0x3f,
367 /*caps2*/0x00,
368 /*caps3*/0xf0,
369 /*caps4*/0x00,
370 /*caps5*/0x29,
371 /*caps6*/0x00,
372 /*obsolete*/{0, 0},
373 /*nvol_levels*/{0, 0},
374 /*buffer_size*/{8, 0},
375 /*obsolete2*/{0, 0},
376 /*reserved*/0,
377 /*digital*/0,
378 /*obsolete3*/0,
379 /*copy_management*/0,
380 /*reserved2*/0,
381 /*rotation_control*/0,
382 /*cur_write_speed*/0,
383 /*num_speed_descr*/0,
384 };
385
386 const static struct scsi_cddvd_capabilities_page cddvd_page_changeable = {
387 /*page_code*/SMS_CDDVD_CAPS_PAGE,
388 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
389 /*caps1*/0,
390 /*caps2*/0,
391 /*caps3*/0,
392 /*caps4*/0,
393 /*caps5*/0,
394 /*caps6*/0,
395 /*obsolete*/{0, 0},
396 /*nvol_levels*/{0, 0},
397 /*buffer_size*/{0, 0},
398 /*obsolete2*/{0, 0},
399 /*reserved*/0,
400 /*digital*/0,
401 /*obsolete3*/0,
402 /*copy_management*/0,
403 /*reserved2*/0,
404 /*rotation_control*/0,
405 /*cur_write_speed*/0,
406 /*num_speed_descr*/0,
407 };
408
409 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
410 static int worker_threads = -1;
411 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
412 &worker_threads, 1, "Number of worker threads");
413 static int ctl_debug = CTL_DEBUG_NONE;
414 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
415 &ctl_debug, 0, "Enabled debug flags");
416 static int ctl_lun_map_size = 1024;
417 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, lun_map_size, CTLFLAG_RWTUN,
418 &ctl_lun_map_size, 0, "Size of per-port LUN map (max LUN + 1)");
419 #ifdef CTL_TIME_IO
420 static int ctl_time_io_secs = CTL_TIME_IO_DEFAULT_SECS;
421 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, time_io_secs, CTLFLAG_RWTUN,
422 &ctl_time_io_secs, 0, "Log requests taking more seconds");
423 #endif
424
425 /*
426 * Maximum number of LUNs we support. MUST be a power of 2.
427 */
428 #define CTL_DEFAULT_MAX_LUNS 1024
429 static int ctl_max_luns = CTL_DEFAULT_MAX_LUNS;
430 TUNABLE_INT("kern.cam.ctl.max_luns", &ctl_max_luns);
431 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, max_luns, CTLFLAG_RDTUN,
432 &ctl_max_luns, CTL_DEFAULT_MAX_LUNS, "Maximum number of LUNs");
433
434 /*
435 * Maximum number of ports registered at one time.
436 */
437 #define CTL_DEFAULT_MAX_PORTS 256
438 static int ctl_max_ports = CTL_DEFAULT_MAX_PORTS;
439 TUNABLE_INT("kern.cam.ctl.max_ports", &ctl_max_ports);
440 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, max_ports, CTLFLAG_RDTUN,
441 &ctl_max_ports, CTL_DEFAULT_MAX_LUNS, "Maximum number of ports");
442
443 /*
444 * Maximum number of initiators we support.
445 */
446 #define CTL_MAX_INITIATORS (CTL_MAX_INIT_PER_PORT * ctl_max_ports)
447
448 /*
449 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
450 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
451 * SCSI Ports (0x88), Third-party Copy (0x8F), SCSI Feature Sets (0x92),
452 * Block limits (0xB0), Block Device Characteristics (0xB1) and
453 * Logical Block Provisioning (0xB2)
454 */
455 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 11
456
457 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
458 int param);
459 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
460 static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest);
461 static int ctl_init(void);
462 static int ctl_shutdown(void);
463 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
464 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
465 static void ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
466 static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
467 struct ctl_ooa *ooa_hdr,
468 struct ctl_ooa_entry *kern_entries);
469 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
470 struct thread *td);
471 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
472 struct ctl_be_lun *be_lun);
473 static int ctl_free_lun(struct ctl_lun *lun);
474 static void ctl_create_lun(struct ctl_be_lun *be_lun);
475
476 static int ctl_do_mode_select(union ctl_io *io);
477 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
478 uint64_t res_key, uint64_t sa_res_key,
479 uint8_t type, uint32_t residx,
480 struct ctl_scsiio *ctsio,
481 struct scsi_per_res_out *cdb,
482 struct scsi_per_res_out_parms* param);
483 static void ctl_pro_preempt_other(struct ctl_lun *lun,
484 union ctl_ha_msg *msg);
485 static void ctl_hndl_per_res_out_on_other_sc(union ctl_io *io);
486 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
487 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
488 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
489 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
490 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
491 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
492 int alloc_len);
493 static int ctl_inquiry_evpd_sfs(struct ctl_scsiio *ctsio, int alloc_len);
494 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
495 int alloc_len);
496 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
497 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
498 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
499 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
500 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
501 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2,
502 bool seq);
503 static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2);
504 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
505 union ctl_io *pending_io, union ctl_io *ooa_io);
506 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
507 union ctl_io **starting_io);
508 static void ctl_try_unblock_io(struct ctl_lun *lun, union ctl_io *io,
509 bool skip);
510 static void ctl_try_unblock_others(struct ctl_lun *lun, union ctl_io *io,
511 bool skip);
512 static int ctl_scsiio_lun_check(struct ctl_lun *lun,
513 const struct ctl_cmd_entry *entry,
514 struct ctl_scsiio *ctsio);
515 static void ctl_failover_lun(union ctl_io *io);
516 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
517 struct ctl_scsiio *ctsio);
518 static int ctl_scsiio(struct ctl_scsiio *ctsio);
519
520 static int ctl_target_reset(union ctl_io *io);
521 static void ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx,
522 ctl_ua_type ua_type);
523 static int ctl_lun_reset(union ctl_io *io);
524 static int ctl_abort_task(union ctl_io *io);
525 static int ctl_abort_task_set(union ctl_io *io);
526 static int ctl_query_task(union ctl_io *io, int task_set);
527 static void ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx,
528 ctl_ua_type ua_type);
529 static int ctl_i_t_nexus_reset(union ctl_io *io);
530 static int ctl_query_async_event(union ctl_io *io);
531 static void ctl_run_task(union ctl_io *io);
532 #ifdef CTL_IO_DELAY
533 static void ctl_datamove_timer_wakeup(void *arg);
534 static void ctl_done_timer_wakeup(void *arg);
535 #endif /* CTL_IO_DELAY */
536
537 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
538 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
539 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
540 static void ctl_datamove_remote_write(union ctl_io *io);
541 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
542 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
543 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
544 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
545 ctl_ha_dt_cb callback);
546 static void ctl_datamove_remote_read(union ctl_io *io);
547 static void ctl_datamove_remote(union ctl_io *io);
548 static void ctl_process_done(union ctl_io *io);
549 static void ctl_lun_thread(void *arg);
550 static void ctl_thresh_thread(void *arg);
551 static void ctl_work_thread(void *arg);
552 static void ctl_enqueue_incoming(union ctl_io *io);
553 static void ctl_enqueue_rtr(union ctl_io *io);
554 static void ctl_enqueue_done(union ctl_io *io);
555 static void ctl_enqueue_isc(union ctl_io *io);
556 static const struct ctl_cmd_entry *
557 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
558 static const struct ctl_cmd_entry *
559 ctl_validate_command(struct ctl_scsiio *ctsio);
560 static int ctl_cmd_applicable(uint8_t lun_type,
561 const struct ctl_cmd_entry *entry);
562 static int ctl_ha_init(void);
563 static int ctl_ha_shutdown(void);
564
565 static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx);
566 static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx);
567 static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx);
568 static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key);
569
570 /*
571 * Load the serialization table. This isn't very pretty, but is probably
572 * the easiest way to do it.
573 */
574 #include "ctl_ser_table.c"
575
576 /*
577 * We only need to define open, close and ioctl routines for this driver.
578 */
579 static struct cdevsw ctl_cdevsw = {
580 .d_version = D_VERSION,
581 .d_flags = 0,
582 .d_open = ctl_open,
583 .d_close = ctl_close,
584 .d_ioctl = ctl_ioctl,
585 .d_name = "ctl",
586 };
587
588
589 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
590
591 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
592
593 static moduledata_t ctl_moduledata = {
594 "ctl",
595 ctl_module_event_handler,
596 NULL
597 };
598
599 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
600 MODULE_VERSION(ctl, 1);
601
602 static struct ctl_frontend ha_frontend =
603 {
604 .name = "ha",
605 .init = ctl_ha_init,
606 .shutdown = ctl_ha_shutdown,
607 };
608
609 static int
ctl_ha_init(void)610 ctl_ha_init(void)
611 {
612 struct ctl_softc *softc = control_softc;
613
614 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
615 &softc->othersc_pool) != 0)
616 return (ENOMEM);
617 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
618 ctl_pool_free(softc->othersc_pool);
619 return (EIO);
620 }
621 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
622 != CTL_HA_STATUS_SUCCESS) {
623 ctl_ha_msg_destroy(softc);
624 ctl_pool_free(softc->othersc_pool);
625 return (EIO);
626 }
627 return (0);
628 };
629
630 static int
ctl_ha_shutdown(void)631 ctl_ha_shutdown(void)
632 {
633 struct ctl_softc *softc = control_softc;
634 struct ctl_port *port;
635
636 ctl_ha_msg_shutdown(softc);
637 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) != CTL_HA_STATUS_SUCCESS)
638 return (EIO);
639 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS)
640 return (EIO);
641 ctl_pool_free(softc->othersc_pool);
642 while ((port = STAILQ_FIRST(&ha_frontend.port_list)) != NULL) {
643 ctl_port_deregister(port);
644 free(port->port_name, M_CTL);
645 free(port, M_CTL);
646 }
647 return (0);
648 };
649
650 static void
ctl_ha_datamove(union ctl_io * io)651 ctl_ha_datamove(union ctl_io *io)
652 {
653 struct ctl_lun *lun = CTL_LUN(io);
654 struct ctl_sg_entry *sgl;
655 union ctl_ha_msg msg;
656 uint32_t sg_entries_sent;
657 int do_sg_copy, i, j;
658
659 memset(&msg.dt, 0, sizeof(msg.dt));
660 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
661 msg.hdr.original_sc = io->io_hdr.remote_io;
662 msg.hdr.serializing_sc = io;
663 msg.hdr.nexus = io->io_hdr.nexus;
664 msg.hdr.status = io->io_hdr.status;
665 msg.dt.flags = io->io_hdr.flags;
666
667 /*
668 * We convert everything into a S/G list here. We can't
669 * pass by reference, only by value between controllers.
670 * So we can't pass a pointer to the S/G list, only as many
671 * S/G entries as we can fit in here. If it's possible for
672 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
673 * then we need to break this up into multiple transfers.
674 */
675 if (io->scsiio.kern_sg_entries == 0) {
676 msg.dt.kern_sg_entries = 1;
677 #if 0
678 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
679 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
680 } else {
681 /* XXX KDM use busdma here! */
682 msg.dt.sg_list[0].addr =
683 (void *)vtophys(io->scsiio.kern_data_ptr);
684 }
685 #else
686 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
687 ("HA does not support BUS_ADDR"));
688 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
689 #endif
690 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
691 do_sg_copy = 0;
692 } else {
693 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
694 do_sg_copy = 1;
695 }
696
697 msg.dt.kern_data_len = io->scsiio.kern_data_len;
698 msg.dt.kern_total_len = io->scsiio.kern_total_len;
699 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
700 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
701 msg.dt.sg_sequence = 0;
702
703 /*
704 * Loop until we've sent all of the S/G entries. On the
705 * other end, we'll recompose these S/G entries into one
706 * contiguous list before processing.
707 */
708 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries;
709 msg.dt.sg_sequence++) {
710 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) /
711 sizeof(msg.dt.sg_list[0])),
712 msg.dt.kern_sg_entries - sg_entries_sent);
713 if (do_sg_copy != 0) {
714 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
715 for (i = sg_entries_sent, j = 0;
716 i < msg.dt.cur_sg_entries; i++, j++) {
717 #if 0
718 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
719 msg.dt.sg_list[j].addr = sgl[i].addr;
720 } else {
721 /* XXX KDM use busdma here! */
722 msg.dt.sg_list[j].addr =
723 (void *)vtophys(sgl[i].addr);
724 }
725 #else
726 KASSERT((io->io_hdr.flags &
727 CTL_FLAG_BUS_ADDR) == 0,
728 ("HA does not support BUS_ADDR"));
729 msg.dt.sg_list[j].addr = sgl[i].addr;
730 #endif
731 msg.dt.sg_list[j].len = sgl[i].len;
732 }
733 }
734
735 sg_entries_sent += msg.dt.cur_sg_entries;
736 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries);
737 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
738 sizeof(msg.dt) - sizeof(msg.dt.sg_list) +
739 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries,
740 M_WAITOK) > CTL_HA_STATUS_SUCCESS) {
741 io->io_hdr.port_status = 31341;
742 io->scsiio.be_move_done(io);
743 return;
744 }
745 msg.dt.sent_sg_entries = sg_entries_sent;
746 }
747
748 /*
749 * Officially handover the request from us to peer.
750 * If failover has just happened, then we must return error.
751 * If failover happen just after, then it is not our problem.
752 */
753 if (lun)
754 mtx_lock(&lun->lun_lock);
755 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
756 if (lun)
757 mtx_unlock(&lun->lun_lock);
758 io->io_hdr.port_status = 31342;
759 io->scsiio.be_move_done(io);
760 return;
761 }
762 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
763 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
764 if (lun)
765 mtx_unlock(&lun->lun_lock);
766 }
767
768 static void
ctl_ha_done(union ctl_io * io)769 ctl_ha_done(union ctl_io *io)
770 {
771 union ctl_ha_msg msg;
772
773 if (io->io_hdr.io_type == CTL_IO_SCSI) {
774 memset(&msg, 0, sizeof(msg));
775 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
776 msg.hdr.original_sc = io->io_hdr.remote_io;
777 msg.hdr.nexus = io->io_hdr.nexus;
778 msg.hdr.status = io->io_hdr.status;
779 msg.scsi.scsi_status = io->scsiio.scsi_status;
780 msg.scsi.tag_num = io->scsiio.tag_num;
781 msg.scsi.tag_type = io->scsiio.tag_type;
782 msg.scsi.sense_len = io->scsiio.sense_len;
783 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
784 io->scsiio.sense_len);
785 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
786 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
787 msg.scsi.sense_len, M_WAITOK);
788 }
789 ctl_free_io(io);
790 }
791
792 static void
ctl_isc_handler_finish_xfer(struct ctl_softc * ctl_softc,union ctl_ha_msg * msg_info)793 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
794 union ctl_ha_msg *msg_info)
795 {
796 struct ctl_scsiio *ctsio;
797
798 if (msg_info->hdr.original_sc == NULL) {
799 printf("%s: original_sc == NULL!\n", __func__);
800 /* XXX KDM now what? */
801 return;
802 }
803
804 ctsio = &msg_info->hdr.original_sc->scsiio;
805 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
806 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
807 ctsio->io_hdr.status = msg_info->hdr.status;
808 ctsio->scsi_status = msg_info->scsi.scsi_status;
809 ctsio->sense_len = msg_info->scsi.sense_len;
810 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
811 msg_info->scsi.sense_len);
812 ctl_enqueue_isc((union ctl_io *)ctsio);
813 }
814
815 static void
ctl_isc_handler_finish_ser_only(struct ctl_softc * ctl_softc,union ctl_ha_msg * msg_info)816 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
817 union ctl_ha_msg *msg_info)
818 {
819 struct ctl_scsiio *ctsio;
820
821 if (msg_info->hdr.serializing_sc == NULL) {
822 printf("%s: serializing_sc == NULL!\n", __func__);
823 /* XXX KDM now what? */
824 return;
825 }
826
827 ctsio = &msg_info->hdr.serializing_sc->scsiio;
828 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
829 ctl_enqueue_isc((union ctl_io *)ctsio);
830 }
831
832 void
ctl_isc_announce_lun(struct ctl_lun * lun)833 ctl_isc_announce_lun(struct ctl_lun *lun)
834 {
835 struct ctl_softc *softc = lun->ctl_softc;
836 union ctl_ha_msg *msg;
837 struct ctl_ha_msg_lun_pr_key pr_key;
838 int i, k;
839
840 if (softc->ha_link != CTL_HA_LINK_ONLINE)
841 return;
842 mtx_lock(&lun->lun_lock);
843 i = sizeof(msg->lun);
844 if (lun->lun_devid)
845 i += lun->lun_devid->len;
846 i += sizeof(pr_key) * lun->pr_key_count;
847 alloc:
848 mtx_unlock(&lun->lun_lock);
849 msg = malloc(i, M_CTL, M_WAITOK);
850 mtx_lock(&lun->lun_lock);
851 k = sizeof(msg->lun);
852 if (lun->lun_devid)
853 k += lun->lun_devid->len;
854 k += sizeof(pr_key) * lun->pr_key_count;
855 if (i < k) {
856 free(msg, M_CTL);
857 i = k;
858 goto alloc;
859 }
860 bzero(&msg->lun, sizeof(msg->lun));
861 msg->hdr.msg_type = CTL_MSG_LUN_SYNC;
862 msg->hdr.nexus.targ_lun = lun->lun;
863 msg->hdr.nexus.targ_mapped_lun = lun->lun;
864 msg->lun.flags = lun->flags;
865 msg->lun.pr_generation = lun->pr_generation;
866 msg->lun.pr_res_idx = lun->pr_res_idx;
867 msg->lun.pr_res_type = lun->pr_res_type;
868 msg->lun.pr_key_count = lun->pr_key_count;
869 i = 0;
870 if (lun->lun_devid) {
871 msg->lun.lun_devid_len = lun->lun_devid->len;
872 memcpy(&msg->lun.data[i], lun->lun_devid->data,
873 msg->lun.lun_devid_len);
874 i += msg->lun.lun_devid_len;
875 }
876 for (k = 0; k < CTL_MAX_INITIATORS; k++) {
877 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0)
878 continue;
879 pr_key.pr_iid = k;
880 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key));
881 i += sizeof(pr_key);
882 }
883 mtx_unlock(&lun->lun_lock);
884 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
885 M_WAITOK);
886 free(msg, M_CTL);
887
888 if (lun->flags & CTL_LUN_PRIMARY_SC) {
889 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
890 ctl_isc_announce_mode(lun, -1,
891 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
892 lun->mode_pages.index[i].subpage);
893 }
894 }
895 }
896
897 void
ctl_isc_announce_port(struct ctl_port * port)898 ctl_isc_announce_port(struct ctl_port *port)
899 {
900 struct ctl_softc *softc = port->ctl_softc;
901 union ctl_ha_msg *msg;
902 int i;
903
904 if (port->targ_port < softc->port_min ||
905 port->targ_port >= softc->port_max ||
906 softc->ha_link != CTL_HA_LINK_ONLINE)
907 return;
908 i = sizeof(msg->port) + strlen(port->port_name) + 1;
909 if (port->lun_map)
910 i += port->lun_map_size * sizeof(uint32_t);
911 if (port->port_devid)
912 i += port->port_devid->len;
913 if (port->target_devid)
914 i += port->target_devid->len;
915 if (port->init_devid)
916 i += port->init_devid->len;
917 msg = malloc(i, M_CTL, M_WAITOK);
918 bzero(&msg->port, sizeof(msg->port));
919 msg->hdr.msg_type = CTL_MSG_PORT_SYNC;
920 msg->hdr.nexus.targ_port = port->targ_port;
921 msg->port.port_type = port->port_type;
922 msg->port.physical_port = port->physical_port;
923 msg->port.virtual_port = port->virtual_port;
924 msg->port.status = port->status;
925 i = 0;
926 msg->port.name_len = sprintf(&msg->port.data[i],
927 "%d:%s", softc->ha_id, port->port_name) + 1;
928 i += msg->port.name_len;
929 if (port->lun_map) {
930 msg->port.lun_map_len = port->lun_map_size * sizeof(uint32_t);
931 memcpy(&msg->port.data[i], port->lun_map,
932 msg->port.lun_map_len);
933 i += msg->port.lun_map_len;
934 }
935 if (port->port_devid) {
936 msg->port.port_devid_len = port->port_devid->len;
937 memcpy(&msg->port.data[i], port->port_devid->data,
938 msg->port.port_devid_len);
939 i += msg->port.port_devid_len;
940 }
941 if (port->target_devid) {
942 msg->port.target_devid_len = port->target_devid->len;
943 memcpy(&msg->port.data[i], port->target_devid->data,
944 msg->port.target_devid_len);
945 i += msg->port.target_devid_len;
946 }
947 if (port->init_devid) {
948 msg->port.init_devid_len = port->init_devid->len;
949 memcpy(&msg->port.data[i], port->init_devid->data,
950 msg->port.init_devid_len);
951 i += msg->port.init_devid_len;
952 }
953 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
954 M_WAITOK);
955 free(msg, M_CTL);
956 }
957
958 void
ctl_isc_announce_iid(struct ctl_port * port,int iid)959 ctl_isc_announce_iid(struct ctl_port *port, int iid)
960 {
961 struct ctl_softc *softc = port->ctl_softc;
962 union ctl_ha_msg *msg;
963 int i, l;
964
965 if (port->targ_port < softc->port_min ||
966 port->targ_port >= softc->port_max ||
967 softc->ha_link != CTL_HA_LINK_ONLINE)
968 return;
969 mtx_lock(&softc->ctl_lock);
970 i = sizeof(msg->iid);
971 l = 0;
972 if (port->wwpn_iid[iid].name)
973 l = strlen(port->wwpn_iid[iid].name) + 1;
974 i += l;
975 msg = malloc(i, M_CTL, M_NOWAIT);
976 if (msg == NULL) {
977 mtx_unlock(&softc->ctl_lock);
978 return;
979 }
980 bzero(&msg->iid, sizeof(msg->iid));
981 msg->hdr.msg_type = CTL_MSG_IID_SYNC;
982 msg->hdr.nexus.targ_port = port->targ_port;
983 msg->hdr.nexus.initid = iid;
984 msg->iid.in_use = port->wwpn_iid[iid].in_use;
985 msg->iid.name_len = l;
986 msg->iid.wwpn = port->wwpn_iid[iid].wwpn;
987 if (port->wwpn_iid[iid].name)
988 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l);
989 mtx_unlock(&softc->ctl_lock);
990 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT);
991 free(msg, M_CTL);
992 }
993
994 void
ctl_isc_announce_mode(struct ctl_lun * lun,uint32_t initidx,uint8_t page,uint8_t subpage)995 ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx,
996 uint8_t page, uint8_t subpage)
997 {
998 struct ctl_softc *softc = lun->ctl_softc;
999 union ctl_ha_msg msg;
1000 u_int i;
1001
1002 if (softc->ha_link != CTL_HA_LINK_ONLINE)
1003 return;
1004 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
1005 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
1006 page && lun->mode_pages.index[i].subpage == subpage)
1007 break;
1008 }
1009 if (i == CTL_NUM_MODE_PAGES)
1010 return;
1011
1012 /* Don't try to replicate pages not present on this device. */
1013 if (lun->mode_pages.index[i].page_data == NULL)
1014 return;
1015
1016 bzero(&msg.mode, sizeof(msg.mode));
1017 msg.hdr.msg_type = CTL_MSG_MODE_SYNC;
1018 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT;
1019 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT;
1020 msg.hdr.nexus.targ_lun = lun->lun;
1021 msg.hdr.nexus.targ_mapped_lun = lun->lun;
1022 msg.mode.page_code = page;
1023 msg.mode.subpage = subpage;
1024 msg.mode.page_len = lun->mode_pages.index[i].page_len;
1025 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data,
1026 msg.mode.page_len);
1027 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode),
1028 M_WAITOK);
1029 }
1030
1031 static void
ctl_isc_ha_link_up(struct ctl_softc * softc)1032 ctl_isc_ha_link_up(struct ctl_softc *softc)
1033 {
1034 struct ctl_port *port;
1035 struct ctl_lun *lun;
1036 union ctl_ha_msg msg;
1037 int i;
1038
1039 /* Announce this node parameters to peer for validation. */
1040 msg.login.msg_type = CTL_MSG_LOGIN;
1041 msg.login.version = CTL_HA_VERSION;
1042 msg.login.ha_mode = softc->ha_mode;
1043 msg.login.ha_id = softc->ha_id;
1044 msg.login.max_luns = ctl_max_luns;
1045 msg.login.max_ports = ctl_max_ports;
1046 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT;
1047 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login),
1048 M_WAITOK);
1049
1050 STAILQ_FOREACH(port, &softc->port_list, links) {
1051 ctl_isc_announce_port(port);
1052 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1053 if (port->wwpn_iid[i].in_use)
1054 ctl_isc_announce_iid(port, i);
1055 }
1056 }
1057 STAILQ_FOREACH(lun, &softc->lun_list, links)
1058 ctl_isc_announce_lun(lun);
1059 }
1060
1061 static void
ctl_isc_ha_link_down(struct ctl_softc * softc)1062 ctl_isc_ha_link_down(struct ctl_softc *softc)
1063 {
1064 struct ctl_port *port;
1065 struct ctl_lun *lun;
1066 union ctl_io *io;
1067 int i;
1068
1069 mtx_lock(&softc->ctl_lock);
1070 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1071 mtx_lock(&lun->lun_lock);
1072 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) {
1073 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1074 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1075 }
1076 mtx_unlock(&lun->lun_lock);
1077
1078 mtx_unlock(&softc->ctl_lock);
1079 io = ctl_alloc_io(softc->othersc_pool);
1080 mtx_lock(&softc->ctl_lock);
1081 ctl_zero_io(io);
1082 io->io_hdr.msg_type = CTL_MSG_FAILOVER;
1083 io->io_hdr.nexus.targ_mapped_lun = lun->lun;
1084 ctl_enqueue_isc(io);
1085 }
1086
1087 STAILQ_FOREACH(port, &softc->port_list, links) {
1088 if (port->targ_port >= softc->port_min &&
1089 port->targ_port < softc->port_max)
1090 continue;
1091 port->status &= ~CTL_PORT_STATUS_ONLINE;
1092 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1093 port->wwpn_iid[i].in_use = 0;
1094 free(port->wwpn_iid[i].name, M_CTL);
1095 port->wwpn_iid[i].name = NULL;
1096 }
1097 }
1098 mtx_unlock(&softc->ctl_lock);
1099 }
1100
1101 static void
ctl_isc_ua(struct ctl_softc * softc,union ctl_ha_msg * msg,int len)1102 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1103 {
1104 struct ctl_lun *lun;
1105 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus);
1106
1107 mtx_lock(&softc->ctl_lock);
1108 if (msg->hdr.nexus.targ_mapped_lun >= ctl_max_luns ||
1109 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) == NULL) {
1110 mtx_unlock(&softc->ctl_lock);
1111 return;
1112 }
1113 mtx_lock(&lun->lun_lock);
1114 mtx_unlock(&softc->ctl_lock);
1115 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && msg->ua.ua_set)
1116 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8);
1117 if (msg->ua.ua_all) {
1118 if (msg->ua.ua_set)
1119 ctl_est_ua_all(lun, iid, msg->ua.ua_type);
1120 else
1121 ctl_clr_ua_all(lun, iid, msg->ua.ua_type);
1122 } else {
1123 if (msg->ua.ua_set)
1124 ctl_est_ua(lun, iid, msg->ua.ua_type);
1125 else
1126 ctl_clr_ua(lun, iid, msg->ua.ua_type);
1127 }
1128 mtx_unlock(&lun->lun_lock);
1129 }
1130
1131 static void
ctl_isc_lun_sync(struct ctl_softc * softc,union ctl_ha_msg * msg,int len)1132 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1133 {
1134 struct ctl_lun *lun;
1135 struct ctl_ha_msg_lun_pr_key pr_key;
1136 int i, k;
1137 ctl_lun_flags oflags;
1138 uint32_t targ_lun;
1139
1140 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1141 mtx_lock(&softc->ctl_lock);
1142 if (targ_lun >= ctl_max_luns ||
1143 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1144 mtx_unlock(&softc->ctl_lock);
1145 return;
1146 }
1147 mtx_lock(&lun->lun_lock);
1148 mtx_unlock(&softc->ctl_lock);
1149 if (lun->flags & CTL_LUN_DISABLED) {
1150 mtx_unlock(&lun->lun_lock);
1151 return;
1152 }
1153 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0;
1154 if (msg->lun.lun_devid_len != i || (i > 0 &&
1155 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) {
1156 mtx_unlock(&lun->lun_lock);
1157 printf("%s: Received conflicting HA LUN %d\n",
1158 __func__, targ_lun);
1159 return;
1160 } else {
1161 /* Record whether peer is primary. */
1162 oflags = lun->flags;
1163 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) &&
1164 (msg->lun.flags & CTL_LUN_DISABLED) == 0)
1165 lun->flags |= CTL_LUN_PEER_SC_PRIMARY;
1166 else
1167 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1168 if (oflags != lun->flags)
1169 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1170
1171 /* If peer is primary and we are not -- use data */
1172 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
1173 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) {
1174 lun->pr_generation = msg->lun.pr_generation;
1175 lun->pr_res_idx = msg->lun.pr_res_idx;
1176 lun->pr_res_type = msg->lun.pr_res_type;
1177 lun->pr_key_count = msg->lun.pr_key_count;
1178 for (k = 0; k < CTL_MAX_INITIATORS; k++)
1179 ctl_clr_prkey(lun, k);
1180 for (k = 0; k < msg->lun.pr_key_count; k++) {
1181 memcpy(&pr_key, &msg->lun.data[i],
1182 sizeof(pr_key));
1183 ctl_alloc_prkey(lun, pr_key.pr_iid);
1184 ctl_set_prkey(lun, pr_key.pr_iid,
1185 pr_key.pr_key);
1186 i += sizeof(pr_key);
1187 }
1188 }
1189
1190 mtx_unlock(&lun->lun_lock);
1191 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n",
1192 __func__, targ_lun,
1193 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ?
1194 "primary" : "secondary"));
1195
1196 /* If we are primary but peer doesn't know -- notify */
1197 if ((lun->flags & CTL_LUN_PRIMARY_SC) &&
1198 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0)
1199 ctl_isc_announce_lun(lun);
1200 }
1201 }
1202
1203 static void
ctl_isc_port_sync(struct ctl_softc * softc,union ctl_ha_msg * msg,int len)1204 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1205 {
1206 struct ctl_port *port;
1207 struct ctl_lun *lun;
1208 int i, new;
1209
1210 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1211 if (port == NULL) {
1212 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__,
1213 msg->hdr.nexus.targ_port));
1214 new = 1;
1215 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO);
1216 port->frontend = &ha_frontend;
1217 port->targ_port = msg->hdr.nexus.targ_port;
1218 port->fe_datamove = ctl_ha_datamove;
1219 port->fe_done = ctl_ha_done;
1220 } else if (port->frontend == &ha_frontend) {
1221 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__,
1222 msg->hdr.nexus.targ_port));
1223 new = 0;
1224 } else {
1225 printf("%s: Received conflicting HA port %d\n",
1226 __func__, msg->hdr.nexus.targ_port);
1227 return;
1228 }
1229 port->port_type = msg->port.port_type;
1230 port->physical_port = msg->port.physical_port;
1231 port->virtual_port = msg->port.virtual_port;
1232 port->status = msg->port.status;
1233 i = 0;
1234 free(port->port_name, M_CTL);
1235 port->port_name = strndup(&msg->port.data[i], msg->port.name_len,
1236 M_CTL);
1237 i += msg->port.name_len;
1238 if (msg->port.lun_map_len != 0) {
1239 if (port->lun_map == NULL ||
1240 port->lun_map_size * sizeof(uint32_t) <
1241 msg->port.lun_map_len) {
1242 port->lun_map_size = 0;
1243 free(port->lun_map, M_CTL);
1244 port->lun_map = malloc(msg->port.lun_map_len,
1245 M_CTL, M_WAITOK);
1246 }
1247 memcpy(port->lun_map, &msg->port.data[i], msg->port.lun_map_len);
1248 port->lun_map_size = msg->port.lun_map_len / sizeof(uint32_t);
1249 i += msg->port.lun_map_len;
1250 } else {
1251 port->lun_map_size = 0;
1252 free(port->lun_map, M_CTL);
1253 port->lun_map = NULL;
1254 }
1255 if (msg->port.port_devid_len != 0) {
1256 if (port->port_devid == NULL ||
1257 port->port_devid->len < msg->port.port_devid_len) {
1258 free(port->port_devid, M_CTL);
1259 port->port_devid = malloc(sizeof(struct ctl_devid) +
1260 msg->port.port_devid_len, M_CTL, M_WAITOK);
1261 }
1262 memcpy(port->port_devid->data, &msg->port.data[i],
1263 msg->port.port_devid_len);
1264 port->port_devid->len = msg->port.port_devid_len;
1265 i += msg->port.port_devid_len;
1266 } else {
1267 free(port->port_devid, M_CTL);
1268 port->port_devid = NULL;
1269 }
1270 if (msg->port.target_devid_len != 0) {
1271 if (port->target_devid == NULL ||
1272 port->target_devid->len < msg->port.target_devid_len) {
1273 free(port->target_devid, M_CTL);
1274 port->target_devid = malloc(sizeof(struct ctl_devid) +
1275 msg->port.target_devid_len, M_CTL, M_WAITOK);
1276 }
1277 memcpy(port->target_devid->data, &msg->port.data[i],
1278 msg->port.target_devid_len);
1279 port->target_devid->len = msg->port.target_devid_len;
1280 i += msg->port.target_devid_len;
1281 } else {
1282 free(port->target_devid, M_CTL);
1283 port->target_devid = NULL;
1284 }
1285 if (msg->port.init_devid_len != 0) {
1286 if (port->init_devid == NULL ||
1287 port->init_devid->len < msg->port.init_devid_len) {
1288 free(port->init_devid, M_CTL);
1289 port->init_devid = malloc(sizeof(struct ctl_devid) +
1290 msg->port.init_devid_len, M_CTL, M_WAITOK);
1291 }
1292 memcpy(port->init_devid->data, &msg->port.data[i],
1293 msg->port.init_devid_len);
1294 port->init_devid->len = msg->port.init_devid_len;
1295 i += msg->port.init_devid_len;
1296 } else {
1297 free(port->init_devid, M_CTL);
1298 port->init_devid = NULL;
1299 }
1300 if (new) {
1301 if (ctl_port_register(port) != 0) {
1302 printf("%s: ctl_port_register() failed with error\n",
1303 __func__);
1304 }
1305 }
1306 mtx_lock(&softc->ctl_lock);
1307 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1308 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
1309 continue;
1310 mtx_lock(&lun->lun_lock);
1311 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
1312 mtx_unlock(&lun->lun_lock);
1313 }
1314 mtx_unlock(&softc->ctl_lock);
1315 }
1316
1317 static void
ctl_isc_iid_sync(struct ctl_softc * softc,union ctl_ha_msg * msg,int len)1318 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1319 {
1320 struct ctl_port *port;
1321 int iid;
1322
1323 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1324 if (port == NULL) {
1325 printf("%s: Received IID for unknown port %d\n",
1326 __func__, msg->hdr.nexus.targ_port);
1327 return;
1328 }
1329 iid = msg->hdr.nexus.initid;
1330 if (port->wwpn_iid[iid].in_use != 0 &&
1331 msg->iid.in_use == 0)
1332 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON);
1333 port->wwpn_iid[iid].in_use = msg->iid.in_use;
1334 port->wwpn_iid[iid].wwpn = msg->iid.wwpn;
1335 free(port->wwpn_iid[iid].name, M_CTL);
1336 if (msg->iid.name_len) {
1337 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0],
1338 msg->iid.name_len, M_CTL);
1339 } else
1340 port->wwpn_iid[iid].name = NULL;
1341 }
1342
1343 static void
ctl_isc_login(struct ctl_softc * softc,union ctl_ha_msg * msg,int len)1344 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1345 {
1346
1347 if (msg->login.version != CTL_HA_VERSION) {
1348 printf("CTL HA peers have different versions %d != %d\n",
1349 msg->login.version, CTL_HA_VERSION);
1350 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1351 return;
1352 }
1353 if (msg->login.ha_mode != softc->ha_mode) {
1354 printf("CTL HA peers have different ha_mode %d != %d\n",
1355 msg->login.ha_mode, softc->ha_mode);
1356 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1357 return;
1358 }
1359 if (msg->login.ha_id == softc->ha_id) {
1360 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id);
1361 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1362 return;
1363 }
1364 if (msg->login.max_luns != ctl_max_luns ||
1365 msg->login.max_ports != ctl_max_ports ||
1366 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) {
1367 printf("CTL HA peers have different limits\n");
1368 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1369 return;
1370 }
1371 }
1372
1373 static void
ctl_isc_mode_sync(struct ctl_softc * softc,union ctl_ha_msg * msg,int len)1374 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1375 {
1376 struct ctl_lun *lun;
1377 u_int i;
1378 uint32_t initidx, targ_lun;
1379
1380 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1381 mtx_lock(&softc->ctl_lock);
1382 if (targ_lun >= ctl_max_luns ||
1383 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1384 mtx_unlock(&softc->ctl_lock);
1385 return;
1386 }
1387 mtx_lock(&lun->lun_lock);
1388 mtx_unlock(&softc->ctl_lock);
1389 if (lun->flags & CTL_LUN_DISABLED) {
1390 mtx_unlock(&lun->lun_lock);
1391 return;
1392 }
1393 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
1394 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
1395 msg->mode.page_code &&
1396 lun->mode_pages.index[i].subpage == msg->mode.subpage)
1397 break;
1398 }
1399 if (i == CTL_NUM_MODE_PAGES) {
1400 mtx_unlock(&lun->lun_lock);
1401 return;
1402 }
1403 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data,
1404 lun->mode_pages.index[i].page_len);
1405 initidx = ctl_get_initindex(&msg->hdr.nexus);
1406 if (initidx != -1)
1407 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
1408 mtx_unlock(&lun->lun_lock);
1409 }
1410
1411 /*
1412 * ISC (Inter Shelf Communication) event handler. Events from the HA
1413 * subsystem come in here.
1414 */
1415 static void
ctl_isc_event_handler(ctl_ha_channel channel,ctl_ha_event event,int param)1416 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
1417 {
1418 struct ctl_softc *softc = control_softc;
1419 union ctl_io *io;
1420 struct ctl_prio *presio;
1421 ctl_ha_status isc_status;
1422
1423 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event));
1424 if (event == CTL_HA_EVT_MSG_RECV) {
1425 union ctl_ha_msg *msg, msgbuf;
1426
1427 if (param > sizeof(msgbuf))
1428 msg = malloc(param, M_CTL, M_WAITOK);
1429 else
1430 msg = &msgbuf;
1431 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param,
1432 M_WAITOK);
1433 if (isc_status != CTL_HA_STATUS_SUCCESS) {
1434 printf("%s: Error receiving message: %d\n",
1435 __func__, isc_status);
1436 if (msg != &msgbuf)
1437 free(msg, M_CTL);
1438 return;
1439 }
1440
1441 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->hdr.msg_type));
1442 switch (msg->hdr.msg_type) {
1443 case CTL_MSG_SERIALIZE:
1444 io = ctl_alloc_io(softc->othersc_pool);
1445 ctl_zero_io(io);
1446 // populate ctsio from msg
1447 io->io_hdr.io_type = CTL_IO_SCSI;
1448 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
1449 io->io_hdr.remote_io = msg->hdr.original_sc;
1450 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
1451 CTL_FLAG_IO_ACTIVE;
1452 /*
1453 * If we're in serialization-only mode, we don't
1454 * want to go through full done processing. Thus
1455 * the COPY flag.
1456 *
1457 * XXX KDM add another flag that is more specific.
1458 */
1459 if (softc->ha_mode != CTL_HA_MODE_XFER)
1460 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
1461 io->io_hdr.nexus = msg->hdr.nexus;
1462 io->scsiio.tag_num = msg->scsi.tag_num;
1463 io->scsiio.tag_type = msg->scsi.tag_type;
1464 #ifdef CTL_TIME_IO
1465 io->io_hdr.start_time = time_uptime;
1466 getbinuptime(&io->io_hdr.start_bt);
1467 #endif /* CTL_TIME_IO */
1468 io->scsiio.cdb_len = msg->scsi.cdb_len;
1469 memcpy(io->scsiio.cdb, msg->scsi.cdb,
1470 CTL_MAX_CDBLEN);
1471 if (softc->ha_mode == CTL_HA_MODE_XFER) {
1472 const struct ctl_cmd_entry *entry;
1473
1474 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
1475 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
1476 io->io_hdr.flags |=
1477 entry->flags & CTL_FLAG_DATA_MASK;
1478 }
1479 ctl_enqueue_isc(io);
1480 break;
1481
1482 /* Performed on the Originating SC, XFER mode only */
1483 case CTL_MSG_DATAMOVE: {
1484 struct ctl_sg_entry *sgl;
1485 int i, j;
1486
1487 io = msg->hdr.original_sc;
1488 if (io == NULL) {
1489 printf("%s: original_sc == NULL!\n", __func__);
1490 /* XXX KDM do something here */
1491 break;
1492 }
1493 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
1494 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1495 /*
1496 * Keep track of this, we need to send it back over
1497 * when the datamove is complete.
1498 */
1499 io->io_hdr.remote_io = msg->hdr.serializing_sc;
1500 if (msg->hdr.status == CTL_SUCCESS)
1501 io->io_hdr.status = msg->hdr.status;
1502
1503 if (msg->dt.sg_sequence == 0) {
1504 #ifdef CTL_TIME_IO
1505 getbinuptime(&io->io_hdr.dma_start_bt);
1506 #endif
1507 i = msg->dt.kern_sg_entries +
1508 msg->dt.kern_data_len /
1509 CTL_HA_DATAMOVE_SEGMENT + 1;
1510 sgl = malloc(sizeof(*sgl) * i, M_CTL,
1511 M_WAITOK | M_ZERO);
1512 CTL_RSGL(io) = sgl;
1513 CTL_LSGL(io) = &sgl[msg->dt.kern_sg_entries];
1514
1515 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
1516
1517 io->scsiio.kern_sg_entries =
1518 msg->dt.kern_sg_entries;
1519 io->scsiio.rem_sg_entries =
1520 msg->dt.kern_sg_entries;
1521 io->scsiio.kern_data_len =
1522 msg->dt.kern_data_len;
1523 io->scsiio.kern_total_len =
1524 msg->dt.kern_total_len;
1525 io->scsiio.kern_data_resid =
1526 msg->dt.kern_data_resid;
1527 io->scsiio.kern_rel_offset =
1528 msg->dt.kern_rel_offset;
1529 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR;
1530 io->io_hdr.flags |= msg->dt.flags &
1531 CTL_FLAG_BUS_ADDR;
1532 } else
1533 sgl = (struct ctl_sg_entry *)
1534 io->scsiio.kern_data_ptr;
1535
1536 for (i = msg->dt.sent_sg_entries, j = 0;
1537 i < (msg->dt.sent_sg_entries +
1538 msg->dt.cur_sg_entries); i++, j++) {
1539 sgl[i].addr = msg->dt.sg_list[j].addr;
1540 sgl[i].len = msg->dt.sg_list[j].len;
1541 }
1542
1543 /*
1544 * If this is the last piece of the I/O, we've got
1545 * the full S/G list. Queue processing in the thread.
1546 * Otherwise wait for the next piece.
1547 */
1548 if (msg->dt.sg_last != 0)
1549 ctl_enqueue_isc(io);
1550 break;
1551 }
1552 /* Performed on the Serializing (primary) SC, XFER mode only */
1553 case CTL_MSG_DATAMOVE_DONE: {
1554 if (msg->hdr.serializing_sc == NULL) {
1555 printf("%s: serializing_sc == NULL!\n",
1556 __func__);
1557 /* XXX KDM now what? */
1558 break;
1559 }
1560 /*
1561 * We grab the sense information here in case
1562 * there was a failure, so we can return status
1563 * back to the initiator.
1564 */
1565 io = msg->hdr.serializing_sc;
1566 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
1567 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1568 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1569 io->io_hdr.port_status = msg->scsi.port_status;
1570 io->scsiio.kern_data_resid = msg->scsi.kern_data_resid;
1571 if (msg->hdr.status != CTL_STATUS_NONE) {
1572 io->io_hdr.status = msg->hdr.status;
1573 io->scsiio.scsi_status = msg->scsi.scsi_status;
1574 io->scsiio.sense_len = msg->scsi.sense_len;
1575 memcpy(&io->scsiio.sense_data,
1576 &msg->scsi.sense_data,
1577 msg->scsi.sense_len);
1578 if (msg->hdr.status == CTL_SUCCESS)
1579 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1580 }
1581 ctl_enqueue_isc(io);
1582 break;
1583 }
1584
1585 /* Preformed on Originating SC, SER_ONLY mode */
1586 case CTL_MSG_R2R:
1587 io = msg->hdr.original_sc;
1588 if (io == NULL) {
1589 printf("%s: original_sc == NULL!\n",
1590 __func__);
1591 break;
1592 }
1593 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1594 io->io_hdr.msg_type = CTL_MSG_R2R;
1595 io->io_hdr.remote_io = msg->hdr.serializing_sc;
1596 ctl_enqueue_isc(io);
1597 break;
1598
1599 /*
1600 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
1601 * mode.
1602 * Performed on the Originating (i.e. secondary) SC in XFER
1603 * mode
1604 */
1605 case CTL_MSG_FINISH_IO:
1606 if (softc->ha_mode == CTL_HA_MODE_XFER)
1607 ctl_isc_handler_finish_xfer(softc, msg);
1608 else
1609 ctl_isc_handler_finish_ser_only(softc, msg);
1610 break;
1611
1612 /* Preformed on Originating SC */
1613 case CTL_MSG_BAD_JUJU:
1614 io = msg->hdr.original_sc;
1615 if (io == NULL) {
1616 printf("%s: Bad JUJU!, original_sc is NULL!\n",
1617 __func__);
1618 break;
1619 }
1620 ctl_copy_sense_data(msg, io);
1621 /*
1622 * IO should have already been cleaned up on other
1623 * SC so clear this flag so we won't send a message
1624 * back to finish the IO there.
1625 */
1626 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1627 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1628
1629 /* io = msg->hdr.serializing_sc; */
1630 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
1631 ctl_enqueue_isc(io);
1632 break;
1633
1634 /* Handle resets sent from the other side */
1635 case CTL_MSG_MANAGE_TASKS: {
1636 struct ctl_taskio *taskio;
1637 taskio = (struct ctl_taskio *)ctl_alloc_io(
1638 softc->othersc_pool);
1639 ctl_zero_io((union ctl_io *)taskio);
1640 taskio->io_hdr.io_type = CTL_IO_TASK;
1641 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1642 taskio->io_hdr.nexus = msg->hdr.nexus;
1643 taskio->task_action = msg->task.task_action;
1644 taskio->tag_num = msg->task.tag_num;
1645 taskio->tag_type = msg->task.tag_type;
1646 #ifdef CTL_TIME_IO
1647 taskio->io_hdr.start_time = time_uptime;
1648 getbinuptime(&taskio->io_hdr.start_bt);
1649 #endif /* CTL_TIME_IO */
1650 ctl_run_task((union ctl_io *)taskio);
1651 break;
1652 }
1653 /* Persistent Reserve action which needs attention */
1654 case CTL_MSG_PERS_ACTION:
1655 presio = (struct ctl_prio *)ctl_alloc_io(
1656 softc->othersc_pool);
1657 ctl_zero_io((union ctl_io *)presio);
1658 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
1659 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1660 presio->io_hdr.nexus = msg->hdr.nexus;
1661 presio->pr_msg = msg->pr;
1662 ctl_enqueue_isc((union ctl_io *)presio);
1663 break;
1664 case CTL_MSG_UA:
1665 ctl_isc_ua(softc, msg, param);
1666 break;
1667 case CTL_MSG_PORT_SYNC:
1668 ctl_isc_port_sync(softc, msg, param);
1669 break;
1670 case CTL_MSG_LUN_SYNC:
1671 ctl_isc_lun_sync(softc, msg, param);
1672 break;
1673 case CTL_MSG_IID_SYNC:
1674 ctl_isc_iid_sync(softc, msg, param);
1675 break;
1676 case CTL_MSG_LOGIN:
1677 ctl_isc_login(softc, msg, param);
1678 break;
1679 case CTL_MSG_MODE_SYNC:
1680 ctl_isc_mode_sync(softc, msg, param);
1681 break;
1682 default:
1683 printf("Received HA message of unknown type %d\n",
1684 msg->hdr.msg_type);
1685 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1686 break;
1687 }
1688 if (msg != &msgbuf)
1689 free(msg, M_CTL);
1690 } else if (event == CTL_HA_EVT_LINK_CHANGE) {
1691 printf("CTL: HA link status changed from %d to %d\n",
1692 softc->ha_link, param);
1693 if (param == softc->ha_link)
1694 return;
1695 if (softc->ha_link == CTL_HA_LINK_ONLINE) {
1696 softc->ha_link = param;
1697 ctl_isc_ha_link_down(softc);
1698 } else {
1699 softc->ha_link = param;
1700 if (softc->ha_link == CTL_HA_LINK_ONLINE)
1701 ctl_isc_ha_link_up(softc);
1702 }
1703 return;
1704 } else {
1705 printf("ctl_isc_event_handler: Unknown event %d\n", event);
1706 return;
1707 }
1708 }
1709
1710 static void
ctl_copy_sense_data(union ctl_ha_msg * src,union ctl_io * dest)1711 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
1712 {
1713
1714 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data,
1715 src->scsi.sense_len);
1716 dest->scsiio.scsi_status = src->scsi.scsi_status;
1717 dest->scsiio.sense_len = src->scsi.sense_len;
1718 dest->io_hdr.status = src->hdr.status;
1719 }
1720
1721 static void
ctl_copy_sense_data_back(union ctl_io * src,union ctl_ha_msg * dest)1722 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest)
1723 {
1724
1725 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data,
1726 src->scsiio.sense_len);
1727 dest->scsi.scsi_status = src->scsiio.scsi_status;
1728 dest->scsi.sense_len = src->scsiio.sense_len;
1729 dest->hdr.status = src->io_hdr.status;
1730 }
1731
1732 void
ctl_est_ua(struct ctl_lun * lun,uint32_t initidx,ctl_ua_type ua)1733 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1734 {
1735 struct ctl_softc *softc = lun->ctl_softc;
1736 ctl_ua_type *pu;
1737
1738 if (initidx < softc->init_min || initidx >= softc->init_max)
1739 return;
1740 mtx_assert(&lun->lun_lock, MA_OWNED);
1741 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1742 if (pu == NULL)
1743 return;
1744 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
1745 }
1746
1747 void
ctl_est_ua_port(struct ctl_lun * lun,int port,uint32_t except,ctl_ua_type ua)1748 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua)
1749 {
1750 int i;
1751
1752 mtx_assert(&lun->lun_lock, MA_OWNED);
1753 if (lun->pending_ua[port] == NULL)
1754 return;
1755 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1756 if (port * CTL_MAX_INIT_PER_PORT + i == except)
1757 continue;
1758 lun->pending_ua[port][i] |= ua;
1759 }
1760 }
1761
1762 void
ctl_est_ua_all(struct ctl_lun * lun,uint32_t except,ctl_ua_type ua)1763 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1764 {
1765 struct ctl_softc *softc = lun->ctl_softc;
1766 int i;
1767
1768 mtx_assert(&lun->lun_lock, MA_OWNED);
1769 for (i = softc->port_min; i < softc->port_max; i++)
1770 ctl_est_ua_port(lun, i, except, ua);
1771 }
1772
1773 void
ctl_clr_ua(struct ctl_lun * lun,uint32_t initidx,ctl_ua_type ua)1774 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1775 {
1776 struct ctl_softc *softc = lun->ctl_softc;
1777 ctl_ua_type *pu;
1778
1779 if (initidx < softc->init_min || initidx >= softc->init_max)
1780 return;
1781 mtx_assert(&lun->lun_lock, MA_OWNED);
1782 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1783 if (pu == NULL)
1784 return;
1785 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1786 }
1787
1788 void
ctl_clr_ua_all(struct ctl_lun * lun,uint32_t except,ctl_ua_type ua)1789 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1790 {
1791 struct ctl_softc *softc = lun->ctl_softc;
1792 int i, j;
1793
1794 mtx_assert(&lun->lun_lock, MA_OWNED);
1795 for (i = softc->port_min; i < softc->port_max; i++) {
1796 if (lun->pending_ua[i] == NULL)
1797 continue;
1798 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1799 if (i * CTL_MAX_INIT_PER_PORT + j == except)
1800 continue;
1801 lun->pending_ua[i][j] &= ~ua;
1802 }
1803 }
1804 }
1805
1806 void
ctl_clr_ua_allluns(struct ctl_softc * ctl_softc,uint32_t initidx,ctl_ua_type ua_type)1807 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
1808 ctl_ua_type ua_type)
1809 {
1810 struct ctl_lun *lun;
1811
1812 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
1813 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
1814 mtx_lock(&lun->lun_lock);
1815 ctl_clr_ua(lun, initidx, ua_type);
1816 mtx_unlock(&lun->lun_lock);
1817 }
1818 }
1819
1820 static int
ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)1821 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)
1822 {
1823 struct ctl_softc *softc = (struct ctl_softc *)arg1;
1824 struct ctl_lun *lun;
1825 struct ctl_lun_req ireq;
1826 int error, value;
1827
1828 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1;
1829 error = sysctl_handle_int(oidp, &value, 0, req);
1830 if ((error != 0) || (req->newptr == NULL))
1831 return (error);
1832
1833 mtx_lock(&softc->ctl_lock);
1834 if (value == 0)
1835 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1836 else
1837 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1838 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1839 mtx_unlock(&softc->ctl_lock);
1840 bzero(&ireq, sizeof(ireq));
1841 ireq.reqtype = CTL_LUNREQ_MODIFY;
1842 ireq.reqdata.modify.lun_id = lun->lun;
1843 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0,
1844 curthread);
1845 if (ireq.status != CTL_LUN_OK) {
1846 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n",
1847 __func__, ireq.status, ireq.error_str);
1848 }
1849 mtx_lock(&softc->ctl_lock);
1850 }
1851 mtx_unlock(&softc->ctl_lock);
1852 return (0);
1853 }
1854
1855 static int
ctl_init(void)1856 ctl_init(void)
1857 {
1858 struct make_dev_args args;
1859 struct ctl_softc *softc;
1860 int i, error;
1861
1862 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1863 M_WAITOK | M_ZERO);
1864
1865 make_dev_args_init(&args);
1866 args.mda_devsw = &ctl_cdevsw;
1867 args.mda_uid = UID_ROOT;
1868 args.mda_gid = GID_OPERATOR;
1869 args.mda_mode = 0600;
1870 args.mda_si_drv1 = softc;
1871 args.mda_si_drv2 = NULL;
1872 error = make_dev_s(&args, &softc->dev, "cam/ctl");
1873 if (error != 0) {
1874 free(softc, M_DEVBUF);
1875 control_softc = NULL;
1876 return (error);
1877 }
1878
1879 sysctl_ctx_init(&softc->sysctl_ctx);
1880 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1881 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1882 CTLFLAG_RD, 0, "CAM Target Layer");
1883
1884 if (softc->sysctl_tree == NULL) {
1885 printf("%s: unable to allocate sysctl tree\n", __func__);
1886 destroy_dev(softc->dev);
1887 free(softc, M_DEVBUF);
1888 control_softc = NULL;
1889 return (ENOMEM);
1890 }
1891
1892 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1893 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1894 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1895 softc->flags = 0;
1896
1897 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1898 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0,
1899 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)");
1900
1901 if (ctl_max_luns <= 0 || powerof2(ctl_max_luns) == 0) {
1902 printf("Bad value %d for kern.cam.ctl.max_luns, must be a power of two, using %d\n",
1903 ctl_max_luns, CTL_DEFAULT_MAX_LUNS);
1904 ctl_max_luns = CTL_DEFAULT_MAX_LUNS;
1905 }
1906 softc->ctl_luns = malloc(sizeof(struct ctl_lun *) * ctl_max_luns,
1907 M_DEVBUF, M_WAITOK | M_ZERO);
1908 softc->ctl_lun_mask = malloc(sizeof(uint32_t) *
1909 ((ctl_max_luns + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO);
1910 if (ctl_max_ports <= 0 || powerof2(ctl_max_ports) == 0) {
1911 printf("Bad value %d for kern.cam.ctl.max_ports, must be a power of two, using %d\n",
1912 ctl_max_ports, CTL_DEFAULT_MAX_PORTS);
1913 ctl_max_ports = CTL_DEFAULT_MAX_PORTS;
1914 }
1915 softc->ctl_port_mask = malloc(sizeof(uint32_t) *
1916 ((ctl_max_ports + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO);
1917 softc->ctl_ports = malloc(sizeof(struct ctl_port *) * ctl_max_ports,
1918 M_DEVBUF, M_WAITOK | M_ZERO);
1919
1920
1921 /*
1922 * In Copan's HA scheme, the "master" and "slave" roles are
1923 * figured out through the slot the controller is in. Although it
1924 * is an active/active system, someone has to be in charge.
1925 */
1926 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1927 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1928 "HA head ID (0 - no HA)");
1929 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) {
1930 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1931 softc->is_single = 1;
1932 softc->port_cnt = ctl_max_ports;
1933 softc->port_min = 0;
1934 } else {
1935 softc->port_cnt = ctl_max_ports / NUM_HA_SHELVES;
1936 softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
1937 }
1938 softc->port_max = softc->port_min + softc->port_cnt;
1939 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
1940 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
1941
1942 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1943 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
1944 "HA link state (0 - offline, 1 - unknown, 2 - online)");
1945
1946 STAILQ_INIT(&softc->lun_list);
1947 STAILQ_INIT(&softc->pending_lun_queue);
1948 STAILQ_INIT(&softc->fe_list);
1949 STAILQ_INIT(&softc->port_list);
1950 STAILQ_INIT(&softc->be_list);
1951 ctl_tpc_init(softc);
1952
1953 if (worker_threads <= 0)
1954 worker_threads = max(1, mp_ncpus / 4);
1955 if (worker_threads > CTL_MAX_THREADS)
1956 worker_threads = CTL_MAX_THREADS;
1957
1958 for (i = 0; i < worker_threads; i++) {
1959 struct ctl_thread *thr = &softc->threads[i];
1960
1961 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1962 thr->ctl_softc = softc;
1963 STAILQ_INIT(&thr->incoming_queue);
1964 STAILQ_INIT(&thr->rtr_queue);
1965 STAILQ_INIT(&thr->done_queue);
1966 STAILQ_INIT(&thr->isc_queue);
1967
1968 error = kproc_kthread_add(ctl_work_thread, thr,
1969 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1970 if (error != 0) {
1971 printf("error creating CTL work thread!\n");
1972 return (error);
1973 }
1974 }
1975 error = kproc_kthread_add(ctl_lun_thread, softc,
1976 &softc->ctl_proc, &softc->lun_thread, 0, 0, "ctl", "lun");
1977 if (error != 0) {
1978 printf("error creating CTL lun thread!\n");
1979 return (error);
1980 }
1981 error = kproc_kthread_add(ctl_thresh_thread, softc,
1982 &softc->ctl_proc, &softc->thresh_thread, 0, 0, "ctl", "thresh");
1983 if (error != 0) {
1984 printf("error creating CTL threshold thread!\n");
1985 return (error);
1986 }
1987
1988 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1989 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN,
1990 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
1991
1992 if (softc->is_single == 0) {
1993 if (ctl_frontend_register(&ha_frontend) != 0)
1994 softc->is_single = 1;
1995 }
1996 return (0);
1997 }
1998
1999 static int
ctl_shutdown(void)2000 ctl_shutdown(void)
2001 {
2002 struct ctl_softc *softc = control_softc;
2003 int i;
2004
2005 if (softc->is_single == 0)
2006 ctl_frontend_deregister(&ha_frontend);
2007
2008 destroy_dev(softc->dev);
2009
2010 /* Shutdown CTL threads. */
2011 softc->shutdown = 1;
2012 for (i = 0; i < worker_threads; i++) {
2013 struct ctl_thread *thr = &softc->threads[i];
2014 while (thr->thread != NULL) {
2015 wakeup(thr);
2016 if (thr->thread != NULL)
2017 pause("CTL thr shutdown", 1);
2018 }
2019 mtx_destroy(&thr->queue_lock);
2020 }
2021 while (softc->lun_thread != NULL) {
2022 wakeup(&softc->pending_lun_queue);
2023 if (softc->lun_thread != NULL)
2024 pause("CTL thr shutdown", 1);
2025 }
2026 while (softc->thresh_thread != NULL) {
2027 wakeup(softc->thresh_thread);
2028 if (softc->thresh_thread != NULL)
2029 pause("CTL thr shutdown", 1);
2030 }
2031
2032 ctl_tpc_shutdown(softc);
2033 uma_zdestroy(softc->io_zone);
2034 mtx_destroy(&softc->ctl_lock);
2035
2036 free(softc->ctl_luns, M_DEVBUF);
2037 free(softc->ctl_lun_mask, M_DEVBUF);
2038 free(softc->ctl_port_mask, M_DEVBUF);
2039 free(softc->ctl_ports, M_DEVBUF);
2040
2041 sysctl_ctx_free(&softc->sysctl_ctx);
2042
2043 free(softc, M_DEVBUF);
2044 control_softc = NULL;
2045 return (0);
2046 }
2047
2048 static int
ctl_module_event_handler(module_t mod,int what,void * arg)2049 ctl_module_event_handler(module_t mod, int what, void *arg)
2050 {
2051
2052 switch (what) {
2053 case MOD_LOAD:
2054 return (ctl_init());
2055 case MOD_UNLOAD:
2056 return (ctl_shutdown());
2057 default:
2058 return (EOPNOTSUPP);
2059 }
2060 }
2061
2062 /*
2063 * XXX KDM should we do some access checks here? Bump a reference count to
2064 * prevent a CTL module from being unloaded while someone has it open?
2065 */
2066 static int
ctl_open(struct cdev * dev,int flags,int fmt,struct thread * td)2067 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
2068 {
2069 return (0);
2070 }
2071
2072 static int
ctl_close(struct cdev * dev,int flags,int fmt,struct thread * td)2073 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
2074 {
2075 return (0);
2076 }
2077
2078 /*
2079 * Remove an initiator by port number and initiator ID.
2080 * Returns 0 for success, -1 for failure.
2081 */
2082 int
ctl_remove_initiator(struct ctl_port * port,int iid)2083 ctl_remove_initiator(struct ctl_port *port, int iid)
2084 {
2085 struct ctl_softc *softc = port->ctl_softc;
2086 int last;
2087
2088 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2089
2090 if (iid > CTL_MAX_INIT_PER_PORT) {
2091 printf("%s: initiator ID %u > maximun %u!\n",
2092 __func__, iid, CTL_MAX_INIT_PER_PORT);
2093 return (-1);
2094 }
2095
2096 mtx_lock(&softc->ctl_lock);
2097 last = (--port->wwpn_iid[iid].in_use == 0);
2098 port->wwpn_iid[iid].last_use = time_uptime;
2099 mtx_unlock(&softc->ctl_lock);
2100 if (last)
2101 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON);
2102 ctl_isc_announce_iid(port, iid);
2103
2104 return (0);
2105 }
2106
2107 /*
2108 * Add an initiator to the initiator map.
2109 * Returns iid for success, < 0 for failure.
2110 */
2111 int
ctl_add_initiator(struct ctl_port * port,int iid,uint64_t wwpn,char * name)2112 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
2113 {
2114 struct ctl_softc *softc = port->ctl_softc;
2115 time_t best_time;
2116 int i, best;
2117
2118 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2119
2120 if (iid >= CTL_MAX_INIT_PER_PORT) {
2121 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
2122 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
2123 free(name, M_CTL);
2124 return (-1);
2125 }
2126
2127 mtx_lock(&softc->ctl_lock);
2128
2129 if (iid < 0 && (wwpn != 0 || name != NULL)) {
2130 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2131 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
2132 iid = i;
2133 break;
2134 }
2135 if (name != NULL && port->wwpn_iid[i].name != NULL &&
2136 strcmp(name, port->wwpn_iid[i].name) == 0) {
2137 iid = i;
2138 break;
2139 }
2140 }
2141 }
2142
2143 if (iid < 0) {
2144 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2145 if (port->wwpn_iid[i].in_use == 0 &&
2146 port->wwpn_iid[i].wwpn == 0 &&
2147 port->wwpn_iid[i].name == NULL) {
2148 iid = i;
2149 break;
2150 }
2151 }
2152 }
2153
2154 if (iid < 0) {
2155 best = -1;
2156 best_time = INT32_MAX;
2157 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2158 if (port->wwpn_iid[i].in_use == 0) {
2159 if (port->wwpn_iid[i].last_use < best_time) {
2160 best = i;
2161 best_time = port->wwpn_iid[i].last_use;
2162 }
2163 }
2164 }
2165 iid = best;
2166 }
2167
2168 if (iid < 0) {
2169 mtx_unlock(&softc->ctl_lock);
2170 free(name, M_CTL);
2171 return (-2);
2172 }
2173
2174 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
2175 /*
2176 * This is not an error yet.
2177 */
2178 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
2179 #if 0
2180 printf("%s: port %d iid %u WWPN %#jx arrived"
2181 " again\n", __func__, port->targ_port,
2182 iid, (uintmax_t)wwpn);
2183 #endif
2184 goto take;
2185 }
2186 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
2187 strcmp(name, port->wwpn_iid[iid].name) == 0) {
2188 #if 0
2189 printf("%s: port %d iid %u name '%s' arrived"
2190 " again\n", __func__, port->targ_port,
2191 iid, name);
2192 #endif
2193 goto take;
2194 }
2195
2196 /*
2197 * This is an error, but what do we do about it? The
2198 * driver is telling us we have a new WWPN for this
2199 * initiator ID, so we pretty much need to use it.
2200 */
2201 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
2202 " but WWPN %#jx '%s' is still at that address\n",
2203 __func__, port->targ_port, iid, wwpn, name,
2204 (uintmax_t)port->wwpn_iid[iid].wwpn,
2205 port->wwpn_iid[iid].name);
2206 }
2207 take:
2208 free(port->wwpn_iid[iid].name, M_CTL);
2209 port->wwpn_iid[iid].name = name;
2210 port->wwpn_iid[iid].wwpn = wwpn;
2211 port->wwpn_iid[iid].in_use++;
2212 mtx_unlock(&softc->ctl_lock);
2213 ctl_isc_announce_iid(port, iid);
2214
2215 return (iid);
2216 }
2217
2218 static int
ctl_create_iid(struct ctl_port * port,int iid,uint8_t * buf)2219 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
2220 {
2221 int len;
2222
2223 switch (port->port_type) {
2224 case CTL_PORT_FC:
2225 {
2226 struct scsi_transportid_fcp *id =
2227 (struct scsi_transportid_fcp *)buf;
2228 if (port->wwpn_iid[iid].wwpn == 0)
2229 return (0);
2230 memset(id, 0, sizeof(*id));
2231 id->format_protocol = SCSI_PROTO_FC;
2232 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
2233 return (sizeof(*id));
2234 }
2235 case CTL_PORT_ISCSI:
2236 {
2237 struct scsi_transportid_iscsi_port *id =
2238 (struct scsi_transportid_iscsi_port *)buf;
2239 if (port->wwpn_iid[iid].name == NULL)
2240 return (0);
2241 memset(id, 0, 256);
2242 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
2243 SCSI_PROTO_ISCSI;
2244 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
2245 len = roundup2(min(len, 252), 4);
2246 scsi_ulto2b(len, id->additional_length);
2247 return (sizeof(*id) + len);
2248 }
2249 case CTL_PORT_SAS:
2250 {
2251 struct scsi_transportid_sas *id =
2252 (struct scsi_transportid_sas *)buf;
2253 if (port->wwpn_iid[iid].wwpn == 0)
2254 return (0);
2255 memset(id, 0, sizeof(*id));
2256 id->format_protocol = SCSI_PROTO_SAS;
2257 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
2258 return (sizeof(*id));
2259 }
2260 default:
2261 {
2262 struct scsi_transportid_spi *id =
2263 (struct scsi_transportid_spi *)buf;
2264 memset(id, 0, sizeof(*id));
2265 id->format_protocol = SCSI_PROTO_SPI;
2266 scsi_ulto2b(iid, id->scsi_addr);
2267 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
2268 return (sizeof(*id));
2269 }
2270 }
2271 }
2272
2273 /*
2274 * Serialize a command that went down the "wrong" side, and so was sent to
2275 * this controller for execution. The logic is a little different than the
2276 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
2277 * sent back to the other side, but in the success case, we execute the
2278 * command on this side (XFER mode) or tell the other side to execute it
2279 * (SER_ONLY mode).
2280 */
2281 static void
ctl_serialize_other_sc_cmd(struct ctl_scsiio * ctsio)2282 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
2283 {
2284 struct ctl_softc *softc = CTL_SOFTC(ctsio);
2285 struct ctl_port *port = CTL_PORT(ctsio);
2286 union ctl_ha_msg msg_info;
2287 struct ctl_lun *lun;
2288 const struct ctl_cmd_entry *entry;
2289 union ctl_io *bio;
2290 uint32_t targ_lun;
2291
2292 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
2293
2294 /* Make sure that we know about this port. */
2295 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) {
2296 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2297 /*retry_count*/ 1);
2298 goto badjuju;
2299 }
2300
2301 /* Make sure that we know about this LUN. */
2302 mtx_lock(&softc->ctl_lock);
2303 if (targ_lun >= ctl_max_luns ||
2304 (lun = softc->ctl_luns[targ_lun]) == NULL) {
2305 mtx_unlock(&softc->ctl_lock);
2306
2307 /*
2308 * The other node would not send this request to us unless
2309 * received announce that we are primary node for this LUN.
2310 * If this LUN does not exist now, it is probably result of
2311 * a race, so respond to initiator in the most opaque way.
2312 */
2313 ctl_set_busy(ctsio);
2314 goto badjuju;
2315 }
2316 mtx_lock(&lun->lun_lock);
2317 mtx_unlock(&softc->ctl_lock);
2318
2319 /*
2320 * If the LUN is invalid, pretend that it doesn't exist.
2321 * It will go away as soon as all pending I/Os completed.
2322 */
2323 if (lun->flags & CTL_LUN_DISABLED) {
2324 mtx_unlock(&lun->lun_lock);
2325 ctl_set_busy(ctsio);
2326 goto badjuju;
2327 }
2328
2329 entry = ctl_get_cmd_entry(ctsio, NULL);
2330 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
2331 mtx_unlock(&lun->lun_lock);
2332 goto badjuju;
2333 }
2334
2335 CTL_LUN(ctsio) = lun;
2336 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
2337
2338 /*
2339 * Every I/O goes into the OOA queue for a
2340 * particular LUN, and stays there until completion.
2341 */
2342 #ifdef CTL_TIME_IO
2343 if (TAILQ_EMPTY(&lun->ooa_queue))
2344 lun->idle_time += getsbinuptime() - lun->last_busy;
2345 #endif
2346 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2347
2348 bio = (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, ooa_links);
2349 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, &bio)) {
2350 case CTL_ACTION_BLOCK:
2351 ctsio->io_hdr.blocker = bio;
2352 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctsio->io_hdr,
2353 blocked_links);
2354 mtx_unlock(&lun->lun_lock);
2355 break;
2356 case CTL_ACTION_PASS:
2357 case CTL_ACTION_SKIP:
2358 if (softc->ha_mode == CTL_HA_MODE_XFER) {
2359 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
2360 ctl_enqueue_rtr((union ctl_io *)ctsio);
2361 mtx_unlock(&lun->lun_lock);
2362 } else {
2363 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
2364 mtx_unlock(&lun->lun_lock);
2365
2366 /* send msg back to other side */
2367 msg_info.hdr.original_sc = ctsio->io_hdr.remote_io;
2368 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
2369 msg_info.hdr.msg_type = CTL_MSG_R2R;
2370 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2371 sizeof(msg_info.hdr), M_WAITOK);
2372 }
2373 break;
2374 case CTL_ACTION_OVERLAP:
2375 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2376 mtx_unlock(&lun->lun_lock);
2377 ctl_set_overlapped_cmd(ctsio);
2378 goto badjuju;
2379 case CTL_ACTION_OVERLAP_TAG:
2380 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2381 mtx_unlock(&lun->lun_lock);
2382 ctl_set_overlapped_tag(ctsio, ctsio->tag_num);
2383 goto badjuju;
2384 case CTL_ACTION_ERROR:
2385 default:
2386 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2387 mtx_unlock(&lun->lun_lock);
2388
2389 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2390 /*retry_count*/ 0);
2391 badjuju:
2392 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
2393 msg_info.hdr.original_sc = ctsio->io_hdr.remote_io;
2394 msg_info.hdr.serializing_sc = NULL;
2395 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
2396 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2397 sizeof(msg_info.scsi), M_WAITOK);
2398 ctl_free_io((union ctl_io *)ctsio);
2399 break;
2400 }
2401 }
2402
2403 /*
2404 * Returns 0 for success, errno for failure.
2405 */
2406 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)2407 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2408 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2409 {
2410 union ctl_io *io;
2411
2412 mtx_lock(&lun->lun_lock);
2413 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2414 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2415 ooa_links)) {
2416 struct ctl_ooa_entry *entry;
2417
2418 /*
2419 * If we've got more than we can fit, just count the
2420 * remaining entries.
2421 */
2422 if (*cur_fill_num >= ooa_hdr->alloc_num)
2423 continue;
2424
2425 entry = &kern_entries[*cur_fill_num];
2426
2427 entry->tag_num = io->scsiio.tag_num;
2428 entry->lun_num = lun->lun;
2429 #ifdef CTL_TIME_IO
2430 entry->start_bt = io->io_hdr.start_bt;
2431 #endif
2432 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2433 entry->cdb_len = io->scsiio.cdb_len;
2434 if (io->io_hdr.blocker != NULL)
2435 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2436
2437 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2438 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2439
2440 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2441 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2442
2443 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2444 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2445
2446 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2447 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2448 }
2449 mtx_unlock(&lun->lun_lock);
2450 }
2451
2452 /*
2453 * Escape characters that are illegal or not recommended in XML.
2454 */
2455 int
ctl_sbuf_printf_esc(struct sbuf * sb,char * str,int size)2456 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2457 {
2458 char *end = str + size;
2459 int retval;
2460
2461 retval = 0;
2462
2463 for (; *str && str < end; str++) {
2464 switch (*str) {
2465 case '&':
2466 retval = sbuf_printf(sb, "&");
2467 break;
2468 case '>':
2469 retval = sbuf_printf(sb, ">");
2470 break;
2471 case '<':
2472 retval = sbuf_printf(sb, "<");
2473 break;
2474 default:
2475 retval = sbuf_putc(sb, *str);
2476 break;
2477 }
2478
2479 if (retval != 0)
2480 break;
2481
2482 }
2483
2484 return (retval);
2485 }
2486
2487 static void
ctl_id_sbuf(struct ctl_devid * id,struct sbuf * sb)2488 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2489 {
2490 struct scsi_vpd_id_descriptor *desc;
2491 int i;
2492
2493 if (id == NULL || id->len < 4)
2494 return;
2495 desc = (struct scsi_vpd_id_descriptor *)id->data;
2496 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2497 case SVPD_ID_TYPE_T10:
2498 sbuf_printf(sb, "t10.");
2499 break;
2500 case SVPD_ID_TYPE_EUI64:
2501 sbuf_printf(sb, "eui.");
2502 break;
2503 case SVPD_ID_TYPE_NAA:
2504 sbuf_printf(sb, "naa.");
2505 break;
2506 case SVPD_ID_TYPE_SCSI_NAME:
2507 break;
2508 }
2509 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2510 case SVPD_ID_CODESET_BINARY:
2511 for (i = 0; i < desc->length; i++)
2512 sbuf_printf(sb, "%02x", desc->identifier[i]);
2513 break;
2514 case SVPD_ID_CODESET_ASCII:
2515 sbuf_printf(sb, "%.*s", (int)desc->length,
2516 (char *)desc->identifier);
2517 break;
2518 case SVPD_ID_CODESET_UTF8:
2519 sbuf_printf(sb, "%s", (char *)desc->identifier);
2520 break;
2521 }
2522 }
2523
2524 static int
ctl_ioctl(struct cdev * dev,u_long cmd,caddr_t addr,int flag,struct thread * td)2525 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2526 struct thread *td)
2527 {
2528 struct ctl_softc *softc = dev->si_drv1;
2529 struct ctl_port *port;
2530 struct ctl_lun *lun;
2531 int retval;
2532
2533 retval = 0;
2534
2535 switch (cmd) {
2536 case CTL_IO:
2537 retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2538 break;
2539 case CTL_ENABLE_PORT:
2540 case CTL_DISABLE_PORT:
2541 case CTL_SET_PORT_WWNS: {
2542 struct ctl_port *port;
2543 struct ctl_port_entry *entry;
2544
2545 entry = (struct ctl_port_entry *)addr;
2546
2547 mtx_lock(&softc->ctl_lock);
2548 STAILQ_FOREACH(port, &softc->port_list, links) {
2549 int action, done;
2550
2551 if (port->targ_port < softc->port_min ||
2552 port->targ_port >= softc->port_max)
2553 continue;
2554
2555 action = 0;
2556 done = 0;
2557 if ((entry->port_type == CTL_PORT_NONE)
2558 && (entry->targ_port == port->targ_port)) {
2559 /*
2560 * If the user only wants to enable or
2561 * disable or set WWNs on a specific port,
2562 * do the operation and we're done.
2563 */
2564 action = 1;
2565 done = 1;
2566 } else if (entry->port_type & port->port_type) {
2567 /*
2568 * Compare the user's type mask with the
2569 * particular frontend type to see if we
2570 * have a match.
2571 */
2572 action = 1;
2573 done = 0;
2574
2575 /*
2576 * Make sure the user isn't trying to set
2577 * WWNs on multiple ports at the same time.
2578 */
2579 if (cmd == CTL_SET_PORT_WWNS) {
2580 printf("%s: Can't set WWNs on "
2581 "multiple ports\n", __func__);
2582 retval = EINVAL;
2583 break;
2584 }
2585 }
2586 if (action == 0)
2587 continue;
2588
2589 /*
2590 * XXX KDM we have to drop the lock here, because
2591 * the online/offline operations can potentially
2592 * block. We need to reference count the frontends
2593 * so they can't go away,
2594 */
2595 if (cmd == CTL_ENABLE_PORT) {
2596 mtx_unlock(&softc->ctl_lock);
2597 ctl_port_online(port);
2598 mtx_lock(&softc->ctl_lock);
2599 } else if (cmd == CTL_DISABLE_PORT) {
2600 mtx_unlock(&softc->ctl_lock);
2601 ctl_port_offline(port);
2602 mtx_lock(&softc->ctl_lock);
2603 } else if (cmd == CTL_SET_PORT_WWNS) {
2604 ctl_port_set_wwns(port,
2605 (entry->flags & CTL_PORT_WWNN_VALID) ?
2606 1 : 0, entry->wwnn,
2607 (entry->flags & CTL_PORT_WWPN_VALID) ?
2608 1 : 0, entry->wwpn);
2609 }
2610 if (done != 0)
2611 break;
2612 }
2613 mtx_unlock(&softc->ctl_lock);
2614 break;
2615 }
2616 case CTL_GET_OOA: {
2617 struct ctl_ooa *ooa_hdr;
2618 struct ctl_ooa_entry *entries;
2619 uint32_t cur_fill_num;
2620
2621 ooa_hdr = (struct ctl_ooa *)addr;
2622
2623 if ((ooa_hdr->alloc_len == 0)
2624 || (ooa_hdr->alloc_num == 0)) {
2625 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2626 "must be non-zero\n", __func__,
2627 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2628 retval = EINVAL;
2629 break;
2630 }
2631
2632 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2633 sizeof(struct ctl_ooa_entry))) {
2634 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2635 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2636 __func__, ooa_hdr->alloc_len,
2637 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2638 retval = EINVAL;
2639 break;
2640 }
2641
2642 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2643 if (entries == NULL) {
2644 printf("%s: could not allocate %d bytes for OOA "
2645 "dump\n", __func__, ooa_hdr->alloc_len);
2646 retval = ENOMEM;
2647 break;
2648 }
2649
2650 mtx_lock(&softc->ctl_lock);
2651 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 &&
2652 (ooa_hdr->lun_num >= ctl_max_luns ||
2653 softc->ctl_luns[ooa_hdr->lun_num] == NULL)) {
2654 mtx_unlock(&softc->ctl_lock);
2655 free(entries, M_CTL);
2656 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2657 __func__, (uintmax_t)ooa_hdr->lun_num);
2658 retval = EINVAL;
2659 break;
2660 }
2661
2662 cur_fill_num = 0;
2663
2664 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2665 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2666 ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2667 ooa_hdr, entries);
2668 }
2669 } else {
2670 lun = softc->ctl_luns[ooa_hdr->lun_num];
2671 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr,
2672 entries);
2673 }
2674 mtx_unlock(&softc->ctl_lock);
2675
2676 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2677 ooa_hdr->fill_len = ooa_hdr->fill_num *
2678 sizeof(struct ctl_ooa_entry);
2679 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2680 if (retval != 0) {
2681 printf("%s: error copying out %d bytes for OOA dump\n",
2682 __func__, ooa_hdr->fill_len);
2683 }
2684
2685 getbinuptime(&ooa_hdr->cur_bt);
2686
2687 if (cur_fill_num > ooa_hdr->alloc_num) {
2688 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2689 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2690 } else {
2691 ooa_hdr->dropped_num = 0;
2692 ooa_hdr->status = CTL_OOA_OK;
2693 }
2694
2695 free(entries, M_CTL);
2696 break;
2697 }
2698 case CTL_DELAY_IO: {
2699 struct ctl_io_delay_info *delay_info;
2700
2701 delay_info = (struct ctl_io_delay_info *)addr;
2702
2703 #ifdef CTL_IO_DELAY
2704 mtx_lock(&softc->ctl_lock);
2705 if (delay_info->lun_id >= ctl_max_luns ||
2706 (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) {
2707 mtx_unlock(&softc->ctl_lock);
2708 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2709 break;
2710 }
2711 mtx_lock(&lun->lun_lock);
2712 mtx_unlock(&softc->ctl_lock);
2713 delay_info->status = CTL_DELAY_STATUS_OK;
2714 switch (delay_info->delay_type) {
2715 case CTL_DELAY_TYPE_CONT:
2716 case CTL_DELAY_TYPE_ONESHOT:
2717 break;
2718 default:
2719 delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE;
2720 break;
2721 }
2722 switch (delay_info->delay_loc) {
2723 case CTL_DELAY_LOC_DATAMOVE:
2724 lun->delay_info.datamove_type = delay_info->delay_type;
2725 lun->delay_info.datamove_delay = delay_info->delay_secs;
2726 break;
2727 case CTL_DELAY_LOC_DONE:
2728 lun->delay_info.done_type = delay_info->delay_type;
2729 lun->delay_info.done_delay = delay_info->delay_secs;
2730 break;
2731 default:
2732 delay_info->status = CTL_DELAY_STATUS_INVALID_LOC;
2733 break;
2734 }
2735 mtx_unlock(&lun->lun_lock);
2736 #else
2737 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2738 #endif /* CTL_IO_DELAY */
2739 break;
2740 }
2741 case CTL_ERROR_INJECT: {
2742 struct ctl_error_desc *err_desc, *new_err_desc;
2743
2744 err_desc = (struct ctl_error_desc *)addr;
2745
2746 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2747 M_WAITOK | M_ZERO);
2748 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2749
2750 mtx_lock(&softc->ctl_lock);
2751 if (err_desc->lun_id >= ctl_max_luns ||
2752 (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) {
2753 mtx_unlock(&softc->ctl_lock);
2754 free(new_err_desc, M_CTL);
2755 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2756 __func__, (uintmax_t)err_desc->lun_id);
2757 retval = EINVAL;
2758 break;
2759 }
2760 mtx_lock(&lun->lun_lock);
2761 mtx_unlock(&softc->ctl_lock);
2762
2763 /*
2764 * We could do some checking here to verify the validity
2765 * of the request, but given the complexity of error
2766 * injection requests, the checking logic would be fairly
2767 * complex.
2768 *
2769 * For now, if the request is invalid, it just won't get
2770 * executed and might get deleted.
2771 */
2772 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2773
2774 /*
2775 * XXX KDM check to make sure the serial number is unique,
2776 * in case we somehow manage to wrap. That shouldn't
2777 * happen for a very long time, but it's the right thing to
2778 * do.
2779 */
2780 new_err_desc->serial = lun->error_serial;
2781 err_desc->serial = lun->error_serial;
2782 lun->error_serial++;
2783
2784 mtx_unlock(&lun->lun_lock);
2785 break;
2786 }
2787 case CTL_ERROR_INJECT_DELETE: {
2788 struct ctl_error_desc *delete_desc, *desc, *desc2;
2789 int delete_done;
2790
2791 delete_desc = (struct ctl_error_desc *)addr;
2792 delete_done = 0;
2793
2794 mtx_lock(&softc->ctl_lock);
2795 if (delete_desc->lun_id >= ctl_max_luns ||
2796 (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) {
2797 mtx_unlock(&softc->ctl_lock);
2798 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2799 __func__, (uintmax_t)delete_desc->lun_id);
2800 retval = EINVAL;
2801 break;
2802 }
2803 mtx_lock(&lun->lun_lock);
2804 mtx_unlock(&softc->ctl_lock);
2805 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2806 if (desc->serial != delete_desc->serial)
2807 continue;
2808
2809 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2810 links);
2811 free(desc, M_CTL);
2812 delete_done = 1;
2813 }
2814 mtx_unlock(&lun->lun_lock);
2815 if (delete_done == 0) {
2816 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2817 "error serial %ju on LUN %u\n", __func__,
2818 delete_desc->serial, delete_desc->lun_id);
2819 retval = EINVAL;
2820 break;
2821 }
2822 break;
2823 }
2824 case CTL_DUMP_STRUCTS: {
2825 int j, k;
2826 struct ctl_port *port;
2827 struct ctl_frontend *fe;
2828
2829 mtx_lock(&softc->ctl_lock);
2830 printf("CTL Persistent Reservation information start:\n");
2831 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2832 mtx_lock(&lun->lun_lock);
2833 if ((lun->flags & CTL_LUN_DISABLED) != 0) {
2834 mtx_unlock(&lun->lun_lock);
2835 continue;
2836 }
2837
2838 for (j = 0; j < ctl_max_ports; j++) {
2839 if (lun->pr_keys[j] == NULL)
2840 continue;
2841 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2842 if (lun->pr_keys[j][k] == 0)
2843 continue;
2844 printf(" LUN %ju port %d iid %d key "
2845 "%#jx\n", lun->lun, j, k,
2846 (uintmax_t)lun->pr_keys[j][k]);
2847 }
2848 }
2849 mtx_unlock(&lun->lun_lock);
2850 }
2851 printf("CTL Persistent Reservation information end\n");
2852 printf("CTL Ports:\n");
2853 STAILQ_FOREACH(port, &softc->port_list, links) {
2854 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2855 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
2856 port->frontend->name, port->port_type,
2857 port->physical_port, port->virtual_port,
2858 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2859 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2860 if (port->wwpn_iid[j].in_use == 0 &&
2861 port->wwpn_iid[j].wwpn == 0 &&
2862 port->wwpn_iid[j].name == NULL)
2863 continue;
2864
2865 printf(" iid %u use %d WWPN %#jx '%s'\n",
2866 j, port->wwpn_iid[j].in_use,
2867 (uintmax_t)port->wwpn_iid[j].wwpn,
2868 port->wwpn_iid[j].name);
2869 }
2870 }
2871 printf("CTL Port information end\n");
2872 mtx_unlock(&softc->ctl_lock);
2873 /*
2874 * XXX KDM calling this without a lock. We'd likely want
2875 * to drop the lock before calling the frontend's dump
2876 * routine anyway.
2877 */
2878 printf("CTL Frontends:\n");
2879 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2880 printf(" Frontend '%s'\n", fe->name);
2881 if (fe->fe_dump != NULL)
2882 fe->fe_dump();
2883 }
2884 printf("CTL Frontend information end\n");
2885 break;
2886 }
2887 case CTL_LUN_REQ: {
2888 struct ctl_lun_req *lun_req;
2889 struct ctl_backend_driver *backend;
2890 void *packed;
2891 nvlist_t *tmp_args_nvl;
2892 size_t packed_len;
2893
2894 lun_req = (struct ctl_lun_req *)addr;
2895 tmp_args_nvl = lun_req->args_nvl;
2896
2897 backend = ctl_backend_find(lun_req->backend);
2898 if (backend == NULL) {
2899 lun_req->status = CTL_LUN_ERROR;
2900 snprintf(lun_req->error_str,
2901 sizeof(lun_req->error_str),
2902 "Backend \"%s\" not found.",
2903 lun_req->backend);
2904 break;
2905 }
2906
2907 if (lun_req->args != NULL) {
2908 packed = malloc(lun_req->args_len, M_CTL, M_WAITOK);
2909 if (copyin(lun_req->args, packed, lun_req->args_len) != 0) {
2910 free(packed, M_CTL);
2911 lun_req->status = CTL_LUN_ERROR;
2912 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
2913 "Cannot copyin args.");
2914 break;
2915 }
2916 lun_req->args_nvl = nvlist_unpack(packed,
2917 lun_req->args_len, 0);
2918 free(packed, M_CTL);
2919
2920 if (lun_req->args_nvl == NULL) {
2921 lun_req->status = CTL_LUN_ERROR;
2922 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
2923 "Cannot unpack args nvlist.");
2924 break;
2925 }
2926 } else
2927 lun_req->args_nvl = nvlist_create(0);
2928
2929 retval = backend->ioctl(dev, cmd, addr, flag, td);
2930 nvlist_destroy(lun_req->args_nvl);
2931 lun_req->args_nvl = tmp_args_nvl;
2932
2933 if (lun_req->result_nvl != NULL) {
2934 if (lun_req->result != NULL) {
2935 packed = nvlist_pack(lun_req->result_nvl,
2936 &packed_len);
2937 if (packed == NULL) {
2938 lun_req->status = CTL_LUN_ERROR;
2939 snprintf(lun_req->error_str,
2940 sizeof(lun_req->error_str),
2941 "Cannot pack result nvlist.");
2942 break;
2943 }
2944
2945 if (packed_len > lun_req->result_len) {
2946 lun_req->status = CTL_LUN_ERROR;
2947 snprintf(lun_req->error_str,
2948 sizeof(lun_req->error_str),
2949 "Result nvlist too large.");
2950 free(packed, M_NVLIST);
2951 break;
2952 }
2953
2954 if (copyout(packed, lun_req->result, packed_len)) {
2955 lun_req->status = CTL_LUN_ERROR;
2956 snprintf(lun_req->error_str,
2957 sizeof(lun_req->error_str),
2958 "Cannot copyout() the result.");
2959 free(packed, M_NVLIST);
2960 break;
2961 }
2962
2963 lun_req->result_len = packed_len;
2964 free(packed, M_NVLIST);
2965 }
2966
2967 nvlist_destroy(lun_req->result_nvl);
2968 }
2969 break;
2970 }
2971 case CTL_LUN_LIST: {
2972 struct sbuf *sb;
2973 struct ctl_lun_list *list;
2974 const char *name, *value;
2975 void *cookie;
2976 int type;
2977
2978 list = (struct ctl_lun_list *)addr;
2979
2980 /*
2981 * Allocate a fixed length sbuf here, based on the length
2982 * of the user's buffer. We could allocate an auto-extending
2983 * buffer, and then tell the user how much larger our
2984 * amount of data is than his buffer, but that presents
2985 * some problems:
2986 *
2987 * 1. The sbuf(9) routines use a blocking malloc, and so
2988 * we can't hold a lock while calling them with an
2989 * auto-extending buffer.
2990 *
2991 * 2. There is not currently a LUN reference counting
2992 * mechanism, outside of outstanding transactions on
2993 * the LUN's OOA queue. So a LUN could go away on us
2994 * while we're getting the LUN number, backend-specific
2995 * information, etc. Thus, given the way things
2996 * currently work, we need to hold the CTL lock while
2997 * grabbing LUN information.
2998 *
2999 * So, from the user's standpoint, the best thing to do is
3000 * allocate what he thinks is a reasonable buffer length,
3001 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3002 * double the buffer length and try again. (And repeat
3003 * that until he succeeds.)
3004 */
3005 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3006 if (sb == NULL) {
3007 list->status = CTL_LUN_LIST_ERROR;
3008 snprintf(list->error_str, sizeof(list->error_str),
3009 "Unable to allocate %d bytes for LUN list",
3010 list->alloc_len);
3011 break;
3012 }
3013
3014 sbuf_printf(sb, "<ctllunlist>\n");
3015
3016 mtx_lock(&softc->ctl_lock);
3017 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3018 mtx_lock(&lun->lun_lock);
3019 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3020 (uintmax_t)lun->lun);
3021
3022 /*
3023 * Bail out as soon as we see that we've overfilled
3024 * the buffer.
3025 */
3026 if (retval != 0)
3027 break;
3028
3029 retval = sbuf_printf(sb, "\t<backend_type>%s"
3030 "</backend_type>\n",
3031 (lun->backend == NULL) ? "none" :
3032 lun->backend->name);
3033
3034 if (retval != 0)
3035 break;
3036
3037 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3038 lun->be_lun->lun_type);
3039
3040 if (retval != 0)
3041 break;
3042
3043 if (lun->backend == NULL) {
3044 retval = sbuf_printf(sb, "</lun>\n");
3045 if (retval != 0)
3046 break;
3047 continue;
3048 }
3049
3050 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3051 (lun->be_lun->maxlba > 0) ?
3052 lun->be_lun->maxlba + 1 : 0);
3053
3054 if (retval != 0)
3055 break;
3056
3057 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3058 lun->be_lun->blocksize);
3059
3060 if (retval != 0)
3061 break;
3062
3063 retval = sbuf_printf(sb, "\t<serial_number>");
3064
3065 if (retval != 0)
3066 break;
3067
3068 retval = ctl_sbuf_printf_esc(sb,
3069 lun->be_lun->serial_num,
3070 sizeof(lun->be_lun->serial_num));
3071
3072 if (retval != 0)
3073 break;
3074
3075 retval = sbuf_printf(sb, "</serial_number>\n");
3076
3077 if (retval != 0)
3078 break;
3079
3080 retval = sbuf_printf(sb, "\t<device_id>");
3081
3082 if (retval != 0)
3083 break;
3084
3085 retval = ctl_sbuf_printf_esc(sb,
3086 lun->be_lun->device_id,
3087 sizeof(lun->be_lun->device_id));
3088
3089 if (retval != 0)
3090 break;
3091
3092 retval = sbuf_printf(sb, "</device_id>\n");
3093
3094 if (retval != 0)
3095 break;
3096
3097 if (lun->backend->lun_info != NULL) {
3098 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3099 if (retval != 0)
3100 break;
3101 }
3102
3103 cookie = NULL;
3104 while ((name = nvlist_next(lun->be_lun->options, &type,
3105 &cookie)) != NULL) {
3106 sbuf_printf(sb, "\t<%s>", name);
3107
3108 if (type == NV_TYPE_STRING) {
3109 value = dnvlist_get_string(
3110 lun->be_lun->options, name, NULL);
3111 if (value != NULL)
3112 sbuf_printf(sb, "%s", value);
3113 }
3114
3115 sbuf_printf(sb, "</%s>\n", name);
3116 }
3117
3118 retval = sbuf_printf(sb, "</lun>\n");
3119
3120 if (retval != 0)
3121 break;
3122 mtx_unlock(&lun->lun_lock);
3123 }
3124 if (lun != NULL)
3125 mtx_unlock(&lun->lun_lock);
3126 mtx_unlock(&softc->ctl_lock);
3127
3128 if ((retval != 0)
3129 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3130 retval = 0;
3131 sbuf_delete(sb);
3132 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3133 snprintf(list->error_str, sizeof(list->error_str),
3134 "Out of space, %d bytes is too small",
3135 list->alloc_len);
3136 break;
3137 }
3138
3139 sbuf_finish(sb);
3140
3141 retval = copyout(sbuf_data(sb), list->lun_xml,
3142 sbuf_len(sb) + 1);
3143
3144 list->fill_len = sbuf_len(sb) + 1;
3145 list->status = CTL_LUN_LIST_OK;
3146 sbuf_delete(sb);
3147 break;
3148 }
3149 case CTL_ISCSI: {
3150 struct ctl_iscsi *ci;
3151 struct ctl_frontend *fe;
3152
3153 ci = (struct ctl_iscsi *)addr;
3154
3155 fe = ctl_frontend_find("iscsi");
3156 if (fe == NULL) {
3157 ci->status = CTL_ISCSI_ERROR;
3158 snprintf(ci->error_str, sizeof(ci->error_str),
3159 "Frontend \"iscsi\" not found.");
3160 break;
3161 }
3162
3163 retval = fe->ioctl(dev, cmd, addr, flag, td);
3164 break;
3165 }
3166 case CTL_PORT_REQ: {
3167 struct ctl_req *req;
3168 struct ctl_frontend *fe;
3169 void *packed;
3170 nvlist_t *tmp_args_nvl;
3171 size_t packed_len;
3172
3173 req = (struct ctl_req *)addr;
3174 tmp_args_nvl = req->args_nvl;
3175
3176 fe = ctl_frontend_find(req->driver);
3177 if (fe == NULL) {
3178 req->status = CTL_LUN_ERROR;
3179 snprintf(req->error_str, sizeof(req->error_str),
3180 "Frontend \"%s\" not found.", req->driver);
3181 break;
3182 }
3183
3184 if (req->args != NULL) {
3185 packed = malloc(req->args_len, M_CTL, M_WAITOK);
3186 if (copyin(req->args, packed, req->args_len) != 0) {
3187 free(packed, M_CTL);
3188 req->status = CTL_LUN_ERROR;
3189 snprintf(req->error_str, sizeof(req->error_str),
3190 "Cannot copyin args.");
3191 break;
3192 }
3193 req->args_nvl = nvlist_unpack(packed,
3194 req->args_len, 0);
3195 free(packed, M_CTL);
3196
3197 if (req->args_nvl == NULL) {
3198 req->status = CTL_LUN_ERROR;
3199 snprintf(req->error_str, sizeof(req->error_str),
3200 "Cannot unpack args nvlist.");
3201 break;
3202 }
3203 } else
3204 req->args_nvl = nvlist_create(0);
3205
3206 if (fe->ioctl)
3207 retval = fe->ioctl(dev, cmd, addr, flag, td);
3208 else
3209 retval = ENODEV;
3210
3211 nvlist_destroy(req->args_nvl);
3212 req->args_nvl = tmp_args_nvl;
3213
3214 if (req->result_nvl != NULL) {
3215 if (req->result != NULL) {
3216 packed = nvlist_pack(req->result_nvl,
3217 &packed_len);
3218 if (packed == NULL) {
3219 req->status = CTL_LUN_ERROR;
3220 snprintf(req->error_str,
3221 sizeof(req->error_str),
3222 "Cannot pack result nvlist.");
3223 break;
3224 }
3225
3226 if (packed_len > req->result_len) {
3227 req->status = CTL_LUN_ERROR;
3228 snprintf(req->error_str,
3229 sizeof(req->error_str),
3230 "Result nvlist too large.");
3231 free(packed, M_NVLIST);
3232 break;
3233 }
3234
3235 if (copyout(packed, req->result, packed_len)) {
3236 req->status = CTL_LUN_ERROR;
3237 snprintf(req->error_str,
3238 sizeof(req->error_str),
3239 "Cannot copyout() the result.");
3240 free(packed, M_NVLIST);
3241 break;
3242 }
3243
3244 req->result_len = packed_len;
3245 free(packed, M_NVLIST);
3246 }
3247
3248 nvlist_destroy(req->result_nvl);
3249 }
3250 break;
3251 }
3252 case CTL_PORT_LIST: {
3253 struct sbuf *sb;
3254 struct ctl_port *port;
3255 struct ctl_lun_list *list;
3256 const char *name, *value;
3257 void *cookie;
3258 int j, type;
3259 uint32_t plun;
3260
3261 list = (struct ctl_lun_list *)addr;
3262
3263 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3264 if (sb == NULL) {
3265 list->status = CTL_LUN_LIST_ERROR;
3266 snprintf(list->error_str, sizeof(list->error_str),
3267 "Unable to allocate %d bytes for LUN list",
3268 list->alloc_len);
3269 break;
3270 }
3271
3272 sbuf_printf(sb, "<ctlportlist>\n");
3273
3274 mtx_lock(&softc->ctl_lock);
3275 STAILQ_FOREACH(port, &softc->port_list, links) {
3276 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3277 (uintmax_t)port->targ_port);
3278
3279 /*
3280 * Bail out as soon as we see that we've overfilled
3281 * the buffer.
3282 */
3283 if (retval != 0)
3284 break;
3285
3286 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3287 "</frontend_type>\n", port->frontend->name);
3288 if (retval != 0)
3289 break;
3290
3291 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3292 port->port_type);
3293 if (retval != 0)
3294 break;
3295
3296 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3297 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3298 if (retval != 0)
3299 break;
3300
3301 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3302 port->port_name);
3303 if (retval != 0)
3304 break;
3305
3306 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3307 port->physical_port);
3308 if (retval != 0)
3309 break;
3310
3311 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3312 port->virtual_port);
3313 if (retval != 0)
3314 break;
3315
3316 if (port->target_devid != NULL) {
3317 sbuf_printf(sb, "\t<target>");
3318 ctl_id_sbuf(port->target_devid, sb);
3319 sbuf_printf(sb, "</target>\n");
3320 }
3321
3322 if (port->port_devid != NULL) {
3323 sbuf_printf(sb, "\t<port>");
3324 ctl_id_sbuf(port->port_devid, sb);
3325 sbuf_printf(sb, "</port>\n");
3326 }
3327
3328 if (port->port_info != NULL) {
3329 retval = port->port_info(port->onoff_arg, sb);
3330 if (retval != 0)
3331 break;
3332 }
3333
3334 cookie = NULL;
3335 while ((name = nvlist_next(port->options, &type,
3336 &cookie)) != NULL) {
3337 sbuf_printf(sb, "\t<%s>", name);
3338
3339 if (type == NV_TYPE_STRING) {
3340 value = dnvlist_get_string(port->options,
3341 name, NULL);
3342 if (value != NULL)
3343 sbuf_printf(sb, "%s", value);
3344 }
3345
3346 sbuf_printf(sb, "</%s>\n", name);
3347 }
3348
3349 if (port->lun_map != NULL) {
3350 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3351 for (j = 0; j < port->lun_map_size; j++) {
3352 plun = ctl_lun_map_from_port(port, j);
3353 if (plun == UINT32_MAX)
3354 continue;
3355 sbuf_printf(sb,
3356 "\t<lun id=\"%u\">%u</lun>\n",
3357 j, plun);
3358 }
3359 }
3360
3361 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3362 if (port->wwpn_iid[j].in_use == 0 ||
3363 (port->wwpn_iid[j].wwpn == 0 &&
3364 port->wwpn_iid[j].name == NULL))
3365 continue;
3366
3367 if (port->wwpn_iid[j].name != NULL)
3368 retval = sbuf_printf(sb,
3369 "\t<initiator id=\"%u\">%s</initiator>\n",
3370 j, port->wwpn_iid[j].name);
3371 else
3372 retval = sbuf_printf(sb,
3373 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3374 j, port->wwpn_iid[j].wwpn);
3375 if (retval != 0)
3376 break;
3377 }
3378 if (retval != 0)
3379 break;
3380
3381 retval = sbuf_printf(sb, "</targ_port>\n");
3382 if (retval != 0)
3383 break;
3384 }
3385 mtx_unlock(&softc->ctl_lock);
3386
3387 if ((retval != 0)
3388 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3389 retval = 0;
3390 sbuf_delete(sb);
3391 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3392 snprintf(list->error_str, sizeof(list->error_str),
3393 "Out of space, %d bytes is too small",
3394 list->alloc_len);
3395 break;
3396 }
3397
3398 sbuf_finish(sb);
3399
3400 retval = copyout(sbuf_data(sb), list->lun_xml,
3401 sbuf_len(sb) + 1);
3402
3403 list->fill_len = sbuf_len(sb) + 1;
3404 list->status = CTL_LUN_LIST_OK;
3405 sbuf_delete(sb);
3406 break;
3407 }
3408 case CTL_LUN_MAP: {
3409 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3410 struct ctl_port *port;
3411
3412 mtx_lock(&softc->ctl_lock);
3413 if (lm->port < softc->port_min ||
3414 lm->port >= softc->port_max ||
3415 (port = softc->ctl_ports[lm->port]) == NULL) {
3416 mtx_unlock(&softc->ctl_lock);
3417 return (ENXIO);
3418 }
3419 if (port->status & CTL_PORT_STATUS_ONLINE) {
3420 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3421 if (ctl_lun_map_to_port(port, lun->lun) ==
3422 UINT32_MAX)
3423 continue;
3424 mtx_lock(&lun->lun_lock);
3425 ctl_est_ua_port(lun, lm->port, -1,
3426 CTL_UA_LUN_CHANGE);
3427 mtx_unlock(&lun->lun_lock);
3428 }
3429 }
3430 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3431 if (lm->plun != UINT32_MAX) {
3432 if (lm->lun == UINT32_MAX)
3433 retval = ctl_lun_map_unset(port, lm->plun);
3434 else if (lm->lun < ctl_max_luns &&
3435 softc->ctl_luns[lm->lun] != NULL)
3436 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3437 else
3438 return (ENXIO);
3439 } else {
3440 if (lm->lun == UINT32_MAX)
3441 retval = ctl_lun_map_deinit(port);
3442 else
3443 retval = ctl_lun_map_init(port);
3444 }
3445 if (port->status & CTL_PORT_STATUS_ONLINE)
3446 ctl_isc_announce_port(port);
3447 break;
3448 }
3449 case CTL_GET_LUN_STATS: {
3450 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3451 int i;
3452
3453 /*
3454 * XXX KDM no locking here. If the LUN list changes,
3455 * things can blow up.
3456 */
3457 i = 0;
3458 stats->status = CTL_SS_OK;
3459 stats->fill_len = 0;
3460 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3461 if (lun->lun < stats->first_item)
3462 continue;
3463 if (stats->fill_len + sizeof(lun->stats) >
3464 stats->alloc_len) {
3465 stats->status = CTL_SS_NEED_MORE_SPACE;
3466 break;
3467 }
3468 retval = copyout(&lun->stats, &stats->stats[i++],
3469 sizeof(lun->stats));
3470 if (retval != 0)
3471 break;
3472 stats->fill_len += sizeof(lun->stats);
3473 }
3474 stats->num_items = softc->num_luns;
3475 stats->flags = CTL_STATS_FLAG_NONE;
3476 #ifdef CTL_TIME_IO
3477 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3478 #endif
3479 getnanouptime(&stats->timestamp);
3480 break;
3481 }
3482 case CTL_GET_PORT_STATS: {
3483 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3484 int i;
3485
3486 /*
3487 * XXX KDM no locking here. If the LUN list changes,
3488 * things can blow up.
3489 */
3490 i = 0;
3491 stats->status = CTL_SS_OK;
3492 stats->fill_len = 0;
3493 STAILQ_FOREACH(port, &softc->port_list, links) {
3494 if (port->targ_port < stats->first_item)
3495 continue;
3496 if (stats->fill_len + sizeof(port->stats) >
3497 stats->alloc_len) {
3498 stats->status = CTL_SS_NEED_MORE_SPACE;
3499 break;
3500 }
3501 retval = copyout(&port->stats, &stats->stats[i++],
3502 sizeof(port->stats));
3503 if (retval != 0)
3504 break;
3505 stats->fill_len += sizeof(port->stats);
3506 }
3507 stats->num_items = softc->num_ports;
3508 stats->flags = CTL_STATS_FLAG_NONE;
3509 #ifdef CTL_TIME_IO
3510 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3511 #endif
3512 getnanouptime(&stats->timestamp);
3513 break;
3514 }
3515 default: {
3516 /* XXX KDM should we fix this? */
3517 #if 0
3518 struct ctl_backend_driver *backend;
3519 unsigned int type;
3520 int found;
3521
3522 found = 0;
3523
3524 /*
3525 * We encode the backend type as the ioctl type for backend
3526 * ioctls. So parse it out here, and then search for a
3527 * backend of this type.
3528 */
3529 type = _IOC_TYPE(cmd);
3530
3531 STAILQ_FOREACH(backend, &softc->be_list, links) {
3532 if (backend->type == type) {
3533 found = 1;
3534 break;
3535 }
3536 }
3537 if (found == 0) {
3538 printf("ctl: unknown ioctl command %#lx or backend "
3539 "%d\n", cmd, type);
3540 retval = EINVAL;
3541 break;
3542 }
3543 retval = backend->ioctl(dev, cmd, addr, flag, td);
3544 #endif
3545 retval = ENOTTY;
3546 break;
3547 }
3548 }
3549 return (retval);
3550 }
3551
3552 uint32_t
ctl_get_initindex(struct ctl_nexus * nexus)3553 ctl_get_initindex(struct ctl_nexus *nexus)
3554 {
3555 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3556 }
3557
3558 int
ctl_lun_map_init(struct ctl_port * port)3559 ctl_lun_map_init(struct ctl_port *port)
3560 {
3561 struct ctl_softc *softc = port->ctl_softc;
3562 struct ctl_lun *lun;
3563 int size = ctl_lun_map_size;
3564 uint32_t i;
3565
3566 if (port->lun_map == NULL || port->lun_map_size < size) {
3567 port->lun_map_size = 0;
3568 free(port->lun_map, M_CTL);
3569 port->lun_map = malloc(size * sizeof(uint32_t),
3570 M_CTL, M_NOWAIT);
3571 }
3572 if (port->lun_map == NULL)
3573 return (ENOMEM);
3574 for (i = 0; i < size; i++)
3575 port->lun_map[i] = UINT32_MAX;
3576 port->lun_map_size = size;
3577 if (port->status & CTL_PORT_STATUS_ONLINE) {
3578 if (port->lun_disable != NULL) {
3579 STAILQ_FOREACH(lun, &softc->lun_list, links)
3580 port->lun_disable(port->targ_lun_arg, lun->lun);
3581 }
3582 ctl_isc_announce_port(port);
3583 }
3584 return (0);
3585 }
3586
3587 int
ctl_lun_map_deinit(struct ctl_port * port)3588 ctl_lun_map_deinit(struct ctl_port *port)
3589 {
3590 struct ctl_softc *softc = port->ctl_softc;
3591 struct ctl_lun *lun;
3592
3593 if (port->lun_map == NULL)
3594 return (0);
3595 port->lun_map_size = 0;
3596 free(port->lun_map, M_CTL);
3597 port->lun_map = NULL;
3598 if (port->status & CTL_PORT_STATUS_ONLINE) {
3599 if (port->lun_enable != NULL) {
3600 STAILQ_FOREACH(lun, &softc->lun_list, links)
3601 port->lun_enable(port->targ_lun_arg, lun->lun);
3602 }
3603 ctl_isc_announce_port(port);
3604 }
3605 return (0);
3606 }
3607
3608 int
ctl_lun_map_set(struct ctl_port * port,uint32_t plun,uint32_t glun)3609 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3610 {
3611 int status;
3612 uint32_t old;
3613
3614 if (port->lun_map == NULL) {
3615 status = ctl_lun_map_init(port);
3616 if (status != 0)
3617 return (status);
3618 }
3619 if (plun >= port->lun_map_size)
3620 return (EINVAL);
3621 old = port->lun_map[plun];
3622 port->lun_map[plun] = glun;
3623 if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) {
3624 if (port->lun_enable != NULL)
3625 port->lun_enable(port->targ_lun_arg, plun);
3626 ctl_isc_announce_port(port);
3627 }
3628 return (0);
3629 }
3630
3631 int
ctl_lun_map_unset(struct ctl_port * port,uint32_t plun)3632 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3633 {
3634 uint32_t old;
3635
3636 if (port->lun_map == NULL || plun >= port->lun_map_size)
3637 return (0);
3638 old = port->lun_map[plun];
3639 port->lun_map[plun] = UINT32_MAX;
3640 if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) {
3641 if (port->lun_disable != NULL)
3642 port->lun_disable(port->targ_lun_arg, plun);
3643 ctl_isc_announce_port(port);
3644 }
3645 return (0);
3646 }
3647
3648 uint32_t
ctl_lun_map_from_port(struct ctl_port * port,uint32_t lun_id)3649 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3650 {
3651
3652 if (port == NULL)
3653 return (UINT32_MAX);
3654 if (port->lun_map == NULL)
3655 return (lun_id);
3656 if (lun_id > port->lun_map_size)
3657 return (UINT32_MAX);
3658 return (port->lun_map[lun_id]);
3659 }
3660
3661 uint32_t
ctl_lun_map_to_port(struct ctl_port * port,uint32_t lun_id)3662 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3663 {
3664 uint32_t i;
3665
3666 if (port == NULL)
3667 return (UINT32_MAX);
3668 if (port->lun_map == NULL)
3669 return (lun_id);
3670 for (i = 0; i < port->lun_map_size; i++) {
3671 if (port->lun_map[i] == lun_id)
3672 return (i);
3673 }
3674 return (UINT32_MAX);
3675 }
3676
3677 uint32_t
ctl_decode_lun(uint64_t encoded)3678 ctl_decode_lun(uint64_t encoded)
3679 {
3680 uint8_t lun[8];
3681 uint32_t result = 0xffffffff;
3682
3683 be64enc(lun, encoded);
3684 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) {
3685 case RPL_LUNDATA_ATYP_PERIPH:
3686 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 &&
3687 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0)
3688 result = lun[1];
3689 break;
3690 case RPL_LUNDATA_ATYP_FLAT:
3691 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 &&
3692 lun[6] == 0 && lun[7] == 0)
3693 result = ((lun[0] & 0x3f) << 8) + lun[1];
3694 break;
3695 case RPL_LUNDATA_ATYP_EXTLUN:
3696 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) {
3697 case 0x02:
3698 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) {
3699 case 0x00:
3700 result = lun[1];
3701 break;
3702 case 0x10:
3703 result = (lun[1] << 16) + (lun[2] << 8) +
3704 lun[3];
3705 break;
3706 case 0x20:
3707 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0)
3708 result = (lun[2] << 24) +
3709 (lun[3] << 16) + (lun[4] << 8) +
3710 lun[5];
3711 break;
3712 }
3713 break;
3714 case RPL_LUNDATA_EXT_EAM_NOT_SPEC:
3715 result = 0xffffffff;
3716 break;
3717 }
3718 break;
3719 }
3720 return (result);
3721 }
3722
3723 uint64_t
ctl_encode_lun(uint32_t decoded)3724 ctl_encode_lun(uint32_t decoded)
3725 {
3726 uint64_t l = decoded;
3727
3728 if (l <= 0xff)
3729 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48));
3730 if (l <= 0x3fff)
3731 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48));
3732 if (l <= 0xffffff)
3733 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) |
3734 (l << 32));
3735 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16));
3736 }
3737
3738 int
ctl_ffz(uint32_t * mask,uint32_t first,uint32_t last)3739 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3740 {
3741 int i;
3742
3743 for (i = first; i < last; i++) {
3744 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3745 return (i);
3746 }
3747 return (-1);
3748 }
3749
3750 int
ctl_set_mask(uint32_t * mask,uint32_t bit)3751 ctl_set_mask(uint32_t *mask, uint32_t bit)
3752 {
3753 uint32_t chunk, piece;
3754
3755 chunk = bit >> 5;
3756 piece = bit % (sizeof(uint32_t) * 8);
3757
3758 if ((mask[chunk] & (1 << piece)) != 0)
3759 return (-1);
3760 else
3761 mask[chunk] |= (1 << piece);
3762
3763 return (0);
3764 }
3765
3766 int
ctl_clear_mask(uint32_t * mask,uint32_t bit)3767 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3768 {
3769 uint32_t chunk, piece;
3770
3771 chunk = bit >> 5;
3772 piece = bit % (sizeof(uint32_t) * 8);
3773
3774 if ((mask[chunk] & (1 << piece)) == 0)
3775 return (-1);
3776 else
3777 mask[chunk] &= ~(1 << piece);
3778
3779 return (0);
3780 }
3781
3782 int
ctl_is_set(uint32_t * mask,uint32_t bit)3783 ctl_is_set(uint32_t *mask, uint32_t bit)
3784 {
3785 uint32_t chunk, piece;
3786
3787 chunk = bit >> 5;
3788 piece = bit % (sizeof(uint32_t) * 8);
3789
3790 if ((mask[chunk] & (1 << piece)) == 0)
3791 return (0);
3792 else
3793 return (1);
3794 }
3795
3796 static uint64_t
ctl_get_prkey(struct ctl_lun * lun,uint32_t residx)3797 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3798 {
3799 uint64_t *t;
3800
3801 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3802 if (t == NULL)
3803 return (0);
3804 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3805 }
3806
3807 static void
ctl_clr_prkey(struct ctl_lun * lun,uint32_t residx)3808 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3809 {
3810 uint64_t *t;
3811
3812 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3813 if (t == NULL)
3814 return;
3815 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3816 }
3817
3818 static void
ctl_alloc_prkey(struct ctl_lun * lun,uint32_t residx)3819 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3820 {
3821 uint64_t *p;
3822 u_int i;
3823
3824 i = residx/CTL_MAX_INIT_PER_PORT;
3825 if (lun->pr_keys[i] != NULL)
3826 return;
3827 mtx_unlock(&lun->lun_lock);
3828 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3829 M_WAITOK | M_ZERO);
3830 mtx_lock(&lun->lun_lock);
3831 if (lun->pr_keys[i] == NULL)
3832 lun->pr_keys[i] = p;
3833 else
3834 free(p, M_CTL);
3835 }
3836
3837 static void
ctl_set_prkey(struct ctl_lun * lun,uint32_t residx,uint64_t key)3838 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3839 {
3840 uint64_t *t;
3841
3842 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3843 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3844 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3845 }
3846
3847 /*
3848 * ctl_softc, pool_name, total_ctl_io are passed in.
3849 * npool is passed out.
3850 */
3851 int
ctl_pool_create(struct ctl_softc * ctl_softc,const char * pool_name,uint32_t total_ctl_io,void ** npool)3852 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3853 uint32_t total_ctl_io, void **npool)
3854 {
3855 struct ctl_io_pool *pool;
3856
3857 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3858 M_NOWAIT | M_ZERO);
3859 if (pool == NULL)
3860 return (ENOMEM);
3861
3862 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3863 pool->ctl_softc = ctl_softc;
3864 #ifdef IO_POOLS
3865 pool->zone = uma_zsecond_create(pool->name, NULL,
3866 NULL, NULL, NULL, ctl_softc->io_zone);
3867 /* uma_prealloc(pool->zone, total_ctl_io); */
3868 #else
3869 pool->zone = ctl_softc->io_zone;
3870 #endif
3871
3872 *npool = pool;
3873 return (0);
3874 }
3875
3876 void
ctl_pool_free(struct ctl_io_pool * pool)3877 ctl_pool_free(struct ctl_io_pool *pool)
3878 {
3879
3880 if (pool == NULL)
3881 return;
3882
3883 #ifdef IO_POOLS
3884 uma_zdestroy(pool->zone);
3885 #endif
3886 free(pool, M_CTL);
3887 }
3888
3889 union ctl_io *
ctl_alloc_io(void * pool_ref)3890 ctl_alloc_io(void *pool_ref)
3891 {
3892 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3893 union ctl_io *io;
3894
3895 io = uma_zalloc(pool->zone, M_WAITOK);
3896 if (io != NULL) {
3897 io->io_hdr.pool = pool_ref;
3898 CTL_SOFTC(io) = pool->ctl_softc;
3899 TAILQ_INIT(&io->io_hdr.blocked_queue);
3900 }
3901 return (io);
3902 }
3903
3904 union ctl_io *
ctl_alloc_io_nowait(void * pool_ref)3905 ctl_alloc_io_nowait(void *pool_ref)
3906 {
3907 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3908 union ctl_io *io;
3909
3910 io = uma_zalloc(pool->zone, M_NOWAIT);
3911 if (io != NULL) {
3912 io->io_hdr.pool = pool_ref;
3913 CTL_SOFTC(io) = pool->ctl_softc;
3914 TAILQ_INIT(&io->io_hdr.blocked_queue);
3915 }
3916 return (io);
3917 }
3918
3919 void
ctl_free_io(union ctl_io * io)3920 ctl_free_io(union ctl_io *io)
3921 {
3922 struct ctl_io_pool *pool;
3923
3924 if (io == NULL)
3925 return;
3926
3927 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3928 uma_zfree(pool->zone, io);
3929 }
3930
3931 void
ctl_zero_io(union ctl_io * io)3932 ctl_zero_io(union ctl_io *io)
3933 {
3934 struct ctl_io_pool *pool;
3935
3936 if (io == NULL)
3937 return;
3938
3939 /*
3940 * May need to preserve linked list pointers at some point too.
3941 */
3942 pool = io->io_hdr.pool;
3943 memset(io, 0, sizeof(*io));
3944 io->io_hdr.pool = pool;
3945 CTL_SOFTC(io) = pool->ctl_softc;
3946 TAILQ_INIT(&io->io_hdr.blocked_queue);
3947 }
3948
3949 int
ctl_expand_number(const char * buf,uint64_t * num)3950 ctl_expand_number(const char *buf, uint64_t *num)
3951 {
3952 char *endptr;
3953 uint64_t number;
3954 unsigned shift;
3955
3956 number = strtoq(buf, &endptr, 0);
3957
3958 switch (tolower((unsigned char)*endptr)) {
3959 case 'e':
3960 shift = 60;
3961 break;
3962 case 'p':
3963 shift = 50;
3964 break;
3965 case 't':
3966 shift = 40;
3967 break;
3968 case 'g':
3969 shift = 30;
3970 break;
3971 case 'm':
3972 shift = 20;
3973 break;
3974 case 'k':
3975 shift = 10;
3976 break;
3977 case 'b':
3978 case '\0': /* No unit. */
3979 *num = number;
3980 return (0);
3981 default:
3982 /* Unrecognized unit. */
3983 return (-1);
3984 }
3985
3986 if ((number << shift) >> shift != number) {
3987 /* Overflow */
3988 return (-1);
3989 }
3990 *num = number << shift;
3991 return (0);
3992 }
3993
3994
3995 /*
3996 * This routine could be used in the future to load default and/or saved
3997 * mode page parameters for a particuar lun.
3998 */
3999 static int
ctl_init_page_index(struct ctl_lun * lun)4000 ctl_init_page_index(struct ctl_lun *lun)
4001 {
4002 int i, page_code;
4003 struct ctl_page_index *page_index;
4004 const char *value;
4005 uint64_t ival;
4006
4007 memcpy(&lun->mode_pages.index, page_index_template,
4008 sizeof(page_index_template));
4009
4010 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4011
4012 page_index = &lun->mode_pages.index[i];
4013 if (lun->be_lun->lun_type == T_DIRECT &&
4014 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4015 continue;
4016 if (lun->be_lun->lun_type == T_PROCESSOR &&
4017 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4018 continue;
4019 if (lun->be_lun->lun_type == T_CDROM &&
4020 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4021 continue;
4022
4023 page_code = page_index->page_code & SMPH_PC_MASK;
4024 switch (page_code) {
4025 case SMS_RW_ERROR_RECOVERY_PAGE: {
4026 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4027 ("subpage %#x for page %#x is incorrect!",
4028 page_index->subpage, page_code));
4029 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
4030 &rw_er_page_default,
4031 sizeof(rw_er_page_default));
4032 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
4033 &rw_er_page_changeable,
4034 sizeof(rw_er_page_changeable));
4035 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
4036 &rw_er_page_default,
4037 sizeof(rw_er_page_default));
4038 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
4039 &rw_er_page_default,
4040 sizeof(rw_er_page_default));
4041 page_index->page_data =
4042 (uint8_t *)lun->mode_pages.rw_er_page;
4043 break;
4044 }
4045 case SMS_FORMAT_DEVICE_PAGE: {
4046 struct scsi_format_page *format_page;
4047
4048 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4049 ("subpage %#x for page %#x is incorrect!",
4050 page_index->subpage, page_code));
4051
4052 /*
4053 * Sectors per track are set above. Bytes per
4054 * sector need to be set here on a per-LUN basis.
4055 */
4056 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
4057 &format_page_default,
4058 sizeof(format_page_default));
4059 memcpy(&lun->mode_pages.format_page[
4060 CTL_PAGE_CHANGEABLE], &format_page_changeable,
4061 sizeof(format_page_changeable));
4062 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
4063 &format_page_default,
4064 sizeof(format_page_default));
4065 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
4066 &format_page_default,
4067 sizeof(format_page_default));
4068
4069 format_page = &lun->mode_pages.format_page[
4070 CTL_PAGE_CURRENT];
4071 scsi_ulto2b(lun->be_lun->blocksize,
4072 format_page->bytes_per_sector);
4073
4074 format_page = &lun->mode_pages.format_page[
4075 CTL_PAGE_DEFAULT];
4076 scsi_ulto2b(lun->be_lun->blocksize,
4077 format_page->bytes_per_sector);
4078
4079 format_page = &lun->mode_pages.format_page[
4080 CTL_PAGE_SAVED];
4081 scsi_ulto2b(lun->be_lun->blocksize,
4082 format_page->bytes_per_sector);
4083
4084 page_index->page_data =
4085 (uint8_t *)lun->mode_pages.format_page;
4086 break;
4087 }
4088 case SMS_RIGID_DISK_PAGE: {
4089 struct scsi_rigid_disk_page *rigid_disk_page;
4090 uint32_t sectors_per_cylinder;
4091 uint64_t cylinders;
4092 #ifndef __XSCALE__
4093 int shift;
4094 #endif /* !__XSCALE__ */
4095
4096 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4097 ("subpage %#x for page %#x is incorrect!",
4098 page_index->subpage, page_code));
4099
4100 /*
4101 * Rotation rate and sectors per track are set
4102 * above. We calculate the cylinders here based on
4103 * capacity. Due to the number of heads and
4104 * sectors per track we're using, smaller arrays
4105 * may turn out to have 0 cylinders. Linux and
4106 * FreeBSD don't pay attention to these mode pages
4107 * to figure out capacity, but Solaris does. It
4108 * seems to deal with 0 cylinders just fine, and
4109 * works out a fake geometry based on the capacity.
4110 */
4111 memcpy(&lun->mode_pages.rigid_disk_page[
4112 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4113 sizeof(rigid_disk_page_default));
4114 memcpy(&lun->mode_pages.rigid_disk_page[
4115 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4116 sizeof(rigid_disk_page_changeable));
4117
4118 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4119 CTL_DEFAULT_HEADS;
4120
4121 /*
4122 * The divide method here will be more accurate,
4123 * probably, but results in floating point being
4124 * used in the kernel on i386 (__udivdi3()). On the
4125 * XScale, though, __udivdi3() is implemented in
4126 * software.
4127 *
4128 * The shift method for cylinder calculation is
4129 * accurate if sectors_per_cylinder is a power of
4130 * 2. Otherwise it might be slightly off -- you
4131 * might have a bit of a truncation problem.
4132 */
4133 #ifdef __XSCALE__
4134 cylinders = (lun->be_lun->maxlba + 1) /
4135 sectors_per_cylinder;
4136 #else
4137 for (shift = 31; shift > 0; shift--) {
4138 if (sectors_per_cylinder & (1 << shift))
4139 break;
4140 }
4141 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4142 #endif
4143
4144 /*
4145 * We've basically got 3 bytes, or 24 bits for the
4146 * cylinder size in the mode page. If we're over,
4147 * just round down to 2^24.
4148 */
4149 if (cylinders > 0xffffff)
4150 cylinders = 0xffffff;
4151
4152 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4153 CTL_PAGE_DEFAULT];
4154 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4155
4156 if ((value = dnvlist_get_string(lun->be_lun->options,
4157 "rpm", NULL)) != NULL) {
4158 scsi_ulto2b(strtol(value, NULL, 0),
4159 rigid_disk_page->rotation_rate);
4160 }
4161
4162 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4163 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4164 sizeof(rigid_disk_page_default));
4165 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4166 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4167 sizeof(rigid_disk_page_default));
4168
4169 page_index->page_data =
4170 (uint8_t *)lun->mode_pages.rigid_disk_page;
4171 break;
4172 }
4173 case SMS_VERIFY_ERROR_RECOVERY_PAGE: {
4174 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4175 ("subpage %#x for page %#x is incorrect!",
4176 page_index->subpage, page_code));
4177 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT],
4178 &verify_er_page_default,
4179 sizeof(verify_er_page_default));
4180 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE],
4181 &verify_er_page_changeable,
4182 sizeof(verify_er_page_changeable));
4183 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT],
4184 &verify_er_page_default,
4185 sizeof(verify_er_page_default));
4186 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED],
4187 &verify_er_page_default,
4188 sizeof(verify_er_page_default));
4189 page_index->page_data =
4190 (uint8_t *)lun->mode_pages.verify_er_page;
4191 break;
4192 }
4193 case SMS_CACHING_PAGE: {
4194 struct scsi_caching_page *caching_page;
4195
4196 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4197 ("subpage %#x for page %#x is incorrect!",
4198 page_index->subpage, page_code));
4199 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4200 &caching_page_default,
4201 sizeof(caching_page_default));
4202 memcpy(&lun->mode_pages.caching_page[
4203 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4204 sizeof(caching_page_changeable));
4205 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4206 &caching_page_default,
4207 sizeof(caching_page_default));
4208 caching_page = &lun->mode_pages.caching_page[
4209 CTL_PAGE_SAVED];
4210 value = dnvlist_get_string(lun->be_lun->options,
4211 "writecache", NULL);
4212 if (value != NULL && strcmp(value, "off") == 0)
4213 caching_page->flags1 &= ~SCP_WCE;
4214 value = dnvlist_get_string(lun->be_lun->options,
4215 "readcache", NULL);
4216 if (value != NULL && strcmp(value, "off") == 0)
4217 caching_page->flags1 |= SCP_RCD;
4218 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4219 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4220 sizeof(caching_page_default));
4221 page_index->page_data =
4222 (uint8_t *)lun->mode_pages.caching_page;
4223 break;
4224 }
4225 case SMS_CONTROL_MODE_PAGE: {
4226 switch (page_index->subpage) {
4227 case SMS_SUBPAGE_PAGE_0: {
4228 struct scsi_control_page *control_page;
4229
4230 memcpy(&lun->mode_pages.control_page[
4231 CTL_PAGE_DEFAULT],
4232 &control_page_default,
4233 sizeof(control_page_default));
4234 memcpy(&lun->mode_pages.control_page[
4235 CTL_PAGE_CHANGEABLE],
4236 &control_page_changeable,
4237 sizeof(control_page_changeable));
4238 memcpy(&lun->mode_pages.control_page[
4239 CTL_PAGE_SAVED],
4240 &control_page_default,
4241 sizeof(control_page_default));
4242 control_page = &lun->mode_pages.control_page[
4243 CTL_PAGE_SAVED];
4244 value = dnvlist_get_string(lun->be_lun->options,
4245 "reordering", NULL);
4246 if (value != NULL &&
4247 strcmp(value, "unrestricted") == 0) {
4248 control_page->queue_flags &=
4249 ~SCP_QUEUE_ALG_MASK;
4250 control_page->queue_flags |=
4251 SCP_QUEUE_ALG_UNRESTRICTED;
4252 }
4253 memcpy(&lun->mode_pages.control_page[
4254 CTL_PAGE_CURRENT],
4255 &lun->mode_pages.control_page[
4256 CTL_PAGE_SAVED],
4257 sizeof(control_page_default));
4258 page_index->page_data =
4259 (uint8_t *)lun->mode_pages.control_page;
4260 break;
4261 }
4262 case 0x01:
4263 memcpy(&lun->mode_pages.control_ext_page[
4264 CTL_PAGE_DEFAULT],
4265 &control_ext_page_default,
4266 sizeof(control_ext_page_default));
4267 memcpy(&lun->mode_pages.control_ext_page[
4268 CTL_PAGE_CHANGEABLE],
4269 &control_ext_page_changeable,
4270 sizeof(control_ext_page_changeable));
4271 memcpy(&lun->mode_pages.control_ext_page[
4272 CTL_PAGE_SAVED],
4273 &control_ext_page_default,
4274 sizeof(control_ext_page_default));
4275 memcpy(&lun->mode_pages.control_ext_page[
4276 CTL_PAGE_CURRENT],
4277 &lun->mode_pages.control_ext_page[
4278 CTL_PAGE_SAVED],
4279 sizeof(control_ext_page_default));
4280 page_index->page_data =
4281 (uint8_t *)lun->mode_pages.control_ext_page;
4282 break;
4283 default:
4284 panic("subpage %#x for page %#x is incorrect!",
4285 page_index->subpage, page_code);
4286 }
4287 break;
4288 }
4289 case SMS_INFO_EXCEPTIONS_PAGE: {
4290 switch (page_index->subpage) {
4291 case SMS_SUBPAGE_PAGE_0:
4292 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4293 &ie_page_default,
4294 sizeof(ie_page_default));
4295 memcpy(&lun->mode_pages.ie_page[
4296 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4297 sizeof(ie_page_changeable));
4298 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4299 &ie_page_default,
4300 sizeof(ie_page_default));
4301 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4302 &ie_page_default,
4303 sizeof(ie_page_default));
4304 page_index->page_data =
4305 (uint8_t *)lun->mode_pages.ie_page;
4306 break;
4307 case 0x02: {
4308 struct ctl_logical_block_provisioning_page *page;
4309
4310 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4311 &lbp_page_default,
4312 sizeof(lbp_page_default));
4313 memcpy(&lun->mode_pages.lbp_page[
4314 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4315 sizeof(lbp_page_changeable));
4316 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4317 &lbp_page_default,
4318 sizeof(lbp_page_default));
4319 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4320 value = dnvlist_get_string(lun->be_lun->options,
4321 "avail-threshold", NULL);
4322 if (value != NULL &&
4323 ctl_expand_number(value, &ival) == 0) {
4324 page->descr[0].flags |= SLBPPD_ENABLED |
4325 SLBPPD_ARMING_DEC;
4326 if (lun->be_lun->blocksize)
4327 ival /= lun->be_lun->blocksize;
4328 else
4329 ival /= 512;
4330 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4331 page->descr[0].count);
4332 }
4333 value = dnvlist_get_string(lun->be_lun->options,
4334 "used-threshold", NULL);
4335 if (value != NULL &&
4336 ctl_expand_number(value, &ival) == 0) {
4337 page->descr[1].flags |= SLBPPD_ENABLED |
4338 SLBPPD_ARMING_INC;
4339 if (lun->be_lun->blocksize)
4340 ival /= lun->be_lun->blocksize;
4341 else
4342 ival /= 512;
4343 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4344 page->descr[1].count);
4345 }
4346 value = dnvlist_get_string(lun->be_lun->options,
4347 "pool-avail-threshold", NULL);
4348 if (value != NULL &&
4349 ctl_expand_number(value, &ival) == 0) {
4350 page->descr[2].flags |= SLBPPD_ENABLED |
4351 SLBPPD_ARMING_DEC;
4352 if (lun->be_lun->blocksize)
4353 ival /= lun->be_lun->blocksize;
4354 else
4355 ival /= 512;
4356 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4357 page->descr[2].count);
4358 }
4359 value = dnvlist_get_string(lun->be_lun->options,
4360 "pool-used-threshold", NULL);
4361 if (value != NULL &&
4362 ctl_expand_number(value, &ival) == 0) {
4363 page->descr[3].flags |= SLBPPD_ENABLED |
4364 SLBPPD_ARMING_INC;
4365 if (lun->be_lun->blocksize)
4366 ival /= lun->be_lun->blocksize;
4367 else
4368 ival /= 512;
4369 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4370 page->descr[3].count);
4371 }
4372 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4373 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4374 sizeof(lbp_page_default));
4375 page_index->page_data =
4376 (uint8_t *)lun->mode_pages.lbp_page;
4377 break;
4378 }
4379 default:
4380 panic("subpage %#x for page %#x is incorrect!",
4381 page_index->subpage, page_code);
4382 }
4383 break;
4384 }
4385 case SMS_CDDVD_CAPS_PAGE:{
4386 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4387 ("subpage %#x for page %#x is incorrect!",
4388 page_index->subpage, page_code));
4389 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT],
4390 &cddvd_page_default,
4391 sizeof(cddvd_page_default));
4392 memcpy(&lun->mode_pages.cddvd_page[
4393 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable,
4394 sizeof(cddvd_page_changeable));
4395 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4396 &cddvd_page_default,
4397 sizeof(cddvd_page_default));
4398 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT],
4399 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4400 sizeof(cddvd_page_default));
4401 page_index->page_data =
4402 (uint8_t *)lun->mode_pages.cddvd_page;
4403 break;
4404 }
4405 default:
4406 panic("invalid page code value %#x", page_code);
4407 }
4408 }
4409
4410 return (CTL_RETVAL_COMPLETE);
4411 }
4412
4413 static int
ctl_init_log_page_index(struct ctl_lun * lun)4414 ctl_init_log_page_index(struct ctl_lun *lun)
4415 {
4416 struct ctl_page_index *page_index;
4417 int i, j, k, prev;
4418
4419 memcpy(&lun->log_pages.index, log_page_index_template,
4420 sizeof(log_page_index_template));
4421
4422 prev = -1;
4423 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4424
4425 page_index = &lun->log_pages.index[i];
4426 if (lun->be_lun->lun_type == T_DIRECT &&
4427 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4428 continue;
4429 if (lun->be_lun->lun_type == T_PROCESSOR &&
4430 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4431 continue;
4432 if (lun->be_lun->lun_type == T_CDROM &&
4433 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4434 continue;
4435
4436 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4437 lun->backend->lun_attr == NULL)
4438 continue;
4439
4440 if (page_index->page_code != prev) {
4441 lun->log_pages.pages_page[j] = page_index->page_code;
4442 prev = page_index->page_code;
4443 j++;
4444 }
4445 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4446 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4447 k++;
4448 }
4449 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4450 lun->log_pages.index[0].page_len = j;
4451 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4452 lun->log_pages.index[1].page_len = k * 2;
4453 lun->log_pages.index[2].page_data = (uint8_t *)&lun->log_pages.temp_page;
4454 lun->log_pages.index[2].page_len = sizeof(lun->log_pages.temp_page);
4455 lun->log_pages.index[3].page_data = &lun->log_pages.lbp_page[0];
4456 lun->log_pages.index[3].page_len = 12*CTL_NUM_LBP_PARAMS;
4457 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.stat_page;
4458 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.stat_page);
4459 lun->log_pages.index[5].page_data = (uint8_t *)&lun->log_pages.ie_page;
4460 lun->log_pages.index[5].page_len = sizeof(lun->log_pages.ie_page);
4461
4462 return (CTL_RETVAL_COMPLETE);
4463 }
4464
4465 static int
hex2bin(const char * str,uint8_t * buf,int buf_size)4466 hex2bin(const char *str, uint8_t *buf, int buf_size)
4467 {
4468 int i;
4469 u_char c;
4470
4471 memset(buf, 0, buf_size);
4472 while (isspace(str[0]))
4473 str++;
4474 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4475 str += 2;
4476 buf_size *= 2;
4477 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4478 while (str[i] == '-') /* Skip dashes in UUIDs. */
4479 str++;
4480 c = str[i];
4481 if (isdigit(c))
4482 c -= '0';
4483 else if (isalpha(c))
4484 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4485 else
4486 break;
4487 if (c >= 16)
4488 break;
4489 if ((i & 1) == 0)
4490 buf[i / 2] |= (c << 4);
4491 else
4492 buf[i / 2] |= c;
4493 }
4494 return ((i + 1) / 2);
4495 }
4496
4497 /*
4498 * LUN allocation.
4499 *
4500 * Requirements:
4501 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4502 * wants us to allocate the LUN and he can block.
4503 * - ctl_softc is always set
4504 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4505 *
4506 * Returns 0 for success, non-zero (errno) for failure.
4507 */
4508 static int
ctl_alloc_lun(struct ctl_softc * ctl_softc,struct ctl_lun * ctl_lun,struct ctl_be_lun * const be_lun)4509 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4510 struct ctl_be_lun *const be_lun)
4511 {
4512 struct ctl_lun *nlun, *lun;
4513 struct scsi_vpd_id_descriptor *desc;
4514 struct scsi_vpd_id_t10 *t10id;
4515 const char *eui, *naa, *scsiname, *uuid, *vendor, *value;
4516 int lun_number, lun_malloced;
4517 int devidlen, idlen1, idlen2 = 0, len;
4518
4519 if (be_lun == NULL)
4520 return (EINVAL);
4521
4522 /*
4523 * We currently only support Direct Access or Processor LUN types.
4524 */
4525 switch (be_lun->lun_type) {
4526 case T_DIRECT:
4527 case T_PROCESSOR:
4528 case T_CDROM:
4529 break;
4530 case T_SEQUENTIAL:
4531 case T_CHANGER:
4532 default:
4533 be_lun->lun_config_status(be_lun->be_lun,
4534 CTL_LUN_CONFIG_FAILURE);
4535 break;
4536 }
4537 if (ctl_lun == NULL) {
4538 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4539 lun_malloced = 1;
4540 } else {
4541 lun_malloced = 0;
4542 lun = ctl_lun;
4543 }
4544
4545 memset(lun, 0, sizeof(*lun));
4546 if (lun_malloced)
4547 lun->flags = CTL_LUN_MALLOCED;
4548
4549 lun->pending_sense = malloc(sizeof(struct scsi_sense_data *) *
4550 ctl_max_ports, M_DEVBUF, M_WAITOK | M_ZERO);
4551 lun->pending_ua = malloc(sizeof(ctl_ua_type *) * ctl_max_ports,
4552 M_DEVBUF, M_WAITOK | M_ZERO);
4553 lun->pr_keys = malloc(sizeof(uint64_t *) * ctl_max_ports,
4554 M_DEVBUF, M_WAITOK | M_ZERO);
4555
4556 /* Generate LUN ID. */
4557 devidlen = max(CTL_DEVID_MIN_LEN,
4558 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4559 idlen1 = sizeof(*t10id) + devidlen;
4560 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4561 scsiname = dnvlist_get_string(be_lun->options, "scsiname", NULL);
4562 if (scsiname != NULL) {
4563 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4564 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4565 }
4566 eui = dnvlist_get_string(be_lun->options, "eui", NULL);
4567 if (eui != NULL) {
4568 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4569 }
4570 naa = dnvlist_get_string(be_lun->options, "naa", NULL);
4571 if (naa != NULL) {
4572 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4573 }
4574 uuid = dnvlist_get_string(be_lun->options, "uuid", NULL);
4575 if (uuid != NULL) {
4576 len += sizeof(struct scsi_vpd_id_descriptor) + 18;
4577 }
4578 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4579 M_CTL, M_WAITOK | M_ZERO);
4580 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4581 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4582 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4583 desc->length = idlen1;
4584 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4585 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4586 if ((vendor = dnvlist_get_string(be_lun->options, "vendor", NULL)) == NULL) {
4587 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4588 } else {
4589 strncpy(t10id->vendor, vendor,
4590 min(sizeof(t10id->vendor), strlen(vendor)));
4591 }
4592 strncpy((char *)t10id->vendor_spec_id,
4593 (char *)be_lun->device_id, devidlen);
4594 if (scsiname != NULL) {
4595 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4596 desc->length);
4597 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4598 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4599 SVPD_ID_TYPE_SCSI_NAME;
4600 desc->length = idlen2;
4601 strlcpy(desc->identifier, scsiname, idlen2);
4602 }
4603 if (eui != NULL) {
4604 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4605 desc->length);
4606 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4607 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4608 SVPD_ID_TYPE_EUI64;
4609 desc->length = hex2bin(eui, desc->identifier, 16);
4610 desc->length = desc->length > 12 ? 16 :
4611 (desc->length > 8 ? 12 : 8);
4612 len -= 16 - desc->length;
4613 }
4614 if (naa != NULL) {
4615 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4616 desc->length);
4617 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4618 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4619 SVPD_ID_TYPE_NAA;
4620 desc->length = hex2bin(naa, desc->identifier, 16);
4621 desc->length = desc->length > 8 ? 16 : 8;
4622 len -= 16 - desc->length;
4623 }
4624 if (uuid != NULL) {
4625 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4626 desc->length);
4627 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4628 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4629 SVPD_ID_TYPE_UUID;
4630 desc->identifier[0] = 0x10;
4631 hex2bin(uuid, &desc->identifier[2], 16);
4632 desc->length = 18;
4633 }
4634 lun->lun_devid->len = len;
4635
4636 mtx_lock(&ctl_softc->ctl_lock);
4637 /*
4638 * See if the caller requested a particular LUN number. If so, see
4639 * if it is available. Otherwise, allocate the first available LUN.
4640 */
4641 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4642 if ((be_lun->req_lun_id > (ctl_max_luns - 1))
4643 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4644 mtx_unlock(&ctl_softc->ctl_lock);
4645 if (be_lun->req_lun_id > (ctl_max_luns - 1)) {
4646 printf("ctl: requested LUN ID %d is higher "
4647 "than ctl_max_luns - 1 (%d)\n",
4648 be_lun->req_lun_id, ctl_max_luns - 1);
4649 } else {
4650 /*
4651 * XXX KDM return an error, or just assign
4652 * another LUN ID in this case??
4653 */
4654 printf("ctl: requested LUN ID %d is already "
4655 "in use\n", be_lun->req_lun_id);
4656 }
4657 fail:
4658 free(lun->lun_devid, M_CTL);
4659 if (lun->flags & CTL_LUN_MALLOCED)
4660 free(lun, M_CTL);
4661 be_lun->lun_config_status(be_lun->be_lun,
4662 CTL_LUN_CONFIG_FAILURE);
4663 return (ENOSPC);
4664 }
4665 lun_number = be_lun->req_lun_id;
4666 } else {
4667 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, ctl_max_luns);
4668 if (lun_number == -1) {
4669 mtx_unlock(&ctl_softc->ctl_lock);
4670 printf("ctl: can't allocate LUN, out of LUNs\n");
4671 goto fail;
4672 }
4673 }
4674 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4675 mtx_unlock(&ctl_softc->ctl_lock);
4676
4677 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4678 lun->lun = lun_number;
4679 lun->be_lun = be_lun;
4680 /*
4681 * The processor LUN is always enabled. Disk LUNs come on line
4682 * disabled, and must be enabled by the backend.
4683 */
4684 lun->flags |= CTL_LUN_DISABLED;
4685 lun->backend = be_lun->be;
4686 be_lun->ctl_lun = lun;
4687 be_lun->lun_id = lun_number;
4688 atomic_add_int(&be_lun->be->num_luns, 1);
4689 if (be_lun->flags & CTL_LUN_FLAG_EJECTED)
4690 lun->flags |= CTL_LUN_EJECTED;
4691 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA)
4692 lun->flags |= CTL_LUN_NO_MEDIA;
4693 if (be_lun->flags & CTL_LUN_FLAG_STOPPED)
4694 lun->flags |= CTL_LUN_STOPPED;
4695
4696 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4697 lun->flags |= CTL_LUN_PRIMARY_SC;
4698
4699 value = dnvlist_get_string(be_lun->options, "removable", NULL);
4700 if (value != NULL) {
4701 if (strcmp(value, "on") == 0)
4702 lun->flags |= CTL_LUN_REMOVABLE;
4703 } else if (be_lun->lun_type == T_CDROM)
4704 lun->flags |= CTL_LUN_REMOVABLE;
4705
4706 lun->ctl_softc = ctl_softc;
4707 #ifdef CTL_TIME_IO
4708 lun->last_busy = getsbinuptime();
4709 #endif
4710 TAILQ_INIT(&lun->ooa_queue);
4711 STAILQ_INIT(&lun->error_list);
4712 lun->ie_reported = 1;
4713 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0);
4714 ctl_tpc_lun_init(lun);
4715 if (lun->flags & CTL_LUN_REMOVABLE) {
4716 lun->prevent = malloc((CTL_MAX_INITIATORS + 31) / 32 * 4,
4717 M_CTL, M_WAITOK);
4718 }
4719
4720 /*
4721 * Initialize the mode and log page index.
4722 */
4723 ctl_init_page_index(lun);
4724 ctl_init_log_page_index(lun);
4725
4726 /* Setup statistics gathering */
4727 lun->stats.item = lun_number;
4728
4729 /*
4730 * Now, before we insert this lun on the lun list, set the lun
4731 * inventory changed UA for all other luns.
4732 */
4733 mtx_lock(&ctl_softc->ctl_lock);
4734 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4735 mtx_lock(&nlun->lun_lock);
4736 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4737 mtx_unlock(&nlun->lun_lock);
4738 }
4739 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4740 ctl_softc->ctl_luns[lun_number] = lun;
4741 ctl_softc->num_luns++;
4742 mtx_unlock(&ctl_softc->ctl_lock);
4743
4744 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4745 return (0);
4746 }
4747
4748 /*
4749 * Delete a LUN.
4750 * Assumptions:
4751 * - LUN has already been marked invalid and any pending I/O has been taken
4752 * care of.
4753 */
4754 static int
ctl_free_lun(struct ctl_lun * lun)4755 ctl_free_lun(struct ctl_lun *lun)
4756 {
4757 struct ctl_softc *softc = lun->ctl_softc;
4758 struct ctl_lun *nlun;
4759 int i;
4760
4761 KASSERT(TAILQ_EMPTY(&lun->ooa_queue),
4762 ("Freeing a LUN %p with outstanding I/O!\n", lun));
4763
4764 mtx_lock(&softc->ctl_lock);
4765 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4766 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4767 softc->ctl_luns[lun->lun] = NULL;
4768 softc->num_luns--;
4769 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4770 mtx_lock(&nlun->lun_lock);
4771 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4772 mtx_unlock(&nlun->lun_lock);
4773 }
4774 mtx_unlock(&softc->ctl_lock);
4775
4776 /*
4777 * Tell the backend to free resources, if this LUN has a backend.
4778 */
4779 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4780 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4781
4782 lun->ie_reportcnt = UINT32_MAX;
4783 callout_drain(&lun->ie_callout);
4784 ctl_tpc_lun_shutdown(lun);
4785 mtx_destroy(&lun->lun_lock);
4786 free(lun->lun_devid, M_CTL);
4787 for (i = 0; i < ctl_max_ports; i++)
4788 free(lun->pending_ua[i], M_CTL);
4789 free(lun->pending_ua, M_DEVBUF);
4790 for (i = 0; i < ctl_max_ports; i++)
4791 free(lun->pr_keys[i], M_CTL);
4792 free(lun->pr_keys, M_DEVBUF);
4793 free(lun->write_buffer, M_CTL);
4794 free(lun->prevent, M_CTL);
4795 if (lun->flags & CTL_LUN_MALLOCED)
4796 free(lun, M_CTL);
4797
4798 return (0);
4799 }
4800
4801 static void
ctl_create_lun(struct ctl_be_lun * be_lun)4802 ctl_create_lun(struct ctl_be_lun *be_lun)
4803 {
4804
4805 /*
4806 * ctl_alloc_lun() should handle all potential failure cases.
4807 */
4808 ctl_alloc_lun(control_softc, NULL, be_lun);
4809 }
4810
4811 int
ctl_add_lun(struct ctl_be_lun * be_lun)4812 ctl_add_lun(struct ctl_be_lun *be_lun)
4813 {
4814 struct ctl_softc *softc = control_softc;
4815
4816 mtx_lock(&softc->ctl_lock);
4817 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4818 mtx_unlock(&softc->ctl_lock);
4819 wakeup(&softc->pending_lun_queue);
4820
4821 return (0);
4822 }
4823
4824 int
ctl_enable_lun(struct ctl_be_lun * be_lun)4825 ctl_enable_lun(struct ctl_be_lun *be_lun)
4826 {
4827 struct ctl_softc *softc;
4828 struct ctl_port *port, *nport;
4829 struct ctl_lun *lun;
4830 int retval;
4831
4832 lun = (struct ctl_lun *)be_lun->ctl_lun;
4833 softc = lun->ctl_softc;
4834
4835 mtx_lock(&softc->ctl_lock);
4836 mtx_lock(&lun->lun_lock);
4837 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4838 /*
4839 * eh? Why did we get called if the LUN is already
4840 * enabled?
4841 */
4842 mtx_unlock(&lun->lun_lock);
4843 mtx_unlock(&softc->ctl_lock);
4844 return (0);
4845 }
4846 lun->flags &= ~CTL_LUN_DISABLED;
4847 mtx_unlock(&lun->lun_lock);
4848
4849 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) {
4850 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4851 port->lun_map != NULL || port->lun_enable == NULL)
4852 continue;
4853
4854 /*
4855 * Drop the lock while we call the FETD's enable routine.
4856 * This can lead to a callback into CTL (at least in the
4857 * case of the internal initiator frontend.
4858 */
4859 mtx_unlock(&softc->ctl_lock);
4860 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4861 mtx_lock(&softc->ctl_lock);
4862 if (retval != 0) {
4863 printf("%s: FETD %s port %d returned error "
4864 "%d for lun_enable on lun %jd\n",
4865 __func__, port->port_name, port->targ_port,
4866 retval, (intmax_t)lun->lun);
4867 }
4868 }
4869
4870 mtx_unlock(&softc->ctl_lock);
4871 ctl_isc_announce_lun(lun);
4872
4873 return (0);
4874 }
4875
4876 int
ctl_disable_lun(struct ctl_be_lun * be_lun)4877 ctl_disable_lun(struct ctl_be_lun *be_lun)
4878 {
4879 struct ctl_softc *softc;
4880 struct ctl_port *port;
4881 struct ctl_lun *lun;
4882 int retval;
4883
4884 lun = (struct ctl_lun *)be_lun->ctl_lun;
4885 softc = lun->ctl_softc;
4886
4887 mtx_lock(&softc->ctl_lock);
4888 mtx_lock(&lun->lun_lock);
4889 if (lun->flags & CTL_LUN_DISABLED) {
4890 mtx_unlock(&lun->lun_lock);
4891 mtx_unlock(&softc->ctl_lock);
4892 return (0);
4893 }
4894 lun->flags |= CTL_LUN_DISABLED;
4895 mtx_unlock(&lun->lun_lock);
4896
4897 STAILQ_FOREACH(port, &softc->port_list, links) {
4898 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4899 port->lun_map != NULL || port->lun_disable == NULL)
4900 continue;
4901
4902 /*
4903 * Drop the lock before we call the frontend's disable
4904 * routine, to avoid lock order reversals.
4905 *
4906 * XXX KDM what happens if the frontend list changes while
4907 * we're traversing it? It's unlikely, but should be handled.
4908 */
4909 mtx_unlock(&softc->ctl_lock);
4910 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4911 mtx_lock(&softc->ctl_lock);
4912 if (retval != 0) {
4913 printf("%s: FETD %s port %d returned error "
4914 "%d for lun_disable on lun %jd\n",
4915 __func__, port->port_name, port->targ_port,
4916 retval, (intmax_t)lun->lun);
4917 }
4918 }
4919
4920 mtx_unlock(&softc->ctl_lock);
4921 ctl_isc_announce_lun(lun);
4922
4923 return (0);
4924 }
4925
4926 int
ctl_start_lun(struct ctl_be_lun * be_lun)4927 ctl_start_lun(struct ctl_be_lun *be_lun)
4928 {
4929 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4930
4931 mtx_lock(&lun->lun_lock);
4932 lun->flags &= ~CTL_LUN_STOPPED;
4933 mtx_unlock(&lun->lun_lock);
4934 return (0);
4935 }
4936
4937 int
ctl_stop_lun(struct ctl_be_lun * be_lun)4938 ctl_stop_lun(struct ctl_be_lun *be_lun)
4939 {
4940 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4941
4942 mtx_lock(&lun->lun_lock);
4943 lun->flags |= CTL_LUN_STOPPED;
4944 mtx_unlock(&lun->lun_lock);
4945 return (0);
4946 }
4947
4948 int
ctl_lun_no_media(struct ctl_be_lun * be_lun)4949 ctl_lun_no_media(struct ctl_be_lun *be_lun)
4950 {
4951 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4952
4953 mtx_lock(&lun->lun_lock);
4954 lun->flags |= CTL_LUN_NO_MEDIA;
4955 mtx_unlock(&lun->lun_lock);
4956 return (0);
4957 }
4958
4959 int
ctl_lun_has_media(struct ctl_be_lun * be_lun)4960 ctl_lun_has_media(struct ctl_be_lun *be_lun)
4961 {
4962 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4963 union ctl_ha_msg msg;
4964
4965 mtx_lock(&lun->lun_lock);
4966 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED);
4967 if (lun->flags & CTL_LUN_REMOVABLE)
4968 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE);
4969 mtx_unlock(&lun->lun_lock);
4970 if ((lun->flags & CTL_LUN_REMOVABLE) &&
4971 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4972 bzero(&msg.ua, sizeof(msg.ua));
4973 msg.hdr.msg_type = CTL_MSG_UA;
4974 msg.hdr.nexus.initid = -1;
4975 msg.hdr.nexus.targ_port = -1;
4976 msg.hdr.nexus.targ_lun = lun->lun;
4977 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4978 msg.ua.ua_all = 1;
4979 msg.ua.ua_set = 1;
4980 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE;
4981 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4982 M_WAITOK);
4983 }
4984 return (0);
4985 }
4986
4987 int
ctl_lun_ejected(struct ctl_be_lun * be_lun)4988 ctl_lun_ejected(struct ctl_be_lun *be_lun)
4989 {
4990 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4991
4992 mtx_lock(&lun->lun_lock);
4993 lun->flags |= CTL_LUN_EJECTED;
4994 mtx_unlock(&lun->lun_lock);
4995 return (0);
4996 }
4997
4998 int
ctl_lun_primary(struct ctl_be_lun * be_lun)4999 ctl_lun_primary(struct ctl_be_lun *be_lun)
5000 {
5001 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5002
5003 mtx_lock(&lun->lun_lock);
5004 lun->flags |= CTL_LUN_PRIMARY_SC;
5005 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
5006 mtx_unlock(&lun->lun_lock);
5007 ctl_isc_announce_lun(lun);
5008 return (0);
5009 }
5010
5011 int
ctl_lun_secondary(struct ctl_be_lun * be_lun)5012 ctl_lun_secondary(struct ctl_be_lun *be_lun)
5013 {
5014 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5015
5016 mtx_lock(&lun->lun_lock);
5017 lun->flags &= ~CTL_LUN_PRIMARY_SC;
5018 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
5019 mtx_unlock(&lun->lun_lock);
5020 ctl_isc_announce_lun(lun);
5021 return (0);
5022 }
5023
5024 int
ctl_invalidate_lun(struct ctl_be_lun * be_lun)5025 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
5026 {
5027 struct ctl_lun *lun;
5028
5029 lun = (struct ctl_lun *)be_lun->ctl_lun;
5030
5031 mtx_lock(&lun->lun_lock);
5032
5033 /*
5034 * The LUN needs to be disabled before it can be marked invalid.
5035 */
5036 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
5037 mtx_unlock(&lun->lun_lock);
5038 return (-1);
5039 }
5040 /*
5041 * Mark the LUN invalid.
5042 */
5043 lun->flags |= CTL_LUN_INVALID;
5044
5045 /*
5046 * If there is nothing in the OOA queue, go ahead and free the LUN.
5047 * If we have something in the OOA queue, we'll free it when the
5048 * last I/O completes.
5049 */
5050 if (TAILQ_EMPTY(&lun->ooa_queue)) {
5051 mtx_unlock(&lun->lun_lock);
5052 ctl_free_lun(lun);
5053 } else
5054 mtx_unlock(&lun->lun_lock);
5055
5056 return (0);
5057 }
5058
5059 void
ctl_lun_capacity_changed(struct ctl_be_lun * be_lun)5060 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
5061 {
5062 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5063 union ctl_ha_msg msg;
5064
5065 mtx_lock(&lun->lun_lock);
5066 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE);
5067 mtx_unlock(&lun->lun_lock);
5068 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
5069 /* Send msg to other side. */
5070 bzero(&msg.ua, sizeof(msg.ua));
5071 msg.hdr.msg_type = CTL_MSG_UA;
5072 msg.hdr.nexus.initid = -1;
5073 msg.hdr.nexus.targ_port = -1;
5074 msg.hdr.nexus.targ_lun = lun->lun;
5075 msg.hdr.nexus.targ_mapped_lun = lun->lun;
5076 msg.ua.ua_all = 1;
5077 msg.ua.ua_set = 1;
5078 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE;
5079 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
5080 M_WAITOK);
5081 }
5082 }
5083
5084 /*
5085 * Backend "memory move is complete" callback for requests that never
5086 * make it down to say RAIDCore's configuration code.
5087 */
5088 int
ctl_config_move_done(union ctl_io * io)5089 ctl_config_move_done(union ctl_io *io)
5090 {
5091 int retval;
5092
5093 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5094 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
5095 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
5096
5097 if ((io->io_hdr.port_status != 0) &&
5098 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5099 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5100 ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1,
5101 /*retry_count*/ io->io_hdr.port_status);
5102 } else if (io->scsiio.kern_data_resid != 0 &&
5103 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT &&
5104 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5105 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5106 ctl_set_invalid_field_ciu(&io->scsiio);
5107 }
5108
5109 if (ctl_debug & CTL_DEBUG_CDB_DATA)
5110 ctl_data_print(io);
5111 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
5112 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5113 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
5114 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5115 /*
5116 * XXX KDM just assuming a single pointer here, and not a
5117 * S/G list. If we start using S/G lists for config data,
5118 * we'll need to know how to clean them up here as well.
5119 */
5120 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5121 free(io->scsiio.kern_data_ptr, M_CTL);
5122 ctl_done(io);
5123 retval = CTL_RETVAL_COMPLETE;
5124 } else {
5125 /*
5126 * XXX KDM now we need to continue data movement. Some
5127 * options:
5128 * - call ctl_scsiio() again? We don't do this for data
5129 * writes, because for those at least we know ahead of
5130 * time where the write will go and how long it is. For
5131 * config writes, though, that information is largely
5132 * contained within the write itself, thus we need to
5133 * parse out the data again.
5134 *
5135 * - Call some other function once the data is in?
5136 */
5137
5138 /*
5139 * XXX KDM call ctl_scsiio() again for now, and check flag
5140 * bits to see whether we're allocated or not.
5141 */
5142 retval = ctl_scsiio(&io->scsiio);
5143 }
5144 return (retval);
5145 }
5146
5147 /*
5148 * This gets called by a backend driver when it is done with a
5149 * data_submit method.
5150 */
5151 void
ctl_data_submit_done(union ctl_io * io)5152 ctl_data_submit_done(union ctl_io *io)
5153 {
5154 /*
5155 * If the IO_CONT flag is set, we need to call the supplied
5156 * function to continue processing the I/O, instead of completing
5157 * the I/O just yet.
5158 *
5159 * If there is an error, though, we don't want to keep processing.
5160 * Instead, just send status back to the initiator.
5161 */
5162 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5163 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5164 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5165 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5166 io->scsiio.io_cont(io);
5167 return;
5168 }
5169 ctl_done(io);
5170 }
5171
5172 /*
5173 * This gets called by a backend driver when it is done with a
5174 * configuration write.
5175 */
5176 void
ctl_config_write_done(union ctl_io * io)5177 ctl_config_write_done(union ctl_io *io)
5178 {
5179 uint8_t *buf;
5180
5181 /*
5182 * If the IO_CONT flag is set, we need to call the supplied
5183 * function to continue processing the I/O, instead of completing
5184 * the I/O just yet.
5185 *
5186 * If there is an error, though, we don't want to keep processing.
5187 * Instead, just send status back to the initiator.
5188 */
5189 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5190 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5191 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5192 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5193 io->scsiio.io_cont(io);
5194 return;
5195 }
5196 /*
5197 * Since a configuration write can be done for commands that actually
5198 * have data allocated, like write buffer, and commands that have
5199 * no data, like start/stop unit, we need to check here.
5200 */
5201 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5202 buf = io->scsiio.kern_data_ptr;
5203 else
5204 buf = NULL;
5205 ctl_done(io);
5206 if (buf)
5207 free(buf, M_CTL);
5208 }
5209
5210 void
ctl_config_read_done(union ctl_io * io)5211 ctl_config_read_done(union ctl_io *io)
5212 {
5213 uint8_t *buf;
5214
5215 /*
5216 * If there is some error -- we are done, skip data transfer.
5217 */
5218 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5219 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5220 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5221 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5222 buf = io->scsiio.kern_data_ptr;
5223 else
5224 buf = NULL;
5225 ctl_done(io);
5226 if (buf)
5227 free(buf, M_CTL);
5228 return;
5229 }
5230
5231 /*
5232 * If the IO_CONT flag is set, we need to call the supplied
5233 * function to continue processing the I/O, instead of completing
5234 * the I/O just yet.
5235 */
5236 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5237 io->scsiio.io_cont(io);
5238 return;
5239 }
5240
5241 ctl_datamove(io);
5242 }
5243
5244 /*
5245 * SCSI release command.
5246 */
5247 int
ctl_scsi_release(struct ctl_scsiio * ctsio)5248 ctl_scsi_release(struct ctl_scsiio *ctsio)
5249 {
5250 struct ctl_lun *lun = CTL_LUN(ctsio);
5251 uint32_t residx;
5252
5253 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5254
5255 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5256
5257 /*
5258 * XXX KDM right now, we only support LUN reservation. We don't
5259 * support 3rd party reservations, or extent reservations, which
5260 * might actually need the parameter list. If we've gotten this
5261 * far, we've got a LUN reservation. Anything else got kicked out
5262 * above. So, according to SPC, ignore the length.
5263 */
5264
5265 mtx_lock(&lun->lun_lock);
5266
5267 /*
5268 * According to SPC, it is not an error for an intiator to attempt
5269 * to release a reservation on a LUN that isn't reserved, or that
5270 * is reserved by another initiator. The reservation can only be
5271 * released, though, by the initiator who made it or by one of
5272 * several reset type events.
5273 */
5274 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5275 lun->flags &= ~CTL_LUN_RESERVED;
5276
5277 mtx_unlock(&lun->lun_lock);
5278
5279 ctl_set_success(ctsio);
5280 ctl_done((union ctl_io *)ctsio);
5281 return (CTL_RETVAL_COMPLETE);
5282 }
5283
5284 int
ctl_scsi_reserve(struct ctl_scsiio * ctsio)5285 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5286 {
5287 struct ctl_lun *lun = CTL_LUN(ctsio);
5288 uint32_t residx;
5289
5290 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5291
5292 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5293
5294 /*
5295 * XXX KDM right now, we only support LUN reservation. We don't
5296 * support 3rd party reservations, or extent reservations, which
5297 * might actually need the parameter list. If we've gotten this
5298 * far, we've got a LUN reservation. Anything else got kicked out
5299 * above. So, according to SPC, ignore the length.
5300 */
5301
5302 mtx_lock(&lun->lun_lock);
5303 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5304 ctl_set_reservation_conflict(ctsio);
5305 goto bailout;
5306 }
5307
5308 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */
5309 if (lun->flags & CTL_LUN_PR_RESERVED) {
5310 ctl_set_success(ctsio);
5311 goto bailout;
5312 }
5313
5314 lun->flags |= CTL_LUN_RESERVED;
5315 lun->res_idx = residx;
5316 ctl_set_success(ctsio);
5317
5318 bailout:
5319 mtx_unlock(&lun->lun_lock);
5320 ctl_done((union ctl_io *)ctsio);
5321 return (CTL_RETVAL_COMPLETE);
5322 }
5323
5324 int
ctl_start_stop(struct ctl_scsiio * ctsio)5325 ctl_start_stop(struct ctl_scsiio *ctsio)
5326 {
5327 struct ctl_lun *lun = CTL_LUN(ctsio);
5328 struct scsi_start_stop_unit *cdb;
5329 int retval;
5330
5331 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5332
5333 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5334
5335 if ((cdb->how & SSS_PC_MASK) == 0) {
5336 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
5337 (cdb->how & SSS_START) == 0) {
5338 uint32_t residx;
5339
5340 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5341 if (ctl_get_prkey(lun, residx) == 0 ||
5342 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) {
5343
5344 ctl_set_reservation_conflict(ctsio);
5345 ctl_done((union ctl_io *)ctsio);
5346 return (CTL_RETVAL_COMPLETE);
5347 }
5348 }
5349
5350 if ((cdb->how & SSS_LOEJ) &&
5351 (lun->flags & CTL_LUN_REMOVABLE) == 0) {
5352 ctl_set_invalid_field(ctsio,
5353 /*sks_valid*/ 1,
5354 /*command*/ 1,
5355 /*field*/ 4,
5356 /*bit_valid*/ 1,
5357 /*bit*/ 1);
5358 ctl_done((union ctl_io *)ctsio);
5359 return (CTL_RETVAL_COMPLETE);
5360 }
5361
5362 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) &&
5363 lun->prevent_count > 0) {
5364 /* "Medium removal prevented" */
5365 ctl_set_sense(ctsio, /*current_error*/ 1,
5366 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ?
5367 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST,
5368 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE);
5369 ctl_done((union ctl_io *)ctsio);
5370 return (CTL_RETVAL_COMPLETE);
5371 }
5372 }
5373
5374 retval = lun->backend->config_write((union ctl_io *)ctsio);
5375 return (retval);
5376 }
5377
5378 int
ctl_prevent_allow(struct ctl_scsiio * ctsio)5379 ctl_prevent_allow(struct ctl_scsiio *ctsio)
5380 {
5381 struct ctl_lun *lun = CTL_LUN(ctsio);
5382 struct scsi_prevent *cdb;
5383 int retval;
5384 uint32_t initidx;
5385
5386 CTL_DEBUG_PRINT(("ctl_prevent_allow\n"));
5387
5388 cdb = (struct scsi_prevent *)ctsio->cdb;
5389
5390 if ((lun->flags & CTL_LUN_REMOVABLE) == 0 || lun->prevent == NULL) {
5391 ctl_set_invalid_opcode(ctsio);
5392 ctl_done((union ctl_io *)ctsio);
5393 return (CTL_RETVAL_COMPLETE);
5394 }
5395
5396 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5397 mtx_lock(&lun->lun_lock);
5398 if ((cdb->how & PR_PREVENT) &&
5399 ctl_is_set(lun->prevent, initidx) == 0) {
5400 ctl_set_mask(lun->prevent, initidx);
5401 lun->prevent_count++;
5402 } else if ((cdb->how & PR_PREVENT) == 0 &&
5403 ctl_is_set(lun->prevent, initidx)) {
5404 ctl_clear_mask(lun->prevent, initidx);
5405 lun->prevent_count--;
5406 }
5407 mtx_unlock(&lun->lun_lock);
5408 retval = lun->backend->config_write((union ctl_io *)ctsio);
5409 return (retval);
5410 }
5411
5412 /*
5413 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5414 * we don't really do anything with the LBA and length fields if the user
5415 * passes them in. Instead we'll just flush out the cache for the entire
5416 * LUN.
5417 */
5418 int
ctl_sync_cache(struct ctl_scsiio * ctsio)5419 ctl_sync_cache(struct ctl_scsiio *ctsio)
5420 {
5421 struct ctl_lun *lun = CTL_LUN(ctsio);
5422 struct ctl_lba_len_flags *lbalen;
5423 uint64_t starting_lba;
5424 uint32_t block_count;
5425 int retval;
5426 uint8_t byte2;
5427
5428 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5429
5430 retval = 0;
5431
5432 switch (ctsio->cdb[0]) {
5433 case SYNCHRONIZE_CACHE: {
5434 struct scsi_sync_cache *cdb;
5435 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5436
5437 starting_lba = scsi_4btoul(cdb->begin_lba);
5438 block_count = scsi_2btoul(cdb->lb_count);
5439 byte2 = cdb->byte2;
5440 break;
5441 }
5442 case SYNCHRONIZE_CACHE_16: {
5443 struct scsi_sync_cache_16 *cdb;
5444 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5445
5446 starting_lba = scsi_8btou64(cdb->begin_lba);
5447 block_count = scsi_4btoul(cdb->lb_count);
5448 byte2 = cdb->byte2;
5449 break;
5450 }
5451 default:
5452 ctl_set_invalid_opcode(ctsio);
5453 ctl_done((union ctl_io *)ctsio);
5454 goto bailout;
5455 break; /* NOTREACHED */
5456 }
5457
5458 /*
5459 * We check the LBA and length, but don't do anything with them.
5460 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5461 * get flushed. This check will just help satisfy anyone who wants
5462 * to see an error for an out of range LBA.
5463 */
5464 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5465 ctl_set_lba_out_of_range(ctsio,
5466 MAX(starting_lba, lun->be_lun->maxlba + 1));
5467 ctl_done((union ctl_io *)ctsio);
5468 goto bailout;
5469 }
5470
5471 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5472 lbalen->lba = starting_lba;
5473 lbalen->len = block_count;
5474 lbalen->flags = byte2;
5475 retval = lun->backend->config_write((union ctl_io *)ctsio);
5476
5477 bailout:
5478 return (retval);
5479 }
5480
5481 int
ctl_format(struct ctl_scsiio * ctsio)5482 ctl_format(struct ctl_scsiio *ctsio)
5483 {
5484 struct scsi_format *cdb;
5485 int length, defect_list_len;
5486
5487 CTL_DEBUG_PRINT(("ctl_format\n"));
5488
5489 cdb = (struct scsi_format *)ctsio->cdb;
5490
5491 length = 0;
5492 if (cdb->byte2 & SF_FMTDATA) {
5493 if (cdb->byte2 & SF_LONGLIST)
5494 length = sizeof(struct scsi_format_header_long);
5495 else
5496 length = sizeof(struct scsi_format_header_short);
5497 }
5498
5499 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5500 && (length > 0)) {
5501 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5502 ctsio->kern_data_len = length;
5503 ctsio->kern_total_len = length;
5504 ctsio->kern_rel_offset = 0;
5505 ctsio->kern_sg_entries = 0;
5506 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5507 ctsio->be_move_done = ctl_config_move_done;
5508 ctl_datamove((union ctl_io *)ctsio);
5509
5510 return (CTL_RETVAL_COMPLETE);
5511 }
5512
5513 defect_list_len = 0;
5514
5515 if (cdb->byte2 & SF_FMTDATA) {
5516 if (cdb->byte2 & SF_LONGLIST) {
5517 struct scsi_format_header_long *header;
5518
5519 header = (struct scsi_format_header_long *)
5520 ctsio->kern_data_ptr;
5521
5522 defect_list_len = scsi_4btoul(header->defect_list_len);
5523 if (defect_list_len != 0) {
5524 ctl_set_invalid_field(ctsio,
5525 /*sks_valid*/ 1,
5526 /*command*/ 0,
5527 /*field*/ 2,
5528 /*bit_valid*/ 0,
5529 /*bit*/ 0);
5530 goto bailout;
5531 }
5532 } else {
5533 struct scsi_format_header_short *header;
5534
5535 header = (struct scsi_format_header_short *)
5536 ctsio->kern_data_ptr;
5537
5538 defect_list_len = scsi_2btoul(header->defect_list_len);
5539 if (defect_list_len != 0) {
5540 ctl_set_invalid_field(ctsio,
5541 /*sks_valid*/ 1,
5542 /*command*/ 0,
5543 /*field*/ 2,
5544 /*bit_valid*/ 0,
5545 /*bit*/ 0);
5546 goto bailout;
5547 }
5548 }
5549 }
5550
5551 ctl_set_success(ctsio);
5552 bailout:
5553
5554 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5555 free(ctsio->kern_data_ptr, M_CTL);
5556 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5557 }
5558
5559 ctl_done((union ctl_io *)ctsio);
5560 return (CTL_RETVAL_COMPLETE);
5561 }
5562
5563 int
ctl_read_buffer(struct ctl_scsiio * ctsio)5564 ctl_read_buffer(struct ctl_scsiio *ctsio)
5565 {
5566 struct ctl_lun *lun = CTL_LUN(ctsio);
5567 uint64_t buffer_offset;
5568 uint32_t len;
5569 uint8_t byte2;
5570 static uint8_t descr[4];
5571 static uint8_t echo_descr[4] = { 0 };
5572
5573 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5574
5575 switch (ctsio->cdb[0]) {
5576 case READ_BUFFER: {
5577 struct scsi_read_buffer *cdb;
5578
5579 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5580 buffer_offset = scsi_3btoul(cdb->offset);
5581 len = scsi_3btoul(cdb->length);
5582 byte2 = cdb->byte2;
5583 break;
5584 }
5585 case READ_BUFFER_16: {
5586 struct scsi_read_buffer_16 *cdb;
5587
5588 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb;
5589 buffer_offset = scsi_8btou64(cdb->offset);
5590 len = scsi_4btoul(cdb->length);
5591 byte2 = cdb->byte2;
5592 break;
5593 }
5594 default: /* This shouldn't happen. */
5595 ctl_set_invalid_opcode(ctsio);
5596 ctl_done((union ctl_io *)ctsio);
5597 return (CTL_RETVAL_COMPLETE);
5598 }
5599
5600 if (buffer_offset > CTL_WRITE_BUFFER_SIZE ||
5601 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5602 ctl_set_invalid_field(ctsio,
5603 /*sks_valid*/ 1,
5604 /*command*/ 1,
5605 /*field*/ 6,
5606 /*bit_valid*/ 0,
5607 /*bit*/ 0);
5608 ctl_done((union ctl_io *)ctsio);
5609 return (CTL_RETVAL_COMPLETE);
5610 }
5611
5612 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5613 descr[0] = 0;
5614 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5615 ctsio->kern_data_ptr = descr;
5616 len = min(len, sizeof(descr));
5617 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5618 ctsio->kern_data_ptr = echo_descr;
5619 len = min(len, sizeof(echo_descr));
5620 } else {
5621 if (lun->write_buffer == NULL) {
5622 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5623 M_CTL, M_WAITOK);
5624 }
5625 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5626 }
5627 ctsio->kern_data_len = len;
5628 ctsio->kern_total_len = len;
5629 ctsio->kern_rel_offset = 0;
5630 ctsio->kern_sg_entries = 0;
5631 ctl_set_success(ctsio);
5632 ctsio->be_move_done = ctl_config_move_done;
5633 ctl_datamove((union ctl_io *)ctsio);
5634 return (CTL_RETVAL_COMPLETE);
5635 }
5636
5637 int
ctl_write_buffer(struct ctl_scsiio * ctsio)5638 ctl_write_buffer(struct ctl_scsiio *ctsio)
5639 {
5640 struct ctl_lun *lun = CTL_LUN(ctsio);
5641 struct scsi_write_buffer *cdb;
5642 int buffer_offset, len;
5643
5644 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5645
5646 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5647
5648 len = scsi_3btoul(cdb->length);
5649 buffer_offset = scsi_3btoul(cdb->offset);
5650
5651 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5652 ctl_set_invalid_field(ctsio,
5653 /*sks_valid*/ 1,
5654 /*command*/ 1,
5655 /*field*/ 6,
5656 /*bit_valid*/ 0,
5657 /*bit*/ 0);
5658 ctl_done((union ctl_io *)ctsio);
5659 return (CTL_RETVAL_COMPLETE);
5660 }
5661
5662 /*
5663 * If we've got a kernel request that hasn't been malloced yet,
5664 * malloc it and tell the caller the data buffer is here.
5665 */
5666 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5667 if (lun->write_buffer == NULL) {
5668 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5669 M_CTL, M_WAITOK);
5670 }
5671 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5672 ctsio->kern_data_len = len;
5673 ctsio->kern_total_len = len;
5674 ctsio->kern_rel_offset = 0;
5675 ctsio->kern_sg_entries = 0;
5676 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5677 ctsio->be_move_done = ctl_config_move_done;
5678 ctl_datamove((union ctl_io *)ctsio);
5679
5680 return (CTL_RETVAL_COMPLETE);
5681 }
5682
5683 ctl_set_success(ctsio);
5684 ctl_done((union ctl_io *)ctsio);
5685 return (CTL_RETVAL_COMPLETE);
5686 }
5687
5688 static int
ctl_write_same_cont(union ctl_io * io)5689 ctl_write_same_cont(union ctl_io *io)
5690 {
5691 struct ctl_lun *lun = CTL_LUN(io);
5692 struct ctl_scsiio *ctsio;
5693 struct ctl_lba_len_flags *lbalen;
5694 int retval;
5695
5696 ctsio = &io->scsiio;
5697 ctsio->io_hdr.status = CTL_STATUS_NONE;
5698 lbalen = (struct ctl_lba_len_flags *)
5699 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5700 lbalen->lba += lbalen->len;
5701 if ((lun->be_lun->maxlba + 1) - lbalen->lba <= UINT32_MAX) {
5702 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
5703 lbalen->len = (lun->be_lun->maxlba + 1) - lbalen->lba;
5704 }
5705
5706 CTL_DEBUG_PRINT(("ctl_write_same_cont: calling config_write()\n"));
5707 retval = lun->backend->config_write((union ctl_io *)ctsio);
5708 return (retval);
5709 }
5710
5711 int
ctl_write_same(struct ctl_scsiio * ctsio)5712 ctl_write_same(struct ctl_scsiio *ctsio)
5713 {
5714 struct ctl_lun *lun = CTL_LUN(ctsio);
5715 struct ctl_lba_len_flags *lbalen;
5716 const char *val;
5717 uint64_t lba, ival;
5718 uint32_t num_blocks;
5719 int len, retval;
5720 uint8_t byte2;
5721
5722 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5723
5724 switch (ctsio->cdb[0]) {
5725 case WRITE_SAME_10: {
5726 struct scsi_write_same_10 *cdb;
5727
5728 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5729
5730 lba = scsi_4btoul(cdb->addr);
5731 num_blocks = scsi_2btoul(cdb->length);
5732 byte2 = cdb->byte2;
5733 break;
5734 }
5735 case WRITE_SAME_16: {
5736 struct scsi_write_same_16 *cdb;
5737
5738 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5739
5740 lba = scsi_8btou64(cdb->addr);
5741 num_blocks = scsi_4btoul(cdb->length);
5742 byte2 = cdb->byte2;
5743 break;
5744 }
5745 default:
5746 /*
5747 * We got a command we don't support. This shouldn't
5748 * happen, commands should be filtered out above us.
5749 */
5750 ctl_set_invalid_opcode(ctsio);
5751 ctl_done((union ctl_io *)ctsio);
5752
5753 return (CTL_RETVAL_COMPLETE);
5754 break; /* NOTREACHED */
5755 }
5756
5757 /* ANCHOR flag can be used only together with UNMAP */
5758 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) {
5759 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5760 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5761 ctl_done((union ctl_io *)ctsio);
5762 return (CTL_RETVAL_COMPLETE);
5763 }
5764
5765 /*
5766 * The first check is to make sure we're in bounds, the second
5767 * check is to catch wrap-around problems. If the lba + num blocks
5768 * is less than the lba, then we've wrapped around and the block
5769 * range is invalid anyway.
5770 */
5771 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5772 || ((lba + num_blocks) < lba)) {
5773 ctl_set_lba_out_of_range(ctsio,
5774 MAX(lba, lun->be_lun->maxlba + 1));
5775 ctl_done((union ctl_io *)ctsio);
5776 return (CTL_RETVAL_COMPLETE);
5777 }
5778
5779 /* Zero number of blocks means "to the last logical block" */
5780 if (num_blocks == 0) {
5781 ival = UINT64_MAX;
5782 val = dnvlist_get_string(lun->be_lun->options,
5783 "write_same_max_lba", NULL);
5784 if (val != NULL)
5785 ctl_expand_number(val, &ival);
5786 if ((lun->be_lun->maxlba + 1) - lba > ival) {
5787 ctl_set_invalid_field(ctsio,
5788 /*sks_valid*/ 1, /*command*/ 1,
5789 /*field*/ ctsio->cdb[0] == WRITE_SAME_10 ? 7 : 10,
5790 /*bit_valid*/ 0, /*bit*/ 0);
5791 ctl_done((union ctl_io *)ctsio);
5792 return (CTL_RETVAL_COMPLETE);
5793 }
5794 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5795 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
5796 ctsio->io_cont = ctl_write_same_cont;
5797 num_blocks = 1 << 31;
5798 } else
5799 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5800 }
5801
5802 len = lun->be_lun->blocksize;
5803
5804 /*
5805 * If we've got a kernel request that hasn't been malloced yet,
5806 * malloc it and tell the caller the data buffer is here.
5807 */
5808 if ((byte2 & SWS_NDOB) == 0 &&
5809 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5810 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5811 ctsio->kern_data_len = len;
5812 ctsio->kern_total_len = len;
5813 ctsio->kern_rel_offset = 0;
5814 ctsio->kern_sg_entries = 0;
5815 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5816 ctsio->be_move_done = ctl_config_move_done;
5817 ctl_datamove((union ctl_io *)ctsio);
5818
5819 return (CTL_RETVAL_COMPLETE);
5820 }
5821
5822 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5823 lbalen->lba = lba;
5824 lbalen->len = num_blocks;
5825 lbalen->flags = byte2;
5826 retval = lun->backend->config_write((union ctl_io *)ctsio);
5827
5828 return (retval);
5829 }
5830
5831 int
ctl_unmap(struct ctl_scsiio * ctsio)5832 ctl_unmap(struct ctl_scsiio *ctsio)
5833 {
5834 struct ctl_lun *lun = CTL_LUN(ctsio);
5835 struct scsi_unmap *cdb;
5836 struct ctl_ptr_len_flags *ptrlen;
5837 struct scsi_unmap_header *hdr;
5838 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5839 uint64_t lba;
5840 uint32_t num_blocks;
5841 int len, retval;
5842 uint8_t byte2;
5843
5844 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5845
5846 cdb = (struct scsi_unmap *)ctsio->cdb;
5847 len = scsi_2btoul(cdb->length);
5848 byte2 = cdb->byte2;
5849
5850 /*
5851 * If we've got a kernel request that hasn't been malloced yet,
5852 * malloc it and tell the caller the data buffer is here.
5853 */
5854 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5855 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5856 ctsio->kern_data_len = len;
5857 ctsio->kern_total_len = len;
5858 ctsio->kern_rel_offset = 0;
5859 ctsio->kern_sg_entries = 0;
5860 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5861 ctsio->be_move_done = ctl_config_move_done;
5862 ctl_datamove((union ctl_io *)ctsio);
5863
5864 return (CTL_RETVAL_COMPLETE);
5865 }
5866
5867 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5868 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5869 if (len < sizeof (*hdr) ||
5870 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5871 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5872 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5873 ctl_set_invalid_field(ctsio,
5874 /*sks_valid*/ 0,
5875 /*command*/ 0,
5876 /*field*/ 0,
5877 /*bit_valid*/ 0,
5878 /*bit*/ 0);
5879 goto done;
5880 }
5881 len = scsi_2btoul(hdr->desc_length);
5882 buf = (struct scsi_unmap_desc *)(hdr + 1);
5883 end = buf + len / sizeof(*buf);
5884
5885 endnz = buf;
5886 for (range = buf; range < end; range++) {
5887 lba = scsi_8btou64(range->lba);
5888 num_blocks = scsi_4btoul(range->length);
5889 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5890 || ((lba + num_blocks) < lba)) {
5891 ctl_set_lba_out_of_range(ctsio,
5892 MAX(lba, lun->be_lun->maxlba + 1));
5893 ctl_done((union ctl_io *)ctsio);
5894 return (CTL_RETVAL_COMPLETE);
5895 }
5896 if (num_blocks != 0)
5897 endnz = range + 1;
5898 }
5899
5900 /*
5901 * Block backend can not handle zero last range.
5902 * Filter it out and return if there is nothing left.
5903 */
5904 len = (uint8_t *)endnz - (uint8_t *)buf;
5905 if (len == 0) {
5906 ctl_set_success(ctsio);
5907 goto done;
5908 }
5909
5910 mtx_lock(&lun->lun_lock);
5911 ptrlen = (struct ctl_ptr_len_flags *)
5912 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5913 ptrlen->ptr = (void *)buf;
5914 ptrlen->len = len;
5915 ptrlen->flags = byte2;
5916 ctl_try_unblock_others(lun, (union ctl_io *)ctsio, FALSE);
5917 mtx_unlock(&lun->lun_lock);
5918
5919 retval = lun->backend->config_write((union ctl_io *)ctsio);
5920 return (retval);
5921
5922 done:
5923 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5924 free(ctsio->kern_data_ptr, M_CTL);
5925 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5926 }
5927 ctl_done((union ctl_io *)ctsio);
5928 return (CTL_RETVAL_COMPLETE);
5929 }
5930
5931 int
ctl_default_page_handler(struct ctl_scsiio * ctsio,struct ctl_page_index * page_index,uint8_t * page_ptr)5932 ctl_default_page_handler(struct ctl_scsiio *ctsio,
5933 struct ctl_page_index *page_index, uint8_t *page_ptr)
5934 {
5935 struct ctl_lun *lun = CTL_LUN(ctsio);
5936 uint8_t *current_cp;
5937 int set_ua;
5938 uint32_t initidx;
5939
5940 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5941 set_ua = 0;
5942
5943 current_cp = (page_index->page_data + (page_index->page_len *
5944 CTL_PAGE_CURRENT));
5945
5946 mtx_lock(&lun->lun_lock);
5947 if (memcmp(current_cp, page_ptr, page_index->page_len)) {
5948 memcpy(current_cp, page_ptr, page_index->page_len);
5949 set_ua = 1;
5950 }
5951 if (set_ua != 0)
5952 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5953 mtx_unlock(&lun->lun_lock);
5954 if (set_ua) {
5955 ctl_isc_announce_mode(lun,
5956 ctl_get_initindex(&ctsio->io_hdr.nexus),
5957 page_index->page_code, page_index->subpage);
5958 }
5959 return (CTL_RETVAL_COMPLETE);
5960 }
5961
5962 static void
ctl_ie_timer(void * arg)5963 ctl_ie_timer(void *arg)
5964 {
5965 struct ctl_lun *lun = arg;
5966 uint64_t t;
5967
5968 if (lun->ie_asc == 0)
5969 return;
5970
5971 if (lun->MODE_IE.mrie == SIEP_MRIE_UA)
5972 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5973 else
5974 lun->ie_reported = 0;
5975
5976 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) {
5977 lun->ie_reportcnt++;
5978 t = scsi_4btoul(lun->MODE_IE.interval_timer);
5979 if (t == 0 || t == UINT32_MAX)
5980 t = 3000; /* 5 min */
5981 callout_schedule(&lun->ie_callout, t * hz / 10);
5982 }
5983 }
5984
5985 int
ctl_ie_page_handler(struct ctl_scsiio * ctsio,struct ctl_page_index * page_index,uint8_t * page_ptr)5986 ctl_ie_page_handler(struct ctl_scsiio *ctsio,
5987 struct ctl_page_index *page_index, uint8_t *page_ptr)
5988 {
5989 struct ctl_lun *lun = CTL_LUN(ctsio);
5990 struct scsi_info_exceptions_page *pg;
5991 uint64_t t;
5992
5993 (void)ctl_default_page_handler(ctsio, page_index, page_ptr);
5994
5995 pg = (struct scsi_info_exceptions_page *)page_ptr;
5996 mtx_lock(&lun->lun_lock);
5997 if (pg->info_flags & SIEP_FLAGS_TEST) {
5998 lun->ie_asc = 0x5d;
5999 lun->ie_ascq = 0xff;
6000 if (pg->mrie == SIEP_MRIE_UA) {
6001 ctl_est_ua_all(lun, -1, CTL_UA_IE);
6002 lun->ie_reported = 1;
6003 } else {
6004 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
6005 lun->ie_reported = -1;
6006 }
6007 lun->ie_reportcnt = 1;
6008 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) {
6009 lun->ie_reportcnt++;
6010 t = scsi_4btoul(pg->interval_timer);
6011 if (t == 0 || t == UINT32_MAX)
6012 t = 3000; /* 5 min */
6013 callout_reset(&lun->ie_callout, t * hz / 10,
6014 ctl_ie_timer, lun);
6015 }
6016 } else {
6017 lun->ie_asc = 0;
6018 lun->ie_ascq = 0;
6019 lun->ie_reported = 1;
6020 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
6021 lun->ie_reportcnt = UINT32_MAX;
6022 callout_stop(&lun->ie_callout);
6023 }
6024 mtx_unlock(&lun->lun_lock);
6025 return (CTL_RETVAL_COMPLETE);
6026 }
6027
6028 static int
ctl_do_mode_select(union ctl_io * io)6029 ctl_do_mode_select(union ctl_io *io)
6030 {
6031 struct ctl_lun *lun = CTL_LUN(io);
6032 struct scsi_mode_page_header *page_header;
6033 struct ctl_page_index *page_index;
6034 struct ctl_scsiio *ctsio;
6035 int page_len, page_len_offset, page_len_size;
6036 union ctl_modepage_info *modepage_info;
6037 uint16_t *len_left, *len_used;
6038 int retval, i;
6039
6040 ctsio = &io->scsiio;
6041 page_index = NULL;
6042 page_len = 0;
6043
6044 modepage_info = (union ctl_modepage_info *)
6045 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6046 len_left = &modepage_info->header.len_left;
6047 len_used = &modepage_info->header.len_used;
6048
6049 do_next_page:
6050
6051 page_header = (struct scsi_mode_page_header *)
6052 (ctsio->kern_data_ptr + *len_used);
6053
6054 if (*len_left == 0) {
6055 free(ctsio->kern_data_ptr, M_CTL);
6056 ctl_set_success(ctsio);
6057 ctl_done((union ctl_io *)ctsio);
6058 return (CTL_RETVAL_COMPLETE);
6059 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6060
6061 free(ctsio->kern_data_ptr, M_CTL);
6062 ctl_set_param_len_error(ctsio);
6063 ctl_done((union ctl_io *)ctsio);
6064 return (CTL_RETVAL_COMPLETE);
6065
6066 } else if ((page_header->page_code & SMPH_SPF)
6067 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6068
6069 free(ctsio->kern_data_ptr, M_CTL);
6070 ctl_set_param_len_error(ctsio);
6071 ctl_done((union ctl_io *)ctsio);
6072 return (CTL_RETVAL_COMPLETE);
6073 }
6074
6075
6076 /*
6077 * XXX KDM should we do something with the block descriptor?
6078 */
6079 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6080 page_index = &lun->mode_pages.index[i];
6081 if (lun->be_lun->lun_type == T_DIRECT &&
6082 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6083 continue;
6084 if (lun->be_lun->lun_type == T_PROCESSOR &&
6085 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6086 continue;
6087 if (lun->be_lun->lun_type == T_CDROM &&
6088 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6089 continue;
6090
6091 if ((page_index->page_code & SMPH_PC_MASK) !=
6092 (page_header->page_code & SMPH_PC_MASK))
6093 continue;
6094
6095 /*
6096 * If neither page has a subpage code, then we've got a
6097 * match.
6098 */
6099 if (((page_index->page_code & SMPH_SPF) == 0)
6100 && ((page_header->page_code & SMPH_SPF) == 0)) {
6101 page_len = page_header->page_length;
6102 break;
6103 }
6104
6105 /*
6106 * If both pages have subpages, then the subpage numbers
6107 * have to match.
6108 */
6109 if ((page_index->page_code & SMPH_SPF)
6110 && (page_header->page_code & SMPH_SPF)) {
6111 struct scsi_mode_page_header_sp *sph;
6112
6113 sph = (struct scsi_mode_page_header_sp *)page_header;
6114 if (page_index->subpage == sph->subpage) {
6115 page_len = scsi_2btoul(sph->page_length);
6116 break;
6117 }
6118 }
6119 }
6120
6121 /*
6122 * If we couldn't find the page, or if we don't have a mode select
6123 * handler for it, send back an error to the user.
6124 */
6125 if ((i >= CTL_NUM_MODE_PAGES)
6126 || (page_index->select_handler == NULL)) {
6127 ctl_set_invalid_field(ctsio,
6128 /*sks_valid*/ 1,
6129 /*command*/ 0,
6130 /*field*/ *len_used,
6131 /*bit_valid*/ 0,
6132 /*bit*/ 0);
6133 free(ctsio->kern_data_ptr, M_CTL);
6134 ctl_done((union ctl_io *)ctsio);
6135 return (CTL_RETVAL_COMPLETE);
6136 }
6137
6138 if (page_index->page_code & SMPH_SPF) {
6139 page_len_offset = 2;
6140 page_len_size = 2;
6141 } else {
6142 page_len_size = 1;
6143 page_len_offset = 1;
6144 }
6145
6146 /*
6147 * If the length the initiator gives us isn't the one we specify in
6148 * the mode page header, or if they didn't specify enough data in
6149 * the CDB to avoid truncating this page, kick out the request.
6150 */
6151 if (page_len != page_index->page_len - page_len_offset - page_len_size) {
6152 ctl_set_invalid_field(ctsio,
6153 /*sks_valid*/ 1,
6154 /*command*/ 0,
6155 /*field*/ *len_used + page_len_offset,
6156 /*bit_valid*/ 0,
6157 /*bit*/ 0);
6158 free(ctsio->kern_data_ptr, M_CTL);
6159 ctl_done((union ctl_io *)ctsio);
6160 return (CTL_RETVAL_COMPLETE);
6161 }
6162 if (*len_left < page_index->page_len) {
6163 free(ctsio->kern_data_ptr, M_CTL);
6164 ctl_set_param_len_error(ctsio);
6165 ctl_done((union ctl_io *)ctsio);
6166 return (CTL_RETVAL_COMPLETE);
6167 }
6168
6169 /*
6170 * Run through the mode page, checking to make sure that the bits
6171 * the user changed are actually legal for him to change.
6172 */
6173 for (i = 0; i < page_index->page_len; i++) {
6174 uint8_t *user_byte, *change_mask, *current_byte;
6175 int bad_bit;
6176 int j;
6177
6178 user_byte = (uint8_t *)page_header + i;
6179 change_mask = page_index->page_data +
6180 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6181 current_byte = page_index->page_data +
6182 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6183
6184 /*
6185 * Check to see whether the user set any bits in this byte
6186 * that he is not allowed to set.
6187 */
6188 if ((*user_byte & ~(*change_mask)) ==
6189 (*current_byte & ~(*change_mask)))
6190 continue;
6191
6192 /*
6193 * Go through bit by bit to determine which one is illegal.
6194 */
6195 bad_bit = 0;
6196 for (j = 7; j >= 0; j--) {
6197 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6198 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6199 bad_bit = i;
6200 break;
6201 }
6202 }
6203 ctl_set_invalid_field(ctsio,
6204 /*sks_valid*/ 1,
6205 /*command*/ 0,
6206 /*field*/ *len_used + i,
6207 /*bit_valid*/ 1,
6208 /*bit*/ bad_bit);
6209 free(ctsio->kern_data_ptr, M_CTL);
6210 ctl_done((union ctl_io *)ctsio);
6211 return (CTL_RETVAL_COMPLETE);
6212 }
6213
6214 /*
6215 * Decrement these before we call the page handler, since we may
6216 * end up getting called back one way or another before the handler
6217 * returns to this context.
6218 */
6219 *len_left -= page_index->page_len;
6220 *len_used += page_index->page_len;
6221
6222 retval = page_index->select_handler(ctsio, page_index,
6223 (uint8_t *)page_header);
6224
6225 /*
6226 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6227 * wait until this queued command completes to finish processing
6228 * the mode page. If it returns anything other than
6229 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6230 * already set the sense information, freed the data pointer, and
6231 * completed the io for us.
6232 */
6233 if (retval != CTL_RETVAL_COMPLETE)
6234 goto bailout_no_done;
6235
6236 /*
6237 * If the initiator sent us more than one page, parse the next one.
6238 */
6239 if (*len_left > 0)
6240 goto do_next_page;
6241
6242 ctl_set_success(ctsio);
6243 free(ctsio->kern_data_ptr, M_CTL);
6244 ctl_done((union ctl_io *)ctsio);
6245
6246 bailout_no_done:
6247
6248 return (CTL_RETVAL_COMPLETE);
6249
6250 }
6251
6252 int
ctl_mode_select(struct ctl_scsiio * ctsio)6253 ctl_mode_select(struct ctl_scsiio *ctsio)
6254 {
6255 struct ctl_lun *lun = CTL_LUN(ctsio);
6256 union ctl_modepage_info *modepage_info;
6257 int bd_len, i, header_size, param_len, rtd;
6258 uint32_t initidx;
6259
6260 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6261 switch (ctsio->cdb[0]) {
6262 case MODE_SELECT_6: {
6263 struct scsi_mode_select_6 *cdb;
6264
6265 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6266
6267 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6268 param_len = cdb->length;
6269 header_size = sizeof(struct scsi_mode_header_6);
6270 break;
6271 }
6272 case MODE_SELECT_10: {
6273 struct scsi_mode_select_10 *cdb;
6274
6275 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6276
6277 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6278 param_len = scsi_2btoul(cdb->length);
6279 header_size = sizeof(struct scsi_mode_header_10);
6280 break;
6281 }
6282 default:
6283 ctl_set_invalid_opcode(ctsio);
6284 ctl_done((union ctl_io *)ctsio);
6285 return (CTL_RETVAL_COMPLETE);
6286 }
6287
6288 if (rtd) {
6289 if (param_len != 0) {
6290 ctl_set_invalid_field(ctsio, /*sks_valid*/ 0,
6291 /*command*/ 1, /*field*/ 0,
6292 /*bit_valid*/ 0, /*bit*/ 0);
6293 ctl_done((union ctl_io *)ctsio);
6294 return (CTL_RETVAL_COMPLETE);
6295 }
6296
6297 /* Revert to defaults. */
6298 ctl_init_page_index(lun);
6299 mtx_lock(&lun->lun_lock);
6300 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
6301 mtx_unlock(&lun->lun_lock);
6302 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6303 ctl_isc_announce_mode(lun, -1,
6304 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
6305 lun->mode_pages.index[i].subpage);
6306 }
6307 ctl_set_success(ctsio);
6308 ctl_done((union ctl_io *)ctsio);
6309 return (CTL_RETVAL_COMPLETE);
6310 }
6311
6312 /*
6313 * From SPC-3:
6314 * "A parameter list length of zero indicates that the Data-Out Buffer
6315 * shall be empty. This condition shall not be considered as an error."
6316 */
6317 if (param_len == 0) {
6318 ctl_set_success(ctsio);
6319 ctl_done((union ctl_io *)ctsio);
6320 return (CTL_RETVAL_COMPLETE);
6321 }
6322
6323 /*
6324 * Since we'll hit this the first time through, prior to
6325 * allocation, we don't need to free a data buffer here.
6326 */
6327 if (param_len < header_size) {
6328 ctl_set_param_len_error(ctsio);
6329 ctl_done((union ctl_io *)ctsio);
6330 return (CTL_RETVAL_COMPLETE);
6331 }
6332
6333 /*
6334 * Allocate the data buffer and grab the user's data. In theory,
6335 * we shouldn't have to sanity check the parameter list length here
6336 * because the maximum size is 64K. We should be able to malloc
6337 * that much without too many problems.
6338 */
6339 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6340 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6341 ctsio->kern_data_len = param_len;
6342 ctsio->kern_total_len = param_len;
6343 ctsio->kern_rel_offset = 0;
6344 ctsio->kern_sg_entries = 0;
6345 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6346 ctsio->be_move_done = ctl_config_move_done;
6347 ctl_datamove((union ctl_io *)ctsio);
6348
6349 return (CTL_RETVAL_COMPLETE);
6350 }
6351
6352 switch (ctsio->cdb[0]) {
6353 case MODE_SELECT_6: {
6354 struct scsi_mode_header_6 *mh6;
6355
6356 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6357 bd_len = mh6->blk_desc_len;
6358 break;
6359 }
6360 case MODE_SELECT_10: {
6361 struct scsi_mode_header_10 *mh10;
6362
6363 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6364 bd_len = scsi_2btoul(mh10->blk_desc_len);
6365 break;
6366 }
6367 default:
6368 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6369 }
6370
6371 if (param_len < (header_size + bd_len)) {
6372 free(ctsio->kern_data_ptr, M_CTL);
6373 ctl_set_param_len_error(ctsio);
6374 ctl_done((union ctl_io *)ctsio);
6375 return (CTL_RETVAL_COMPLETE);
6376 }
6377
6378 /*
6379 * Set the IO_CONT flag, so that if this I/O gets passed to
6380 * ctl_config_write_done(), it'll get passed back to
6381 * ctl_do_mode_select() for further processing, or completion if
6382 * we're all done.
6383 */
6384 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6385 ctsio->io_cont = ctl_do_mode_select;
6386
6387 modepage_info = (union ctl_modepage_info *)
6388 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6389 memset(modepage_info, 0, sizeof(*modepage_info));
6390 modepage_info->header.len_left = param_len - header_size - bd_len;
6391 modepage_info->header.len_used = header_size + bd_len;
6392
6393 return (ctl_do_mode_select((union ctl_io *)ctsio));
6394 }
6395
6396 int
ctl_mode_sense(struct ctl_scsiio * ctsio)6397 ctl_mode_sense(struct ctl_scsiio *ctsio)
6398 {
6399 struct ctl_lun *lun = CTL_LUN(ctsio);
6400 int pc, page_code, llba, subpage;
6401 int alloc_len, page_len, header_len, bd_len, total_len;
6402 void *block_desc;
6403 struct ctl_page_index *page_index;
6404
6405 llba = 0;
6406
6407 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6408
6409 switch (ctsio->cdb[0]) {
6410 case MODE_SENSE_6: {
6411 struct scsi_mode_sense_6 *cdb;
6412
6413 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6414
6415 header_len = sizeof(struct scsi_mode_hdr_6);
6416 if (cdb->byte2 & SMS_DBD)
6417 bd_len = 0;
6418 else
6419 bd_len = sizeof(struct scsi_mode_block_descr);
6420 header_len += bd_len;
6421
6422 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6423 page_code = cdb->page & SMS_PAGE_CODE;
6424 subpage = cdb->subpage;
6425 alloc_len = cdb->length;
6426 break;
6427 }
6428 case MODE_SENSE_10: {
6429 struct scsi_mode_sense_10 *cdb;
6430
6431 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6432
6433 header_len = sizeof(struct scsi_mode_hdr_10);
6434 if (cdb->byte2 & SMS_DBD) {
6435 bd_len = 0;
6436 } else if (lun->be_lun->lun_type == T_DIRECT) {
6437 if (cdb->byte2 & SMS10_LLBAA) {
6438 llba = 1;
6439 bd_len = sizeof(struct scsi_mode_block_descr_dlong);
6440 } else
6441 bd_len = sizeof(struct scsi_mode_block_descr_dshort);
6442 } else
6443 bd_len = sizeof(struct scsi_mode_block_descr);
6444 header_len += bd_len;
6445
6446 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6447 page_code = cdb->page & SMS_PAGE_CODE;
6448 subpage = cdb->subpage;
6449 alloc_len = scsi_2btoul(cdb->length);
6450 break;
6451 }
6452 default:
6453 ctl_set_invalid_opcode(ctsio);
6454 ctl_done((union ctl_io *)ctsio);
6455 return (CTL_RETVAL_COMPLETE);
6456 break; /* NOTREACHED */
6457 }
6458
6459 /*
6460 * We have to make a first pass through to calculate the size of
6461 * the pages that match the user's query. Then we allocate enough
6462 * memory to hold it, and actually copy the data into the buffer.
6463 */
6464 switch (page_code) {
6465 case SMS_ALL_PAGES_PAGE: {
6466 u_int i;
6467
6468 page_len = 0;
6469
6470 /*
6471 * At the moment, values other than 0 and 0xff here are
6472 * reserved according to SPC-3.
6473 */
6474 if ((subpage != SMS_SUBPAGE_PAGE_0)
6475 && (subpage != SMS_SUBPAGE_ALL)) {
6476 ctl_set_invalid_field(ctsio,
6477 /*sks_valid*/ 1,
6478 /*command*/ 1,
6479 /*field*/ 3,
6480 /*bit_valid*/ 0,
6481 /*bit*/ 0);
6482 ctl_done((union ctl_io *)ctsio);
6483 return (CTL_RETVAL_COMPLETE);
6484 }
6485
6486 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6487 page_index = &lun->mode_pages.index[i];
6488
6489 /* Make sure the page is supported for this dev type */
6490 if (lun->be_lun->lun_type == T_DIRECT &&
6491 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6492 continue;
6493 if (lun->be_lun->lun_type == T_PROCESSOR &&
6494 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6495 continue;
6496 if (lun->be_lun->lun_type == T_CDROM &&
6497 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6498 continue;
6499
6500 /*
6501 * We don't use this subpage if the user didn't
6502 * request all subpages.
6503 */
6504 if ((page_index->subpage != 0)
6505 && (subpage == SMS_SUBPAGE_PAGE_0))
6506 continue;
6507
6508 page_len += page_index->page_len;
6509 }
6510 break;
6511 }
6512 default: {
6513 u_int i;
6514
6515 page_len = 0;
6516
6517 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6518 page_index = &lun->mode_pages.index[i];
6519
6520 /* Make sure the page is supported for this dev type */
6521 if (lun->be_lun->lun_type == T_DIRECT &&
6522 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6523 continue;
6524 if (lun->be_lun->lun_type == T_PROCESSOR &&
6525 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6526 continue;
6527 if (lun->be_lun->lun_type == T_CDROM &&
6528 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6529 continue;
6530
6531 /* Look for the right page code */
6532 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6533 continue;
6534
6535 /* Look for the right subpage or the subpage wildcard*/
6536 if ((page_index->subpage != subpage)
6537 && (subpage != SMS_SUBPAGE_ALL))
6538 continue;
6539
6540 page_len += page_index->page_len;
6541 }
6542
6543 if (page_len == 0) {
6544 ctl_set_invalid_field(ctsio,
6545 /*sks_valid*/ 1,
6546 /*command*/ 1,
6547 /*field*/ 2,
6548 /*bit_valid*/ 1,
6549 /*bit*/ 5);
6550 ctl_done((union ctl_io *)ctsio);
6551 return (CTL_RETVAL_COMPLETE);
6552 }
6553 break;
6554 }
6555 }
6556
6557 total_len = header_len + page_len;
6558
6559 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6560 ctsio->kern_sg_entries = 0;
6561 ctsio->kern_rel_offset = 0;
6562 ctsio->kern_data_len = min(total_len, alloc_len);
6563 ctsio->kern_total_len = ctsio->kern_data_len;
6564
6565 switch (ctsio->cdb[0]) {
6566 case MODE_SENSE_6: {
6567 struct scsi_mode_hdr_6 *header;
6568
6569 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6570
6571 header->datalen = MIN(total_len - 1, 254);
6572 if (lun->be_lun->lun_type == T_DIRECT) {
6573 header->dev_specific = 0x10; /* DPOFUA */
6574 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6575 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6576 header->dev_specific |= 0x80; /* WP */
6577 }
6578 header->block_descr_len = bd_len;
6579 block_desc = &header[1];
6580 break;
6581 }
6582 case MODE_SENSE_10: {
6583 struct scsi_mode_hdr_10 *header;
6584 int datalen;
6585
6586 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6587
6588 datalen = MIN(total_len - 2, 65533);
6589 scsi_ulto2b(datalen, header->datalen);
6590 if (lun->be_lun->lun_type == T_DIRECT) {
6591 header->dev_specific = 0x10; /* DPOFUA */
6592 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6593 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6594 header->dev_specific |= 0x80; /* WP */
6595 }
6596 if (llba)
6597 header->flags |= SMH_LONGLBA;
6598 scsi_ulto2b(bd_len, header->block_descr_len);
6599 block_desc = &header[1];
6600 break;
6601 }
6602 default:
6603 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6604 }
6605
6606 /*
6607 * If we've got a disk, use its blocksize in the block
6608 * descriptor. Otherwise, just set it to 0.
6609 */
6610 if (bd_len > 0) {
6611 if (lun->be_lun->lun_type == T_DIRECT) {
6612 if (llba) {
6613 struct scsi_mode_block_descr_dlong *bd = block_desc;
6614 if (lun->be_lun->maxlba != 0)
6615 scsi_u64to8b(lun->be_lun->maxlba + 1,
6616 bd->num_blocks);
6617 scsi_ulto4b(lun->be_lun->blocksize,
6618 bd->block_len);
6619 } else {
6620 struct scsi_mode_block_descr_dshort *bd = block_desc;
6621 if (lun->be_lun->maxlba != 0)
6622 scsi_ulto4b(MIN(lun->be_lun->maxlba+1,
6623 UINT32_MAX), bd->num_blocks);
6624 scsi_ulto3b(lun->be_lun->blocksize,
6625 bd->block_len);
6626 }
6627 } else {
6628 struct scsi_mode_block_descr *bd = block_desc;
6629 scsi_ulto3b(0, bd->block_len);
6630 }
6631 }
6632
6633 switch (page_code) {
6634 case SMS_ALL_PAGES_PAGE: {
6635 int i, data_used;
6636
6637 data_used = header_len;
6638 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6639 struct ctl_page_index *page_index;
6640
6641 page_index = &lun->mode_pages.index[i];
6642 if (lun->be_lun->lun_type == T_DIRECT &&
6643 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6644 continue;
6645 if (lun->be_lun->lun_type == T_PROCESSOR &&
6646 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6647 continue;
6648 if (lun->be_lun->lun_type == T_CDROM &&
6649 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6650 continue;
6651
6652 /*
6653 * We don't use this subpage if the user didn't
6654 * request all subpages. We already checked (above)
6655 * to make sure the user only specified a subpage
6656 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6657 */
6658 if ((page_index->subpage != 0)
6659 && (subpage == SMS_SUBPAGE_PAGE_0))
6660 continue;
6661
6662 /*
6663 * Call the handler, if it exists, to update the
6664 * page to the latest values.
6665 */
6666 if (page_index->sense_handler != NULL)
6667 page_index->sense_handler(ctsio, page_index,pc);
6668
6669 memcpy(ctsio->kern_data_ptr + data_used,
6670 page_index->page_data +
6671 (page_index->page_len * pc),
6672 page_index->page_len);
6673 data_used += page_index->page_len;
6674 }
6675 break;
6676 }
6677 default: {
6678 int i, data_used;
6679
6680 data_used = header_len;
6681
6682 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6683 struct ctl_page_index *page_index;
6684
6685 page_index = &lun->mode_pages.index[i];
6686
6687 /* Look for the right page code */
6688 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6689 continue;
6690
6691 /* Look for the right subpage or the subpage wildcard*/
6692 if ((page_index->subpage != subpage)
6693 && (subpage != SMS_SUBPAGE_ALL))
6694 continue;
6695
6696 /* Make sure the page is supported for this dev type */
6697 if (lun->be_lun->lun_type == T_DIRECT &&
6698 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6699 continue;
6700 if (lun->be_lun->lun_type == T_PROCESSOR &&
6701 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6702 continue;
6703 if (lun->be_lun->lun_type == T_CDROM &&
6704 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6705 continue;
6706
6707 /*
6708 * Call the handler, if it exists, to update the
6709 * page to the latest values.
6710 */
6711 if (page_index->sense_handler != NULL)
6712 page_index->sense_handler(ctsio, page_index,pc);
6713
6714 memcpy(ctsio->kern_data_ptr + data_used,
6715 page_index->page_data +
6716 (page_index->page_len * pc),
6717 page_index->page_len);
6718 data_used += page_index->page_len;
6719 }
6720 break;
6721 }
6722 }
6723
6724 ctl_set_success(ctsio);
6725 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6726 ctsio->be_move_done = ctl_config_move_done;
6727 ctl_datamove((union ctl_io *)ctsio);
6728 return (CTL_RETVAL_COMPLETE);
6729 }
6730
6731 int
ctl_temp_log_sense_handler(struct ctl_scsiio * ctsio,struct ctl_page_index * page_index,int pc)6732 ctl_temp_log_sense_handler(struct ctl_scsiio *ctsio,
6733 struct ctl_page_index *page_index,
6734 int pc)
6735 {
6736 struct ctl_lun *lun = CTL_LUN(ctsio);
6737 struct scsi_log_temperature *data;
6738 const char *value;
6739
6740 data = (struct scsi_log_temperature *)page_index->page_data;
6741
6742 scsi_ulto2b(SLP_TEMPERATURE, data->hdr.param_code);
6743 data->hdr.param_control = SLP_LBIN;
6744 data->hdr.param_len = sizeof(struct scsi_log_temperature) -
6745 sizeof(struct scsi_log_param_header);
6746 if ((value = dnvlist_get_string(lun->be_lun->options, "temperature",
6747 NULL)) != NULL)
6748 data->temperature = strtol(value, NULL, 0);
6749 else
6750 data->temperature = 0xff;
6751 data++;
6752
6753 scsi_ulto2b(SLP_REFTEMPERATURE, data->hdr.param_code);
6754 data->hdr.param_control = SLP_LBIN;
6755 data->hdr.param_len = sizeof(struct scsi_log_temperature) -
6756 sizeof(struct scsi_log_param_header);
6757 if ((value = dnvlist_get_string(lun->be_lun->options, "reftemperature",
6758 NULL)) != NULL)
6759 data->temperature = strtol(value, NULL, 0);
6760 else
6761 data->temperature = 0xff;
6762 return (0);
6763 }
6764
6765 int
ctl_lbp_log_sense_handler(struct ctl_scsiio * ctsio,struct ctl_page_index * page_index,int pc)6766 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6767 struct ctl_page_index *page_index,
6768 int pc)
6769 {
6770 struct ctl_lun *lun = CTL_LUN(ctsio);
6771 struct scsi_log_param_header *phdr;
6772 uint8_t *data;
6773 uint64_t val;
6774
6775 data = page_index->page_data;
6776
6777 if (lun->backend->lun_attr != NULL &&
6778 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6779 != UINT64_MAX) {
6780 phdr = (struct scsi_log_param_header *)data;
6781 scsi_ulto2b(0x0001, phdr->param_code);
6782 phdr->param_control = SLP_LBIN | SLP_LP;
6783 phdr->param_len = 8;
6784 data = (uint8_t *)(phdr + 1);
6785 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6786 data[4] = 0x02; /* per-pool */
6787 data += phdr->param_len;
6788 }
6789
6790 if (lun->backend->lun_attr != NULL &&
6791 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6792 != UINT64_MAX) {
6793 phdr = (struct scsi_log_param_header *)data;
6794 scsi_ulto2b(0x0002, phdr->param_code);
6795 phdr->param_control = SLP_LBIN | SLP_LP;
6796 phdr->param_len = 8;
6797 data = (uint8_t *)(phdr + 1);
6798 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6799 data[4] = 0x01; /* per-LUN */
6800 data += phdr->param_len;
6801 }
6802
6803 if (lun->backend->lun_attr != NULL &&
6804 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6805 != UINT64_MAX) {
6806 phdr = (struct scsi_log_param_header *)data;
6807 scsi_ulto2b(0x00f1, phdr->param_code);
6808 phdr->param_control = SLP_LBIN | SLP_LP;
6809 phdr->param_len = 8;
6810 data = (uint8_t *)(phdr + 1);
6811 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6812 data[4] = 0x02; /* per-pool */
6813 data += phdr->param_len;
6814 }
6815
6816 if (lun->backend->lun_attr != NULL &&
6817 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6818 != UINT64_MAX) {
6819 phdr = (struct scsi_log_param_header *)data;
6820 scsi_ulto2b(0x00f2, phdr->param_code);
6821 phdr->param_control = SLP_LBIN | SLP_LP;
6822 phdr->param_len = 8;
6823 data = (uint8_t *)(phdr + 1);
6824 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6825 data[4] = 0x02; /* per-pool */
6826 data += phdr->param_len;
6827 }
6828
6829 page_index->page_len = data - page_index->page_data;
6830 return (0);
6831 }
6832
6833 int
ctl_sap_log_sense_handler(struct ctl_scsiio * ctsio,struct ctl_page_index * page_index,int pc)6834 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6835 struct ctl_page_index *page_index,
6836 int pc)
6837 {
6838 struct ctl_lun *lun = CTL_LUN(ctsio);
6839 struct stat_page *data;
6840 struct bintime *t;
6841
6842 data = (struct stat_page *)page_index->page_data;
6843
6844 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6845 data->sap.hdr.param_control = SLP_LBIN;
6846 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6847 sizeof(struct scsi_log_param_header);
6848 scsi_u64to8b(lun->stats.operations[CTL_STATS_READ],
6849 data->sap.read_num);
6850 scsi_u64to8b(lun->stats.operations[CTL_STATS_WRITE],
6851 data->sap.write_num);
6852 if (lun->be_lun->blocksize > 0) {
6853 scsi_u64to8b(lun->stats.bytes[CTL_STATS_WRITE] /
6854 lun->be_lun->blocksize, data->sap.recvieved_lba);
6855 scsi_u64to8b(lun->stats.bytes[CTL_STATS_READ] /
6856 lun->be_lun->blocksize, data->sap.transmitted_lba);
6857 }
6858 t = &lun->stats.time[CTL_STATS_READ];
6859 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6860 data->sap.read_int);
6861 t = &lun->stats.time[CTL_STATS_WRITE];
6862 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6863 data->sap.write_int);
6864 scsi_u64to8b(0, data->sap.weighted_num);
6865 scsi_u64to8b(0, data->sap.weighted_int);
6866 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6867 data->it.hdr.param_control = SLP_LBIN;
6868 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6869 sizeof(struct scsi_log_param_header);
6870 #ifdef CTL_TIME_IO
6871 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6872 #endif
6873 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6874 data->it.hdr.param_control = SLP_LBIN;
6875 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6876 sizeof(struct scsi_log_param_header);
6877 scsi_ulto4b(3, data->ti.exponent);
6878 scsi_ulto4b(1, data->ti.integer);
6879 return (0);
6880 }
6881
6882 int
ctl_ie_log_sense_handler(struct ctl_scsiio * ctsio,struct ctl_page_index * page_index,int pc)6883 ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio,
6884 struct ctl_page_index *page_index,
6885 int pc)
6886 {
6887 struct ctl_lun *lun = CTL_LUN(ctsio);
6888 struct scsi_log_informational_exceptions *data;
6889 const char *value;
6890
6891 data = (struct scsi_log_informational_exceptions *)page_index->page_data;
6892
6893 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code);
6894 data->hdr.param_control = SLP_LBIN;
6895 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) -
6896 sizeof(struct scsi_log_param_header);
6897 data->ie_asc = lun->ie_asc;
6898 data->ie_ascq = lun->ie_ascq;
6899 if ((value = dnvlist_get_string(lun->be_lun->options, "temperature",
6900 NULL)) != NULL)
6901 data->temperature = strtol(value, NULL, 0);
6902 else
6903 data->temperature = 0xff;
6904 return (0);
6905 }
6906
6907 int
ctl_log_sense(struct ctl_scsiio * ctsio)6908 ctl_log_sense(struct ctl_scsiio *ctsio)
6909 {
6910 struct ctl_lun *lun = CTL_LUN(ctsio);
6911 int i, pc, page_code, subpage;
6912 int alloc_len, total_len;
6913 struct ctl_page_index *page_index;
6914 struct scsi_log_sense *cdb;
6915 struct scsi_log_header *header;
6916
6917 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6918
6919 cdb = (struct scsi_log_sense *)ctsio->cdb;
6920 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6921 page_code = cdb->page & SLS_PAGE_CODE;
6922 subpage = cdb->subpage;
6923 alloc_len = scsi_2btoul(cdb->length);
6924
6925 page_index = NULL;
6926 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6927 page_index = &lun->log_pages.index[i];
6928
6929 /* Look for the right page code */
6930 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6931 continue;
6932
6933 /* Look for the right subpage or the subpage wildcard*/
6934 if (page_index->subpage != subpage)
6935 continue;
6936
6937 break;
6938 }
6939 if (i >= CTL_NUM_LOG_PAGES) {
6940 ctl_set_invalid_field(ctsio,
6941 /*sks_valid*/ 1,
6942 /*command*/ 1,
6943 /*field*/ 2,
6944 /*bit_valid*/ 0,
6945 /*bit*/ 0);
6946 ctl_done((union ctl_io *)ctsio);
6947 return (CTL_RETVAL_COMPLETE);
6948 }
6949
6950 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6951
6952 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6953 ctsio->kern_sg_entries = 0;
6954 ctsio->kern_rel_offset = 0;
6955 ctsio->kern_data_len = min(total_len, alloc_len);
6956 ctsio->kern_total_len = ctsio->kern_data_len;
6957
6958 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6959 header->page = page_index->page_code;
6960 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING)
6961 header->page |= SL_DS;
6962 if (page_index->subpage) {
6963 header->page |= SL_SPF;
6964 header->subpage = page_index->subpage;
6965 }
6966 scsi_ulto2b(page_index->page_len, header->datalen);
6967
6968 /*
6969 * Call the handler, if it exists, to update the
6970 * page to the latest values.
6971 */
6972 if (page_index->sense_handler != NULL)
6973 page_index->sense_handler(ctsio, page_index, pc);
6974
6975 memcpy(header + 1, page_index->page_data, page_index->page_len);
6976
6977 ctl_set_success(ctsio);
6978 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6979 ctsio->be_move_done = ctl_config_move_done;
6980 ctl_datamove((union ctl_io *)ctsio);
6981 return (CTL_RETVAL_COMPLETE);
6982 }
6983
6984 int
ctl_read_capacity(struct ctl_scsiio * ctsio)6985 ctl_read_capacity(struct ctl_scsiio *ctsio)
6986 {
6987 struct ctl_lun *lun = CTL_LUN(ctsio);
6988 struct scsi_read_capacity *cdb;
6989 struct scsi_read_capacity_data *data;
6990 uint32_t lba;
6991
6992 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6993
6994 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6995
6996 lba = scsi_4btoul(cdb->addr);
6997 if (((cdb->pmi & SRC_PMI) == 0)
6998 && (lba != 0)) {
6999 ctl_set_invalid_field(/*ctsio*/ ctsio,
7000 /*sks_valid*/ 1,
7001 /*command*/ 1,
7002 /*field*/ 2,
7003 /*bit_valid*/ 0,
7004 /*bit*/ 0);
7005 ctl_done((union ctl_io *)ctsio);
7006 return (CTL_RETVAL_COMPLETE);
7007 }
7008
7009 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7010 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
7011 ctsio->kern_data_len = sizeof(*data);
7012 ctsio->kern_total_len = sizeof(*data);
7013 ctsio->kern_rel_offset = 0;
7014 ctsio->kern_sg_entries = 0;
7015
7016 /*
7017 * If the maximum LBA is greater than 0xfffffffe, the user must
7018 * issue a SERVICE ACTION IN (16) command, with the read capacity
7019 * serivce action set.
7020 */
7021 if (lun->be_lun->maxlba > 0xfffffffe)
7022 scsi_ulto4b(0xffffffff, data->addr);
7023 else
7024 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
7025
7026 /*
7027 * XXX KDM this may not be 512 bytes...
7028 */
7029 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7030
7031 ctl_set_success(ctsio);
7032 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7033 ctsio->be_move_done = ctl_config_move_done;
7034 ctl_datamove((union ctl_io *)ctsio);
7035 return (CTL_RETVAL_COMPLETE);
7036 }
7037
7038 int
ctl_read_capacity_16(struct ctl_scsiio * ctsio)7039 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
7040 {
7041 struct ctl_lun *lun = CTL_LUN(ctsio);
7042 struct scsi_read_capacity_16 *cdb;
7043 struct scsi_read_capacity_data_long *data;
7044 uint64_t lba;
7045 uint32_t alloc_len;
7046
7047 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7048
7049 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7050
7051 alloc_len = scsi_4btoul(cdb->alloc_len);
7052 lba = scsi_8btou64(cdb->addr);
7053
7054 if ((cdb->reladr & SRC16_PMI)
7055 && (lba != 0)) {
7056 ctl_set_invalid_field(/*ctsio*/ ctsio,
7057 /*sks_valid*/ 1,
7058 /*command*/ 1,
7059 /*field*/ 2,
7060 /*bit_valid*/ 0,
7061 /*bit*/ 0);
7062 ctl_done((union ctl_io *)ctsio);
7063 return (CTL_RETVAL_COMPLETE);
7064 }
7065
7066 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7067 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7068 ctsio->kern_rel_offset = 0;
7069 ctsio->kern_sg_entries = 0;
7070 ctsio->kern_data_len = min(sizeof(*data), alloc_len);
7071 ctsio->kern_total_len = ctsio->kern_data_len;
7072
7073 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7074 /* XXX KDM this may not be 512 bytes... */
7075 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7076 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7077 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7078 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7079 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
7080
7081 ctl_set_success(ctsio);
7082 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7083 ctsio->be_move_done = ctl_config_move_done;
7084 ctl_datamove((union ctl_io *)ctsio);
7085 return (CTL_RETVAL_COMPLETE);
7086 }
7087
7088 int
ctl_get_lba_status(struct ctl_scsiio * ctsio)7089 ctl_get_lba_status(struct ctl_scsiio *ctsio)
7090 {
7091 struct ctl_lun *lun = CTL_LUN(ctsio);
7092 struct scsi_get_lba_status *cdb;
7093 struct scsi_get_lba_status_data *data;
7094 struct ctl_lba_len_flags *lbalen;
7095 uint64_t lba;
7096 uint32_t alloc_len, total_len;
7097 int retval;
7098
7099 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
7100
7101 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
7102 lba = scsi_8btou64(cdb->addr);
7103 alloc_len = scsi_4btoul(cdb->alloc_len);
7104
7105 if (lba > lun->be_lun->maxlba) {
7106 ctl_set_lba_out_of_range(ctsio, lba);
7107 ctl_done((union ctl_io *)ctsio);
7108 return (CTL_RETVAL_COMPLETE);
7109 }
7110
7111 total_len = sizeof(*data) + sizeof(data->descr[0]);
7112 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7113 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
7114 ctsio->kern_rel_offset = 0;
7115 ctsio->kern_sg_entries = 0;
7116 ctsio->kern_data_len = min(total_len, alloc_len);
7117 ctsio->kern_total_len = ctsio->kern_data_len;
7118
7119 /* Fill dummy data in case backend can't tell anything. */
7120 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
7121 scsi_u64to8b(lba, data->descr[0].addr);
7122 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
7123 data->descr[0].length);
7124 data->descr[0].status = 0; /* Mapped or unknown. */
7125
7126 ctl_set_success(ctsio);
7127 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7128 ctsio->be_move_done = ctl_config_move_done;
7129
7130 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7131 lbalen->lba = lba;
7132 lbalen->len = total_len;
7133 lbalen->flags = 0;
7134 retval = lun->backend->config_read((union ctl_io *)ctsio);
7135 return (retval);
7136 }
7137
7138 int
ctl_read_defect(struct ctl_scsiio * ctsio)7139 ctl_read_defect(struct ctl_scsiio *ctsio)
7140 {
7141 struct scsi_read_defect_data_10 *ccb10;
7142 struct scsi_read_defect_data_12 *ccb12;
7143 struct scsi_read_defect_data_hdr_10 *data10;
7144 struct scsi_read_defect_data_hdr_12 *data12;
7145 uint32_t alloc_len, data_len;
7146 uint8_t format;
7147
7148 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7149
7150 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7151 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7152 format = ccb10->format;
7153 alloc_len = scsi_2btoul(ccb10->alloc_length);
7154 data_len = sizeof(*data10);
7155 } else {
7156 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7157 format = ccb12->format;
7158 alloc_len = scsi_4btoul(ccb12->alloc_length);
7159 data_len = sizeof(*data12);
7160 }
7161 if (alloc_len == 0) {
7162 ctl_set_success(ctsio);
7163 ctl_done((union ctl_io *)ctsio);
7164 return (CTL_RETVAL_COMPLETE);
7165 }
7166
7167 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7168 ctsio->kern_rel_offset = 0;
7169 ctsio->kern_sg_entries = 0;
7170 ctsio->kern_data_len = min(data_len, alloc_len);
7171 ctsio->kern_total_len = ctsio->kern_data_len;
7172
7173 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7174 data10 = (struct scsi_read_defect_data_hdr_10 *)
7175 ctsio->kern_data_ptr;
7176 data10->format = format;
7177 scsi_ulto2b(0, data10->length);
7178 } else {
7179 data12 = (struct scsi_read_defect_data_hdr_12 *)
7180 ctsio->kern_data_ptr;
7181 data12->format = format;
7182 scsi_ulto2b(0, data12->generation);
7183 scsi_ulto4b(0, data12->length);
7184 }
7185
7186 ctl_set_success(ctsio);
7187 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7188 ctsio->be_move_done = ctl_config_move_done;
7189 ctl_datamove((union ctl_io *)ctsio);
7190 return (CTL_RETVAL_COMPLETE);
7191 }
7192
7193 int
ctl_report_tagret_port_groups(struct ctl_scsiio * ctsio)7194 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7195 {
7196 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7197 struct ctl_lun *lun = CTL_LUN(ctsio);
7198 struct scsi_maintenance_in *cdb;
7199 int retval;
7200 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os;
7201 int num_ha_groups, num_target_ports, shared_group;
7202 struct ctl_port *port;
7203 struct scsi_target_group_data *rtg_ptr;
7204 struct scsi_target_group_data_extended *rtg_ext_ptr;
7205 struct scsi_target_port_group_descriptor *tpg_desc;
7206
7207 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7208
7209 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7210 retval = CTL_RETVAL_COMPLETE;
7211
7212 switch (cdb->byte2 & STG_PDF_MASK) {
7213 case STG_PDF_LENGTH:
7214 ext = 0;
7215 break;
7216 case STG_PDF_EXTENDED:
7217 ext = 1;
7218 break;
7219 default:
7220 ctl_set_invalid_field(/*ctsio*/ ctsio,
7221 /*sks_valid*/ 1,
7222 /*command*/ 1,
7223 /*field*/ 2,
7224 /*bit_valid*/ 1,
7225 /*bit*/ 5);
7226 ctl_done((union ctl_io *)ctsio);
7227 return(retval);
7228 }
7229
7230 num_target_ports = 0;
7231 shared_group = (softc->is_single != 0);
7232 mtx_lock(&softc->ctl_lock);
7233 STAILQ_FOREACH(port, &softc->port_list, links) {
7234 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7235 continue;
7236 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7237 continue;
7238 num_target_ports++;
7239 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7240 shared_group = 1;
7241 }
7242 mtx_unlock(&softc->ctl_lock);
7243 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES;
7244
7245 if (ext)
7246 total_len = sizeof(struct scsi_target_group_data_extended);
7247 else
7248 total_len = sizeof(struct scsi_target_group_data);
7249 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7250 (shared_group + num_ha_groups) +
7251 sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7252
7253 alloc_len = scsi_4btoul(cdb->length);
7254
7255 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7256 ctsio->kern_sg_entries = 0;
7257 ctsio->kern_rel_offset = 0;
7258 ctsio->kern_data_len = min(total_len, alloc_len);
7259 ctsio->kern_total_len = ctsio->kern_data_len;
7260
7261 if (ext) {
7262 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7263 ctsio->kern_data_ptr;
7264 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7265 rtg_ext_ptr->format_type = 0x10;
7266 rtg_ext_ptr->implicit_transition_time = 0;
7267 tpg_desc = &rtg_ext_ptr->groups[0];
7268 } else {
7269 rtg_ptr = (struct scsi_target_group_data *)
7270 ctsio->kern_data_ptr;
7271 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7272 tpg_desc = &rtg_ptr->groups[0];
7273 }
7274
7275 mtx_lock(&softc->ctl_lock);
7276 pg = softc->port_min / softc->port_cnt;
7277 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) {
7278 /* Some shelf is known to be primary. */
7279 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7280 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7281 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7282 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7283 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7284 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7285 else
7286 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7287 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7288 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7289 } else {
7290 ts = os;
7291 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7292 }
7293 } else {
7294 /* No known primary shelf. */
7295 if (softc->ha_link == CTL_HA_LINK_OFFLINE) {
7296 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7297 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7298 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) {
7299 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7300 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7301 } else {
7302 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7303 }
7304 }
7305 if (shared_group) {
7306 tpg_desc->pref_state = ts;
7307 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7308 TPG_U_SUP | TPG_T_SUP;
7309 scsi_ulto2b(1, tpg_desc->target_port_group);
7310 tpg_desc->status = TPG_IMPLICIT;
7311 pc = 0;
7312 STAILQ_FOREACH(port, &softc->port_list, links) {
7313 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7314 continue;
7315 if (!softc->is_single &&
7316 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0)
7317 continue;
7318 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7319 continue;
7320 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7321 relative_target_port_identifier);
7322 pc++;
7323 }
7324 tpg_desc->target_port_count = pc;
7325 tpg_desc = (struct scsi_target_port_group_descriptor *)
7326 &tpg_desc->descriptors[pc];
7327 }
7328 for (g = 0; g < num_ha_groups; g++) {
7329 tpg_desc->pref_state = (g == pg) ? ts : os;
7330 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7331 TPG_U_SUP | TPG_T_SUP;
7332 scsi_ulto2b(2 + g, tpg_desc->target_port_group);
7333 tpg_desc->status = TPG_IMPLICIT;
7334 pc = 0;
7335 STAILQ_FOREACH(port, &softc->port_list, links) {
7336 if (port->targ_port < g * softc->port_cnt ||
7337 port->targ_port >= (g + 1) * softc->port_cnt)
7338 continue;
7339 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7340 continue;
7341 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7342 continue;
7343 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7344 continue;
7345 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7346 relative_target_port_identifier);
7347 pc++;
7348 }
7349 tpg_desc->target_port_count = pc;
7350 tpg_desc = (struct scsi_target_port_group_descriptor *)
7351 &tpg_desc->descriptors[pc];
7352 }
7353 mtx_unlock(&softc->ctl_lock);
7354
7355 ctl_set_success(ctsio);
7356 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7357 ctsio->be_move_done = ctl_config_move_done;
7358 ctl_datamove((union ctl_io *)ctsio);
7359 return(retval);
7360 }
7361
7362 int
ctl_report_supported_opcodes(struct ctl_scsiio * ctsio)7363 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7364 {
7365 struct ctl_lun *lun = CTL_LUN(ctsio);
7366 struct scsi_report_supported_opcodes *cdb;
7367 const struct ctl_cmd_entry *entry, *sentry;
7368 struct scsi_report_supported_opcodes_all *all;
7369 struct scsi_report_supported_opcodes_descr *descr;
7370 struct scsi_report_supported_opcodes_one *one;
7371 int retval;
7372 int alloc_len, total_len;
7373 int opcode, service_action, i, j, num;
7374
7375 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7376
7377 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7378 retval = CTL_RETVAL_COMPLETE;
7379
7380 opcode = cdb->requested_opcode;
7381 service_action = scsi_2btoul(cdb->requested_service_action);
7382 switch (cdb->options & RSO_OPTIONS_MASK) {
7383 case RSO_OPTIONS_ALL:
7384 num = 0;
7385 for (i = 0; i < 256; i++) {
7386 entry = &ctl_cmd_table[i];
7387 if (entry->flags & CTL_CMD_FLAG_SA5) {
7388 for (j = 0; j < 32; j++) {
7389 sentry = &((const struct ctl_cmd_entry *)
7390 entry->execute)[j];
7391 if (ctl_cmd_applicable(
7392 lun->be_lun->lun_type, sentry))
7393 num++;
7394 }
7395 } else {
7396 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7397 entry))
7398 num++;
7399 }
7400 }
7401 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7402 num * sizeof(struct scsi_report_supported_opcodes_descr);
7403 break;
7404 case RSO_OPTIONS_OC:
7405 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7406 ctl_set_invalid_field(/*ctsio*/ ctsio,
7407 /*sks_valid*/ 1,
7408 /*command*/ 1,
7409 /*field*/ 2,
7410 /*bit_valid*/ 1,
7411 /*bit*/ 2);
7412 ctl_done((union ctl_io *)ctsio);
7413 return (CTL_RETVAL_COMPLETE);
7414 }
7415 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7416 break;
7417 case RSO_OPTIONS_OC_SA:
7418 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7419 service_action >= 32) {
7420 ctl_set_invalid_field(/*ctsio*/ ctsio,
7421 /*sks_valid*/ 1,
7422 /*command*/ 1,
7423 /*field*/ 2,
7424 /*bit_valid*/ 1,
7425 /*bit*/ 2);
7426 ctl_done((union ctl_io *)ctsio);
7427 return (CTL_RETVAL_COMPLETE);
7428 }
7429 /* FALLTHROUGH */
7430 case RSO_OPTIONS_OC_ASA:
7431 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7432 break;
7433 default:
7434 ctl_set_invalid_field(/*ctsio*/ ctsio,
7435 /*sks_valid*/ 1,
7436 /*command*/ 1,
7437 /*field*/ 2,
7438 /*bit_valid*/ 1,
7439 /*bit*/ 2);
7440 ctl_done((union ctl_io *)ctsio);
7441 return (CTL_RETVAL_COMPLETE);
7442 }
7443
7444 alloc_len = scsi_4btoul(cdb->length);
7445
7446 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7447 ctsio->kern_sg_entries = 0;
7448 ctsio->kern_rel_offset = 0;
7449 ctsio->kern_data_len = min(total_len, alloc_len);
7450 ctsio->kern_total_len = ctsio->kern_data_len;
7451
7452 switch (cdb->options & RSO_OPTIONS_MASK) {
7453 case RSO_OPTIONS_ALL:
7454 all = (struct scsi_report_supported_opcodes_all *)
7455 ctsio->kern_data_ptr;
7456 num = 0;
7457 for (i = 0; i < 256; i++) {
7458 entry = &ctl_cmd_table[i];
7459 if (entry->flags & CTL_CMD_FLAG_SA5) {
7460 for (j = 0; j < 32; j++) {
7461 sentry = &((const struct ctl_cmd_entry *)
7462 entry->execute)[j];
7463 if (!ctl_cmd_applicable(
7464 lun->be_lun->lun_type, sentry))
7465 continue;
7466 descr = &all->descr[num++];
7467 descr->opcode = i;
7468 scsi_ulto2b(j, descr->service_action);
7469 descr->flags = RSO_SERVACTV;
7470 scsi_ulto2b(sentry->length,
7471 descr->cdb_length);
7472 }
7473 } else {
7474 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7475 entry))
7476 continue;
7477 descr = &all->descr[num++];
7478 descr->opcode = i;
7479 scsi_ulto2b(0, descr->service_action);
7480 descr->flags = 0;
7481 scsi_ulto2b(entry->length, descr->cdb_length);
7482 }
7483 }
7484 scsi_ulto4b(
7485 num * sizeof(struct scsi_report_supported_opcodes_descr),
7486 all->length);
7487 break;
7488 case RSO_OPTIONS_OC:
7489 one = (struct scsi_report_supported_opcodes_one *)
7490 ctsio->kern_data_ptr;
7491 entry = &ctl_cmd_table[opcode];
7492 goto fill_one;
7493 case RSO_OPTIONS_OC_SA:
7494 one = (struct scsi_report_supported_opcodes_one *)
7495 ctsio->kern_data_ptr;
7496 entry = &ctl_cmd_table[opcode];
7497 entry = &((const struct ctl_cmd_entry *)
7498 entry->execute)[service_action];
7499 fill_one:
7500 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7501 one->support = 3;
7502 scsi_ulto2b(entry->length, one->cdb_length);
7503 one->cdb_usage[0] = opcode;
7504 memcpy(&one->cdb_usage[1], entry->usage,
7505 entry->length - 1);
7506 } else
7507 one->support = 1;
7508 break;
7509 case RSO_OPTIONS_OC_ASA:
7510 one = (struct scsi_report_supported_opcodes_one *)
7511 ctsio->kern_data_ptr;
7512 entry = &ctl_cmd_table[opcode];
7513 if (entry->flags & CTL_CMD_FLAG_SA5) {
7514 entry = &((const struct ctl_cmd_entry *)
7515 entry->execute)[service_action];
7516 } else if (service_action != 0) {
7517 one->support = 1;
7518 break;
7519 }
7520 goto fill_one;
7521 }
7522
7523 ctl_set_success(ctsio);
7524 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7525 ctsio->be_move_done = ctl_config_move_done;
7526 ctl_datamove((union ctl_io *)ctsio);
7527 return(retval);
7528 }
7529
7530 int
ctl_report_supported_tmf(struct ctl_scsiio * ctsio)7531 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7532 {
7533 struct scsi_report_supported_tmf *cdb;
7534 struct scsi_report_supported_tmf_ext_data *data;
7535 int retval;
7536 int alloc_len, total_len;
7537
7538 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7539
7540 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7541
7542 retval = CTL_RETVAL_COMPLETE;
7543
7544 if (cdb->options & RST_REPD)
7545 total_len = sizeof(struct scsi_report_supported_tmf_ext_data);
7546 else
7547 total_len = sizeof(struct scsi_report_supported_tmf_data);
7548 alloc_len = scsi_4btoul(cdb->length);
7549
7550 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7551 ctsio->kern_sg_entries = 0;
7552 ctsio->kern_rel_offset = 0;
7553 ctsio->kern_data_len = min(total_len, alloc_len);
7554 ctsio->kern_total_len = ctsio->kern_data_len;
7555
7556 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr;
7557 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7558 RST_TRS;
7559 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7560 data->length = total_len - 4;
7561
7562 ctl_set_success(ctsio);
7563 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7564 ctsio->be_move_done = ctl_config_move_done;
7565 ctl_datamove((union ctl_io *)ctsio);
7566 return (retval);
7567 }
7568
7569 int
ctl_report_timestamp(struct ctl_scsiio * ctsio)7570 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7571 {
7572 struct scsi_report_timestamp *cdb;
7573 struct scsi_report_timestamp_data *data;
7574 struct timeval tv;
7575 int64_t timestamp;
7576 int retval;
7577 int alloc_len, total_len;
7578
7579 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7580
7581 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7582
7583 retval = CTL_RETVAL_COMPLETE;
7584
7585 total_len = sizeof(struct scsi_report_timestamp_data);
7586 alloc_len = scsi_4btoul(cdb->length);
7587
7588 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7589 ctsio->kern_sg_entries = 0;
7590 ctsio->kern_rel_offset = 0;
7591 ctsio->kern_data_len = min(total_len, alloc_len);
7592 ctsio->kern_total_len = ctsio->kern_data_len;
7593
7594 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7595 scsi_ulto2b(sizeof(*data) - 2, data->length);
7596 data->origin = RTS_ORIG_OUTSIDE;
7597 getmicrotime(&tv);
7598 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7599 scsi_ulto4b(timestamp >> 16, data->timestamp);
7600 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7601
7602 ctl_set_success(ctsio);
7603 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7604 ctsio->be_move_done = ctl_config_move_done;
7605 ctl_datamove((union ctl_io *)ctsio);
7606 return (retval);
7607 }
7608
7609 int
ctl_persistent_reserve_in(struct ctl_scsiio * ctsio)7610 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7611 {
7612 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7613 struct ctl_lun *lun = CTL_LUN(ctsio);
7614 struct scsi_per_res_in *cdb;
7615 int alloc_len, total_len = 0;
7616 /* struct scsi_per_res_in_rsrv in_data; */
7617 uint64_t key;
7618
7619 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7620
7621 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7622
7623 alloc_len = scsi_2btoul(cdb->length);
7624
7625 retry:
7626 mtx_lock(&lun->lun_lock);
7627 switch (cdb->action) {
7628 case SPRI_RK: /* read keys */
7629 total_len = sizeof(struct scsi_per_res_in_keys) +
7630 lun->pr_key_count *
7631 sizeof(struct scsi_per_res_key);
7632 break;
7633 case SPRI_RR: /* read reservation */
7634 if (lun->flags & CTL_LUN_PR_RESERVED)
7635 total_len = sizeof(struct scsi_per_res_in_rsrv);
7636 else
7637 total_len = sizeof(struct scsi_per_res_in_header);
7638 break;
7639 case SPRI_RC: /* report capabilities */
7640 total_len = sizeof(struct scsi_per_res_cap);
7641 break;
7642 case SPRI_RS: /* read full status */
7643 total_len = sizeof(struct scsi_per_res_in_header) +
7644 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7645 lun->pr_key_count;
7646 break;
7647 default:
7648 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7649 }
7650 mtx_unlock(&lun->lun_lock);
7651
7652 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7653 ctsio->kern_rel_offset = 0;
7654 ctsio->kern_sg_entries = 0;
7655 ctsio->kern_data_len = min(total_len, alloc_len);
7656 ctsio->kern_total_len = ctsio->kern_data_len;
7657
7658 mtx_lock(&lun->lun_lock);
7659 switch (cdb->action) {
7660 case SPRI_RK: { // read keys
7661 struct scsi_per_res_in_keys *res_keys;
7662 int i, key_count;
7663
7664 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7665
7666 /*
7667 * We had to drop the lock to allocate our buffer, which
7668 * leaves time for someone to come in with another
7669 * persistent reservation. (That is unlikely, though,
7670 * since this should be the only persistent reservation
7671 * command active right now.)
7672 */
7673 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7674 (lun->pr_key_count *
7675 sizeof(struct scsi_per_res_key)))){
7676 mtx_unlock(&lun->lun_lock);
7677 free(ctsio->kern_data_ptr, M_CTL);
7678 printf("%s: reservation length changed, retrying\n",
7679 __func__);
7680 goto retry;
7681 }
7682
7683 scsi_ulto4b(lun->pr_generation, res_keys->header.generation);
7684
7685 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7686 lun->pr_key_count, res_keys->header.length);
7687
7688 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7689 if ((key = ctl_get_prkey(lun, i)) == 0)
7690 continue;
7691
7692 /*
7693 * We used lun->pr_key_count to calculate the
7694 * size to allocate. If it turns out the number of
7695 * initiators with the registered flag set is
7696 * larger than that (i.e. they haven't been kept in
7697 * sync), we've got a problem.
7698 */
7699 if (key_count >= lun->pr_key_count) {
7700 key_count++;
7701 continue;
7702 }
7703 scsi_u64to8b(key, res_keys->keys[key_count].key);
7704 key_count++;
7705 }
7706 break;
7707 }
7708 case SPRI_RR: { // read reservation
7709 struct scsi_per_res_in_rsrv *res;
7710 int tmp_len, header_only;
7711
7712 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7713
7714 scsi_ulto4b(lun->pr_generation, res->header.generation);
7715
7716 if (lun->flags & CTL_LUN_PR_RESERVED)
7717 {
7718 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7719 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7720 res->header.length);
7721 header_only = 0;
7722 } else {
7723 tmp_len = sizeof(struct scsi_per_res_in_header);
7724 scsi_ulto4b(0, res->header.length);
7725 header_only = 1;
7726 }
7727
7728 /*
7729 * We had to drop the lock to allocate our buffer, which
7730 * leaves time for someone to come in with another
7731 * persistent reservation. (That is unlikely, though,
7732 * since this should be the only persistent reservation
7733 * command active right now.)
7734 */
7735 if (tmp_len != total_len) {
7736 mtx_unlock(&lun->lun_lock);
7737 free(ctsio->kern_data_ptr, M_CTL);
7738 printf("%s: reservation status changed, retrying\n",
7739 __func__);
7740 goto retry;
7741 }
7742
7743 /*
7744 * No reservation held, so we're done.
7745 */
7746 if (header_only != 0)
7747 break;
7748
7749 /*
7750 * If the registration is an All Registrants type, the key
7751 * is 0, since it doesn't really matter.
7752 */
7753 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7754 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7755 res->data.reservation);
7756 }
7757 res->data.scopetype = lun->pr_res_type;
7758 break;
7759 }
7760 case SPRI_RC: //report capabilities
7761 {
7762 struct scsi_per_res_cap *res_cap;
7763 uint16_t type_mask;
7764
7765 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7766 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7767 res_cap->flags1 = SPRI_CRH;
7768 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5;
7769 type_mask = SPRI_TM_WR_EX_AR |
7770 SPRI_TM_EX_AC_RO |
7771 SPRI_TM_WR_EX_RO |
7772 SPRI_TM_EX_AC |
7773 SPRI_TM_WR_EX |
7774 SPRI_TM_EX_AC_AR;
7775 scsi_ulto2b(type_mask, res_cap->type_mask);
7776 break;
7777 }
7778 case SPRI_RS: { // read full status
7779 struct scsi_per_res_in_full *res_status;
7780 struct scsi_per_res_in_full_desc *res_desc;
7781 struct ctl_port *port;
7782 int i, len;
7783
7784 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7785
7786 /*
7787 * We had to drop the lock to allocate our buffer, which
7788 * leaves time for someone to come in with another
7789 * persistent reservation. (That is unlikely, though,
7790 * since this should be the only persistent reservation
7791 * command active right now.)
7792 */
7793 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7794 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7795 lun->pr_key_count)){
7796 mtx_unlock(&lun->lun_lock);
7797 free(ctsio->kern_data_ptr, M_CTL);
7798 printf("%s: reservation length changed, retrying\n",
7799 __func__);
7800 goto retry;
7801 }
7802
7803 scsi_ulto4b(lun->pr_generation, res_status->header.generation);
7804
7805 res_desc = &res_status->desc[0];
7806 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7807 if ((key = ctl_get_prkey(lun, i)) == 0)
7808 continue;
7809
7810 scsi_u64to8b(key, res_desc->res_key.key);
7811 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7812 (lun->pr_res_idx == i ||
7813 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7814 res_desc->flags = SPRI_FULL_R_HOLDER;
7815 res_desc->scopetype = lun->pr_res_type;
7816 }
7817 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7818 res_desc->rel_trgt_port_id);
7819 len = 0;
7820 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7821 if (port != NULL)
7822 len = ctl_create_iid(port,
7823 i % CTL_MAX_INIT_PER_PORT,
7824 res_desc->transport_id);
7825 scsi_ulto4b(len, res_desc->additional_length);
7826 res_desc = (struct scsi_per_res_in_full_desc *)
7827 &res_desc->transport_id[len];
7828 }
7829 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7830 res_status->header.length);
7831 break;
7832 }
7833 default:
7834 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7835 }
7836 mtx_unlock(&lun->lun_lock);
7837
7838 ctl_set_success(ctsio);
7839 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7840 ctsio->be_move_done = ctl_config_move_done;
7841 ctl_datamove((union ctl_io *)ctsio);
7842 return (CTL_RETVAL_COMPLETE);
7843 }
7844
7845 /*
7846 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7847 * it should return.
7848 */
7849 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)7850 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7851 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7852 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7853 struct scsi_per_res_out_parms* param)
7854 {
7855 union ctl_ha_msg persis_io;
7856 int i;
7857
7858 mtx_lock(&lun->lun_lock);
7859 if (sa_res_key == 0) {
7860 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7861 /* validate scope and type */
7862 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7863 SPR_LU_SCOPE) {
7864 mtx_unlock(&lun->lun_lock);
7865 ctl_set_invalid_field(/*ctsio*/ ctsio,
7866 /*sks_valid*/ 1,
7867 /*command*/ 1,
7868 /*field*/ 2,
7869 /*bit_valid*/ 1,
7870 /*bit*/ 4);
7871 ctl_done((union ctl_io *)ctsio);
7872 return (1);
7873 }
7874
7875 if (type>8 || type==2 || type==4 || type==0) {
7876 mtx_unlock(&lun->lun_lock);
7877 ctl_set_invalid_field(/*ctsio*/ ctsio,
7878 /*sks_valid*/ 1,
7879 /*command*/ 1,
7880 /*field*/ 2,
7881 /*bit_valid*/ 1,
7882 /*bit*/ 0);
7883 ctl_done((union ctl_io *)ctsio);
7884 return (1);
7885 }
7886
7887 /*
7888 * Unregister everybody else and build UA for
7889 * them
7890 */
7891 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7892 if (i == residx || ctl_get_prkey(lun, i) == 0)
7893 continue;
7894
7895 ctl_clr_prkey(lun, i);
7896 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7897 }
7898 lun->pr_key_count = 1;
7899 lun->pr_res_type = type;
7900 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7901 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7902 lun->pr_res_idx = residx;
7903 lun->pr_generation++;
7904 mtx_unlock(&lun->lun_lock);
7905
7906 /* send msg to other side */
7907 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7908 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7909 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7910 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7911 persis_io.pr.pr_info.res_type = type;
7912 memcpy(persis_io.pr.pr_info.sa_res_key,
7913 param->serv_act_res_key,
7914 sizeof(param->serv_act_res_key));
7915 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7916 sizeof(persis_io.pr), M_WAITOK);
7917 } else {
7918 /* not all registrants */
7919 mtx_unlock(&lun->lun_lock);
7920 free(ctsio->kern_data_ptr, M_CTL);
7921 ctl_set_invalid_field(ctsio,
7922 /*sks_valid*/ 1,
7923 /*command*/ 0,
7924 /*field*/ 8,
7925 /*bit_valid*/ 0,
7926 /*bit*/ 0);
7927 ctl_done((union ctl_io *)ctsio);
7928 return (1);
7929 }
7930 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7931 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7932 int found = 0;
7933
7934 if (res_key == sa_res_key) {
7935 /* special case */
7936 /*
7937 * The spec implies this is not good but doesn't
7938 * say what to do. There are two choices either
7939 * generate a res conflict or check condition
7940 * with illegal field in parameter data. Since
7941 * that is what is done when the sa_res_key is
7942 * zero I'll take that approach since this has
7943 * to do with the sa_res_key.
7944 */
7945 mtx_unlock(&lun->lun_lock);
7946 free(ctsio->kern_data_ptr, M_CTL);
7947 ctl_set_invalid_field(ctsio,
7948 /*sks_valid*/ 1,
7949 /*command*/ 0,
7950 /*field*/ 8,
7951 /*bit_valid*/ 0,
7952 /*bit*/ 0);
7953 ctl_done((union ctl_io *)ctsio);
7954 return (1);
7955 }
7956
7957 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7958 if (ctl_get_prkey(lun, i) != sa_res_key)
7959 continue;
7960
7961 found = 1;
7962 ctl_clr_prkey(lun, i);
7963 lun->pr_key_count--;
7964 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7965 }
7966 if (!found) {
7967 mtx_unlock(&lun->lun_lock);
7968 free(ctsio->kern_data_ptr, M_CTL);
7969 ctl_set_reservation_conflict(ctsio);
7970 ctl_done((union ctl_io *)ctsio);
7971 return (CTL_RETVAL_COMPLETE);
7972 }
7973 lun->pr_generation++;
7974 mtx_unlock(&lun->lun_lock);
7975
7976 /* send msg to other side */
7977 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7978 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7979 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7980 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7981 persis_io.pr.pr_info.res_type = type;
7982 memcpy(persis_io.pr.pr_info.sa_res_key,
7983 param->serv_act_res_key,
7984 sizeof(param->serv_act_res_key));
7985 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7986 sizeof(persis_io.pr), M_WAITOK);
7987 } else {
7988 /* Reserved but not all registrants */
7989 /* sa_res_key is res holder */
7990 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
7991 /* validate scope and type */
7992 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7993 SPR_LU_SCOPE) {
7994 mtx_unlock(&lun->lun_lock);
7995 ctl_set_invalid_field(/*ctsio*/ ctsio,
7996 /*sks_valid*/ 1,
7997 /*command*/ 1,
7998 /*field*/ 2,
7999 /*bit_valid*/ 1,
8000 /*bit*/ 4);
8001 ctl_done((union ctl_io *)ctsio);
8002 return (1);
8003 }
8004
8005 if (type>8 || type==2 || type==4 || type==0) {
8006 mtx_unlock(&lun->lun_lock);
8007 ctl_set_invalid_field(/*ctsio*/ ctsio,
8008 /*sks_valid*/ 1,
8009 /*command*/ 1,
8010 /*field*/ 2,
8011 /*bit_valid*/ 1,
8012 /*bit*/ 0);
8013 ctl_done((union ctl_io *)ctsio);
8014 return (1);
8015 }
8016
8017 /*
8018 * Do the following:
8019 * if sa_res_key != res_key remove all
8020 * registrants w/sa_res_key and generate UA
8021 * for these registrants(Registrations
8022 * Preempted) if it wasn't an exclusive
8023 * reservation generate UA(Reservations
8024 * Preempted) for all other registered nexuses
8025 * if the type has changed. Establish the new
8026 * reservation and holder. If res_key and
8027 * sa_res_key are the same do the above
8028 * except don't unregister the res holder.
8029 */
8030
8031 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8032 if (i == residx || ctl_get_prkey(lun, i) == 0)
8033 continue;
8034
8035 if (sa_res_key == ctl_get_prkey(lun, i)) {
8036 ctl_clr_prkey(lun, i);
8037 lun->pr_key_count--;
8038 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8039 } else if (type != lun->pr_res_type &&
8040 (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8041 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8042 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8043 }
8044 }
8045 lun->pr_res_type = type;
8046 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8047 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8048 lun->pr_res_idx = residx;
8049 else
8050 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8051 lun->pr_generation++;
8052 mtx_unlock(&lun->lun_lock);
8053
8054 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8055 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8056 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8057 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8058 persis_io.pr.pr_info.res_type = type;
8059 memcpy(persis_io.pr.pr_info.sa_res_key,
8060 param->serv_act_res_key,
8061 sizeof(param->serv_act_res_key));
8062 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8063 sizeof(persis_io.pr), M_WAITOK);
8064 } else {
8065 /*
8066 * sa_res_key is not the res holder just
8067 * remove registrants
8068 */
8069 int found=0;
8070
8071 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8072 if (sa_res_key != ctl_get_prkey(lun, i))
8073 continue;
8074
8075 found = 1;
8076 ctl_clr_prkey(lun, i);
8077 lun->pr_key_count--;
8078 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8079 }
8080
8081 if (!found) {
8082 mtx_unlock(&lun->lun_lock);
8083 free(ctsio->kern_data_ptr, M_CTL);
8084 ctl_set_reservation_conflict(ctsio);
8085 ctl_done((union ctl_io *)ctsio);
8086 return (1);
8087 }
8088 lun->pr_generation++;
8089 mtx_unlock(&lun->lun_lock);
8090
8091 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8092 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8093 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8094 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8095 persis_io.pr.pr_info.res_type = type;
8096 memcpy(persis_io.pr.pr_info.sa_res_key,
8097 param->serv_act_res_key,
8098 sizeof(param->serv_act_res_key));
8099 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8100 sizeof(persis_io.pr), M_WAITOK);
8101 }
8102 }
8103 return (0);
8104 }
8105
8106 static void
ctl_pro_preempt_other(struct ctl_lun * lun,union ctl_ha_msg * msg)8107 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8108 {
8109 uint64_t sa_res_key;
8110 int i;
8111
8112 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8113
8114 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8115 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8116 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8117 if (sa_res_key == 0) {
8118 /*
8119 * Unregister everybody else and build UA for
8120 * them
8121 */
8122 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8123 if (i == msg->pr.pr_info.residx ||
8124 ctl_get_prkey(lun, i) == 0)
8125 continue;
8126
8127 ctl_clr_prkey(lun, i);
8128 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8129 }
8130
8131 lun->pr_key_count = 1;
8132 lun->pr_res_type = msg->pr.pr_info.res_type;
8133 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8134 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8135 lun->pr_res_idx = msg->pr.pr_info.residx;
8136 } else {
8137 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8138 if (sa_res_key == ctl_get_prkey(lun, i))
8139 continue;
8140
8141 ctl_clr_prkey(lun, i);
8142 lun->pr_key_count--;
8143 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8144 }
8145 }
8146 } else {
8147 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8148 if (i == msg->pr.pr_info.residx ||
8149 ctl_get_prkey(lun, i) == 0)
8150 continue;
8151
8152 if (sa_res_key == ctl_get_prkey(lun, i)) {
8153 ctl_clr_prkey(lun, i);
8154 lun->pr_key_count--;
8155 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8156 } else if (msg->pr.pr_info.res_type != lun->pr_res_type
8157 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8158 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8159 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8160 }
8161 }
8162 lun->pr_res_type = msg->pr.pr_info.res_type;
8163 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8164 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8165 lun->pr_res_idx = msg->pr.pr_info.residx;
8166 else
8167 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8168 }
8169 lun->pr_generation++;
8170
8171 }
8172
8173
8174 int
ctl_persistent_reserve_out(struct ctl_scsiio * ctsio)8175 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8176 {
8177 struct ctl_softc *softc = CTL_SOFTC(ctsio);
8178 struct ctl_lun *lun = CTL_LUN(ctsio);
8179 int retval;
8180 u_int32_t param_len;
8181 struct scsi_per_res_out *cdb;
8182 struct scsi_per_res_out_parms* param;
8183 uint32_t residx;
8184 uint64_t res_key, sa_res_key, key;
8185 uint8_t type;
8186 union ctl_ha_msg persis_io;
8187 int i;
8188
8189 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8190
8191 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8192 retval = CTL_RETVAL_COMPLETE;
8193
8194 /*
8195 * We only support whole-LUN scope. The scope & type are ignored for
8196 * register, register and ignore existing key and clear.
8197 * We sometimes ignore scope and type on preempts too!!
8198 * Verify reservation type here as well.
8199 */
8200 type = cdb->scope_type & SPR_TYPE_MASK;
8201 if ((cdb->action == SPRO_RESERVE)
8202 || (cdb->action == SPRO_RELEASE)) {
8203 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8204 ctl_set_invalid_field(/*ctsio*/ ctsio,
8205 /*sks_valid*/ 1,
8206 /*command*/ 1,
8207 /*field*/ 2,
8208 /*bit_valid*/ 1,
8209 /*bit*/ 4);
8210 ctl_done((union ctl_io *)ctsio);
8211 return (CTL_RETVAL_COMPLETE);
8212 }
8213
8214 if (type>8 || type==2 || type==4 || type==0) {
8215 ctl_set_invalid_field(/*ctsio*/ ctsio,
8216 /*sks_valid*/ 1,
8217 /*command*/ 1,
8218 /*field*/ 2,
8219 /*bit_valid*/ 1,
8220 /*bit*/ 0);
8221 ctl_done((union ctl_io *)ctsio);
8222 return (CTL_RETVAL_COMPLETE);
8223 }
8224 }
8225
8226 param_len = scsi_4btoul(cdb->length);
8227
8228 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8229 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8230 ctsio->kern_data_len = param_len;
8231 ctsio->kern_total_len = param_len;
8232 ctsio->kern_rel_offset = 0;
8233 ctsio->kern_sg_entries = 0;
8234 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8235 ctsio->be_move_done = ctl_config_move_done;
8236 ctl_datamove((union ctl_io *)ctsio);
8237
8238 return (CTL_RETVAL_COMPLETE);
8239 }
8240
8241 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8242
8243 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8244 res_key = scsi_8btou64(param->res_key.key);
8245 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8246
8247 /*
8248 * Validate the reservation key here except for SPRO_REG_IGNO
8249 * This must be done for all other service actions
8250 */
8251 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8252 mtx_lock(&lun->lun_lock);
8253 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8254 if (res_key != key) {
8255 /*
8256 * The current key passed in doesn't match
8257 * the one the initiator previously
8258 * registered.
8259 */
8260 mtx_unlock(&lun->lun_lock);
8261 free(ctsio->kern_data_ptr, M_CTL);
8262 ctl_set_reservation_conflict(ctsio);
8263 ctl_done((union ctl_io *)ctsio);
8264 return (CTL_RETVAL_COMPLETE);
8265 }
8266 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8267 /*
8268 * We are not registered
8269 */
8270 mtx_unlock(&lun->lun_lock);
8271 free(ctsio->kern_data_ptr, M_CTL);
8272 ctl_set_reservation_conflict(ctsio);
8273 ctl_done((union ctl_io *)ctsio);
8274 return (CTL_RETVAL_COMPLETE);
8275 } else if (res_key != 0) {
8276 /*
8277 * We are not registered and trying to register but
8278 * the register key isn't zero.
8279 */
8280 mtx_unlock(&lun->lun_lock);
8281 free(ctsio->kern_data_ptr, M_CTL);
8282 ctl_set_reservation_conflict(ctsio);
8283 ctl_done((union ctl_io *)ctsio);
8284 return (CTL_RETVAL_COMPLETE);
8285 }
8286 mtx_unlock(&lun->lun_lock);
8287 }
8288
8289 switch (cdb->action & SPRO_ACTION_MASK) {
8290 case SPRO_REGISTER:
8291 case SPRO_REG_IGNO: {
8292
8293 /*
8294 * We don't support any of these options, as we report in
8295 * the read capabilities request (see
8296 * ctl_persistent_reserve_in(), above).
8297 */
8298 if ((param->flags & SPR_SPEC_I_PT)
8299 || (param->flags & SPR_ALL_TG_PT)
8300 || (param->flags & SPR_APTPL)) {
8301 int bit_ptr;
8302
8303 if (param->flags & SPR_APTPL)
8304 bit_ptr = 0;
8305 else if (param->flags & SPR_ALL_TG_PT)
8306 bit_ptr = 2;
8307 else /* SPR_SPEC_I_PT */
8308 bit_ptr = 3;
8309
8310 free(ctsio->kern_data_ptr, M_CTL);
8311 ctl_set_invalid_field(ctsio,
8312 /*sks_valid*/ 1,
8313 /*command*/ 0,
8314 /*field*/ 20,
8315 /*bit_valid*/ 1,
8316 /*bit*/ bit_ptr);
8317 ctl_done((union ctl_io *)ctsio);
8318 return (CTL_RETVAL_COMPLETE);
8319 }
8320
8321 mtx_lock(&lun->lun_lock);
8322
8323 /*
8324 * The initiator wants to clear the
8325 * key/unregister.
8326 */
8327 if (sa_res_key == 0) {
8328 if ((res_key == 0
8329 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8330 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8331 && ctl_get_prkey(lun, residx) == 0)) {
8332 mtx_unlock(&lun->lun_lock);
8333 goto done;
8334 }
8335
8336 ctl_clr_prkey(lun, residx);
8337 lun->pr_key_count--;
8338
8339 if (residx == lun->pr_res_idx) {
8340 lun->flags &= ~CTL_LUN_PR_RESERVED;
8341 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8342
8343 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8344 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8345 lun->pr_key_count) {
8346 /*
8347 * If the reservation is a registrants
8348 * only type we need to generate a UA
8349 * for other registered inits. The
8350 * sense code should be RESERVATIONS
8351 * RELEASED
8352 */
8353
8354 for (i = softc->init_min; i < softc->init_max; i++){
8355 if (ctl_get_prkey(lun, i) == 0)
8356 continue;
8357 ctl_est_ua(lun, i,
8358 CTL_UA_RES_RELEASE);
8359 }
8360 }
8361 lun->pr_res_type = 0;
8362 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8363 if (lun->pr_key_count==0) {
8364 lun->flags &= ~CTL_LUN_PR_RESERVED;
8365 lun->pr_res_type = 0;
8366 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8367 }
8368 }
8369 lun->pr_generation++;
8370 mtx_unlock(&lun->lun_lock);
8371
8372 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8373 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8374 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8375 persis_io.pr.pr_info.residx = residx;
8376 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8377 sizeof(persis_io.pr), M_WAITOK);
8378 } else /* sa_res_key != 0 */ {
8379
8380 /*
8381 * If we aren't registered currently then increment
8382 * the key count and set the registered flag.
8383 */
8384 ctl_alloc_prkey(lun, residx);
8385 if (ctl_get_prkey(lun, residx) == 0)
8386 lun->pr_key_count++;
8387 ctl_set_prkey(lun, residx, sa_res_key);
8388 lun->pr_generation++;
8389 mtx_unlock(&lun->lun_lock);
8390
8391 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8392 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8393 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8394 persis_io.pr.pr_info.residx = residx;
8395 memcpy(persis_io.pr.pr_info.sa_res_key,
8396 param->serv_act_res_key,
8397 sizeof(param->serv_act_res_key));
8398 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8399 sizeof(persis_io.pr), M_WAITOK);
8400 }
8401
8402 break;
8403 }
8404 case SPRO_RESERVE:
8405 mtx_lock(&lun->lun_lock);
8406 if (lun->flags & CTL_LUN_PR_RESERVED) {
8407 /*
8408 * if this isn't the reservation holder and it's
8409 * not a "all registrants" type or if the type is
8410 * different then we have a conflict
8411 */
8412 if ((lun->pr_res_idx != residx
8413 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8414 || lun->pr_res_type != type) {
8415 mtx_unlock(&lun->lun_lock);
8416 free(ctsio->kern_data_ptr, M_CTL);
8417 ctl_set_reservation_conflict(ctsio);
8418 ctl_done((union ctl_io *)ctsio);
8419 return (CTL_RETVAL_COMPLETE);
8420 }
8421 mtx_unlock(&lun->lun_lock);
8422 } else /* create a reservation */ {
8423 /*
8424 * If it's not an "all registrants" type record
8425 * reservation holder
8426 */
8427 if (type != SPR_TYPE_WR_EX_AR
8428 && type != SPR_TYPE_EX_AC_AR)
8429 lun->pr_res_idx = residx; /* Res holder */
8430 else
8431 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8432
8433 lun->flags |= CTL_LUN_PR_RESERVED;
8434 lun->pr_res_type = type;
8435
8436 mtx_unlock(&lun->lun_lock);
8437
8438 /* send msg to other side */
8439 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8440 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8441 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8442 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8443 persis_io.pr.pr_info.res_type = type;
8444 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8445 sizeof(persis_io.pr), M_WAITOK);
8446 }
8447 break;
8448
8449 case SPRO_RELEASE:
8450 mtx_lock(&lun->lun_lock);
8451 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8452 /* No reservation exists return good status */
8453 mtx_unlock(&lun->lun_lock);
8454 goto done;
8455 }
8456 /*
8457 * Is this nexus a reservation holder?
8458 */
8459 if (lun->pr_res_idx != residx
8460 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8461 /*
8462 * not a res holder return good status but
8463 * do nothing
8464 */
8465 mtx_unlock(&lun->lun_lock);
8466 goto done;
8467 }
8468
8469 if (lun->pr_res_type != type) {
8470 mtx_unlock(&lun->lun_lock);
8471 free(ctsio->kern_data_ptr, M_CTL);
8472 ctl_set_illegal_pr_release(ctsio);
8473 ctl_done((union ctl_io *)ctsio);
8474 return (CTL_RETVAL_COMPLETE);
8475 }
8476
8477 /* okay to release */
8478 lun->flags &= ~CTL_LUN_PR_RESERVED;
8479 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8480 lun->pr_res_type = 0;
8481
8482 /*
8483 * If this isn't an exclusive access reservation and NUAR
8484 * is not set, generate UA for all other registrants.
8485 */
8486 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX &&
8487 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8488 for (i = softc->init_min; i < softc->init_max; i++) {
8489 if (i == residx || ctl_get_prkey(lun, i) == 0)
8490 continue;
8491 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8492 }
8493 }
8494 mtx_unlock(&lun->lun_lock);
8495
8496 /* Send msg to other side */
8497 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8498 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8499 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8500 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8501 sizeof(persis_io.pr), M_WAITOK);
8502 break;
8503
8504 case SPRO_CLEAR:
8505 /* send msg to other side */
8506
8507 mtx_lock(&lun->lun_lock);
8508 lun->flags &= ~CTL_LUN_PR_RESERVED;
8509 lun->pr_res_type = 0;
8510 lun->pr_key_count = 0;
8511 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8512
8513 ctl_clr_prkey(lun, residx);
8514 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8515 if (ctl_get_prkey(lun, i) != 0) {
8516 ctl_clr_prkey(lun, i);
8517 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8518 }
8519 lun->pr_generation++;
8520 mtx_unlock(&lun->lun_lock);
8521
8522 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8523 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8524 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8525 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8526 sizeof(persis_io.pr), M_WAITOK);
8527 break;
8528
8529 case SPRO_PREEMPT:
8530 case SPRO_PRE_ABO: {
8531 int nretval;
8532
8533 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8534 residx, ctsio, cdb, param);
8535 if (nretval != 0)
8536 return (CTL_RETVAL_COMPLETE);
8537 break;
8538 }
8539 default:
8540 panic("%s: Invalid PR type %#x", __func__, cdb->action);
8541 }
8542
8543 done:
8544 free(ctsio->kern_data_ptr, M_CTL);
8545 ctl_set_success(ctsio);
8546 ctl_done((union ctl_io *)ctsio);
8547
8548 return (retval);
8549 }
8550
8551 /*
8552 * This routine is for handling a message from the other SC pertaining to
8553 * persistent reserve out. All the error checking will have been done
8554 * so only perorming the action need be done here to keep the two
8555 * in sync.
8556 */
8557 static void
ctl_hndl_per_res_out_on_other_sc(union ctl_io * io)8558 ctl_hndl_per_res_out_on_other_sc(union ctl_io *io)
8559 {
8560 struct ctl_softc *softc = CTL_SOFTC(io);
8561 union ctl_ha_msg *msg = (union ctl_ha_msg *)&io->presio.pr_msg;
8562 struct ctl_lun *lun;
8563 int i;
8564 uint32_t residx, targ_lun;
8565
8566 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8567 mtx_lock(&softc->ctl_lock);
8568 if (targ_lun >= ctl_max_luns ||
8569 (lun = softc->ctl_luns[targ_lun]) == NULL) {
8570 mtx_unlock(&softc->ctl_lock);
8571 return;
8572 }
8573 mtx_lock(&lun->lun_lock);
8574 mtx_unlock(&softc->ctl_lock);
8575 if (lun->flags & CTL_LUN_DISABLED) {
8576 mtx_unlock(&lun->lun_lock);
8577 return;
8578 }
8579 residx = ctl_get_initindex(&msg->hdr.nexus);
8580 switch(msg->pr.pr_info.action) {
8581 case CTL_PR_REG_KEY:
8582 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8583 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8584 lun->pr_key_count++;
8585 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8586 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8587 lun->pr_generation++;
8588 break;
8589
8590 case CTL_PR_UNREG_KEY:
8591 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8592 lun->pr_key_count--;
8593
8594 /* XXX Need to see if the reservation has been released */
8595 /* if so do we need to generate UA? */
8596 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8597 lun->flags &= ~CTL_LUN_PR_RESERVED;
8598 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8599
8600 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8601 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8602 lun->pr_key_count) {
8603 /*
8604 * If the reservation is a registrants
8605 * only type we need to generate a UA
8606 * for other registered inits. The
8607 * sense code should be RESERVATIONS
8608 * RELEASED
8609 */
8610
8611 for (i = softc->init_min; i < softc->init_max; i++) {
8612 if (ctl_get_prkey(lun, i) == 0)
8613 continue;
8614
8615 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8616 }
8617 }
8618 lun->pr_res_type = 0;
8619 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8620 if (lun->pr_key_count==0) {
8621 lun->flags &= ~CTL_LUN_PR_RESERVED;
8622 lun->pr_res_type = 0;
8623 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8624 }
8625 }
8626 lun->pr_generation++;
8627 break;
8628
8629 case CTL_PR_RESERVE:
8630 lun->flags |= CTL_LUN_PR_RESERVED;
8631 lun->pr_res_type = msg->pr.pr_info.res_type;
8632 lun->pr_res_idx = msg->pr.pr_info.residx;
8633
8634 break;
8635
8636 case CTL_PR_RELEASE:
8637 /*
8638 * If this isn't an exclusive access reservation and NUAR
8639 * is not set, generate UA for all other registrants.
8640 */
8641 if (lun->pr_res_type != SPR_TYPE_EX_AC &&
8642 lun->pr_res_type != SPR_TYPE_WR_EX &&
8643 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8644 for (i = softc->init_min; i < softc->init_max; i++) {
8645 if (i == residx || ctl_get_prkey(lun, i) == 0)
8646 continue;
8647 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8648 }
8649 }
8650
8651 lun->flags &= ~CTL_LUN_PR_RESERVED;
8652 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8653 lun->pr_res_type = 0;
8654 break;
8655
8656 case CTL_PR_PREEMPT:
8657 ctl_pro_preempt_other(lun, msg);
8658 break;
8659 case CTL_PR_CLEAR:
8660 lun->flags &= ~CTL_LUN_PR_RESERVED;
8661 lun->pr_res_type = 0;
8662 lun->pr_key_count = 0;
8663 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8664
8665 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8666 if (ctl_get_prkey(lun, i) == 0)
8667 continue;
8668 ctl_clr_prkey(lun, i);
8669 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8670 }
8671 lun->pr_generation++;
8672 break;
8673 }
8674
8675 mtx_unlock(&lun->lun_lock);
8676 }
8677
8678 int
ctl_read_write(struct ctl_scsiio * ctsio)8679 ctl_read_write(struct ctl_scsiio *ctsio)
8680 {
8681 struct ctl_lun *lun = CTL_LUN(ctsio);
8682 struct ctl_lba_len_flags *lbalen;
8683 uint64_t lba;
8684 uint32_t num_blocks;
8685 int flags, retval;
8686 int isread;
8687
8688 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8689
8690 flags = 0;
8691 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8692 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8693 switch (ctsio->cdb[0]) {
8694 case READ_6:
8695 case WRITE_6: {
8696 struct scsi_rw_6 *cdb;
8697
8698 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8699
8700 lba = scsi_3btoul(cdb->addr);
8701 /* only 5 bits are valid in the most significant address byte */
8702 lba &= 0x1fffff;
8703 num_blocks = cdb->length;
8704 /*
8705 * This is correct according to SBC-2.
8706 */
8707 if (num_blocks == 0)
8708 num_blocks = 256;
8709 break;
8710 }
8711 case READ_10:
8712 case WRITE_10: {
8713 struct scsi_rw_10 *cdb;
8714
8715 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8716 if (cdb->byte2 & SRW10_FUA)
8717 flags |= CTL_LLF_FUA;
8718 if (cdb->byte2 & SRW10_DPO)
8719 flags |= CTL_LLF_DPO;
8720 lba = scsi_4btoul(cdb->addr);
8721 num_blocks = scsi_2btoul(cdb->length);
8722 break;
8723 }
8724 case WRITE_VERIFY_10: {
8725 struct scsi_write_verify_10 *cdb;
8726
8727 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8728 flags |= CTL_LLF_FUA;
8729 if (cdb->byte2 & SWV_DPO)
8730 flags |= CTL_LLF_DPO;
8731 lba = scsi_4btoul(cdb->addr);
8732 num_blocks = scsi_2btoul(cdb->length);
8733 break;
8734 }
8735 case READ_12:
8736 case WRITE_12: {
8737 struct scsi_rw_12 *cdb;
8738
8739 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8740 if (cdb->byte2 & SRW12_FUA)
8741 flags |= CTL_LLF_FUA;
8742 if (cdb->byte2 & SRW12_DPO)
8743 flags |= CTL_LLF_DPO;
8744 lba = scsi_4btoul(cdb->addr);
8745 num_blocks = scsi_4btoul(cdb->length);
8746 break;
8747 }
8748 case WRITE_VERIFY_12: {
8749 struct scsi_write_verify_12 *cdb;
8750
8751 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8752 flags |= CTL_LLF_FUA;
8753 if (cdb->byte2 & SWV_DPO)
8754 flags |= CTL_LLF_DPO;
8755 lba = scsi_4btoul(cdb->addr);
8756 num_blocks = scsi_4btoul(cdb->length);
8757 break;
8758 }
8759 case READ_16:
8760 case WRITE_16: {
8761 struct scsi_rw_16 *cdb;
8762
8763 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8764 if (cdb->byte2 & SRW12_FUA)
8765 flags |= CTL_LLF_FUA;
8766 if (cdb->byte2 & SRW12_DPO)
8767 flags |= CTL_LLF_DPO;
8768 lba = scsi_8btou64(cdb->addr);
8769 num_blocks = scsi_4btoul(cdb->length);
8770 break;
8771 }
8772 case WRITE_ATOMIC_16: {
8773 struct scsi_write_atomic_16 *cdb;
8774
8775 if (lun->be_lun->atomicblock == 0) {
8776 ctl_set_invalid_opcode(ctsio);
8777 ctl_done((union ctl_io *)ctsio);
8778 return (CTL_RETVAL_COMPLETE);
8779 }
8780
8781 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb;
8782 if (cdb->byte2 & SRW12_FUA)
8783 flags |= CTL_LLF_FUA;
8784 if (cdb->byte2 & SRW12_DPO)
8785 flags |= CTL_LLF_DPO;
8786 lba = scsi_8btou64(cdb->addr);
8787 num_blocks = scsi_2btoul(cdb->length);
8788 if (num_blocks > lun->be_lun->atomicblock) {
8789 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8790 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8791 /*bit*/ 0);
8792 ctl_done((union ctl_io *)ctsio);
8793 return (CTL_RETVAL_COMPLETE);
8794 }
8795 break;
8796 }
8797 case WRITE_VERIFY_16: {
8798 struct scsi_write_verify_16 *cdb;
8799
8800 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8801 flags |= CTL_LLF_FUA;
8802 if (cdb->byte2 & SWV_DPO)
8803 flags |= CTL_LLF_DPO;
8804 lba = scsi_8btou64(cdb->addr);
8805 num_blocks = scsi_4btoul(cdb->length);
8806 break;
8807 }
8808 default:
8809 /*
8810 * We got a command we don't support. This shouldn't
8811 * happen, commands should be filtered out above us.
8812 */
8813 ctl_set_invalid_opcode(ctsio);
8814 ctl_done((union ctl_io *)ctsio);
8815
8816 return (CTL_RETVAL_COMPLETE);
8817 break; /* NOTREACHED */
8818 }
8819
8820 /*
8821 * The first check is to make sure we're in bounds, the second
8822 * check is to catch wrap-around problems. If the lba + num blocks
8823 * is less than the lba, then we've wrapped around and the block
8824 * range is invalid anyway.
8825 */
8826 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8827 || ((lba + num_blocks) < lba)) {
8828 ctl_set_lba_out_of_range(ctsio,
8829 MAX(lba, lun->be_lun->maxlba + 1));
8830 ctl_done((union ctl_io *)ctsio);
8831 return (CTL_RETVAL_COMPLETE);
8832 }
8833
8834 /*
8835 * According to SBC-3, a transfer length of 0 is not an error.
8836 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8837 * translates to 256 blocks for those commands.
8838 */
8839 if (num_blocks == 0) {
8840 ctl_set_success(ctsio);
8841 ctl_done((union ctl_io *)ctsio);
8842 return (CTL_RETVAL_COMPLETE);
8843 }
8844
8845 /* Set FUA and/or DPO if caches are disabled. */
8846 if (isread) {
8847 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0)
8848 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8849 } else {
8850 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8851 flags |= CTL_LLF_FUA;
8852 }
8853
8854 lbalen = (struct ctl_lba_len_flags *)
8855 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8856 lbalen->lba = lba;
8857 lbalen->len = num_blocks;
8858 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8859
8860 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8861 ctsio->kern_rel_offset = 0;
8862
8863 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8864
8865 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8866 return (retval);
8867 }
8868
8869 static int
ctl_cnw_cont(union ctl_io * io)8870 ctl_cnw_cont(union ctl_io *io)
8871 {
8872 struct ctl_lun *lun = CTL_LUN(io);
8873 struct ctl_scsiio *ctsio;
8874 struct ctl_lba_len_flags *lbalen;
8875 int retval;
8876
8877 ctsio = &io->scsiio;
8878 ctsio->io_hdr.status = CTL_STATUS_NONE;
8879 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8880 lbalen = (struct ctl_lba_len_flags *)
8881 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8882 lbalen->flags &= ~CTL_LLF_COMPARE;
8883 lbalen->flags |= CTL_LLF_WRITE;
8884
8885 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8886 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8887 return (retval);
8888 }
8889
8890 int
ctl_cnw(struct ctl_scsiio * ctsio)8891 ctl_cnw(struct ctl_scsiio *ctsio)
8892 {
8893 struct ctl_lun *lun = CTL_LUN(ctsio);
8894 struct ctl_lba_len_flags *lbalen;
8895 uint64_t lba;
8896 uint32_t num_blocks;
8897 int flags, retval;
8898
8899 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8900
8901 flags = 0;
8902 switch (ctsio->cdb[0]) {
8903 case COMPARE_AND_WRITE: {
8904 struct scsi_compare_and_write *cdb;
8905
8906 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8907 if (cdb->byte2 & SRW10_FUA)
8908 flags |= CTL_LLF_FUA;
8909 if (cdb->byte2 & SRW10_DPO)
8910 flags |= CTL_LLF_DPO;
8911 lba = scsi_8btou64(cdb->addr);
8912 num_blocks = cdb->length;
8913 break;
8914 }
8915 default:
8916 /*
8917 * We got a command we don't support. This shouldn't
8918 * happen, commands should be filtered out above us.
8919 */
8920 ctl_set_invalid_opcode(ctsio);
8921 ctl_done((union ctl_io *)ctsio);
8922
8923 return (CTL_RETVAL_COMPLETE);
8924 break; /* NOTREACHED */
8925 }
8926
8927 /*
8928 * The first check is to make sure we're in bounds, the second
8929 * check is to catch wrap-around problems. If the lba + num blocks
8930 * is less than the lba, then we've wrapped around and the block
8931 * range is invalid anyway.
8932 */
8933 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8934 || ((lba + num_blocks) < lba)) {
8935 ctl_set_lba_out_of_range(ctsio,
8936 MAX(lba, lun->be_lun->maxlba + 1));
8937 ctl_done((union ctl_io *)ctsio);
8938 return (CTL_RETVAL_COMPLETE);
8939 }
8940
8941 /*
8942 * According to SBC-3, a transfer length of 0 is not an error.
8943 */
8944 if (num_blocks == 0) {
8945 ctl_set_success(ctsio);
8946 ctl_done((union ctl_io *)ctsio);
8947 return (CTL_RETVAL_COMPLETE);
8948 }
8949
8950 /* Set FUA if write cache is disabled. */
8951 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8952 flags |= CTL_LLF_FUA;
8953
8954 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8955 ctsio->kern_rel_offset = 0;
8956
8957 /*
8958 * Set the IO_CONT flag, so that if this I/O gets passed to
8959 * ctl_data_submit_done(), it'll get passed back to
8960 * ctl_ctl_cnw_cont() for further processing.
8961 */
8962 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8963 ctsio->io_cont = ctl_cnw_cont;
8964
8965 lbalen = (struct ctl_lba_len_flags *)
8966 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8967 lbalen->lba = lba;
8968 lbalen->len = num_blocks;
8969 lbalen->flags = CTL_LLF_COMPARE | flags;
8970
8971 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8972 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8973 return (retval);
8974 }
8975
8976 int
ctl_verify(struct ctl_scsiio * ctsio)8977 ctl_verify(struct ctl_scsiio *ctsio)
8978 {
8979 struct ctl_lun *lun = CTL_LUN(ctsio);
8980 struct ctl_lba_len_flags *lbalen;
8981 uint64_t lba;
8982 uint32_t num_blocks;
8983 int bytchk, flags;
8984 int retval;
8985
8986 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
8987
8988 bytchk = 0;
8989 flags = CTL_LLF_FUA;
8990 switch (ctsio->cdb[0]) {
8991 case VERIFY_10: {
8992 struct scsi_verify_10 *cdb;
8993
8994 cdb = (struct scsi_verify_10 *)ctsio->cdb;
8995 if (cdb->byte2 & SVFY_BYTCHK)
8996 bytchk = 1;
8997 if (cdb->byte2 & SVFY_DPO)
8998 flags |= CTL_LLF_DPO;
8999 lba = scsi_4btoul(cdb->addr);
9000 num_blocks = scsi_2btoul(cdb->length);
9001 break;
9002 }
9003 case VERIFY_12: {
9004 struct scsi_verify_12 *cdb;
9005
9006 cdb = (struct scsi_verify_12 *)ctsio->cdb;
9007 if (cdb->byte2 & SVFY_BYTCHK)
9008 bytchk = 1;
9009 if (cdb->byte2 & SVFY_DPO)
9010 flags |= CTL_LLF_DPO;
9011 lba = scsi_4btoul(cdb->addr);
9012 num_blocks = scsi_4btoul(cdb->length);
9013 break;
9014 }
9015 case VERIFY_16: {
9016 struct scsi_rw_16 *cdb;
9017
9018 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9019 if (cdb->byte2 & SVFY_BYTCHK)
9020 bytchk = 1;
9021 if (cdb->byte2 & SVFY_DPO)
9022 flags |= CTL_LLF_DPO;
9023 lba = scsi_8btou64(cdb->addr);
9024 num_blocks = scsi_4btoul(cdb->length);
9025 break;
9026 }
9027 default:
9028 /*
9029 * We got a command we don't support. This shouldn't
9030 * happen, commands should be filtered out above us.
9031 */
9032 ctl_set_invalid_opcode(ctsio);
9033 ctl_done((union ctl_io *)ctsio);
9034 return (CTL_RETVAL_COMPLETE);
9035 }
9036
9037 /*
9038 * The first check is to make sure we're in bounds, the second
9039 * check is to catch wrap-around problems. If the lba + num blocks
9040 * is less than the lba, then we've wrapped around and the block
9041 * range is invalid anyway.
9042 */
9043 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9044 || ((lba + num_blocks) < lba)) {
9045 ctl_set_lba_out_of_range(ctsio,
9046 MAX(lba, lun->be_lun->maxlba + 1));
9047 ctl_done((union ctl_io *)ctsio);
9048 return (CTL_RETVAL_COMPLETE);
9049 }
9050
9051 /*
9052 * According to SBC-3, a transfer length of 0 is not an error.
9053 */
9054 if (num_blocks == 0) {
9055 ctl_set_success(ctsio);
9056 ctl_done((union ctl_io *)ctsio);
9057 return (CTL_RETVAL_COMPLETE);
9058 }
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 if (bytchk) {
9065 lbalen->flags = CTL_LLF_COMPARE | flags;
9066 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9067 } else {
9068 lbalen->flags = CTL_LLF_VERIFY | flags;
9069 ctsio->kern_total_len = 0;
9070 }
9071 ctsio->kern_rel_offset = 0;
9072
9073 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9074 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9075 return (retval);
9076 }
9077
9078 int
ctl_report_luns(struct ctl_scsiio * ctsio)9079 ctl_report_luns(struct ctl_scsiio *ctsio)
9080 {
9081 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9082 struct ctl_port *port = CTL_PORT(ctsio);
9083 struct ctl_lun *lun, *request_lun = CTL_LUN(ctsio);
9084 struct scsi_report_luns *cdb;
9085 struct scsi_report_luns_data *lun_data;
9086 int num_filled, num_luns, num_port_luns, retval;
9087 uint32_t alloc_len, lun_datalen;
9088 uint32_t initidx, targ_lun_id, lun_id;
9089
9090 retval = CTL_RETVAL_COMPLETE;
9091 cdb = (struct scsi_report_luns *)ctsio->cdb;
9092
9093 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9094
9095 num_luns = 0;
9096 num_port_luns = port->lun_map ? port->lun_map_size : ctl_max_luns;
9097 mtx_lock(&softc->ctl_lock);
9098 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) {
9099 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX)
9100 num_luns++;
9101 }
9102 mtx_unlock(&softc->ctl_lock);
9103
9104 switch (cdb->select_report) {
9105 case RPL_REPORT_DEFAULT:
9106 case RPL_REPORT_ALL:
9107 case RPL_REPORT_NONSUBSID:
9108 break;
9109 case RPL_REPORT_WELLKNOWN:
9110 case RPL_REPORT_ADMIN:
9111 case RPL_REPORT_CONGLOM:
9112 num_luns = 0;
9113 break;
9114 default:
9115 ctl_set_invalid_field(ctsio,
9116 /*sks_valid*/ 1,
9117 /*command*/ 1,
9118 /*field*/ 2,
9119 /*bit_valid*/ 0,
9120 /*bit*/ 0);
9121 ctl_done((union ctl_io *)ctsio);
9122 return (retval);
9123 break; /* NOTREACHED */
9124 }
9125
9126 alloc_len = scsi_4btoul(cdb->length);
9127 /*
9128 * The initiator has to allocate at least 16 bytes for this request,
9129 * so he can at least get the header and the first LUN. Otherwise
9130 * we reject the request (per SPC-3 rev 14, section 6.21).
9131 */
9132 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9133 sizeof(struct scsi_report_luns_lundata))) {
9134 ctl_set_invalid_field(ctsio,
9135 /*sks_valid*/ 1,
9136 /*command*/ 1,
9137 /*field*/ 6,
9138 /*bit_valid*/ 0,
9139 /*bit*/ 0);
9140 ctl_done((union ctl_io *)ctsio);
9141 return (retval);
9142 }
9143
9144 lun_datalen = sizeof(*lun_data) +
9145 (num_luns * sizeof(struct scsi_report_luns_lundata));
9146
9147 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9148 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9149 ctsio->kern_sg_entries = 0;
9150
9151 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9152
9153 mtx_lock(&softc->ctl_lock);
9154 for (targ_lun_id = 0, num_filled = 0;
9155 targ_lun_id < num_port_luns && num_filled < num_luns;
9156 targ_lun_id++) {
9157 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9158 if (lun_id == UINT32_MAX)
9159 continue;
9160 lun = softc->ctl_luns[lun_id];
9161 if (lun == NULL)
9162 continue;
9163
9164 be64enc(lun_data->luns[num_filled++].lundata,
9165 ctl_encode_lun(targ_lun_id));
9166
9167 /*
9168 * According to SPC-3, rev 14 section 6.21:
9169 *
9170 * "The execution of a REPORT LUNS command to any valid and
9171 * installed logical unit shall clear the REPORTED LUNS DATA
9172 * HAS CHANGED unit attention condition for all logical
9173 * units of that target with respect to the requesting
9174 * initiator. A valid and installed logical unit is one
9175 * having a PERIPHERAL QUALIFIER of 000b in the standard
9176 * INQUIRY data (see 6.4.2)."
9177 *
9178 * If request_lun is NULL, the LUN this report luns command
9179 * was issued to is either disabled or doesn't exist. In that
9180 * case, we shouldn't clear any pending lun change unit
9181 * attention.
9182 */
9183 if (request_lun != NULL) {
9184 mtx_lock(&lun->lun_lock);
9185 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9186 mtx_unlock(&lun->lun_lock);
9187 }
9188 }
9189 mtx_unlock(&softc->ctl_lock);
9190
9191 /*
9192 * It's quite possible that we've returned fewer LUNs than we allocated
9193 * space for. Trim it.
9194 */
9195 lun_datalen = sizeof(*lun_data) +
9196 (num_filled * sizeof(struct scsi_report_luns_lundata));
9197 ctsio->kern_rel_offset = 0;
9198 ctsio->kern_sg_entries = 0;
9199 ctsio->kern_data_len = min(lun_datalen, alloc_len);
9200 ctsio->kern_total_len = ctsio->kern_data_len;
9201
9202 /*
9203 * We set this to the actual data length, regardless of how much
9204 * space we actually have to return results. If the user looks at
9205 * this value, he'll know whether or not he allocated enough space
9206 * and reissue the command if necessary. We don't support well
9207 * known logical units, so if the user asks for that, return none.
9208 */
9209 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9210
9211 /*
9212 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9213 * this request.
9214 */
9215 ctl_set_success(ctsio);
9216 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9217 ctsio->be_move_done = ctl_config_move_done;
9218 ctl_datamove((union ctl_io *)ctsio);
9219 return (retval);
9220 }
9221
9222 int
ctl_request_sense(struct ctl_scsiio * ctsio)9223 ctl_request_sense(struct ctl_scsiio *ctsio)
9224 {
9225 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9226 struct ctl_lun *lun = CTL_LUN(ctsio);
9227 struct scsi_request_sense *cdb;
9228 struct scsi_sense_data *sense_ptr, *ps;
9229 uint32_t initidx;
9230 int have_error;
9231 u_int sense_len = SSD_FULL_SIZE;
9232 scsi_sense_data_type sense_format;
9233 ctl_ua_type ua_type;
9234 uint8_t asc = 0, ascq = 0;
9235
9236 cdb = (struct scsi_request_sense *)ctsio->cdb;
9237
9238 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9239
9240 /*
9241 * Determine which sense format the user wants.
9242 */
9243 if (cdb->byte2 & SRS_DESC)
9244 sense_format = SSD_TYPE_DESC;
9245 else
9246 sense_format = SSD_TYPE_FIXED;
9247
9248 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9249 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9250 ctsio->kern_sg_entries = 0;
9251 ctsio->kern_rel_offset = 0;
9252
9253 /*
9254 * struct scsi_sense_data, which is currently set to 256 bytes, is
9255 * larger than the largest allowed value for the length field in the
9256 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9257 */
9258 ctsio->kern_data_len = cdb->length;
9259 ctsio->kern_total_len = cdb->length;
9260
9261 /*
9262 * If we don't have a LUN, we don't have any pending sense.
9263 */
9264 if (lun == NULL ||
9265 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
9266 softc->ha_link < CTL_HA_LINK_UNKNOWN)) {
9267 /* "Logical unit not supported" */
9268 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format,
9269 /*current_error*/ 1,
9270 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
9271 /*asc*/ 0x25,
9272 /*ascq*/ 0x00,
9273 SSD_ELEM_NONE);
9274 goto send;
9275 }
9276
9277 have_error = 0;
9278 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9279 /*
9280 * Check for pending sense, and then for pending unit attentions.
9281 * Pending sense gets returned first, then pending unit attentions.
9282 */
9283 mtx_lock(&lun->lun_lock);
9284 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
9285 if (ps != NULL)
9286 ps += initidx % CTL_MAX_INIT_PER_PORT;
9287 if (ps != NULL && ps->error_code != 0) {
9288 scsi_sense_data_type stored_format;
9289
9290 /*
9291 * Check to see which sense format was used for the stored
9292 * sense data.
9293 */
9294 stored_format = scsi_sense_type(ps);
9295
9296 /*
9297 * If the user requested a different sense format than the
9298 * one we stored, then we need to convert it to the other
9299 * format. If we're going from descriptor to fixed format
9300 * sense data, we may lose things in translation, depending
9301 * on what options were used.
9302 *
9303 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9304 * for some reason we'll just copy it out as-is.
9305 */
9306 if ((stored_format == SSD_TYPE_FIXED)
9307 && (sense_format == SSD_TYPE_DESC))
9308 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9309 ps, (struct scsi_sense_data_desc *)sense_ptr);
9310 else if ((stored_format == SSD_TYPE_DESC)
9311 && (sense_format == SSD_TYPE_FIXED))
9312 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9313 ps, (struct scsi_sense_data_fixed *)sense_ptr);
9314 else
9315 memcpy(sense_ptr, ps, sizeof(*sense_ptr));
9316
9317 ps->error_code = 0;
9318 have_error = 1;
9319 } else {
9320 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len,
9321 sense_format);
9322 if (ua_type != CTL_UA_NONE)
9323 have_error = 1;
9324 }
9325 if (have_error == 0) {
9326 /*
9327 * Report informational exception if have one and allowed.
9328 */
9329 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) {
9330 asc = lun->ie_asc;
9331 ascq = lun->ie_ascq;
9332 }
9333 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format,
9334 /*current_error*/ 1,
9335 /*sense_key*/ SSD_KEY_NO_SENSE,
9336 /*asc*/ asc,
9337 /*ascq*/ ascq,
9338 SSD_ELEM_NONE);
9339 }
9340 mtx_unlock(&lun->lun_lock);
9341
9342 send:
9343 /*
9344 * We report the SCSI status as OK, since the status of the command
9345 * itself is OK. We're reporting sense as parameter data.
9346 */
9347 ctl_set_success(ctsio);
9348 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9349 ctsio->be_move_done = ctl_config_move_done;
9350 ctl_datamove((union ctl_io *)ctsio);
9351 return (CTL_RETVAL_COMPLETE);
9352 }
9353
9354 int
ctl_tur(struct ctl_scsiio * ctsio)9355 ctl_tur(struct ctl_scsiio *ctsio)
9356 {
9357
9358 CTL_DEBUG_PRINT(("ctl_tur\n"));
9359
9360 ctl_set_success(ctsio);
9361 ctl_done((union ctl_io *)ctsio);
9362
9363 return (CTL_RETVAL_COMPLETE);
9364 }
9365
9366 /*
9367 * SCSI VPD page 0x00, the Supported VPD Pages page.
9368 */
9369 static int
ctl_inquiry_evpd_supported(struct ctl_scsiio * ctsio,int alloc_len)9370 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9371 {
9372 struct ctl_lun *lun = CTL_LUN(ctsio);
9373 struct scsi_vpd_supported_pages *pages;
9374 int sup_page_size;
9375 int p;
9376
9377 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9378 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9379 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9380 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9381 ctsio->kern_rel_offset = 0;
9382 ctsio->kern_sg_entries = 0;
9383 ctsio->kern_data_len = min(sup_page_size, alloc_len);
9384 ctsio->kern_total_len = ctsio->kern_data_len;
9385
9386 /*
9387 * The control device is always connected. The disk device, on the
9388 * other hand, may not be online all the time. Need to change this
9389 * to figure out whether the disk device is actually online or not.
9390 */
9391 if (lun != NULL)
9392 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9393 lun->be_lun->lun_type;
9394 else
9395 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9396
9397 p = 0;
9398 /* Supported VPD pages */
9399 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9400 /* Serial Number */
9401 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9402 /* Device Identification */
9403 pages->page_list[p++] = SVPD_DEVICE_ID;
9404 /* Extended INQUIRY Data */
9405 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9406 /* Mode Page Policy */
9407 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9408 /* SCSI Ports */
9409 pages->page_list[p++] = SVPD_SCSI_PORTS;
9410 /* Third-party Copy */
9411 pages->page_list[p++] = SVPD_SCSI_TPC;
9412 /* SCSI Feature Sets */
9413 pages->page_list[p++] = SVPD_SCSI_SFS;
9414 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9415 /* Block limits */
9416 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9417 /* Block Device Characteristics */
9418 pages->page_list[p++] = SVPD_BDC;
9419 /* Logical Block Provisioning */
9420 pages->page_list[p++] = SVPD_LBP;
9421 }
9422 pages->length = p;
9423
9424 ctl_set_success(ctsio);
9425 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9426 ctsio->be_move_done = ctl_config_move_done;
9427 ctl_datamove((union ctl_io *)ctsio);
9428 return (CTL_RETVAL_COMPLETE);
9429 }
9430
9431 /*
9432 * SCSI VPD page 0x80, the Unit Serial Number page.
9433 */
9434 static int
ctl_inquiry_evpd_serial(struct ctl_scsiio * ctsio,int alloc_len)9435 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9436 {
9437 struct ctl_lun *lun = CTL_LUN(ctsio);
9438 struct scsi_vpd_unit_serial_number *sn_ptr;
9439 int data_len;
9440
9441 data_len = 4 + CTL_SN_LEN;
9442 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9443 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9444 ctsio->kern_rel_offset = 0;
9445 ctsio->kern_sg_entries = 0;
9446 ctsio->kern_data_len = min(data_len, alloc_len);
9447 ctsio->kern_total_len = ctsio->kern_data_len;
9448
9449 /*
9450 * The control device is always connected. The disk device, on the
9451 * other hand, may not be online all the time. Need to change this
9452 * to figure out whether the disk device is actually online or not.
9453 */
9454 if (lun != NULL)
9455 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9456 lun->be_lun->lun_type;
9457 else
9458 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9459
9460 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9461 sn_ptr->length = CTL_SN_LEN;
9462 /*
9463 * If we don't have a LUN, we just leave the serial number as
9464 * all spaces.
9465 */
9466 if (lun != NULL) {
9467 strncpy((char *)sn_ptr->serial_num,
9468 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9469 } else
9470 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9471
9472 ctl_set_success(ctsio);
9473 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9474 ctsio->be_move_done = ctl_config_move_done;
9475 ctl_datamove((union ctl_io *)ctsio);
9476 return (CTL_RETVAL_COMPLETE);
9477 }
9478
9479
9480 /*
9481 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9482 */
9483 static int
ctl_inquiry_evpd_eid(struct ctl_scsiio * ctsio,int alloc_len)9484 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9485 {
9486 struct ctl_lun *lun = CTL_LUN(ctsio);
9487 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9488 int data_len;
9489
9490 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9491 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9492 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9493 ctsio->kern_sg_entries = 0;
9494 ctsio->kern_rel_offset = 0;
9495 ctsio->kern_data_len = min(data_len, alloc_len);
9496 ctsio->kern_total_len = ctsio->kern_data_len;
9497
9498 /*
9499 * The control device is always connected. The disk device, on the
9500 * other hand, may not be online all the time.
9501 */
9502 if (lun != NULL)
9503 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9504 lun->be_lun->lun_type;
9505 else
9506 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9507 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9508 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9509 /*
9510 * We support head of queue, ordered and simple tags.
9511 */
9512 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9513 /*
9514 * Volatile cache supported.
9515 */
9516 eid_ptr->flags3 = SVPD_EID_V_SUP;
9517
9518 /*
9519 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9520 * attention for a particular IT nexus on all LUNs once we report
9521 * it to that nexus once. This bit is required as of SPC-4.
9522 */
9523 eid_ptr->flags4 = SVPD_EID_LUICLR;
9524
9525 /*
9526 * We support revert to defaults (RTD) bit in MODE SELECT.
9527 */
9528 eid_ptr->flags5 = SVPD_EID_RTD_SUP;
9529
9530 /*
9531 * XXX KDM in order to correctly answer this, we would need
9532 * information from the SIM to determine how much sense data it
9533 * can send. So this would really be a path inquiry field, most
9534 * likely. This can be set to a maximum of 252 according to SPC-4,
9535 * but the hardware may or may not be able to support that much.
9536 * 0 just means that the maximum sense data length is not reported.
9537 */
9538 eid_ptr->max_sense_length = 0;
9539
9540 ctl_set_success(ctsio);
9541 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9542 ctsio->be_move_done = ctl_config_move_done;
9543 ctl_datamove((union ctl_io *)ctsio);
9544 return (CTL_RETVAL_COMPLETE);
9545 }
9546
9547 static int
ctl_inquiry_evpd_mpp(struct ctl_scsiio * ctsio,int alloc_len)9548 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9549 {
9550 struct ctl_lun *lun = CTL_LUN(ctsio);
9551 struct scsi_vpd_mode_page_policy *mpp_ptr;
9552 int data_len;
9553
9554 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9555 sizeof(struct scsi_vpd_mode_page_policy_descr);
9556
9557 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9558 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9559 ctsio->kern_rel_offset = 0;
9560 ctsio->kern_sg_entries = 0;
9561 ctsio->kern_data_len = min(data_len, alloc_len);
9562 ctsio->kern_total_len = ctsio->kern_data_len;
9563
9564 /*
9565 * The control device is always connected. The disk device, on the
9566 * other hand, may not be online all the time.
9567 */
9568 if (lun != NULL)
9569 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9570 lun->be_lun->lun_type;
9571 else
9572 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9573 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9574 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9575 mpp_ptr->descr[0].page_code = 0x3f;
9576 mpp_ptr->descr[0].subpage_code = 0xff;
9577 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9578
9579 ctl_set_success(ctsio);
9580 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9581 ctsio->be_move_done = ctl_config_move_done;
9582 ctl_datamove((union ctl_io *)ctsio);
9583 return (CTL_RETVAL_COMPLETE);
9584 }
9585
9586 /*
9587 * SCSI VPD page 0x83, the Device Identification page.
9588 */
9589 static int
ctl_inquiry_evpd_devid(struct ctl_scsiio * ctsio,int alloc_len)9590 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9591 {
9592 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9593 struct ctl_port *port = CTL_PORT(ctsio);
9594 struct ctl_lun *lun = CTL_LUN(ctsio);
9595 struct scsi_vpd_device_id *devid_ptr;
9596 struct scsi_vpd_id_descriptor *desc;
9597 int data_len, g;
9598 uint8_t proto;
9599
9600 data_len = sizeof(struct scsi_vpd_device_id) +
9601 sizeof(struct scsi_vpd_id_descriptor) +
9602 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9603 sizeof(struct scsi_vpd_id_descriptor) +
9604 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9605 if (lun && lun->lun_devid)
9606 data_len += lun->lun_devid->len;
9607 if (port && port->port_devid)
9608 data_len += port->port_devid->len;
9609 if (port && port->target_devid)
9610 data_len += port->target_devid->len;
9611
9612 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9613 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9614 ctsio->kern_sg_entries = 0;
9615 ctsio->kern_rel_offset = 0;
9616 ctsio->kern_sg_entries = 0;
9617 ctsio->kern_data_len = min(data_len, alloc_len);
9618 ctsio->kern_total_len = ctsio->kern_data_len;
9619
9620 /*
9621 * The control device is always connected. The disk device, on the
9622 * other hand, may not be online all the time.
9623 */
9624 if (lun != NULL)
9625 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9626 lun->be_lun->lun_type;
9627 else
9628 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9629 devid_ptr->page_code = SVPD_DEVICE_ID;
9630 scsi_ulto2b(data_len - 4, devid_ptr->length);
9631
9632 if (port && port->port_type == CTL_PORT_FC)
9633 proto = SCSI_PROTO_FC << 4;
9634 else if (port && port->port_type == CTL_PORT_SAS)
9635 proto = SCSI_PROTO_SAS << 4;
9636 else if (port && port->port_type == CTL_PORT_ISCSI)
9637 proto = SCSI_PROTO_ISCSI << 4;
9638 else
9639 proto = SCSI_PROTO_SPI << 4;
9640 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9641
9642 /*
9643 * We're using a LUN association here. i.e., this device ID is a
9644 * per-LUN identifier.
9645 */
9646 if (lun && lun->lun_devid) {
9647 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9648 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9649 lun->lun_devid->len);
9650 }
9651
9652 /*
9653 * This is for the WWPN which is a port association.
9654 */
9655 if (port && port->port_devid) {
9656 memcpy(desc, port->port_devid->data, port->port_devid->len);
9657 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9658 port->port_devid->len);
9659 }
9660
9661 /*
9662 * This is for the Relative Target Port(type 4h) identifier
9663 */
9664 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9665 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9666 SVPD_ID_TYPE_RELTARG;
9667 desc->length = 4;
9668 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9669 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9670 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9671
9672 /*
9673 * This is for the Target Port Group(type 5h) identifier
9674 */
9675 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9676 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9677 SVPD_ID_TYPE_TPORTGRP;
9678 desc->length = 4;
9679 if (softc->is_single ||
9680 (port && port->status & CTL_PORT_STATUS_HA_SHARED))
9681 g = 1;
9682 else
9683 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt;
9684 scsi_ulto2b(g, &desc->identifier[2]);
9685 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9686 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9687
9688 /*
9689 * This is for the Target identifier
9690 */
9691 if (port && port->target_devid) {
9692 memcpy(desc, port->target_devid->data, port->target_devid->len);
9693 }
9694
9695 ctl_set_success(ctsio);
9696 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9697 ctsio->be_move_done = ctl_config_move_done;
9698 ctl_datamove((union ctl_io *)ctsio);
9699 return (CTL_RETVAL_COMPLETE);
9700 }
9701
9702 static int
ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio * ctsio,int alloc_len)9703 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9704 {
9705 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9706 struct ctl_lun *lun = CTL_LUN(ctsio);
9707 struct scsi_vpd_scsi_ports *sp;
9708 struct scsi_vpd_port_designation *pd;
9709 struct scsi_vpd_port_designation_cont *pdc;
9710 struct ctl_port *port;
9711 int data_len, num_target_ports, iid_len, id_len;
9712
9713 num_target_ports = 0;
9714 iid_len = 0;
9715 id_len = 0;
9716 mtx_lock(&softc->ctl_lock);
9717 STAILQ_FOREACH(port, &softc->port_list, links) {
9718 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9719 continue;
9720 if (lun != NULL &&
9721 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9722 continue;
9723 num_target_ports++;
9724 if (port->init_devid)
9725 iid_len += port->init_devid->len;
9726 if (port->port_devid)
9727 id_len += port->port_devid->len;
9728 }
9729 mtx_unlock(&softc->ctl_lock);
9730
9731 data_len = sizeof(struct scsi_vpd_scsi_ports) +
9732 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9733 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9734 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9735 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9736 ctsio->kern_sg_entries = 0;
9737 ctsio->kern_rel_offset = 0;
9738 ctsio->kern_sg_entries = 0;
9739 ctsio->kern_data_len = min(data_len, alloc_len);
9740 ctsio->kern_total_len = ctsio->kern_data_len;
9741
9742 /*
9743 * The control device is always connected. The disk device, on the
9744 * other hand, may not be online all the time. Need to change this
9745 * to figure out whether the disk device is actually online or not.
9746 */
9747 if (lun != NULL)
9748 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9749 lun->be_lun->lun_type;
9750 else
9751 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9752
9753 sp->page_code = SVPD_SCSI_PORTS;
9754 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9755 sp->page_length);
9756 pd = &sp->design[0];
9757
9758 mtx_lock(&softc->ctl_lock);
9759 STAILQ_FOREACH(port, &softc->port_list, links) {
9760 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9761 continue;
9762 if (lun != NULL &&
9763 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9764 continue;
9765 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9766 if (port->init_devid) {
9767 iid_len = port->init_devid->len;
9768 memcpy(pd->initiator_transportid,
9769 port->init_devid->data, port->init_devid->len);
9770 } else
9771 iid_len = 0;
9772 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9773 pdc = (struct scsi_vpd_port_designation_cont *)
9774 (&pd->initiator_transportid[iid_len]);
9775 if (port->port_devid) {
9776 id_len = port->port_devid->len;
9777 memcpy(pdc->target_port_descriptors,
9778 port->port_devid->data, port->port_devid->len);
9779 } else
9780 id_len = 0;
9781 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9782 pd = (struct scsi_vpd_port_designation *)
9783 ((uint8_t *)pdc->target_port_descriptors + id_len);
9784 }
9785 mtx_unlock(&softc->ctl_lock);
9786
9787 ctl_set_success(ctsio);
9788 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9789 ctsio->be_move_done = ctl_config_move_done;
9790 ctl_datamove((union ctl_io *)ctsio);
9791 return (CTL_RETVAL_COMPLETE);
9792 }
9793
9794 static int
ctl_inquiry_evpd_sfs(struct ctl_scsiio * ctsio,int alloc_len)9795 ctl_inquiry_evpd_sfs(struct ctl_scsiio *ctsio, int alloc_len)
9796 {
9797 struct ctl_lun *lun = CTL_LUN(ctsio);
9798 struct scsi_vpd_sfs *sfs_ptr;
9799 int sfs_page_size, n;
9800
9801 sfs_page_size = sizeof(*sfs_ptr) + 5 * 2;
9802 ctsio->kern_data_ptr = malloc(sfs_page_size, M_CTL, M_WAITOK | M_ZERO);
9803 sfs_ptr = (struct scsi_vpd_sfs *)ctsio->kern_data_ptr;
9804 ctsio->kern_sg_entries = 0;
9805 ctsio->kern_rel_offset = 0;
9806 ctsio->kern_sg_entries = 0;
9807 ctsio->kern_data_len = min(sfs_page_size, alloc_len);
9808 ctsio->kern_total_len = ctsio->kern_data_len;
9809
9810 /*
9811 * The control device is always connected. The disk device, on the
9812 * other hand, may not be online all the time. Need to change this
9813 * to figure out whether the disk device is actually online or not.
9814 */
9815 if (lun != NULL)
9816 sfs_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9817 lun->be_lun->lun_type;
9818 else
9819 sfs_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9820
9821 sfs_ptr->page_code = SVPD_SCSI_SFS;
9822 n = 0;
9823 /* Discovery 2016 */
9824 scsi_ulto2b(0x0001, &sfs_ptr->codes[2 * n++]);
9825 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9826 /* SBC Base 2016 */
9827 scsi_ulto2b(0x0101, &sfs_ptr->codes[2 * n++]);
9828 /* SBC Base 2010 */
9829 scsi_ulto2b(0x0102, &sfs_ptr->codes[2 * n++]);
9830 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9831 /* Basic Provisioning 2016 */
9832 scsi_ulto2b(0x0103, &sfs_ptr->codes[2 * n++]);
9833 }
9834 /* Drive Maintenance 2016 */
9835 //scsi_ulto2b(0x0104, &sfs_ptr->codes[2 * n++]);
9836 }
9837 scsi_ulto2b(4 + 2 * n, sfs_ptr->page_length);
9838
9839 ctl_set_success(ctsio);
9840 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9841 ctsio->be_move_done = ctl_config_move_done;
9842 ctl_datamove((union ctl_io *)ctsio);
9843 return (CTL_RETVAL_COMPLETE);
9844 }
9845
9846 static int
ctl_inquiry_evpd_block_limits(struct ctl_scsiio * ctsio,int alloc_len)9847 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9848 {
9849 struct ctl_lun *lun = CTL_LUN(ctsio);
9850 struct scsi_vpd_block_limits *bl_ptr;
9851 const char *val;
9852 uint64_t ival;
9853
9854 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9855 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9856 ctsio->kern_sg_entries = 0;
9857 ctsio->kern_rel_offset = 0;
9858 ctsio->kern_sg_entries = 0;
9859 ctsio->kern_data_len = min(sizeof(*bl_ptr), alloc_len);
9860 ctsio->kern_total_len = ctsio->kern_data_len;
9861
9862 /*
9863 * The control device is always connected. The disk device, on the
9864 * other hand, may not be online all the time. Need to change this
9865 * to figure out whether the disk device is actually online or not.
9866 */
9867 if (lun != NULL)
9868 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9869 lun->be_lun->lun_type;
9870 else
9871 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9872
9873 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9874 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9875 bl_ptr->max_cmp_write_len = 0xff;
9876 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9877 if (lun != NULL) {
9878 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9879 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9880 ival = 0xffffffff;
9881 val = dnvlist_get_string(lun->be_lun->options,
9882 "unmap_max_lba", NULL);
9883 if (val != NULL)
9884 ctl_expand_number(val, &ival);
9885 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt);
9886 ival = 0xffffffff;
9887 val = dnvlist_get_string(lun->be_lun->options,
9888 "unmap_max_descr", NULL);
9889 if (val != NULL)
9890 ctl_expand_number(val, &ival);
9891 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt);
9892 if (lun->be_lun->ublockexp != 0) {
9893 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9894 bl_ptr->opt_unmap_grain);
9895 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9896 bl_ptr->unmap_grain_align);
9897 }
9898 }
9899 scsi_ulto4b(lun->be_lun->atomicblock,
9900 bl_ptr->max_atomic_transfer_length);
9901 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9902 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9903 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary);
9904 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size);
9905 ival = UINT64_MAX;
9906 val = dnvlist_get_string(lun->be_lun->options,
9907 "write_same_max_lba", NULL);
9908 if (val != NULL)
9909 ctl_expand_number(val, &ival);
9910 scsi_u64to8b(ival, bl_ptr->max_write_same_length);
9911 if (lun->be_lun->maxlba + 1 > ival)
9912 bl_ptr->flags |= SVPD_BL_WSNZ;
9913 }
9914
9915 ctl_set_success(ctsio);
9916 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9917 ctsio->be_move_done = ctl_config_move_done;
9918 ctl_datamove((union ctl_io *)ctsio);
9919 return (CTL_RETVAL_COMPLETE);
9920 }
9921
9922 static int
ctl_inquiry_evpd_bdc(struct ctl_scsiio * ctsio,int alloc_len)9923 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
9924 {
9925 struct ctl_lun *lun = CTL_LUN(ctsio);
9926 struct scsi_vpd_block_device_characteristics *bdc_ptr;
9927 const char *value;
9928 u_int i;
9929
9930 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
9931 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
9932 ctsio->kern_sg_entries = 0;
9933 ctsio->kern_rel_offset = 0;
9934 ctsio->kern_data_len = min(sizeof(*bdc_ptr), alloc_len);
9935 ctsio->kern_total_len = ctsio->kern_data_len;
9936
9937 /*
9938 * The control device is always connected. The disk device, on the
9939 * other hand, may not be online all the time. Need to change this
9940 * to figure out whether the disk device is actually online or not.
9941 */
9942 if (lun != NULL)
9943 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9944 lun->be_lun->lun_type;
9945 else
9946 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9947 bdc_ptr->page_code = SVPD_BDC;
9948 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
9949 if (lun != NULL &&
9950 (value = dnvlist_get_string(lun->be_lun->options, "rpm", NULL)) != NULL)
9951 i = strtol(value, NULL, 0);
9952 else
9953 i = CTL_DEFAULT_ROTATION_RATE;
9954 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
9955 if (lun != NULL &&
9956 (value = dnvlist_get_string(lun->be_lun->options, "formfactor", NULL)) != NULL)
9957 i = strtol(value, NULL, 0);
9958 else
9959 i = 0;
9960 bdc_ptr->wab_wac_ff = (i & 0x0f);
9961 bdc_ptr->flags = SVPD_RBWZ | SVPD_FUAB | SVPD_VBULS;
9962
9963 ctl_set_success(ctsio);
9964 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9965 ctsio->be_move_done = ctl_config_move_done;
9966 ctl_datamove((union ctl_io *)ctsio);
9967 return (CTL_RETVAL_COMPLETE);
9968 }
9969
9970 static int
ctl_inquiry_evpd_lbp(struct ctl_scsiio * ctsio,int alloc_len)9971 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9972 {
9973 struct ctl_lun *lun = CTL_LUN(ctsio);
9974 struct scsi_vpd_logical_block_prov *lbp_ptr;
9975 const char *value;
9976
9977 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
9978 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
9979 ctsio->kern_sg_entries = 0;
9980 ctsio->kern_rel_offset = 0;
9981 ctsio->kern_data_len = min(sizeof(*lbp_ptr), alloc_len);
9982 ctsio->kern_total_len = ctsio->kern_data_len;
9983
9984 /*
9985 * The control device is always connected. The disk device, on the
9986 * other hand, may not be online all the time. Need to change this
9987 * to figure out whether the disk device is actually online or not.
9988 */
9989 if (lun != NULL)
9990 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9991 lun->be_lun->lun_type;
9992 else
9993 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9994
9995 lbp_ptr->page_code = SVPD_LBP;
9996 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
9997 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
9998 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9999 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10000 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10001 value = dnvlist_get_string(lun->be_lun->options,
10002 "provisioning_type", NULL);
10003 if (value != NULL) {
10004 if (strcmp(value, "resource") == 0)
10005 lbp_ptr->prov_type = SVPD_LBP_RESOURCE;
10006 else if (strcmp(value, "thin") == 0)
10007 lbp_ptr->prov_type = SVPD_LBP_THIN;
10008 } else
10009 lbp_ptr->prov_type = SVPD_LBP_THIN;
10010 }
10011
10012 ctl_set_success(ctsio);
10013 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10014 ctsio->be_move_done = ctl_config_move_done;
10015 ctl_datamove((union ctl_io *)ctsio);
10016 return (CTL_RETVAL_COMPLETE);
10017 }
10018
10019 /*
10020 * INQUIRY with the EVPD bit set.
10021 */
10022 static int
ctl_inquiry_evpd(struct ctl_scsiio * ctsio)10023 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10024 {
10025 struct ctl_lun *lun = CTL_LUN(ctsio);
10026 struct scsi_inquiry *cdb;
10027 int alloc_len, retval;
10028
10029 cdb = (struct scsi_inquiry *)ctsio->cdb;
10030 alloc_len = scsi_2btoul(cdb->length);
10031
10032 switch (cdb->page_code) {
10033 case SVPD_SUPPORTED_PAGES:
10034 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10035 break;
10036 case SVPD_UNIT_SERIAL_NUMBER:
10037 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10038 break;
10039 case SVPD_DEVICE_ID:
10040 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10041 break;
10042 case SVPD_EXTENDED_INQUIRY_DATA:
10043 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10044 break;
10045 case SVPD_MODE_PAGE_POLICY:
10046 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10047 break;
10048 case SVPD_SCSI_PORTS:
10049 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10050 break;
10051 case SVPD_SCSI_TPC:
10052 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10053 break;
10054 case SVPD_SCSI_SFS:
10055 retval = ctl_inquiry_evpd_sfs(ctsio, alloc_len);
10056 break;
10057 case SVPD_BLOCK_LIMITS:
10058 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10059 goto err;
10060 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10061 break;
10062 case SVPD_BDC:
10063 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10064 goto err;
10065 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10066 break;
10067 case SVPD_LBP:
10068 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10069 goto err;
10070 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10071 break;
10072 default:
10073 err:
10074 ctl_set_invalid_field(ctsio,
10075 /*sks_valid*/ 1,
10076 /*command*/ 1,
10077 /*field*/ 2,
10078 /*bit_valid*/ 0,
10079 /*bit*/ 0);
10080 ctl_done((union ctl_io *)ctsio);
10081 retval = CTL_RETVAL_COMPLETE;
10082 break;
10083 }
10084
10085 return (retval);
10086 }
10087
10088 /*
10089 * Standard INQUIRY data.
10090 */
10091 static int
ctl_inquiry_std(struct ctl_scsiio * ctsio)10092 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10093 {
10094 struct ctl_softc *softc = CTL_SOFTC(ctsio);
10095 struct ctl_port *port = CTL_PORT(ctsio);
10096 struct ctl_lun *lun = CTL_LUN(ctsio);
10097 struct scsi_inquiry_data *inq_ptr;
10098 struct scsi_inquiry *cdb;
10099 const char *val;
10100 uint32_t alloc_len, data_len;
10101 ctl_port_type port_type;
10102
10103 port_type = port->port_type;
10104 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10105 port_type = CTL_PORT_SCSI;
10106
10107 cdb = (struct scsi_inquiry *)ctsio->cdb;
10108 alloc_len = scsi_2btoul(cdb->length);
10109
10110 /*
10111 * We malloc the full inquiry data size here and fill it
10112 * in. If the user only asks for less, we'll give him
10113 * that much.
10114 */
10115 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10116 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10117 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10118 ctsio->kern_sg_entries = 0;
10119 ctsio->kern_rel_offset = 0;
10120 ctsio->kern_data_len = min(data_len, alloc_len);
10121 ctsio->kern_total_len = ctsio->kern_data_len;
10122
10123 if (lun != NULL) {
10124 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
10125 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
10126 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10127 lun->be_lun->lun_type;
10128 } else {
10129 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
10130 lun->be_lun->lun_type;
10131 }
10132 if (lun->flags & CTL_LUN_REMOVABLE)
10133 inq_ptr->dev_qual2 |= SID_RMB;
10134 } else
10135 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10136
10137 /* RMB in byte 2 is 0 */
10138 inq_ptr->version = SCSI_REV_SPC5;
10139
10140 /*
10141 * According to SAM-3, even if a device only supports a single
10142 * level of LUN addressing, it should still set the HISUP bit:
10143 *
10144 * 4.9.1 Logical unit numbers overview
10145 *
10146 * All logical unit number formats described in this standard are
10147 * hierarchical in structure even when only a single level in that
10148 * hierarchy is used. The HISUP bit shall be set to one in the
10149 * standard INQUIRY data (see SPC-2) when any logical unit number
10150 * format described in this standard is used. Non-hierarchical
10151 * formats are outside the scope of this standard.
10152 *
10153 * Therefore we set the HiSup bit here.
10154 *
10155 * The response format is 2, per SPC-3.
10156 */
10157 inq_ptr->response_format = SID_HiSup | 2;
10158
10159 inq_ptr->additional_length = data_len -
10160 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10161 CTL_DEBUG_PRINT(("additional_length = %d\n",
10162 inq_ptr->additional_length));
10163
10164 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10165 if (port_type == CTL_PORT_SCSI)
10166 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10167 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10168 inq_ptr->flags = SID_CmdQue;
10169 if (port_type == CTL_PORT_SCSI)
10170 inq_ptr->flags |= SID_WBus16 | SID_Sync;
10171
10172 /*
10173 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10174 * We have 8 bytes for the vendor name, and 16 bytes for the device
10175 * name and 4 bytes for the revision.
10176 */
10177 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options,
10178 "vendor", NULL)) == NULL) {
10179 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10180 } else {
10181 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10182 strncpy(inq_ptr->vendor, val,
10183 min(sizeof(inq_ptr->vendor), strlen(val)));
10184 }
10185 if (lun == NULL) {
10186 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10187 sizeof(inq_ptr->product));
10188 } else if ((val = dnvlist_get_string(lun->be_lun->options, "product",
10189 NULL)) == NULL) {
10190 switch (lun->be_lun->lun_type) {
10191 case T_DIRECT:
10192 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10193 sizeof(inq_ptr->product));
10194 break;
10195 case T_PROCESSOR:
10196 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10197 sizeof(inq_ptr->product));
10198 break;
10199 case T_CDROM:
10200 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT,
10201 sizeof(inq_ptr->product));
10202 break;
10203 default:
10204 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10205 sizeof(inq_ptr->product));
10206 break;
10207 }
10208 } else {
10209 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10210 strncpy(inq_ptr->product, val,
10211 min(sizeof(inq_ptr->product), strlen(val)));
10212 }
10213
10214 /*
10215 * XXX make this a macro somewhere so it automatically gets
10216 * incremented when we make changes.
10217 */
10218 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options,
10219 "revision", NULL)) == NULL) {
10220 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10221 } else {
10222 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10223 strncpy(inq_ptr->revision, val,
10224 min(sizeof(inq_ptr->revision), strlen(val)));
10225 }
10226
10227 /*
10228 * For parallel SCSI, we support double transition and single
10229 * transition clocking. We also support QAS (Quick Arbitration
10230 * and Selection) and Information Unit transfers on both the
10231 * control and array devices.
10232 */
10233 if (port_type == CTL_PORT_SCSI)
10234 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10235 SID_SPI_IUS;
10236
10237 /* SAM-6 (no version claimed) */
10238 scsi_ulto2b(0x00C0, inq_ptr->version1);
10239 /* SPC-5 (no version claimed) */
10240 scsi_ulto2b(0x05C0, inq_ptr->version2);
10241 if (port_type == CTL_PORT_FC) {
10242 /* FCP-2 ANSI INCITS.350:2003 */
10243 scsi_ulto2b(0x0917, inq_ptr->version3);
10244 } else if (port_type == CTL_PORT_SCSI) {
10245 /* SPI-4 ANSI INCITS.362:200x */
10246 scsi_ulto2b(0x0B56, inq_ptr->version3);
10247 } else if (port_type == CTL_PORT_ISCSI) {
10248 /* iSCSI (no version claimed) */
10249 scsi_ulto2b(0x0960, inq_ptr->version3);
10250 } else if (port_type == CTL_PORT_SAS) {
10251 /* SAS (no version claimed) */
10252 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10253 } else if (port_type == CTL_PORT_UMASS) {
10254 /* USB Mass Storage Class Bulk-Only Transport, Revision 1.0 */
10255 scsi_ulto2b(0x1730, inq_ptr->version3);
10256 }
10257
10258 if (lun == NULL) {
10259 /* SBC-4 (no version claimed) */
10260 scsi_ulto2b(0x0600, inq_ptr->version4);
10261 } else {
10262 switch (lun->be_lun->lun_type) {
10263 case T_DIRECT:
10264 /* SBC-4 (no version claimed) */
10265 scsi_ulto2b(0x0600, inq_ptr->version4);
10266 break;
10267 case T_PROCESSOR:
10268 break;
10269 case T_CDROM:
10270 /* MMC-6 (no version claimed) */
10271 scsi_ulto2b(0x04E0, inq_ptr->version4);
10272 break;
10273 default:
10274 break;
10275 }
10276 }
10277
10278 ctl_set_success(ctsio);
10279 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10280 ctsio->be_move_done = ctl_config_move_done;
10281 ctl_datamove((union ctl_io *)ctsio);
10282 return (CTL_RETVAL_COMPLETE);
10283 }
10284
10285 int
ctl_inquiry(struct ctl_scsiio * ctsio)10286 ctl_inquiry(struct ctl_scsiio *ctsio)
10287 {
10288 struct scsi_inquiry *cdb;
10289 int retval;
10290
10291 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10292
10293 cdb = (struct scsi_inquiry *)ctsio->cdb;
10294 if (cdb->byte2 & SI_EVPD)
10295 retval = ctl_inquiry_evpd(ctsio);
10296 else if (cdb->page_code == 0)
10297 retval = ctl_inquiry_std(ctsio);
10298 else {
10299 ctl_set_invalid_field(ctsio,
10300 /*sks_valid*/ 1,
10301 /*command*/ 1,
10302 /*field*/ 2,
10303 /*bit_valid*/ 0,
10304 /*bit*/ 0);
10305 ctl_done((union ctl_io *)ctsio);
10306 return (CTL_RETVAL_COMPLETE);
10307 }
10308
10309 return (retval);
10310 }
10311
10312 int
ctl_get_config(struct ctl_scsiio * ctsio)10313 ctl_get_config(struct ctl_scsiio *ctsio)
10314 {
10315 struct ctl_lun *lun = CTL_LUN(ctsio);
10316 struct scsi_get_config_header *hdr;
10317 struct scsi_get_config_feature *feature;
10318 struct scsi_get_config *cdb;
10319 uint32_t alloc_len, data_len;
10320 int rt, starting;
10321
10322 cdb = (struct scsi_get_config *)ctsio->cdb;
10323 rt = (cdb->rt & SGC_RT_MASK);
10324 starting = scsi_2btoul(cdb->starting_feature);
10325 alloc_len = scsi_2btoul(cdb->length);
10326
10327 data_len = sizeof(struct scsi_get_config_header) +
10328 sizeof(struct scsi_get_config_feature) + 8 +
10329 sizeof(struct scsi_get_config_feature) + 8 +
10330 sizeof(struct scsi_get_config_feature) + 4 +
10331 sizeof(struct scsi_get_config_feature) + 4 +
10332 sizeof(struct scsi_get_config_feature) + 8 +
10333 sizeof(struct scsi_get_config_feature) +
10334 sizeof(struct scsi_get_config_feature) + 4 +
10335 sizeof(struct scsi_get_config_feature) + 4 +
10336 sizeof(struct scsi_get_config_feature) + 4 +
10337 sizeof(struct scsi_get_config_feature) + 4 +
10338 sizeof(struct scsi_get_config_feature) + 4 +
10339 sizeof(struct scsi_get_config_feature) + 4;
10340 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10341 ctsio->kern_sg_entries = 0;
10342 ctsio->kern_rel_offset = 0;
10343
10344 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr;
10345 if (lun->flags & CTL_LUN_NO_MEDIA)
10346 scsi_ulto2b(0x0000, hdr->current_profile);
10347 else
10348 scsi_ulto2b(0x0010, hdr->current_profile);
10349 feature = (struct scsi_get_config_feature *)(hdr + 1);
10350
10351 if (starting > 0x003b)
10352 goto done;
10353 if (starting > 0x003a)
10354 goto f3b;
10355 if (starting > 0x002b)
10356 goto f3a;
10357 if (starting > 0x002a)
10358 goto f2b;
10359 if (starting > 0x001f)
10360 goto f2a;
10361 if (starting > 0x001e)
10362 goto f1f;
10363 if (starting > 0x001d)
10364 goto f1e;
10365 if (starting > 0x0010)
10366 goto f1d;
10367 if (starting > 0x0003)
10368 goto f10;
10369 if (starting > 0x0002)
10370 goto f3;
10371 if (starting > 0x0001)
10372 goto f2;
10373 if (starting > 0x0000)
10374 goto f1;
10375
10376 /* Profile List */
10377 scsi_ulto2b(0x0000, feature->feature_code);
10378 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT;
10379 feature->add_length = 8;
10380 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */
10381 feature->feature_data[2] = 0x00;
10382 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */
10383 feature->feature_data[6] = 0x01;
10384 feature = (struct scsi_get_config_feature *)
10385 &feature->feature_data[feature->add_length];
10386
10387 f1: /* Core */
10388 scsi_ulto2b(0x0001, feature->feature_code);
10389 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10390 feature->add_length = 8;
10391 scsi_ulto4b(0x00000000, &feature->feature_data[0]);
10392 feature->feature_data[4] = 0x03;
10393 feature = (struct scsi_get_config_feature *)
10394 &feature->feature_data[feature->add_length];
10395
10396 f2: /* Morphing */
10397 scsi_ulto2b(0x0002, feature->feature_code);
10398 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10399 feature->add_length = 4;
10400 feature->feature_data[0] = 0x02;
10401 feature = (struct scsi_get_config_feature *)
10402 &feature->feature_data[feature->add_length];
10403
10404 f3: /* Removable Medium */
10405 scsi_ulto2b(0x0003, feature->feature_code);
10406 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10407 feature->add_length = 4;
10408 feature->feature_data[0] = 0x39;
10409 feature = (struct scsi_get_config_feature *)
10410 &feature->feature_data[feature->add_length];
10411
10412 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA))
10413 goto done;
10414
10415 f10: /* Random Read */
10416 scsi_ulto2b(0x0010, feature->feature_code);
10417 feature->flags = 0x00;
10418 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10419 feature->flags |= SGC_F_CURRENT;
10420 feature->add_length = 8;
10421 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]);
10422 scsi_ulto2b(1, &feature->feature_data[4]);
10423 feature->feature_data[6] = 0x00;
10424 feature = (struct scsi_get_config_feature *)
10425 &feature->feature_data[feature->add_length];
10426
10427 f1d: /* Multi-Read */
10428 scsi_ulto2b(0x001D, feature->feature_code);
10429 feature->flags = 0x00;
10430 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10431 feature->flags |= SGC_F_CURRENT;
10432 feature->add_length = 0;
10433 feature = (struct scsi_get_config_feature *)
10434 &feature->feature_data[feature->add_length];
10435
10436 f1e: /* CD Read */
10437 scsi_ulto2b(0x001E, feature->feature_code);
10438 feature->flags = 0x00;
10439 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10440 feature->flags |= SGC_F_CURRENT;
10441 feature->add_length = 4;
10442 feature->feature_data[0] = 0x00;
10443 feature = (struct scsi_get_config_feature *)
10444 &feature->feature_data[feature->add_length];
10445
10446 f1f: /* DVD Read */
10447 scsi_ulto2b(0x001F, feature->feature_code);
10448 feature->flags = 0x08;
10449 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10450 feature->flags |= SGC_F_CURRENT;
10451 feature->add_length = 4;
10452 feature->feature_data[0] = 0x01;
10453 feature->feature_data[2] = 0x03;
10454 feature = (struct scsi_get_config_feature *)
10455 &feature->feature_data[feature->add_length];
10456
10457 f2a: /* DVD+RW */
10458 scsi_ulto2b(0x002A, feature->feature_code);
10459 feature->flags = 0x04;
10460 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10461 feature->flags |= SGC_F_CURRENT;
10462 feature->add_length = 4;
10463 feature->feature_data[0] = 0x00;
10464 feature->feature_data[1] = 0x00;
10465 feature = (struct scsi_get_config_feature *)
10466 &feature->feature_data[feature->add_length];
10467
10468 f2b: /* DVD+R */
10469 scsi_ulto2b(0x002B, feature->feature_code);
10470 feature->flags = 0x00;
10471 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10472 feature->flags |= SGC_F_CURRENT;
10473 feature->add_length = 4;
10474 feature->feature_data[0] = 0x00;
10475 feature = (struct scsi_get_config_feature *)
10476 &feature->feature_data[feature->add_length];
10477
10478 f3a: /* DVD+RW Dual Layer */
10479 scsi_ulto2b(0x003A, feature->feature_code);
10480 feature->flags = 0x00;
10481 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10482 feature->flags |= SGC_F_CURRENT;
10483 feature->add_length = 4;
10484 feature->feature_data[0] = 0x00;
10485 feature->feature_data[1] = 0x00;
10486 feature = (struct scsi_get_config_feature *)
10487 &feature->feature_data[feature->add_length];
10488
10489 f3b: /* DVD+R Dual Layer */
10490 scsi_ulto2b(0x003B, feature->feature_code);
10491 feature->flags = 0x00;
10492 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10493 feature->flags |= SGC_F_CURRENT;
10494 feature->add_length = 4;
10495 feature->feature_data[0] = 0x00;
10496 feature = (struct scsi_get_config_feature *)
10497 &feature->feature_data[feature->add_length];
10498
10499 done:
10500 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10501 if (rt == SGC_RT_SPECIFIC && data_len > 4) {
10502 feature = (struct scsi_get_config_feature *)(hdr + 1);
10503 if (scsi_2btoul(feature->feature_code) == starting)
10504 feature = (struct scsi_get_config_feature *)
10505 &feature->feature_data[feature->add_length];
10506 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10507 }
10508 scsi_ulto4b(data_len - 4, hdr->data_length);
10509 ctsio->kern_data_len = min(data_len, alloc_len);
10510 ctsio->kern_total_len = ctsio->kern_data_len;
10511
10512 ctl_set_success(ctsio);
10513 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10514 ctsio->be_move_done = ctl_config_move_done;
10515 ctl_datamove((union ctl_io *)ctsio);
10516 return (CTL_RETVAL_COMPLETE);
10517 }
10518
10519 int
ctl_get_event_status(struct ctl_scsiio * ctsio)10520 ctl_get_event_status(struct ctl_scsiio *ctsio)
10521 {
10522 struct scsi_get_event_status_header *hdr;
10523 struct scsi_get_event_status *cdb;
10524 uint32_t alloc_len, data_len;
10525
10526 cdb = (struct scsi_get_event_status *)ctsio->cdb;
10527 if ((cdb->byte2 & SGESN_POLLED) == 0) {
10528 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
10529 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
10530 ctl_done((union ctl_io *)ctsio);
10531 return (CTL_RETVAL_COMPLETE);
10532 }
10533 alloc_len = scsi_2btoul(cdb->length);
10534
10535 data_len = sizeof(struct scsi_get_event_status_header);
10536 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10537 ctsio->kern_sg_entries = 0;
10538 ctsio->kern_rel_offset = 0;
10539 ctsio->kern_data_len = min(data_len, alloc_len);
10540 ctsio->kern_total_len = ctsio->kern_data_len;
10541
10542 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr;
10543 scsi_ulto2b(0, hdr->descr_length);
10544 hdr->nea_class = SGESN_NEA;
10545 hdr->supported_class = 0;
10546
10547 ctl_set_success(ctsio);
10548 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10549 ctsio->be_move_done = ctl_config_move_done;
10550 ctl_datamove((union ctl_io *)ctsio);
10551 return (CTL_RETVAL_COMPLETE);
10552 }
10553
10554 int
ctl_mechanism_status(struct ctl_scsiio * ctsio)10555 ctl_mechanism_status(struct ctl_scsiio *ctsio)
10556 {
10557 struct scsi_mechanism_status_header *hdr;
10558 struct scsi_mechanism_status *cdb;
10559 uint32_t alloc_len, data_len;
10560
10561 cdb = (struct scsi_mechanism_status *)ctsio->cdb;
10562 alloc_len = scsi_2btoul(cdb->length);
10563
10564 data_len = sizeof(struct scsi_mechanism_status_header);
10565 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10566 ctsio->kern_sg_entries = 0;
10567 ctsio->kern_rel_offset = 0;
10568 ctsio->kern_data_len = min(data_len, alloc_len);
10569 ctsio->kern_total_len = ctsio->kern_data_len;
10570
10571 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr;
10572 hdr->state1 = 0x00;
10573 hdr->state2 = 0xe0;
10574 scsi_ulto3b(0, hdr->lba);
10575 hdr->slots_num = 0;
10576 scsi_ulto2b(0, hdr->slots_length);
10577
10578 ctl_set_success(ctsio);
10579 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10580 ctsio->be_move_done = ctl_config_move_done;
10581 ctl_datamove((union ctl_io *)ctsio);
10582 return (CTL_RETVAL_COMPLETE);
10583 }
10584
10585 static void
ctl_ultomsf(uint32_t lba,uint8_t * buf)10586 ctl_ultomsf(uint32_t lba, uint8_t *buf)
10587 {
10588
10589 lba += 150;
10590 buf[0] = 0;
10591 buf[1] = bin2bcd((lba / 75) / 60);
10592 buf[2] = bin2bcd((lba / 75) % 60);
10593 buf[3] = bin2bcd(lba % 75);
10594 }
10595
10596 int
ctl_read_toc(struct ctl_scsiio * ctsio)10597 ctl_read_toc(struct ctl_scsiio *ctsio)
10598 {
10599 struct ctl_lun *lun = CTL_LUN(ctsio);
10600 struct scsi_read_toc_hdr *hdr;
10601 struct scsi_read_toc_type01_descr *descr;
10602 struct scsi_read_toc *cdb;
10603 uint32_t alloc_len, data_len;
10604 int format, msf;
10605
10606 cdb = (struct scsi_read_toc *)ctsio->cdb;
10607 msf = (cdb->byte2 & CD_MSF) != 0;
10608 format = cdb->format;
10609 alloc_len = scsi_2btoul(cdb->data_len);
10610
10611 data_len = sizeof(struct scsi_read_toc_hdr);
10612 if (format == 0)
10613 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr);
10614 else
10615 data_len += sizeof(struct scsi_read_toc_type01_descr);
10616 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10617 ctsio->kern_sg_entries = 0;
10618 ctsio->kern_rel_offset = 0;
10619 ctsio->kern_data_len = min(data_len, alloc_len);
10620 ctsio->kern_total_len = ctsio->kern_data_len;
10621
10622 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr;
10623 if (format == 0) {
10624 scsi_ulto2b(0x12, hdr->data_length);
10625 hdr->first = 1;
10626 hdr->last = 1;
10627 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10628 descr->addr_ctl = 0x14;
10629 descr->track_number = 1;
10630 if (msf)
10631 ctl_ultomsf(0, descr->track_start);
10632 else
10633 scsi_ulto4b(0, descr->track_start);
10634 descr++;
10635 descr->addr_ctl = 0x14;
10636 descr->track_number = 0xaa;
10637 if (msf)
10638 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start);
10639 else
10640 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start);
10641 } else {
10642 scsi_ulto2b(0x0a, hdr->data_length);
10643 hdr->first = 1;
10644 hdr->last = 1;
10645 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10646 descr->addr_ctl = 0x14;
10647 descr->track_number = 1;
10648 if (msf)
10649 ctl_ultomsf(0, descr->track_start);
10650 else
10651 scsi_ulto4b(0, descr->track_start);
10652 }
10653
10654 ctl_set_success(ctsio);
10655 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10656 ctsio->be_move_done = ctl_config_move_done;
10657 ctl_datamove((union ctl_io *)ctsio);
10658 return (CTL_RETVAL_COMPLETE);
10659 }
10660
10661 /*
10662 * For known CDB types, parse the LBA and length.
10663 */
10664 static int
ctl_get_lba_len(union ctl_io * io,uint64_t * lba,uint64_t * len)10665 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10666 {
10667 if (io->io_hdr.io_type != CTL_IO_SCSI)
10668 return (1);
10669
10670 switch (io->scsiio.cdb[0]) {
10671 case COMPARE_AND_WRITE: {
10672 struct scsi_compare_and_write *cdb;
10673
10674 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10675
10676 *lba = scsi_8btou64(cdb->addr);
10677 *len = cdb->length;
10678 break;
10679 }
10680 case READ_6:
10681 case WRITE_6: {
10682 struct scsi_rw_6 *cdb;
10683
10684 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10685
10686 *lba = scsi_3btoul(cdb->addr);
10687 /* only 5 bits are valid in the most significant address byte */
10688 *lba &= 0x1fffff;
10689 *len = cdb->length;
10690 break;
10691 }
10692 case READ_10:
10693 case WRITE_10: {
10694 struct scsi_rw_10 *cdb;
10695
10696 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10697
10698 *lba = scsi_4btoul(cdb->addr);
10699 *len = scsi_2btoul(cdb->length);
10700 break;
10701 }
10702 case WRITE_VERIFY_10: {
10703 struct scsi_write_verify_10 *cdb;
10704
10705 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10706
10707 *lba = scsi_4btoul(cdb->addr);
10708 *len = scsi_2btoul(cdb->length);
10709 break;
10710 }
10711 case READ_12:
10712 case WRITE_12: {
10713 struct scsi_rw_12 *cdb;
10714
10715 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10716
10717 *lba = scsi_4btoul(cdb->addr);
10718 *len = scsi_4btoul(cdb->length);
10719 break;
10720 }
10721 case WRITE_VERIFY_12: {
10722 struct scsi_write_verify_12 *cdb;
10723
10724 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10725
10726 *lba = scsi_4btoul(cdb->addr);
10727 *len = scsi_4btoul(cdb->length);
10728 break;
10729 }
10730 case READ_16:
10731 case WRITE_16: {
10732 struct scsi_rw_16 *cdb;
10733
10734 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10735
10736 *lba = scsi_8btou64(cdb->addr);
10737 *len = scsi_4btoul(cdb->length);
10738 break;
10739 }
10740 case WRITE_ATOMIC_16: {
10741 struct scsi_write_atomic_16 *cdb;
10742
10743 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb;
10744
10745 *lba = scsi_8btou64(cdb->addr);
10746 *len = scsi_2btoul(cdb->length);
10747 break;
10748 }
10749 case WRITE_VERIFY_16: {
10750 struct scsi_write_verify_16 *cdb;
10751
10752 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10753
10754 *lba = scsi_8btou64(cdb->addr);
10755 *len = scsi_4btoul(cdb->length);
10756 break;
10757 }
10758 case WRITE_SAME_10: {
10759 struct scsi_write_same_10 *cdb;
10760
10761 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10762
10763 *lba = scsi_4btoul(cdb->addr);
10764 *len = scsi_2btoul(cdb->length);
10765 break;
10766 }
10767 case WRITE_SAME_16: {
10768 struct scsi_write_same_16 *cdb;
10769
10770 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10771
10772 *lba = scsi_8btou64(cdb->addr);
10773 *len = scsi_4btoul(cdb->length);
10774 break;
10775 }
10776 case VERIFY_10: {
10777 struct scsi_verify_10 *cdb;
10778
10779 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10780
10781 *lba = scsi_4btoul(cdb->addr);
10782 *len = scsi_2btoul(cdb->length);
10783 break;
10784 }
10785 case VERIFY_12: {
10786 struct scsi_verify_12 *cdb;
10787
10788 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10789
10790 *lba = scsi_4btoul(cdb->addr);
10791 *len = scsi_4btoul(cdb->length);
10792 break;
10793 }
10794 case VERIFY_16: {
10795 struct scsi_verify_16 *cdb;
10796
10797 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10798
10799 *lba = scsi_8btou64(cdb->addr);
10800 *len = scsi_4btoul(cdb->length);
10801 break;
10802 }
10803 case UNMAP: {
10804 *lba = 0;
10805 *len = UINT64_MAX;
10806 break;
10807 }
10808 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10809 struct scsi_get_lba_status *cdb;
10810
10811 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10812 *lba = scsi_8btou64(cdb->addr);
10813 *len = UINT32_MAX;
10814 break;
10815 }
10816 default:
10817 return (1);
10818 break; /* NOTREACHED */
10819 }
10820
10821 return (0);
10822 }
10823
10824 static ctl_action
ctl_extent_check_lba(uint64_t lba1,uint64_t len1,uint64_t lba2,uint64_t len2,bool seq)10825 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10826 bool seq)
10827 {
10828 uint64_t endlba1, endlba2;
10829
10830 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10831 endlba2 = lba2 + len2 - 1;
10832
10833 if ((endlba1 < lba2) || (endlba2 < lba1))
10834 return (CTL_ACTION_PASS);
10835 else
10836 return (CTL_ACTION_BLOCK);
10837 }
10838
10839 static int
ctl_extent_check_unmap(union ctl_io * io,uint64_t lba2,uint64_t len2)10840 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10841 {
10842 struct ctl_ptr_len_flags *ptrlen;
10843 struct scsi_unmap_desc *buf, *end, *range;
10844 uint64_t lba;
10845 uint32_t len;
10846
10847 /* If not UNMAP -- go other way. */
10848 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10849 io->scsiio.cdb[0] != UNMAP)
10850 return (CTL_ACTION_ERROR);
10851
10852 /* If UNMAP without data -- block and wait for data. */
10853 ptrlen = (struct ctl_ptr_len_flags *)
10854 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10855 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10856 ptrlen->ptr == NULL)
10857 return (CTL_ACTION_BLOCK);
10858
10859 /* UNMAP with data -- check for collision. */
10860 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10861 end = buf + ptrlen->len / sizeof(*buf);
10862 for (range = buf; range < end; range++) {
10863 lba = scsi_8btou64(range->lba);
10864 len = scsi_4btoul(range->length);
10865 if ((lba < lba2 + len2) && (lba + len > lba2))
10866 return (CTL_ACTION_BLOCK);
10867 }
10868 return (CTL_ACTION_PASS);
10869 }
10870
10871 static ctl_action
ctl_extent_check(union ctl_io * io1,union ctl_io * io2,bool seq)10872 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10873 {
10874 uint64_t lba1, lba2;
10875 uint64_t len1, len2;
10876 int retval;
10877
10878 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10879 return (CTL_ACTION_ERROR);
10880
10881 retval = ctl_extent_check_unmap(io1, lba2, len2);
10882 if (retval != CTL_ACTION_ERROR)
10883 return (retval);
10884
10885 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10886 return (CTL_ACTION_ERROR);
10887
10888 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10889 seq = FALSE;
10890 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10891 }
10892
10893 static ctl_action
ctl_extent_check_seq(union ctl_io * io1,union ctl_io * io2)10894 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
10895 {
10896 uint64_t lba1, lba2;
10897 uint64_t len1, len2;
10898
10899 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10900 return (CTL_ACTION_PASS);
10901 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10902 return (CTL_ACTION_ERROR);
10903 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10904 return (CTL_ACTION_ERROR);
10905
10906 if (lba1 + len1 == lba2)
10907 return (CTL_ACTION_BLOCK);
10908 return (CTL_ACTION_PASS);
10909 }
10910
10911 static ctl_action
ctl_check_for_blockage(struct ctl_lun * lun,union ctl_io * pending_io,union ctl_io * ooa_io)10912 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10913 union ctl_io *ooa_io)
10914 {
10915 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10916 const ctl_serialize_action *serialize_row;
10917
10918 /*
10919 * Aborted commands are not going to be executed and may even
10920 * not report completion, so we don't care about their order.
10921 * Let them complete ASAP to clean the OOA queue.
10922 */
10923 if (pending_io->io_hdr.flags & CTL_FLAG_ABORT)
10924 return (CTL_ACTION_SKIP);
10925
10926 /*
10927 * The initiator attempted multiple untagged commands at the same
10928 * time. Can't do that.
10929 */
10930 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10931 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10932 && ((pending_io->io_hdr.nexus.targ_port ==
10933 ooa_io->io_hdr.nexus.targ_port)
10934 && (pending_io->io_hdr.nexus.initid ==
10935 ooa_io->io_hdr.nexus.initid))
10936 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10937 CTL_FLAG_STATUS_SENT)) == 0))
10938 return (CTL_ACTION_OVERLAP);
10939
10940 /*
10941 * The initiator attempted to send multiple tagged commands with
10942 * the same ID. (It's fine if different initiators have the same
10943 * tag ID.)
10944 *
10945 * Even if all of those conditions are true, we don't kill the I/O
10946 * if the command ahead of us has been aborted. We won't end up
10947 * sending it to the FETD, and it's perfectly legal to resend a
10948 * command with the same tag number as long as the previous
10949 * instance of this tag number has been aborted somehow.
10950 */
10951 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10952 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10953 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10954 && ((pending_io->io_hdr.nexus.targ_port ==
10955 ooa_io->io_hdr.nexus.targ_port)
10956 && (pending_io->io_hdr.nexus.initid ==
10957 ooa_io->io_hdr.nexus.initid))
10958 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10959 CTL_FLAG_STATUS_SENT)) == 0))
10960 return (CTL_ACTION_OVERLAP_TAG);
10961
10962 /*
10963 * If we get a head of queue tag, SAM-3 says that we should
10964 * immediately execute it.
10965 *
10966 * What happens if this command would normally block for some other
10967 * reason? e.g. a request sense with a head of queue tag
10968 * immediately after a write. Normally that would block, but this
10969 * will result in its getting executed immediately...
10970 *
10971 * We currently return "pass" instead of "skip", so we'll end up
10972 * going through the rest of the queue to check for overlapped tags.
10973 *
10974 * XXX KDM check for other types of blockage first??
10975 */
10976 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10977 return (CTL_ACTION_PASS);
10978
10979 /*
10980 * Ordered tags have to block until all items ahead of them
10981 * have completed. If we get called with an ordered tag, we always
10982 * block, if something else is ahead of us in the queue.
10983 */
10984 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10985 return (CTL_ACTION_BLOCK);
10986
10987 /*
10988 * Simple tags get blocked until all head of queue and ordered tags
10989 * ahead of them have completed. I'm lumping untagged commands in
10990 * with simple tags here. XXX KDM is that the right thing to do?
10991 */
10992 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10993 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10994 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10995 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10996 return (CTL_ACTION_BLOCK);
10997
10998 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
10999 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT,
11000 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p",
11001 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0],
11002 pending_io->scsiio.cdb[1], pending_io));
11003 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
11004 if (ooa_entry->seridx == CTL_SERIDX_INVLD)
11005 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */
11006 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT,
11007 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p",
11008 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0],
11009 ooa_io->scsiio.cdb[1], ooa_io));
11010
11011 serialize_row = ctl_serialize_table[ooa_entry->seridx];
11012
11013 switch (serialize_row[pending_entry->seridx]) {
11014 case CTL_SER_BLOCK:
11015 return (CTL_ACTION_BLOCK);
11016 case CTL_SER_EXTENT:
11017 return (ctl_extent_check(ooa_io, pending_io,
11018 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
11019 case CTL_SER_EXTENTOPT:
11020 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
11021 SCP_QUEUE_ALG_UNRESTRICTED)
11022 return (ctl_extent_check(ooa_io, pending_io,
11023 (lun->be_lun &&
11024 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
11025 return (CTL_ACTION_PASS);
11026 case CTL_SER_EXTENTSEQ:
11027 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
11028 return (ctl_extent_check_seq(ooa_io, pending_io));
11029 return (CTL_ACTION_PASS);
11030 case CTL_SER_PASS:
11031 return (CTL_ACTION_PASS);
11032 case CTL_SER_BLOCKOPT:
11033 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
11034 SCP_QUEUE_ALG_UNRESTRICTED)
11035 return (CTL_ACTION_BLOCK);
11036 return (CTL_ACTION_PASS);
11037 case CTL_SER_SKIP:
11038 return (CTL_ACTION_SKIP);
11039 default:
11040 panic("%s: Invalid serialization value %d for %d => %d",
11041 __func__, serialize_row[pending_entry->seridx],
11042 pending_entry->seridx, ooa_entry->seridx);
11043 }
11044
11045 return (CTL_ACTION_ERROR);
11046 }
11047
11048 /*
11049 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
11050 * Assumptions:
11051 * - pending_io is generally either incoming, or on the blocked queue
11052 * - starting I/O is the I/O we want to start the check with.
11053 */
11054 static ctl_action
ctl_check_ooa(struct ctl_lun * lun,union ctl_io * pending_io,union ctl_io ** starting_io)11055 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
11056 union ctl_io **starting_io)
11057 {
11058 union ctl_io *ooa_io;
11059 ctl_action action;
11060
11061 mtx_assert(&lun->lun_lock, MA_OWNED);
11062
11063 /*
11064 * Run back along the OOA queue, starting with the current
11065 * blocked I/O and going through every I/O before it on the
11066 * queue. If starting_io is NULL, we'll just end up returning
11067 * CTL_ACTION_PASS.
11068 */
11069 for (ooa_io = *starting_io; ooa_io != NULL;
11070 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
11071 ooa_links)){
11072 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
11073 if (action != CTL_ACTION_PASS) {
11074 *starting_io = ooa_io;
11075 return (action);
11076 }
11077 }
11078
11079 *starting_io = NULL;
11080 return (CTL_ACTION_PASS);
11081 }
11082
11083 /*
11084 * Try to unblock the specified I/O.
11085 *
11086 * skip parameter allows explicitly skip present blocker of the I/O,
11087 * starting from the previous one on OOA queue. It can be used when
11088 * we know for sure that the blocker I/O does no longer count.
11089 */
11090 static void
ctl_try_unblock_io(struct ctl_lun * lun,union ctl_io * io,bool skip)11091 ctl_try_unblock_io(struct ctl_lun *lun, union ctl_io *io, bool skip)
11092 {
11093 struct ctl_softc *softc = lun->ctl_softc;
11094 union ctl_io *bio, *obio;
11095 const struct ctl_cmd_entry *entry;
11096 union ctl_ha_msg msg_info;
11097 ctl_action action;
11098
11099 mtx_assert(&lun->lun_lock, MA_OWNED);
11100
11101 if (io->io_hdr.blocker == NULL)
11102 return;
11103
11104 obio = bio = io->io_hdr.blocker;
11105 if (skip)
11106 bio = (union ctl_io *)TAILQ_PREV(&bio->io_hdr, ctl_ooaq,
11107 ooa_links);
11108 action = ctl_check_ooa(lun, io, &bio);
11109 if (action == CTL_ACTION_BLOCK) {
11110 /* Still blocked, but may be by different I/O now. */
11111 if (bio != obio) {
11112 TAILQ_REMOVE(&obio->io_hdr.blocked_queue,
11113 &io->io_hdr, blocked_links);
11114 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue,
11115 &io->io_hdr, blocked_links);
11116 io->io_hdr.blocker = bio;
11117 }
11118 return;
11119 }
11120
11121 /* No longer blocked, one way or another. */
11122 TAILQ_REMOVE(&obio->io_hdr.blocked_queue, &io->io_hdr, blocked_links);
11123 io->io_hdr.blocker = NULL;
11124
11125 switch (action) {
11126 case CTL_ACTION_OVERLAP:
11127 ctl_set_overlapped_cmd(&io->scsiio);
11128 goto error;
11129 case CTL_ACTION_OVERLAP_TAG:
11130 ctl_set_overlapped_tag(&io->scsiio,
11131 io->scsiio.tag_num & 0xff);
11132 goto error;
11133 case CTL_ACTION_PASS:
11134 case CTL_ACTION_SKIP:
11135
11136 /* Serializing commands from the other SC retire there. */
11137 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) &&
11138 (softc->ha_mode != CTL_HA_MODE_XFER)) {
11139 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11140 msg_info.hdr.original_sc = io->io_hdr.remote_io;
11141 msg_info.hdr.serializing_sc = io;
11142 msg_info.hdr.msg_type = CTL_MSG_R2R;
11143 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11144 sizeof(msg_info.hdr), M_NOWAIT);
11145 break;
11146 }
11147
11148 /*
11149 * Check this I/O for LUN state changes that may have happened
11150 * while this command was blocked. The LUN state may have been
11151 * changed by a command ahead of us in the queue.
11152 */
11153 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
11154 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) {
11155 ctl_done(io);
11156 break;
11157 }
11158
11159 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11160 ctl_enqueue_rtr(io);
11161 break;
11162 case CTL_ACTION_ERROR:
11163 default:
11164 ctl_set_internal_failure(&io->scsiio,
11165 /*sks_valid*/ 0,
11166 /*retry_count*/ 0);
11167
11168 error:
11169 /* Serializing commands from the other SC are done here. */
11170 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) &&
11171 (softc->ha_mode != CTL_HA_MODE_XFER)) {
11172 ctl_try_unblock_others(lun, io, TRUE);
11173 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
11174
11175 ctl_copy_sense_data_back(io, &msg_info);
11176 msg_info.hdr.original_sc = io->io_hdr.remote_io;
11177 msg_info.hdr.serializing_sc = NULL;
11178 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
11179 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11180 sizeof(msg_info.scsi), M_WAITOK);
11181 ctl_free_io(io);
11182 break;
11183 }
11184
11185 ctl_done(io);
11186 break;
11187 }
11188 }
11189
11190 /*
11191 * Try to unblock I/Os blocked by the specified I/O.
11192 *
11193 * skip parameter allows explicitly skip the specified I/O as blocker,
11194 * starting from the previous one on the OOA queue. It can be used when
11195 * we know for sure that the specified I/O does no longer count (done).
11196 * It has to be still on OOA queue though so that we know where to start.
11197 */
11198 static void
ctl_try_unblock_others(struct ctl_lun * lun,union ctl_io * bio,bool skip)11199 ctl_try_unblock_others(struct ctl_lun *lun, union ctl_io *bio, bool skip)
11200 {
11201 union ctl_io *io, *next_io;
11202
11203 mtx_assert(&lun->lun_lock, MA_OWNED);
11204
11205 for (io = (union ctl_io *)TAILQ_FIRST(&bio->io_hdr.blocked_queue);
11206 io != NULL; io = next_io) {
11207 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, blocked_links);
11208
11209 KASSERT(io->io_hdr.blocker != NULL,
11210 ("I/O %p on blocked list without blocker", io));
11211 ctl_try_unblock_io(lun, io, skip);
11212 }
11213 KASSERT(!skip || TAILQ_EMPTY(&bio->io_hdr.blocked_queue),
11214 ("blocked_queue is not empty after skipping %p", bio));
11215 }
11216
11217 /*
11218 * This routine (with one exception) checks LUN flags that can be set by
11219 * commands ahead of us in the OOA queue. These flags have to be checked
11220 * when a command initially comes in, and when we pull a command off the
11221 * blocked queue and are preparing to execute it. The reason we have to
11222 * check these flags for commands on the blocked queue is that the LUN
11223 * state may have been changed by a command ahead of us while we're on the
11224 * blocked queue.
11225 *
11226 * Ordering is somewhat important with these checks, so please pay
11227 * careful attention to the placement of any new checks.
11228 */
11229 static int
ctl_scsiio_lun_check(struct ctl_lun * lun,const struct ctl_cmd_entry * entry,struct ctl_scsiio * ctsio)11230 ctl_scsiio_lun_check(struct ctl_lun *lun,
11231 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11232 {
11233 struct ctl_softc *softc = lun->ctl_softc;
11234 int retval;
11235 uint32_t residx;
11236
11237 retval = 0;
11238
11239 mtx_assert(&lun->lun_lock, MA_OWNED);
11240
11241 /*
11242 * If this shelf is a secondary shelf controller, we may have to
11243 * reject some commands disallowed by HA mode and link state.
11244 */
11245 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11246 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
11247 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11248 ctl_set_lun_unavail(ctsio);
11249 retval = 1;
11250 goto bailout;
11251 }
11252 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
11253 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11254 ctl_set_lun_transit(ctsio);
11255 retval = 1;
11256 goto bailout;
11257 }
11258 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
11259 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
11260 ctl_set_lun_standby(ctsio);
11261 retval = 1;
11262 goto bailout;
11263 }
11264
11265 /* The rest of checks are only done on executing side */
11266 if (softc->ha_mode == CTL_HA_MODE_XFER)
11267 goto bailout;
11268 }
11269
11270 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11271 if (lun->be_lun &&
11272 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
11273 ctl_set_hw_write_protected(ctsio);
11274 retval = 1;
11275 goto bailout;
11276 }
11277 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) {
11278 ctl_set_sense(ctsio, /*current_error*/ 1,
11279 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11280 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11281 retval = 1;
11282 goto bailout;
11283 }
11284 }
11285
11286 /*
11287 * Check for a reservation conflict. If this command isn't allowed
11288 * even on reserved LUNs, and if this initiator isn't the one who
11289 * reserved us, reject the command with a reservation conflict.
11290 */
11291 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11292 if ((lun->flags & CTL_LUN_RESERVED)
11293 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11294 if (lun->res_idx != residx) {
11295 ctl_set_reservation_conflict(ctsio);
11296 retval = 1;
11297 goto bailout;
11298 }
11299 }
11300
11301 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11302 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11303 /* No reservation or command is allowed. */;
11304 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11305 (lun->pr_res_type == SPR_TYPE_WR_EX ||
11306 lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
11307 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) {
11308 /* The command is allowed for Write Exclusive resv. */;
11309 } else {
11310 /*
11311 * if we aren't registered or it's a res holder type
11312 * reservation and this isn't the res holder then set a
11313 * conflict.
11314 */
11315 if (ctl_get_prkey(lun, residx) == 0 ||
11316 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) {
11317 ctl_set_reservation_conflict(ctsio);
11318 retval = 1;
11319 goto bailout;
11320 }
11321 }
11322
11323 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) {
11324 if (lun->flags & CTL_LUN_EJECTED)
11325 ctl_set_lun_ejected(ctsio);
11326 else if (lun->flags & CTL_LUN_NO_MEDIA) {
11327 if (lun->flags & CTL_LUN_REMOVABLE)
11328 ctl_set_lun_no_media(ctsio);
11329 else
11330 ctl_set_lun_int_reqd(ctsio);
11331 } else if (lun->flags & CTL_LUN_STOPPED)
11332 ctl_set_lun_stopped(ctsio);
11333 else
11334 goto bailout;
11335 retval = 1;
11336 goto bailout;
11337 }
11338
11339 bailout:
11340 return (retval);
11341 }
11342
11343 static void
ctl_failover_io(union ctl_io * io,int have_lock)11344 ctl_failover_io(union ctl_io *io, int have_lock)
11345 {
11346 ctl_set_busy(&io->scsiio);
11347 ctl_done(io);
11348 }
11349
11350 static void
ctl_failover_lun(union ctl_io * rio)11351 ctl_failover_lun(union ctl_io *rio)
11352 {
11353 struct ctl_softc *softc = CTL_SOFTC(rio);
11354 struct ctl_lun *lun;
11355 struct ctl_io_hdr *io, *next_io;
11356 uint32_t targ_lun;
11357
11358 targ_lun = rio->io_hdr.nexus.targ_mapped_lun;
11359 CTL_DEBUG_PRINT(("FAILOVER for lun %u\n", targ_lun));
11360
11361 /* Find and lock the LUN. */
11362 mtx_lock(&softc->ctl_lock);
11363 if (targ_lun > ctl_max_luns ||
11364 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11365 mtx_unlock(&softc->ctl_lock);
11366 return;
11367 }
11368 mtx_lock(&lun->lun_lock);
11369 mtx_unlock(&softc->ctl_lock);
11370 if (lun->flags & CTL_LUN_DISABLED) {
11371 mtx_unlock(&lun->lun_lock);
11372 return;
11373 }
11374
11375 if (softc->ha_mode == CTL_HA_MODE_XFER) {
11376 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11377 /* We are master */
11378 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11379 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11380 io->flags |= CTL_FLAG_ABORT;
11381 io->flags |= CTL_FLAG_FAILOVER;
11382 ctl_try_unblock_io(lun,
11383 (union ctl_io *)io, FALSE);
11384 } else { /* This can be only due to DATAMOVE */
11385 io->msg_type = CTL_MSG_DATAMOVE_DONE;
11386 io->flags &= ~CTL_FLAG_DMA_INPROG;
11387 io->flags |= CTL_FLAG_IO_ACTIVE;
11388 io->port_status = 31340;
11389 ctl_enqueue_isc((union ctl_io *)io);
11390 }
11391 } else
11392 /* We are slave */
11393 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11394 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11395 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11396 io->flags |= CTL_FLAG_FAILOVER;
11397 } else {
11398 ctl_set_busy(&((union ctl_io *)io)->
11399 scsiio);
11400 ctl_done((union ctl_io *)io);
11401 }
11402 }
11403 }
11404 } else { /* SERIALIZE modes */
11405 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11406 /* We are master */
11407 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11408 if (io->blocker != NULL) {
11409 TAILQ_REMOVE(&io->blocker->io_hdr.blocked_queue,
11410 io, blocked_links);
11411 io->blocker = NULL;
11412 }
11413 ctl_try_unblock_others(lun, (union ctl_io *)io,
11414 TRUE);
11415 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11416 ctl_free_io((union ctl_io *)io);
11417 } else
11418 /* We are slave */
11419 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11420 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11421 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
11422 ctl_set_busy(&((union ctl_io *)io)->
11423 scsiio);
11424 ctl_done((union ctl_io *)io);
11425 }
11426 }
11427 }
11428 }
11429 mtx_unlock(&lun->lun_lock);
11430 }
11431
11432 static int
ctl_scsiio_precheck(struct ctl_softc * softc,struct ctl_scsiio * ctsio)11433 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11434 {
11435 struct ctl_lun *lun;
11436 const struct ctl_cmd_entry *entry;
11437 union ctl_io *bio;
11438 uint32_t initidx, targ_lun;
11439 int retval = 0;
11440
11441 lun = NULL;
11442 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11443 if (targ_lun < ctl_max_luns)
11444 lun = softc->ctl_luns[targ_lun];
11445 if (lun) {
11446 /*
11447 * If the LUN is invalid, pretend that it doesn't exist.
11448 * It will go away as soon as all pending I/O has been
11449 * completed.
11450 */
11451 mtx_lock(&lun->lun_lock);
11452 if (lun->flags & CTL_LUN_DISABLED) {
11453 mtx_unlock(&lun->lun_lock);
11454 lun = NULL;
11455 }
11456 }
11457 CTL_LUN(ctsio) = lun;
11458 if (lun) {
11459 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
11460
11461 /*
11462 * Every I/O goes into the OOA queue for a particular LUN,
11463 * and stays there until completion.
11464 */
11465 #ifdef CTL_TIME_IO
11466 if (TAILQ_EMPTY(&lun->ooa_queue))
11467 lun->idle_time += getsbinuptime() - lun->last_busy;
11468 #endif
11469 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
11470 }
11471
11472 /* Get command entry and return error if it is unsuppotyed. */
11473 entry = ctl_validate_command(ctsio);
11474 if (entry == NULL) {
11475 if (lun)
11476 mtx_unlock(&lun->lun_lock);
11477 return (retval);
11478 }
11479
11480 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11481 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11482
11483 /*
11484 * Check to see whether we can send this command to LUNs that don't
11485 * exist. This should pretty much only be the case for inquiry
11486 * and request sense. Further checks, below, really require having
11487 * a LUN, so we can't really check the command anymore. Just put
11488 * it on the rtr queue.
11489 */
11490 if (lun == NULL) {
11491 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11492 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11493 ctl_enqueue_rtr((union ctl_io *)ctsio);
11494 return (retval);
11495 }
11496
11497 ctl_set_unsupported_lun(ctsio);
11498 ctl_done((union ctl_io *)ctsio);
11499 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11500 return (retval);
11501 } else {
11502 /*
11503 * Make sure we support this particular command on this LUN.
11504 * e.g., we don't support writes to the control LUN.
11505 */
11506 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11507 mtx_unlock(&lun->lun_lock);
11508 ctl_set_invalid_opcode(ctsio);
11509 ctl_done((union ctl_io *)ctsio);
11510 return (retval);
11511 }
11512 }
11513
11514 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11515
11516 /*
11517 * If we've got a request sense, it'll clear the contingent
11518 * allegiance condition. Otherwise, if we have a CA condition for
11519 * this initiator, clear it, because it sent down a command other
11520 * than request sense.
11521 */
11522 if (ctsio->cdb[0] != REQUEST_SENSE) {
11523 struct scsi_sense_data *ps;
11524
11525 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
11526 if (ps != NULL)
11527 ps[initidx % CTL_MAX_INIT_PER_PORT].error_code = 0;
11528 }
11529
11530 /*
11531 * If the command has this flag set, it handles its own unit
11532 * attention reporting, we shouldn't do anything. Otherwise we
11533 * check for any pending unit attentions, and send them back to the
11534 * initiator. We only do this when a command initially comes in,
11535 * not when we pull it off the blocked queue.
11536 *
11537 * According to SAM-3, section 5.3.2, the order that things get
11538 * presented back to the host is basically unit attentions caused
11539 * by some sort of reset event, busy status, reservation conflicts
11540 * or task set full, and finally any other status.
11541 *
11542 * One issue here is that some of the unit attentions we report
11543 * don't fall into the "reset" category (e.g. "reported luns data
11544 * has changed"). So reporting it here, before the reservation
11545 * check, may be technically wrong. I guess the only thing to do
11546 * would be to check for and report the reset events here, and then
11547 * check for the other unit attention types after we check for a
11548 * reservation conflict.
11549 *
11550 * XXX KDM need to fix this
11551 */
11552 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11553 ctl_ua_type ua_type;
11554 u_int sense_len = 0;
11555
11556 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11557 &sense_len, SSD_TYPE_NONE);
11558 if (ua_type != CTL_UA_NONE) {
11559 mtx_unlock(&lun->lun_lock);
11560 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11561 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11562 ctsio->sense_len = sense_len;
11563 ctl_done((union ctl_io *)ctsio);
11564 return (retval);
11565 }
11566 }
11567
11568
11569 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11570 mtx_unlock(&lun->lun_lock);
11571 ctl_done((union ctl_io *)ctsio);
11572 return (retval);
11573 }
11574
11575 /*
11576 * XXX CHD this is where we want to send IO to other side if
11577 * this LUN is secondary on this SC. We will need to make a copy
11578 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11579 * the copy we send as FROM_OTHER.
11580 * We also need to stuff the address of the original IO so we can
11581 * find it easily. Something similar will need be done on the other
11582 * side so when we are done we can find the copy.
11583 */
11584 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11585 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
11586 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
11587 union ctl_ha_msg msg_info;
11588 int isc_retval;
11589
11590 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11591 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11592 mtx_unlock(&lun->lun_lock);
11593
11594 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11595 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11596 msg_info.hdr.serializing_sc = NULL;
11597 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11598 msg_info.scsi.tag_num = ctsio->tag_num;
11599 msg_info.scsi.tag_type = ctsio->tag_type;
11600 msg_info.scsi.cdb_len = ctsio->cdb_len;
11601 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11602
11603 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11604 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11605 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11606 ctl_set_busy(ctsio);
11607 ctl_done((union ctl_io *)ctsio);
11608 return (retval);
11609 }
11610 return (retval);
11611 }
11612
11613 bio = (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, ooa_links);
11614 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, &bio)) {
11615 case CTL_ACTION_BLOCK:
11616 ctsio->io_hdr.blocker = bio;
11617 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctsio->io_hdr,
11618 blocked_links);
11619 mtx_unlock(&lun->lun_lock);
11620 return (retval);
11621 case CTL_ACTION_PASS:
11622 case CTL_ACTION_SKIP:
11623 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11624 mtx_unlock(&lun->lun_lock);
11625 ctl_enqueue_rtr((union ctl_io *)ctsio);
11626 break;
11627 case CTL_ACTION_OVERLAP:
11628 mtx_unlock(&lun->lun_lock);
11629 ctl_set_overlapped_cmd(ctsio);
11630 ctl_done((union ctl_io *)ctsio);
11631 break;
11632 case CTL_ACTION_OVERLAP_TAG:
11633 mtx_unlock(&lun->lun_lock);
11634 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11635 ctl_done((union ctl_io *)ctsio);
11636 break;
11637 case CTL_ACTION_ERROR:
11638 default:
11639 mtx_unlock(&lun->lun_lock);
11640 ctl_set_internal_failure(ctsio,
11641 /*sks_valid*/ 0,
11642 /*retry_count*/ 0);
11643 ctl_done((union ctl_io *)ctsio);
11644 break;
11645 }
11646 return (retval);
11647 }
11648
11649 const struct ctl_cmd_entry *
ctl_get_cmd_entry(struct ctl_scsiio * ctsio,int * sa)11650 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11651 {
11652 const struct ctl_cmd_entry *entry;
11653 int service_action;
11654
11655 entry = &ctl_cmd_table[ctsio->cdb[0]];
11656 if (sa)
11657 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11658 if (entry->flags & CTL_CMD_FLAG_SA5) {
11659 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11660 entry = &((const struct ctl_cmd_entry *)
11661 entry->execute)[service_action];
11662 }
11663 return (entry);
11664 }
11665
11666 const struct ctl_cmd_entry *
ctl_validate_command(struct ctl_scsiio * ctsio)11667 ctl_validate_command(struct ctl_scsiio *ctsio)
11668 {
11669 const struct ctl_cmd_entry *entry;
11670 int i, sa;
11671 uint8_t diff;
11672
11673 entry = ctl_get_cmd_entry(ctsio, &sa);
11674 if (entry->execute == NULL) {
11675 if (sa)
11676 ctl_set_invalid_field(ctsio,
11677 /*sks_valid*/ 1,
11678 /*command*/ 1,
11679 /*field*/ 1,
11680 /*bit_valid*/ 1,
11681 /*bit*/ 4);
11682 else
11683 ctl_set_invalid_opcode(ctsio);
11684 ctl_done((union ctl_io *)ctsio);
11685 return (NULL);
11686 }
11687 KASSERT(entry->length > 0,
11688 ("Not defined length for command 0x%02x/0x%02x",
11689 ctsio->cdb[0], ctsio->cdb[1]));
11690 for (i = 1; i < entry->length; i++) {
11691 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11692 if (diff == 0)
11693 continue;
11694 ctl_set_invalid_field(ctsio,
11695 /*sks_valid*/ 1,
11696 /*command*/ 1,
11697 /*field*/ i,
11698 /*bit_valid*/ 1,
11699 /*bit*/ fls(diff) - 1);
11700 ctl_done((union ctl_io *)ctsio);
11701 return (NULL);
11702 }
11703 return (entry);
11704 }
11705
11706 static int
ctl_cmd_applicable(uint8_t lun_type,const struct ctl_cmd_entry * entry)11707 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11708 {
11709
11710 switch (lun_type) {
11711 case T_DIRECT:
11712 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0)
11713 return (0);
11714 break;
11715 case T_PROCESSOR:
11716 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11717 return (0);
11718 break;
11719 case T_CDROM:
11720 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0)
11721 return (0);
11722 break;
11723 default:
11724 return (0);
11725 }
11726 return (1);
11727 }
11728
11729 static int
ctl_scsiio(struct ctl_scsiio * ctsio)11730 ctl_scsiio(struct ctl_scsiio *ctsio)
11731 {
11732 int retval;
11733 const struct ctl_cmd_entry *entry;
11734
11735 retval = CTL_RETVAL_COMPLETE;
11736
11737 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11738
11739 entry = ctl_get_cmd_entry(ctsio, NULL);
11740
11741 /*
11742 * If this I/O has been aborted, just send it straight to
11743 * ctl_done() without executing it.
11744 */
11745 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11746 ctl_done((union ctl_io *)ctsio);
11747 goto bailout;
11748 }
11749
11750 /*
11751 * All the checks should have been handled by ctl_scsiio_precheck().
11752 * We should be clear now to just execute the I/O.
11753 */
11754 retval = entry->execute(ctsio);
11755
11756 bailout:
11757 return (retval);
11758 }
11759
11760 static int
ctl_target_reset(union ctl_io * io)11761 ctl_target_reset(union ctl_io *io)
11762 {
11763 struct ctl_softc *softc = CTL_SOFTC(io);
11764 struct ctl_port *port = CTL_PORT(io);
11765 struct ctl_lun *lun;
11766 uint32_t initidx;
11767 ctl_ua_type ua_type;
11768
11769 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11770 union ctl_ha_msg msg_info;
11771
11772 msg_info.hdr.nexus = io->io_hdr.nexus;
11773 msg_info.task.task_action = io->taskio.task_action;
11774 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11775 msg_info.hdr.original_sc = NULL;
11776 msg_info.hdr.serializing_sc = NULL;
11777 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11778 sizeof(msg_info.task), M_WAITOK);
11779 }
11780
11781 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11782 if (io->taskio.task_action == CTL_TASK_TARGET_RESET)
11783 ua_type = CTL_UA_TARG_RESET;
11784 else
11785 ua_type = CTL_UA_BUS_RESET;
11786 mtx_lock(&softc->ctl_lock);
11787 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11788 if (port != NULL &&
11789 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
11790 continue;
11791 ctl_do_lun_reset(lun, initidx, ua_type);
11792 }
11793 mtx_unlock(&softc->ctl_lock);
11794 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11795 return (0);
11796 }
11797
11798 /*
11799 * The LUN should always be set. The I/O is optional, and is used to
11800 * distinguish between I/Os sent by this initiator, and by other
11801 * initiators. We set unit attention for initiators other than this one.
11802 * SAM-3 is vague on this point. It does say that a unit attention should
11803 * be established for other initiators when a LUN is reset (see section
11804 * 5.7.3), but it doesn't specifically say that the unit attention should
11805 * be established for this particular initiator when a LUN is reset. Here
11806 * is the relevant text, from SAM-3 rev 8:
11807 *
11808 * 5.7.2 When a SCSI initiator port aborts its own tasks
11809 *
11810 * When a SCSI initiator port causes its own task(s) to be aborted, no
11811 * notification that the task(s) have been aborted shall be returned to
11812 * the SCSI initiator port other than the completion response for the
11813 * command or task management function action that caused the task(s) to
11814 * be aborted and notification(s) associated with related effects of the
11815 * action (e.g., a reset unit attention condition).
11816 *
11817 * XXX KDM for now, we're setting unit attention for all initiators.
11818 */
11819 static void
ctl_do_lun_reset(struct ctl_lun * lun,uint32_t initidx,ctl_ua_type ua_type)11820 ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua_type)
11821 {
11822 union ctl_io *xio;
11823 int i;
11824
11825 mtx_lock(&lun->lun_lock);
11826 /* Abort tasks. */
11827 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11828 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11829 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11830 ctl_try_unblock_io(lun, xio, FALSE);
11831 }
11832 /* Clear CA. */
11833 for (i = 0; i < ctl_max_ports; i++) {
11834 free(lun->pending_sense[i], M_CTL);
11835 lun->pending_sense[i] = NULL;
11836 }
11837 /* Clear reservation. */
11838 lun->flags &= ~CTL_LUN_RESERVED;
11839 /* Clear prevent media removal. */
11840 if (lun->prevent) {
11841 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11842 ctl_clear_mask(lun->prevent, i);
11843 lun->prevent_count = 0;
11844 }
11845 /* Clear TPC status */
11846 ctl_tpc_lun_clear(lun, -1);
11847 /* Establish UA. */
11848 #if 0
11849 ctl_est_ua_all(lun, initidx, ua_type);
11850 #else
11851 ctl_est_ua_all(lun, -1, ua_type);
11852 #endif
11853 mtx_unlock(&lun->lun_lock);
11854 }
11855
11856 static int
ctl_lun_reset(union ctl_io * io)11857 ctl_lun_reset(union ctl_io *io)
11858 {
11859 struct ctl_softc *softc = CTL_SOFTC(io);
11860 struct ctl_lun *lun;
11861 uint32_t targ_lun, initidx;
11862
11863 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11864 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11865 mtx_lock(&softc->ctl_lock);
11866 if (targ_lun >= ctl_max_luns ||
11867 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11868 mtx_unlock(&softc->ctl_lock);
11869 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11870 return (1);
11871 }
11872 ctl_do_lun_reset(lun, initidx, CTL_UA_LUN_RESET);
11873 mtx_unlock(&softc->ctl_lock);
11874 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11875
11876 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
11877 union ctl_ha_msg msg_info;
11878
11879 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11880 msg_info.hdr.nexus = io->io_hdr.nexus;
11881 msg_info.task.task_action = CTL_TASK_LUN_RESET;
11882 msg_info.hdr.original_sc = NULL;
11883 msg_info.hdr.serializing_sc = NULL;
11884 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11885 sizeof(msg_info.task), M_WAITOK);
11886 }
11887 return (0);
11888 }
11889
11890 static void
ctl_abort_tasks_lun(struct ctl_lun * lun,uint32_t targ_port,uint32_t init_id,int other_sc)11891 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11892 int other_sc)
11893 {
11894 union ctl_io *xio;
11895
11896 mtx_assert(&lun->lun_lock, MA_OWNED);
11897
11898 /*
11899 * Run through the OOA queue and attempt to find the given I/O.
11900 * The target port, initiator ID, tag type and tag number have to
11901 * match the values that we got from the initiator. If we have an
11902 * untagged command to abort, simply abort the first untagged command
11903 * we come to. We only allow one untagged command at a time of course.
11904 */
11905 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11906 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11907
11908 if ((targ_port == UINT32_MAX ||
11909 targ_port == xio->io_hdr.nexus.targ_port) &&
11910 (init_id == UINT32_MAX ||
11911 init_id == xio->io_hdr.nexus.initid)) {
11912 if (targ_port != xio->io_hdr.nexus.targ_port ||
11913 init_id != xio->io_hdr.nexus.initid)
11914 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
11915 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11916 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11917 union ctl_ha_msg msg_info;
11918
11919 msg_info.hdr.nexus = xio->io_hdr.nexus;
11920 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11921 msg_info.task.tag_num = xio->scsiio.tag_num;
11922 msg_info.task.tag_type = xio->scsiio.tag_type;
11923 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11924 msg_info.hdr.original_sc = NULL;
11925 msg_info.hdr.serializing_sc = NULL;
11926 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11927 sizeof(msg_info.task), M_NOWAIT);
11928 }
11929 ctl_try_unblock_io(lun, xio, FALSE);
11930 }
11931 }
11932 }
11933
11934 static int
ctl_abort_task_set(union ctl_io * io)11935 ctl_abort_task_set(union ctl_io *io)
11936 {
11937 struct ctl_softc *softc = CTL_SOFTC(io);
11938 struct ctl_lun *lun;
11939 uint32_t targ_lun;
11940
11941 /*
11942 * Look up the LUN.
11943 */
11944 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11945 mtx_lock(&softc->ctl_lock);
11946 if (targ_lun >= ctl_max_luns ||
11947 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11948 mtx_unlock(&softc->ctl_lock);
11949 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11950 return (1);
11951 }
11952
11953 mtx_lock(&lun->lun_lock);
11954 mtx_unlock(&softc->ctl_lock);
11955 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
11956 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11957 io->io_hdr.nexus.initid,
11958 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11959 } else { /* CTL_TASK_CLEAR_TASK_SET */
11960 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
11961 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11962 }
11963 mtx_unlock(&lun->lun_lock);
11964 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11965 return (0);
11966 }
11967
11968 static void
ctl_i_t_nexus_loss(struct ctl_softc * softc,uint32_t initidx,ctl_ua_type ua_type)11969 ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx,
11970 ctl_ua_type ua_type)
11971 {
11972 struct ctl_lun *lun;
11973 struct scsi_sense_data *ps;
11974 uint32_t p, i;
11975
11976 p = initidx / CTL_MAX_INIT_PER_PORT;
11977 i = initidx % CTL_MAX_INIT_PER_PORT;
11978 mtx_lock(&softc->ctl_lock);
11979 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11980 mtx_lock(&lun->lun_lock);
11981 /* Abort tasks. */
11982 ctl_abort_tasks_lun(lun, p, i, 1);
11983 /* Clear CA. */
11984 ps = lun->pending_sense[p];
11985 if (ps != NULL)
11986 ps[i].error_code = 0;
11987 /* Clear reservation. */
11988 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
11989 lun->flags &= ~CTL_LUN_RESERVED;
11990 /* Clear prevent media removal. */
11991 if (lun->prevent && ctl_is_set(lun->prevent, initidx)) {
11992 ctl_clear_mask(lun->prevent, initidx);
11993 lun->prevent_count--;
11994 }
11995 /* Clear TPC status */
11996 ctl_tpc_lun_clear(lun, initidx);
11997 /* Establish UA. */
11998 ctl_est_ua(lun, initidx, ua_type);
11999 mtx_unlock(&lun->lun_lock);
12000 }
12001 mtx_unlock(&softc->ctl_lock);
12002 }
12003
12004 static int
ctl_i_t_nexus_reset(union ctl_io * io)12005 ctl_i_t_nexus_reset(union ctl_io *io)
12006 {
12007 struct ctl_softc *softc = CTL_SOFTC(io);
12008 uint32_t initidx;
12009
12010 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12011 union ctl_ha_msg msg_info;
12012
12013 msg_info.hdr.nexus = io->io_hdr.nexus;
12014 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
12015 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12016 msg_info.hdr.original_sc = NULL;
12017 msg_info.hdr.serializing_sc = NULL;
12018 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12019 sizeof(msg_info.task), M_WAITOK);
12020 }
12021
12022 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12023 ctl_i_t_nexus_loss(softc, initidx, CTL_UA_I_T_NEXUS_LOSS);
12024 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12025 return (0);
12026 }
12027
12028 static int
ctl_abort_task(union ctl_io * io)12029 ctl_abort_task(union ctl_io *io)
12030 {
12031 struct ctl_softc *softc = CTL_SOFTC(io);
12032 union ctl_io *xio;
12033 struct ctl_lun *lun;
12034 uint32_t targ_lun;
12035
12036 /*
12037 * Look up the LUN.
12038 */
12039 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12040 mtx_lock(&softc->ctl_lock);
12041 if (targ_lun >= ctl_max_luns ||
12042 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12043 mtx_unlock(&softc->ctl_lock);
12044 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12045 return (1);
12046 }
12047
12048 mtx_lock(&lun->lun_lock);
12049 mtx_unlock(&softc->ctl_lock);
12050 /*
12051 * Run through the OOA queue and attempt to find the given I/O.
12052 * The target port, initiator ID, tag type and tag number have to
12053 * match the values that we got from the initiator. If we have an
12054 * untagged command to abort, simply abort the first untagged command
12055 * we come to. We only allow one untagged command at a time of course.
12056 */
12057 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12058 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12059
12060 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12061 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12062 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12063 continue;
12064
12065 /*
12066 * If the abort says that the task is untagged, the
12067 * task in the queue must be untagged. Otherwise,
12068 * we just check to see whether the tag numbers
12069 * match. This is because the QLogic firmware
12070 * doesn't pass back the tag type in an abort
12071 * request.
12072 */
12073 #if 0
12074 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12075 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12076 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
12077 #else
12078 /*
12079 * XXX KDM we've got problems with FC, because it
12080 * doesn't send down a tag type with aborts. So we
12081 * can only really go by the tag number...
12082 * This may cause problems with parallel SCSI.
12083 * Need to figure that out!!
12084 */
12085 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12086 #endif
12087 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12088 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
12089 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12090 union ctl_ha_msg msg_info;
12091
12092 msg_info.hdr.nexus = io->io_hdr.nexus;
12093 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12094 msg_info.task.tag_num = io->taskio.tag_num;
12095 msg_info.task.tag_type = io->taskio.tag_type;
12096 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12097 msg_info.hdr.original_sc = NULL;
12098 msg_info.hdr.serializing_sc = NULL;
12099 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12100 sizeof(msg_info.task), M_NOWAIT);
12101 }
12102 ctl_try_unblock_io(lun, xio, FALSE);
12103 }
12104 }
12105 mtx_unlock(&lun->lun_lock);
12106 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12107 return (0);
12108 }
12109
12110 static int
12111 ctl_query_task(union ctl_io *io, int task_set)
12112 {
12113 struct ctl_softc *softc = CTL_SOFTC(io);
12114 union ctl_io *xio;
12115 struct ctl_lun *lun;
12116 int found = 0;
12117 uint32_t targ_lun;
12118
12119 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12120 mtx_lock(&softc->ctl_lock);
12121 if (targ_lun >= ctl_max_luns ||
12122 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12123 mtx_unlock(&softc->ctl_lock);
12124 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12125 return (1);
12126 }
12127 mtx_lock(&lun->lun_lock);
12128 mtx_unlock(&softc->ctl_lock);
12129 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12130 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12131
12132 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12133 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12134 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12135 continue;
12136
12137 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
12138 found = 1;
12139 break;
12140 }
12141 }
12142 mtx_unlock(&lun->lun_lock);
12143 if (found)
12144 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12145 else
12146 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12147 return (0);
12148 }
12149
12150 static int
12151 ctl_query_async_event(union ctl_io *io)
12152 {
12153 struct ctl_softc *softc = CTL_SOFTC(io);
12154 struct ctl_lun *lun;
12155 ctl_ua_type ua;
12156 uint32_t targ_lun, initidx;
12157
12158 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12159 mtx_lock(&softc->ctl_lock);
12160 if (targ_lun >= ctl_max_luns ||
12161 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12162 mtx_unlock(&softc->ctl_lock);
12163 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12164 return (1);
12165 }
12166 mtx_lock(&lun->lun_lock);
12167 mtx_unlock(&softc->ctl_lock);
12168 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12169 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
12170 mtx_unlock(&lun->lun_lock);
12171 if (ua != CTL_UA_NONE)
12172 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12173 else
12174 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12175 return (0);
12176 }
12177
12178 static void
12179 ctl_run_task(union ctl_io *io)
12180 {
12181 int retval = 1;
12182
12183 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12184 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12185 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
12186 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
12187 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
12188 switch (io->taskio.task_action) {
12189 case CTL_TASK_ABORT_TASK:
12190 retval = ctl_abort_task(io);
12191 break;
12192 case CTL_TASK_ABORT_TASK_SET:
12193 case CTL_TASK_CLEAR_TASK_SET:
12194 retval = ctl_abort_task_set(io);
12195 break;
12196 case CTL_TASK_CLEAR_ACA:
12197 break;
12198 case CTL_TASK_I_T_NEXUS_RESET:
12199 retval = ctl_i_t_nexus_reset(io);
12200 break;
12201 case CTL_TASK_LUN_RESET:
12202 retval = ctl_lun_reset(io);
12203 break;
12204 case CTL_TASK_TARGET_RESET:
12205 case CTL_TASK_BUS_RESET:
12206 retval = ctl_target_reset(io);
12207 break;
12208 case CTL_TASK_PORT_LOGIN:
12209 break;
12210 case CTL_TASK_PORT_LOGOUT:
12211 break;
12212 case CTL_TASK_QUERY_TASK:
12213 retval = ctl_query_task(io, 0);
12214 break;
12215 case CTL_TASK_QUERY_TASK_SET:
12216 retval = ctl_query_task(io, 1);
12217 break;
12218 case CTL_TASK_QUERY_ASYNC_EVENT:
12219 retval = ctl_query_async_event(io);
12220 break;
12221 default:
12222 printf("%s: got unknown task management event %d\n",
12223 __func__, io->taskio.task_action);
12224 break;
12225 }
12226 if (retval == 0)
12227 io->io_hdr.status = CTL_SUCCESS;
12228 else
12229 io->io_hdr.status = CTL_ERROR;
12230 ctl_done(io);
12231 }
12232
12233 /*
12234 * For HA operation. Handle commands that come in from the other
12235 * controller.
12236 */
12237 static void
12238 ctl_handle_isc(union ctl_io *io)
12239 {
12240 struct ctl_softc *softc = CTL_SOFTC(io);
12241 struct ctl_lun *lun;
12242 const struct ctl_cmd_entry *entry;
12243 uint32_t targ_lun;
12244
12245 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12246 switch (io->io_hdr.msg_type) {
12247 case CTL_MSG_SERIALIZE:
12248 ctl_serialize_other_sc_cmd(&io->scsiio);
12249 break;
12250 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */
12251 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12252 if (targ_lun >= ctl_max_luns ||
12253 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12254 ctl_done(io);
12255 break;
12256 }
12257 mtx_lock(&lun->lun_lock);
12258 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) {
12259 mtx_unlock(&lun->lun_lock);
12260 ctl_done(io);
12261 break;
12262 }
12263 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12264 mtx_unlock(&lun->lun_lock);
12265 ctl_enqueue_rtr(io);
12266 break;
12267 case CTL_MSG_FINISH_IO:
12268 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12269 ctl_done(io);
12270 break;
12271 }
12272 if (targ_lun >= ctl_max_luns ||
12273 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12274 ctl_free_io(io);
12275 break;
12276 }
12277 mtx_lock(&lun->lun_lock);
12278 ctl_try_unblock_others(lun, io, TRUE);
12279 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12280 mtx_unlock(&lun->lun_lock);
12281 ctl_free_io(io);
12282 break;
12283 case CTL_MSG_PERS_ACTION:
12284 ctl_hndl_per_res_out_on_other_sc(io);
12285 ctl_free_io(io);
12286 break;
12287 case CTL_MSG_BAD_JUJU:
12288 ctl_done(io);
12289 break;
12290 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */
12291 ctl_datamove_remote(io);
12292 break;
12293 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */
12294 io->scsiio.be_move_done(io);
12295 break;
12296 case CTL_MSG_FAILOVER:
12297 ctl_failover_lun(io);
12298 ctl_free_io(io);
12299 break;
12300 default:
12301 printf("%s: Invalid message type %d\n",
12302 __func__, io->io_hdr.msg_type);
12303 ctl_free_io(io);
12304 break;
12305 }
12306
12307 }
12308
12309
12310 /*
12311 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12312 * there is no match.
12313 */
12314 static ctl_lun_error_pattern
12315 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12316 {
12317 const struct ctl_cmd_entry *entry;
12318 ctl_lun_error_pattern filtered_pattern, pattern;
12319
12320 pattern = desc->error_pattern;
12321
12322 /*
12323 * XXX KDM we need more data passed into this function to match a
12324 * custom pattern, and we actually need to implement custom pattern
12325 * matching.
12326 */
12327 if (pattern & CTL_LUN_PAT_CMD)
12328 return (CTL_LUN_PAT_CMD);
12329
12330 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12331 return (CTL_LUN_PAT_ANY);
12332
12333 entry = ctl_get_cmd_entry(ctsio, NULL);
12334
12335 filtered_pattern = entry->pattern & pattern;
12336
12337 /*
12338 * If the user requested specific flags in the pattern (e.g.
12339 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12340 * flags.
12341 *
12342 * If the user did not specify any flags, it doesn't matter whether
12343 * or not the command supports the flags.
12344 */
12345 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12346 (pattern & ~CTL_LUN_PAT_MASK))
12347 return (CTL_LUN_PAT_NONE);
12348
12349 /*
12350 * If the user asked for a range check, see if the requested LBA
12351 * range overlaps with this command's LBA range.
12352 */
12353 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12354 uint64_t lba1;
12355 uint64_t len1;
12356 ctl_action action;
12357 int retval;
12358
12359 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12360 if (retval != 0)
12361 return (CTL_LUN_PAT_NONE);
12362
12363 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12364 desc->lba_range.len, FALSE);
12365 /*
12366 * A "pass" means that the LBA ranges don't overlap, so
12367 * this doesn't match the user's range criteria.
12368 */
12369 if (action == CTL_ACTION_PASS)
12370 return (CTL_LUN_PAT_NONE);
12371 }
12372
12373 return (filtered_pattern);
12374 }
12375
12376 static void
12377 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12378 {
12379 struct ctl_error_desc *desc, *desc2;
12380
12381 mtx_assert(&lun->lun_lock, MA_OWNED);
12382
12383 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12384 ctl_lun_error_pattern pattern;
12385 /*
12386 * Check to see whether this particular command matches
12387 * the pattern in the descriptor.
12388 */
12389 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12390 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12391 continue;
12392
12393 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12394 case CTL_LUN_INJ_ABORTED:
12395 ctl_set_aborted(&io->scsiio);
12396 break;
12397 case CTL_LUN_INJ_MEDIUM_ERR:
12398 ctl_set_medium_error(&io->scsiio,
12399 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
12400 CTL_FLAG_DATA_OUT);
12401 break;
12402 case CTL_LUN_INJ_UA:
12403 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12404 * OCCURRED */
12405 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12406 break;
12407 case CTL_LUN_INJ_CUSTOM:
12408 /*
12409 * We're assuming the user knows what he is doing.
12410 * Just copy the sense information without doing
12411 * checks.
12412 */
12413 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12414 MIN(sizeof(desc->custom_sense),
12415 sizeof(io->scsiio.sense_data)));
12416 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12417 io->scsiio.sense_len = SSD_FULL_SIZE;
12418 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12419 break;
12420 case CTL_LUN_INJ_NONE:
12421 default:
12422 /*
12423 * If this is an error injection type we don't know
12424 * about, clear the continuous flag (if it is set)
12425 * so it will get deleted below.
12426 */
12427 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12428 break;
12429 }
12430 /*
12431 * By default, each error injection action is a one-shot
12432 */
12433 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12434 continue;
12435
12436 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12437
12438 free(desc, M_CTL);
12439 }
12440 }
12441
12442 #ifdef CTL_IO_DELAY
12443 static void
12444 ctl_datamove_timer_wakeup(void *arg)
12445 {
12446 union ctl_io *io;
12447
12448 io = (union ctl_io *)arg;
12449
12450 ctl_datamove(io);
12451 }
12452 #endif /* CTL_IO_DELAY */
12453
12454 void
12455 ctl_datamove(union ctl_io *io)
12456 {
12457 void (*fe_datamove)(union ctl_io *io);
12458
12459 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12460
12461 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12462
12463 /* No data transferred yet. Frontend must update this when done. */
12464 io->scsiio.kern_data_resid = io->scsiio.kern_data_len;
12465
12466 #ifdef CTL_TIME_IO
12467 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12468 char str[256];
12469 char path_str[64];
12470 struct sbuf sb;
12471
12472 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12473 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12474
12475 sbuf_cat(&sb, path_str);
12476 switch (io->io_hdr.io_type) {
12477 case CTL_IO_SCSI:
12478 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12479 sbuf_printf(&sb, "\n");
12480 sbuf_cat(&sb, path_str);
12481 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12482 io->scsiio.tag_num, io->scsiio.tag_type);
12483 break;
12484 case CTL_IO_TASK:
12485 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12486 "Tag Type: %d\n", io->taskio.task_action,
12487 io->taskio.tag_num, io->taskio.tag_type);
12488 break;
12489 default:
12490 panic("%s: Invalid CTL I/O type %d\n",
12491 __func__, io->io_hdr.io_type);
12492 }
12493 sbuf_cat(&sb, path_str);
12494 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12495 (intmax_t)time_uptime - io->io_hdr.start_time);
12496 sbuf_finish(&sb);
12497 printf("%s", sbuf_data(&sb));
12498 }
12499 #endif /* CTL_TIME_IO */
12500
12501 #ifdef CTL_IO_DELAY
12502 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12503 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12504 } else {
12505 struct ctl_lun *lun;
12506
12507 lun = CTL_LUN(io);
12508 if ((lun != NULL)
12509 && (lun->delay_info.datamove_delay > 0)) {
12510
12511 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12512 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12513 callout_reset(&io->io_hdr.delay_callout,
12514 lun->delay_info.datamove_delay * hz,
12515 ctl_datamove_timer_wakeup, io);
12516 if (lun->delay_info.datamove_type ==
12517 CTL_DELAY_TYPE_ONESHOT)
12518 lun->delay_info.datamove_delay = 0;
12519 return;
12520 }
12521 }
12522 #endif
12523
12524 /*
12525 * This command has been aborted. Set the port status, so we fail
12526 * the data move.
12527 */
12528 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12529 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12530 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12531 io->io_hdr.nexus.targ_port,
12532 io->io_hdr.nexus.targ_lun);
12533 io->io_hdr.port_status = 31337;
12534 /*
12535 * Note that the backend, in this case, will get the
12536 * callback in its context. In other cases it may get
12537 * called in the frontend's interrupt thread context.
12538 */
12539 io->scsiio.be_move_done(io);
12540 return;
12541 }
12542
12543 /* Don't confuse frontend with zero length data move. */
12544 if (io->scsiio.kern_data_len == 0) {
12545 io->scsiio.be_move_done(io);
12546 return;
12547 }
12548
12549 fe_datamove = CTL_PORT(io)->fe_datamove;
12550 fe_datamove(io);
12551 }
12552
12553 static void
12554 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12555 {
12556 union ctl_ha_msg msg;
12557 #ifdef CTL_TIME_IO
12558 struct bintime cur_bt;
12559 #endif
12560
12561 memset(&msg, 0, sizeof(msg));
12562 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12563 msg.hdr.original_sc = io;
12564 msg.hdr.serializing_sc = io->io_hdr.remote_io;
12565 msg.hdr.nexus = io->io_hdr.nexus;
12566 msg.hdr.status = io->io_hdr.status;
12567 msg.scsi.kern_data_resid = io->scsiio.kern_data_resid;
12568 msg.scsi.tag_num = io->scsiio.tag_num;
12569 msg.scsi.tag_type = io->scsiio.tag_type;
12570 msg.scsi.scsi_status = io->scsiio.scsi_status;
12571 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12572 io->scsiio.sense_len);
12573 msg.scsi.sense_len = io->scsiio.sense_len;
12574 msg.scsi.port_status = io->io_hdr.port_status;
12575 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12576 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12577 ctl_failover_io(io, /*have_lock*/ have_lock);
12578 return;
12579 }
12580 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12581 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12582 msg.scsi.sense_len, M_WAITOK);
12583
12584 #ifdef CTL_TIME_IO
12585 getbinuptime(&cur_bt);
12586 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12587 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12588 #endif
12589 io->io_hdr.num_dmas++;
12590 }
12591
12592 /*
12593 * The DMA to the remote side is done, now we need to tell the other side
12594 * we're done so it can continue with its data movement.
12595 */
12596 static void
12597 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12598 {
12599 union ctl_io *io;
12600 uint32_t i;
12601
12602 io = rq->context;
12603
12604 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12605 printf("%s: ISC DMA write failed with error %d", __func__,
12606 rq->ret);
12607 ctl_set_internal_failure(&io->scsiio,
12608 /*sks_valid*/ 1,
12609 /*retry_count*/ rq->ret);
12610 }
12611
12612 ctl_dt_req_free(rq);
12613
12614 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12615 free(CTL_LSGLT(io)[i].addr, M_CTL);
12616 free(CTL_RSGL(io), M_CTL);
12617 CTL_RSGL(io) = NULL;
12618 CTL_LSGL(io) = NULL;
12619
12620 /*
12621 * The data is in local and remote memory, so now we need to send
12622 * status (good or back) back to the other side.
12623 */
12624 ctl_send_datamove_done(io, /*have_lock*/ 0);
12625 }
12626
12627 /*
12628 * We've moved the data from the host/controller into local memory. Now we
12629 * need to push it over to the remote controller's memory.
12630 */
12631 static int
12632 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12633 {
12634 int retval;
12635
12636 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12637 ctl_datamove_remote_write_cb);
12638 return (retval);
12639 }
12640
12641 static void
12642 ctl_datamove_remote_write(union ctl_io *io)
12643 {
12644 int retval;
12645 void (*fe_datamove)(union ctl_io *io);
12646
12647 /*
12648 * - Get the data from the host/HBA into local memory.
12649 * - DMA memory from the local controller to the remote controller.
12650 * - Send status back to the remote controller.
12651 */
12652
12653 retval = ctl_datamove_remote_sgl_setup(io);
12654 if (retval != 0)
12655 return;
12656
12657 /* Switch the pointer over so the FETD knows what to do */
12658 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io);
12659
12660 /*
12661 * Use a custom move done callback, since we need to send completion
12662 * back to the other controller, not to the backend on this side.
12663 */
12664 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12665
12666 fe_datamove = CTL_PORT(io)->fe_datamove;
12667 fe_datamove(io);
12668 }
12669
12670 static int
12671 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12672 {
12673 uint32_t i;
12674
12675 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12676 free(CTL_LSGLT(io)[i].addr, M_CTL);
12677 free(CTL_RSGL(io), M_CTL);
12678 CTL_RSGL(io) = NULL;
12679 CTL_LSGL(io) = NULL;
12680
12681 /*
12682 * The read is done, now we need to send status (good or bad) back
12683 * to the other side.
12684 */
12685 ctl_send_datamove_done(io, /*have_lock*/ 0);
12686
12687 return (0);
12688 }
12689
12690 static void
12691 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12692 {
12693 union ctl_io *io;
12694 void (*fe_datamove)(union ctl_io *io);
12695
12696 io = rq->context;
12697
12698 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12699 printf("%s: ISC DMA read failed with error %d\n", __func__,
12700 rq->ret);
12701 ctl_set_internal_failure(&io->scsiio,
12702 /*sks_valid*/ 1,
12703 /*retry_count*/ rq->ret);
12704 }
12705
12706 ctl_dt_req_free(rq);
12707
12708 /* Switch the pointer over so the FETD knows what to do */
12709 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io);
12710
12711 /*
12712 * Use a custom move done callback, since we need to send completion
12713 * back to the other controller, not to the backend on this side.
12714 */
12715 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12716
12717 /* XXX KDM add checks like the ones in ctl_datamove? */
12718
12719 fe_datamove = CTL_PORT(io)->fe_datamove;
12720 fe_datamove(io);
12721 }
12722
12723 static int
12724 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12725 {
12726 struct ctl_sg_entry *local_sglist;
12727 uint32_t len_to_go;
12728 int retval;
12729 int i;
12730
12731 retval = 0;
12732 local_sglist = CTL_LSGL(io);
12733 len_to_go = io->scsiio.kern_data_len;
12734
12735 /*
12736 * The difficult thing here is that the size of the various
12737 * S/G segments may be different than the size from the
12738 * remote controller. That'll make it harder when DMAing
12739 * the data back to the other side.
12740 */
12741 for (i = 0; len_to_go > 0; i++) {
12742 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12743 local_sglist[i].addr =
12744 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12745
12746 len_to_go -= local_sglist[i].len;
12747 }
12748 /*
12749 * Reset the number of S/G entries accordingly. The original
12750 * number of S/G entries is available in rem_sg_entries.
12751 */
12752 io->scsiio.kern_sg_entries = i;
12753
12754 return (retval);
12755 }
12756
12757 static int
12758 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12759 ctl_ha_dt_cb callback)
12760 {
12761 struct ctl_ha_dt_req *rq;
12762 struct ctl_sg_entry *remote_sglist, *local_sglist;
12763 uint32_t local_used, remote_used, total_used;
12764 int i, j, isc_ret;
12765
12766 rq = ctl_dt_req_alloc();
12767
12768 /*
12769 * If we failed to allocate the request, and if the DMA didn't fail
12770 * anyway, set busy status. This is just a resource allocation
12771 * failure.
12772 */
12773 if ((rq == NULL)
12774 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12775 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12776 ctl_set_busy(&io->scsiio);
12777
12778 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12779 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12780
12781 if (rq != NULL)
12782 ctl_dt_req_free(rq);
12783
12784 /*
12785 * The data move failed. We need to return status back
12786 * to the other controller. No point in trying to DMA
12787 * data to the remote controller.
12788 */
12789
12790 ctl_send_datamove_done(io, /*have_lock*/ 0);
12791
12792 return (1);
12793 }
12794
12795 local_sglist = CTL_LSGL(io);
12796 remote_sglist = CTL_RSGL(io);
12797 local_used = 0;
12798 remote_used = 0;
12799 total_used = 0;
12800
12801 /*
12802 * Pull/push the data over the wire from/to the other controller.
12803 * This takes into account the possibility that the local and
12804 * remote sglists may not be identical in terms of the size of
12805 * the elements and the number of elements.
12806 *
12807 * One fundamental assumption here is that the length allocated for
12808 * both the local and remote sglists is identical. Otherwise, we've
12809 * essentially got a coding error of some sort.
12810 */
12811 isc_ret = CTL_HA_STATUS_SUCCESS;
12812 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12813 uint32_t cur_len;
12814 uint8_t *tmp_ptr;
12815
12816 rq->command = command;
12817 rq->context = io;
12818
12819 /*
12820 * Both pointers should be aligned. But it is possible
12821 * that the allocation length is not. They should both
12822 * also have enough slack left over at the end, though,
12823 * to round up to the next 8 byte boundary.
12824 */
12825 cur_len = MIN(local_sglist[i].len - local_used,
12826 remote_sglist[j].len - remote_used);
12827 rq->size = cur_len;
12828
12829 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12830 tmp_ptr += local_used;
12831
12832 #if 0
12833 /* Use physical addresses when talking to ISC hardware */
12834 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12835 /* XXX KDM use busdma */
12836 rq->local = vtophys(tmp_ptr);
12837 } else
12838 rq->local = tmp_ptr;
12839 #else
12840 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12841 ("HA does not support BUS_ADDR"));
12842 rq->local = tmp_ptr;
12843 #endif
12844
12845 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12846 tmp_ptr += remote_used;
12847 rq->remote = tmp_ptr;
12848
12849 rq->callback = NULL;
12850
12851 local_used += cur_len;
12852 if (local_used >= local_sglist[i].len) {
12853 i++;
12854 local_used = 0;
12855 }
12856
12857 remote_used += cur_len;
12858 if (remote_used >= remote_sglist[j].len) {
12859 j++;
12860 remote_used = 0;
12861 }
12862 total_used += cur_len;
12863
12864 if (total_used >= io->scsiio.kern_data_len)
12865 rq->callback = callback;
12866
12867 isc_ret = ctl_dt_single(rq);
12868 if (isc_ret > CTL_HA_STATUS_SUCCESS)
12869 break;
12870 }
12871 if (isc_ret != CTL_HA_STATUS_WAIT) {
12872 rq->ret = isc_ret;
12873 callback(rq);
12874 }
12875
12876 return (0);
12877 }
12878
12879 static void
12880 ctl_datamove_remote_read(union ctl_io *io)
12881 {
12882 int retval;
12883 uint32_t i;
12884
12885 /*
12886 * This will send an error to the other controller in the case of a
12887 * failure.
12888 */
12889 retval = ctl_datamove_remote_sgl_setup(io);
12890 if (retval != 0)
12891 return;
12892
12893 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12894 ctl_datamove_remote_read_cb);
12895 if (retval != 0) {
12896 /*
12897 * Make sure we free memory if there was an error.. The
12898 * ctl_datamove_remote_xfer() function will send the
12899 * datamove done message, or call the callback with an
12900 * error if there is a problem.
12901 */
12902 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12903 free(CTL_LSGLT(io)[i].addr, M_CTL);
12904 free(CTL_RSGL(io), M_CTL);
12905 CTL_RSGL(io) = NULL;
12906 CTL_LSGL(io) = NULL;
12907 }
12908 }
12909
12910 /*
12911 * Process a datamove request from the other controller. This is used for
12912 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12913 * first. Once that is complete, the data gets DMAed into the remote
12914 * controller's memory. For reads, we DMA from the remote controller's
12915 * memory into our memory first, and then move it out to the FETD.
12916 */
12917 static void
12918 ctl_datamove_remote(union ctl_io *io)
12919 {
12920
12921 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12922
12923 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12924 ctl_failover_io(io, /*have_lock*/ 0);
12925 return;
12926 }
12927
12928 /*
12929 * Note that we look for an aborted I/O here, but don't do some of
12930 * the other checks that ctl_datamove() normally does.
12931 * We don't need to run the datamove delay code, since that should
12932 * have been done if need be on the other controller.
12933 */
12934 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12935 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
12936 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12937 io->io_hdr.nexus.targ_port,
12938 io->io_hdr.nexus.targ_lun);
12939 io->io_hdr.port_status = 31338;
12940 ctl_send_datamove_done(io, /*have_lock*/ 0);
12941 return;
12942 }
12943
12944 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
12945 ctl_datamove_remote_write(io);
12946 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
12947 ctl_datamove_remote_read(io);
12948 else {
12949 io->io_hdr.port_status = 31339;
12950 ctl_send_datamove_done(io, /*have_lock*/ 0);
12951 }
12952 }
12953
12954 static void
12955 ctl_process_done(union ctl_io *io)
12956 {
12957 struct ctl_softc *softc = CTL_SOFTC(io);
12958 struct ctl_port *port = CTL_PORT(io);
12959 struct ctl_lun *lun = CTL_LUN(io);
12960 void (*fe_done)(union ctl_io *io);
12961 union ctl_ha_msg msg;
12962
12963 CTL_DEBUG_PRINT(("ctl_process_done\n"));
12964 fe_done = port->fe_done;
12965
12966 #ifdef CTL_TIME_IO
12967 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12968 char str[256];
12969 char path_str[64];
12970 struct sbuf sb;
12971
12972 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12973 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12974
12975 sbuf_cat(&sb, path_str);
12976 switch (io->io_hdr.io_type) {
12977 case CTL_IO_SCSI:
12978 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12979 sbuf_printf(&sb, "\n");
12980 sbuf_cat(&sb, path_str);
12981 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12982 io->scsiio.tag_num, io->scsiio.tag_type);
12983 break;
12984 case CTL_IO_TASK:
12985 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12986 "Tag Type: %d\n", io->taskio.task_action,
12987 io->taskio.tag_num, io->taskio.tag_type);
12988 break;
12989 default:
12990 panic("%s: Invalid CTL I/O type %d\n",
12991 __func__, io->io_hdr.io_type);
12992 }
12993 sbuf_cat(&sb, path_str);
12994 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12995 (intmax_t)time_uptime - io->io_hdr.start_time);
12996 sbuf_finish(&sb);
12997 printf("%s", sbuf_data(&sb));
12998 }
12999 #endif /* CTL_TIME_IO */
13000
13001 switch (io->io_hdr.io_type) {
13002 case CTL_IO_SCSI:
13003 break;
13004 case CTL_IO_TASK:
13005 if (ctl_debug & CTL_DEBUG_INFO)
13006 ctl_io_error_print(io, NULL);
13007 fe_done(io);
13008 return;
13009 default:
13010 panic("%s: Invalid CTL I/O type %d\n",
13011 __func__, io->io_hdr.io_type);
13012 }
13013
13014 if (lun == NULL) {
13015 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13016 io->io_hdr.nexus.targ_mapped_lun));
13017 goto bailout;
13018 }
13019
13020 mtx_lock(&lun->lun_lock);
13021
13022 /*
13023 * Check to see if we have any informational exception and status
13024 * of this command can be modified to report it in form of either
13025 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field.
13026 */
13027 if (lun->ie_reported == 0 && lun->ie_asc != 0 &&
13028 io->io_hdr.status == CTL_SUCCESS &&
13029 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) {
13030 uint8_t mrie = lun->MODE_IE.mrie;
13031 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) ||
13032 (lun->MODE_VER.byte3 & SMS_VER_PER));
13033 if (((mrie == SIEP_MRIE_REC_COND && per) ||
13034 mrie == SIEP_MRIE_REC_UNCOND ||
13035 mrie == SIEP_MRIE_NO_SENSE) &&
13036 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags &
13037 CTL_CMD_FLAG_NO_SENSE) == 0) {
13038 ctl_set_sense(&io->scsiio,
13039 /*current_error*/ 1,
13040 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ?
13041 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR,
13042 /*asc*/ lun->ie_asc,
13043 /*ascq*/ lun->ie_ascq,
13044 SSD_ELEM_NONE);
13045 lun->ie_reported = 1;
13046 }
13047 } else if (lun->ie_reported < 0)
13048 lun->ie_reported = 0;
13049
13050 /*
13051 * Check to see if we have any errors to inject here. We only
13052 * inject errors for commands that don't already have errors set.
13053 */
13054 if (!STAILQ_EMPTY(&lun->error_list) &&
13055 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
13056 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
13057 ctl_inject_error(lun, io);
13058
13059 /*
13060 * XXX KDM how do we treat commands that aren't completed
13061 * successfully?
13062 *
13063 * XXX KDM should we also track I/O latency?
13064 */
13065 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13066 io->io_hdr.io_type == CTL_IO_SCSI) {
13067 int type;
13068 #ifdef CTL_TIME_IO
13069 struct bintime bt;
13070
13071 getbinuptime(&bt);
13072 bintime_sub(&bt, &io->io_hdr.start_bt);
13073 #endif
13074 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13075 CTL_FLAG_DATA_IN)
13076 type = CTL_STATS_READ;
13077 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13078 CTL_FLAG_DATA_OUT)
13079 type = CTL_STATS_WRITE;
13080 else
13081 type = CTL_STATS_NO_IO;
13082
13083 lun->stats.bytes[type] += io->scsiio.kern_total_len;
13084 lun->stats.operations[type] ++;
13085 lun->stats.dmas[type] += io->io_hdr.num_dmas;
13086 #ifdef CTL_TIME_IO
13087 bintime_add(&lun->stats.dma_time[type], &io->io_hdr.dma_bt);
13088 bintime_add(&lun->stats.time[type], &bt);
13089 #endif
13090
13091 mtx_lock(&port->port_lock);
13092 port->stats.bytes[type] += io->scsiio.kern_total_len;
13093 port->stats.operations[type] ++;
13094 port->stats.dmas[type] += io->io_hdr.num_dmas;
13095 #ifdef CTL_TIME_IO
13096 bintime_add(&port->stats.dma_time[type], &io->io_hdr.dma_bt);
13097 bintime_add(&port->stats.time[type], &bt);
13098 #endif
13099 mtx_unlock(&port->port_lock);
13100 }
13101
13102 /*
13103 * Run through the blocked queue of this I/O and see if anything
13104 * can be unblocked, now that this I/O is done and will be removed.
13105 * We need to do it before removal to have OOA position to start.
13106 */
13107 ctl_try_unblock_others(lun, io, TRUE);
13108
13109 /*
13110 * Remove this from the OOA queue.
13111 */
13112 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13113 #ifdef CTL_TIME_IO
13114 if (TAILQ_EMPTY(&lun->ooa_queue))
13115 lun->last_busy = getsbinuptime();
13116 #endif
13117
13118 /*
13119 * If the LUN has been invalidated, free it if there is nothing
13120 * left on its OOA queue.
13121 */
13122 if ((lun->flags & CTL_LUN_INVALID)
13123 && TAILQ_EMPTY(&lun->ooa_queue)) {
13124 mtx_unlock(&lun->lun_lock);
13125 ctl_free_lun(lun);
13126 } else
13127 mtx_unlock(&lun->lun_lock);
13128
13129 bailout:
13130
13131 /*
13132 * If this command has been aborted, make sure we set the status
13133 * properly. The FETD is responsible for freeing the I/O and doing
13134 * whatever it needs to do to clean up its state.
13135 */
13136 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13137 ctl_set_task_aborted(&io->scsiio);
13138
13139 /*
13140 * If enabled, print command error status.
13141 */
13142 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
13143 (ctl_debug & CTL_DEBUG_INFO) != 0)
13144 ctl_io_error_print(io, NULL);
13145
13146 /*
13147 * Tell the FETD or the other shelf controller we're done with this
13148 * command. Note that only SCSI commands get to this point. Task
13149 * management commands are completed above.
13150 */
13151 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
13152 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
13153 memset(&msg, 0, sizeof(msg));
13154 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13155 msg.hdr.serializing_sc = io->io_hdr.remote_io;
13156 msg.hdr.nexus = io->io_hdr.nexus;
13157 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13158 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
13159 M_WAITOK);
13160 }
13161
13162 fe_done(io);
13163 }
13164
13165 /*
13166 * Front end should call this if it doesn't do autosense. When the request
13167 * sense comes back in from the initiator, we'll dequeue this and send it.
13168 */
13169 int
13170 ctl_queue_sense(union ctl_io *io)
13171 {
13172 struct ctl_softc *softc = CTL_SOFTC(io);
13173 struct ctl_port *port = CTL_PORT(io);
13174 struct ctl_lun *lun;
13175 struct scsi_sense_data *ps;
13176 uint32_t initidx, p, targ_lun;
13177
13178 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13179
13180 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13181
13182 /*
13183 * LUN lookup will likely move to the ctl_work_thread() once we
13184 * have our new queueing infrastructure (that doesn't put things on
13185 * a per-LUN queue initially). That is so that we can handle
13186 * things like an INQUIRY to a LUN that we don't have enabled. We
13187 * can't deal with that right now.
13188 * If we don't have a LUN for this, just toss the sense information.
13189 */
13190 mtx_lock(&softc->ctl_lock);
13191 if (targ_lun >= ctl_max_luns ||
13192 (lun = softc->ctl_luns[targ_lun]) == NULL) {
13193 mtx_unlock(&softc->ctl_lock);
13194 goto bailout;
13195 }
13196 mtx_lock(&lun->lun_lock);
13197 mtx_unlock(&softc->ctl_lock);
13198
13199 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13200 p = initidx / CTL_MAX_INIT_PER_PORT;
13201 if (lun->pending_sense[p] == NULL) {
13202 lun->pending_sense[p] = malloc(sizeof(*ps) * CTL_MAX_INIT_PER_PORT,
13203 M_CTL, M_NOWAIT | M_ZERO);
13204 }
13205 if ((ps = lun->pending_sense[p]) != NULL) {
13206 ps += initidx % CTL_MAX_INIT_PER_PORT;
13207 memset(ps, 0, sizeof(*ps));
13208 memcpy(ps, &io->scsiio.sense_data, io->scsiio.sense_len);
13209 }
13210 mtx_unlock(&lun->lun_lock);
13211
13212 bailout:
13213 ctl_free_io(io);
13214 return (CTL_RETVAL_COMPLETE);
13215 }
13216
13217 /*
13218 * Primary command inlet from frontend ports. All SCSI and task I/O
13219 * requests must go through this function.
13220 */
13221 int
13222 ctl_queue(union ctl_io *io)
13223 {
13224 struct ctl_port *port = CTL_PORT(io);
13225
13226 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13227
13228 #ifdef CTL_TIME_IO
13229 io->io_hdr.start_time = time_uptime;
13230 getbinuptime(&io->io_hdr.start_bt);
13231 #endif /* CTL_TIME_IO */
13232
13233 /* Map FE-specific LUN ID into global one. */
13234 io->io_hdr.nexus.targ_mapped_lun =
13235 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13236
13237 switch (io->io_hdr.io_type) {
13238 case CTL_IO_SCSI:
13239 case CTL_IO_TASK:
13240 if (ctl_debug & CTL_DEBUG_CDB)
13241 ctl_io_print(io);
13242 ctl_enqueue_incoming(io);
13243 break;
13244 default:
13245 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13246 return (EINVAL);
13247 }
13248
13249 return (CTL_RETVAL_COMPLETE);
13250 }
13251
13252 #ifdef CTL_IO_DELAY
13253 static void
13254 ctl_done_timer_wakeup(void *arg)
13255 {
13256 union ctl_io *io;
13257
13258 io = (union ctl_io *)arg;
13259 ctl_done(io);
13260 }
13261 #endif /* CTL_IO_DELAY */
13262
13263 void
13264 ctl_serseq_done(union ctl_io *io)
13265 {
13266 struct ctl_lun *lun = CTL_LUN(io);;
13267
13268 if (lun->be_lun == NULL ||
13269 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF)
13270 return;
13271 mtx_lock(&lun->lun_lock);
13272 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13273 ctl_try_unblock_others(lun, io, FALSE);
13274 mtx_unlock(&lun->lun_lock);
13275 }
13276
13277 void
13278 ctl_done(union ctl_io *io)
13279 {
13280
13281 /*
13282 * Enable this to catch duplicate completion issues.
13283 */
13284 #if 0
13285 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13286 printf("%s: type %d msg %d cdb %x iptl: "
13287 "%u:%u:%u tag 0x%04x "
13288 "flag %#x status %x\n",
13289 __func__,
13290 io->io_hdr.io_type,
13291 io->io_hdr.msg_type,
13292 io->scsiio.cdb[0],
13293 io->io_hdr.nexus.initid,
13294 io->io_hdr.nexus.targ_port,
13295 io->io_hdr.nexus.targ_lun,
13296 (io->io_hdr.io_type ==
13297 CTL_IO_TASK) ?
13298 io->taskio.tag_num :
13299 io->scsiio.tag_num,
13300 io->io_hdr.flags,
13301 io->io_hdr.status);
13302 } else
13303 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13304 #endif
13305
13306 /*
13307 * This is an internal copy of an I/O, and should not go through
13308 * the normal done processing logic.
13309 */
13310 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13311 return;
13312
13313 #ifdef CTL_IO_DELAY
13314 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13315 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13316 } else {
13317 struct ctl_lun *lun = CTL_LUN(io);
13318
13319 if ((lun != NULL)
13320 && (lun->delay_info.done_delay > 0)) {
13321
13322 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13323 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13324 callout_reset(&io->io_hdr.delay_callout,
13325 lun->delay_info.done_delay * hz,
13326 ctl_done_timer_wakeup, io);
13327 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13328 lun->delay_info.done_delay = 0;
13329 return;
13330 }
13331 }
13332 #endif /* CTL_IO_DELAY */
13333
13334 ctl_enqueue_done(io);
13335 }
13336
13337 static void
13338 ctl_work_thread(void *arg)
13339 {
13340 struct ctl_thread *thr = (struct ctl_thread *)arg;
13341 struct ctl_softc *softc = thr->ctl_softc;
13342 union ctl_io *io;
13343 int retval;
13344
13345 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13346 thread_lock(curthread);
13347 sched_prio(curthread, PUSER - 1);
13348 thread_unlock(curthread);
13349
13350 while (!softc->shutdown) {
13351 /*
13352 * We handle the queues in this order:
13353 * - ISC
13354 * - done queue (to free up resources, unblock other commands)
13355 * - incoming queue
13356 * - RtR queue
13357 *
13358 * If those queues are empty, we break out of the loop and
13359 * go to sleep.
13360 */
13361 mtx_lock(&thr->queue_lock);
13362 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13363 if (io != NULL) {
13364 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13365 mtx_unlock(&thr->queue_lock);
13366 ctl_handle_isc(io);
13367 continue;
13368 }
13369 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13370 if (io != NULL) {
13371 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13372 /* clear any blocked commands, call fe_done */
13373 mtx_unlock(&thr->queue_lock);
13374 ctl_process_done(io);
13375 continue;
13376 }
13377 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13378 if (io != NULL) {
13379 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13380 mtx_unlock(&thr->queue_lock);
13381 if (io->io_hdr.io_type == CTL_IO_TASK)
13382 ctl_run_task(io);
13383 else
13384 ctl_scsiio_precheck(softc, &io->scsiio);
13385 continue;
13386 }
13387 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13388 if (io != NULL) {
13389 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13390 mtx_unlock(&thr->queue_lock);
13391 retval = ctl_scsiio(&io->scsiio);
13392 if (retval != CTL_RETVAL_COMPLETE)
13393 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13394 continue;
13395 }
13396
13397 /* Sleep until we have something to do. */
13398 mtx_sleep(thr, &thr->queue_lock, PDROP, "-", 0);
13399 }
13400 thr->thread = NULL;
13401 kthread_exit();
13402 }
13403
13404 static void
13405 ctl_lun_thread(void *arg)
13406 {
13407 struct ctl_softc *softc = (struct ctl_softc *)arg;
13408 struct ctl_be_lun *be_lun;
13409
13410 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13411 thread_lock(curthread);
13412 sched_prio(curthread, PUSER - 1);
13413 thread_unlock(curthread);
13414
13415 while (!softc->shutdown) {
13416 mtx_lock(&softc->ctl_lock);
13417 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13418 if (be_lun != NULL) {
13419 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13420 mtx_unlock(&softc->ctl_lock);
13421 ctl_create_lun(be_lun);
13422 continue;
13423 }
13424
13425 /* Sleep until we have something to do. */
13426 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13427 PDROP, "-", 0);
13428 }
13429 softc->lun_thread = NULL;
13430 kthread_exit();
13431 }
13432
13433 static void
13434 ctl_thresh_thread(void *arg)
13435 {
13436 struct ctl_softc *softc = (struct ctl_softc *)arg;
13437 struct ctl_lun *lun;
13438 struct ctl_logical_block_provisioning_page *page;
13439 const char *attr;
13440 union ctl_ha_msg msg;
13441 uint64_t thres, val;
13442 int i, e, set;
13443
13444 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13445 thread_lock(curthread);
13446 sched_prio(curthread, PUSER - 1);
13447 thread_unlock(curthread);
13448
13449 while (!softc->shutdown) {
13450 mtx_lock(&softc->ctl_lock);
13451 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13452 if ((lun->flags & CTL_LUN_DISABLED) ||
13453 (lun->flags & CTL_LUN_NO_MEDIA) ||
13454 lun->backend->lun_attr == NULL)
13455 continue;
13456 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13457 softc->ha_mode == CTL_HA_MODE_XFER)
13458 continue;
13459 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0)
13460 continue;
13461 e = 0;
13462 page = &lun->MODE_LBP;
13463 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13464 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13465 continue;
13466 thres = scsi_4btoul(page->descr[i].count);
13467 thres <<= CTL_LBP_EXPONENT;
13468 switch (page->descr[i].resource) {
13469 case 0x01:
13470 attr = "blocksavail";
13471 break;
13472 case 0x02:
13473 attr = "blocksused";
13474 break;
13475 case 0xf1:
13476 attr = "poolblocksavail";
13477 break;
13478 case 0xf2:
13479 attr = "poolblocksused";
13480 break;
13481 default:
13482 continue;
13483 }
13484 mtx_unlock(&softc->ctl_lock); // XXX
13485 val = lun->backend->lun_attr(
13486 lun->be_lun->be_lun, attr);
13487 mtx_lock(&softc->ctl_lock);
13488 if (val == UINT64_MAX)
13489 continue;
13490 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13491 == SLBPPD_ARMING_INC)
13492 e = (val >= thres);
13493 else
13494 e = (val <= thres);
13495 if (e)
13496 break;
13497 }
13498 mtx_lock(&lun->lun_lock);
13499 if (e) {
13500 scsi_u64to8b((uint8_t *)&page->descr[i] -
13501 (uint8_t *)page, lun->ua_tpt_info);
13502 if (lun->lasttpt == 0 ||
13503 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13504 lun->lasttpt = time_uptime;
13505 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13506 set = 1;
13507 } else
13508 set = 0;
13509 } else {
13510 lun->lasttpt = 0;
13511 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13512 set = -1;
13513 }
13514 mtx_unlock(&lun->lun_lock);
13515 if (set != 0 &&
13516 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13517 /* Send msg to other side. */
13518 bzero(&msg.ua, sizeof(msg.ua));
13519 msg.hdr.msg_type = CTL_MSG_UA;
13520 msg.hdr.nexus.initid = -1;
13521 msg.hdr.nexus.targ_port = -1;
13522 msg.hdr.nexus.targ_lun = lun->lun;
13523 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13524 msg.ua.ua_all = 1;
13525 msg.ua.ua_set = (set > 0);
13526 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13527 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
13528 mtx_unlock(&softc->ctl_lock); // XXX
13529 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13530 sizeof(msg.ua), M_WAITOK);
13531 mtx_lock(&softc->ctl_lock);
13532 }
13533 }
13534 mtx_sleep(&softc->thresh_thread, &softc->ctl_lock,
13535 PDROP, "-", CTL_LBP_PERIOD * hz);
13536 }
13537 softc->thresh_thread = NULL;
13538 kthread_exit();
13539 }
13540
13541 static void
13542 ctl_enqueue_incoming(union ctl_io *io)
13543 {
13544 struct ctl_softc *softc = CTL_SOFTC(io);
13545 struct ctl_thread *thr;
13546 u_int idx;
13547
13548 idx = (io->io_hdr.nexus.targ_port * 127 +
13549 io->io_hdr.nexus.initid) % worker_threads;
13550 thr = &softc->threads[idx];
13551 mtx_lock(&thr->queue_lock);
13552 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13553 mtx_unlock(&thr->queue_lock);
13554 wakeup(thr);
13555 }
13556
13557 static void
13558 ctl_enqueue_rtr(union ctl_io *io)
13559 {
13560 struct ctl_softc *softc = CTL_SOFTC(io);
13561 struct ctl_thread *thr;
13562
13563 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13564 mtx_lock(&thr->queue_lock);
13565 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13566 mtx_unlock(&thr->queue_lock);
13567 wakeup(thr);
13568 }
13569
13570 static void
13571 ctl_enqueue_done(union ctl_io *io)
13572 {
13573 struct ctl_softc *softc = CTL_SOFTC(io);
13574 struct ctl_thread *thr;
13575
13576 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13577 mtx_lock(&thr->queue_lock);
13578 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13579 mtx_unlock(&thr->queue_lock);
13580 wakeup(thr);
13581 }
13582
13583 static void
13584 ctl_enqueue_isc(union ctl_io *io)
13585 {
13586 struct ctl_softc *softc = CTL_SOFTC(io);
13587 struct ctl_thread *thr;
13588
13589 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13590 mtx_lock(&thr->queue_lock);
13591 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13592 mtx_unlock(&thr->queue_lock);
13593 wakeup(thr);
13594 }
13595
13596 /*
13597 * vim: ts=8
13598 */
13599