blob: f0d4c2d7ba9e330a482a3b8219ac8cf750b7978d [file] [log] [blame]
Dmitry V. Levin27aeaa22012-03-16 10:43:32 +00001/*
2 * Copyright (c) 2007 Vladimir Nadvornik <nadvornik@suse.cz>
3 * Copyright (c) 2007 Dmitry V. Levin <ldv@altlinux.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
Dmitry V. Levinb011af52007-06-30 11:37:09 +000029#include "defs.h"
30
Dmitry V. Levinb011af52007-06-30 11:37:09 +000031#include <sys/ioctl.h>
32#include <scsi/sg.h>
33
34static const struct xlat sg_io_dxfer_direction[] = {
35 {SG_DXFER_NONE, "SG_DXFER_NONE"},
36 {SG_DXFER_TO_DEV, "SG_DXFER_TO_DEV"},
37 {SG_DXFER_FROM_DEV, "SG_DXFER_FROM_DEV"},
38 {SG_DXFER_TO_FROM_DEV, "SG_DXFER_TO_FROM_DEV"},
39 {0, NULL}
40};
41
42static void
43print_sg_io_buffer(struct tcb *tcp, unsigned char *addr, int len)
44{
45 unsigned char *buf = NULL;
46 int allocated, i;
47
48 if (len == 0)
49 return;
50 allocated = (len > max_strlen) ? max_strlen : len;
51 if (len < 0 ||
52 (buf = malloc(allocated)) == NULL ||
53 umoven(tcp, (unsigned long) addr, allocated, (char *) buf) < 0) {
54 tprintf("%p", addr);
55 free(buf);
56 return;
57 }
58 tprintf("%02x", buf[0]);
59 for (i = 1; i < allocated; ++i)
60 tprintf(", %02x", buf[i]);
61 free(buf);
62 if (allocated != len)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +020063 tprints(", ...");
Dmitry V. Levinb011af52007-06-30 11:37:09 +000064}
65
66static void
67print_sg_io_req(struct tcb *tcp, struct sg_io_hdr *sg_io)
68{
69 tprintf("{'%c', ", sg_io->interface_id);
70 printxval(sg_io_dxfer_direction, sg_io->dxfer_direction,
71 "SG_DXFER_???");
72 tprintf(", cmd[%u]=[", sg_io->cmd_len);
73 print_sg_io_buffer(tcp, sg_io->cmdp, sg_io->cmd_len);
74 tprintf("], mx_sb_len=%d, ", sg_io->mx_sb_len);
75 tprintf("iovec_count=%d, ", sg_io->iovec_count);
76 tprintf("dxfer_len=%u, ", sg_io->dxfer_len);
77 tprintf("timeout=%u, ", sg_io->timeout);
78 tprintf("flags=%#x", sg_io->flags);
79
80 if (sg_io->dxfer_direction == SG_DXFER_TO_DEV ||
81 sg_io->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
82 tprintf(", data[%u]=[", sg_io->dxfer_len);
83 printstr(tcp, (unsigned long) sg_io->dxferp,
84 sg_io->dxfer_len);
Denys Vlasenko60fe8c12011-09-01 10:00:28 +020085 tprints("]");
Dmitry V. Levinb011af52007-06-30 11:37:09 +000086 }
87}
88
89static void
90print_sg_io_res(struct tcb *tcp, struct sg_io_hdr *sg_io)
91{
92 if (sg_io->dxfer_direction == SG_DXFER_FROM_DEV ||
93 sg_io->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
94 tprintf(", data[%u]=[", sg_io->dxfer_len);
95 printstr(tcp, (unsigned long) sg_io->dxferp,
96 sg_io->dxfer_len);
Denys Vlasenko60fe8c12011-09-01 10:00:28 +020097 tprints("]");
Dmitry V. Levinb011af52007-06-30 11:37:09 +000098 }
99 tprintf(", status=%02x, ", sg_io->status);
100 tprintf("masked_status=%02x, ", sg_io->masked_status);
101 tprintf("sb[%u]=[", sg_io->sb_len_wr);
102 print_sg_io_buffer(tcp, sg_io->sbp, sg_io->sb_len_wr);
103 tprintf("], host_status=%#x, ", sg_io->host_status);
104 tprintf("driver_status=%#x, ", sg_io->driver_status);
105 tprintf("resid=%d, ", sg_io->resid);
106 tprintf("duration=%d, ", sg_io->duration);
107 tprintf("info=%#x}", sg_io->info);
108}
109
110int
111scsi_ioctl(struct tcb *tcp, long code, long arg)
112{
113 switch (code) {
114 case SG_IO:
115 if (entering(tcp)) {
116 struct sg_io_hdr sg_io;
117
118 if (umove(tcp, arg, &sg_io) < 0)
119 tprintf(", %#lx", arg);
120 else {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200121 tprints(", ");
Dmitry V. Levinb011af52007-06-30 11:37:09 +0000122 print_sg_io_req(tcp, &sg_io);
123 }
124 }
125 if (exiting(tcp)) {
126 struct sg_io_hdr sg_io;
127
128 if (!syserror(tcp) && umove(tcp, arg, &sg_io) >= 0)
129 print_sg_io_res(tcp, &sg_io);
130 else
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200131 tprints("}");
Dmitry V. Levinb011af52007-06-30 11:37:09 +0000132 }
133 break;
134 default:
135 if (entering(tcp))
136 tprintf(", %#lx", arg);
137 break;
138 }
139 return 1;
140}