您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

iscsi.h 16KB

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