scsi_target.c revision 39215
1/* 2 * Sample program to attach to the "targ" processor target, target mode 3 * peripheral driver and push or receive data. 4 * 5 * Copyright (c) 1998 Justin T. Gibbs. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions, and the following disclaimer, 13 * without modification, immediately at the beginning of the file. 14 * 2. 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 AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $Id$ 30 */ 31 32#include <sys/types.h> 33 34#include <fcntl.h> 35#include <poll.h> 36#include <stddef.h> 37#include <stdio.h> 38#include <stdlib.h> 39#include <sysexits.h> 40#include <unistd.h> 41 42#include <cam/scsi/scsi_all.h> 43#include <cam/scsi/scsi_message.h> 44#include <cam/scsi/scsi_targetio.h> 45 46char *appname; 47int ifd; 48char *ifilename; 49int ofd; 50char *ofilename; 51size_t bufsize = 64 * 1024; 52void *buf; 53char *targdevname; 54int targfd; 55 56static void pump_events(); 57static void handle_exception(); 58static void usage(); 59 60int 61main(int argc, char *argv[]) 62{ 63 int ch; 64 65 appname = *argv; 66 while ((ch = getopt(argc, argv, "i:o:")) != -1) { 67 switch(ch) { 68 case 'i': 69 if ((ifd = open(optarg, O_RDONLY)) == -1) { 70 perror(optarg); 71 exit(EX_NOINPUT); 72 } 73 ifilename = optarg; 74 break; 75 case 'o': 76 if ((ofd = open(optarg, 77 O_WRONLY|O_CREAT), 0600) == -1) { 78 perror(optarg); 79 exit(EX_CANTCREAT); 80 } 81 ofilename = optarg; 82 break; 83 case '?': 84 default: 85 usage(); 86 /* NOTREACHED */ 87 } 88 } 89 argc -= optind; 90 argv += optind; 91 92 if (argc != 1) { 93 fprintf(stderr, "%s: No target device specifiled\n", appname); 94 usage(); 95 /* NOTREACHED */ 96 } 97 98 targdevname = *argv; 99 if ((targfd = open(targdevname, O_RDWR)) == -1) { 100 perror(targdevname); 101 exit(EX_NOINPUT); 102 } 103 104 buf = malloc(bufsize); 105 106 if (buf == NULL) { 107 fprintf(stderr, "%s: Could not malloc I/O buffer", appname); 108 exit(EX_OSERR); 109 } 110 111 pump_events(); 112 113 return (0); 114} 115 116static void 117pump_events() 118{ 119 struct pollfd targpoll; 120 121 targpoll.fd = targfd; 122 targpoll.events = POLLRDNORM|POLLWRNORM; 123 124 while (1) { 125 int retval; 126 127 retval = poll(&targpoll, 1, INFTIM); 128 129 if (retval == -1) { 130 perror("Poll Failed"); 131 exit(EX_SOFTWARE); 132 } 133 134 if (retval == 0) { 135 perror("Poll returned 0 although timeout infinite???"); 136 exit(EX_SOFTWARE); 137 } 138 139 if (retval > 1) { 140 perror("Poll returned more fds ready than allocated"); 141 exit(EX_SOFTWARE); 142 } 143 144 /* Process events */ 145 if ((targpoll.revents & POLLERR) != 0) { 146 handle_exception(); 147 } 148 149 if ((targpoll.revents & POLLRDNORM) != 0) { 150 printf("Read Poll event came true\n"); 151 retval = read(targfd, buf, bufsize); 152 153 if (retval == -1) { 154 perror("Read from targ failed"); 155 } else { 156 retval = write(ofd, buf, retval); 157 if (retval == -1) { 158 perror("Write to file failed"); 159 } 160 } 161 } 162 163 if ((targpoll.revents & POLLWRNORM) != 0) { 164 int amount_read; 165 166 printf("Write Poll event came true\n"); 167 retval = read(ifd, buf, bufsize); 168 if (retval == -1) { 169 perror("Read from file failed"); 170 exit(EX_SOFTWARE); 171 } 172 173 amount_read = retval; 174 retval = write(targfd, buf, retval); 175 if (retval == -1) { 176 perror("Write to targ failed"); 177 retval = 0; 178 } 179 180 /* Backup in our input stream on short writes */ 181 if (retval != amount_read) 182 lseek(ifd, retval - amount_read, SEEK_CUR); 183 } 184 } 185} 186 187static void 188handle_exception() 189{ 190 targ_exception exceptions; 191 192 if (ioctl(targfd, TARGIOCFETCHEXCEPTION, &exceptions) == -1) { 193 perror("TARGIOCFETCHEXCEPTION"); 194 exit(EX_SOFTWARE); 195 } 196 197 if ((exceptions & TARG_EXCEPT_DEVICE_INVALID) != 0) { 198 /* Device went away. Nothing more to do. */ 199 exit(0); 200 } 201 202 if ((exceptions & TARG_EXCEPT_UNKNOWN_ATIO) != 0) { 203 struct ccb_accept_tio atio; 204 struct ioc_initiator_state ioc_istate; 205 struct scsi_sense_data *sense; 206 union ccb ccb; 207 208 if (ioctl(targfd, TARGIOCFETCHATIO, &atio) == -1) { 209 perror("TARGIOCFETCHATIO"); 210 exit(EX_SOFTWARE); 211 } 212 213 printf("Ignoring unhandled command 0x%x for Id %d\n", 214 atio.cdb_io.cdb_bytes[0], atio.init_id); 215 216 ioc_istate.initiator_id = atio.init_id; 217 if (ioctl(targfd, TARGIOCGETISTATE, &ioc_istate) == -1) { 218 perror("TARGIOCGETISTATE"); 219 exit(EX_SOFTWARE); 220 } 221 222 /* Send back Illegal Command code status */ 223 ioc_istate.istate.pending_ca |= CA_CMD_SENSE; 224 sense = &ioc_istate.istate.sense_data; 225 bzero(sense, sizeof(*sense)); 226 sense->error_code = SSD_CURRENT_ERROR; 227 sense->flags = SSD_KEY_ILLEGAL_REQUEST; 228 sense->add_sense_code = 0x20; 229 sense->add_sense_code_qual = 0x00; 230 sense->extra_len = offsetof(struct scsi_sense_data, fru) 231 - offsetof(struct scsi_sense_data, extra_len); 232 233 if (ioctl(targfd, TARGIOCSETISTATE, &ioc_istate) == -1) { 234 perror("TARGIOCSETISTATE"); 235 exit(EX_SOFTWARE); 236 } 237 238 bzero(&ccb, sizeof(ccb)); 239 cam_fill_ctio(&ccb.csio, /*retries*/2, 240 /*cbfcnp*/NULL, 241 /*flags*/CAM_DIR_NONE 242 | (atio.ccb_h.flags & CAM_TAG_ACTION_VALID) 243 | CAM_SEND_STATUS, 244 /*tag_action*/MSG_SIMPLE_Q_TAG, 245 atio.tag_id, 246 atio.init_id, 247 SCSI_STATUS_CHECK_COND, 248 /*data_ptr*/NULL, 249 /*dxfer_len*/0, 250 /*timeout*/5 * 1000); 251 if (ioctl(targfd, TARGIOCCOMMAND, &ccb) == -1) { 252 perror("TARGIOCCOMMAND"); 253 exit(EX_SOFTWARE); 254 } 255 256 } 257 258 if (ioctl(targfd, TARGIOCCLEAREXCEPTION, &exceptions) == -1) { 259 perror("TARGIOCCLEAREXCEPTION"); 260 exit(EX_SOFTWARE); 261 } 262} 263 264static void 265usage() 266{ 267 268 (void)fprintf(stderr, 269"usage: %-16s [-o output_file] [-i input_file] /dev/targ?\n", appname); 270 271 exit(EX_USAGE); 272} 273 274