1 /*-
2 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3 * Copyright (c) 2012 The FreeBSD Foundation
4 * Copyright (c) 2014-2017 Alexander Motin <mav@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Portions of this software were developed by Edward Tomasz Napierala
8 * under sponsorship from the FreeBSD Foundation.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification.
16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17 * substantially similar to the "NO WARRANTY" disclaimer below
18 * ("Disclaimer") and any redistribution must be conditioned upon
19 * including a substantially similar Disclaimer requirement for further
20 * binary redistribution.
21 *
22 * NO WARRANTY
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGES.
34 *
35 * $Id$
36 */
37 /*
38 * CAM Target Layer, a SCSI device emulation subsystem.
39 *
40 * Author: Ken Merry <ken@FreeBSD.org>
41 */
42
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/ctype.h>
49 #include <sys/kernel.h>
50 #include <sys/types.h>
51 #include <sys/kthread.h>
52 #include <sys/bio.h>
53 #include <sys/fcntl.h>
54 #include <sys/lock.h>
55 #include <sys/module.h>
56 #include <sys/mutex.h>
57 #include <sys/condvar.h>
58 #include <sys/malloc.h>
59 #include <sys/conf.h>
60 #include <sys/ioccom.h>
61 #include <sys/queue.h>
62 #include <sys/sbuf.h>
63 #include <sys/smp.h>
64 #include <sys/endian.h>
65 #include <sys/proc.h>
66 #include <sys/sched.h>
67 #include <sys/sysctl.h>
68 #include <vm/uma.h>
69
70 #include <cam/cam.h>
71 #include <cam/scsi/scsi_all.h>
72 #include <cam/scsi/scsi_cd.h>
73 #include <cam/scsi/scsi_da.h>
74 #include <cam/ctl/ctl_io.h>
75 #include <cam/ctl/ctl.h>
76 #include <cam/ctl/ctl_frontend.h>
77 #include <cam/ctl/ctl_util.h>
78 #include <cam/ctl/ctl_backend.h>
79 #include <cam/ctl/ctl_ioctl.h>
80 #include <cam/ctl/ctl_ha.h>
81 #include <cam/ctl/ctl_private.h>
82 #include <cam/ctl/ctl_debug.h>
83 #include <cam/ctl/ctl_scsi_all.h>
84 #include <cam/ctl/ctl_error.h>
85
86 struct ctl_softc *control_softc = NULL;
87
88 /*
89 * Template mode pages.
90 */
91
92 /*
93 * Note that these are default values only. The actual values will be
94 * filled in when the user does a mode sense.
95 */
96 const static struct scsi_da_rw_recovery_page rw_er_page_default = {
97 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
98 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
99 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
100 /*read_retry_count*/0,
101 /*correction_span*/0,
102 /*head_offset_count*/0,
103 /*data_strobe_offset_cnt*/0,
104 /*byte8*/SMS_RWER_LBPERE,
105 /*write_retry_count*/0,
106 /*reserved2*/0,
107 /*recovery_time_limit*/{0, 0},
108 };
109
110 const static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
111 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
112 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
113 /*byte3*/SMS_RWER_PER,
114 /*read_retry_count*/0,
115 /*correction_span*/0,
116 /*head_offset_count*/0,
117 /*data_strobe_offset_cnt*/0,
118 /*byte8*/SMS_RWER_LBPERE,
119 /*write_retry_count*/0,
120 /*reserved2*/0,
121 /*recovery_time_limit*/{0, 0},
122 };
123
124 const static struct scsi_format_page format_page_default = {
125 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
126 /*page_length*/sizeof(struct scsi_format_page) - 2,
127 /*tracks_per_zone*/ {0, 0},
128 /*alt_sectors_per_zone*/ {0, 0},
129 /*alt_tracks_per_zone*/ {0, 0},
130 /*alt_tracks_per_lun*/ {0, 0},
131 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
132 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
133 /*bytes_per_sector*/ {0, 0},
134 /*interleave*/ {0, 0},
135 /*track_skew*/ {0, 0},
136 /*cylinder_skew*/ {0, 0},
137 /*flags*/ SFP_HSEC,
138 /*reserved*/ {0, 0, 0}
139 };
140
141 const static struct scsi_format_page format_page_changeable = {
142 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
143 /*page_length*/sizeof(struct scsi_format_page) - 2,
144 /*tracks_per_zone*/ {0, 0},
145 /*alt_sectors_per_zone*/ {0, 0},
146 /*alt_tracks_per_zone*/ {0, 0},
147 /*alt_tracks_per_lun*/ {0, 0},
148 /*sectors_per_track*/ {0, 0},
149 /*bytes_per_sector*/ {0, 0},
150 /*interleave*/ {0, 0},
151 /*track_skew*/ {0, 0},
152 /*cylinder_skew*/ {0, 0},
153 /*flags*/ 0,
154 /*reserved*/ {0, 0, 0}
155 };
156
157 const static struct scsi_rigid_disk_page rigid_disk_page_default = {
158 /*page_code*/SMS_RIGID_DISK_PAGE,
159 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
160 /*cylinders*/ {0, 0, 0},
161 /*heads*/ CTL_DEFAULT_HEADS,
162 /*start_write_precomp*/ {0, 0, 0},
163 /*start_reduced_current*/ {0, 0, 0},
164 /*step_rate*/ {0, 0},
165 /*landing_zone_cylinder*/ {0, 0, 0},
166 /*rpl*/ SRDP_RPL_DISABLED,
167 /*rotational_offset*/ 0,
168 /*reserved1*/ 0,
169 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
170 CTL_DEFAULT_ROTATION_RATE & 0xff},
171 /*reserved2*/ {0, 0}
172 };
173
174 const static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
175 /*page_code*/SMS_RIGID_DISK_PAGE,
176 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
177 /*cylinders*/ {0, 0, 0},
178 /*heads*/ 0,
179 /*start_write_precomp*/ {0, 0, 0},
180 /*start_reduced_current*/ {0, 0, 0},
181 /*step_rate*/ {0, 0},
182 /*landing_zone_cylinder*/ {0, 0, 0},
183 /*rpl*/ 0,
184 /*rotational_offset*/ 0,
185 /*reserved1*/ 0,
186 /*rotation_rate*/ {0, 0},
187 /*reserved2*/ {0, 0}
188 };
189
190 const static struct scsi_da_verify_recovery_page verify_er_page_default = {
191 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
192 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
193 /*byte3*/0,
194 /*read_retry_count*/0,
195 /*reserved*/{ 0, 0, 0, 0, 0, 0 },
196 /*recovery_time_limit*/{0, 0},
197 };
198
199 const static struct scsi_da_verify_recovery_page verify_er_page_changeable = {
200 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
201 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
202 /*byte3*/SMS_VER_PER,
203 /*read_retry_count*/0,
204 /*reserved*/{ 0, 0, 0, 0, 0, 0 },
205 /*recovery_time_limit*/{0, 0},
206 };
207
208 const static struct scsi_caching_page caching_page_default = {
209 /*page_code*/SMS_CACHING_PAGE,
210 /*page_length*/sizeof(struct scsi_caching_page) - 2,
211 /*flags1*/ SCP_DISC | SCP_WCE,
212 /*ret_priority*/ 0,
213 /*disable_pf_transfer_len*/ {0xff, 0xff},
214 /*min_prefetch*/ {0, 0},
215 /*max_prefetch*/ {0xff, 0xff},
216 /*max_pf_ceiling*/ {0xff, 0xff},
217 /*flags2*/ 0,
218 /*cache_segments*/ 0,
219 /*cache_seg_size*/ {0, 0},
220 /*reserved*/ 0,
221 /*non_cache_seg_size*/ {0, 0, 0}
222 };
223
224 const static struct scsi_caching_page caching_page_changeable = {
225 /*page_code*/SMS_CACHING_PAGE,
226 /*page_length*/sizeof(struct scsi_caching_page) - 2,
227 /*flags1*/ SCP_WCE | SCP_RCD,
228 /*ret_priority*/ 0,
229 /*disable_pf_transfer_len*/ {0, 0},
230 /*min_prefetch*/ {0, 0},
231 /*max_prefetch*/ {0, 0},
232 /*max_pf_ceiling*/ {0, 0},
233 /*flags2*/ 0,
234 /*cache_segments*/ 0,
235 /*cache_seg_size*/ {0, 0},
236 /*reserved*/ 0,
237 /*non_cache_seg_size*/ {0, 0, 0}
238 };
239
240 const static struct scsi_control_page control_page_default = {
241 /*page_code*/SMS_CONTROL_MODE_PAGE,
242 /*page_length*/sizeof(struct scsi_control_page) - 2,
243 /*rlec*/0,
244 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
245 /*eca_and_aen*/0,
246 /*flags4*/SCP_TAS,
247 /*aen_holdoff_period*/{0, 0},
248 /*busy_timeout_period*/{0, 0},
249 /*extended_selftest_completion_time*/{0, 0}
250 };
251
252 const static struct scsi_control_page control_page_changeable = {
253 /*page_code*/SMS_CONTROL_MODE_PAGE,
254 /*page_length*/sizeof(struct scsi_control_page) - 2,
255 /*rlec*/SCP_DSENSE,
256 /*queue_flags*/SCP_QUEUE_ALG_MASK | SCP_NUAR,
257 /*eca_and_aen*/SCP_SWP,
258 /*flags4*/0,
259 /*aen_holdoff_period*/{0, 0},
260 /*busy_timeout_period*/{0, 0},
261 /*extended_selftest_completion_time*/{0, 0}
262 };
263
264 #define CTL_CEM_LEN (sizeof(struct scsi_control_ext_page) - 4)
265
266 const static struct scsi_control_ext_page control_ext_page_default = {
267 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
268 /*subpage_code*/0x01,
269 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
270 /*flags*/0,
271 /*prio*/0,
272 /*max_sense*/0
273 };
274
275 const static struct scsi_control_ext_page control_ext_page_changeable = {
276 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
277 /*subpage_code*/0x01,
278 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
279 /*flags*/0,
280 /*prio*/0,
281 /*max_sense*/0xff
282 };
283
284 const static struct scsi_info_exceptions_page ie_page_default = {
285 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
286 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
287 /*info_flags*/SIEP_FLAGS_EWASC,
288 /*mrie*/SIEP_MRIE_NO,
289 /*interval_timer*/{0, 0, 0, 0},
290 /*report_count*/{0, 0, 0, 1}
291 };
292
293 const static struct scsi_info_exceptions_page ie_page_changeable = {
294 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
295 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
296 /*info_flags*/SIEP_FLAGS_EWASC | SIEP_FLAGS_DEXCPT | SIEP_FLAGS_TEST |
297 SIEP_FLAGS_LOGERR,
298 /*mrie*/0x0f,
299 /*interval_timer*/{0xff, 0xff, 0xff, 0xff},
300 /*report_count*/{0xff, 0xff, 0xff, 0xff}
301 };
302
303 #define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4)
304
305 const static struct ctl_logical_block_provisioning_page lbp_page_default = {{
306 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
307 /*subpage_code*/0x02,
308 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
309 /*flags*/0,
310 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
311 /*descr*/{}},
312 {{/*flags*/0,
313 /*resource*/0x01,
314 /*reserved*/{0, 0},
315 /*count*/{0, 0, 0, 0}},
316 {/*flags*/0,
317 /*resource*/0x02,
318 /*reserved*/{0, 0},
319 /*count*/{0, 0, 0, 0}},
320 {/*flags*/0,
321 /*resource*/0xf1,
322 /*reserved*/{0, 0},
323 /*count*/{0, 0, 0, 0}},
324 {/*flags*/0,
325 /*resource*/0xf2,
326 /*reserved*/{0, 0},
327 /*count*/{0, 0, 0, 0}}
328 }
329 };
330
331 const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{
332 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
333 /*subpage_code*/0x02,
334 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
335 /*flags*/SLBPP_SITUA,
336 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
337 /*descr*/{}},
338 {{/*flags*/0,
339 /*resource*/0,
340 /*reserved*/{0, 0},
341 /*count*/{0, 0, 0, 0}},
342 {/*flags*/0,
343 /*resource*/0,
344 /*reserved*/{0, 0},
345 /*count*/{0, 0, 0, 0}},
346 {/*flags*/0,
347 /*resource*/0,
348 /*reserved*/{0, 0},
349 /*count*/{0, 0, 0, 0}},
350 {/*flags*/0,
351 /*resource*/0,
352 /*reserved*/{0, 0},
353 /*count*/{0, 0, 0, 0}}
354 }
355 };
356
357 const static struct scsi_cddvd_capabilities_page cddvd_page_default = {
358 /*page_code*/SMS_CDDVD_CAPS_PAGE,
359 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
360 /*caps1*/0x3f,
361 /*caps2*/0x00,
362 /*caps3*/0xf0,
363 /*caps4*/0x00,
364 /*caps5*/0x29,
365 /*caps6*/0x00,
366 /*obsolete*/{0, 0},
367 /*nvol_levels*/{0, 0},
368 /*buffer_size*/{8, 0},
369 /*obsolete2*/{0, 0},
370 /*reserved*/0,
371 /*digital*/0,
372 /*obsolete3*/0,
373 /*copy_management*/0,
374 /*reserved2*/0,
375 /*rotation_control*/0,
376 /*cur_write_speed*/0,
377 /*num_speed_descr*/0,
378 };
379
380 const static struct scsi_cddvd_capabilities_page cddvd_page_changeable = {
381 /*page_code*/SMS_CDDVD_CAPS_PAGE,
382 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
383 /*caps1*/0,
384 /*caps2*/0,
385 /*caps3*/0,
386 /*caps4*/0,
387 /*caps5*/0,
388 /*caps6*/0,
389 /*obsolete*/{0, 0},
390 /*nvol_levels*/{0, 0},
391 /*buffer_size*/{0, 0},
392 /*obsolete2*/{0, 0},
393 /*reserved*/0,
394 /*digital*/0,
395 /*obsolete3*/0,
396 /*copy_management*/0,
397 /*reserved2*/0,
398 /*rotation_control*/0,
399 /*cur_write_speed*/0,
400 /*num_speed_descr*/0,
401 };
402
403 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
404 static int worker_threads = -1;
405 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
406 &worker_threads, 1, "Number of worker threads");
407 static int ctl_debug = CTL_DEBUG_NONE;
408 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
409 &ctl_debug, 0, "Enabled debug flags");
410 static int ctl_lun_map_size = 1024;
411 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, lun_map_size, CTLFLAG_RWTUN,
412 &ctl_lun_map_size, 0, "Size of per-port LUN map (max LUN + 1)");
413 #ifdef CTL_TIME_IO
414 static int ctl_time_io_secs = CTL_TIME_IO_DEFAULT_SECS;
415 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, time_io_secs, CTLFLAG_RWTUN,
416 &ctl_time_io_secs, 0, "Log requests taking more seconds");
417 #endif
418
419 /*
420 * Maximum number of LUNs we support. MUST be a power of 2.
421 */
422 #define CTL_DEFAULT_MAX_LUNS 1024
423 static int ctl_max_luns = CTL_DEFAULT_MAX_LUNS;
424 TUNABLE_INT("kern.cam.ctl.max_luns", &ctl_max_luns);
425 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, max_luns, CTLFLAG_RDTUN,
426 &ctl_max_luns, CTL_DEFAULT_MAX_LUNS, "Maximum number of LUNs");
427
428 /*
429 * Maximum number of ports registered at one time.
430 */
431 #define CTL_DEFAULT_MAX_PORTS 256
432 static int ctl_max_ports = CTL_DEFAULT_MAX_PORTS;
433 TUNABLE_INT("kern.cam.ctl.max_ports", &ctl_max_ports);
434 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, max_ports, CTLFLAG_RDTUN,
435 &ctl_max_ports, CTL_DEFAULT_MAX_LUNS, "Maximum number of ports");
436
437 /*
438 * Maximum number of initiators we support.
439 */
440 #define CTL_MAX_INITIATORS (CTL_MAX_INIT_PER_PORT * ctl_max_ports)
441
442 /*
443 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
444 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
445 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
446 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
447 */
448 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10
449
450 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
451 int param);
452 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
453 static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest);
454 static int ctl_init(void);
455 static int ctl_shutdown(void);
456 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
457 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
458 static void ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
459 static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
460 struct ctl_ooa *ooa_hdr,
461 struct ctl_ooa_entry *kern_entries);
462 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
463 struct thread *td);
464 static int ctl_enable_lun(struct ctl_lun *lun);
465 static int ctl_disable_lun(struct ctl_lun *lun);
466 static int ctl_free_lun(struct ctl_lun *lun);
467
468 static int ctl_do_mode_select(union ctl_io *io);
469 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
470 uint64_t res_key, uint64_t sa_res_key,
471 uint8_t type, uint32_t residx,
472 struct ctl_scsiio *ctsio,
473 struct scsi_per_res_out *cdb,
474 struct scsi_per_res_out_parms* param);
475 static void ctl_pro_preempt_other(struct ctl_lun *lun,
476 union ctl_ha_msg *msg);
477 static void ctl_hndl_per_res_out_on_other_sc(union ctl_io *io);
478 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
479 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
480 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
481 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
482 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
483 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
484 int alloc_len);
485 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
486 int alloc_len);
487 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
488 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
489 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
490 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
491 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
492 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2,
493 bool seq);
494 static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2);
495 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
496 union ctl_io *pending_io, union ctl_io *ooa_io);
497 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
498 union ctl_io **starting_io);
499 static void ctl_try_unblock_io(struct ctl_lun *lun, union ctl_io *io,
500 bool skip);
501 static void ctl_try_unblock_others(struct ctl_lun *lun, union ctl_io *io,
502 bool skip);
503 static int ctl_scsiio_lun_check(struct ctl_lun *lun,
504 const struct ctl_cmd_entry *entry,
505 struct ctl_scsiio *ctsio);
506 static void ctl_failover_lun(union ctl_io *io);
507 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
508 struct ctl_scsiio *ctsio);
509 static int ctl_scsiio(struct ctl_scsiio *ctsio);
510
511 static int ctl_target_reset(union ctl_io *io);
512 static void ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx,
513 ctl_ua_type ua_type);
514 static int ctl_lun_reset(union ctl_io *io);
515 static int ctl_abort_task(union ctl_io *io);
516 static int ctl_abort_task_set(union ctl_io *io);
517 static int ctl_query_task(union ctl_io *io, int task_set);
518 static void ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx,
519 ctl_ua_type ua_type);
520 static int ctl_i_t_nexus_reset(union ctl_io *io);
521 static int ctl_query_async_event(union ctl_io *io);
522 static void ctl_run_task(union ctl_io *io);
523 #ifdef CTL_IO_DELAY
524 static void ctl_datamove_timer_wakeup(void *arg);
525 static void ctl_done_timer_wakeup(void *arg);
526 #endif /* CTL_IO_DELAY */
527
528 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
529 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
530 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
531 static void ctl_datamove_remote_write(union ctl_io *io);
532 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
533 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
534 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
535 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
536 ctl_ha_dt_cb callback);
537 static void ctl_datamove_remote_read(union ctl_io *io);
538 static void ctl_datamove_remote(union ctl_io *io);
539 static void ctl_process_done(union ctl_io *io);
540 static void ctl_thresh_thread(void *arg);
541 static void ctl_work_thread(void *arg);
542 static void ctl_enqueue_incoming(union ctl_io *io);
543 static void ctl_enqueue_rtr(union ctl_io *io);
544 static void ctl_enqueue_done(union ctl_io *io);
545 static void ctl_enqueue_isc(union ctl_io *io);
546 static const struct ctl_cmd_entry *
547 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
548 static const struct ctl_cmd_entry *
549 ctl_validate_command(struct ctl_scsiio *ctsio);
550 static int ctl_cmd_applicable(uint8_t lun_type,
551 const struct ctl_cmd_entry *entry);
552 static int ctl_ha_init(void);
553 static int ctl_ha_shutdown(void);
554
555 static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx);
556 static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx);
557 static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx);
558 static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key);
559
560 /*
561 * Load the serialization table. This isn't very pretty, but is probably
562 * the easiest way to do it.
563 */
564 #include "ctl_ser_table.c"
565
566 /*
567 * We only need to define open, close and ioctl routines for this driver.
568 */
569 static struct cdevsw ctl_cdevsw = {
570 .d_version = D_VERSION,
571 .d_flags = 0,
572 .d_open = ctl_open,
573 .d_close = ctl_close,
574 .d_ioctl = ctl_ioctl,
575 .d_name = "ctl",
576 };
577
578
579 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
580
581 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
582
583 static moduledata_t ctl_moduledata = {
584 "ctl",
585 ctl_module_event_handler,
586 NULL
587 };
588
589 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
590 MODULE_VERSION(ctl, 1);
591
592 static struct ctl_frontend ha_frontend =
593 {
594 .name = "ha",
595 .init = ctl_ha_init,
596 .shutdown = ctl_ha_shutdown,
597 };
598
599 static int
ctl_ha_init(void)600 ctl_ha_init(void)
601 {
602 struct ctl_softc *softc = control_softc;
603
604 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
605 &softc->othersc_pool) != 0)
606 return (ENOMEM);
607 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
608 ctl_pool_free(softc->othersc_pool);
609 return (EIO);
610 }
611 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
612 != CTL_HA_STATUS_SUCCESS) {
613 ctl_ha_msg_destroy(softc);
614 ctl_pool_free(softc->othersc_pool);
615 return (EIO);
616 }
617 return (0);
618 };
619
620 static int
ctl_ha_shutdown(void)621 ctl_ha_shutdown(void)
622 {
623 struct ctl_softc *softc = control_softc;
624 struct ctl_port *port;
625
626 ctl_ha_msg_shutdown(softc);
627 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) != CTL_HA_STATUS_SUCCESS)
628 return (EIO);
629 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS)
630 return (EIO);
631 ctl_pool_free(softc->othersc_pool);
632 while ((port = STAILQ_FIRST(&ha_frontend.port_list)) != NULL) {
633 ctl_port_deregister(port);
634 free(port->port_name, M_CTL);
635 free(port, M_CTL);
636 }
637 return (0);
638 };
639
640 static void
ctl_ha_datamove(union ctl_io * io)641 ctl_ha_datamove(union ctl_io *io)
642 {
643 struct ctl_lun *lun = CTL_LUN(io);
644 struct ctl_sg_entry *sgl;
645 union ctl_ha_msg msg;
646 uint32_t sg_entries_sent;
647 int do_sg_copy, i, j;
648
649 memset(&msg.dt, 0, sizeof(msg.dt));
650 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
651 msg.hdr.original_sc = io->io_hdr.remote_io;
652 msg.hdr.serializing_sc = io;
653 msg.hdr.nexus = io->io_hdr.nexus;
654 msg.hdr.status = io->io_hdr.status;
655 msg.dt.flags = io->io_hdr.flags;
656
657 /*
658 * We convert everything into a S/G list here. We can't
659 * pass by reference, only by value between controllers.
660 * So we can't pass a pointer to the S/G list, only as many
661 * S/G entries as we can fit in here. If it's possible for
662 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
663 * then we need to break this up into multiple transfers.
664 */
665 if (io->scsiio.kern_sg_entries == 0) {
666 msg.dt.kern_sg_entries = 1;
667 #if 0
668 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
669 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
670 } else {
671 /* XXX KDM use busdma here! */
672 msg.dt.sg_list[0].addr =
673 (void *)vtophys(io->scsiio.kern_data_ptr);
674 }
675 #else
676 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
677 ("HA does not support BUS_ADDR"));
678 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
679 #endif
680 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
681 do_sg_copy = 0;
682 } else {
683 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
684 do_sg_copy = 1;
685 }
686
687 msg.dt.kern_data_len = io->scsiio.kern_data_len;
688 msg.dt.kern_total_len = io->scsiio.kern_total_len;
689 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
690 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
691 msg.dt.sg_sequence = 0;
692
693 /*
694 * Loop until we've sent all of the S/G entries. On the
695 * other end, we'll recompose these S/G entries into one
696 * contiguous list before processing.
697 */
698 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries;
699 msg.dt.sg_sequence++) {
700 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) /
701 sizeof(msg.dt.sg_list[0])),
702 msg.dt.kern_sg_entries - sg_entries_sent);
703 if (do_sg_copy != 0) {
704 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
705 for (i = sg_entries_sent, j = 0;
706 i < msg.dt.cur_sg_entries; i++, j++) {
707 #if 0
708 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
709 msg.dt.sg_list[j].addr = sgl[i].addr;
710 } else {
711 /* XXX KDM use busdma here! */
712 msg.dt.sg_list[j].addr =
713 (void *)vtophys(sgl[i].addr);
714 }
715 #else
716 KASSERT((io->io_hdr.flags &
717 CTL_FLAG_BUS_ADDR) == 0,
718 ("HA does not support BUS_ADDR"));
719 msg.dt.sg_list[j].addr = sgl[i].addr;
720 #endif
721 msg.dt.sg_list[j].len = sgl[i].len;
722 }
723 }
724
725 sg_entries_sent += msg.dt.cur_sg_entries;
726 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries);
727 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
728 sizeof(msg.dt) - sizeof(msg.dt.sg_list) +
729 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries,
730 M_WAITOK) > CTL_HA_STATUS_SUCCESS) {
731 io->io_hdr.port_status = 31341;
732 io->scsiio.be_move_done(io);
733 return;
734 }
735 msg.dt.sent_sg_entries = sg_entries_sent;
736 }
737
738 /*
739 * Officially handover the request from us to peer.
740 * If failover has just happened, then we must return error.
741 * If failover happen just after, then it is not our problem.
742 */
743 if (lun)
744 mtx_lock(&lun->lun_lock);
745 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
746 if (lun)
747 mtx_unlock(&lun->lun_lock);
748 io->io_hdr.port_status = 31342;
749 io->scsiio.be_move_done(io);
750 return;
751 }
752 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
753 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
754 if (lun)
755 mtx_unlock(&lun->lun_lock);
756 }
757
758 static void
ctl_ha_done(union ctl_io * io)759 ctl_ha_done(union ctl_io *io)
760 {
761 union ctl_ha_msg msg;
762
763 if (io->io_hdr.io_type == CTL_IO_SCSI) {
764 memset(&msg, 0, sizeof(msg));
765 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
766 msg.hdr.original_sc = io->io_hdr.remote_io;
767 msg.hdr.nexus = io->io_hdr.nexus;
768 msg.hdr.status = io->io_hdr.status;
769 msg.scsi.scsi_status = io->scsiio.scsi_status;
770 msg.scsi.tag_num = io->scsiio.tag_num;
771 msg.scsi.tag_type = io->scsiio.tag_type;
772 msg.scsi.sense_len = io->scsiio.sense_len;
773 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
774 io->scsiio.sense_len);
775 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
776 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
777 msg.scsi.sense_len, M_WAITOK);
778 }
779 ctl_free_io(io);
780 }
781
782 static void
ctl_isc_handler_finish_xfer(struct ctl_softc * ctl_softc,union ctl_ha_msg * msg_info)783 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
784 union ctl_ha_msg *msg_info)
785 {
786 struct ctl_scsiio *ctsio;
787
788 if (msg_info->hdr.original_sc == NULL) {
789 printf("%s: original_sc == NULL!\n", __func__);
790 /* XXX KDM now what? */
791 return;
792 }
793
794 ctsio = &msg_info->hdr.original_sc->scsiio;
795 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
796 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
797 ctsio->io_hdr.status = msg_info->hdr.status;
798 ctsio->scsi_status = msg_info->scsi.scsi_status;
799 ctsio->sense_len = msg_info->scsi.sense_len;
800 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
801 msg_info->scsi.sense_len);
802 ctl_enqueue_isc((union ctl_io *)ctsio);
803 }
804
805 static void
ctl_isc_handler_finish_ser_only(struct ctl_softc * ctl_softc,union ctl_ha_msg * msg_info)806 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
807 union ctl_ha_msg *msg_info)
808 {
809 struct ctl_scsiio *ctsio;
810
811 if (msg_info->hdr.serializing_sc == NULL) {
812 printf("%s: serializing_sc == NULL!\n", __func__);
813 /* XXX KDM now what? */
814 return;
815 }
816
817 ctsio = &msg_info->hdr.serializing_sc->scsiio;
818 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
819 ctl_enqueue_isc((union ctl_io *)ctsio);
820 }
821
822 void
ctl_isc_announce_lun(struct ctl_lun * lun)823 ctl_isc_announce_lun(struct ctl_lun *lun)
824 {
825 struct ctl_softc *softc = lun->ctl_softc;
826 union ctl_ha_msg *msg;
827 struct ctl_ha_msg_lun_pr_key pr_key;
828 int i, k;
829
830 if (softc->ha_link != CTL_HA_LINK_ONLINE)
831 return;
832 mtx_lock(&lun->lun_lock);
833 i = sizeof(msg->lun);
834 if (lun->lun_devid)
835 i += lun->lun_devid->len;
836 i += sizeof(pr_key) * lun->pr_key_count;
837 alloc:
838 mtx_unlock(&lun->lun_lock);
839 msg = malloc(i, M_CTL, M_WAITOK);
840 mtx_lock(&lun->lun_lock);
841 k = sizeof(msg->lun);
842 if (lun->lun_devid)
843 k += lun->lun_devid->len;
844 k += sizeof(pr_key) * lun->pr_key_count;
845 if (i < k) {
846 free(msg, M_CTL);
847 i = k;
848 goto alloc;
849 }
850 bzero(&msg->lun, sizeof(msg->lun));
851 msg->hdr.msg_type = CTL_MSG_LUN_SYNC;
852 msg->hdr.nexus.targ_lun = lun->lun;
853 msg->hdr.nexus.targ_mapped_lun = lun->lun;
854 msg->lun.flags = lun->flags;
855 msg->lun.pr_generation = lun->pr_generation;
856 msg->lun.pr_res_idx = lun->pr_res_idx;
857 msg->lun.pr_res_type = lun->pr_res_type;
858 msg->lun.pr_key_count = lun->pr_key_count;
859 i = 0;
860 if (lun->lun_devid) {
861 msg->lun.lun_devid_len = lun->lun_devid->len;
862 memcpy(&msg->lun.data[i], lun->lun_devid->data,
863 msg->lun.lun_devid_len);
864 i += msg->lun.lun_devid_len;
865 }
866 for (k = 0; k < CTL_MAX_INITIATORS; k++) {
867 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0)
868 continue;
869 pr_key.pr_iid = k;
870 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key));
871 i += sizeof(pr_key);
872 }
873 mtx_unlock(&lun->lun_lock);
874 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
875 M_WAITOK);
876 free(msg, M_CTL);
877
878 if (lun->flags & CTL_LUN_PRIMARY_SC) {
879 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
880 ctl_isc_announce_mode(lun, -1,
881 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
882 lun->mode_pages.index[i].subpage);
883 }
884 }
885 }
886
887 void
ctl_isc_announce_port(struct ctl_port * port)888 ctl_isc_announce_port(struct ctl_port *port)
889 {
890 struct ctl_softc *softc = port->ctl_softc;
891 union ctl_ha_msg *msg;
892 int i;
893
894 if (port->targ_port < softc->port_min ||
895 port->targ_port >= softc->port_max ||
896 softc->ha_link != CTL_HA_LINK_ONLINE)
897 return;
898 i = sizeof(msg->port) + strlen(port->port_name) + 1;
899 if (port->lun_map)
900 i += port->lun_map_size * sizeof(uint32_t);
901 if (port->port_devid)
902 i += port->port_devid->len;
903 if (port->target_devid)
904 i += port->target_devid->len;
905 if (port->init_devid)
906 i += port->init_devid->len;
907 msg = malloc(i, M_CTL, M_WAITOK);
908 bzero(&msg->port, sizeof(msg->port));
909 msg->hdr.msg_type = CTL_MSG_PORT_SYNC;
910 msg->hdr.nexus.targ_port = port->targ_port;
911 msg->port.port_type = port->port_type;
912 msg->port.physical_port = port->physical_port;
913 msg->port.virtual_port = port->virtual_port;
914 msg->port.status = port->status;
915 i = 0;
916 msg->port.name_len = sprintf(&msg->port.data[i],
917 "%d:%s", softc->ha_id, port->port_name) + 1;
918 i += msg->port.name_len;
919 if (port->lun_map) {
920 msg->port.lun_map_len = port->lun_map_size * sizeof(uint32_t);
921 memcpy(&msg->port.data[i], port->lun_map,
922 msg->port.lun_map_len);
923 i += msg->port.lun_map_len;
924 }
925 if (port->port_devid) {
926 msg->port.port_devid_len = port->port_devid->len;
927 memcpy(&msg->port.data[i], port->port_devid->data,
928 msg->port.port_devid_len);
929 i += msg->port.port_devid_len;
930 }
931 if (port->target_devid) {
932 msg->port.target_devid_len = port->target_devid->len;
933 memcpy(&msg->port.data[i], port->target_devid->data,
934 msg->port.target_devid_len);
935 i += msg->port.target_devid_len;
936 }
937 if (port->init_devid) {
938 msg->port.init_devid_len = port->init_devid->len;
939 memcpy(&msg->port.data[i], port->init_devid->data,
940 msg->port.init_devid_len);
941 i += msg->port.init_devid_len;
942 }
943 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
944 M_WAITOK);
945 free(msg, M_CTL);
946 }
947
948 void
ctl_isc_announce_iid(struct ctl_port * port,int iid)949 ctl_isc_announce_iid(struct ctl_port *port, int iid)
950 {
951 struct ctl_softc *softc = port->ctl_softc;
952 union ctl_ha_msg *msg;
953 int i, l;
954
955 if (port->targ_port < softc->port_min ||
956 port->targ_port >= softc->port_max ||
957 softc->ha_link != CTL_HA_LINK_ONLINE)
958 return;
959 mtx_lock(&softc->ctl_lock);
960 i = sizeof(msg->iid);
961 l = 0;
962 if (port->wwpn_iid[iid].name)
963 l = strlen(port->wwpn_iid[iid].name) + 1;
964 i += l;
965 msg = malloc(i, M_CTL, M_NOWAIT);
966 if (msg == NULL) {
967 mtx_unlock(&softc->ctl_lock);
968 return;
969 }
970 bzero(&msg->iid, sizeof(msg->iid));
971 msg->hdr.msg_type = CTL_MSG_IID_SYNC;
972 msg->hdr.nexus.targ_port = port->targ_port;
973 msg->hdr.nexus.initid = iid;
974 msg->iid.in_use = port->wwpn_iid[iid].in_use;
975 msg->iid.name_len = l;
976 msg->iid.wwpn = port->wwpn_iid[iid].wwpn;
977 if (port->wwpn_iid[iid].name)
978 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l);
979 mtx_unlock(&softc->ctl_lock);
980 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT);
981 free(msg, M_CTL);
982 }
983
984 void
ctl_isc_announce_mode(struct ctl_lun * lun,uint32_t initidx,uint8_t page,uint8_t subpage)985 ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx,
986 uint8_t page, uint8_t subpage)
987 {
988 struct ctl_softc *softc = lun->ctl_softc;
989 union ctl_ha_msg msg;
990 u_int i;
991
992 if (softc->ha_link != CTL_HA_LINK_ONLINE)
993 return;
994 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
995 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
996 page && lun->mode_pages.index[i].subpage == subpage)
997 break;
998 }
999 if (i == CTL_NUM_MODE_PAGES)
1000 return;
1001
1002 /* Don't try to replicate pages not present on this device. */
1003 if (lun->mode_pages.index[i].page_data == NULL)
1004 return;
1005
1006 bzero(&msg.mode, sizeof(msg.mode));
1007 msg.hdr.msg_type = CTL_MSG_MODE_SYNC;
1008 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT;
1009 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT;
1010 msg.hdr.nexus.targ_lun = lun->lun;
1011 msg.hdr.nexus.targ_mapped_lun = lun->lun;
1012 msg.mode.page_code = page;
1013 msg.mode.subpage = subpage;
1014 msg.mode.page_len = lun->mode_pages.index[i].page_len;
1015 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data,
1016 msg.mode.page_len);
1017 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode),
1018 M_WAITOK);
1019 }
1020
1021 static void
ctl_isc_ha_link_up(struct ctl_softc * softc)1022 ctl_isc_ha_link_up(struct ctl_softc *softc)
1023 {
1024 struct ctl_port *port;
1025 struct ctl_lun *lun;
1026 union ctl_ha_msg msg;
1027 int i;
1028
1029 /* Announce this node parameters to peer for validation. */
1030 msg.login.msg_type = CTL_MSG_LOGIN;
1031 msg.login.version = CTL_HA_VERSION;
1032 msg.login.ha_mode = softc->ha_mode;
1033 msg.login.ha_id = softc->ha_id;
1034 msg.login.max_luns = ctl_max_luns;
1035 msg.login.max_ports = ctl_max_ports;
1036 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT;
1037 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login),
1038 M_WAITOK);
1039
1040 STAILQ_FOREACH(port, &softc->port_list, links) {
1041 ctl_isc_announce_port(port);
1042 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1043 if (port->wwpn_iid[i].in_use)
1044 ctl_isc_announce_iid(port, i);
1045 }
1046 }
1047 STAILQ_FOREACH(lun, &softc->lun_list, links)
1048 ctl_isc_announce_lun(lun);
1049 }
1050
1051 static void
ctl_isc_ha_link_down(struct ctl_softc * softc)1052 ctl_isc_ha_link_down(struct ctl_softc *softc)
1053 {
1054 struct ctl_port *port;
1055 struct ctl_lun *lun;
1056 union ctl_io *io;
1057 int i;
1058
1059 mtx_lock(&softc->ctl_lock);
1060 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1061 mtx_lock(&lun->lun_lock);
1062 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) {
1063 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1064 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1065 }
1066 mtx_unlock(&lun->lun_lock);
1067
1068 mtx_unlock(&softc->ctl_lock);
1069 io = ctl_alloc_io(softc->othersc_pool);
1070 mtx_lock(&softc->ctl_lock);
1071 ctl_zero_io(io);
1072 io->io_hdr.msg_type = CTL_MSG_FAILOVER;
1073 io->io_hdr.nexus.targ_mapped_lun = lun->lun;
1074 ctl_enqueue_isc(io);
1075 }
1076
1077 STAILQ_FOREACH(port, &softc->port_list, links) {
1078 if (port->targ_port >= softc->port_min &&
1079 port->targ_port < softc->port_max)
1080 continue;
1081 port->status &= ~CTL_PORT_STATUS_ONLINE;
1082 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1083 port->wwpn_iid[i].in_use = 0;
1084 free(port->wwpn_iid[i].name, M_CTL);
1085 port->wwpn_iid[i].name = NULL;
1086 }
1087 }
1088 mtx_unlock(&softc->ctl_lock);
1089 }
1090
1091 static void
ctl_isc_ua(struct ctl_softc * softc,union ctl_ha_msg * msg,int len)1092 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1093 {
1094 struct ctl_lun *lun;
1095 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus);
1096
1097 mtx_lock(&softc->ctl_lock);
1098 if (msg->hdr.nexus.targ_mapped_lun >= ctl_max_luns ||
1099 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) == NULL) {
1100 mtx_unlock(&softc->ctl_lock);
1101 return;
1102 }
1103 mtx_lock(&lun->lun_lock);
1104 mtx_unlock(&softc->ctl_lock);
1105 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && msg->ua.ua_set)
1106 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8);
1107 if (msg->ua.ua_all) {
1108 if (msg->ua.ua_set)
1109 ctl_est_ua_all(lun, iid, msg->ua.ua_type);
1110 else
1111 ctl_clr_ua_all(lun, iid, msg->ua.ua_type);
1112 } else {
1113 if (msg->ua.ua_set)
1114 ctl_est_ua(lun, iid, msg->ua.ua_type);
1115 else
1116 ctl_clr_ua(lun, iid, msg->ua.ua_type);
1117 }
1118 mtx_unlock(&lun->lun_lock);
1119 }
1120
1121 static void
ctl_isc_lun_sync(struct ctl_softc * softc,union ctl_ha_msg * msg,int len)1122 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1123 {
1124 struct ctl_lun *lun;
1125 struct ctl_ha_msg_lun_pr_key pr_key;
1126 int i, k;
1127 ctl_lun_flags oflags;
1128 uint32_t targ_lun;
1129
1130 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1131 mtx_lock(&softc->ctl_lock);
1132 if (targ_lun >= ctl_max_luns ||
1133 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1134 mtx_unlock(&softc->ctl_lock);
1135 return;
1136 }
1137 mtx_lock(&lun->lun_lock);
1138 mtx_unlock(&softc->ctl_lock);
1139 if (lun->flags & CTL_LUN_DISABLED) {
1140 mtx_unlock(&lun->lun_lock);
1141 return;
1142 }
1143 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0;
1144 if (msg->lun.lun_devid_len != i || (i > 0 &&
1145 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) {
1146 mtx_unlock(&lun->lun_lock);
1147 printf("%s: Received conflicting HA LUN %d\n",
1148 __func__, targ_lun);
1149 return;
1150 } else {
1151 /* Record whether peer is primary. */
1152 oflags = lun->flags;
1153 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) &&
1154 (msg->lun.flags & CTL_LUN_DISABLED) == 0)
1155 lun->flags |= CTL_LUN_PEER_SC_PRIMARY;
1156 else
1157 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1158 if (oflags != lun->flags)
1159 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1160
1161 /* If peer is primary and we are not -- use data */
1162 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
1163 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) {
1164 lun->pr_generation = msg->lun.pr_generation;
1165 lun->pr_res_idx = msg->lun.pr_res_idx;
1166 lun->pr_res_type = msg->lun.pr_res_type;
1167 lun->pr_key_count = msg->lun.pr_key_count;
1168 for (k = 0; k < CTL_MAX_INITIATORS; k++)
1169 ctl_clr_prkey(lun, k);
1170 for (k = 0; k < msg->lun.pr_key_count; k++) {
1171 memcpy(&pr_key, &msg->lun.data[i],
1172 sizeof(pr_key));
1173 ctl_alloc_prkey(lun, pr_key.pr_iid);
1174 ctl_set_prkey(lun, pr_key.pr_iid,
1175 pr_key.pr_key);
1176 i += sizeof(pr_key);
1177 }
1178 }
1179
1180 mtx_unlock(&lun->lun_lock);
1181 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n",
1182 __func__, targ_lun,
1183 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ?
1184 "primary" : "secondary"));
1185
1186 /* If we are primary but peer doesn't know -- notify */
1187 if ((lun->flags & CTL_LUN_PRIMARY_SC) &&
1188 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0)
1189 ctl_isc_announce_lun(lun);
1190 }
1191 }
1192
1193 static void
ctl_isc_port_sync(struct ctl_softc * softc,union ctl_ha_msg * msg,int len)1194 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1195 {
1196 struct ctl_port *port;
1197 struct ctl_lun *lun;
1198 int i, new;
1199
1200 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1201 if (port == NULL) {
1202 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__,
1203 msg->hdr.nexus.targ_port));
1204 new = 1;
1205 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO);
1206 port->frontend = &ha_frontend;
1207 port->targ_port = msg->hdr.nexus.targ_port;
1208 port->fe_datamove = ctl_ha_datamove;
1209 port->fe_done = ctl_ha_done;
1210 } else if (port->frontend == &ha_frontend) {
1211 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__,
1212 msg->hdr.nexus.targ_port));
1213 new = 0;
1214 } else {
1215 printf("%s: Received conflicting HA port %d\n",
1216 __func__, msg->hdr.nexus.targ_port);
1217 return;
1218 }
1219 port->port_type = msg->port.port_type;
1220 port->physical_port = msg->port.physical_port;
1221 port->virtual_port = msg->port.virtual_port;
1222 port->status = msg->port.status;
1223 i = 0;
1224 free(port->port_name, M_CTL);
1225 port->port_name = strndup(&msg->port.data[i], msg->port.name_len,
1226 M_CTL);
1227 i += msg->port.name_len;
1228 if (msg->port.lun_map_len != 0) {
1229 if (port->lun_map == NULL ||
1230 port->lun_map_size * sizeof(uint32_t) <
1231 msg->port.lun_map_len) {
1232 port->lun_map_size = 0;
1233 free(port->lun_map, M_CTL);
1234 port->lun_map = malloc(msg->port.lun_map_len,
1235 M_CTL, M_WAITOK);
1236 }
1237 memcpy(port->lun_map, &msg->port.data[i], msg->port.lun_map_len);
1238 port->lun_map_size = msg->port.lun_map_len / sizeof(uint32_t);
1239 i += msg->port.lun_map_len;
1240 } else {
1241 port->lun_map_size = 0;
1242 free(port->lun_map, M_CTL);
1243 port->lun_map = NULL;
1244 }
1245 if (msg->port.port_devid_len != 0) {
1246 if (port->port_devid == NULL ||
1247 port->port_devid->len < msg->port.port_devid_len) {
1248 free(port->port_devid, M_CTL);
1249 port->port_devid = malloc(sizeof(struct ctl_devid) +
1250 msg->port.port_devid_len, M_CTL, M_WAITOK);
1251 }
1252 memcpy(port->port_devid->data, &msg->port.data[i],
1253 msg->port.port_devid_len);
1254 port->port_devid->len = msg->port.port_devid_len;
1255 i += msg->port.port_devid_len;
1256 } else {
1257 free(port->port_devid, M_CTL);
1258 port->port_devid = NULL;
1259 }
1260 if (msg->port.target_devid_len != 0) {
1261 if (port->target_devid == NULL ||
1262 port->target_devid->len < msg->port.target_devid_len) {
1263 free(port->target_devid, M_CTL);
1264 port->target_devid = malloc(sizeof(struct ctl_devid) +
1265 msg->port.target_devid_len, M_CTL, M_WAITOK);
1266 }
1267 memcpy(port->target_devid->data, &msg->port.data[i],
1268 msg->port.target_devid_len);
1269 port->target_devid->len = msg->port.target_devid_len;
1270 i += msg->port.target_devid_len;
1271 } else {
1272 free(port->target_devid, M_CTL);
1273 port->target_devid = NULL;
1274 }
1275 if (msg->port.init_devid_len != 0) {
1276 if (port->init_devid == NULL ||
1277 port->init_devid->len < msg->port.init_devid_len) {
1278 free(port->init_devid, M_CTL);
1279 port->init_devid = malloc(sizeof(struct ctl_devid) +
1280 msg->port.init_devid_len, M_CTL, M_WAITOK);
1281 }
1282 memcpy(port->init_devid->data, &msg->port.data[i],
1283 msg->port.init_devid_len);
1284 port->init_devid->len = msg->port.init_devid_len;
1285 i += msg->port.init_devid_len;
1286 } else {
1287 free(port->init_devid, M_CTL);
1288 port->init_devid = NULL;
1289 }
1290 if (new) {
1291 if (ctl_port_register(port) != 0) {
1292 printf("%s: ctl_port_register() failed with error\n",
1293 __func__);
1294 }
1295 }
1296 mtx_lock(&softc->ctl_lock);
1297 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1298 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
1299 continue;
1300 mtx_lock(&lun->lun_lock);
1301 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
1302 mtx_unlock(&lun->lun_lock);
1303 }
1304 mtx_unlock(&softc->ctl_lock);
1305 }
1306
1307 static void
ctl_isc_iid_sync(struct ctl_softc * softc,union ctl_ha_msg * msg,int len)1308 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1309 {
1310 struct ctl_port *port;
1311 int iid;
1312
1313 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1314 if (port == NULL) {
1315 printf("%s: Received IID for unknown port %d\n",
1316 __func__, msg->hdr.nexus.targ_port);
1317 return;
1318 }
1319 iid = msg->hdr.nexus.initid;
1320 if (port->wwpn_iid[iid].in_use != 0 &&
1321 msg->iid.in_use == 0)
1322 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON);
1323 port->wwpn_iid[iid].in_use = msg->iid.in_use;
1324 port->wwpn_iid[iid].wwpn = msg->iid.wwpn;
1325 free(port->wwpn_iid[iid].name, M_CTL);
1326 if (msg->iid.name_len) {
1327 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0],
1328 msg->iid.name_len, M_CTL);
1329 } else
1330 port->wwpn_iid[iid].name = NULL;
1331 }
1332
1333 static void
ctl_isc_login(struct ctl_softc * softc,union ctl_ha_msg * msg,int len)1334 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1335 {
1336
1337 if (msg->login.version != CTL_HA_VERSION) {
1338 printf("CTL HA peers have different versions %d != %d\n",
1339 msg->login.version, CTL_HA_VERSION);
1340 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1341 return;
1342 }
1343 if (msg->login.ha_mode != softc->ha_mode) {
1344 printf("CTL HA peers have different ha_mode %d != %d\n",
1345 msg->login.ha_mode, softc->ha_mode);
1346 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1347 return;
1348 }
1349 if (msg->login.ha_id == softc->ha_id) {
1350 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id);
1351 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1352 return;
1353 }
1354 if (msg->login.max_luns != ctl_max_luns ||
1355 msg->login.max_ports != ctl_max_ports ||
1356 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) {
1357 printf("CTL HA peers have different limits\n");
1358 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1359 return;
1360 }
1361 }
1362
1363 static void
ctl_isc_mode_sync(struct ctl_softc * softc,union ctl_ha_msg * msg,int len)1364 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1365 {
1366 struct ctl_lun *lun;
1367 u_int i;
1368 uint32_t initidx, targ_lun;
1369
1370 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1371 mtx_lock(&softc->ctl_lock);
1372 if (targ_lun >= ctl_max_luns ||
1373 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1374 mtx_unlock(&softc->ctl_lock);
1375 return;
1376 }
1377 mtx_lock(&lun->lun_lock);
1378 mtx_unlock(&softc->ctl_lock);
1379 if (lun->flags & CTL_LUN_DISABLED) {
1380 mtx_unlock(&lun->lun_lock);
1381 return;
1382 }
1383 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
1384 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
1385 msg->mode.page_code &&
1386 lun->mode_pages.index[i].subpage == msg->mode.subpage)
1387 break;
1388 }
1389 if (i == CTL_NUM_MODE_PAGES) {
1390 mtx_unlock(&lun->lun_lock);
1391 return;
1392 }
1393 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data,
1394 lun->mode_pages.index[i].page_len);
1395 initidx = ctl_get_initindex(&msg->hdr.nexus);
1396 if (initidx != -1)
1397 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
1398 mtx_unlock(&lun->lun_lock);
1399 }
1400
1401 /*
1402 * ISC (Inter Shelf Communication) event handler. Events from the HA
1403 * subsystem come in here.
1404 */
1405 static void
ctl_isc_event_handler(ctl_ha_channel channel,ctl_ha_event event,int param)1406 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
1407 {
1408 struct ctl_softc *softc = control_softc;
1409 union ctl_io *io;
1410 struct ctl_prio *presio;
1411 ctl_ha_status isc_status;
1412
1413 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event));
1414 if (event == CTL_HA_EVT_MSG_RECV) {
1415 union ctl_ha_msg *msg, msgbuf;
1416
1417 if (param > sizeof(msgbuf))
1418 msg = malloc(param, M_CTL, M_WAITOK);
1419 else
1420 msg = &msgbuf;
1421 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param,
1422 M_WAITOK);
1423 if (isc_status != CTL_HA_STATUS_SUCCESS) {
1424 printf("%s: Error receiving message: %d\n",
1425 __func__, isc_status);
1426 if (msg != &msgbuf)
1427 free(msg, M_CTL);
1428 return;
1429 }
1430
1431 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type));
1432 switch (msg->hdr.msg_type) {
1433 case CTL_MSG_SERIALIZE:
1434 io = ctl_alloc_io(softc->othersc_pool);
1435 ctl_zero_io(io);
1436 // populate ctsio from msg
1437 io->io_hdr.io_type = CTL_IO_SCSI;
1438 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
1439 io->io_hdr.remote_io = msg->hdr.original_sc;
1440 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
1441 CTL_FLAG_IO_ACTIVE;
1442 /*
1443 * If we're in serialization-only mode, we don't
1444 * want to go through full done processing. Thus
1445 * the COPY flag.
1446 *
1447 * XXX KDM add another flag that is more specific.
1448 */
1449 if (softc->ha_mode != CTL_HA_MODE_XFER)
1450 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
1451 io->io_hdr.nexus = msg->hdr.nexus;
1452 io->scsiio.tag_num = msg->scsi.tag_num;
1453 io->scsiio.tag_type = msg->scsi.tag_type;
1454 #ifdef CTL_TIME_IO
1455 io->io_hdr.start_time = time_uptime;
1456 getbinuptime(&io->io_hdr.start_bt);
1457 #endif /* CTL_TIME_IO */
1458 io->scsiio.cdb_len = msg->scsi.cdb_len;
1459 memcpy(io->scsiio.cdb, msg->scsi.cdb,
1460 CTL_MAX_CDBLEN);
1461 if (softc->ha_mode == CTL_HA_MODE_XFER) {
1462 const struct ctl_cmd_entry *entry;
1463
1464 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
1465 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
1466 io->io_hdr.flags |=
1467 entry->flags & CTL_FLAG_DATA_MASK;
1468 }
1469 ctl_enqueue_isc(io);
1470 break;
1471
1472 /* Performed on the Originating SC, XFER mode only */
1473 case CTL_MSG_DATAMOVE: {
1474 struct ctl_sg_entry *sgl;
1475 int i, j;
1476
1477 io = msg->hdr.original_sc;
1478 if (io == NULL) {
1479 printf("%s: original_sc == NULL!\n", __func__);
1480 /* XXX KDM do something here */
1481 break;
1482 }
1483 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
1484 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1485 /*
1486 * Keep track of this, we need to send it back over
1487 * when the datamove is complete.
1488 */
1489 io->io_hdr.remote_io = msg->hdr.serializing_sc;
1490 if (msg->hdr.status == CTL_SUCCESS)
1491 io->io_hdr.status = msg->hdr.status;
1492
1493 if (msg->dt.sg_sequence == 0) {
1494 #ifdef CTL_TIME_IO
1495 getbinuptime(&io->io_hdr.dma_start_bt);
1496 #endif
1497 i = msg->dt.kern_sg_entries +
1498 msg->dt.kern_data_len /
1499 CTL_HA_DATAMOVE_SEGMENT + 1;
1500 sgl = malloc(sizeof(*sgl) * i, M_CTL,
1501 M_WAITOK | M_ZERO);
1502 CTL_RSGL(io) = sgl;
1503 CTL_LSGL(io) = &sgl[msg->dt.kern_sg_entries];
1504
1505 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
1506
1507 io->scsiio.kern_sg_entries =
1508 msg->dt.kern_sg_entries;
1509 io->scsiio.rem_sg_entries =
1510 msg->dt.kern_sg_entries;
1511 io->scsiio.kern_data_len =
1512 msg->dt.kern_data_len;
1513 io->scsiio.kern_total_len =
1514 msg->dt.kern_total_len;
1515 io->scsiio.kern_data_resid =
1516 msg->dt.kern_data_resid;
1517 io->scsiio.kern_rel_offset =
1518 msg->dt.kern_rel_offset;
1519 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR;
1520 io->io_hdr.flags |= msg->dt.flags &
1521 CTL_FLAG_BUS_ADDR;
1522 } else
1523 sgl = (struct ctl_sg_entry *)
1524 io->scsiio.kern_data_ptr;
1525
1526 for (i = msg->dt.sent_sg_entries, j = 0;
1527 i < (msg->dt.sent_sg_entries +
1528 msg->dt.cur_sg_entries); i++, j++) {
1529 sgl[i].addr = msg->dt.sg_list[j].addr;
1530 sgl[i].len = msg->dt.sg_list[j].len;
1531 }
1532
1533 /*
1534 * If this is the last piece of the I/O, we've got
1535 * the full S/G list. Queue processing in the thread.
1536 * Otherwise wait for the next piece.
1537 */
1538 if (msg->dt.sg_last != 0)
1539 ctl_enqueue_isc(io);
1540 break;
1541 }
1542 /* Performed on the Serializing (primary) SC, XFER mode only */
1543 case CTL_MSG_DATAMOVE_DONE: {
1544 if (msg->hdr.serializing_sc == NULL) {
1545 printf("%s: serializing_sc == NULL!\n",
1546 __func__);
1547 /* XXX KDM now what? */
1548 break;
1549 }
1550 /*
1551 * We grab the sense information here in case
1552 * there was a failure, so we can return status
1553 * back to the initiator.
1554 */
1555 io = msg->hdr.serializing_sc;
1556 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
1557 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1558 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1559 io->io_hdr.port_status = msg->scsi.port_status;
1560 io->scsiio.kern_data_resid = msg->scsi.kern_data_resid;
1561 if (msg->hdr.status != CTL_STATUS_NONE) {
1562 io->io_hdr.status = msg->hdr.status;
1563 io->scsiio.scsi_status = msg->scsi.scsi_status;
1564 io->scsiio.sense_len = msg->scsi.sense_len;
1565 memcpy(&io->scsiio.sense_data,
1566 &msg->scsi.sense_data,
1567 msg->scsi.sense_len);
1568 if (msg->hdr.status == CTL_SUCCESS)
1569 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1570 }
1571 ctl_enqueue_isc(io);
1572 break;
1573 }
1574
1575 /* Preformed on Originating SC, SER_ONLY mode */
1576 case CTL_MSG_R2R:
1577 io = msg->hdr.original_sc;
1578 if (io == NULL) {
1579 printf("%s: original_sc == NULL!\n",
1580 __func__);
1581 break;
1582 }
1583 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1584 io->io_hdr.msg_type = CTL_MSG_R2R;
1585 io->io_hdr.remote_io = msg->hdr.serializing_sc;
1586 ctl_enqueue_isc(io);
1587 break;
1588
1589 /*
1590 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
1591 * mode.
1592 * Performed on the Originating (i.e. secondary) SC in XFER
1593 * mode
1594 */
1595 case CTL_MSG_FINISH_IO:
1596 if (softc->ha_mode == CTL_HA_MODE_XFER)
1597 ctl_isc_handler_finish_xfer(softc, msg);
1598 else
1599 ctl_isc_handler_finish_ser_only(softc, msg);
1600 break;
1601
1602 /* Preformed on Originating SC */
1603 case CTL_MSG_BAD_JUJU:
1604 io = msg->hdr.original_sc;
1605 if (io == NULL) {
1606 printf("%s: Bad JUJU!, original_sc is NULL!\n",
1607 __func__);
1608 break;
1609 }
1610 ctl_copy_sense_data(msg, io);
1611 /*
1612 * IO should have already been cleaned up on other
1613 * SC so clear this flag so we won't send a message
1614 * back to finish the IO there.
1615 */
1616 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1617 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1618
1619 /* io = msg->hdr.serializing_sc; */
1620 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
1621 ctl_enqueue_isc(io);
1622 break;
1623
1624 /* Handle resets sent from the other side */
1625 case CTL_MSG_MANAGE_TASKS: {
1626 struct ctl_taskio *taskio;
1627 taskio = (struct ctl_taskio *)ctl_alloc_io(
1628 softc->othersc_pool);
1629 ctl_zero_io((union ctl_io *)taskio);
1630 taskio->io_hdr.io_type = CTL_IO_TASK;
1631 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1632 taskio->io_hdr.nexus = msg->hdr.nexus;
1633 taskio->task_action = msg->task.task_action;
1634 taskio->tag_num = msg->task.tag_num;
1635 taskio->tag_type = msg->task.tag_type;
1636 #ifdef CTL_TIME_IO
1637 taskio->io_hdr.start_time = time_uptime;
1638 getbinuptime(&taskio->io_hdr.start_bt);
1639 #endif /* CTL_TIME_IO */
1640 ctl_run_task((union ctl_io *)taskio);
1641 break;
1642 }
1643 /* Persistent Reserve action which needs attention */
1644 case CTL_MSG_PERS_ACTION:
1645 presio = (struct ctl_prio *)ctl_alloc_io(
1646 softc->othersc_pool);
1647 ctl_zero_io((union ctl_io *)presio);
1648 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
1649 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1650 presio->io_hdr.nexus = msg->hdr.nexus;
1651 presio->pr_msg = msg->pr;
1652 ctl_enqueue_isc((union ctl_io *)presio);
1653 break;
1654 case CTL_MSG_UA:
1655 ctl_isc_ua(softc, msg, param);
1656 break;
1657 case CTL_MSG_PORT_SYNC:
1658 ctl_isc_port_sync(softc, msg, param);
1659 break;
1660 case CTL_MSG_LUN_SYNC:
1661 ctl_isc_lun_sync(softc, msg, param);
1662 break;
1663 case CTL_MSG_IID_SYNC:
1664 ctl_isc_iid_sync(softc, msg, param);
1665 break;
1666 case CTL_MSG_LOGIN:
1667 ctl_isc_login(softc, msg, param);
1668 break;
1669 case CTL_MSG_MODE_SYNC:
1670 ctl_isc_mode_sync(softc, msg, param);
1671 break;
1672 default:
1673 printf("Received HA message of unknown type %d\n",
1674 msg->hdr.msg_type);
1675 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1676 break;
1677 }
1678 if (msg != &msgbuf)
1679 free(msg, M_CTL);
1680 } else if (event == CTL_HA_EVT_LINK_CHANGE) {
1681 printf("CTL: HA link status changed from %d to %d\n",
1682 softc->ha_link, param);
1683 if (param == softc->ha_link)
1684 return;
1685 if (softc->ha_link == CTL_HA_LINK_ONLINE) {
1686 softc->ha_link = param;
1687 ctl_isc_ha_link_down(softc);
1688 } else {
1689 softc->ha_link = param;
1690 if (softc->ha_link == CTL_HA_LINK_ONLINE)
1691 ctl_isc_ha_link_up(softc);
1692 }
1693 return;
1694 } else {
1695 printf("ctl_isc_event_handler: Unknown event %d\n", event);
1696 return;
1697 }
1698 }
1699
1700 static void
ctl_copy_sense_data(union ctl_ha_msg * src,union ctl_io * dest)1701 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
1702 {
1703
1704 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data,
1705 src->scsi.sense_len);
1706 dest->scsiio.scsi_status = src->scsi.scsi_status;
1707 dest->scsiio.sense_len = src->scsi.sense_len;
1708 dest->io_hdr.status = src->hdr.status;
1709 }
1710
1711 static void
ctl_copy_sense_data_back(union ctl_io * src,union ctl_ha_msg * dest)1712 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest)
1713 {
1714
1715 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data,
1716 src->scsiio.sense_len);
1717 dest->scsi.scsi_status = src->scsiio.scsi_status;
1718 dest->scsi.sense_len = src->scsiio.sense_len;
1719 dest->hdr.status = src->io_hdr.status;
1720 }
1721
1722 void
ctl_est_ua(struct ctl_lun * lun,uint32_t initidx,ctl_ua_type ua)1723 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1724 {
1725 struct ctl_softc *softc = lun->ctl_softc;
1726 ctl_ua_type *pu;
1727
1728 if (initidx < softc->init_min || initidx >= softc->init_max)
1729 return;
1730 mtx_assert(&lun->lun_lock, MA_OWNED);
1731 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1732 if (pu == NULL)
1733 return;
1734 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
1735 }
1736
1737 void
ctl_est_ua_port(struct ctl_lun * lun,int port,uint32_t except,ctl_ua_type ua)1738 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua)
1739 {
1740 int i;
1741
1742 mtx_assert(&lun->lun_lock, MA_OWNED);
1743 if (lun->pending_ua[port] == NULL)
1744 return;
1745 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1746 if (port * CTL_MAX_INIT_PER_PORT + i == except)
1747 continue;
1748 lun->pending_ua[port][i] |= ua;
1749 }
1750 }
1751
1752 void
ctl_est_ua_all(struct ctl_lun * lun,uint32_t except,ctl_ua_type ua)1753 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1754 {
1755 struct ctl_softc *softc = lun->ctl_softc;
1756 int i;
1757
1758 mtx_assert(&lun->lun_lock, MA_OWNED);
1759 for (i = softc->port_min; i < softc->port_max; i++)
1760 ctl_est_ua_port(lun, i, except, ua);
1761 }
1762
1763 void
ctl_clr_ua(struct ctl_lun * lun,uint32_t initidx,ctl_ua_type ua)1764 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1765 {
1766 struct ctl_softc *softc = lun->ctl_softc;
1767 ctl_ua_type *pu;
1768
1769 if (initidx < softc->init_min || initidx >= softc->init_max)
1770 return;
1771 mtx_assert(&lun->lun_lock, MA_OWNED);
1772 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1773 if (pu == NULL)
1774 return;
1775 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1776 }
1777
1778 void
ctl_clr_ua_all(struct ctl_lun * lun,uint32_t except,ctl_ua_type ua)1779 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1780 {
1781 struct ctl_softc *softc = lun->ctl_softc;
1782 int i, j;
1783
1784 mtx_assert(&lun->lun_lock, MA_OWNED);
1785 for (i = softc->port_min; i < softc->port_max; i++) {
1786 if (lun->pending_ua[i] == NULL)
1787 continue;
1788 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1789 if (i * CTL_MAX_INIT_PER_PORT + j == except)
1790 continue;
1791 lun->pending_ua[i][j] &= ~ua;
1792 }
1793 }
1794 }
1795
1796 void
ctl_clr_ua_allluns(struct ctl_softc * ctl_softc,uint32_t initidx,ctl_ua_type ua_type)1797 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
1798 ctl_ua_type ua_type)
1799 {
1800 struct ctl_lun *lun;
1801
1802 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
1803 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
1804 mtx_lock(&lun->lun_lock);
1805 ctl_clr_ua(lun, initidx, ua_type);
1806 mtx_unlock(&lun->lun_lock);
1807 }
1808 }
1809
1810 static int
ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)1811 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)
1812 {
1813 struct ctl_softc *softc = (struct ctl_softc *)arg1;
1814 struct ctl_lun *lun;
1815 struct ctl_lun_req ireq;
1816 int error, value;
1817
1818 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1;
1819 error = sysctl_handle_int(oidp, &value, 0, req);
1820 if ((error != 0) || (req->newptr == NULL))
1821 return (error);
1822
1823 mtx_lock(&softc->ctl_lock);
1824 if (value == 0)
1825 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1826 else
1827 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1828 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1829 mtx_unlock(&softc->ctl_lock);
1830 bzero(&ireq, sizeof(ireq));
1831 ireq.reqtype = CTL_LUNREQ_MODIFY;
1832 ireq.reqdata.modify.lun_id = lun->lun;
1833 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0,
1834 curthread);
1835 if (ireq.status != CTL_LUN_OK) {
1836 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n",
1837 __func__, ireq.status, ireq.error_str);
1838 }
1839 mtx_lock(&softc->ctl_lock);
1840 }
1841 mtx_unlock(&softc->ctl_lock);
1842 return (0);
1843 }
1844
1845 static int
ctl_init(void)1846 ctl_init(void)
1847 {
1848 struct make_dev_args args;
1849 struct ctl_softc *softc;
1850 int i, error;
1851
1852 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1853 M_WAITOK | M_ZERO);
1854
1855 make_dev_args_init(&args);
1856 args.mda_devsw = &ctl_cdevsw;
1857 args.mda_uid = UID_ROOT;
1858 args.mda_gid = GID_OPERATOR;
1859 args.mda_mode = 0600;
1860 args.mda_si_drv1 = softc;
1861 error = make_dev_s(&args, &softc->dev, "cam/ctl");
1862 if (error != 0) {
1863 free(softc, M_DEVBUF);
1864 control_softc = NULL;
1865 return (error);
1866 }
1867
1868 sysctl_ctx_init(&softc->sysctl_ctx);
1869 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1870 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1871 CTLFLAG_RD, 0, "CAM Target Layer");
1872
1873 if (softc->sysctl_tree == NULL) {
1874 printf("%s: unable to allocate sysctl tree\n", __func__);
1875 destroy_dev(softc->dev);
1876 free(softc, M_DEVBUF);
1877 control_softc = NULL;
1878 return (ENOMEM);
1879 }
1880
1881 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1882 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1883 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1884 softc->flags = 0;
1885
1886 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1887 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0,
1888 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)");
1889
1890 if (ctl_max_luns <= 0 || powerof2(ctl_max_luns) == 0) {
1891 printf("Bad value %d for kern.cam.ctl.max_luns, must be a power of two, using %d\n",
1892 ctl_max_luns, CTL_DEFAULT_MAX_LUNS);
1893 ctl_max_luns = CTL_DEFAULT_MAX_LUNS;
1894 }
1895 softc->ctl_luns = malloc(sizeof(struct ctl_lun *) * ctl_max_luns,
1896 M_DEVBUF, M_WAITOK | M_ZERO);
1897 softc->ctl_lun_mask = malloc(sizeof(uint32_t) *
1898 ((ctl_max_luns + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO);
1899 if (ctl_max_ports <= 0 || powerof2(ctl_max_ports) == 0) {
1900 printf("Bad value %d for kern.cam.ctl.max_ports, must be a power of two, using %d\n",
1901 ctl_max_ports, CTL_DEFAULT_MAX_PORTS);
1902 ctl_max_ports = CTL_DEFAULT_MAX_PORTS;
1903 }
1904 softc->ctl_port_mask = malloc(sizeof(uint32_t) *
1905 ((ctl_max_ports + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO);
1906 softc->ctl_ports = malloc(sizeof(struct ctl_port *) * ctl_max_ports,
1907 M_DEVBUF, M_WAITOK | M_ZERO);
1908
1909
1910 /*
1911 * In Copan's HA scheme, the "master" and "slave" roles are
1912 * figured out through the slot the controller is in. Although it
1913 * is an active/active system, someone has to be in charge.
1914 */
1915 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1916 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1917 "HA head ID (0 - no HA)");
1918 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) {
1919 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1920 softc->is_single = 1;
1921 softc->port_cnt = ctl_max_ports;
1922 softc->port_min = 0;
1923 } else {
1924 softc->port_cnt = ctl_max_ports / NUM_HA_SHELVES;
1925 softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
1926 }
1927 softc->port_max = softc->port_min + softc->port_cnt;
1928 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
1929 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
1930
1931 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1932 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
1933 "HA link state (0 - offline, 1 - unknown, 2 - online)");
1934
1935 STAILQ_INIT(&softc->lun_list);
1936 STAILQ_INIT(&softc->fe_list);
1937 STAILQ_INIT(&softc->port_list);
1938 STAILQ_INIT(&softc->be_list);
1939 ctl_tpc_init(softc);
1940
1941 if (worker_threads <= 0)
1942 worker_threads = max(1, mp_ncpus / 4);
1943 if (worker_threads > CTL_MAX_THREADS)
1944 worker_threads = CTL_MAX_THREADS;
1945
1946 for (i = 0; i < worker_threads; i++) {
1947 struct ctl_thread *thr = &softc->threads[i];
1948
1949 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1950 thr->ctl_softc = softc;
1951 STAILQ_INIT(&thr->incoming_queue);
1952 STAILQ_INIT(&thr->rtr_queue);
1953 STAILQ_INIT(&thr->done_queue);
1954 STAILQ_INIT(&thr->isc_queue);
1955
1956 error = kproc_kthread_add(ctl_work_thread, thr,
1957 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1958 if (error != 0) {
1959 printf("error creating CTL work thread!\n");
1960 return (error);
1961 }
1962 }
1963 error = kproc_kthread_add(ctl_thresh_thread, softc,
1964 &softc->ctl_proc, &softc->thresh_thread, 0, 0, "ctl", "thresh");
1965 if (error != 0) {
1966 printf("error creating CTL threshold thread!\n");
1967 return (error);
1968 }
1969
1970 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1971 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN,
1972 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
1973
1974 if (softc->is_single == 0) {
1975 if (ctl_frontend_register(&ha_frontend) != 0)
1976 softc->is_single = 1;
1977 }
1978 return (0);
1979 }
1980
1981 static int
ctl_shutdown(void)1982 ctl_shutdown(void)
1983 {
1984 struct ctl_softc *softc = control_softc;
1985 int i;
1986
1987 if (softc->is_single == 0)
1988 ctl_frontend_deregister(&ha_frontend);
1989
1990 destroy_dev(softc->dev);
1991
1992 /* Shutdown CTL threads. */
1993 softc->shutdown = 1;
1994 for (i = 0; i < worker_threads; i++) {
1995 struct ctl_thread *thr = &softc->threads[i];
1996 while (thr->thread != NULL) {
1997 wakeup(thr);
1998 if (thr->thread != NULL)
1999 pause("CTL thr shutdown", 1);
2000 }
2001 mtx_destroy(&thr->queue_lock);
2002 }
2003 while (softc->thresh_thread != NULL) {
2004 wakeup(softc->thresh_thread);
2005 if (softc->thresh_thread != NULL)
2006 pause("CTL thr shutdown", 1);
2007 }
2008
2009 ctl_tpc_shutdown(softc);
2010 uma_zdestroy(softc->io_zone);
2011 mtx_destroy(&softc->ctl_lock);
2012
2013 free(softc->ctl_luns, M_DEVBUF);
2014 free(softc->ctl_lun_mask, M_DEVBUF);
2015 free(softc->ctl_port_mask, M_DEVBUF);
2016 free(softc->ctl_ports, M_DEVBUF);
2017
2018 sysctl_ctx_free(&softc->sysctl_ctx);
2019
2020 free(softc, M_DEVBUF);
2021 control_softc = NULL;
2022 return (0);
2023 }
2024
2025 static int
ctl_module_event_handler(module_t mod,int what,void * arg)2026 ctl_module_event_handler(module_t mod, int what, void *arg)
2027 {
2028
2029 switch (what) {
2030 case MOD_LOAD:
2031 return (ctl_init());
2032 case MOD_UNLOAD:
2033 return (ctl_shutdown());
2034 default:
2035 return (EOPNOTSUPP);
2036 }
2037 }
2038
2039 /*
2040 * XXX KDM should we do some access checks here? Bump a reference count to
2041 * prevent a CTL module from being unloaded while someone has it open?
2042 */
2043 static int
ctl_open(struct cdev * dev,int flags,int fmt,struct thread * td)2044 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
2045 {
2046 return (0);
2047 }
2048
2049 static int
ctl_close(struct cdev * dev,int flags,int fmt,struct thread * td)2050 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
2051 {
2052 return (0);
2053 }
2054
2055 /*
2056 * Remove an initiator by port number and initiator ID.
2057 * Returns 0 for success, -1 for failure.
2058 */
2059 int
ctl_remove_initiator(struct ctl_port * port,int iid)2060 ctl_remove_initiator(struct ctl_port *port, int iid)
2061 {
2062 struct ctl_softc *softc = port->ctl_softc;
2063 int last;
2064
2065 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2066
2067 if (iid > CTL_MAX_INIT_PER_PORT) {
2068 printf("%s: initiator ID %u > maximun %u!\n",
2069 __func__, iid, CTL_MAX_INIT_PER_PORT);
2070 return (-1);
2071 }
2072
2073 mtx_lock(&softc->ctl_lock);
2074 last = (--port->wwpn_iid[iid].in_use == 0);
2075 port->wwpn_iid[iid].last_use = time_uptime;
2076 mtx_unlock(&softc->ctl_lock);
2077 if (last)
2078 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON);
2079 ctl_isc_announce_iid(port, iid);
2080
2081 return (0);
2082 }
2083
2084 /*
2085 * Add an initiator to the initiator map.
2086 * Returns iid for success, < 0 for failure.
2087 */
2088 int
ctl_add_initiator(struct ctl_port * port,int iid,uint64_t wwpn,char * name)2089 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
2090 {
2091 struct ctl_softc *softc = port->ctl_softc;
2092 time_t best_time;
2093 int i, best;
2094
2095 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2096
2097 if (iid >= CTL_MAX_INIT_PER_PORT) {
2098 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
2099 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
2100 free(name, M_CTL);
2101 return (-1);
2102 }
2103
2104 mtx_lock(&softc->ctl_lock);
2105
2106 if (iid < 0 && (wwpn != 0 || name != NULL)) {
2107 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2108 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
2109 iid = i;
2110 break;
2111 }
2112 if (name != NULL && port->wwpn_iid[i].name != NULL &&
2113 strcmp(name, port->wwpn_iid[i].name) == 0) {
2114 iid = i;
2115 break;
2116 }
2117 }
2118 }
2119
2120 if (iid < 0) {
2121 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2122 if (port->wwpn_iid[i].in_use == 0 &&
2123 port->wwpn_iid[i].wwpn == 0 &&
2124 port->wwpn_iid[i].name == NULL) {
2125 iid = i;
2126 break;
2127 }
2128 }
2129 }
2130
2131 if (iid < 0) {
2132 best = -1;
2133 best_time = INT32_MAX;
2134 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2135 if (port->wwpn_iid[i].in_use == 0) {
2136 if (port->wwpn_iid[i].last_use < best_time) {
2137 best = i;
2138 best_time = port->wwpn_iid[i].last_use;
2139 }
2140 }
2141 }
2142 iid = best;
2143 }
2144
2145 if (iid < 0) {
2146 mtx_unlock(&softc->ctl_lock);
2147 free(name, M_CTL);
2148 return (-2);
2149 }
2150
2151 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
2152 /*
2153 * This is not an error yet.
2154 */
2155 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
2156 #if 0
2157 printf("%s: port %d iid %u WWPN %#jx arrived"
2158 " again\n", __func__, port->targ_port,
2159 iid, (uintmax_t)wwpn);
2160 #endif
2161 goto take;
2162 }
2163 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
2164 strcmp(name, port->wwpn_iid[iid].name) == 0) {
2165 #if 0
2166 printf("%s: port %d iid %u name '%s' arrived"
2167 " again\n", __func__, port->targ_port,
2168 iid, name);
2169 #endif
2170 goto take;
2171 }
2172
2173 /*
2174 * This is an error, but what do we do about it? The
2175 * driver is telling us we have a new WWPN for this
2176 * initiator ID, so we pretty much need to use it.
2177 */
2178 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
2179 " but WWPN %#jx '%s' is still at that address\n",
2180 __func__, port->targ_port, iid, wwpn, name,
2181 (uintmax_t)port->wwpn_iid[iid].wwpn,
2182 port->wwpn_iid[iid].name);
2183 }
2184 take:
2185 free(port->wwpn_iid[iid].name, M_CTL);
2186 port->wwpn_iid[iid].name = name;
2187 port->wwpn_iid[iid].wwpn = wwpn;
2188 port->wwpn_iid[iid].in_use++;
2189 mtx_unlock(&softc->ctl_lock);
2190 ctl_isc_announce_iid(port, iid);
2191
2192 return (iid);
2193 }
2194
2195 static int
ctl_create_iid(struct ctl_port * port,int iid,uint8_t * buf)2196 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
2197 {
2198 int len;
2199
2200 switch (port->port_type) {
2201 case CTL_PORT_FC:
2202 {
2203 struct scsi_transportid_fcp *id =
2204 (struct scsi_transportid_fcp *)buf;
2205 if (port->wwpn_iid[iid].wwpn == 0)
2206 return (0);
2207 memset(id, 0, sizeof(*id));
2208 id->format_protocol = SCSI_PROTO_FC;
2209 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
2210 return (sizeof(*id));
2211 }
2212 case CTL_PORT_ISCSI:
2213 {
2214 struct scsi_transportid_iscsi_port *id =
2215 (struct scsi_transportid_iscsi_port *)buf;
2216 if (port->wwpn_iid[iid].name == NULL)
2217 return (0);
2218 memset(id, 0, 256);
2219 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
2220 SCSI_PROTO_ISCSI;
2221 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
2222 len = roundup2(min(len, 252), 4);
2223 scsi_ulto2b(len, id->additional_length);
2224 return (sizeof(*id) + len);
2225 }
2226 case CTL_PORT_SAS:
2227 {
2228 struct scsi_transportid_sas *id =
2229 (struct scsi_transportid_sas *)buf;
2230 if (port->wwpn_iid[iid].wwpn == 0)
2231 return (0);
2232 memset(id, 0, sizeof(*id));
2233 id->format_protocol = SCSI_PROTO_SAS;
2234 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
2235 return (sizeof(*id));
2236 }
2237 default:
2238 {
2239 struct scsi_transportid_spi *id =
2240 (struct scsi_transportid_spi *)buf;
2241 memset(id, 0, sizeof(*id));
2242 id->format_protocol = SCSI_PROTO_SPI;
2243 scsi_ulto2b(iid, id->scsi_addr);
2244 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
2245 return (sizeof(*id));
2246 }
2247 }
2248 }
2249
2250 /*
2251 * Serialize a command that went down the "wrong" side, and so was sent to
2252 * this controller for execution. The logic is a little different than the
2253 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
2254 * sent back to the other side, but in the success case, we execute the
2255 * command on this side (XFER mode) or tell the other side to execute it
2256 * (SER_ONLY mode).
2257 */
2258 static void
ctl_serialize_other_sc_cmd(struct ctl_scsiio * ctsio)2259 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
2260 {
2261 struct ctl_softc *softc = CTL_SOFTC(ctsio);
2262 struct ctl_port *port = CTL_PORT(ctsio);
2263 union ctl_ha_msg msg_info;
2264 struct ctl_lun *lun;
2265 const struct ctl_cmd_entry *entry;
2266 union ctl_io *bio;
2267 uint32_t targ_lun;
2268
2269 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
2270
2271 /* Make sure that we know about this port. */
2272 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) {
2273 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2274 /*retry_count*/ 1);
2275 goto badjuju;
2276 }
2277
2278 /* Make sure that we know about this LUN. */
2279 mtx_lock(&softc->ctl_lock);
2280 if (targ_lun >= ctl_max_luns ||
2281 (lun = softc->ctl_luns[targ_lun]) == NULL) {
2282 mtx_unlock(&softc->ctl_lock);
2283
2284 /*
2285 * The other node would not send this request to us unless
2286 * received announce that we are primary node for this LUN.
2287 * If this LUN does not exist now, it is probably result of
2288 * a race, so respond to initiator in the most opaque way.
2289 */
2290 ctl_set_busy(ctsio);
2291 goto badjuju;
2292 }
2293 mtx_lock(&lun->lun_lock);
2294 mtx_unlock(&softc->ctl_lock);
2295
2296 /*
2297 * If the LUN is invalid, pretend that it doesn't exist.
2298 * It will go away as soon as all pending I/Os completed.
2299 */
2300 if (lun->flags & CTL_LUN_DISABLED) {
2301 mtx_unlock(&lun->lun_lock);
2302 ctl_set_busy(ctsio);
2303 goto badjuju;
2304 }
2305
2306 entry = ctl_get_cmd_entry(ctsio, NULL);
2307 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
2308 mtx_unlock(&lun->lun_lock);
2309 goto badjuju;
2310 }
2311
2312 CTL_LUN(ctsio) = lun;
2313 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
2314
2315 /*
2316 * Every I/O goes into the OOA queue for a
2317 * particular LUN, and stays there until completion.
2318 */
2319 #ifdef CTL_TIME_IO
2320 if (TAILQ_EMPTY(&lun->ooa_queue))
2321 lun->idle_time += getsbinuptime() - lun->last_busy;
2322 #endif
2323 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2324
2325 bio = (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, ooa_links);
2326 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, &bio)) {
2327 case CTL_ACTION_BLOCK:
2328 ctsio->io_hdr.blocker = bio;
2329 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctsio->io_hdr,
2330 blocked_links);
2331 mtx_unlock(&lun->lun_lock);
2332 break;
2333 case CTL_ACTION_PASS:
2334 case CTL_ACTION_SKIP:
2335 if (softc->ha_mode == CTL_HA_MODE_XFER) {
2336 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
2337 ctl_enqueue_rtr((union ctl_io *)ctsio);
2338 mtx_unlock(&lun->lun_lock);
2339 } else {
2340 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
2341 mtx_unlock(&lun->lun_lock);
2342
2343 /* send msg back to other side */
2344 msg_info.hdr.original_sc = ctsio->io_hdr.remote_io;
2345 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
2346 msg_info.hdr.msg_type = CTL_MSG_R2R;
2347 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2348 sizeof(msg_info.hdr), M_WAITOK);
2349 }
2350 break;
2351 case CTL_ACTION_OVERLAP:
2352 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2353 mtx_unlock(&lun->lun_lock);
2354 ctl_set_overlapped_cmd(ctsio);
2355 goto badjuju;
2356 case CTL_ACTION_OVERLAP_TAG:
2357 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2358 mtx_unlock(&lun->lun_lock);
2359 ctl_set_overlapped_tag(ctsio, ctsio->tag_num);
2360 goto badjuju;
2361 case CTL_ACTION_ERROR:
2362 default:
2363 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2364 mtx_unlock(&lun->lun_lock);
2365
2366 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2367 /*retry_count*/ 0);
2368 badjuju:
2369 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
2370 msg_info.hdr.original_sc = ctsio->io_hdr.remote_io;
2371 msg_info.hdr.serializing_sc = NULL;
2372 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
2373 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2374 sizeof(msg_info.scsi), M_WAITOK);
2375 ctl_free_io((union ctl_io *)ctsio);
2376 break;
2377 }
2378 }
2379
2380 /*
2381 * Returns 0 for success, errno for failure.
2382 */
2383 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)2384 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2385 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2386 {
2387 union ctl_io *io;
2388
2389 mtx_lock(&lun->lun_lock);
2390 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2391 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2392 ooa_links)) {
2393 struct ctl_ooa_entry *entry;
2394
2395 /*
2396 * If we've got more than we can fit, just count the
2397 * remaining entries.
2398 */
2399 if (*cur_fill_num >= ooa_hdr->alloc_num)
2400 continue;
2401
2402 entry = &kern_entries[*cur_fill_num];
2403
2404 entry->tag_num = io->scsiio.tag_num;
2405 entry->lun_num = lun->lun;
2406 #ifdef CTL_TIME_IO
2407 entry->start_bt = io->io_hdr.start_bt;
2408 #endif
2409 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2410 entry->cdb_len = io->scsiio.cdb_len;
2411 if (io->io_hdr.blocker != NULL)
2412 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2413
2414 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2415 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2416
2417 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2418 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2419
2420 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2421 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2422
2423 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2424 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2425 }
2426 mtx_unlock(&lun->lun_lock);
2427 }
2428
2429 static void *
ctl_copyin_alloc(void * user_addr,unsigned int len,char * error_str,size_t error_str_len)2430 ctl_copyin_alloc(void *user_addr, unsigned int len, char *error_str,
2431 size_t error_str_len)
2432 {
2433 void *kptr;
2434
2435 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2436
2437 if (copyin(user_addr, kptr, len) != 0) {
2438 snprintf(error_str, error_str_len, "Error copying %d bytes "
2439 "from user address %p to kernel address %p", len,
2440 user_addr, kptr);
2441 free(kptr, M_CTL);
2442 return (NULL);
2443 }
2444
2445 return (kptr);
2446 }
2447
2448 static void
ctl_free_args(int num_args,struct ctl_be_arg * args)2449 ctl_free_args(int num_args, struct ctl_be_arg *args)
2450 {
2451 int i;
2452
2453 if (args == NULL)
2454 return;
2455
2456 for (i = 0; i < num_args; i++) {
2457 free(args[i].kname, M_CTL);
2458 free(args[i].kvalue, M_CTL);
2459 }
2460
2461 free(args, M_CTL);
2462 }
2463
2464 static struct ctl_be_arg *
ctl_copyin_args(int num_args,struct ctl_be_arg * uargs,char * error_str,size_t error_str_len)2465 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2466 char *error_str, size_t error_str_len)
2467 {
2468 struct ctl_be_arg *args;
2469 int i;
2470
2471 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2472 error_str, error_str_len);
2473
2474 if (args == NULL)
2475 goto bailout;
2476
2477 for (i = 0; i < num_args; i++) {
2478 args[i].kname = NULL;
2479 args[i].kvalue = NULL;
2480 }
2481
2482 for (i = 0; i < num_args; i++) {
2483 uint8_t *tmpptr;
2484
2485 if (args[i].namelen == 0) {
2486 snprintf(error_str, error_str_len, "Argument %d "
2487 "name length is zero", i);
2488 goto bailout;
2489 }
2490
2491 args[i].kname = ctl_copyin_alloc(args[i].name,
2492 args[i].namelen, error_str, error_str_len);
2493 if (args[i].kname == NULL)
2494 goto bailout;
2495
2496 if (args[i].kname[args[i].namelen - 1] != '\0') {
2497 snprintf(error_str, error_str_len, "Argument %d "
2498 "name is not NUL-terminated", i);
2499 goto bailout;
2500 }
2501
2502 if (args[i].flags & CTL_BEARG_RD) {
2503 if (args[i].vallen == 0) {
2504 snprintf(error_str, error_str_len, "Argument %d "
2505 "value length is zero", i);
2506 goto bailout;
2507 }
2508
2509 tmpptr = ctl_copyin_alloc(args[i].value,
2510 args[i].vallen, error_str, error_str_len);
2511 if (tmpptr == NULL)
2512 goto bailout;
2513
2514 if ((args[i].flags & CTL_BEARG_ASCII)
2515 && (tmpptr[args[i].vallen - 1] != '\0')) {
2516 snprintf(error_str, error_str_len, "Argument "
2517 "%d value is not NUL-terminated", i);
2518 free(tmpptr, M_CTL);
2519 goto bailout;
2520 }
2521 args[i].kvalue = tmpptr;
2522 } else {
2523 args[i].kvalue = malloc(args[i].vallen,
2524 M_CTL, M_WAITOK | M_ZERO);
2525 }
2526 }
2527
2528 return (args);
2529 bailout:
2530
2531 ctl_free_args(num_args, args);
2532
2533 return (NULL);
2534 }
2535
2536 static void
ctl_copyout_args(int num_args,struct ctl_be_arg * args)2537 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2538 {
2539 int i;
2540
2541 for (i = 0; i < num_args; i++) {
2542 if (args[i].flags & CTL_BEARG_WR)
2543 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2544 }
2545 }
2546
2547 /*
2548 * Escape characters that are illegal or not recommended in XML.
2549 */
2550 int
ctl_sbuf_printf_esc(struct sbuf * sb,char * str,int size)2551 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2552 {
2553 char *end = str + size;
2554 int retval;
2555
2556 retval = 0;
2557
2558 for (; *str && str < end; str++) {
2559 switch (*str) {
2560 case '&':
2561 retval = sbuf_printf(sb, "&");
2562 break;
2563 case '>':
2564 retval = sbuf_printf(sb, ">");
2565 break;
2566 case '<':
2567 retval = sbuf_printf(sb, "<");
2568 break;
2569 default:
2570 retval = sbuf_putc(sb, *str);
2571 break;
2572 }
2573
2574 if (retval != 0)
2575 break;
2576
2577 }
2578
2579 return (retval);
2580 }
2581
2582 static void
ctl_id_sbuf(struct ctl_devid * id,struct sbuf * sb)2583 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2584 {
2585 struct scsi_vpd_id_descriptor *desc;
2586 int i;
2587
2588 if (id == NULL || id->len < 4)
2589 return;
2590 desc = (struct scsi_vpd_id_descriptor *)id->data;
2591 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2592 case SVPD_ID_TYPE_T10:
2593 sbuf_printf(sb, "t10.");
2594 break;
2595 case SVPD_ID_TYPE_EUI64:
2596 sbuf_printf(sb, "eui.");
2597 break;
2598 case SVPD_ID_TYPE_NAA:
2599 sbuf_printf(sb, "naa.");
2600 break;
2601 case SVPD_ID_TYPE_SCSI_NAME:
2602 break;
2603 }
2604 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2605 case SVPD_ID_CODESET_BINARY:
2606 for (i = 0; i < desc->length; i++)
2607 sbuf_printf(sb, "%02x", desc->identifier[i]);
2608 break;
2609 case SVPD_ID_CODESET_ASCII:
2610 sbuf_printf(sb, "%.*s", (int)desc->length,
2611 (char *)desc->identifier);
2612 break;
2613 case SVPD_ID_CODESET_UTF8:
2614 sbuf_printf(sb, "%s", (char *)desc->identifier);
2615 break;
2616 }
2617 }
2618
2619 static int
ctl_ioctl(struct cdev * dev,u_long cmd,caddr_t addr,int flag,struct thread * td)2620 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2621 struct thread *td)
2622 {
2623 struct ctl_softc *softc = dev->si_drv1;
2624 struct ctl_port *port;
2625 struct ctl_lun *lun;
2626 int retval;
2627
2628 retval = 0;
2629
2630 switch (cmd) {
2631 case CTL_IO:
2632 retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2633 break;
2634 case CTL_ENABLE_PORT:
2635 case CTL_DISABLE_PORT:
2636 case CTL_SET_PORT_WWNS: {
2637 struct ctl_port *port;
2638 struct ctl_port_entry *entry;
2639
2640 entry = (struct ctl_port_entry *)addr;
2641
2642 mtx_lock(&softc->ctl_lock);
2643 STAILQ_FOREACH(port, &softc->port_list, links) {
2644 int action, done;
2645
2646 if (port->targ_port < softc->port_min ||
2647 port->targ_port >= softc->port_max)
2648 continue;
2649
2650 action = 0;
2651 done = 0;
2652 if ((entry->port_type == CTL_PORT_NONE)
2653 && (entry->targ_port == port->targ_port)) {
2654 /*
2655 * If the user only wants to enable or
2656 * disable or set WWNs on a specific port,
2657 * do the operation and we're done.
2658 */
2659 action = 1;
2660 done = 1;
2661 } else if (entry->port_type & port->port_type) {
2662 /*
2663 * Compare the user's type mask with the
2664 * particular frontend type to see if we
2665 * have a match.
2666 */
2667 action = 1;
2668 done = 0;
2669
2670 /*
2671 * Make sure the user isn't trying to set
2672 * WWNs on multiple ports at the same time.
2673 */
2674 if (cmd == CTL_SET_PORT_WWNS) {
2675 printf("%s: Can't set WWNs on "
2676 "multiple ports\n", __func__);
2677 retval = EINVAL;
2678 break;
2679 }
2680 }
2681 if (action == 0)
2682 continue;
2683
2684 /*
2685 * XXX KDM we have to drop the lock here, because
2686 * the online/offline operations can potentially
2687 * block. We need to reference count the frontends
2688 * so they can't go away,
2689 */
2690 if (cmd == CTL_ENABLE_PORT) {
2691 mtx_unlock(&softc->ctl_lock);
2692 ctl_port_online(port);
2693 mtx_lock(&softc->ctl_lock);
2694 } else if (cmd == CTL_DISABLE_PORT) {
2695 mtx_unlock(&softc->ctl_lock);
2696 ctl_port_offline(port);
2697 mtx_lock(&softc->ctl_lock);
2698 } else if (cmd == CTL_SET_PORT_WWNS) {
2699 ctl_port_set_wwns(port,
2700 (entry->flags & CTL_PORT_WWNN_VALID) ?
2701 1 : 0, entry->wwnn,
2702 (entry->flags & CTL_PORT_WWPN_VALID) ?
2703 1 : 0, entry->wwpn);
2704 }
2705 if (done != 0)
2706 break;
2707 }
2708 mtx_unlock(&softc->ctl_lock);
2709 break;
2710 }
2711 case CTL_GET_OOA: {
2712 struct ctl_ooa *ooa_hdr;
2713 struct ctl_ooa_entry *entries;
2714 uint32_t cur_fill_num;
2715
2716 ooa_hdr = (struct ctl_ooa *)addr;
2717
2718 if ((ooa_hdr->alloc_len == 0)
2719 || (ooa_hdr->alloc_num == 0)) {
2720 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2721 "must be non-zero\n", __func__,
2722 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2723 retval = EINVAL;
2724 break;
2725 }
2726
2727 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2728 sizeof(struct ctl_ooa_entry))) {
2729 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2730 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2731 __func__, ooa_hdr->alloc_len,
2732 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2733 retval = EINVAL;
2734 break;
2735 }
2736
2737 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2738 if (entries == NULL) {
2739 printf("%s: could not allocate %d bytes for OOA "
2740 "dump\n", __func__, ooa_hdr->alloc_len);
2741 retval = ENOMEM;
2742 break;
2743 }
2744
2745 mtx_lock(&softc->ctl_lock);
2746 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 &&
2747 (ooa_hdr->lun_num >= ctl_max_luns ||
2748 softc->ctl_luns[ooa_hdr->lun_num] == NULL)) {
2749 mtx_unlock(&softc->ctl_lock);
2750 free(entries, M_CTL);
2751 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2752 __func__, (uintmax_t)ooa_hdr->lun_num);
2753 retval = EINVAL;
2754 break;
2755 }
2756
2757 cur_fill_num = 0;
2758
2759 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2760 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2761 ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2762 ooa_hdr, entries);
2763 }
2764 } else {
2765 lun = softc->ctl_luns[ooa_hdr->lun_num];
2766 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr,
2767 entries);
2768 }
2769 mtx_unlock(&softc->ctl_lock);
2770
2771 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2772 ooa_hdr->fill_len = ooa_hdr->fill_num *
2773 sizeof(struct ctl_ooa_entry);
2774 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2775 if (retval != 0) {
2776 printf("%s: error copying out %d bytes for OOA dump\n",
2777 __func__, ooa_hdr->fill_len);
2778 }
2779
2780 getbinuptime(&ooa_hdr->cur_bt);
2781
2782 if (cur_fill_num > ooa_hdr->alloc_num) {
2783 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2784 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2785 } else {
2786 ooa_hdr->dropped_num = 0;
2787 ooa_hdr->status = CTL_OOA_OK;
2788 }
2789
2790 free(entries, M_CTL);
2791 break;
2792 }
2793 case CTL_DELAY_IO: {
2794 struct ctl_io_delay_info *delay_info;
2795
2796 delay_info = (struct ctl_io_delay_info *)addr;
2797
2798 #ifdef CTL_IO_DELAY
2799 mtx_lock(&softc->ctl_lock);
2800 if (delay_info->lun_id >= ctl_max_luns ||
2801 (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) {
2802 mtx_unlock(&softc->ctl_lock);
2803 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2804 break;
2805 }
2806 mtx_lock(&lun->lun_lock);
2807 mtx_unlock(&softc->ctl_lock);
2808 delay_info->status = CTL_DELAY_STATUS_OK;
2809 switch (delay_info->delay_type) {
2810 case CTL_DELAY_TYPE_CONT:
2811 case CTL_DELAY_TYPE_ONESHOT:
2812 break;
2813 default:
2814 delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE;
2815 break;
2816 }
2817 switch (delay_info->delay_loc) {
2818 case CTL_DELAY_LOC_DATAMOVE:
2819 lun->delay_info.datamove_type = delay_info->delay_type;
2820 lun->delay_info.datamove_delay = delay_info->delay_secs;
2821 break;
2822 case CTL_DELAY_LOC_DONE:
2823 lun->delay_info.done_type = delay_info->delay_type;
2824 lun->delay_info.done_delay = delay_info->delay_secs;
2825 break;
2826 default:
2827 delay_info->status = CTL_DELAY_STATUS_INVALID_LOC;
2828 break;
2829 }
2830 mtx_unlock(&lun->lun_lock);
2831 #else
2832 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2833 #endif /* CTL_IO_DELAY */
2834 break;
2835 }
2836 #ifdef CTL_LEGACY_STATS
2837 case CTL_GETSTATS: {
2838 struct ctl_stats *stats = (struct ctl_stats *)addr;
2839 int i;
2840
2841 /*
2842 * XXX KDM no locking here. If the LUN list changes,
2843 * things can blow up.
2844 */
2845 i = 0;
2846 stats->status = CTL_SS_OK;
2847 stats->fill_len = 0;
2848 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2849 if (stats->fill_len + sizeof(lun->legacy_stats) >
2850 stats->alloc_len) {
2851 stats->status = CTL_SS_NEED_MORE_SPACE;
2852 break;
2853 }
2854 retval = copyout(&lun->legacy_stats, &stats->lun_stats[i++],
2855 sizeof(lun->legacy_stats));
2856 if (retval != 0)
2857 break;
2858 stats->fill_len += sizeof(lun->legacy_stats);
2859 }
2860 stats->num_luns = softc->num_luns;
2861 stats->flags = CTL_STATS_FLAG_NONE;
2862 #ifdef CTL_TIME_IO
2863 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
2864 #endif
2865 getnanouptime(&stats->timestamp);
2866 break;
2867 }
2868 #endif /* CTL_LEGACY_STATS */
2869 case CTL_ERROR_INJECT: {
2870 struct ctl_error_desc *err_desc, *new_err_desc;
2871
2872 err_desc = (struct ctl_error_desc *)addr;
2873
2874 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2875 M_WAITOK | M_ZERO);
2876 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2877
2878 mtx_lock(&softc->ctl_lock);
2879 if (err_desc->lun_id >= ctl_max_luns ||
2880 (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) {
2881 mtx_unlock(&softc->ctl_lock);
2882 free(new_err_desc, M_CTL);
2883 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2884 __func__, (uintmax_t)err_desc->lun_id);
2885 retval = EINVAL;
2886 break;
2887 }
2888 mtx_lock(&lun->lun_lock);
2889 mtx_unlock(&softc->ctl_lock);
2890
2891 /*
2892 * We could do some checking here to verify the validity
2893 * of the request, but given the complexity of error
2894 * injection requests, the checking logic would be fairly
2895 * complex.
2896 *
2897 * For now, if the request is invalid, it just won't get
2898 * executed and might get deleted.
2899 */
2900 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2901
2902 /*
2903 * XXX KDM check to make sure the serial number is unique,
2904 * in case we somehow manage to wrap. That shouldn't
2905 * happen for a very long time, but it's the right thing to
2906 * do.
2907 */
2908 new_err_desc->serial = lun->error_serial;
2909 err_desc->serial = lun->error_serial;
2910 lun->error_serial++;
2911
2912 mtx_unlock(&lun->lun_lock);
2913 break;
2914 }
2915 case CTL_ERROR_INJECT_DELETE: {
2916 struct ctl_error_desc *delete_desc, *desc, *desc2;
2917 int delete_done;
2918
2919 delete_desc = (struct ctl_error_desc *)addr;
2920 delete_done = 0;
2921
2922 mtx_lock(&softc->ctl_lock);
2923 if (delete_desc->lun_id >= ctl_max_luns ||
2924 (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) {
2925 mtx_unlock(&softc->ctl_lock);
2926 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2927 __func__, (uintmax_t)delete_desc->lun_id);
2928 retval = EINVAL;
2929 break;
2930 }
2931 mtx_lock(&lun->lun_lock);
2932 mtx_unlock(&softc->ctl_lock);
2933 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2934 if (desc->serial != delete_desc->serial)
2935 continue;
2936
2937 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2938 links);
2939 free(desc, M_CTL);
2940 delete_done = 1;
2941 }
2942 mtx_unlock(&lun->lun_lock);
2943 if (delete_done == 0) {
2944 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2945 "error serial %ju on LUN %u\n", __func__,
2946 delete_desc->serial, delete_desc->lun_id);
2947 retval = EINVAL;
2948 break;
2949 }
2950 break;
2951 }
2952 case CTL_DUMP_STRUCTS: {
2953 int j, k;
2954 struct ctl_port *port;
2955 struct ctl_frontend *fe;
2956
2957 mtx_lock(&softc->ctl_lock);
2958 printf("CTL Persistent Reservation information start:\n");
2959 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2960 mtx_lock(&lun->lun_lock);
2961 if ((lun->flags & CTL_LUN_DISABLED) != 0) {
2962 mtx_unlock(&lun->lun_lock);
2963 continue;
2964 }
2965
2966 for (j = 0; j < ctl_max_ports; j++) {
2967 if (lun->pr_keys[j] == NULL)
2968 continue;
2969 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2970 if (lun->pr_keys[j][k] == 0)
2971 continue;
2972 printf(" LUN %ju port %d iid %d key "
2973 "%#jx\n", lun->lun, j, k,
2974 (uintmax_t)lun->pr_keys[j][k]);
2975 }
2976 }
2977 mtx_unlock(&lun->lun_lock);
2978 }
2979 printf("CTL Persistent Reservation information end\n");
2980 printf("CTL Ports:\n");
2981 STAILQ_FOREACH(port, &softc->port_list, links) {
2982 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2983 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
2984 port->frontend->name, port->port_type,
2985 port->physical_port, port->virtual_port,
2986 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2987 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2988 if (port->wwpn_iid[j].in_use == 0 &&
2989 port->wwpn_iid[j].wwpn == 0 &&
2990 port->wwpn_iid[j].name == NULL)
2991 continue;
2992
2993 printf(" iid %u use %d WWPN %#jx '%s'\n",
2994 j, port->wwpn_iid[j].in_use,
2995 (uintmax_t)port->wwpn_iid[j].wwpn,
2996 port->wwpn_iid[j].name);
2997 }
2998 }
2999 printf("CTL Port information end\n");
3000 mtx_unlock(&softc->ctl_lock);
3001 /*
3002 * XXX KDM calling this without a lock. We'd likely want
3003 * to drop the lock before calling the frontend's dump
3004 * routine anyway.
3005 */
3006 printf("CTL Frontends:\n");
3007 STAILQ_FOREACH(fe, &softc->fe_list, links) {
3008 printf(" Frontend '%s'\n", fe->name);
3009 if (fe->fe_dump != NULL)
3010 fe->fe_dump();
3011 }
3012 printf("CTL Frontend information end\n");
3013 break;
3014 }
3015 case CTL_LUN_REQ: {
3016 struct ctl_lun_req *lun_req;
3017 struct ctl_backend_driver *backend;
3018
3019 lun_req = (struct ctl_lun_req *)addr;
3020
3021 backend = ctl_backend_find(lun_req->backend);
3022 if (backend == NULL) {
3023 lun_req->status = CTL_LUN_ERROR;
3024 snprintf(lun_req->error_str,
3025 sizeof(lun_req->error_str),
3026 "Backend \"%s\" not found.",
3027 lun_req->backend);
3028 break;
3029 }
3030 if (lun_req->num_be_args > 0) {
3031 lun_req->kern_be_args = ctl_copyin_args(
3032 lun_req->num_be_args,
3033 lun_req->be_args,
3034 lun_req->error_str,
3035 sizeof(lun_req->error_str));
3036 if (lun_req->kern_be_args == NULL) {
3037 lun_req->status = CTL_LUN_ERROR;
3038 break;
3039 }
3040 }
3041
3042 retval = backend->ioctl(dev, cmd, addr, flag, td);
3043
3044 if (lun_req->num_be_args > 0) {
3045 ctl_copyout_args(lun_req->num_be_args,
3046 lun_req->kern_be_args);
3047 ctl_free_args(lun_req->num_be_args,
3048 lun_req->kern_be_args);
3049 }
3050 break;
3051 }
3052 case CTL_LUN_LIST: {
3053 struct sbuf *sb;
3054 struct ctl_lun_list *list;
3055 struct ctl_option *opt;
3056
3057 list = (struct ctl_lun_list *)addr;
3058
3059 /*
3060 * Allocate a fixed length sbuf here, based on the length
3061 * of the user's buffer. We could allocate an auto-extending
3062 * buffer, and then tell the user how much larger our
3063 * amount of data is than his buffer, but that presents
3064 * some problems:
3065 *
3066 * 1. The sbuf(9) routines use a blocking malloc, and so
3067 * we can't hold a lock while calling them with an
3068 * auto-extending buffer.
3069 *
3070 * 2. There is not currently a LUN reference counting
3071 * mechanism, outside of outstanding transactions on
3072 * the LUN's OOA queue. So a LUN could go away on us
3073 * while we're getting the LUN number, backend-specific
3074 * information, etc. Thus, given the way things
3075 * currently work, we need to hold the CTL lock while
3076 * grabbing LUN information.
3077 *
3078 * So, from the user's standpoint, the best thing to do is
3079 * allocate what he thinks is a reasonable buffer length,
3080 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3081 * double the buffer length and try again. (And repeat
3082 * that until he succeeds.)
3083 */
3084 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3085 if (sb == NULL) {
3086 list->status = CTL_LUN_LIST_ERROR;
3087 snprintf(list->error_str, sizeof(list->error_str),
3088 "Unable to allocate %d bytes for LUN list",
3089 list->alloc_len);
3090 break;
3091 }
3092
3093 sbuf_printf(sb, "<ctllunlist>\n");
3094
3095 mtx_lock(&softc->ctl_lock);
3096 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3097 mtx_lock(&lun->lun_lock);
3098 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3099 (uintmax_t)lun->lun);
3100
3101 /*
3102 * Bail out as soon as we see that we've overfilled
3103 * the buffer.
3104 */
3105 if (retval != 0)
3106 break;
3107
3108 retval = sbuf_printf(sb, "\t<backend_type>%s"
3109 "</backend_type>\n",
3110 (lun->backend == NULL) ? "none" :
3111 lun->backend->name);
3112
3113 if (retval != 0)
3114 break;
3115
3116 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3117 lun->be_lun->lun_type);
3118
3119 if (retval != 0)
3120 break;
3121
3122 if (lun->backend == NULL) {
3123 retval = sbuf_printf(sb, "</lun>\n");
3124 if (retval != 0)
3125 break;
3126 continue;
3127 }
3128
3129 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3130 (lun->be_lun->maxlba > 0) ?
3131 lun->be_lun->maxlba + 1 : 0);
3132
3133 if (retval != 0)
3134 break;
3135
3136 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3137 lun->be_lun->blocksize);
3138
3139 if (retval != 0)
3140 break;
3141
3142 retval = sbuf_printf(sb, "\t<serial_number>");
3143
3144 if (retval != 0)
3145 break;
3146
3147 retval = ctl_sbuf_printf_esc(sb,
3148 lun->be_lun->serial_num,
3149 sizeof(lun->be_lun->serial_num));
3150
3151 if (retval != 0)
3152 break;
3153
3154 retval = sbuf_printf(sb, "</serial_number>\n");
3155
3156 if (retval != 0)
3157 break;
3158
3159 retval = sbuf_printf(sb, "\t<device_id>");
3160
3161 if (retval != 0)
3162 break;
3163
3164 retval = ctl_sbuf_printf_esc(sb,
3165 lun->be_lun->device_id,
3166 sizeof(lun->be_lun->device_id));
3167
3168 if (retval != 0)
3169 break;
3170
3171 retval = sbuf_printf(sb, "</device_id>\n");
3172
3173 if (retval != 0)
3174 break;
3175
3176 if (lun->backend->lun_info != NULL) {
3177 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3178 if (retval != 0)
3179 break;
3180 }
3181 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3182 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3183 opt->name, opt->value, opt->name);
3184 if (retval != 0)
3185 break;
3186 }
3187
3188 retval = sbuf_printf(sb, "</lun>\n");
3189
3190 if (retval != 0)
3191 break;
3192 mtx_unlock(&lun->lun_lock);
3193 }
3194 if (lun != NULL)
3195 mtx_unlock(&lun->lun_lock);
3196 mtx_unlock(&softc->ctl_lock);
3197
3198 if ((retval != 0)
3199 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3200 retval = 0;
3201 sbuf_delete(sb);
3202 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3203 snprintf(list->error_str, sizeof(list->error_str),
3204 "Out of space, %d bytes is too small",
3205 list->alloc_len);
3206 break;
3207 }
3208
3209 sbuf_finish(sb);
3210
3211 retval = copyout(sbuf_data(sb), list->lun_xml,
3212 sbuf_len(sb) + 1);
3213
3214 list->fill_len = sbuf_len(sb) + 1;
3215 list->status = CTL_LUN_LIST_OK;
3216 sbuf_delete(sb);
3217 break;
3218 }
3219 case CTL_ISCSI: {
3220 struct ctl_iscsi *ci;
3221 struct ctl_frontend *fe;
3222
3223 ci = (struct ctl_iscsi *)addr;
3224
3225 fe = ctl_frontend_find("iscsi");
3226 if (fe == NULL) {
3227 ci->status = CTL_ISCSI_ERROR;
3228 snprintf(ci->error_str, sizeof(ci->error_str),
3229 "Frontend \"iscsi\" not found.");
3230 break;
3231 }
3232
3233 retval = fe->ioctl(dev, cmd, addr, flag, td);
3234 break;
3235 }
3236 case CTL_PORT_REQ: {
3237 struct ctl_req *req;
3238 struct ctl_frontend *fe;
3239
3240 req = (struct ctl_req *)addr;
3241
3242 fe = ctl_frontend_find(req->driver);
3243 if (fe == NULL) {
3244 req->status = CTL_LUN_ERROR;
3245 snprintf(req->error_str, sizeof(req->error_str),
3246 "Frontend \"%s\" not found.", req->driver);
3247 break;
3248 }
3249 if (req->num_args > 0) {
3250 req->kern_args = ctl_copyin_args(req->num_args,
3251 req->args, req->error_str, sizeof(req->error_str));
3252 if (req->kern_args == NULL) {
3253 req->status = CTL_LUN_ERROR;
3254 break;
3255 }
3256 }
3257
3258 if (fe->ioctl)
3259 retval = fe->ioctl(dev, cmd, addr, flag, td);
3260 else
3261 retval = ENODEV;
3262
3263 if (req->num_args > 0) {
3264 ctl_copyout_args(req->num_args, req->kern_args);
3265 ctl_free_args(req->num_args, req->kern_args);
3266 }
3267 break;
3268 }
3269 case CTL_PORT_LIST: {
3270 struct sbuf *sb;
3271 struct ctl_port *port;
3272 struct ctl_lun_list *list;
3273 struct ctl_option *opt;
3274 int j;
3275 uint32_t plun;
3276
3277 list = (struct ctl_lun_list *)addr;
3278
3279 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3280 if (sb == NULL) {
3281 list->status = CTL_LUN_LIST_ERROR;
3282 snprintf(list->error_str, sizeof(list->error_str),
3283 "Unable to allocate %d bytes for LUN list",
3284 list->alloc_len);
3285 break;
3286 }
3287
3288 sbuf_printf(sb, "<ctlportlist>\n");
3289
3290 mtx_lock(&softc->ctl_lock);
3291 STAILQ_FOREACH(port, &softc->port_list, links) {
3292 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3293 (uintmax_t)port->targ_port);
3294
3295 /*
3296 * Bail out as soon as we see that we've overfilled
3297 * the buffer.
3298 */
3299 if (retval != 0)
3300 break;
3301
3302 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3303 "</frontend_type>\n", port->frontend->name);
3304 if (retval != 0)
3305 break;
3306
3307 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3308 port->port_type);
3309 if (retval != 0)
3310 break;
3311
3312 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3313 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3314 if (retval != 0)
3315 break;
3316
3317 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3318 port->port_name);
3319 if (retval != 0)
3320 break;
3321
3322 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3323 port->physical_port);
3324 if (retval != 0)
3325 break;
3326
3327 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3328 port->virtual_port);
3329 if (retval != 0)
3330 break;
3331
3332 if (port->target_devid != NULL) {
3333 sbuf_printf(sb, "\t<target>");
3334 ctl_id_sbuf(port->target_devid, sb);
3335 sbuf_printf(sb, "</target>\n");
3336 }
3337
3338 if (port->port_devid != NULL) {
3339 sbuf_printf(sb, "\t<port>");
3340 ctl_id_sbuf(port->port_devid, sb);
3341 sbuf_printf(sb, "</port>\n");
3342 }
3343
3344 if (port->port_info != NULL) {
3345 retval = port->port_info(port->onoff_arg, sb);
3346 if (retval != 0)
3347 break;
3348 }
3349 STAILQ_FOREACH(opt, &port->options, links) {
3350 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3351 opt->name, opt->value, opt->name);
3352 if (retval != 0)
3353 break;
3354 }
3355
3356 if (port->lun_map != NULL) {
3357 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3358 for (j = 0; j < port->lun_map_size; j++) {
3359 plun = ctl_lun_map_from_port(port, j);
3360 if (plun == UINT32_MAX)
3361 continue;
3362 sbuf_printf(sb,
3363 "\t<lun id=\"%u\">%u</lun>\n",
3364 j, plun);
3365 }
3366 }
3367
3368 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3369 if (port->wwpn_iid[j].in_use == 0 ||
3370 (port->wwpn_iid[j].wwpn == 0 &&
3371 port->wwpn_iid[j].name == NULL))
3372 continue;
3373
3374 if (port->wwpn_iid[j].name != NULL)
3375 retval = sbuf_printf(sb,
3376 "\t<initiator id=\"%u\">%s</initiator>\n",
3377 j, port->wwpn_iid[j].name);
3378 else
3379 retval = sbuf_printf(sb,
3380 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3381 j, port->wwpn_iid[j].wwpn);
3382 if (retval != 0)
3383 break;
3384 }
3385 if (retval != 0)
3386 break;
3387
3388 retval = sbuf_printf(sb, "</targ_port>\n");
3389 if (retval != 0)
3390 break;
3391 }
3392 mtx_unlock(&softc->ctl_lock);
3393
3394 if ((retval != 0)
3395 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3396 retval = 0;
3397 sbuf_delete(sb);
3398 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3399 snprintf(list->error_str, sizeof(list->error_str),
3400 "Out of space, %d bytes is too small",
3401 list->alloc_len);
3402 break;
3403 }
3404
3405 sbuf_finish(sb);
3406
3407 retval = copyout(sbuf_data(sb), list->lun_xml,
3408 sbuf_len(sb) + 1);
3409
3410 list->fill_len = sbuf_len(sb) + 1;
3411 list->status = CTL_LUN_LIST_OK;
3412 sbuf_delete(sb);
3413 break;
3414 }
3415 case CTL_LUN_MAP: {
3416 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3417 struct ctl_port *port;
3418
3419 mtx_lock(&softc->ctl_lock);
3420 if (lm->port < softc->port_min ||
3421 lm->port >= softc->port_max ||
3422 (port = softc->ctl_ports[lm->port]) == NULL) {
3423 mtx_unlock(&softc->ctl_lock);
3424 return (ENXIO);
3425 }
3426 if (port->status & CTL_PORT_STATUS_ONLINE) {
3427 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3428 if (ctl_lun_map_to_port(port, lun->lun) ==
3429 UINT32_MAX)
3430 continue;
3431 mtx_lock(&lun->lun_lock);
3432 ctl_est_ua_port(lun, lm->port, -1,
3433 CTL_UA_LUN_CHANGE);
3434 mtx_unlock(&lun->lun_lock);
3435 }
3436 }
3437 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3438 if (lm->plun != UINT32_MAX) {
3439 if (lm->lun == UINT32_MAX)
3440 retval = ctl_lun_map_unset(port, lm->plun);
3441 else if (lm->lun < ctl_max_luns &&
3442 softc->ctl_luns[lm->lun] != NULL)
3443 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3444 else
3445 return (ENXIO);
3446 } else {
3447 if (lm->lun == UINT32_MAX)
3448 retval = ctl_lun_map_deinit(port);
3449 else
3450 retval = ctl_lun_map_init(port);
3451 }
3452 if (port->status & CTL_PORT_STATUS_ONLINE)
3453 ctl_isc_announce_port(port);
3454 break;
3455 }
3456 case CTL_GET_LUN_STATS: {
3457 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3458 int i;
3459
3460 /*
3461 * XXX KDM no locking here. If the LUN list changes,
3462 * things can blow up.
3463 */
3464 i = 0;
3465 stats->status = CTL_SS_OK;
3466 stats->fill_len = 0;
3467 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3468 if (lun->lun < stats->first_item)
3469 continue;
3470 if (stats->fill_len + sizeof(lun->stats) >
3471 stats->alloc_len) {
3472 stats->status = CTL_SS_NEED_MORE_SPACE;
3473 break;
3474 }
3475 retval = copyout(&lun->stats, &stats->stats[i++],
3476 sizeof(lun->stats));
3477 if (retval != 0)
3478 break;
3479 stats->fill_len += sizeof(lun->stats);
3480 }
3481 stats->num_items = softc->num_luns;
3482 stats->flags = CTL_STATS_FLAG_NONE;
3483 #ifdef CTL_TIME_IO
3484 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3485 #endif
3486 getnanouptime(&stats->timestamp);
3487 break;
3488 }
3489 case CTL_GET_PORT_STATS: {
3490 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3491 int i;
3492
3493 /*
3494 * XXX KDM no locking here. If the LUN list changes,
3495 * things can blow up.
3496 */
3497 i = 0;
3498 stats->status = CTL_SS_OK;
3499 stats->fill_len = 0;
3500 STAILQ_FOREACH(port, &softc->port_list, links) {
3501 if (port->targ_port < stats->first_item)
3502 continue;
3503 if (stats->fill_len + sizeof(port->stats) >
3504 stats->alloc_len) {
3505 stats->status = CTL_SS_NEED_MORE_SPACE;
3506 break;
3507 }
3508 retval = copyout(&port->stats, &stats->stats[i++],
3509 sizeof(port->stats));
3510 if (retval != 0)
3511 break;
3512 stats->fill_len += sizeof(port->stats);
3513 }
3514 stats->num_items = softc->num_ports;
3515 stats->flags = CTL_STATS_FLAG_NONE;
3516 #ifdef CTL_TIME_IO
3517 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3518 #endif
3519 getnanouptime(&stats->timestamp);
3520 break;
3521 }
3522 default: {
3523 /* XXX KDM should we fix this? */
3524 #if 0
3525 struct ctl_backend_driver *backend;
3526 unsigned int type;
3527 int found;
3528
3529 found = 0;
3530
3531 /*
3532 * We encode the backend type as the ioctl type for backend
3533 * ioctls. So parse it out here, and then search for a
3534 * backend of this type.
3535 */
3536 type = _IOC_TYPE(cmd);
3537
3538 STAILQ_FOREACH(backend, &softc->be_list, links) {
3539 if (backend->type == type) {
3540 found = 1;
3541 break;
3542 }
3543 }
3544 if (found == 0) {
3545 printf("ctl: unknown ioctl command %#lx or backend "
3546 "%d\n", cmd, type);
3547 retval = EINVAL;
3548 break;
3549 }
3550 retval = backend->ioctl(dev, cmd, addr, flag, td);
3551 #endif
3552 retval = ENOTTY;
3553 break;
3554 }
3555 }
3556 return (retval);
3557 }
3558
3559 uint32_t
ctl_get_initindex(struct ctl_nexus * nexus)3560 ctl_get_initindex(struct ctl_nexus *nexus)
3561 {
3562 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3563 }
3564
3565 int
ctl_lun_map_init(struct ctl_port * port)3566 ctl_lun_map_init(struct ctl_port *port)
3567 {
3568 struct ctl_softc *softc = port->ctl_softc;
3569 struct ctl_lun *lun;
3570 int size = ctl_lun_map_size;
3571 uint32_t i;
3572
3573 if (port->lun_map == NULL || port->lun_map_size < size) {
3574 port->lun_map_size = 0;
3575 free(port->lun_map, M_CTL);
3576 port->lun_map = malloc(size * sizeof(uint32_t),
3577 M_CTL, M_NOWAIT);
3578 }
3579 if (port->lun_map == NULL)
3580 return (ENOMEM);
3581 for (i = 0; i < size; i++)
3582 port->lun_map[i] = UINT32_MAX;
3583 port->lun_map_size = size;
3584 if (port->status & CTL_PORT_STATUS_ONLINE) {
3585 if (port->lun_disable != NULL) {
3586 STAILQ_FOREACH(lun, &softc->lun_list, links)
3587 port->lun_disable(port->targ_lun_arg, lun->lun);
3588 }
3589 ctl_isc_announce_port(port);
3590 }
3591 return (0);
3592 }
3593
3594 int
ctl_lun_map_deinit(struct ctl_port * port)3595 ctl_lun_map_deinit(struct ctl_port *port)
3596 {
3597 struct ctl_softc *softc = port->ctl_softc;
3598 struct ctl_lun *lun;
3599
3600 if (port->lun_map == NULL)
3601 return (0);
3602 port->lun_map_size = 0;
3603 free(port->lun_map, M_CTL);
3604 port->lun_map = NULL;
3605 if (port->status & CTL_PORT_STATUS_ONLINE) {
3606 if (port->lun_enable != NULL) {
3607 STAILQ_FOREACH(lun, &softc->lun_list, links)
3608 port->lun_enable(port->targ_lun_arg, lun->lun);
3609 }
3610 ctl_isc_announce_port(port);
3611 }
3612 return (0);
3613 }
3614
3615 int
ctl_lun_map_set(struct ctl_port * port,uint32_t plun,uint32_t glun)3616 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3617 {
3618 int status;
3619 uint32_t old;
3620
3621 if (port->lun_map == NULL) {
3622 status = ctl_lun_map_init(port);
3623 if (status != 0)
3624 return (status);
3625 }
3626 if (plun >= port->lun_map_size)
3627 return (EINVAL);
3628 old = port->lun_map[plun];
3629 port->lun_map[plun] = glun;
3630 if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) {
3631 if (port->lun_enable != NULL)
3632 port->lun_enable(port->targ_lun_arg, plun);
3633 ctl_isc_announce_port(port);
3634 }
3635 return (0);
3636 }
3637
3638 int
ctl_lun_map_unset(struct ctl_port * port,uint32_t plun)3639 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3640 {
3641 uint32_t old;
3642
3643 if (port->lun_map == NULL || plun >= port->lun_map_size)
3644 return (0);
3645 old = port->lun_map[plun];
3646 port->lun_map[plun] = UINT32_MAX;
3647 if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) {
3648 if (port->lun_disable != NULL)
3649 port->lun_disable(port->targ_lun_arg, plun);
3650 ctl_isc_announce_port(port);
3651 }
3652 return (0);
3653 }
3654
3655 uint32_t
ctl_lun_map_from_port(struct ctl_port * port,uint32_t lun_id)3656 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3657 {
3658
3659 if (port == NULL)
3660 return (UINT32_MAX);
3661 if (port->lun_map == NULL)
3662 return (lun_id);
3663 if (lun_id > port->lun_map_size)
3664 return (UINT32_MAX);
3665 return (port->lun_map[lun_id]);
3666 }
3667
3668 uint32_t
ctl_lun_map_to_port(struct ctl_port * port,uint32_t lun_id)3669 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3670 {
3671 uint32_t i;
3672
3673 if (port == NULL)
3674 return (UINT32_MAX);
3675 if (port->lun_map == NULL)
3676 return (lun_id);
3677 for (i = 0; i < port->lun_map_size; i++) {
3678 if (port->lun_map[i] == lun_id)
3679 return (i);
3680 }
3681 return (UINT32_MAX);
3682 }
3683
3684 uint32_t
ctl_decode_lun(uint64_t encoded)3685 ctl_decode_lun(uint64_t encoded)
3686 {
3687 uint8_t lun[8];
3688 uint32_t result = 0xffffffff;
3689
3690 be64enc(lun, encoded);
3691 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) {
3692 case RPL_LUNDATA_ATYP_PERIPH:
3693 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 &&
3694 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0)
3695 result = lun[1];
3696 break;
3697 case RPL_LUNDATA_ATYP_FLAT:
3698 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 &&
3699 lun[6] == 0 && lun[7] == 0)
3700 result = ((lun[0] & 0x3f) << 8) + lun[1];
3701 break;
3702 case RPL_LUNDATA_ATYP_EXTLUN:
3703 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) {
3704 case 0x02:
3705 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) {
3706 case 0x00:
3707 result = lun[1];
3708 break;
3709 case 0x10:
3710 result = (lun[1] << 16) + (lun[2] << 8) +
3711 lun[3];
3712 break;
3713 case 0x20:
3714 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0)
3715 result = (lun[2] << 24) +
3716 (lun[3] << 16) + (lun[4] << 8) +
3717 lun[5];
3718 break;
3719 }
3720 break;
3721 case RPL_LUNDATA_EXT_EAM_NOT_SPEC:
3722 result = 0xffffffff;
3723 break;
3724 }
3725 break;
3726 }
3727 return (result);
3728 }
3729
3730 uint64_t
ctl_encode_lun(uint32_t decoded)3731 ctl_encode_lun(uint32_t decoded)
3732 {
3733 uint64_t l = decoded;
3734
3735 if (l <= 0xff)
3736 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48));
3737 if (l <= 0x3fff)
3738 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48));
3739 if (l <= 0xffffff)
3740 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) |
3741 (l << 32));
3742 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16));
3743 }
3744
3745 int
ctl_ffz(uint32_t * mask,uint32_t first,uint32_t last)3746 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3747 {
3748 int i;
3749
3750 for (i = first; i < last; i++) {
3751 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3752 return (i);
3753 }
3754 return (-1);
3755 }
3756
3757 int
ctl_set_mask(uint32_t * mask,uint32_t bit)3758 ctl_set_mask(uint32_t *mask, uint32_t bit)
3759 {
3760 uint32_t chunk, piece;
3761
3762 chunk = bit >> 5;
3763 piece = bit % (sizeof(uint32_t) * 8);
3764
3765 if ((mask[chunk] & (1 << piece)) != 0)
3766 return (-1);
3767 else
3768 mask[chunk] |= (1 << piece);
3769
3770 return (0);
3771 }
3772
3773 int
ctl_clear_mask(uint32_t * mask,uint32_t bit)3774 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3775 {
3776 uint32_t chunk, piece;
3777
3778 chunk = bit >> 5;
3779 piece = bit % (sizeof(uint32_t) * 8);
3780
3781 if ((mask[chunk] & (1 << piece)) == 0)
3782 return (-1);
3783 else
3784 mask[chunk] &= ~(1 << piece);
3785
3786 return (0);
3787 }
3788
3789 int
ctl_is_set(uint32_t * mask,uint32_t bit)3790 ctl_is_set(uint32_t *mask, uint32_t bit)
3791 {
3792 uint32_t chunk, piece;
3793
3794 chunk = bit >> 5;
3795 piece = bit % (sizeof(uint32_t) * 8);
3796
3797 if ((mask[chunk] & (1 << piece)) == 0)
3798 return (0);
3799 else
3800 return (1);
3801 }
3802
3803 static uint64_t
ctl_get_prkey(struct ctl_lun * lun,uint32_t residx)3804 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3805 {
3806 uint64_t *t;
3807
3808 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3809 if (t == NULL)
3810 return (0);
3811 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3812 }
3813
3814 static void
ctl_clr_prkey(struct ctl_lun * lun,uint32_t residx)3815 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3816 {
3817 uint64_t *t;
3818
3819 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3820 if (t == NULL)
3821 return;
3822 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3823 }
3824
3825 static void
ctl_alloc_prkey(struct ctl_lun * lun,uint32_t residx)3826 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3827 {
3828 uint64_t *p;
3829 u_int i;
3830
3831 i = residx/CTL_MAX_INIT_PER_PORT;
3832 if (lun->pr_keys[i] != NULL)
3833 return;
3834 mtx_unlock(&lun->lun_lock);
3835 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3836 M_WAITOK | M_ZERO);
3837 mtx_lock(&lun->lun_lock);
3838 if (lun->pr_keys[i] == NULL)
3839 lun->pr_keys[i] = p;
3840 else
3841 free(p, M_CTL);
3842 }
3843
3844 static void
ctl_set_prkey(struct ctl_lun * lun,uint32_t residx,uint64_t key)3845 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3846 {
3847 uint64_t *t;
3848
3849 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3850 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3851 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3852 }
3853
3854 /*
3855 * ctl_softc, pool_name, total_ctl_io are passed in.
3856 * npool is passed out.
3857 */
3858 int
ctl_pool_create(struct ctl_softc * ctl_softc,const char * pool_name,uint32_t total_ctl_io,void ** npool)3859 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3860 uint32_t total_ctl_io, void **npool)
3861 {
3862 struct ctl_io_pool *pool;
3863
3864 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3865 M_NOWAIT | M_ZERO);
3866 if (pool == NULL)
3867 return (ENOMEM);
3868
3869 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3870 pool->ctl_softc = ctl_softc;
3871 #ifdef IO_POOLS
3872 pool->zone = uma_zsecond_create(pool->name, NULL,
3873 NULL, NULL, NULL, ctl_softc->io_zone);
3874 /* uma_prealloc(pool->zone, total_ctl_io); */
3875 #else
3876 pool->zone = ctl_softc->io_zone;
3877 #endif
3878
3879 *npool = pool;
3880 return (0);
3881 }
3882
3883 void
ctl_pool_free(struct ctl_io_pool * pool)3884 ctl_pool_free(struct ctl_io_pool *pool)
3885 {
3886
3887 if (pool == NULL)
3888 return;
3889
3890 #ifdef IO_POOLS
3891 uma_zdestroy(pool->zone);
3892 #endif
3893 free(pool, M_CTL);
3894 }
3895
3896 union ctl_io *
ctl_alloc_io(void * pool_ref)3897 ctl_alloc_io(void *pool_ref)
3898 {
3899 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3900 union ctl_io *io;
3901
3902 io = uma_zalloc(pool->zone, M_WAITOK);
3903 if (io != NULL) {
3904 io->io_hdr.pool = pool_ref;
3905 CTL_SOFTC(io) = pool->ctl_softc;
3906 TAILQ_INIT(&io->io_hdr.blocked_queue);
3907 }
3908 return (io);
3909 }
3910
3911 union ctl_io *
ctl_alloc_io_nowait(void * pool_ref)3912 ctl_alloc_io_nowait(void *pool_ref)
3913 {
3914 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3915 union ctl_io *io;
3916
3917 io = uma_zalloc(pool->zone, M_NOWAIT);
3918 if (io != NULL) {
3919 io->io_hdr.pool = pool_ref;
3920 CTL_SOFTC(io) = pool->ctl_softc;
3921 TAILQ_INIT(&io->io_hdr.blocked_queue);
3922 }
3923 return (io);
3924 }
3925
3926 void
ctl_free_io(union ctl_io * io)3927 ctl_free_io(union ctl_io *io)
3928 {
3929 struct ctl_io_pool *pool;
3930
3931 if (io == NULL)
3932 return;
3933
3934 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3935 uma_zfree(pool->zone, io);
3936 }
3937
3938 void
ctl_zero_io(union ctl_io * io)3939 ctl_zero_io(union ctl_io *io)
3940 {
3941 struct ctl_io_pool *pool;
3942
3943 if (io == NULL)
3944 return;
3945
3946 /*
3947 * May need to preserve linked list pointers at some point too.
3948 */
3949 pool = io->io_hdr.pool;
3950 memset(io, 0, sizeof(*io));
3951 io->io_hdr.pool = pool;
3952 CTL_SOFTC(io) = pool->ctl_softc;
3953 TAILQ_INIT(&io->io_hdr.blocked_queue);
3954 }
3955
3956 int
ctl_expand_number(const char * buf,uint64_t * num)3957 ctl_expand_number(const char *buf, uint64_t *num)
3958 {
3959 char *endptr;
3960 uint64_t number;
3961 unsigned shift;
3962
3963 number = strtoq(buf, &endptr, 0);
3964
3965 switch (tolower((unsigned char)*endptr)) {
3966 case 'e':
3967 shift = 60;
3968 break;
3969 case 'p':
3970 shift = 50;
3971 break;
3972 case 't':
3973 shift = 40;
3974 break;
3975 case 'g':
3976 shift = 30;
3977 break;
3978 case 'm':
3979 shift = 20;
3980 break;
3981 case 'k':
3982 shift = 10;
3983 break;
3984 case 'b':
3985 case '\0': /* No unit. */
3986 *num = number;
3987 return (0);
3988 default:
3989 /* Unrecognized unit. */
3990 return (-1);
3991 }
3992
3993 if ((number << shift) >> shift != number) {
3994 /* Overflow */
3995 return (-1);
3996 }
3997 *num = number << shift;
3998 return (0);
3999 }
4000
4001
4002 /*
4003 * This routine could be used in the future to load default and/or saved
4004 * mode page parameters for a particuar lun.
4005 */
4006 static int
ctl_init_page_index(struct ctl_lun * lun)4007 ctl_init_page_index(struct ctl_lun *lun)
4008 {
4009 int i, page_code;
4010 struct ctl_page_index *page_index;
4011 const char *value;
4012 uint64_t ival;
4013
4014 memcpy(&lun->mode_pages.index, page_index_template,
4015 sizeof(page_index_template));
4016
4017 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4018
4019 page_index = &lun->mode_pages.index[i];
4020 if (lun->be_lun->lun_type == T_DIRECT &&
4021 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4022 continue;
4023 if (lun->be_lun->lun_type == T_PROCESSOR &&
4024 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4025 continue;
4026 if (lun->be_lun->lun_type == T_CDROM &&
4027 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4028 continue;
4029
4030 page_code = page_index->page_code & SMPH_PC_MASK;
4031 switch (page_code) {
4032 case SMS_RW_ERROR_RECOVERY_PAGE: {
4033 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4034 ("subpage %#x for page %#x is incorrect!",
4035 page_index->subpage, page_code));
4036 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
4037 &rw_er_page_default,
4038 sizeof(rw_er_page_default));
4039 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
4040 &rw_er_page_changeable,
4041 sizeof(rw_er_page_changeable));
4042 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
4043 &rw_er_page_default,
4044 sizeof(rw_er_page_default));
4045 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
4046 &rw_er_page_default,
4047 sizeof(rw_er_page_default));
4048 page_index->page_data =
4049 (uint8_t *)lun->mode_pages.rw_er_page;
4050 break;
4051 }
4052 case SMS_FORMAT_DEVICE_PAGE: {
4053 struct scsi_format_page *format_page;
4054
4055 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4056 ("subpage %#x for page %#x is incorrect!",
4057 page_index->subpage, page_code));
4058
4059 /*
4060 * Sectors per track are set above. Bytes per
4061 * sector need to be set here on a per-LUN basis.
4062 */
4063 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
4064 &format_page_default,
4065 sizeof(format_page_default));
4066 memcpy(&lun->mode_pages.format_page[
4067 CTL_PAGE_CHANGEABLE], &format_page_changeable,
4068 sizeof(format_page_changeable));
4069 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
4070 &format_page_default,
4071 sizeof(format_page_default));
4072 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
4073 &format_page_default,
4074 sizeof(format_page_default));
4075
4076 format_page = &lun->mode_pages.format_page[
4077 CTL_PAGE_CURRENT];
4078 scsi_ulto2b(lun->be_lun->blocksize,
4079 format_page->bytes_per_sector);
4080
4081 format_page = &lun->mode_pages.format_page[
4082 CTL_PAGE_DEFAULT];
4083 scsi_ulto2b(lun->be_lun->blocksize,
4084 format_page->bytes_per_sector);
4085
4086 format_page = &lun->mode_pages.format_page[
4087 CTL_PAGE_SAVED];
4088 scsi_ulto2b(lun->be_lun->blocksize,
4089 format_page->bytes_per_sector);
4090
4091 page_index->page_data =
4092 (uint8_t *)lun->mode_pages.format_page;
4093 break;
4094 }
4095 case SMS_RIGID_DISK_PAGE: {
4096 struct scsi_rigid_disk_page *rigid_disk_page;
4097 uint32_t sectors_per_cylinder;
4098 uint64_t cylinders;
4099 #ifndef __XSCALE__
4100 int shift;
4101 #endif /* !__XSCALE__ */
4102
4103 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4104 ("subpage %#x for page %#x is incorrect!",
4105 page_index->subpage, page_code));
4106
4107 /*
4108 * Rotation rate and sectors per track are set
4109 * above. We calculate the cylinders here based on
4110 * capacity. Due to the number of heads and
4111 * sectors per track we're using, smaller arrays
4112 * may turn out to have 0 cylinders. Linux and
4113 * FreeBSD don't pay attention to these mode pages
4114 * to figure out capacity, but Solaris does. It
4115 * seems to deal with 0 cylinders just fine, and
4116 * works out a fake geometry based on the capacity.
4117 */
4118 memcpy(&lun->mode_pages.rigid_disk_page[
4119 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4120 sizeof(rigid_disk_page_default));
4121 memcpy(&lun->mode_pages.rigid_disk_page[
4122 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4123 sizeof(rigid_disk_page_changeable));
4124
4125 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4126 CTL_DEFAULT_HEADS;
4127
4128 /*
4129 * The divide method here will be more accurate,
4130 * probably, but results in floating point being
4131 * used in the kernel on i386 (__udivdi3()). On the
4132 * XScale, though, __udivdi3() is implemented in
4133 * software.
4134 *
4135 * The shift method for cylinder calculation is
4136 * accurate if sectors_per_cylinder is a power of
4137 * 2. Otherwise it might be slightly off -- you
4138 * might have a bit of a truncation problem.
4139 */
4140 #ifdef __XSCALE__
4141 cylinders = (lun->be_lun->maxlba + 1) /
4142 sectors_per_cylinder;
4143 #else
4144 for (shift = 31; shift > 0; shift--) {
4145 if (sectors_per_cylinder & (1 << shift))
4146 break;
4147 }
4148 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4149 #endif
4150
4151 /*
4152 * We've basically got 3 bytes, or 24 bits for the
4153 * cylinder size in the mode page. If we're over,
4154 * just round down to 2^24.
4155 */
4156 if (cylinders > 0xffffff)
4157 cylinders = 0xffffff;
4158
4159 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4160 CTL_PAGE_DEFAULT];
4161 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4162
4163 if ((value = ctl_get_opt(&lun->be_lun->options,
4164 "rpm")) != NULL) {
4165 scsi_ulto2b(strtol(value, NULL, 0),
4166 rigid_disk_page->rotation_rate);
4167 }
4168
4169 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4170 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4171 sizeof(rigid_disk_page_default));
4172 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4173 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4174 sizeof(rigid_disk_page_default));
4175
4176 page_index->page_data =
4177 (uint8_t *)lun->mode_pages.rigid_disk_page;
4178 break;
4179 }
4180 case SMS_VERIFY_ERROR_RECOVERY_PAGE: {
4181 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4182 ("subpage %#x for page %#x is incorrect!",
4183 page_index->subpage, page_code));
4184 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT],
4185 &verify_er_page_default,
4186 sizeof(verify_er_page_default));
4187 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE],
4188 &verify_er_page_changeable,
4189 sizeof(verify_er_page_changeable));
4190 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT],
4191 &verify_er_page_default,
4192 sizeof(verify_er_page_default));
4193 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED],
4194 &verify_er_page_default,
4195 sizeof(verify_er_page_default));
4196 page_index->page_data =
4197 (uint8_t *)lun->mode_pages.verify_er_page;
4198 break;
4199 }
4200 case SMS_CACHING_PAGE: {
4201 struct scsi_caching_page *caching_page;
4202
4203 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4204 ("subpage %#x for page %#x is incorrect!",
4205 page_index->subpage, page_code));
4206 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4207 &caching_page_default,
4208 sizeof(caching_page_default));
4209 memcpy(&lun->mode_pages.caching_page[
4210 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4211 sizeof(caching_page_changeable));
4212 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4213 &caching_page_default,
4214 sizeof(caching_page_default));
4215 caching_page = &lun->mode_pages.caching_page[
4216 CTL_PAGE_SAVED];
4217 value = ctl_get_opt(&lun->be_lun->options, "writecache");
4218 if (value != NULL && strcmp(value, "off") == 0)
4219 caching_page->flags1 &= ~SCP_WCE;
4220 value = ctl_get_opt(&lun->be_lun->options, "readcache");
4221 if (value != NULL && strcmp(value, "off") == 0)
4222 caching_page->flags1 |= SCP_RCD;
4223 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4224 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4225 sizeof(caching_page_default));
4226 page_index->page_data =
4227 (uint8_t *)lun->mode_pages.caching_page;
4228 break;
4229 }
4230 case SMS_CONTROL_MODE_PAGE: {
4231 switch (page_index->subpage) {
4232 case SMS_SUBPAGE_PAGE_0: {
4233 struct scsi_control_page *control_page;
4234
4235 memcpy(&lun->mode_pages.control_page[
4236 CTL_PAGE_DEFAULT],
4237 &control_page_default,
4238 sizeof(control_page_default));
4239 memcpy(&lun->mode_pages.control_page[
4240 CTL_PAGE_CHANGEABLE],
4241 &control_page_changeable,
4242 sizeof(control_page_changeable));
4243 memcpy(&lun->mode_pages.control_page[
4244 CTL_PAGE_SAVED],
4245 &control_page_default,
4246 sizeof(control_page_default));
4247 control_page = &lun->mode_pages.control_page[
4248 CTL_PAGE_SAVED];
4249 value = ctl_get_opt(&lun->be_lun->options,
4250 "reordering");
4251 if (value != NULL &&
4252 strcmp(value, "unrestricted") == 0) {
4253 control_page->queue_flags &=
4254 ~SCP_QUEUE_ALG_MASK;
4255 control_page->queue_flags |=
4256 SCP_QUEUE_ALG_UNRESTRICTED;
4257 }
4258 memcpy(&lun->mode_pages.control_page[
4259 CTL_PAGE_CURRENT],
4260 &lun->mode_pages.control_page[
4261 CTL_PAGE_SAVED],
4262 sizeof(control_page_default));
4263 page_index->page_data =
4264 (uint8_t *)lun->mode_pages.control_page;
4265 break;
4266 }
4267 case 0x01:
4268 memcpy(&lun->mode_pages.control_ext_page[
4269 CTL_PAGE_DEFAULT],
4270 &control_ext_page_default,
4271 sizeof(control_ext_page_default));
4272 memcpy(&lun->mode_pages.control_ext_page[
4273 CTL_PAGE_CHANGEABLE],
4274 &control_ext_page_changeable,
4275 sizeof(control_ext_page_changeable));
4276 memcpy(&lun->mode_pages.control_ext_page[
4277 CTL_PAGE_SAVED],
4278 &control_ext_page_default,
4279 sizeof(control_ext_page_default));
4280 memcpy(&lun->mode_pages.control_ext_page[
4281 CTL_PAGE_CURRENT],
4282 &lun->mode_pages.control_ext_page[
4283 CTL_PAGE_SAVED],
4284 sizeof(control_ext_page_default));
4285 page_index->page_data =
4286 (uint8_t *)lun->mode_pages.control_ext_page;
4287 break;
4288 default:
4289 panic("subpage %#x for page %#x is incorrect!",
4290 page_index->subpage, page_code);
4291 }
4292 break;
4293 }
4294 case SMS_INFO_EXCEPTIONS_PAGE: {
4295 switch (page_index->subpage) {
4296 case SMS_SUBPAGE_PAGE_0:
4297 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4298 &ie_page_default,
4299 sizeof(ie_page_default));
4300 memcpy(&lun->mode_pages.ie_page[
4301 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4302 sizeof(ie_page_changeable));
4303 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4304 &ie_page_default,
4305 sizeof(ie_page_default));
4306 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4307 &ie_page_default,
4308 sizeof(ie_page_default));
4309 page_index->page_data =
4310 (uint8_t *)lun->mode_pages.ie_page;
4311 break;
4312 case 0x02: {
4313 struct ctl_logical_block_provisioning_page *page;
4314
4315 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4316 &lbp_page_default,
4317 sizeof(lbp_page_default));
4318 memcpy(&lun->mode_pages.lbp_page[
4319 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4320 sizeof(lbp_page_changeable));
4321 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4322 &lbp_page_default,
4323 sizeof(lbp_page_default));
4324 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4325 value = ctl_get_opt(&lun->be_lun->options,
4326 "avail-threshold");
4327 if (value != NULL &&
4328 ctl_expand_number(value, &ival) == 0) {
4329 page->descr[0].flags |= SLBPPD_ENABLED |
4330 SLBPPD_ARMING_DEC;
4331 if (lun->be_lun->blocksize)
4332 ival /= lun->be_lun->blocksize;
4333 else
4334 ival /= 512;
4335 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4336 page->descr[0].count);
4337 }
4338 value = ctl_get_opt(&lun->be_lun->options,
4339 "used-threshold");
4340 if (value != NULL &&
4341 ctl_expand_number(value, &ival) == 0) {
4342 page->descr[1].flags |= SLBPPD_ENABLED |
4343 SLBPPD_ARMING_INC;
4344 if (lun->be_lun->blocksize)
4345 ival /= lun->be_lun->blocksize;
4346 else
4347 ival /= 512;
4348 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4349 page->descr[1].count);
4350 }
4351 value = ctl_get_opt(&lun->be_lun->options,
4352 "pool-avail-threshold");
4353 if (value != NULL &&
4354 ctl_expand_number(value, &ival) == 0) {
4355 page->descr[2].flags |= SLBPPD_ENABLED |
4356 SLBPPD_ARMING_DEC;
4357 if (lun->be_lun->blocksize)
4358 ival /= lun->be_lun->blocksize;
4359 else
4360 ival /= 512;
4361 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4362 page->descr[2].count);
4363 }
4364 value = ctl_get_opt(&lun->be_lun->options,
4365 "pool-used-threshold");
4366 if (value != NULL &&
4367 ctl_expand_number(value, &ival) == 0) {
4368 page->descr[3].flags |= SLBPPD_ENABLED |
4369 SLBPPD_ARMING_INC;
4370 if (lun->be_lun->blocksize)
4371 ival /= lun->be_lun->blocksize;
4372 else
4373 ival /= 512;
4374 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4375 page->descr[3].count);
4376 }
4377 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4378 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4379 sizeof(lbp_page_default));
4380 page_index->page_data =
4381 (uint8_t *)lun->mode_pages.lbp_page;
4382 break;
4383 }
4384 default:
4385 panic("subpage %#x for page %#x is incorrect!",
4386 page_index->subpage, page_code);
4387 }
4388 break;
4389 }
4390 case SMS_CDDVD_CAPS_PAGE:{
4391 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4392 ("subpage %#x for page %#x is incorrect!",
4393 page_index->subpage, page_code));
4394 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT],
4395 &cddvd_page_default,
4396 sizeof(cddvd_page_default));
4397 memcpy(&lun->mode_pages.cddvd_page[
4398 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable,
4399 sizeof(cddvd_page_changeable));
4400 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4401 &cddvd_page_default,
4402 sizeof(cddvd_page_default));
4403 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT],
4404 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4405 sizeof(cddvd_page_default));
4406 page_index->page_data =
4407 (uint8_t *)lun->mode_pages.cddvd_page;
4408 break;
4409 }
4410 default:
4411 panic("invalid page code value %#x", page_code);
4412 }
4413 }
4414
4415 return (CTL_RETVAL_COMPLETE);
4416 }
4417
4418 static int
ctl_init_log_page_index(struct ctl_lun * lun)4419 ctl_init_log_page_index(struct ctl_lun *lun)
4420 {
4421 struct ctl_page_index *page_index;
4422 int i, j, k, prev;
4423
4424 memcpy(&lun->log_pages.index, log_page_index_template,
4425 sizeof(log_page_index_template));
4426
4427 prev = -1;
4428 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4429
4430 page_index = &lun->log_pages.index[i];
4431 if (lun->be_lun->lun_type == T_DIRECT &&
4432 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4433 continue;
4434 if (lun->be_lun->lun_type == T_PROCESSOR &&
4435 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4436 continue;
4437 if (lun->be_lun->lun_type == T_CDROM &&
4438 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4439 continue;
4440
4441 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4442 lun->backend->lun_attr == NULL)
4443 continue;
4444
4445 if (page_index->page_code != prev) {
4446 lun->log_pages.pages_page[j] = page_index->page_code;
4447 prev = page_index->page_code;
4448 j++;
4449 }
4450 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4451 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4452 k++;
4453 }
4454 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4455 lun->log_pages.index[0].page_len = j;
4456 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4457 lun->log_pages.index[1].page_len = k * 2;
4458 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4459 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4460 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page;
4461 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page);
4462 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.ie_page;
4463 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.ie_page);
4464
4465 return (CTL_RETVAL_COMPLETE);
4466 }
4467
4468 static int
hex2bin(const char * str,uint8_t * buf,int buf_size)4469 hex2bin(const char *str, uint8_t *buf, int buf_size)
4470 {
4471 int i;
4472 u_char c;
4473
4474 memset(buf, 0, buf_size);
4475 while (isspace(str[0]))
4476 str++;
4477 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4478 str += 2;
4479 buf_size *= 2;
4480 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4481 while (str[i] == '-') /* Skip dashes in UUIDs. */
4482 str++;
4483 c = str[i];
4484 if (isdigit(c))
4485 c -= '0';
4486 else if (isalpha(c))
4487 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4488 else
4489 break;
4490 if (c >= 16)
4491 break;
4492 if ((i & 1) == 0)
4493 buf[i / 2] |= (c << 4);
4494 else
4495 buf[i / 2] |= c;
4496 }
4497 return ((i + 1) / 2);
4498 }
4499
4500 /*
4501 * Add LUN.
4502 *
4503 * Returns 0 for success, non-zero (errno) for failure.
4504 */
4505 int
ctl_add_lun(struct ctl_be_lun * be_lun)4506 ctl_add_lun(struct ctl_be_lun *be_lun)
4507 {
4508 struct ctl_softc *ctl_softc = control_softc;
4509 struct ctl_lun *nlun, *lun;
4510 struct scsi_vpd_id_descriptor *desc;
4511 struct scsi_vpd_id_t10 *t10id;
4512 const char *eui, *naa, *scsiname, *uuid, *vendor, *value;
4513 int lun_number;
4514 int devidlen, idlen1, idlen2 = 0, len;
4515
4516 /*
4517 * We support only Direct Access, CD-ROM or Processor LUN types.
4518 */
4519 switch (be_lun->lun_type) {
4520 case T_DIRECT:
4521 case T_PROCESSOR:
4522 case T_CDROM:
4523 break;
4524 case T_SEQUENTIAL:
4525 case T_CHANGER:
4526 default:
4527 return (EINVAL);
4528 }
4529 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK | M_ZERO);
4530
4531 lun->pending_sense = malloc(sizeof(struct scsi_sense_data *) *
4532 ctl_max_ports, M_DEVBUF, M_WAITOK | M_ZERO);
4533 lun->pending_ua = malloc(sizeof(ctl_ua_type *) * ctl_max_ports,
4534 M_DEVBUF, M_WAITOK | M_ZERO);
4535 lun->pr_keys = malloc(sizeof(uint64_t *) * ctl_max_ports,
4536 M_DEVBUF, M_WAITOK | M_ZERO);
4537
4538 /* Generate LUN ID. */
4539 devidlen = max(CTL_DEVID_MIN_LEN,
4540 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4541 idlen1 = sizeof(*t10id) + devidlen;
4542 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4543 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4544 if (scsiname != NULL) {
4545 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4546 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4547 }
4548 eui = ctl_get_opt(&be_lun->options, "eui");
4549 if (eui != NULL) {
4550 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4551 }
4552 naa = ctl_get_opt(&be_lun->options, "naa");
4553 if (naa != NULL) {
4554 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4555 }
4556 uuid = ctl_get_opt(&be_lun->options, "uuid");
4557 if (uuid != NULL) {
4558 len += sizeof(struct scsi_vpd_id_descriptor) + 18;
4559 }
4560 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4561 M_CTL, M_WAITOK | M_ZERO);
4562 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4563 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4564 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4565 desc->length = idlen1;
4566 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4567 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4568 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4569 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4570 } else {
4571 strncpy(t10id->vendor, vendor,
4572 min(sizeof(t10id->vendor), strlen(vendor)));
4573 }
4574 strncpy((char *)t10id->vendor_spec_id,
4575 (char *)be_lun->device_id, devidlen);
4576 if (scsiname != NULL) {
4577 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4578 desc->length);
4579 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4580 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4581 SVPD_ID_TYPE_SCSI_NAME;
4582 desc->length = idlen2;
4583 strlcpy(desc->identifier, scsiname, idlen2);
4584 }
4585 if (eui != NULL) {
4586 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4587 desc->length);
4588 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4589 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4590 SVPD_ID_TYPE_EUI64;
4591 desc->length = hex2bin(eui, desc->identifier, 16);
4592 desc->length = desc->length > 12 ? 16 :
4593 (desc->length > 8 ? 12 : 8);
4594 len -= 16 - desc->length;
4595 }
4596 if (naa != NULL) {
4597 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4598 desc->length);
4599 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4600 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4601 SVPD_ID_TYPE_NAA;
4602 desc->length = hex2bin(naa, desc->identifier, 16);
4603 desc->length = desc->length > 8 ? 16 : 8;
4604 len -= 16 - desc->length;
4605 }
4606 if (uuid != NULL) {
4607 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4608 desc->length);
4609 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4610 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4611 SVPD_ID_TYPE_UUID;
4612 desc->identifier[0] = 0x10;
4613 hex2bin(uuid, &desc->identifier[2], 16);
4614 desc->length = 18;
4615 }
4616 lun->lun_devid->len = len;
4617
4618 mtx_lock(&ctl_softc->ctl_lock);
4619 /*
4620 * See if the caller requested a particular LUN number. If so, see
4621 * if it is available. Otherwise, allocate the first available LUN.
4622 */
4623 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4624 if ((be_lun->req_lun_id > (ctl_max_luns - 1))
4625 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4626 mtx_unlock(&ctl_softc->ctl_lock);
4627 if (be_lun->req_lun_id > (ctl_max_luns - 1)) {
4628 printf("ctl: requested LUN ID %d is higher "
4629 "than ctl_max_luns - 1 (%d)\n",
4630 be_lun->req_lun_id, ctl_max_luns - 1);
4631 } else {
4632 /*
4633 * XXX KDM return an error, or just assign
4634 * another LUN ID in this case??
4635 */
4636 printf("ctl: requested LUN ID %d is already "
4637 "in use\n", be_lun->req_lun_id);
4638 }
4639 fail:
4640 free(lun->lun_devid, M_CTL);
4641 free(lun, M_CTL);
4642 return (ENOSPC);
4643 }
4644 lun_number = be_lun->req_lun_id;
4645 } else {
4646 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, ctl_max_luns);
4647 if (lun_number == -1) {
4648 mtx_unlock(&ctl_softc->ctl_lock);
4649 printf("ctl: can't allocate LUN, out of LUNs\n");
4650 goto fail;
4651 }
4652 }
4653 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4654 mtx_unlock(&ctl_softc->ctl_lock);
4655
4656 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4657 lun->lun = lun_number;
4658 lun->be_lun = be_lun;
4659 /*
4660 * The processor LUN is always enabled. Disk LUNs come on line
4661 * disabled, and must be enabled by the backend.
4662 */
4663 lun->flags |= CTL_LUN_DISABLED;
4664 lun->backend = be_lun->be;
4665 be_lun->ctl_lun = lun;
4666 be_lun->lun_id = lun_number;
4667 if (be_lun->flags & CTL_LUN_FLAG_EJECTED)
4668 lun->flags |= CTL_LUN_EJECTED;
4669 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA)
4670 lun->flags |= CTL_LUN_NO_MEDIA;
4671 if (be_lun->flags & CTL_LUN_FLAG_STOPPED)
4672 lun->flags |= CTL_LUN_STOPPED;
4673
4674 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4675 lun->flags |= CTL_LUN_PRIMARY_SC;
4676
4677 value = ctl_get_opt(&be_lun->options, "removable");
4678 if (value != NULL) {
4679 if (strcmp(value, "on") == 0)
4680 lun->flags |= CTL_LUN_REMOVABLE;
4681 } else if (be_lun->lun_type == T_CDROM)
4682 lun->flags |= CTL_LUN_REMOVABLE;
4683
4684 lun->ctl_softc = ctl_softc;
4685 #ifdef CTL_TIME_IO
4686 lun->last_busy = getsbinuptime();
4687 #endif
4688 TAILQ_INIT(&lun->ooa_queue);
4689 STAILQ_INIT(&lun->error_list);
4690 lun->ie_reported = 1;
4691 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0);
4692 ctl_tpc_lun_init(lun);
4693 if (lun->flags & CTL_LUN_REMOVABLE) {
4694 lun->prevent = malloc((CTL_MAX_INITIATORS + 31) / 32 * 4,
4695 M_CTL, M_WAITOK);
4696 }
4697
4698 /*
4699 * Initialize the mode and log page index.
4700 */
4701 ctl_init_page_index(lun);
4702 ctl_init_log_page_index(lun);
4703
4704 /* Setup statistics gathering */
4705 #ifdef CTL_LEGACY_STATS
4706 lun->legacy_stats.device_type = be_lun->lun_type;
4707 lun->legacy_stats.lun_number = lun_number;
4708 lun->legacy_stats.blocksize = be_lun->blocksize;
4709 if (be_lun->blocksize == 0)
4710 lun->legacy_stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4711 lun->legacy_stats.ports = malloc(sizeof(struct ctl_lun_io_port_stats) *
4712 ctl_max_ports, M_DEVBUF, M_WAITOK | M_ZERO);
4713 for (len = 0; len < ctl_max_ports; len++)
4714 lun->legacy_stats.ports[len].targ_port = len;
4715 #endif /* CTL_LEGACY_STATS */
4716 lun->stats.item = lun_number;
4717
4718 /*
4719 * Now, before we insert this lun on the lun list, set the lun
4720 * inventory changed UA for all other luns.
4721 */
4722 mtx_lock(&ctl_softc->ctl_lock);
4723 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4724 mtx_lock(&nlun->lun_lock);
4725 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4726 mtx_unlock(&nlun->lun_lock);
4727 }
4728 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4729 ctl_softc->ctl_luns[lun_number] = lun;
4730 ctl_softc->num_luns++;
4731 mtx_unlock(&ctl_softc->ctl_lock);
4732
4733 /*
4734 * We successfully added the LUN, attempt to enable it.
4735 */
4736 if (ctl_enable_lun(lun) != 0) {
4737 printf("%s: ctl_enable_lun() failed!\n", __func__);
4738 mtx_lock(&ctl_softc->ctl_lock);
4739 STAILQ_REMOVE(&ctl_softc->lun_list, lun, ctl_lun, links);
4740 ctl_clear_mask(ctl_softc->ctl_lun_mask, lun_number);
4741 ctl_softc->ctl_luns[lun_number] = NULL;
4742 ctl_softc->num_luns--;
4743 mtx_unlock(&ctl_softc->ctl_lock);
4744 free(lun->lun_devid, M_CTL);
4745 free(lun, M_CTL);
4746 return (EIO);
4747 }
4748
4749 return (0);
4750 }
4751
4752 /*
4753 * Free LUN that has no active requests.
4754 */
4755 static int
ctl_free_lun(struct ctl_lun * lun)4756 ctl_free_lun(struct ctl_lun *lun)
4757 {
4758 struct ctl_softc *softc = lun->ctl_softc;
4759 struct ctl_lun *nlun;
4760 int i;
4761
4762 KASSERT(TAILQ_EMPTY(&lun->ooa_queue),
4763 ("Freeing a LUN %p with outstanding I/O!\n", lun));
4764
4765 mtx_lock(&softc->ctl_lock);
4766 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4767 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4768 softc->ctl_luns[lun->lun] = NULL;
4769 softc->num_luns--;
4770 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4771 mtx_lock(&nlun->lun_lock);
4772 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4773 mtx_unlock(&nlun->lun_lock);
4774 }
4775 mtx_unlock(&softc->ctl_lock);
4776
4777 /*
4778 * Tell the backend to free resources, if this LUN has a backend.
4779 */
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 free(lun, M_CTL);
4796
4797 return (0);
4798 }
4799
4800 static int
ctl_enable_lun(struct ctl_lun * lun)4801 ctl_enable_lun(struct ctl_lun *lun)
4802 {
4803 struct ctl_softc *softc;
4804 struct ctl_port *port, *nport;
4805 int retval;
4806
4807 softc = lun->ctl_softc;
4808
4809 mtx_lock(&softc->ctl_lock);
4810 mtx_lock(&lun->lun_lock);
4811 KASSERT((lun->flags & CTL_LUN_DISABLED) != 0,
4812 ("%s: LUN not disabled", __func__));
4813 lun->flags &= ~CTL_LUN_DISABLED;
4814 mtx_unlock(&lun->lun_lock);
4815
4816 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) {
4817 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4818 port->lun_map != NULL || port->lun_enable == NULL)
4819 continue;
4820
4821 /*
4822 * Drop the lock while we call the FETD's enable routine.
4823 * This can lead to a callback into CTL (at least in the
4824 * case of the internal initiator frontend.
4825 */
4826 mtx_unlock(&softc->ctl_lock);
4827 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4828 mtx_lock(&softc->ctl_lock);
4829 if (retval != 0) {
4830 printf("%s: FETD %s port %d returned error "
4831 "%d for lun_enable on lun %jd\n",
4832 __func__, port->port_name, port->targ_port,
4833 retval, (intmax_t)lun->lun);
4834 }
4835 }
4836
4837 mtx_unlock(&softc->ctl_lock);
4838 ctl_isc_announce_lun(lun);
4839
4840 return (0);
4841 }
4842
4843 static int
ctl_disable_lun(struct ctl_lun * lun)4844 ctl_disable_lun(struct ctl_lun *lun)
4845 {
4846 struct ctl_softc *softc;
4847 struct ctl_port *port;
4848 int retval;
4849
4850 softc = lun->ctl_softc;
4851
4852 mtx_lock(&softc->ctl_lock);
4853 mtx_lock(&lun->lun_lock);
4854 KASSERT((lun->flags & CTL_LUN_DISABLED) == 0,
4855 ("%s: LUN not enabled", __func__));
4856 lun->flags |= CTL_LUN_DISABLED;
4857 mtx_unlock(&lun->lun_lock);
4858
4859 STAILQ_FOREACH(port, &softc->port_list, links) {
4860 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4861 port->lun_map != NULL || port->lun_disable == NULL)
4862 continue;
4863
4864 /*
4865 * Drop the lock before we call the frontend's disable
4866 * routine, to avoid lock order reversals.
4867 *
4868 * XXX KDM what happens if the frontend list changes while
4869 * we're traversing it? It's unlikely, but should be handled.
4870 */
4871 mtx_unlock(&softc->ctl_lock);
4872 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4873 mtx_lock(&softc->ctl_lock);
4874 if (retval != 0) {
4875 printf("%s: FETD %s port %d returned error "
4876 "%d for lun_disable on lun %jd\n",
4877 __func__, port->port_name, port->targ_port,
4878 retval, (intmax_t)lun->lun);
4879 }
4880 }
4881
4882 mtx_unlock(&softc->ctl_lock);
4883 ctl_isc_announce_lun(lun);
4884
4885 return (0);
4886 }
4887
4888 int
ctl_start_lun(struct ctl_be_lun * be_lun)4889 ctl_start_lun(struct ctl_be_lun *be_lun)
4890 {
4891 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4892
4893 mtx_lock(&lun->lun_lock);
4894 lun->flags &= ~CTL_LUN_STOPPED;
4895 mtx_unlock(&lun->lun_lock);
4896 return (0);
4897 }
4898
4899 int
ctl_stop_lun(struct ctl_be_lun * be_lun)4900 ctl_stop_lun(struct ctl_be_lun *be_lun)
4901 {
4902 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4903
4904 mtx_lock(&lun->lun_lock);
4905 lun->flags |= CTL_LUN_STOPPED;
4906 mtx_unlock(&lun->lun_lock);
4907 return (0);
4908 }
4909
4910 int
ctl_lun_no_media(struct ctl_be_lun * be_lun)4911 ctl_lun_no_media(struct ctl_be_lun *be_lun)
4912 {
4913 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4914
4915 mtx_lock(&lun->lun_lock);
4916 lun->flags |= CTL_LUN_NO_MEDIA;
4917 mtx_unlock(&lun->lun_lock);
4918 return (0);
4919 }
4920
4921 int
ctl_lun_has_media(struct ctl_be_lun * be_lun)4922 ctl_lun_has_media(struct ctl_be_lun *be_lun)
4923 {
4924 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4925 union ctl_ha_msg msg;
4926
4927 mtx_lock(&lun->lun_lock);
4928 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED);
4929 if (lun->flags & CTL_LUN_REMOVABLE)
4930 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE);
4931 mtx_unlock(&lun->lun_lock);
4932 if ((lun->flags & CTL_LUN_REMOVABLE) &&
4933 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4934 bzero(&msg.ua, sizeof(msg.ua));
4935 msg.hdr.msg_type = CTL_MSG_UA;
4936 msg.hdr.nexus.initid = -1;
4937 msg.hdr.nexus.targ_port = -1;
4938 msg.hdr.nexus.targ_lun = lun->lun;
4939 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4940 msg.ua.ua_all = 1;
4941 msg.ua.ua_set = 1;
4942 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE;
4943 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4944 M_WAITOK);
4945 }
4946 return (0);
4947 }
4948
4949 int
ctl_lun_ejected(struct ctl_be_lun * be_lun)4950 ctl_lun_ejected(struct ctl_be_lun *be_lun)
4951 {
4952 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4953
4954 mtx_lock(&lun->lun_lock);
4955 lun->flags |= CTL_LUN_EJECTED;
4956 mtx_unlock(&lun->lun_lock);
4957 return (0);
4958 }
4959
4960 int
ctl_lun_primary(struct ctl_be_lun * be_lun)4961 ctl_lun_primary(struct ctl_be_lun *be_lun)
4962 {
4963 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4964
4965 mtx_lock(&lun->lun_lock);
4966 lun->flags |= CTL_LUN_PRIMARY_SC;
4967 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4968 mtx_unlock(&lun->lun_lock);
4969 ctl_isc_announce_lun(lun);
4970 return (0);
4971 }
4972
4973 int
ctl_lun_secondary(struct ctl_be_lun * be_lun)4974 ctl_lun_secondary(struct ctl_be_lun *be_lun)
4975 {
4976 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4977
4978 mtx_lock(&lun->lun_lock);
4979 lun->flags &= ~CTL_LUN_PRIMARY_SC;
4980 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4981 mtx_unlock(&lun->lun_lock);
4982 ctl_isc_announce_lun(lun);
4983 return (0);
4984 }
4985
4986 /*
4987 * Remove LUN. If there are active requests, wait for completion.
4988 *
4989 * Returns 0 for success, non-zero (errno) for failure.
4990 * Completion is reported to backed via the lun_shutdown() method.
4991 */
4992 int
ctl_remove_lun(struct ctl_be_lun * be_lun)4993 ctl_remove_lun(struct ctl_be_lun *be_lun)
4994 {
4995 struct ctl_softc *softc;
4996 struct ctl_lun *lun;
4997
4998 lun = (struct ctl_lun *)be_lun->ctl_lun;
4999 softc = lun->ctl_softc;
5000
5001 ctl_disable_lun(lun);
5002
5003 mtx_lock(&lun->lun_lock);
5004 lun->flags |= CTL_LUN_INVALID;
5005
5006 /*
5007 * If there is nothing in the OOA queue, go ahead and free the LUN.
5008 * If we have something in the OOA queue, we'll free it when the
5009 * last I/O completes.
5010 */
5011 if (TAILQ_EMPTY(&lun->ooa_queue)) {
5012 mtx_unlock(&lun->lun_lock);
5013 ctl_free_lun(lun);
5014 } else
5015 mtx_unlock(&lun->lun_lock);
5016
5017 return (0);
5018 }
5019
5020 void
ctl_lun_capacity_changed(struct ctl_be_lun * be_lun)5021 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
5022 {
5023 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5024 union ctl_ha_msg msg;
5025
5026 mtx_lock(&lun->lun_lock);
5027 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE);
5028 mtx_unlock(&lun->lun_lock);
5029 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
5030 /* Send msg to other side. */
5031 bzero(&msg.ua, sizeof(msg.ua));
5032 msg.hdr.msg_type = CTL_MSG_UA;
5033 msg.hdr.nexus.initid = -1;
5034 msg.hdr.nexus.targ_port = -1;
5035 msg.hdr.nexus.targ_lun = lun->lun;
5036 msg.hdr.nexus.targ_mapped_lun = lun->lun;
5037 msg.ua.ua_all = 1;
5038 msg.ua.ua_set = 1;
5039 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE;
5040 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
5041 M_WAITOK);
5042 }
5043 }
5044
5045 /*
5046 * Backend "memory move is complete" callback for requests that never
5047 * make it down to say RAIDCore's configuration code.
5048 */
5049 int
ctl_config_move_done(union ctl_io * io)5050 ctl_config_move_done(union ctl_io *io)
5051 {
5052 int retval;
5053
5054 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5055 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
5056 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
5057
5058 if ((io->io_hdr.port_status != 0) &&
5059 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5060 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5061 ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1,
5062 /*retry_count*/ io->io_hdr.port_status);
5063 } else if (io->scsiio.kern_data_resid != 0 &&
5064 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT &&
5065 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5066 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5067 ctl_set_invalid_field_ciu(&io->scsiio);
5068 }
5069
5070 if (ctl_debug & CTL_DEBUG_CDB_DATA)
5071 ctl_data_print(io);
5072 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
5073 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5074 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
5075 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5076 /*
5077 * XXX KDM just assuming a single pointer here, and not a
5078 * S/G list. If we start using S/G lists for config data,
5079 * we'll need to know how to clean them up here as well.
5080 */
5081 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5082 free(io->scsiio.kern_data_ptr, M_CTL);
5083 ctl_done(io);
5084 retval = CTL_RETVAL_COMPLETE;
5085 } else {
5086 /*
5087 * XXX KDM now we need to continue data movement. Some
5088 * options:
5089 * - call ctl_scsiio() again? We don't do this for data
5090 * writes, because for those at least we know ahead of
5091 * time where the write will go and how long it is. For
5092 * config writes, though, that information is largely
5093 * contained within the write itself, thus we need to
5094 * parse out the data again.
5095 *
5096 * - Call some other function once the data is in?
5097 */
5098
5099 /*
5100 * XXX KDM call ctl_scsiio() again for now, and check flag
5101 * bits to see whether we're allocated or not.
5102 */
5103 retval = ctl_scsiio(&io->scsiio);
5104 }
5105 return (retval);
5106 }
5107
5108 /*
5109 * This gets called by a backend driver when it is done with a
5110 * data_submit method.
5111 */
5112 void
ctl_data_submit_done(union ctl_io * io)5113 ctl_data_submit_done(union ctl_io *io)
5114 {
5115 /*
5116 * If the IO_CONT flag is set, we need to call the supplied
5117 * function to continue processing the I/O, instead of completing
5118 * the I/O just yet.
5119 *
5120 * If there is an error, though, we don't want to keep processing.
5121 * Instead, just send status back to the initiator.
5122 */
5123 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5124 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5125 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5126 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5127 io->scsiio.io_cont(io);
5128 return;
5129 }
5130 ctl_done(io);
5131 }
5132
5133 /*
5134 * This gets called by a backend driver when it is done with a
5135 * configuration write.
5136 */
5137 void
ctl_config_write_done(union ctl_io * io)5138 ctl_config_write_done(union ctl_io *io)
5139 {
5140 uint8_t *buf;
5141
5142 /*
5143 * If the IO_CONT flag is set, we need to call the supplied
5144 * function to continue processing the I/O, instead of completing
5145 * the I/O just yet.
5146 *
5147 * If there is an error, though, we don't want to keep processing.
5148 * Instead, just send status back to the initiator.
5149 */
5150 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5151 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5152 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5153 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5154 io->scsiio.io_cont(io);
5155 return;
5156 }
5157 /*
5158 * Since a configuration write can be done for commands that actually
5159 * have data allocated, like write buffer, and commands that have
5160 * no data, like start/stop unit, we need to check here.
5161 */
5162 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5163 buf = io->scsiio.kern_data_ptr;
5164 else
5165 buf = NULL;
5166 ctl_done(io);
5167 if (buf)
5168 free(buf, M_CTL);
5169 }
5170
5171 void
ctl_config_read_done(union ctl_io * io)5172 ctl_config_read_done(union ctl_io *io)
5173 {
5174 uint8_t *buf;
5175
5176 /*
5177 * If there is some error -- we are done, skip data transfer.
5178 */
5179 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5180 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5181 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5182 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5183 buf = io->scsiio.kern_data_ptr;
5184 else
5185 buf = NULL;
5186 ctl_done(io);
5187 if (buf)
5188 free(buf, M_CTL);
5189 return;
5190 }
5191
5192 /*
5193 * If the IO_CONT flag is set, we need to call the supplied
5194 * function to continue processing the I/O, instead of completing
5195 * the I/O just yet.
5196 */
5197 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5198 io->scsiio.io_cont(io);
5199 return;
5200 }
5201
5202 ctl_datamove(io);
5203 }
5204
5205 /*
5206 * SCSI release command.
5207 */
5208 int
ctl_scsi_release(struct ctl_scsiio * ctsio)5209 ctl_scsi_release(struct ctl_scsiio *ctsio)
5210 {
5211 struct ctl_lun *lun = CTL_LUN(ctsio);
5212 uint32_t residx;
5213
5214 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5215
5216 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5217
5218 /*
5219 * XXX KDM right now, we only support LUN reservation. We don't
5220 * support 3rd party reservations, or extent reservations, which
5221 * might actually need the parameter list. If we've gotten this
5222 * far, we've got a LUN reservation. Anything else got kicked out
5223 * above. So, according to SPC, ignore the length.
5224 */
5225
5226 mtx_lock(&lun->lun_lock);
5227
5228 /*
5229 * According to SPC, it is not an error for an intiator to attempt
5230 * to release a reservation on a LUN that isn't reserved, or that
5231 * is reserved by another initiator. The reservation can only be
5232 * released, though, by the initiator who made it or by one of
5233 * several reset type events.
5234 */
5235 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5236 lun->flags &= ~CTL_LUN_RESERVED;
5237
5238 mtx_unlock(&lun->lun_lock);
5239
5240 ctl_set_success(ctsio);
5241 ctl_done((union ctl_io *)ctsio);
5242 return (CTL_RETVAL_COMPLETE);
5243 }
5244
5245 int
ctl_scsi_reserve(struct ctl_scsiio * ctsio)5246 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5247 {
5248 struct ctl_lun *lun = CTL_LUN(ctsio);
5249 uint32_t residx;
5250
5251 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5252
5253 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5254
5255 /*
5256 * XXX KDM right now, we only support LUN reservation. We don't
5257 * support 3rd party reservations, or extent reservations, which
5258 * might actually need the parameter list. If we've gotten this
5259 * far, we've got a LUN reservation. Anything else got kicked out
5260 * above. So, according to SPC, ignore the length.
5261 */
5262
5263 mtx_lock(&lun->lun_lock);
5264 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5265 ctl_set_reservation_conflict(ctsio);
5266 goto bailout;
5267 }
5268
5269 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */
5270 if (lun->flags & CTL_LUN_PR_RESERVED) {
5271 ctl_set_success(ctsio);
5272 goto bailout;
5273 }
5274
5275 lun->flags |= CTL_LUN_RESERVED;
5276 lun->res_idx = residx;
5277 ctl_set_success(ctsio);
5278
5279 bailout:
5280 mtx_unlock(&lun->lun_lock);
5281 ctl_done((union ctl_io *)ctsio);
5282 return (CTL_RETVAL_COMPLETE);
5283 }
5284
5285 int
ctl_start_stop(struct ctl_scsiio * ctsio)5286 ctl_start_stop(struct ctl_scsiio *ctsio)
5287 {
5288 struct ctl_lun *lun = CTL_LUN(ctsio);
5289 struct scsi_start_stop_unit *cdb;
5290 int retval;
5291
5292 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5293
5294 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5295
5296 if ((cdb->how & SSS_PC_MASK) == 0) {
5297 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
5298 (cdb->how & SSS_START) == 0) {
5299 uint32_t residx;
5300
5301 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5302 if (ctl_get_prkey(lun, residx) == 0 ||
5303 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) {
5304
5305 ctl_set_reservation_conflict(ctsio);
5306 ctl_done((union ctl_io *)ctsio);
5307 return (CTL_RETVAL_COMPLETE);
5308 }
5309 }
5310
5311 if ((cdb->how & SSS_LOEJ) &&
5312 (lun->flags & CTL_LUN_REMOVABLE) == 0) {
5313 ctl_set_invalid_field(ctsio,
5314 /*sks_valid*/ 1,
5315 /*command*/ 1,
5316 /*field*/ 4,
5317 /*bit_valid*/ 1,
5318 /*bit*/ 1);
5319 ctl_done((union ctl_io *)ctsio);
5320 return (CTL_RETVAL_COMPLETE);
5321 }
5322
5323 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) &&
5324 lun->prevent_count > 0) {
5325 /* "Medium removal prevented" */
5326 ctl_set_sense(ctsio, /*current_error*/ 1,
5327 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ?
5328 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST,
5329 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE);
5330 ctl_done((union ctl_io *)ctsio);
5331 return (CTL_RETVAL_COMPLETE);
5332 }
5333 }
5334
5335 retval = lun->backend->config_write((union ctl_io *)ctsio);
5336 return (retval);
5337 }
5338
5339 int
ctl_prevent_allow(struct ctl_scsiio * ctsio)5340 ctl_prevent_allow(struct ctl_scsiio *ctsio)
5341 {
5342 struct ctl_lun *lun = CTL_LUN(ctsio);
5343 struct scsi_prevent *cdb;
5344 int retval;
5345 uint32_t initidx;
5346
5347 CTL_DEBUG_PRINT(("ctl_prevent_allow\n"));
5348
5349 cdb = (struct scsi_prevent *)ctsio->cdb;
5350
5351 if ((lun->flags & CTL_LUN_REMOVABLE) == 0 || lun->prevent == NULL) {
5352 ctl_set_invalid_opcode(ctsio);
5353 ctl_done((union ctl_io *)ctsio);
5354 return (CTL_RETVAL_COMPLETE);
5355 }
5356
5357 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5358 mtx_lock(&lun->lun_lock);
5359 if ((cdb->how & PR_PREVENT) &&
5360 ctl_is_set(lun->prevent, initidx) == 0) {
5361 ctl_set_mask(lun->prevent, initidx);
5362 lun->prevent_count++;
5363 } else if ((cdb->how & PR_PREVENT) == 0 &&
5364 ctl_is_set(lun->prevent, initidx)) {
5365 ctl_clear_mask(lun->prevent, initidx);
5366 lun->prevent_count--;
5367 }
5368 mtx_unlock(&lun->lun_lock);
5369 retval = lun->backend->config_write((union ctl_io *)ctsio);
5370 return (retval);
5371 }
5372
5373 /*
5374 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5375 * we don't really do anything with the LBA and length fields if the user
5376 * passes them in. Instead we'll just flush out the cache for the entire
5377 * LUN.
5378 */
5379 int
ctl_sync_cache(struct ctl_scsiio * ctsio)5380 ctl_sync_cache(struct ctl_scsiio *ctsio)
5381 {
5382 struct ctl_lun *lun = CTL_LUN(ctsio);
5383 struct ctl_lba_len_flags *lbalen;
5384 uint64_t starting_lba;
5385 uint32_t block_count;
5386 int retval;
5387 uint8_t byte2;
5388
5389 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5390
5391 retval = 0;
5392
5393 switch (ctsio->cdb[0]) {
5394 case SYNCHRONIZE_CACHE: {
5395 struct scsi_sync_cache *cdb;
5396 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5397
5398 starting_lba = scsi_4btoul(cdb->begin_lba);
5399 block_count = scsi_2btoul(cdb->lb_count);
5400 byte2 = cdb->byte2;
5401 break;
5402 }
5403 case SYNCHRONIZE_CACHE_16: {
5404 struct scsi_sync_cache_16 *cdb;
5405 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5406
5407 starting_lba = scsi_8btou64(cdb->begin_lba);
5408 block_count = scsi_4btoul(cdb->lb_count);
5409 byte2 = cdb->byte2;
5410 break;
5411 }
5412 default:
5413 ctl_set_invalid_opcode(ctsio);
5414 ctl_done((union ctl_io *)ctsio);
5415 goto bailout;
5416 break; /* NOTREACHED */
5417 }
5418
5419 /*
5420 * We check the LBA and length, but don't do anything with them.
5421 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5422 * get flushed. This check will just help satisfy anyone who wants
5423 * to see an error for an out of range LBA.
5424 */
5425 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5426 ctl_set_lba_out_of_range(ctsio,
5427 MAX(starting_lba, lun->be_lun->maxlba + 1));
5428 ctl_done((union ctl_io *)ctsio);
5429 goto bailout;
5430 }
5431
5432 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5433 lbalen->lba = starting_lba;
5434 lbalen->len = block_count;
5435 lbalen->flags = byte2;
5436 retval = lun->backend->config_write((union ctl_io *)ctsio);
5437
5438 bailout:
5439 return (retval);
5440 }
5441
5442 int
ctl_format(struct ctl_scsiio * ctsio)5443 ctl_format(struct ctl_scsiio *ctsio)
5444 {
5445 struct scsi_format *cdb;
5446 int length, defect_list_len;
5447
5448 CTL_DEBUG_PRINT(("ctl_format\n"));
5449
5450 cdb = (struct scsi_format *)ctsio->cdb;
5451
5452 length = 0;
5453 if (cdb->byte2 & SF_FMTDATA) {
5454 if (cdb->byte2 & SF_LONGLIST)
5455 length = sizeof(struct scsi_format_header_long);
5456 else
5457 length = sizeof(struct scsi_format_header_short);
5458 }
5459
5460 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5461 && (length > 0)) {
5462 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5463 ctsio->kern_data_len = length;
5464 ctsio->kern_total_len = length;
5465 ctsio->kern_rel_offset = 0;
5466 ctsio->kern_sg_entries = 0;
5467 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5468 ctsio->be_move_done = ctl_config_move_done;
5469 ctl_datamove((union ctl_io *)ctsio);
5470
5471 return (CTL_RETVAL_COMPLETE);
5472 }
5473
5474 defect_list_len = 0;
5475
5476 if (cdb->byte2 & SF_FMTDATA) {
5477 if (cdb->byte2 & SF_LONGLIST) {
5478 struct scsi_format_header_long *header;
5479
5480 header = (struct scsi_format_header_long *)
5481 ctsio->kern_data_ptr;
5482
5483 defect_list_len = scsi_4btoul(header->defect_list_len);
5484 if (defect_list_len != 0) {
5485 ctl_set_invalid_field(ctsio,
5486 /*sks_valid*/ 1,
5487 /*command*/ 0,
5488 /*field*/ 2,
5489 /*bit_valid*/ 0,
5490 /*bit*/ 0);
5491 goto bailout;
5492 }
5493 } else {
5494 struct scsi_format_header_short *header;
5495
5496 header = (struct scsi_format_header_short *)
5497 ctsio->kern_data_ptr;
5498
5499 defect_list_len = scsi_2btoul(header->defect_list_len);
5500 if (defect_list_len != 0) {
5501 ctl_set_invalid_field(ctsio,
5502 /*sks_valid*/ 1,
5503 /*command*/ 0,
5504 /*field*/ 2,
5505 /*bit_valid*/ 0,
5506 /*bit*/ 0);
5507 goto bailout;
5508 }
5509 }
5510 }
5511
5512 ctl_set_success(ctsio);
5513 bailout:
5514
5515 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5516 free(ctsio->kern_data_ptr, M_CTL);
5517 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5518 }
5519
5520 ctl_done((union ctl_io *)ctsio);
5521 return (CTL_RETVAL_COMPLETE);
5522 }
5523
5524 int
ctl_read_buffer(struct ctl_scsiio * ctsio)5525 ctl_read_buffer(struct ctl_scsiio *ctsio)
5526 {
5527 struct ctl_lun *lun = CTL_LUN(ctsio);
5528 uint64_t buffer_offset;
5529 uint32_t len;
5530 uint8_t byte2;
5531 static uint8_t descr[4];
5532 static uint8_t echo_descr[4] = { 0 };
5533
5534 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5535
5536 switch (ctsio->cdb[0]) {
5537 case READ_BUFFER: {
5538 struct scsi_read_buffer *cdb;
5539
5540 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5541 buffer_offset = scsi_3btoul(cdb->offset);
5542 len = scsi_3btoul(cdb->length);
5543 byte2 = cdb->byte2;
5544 break;
5545 }
5546 case READ_BUFFER_16: {
5547 struct scsi_read_buffer_16 *cdb;
5548
5549 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb;
5550 buffer_offset = scsi_8btou64(cdb->offset);
5551 len = scsi_4btoul(cdb->length);
5552 byte2 = cdb->byte2;
5553 break;
5554 }
5555 default: /* This shouldn't happen. */
5556 ctl_set_invalid_opcode(ctsio);
5557 ctl_done((union ctl_io *)ctsio);
5558 return (CTL_RETVAL_COMPLETE);
5559 }
5560
5561 if (buffer_offset > CTL_WRITE_BUFFER_SIZE ||
5562 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5563 ctl_set_invalid_field(ctsio,
5564 /*sks_valid*/ 1,
5565 /*command*/ 1,
5566 /*field*/ 6,
5567 /*bit_valid*/ 0,
5568 /*bit*/ 0);
5569 ctl_done((union ctl_io *)ctsio);
5570 return (CTL_RETVAL_COMPLETE);
5571 }
5572
5573 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5574 descr[0] = 0;
5575 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5576 ctsio->kern_data_ptr = descr;
5577 len = min(len, sizeof(descr));
5578 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5579 ctsio->kern_data_ptr = echo_descr;
5580 len = min(len, sizeof(echo_descr));
5581 } else {
5582 if (lun->write_buffer == NULL) {
5583 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5584 M_CTL, M_WAITOK);
5585 }
5586 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5587 }
5588 ctsio->kern_data_len = len;
5589 ctsio->kern_total_len = len;
5590 ctsio->kern_rel_offset = 0;
5591 ctsio->kern_sg_entries = 0;
5592 ctl_set_success(ctsio);
5593 ctsio->be_move_done = ctl_config_move_done;
5594 ctl_datamove((union ctl_io *)ctsio);
5595 return (CTL_RETVAL_COMPLETE);
5596 }
5597
5598 int
ctl_write_buffer(struct ctl_scsiio * ctsio)5599 ctl_write_buffer(struct ctl_scsiio *ctsio)
5600 {
5601 struct ctl_lun *lun = CTL_LUN(ctsio);
5602 struct scsi_write_buffer *cdb;
5603 int buffer_offset, len;
5604
5605 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5606
5607 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5608
5609 len = scsi_3btoul(cdb->length);
5610 buffer_offset = scsi_3btoul(cdb->offset);
5611
5612 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5613 ctl_set_invalid_field(ctsio,
5614 /*sks_valid*/ 1,
5615 /*command*/ 1,
5616 /*field*/ 6,
5617 /*bit_valid*/ 0,
5618 /*bit*/ 0);
5619 ctl_done((union ctl_io *)ctsio);
5620 return (CTL_RETVAL_COMPLETE);
5621 }
5622
5623 /*
5624 * If we've got a kernel request that hasn't been malloced yet,
5625 * malloc it and tell the caller the data buffer is here.
5626 */
5627 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5628 if (lun->write_buffer == NULL) {
5629 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5630 M_CTL, M_WAITOK);
5631 }
5632 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5633 ctsio->kern_data_len = len;
5634 ctsio->kern_total_len = len;
5635 ctsio->kern_rel_offset = 0;
5636 ctsio->kern_sg_entries = 0;
5637 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5638 ctsio->be_move_done = ctl_config_move_done;
5639 ctl_datamove((union ctl_io *)ctsio);
5640
5641 return (CTL_RETVAL_COMPLETE);
5642 }
5643
5644 ctl_set_success(ctsio);
5645 ctl_done((union ctl_io *)ctsio);
5646 return (CTL_RETVAL_COMPLETE);
5647 }
5648
5649 int
ctl_write_same(struct ctl_scsiio * ctsio)5650 ctl_write_same(struct ctl_scsiio *ctsio)
5651 {
5652 struct ctl_lun *lun = CTL_LUN(ctsio);
5653 struct ctl_lba_len_flags *lbalen;
5654 uint64_t lba;
5655 uint32_t num_blocks;
5656 int len, retval;
5657 uint8_t byte2;
5658
5659 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5660
5661 switch (ctsio->cdb[0]) {
5662 case WRITE_SAME_10: {
5663 struct scsi_write_same_10 *cdb;
5664
5665 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5666
5667 lba = scsi_4btoul(cdb->addr);
5668 num_blocks = scsi_2btoul(cdb->length);
5669 byte2 = cdb->byte2;
5670 break;
5671 }
5672 case WRITE_SAME_16: {
5673 struct scsi_write_same_16 *cdb;
5674
5675 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5676
5677 lba = scsi_8btou64(cdb->addr);
5678 num_blocks = scsi_4btoul(cdb->length);
5679 byte2 = cdb->byte2;
5680 break;
5681 }
5682 default:
5683 /*
5684 * We got a command we don't support. This shouldn't
5685 * happen, commands should be filtered out above us.
5686 */
5687 ctl_set_invalid_opcode(ctsio);
5688 ctl_done((union ctl_io *)ctsio);
5689
5690 return (CTL_RETVAL_COMPLETE);
5691 break; /* NOTREACHED */
5692 }
5693
5694 /* ANCHOR flag can be used only together with UNMAP */
5695 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) {
5696 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5697 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5698 ctl_done((union ctl_io *)ctsio);
5699 return (CTL_RETVAL_COMPLETE);
5700 }
5701
5702 /*
5703 * The first check is to make sure we're in bounds, the second
5704 * check is to catch wrap-around problems. If the lba + num blocks
5705 * is less than the lba, then we've wrapped around and the block
5706 * range is invalid anyway.
5707 */
5708 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5709 || ((lba + num_blocks) < lba)) {
5710 ctl_set_lba_out_of_range(ctsio,
5711 MAX(lba, lun->be_lun->maxlba + 1));
5712 ctl_done((union ctl_io *)ctsio);
5713 return (CTL_RETVAL_COMPLETE);
5714 }
5715
5716 /* Zero number of blocks means "to the last logical block" */
5717 if (num_blocks == 0) {
5718 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5719 ctl_set_invalid_field(ctsio,
5720 /*sks_valid*/ 0,
5721 /*command*/ 1,
5722 /*field*/ 0,
5723 /*bit_valid*/ 0,
5724 /*bit*/ 0);
5725 ctl_done((union ctl_io *)ctsio);
5726 return (CTL_RETVAL_COMPLETE);
5727 }
5728 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5729 }
5730
5731 len = lun->be_lun->blocksize;
5732
5733 /*
5734 * If we've got a kernel request that hasn't been malloced yet,
5735 * malloc it and tell the caller the data buffer is here.
5736 */
5737 if ((byte2 & SWS_NDOB) == 0 &&
5738 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5739 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5740 ctsio->kern_data_len = len;
5741 ctsio->kern_total_len = len;
5742 ctsio->kern_rel_offset = 0;
5743 ctsio->kern_sg_entries = 0;
5744 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5745 ctsio->be_move_done = ctl_config_move_done;
5746 ctl_datamove((union ctl_io *)ctsio);
5747
5748 return (CTL_RETVAL_COMPLETE);
5749 }
5750
5751 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5752 lbalen->lba = lba;
5753 lbalen->len = num_blocks;
5754 lbalen->flags = byte2;
5755 retval = lun->backend->config_write((union ctl_io *)ctsio);
5756
5757 return (retval);
5758 }
5759
5760 int
ctl_unmap(struct ctl_scsiio * ctsio)5761 ctl_unmap(struct ctl_scsiio *ctsio)
5762 {
5763 struct ctl_lun *lun = CTL_LUN(ctsio);
5764 struct scsi_unmap *cdb;
5765 struct ctl_ptr_len_flags *ptrlen;
5766 struct scsi_unmap_header *hdr;
5767 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5768 uint64_t lba;
5769 uint32_t num_blocks;
5770 int len, retval;
5771 uint8_t byte2;
5772
5773 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5774
5775 cdb = (struct scsi_unmap *)ctsio->cdb;
5776 len = scsi_2btoul(cdb->length);
5777 byte2 = cdb->byte2;
5778
5779 /*
5780 * If we've got a kernel request that hasn't been malloced yet,
5781 * malloc it and tell the caller the data buffer is here.
5782 */
5783 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5784 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5785 ctsio->kern_data_len = len;
5786 ctsio->kern_total_len = len;
5787 ctsio->kern_rel_offset = 0;
5788 ctsio->kern_sg_entries = 0;
5789 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5790 ctsio->be_move_done = ctl_config_move_done;
5791 ctl_datamove((union ctl_io *)ctsio);
5792
5793 return (CTL_RETVAL_COMPLETE);
5794 }
5795
5796 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5797 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5798 if (len < sizeof (*hdr) ||
5799 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5800 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5801 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5802 ctl_set_invalid_field(ctsio,
5803 /*sks_valid*/ 0,
5804 /*command*/ 0,
5805 /*field*/ 0,
5806 /*bit_valid*/ 0,
5807 /*bit*/ 0);
5808 goto done;
5809 }
5810 len = scsi_2btoul(hdr->desc_length);
5811 buf = (struct scsi_unmap_desc *)(hdr + 1);
5812 end = buf + len / sizeof(*buf);
5813
5814 endnz = buf;
5815 for (range = buf; range < end; range++) {
5816 lba = scsi_8btou64(range->lba);
5817 num_blocks = scsi_4btoul(range->length);
5818 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5819 || ((lba + num_blocks) < lba)) {
5820 ctl_set_lba_out_of_range(ctsio,
5821 MAX(lba, lun->be_lun->maxlba + 1));
5822 ctl_done((union ctl_io *)ctsio);
5823 return (CTL_RETVAL_COMPLETE);
5824 }
5825 if (num_blocks != 0)
5826 endnz = range + 1;
5827 }
5828
5829 /*
5830 * Block backend can not handle zero last range.
5831 * Filter it out and return if there is nothing left.
5832 */
5833 len = (uint8_t *)endnz - (uint8_t *)buf;
5834 if (len == 0) {
5835 ctl_set_success(ctsio);
5836 goto done;
5837 }
5838
5839 mtx_lock(&lun->lun_lock);
5840 ptrlen = (struct ctl_ptr_len_flags *)
5841 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5842 ptrlen->ptr = (void *)buf;
5843 ptrlen->len = len;
5844 ptrlen->flags = byte2;
5845 ctl_try_unblock_others(lun, (union ctl_io *)ctsio, FALSE);
5846 mtx_unlock(&lun->lun_lock);
5847
5848 retval = lun->backend->config_write((union ctl_io *)ctsio);
5849 return (retval);
5850
5851 done:
5852 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5853 free(ctsio->kern_data_ptr, M_CTL);
5854 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5855 }
5856 ctl_done((union ctl_io *)ctsio);
5857 return (CTL_RETVAL_COMPLETE);
5858 }
5859
5860 int
ctl_default_page_handler(struct ctl_scsiio * ctsio,struct ctl_page_index * page_index,uint8_t * page_ptr)5861 ctl_default_page_handler(struct ctl_scsiio *ctsio,
5862 struct ctl_page_index *page_index, uint8_t *page_ptr)
5863 {
5864 struct ctl_lun *lun = CTL_LUN(ctsio);
5865 uint8_t *current_cp;
5866 int set_ua;
5867 uint32_t initidx;
5868
5869 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5870 set_ua = 0;
5871
5872 current_cp = (page_index->page_data + (page_index->page_len *
5873 CTL_PAGE_CURRENT));
5874
5875 mtx_lock(&lun->lun_lock);
5876 if (memcmp(current_cp, page_ptr, page_index->page_len)) {
5877 memcpy(current_cp, page_ptr, page_index->page_len);
5878 set_ua = 1;
5879 }
5880 if (set_ua != 0)
5881 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5882 mtx_unlock(&lun->lun_lock);
5883 if (set_ua) {
5884 ctl_isc_announce_mode(lun,
5885 ctl_get_initindex(&ctsio->io_hdr.nexus),
5886 page_index->page_code, page_index->subpage);
5887 }
5888 return (CTL_RETVAL_COMPLETE);
5889 }
5890
5891 static void
ctl_ie_timer(void * arg)5892 ctl_ie_timer(void *arg)
5893 {
5894 struct ctl_lun *lun = arg;
5895 uint64_t t;
5896
5897 if (lun->ie_asc == 0)
5898 return;
5899
5900 if (lun->MODE_IE.mrie == SIEP_MRIE_UA)
5901 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5902 else
5903 lun->ie_reported = 0;
5904
5905 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) {
5906 lun->ie_reportcnt++;
5907 t = scsi_4btoul(lun->MODE_IE.interval_timer);
5908 if (t == 0 || t == UINT32_MAX)
5909 t = 3000; /* 5 min */
5910 callout_schedule(&lun->ie_callout, t * hz / 10);
5911 }
5912 }
5913
5914 int
ctl_ie_page_handler(struct ctl_scsiio * ctsio,struct ctl_page_index * page_index,uint8_t * page_ptr)5915 ctl_ie_page_handler(struct ctl_scsiio *ctsio,
5916 struct ctl_page_index *page_index, uint8_t *page_ptr)
5917 {
5918 struct ctl_lun *lun = CTL_LUN(ctsio);
5919 struct scsi_info_exceptions_page *pg;
5920 uint64_t t;
5921
5922 (void)ctl_default_page_handler(ctsio, page_index, page_ptr);
5923
5924 pg = (struct scsi_info_exceptions_page *)page_ptr;
5925 mtx_lock(&lun->lun_lock);
5926 if (pg->info_flags & SIEP_FLAGS_TEST) {
5927 lun->ie_asc = 0x5d;
5928 lun->ie_ascq = 0xff;
5929 if (pg->mrie == SIEP_MRIE_UA) {
5930 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5931 lun->ie_reported = 1;
5932 } else {
5933 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
5934 lun->ie_reported = -1;
5935 }
5936 lun->ie_reportcnt = 1;
5937 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) {
5938 lun->ie_reportcnt++;
5939 t = scsi_4btoul(pg->interval_timer);
5940 if (t == 0 || t == UINT32_MAX)
5941 t = 3000; /* 5 min */
5942 callout_reset(&lun->ie_callout, t * hz / 10,
5943 ctl_ie_timer, lun);
5944 }
5945 } else {
5946 lun->ie_asc = 0;
5947 lun->ie_ascq = 0;
5948 lun->ie_reported = 1;
5949 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
5950 lun->ie_reportcnt = UINT32_MAX;
5951 callout_stop(&lun->ie_callout);
5952 }
5953 mtx_unlock(&lun->lun_lock);
5954 return (CTL_RETVAL_COMPLETE);
5955 }
5956
5957 static int
ctl_do_mode_select(union ctl_io * io)5958 ctl_do_mode_select(union ctl_io *io)
5959 {
5960 struct ctl_lun *lun = CTL_LUN(io);
5961 struct scsi_mode_page_header *page_header;
5962 struct ctl_page_index *page_index;
5963 struct ctl_scsiio *ctsio;
5964 int page_len, page_len_offset, page_len_size;
5965 union ctl_modepage_info *modepage_info;
5966 uint16_t *len_left, *len_used;
5967 int retval, i;
5968
5969 ctsio = &io->scsiio;
5970 page_index = NULL;
5971 page_len = 0;
5972
5973 modepage_info = (union ctl_modepage_info *)
5974 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
5975 len_left = &modepage_info->header.len_left;
5976 len_used = &modepage_info->header.len_used;
5977
5978 do_next_page:
5979
5980 page_header = (struct scsi_mode_page_header *)
5981 (ctsio->kern_data_ptr + *len_used);
5982
5983 if (*len_left == 0) {
5984 free(ctsio->kern_data_ptr, M_CTL);
5985 ctl_set_success(ctsio);
5986 ctl_done((union ctl_io *)ctsio);
5987 return (CTL_RETVAL_COMPLETE);
5988 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
5989
5990 free(ctsio->kern_data_ptr, M_CTL);
5991 ctl_set_param_len_error(ctsio);
5992 ctl_done((union ctl_io *)ctsio);
5993 return (CTL_RETVAL_COMPLETE);
5994
5995 } else if ((page_header->page_code & SMPH_SPF)
5996 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
5997
5998 free(ctsio->kern_data_ptr, M_CTL);
5999 ctl_set_param_len_error(ctsio);
6000 ctl_done((union ctl_io *)ctsio);
6001 return (CTL_RETVAL_COMPLETE);
6002 }
6003
6004
6005 /*
6006 * XXX KDM should we do something with the block descriptor?
6007 */
6008 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6009 page_index = &lun->mode_pages.index[i];
6010 if (lun->be_lun->lun_type == T_DIRECT &&
6011 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6012 continue;
6013 if (lun->be_lun->lun_type == T_PROCESSOR &&
6014 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6015 continue;
6016 if (lun->be_lun->lun_type == T_CDROM &&
6017 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6018 continue;
6019
6020 if ((page_index->page_code & SMPH_PC_MASK) !=
6021 (page_header->page_code & SMPH_PC_MASK))
6022 continue;
6023
6024 /*
6025 * If neither page has a subpage code, then we've got a
6026 * match.
6027 */
6028 if (((page_index->page_code & SMPH_SPF) == 0)
6029 && ((page_header->page_code & SMPH_SPF) == 0)) {
6030 page_len = page_header->page_length;
6031 break;
6032 }
6033
6034 /*
6035 * If both pages have subpages, then the subpage numbers
6036 * have to match.
6037 */
6038 if ((page_index->page_code & SMPH_SPF)
6039 && (page_header->page_code & SMPH_SPF)) {
6040 struct scsi_mode_page_header_sp *sph;
6041
6042 sph = (struct scsi_mode_page_header_sp *)page_header;
6043 if (page_index->subpage == sph->subpage) {
6044 page_len = scsi_2btoul(sph->page_length);
6045 break;
6046 }
6047 }
6048 }
6049
6050 /*
6051 * If we couldn't find the page, or if we don't have a mode select
6052 * handler for it, send back an error to the user.
6053 */
6054 if ((i >= CTL_NUM_MODE_PAGES)
6055 || (page_index->select_handler == NULL)) {
6056 ctl_set_invalid_field(ctsio,
6057 /*sks_valid*/ 1,
6058 /*command*/ 0,
6059 /*field*/ *len_used,
6060 /*bit_valid*/ 0,
6061 /*bit*/ 0);
6062 free(ctsio->kern_data_ptr, M_CTL);
6063 ctl_done((union ctl_io *)ctsio);
6064 return (CTL_RETVAL_COMPLETE);
6065 }
6066
6067 if (page_index->page_code & SMPH_SPF) {
6068 page_len_offset = 2;
6069 page_len_size = 2;
6070 } else {
6071 page_len_size = 1;
6072 page_len_offset = 1;
6073 }
6074
6075 /*
6076 * If the length the initiator gives us isn't the one we specify in
6077 * the mode page header, or if they didn't specify enough data in
6078 * the CDB to avoid truncating this page, kick out the request.
6079 */
6080 if (page_len != page_index->page_len - page_len_offset - page_len_size) {
6081 ctl_set_invalid_field(ctsio,
6082 /*sks_valid*/ 1,
6083 /*command*/ 0,
6084 /*field*/ *len_used + page_len_offset,
6085 /*bit_valid*/ 0,
6086 /*bit*/ 0);
6087 free(ctsio->kern_data_ptr, M_CTL);
6088 ctl_done((union ctl_io *)ctsio);
6089 return (CTL_RETVAL_COMPLETE);
6090 }
6091 if (*len_left < page_index->page_len) {
6092 free(ctsio->kern_data_ptr, M_CTL);
6093 ctl_set_param_len_error(ctsio);
6094 ctl_done((union ctl_io *)ctsio);
6095 return (CTL_RETVAL_COMPLETE);
6096 }
6097
6098 /*
6099 * Run through the mode page, checking to make sure that the bits
6100 * the user changed are actually legal for him to change.
6101 */
6102 for (i = 0; i < page_index->page_len; i++) {
6103 uint8_t *user_byte, *change_mask, *current_byte;
6104 int bad_bit;
6105 int j;
6106
6107 user_byte = (uint8_t *)page_header + i;
6108 change_mask = page_index->page_data +
6109 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6110 current_byte = page_index->page_data +
6111 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6112
6113 /*
6114 * Check to see whether the user set any bits in this byte
6115 * that he is not allowed to set.
6116 */
6117 if ((*user_byte & ~(*change_mask)) ==
6118 (*current_byte & ~(*change_mask)))
6119 continue;
6120
6121 /*
6122 * Go through bit by bit to determine which one is illegal.
6123 */
6124 bad_bit = 0;
6125 for (j = 7; j >= 0; j--) {
6126 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6127 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6128 bad_bit = i;
6129 break;
6130 }
6131 }
6132 ctl_set_invalid_field(ctsio,
6133 /*sks_valid*/ 1,
6134 /*command*/ 0,
6135 /*field*/ *len_used + i,
6136 /*bit_valid*/ 1,
6137 /*bit*/ bad_bit);
6138 free(ctsio->kern_data_ptr, M_CTL);
6139 ctl_done((union ctl_io *)ctsio);
6140 return (CTL_RETVAL_COMPLETE);
6141 }
6142
6143 /*
6144 * Decrement these before we call the page handler, since we may
6145 * end up getting called back one way or another before the handler
6146 * returns to this context.
6147 */
6148 *len_left -= page_index->page_len;
6149 *len_used += page_index->page_len;
6150
6151 retval = page_index->select_handler(ctsio, page_index,
6152 (uint8_t *)page_header);
6153
6154 /*
6155 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6156 * wait until this queued command completes to finish processing
6157 * the mode page. If it returns anything other than
6158 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6159 * already set the sense information, freed the data pointer, and
6160 * completed the io for us.
6161 */
6162 if (retval != CTL_RETVAL_COMPLETE)
6163 goto bailout_no_done;
6164
6165 /*
6166 * If the initiator sent us more than one page, parse the next one.
6167 */
6168 if (*len_left > 0)
6169 goto do_next_page;
6170
6171 ctl_set_success(ctsio);
6172 free(ctsio->kern_data_ptr, M_CTL);
6173 ctl_done((union ctl_io *)ctsio);
6174
6175 bailout_no_done:
6176
6177 return (CTL_RETVAL_COMPLETE);
6178
6179 }
6180
6181 int
ctl_mode_select(struct ctl_scsiio * ctsio)6182 ctl_mode_select(struct ctl_scsiio *ctsio)
6183 {
6184 struct ctl_lun *lun = CTL_LUN(ctsio);
6185 union ctl_modepage_info *modepage_info;
6186 int bd_len, i, header_size, param_len, pf, rtd, sp;
6187 uint32_t initidx;
6188
6189 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6190 switch (ctsio->cdb[0]) {
6191 case MODE_SELECT_6: {
6192 struct scsi_mode_select_6 *cdb;
6193
6194 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6195
6196 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6197 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6198 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6199 param_len = cdb->length;
6200 header_size = sizeof(struct scsi_mode_header_6);
6201 break;
6202 }
6203 case MODE_SELECT_10: {
6204 struct scsi_mode_select_10 *cdb;
6205
6206 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6207
6208 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6209 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6210 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6211 param_len = scsi_2btoul(cdb->length);
6212 header_size = sizeof(struct scsi_mode_header_10);
6213 break;
6214 }
6215 default:
6216 ctl_set_invalid_opcode(ctsio);
6217 ctl_done((union ctl_io *)ctsio);
6218 return (CTL_RETVAL_COMPLETE);
6219 }
6220
6221 if (rtd) {
6222 if (param_len != 0) {
6223 ctl_set_invalid_field(ctsio, /*sks_valid*/ 0,
6224 /*command*/ 1, /*field*/ 0,
6225 /*bit_valid*/ 0, /*bit*/ 0);
6226 ctl_done((union ctl_io *)ctsio);
6227 return (CTL_RETVAL_COMPLETE);
6228 }
6229
6230 /* Revert to defaults. */
6231 ctl_init_page_index(lun);
6232 mtx_lock(&lun->lun_lock);
6233 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
6234 mtx_unlock(&lun->lun_lock);
6235 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6236 ctl_isc_announce_mode(lun, -1,
6237 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
6238 lun->mode_pages.index[i].subpage);
6239 }
6240 ctl_set_success(ctsio);
6241 ctl_done((union ctl_io *)ctsio);
6242 return (CTL_RETVAL_COMPLETE);
6243 }
6244
6245 /*
6246 * From SPC-3:
6247 * "A parameter list length of zero indicates that the Data-Out Buffer
6248 * shall be empty. This condition shall not be considered as an error."
6249 */
6250 if (param_len == 0) {
6251 ctl_set_success(ctsio);
6252 ctl_done((union ctl_io *)ctsio);
6253 return (CTL_RETVAL_COMPLETE);
6254 }
6255
6256 /*
6257 * Since we'll hit this the first time through, prior to
6258 * allocation, we don't need to free a data buffer here.
6259 */
6260 if (param_len < header_size) {
6261 ctl_set_param_len_error(ctsio);
6262 ctl_done((union ctl_io *)ctsio);
6263 return (CTL_RETVAL_COMPLETE);
6264 }
6265
6266 /*
6267 * Allocate the data buffer and grab the user's data. In theory,
6268 * we shouldn't have to sanity check the parameter list length here
6269 * because the maximum size is 64K. We should be able to malloc
6270 * that much without too many problems.
6271 */
6272 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6273 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6274 ctsio->kern_data_len = param_len;
6275 ctsio->kern_total_len = param_len;
6276 ctsio->kern_rel_offset = 0;
6277 ctsio->kern_sg_entries = 0;
6278 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6279 ctsio->be_move_done = ctl_config_move_done;
6280 ctl_datamove((union ctl_io *)ctsio);
6281
6282 return (CTL_RETVAL_COMPLETE);
6283 }
6284
6285 switch (ctsio->cdb[0]) {
6286 case MODE_SELECT_6: {
6287 struct scsi_mode_header_6 *mh6;
6288
6289 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6290 bd_len = mh6->blk_desc_len;
6291 break;
6292 }
6293 case MODE_SELECT_10: {
6294 struct scsi_mode_header_10 *mh10;
6295
6296 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6297 bd_len = scsi_2btoul(mh10->blk_desc_len);
6298 break;
6299 }
6300 default:
6301 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6302 }
6303
6304 if (param_len < (header_size + bd_len)) {
6305 free(ctsio->kern_data_ptr, M_CTL);
6306 ctl_set_param_len_error(ctsio);
6307 ctl_done((union ctl_io *)ctsio);
6308 return (CTL_RETVAL_COMPLETE);
6309 }
6310
6311 /*
6312 * Set the IO_CONT flag, so that if this I/O gets passed to
6313 * ctl_config_write_done(), it'll get passed back to
6314 * ctl_do_mode_select() for further processing, or completion if
6315 * we're all done.
6316 */
6317 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6318 ctsio->io_cont = ctl_do_mode_select;
6319
6320 modepage_info = (union ctl_modepage_info *)
6321 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6322 memset(modepage_info, 0, sizeof(*modepage_info));
6323 modepage_info->header.len_left = param_len - header_size - bd_len;
6324 modepage_info->header.len_used = header_size + bd_len;
6325
6326 return (ctl_do_mode_select((union ctl_io *)ctsio));
6327 }
6328
6329 int
ctl_mode_sense(struct ctl_scsiio * ctsio)6330 ctl_mode_sense(struct ctl_scsiio *ctsio)
6331 {
6332 struct ctl_lun *lun = CTL_LUN(ctsio);
6333 int pc, page_code, dbd, llba, subpage;
6334 int alloc_len, page_len, header_len, total_len;
6335 struct scsi_mode_block_descr *block_desc;
6336 struct ctl_page_index *page_index;
6337
6338 dbd = 0;
6339 llba = 0;
6340 block_desc = NULL;
6341
6342 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6343
6344 switch (ctsio->cdb[0]) {
6345 case MODE_SENSE_6: {
6346 struct scsi_mode_sense_6 *cdb;
6347
6348 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6349
6350 header_len = sizeof(struct scsi_mode_hdr_6);
6351 if (cdb->byte2 & SMS_DBD)
6352 dbd = 1;
6353 else
6354 header_len += sizeof(struct scsi_mode_block_descr);
6355
6356 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6357 page_code = cdb->page & SMS_PAGE_CODE;
6358 subpage = cdb->subpage;
6359 alloc_len = cdb->length;
6360 break;
6361 }
6362 case MODE_SENSE_10: {
6363 struct scsi_mode_sense_10 *cdb;
6364
6365 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6366
6367 header_len = sizeof(struct scsi_mode_hdr_10);
6368
6369 if (cdb->byte2 & SMS_DBD)
6370 dbd = 1;
6371 else
6372 header_len += sizeof(struct scsi_mode_block_descr);
6373 if (cdb->byte2 & SMS10_LLBAA)
6374 llba = 1;
6375 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6376 page_code = cdb->page & SMS_PAGE_CODE;
6377 subpage = cdb->subpage;
6378 alloc_len = scsi_2btoul(cdb->length);
6379 break;
6380 }
6381 default:
6382 ctl_set_invalid_opcode(ctsio);
6383 ctl_done((union ctl_io *)ctsio);
6384 return (CTL_RETVAL_COMPLETE);
6385 break; /* NOTREACHED */
6386 }
6387
6388 /*
6389 * We have to make a first pass through to calculate the size of
6390 * the pages that match the user's query. Then we allocate enough
6391 * memory to hold it, and actually copy the data into the buffer.
6392 */
6393 switch (page_code) {
6394 case SMS_ALL_PAGES_PAGE: {
6395 u_int i;
6396
6397 page_len = 0;
6398
6399 /*
6400 * At the moment, values other than 0 and 0xff here are
6401 * reserved according to SPC-3.
6402 */
6403 if ((subpage != SMS_SUBPAGE_PAGE_0)
6404 && (subpage != SMS_SUBPAGE_ALL)) {
6405 ctl_set_invalid_field(ctsio,
6406 /*sks_valid*/ 1,
6407 /*command*/ 1,
6408 /*field*/ 3,
6409 /*bit_valid*/ 0,
6410 /*bit*/ 0);
6411 ctl_done((union ctl_io *)ctsio);
6412 return (CTL_RETVAL_COMPLETE);
6413 }
6414
6415 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6416 page_index = &lun->mode_pages.index[i];
6417
6418 /* Make sure the page is supported for this dev type */
6419 if (lun->be_lun->lun_type == T_DIRECT &&
6420 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6421 continue;
6422 if (lun->be_lun->lun_type == T_PROCESSOR &&
6423 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6424 continue;
6425 if (lun->be_lun->lun_type == T_CDROM &&
6426 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6427 continue;
6428
6429 /*
6430 * We don't use this subpage if the user didn't
6431 * request all subpages.
6432 */
6433 if ((page_index->subpage != 0)
6434 && (subpage == SMS_SUBPAGE_PAGE_0))
6435 continue;
6436
6437 page_len += page_index->page_len;
6438 }
6439 break;
6440 }
6441 default: {
6442 u_int i;
6443
6444 page_len = 0;
6445
6446 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6447 page_index = &lun->mode_pages.index[i];
6448
6449 /* Make sure the page is supported for this dev type */
6450 if (lun->be_lun->lun_type == T_DIRECT &&
6451 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6452 continue;
6453 if (lun->be_lun->lun_type == T_PROCESSOR &&
6454 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6455 continue;
6456 if (lun->be_lun->lun_type == T_CDROM &&
6457 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6458 continue;
6459
6460 /* Look for the right page code */
6461 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6462 continue;
6463
6464 /* Look for the right subpage or the subpage wildcard*/
6465 if ((page_index->subpage != subpage)
6466 && (subpage != SMS_SUBPAGE_ALL))
6467 continue;
6468
6469 page_len += page_index->page_len;
6470 }
6471
6472 if (page_len == 0) {
6473 ctl_set_invalid_field(ctsio,
6474 /*sks_valid*/ 1,
6475 /*command*/ 1,
6476 /*field*/ 2,
6477 /*bit_valid*/ 1,
6478 /*bit*/ 5);
6479 ctl_done((union ctl_io *)ctsio);
6480 return (CTL_RETVAL_COMPLETE);
6481 }
6482 break;
6483 }
6484 }
6485
6486 total_len = header_len + page_len;
6487
6488 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6489 ctsio->kern_sg_entries = 0;
6490 ctsio->kern_rel_offset = 0;
6491 ctsio->kern_data_len = min(total_len, alloc_len);
6492 ctsio->kern_total_len = ctsio->kern_data_len;
6493
6494 switch (ctsio->cdb[0]) {
6495 case MODE_SENSE_6: {
6496 struct scsi_mode_hdr_6 *header;
6497
6498 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6499
6500 header->datalen = MIN(total_len - 1, 254);
6501 if (lun->be_lun->lun_type == T_DIRECT) {
6502 header->dev_specific = 0x10; /* DPOFUA */
6503 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6504 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6505 header->dev_specific |= 0x80; /* WP */
6506 }
6507 if (dbd)
6508 header->block_descr_len = 0;
6509 else
6510 header->block_descr_len =
6511 sizeof(struct scsi_mode_block_descr);
6512 block_desc = (struct scsi_mode_block_descr *)&header[1];
6513 break;
6514 }
6515 case MODE_SENSE_10: {
6516 struct scsi_mode_hdr_10 *header;
6517 int datalen;
6518
6519 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6520
6521 datalen = MIN(total_len - 2, 65533);
6522 scsi_ulto2b(datalen, header->datalen);
6523 if (lun->be_lun->lun_type == T_DIRECT) {
6524 header->dev_specific = 0x10; /* DPOFUA */
6525 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6526 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6527 header->dev_specific |= 0x80; /* WP */
6528 }
6529 if (dbd)
6530 scsi_ulto2b(0, header->block_descr_len);
6531 else
6532 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6533 header->block_descr_len);
6534 block_desc = (struct scsi_mode_block_descr *)&header[1];
6535 break;
6536 }
6537 default:
6538 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6539 }
6540
6541 /*
6542 * If we've got a disk, use its blocksize in the block
6543 * descriptor. Otherwise, just set it to 0.
6544 */
6545 if (dbd == 0) {
6546 if (lun->be_lun->lun_type == T_DIRECT)
6547 scsi_ulto3b(lun->be_lun->blocksize,
6548 block_desc->block_len);
6549 else
6550 scsi_ulto3b(0, block_desc->block_len);
6551 }
6552
6553 switch (page_code) {
6554 case SMS_ALL_PAGES_PAGE: {
6555 int i, data_used;
6556
6557 data_used = header_len;
6558 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6559 struct ctl_page_index *page_index;
6560
6561 page_index = &lun->mode_pages.index[i];
6562 if (lun->be_lun->lun_type == T_DIRECT &&
6563 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6564 continue;
6565 if (lun->be_lun->lun_type == T_PROCESSOR &&
6566 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6567 continue;
6568 if (lun->be_lun->lun_type == T_CDROM &&
6569 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6570 continue;
6571
6572 /*
6573 * We don't use this subpage if the user didn't
6574 * request all subpages. We already checked (above)
6575 * to make sure the user only specified a subpage
6576 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6577 */
6578 if ((page_index->subpage != 0)
6579 && (subpage == SMS_SUBPAGE_PAGE_0))
6580 continue;
6581
6582 /*
6583 * Call the handler, if it exists, to update the
6584 * page to the latest values.
6585 */
6586 if (page_index->sense_handler != NULL)
6587 page_index->sense_handler(ctsio, page_index,pc);
6588
6589 memcpy(ctsio->kern_data_ptr + data_used,
6590 page_index->page_data +
6591 (page_index->page_len * pc),
6592 page_index->page_len);
6593 data_used += page_index->page_len;
6594 }
6595 break;
6596 }
6597 default: {
6598 int i, data_used;
6599
6600 data_used = header_len;
6601
6602 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6603 struct ctl_page_index *page_index;
6604
6605 page_index = &lun->mode_pages.index[i];
6606
6607 /* Look for the right page code */
6608 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6609 continue;
6610
6611 /* Look for the right subpage or the subpage wildcard*/
6612 if ((page_index->subpage != subpage)
6613 && (subpage != SMS_SUBPAGE_ALL))
6614 continue;
6615
6616 /* Make sure the page is supported for this dev type */
6617 if (lun->be_lun->lun_type == T_DIRECT &&
6618 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6619 continue;
6620 if (lun->be_lun->lun_type == T_PROCESSOR &&
6621 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6622 continue;
6623 if (lun->be_lun->lun_type == T_CDROM &&
6624 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6625 continue;
6626
6627 /*
6628 * Call the handler, if it exists, to update the
6629 * page to the latest values.
6630 */
6631 if (page_index->sense_handler != NULL)
6632 page_index->sense_handler(ctsio, page_index,pc);
6633
6634 memcpy(ctsio->kern_data_ptr + data_used,
6635 page_index->page_data +
6636 (page_index->page_len * pc),
6637 page_index->page_len);
6638 data_used += page_index->page_len;
6639 }
6640 break;
6641 }
6642 }
6643
6644 ctl_set_success(ctsio);
6645 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6646 ctsio->be_move_done = ctl_config_move_done;
6647 ctl_datamove((union ctl_io *)ctsio);
6648 return (CTL_RETVAL_COMPLETE);
6649 }
6650
6651 int
ctl_lbp_log_sense_handler(struct ctl_scsiio * ctsio,struct ctl_page_index * page_index,int pc)6652 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6653 struct ctl_page_index *page_index,
6654 int pc)
6655 {
6656 struct ctl_lun *lun = CTL_LUN(ctsio);
6657 struct scsi_log_param_header *phdr;
6658 uint8_t *data;
6659 uint64_t val;
6660
6661 data = page_index->page_data;
6662
6663 if (lun->backend->lun_attr != NULL &&
6664 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6665 != UINT64_MAX) {
6666 phdr = (struct scsi_log_param_header *)data;
6667 scsi_ulto2b(0x0001, phdr->param_code);
6668 phdr->param_control = SLP_LBIN | SLP_LP;
6669 phdr->param_len = 8;
6670 data = (uint8_t *)(phdr + 1);
6671 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6672 data[4] = 0x02; /* per-pool */
6673 data += phdr->param_len;
6674 }
6675
6676 if (lun->backend->lun_attr != NULL &&
6677 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6678 != UINT64_MAX) {
6679 phdr = (struct scsi_log_param_header *)data;
6680 scsi_ulto2b(0x0002, phdr->param_code);
6681 phdr->param_control = SLP_LBIN | SLP_LP;
6682 phdr->param_len = 8;
6683 data = (uint8_t *)(phdr + 1);
6684 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6685 data[4] = 0x01; /* per-LUN */
6686 data += phdr->param_len;
6687 }
6688
6689 if (lun->backend->lun_attr != NULL &&
6690 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6691 != UINT64_MAX) {
6692 phdr = (struct scsi_log_param_header *)data;
6693 scsi_ulto2b(0x00f1, phdr->param_code);
6694 phdr->param_control = SLP_LBIN | SLP_LP;
6695 phdr->param_len = 8;
6696 data = (uint8_t *)(phdr + 1);
6697 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6698 data[4] = 0x02; /* per-pool */
6699 data += phdr->param_len;
6700 }
6701
6702 if (lun->backend->lun_attr != NULL &&
6703 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6704 != UINT64_MAX) {
6705 phdr = (struct scsi_log_param_header *)data;
6706 scsi_ulto2b(0x00f2, phdr->param_code);
6707 phdr->param_control = SLP_LBIN | SLP_LP;
6708 phdr->param_len = 8;
6709 data = (uint8_t *)(phdr + 1);
6710 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6711 data[4] = 0x02; /* per-pool */
6712 data += phdr->param_len;
6713 }
6714
6715 page_index->page_len = data - page_index->page_data;
6716 return (0);
6717 }
6718
6719 int
ctl_sap_log_sense_handler(struct ctl_scsiio * ctsio,struct ctl_page_index * page_index,int pc)6720 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6721 struct ctl_page_index *page_index,
6722 int pc)
6723 {
6724 struct ctl_lun *lun = CTL_LUN(ctsio);
6725 struct stat_page *data;
6726 struct bintime *t;
6727
6728 data = (struct stat_page *)page_index->page_data;
6729
6730 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6731 data->sap.hdr.param_control = SLP_LBIN;
6732 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6733 sizeof(struct scsi_log_param_header);
6734 scsi_u64to8b(lun->stats.operations[CTL_STATS_READ],
6735 data->sap.read_num);
6736 scsi_u64to8b(lun->stats.operations[CTL_STATS_WRITE],
6737 data->sap.write_num);
6738 if (lun->be_lun->blocksize > 0) {
6739 scsi_u64to8b(lun->stats.bytes[CTL_STATS_WRITE] /
6740 lun->be_lun->blocksize, data->sap.recvieved_lba);
6741 scsi_u64to8b(lun->stats.bytes[CTL_STATS_READ] /
6742 lun->be_lun->blocksize, data->sap.transmitted_lba);
6743 }
6744 t = &lun->stats.time[CTL_STATS_READ];
6745 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6746 data->sap.read_int);
6747 t = &lun->stats.time[CTL_STATS_WRITE];
6748 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6749 data->sap.write_int);
6750 scsi_u64to8b(0, data->sap.weighted_num);
6751 scsi_u64to8b(0, data->sap.weighted_int);
6752 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6753 data->it.hdr.param_control = SLP_LBIN;
6754 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6755 sizeof(struct scsi_log_param_header);
6756 #ifdef CTL_TIME_IO
6757 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6758 #endif
6759 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6760 data->it.hdr.param_control = SLP_LBIN;
6761 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6762 sizeof(struct scsi_log_param_header);
6763 scsi_ulto4b(3, data->ti.exponent);
6764 scsi_ulto4b(1, data->ti.integer);
6765 return (0);
6766 }
6767
6768 int
ctl_ie_log_sense_handler(struct ctl_scsiio * ctsio,struct ctl_page_index * page_index,int pc)6769 ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio,
6770 struct ctl_page_index *page_index,
6771 int pc)
6772 {
6773 struct ctl_lun *lun = CTL_LUN(ctsio);
6774 struct scsi_log_informational_exceptions *data;
6775
6776 data = (struct scsi_log_informational_exceptions *)page_index->page_data;
6777
6778 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code);
6779 data->hdr.param_control = SLP_LBIN;
6780 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) -
6781 sizeof(struct scsi_log_param_header);
6782 data->ie_asc = lun->ie_asc;
6783 data->ie_ascq = lun->ie_ascq;
6784 data->temperature = 0xff;
6785 return (0);
6786 }
6787
6788 int
ctl_log_sense(struct ctl_scsiio * ctsio)6789 ctl_log_sense(struct ctl_scsiio *ctsio)
6790 {
6791 struct ctl_lun *lun = CTL_LUN(ctsio);
6792 int i, pc, page_code, subpage;
6793 int alloc_len, total_len;
6794 struct ctl_page_index *page_index;
6795 struct scsi_log_sense *cdb;
6796 struct scsi_log_header *header;
6797
6798 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6799
6800 cdb = (struct scsi_log_sense *)ctsio->cdb;
6801 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6802 page_code = cdb->page & SLS_PAGE_CODE;
6803 subpage = cdb->subpage;
6804 alloc_len = scsi_2btoul(cdb->length);
6805
6806 page_index = NULL;
6807 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6808 page_index = &lun->log_pages.index[i];
6809
6810 /* Look for the right page code */
6811 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6812 continue;
6813
6814 /* Look for the right subpage or the subpage wildcard*/
6815 if (page_index->subpage != subpage)
6816 continue;
6817
6818 break;
6819 }
6820 if (i >= CTL_NUM_LOG_PAGES) {
6821 ctl_set_invalid_field(ctsio,
6822 /*sks_valid*/ 1,
6823 /*command*/ 1,
6824 /*field*/ 2,
6825 /*bit_valid*/ 0,
6826 /*bit*/ 0);
6827 ctl_done((union ctl_io *)ctsio);
6828 return (CTL_RETVAL_COMPLETE);
6829 }
6830
6831 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6832
6833 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6834 ctsio->kern_sg_entries = 0;
6835 ctsio->kern_rel_offset = 0;
6836 ctsio->kern_data_len = min(total_len, alloc_len);
6837 ctsio->kern_total_len = ctsio->kern_data_len;
6838
6839 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6840 header->page = page_index->page_code;
6841 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING)
6842 header->page |= SL_DS;
6843 if (page_index->subpage) {
6844 header->page |= SL_SPF;
6845 header->subpage = page_index->subpage;
6846 }
6847 scsi_ulto2b(page_index->page_len, header->datalen);
6848
6849 /*
6850 * Call the handler, if it exists, to update the
6851 * page to the latest values.
6852 */
6853 if (page_index->sense_handler != NULL)
6854 page_index->sense_handler(ctsio, page_index, pc);
6855
6856 memcpy(header + 1, page_index->page_data, page_index->page_len);
6857
6858 ctl_set_success(ctsio);
6859 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6860 ctsio->be_move_done = ctl_config_move_done;
6861 ctl_datamove((union ctl_io *)ctsio);
6862 return (CTL_RETVAL_COMPLETE);
6863 }
6864
6865 int
ctl_read_capacity(struct ctl_scsiio * ctsio)6866 ctl_read_capacity(struct ctl_scsiio *ctsio)
6867 {
6868 struct ctl_lun *lun = CTL_LUN(ctsio);
6869 struct scsi_read_capacity *cdb;
6870 struct scsi_read_capacity_data *data;
6871 uint32_t lba;
6872
6873 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6874
6875 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6876
6877 lba = scsi_4btoul(cdb->addr);
6878 if (((cdb->pmi & SRC_PMI) == 0)
6879 && (lba != 0)) {
6880 ctl_set_invalid_field(/*ctsio*/ ctsio,
6881 /*sks_valid*/ 1,
6882 /*command*/ 1,
6883 /*field*/ 2,
6884 /*bit_valid*/ 0,
6885 /*bit*/ 0);
6886 ctl_done((union ctl_io *)ctsio);
6887 return (CTL_RETVAL_COMPLETE);
6888 }
6889
6890 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6891 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6892 ctsio->kern_data_len = sizeof(*data);
6893 ctsio->kern_total_len = sizeof(*data);
6894 ctsio->kern_rel_offset = 0;
6895 ctsio->kern_sg_entries = 0;
6896
6897 /*
6898 * If the maximum LBA is greater than 0xfffffffe, the user must
6899 * issue a SERVICE ACTION IN (16) command, with the read capacity
6900 * serivce action set.
6901 */
6902 if (lun->be_lun->maxlba > 0xfffffffe)
6903 scsi_ulto4b(0xffffffff, data->addr);
6904 else
6905 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6906
6907 /*
6908 * XXX KDM this may not be 512 bytes...
6909 */
6910 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6911
6912 ctl_set_success(ctsio);
6913 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6914 ctsio->be_move_done = ctl_config_move_done;
6915 ctl_datamove((union ctl_io *)ctsio);
6916 return (CTL_RETVAL_COMPLETE);
6917 }
6918
6919 int
ctl_read_capacity_16(struct ctl_scsiio * ctsio)6920 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6921 {
6922 struct ctl_lun *lun = CTL_LUN(ctsio);
6923 struct scsi_read_capacity_16 *cdb;
6924 struct scsi_read_capacity_data_long *data;
6925 uint64_t lba;
6926 uint32_t alloc_len;
6927
6928 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6929
6930 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6931
6932 alloc_len = scsi_4btoul(cdb->alloc_len);
6933 lba = scsi_8btou64(cdb->addr);
6934
6935 if ((cdb->reladr & SRC16_PMI)
6936 && (lba != 0)) {
6937 ctl_set_invalid_field(/*ctsio*/ ctsio,
6938 /*sks_valid*/ 1,
6939 /*command*/ 1,
6940 /*field*/ 2,
6941 /*bit_valid*/ 0,
6942 /*bit*/ 0);
6943 ctl_done((union ctl_io *)ctsio);
6944 return (CTL_RETVAL_COMPLETE);
6945 }
6946
6947 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6948 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6949 ctsio->kern_rel_offset = 0;
6950 ctsio->kern_sg_entries = 0;
6951 ctsio->kern_data_len = min(sizeof(*data), alloc_len);
6952 ctsio->kern_total_len = ctsio->kern_data_len;
6953
6954 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
6955 /* XXX KDM this may not be 512 bytes... */
6956 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6957 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
6958 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
6959 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
6960 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
6961
6962 ctl_set_success(ctsio);
6963 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6964 ctsio->be_move_done = ctl_config_move_done;
6965 ctl_datamove((union ctl_io *)ctsio);
6966 return (CTL_RETVAL_COMPLETE);
6967 }
6968
6969 int
ctl_get_lba_status(struct ctl_scsiio * ctsio)6970 ctl_get_lba_status(struct ctl_scsiio *ctsio)
6971 {
6972 struct ctl_lun *lun = CTL_LUN(ctsio);
6973 struct scsi_get_lba_status *cdb;
6974 struct scsi_get_lba_status_data *data;
6975 struct ctl_lba_len_flags *lbalen;
6976 uint64_t lba;
6977 uint32_t alloc_len, total_len;
6978 int retval;
6979
6980 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
6981
6982 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
6983 lba = scsi_8btou64(cdb->addr);
6984 alloc_len = scsi_4btoul(cdb->alloc_len);
6985
6986 if (lba > lun->be_lun->maxlba) {
6987 ctl_set_lba_out_of_range(ctsio, lba);
6988 ctl_done((union ctl_io *)ctsio);
6989 return (CTL_RETVAL_COMPLETE);
6990 }
6991
6992 total_len = sizeof(*data) + sizeof(data->descr[0]);
6993 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6994 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
6995 ctsio->kern_rel_offset = 0;
6996 ctsio->kern_sg_entries = 0;
6997 ctsio->kern_data_len = min(total_len, alloc_len);
6998 ctsio->kern_total_len = ctsio->kern_data_len;
6999
7000 /* Fill dummy data in case backend can't tell anything. */
7001 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
7002 scsi_u64to8b(lba, data->descr[0].addr);
7003 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
7004 data->descr[0].length);
7005 data->descr[0].status = 0; /* Mapped or unknown. */
7006
7007 ctl_set_success(ctsio);
7008 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7009 ctsio->be_move_done = ctl_config_move_done;
7010
7011 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7012 lbalen->lba = lba;
7013 lbalen->len = total_len;
7014 lbalen->flags = 0;
7015 retval = lun->backend->config_read((union ctl_io *)ctsio);
7016 return (retval);
7017 }
7018
7019 int
ctl_read_defect(struct ctl_scsiio * ctsio)7020 ctl_read_defect(struct ctl_scsiio *ctsio)
7021 {
7022 struct scsi_read_defect_data_10 *ccb10;
7023 struct scsi_read_defect_data_12 *ccb12;
7024 struct scsi_read_defect_data_hdr_10 *data10;
7025 struct scsi_read_defect_data_hdr_12 *data12;
7026 uint32_t alloc_len, data_len;
7027 uint8_t format;
7028
7029 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7030
7031 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7032 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7033 format = ccb10->format;
7034 alloc_len = scsi_2btoul(ccb10->alloc_length);
7035 data_len = sizeof(*data10);
7036 } else {
7037 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7038 format = ccb12->format;
7039 alloc_len = scsi_4btoul(ccb12->alloc_length);
7040 data_len = sizeof(*data12);
7041 }
7042 if (alloc_len == 0) {
7043 ctl_set_success(ctsio);
7044 ctl_done((union ctl_io *)ctsio);
7045 return (CTL_RETVAL_COMPLETE);
7046 }
7047
7048 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7049 ctsio->kern_rel_offset = 0;
7050 ctsio->kern_sg_entries = 0;
7051 ctsio->kern_data_len = min(data_len, alloc_len);
7052 ctsio->kern_total_len = ctsio->kern_data_len;
7053
7054 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7055 data10 = (struct scsi_read_defect_data_hdr_10 *)
7056 ctsio->kern_data_ptr;
7057 data10->format = format;
7058 scsi_ulto2b(0, data10->length);
7059 } else {
7060 data12 = (struct scsi_read_defect_data_hdr_12 *)
7061 ctsio->kern_data_ptr;
7062 data12->format = format;
7063 scsi_ulto2b(0, data12->generation);
7064 scsi_ulto4b(0, data12->length);
7065 }
7066
7067 ctl_set_success(ctsio);
7068 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7069 ctsio->be_move_done = ctl_config_move_done;
7070 ctl_datamove((union ctl_io *)ctsio);
7071 return (CTL_RETVAL_COMPLETE);
7072 }
7073
7074 int
ctl_report_tagret_port_groups(struct ctl_scsiio * ctsio)7075 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7076 {
7077 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7078 struct ctl_lun *lun = CTL_LUN(ctsio);
7079 struct scsi_maintenance_in *cdb;
7080 int retval;
7081 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os;
7082 int num_ha_groups, num_target_ports, shared_group;
7083 struct ctl_port *port;
7084 struct scsi_target_group_data *rtg_ptr;
7085 struct scsi_target_group_data_extended *rtg_ext_ptr;
7086 struct scsi_target_port_group_descriptor *tpg_desc;
7087
7088 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7089
7090 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7091 retval = CTL_RETVAL_COMPLETE;
7092
7093 switch (cdb->byte2 & STG_PDF_MASK) {
7094 case STG_PDF_LENGTH:
7095 ext = 0;
7096 break;
7097 case STG_PDF_EXTENDED:
7098 ext = 1;
7099 break;
7100 default:
7101 ctl_set_invalid_field(/*ctsio*/ ctsio,
7102 /*sks_valid*/ 1,
7103 /*command*/ 1,
7104 /*field*/ 2,
7105 /*bit_valid*/ 1,
7106 /*bit*/ 5);
7107 ctl_done((union ctl_io *)ctsio);
7108 return(retval);
7109 }
7110
7111 num_target_ports = 0;
7112 shared_group = (softc->is_single != 0);
7113 mtx_lock(&softc->ctl_lock);
7114 STAILQ_FOREACH(port, &softc->port_list, links) {
7115 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7116 continue;
7117 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7118 continue;
7119 num_target_ports++;
7120 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7121 shared_group = 1;
7122 }
7123 mtx_unlock(&softc->ctl_lock);
7124 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES;
7125
7126 if (ext)
7127 total_len = sizeof(struct scsi_target_group_data_extended);
7128 else
7129 total_len = sizeof(struct scsi_target_group_data);
7130 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7131 (shared_group + num_ha_groups) +
7132 sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7133
7134 alloc_len = scsi_4btoul(cdb->length);
7135
7136 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7137 ctsio->kern_sg_entries = 0;
7138 ctsio->kern_rel_offset = 0;
7139 ctsio->kern_data_len = min(total_len, alloc_len);
7140 ctsio->kern_total_len = ctsio->kern_data_len;
7141
7142 if (ext) {
7143 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7144 ctsio->kern_data_ptr;
7145 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7146 rtg_ext_ptr->format_type = 0x10;
7147 rtg_ext_ptr->implicit_transition_time = 0;
7148 tpg_desc = &rtg_ext_ptr->groups[0];
7149 } else {
7150 rtg_ptr = (struct scsi_target_group_data *)
7151 ctsio->kern_data_ptr;
7152 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7153 tpg_desc = &rtg_ptr->groups[0];
7154 }
7155
7156 mtx_lock(&softc->ctl_lock);
7157 pg = softc->port_min / softc->port_cnt;
7158 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) {
7159 /* Some shelf is known to be primary. */
7160 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7161 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7162 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7163 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7164 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7165 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7166 else
7167 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7168 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7169 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7170 } else {
7171 ts = os;
7172 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7173 }
7174 } else {
7175 /* No known primary shelf. */
7176 if (softc->ha_link == CTL_HA_LINK_OFFLINE) {
7177 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7178 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7179 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) {
7180 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7181 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7182 } else {
7183 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7184 }
7185 }
7186 if (shared_group) {
7187 tpg_desc->pref_state = ts;
7188 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7189 TPG_U_SUP | TPG_T_SUP;
7190 scsi_ulto2b(1, tpg_desc->target_port_group);
7191 tpg_desc->status = TPG_IMPLICIT;
7192 pc = 0;
7193 STAILQ_FOREACH(port, &softc->port_list, links) {
7194 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7195 continue;
7196 if (!softc->is_single &&
7197 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0)
7198 continue;
7199 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7200 continue;
7201 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7202 relative_target_port_identifier);
7203 pc++;
7204 }
7205 tpg_desc->target_port_count = pc;
7206 tpg_desc = (struct scsi_target_port_group_descriptor *)
7207 &tpg_desc->descriptors[pc];
7208 }
7209 for (g = 0; g < num_ha_groups; g++) {
7210 tpg_desc->pref_state = (g == pg) ? ts : os;
7211 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7212 TPG_U_SUP | TPG_T_SUP;
7213 scsi_ulto2b(2 + g, tpg_desc->target_port_group);
7214 tpg_desc->status = TPG_IMPLICIT;
7215 pc = 0;
7216 STAILQ_FOREACH(port, &softc->port_list, links) {
7217 if (port->targ_port < g * softc->port_cnt ||
7218 port->targ_port >= (g + 1) * softc->port_cnt)
7219 continue;
7220 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7221 continue;
7222 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7223 continue;
7224 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7225 continue;
7226 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7227 relative_target_port_identifier);
7228 pc++;
7229 }
7230 tpg_desc->target_port_count = pc;
7231 tpg_desc = (struct scsi_target_port_group_descriptor *)
7232 &tpg_desc->descriptors[pc];
7233 }
7234 mtx_unlock(&softc->ctl_lock);
7235
7236 ctl_set_success(ctsio);
7237 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7238 ctsio->be_move_done = ctl_config_move_done;
7239 ctl_datamove((union ctl_io *)ctsio);
7240 return(retval);
7241 }
7242
7243 int
ctl_report_supported_opcodes(struct ctl_scsiio * ctsio)7244 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7245 {
7246 struct ctl_lun *lun = CTL_LUN(ctsio);
7247 struct scsi_report_supported_opcodes *cdb;
7248 const struct ctl_cmd_entry *entry, *sentry;
7249 struct scsi_report_supported_opcodes_all *all;
7250 struct scsi_report_supported_opcodes_descr *descr;
7251 struct scsi_report_supported_opcodes_one *one;
7252 int retval;
7253 int alloc_len, total_len;
7254 int opcode, service_action, i, j, num;
7255
7256 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7257
7258 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7259 retval = CTL_RETVAL_COMPLETE;
7260
7261 opcode = cdb->requested_opcode;
7262 service_action = scsi_2btoul(cdb->requested_service_action);
7263 switch (cdb->options & RSO_OPTIONS_MASK) {
7264 case RSO_OPTIONS_ALL:
7265 num = 0;
7266 for (i = 0; i < 256; i++) {
7267 entry = &ctl_cmd_table[i];
7268 if (entry->flags & CTL_CMD_FLAG_SA5) {
7269 for (j = 0; j < 32; j++) {
7270 sentry = &((const struct ctl_cmd_entry *)
7271 entry->execute)[j];
7272 if (ctl_cmd_applicable(
7273 lun->be_lun->lun_type, sentry))
7274 num++;
7275 }
7276 } else {
7277 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7278 entry))
7279 num++;
7280 }
7281 }
7282 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7283 num * sizeof(struct scsi_report_supported_opcodes_descr);
7284 break;
7285 case RSO_OPTIONS_OC:
7286 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7287 ctl_set_invalid_field(/*ctsio*/ ctsio,
7288 /*sks_valid*/ 1,
7289 /*command*/ 1,
7290 /*field*/ 2,
7291 /*bit_valid*/ 1,
7292 /*bit*/ 2);
7293 ctl_done((union ctl_io *)ctsio);
7294 return (CTL_RETVAL_COMPLETE);
7295 }
7296 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7297 break;
7298 case RSO_OPTIONS_OC_SA:
7299 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7300 service_action >= 32) {
7301 ctl_set_invalid_field(/*ctsio*/ ctsio,
7302 /*sks_valid*/ 1,
7303 /*command*/ 1,
7304 /*field*/ 2,
7305 /*bit_valid*/ 1,
7306 /*bit*/ 2);
7307 ctl_done((union ctl_io *)ctsio);
7308 return (CTL_RETVAL_COMPLETE);
7309 }
7310 /* FALLTHROUGH */
7311 case RSO_OPTIONS_OC_ASA:
7312 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7313 break;
7314 default:
7315 ctl_set_invalid_field(/*ctsio*/ ctsio,
7316 /*sks_valid*/ 1,
7317 /*command*/ 1,
7318 /*field*/ 2,
7319 /*bit_valid*/ 1,
7320 /*bit*/ 2);
7321 ctl_done((union ctl_io *)ctsio);
7322 return (CTL_RETVAL_COMPLETE);
7323 }
7324
7325 alloc_len = scsi_4btoul(cdb->length);
7326
7327 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7328 ctsio->kern_sg_entries = 0;
7329 ctsio->kern_rel_offset = 0;
7330 ctsio->kern_data_len = min(total_len, alloc_len);
7331 ctsio->kern_total_len = ctsio->kern_data_len;
7332
7333 switch (cdb->options & RSO_OPTIONS_MASK) {
7334 case RSO_OPTIONS_ALL:
7335 all = (struct scsi_report_supported_opcodes_all *)
7336 ctsio->kern_data_ptr;
7337 num = 0;
7338 for (i = 0; i < 256; i++) {
7339 entry = &ctl_cmd_table[i];
7340 if (entry->flags & CTL_CMD_FLAG_SA5) {
7341 for (j = 0; j < 32; j++) {
7342 sentry = &((const struct ctl_cmd_entry *)
7343 entry->execute)[j];
7344 if (!ctl_cmd_applicable(
7345 lun->be_lun->lun_type, sentry))
7346 continue;
7347 descr = &all->descr[num++];
7348 descr->opcode = i;
7349 scsi_ulto2b(j, descr->service_action);
7350 descr->flags = RSO_SERVACTV;
7351 scsi_ulto2b(sentry->length,
7352 descr->cdb_length);
7353 }
7354 } else {
7355 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7356 entry))
7357 continue;
7358 descr = &all->descr[num++];
7359 descr->opcode = i;
7360 scsi_ulto2b(0, descr->service_action);
7361 descr->flags = 0;
7362 scsi_ulto2b(entry->length, descr->cdb_length);
7363 }
7364 }
7365 scsi_ulto4b(
7366 num * sizeof(struct scsi_report_supported_opcodes_descr),
7367 all->length);
7368 break;
7369 case RSO_OPTIONS_OC:
7370 one = (struct scsi_report_supported_opcodes_one *)
7371 ctsio->kern_data_ptr;
7372 entry = &ctl_cmd_table[opcode];
7373 goto fill_one;
7374 case RSO_OPTIONS_OC_SA:
7375 one = (struct scsi_report_supported_opcodes_one *)
7376 ctsio->kern_data_ptr;
7377 entry = &ctl_cmd_table[opcode];
7378 entry = &((const struct ctl_cmd_entry *)
7379 entry->execute)[service_action];
7380 fill_one:
7381 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7382 one->support = 3;
7383 scsi_ulto2b(entry->length, one->cdb_length);
7384 one->cdb_usage[0] = opcode;
7385 memcpy(&one->cdb_usage[1], entry->usage,
7386 entry->length - 1);
7387 } else
7388 one->support = 1;
7389 break;
7390 case RSO_OPTIONS_OC_ASA:
7391 one = (struct scsi_report_supported_opcodes_one *)
7392 ctsio->kern_data_ptr;
7393 entry = &ctl_cmd_table[opcode];
7394 if (entry->flags & CTL_CMD_FLAG_SA5) {
7395 entry = &((const struct ctl_cmd_entry *)
7396 entry->execute)[service_action];
7397 } else if (service_action != 0) {
7398 one->support = 1;
7399 break;
7400 }
7401 goto fill_one;
7402 }
7403
7404 ctl_set_success(ctsio);
7405 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7406 ctsio->be_move_done = ctl_config_move_done;
7407 ctl_datamove((union ctl_io *)ctsio);
7408 return(retval);
7409 }
7410
7411 int
ctl_report_supported_tmf(struct ctl_scsiio * ctsio)7412 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7413 {
7414 struct scsi_report_supported_tmf *cdb;
7415 struct scsi_report_supported_tmf_ext_data *data;
7416 int retval;
7417 int alloc_len, total_len;
7418
7419 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7420
7421 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7422
7423 retval = CTL_RETVAL_COMPLETE;
7424
7425 if (cdb->options & RST_REPD)
7426 total_len = sizeof(struct scsi_report_supported_tmf_ext_data);
7427 else
7428 total_len = sizeof(struct scsi_report_supported_tmf_data);
7429 alloc_len = scsi_4btoul(cdb->length);
7430
7431 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7432 ctsio->kern_sg_entries = 0;
7433 ctsio->kern_rel_offset = 0;
7434 ctsio->kern_data_len = min(total_len, alloc_len);
7435 ctsio->kern_total_len = ctsio->kern_data_len;
7436
7437 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr;
7438 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7439 RST_TRS;
7440 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7441 data->length = total_len - 4;
7442
7443 ctl_set_success(ctsio);
7444 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7445 ctsio->be_move_done = ctl_config_move_done;
7446 ctl_datamove((union ctl_io *)ctsio);
7447 return (retval);
7448 }
7449
7450 int
ctl_report_timestamp(struct ctl_scsiio * ctsio)7451 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7452 {
7453 struct scsi_report_timestamp *cdb;
7454 struct scsi_report_timestamp_data *data;
7455 struct timeval tv;
7456 int64_t timestamp;
7457 int retval;
7458 int alloc_len, total_len;
7459
7460 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7461
7462 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7463
7464 retval = CTL_RETVAL_COMPLETE;
7465
7466 total_len = sizeof(struct scsi_report_timestamp_data);
7467 alloc_len = scsi_4btoul(cdb->length);
7468
7469 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7470 ctsio->kern_sg_entries = 0;
7471 ctsio->kern_rel_offset = 0;
7472 ctsio->kern_data_len = min(total_len, alloc_len);
7473 ctsio->kern_total_len = ctsio->kern_data_len;
7474
7475 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7476 scsi_ulto2b(sizeof(*data) - 2, data->length);
7477 data->origin = RTS_ORIG_OUTSIDE;
7478 getmicrotime(&tv);
7479 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7480 scsi_ulto4b(timestamp >> 16, data->timestamp);
7481 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7482
7483 ctl_set_success(ctsio);
7484 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7485 ctsio->be_move_done = ctl_config_move_done;
7486 ctl_datamove((union ctl_io *)ctsio);
7487 return (retval);
7488 }
7489
7490 int
ctl_persistent_reserve_in(struct ctl_scsiio * ctsio)7491 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7492 {
7493 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7494 struct ctl_lun *lun = CTL_LUN(ctsio);
7495 struct scsi_per_res_in *cdb;
7496 int alloc_len, total_len = 0;
7497 /* struct scsi_per_res_in_rsrv in_data; */
7498 uint64_t key;
7499
7500 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7501
7502 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7503
7504 alloc_len = scsi_2btoul(cdb->length);
7505
7506 retry:
7507 mtx_lock(&lun->lun_lock);
7508 switch (cdb->action) {
7509 case SPRI_RK: /* read keys */
7510 total_len = sizeof(struct scsi_per_res_in_keys) +
7511 lun->pr_key_count *
7512 sizeof(struct scsi_per_res_key);
7513 break;
7514 case SPRI_RR: /* read reservation */
7515 if (lun->flags & CTL_LUN_PR_RESERVED)
7516 total_len = sizeof(struct scsi_per_res_in_rsrv);
7517 else
7518 total_len = sizeof(struct scsi_per_res_in_header);
7519 break;
7520 case SPRI_RC: /* report capabilities */
7521 total_len = sizeof(struct scsi_per_res_cap);
7522 break;
7523 case SPRI_RS: /* read full status */
7524 total_len = sizeof(struct scsi_per_res_in_header) +
7525 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7526 lun->pr_key_count;
7527 break;
7528 default:
7529 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7530 }
7531 mtx_unlock(&lun->lun_lock);
7532
7533 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7534 ctsio->kern_rel_offset = 0;
7535 ctsio->kern_sg_entries = 0;
7536 ctsio->kern_data_len = min(total_len, alloc_len);
7537 ctsio->kern_total_len = ctsio->kern_data_len;
7538
7539 mtx_lock(&lun->lun_lock);
7540 switch (cdb->action) {
7541 case SPRI_RK: { // read keys
7542 struct scsi_per_res_in_keys *res_keys;
7543 int i, key_count;
7544
7545 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7546
7547 /*
7548 * We had to drop the lock to allocate our buffer, which
7549 * leaves time for someone to come in with another
7550 * persistent reservation. (That is unlikely, though,
7551 * since this should be the only persistent reservation
7552 * command active right now.)
7553 */
7554 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7555 (lun->pr_key_count *
7556 sizeof(struct scsi_per_res_key)))){
7557 mtx_unlock(&lun->lun_lock);
7558 free(ctsio->kern_data_ptr, M_CTL);
7559 printf("%s: reservation length changed, retrying\n",
7560 __func__);
7561 goto retry;
7562 }
7563
7564 scsi_ulto4b(lun->pr_generation, res_keys->header.generation);
7565
7566 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7567 lun->pr_key_count, res_keys->header.length);
7568
7569 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7570 if ((key = ctl_get_prkey(lun, i)) == 0)
7571 continue;
7572
7573 /*
7574 * We used lun->pr_key_count to calculate the
7575 * size to allocate. If it turns out the number of
7576 * initiators with the registered flag set is
7577 * larger than that (i.e. they haven't been kept in
7578 * sync), we've got a problem.
7579 */
7580 if (key_count >= lun->pr_key_count) {
7581 key_count++;
7582 continue;
7583 }
7584 scsi_u64to8b(key, res_keys->keys[key_count].key);
7585 key_count++;
7586 }
7587 break;
7588 }
7589 case SPRI_RR: { // read reservation
7590 struct scsi_per_res_in_rsrv *res;
7591 int tmp_len, header_only;
7592
7593 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7594
7595 scsi_ulto4b(lun->pr_generation, res->header.generation);
7596
7597 if (lun->flags & CTL_LUN_PR_RESERVED)
7598 {
7599 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7600 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7601 res->header.length);
7602 header_only = 0;
7603 } else {
7604 tmp_len = sizeof(struct scsi_per_res_in_header);
7605 scsi_ulto4b(0, res->header.length);
7606 header_only = 1;
7607 }
7608
7609 /*
7610 * We had to drop the lock to allocate our buffer, which
7611 * leaves time for someone to come in with another
7612 * persistent reservation. (That is unlikely, though,
7613 * since this should be the only persistent reservation
7614 * command active right now.)
7615 */
7616 if (tmp_len != total_len) {
7617 mtx_unlock(&lun->lun_lock);
7618 free(ctsio->kern_data_ptr, M_CTL);
7619 printf("%s: reservation status changed, retrying\n",
7620 __func__);
7621 goto retry;
7622 }
7623
7624 /*
7625 * No reservation held, so we're done.
7626 */
7627 if (header_only != 0)
7628 break;
7629
7630 /*
7631 * If the registration is an All Registrants type, the key
7632 * is 0, since it doesn't really matter.
7633 */
7634 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7635 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7636 res->data.reservation);
7637 }
7638 res->data.scopetype = lun->pr_res_type;
7639 break;
7640 }
7641 case SPRI_RC: //report capabilities
7642 {
7643 struct scsi_per_res_cap *res_cap;
7644 uint16_t type_mask;
7645
7646 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7647 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7648 res_cap->flags1 = SPRI_CRH;
7649 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5;
7650 type_mask = SPRI_TM_WR_EX_AR |
7651 SPRI_TM_EX_AC_RO |
7652 SPRI_TM_WR_EX_RO |
7653 SPRI_TM_EX_AC |
7654 SPRI_TM_WR_EX |
7655 SPRI_TM_EX_AC_AR;
7656 scsi_ulto2b(type_mask, res_cap->type_mask);
7657 break;
7658 }
7659 case SPRI_RS: { // read full status
7660 struct scsi_per_res_in_full *res_status;
7661 struct scsi_per_res_in_full_desc *res_desc;
7662 struct ctl_port *port;
7663 int i, len;
7664
7665 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7666
7667 /*
7668 * We had to drop the lock to allocate our buffer, which
7669 * leaves time for someone to come in with another
7670 * persistent reservation. (That is unlikely, though,
7671 * since this should be the only persistent reservation
7672 * command active right now.)
7673 */
7674 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7675 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7676 lun->pr_key_count)){
7677 mtx_unlock(&lun->lun_lock);
7678 free(ctsio->kern_data_ptr, M_CTL);
7679 printf("%s: reservation length changed, retrying\n",
7680 __func__);
7681 goto retry;
7682 }
7683
7684 scsi_ulto4b(lun->pr_generation, res_status->header.generation);
7685
7686 res_desc = &res_status->desc[0];
7687 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7688 if ((key = ctl_get_prkey(lun, i)) == 0)
7689 continue;
7690
7691 scsi_u64to8b(key, res_desc->res_key.key);
7692 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7693 (lun->pr_res_idx == i ||
7694 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7695 res_desc->flags = SPRI_FULL_R_HOLDER;
7696 res_desc->scopetype = lun->pr_res_type;
7697 }
7698 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7699 res_desc->rel_trgt_port_id);
7700 len = 0;
7701 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7702 if (port != NULL)
7703 len = ctl_create_iid(port,
7704 i % CTL_MAX_INIT_PER_PORT,
7705 res_desc->transport_id);
7706 scsi_ulto4b(len, res_desc->additional_length);
7707 res_desc = (struct scsi_per_res_in_full_desc *)
7708 &res_desc->transport_id[len];
7709 }
7710 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7711 res_status->header.length);
7712 break;
7713 }
7714 default:
7715 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7716 }
7717 mtx_unlock(&lun->lun_lock);
7718
7719 ctl_set_success(ctsio);
7720 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7721 ctsio->be_move_done = ctl_config_move_done;
7722 ctl_datamove((union ctl_io *)ctsio);
7723 return (CTL_RETVAL_COMPLETE);
7724 }
7725
7726 /*
7727 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7728 * it should return.
7729 */
7730 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)7731 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7732 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7733 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7734 struct scsi_per_res_out_parms* param)
7735 {
7736 union ctl_ha_msg persis_io;
7737 int i;
7738
7739 mtx_lock(&lun->lun_lock);
7740 if (sa_res_key == 0) {
7741 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7742 /* validate scope and type */
7743 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7744 SPR_LU_SCOPE) {
7745 mtx_unlock(&lun->lun_lock);
7746 ctl_set_invalid_field(/*ctsio*/ ctsio,
7747 /*sks_valid*/ 1,
7748 /*command*/ 1,
7749 /*field*/ 2,
7750 /*bit_valid*/ 1,
7751 /*bit*/ 4);
7752 ctl_done((union ctl_io *)ctsio);
7753 return (1);
7754 }
7755
7756 if (type>8 || type==2 || type==4 || type==0) {
7757 mtx_unlock(&lun->lun_lock);
7758 ctl_set_invalid_field(/*ctsio*/ ctsio,
7759 /*sks_valid*/ 1,
7760 /*command*/ 1,
7761 /*field*/ 2,
7762 /*bit_valid*/ 1,
7763 /*bit*/ 0);
7764 ctl_done((union ctl_io *)ctsio);
7765 return (1);
7766 }
7767
7768 /*
7769 * Unregister everybody else and build UA for
7770 * them
7771 */
7772 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7773 if (i == residx || ctl_get_prkey(lun, i) == 0)
7774 continue;
7775
7776 ctl_clr_prkey(lun, i);
7777 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7778 }
7779 lun->pr_key_count = 1;
7780 lun->pr_res_type = type;
7781 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7782 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7783 lun->pr_res_idx = residx;
7784 lun->pr_generation++;
7785 mtx_unlock(&lun->lun_lock);
7786
7787 /* send msg to other side */
7788 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7789 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7790 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7791 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7792 persis_io.pr.pr_info.res_type = type;
7793 memcpy(persis_io.pr.pr_info.sa_res_key,
7794 param->serv_act_res_key,
7795 sizeof(param->serv_act_res_key));
7796 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7797 sizeof(persis_io.pr), M_WAITOK);
7798 } else {
7799 /* not all registrants */
7800 mtx_unlock(&lun->lun_lock);
7801 free(ctsio->kern_data_ptr, M_CTL);
7802 ctl_set_invalid_field(ctsio,
7803 /*sks_valid*/ 1,
7804 /*command*/ 0,
7805 /*field*/ 8,
7806 /*bit_valid*/ 0,
7807 /*bit*/ 0);
7808 ctl_done((union ctl_io *)ctsio);
7809 return (1);
7810 }
7811 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7812 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7813 int found = 0;
7814
7815 if (res_key == sa_res_key) {
7816 /* special case */
7817 /*
7818 * The spec implies this is not good but doesn't
7819 * say what to do. There are two choices either
7820 * generate a res conflict or check condition
7821 * with illegal field in parameter data. Since
7822 * that is what is done when the sa_res_key is
7823 * zero I'll take that approach since this has
7824 * to do with the sa_res_key.
7825 */
7826 mtx_unlock(&lun->lun_lock);
7827 free(ctsio->kern_data_ptr, M_CTL);
7828 ctl_set_invalid_field(ctsio,
7829 /*sks_valid*/ 1,
7830 /*command*/ 0,
7831 /*field*/ 8,
7832 /*bit_valid*/ 0,
7833 /*bit*/ 0);
7834 ctl_done((union ctl_io *)ctsio);
7835 return (1);
7836 }
7837
7838 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7839 if (ctl_get_prkey(lun, i) != sa_res_key)
7840 continue;
7841
7842 found = 1;
7843 ctl_clr_prkey(lun, i);
7844 lun->pr_key_count--;
7845 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7846 }
7847 if (!found) {
7848 mtx_unlock(&lun->lun_lock);
7849 free(ctsio->kern_data_ptr, M_CTL);
7850 ctl_set_reservation_conflict(ctsio);
7851 ctl_done((union ctl_io *)ctsio);
7852 return (CTL_RETVAL_COMPLETE);
7853 }
7854 lun->pr_generation++;
7855 mtx_unlock(&lun->lun_lock);
7856
7857 /* send msg to other side */
7858 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7859 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7860 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7861 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7862 persis_io.pr.pr_info.res_type = type;
7863 memcpy(persis_io.pr.pr_info.sa_res_key,
7864 param->serv_act_res_key,
7865 sizeof(param->serv_act_res_key));
7866 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7867 sizeof(persis_io.pr), M_WAITOK);
7868 } else {
7869 /* Reserved but not all registrants */
7870 /* sa_res_key is res holder */
7871 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
7872 /* validate scope and type */
7873 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7874 SPR_LU_SCOPE) {
7875 mtx_unlock(&lun->lun_lock);
7876 ctl_set_invalid_field(/*ctsio*/ ctsio,
7877 /*sks_valid*/ 1,
7878 /*command*/ 1,
7879 /*field*/ 2,
7880 /*bit_valid*/ 1,
7881 /*bit*/ 4);
7882 ctl_done((union ctl_io *)ctsio);
7883 return (1);
7884 }
7885
7886 if (type>8 || type==2 || type==4 || type==0) {
7887 mtx_unlock(&lun->lun_lock);
7888 ctl_set_invalid_field(/*ctsio*/ ctsio,
7889 /*sks_valid*/ 1,
7890 /*command*/ 1,
7891 /*field*/ 2,
7892 /*bit_valid*/ 1,
7893 /*bit*/ 0);
7894 ctl_done((union ctl_io *)ctsio);
7895 return (1);
7896 }
7897
7898 /*
7899 * Do the following:
7900 * if sa_res_key != res_key remove all
7901 * registrants w/sa_res_key and generate UA
7902 * for these registrants(Registrations
7903 * Preempted) if it wasn't an exclusive
7904 * reservation generate UA(Reservations
7905 * Preempted) for all other registered nexuses
7906 * if the type has changed. Establish the new
7907 * reservation and holder. If res_key and
7908 * sa_res_key are the same do the above
7909 * except don't unregister the res holder.
7910 */
7911
7912 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7913 if (i == residx || ctl_get_prkey(lun, i) == 0)
7914 continue;
7915
7916 if (sa_res_key == ctl_get_prkey(lun, i)) {
7917 ctl_clr_prkey(lun, i);
7918 lun->pr_key_count--;
7919 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7920 } else if (type != lun->pr_res_type &&
7921 (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
7922 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
7923 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
7924 }
7925 }
7926 lun->pr_res_type = type;
7927 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7928 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7929 lun->pr_res_idx = residx;
7930 else
7931 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
7932 lun->pr_generation++;
7933 mtx_unlock(&lun->lun_lock);
7934
7935 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7936 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7937 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7938 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7939 persis_io.pr.pr_info.res_type = type;
7940 memcpy(persis_io.pr.pr_info.sa_res_key,
7941 param->serv_act_res_key,
7942 sizeof(param->serv_act_res_key));
7943 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7944 sizeof(persis_io.pr), M_WAITOK);
7945 } else {
7946 /*
7947 * sa_res_key is not the res holder just
7948 * remove registrants
7949 */
7950 int found=0;
7951
7952 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7953 if (sa_res_key != ctl_get_prkey(lun, i))
7954 continue;
7955
7956 found = 1;
7957 ctl_clr_prkey(lun, i);
7958 lun->pr_key_count--;
7959 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7960 }
7961
7962 if (!found) {
7963 mtx_unlock(&lun->lun_lock);
7964 free(ctsio->kern_data_ptr, M_CTL);
7965 ctl_set_reservation_conflict(ctsio);
7966 ctl_done((union ctl_io *)ctsio);
7967 return (1);
7968 }
7969 lun->pr_generation++;
7970 mtx_unlock(&lun->lun_lock);
7971
7972 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7973 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7974 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7975 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7976 persis_io.pr.pr_info.res_type = type;
7977 memcpy(persis_io.pr.pr_info.sa_res_key,
7978 param->serv_act_res_key,
7979 sizeof(param->serv_act_res_key));
7980 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7981 sizeof(persis_io.pr), M_WAITOK);
7982 }
7983 }
7984 return (0);
7985 }
7986
7987 static void
ctl_pro_preempt_other(struct ctl_lun * lun,union ctl_ha_msg * msg)7988 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
7989 {
7990 uint64_t sa_res_key;
7991 int i;
7992
7993 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
7994
7995 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7996 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
7997 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
7998 if (sa_res_key == 0) {
7999 /*
8000 * Unregister everybody else and build UA for
8001 * them
8002 */
8003 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8004 if (i == msg->pr.pr_info.residx ||
8005 ctl_get_prkey(lun, i) == 0)
8006 continue;
8007
8008 ctl_clr_prkey(lun, i);
8009 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8010 }
8011
8012 lun->pr_key_count = 1;
8013 lun->pr_res_type = msg->pr.pr_info.res_type;
8014 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8015 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8016 lun->pr_res_idx = msg->pr.pr_info.residx;
8017 } else {
8018 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8019 if (sa_res_key == ctl_get_prkey(lun, i))
8020 continue;
8021
8022 ctl_clr_prkey(lun, i);
8023 lun->pr_key_count--;
8024 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8025 }
8026 }
8027 } else {
8028 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8029 if (i == msg->pr.pr_info.residx ||
8030 ctl_get_prkey(lun, i) == 0)
8031 continue;
8032
8033 if (sa_res_key == ctl_get_prkey(lun, i)) {
8034 ctl_clr_prkey(lun, i);
8035 lun->pr_key_count--;
8036 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8037 } else if (msg->pr.pr_info.res_type != lun->pr_res_type
8038 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8039 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8040 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8041 }
8042 }
8043 lun->pr_res_type = msg->pr.pr_info.res_type;
8044 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8045 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8046 lun->pr_res_idx = msg->pr.pr_info.residx;
8047 else
8048 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8049 }
8050 lun->pr_generation++;
8051
8052 }
8053
8054
8055 int
ctl_persistent_reserve_out(struct ctl_scsiio * ctsio)8056 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8057 {
8058 struct ctl_softc *softc = CTL_SOFTC(ctsio);
8059 struct ctl_lun *lun = CTL_LUN(ctsio);
8060 int retval;
8061 u_int32_t param_len;
8062 struct scsi_per_res_out *cdb;
8063 struct scsi_per_res_out_parms* param;
8064 uint32_t residx;
8065 uint64_t res_key, sa_res_key, key;
8066 uint8_t type;
8067 union ctl_ha_msg persis_io;
8068 int i;
8069
8070 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8071
8072 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8073 retval = CTL_RETVAL_COMPLETE;
8074
8075 /*
8076 * We only support whole-LUN scope. The scope & type are ignored for
8077 * register, register and ignore existing key and clear.
8078 * We sometimes ignore scope and type on preempts too!!
8079 * Verify reservation type here as well.
8080 */
8081 type = cdb->scope_type & SPR_TYPE_MASK;
8082 if ((cdb->action == SPRO_RESERVE)
8083 || (cdb->action == SPRO_RELEASE)) {
8084 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8085 ctl_set_invalid_field(/*ctsio*/ ctsio,
8086 /*sks_valid*/ 1,
8087 /*command*/ 1,
8088 /*field*/ 2,
8089 /*bit_valid*/ 1,
8090 /*bit*/ 4);
8091 ctl_done((union ctl_io *)ctsio);
8092 return (CTL_RETVAL_COMPLETE);
8093 }
8094
8095 if (type>8 || type==2 || type==4 || type==0) {
8096 ctl_set_invalid_field(/*ctsio*/ ctsio,
8097 /*sks_valid*/ 1,
8098 /*command*/ 1,
8099 /*field*/ 2,
8100 /*bit_valid*/ 1,
8101 /*bit*/ 0);
8102 ctl_done((union ctl_io *)ctsio);
8103 return (CTL_RETVAL_COMPLETE);
8104 }
8105 }
8106
8107 param_len = scsi_4btoul(cdb->length);
8108
8109 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8110 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8111 ctsio->kern_data_len = param_len;
8112 ctsio->kern_total_len = param_len;
8113 ctsio->kern_rel_offset = 0;
8114 ctsio->kern_sg_entries = 0;
8115 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8116 ctsio->be_move_done = ctl_config_move_done;
8117 ctl_datamove((union ctl_io *)ctsio);
8118
8119 return (CTL_RETVAL_COMPLETE);
8120 }
8121
8122 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8123
8124 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8125 res_key = scsi_8btou64(param->res_key.key);
8126 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8127
8128 /*
8129 * Validate the reservation key here except for SPRO_REG_IGNO
8130 * This must be done for all other service actions
8131 */
8132 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8133 mtx_lock(&lun->lun_lock);
8134 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8135 if (res_key != key) {
8136 /*
8137 * The current key passed in doesn't match
8138 * the one the initiator previously
8139 * registered.
8140 */
8141 mtx_unlock(&lun->lun_lock);
8142 free(ctsio->kern_data_ptr, M_CTL);
8143 ctl_set_reservation_conflict(ctsio);
8144 ctl_done((union ctl_io *)ctsio);
8145 return (CTL_RETVAL_COMPLETE);
8146 }
8147 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8148 /*
8149 * We are not registered
8150 */
8151 mtx_unlock(&lun->lun_lock);
8152 free(ctsio->kern_data_ptr, M_CTL);
8153 ctl_set_reservation_conflict(ctsio);
8154 ctl_done((union ctl_io *)ctsio);
8155 return (CTL_RETVAL_COMPLETE);
8156 } else if (res_key != 0) {
8157 /*
8158 * We are not registered and trying to register but
8159 * the register key isn't zero.
8160 */
8161 mtx_unlock(&lun->lun_lock);
8162 free(ctsio->kern_data_ptr, M_CTL);
8163 ctl_set_reservation_conflict(ctsio);
8164 ctl_done((union ctl_io *)ctsio);
8165 return (CTL_RETVAL_COMPLETE);
8166 }
8167 mtx_unlock(&lun->lun_lock);
8168 }
8169
8170 switch (cdb->action & SPRO_ACTION_MASK) {
8171 case SPRO_REGISTER:
8172 case SPRO_REG_IGNO: {
8173
8174 /*
8175 * We don't support any of these options, as we report in
8176 * the read capabilities request (see
8177 * ctl_persistent_reserve_in(), above).
8178 */
8179 if ((param->flags & SPR_SPEC_I_PT)
8180 || (param->flags & SPR_ALL_TG_PT)
8181 || (param->flags & SPR_APTPL)) {
8182 int bit_ptr;
8183
8184 if (param->flags & SPR_APTPL)
8185 bit_ptr = 0;
8186 else if (param->flags & SPR_ALL_TG_PT)
8187 bit_ptr = 2;
8188 else /* SPR_SPEC_I_PT */
8189 bit_ptr = 3;
8190
8191 free(ctsio->kern_data_ptr, M_CTL);
8192 ctl_set_invalid_field(ctsio,
8193 /*sks_valid*/ 1,
8194 /*command*/ 0,
8195 /*field*/ 20,
8196 /*bit_valid*/ 1,
8197 /*bit*/ bit_ptr);
8198 ctl_done((union ctl_io *)ctsio);
8199 return (CTL_RETVAL_COMPLETE);
8200 }
8201
8202 mtx_lock(&lun->lun_lock);
8203
8204 /*
8205 * The initiator wants to clear the
8206 * key/unregister.
8207 */
8208 if (sa_res_key == 0) {
8209 if ((res_key == 0
8210 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8211 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8212 && ctl_get_prkey(lun, residx) == 0)) {
8213 mtx_unlock(&lun->lun_lock);
8214 goto done;
8215 }
8216
8217 ctl_clr_prkey(lun, residx);
8218 lun->pr_key_count--;
8219
8220 if (residx == lun->pr_res_idx) {
8221 lun->flags &= ~CTL_LUN_PR_RESERVED;
8222 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8223
8224 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8225 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8226 lun->pr_key_count) {
8227 /*
8228 * If the reservation is a registrants
8229 * only type we need to generate a UA
8230 * for other registered inits. The
8231 * sense code should be RESERVATIONS
8232 * RELEASED
8233 */
8234
8235 for (i = softc->init_min; i < softc->init_max; i++){
8236 if (ctl_get_prkey(lun, i) == 0)
8237 continue;
8238 ctl_est_ua(lun, i,
8239 CTL_UA_RES_RELEASE);
8240 }
8241 }
8242 lun->pr_res_type = 0;
8243 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8244 if (lun->pr_key_count==0) {
8245 lun->flags &= ~CTL_LUN_PR_RESERVED;
8246 lun->pr_res_type = 0;
8247 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8248 }
8249 }
8250 lun->pr_generation++;
8251 mtx_unlock(&lun->lun_lock);
8252
8253 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8254 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8255 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8256 persis_io.pr.pr_info.residx = residx;
8257 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8258 sizeof(persis_io.pr), M_WAITOK);
8259 } else /* sa_res_key != 0 */ {
8260
8261 /*
8262 * If we aren't registered currently then increment
8263 * the key count and set the registered flag.
8264 */
8265 ctl_alloc_prkey(lun, residx);
8266 if (ctl_get_prkey(lun, residx) == 0)
8267 lun->pr_key_count++;
8268 ctl_set_prkey(lun, residx, sa_res_key);
8269 lun->pr_generation++;
8270 mtx_unlock(&lun->lun_lock);
8271
8272 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8273 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8274 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8275 persis_io.pr.pr_info.residx = residx;
8276 memcpy(persis_io.pr.pr_info.sa_res_key,
8277 param->serv_act_res_key,
8278 sizeof(param->serv_act_res_key));
8279 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8280 sizeof(persis_io.pr), M_WAITOK);
8281 }
8282
8283 break;
8284 }
8285 case SPRO_RESERVE:
8286 mtx_lock(&lun->lun_lock);
8287 if (lun->flags & CTL_LUN_PR_RESERVED) {
8288 /*
8289 * if this isn't the reservation holder and it's
8290 * not a "all registrants" type or if the type is
8291 * different then we have a conflict
8292 */
8293 if ((lun->pr_res_idx != residx
8294 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8295 || lun->pr_res_type != type) {
8296 mtx_unlock(&lun->lun_lock);
8297 free(ctsio->kern_data_ptr, M_CTL);
8298 ctl_set_reservation_conflict(ctsio);
8299 ctl_done((union ctl_io *)ctsio);
8300 return (CTL_RETVAL_COMPLETE);
8301 }
8302 mtx_unlock(&lun->lun_lock);
8303 } else /* create a reservation */ {
8304 /*
8305 * If it's not an "all registrants" type record
8306 * reservation holder
8307 */
8308 if (type != SPR_TYPE_WR_EX_AR
8309 && type != SPR_TYPE_EX_AC_AR)
8310 lun->pr_res_idx = residx; /* Res holder */
8311 else
8312 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8313
8314 lun->flags |= CTL_LUN_PR_RESERVED;
8315 lun->pr_res_type = type;
8316
8317 mtx_unlock(&lun->lun_lock);
8318
8319 /* send msg to other side */
8320 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8321 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8322 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8323 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8324 persis_io.pr.pr_info.res_type = type;
8325 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8326 sizeof(persis_io.pr), M_WAITOK);
8327 }
8328 break;
8329
8330 case SPRO_RELEASE:
8331 mtx_lock(&lun->lun_lock);
8332 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8333 /* No reservation exists return good status */
8334 mtx_unlock(&lun->lun_lock);
8335 goto done;
8336 }
8337 /*
8338 * Is this nexus a reservation holder?
8339 */
8340 if (lun->pr_res_idx != residx
8341 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8342 /*
8343 * not a res holder return good status but
8344 * do nothing
8345 */
8346 mtx_unlock(&lun->lun_lock);
8347 goto done;
8348 }
8349
8350 if (lun->pr_res_type != type) {
8351 mtx_unlock(&lun->lun_lock);
8352 free(ctsio->kern_data_ptr, M_CTL);
8353 ctl_set_illegal_pr_release(ctsio);
8354 ctl_done((union ctl_io *)ctsio);
8355 return (CTL_RETVAL_COMPLETE);
8356 }
8357
8358 /* okay to release */
8359 lun->flags &= ~CTL_LUN_PR_RESERVED;
8360 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8361 lun->pr_res_type = 0;
8362
8363 /*
8364 * If this isn't an exclusive access reservation and NUAR
8365 * is not set, generate UA for all other registrants.
8366 */
8367 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX &&
8368 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8369 for (i = softc->init_min; i < softc->init_max; i++) {
8370 if (i == residx || ctl_get_prkey(lun, i) == 0)
8371 continue;
8372 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8373 }
8374 }
8375 mtx_unlock(&lun->lun_lock);
8376
8377 /* Send msg to other side */
8378 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8379 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8380 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8381 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8382 sizeof(persis_io.pr), M_WAITOK);
8383 break;
8384
8385 case SPRO_CLEAR:
8386 /* send msg to other side */
8387
8388 mtx_lock(&lun->lun_lock);
8389 lun->flags &= ~CTL_LUN_PR_RESERVED;
8390 lun->pr_res_type = 0;
8391 lun->pr_key_count = 0;
8392 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8393
8394 ctl_clr_prkey(lun, residx);
8395 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8396 if (ctl_get_prkey(lun, i) != 0) {
8397 ctl_clr_prkey(lun, i);
8398 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8399 }
8400 lun->pr_generation++;
8401 mtx_unlock(&lun->lun_lock);
8402
8403 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8404 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8405 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8406 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8407 sizeof(persis_io.pr), M_WAITOK);
8408 break;
8409
8410 case SPRO_PREEMPT:
8411 case SPRO_PRE_ABO: {
8412 int nretval;
8413
8414 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8415 residx, ctsio, cdb, param);
8416 if (nretval != 0)
8417 return (CTL_RETVAL_COMPLETE);
8418 break;
8419 }
8420 default:
8421 panic("%s: Invalid PR type %#x", __func__, cdb->action);
8422 }
8423
8424 done:
8425 free(ctsio->kern_data_ptr, M_CTL);
8426 ctl_set_success(ctsio);
8427 ctl_done((union ctl_io *)ctsio);
8428
8429 return (retval);
8430 }
8431
8432 /*
8433 * This routine is for handling a message from the other SC pertaining to
8434 * persistent reserve out. All the error checking will have been done
8435 * so only perorming the action need be done here to keep the two
8436 * in sync.
8437 */
8438 static void
ctl_hndl_per_res_out_on_other_sc(union ctl_io * io)8439 ctl_hndl_per_res_out_on_other_sc(union ctl_io *io)
8440 {
8441 struct ctl_softc *softc = CTL_SOFTC(io);
8442 union ctl_ha_msg *msg = (union ctl_ha_msg *)&io->presio.pr_msg;
8443 struct ctl_lun *lun;
8444 int i;
8445 uint32_t residx, targ_lun;
8446
8447 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8448 mtx_lock(&softc->ctl_lock);
8449 if (targ_lun >= ctl_max_luns ||
8450 (lun = softc->ctl_luns[targ_lun]) == NULL) {
8451 mtx_unlock(&softc->ctl_lock);
8452 return;
8453 }
8454 mtx_lock(&lun->lun_lock);
8455 mtx_unlock(&softc->ctl_lock);
8456 if (lun->flags & CTL_LUN_DISABLED) {
8457 mtx_unlock(&lun->lun_lock);
8458 return;
8459 }
8460 residx = ctl_get_initindex(&msg->hdr.nexus);
8461 switch(msg->pr.pr_info.action) {
8462 case CTL_PR_REG_KEY:
8463 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8464 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8465 lun->pr_key_count++;
8466 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8467 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8468 lun->pr_generation++;
8469 break;
8470
8471 case CTL_PR_UNREG_KEY:
8472 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8473 lun->pr_key_count--;
8474
8475 /* XXX Need to see if the reservation has been released */
8476 /* if so do we need to generate UA? */
8477 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8478 lun->flags &= ~CTL_LUN_PR_RESERVED;
8479 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8480
8481 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8482 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8483 lun->pr_key_count) {
8484 /*
8485 * If the reservation is a registrants
8486 * only type we need to generate a UA
8487 * for other registered inits. The
8488 * sense code should be RESERVATIONS
8489 * RELEASED
8490 */
8491
8492 for (i = softc->init_min; i < softc->init_max; i++) {
8493 if (ctl_get_prkey(lun, i) == 0)
8494 continue;
8495
8496 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8497 }
8498 }
8499 lun->pr_res_type = 0;
8500 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8501 if (lun->pr_key_count==0) {
8502 lun->flags &= ~CTL_LUN_PR_RESERVED;
8503 lun->pr_res_type = 0;
8504 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8505 }
8506 }
8507 lun->pr_generation++;
8508 break;
8509
8510 case CTL_PR_RESERVE:
8511 lun->flags |= CTL_LUN_PR_RESERVED;
8512 lun->pr_res_type = msg->pr.pr_info.res_type;
8513 lun->pr_res_idx = msg->pr.pr_info.residx;
8514
8515 break;
8516
8517 case CTL_PR_RELEASE:
8518 /*
8519 * If this isn't an exclusive access reservation and NUAR
8520 * is not set, generate UA for all other registrants.
8521 */
8522 if (lun->pr_res_type != SPR_TYPE_EX_AC &&
8523 lun->pr_res_type != SPR_TYPE_WR_EX &&
8524 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8525 for (i = softc->init_min; i < softc->init_max; i++) {
8526 if (i == residx || ctl_get_prkey(lun, i) == 0)
8527 continue;
8528 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8529 }
8530 }
8531
8532 lun->flags &= ~CTL_LUN_PR_RESERVED;
8533 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8534 lun->pr_res_type = 0;
8535 break;
8536
8537 case CTL_PR_PREEMPT:
8538 ctl_pro_preempt_other(lun, msg);
8539 break;
8540 case CTL_PR_CLEAR:
8541 lun->flags &= ~CTL_LUN_PR_RESERVED;
8542 lun->pr_res_type = 0;
8543 lun->pr_key_count = 0;
8544 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8545
8546 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8547 if (ctl_get_prkey(lun, i) == 0)
8548 continue;
8549 ctl_clr_prkey(lun, i);
8550 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8551 }
8552 lun->pr_generation++;
8553 break;
8554 }
8555
8556 mtx_unlock(&lun->lun_lock);
8557 }
8558
8559 int
ctl_read_write(struct ctl_scsiio * ctsio)8560 ctl_read_write(struct ctl_scsiio *ctsio)
8561 {
8562 struct ctl_lun *lun = CTL_LUN(ctsio);
8563 struct ctl_lba_len_flags *lbalen;
8564 uint64_t lba;
8565 uint32_t num_blocks;
8566 int flags, retval;
8567 int isread;
8568
8569 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8570
8571 flags = 0;
8572 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8573 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8574 switch (ctsio->cdb[0]) {
8575 case READ_6:
8576 case WRITE_6: {
8577 struct scsi_rw_6 *cdb;
8578
8579 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8580
8581 lba = scsi_3btoul(cdb->addr);
8582 /* only 5 bits are valid in the most significant address byte */
8583 lba &= 0x1fffff;
8584 num_blocks = cdb->length;
8585 /*
8586 * This is correct according to SBC-2.
8587 */
8588 if (num_blocks == 0)
8589 num_blocks = 256;
8590 break;
8591 }
8592 case READ_10:
8593 case WRITE_10: {
8594 struct scsi_rw_10 *cdb;
8595
8596 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8597 if (cdb->byte2 & SRW10_FUA)
8598 flags |= CTL_LLF_FUA;
8599 if (cdb->byte2 & SRW10_DPO)
8600 flags |= CTL_LLF_DPO;
8601 lba = scsi_4btoul(cdb->addr);
8602 num_blocks = scsi_2btoul(cdb->length);
8603 break;
8604 }
8605 case WRITE_VERIFY_10: {
8606 struct scsi_write_verify_10 *cdb;
8607
8608 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8609 flags |= CTL_LLF_FUA;
8610 if (cdb->byte2 & SWV_DPO)
8611 flags |= CTL_LLF_DPO;
8612 lba = scsi_4btoul(cdb->addr);
8613 num_blocks = scsi_2btoul(cdb->length);
8614 break;
8615 }
8616 case READ_12:
8617 case WRITE_12: {
8618 struct scsi_rw_12 *cdb;
8619
8620 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8621 if (cdb->byte2 & SRW12_FUA)
8622 flags |= CTL_LLF_FUA;
8623 if (cdb->byte2 & SRW12_DPO)
8624 flags |= CTL_LLF_DPO;
8625 lba = scsi_4btoul(cdb->addr);
8626 num_blocks = scsi_4btoul(cdb->length);
8627 break;
8628 }
8629 case WRITE_VERIFY_12: {
8630 struct scsi_write_verify_12 *cdb;
8631
8632 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8633 flags |= CTL_LLF_FUA;
8634 if (cdb->byte2 & SWV_DPO)
8635 flags |= CTL_LLF_DPO;
8636 lba = scsi_4btoul(cdb->addr);
8637 num_blocks = scsi_4btoul(cdb->length);
8638 break;
8639 }
8640 case READ_16:
8641 case WRITE_16: {
8642 struct scsi_rw_16 *cdb;
8643
8644 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8645 if (cdb->byte2 & SRW12_FUA)
8646 flags |= CTL_LLF_FUA;
8647 if (cdb->byte2 & SRW12_DPO)
8648 flags |= CTL_LLF_DPO;
8649 lba = scsi_8btou64(cdb->addr);
8650 num_blocks = scsi_4btoul(cdb->length);
8651 break;
8652 }
8653 case WRITE_ATOMIC_16: {
8654 struct scsi_write_atomic_16 *cdb;
8655
8656 if (lun->be_lun->atomicblock == 0) {
8657 ctl_set_invalid_opcode(ctsio);
8658 ctl_done((union ctl_io *)ctsio);
8659 return (CTL_RETVAL_COMPLETE);
8660 }
8661
8662 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb;
8663 if (cdb->byte2 & SRW12_FUA)
8664 flags |= CTL_LLF_FUA;
8665 if (cdb->byte2 & SRW12_DPO)
8666 flags |= CTL_LLF_DPO;
8667 lba = scsi_8btou64(cdb->addr);
8668 num_blocks = scsi_2btoul(cdb->length);
8669 if (num_blocks > lun->be_lun->atomicblock) {
8670 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8671 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8672 /*bit*/ 0);
8673 ctl_done((union ctl_io *)ctsio);
8674 return (CTL_RETVAL_COMPLETE);
8675 }
8676 break;
8677 }
8678 case WRITE_VERIFY_16: {
8679 struct scsi_write_verify_16 *cdb;
8680
8681 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8682 flags |= CTL_LLF_FUA;
8683 if (cdb->byte2 & SWV_DPO)
8684 flags |= CTL_LLF_DPO;
8685 lba = scsi_8btou64(cdb->addr);
8686 num_blocks = scsi_4btoul(cdb->length);
8687 break;
8688 }
8689 default:
8690 /*
8691 * We got a command we don't support. This shouldn't
8692 * happen, commands should be filtered out above us.
8693 */
8694 ctl_set_invalid_opcode(ctsio);
8695 ctl_done((union ctl_io *)ctsio);
8696
8697 return (CTL_RETVAL_COMPLETE);
8698 break; /* NOTREACHED */
8699 }
8700
8701 /*
8702 * The first check is to make sure we're in bounds, the second
8703 * check is to catch wrap-around problems. If the lba + num blocks
8704 * is less than the lba, then we've wrapped around and the block
8705 * range is invalid anyway.
8706 */
8707 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8708 || ((lba + num_blocks) < lba)) {
8709 ctl_set_lba_out_of_range(ctsio,
8710 MAX(lba, lun->be_lun->maxlba + 1));
8711 ctl_done((union ctl_io *)ctsio);
8712 return (CTL_RETVAL_COMPLETE);
8713 }
8714
8715 /*
8716 * According to SBC-3, a transfer length of 0 is not an error.
8717 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8718 * translates to 256 blocks for those commands.
8719 */
8720 if (num_blocks == 0) {
8721 ctl_set_success(ctsio);
8722 ctl_done((union ctl_io *)ctsio);
8723 return (CTL_RETVAL_COMPLETE);
8724 }
8725
8726 /* Set FUA and/or DPO if caches are disabled. */
8727 if (isread) {
8728 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0)
8729 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8730 } else {
8731 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8732 flags |= CTL_LLF_FUA;
8733 }
8734
8735 lbalen = (struct ctl_lba_len_flags *)
8736 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8737 lbalen->lba = lba;
8738 lbalen->len = num_blocks;
8739 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8740
8741 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8742 ctsio->kern_rel_offset = 0;
8743
8744 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8745
8746 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8747 return (retval);
8748 }
8749
8750 static int
ctl_cnw_cont(union ctl_io * io)8751 ctl_cnw_cont(union ctl_io *io)
8752 {
8753 struct ctl_lun *lun = CTL_LUN(io);
8754 struct ctl_scsiio *ctsio;
8755 struct ctl_lba_len_flags *lbalen;
8756 int retval;
8757
8758 ctsio = &io->scsiio;
8759 ctsio->io_hdr.status = CTL_STATUS_NONE;
8760 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8761 lbalen = (struct ctl_lba_len_flags *)
8762 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8763 lbalen->flags &= ~CTL_LLF_COMPARE;
8764 lbalen->flags |= CTL_LLF_WRITE;
8765
8766 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8767 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8768 return (retval);
8769 }
8770
8771 int
ctl_cnw(struct ctl_scsiio * ctsio)8772 ctl_cnw(struct ctl_scsiio *ctsio)
8773 {
8774 struct ctl_lun *lun = CTL_LUN(ctsio);
8775 struct ctl_lba_len_flags *lbalen;
8776 uint64_t lba;
8777 uint32_t num_blocks;
8778 int flags, retval;
8779
8780 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8781
8782 flags = 0;
8783 switch (ctsio->cdb[0]) {
8784 case COMPARE_AND_WRITE: {
8785 struct scsi_compare_and_write *cdb;
8786
8787 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8788 if (cdb->byte2 & SRW10_FUA)
8789 flags |= CTL_LLF_FUA;
8790 if (cdb->byte2 & SRW10_DPO)
8791 flags |= CTL_LLF_DPO;
8792 lba = scsi_8btou64(cdb->addr);
8793 num_blocks = cdb->length;
8794 break;
8795 }
8796 default:
8797 /*
8798 * We got a command we don't support. This shouldn't
8799 * happen, commands should be filtered out above us.
8800 */
8801 ctl_set_invalid_opcode(ctsio);
8802 ctl_done((union ctl_io *)ctsio);
8803
8804 return (CTL_RETVAL_COMPLETE);
8805 break; /* NOTREACHED */
8806 }
8807
8808 /*
8809 * The first check is to make sure we're in bounds, the second
8810 * check is to catch wrap-around problems. If the lba + num blocks
8811 * is less than the lba, then we've wrapped around and the block
8812 * range is invalid anyway.
8813 */
8814 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8815 || ((lba + num_blocks) < lba)) {
8816 ctl_set_lba_out_of_range(ctsio,
8817 MAX(lba, lun->be_lun->maxlba + 1));
8818 ctl_done((union ctl_io *)ctsio);
8819 return (CTL_RETVAL_COMPLETE);
8820 }
8821
8822 /*
8823 * According to SBC-3, a transfer length of 0 is not an error.
8824 */
8825 if (num_blocks == 0) {
8826 ctl_set_success(ctsio);
8827 ctl_done((union ctl_io *)ctsio);
8828 return (CTL_RETVAL_COMPLETE);
8829 }
8830
8831 /* Set FUA if write cache is disabled. */
8832 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8833 flags |= CTL_LLF_FUA;
8834
8835 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8836 ctsio->kern_rel_offset = 0;
8837
8838 /*
8839 * Set the IO_CONT flag, so that if this I/O gets passed to
8840 * ctl_data_submit_done(), it'll get passed back to
8841 * ctl_ctl_cnw_cont() for further processing.
8842 */
8843 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8844 ctsio->io_cont = ctl_cnw_cont;
8845
8846 lbalen = (struct ctl_lba_len_flags *)
8847 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8848 lbalen->lba = lba;
8849 lbalen->len = num_blocks;
8850 lbalen->flags = CTL_LLF_COMPARE | flags;
8851
8852 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8853 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8854 return (retval);
8855 }
8856
8857 int
ctl_verify(struct ctl_scsiio * ctsio)8858 ctl_verify(struct ctl_scsiio *ctsio)
8859 {
8860 struct ctl_lun *lun = CTL_LUN(ctsio);
8861 struct ctl_lba_len_flags *lbalen;
8862 uint64_t lba;
8863 uint32_t num_blocks;
8864 int bytchk, flags;
8865 int retval;
8866
8867 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
8868
8869 bytchk = 0;
8870 flags = CTL_LLF_FUA;
8871 switch (ctsio->cdb[0]) {
8872 case VERIFY_10: {
8873 struct scsi_verify_10 *cdb;
8874
8875 cdb = (struct scsi_verify_10 *)ctsio->cdb;
8876 if (cdb->byte2 & SVFY_BYTCHK)
8877 bytchk = 1;
8878 if (cdb->byte2 & SVFY_DPO)
8879 flags |= CTL_LLF_DPO;
8880 lba = scsi_4btoul(cdb->addr);
8881 num_blocks = scsi_2btoul(cdb->length);
8882 break;
8883 }
8884 case VERIFY_12: {
8885 struct scsi_verify_12 *cdb;
8886
8887 cdb = (struct scsi_verify_12 *)ctsio->cdb;
8888 if (cdb->byte2 & SVFY_BYTCHK)
8889 bytchk = 1;
8890 if (cdb->byte2 & SVFY_DPO)
8891 flags |= CTL_LLF_DPO;
8892 lba = scsi_4btoul(cdb->addr);
8893 num_blocks = scsi_4btoul(cdb->length);
8894 break;
8895 }
8896 case VERIFY_16: {
8897 struct scsi_rw_16 *cdb;
8898
8899 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8900 if (cdb->byte2 & SVFY_BYTCHK)
8901 bytchk = 1;
8902 if (cdb->byte2 & SVFY_DPO)
8903 flags |= CTL_LLF_DPO;
8904 lba = scsi_8btou64(cdb->addr);
8905 num_blocks = scsi_4btoul(cdb->length);
8906 break;
8907 }
8908 default:
8909 /*
8910 * We got a command we don't support. This shouldn't
8911 * happen, commands should be filtered out above us.
8912 */
8913 ctl_set_invalid_opcode(ctsio);
8914 ctl_done((union ctl_io *)ctsio);
8915 return (CTL_RETVAL_COMPLETE);
8916 }
8917
8918 /*
8919 * The first check is to make sure we're in bounds, the second
8920 * check is to catch wrap-around problems. If the lba + num blocks
8921 * is less than the lba, then we've wrapped around and the block
8922 * range is invalid anyway.
8923 */
8924 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8925 || ((lba + num_blocks) < lba)) {
8926 ctl_set_lba_out_of_range(ctsio,
8927 MAX(lba, lun->be_lun->maxlba + 1));
8928 ctl_done((union ctl_io *)ctsio);
8929 return (CTL_RETVAL_COMPLETE);
8930 }
8931
8932 /*
8933 * According to SBC-3, a transfer length of 0 is not an error.
8934 */
8935 if (num_blocks == 0) {
8936 ctl_set_success(ctsio);
8937 ctl_done((union ctl_io *)ctsio);
8938 return (CTL_RETVAL_COMPLETE);
8939 }
8940
8941 lbalen = (struct ctl_lba_len_flags *)
8942 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8943 lbalen->lba = lba;
8944 lbalen->len = num_blocks;
8945 if (bytchk) {
8946 lbalen->flags = CTL_LLF_COMPARE | flags;
8947 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8948 } else {
8949 lbalen->flags = CTL_LLF_VERIFY | flags;
8950 ctsio->kern_total_len = 0;
8951 }
8952 ctsio->kern_rel_offset = 0;
8953
8954 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
8955 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8956 return (retval);
8957 }
8958
8959 int
ctl_report_luns(struct ctl_scsiio * ctsio)8960 ctl_report_luns(struct ctl_scsiio *ctsio)
8961 {
8962 struct ctl_softc *softc = CTL_SOFTC(ctsio);
8963 struct ctl_port *port = CTL_PORT(ctsio);
8964 struct ctl_lun *lun, *request_lun = CTL_LUN(ctsio);
8965 struct scsi_report_luns *cdb;
8966 struct scsi_report_luns_data *lun_data;
8967 int num_filled, num_luns, num_port_luns, retval;
8968 uint32_t alloc_len, lun_datalen;
8969 uint32_t initidx, targ_lun_id, lun_id;
8970
8971 retval = CTL_RETVAL_COMPLETE;
8972 cdb = (struct scsi_report_luns *)ctsio->cdb;
8973
8974 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
8975
8976 num_luns = 0;
8977 num_port_luns = port->lun_map ? port->lun_map_size : ctl_max_luns;
8978 mtx_lock(&softc->ctl_lock);
8979 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) {
8980 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX)
8981 num_luns++;
8982 }
8983 mtx_unlock(&softc->ctl_lock);
8984
8985 switch (cdb->select_report) {
8986 case RPL_REPORT_DEFAULT:
8987 case RPL_REPORT_ALL:
8988 case RPL_REPORT_NONSUBSID:
8989 break;
8990 case RPL_REPORT_WELLKNOWN:
8991 case RPL_REPORT_ADMIN:
8992 case RPL_REPORT_CONGLOM:
8993 num_luns = 0;
8994 break;
8995 default:
8996 ctl_set_invalid_field(ctsio,
8997 /*sks_valid*/ 1,
8998 /*command*/ 1,
8999 /*field*/ 2,
9000 /*bit_valid*/ 0,
9001 /*bit*/ 0);
9002 ctl_done((union ctl_io *)ctsio);
9003 return (retval);
9004 break; /* NOTREACHED */
9005 }
9006
9007 alloc_len = scsi_4btoul(cdb->length);
9008 /*
9009 * The initiator has to allocate at least 16 bytes for this request,
9010 * so he can at least get the header and the first LUN. Otherwise
9011 * we reject the request (per SPC-3 rev 14, section 6.21).
9012 */
9013 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9014 sizeof(struct scsi_report_luns_lundata))) {
9015 ctl_set_invalid_field(ctsio,
9016 /*sks_valid*/ 1,
9017 /*command*/ 1,
9018 /*field*/ 6,
9019 /*bit_valid*/ 0,
9020 /*bit*/ 0);
9021 ctl_done((union ctl_io *)ctsio);
9022 return (retval);
9023 }
9024
9025 lun_datalen = sizeof(*lun_data) +
9026 (num_luns * sizeof(struct scsi_report_luns_lundata));
9027
9028 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9029 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9030 ctsio->kern_sg_entries = 0;
9031
9032 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9033
9034 mtx_lock(&softc->ctl_lock);
9035 for (targ_lun_id = 0, num_filled = 0;
9036 targ_lun_id < num_port_luns && num_filled < num_luns;
9037 targ_lun_id++) {
9038 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9039 if (lun_id == UINT32_MAX)
9040 continue;
9041 lun = softc->ctl_luns[lun_id];
9042 if (lun == NULL)
9043 continue;
9044
9045 be64enc(lun_data->luns[num_filled++].lundata,
9046 ctl_encode_lun(targ_lun_id));
9047
9048 /*
9049 * According to SPC-3, rev 14 section 6.21:
9050 *
9051 * "The execution of a REPORT LUNS command to any valid and
9052 * installed logical unit shall clear the REPORTED LUNS DATA
9053 * HAS CHANGED unit attention condition for all logical
9054 * units of that target with respect to the requesting
9055 * initiator. A valid and installed logical unit is one
9056 * having a PERIPHERAL QUALIFIER of 000b in the standard
9057 * INQUIRY data (see 6.4.2)."
9058 *
9059 * If request_lun is NULL, the LUN this report luns command
9060 * was issued to is either disabled or doesn't exist. In that
9061 * case, we shouldn't clear any pending lun change unit
9062 * attention.
9063 */
9064 if (request_lun != NULL) {
9065 mtx_lock(&lun->lun_lock);
9066 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9067 mtx_unlock(&lun->lun_lock);
9068 }
9069 }
9070 mtx_unlock(&softc->ctl_lock);
9071
9072 /*
9073 * It's quite possible that we've returned fewer LUNs than we allocated
9074 * space for. Trim it.
9075 */
9076 lun_datalen = sizeof(*lun_data) +
9077 (num_filled * sizeof(struct scsi_report_luns_lundata));
9078 ctsio->kern_rel_offset = 0;
9079 ctsio->kern_sg_entries = 0;
9080 ctsio->kern_data_len = min(lun_datalen, alloc_len);
9081 ctsio->kern_total_len = ctsio->kern_data_len;
9082
9083 /*
9084 * We set this to the actual data length, regardless of how much
9085 * space we actually have to return results. If the user looks at
9086 * this value, he'll know whether or not he allocated enough space
9087 * and reissue the command if necessary. We don't support well
9088 * known logical units, so if the user asks for that, return none.
9089 */
9090 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9091
9092 /*
9093 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9094 * this request.
9095 */
9096 ctl_set_success(ctsio);
9097 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9098 ctsio->be_move_done = ctl_config_move_done;
9099 ctl_datamove((union ctl_io *)ctsio);
9100 return (retval);
9101 }
9102
9103 int
ctl_request_sense(struct ctl_scsiio * ctsio)9104 ctl_request_sense(struct ctl_scsiio *ctsio)
9105 {
9106 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9107 struct ctl_lun *lun = CTL_LUN(ctsio);
9108 struct scsi_request_sense *cdb;
9109 struct scsi_sense_data *sense_ptr, *ps;
9110 uint32_t initidx;
9111 int have_error;
9112 u_int sense_len = SSD_FULL_SIZE;
9113 scsi_sense_data_type sense_format;
9114 ctl_ua_type ua_type;
9115 uint8_t asc = 0, ascq = 0;
9116
9117 cdb = (struct scsi_request_sense *)ctsio->cdb;
9118
9119 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9120
9121 /*
9122 * Determine which sense format the user wants.
9123 */
9124 if (cdb->byte2 & SRS_DESC)
9125 sense_format = SSD_TYPE_DESC;
9126 else
9127 sense_format = SSD_TYPE_FIXED;
9128
9129 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9130 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9131 ctsio->kern_sg_entries = 0;
9132 ctsio->kern_rel_offset = 0;
9133
9134 /*
9135 * struct scsi_sense_data, which is currently set to 256 bytes, is
9136 * larger than the largest allowed value for the length field in the
9137 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9138 */
9139 ctsio->kern_data_len = cdb->length;
9140 ctsio->kern_total_len = cdb->length;
9141
9142 /*
9143 * If we don't have a LUN, we don't have any pending sense.
9144 */
9145 if (lun == NULL ||
9146 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
9147 softc->ha_link < CTL_HA_LINK_UNKNOWN)) {
9148 /* "Logical unit not supported" */
9149 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format,
9150 /*current_error*/ 1,
9151 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
9152 /*asc*/ 0x25,
9153 /*ascq*/ 0x00,
9154 SSD_ELEM_NONE);
9155 goto send;
9156 }
9157
9158 have_error = 0;
9159 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9160 /*
9161 * Check for pending sense, and then for pending unit attentions.
9162 * Pending sense gets returned first, then pending unit attentions.
9163 */
9164 mtx_lock(&lun->lun_lock);
9165 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
9166 if (ps != NULL)
9167 ps += initidx % CTL_MAX_INIT_PER_PORT;
9168 if (ps != NULL && ps->error_code != 0) {
9169 scsi_sense_data_type stored_format;
9170
9171 /*
9172 * Check to see which sense format was used for the stored
9173 * sense data.
9174 */
9175 stored_format = scsi_sense_type(ps);
9176
9177 /*
9178 * If the user requested a different sense format than the
9179 * one we stored, then we need to convert it to the other
9180 * format. If we're going from descriptor to fixed format
9181 * sense data, we may lose things in translation, depending
9182 * on what options were used.
9183 *
9184 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9185 * for some reason we'll just copy it out as-is.
9186 */
9187 if ((stored_format == SSD_TYPE_FIXED)
9188 && (sense_format == SSD_TYPE_DESC))
9189 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9190 ps, (struct scsi_sense_data_desc *)sense_ptr);
9191 else if ((stored_format == SSD_TYPE_DESC)
9192 && (sense_format == SSD_TYPE_FIXED))
9193 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9194 ps, (struct scsi_sense_data_fixed *)sense_ptr);
9195 else
9196 memcpy(sense_ptr, ps, sizeof(*sense_ptr));
9197
9198 ps->error_code = 0;
9199 have_error = 1;
9200 } else {
9201 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len,
9202 sense_format);
9203 if (ua_type != CTL_UA_NONE)
9204 have_error = 1;
9205 }
9206 if (have_error == 0) {
9207 /*
9208 * Report informational exception if have one and allowed.
9209 */
9210 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) {
9211 asc = lun->ie_asc;
9212 ascq = lun->ie_ascq;
9213 }
9214 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format,
9215 /*current_error*/ 1,
9216 /*sense_key*/ SSD_KEY_NO_SENSE,
9217 /*asc*/ asc,
9218 /*ascq*/ ascq,
9219 SSD_ELEM_NONE);
9220 }
9221 mtx_unlock(&lun->lun_lock);
9222
9223 send:
9224 /*
9225 * We report the SCSI status as OK, since the status of the command
9226 * itself is OK. We're reporting sense as parameter data.
9227 */
9228 ctl_set_success(ctsio);
9229 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9230 ctsio->be_move_done = ctl_config_move_done;
9231 ctl_datamove((union ctl_io *)ctsio);
9232 return (CTL_RETVAL_COMPLETE);
9233 }
9234
9235 int
ctl_tur(struct ctl_scsiio * ctsio)9236 ctl_tur(struct ctl_scsiio *ctsio)
9237 {
9238
9239 CTL_DEBUG_PRINT(("ctl_tur\n"));
9240
9241 ctl_set_success(ctsio);
9242 ctl_done((union ctl_io *)ctsio);
9243
9244 return (CTL_RETVAL_COMPLETE);
9245 }
9246
9247 /*
9248 * SCSI VPD page 0x00, the Supported VPD Pages page.
9249 */
9250 static int
ctl_inquiry_evpd_supported(struct ctl_scsiio * ctsio,int alloc_len)9251 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9252 {
9253 struct ctl_lun *lun = CTL_LUN(ctsio);
9254 struct scsi_vpd_supported_pages *pages;
9255 int sup_page_size;
9256 int p;
9257
9258 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9259 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9260 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9261 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9262 ctsio->kern_rel_offset = 0;
9263 ctsio->kern_sg_entries = 0;
9264 ctsio->kern_data_len = min(sup_page_size, alloc_len);
9265 ctsio->kern_total_len = ctsio->kern_data_len;
9266
9267 /*
9268 * The control device is always connected. The disk device, on the
9269 * other hand, may not be online all the time. Need to change this
9270 * to figure out whether the disk device is actually online or not.
9271 */
9272 if (lun != NULL)
9273 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9274 lun->be_lun->lun_type;
9275 else
9276 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9277
9278 p = 0;
9279 /* Supported VPD pages */
9280 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9281 /* Serial Number */
9282 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9283 /* Device Identification */
9284 pages->page_list[p++] = SVPD_DEVICE_ID;
9285 /* Extended INQUIRY Data */
9286 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9287 /* Mode Page Policy */
9288 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9289 /* SCSI Ports */
9290 pages->page_list[p++] = SVPD_SCSI_PORTS;
9291 /* Third-party Copy */
9292 pages->page_list[p++] = SVPD_SCSI_TPC;
9293 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9294 /* Block limits */
9295 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9296 /* Block Device Characteristics */
9297 pages->page_list[p++] = SVPD_BDC;
9298 /* Logical Block Provisioning */
9299 pages->page_list[p++] = SVPD_LBP;
9300 }
9301 pages->length = p;
9302
9303 ctl_set_success(ctsio);
9304 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9305 ctsio->be_move_done = ctl_config_move_done;
9306 ctl_datamove((union ctl_io *)ctsio);
9307 return (CTL_RETVAL_COMPLETE);
9308 }
9309
9310 /*
9311 * SCSI VPD page 0x80, the Unit Serial Number page.
9312 */
9313 static int
ctl_inquiry_evpd_serial(struct ctl_scsiio * ctsio,int alloc_len)9314 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9315 {
9316 struct ctl_lun *lun = CTL_LUN(ctsio);
9317 struct scsi_vpd_unit_serial_number *sn_ptr;
9318 int data_len;
9319
9320 data_len = 4 + CTL_SN_LEN;
9321 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9322 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9323 ctsio->kern_rel_offset = 0;
9324 ctsio->kern_sg_entries = 0;
9325 ctsio->kern_data_len = min(data_len, alloc_len);
9326 ctsio->kern_total_len = ctsio->kern_data_len;
9327
9328 /*
9329 * The control device is always connected. The disk device, on the
9330 * other hand, may not be online all the time. Need to change this
9331 * to figure out whether the disk device is actually online or not.
9332 */
9333 if (lun != NULL)
9334 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9335 lun->be_lun->lun_type;
9336 else
9337 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9338
9339 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9340 sn_ptr->length = CTL_SN_LEN;
9341 /*
9342 * If we don't have a LUN, we just leave the serial number as
9343 * all spaces.
9344 */
9345 if (lun != NULL) {
9346 strncpy((char *)sn_ptr->serial_num,
9347 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9348 } else
9349 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9350
9351 ctl_set_success(ctsio);
9352 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9353 ctsio->be_move_done = ctl_config_move_done;
9354 ctl_datamove((union ctl_io *)ctsio);
9355 return (CTL_RETVAL_COMPLETE);
9356 }
9357
9358
9359 /*
9360 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9361 */
9362 static int
ctl_inquiry_evpd_eid(struct ctl_scsiio * ctsio,int alloc_len)9363 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9364 {
9365 struct ctl_lun *lun = CTL_LUN(ctsio);
9366 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9367 int data_len;
9368
9369 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9370 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9371 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9372 ctsio->kern_sg_entries = 0;
9373 ctsio->kern_rel_offset = 0;
9374 ctsio->kern_data_len = min(data_len, alloc_len);
9375 ctsio->kern_total_len = ctsio->kern_data_len;
9376
9377 /*
9378 * The control device is always connected. The disk device, on the
9379 * other hand, may not be online all the time.
9380 */
9381 if (lun != NULL)
9382 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9383 lun->be_lun->lun_type;
9384 else
9385 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9386 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9387 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9388 /*
9389 * We support head of queue, ordered and simple tags.
9390 */
9391 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9392 /*
9393 * Volatile cache supported.
9394 */
9395 eid_ptr->flags3 = SVPD_EID_V_SUP;
9396
9397 /*
9398 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9399 * attention for a particular IT nexus on all LUNs once we report
9400 * it to that nexus once. This bit is required as of SPC-4.
9401 */
9402 eid_ptr->flags4 = SVPD_EID_LUICLR;
9403
9404 /*
9405 * We support revert to defaults (RTD) bit in MODE SELECT.
9406 */
9407 eid_ptr->flags5 = SVPD_EID_RTD_SUP;
9408
9409 /*
9410 * XXX KDM in order to correctly answer this, we would need
9411 * information from the SIM to determine how much sense data it
9412 * can send. So this would really be a path inquiry field, most
9413 * likely. This can be set to a maximum of 252 according to SPC-4,
9414 * but the hardware may or may not be able to support that much.
9415 * 0 just means that the maximum sense data length is not reported.
9416 */
9417 eid_ptr->max_sense_length = 0;
9418
9419 ctl_set_success(ctsio);
9420 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9421 ctsio->be_move_done = ctl_config_move_done;
9422 ctl_datamove((union ctl_io *)ctsio);
9423 return (CTL_RETVAL_COMPLETE);
9424 }
9425
9426 static int
ctl_inquiry_evpd_mpp(struct ctl_scsiio * ctsio,int alloc_len)9427 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9428 {
9429 struct ctl_lun *lun = CTL_LUN(ctsio);
9430 struct scsi_vpd_mode_page_policy *mpp_ptr;
9431 int data_len;
9432
9433 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9434 sizeof(struct scsi_vpd_mode_page_policy_descr);
9435
9436 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9437 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9438 ctsio->kern_rel_offset = 0;
9439 ctsio->kern_sg_entries = 0;
9440 ctsio->kern_data_len = min(data_len, alloc_len);
9441 ctsio->kern_total_len = ctsio->kern_data_len;
9442
9443 /*
9444 * The control device is always connected. The disk device, on the
9445 * other hand, may not be online all the time.
9446 */
9447 if (lun != NULL)
9448 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9449 lun->be_lun->lun_type;
9450 else
9451 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9452 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9453 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9454 mpp_ptr->descr[0].page_code = 0x3f;
9455 mpp_ptr->descr[0].subpage_code = 0xff;
9456 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9457
9458 ctl_set_success(ctsio);
9459 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9460 ctsio->be_move_done = ctl_config_move_done;
9461 ctl_datamove((union ctl_io *)ctsio);
9462 return (CTL_RETVAL_COMPLETE);
9463 }
9464
9465 /*
9466 * SCSI VPD page 0x83, the Device Identification page.
9467 */
9468 static int
ctl_inquiry_evpd_devid(struct ctl_scsiio * ctsio,int alloc_len)9469 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9470 {
9471 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9472 struct ctl_port *port = CTL_PORT(ctsio);
9473 struct ctl_lun *lun = CTL_LUN(ctsio);
9474 struct scsi_vpd_device_id *devid_ptr;
9475 struct scsi_vpd_id_descriptor *desc;
9476 int data_len, g;
9477 uint8_t proto;
9478
9479 data_len = sizeof(struct scsi_vpd_device_id) +
9480 sizeof(struct scsi_vpd_id_descriptor) +
9481 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9482 sizeof(struct scsi_vpd_id_descriptor) +
9483 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9484 if (lun && lun->lun_devid)
9485 data_len += lun->lun_devid->len;
9486 if (port && port->port_devid)
9487 data_len += port->port_devid->len;
9488 if (port && port->target_devid)
9489 data_len += port->target_devid->len;
9490
9491 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9492 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9493 ctsio->kern_sg_entries = 0;
9494 ctsio->kern_rel_offset = 0;
9495 ctsio->kern_sg_entries = 0;
9496 ctsio->kern_data_len = min(data_len, alloc_len);
9497 ctsio->kern_total_len = ctsio->kern_data_len;
9498
9499 /*
9500 * The control device is always connected. The disk device, on the
9501 * other hand, may not be online all the time.
9502 */
9503 if (lun != NULL)
9504 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9505 lun->be_lun->lun_type;
9506 else
9507 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9508 devid_ptr->page_code = SVPD_DEVICE_ID;
9509 scsi_ulto2b(data_len - 4, devid_ptr->length);
9510
9511 if (port && port->port_type == CTL_PORT_FC)
9512 proto = SCSI_PROTO_FC << 4;
9513 else if (port && port->port_type == CTL_PORT_SAS)
9514 proto = SCSI_PROTO_SAS << 4;
9515 else if (port && port->port_type == CTL_PORT_ISCSI)
9516 proto = SCSI_PROTO_ISCSI << 4;
9517 else
9518 proto = SCSI_PROTO_SPI << 4;
9519 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9520
9521 /*
9522 * We're using a LUN association here. i.e., this device ID is a
9523 * per-LUN identifier.
9524 */
9525 if (lun && lun->lun_devid) {
9526 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9527 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9528 lun->lun_devid->len);
9529 }
9530
9531 /*
9532 * This is for the WWPN which is a port association.
9533 */
9534 if (port && port->port_devid) {
9535 memcpy(desc, port->port_devid->data, port->port_devid->len);
9536 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9537 port->port_devid->len);
9538 }
9539
9540 /*
9541 * This is for the Relative Target Port(type 4h) identifier
9542 */
9543 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9544 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9545 SVPD_ID_TYPE_RELTARG;
9546 desc->length = 4;
9547 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9548 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9549 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9550
9551 /*
9552 * This is for the Target Port Group(type 5h) identifier
9553 */
9554 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9555 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9556 SVPD_ID_TYPE_TPORTGRP;
9557 desc->length = 4;
9558 if (softc->is_single ||
9559 (port && port->status & CTL_PORT_STATUS_HA_SHARED))
9560 g = 1;
9561 else
9562 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt;
9563 scsi_ulto2b(g, &desc->identifier[2]);
9564 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9565 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9566
9567 /*
9568 * This is for the Target identifier
9569 */
9570 if (port && port->target_devid) {
9571 memcpy(desc, port->target_devid->data, port->target_devid->len);
9572 }
9573
9574 ctl_set_success(ctsio);
9575 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9576 ctsio->be_move_done = ctl_config_move_done;
9577 ctl_datamove((union ctl_io *)ctsio);
9578 return (CTL_RETVAL_COMPLETE);
9579 }
9580
9581 static int
ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio * ctsio,int alloc_len)9582 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9583 {
9584 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9585 struct ctl_lun *lun = CTL_LUN(ctsio);
9586 struct scsi_vpd_scsi_ports *sp;
9587 struct scsi_vpd_port_designation *pd;
9588 struct scsi_vpd_port_designation_cont *pdc;
9589 struct ctl_port *port;
9590 int data_len, num_target_ports, iid_len, id_len;
9591
9592 num_target_ports = 0;
9593 iid_len = 0;
9594 id_len = 0;
9595 mtx_lock(&softc->ctl_lock);
9596 STAILQ_FOREACH(port, &softc->port_list, links) {
9597 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9598 continue;
9599 if (lun != NULL &&
9600 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9601 continue;
9602 num_target_ports++;
9603 if (port->init_devid)
9604 iid_len += port->init_devid->len;
9605 if (port->port_devid)
9606 id_len += port->port_devid->len;
9607 }
9608 mtx_unlock(&softc->ctl_lock);
9609
9610 data_len = sizeof(struct scsi_vpd_scsi_ports) +
9611 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9612 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9613 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9614 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9615 ctsio->kern_sg_entries = 0;
9616 ctsio->kern_rel_offset = 0;
9617 ctsio->kern_sg_entries = 0;
9618 ctsio->kern_data_len = min(data_len, alloc_len);
9619 ctsio->kern_total_len = ctsio->kern_data_len;
9620
9621 /*
9622 * The control device is always connected. The disk device, on the
9623 * other hand, may not be online all the time. Need to change this
9624 * to figure out whether the disk device is actually online or not.
9625 */
9626 if (lun != NULL)
9627 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9628 lun->be_lun->lun_type;
9629 else
9630 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9631
9632 sp->page_code = SVPD_SCSI_PORTS;
9633 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9634 sp->page_length);
9635 pd = &sp->design[0];
9636
9637 mtx_lock(&softc->ctl_lock);
9638 STAILQ_FOREACH(port, &softc->port_list, links) {
9639 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9640 continue;
9641 if (lun != NULL &&
9642 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9643 continue;
9644 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9645 if (port->init_devid) {
9646 iid_len = port->init_devid->len;
9647 memcpy(pd->initiator_transportid,
9648 port->init_devid->data, port->init_devid->len);
9649 } else
9650 iid_len = 0;
9651 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9652 pdc = (struct scsi_vpd_port_designation_cont *)
9653 (&pd->initiator_transportid[iid_len]);
9654 if (port->port_devid) {
9655 id_len = port->port_devid->len;
9656 memcpy(pdc->target_port_descriptors,
9657 port->port_devid->data, port->port_devid->len);
9658 } else
9659 id_len = 0;
9660 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9661 pd = (struct scsi_vpd_port_designation *)
9662 ((uint8_t *)pdc->target_port_descriptors + id_len);
9663 }
9664 mtx_unlock(&softc->ctl_lock);
9665
9666 ctl_set_success(ctsio);
9667 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9668 ctsio->be_move_done = ctl_config_move_done;
9669 ctl_datamove((union ctl_io *)ctsio);
9670 return (CTL_RETVAL_COMPLETE);
9671 }
9672
9673 static int
ctl_inquiry_evpd_block_limits(struct ctl_scsiio * ctsio,int alloc_len)9674 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9675 {
9676 struct ctl_lun *lun = CTL_LUN(ctsio);
9677 struct scsi_vpd_block_limits *bl_ptr;
9678 uint64_t ival;
9679
9680 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9681 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9682 ctsio->kern_sg_entries = 0;
9683 ctsio->kern_rel_offset = 0;
9684 ctsio->kern_sg_entries = 0;
9685 ctsio->kern_data_len = min(sizeof(*bl_ptr), alloc_len);
9686 ctsio->kern_total_len = ctsio->kern_data_len;
9687
9688 /*
9689 * The control device is always connected. The disk device, on the
9690 * other hand, may not be online all the time. Need to change this
9691 * to figure out whether the disk device is actually online or not.
9692 */
9693 if (lun != NULL)
9694 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9695 lun->be_lun->lun_type;
9696 else
9697 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9698
9699 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9700 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9701 bl_ptr->max_cmp_write_len = 0xff;
9702 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9703 if (lun != NULL) {
9704 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9705 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9706 ival = 0xffffffff;
9707 ctl_get_opt_number(&lun->be_lun->options,
9708 "unmap_max_lba", &ival);
9709 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt);
9710 ival = 0xffffffff;
9711 ctl_get_opt_number(&lun->be_lun->options,
9712 "unmap_max_descr", &ival);
9713 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt);
9714 if (lun->be_lun->ublockexp != 0) {
9715 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9716 bl_ptr->opt_unmap_grain);
9717 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9718 bl_ptr->unmap_grain_align);
9719 }
9720 }
9721 scsi_ulto4b(lun->be_lun->atomicblock,
9722 bl_ptr->max_atomic_transfer_length);
9723 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9724 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9725 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary);
9726 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size);
9727 ival = UINT64_MAX;
9728 ctl_get_opt_number(&lun->be_lun->options, "write_same_max_lba", &ival);
9729 scsi_u64to8b(ival, bl_ptr->max_write_same_length);
9730 }
9731
9732 ctl_set_success(ctsio);
9733 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9734 ctsio->be_move_done = ctl_config_move_done;
9735 ctl_datamove((union ctl_io *)ctsio);
9736 return (CTL_RETVAL_COMPLETE);
9737 }
9738
9739 static int
ctl_inquiry_evpd_bdc(struct ctl_scsiio * ctsio,int alloc_len)9740 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
9741 {
9742 struct ctl_lun *lun = CTL_LUN(ctsio);
9743 struct scsi_vpd_block_device_characteristics *bdc_ptr;
9744 const char *value;
9745 u_int i;
9746
9747 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
9748 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
9749 ctsio->kern_sg_entries = 0;
9750 ctsio->kern_rel_offset = 0;
9751 ctsio->kern_data_len = min(sizeof(*bdc_ptr), alloc_len);
9752 ctsio->kern_total_len = ctsio->kern_data_len;
9753
9754 /*
9755 * The control device is always connected. The disk device, on the
9756 * other hand, may not be online all the time. Need to change this
9757 * to figure out whether the disk device is actually online or not.
9758 */
9759 if (lun != NULL)
9760 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9761 lun->be_lun->lun_type;
9762 else
9763 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9764 bdc_ptr->page_code = SVPD_BDC;
9765 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
9766 if (lun != NULL &&
9767 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
9768 i = strtol(value, NULL, 0);
9769 else
9770 i = CTL_DEFAULT_ROTATION_RATE;
9771 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
9772 if (lun != NULL &&
9773 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
9774 i = strtol(value, NULL, 0);
9775 else
9776 i = 0;
9777 bdc_ptr->wab_wac_ff = (i & 0x0f);
9778 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
9779
9780 ctl_set_success(ctsio);
9781 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9782 ctsio->be_move_done = ctl_config_move_done;
9783 ctl_datamove((union ctl_io *)ctsio);
9784 return (CTL_RETVAL_COMPLETE);
9785 }
9786
9787 static int
ctl_inquiry_evpd_lbp(struct ctl_scsiio * ctsio,int alloc_len)9788 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9789 {
9790 struct ctl_lun *lun = CTL_LUN(ctsio);
9791 struct scsi_vpd_logical_block_prov *lbp_ptr;
9792 const char *value;
9793
9794 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
9795 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
9796 ctsio->kern_sg_entries = 0;
9797 ctsio->kern_rel_offset = 0;
9798 ctsio->kern_data_len = min(sizeof(*lbp_ptr), alloc_len);
9799 ctsio->kern_total_len = ctsio->kern_data_len;
9800
9801 /*
9802 * The control device is always connected. The disk device, on the
9803 * other hand, may not be online all the time. Need to change this
9804 * to figure out whether the disk device is actually online or not.
9805 */
9806 if (lun != NULL)
9807 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9808 lun->be_lun->lun_type;
9809 else
9810 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9811
9812 lbp_ptr->page_code = SVPD_LBP;
9813 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
9814 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
9815 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9816 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
9817 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
9818 value = ctl_get_opt(&lun->be_lun->options, "provisioning_type");
9819 if (value != NULL) {
9820 if (strcmp(value, "resource") == 0)
9821 lbp_ptr->prov_type = SVPD_LBP_RESOURCE;
9822 else if (strcmp(value, "thin") == 0)
9823 lbp_ptr->prov_type = SVPD_LBP_THIN;
9824 } else
9825 lbp_ptr->prov_type = SVPD_LBP_THIN;
9826 }
9827
9828 ctl_set_success(ctsio);
9829 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9830 ctsio->be_move_done = ctl_config_move_done;
9831 ctl_datamove((union ctl_io *)ctsio);
9832 return (CTL_RETVAL_COMPLETE);
9833 }
9834
9835 /*
9836 * INQUIRY with the EVPD bit set.
9837 */
9838 static int
ctl_inquiry_evpd(struct ctl_scsiio * ctsio)9839 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
9840 {
9841 struct ctl_lun *lun = CTL_LUN(ctsio);
9842 struct scsi_inquiry *cdb;
9843 int alloc_len, retval;
9844
9845 cdb = (struct scsi_inquiry *)ctsio->cdb;
9846 alloc_len = scsi_2btoul(cdb->length);
9847
9848 switch (cdb->page_code) {
9849 case SVPD_SUPPORTED_PAGES:
9850 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
9851 break;
9852 case SVPD_UNIT_SERIAL_NUMBER:
9853 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
9854 break;
9855 case SVPD_DEVICE_ID:
9856 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
9857 break;
9858 case SVPD_EXTENDED_INQUIRY_DATA:
9859 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
9860 break;
9861 case SVPD_MODE_PAGE_POLICY:
9862 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
9863 break;
9864 case SVPD_SCSI_PORTS:
9865 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
9866 break;
9867 case SVPD_SCSI_TPC:
9868 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
9869 break;
9870 case SVPD_BLOCK_LIMITS:
9871 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9872 goto err;
9873 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
9874 break;
9875 case SVPD_BDC:
9876 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9877 goto err;
9878 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
9879 break;
9880 case SVPD_LBP:
9881 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9882 goto err;
9883 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
9884 break;
9885 default:
9886 err:
9887 ctl_set_invalid_field(ctsio,
9888 /*sks_valid*/ 1,
9889 /*command*/ 1,
9890 /*field*/ 2,
9891 /*bit_valid*/ 0,
9892 /*bit*/ 0);
9893 ctl_done((union ctl_io *)ctsio);
9894 retval = CTL_RETVAL_COMPLETE;
9895 break;
9896 }
9897
9898 return (retval);
9899 }
9900
9901 /*
9902 * Standard INQUIRY data.
9903 */
9904 static int
ctl_inquiry_std(struct ctl_scsiio * ctsio)9905 ctl_inquiry_std(struct ctl_scsiio *ctsio)
9906 {
9907 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9908 struct ctl_port *port = CTL_PORT(ctsio);
9909 struct ctl_lun *lun = CTL_LUN(ctsio);
9910 struct scsi_inquiry_data *inq_ptr;
9911 struct scsi_inquiry *cdb;
9912 char *val;
9913 uint32_t alloc_len, data_len;
9914 ctl_port_type port_type;
9915
9916 port_type = port->port_type;
9917 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
9918 port_type = CTL_PORT_SCSI;
9919
9920 cdb = (struct scsi_inquiry *)ctsio->cdb;
9921 alloc_len = scsi_2btoul(cdb->length);
9922
9923 /*
9924 * We malloc the full inquiry data size here and fill it
9925 * in. If the user only asks for less, we'll give him
9926 * that much.
9927 */
9928 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
9929 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9930 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
9931 ctsio->kern_sg_entries = 0;
9932 ctsio->kern_rel_offset = 0;
9933 ctsio->kern_data_len = min(data_len, alloc_len);
9934 ctsio->kern_total_len = ctsio->kern_data_len;
9935
9936 if (lun != NULL) {
9937 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
9938 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
9939 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9940 lun->be_lun->lun_type;
9941 } else {
9942 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
9943 lun->be_lun->lun_type;
9944 }
9945 if (lun->flags & CTL_LUN_REMOVABLE)
9946 inq_ptr->dev_qual2 |= SID_RMB;
9947 } else
9948 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
9949
9950 /* RMB in byte 2 is 0 */
9951 inq_ptr->version = SCSI_REV_SPC5;
9952
9953 /*
9954 * According to SAM-3, even if a device only supports a single
9955 * level of LUN addressing, it should still set the HISUP bit:
9956 *
9957 * 4.9.1 Logical unit numbers overview
9958 *
9959 * All logical unit number formats described in this standard are
9960 * hierarchical in structure even when only a single level in that
9961 * hierarchy is used. The HISUP bit shall be set to one in the
9962 * standard INQUIRY data (see SPC-2) when any logical unit number
9963 * format described in this standard is used. Non-hierarchical
9964 * formats are outside the scope of this standard.
9965 *
9966 * Therefore we set the HiSup bit here.
9967 *
9968 * The response format is 2, per SPC-3.
9969 */
9970 inq_ptr->response_format = SID_HiSup | 2;
9971
9972 inq_ptr->additional_length = data_len -
9973 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
9974 CTL_DEBUG_PRINT(("additional_length = %d\n",
9975 inq_ptr->additional_length));
9976
9977 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
9978 if (port_type == CTL_PORT_SCSI)
9979 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
9980 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
9981 inq_ptr->flags = SID_CmdQue;
9982 if (port_type == CTL_PORT_SCSI)
9983 inq_ptr->flags |= SID_WBus16 | SID_Sync;
9984
9985 /*
9986 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
9987 * We have 8 bytes for the vendor name, and 16 bytes for the device
9988 * name and 4 bytes for the revision.
9989 */
9990 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
9991 "vendor")) == NULL) {
9992 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
9993 } else {
9994 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
9995 strncpy(inq_ptr->vendor, val,
9996 min(sizeof(inq_ptr->vendor), strlen(val)));
9997 }
9998 if (lun == NULL) {
9999 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10000 sizeof(inq_ptr->product));
10001 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10002 switch (lun->be_lun->lun_type) {
10003 case T_DIRECT:
10004 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10005 sizeof(inq_ptr->product));
10006 break;
10007 case T_PROCESSOR:
10008 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10009 sizeof(inq_ptr->product));
10010 break;
10011 case T_CDROM:
10012 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT,
10013 sizeof(inq_ptr->product));
10014 break;
10015 default:
10016 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10017 sizeof(inq_ptr->product));
10018 break;
10019 }
10020 } else {
10021 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10022 strncpy(inq_ptr->product, val,
10023 min(sizeof(inq_ptr->product), strlen(val)));
10024 }
10025
10026 /*
10027 * XXX make this a macro somewhere so it automatically gets
10028 * incremented when we make changes.
10029 */
10030 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10031 "revision")) == NULL) {
10032 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10033 } else {
10034 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10035 strncpy(inq_ptr->revision, val,
10036 min(sizeof(inq_ptr->revision), strlen(val)));
10037 }
10038
10039 /*
10040 * For parallel SCSI, we support double transition and single
10041 * transition clocking. We also support QAS (Quick Arbitration
10042 * and Selection) and Information Unit transfers on both the
10043 * control and array devices.
10044 */
10045 if (port_type == CTL_PORT_SCSI)
10046 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10047 SID_SPI_IUS;
10048
10049 /* SAM-6 (no version claimed) */
10050 scsi_ulto2b(0x00C0, inq_ptr->version1);
10051 /* SPC-5 (no version claimed) */
10052 scsi_ulto2b(0x05C0, inq_ptr->version2);
10053 if (port_type == CTL_PORT_FC) {
10054 /* FCP-2 ANSI INCITS.350:2003 */
10055 scsi_ulto2b(0x0917, inq_ptr->version3);
10056 } else if (port_type == CTL_PORT_SCSI) {
10057 /* SPI-4 ANSI INCITS.362:200x */
10058 scsi_ulto2b(0x0B56, inq_ptr->version3);
10059 } else if (port_type == CTL_PORT_ISCSI) {
10060 /* iSCSI (no version claimed) */
10061 scsi_ulto2b(0x0960, inq_ptr->version3);
10062 } else if (port_type == CTL_PORT_SAS) {
10063 /* SAS (no version claimed) */
10064 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10065 } else if (port_type == CTL_PORT_UMASS) {
10066 /* USB Mass Storage Class Bulk-Only Transport, Revision 1.0 */
10067 scsi_ulto2b(0x1730, inq_ptr->version3);
10068 }
10069
10070 if (lun == NULL) {
10071 /* SBC-4 (no version claimed) */
10072 scsi_ulto2b(0x0600, inq_ptr->version4);
10073 } else {
10074 switch (lun->be_lun->lun_type) {
10075 case T_DIRECT:
10076 /* SBC-4 (no version claimed) */
10077 scsi_ulto2b(0x0600, inq_ptr->version4);
10078 break;
10079 case T_PROCESSOR:
10080 break;
10081 case T_CDROM:
10082 /* MMC-6 (no version claimed) */
10083 scsi_ulto2b(0x04E0, inq_ptr->version4);
10084 break;
10085 default:
10086 break;
10087 }
10088 }
10089
10090 ctl_set_success(ctsio);
10091 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10092 ctsio->be_move_done = ctl_config_move_done;
10093 ctl_datamove((union ctl_io *)ctsio);
10094 return (CTL_RETVAL_COMPLETE);
10095 }
10096
10097 int
ctl_inquiry(struct ctl_scsiio * ctsio)10098 ctl_inquiry(struct ctl_scsiio *ctsio)
10099 {
10100 struct scsi_inquiry *cdb;
10101 int retval;
10102
10103 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10104
10105 cdb = (struct scsi_inquiry *)ctsio->cdb;
10106 if (cdb->byte2 & SI_EVPD)
10107 retval = ctl_inquiry_evpd(ctsio);
10108 else if (cdb->page_code == 0)
10109 retval = ctl_inquiry_std(ctsio);
10110 else {
10111 ctl_set_invalid_field(ctsio,
10112 /*sks_valid*/ 1,
10113 /*command*/ 1,
10114 /*field*/ 2,
10115 /*bit_valid*/ 0,
10116 /*bit*/ 0);
10117 ctl_done((union ctl_io *)ctsio);
10118 return (CTL_RETVAL_COMPLETE);
10119 }
10120
10121 return (retval);
10122 }
10123
10124 int
ctl_get_config(struct ctl_scsiio * ctsio)10125 ctl_get_config(struct ctl_scsiio *ctsio)
10126 {
10127 struct ctl_lun *lun = CTL_LUN(ctsio);
10128 struct scsi_get_config_header *hdr;
10129 struct scsi_get_config_feature *feature;
10130 struct scsi_get_config *cdb;
10131 uint32_t alloc_len, data_len;
10132 int rt, starting;
10133
10134 cdb = (struct scsi_get_config *)ctsio->cdb;
10135 rt = (cdb->rt & SGC_RT_MASK);
10136 starting = scsi_2btoul(cdb->starting_feature);
10137 alloc_len = scsi_2btoul(cdb->length);
10138
10139 data_len = sizeof(struct scsi_get_config_header) +
10140 sizeof(struct scsi_get_config_feature) + 8 +
10141 sizeof(struct scsi_get_config_feature) + 8 +
10142 sizeof(struct scsi_get_config_feature) + 4 +
10143 sizeof(struct scsi_get_config_feature) + 4 +
10144 sizeof(struct scsi_get_config_feature) + 8 +
10145 sizeof(struct scsi_get_config_feature) +
10146 sizeof(struct scsi_get_config_feature) + 4 +
10147 sizeof(struct scsi_get_config_feature) + 4 +
10148 sizeof(struct scsi_get_config_feature) + 4 +
10149 sizeof(struct scsi_get_config_feature) + 4 +
10150 sizeof(struct scsi_get_config_feature) + 4 +
10151 sizeof(struct scsi_get_config_feature) + 4;
10152 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10153 ctsio->kern_sg_entries = 0;
10154 ctsio->kern_rel_offset = 0;
10155
10156 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr;
10157 if (lun->flags & CTL_LUN_NO_MEDIA)
10158 scsi_ulto2b(0x0000, hdr->current_profile);
10159 else
10160 scsi_ulto2b(0x0010, hdr->current_profile);
10161 feature = (struct scsi_get_config_feature *)(hdr + 1);
10162
10163 if (starting > 0x003b)
10164 goto done;
10165 if (starting > 0x003a)
10166 goto f3b;
10167 if (starting > 0x002b)
10168 goto f3a;
10169 if (starting > 0x002a)
10170 goto f2b;
10171 if (starting > 0x001f)
10172 goto f2a;
10173 if (starting > 0x001e)
10174 goto f1f;
10175 if (starting > 0x001d)
10176 goto f1e;
10177 if (starting > 0x0010)
10178 goto f1d;
10179 if (starting > 0x0003)
10180 goto f10;
10181 if (starting > 0x0002)
10182 goto f3;
10183 if (starting > 0x0001)
10184 goto f2;
10185 if (starting > 0x0000)
10186 goto f1;
10187
10188 /* Profile List */
10189 scsi_ulto2b(0x0000, feature->feature_code);
10190 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT;
10191 feature->add_length = 8;
10192 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */
10193 feature->feature_data[2] = 0x00;
10194 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */
10195 feature->feature_data[6] = 0x01;
10196 feature = (struct scsi_get_config_feature *)
10197 &feature->feature_data[feature->add_length];
10198
10199 f1: /* Core */
10200 scsi_ulto2b(0x0001, feature->feature_code);
10201 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10202 feature->add_length = 8;
10203 scsi_ulto4b(0x00000000, &feature->feature_data[0]);
10204 feature->feature_data[4] = 0x03;
10205 feature = (struct scsi_get_config_feature *)
10206 &feature->feature_data[feature->add_length];
10207
10208 f2: /* Morphing */
10209 scsi_ulto2b(0x0002, feature->feature_code);
10210 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10211 feature->add_length = 4;
10212 feature->feature_data[0] = 0x02;
10213 feature = (struct scsi_get_config_feature *)
10214 &feature->feature_data[feature->add_length];
10215
10216 f3: /* Removable Medium */
10217 scsi_ulto2b(0x0003, feature->feature_code);
10218 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10219 feature->add_length = 4;
10220 feature->feature_data[0] = 0x39;
10221 feature = (struct scsi_get_config_feature *)
10222 &feature->feature_data[feature->add_length];
10223
10224 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA))
10225 goto done;
10226
10227 f10: /* Random Read */
10228 scsi_ulto2b(0x0010, feature->feature_code);
10229 feature->flags = 0x00;
10230 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10231 feature->flags |= SGC_F_CURRENT;
10232 feature->add_length = 8;
10233 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]);
10234 scsi_ulto2b(1, &feature->feature_data[4]);
10235 feature->feature_data[6] = 0x00;
10236 feature = (struct scsi_get_config_feature *)
10237 &feature->feature_data[feature->add_length];
10238
10239 f1d: /* Multi-Read */
10240 scsi_ulto2b(0x001D, feature->feature_code);
10241 feature->flags = 0x00;
10242 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10243 feature->flags |= SGC_F_CURRENT;
10244 feature->add_length = 0;
10245 feature = (struct scsi_get_config_feature *)
10246 &feature->feature_data[feature->add_length];
10247
10248 f1e: /* CD Read */
10249 scsi_ulto2b(0x001E, feature->feature_code);
10250 feature->flags = 0x00;
10251 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10252 feature->flags |= SGC_F_CURRENT;
10253 feature->add_length = 4;
10254 feature->feature_data[0] = 0x00;
10255 feature = (struct scsi_get_config_feature *)
10256 &feature->feature_data[feature->add_length];
10257
10258 f1f: /* DVD Read */
10259 scsi_ulto2b(0x001F, feature->feature_code);
10260 feature->flags = 0x08;
10261 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10262 feature->flags |= SGC_F_CURRENT;
10263 feature->add_length = 4;
10264 feature->feature_data[0] = 0x01;
10265 feature->feature_data[2] = 0x03;
10266 feature = (struct scsi_get_config_feature *)
10267 &feature->feature_data[feature->add_length];
10268
10269 f2a: /* DVD+RW */
10270 scsi_ulto2b(0x002A, feature->feature_code);
10271 feature->flags = 0x04;
10272 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10273 feature->flags |= SGC_F_CURRENT;
10274 feature->add_length = 4;
10275 feature->feature_data[0] = 0x00;
10276 feature->feature_data[1] = 0x00;
10277 feature = (struct scsi_get_config_feature *)
10278 &feature->feature_data[feature->add_length];
10279
10280 f2b: /* DVD+R */
10281 scsi_ulto2b(0x002B, feature->feature_code);
10282 feature->flags = 0x00;
10283 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10284 feature->flags |= SGC_F_CURRENT;
10285 feature->add_length = 4;
10286 feature->feature_data[0] = 0x00;
10287 feature = (struct scsi_get_config_feature *)
10288 &feature->feature_data[feature->add_length];
10289
10290 f3a: /* DVD+RW Dual Layer */
10291 scsi_ulto2b(0x003A, feature->feature_code);
10292 feature->flags = 0x00;
10293 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10294 feature->flags |= SGC_F_CURRENT;
10295 feature->add_length = 4;
10296 feature->feature_data[0] = 0x00;
10297 feature->feature_data[1] = 0x00;
10298 feature = (struct scsi_get_config_feature *)
10299 &feature->feature_data[feature->add_length];
10300
10301 f3b: /* DVD+R Dual Layer */
10302 scsi_ulto2b(0x003B, feature->feature_code);
10303 feature->flags = 0x00;
10304 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10305 feature->flags |= SGC_F_CURRENT;
10306 feature->add_length = 4;
10307 feature->feature_data[0] = 0x00;
10308 feature = (struct scsi_get_config_feature *)
10309 &feature->feature_data[feature->add_length];
10310
10311 done:
10312 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10313 if (rt == SGC_RT_SPECIFIC && data_len > 4) {
10314 feature = (struct scsi_get_config_feature *)(hdr + 1);
10315 if (scsi_2btoul(feature->feature_code) == starting)
10316 feature = (struct scsi_get_config_feature *)
10317 &feature->feature_data[feature->add_length];
10318 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10319 }
10320 scsi_ulto4b(data_len - 4, hdr->data_length);
10321 ctsio->kern_data_len = min(data_len, alloc_len);
10322 ctsio->kern_total_len = ctsio->kern_data_len;
10323
10324 ctl_set_success(ctsio);
10325 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10326 ctsio->be_move_done = ctl_config_move_done;
10327 ctl_datamove((union ctl_io *)ctsio);
10328 return (CTL_RETVAL_COMPLETE);
10329 }
10330
10331 int
ctl_get_event_status(struct ctl_scsiio * ctsio)10332 ctl_get_event_status(struct ctl_scsiio *ctsio)
10333 {
10334 struct scsi_get_event_status_header *hdr;
10335 struct scsi_get_event_status *cdb;
10336 uint32_t alloc_len, data_len;
10337 int notif_class;
10338
10339 cdb = (struct scsi_get_event_status *)ctsio->cdb;
10340 if ((cdb->byte2 & SGESN_POLLED) == 0) {
10341 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
10342 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
10343 ctl_done((union ctl_io *)ctsio);
10344 return (CTL_RETVAL_COMPLETE);
10345 }
10346 notif_class = cdb->notif_class;
10347 alloc_len = scsi_2btoul(cdb->length);
10348
10349 data_len = sizeof(struct scsi_get_event_status_header);
10350 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10351 ctsio->kern_sg_entries = 0;
10352 ctsio->kern_rel_offset = 0;
10353 ctsio->kern_data_len = min(data_len, alloc_len);
10354 ctsio->kern_total_len = ctsio->kern_data_len;
10355
10356 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr;
10357 scsi_ulto2b(0, hdr->descr_length);
10358 hdr->nea_class = SGESN_NEA;
10359 hdr->supported_class = 0;
10360
10361 ctl_set_success(ctsio);
10362 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10363 ctsio->be_move_done = ctl_config_move_done;
10364 ctl_datamove((union ctl_io *)ctsio);
10365 return (CTL_RETVAL_COMPLETE);
10366 }
10367
10368 int
ctl_mechanism_status(struct ctl_scsiio * ctsio)10369 ctl_mechanism_status(struct ctl_scsiio *ctsio)
10370 {
10371 struct scsi_mechanism_status_header *hdr;
10372 struct scsi_mechanism_status *cdb;
10373 uint32_t alloc_len, data_len;
10374
10375 cdb = (struct scsi_mechanism_status *)ctsio->cdb;
10376 alloc_len = scsi_2btoul(cdb->length);
10377
10378 data_len = sizeof(struct scsi_mechanism_status_header);
10379 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10380 ctsio->kern_sg_entries = 0;
10381 ctsio->kern_rel_offset = 0;
10382 ctsio->kern_data_len = min(data_len, alloc_len);
10383 ctsio->kern_total_len = ctsio->kern_data_len;
10384
10385 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr;
10386 hdr->state1 = 0x00;
10387 hdr->state2 = 0xe0;
10388 scsi_ulto3b(0, hdr->lba);
10389 hdr->slots_num = 0;
10390 scsi_ulto2b(0, hdr->slots_length);
10391
10392 ctl_set_success(ctsio);
10393 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10394 ctsio->be_move_done = ctl_config_move_done;
10395 ctl_datamove((union ctl_io *)ctsio);
10396 return (CTL_RETVAL_COMPLETE);
10397 }
10398
10399 static void
ctl_ultomsf(uint32_t lba,uint8_t * buf)10400 ctl_ultomsf(uint32_t lba, uint8_t *buf)
10401 {
10402
10403 lba += 150;
10404 buf[0] = 0;
10405 buf[1] = bin2bcd((lba / 75) / 60);
10406 buf[2] = bin2bcd((lba / 75) % 60);
10407 buf[3] = bin2bcd(lba % 75);
10408 }
10409
10410 int
ctl_read_toc(struct ctl_scsiio * ctsio)10411 ctl_read_toc(struct ctl_scsiio *ctsio)
10412 {
10413 struct ctl_lun *lun = CTL_LUN(ctsio);
10414 struct scsi_read_toc_hdr *hdr;
10415 struct scsi_read_toc_type01_descr *descr;
10416 struct scsi_read_toc *cdb;
10417 uint32_t alloc_len, data_len;
10418 int format, msf;
10419
10420 cdb = (struct scsi_read_toc *)ctsio->cdb;
10421 msf = (cdb->byte2 & CD_MSF) != 0;
10422 format = cdb->format;
10423 alloc_len = scsi_2btoul(cdb->data_len);
10424
10425 data_len = sizeof(struct scsi_read_toc_hdr);
10426 if (format == 0)
10427 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr);
10428 else
10429 data_len += sizeof(struct scsi_read_toc_type01_descr);
10430 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10431 ctsio->kern_sg_entries = 0;
10432 ctsio->kern_rel_offset = 0;
10433 ctsio->kern_data_len = min(data_len, alloc_len);
10434 ctsio->kern_total_len = ctsio->kern_data_len;
10435
10436 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr;
10437 if (format == 0) {
10438 scsi_ulto2b(0x12, hdr->data_length);
10439 hdr->first = 1;
10440 hdr->last = 1;
10441 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10442 descr->addr_ctl = 0x14;
10443 descr->track_number = 1;
10444 if (msf)
10445 ctl_ultomsf(0, descr->track_start);
10446 else
10447 scsi_ulto4b(0, descr->track_start);
10448 descr++;
10449 descr->addr_ctl = 0x14;
10450 descr->track_number = 0xaa;
10451 if (msf)
10452 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start);
10453 else
10454 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start);
10455 } else {
10456 scsi_ulto2b(0x0a, hdr->data_length);
10457 hdr->first = 1;
10458 hdr->last = 1;
10459 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10460 descr->addr_ctl = 0x14;
10461 descr->track_number = 1;
10462 if (msf)
10463 ctl_ultomsf(0, descr->track_start);
10464 else
10465 scsi_ulto4b(0, descr->track_start);
10466 }
10467
10468 ctl_set_success(ctsio);
10469 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10470 ctsio->be_move_done = ctl_config_move_done;
10471 ctl_datamove((union ctl_io *)ctsio);
10472 return (CTL_RETVAL_COMPLETE);
10473 }
10474
10475 /*
10476 * For known CDB types, parse the LBA and length.
10477 */
10478 static int
ctl_get_lba_len(union ctl_io * io,uint64_t * lba,uint64_t * len)10479 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10480 {
10481 if (io->io_hdr.io_type != CTL_IO_SCSI)
10482 return (1);
10483
10484 switch (io->scsiio.cdb[0]) {
10485 case COMPARE_AND_WRITE: {
10486 struct scsi_compare_and_write *cdb;
10487
10488 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10489
10490 *lba = scsi_8btou64(cdb->addr);
10491 *len = cdb->length;
10492 break;
10493 }
10494 case READ_6:
10495 case WRITE_6: {
10496 struct scsi_rw_6 *cdb;
10497
10498 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10499
10500 *lba = scsi_3btoul(cdb->addr);
10501 /* only 5 bits are valid in the most significant address byte */
10502 *lba &= 0x1fffff;
10503 *len = cdb->length;
10504 break;
10505 }
10506 case READ_10:
10507 case WRITE_10: {
10508 struct scsi_rw_10 *cdb;
10509
10510 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10511
10512 *lba = scsi_4btoul(cdb->addr);
10513 *len = scsi_2btoul(cdb->length);
10514 break;
10515 }
10516 case WRITE_VERIFY_10: {
10517 struct scsi_write_verify_10 *cdb;
10518
10519 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10520
10521 *lba = scsi_4btoul(cdb->addr);
10522 *len = scsi_2btoul(cdb->length);
10523 break;
10524 }
10525 case READ_12:
10526 case WRITE_12: {
10527 struct scsi_rw_12 *cdb;
10528
10529 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10530
10531 *lba = scsi_4btoul(cdb->addr);
10532 *len = scsi_4btoul(cdb->length);
10533 break;
10534 }
10535 case WRITE_VERIFY_12: {
10536 struct scsi_write_verify_12 *cdb;
10537
10538 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10539
10540 *lba = scsi_4btoul(cdb->addr);
10541 *len = scsi_4btoul(cdb->length);
10542 break;
10543 }
10544 case READ_16:
10545 case WRITE_16: {
10546 struct scsi_rw_16 *cdb;
10547
10548 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10549
10550 *lba = scsi_8btou64(cdb->addr);
10551 *len = scsi_4btoul(cdb->length);
10552 break;
10553 }
10554 case WRITE_ATOMIC_16: {
10555 struct scsi_write_atomic_16 *cdb;
10556
10557 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb;
10558
10559 *lba = scsi_8btou64(cdb->addr);
10560 *len = scsi_2btoul(cdb->length);
10561 break;
10562 }
10563 case WRITE_VERIFY_16: {
10564 struct scsi_write_verify_16 *cdb;
10565
10566 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10567
10568 *lba = scsi_8btou64(cdb->addr);
10569 *len = scsi_4btoul(cdb->length);
10570 break;
10571 }
10572 case WRITE_SAME_10: {
10573 struct scsi_write_same_10 *cdb;
10574
10575 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10576
10577 *lba = scsi_4btoul(cdb->addr);
10578 *len = scsi_2btoul(cdb->length);
10579 break;
10580 }
10581 case WRITE_SAME_16: {
10582 struct scsi_write_same_16 *cdb;
10583
10584 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10585
10586 *lba = scsi_8btou64(cdb->addr);
10587 *len = scsi_4btoul(cdb->length);
10588 break;
10589 }
10590 case VERIFY_10: {
10591 struct scsi_verify_10 *cdb;
10592
10593 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10594
10595 *lba = scsi_4btoul(cdb->addr);
10596 *len = scsi_2btoul(cdb->length);
10597 break;
10598 }
10599 case VERIFY_12: {
10600 struct scsi_verify_12 *cdb;
10601
10602 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10603
10604 *lba = scsi_4btoul(cdb->addr);
10605 *len = scsi_4btoul(cdb->length);
10606 break;
10607 }
10608 case VERIFY_16: {
10609 struct scsi_verify_16 *cdb;
10610
10611 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10612
10613 *lba = scsi_8btou64(cdb->addr);
10614 *len = scsi_4btoul(cdb->length);
10615 break;
10616 }
10617 case UNMAP: {
10618 *lba = 0;
10619 *len = UINT64_MAX;
10620 break;
10621 }
10622 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10623 struct scsi_get_lba_status *cdb;
10624
10625 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10626 *lba = scsi_8btou64(cdb->addr);
10627 *len = UINT32_MAX;
10628 break;
10629 }
10630 default:
10631 return (1);
10632 break; /* NOTREACHED */
10633 }
10634
10635 return (0);
10636 }
10637
10638 static ctl_action
ctl_extent_check_lba(uint64_t lba1,uint64_t len1,uint64_t lba2,uint64_t len2,bool seq)10639 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10640 bool seq)
10641 {
10642 uint64_t endlba1, endlba2;
10643
10644 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10645 endlba2 = lba2 + len2 - 1;
10646
10647 if ((endlba1 < lba2) || (endlba2 < lba1))
10648 return (CTL_ACTION_PASS);
10649 else
10650 return (CTL_ACTION_BLOCK);
10651 }
10652
10653 static int
ctl_extent_check_unmap(union ctl_io * io,uint64_t lba2,uint64_t len2)10654 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10655 {
10656 struct ctl_ptr_len_flags *ptrlen;
10657 struct scsi_unmap_desc *buf, *end, *range;
10658 uint64_t lba;
10659 uint32_t len;
10660
10661 /* If not UNMAP -- go other way. */
10662 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10663 io->scsiio.cdb[0] != UNMAP)
10664 return (CTL_ACTION_ERROR);
10665
10666 /* If UNMAP without data -- block and wait for data. */
10667 ptrlen = (struct ctl_ptr_len_flags *)
10668 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10669 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10670 ptrlen->ptr == NULL)
10671 return (CTL_ACTION_BLOCK);
10672
10673 /* UNMAP with data -- check for collision. */
10674 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10675 end = buf + ptrlen->len / sizeof(*buf);
10676 for (range = buf; range < end; range++) {
10677 lba = scsi_8btou64(range->lba);
10678 len = scsi_4btoul(range->length);
10679 if ((lba < lba2 + len2) && (lba + len > lba2))
10680 return (CTL_ACTION_BLOCK);
10681 }
10682 return (CTL_ACTION_PASS);
10683 }
10684
10685 static ctl_action
ctl_extent_check(union ctl_io * io1,union ctl_io * io2,bool seq)10686 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10687 {
10688 uint64_t lba1, lba2;
10689 uint64_t len1, len2;
10690 int retval;
10691
10692 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10693 return (CTL_ACTION_ERROR);
10694
10695 retval = ctl_extent_check_unmap(io1, lba2, len2);
10696 if (retval != CTL_ACTION_ERROR)
10697 return (retval);
10698
10699 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10700 return (CTL_ACTION_ERROR);
10701
10702 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10703 seq = FALSE;
10704 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10705 }
10706
10707 static ctl_action
ctl_extent_check_seq(union ctl_io * io1,union ctl_io * io2)10708 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
10709 {
10710 uint64_t lba1, lba2;
10711 uint64_t len1, len2;
10712
10713 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10714 return (CTL_ACTION_PASS);
10715 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10716 return (CTL_ACTION_ERROR);
10717 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10718 return (CTL_ACTION_ERROR);
10719
10720 if (lba1 + len1 == lba2)
10721 return (CTL_ACTION_BLOCK);
10722 return (CTL_ACTION_PASS);
10723 }
10724
10725 static ctl_action
ctl_check_for_blockage(struct ctl_lun * lun,union ctl_io * pending_io,union ctl_io * ooa_io)10726 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10727 union ctl_io *ooa_io)
10728 {
10729 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10730 const ctl_serialize_action *serialize_row;
10731
10732 /*
10733 * Aborted commands are not going to be executed and may even
10734 * not report completion, so we don't care about their order.
10735 * Let them complete ASAP to clean the OOA queue.
10736 */
10737 if (pending_io->io_hdr.flags & CTL_FLAG_ABORT)
10738 return (CTL_ACTION_SKIP);
10739
10740 /*
10741 * The initiator attempted multiple untagged commands at the same
10742 * time. Can't do that.
10743 */
10744 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10745 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10746 && ((pending_io->io_hdr.nexus.targ_port ==
10747 ooa_io->io_hdr.nexus.targ_port)
10748 && (pending_io->io_hdr.nexus.initid ==
10749 ooa_io->io_hdr.nexus.initid))
10750 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10751 CTL_FLAG_STATUS_SENT)) == 0))
10752 return (CTL_ACTION_OVERLAP);
10753
10754 /*
10755 * The initiator attempted to send multiple tagged commands with
10756 * the same ID. (It's fine if different initiators have the same
10757 * tag ID.)
10758 *
10759 * Even if all of those conditions are true, we don't kill the I/O
10760 * if the command ahead of us has been aborted. We won't end up
10761 * sending it to the FETD, and it's perfectly legal to resend a
10762 * command with the same tag number as long as the previous
10763 * instance of this tag number has been aborted somehow.
10764 */
10765 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10766 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10767 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10768 && ((pending_io->io_hdr.nexus.targ_port ==
10769 ooa_io->io_hdr.nexus.targ_port)
10770 && (pending_io->io_hdr.nexus.initid ==
10771 ooa_io->io_hdr.nexus.initid))
10772 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10773 CTL_FLAG_STATUS_SENT)) == 0))
10774 return (CTL_ACTION_OVERLAP_TAG);
10775
10776 /*
10777 * If we get a head of queue tag, SAM-3 says that we should
10778 * immediately execute it.
10779 *
10780 * What happens if this command would normally block for some other
10781 * reason? e.g. a request sense with a head of queue tag
10782 * immediately after a write. Normally that would block, but this
10783 * will result in its getting executed immediately...
10784 *
10785 * We currently return "pass" instead of "skip", so we'll end up
10786 * going through the rest of the queue to check for overlapped tags.
10787 *
10788 * XXX KDM check for other types of blockage first??
10789 */
10790 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10791 return (CTL_ACTION_PASS);
10792
10793 /*
10794 * Ordered tags have to block until all items ahead of them
10795 * have completed. If we get called with an ordered tag, we always
10796 * block, if something else is ahead of us in the queue.
10797 */
10798 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10799 return (CTL_ACTION_BLOCK);
10800
10801 /*
10802 * Simple tags get blocked until all head of queue and ordered tags
10803 * ahead of them have completed. I'm lumping untagged commands in
10804 * with simple tags here. XXX KDM is that the right thing to do?
10805 */
10806 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10807 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10808 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10809 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10810 return (CTL_ACTION_BLOCK);
10811
10812 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
10813 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT,
10814 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p",
10815 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0],
10816 pending_io->scsiio.cdb[1], pending_io));
10817 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
10818 if (ooa_entry->seridx == CTL_SERIDX_INVLD)
10819 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */
10820 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT,
10821 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p",
10822 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0],
10823 ooa_io->scsiio.cdb[1], ooa_io));
10824
10825 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10826
10827 switch (serialize_row[pending_entry->seridx]) {
10828 case CTL_SER_BLOCK:
10829 return (CTL_ACTION_BLOCK);
10830 case CTL_SER_EXTENT:
10831 return (ctl_extent_check(ooa_io, pending_io,
10832 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10833 case CTL_SER_EXTENTOPT:
10834 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
10835 SCP_QUEUE_ALG_UNRESTRICTED)
10836 return (ctl_extent_check(ooa_io, pending_io,
10837 (lun->be_lun &&
10838 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10839 return (CTL_ACTION_PASS);
10840 case CTL_SER_EXTENTSEQ:
10841 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
10842 return (ctl_extent_check_seq(ooa_io, pending_io));
10843 return (CTL_ACTION_PASS);
10844 case CTL_SER_PASS:
10845 return (CTL_ACTION_PASS);
10846 case CTL_SER_BLOCKOPT:
10847 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
10848 SCP_QUEUE_ALG_UNRESTRICTED)
10849 return (CTL_ACTION_BLOCK);
10850 return (CTL_ACTION_PASS);
10851 case CTL_SER_SKIP:
10852 return (CTL_ACTION_SKIP);
10853 default:
10854 panic("%s: Invalid serialization value %d for %d => %d",
10855 __func__, serialize_row[pending_entry->seridx],
10856 pending_entry->seridx, ooa_entry->seridx);
10857 }
10858
10859 return (CTL_ACTION_ERROR);
10860 }
10861
10862 /*
10863 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10864 * Assumptions:
10865 * - pending_io is generally either incoming, or on the blocked queue
10866 * - starting I/O is the I/O we want to start the check with.
10867 */
10868 static ctl_action
ctl_check_ooa(struct ctl_lun * lun,union ctl_io * pending_io,union ctl_io ** starting_io)10869 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10870 union ctl_io **starting_io)
10871 {
10872 union ctl_io *ooa_io;
10873 ctl_action action;
10874
10875 mtx_assert(&lun->lun_lock, MA_OWNED);
10876
10877 /*
10878 * Run back along the OOA queue, starting with the current
10879 * blocked I/O and going through every I/O before it on the
10880 * queue. If starting_io is NULL, we'll just end up returning
10881 * CTL_ACTION_PASS.
10882 */
10883 for (ooa_io = *starting_io; ooa_io != NULL;
10884 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10885 ooa_links)){
10886 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
10887 if (action != CTL_ACTION_PASS) {
10888 *starting_io = ooa_io;
10889 return (action);
10890 }
10891 }
10892
10893 *starting_io = NULL;
10894 return (CTL_ACTION_PASS);
10895 }
10896
10897 /*
10898 * Try to unblock the specified I/O.
10899 *
10900 * skip parameter allows explicitly skip present blocker of the I/O,
10901 * starting from the previous one on OOA queue. It can be used when
10902 * we know for sure that the blocker I/O does no longer count.
10903 */
10904 static void
ctl_try_unblock_io(struct ctl_lun * lun,union ctl_io * io,bool skip)10905 ctl_try_unblock_io(struct ctl_lun *lun, union ctl_io *io, bool skip)
10906 {
10907 struct ctl_softc *softc = lun->ctl_softc;
10908 union ctl_io *bio, *obio;
10909 const struct ctl_cmd_entry *entry;
10910 union ctl_ha_msg msg_info;
10911 ctl_action action;
10912
10913 mtx_assert(&lun->lun_lock, MA_OWNED);
10914
10915 if (io->io_hdr.blocker == NULL)
10916 return;
10917
10918 obio = bio = io->io_hdr.blocker;
10919 if (skip)
10920 bio = (union ctl_io *)TAILQ_PREV(&bio->io_hdr, ctl_ooaq,
10921 ooa_links);
10922 action = ctl_check_ooa(lun, io, &bio);
10923 if (action == CTL_ACTION_BLOCK) {
10924 /* Still blocked, but may be by different I/O now. */
10925 if (bio != obio) {
10926 TAILQ_REMOVE(&obio->io_hdr.blocked_queue,
10927 &io->io_hdr, blocked_links);
10928 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue,
10929 &io->io_hdr, blocked_links);
10930 io->io_hdr.blocker = bio;
10931 }
10932 return;
10933 }
10934
10935 /* No longer blocked, one way or another. */
10936 TAILQ_REMOVE(&obio->io_hdr.blocked_queue, &io->io_hdr, blocked_links);
10937 io->io_hdr.blocker = NULL;
10938
10939 switch (action) {
10940 case CTL_ACTION_OVERLAP:
10941 ctl_set_overlapped_cmd(&io->scsiio);
10942 goto error;
10943 case CTL_ACTION_OVERLAP_TAG:
10944 ctl_set_overlapped_tag(&io->scsiio,
10945 io->scsiio.tag_num & 0xff);
10946 goto error;
10947 case CTL_ACTION_PASS:
10948 case CTL_ACTION_SKIP:
10949
10950 /* Serializing commands from the other SC retire there. */
10951 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) &&
10952 (softc->ha_mode != CTL_HA_MODE_XFER)) {
10953 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
10954 msg_info.hdr.original_sc = io->io_hdr.remote_io;
10955 msg_info.hdr.serializing_sc = io;
10956 msg_info.hdr.msg_type = CTL_MSG_R2R;
10957 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
10958 sizeof(msg_info.hdr), M_NOWAIT);
10959 break;
10960 }
10961
10962 /*
10963 * Check this I/O for LUN state changes that may have happened
10964 * while this command was blocked. The LUN state may have been
10965 * changed by a command ahead of us in the queue.
10966 */
10967 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
10968 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) {
10969 ctl_done(io);
10970 break;
10971 }
10972
10973 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
10974 ctl_enqueue_rtr(io);
10975 break;
10976 case CTL_ACTION_ERROR:
10977 default:
10978 ctl_set_internal_failure(&io->scsiio,
10979 /*sks_valid*/ 0,
10980 /*retry_count*/ 0);
10981
10982 error:
10983 /* Serializing commands from the other SC are done here. */
10984 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) &&
10985 (softc->ha_mode != CTL_HA_MODE_XFER)) {
10986 ctl_try_unblock_others(lun, io, TRUE);
10987 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
10988
10989 ctl_copy_sense_data_back(io, &msg_info);
10990 msg_info.hdr.original_sc = io->io_hdr.remote_io;
10991 msg_info.hdr.serializing_sc = NULL;
10992 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
10993 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
10994 sizeof(msg_info.scsi), M_WAITOK);
10995 ctl_free_io(io);
10996 break;
10997 }
10998
10999 ctl_done(io);
11000 break;
11001 }
11002 }
11003
11004 /*
11005 * Try to unblock I/Os blocked by the specified I/O.
11006 *
11007 * skip parameter allows explicitly skip the specified I/O as blocker,
11008 * starting from the previous one on the OOA queue. It can be used when
11009 * we know for sure that the specified I/O does no longer count (done).
11010 * It has to be still on OOA queue though so that we know where to start.
11011 */
11012 static void
ctl_try_unblock_others(struct ctl_lun * lun,union ctl_io * bio,bool skip)11013 ctl_try_unblock_others(struct ctl_lun *lun, union ctl_io *bio, bool skip)
11014 {
11015 union ctl_io *io, *next_io;
11016
11017 mtx_assert(&lun->lun_lock, MA_OWNED);
11018
11019 for (io = (union ctl_io *)TAILQ_FIRST(&bio->io_hdr.blocked_queue);
11020 io != NULL; io = next_io) {
11021 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, blocked_links);
11022
11023 KASSERT(io->io_hdr.blocker != NULL,
11024 ("I/O %p on blocked list without blocker", io));
11025 ctl_try_unblock_io(lun, io, skip);
11026 }
11027 KASSERT(!skip || TAILQ_EMPTY(&bio->io_hdr.blocked_queue),
11028 ("blocked_queue is not empty after skipping %p", bio));
11029 }
11030
11031 /*
11032 * This routine (with one exception) checks LUN flags that can be set by
11033 * commands ahead of us in the OOA queue. These flags have to be checked
11034 * when a command initially comes in, and when we pull a command off the
11035 * blocked queue and are preparing to execute it. The reason we have to
11036 * check these flags for commands on the blocked queue is that the LUN
11037 * state may have been changed by a command ahead of us while we're on the
11038 * blocked queue.
11039 *
11040 * Ordering is somewhat important with these checks, so please pay
11041 * careful attention to the placement of any new checks.
11042 */
11043 static int
ctl_scsiio_lun_check(struct ctl_lun * lun,const struct ctl_cmd_entry * entry,struct ctl_scsiio * ctsio)11044 ctl_scsiio_lun_check(struct ctl_lun *lun,
11045 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11046 {
11047 struct ctl_softc *softc = lun->ctl_softc;
11048 int retval;
11049 uint32_t residx;
11050
11051 retval = 0;
11052
11053 mtx_assert(&lun->lun_lock, MA_OWNED);
11054
11055 /*
11056 * If this shelf is a secondary shelf controller, we may have to
11057 * reject some commands disallowed by HA mode and link state.
11058 */
11059 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11060 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
11061 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11062 ctl_set_lun_unavail(ctsio);
11063 retval = 1;
11064 goto bailout;
11065 }
11066 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
11067 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11068 ctl_set_lun_transit(ctsio);
11069 retval = 1;
11070 goto bailout;
11071 }
11072 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
11073 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
11074 ctl_set_lun_standby(ctsio);
11075 retval = 1;
11076 goto bailout;
11077 }
11078
11079 /* The rest of checks are only done on executing side */
11080 if (softc->ha_mode == CTL_HA_MODE_XFER)
11081 goto bailout;
11082 }
11083
11084 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11085 if (lun->be_lun &&
11086 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
11087 ctl_set_hw_write_protected(ctsio);
11088 retval = 1;
11089 goto bailout;
11090 }
11091 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) {
11092 ctl_set_sense(ctsio, /*current_error*/ 1,
11093 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11094 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11095 retval = 1;
11096 goto bailout;
11097 }
11098 }
11099
11100 /*
11101 * Check for a reservation conflict. If this command isn't allowed
11102 * even on reserved LUNs, and if this initiator isn't the one who
11103 * reserved us, reject the command with a reservation conflict.
11104 */
11105 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11106 if ((lun->flags & CTL_LUN_RESERVED)
11107 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11108 if (lun->res_idx != residx) {
11109 ctl_set_reservation_conflict(ctsio);
11110 retval = 1;
11111 goto bailout;
11112 }
11113 }
11114
11115 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11116 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11117 /* No reservation or command is allowed. */;
11118 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11119 (lun->pr_res_type == SPR_TYPE_WR_EX ||
11120 lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
11121 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) {
11122 /* The command is allowed for Write Exclusive resv. */;
11123 } else {
11124 /*
11125 * if we aren't registered or it's a res holder type
11126 * reservation and this isn't the res holder then set a
11127 * conflict.
11128 */
11129 if (ctl_get_prkey(lun, residx) == 0 ||
11130 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) {
11131 ctl_set_reservation_conflict(ctsio);
11132 retval = 1;
11133 goto bailout;
11134 }
11135 }
11136
11137 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) {
11138 if (lun->flags & CTL_LUN_EJECTED)
11139 ctl_set_lun_ejected(ctsio);
11140 else if (lun->flags & CTL_LUN_NO_MEDIA) {
11141 if (lun->flags & CTL_LUN_REMOVABLE)
11142 ctl_set_lun_no_media(ctsio);
11143 else
11144 ctl_set_lun_int_reqd(ctsio);
11145 } else if (lun->flags & CTL_LUN_STOPPED)
11146 ctl_set_lun_stopped(ctsio);
11147 else
11148 goto bailout;
11149 retval = 1;
11150 goto bailout;
11151 }
11152
11153 bailout:
11154 return (retval);
11155 }
11156
11157 static void
ctl_failover_io(union ctl_io * io,int have_lock)11158 ctl_failover_io(union ctl_io *io, int have_lock)
11159 {
11160 ctl_set_busy(&io->scsiio);
11161 ctl_done(io);
11162 }
11163
11164 static void
ctl_failover_lun(union ctl_io * rio)11165 ctl_failover_lun(union ctl_io *rio)
11166 {
11167 struct ctl_softc *softc = CTL_SOFTC(rio);
11168 struct ctl_lun *lun;
11169 struct ctl_io_hdr *io, *next_io;
11170 uint32_t targ_lun;
11171
11172 targ_lun = rio->io_hdr.nexus.targ_mapped_lun;
11173 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun));
11174
11175 /* Find and lock the LUN. */
11176 mtx_lock(&softc->ctl_lock);
11177 if (targ_lun > ctl_max_luns ||
11178 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11179 mtx_unlock(&softc->ctl_lock);
11180 return;
11181 }
11182 mtx_lock(&lun->lun_lock);
11183 mtx_unlock(&softc->ctl_lock);
11184 if (lun->flags & CTL_LUN_DISABLED) {
11185 mtx_unlock(&lun->lun_lock);
11186 return;
11187 }
11188
11189 if (softc->ha_mode == CTL_HA_MODE_XFER) {
11190 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11191 /* We are master */
11192 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11193 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11194 io->flags |= CTL_FLAG_ABORT;
11195 io->flags |= CTL_FLAG_FAILOVER;
11196 ctl_try_unblock_io(lun,
11197 (union ctl_io *)io, FALSE);
11198 } else { /* This can be only due to DATAMOVE */
11199 io->msg_type = CTL_MSG_DATAMOVE_DONE;
11200 io->flags &= ~CTL_FLAG_DMA_INPROG;
11201 io->flags |= CTL_FLAG_IO_ACTIVE;
11202 io->port_status = 31340;
11203 ctl_enqueue_isc((union ctl_io *)io);
11204 }
11205 } else
11206 /* We are slave */
11207 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11208 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11209 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11210 io->flags |= CTL_FLAG_FAILOVER;
11211 } else {
11212 ctl_set_busy(&((union ctl_io *)io)->
11213 scsiio);
11214 ctl_done((union ctl_io *)io);
11215 }
11216 }
11217 }
11218 } else { /* SERIALIZE modes */
11219 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11220 /* We are master */
11221 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11222 if (io->blocker != NULL) {
11223 TAILQ_REMOVE(&io->blocker->io_hdr.blocked_queue,
11224 io, blocked_links);
11225 io->blocker = NULL;
11226 }
11227 ctl_try_unblock_others(lun, (union ctl_io *)io,
11228 TRUE);
11229 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11230 ctl_free_io((union ctl_io *)io);
11231 } else
11232 /* We are slave */
11233 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11234 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11235 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
11236 ctl_set_busy(&((union ctl_io *)io)->
11237 scsiio);
11238 ctl_done((union ctl_io *)io);
11239 }
11240 }
11241 }
11242 }
11243 mtx_unlock(&lun->lun_lock);
11244 }
11245
11246 static int
ctl_scsiio_precheck(struct ctl_softc * softc,struct ctl_scsiio * ctsio)11247 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11248 {
11249 struct ctl_lun *lun;
11250 const struct ctl_cmd_entry *entry;
11251 union ctl_io *bio;
11252 uint32_t initidx, targ_lun;
11253 int retval = 0;
11254
11255 lun = NULL;
11256 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11257 if (targ_lun < ctl_max_luns)
11258 lun = softc->ctl_luns[targ_lun];
11259 if (lun) {
11260 /*
11261 * If the LUN is invalid, pretend that it doesn't exist.
11262 * It will go away as soon as all pending I/O has been
11263 * completed.
11264 */
11265 mtx_lock(&lun->lun_lock);
11266 if (lun->flags & CTL_LUN_DISABLED) {
11267 mtx_unlock(&lun->lun_lock);
11268 lun = NULL;
11269 }
11270 }
11271 CTL_LUN(ctsio) = lun;
11272 if (lun) {
11273 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
11274
11275 /*
11276 * Every I/O goes into the OOA queue for a particular LUN,
11277 * and stays there until completion.
11278 */
11279 #ifdef CTL_TIME_IO
11280 if (TAILQ_EMPTY(&lun->ooa_queue))
11281 lun->idle_time += getsbinuptime() - lun->last_busy;
11282 #endif
11283 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
11284 }
11285
11286 /* Get command entry and return error if it is unsuppotyed. */
11287 entry = ctl_validate_command(ctsio);
11288 if (entry == NULL) {
11289 if (lun)
11290 mtx_unlock(&lun->lun_lock);
11291 return (retval);
11292 }
11293
11294 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11295 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11296
11297 /*
11298 * Check to see whether we can send this command to LUNs that don't
11299 * exist. This should pretty much only be the case for inquiry
11300 * and request sense. Further checks, below, really require having
11301 * a LUN, so we can't really check the command anymore. Just put
11302 * it on the rtr queue.
11303 */
11304 if (lun == NULL) {
11305 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11306 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11307 ctl_enqueue_rtr((union ctl_io *)ctsio);
11308 return (retval);
11309 }
11310
11311 ctl_set_unsupported_lun(ctsio);
11312 ctl_done((union ctl_io *)ctsio);
11313 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11314 return (retval);
11315 } else {
11316 /*
11317 * Make sure we support this particular command on this LUN.
11318 * e.g., we don't support writes to the control LUN.
11319 */
11320 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11321 mtx_unlock(&lun->lun_lock);
11322 ctl_set_invalid_opcode(ctsio);
11323 ctl_done((union ctl_io *)ctsio);
11324 return (retval);
11325 }
11326 }
11327
11328 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11329
11330 /*
11331 * If we've got a request sense, it'll clear the contingent
11332 * allegiance condition. Otherwise, if we have a CA condition for
11333 * this initiator, clear it, because it sent down a command other
11334 * than request sense.
11335 */
11336 if (ctsio->cdb[0] != REQUEST_SENSE) {
11337 struct scsi_sense_data *ps;
11338
11339 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
11340 if (ps != NULL)
11341 ps[initidx % CTL_MAX_INIT_PER_PORT].error_code = 0;
11342 }
11343
11344 /*
11345 * If the command has this flag set, it handles its own unit
11346 * attention reporting, we shouldn't do anything. Otherwise we
11347 * check for any pending unit attentions, and send them back to the
11348 * initiator. We only do this when a command initially comes in,
11349 * not when we pull it off the blocked queue.
11350 *
11351 * According to SAM-3, section 5.3.2, the order that things get
11352 * presented back to the host is basically unit attentions caused
11353 * by some sort of reset event, busy status, reservation conflicts
11354 * or task set full, and finally any other status.
11355 *
11356 * One issue here is that some of the unit attentions we report
11357 * don't fall into the "reset" category (e.g. "reported luns data
11358 * has changed"). So reporting it here, before the reservation
11359 * check, may be technically wrong. I guess the only thing to do
11360 * would be to check for and report the reset events here, and then
11361 * check for the other unit attention types after we check for a
11362 * reservation conflict.
11363 *
11364 * XXX KDM need to fix this
11365 */
11366 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11367 ctl_ua_type ua_type;
11368 u_int sense_len = 0;
11369
11370 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11371 &sense_len, SSD_TYPE_NONE);
11372 if (ua_type != CTL_UA_NONE) {
11373 mtx_unlock(&lun->lun_lock);
11374 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11375 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11376 ctsio->sense_len = sense_len;
11377 ctl_done((union ctl_io *)ctsio);
11378 return (retval);
11379 }
11380 }
11381
11382
11383 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11384 mtx_unlock(&lun->lun_lock);
11385 ctl_done((union ctl_io *)ctsio);
11386 return (retval);
11387 }
11388
11389 /*
11390 * XXX CHD this is where we want to send IO to other side if
11391 * this LUN is secondary on this SC. We will need to make a copy
11392 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11393 * the copy we send as FROM_OTHER.
11394 * We also need to stuff the address of the original IO so we can
11395 * find it easily. Something similar will need be done on the other
11396 * side so when we are done we can find the copy.
11397 */
11398 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11399 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
11400 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
11401 union ctl_ha_msg msg_info;
11402 int isc_retval;
11403
11404 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11405 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11406 mtx_unlock(&lun->lun_lock);
11407
11408 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11409 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11410 msg_info.hdr.serializing_sc = NULL;
11411 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11412 msg_info.scsi.tag_num = ctsio->tag_num;
11413 msg_info.scsi.tag_type = ctsio->tag_type;
11414 msg_info.scsi.cdb_len = ctsio->cdb_len;
11415 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11416
11417 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11418 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11419 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11420 ctl_set_busy(ctsio);
11421 ctl_done((union ctl_io *)ctsio);
11422 return (retval);
11423 }
11424 return (retval);
11425 }
11426
11427 bio = (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, ooa_links);
11428 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, &bio)) {
11429 case CTL_ACTION_BLOCK:
11430 ctsio->io_hdr.blocker = bio;
11431 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctsio->io_hdr,
11432 blocked_links);
11433 mtx_unlock(&lun->lun_lock);
11434 return (retval);
11435 case CTL_ACTION_PASS:
11436 case CTL_ACTION_SKIP:
11437 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11438 mtx_unlock(&lun->lun_lock);
11439 ctl_enqueue_rtr((union ctl_io *)ctsio);
11440 break;
11441 case CTL_ACTION_OVERLAP:
11442 mtx_unlock(&lun->lun_lock);
11443 ctl_set_overlapped_cmd(ctsio);
11444 ctl_done((union ctl_io *)ctsio);
11445 break;
11446 case CTL_ACTION_OVERLAP_TAG:
11447 mtx_unlock(&lun->lun_lock);
11448 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11449 ctl_done((union ctl_io *)ctsio);
11450 break;
11451 case CTL_ACTION_ERROR:
11452 default:
11453 mtx_unlock(&lun->lun_lock);
11454 ctl_set_internal_failure(ctsio,
11455 /*sks_valid*/ 0,
11456 /*retry_count*/ 0);
11457 ctl_done((union ctl_io *)ctsio);
11458 break;
11459 }
11460 return (retval);
11461 }
11462
11463 const struct ctl_cmd_entry *
ctl_get_cmd_entry(struct ctl_scsiio * ctsio,int * sa)11464 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11465 {
11466 const struct ctl_cmd_entry *entry;
11467 int service_action;
11468
11469 entry = &ctl_cmd_table[ctsio->cdb[0]];
11470 if (sa)
11471 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11472 if (entry->flags & CTL_CMD_FLAG_SA5) {
11473 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11474 entry = &((const struct ctl_cmd_entry *)
11475 entry->execute)[service_action];
11476 }
11477 return (entry);
11478 }
11479
11480 const struct ctl_cmd_entry *
ctl_validate_command(struct ctl_scsiio * ctsio)11481 ctl_validate_command(struct ctl_scsiio *ctsio)
11482 {
11483 const struct ctl_cmd_entry *entry;
11484 int i, sa;
11485 uint8_t diff;
11486
11487 entry = ctl_get_cmd_entry(ctsio, &sa);
11488 if (entry->execute == NULL) {
11489 if (sa)
11490 ctl_set_invalid_field(ctsio,
11491 /*sks_valid*/ 1,
11492 /*command*/ 1,
11493 /*field*/ 1,
11494 /*bit_valid*/ 1,
11495 /*bit*/ 4);
11496 else
11497 ctl_set_invalid_opcode(ctsio);
11498 ctl_done((union ctl_io *)ctsio);
11499 return (NULL);
11500 }
11501 KASSERT(entry->length > 0,
11502 ("Not defined length for command 0x%02x/0x%02x",
11503 ctsio->cdb[0], ctsio->cdb[1]));
11504 for (i = 1; i < entry->length; i++) {
11505 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11506 if (diff == 0)
11507 continue;
11508 ctl_set_invalid_field(ctsio,
11509 /*sks_valid*/ 1,
11510 /*command*/ 1,
11511 /*field*/ i,
11512 /*bit_valid*/ 1,
11513 /*bit*/ fls(diff) - 1);
11514 ctl_done((union ctl_io *)ctsio);
11515 return (NULL);
11516 }
11517 return (entry);
11518 }
11519
11520 static int
ctl_cmd_applicable(uint8_t lun_type,const struct ctl_cmd_entry * entry)11521 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11522 {
11523
11524 switch (lun_type) {
11525 case T_DIRECT:
11526 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0)
11527 return (0);
11528 break;
11529 case T_PROCESSOR:
11530 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11531 return (0);
11532 break;
11533 case T_CDROM:
11534 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0)
11535 return (0);
11536 break;
11537 default:
11538 return (0);
11539 }
11540 return (1);
11541 }
11542
11543 static int
ctl_scsiio(struct ctl_scsiio * ctsio)11544 ctl_scsiio(struct ctl_scsiio *ctsio)
11545 {
11546 int retval;
11547 const struct ctl_cmd_entry *entry;
11548
11549 retval = CTL_RETVAL_COMPLETE;
11550
11551 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11552
11553 entry = ctl_get_cmd_entry(ctsio, NULL);
11554
11555 /*
11556 * If this I/O has been aborted, just send it straight to
11557 * ctl_done() without executing it.
11558 */
11559 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11560 ctl_done((union ctl_io *)ctsio);
11561 goto bailout;
11562 }
11563
11564 /*
11565 * All the checks should have been handled by ctl_scsiio_precheck().
11566 * We should be clear now to just execute the I/O.
11567 */
11568 retval = entry->execute(ctsio);
11569
11570 bailout:
11571 return (retval);
11572 }
11573
11574 static int
ctl_target_reset(union ctl_io * io)11575 ctl_target_reset(union ctl_io *io)
11576 {
11577 struct ctl_softc *softc = CTL_SOFTC(io);
11578 struct ctl_port *port = CTL_PORT(io);
11579 struct ctl_lun *lun;
11580 uint32_t initidx;
11581 ctl_ua_type ua_type;
11582
11583 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11584 union ctl_ha_msg msg_info;
11585
11586 msg_info.hdr.nexus = io->io_hdr.nexus;
11587 msg_info.task.task_action = io->taskio.task_action;
11588 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11589 msg_info.hdr.original_sc = NULL;
11590 msg_info.hdr.serializing_sc = NULL;
11591 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11592 sizeof(msg_info.task), M_WAITOK);
11593 }
11594
11595 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11596 if (io->taskio.task_action == CTL_TASK_TARGET_RESET)
11597 ua_type = CTL_UA_TARG_RESET;
11598 else
11599 ua_type = CTL_UA_BUS_RESET;
11600 mtx_lock(&softc->ctl_lock);
11601 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11602 if (port != NULL &&
11603 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
11604 continue;
11605 ctl_do_lun_reset(lun, initidx, ua_type);
11606 }
11607 mtx_unlock(&softc->ctl_lock);
11608 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11609 return (0);
11610 }
11611
11612 /*
11613 * The LUN should always be set. The I/O is optional, and is used to
11614 * distinguish between I/Os sent by this initiator, and by other
11615 * initiators. We set unit attention for initiators other than this one.
11616 * SAM-3 is vague on this point. It does say that a unit attention should
11617 * be established for other initiators when a LUN is reset (see section
11618 * 5.7.3), but it doesn't specifically say that the unit attention should
11619 * be established for this particular initiator when a LUN is reset. Here
11620 * is the relevant text, from SAM-3 rev 8:
11621 *
11622 * 5.7.2 When a SCSI initiator port aborts its own tasks
11623 *
11624 * When a SCSI initiator port causes its own task(s) to be aborted, no
11625 * notification that the task(s) have been aborted shall be returned to
11626 * the SCSI initiator port other than the completion response for the
11627 * command or task management function action that caused the task(s) to
11628 * be aborted and notification(s) associated with related effects of the
11629 * action (e.g., a reset unit attention condition).
11630 *
11631 * XXX KDM for now, we're setting unit attention for all initiators.
11632 */
11633 static void
ctl_do_lun_reset(struct ctl_lun * lun,uint32_t initidx,ctl_ua_type ua_type)11634 ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua_type)
11635 {
11636 union ctl_io *xio;
11637 int i;
11638
11639 mtx_lock(&lun->lun_lock);
11640 /* Abort tasks. */
11641 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11642 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11643 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11644 ctl_try_unblock_io(lun, xio, FALSE);
11645 }
11646 /* Clear CA. */
11647 for (i = 0; i < ctl_max_ports; i++) {
11648 free(lun->pending_sense[i], M_CTL);
11649 lun->pending_sense[i] = NULL;
11650 }
11651 /* Clear reservation. */
11652 lun->flags &= ~CTL_LUN_RESERVED;
11653 /* Clear prevent media removal. */
11654 if (lun->prevent) {
11655 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11656 ctl_clear_mask(lun->prevent, i);
11657 lun->prevent_count = 0;
11658 }
11659 /* Clear TPC status */
11660 ctl_tpc_lun_clear(lun, -1);
11661 /* Establish UA. */
11662 #if 0
11663 ctl_est_ua_all(lun, initidx, ua_type);
11664 #else
11665 ctl_est_ua_all(lun, -1, ua_type);
11666 #endif
11667 mtx_unlock(&lun->lun_lock);
11668 }
11669
11670 static int
ctl_lun_reset(union ctl_io * io)11671 ctl_lun_reset(union ctl_io *io)
11672 {
11673 struct ctl_softc *softc = CTL_SOFTC(io);
11674 struct ctl_lun *lun;
11675 uint32_t targ_lun, initidx;
11676
11677 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11678 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11679 mtx_lock(&softc->ctl_lock);
11680 if (targ_lun >= ctl_max_luns ||
11681 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11682 mtx_unlock(&softc->ctl_lock);
11683 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11684 return (1);
11685 }
11686 ctl_do_lun_reset(lun, initidx, CTL_UA_LUN_RESET);
11687 mtx_unlock(&softc->ctl_lock);
11688 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11689
11690 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
11691 union ctl_ha_msg msg_info;
11692
11693 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11694 msg_info.hdr.nexus = io->io_hdr.nexus;
11695 msg_info.task.task_action = CTL_TASK_LUN_RESET;
11696 msg_info.hdr.original_sc = NULL;
11697 msg_info.hdr.serializing_sc = NULL;
11698 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11699 sizeof(msg_info.task), M_WAITOK);
11700 }
11701 return (0);
11702 }
11703
11704 static void
ctl_abort_tasks_lun(struct ctl_lun * lun,uint32_t targ_port,uint32_t init_id,int other_sc)11705 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11706 int other_sc)
11707 {
11708 union ctl_io *xio;
11709
11710 mtx_assert(&lun->lun_lock, MA_OWNED);
11711
11712 /*
11713 * Run through the OOA queue and attempt to find the given I/O.
11714 * The target port, initiator ID, tag type and tag number have to
11715 * match the values that we got from the initiator. If we have an
11716 * untagged command to abort, simply abort the first untagged command
11717 * we come to. We only allow one untagged command at a time of course.
11718 */
11719 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11720 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11721
11722 if ((targ_port == UINT32_MAX ||
11723 targ_port == xio->io_hdr.nexus.targ_port) &&
11724 (init_id == UINT32_MAX ||
11725 init_id == xio->io_hdr.nexus.initid)) {
11726 if (targ_port != xio->io_hdr.nexus.targ_port ||
11727 init_id != xio->io_hdr.nexus.initid)
11728 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
11729 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11730 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11731 union ctl_ha_msg msg_info;
11732
11733 msg_info.hdr.nexus = xio->io_hdr.nexus;
11734 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11735 msg_info.task.tag_num = xio->scsiio.tag_num;
11736 msg_info.task.tag_type = xio->scsiio.tag_type;
11737 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11738 msg_info.hdr.original_sc = NULL;
11739 msg_info.hdr.serializing_sc = NULL;
11740 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11741 sizeof(msg_info.task), M_NOWAIT);
11742 }
11743 ctl_try_unblock_io(lun, xio, FALSE);
11744 }
11745 }
11746 }
11747
11748 static int
ctl_abort_task_set(union ctl_io * io)11749 ctl_abort_task_set(union ctl_io *io)
11750 {
11751 struct ctl_softc *softc = CTL_SOFTC(io);
11752 struct ctl_lun *lun;
11753 uint32_t targ_lun;
11754
11755 /*
11756 * Look up the LUN.
11757 */
11758 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11759 mtx_lock(&softc->ctl_lock);
11760 if (targ_lun >= ctl_max_luns ||
11761 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11762 mtx_unlock(&softc->ctl_lock);
11763 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11764 return (1);
11765 }
11766
11767 mtx_lock(&lun->lun_lock);
11768 mtx_unlock(&softc->ctl_lock);
11769 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
11770 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11771 io->io_hdr.nexus.initid,
11772 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11773 } else { /* CTL_TASK_CLEAR_TASK_SET */
11774 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
11775 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11776 }
11777 mtx_unlock(&lun->lun_lock);
11778 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11779 return (0);
11780 }
11781
11782 static void
ctl_i_t_nexus_loss(struct ctl_softc * softc,uint32_t initidx,ctl_ua_type ua_type)11783 ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx,
11784 ctl_ua_type ua_type)
11785 {
11786 struct ctl_lun *lun;
11787 struct scsi_sense_data *ps;
11788 uint32_t p, i;
11789
11790 p = initidx / CTL_MAX_INIT_PER_PORT;
11791 i = initidx % CTL_MAX_INIT_PER_PORT;
11792 mtx_lock(&softc->ctl_lock);
11793 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11794 mtx_lock(&lun->lun_lock);
11795 /* Abort tasks. */
11796 ctl_abort_tasks_lun(lun, p, i, 1);
11797 /* Clear CA. */
11798 ps = lun->pending_sense[p];
11799 if (ps != NULL)
11800 ps[i].error_code = 0;
11801 /* Clear reservation. */
11802 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
11803 lun->flags &= ~CTL_LUN_RESERVED;
11804 /* Clear prevent media removal. */
11805 if (lun->prevent && ctl_is_set(lun->prevent, initidx)) {
11806 ctl_clear_mask(lun->prevent, initidx);
11807 lun->prevent_count--;
11808 }
11809 /* Clear TPC status */
11810 ctl_tpc_lun_clear(lun, initidx);
11811 /* Establish UA. */
11812 ctl_est_ua(lun, initidx, ua_type);
11813 mtx_unlock(&lun->lun_lock);
11814 }
11815 mtx_unlock(&softc->ctl_lock);
11816 }
11817
11818 static int
ctl_i_t_nexus_reset(union ctl_io * io)11819 ctl_i_t_nexus_reset(union ctl_io *io)
11820 {
11821 struct ctl_softc *softc = CTL_SOFTC(io);
11822 uint32_t initidx;
11823
11824 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11825 union ctl_ha_msg msg_info;
11826
11827 msg_info.hdr.nexus = io->io_hdr.nexus;
11828 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
11829 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11830 msg_info.hdr.original_sc = NULL;
11831 msg_info.hdr.serializing_sc = NULL;
11832 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11833 sizeof(msg_info.task), M_WAITOK);
11834 }
11835
11836 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11837 ctl_i_t_nexus_loss(softc, initidx, CTL_UA_I_T_NEXUS_LOSS);
11838 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11839 return (0);
11840 }
11841
11842 static int
ctl_abort_task(union ctl_io * io)11843 ctl_abort_task(union ctl_io *io)
11844 {
11845 struct ctl_softc *softc = CTL_SOFTC(io);
11846 union ctl_io *xio;
11847 struct ctl_lun *lun;
11848 uint32_t targ_lun;
11849
11850 /*
11851 * Look up the LUN.
11852 */
11853 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11854 mtx_lock(&softc->ctl_lock);
11855 if (targ_lun >= ctl_max_luns ||
11856 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11857 mtx_unlock(&softc->ctl_lock);
11858 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11859 return (1);
11860 }
11861
11862 mtx_lock(&lun->lun_lock);
11863 mtx_unlock(&softc->ctl_lock);
11864 /*
11865 * Run through the OOA queue and attempt to find the given I/O.
11866 * The target port, initiator ID, tag type and tag number have to
11867 * match the values that we got from the initiator. If we have an
11868 * untagged command to abort, simply abort the first untagged command
11869 * we come to. We only allow one untagged command at a time of course.
11870 */
11871 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11872 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11873
11874 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
11875 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
11876 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
11877 continue;
11878
11879 /*
11880 * If the abort says that the task is untagged, the
11881 * task in the queue must be untagged. Otherwise,
11882 * we just check to see whether the tag numbers
11883 * match. This is because the QLogic firmware
11884 * doesn't pass back the tag type in an abort
11885 * request.
11886 */
11887 #if 0
11888 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
11889 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
11890 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
11891 #else
11892 /*
11893 * XXX KDM we've got problems with FC, because it
11894 * doesn't send down a tag type with aborts. So we
11895 * can only really go by the tag number...
11896 * This may cause problems with parallel SCSI.
11897 * Need to figure that out!!
11898 */
11899 if (xio->scsiio.tag_num == io->taskio.tag_num) {
11900 #endif
11901 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11902 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
11903 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11904 union ctl_ha_msg msg_info;
11905
11906 msg_info.hdr.nexus = io->io_hdr.nexus;
11907 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11908 msg_info.task.tag_num = io->taskio.tag_num;
11909 msg_info.task.tag_type = io->taskio.tag_type;
11910 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11911 msg_info.hdr.original_sc = NULL;
11912 msg_info.hdr.serializing_sc = NULL;
11913 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11914 sizeof(msg_info.task), M_NOWAIT);
11915 }
11916 ctl_try_unblock_io(lun, xio, FALSE);
11917 }
11918 }
11919 mtx_unlock(&lun->lun_lock);
11920 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11921 return (0);
11922 }
11923
11924 static int
11925 ctl_query_task(union ctl_io *io, int task_set)
11926 {
11927 struct ctl_softc *softc = CTL_SOFTC(io);
11928 union ctl_io *xio;
11929 struct ctl_lun *lun;
11930 int found = 0;
11931 uint32_t targ_lun;
11932
11933 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11934 mtx_lock(&softc->ctl_lock);
11935 if (targ_lun >= ctl_max_luns ||
11936 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11937 mtx_unlock(&softc->ctl_lock);
11938 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11939 return (1);
11940 }
11941 mtx_lock(&lun->lun_lock);
11942 mtx_unlock(&softc->ctl_lock);
11943 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11944 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11945
11946 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
11947 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
11948 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
11949 continue;
11950
11951 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
11952 found = 1;
11953 break;
11954 }
11955 }
11956 mtx_unlock(&lun->lun_lock);
11957 if (found)
11958 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
11959 else
11960 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11961 return (0);
11962 }
11963
11964 static int
11965 ctl_query_async_event(union ctl_io *io)
11966 {
11967 struct ctl_softc *softc = CTL_SOFTC(io);
11968 struct ctl_lun *lun;
11969 ctl_ua_type ua;
11970 uint32_t targ_lun, initidx;
11971
11972 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11973 mtx_lock(&softc->ctl_lock);
11974 if (targ_lun >= ctl_max_luns ||
11975 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11976 mtx_unlock(&softc->ctl_lock);
11977 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11978 return (1);
11979 }
11980 mtx_lock(&lun->lun_lock);
11981 mtx_unlock(&softc->ctl_lock);
11982 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11983 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
11984 mtx_unlock(&lun->lun_lock);
11985 if (ua != CTL_UA_NONE)
11986 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
11987 else
11988 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11989 return (0);
11990 }
11991
11992 static void
11993 ctl_run_task(union ctl_io *io)
11994 {
11995 int retval = 1;
11996
11997 CTL_DEBUG_PRINT(("ctl_run_task\n"));
11998 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
11999 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
12000 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
12001 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
12002 switch (io->taskio.task_action) {
12003 case CTL_TASK_ABORT_TASK:
12004 retval = ctl_abort_task(io);
12005 break;
12006 case CTL_TASK_ABORT_TASK_SET:
12007 case CTL_TASK_CLEAR_TASK_SET:
12008 retval = ctl_abort_task_set(io);
12009 break;
12010 case CTL_TASK_CLEAR_ACA:
12011 break;
12012 case CTL_TASK_I_T_NEXUS_RESET:
12013 retval = ctl_i_t_nexus_reset(io);
12014 break;
12015 case CTL_TASK_LUN_RESET:
12016 retval = ctl_lun_reset(io);
12017 break;
12018 case CTL_TASK_TARGET_RESET:
12019 case CTL_TASK_BUS_RESET:
12020 retval = ctl_target_reset(io);
12021 break;
12022 case CTL_TASK_PORT_LOGIN:
12023 break;
12024 case CTL_TASK_PORT_LOGOUT:
12025 break;
12026 case CTL_TASK_QUERY_TASK:
12027 retval = ctl_query_task(io, 0);
12028 break;
12029 case CTL_TASK_QUERY_TASK_SET:
12030 retval = ctl_query_task(io, 1);
12031 break;
12032 case CTL_TASK_QUERY_ASYNC_EVENT:
12033 retval = ctl_query_async_event(io);
12034 break;
12035 default:
12036 printf("%s: got unknown task management event %d\n",
12037 __func__, io->taskio.task_action);
12038 break;
12039 }
12040 if (retval == 0)
12041 io->io_hdr.status = CTL_SUCCESS;
12042 else
12043 io->io_hdr.status = CTL_ERROR;
12044 ctl_done(io);
12045 }
12046
12047 /*
12048 * For HA operation. Handle commands that come in from the other
12049 * controller.
12050 */
12051 static void
12052 ctl_handle_isc(union ctl_io *io)
12053 {
12054 struct ctl_softc *softc = CTL_SOFTC(io);
12055 struct ctl_lun *lun;
12056 const struct ctl_cmd_entry *entry;
12057 uint32_t targ_lun;
12058
12059 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12060 switch (io->io_hdr.msg_type) {
12061 case CTL_MSG_SERIALIZE:
12062 ctl_serialize_other_sc_cmd(&io->scsiio);
12063 break;
12064 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */
12065 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12066 if (targ_lun >= ctl_max_luns ||
12067 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12068 ctl_done(io);
12069 break;
12070 }
12071 mtx_lock(&lun->lun_lock);
12072 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) {
12073 mtx_unlock(&lun->lun_lock);
12074 ctl_done(io);
12075 break;
12076 }
12077 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12078 mtx_unlock(&lun->lun_lock);
12079 ctl_enqueue_rtr(io);
12080 break;
12081 case CTL_MSG_FINISH_IO:
12082 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12083 ctl_done(io);
12084 break;
12085 }
12086 if (targ_lun >= ctl_max_luns ||
12087 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12088 ctl_free_io(io);
12089 break;
12090 }
12091 mtx_lock(&lun->lun_lock);
12092 ctl_try_unblock_others(lun, io, TRUE);
12093 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12094 mtx_unlock(&lun->lun_lock);
12095 ctl_free_io(io);
12096 break;
12097 case CTL_MSG_PERS_ACTION:
12098 ctl_hndl_per_res_out_on_other_sc(io);
12099 ctl_free_io(io);
12100 break;
12101 case CTL_MSG_BAD_JUJU:
12102 ctl_done(io);
12103 break;
12104 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */
12105 ctl_datamove_remote(io);
12106 break;
12107 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */
12108 io->scsiio.be_move_done(io);
12109 break;
12110 case CTL_MSG_FAILOVER:
12111 ctl_failover_lun(io);
12112 ctl_free_io(io);
12113 break;
12114 default:
12115 printf("%s: Invalid message type %d\n",
12116 __func__, io->io_hdr.msg_type);
12117 ctl_free_io(io);
12118 break;
12119 }
12120
12121 }
12122
12123
12124 /*
12125 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12126 * there is no match.
12127 */
12128 static ctl_lun_error_pattern
12129 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12130 {
12131 const struct ctl_cmd_entry *entry;
12132 ctl_lun_error_pattern filtered_pattern, pattern;
12133
12134 pattern = desc->error_pattern;
12135
12136 /*
12137 * XXX KDM we need more data passed into this function to match a
12138 * custom pattern, and we actually need to implement custom pattern
12139 * matching.
12140 */
12141 if (pattern & CTL_LUN_PAT_CMD)
12142 return (CTL_LUN_PAT_CMD);
12143
12144 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12145 return (CTL_LUN_PAT_ANY);
12146
12147 entry = ctl_get_cmd_entry(ctsio, NULL);
12148
12149 filtered_pattern = entry->pattern & pattern;
12150
12151 /*
12152 * If the user requested specific flags in the pattern (e.g.
12153 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12154 * flags.
12155 *
12156 * If the user did not specify any flags, it doesn't matter whether
12157 * or not the command supports the flags.
12158 */
12159 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12160 (pattern & ~CTL_LUN_PAT_MASK))
12161 return (CTL_LUN_PAT_NONE);
12162
12163 /*
12164 * If the user asked for a range check, see if the requested LBA
12165 * range overlaps with this command's LBA range.
12166 */
12167 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12168 uint64_t lba1;
12169 uint64_t len1;
12170 ctl_action action;
12171 int retval;
12172
12173 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12174 if (retval != 0)
12175 return (CTL_LUN_PAT_NONE);
12176
12177 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12178 desc->lba_range.len, FALSE);
12179 /*
12180 * A "pass" means that the LBA ranges don't overlap, so
12181 * this doesn't match the user's range criteria.
12182 */
12183 if (action == CTL_ACTION_PASS)
12184 return (CTL_LUN_PAT_NONE);
12185 }
12186
12187 return (filtered_pattern);
12188 }
12189
12190 static void
12191 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12192 {
12193 struct ctl_error_desc *desc, *desc2;
12194
12195 mtx_assert(&lun->lun_lock, MA_OWNED);
12196
12197 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12198 ctl_lun_error_pattern pattern;
12199 /*
12200 * Check to see whether this particular command matches
12201 * the pattern in the descriptor.
12202 */
12203 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12204 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12205 continue;
12206
12207 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12208 case CTL_LUN_INJ_ABORTED:
12209 ctl_set_aborted(&io->scsiio);
12210 break;
12211 case CTL_LUN_INJ_MEDIUM_ERR:
12212 ctl_set_medium_error(&io->scsiio,
12213 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
12214 CTL_FLAG_DATA_OUT);
12215 break;
12216 case CTL_LUN_INJ_UA:
12217 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12218 * OCCURRED */
12219 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12220 break;
12221 case CTL_LUN_INJ_CUSTOM:
12222 /*
12223 * We're assuming the user knows what he is doing.
12224 * Just copy the sense information without doing
12225 * checks.
12226 */
12227 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12228 MIN(sizeof(desc->custom_sense),
12229 sizeof(io->scsiio.sense_data)));
12230 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12231 io->scsiio.sense_len = SSD_FULL_SIZE;
12232 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12233 break;
12234 case CTL_LUN_INJ_NONE:
12235 default:
12236 /*
12237 * If this is an error injection type we don't know
12238 * about, clear the continuous flag (if it is set)
12239 * so it will get deleted below.
12240 */
12241 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12242 break;
12243 }
12244 /*
12245 * By default, each error injection action is a one-shot
12246 */
12247 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12248 continue;
12249
12250 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12251
12252 free(desc, M_CTL);
12253 }
12254 }
12255
12256 #ifdef CTL_IO_DELAY
12257 static void
12258 ctl_datamove_timer_wakeup(void *arg)
12259 {
12260 union ctl_io *io;
12261
12262 io = (union ctl_io *)arg;
12263
12264 ctl_datamove(io);
12265 }
12266 #endif /* CTL_IO_DELAY */
12267
12268 void
12269 ctl_datamove(union ctl_io *io)
12270 {
12271 void (*fe_datamove)(union ctl_io *io);
12272
12273 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12274
12275 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12276
12277 /* No data transferred yet. Frontend must update this when done. */
12278 io->scsiio.kern_data_resid = io->scsiio.kern_data_len;
12279
12280 #ifdef CTL_TIME_IO
12281 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12282 char str[256];
12283 char path_str[64];
12284 struct sbuf sb;
12285
12286 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12287 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12288
12289 sbuf_cat(&sb, path_str);
12290 switch (io->io_hdr.io_type) {
12291 case CTL_IO_SCSI:
12292 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12293 sbuf_printf(&sb, "\n");
12294 sbuf_cat(&sb, path_str);
12295 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12296 io->scsiio.tag_num, io->scsiio.tag_type);
12297 break;
12298 case CTL_IO_TASK:
12299 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12300 "Tag Type: %d\n", io->taskio.task_action,
12301 io->taskio.tag_num, io->taskio.tag_type);
12302 break;
12303 default:
12304 panic("%s: Invalid CTL I/O type %d\n",
12305 __func__, io->io_hdr.io_type);
12306 }
12307 sbuf_cat(&sb, path_str);
12308 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12309 (intmax_t)time_uptime - io->io_hdr.start_time);
12310 sbuf_finish(&sb);
12311 printf("%s", sbuf_data(&sb));
12312 }
12313 #endif /* CTL_TIME_IO */
12314
12315 #ifdef CTL_IO_DELAY
12316 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12317 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12318 } else {
12319 struct ctl_lun *lun;
12320
12321 lun = CTL_LUN(io);
12322 if ((lun != NULL)
12323 && (lun->delay_info.datamove_delay > 0)) {
12324
12325 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12326 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12327 callout_reset(&io->io_hdr.delay_callout,
12328 lun->delay_info.datamove_delay * hz,
12329 ctl_datamove_timer_wakeup, io);
12330 if (lun->delay_info.datamove_type ==
12331 CTL_DELAY_TYPE_ONESHOT)
12332 lun->delay_info.datamove_delay = 0;
12333 return;
12334 }
12335 }
12336 #endif
12337
12338 /*
12339 * This command has been aborted. Set the port status, so we fail
12340 * the data move.
12341 */
12342 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12343 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12344 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12345 io->io_hdr.nexus.targ_port,
12346 io->io_hdr.nexus.targ_lun);
12347 io->io_hdr.port_status = 31337;
12348 /*
12349 * Note that the backend, in this case, will get the
12350 * callback in its context. In other cases it may get
12351 * called in the frontend's interrupt thread context.
12352 */
12353 io->scsiio.be_move_done(io);
12354 return;
12355 }
12356
12357 /* Don't confuse frontend with zero length data move. */
12358 if (io->scsiio.kern_data_len == 0) {
12359 io->scsiio.be_move_done(io);
12360 return;
12361 }
12362
12363 fe_datamove = CTL_PORT(io)->fe_datamove;
12364 fe_datamove(io);
12365 }
12366
12367 static void
12368 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12369 {
12370 union ctl_ha_msg msg;
12371 #ifdef CTL_TIME_IO
12372 struct bintime cur_bt;
12373 #endif
12374
12375 memset(&msg, 0, sizeof(msg));
12376 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12377 msg.hdr.original_sc = io;
12378 msg.hdr.serializing_sc = io->io_hdr.remote_io;
12379 msg.hdr.nexus = io->io_hdr.nexus;
12380 msg.hdr.status = io->io_hdr.status;
12381 msg.scsi.kern_data_resid = io->scsiio.kern_data_resid;
12382 msg.scsi.tag_num = io->scsiio.tag_num;
12383 msg.scsi.tag_type = io->scsiio.tag_type;
12384 msg.scsi.scsi_status = io->scsiio.scsi_status;
12385 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12386 io->scsiio.sense_len);
12387 msg.scsi.sense_len = io->scsiio.sense_len;
12388 msg.scsi.port_status = io->io_hdr.port_status;
12389 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12390 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12391 ctl_failover_io(io, /*have_lock*/ have_lock);
12392 return;
12393 }
12394 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12395 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12396 msg.scsi.sense_len, M_WAITOK);
12397
12398 #ifdef CTL_TIME_IO
12399 getbinuptime(&cur_bt);
12400 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12401 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12402 #endif
12403 io->io_hdr.num_dmas++;
12404 }
12405
12406 /*
12407 * The DMA to the remote side is done, now we need to tell the other side
12408 * we're done so it can continue with its data movement.
12409 */
12410 static void
12411 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12412 {
12413 union ctl_io *io;
12414 uint32_t i;
12415
12416 io = rq->context;
12417
12418 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12419 printf("%s: ISC DMA write failed with error %d", __func__,
12420 rq->ret);
12421 ctl_set_internal_failure(&io->scsiio,
12422 /*sks_valid*/ 1,
12423 /*retry_count*/ rq->ret);
12424 }
12425
12426 ctl_dt_req_free(rq);
12427
12428 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12429 free(CTL_LSGLT(io)[i].addr, M_CTL);
12430 free(CTL_RSGL(io), M_CTL);
12431 CTL_RSGL(io) = NULL;
12432 CTL_LSGL(io) = NULL;
12433
12434 /*
12435 * The data is in local and remote memory, so now we need to send
12436 * status (good or back) back to the other side.
12437 */
12438 ctl_send_datamove_done(io, /*have_lock*/ 0);
12439 }
12440
12441 /*
12442 * We've moved the data from the host/controller into local memory. Now we
12443 * need to push it over to the remote controller's memory.
12444 */
12445 static int
12446 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12447 {
12448 int retval;
12449
12450 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12451 ctl_datamove_remote_write_cb);
12452 return (retval);
12453 }
12454
12455 static void
12456 ctl_datamove_remote_write(union ctl_io *io)
12457 {
12458 int retval;
12459 void (*fe_datamove)(union ctl_io *io);
12460
12461 /*
12462 * - Get the data from the host/HBA into local memory.
12463 * - DMA memory from the local controller to the remote controller.
12464 * - Send status back to the remote controller.
12465 */
12466
12467 retval = ctl_datamove_remote_sgl_setup(io);
12468 if (retval != 0)
12469 return;
12470
12471 /* Switch the pointer over so the FETD knows what to do */
12472 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io);
12473
12474 /*
12475 * Use a custom move done callback, since we need to send completion
12476 * back to the other controller, not to the backend on this side.
12477 */
12478 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12479
12480 fe_datamove = CTL_PORT(io)->fe_datamove;
12481 fe_datamove(io);
12482 }
12483
12484 static int
12485 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12486 {
12487 uint32_t i;
12488
12489 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12490 free(CTL_LSGLT(io)[i].addr, M_CTL);
12491 free(CTL_RSGL(io), M_CTL);
12492 CTL_RSGL(io) = NULL;
12493 CTL_LSGL(io) = NULL;
12494
12495 /*
12496 * The read is done, now we need to send status (good or bad) back
12497 * to the other side.
12498 */
12499 ctl_send_datamove_done(io, /*have_lock*/ 0);
12500
12501 return (0);
12502 }
12503
12504 static void
12505 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12506 {
12507 union ctl_io *io;
12508 void (*fe_datamove)(union ctl_io *io);
12509
12510 io = rq->context;
12511
12512 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12513 printf("%s: ISC DMA read failed with error %d\n", __func__,
12514 rq->ret);
12515 ctl_set_internal_failure(&io->scsiio,
12516 /*sks_valid*/ 1,
12517 /*retry_count*/ rq->ret);
12518 }
12519
12520 ctl_dt_req_free(rq);
12521
12522 /* Switch the pointer over so the FETD knows what to do */
12523 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io);
12524
12525 /*
12526 * Use a custom move done callback, since we need to send completion
12527 * back to the other controller, not to the backend on this side.
12528 */
12529 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12530
12531 /* XXX KDM add checks like the ones in ctl_datamove? */
12532
12533 fe_datamove = CTL_PORT(io)->fe_datamove;
12534 fe_datamove(io);
12535 }
12536
12537 static int
12538 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12539 {
12540 struct ctl_sg_entry *local_sglist;
12541 uint32_t len_to_go;
12542 int retval;
12543 int i;
12544
12545 retval = 0;
12546 local_sglist = CTL_LSGL(io);
12547 len_to_go = io->scsiio.kern_data_len;
12548
12549 /*
12550 * The difficult thing here is that the size of the various
12551 * S/G segments may be different than the size from the
12552 * remote controller. That'll make it harder when DMAing
12553 * the data back to the other side.
12554 */
12555 for (i = 0; len_to_go > 0; i++) {
12556 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12557 local_sglist[i].addr =
12558 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12559
12560 len_to_go -= local_sglist[i].len;
12561 }
12562 /*
12563 * Reset the number of S/G entries accordingly. The original
12564 * number of S/G entries is available in rem_sg_entries.
12565 */
12566 io->scsiio.kern_sg_entries = i;
12567
12568 return (retval);
12569 }
12570
12571 static int
12572 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12573 ctl_ha_dt_cb callback)
12574 {
12575 struct ctl_ha_dt_req *rq;
12576 struct ctl_sg_entry *remote_sglist, *local_sglist;
12577 uint32_t local_used, remote_used, total_used;
12578 int i, j, isc_ret;
12579
12580 rq = ctl_dt_req_alloc();
12581
12582 /*
12583 * If we failed to allocate the request, and if the DMA didn't fail
12584 * anyway, set busy status. This is just a resource allocation
12585 * failure.
12586 */
12587 if ((rq == NULL)
12588 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12589 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12590 ctl_set_busy(&io->scsiio);
12591
12592 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12593 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12594
12595 if (rq != NULL)
12596 ctl_dt_req_free(rq);
12597
12598 /*
12599 * The data move failed. We need to return status back
12600 * to the other controller. No point in trying to DMA
12601 * data to the remote controller.
12602 */
12603
12604 ctl_send_datamove_done(io, /*have_lock*/ 0);
12605
12606 return (1);
12607 }
12608
12609 local_sglist = CTL_LSGL(io);
12610 remote_sglist = CTL_RSGL(io);
12611 local_used = 0;
12612 remote_used = 0;
12613 total_used = 0;
12614
12615 /*
12616 * Pull/push the data over the wire from/to the other controller.
12617 * This takes into account the possibility that the local and
12618 * remote sglists may not be identical in terms of the size of
12619 * the elements and the number of elements.
12620 *
12621 * One fundamental assumption here is that the length allocated for
12622 * both the local and remote sglists is identical. Otherwise, we've
12623 * essentially got a coding error of some sort.
12624 */
12625 isc_ret = CTL_HA_STATUS_SUCCESS;
12626 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12627 uint32_t cur_len;
12628 uint8_t *tmp_ptr;
12629
12630 rq->command = command;
12631 rq->context = io;
12632
12633 /*
12634 * Both pointers should be aligned. But it is possible
12635 * that the allocation length is not. They should both
12636 * also have enough slack left over at the end, though,
12637 * to round up to the next 8 byte boundary.
12638 */
12639 cur_len = MIN(local_sglist[i].len - local_used,
12640 remote_sglist[j].len - remote_used);
12641 rq->size = cur_len;
12642
12643 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12644 tmp_ptr += local_used;
12645
12646 #if 0
12647 /* Use physical addresses when talking to ISC hardware */
12648 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12649 /* XXX KDM use busdma */
12650 rq->local = vtophys(tmp_ptr);
12651 } else
12652 rq->local = tmp_ptr;
12653 #else
12654 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12655 ("HA does not support BUS_ADDR"));
12656 rq->local = tmp_ptr;
12657 #endif
12658
12659 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12660 tmp_ptr += remote_used;
12661 rq->remote = tmp_ptr;
12662
12663 rq->callback = NULL;
12664
12665 local_used += cur_len;
12666 if (local_used >= local_sglist[i].len) {
12667 i++;
12668 local_used = 0;
12669 }
12670
12671 remote_used += cur_len;
12672 if (remote_used >= remote_sglist[j].len) {
12673 j++;
12674 remote_used = 0;
12675 }
12676 total_used += cur_len;
12677
12678 if (total_used >= io->scsiio.kern_data_len)
12679 rq->callback = callback;
12680
12681 isc_ret = ctl_dt_single(rq);
12682 if (isc_ret > CTL_HA_STATUS_SUCCESS)
12683 break;
12684 }
12685 if (isc_ret != CTL_HA_STATUS_WAIT) {
12686 rq->ret = isc_ret;
12687 callback(rq);
12688 }
12689
12690 return (0);
12691 }
12692
12693 static void
12694 ctl_datamove_remote_read(union ctl_io *io)
12695 {
12696 int retval;
12697 uint32_t i;
12698
12699 /*
12700 * This will send an error to the other controller in the case of a
12701 * failure.
12702 */
12703 retval = ctl_datamove_remote_sgl_setup(io);
12704 if (retval != 0)
12705 return;
12706
12707 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12708 ctl_datamove_remote_read_cb);
12709 if (retval != 0) {
12710 /*
12711 * Make sure we free memory if there was an error.. The
12712 * ctl_datamove_remote_xfer() function will send the
12713 * datamove done message, or call the callback with an
12714 * error if there is a problem.
12715 */
12716 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12717 free(CTL_LSGLT(io)[i].addr, M_CTL);
12718 free(CTL_RSGL(io), M_CTL);
12719 CTL_RSGL(io) = NULL;
12720 CTL_LSGL(io) = NULL;
12721 }
12722 }
12723
12724 /*
12725 * Process a datamove request from the other controller. This is used for
12726 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12727 * first. Once that is complete, the data gets DMAed into the remote
12728 * controller's memory. For reads, we DMA from the remote controller's
12729 * memory into our memory first, and then move it out to the FETD.
12730 */
12731 static void
12732 ctl_datamove_remote(union ctl_io *io)
12733 {
12734
12735 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12736
12737 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12738 ctl_failover_io(io, /*have_lock*/ 0);
12739 return;
12740 }
12741
12742 /*
12743 * Note that we look for an aborted I/O here, but don't do some of
12744 * the other checks that ctl_datamove() normally does.
12745 * We don't need to run the datamove delay code, since that should
12746 * have been done if need be on the other controller.
12747 */
12748 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12749 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
12750 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12751 io->io_hdr.nexus.targ_port,
12752 io->io_hdr.nexus.targ_lun);
12753 io->io_hdr.port_status = 31338;
12754 ctl_send_datamove_done(io, /*have_lock*/ 0);
12755 return;
12756 }
12757
12758 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
12759 ctl_datamove_remote_write(io);
12760 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
12761 ctl_datamove_remote_read(io);
12762 else {
12763 io->io_hdr.port_status = 31339;
12764 ctl_send_datamove_done(io, /*have_lock*/ 0);
12765 }
12766 }
12767
12768 static void
12769 ctl_process_done(union ctl_io *io)
12770 {
12771 struct ctl_softc *softc = CTL_SOFTC(io);
12772 struct ctl_port *port = CTL_PORT(io);
12773 struct ctl_lun *lun = CTL_LUN(io);
12774 void (*fe_done)(union ctl_io *io);
12775 union ctl_ha_msg msg;
12776
12777 CTL_DEBUG_PRINT(("ctl_process_done\n"));
12778 fe_done = port->fe_done;
12779
12780 #ifdef CTL_TIME_IO
12781 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12782 char str[256];
12783 char path_str[64];
12784 struct sbuf sb;
12785
12786 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12787 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12788
12789 sbuf_cat(&sb, path_str);
12790 switch (io->io_hdr.io_type) {
12791 case CTL_IO_SCSI:
12792 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12793 sbuf_printf(&sb, "\n");
12794 sbuf_cat(&sb, path_str);
12795 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12796 io->scsiio.tag_num, io->scsiio.tag_type);
12797 break;
12798 case CTL_IO_TASK:
12799 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12800 "Tag Type: %d\n", io->taskio.task_action,
12801 io->taskio.tag_num, io->taskio.tag_type);
12802 break;
12803 default:
12804 panic("%s: Invalid CTL I/O type %d\n",
12805 __func__, io->io_hdr.io_type);
12806 }
12807 sbuf_cat(&sb, path_str);
12808 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12809 (intmax_t)time_uptime - io->io_hdr.start_time);
12810 sbuf_finish(&sb);
12811 printf("%s", sbuf_data(&sb));
12812 }
12813 #endif /* CTL_TIME_IO */
12814
12815 switch (io->io_hdr.io_type) {
12816 case CTL_IO_SCSI:
12817 break;
12818 case CTL_IO_TASK:
12819 if (ctl_debug & CTL_DEBUG_INFO)
12820 ctl_io_error_print(io, NULL);
12821 fe_done(io);
12822 return;
12823 default:
12824 panic("%s: Invalid CTL I/O type %d\n",
12825 __func__, io->io_hdr.io_type);
12826 }
12827
12828 if (lun == NULL) {
12829 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
12830 io->io_hdr.nexus.targ_mapped_lun));
12831 goto bailout;
12832 }
12833
12834 mtx_lock(&lun->lun_lock);
12835
12836 /*
12837 * Check to see if we have any informational exception and status
12838 * of this command can be modified to report it in form of either
12839 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field.
12840 */
12841 if (lun->ie_reported == 0 && lun->ie_asc != 0 &&
12842 io->io_hdr.status == CTL_SUCCESS &&
12843 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) {
12844 uint8_t mrie = lun->MODE_IE.mrie;
12845 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) ||
12846 (lun->MODE_VER.byte3 & SMS_VER_PER));
12847 if (((mrie == SIEP_MRIE_REC_COND && per) ||
12848 mrie == SIEP_MRIE_REC_UNCOND ||
12849 mrie == SIEP_MRIE_NO_SENSE) &&
12850 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags &
12851 CTL_CMD_FLAG_NO_SENSE) == 0) {
12852 ctl_set_sense(&io->scsiio,
12853 /*current_error*/ 1,
12854 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ?
12855 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR,
12856 /*asc*/ lun->ie_asc,
12857 /*ascq*/ lun->ie_ascq,
12858 SSD_ELEM_NONE);
12859 lun->ie_reported = 1;
12860 }
12861 } else if (lun->ie_reported < 0)
12862 lun->ie_reported = 0;
12863
12864 /*
12865 * Check to see if we have any errors to inject here. We only
12866 * inject errors for commands that don't already have errors set.
12867 */
12868 if (!STAILQ_EMPTY(&lun->error_list) &&
12869 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
12870 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
12871 ctl_inject_error(lun, io);
12872
12873 /*
12874 * XXX KDM how do we treat commands that aren't completed
12875 * successfully?
12876 *
12877 * XXX KDM should we also track I/O latency?
12878 */
12879 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
12880 io->io_hdr.io_type == CTL_IO_SCSI) {
12881 int type;
12882 #ifdef CTL_TIME_IO
12883 struct bintime bt;
12884
12885 getbinuptime(&bt);
12886 bintime_sub(&bt, &io->io_hdr.start_bt);
12887 #endif
12888 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12889 CTL_FLAG_DATA_IN)
12890 type = CTL_STATS_READ;
12891 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12892 CTL_FLAG_DATA_OUT)
12893 type = CTL_STATS_WRITE;
12894 else
12895 type = CTL_STATS_NO_IO;
12896
12897 #ifdef CTL_LEGACY_STATS
12898 uint32_t targ_port = port->targ_port;
12899 lun->legacy_stats.ports[targ_port].bytes[type] +=
12900 io->scsiio.kern_total_len;
12901 lun->legacy_stats.ports[targ_port].operations[type] ++;
12902 lun->legacy_stats.ports[targ_port].num_dmas[type] +=
12903 io->io_hdr.num_dmas;
12904 #ifdef CTL_TIME_IO
12905 bintime_add(&lun->legacy_stats.ports[targ_port].dma_time[type],
12906 &io->io_hdr.dma_bt);
12907 bintime_add(&lun->legacy_stats.ports[targ_port].time[type],
12908 &bt);
12909 #endif
12910 #endif /* CTL_LEGACY_STATS */
12911
12912 lun->stats.bytes[type] += io->scsiio.kern_total_len;
12913 lun->stats.operations[type] ++;
12914 lun->stats.dmas[type] += io->io_hdr.num_dmas;
12915 #ifdef CTL_TIME_IO
12916 bintime_add(&lun->stats.dma_time[type], &io->io_hdr.dma_bt);
12917 bintime_add(&lun->stats.time[type], &bt);
12918 #endif
12919
12920 mtx_lock(&port->port_lock);
12921 port->stats.bytes[type] += io->scsiio.kern_total_len;
12922 port->stats.operations[type] ++;
12923 port->stats.dmas[type] += io->io_hdr.num_dmas;
12924 #ifdef CTL_TIME_IO
12925 bintime_add(&port->stats.dma_time[type], &io->io_hdr.dma_bt);
12926 bintime_add(&port->stats.time[type], &bt);
12927 #endif
12928 mtx_unlock(&port->port_lock);
12929 }
12930
12931 /*
12932 * Run through the blocked queue of this I/O and see if anything
12933 * can be unblocked, now that this I/O is done and will be removed.
12934 * We need to do it before removal to have OOA position to start.
12935 */
12936 ctl_try_unblock_others(lun, io, TRUE);
12937
12938 /*
12939 * Remove this from the OOA queue.
12940 */
12941 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12942 #ifdef CTL_TIME_IO
12943 if (TAILQ_EMPTY(&lun->ooa_queue))
12944 lun->last_busy = getsbinuptime();
12945 #endif
12946
12947 /*
12948 * If the LUN has been invalidated, free it if there is nothing
12949 * left on its OOA queue.
12950 */
12951 if ((lun->flags & CTL_LUN_INVALID)
12952 && TAILQ_EMPTY(&lun->ooa_queue)) {
12953 mtx_unlock(&lun->lun_lock);
12954 ctl_free_lun(lun);
12955 } else
12956 mtx_unlock(&lun->lun_lock);
12957
12958 bailout:
12959
12960 /*
12961 * If this command has been aborted, make sure we set the status
12962 * properly. The FETD is responsible for freeing the I/O and doing
12963 * whatever it needs to do to clean up its state.
12964 */
12965 if (io->io_hdr.flags & CTL_FLAG_ABORT)
12966 ctl_set_task_aborted(&io->scsiio);
12967
12968 /*
12969 * If enabled, print command error status.
12970 */
12971 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
12972 (ctl_debug & CTL_DEBUG_INFO) != 0)
12973 ctl_io_error_print(io, NULL);
12974
12975 /*
12976 * Tell the FETD or the other shelf controller we're done with this
12977 * command. Note that only SCSI commands get to this point. Task
12978 * management commands are completed above.
12979 */
12980 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
12981 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
12982 memset(&msg, 0, sizeof(msg));
12983 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
12984 msg.hdr.serializing_sc = io->io_hdr.remote_io;
12985 msg.hdr.nexus = io->io_hdr.nexus;
12986 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12987 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
12988 M_WAITOK);
12989 }
12990
12991 fe_done(io);
12992 }
12993
12994 /*
12995 * Front end should call this if it doesn't do autosense. When the request
12996 * sense comes back in from the initiator, we'll dequeue this and send it.
12997 */
12998 int
12999 ctl_queue_sense(union ctl_io *io)
13000 {
13001 struct ctl_softc *softc = CTL_SOFTC(io);
13002 struct ctl_port *port = CTL_PORT(io);
13003 struct ctl_lun *lun;
13004 struct scsi_sense_data *ps;
13005 uint32_t initidx, p, targ_lun;
13006
13007 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13008
13009 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13010
13011 /*
13012 * LUN lookup will likely move to the ctl_work_thread() once we
13013 * have our new queueing infrastructure (that doesn't put things on
13014 * a per-LUN queue initially). That is so that we can handle
13015 * things like an INQUIRY to a LUN that we don't have enabled. We
13016 * can't deal with that right now.
13017 * If we don't have a LUN for this, just toss the sense information.
13018 */
13019 mtx_lock(&softc->ctl_lock);
13020 if (targ_lun >= ctl_max_luns ||
13021 (lun = softc->ctl_luns[targ_lun]) == NULL) {
13022 mtx_unlock(&softc->ctl_lock);
13023 goto bailout;
13024 }
13025 mtx_lock(&lun->lun_lock);
13026 mtx_unlock(&softc->ctl_lock);
13027
13028 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13029 p = initidx / CTL_MAX_INIT_PER_PORT;
13030 if (lun->pending_sense[p] == NULL) {
13031 lun->pending_sense[p] = malloc(sizeof(*ps) * CTL_MAX_INIT_PER_PORT,
13032 M_CTL, M_NOWAIT | M_ZERO);
13033 }
13034 if ((ps = lun->pending_sense[p]) != NULL) {
13035 ps += initidx % CTL_MAX_INIT_PER_PORT;
13036 memset(ps, 0, sizeof(*ps));
13037 memcpy(ps, &io->scsiio.sense_data, io->scsiio.sense_len);
13038 }
13039 mtx_unlock(&lun->lun_lock);
13040
13041 bailout:
13042 ctl_free_io(io);
13043 return (CTL_RETVAL_COMPLETE);
13044 }
13045
13046 /*
13047 * Primary command inlet from frontend ports. All SCSI and task I/O
13048 * requests must go through this function.
13049 */
13050 int
13051 ctl_queue(union ctl_io *io)
13052 {
13053 struct ctl_port *port = CTL_PORT(io);
13054
13055 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13056
13057 #ifdef CTL_TIME_IO
13058 io->io_hdr.start_time = time_uptime;
13059 getbinuptime(&io->io_hdr.start_bt);
13060 #endif /* CTL_TIME_IO */
13061
13062 /* Map FE-specific LUN ID into global one. */
13063 io->io_hdr.nexus.targ_mapped_lun =
13064 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13065
13066 switch (io->io_hdr.io_type) {
13067 case CTL_IO_SCSI:
13068 case CTL_IO_TASK:
13069 if (ctl_debug & CTL_DEBUG_CDB)
13070 ctl_io_print(io);
13071 ctl_enqueue_incoming(io);
13072 break;
13073 default:
13074 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13075 return (EINVAL);
13076 }
13077
13078 return (CTL_RETVAL_COMPLETE);
13079 }
13080
13081 #ifdef CTL_IO_DELAY
13082 static void
13083 ctl_done_timer_wakeup(void *arg)
13084 {
13085 union ctl_io *io;
13086
13087 io = (union ctl_io *)arg;
13088 ctl_done(io);
13089 }
13090 #endif /* CTL_IO_DELAY */
13091
13092 void
13093 ctl_serseq_done(union ctl_io *io)
13094 {
13095 struct ctl_lun *lun = CTL_LUN(io);;
13096
13097 if (lun->be_lun == NULL ||
13098 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF)
13099 return;
13100 mtx_lock(&lun->lun_lock);
13101 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13102 ctl_try_unblock_others(lun, io, FALSE);
13103 mtx_unlock(&lun->lun_lock);
13104 }
13105
13106 void
13107 ctl_done(union ctl_io *io)
13108 {
13109
13110 /*
13111 * Enable this to catch duplicate completion issues.
13112 */
13113 #if 0
13114 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13115 printf("%s: type %d msg %d cdb %x iptl: "
13116 "%u:%u:%u tag 0x%04x "
13117 "flag %#x status %x\n",
13118 __func__,
13119 io->io_hdr.io_type,
13120 io->io_hdr.msg_type,
13121 io->scsiio.cdb[0],
13122 io->io_hdr.nexus.initid,
13123 io->io_hdr.nexus.targ_port,
13124 io->io_hdr.nexus.targ_lun,
13125 (io->io_hdr.io_type ==
13126 CTL_IO_TASK) ?
13127 io->taskio.tag_num :
13128 io->scsiio.tag_num,
13129 io->io_hdr.flags,
13130 io->io_hdr.status);
13131 } else
13132 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13133 #endif
13134
13135 /*
13136 * This is an internal copy of an I/O, and should not go through
13137 * the normal done processing logic.
13138 */
13139 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13140 return;
13141
13142 #ifdef CTL_IO_DELAY
13143 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13144 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13145 } else {
13146 struct ctl_lun *lun = CTL_LUN(io);
13147
13148 if ((lun != NULL)
13149 && (lun->delay_info.done_delay > 0)) {
13150
13151 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13152 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13153 callout_reset(&io->io_hdr.delay_callout,
13154 lun->delay_info.done_delay * hz,
13155 ctl_done_timer_wakeup, io);
13156 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13157 lun->delay_info.done_delay = 0;
13158 return;
13159 }
13160 }
13161 #endif /* CTL_IO_DELAY */
13162
13163 ctl_enqueue_done(io);
13164 }
13165
13166 static void
13167 ctl_work_thread(void *arg)
13168 {
13169 struct ctl_thread *thr = (struct ctl_thread *)arg;
13170 struct ctl_softc *softc = thr->ctl_softc;
13171 union ctl_io *io;
13172 int retval;
13173
13174 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13175 thread_lock(curthread);
13176 sched_prio(curthread, PUSER - 1);
13177 thread_unlock(curthread);
13178
13179 while (!softc->shutdown) {
13180 /*
13181 * We handle the queues in this order:
13182 * - ISC
13183 * - done queue (to free up resources, unblock other commands)
13184 * - incoming queue
13185 * - RtR queue
13186 *
13187 * If those queues are empty, we break out of the loop and
13188 * go to sleep.
13189 */
13190 mtx_lock(&thr->queue_lock);
13191 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13192 if (io != NULL) {
13193 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13194 mtx_unlock(&thr->queue_lock);
13195 ctl_handle_isc(io);
13196 continue;
13197 }
13198 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13199 if (io != NULL) {
13200 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13201 /* clear any blocked commands, call fe_done */
13202 mtx_unlock(&thr->queue_lock);
13203 ctl_process_done(io);
13204 continue;
13205 }
13206 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13207 if (io != NULL) {
13208 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13209 mtx_unlock(&thr->queue_lock);
13210 if (io->io_hdr.io_type == CTL_IO_TASK)
13211 ctl_run_task(io);
13212 else
13213 ctl_scsiio_precheck(softc, &io->scsiio);
13214 continue;
13215 }
13216 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13217 if (io != NULL) {
13218 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13219 mtx_unlock(&thr->queue_lock);
13220 retval = ctl_scsiio(&io->scsiio);
13221 if (retval != CTL_RETVAL_COMPLETE)
13222 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13223 continue;
13224 }
13225
13226 /* Sleep until we have something to do. */
13227 mtx_sleep(thr, &thr->queue_lock, PDROP, "-", 0);
13228 }
13229 thr->thread = NULL;
13230 kthread_exit();
13231 }
13232
13233 static void
13234 ctl_thresh_thread(void *arg)
13235 {
13236 struct ctl_softc *softc = (struct ctl_softc *)arg;
13237 struct ctl_lun *lun;
13238 struct ctl_logical_block_provisioning_page *page;
13239 const char *attr;
13240 union ctl_ha_msg msg;
13241 uint64_t thres, val;
13242 int i, e, set;
13243
13244 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13245 thread_lock(curthread);
13246 sched_prio(curthread, PUSER - 1);
13247 thread_unlock(curthread);
13248
13249 while (!softc->shutdown) {
13250 mtx_lock(&softc->ctl_lock);
13251 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13252 if ((lun->flags & CTL_LUN_DISABLED) ||
13253 (lun->flags & CTL_LUN_NO_MEDIA) ||
13254 lun->backend->lun_attr == NULL)
13255 continue;
13256 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13257 softc->ha_mode == CTL_HA_MODE_XFER)
13258 continue;
13259 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0)
13260 continue;
13261 e = 0;
13262 page = &lun->MODE_LBP;
13263 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13264 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13265 continue;
13266 thres = scsi_4btoul(page->descr[i].count);
13267 thres <<= CTL_LBP_EXPONENT;
13268 switch (page->descr[i].resource) {
13269 case 0x01:
13270 attr = "blocksavail";
13271 break;
13272 case 0x02:
13273 attr = "blocksused";
13274 break;
13275 case 0xf1:
13276 attr = "poolblocksavail";
13277 break;
13278 case 0xf2:
13279 attr = "poolblocksused";
13280 break;
13281 default:
13282 continue;
13283 }
13284 mtx_unlock(&softc->ctl_lock); // XXX
13285 val = lun->backend->lun_attr(
13286 lun->be_lun->be_lun, attr);
13287 mtx_lock(&softc->ctl_lock);
13288 if (val == UINT64_MAX)
13289 continue;
13290 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13291 == SLBPPD_ARMING_INC)
13292 e = (val >= thres);
13293 else
13294 e = (val <= thres);
13295 if (e)
13296 break;
13297 }
13298 mtx_lock(&lun->lun_lock);
13299 if (e) {
13300 scsi_u64to8b((uint8_t *)&page->descr[i] -
13301 (uint8_t *)page, lun->ua_tpt_info);
13302 if (lun->lasttpt == 0 ||
13303 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13304 lun->lasttpt = time_uptime;
13305 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13306 set = 1;
13307 } else
13308 set = 0;
13309 } else {
13310 lun->lasttpt = 0;
13311 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13312 set = -1;
13313 }
13314 mtx_unlock(&lun->lun_lock);
13315 if (set != 0 &&
13316 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13317 /* Send msg to other side. */
13318 bzero(&msg.ua, sizeof(msg.ua));
13319 msg.hdr.msg_type = CTL_MSG_UA;
13320 msg.hdr.nexus.initid = -1;
13321 msg.hdr.nexus.targ_port = -1;
13322 msg.hdr.nexus.targ_lun = lun->lun;
13323 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13324 msg.ua.ua_all = 1;
13325 msg.ua.ua_set = (set > 0);
13326 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13327 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
13328 mtx_unlock(&softc->ctl_lock); // XXX
13329 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13330 sizeof(msg.ua), M_WAITOK);
13331 mtx_lock(&softc->ctl_lock);
13332 }
13333 }
13334 mtx_sleep(&softc->thresh_thread, &softc->ctl_lock,
13335 PDROP, "-", CTL_LBP_PERIOD * hz);
13336 }
13337 softc->thresh_thread = NULL;
13338 kthread_exit();
13339 }
13340
13341 static void
13342 ctl_enqueue_incoming(union ctl_io *io)
13343 {
13344 struct ctl_softc *softc = CTL_SOFTC(io);
13345 struct ctl_thread *thr;
13346 u_int idx;
13347
13348 idx = (io->io_hdr.nexus.targ_port * 127 +
13349 io->io_hdr.nexus.initid) % worker_threads;
13350 thr = &softc->threads[idx];
13351 mtx_lock(&thr->queue_lock);
13352 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13353 mtx_unlock(&thr->queue_lock);
13354 wakeup(thr);
13355 }
13356
13357 static void
13358 ctl_enqueue_rtr(union ctl_io *io)
13359 {
13360 struct ctl_softc *softc = CTL_SOFTC(io);
13361 struct ctl_thread *thr;
13362
13363 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13364 mtx_lock(&thr->queue_lock);
13365 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13366 mtx_unlock(&thr->queue_lock);
13367 wakeup(thr);
13368 }
13369
13370 static void
13371 ctl_enqueue_done(union ctl_io *io)
13372 {
13373 struct ctl_softc *softc = CTL_SOFTC(io);
13374 struct ctl_thread *thr;
13375
13376 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13377 mtx_lock(&thr->queue_lock);
13378 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13379 mtx_unlock(&thr->queue_lock);
13380 wakeup(thr);
13381 }
13382
13383 static void
13384 ctl_enqueue_isc(union ctl_io *io)
13385 {
13386 struct ctl_softc *softc = CTL_SOFTC(io);
13387 struct ctl_thread *thr;
13388
13389 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13390 mtx_lock(&thr->queue_lock);
13391 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13392 mtx_unlock(&thr->queue_lock);
13393 wakeup(thr);
13394 }
13395
13396 /*
13397 * vim: ts=8
13398 */
13399