You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

iscsi.h 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. #ifndef _GPXE_ISCSI_H
  2. #define _GPXE_ISCSI_H
  3. /** @file
  4. *
  5. * iSCSI protocol
  6. *
  7. */
  8. #include <stdint.h>
  9. #include <gpxe/tcp.h>
  10. #include <gpxe/scsi.h>
  11. /**
  12. * iSCSI segment lengths
  13. *
  14. * iSCSI uses an icky structure with one one-byte field (a dword
  15. * count) and one three-byte field (a byte count). This structure,
  16. * and the accompanying macros, relieve some of the pain.
  17. */
  18. union iscsi_segment_lengths {
  19. struct {
  20. /** The AHS length (measured in dwords) */
  21. uint8_t ahs_len;
  22. /** The data length (measured in bytes), in network
  23. * byte order
  24. */
  25. uint8_t data_len[3];
  26. } bytes;
  27. /** Ths data length (measured in bytes), in network byte
  28. * order, with ahs_len as the first byte.
  29. */
  30. uint32_t ahs_and_data_len;
  31. };
  32. /** The length of the additional header segment, in dwords */
  33. #define ISCSI_AHS_LEN( segment_lengths ) \
  34. ( (segment_lengths).bytes.ahs_len )
  35. /** The length of the data segment, in bytes, excluding any padding */
  36. #define ISCSI_DATA_LEN( segment_lengths ) \
  37. ( ntohl ( (segment_lengths).ahs_and_data_len ) & 0xffffff )
  38. /** The padding of the data segment, in bytes */
  39. #define ISCSI_DATA_PAD_LEN( segment_lengths ) \
  40. ( ( 0 - (segment_lengths).bytes.data_len[2] ) & 0x03 )
  41. /** Set additional header and data segment lengths */
  42. #define ISCSI_SET_LENGTHS( segment_lengths, ahs_len, data_len ) do { \
  43. (segment_lengths).ahs_and_data_len = \
  44. htonl ( data_len | ( ahs_len << 24 ) ); \
  45. } while ( 0 )
  46. /**
  47. * iSCSI basic header segment common fields
  48. *
  49. */
  50. struct iscsi_bhs_common {
  51. /** Opcode */
  52. uint8_t opcode;
  53. /** Flags */
  54. uint8_t flags;
  55. /** Fields specific to the PDU type */
  56. uint8_t other_a[2];
  57. /** Segment lengths */
  58. union iscsi_segment_lengths lengths;
  59. /** Fields specific to the PDU type */
  60. uint8_t other_b[8];
  61. /** Initiator Task Tag */
  62. uint32_t itt;
  63. /** Fields specific to the PDU type */
  64. uint8_t other_c[28];
  65. };
  66. /** Opcode mask */
  67. #define ISCSI_OPCODE_MASK 0x3f
  68. /** Immediate delivery */
  69. #define ISCSI_FLAG_IMMEDIATE 0x40
  70. /** Final PDU of a sequence */
  71. #define ISCSI_FLAG_FINAL 0x80
  72. /**
  73. * iSCSI basic header segment common request fields
  74. *
  75. */
  76. struct iscsi_bhs_common_request {
  77. /** Opcode */
  78. uint8_t opcode;
  79. /** Flags */
  80. uint8_t flags;
  81. /** Fields specific to the PDU type */
  82. uint8_t other_a[2];
  83. /** Segment lengths */
  84. union iscsi_segment_lengths lengths;
  85. /** Fields specific to the PDU type */
  86. uint8_t other_b[8];
  87. /** Initiator Task Tag */
  88. uint32_t itt;
  89. /** Fields specific to the PDU type */
  90. uint8_t other_c[4];
  91. /** Command sequence number */
  92. uint32_t cmdsn;
  93. /** Expected status sequence number */
  94. uint32_t expstatsn;
  95. /** Fields specific to the PDU type */
  96. uint8_t other_d[16];
  97. };
  98. /**
  99. * iSCSI basic header segment common request fields
  100. *
  101. */
  102. struct iscsi_bhs_common_response {
  103. /** Opcode */
  104. uint8_t opcode;
  105. /** Flags */
  106. uint8_t flags;
  107. /** Fields specific to the PDU type */
  108. uint8_t other_a[2];
  109. /** Segment lengths */
  110. union iscsi_segment_lengths lengths;
  111. /** Fields specific to the PDU type */
  112. uint8_t other_b[8];
  113. /** Initiator Task Tag */
  114. uint32_t itt;
  115. /** Fields specific to the PDU type */
  116. uint8_t other_c[4];
  117. /** Status sequence number */
  118. uint32_t statsn;
  119. /** Expected command sequence number */
  120. uint32_t expcmdsn;
  121. /** Fields specific to the PDU type */
  122. uint8_t other_d[16];
  123. };
  124. /**
  125. * iSCSI login request basic header segment
  126. *
  127. */
  128. struct iscsi_bhs_login_request {
  129. /** Opcode */
  130. uint8_t opcode;
  131. /** Flags */
  132. uint8_t flags;
  133. /** Maximum supported version number */
  134. uint8_t version_max;
  135. /** Minimum supported version number */
  136. uint8_t version_min;
  137. /** Segment lengths */
  138. union iscsi_segment_lengths lengths;
  139. /** Initiator session ID (IANA format) enterprise number and flags */
  140. uint32_t isid_iana_en;
  141. /** Initiator session ID (IANA format) qualifier */
  142. uint16_t isid_iana_qual;
  143. /** Target session identifying handle */
  144. uint16_t tsih;
  145. /** Initiator Task Tag */
  146. uint32_t itt;
  147. /** Connection ID */
  148. uint16_t cid;
  149. /** Reserved */
  150. uint16_t reserved_a;
  151. /** Command sequence number */
  152. uint32_t cmdsn;
  153. /** Expected status sequence number */
  154. uint32_t expstatsn;
  155. /** Reserved */
  156. uint8_t reserved_b[16];
  157. };
  158. /** Login request opcode */
  159. #define ISCSI_OPCODE_LOGIN_REQUEST 0x03
  160. /** Willingness to transition to next stage */
  161. #define ISCSI_LOGIN_FLAG_TRANSITION 0x80
  162. /** Key=value pairs continued in subsequent request */
  163. #define ISCSI_LOGIN_FLAG_CONTINUE 0x40
  164. /* Current stage values and mask */
  165. #define ISCSI_LOGIN_CSG_MASK 0x0c
  166. #define ISCSI_LOGIN_CSG_SECURITY_NEGOTIATION 0x00
  167. #define ISCSI_LOGIN_CSG_OPERATIONAL_NEGOTIATION 0x04
  168. #define ISCSI_LOGIN_CSG_FULL_FEATURE_PHASE 0x0c
  169. /* Next stage values and mask */
  170. #define ISCSI_LOGIN_NSG_MASK 0x03
  171. #define ISCSI_LOGIN_NSG_SECURITY_NEGOTIATION 0x00
  172. #define ISCSI_LOGIN_NSG_OPERATIONAL_NEGOTIATION 0x01
  173. #define ISCSI_LOGIN_NSG_FULL_FEATURE_PHASE 0x03
  174. /** ISID IANA format marker */
  175. #define ISCSI_ISID_IANA 0x40000000
  176. /** Fen Systems Ltd. IANA enterprise number
  177. *
  178. * Permission is hereby granted to use Fen Systems Ltd.'s IANA
  179. * enterprise number with this iSCSI implementation.
  180. */
  181. #define IANA_EN_FEN_SYSTEMS 10019
  182. /**
  183. * iSCSI login response basic header segment
  184. *
  185. */
  186. struct iscsi_bhs_login_response {
  187. /** Opcode */
  188. uint8_t opcode;
  189. /** Flags */
  190. uint8_t flags;
  191. /** Maximum supported version number */
  192. uint8_t version_max;
  193. /** Minimum supported version number */
  194. uint8_t version_min;
  195. /** Segment lengths */
  196. union iscsi_segment_lengths lengths;
  197. /** Initiator session ID (IANA format) enterprise number and flags */
  198. uint32_t isid_iana_en;
  199. /** Initiator session ID (IANA format) qualifier */
  200. uint16_t isid_iana_qual;
  201. /** Target session identifying handle */
  202. uint16_t tsih;
  203. /** Initiator Task Tag */
  204. uint32_t itt;
  205. /** Reserved */
  206. uint32_t reserved_a;
  207. /** Status sequence number */
  208. uint32_t statsn;
  209. /** Expected command sequence number */
  210. uint32_t expcmdsn;
  211. /** Maximum command sequence number */
  212. uint32_t maxcmdsn;
  213. /** Status class */
  214. uint8_t status_class;
  215. /** Status detail */
  216. uint8_t status_detail;
  217. /** Reserved */
  218. uint8_t reserved_b[10];
  219. };
  220. /** Login response opcode */
  221. #define ISCSI_OPCODE_LOGIN_RESPONSE 0x23
  222. /**
  223. * iSCSI SCSI command basic header segment
  224. *
  225. */
  226. struct iscsi_bhs_scsi_command {
  227. /** Opcode */
  228. uint8_t opcode;
  229. /** Flags */
  230. uint8_t flags;
  231. /** Reserved */
  232. uint16_t reserved_a;
  233. /** Segment lengths */
  234. union iscsi_segment_lengths lengths;
  235. /** SCSI Logical Unit Number */
  236. uint64_t lun;
  237. /** Initiator Task Tag */
  238. uint32_t itt;
  239. /** Expected data transfer length */
  240. uint32_t exp_len;
  241. /** Command sequence number */
  242. uint32_t cmdsn;
  243. /** Expected status sequence number */
  244. uint32_t expstatsn;
  245. /** SCSI Command Descriptor Block (CDB) */
  246. union scsi_cdb cdb;
  247. };
  248. /** SCSI command opcode */
  249. #define ISCSI_OPCODE_SCSI_COMMAND 0x01
  250. /** Command will read data */
  251. #define ISCSI_COMMAND_FLAG_READ 0x40
  252. /** Command will write data */
  253. #define ISCSI_COMMAND_FLAG_WRITE 0x20
  254. /* Task attributes */
  255. #define ISCSI_COMMAND_ATTR_UNTAGGED 0x00
  256. #define ISCSI_COMMAND_ATTR_SIMPLE 0x01
  257. #define ISCSI_COMMAND_ATTR_ORDERED 0x02
  258. #define ISCSI_COMMAND_ATTR_HEAD_OF_QUEUE 0x03
  259. #define ISCSI_COMMAND_ATTR_ACA 0x04
  260. /**
  261. * iSCSI SCSI response basic header segment
  262. *
  263. */
  264. struct iscsi_bhs_scsi_response {
  265. /** Opcode */
  266. uint8_t opcode;
  267. /** Flags */
  268. uint8_t flags;
  269. /** Response code */
  270. uint8_t response;
  271. /** SCSI status code */
  272. uint8_t status;
  273. /** Segment lengths */
  274. union iscsi_segment_lengths lengths;
  275. /** Reserved */
  276. uint8_t reserved_a[8];
  277. /** Initiator Task Tag */
  278. uint32_t itt;
  279. /** SNACK tag */
  280. uint32_t snack;
  281. /** Status sequence number */
  282. uint32_t statsn;
  283. /** Expected command sequence number */
  284. uint32_t expcmdsn;
  285. /** Maximum command sequence number */
  286. uint32_t maxcmdsn;
  287. /** Expected data sequence number */
  288. uint32_t expdatasn;
  289. /** Reserved */
  290. uint8_t reserved_b[8];
  291. };
  292. /** SCSI response opcode */
  293. #define ISCSI_OPCODE_SCSI_RESPONSE 0x21
  294. /** SCSI command completed at target */
  295. #define ISCSI_RESPONSE_COMMAND_COMPLETE 0x00
  296. /** SCSI target failure */
  297. #define ISCSI_RESPONSE_TARGET_FAILURE 0x01
  298. /**
  299. * iSCSI data in basic header segment
  300. *
  301. */
  302. struct iscsi_bhs_data_in {
  303. /** Opcode */
  304. uint8_t opcode;
  305. /** Flags */
  306. uint8_t flags;
  307. /** Reserved */
  308. uint8_t reserved_a;
  309. /** SCSI status code */
  310. uint8_t status;
  311. /** Segment lengths */
  312. union iscsi_segment_lengths lengths;
  313. /** Logical Unit Number */
  314. uint64_t lun;
  315. /** Initiator Task Tag */
  316. uint32_t itt;
  317. /** Target Transfer Tag */
  318. uint32_t ttt;
  319. /** Status sequence number */
  320. uint32_t statsn;
  321. /** Expected command sequence number */
  322. uint32_t expcmdsn;
  323. /** Maximum command sequence number */
  324. uint32_t maxcmdsn;
  325. /** Data sequence number */
  326. uint32_t datasn;
  327. /** Buffer offset */
  328. uint32_t offset;
  329. /** Residual count */
  330. uint32_t residual_count;
  331. };
  332. /** Data in opcode */
  333. #define ISCSI_OPCODE_DATA_IN 0x25
  334. /** Data requires acknowledgement */
  335. #define ISCSI_DATA_FLAG_ACKNOWLEDGE 0x40
  336. /** Data overflow occurred */
  337. #define ISCSI_DATA_FLAG_OVERFLOW 0x04
  338. /** Data underflow occurred */
  339. #define ISCSI_DATA_FLAG_UNDERFLOW 0x02
  340. /** SCSI status code and verflow/underflow flags are valid */
  341. #define ISCSI_DATA_FLAG_STATUS 0x01
  342. /**
  343. * An iSCSI basic header segment
  344. */
  345. union iscsi_bhs {
  346. struct iscsi_bhs_common common;
  347. struct iscsi_bhs_common_request common_request;
  348. struct iscsi_bhs_common_response common_response;
  349. struct iscsi_bhs_login_request login_request;
  350. struct iscsi_bhs_login_response login_response;
  351. struct iscsi_bhs_scsi_command scsi_command;
  352. struct iscsi_bhs_scsi_response scsi_response;
  353. struct iscsi_bhs_data_in data_in;
  354. unsigned char bytes[ sizeof ( struct iscsi_bhs_common ) ];
  355. };
  356. /** State */
  357. enum iscsi_state {
  358. ISCSI_STATE_IDLE,
  359. ISCSI_STATE_LOGIN,
  360. ISCSI_STATE_LOGIN_CONT,
  361. ISCSI_STATE_COMMAND,
  362. };
  363. /** State of an iSCSI TX engine */
  364. enum iscsi_tx_state {
  365. /** Nothing to send */
  366. ISCSI_TX_IDLE = 0,
  367. /** Sending the basic header segment */
  368. ISCSI_TX_BHS,
  369. /** Sending the additional header segment */
  370. ISCSI_TX_AHS,
  371. /** Sending the data segment */
  372. ISCSI_TX_DATA,
  373. /** Sending the data segment padding */
  374. ISCSI_TX_DATA_PADDING,
  375. };
  376. /** State of an iSCSI RX engine */
  377. enum iscsi_rx_state {
  378. /** Receiving the basic header segment */
  379. ISCSI_RX_BHS = 0,
  380. /** Receiving the additional header segment */
  381. ISCSI_RX_AHS,
  382. /** Receiving the data segment */
  383. ISCSI_RX_DATA,
  384. /** Receiving the data segment padding */
  385. ISCSI_RX_DATA_PADDING,
  386. };
  387. /** An iSCSI session */
  388. struct iscsi_session {
  389. /** TCP connection for this session */
  390. struct tcp_connection tcp;
  391. /** Session status
  392. *
  393. * This is the bitwise-OR of zero or more ISCSI_STATUS_XXX
  394. * constants.
  395. */
  396. int status;
  397. /** Retry count
  398. *
  399. * Number of times that the connection has been retried.
  400. * Reset upon a successful connection.
  401. */
  402. int retry_count;
  403. /** Initiator IQN */
  404. const char *initiator;
  405. /** Target IQN */
  406. const char *target;
  407. /** Logical Unit Number (LUN) */
  408. uint64_t lun;
  409. /** Target session identifying handle
  410. *
  411. * This is assigned by the target when we first log in, and
  412. * must be reused on subsequent login attempts.
  413. */
  414. uint16_t tsih;
  415. /** Initiator task tag
  416. *
  417. * This is the tag of the current command. It is incremented
  418. * whenever a final response PDU is received.
  419. */
  420. uint32_t itt;
  421. /** Command sequence number
  422. *
  423. * This is the sequence number of the current command, used to
  424. * fill out the CmdSN field in iSCSI request PDUs. It is
  425. * updated with the value of the ExpCmdSN field whenever we
  426. * receive an iSCSI response PDU containing such a field.
  427. */
  428. uint32_t cmdsn;
  429. /** Status sequence number
  430. *
  431. * This is the most recent status sequence number present in
  432. * the StatSN field of an iSCSI response PDU containing such a
  433. * field. Whenever we send an iSCSI request PDU, we fill out
  434. * the ExpStatSN field with this value plus one.
  435. */
  436. uint32_t statsn;
  437. /** Basic header segment for current TX PDU */
  438. union iscsi_bhs tx_bhs;
  439. /** State of the TX engine */
  440. enum iscsi_tx_state tx_state;
  441. /** Byte offset within the current TX state */
  442. size_t tx_offset;
  443. /** Basic header segment for current RX PDU */
  444. union iscsi_bhs rx_bhs;
  445. /** State of the RX engine */
  446. enum iscsi_rx_state rx_state;
  447. /** Byte offset within the current RX state */
  448. size_t rx_offset;
  449. /** Current SCSI command
  450. *
  451. * Set to NULL when command is complete.
  452. */
  453. struct scsi_command *command;
  454. };
  455. /** Session is currently connected */
  456. #define ISCSI_STATUS_CONNECTED 0x01
  457. /** Session has completed */
  458. #define ISCSI_STATUS_DONE 0x02
  459. /** Session failed */
  460. #define ISCSI_STATUS_ERR 0x04
  461. /** Maximum number of retries at connecting */
  462. #define ISCSI_MAX_RETRIES 2
  463. extern int iscsi_issue ( struct iscsi_session *iscsi,
  464. struct scsi_command *command );
  465. /** An iSCSI device */
  466. struct iscsi_device {
  467. /** SCSI device interface */
  468. struct scsi_device scsi;
  469. /** iSCSI protocol instance */
  470. struct iscsi_session iscsi;
  471. };
  472. extern int init_iscsidev ( struct iscsi_device *iscsidev );
  473. #endif /* _GPXE_ISCSI_H */