2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2003 Silicon Graphics International Corp.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification.
13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14 * substantially similar to the "NO WARRANTY" disclaimer below
15 * ("Disclaimer") and any redistribution must be conditioned upon
16 * including a substantially similar Disclaimer requirement for further
17 * binary redistribution.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGES.
32 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_util.c#2 $
35 * CAM Target Layer SCSI library
37 * Author: Ken Merry <ken@FreeBSD.org>
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/types.h>
45 #include <sys/malloc.h>
46 #else /* __KERNEL__ */
47 #include <sys/types.h>
53 #endif /* __KERNEL__ */
55 #include <sys/queue.h>
56 #include <sys/callout.h>
57 #include <cam/scsi/scsi_all.h>
58 #include <cam/ctl/ctl_io.h>
59 #include <cam/ctl/ctl_scsi_all.h>
60 #include <cam/ctl/ctl_util.h>
62 struct ctl_status_desc {
64 const char *description;
67 struct ctl_task_desc {
68 ctl_task_type task_action;
69 const char *description;
71 static struct ctl_status_desc ctl_status_table[] = {
72 {CTL_STATUS_NONE, "No Status"},
73 {CTL_SUCCESS, "Command Completed Successfully"},
74 {CTL_CMD_TIMEOUT, "Command Timed Out"},
75 {CTL_SEL_TIMEOUT, "Selection Timeout"},
76 {CTL_ERROR, "Command Failed"},
77 {CTL_SCSI_ERROR, "SCSI Error"},
78 {CTL_CMD_ABORTED, "Command Aborted"},
81 static struct ctl_task_desc ctl_task_table[] = {
82 {CTL_TASK_ABORT_TASK, "Abort Task"},
83 {CTL_TASK_ABORT_TASK_SET, "Abort Task Set"},
84 {CTL_TASK_CLEAR_ACA, "Clear ACA"},
85 {CTL_TASK_CLEAR_TASK_SET, "Clear Task Set"},
86 {CTL_TASK_I_T_NEXUS_RESET, "I_T Nexus Reset"},
87 {CTL_TASK_LUN_RESET, "LUN Reset"},
88 {CTL_TASK_TARGET_RESET, "Target Reset"},
89 {CTL_TASK_BUS_RESET, "Bus Reset"},
90 {CTL_TASK_PORT_LOGIN, "Port Login"},
91 {CTL_TASK_PORT_LOGOUT, "Port Logout"},
92 {CTL_TASK_QUERY_TASK, "Query Task"},
93 {CTL_TASK_QUERY_TASK_SET, "Query Task Set"},
94 {CTL_TASK_QUERY_ASYNC_EVENT, "Query Async Event"}
98 ctl_scsi_tur(union ctl_io *io, ctl_tag_type tag_type, uint8_t control)
100 struct ctl_scsiio *ctsio;
101 struct scsi_test_unit_ready *cdb;
103 ctl_scsi_zero_io(io);
105 io->io_hdr.io_type = CTL_IO_SCSI;
107 cdb = (struct scsi_test_unit_ready *)ctsio->cdb;
109 cdb->opcode = TEST_UNIT_READY;
110 cdb->control = control;
111 io->io_hdr.flags = CTL_FLAG_DATA_NONE;
112 ctsio->tag_type = tag_type;
113 ctsio->cdb_len = sizeof(*cdb);
114 ctsio->ext_data_len = 0;
115 ctsio->ext_data_ptr = NULL;
116 ctsio->ext_sg_entries = 0;
117 ctsio->ext_data_filled = 0;
118 ctsio->sense_len = SSD_FULL_SIZE;
122 ctl_scsi_inquiry(union ctl_io *io, uint8_t *data_ptr, int32_t data_len,
123 uint8_t byte2, uint8_t page_code, ctl_tag_type tag_type,
126 struct ctl_scsiio *ctsio;
127 struct scsi_inquiry *cdb;
129 ctl_scsi_zero_io(io);
131 io->io_hdr.io_type = CTL_IO_SCSI;
133 cdb = (struct scsi_inquiry *)ctsio->cdb;
135 cdb->opcode = INQUIRY;
137 cdb->page_code = page_code;
138 cdb->control = control;
139 scsi_ulto2b(data_len, cdb->length);
140 io->io_hdr.io_type = CTL_IO_SCSI;
141 io->io_hdr.flags = CTL_FLAG_DATA_IN;
142 ctsio->tag_type = tag_type;
143 ctsio->cdb_len = sizeof(*cdb);
144 ctsio->ext_data_len = data_len;
145 ctsio->ext_data_ptr = data_ptr;
146 ctsio->ext_sg_entries = 0;
147 ctsio->ext_data_filled = 0;
148 ctsio->sense_len = SSD_FULL_SIZE;
152 ctl_scsi_request_sense(union ctl_io *io, uint8_t *data_ptr,
153 int32_t data_len, uint8_t byte2, ctl_tag_type tag_type,
156 struct ctl_scsiio *ctsio;
157 struct scsi_request_sense *cdb;
159 ctl_scsi_zero_io(io);
161 io->io_hdr.io_type = CTL_IO_SCSI;
163 cdb = (struct scsi_request_sense *)ctsio->cdb;
165 cdb->opcode = REQUEST_SENSE;
167 cdb->control = control;
168 cdb->length = data_len;
169 io->io_hdr.io_type = CTL_IO_SCSI;
170 io->io_hdr.flags = CTL_FLAG_DATA_IN;
171 ctsio->tag_type = tag_type;
172 ctsio->cdb_len = sizeof(*cdb);
173 ctsio->ext_data_ptr = data_ptr;
174 ctsio->ext_data_len = data_len;
175 ctsio->ext_sg_entries = 0;
176 ctsio->ext_data_filled = 0;
177 ctsio->sense_len = SSD_FULL_SIZE;
181 ctl_scsi_report_luns(union ctl_io *io, uint8_t *data_ptr, uint32_t data_len,
182 uint8_t select_report, ctl_tag_type tag_type,
185 struct ctl_scsiio *ctsio;
186 struct scsi_report_luns *cdb;
188 ctl_scsi_zero_io(io);
190 io->io_hdr.io_type = CTL_IO_SCSI;
192 cdb = (struct scsi_report_luns *)ctsio->cdb;
194 cdb->opcode = REPORT_LUNS;
195 cdb->select_report = select_report;
196 scsi_ulto4b(data_len, cdb->length);
197 cdb->control = control;
198 io->io_hdr.io_type = CTL_IO_SCSI;
199 io->io_hdr.flags = CTL_FLAG_DATA_IN;
200 ctsio->tag_type = tag_type;
201 ctsio->cdb_len = sizeof(*cdb);
202 ctsio->ext_data_ptr = data_ptr;
203 ctsio->ext_data_len = data_len;
204 ctsio->ext_sg_entries = 0;
205 ctsio->ext_data_filled = 0;
206 ctsio->sense_len = SSD_FULL_SIZE;
210 ctl_scsi_read_write_buffer(union ctl_io *io, uint8_t *data_ptr,
211 uint32_t data_len, int read_buffer, uint8_t mode,
212 uint8_t buffer_id, uint32_t buffer_offset,
213 ctl_tag_type tag_type, uint8_t control)
215 struct ctl_scsiio *ctsio;
216 struct scsi_write_buffer *cdb;
218 ctl_scsi_zero_io(io);
220 io->io_hdr.io_type = CTL_IO_SCSI;
222 cdb = (struct scsi_write_buffer *)ctsio->cdb;
224 if (read_buffer != 0)
225 cdb->opcode = READ_BUFFER;
227 cdb->opcode = WRITE_BUFFER;
229 cdb->byte2 = mode & RWB_MODE;
230 cdb->buffer_id = buffer_id;
231 scsi_ulto3b(buffer_offset, cdb->offset);
232 scsi_ulto3b(data_len, cdb->length);
233 cdb->control = control;
234 io->io_hdr.io_type = CTL_IO_SCSI;
235 if (read_buffer != 0)
236 io->io_hdr.flags = CTL_FLAG_DATA_IN;
238 io->io_hdr.flags = CTL_FLAG_DATA_OUT;
239 ctsio->tag_type = tag_type;
240 ctsio->cdb_len = sizeof(*cdb);
241 ctsio->ext_data_ptr = data_ptr;
242 ctsio->ext_data_len = data_len;
243 ctsio->ext_sg_entries = 0;
244 ctsio->ext_data_filled = 0;
245 ctsio->sense_len = SSD_FULL_SIZE;
249 ctl_scsi_read_write(union ctl_io *io, uint8_t *data_ptr, uint32_t data_len,
250 int read_op, uint8_t byte2, int minimum_cdb_size,
251 uint64_t lba, uint32_t num_blocks, ctl_tag_type tag_type,
254 struct ctl_scsiio *ctsio;
256 ctl_scsi_zero_io(io);
258 io->io_hdr.io_type = CTL_IO_SCSI;
262 * Pick out the smallest CDB that will hold the user's request.
263 * minimum_cdb_size allows cranking the CDB size up, even for
264 * requests that would not normally need a large CDB. This can be
265 * useful for testing (e.g. to make sure READ_16 support works without
266 * having an array larger than 2TB) and for compatibility -- e.g.
267 * if your device doesn't support READ_6. (ATAPI drives don't.)
269 if ((minimum_cdb_size < 10)
270 && ((lba & 0x1fffff) == lba)
271 && ((num_blocks & 0xff) == num_blocks)
273 struct scsi_rw_6 *cdb;
276 * Note that according to SBC-2, the target should return 256
277 * blocks if the transfer length in a READ(6) or WRITE(6) CDB
278 * is set to 0. Since it's possible that some targets
279 * won't do the right thing, we only send a READ(6) or
280 * WRITE(6) for transfer sizes up to and including 255 blocks.
282 cdb = (struct scsi_rw_6 *)ctsio->cdb;
284 cdb->opcode = (read_op) ? READ_6 : WRITE_6;
285 scsi_ulto3b(lba, cdb->addr);
286 cdb->length = num_blocks & 0xff;
287 cdb->control = control;
289 ctsio->cdb_len = sizeof(*cdb);
291 } else if ((minimum_cdb_size < 12)
292 && ((num_blocks & 0xffff) == num_blocks)
293 && ((lba & 0xffffffff) == lba)) {
294 struct scsi_rw_10 *cdb;
296 cdb = (struct scsi_rw_10 *)ctsio->cdb;
298 cdb->opcode = (read_op) ? READ_10 : WRITE_10;
300 scsi_ulto4b(lba, cdb->addr);
302 scsi_ulto2b(num_blocks, cdb->length);
303 cdb->control = control;
305 ctsio->cdb_len = sizeof(*cdb);
306 } else if ((minimum_cdb_size < 16)
307 && ((num_blocks & 0xffffffff) == num_blocks)
308 && ((lba & 0xffffffff) == lba)) {
309 struct scsi_rw_12 *cdb;
311 cdb = (struct scsi_rw_12 *)ctsio->cdb;
313 cdb->opcode = (read_op) ? READ_12 : WRITE_12;
315 scsi_ulto4b(lba, cdb->addr);
316 scsi_ulto4b(num_blocks, cdb->length);
318 cdb->control = control;
320 ctsio->cdb_len = sizeof(*cdb);
322 struct scsi_rw_16 *cdb;
324 cdb = (struct scsi_rw_16 *)ctsio->cdb;
326 cdb->opcode = (read_op) ? READ_16 : WRITE_16;
328 scsi_u64to8b(lba, cdb->addr);
329 scsi_ulto4b(num_blocks, cdb->length);
331 cdb->control = control;
333 ctsio->cdb_len = sizeof(*cdb);
336 io->io_hdr.io_type = CTL_IO_SCSI;
338 io->io_hdr.flags = CTL_FLAG_DATA_IN;
340 io->io_hdr.flags = CTL_FLAG_DATA_OUT;
341 ctsio->tag_type = tag_type;
342 ctsio->ext_data_ptr = data_ptr;
343 ctsio->ext_data_len = data_len;
344 ctsio->ext_sg_entries = 0;
345 ctsio->ext_data_filled = 0;
346 ctsio->sense_len = SSD_FULL_SIZE;
350 ctl_scsi_write_same(union ctl_io *io, uint8_t *data_ptr, uint32_t data_len,
351 uint8_t byte2, uint64_t lba, uint32_t num_blocks,
352 ctl_tag_type tag_type, uint8_t control)
354 struct ctl_scsiio *ctsio;
355 struct scsi_write_same_16 *cdb;
357 ctl_scsi_zero_io(io);
359 io->io_hdr.io_type = CTL_IO_SCSI;
361 ctsio->cdb_len = sizeof(*cdb);
362 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
363 cdb->opcode = WRITE_SAME_16;
365 scsi_u64to8b(lba, cdb->addr);
366 scsi_ulto4b(num_blocks, cdb->length);
368 cdb->control = control;
370 io->io_hdr.io_type = CTL_IO_SCSI;
371 io->io_hdr.flags = CTL_FLAG_DATA_OUT;
372 ctsio->tag_type = tag_type;
373 ctsio->ext_data_ptr = data_ptr;
374 ctsio->ext_data_len = data_len;
375 ctsio->ext_sg_entries = 0;
376 ctsio->ext_data_filled = 0;
377 ctsio->sense_len = SSD_FULL_SIZE;
381 ctl_scsi_read_capacity(union ctl_io *io, uint8_t *data_ptr, uint32_t data_len,
382 uint32_t addr, int reladr, int pmi,
383 ctl_tag_type tag_type, uint8_t control)
385 struct scsi_read_capacity *cdb;
387 ctl_scsi_zero_io(io);
389 io->io_hdr.io_type = CTL_IO_SCSI;
390 cdb = (struct scsi_read_capacity *)io->scsiio.cdb;
392 cdb->opcode = READ_CAPACITY;
394 cdb->byte2 = SRC_RELADR;
397 scsi_ulto4b(addr, cdb->addr);
398 cdb->control = control;
399 io->io_hdr.io_type = CTL_IO_SCSI;
400 io->io_hdr.flags = CTL_FLAG_DATA_IN;
401 io->scsiio.tag_type = tag_type;
402 io->scsiio.ext_data_ptr = data_ptr;
403 io->scsiio.ext_data_len = data_len;
404 io->scsiio.ext_sg_entries = 0;
405 io->scsiio.ext_data_filled = 0;
406 io->scsiio.sense_len = SSD_FULL_SIZE;
410 ctl_scsi_read_capacity_16(union ctl_io *io, uint8_t *data_ptr,
411 uint32_t data_len, uint64_t addr, int reladr,
412 int pmi, ctl_tag_type tag_type, uint8_t control)
414 struct scsi_read_capacity_16 *cdb;
416 ctl_scsi_zero_io(io);
418 io->io_hdr.io_type = CTL_IO_SCSI;
419 cdb = (struct scsi_read_capacity_16 *)io->scsiio.cdb;
421 cdb->opcode = SERVICE_ACTION_IN;
422 cdb->service_action = SRC16_SERVICE_ACTION;
424 cdb->reladr |= SRC16_RELADR;
426 cdb->reladr |= SRC16_PMI;
427 scsi_u64to8b(addr, cdb->addr);
428 scsi_ulto4b(data_len, cdb->alloc_len);
429 cdb->control = control;
431 io->io_hdr.io_type = CTL_IO_SCSI;
432 io->io_hdr.flags = CTL_FLAG_DATA_IN;
433 io->scsiio.tag_type = tag_type;
434 io->scsiio.ext_data_ptr = data_ptr;
435 io->scsiio.ext_data_len = data_len;
436 io->scsiio.ext_sg_entries = 0;
437 io->scsiio.ext_data_filled = 0;
438 io->scsiio.sense_len = SSD_FULL_SIZE;
442 ctl_scsi_mode_sense(union ctl_io *io, uint8_t *data_ptr, uint32_t data_len,
443 int dbd, int llbaa, uint8_t page_code, uint8_t pc,
444 uint8_t subpage, int minimum_cdb_size,
445 ctl_tag_type tag_type, uint8_t control)
447 ctl_scsi_zero_io(io);
449 if ((minimum_cdb_size < 10)
451 && (data_len < 256)) {
452 struct scsi_mode_sense_6 *cdb;
454 cdb = (struct scsi_mode_sense_6 *)io->scsiio.cdb;
456 cdb->opcode = MODE_SENSE_6;
458 cdb->byte2 |= SMS_DBD;
459 cdb->page = page_code | pc;
460 cdb->subpage = subpage;
461 cdb->length = data_len;
462 cdb->control = control;
464 struct scsi_mode_sense_10 *cdb;
466 cdb = (struct scsi_mode_sense_10 *)io->scsiio.cdb;
468 cdb->opcode = MODE_SENSE_10;
470 cdb->byte2 |= SMS_DBD;
472 cdb->byte2 |= SMS10_LLBAA;
473 cdb->page = page_code | pc;
474 cdb->subpage = subpage;
475 scsi_ulto2b(data_len, cdb->length);
476 cdb->control = control;
479 io->io_hdr.io_type = CTL_IO_SCSI;
480 io->io_hdr.flags = CTL_FLAG_DATA_IN;
481 io->scsiio.tag_type = tag_type;
482 io->scsiio.ext_data_ptr = data_ptr;
483 io->scsiio.ext_data_len = data_len;
484 io->scsiio.ext_sg_entries = 0;
485 io->scsiio.ext_data_filled = 0;
486 io->scsiio.sense_len = SSD_FULL_SIZE;
490 ctl_scsi_start_stop(union ctl_io *io, int start, int load_eject, int immediate,
491 int power_conditions, ctl_tag_type tag_type, uint8_t control)
493 struct scsi_start_stop_unit *cdb;
495 cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
497 ctl_scsi_zero_io(io);
499 cdb->opcode = START_STOP_UNIT;
501 cdb->byte2 |= SSS_IMMED;
502 cdb->how = power_conditions;
504 cdb->how |= SSS_LOEJ;
506 cdb->how |= SSS_START;
507 cdb->control = control;
508 io->io_hdr.io_type = CTL_IO_SCSI;
509 io->io_hdr.flags = CTL_FLAG_DATA_NONE;
510 io->scsiio.tag_type = tag_type;
511 io->scsiio.ext_data_ptr = NULL;
512 io->scsiio.ext_data_len = 0;
513 io->scsiio.ext_sg_entries = 0;
514 io->scsiio.ext_data_filled = 0;
515 io->scsiio.sense_len = SSD_FULL_SIZE;
519 ctl_scsi_sync_cache(union ctl_io *io, int immed, int reladr,
520 int minimum_cdb_size, uint64_t starting_lba,
521 uint32_t block_count, ctl_tag_type tag_type,
524 ctl_scsi_zero_io(io);
526 if ((minimum_cdb_size < 16)
527 && ((block_count & 0xffff) == block_count)
528 && ((starting_lba & 0xffffffff) == starting_lba)) {
529 struct scsi_sync_cache *cdb;
531 cdb = (struct scsi_sync_cache *)io->scsiio.cdb;
533 cdb->opcode = SYNCHRONIZE_CACHE;
535 cdb->byte2 |= SSC_RELADR;
538 cdb->byte2 |= SSC_IMMED;
540 scsi_ulto4b(starting_lba, cdb->begin_lba);
541 scsi_ulto2b(block_count, cdb->lb_count);
542 cdb->control = control;
544 struct scsi_sync_cache_16 *cdb;
546 cdb = (struct scsi_sync_cache_16 *)io->scsiio.cdb;
548 cdb->opcode = SYNCHRONIZE_CACHE_16;
550 cdb->byte2 |= SSC_RELADR;
553 cdb->byte2 |= SSC_IMMED;
555 scsi_u64to8b(starting_lba, cdb->begin_lba);
556 scsi_ulto4b(block_count, cdb->lb_count);
557 cdb->control = control;
559 io->io_hdr.io_type = CTL_IO_SCSI;
560 io->io_hdr.flags = CTL_FLAG_DATA_NONE;
561 io->scsiio.tag_type = tag_type;
562 io->scsiio.ext_data_ptr = NULL;
563 io->scsiio.ext_data_len = 0;
564 io->scsiio.ext_sg_entries = 0;
565 io->scsiio.ext_data_filled = 0;
566 io->scsiio.sense_len = SSD_FULL_SIZE;
570 ctl_scsi_persistent_res_in(union ctl_io *io, uint8_t *data_ptr,
571 uint32_t data_len, int action,
572 ctl_tag_type tag_type, uint8_t control)
575 struct scsi_per_res_in *cdb;
577 ctl_scsi_zero_io(io);
579 cdb = (struct scsi_per_res_in *)io->scsiio.cdb;
580 cdb->opcode = PERSISTENT_RES_IN;
581 cdb->action = action;
582 scsi_ulto2b(data_len, cdb->length);
583 cdb->control = control;
585 io->io_hdr.io_type = CTL_IO_SCSI;
586 io->io_hdr.flags = CTL_FLAG_DATA_IN;
587 io->scsiio.tag_type = tag_type;
588 io->scsiio.ext_data_ptr = data_ptr;
589 io->scsiio.ext_data_len = data_len;
590 io->scsiio.ext_sg_entries = 0;
591 io->scsiio.ext_data_filled = 0;
592 io->scsiio.sense_len = SSD_FULL_SIZE;
596 ctl_scsi_persistent_res_out(union ctl_io *io, uint8_t *data_ptr,
597 uint32_t data_len, int action, int type,
598 uint64_t key, uint64_t sa_key,
599 ctl_tag_type tag_type, uint8_t control)
602 struct scsi_per_res_out *cdb;
603 struct scsi_per_res_out_parms *params;
605 ctl_scsi_zero_io(io);
607 cdb = (struct scsi_per_res_out *)io->scsiio.cdb;
608 params = (struct scsi_per_res_out_parms *)data_ptr;
610 cdb->opcode = PERSISTENT_RES_OUT;
614 cdb->action = action;
636 scsi_ulto4b(data_len, cdb->length);
637 cdb->control = control;
639 scsi_u64to8b(key, params->res_key.key);
640 scsi_u64to8b(sa_key, params->serv_act_res_key);
642 io->io_hdr.io_type = CTL_IO_SCSI;
643 io->io_hdr.flags = CTL_FLAG_DATA_OUT;
644 io->scsiio.tag_type = tag_type;
645 io->scsiio.ext_data_ptr = data_ptr;
646 io->scsiio.ext_data_len = data_len;
647 io->scsiio.ext_sg_entries = 0;
648 io->scsiio.ext_data_filled = 0;
649 io->scsiio.sense_len = SSD_FULL_SIZE;
654 ctl_scsi_maintenance_in(union ctl_io *io, uint8_t *data_ptr, uint32_t data_len,
655 uint8_t action, ctl_tag_type tag_type, uint8_t control)
657 struct scsi_maintenance_in *cdb;
659 ctl_scsi_zero_io(io);
661 cdb = (struct scsi_maintenance_in *)io->scsiio.cdb;
662 cdb->opcode = MAINTENANCE_IN;
664 scsi_ulto4b(data_len, cdb->length);
665 cdb->control = control;
667 io->io_hdr.io_type = CTL_IO_SCSI;
668 io->io_hdr.flags = CTL_FLAG_DATA_IN;
669 io->scsiio.tag_type = tag_type;
670 io->scsiio.ext_data_ptr = data_ptr;
671 io->scsiio.ext_data_len = data_len;
672 io->scsiio.ext_sg_entries = 0;
673 io->scsiio.ext_data_filled = 0;
674 io->scsiio.sense_len = SSD_FULL_SIZE;
679 ctl_scsi_alloc_io(uint32_t initid)
683 io = (union ctl_io *)malloc(sizeof(*io));
687 io->io_hdr.nexus.initid = initid;
694 ctl_scsi_free_io(union ctl_io *io)
700 ctl_scsi_zero_io(union ctl_io *io)
707 pool_ref = io->io_hdr.pool;
708 memset(io, 0, sizeof(*io));
709 io->io_hdr.pool = pool_ref;
711 #endif /* !_KERNEL */
714 ctl_scsi_task_string(struct ctl_taskio *taskio)
718 for (i = 0; i < (sizeof(ctl_task_table)/sizeof(ctl_task_table[0]));
720 if (taskio->task_action == ctl_task_table[i].task_action) {
721 return (ctl_task_table[i].description);
729 ctl_io_sbuf(union ctl_io *io, struct sbuf *sb)
731 const char *task_desc;
734 ctl_scsi_path_string(io, path_str, sizeof(path_str));
736 switch (io->io_hdr.io_type) {
738 sbuf_cat(sb, path_str);
739 ctl_scsi_command_string(&io->scsiio, NULL, sb);
740 sbuf_printf(sb, " Tag: %#jx/%d, Prio: %d\n",
741 io->scsiio.tag_num, io->scsiio.tag_type,
742 io->scsiio.priority);
745 sbuf_cat(sb, path_str);
746 task_desc = ctl_scsi_task_string(&io->taskio);
747 if (task_desc == NULL)
748 sbuf_printf(sb, "Unknown Task Action %d (%#x)",
749 io->taskio.task_action, io->taskio.task_action);
751 sbuf_printf(sb, "Task Action: %s", task_desc);
752 switch (io->taskio.task_action) {
753 case CTL_TASK_ABORT_TASK:
754 sbuf_printf(sb, " Tag: %#jx/%d\n",
755 io->taskio.tag_num, io->taskio.tag_type);
768 ctl_io_error_sbuf(union ctl_io *io, struct scsi_inquiry_data *inq_data,
771 struct ctl_status_desc *status_desc;
778 for (i = 0; i < (sizeof(ctl_status_table)/sizeof(ctl_status_table[0]));
780 if ((io->io_hdr.status & CTL_STATUS_MASK) ==
781 ctl_status_table[i].status) {
782 status_desc = &ctl_status_table[i];
787 ctl_scsi_path_string(io, path_str, sizeof(path_str));
789 sbuf_cat(sb, path_str);
790 if (status_desc == NULL)
791 sbuf_printf(sb, "CTL Status: Unknown status %#x\n",
794 sbuf_printf(sb, "CTL Status: %s\n", status_desc->description);
796 if ((io->io_hdr.io_type == CTL_IO_SCSI)
797 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR)) {
798 sbuf_cat(sb, path_str);
799 sbuf_printf(sb, "SCSI Status: %s\n",
800 ctl_scsi_status_string(&io->scsiio));
802 if (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)
803 ctl_scsi_sense_sbuf(&io->scsiio, inq_data,
809 ctl_io_string(union ctl_io *io, char *str, int str_len)
813 sbuf_new(&sb, str, str_len, SBUF_FIXEDLEN);
814 ctl_io_sbuf(io, &sb);
816 return (sbuf_data(&sb));
820 ctl_io_error_string(union ctl_io *io, struct scsi_inquiry_data *inq_data,
821 char *str, int str_len)
825 sbuf_new(&sb, str, str_len, SBUF_FIXEDLEN);
826 ctl_io_error_sbuf(io, inq_data, &sb);
828 return (sbuf_data(&sb));
834 ctl_io_print(union ctl_io *io)
838 printf("%s", ctl_io_string(io, str, sizeof(str)));
842 ctl_io_error_print(union ctl_io *io, struct scsi_inquiry_data *inq_data)
846 printf("%s", ctl_io_error_string(io, inq_data, str, sizeof(str)));
851 ctl_data_print(union ctl_io *io)
858 if (io->io_hdr.io_type != CTL_IO_SCSI)
860 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
862 if (io->scsiio.kern_sg_entries > 0) /* XXX: Implement */
864 ctl_scsi_path_string(io, path_str, sizeof(path_str));
865 len = min(io->scsiio.kern_data_len, 4096);
866 for (i = 0; i < len; ) {
867 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
868 sbuf_cat(&sb, path_str);
869 sbuf_printf(&sb, " %#jx:%04x:", io->scsiio.tag_num, i);
870 for (j = 0; j < 16 && i < len; i++, j++) {
873 sbuf_printf(&sb, " %02x", io->scsiio.kern_data_ptr[i]);
877 printf("%s", sbuf_data(&sb));
884 ctl_io_error_print(union ctl_io *io, struct scsi_inquiry_data *inq_data,
889 fprintf(ofile, "%s", ctl_io_error_string(io, inq_data, str,