libiscsi.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. #define ISID_SIZE 6
  60. /* Connection suspend "bit" */
  61. #define ISCSI_SUSPEND_BIT 1
  62. #define ISCSI_ITT_MASK 0x1fff
  63. #define ISCSI_TOTAL_CMDS_MAX 4096
  64. /* this must be a power of two greater than ISCSI_MGMT_CMDS_MAX */
  65. #define ISCSI_TOTAL_CMDS_MIN 16
  66. #define ISCSI_AGE_SHIFT 28
  67. #define ISCSI_AGE_MASK 0xf
  68. #define ISCSI_ADDRESS_BUF_LEN 64
  69. enum {
  70. /* this is the maximum possible storage for AHSs */
  71. ISCSI_MAX_AHS_SIZE = sizeof(struct iscsi_ecdb_ahdr) +
  72. sizeof(struct iscsi_rlength_ahdr),
  73. ISCSI_DIGEST_SIZE = sizeof(__u32),
  74. };
  75. enum {
  76. ISCSI_TASK_FREE,
  77. ISCSI_TASK_COMPLETED,
  78. ISCSI_TASK_PENDING,
  79. ISCSI_TASK_RUNNING,
  80. ISCSI_TASK_ABRT_TMF, /* aborted due to TMF */
  81. ISCSI_TASK_ABRT_SESS_RECOV, /* aborted due to session recovery */
  82. ISCSI_TASK_REQUEUE_SCSIQ, /* qcmd requeueing to scsi-ml */
  83. };
  84. struct iscsi_r2t_info {
  85. __be32 ttt; /* copied from R2T */
  86. __be32 exp_statsn; /* copied from R2T */
  87. uint32_t data_length; /* copied from R2T */
  88. uint32_t data_offset; /* copied from R2T */
  89. int data_count; /* DATA-Out payload progress */
  90. int datasn;
  91. /* LLDs should set/update these values */
  92. int sent; /* R2T sequence progress */
  93. };
  94. struct iscsi_task {
  95. /*
  96. * Because LLDs allocate their hdr differently, this is a pointer
  97. * and length to that storage. It must be setup at session
  98. * creation time.
  99. */
  100. struct iscsi_hdr *hdr;
  101. unsigned short hdr_max;
  102. unsigned short hdr_len; /* accumulated size of hdr used */
  103. /* copied values in case we need to send tmfs */
  104. itt_t hdr_itt;
  105. __be32 cmdsn;
  106. struct scsi_lun lun;
  107. int itt; /* this ITT */
  108. unsigned imm_count; /* imm-data (bytes) */
  109. /* offset in unsolicited stream (bytes); */
  110. struct iscsi_r2t_info unsol_r2t;
  111. char *data; /* mgmt payload */
  112. unsigned data_count;
  113. struct scsi_cmnd *sc; /* associated SCSI cmd*/
  114. struct iscsi_conn *conn; /* used connection */
  115. /* data processing tracking */
  116. unsigned long last_xfer;
  117. unsigned long last_timeout;
  118. bool have_checked_conn;
  119. /* T10 protection information */
  120. bool protected;
  121. /* state set/tested under session->lock */
  122. int state;
  123. atomic_t refcount;
  124. struct list_head running; /* running cmd list */
  125. void *dd_data; /* driver/transport data */
  126. };
  127. static inline int iscsi_task_has_unsol_data(struct iscsi_task *task)
  128. {
  129. return task->unsol_r2t.data_length > task->unsol_r2t.sent;
  130. }
  131. static inline void* iscsi_next_hdr(struct iscsi_task *task)
  132. {
  133. return (void*)task->hdr + task->hdr_len;
  134. }
  135. /* Connection's states */
  136. enum {
  137. ISCSI_CONN_INITIAL_STAGE,
  138. ISCSI_CONN_STARTED,
  139. ISCSI_CONN_STOPPED,
  140. ISCSI_CONN_CLEANUP_WAIT,
  141. };
  142. struct iscsi_conn {
  143. struct iscsi_cls_conn *cls_conn; /* ptr to class connection */
  144. void *dd_data; /* iscsi_transport data */
  145. struct iscsi_session *session; /* parent session */
  146. /*
  147. * conn_stop() flag: stop to recover, stop to terminate
  148. */
  149. int stop_stage;
  150. struct timer_list transport_timer;
  151. unsigned long last_recv;
  152. unsigned long last_ping;
  153. int ping_timeout;
  154. int recv_timeout;
  155. struct iscsi_task *ping_task;
  156. /* iSCSI connection-wide sequencing */
  157. uint32_t exp_statsn;
  158. uint32_t statsn;
  159. /* control data */
  160. int id; /* CID */
  161. int c_stage; /* connection state */
  162. /*
  163. * Preallocated buffer for pdus that have data but do not
  164. * originate from scsi-ml. We never have two pdus using the
  165. * buffer at the same time. It is only allocated to
  166. * the default max recv size because the pdus we support
  167. * should always fit in this buffer
  168. */
  169. char *data;
  170. struct iscsi_task *login_task; /* mtask used for login/text */
  171. struct iscsi_task *task; /* xmit task in progress */
  172. /* xmit */
  173. spinlock_t taskqueuelock; /* protects the next three lists */
  174. struct list_head mgmtqueue; /* mgmt (control) xmit queue */
  175. struct list_head cmdqueue; /* data-path cmd queue */
  176. struct list_head requeue; /* tasks needing another run */
  177. struct work_struct xmitwork; /* per-conn. xmit workqueue */
  178. unsigned long suspend_tx; /* suspend Tx */
  179. unsigned long suspend_rx; /* suspend Rx */
  180. /* abort */
  181. wait_queue_head_t ehwait; /* used in eh_abort() */
  182. struct iscsi_tm tmhdr;
  183. struct timer_list tmf_timer;
  184. int tmf_state; /* see TMF_INITIAL, etc.*/
  185. /* negotiated params */
  186. unsigned max_recv_dlength; /* initiator_max_recv_dsl*/
  187. unsigned max_xmit_dlength; /* target_max_recv_dsl */
  188. int hdrdgst_en;
  189. int datadgst_en;
  190. int ifmarker_en;
  191. int ofmarker_en;
  192. /* values userspace uses to id a conn */
  193. int persistent_port;
  194. char *persistent_address;
  195. unsigned max_segment_size;
  196. unsigned tcp_xmit_wsf;
  197. unsigned tcp_recv_wsf;
  198. uint16_t keepalive_tmo;
  199. uint16_t local_port;
  200. uint8_t tcp_timestamp_stat;
  201. uint8_t tcp_nagle_disable;
  202. uint8_t tcp_wsf_disable;
  203. uint8_t tcp_timer_scale;
  204. uint8_t tcp_timestamp_en;
  205. uint8_t fragment_disable;
  206. uint8_t ipv4_tos;
  207. uint8_t ipv6_traffic_class;
  208. uint8_t ipv6_flow_label;
  209. uint8_t is_fw_assigned_ipv6;
  210. char *local_ipaddr;
  211. /* MIB-statistics */
  212. uint64_t txdata_octets;
  213. uint64_t rxdata_octets;
  214. uint32_t scsicmd_pdus_cnt;
  215. uint32_t dataout_pdus_cnt;
  216. uint32_t scsirsp_pdus_cnt;
  217. uint32_t datain_pdus_cnt;
  218. uint32_t r2t_pdus_cnt;
  219. uint32_t tmfcmd_pdus_cnt;
  220. int32_t tmfrsp_pdus_cnt;
  221. /* custom statistics */
  222. uint32_t eh_abort_cnt;
  223. uint32_t fmr_unalign_cnt;
  224. };
  225. struct iscsi_pool {
  226. struct kfifo queue; /* FIFO Queue */
  227. void **pool; /* Pool of elements */
  228. int max; /* Max number of elements */
  229. };
  230. /* Session's states */
  231. enum {
  232. ISCSI_STATE_FREE = 1,
  233. ISCSI_STATE_LOGGED_IN,
  234. ISCSI_STATE_FAILED,
  235. ISCSI_STATE_TERMINATE,
  236. ISCSI_STATE_IN_RECOVERY,
  237. ISCSI_STATE_RECOVERY_FAILED,
  238. ISCSI_STATE_LOGGING_OUT,
  239. };
  240. struct iscsi_session {
  241. struct iscsi_cls_session *cls_session;
  242. /*
  243. * Syncs up the scsi eh thread with the iscsi eh thread when sending
  244. * task management functions. This must be taken before the session
  245. * and recv lock.
  246. */
  247. struct mutex eh_mutex;
  248. /* iSCSI session-wide sequencing */
  249. uint32_t cmdsn;
  250. uint32_t exp_cmdsn;
  251. uint32_t max_cmdsn;
  252. /* This tracks the reqs queued into the initiator */
  253. uint32_t queued_cmdsn;
  254. /* configuration */
  255. int abort_timeout;
  256. int lu_reset_timeout;
  257. int tgt_reset_timeout;
  258. int initial_r2t_en;
  259. unsigned short max_r2t;
  260. int imm_data_en;
  261. unsigned first_burst;
  262. unsigned max_burst;
  263. int time2wait;
  264. int time2retain;
  265. int pdu_inorder_en;
  266. int dataseq_inorder_en;
  267. int erl;
  268. int fast_abort;
  269. int tpgt;
  270. char *username;
  271. char *username_in;
  272. char *password;
  273. char *password_in;
  274. char *targetname;
  275. char *targetalias;
  276. char *ifacename;
  277. char *initiatorname;
  278. char *boot_root;
  279. char *boot_nic;
  280. char *boot_target;
  281. char *portal_type;
  282. char *discovery_parent_type;
  283. uint16_t discovery_parent_idx;
  284. uint16_t def_taskmgmt_tmo;
  285. uint16_t tsid;
  286. uint8_t auto_snd_tgt_disable;
  287. uint8_t discovery_sess;
  288. uint8_t chap_auth_en;
  289. uint8_t discovery_logout_en;
  290. uint8_t bidi_chap_en;
  291. uint8_t discovery_auth_optional;
  292. uint8_t isid[ISID_SIZE];
  293. /* control data */
  294. struct iscsi_transport *tt;
  295. struct Scsi_Host *host;
  296. struct iscsi_conn *leadconn; /* leading connection */
  297. /* Between the forward and the backward locks exists a strict locking
  298. * hierarchy. The mutual exclusion zone protected by the forward lock
  299. * can enclose the mutual exclusion zone protected by the backward lock
  300. * but not vice versa.
  301. */
  302. spinlock_t frwd_lock; /* protects session state, *
  303. * cmdsn, queued_cmdsn *
  304. * session resources: *
  305. * - cmdpool kfifo_out , *
  306. * - mgmtpool, */
  307. spinlock_t back_lock; /* protects cmdsn_exp *
  308. * cmdsn_max, *
  309. * cmdpool kfifo_in */
  310. int state; /* session state */
  311. int age; /* counts session re-opens */
  312. int scsi_cmds_max; /* max scsi commands */
  313. int cmds_max; /* size of cmds array */
  314. struct iscsi_task **cmds; /* Original Cmds arr */
  315. struct iscsi_pool cmdpool; /* PDU's pool */
  316. void *dd_data; /* LLD private data */
  317. };
  318. enum {
  319. ISCSI_HOST_SETUP,
  320. ISCSI_HOST_REMOVED,
  321. };
  322. struct iscsi_host {
  323. char *initiatorname;
  324. /* hw address or netdev iscsi connection is bound to */
  325. char *hwaddress;
  326. char *netdev;
  327. wait_queue_head_t session_removal_wq;
  328. /* protects sessions and state */
  329. spinlock_t lock;
  330. int num_sessions;
  331. int state;
  332. struct workqueue_struct *workq;
  333. char workq_name[20];
  334. };
  335. /*
  336. * scsi host template
  337. */
  338. extern int iscsi_eh_abort(struct scsi_cmnd *sc);
  339. extern int iscsi_eh_recover_target(struct scsi_cmnd *sc);
  340. extern int iscsi_eh_session_reset(struct scsi_cmnd *sc);
  341. extern int iscsi_eh_device_reset(struct scsi_cmnd *sc);
  342. extern int iscsi_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *sc);
  343. /*
  344. * iSCSI host helpers.
  345. */
  346. #define iscsi_host_priv(_shost) \
  347. (shost_priv(_shost) + sizeof(struct iscsi_host))
  348. extern int iscsi_host_set_param(struct Scsi_Host *shost,
  349. enum iscsi_host_param param, char *buf,
  350. int buflen);
  351. extern int iscsi_host_get_param(struct Scsi_Host *shost,
  352. enum iscsi_host_param param, char *buf);
  353. extern int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev);
  354. extern struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
  355. int dd_data_size,
  356. bool xmit_can_sleep);
  357. extern void iscsi_host_remove(struct Scsi_Host *shost);
  358. extern void iscsi_host_free(struct Scsi_Host *shost);
  359. extern int iscsi_target_alloc(struct scsi_target *starget);
  360. /*
  361. * session management
  362. */
  363. extern struct iscsi_cls_session *
  364. iscsi_session_setup(struct iscsi_transport *, struct Scsi_Host *shost,
  365. uint16_t, int, int, uint32_t, unsigned int);
  366. extern void iscsi_session_teardown(struct iscsi_cls_session *);
  367. extern void iscsi_session_recovery_timedout(struct iscsi_cls_session *);
  368. extern int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
  369. enum iscsi_param param, char *buf, int buflen);
  370. extern int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
  371. enum iscsi_param param, char *buf);
  372. #define iscsi_session_printk(prefix, _sess, fmt, a...) \
  373. iscsi_cls_session_printk(prefix, _sess->cls_session, fmt, ##a)
  374. /*
  375. * connection management
  376. */
  377. extern struct iscsi_cls_conn *iscsi_conn_setup(struct iscsi_cls_session *,
  378. int, uint32_t);
  379. extern void iscsi_conn_teardown(struct iscsi_cls_conn *);
  380. extern int iscsi_conn_start(struct iscsi_cls_conn *);
  381. extern void iscsi_conn_stop(struct iscsi_cls_conn *, int);
  382. extern int iscsi_conn_bind(struct iscsi_cls_session *, struct iscsi_cls_conn *,
  383. int);
  384. extern void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err);
  385. extern void iscsi_session_failure(struct iscsi_session *session,
  386. enum iscsi_err err);
  387. extern int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  388. enum iscsi_param param, char *buf);
  389. extern int iscsi_conn_get_addr_param(struct sockaddr_storage *addr,
  390. enum iscsi_param param, char *buf);
  391. extern void iscsi_suspend_tx(struct iscsi_conn *conn);
  392. extern void iscsi_suspend_queue(struct iscsi_conn *conn);
  393. extern void iscsi_conn_queue_work(struct iscsi_conn *conn);
  394. #define iscsi_conn_printk(prefix, _c, fmt, a...) \
  395. iscsi_cls_conn_printk(prefix, ((struct iscsi_conn *)_c)->cls_conn, \
  396. fmt, ##a)
  397. /*
  398. * pdu and task processing
  399. */
  400. extern void iscsi_update_cmdsn(struct iscsi_session *, struct iscsi_nopin *);
  401. extern void iscsi_prep_data_out_pdu(struct iscsi_task *task,
  402. struct iscsi_r2t_info *r2t,
  403. struct iscsi_data *hdr);
  404. extern int iscsi_conn_send_pdu(struct iscsi_cls_conn *, struct iscsi_hdr *,
  405. char *, uint32_t);
  406. extern int iscsi_complete_pdu(struct iscsi_conn *, struct iscsi_hdr *,
  407. char *, int);
  408. extern int __iscsi_complete_pdu(struct iscsi_conn *, struct iscsi_hdr *,
  409. char *, int);
  410. extern int iscsi_verify_itt(struct iscsi_conn *, itt_t);
  411. extern struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *, itt_t);
  412. extern struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *, itt_t);
  413. extern void iscsi_requeue_task(struct iscsi_task *task);
  414. extern void iscsi_put_task(struct iscsi_task *task);
  415. extern void __iscsi_put_task(struct iscsi_task *task);
  416. extern void __iscsi_get_task(struct iscsi_task *task);
  417. extern void iscsi_complete_scsi_task(struct iscsi_task *task,
  418. uint32_t exp_cmdsn, uint32_t max_cmdsn);
  419. /*
  420. * generic helpers
  421. */
  422. extern void iscsi_pool_free(struct iscsi_pool *);
  423. extern int iscsi_pool_init(struct iscsi_pool *, int, void ***, int);
  424. extern int iscsi_switch_str_param(char **, char *);
  425. /*
  426. * inline functions to deal with padding.
  427. */
  428. static inline unsigned int
  429. iscsi_padded(unsigned int len)
  430. {
  431. return (len + ISCSI_PAD_LEN - 1) & ~(ISCSI_PAD_LEN - 1);
  432. }
  433. static inline unsigned int
  434. iscsi_padding(unsigned int len)
  435. {
  436. len &= (ISCSI_PAD_LEN - 1);
  437. if (len)
  438. len = ISCSI_PAD_LEN - len;
  439. return len;
  440. }
  441. #endif