1 /* 2 * scsi.h Copyright (C) 1992 Drew Eckhardt 3 * generic SCSI package header file by 4 * Drew Eckhardt 5 * 6 * <drew@colorado.edu> 7 * 8 * Modified by Eric Youngdale eric@tantalus.nrl.navy.mil to 9 * add scatter-gather, multiple outstanding request, and other 10 * enhancements. 11 */ 12
13 #ifndef_SCSI_H 14 #define_SCSI_H 15
16 /* 17 $Header: /usr/src/linux/kernel/blk_drv/scsi/RCS/scsi.h,v 1.3 1993/09/24 12:20:33 drew Exp $ 18
19 For documentation on the OPCODES, MESSAGES, and SENSE values, 20 please consult the SCSI standard. 21
22 */ 23
24 /* 25 SCSI opcodes 26 */ 27
28 #defineTEST_UNIT_READY 0x00
29 #defineREZERO_UNIT 0x01
30 #defineREQUEST_SENSE 0x03
31 #defineFORMAT_UNIT 0x04
32 #defineREAD_BLOCK_LIMITS 0x05
33 #defineREASSIGN_BLOCKS 0x07
34 #defineREAD_6 0x08
35 #defineWRITE_6 0x0a
36 #defineSEEK_6 0x0b
37 #define READ_REVERSE 0x0f
38 #defineWRITE_FILEMARKS 0x10
39 #defineSPACE 0x11
40 #defineINQUIRY 0x12
41 #define RECOVER_BUFFERED_DATA 0x14
42 #defineMODE_SELECT 0x15
43 #defineRESERVE 0x16
44 #define RELEASE 0x17
45 #defineCOPY 0x18
46 #defineERASE 0x19
47 #defineMODE_SENSE 0x1a
48 #defineSTART_STOP 0x1b
49 #define RECEIVE_DIAGNOSTIC 0x1c
50 #defineSEND_DIAGNOSTIC 0x1d
51 #defineALLOW_MEDIUM_REMOVAL 0x1e
52
53 #defineREAD_CAPACITY 0x25
54 #defineREAD_10 0x28
55 #defineWRITE_10 0x2a
56 #defineSEEK_10 0x2b
57 #defineWRITE_VERIFY 0x2e
58 #define VERIFY 0x2f
59 #defineSEARCH_HIGH 0x30
60 #defineSEARCH_EQUAL 0x31
61 #defineSEARCH_LOW 0x32
62 #define SET_LIMITS 0x33
63 #define PRE_FETCH 0x34
64 #defineREAD_POSITION 0x34
65 #define SYNCHRONIZE_CACHE 0x35
66 #define LOCK_UNLOCK_CACHE 0x36
67 #define READ_DEFECT_DATA 0x37
68 #defineCOMPARE 0x39
69 #defineCOPY_VERIFY 0x3a
70 #defineWRITE_BUFFER 0x3b
71 #define READ_BUFFER 0x3c
72 #define READ_LONG 0x3e
73 #defineCHANGE_DEFINITION 0x40
74 #defineLOG_SELECT 0x4c
75 #define LOG_SENSE 0x4d
76 #defineMODE_SELECT_10 0x55
77 #define MODE_SENSE_10 0x5a
78
79 externvolatileintin_scan_scsis;
80 externconstunsignedcharscsi_command_size[8];
81 #defineCOMMAND_SIZE(opcode) scsi_command_size[((opcode) >> 5) & 7]
82
83 /* 84 MESSAGE CODES 85 */ 86
87 #defineCOMMAND_COMPLETE 0x00
88 #defineEXTENDED_MESSAGE 0x01
89 #defineEXTENDED_MODIFY_DATA_POINTER 0x00
90 #defineEXTENDED_SDTR 0x01
91 #defineEXTENDED_EXTENDED_IDENTIFY 0x02 /* SCSI-I only */ 92 #defineEXTENDED_WDTR 0x03
93 #defineSAVE_POINTERS 0x02
94 #defineRESTORE_POINTERS 0x03
95 #defineDISCONNECT 0x04
96 #define INITIATOR_ERROR 0x05
97 #defineABORT 0x06
98 #defineMESSAGE_REJECT 0x07
99 #defineNOP 0x08
100 #define MSG_PARITY_ERROR 0x09
101 #defineLINKED_CMD_COMPLETE 0x0a
102 #defineLINKED_FLG_CMD_COMPLETE 0x0b
103 #define BUS_DEVICE_RESET 0x0c
104
105 #defineINITIATE_RECOVERY 0x0f /* SCSI-II only */ 106 #define RELEASE_RECOVERY 0x10 /* SCSI-II only */ 107
108 #defineSIMPLE_QUEUE_TAG 0x20
109 #defineHEAD_OF_QUEUE_TAG 0x21
110 #defineORDERED_QUEUE_TAG 0x22
111
112 #defineIDENTIFY_BASE 0x80
113 #defineIDENTIFY(can_disconnect, lun) (IDENTIFY_BASE |\
114 ((can_disconnect) ? 0x40 : 0) |\
115 ((lun) & 0x07))
116
117
118 /* 119 Status codes 120 */ 121
122 #defineGOOD 0x00
123 #defineCHECK_CONDITION 0x01
124 #defineCONDITION_GOOD 0x02
125 #defineBUSY 0x04
126 #defineINTERMEDIATE_GOOD 0x08
127 #defineINTERMEDIATE_C_GOOD 0x0a
128 #defineRESERVATION_CONFLICT 0x0c
129
130 #define STATUS_MASK 0x1e
131
132 /* 133 the return of the status word will be in the following format : 134 The low byte is the status returned by the SCSI command, 135 with vendor specific bits masked. 136
137 The next byte is the message which followed the SCSI status. 138 This allows a stos to be used, since the Intel is a little 139 endian machine. 140
141 The final byte is a host return code, which is one of the following. 142
143 IE 144 lsb msb 145 status msg host code 146
147 Our errors returned by OUR driver, NOT SCSI message. Or'd with 148 SCSI message passed back to driver <IF any>. 149 */ 150
151 /* NO error */ 152 #defineDID_OK 0x00
153 /* Couldn't connect before timeout period */ 154 #defineDID_NO_CONNECT 0x01
155 /* BUS stayed busy through time out period */ 156 #defineDID_BUS_BUSY 0x02
157 /* TIMED OUT for other reason */ 158 #defineDID_TIME_OUT 0x03
159 /* BAD target. */ 160 #defineDID_BAD_TARGET 0x04
161 /* Told to abort for some other reason */ 162 #defineDID_ABORT 0x05
163 /* 164 Parity error 165 */ 166 #defineDID_PARITY 0x06
167 /* 168 Internal error 169 */ 170 #defineDID_ERROR 0x07
171 /* 172 Reset by somebody. 173 */ 174 #defineDID_RESET 0x08
175 /* 176 Got an interrupt we weren't expecting. 177 */ 178 #defineDID_BAD_INTR 0x09
179
180 /* 181 Driver status 182 */ 183 #defineDRIVER_OK 0x00
184
185 /* 186 These indicate the error that occured, and what is available. 187 */ 188
189 #define DRIVER_BUSY 0x01
190 #defineDRIVER_SOFT 0x02
191 #define DRIVER_MEDIA 0x03
192 #defineDRIVER_ERROR 0x04
193
194 #defineDRIVER_INVALID 0x05
195 #defineDRIVER_TIMEOUT 0x06
196 #defineDRIVER_HARD 0x07
197
198 #defineSUGGEST_RETRY 0x10
199 #defineSUGGEST_ABORT 0x20
200 #defineSUGGEST_REMAP 0x30
201 #defineSUGGEST_DIE 0x40
202 #defineSUGGEST_SENSE 0x80
203 #defineSUGGEST_IS_OK 0xff
204
205 #defineDRIVER_SENSE 0x08
206
207 #define DRIVER_MASK 0x0f
208 #defineSUGGEST_MASK 0xf0
209
210 /* 211
212 SENSE KEYS 213 */ 214
215 #defineNO_SENSE 0x00
216 #defineRECOVERED_ERROR 0x01
217 #defineNOT_READY 0x02
218 #defineMEDIUM_ERROR 0x03
219 #defineHARDWARE_ERROR 0x04
220 #defineILLEGAL_REQUEST 0x05
221 #defineUNIT_ATTENTION 0x06
222 #defineDATA_PROTECT 0x07
223 #defineBLANK_CHECK 0x08
224 #defineCOPY_ABORTED 0x0a
225 #defineABORTED_COMMAND 0x0b
226 #defineVOLUME_OVERFLOW 0x0d
227 #defineMISCOMPARE 0x0e
228
229
230 /* 231 DEVICE TYPES 232
233 */ 234
235 #defineTYPE_DISK 0x00
236 #defineTYPE_TAPE 0x01
237 #defineTYPE_WORM 0x04 /* Treated as ROM by our system */ 238 #defineTYPE_ROM 0x05
239 #defineTYPE_MOD 0x07 /* Magneto-optical disk - treated as TYPE_DISK */ 240 #define TYPE_NO_LUN 0x7f
241
242
243 #defineMAX_COMMAND_SIZE 12
244 /* 245 SCSI command sets 246
247 */ 248
249 #define SCSI_UNKNOWN 0
250 #define SCSI_1 1
251 #define SCSI_1_CCS 2
252 #defineSCSI_2 3
253
254 /* 255 Every SCSI command starts with a one byte OP-code. 256 The next byte's high three bits are the LUN of the 257 device. Any multi-byte quantities are stored high byte 258 first, and may have a 5 bit MSB in the same byte 259 as the LUN. 260 */ 261
262
263 /* 264 The scsi_device struct contains what we know about each given scsi 265 device. 266 */ 267
268 typedefstructscsi_device{ 269 structscsi_device * next; /* Used for linked list */ 270 unsignedcharid, lun;
271 intattached; /* # of high level drivers attached to this */ 272 intaccess_count; /* Count of open channels/mounts */ 273 structwait_queue * device_wait; /* Used to wait if device is busy */ 274 structScsi_Host * host;
275 void (*scsi_request_fn)(void); /* Used to jumpstart things after an ioctl */ 276 void *hostdata; /* available to low-level driver */ 277 chartype;
278 charscsi_level;
279 unsignedwriteable:1;
280 unsignedremovable:1;
281 unsignedrandom:1;
282 unsignedchanged:1; /* Data invalid due to media change */ 283 unsignedbusy:1; /* Used to prevent races */ 284 unsignedlockable:1; /* Able to prevent media removal */ 285 unsignedborken:1; /* Tell the Seagate driver to be 286 painfully slow on this device */ 287 unsignedtagged_supported:1; /* Supports SCSI-II tagged queuing */ 288 unsignedtagged_queue:1; /*SCSI-II tagged queuing enabled */ 289 unsigneddisconnect:1; /* can disconnect */ 290 unsignedsoft_reset:1; /* Uses soft reset option */ 291 unsignedcharcurrent_tag; /* current tag */ 292 unsignedsync:1; /* Negotiate for sync transfers */ 293 unsignedchar sync_min_period; /* Not less than this period */ 294 unsignedchar sync_max_offset; /* Not greater than this offset */ 295 }Scsi_Device;
296 /* 297 Use these to separate status msg and our bytes 298 */ 299
300 #definestatus_byte(result) (((result) >> 1) & 0xf)
301 #definemsg_byte(result) (((result) >> 8) & 0xff)
302 #definehost_byte(result) (((result) >> 16) & 0xff)
303 #definedriver_byte(result) (((result) >> 24) & 0xff)
304 #definesuggestion(result) (driver_byte(result) & SUGGEST_MASK)
305
306 #definesense_class(sense) (((sense) >> 4) & 0x7)
307 #definesense_error(sense) ((sense) & 0xf)
308 #define sense_valid(sense) ((sense) & 0x80);
309
310 /* 311 These are the SCSI devices available on the system. 312 */ 313
314 externScsi_Device * scsi_devices;
315 /* 316 Initializes all SCSI devices. This scans all scsi busses. 317 */ 318
319 externunsignedlongscsi_dev_init (unsignedlong, unsignedlong);
320
321 structscatterlist{ 322 char * address; /* Location data is to be transferred to */ 323 char * alt_address; /* Location of actual if address is a 324 dma indirect buffer. NULL otherwise */ 325 unsignedintlength;
326 };
327
328 #defineISA_DMA_THRESHOLD (0x00ffffff)
329 #defineCONTIGUOUS_BUFFERS(X,Y) ((X->b_data+X->b_size) == Y->b_data)
330
331
332 /* 333 * These are the return codes for the abort and reset functions. The mid-level 334 * code uses these to decide what to do next. Each of the low level abort 335 * and reset functions must correctly indicate what it has done. 336 */ 337
338 /* We did not do anything. Wait 339 some more for this command to complete, and if this does not work, try 340 something more serious. */ 341 #defineSCSI_ABORT_SNOOZE 0
342
343 /* This means that we were able to abort the command. We have already 344 called the mid-level done function, and do not expect an interrupt that will 345 lead to another call to the mid-level done function for this command */ 346 #defineSCSI_ABORT_SUCCESS 1
347
348 /* We called for an abort of this command, and we should get an interrupt 349 when this succeeds. Thus we should not restore the timer for this 350 command in the mid-level abort function. */ 351 #defineSCSI_ABORT_PENDING 2
352
353 /* Unable to abort - command is currently on the bus. Grin and bear it. */ 354 #defineSCSI_ABORT_BUSY 3
355
356 /* The command is not active in the low level code. Command probably 357 finished. */ 358 #defineSCSI_ABORT_NOT_RUNNING 4
359
360 /* Something went wrong. The low level driver will indicate the correct 361 error condition when it calls scsi_done, so the mid-level abort function 362 can simply wait until this comes through */ 363 #defineSCSI_ABORT_ERROR 5
364
365 /* We do not know how to reset the bus, or we do not want to. Bummer. 366 Anyway, just wait a little more for the command in question, and hope that 367 it eventually finishes. If it never finishes, the SCSI device could 368 hang, so use this with caution. */ 369 #defineSCSI_RESET_SNOOZE 0
370
371 /* We do not know how to reset the bus, or we do not want to. Bummer. 372 We have given up on this ever completing. The mid-level code will 373 request sense information to decide how to proceed from here. */ 374 #defineSCSI_RESET_PUNT 1
375
376 /* This means that we were able to reset the bus. We have restarted all of 377 the commands that should be restarted, and we should be able to continue 378 on normally from here. We do not expect any interrupts that will return 379 DID_RESET to any of the other commands in the host_queue, and the mid-level 380 code does not need to do anything special to keep the commands alive. */ 381 #defineSCSI_RESET_SUCCESS 2
382
383 /* We called for an reset of this bus, and we should get an interrupt 384 when this succeeds. Each command should get it's own status 385 passed up to scsi_done, but this has not happened yet. */ 386 #defineSCSI_RESET_PENDING 3
387
388 /* We did a reset, but do not expect an interrupt to signal DID_RESET. 389 This tells the upper level code to request the sense info, and this 390 should keep the command alive. */ 391 #defineSCSI_RESET_WAKEUP 4
392
393 /* Something went wrong, and we do not know how to fix it. */ 394 #defineSCSI_RESET_ERROR 5
395
396 void * scsi_malloc(unsignedint);
397 intscsi_free(void *, unsignedint);
398 externunsignedintdma_free_sectors; /* How much room do we have left */ 399 externunsignedintneed_isa_buffer; /* True if some devices need indirection 400 buffers */ 401
402 /* 403 The Scsi_Cmnd structure is used by scsi.c internally, and for communication with 404 low level drivers that support multiple outstanding commands. 405 */ 406 typedefstruct scsi_pointer { 407 char * ptr; /* data pointer */ 408 intthis_residual; /* left in this buffer */ 409 structscatterlist *buffer; /* which buffer */ 410 intbuffers_residual; /* how many buffers left */ 411
412 volatileintStatus;
413 volatileintMessage;
414 volatileinthave_data_in;
415 volatileintsent_command;
416 volatileintphase;
417 }Scsi_Pointer;
418
419 typedefstructscsi_cmnd{ 420 structScsi_Host * host;
421 Scsi_Device * device;
422 unsignedchartarget, lun;
423 structscsi_cmnd *next, *prev;
424
425 /* These elements define the operation we are about to perform */ 426 unsignedcharcmnd[12];
427 unsignedrequest_bufflen; /* Actual request size */ 428
429 void * request_buffer; /* Actual requested buffer */ 430
431 /* These elements define the operation we ultimately want to perform */ 432 unsignedchardata_cmnd[12];
433 unsignedshortold_use_sg; /* We save use_sg here when requesting 434 sense info */ 435 unsignedshortuse_sg; /* Number of pieces of scatter-gather */ 436 unsignedshortsglist_len; /* size of malloc'd scatter-gather list */ 437 unsignedshortabort_reason; /* If the mid-level code requests an 438 abort, this is the reason. */ 439 unsignedbufflen; /* Size of data buffer */ 440 void *buffer; /* Data buffer */ 441
442 unsignedunderflow; /* Return error if less than this amount is 443 transfered */ 444
445 unsignedtransfersize; /* How much we are guaranteed to transfer with 446 each SCSI transfer (ie, between disconnect / 447 reconnects. Probably == sector size */ 448
449
450
451 structrequestrequest; /* A copy of the command we are working on*/ 452
453 unsignedcharsense_buffer[16]; /* Sense for this command, if needed*/ 454
455
456 intretries;
457 intallowed;
458 inttimeout_per_command, timeout_total, timeout;
459 /* 460 * We handle the timeout differently if it happens when a reset, 461 * abort, etc are in process. 462 */ 463
464 unsignedvolatilecharinternal_timeout;
465
466 unsignedflags;
467
468 /* These variables are for the cdrom only. Once we have variable size buffers 469 in the buffer cache, they will go away. */ 470 intthis_count;
471 /* End of special cdrom variables */ 472
473 /* Low-level done function - can be used by low-level driver to point 474 to completion function. Not used by mid/upper level code. */ 475 void (*scsi_done)(structscsi_cmnd *);
476 void (*done)(structscsi_cmnd *); /* Mid-level done function */ 477
478 /* The following fields can be written to by the host specific code. 479 Everything else should be left alone. */ 480
481 Scsi_PointerSCp; /* Scratchpad used by some host adapters */ 482
483 unsignedchar * host_scribble; /* The host adapter is allowed to 484 call scsi_malloc and get some memory 485 and hang it here. The host adapter 486 is also expected to call scsi_free 487 to release this memory. (The memory 488 obtained by scsi_malloc is guaranteed 489 to be at an address < 16Mb). */ 490
491 intresult; /* Status code from lower level driver */ 492
493 unsignedchartag; /* SCSI-II queued command tag */ 494 unsignedlongpid; /* Process ID, starts at 0 */ 495 }Scsi_Cmnd;
496
497 /* 498 scsi_abort aborts the current command that is executing on host host. 499 The error code, if non zero is returned in the host byte, otherwise 500 DID_ABORT is returned in the hostbyte. 501 */ 502
503 externintscsi_abort (Scsi_Cmnd *, intcode);
504
505 externvoidscsi_do_cmd (Scsi_Cmnd *, constvoid *cmnd ,
506 void *buffer, unsignedbufflen, void (*done)(structscsi_cmnd *),
507 inttimeout, intretries);
508
509
510 externScsi_Cmnd * allocate_device(structrequest **, Scsi_Device *, int);
511
512 externScsi_Cmnd * request_queueable(structrequest *, Scsi_Device *);
513 externintscsi_reset (Scsi_Cmnd *);
514
515 externintmax_scsi_hosts;
516
517 #ifdefined(MAJOR_NR) && (MAJOR_NR != SCSI_TAPE_MAJOR)
518 staticvoidend_scsi_request(Scsi_Cmnd * SCpnt, intuptodate, intsectors)
/* */ 519 { 520 structrequest * req;
521 structbuffer_head * bh;
522
523 req = &SCpnt->request;
524 req->errors = 0;
525 if (!uptodate) { 526 printk(DEVICE_NAME " I/O error: dev %04x, sector %lu\n",
527 req->dev,req->sector);
528 } 529
530 do{ 531 if ((bh = req->bh) != NULL) { 532 req->bh = bh->b_reqnext;
533 req->nr_sectors -= bh->b_size >> 9;
534 req->sector += bh->b_size >> 9;
535 bh->b_reqnext = NULL;
536 bh->b_uptodate = uptodate;
537 unlock_buffer(bh);
538 sectors -= bh->b_size >> 9;
539 if ((bh = req->bh) != NULL) { 540 req->current_nr_sectors = bh->b_size >> 9;
541 if (req->nr_sectors < req->current_nr_sectors) { 542 req->nr_sectors = req->current_nr_sectors;
543 printk("end_scsi_request: buffer-list destroyed\n");
544 } 545 } 546 } 547 }while(sectors && bh);
548 if (req->bh){ 549 req->buffer = bh->b_data;
550 return;
551 };
552 DEVICE_OFF(req->dev);
553 if (req->sem != NULL) { 554 up(req->sem);
555 } 556 req->dev = -1;
557 wake_up(&SCpnt->device->device_wait);
558 return;
559 } 560
561
562 /* This is just like INIT_REQUEST, but we need to be aware of the fact 563 that an interrupt may start another request, so we run this with interrupts 564 turned off */ 565
566 #defineINIT_SCSI_REQUEST \
567 if (!CURRENT) {\
568 CLEAR_INTR; \
569 sti(); \
570 return; \
571 } \
572 if (MAJOR(CURRENT->dev) != MAJOR_NR) \
573 panic(DEVICE_NAME ": request list destroyed"); \
574 if (CURRENT->bh) { \
575 if (!CURRENT->bh->b_lock) \
576 panic(DEVICE_NAME ": block not locked"); \
577 } 578 #endif 579
580 #defineSCSI_SLEEP(QUEUE, CONDITION) { \
581 if (CONDITION) { \
582 structwait_queuewait = {current, NULL}; \
583 add_wait_queue(QUEUE, &wait); \
584 sleep_repeat: \
585 current->state = TASK_UNINTERRUPTIBLE; \
586 if (CONDITION) { \
587 schedule(); \
588 gotosleep_repeat; \
589 } \
590 remove_wait_queue(QUEUE, &wait); \
591 current->state = TASK_RUNNING; \
592 }; } 593
594 #endif