libiscsi.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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/mutex.h>
  27. #include <linux/timer.h>
  28. #include <linux/workqueue.h>
  29. #include <scsi/iscsi_proto.h>
  30. #include <scsi/iscsi_if.h>
  31. struct scsi_transport_template;
  32. struct scsi_device;
  33. struct Scsi_Host;
  34. struct scsi_cmnd;
  35. struct socket;
  36. struct iscsi_transport;
  37. struct iscsi_cls_session;
  38. struct iscsi_cls_conn;
  39. struct iscsi_session;
  40. struct iscsi_nopin;
  41. /* #define DEBUG_SCSI */
  42. #ifdef DEBUG_SCSI
  43. #define debug_scsi(fmt...) printk(KERN_INFO "iscsi: " fmt)
  44. #else
  45. #define debug_scsi(fmt...)
  46. #endif
  47. #define ISCSI_DEF_XMIT_CMDS_MAX 128 /* must be power of 2 */
  48. #define ISCSI_MGMT_CMDS_MAX 16 /* must be power of 2 */
  49. #define ISCSI_MGMT_ITT_OFFSET 0xa00
  50. #define ISCSI_DEF_CMD_PER_LUN 32
  51. #define ISCSI_MAX_CMD_PER_LUN 128
  52. /* Task Mgmt states */
  53. enum {
  54. TMF_INITIAL,
  55. TMF_QUEUED,
  56. TMF_SUCCESS,
  57. TMF_FAILED,
  58. TMF_TIMEDOUT,
  59. TMF_NOT_FOUND,
  60. };
  61. /* Connection suspend "bit" */
  62. #define ISCSI_SUSPEND_BIT 1
  63. #define ISCSI_ITT_MASK (0xfff)
  64. #define ISCSI_AGE_SHIFT 28
  65. #define ISCSI_AGE_MASK (0xf << ISCSI_AGE_SHIFT)
  66. #define ISCSI_ADDRESS_BUF_LEN 64
  67. enum {
  68. /* this is the maximum possible storage for AHSs */
  69. ISCSI_MAX_AHS_SIZE = sizeof(struct iscsi_ecdb_ahdr) +
  70. sizeof(struct iscsi_rlength_ahdr),
  71. ISCSI_DIGEST_SIZE = sizeof(__u32),
  72. };
  73. struct iscsi_mgmt_task {
  74. /*
  75. * Becuae LLDs allocate their hdr differently, this is a pointer to
  76. * that storage. It must be setup at session creation time.
  77. */
  78. struct iscsi_hdr *hdr;
  79. char *data; /* mgmt payload */
  80. unsigned data_count; /* counts data to be sent */
  81. uint32_t itt; /* this ITT */
  82. void *dd_data; /* driver/transport data */
  83. struct list_head running;
  84. };
  85. enum {
  86. ISCSI_TASK_COMPLETED,
  87. ISCSI_TASK_PENDING,
  88. ISCSI_TASK_RUNNING,
  89. };
  90. struct iscsi_cmd_task {
  91. /*
  92. * Because LLDs allocate their hdr differently, this is a pointer
  93. * and length to that storage. It must be setup at session
  94. * creation time.
  95. */
  96. struct iscsi_cmd *hdr;
  97. unsigned short hdr_max;
  98. unsigned short hdr_len; /* accumulated size of hdr used */
  99. int itt; /* this ITT */
  100. uint32_t unsol_datasn;
  101. unsigned imm_count; /* imm-data (bytes) */
  102. unsigned unsol_count; /* unsolicited (bytes)*/
  103. /* offset in unsolicited stream (bytes); */
  104. unsigned unsol_offset;
  105. unsigned data_count; /* remaining Data-Out */
  106. struct scsi_cmnd *sc; /* associated SCSI cmd*/
  107. struct iscsi_conn *conn; /* used connection */
  108. /* state set/tested under session->lock */
  109. int state;
  110. atomic_t refcount;
  111. struct list_head running; /* running cmd list */
  112. void *dd_data; /* driver/transport data */
  113. };
  114. static inline void* iscsi_next_hdr(struct iscsi_cmd_task *ctask)
  115. {
  116. return (void*)ctask->hdr + ctask->hdr_len;
  117. }
  118. /* Connection's states */
  119. enum {
  120. ISCSI_CONN_INITIAL_STAGE,
  121. ISCSI_CONN_STARTED,
  122. ISCSI_CONN_STOPPED,
  123. ISCSI_CONN_CLEANUP_WAIT,
  124. };
  125. struct iscsi_conn {
  126. struct iscsi_cls_conn *cls_conn; /* ptr to class connection */
  127. void *dd_data; /* iscsi_transport data */
  128. struct iscsi_session *session; /* parent session */
  129. /*
  130. * LLDs should set this lock. It protects the transport recv
  131. * code
  132. */
  133. rwlock_t *recv_lock;
  134. /*
  135. * conn_stop() flag: stop to recover, stop to terminate
  136. */
  137. int stop_stage;
  138. struct timer_list transport_timer;
  139. unsigned long last_recv;
  140. unsigned long last_ping;
  141. int ping_timeout;
  142. int recv_timeout;
  143. struct iscsi_mgmt_task *ping_mtask;
  144. /* iSCSI connection-wide sequencing */
  145. uint32_t exp_statsn;
  146. /* control data */
  147. int id; /* CID */
  148. int c_stage; /* connection state */
  149. /*
  150. * Preallocated buffer for pdus that have data but do not
  151. * originate from scsi-ml. We never have two pdus using the
  152. * buffer at the same time. It is only allocated to
  153. * the default max recv size because the pdus we support
  154. * should always fit in this buffer
  155. */
  156. char *data;
  157. struct iscsi_mgmt_task *login_mtask; /* mtask used for login/text */
  158. struct iscsi_mgmt_task *mtask; /* xmit mtask in progress */
  159. struct iscsi_cmd_task *ctask; /* xmit ctask in progress */
  160. /* xmit */
  161. struct list_head mgmtqueue; /* mgmt (control) xmit queue */
  162. struct list_head mgmt_run_list; /* list of control tasks */
  163. struct list_head xmitqueue; /* data-path cmd queue */
  164. struct list_head run_list; /* list of cmds in progress */
  165. struct list_head requeue; /* tasks needing another run */
  166. struct work_struct xmitwork; /* per-conn. xmit workqueue */
  167. unsigned long suspend_tx; /* suspend Tx */
  168. unsigned long suspend_rx; /* suspend Rx */
  169. /* abort */
  170. wait_queue_head_t ehwait; /* used in eh_abort() */
  171. struct iscsi_tm tmhdr;
  172. struct timer_list tmf_timer;
  173. int tmf_state; /* see TMF_INITIAL, etc.*/
  174. /* negotiated params */
  175. unsigned max_recv_dlength; /* initiator_max_recv_dsl*/
  176. unsigned max_xmit_dlength; /* target_max_recv_dsl */
  177. int hdrdgst_en;
  178. int datadgst_en;
  179. int ifmarker_en;
  180. int ofmarker_en;
  181. /* values userspace uses to id a conn */
  182. int persistent_port;
  183. char *persistent_address;
  184. /* remote portal currently connected to */
  185. int portal_port;
  186. char portal_address[ISCSI_ADDRESS_BUF_LEN];
  187. /* local address */
  188. int local_port;
  189. char local_address[ISCSI_ADDRESS_BUF_LEN];
  190. /* MIB-statistics */
  191. uint64_t txdata_octets;
  192. uint64_t rxdata_octets;
  193. uint32_t scsicmd_pdus_cnt;
  194. uint32_t dataout_pdus_cnt;
  195. uint32_t scsirsp_pdus_cnt;
  196. uint32_t datain_pdus_cnt;
  197. uint32_t r2t_pdus_cnt;
  198. uint32_t tmfcmd_pdus_cnt;
  199. int32_t tmfrsp_pdus_cnt;
  200. /* custom statistics */
  201. uint32_t eh_abort_cnt;
  202. uint32_t fmr_unalign_cnt;
  203. };
  204. struct iscsi_pool {
  205. struct kfifo *queue; /* FIFO Queue */
  206. void **pool; /* Pool of elements */
  207. int max; /* Max number of elements */
  208. };
  209. /* Session's states */
  210. enum {
  211. ISCSI_STATE_FREE = 1,
  212. ISCSI_STATE_LOGGED_IN,
  213. ISCSI_STATE_FAILED,
  214. ISCSI_STATE_TERMINATE,
  215. ISCSI_STATE_IN_RECOVERY,
  216. ISCSI_STATE_RECOVERY_FAILED,
  217. ISCSI_STATE_LOGGING_OUT,
  218. };
  219. struct iscsi_session {
  220. /*
  221. * Syncs up the scsi eh thread with the iscsi eh thread when sending
  222. * task management functions. This must be taken before the session
  223. * and recv lock.
  224. */
  225. struct mutex eh_mutex;
  226. /* iSCSI session-wide sequencing */
  227. uint32_t cmdsn;
  228. uint32_t exp_cmdsn;
  229. uint32_t max_cmdsn;
  230. /* This tracks the reqs queued into the initiator */
  231. uint32_t queued_cmdsn;
  232. /* configuration */
  233. int abort_timeout;
  234. int lu_reset_timeout;
  235. int initial_r2t_en;
  236. unsigned max_r2t;
  237. int imm_data_en;
  238. unsigned first_burst;
  239. unsigned max_burst;
  240. int time2wait;
  241. int time2retain;
  242. int pdu_inorder_en;
  243. int dataseq_inorder_en;
  244. int erl;
  245. int fast_abort;
  246. int tpgt;
  247. char *username;
  248. char *username_in;
  249. char *password;
  250. char *password_in;
  251. char *targetname;
  252. char *initiatorname;
  253. /* hw address or netdev iscsi connection is bound to */
  254. char *hwaddress;
  255. char *netdev;
  256. /* control data */
  257. struct iscsi_transport *tt;
  258. struct Scsi_Host *host;
  259. struct iscsi_conn *leadconn; /* leading connection */
  260. spinlock_t lock; /* protects session state, *
  261. * sequence numbers, *
  262. * session resources: *
  263. * - cmdpool, *
  264. * - mgmtpool, *
  265. * - r2tpool */
  266. int state; /* session state */
  267. int age; /* counts session re-opens */
  268. int cmds_max; /* size of cmds array */
  269. struct iscsi_cmd_task **cmds; /* Original Cmds arr */
  270. struct iscsi_pool cmdpool; /* PDU's pool */
  271. int mgmtpool_max; /* size of mgmt array */
  272. struct iscsi_mgmt_task **mgmt_cmds; /* Original mgmt arr */
  273. struct iscsi_pool mgmtpool; /* Mgmt PDU's pool */
  274. };
  275. /*
  276. * scsi host template
  277. */
  278. extern int iscsi_change_queue_depth(struct scsi_device *sdev, int depth);
  279. extern int iscsi_eh_abort(struct scsi_cmnd *sc);
  280. extern int iscsi_eh_host_reset(struct scsi_cmnd *sc);
  281. extern int iscsi_eh_device_reset(struct scsi_cmnd *sc);
  282. extern int iscsi_queuecommand(struct scsi_cmnd *sc,
  283. void (*done)(struct scsi_cmnd *));
  284. /*
  285. * iSCSI host helpers.
  286. */
  287. extern int iscsi_host_set_param(struct Scsi_Host *shost,
  288. enum iscsi_host_param param, char *buf,
  289. int buflen);
  290. extern int iscsi_host_get_param(struct Scsi_Host *shost,
  291. enum iscsi_host_param param, char *buf);
  292. /*
  293. * session management
  294. */
  295. extern struct iscsi_cls_session *
  296. iscsi_session_setup(struct iscsi_transport *, struct scsi_transport_template *,
  297. uint16_t, uint16_t, int, int, uint32_t, uint32_t *);
  298. extern void iscsi_session_teardown(struct iscsi_cls_session *);
  299. extern struct iscsi_session *class_to_transport_session(struct iscsi_cls_session *);
  300. extern void iscsi_session_recovery_timedout(struct iscsi_cls_session *);
  301. extern int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
  302. enum iscsi_param param, char *buf, int buflen);
  303. extern int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
  304. enum iscsi_param param, char *buf);
  305. #define session_to_cls(_sess) \
  306. hostdata_session(_sess->host->hostdata)
  307. #define iscsi_session_printk(prefix, _sess, fmt, a...) \
  308. iscsi_cls_session_printk(prefix, \
  309. (struct iscsi_cls_session *)session_to_cls(_sess), fmt, ##a)
  310. /*
  311. * connection management
  312. */
  313. extern struct iscsi_cls_conn *iscsi_conn_setup(struct iscsi_cls_session *,
  314. uint32_t);
  315. extern void iscsi_conn_teardown(struct iscsi_cls_conn *);
  316. extern int iscsi_conn_start(struct iscsi_cls_conn *);
  317. extern void iscsi_conn_stop(struct iscsi_cls_conn *, int);
  318. extern int iscsi_conn_bind(struct iscsi_cls_session *, struct iscsi_cls_conn *,
  319. int);
  320. extern void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err);
  321. extern int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  322. enum iscsi_param param, char *buf);
  323. #define iscsi_conn_printk(prefix, _c, fmt, a...) \
  324. iscsi_cls_conn_printk(prefix, _c->cls_conn, fmt, ##a)
  325. /*
  326. * pdu and task processing
  327. */
  328. extern void iscsi_update_cmdsn(struct iscsi_session *, struct iscsi_nopin *);
  329. extern void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *,
  330. struct iscsi_data *hdr);
  331. extern int iscsi_conn_send_pdu(struct iscsi_cls_conn *, struct iscsi_hdr *,
  332. char *, uint32_t);
  333. extern int iscsi_complete_pdu(struct iscsi_conn *, struct iscsi_hdr *,
  334. char *, int);
  335. extern int iscsi_verify_itt(struct iscsi_conn *, struct iscsi_hdr *,
  336. uint32_t *);
  337. extern void iscsi_requeue_ctask(struct iscsi_cmd_task *ctask);
  338. extern void iscsi_free_mgmt_task(struct iscsi_conn *conn,
  339. struct iscsi_mgmt_task *mtask);
  340. /*
  341. * generic helpers
  342. */
  343. extern void iscsi_pool_free(struct iscsi_pool *);
  344. extern int iscsi_pool_init(struct iscsi_pool *, int, void ***, int);
  345. /*
  346. * inline functions to deal with padding.
  347. */
  348. static inline unsigned int
  349. iscsi_padded(unsigned int len)
  350. {
  351. return (len + ISCSI_PAD_LEN - 1) & ~(ISCSI_PAD_LEN - 1);
  352. }
  353. static inline unsigned int
  354. iscsi_padding(unsigned int len)
  355. {
  356. len &= (ISCSI_PAD_LEN - 1);
  357. if (len)
  358. len = ISCSI_PAD_LEN - len;
  359. return len;
  360. }
  361. #endif