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
scsi_to_pci_dma_dir(unsigned char scsi_dir)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
scsi_to_sbus_dma_dir(unsigned char scsi_dir)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 */
612 
613 	// Flag to allow revalidate to succeed in sd_open
614 	int allow_revalidate;
615 };
616 
617 
618 /*
619  * The Scsi_Cmnd structure is used by scsi.c internally, and for communication
620  * with low level drivers that support multiple outstanding commands.
621  */
622 typedef struct scsi_pointer {
623 	char *ptr;		/* data pointer */
624 	int this_residual;	/* left in this buffer */
625 	struct scatterlist *buffer;	/* which buffer */
626 	int buffers_residual;	/* how many buffers left */
627 
628 	dma_addr_t dma_handle;
629 
630 	volatile int Status;
631 	volatile int Message;
632 	volatile int have_data_in;
633 	volatile int sent_command;
634 	volatile int phase;
635 } Scsi_Pointer;
636 
637 /*
638  * This is essentially a slimmed down version of Scsi_Cmnd.  The point of
639  * having this is that requests that are injected into the queue as result
640  * of things like ioctls and character devices shouldn't be using a
641  * Scsi_Cmnd until such a time that the command is actually at the head
642  * of the queue and being sent to the driver.
643  */
644 struct scsi_request {
645 	int     sr_magic;
646 	int     sr_result;	/* Status code from lower level driver */
647 	unsigned char sr_sense_buffer[SCSI_SENSE_BUFFERSIZE];		/* obtained by REQUEST SENSE
648 						 * when CHECK CONDITION is
649 						 * received on original command
650 						 * (auto-sense) */
651 
652 	struct Scsi_Host *sr_host;
653 	Scsi_Device *sr_device;
654 	Scsi_Cmnd *sr_command;
655 	struct request sr_request;	/* A copy of the command we are
656 				   working on */
657 	unsigned sr_bufflen;	/* Size of data buffer */
658 	void *sr_buffer;		/* Data buffer */
659 	int sr_allowed;
660 	unsigned char sr_data_direction;
661 	unsigned char sr_cmd_len;
662 	unsigned char sr_cmnd[MAX_COMMAND_SIZE];
663 	void (*sr_done) (struct scsi_cmnd *);	/* Mid-level done function */
664 	int sr_timeout_per_command;
665 	unsigned short sr_use_sg;	/* Number of pieces of scatter-gather */
666 	unsigned short sr_sglist_len;	/* size of malloc'd scatter-gather list */
667 	unsigned sr_underflow;	/* Return error if less than
668 				   this amount is transferred */
669 };
670 
671 /*
672  * FIXME(eric) - one of the great regrets that I have is that I failed to define
673  * these structure elements as something like sc_foo instead of foo.  This would
674  * make it so much easier to grep through sources and so forth.  I propose that
675  * all new elements that get added to these structures follow this convention.
676  * As time goes on and as people have the stomach for it, it should be possible to
677  * go back and retrofit at least some of the elements here with with the prefix.
678  */
679 struct scsi_cmnd {
680 	int     sc_magic;
681 /* private: */
682 	/*
683 	 * This information is private to the scsi mid-layer.  Wrapping it in a
684 	 * struct private is a way of marking it in a sort of C++ type of way.
685 	 */
686 	struct Scsi_Host *host;
687 	unsigned short state;
688 	unsigned short owner;
689 	Scsi_Device *device;
690 	Scsi_Request *sc_request;
691 	struct scsi_cmnd *next;
692 	struct scsi_cmnd *reset_chain;
693 
694 	int eh_state;		/* Used for state tracking in error handlr */
695 	void (*done) (struct scsi_cmnd *);	/* Mid-level done function */
696 	/*
697 	   A SCSI Command is assigned a nonzero serial_number when internal_cmnd
698 	   passes it to the driver's queue command function.  The serial_number
699 	   is cleared when scsi_done is entered indicating that the command has
700 	   been completed.  If a timeout occurs, the serial number at the moment
701 	   of timeout is copied into serial_number_at_timeout.  By subsequently
702 	   comparing the serial_number and serial_number_at_timeout fields
703 	   during abort or reset processing, we can detect whether the command
704 	   has already completed.  This also detects cases where the command has
705 	   completed and the SCSI Command structure has already being reused
706 	   for another command, so that we can avoid incorrectly aborting or
707 	   resetting the new command.
708 	 */
709 
710 	unsigned long serial_number;
711 	unsigned long serial_number_at_timeout;
712 
713 	int retries;
714 	int allowed;
715 	int timeout_per_command;
716 	int timeout_total;
717 	int timeout;
718 
719 	/*
720 	 * We handle the timeout differently if it happens when a reset,
721 	 * abort, etc are in process.
722 	 */
723 	unsigned volatile char internal_timeout;
724 	struct scsi_cmnd *bh_next;	/* To enumerate the commands waiting
725 					   to be processed. */
726 
727 /* public: */
728 
729 	unsigned int target;
730 	unsigned int lun;
731 	unsigned int channel;
732 	unsigned char cmd_len;
733 	unsigned char old_cmd_len;
734 	unsigned char sc_data_direction;
735 	unsigned char sc_old_data_direction;
736 
737 	/* These elements define the operation we are about to perform */
738 	unsigned char cmnd[MAX_COMMAND_SIZE];
739 	unsigned request_bufflen;	/* Actual request size */
740 
741 	struct timer_list eh_timeout;	/* Used to time out the command. */
742 	void *request_buffer;		/* Actual requested buffer */
743         void **bounce_buffers;		/* Array of bounce buffers when using scatter-gather */
744 
745 	/* These elements define the operation we ultimately want to perform */
746 	unsigned char data_cmnd[MAX_COMMAND_SIZE];
747 	unsigned short old_use_sg;	/* We save  use_sg here when requesting
748 					 * sense info */
749 	unsigned short use_sg;	/* Number of pieces of scatter-gather */
750 	unsigned short sglist_len;	/* size of malloc'd scatter-gather list */
751 	unsigned short abort_reason;	/* If the mid-level code requests an
752 					 * abort, this is the reason. */
753 	unsigned bufflen;	/* Size of data buffer */
754 	void *buffer;		/* Data buffer */
755 
756 	unsigned underflow;	/* Return error if less than
757 				   this amount is transferred */
758 	unsigned old_underflow;	/* save underflow here when reusing the
759 				 * command for error handling */
760 
761 	unsigned transfersize;	/* How much we are guaranteed to
762 				   transfer with each SCSI transfer
763 				   (ie, between disconnect /
764 				   reconnects.   Probably == sector
765 				   size */
766 
767 	int resid;		/* Number of bytes requested to be
768 				   transferred less actual number
769 				   transferred (0 if not supported) */
770 
771 	struct request request;	/* A copy of the command we are
772 				   working on */
773 
774 	unsigned char sense_buffer[SCSI_SENSE_BUFFERSIZE];		/* obtained by REQUEST SENSE
775 						 * when CHECK CONDITION is
776 						 * received on original command
777 						 * (auto-sense) */
778 
779 	unsigned flags;
780 
781 	/*
782 	 * Used to indicate that a command which has timed out also
783 	 * completed normally.  Typically the completion function will
784 	 * do nothing but set this flag in this instance because the
785 	 * timeout handler is already running.
786 	 */
787 	unsigned done_late:1;
788 
789 	/* Low-level done function - can be used by low-level driver to point
790 	 *        to completion function.  Not used by mid/upper level code. */
791 	void (*scsi_done) (struct scsi_cmnd *);
792 
793 	/*
794 	 * The following fields can be written to by the host specific code.
795 	 * Everything else should be left alone.
796 	 */
797 
798 	Scsi_Pointer SCp;	/* Scratchpad used by some host adapters */
799 
800 	unsigned char *host_scribble;	/* The host adapter is allowed to
801 					   * call scsi_malloc and get some memory
802 					   * and hang it here.     The host adapter
803 					   * is also expected to call scsi_free
804 					   * to release this memory.  (The memory
805 					   * obtained by scsi_malloc is guaranteed
806 					   * to be at an address < 16Mb). */
807 
808 	int result;		/* Status code from lower level driver */
809 
810 	unsigned char tag;	/* SCSI-II queued command tag */
811 	unsigned long pid;	/* Process ID, starts at 0 */
812 };
813 
814 /*
815  *  Flag bit for the internal_timeout array
816  */
817 #define NORMAL_TIMEOUT 0
818 
819 /*
820  * Definitions and prototypes used for scsi mid-level queue.
821  */
822 #define SCSI_MLQUEUE_HOST_BUSY   0x1055
823 #define SCSI_MLQUEUE_DEVICE_BUSY 0x1056
824 
825 #define SCSI_SLEEP(QUEUE, CONDITION) {		    \
826     if (CONDITION) {			            \
827 	DECLARE_WAITQUEUE(wait, current);	    \
828 	add_wait_queue(QUEUE, &wait);		    \
829 	for(;;) {			            \
830 	set_current_state(TASK_UNINTERRUPTIBLE);    \
831 	if (CONDITION) {		            \
832             if (in_interrupt())	                    \
833 	        panic("scsi: trying to call schedule() in interrupt" \
834 		      ", file %s, line %d.\n", __FILE__, __LINE__);  \
835 	    schedule();			\
836         }				\
837 	else			        \
838 	    break;      		\
839 	}			        \
840 	remove_wait_queue(QUEUE, &wait);\
841 	current->state = TASK_RUNNING;	\
842     }; }
843 
844 /*
845  * old style reset request from external source
846  * (private to sg.c and scsi_error.c, supplied by scsi_obsolete.c)
847  */
848 #define SCSI_TRY_RESET_DEVICE	1
849 #define SCSI_TRY_RESET_BUS	2
850 #define SCSI_TRY_RESET_HOST	3
851 
852 extern int scsi_reset_provider(Scsi_Device *, int);
853 
854 #endif
855 
856 /*
857  * Overrides for Emacs so that we follow Linus's tabbing style.
858  * Emacs will notice this stuff at the end of the file and automatically
859  * adjust the settings for this buffer only.  This must remain at the end
860  * of the file.
861  * ---------------------------------------------------------------------------
862  * Local variables:
863  * c-indent-level: 4
864  * c-brace-imaginary-offset: 0
865  * c-brace-offset: -4
866  * c-argdecl-indent: 4
867  * c-label-offset: -4
868  * c-continued-statement-offset: 4
869  * c-continued-brace-offset: 0
870  * indent-tabs-mode: nil
871  * tab-width: 8
872  * End:
873  */
874