Merge branch 'smack-for-4.17' of git://github.com/cschaufler/next-smack into next...
[sfrench/cifs-2.6.git] / include / scsi / scsi_host.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _SCSI_SCSI_HOST_H
3 #define _SCSI_SCSI_HOST_H
4
5 #include <linux/device.h>
6 #include <linux/list.h>
7 #include <linux/types.h>
8 #include <linux/workqueue.h>
9 #include <linux/mutex.h>
10 #include <linux/seq_file.h>
11 #include <linux/blk-mq.h>
12 #include <scsi/scsi.h>
13
14 struct request_queue;
15 struct block_device;
16 struct completion;
17 struct module;
18 struct scsi_cmnd;
19 struct scsi_device;
20 struct scsi_host_cmd_pool;
21 struct scsi_target;
22 struct Scsi_Host;
23 struct scsi_host_cmd_pool;
24 struct scsi_transport_template;
25 struct blk_queue_tags;
26
27
28 /*
29  * The various choices mean:
30  * NONE: Self evident.  Host adapter is not capable of scatter-gather.
31  * ALL:  Means that the host adapter module can do scatter-gather,
32  *       and that there is no limit to the size of the table to which
33  *       we scatter/gather data.  The value we set here is the maximum
34  *       single element sglist.  To use chained sglists, the adapter
35  *       has to set a value beyond ALL (and correctly use the chain
36  *       handling API.
37  * Anything else:  Indicates the maximum number of chains that can be
38  *       used in one scatter-gather request.
39  */
40 #define SG_NONE 0
41 #define SG_ALL  SG_CHUNK_SIZE
42
43 #define MODE_UNKNOWN 0x00
44 #define MODE_INITIATOR 0x01
45 #define MODE_TARGET 0x02
46
47 #define DISABLE_CLUSTERING 0
48 #define ENABLE_CLUSTERING 1
49
50 struct scsi_host_template {
51         struct module *module;
52         const char *name;
53
54         /*
55          * Used to initialize old-style drivers.  For new-style drivers
56          * just perform all work in your module initialization function.
57          *
58          * Status:  OBSOLETE
59          */
60         int (* detect)(struct scsi_host_template *);
61
62         /*
63          * Used as unload callback for hosts with old-style drivers.
64          *
65          * Status: OBSOLETE
66          */
67         int (* release)(struct Scsi_Host *);
68
69         /*
70          * The info function will return whatever useful information the
71          * developer sees fit.  If not provided, then the name field will
72          * be used instead.
73          *
74          * Status: OPTIONAL
75          */
76         const char *(* info)(struct Scsi_Host *);
77
78         /*
79          * Ioctl interface
80          *
81          * Status: OPTIONAL
82          */
83         int (* ioctl)(struct scsi_device *dev, int cmd, void __user *arg);
84
85
86 #ifdef CONFIG_COMPAT
87         /* 
88          * Compat handler. Handle 32bit ABI.
89          * When unknown ioctl is passed return -ENOIOCTLCMD.
90          *
91          * Status: OPTIONAL
92          */
93         int (* compat_ioctl)(struct scsi_device *dev, int cmd, void __user *arg);
94 #endif
95
96         /*
97          * The queuecommand function is used to queue up a scsi
98          * command block to the LLDD.  When the driver finished
99          * processing the command the done callback is invoked.
100          *
101          * If queuecommand returns 0, then the HBA has accepted the
102          * command.  The done() function must be called on the command
103          * when the driver has finished with it. (you may call done on the
104          * command before queuecommand returns, but in this case you
105          * *must* return 0 from queuecommand).
106          *
107          * Queuecommand may also reject the command, in which case it may
108          * not touch the command and must not call done() for it.
109          *
110          * There are two possible rejection returns:
111          *
112          *   SCSI_MLQUEUE_DEVICE_BUSY: Block this device temporarily, but
113          *   allow commands to other devices serviced by this host.
114          *
115          *   SCSI_MLQUEUE_HOST_BUSY: Block all devices served by this
116          *   host temporarily.
117          *
118          * For compatibility, any other non-zero return is treated the
119          * same as SCSI_MLQUEUE_HOST_BUSY.
120          *
121          * NOTE: "temporarily" means either until the next command for#
122          * this device/host completes, or a period of time determined by
123          * I/O pressure in the system if there are no other outstanding
124          * commands.
125          *
126          * STATUS: REQUIRED
127          */
128         int (* queuecommand)(struct Scsi_Host *, struct scsi_cmnd *);
129
130         /*
131          * This is an error handling strategy routine.  You don't need to
132          * define one of these if you don't want to - there is a default
133          * routine that is present that should work in most cases.  For those
134          * driver authors that have the inclination and ability to write their
135          * own strategy routine, this is where it is specified.  Note - the
136          * strategy routine is *ALWAYS* run in the context of the kernel eh
137          * thread.  Thus you are guaranteed to *NOT* be in an interrupt
138          * handler when you execute this, and you are also guaranteed to
139          * *NOT* have any other commands being queued while you are in the
140          * strategy routine. When you return from this function, operations
141          * return to normal.
142          *
143          * See scsi_error.c scsi_unjam_host for additional comments about
144          * what this function should and should not be attempting to do.
145          *
146          * Status: REQUIRED     (at least one of them)
147          */
148         int (* eh_abort_handler)(struct scsi_cmnd *);
149         int (* eh_device_reset_handler)(struct scsi_cmnd *);
150         int (* eh_target_reset_handler)(struct scsi_cmnd *);
151         int (* eh_bus_reset_handler)(struct scsi_cmnd *);
152         int (* eh_host_reset_handler)(struct scsi_cmnd *);
153
154         /*
155          * Before the mid layer attempts to scan for a new device where none
156          * currently exists, it will call this entry in your driver.  Should
157          * your driver need to allocate any structs or perform any other init
158          * items in order to send commands to a currently unused target/lun
159          * combo, then this is where you can perform those allocations.  This
160          * is specifically so that drivers won't have to perform any kind of
161          * "is this a new device" checks in their queuecommand routine,
162          * thereby making the hot path a bit quicker.
163          *
164          * Return values: 0 on success, non-0 on failure
165          *
166          * Deallocation:  If we didn't find any devices at this ID, you will
167          * get an immediate call to slave_destroy().  If we find something
168          * here then you will get a call to slave_configure(), then the
169          * device will be used for however long it is kept around, then when
170          * the device is removed from the system (or * possibly at reboot
171          * time), you will then get a call to slave_destroy().  This is
172          * assuming you implement slave_configure and slave_destroy.
173          * However, if you allocate memory and hang it off the device struct,
174          * then you must implement the slave_destroy() routine at a minimum
175          * in order to avoid leaking memory
176          * each time a device is tore down.
177          *
178          * Status: OPTIONAL
179          */
180         int (* slave_alloc)(struct scsi_device *);
181
182         /*
183          * Once the device has responded to an INQUIRY and we know the
184          * device is online, we call into the low level driver with the
185          * struct scsi_device *.  If the low level device driver implements
186          * this function, it *must* perform the task of setting the queue
187          * depth on the device.  All other tasks are optional and depend
188          * on what the driver supports and various implementation details.
189          * 
190          * Things currently recommended to be handled at this time include:
191          *
192          * 1.  Setting the device queue depth.  Proper setting of this is
193          *     described in the comments for scsi_change_queue_depth.
194          * 2.  Determining if the device supports the various synchronous
195          *     negotiation protocols.  The device struct will already have
196          *     responded to INQUIRY and the results of the standard items
197          *     will have been shoved into the various device flag bits, eg.
198          *     device->sdtr will be true if the device supports SDTR messages.
199          * 3.  Allocating command structs that the device will need.
200          * 4.  Setting the default timeout on this device (if needed).
201          * 5.  Anything else the low level driver might want to do on a device
202          *     specific setup basis...
203          * 6.  Return 0 on success, non-0 on error.  The device will be marked
204          *     as offline on error so that no access will occur.  If you return
205          *     non-0, your slave_destroy routine will never get called for this
206          *     device, so don't leave any loose memory hanging around, clean
207          *     up after yourself before returning non-0
208          *
209          * Status: OPTIONAL
210          */
211         int (* slave_configure)(struct scsi_device *);
212
213         /*
214          * Immediately prior to deallocating the device and after all activity
215          * has ceased the mid layer calls this point so that the low level
216          * driver may completely detach itself from the scsi device and vice
217          * versa.  The low level driver is responsible for freeing any memory
218          * it allocated in the slave_alloc or slave_configure calls. 
219          *
220          * Status: OPTIONAL
221          */
222         void (* slave_destroy)(struct scsi_device *);
223
224         /*
225          * Before the mid layer attempts to scan for a new device attached
226          * to a target where no target currently exists, it will call this
227          * entry in your driver.  Should your driver need to allocate any
228          * structs or perform any other init items in order to send commands
229          * to a currently unused target, then this is where you can perform
230          * those allocations.
231          *
232          * Return values: 0 on success, non-0 on failure
233          *
234          * Status: OPTIONAL
235          */
236         int (* target_alloc)(struct scsi_target *);
237
238         /*
239          * Immediately prior to deallocating the target structure, and
240          * after all activity to attached scsi devices has ceased, the
241          * midlayer calls this point so that the driver may deallocate
242          * and terminate any references to the target.
243          *
244          * Status: OPTIONAL
245          */
246         void (* target_destroy)(struct scsi_target *);
247
248         /*
249          * If a host has the ability to discover targets on its own instead
250          * of scanning the entire bus, it can fill in this function and
251          * call scsi_scan_host().  This function will be called periodically
252          * until it returns 1 with the scsi_host and the elapsed time of
253          * the scan in jiffies.
254          *
255          * Status: OPTIONAL
256          */
257         int (* scan_finished)(struct Scsi_Host *, unsigned long);
258
259         /*
260          * If the host wants to be called before the scan starts, but
261          * after the midlayer has set up ready for the scan, it can fill
262          * in this function.
263          *
264          * Status: OPTIONAL
265          */
266         void (* scan_start)(struct Scsi_Host *);
267
268         /*
269          * Fill in this function to allow the queue depth of this host
270          * to be changeable (on a per device basis).  Returns either
271          * the current queue depth setting (may be different from what
272          * was passed in) or an error.  An error should only be
273          * returned if the requested depth is legal but the driver was
274          * unable to set it.  If the requested depth is illegal, the
275          * driver should set and return the closest legal queue depth.
276          *
277          * Status: OPTIONAL
278          */
279         int (* change_queue_depth)(struct scsi_device *, int);
280
281         /*
282          * This functions lets the driver expose the queue mapping
283          * to the block layer.
284          *
285          * Status: OPTIONAL
286          */
287         int (* map_queues)(struct Scsi_Host *shost);
288
289         /*
290          * This function determines the BIOS parameters for a given
291          * harddisk.  These tend to be numbers that are made up by
292          * the host adapter.  Parameters:
293          * size, device, list (heads, sectors, cylinders)
294          *
295          * Status: OPTIONAL
296          */
297         int (* bios_param)(struct scsi_device *, struct block_device *,
298                         sector_t, int []);
299
300         /*
301          * This function is called when one or more partitions on the
302          * device reach beyond the end of the device.
303          *
304          * Status: OPTIONAL
305          */
306         void (*unlock_native_capacity)(struct scsi_device *);
307
308         /*
309          * Can be used to export driver statistics and other infos to the
310          * world outside the kernel ie. userspace and it also provides an
311          * interface to feed the driver with information.
312          *
313          * Status: OBSOLETE
314          */
315         int (*show_info)(struct seq_file *, struct Scsi_Host *);
316         int (*write_info)(struct Scsi_Host *, char *, int);
317
318         /*
319          * This is an optional routine that allows the transport to become
320          * involved when a scsi io timer fires. The return value tells the
321          * timer routine how to finish the io timeout handling:
322          * EH_HANDLED:          I fixed the error, please complete the command
323          * EH_RESET_TIMER:      I need more time, reset the timer and
324          *                      begin counting again
325          * EH_NOT_HANDLED       Begin normal error recovery
326          *
327          * Status: OPTIONAL
328          */
329         enum blk_eh_timer_return (*eh_timed_out)(struct scsi_cmnd *);
330
331         /* This is an optional routine that allows transport to initiate
332          * LLD adapter or firmware reset using sysfs attribute.
333          *
334          * Return values: 0 on success, -ve value on failure.
335          *
336          * Status: OPTIONAL
337          */
338
339         int (*host_reset)(struct Scsi_Host *shost, int reset_type);
340 #define SCSI_ADAPTER_RESET      1
341 #define SCSI_FIRMWARE_RESET     2
342
343
344         /*
345          * Name of proc directory
346          */
347         const char *proc_name;
348
349         /*
350          * Used to store the procfs directory if a driver implements the
351          * show_info method.
352          */
353         struct proc_dir_entry *proc_dir;
354
355         /*
356          * This determines if we will use a non-interrupt driven
357          * or an interrupt driven scheme.  It is set to the maximum number
358          * of simultaneous commands a given host adapter will accept.
359          */
360         int can_queue;
361
362         /*
363          * In many instances, especially where disconnect / reconnect are
364          * supported, our host also has an ID on the SCSI bus.  If this is
365          * the case, then it must be reserved.  Please set this_id to -1 if
366          * your setup is in single initiator mode, and the host lacks an
367          * ID.
368          */
369         int this_id;
370
371         /*
372          * This determines the degree to which the host adapter is capable
373          * of scatter-gather.
374          */
375         unsigned short sg_tablesize;
376         unsigned short sg_prot_tablesize;
377
378         /*
379          * Set this if the host adapter has limitations beside segment count.
380          */
381         unsigned int max_sectors;
382
383         /*
384          * DMA scatter gather segment boundary limit. A segment crossing this
385          * boundary will be split in two.
386          */
387         unsigned long dma_boundary;
388
389         /*
390          * This specifies "machine infinity" for host templates which don't
391          * limit the transfer size.  Note this limit represents an absolute
392          * maximum, and may be over the transfer limits allowed for
393          * individual devices (e.g. 256 for SCSI-1).
394          */
395 #define SCSI_DEFAULT_MAX_SECTORS        1024
396
397         /*
398          * True if this host adapter can make good use of linked commands.
399          * This will allow more than one command to be queued to a given
400          * unit on a given host.  Set this to the maximum number of command
401          * blocks to be provided for each device.  Set this to 1 for one
402          * command block per lun, 2 for two, etc.  Do not set this to 0.
403          * You should make sure that the host adapter will do the right thing
404          * before you try setting this above 1.
405          */
406         short cmd_per_lun;
407
408         /*
409          * present contains counter indicating how many boards of this
410          * type were found when we did the scan.
411          */
412         unsigned char present;
413
414         /* If use block layer to manage tags, this is tag allocation policy */
415         int tag_alloc_policy;
416
417         /*
418          * Track QUEUE_FULL events and reduce queue depth on demand.
419          */
420         unsigned track_queue_depth:1;
421
422         /*
423          * This specifies the mode that a LLD supports.
424          */
425         unsigned supported_mode:2;
426
427         /*
428          * True if this host adapter uses unchecked DMA onto an ISA bus.
429          */
430         unsigned unchecked_isa_dma:1;
431
432         /*
433          * True if this host adapter can make good use of clustering.
434          * I originally thought that if the tablesize was large that it
435          * was a waste of CPU cycles to prepare a cluster list, but
436          * it works out that the Buslogic is faster if you use a smaller
437          * number of segments (i.e. use clustering).  I guess it is
438          * inefficient.
439          */
440         unsigned use_clustering:1;
441
442         /*
443          * True for emulated SCSI host adapters (e.g. ATAPI).
444          */
445         unsigned emulated:1;
446
447         /*
448          * True if the low-level driver performs its own reset-settle delays.
449          */
450         unsigned skip_settle_delay:1;
451
452         /* True if the controller does not support WRITE SAME */
453         unsigned no_write_same:1;
454
455         /*
456          * Countdown for host blocking with no commands outstanding.
457          */
458         unsigned int max_host_blocked;
459
460         /*
461          * Default value for the blocking.  If the queue is empty,
462          * host_blocked counts down in the request_fn until it restarts
463          * host operations as zero is reached.  
464          *
465          * FIXME: This should probably be a value in the template
466          */
467 #define SCSI_DEFAULT_HOST_BLOCKED       7
468
469         /*
470          * Pointer to the sysfs class properties for this host, NULL terminated.
471          */
472         struct device_attribute **shost_attrs;
473
474         /*
475          * Pointer to the SCSI device properties for this host, NULL terminated.
476          */
477         struct device_attribute **sdev_attrs;
478
479         /*
480          * List of hosts per template.
481          *
482          * This is only for use by scsi_module.c for legacy templates.
483          * For these access to it is synchronized implicitly by
484          * module_init/module_exit.
485          */
486         struct list_head legacy_hosts;
487
488         /*
489          * Vendor Identifier associated with the host
490          *
491          * Note: When specifying vendor_id, be sure to read the
492          *   Vendor Type and ID formatting requirements specified in
493          *   scsi_netlink.h
494          */
495         u64 vendor_id;
496
497         /*
498          * Additional per-command data allocated for the driver.
499          */
500         unsigned int cmd_size;
501         struct scsi_host_cmd_pool *cmd_pool;
502 };
503
504 /*
505  * Temporary #define for host lock push down. Can be removed when all
506  * drivers have been updated to take advantage of unlocked
507  * queuecommand.
508  *
509  */
510 #define DEF_SCSI_QCMD(func_name) \
511         int func_name(struct Scsi_Host *shost, struct scsi_cmnd *cmd)   \
512         {                                                               \
513                 unsigned long irq_flags;                                \
514                 int rc;                                                 \
515                 spin_lock_irqsave(shost->host_lock, irq_flags);         \
516                 scsi_cmd_get_serial(shost, cmd);                        \
517                 rc = func_name##_lck (cmd, cmd->scsi_done);                     \
518                 spin_unlock_irqrestore(shost->host_lock, irq_flags);    \
519                 return rc;                                              \
520         }
521
522
523 /*
524  * shost state: If you alter this, you also need to alter scsi_sysfs.c
525  * (for the ascii descriptions) and the state model enforcer:
526  * scsi_host_set_state()
527  */
528 enum scsi_host_state {
529         SHOST_CREATED = 1,
530         SHOST_RUNNING,
531         SHOST_CANCEL,
532         SHOST_DEL,
533         SHOST_RECOVERY,
534         SHOST_CANCEL_RECOVERY,
535         SHOST_DEL_RECOVERY,
536 };
537
538 struct Scsi_Host {
539         /*
540          * __devices is protected by the host_lock, but you should
541          * usually use scsi_device_lookup / shost_for_each_device
542          * to access it and don't care about locking yourself.
543          * In the rare case of being in irq context you can use
544          * their __ prefixed variants with the lock held. NEVER
545          * access this list directly from a driver.
546          */
547         struct list_head        __devices;
548         struct list_head        __targets;
549         
550         struct list_head        starved_list;
551
552         spinlock_t              default_lock;
553         spinlock_t              *host_lock;
554
555         struct mutex            scan_mutex;/* serialize scanning activity */
556
557         struct list_head        eh_cmd_q;
558         struct task_struct    * ehandler;  /* Error recovery thread. */
559         struct completion     * eh_action; /* Wait for specific actions on the
560                                               host. */
561         wait_queue_head_t       host_wait;
562         struct scsi_host_template *hostt;
563         struct scsi_transport_template *transportt;
564
565         /*
566          * Area to keep a shared tag map (if needed, will be
567          * NULL if not).
568          */
569         union {
570                 struct blk_queue_tag    *bqt;
571                 struct blk_mq_tag_set   tag_set;
572         };
573
574         atomic_t host_busy;                /* commands actually active on low-level */
575         atomic_t host_blocked;
576
577         unsigned int host_failed;          /* commands that failed.
578                                               protected by host_lock */
579         unsigned int host_eh_scheduled;    /* EH scheduled without command */
580     
581         unsigned int host_no;  /* Used for IOCTL_GET_IDLUN, /proc/scsi et al. */
582
583         /* next two fields are used to bound the time spent in error handling */
584         int eh_deadline;
585         unsigned long last_reset;
586
587
588         /*
589          * These three parameters can be used to allow for wide scsi,
590          * and for host adapters that support multiple busses
591          * The last two should be set to 1 more than the actual max id
592          * or lun (e.g. 8 for SCSI parallel systems).
593          */
594         unsigned int max_channel;
595         unsigned int max_id;
596         u64 max_lun;
597
598         /*
599          * This is a unique identifier that must be assigned so that we
600          * have some way of identifying each detected host adapter properly
601          * and uniquely.  For hosts that do not support more than one card
602          * in the system at one time, this does not need to be set.  It is
603          * initialized to 0 in scsi_register.
604          */
605         unsigned int unique_id;
606
607         /*
608          * The maximum length of SCSI commands that this host can accept.
609          * Probably 12 for most host adapters, but could be 16 for others.
610          * or 260 if the driver supports variable length cdbs.
611          * For drivers that don't set this field, a value of 12 is
612          * assumed.
613          */
614         unsigned short max_cmd_len;
615
616         int this_id;
617         int can_queue;
618         short cmd_per_lun;
619         short unsigned int sg_tablesize;
620         short unsigned int sg_prot_tablesize;
621         unsigned int max_sectors;
622         unsigned long dma_boundary;
623         /*
624          * In scsi-mq mode, the number of hardware queues supported by the LLD.
625          *
626          * Note: it is assumed that each hardware queue has a queue depth of
627          * can_queue. In other words, the total queue depth per host
628          * is nr_hw_queues * can_queue.
629          */
630         unsigned nr_hw_queues;
631         /* 
632          * Used to assign serial numbers to the cmds.
633          * Protected by the host lock.
634          */
635         unsigned long cmd_serial_number;
636         
637         unsigned active_mode:2;
638         unsigned unchecked_isa_dma:1;
639         unsigned use_clustering:1;
640
641         /*
642          * Host has requested that no further requests come through for the
643          * time being.
644          */
645         unsigned host_self_blocked:1;
646     
647         /*
648          * Host uses correct SCSI ordering not PC ordering. The bit is
649          * set for the minority of drivers whose authors actually read
650          * the spec ;).
651          */
652         unsigned reverse_ordering:1;
653
654         /* Task mgmt function in progress */
655         unsigned tmf_in_progress:1;
656
657         /* Asynchronous scan in progress */
658         unsigned async_scan:1;
659
660         /* Don't resume host in EH */
661         unsigned eh_noresume:1;
662
663         /* The controller does not support WRITE SAME */
664         unsigned no_write_same:1;
665
666         unsigned use_blk_mq:1;
667         unsigned use_cmd_list:1;
668
669         /* Host responded with short (<36 bytes) INQUIRY result */
670         unsigned short_inquiry:1;
671
672         /*
673          * Optional work queue to be utilized by the transport
674          */
675         char work_q_name[20];
676         struct workqueue_struct *work_q;
677
678         /*
679          * Task management function work queue
680          */
681         struct workqueue_struct *tmf_work_q;
682
683         /* The transport requires the LUN bits NOT to be stored in CDB[1] */
684         unsigned no_scsi2_lun_in_cdb:1;
685
686         /*
687          * Value host_blocked counts down from
688          */
689         unsigned int max_host_blocked;
690
691         /* Protection Information */
692         unsigned int prot_capabilities;
693         unsigned char prot_guard_type;
694
695         /* legacy crap */
696         unsigned long base;
697         unsigned long io_port;
698         unsigned char n_io_port;
699         unsigned char dma_channel;
700         unsigned int  irq;
701         
702
703         enum scsi_host_state shost_state;
704
705         /* ldm bits */
706         struct device           shost_gendev, shost_dev;
707
708         /*
709          * List of hosts per template.
710          *
711          * This is only for use by scsi_module.c for legacy templates.
712          * For these access to it is synchronized implicitly by
713          * module_init/module_exit.
714          */
715         struct list_head sht_legacy_list;
716
717         /*
718          * Points to the transport data (if any) which is allocated
719          * separately
720          */
721         void *shost_data;
722
723         /*
724          * Points to the physical bus device we'd use to do DMA
725          * Needed just in case we have virtual hosts.
726          */
727         struct device *dma_dev;
728
729         /*
730          * We should ensure that this is aligned, both for better performance
731          * and also because some compilers (m68k) don't automatically force
732          * alignment to a long boundary.
733          */
734         unsigned long hostdata[0]  /* Used for storage of host specific stuff */
735                 __attribute__ ((aligned (sizeof(unsigned long))));
736 };
737
738 #define         class_to_shost(d)       \
739         container_of(d, struct Scsi_Host, shost_dev)
740
741 #define shost_printk(prefix, shost, fmt, a...)  \
742         dev_printk(prefix, &(shost)->shost_gendev, fmt, ##a)
743
744 static inline void *shost_priv(struct Scsi_Host *shost)
745 {
746         return (void *)shost->hostdata;
747 }
748
749 int scsi_is_host_device(const struct device *);
750
751 static inline struct Scsi_Host *dev_to_shost(struct device *dev)
752 {
753         while (!scsi_is_host_device(dev)) {
754                 if (!dev->parent)
755                         return NULL;
756                 dev = dev->parent;
757         }
758         return container_of(dev, struct Scsi_Host, shost_gendev);
759 }
760
761 static inline int scsi_host_in_recovery(struct Scsi_Host *shost)
762 {
763         return shost->shost_state == SHOST_RECOVERY ||
764                 shost->shost_state == SHOST_CANCEL_RECOVERY ||
765                 shost->shost_state == SHOST_DEL_RECOVERY ||
766                 shost->tmf_in_progress;
767 }
768
769 static inline bool shost_use_blk_mq(struct Scsi_Host *shost)
770 {
771         return shost->use_blk_mq;
772 }
773
774 extern int scsi_queue_work(struct Scsi_Host *, struct work_struct *);
775 extern void scsi_flush_work(struct Scsi_Host *);
776
777 extern struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *, int);
778 extern int __must_check scsi_add_host_with_dma(struct Scsi_Host *,
779                                                struct device *,
780                                                struct device *);
781 extern void scsi_scan_host(struct Scsi_Host *);
782 extern void scsi_rescan_device(struct device *);
783 extern void scsi_remove_host(struct Scsi_Host *);
784 extern struct Scsi_Host *scsi_host_get(struct Scsi_Host *);
785 extern void scsi_host_put(struct Scsi_Host *t);
786 extern struct Scsi_Host *scsi_host_lookup(unsigned short);
787 extern const char *scsi_host_state_name(enum scsi_host_state);
788 extern void scsi_cmd_get_serial(struct Scsi_Host *, struct scsi_cmnd *);
789
790 static inline int __must_check scsi_add_host(struct Scsi_Host *host,
791                                              struct device *dev)
792 {
793         return scsi_add_host_with_dma(host, dev, dev);
794 }
795
796 static inline struct device *scsi_get_device(struct Scsi_Host *shost)
797 {
798         return shost->shost_gendev.parent;
799 }
800
801 /**
802  * scsi_host_scan_allowed - Is scanning of this host allowed
803  * @shost:      Pointer to Scsi_Host.
804  **/
805 static inline int scsi_host_scan_allowed(struct Scsi_Host *shost)
806 {
807         return shost->shost_state == SHOST_RUNNING ||
808                shost->shost_state == SHOST_RECOVERY;
809 }
810
811 extern void scsi_unblock_requests(struct Scsi_Host *);
812 extern void scsi_block_requests(struct Scsi_Host *);
813
814 struct class_container;
815
816 /*
817  * These two functions are used to allocate and free a pseudo device
818  * which will connect to the host adapter itself rather than any
819  * physical device.  You must deallocate when you are done with the
820  * thing.  This physical pseudo-device isn't real and won't be available
821  * from any high-level drivers.
822  */
823 extern void scsi_free_host_dev(struct scsi_device *);
824 extern struct scsi_device *scsi_get_host_dev(struct Scsi_Host *);
825
826 /*
827  * DIF defines the exchange of protection information between
828  * initiator and SBC block device.
829  *
830  * DIX defines the exchange of protection information between OS and
831  * initiator.
832  */
833 enum scsi_host_prot_capabilities {
834         SHOST_DIF_TYPE1_PROTECTION = 1 << 0, /* T10 DIF Type 1 */
835         SHOST_DIF_TYPE2_PROTECTION = 1 << 1, /* T10 DIF Type 2 */
836         SHOST_DIF_TYPE3_PROTECTION = 1 << 2, /* T10 DIF Type 3 */
837
838         SHOST_DIX_TYPE0_PROTECTION = 1 << 3, /* DIX between OS and HBA only */
839         SHOST_DIX_TYPE1_PROTECTION = 1 << 4, /* DIX with DIF Type 1 */
840         SHOST_DIX_TYPE2_PROTECTION = 1 << 5, /* DIX with DIF Type 2 */
841         SHOST_DIX_TYPE3_PROTECTION = 1 << 6, /* DIX with DIF Type 3 */
842 };
843
844 /*
845  * SCSI hosts which support the Data Integrity Extensions must
846  * indicate their capabilities by setting the prot_capabilities using
847  * this call.
848  */
849 static inline void scsi_host_set_prot(struct Scsi_Host *shost, unsigned int mask)
850 {
851         shost->prot_capabilities = mask;
852 }
853
854 static inline unsigned int scsi_host_get_prot(struct Scsi_Host *shost)
855 {
856         return shost->prot_capabilities;
857 }
858
859 static inline int scsi_host_prot_dma(struct Scsi_Host *shost)
860 {
861         return shost->prot_capabilities >= SHOST_DIX_TYPE0_PROTECTION;
862 }
863
864 static inline unsigned int scsi_host_dif_capable(struct Scsi_Host *shost, unsigned int target_type)
865 {
866         static unsigned char cap[] = { 0,
867                                        SHOST_DIF_TYPE1_PROTECTION,
868                                        SHOST_DIF_TYPE2_PROTECTION,
869                                        SHOST_DIF_TYPE3_PROTECTION };
870
871         if (target_type >= ARRAY_SIZE(cap))
872                 return 0;
873
874         return shost->prot_capabilities & cap[target_type] ? target_type : 0;
875 }
876
877 static inline unsigned int scsi_host_dix_capable(struct Scsi_Host *shost, unsigned int target_type)
878 {
879 #if defined(CONFIG_BLK_DEV_INTEGRITY)
880         static unsigned char cap[] = { SHOST_DIX_TYPE0_PROTECTION,
881                                        SHOST_DIX_TYPE1_PROTECTION,
882                                        SHOST_DIX_TYPE2_PROTECTION,
883                                        SHOST_DIX_TYPE3_PROTECTION };
884
885         if (target_type >= ARRAY_SIZE(cap))
886                 return 0;
887
888         return shost->prot_capabilities & cap[target_type];
889 #endif
890         return 0;
891 }
892
893 /*
894  * All DIX-capable initiators must support the T10-mandated CRC
895  * checksum.  Controllers can optionally implement the IP checksum
896  * scheme which has much lower impact on system performance.  Note
897  * that the main rationale for the checksum is to match integrity
898  * metadata with data.  Detecting bit errors are a job for ECC memory
899  * and buses.
900  */
901
902 enum scsi_host_guard_type {
903         SHOST_DIX_GUARD_CRC = 1 << 0,
904         SHOST_DIX_GUARD_IP  = 1 << 1,
905 };
906
907 static inline void scsi_host_set_guard(struct Scsi_Host *shost, unsigned char type)
908 {
909         shost->prot_guard_type = type;
910 }
911
912 static inline unsigned char scsi_host_get_guard(struct Scsi_Host *shost)
913 {
914         return shost->prot_guard_type;
915 }
916
917 /* legacy interfaces */
918 extern struct Scsi_Host *scsi_register(struct scsi_host_template *, int);
919 extern void scsi_unregister(struct Scsi_Host *);
920 extern int scsi_host_set_state(struct Scsi_Host *, enum scsi_host_state);
921
922 #endif /* _SCSI_SCSI_HOST_H */