scsi_device.h 24 KB

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