libiscsi.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 <scsi/iscsi_proto.h>
  32. #include <scsi/iscsi_if.h>
  33. #include <scsi/scsi_transport_iscsi.h>
  34. struct scsi_transport_template;
  35. struct scsi_host_template;
  36. struct scsi_device;
  37. struct Scsi_Host;
  38. struct scsi_target;
  39. struct scsi_cmnd;
  40. struct socket;
  41. struct iscsi_transport;
  42. struct iscsi_cls_session;
  43. struct iscsi_cls_conn;
  44. struct iscsi_session;
  45. struct iscsi_nopin;
  46. struct device;
  47. #define ISCSI_DEF_XMIT_CMDS_MAX 128 /* must be power of 2 */
  48. #define ISCSI_MGMT_CMDS_MAX 15
  49. #define ISCSI_DEF_CMD_PER_LUN 32
  50. /* Task Mgmt states */
  51. enum {
  52. TMF_INITIAL,
  53. TMF_QUEUED,
  54. TMF_SUCCESS,
  55. TMF_FAILED,
  56. TMF_TIMEDOUT,
  57. TMF_NOT_FOUND,
  58. };
  59. /* Connection suspend "bit" */
  60. #define ISCSI_SUSPEND_BIT 1
  61. #define ISCSI_ITT_MASK 0x1fff
  62. #define ISCSI_TOTAL_CMDS_MAX 4096
  63. /* this must be a power of two greater than ISCSI_MGMT_CMDS_MAX */
  64. #define ISCSI_TOTAL_CMDS_MIN 16
  65. #define ISCSI_AGE_SHIFT 28
  66. #define ISCSI_AGE_MASK 0xf
  67. #define ISCSI_ADDRESS_BUF_LEN 64
  68. enum {
  69. /* this is the maximum possible storage for AHSs */
  70. ISCSI_MAX_AHS_SIZE = sizeof(struct iscsi_ecdb_ahdr) +
  71. sizeof(struct iscsi_rlength_ahdr),
  72. ISCSI_DIGEST_SIZE = sizeof(__u32),
  73. };
  74. enum {
  75. ISCSI_TASK_FREE,
  76. ISCSI_TASK_COMPLETED,
  77. ISCSI_TASK_PENDING,
  78. ISCSI_TASK_RUNNING,
  79. ISCSI_TASK_ABRT_TMF, /* aborted due to TMF */
  80. ISCSI_TASK_ABRT_SESS_RECOV, /* aborted due to session recovery */
  81. ISCSI_TASK_REQUEUE_SCSIQ, /* qcmd requeueing to scsi-ml */
  82. };
  83. struct iscsi_r2t_info {
  84. __be32 ttt; /* copied from R2T */
  85. __be32 exp_statsn; /* copied from R2T */
  86. uint32_t data_length; /* copied from R2T */
  87. uint32_t data_offset; /* copied from R2T */
  88. int data_count; /* DATA-Out payload progress */
  89. int datasn;
  90. /* LLDs should set/update these values */
  91. int sent; /* R2T sequence progress */
  92. };
  93. struct iscsi_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_hdr *hdr;
  100. unsigned short hdr_max;
  101. unsigned short hdr_len; /* accumulated size of hdr used */
  102. /* copied values in case we need to send tmfs */
  103. itt_t hdr_itt;
  104. __be32 cmdsn;
  105. uint8_t lun[8];
  106. int itt; /* this ITT */
  107. unsigned imm_count; /* imm-data (bytes) */
  108. /* offset in unsolicited stream (bytes); */
  109. struct iscsi_r2t_info unsol_r2t;
  110. char *data; /* mgmt payload */
  111. unsigned data_count;
  112. struct scsi_cmnd *sc; /* associated SCSI cmd*/
  113. struct iscsi_conn *conn; /* used connection */
  114. /* data processing tracking */
  115. unsigned long last_xfer;
  116. unsigned long last_timeout;
  117. bool have_checked_conn;
  118. /* state set/tested under session->lock */
  119. int state;
  120. atomic_t refcount;
  121. struct list_head running; /* running cmd list */
  122. void *dd_data; /* driver/transport data */
  123. };
  124. static inline int iscsi_task_has_unsol_data(struct iscsi_task *task)
  125. {
  126. return task->unsol_r2t.data_length > task->unsol_r2t.sent;
  127. }
  128. static inline void* iscsi_next_hdr(struct iscsi_task *task)
  129. {
  130. return (void*)task->hdr + task->hdr_len;
  131. }
  132. /* Connection's states */
  133. enum {
  134. ISCSI_CONN_INITIAL_STAGE,
  135. ISCSI_CONN_STARTED,
  136. ISCSI_CONN_STOPPED,
  137. ISCSI_CONN_CLEANUP_WAIT,
  138. };
  139. struct iscsi_conn {
  140. struct iscsi_cls_conn *cls_conn; /* ptr to class connection */
  141. void *dd_data; /* iscsi_transport data */
  142. struct iscsi_session *session; /* parent session */
  143. /*
  144. * conn_stop() flag: stop to recover, stop to terminate
  145. */
  146. int stop_stage;
  147. struct timer_list transport_timer;
  148. unsigned long last_recv;
  149. unsigned long last_ping;
  150. int ping_timeout;
  151. int recv_timeout;
  152. struct iscsi_task *ping_task;
  153. /* iSCSI connection-wide sequencing */
  154. uint32_t exp_statsn;
  155. /* control data */
  156. int id; /* CID */
  157. int c_stage; /* connection state */
  158. /*
  159. * Preallocated buffer for pdus that have data but do not
  160. * originate from scsi-ml. We never have two pdus using the
  161. * buffer at the same time. It is only allocated to
  162. * the default max recv size because the pdus we support
  163. * should always fit in this buffer
  164. */
  165. char *data;
  166. struct iscsi_task *login_task; /* mtask used for login/text */
  167. struct iscsi_task *task; /* xmit task in progress */
  168. /* xmit */
  169. struct list_head mgmtqueue; /* mgmt (control) xmit queue */
  170. struct list_head cmdqueue; /* data-path cmd queue */
  171. struct list_head requeue; /* tasks needing another run */
  172. struct work_struct xmitwork; /* per-conn. xmit workqueue */
  173. unsigned long suspend_tx; /* suspend Tx */
  174. unsigned long suspend_rx; /* suspend Rx */
  175. /* abort */
  176. wait_queue_head_t ehwait; /* used in eh_abort() */
  177. struct iscsi_tm tmhdr;
  178. struct timer_list tmf_timer;
  179. int tmf_state; /* see TMF_INITIAL, etc.*/
  180. /* negotiated params */
  181. unsigned max_recv_dlength; /* initiator_max_recv_dsl*/
  182. unsigned max_xmit_dlength; /* target_max_recv_dsl */
  183. int hdrdgst_en;
  184. int datadgst_en;
  185. int ifmarker_en;
  186. int ofmarker_en;
  187. /* values userspace uses to id a conn */
  188. int persistent_port;
  189. char *persistent_address;
  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 tgt_reset_timeout;
  237. int initial_r2t_en;
  238. unsigned max_r2t;
  239. int imm_data_en;
  240. unsigned first_burst;
  241. unsigned max_burst;
  242. int time2wait;
  243. int time2retain;
  244. int pdu_inorder_en;
  245. int dataseq_inorder_en;
  246. int erl;
  247. int fast_abort;
  248. int tpgt;
  249. char *username;
  250. char *username_in;
  251. char *password;
  252. char *password_in;
  253. char *targetname;
  254. char *ifacename;
  255. char *initiatorname;
  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 scsi_cmds_max; /* max scsi commands */
  269. int cmds_max; /* size of cmds array */
  270. struct iscsi_task **cmds; /* Original Cmds arr */
  271. struct iscsi_pool cmdpool; /* PDU's pool */
  272. void *dd_data; /* LLD private data */
  273. };
  274. enum {
  275. ISCSI_HOST_SETUP,
  276. ISCSI_HOST_REMOVED,
  277. };
  278. struct iscsi_host {
  279. char *initiatorname;
  280. /* hw address or netdev iscsi connection is bound to */
  281. char *hwaddress;
  282. char *netdev;
  283. wait_queue_head_t session_removal_wq;
  284. /* protects sessions and state */
  285. spinlock_t lock;
  286. int num_sessions;
  287. int state;
  288. struct workqueue_struct *workq;
  289. char workq_name[20];
  290. };
  291. /*
  292. * scsi host template
  293. */
  294. extern int iscsi_change_queue_depth(struct scsi_device *sdev, int depth,
  295. int reason);
  296. extern int iscsi_eh_abort(struct scsi_cmnd *sc);
  297. extern int iscsi_eh_recover_target(struct scsi_cmnd *sc);
  298. extern int iscsi_eh_session_reset(struct scsi_cmnd *sc);
  299. extern int iscsi_eh_device_reset(struct scsi_cmnd *sc);
  300. extern int iscsi_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *sc);
  301. /*
  302. * iSCSI host helpers.
  303. */
  304. #define iscsi_host_priv(_shost) \
  305. (shost_priv(_shost) + sizeof(struct iscsi_host))
  306. extern int iscsi_host_set_param(struct Scsi_Host *shost,
  307. enum iscsi_host_param param, char *buf,
  308. int buflen);
  309. extern int iscsi_host_get_param(struct Scsi_Host *shost,
  310. enum iscsi_host_param param, char *buf);
  311. extern int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev);
  312. extern struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
  313. int dd_data_size,
  314. bool xmit_can_sleep);
  315. extern void iscsi_host_remove(struct Scsi_Host *shost);
  316. extern void iscsi_host_free(struct Scsi_Host *shost);
  317. extern int iscsi_target_alloc(struct scsi_target *starget);
  318. /*
  319. * session management
  320. */
  321. extern struct iscsi_cls_session *
  322. iscsi_session_setup(struct iscsi_transport *, struct Scsi_Host *shost,
  323. uint16_t, int, int, uint32_t, unsigned int);
  324. extern void iscsi_session_teardown(struct iscsi_cls_session *);
  325. extern void iscsi_session_recovery_timedout(struct iscsi_cls_session *);
  326. extern int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
  327. enum iscsi_param param, char *buf, int buflen);
  328. extern int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
  329. enum iscsi_param param, char *buf);
  330. #define iscsi_session_printk(prefix, _sess, fmt, a...) \
  331. iscsi_cls_session_printk(prefix, _sess->cls_session, fmt, ##a)
  332. /*
  333. * connection management
  334. */
  335. extern struct iscsi_cls_conn *iscsi_conn_setup(struct iscsi_cls_session *,
  336. int, uint32_t);
  337. extern void iscsi_conn_teardown(struct iscsi_cls_conn *);
  338. extern int iscsi_conn_start(struct iscsi_cls_conn *);
  339. extern void iscsi_conn_stop(struct iscsi_cls_conn *, int);
  340. extern int iscsi_conn_bind(struct iscsi_cls_session *, struct iscsi_cls_conn *,
  341. int);
  342. extern void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err);
  343. extern void iscsi_session_failure(struct iscsi_session *session,
  344. enum iscsi_err err);
  345. extern int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  346. enum iscsi_param param, char *buf);
  347. extern int iscsi_conn_get_addr_param(struct sockaddr_storage *addr,
  348. enum iscsi_param param, char *buf);
  349. extern void iscsi_suspend_tx(struct iscsi_conn *conn);
  350. extern void iscsi_suspend_queue(struct iscsi_conn *conn);
  351. extern void iscsi_conn_queue_work(struct iscsi_conn *conn);
  352. #define iscsi_conn_printk(prefix, _c, fmt, a...) \
  353. iscsi_cls_conn_printk(prefix, ((struct iscsi_conn *)_c)->cls_conn, \
  354. fmt, ##a)
  355. /*
  356. * pdu and task processing
  357. */
  358. extern void iscsi_update_cmdsn(struct iscsi_session *, struct iscsi_nopin *);
  359. extern void iscsi_prep_data_out_pdu(struct iscsi_task *task,
  360. struct iscsi_r2t_info *r2t,
  361. struct iscsi_data *hdr);
  362. extern int iscsi_conn_send_pdu(struct iscsi_cls_conn *, struct iscsi_hdr *,
  363. char *, uint32_t);
  364. extern int iscsi_complete_pdu(struct iscsi_conn *, struct iscsi_hdr *,
  365. char *, int);
  366. extern int __iscsi_complete_pdu(struct iscsi_conn *, struct iscsi_hdr *,
  367. char *, int);
  368. extern int iscsi_verify_itt(struct iscsi_conn *, itt_t);
  369. extern struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *, itt_t);
  370. extern struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *, itt_t);
  371. extern void iscsi_requeue_task(struct iscsi_task *task);
  372. extern void iscsi_put_task(struct iscsi_task *task);
  373. extern void __iscsi_put_task(struct iscsi_task *task);
  374. extern void __iscsi_get_task(struct iscsi_task *task);
  375. extern void iscsi_complete_scsi_task(struct iscsi_task *task,
  376. uint32_t exp_cmdsn, uint32_t max_cmdsn);
  377. /*
  378. * generic helpers
  379. */
  380. extern void iscsi_pool_free(struct iscsi_pool *);
  381. extern int iscsi_pool_init(struct iscsi_pool *, int, void ***, int);
  382. /*
  383. * inline functions to deal with padding.
  384. */
  385. static inline unsigned int
  386. iscsi_padded(unsigned int len)
  387. {
  388. return (len + ISCSI_PAD_LEN - 1) & ~(ISCSI_PAD_LEN - 1);
  389. }
  390. static inline unsigned int
  391. iscsi_padding(unsigned int len)
  392. {
  393. len &= (ISCSI_PAD_LEN - 1);
  394. if (len)
  395. len = ISCSI_PAD_LEN - len;
  396. return len;
  397. }
  398. #endif