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