libiscsi.h 14 KB

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