make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / drivers / scsi / scsi.h
1 /*
2  *  scsi.h Copyright (C) 1992 Drew Eckhardt 
3  *         Copyright (C) 1993, 1994, 1995, 1998, 1999 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@andante.org to
11  *       add scatter-gather, multiple outstanding request, and other
12  *       enhancements.
13  */
14
15 #ifndef _SCSI_H
16 #define _SCSI_H
17
18 #include <linux/config.h>       /* for CONFIG_SCSI_LOGGING */
19 #include <linux/devfs_fs_kernel.h>
20 #include <linux/proc_fs.h>
21
22 /*
23  * Some of the public constants are being moved to this file.
24  * We include it here so that what came from where is transparent.
25  */
26 #include <scsi/scsi.h>
27
28 #include <linux/random.h>
29
30 #include <asm/hardirq.h>
31 #include <asm/scatterlist.h>
32 #include <asm/io.h>
33
34 /*
35  * These are the values that the SCpnt->sc_data_direction and 
36  * SRpnt->sr_data_direction can take.  These need to be set
37  * The SCSI_DATA_UNKNOWN value is essentially the default.
38  * In the event that the command creator didn't bother to
39  * set a value, you will see SCSI_DATA_UNKNOWN.
40  */
41 #define SCSI_DATA_UNKNOWN       0
42 #define SCSI_DATA_WRITE         1
43 #define SCSI_DATA_READ          2
44 #define SCSI_DATA_NONE          3
45
46 #ifdef CONFIG_PCI
47 #include <linux/pci.h>
48 #if ((SCSI_DATA_UNKNOWN == PCI_DMA_BIDIRECTIONAL) && (SCSI_DATA_WRITE == PCI_DMA_TODEVICE) && (SCSI_DATA_READ == PCI_DMA_FROMDEVICE) && (SCSI_DATA_NONE == PCI_DMA_NONE))
49 #define scsi_to_pci_dma_dir(scsi_dir)   ((int)(scsi_dir))
50 #else
51 static inline int scsi_to_pci_dma_dir(unsigned char scsi_dir)
52 {
53         if (scsi_dir == SCSI_DATA_UNKNOWN)
54                 return PCI_DMA_BIDIRECTIONAL;
55         if (scsi_dir == SCSI_DATA_WRITE)
56                 return PCI_DMA_TODEVICE;
57         if (scsi_dir == SCSI_DATA_READ)
58                 return PCI_DMA_FROMDEVICE;
59         return PCI_DMA_NONE;
60 }
61 #endif
62 #endif
63
64 #if defined(CONFIG_SBUS) && !defined(CONFIG_SUN3) && !defined(CONFIG_SUN3X)
65 #include <asm/sbus.h>
66 #if ((SCSI_DATA_UNKNOWN == SBUS_DMA_BIDIRECTIONAL) && (SCSI_DATA_WRITE == SBUS_DMA_TODEVICE) && (SCSI_DATA_READ == SBUS_DMA_FROMDEVICE) && (SCSI_DATA_NONE == SBUS_DMA_NONE))
67 #define scsi_to_sbus_dma_dir(scsi_dir)  ((int)(scsi_dir))
68 #else
69 static inline int scsi_to_sbus_dma_dir(unsigned char scsi_dir)
70 {
71         if (scsi_dir == SCSI_DATA_UNKNOWN)
72                 return SBUS_DMA_BIDIRECTIONAL;
73         if (scsi_dir == SCSI_DATA_WRITE)
74                 return SBUS_DMA_TODEVICE;
75         if (scsi_dir == SCSI_DATA_READ)
76                 return SBUS_DMA_FROMDEVICE;
77         return SBUS_DMA_NONE;
78 }
79 #endif
80 #endif
81
82 /*
83  * Some defs, in case these are not defined elsewhere.
84  */
85 #ifndef TRUE
86 #define TRUE 1
87 #endif
88 #ifndef FALSE
89 #define FALSE 0
90 #endif
91
92 #define MAX_SCSI_DEVICE_CODE 14
93 extern const char *const scsi_device_types[MAX_SCSI_DEVICE_CODE];
94
95 #ifdef DEBUG
96 #define SCSI_TIMEOUT (5*HZ)
97 #else
98 #define SCSI_TIMEOUT (2*HZ)
99 #endif
100
101 /*
102  * Used for debugging the new queueing code.  We want to make sure
103  * that the lock state is consistent with design.  Only do this in
104  * the user space simulator.
105  */
106 #define ASSERT_LOCK(_LOCK, _COUNT)
107
108 #if defined(CONFIG_SMP) && defined(CONFIG_USER_DEBUG)
109 #undef ASSERT_LOCK
110 #define ASSERT_LOCK(_LOCK,_COUNT)       \
111         { if( (_LOCK)->lock != _COUNT )   \
112                 panic("Lock count inconsistent %s %d\n", __FILE__, __LINE__); \
113                                                                                        }
114 #endif
115
116 /*
117  *  Use these to separate status msg and our bytes
118  *
119  *  These are set by:
120  *
121  *      status byte = set from target device
122  *      msg_byte    = return status from host adapter itself.
123  *      host_byte   = set by low-level driver to indicate status.
124  *      driver_byte = set by mid-level.
125  */
126 #define status_byte(result) (((result) >> 1) & 0x1f)
127 #define msg_byte(result)    (((result) >> 8) & 0xff)
128 #define host_byte(result)   (((result) >> 16) & 0xff)
129 #define driver_byte(result) (((result) >> 24) & 0xff)
130 #define suggestion(result)  (driver_byte(result) & SUGGEST_MASK)
131
132 #define sense_class(sense)  (((sense) >> 4) & 0x7)
133 #define sense_error(sense)  ((sense) & 0xf)
134 #define sense_valid(sense)  ((sense) & 0x80);
135
136 #define NEEDS_RETRY     0x2001
137 #define SUCCESS         0x2002
138 #define FAILED          0x2003
139 #define QUEUED          0x2004
140 #define SOFT_ERROR      0x2005
141 #define ADD_TO_MLQUEUE  0x2006
142
143 /*
144  * These are the values that scsi_cmd->state can take.
145  */
146 #define SCSI_STATE_TIMEOUT         0x1000
147 #define SCSI_STATE_FINISHED        0x1001
148 #define SCSI_STATE_FAILED          0x1002
149 #define SCSI_STATE_QUEUED          0x1003
150 #define SCSI_STATE_UNUSED          0x1006
151 #define SCSI_STATE_DISCONNECTING   0x1008
152 #define SCSI_STATE_INITIALIZING    0x1009
153 #define SCSI_STATE_BHQUEUE         0x100a
154 #define SCSI_STATE_MLQUEUE         0x100b
155
156 /*
157  * These are the values that the owner field can take.
158  * They are used as an indication of who the command belongs to.
159  */
160 #define SCSI_OWNER_HIGHLEVEL      0x100
161 #define SCSI_OWNER_MIDLEVEL       0x101
162 #define SCSI_OWNER_LOWLEVEL       0x102
163 #define SCSI_OWNER_ERROR_HANDLER  0x103
164 #define SCSI_OWNER_BH_HANDLER     0x104
165 #define SCSI_OWNER_NOBODY         0x105
166
167 #define COMMAND_SIZE(opcode) scsi_command_size[((opcode) >> 5) & 7]
168
169 #define IDENTIFY_BASE       0x80
170 #define IDENTIFY(can_disconnect, lun)   (IDENTIFY_BASE |\
171                      ((can_disconnect) ?  0x40 : 0) |\
172                      ((lun) & 0x07))
173
174
175 /*
176  * This defines the scsi logging feature.  It is a means by which the
177  * user can select how much information they get about various goings on,
178  * and it can be really useful for fault tracing.  The logging word is divided
179  * into 8 nibbles, each of which describes a loglevel.  The division of things
180  * is somewhat arbitrary, and the division of the word could be changed if it
181  * were really needed for any reason.  The numbers below are the only place where these
182  * are specified.  For a first go-around, 3 bits is more than enough, since this
183  * gives 8 levels of logging (really 7, since 0 is always off).  Cutting to 2 bits
184  * might be wise at some point.
185  */
186
187 #define SCSI_LOG_ERROR_SHIFT              0
188 #define SCSI_LOG_TIMEOUT_SHIFT            3
189 #define SCSI_LOG_SCAN_SHIFT               6
190 #define SCSI_LOG_MLQUEUE_SHIFT            9
191 #define SCSI_LOG_MLCOMPLETE_SHIFT         12
192 #define SCSI_LOG_LLQUEUE_SHIFT            15
193 #define SCSI_LOG_LLCOMPLETE_SHIFT         18
194 #define SCSI_LOG_HLQUEUE_SHIFT            21
195 #define SCSI_LOG_HLCOMPLETE_SHIFT         24
196 #define SCSI_LOG_IOCTL_SHIFT              27
197
198 #define SCSI_LOG_ERROR_BITS               3
199 #define SCSI_LOG_TIMEOUT_BITS             3
200 #define SCSI_LOG_SCAN_BITS                3
201 #define SCSI_LOG_MLQUEUE_BITS             3
202 #define SCSI_LOG_MLCOMPLETE_BITS          3
203 #define SCSI_LOG_LLQUEUE_BITS             3
204 #define SCSI_LOG_LLCOMPLETE_BITS          3
205 #define SCSI_LOG_HLQUEUE_BITS             3
206 #define SCSI_LOG_HLCOMPLETE_BITS          3
207 #define SCSI_LOG_IOCTL_BITS               3
208
209 #if CONFIG_SCSI_LOGGING
210
211 #define SCSI_CHECK_LOGGING(SHIFT, BITS, LEVEL, CMD)     \
212 {                                                       \
213         unsigned int mask;                              \
214                                                         \
215         mask = (1 << (BITS)) - 1;                       \
216         if( ((scsi_logging_level >> (SHIFT)) & mask) > (LEVEL) ) \
217         {                                               \
218                 (CMD);                                  \
219         }                                               \
220 }
221
222 #define SCSI_SET_LOGGING(SHIFT, BITS, LEVEL)            \
223 {                                                       \
224         unsigned int mask;                              \
225                                                         \
226         mask = ((1 << (BITS)) - 1) << SHIFT;            \
227         scsi_logging_level = ((scsi_logging_level & ~mask) \
228                               | ((LEVEL << SHIFT) & mask));     \
229 }
230
231
232
233 #else
234
235 /*
236  * With no logging enabled, stub these out so they don't do anything.
237  */
238 #define SCSI_SET_LOGGING(SHIFT, BITS, LEVEL)
239
240 #define SCSI_CHECK_LOGGING(SHIFT, BITS, LEVEL, CMD)
241 #endif
242
243 /*
244  * These are the macros that are actually used throughout the code to
245  * log events.  If logging isn't enabled, they are no-ops and will be
246  * completely absent from the user's code.
247  *
248  * The 'set' versions of the macros are really intended to only be called
249  * from the /proc filesystem, and in production kernels this will be about
250  * all that is ever used.  It could be useful in a debugging environment to
251  * bump the logging level when certain strange events are detected, however.
252  */
253 #define SCSI_LOG_ERROR_RECOVERY(LEVEL,CMD)  \
254         SCSI_CHECK_LOGGING(SCSI_LOG_ERROR_SHIFT, SCSI_LOG_ERROR_BITS, LEVEL,CMD);
255 #define SCSI_LOG_TIMEOUT(LEVEL,CMD)  \
256         SCSI_CHECK_LOGGING(SCSI_LOG_TIMEOUT_SHIFT, SCSI_LOG_TIMEOUT_BITS, LEVEL,CMD);
257 #define SCSI_LOG_SCAN_BUS(LEVEL,CMD)  \
258         SCSI_CHECK_LOGGING(SCSI_LOG_SCAN_SHIFT, SCSI_LOG_SCAN_BITS, LEVEL,CMD);
259 #define SCSI_LOG_MLQUEUE(LEVEL,CMD)  \
260         SCSI_CHECK_LOGGING(SCSI_LOG_MLQUEUE_SHIFT, SCSI_LOG_MLQUEUE_BITS, LEVEL,CMD);
261 #define SCSI_LOG_MLCOMPLETE(LEVEL,CMD)  \
262         SCSI_CHECK_LOGGING(SCSI_LOG_MLCOMPLETE_SHIFT, SCSI_LOG_MLCOMPLETE_BITS, LEVEL,CMD);
263 #define SCSI_LOG_LLQUEUE(LEVEL,CMD)  \
264         SCSI_CHECK_LOGGING(SCSI_LOG_LLQUEUE_SHIFT, SCSI_LOG_LLQUEUE_BITS, LEVEL,CMD);
265 #define SCSI_LOG_LLCOMPLETE(LEVEL,CMD)  \
266         SCSI_CHECK_LOGGING(SCSI_LOG_LLCOMPLETE_SHIFT, SCSI_LOG_LLCOMPLETE_BITS, LEVEL,CMD);
267 #define SCSI_LOG_HLQUEUE(LEVEL,CMD)  \
268         SCSI_CHECK_LOGGING(SCSI_LOG_HLQUEUE_SHIFT, SCSI_LOG_HLQUEUE_BITS, LEVEL,CMD);
269 #define SCSI_LOG_HLCOMPLETE(LEVEL,CMD)  \
270         SCSI_CHECK_LOGGING(SCSI_LOG_HLCOMPLETE_SHIFT, SCSI_LOG_HLCOMPLETE_BITS, LEVEL,CMD);
271 #define SCSI_LOG_IOCTL(LEVEL,CMD)  \
272         SCSI_CHECK_LOGGING(SCSI_LOG_IOCTL_SHIFT, SCSI_LOG_IOCTL_BITS, LEVEL,CMD);
273
274
275 #define SCSI_SET_ERROR_RECOVERY_LOGGING(LEVEL)  \
276         SCSI_SET_LOGGING(SCSI_LOG_ERROR_SHIFT, SCSI_LOG_ERROR_BITS, LEVEL);
277 #define SCSI_SET_TIMEOUT_LOGGING(LEVEL)  \
278         SCSI_SET_LOGGING(SCSI_LOG_TIMEOUT_SHIFT, SCSI_LOG_TIMEOUT_BITS, LEVEL);
279 #define SCSI_SET_SCAN_BUS_LOGGING(LEVEL)  \
280         SCSI_SET_LOGGING(SCSI_LOG_SCAN_SHIFT, SCSI_LOG_SCAN_BITS, LEVEL);
281 #define SCSI_SET_MLQUEUE_LOGGING(LEVEL)  \
282         SCSI_SET_LOGGING(SCSI_LOG_MLQUEUE_SHIFT, SCSI_LOG_MLQUEUE_BITS, LEVEL);
283 #define SCSI_SET_MLCOMPLETE_LOGGING(LEVEL)  \
284         SCSI_SET_LOGGING(SCSI_LOG_MLCOMPLETE_SHIFT, SCSI_LOG_MLCOMPLETE_BITS, LEVEL);
285 #define SCSI_SET_LLQUEUE_LOGGING(LEVEL)  \
286         SCSI_SET_LOGGING(SCSI_LOG_LLQUEUE_SHIFT, SCSI_LOG_LLQUEUE_BITS, LEVEL);
287 #define SCSI_SET_LLCOMPLETE_LOGGING(LEVEL)  \
288         SCSI_SET_LOGGING(SCSI_LOG_LLCOMPLETE_SHIFT, SCSI_LOG_LLCOMPLETE_BITS, LEVEL);
289 #define SCSI_SET_HLQUEUE_LOGGING(LEVEL)  \
290         SCSI_SET_LOGGING(SCSI_LOG_HLQUEUE_SHIFT, SCSI_LOG_HLQUEUE_BITS, LEVEL);
291 #define SCSI_SET_HLCOMPLETE_LOGGING(LEVEL)  \
292         SCSI_SET_LOGGING(SCSI_LOG_HLCOMPLETE_SHIFT, SCSI_LOG_HLCOMPLETE_BITS, LEVEL);
293 #define SCSI_SET_IOCTL_LOGGING(LEVEL)  \
294         SCSI_SET_LOGGING(SCSI_LOG_IOCTL_SHIFT, SCSI_LOG_IOCTL_BITS, LEVEL);
295
296 /*
297  *  the return of the status word will be in the following format :
298  *  The low byte is the status returned by the SCSI command, 
299  *  with vendor specific bits masked.
300  *  
301  *  The next byte is the message which followed the SCSI status.
302  *  This allows a stos to be used, since the Intel is a little
303  *  endian machine.
304  *  
305  *  The final byte is a host return code, which is one of the following.
306  *  
307  *  IE 
308  *  lsb     msb
309  *  status  msg host code   
310  *  
311  *  Our errors returned by OUR driver, NOT SCSI message.  Or'd with
312  *  SCSI message passed back to driver <IF any>.
313  */
314
315
316 #define DID_OK          0x00    /* NO error                                */
317 #define DID_NO_CONNECT  0x01    /* Couldn't connect before timeout period  */
318 #define DID_BUS_BUSY    0x02    /* BUS stayed busy through time out period */
319 #define DID_TIME_OUT    0x03    /* TIMED OUT for other reason              */
320 #define DID_BAD_TARGET  0x04    /* BAD target.                             */
321 #define DID_ABORT       0x05    /* Told to abort for some other reason     */
322 #define DID_PARITY      0x06    /* Parity error                            */
323 #define DID_ERROR       0x07    /* Internal error                          */
324 #define DID_RESET       0x08    /* Reset by somebody.                      */
325 #define DID_BAD_INTR    0x09    /* Got an interrupt we weren't expecting.  */
326 #define DID_PASSTHROUGH 0x0a    /* Force command past mid-layer            */
327 #define DID_SOFT_ERROR  0x0b    /* The low level driver just wish a retry  */
328 #define DRIVER_OK       0x00    /* Driver status                           */
329
330 /*
331  *  These indicate the error that occurred, and what is available.
332  */
333
334 #define DRIVER_BUSY         0x01
335 #define DRIVER_SOFT         0x02
336 #define DRIVER_MEDIA        0x03
337 #define DRIVER_ERROR        0x04
338
339 #define DRIVER_INVALID      0x05
340 #define DRIVER_TIMEOUT      0x06
341 #define DRIVER_HARD         0x07
342 #define DRIVER_SENSE        0x08
343
344 #define SUGGEST_RETRY       0x10
345 #define SUGGEST_ABORT       0x20
346 #define SUGGEST_REMAP       0x30
347 #define SUGGEST_DIE         0x40
348 #define SUGGEST_SENSE       0x80
349 #define SUGGEST_IS_OK       0xff
350
351 #define DRIVER_MASK         0x0f
352 #define SUGGEST_MASK        0xf0
353
354 #define MAX_COMMAND_SIZE    16
355 #define SCSI_SENSE_BUFFERSIZE   64
356
357 /*
358  *  SCSI command sets
359  */
360
361 #define SCSI_UNKNOWN    0
362 #define SCSI_1          1
363 #define SCSI_1_CCS      2
364 #define SCSI_2          3
365 #define SCSI_3          4
366
367 /*
368  *  Every SCSI command starts with a one byte OP-code.
369  *  The next byte's high three bits are the LUN of the
370  *  device.  Any multi-byte quantities are stored high byte
371  *  first, and may have a 5 bit MSB in the same byte
372  *  as the LUN.
373  */
374
375 /*
376  *  As the scsi do command functions are intelligent, and may need to
377  *  redo a command, we need to keep track of the last command
378  *  executed on each one.
379  */
380
381 #define WAS_RESET       0x01
382 #define WAS_TIMEDOUT    0x02
383 #define WAS_SENSE       0x04
384 #define IS_RESETTING    0x08
385 #define IS_ABORTING     0x10
386 #define ASKED_FOR_SENSE 0x20
387 #define SYNC_RESET      0x40
388
389 /*
390  * This is the crap from the old error handling code.  We have it in a special
391  * place so that we can more easily delete it later on.
392  */
393 #include "scsi_obsolete.h"
394
395 /*
396  * Add some typedefs so that we can prototyope a bunch of the functions.
397  */
398 typedef struct scsi_device Scsi_Device;
399 typedef struct scsi_cmnd Scsi_Cmnd;
400 typedef struct scsi_request Scsi_Request;
401
402 #define SCSI_CMND_MAGIC 0xE25C23A5
403 #define SCSI_REQ_MAGIC  0x75F6D354
404
405 /*
406  * Here is where we prototype most of the mid-layer.
407  */
408
409 /*
410  *  Initializes all SCSI devices.  This scans all scsi busses.
411  */
412
413 extern unsigned int scsi_logging_level;         /* What do we log? */
414 extern unsigned int scsi_dma_free_sectors;      /* How much room do we have left */
415 extern unsigned int scsi_need_isa_buffer;       /* True if some devices need indirection
416                                                    * buffers */
417 extern volatile int in_scan_scsis;
418 extern const unsigned char scsi_command_size[8];
419
420
421 /*
422  * These are the error handling functions defined in scsi_error.c
423  */
424 extern void scsi_times_out(Scsi_Cmnd * SCpnt);
425 extern void scsi_add_timer(Scsi_Cmnd * SCset, int timeout,
426                            void (*complete) (Scsi_Cmnd *));
427 extern int scsi_delete_timer(Scsi_Cmnd * SCset);
428 extern void scsi_error_handler(void *host);
429 extern int scsi_sense_valid(Scsi_Cmnd *);
430 extern int scsi_decide_disposition(Scsi_Cmnd * SCpnt);
431 extern int scsi_block_when_processing_errors(Scsi_Device *);
432 extern void scsi_sleep(int);
433
434 /*
435  * Prototypes for functions in scsicam.c
436  */
437 extern int  scsi_partsize(struct buffer_head *bh, unsigned long capacity,
438                     unsigned int *cyls, unsigned int *hds,
439                     unsigned int *secs);
440
441 /*
442  * Prototypes for functions in scsi_dma.c
443  */
444 void scsi_resize_dma_pool(void);
445 int scsi_init_minimal_dma_pool(void);
446 void *scsi_malloc(unsigned int);
447 int scsi_free(void *, unsigned int);
448
449 /*
450  * Prototypes for functions in scsi_merge.c
451  */
452 extern void recount_segments(Scsi_Cmnd * SCpnt);
453 extern void initialize_merge_fn(Scsi_Device * SDpnt);
454
455 /*
456  * Prototypes for functions in scsi_queue.c
457  */
458 extern int scsi_mlqueue_insert(Scsi_Cmnd * cmd, int reason);
459
460 /*
461  * Prototypes for functions in scsi_lib.c
462  */
463 extern int scsi_maybe_unblock_host(Scsi_Device * SDpnt);
464 extern Scsi_Cmnd *scsi_end_request(Scsi_Cmnd * SCpnt, int uptodate,
465                                    int sectors);
466 extern struct Scsi_Device_Template *scsi_get_request_dev(struct request *);
467 extern int scsi_init_cmd_errh(Scsi_Cmnd * SCpnt);
468 extern int scsi_insert_special_cmd(Scsi_Cmnd * SCpnt, int);
469 extern void scsi_io_completion(Scsi_Cmnd * SCpnt, int good_sectors,
470                                int block_sectors);
471 extern void scsi_queue_next_request(request_queue_t * q, Scsi_Cmnd * SCpnt);
472 extern void scsi_request_fn(request_queue_t * q);
473 extern int scsi_starvation_completion(Scsi_Device * SDpnt);
474
475 /*
476  * Prototypes for functions in scsi.c
477  */
478 extern int scsi_dispatch_cmd(Scsi_Cmnd * SCpnt);
479 extern void scsi_bottom_half_handler(void);
480 extern void scsi_release_commandblocks(Scsi_Device * SDpnt);
481 extern void scsi_build_commandblocks(Scsi_Device * SDpnt);
482 extern void scsi_done(Scsi_Cmnd * SCpnt);
483 extern void scsi_finish_command(Scsi_Cmnd *);
484 extern int scsi_retry_command(Scsi_Cmnd *);
485 extern Scsi_Cmnd *scsi_allocate_device(Scsi_Device *, int, int);
486 extern void __scsi_release_command(Scsi_Cmnd *);
487 extern void scsi_release_command(Scsi_Cmnd *);
488 extern void scsi_do_cmd(Scsi_Cmnd *, const void *cmnd,
489                         void *buffer, unsigned bufflen,
490                         void (*done) (struct scsi_cmnd *),
491                         int timeout, int retries);
492 extern int scsi_dev_init(void);
493
494 /*
495  * Newer request-based interfaces.
496  */
497 extern Scsi_Request *scsi_allocate_request(Scsi_Device *);
498 extern void scsi_release_request(Scsi_Request *);
499 extern void scsi_wait_req(Scsi_Request *, const void *cmnd,
500                           void *buffer, unsigned bufflen,
501                           int timeout, int retries);
502
503 extern void scsi_do_req(Scsi_Request *, const void *cmnd,
504                         void *buffer, unsigned bufflen,
505                         void (*done) (struct scsi_cmnd *),
506                         int timeout, int retries);
507 extern int scsi_insert_special_req(Scsi_Request * SRpnt, int);
508 extern void scsi_init_cmd_from_req(Scsi_Cmnd *, Scsi_Request *);
509
510
511 /*
512  * Prototypes for functions/data in hosts.c
513  */
514 extern int max_scsi_hosts;
515
516 /*
517  * Prototypes for functions in scsi_proc.c
518  */
519 extern void proc_print_scsidevice(Scsi_Device *, char *, int *, int);
520 extern struct proc_dir_entry *proc_scsi;
521
522 /*
523  * Prototypes for functions in constants.c
524  */
525 extern void print_command(unsigned char *);
526 extern void print_sense(const char *, Scsi_Cmnd *);
527 extern void print_req_sense(const char *, Scsi_Request *);
528 extern void print_driverbyte(int scsiresult);
529 extern void print_hostbyte(int scsiresult);
530 extern void print_status (int status);
531
532 /*
533  *  The scsi_device struct contains what we know about each given scsi
534  *  device.
535  *
536  * FIXME(eric) - one of the great regrets that I have is that I failed to define
537  * these structure elements as something like sdev_foo instead of foo.  This would
538  * make it so much easier to grep through sources and so forth.  I propose that
539  * all new elements that get added to these structures follow this convention.
540  * As time goes on and as people have the stomach for it, it should be possible to 
541  * go back and retrofit at least some of the elements here with with the prefix.
542  */
543
544 struct scsi_device {
545 /* private: */
546         /*
547          * This information is private to the scsi mid-layer.  Wrapping it in a
548          * struct private is a way of marking it in a sort of C++ type of way.
549          */
550         struct scsi_device *next;       /* Used for linked list */
551         struct scsi_device *prev;       /* Used for linked list */
552         wait_queue_head_t   scpnt_wait; /* Used to wait if
553                                            device is busy */
554         struct Scsi_Host *host;
555         request_queue_t request_queue;
556         atomic_t                device_active; /* commands checked out for device */
557         volatile unsigned short device_busy;    /* commands actually active on low-level */
558         int (*scsi_init_io_fn) (Scsi_Cmnd *);   /* Used to initialize
559                                                    new request */
560         Scsi_Cmnd *device_queue;        /* queue of SCSI Command structures */
561
562 /* public: */
563         unsigned int id, lun, channel;
564
565         unsigned int manufacturer;      /* Manufacturer of device, for using 
566                                          * vendor-specific cmd's */
567         unsigned sector_size;   /* size in bytes */
568
569         int attached;           /* # of high level drivers attached to this */
570         int detected;           /* Delta attached - don't use in drivers! */
571         int access_count;       /* Count of open channels/mounts */
572
573         void *hostdata;         /* available to low-level driver */
574         devfs_handle_t de;      /* directory for the device      */
575         char type;
576         char scsi_level;
577         char vendor[8], model[16], rev[4];
578         unsigned char current_tag;      /* current tag */
579         unsigned char sync_min_period;  /* Not less than this period */
580         unsigned char sync_max_offset;  /* Not greater than this offset */
581         unsigned char queue_depth;      /* How deep a queue to use */
582
583         unsigned online:1;
584         unsigned writeable:1;
585         unsigned removable:1;
586         unsigned random:1;
587         unsigned has_cmdblocks:1;
588         unsigned changed:1;     /* Data invalid due to media change */
589         unsigned busy:1;        /* Used to prevent races */
590         unsigned lockable:1;    /* Able to prevent media removal */
591         unsigned borken:1;      /* Tell the Seagate driver to be 
592                                  * painfully slow on this device */
593         unsigned tagged_supported:1;    /* Supports SCSI-II tagged queuing */
594         unsigned tagged_queue:1;        /* SCSI-II tagged queuing enabled */
595         unsigned disconnect:1;  /* can disconnect */
596         unsigned soft_reset:1;  /* Uses soft reset option */
597         unsigned sync:1;        /* Negotiate for sync transfers */
598         unsigned wide:1;        /* Negotiate for WIDE transfers */
599         unsigned single_lun:1;  /* Indicates we should only allow I/O to
600                                  * one of the luns for the device at a 
601                                  * time. */
602         unsigned was_reset:1;   /* There was a bus reset on the bus for 
603                                  * this device */
604         unsigned expecting_cc_ua:1;     /* Expecting a CHECK_CONDITION/UNIT_ATTN
605                                          * because we did a bus reset. */
606         unsigned device_blocked:1;      /* Device returned QUEUE_FULL. */
607         unsigned ten:1;         /* support ten byte read / write */
608         unsigned remap:1;       /* support remapping  */
609         unsigned starved:1;     /* unable to process commands because
610                                    host busy */
611         unsigned no_start_on_add:1;     /* do not issue start on add,+Wilson04012004,from 2.4.24*/
612         // Flag to allow revalidate to succeed in sd_open
613         int allow_revalidate;
614 };
615
616
617 /*
618  * The Scsi_Cmnd structure is used by scsi.c internally, and for communication
619  * with low level drivers that support multiple outstanding commands.
620  */
621 typedef struct scsi_pointer {
622         char *ptr;              /* data pointer */
623         int this_residual;      /* left in this buffer */
624         struct scatterlist *buffer;     /* which buffer */
625         int buffers_residual;   /* how many buffers left */
626
627         dma_addr_t dma_handle;
628
629         volatile int Status;
630         volatile int Message;
631         volatile int have_data_in;
632         volatile int sent_command;
633         volatile int phase;
634 } Scsi_Pointer;
635
636 /*
637  * This is essentially a slimmed down version of Scsi_Cmnd.  The point of
638  * having this is that requests that are injected into the queue as result
639  * of things like ioctls and character devices shouldn't be using a
640  * Scsi_Cmnd until such a time that the command is actually at the head
641  * of the queue and being sent to the driver.
642  */
643 struct scsi_request {
644         int     sr_magic;
645         int     sr_result;      /* Status code from lower level driver */
646         unsigned char sr_sense_buffer[SCSI_SENSE_BUFFERSIZE];           /* obtained by REQUEST SENSE
647                                                  * when CHECK CONDITION is
648                                                  * received on original command 
649                                                  * (auto-sense) */
650
651         struct Scsi_Host *sr_host;
652         Scsi_Device *sr_device;
653         Scsi_Cmnd *sr_command;
654         struct request sr_request;      /* A copy of the command we are
655                                    working on */
656         unsigned sr_bufflen;    /* Size of data buffer */
657         void *sr_buffer;                /* Data buffer */
658         int sr_allowed;
659         unsigned char sr_data_direction;
660         unsigned char sr_cmd_len;
661         unsigned char sr_cmnd[MAX_COMMAND_SIZE];
662         void (*sr_done) (struct scsi_cmnd *);   /* Mid-level done function */
663         int sr_timeout_per_command;
664         unsigned short sr_use_sg;       /* Number of pieces of scatter-gather */
665         unsigned short sr_sglist_len;   /* size of malloc'd scatter-gather list */
666         unsigned sr_underflow;  /* Return error if less than
667                                    this amount is transferred */
668 };
669
670 /*
671  * FIXME(eric) - one of the great regrets that I have is that I failed to define
672  * these structure elements as something like sc_foo instead of foo.  This would
673  * make it so much easier to grep through sources and so forth.  I propose that
674  * all new elements that get added to these structures follow this convention.
675  * As time goes on and as people have the stomach for it, it should be possible to 
676  * go back and retrofit at least some of the elements here with with the prefix.
677  */
678 struct scsi_cmnd {
679         int     sc_magic;
680 /* private: */
681         /*
682          * This information is private to the scsi mid-layer.  Wrapping it in a
683          * struct private is a way of marking it in a sort of C++ type of way.
684          */
685         struct Scsi_Host *host;
686         unsigned short state;
687         unsigned short owner;
688         Scsi_Device *device;
689         Scsi_Request *sc_request;
690         struct scsi_cmnd *next;
691         struct scsi_cmnd *reset_chain;
692
693         int eh_state;           /* Used for state tracking in error handlr */
694         void (*done) (struct scsi_cmnd *);      /* Mid-level done function */
695         /*
696            A SCSI Command is assigned a nonzero serial_number when internal_cmnd
697            passes it to the driver's queue command function.  The serial_number
698            is cleared when scsi_done is entered indicating that the command has
699            been completed.  If a timeout occurs, the serial number at the moment
700            of timeout is copied into serial_number_at_timeout.  By subsequently
701            comparing the serial_number and serial_number_at_timeout fields
702            during abort or reset processing, we can detect whether the command
703            has already completed.  This also detects cases where the command has
704            completed and the SCSI Command structure has already being reused
705            for another command, so that we can avoid incorrectly aborting or
706            resetting the new command.
707          */
708
709         unsigned long serial_number;
710         unsigned long serial_number_at_timeout;
711
712         int retries;
713         int allowed;
714         int timeout_per_command;
715         int timeout_total;
716         int timeout;
717
718         /*
719          * We handle the timeout differently if it happens when a reset, 
720          * abort, etc are in process. 
721          */
722         unsigned volatile char internal_timeout;
723         struct scsi_cmnd *bh_next;      /* To enumerate the commands waiting 
724                                            to be processed. */
725
726 /* public: */
727
728         unsigned int target;
729         unsigned int lun;
730         unsigned int channel;
731         unsigned char cmd_len;
732         unsigned char old_cmd_len;
733         unsigned char sc_data_direction;
734         unsigned char sc_old_data_direction;
735
736         /* These elements define the operation we are about to perform */
737         unsigned char cmnd[MAX_COMMAND_SIZE];
738         unsigned request_bufflen;       /* Actual request size */
739
740         struct timer_list eh_timeout;   /* Used to time out the command. */
741         void *request_buffer;           /* Actual requested buffer */
742         void **bounce_buffers;          /* Array of bounce buffers when using scatter-gather */
743
744         /* These elements define the operation we ultimately want to perform */
745         unsigned char data_cmnd[MAX_COMMAND_SIZE];
746         unsigned short old_use_sg;      /* We save  use_sg here when requesting
747                                          * sense info */
748         unsigned short use_sg;  /* Number of pieces of scatter-gather */
749         unsigned short sglist_len;      /* size of malloc'd scatter-gather list */
750         unsigned short abort_reason;    /* If the mid-level code requests an
751                                          * abort, this is the reason. */
752         unsigned bufflen;       /* Size of data buffer */
753         void *buffer;           /* Data buffer */
754
755         unsigned underflow;     /* Return error if less than
756                                    this amount is transferred */
757         unsigned old_underflow; /* save underflow here when reusing the
758                                  * command for error handling */
759
760         unsigned transfersize;  /* How much we are guaranteed to
761                                    transfer with each SCSI transfer
762                                    (ie, between disconnect / 
763                                    reconnects.   Probably == sector
764                                    size */
765
766         int resid;              /* Number of bytes requested to be
767                                    transferred less actual number
768                                    transferred (0 if not supported) */
769
770         struct request request; /* A copy of the command we are
771                                    working on */
772
773         unsigned char sense_buffer[SCSI_SENSE_BUFFERSIZE];              /* obtained by REQUEST SENSE
774                                                  * when CHECK CONDITION is
775                                                  * received on original command 
776                                                  * (auto-sense) */
777
778         unsigned flags;
779
780         /*
781          * Used to indicate that a command which has timed out also
782          * completed normally.  Typically the completion function will
783          * do nothing but set this flag in this instance because the
784          * timeout handler is already running.
785          */
786         unsigned done_late:1;
787
788         /* Low-level done function - can be used by low-level driver to point
789          *        to completion function.  Not used by mid/upper level code. */
790         void (*scsi_done) (struct scsi_cmnd *);
791
792         /*
793          * The following fields can be written to by the host specific code. 
794          * Everything else should be left alone. 
795          */
796
797         Scsi_Pointer SCp;       /* Scratchpad used by some host adapters */
798
799         unsigned char *host_scribble;   /* The host adapter is allowed to
800                                            * call scsi_malloc and get some memory
801                                            * and hang it here.     The host adapter
802                                            * is also expected to call scsi_free
803                                            * to release this memory.  (The memory
804                                            * obtained by scsi_malloc is guaranteed
805                                            * to be at an address < 16Mb). */
806
807         int result;             /* Status code from lower level driver */
808
809         unsigned char tag;      /* SCSI-II queued command tag */
810         unsigned long pid;      /* Process ID, starts at 0 */
811 };
812
813 /*
814  *  Flag bit for the internal_timeout array
815  */
816 #define NORMAL_TIMEOUT 0
817
818 /*
819  * Definitions and prototypes used for scsi mid-level queue.
820  */
821 #define SCSI_MLQUEUE_HOST_BUSY   0x1055
822 #define SCSI_MLQUEUE_DEVICE_BUSY 0x1056
823
824 #define SCSI_SLEEP(QUEUE, CONDITION) {              \
825     if (CONDITION) {                                \
826         DECLARE_WAITQUEUE(wait, current);           \
827         add_wait_queue(QUEUE, &wait);               \
828         for(;;) {                                   \
829         set_current_state(TASK_UNINTERRUPTIBLE);    \
830         if (CONDITION) {                            \
831             if (in_interrupt())                     \
832                 panic("scsi: trying to call schedule() in interrupt" \
833                       ", file %s, line %d.\n", __FILE__, __LINE__);  \
834             schedule();                 \
835         }                               \
836         else                            \
837             break;                      \
838         }                               \
839         remove_wait_queue(QUEUE, &wait);\
840         current->state = TASK_RUNNING;  \
841     }; }
842
843 /*
844  * old style reset request from external source
845  * (private to sg.c and scsi_error.c, supplied by scsi_obsolete.c)
846  */
847 #define SCSI_TRY_RESET_DEVICE   1
848 #define SCSI_TRY_RESET_BUS      2
849 #define SCSI_TRY_RESET_HOST     3
850
851 extern int scsi_reset_provider(Scsi_Device *, int);
852
853 #endif
854
855 /*
856  * Overrides for Emacs so that we follow Linus's tabbing style.
857  * Emacs will notice this stuff at the end of the file and automatically
858  * adjust the settings for this buffer only.  This must remain at the end
859  * of the file.
860  * ---------------------------------------------------------------------------
861  * Local variables:
862  * c-indent-level: 4 
863  * c-brace-imaginary-offset: 0
864  * c-brace-offset: -4
865  * c-argdecl-indent: 4
866  * c-label-offset: -4
867  * c-continued-statement-offset: 4
868  * c-continued-brace-offset: 0
869  * indent-tabs-mode: nil
870  * tab-width: 8
871  * End:
872  */