libiscsi.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * iSCSI lib definitions
  4. *
  5. * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
  6. * Copyright (C) 2004 - 2006 Mike Christie
  7. * Copyright (C) 2004 - 2005 Dmitry Yusupov
  8. * Copyright (C) 2004 - 2005 Alex Aizman
  9. */
  10. #ifndef LIBISCSI_H
  11. #define LIBISCSI_H
  12. #include <linux/types.h>
  13. #include <linux/wait.h>
  14. #include <linux/mutex.h>
  15. #include <linux/timer.h>
  16. #include <linux/workqueue.h>
  17. #include <linux/kfifo.h>
  18. #include <linux/refcount.h>
  19. #include <scsi/iscsi_proto.h>
  20. #include <scsi/iscsi_if.h>
  21. #include <scsi/scsi_transport_iscsi.h>
  22. struct scsi_transport_template;
  23. struct scsi_host_template;
  24. struct scsi_device;
  25. struct Scsi_Host;
  26. struct scsi_target;
  27. struct scsi_cmnd;
  28. struct socket;
  29. struct iscsi_transport;
  30. struct iscsi_cls_session;
  31. struct iscsi_cls_conn;
  32. struct iscsi_session;
  33. struct iscsi_nopin;
  34. struct device;
  35. #define ISCSI_DEF_XMIT_CMDS_MAX 128 /* must be power of 2 */
  36. #define ISCSI_MGMT_CMDS_MAX 15
  37. #define ISCSI_DEF_CMD_PER_LUN 32
  38. /* Task Mgmt states */
  39. enum {
  40. TMF_INITIAL,
  41. TMF_QUEUED,
  42. TMF_SUCCESS,
  43. TMF_FAILED,
  44. TMF_TIMEDOUT,
  45. TMF_NOT_FOUND,
  46. };
  47. #define ISID_SIZE 6
  48. /* Connection suspend "bit" */
  49. #define ISCSI_SUSPEND_BIT 1
  50. #define ISCSI_ITT_MASK 0x1fff
  51. #define ISCSI_TOTAL_CMDS_MAX 4096
  52. /* this must be a power of two greater than ISCSI_MGMT_CMDS_MAX */
  53. #define ISCSI_TOTAL_CMDS_MIN 16
  54. #define ISCSI_AGE_SHIFT 28
  55. #define ISCSI_AGE_MASK 0xf
  56. #define ISCSI_ADDRESS_BUF_LEN 64
  57. enum {
  58. /* this is the maximum possible storage for AHSs */
  59. ISCSI_MAX_AHS_SIZE = sizeof(struct iscsi_ecdb_ahdr) +
  60. sizeof(struct iscsi_rlength_ahdr),
  61. ISCSI_DIGEST_SIZE = sizeof(__u32),
  62. };
  63. enum {
  64. ISCSI_TASK_FREE,
  65. ISCSI_TASK_COMPLETED,
  66. ISCSI_TASK_PENDING,
  67. ISCSI_TASK_RUNNING,
  68. ISCSI_TASK_ABRT_TMF, /* aborted due to TMF */
  69. ISCSI_TASK_ABRT_SESS_RECOV, /* aborted due to session recovery */
  70. ISCSI_TASK_REQUEUE_SCSIQ, /* qcmd requeueing to scsi-ml */
  71. };
  72. struct iscsi_r2t_info {
  73. __be32 ttt; /* copied from R2T */
  74. __be32 exp_statsn; /* copied from R2T */
  75. uint32_t data_length; /* copied from R2T */
  76. uint32_t data_offset; /* copied from R2T */
  77. int data_count; /* DATA-Out payload progress */
  78. int datasn;
  79. /* LLDs should set/update these values */
  80. int sent; /* R2T sequence progress */
  81. };
  82. struct iscsi_task {
  83. /*
  84. * Because LLDs allocate their hdr differently, this is a pointer
  85. * and length to that storage. It must be setup at session
  86. * creation time.
  87. */
  88. struct iscsi_hdr *hdr;
  89. unsigned short hdr_max;
  90. unsigned short hdr_len; /* accumulated size of hdr used */
  91. /* copied values in case we need to send tmfs */
  92. itt_t hdr_itt;
  93. __be32 cmdsn;
  94. struct scsi_lun lun;
  95. int itt; /* this ITT */
  96. unsigned imm_count; /* imm-data (bytes) */
  97. /* offset in unsolicited stream (bytes); */
  98. struct iscsi_r2t_info unsol_r2t;
  99. char *data; /* mgmt payload */
  100. unsigned data_count;
  101. struct scsi_cmnd *sc; /* associated SCSI cmd*/
  102. struct iscsi_conn *conn; /* used connection */
  103. /* data processing tracking */
  104. unsigned long last_xfer;
  105. unsigned long last_timeout;
  106. bool have_checked_conn;
  107. /* T10 protection information */
  108. bool protected;
  109. /* state set/tested under session->lock */
  110. int state;
  111. refcount_t refcount;
  112. struct list_head running; /* running cmd list */
  113. void *dd_data; /* driver/transport data */
  114. };
  115. /* invalid scsi_task pointer */
  116. #define INVALID_SCSI_TASK (struct iscsi_task *)-1l
  117. static inline int iscsi_task_has_unsol_data(struct iscsi_task *task)
  118. {
  119. return task->unsol_r2t.data_length > task->unsol_r2t.sent;
  120. }
  121. static inline void* iscsi_next_hdr(struct iscsi_task *task)
  122. {
  123. return (void*)task->hdr + task->hdr_len;
  124. }
  125. /* Connection's states */
  126. enum {
  127. ISCSI_CONN_INITIAL_STAGE,
  128. ISCSI_CONN_STARTED,
  129. ISCSI_CONN_STOPPED,
  130. ISCSI_CONN_CLEANUP_WAIT,
  131. };
  132. struct iscsi_conn {
  133. struct iscsi_cls_conn *cls_conn; /* ptr to class connection */
  134. void *dd_data; /* iscsi_transport data */
  135. struct iscsi_session *session; /* parent session */
  136. /*
  137. * conn_stop() flag: stop to recover, stop to terminate
  138. */
  139. int stop_stage;
  140. struct timer_list transport_timer;
  141. unsigned long last_recv;
  142. unsigned long last_ping;
  143. int ping_timeout;
  144. int recv_timeout;
  145. struct iscsi_task *ping_task;
  146. /* iSCSI connection-wide sequencing */
  147. uint32_t exp_statsn;
  148. uint32_t statsn;
  149. /* control data */
  150. int id; /* CID */
  151. int c_stage; /* connection state */
  152. /*
  153. * Preallocated buffer for pdus that have data but do not
  154. * originate from scsi-ml. We never have two pdus using the
  155. * buffer at the same time. It is only allocated to
  156. * the default max recv size because the pdus we support
  157. * should always fit in this buffer
  158. */
  159. char *data;
  160. struct iscsi_task *login_task; /* mtask used for login/text */
  161. struct iscsi_task *task; /* xmit task in progress */
  162. /* xmit */
  163. spinlock_t taskqueuelock; /* protects the next three lists */
  164. struct list_head mgmtqueue; /* mgmt (control) xmit queue */
  165. struct list_head cmdqueue; /* data-path cmd queue */
  166. struct list_head requeue; /* tasks needing another run */
  167. struct work_struct xmitwork; /* per-conn. xmit workqueue */
  168. unsigned long suspend_tx; /* suspend Tx */
  169. unsigned long suspend_rx; /* suspend Rx */
  170. /* negotiated params */
  171. unsigned max_recv_dlength; /* initiator_max_recv_dsl*/
  172. unsigned max_xmit_dlength; /* target_max_recv_dsl */
  173. int hdrdgst_en;
  174. int datadgst_en;
  175. int ifmarker_en;
  176. int ofmarker_en;
  177. /* values userspace uses to id a conn */
  178. int persistent_port;
  179. char *persistent_address;
  180. unsigned max_segment_size;
  181. unsigned tcp_xmit_wsf;
  182. unsigned tcp_recv_wsf;
  183. uint16_t keepalive_tmo;
  184. uint16_t local_port;
  185. uint8_t tcp_timestamp_stat;
  186. uint8_t tcp_nagle_disable;
  187. uint8_t tcp_wsf_disable;
  188. uint8_t tcp_timer_scale;
  189. uint8_t tcp_timestamp_en;
  190. uint8_t fragment_disable;
  191. uint8_t ipv4_tos;
  192. uint8_t ipv6_traffic_class;
  193. uint8_t ipv6_flow_label;
  194. uint8_t is_fw_assigned_ipv6;
  195. char *local_ipaddr;
  196. /* MIB-statistics */
  197. uint64_t txdata_octets;
  198. uint64_t rxdata_octets;
  199. uint32_t scsicmd_pdus_cnt;
  200. uint32_t dataout_pdus_cnt;
  201. uint32_t scsirsp_pdus_cnt;
  202. uint32_t datain_pdus_cnt;
  203. uint32_t r2t_pdus_cnt;
  204. uint32_t tmfcmd_pdus_cnt;
  205. int32_t tmfrsp_pdus_cnt;
  206. /* custom statistics */
  207. uint32_t eh_abort_cnt;
  208. uint32_t fmr_unalign_cnt;
  209. };
  210. struct iscsi_pool {
  211. struct kfifo queue; /* FIFO Queue */
  212. void **pool; /* Pool of elements */
  213. int max; /* Max number of elements */
  214. };
  215. /* Session's states */
  216. enum {
  217. ISCSI_STATE_FREE = 1,
  218. ISCSI_STATE_LOGGED_IN,
  219. ISCSI_STATE_FAILED,
  220. ISCSI_STATE_TERMINATE,
  221. ISCSI_STATE_IN_RECOVERY,
  222. ISCSI_STATE_RECOVERY_FAILED,
  223. ISCSI_STATE_LOGGING_OUT,
  224. };
  225. struct iscsi_session {
  226. struct iscsi_cls_session *cls_session;
  227. /*
  228. * Syncs up the scsi eh thread with the iscsi eh thread when sending
  229. * task management functions. This must be taken before the session
  230. * and recv lock.
  231. */
  232. struct mutex eh_mutex;
  233. /* abort */
  234. wait_queue_head_t ehwait; /* used in eh_abort() */
  235. struct iscsi_tm tmhdr;
  236. struct timer_list tmf_timer;
  237. int tmf_state; /* see TMF_INITIAL, etc.*/
  238. /* iSCSI session-wide sequencing */
  239. uint32_t cmdsn;
  240. uint32_t exp_cmdsn;
  241. uint32_t max_cmdsn;
  242. /* This tracks the reqs queued into the initiator */
  243. uint32_t queued_cmdsn;
  244. /* configuration */
  245. int abort_timeout;
  246. int lu_reset_timeout;
  247. int tgt_reset_timeout;
  248. int initial_r2t_en;
  249. unsigned short max_r2t;
  250. int imm_data_en;
  251. unsigned first_burst;
  252. unsigned max_burst;
  253. int time2wait;
  254. int time2retain;
  255. int pdu_inorder_en;
  256. int dataseq_inorder_en;
  257. int erl;
  258. int fast_abort;
  259. int tpgt;
  260. char *username;
  261. char *username_in;
  262. char *password;
  263. char *password_in;
  264. char *targetname;
  265. char *targetalias;
  266. char *ifacename;
  267. char *initiatorname;
  268. char *boot_root;
  269. char *boot_nic;
  270. char *boot_target;
  271. char *portal_type;
  272. char *discovery_parent_type;
  273. uint16_t discovery_parent_idx;
  274. uint16_t def_taskmgmt_tmo;
  275. uint16_t tsid;
  276. uint8_t auto_snd_tgt_disable;
  277. uint8_t discovery_sess;
  278. uint8_t chap_auth_en;
  279. uint8_t discovery_logout_en;
  280. uint8_t bidi_chap_en;
  281. uint8_t discovery_auth_optional;
  282. uint8_t isid[ISID_SIZE];
  283. /* control data */
  284. struct iscsi_transport *tt;
  285. struct Scsi_Host *host;
  286. struct iscsi_conn *leadconn; /* leading connection */
  287. /* Between the forward and the backward locks exists a strict locking
  288. * hierarchy. The mutual exclusion zone protected by the forward lock
  289. * can enclose the mutual exclusion zone protected by the backward lock
  290. * but not vice versa.
  291. */
  292. spinlock_t frwd_lock; /* protects session state, *
  293. * cmdsn, queued_cmdsn *
  294. * session resources: *
  295. * - cmdpool kfifo_out , *
  296. * - mgmtpool, */
  297. spinlock_t back_lock; /* protects cmdsn_exp *
  298. * cmdsn_max, *
  299. * cmdpool kfifo_in */
  300. int state; /* session state */
  301. int age; /* counts session re-opens */
  302. int scsi_cmds_max; /* max scsi commands */
  303. int cmds_max; /* size of cmds array */
  304. struct iscsi_task **cmds; /* Original Cmds arr */
  305. struct iscsi_pool cmdpool; /* PDU's pool */
  306. void *dd_data; /* LLD private data */
  307. };
  308. enum {
  309. ISCSI_HOST_SETUP,
  310. ISCSI_HOST_REMOVED,
  311. };
  312. struct iscsi_host {
  313. char *initiatorname;
  314. /* hw address or netdev iscsi connection is bound to */
  315. char *hwaddress;
  316. char *netdev;
  317. wait_queue_head_t session_removal_wq;
  318. /* protects sessions and state */
  319. spinlock_t lock;
  320. int num_sessions;
  321. int state;
  322. struct workqueue_struct *workq;
  323. char workq_name[20];
  324. };
  325. /*
  326. * scsi host template
  327. */
  328. extern int iscsi_eh_abort(struct scsi_cmnd *sc);
  329. extern int iscsi_eh_recover_target(struct scsi_cmnd *sc);
  330. extern int iscsi_eh_session_reset(struct scsi_cmnd *sc);
  331. extern int iscsi_eh_device_reset(struct scsi_cmnd *sc);
  332. extern int iscsi_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *sc);
  333. extern enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc);
  334. /*
  335. * iSCSI host helpers.
  336. */
  337. #define iscsi_host_priv(_shost) \
  338. (shost_priv(_shost) + sizeof(struct iscsi_host))
  339. extern int iscsi_host_set_param(struct Scsi_Host *shost,
  340. enum iscsi_host_param param, char *buf,
  341. int buflen);
  342. extern int iscsi_host_get_param(struct Scsi_Host *shost,
  343. enum iscsi_host_param param, char *buf);
  344. extern int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev);
  345. extern struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
  346. int dd_data_size,
  347. bool xmit_can_sleep);
  348. extern void iscsi_host_remove(struct Scsi_Host *shost);
  349. extern void iscsi_host_free(struct Scsi_Host *shost);
  350. extern int iscsi_target_alloc(struct scsi_target *starget);
  351. /*
  352. * session management
  353. */
  354. extern struct iscsi_cls_session *
  355. iscsi_session_setup(struct iscsi_transport *, struct Scsi_Host *shost,
  356. uint16_t, int, int, uint32_t, unsigned int);
  357. extern void iscsi_session_teardown(struct iscsi_cls_session *);
  358. extern void iscsi_session_recovery_timedout(struct iscsi_cls_session *);
  359. extern int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
  360. enum iscsi_param param, char *buf, int buflen);
  361. extern int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
  362. enum iscsi_param param, char *buf);
  363. #define iscsi_session_printk(prefix, _sess, fmt, a...) \
  364. iscsi_cls_session_printk(prefix, _sess->cls_session, fmt, ##a)
  365. /*
  366. * connection management
  367. */
  368. extern struct iscsi_cls_conn *iscsi_conn_setup(struct iscsi_cls_session *,
  369. int, uint32_t);
  370. extern void iscsi_conn_teardown(struct iscsi_cls_conn *);
  371. extern int iscsi_conn_start(struct iscsi_cls_conn *);
  372. extern void iscsi_conn_stop(struct iscsi_cls_conn *, int);
  373. extern int iscsi_conn_bind(struct iscsi_cls_session *, struct iscsi_cls_conn *,
  374. int);
  375. extern void iscsi_conn_unbind(struct iscsi_cls_conn *cls_conn, bool is_active);
  376. extern void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err);
  377. extern void iscsi_session_failure(struct iscsi_session *session,
  378. enum iscsi_err err);
  379. extern int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  380. enum iscsi_param param, char *buf);
  381. extern int iscsi_conn_get_addr_param(struct sockaddr_storage *addr,
  382. enum iscsi_param param, char *buf);
  383. extern void iscsi_suspend_tx(struct iscsi_conn *conn);
  384. extern void iscsi_suspend_queue(struct iscsi_conn *conn);
  385. extern void iscsi_conn_queue_work(struct iscsi_conn *conn);
  386. #define iscsi_conn_printk(prefix, _c, fmt, a...) \
  387. iscsi_cls_conn_printk(prefix, ((struct iscsi_conn *)_c)->cls_conn, \
  388. fmt, ##a)
  389. /*
  390. * pdu and task processing
  391. */
  392. extern void iscsi_update_cmdsn(struct iscsi_session *, struct iscsi_nopin *);
  393. extern void iscsi_prep_data_out_pdu(struct iscsi_task *task,
  394. struct iscsi_r2t_info *r2t,
  395. struct iscsi_data *hdr);
  396. extern int iscsi_conn_send_pdu(struct iscsi_cls_conn *, struct iscsi_hdr *,
  397. char *, uint32_t);
  398. extern int iscsi_complete_pdu(struct iscsi_conn *, struct iscsi_hdr *,
  399. char *, int);
  400. extern int __iscsi_complete_pdu(struct iscsi_conn *, struct iscsi_hdr *,
  401. char *, int);
  402. extern int iscsi_verify_itt(struct iscsi_conn *, itt_t);
  403. extern struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *, itt_t);
  404. extern struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *, itt_t);
  405. extern void iscsi_requeue_task(struct iscsi_task *task);
  406. extern void iscsi_put_task(struct iscsi_task *task);
  407. extern void __iscsi_put_task(struct iscsi_task *task);
  408. extern void __iscsi_get_task(struct iscsi_task *task);
  409. extern void iscsi_complete_scsi_task(struct iscsi_task *task,
  410. uint32_t exp_cmdsn, uint32_t max_cmdsn);
  411. /*
  412. * generic helpers
  413. */
  414. extern void iscsi_pool_free(struct iscsi_pool *);
  415. extern int iscsi_pool_init(struct iscsi_pool *, int, void ***, int);
  416. extern int iscsi_switch_str_param(char **, char *);
  417. /*
  418. * inline functions to deal with padding.
  419. */
  420. static inline unsigned int
  421. iscsi_padded(unsigned int len)
  422. {
  423. return (len + ISCSI_PAD_LEN - 1) & ~(ISCSI_PAD_LEN - 1);
  424. }
  425. static inline unsigned int
  426. iscsi_padding(unsigned int len)
  427. {
  428. len &= (ISCSI_PAD_LEN - 1);
  429. if (len)
  430. len = ISCSI_PAD_LEN - len;
  431. return len;
  432. }
  433. #endif