scsi_target.c revision 63290
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 * $FreeBSD: head/share/examples/scsi_target/scsi_target.c 63290 2000-07-17 02:05:45Z mjacob $ 30 */ 31 32#include <sys/types.h> 33#include <errno.h> 34#include <fcntl.h> 35#include <poll.h> 36#include <signal.h> 37#include <stddef.h> 38#include <stdio.h> 39#include <stdlib.h> 40#include <sysexits.h> 41#include <unistd.h> 42 43#include <cam/scsi/scsi_all.h> 44#include <cam/scsi/scsi_message.h> 45#include <cam/scsi/scsi_targetio.h> 46 47char *appname; 48int ifd; 49char *ifilename; 50int ofd; 51char *ofilename; 52size_t bufsize = 64 * 1024; 53void *buf; 54char targdevname[80]; 55int targctlfd; 56int targfd; 57int quit; 58int debug = 0; 59struct ioc_alloc_unit alloc_unit = { 60 CAM_BUS_WILDCARD, 61 CAM_TARGET_WILDCARD, 62 CAM_LUN_WILDCARD 63}; 64 65static void pump_events(); 66static void cleanup(); 67static void handle_exception(); 68static void quit_handler(); 69static void usage(); 70 71int 72main(int argc, char *argv[]) 73{ 74 int ch; 75 76 appname = *argv; 77 while ((ch = getopt(argc, argv, "i:o:p:t:l:d")) != -1) { 78 switch(ch) { 79 case 'i': 80 if ((ifd = open(optarg, O_RDONLY)) == -1) { 81 perror(optarg); 82 exit(EX_NOINPUT); 83 } 84 ifilename = optarg; 85 break; 86 case 'o': 87 if ((ofd = open(optarg, 88 O_WRONLY|O_CREAT), 0600) == -1) { 89 perror(optarg); 90 exit(EX_CANTCREAT); 91 } 92 ofilename = optarg; 93 break; 94 case 'p': 95 alloc_unit.path_id = atoi(optarg); 96 break; 97 case 't': 98 alloc_unit.target_id = atoi(optarg); 99 break; 100 case 'l': 101 alloc_unit.lun_id = atoi(optarg); 102 break; 103 case 'd': 104 debug++; 105 break; 106 case '?': 107 default: 108 usage(); 109 /* NOTREACHED */ 110 } 111 } 112 argc -= optind; 113 argv += optind; 114 115 if (alloc_unit.path_id == CAM_BUS_WILDCARD 116 || alloc_unit.target_id == CAM_TARGET_WILDCARD 117 || alloc_unit.lun_id == CAM_LUN_WILDCARD) { 118 fprintf(stderr, "%s: Incomplete device path specifiled\n", 119 appname); 120 usage(); 121 /* NOTREACHED */ 122 } 123 124 if (argc != 0) { 125 fprintf(stderr, "%s: Too many arguments\n", appname); 126 usage(); 127 /* NOTREACHED */ 128 } 129 130 /* Allocate a new instance */ 131 if ((targctlfd = open("/dev/targ.ctl", O_RDWR)) == -1) { 132 perror("/dev/targ.ctl"); 133 exit(EX_UNAVAILABLE); 134 } 135 136 if (ioctl(targctlfd, TARGCTLIOALLOCUNIT, &alloc_unit) == -1) { 137 perror("TARGCTLIOALLOCUNIT"); 138 exit(EX_SOFTWARE); 139 } 140 141 snprintf(targdevname, sizeof(targdevname), "/dev/targ%d", 142 alloc_unit.unit); 143 144 if ((targfd = open(targdevname, O_RDWR)) == -1) { 145 perror(targdevname); 146 ioctl(targctlfd, TARGCTLIOFREEUNIT, &alloc_unit); 147 exit(EX_NOINPUT); 148 } 149 150 if (ioctl(targfd, TARGIODEBUG, &debug) == -1) { 151 perror("TARGIODEBUG"); 152 (void) ioctl(targctlfd, TARGCTLIOFREEUNIT, &alloc_unit); 153 exit(EX_SOFTWARE); 154 } 155 156 buf = malloc(bufsize); 157 158 if (buf == NULL) { 159 fprintf(stderr, "%s: Could not malloc I/O buffer", appname); 160 if (debug) { 161 debug = 0; 162 (void) ioctl(targfd, TARGIODEBUG, &debug); 163 } 164 (void) ioctl(targctlfd, TARGCTLIOFREEUNIT, &alloc_unit); 165 exit(EX_OSERR); 166 } 167 168 signal(SIGHUP, quit_handler); 169 signal(SIGINT, quit_handler); 170 signal(SIGTERM, quit_handler); 171 172 atexit(cleanup); 173 174 pump_events(); 175 176 return (0); 177} 178 179static void 180cleanup() 181{ 182 if (debug) { 183 debug = 0; 184 (void) ioctl(targfd, TARGIODEBUG, &debug); 185 } 186 close(targfd); 187 188 if (ioctl(targctlfd, TARGCTLIOFREEUNIT, &alloc_unit) == -1) { 189 perror("TARGCTLIOFREEUNIT"); 190 exit(EX_SOFTWARE); 191 } 192 193 close(targctlfd); 194} 195 196static void 197pump_events() 198{ 199 struct pollfd targpoll; 200 201 targpoll.fd = targfd; 202 targpoll.events = POLLRDNORM|POLLWRNORM; 203 204 while (quit == 0) { 205 int retval; 206 207 retval = poll(&targpoll, 1, INFTIM); 208 209 if (retval == -1) { 210 if (errno == EINTR) 211 continue; 212 perror("Poll Failed"); 213 exit(EX_SOFTWARE); 214 } 215 216 if (retval == 0) { 217 perror("Poll returned 0 although timeout infinite???"); 218 exit(EX_SOFTWARE); 219 } 220 221 if (retval > 1) { 222 perror("Poll returned more fds ready than allocated"); 223 exit(EX_SOFTWARE); 224 } 225 226 /* Process events */ 227 if ((targpoll.revents & POLLERR) != 0) { 228 handle_exception(); 229 } 230 231 if ((targpoll.revents & POLLRDNORM) != 0) { 232 retval = read(targfd, buf, bufsize); 233 234 if (retval == -1) { 235 perror("Read from targ failed"); 236 /* Go look for exceptions */ 237 continue; 238 } else { 239 retval = write(ofd, buf, retval); 240 if (retval == -1) { 241 perror("Write to file failed"); 242 } 243 } 244 } 245 246 if ((targpoll.revents & POLLWRNORM) != 0) { 247 int amount_read; 248 249 retval = read(ifd, buf, bufsize); 250 if (retval == -1) { 251 perror("Read from file failed"); 252 exit(EX_SOFTWARE); 253 } 254 255 amount_read = retval; 256 retval = write(targfd, buf, retval); 257 if (retval == -1) { 258 perror("Write to targ failed"); 259 retval = 0; 260 } 261 262 /* Backup in our input stream on short writes */ 263 if (retval != amount_read) 264 lseek(ifd, retval - amount_read, SEEK_CUR); 265 } 266 } 267} 268 269static void 270handle_exception() 271{ 272 targ_exception exceptions; 273 274 if (ioctl(targfd, TARGIOCFETCHEXCEPTION, &exceptions) == -1) { 275 perror("TARGIOCFETCHEXCEPTION"); 276 exit(EX_SOFTWARE); 277 } 278 279 printf("Saw exceptions %x\n", exceptions); 280 if ((exceptions & TARG_EXCEPT_DEVICE_INVALID) != 0) { 281 /* Device went away. Nothing more to do. */ 282 printf("Device went away\n"); 283 exit(0); 284 } 285 286 if ((exceptions & TARG_EXCEPT_UNKNOWN_ATIO) != 0) { 287 struct ccb_accept_tio atio; 288 struct ioc_initiator_state ioc_istate; 289 struct scsi_sense_data *sense; 290 union ccb ccb; 291 292 if (ioctl(targfd, TARGIOCFETCHATIO, &atio) == -1) { 293 perror("TARGIOCFETCHATIO"); 294 exit(EX_SOFTWARE); 295 } 296 297 printf("Ignoring unhandled command 0x%x for Id %d\n", 298 atio.cdb_io.cdb_bytes[0], atio.init_id); 299 300 ioc_istate.initiator_id = atio.init_id; 301 if (ioctl(targfd, TARGIOCGETISTATE, &ioc_istate) == -1) { 302 perror("TARGIOCGETISTATE"); 303 exit(EX_SOFTWARE); 304 } 305 306 /* Send back Illegal Command code status */ 307 ioc_istate.istate.pending_ca |= CA_CMD_SENSE; 308 sense = &ioc_istate.istate.sense_data; 309 bzero(sense, sizeof(*sense)); 310 sense->error_code = SSD_CURRENT_ERROR; 311 sense->flags = SSD_KEY_ILLEGAL_REQUEST; 312 sense->add_sense_code = 0x20; 313 sense->add_sense_code_qual = 0x00; 314 sense->extra_len = offsetof(struct scsi_sense_data, fru) 315 - offsetof(struct scsi_sense_data, extra_len); 316 317 if (ioctl(targfd, TARGIOCSETISTATE, &ioc_istate) == -1) { 318 perror("TARGIOCSETISTATE"); 319 exit(EX_SOFTWARE); 320 } 321 322 /* Clear the exception so the kernel will take our response */ 323 if (ioctl(targfd, TARGIOCCLEAREXCEPTION, &exceptions) == -1) { 324 perror("TARGIOCCLEAREXCEPTION"); 325 exit(EX_SOFTWARE); 326 } 327 328 bzero(&ccb, sizeof(ccb)); 329 cam_fill_ctio(&ccb.csio, /*retries*/2, 330 /*cbfcnp*/NULL, 331 /*flags*/CAM_DIR_NONE 332 | (atio.ccb_h.flags & CAM_TAG_ACTION_VALID) 333 | CAM_SEND_STATUS, 334 /*tag_action*/MSG_SIMPLE_Q_TAG, 335 atio.tag_id, 336 atio.init_id, 337 SCSI_STATUS_CHECK_COND, 338 /*data_ptr*/NULL, 339 /*dxfer_len*/0, 340 /*timeout*/5 * 1000); 341 342 if (ioctl(targfd, TARGIOCCOMMAND, &ccb) == -1) { 343 perror("TARGIOCCOMMAND"); 344 exit(EX_SOFTWARE); 345 } 346 347 } else { 348 if (ioctl(targfd, TARGIOCCLEAREXCEPTION, &exceptions) == -1) { 349 perror("TARGIOCCLEAREXCEPTION"); 350 exit(EX_SOFTWARE); 351 } 352 } 353 354} 355 356static void 357quit_handler(int signum) 358{ 359 quit = 1; 360} 361 362static void 363usage() 364{ 365 366 (void)fprintf(stderr, 367"usage: %-16s [ -d ] [-o output_file] [-i input_file] -p path -t target -l lun\n", 368 appname); 369 370 exit(EX_USAGE); 371} 372 373