vserver 1.9.5.x5
[linux-2.6.git] / include / scsi / scsi_host.h
1 #ifndef _SCSI_SCSI_HOST_H
2 #define _SCSI_SCSI_HOST_H
3
4 #include <linux/device.h>
5 #include <linux/list.h>
6 #include <linux/types.h>
7
8 struct block_device;
9 struct module;
10 struct scsi_cmnd;
11 struct scsi_device;
12 struct Scsi_Host;
13 struct scsi_host_cmd_pool;
14 struct scsi_transport_template;
15
16
17 /*
18  * The various choices mean:
19  * NONE: Self evident.  Host adapter is not capable of scatter-gather.
20  * ALL:  Means that the host adapter module can do scatter-gather,
21  *       and that there is no limit to the size of the table to which
22  *       we scatter/gather data.
23  * Anything else:  Indicates the maximum number of chains that can be
24  *       used in one scatter-gather request.
25  */
26 #define SG_NONE 0
27 #define SG_ALL 0xff
28
29
30 #define DISABLE_CLUSTERING 0
31 #define ENABLE_CLUSTERING 1
32
33 enum scsi_eh_timer_return {
34         EH_NOT_HANDLED,
35         EH_HANDLED,
36         EH_RESET_TIMER,
37 };
38
39
40 struct scsi_host_template {
41         struct module *module;
42         const char *name;
43
44         /*
45          * Used to initialize old-style drivers.  For new-style drivers
46          * just perform all work in your module initialization function.
47          *
48          * Status:  OBSOLETE
49          */
50         int (* detect)(struct scsi_host_template *);
51
52         /*
53          * Used as unload callback for hosts with old-style drivers.
54          *
55          * Status: OBSOLETE
56          */
57         int (* release)(struct Scsi_Host *);
58
59         /*
60          * The info function will return whatever useful information the
61          * developer sees fit.  If not provided, then the name field will
62          * be used instead.
63          *
64          * Status: OPTIONAL
65          */
66         const char *(* info)(struct Scsi_Host *);
67
68         /*
69          * Ioctl interface
70          *
71          * Status: OPTIONAL
72          */
73         int (* ioctl)(struct scsi_device *dev, int cmd, void __user *arg);
74
75
76 #ifdef CONFIG_COMPAT
77         /* 
78          * Compat handler. Handle 32bit ABI.
79          * When unknown ioctl is passed return -ENOIOCTLCMD.
80          *
81          * Status: OPTIONAL
82          */
83         int (* compat_ioctl)(struct scsi_device *dev, int cmd, void __user *arg);
84 #endif
85
86         /*
87          * The queuecommand function is used to queue up a scsi
88          * command block to the LLDD.  When the driver finished
89          * processing the command the done callback is invoked.
90          *
91          * If queuecommand returns 0, then the HBA has accepted the
92          * command.  The done() function must be called on the command
93          * when the driver has finished with it. (you may call done on the
94          * command before queuecommand returns, but in this case you
95          * *must* return 0 from queuecommand).
96          *
97          * Queuecommand may also reject the command, in which case it may
98          * not touch the command and must not call done() for it.
99          *
100          * There are two possible rejection returns:
101          *
102          *   SCSI_MLQUEUE_DEVICE_BUSY: Block this device temporarily, but
103          *   allow commands to other devices serviced by this host.
104          *
105          *   SCSI_MLQUEUE_HOST_BUSY: Block all devices served by this
106          *   host temporarily.
107          *
108          * For compatibility, any other non-zero return is treated the
109          * same as SCSI_MLQUEUE_HOST_BUSY.
110          *
111          * NOTE: "temporarily" means either until the next command for#
112          * this device/host completes, or a period of time determined by
113          * I/O pressure in the system if there are no other outstanding
114          * commands.
115          *
116          * STATUS: REQUIRED
117          */
118         int (* queuecommand)(struct scsi_cmnd *,
119                              void (*done)(struct scsi_cmnd *));
120
121         /*
122          * This is an error handling strategy routine.  You don't need to
123          * define one of these if you don't want to - there is a default
124          * routine that is present that should work in most cases.  For those
125          * driver authors that have the inclination and ability to write their
126          * own strategy routine, this is where it is specified.  Note - the
127          * strategy routine is *ALWAYS* run in the context of the kernel eh
128          * thread.  Thus you are guaranteed to *NOT* be in an interrupt
129          * handler when you execute this, and you are also guaranteed to
130          * *NOT* have any other commands being queued while you are in the
131          * strategy routine. When you return from this function, operations
132          * return to normal.
133          *
134          * See scsi_error.c scsi_unjam_host for additional comments about
135          * what this function should and should not be attempting to do.
136          *
137          * Status: REQUIRED     (at least one of them)
138          */
139         int (* eh_strategy_handler)(struct Scsi_Host *);
140         int (* eh_abort_handler)(struct scsi_cmnd *);
141         int (* eh_device_reset_handler)(struct scsi_cmnd *);
142         int (* eh_bus_reset_handler)(struct scsi_cmnd *);
143         int (* eh_host_reset_handler)(struct scsi_cmnd *);
144
145         /*
146          * This is an optional routine to notify the host that the scsi
147          * timer just fired.  The returns tell the timer routine what to
148          * do about this:
149          *
150          * EH_HANDLED:          I fixed the error, please complete the command
151          * EH_RESET_TIMER:      I need more time, reset the timer and
152          *                      begin counting again
153          * EH_NOT_HANDLED       Begin normal error recovery
154          *
155          * Status: OPTIONAL
156          */
157         enum scsi_eh_timer_return (* eh_timed_out)(struct scsi_cmnd *);
158
159         /*
160          * Before the mid layer attempts to scan for a new device where none
161          * currently exists, it will call this entry in your driver.  Should
162          * your driver need to allocate any structs or perform any other init
163          * items in order to send commands to a currently unused target/lun
164          * combo, then this is where you can perform those allocations.  This
165          * is specifically so that drivers won't have to perform any kind of
166          * "is this a new device" checks in their queuecommand routine,
167          * thereby making the hot path a bit quicker.
168          *
169          * Return values: 0 on success, non-0 on failure
170          *
171          * Deallocation:  If we didn't find any devices at this ID, you will
172          * get an immediate call to slave_destroy().  If we find something
173          * here then you will get a call to slave_configure(), then the
174          * device will be used for however long it is kept around, then when
175          * the device is removed from the system (or * possibly at reboot
176          * time), you will then get a call to slave_destroy().  This is
177          * assuming you implement slave_configure and slave_destroy.
178          * However, if you allocate memory and hang it off the device struct,
179          * then you must implement the slave_destroy() routine at a minimum
180          * in order to avoid leaking memory
181          * each time a device is tore down.
182          *
183          * Status: OPTIONAL
184          */
185         int (* slave_alloc)(struct scsi_device *);
186
187         /*
188          * Once the device has responded to an INQUIRY and we know the
189          * device is online, we call into the low level driver with the
190          * struct scsi_device *.  If the low level device driver implements
191          * this function, it *must* perform the task of setting the queue
192          * depth on the device.  All other tasks are optional and depend
193          * on what the driver supports and various implementation details.
194          * 
195          * Things currently recommended to be handled at this time include:
196          *
197          * 1.  Setting the device queue depth.  Proper setting of this is
198          *     described in the comments for scsi_adjust_queue_depth.
199          * 2.  Determining if the device supports the various synchronous
200          *     negotiation protocols.  The device struct will already have
201          *     responded to INQUIRY and the results of the standard items
202          *     will have been shoved into the various device flag bits, eg.
203          *     device->sdtr will be true if the device supports SDTR messages.
204          * 3.  Allocating command structs that the device will need.
205          * 4.  Setting the default timeout on this device (if needed).
206          * 5.  Anything else the low level driver might want to do on a device
207          *     specific setup basis...
208          * 6.  Return 0 on success, non-0 on error.  The device will be marked
209          *     as offline on error so that no access will occur.  If you return
210          *     non-0, your slave_destroy routine will never get called for this
211          *     device, so don't leave any loose memory hanging around, clean
212          *     up after yourself before returning non-0
213          *
214          * Status: OPTIONAL
215          */
216         int (* slave_configure)(struct scsi_device *);
217
218         /*
219          * Immediately prior to deallocating the device and after all activity
220          * has ceased the mid layer calls this point so that the low level
221          * driver may completely detach itself from the scsi device and vice
222          * versa.  The low level driver is responsible for freeing any memory
223          * it allocated in the slave_alloc or slave_configure calls. 
224          *
225          * Status: OPTIONAL
226          */
227         void (* slave_destroy)(struct scsi_device *);
228
229         /*
230          * fill in this function to allow the queue depth of this host
231          * to be changeable (on a per device basis).  returns either
232          * the current queue depth setting (may be different from what
233          * was passed in) or an error.  An error should only be
234          * returned if the requested depth is legal but the driver was
235          * unable to set it.  If the requested depth is illegal, the
236          * driver should set and return the closest legal queue depth.
237          *
238          */
239         int (* change_queue_depth)(struct scsi_device *, int);
240
241         /*
242          * fill in this function to allow the changing of tag types
243          * (this also allows the enabling/disabling of tag command
244          * queueing).  An error should only be returned if something
245          * went wrong in the driver while trying to set the tag type.
246          * If the driver doesn't support the requested tag type, then
247          * it should set the closest type it does support without
248          * returning an error.  Returns the actual tag type set.
249          */
250         int (* change_queue_type)(struct scsi_device *, int);
251
252         /*
253          * This function determines the bios parameters for a given
254          * harddisk.  These tend to be numbers that are made up by
255          * the host adapter.  Parameters:
256          * size, device, list (heads, sectors, cylinders)
257          *
258          * Status: OPTIONAL */
259         int (* bios_param)(struct scsi_device *, struct block_device *,
260                         sector_t, int []);
261
262         /*
263          * Can be used to export driver statistics and other infos to the
264          * world outside the kernel ie. userspace and it also provides an
265          * interface to feed the driver with information.
266          *
267          * Status: OBSOLETE
268          */
269         int (*proc_info)(struct Scsi_Host *, char *, char **, off_t, int, int);
270
271         /*
272          * Name of proc directory
273          */
274         char *proc_name;
275
276         /*
277          * Used to store the procfs directory if a driver implements the
278          * proc_info method.
279          */
280         struct proc_dir_entry *proc_dir;
281
282         /*
283          * This determines if we will use a non-interrupt driven
284          * or an interrupt driven scheme,  It is set to the maximum number
285          * of simultaneous commands a given host adapter will accept.
286          */
287         int can_queue;
288
289         /*
290          * In many instances, especially where disconnect / reconnect are
291          * supported, our host also has an ID on the SCSI bus.  If this is
292          * the case, then it must be reserved.  Please set this_id to -1 if
293          * your setup is in single initiator mode, and the host lacks an
294          * ID.
295          */
296         int this_id;
297
298         /*
299          * This determines the degree to which the host adapter is capable
300          * of scatter-gather.
301          */
302         unsigned short sg_tablesize;
303
304         /*
305          * If the host adapter has limitations beside segment count
306          */
307         unsigned short max_sectors;
308
309         /*
310          * dma scatter gather segment boundary limit. a segment crossing this
311          * boundary will be split in two.
312          */
313         unsigned long dma_boundary;
314
315         /*
316          * This specifies "machine infinity" for host templates which don't
317          * limit the transfer size.  Note this limit represents an absolute
318          * maximum, and may be over the transfer limits allowed for
319          * individual devices (e.g. 256 for SCSI-1)
320          */
321 #define SCSI_DEFAULT_MAX_SECTORS        1024
322
323         /*
324          * True if this host adapter can make good use of linked commands.
325          * This will allow more than one command to be queued to a given
326          * unit on a given host.  Set this to the maximum number of command
327          * blocks to be provided for each device.  Set this to 1 for one
328          * command block per lun, 2 for two, etc.  Do not set this to 0.
329          * You should make sure that the host adapter will do the right thing
330          * before you try setting this above 1.
331          */
332         short cmd_per_lun;
333
334         /*
335          * present contains counter indicating how many boards of this
336          * type were found when we did the scan.
337          */
338         unsigned char present;
339
340         /*
341          * true if this host adapter uses unchecked DMA onto an ISA bus.
342          */
343         unsigned unchecked_isa_dma:1;
344
345         /*
346          * true if this host adapter can make good use of clustering.
347          * I originally thought that if the tablesize was large that it
348          * was a waste of CPU cycles to prepare a cluster list, but
349          * it works out that the Buslogic is faster if you use a smaller
350          * number of segments (i.e. use clustering).  I guess it is
351          * inefficient.
352          */
353         unsigned use_clustering:1;
354
355         /*
356          * True for emulated SCSI host adapters (e.g. ATAPI)
357          */
358         unsigned emulated:1;
359
360         /*
361          * True if the low-level driver performs its own reset-settle delays.
362          */
363         unsigned skip_settle_delay:1;
364
365         /*
366          * Countdown for host blocking with no commands outstanding
367          */
368         unsigned int max_host_blocked;
369
370         /*
371          * Default value for the blocking.  If the queue is empty,
372          * host_blocked counts down in the request_fn until it restarts
373          * host operations as zero is reached.  
374          *
375          * FIXME: This should probably be a value in the template
376          */
377 #define SCSI_DEFAULT_HOST_BLOCKED       7
378
379         /*
380          * Pointer to the sysfs class properties for this host, NULL terminated.
381          */
382         struct class_device_attribute **shost_attrs;
383
384         /*
385          * Pointer to the SCSI device properties for this host, NULL terminated.
386          */
387         struct device_attribute **sdev_attrs;
388
389         /*
390          * List of hosts per template.
391          *
392          * This is only for use by scsi_module.c for legacy templates.
393          * For these access to it is synchronized implicitly by
394          * module_init/module_exit.
395          */
396         struct list_head legacy_hosts;
397 };
398
399 /*
400  * shost states
401  */
402 enum {
403         SHOST_ADD,
404         SHOST_DEL,
405         SHOST_CANCEL,
406         SHOST_RECOVERY,
407 };
408
409 struct Scsi_Host {
410         /*
411          * __devices is protected by the host_lock, but you should
412          * usually use scsi_device_lookup / shost_for_each_device
413          * to access it and don't care about locking yourself.
414          * In the rare case of beeing in irq context you can use
415          * their __ prefixed variants with the lock held. NEVER
416          * access this list directly from a driver.
417          */
418         struct list_head        __devices;
419         
420         struct scsi_host_cmd_pool *cmd_pool;
421         spinlock_t              free_list_lock;
422         struct list_head        free_list; /* backup store of cmd structs */
423         struct list_head        starved_list;
424
425         spinlock_t              default_lock;
426         spinlock_t              *host_lock;
427
428         struct semaphore        scan_mutex;/* serialize scanning activity */
429
430         struct list_head        eh_cmd_q;
431         struct task_struct    * ehandler;  /* Error recovery thread. */
432         struct semaphore      * eh_wait;   /* The error recovery thread waits
433                                               on this. */
434         struct completion     * eh_notify; /* wait for eh to begin or end */
435         struct semaphore      * eh_action; /* Wait for specific actions on the
436                                           host. */
437         unsigned int            eh_active:1; /* Indicates the eh thread is awake and active if
438                                           this is true. */
439         unsigned int            eh_kill:1; /* set when killing the eh thread */
440         wait_queue_head_t       host_wait;
441         struct scsi_host_template *hostt;
442         struct scsi_transport_template *transportt;
443         volatile unsigned short host_busy;   /* commands actually active on low-level */
444         volatile unsigned short host_failed; /* commands that failed. */
445     
446         unsigned short host_no;  /* Used for IOCTL_GET_IDLUN, /proc/scsi et al. */
447         int resetting; /* if set, it means that last_reset is a valid value */
448         unsigned long last_reset;
449
450         /*
451          * These three parameters can be used to allow for wide scsi,
452          * and for host adapters that support multiple busses
453          * The first two should be set to 1 more than the actual max id
454          * or lun (i.e. 8 for normal systems).
455          */
456         unsigned int max_id;
457         unsigned int max_lun;
458         unsigned int max_channel;
459
460         /*
461          * This is a unique identifier that must be assigned so that we
462          * have some way of identifying each detected host adapter properly
463          * and uniquely.  For hosts that do not support more than one card
464          * in the system at one time, this does not need to be set.  It is
465          * initialized to 0 in scsi_register.
466          */
467         unsigned int unique_id;
468
469         /*
470          * The maximum length of SCSI commands that this host can accept.
471          * Probably 12 for most host adapters, but could be 16 for others.
472          * For drivers that don't set this field, a value of 12 is
473          * assumed.  I am leaving this as a number rather than a bit
474          * because you never know what subsequent SCSI standards might do
475          * (i.e. could there be a 20 byte or a 24-byte command a few years
476          * down the road?).  
477          */
478         unsigned char max_cmd_len;
479
480         int this_id;
481         int can_queue;
482         short cmd_per_lun;
483         short unsigned int sg_tablesize;
484         short unsigned int max_sectors;
485         unsigned long dma_boundary;
486
487         unsigned unchecked_isa_dma:1;
488         unsigned use_clustering:1;
489         unsigned use_blk_tcq:1;
490
491         /*
492          * Host has requested that no further requests come through for the
493          * time being.
494          */
495         unsigned host_self_blocked:1;
496     
497         /*
498          * Host uses correct SCSI ordering not PC ordering. The bit is
499          * set for the minority of drivers whose authors actually read
500          * the spec ;)
501          */
502         unsigned reverse_ordering:1;
503
504         /*
505          * Host has rejected a command because it was busy.
506          */
507         unsigned int host_blocked;
508
509         /*
510          * Value host_blocked counts down from
511          */
512         unsigned int max_host_blocked;
513
514         /* legacy crap */
515         unsigned long base;
516         unsigned long io_port;
517         unsigned char n_io_port;
518         unsigned char dma_channel;
519         unsigned int  irq;
520         
521
522         unsigned long shost_state;
523
524         /* ldm bits */
525         struct device           shost_gendev;
526         struct class_device     shost_classdev;
527
528         /*
529          * List of hosts per template.
530          *
531          * This is only for use by scsi_module.c for legacy templates.
532          * For these access to it is synchronized implicitly by
533          * module_init/module_exit.
534          */
535         struct list_head sht_legacy_list;
536
537         /*
538          * Points to the transport data (if any) which is allocated
539          * separately
540          */
541         void *shost_data;
542
543         /*
544          * We should ensure that this is aligned, both for better performance
545          * and also because some compilers (m68k) don't automatically force
546          * alignment to a long boundary.
547          */
548         unsigned long hostdata[0]  /* Used for storage of host specific stuff */
549                 __attribute__ ((aligned (sizeof(unsigned long))));
550 };
551 #define         dev_to_shost(d)         \
552         container_of(d, struct Scsi_Host, shost_gendev)
553 #define         class_to_shost(d)       \
554         container_of(d, struct Scsi_Host, shost_classdev)
555
556
557 extern struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *, int);
558 extern int __must_check scsi_add_host(struct Scsi_Host *, struct device *);
559 extern void scsi_scan_host(struct Scsi_Host *);
560 extern void scsi_scan_single_target(struct Scsi_Host *, unsigned int,
561         unsigned int);
562 extern void scsi_rescan_device(struct device *);
563 extern void scsi_remove_host(struct Scsi_Host *);
564 extern struct Scsi_Host *scsi_host_get(struct Scsi_Host *);
565 extern void scsi_host_put(struct Scsi_Host *t);
566 extern struct Scsi_Host *scsi_host_lookup(unsigned short);
567
568 extern u64 scsi_calculate_bounce_limit(struct Scsi_Host *);
569
570 static inline void scsi_assign_lock(struct Scsi_Host *shost, spinlock_t *lock)
571 {
572         shost->host_lock = lock;
573 }
574
575 static inline void scsi_set_device(struct Scsi_Host *shost,
576                                    struct device *dev)
577 {
578         shost->shost_gendev.parent = dev;
579 }
580
581 static inline struct device *scsi_get_device(struct Scsi_Host *shost)
582 {
583         return shost->shost_gendev.parent;
584 }
585
586 extern void scsi_unblock_requests(struct Scsi_Host *);
587 extern void scsi_block_requests(struct Scsi_Host *);
588
589 struct class_container;
590 /*
591  * These two functions are used to allocate and free a pseudo device
592  * which will connect to the host adapter itself rather than any
593  * physical device.  You must deallocate when you are done with the
594  * thing.  This physical pseudo-device isn't real and won't be available
595  * from any high-level drivers.
596  */
597 extern void scsi_free_host_dev(struct scsi_device *);
598 extern struct scsi_device *scsi_get_host_dev(struct Scsi_Host *);
599 int scsi_is_host_device(const struct device *);
600
601
602 /* legacy interfaces */
603 extern struct Scsi_Host *scsi_register(struct scsi_host_template *, int);
604 extern void scsi_unregister(struct Scsi_Host *);
605
606 #endif /* _SCSI_SCSI_HOST_H */