1 /* 2 * scsi.h Copyright (C) 1992 Drew Eckhardt 3 * Copyright (C) 1993, 1994, 1995 Eric Youngdale 4 * generic SCSI package header file by 5 * Initial versions: Drew Eckhardt 6 * Subsequent revisions: Eric Youngdale 7 * 8 * <drew@colorado.edu> 9 * 10 * Modified by Eric Youngdale eric@aib.com to 11 * add scatter-gather, multiple outstanding request, and other 12 * enhancements. 13 */ 14
15 #ifndef_SCSI_H 16 #define_SCSI_H 17
18 /* 19 * Some of the public constants are being movedd to this file. 20 * We include it here so that what came from where is transparent. 21 */ 22 #include <linux/scsi.h>
23
24 /* 25 * Some defs, in case these are not defined elsewhere. 26 */ 27 #ifndefTRUE 28 # defineTRUE 1
29 #endif 30 #ifndefFALSE 31 # defineFALSE 0
32 #endif 33
34
35 externvoidscsi_make_blocked_list(void);
36 externvolatileintin_scan_scsis;
37 externconstunsignedcharscsi_command_size[8];
38 #defineCOMMAND_SIZE(opcode) scsi_command_size[((opcode) >> 5) & 7]
39
40 #defineIDENTIFY_BASE 0x80
41 #defineIDENTIFY(can_disconnect, lun) (IDENTIFY_BASE |\
42 ((can_disconnect) ? 0x40 : 0) |\
43 ((lun) & 0x07))
44
45
46
47 /* 48 * the return of the status word will be in the following format : 49 * The low byte is the status returned by the SCSI command, 50 * with vendor specific bits masked. 51 * 52 * The next byte is the message which followed the SCSI status. 53 * This allows a stos to be used, since the Intel is a little 54 * endian machine. 55 * 56 * The final byte is a host return code, which is one of the following. 57 * 58 * IE 59 * lsb msb 60 * status msg host code 61 * 62 * Our errors returned by OUR driver, NOT SCSI message. Or'd with 63 * SCSI message passed back to driver <IF any>. 64 */ 65
66
67 #defineDID_OK 0x00 /* NO error */ 68 #defineDID_NO_CONNECT 0x01 /* Couldn't connect before timeout period */ 69 #defineDID_BUS_BUSY 0x02 /* BUS stayed busy through time out period */ 70 #defineDID_TIME_OUT 0x03 /* TIMED OUT for other reason */ 71 #defineDID_BAD_TARGET 0x04 /* BAD target. */ 72 #defineDID_ABORT 0x05 /* Told to abort for some other reason */ 73 #defineDID_PARITY 0x06 /* Parity error */ 74 #defineDID_ERROR 0x07 /* Internal error */ 75 #defineDID_RESET 0x08 /* Reset by somebody. */ 76 #defineDID_BAD_INTR 0x09 /* Got an interrupt we weren't expecting. */ 77 #defineDRIVER_OK 0x00 /* Driver status */ 78
79 /* 80 * These indicate the error that occurred, and what is available. 81 */ 82
83 #define DRIVER_BUSY 0x01
84 #defineDRIVER_SOFT 0x02
85 #define DRIVER_MEDIA 0x03
86 #defineDRIVER_ERROR 0x04
87
88 #defineDRIVER_INVALID 0x05
89 #defineDRIVER_TIMEOUT 0x06
90 #defineDRIVER_HARD 0x07
91
92 #defineSUGGEST_RETRY 0x10
93 #defineSUGGEST_ABORT 0x20
94 #defineSUGGEST_REMAP 0x30
95 #defineSUGGEST_DIE 0x40
96 #defineSUGGEST_SENSE 0x80
97 #defineSUGGEST_IS_OK 0xff
98
99 #defineDRIVER_SENSE 0x08
100
101 #define DRIVER_MASK 0x0f
102 #defineSUGGEST_MASK 0xf0
103
104 #defineMAX_COMMAND_SIZE 12
105
106 /* 107 * SCSI command sets 108 */ 109
110 #define SCSI_UNKNOWN 0
111 #define SCSI_1 1
112 #define SCSI_1_CCS 2
113 #defineSCSI_2 3
114
115 /* 116 * Every SCSI command starts with a one byte OP-code. 117 * The next byte's high three bits are the LUN of the 118 * device. Any multi-byte quantities are stored high byte 119 * first, and may have a 5 bit MSB in the same byte 120 * as the LUN. 121 */ 122
123 /* 124 * Manufacturers list 125 */ 126
127 #defineSCSI_MAN_UNKNOWN 0
128 #defineSCSI_MAN_NEC 1
129 #defineSCSI_MAN_TOSHIBA 2
130 #defineSCSI_MAN_NEC_OLDCDR 3
131 #defineSCSI_MAN_SONY 4
132
133 /* 134 * As the scsi do command functions are intelligent, and may need to 135 * redo a command, we need to keep track of the last command 136 * executed on each one. 137 */ 138
139 #defineWAS_RESET 0x01
140 #defineWAS_TIMEDOUT 0x02
141 #defineWAS_SENSE 0x04
142 #defineIS_RESETTING 0x08
143 #define IS_ABORTING 0x10
144 #defineASKED_FOR_SENSE 0x20
145
146 /* 147 * The scsi_device struct contains what we know about each given scsi 148 * device. 149 */ 150
151 typedefstructscsi_device{ 152 structscsi_device * next; /* Used for linked list */ 153
154 unsignedcharid, lun, channel;
155
156 unsignedintmanufacturer; /* Manufacturer of device, for using 157 * vendor-specific cmd's */ 158 intattached; /* # of high level drivers attached to 159 * this */ 160 intaccess_count; /* Count of open channels/mounts */ 161 structwait_queue * device_wait;/* Used to wait if device is busy */ 162 structScsi_Host * host;
163 void (*scsi_request_fn)(void); /* Used to jumpstart things after an 164 * ioctl */ 165 void *hostdata; /* available to low-level driver */ 166 chartype;
167 charscsi_level;
168 charvendor[8], model[16], rev[4];
169 unsignedcharcurrent_tag; /* current tag */ 170 unsignedchar sync_min_period; /* Not less than this period */ 171 unsignedchar sync_max_offset; /* Not greater than this offset */ 172
173 unsignedwriteable:1;
174 unsignedremovable:1;
175 unsignedrandom:1;
176 unsignedhas_cmdblocks:1;
177 unsignedchanged:1; /* Data invalid due to media change */ 178 unsignedbusy:1; /* Used to prevent races */ 179 unsignedlockable:1; /* Able to prevent media removal */ 180 unsignedborken:1; /* Tell the Seagate driver to be 181 * painfully slow on this device */ 182 unsignedtagged_supported:1; /* Supports SCSI-II tagged queuing */ 183 unsignedtagged_queue:1; /* SCSI-II tagged queuing enabled */ 184 unsigneddisconnect:1; /* can disconnect */ 185 unsignedsoft_reset:1; /* Uses soft reset option */ 186 unsignedsync:1; /* Negotiate for sync transfers */ 187 unsignedsingle_lun:1; /* Indicates we should only allow I/O to 188 one of the luns for the device at a time. */ 189 unsignedwas_reset:1; /* There was a bus reset on the bus for this 190 device */ 191 }Scsi_Device;
192
193 /* 194 * Use these to separate status msg and our bytes 195 */ 196
197 #definestatus_byte(result) (((result) >> 1) & 0xf)
198 #definemsg_byte(result) (((result) >> 8) & 0xff)
199 #definehost_byte(result) (((result) >> 16) & 0xff)
200 #definedriver_byte(result) (((result) >> 24) & 0xff)
201 #definesuggestion(result) (driver_byte(result) & SUGGEST_MASK)
202
203 #definesense_class(sense) (((sense) >> 4) & 0x7)
204 #definesense_error(sense) ((sense) & 0xf)
205 #define sense_valid(sense) ((sense) & 0x80);
206
207 /* 208 * These are the SCSI devices available on the system. 209 */ 210
211 externScsi_Device * scsi_devices;
212
213 /* 214 * Initializes all SCSI devices. This scans all scsi busses. 215 */ 216
217 externunsignedlongscsi_dev_init (unsignedlong, unsignedlong);
218
219 structscatterlist{ 220 char * address; /* Location data is to be transferred to */ 221 char * alt_address; /* Location of actual if address is a 222 * dma indirect buffer. NULL otherwise */ 223 unsignedintlength;
224 };
225
226 #ifdef__alpha__ 227 # defineISA_DMA_THRESHOLD (~0UL)
228 #else 229 # defineISA_DMA_THRESHOLD (0x00ffffff)
230 #endif 231 #defineCONTIGUOUS_BUFFERS(X,Y) ((X->b_data+X->b_size) == Y->b_data)
232
233
234 /* 235 * These are the return codes for the abort and reset functions. The mid-level 236 * code uses these to decide what to do next. Each of the low level abort 237 * and reset functions must correctly indicate what it has done. 238 * The descriptions are written from the point of view of the mid-level code, 239 * so that the return code is telling the mid-level drivers exactly what 240 * the low level driver has already done, and what remains to be done. 241 */ 242
243 /* We did not do anything. 244 * Wait some more for this command to complete, and if this does not work, 245 * try something more serious. */ 246 #defineSCSI_ABORT_SNOOZE 0
247
248 /* This means that we were able to abort the command. We have already 249 * called the mid-level done function, and do not expect an interrupt that 250 * will lead to another call to the mid-level done function for this command */ 251 #defineSCSI_ABORT_SUCCESS 1
252
253 /* We called for an abort of this command, and we should get an interrupt 254 * when this succeeds. Thus we should not restore the timer for this 255 * command in the mid-level abort function. */ 256 #defineSCSI_ABORT_PENDING 2
257
258 /* Unable to abort - command is currently on the bus. Grin and bear it. */ 259 #defineSCSI_ABORT_BUSY 3
260
261 /* The command is not active in the low level code. Command probably 262 * finished. */ 263 #defineSCSI_ABORT_NOT_RUNNING 4
264
265 /* Something went wrong. The low level driver will indicate the correct 266 * error condition when it calls scsi_done, so the mid-level abort function 267 * can simply wait until this comes through */ 268 #defineSCSI_ABORT_ERROR 5
269
270 /* We do not know how to reset the bus, or we do not want to. Bummer. 271 * Anyway, just wait a little more for the command in question, and hope that 272 * it eventually finishes. If it never finishes, the SCSI device could 273 * hang, so use this with caution. */ 274 #defineSCSI_RESET_SNOOZE 0
275
276 /* We do not know how to reset the bus, or we do not want to. Bummer. 277 * We have given up on this ever completing. The mid-level code will 278 * request sense information to decide how to proceed from here. */ 279 #defineSCSI_RESET_PUNT 1
280
281 /* This means that we were able to reset the bus. We have restarted all of 282 * the commands that should be restarted, and we should be able to continue 283 * on normally from here. We do not expect any interrupts that will return 284 * DID_RESET to any of the other commands in the host_queue, and the mid-level 285 * code does not need to do anything special to keep the commands alive. 286 * If a hard reset was performed then all outstanding commands on the 287 * bus have been restarted. */ 288 #defineSCSI_RESET_SUCCESS 2
289
290 /* We called for a reset of this bus, and we should get an interrupt 291 * when this succeeds. Each command should get its own status 292 * passed up to scsi_done, but this has not happened yet. 293 * If a hard reset was performed, then we expect an interrupt 294 * for *each* of the outstanding commands that will have the 295 * effect of restarting the commands. 296 */ 297 #defineSCSI_RESET_PENDING 3
298
299 /* We did a reset, but do not expect an interrupt to signal DID_RESET. 300 * This tells the upper level code to request the sense info, and this 301 * should keep the command alive. */ 302 #defineSCSI_RESET_WAKEUP 4
303
304 /* Something went wrong, and we do not know how to fix it. */ 305 #defineSCSI_RESET_ERROR 5
306
307 /* 308 * This is a bitmask that is ored with one of the above codes. 309 * It tells the mid-level code that we did a hard reset. 310 */ 311 #defineSCSI_RESET_BUS_RESET 0x100
312 /* 313 * Used to mask off bits and to obtain the basic action that was 314 * performed. 315 */ 316 #defineSCSI_RESET_ACTION 0xff
317
318 void * scsi_malloc(unsignedint);
319 intscsi_free(void *, unsignedint);
320 externunsignedintdma_free_sectors; /* How much room do we have left */ 321 externunsignedintneed_isa_buffer; /* True if some devices need indirection 322 * buffers */ 323
324 /* 325 * The Scsi_Cmnd structure is used by scsi.c internally, and for communication 326 * with low level drivers that support multiple outstanding commands. 327 */ 328 typedefstruct scsi_pointer { 329 char * ptr; /* data pointer */ 330 intthis_residual; /* left in this buffer */ 331 structscatterlist *buffer; /* which buffer */ 332 intbuffers_residual; /* how many buffers left */ 333
334 volatileintStatus;
335 volatileintMessage;
336 volatileinthave_data_in;
337 volatileintsent_command;
338 volatileintphase;
339 }Scsi_Pointer;
340
341 typedefstructscsi_cmnd{ 342 structScsi_Host * host;
343 Scsi_Device * device;
344 unsignedchartarget, lun, channel;
345 unsignedcharcmd_len;
346 unsignedcharold_cmd_len;
347 structscsi_cmnd *next, *prev;
348
349 /* These elements define the operation we are about to perform */ 350 unsignedcharcmnd[12];
351 unsignedrequest_bufflen; /* Actual request size */ 352
353 void * request_buffer; /* Actual requested buffer */ 354
355 /* These elements define the operation we ultimately want to perform */ 356 unsignedchardata_cmnd[12];
357 unsignedshortold_use_sg; /* We save use_sg here when requesting 358 * sense info */ 359 unsignedshortuse_sg; /* Number of pieces of scatter-gather */ 360 unsignedshortsglist_len; /* size of malloc'd scatter-gather list */ 361 unsignedshortabort_reason;/* If the mid-level code requests an 362 * abort, this is the reason. */ 363 unsignedbufflen; /* Size of data buffer */ 364 void *buffer; /* Data buffer */ 365
366 unsignedunderflow; /* Return error if less than this amount is 367 * transfered */ 368
369 unsignedtransfersize; /* How much we are guaranteed to transfer with 370 * each SCSI transfer (ie, between disconnect / 371 * reconnects. Probably == sector size */ 372
373
374 structrequestrequest; /* A copy of the command we are working on */ 375
376 unsignedcharsense_buffer[16]; /* Sense for this command, if needed */ 377
378
379 intretries;
380 intallowed;
381 inttimeout_per_command, timeout_total, timeout;
382
383 /* 384 * We handle the timeout differently if it happens when a reset, 385 * abort, etc are in process. 386 */ 387 unsignedvolatilecharinternal_timeout;
388
389 unsignedflags;
390
391 /* These variables are for the cdrom only. Once we have variable size 392 * buffers in the buffer cache, they will go away. */ 393 intthis_count;
394 /* End of special cdrom variables */ 395
396 /* Low-level done function - can be used by low-level driver to point 397 * to completion function. Not used by mid/upper level code. */ 398 void (*scsi_done)(structscsi_cmnd *);
399 void (*done)(structscsi_cmnd *); /* Mid-level done function */ 400
401 /* 402 * The following fields can be written to by the host specific code. 403 * Everything else should be left alone. 404 */ 405
406 Scsi_PointerSCp; /* Scratchpad used by some host adapters */ 407
408 unsignedchar * host_scribble; /* The host adapter is allowed to 409 * call scsi_malloc and get some memory 410 * and hang it here. The host adapter 411 * is also expected to call scsi_free 412 * to release this memory. (The memory 413 * obtained by scsi_malloc is guaranteed 414 * to be at an address < 16Mb). */ 415
416 intresult; /* Status code from lower level driver */ 417
418 unsignedchartag; /* SCSI-II queued command tag */ 419 unsignedlongpid; /* Process ID, starts at 0 */ 420 }Scsi_Cmnd;
421
422 /* 423 * scsi_abort aborts the current command that is executing on host host. 424 * The error code, if non zero is returned in the host byte, otherwise 425 * DID_ABORT is returned in the hostbyte. 426 */ 427
428 externintscsi_abort (Scsi_Cmnd *, intcode, intpid);
429
430 externvoidscsi_do_cmd (Scsi_Cmnd *, constvoid *cmnd ,
431 void *buffer, unsignedbufflen,
432 void (*done)(structscsi_cmnd *),
433 inttimeout, intretries);
434
435
436 externScsi_Cmnd * allocate_device(structrequest **, Scsi_Device *, int);
437
438 externScsi_Cmnd * request_queueable(structrequest *, Scsi_Device *);
439 externintscsi_reset (Scsi_Cmnd *, int);
440
441 externintmax_scsi_hosts;
442
443 externvoidbuild_proc_dir_entries(void);
444 externvoidproc_print_scsidevice(Scsi_Device *, char *, int *, int);
445
446
447 externvoidprint_command(unsignedchar *);
448 externvoidprint_sense(constchar *, Scsi_Cmnd *);
449
450
451 #ifdefined(MAJOR_NR) && (MAJOR_NR != SCSI_TAPE_MAJOR)
452 #include "hosts.h"
453
454 staticScsi_Cmnd * end_scsi_request(Scsi_Cmnd * SCpnt, intuptodate, intsectors)
/* */ 455 { 456 structrequest * req;
457 structbuffer_head * bh;
458
459 req = &SCpnt->request;
460 req->errors = 0;
461 if (!uptodate) { 462 printk(DEVICE_NAME " I/O error: dev %04x, sector %lu\n",
463 req->dev,req->sector);
464 } 465
466 do{ 467 if ((bh = req->bh) != NULL) { 468 req->bh = bh->b_reqnext;
469 req->nr_sectors -= bh->b_size >> 9;
470 req->sector += bh->b_size >> 9;
471 bh->b_reqnext = NULL;
472 bh->b_uptodate = uptodate;
473 unlock_buffer(bh);
474 sectors -= bh->b_size >> 9;
475 if ((bh = req->bh) != NULL) { 476 req->current_nr_sectors = bh->b_size >> 9;
477 if (req->nr_sectors < req->current_nr_sectors) { 478 req->nr_sectors = req->current_nr_sectors;
479 printk("end_scsi_request: buffer-list destroyed\n");
480 } 481 } 482 } 483 }while(sectors && bh);
484 if (req->bh){ 485 req->buffer = bh->b_data;
486 returnSCpnt;
487 };
488 DEVICE_OFF(req->dev);
489 if (req->sem != NULL) { 490 up(req->sem);
491 } 492
493 if (SCpnt->host->block) { 494 structScsi_Host * next;
495
496 for (next = SCpnt->host->block; next != SCpnt->host;
497 next = next->block)
498 wake_up(&next->host_wait);
499 } 500
501 req->dev = -1;
502 wake_up(&wait_for_request);
503 wake_up(&SCpnt->device->device_wait);
504 returnNULL;
505 } 506
507
508 /* This is just like INIT_REQUEST, but we need to be aware of the fact 509 * that an interrupt may start another request, so we run this with interrupts 510 * turned off 511 */ 512 #defineINIT_SCSI_REQUEST \
513 if (!CURRENT) {\
514 CLEAR_INTR; \
515 restore_flags(flags); \
516 return; \
517 } \
518 if (MAJOR(CURRENT->dev) != MAJOR_NR) \
519 panic(DEVICE_NAME ": request list destroyed"); \
520 if (CURRENT->bh) { \
521 if (!CURRENT->bh->b_lock) \
522 panic(DEVICE_NAME ": block not locked"); \
523 } 524 #endif 525
526 #defineSCSI_SLEEP(QUEUE, CONDITION) { \
527 if (CONDITION) { \
528 structwait_queuewait = {current, NULL}; \
529 add_wait_queue(QUEUE, &wait); \
530 for(;;) { \
531 current->state = TASK_UNINTERRUPTIBLE; \
532 if (CONDITION) { \
533 if (intr_count) \
534 panic("scsi: trying to call schedule() in interrupt" \
535 ", file %s, line %d.\n", __FILE__, __LINE__); \
536 schedule(); \
537 } \
538 else \
539 break; \
540 } \
541 remove_wait_queue(QUEUE, &wait); \
542 current->state = TASK_RUNNING; \
543 }; } 544
545 #endif 546
547 /* 548 * Overrides for Emacs so that we follow Linus's tabbing style. 549 * Emacs will notice this stuff at the end of the file and automatically 550 * adjust the settings for this buffer only. This must remain at the end 551 * of the file. 552 * --------------------------------------------------------------------------- 553 * Local variables: 554 * c-indent-level: 4 555 * c-brace-imaginary-offset: 0 556 * c-brace-offset: -4 557 * c-argdecl-indent: 4 558 * c-label-offset: -4 559 * c-continued-statement-offset: 4 560 * c-continued-brace-offset: 0 561 * indent-tabs-mode: nil 562 * tab-width: 8 563 * End: 564 */