libiscsi.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. struct iscsi_mgmt_task {
  77. /*
  78. * Becuae LLDs allocate their hdr differently, this is a pointer to
  79. * that storage. It must be setup at session creation time.
  80. */
  81. struct iscsi_hdr *hdr;
  82. char *data; /* mgmt payload */
  83. unsigned data_count; /* counts data to be sent */
  84. uint32_t itt; /* this ITT */
  85. void *dd_data; /* driver/transport data */
  86. struct list_head running;
  87. };
  88. enum {
  89. ISCSI_TASK_COMPLETED,
  90. ISCSI_TASK_PENDING,
  91. ISCSI_TASK_RUNNING,
  92. };
  93. struct iscsi_cmd_task {
  94. /*
  95. * Because LLDs allocate their hdr differently, this is a pointer
  96. * and length to that storage. It must be setup at session
  97. * creation time.
  98. */
  99. struct iscsi_cmd *hdr;
  100. unsigned short hdr_max;
  101. unsigned short hdr_len; /* accumulated size of hdr used */
  102. int itt; /* this ITT */
  103. uint32_t unsol_datasn;
  104. unsigned imm_count; /* imm-data (bytes) */
  105. unsigned unsol_count; /* unsolicited (bytes)*/
  106. /* offset in unsolicited stream (bytes); */
  107. unsigned unsol_offset;
  108. unsigned data_count; /* remaining Data-Out */
  109. struct scsi_cmnd *sc; /* associated SCSI cmd*/
  110. struct iscsi_conn *conn; /* used connection */
  111. /* state set/tested under session->lock */
  112. int state;
  113. atomic_t refcount;
  114. struct list_head running; /* running cmd list */
  115. void *dd_data; /* driver/transport data */
  116. };
  117. static inline void* iscsi_next_hdr(struct iscsi_cmd_task *ctask)
  118. {
  119. return (void*)ctask->hdr + ctask->hdr_len;
  120. }
  121. /* Connection's states */
  122. enum {
  123. ISCSI_CONN_INITIAL_STAGE,
  124. ISCSI_CONN_STARTED,
  125. ISCSI_CONN_STOPPED,
  126. ISCSI_CONN_CLEANUP_WAIT,
  127. };
  128. struct iscsi_conn {
  129. struct iscsi_cls_conn *cls_conn; /* ptr to class connection */
  130. void *dd_data; /* iscsi_transport data */
  131. struct iscsi_session *session; /* parent session */
  132. /*
  133. * LLDs should set this lock. It protects the transport recv
  134. * code
  135. */
  136. rwlock_t *recv_lock;
  137. /*
  138. * conn_stop() flag: stop to recover, stop to terminate
  139. */
  140. int stop_stage;
  141. struct timer_list transport_timer;
  142. unsigned long last_recv;
  143. unsigned long last_ping;
  144. int ping_timeout;
  145. int recv_timeout;
  146. struct iscsi_mgmt_task *ping_mtask;
  147. /* iSCSI connection-wide sequencing */
  148. uint32_t exp_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_mgmt_task *login_mtask; /* mtask used for login/text */
  161. struct iscsi_mgmt_task *mtask; /* xmit mtask in progress */
  162. struct iscsi_cmd_task *ctask; /* xmit ctask in progress */
  163. /* xmit */
  164. struct list_head mgmtqueue; /* mgmt (control) xmit queue */
  165. struct list_head mgmt_run_list; /* list of control tasks */
  166. struct list_head xmitqueue; /* data-path cmd queue */
  167. struct list_head run_list; /* list of cmds in progress */
  168. struct list_head requeue; /* tasks needing another run */
  169. struct work_struct xmitwork; /* per-conn. xmit workqueue */
  170. unsigned long suspend_tx; /* suspend Tx */
  171. unsigned long suspend_rx; /* suspend Rx */
  172. /* abort */
  173. wait_queue_head_t ehwait; /* used in eh_abort() */
  174. struct iscsi_tm tmhdr;
  175. struct timer_list tmf_timer;
  176. int tmf_state; /* see TMF_INITIAL, etc.*/
  177. /* negotiated params */
  178. unsigned max_recv_dlength; /* initiator_max_recv_dsl*/
  179. unsigned max_xmit_dlength; /* target_max_recv_dsl */
  180. int hdrdgst_en;
  181. int datadgst_en;
  182. int ifmarker_en;
  183. int ofmarker_en;
  184. /* values userspace uses to id a conn */
  185. int persistent_port;
  186. char *persistent_address;
  187. /* remote portal currently connected to */
  188. int portal_port;
  189. char portal_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. struct iscsi_cls_session *cls_session;
  221. /*
  222. * Syncs up the scsi eh thread with the iscsi eh thread when sending
  223. * task management functions. This must be taken before the session
  224. * and recv lock.
  225. */
  226. struct mutex eh_mutex;
  227. /* iSCSI session-wide sequencing */
  228. uint32_t cmdsn;
  229. uint32_t exp_cmdsn;
  230. uint32_t max_cmdsn;
  231. /* This tracks the reqs queued into the initiator */
  232. uint32_t queued_cmdsn;
  233. /* configuration */
  234. int abort_timeout;
  235. int lu_reset_timeout;
  236. int initial_r2t_en;
  237. unsigned max_r2t;
  238. int imm_data_en;
  239. unsigned first_burst;
  240. unsigned max_burst;
  241. int time2wait;
  242. int time2retain;
  243. int pdu_inorder_en;
  244. int dataseq_inorder_en;
  245. int erl;
  246. int fast_abort;
  247. int tpgt;
  248. char *username;
  249. char *username_in;
  250. char *password;
  251. char *password_in;
  252. char *targetname;
  253. /* control data */
  254. struct iscsi_transport *tt;
  255. struct Scsi_Host *host;
  256. struct iscsi_conn *leadconn; /* leading connection */
  257. spinlock_t lock; /* protects session state, *
  258. * sequence numbers, *
  259. * session resources: *
  260. * - cmdpool, *
  261. * - mgmtpool, *
  262. * - r2tpool */
  263. int state; /* session state */
  264. int age; /* counts session re-opens */
  265. int cmds_max; /* size of cmds array */
  266. struct iscsi_cmd_task **cmds; /* Original Cmds arr */
  267. struct iscsi_pool cmdpool; /* PDU's pool */
  268. int mgmtpool_max; /* size of mgmt array */
  269. struct iscsi_mgmt_task **mgmt_cmds; /* Original mgmt arr */
  270. struct iscsi_pool mgmtpool; /* Mgmt PDU's pool */
  271. };
  272. struct iscsi_host {
  273. char *initiatorname;
  274. /* hw address or netdev iscsi connection is bound to */
  275. char *hwaddress;
  276. char *netdev;
  277. /* local address */
  278. int local_port;
  279. char local_address[ISCSI_ADDRESS_BUF_LEN];
  280. };
  281. /*
  282. * scsi host template
  283. */
  284. extern int iscsi_change_queue_depth(struct scsi_device *sdev, int depth);
  285. extern int iscsi_eh_abort(struct scsi_cmnd *sc);
  286. extern int iscsi_eh_host_reset(struct scsi_cmnd *sc);
  287. extern int iscsi_eh_device_reset(struct scsi_cmnd *sc);
  288. extern int iscsi_queuecommand(struct scsi_cmnd *sc,
  289. void (*done)(struct scsi_cmnd *));
  290. /*
  291. * iSCSI host helpers.
  292. */
  293. #define iscsi_host_priv(_shost) \
  294. (shost_priv(_shost) + sizeof(struct iscsi_host))
  295. extern int iscsi_host_set_param(struct Scsi_Host *shost,
  296. enum iscsi_host_param param, char *buf,
  297. int buflen);
  298. extern int iscsi_host_get_param(struct Scsi_Host *shost,
  299. enum iscsi_host_param param, char *buf);
  300. extern int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev);
  301. extern struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
  302. int dd_data_size, uint16_t qdepth);
  303. extern void iscsi_host_remove(struct Scsi_Host *shost);
  304. extern void iscsi_host_free(struct Scsi_Host *shost);
  305. /*
  306. * session management
  307. */
  308. extern struct iscsi_cls_session *
  309. iscsi_session_setup(struct iscsi_transport *, struct Scsi_Host *shost,
  310. uint16_t, int, int, uint32_t);
  311. extern void iscsi_session_teardown(struct iscsi_cls_session *);
  312. extern void iscsi_session_recovery_timedout(struct iscsi_cls_session *);
  313. extern int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
  314. enum iscsi_param param, char *buf, int buflen);
  315. extern int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
  316. enum iscsi_param param, char *buf);
  317. #define iscsi_session_printk(prefix, _sess, fmt, a...) \
  318. iscsi_cls_session_printk(prefix, _sess->cls_session, fmt, ##a)
  319. /*
  320. * connection management
  321. */
  322. extern struct iscsi_cls_conn *iscsi_conn_setup(struct iscsi_cls_session *,
  323. int, uint32_t);
  324. extern void iscsi_conn_teardown(struct iscsi_cls_conn *);
  325. extern int iscsi_conn_start(struct iscsi_cls_conn *);
  326. extern void iscsi_conn_stop(struct iscsi_cls_conn *, int);
  327. extern int iscsi_conn_bind(struct iscsi_cls_session *, struct iscsi_cls_conn *,
  328. int);
  329. extern void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err);
  330. extern int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  331. enum iscsi_param param, char *buf);
  332. extern void iscsi_suspend_tx(struct iscsi_conn *conn);
  333. #define iscsi_conn_printk(prefix, _c, fmt, a...) \
  334. iscsi_cls_conn_printk(prefix, ((struct iscsi_conn *)_c)->cls_conn, \
  335. fmt, ##a)
  336. /*
  337. * pdu and task processing
  338. */
  339. extern void iscsi_update_cmdsn(struct iscsi_session *, struct iscsi_nopin *);
  340. extern void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *,
  341. struct iscsi_data *hdr);
  342. extern int iscsi_conn_send_pdu(struct iscsi_cls_conn *, struct iscsi_hdr *,
  343. char *, uint32_t);
  344. extern int iscsi_complete_pdu(struct iscsi_conn *, struct iscsi_hdr *,
  345. char *, int);
  346. extern int iscsi_verify_itt(struct iscsi_conn *, itt_t);
  347. extern struct iscsi_cmd_task *iscsi_itt_to_ctask(struct iscsi_conn *, itt_t);
  348. extern void iscsi_requeue_ctask(struct iscsi_cmd_task *ctask);
  349. extern void iscsi_free_mgmt_task(struct iscsi_conn *conn,
  350. struct iscsi_mgmt_task *mtask);
  351. /*
  352. * generic helpers
  353. */
  354. extern void iscsi_pool_free(struct iscsi_pool *);
  355. extern int iscsi_pool_init(struct iscsi_pool *, int, void ***, int);
  356. /*
  357. * inline functions to deal with padding.
  358. */
  359. static inline unsigned int
  360. iscsi_padded(unsigned int len)
  361. {
  362. return (len + ISCSI_PAD_LEN - 1) & ~(ISCSI_PAD_LEN - 1);
  363. }
  364. static inline unsigned int
  365. iscsi_padding(unsigned int len)
  366. {
  367. len &= (ISCSI_PAD_LEN - 1);
  368. if (len)
  369. len = ISCSI_PAD_LEN - len;
  370. return len;
  371. }
  372. #endif