1*cf84ac9aSAndroid Build Coastguard Worker /*
2*cf84ac9aSAndroid Build Coastguard Worker * Copyright (c) 2007 Vladimir Nadvornik <[email protected]>
3*cf84ac9aSAndroid Build Coastguard Worker * Copyright (c) 2007-2017 Dmitry V. Levin <[email protected]>
4*cf84ac9aSAndroid Build Coastguard Worker * Copyright (c) 2015 Bart Van Assche <[email protected]>
5*cf84ac9aSAndroid Build Coastguard Worker * All rights reserved.
6*cf84ac9aSAndroid Build Coastguard Worker *
7*cf84ac9aSAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
8*cf84ac9aSAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions
9*cf84ac9aSAndroid Build Coastguard Worker * are met:
10*cf84ac9aSAndroid Build Coastguard Worker * 1. Redistributions of source code must retain the above copyright
11*cf84ac9aSAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer.
12*cf84ac9aSAndroid Build Coastguard Worker * 2. Redistributions in binary form must reproduce the above copyright
13*cf84ac9aSAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in the
14*cf84ac9aSAndroid Build Coastguard Worker * documentation and/or other materials provided with the distribution.
15*cf84ac9aSAndroid Build Coastguard Worker * 3. The name of the author may not be used to endorse or promote products
16*cf84ac9aSAndroid Build Coastguard Worker * derived from this software without specific prior written permission.
17*cf84ac9aSAndroid Build Coastguard Worker *
18*cf84ac9aSAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19*cf84ac9aSAndroid Build Coastguard Worker * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20*cf84ac9aSAndroid Build Coastguard Worker * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21*cf84ac9aSAndroid Build Coastguard Worker * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22*cf84ac9aSAndroid Build Coastguard Worker * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23*cf84ac9aSAndroid Build Coastguard Worker * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*cf84ac9aSAndroid Build Coastguard Worker * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*cf84ac9aSAndroid Build Coastguard Worker * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*cf84ac9aSAndroid Build Coastguard Worker * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27*cf84ac9aSAndroid Build Coastguard Worker * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*cf84ac9aSAndroid Build Coastguard Worker */
29*cf84ac9aSAndroid Build Coastguard Worker
30*cf84ac9aSAndroid Build Coastguard Worker #include "defs.h"
31*cf84ac9aSAndroid Build Coastguard Worker
32*cf84ac9aSAndroid Build Coastguard Worker #ifdef HAVE_SCSI_SG_H
33*cf84ac9aSAndroid Build Coastguard Worker
34*cf84ac9aSAndroid Build Coastguard Worker #include DEF_MPERS_TYPE(struct_sg_io_hdr)
35*cf84ac9aSAndroid Build Coastguard Worker
36*cf84ac9aSAndroid Build Coastguard Worker # include <scsi/sg.h>
37*cf84ac9aSAndroid Build Coastguard Worker
38*cf84ac9aSAndroid Build Coastguard Worker typedef struct sg_io_hdr struct_sg_io_hdr;
39*cf84ac9aSAndroid Build Coastguard Worker
40*cf84ac9aSAndroid Build Coastguard Worker #endif /* HAVE_SCSI_SG_H */
41*cf84ac9aSAndroid Build Coastguard Worker
42*cf84ac9aSAndroid Build Coastguard Worker #include MPERS_DEFS
43*cf84ac9aSAndroid Build Coastguard Worker
44*cf84ac9aSAndroid Build Coastguard Worker #include "xlat/sg_io_info.h"
45*cf84ac9aSAndroid Build Coastguard Worker
46*cf84ac9aSAndroid Build Coastguard Worker #ifdef HAVE_SCSI_SG_H
47*cf84ac9aSAndroid Build Coastguard Worker # include "print_fields.h"
48*cf84ac9aSAndroid Build Coastguard Worker # include "xlat/sg_io_dxfer_direction.h"
49*cf84ac9aSAndroid Build Coastguard Worker # include "xlat/sg_io_flags.h"
50*cf84ac9aSAndroid Build Coastguard Worker
51*cf84ac9aSAndroid Build Coastguard Worker static void
print_sg_io_buffer(struct tcb * const tcp,const kernel_ulong_t addr,const unsigned int data_size,const unsigned int iovec_count)52*cf84ac9aSAndroid Build Coastguard Worker print_sg_io_buffer(struct tcb *const tcp, const kernel_ulong_t addr,
53*cf84ac9aSAndroid Build Coastguard Worker const unsigned int data_size, const unsigned int iovec_count)
54*cf84ac9aSAndroid Build Coastguard Worker {
55*cf84ac9aSAndroid Build Coastguard Worker if (iovec_count) {
56*cf84ac9aSAndroid Build Coastguard Worker tprint_iov_upto(tcp, iovec_count, addr, IOV_DECODE_STR,
57*cf84ac9aSAndroid Build Coastguard Worker data_size);
58*cf84ac9aSAndroid Build Coastguard Worker } else {
59*cf84ac9aSAndroid Build Coastguard Worker printstr_ex(tcp, addr, data_size, QUOTE_FORCE_HEX);
60*cf84ac9aSAndroid Build Coastguard Worker }
61*cf84ac9aSAndroid Build Coastguard Worker }
62*cf84ac9aSAndroid Build Coastguard Worker
63*cf84ac9aSAndroid Build Coastguard Worker #define PRINT_FIELD_SG_IO_BUFFER(prefix_, where_, field_, size_, count_, tcp_) \
64*cf84ac9aSAndroid Build Coastguard Worker do { \
65*cf84ac9aSAndroid Build Coastguard Worker STRACE_PRINTF("%s%s=", (prefix_), #field_); \
66*cf84ac9aSAndroid Build Coastguard Worker print_sg_io_buffer((tcp_), (mpers_ptr_t)((where_).field_), \
67*cf84ac9aSAndroid Build Coastguard Worker (size_), (count_)); \
68*cf84ac9aSAndroid Build Coastguard Worker } while (0)
69*cf84ac9aSAndroid Build Coastguard Worker
70*cf84ac9aSAndroid Build Coastguard Worker static int
decode_request(struct tcb * const tcp,const kernel_ulong_t arg)71*cf84ac9aSAndroid Build Coastguard Worker decode_request(struct tcb *const tcp, const kernel_ulong_t arg)
72*cf84ac9aSAndroid Build Coastguard Worker {
73*cf84ac9aSAndroid Build Coastguard Worker struct_sg_io_hdr sg_io;
74*cf84ac9aSAndroid Build Coastguard Worker static const size_t skip_iid =
75*cf84ac9aSAndroid Build Coastguard Worker offsetof(struct_sg_io_hdr, dxfer_direction);
76*cf84ac9aSAndroid Build Coastguard Worker
77*cf84ac9aSAndroid Build Coastguard Worker tprints("{interface_id='S', ");
78*cf84ac9aSAndroid Build Coastguard Worker if (umoven_or_printaddr(tcp, arg + skip_iid, sizeof(sg_io) - skip_iid,
79*cf84ac9aSAndroid Build Coastguard Worker &sg_io.dxfer_direction)) {
80*cf84ac9aSAndroid Build Coastguard Worker tprints("}");
81*cf84ac9aSAndroid Build Coastguard Worker return RVAL_IOCTL_DECODED;
82*cf84ac9aSAndroid Build Coastguard Worker }
83*cf84ac9aSAndroid Build Coastguard Worker
84*cf84ac9aSAndroid Build Coastguard Worker PRINT_FIELD_XVAL("", sg_io, dxfer_direction, sg_io_dxfer_direction,
85*cf84ac9aSAndroid Build Coastguard Worker "SG_DXFER_???");
86*cf84ac9aSAndroid Build Coastguard Worker PRINT_FIELD_U(", ", sg_io, cmd_len);
87*cf84ac9aSAndroid Build Coastguard Worker PRINT_FIELD_SG_IO_BUFFER(", ", sg_io, cmdp, sg_io.cmd_len, 0, tcp);
88*cf84ac9aSAndroid Build Coastguard Worker PRINT_FIELD_U(", ", sg_io, mx_sb_len);
89*cf84ac9aSAndroid Build Coastguard Worker PRINT_FIELD_U(", ", sg_io, iovec_count);
90*cf84ac9aSAndroid Build Coastguard Worker PRINT_FIELD_U(", ", sg_io, dxfer_len);
91*cf84ac9aSAndroid Build Coastguard Worker PRINT_FIELD_U(", ", sg_io, timeout);
92*cf84ac9aSAndroid Build Coastguard Worker PRINT_FIELD_FLAGS(", ", sg_io, flags, sg_io_flags, "SG_FLAG_???");
93*cf84ac9aSAndroid Build Coastguard Worker
94*cf84ac9aSAndroid Build Coastguard Worker if (sg_io.dxfer_direction == SG_DXFER_TO_DEV ||
95*cf84ac9aSAndroid Build Coastguard Worker sg_io.dxfer_direction == SG_DXFER_TO_FROM_DEV) {
96*cf84ac9aSAndroid Build Coastguard Worker PRINT_FIELD_SG_IO_BUFFER(", ", sg_io, dxferp, sg_io.dxfer_len, sg_io.iovec_count, tcp);
97*cf84ac9aSAndroid Build Coastguard Worker }
98*cf84ac9aSAndroid Build Coastguard Worker
99*cf84ac9aSAndroid Build Coastguard Worker struct_sg_io_hdr *entering_sg_io = malloc(sizeof(*entering_sg_io));
100*cf84ac9aSAndroid Build Coastguard Worker if (entering_sg_io) {
101*cf84ac9aSAndroid Build Coastguard Worker memcpy(entering_sg_io, &sg_io, sizeof(sg_io));
102*cf84ac9aSAndroid Build Coastguard Worker entering_sg_io->interface_id = (unsigned char) 'S';
103*cf84ac9aSAndroid Build Coastguard Worker set_tcb_priv_data(tcp, entering_sg_io, free);
104*cf84ac9aSAndroid Build Coastguard Worker }
105*cf84ac9aSAndroid Build Coastguard Worker
106*cf84ac9aSAndroid Build Coastguard Worker return 0;
107*cf84ac9aSAndroid Build Coastguard Worker }
108*cf84ac9aSAndroid Build Coastguard Worker
109*cf84ac9aSAndroid Build Coastguard Worker static int
decode_response(struct tcb * const tcp,const kernel_ulong_t arg)110*cf84ac9aSAndroid Build Coastguard Worker decode_response(struct tcb *const tcp, const kernel_ulong_t arg)
111*cf84ac9aSAndroid Build Coastguard Worker {
112*cf84ac9aSAndroid Build Coastguard Worker struct_sg_io_hdr *entering_sg_io = get_tcb_priv_data(tcp);
113*cf84ac9aSAndroid Build Coastguard Worker struct_sg_io_hdr sg_io;
114*cf84ac9aSAndroid Build Coastguard Worker
115*cf84ac9aSAndroid Build Coastguard Worker if (umove(tcp, arg, &sg_io) < 0) {
116*cf84ac9aSAndroid Build Coastguard Worker /* print i/o fields fetched on entering syscall */
117*cf84ac9aSAndroid Build Coastguard Worker if (entering_sg_io->dxfer_direction == SG_DXFER_FROM_DEV)
118*cf84ac9aSAndroid Build Coastguard Worker PRINT_FIELD_PTR(", ", *entering_sg_io, dxferp);
119*cf84ac9aSAndroid Build Coastguard Worker PRINT_FIELD_PTR(", ", *entering_sg_io, sbp);
120*cf84ac9aSAndroid Build Coastguard Worker return RVAL_IOCTL_DECODED;
121*cf84ac9aSAndroid Build Coastguard Worker }
122*cf84ac9aSAndroid Build Coastguard Worker
123*cf84ac9aSAndroid Build Coastguard Worker if (sg_io.interface_id != entering_sg_io->interface_id) {
124*cf84ac9aSAndroid Build Coastguard Worker PRINT_FIELD_U(" => ", sg_io, interface_id);
125*cf84ac9aSAndroid Build Coastguard Worker return RVAL_IOCTL_DECODED;
126*cf84ac9aSAndroid Build Coastguard Worker }
127*cf84ac9aSAndroid Build Coastguard Worker
128*cf84ac9aSAndroid Build Coastguard Worker if (sg_io.dxfer_direction == SG_DXFER_FROM_DEV ||
129*cf84ac9aSAndroid Build Coastguard Worker sg_io.dxfer_direction == SG_DXFER_TO_FROM_DEV) {
130*cf84ac9aSAndroid Build Coastguard Worker uint32_t din_len = sg_io.dxfer_len;
131*cf84ac9aSAndroid Build Coastguard Worker const char *prefix = NULL;
132*cf84ac9aSAndroid Build Coastguard Worker
133*cf84ac9aSAndroid Build Coastguard Worker if (sg_io.resid > 0 && (unsigned int) sg_io.resid <= din_len)
134*cf84ac9aSAndroid Build Coastguard Worker din_len -= sg_io.resid;
135*cf84ac9aSAndroid Build Coastguard Worker
136*cf84ac9aSAndroid Build Coastguard Worker if (sg_io.dxfer_direction == SG_DXFER_FROM_DEV)
137*cf84ac9aSAndroid Build Coastguard Worker prefix = ", ";
138*cf84ac9aSAndroid Build Coastguard Worker else if (din_len)
139*cf84ac9aSAndroid Build Coastguard Worker prefix = " => ";
140*cf84ac9aSAndroid Build Coastguard Worker
141*cf84ac9aSAndroid Build Coastguard Worker if (prefix) {
142*cf84ac9aSAndroid Build Coastguard Worker tprints(prefix);
143*cf84ac9aSAndroid Build Coastguard Worker PRINT_FIELD_SG_IO_BUFFER("", sg_io, dxferp, din_len,
144*cf84ac9aSAndroid Build Coastguard Worker sg_io.iovec_count, tcp);
145*cf84ac9aSAndroid Build Coastguard Worker }
146*cf84ac9aSAndroid Build Coastguard Worker }
147*cf84ac9aSAndroid Build Coastguard Worker PRINT_FIELD_X(", ", sg_io, status);
148*cf84ac9aSAndroid Build Coastguard Worker PRINT_FIELD_X(", ", sg_io, masked_status);
149*cf84ac9aSAndroid Build Coastguard Worker PRINT_FIELD_X(", ", sg_io, msg_status);
150*cf84ac9aSAndroid Build Coastguard Worker PRINT_FIELD_U(", ", sg_io, sb_len_wr);
151*cf84ac9aSAndroid Build Coastguard Worker PRINT_FIELD_SG_IO_BUFFER(", ", sg_io, sbp, sg_io.sb_len_wr, 0, tcp);
152*cf84ac9aSAndroid Build Coastguard Worker PRINT_FIELD_X(", ", sg_io, host_status);
153*cf84ac9aSAndroid Build Coastguard Worker PRINT_FIELD_X(", ", sg_io, driver_status);
154*cf84ac9aSAndroid Build Coastguard Worker PRINT_FIELD_D(", ", sg_io, resid);
155*cf84ac9aSAndroid Build Coastguard Worker PRINT_FIELD_U(", ", sg_io, duration);
156*cf84ac9aSAndroid Build Coastguard Worker PRINT_FIELD_FLAGS(", ", sg_io, info, sg_io_info, "SG_INFO_???");
157*cf84ac9aSAndroid Build Coastguard Worker
158*cf84ac9aSAndroid Build Coastguard Worker return RVAL_IOCTL_DECODED;
159*cf84ac9aSAndroid Build Coastguard Worker }
160*cf84ac9aSAndroid Build Coastguard Worker
161*cf84ac9aSAndroid Build Coastguard Worker #else /* !HAVE_SCSI_SG_H */
162*cf84ac9aSAndroid Build Coastguard Worker
163*cf84ac9aSAndroid Build Coastguard Worker static int
decode_request(struct tcb * const tcp,const kernel_ulong_t arg)164*cf84ac9aSAndroid Build Coastguard Worker decode_request(struct tcb *const tcp, const kernel_ulong_t arg)
165*cf84ac9aSAndroid Build Coastguard Worker {
166*cf84ac9aSAndroid Build Coastguard Worker tprints("{interface_id='S', ...}");
167*cf84ac9aSAndroid Build Coastguard Worker return RVAL_IOCTL_DECODED;
168*cf84ac9aSAndroid Build Coastguard Worker }
169*cf84ac9aSAndroid Build Coastguard Worker
170*cf84ac9aSAndroid Build Coastguard Worker static int
decode_response(struct tcb * const tcp,const kernel_ulong_t arg)171*cf84ac9aSAndroid Build Coastguard Worker decode_response(struct tcb *const tcp, const kernel_ulong_t arg)
172*cf84ac9aSAndroid Build Coastguard Worker {
173*cf84ac9aSAndroid Build Coastguard Worker return 0;
174*cf84ac9aSAndroid Build Coastguard Worker }
175*cf84ac9aSAndroid Build Coastguard Worker
176*cf84ac9aSAndroid Build Coastguard Worker #endif
177*cf84ac9aSAndroid Build Coastguard Worker
MPERS_PRINTER_DECL(int,decode_sg_io_v3,struct tcb * const tcp,const kernel_ulong_t arg)178*cf84ac9aSAndroid Build Coastguard Worker MPERS_PRINTER_DECL(int, decode_sg_io_v3,
179*cf84ac9aSAndroid Build Coastguard Worker struct tcb *const tcp, const kernel_ulong_t arg)
180*cf84ac9aSAndroid Build Coastguard Worker {
181*cf84ac9aSAndroid Build Coastguard Worker return entering(tcp) ? decode_request(tcp, arg)
182*cf84ac9aSAndroid Build Coastguard Worker : decode_response(tcp, arg);
183*cf84ac9aSAndroid Build Coastguard Worker }
184