blob: da7851ec6edd39808604eb2294a04b61d4379a28 [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"
Dmitry V. Levinfdb896e2014-02-25 23:04:55 +000030
31#ifdef HAVE_SCSI_SG_H
32
33# include <sys/ioctl.h>
34# include <scsi/sg.h>
Dmitry V. Levinb011af52007-06-30 11:37:09 +000035
Dmitry V. Levin0ed617b2014-04-25 23:30:54 +000036#include "xlat/sg_io_dxfer_direction.h"
Dmitry V. Levinb011af52007-06-30 11:37:09 +000037
38static void
Bart Van Asschec7602512015-02-06 13:36:26 +010039print_sg_io_buffer(struct tcb *tcp, unsigned long addr, const unsigned int len)
Dmitry V. Levinb011af52007-06-30 11:37:09 +000040{
41 unsigned char *buf = NULL;
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +000042 unsigned int allocated, i;
Dmitry V. Levinb011af52007-06-30 11:37:09 +000043
Bart Van Asscheea6972c2015-02-06 13:36:26 +010044 tprints("[");
Dmitry V. Levinb011af52007-06-30 11:37:09 +000045 if (len == 0)
Bart Van Asscheea6972c2015-02-06 13:36:26 +010046 goto out;
Dmitry V. Levinb011af52007-06-30 11:37:09 +000047 allocated = (len > max_strlen) ? max_strlen : len;
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +000048 if ((buf = malloc(allocated)) == NULL ||
Bart Van Asschec7602512015-02-06 13:36:26 +010049 umoven(tcp, addr, allocated, (char *) buf) < 0) {
50 tprintf("%#lx", addr);
Bart Van Asscheea6972c2015-02-06 13:36:26 +010051 goto out;
Dmitry V. Levinb011af52007-06-30 11:37:09 +000052 }
53 tprintf("%02x", buf[0]);
54 for (i = 1; i < allocated; ++i)
55 tprintf(", %02x", buf[i]);
Dmitry V. Levinb011af52007-06-30 11:37:09 +000056 if (allocated != len)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +020057 tprints(", ...");
Bart Van Asscheea6972c2015-02-06 13:36:26 +010058out:
59 free(buf);
60 tprints("]");
Dmitry V. Levinb011af52007-06-30 11:37:09 +000061}
62
63static void
64print_sg_io_req(struct tcb *tcp, struct sg_io_hdr *sg_io)
65{
66 tprintf("{'%c', ", sg_io->interface_id);
67 printxval(sg_io_dxfer_direction, sg_io->dxfer_direction,
68 "SG_DXFER_???");
Bart Van Asscheea6972c2015-02-06 13:36:26 +010069 tprintf(", cmd[%u]=", sg_io->cmd_len);
Bart Van Asschec7602512015-02-06 13:36:26 +010070 print_sg_io_buffer(tcp, (unsigned long) sg_io->cmdp, sg_io->cmd_len);
Bart Van Asscheea6972c2015-02-06 13:36:26 +010071 tprintf(", mx_sb_len=%d, ", sg_io->mx_sb_len);
Dmitry V. Levinb011af52007-06-30 11:37:09 +000072 tprintf("iovec_count=%d, ", sg_io->iovec_count);
73 tprintf("dxfer_len=%u, ", sg_io->dxfer_len);
74 tprintf("timeout=%u, ", sg_io->timeout);
75 tprintf("flags=%#x", sg_io->flags);
76
77 if (sg_io->dxfer_direction == SG_DXFER_TO_DEV ||
78 sg_io->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
Bart Van Assche791145e2015-02-06 13:36:26 +010079 tprintf(", data[%u]=", sg_io->dxfer_len);
80 if (sg_io->iovec_count)
81 tprint_iov_upto(tcp, sg_io->iovec_count,
82 (unsigned long) sg_io->dxferp, 1,
83 sg_io->dxfer_len);
84 else
85 print_sg_io_buffer(tcp, (unsigned long) sg_io->dxferp,
86 sg_io->dxfer_len);
Dmitry V. Levinb011af52007-06-30 11:37:09 +000087 }
88}
89
90static void
91print_sg_io_res(struct tcb *tcp, struct sg_io_hdr *sg_io)
92{
93 if (sg_io->dxfer_direction == SG_DXFER_FROM_DEV ||
94 sg_io->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
Bart Van Assche0014bb12015-02-06 13:36:26 +010095 uint32_t din_len = sg_io->dxfer_len;
96
97 if (sg_io->resid > 0)
98 din_len -= sg_io->resid;
Bart Van Assche791145e2015-02-06 13:36:26 +010099 tprintf(", data[%u]=", din_len);
100 if (sg_io->iovec_count)
101 tprint_iov_upto(tcp, sg_io->iovec_count,
102 (unsigned long) sg_io->dxferp, 1,
103 din_len);
104 else
105 print_sg_io_buffer(tcp, (unsigned long) sg_io->dxferp,
106 din_len);
Dmitry V. Levinb011af52007-06-30 11:37:09 +0000107 }
108 tprintf(", status=%02x, ", sg_io->status);
109 tprintf("masked_status=%02x, ", sg_io->masked_status);
Bart Van Asscheea6972c2015-02-06 13:36:26 +0100110 tprintf("sb[%u]=", sg_io->sb_len_wr);
Bart Van Asschec7602512015-02-06 13:36:26 +0100111 print_sg_io_buffer(tcp, (unsigned long) sg_io->sbp, sg_io->sb_len_wr);
Bart Van Asscheea6972c2015-02-06 13:36:26 +0100112 tprintf(", host_status=%#x, ", sg_io->host_status);
Dmitry V. Levinb011af52007-06-30 11:37:09 +0000113 tprintf("driver_status=%#x, ", sg_io->driver_status);
114 tprintf("resid=%d, ", sg_io->resid);
115 tprintf("duration=%d, ", sg_io->duration);
116 tprintf("info=%#x}", sg_io->info);
117}
118
119int
Dmitry V. Levinc7afb482015-01-19 18:44:21 +0000120scsi_ioctl(struct tcb *tcp, const unsigned int code, long arg)
Dmitry V. Levinb011af52007-06-30 11:37:09 +0000121{
122 switch (code) {
123 case SG_IO:
124 if (entering(tcp)) {
125 struct sg_io_hdr sg_io;
126
127 if (umove(tcp, arg, &sg_io) < 0)
128 tprintf(", %#lx", arg);
129 else {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200130 tprints(", ");
Dmitry V. Levinb011af52007-06-30 11:37:09 +0000131 print_sg_io_req(tcp, &sg_io);
132 }
133 }
134 if (exiting(tcp)) {
135 struct sg_io_hdr sg_io;
136
137 if (!syserror(tcp) && umove(tcp, arg, &sg_io) >= 0)
138 print_sg_io_res(tcp, &sg_io);
139 else
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200140 tprints("}");
Dmitry V. Levinb011af52007-06-30 11:37:09 +0000141 }
142 break;
143 default:
144 if (entering(tcp))
145 tprintf(", %#lx", arg);
146 break;
147 }
148 return 1;
149}
Dmitry V. Levinfdb896e2014-02-25 23:04:55 +0000150
151#endif /* HAVE_SCSI_SG_H */