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 /* 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 void *hostdata; /* available to low-level driver */ 167 chartype;
168 charscsi_level;
169 charvendor[8], model[16], rev[4];
170 unsignedcharcurrent_tag; /* current tag */ 171 unsignedchar sync_min_period; /* Not less than this period */ 172 unsignedchar sync_max_offset; /* Not greater than this offset */ 173
174 unsignedwriteable:1;
175 unsignedremovable:1;
176 unsignedrandom:1;
177 unsignedhas_cmdblocks:1;
178 unsignedchanged:1; /* Data invalid due to media change */ 179 unsignedbusy:1; /* Used to prevent races */ 180 unsignedlockable:1; /* Able to prevent media removal */ 181 unsignedborken:1; /* Tell the Seagate driver to be 182 * painfully slow on this device */ 183 unsignedtagged_supported:1; /* Supports SCSI-II tagged queuing */ 184 unsignedtagged_queue:1; /* SCSI-II tagged queuing enabled */ 185 unsigneddisconnect:1; /* can disconnect */ 186 unsignedsoft_reset:1; /* Uses soft reset option */ 187 unsignedsync:1; /* Negotiate for sync transfers */ 188 unsignedsingle_lun:1; /* Indicates we should only allow I/O to 189 one of the luns for the device at a time. */ 190 unsignedwas_reset:1; /* There was a bus reset on the bus for this 191 device */ 192 unsignedexpecting_cc_ua:1; /* Expecting a CHECK_CONDITION/UNIT_ATTN 193 because we did a bus reset. */ 194 }Scsi_Device;
195
196 /* 197 * Use these to separate status msg and our bytes 198 */ 199
200 #definestatus_byte(result) (((result) >> 1) & 0xf)
201 #definemsg_byte(result) (((result) >> 8) & 0xff)
202 #definehost_byte(result) (((result) >> 16) & 0xff)
203 #definedriver_byte(result) (((result) >> 24) & 0xff)
204 #definesuggestion(result) (driver_byte(result) & SUGGEST_MASK)
205
206 #definesense_class(sense) (((sense) >> 4) & 0x7)
207 #definesense_error(sense) ((sense) & 0xf)
208 #define sense_valid(sense) ((sense) & 0x80);
209
210 /* 211 * These are the SCSI devices available on the system. 212 */ 213
214 externScsi_Device * scsi_devices;
215
216 /* 217 * Initializes all SCSI devices. This scans all scsi busses. 218 */ 219
220 externintscsi_dev_init (void);
221
222 structscatterlist{ 223 char * address; /* Location data is to be transferred to */ 224 char * alt_address; /* Location of actual if address is a 225 * dma indirect buffer. NULL otherwise */ 226 unsignedintlength;
227 };
228
229 #ifdef__alpha__ 230 # defineISA_DMA_THRESHOLD (~0UL)
231 #else 232 # defineISA_DMA_THRESHOLD (0x00ffffff)
233 #endif 234 #defineCONTIGUOUS_BUFFERS(X,Y) ((X->b_data+X->b_size) == Y->b_data)
235
236
237 /* 238 * These are the return codes for the abort and reset functions. The mid-level 239 * code uses these to decide what to do next. Each of the low level abort 240 * and reset functions must correctly indicate what it has done. 241 * The descriptions are written from the point of view of the mid-level code, 242 * so that the return code is telling the mid-level drivers exactly what 243 * the low level driver has already done, and what remains to be done. 244 */ 245
246 /* We did not do anything. 247 * Wait some more for this command to complete, and if this does not work, 248 * try something more serious. */ 249 #defineSCSI_ABORT_SNOOZE 0
250
251 /* This means that we were able to abort the command. We have already 252 * called the mid-level done function, and do not expect an interrupt that 253 * will lead to another call to the mid-level done function for this command */ 254 #defineSCSI_ABORT_SUCCESS 1
255
256 /* We called for an abort of this command, and we should get an interrupt 257 * when this succeeds. Thus we should not restore the timer for this 258 * command in the mid-level abort function. */ 259 #defineSCSI_ABORT_PENDING 2
260
261 /* Unable to abort - command is currently on the bus. Grin and bear it. */ 262 #defineSCSI_ABORT_BUSY 3
263
264 /* The command is not active in the low level code. Command probably 265 * finished. */ 266 #defineSCSI_ABORT_NOT_RUNNING 4
267
268 /* Something went wrong. The low level driver will indicate the correct 269 * error condition when it calls scsi_done, so the mid-level abort function 270 * can simply wait until this comes through */ 271 #defineSCSI_ABORT_ERROR 5
272
273 /* We do not know how to reset the bus, or we do not want to. Bummer. 274 * Anyway, just wait a little more for the command in question, and hope that 275 * it eventually finishes. If it never finishes, the SCSI device could 276 * hang, so use this with caution. */ 277 #defineSCSI_RESET_SNOOZE 0
278
279 /* We do not know how to reset the bus, or we do not want to. Bummer. 280 * We have given up on this ever completing. The mid-level code will 281 * request sense information to decide how to proceed from here. */ 282 #defineSCSI_RESET_PUNT 1
283
284 /* This means that we were able to reset the bus. We have restarted all of 285 * the commands that should be restarted, and we should be able to continue 286 * on normally from here. We do not expect any interrupts that will return 287 * DID_RESET to any of the other commands in the host_queue, and the mid-level 288 * code does not need to do anything special to keep the commands alive. 289 * If a hard reset was performed then all outstanding commands on the 290 * bus have been restarted. */ 291 #defineSCSI_RESET_SUCCESS 2
292
293 /* We called for a reset of this bus, and we should get an interrupt 294 * when this succeeds. Each command should get its own status 295 * passed up to scsi_done, but this has not happened yet. 296 * If a hard reset was performed, then we expect an interrupt 297 * for *each* of the outstanding commands that will have the 298 * effect of restarting the commands. 299 */ 300 #defineSCSI_RESET_PENDING 3
301
302 /* We did a reset, but do not expect an interrupt to signal DID_RESET. 303 * This tells the upper level code to request the sense info, and this 304 * should keep the command alive. */ 305 #defineSCSI_RESET_WAKEUP 4
306
307 /* Something went wrong, and we do not know how to fix it. */ 308 #defineSCSI_RESET_ERROR 5
309
310 /* 311 * This is a bitmask that is ored with one of the above codes. 312 * It tells the mid-level code that we did a hard reset. 313 */ 314 #defineSCSI_RESET_BUS_RESET 0x100
315 /* 316 * Used to mask off bits and to obtain the basic action that was 317 * performed. 318 */ 319 #defineSCSI_RESET_ACTION 0xff
320
321 void * scsi_malloc(unsignedint);
322 intscsi_free(void *, unsignedint);
323 externunsignedintdma_free_sectors; /* How much room do we have left */ 324 externunsignedintneed_isa_buffer; /* True if some devices need indirection 325 * buffers */ 326
327 /* 328 * The Scsi_Cmnd structure is used by scsi.c internally, and for communication 329 * with low level drivers that support multiple outstanding commands. 330 */ 331 typedefstruct scsi_pointer { 332 char * ptr; /* data pointer */ 333 intthis_residual; /* left in this buffer */ 334 structscatterlist *buffer; /* which buffer */ 335 intbuffers_residual; /* how many buffers left */ 336
337 volatileintStatus;
338 volatileintMessage;
339 volatileinthave_data_in;
340 volatileintsent_command;
341 volatileintphase;
342 }Scsi_Pointer;
343
344 typedefstructscsi_cmnd{ 345 structScsi_Host * host;
346 Scsi_Device * device;
347 unsignedchartarget, lun, channel;
348 unsignedcharcmd_len;
349 unsignedcharold_cmd_len;
350 structscsi_cmnd *next, *prev;
351
352 /* These elements define the operation we are about to perform */ 353 unsignedcharcmnd[12];
354 unsignedrequest_bufflen; /* Actual request size */ 355
356 void * request_buffer; /* Actual requested buffer */ 357
358 /* These elements define the operation we ultimately want to perform */ 359 unsignedchardata_cmnd[12];
360 unsignedshortold_use_sg; /* We save use_sg here when requesting 361 * sense info */ 362 unsignedshortuse_sg; /* Number of pieces of scatter-gather */ 363 unsignedshortsglist_len; /* size of malloc'd scatter-gather list */ 364 unsignedshortabort_reason;/* If the mid-level code requests an 365 * abort, this is the reason. */ 366 unsignedbufflen; /* Size of data buffer */ 367 void *buffer; /* Data buffer */ 368
369 unsignedunderflow; /* Return error if less than this amount is 370 * transfered */ 371
372 unsignedtransfersize; /* How much we are guaranteed to transfer with 373 * each SCSI transfer (ie, between disconnect / 374 * reconnects. Probably == sector size */ 375
376
377 structrequestrequest; /* A copy of the command we are working on */ 378
379 unsignedcharsense_buffer[16]; /* Sense for this command, if needed */ 380
381
382 intretries;
383 intallowed;
384 inttimeout_per_command, timeout_total, timeout;
385
386 /* 387 * We handle the timeout differently if it happens when a reset, 388 * abort, etc are in process. 389 */ 390 unsignedvolatilecharinternal_timeout;
391
392 unsignedflags;
393
394 /* These variables are for the cdrom only. Once we have variable size 395 * buffers in the buffer cache, they will go away. */ 396 intthis_count;
397 /* End of special cdrom variables */ 398
399 /* Low-level done function - can be used by low-level driver to point 400 * to completion function. Not used by mid/upper level code. */ 401 void (*scsi_done)(structscsi_cmnd *);
402 void (*done)(structscsi_cmnd *); /* Mid-level done function */ 403
404 /* 405 * The following fields can be written to by the host specific code. 406 * Everything else should be left alone. 407 */ 408
409 Scsi_PointerSCp; /* Scratchpad used by some host adapters */ 410
411 unsignedchar * host_scribble; /* The host adapter is allowed to 412 * call scsi_malloc and get some memory 413 * and hang it here. The host adapter 414 * is also expected to call scsi_free 415 * to release this memory. (The memory 416 * obtained by scsi_malloc is guaranteed 417 * to be at an address < 16Mb). */ 418
419 intresult; /* Status code from lower level driver */ 420
421 unsignedchartag; /* SCSI-II queued command tag */ 422 unsignedlongpid; /* Process ID, starts at 0 */ 423 }Scsi_Cmnd;
424
425 /* 426 * scsi_abort aborts the current command that is executing on host host. 427 * The error code, if non zero is returned in the host byte, otherwise 428 * DID_ABORT is returned in the hostbyte. 429 */ 430
431 externintscsi_abort (Scsi_Cmnd *, intcode, intpid);
432
433 externvoidscsi_do_cmd (Scsi_Cmnd *, constvoid *cmnd ,
434 void *buffer, unsignedbufflen,
435 void (*done)(structscsi_cmnd *),
436 inttimeout, intretries);
437
438
439 externScsi_Cmnd * allocate_device(structrequest **, Scsi_Device *, int);
440
441 externScsi_Cmnd * request_queueable(structrequest *, Scsi_Device *);
442 externintscsi_reset (Scsi_Cmnd *, int);
443
444 externintmax_scsi_hosts;
445
446 externvoidproc_print_scsidevice(Scsi_Device *, char *, int *, int);
447
448 externvoidprint_command(unsignedchar *);
449 externvoidprint_sense(constchar *, Scsi_Cmnd *);
450 externvoidprint_driverbyte(intscsiresult);
451 externvoidprint_hostbyte(intscsiresult);
452
453 externvoidscsi_mark_host_bus_reset(structScsi_Host *Host);
454
455 #ifdefined(MAJOR_NR) && (MAJOR_NR != SCSI_TAPE_MAJOR)
456 #include "hosts.h"
457
458 staticScsi_Cmnd * end_scsi_request(Scsi_Cmnd * SCpnt, intuptodate, intsectors)
/* */ 459 { 460 structrequest * req;
461 structbuffer_head * bh;
462
463 req = &SCpnt->request;
464 req->errors = 0;
465 if (!uptodate) { 466 printk(DEVICE_NAME " I/O error: dev %s, sector %lu\n",
467 kdevname(req->rq_dev), req->sector);
468 } 469
470 do{ 471 if ((bh = req->bh) != NULL) { 472 req->bh = bh->b_reqnext;
473 req->nr_sectors -= bh->b_size >> 9;
474 req->sector += bh->b_size >> 9;
475 bh->b_reqnext = NULL;
476 mark_buffer_uptodate(bh, uptodate);
477 unlock_buffer(bh);
478 sectors -= bh->b_size >> 9;
479 if ((bh = req->bh) != NULL) { 480 req->current_nr_sectors = bh->b_size >> 9;
481 if (req->nr_sectors < req->current_nr_sectors) { 482 req->nr_sectors = req->current_nr_sectors;
483 printk("end_scsi_request: buffer-list destroyed\n");
484 } 485 } 486 } 487 }while(sectors && bh);
488 if (req->bh){ 489 req->buffer = bh->b_data;
490 returnSCpnt;
491 };
492 DEVICE_OFF(req->rq_dev);
493 if (req->sem != NULL) { 494 up(req->sem);
495 } 496
497 if (SCpnt->host->block) { 498 structScsi_Host * next;
499
500 for (next = SCpnt->host->block; next != SCpnt->host;
501 next = next->block)
502 wake_up(&next->host_wait);
503 } 504
505 req->rq_status = RQ_INACTIVE;
506 wake_up(&wait_for_request);
507 wake_up(&SCpnt->device->device_wait);
508 returnNULL;
509 } 510
511
512 /* This is just like INIT_REQUEST, but we need to be aware of the fact 513 * that an interrupt may start another request, so we run this with interrupts 514 * turned off 515 */ 516 #defineINIT_SCSI_REQUEST \
517 if (!CURRENT) { \
518 CLEAR_INTR; \
519 restore_flags(flags); \
520 return; \
521 } \
522 if (MAJOR(CURRENT->rq_dev) != MAJOR_NR) \
523 panic(DEVICE_NAME ": request list destroyed");\
524 if (CURRENT->bh) { \
525 if (!buffer_locked(CURRENT->bh)) \
526 panic(DEVICE_NAME ": block not locked"); \
527 } 528 #endif 529
530 #defineSCSI_SLEEP(QUEUE, CONDITION) { \
531 if (CONDITION) { \
532 structwait_queuewait = {current, NULL}; \
533 add_wait_queue(QUEUE, &wait); \
534 for(;;) { \
535 current->state = TASK_UNINTERRUPTIBLE; \
536 if (CONDITION) { \
537 if (intr_count) \
538 panic("scsi: trying to call schedule() in interrupt" \
539 ", file %s, line %d.\n", __FILE__, __LINE__); \
540 schedule(); \
541 } \
542 else \
543 break; \
544 } \
545 remove_wait_queue(QUEUE, &wait);\
546 current->state = TASK_RUNNING; \
547 }; } 548
549 #endif 550
551 /* 552 * Overrides for Emacs so that we follow Linus's tabbing style. 553 * Emacs will notice this stuff at the end of the file and automatically 554 * adjust the settings for this buffer only. This must remain at the end 555 * of the file. 556 * --------------------------------------------------------------------------- 557 * Local variables: 558 * c-indent-level: 4 559 * c-brace-imaginary-offset: 0 560 * c-brace-offset: -4 561 * c-argdecl-indent: 4 562 * c-label-offset: -4 563 * c-continued-statement-offset: 4 564 * c-continued-brace-offset: 0 565 * indent-tabs-mode: nil 566 * tab-width: 8 567 * End: 568 */