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