Dmitry V. Levin | 27aeaa2 | 2012-03-16 10:43:32 +0000 | [diff] [blame] | 1 | /* |
| 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. Levin | b011af5 | 2007-06-30 11:37:09 +0000 | [diff] [blame] | 29 | #include "defs.h" |
Dmitry V. Levin | b011af5 | 2007-06-30 11:37:09 +0000 | [diff] [blame] | 30 | #include <sys/ioctl.h> |
| 31 | #include <scsi/sg.h> |
| 32 | |
| 33 | static const struct xlat sg_io_dxfer_direction[] = { |
| 34 | {SG_DXFER_NONE, "SG_DXFER_NONE"}, |
| 35 | {SG_DXFER_TO_DEV, "SG_DXFER_TO_DEV"}, |
| 36 | {SG_DXFER_FROM_DEV, "SG_DXFER_FROM_DEV"}, |
| 37 | {SG_DXFER_TO_FROM_DEV, "SG_DXFER_TO_FROM_DEV"}, |
| 38 | {0, NULL} |
| 39 | }; |
| 40 | |
| 41 | static void |
| 42 | print_sg_io_buffer(struct tcb *tcp, unsigned char *addr, int len) |
| 43 | { |
| 44 | unsigned char *buf = NULL; |
| 45 | int allocated, i; |
| 46 | |
| 47 | if (len == 0) |
| 48 | return; |
| 49 | allocated = (len > max_strlen) ? max_strlen : len; |
| 50 | if (len < 0 || |
| 51 | (buf = malloc(allocated)) == NULL || |
| 52 | umoven(tcp, (unsigned long) addr, allocated, (char *) buf) < 0) { |
| 53 | tprintf("%p", addr); |
| 54 | free(buf); |
| 55 | return; |
| 56 | } |
| 57 | tprintf("%02x", buf[0]); |
| 58 | for (i = 1; i < allocated; ++i) |
| 59 | tprintf(", %02x", buf[i]); |
| 60 | free(buf); |
| 61 | if (allocated != len) |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 62 | tprints(", ..."); |
Dmitry V. Levin | b011af5 | 2007-06-30 11:37:09 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static void |
| 66 | print_sg_io_req(struct tcb *tcp, struct sg_io_hdr *sg_io) |
| 67 | { |
| 68 | tprintf("{'%c', ", sg_io->interface_id); |
| 69 | printxval(sg_io_dxfer_direction, sg_io->dxfer_direction, |
| 70 | "SG_DXFER_???"); |
| 71 | tprintf(", cmd[%u]=[", sg_io->cmd_len); |
| 72 | print_sg_io_buffer(tcp, sg_io->cmdp, sg_io->cmd_len); |
| 73 | tprintf("], mx_sb_len=%d, ", sg_io->mx_sb_len); |
| 74 | tprintf("iovec_count=%d, ", sg_io->iovec_count); |
| 75 | tprintf("dxfer_len=%u, ", sg_io->dxfer_len); |
| 76 | tprintf("timeout=%u, ", sg_io->timeout); |
| 77 | tprintf("flags=%#x", sg_io->flags); |
| 78 | |
| 79 | if (sg_io->dxfer_direction == SG_DXFER_TO_DEV || |
| 80 | sg_io->dxfer_direction == SG_DXFER_TO_FROM_DEV) { |
| 81 | tprintf(", data[%u]=[", sg_io->dxfer_len); |
| 82 | printstr(tcp, (unsigned long) sg_io->dxferp, |
| 83 | sg_io->dxfer_len); |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 84 | tprints("]"); |
Dmitry V. Levin | b011af5 | 2007-06-30 11:37:09 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
| 88 | static void |
| 89 | print_sg_io_res(struct tcb *tcp, struct sg_io_hdr *sg_io) |
| 90 | { |
| 91 | if (sg_io->dxfer_direction == SG_DXFER_FROM_DEV || |
| 92 | sg_io->dxfer_direction == SG_DXFER_TO_FROM_DEV) { |
| 93 | tprintf(", data[%u]=[", sg_io->dxfer_len); |
| 94 | printstr(tcp, (unsigned long) sg_io->dxferp, |
| 95 | sg_io->dxfer_len); |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 96 | tprints("]"); |
Dmitry V. Levin | b011af5 | 2007-06-30 11:37:09 +0000 | [diff] [blame] | 97 | } |
| 98 | tprintf(", status=%02x, ", sg_io->status); |
| 99 | tprintf("masked_status=%02x, ", sg_io->masked_status); |
| 100 | tprintf("sb[%u]=[", sg_io->sb_len_wr); |
| 101 | print_sg_io_buffer(tcp, sg_io->sbp, sg_io->sb_len_wr); |
| 102 | tprintf("], host_status=%#x, ", sg_io->host_status); |
| 103 | tprintf("driver_status=%#x, ", sg_io->driver_status); |
| 104 | tprintf("resid=%d, ", sg_io->resid); |
| 105 | tprintf("duration=%d, ", sg_io->duration); |
| 106 | tprintf("info=%#x}", sg_io->info); |
| 107 | } |
| 108 | |
| 109 | int |
| 110 | scsi_ioctl(struct tcb *tcp, long code, long arg) |
| 111 | { |
| 112 | switch (code) { |
| 113 | case SG_IO: |
| 114 | if (entering(tcp)) { |
| 115 | struct sg_io_hdr sg_io; |
| 116 | |
| 117 | if (umove(tcp, arg, &sg_io) < 0) |
| 118 | tprintf(", %#lx", arg); |
| 119 | else { |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 120 | tprints(", "); |
Dmitry V. Levin | b011af5 | 2007-06-30 11:37:09 +0000 | [diff] [blame] | 121 | print_sg_io_req(tcp, &sg_io); |
| 122 | } |
| 123 | } |
| 124 | if (exiting(tcp)) { |
| 125 | struct sg_io_hdr sg_io; |
| 126 | |
| 127 | if (!syserror(tcp) && umove(tcp, arg, &sg_io) >= 0) |
| 128 | print_sg_io_res(tcp, &sg_io); |
| 129 | else |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 130 | tprints("}"); |
Dmitry V. Levin | b011af5 | 2007-06-30 11:37:09 +0000 | [diff] [blame] | 131 | } |
| 132 | break; |
| 133 | default: |
| 134 | if (entering(tcp)) |
| 135 | tprintf(", %#lx", arg); |
| 136 | break; |
| 137 | } |
| 138 | return 1; |
| 139 | } |