scsi_device.h 21 KB

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