blob: c49f9548b037aeefcd4bfe5af700909aa1717e7f [file] [log] [blame]
Elliott Hughesbb0c2d52014-01-07 17:34:14 -08001/*
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 */
Jeff Brownf76f96e2012-03-02 16:23:23 -080028
Elliott Hughesbb0c2d52014-01-07 17:34:14 -080029#include "defs.h"
Elliott Hughes5dec78d2014-02-26 15:56:23 -080030
31#ifdef HAVE_SCSI_SG_H
32
33# include <sys/ioctl.h>
34# include <scsi/sg.h>
Jeff Brownf76f96e2012-03-02 16:23:23 -080035
36static const struct xlat sg_io_dxfer_direction[] = {
Elliott Hughes5dec78d2014-02-26 15:56:23 -080037 XLAT(SG_DXFER_NONE),
38 XLAT(SG_DXFER_TO_DEV),
39 XLAT(SG_DXFER_FROM_DEV),
40 XLAT(SG_DXFER_TO_FROM_DEV),
41 XLAT_END
Jeff Brownf76f96e2012-03-02 16:23:23 -080042};
43
44static void
45print_sg_io_buffer(struct tcb *tcp, unsigned char *addr, int len)
46{
47 unsigned char *buf = NULL;
48 int allocated, i;
49
50 if (len == 0)
51 return;
52 allocated = (len > max_strlen) ? max_strlen : len;
53 if (len < 0 ||
54 (buf = malloc(allocated)) == NULL ||
55 umoven(tcp, (unsigned long) addr, allocated, (char *) buf) < 0) {
56 tprintf("%p", addr);
57 free(buf);
58 return;
59 }
60 tprintf("%02x", buf[0]);
61 for (i = 1; i < allocated; ++i)
62 tprintf(", %02x", buf[i]);
63 free(buf);
64 if (allocated != len)
Elliott Hughesbb0c2d52014-01-07 17:34:14 -080065 tprints(", ...");
Jeff Brownf76f96e2012-03-02 16:23:23 -080066}
67
68static void
69print_sg_io_req(struct tcb *tcp, struct sg_io_hdr *sg_io)
70{
71 tprintf("{'%c', ", sg_io->interface_id);
72 printxval(sg_io_dxfer_direction, sg_io->dxfer_direction,
73 "SG_DXFER_???");
74 tprintf(", cmd[%u]=[", sg_io->cmd_len);
75 print_sg_io_buffer(tcp, sg_io->cmdp, sg_io->cmd_len);
76 tprintf("], mx_sb_len=%d, ", sg_io->mx_sb_len);
77 tprintf("iovec_count=%d, ", sg_io->iovec_count);
78 tprintf("dxfer_len=%u, ", sg_io->dxfer_len);
79 tprintf("timeout=%u, ", sg_io->timeout);
80 tprintf("flags=%#x", sg_io->flags);
81
82 if (sg_io->dxfer_direction == SG_DXFER_TO_DEV ||
83 sg_io->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
84 tprintf(", data[%u]=[", sg_io->dxfer_len);
85 printstr(tcp, (unsigned long) sg_io->dxferp,
86 sg_io->dxfer_len);
Elliott Hughesbb0c2d52014-01-07 17:34:14 -080087 tprints("]");
Jeff Brownf76f96e2012-03-02 16:23:23 -080088 }
89}
90
91static void
92print_sg_io_res(struct tcb *tcp, struct sg_io_hdr *sg_io)
93{
94 if (sg_io->dxfer_direction == SG_DXFER_FROM_DEV ||
95 sg_io->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
96 tprintf(", data[%u]=[", sg_io->dxfer_len);
97 printstr(tcp, (unsigned long) sg_io->dxferp,
98 sg_io->dxfer_len);
Elliott Hughesbb0c2d52014-01-07 17:34:14 -080099 tprints("]");
Jeff Brownf76f96e2012-03-02 16:23:23 -0800100 }
101 tprintf(", status=%02x, ", sg_io->status);
102 tprintf("masked_status=%02x, ", sg_io->masked_status);
103 tprintf("sb[%u]=[", sg_io->sb_len_wr);
104 print_sg_io_buffer(tcp, sg_io->sbp, sg_io->sb_len_wr);
105 tprintf("], host_status=%#x, ", sg_io->host_status);
106 tprintf("driver_status=%#x, ", sg_io->driver_status);
107 tprintf("resid=%d, ", sg_io->resid);
108 tprintf("duration=%d, ", sg_io->duration);
109 tprintf("info=%#x}", sg_io->info);
110}
111
112int
113scsi_ioctl(struct tcb *tcp, long code, long arg)
114{
115 switch (code) {
116 case SG_IO:
117 if (entering(tcp)) {
118 struct sg_io_hdr sg_io;
119
120 if (umove(tcp, arg, &sg_io) < 0)
121 tprintf(", %#lx", arg);
122 else {
Elliott Hughesbb0c2d52014-01-07 17:34:14 -0800123 tprints(", ");
Jeff Brownf76f96e2012-03-02 16:23:23 -0800124 print_sg_io_req(tcp, &sg_io);
125 }
126 }
127 if (exiting(tcp)) {
128 struct sg_io_hdr sg_io;
129
130 if (!syserror(tcp) && umove(tcp, arg, &sg_io) >= 0)
131 print_sg_io_res(tcp, &sg_io);
132 else
Elliott Hughesbb0c2d52014-01-07 17:34:14 -0800133 tprints("}");
Jeff Brownf76f96e2012-03-02 16:23:23 -0800134 }
135 break;
136 default:
137 if (entering(tcp))
138 tprintf(", %#lx", arg);
139 break;
140 }
141 return 1;
142}
Elliott Hughes5dec78d2014-02-26 15:56:23 -0800143
144#endif /* HAVE_SCSI_SG_H */