File: /usr/src/linux/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     extern __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)
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     extern __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    12
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     #if defined(__mc68000__) || defined(CONFIG_APUS)
390     #include <asm/pgtable.h>
391     #define CONTIGUOUS_BUFFERS(X,Y) \
392     	(virt_to_phys((X)->b_data+(X)->b_size-1)+1==virt_to_phys((Y)->b_data))
393     #else
394     #define CONTIGUOUS_BUFFERS(X,Y) ((X->b_data+X->b_size) == Y->b_data)
395     #endif
396     
397     
398     /*
399      * This is the crap from the old error handling code.  We have it in a special
400      * place so that we can more easily delete it later on.
401      */
402     #include "scsi_obsolete.h"
403     
404     /*
405      * Add some typedefs so that we can prototyope a bunch of the functions.
406      */
407     typedef struct scsi_device Scsi_Device;
408     typedef struct scsi_cmnd Scsi_Cmnd;
409     typedef struct scsi_request Scsi_Request;
410     
411     #define SCSI_CMND_MAGIC 0xE25C23A5
412     #define SCSI_REQ_MAGIC  0x75F6D354
413     
414     /*
415      * Here is where we prototype most of the mid-layer.
416      */
417     
418     /*
419      *  Initializes all SCSI devices.  This scans all scsi busses.
420      */
421     
422     extern unsigned int scsi_logging_level;		/* What do we log? */
423     extern unsigned int scsi_dma_free_sectors;	/* How much room do we have left */
424     extern unsigned int scsi_need_isa_buffer;	/* True if some devices need indirection
425     						   * buffers */
426     extern volatile int in_scan_scsis;
427     extern const unsigned char scsi_command_size[8];
428     
429     
430     /*
431      * These are the error handling functions defined in scsi_error.c
432      */
433     extern void scsi_times_out(Scsi_Cmnd * SCpnt);
434     extern void scsi_add_timer(Scsi_Cmnd * SCset, int timeout,
435     			   void (*complete) (Scsi_Cmnd *));
436     extern int scsi_delete_timer(Scsi_Cmnd * SCset);
437     extern void scsi_error_handler(void *host);
438     extern int scsi_sense_valid(Scsi_Cmnd *);
439     extern int scsi_decide_disposition(Scsi_Cmnd * SCpnt);
440     extern int scsi_block_when_processing_errors(Scsi_Device *);
441     extern void scsi_sleep(int);
442     
443     /*
444      * Prototypes for functions in scsicam.c
445      */
446     extern int  scsi_partsize(struct buffer_head *bh, unsigned long capacity,
447                         unsigned int *cyls, unsigned int *hds,
448                         unsigned int *secs);
449     
450     /*
451      * Prototypes for functions in scsi_dma.c
452      */
453     void scsi_resize_dma_pool(void);
454     int scsi_init_minimal_dma_pool(void);
455     void *scsi_malloc(unsigned int);
456     int scsi_free(void *, unsigned int);
457     
458     /*
459      * Prototypes for functions in scsi_merge.c
460      */
461     extern void recount_segments(Scsi_Cmnd * SCpnt);
462     extern void initialize_merge_fn(Scsi_Device * SDpnt);
463     
464     /*
465      * Prototypes for functions in scsi_queue.c
466      */
467     extern int scsi_mlqueue_insert(Scsi_Cmnd * cmd, int reason);
468     
469     /*
470      * Prototypes for functions in scsi_lib.c
471      */
472     extern int scsi_maybe_unblock_host(Scsi_Device * SDpnt);
473     extern Scsi_Cmnd *scsi_end_request(Scsi_Cmnd * SCpnt, int uptodate,
474     				   int sectors);
475     extern struct Scsi_Device_Template *scsi_get_request_dev(struct request *);
476     extern int scsi_init_cmd_errh(Scsi_Cmnd * SCpnt);
477     extern int scsi_insert_special_cmd(Scsi_Cmnd * SCpnt, int);
478     extern void scsi_io_completion(Scsi_Cmnd * SCpnt, int good_sectors,
479     			       int block_sectors);
480     extern void scsi_queue_next_request(request_queue_t * q, Scsi_Cmnd * SCpnt);
481     extern void scsi_request_fn(request_queue_t * q);
482     extern int scsi_starvation_completion(Scsi_Device * SDpnt);
483     
484     /*
485      * Prototypes for functions in scsi.c
486      */
487     extern int scsi_dispatch_cmd(Scsi_Cmnd * SCpnt);
488     extern void scsi_bottom_half_handler(void);
489     extern void scsi_release_commandblocks(Scsi_Device * SDpnt);
490     extern void scsi_build_commandblocks(Scsi_Device * SDpnt);
491     extern void scsi_done(Scsi_Cmnd * SCpnt);
492     extern void scsi_finish_command(Scsi_Cmnd *);
493     extern int scsi_retry_command(Scsi_Cmnd *);
494     extern Scsi_Cmnd *scsi_allocate_device(Scsi_Device *, int, int);
495     extern void __scsi_release_command(Scsi_Cmnd *);
496     extern void scsi_release_command(Scsi_Cmnd *);
497     extern void scsi_do_cmd(Scsi_Cmnd *, const void *cmnd,
498     			void *buffer, unsigned bufflen,
499     			void (*done) (struct scsi_cmnd *),
500     			int timeout, int retries);
501     extern int scsi_dev_init(void);
502     
503     /*
504      * Newer request-based interfaces.
505      */
506     extern Scsi_Request *scsi_allocate_request(Scsi_Device *);
507     extern void scsi_release_request(Scsi_Request *);
508     extern void scsi_wait_req(Scsi_Request *, const void *cmnd,
509     			  void *buffer, unsigned bufflen,
510     			  int timeout, int retries);
511     
512     extern void scsi_do_req(Scsi_Request *, const void *cmnd,
513     			void *buffer, unsigned bufflen,
514     			void (*done) (struct scsi_cmnd *),
515     			int timeout, int retries);
516     extern int scsi_insert_special_req(Scsi_Request * SRpnt, int);
517     extern void scsi_init_cmd_from_req(Scsi_Cmnd *, Scsi_Request *);
518     
519     
520     /*
521      * Prototypes for functions/data in hosts.c
522      */
523     extern int max_scsi_hosts;
524     
525     /*
526      * Prototypes for functions in scsi_proc.c
527      */
528     extern void proc_print_scsidevice(Scsi_Device *, char *, int *, int);
529     extern struct proc_dir_entry *proc_scsi;
530     
531     /*
532      * Prototypes for functions in constants.c
533      */
534     extern void print_command(unsigned char *);
535     extern void print_sense(const char *, Scsi_Cmnd *);
536     extern void print_req_sense(const char *, Scsi_Request *);
537     extern void print_driverbyte(int scsiresult);
538     extern void print_hostbyte(int scsiresult);
539     extern void print_status (int status);
540     
541     /*
542      *  The scsi_device struct contains what we know about each given scsi
543      *  device.
544      *
545      * FIXME(eric) - one of the great regrets that I have is that I failed to define
546      * these structure elements as something like sdev_foo instead of foo.  This would
547      * make it so much easier to grep through sources and so forth.  I propose that
548      * all new elements that get added to these structures follow this convention.
549      * As time goes on and as people have the stomach for it, it should be possible to 
550      * go back and retrofit at least some of the elements here with with the prefix.
551      */
552     
553     struct scsi_device {
554     /* private: */
555     	/*
556     	 * This information is private to the scsi mid-layer.  Wrapping it in a
557     	 * struct private is a way of marking it in a sort of C++ type of way.
558     	 */
559     	struct scsi_device *next;	/* Used for linked list */
560     	struct scsi_device *prev;	/* Used for linked list */
561     	wait_queue_head_t   scpnt_wait;	/* Used to wait if
562     					   device is busy */
563     	struct Scsi_Host *host;
564     	request_queue_t request_queue;
565             atomic_t                device_active; /* commands checked out for device */
566     	volatile unsigned short device_busy;	/* commands actually active on low-level */
567     	int (*scsi_init_io_fn) (Scsi_Cmnd *);	/* Used to initialize
568     						   new request */
569     	Scsi_Cmnd *device_queue;	/* queue of SCSI Command structures */
570     
571     /* public: */
572     	unsigned int id, lun, channel;
573     
574     	unsigned int manufacturer;	/* Manufacturer of device, for using 
575     					 * vendor-specific cmd's */
576     	unsigned sector_size;	/* size in bytes */
577     
578     	int attached;		/* # of high level drivers attached to 
579     				 * this */
580     	int access_count;	/* Count of open channels/mounts */
581     
582     	void *hostdata;		/* available to low-level driver */
583     	devfs_handle_t de;      /* directory for the device      */
584     	char type;
585     	char scsi_level;
586     	char vendor[8], model[16], rev[4];
587     	unsigned char current_tag;	/* current tag */
588     	unsigned char sync_min_period;	/* Not less than this period */
589     	unsigned char sync_max_offset;	/* Not greater than this offset */
590     	unsigned char queue_depth;	/* How deep a queue to use */
591     
592     	unsigned online:1;
593     	unsigned writeable:1;
594     	unsigned removable:1;
595     	unsigned random:1;
596     	unsigned has_cmdblocks:1;
597     	unsigned changed:1;	/* Data invalid due to media change */
598     	unsigned busy:1;	/* Used to prevent races */
599     	unsigned lockable:1;	/* Able to prevent media removal */
600     	unsigned borken:1;	/* Tell the Seagate driver to be 
601     				 * painfully slow on this device */
602     	unsigned tagged_supported:1;	/* Supports SCSI-II tagged queuing */
603     	unsigned tagged_queue:1;	/* SCSI-II tagged queuing enabled */
604     	unsigned disconnect:1;	/* can disconnect */
605     	unsigned soft_reset:1;	/* Uses soft reset option */
606     	unsigned sync:1;	/* Negotiate for sync transfers */
607     	unsigned wide:1;	/* Negotiate for WIDE transfers */
608     	unsigned single_lun:1;	/* Indicates we should only allow I/O to
609     				 * one of the luns for the device at a 
610     				 * time. */
611     	unsigned was_reset:1;	/* There was a bus reset on the bus for 
612     				 * this device */
613     	unsigned expecting_cc_ua:1;	/* Expecting a CHECK_CONDITION/UNIT_ATTN
614     					 * because we did a bus reset. */
615     	unsigned device_blocked:1;	/* Device returned QUEUE_FULL. */
616     	unsigned ten:1;		/* support ten byte read / write */
617     	unsigned remap:1;	/* support remapping  */
618     	unsigned starved:1;	/* unable to process commands because
619     				   host busy */
620     
621     	// Flag to allow revalidate to succeed in sd_open
622     	int allow_revalidate;
623     };
624     
625     
626     /*
627      * The Scsi_Cmnd structure is used by scsi.c internally, and for communication
628      * with low level drivers that support multiple outstanding commands.
629      */
630     typedef struct scsi_pointer {
631     	char *ptr;		/* data pointer */
632     	int this_residual;	/* left in this buffer */
633     	struct scatterlist *buffer;	/* which buffer */
634     	int buffers_residual;	/* how many buffers left */
635     
636     	volatile int Status;
637     	volatile int Message;
638     	volatile int have_data_in;
639     	volatile int sent_command;
640     	volatile int phase;
641     } Scsi_Pointer;
642     
643     /*
644      * This is essentially a slimmed down version of Scsi_Cmnd.  The point of
645      * having this is that requests that are injected into the queue as result
646      * of things like ioctls and character devices shouldn't be using a
647      * Scsi_Cmnd until such a time that the command is actually at the head
648      * of the queue and being sent to the driver.
649      */
650     struct scsi_request {
651     	int     sr_magic;
652     	int     sr_result;	/* Status code from lower level driver */
653     	unsigned char sr_sense_buffer[SCSI_SENSE_BUFFERSIZE];		/* obtained by REQUEST SENSE
654     						 * when CHECK CONDITION is
655     						 * received on original command 
656     						 * (auto-sense) */
657     
658     	struct Scsi_Host *sr_host;
659     	Scsi_Device *sr_device;
660     	Scsi_Cmnd *sr_command;
661     	struct request sr_request;	/* A copy of the command we are
662     				   working on */
663     	unsigned sr_bufflen;	/* Size of data buffer */
664     	void *sr_buffer;		/* Data buffer */
665     	int sr_allowed;
666     	unsigned char sr_data_direction;
667     	unsigned char sr_cmd_len;
668     	unsigned char sr_cmnd[MAX_COMMAND_SIZE];
669     	void (*sr_done) (struct scsi_cmnd *);	/* Mid-level done function */
670     	int sr_timeout_per_command;
671     	unsigned short sr_use_sg;	/* Number of pieces of scatter-gather */
672     	unsigned short sr_sglist_len;	/* size of malloc'd scatter-gather list */
673     	unsigned sr_underflow;	/* Return error if less than
674     				   this amount is transferred */
675     };
676     
677     /*
678      * FIXME(eric) - one of the great regrets that I have is that I failed to define
679      * these structure elements as something like sc_foo instead of foo.  This would
680      * make it so much easier to grep through sources and so forth.  I propose that
681      * all new elements that get added to these structures follow this convention.
682      * As time goes on and as people have the stomach for it, it should be possible to 
683      * go back and retrofit at least some of the elements here with with the prefix.
684      */
685     struct scsi_cmnd {
686     	int     sc_magic;
687     /* private: */
688     	/*
689     	 * This information is private to the scsi mid-layer.  Wrapping it in a
690     	 * struct private is a way of marking it in a sort of C++ type of way.
691     	 */
692     	struct Scsi_Host *host;
693     	unsigned short state;
694     	unsigned short owner;
695     	Scsi_Device *device;
696     	Scsi_Request *sc_request;
697     	struct scsi_cmnd *next;
698     	struct scsi_cmnd *reset_chain;
699     
700     	int eh_state;		/* Used for state tracking in error handlr */
701     	void (*done) (struct scsi_cmnd *);	/* Mid-level done function */
702     	/*
703     	   A SCSI Command is assigned a nonzero serial_number when internal_cmnd
704     	   passes it to the driver's queue command function.  The serial_number
705     	   is cleared when scsi_done is entered indicating that the command has
706     	   been completed.  If a timeout occurs, the serial number at the moment
707     	   of timeout is copied into serial_number_at_timeout.  By subsequently
708     	   comparing the serial_number and serial_number_at_timeout fields
709     	   during abort or reset processing, we can detect whether the command
710     	   has already completed.  This also detects cases where the command has
711     	   completed and the SCSI Command structure has already being reused
712     	   for another command, so that we can avoid incorrectly aborting or
713     	   resetting the new command.
714     	 */
715     
716     	unsigned long serial_number;
717     	unsigned long serial_number_at_timeout;
718     
719     	int retries;
720     	int allowed;
721     	int timeout_per_command;
722     	int timeout_total;
723     	int timeout;
724     
725     	/*
726     	 * We handle the timeout differently if it happens when a reset, 
727     	 * abort, etc are in process. 
728     	 */
729     	unsigned volatile char internal_timeout;
730     	struct scsi_cmnd *bh_next;	/* To enumerate the commands waiting 
731     					   to be processed. */
732     
733     /* public: */
734     
735     	unsigned int target;
736     	unsigned int lun;
737     	unsigned int channel;
738     	unsigned char cmd_len;
739     	unsigned char old_cmd_len;
740     	unsigned char sc_data_direction;
741     	unsigned char sc_old_data_direction;
742     
743     	/* These elements define the operation we are about to perform */
744     	unsigned char cmnd[MAX_COMMAND_SIZE];
745     	unsigned request_bufflen;	/* Actual request size */
746     
747     	struct timer_list eh_timeout;	/* Used to time out the command. */
748     	void *request_buffer;	/* Actual requested buffer */
749     
750     	/* These elements define the operation we ultimately want to perform */
751     	unsigned char data_cmnd[MAX_COMMAND_SIZE];
752     	unsigned short old_use_sg;	/* We save  use_sg here when requesting
753     					 * sense info */
754     	unsigned short use_sg;	/* Number of pieces of scatter-gather */
755     	unsigned short sglist_len;	/* size of malloc'd scatter-gather list */
756     	unsigned short abort_reason;	/* If the mid-level code requests an
757     					 * abort, this is the reason. */
758     	unsigned bufflen;	/* Size of data buffer */
759     	void *buffer;		/* Data buffer */
760     
761     	unsigned underflow;	/* Return error if less than
762     				   this amount is transferred */
763     	unsigned old_underflow;	/* save underflow here when reusing the
764     				 * command for error handling */
765     
766     	unsigned transfersize;	/* How much we are guaranteed to
767     				   transfer with each SCSI transfer
768     				   (ie, between disconnect / 
769     				   reconnects.   Probably == sector
770     				   size */
771     
772     	int resid;		/* Number of bytes requested to be
773     				   transferred less actual number
774     				   transferred (0 if not supported) */
775     
776     	struct request request;	/* A copy of the command we are
777     				   working on */
778     
779     	unsigned char sense_buffer[SCSI_SENSE_BUFFERSIZE];		/* obtained by REQUEST SENSE
780     						 * when CHECK CONDITION is
781     						 * received on original command 
782     						 * (auto-sense) */
783     
784     	unsigned flags;
785     
786     	/*
787     	 * Used to indicate that a command which has timed out also
788     	 * completed normally.  Typically the completion function will
789     	 * do nothing but set this flag in this instance because the
790     	 * timeout handler is already running.
791     	 */
792     	unsigned done_late:1;
793     
794     	/* Low-level done function - can be used by low-level driver to point
795     	 *        to completion function.  Not used by mid/upper level code. */
796     	void (*scsi_done) (struct scsi_cmnd *);
797     
798     	/*
799     	 * The following fields can be written to by the host specific code. 
800     	 * Everything else should be left alone. 
801     	 */
802     
803     	Scsi_Pointer SCp;	/* Scratchpad used by some host adapters */
804     
805     	unsigned char *host_scribble;	/* The host adapter is allowed to
806     					   * call scsi_malloc and get some memory
807     					   * and hang it here.     The host adapter
808     					   * is also expected to call scsi_free
809     					   * to release this memory.  (The memory
810     					   * obtained by scsi_malloc is guaranteed
811     					   * to be at an address < 16Mb). */
812     
813     	int result;		/* Status code from lower level driver */
814     
815     	unsigned char tag;	/* SCSI-II queued command tag */
816     	unsigned long pid;	/* Process ID, starts at 0 */
817     };
818     
819     /*
820      *  Flag bit for the internal_timeout array
821      */
822     #define NORMAL_TIMEOUT 0
823     
824     /*
825      * Definitions and prototypes used for scsi mid-level queue.
826      */
827     #define SCSI_MLQUEUE_HOST_BUSY   0x1055
828     #define SCSI_MLQUEUE_DEVICE_BUSY 0x1056
829     
830     #define SCSI_SLEEP(QUEUE, CONDITION) {		    \
831         if (CONDITION) {			            \
832     	DECLARE_WAITQUEUE(wait, current);	    \
833     	add_wait_queue(QUEUE, &wait);		    \
834     	for(;;) {			            \
835     	set_current_state(TASK_UNINTERRUPTIBLE);    \
836     	if (CONDITION) {		            \
837                 if (in_interrupt())	                    \
838     	        panic("scsi: trying to call schedule() in interrupt" \
839     		      ", file %s, line %d.\n", __FILE__, __LINE__);  \
840     	    schedule();			\
841             }				\
842     	else			        \
843     	    break;      		\
844     	}			        \
845     	remove_wait_queue(QUEUE, &wait);\
846     	current->state = TASK_RUNNING;	\
847         }; }
848     
849     #endif
850     
851     /*
852      * Overrides for Emacs so that we follow Linus's tabbing style.
853      * Emacs will notice this stuff at the end of the file and automatically
854      * adjust the settings for this buffer only.  This must remain at the end
855      * of the file.
856      * ---------------------------------------------------------------------------
857      * Local variables:
858      * c-indent-level: 4 
859      * c-brace-imaginary-offset: 0
860      * c-brace-offset: -4
861      * c-argdecl-indent: 4
862      * c-label-offset: -4
863      * c-continued-statement-offset: 4
864      * c-continued-brace-offset: 0
865      * indent-tabs-mode: nil
866      * tab-width: 8
867      * End:
868      */
869