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 #define TEST_UNIT_READY 0x00
29 #define REZERO_UNIT 0x01
30 #define REQUEST_SENSE 0x03
31 #define FORMAT_UNIT 0x04
32 #define READ_BLOCK_LIMITS 0x05
33 #define REASSIGN_BLOCKS 0x07
34 #define READ_6 0x08
35 #define WRITE_6 0x0a
36 #define SEEK_6 0x0b
37 #define READ_REVERSE 0x0f
38 #define WRITE_FILEMARKS 0x10
39 #define SPACE 0x11
40 #define INQUIRY 0x12
41 #define RECOVER_BUFFERED_DATA 0x14
42 #define MODE_SELECT 0x15
43 #define RESERVE 0x16
44 #define RELEASE 0x17
45 #define COPY 0x18
46 #define ERASE 0x19
47 #define MODE_SENSE 0x1a
48 #define START_STOP 0x1b
49 #define RECEIVE_DIAGNOSTIC 0x1c
50 #define SEND_DIAGNOSTIC 0x1d
51 #define ALLOW_MEDIUM_REMOVAL 0x1e
52
53 #define READ_CAPACITY 0x25
54 #define READ_10 0x28
55 #define WRITE_10 0x2a
56 #define SEEK_10 0x2b
57 #define WRITE_VERIFY 0x2e
58 #define VERIFY 0x2f
59 #define SEARCH_HIGH 0x30
60 #define SEARCH_EQUAL 0x31
61 #define SEARCH_LOW 0x32
62 #define SET_LIMITS 0x33
63 #define PRE_FETCH 0x34
64 #define READ_POSITION 0x34
65 #define SYNCHRONIZE_CACHE 0x35
66 #define LOCK_UNLOCK_CACHE 0x36
67 #define READ_DEFECT_DATA 0x37
68 #define COMPARE 0x39
69 #define COPY_VERIFY 0x3a
70 #define WRITE_BUFFER 0x3b
71 #define READ_BUFFER 0x3c
72 #define READ_LONG 0x3e
73 #define CHANGE_DEFINITION 0x40
74 #define LOG_SELECT 0x4c
75 #define LOG_SENSE 0x4d
76 #define MODE_SELECT_10 0x55
77 #define MODE_SENSE_10 0x5a
78
79 extern void scsi_make_blocked_list(void);
80 extern volatile int in_scan_scsis;
81 extern const unsigned char scsi_command_size[8];
82 #define COMMAND_SIZE(opcode) scsi_command_size[((opcode) >> 5) & 7]
83
84 /*
85 MESSAGE CODES
86 */
87
88 #define COMMAND_COMPLETE 0x00
89 #define EXTENDED_MESSAGE 0x01
90 #define EXTENDED_MODIFY_DATA_POINTER 0x00
91 #define EXTENDED_SDTR 0x01
92 #define EXTENDED_EXTENDED_IDENTIFY 0x02 /* SCSI-I only */
93 #define EXTENDED_WDTR 0x03
94 #define SAVE_POINTERS 0x02
95 #define RESTORE_POINTERS 0x03
96 #define DISCONNECT 0x04
97 #define INITIATOR_ERROR 0x05
98 #define ABORT 0x06
99 #define MESSAGE_REJECT 0x07
100 #define NOP 0x08
101 #define MSG_PARITY_ERROR 0x09
102 #define LINKED_CMD_COMPLETE 0x0a
103 #define LINKED_FLG_CMD_COMPLETE 0x0b
104 #define BUS_DEVICE_RESET 0x0c
105
106 #define INITIATE_RECOVERY 0x0f /* SCSI-II only */
107 #define RELEASE_RECOVERY 0x10 /* SCSI-II only */
108
109 #define SIMPLE_QUEUE_TAG 0x20
110 #define HEAD_OF_QUEUE_TAG 0x21
111 #define ORDERED_QUEUE_TAG 0x22
112
113 #define IDENTIFY_BASE 0x80
114 #define IDENTIFY(can_disconnect, lun) (IDENTIFY_BASE |\
115 ((can_disconnect) ? 0x40 : 0) |\
116 ((lun) & 0x07))
117
118
119 /*
120 Status codes
121 */
122
123 #define GOOD 0x00
124 #define CHECK_CONDITION 0x01
125 #define CONDITION_GOOD 0x02
126 #define BUSY 0x04
127 #define INTERMEDIATE_GOOD 0x08
128 #define INTERMEDIATE_C_GOOD 0x0a
129 #define RESERVATION_CONFLICT 0x0c
130 #define QUEUE_FULL 0x1a
131
132 #define STATUS_MASK 0x1e
133
134 /*
135 the return of the status word will be in the following format :
136 The low byte is the status returned by the SCSI command,
137 with vendor specific bits masked.
138
139 The next byte is the message which followed the SCSI status.
140 This allows a stos to be used, since the Intel is a little
141 endian machine.
142
143 The final byte is a host return code, which is one of the following.
144
145 IE
146 lsb msb
147 status msg host code
148
149 Our errors returned by OUR driver, NOT SCSI message. Or'd with
150 SCSI message passed back to driver <IF any>.
151 */
152
153 /* NO error */
154 #define DID_OK 0x00
155 /* Couldn't connect before timeout period */
156 #define DID_NO_CONNECT 0x01
157 /* BUS stayed busy through time out period */
158 #define DID_BUS_BUSY 0x02
159 /* TIMED OUT for other reason */
160 #define DID_TIME_OUT 0x03
161 /* BAD target. */
162 #define DID_BAD_TARGET 0x04
163 /* Told to abort for some other reason */
164 #define DID_ABORT 0x05
165 /*
166 Parity error
167 */
168 #define DID_PARITY 0x06
169 /*
170 Internal error
171 */
172 #define DID_ERROR 0x07
173 /*
174 Reset by somebody.
175 */
176 #define DID_RESET 0x08
177 /*
178 Got an interrupt we weren't expecting.
179 */
180 #define DID_BAD_INTR 0x09
181
182 /*
183 Driver status
184 */
185 #define DRIVER_OK 0x00
186
187 /*
188 These indicate the error that occurred, and what is available.
189 */
190
191 #define DRIVER_BUSY 0x01
192 #define DRIVER_SOFT 0x02
193 #define DRIVER_MEDIA 0x03
194 #define DRIVER_ERROR 0x04
195
196 #define DRIVER_INVALID 0x05
197 #define DRIVER_TIMEOUT 0x06
198 #define DRIVER_HARD 0x07
199
200 #define SUGGEST_RETRY 0x10
201 #define SUGGEST_ABORT 0x20
202 #define SUGGEST_REMAP 0x30
203 #define SUGGEST_DIE 0x40
204 #define SUGGEST_SENSE 0x80
205 #define SUGGEST_IS_OK 0xff
206
207 #define DRIVER_SENSE 0x08
208
209 #define DRIVER_MASK 0x0f
210 #define SUGGEST_MASK 0xf0
211
212 /*
213
214 SENSE KEYS
215 */
216
217 #define NO_SENSE 0x00
218 #define RECOVERED_ERROR 0x01
219 #define NOT_READY 0x02
220 #define MEDIUM_ERROR 0x03
221 #define HARDWARE_ERROR 0x04
222 #define ILLEGAL_REQUEST 0x05
223 #define UNIT_ATTENTION 0x06
224 #define DATA_PROTECT 0x07
225 #define BLANK_CHECK 0x08
226 #define COPY_ABORTED 0x0a
227 #define ABORTED_COMMAND 0x0b
228 #define VOLUME_OVERFLOW 0x0d
229 #define MISCOMPARE 0x0e
230
231
232 /*
233 DEVICE TYPES
234
235 */
236
237 #define TYPE_DISK 0x00
238 #define TYPE_TAPE 0x01
239 #define TYPE_PROCESSOR 0x03 /* HP scanners use this */
240 #define TYPE_WORM 0x04 /* Treated as ROM by our system */
241 #define TYPE_ROM 0x05
242 #define TYPE_SCANNER 0x06
243 #define TYPE_MOD 0x07 /* Magneto-optical disk - treated as TYPE_DISK */
244 #define TYPE_NO_LUN 0x7f
245
246
247 #define MAX_COMMAND_SIZE 12
248 /*
249 SCSI command sets
250
251 */
252
253 #define SCSI_UNKNOWN 0
254 #define SCSI_1 1
255 #define SCSI_1_CCS 2
256 #define SCSI_2 3
257
258 /*
259 Every SCSI command starts with a one byte OP-code.
260 The next byte's high three bits are the LUN of the
261 device. Any multi-byte quantities are stored high byte
262 first, and may have a 5 bit MSB in the same byte
263 as the LUN.
264 */
265
266 /*
267 Manufacturers list
268 */
269
270 #define SCSI_MAN_UNKNOWN 0
271 #define SCSI_MAN_NEC 1
272 #define SCSI_MAN_TOSHIBA 2
273
274 /*
275 The scsi_device struct contains what we know about each given scsi
276 device.
277 */
278
279 typedef struct scsi_device {
280 struct scsi_device * next; /* Used for linked list */
281 unsigned char id, lun;
282 unsigned int manufacturer; /* Manufacturer of device, for using vendor-specific cmd's */
283 int attached; /* # of high level drivers attached to this */
284 int access_count; /* Count of open channels/mounts */
285 struct wait_queue * device_wait; /* Used to wait if device is busy */
286 struct Scsi_Host * host;
287 void (*scsi_request_fn)(void); /* Used to jumpstart things after an ioctl */
288 void *hostdata; /* available to low-level driver */
289 char type;
290 char scsi_level;
291 unsigned writeable:1;
292 unsigned removable:1;
293 unsigned random:1;
294 unsigned changed:1; /* Data invalid due to media change */
295 unsigned busy:1; /* Used to prevent races */
296 unsigned lockable:1; /* Able to prevent media removal */
297 unsigned borken:1; /* Tell the Seagate driver to be
298 painfully slow on this device */
299 unsigned tagged_supported:1; /* Supports SCSI-II tagged queuing */
300 unsigned tagged_queue:1; /*SCSI-II tagged queuing enabled */
301 unsigned disconnect:1; /* can disconnect */
302 unsigned soft_reset:1; /* Uses soft reset option */
303 unsigned char current_tag; /* current tag */
304 unsigned sync:1; /* Negotiate for sync transfers */
305 unsigned char sync_min_period; /* Not less than this period */
306 unsigned char sync_max_offset; /* Not greater than this offset */
307 } Scsi_Device;
308 /*
309 Use these to separate status msg and our bytes
310 */
311
312 #define status_byte(result) (((result) >> 1) & 0xf)
313 #define msg_byte(result) (((result) >> 8) & 0xff)
314 #define host_byte(result) (((result) >> 16) & 0xff)
315 #define driver_byte(result) (((result) >> 24) & 0xff)
316 #define suggestion(result) (driver_byte(result) & SUGGEST_MASK)
317
318 #define sense_class(sense) (((sense) >> 4) & 0x7)
319 #define sense_error(sense) ((sense) & 0xf)
320 #define sense_valid(sense) ((sense) & 0x80);
321
322 /*
323 These are the SCSI devices available on the system.
324 */
325
326 extern Scsi_Device * scsi_devices;
327 /*
328 Initializes all SCSI devices. This scans all scsi busses.
329 */
330
331 extern unsigned long scsi_dev_init (unsigned long, unsigned long);
332
333 struct scatterlist {
334 char * address; /* Location data is to be transferred to */
335 char * alt_address; /* Location of actual if address is a
336 dma indirect buffer. NULL otherwise */
337 unsigned int length;
338 };
339
340 #define ISA_DMA_THRESHOLD (0x00ffffff)
341 #define CONTIGUOUS_BUFFERS(X,Y) ((X->b_data+X->b_size) == Y->b_data)
342
343
344 /*
345 * These are the return codes for the abort and reset functions. The mid-level
346 * code uses these to decide what to do next. Each of the low level abort
347 * and reset functions must correctly indicate what it has done.
348 */
349
350 /* We did not do anything. Wait
351 some more for this command to complete, and if this does not work, try
352 something more serious. */
353 #define SCSI_ABORT_SNOOZE 0
354
355 /* This means that we were able to abort the command. We have already
356 called the mid-level done function, and do not expect an interrupt that will
357 lead to another call to the mid-level done function for this command */
358 #define SCSI_ABORT_SUCCESS 1
359
360 /* We called for an abort of this command, and we should get an interrupt
361 when this succeeds. Thus we should not restore the timer for this
362 command in the mid-level abort function. */
363 #define SCSI_ABORT_PENDING 2
364
365 /* Unable to abort - command is currently on the bus. Grin and bear it. */
366 #define SCSI_ABORT_BUSY 3
367
368 /* The command is not active in the low level code. Command probably
369 finished. */
370 #define SCSI_ABORT_NOT_RUNNING 4
371
372 /* Something went wrong. The low level driver will indicate the correct
373 error condition when it calls scsi_done, so the mid-level abort function
374 can simply wait until this comes through */
375 #define SCSI_ABORT_ERROR 5
376
377 /* We do not know how to reset the bus, or we do not want to. Bummer.
378 Anyway, just wait a little more for the command in question, and hope that
379 it eventually finishes. If it never finishes, the SCSI device could
380 hang, so use this with caution. */
381 #define SCSI_RESET_SNOOZE 0
382
383 /* We do not know how to reset the bus, or we do not want to. Bummer.
384 We have given up on this ever completing. The mid-level code will
385 request sense information to decide how to proceed from here. */
386 #define SCSI_RESET_PUNT 1
387
388 /* This means that we were able to reset the bus. We have restarted all of
389 the commands that should be restarted, and we should be able to continue
390 on normally from here. We do not expect any interrupts that will return
391 DID_RESET to any of the other commands in the host_queue, and the mid-level
392 code does not need to do anything special to keep the commands alive. */
393 #define SCSI_RESET_SUCCESS 2
394
395 /* We called for a reset of this bus, and we should get an interrupt
396 when this succeeds. Each command should get its own status
397 passed up to scsi_done, but this has not happened yet. */
398 #define SCSI_RESET_PENDING 3
399
400 /* We did a reset, but do not expect an interrupt to signal DID_RESET.
401 This tells the upper level code to request the sense info, and this
402 should keep the command alive. */
403 #define SCSI_RESET_WAKEUP 4
404
405 /* Something went wrong, and we do not know how to fix it. */
406 #define SCSI_RESET_ERROR 5
407
408 void * scsi_malloc(unsigned int);
409 int scsi_free(void *, unsigned int);
410 extern unsigned int dma_free_sectors; /* How much room do we have left */
411 extern unsigned int need_isa_buffer; /* True if some devices need indirection
412 buffers */
413
414 /*
415 The Scsi_Cmnd structure is used by scsi.c internally, and for communication with
416 low level drivers that support multiple outstanding commands.
417 */
418 typedef struct scsi_pointer {
419 char * ptr; /* data pointer */
420 int this_residual; /* left in this buffer */
421 struct scatterlist *buffer; /* which buffer */
422 int buffers_residual; /* how many buffers left */
423
424 volatile int Status;
425 volatile int Message;
426 volatile int have_data_in;
427 volatile int sent_command;
428 volatile int phase;
429 } Scsi_Pointer;
430
431 typedef struct scsi_cmnd {
432 struct Scsi_Host * host;
433 Scsi_Device * device;
434 unsigned char target, lun;
435 unsigned char cmd_len;
436 unsigned char old_cmd_len;
437 struct scsi_cmnd *next, *prev;
438
439 /* These elements define the operation we are about to perform */
440 unsigned char cmnd[12];
441 unsigned request_bufflen; /* Actual request size */
442
443 void * request_buffer; /* Actual requested buffer */
444
445 /* These elements define the operation we ultimately want to perform */
446 unsigned char data_cmnd[12];
447 unsigned short old_use_sg; /* We save use_sg here when requesting
448 sense info */
449 unsigned short use_sg; /* Number of pieces of scatter-gather */
450 unsigned short sglist_len; /* size of malloc'd scatter-gather list */
451 unsigned short abort_reason; /* If the mid-level code requests an
452 abort, this is the reason. */
453 unsigned bufflen; /* Size of data buffer */
454 void *buffer; /* Data buffer */
455
456 unsigned underflow; /* Return error if less than this amount is
457 transfered */
458
459 unsigned transfersize; /* How much we are guaranteed to transfer with
460 each SCSI transfer (ie, between disconnect /
461 reconnects. Probably == sector size */
462
463
464
465 struct request request; /* A copy of the command we are working on*/
466
467 unsigned char sense_buffer[16]; /* Sense for this command, if needed*/
468
469
470 int retries;
471 int allowed;
472 int timeout_per_command, timeout_total, timeout;
473 /*
474 * We handle the timeout differently if it happens when a reset,
475 * abort, etc are in process.
476 */
477
478 unsigned volatile char internal_timeout;
479
480 unsigned flags;
481
482 /* These variables are for the cdrom only. Once we have variable size buffers
483 in the buffer cache, they will go away. */
484 int this_count;
485 /* End of special cdrom variables */
486
487 /* Low-level done function - can be used by low-level driver to point
488 to completion function. Not used by mid/upper level code. */
489 void (*scsi_done)(struct scsi_cmnd *);
490 void (*done)(struct scsi_cmnd *); /* Mid-level done function */
491
492 /* The following fields can be written to by the host specific code.
493 Everything else should be left alone. */
494
495 Scsi_Pointer SCp; /* Scratchpad used by some host adapters */
496
497 unsigned char * host_scribble; /* The host adapter is allowed to
498 call scsi_malloc and get some memory
499 and hang it here. The host adapter
500 is also expected to call scsi_free
501 to release this memory. (The memory
502 obtained by scsi_malloc is guaranteed
503 to be at an address < 16Mb). */
504
505 int result; /* Status code from lower level driver */
506
507 unsigned char tag; /* SCSI-II queued command tag */
508 unsigned long pid; /* Process ID, starts at 0 */
509 } Scsi_Cmnd;
510
511 /*
512 scsi_abort aborts the current command that is executing on host host.
513 The error code, if non zero is returned in the host byte, otherwise
514 DID_ABORT is returned in the hostbyte.
515 */
516
517 extern int scsi_abort (Scsi_Cmnd *, int code, int pid);
518
519 extern void scsi_do_cmd (Scsi_Cmnd *, const void *cmnd ,
520 void *buffer, unsigned bufflen, void (*done)(struct scsi_cmnd *),
521 int timeout, int retries);
522
523
524 extern Scsi_Cmnd * allocate_device(struct request **, Scsi_Device *, int);
525
526 extern Scsi_Cmnd * request_queueable(struct request *, Scsi_Device *);
527 extern int scsi_reset (Scsi_Cmnd *);
528
529 extern int max_scsi_hosts;
530
531 #if defined(MAJOR_NR) && (MAJOR_NR != SCSI_TAPE_MAJOR)
532 #include "hosts.h"
533 static Scsi_Cmnd * end_scsi_request(Scsi_Cmnd * SCpnt, int uptodate, int sectors)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
534 {
535 struct request * req;
536 struct buffer_head * bh;
537
538 req = &SCpnt->request;
539 req->errors = 0;
540 if (!uptodate) {
541 printk(DEVICE_NAME " I/O error: dev %04x, sector %lu\n",
542 req->dev,req->sector);
543 }
544
545 do {
546 if ((bh = req->bh) != NULL) {
547 req->bh = bh->b_reqnext;
548 req->nr_sectors -= bh->b_size >> 9;
549 req->sector += bh->b_size >> 9;
550 bh->b_reqnext = NULL;
551 bh->b_uptodate = uptodate;
552 unlock_buffer(bh);
553 sectors -= bh->b_size >> 9;
554 if ((bh = req->bh) != NULL) {
555 req->current_nr_sectors = bh->b_size >> 9;
556 if (req->nr_sectors < req->current_nr_sectors) {
557 req->nr_sectors = req->current_nr_sectors;
558 printk("end_scsi_request: buffer-list destroyed\n");
559 }
560 }
561 }
562 } while(sectors && bh);
563 if (req->bh){
564 req->buffer = bh->b_data;
565 return SCpnt;
566 };
567 DEVICE_OFF(req->dev);
568 if (req->sem != NULL) {
569 up(req->sem);
570 }
571
572 if (SCpnt->host->block) {
573 struct Scsi_Host * next;
574
575 for (next = SCpnt->host->block; next != SCpnt->host;
576 next = next->block)
577 wake_up(&next->host_wait);
578 }
579
580 req->dev = -1;
581 wake_up(&wait_for_request);
582 wake_up(&SCpnt->device->device_wait);
583 return NULL;
584 }
585
586
587 /* This is just like INIT_REQUEST, but we need to be aware of the fact
588 that an interrupt may start another request, so we run this with interrupts
589 turned off */
590
591 #define INIT_SCSI_REQUEST \
592 if (!CURRENT) {\
593 CLEAR_INTR; \
594 restore_flags(flags); \
595 return; \
596 } \
597 if (MAJOR(CURRENT->dev) != MAJOR_NR) \
598 panic(DEVICE_NAME ": request list destroyed"); \
599 if (CURRENT->bh) { \
600 if (!CURRENT->bh->b_lock) \
601 panic(DEVICE_NAME ": block not locked"); \
602 }
603 #endif
604
605 #define SCSI_SLEEP(QUEUE, CONDITION) { \
606 if (CONDITION) { \
607 struct wait_queue wait = { current, NULL}; \
608 add_wait_queue(QUEUE, &wait); \
609 for(;;) { \
610 current->state = TASK_UNINTERRUPTIBLE; \
611 if (CONDITION) { \
612 if (intr_count) \
613 panic("scsi: trying to call schedule() in interrupt" \
614 ", file %s, line %d.\n", __FILE__, __LINE__); \
615 schedule(); \
616 } \
617 else \
618 break; \
619 } \
620 remove_wait_queue(QUEUE, &wait); \
621 current->state = TASK_RUNNING; \
622 }; }
623
624 #endif
625
626 /*
627 * Overrides for Emacs so that we follow Linus's tabbing style.
628 * Emacs will notice this stuff at the end of the file and automatically
629 * adjust the settings for this buffer only. This must remain at the end
630 * of the file.
631 * ---------------------------------------------------------------------------
632 * Local variables:
633 * c-indent-level: 8
634 * c-brace-imaginary-offset: 0
635 * c-brace-offset: -8
636 * c-argdecl-indent: 8
637 * c-label-offset: -8
638 * c-continued-statement-offset: 8
639 * c-continued-brace-offset: 0
640 * End:
641 */