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