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 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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/scsi.h>
  10. #include <gpxe/chap.h>
  11. #include <gpxe/refcnt.h>
  12. #include <gpxe/xfer.h>
  13. #include <gpxe/process.h>
  14. /** Default iSCSI port */
  15. #define ISCSI_PORT 3260
  16. /**
  17. * iSCSI segment lengths
  18. *
  19. * iSCSI uses an icky structure with one one-byte field (a dword
  20. * count) and one three-byte field (a byte count). This structure,
  21. * and the accompanying macros, relieve some of the pain.
  22. */
  23. union iscsi_segment_lengths {
  24. struct {
  25. /** The AHS length (measured in dwords) */
  26. uint8_t ahs_len;
  27. /** The data length (measured in bytes), in network
  28. * byte order
  29. */
  30. uint8_t data_len[3];
  31. } bytes;
  32. /** Ths data length (measured in bytes), in network byte
  33. * order, with ahs_len as the first byte.
  34. */
  35. uint32_t ahs_and_data_len;
  36. };
  37. /** The length of the additional header segment, in dwords */
  38. #define ISCSI_AHS_LEN( segment_lengths ) \
  39. ( (segment_lengths).bytes.ahs_len )
  40. /** The length of the data segment, in bytes, excluding any padding */
  41. #define ISCSI_DATA_LEN( segment_lengths ) \
  42. ( ntohl ( (segment_lengths).ahs_and_data_len ) & 0xffffff )
  43. /** The padding of the data segment, in bytes */
  44. #define ISCSI_DATA_PAD_LEN( segment_lengths ) \
  45. ( ( 0 - (segment_lengths).bytes.data_len[2] ) & 0x03 )
  46. /** Set additional header and data segment lengths */
  47. #define ISCSI_SET_LENGTHS( segment_lengths, ahs_len, data_len ) do { \
  48. (segment_lengths).ahs_and_data_len = \
  49. htonl ( data_len | ( ahs_len << 24 ) ); \
  50. } while ( 0 )
  51. /**
  52. * iSCSI basic header segment common fields
  53. *
  54. */
  55. struct iscsi_bhs_common {
  56. /** Opcode */
  57. uint8_t opcode;
  58. /** Flags */
  59. uint8_t flags;
  60. /** Fields specific to the PDU type */
  61. uint8_t other_a[2];
  62. /** Segment lengths */
  63. union iscsi_segment_lengths lengths;
  64. /** Fields specific to the PDU type */
  65. uint8_t other_b[8];
  66. /** Initiator Task Tag */
  67. uint32_t itt;
  68. /** Fields specific to the PDU type */
  69. uint8_t other_c[28];
  70. };
  71. /** Opcode mask */
  72. #define ISCSI_OPCODE_MASK 0x3f
  73. /** Immediate delivery */
  74. #define ISCSI_FLAG_IMMEDIATE 0x40
  75. /** Final PDU of a sequence */
  76. #define ISCSI_FLAG_FINAL 0x80
  77. /**
  78. * iSCSI basic header segment common request fields
  79. *
  80. */
  81. struct iscsi_bhs_common_response {
  82. /** Opcode */
  83. uint8_t opcode;
  84. /** Flags */
  85. uint8_t flags;
  86. /** Fields specific to the PDU type */
  87. uint8_t other_a[2];
  88. /** Segment lengths */
  89. union iscsi_segment_lengths lengths;
  90. /** Fields specific to the PDU type */
  91. uint8_t other_b[8];
  92. /** Initiator Task Tag */
  93. uint32_t itt;
  94. /** Fields specific to the PDU type */
  95. uint8_t other_c[4];
  96. /** Status sequence number */
  97. uint32_t statsn;
  98. /** Expected command sequence number */
  99. uint32_t expcmdsn;
  100. /** Fields specific to the PDU type */
  101. uint8_t other_d[16];
  102. };
  103. /**
  104. * iSCSI login request basic header segment
  105. *
  106. */
  107. struct iscsi_bhs_login_request {
  108. /** Opcode */
  109. uint8_t opcode;
  110. /** Flags */
  111. uint8_t flags;
  112. /** Maximum supported version number */
  113. uint8_t version_max;
  114. /** Minimum supported version number */
  115. uint8_t version_min;
  116. /** Segment lengths */
  117. union iscsi_segment_lengths lengths;
  118. /** Initiator session ID (IANA format) enterprise number and flags */
  119. uint32_t isid_iana_en;
  120. /** Initiator session ID (IANA format) qualifier */
  121. uint16_t isid_iana_qual;
  122. /** Target session identifying handle */
  123. uint16_t tsih;
  124. /** Initiator Task Tag */
  125. uint32_t itt;
  126. /** Connection ID */
  127. uint16_t cid;
  128. /** Reserved */
  129. uint16_t reserved_a;
  130. /** Command sequence number */
  131. uint32_t cmdsn;
  132. /** Expected status sequence number */
  133. uint32_t expstatsn;
  134. /** Reserved */
  135. uint8_t reserved_b[16];
  136. };
  137. /** Login request opcode */
  138. #define ISCSI_OPCODE_LOGIN_REQUEST 0x03
  139. /** Willingness to transition to next stage */
  140. #define ISCSI_LOGIN_FLAG_TRANSITION 0x80
  141. /** Key=value pairs continued in subsequent request */
  142. #define ISCSI_LOGIN_FLAG_CONTINUE 0x40
  143. /* Current stage values and mask */
  144. #define ISCSI_LOGIN_CSG_MASK 0x0c
  145. #define ISCSI_LOGIN_CSG_SECURITY_NEGOTIATION 0x00
  146. #define ISCSI_LOGIN_CSG_OPERATIONAL_NEGOTIATION 0x04
  147. #define ISCSI_LOGIN_CSG_FULL_FEATURE_PHASE 0x0c
  148. /* Next stage values and mask */
  149. #define ISCSI_LOGIN_NSG_MASK 0x03
  150. #define ISCSI_LOGIN_NSG_SECURITY_NEGOTIATION 0x00
  151. #define ISCSI_LOGIN_NSG_OPERATIONAL_NEGOTIATION 0x01
  152. #define ISCSI_LOGIN_NSG_FULL_FEATURE_PHASE 0x03
  153. /** ISID IANA format marker */
  154. #define ISCSI_ISID_IANA 0x40000000
  155. /** Fen Systems Ltd. IANA enterprise number
  156. *
  157. * Permission is hereby granted to use Fen Systems Ltd.'s IANA
  158. * enterprise number with this iSCSI implementation.
  159. */
  160. #define IANA_EN_FEN_SYSTEMS 10019
  161. /**
  162. * iSCSI login response basic header segment
  163. *
  164. */
  165. struct iscsi_bhs_login_response {
  166. /** Opcode */
  167. uint8_t opcode;
  168. /** Flags */
  169. uint8_t flags;
  170. /** Maximum supported version number */
  171. uint8_t version_max;
  172. /** Minimum supported version number */
  173. uint8_t version_min;
  174. /** Segment lengths */
  175. union iscsi_segment_lengths lengths;
  176. /** Initiator session ID (IANA format) enterprise number and flags */
  177. uint32_t isid_iana_en;
  178. /** Initiator session ID (IANA format) qualifier */
  179. uint16_t isid_iana_qual;
  180. /** Target session identifying handle */
  181. uint16_t tsih;
  182. /** Initiator Task Tag */
  183. uint32_t itt;
  184. /** Reserved */
  185. uint32_t reserved_a;
  186. /** Status sequence number */
  187. uint32_t statsn;
  188. /** Expected command sequence number */
  189. uint32_t expcmdsn;
  190. /** Maximum command sequence number */
  191. uint32_t maxcmdsn;
  192. /** Status class */
  193. uint8_t status_class;
  194. /** Status detail */
  195. uint8_t status_detail;
  196. /** Reserved */
  197. uint8_t reserved_b[10];
  198. };
  199. /** Login response opcode */
  200. #define ISCSI_OPCODE_LOGIN_RESPONSE 0x23
  201. /* Login response status codes */
  202. #define ISCSI_STATUS_SUCCESS 0x00
  203. #define ISCSI_STATUS_REDIRECT 0x01
  204. #define ISCSI_STATUS_INITIATOR_ERROR 0x02
  205. #define ISCSI_STATUS_TARGET_ERROR 0x03
  206. /**
  207. * iSCSI SCSI command basic header segment
  208. *
  209. */
  210. struct iscsi_bhs_scsi_command {
  211. /** Opcode */
  212. uint8_t opcode;
  213. /** Flags */
  214. uint8_t flags;
  215. /** Reserved */
  216. uint16_t reserved_a;
  217. /** Segment lengths */
  218. union iscsi_segment_lengths lengths;
  219. /** SCSI Logical Unit Number */
  220. uint64_t lun;
  221. /** Initiator Task Tag */
  222. uint32_t itt;
  223. /** Expected data transfer length */
  224. uint32_t exp_len;
  225. /** Command sequence number */
  226. uint32_t cmdsn;
  227. /** Expected status sequence number */
  228. uint32_t expstatsn;
  229. /** SCSI Command Descriptor Block (CDB) */
  230. union scsi_cdb cdb;
  231. };
  232. /** SCSI command opcode */
  233. #define ISCSI_OPCODE_SCSI_COMMAND 0x01
  234. /** Command will read data */
  235. #define ISCSI_COMMAND_FLAG_READ 0x40
  236. /** Command will write data */
  237. #define ISCSI_COMMAND_FLAG_WRITE 0x20
  238. /* Task attributes */
  239. #define ISCSI_COMMAND_ATTR_UNTAGGED 0x00
  240. #define ISCSI_COMMAND_ATTR_SIMPLE 0x01
  241. #define ISCSI_COMMAND_ATTR_ORDERED 0x02
  242. #define ISCSI_COMMAND_ATTR_HEAD_OF_QUEUE 0x03
  243. #define ISCSI_COMMAND_ATTR_ACA 0x04
  244. /**
  245. * iSCSI SCSI response basic header segment
  246. *
  247. */
  248. struct iscsi_bhs_scsi_response {
  249. /** Opcode */
  250. uint8_t opcode;
  251. /** Flags */
  252. uint8_t flags;
  253. /** Response code */
  254. uint8_t response;
  255. /** SCSI status code */
  256. uint8_t status;
  257. /** Segment lengths */
  258. union iscsi_segment_lengths lengths;
  259. /** Reserved */
  260. uint8_t reserved_a[8];
  261. /** Initiator Task Tag */
  262. uint32_t itt;
  263. /** SNACK tag */
  264. uint32_t snack;
  265. /** Status sequence number */
  266. uint32_t statsn;
  267. /** Expected command sequence number */
  268. uint32_t expcmdsn;
  269. /** Maximum command sequence number */
  270. uint32_t maxcmdsn;
  271. /** Expected data sequence number */
  272. uint32_t expdatasn;
  273. /** Reserved */
  274. uint8_t reserved_b[8];
  275. };
  276. /** SCSI response opcode */
  277. #define ISCSI_OPCODE_SCSI_RESPONSE 0x21
  278. /** SCSI command completed at target */
  279. #define ISCSI_RESPONSE_COMMAND_COMPLETE 0x00
  280. /** SCSI target failure */
  281. #define ISCSI_RESPONSE_TARGET_FAILURE 0x01
  282. /** SCSI sense response code offset
  283. *
  284. * The SCSI response may contain unsolicited sense data in the data
  285. * segment. If it does, this is the offset to the sense response code
  286. * byte, which is the only byte we care about.
  287. */
  288. #define ISCSI_SENSE_RESPONSE_CODE_OFFSET 2
  289. /**
  290. * iSCSI data-in basic header segment
  291. *
  292. */
  293. struct iscsi_bhs_data_in {
  294. /** Opcode */
  295. uint8_t opcode;
  296. /** Flags */
  297. uint8_t flags;
  298. /** Reserved */
  299. uint8_t reserved_a;
  300. /** SCSI status code */
  301. uint8_t status;
  302. /** Segment lengths */
  303. union iscsi_segment_lengths lengths;
  304. /** Logical Unit Number */
  305. uint64_t lun;
  306. /** Initiator Task Tag */
  307. uint32_t itt;
  308. /** Target Transfer Tag */
  309. uint32_t ttt;
  310. /** Status sequence number */
  311. uint32_t statsn;
  312. /** Expected command sequence number */
  313. uint32_t expcmdsn;
  314. /** Maximum command sequence number */
  315. uint32_t maxcmdsn;
  316. /** Data sequence number */
  317. uint32_t datasn;
  318. /** Buffer offset */
  319. uint32_t offset;
  320. /** Residual count */
  321. uint32_t residual_count;
  322. };
  323. /** Data-in opcode */
  324. #define ISCSI_OPCODE_DATA_IN 0x25
  325. /** Data requires acknowledgement */
  326. #define ISCSI_DATA_FLAG_ACKNOWLEDGE 0x40
  327. /** Data overflow occurred */
  328. #define ISCSI_DATA_FLAG_OVERFLOW 0x04
  329. /** Data underflow occurred */
  330. #define ISCSI_DATA_FLAG_UNDERFLOW 0x02
  331. /** SCSI status code and overflow/underflow flags are valid */
  332. #define ISCSI_DATA_FLAG_STATUS 0x01
  333. /**
  334. * iSCSI data-out basic header segment
  335. *
  336. */
  337. struct iscsi_bhs_data_out {
  338. /** Opcode */
  339. uint8_t opcode;
  340. /** Flags */
  341. uint8_t flags;
  342. /** Reserved */
  343. uint16_t reserved_a;
  344. /** Segment lengths */
  345. union iscsi_segment_lengths lengths;
  346. /** Logical Unit Number */
  347. uint64_t lun;
  348. /** Initiator Task Tag */
  349. uint32_t itt;
  350. /** Target Transfer Tag */
  351. uint32_t ttt;
  352. /** Reserved */
  353. uint32_t reserved_b;
  354. /** Expected status sequence number */
  355. uint32_t expstatsn;
  356. /** Reserved */
  357. uint32_t reserved_c;
  358. /** Data sequence number */
  359. uint32_t datasn;
  360. /** Buffer offset */
  361. uint32_t offset;
  362. /** Reserved */
  363. uint32_t reserved_d;
  364. };
  365. /** Data-out opcode */
  366. #define ISCSI_OPCODE_DATA_OUT 0x05
  367. /**
  368. * iSCSI request to transfer basic header segment
  369. *
  370. */
  371. struct iscsi_bhs_r2t {
  372. /** Opcode */
  373. uint8_t opcode;
  374. /** Flags */
  375. uint8_t flags;
  376. /** Reserved */
  377. uint16_t reserved_a;
  378. /** Segment lengths */
  379. union iscsi_segment_lengths lengths;
  380. /** Logical Unit Number */
  381. uint64_t lun;
  382. /** Initiator Task Tag */
  383. uint32_t itt;
  384. /** Target Transfer Tag */
  385. uint32_t ttt;
  386. /** Status sequence number */
  387. uint32_t statsn;
  388. /** Expected command sequence number */
  389. uint32_t expcmdsn;
  390. /** Maximum command sequence number */
  391. uint32_t maxcmdsn;
  392. /** R2T sequence number */
  393. uint32_t r2tsn;
  394. /** Buffer offset */
  395. uint32_t offset;
  396. /** Desired data transfer length */
  397. uint32_t len;
  398. };
  399. /** R2T opcode */
  400. #define ISCSI_OPCODE_R2T 0x31
  401. /**
  402. * An iSCSI basic header segment
  403. */
  404. union iscsi_bhs {
  405. struct iscsi_bhs_common common;
  406. struct iscsi_bhs_common_response common_response;
  407. struct iscsi_bhs_login_request login_request;
  408. struct iscsi_bhs_login_response login_response;
  409. struct iscsi_bhs_scsi_command scsi_command;
  410. struct iscsi_bhs_scsi_response scsi_response;
  411. struct iscsi_bhs_data_in data_in;
  412. struct iscsi_bhs_data_out data_out;
  413. struct iscsi_bhs_r2t r2t;
  414. unsigned char bytes[ sizeof ( struct iscsi_bhs_common ) ];
  415. };
  416. /** State of an iSCSI TX engine */
  417. enum iscsi_tx_state {
  418. /** Nothing to send */
  419. ISCSI_TX_IDLE = 0,
  420. /** Sending the basic header segment */
  421. ISCSI_TX_BHS,
  422. /** Sending the additional header segment */
  423. ISCSI_TX_AHS,
  424. /** Sending the data segment */
  425. ISCSI_TX_DATA,
  426. /** Sending the data segment padding */
  427. ISCSI_TX_DATA_PADDING,
  428. };
  429. /** State of an iSCSI RX engine */
  430. enum iscsi_rx_state {
  431. /** Receiving the basic header segment */
  432. ISCSI_RX_BHS = 0,
  433. /** Receiving the additional header segment */
  434. ISCSI_RX_AHS,
  435. /** Receiving the data segment */
  436. ISCSI_RX_DATA,
  437. /** Receiving the data segment padding */
  438. ISCSI_RX_DATA_PADDING,
  439. };
  440. /** An iSCSI session */
  441. struct iscsi_session {
  442. /** Reference counter */
  443. struct refcnt refcnt;
  444. /** Transport-layer socket */
  445. struct xfer_interface socket;
  446. /** Target address */
  447. char *target_address;
  448. /** Target port */
  449. unsigned int target_port;
  450. /** Target IQN */
  451. char *target_iqn;
  452. /** Logical Unit Number (LUN) */
  453. uint64_t lun;
  454. /** Session status
  455. *
  456. * This is the bitwise-OR of zero or more ISCSI_STATUS_XXX
  457. * constants.
  458. */
  459. int status;
  460. /** Retry count
  461. *
  462. * Number of times that the connection has been retried.
  463. * Reset upon a successful connection.
  464. */
  465. int retry_count;
  466. /** CHAP challenge/response */
  467. struct chap_challenge chap;
  468. /** Target session identifying handle
  469. *
  470. * This is assigned by the target when we first log in, and
  471. * must be reused on subsequent login attempts.
  472. */
  473. uint16_t tsih;
  474. /** Initiator task tag
  475. *
  476. * This is the tag of the current command. It is incremented
  477. * whenever a new command is started.
  478. */
  479. uint32_t itt;
  480. /** Target transfer tag
  481. *
  482. * This is the tag attached to a sequence of data-out PDUs in
  483. * response to an R2T.
  484. */
  485. uint32_t ttt;
  486. /**
  487. * Transfer offset
  488. *
  489. * This is the offset for an in-progress sequence of data-out
  490. * PDUs in response to an R2T.
  491. */
  492. uint32_t transfer_offset;
  493. /**
  494. * Transfer length
  495. *
  496. * This is the length for an in-progress sequence of data-out
  497. * PDUs in response to an R2T.
  498. */
  499. uint32_t transfer_len;
  500. /** Command sequence number
  501. *
  502. * This is the sequence number of the current command, used to
  503. * fill out the CmdSN field in iSCSI request PDUs. It is
  504. * updated with the value of the ExpCmdSN field whenever we
  505. * receive an iSCSI response PDU containing such a field.
  506. */
  507. uint32_t cmdsn;
  508. /** Status sequence number
  509. *
  510. * This is the most recent status sequence number present in
  511. * the StatSN field of an iSCSI response PDU containing such a
  512. * field. Whenever we send an iSCSI request PDU, we fill out
  513. * the ExpStatSN field with this value plus one.
  514. */
  515. uint32_t statsn;
  516. /** Basic header segment for current TX PDU */
  517. union iscsi_bhs tx_bhs;
  518. /** State of the TX engine */
  519. enum iscsi_tx_state tx_state;
  520. /** TX process */
  521. struct process process;
  522. /** Basic header segment for current RX PDU */
  523. union iscsi_bhs rx_bhs;
  524. /** State of the RX engine */
  525. enum iscsi_rx_state rx_state;
  526. /** Byte offset within the current RX state */
  527. size_t rx_offset;
  528. /** Length of the current RX state */
  529. size_t rx_len;
  530. /** Buffer for received data (not always used) */
  531. void *rx_buffer;
  532. /** Current SCSI command
  533. *
  534. * Set to NULL when command is complete.
  535. */
  536. struct scsi_command *command;
  537. /** SCSI command return code
  538. *
  539. * Set to -EINPROGRESS while command is processing.
  540. */
  541. int rc;
  542. /** Instant return code
  543. *
  544. * Set to a non-zero value if all requests should return
  545. * immediately. This can be used to e.g. avoid retrying
  546. * logins that are doomed to fail authentication.
  547. */
  548. int instant_rc;
  549. };
  550. /** iSCSI session is currently in the security negotiation phase */
  551. #define ISCSI_STATUS_SECURITY_NEGOTIATION_PHASE \
  552. ( ISCSI_LOGIN_CSG_SECURITY_NEGOTIATION | \
  553. ISCSI_LOGIN_NSG_OPERATIONAL_NEGOTIATION )
  554. /** iSCSI session is currently in the operational parameter
  555. * negotiation phase
  556. */
  557. #define ISCSI_STATUS_OPERATIONAL_NEGOTIATION_PHASE \
  558. ( ISCSI_LOGIN_CSG_OPERATIONAL_NEGOTIATION | \
  559. ISCSI_LOGIN_NSG_FULL_FEATURE_PHASE )
  560. /** iSCSI session is currently in the full feature phase */
  561. #define ISCSI_STATUS_FULL_FEATURE_PHASE ISCSI_LOGIN_CSG_FULL_FEATURE_PHASE
  562. /** Mask for all iSCSI session phases */
  563. #define ISCSI_STATUS_PHASE_MASK ( ISCSI_LOGIN_CSG_MASK | ISCSI_LOGIN_NSG_MASK )
  564. /** iSCSI session needs to send the initial security negotiation strings */
  565. #define ISCSI_STATUS_STRINGS_SECURITY 0x0100
  566. /** iSCSI session needs to send the CHAP_A string */
  567. #define ISCSI_STATUS_STRINGS_CHAP_ALGORITHM 0x0200
  568. /** iSCSI session needs to send the CHAP response */
  569. #define ISCSI_STATUS_STRINGS_CHAP_RESPONSE 0x0400
  570. /** iSCSI session needs to send the operational negotiation strings */
  571. #define ISCSI_STATUS_STRINGS_OPERATIONAL 0x0800
  572. /** Mask for all iSCSI "needs to send" flags */
  573. #define ISCSI_STATUS_STRINGS_MASK 0xff00
  574. /** Maximum number of retries at connecting */
  575. #define ISCSI_MAX_RETRIES 2
  576. extern int iscsi_attach ( struct scsi_device *scsi, const char *root_path );
  577. extern void iscsi_detach ( struct scsi_device *scsi );
  578. #endif /* _GPXE_ISCSI_H */