libiscsi.h 15 KB

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