1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _SCSI_SCSI_DEVICE_H
3 #define _SCSI_SCSI_DEVICE_H
4 
5 #include <linux/list.h>
6 #include <linux/spinlock.h>
7 #include <linux/workqueue.h>
8 #include <linux/blk-mq.h>
9 #include <scsi/scsi.h>
10 #include <linux/atomic.h>
11 #include <linux/sbitmap.h>
12 #include <linux/android_kabi.h>
13 
14 struct bsg_device;
15 struct device;
16 struct request_queue;
17 struct scsi_cmnd;
18 struct scsi_lun;
19 struct scsi_sense_hdr;
20 
21 typedef __u64 __bitwise blist_flags_t;
22 
23 #define SCSI_SENSE_BUFFERSIZE	96
24 
25 struct scsi_mode_data {
26 	__u32	length;
27 	__u16	block_descriptor_length;
28 	__u8	medium_type;
29 	__u8	device_specific;
30 	__u8	header_length;
31 	__u8	longlba:1;
32 };
33 
34 /*
35  * sdev state: If you alter this, you also need to alter scsi_sysfs.c
36  * (for the ascii descriptions) and the state model enforcer:
37  * scsi_lib:scsi_device_set_state().
38  */
39 enum scsi_device_state {
40 	SDEV_CREATED = 1,	/* device created but not added to sysfs
41 				 * Only internal commands allowed (for inq) */
42 	SDEV_RUNNING,		/* device properly configured
43 				 * All commands allowed */
44 	SDEV_CANCEL,		/* beginning to delete device
45 				 * Only error handler commands allowed */
46 	SDEV_DEL,		/* device deleted
47 				 * no commands allowed */
48 	SDEV_QUIESCE,		/* Device quiescent.  No block commands
49 				 * will be accepted, only specials (which
50 				 * originate in the mid-layer) */
51 	SDEV_OFFLINE,		/* Device offlined (by error handling or
52 				 * user request */
53 	SDEV_TRANSPORT_OFFLINE,	/* Offlined by transport class error handler */
54 	SDEV_BLOCK,		/* Device blocked by scsi lld.  No
55 				 * scsi commands from user or midlayer
56 				 * should be issued to the scsi
57 				 * lld. */
58 	SDEV_CREATED_BLOCK,	/* same as above but for created devices */
59 };
60 
61 enum scsi_scan_mode {
62 	SCSI_SCAN_INITIAL = 0,
63 	SCSI_SCAN_RESCAN,
64 	SCSI_SCAN_MANUAL,
65 };
66 
67 enum scsi_device_event {
68 	SDEV_EVT_MEDIA_CHANGE	= 1,	/* media has changed */
69 	SDEV_EVT_INQUIRY_CHANGE_REPORTED,		/* 3F 03  UA reported */
70 	SDEV_EVT_CAPACITY_CHANGE_REPORTED,		/* 2A 09  UA reported */
71 	SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED,	/* 38 07  UA reported */
72 	SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED,	/* 2A 01  UA reported */
73 	SDEV_EVT_LUN_CHANGE_REPORTED,			/* 3F 0E  UA reported */
74 	SDEV_EVT_ALUA_STATE_CHANGE_REPORTED,		/* 2A 06  UA reported */
75 	SDEV_EVT_POWER_ON_RESET_OCCURRED,		/* 29 00  UA reported */
76 
77 	SDEV_EVT_FIRST		= SDEV_EVT_MEDIA_CHANGE,
78 	SDEV_EVT_LAST		= SDEV_EVT_POWER_ON_RESET_OCCURRED,
79 
80 	SDEV_EVT_MAXBITS	= SDEV_EVT_LAST + 1
81 };
82 
83 struct scsi_event {
84 	enum scsi_device_event	evt_type;
85 	struct list_head	node;
86 
87 	/* put union of data structures, for non-simple event types,
88 	 * here
89 	 */
90 };
91 
92 /**
93  * struct scsi_vpd - SCSI Vital Product Data
94  * @rcu: For kfree_rcu().
95  * @len: Length in bytes of @data.
96  * @data: VPD data as defined in various T10 SCSI standard documents.
97  */
98 struct scsi_vpd {
99 	struct rcu_head	rcu;
100 	int		len;
101 	unsigned char	data[];
102 };
103 
104 struct scsi_device {
105 	struct Scsi_Host *host;
106 	struct request_queue *request_queue;
107 
108 	/* the next two are protected by the host->host_lock */
109 	struct list_head    siblings;   /* list of all devices on this host */
110 	struct list_head    same_target_siblings; /* just the devices sharing same target id */
111 
112 	struct sbitmap budget_map;
113 	atomic_t device_blocked;	/* Device returned QUEUE_FULL. */
114 
115 	atomic_t restarts;
116 	spinlock_t list_lock;
117 	struct list_head starved_entry;
118 	unsigned short queue_depth;	/* How deep of a queue we want */
119 	unsigned short max_queue_depth;	/* max queue depth */
120 	unsigned short last_queue_full_depth; /* These two are used by */
121 	unsigned short last_queue_full_count; /* scsi_track_queue_full() */
122 	unsigned long last_queue_full_time;	/* last queue full time */
123 	unsigned long queue_ramp_up_period;	/* ramp up period in jiffies */
124 #define SCSI_DEFAULT_RAMP_UP_PERIOD	(120 * HZ)
125 
126 	unsigned long last_queue_ramp_up;	/* last queue ramp up time */
127 
128 	unsigned int id, channel;
129 	u64 lun;
130 	unsigned int manufacturer;	/* Manufacturer of device, for using
131 					 * vendor-specific cmd's */
132 	unsigned sector_size;	/* size in bytes */
133 
134 	void *hostdata;		/* available to low-level driver */
135 	unsigned char type;
136 	char scsi_level;
137 	char inq_periph_qual;	/* PQ from INQUIRY data */
138 	struct mutex inquiry_mutex;
139 	unsigned char inquiry_len;	/* valid bytes in 'inquiry' */
140 	unsigned char * inquiry;	/* INQUIRY response data */
141 	const char * vendor;		/* [back_compat] point into 'inquiry' ... */
142 	const char * model;		/* ... after scan; point to static string */
143 	const char * rev;		/* ... "nullnullnullnull" before scan */
144 
145 #define SCSI_DEFAULT_VPD_LEN	255	/* default SCSI VPD page size (max) */
146 	struct scsi_vpd __rcu *vpd_pg0;
147 	struct scsi_vpd __rcu *vpd_pg83;
148 	struct scsi_vpd __rcu *vpd_pg80;
149 	struct scsi_vpd __rcu *vpd_pg89;
150 	struct scsi_vpd __rcu *vpd_pgb0;
151 	struct scsi_vpd __rcu *vpd_pgb1;
152 	struct scsi_vpd __rcu *vpd_pgb2;
153 	struct scsi_vpd __rcu *vpd_pgb7;
154 
155 	struct scsi_target      *sdev_target;
156 
157 	blist_flags_t		sdev_bflags; /* black/white flags as also found in
158 				 * scsi_devinfo.[hc]. For now used only to
159 				 * pass settings from slave_alloc to scsi
160 				 * core. */
161 	unsigned int eh_timeout; /* Error handling timeout */
162 
163 	/*
164 	 * If true, let the high-level device driver (sd) manage the device
165 	 * power state for system suspend/resume (suspend to RAM and
166 	 * hibernation) operations.
167 	 */
168 	unsigned manage_system_start_stop:1;
169 
170 	/*
171 	 * If true, let the high-level device driver (sd) manage the device
172 	 * power state for runtime device suspand and resume operations.
173 	 */
174 	unsigned manage_runtime_start_stop:1;
175 
176 	/*
177 	 * If true, let the high-level device driver (sd) manage the device
178 	 * power state for system shutdown (power off) operations.
179 	 */
180 	unsigned manage_shutdown:1;
181 
182 	/*
183 	 * If set and if the device is runtime suspended, ask the high-level
184 	 * device driver (sd) to force a runtime resume of the device.
185 	 */
186 	unsigned force_runtime_start_on_system_start:1;
187 
188 	unsigned removable:1;
189 	unsigned changed:1;	/* Data invalid due to media change */
190 	unsigned busy:1;	/* Used to prevent races */
191 	unsigned lockable:1;	/* Able to prevent media removal */
192 	unsigned locked:1;      /* Media removal disabled */
193 	unsigned borken:1;	/* Tell the Seagate driver to be
194 				 * painfully slow on this device */
195 	unsigned disconnect:1;	/* can disconnect */
196 	unsigned soft_reset:1;	/* Uses soft reset option */
197 	unsigned sdtr:1;	/* Device supports SDTR messages */
198 	unsigned wdtr:1;	/* Device supports WDTR messages */
199 	unsigned ppr:1;		/* Device supports PPR messages */
200 	unsigned tagged_supported:1;	/* Supports SCSI-II tagged queuing */
201 	unsigned simple_tags:1;	/* simple queue tag messages are enabled */
202 	unsigned was_reset:1;	/* There was a bus reset on the bus for
203 				 * this device */
204 	unsigned expecting_cc_ua:1; /* Expecting a CHECK_CONDITION/UNIT_ATTN
205 				     * because we did a bus reset. */
206 	unsigned use_10_for_rw:1; /* first try 10-byte read / write */
207 	unsigned use_10_for_ms:1; /* first try 10-byte mode sense/select */
208 	unsigned set_dbd_for_ms:1; /* Set "DBD" field in mode sense */
209 	unsigned read_before_ms:1;	/* perform a READ before MODE SENSE */
210 	unsigned no_report_opcodes:1;	/* no REPORT SUPPORTED OPERATION CODES */
211 	unsigned no_write_same:1;	/* no WRITE SAME command */
212 	unsigned use_16_for_rw:1; /* Use read/write(16) over read/write(10) */
213 	unsigned use_16_for_sync:1;	/* Use sync (16) over sync (10) */
214 	unsigned skip_ms_page_8:1;	/* do not use MODE SENSE page 0x08 */
215 	unsigned skip_ms_page_3f:1;	/* do not use MODE SENSE page 0x3f */
216 	unsigned skip_vpd_pages:1;	/* do not read VPD pages */
217 	unsigned try_vpd_pages:1;	/* attempt to read VPD pages */
218 	unsigned use_192_bytes_for_3f:1; /* ask for 192 bytes from page 0x3f */
219 	unsigned no_start_on_add:1;	/* do not issue start on add */
220 	unsigned allow_restart:1; /* issue START_UNIT in error handler */
221 	unsigned start_stop_pwr_cond:1;	/* Set power cond. in START_STOP_UNIT */
222 	unsigned no_uld_attach:1; /* disable connecting to upper level drivers */
223 	unsigned select_no_atn:1;
224 	unsigned fix_capacity:1;	/* READ_CAPACITY is too high by 1 */
225 	unsigned guess_capacity:1;	/* READ_CAPACITY might be too high by 1 */
226 	unsigned retry_hwerror:1;	/* Retry HARDWARE_ERROR */
227 	unsigned last_sector_bug:1;	/* do not use multisector accesses on
228 					   SD_LAST_BUGGY_SECTORS */
229 	unsigned no_read_disc_info:1;	/* Avoid READ_DISC_INFO cmds */
230 	unsigned no_read_capacity_16:1; /* Avoid READ_CAPACITY_16 cmds */
231 	unsigned try_rc_10_first:1;	/* Try READ_CAPACACITY_10 first */
232 	unsigned security_supported:1;	/* Supports Security Protocols */
233 	unsigned is_visible:1;	/* is the device visible in sysfs */
234 	unsigned wce_default_on:1;	/* Cache is ON by default */
235 	unsigned no_dif:1;	/* T10 PI (DIF) should be disabled */
236 	unsigned broken_fua:1;		/* Don't set FUA bit */
237 	unsigned lun_in_cdb:1;		/* Store LUN bits in CDB[1] */
238 	unsigned unmap_limit_for_ws:1;	/* Use the UNMAP limit for WRITE SAME */
239 	unsigned rpm_autosuspend:1;	/* Enable runtime autosuspend at device
240 					 * creation time */
241 	unsigned ignore_media_change:1; /* Ignore MEDIA CHANGE on resume */
242 	unsigned silence_suspend:1;	/* Do not print runtime PM related messages */
243 	unsigned no_vpd_size:1;		/* No VPD size reported in header */
244 
245 	unsigned cdl_supported:1;	/* Command duration limits supported */
246 	unsigned cdl_enable:1;		/* Enable/disable Command duration limits */
247 
248 	unsigned int queue_stopped;	/* request queue is quiesced */
249 	bool offline_already;		/* Device offline message logged */
250 
251 	atomic_t disk_events_disable_depth; /* disable depth for disk events */
252 
253 	DECLARE_BITMAP(supported_events, SDEV_EVT_MAXBITS); /* supported events */
254 	DECLARE_BITMAP(pending_events, SDEV_EVT_MAXBITS); /* pending events */
255 	struct list_head event_list;	/* asserted events */
256 	struct work_struct event_work;
257 
258 	unsigned int max_device_blocked; /* what device_blocked counts down from  */
259 #define SCSI_DEFAULT_DEVICE_BLOCKED	3
260 
261 	atomic_t iorequest_cnt;
262 	atomic_t iodone_cnt;
263 	atomic_t ioerr_cnt;
264 	atomic_t iotmo_cnt;
265 
266 	struct device		sdev_gendev,
267 				sdev_dev;
268 
269 	struct work_struct	requeue_work;
270 
271 	struct scsi_device_handler *handler;
272 	void			*handler_data;
273 
274 	size_t			dma_drain_len;
275 	void			*dma_drain_buf;
276 
277 	unsigned int		sg_timeout;
278 	unsigned int		sg_reserved_size;
279 
280 	struct bsg_device	*bsg_dev;
281 	unsigned char		access_state;
282 	struct mutex		state_mutex;
283 	enum scsi_device_state sdev_state;
284 	struct task_struct	*quiesced_by;
285 
286 	ANDROID_KABI_RESERVE(1);
287 	ANDROID_KABI_RESERVE(2);
288 	ANDROID_KABI_RESERVE(3);
289 	ANDROID_KABI_RESERVE(4);
290 
291 	unsigned long		sdev_data[];
292 } __attribute__((aligned(sizeof(unsigned long))));
293 
294 #define	to_scsi_device(d)	\
295 	container_of(d, struct scsi_device, sdev_gendev)
296 #define	class_to_sdev(d)	\
297 	container_of(d, struct scsi_device, sdev_dev)
298 #define transport_class_to_sdev(class_dev) \
299 	to_scsi_device(class_dev->parent)
300 
301 #define sdev_dbg(sdev, fmt, a...) \
302 	dev_dbg(&(sdev)->sdev_gendev, fmt, ##a)
303 
304 /*
305  * like scmd_printk, but the device name is passed in
306  * as a string pointer
307  */
308 __printf(4, 5) void
309 sdev_prefix_printk(const char *, const struct scsi_device *, const char *,
310 		const char *, ...);
311 
312 #define sdev_printk(l, sdev, fmt, a...)				\
313 	sdev_prefix_printk(l, sdev, NULL, fmt, ##a)
314 
315 __printf(3, 4) void
316 scmd_printk(const char *, const struct scsi_cmnd *, const char *, ...);
317 
318 #define scmd_dbg(scmd, fmt, a...)					\
319 	do {								\
320 		struct request *__rq = scsi_cmd_to_rq((scmd));		\
321 									\
322 		if (__rq->q->disk)					\
323 			sdev_dbg((scmd)->device, "[%s] " fmt,		\
324 				 __rq->q->disk->disk_name, ##a);	\
325 		else							\
326 			sdev_dbg((scmd)->device, fmt, ##a);		\
327 	} while (0)
328 
329 enum scsi_target_state {
330 	STARGET_CREATED = 1,
331 	STARGET_RUNNING,
332 	STARGET_REMOVE,
333 	STARGET_CREATED_REMOVE,
334 	STARGET_DEL,
335 };
336 
337 /*
338  * scsi_target: representation of a scsi target, for now, this is only
339  * used for single_lun devices. If no one has active IO to the target,
340  * starget_sdev_user is NULL, else it points to the active sdev.
341  */
342 struct scsi_target {
343 	struct scsi_device	*starget_sdev_user;
344 	struct list_head	siblings;
345 	struct list_head	devices;
346 	struct device		dev;
347 	struct kref		reap_ref; /* last put renders target invisible */
348 	unsigned int		channel;
349 	unsigned int		id; /* target id ... replace
350 				     * scsi_device.id eventually */
351 	unsigned int		create:1; /* signal that it needs to be added */
352 	unsigned int		single_lun:1;	/* Indicates we should only
353 						 * allow I/O to one of the luns
354 						 * for the device at a time. */
355 	unsigned int		pdt_1f_for_no_lun:1;	/* PDT = 0x1f
356 						 * means no lun present. */
357 	unsigned int		no_report_luns:1;	/* Don't use
358 						 * REPORT LUNS for scanning. */
359 	unsigned int		expecting_lun_change:1;	/* A device has reported
360 						 * a 3F/0E UA, other devices on
361 						 * the same target will also. */
362 	/* commands actually active on LLD. */
363 	atomic_t		target_busy;
364 	atomic_t		target_blocked;
365 
366 	/*
367 	 * LLDs should set this in the slave_alloc host template callout.
368 	 * If set to zero then there is not limit.
369 	 */
370 	unsigned int		can_queue;
371 	unsigned int		max_target_blocked;
372 #define SCSI_DEFAULT_TARGET_BLOCKED	3
373 
374 	char			scsi_level;
375 	enum scsi_target_state	state;
376 	void 			*hostdata; /* available to low-level driver */
377 	unsigned long		starget_data[]; /* for the transport */
378 	/* starget_data must be the last element!!!! */
379 } __attribute__((aligned(sizeof(unsigned long))));
380 
381 #define to_scsi_target(d)	container_of(d, struct scsi_target, dev)
scsi_target(struct scsi_device * sdev)382 static inline struct scsi_target *scsi_target(struct scsi_device *sdev)
383 {
384 	return to_scsi_target(sdev->sdev_gendev.parent);
385 }
386 #define transport_class_to_starget(class_dev) \
387 	to_scsi_target(class_dev->parent)
388 
389 #define starget_printk(prefix, starget, fmt, a...)	\
390 	dev_printk(prefix, &(starget)->dev, fmt, ##a)
391 
392 extern struct scsi_device *__scsi_add_device(struct Scsi_Host *,
393 		uint, uint, u64, void *hostdata);
394 extern int scsi_add_device(struct Scsi_Host *host, uint channel,
395 			   uint target, u64 lun);
396 extern int scsi_register_device_handler(struct scsi_device_handler *scsi_dh);
397 extern void scsi_remove_device(struct scsi_device *);
398 extern int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh);
399 void scsi_attach_vpd(struct scsi_device *sdev);
400 void scsi_cdl_check(struct scsi_device *sdev);
401 int scsi_cdl_enable(struct scsi_device *sdev, bool enable);
402 
403 extern struct scsi_device *scsi_device_from_queue(struct request_queue *q);
404 extern int __must_check scsi_device_get(struct scsi_device *);
405 extern void scsi_device_put(struct scsi_device *);
406 extern struct scsi_device *scsi_device_lookup(struct Scsi_Host *,
407 					      uint, uint, u64);
408 extern struct scsi_device *__scsi_device_lookup(struct Scsi_Host *,
409 						uint, uint, u64);
410 extern struct scsi_device *scsi_device_lookup_by_target(struct scsi_target *,
411 							u64);
412 extern struct scsi_device *__scsi_device_lookup_by_target(struct scsi_target *,
413 							  u64);
414 extern void starget_for_each_device(struct scsi_target *, void *,
415 		     void (*fn)(struct scsi_device *, void *));
416 extern void __starget_for_each_device(struct scsi_target *, void *,
417 				      void (*fn)(struct scsi_device *,
418 						 void *));
419 
420 /* only exposed to implement shost_for_each_device */
421 extern struct scsi_device *__scsi_iterate_devices(struct Scsi_Host *,
422 						  struct scsi_device *);
423 
424 /**
425  * shost_for_each_device - iterate over all devices of a host
426  * @sdev: the &struct scsi_device to use as a cursor
427  * @shost: the &struct scsi_host to iterate over
428  *
429  * Iterator that returns each device attached to @shost.  This loop
430  * takes a reference on each device and releases it at the end.  If
431  * you break out of the loop, you must call scsi_device_put(sdev).
432  */
433 #define shost_for_each_device(sdev, shost) \
434 	for ((sdev) = __scsi_iterate_devices((shost), NULL); \
435 	     (sdev); \
436 	     (sdev) = __scsi_iterate_devices((shost), (sdev)))
437 
438 /**
439  * __shost_for_each_device - iterate over all devices of a host (UNLOCKED)
440  * @sdev: the &struct scsi_device to use as a cursor
441  * @shost: the &struct scsi_host to iterate over
442  *
443  * Iterator that returns each device attached to @shost.  It does _not_
444  * take a reference on the scsi_device, so the whole loop must be
445  * protected by shost->host_lock.
446  *
447  * Note: The only reason to use this is because you need to access the
448  * device list in interrupt context.  Otherwise you really want to use
449  * shost_for_each_device instead.
450  */
451 #define __shost_for_each_device(sdev, shost) \
452 	list_for_each_entry((sdev), &((shost)->__devices), siblings)
453 
454 extern int scsi_change_queue_depth(struct scsi_device *, int);
455 extern int scsi_track_queue_full(struct scsi_device *, int);
456 
457 extern int scsi_set_medium_removal(struct scsi_device *, char);
458 
459 int scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
460 		    int subpage, unsigned char *buffer, int len, int timeout,
461 		    int retries, struct scsi_mode_data *data,
462 		    struct scsi_sense_hdr *);
463 extern int scsi_mode_select(struct scsi_device *sdev, int pf, int sp,
464 			    unsigned char *buffer, int len, int timeout,
465 			    int retries, struct scsi_mode_data *data,
466 			    struct scsi_sense_hdr *);
467 extern int scsi_test_unit_ready(struct scsi_device *sdev, int timeout,
468 				int retries, struct scsi_sense_hdr *sshdr);
469 extern int scsi_get_vpd_page(struct scsi_device *, u8 page, unsigned char *buf,
470 			     int buf_len);
471 int scsi_report_opcode(struct scsi_device *sdev, unsigned char *buffer,
472 		       unsigned int len, unsigned char opcode,
473 		       unsigned short sa);
474 extern int scsi_device_set_state(struct scsi_device *sdev,
475 				 enum scsi_device_state state);
476 extern struct scsi_event *sdev_evt_alloc(enum scsi_device_event evt_type,
477 					  gfp_t gfpflags);
478 extern void sdev_evt_send(struct scsi_device *sdev, struct scsi_event *evt);
479 extern void sdev_evt_send_simple(struct scsi_device *sdev,
480 			  enum scsi_device_event evt_type, gfp_t gfpflags);
481 extern int scsi_device_quiesce(struct scsi_device *sdev);
482 extern void scsi_device_resume(struct scsi_device *sdev);
483 extern void scsi_target_quiesce(struct scsi_target *);
484 extern void scsi_target_resume(struct scsi_target *);
485 extern void scsi_scan_target(struct device *parent, unsigned int channel,
486 			     unsigned int id, u64 lun,
487 			     enum scsi_scan_mode rescan);
488 extern void scsi_target_reap(struct scsi_target *);
489 void scsi_block_targets(struct Scsi_Host *shost, struct device *dev);
490 extern void scsi_target_unblock(struct device *, enum scsi_device_state);
491 extern void scsi_remove_target(struct device *);
492 extern const char *scsi_device_state_name(enum scsi_device_state);
493 extern int scsi_is_sdev_device(const struct device *);
494 extern int scsi_is_target_device(const struct device *);
495 extern void scsi_sanitize_inquiry_string(unsigned char *s, int len);
496 
497 /*
498  * scsi_execute_cmd users can set scsi_failure.result to have
499  * scsi_check_passthrough fail/retry a command. scsi_failure.result can be a
500  * specific host byte or message code, or SCMD_FAILURE_RESULT_ANY can be used
501  * to match any host or message code.
502  */
503 #define SCMD_FAILURE_RESULT_ANY	0x7fffffff
504 /*
505  * Set scsi_failure.result to SCMD_FAILURE_STAT_ANY to fail/retry any failure
506  * scsi_status_is_good returns false for.
507  */
508 #define SCMD_FAILURE_STAT_ANY	0xff
509 /*
510  * The following can be set to the scsi_failure sense, asc and ascq fields to
511  * match on any sense, ASC, or ASCQ value.
512  */
513 #define SCMD_FAILURE_SENSE_ANY	0xff
514 #define SCMD_FAILURE_ASC_ANY	0xff
515 #define SCMD_FAILURE_ASCQ_ANY	0xff
516 /* Always retry a matching failure. */
517 #define SCMD_FAILURE_NO_LIMIT	-1
518 
519 struct scsi_failure {
520 	int result;
521 	u8 sense;
522 	u8 asc;
523 	u8 ascq;
524 	/*
525 	 * Number of times scsi_execute_cmd will retry the failure. It does
526 	 * not count for the total_allowed.
527 	 */
528 	s8 allowed;
529 	/* Number of times the failure has been retried. */
530 	s8 retries;
531 };
532 
533 struct scsi_failures {
534 	/*
535 	 * If a scsi_failure does not have a retry limit setup this limit will
536 	 * be used.
537 	 */
538 	int total_allowed;
539 	int total_retries;
540 	struct scsi_failure *failure_definitions;
541 };
542 
543 /* Optional arguments to scsi_execute_cmd */
544 struct scsi_exec_args {
545 	unsigned char *sense;		/* sense buffer */
546 	unsigned int sense_len;		/* sense buffer len */
547 	struct scsi_sense_hdr *sshdr;	/* decoded sense header */
548 	blk_mq_req_flags_t req_flags;	/* BLK_MQ_REQ flags */
549 	int scmd_flags;			/* SCMD flags */
550 	int *resid;			/* residual length */
551 	struct scsi_failures *failures;	/* failures to retry */
552 };
553 
554 int scsi_execute_cmd(struct scsi_device *sdev, const unsigned char *cmd,
555 		     blk_opf_t opf, void *buffer, unsigned int bufflen,
556 		     int timeout, int retries,
557 		     const struct scsi_exec_args *args);
558 void scsi_failures_reset_retries(struct scsi_failures *failures);
559 
560 extern void sdev_disable_disk_events(struct scsi_device *sdev);
561 extern void sdev_enable_disk_events(struct scsi_device *sdev);
562 extern int scsi_vpd_lun_id(struct scsi_device *, char *, size_t);
563 extern int scsi_vpd_tpg_id(struct scsi_device *, int *);
564 
565 #ifdef CONFIG_PM
566 extern int scsi_autopm_get_device(struct scsi_device *);
567 extern void scsi_autopm_put_device(struct scsi_device *);
568 #else
scsi_autopm_get_device(struct scsi_device * d)569 static inline int scsi_autopm_get_device(struct scsi_device *d) { return 0; }
scsi_autopm_put_device(struct scsi_device * d)570 static inline void scsi_autopm_put_device(struct scsi_device *d) {}
571 #endif /* CONFIG_PM */
572 
scsi_device_reprobe(struct scsi_device * sdev)573 static inline int __must_check scsi_device_reprobe(struct scsi_device *sdev)
574 {
575 	return device_reprobe(&sdev->sdev_gendev);
576 }
577 
sdev_channel(struct scsi_device * sdev)578 static inline unsigned int sdev_channel(struct scsi_device *sdev)
579 {
580 	return sdev->channel;
581 }
582 
sdev_id(struct scsi_device * sdev)583 static inline unsigned int sdev_id(struct scsi_device *sdev)
584 {
585 	return sdev->id;
586 }
587 
588 #define scmd_id(scmd) sdev_id((scmd)->device)
589 #define scmd_channel(scmd) sdev_channel((scmd)->device)
590 
591 /*
592  * checks for positions of the SCSI state machine
593  */
scsi_device_online(struct scsi_device * sdev)594 static inline int scsi_device_online(struct scsi_device *sdev)
595 {
596 	return (sdev->sdev_state != SDEV_OFFLINE &&
597 		sdev->sdev_state != SDEV_TRANSPORT_OFFLINE &&
598 		sdev->sdev_state != SDEV_DEL);
599 }
scsi_device_blocked(struct scsi_device * sdev)600 static inline int scsi_device_blocked(struct scsi_device *sdev)
601 {
602 	return sdev->sdev_state == SDEV_BLOCK ||
603 		sdev->sdev_state == SDEV_CREATED_BLOCK;
604 }
scsi_device_created(struct scsi_device * sdev)605 static inline int scsi_device_created(struct scsi_device *sdev)
606 {
607 	return sdev->sdev_state == SDEV_CREATED ||
608 		sdev->sdev_state == SDEV_CREATED_BLOCK;
609 }
610 
611 int scsi_internal_device_block_nowait(struct scsi_device *sdev);
612 int scsi_internal_device_unblock_nowait(struct scsi_device *sdev,
613 					enum scsi_device_state new_state);
614 
615 /* accessor functions for the SCSI parameters */
scsi_device_sync(struct scsi_device * sdev)616 static inline int scsi_device_sync(struct scsi_device *sdev)
617 {
618 	return sdev->sdtr;
619 }
scsi_device_wide(struct scsi_device * sdev)620 static inline int scsi_device_wide(struct scsi_device *sdev)
621 {
622 	return sdev->wdtr;
623 }
scsi_device_dt(struct scsi_device * sdev)624 static inline int scsi_device_dt(struct scsi_device *sdev)
625 {
626 	return sdev->ppr;
627 }
scsi_device_dt_only(struct scsi_device * sdev)628 static inline int scsi_device_dt_only(struct scsi_device *sdev)
629 {
630 	if (sdev->inquiry_len < 57)
631 		return 0;
632 	return (sdev->inquiry[56] & 0x0c) == 0x04;
633 }
scsi_device_ius(struct scsi_device * sdev)634 static inline int scsi_device_ius(struct scsi_device *sdev)
635 {
636 	if (sdev->inquiry_len < 57)
637 		return 0;
638 	return sdev->inquiry[56] & 0x01;
639 }
scsi_device_qas(struct scsi_device * sdev)640 static inline int scsi_device_qas(struct scsi_device *sdev)
641 {
642 	if (sdev->inquiry_len < 57)
643 		return 0;
644 	return sdev->inquiry[56] & 0x02;
645 }
scsi_device_enclosure(struct scsi_device * sdev)646 static inline int scsi_device_enclosure(struct scsi_device *sdev)
647 {
648 	return sdev->inquiry ? (sdev->inquiry[6] & (1<<6)) : 1;
649 }
650 
scsi_device_protection(struct scsi_device * sdev)651 static inline int scsi_device_protection(struct scsi_device *sdev)
652 {
653 	if (sdev->no_dif)
654 		return 0;
655 
656 	return sdev->scsi_level > SCSI_2 && sdev->inquiry[5] & (1<<0);
657 }
658 
scsi_device_tpgs(struct scsi_device * sdev)659 static inline int scsi_device_tpgs(struct scsi_device *sdev)
660 {
661 	return sdev->inquiry ? (sdev->inquiry[5] >> 4) & 0x3 : 0;
662 }
663 
664 /**
665  * scsi_device_supports_vpd - test if a device supports VPD pages
666  * @sdev: the &struct scsi_device to test
667  *
668  * If the 'try_vpd_pages' flag is set it takes precedence.
669  * Otherwise we will assume VPD pages are supported if the
670  * SCSI level is at least SPC-3 and 'skip_vpd_pages' is not set.
671  */
scsi_device_supports_vpd(struct scsi_device * sdev)672 static inline int scsi_device_supports_vpd(struct scsi_device *sdev)
673 {
674 	/* Attempt VPD inquiry if the device blacklist explicitly calls
675 	 * for it.
676 	 */
677 	if (sdev->try_vpd_pages)
678 		return 1;
679 	/*
680 	 * Although VPD inquiries can go to SCSI-2 type devices,
681 	 * some USB ones crash on receiving them, and the pages
682 	 * we currently ask for are mandatory for SPC-2 and beyond
683 	 */
684 	if (sdev->scsi_level >= SCSI_SPC_2 && !sdev->skip_vpd_pages)
685 		return 1;
686 	return 0;
687 }
688 
scsi_device_busy(struct scsi_device * sdev)689 static inline int scsi_device_busy(struct scsi_device *sdev)
690 {
691 	return sbitmap_weight(&sdev->budget_map);
692 }
693 
694 #define MODULE_ALIAS_SCSI_DEVICE(type) \
695 	MODULE_ALIAS("scsi:t-" __stringify(type) "*")
696 #define SCSI_DEVICE_MODALIAS_FMT "scsi:t-0x%02x"
697 
698 #endif /* _SCSI_SCSI_DEVICE_H */
699