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