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>
40 #include <sys/cdefs.h>
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/types.h>
46 #include <sys/malloc.h>
47 #else /* __KERNEL__ */
48 #include <sys/types.h>
54 #endif /* __KERNEL__ */
56 #include <sys/queue.h>
57 #include <sys/callout.h>
58 #include <cam/scsi/scsi_all.h>
59 #include <cam/ctl/ctl_io.h>
60 #include <cam/ctl/ctl_scsi_all.h>
61 #include <cam/ctl/ctl_util.h>
63 struct ctl_status_desc {
65 const char *description;
68 struct ctl_task_desc {
69 ctl_task_type task_action;
70 const char *description;
72 static struct ctl_status_desc ctl_status_table[] = {
73 {CTL_STATUS_NONE, "No Status"},
74 {CTL_SUCCESS, "Command Completed Successfully"},
75 {CTL_CMD_TIMEOUT, "Command Timed Out"},
76 {CTL_SEL_TIMEOUT, "Selection Timeout"},
77 {CTL_ERROR, "Command Failed"},
78 {CTL_SCSI_ERROR, "SCSI Error"},
79 {CTL_CMD_ABORTED, "Command Aborted"},
82 static struct ctl_task_desc ctl_task_table[] = {
83 {CTL_TASK_ABORT_TASK, "Abort Task"},
84 {CTL_TASK_ABORT_TASK_SET, "Abort Task Set"},
85 {CTL_TASK_CLEAR_ACA, "Clear ACA"},
86 {CTL_TASK_CLEAR_TASK_SET, "Clear Task Set"},
87 {CTL_TASK_I_T_NEXUS_RESET, "I_T Nexus Reset"},
88 {CTL_TASK_LUN_RESET, "LUN Reset"},
89 {CTL_TASK_TARGET_RESET, "Target Reset"},
90 {CTL_TASK_BUS_RESET, "Bus Reset"},
91 {CTL_TASK_PORT_LOGIN, "Port Login"},
92 {CTL_TASK_PORT_LOGOUT, "Port Logout"},
93 {CTL_TASK_QUERY_TASK, "Query Task"},
94 {CTL_TASK_QUERY_TASK_SET, "Query Task Set"},
95 {CTL_TASK_QUERY_ASYNC_EVENT, "Query Async Event"}
99 ctl_scsi_tur(union ctl_io *io, ctl_tag_type tag_type, uint8_t control)
101 struct ctl_scsiio *ctsio;
102 struct scsi_test_unit_ready *cdb;
104 ctl_scsi_zero_io(io);
106 io->io_hdr.io_type = CTL_IO_SCSI;
108 cdb = (struct scsi_test_unit_ready *)ctsio->cdb;
110 cdb->opcode = TEST_UNIT_READY;
111 cdb->control = control;
112 io->io_hdr.flags = CTL_FLAG_DATA_NONE;
113 ctsio->tag_type = tag_type;
114 ctsio->cdb_len = sizeof(*cdb);
115 ctsio->ext_data_len = 0;
116 ctsio->ext_data_ptr = NULL;
117 ctsio->ext_sg_entries = 0;
118 ctsio->ext_data_filled = 0;
119 ctsio->sense_len = SSD_FULL_SIZE;
123 ctl_scsi_inquiry(union ctl_io *io, uint8_t *data_ptr, int32_t data_len,
124 uint8_t byte2, uint8_t page_code, ctl_tag_type tag_type,
127 struct ctl_scsiio *ctsio;
128 struct scsi_inquiry *cdb;
130 ctl_scsi_zero_io(io);
132 io->io_hdr.io_type = CTL_IO_SCSI;
134 cdb = (struct scsi_inquiry *)ctsio->cdb;
136 cdb->opcode = INQUIRY;
138 cdb->page_code = page_code;
139 cdb->control = control;
140 scsi_ulto2b(data_len, cdb->length);
141 io->io_hdr.io_type = CTL_IO_SCSI;
142 io->io_hdr.flags = CTL_FLAG_DATA_IN;
143 ctsio->tag_type = tag_type;
144 ctsio->cdb_len = sizeof(*cdb);
145 ctsio->ext_data_len = data_len;
146 ctsio->ext_data_ptr = data_ptr;
147 ctsio->ext_sg_entries = 0;
148 ctsio->ext_data_filled = 0;
149 ctsio->sense_len = SSD_FULL_SIZE;
153 ctl_scsi_request_sense(union ctl_io *io, uint8_t *data_ptr,
154 int32_t data_len, uint8_t byte2, ctl_tag_type tag_type,
157 struct ctl_scsiio *ctsio;
158 struct scsi_request_sense *cdb;
160 ctl_scsi_zero_io(io);
162 io->io_hdr.io_type = CTL_IO_SCSI;
164 cdb = (struct scsi_request_sense *)ctsio->cdb;
166 cdb->opcode = REQUEST_SENSE;
168 cdb->control = control;
169 cdb->length = data_len;
170 io->io_hdr.io_type = CTL_IO_SCSI;
171 io->io_hdr.flags = CTL_FLAG_DATA_IN;
172 ctsio->tag_type = tag_type;
173 ctsio->cdb_len = sizeof(*cdb);
174 ctsio->ext_data_ptr = data_ptr;
175 ctsio->ext_data_len = data_len;
176 ctsio->ext_sg_entries = 0;
177 ctsio->ext_data_filled = 0;
178 ctsio->sense_len = SSD_FULL_SIZE;
182 ctl_scsi_report_luns(union ctl_io *io, uint8_t *data_ptr, uint32_t data_len,
183 uint8_t select_report, ctl_tag_type tag_type,
186 struct ctl_scsiio *ctsio;
187 struct scsi_report_luns *cdb;
189 ctl_scsi_zero_io(io);
191 io->io_hdr.io_type = CTL_IO_SCSI;
193 cdb = (struct scsi_report_luns *)ctsio->cdb;
195 cdb->opcode = REPORT_LUNS;
196 cdb->select_report = select_report;
197 scsi_ulto4b(data_len, cdb->length);
198 cdb->control = control;
199 io->io_hdr.io_type = CTL_IO_SCSI;
200 io->io_hdr.flags = CTL_FLAG_DATA_IN;
201 ctsio->tag_type = tag_type;
202 ctsio->cdb_len = sizeof(*cdb);
203 ctsio->ext_data_ptr = data_ptr;
204 ctsio->ext_data_len = data_len;
205 ctsio->ext_sg_entries = 0;
206 ctsio->ext_data_filled = 0;
207 ctsio->sense_len = SSD_FULL_SIZE;
211 ctl_scsi_read_write_buffer(union ctl_io *io, uint8_t *data_ptr,
212 uint32_t data_len, int read_buffer, uint8_t mode,
213 uint8_t buffer_id, uint32_t buffer_offset,
214 ctl_tag_type tag_type, uint8_t control)
216 struct ctl_scsiio *ctsio;
217 struct scsi_write_buffer *cdb;
219 ctl_scsi_zero_io(io);
221 io->io_hdr.io_type = CTL_IO_SCSI;
223 cdb = (struct scsi_write_buffer *)ctsio->cdb;
225 if (read_buffer != 0)
226 cdb->opcode = READ_BUFFER;
228 cdb->opcode = WRITE_BUFFER;
230 cdb->byte2 = mode & RWB_MODE;
231 cdb->buffer_id = buffer_id;
232 scsi_ulto3b(buffer_offset, cdb->offset);
233 scsi_ulto3b(data_len, cdb->length);
234 cdb->control = control;
235 io->io_hdr.io_type = CTL_IO_SCSI;
236 if (read_buffer != 0)
237 io->io_hdr.flags = CTL_FLAG_DATA_IN;
239 io->io_hdr.flags = CTL_FLAG_DATA_OUT;
240 ctsio->tag_type = tag_type;
241 ctsio->cdb_len = sizeof(*cdb);
242 ctsio->ext_data_ptr = data_ptr;
243 ctsio->ext_data_len = data_len;
244 ctsio->ext_sg_entries = 0;
245 ctsio->ext_data_filled = 0;
246 ctsio->sense_len = SSD_FULL_SIZE;
250 ctl_scsi_read_write(union ctl_io *io, uint8_t *data_ptr, uint32_t data_len,
251 int read_op, uint8_t byte2, int minimum_cdb_size,
252 uint64_t lba, uint32_t num_blocks, ctl_tag_type tag_type,
255 struct ctl_scsiio *ctsio;
257 ctl_scsi_zero_io(io);
259 io->io_hdr.io_type = CTL_IO_SCSI;
263 * Pick out the smallest CDB that will hold the user's request.
264 * minimum_cdb_size allows cranking the CDB size up, even for
265 * requests that would not normally need a large CDB. This can be
266 * useful for testing (e.g. to make sure READ_16 support works without
267 * having an array larger than 2TB) and for compatibility -- e.g.
268 * if your device doesn't support READ_6. (ATAPI drives don't.)
270 if ((minimum_cdb_size < 10)
271 && ((lba & 0x1fffff) == lba)
272 && ((num_blocks & 0xff) == num_blocks)
274 struct scsi_rw_6 *cdb;
277 * Note that according to SBC-2, the target should return 256
278 * blocks if the transfer length in a READ(6) or WRITE(6) CDB
279 * is set to 0. Since it's possible that some targets
280 * won't do the right thing, we only send a READ(6) or
281 * WRITE(6) for transfer sizes up to and including 255 blocks.
283 cdb = (struct scsi_rw_6 *)ctsio->cdb;
285 cdb->opcode = (read_op) ? READ_6 : WRITE_6;
286 scsi_ulto3b(lba, cdb->addr);
287 cdb->length = num_blocks & 0xff;
288 cdb->control = control;
290 ctsio->cdb_len = sizeof(*cdb);
292 } else if ((minimum_cdb_size < 12)
293 && ((num_blocks & 0xffff) == num_blocks)
294 && ((lba & 0xffffffff) == lba)) {
295 struct scsi_rw_10 *cdb;
297 cdb = (struct scsi_rw_10 *)ctsio->cdb;
299 cdb->opcode = (read_op) ? READ_10 : WRITE_10;
301 scsi_ulto4b(lba, cdb->addr);
303 scsi_ulto2b(num_blocks, cdb->length);
304 cdb->control = control;
306 ctsio->cdb_len = sizeof(*cdb);
307 } else if ((minimum_cdb_size < 16)
308 && ((num_blocks & 0xffffffff) == num_blocks)
309 && ((lba & 0xffffffff) == lba)) {
310 struct scsi_rw_12 *cdb;
312 cdb = (struct scsi_rw_12 *)ctsio->cdb;
314 cdb->opcode = (read_op) ? READ_12 : WRITE_12;
316 scsi_ulto4b(lba, cdb->addr);
317 scsi_ulto4b(num_blocks, cdb->length);
319 cdb->control = control;
321 ctsio->cdb_len = sizeof(*cdb);
323 struct scsi_rw_16 *cdb;
325 cdb = (struct scsi_rw_16 *)ctsio->cdb;
327 cdb->opcode = (read_op) ? READ_16 : WRITE_16;
329 scsi_u64to8b(lba, cdb->addr);
330 scsi_ulto4b(num_blocks, cdb->length);
332 cdb->control = control;
334 ctsio->cdb_len = sizeof(*cdb);
337 io->io_hdr.io_type = CTL_IO_SCSI;
339 io->io_hdr.flags = CTL_FLAG_DATA_IN;
341 io->io_hdr.flags = CTL_FLAG_DATA_OUT;
342 ctsio->tag_type = tag_type;
343 ctsio->ext_data_ptr = data_ptr;
344 ctsio->ext_data_len = data_len;
345 ctsio->ext_sg_entries = 0;
346 ctsio->ext_data_filled = 0;
347 ctsio->sense_len = SSD_FULL_SIZE;
351 ctl_scsi_write_same(union ctl_io *io, uint8_t *data_ptr, uint32_t data_len,
352 uint8_t byte2, uint64_t lba, uint32_t num_blocks,
353 ctl_tag_type tag_type, uint8_t control)
355 struct ctl_scsiio *ctsio;
356 struct scsi_write_same_16 *cdb;
358 ctl_scsi_zero_io(io);
360 io->io_hdr.io_type = CTL_IO_SCSI;
362 ctsio->cdb_len = sizeof(*cdb);
363 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
364 cdb->opcode = WRITE_SAME_16;
366 scsi_u64to8b(lba, cdb->addr);
367 scsi_ulto4b(num_blocks, cdb->length);
369 cdb->control = control;
371 io->io_hdr.io_type = CTL_IO_SCSI;
372 io->io_hdr.flags = CTL_FLAG_DATA_OUT;
373 ctsio->tag_type = tag_type;
374 ctsio->ext_data_ptr = data_ptr;
375 ctsio->ext_data_len = data_len;
376 ctsio->ext_sg_entries = 0;
377 ctsio->ext_data_filled = 0;
378 ctsio->sense_len = SSD_FULL_SIZE;
382 ctl_scsi_read_capacity(union ctl_io *io, uint8_t *data_ptr, uint32_t data_len,
383 uint32_t addr, int reladr, int pmi,
384 ctl_tag_type tag_type, uint8_t control)
386 struct scsi_read_capacity *cdb;
388 ctl_scsi_zero_io(io);
390 io->io_hdr.io_type = CTL_IO_SCSI;
391 cdb = (struct scsi_read_capacity *)io->scsiio.cdb;
393 cdb->opcode = READ_CAPACITY;
395 cdb->byte2 = SRC_RELADR;
398 scsi_ulto4b(addr, cdb->addr);
399 cdb->control = control;
400 io->io_hdr.io_type = CTL_IO_SCSI;
401 io->io_hdr.flags = CTL_FLAG_DATA_IN;
402 io->scsiio.tag_type = tag_type;
403 io->scsiio.ext_data_ptr = data_ptr;
404 io->scsiio.ext_data_len = data_len;
405 io->scsiio.ext_sg_entries = 0;
406 io->scsiio.ext_data_filled = 0;
407 io->scsiio.sense_len = SSD_FULL_SIZE;
411 ctl_scsi_read_capacity_16(union ctl_io *io, uint8_t *data_ptr,
412 uint32_t data_len, uint64_t addr, int reladr,
413 int pmi, ctl_tag_type tag_type, uint8_t control)
415 struct scsi_read_capacity_16 *cdb;
417 ctl_scsi_zero_io(io);
419 io->io_hdr.io_type = CTL_IO_SCSI;
420 cdb = (struct scsi_read_capacity_16 *)io->scsiio.cdb;
422 cdb->opcode = SERVICE_ACTION_IN;
423 cdb->service_action = SRC16_SERVICE_ACTION;
425 cdb->reladr |= SRC16_RELADR;
427 cdb->reladr |= SRC16_PMI;
428 scsi_u64to8b(addr, cdb->addr);
429 scsi_ulto4b(data_len, cdb->alloc_len);
430 cdb->control = control;
432 io->io_hdr.io_type = CTL_IO_SCSI;
433 io->io_hdr.flags = CTL_FLAG_DATA_IN;
434 io->scsiio.tag_type = tag_type;
435 io->scsiio.ext_data_ptr = data_ptr;
436 io->scsiio.ext_data_len = data_len;
437 io->scsiio.ext_sg_entries = 0;
438 io->scsiio.ext_data_filled = 0;
439 io->scsiio.sense_len = SSD_FULL_SIZE;
443 ctl_scsi_mode_sense(union ctl_io *io, uint8_t *data_ptr, uint32_t data_len,
444 int dbd, int llbaa, uint8_t page_code, uint8_t pc,
445 uint8_t subpage, int minimum_cdb_size,
446 ctl_tag_type tag_type, uint8_t control)
448 ctl_scsi_zero_io(io);
450 if ((minimum_cdb_size < 10)
452 && (data_len < 256)) {
453 struct scsi_mode_sense_6 *cdb;
455 cdb = (struct scsi_mode_sense_6 *)io->scsiio.cdb;
457 cdb->opcode = MODE_SENSE_6;
459 cdb->byte2 |= SMS_DBD;
460 cdb->page = page_code | pc;
461 cdb->subpage = subpage;
462 cdb->length = data_len;
463 cdb->control = control;
465 struct scsi_mode_sense_10 *cdb;
467 cdb = (struct scsi_mode_sense_10 *)io->scsiio.cdb;
469 cdb->opcode = MODE_SENSE_10;
471 cdb->byte2 |= SMS_DBD;
473 cdb->byte2 |= SMS10_LLBAA;
474 cdb->page = page_code | pc;
475 cdb->subpage = subpage;
476 scsi_ulto2b(data_len, cdb->length);
477 cdb->control = control;
480 io->io_hdr.io_type = CTL_IO_SCSI;
481 io->io_hdr.flags = CTL_FLAG_DATA_IN;
482 io->scsiio.tag_type = tag_type;
483 io->scsiio.ext_data_ptr = data_ptr;
484 io->scsiio.ext_data_len = data_len;
485 io->scsiio.ext_sg_entries = 0;
486 io->scsiio.ext_data_filled = 0;
487 io->scsiio.sense_len = SSD_FULL_SIZE;
491 ctl_scsi_start_stop(union ctl_io *io, int start, int load_eject, int immediate,
492 int power_conditions, ctl_tag_type tag_type, uint8_t control)
494 struct scsi_start_stop_unit *cdb;
496 cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
498 ctl_scsi_zero_io(io);
500 cdb->opcode = START_STOP_UNIT;
502 cdb->byte2 |= SSS_IMMED;
503 cdb->how = power_conditions;
505 cdb->how |= SSS_LOEJ;
507 cdb->how |= SSS_START;
508 cdb->control = control;
509 io->io_hdr.io_type = CTL_IO_SCSI;
510 io->io_hdr.flags = CTL_FLAG_DATA_NONE;
511 io->scsiio.tag_type = tag_type;
512 io->scsiio.ext_data_ptr = NULL;
513 io->scsiio.ext_data_len = 0;
514 io->scsiio.ext_sg_entries = 0;
515 io->scsiio.ext_data_filled = 0;
516 io->scsiio.sense_len = SSD_FULL_SIZE;
520 ctl_scsi_sync_cache(union ctl_io *io, int immed, int reladr,
521 int minimum_cdb_size, uint64_t starting_lba,
522 uint32_t block_count, ctl_tag_type tag_type,
525 ctl_scsi_zero_io(io);
527 if ((minimum_cdb_size < 16)
528 && ((block_count & 0xffff) == block_count)
529 && ((starting_lba & 0xffffffff) == starting_lba)) {
530 struct scsi_sync_cache *cdb;
532 cdb = (struct scsi_sync_cache *)io->scsiio.cdb;
534 cdb->opcode = SYNCHRONIZE_CACHE;
536 cdb->byte2 |= SSC_RELADR;
539 cdb->byte2 |= SSC_IMMED;
541 scsi_ulto4b(starting_lba, cdb->begin_lba);
542 scsi_ulto2b(block_count, cdb->lb_count);
543 cdb->control = control;
545 struct scsi_sync_cache_16 *cdb;
547 cdb = (struct scsi_sync_cache_16 *)io->scsiio.cdb;
549 cdb->opcode = SYNCHRONIZE_CACHE_16;
551 cdb->byte2 |= SSC_RELADR;
554 cdb->byte2 |= SSC_IMMED;
556 scsi_u64to8b(starting_lba, cdb->begin_lba);
557 scsi_ulto4b(block_count, cdb->lb_count);
558 cdb->control = control;
560 io->io_hdr.io_type = CTL_IO_SCSI;
561 io->io_hdr.flags = CTL_FLAG_DATA_NONE;
562 io->scsiio.tag_type = tag_type;
563 io->scsiio.ext_data_ptr = NULL;
564 io->scsiio.ext_data_len = 0;
565 io->scsiio.ext_sg_entries = 0;
566 io->scsiio.ext_data_filled = 0;
567 io->scsiio.sense_len = SSD_FULL_SIZE;
571 ctl_scsi_persistent_res_in(union ctl_io *io, uint8_t *data_ptr,
572 uint32_t data_len, int action,
573 ctl_tag_type tag_type, uint8_t control)
576 struct scsi_per_res_in *cdb;
578 ctl_scsi_zero_io(io);
580 cdb = (struct scsi_per_res_in *)io->scsiio.cdb;
581 cdb->opcode = PERSISTENT_RES_IN;
582 cdb->action = action;
583 scsi_ulto2b(data_len, cdb->length);
584 cdb->control = control;
586 io->io_hdr.io_type = CTL_IO_SCSI;
587 io->io_hdr.flags = CTL_FLAG_DATA_IN;
588 io->scsiio.tag_type = tag_type;
589 io->scsiio.ext_data_ptr = data_ptr;
590 io->scsiio.ext_data_len = data_len;
591 io->scsiio.ext_sg_entries = 0;
592 io->scsiio.ext_data_filled = 0;
593 io->scsiio.sense_len = SSD_FULL_SIZE;
597 ctl_scsi_persistent_res_out(union ctl_io *io, uint8_t *data_ptr,
598 uint32_t data_len, int action, int type,
599 uint64_t key, uint64_t sa_key,
600 ctl_tag_type tag_type, uint8_t control)
603 struct scsi_per_res_out *cdb;
604 struct scsi_per_res_out_parms *params;
606 ctl_scsi_zero_io(io);
608 cdb = (struct scsi_per_res_out *)io->scsiio.cdb;
609 params = (struct scsi_per_res_out_parms *)data_ptr;
611 cdb->opcode = PERSISTENT_RES_OUT;
615 cdb->action = action;
637 scsi_ulto4b(data_len, cdb->length);
638 cdb->control = control;
640 scsi_u64to8b(key, params->res_key.key);
641 scsi_u64to8b(sa_key, params->serv_act_res_key);
643 io->io_hdr.io_type = CTL_IO_SCSI;
644 io->io_hdr.flags = CTL_FLAG_DATA_OUT;
645 io->scsiio.tag_type = tag_type;
646 io->scsiio.ext_data_ptr = data_ptr;
647 io->scsiio.ext_data_len = data_len;
648 io->scsiio.ext_sg_entries = 0;
649 io->scsiio.ext_data_filled = 0;
650 io->scsiio.sense_len = SSD_FULL_SIZE;
655 ctl_scsi_maintenance_in(union ctl_io *io, uint8_t *data_ptr, uint32_t data_len,
656 uint8_t action, ctl_tag_type tag_type, uint8_t control)
658 struct scsi_maintenance_in *cdb;
660 ctl_scsi_zero_io(io);
662 cdb = (struct scsi_maintenance_in *)io->scsiio.cdb;
663 cdb->opcode = MAINTENANCE_IN;
665 scsi_ulto4b(data_len, cdb->length);
666 cdb->control = control;
668 io->io_hdr.io_type = CTL_IO_SCSI;
669 io->io_hdr.flags = CTL_FLAG_DATA_IN;
670 io->scsiio.tag_type = tag_type;
671 io->scsiio.ext_data_ptr = data_ptr;
672 io->scsiio.ext_data_len = data_len;
673 io->scsiio.ext_sg_entries = 0;
674 io->scsiio.ext_data_filled = 0;
675 io->scsiio.sense_len = SSD_FULL_SIZE;
680 ctl_scsi_alloc_io(uint32_t initid)
684 io = (union ctl_io *)malloc(sizeof(*io));
688 io->io_hdr.nexus.initid = initid;
695 ctl_scsi_free_io(union ctl_io *io)
701 ctl_scsi_zero_io(union ctl_io *io)
708 pool_ref = io->io_hdr.pool;
709 memset(io, 0, sizeof(*io));
710 io->io_hdr.pool = pool_ref;
712 #endif /* !_KERNEL */
715 ctl_scsi_task_string(struct ctl_taskio *taskio)
719 for (i = 0; i < (sizeof(ctl_task_table)/sizeof(ctl_task_table[0]));
721 if (taskio->task_action == ctl_task_table[i].task_action) {
722 return (ctl_task_table[i].description);
730 ctl_io_sbuf(union ctl_io *io, struct sbuf *sb)
732 const char *task_desc;
735 ctl_scsi_path_string(io, path_str, sizeof(path_str));
737 switch (io->io_hdr.io_type) {
739 sbuf_cat(sb, path_str);
740 ctl_scsi_command_string(&io->scsiio, NULL, sb);
741 sbuf_printf(sb, " Tag: %#jx/%d, Prio: %d\n",
742 io->scsiio.tag_num, io->scsiio.tag_type,
743 io->scsiio.priority);
746 sbuf_cat(sb, path_str);
747 task_desc = ctl_scsi_task_string(&io->taskio);
748 if (task_desc == NULL)
749 sbuf_printf(sb, "Unknown Task Action %d (%#x)",
750 io->taskio.task_action, io->taskio.task_action);
752 sbuf_printf(sb, "Task Action: %s", task_desc);
753 switch (io->taskio.task_action) {
754 case CTL_TASK_ABORT_TASK:
755 sbuf_printf(sb, " Tag: %#jx/%d\n",
756 io->taskio.tag_num, io->taskio.tag_type);
759 sbuf_printf(sb, "\n");
769 ctl_io_error_sbuf(union ctl_io *io, struct scsi_inquiry_data *inq_data,
772 struct ctl_status_desc *status_desc;
779 for (i = 0; i < (sizeof(ctl_status_table)/sizeof(ctl_status_table[0]));
781 if ((io->io_hdr.status & CTL_STATUS_MASK) ==
782 ctl_status_table[i].status) {
783 status_desc = &ctl_status_table[i];
788 ctl_scsi_path_string(io, path_str, sizeof(path_str));
790 sbuf_cat(sb, path_str);
791 if (status_desc == NULL)
792 sbuf_printf(sb, "CTL Status: Unknown status %#x\n",
795 sbuf_printf(sb, "CTL Status: %s\n", status_desc->description);
797 if ((io->io_hdr.io_type == CTL_IO_SCSI)
798 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR)) {
799 sbuf_cat(sb, path_str);
800 sbuf_printf(sb, "SCSI Status: %s\n",
801 ctl_scsi_status_string(&io->scsiio));
803 if (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)
804 ctl_scsi_sense_sbuf(&io->scsiio, inq_data,
810 ctl_io_string(union ctl_io *io, char *str, int str_len)
814 sbuf_new(&sb, str, str_len, SBUF_FIXEDLEN);
815 ctl_io_sbuf(io, &sb);
817 return (sbuf_data(&sb));
821 ctl_io_error_string(union ctl_io *io, struct scsi_inquiry_data *inq_data,
822 char *str, int str_len)
826 sbuf_new(&sb, str, str_len, SBUF_FIXEDLEN);
827 ctl_io_error_sbuf(io, inq_data, &sb);
829 return (sbuf_data(&sb));
835 ctl_io_print(union ctl_io *io)
839 printf("%s", ctl_io_string(io, str, sizeof(str)));
843 ctl_io_error_print(union ctl_io *io, struct scsi_inquiry_data *inq_data)
847 printf("%s", ctl_io_error_string(io, inq_data, str, sizeof(str)));
852 ctl_data_print(union ctl_io *io)
859 if (io->io_hdr.io_type != CTL_IO_SCSI)
861 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
863 if (io->scsiio.kern_sg_entries > 0) /* XXX: Implement */
865 ctl_scsi_path_string(io, path_str, sizeof(path_str));
866 len = min(io->scsiio.kern_data_len, 4096);
867 for (i = 0; i < len; ) {
868 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
869 sbuf_cat(&sb, path_str);
870 sbuf_printf(&sb, " %#jx:%04x:", io->scsiio.tag_num, i);
871 for (j = 0; j < 16 && i < len; i++, j++) {
874 sbuf_printf(&sb, " %02x", io->scsiio.kern_data_ptr[i]);
878 printf("%s", sbuf_data(&sb));
885 ctl_io_error_print(union ctl_io *io, struct scsi_inquiry_data *inq_data,
890 fprintf(ofile, "%s", ctl_io_error_string(io, inq_data, str,