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