scsi_target.c revision 63186
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 63186 2000-07-14 20:30:28Z 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 exit(EX_SOFTWARE); 153 } 154 155 buf = malloc(bufsize); 156 157 if (buf == NULL) { 158 fprintf(stderr, "%s: Could not malloc I/O buffer", appname); 159 exit(EX_OSERR); 160 } 161 162 signal(SIGHUP, quit_handler); 163 signal(SIGINT, quit_handler); 164 signal(SIGTERM, quit_handler); 165 166 atexit(cleanup); 167 168 pump_events(); 169 170 return (0); 171} 172 173static void 174cleanup() 175{ 176 close(targfd); 177 178 if (ioctl(targctlfd, TARGCTLIOFREEUNIT, &alloc_unit) == -1) { 179 perror("TARGCTLIOFREEUNIT"); 180 exit(EX_SOFTWARE); 181 } 182 183 close(targctlfd); 184} 185 186static void 187pump_events() 188{ 189 struct pollfd targpoll; 190 191 targpoll.fd = targfd; 192 targpoll.events = POLLRDNORM|POLLWRNORM; 193 194 while (quit == 0) { 195 int retval; 196 197 retval = poll(&targpoll, 1, INFTIM); 198 199 if (retval == -1) { 200 if (errno == EINTR) 201 continue; 202 perror("Poll Failed"); 203 exit(EX_SOFTWARE); 204 } 205 206 if (retval == 0) { 207 perror("Poll returned 0 although timeout infinite???"); 208 exit(EX_SOFTWARE); 209 } 210 211 if (retval > 1) { 212 perror("Poll returned more fds ready than allocated"); 213 exit(EX_SOFTWARE); 214 } 215 216 /* Process events */ 217 if ((targpoll.revents & POLLERR) != 0) { 218 handle_exception(); 219 } 220 221 if ((targpoll.revents & POLLRDNORM) != 0) { 222 retval = read(targfd, buf, bufsize); 223 224 if (retval == -1) { 225 perror("Read from targ failed"); 226 /* Go look for exceptions */ 227 continue; 228 } else { 229 retval = write(ofd, buf, retval); 230 if (retval == -1) { 231 perror("Write to file failed"); 232 } 233 } 234 } 235 236 if ((targpoll.revents & POLLWRNORM) != 0) { 237 int amount_read; 238 239 retval = read(ifd, buf, bufsize); 240 if (retval == -1) { 241 perror("Read from file failed"); 242 exit(EX_SOFTWARE); 243 } 244 245 amount_read = retval; 246 retval = write(targfd, buf, retval); 247 if (retval == -1) { 248 perror("Write to targ failed"); 249 retval = 0; 250 } 251 252 /* Backup in our input stream on short writes */ 253 if (retval != amount_read) 254 lseek(ifd, retval - amount_read, SEEK_CUR); 255 } 256 } 257} 258 259static void 260handle_exception() 261{ 262 targ_exception exceptions; 263 264 if (ioctl(targfd, TARGIOCFETCHEXCEPTION, &exceptions) == -1) { 265 perror("TARGIOCFETCHEXCEPTION"); 266 exit(EX_SOFTWARE); 267 } 268 269 printf("Saw exceptions %x\n", exceptions); 270 if ((exceptions & TARG_EXCEPT_DEVICE_INVALID) != 0) { 271 /* Device went away. Nothing more to do. */ 272 printf("Device went away\n"); 273 exit(0); 274 } 275 276 if ((exceptions & TARG_EXCEPT_UNKNOWN_ATIO) != 0) { 277 struct ccb_accept_tio atio; 278 struct ioc_initiator_state ioc_istate; 279 struct scsi_sense_data *sense; 280 union ccb ccb; 281 282 if (ioctl(targfd, TARGIOCFETCHATIO, &atio) == -1) { 283 perror("TARGIOCFETCHATIO"); 284 exit(EX_SOFTWARE); 285 } 286 287 printf("Ignoring unhandled command 0x%x for Id %d\n", 288 atio.cdb_io.cdb_bytes[0], atio.init_id); 289 290 ioc_istate.initiator_id = atio.init_id; 291 if (ioctl(targfd, TARGIOCGETISTATE, &ioc_istate) == -1) { 292 perror("TARGIOCGETISTATE"); 293 exit(EX_SOFTWARE); 294 } 295 296 /* Send back Illegal Command code status */ 297 ioc_istate.istate.pending_ca |= CA_CMD_SENSE; 298 sense = &ioc_istate.istate.sense_data; 299 bzero(sense, sizeof(*sense)); 300 sense->error_code = SSD_CURRENT_ERROR; 301 sense->flags = SSD_KEY_ILLEGAL_REQUEST; 302 sense->add_sense_code = 0x20; 303 sense->add_sense_code_qual = 0x00; 304 sense->extra_len = offsetof(struct scsi_sense_data, fru) 305 - offsetof(struct scsi_sense_data, extra_len); 306 307 if (ioctl(targfd, TARGIOCSETISTATE, &ioc_istate) == -1) { 308 perror("TARGIOCSETISTATE"); 309 exit(EX_SOFTWARE); 310 } 311 312 /* Clear the exception so the kernel will take our response */ 313 if (ioctl(targfd, TARGIOCCLEAREXCEPTION, &exceptions) == -1) { 314 perror("TARGIOCCLEAREXCEPTION"); 315 exit(EX_SOFTWARE); 316 } 317 318 bzero(&ccb, sizeof(ccb)); 319 cam_fill_ctio(&ccb.csio, /*retries*/2, 320 /*cbfcnp*/NULL, 321 /*flags*/CAM_DIR_NONE 322 | (atio.ccb_h.flags & CAM_TAG_ACTION_VALID) 323 | CAM_SEND_STATUS, 324 /*tag_action*/MSG_SIMPLE_Q_TAG, 325 atio.tag_id, 326 atio.init_id, 327 SCSI_STATUS_CHECK_COND, 328 /*data_ptr*/NULL, 329 /*dxfer_len*/0, 330 /*timeout*/5 * 1000); 331 332 if (ioctl(targfd, TARGIOCCOMMAND, &ccb) == -1) { 333 perror("TARGIOCCOMMAND"); 334 exit(EX_SOFTWARE); 335 } 336 337 } else { 338 if (ioctl(targfd, TARGIOCCLEAREXCEPTION, &exceptions) == -1) { 339 perror("TARGIOCCLEAREXCEPTION"); 340 exit(EX_SOFTWARE); 341 } 342 } 343 344} 345 346static void 347quit_handler(int signum) 348{ 349 quit = 1; 350} 351 352static void 353usage() 354{ 355 356 (void)fprintf(stderr, 357"usage: %-16s [ -d ] [-o output_file] [-i input_file] -p path -t target -l lun\n", 358 appname); 359 360 exit(EX_USAGE); 361} 362 363