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