iscsi.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #ifndef CCAN_ISCSI_H
  15. #define CCAN_ISCSI_H
  16. struct iscsi_context;
  17. struct sockaddr;
  18. /*
  19. * Returns the file descriptor that libiscsi uses.
  20. */
  21. int iscsi_get_fd(struct iscsi_context *iscsi);
  22. /*
  23. * Returns which events that we need to poll for for the iscsi file descriptor.
  24. */
  25. int iscsi_which_events(struct iscsi_context *iscsi);
  26. /*
  27. * Called to process the events when events become available for the iscsi file descriptor.
  28. */
  29. int iscsi_service(struct iscsi_context *iscsi, int revents);
  30. /*
  31. * Create a context for an ISCSI session.
  32. * Initiator_name is the iqn name we want to identify to the target as.
  33. *
  34. * Returns:
  35. * 0: success
  36. * <0: error
  37. */
  38. struct iscsi_context *iscsi_create_context(const char *initiator_name);
  39. /*
  40. * Destroy an existing ISCSI context and tear down any existing connection.
  41. * Callbacks for any command in flight will be invoked with ISCSI_STATUS_CANCELLED.
  42. *
  43. * Returns:
  44. * 0: success
  45. * <0: error
  46. */
  47. int iscsi_destroy_context(struct iscsi_context *iscsi);
  48. /*
  49. * Set an optional alias name to identify with when connecting to the target
  50. *
  51. * Returns:
  52. * 0: success
  53. * <0: error
  54. */
  55. int iscsi_set_alias(struct iscsi_context *iscsi, const char *alias);
  56. /*
  57. * Set the iqn name of the taqget to login to.
  58. * The target name must be set before a normal-login can be initiated.
  59. * Only discovery-logins are possible without setting the target iqn name.
  60. *
  61. * Returns:
  62. * 0: success
  63. * <0: error
  64. */
  65. int iscsi_set_targetname(struct iscsi_context *iscsi, const char *targetname);
  66. /* Types of icsi sessions. Discovery sessions are used to query for what targets exist behind
  67. * the portal connected to. Normal sessions are used to log in and do I/O to the SCSI LUNs
  68. */
  69. enum iscsi_session_type {ISCSI_SESSION_DISCOVERY=1, ISCSI_SESSION_NORMAL=2};
  70. /*
  71. * Set the session type for a scsi context.
  72. * Session type can only be set/changed while the iscsi context is not logged in to
  73. * a target.
  74. *
  75. * Returns:
  76. * 0: success
  77. * <0: error
  78. */
  79. int iscsi_set_session_type(struct iscsi_context *iscsi, enum iscsi_session_type session_type);
  80. /* ISCSI_STATUS_GOOD must map to SCSI_STATUS_GOOD
  81. * and ISCSI_STATUS_CHECK_CONDITION must map to SCSI_STATUS_CHECK_CONDITION
  82. */
  83. enum icsi_status { ISCSI_STATUS_GOOD =0,
  84. ISCSI_STATUS_CHECK_CONDITION =2,
  85. ISCSI_STATUS_CANCELLED =0x0f000000,
  86. ISCSI_STATUS_ERROR =0x0f000001 };
  87. /*
  88. * Generic callback for completion of iscsi_*_async().
  89. * command_data depends on status.
  90. */
  91. typedef void (*iscsi_command_cb)(struct iscsi_context *iscsi, int status, void *command_data, void *private_data);
  92. /*
  93. * Asynchronous call to connect a TCP connection to the target-host/port
  94. *
  95. * Returns:
  96. * 0 if the call was initiated and a connection will be attempted. Result of the connection will be reported
  97. * through the callback function.
  98. * <0 if there was an error. The callback function will not be invoked.
  99. *
  100. * This command is unique in that the callback can be invoked twice.
  101. *
  102. * Callback parameters :
  103. * status can be either of :
  104. * ISCSI_STATUS_GOOD : Connection was successful. Command_data is NULL.
  105. * In this case the callback will be invoked a second time once the connection
  106. * is torn down.
  107. *
  108. * ISCSI_STATUS_ERROR : Either failed to establish the connection, or an already established connection
  109. * has failed with an error.
  110. *
  111. * The callback will NOT be invoked if the session is explicitly torn down through a call to
  112. * iscsi_disconnect() or iscsi_destroy_context().
  113. */
  114. int iscsi_connect_async(struct iscsi_context *iscsi, const char *target, iscsi_command_cb cb, void *private_data);
  115. /*
  116. * Disconnect a connection to a target.
  117. * You can not disconnect while being logged in to a target.
  118. *
  119. * Returns:
  120. * 0 disconnect was successful
  121. * <0 error
  122. */
  123. int iscsi_disconnect(struct iscsi_context *iscsi);
  124. /*
  125. * Asynchronous call to perform an ISCSI login.
  126. *
  127. * Returns:
  128. * 0 if the call was initiated and a login will be attempted. Result of the login will be reported
  129. * through the callback function.
  130. * <0 if there was an error. The callback function will not be invoked.
  131. *
  132. * Callback parameters :
  133. * status can be either of :
  134. * ISCSI_STATUS_GOOD : login was successful. Command_data is always NULL.
  135. * ISCSI_STATUS_CANCELLED: login was aborted. Command_data is NULL.
  136. * ISCSI_STATUS_ERROR : login failed. Command_data is NULL.
  137. */
  138. int iscsi_login_async(struct iscsi_context *iscsi, iscsi_command_cb cb, void *private_data);
  139. /*
  140. * Asynchronous call to perform an ISCSI logout.
  141. *
  142. * Returns:
  143. * 0 if the call was initiated and a logout will be attempted. Result of the logout will be reported
  144. * through the callback function.
  145. * <0 if there was an error. The callback function will not be invoked.
  146. *
  147. * Callback parameters :
  148. * status can be either of :
  149. * ISCSI_STATUS_GOOD : logout was successful. Command_data is always NULL.
  150. * ISCSI_STATUS_CANCELLED: logout was aborted. Command_data is NULL.
  151. */
  152. int iscsi_logout_async(struct iscsi_context *iscsi, iscsi_command_cb cb, void *private_data);
  153. /*
  154. * Asynchronous call to perform an ISCSI discovery.
  155. *
  156. * discoveries can only be done on connected and logged in discovery sessions.
  157. *
  158. * Returns:
  159. * 0 if the call was initiated and a discovery will be attempted. Result of the logout will be reported
  160. * through the callback function.
  161. * <0 if there was an error. The callback function will not be invoked.
  162. *
  163. * Callback parameters :
  164. * status can be either of :
  165. * ISCSI_STATUS_GOOD : Discovery was successful. Command_data is a pointer to a
  166. * iscsi_discovery_address list of structures.
  167. * This list of structures is only valid for the duration of the
  168. * callback and all data will be freed once the callback returns.
  169. * ISCSI_STATUS_CANCELLED: Discovery was aborted. Command_data is NULL.
  170. */
  171. int iscsi_discovery_async(struct iscsi_context *iscsi, iscsi_command_cb cb, void *private_data);
  172. struct iscsi_discovery_address {
  173. struct iscsi_discovery_address *next;
  174. const char *target_name;
  175. const char *target_address;
  176. };
  177. /*
  178. * Asynchronous call to perform an ISCSI NOP-OUT call
  179. *
  180. * Returns:
  181. * 0 if the call was initiated and a nop-out will be attempted. Result will be reported
  182. * through the callback function.
  183. * <0 if there was an error. The callback function will not be invoked.
  184. *
  185. * Callback parameters :
  186. * status can be either of :
  187. * ISCSI_STATUS_GOOD : NOP-OUT was successful and the server responded with a NOP-IN
  188. * callback_data is a iscsi_data structure containing the data returned from
  189. * the server.
  190. * ISCSI_STATUS_CANCELLED: Discovery was aborted. Command_data is NULL.
  191. */
  192. int iscsi_nop_out_async(struct iscsi_context *iscsi, iscsi_command_cb cb, unsigned char *data, int len, void *private_data);
  193. /* These are the possible status values for the callbacks for scsi commands.
  194. * The content of command_data depends on the status type.
  195. *
  196. * status :
  197. * ISCSI_STATUS_GOOD the scsi command completed successfullt on the target.
  198. * If this scsi command returns DATA-IN, that data is stored in an scsi_task structure
  199. * returned in the command_data parameter. This buffer will be automatically freed once the callback
  200. * returns.
  201. *
  202. * ISCSI_STATUS_CHECK_CONDITION the scsi command failed with a scsi sense.
  203. * Command_data contains a struct scsi_task. When the callback returns, this buffer
  204. * will automatically become freed.
  205. *
  206. * ISCSI_STATUS_CANCELLED the scsi command was aborted. Command_data is NULL.
  207. *
  208. * ISCSI_STATUS_ERROR the command failed. Command_data is NULL.
  209. */
  210. struct iscsi_data {
  211. int size;
  212. unsigned char *data;
  213. };
  214. /*
  215. * Async commands for SCSI
  216. */
  217. int iscsi_reportluns_async(struct iscsi_context *iscsi, iscsi_command_cb cb, int report_type, int alloc_len, void *private_data);
  218. int iscsi_testunitready_async(struct iscsi_context *iscsi, int lun, iscsi_command_cb cb, void *private_data);
  219. int iscsi_inquiry_async(struct iscsi_context *iscsi, int lun, iscsi_command_cb cb, int evpd, int page_code, int maxsize, void *private_data);
  220. int iscsi_readcapacity10_async(struct iscsi_context *iscsi, int lun, iscsi_command_cb cb, int lba, int pmi, void *private_data);
  221. int iscsi_read10_async(struct iscsi_context *iscsi, int lun, iscsi_command_cb cb, int lba, int datalen, int blocksize, void *private_data);
  222. int iscsi_write10_async(struct iscsi_context *iscsi, int lun, iscsi_command_cb cb, unsigned char *data, int datalen, int lba, int fua, int fuanv, int blocksize, void *private_data);
  223. int iscsi_modesense6_async(struct iscsi_context *iscsi, int lun, iscsi_command_cb cb, int dbd, int pc, int page_code, int sub_page_code, unsigned char alloc_len, void *private_data);
  224. #endif /* CCAN_ISCSI_H */