libiscsi.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. #include <scsi/scsi_transport_iscsi.h>
  33. struct scsi_transport_template;
  34. struct scsi_host_template;
  35. struct scsi_device;
  36. struct Scsi_Host;
  37. struct scsi_target;
  38. struct scsi_cmnd;
  39. struct socket;
  40. struct iscsi_transport;
  41. struct iscsi_cls_session;
  42. struct iscsi_cls_conn;
  43. struct iscsi_session;
  44. struct iscsi_nopin;
  45. struct device;
  46. #define ISCSI_DEF_XMIT_CMDS_MAX 128 /* must be power of 2 */
  47. #define ISCSI_MGMT_CMDS_MAX 15
  48. #define ISCSI_DEF_CMD_PER_LUN 32
  49. /* Task Mgmt states */
  50. enum {
  51. TMF_INITIAL,
  52. TMF_QUEUED,
  53. TMF_SUCCESS,
  54. TMF_FAILED,
  55. TMF_TIMEDOUT,
  56. TMF_NOT_FOUND,
  57. };
  58. /* Connection suspend "bit" */
  59. #define ISCSI_SUSPEND_BIT 1
  60. #define ISCSI_ITT_MASK 0x1fff
  61. #define ISCSI_TOTAL_CMDS_MAX 4096
  62. /* this must be a power of two greater than ISCSI_MGMT_CMDS_MAX */
  63. #define ISCSI_TOTAL_CMDS_MIN 16
  64. #define ISCSI_AGE_SHIFT 28
  65. #define ISCSI_AGE_MASK 0xf
  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. enum {
  74. ISCSI_TASK_COMPLETED,
  75. ISCSI_TASK_PENDING,
  76. ISCSI_TASK_RUNNING,
  77. };
  78. struct iscsi_r2t_info {
  79. __be32 ttt; /* copied from R2T */
  80. __be32 exp_statsn; /* copied from R2T */
  81. uint32_t data_length; /* copied from R2T */
  82. uint32_t data_offset; /* copied from R2T */
  83. int data_count; /* DATA-Out payload progress */
  84. int datasn;
  85. /* LLDs should set/update these values */
  86. int sent; /* R2T sequence progress */
  87. };
  88. struct iscsi_task {
  89. /*
  90. * Because LLDs allocate their hdr differently, this is a pointer
  91. * and length to that storage. It must be setup at session
  92. * creation time.
  93. */
  94. struct iscsi_hdr *hdr;
  95. unsigned short hdr_max;
  96. unsigned short hdr_len; /* accumulated size of hdr used */
  97. /* copied values in case we need to send tmfs */
  98. itt_t hdr_itt;
  99. __be32 cmdsn;
  100. uint8_t lun[8];
  101. int itt; /* this ITT */
  102. unsigned imm_count; /* imm-data (bytes) */
  103. /* offset in unsolicited stream (bytes); */
  104. struct iscsi_r2t_info unsol_r2t;
  105. char *data; /* mgmt payload */
  106. unsigned data_count;
  107. struct scsi_cmnd *sc; /* associated SCSI cmd*/
  108. struct iscsi_conn *conn; /* used connection */
  109. /* state set/tested under session->lock */
  110. int state;
  111. atomic_t refcount;
  112. struct list_head running; /* running cmd list */
  113. void *dd_data; /* driver/transport data */
  114. };
  115. static inline int iscsi_task_has_unsol_data(struct iscsi_task *task)
  116. {
  117. return task->unsol_r2t.data_length > task->unsol_r2t.sent;
  118. }
  119. static inline void* iscsi_next_hdr(struct iscsi_task *task)
  120. {
  121. return (void*)task->hdr + task->hdr_len;
  122. }
  123. /* Connection's states */
  124. enum {
  125. ISCSI_CONN_INITIAL_STAGE,
  126. ISCSI_CONN_STARTED,
  127. ISCSI_CONN_STOPPED,
  128. ISCSI_CONN_CLEANUP_WAIT,
  129. };
  130. struct iscsi_conn {
  131. struct iscsi_cls_conn *cls_conn; /* ptr to class connection */
  132. void *dd_data; /* iscsi_transport data */
  133. struct iscsi_session *session; /* parent session */
  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_task *ping_task;
  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_task *login_task; /* mtask used for login/text */
  158. struct iscsi_task *task; /* xmit task in progress */
  159. /* xmit */
  160. struct list_head mgmtqueue; /* mgmt (control) xmit queue */
  161. struct list_head mgmt_run_list; /* list of control tasks */
  162. struct list_head xmitqueue; /* data-path cmd queue */
  163. struct list_head run_list; /* list of cmds in progress */
  164. struct list_head requeue; /* tasks needing another run */
  165. struct work_struct xmitwork; /* per-conn. xmit workqueue */
  166. unsigned long suspend_tx; /* suspend Tx */
  167. unsigned long suspend_rx; /* suspend Rx */
  168. /* abort */
  169. wait_queue_head_t ehwait; /* used in eh_abort() */
  170. struct iscsi_tm tmhdr;
  171. struct timer_list tmf_timer;
  172. int tmf_state; /* see TMF_INITIAL, etc.*/
  173. /* negotiated params */
  174. unsigned max_recv_dlength; /* initiator_max_recv_dsl*/
  175. unsigned max_xmit_dlength; /* target_max_recv_dsl */
  176. int hdrdgst_en;
  177. int datadgst_en;
  178. int ifmarker_en;
  179. int ofmarker_en;
  180. /* values userspace uses to id a conn */
  181. int persistent_port;
  182. char *persistent_address;
  183. /* remote portal currently connected to */
  184. int portal_port;
  185. char portal_address[ISCSI_ADDRESS_BUF_LEN];
  186. /* MIB-statistics */
  187. uint64_t txdata_octets;
  188. uint64_t rxdata_octets;
  189. uint32_t scsicmd_pdus_cnt;
  190. uint32_t dataout_pdus_cnt;
  191. uint32_t scsirsp_pdus_cnt;
  192. uint32_t datain_pdus_cnt;
  193. uint32_t r2t_pdus_cnt;
  194. uint32_t tmfcmd_pdus_cnt;
  195. int32_t tmfrsp_pdus_cnt;
  196. /* custom statistics */
  197. uint32_t eh_abort_cnt;
  198. uint32_t fmr_unalign_cnt;
  199. };
  200. struct iscsi_pool {
  201. struct kfifo *queue; /* FIFO Queue */
  202. void **pool; /* Pool of elements */
  203. int max; /* Max number of elements */
  204. };
  205. /* Session's states */
  206. enum {
  207. ISCSI_STATE_FREE = 1,
  208. ISCSI_STATE_LOGGED_IN,
  209. ISCSI_STATE_FAILED,
  210. ISCSI_STATE_TERMINATE,
  211. ISCSI_STATE_IN_RECOVERY,
  212. ISCSI_STATE_RECOVERY_FAILED,
  213. ISCSI_STATE_LOGGING_OUT,
  214. };
  215. struct iscsi_session {
  216. struct iscsi_cls_session *cls_session;
  217. /*
  218. * Syncs up the scsi eh thread with the iscsi eh thread when sending
  219. * task management functions. This must be taken before the session
  220. * and recv lock.
  221. */
  222. struct mutex eh_mutex;
  223. /* iSCSI session-wide sequencing */
  224. uint32_t cmdsn;
  225. uint32_t exp_cmdsn;
  226. uint32_t max_cmdsn;
  227. /* This tracks the reqs queued into the initiator */
  228. uint32_t queued_cmdsn;
  229. /* configuration */
  230. int abort_timeout;
  231. int lu_reset_timeout;
  232. int initial_r2t_en;
  233. unsigned max_r2t;
  234. int imm_data_en;
  235. unsigned first_burst;
  236. unsigned max_burst;
  237. int time2wait;
  238. int time2retain;
  239. int pdu_inorder_en;
  240. int dataseq_inorder_en;
  241. int erl;
  242. int fast_abort;
  243. int tpgt;
  244. char *username;
  245. char *username_in;
  246. char *password;
  247. char *password_in;
  248. char *targetname;
  249. char *ifacename;
  250. char *initiatorname;
  251. /* control data */
  252. struct iscsi_transport *tt;
  253. struct Scsi_Host *host;
  254. struct iscsi_conn *leadconn; /* leading connection */
  255. spinlock_t lock; /* protects session state, *
  256. * sequence numbers, *
  257. * session resources: *
  258. * - cmdpool, *
  259. * - mgmtpool, *
  260. * - r2tpool */
  261. int state; /* session state */
  262. int age; /* counts session re-opens */
  263. int scsi_cmds_max; /* max scsi commands */
  264. int cmds_max; /* size of cmds array */
  265. struct iscsi_task **cmds; /* Original Cmds arr */
  266. struct iscsi_pool cmdpool; /* PDU's pool */
  267. };
  268. enum {
  269. ISCSI_HOST_SETUP,
  270. ISCSI_HOST_REMOVED,
  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. wait_queue_head_t session_removal_wq;
  281. /* protects sessions and state */
  282. spinlock_t lock;
  283. int num_sessions;
  284. int state;
  285. struct workqueue_struct *workq;
  286. char workq_name[20];
  287. };
  288. /*
  289. * scsi host template
  290. */
  291. extern int iscsi_change_queue_depth(struct scsi_device *sdev, int depth);
  292. extern int iscsi_eh_abort(struct scsi_cmnd *sc);
  293. extern int iscsi_eh_target_reset(struct scsi_cmnd *sc);
  294. extern int iscsi_eh_device_reset(struct scsi_cmnd *sc);
  295. extern int iscsi_queuecommand(struct scsi_cmnd *sc,
  296. void (*done)(struct scsi_cmnd *));
  297. /*
  298. * iSCSI host helpers.
  299. */
  300. #define iscsi_host_priv(_shost) \
  301. (shost_priv(_shost) + sizeof(struct iscsi_host))
  302. extern int iscsi_host_set_param(struct Scsi_Host *shost,
  303. enum iscsi_host_param param, char *buf,
  304. int buflen);
  305. extern int iscsi_host_get_param(struct Scsi_Host *shost,
  306. enum iscsi_host_param param, char *buf);
  307. extern int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev);
  308. extern struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
  309. int dd_data_size,
  310. bool xmit_can_sleep);
  311. extern void iscsi_host_remove(struct Scsi_Host *shost);
  312. extern void iscsi_host_free(struct Scsi_Host *shost);
  313. extern int iscsi_target_alloc(struct scsi_target *starget);
  314. /*
  315. * session management
  316. */
  317. extern struct iscsi_cls_session *
  318. iscsi_session_setup(struct iscsi_transport *, struct Scsi_Host *shost,
  319. uint16_t, int, uint32_t, unsigned int);
  320. extern void iscsi_session_teardown(struct iscsi_cls_session *);
  321. extern void iscsi_session_recovery_timedout(struct iscsi_cls_session *);
  322. extern int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
  323. enum iscsi_param param, char *buf, int buflen);
  324. extern int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
  325. enum iscsi_param param, char *buf);
  326. #define iscsi_session_printk(prefix, _sess, fmt, a...) \
  327. iscsi_cls_session_printk(prefix, _sess->cls_session, fmt, ##a)
  328. /*
  329. * connection management
  330. */
  331. extern struct iscsi_cls_conn *iscsi_conn_setup(struct iscsi_cls_session *,
  332. int, uint32_t);
  333. extern void iscsi_conn_teardown(struct iscsi_cls_conn *);
  334. extern int iscsi_conn_start(struct iscsi_cls_conn *);
  335. extern void iscsi_conn_stop(struct iscsi_cls_conn *, int);
  336. extern int iscsi_conn_bind(struct iscsi_cls_session *, struct iscsi_cls_conn *,
  337. int);
  338. extern void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err);
  339. extern void iscsi_session_failure(struct iscsi_session *session,
  340. enum iscsi_err err);
  341. extern int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  342. enum iscsi_param param, char *buf);
  343. extern void iscsi_suspend_tx(struct iscsi_conn *conn);
  344. extern void iscsi_conn_queue_work(struct iscsi_conn *conn);
  345. #define iscsi_conn_printk(prefix, _c, fmt, a...) \
  346. iscsi_cls_conn_printk(prefix, ((struct iscsi_conn *)_c)->cls_conn, \
  347. fmt, ##a)
  348. /*
  349. * pdu and task processing
  350. */
  351. extern void iscsi_update_cmdsn(struct iscsi_session *, struct iscsi_nopin *);
  352. extern void iscsi_prep_data_out_pdu(struct iscsi_task *task,
  353. struct iscsi_r2t_info *r2t,
  354. struct iscsi_data *hdr);
  355. extern int iscsi_conn_send_pdu(struct iscsi_cls_conn *, struct iscsi_hdr *,
  356. char *, uint32_t);
  357. extern int iscsi_complete_pdu(struct iscsi_conn *, struct iscsi_hdr *,
  358. char *, int);
  359. extern int __iscsi_complete_pdu(struct iscsi_conn *, struct iscsi_hdr *,
  360. char *, int);
  361. extern int iscsi_verify_itt(struct iscsi_conn *, itt_t);
  362. extern struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *, itt_t);
  363. extern void iscsi_requeue_task(struct iscsi_task *task);
  364. extern void iscsi_put_task(struct iscsi_task *task);
  365. extern void __iscsi_get_task(struct iscsi_task *task);
  366. /*
  367. * generic helpers
  368. */
  369. extern void iscsi_pool_free(struct iscsi_pool *);
  370. extern int iscsi_pool_init(struct iscsi_pool *, int, void ***, int);
  371. /*
  372. * inline functions to deal with padding.
  373. */
  374. static inline unsigned int
  375. iscsi_padded(unsigned int len)
  376. {
  377. return (len + ISCSI_PAD_LEN - 1) & ~(ISCSI_PAD_LEN - 1);
  378. }
  379. static inline unsigned int
  380. iscsi_padding(unsigned int len)
  381. {
  382. len &= (ISCSI_PAD_LEN - 1);
  383. if (len)
  384. len = ISCSI_PAD_LEN - len;
  385. return len;
  386. }
  387. #endif