libiscsi.h 11 KB

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