xref: /freebsd/sys/cam/ctl/ctl_util.c (revision c9dbb1cc52b063bbd9ab078a7afc89a8696da659)
1 /*-
2  * Copyright (c) 2003 Silicon Graphics International Corp.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions, and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    substantially similar to the "NO WARRANTY" disclaimer below
13  *    ("Disclaimer") and any redistribution must be conditioned upon
14  *    including a substantially similar Disclaimer requirement for further
15  *    binary redistribution.
16  *
17  * NO WARRANTY
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGES.
29  *
30  * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_util.c#2 $
31  */
32 /*
33  * CAM Target Layer SCSI library
34  *
35  * Author: Ken Merry <ken@FreeBSD.org>
36  */
37 
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 #ifdef _KERNEL
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>
49 #include <sys/time.h>
50 #include <stdint.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #endif /* __KERNEL__ */
55 #include <sys/sbuf.h>
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>
62 
63 struct ctl_status_desc {
64 	ctl_io_status status;
65 	const char *description;
66 };
67 
68 struct ctl_task_desc {
69 	ctl_task_type	task_action;
70 	const char	*description;
71 };
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"},
80 };
81 
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"}
96 };
97 
98 void
99 ctl_scsi_tur(union ctl_io *io, ctl_tag_type tag_type, uint8_t control)
100 {
101 	struct ctl_scsiio *ctsio;
102 	struct scsi_test_unit_ready *cdb;
103 
104 	ctl_scsi_zero_io(io);
105 
106 	io->io_hdr.io_type = CTL_IO_SCSI;
107 	ctsio = &io->scsiio;
108 	cdb = (struct scsi_test_unit_ready *)ctsio->cdb;
109 
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;
120 }
121 
122 void
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,
125 		 uint8_t control)
126 {
127 	struct ctl_scsiio *ctsio;
128 	struct scsi_inquiry *cdb;
129 
130 	ctl_scsi_zero_io(io);
131 
132 	io->io_hdr.io_type = CTL_IO_SCSI;
133 	ctsio = &io->scsiio;
134 	cdb = (struct scsi_inquiry *)ctsio->cdb;
135 
136 	cdb->opcode = INQUIRY;
137 	cdb->byte2 = byte2;
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;
150 }
151 
152 void
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,
155 		       uint8_t control)
156 {
157 	struct ctl_scsiio *ctsio;
158 	struct scsi_request_sense *cdb;
159 
160 	ctl_scsi_zero_io(io);
161 
162 	io->io_hdr.io_type = CTL_IO_SCSI;
163 	ctsio = &io->scsiio;
164 	cdb = (struct scsi_request_sense *)ctsio->cdb;
165 
166 	cdb->opcode = REQUEST_SENSE;
167 	cdb->byte2 = byte2;
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;
179 }
180 
181 void
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,
184 		     uint8_t control)
185 {
186 	struct ctl_scsiio *ctsio;
187 	struct scsi_report_luns *cdb;
188 
189 	ctl_scsi_zero_io(io);
190 
191 	io->io_hdr.io_type = CTL_IO_SCSI;
192 	ctsio = &io->scsiio;
193 	cdb = (struct scsi_report_luns *)ctsio->cdb;
194 
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;
208 }
209 
210 void
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)
215 {
216 	struct ctl_scsiio *ctsio;
217 	struct scsi_write_buffer *cdb;
218 
219 	ctl_scsi_zero_io(io);
220 
221 	io->io_hdr.io_type = CTL_IO_SCSI;
222 	ctsio = &io->scsiio;
223 	cdb = (struct scsi_write_buffer *)ctsio->cdb;
224 
225 	if (read_buffer != 0)
226 		cdb->opcode = READ_BUFFER;
227 	else
228 		cdb->opcode = WRITE_BUFFER;
229 
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;
238 	else
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;
247 }
248 
249 void
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,
253 		    uint8_t control)
254 {
255 	struct ctl_scsiio *ctsio;
256 
257 	ctl_scsi_zero_io(io);
258 
259 	io->io_hdr.io_type = CTL_IO_SCSI;
260 	ctsio = &io->scsiio;
261 
262 	/*
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.)
269 	 */
270 	if ((minimum_cdb_size < 10)
271 	 && ((lba & 0x1fffff) == lba)
272 	 && ((num_blocks & 0xff) == num_blocks)
273 	 && (byte2 == 0)) {
274 		struct scsi_rw_6 *cdb;
275 
276 		/*
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.
282 		 */
283 		cdb = (struct scsi_rw_6 *)ctsio->cdb;
284 
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;
289 
290 		ctsio->cdb_len = sizeof(*cdb);
291 
292 	} else if ((minimum_cdb_size < 12)
293 		&& ((num_blocks & 0xffff) == num_blocks)
294 		&& ((lba & 0xffffffff) == lba)) {
295 		struct scsi_rw_10 *cdb;
296 
297 		cdb = (struct scsi_rw_10 *)ctsio->cdb;
298 
299 		cdb->opcode = (read_op) ? READ_10 : WRITE_10;
300 		cdb->byte2 = byte2;
301 		scsi_ulto4b(lba, cdb->addr);
302 		cdb->reserved = 0;
303 		scsi_ulto2b(num_blocks, cdb->length);
304 		cdb->control = control;
305 
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;
311 
312 		cdb = (struct scsi_rw_12 *)ctsio->cdb;
313 
314 		cdb->opcode = (read_op) ? READ_12 : WRITE_12;
315 		cdb->byte2 = byte2;
316 		scsi_ulto4b(lba, cdb->addr);
317 		scsi_ulto4b(num_blocks, cdb->length);
318 		cdb->reserved = 0;
319 		cdb->control = control;
320 
321 		ctsio->cdb_len = sizeof(*cdb);
322 	} else {
323 		struct scsi_rw_16 *cdb;
324 
325 		cdb = (struct scsi_rw_16 *)ctsio->cdb;
326 
327 		cdb->opcode = (read_op) ? READ_16 : WRITE_16;
328 		cdb->byte2 = byte2;
329 		scsi_u64to8b(lba, cdb->addr);
330 		scsi_ulto4b(num_blocks, cdb->length);
331 		cdb->reserved = 0;
332 		cdb->control = control;
333 
334 		ctsio->cdb_len = sizeof(*cdb);
335 	}
336 
337 	io->io_hdr.io_type = CTL_IO_SCSI;
338 	if (read_op != 0)
339 		io->io_hdr.flags = CTL_FLAG_DATA_IN;
340 	else
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;
348 }
349 
350 void
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)
354 {
355 	struct ctl_scsiio *ctsio;
356 	struct scsi_write_same_16 *cdb;
357 
358 	ctl_scsi_zero_io(io);
359 
360 	io->io_hdr.io_type = CTL_IO_SCSI;
361 	ctsio = &io->scsiio;
362 	ctsio->cdb_len = sizeof(*cdb);
363 	cdb = (struct scsi_write_same_16 *)ctsio->cdb;
364 	cdb->opcode = WRITE_SAME_16;
365 	cdb->byte2 = byte2;
366 	scsi_u64to8b(lba, cdb->addr);
367 	scsi_ulto4b(num_blocks, cdb->length);
368 	cdb->group = 0;
369 	cdb->control = control;
370 
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;
379 }
380 
381 void
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)
385 {
386 	struct scsi_read_capacity *cdb;
387 
388 	ctl_scsi_zero_io(io);
389 
390 	io->io_hdr.io_type = CTL_IO_SCSI;
391 	cdb = (struct scsi_read_capacity *)io->scsiio.cdb;
392 
393 	cdb->opcode = READ_CAPACITY;
394 	if (reladr)
395 		cdb->byte2 = SRC_RELADR;
396 	if (pmi)
397 		cdb->pmi = SRC_PMI;
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;
408 }
409 
410 void
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)
414 {
415 	struct scsi_read_capacity_16 *cdb;
416 
417 	ctl_scsi_zero_io(io);
418 
419 	io->io_hdr.io_type = CTL_IO_SCSI;
420 	cdb = (struct scsi_read_capacity_16 *)io->scsiio.cdb;
421 
422 	cdb->opcode = SERVICE_ACTION_IN;
423 	cdb->service_action = SRC16_SERVICE_ACTION;
424 	if (reladr)
425 		cdb->reladr |= SRC16_RELADR;
426 	if (pmi)
427 		cdb->reladr |= SRC16_PMI;
428 	scsi_u64to8b(addr, cdb->addr);
429 	scsi_ulto4b(data_len, cdb->alloc_len);
430 	cdb->control = control;
431 
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;
440 }
441 
442 void
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)
447 {
448 	ctl_scsi_zero_io(io);
449 
450 	if ((minimum_cdb_size < 10)
451 	 && (llbaa == 0)
452 	 && (data_len < 256)) {
453 		struct scsi_mode_sense_6 *cdb;
454 
455 		cdb = (struct scsi_mode_sense_6 *)io->scsiio.cdb;
456 
457 		cdb->opcode = MODE_SENSE_6;
458 		if (dbd)
459 			cdb->byte2 |= SMS_DBD;
460 		cdb->page = page_code | pc;
461 		cdb->subpage = subpage;
462 		cdb->length = data_len;
463 		cdb->control = control;
464 	} else {
465 		struct scsi_mode_sense_10 *cdb;
466 
467 		cdb = (struct scsi_mode_sense_10 *)io->scsiio.cdb;
468 
469 		cdb->opcode = MODE_SENSE_10;
470 		if (dbd)
471 			cdb->byte2 |= SMS_DBD;
472 		if (llbaa)
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;
478 	}
479 
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;
488 }
489 
490 void
491 ctl_scsi_start_stop(union ctl_io *io, int start, int load_eject, int immediate,
492 		    int power_conditions, int onoffline __unused,
493 		    ctl_tag_type tag_type, uint8_t control)
494 {
495 	struct scsi_start_stop_unit *cdb;
496 
497 	cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
498 
499 	ctl_scsi_zero_io(io);
500 
501 	cdb->opcode = START_STOP_UNIT;
502 	if (immediate)
503 		cdb->byte2 |= SSS_IMMED;
504 #ifdef NEEDTOPORT
505 	if (onoffline)
506 		cdb->byte2 |= SSS_ONOFFLINE;
507 #endif
508 	cdb->how = power_conditions;
509 	if (load_eject)
510 		cdb->how |= SSS_LOEJ;
511 	if (start)
512 		cdb->how |= SSS_START;
513 	cdb->control = control;
514 	io->io_hdr.io_type = CTL_IO_SCSI;
515 	io->io_hdr.flags = CTL_FLAG_DATA_NONE;
516 	io->scsiio.tag_type = tag_type;
517 	io->scsiio.ext_data_ptr = NULL;
518 	io->scsiio.ext_data_len = 0;
519 	io->scsiio.ext_sg_entries = 0;
520 	io->scsiio.ext_data_filled = 0;
521 	io->scsiio.sense_len = SSD_FULL_SIZE;
522 }
523 
524 void
525 ctl_scsi_sync_cache(union ctl_io *io, int immed, int reladr,
526 		    int minimum_cdb_size, uint64_t starting_lba,
527 		    uint32_t block_count, ctl_tag_type tag_type,
528 		    uint8_t control)
529 {
530 	ctl_scsi_zero_io(io);
531 
532 	if ((minimum_cdb_size < 16)
533 	 && ((block_count & 0xffff) == block_count)
534 	 && ((starting_lba & 0xffffffff) == starting_lba)) {
535 		struct scsi_sync_cache *cdb;
536 
537 		cdb = (struct scsi_sync_cache *)io->scsiio.cdb;
538 
539 		cdb->opcode = SYNCHRONIZE_CACHE;
540 		if (reladr)
541 			cdb->byte2 |= SSC_RELADR;
542 
543 		if (immed)
544 			cdb->byte2 |= SSC_IMMED;
545 
546 		scsi_ulto4b(starting_lba, cdb->begin_lba);
547 		scsi_ulto2b(block_count, cdb->lb_count);
548 		cdb->control = control;
549 	} else {
550 		struct scsi_sync_cache_16 *cdb;
551 
552 		cdb = (struct scsi_sync_cache_16 *)io->scsiio.cdb;
553 
554 		cdb->opcode = SYNCHRONIZE_CACHE_16;
555 		if (reladr)
556 			cdb->byte2 |= SSC_RELADR;
557 
558 		if (immed)
559 			cdb->byte2 |= SSC_IMMED;
560 
561 		scsi_u64to8b(starting_lba, cdb->begin_lba);
562 		scsi_ulto4b(block_count, cdb->lb_count);
563 		cdb->control = control;
564 	}
565 	io->io_hdr.io_type = CTL_IO_SCSI;
566 	io->io_hdr.flags = CTL_FLAG_DATA_NONE;
567 	io->scsiio.tag_type = tag_type;
568 	io->scsiio.ext_data_ptr = NULL;
569 	io->scsiio.ext_data_len = 0;
570 	io->scsiio.ext_sg_entries = 0;
571 	io->scsiio.ext_data_filled = 0;
572 	io->scsiio.sense_len = SSD_FULL_SIZE;
573 }
574 
575 void
576 ctl_scsi_persistent_res_in(union ctl_io *io, uint8_t *data_ptr,
577 			   uint32_t data_len, int action,
578 			   ctl_tag_type tag_type, uint8_t control)
579 {
580 
581 	struct scsi_per_res_in *cdb;
582 
583 	ctl_scsi_zero_io(io);
584 
585 	cdb = (struct scsi_per_res_in *)io->scsiio.cdb;
586 	cdb->opcode = PERSISTENT_RES_IN;
587 	cdb->action = action;
588 	scsi_ulto2b(data_len, cdb->length);
589 	cdb->control = control;
590 
591 	io->io_hdr.io_type = CTL_IO_SCSI;
592 	io->io_hdr.flags = CTL_FLAG_DATA_IN;
593 	io->scsiio.tag_type = tag_type;
594 	io->scsiio.ext_data_ptr = data_ptr;
595 	io->scsiio.ext_data_len = data_len;
596 	io->scsiio.ext_sg_entries = 0;
597 	io->scsiio.ext_data_filled = 0;
598 	io->scsiio.sense_len = SSD_FULL_SIZE;
599 }
600 
601 void
602 ctl_scsi_persistent_res_out(union ctl_io *io, uint8_t *data_ptr,
603 			    uint32_t data_len, int action, int type,
604 			    uint64_t key, uint64_t sa_key,
605 			    ctl_tag_type tag_type, uint8_t control)
606 {
607 
608 	struct scsi_per_res_out *cdb;
609 	struct scsi_per_res_out_parms *params;
610 
611 	ctl_scsi_zero_io(io);
612 
613 	cdb = (struct scsi_per_res_out *)io->scsiio.cdb;
614 	params = (struct scsi_per_res_out_parms *)data_ptr;
615 
616 	cdb->opcode = PERSISTENT_RES_OUT;
617 	if (action == 5)
618 	    cdb->action = 6;
619 	else
620 	    cdb->action = action;
621 	switch(type)
622 	{
623 	    case 0:
624 		    cdb->scope_type = 1;
625 			break;
626 	    case 1:
627 		    cdb->scope_type = 3;
628 			break;
629 	    case 2:
630 		    cdb->scope_type = 5;
631 			break;
632 	    case 3:
633 		    cdb->scope_type = 6;
634 			break;
635 	    case 4:
636 		    cdb->scope_type = 7;
637 			break;
638 	    case 5:
639 		    cdb->scope_type = 8;
640 			break;
641 	}
642 	scsi_ulto4b(data_len, cdb->length);
643 	cdb->control = control;
644 
645 	scsi_u64to8b(key, params->res_key.key);
646 	scsi_u64to8b(sa_key, params->serv_act_res_key);
647 
648 	io->io_hdr.io_type = CTL_IO_SCSI;
649 	io->io_hdr.flags = CTL_FLAG_DATA_OUT;
650 	io->scsiio.tag_type = tag_type;
651 	io->scsiio.ext_data_ptr = data_ptr;
652 	io->scsiio.ext_data_len = data_len;
653 	io->scsiio.ext_sg_entries = 0;
654 	io->scsiio.ext_data_filled = 0;
655 	io->scsiio.sense_len = SSD_FULL_SIZE;
656 
657 }
658 
659 void
660 ctl_scsi_maintenance_in(union ctl_io *io, uint8_t *data_ptr, uint32_t data_len,
661 			uint8_t action, ctl_tag_type tag_type, uint8_t control)
662 {
663 	struct scsi_maintenance_in *cdb;
664 
665 	ctl_scsi_zero_io(io);
666 
667 	cdb = (struct scsi_maintenance_in *)io->scsiio.cdb;
668 	cdb->opcode = MAINTENANCE_IN;
669 	cdb->byte2 = action;
670 	scsi_ulto4b(data_len, cdb->length);
671 	cdb->control = control;
672 
673 	io->io_hdr.io_type = CTL_IO_SCSI;
674 	io->io_hdr.flags = CTL_FLAG_DATA_IN;
675 	io->scsiio.tag_type = tag_type;
676 	io->scsiio.ext_data_ptr = data_ptr;
677 	io->scsiio.ext_data_len = data_len;
678 	io->scsiio.ext_sg_entries = 0;
679 	io->scsiio.ext_data_filled = 0;
680 	io->scsiio.sense_len = SSD_FULL_SIZE;
681 }
682 
683 #ifndef _KERNEL
684 union ctl_io *
685 ctl_scsi_alloc_io(uint32_t initid)
686 {
687 	union ctl_io *io;
688 
689 	io = (union ctl_io *)malloc(sizeof(*io));
690 	if (io == NULL)
691 		goto bailout;
692 
693 	io->io_hdr.nexus.initid = initid;
694 
695 bailout:
696 	return (io);
697 }
698 
699 void
700 ctl_scsi_free_io(union ctl_io *io)
701 {
702 	free(io);
703 }
704 
705 #endif /* !_KERNEL */
706 void
707 ctl_scsi_zero_io(union ctl_io *io)
708 {
709 	void *pool_ref;
710 
711 	if (io == NULL)
712 		return;
713 
714 	pool_ref = io->io_hdr.pool;
715 
716 	memset(io, 0, sizeof(*io));
717 
718 	io->io_hdr.pool = pool_ref;
719 }
720 
721 const char *
722 ctl_scsi_task_string(struct ctl_taskio *taskio)
723 {
724 	unsigned int i;
725 
726 	for (i = 0; i < (sizeof(ctl_task_table)/sizeof(ctl_task_table[0]));
727 	     i++) {
728 		if (taskio->task_action == ctl_task_table[i].task_action) {
729 			return (ctl_task_table[i].description);
730 		}
731 	}
732 
733 	return (NULL);
734 }
735 
736 void
737 ctl_io_sbuf(union ctl_io *io, struct sbuf *sb)
738 {
739 	const char *task_desc;
740 	char path_str[64];
741 
742 	ctl_scsi_path_string(io, path_str, sizeof(path_str));
743 
744 	switch (io->io_hdr.io_type) {
745 	case CTL_IO_SCSI:
746 		sbuf_cat(sb, path_str);
747 		ctl_scsi_command_string(&io->scsiio, NULL, sb);
748 		sbuf_printf(sb, " Tag: %#x/%d\n",
749 			    io->scsiio.tag_num, io->scsiio.tag_type);
750 		break;
751 	case CTL_IO_TASK:
752 		sbuf_cat(sb, path_str);
753 		task_desc = ctl_scsi_task_string(&io->taskio);
754 		if (task_desc == NULL)
755 			sbuf_printf(sb, "Unknown Task Action %d (%#x)",
756 			    io->taskio.task_action, io->taskio.task_action);
757 		else
758 			sbuf_printf(sb, "Task Action: %s", task_desc);
759 		switch (io->taskio.task_action) {
760 		case CTL_TASK_ABORT_TASK:
761 			sbuf_printf(sb, " Tag: %#x/%d\n",
762 			    io->taskio.tag_num, io->taskio.tag_type);
763 			break;
764 		default:
765 			sbuf_printf(sb, "\n");
766 			break;
767 		}
768 		break;
769 	default:
770 		break;
771 	}
772 }
773 
774 void
775 ctl_io_error_sbuf(union ctl_io *io, struct scsi_inquiry_data *inq_data,
776 		  struct sbuf *sb)
777 {
778 	struct ctl_status_desc *status_desc;
779 	char path_str[64];
780 	unsigned int i;
781 
782 	ctl_io_sbuf(io, sb);
783 
784 	status_desc = NULL;
785 	for (i = 0; i < (sizeof(ctl_status_table)/sizeof(ctl_status_table[0]));
786 	     i++) {
787 		if ((io->io_hdr.status & CTL_STATUS_MASK) ==
788 		     ctl_status_table[i].status) {
789 			status_desc = &ctl_status_table[i];
790 			break;
791 		}
792 	}
793 
794 	ctl_scsi_path_string(io, path_str, sizeof(path_str));
795 
796 	sbuf_cat(sb, path_str);
797 	if (status_desc == NULL)
798 		sbuf_printf(sb, "CTL Status: Unknown status %#x\n",
799 			    io->io_hdr.status);
800 	else
801 		sbuf_printf(sb, "CTL Status: %s\n", status_desc->description);
802 
803 	if ((io->io_hdr.io_type == CTL_IO_SCSI)
804 	 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR)) {
805 		sbuf_cat(sb, path_str);
806 		sbuf_printf(sb, "SCSI Status: %s\n",
807 			    ctl_scsi_status_string(&io->scsiio));
808 
809 		if (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)
810 			ctl_scsi_sense_sbuf(&io->scsiio, inq_data,
811 					    sb, SSS_FLAG_NONE);
812 	}
813 }
814 
815 char *
816 ctl_io_string(union ctl_io *io, char *str, int str_len)
817 {
818 	struct sbuf sb;
819 
820 	sbuf_new(&sb, str, str_len, SBUF_FIXEDLEN);
821 	ctl_io_sbuf(io, &sb);
822 	sbuf_finish(&sb);
823 	return (sbuf_data(&sb));
824 }
825 
826 char *
827 ctl_io_error_string(union ctl_io *io, struct scsi_inquiry_data *inq_data,
828 		    char *str, int str_len)
829 {
830 	struct sbuf sb;
831 
832 	sbuf_new(&sb, str, str_len, SBUF_FIXEDLEN);
833 	ctl_io_error_sbuf(io, inq_data, &sb);
834 	sbuf_finish(&sb);
835 	return (sbuf_data(&sb));
836 }
837 
838 #ifdef _KERNEL
839 
840 void
841 ctl_io_print(union ctl_io *io)
842 {
843 	char str[512];
844 
845 	printf("%s", ctl_io_string(io, str, sizeof(str)));
846 }
847 
848 void
849 ctl_io_error_print(union ctl_io *io, struct scsi_inquiry_data *inq_data)
850 {
851 	char str[512];
852 #ifdef NEEDTOPORT
853 	char *message;
854 	char *line;
855 
856 	message = io_error_string(io, inq_data, str, sizeof(str));
857 
858 	for (line = strsep(&message, "\n"); line != NULL;
859 	     line = strsep(&message, "\n")) {
860 		csevent_log(CSC_CTL | CSC_SHELF_SW | CTL_ERROR_REPORT,
861                             csevent_LogType_Trace,
862                             csevent_Severity_Information,
863                             csevent_AlertLevel_Green,
864                             csevent_FRU_Firmware,
865                             csevent_FRU_Unknown, "%s", line);
866 	}
867 #else
868 	printf("%s", ctl_io_error_string(io, inq_data, str, sizeof(str)));
869 #endif
870 
871 }
872 
873 void
874 ctl_data_print(union ctl_io *io)
875 {
876 	char str[128];
877 	char path_str[64];
878 	struct sbuf sb;
879 	int i, j, len;
880 
881 	if (io->io_hdr.io_type != CTL_IO_SCSI)
882 		return;
883 	if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
884 		return;
885 	if (io->scsiio.ext_sg_entries > 0)	/* XXX: Implement */
886 		return;
887 	ctl_scsi_path_string(io, path_str, sizeof(path_str));
888 	len = min(io->scsiio.kern_data_len, 4096);
889 	for (i = 0; i < len; ) {
890 		sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
891 		sbuf_cat(&sb, path_str);
892 		sbuf_printf(&sb, " %#6x:%04x:", io->scsiio.tag_num, i);
893 		for (j = 0; j < 16 && i < len; i++, j++) {
894 			if (j == 8)
895 				sbuf_cat(&sb, " ");
896 			sbuf_printf(&sb, " %02x", io->scsiio.kern_data_ptr[i]);
897 		}
898 		sbuf_cat(&sb, "\n");
899 		sbuf_finish(&sb);
900 		printf("%s", sbuf_data(&sb));
901 	}
902 }
903 
904 #else /* _KERNEL */
905 
906 void
907 ctl_io_error_print(union ctl_io *io, struct scsi_inquiry_data *inq_data,
908 		   FILE *ofile)
909 {
910 	char str[512];
911 
912 	fprintf(ofile, "%s", ctl_io_error_string(io, inq_data, str,
913 		sizeof(str)));
914 }
915 
916 #endif /* _KERNEL */
917 
918 /*
919  * vim: ts=8
920  */
921