blob: a2e23c8b1cb10da3cbff19e7e686ccb310b339cd [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
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +000039print_sg_io_buffer(struct tcb *tcp, unsigned char *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
44 if (len == 0)
45 return;
46 allocated = (len > max_strlen) ? max_strlen : len;
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +000047 if ((buf = malloc(allocated)) == NULL ||
Dmitry V. Levinb011af52007-06-30 11:37:09 +000048 umoven(tcp, (unsigned long) addr, allocated, (char *) buf) < 0) {
49 tprintf("%p", addr);
50 free(buf);
51 return;
52 }
53 tprintf("%02x", buf[0]);
54 for (i = 1; i < allocated; ++i)
55 tprintf(", %02x", buf[i]);
56 free(buf);
57 if (allocated != len)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +020058 tprints(", ...");
Dmitry V. Levinb011af52007-06-30 11:37:09 +000059}
60
61static void
62print_sg_io_req(struct tcb *tcp, struct sg_io_hdr *sg_io)
63{
64 tprintf("{'%c', ", sg_io->interface_id);
65 printxval(sg_io_dxfer_direction, sg_io->dxfer_direction,
66 "SG_DXFER_???");
67 tprintf(", cmd[%u]=[", sg_io->cmd_len);
68 print_sg_io_buffer(tcp, sg_io->cmdp, sg_io->cmd_len);
69 tprintf("], mx_sb_len=%d, ", sg_io->mx_sb_len);
70 tprintf("iovec_count=%d, ", sg_io->iovec_count);
71 tprintf("dxfer_len=%u, ", sg_io->dxfer_len);
72 tprintf("timeout=%u, ", sg_io->timeout);
73 tprintf("flags=%#x", sg_io->flags);
74
75 if (sg_io->dxfer_direction == SG_DXFER_TO_DEV ||
76 sg_io->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
77 tprintf(", data[%u]=[", sg_io->dxfer_len);
78 printstr(tcp, (unsigned long) sg_io->dxferp,
79 sg_io->dxfer_len);
Denys Vlasenko60fe8c12011-09-01 10:00:28 +020080 tprints("]");
Dmitry V. Levinb011af52007-06-30 11:37:09 +000081 }
82}
83
84static void
85print_sg_io_res(struct tcb *tcp, struct sg_io_hdr *sg_io)
86{
87 if (sg_io->dxfer_direction == SG_DXFER_FROM_DEV ||
88 sg_io->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
89 tprintf(", data[%u]=[", sg_io->dxfer_len);
90 printstr(tcp, (unsigned long) sg_io->dxferp,
91 sg_io->dxfer_len);
Denys Vlasenko60fe8c12011-09-01 10:00:28 +020092 tprints("]");
Dmitry V. Levinb011af52007-06-30 11:37:09 +000093 }
94 tprintf(", status=%02x, ", sg_io->status);
95 tprintf("masked_status=%02x, ", sg_io->masked_status);
96 tprintf("sb[%u]=[", sg_io->sb_len_wr);
97 print_sg_io_buffer(tcp, sg_io->sbp, sg_io->sb_len_wr);
98 tprintf("], host_status=%#x, ", sg_io->host_status);
99 tprintf("driver_status=%#x, ", sg_io->driver_status);
100 tprintf("resid=%d, ", sg_io->resid);
101 tprintf("duration=%d, ", sg_io->duration);
102 tprintf("info=%#x}", sg_io->info);
103}
104
105int
106scsi_ioctl(struct tcb *tcp, long code, long arg)
107{
108 switch (code) {
109 case SG_IO:
110 if (entering(tcp)) {
111 struct sg_io_hdr sg_io;
112
113 if (umove(tcp, arg, &sg_io) < 0)
114 tprintf(", %#lx", arg);
115 else {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200116 tprints(", ");
Dmitry V. Levinb011af52007-06-30 11:37:09 +0000117 print_sg_io_req(tcp, &sg_io);
118 }
119 }
120 if (exiting(tcp)) {
121 struct sg_io_hdr sg_io;
122
123 if (!syserror(tcp) && umove(tcp, arg, &sg_io) >= 0)
124 print_sg_io_res(tcp, &sg_io);
125 else
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200126 tprints("}");
Dmitry V. Levinb011af52007-06-30 11:37:09 +0000127 }
128 break;
129 default:
130 if (entering(tcp))
131 tprintf(", %#lx", arg);
132 break;
133 }
134 return 1;
135}
Dmitry V. Levinfdb896e2014-02-25 23:04:55 +0000136
137#endif /* HAVE_SCSI_SG_H */