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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <stddef.h>
  19. #include <string.h>
  20. #include <vsprintf.h>
  21. #include <errno.h>
  22. #include <assert.h>
  23. #include <byteswap.h>
  24. #include <gpxe/scsi.h>
  25. #include <gpxe/process.h>
  26. #include <gpxe/iscsi.h>
  27. /** @file
  28. *
  29. * iSCSI protocol
  30. *
  31. */
  32. static void iscsi_start_tx ( struct iscsi_session *iscsi );
  33. /****************************************************************************
  34. *
  35. * iSCSI SCSI command issuing
  36. *
  37. */
  38. /**
  39. * Build iSCSI SCSI command BHS
  40. *
  41. * @v iscsi iSCSI session
  42. */
  43. static void iscsi_start_command ( struct iscsi_session *iscsi ) {
  44. struct iscsi_bhs_scsi_command *command = &iscsi->tx_bhs.scsi_command;
  45. assert ( ! ( iscsi->command->data_in && iscsi->command->data_out ) );
  46. /* Construct BHS and initiate transmission */
  47. iscsi_start_tx ( iscsi );
  48. command->opcode = ISCSI_OPCODE_SCSI_COMMAND;
  49. command->flags = ( ISCSI_FLAG_FINAL |
  50. ISCSI_COMMAND_ATTR_SIMPLE );
  51. if ( iscsi->command->data_in )
  52. command->flags |= ISCSI_COMMAND_FLAG_READ;
  53. if ( iscsi->command->data_out )
  54. command->flags |= ISCSI_COMMAND_FLAG_WRITE;
  55. ISCSI_SET_LENGTHS ( command->lengths, 0, iscsi->command->data_out_len);
  56. command->lun = iscsi->lun;
  57. command->itt = htonl ( iscsi->itt );
  58. command->exp_len = htonl ( iscsi->command->data_in_len );
  59. memcpy ( &command->cdb, &iscsi->command->cdb, sizeof ( command->cdb ));
  60. }
  61. /**
  62. * Send iSCSI SCSI command data
  63. *
  64. * @v iscsi iSCSI session
  65. */
  66. static void iscsi_tx_command ( struct iscsi_session *iscsi ) {
  67. tcp_send ( &iscsi->tcp, iscsi->command->data_out + iscsi->tx_offset,
  68. iscsi->command->data_out_len - iscsi->tx_offset );
  69. }
  70. /**
  71. * Receive data segment of an iSCSI data-in PDU
  72. *
  73. * @v iscsi iSCSI session
  74. * @v data Received data
  75. * @v len Length of received data
  76. * @v remaining Data remaining after this data
  77. *
  78. */
  79. static void iscsi_rx_data_in ( struct iscsi_session *iscsi, void *data,
  80. size_t len, size_t remaining ) {
  81. struct iscsi_bhs_data_in *data_in = &iscsi->rx_bhs.data_in;
  82. unsigned long offset;
  83. /* Copy data to data-in buffer */
  84. offset = ntohl ( data_in->offset ) + iscsi->rx_offset;
  85. assert ( iscsi->command != NULL );
  86. assert ( iscsi->command->data_in != NULL );
  87. assert ( ( offset + len ) <= iscsi->command->data_in_len );
  88. memcpy ( ( iscsi->command->data_in + offset ), data, len );
  89. /* If this is the end, flag as complete */
  90. if ( ( data_in->flags & ISCSI_FLAG_FINAL ) && ( remaining == 0 ) )
  91. iscsi->status |= ISCSI_STATUS_DONE;
  92. }
  93. /****************************************************************************
  94. *
  95. * iSCSI login
  96. *
  97. */
  98. /**
  99. * Build iSCSI login request strings
  100. *
  101. * @v iscsi iSCSI session
  102. *
  103. * These are the initial set of strings sent in the first login
  104. * request PDU.
  105. */
  106. static int iscsi_build_login_request_strings ( struct iscsi_session *iscsi,
  107. void *data, size_t len ) {
  108. return snprintf ( data, len,
  109. "InitiatorName=%s%c"
  110. "TargetName=%s%c"
  111. "MaxRecvDataSegmentLength=512%c"
  112. "SessionType=Normal%c"
  113. "DataDigest=None%c"
  114. "HeaderDigest=None%c"
  115. "ErrorRecoveryLevel=0%c",
  116. iscsi->initiator, 0, iscsi->target, 0,
  117. 0, 0, 0, 0, 0 );
  118. }
  119. /**
  120. * Build iSCSI login request BHS
  121. *
  122. * @v iscsi iSCSI session
  123. * @v first Login request is the first request of a session
  124. */
  125. static void iscsi_start_login ( struct iscsi_session *iscsi, int first ) {
  126. struct iscsi_bhs_login_request *request = &iscsi->tx_bhs.login_request;
  127. int len;
  128. /* Construct BHS and initiate transmission */
  129. iscsi_start_tx ( iscsi );
  130. request->opcode = ( ISCSI_OPCODE_LOGIN_REQUEST |
  131. ISCSI_FLAG_IMMEDIATE );
  132. request->flags = ( ISCSI_LOGIN_FLAG_TRANSITION |
  133. ISCSI_LOGIN_CSG_OPERATIONAL_NEGOTIATION |
  134. ISCSI_LOGIN_NSG_FULL_FEATURE_PHASE );
  135. /* version_max and version_min left as zero */
  136. if ( first ) {
  137. len = iscsi_build_login_request_strings ( iscsi, NULL, 0 );
  138. ISCSI_SET_LENGTHS ( request->lengths, 0, len );
  139. }
  140. request->isid_iana_en = htonl ( ISCSI_ISID_IANA |
  141. IANA_EN_FEN_SYSTEMS );
  142. /* isid_iana_qual left as zero */
  143. request->tsih = htons ( iscsi->tsih );
  144. /* itt left as zero */
  145. /* cid left as zero */
  146. }
  147. /**
  148. * Transmit data segment of an iSCSI login request PDU
  149. *
  150. * @v iscsi iSCSI session
  151. *
  152. * For login requests, the data segment consists of the login strings.
  153. */
  154. static void iscsi_tx_login_request ( struct iscsi_session *iscsi ) {
  155. int len;
  156. len = iscsi_build_login_request_strings ( iscsi, tcp_buffer,
  157. tcp_buflen );
  158. tcp_send ( &iscsi->tcp, tcp_buffer + iscsi->tx_offset,
  159. len - iscsi->tx_offset );
  160. }
  161. /**
  162. * Receive data segment of an iSCSI login response PDU
  163. *
  164. * @v iscsi iSCSI session
  165. * @v data Received data
  166. * @v len Length of received data
  167. * @v remaining Data remaining after this data
  168. *
  169. */
  170. static void iscsi_rx_login_response ( struct iscsi_session *iscsi,
  171. void *data __unused,
  172. size_t len __unused,
  173. size_t remaining __unused ) {
  174. struct iscsi_bhs_login_response *response
  175. = &iscsi->rx_bhs.login_response;
  176. /* Check for fatal errors */
  177. if ( response->status_class != 0 ) {
  178. printf ( "iSCSI login failure: class %02x detail %02x\n",
  179. response->status_class, response->status_detail );
  180. iscsi->status |= ( ISCSI_STATUS_DONE | ISCSI_STATUS_ERR );
  181. tcp_close ( &iscsi->tcp );
  182. return;
  183. }
  184. /* If server did not transition, send back another login
  185. * request without any login strings.
  186. */
  187. if ( ! ( response->flags & ISCSI_LOGIN_FLAG_TRANSITION ) ) {
  188. iscsi_start_login ( iscsi, 0 );
  189. return;
  190. }
  191. /* Record TSIH for future reference */
  192. iscsi->tsih = ntohl ( response->tsih );
  193. /* Send the SCSI command */
  194. iscsi_start_command ( iscsi );
  195. }
  196. /****************************************************************************
  197. *
  198. * iSCSI to TCP interface
  199. *
  200. */
  201. static inline struct iscsi_session *
  202. tcp_to_iscsi ( struct tcp_connection *conn ) {
  203. return container_of ( conn, struct iscsi_session, tcp );
  204. }
  205. /**
  206. * Start up a new TX PDU
  207. *
  208. * @v iscsi iSCSI session
  209. *
  210. * This initiates the process of sending a new PDU. Only one PDU may
  211. * be in transit at any one time.
  212. */
  213. static void iscsi_start_tx ( struct iscsi_session *iscsi ) {
  214. assert ( iscsi->tx_state == ISCSI_TX_IDLE );
  215. /* Initialise TX BHS */
  216. memset ( &iscsi->tx_bhs, 0, sizeof ( iscsi->tx_bhs ) );
  217. iscsi->tx_bhs.common_request.cmdsn = htonl ( iscsi->cmdsn );
  218. iscsi->tx_bhs.common_request.expstatsn = htonl ( iscsi->statsn + 1 );
  219. /* Flag TX engine to start transmitting */
  220. iscsi->tx_state = ISCSI_TX_BHS;
  221. iscsi->tx_offset = 0;
  222. }
  223. /**
  224. * Transmit data segment of an iSCSI PDU
  225. *
  226. * @v iscsi iSCSI session
  227. *
  228. * Handle transmission of part of a PDU data segment. iscsi::tx_bhs
  229. * will be valid when this is called.
  230. */
  231. static void iscsi_tx_data ( struct iscsi_session *iscsi ) {
  232. struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
  233. switch ( common->opcode & ISCSI_OPCODE_MASK ) {
  234. case ISCSI_OPCODE_SCSI_COMMAND:
  235. iscsi_tx_command ( iscsi );
  236. break;
  237. case ISCSI_OPCODE_LOGIN_REQUEST:
  238. iscsi_tx_login_request ( iscsi );
  239. break;
  240. default:
  241. assert ( 0 );
  242. break;
  243. }
  244. }
  245. /**
  246. * Handle TCP ACKs
  247. *
  248. * @v iscsi iSCSI session
  249. *
  250. * Updates iscsi->tx_offset and, if applicable, transitions to the
  251. * next TX state.
  252. */
  253. static void iscsi_acked ( struct tcp_connection *conn, size_t len ) {
  254. struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
  255. struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
  256. size_t max_tx_offset;
  257. enum iscsi_tx_state next_state;
  258. iscsi->tx_offset += len;
  259. while ( 1 ) {
  260. switch ( iscsi->tx_state ) {
  261. case ISCSI_TX_BHS:
  262. max_tx_offset = sizeof ( iscsi->tx_bhs );
  263. next_state = ISCSI_TX_AHS;
  264. break;
  265. case ISCSI_TX_AHS:
  266. max_tx_offset = 4 * ISCSI_AHS_LEN ( common->lengths );
  267. next_state = ISCSI_TX_DATA;
  268. break;
  269. case ISCSI_TX_DATA:
  270. max_tx_offset = ISCSI_DATA_LEN ( common->lengths );
  271. next_state = ISCSI_TX_DATA_PADDING;
  272. break;
  273. case ISCSI_TX_DATA_PADDING:
  274. max_tx_offset = ISCSI_DATA_PAD_LEN ( common->lengths );
  275. next_state = ISCSI_TX_IDLE;
  276. break;
  277. case ISCSI_TX_IDLE:
  278. return;
  279. default:
  280. assert ( 0 );
  281. return;
  282. }
  283. assert ( iscsi->tx_offset <= max_tx_offset );
  284. /* If the whole of the current portion has not yet
  285. * been acked, stay in this state for now.
  286. */
  287. if ( iscsi->tx_offset != max_tx_offset )
  288. return;
  289. iscsi->tx_state = next_state;
  290. iscsi->tx_offset = 0;
  291. }
  292. }
  293. /**
  294. * Transmit iSCSI PDU
  295. *
  296. * @v iscsi iSCSI session
  297. *
  298. * Constructs data to be sent for the current TX state
  299. */
  300. static void iscsi_senddata ( struct tcp_connection *conn ) {
  301. struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
  302. struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
  303. static const char pad[] = { '\0', '\0', '\0' };
  304. switch ( iscsi->tx_state ) {
  305. case ISCSI_TX_IDLE:
  306. /* Nothing to send */
  307. break;
  308. case ISCSI_TX_BHS:
  309. tcp_send ( conn, &iscsi->tx_bhs.bytes[iscsi->tx_offset],
  310. ( sizeof ( iscsi->tx_bhs ) - iscsi->tx_offset ) );
  311. break;
  312. case ISCSI_TX_AHS:
  313. /* We don't yet have an AHS transmission mechanism */
  314. assert ( 0 );
  315. break;
  316. case ISCSI_TX_DATA:
  317. iscsi_tx_data ( iscsi );
  318. break;
  319. case ISCSI_TX_DATA_PADDING:
  320. tcp_send ( conn, pad, ( ISCSI_DATA_PAD_LEN ( common->lengths )
  321. - iscsi->tx_offset ) );
  322. break;
  323. default:
  324. assert ( 0 );
  325. break;
  326. }
  327. }
  328. /**
  329. * Receive data segment of an iSCSI PDU
  330. *
  331. * @v iscsi iSCSI session
  332. * @v data Received data
  333. * @v len Length of received data
  334. * @v remaining Data remaining after this data
  335. *
  336. * Handle processing of part of a PDU data segment. iscsi::rx_bhs
  337. * will be valid when this is called.
  338. */
  339. static void iscsi_rx_data ( struct iscsi_session *iscsi, void *data,
  340. size_t len, size_t remaining ) {
  341. struct iscsi_bhs_common_response *response
  342. = &iscsi->rx_bhs.common_response;
  343. /* Update cmdsn and statsn */
  344. iscsi->cmdsn = ntohl ( response->expcmdsn );
  345. iscsi->statsn = ntohl ( response->statsn );
  346. /* Increment itt when we receive a final response */
  347. if ( response->flags & ISCSI_FLAG_FINAL )
  348. iscsi->itt++;
  349. switch ( response->opcode & ISCSI_OPCODE_MASK ) {
  350. case ISCSI_OPCODE_LOGIN_RESPONSE:
  351. iscsi_rx_login_response ( iscsi, data, len, remaining );
  352. break;
  353. case ISCSI_OPCODE_DATA_IN:
  354. iscsi_rx_data_in ( iscsi, data, len, remaining );
  355. break;
  356. default:
  357. printf ( "Unknown iSCSI opcode %02x\n", response->opcode );
  358. iscsi->status |= ( ISCSI_STATUS_DONE | ISCSI_STATUS_ERR );
  359. break;
  360. }
  361. }
  362. /**
  363. * Discard portion of an iSCSI PDU.
  364. *
  365. * @v iscsi iSCSI session
  366. * @v data Received data
  367. * @v len Length of received data
  368. * @v remaining Data remaining after this data
  369. *
  370. * This discards data from a portion of a received PDU.
  371. */
  372. static void iscsi_rx_discard ( struct iscsi_session *iscsi __unused,
  373. void *data __unused, size_t len __unused,
  374. size_t remaining __unused ) {
  375. /* Do nothing */
  376. }
  377. /**
  378. * Receive basic header segment of an iSCSI PDU
  379. *
  380. * @v iscsi iSCSI session
  381. * @v data Received data
  382. * @v len Length of received data
  383. * @v remaining Data remaining after this data
  384. *
  385. * This fills in iscsi::rx_bhs with the data from the BHS portion of
  386. * the received PDU.
  387. */
  388. static void iscsi_rx_bhs ( struct iscsi_session *iscsi, void *data,
  389. size_t len, size_t remaining __unused ) {
  390. memcpy ( &iscsi->rx_bhs.bytes[iscsi->rx_offset], data, len );
  391. }
  392. /**
  393. * Receive new data
  394. *
  395. * @v tcp TCP connection
  396. * @v data Received data
  397. * @v len Length of received data
  398. *
  399. * This handles received PDUs. The receive strategy is to fill in
  400. * iscsi::rx_bhs with the contents of the BHS portion of the PDU,
  401. * throw away any AHS portion, and then process each part of the data
  402. * portion as it arrives. The data processing routine therefore
  403. * always has a full copy of the BHS available, even for portions of
  404. * the data in different packets to the BHS.
  405. */
  406. static void iscsi_newdata ( struct tcp_connection *conn, void *data,
  407. size_t len ) {
  408. struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
  409. struct iscsi_bhs_common *common = &iscsi->rx_bhs.common;
  410. void ( *process ) ( struct iscsi_session *iscsi, void *data,
  411. size_t len, size_t remaining );
  412. size_t max_rx_offset;
  413. enum iscsi_rx_state next_state;
  414. size_t frag_len;
  415. size_t remaining;
  416. while ( 1 ) {
  417. switch ( iscsi->rx_state ) {
  418. case ISCSI_RX_BHS:
  419. process = iscsi_rx_bhs;
  420. max_rx_offset = sizeof ( iscsi->rx_bhs );
  421. next_state = ISCSI_RX_AHS;
  422. break;
  423. case ISCSI_RX_AHS:
  424. process = iscsi_rx_discard;
  425. max_rx_offset = 4 * ISCSI_AHS_LEN ( common->lengths );
  426. next_state = ISCSI_RX_DATA;
  427. break;
  428. case ISCSI_RX_DATA:
  429. process = iscsi_rx_data;
  430. max_rx_offset = ISCSI_DATA_LEN ( common->lengths );
  431. next_state = ISCSI_RX_DATA_PADDING;
  432. break;
  433. case ISCSI_RX_DATA_PADDING:
  434. process = iscsi_rx_discard;
  435. max_rx_offset = ISCSI_DATA_PAD_LEN ( common->lengths );
  436. next_state = ISCSI_RX_BHS;
  437. break;
  438. default:
  439. assert ( 0 );
  440. return;
  441. }
  442. frag_len = max_rx_offset - iscsi->rx_offset;
  443. if ( frag_len > len )
  444. frag_len = len;
  445. remaining = max_rx_offset - iscsi->rx_offset - frag_len;
  446. process ( iscsi, data, frag_len, remaining );
  447. iscsi->rx_offset += frag_len;
  448. data += frag_len;
  449. len -= frag_len;
  450. /* If all the data for this state has not yet been
  451. * received, stay in this state for now.
  452. */
  453. if ( iscsi->rx_offset != max_rx_offset )
  454. return;
  455. iscsi->rx_state = next_state;
  456. iscsi->rx_offset = 0;
  457. }
  458. }
  459. /**
  460. * Handle TCP connection closure
  461. *
  462. * @v conn TCP connection
  463. * @v status Error code, if any
  464. *
  465. */
  466. static void iscsi_closed ( struct tcp_connection *conn, int status __unused ) {
  467. struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
  468. /* Clear connected flag */
  469. iscsi->status &= ~ISCSI_STATUS_CONNECTED;
  470. /* Retry connection if within the retry limit, otherwise fail */
  471. if ( ++iscsi->retry_count <= ISCSI_MAX_RETRIES ) {
  472. tcp_connect ( conn );
  473. } else {
  474. iscsi->status |= ( ISCSI_STATUS_DONE | ISCSI_STATUS_ERR );
  475. }
  476. }
  477. /**
  478. * Handle TCP connection opening
  479. *
  480. * @v conn TCP connection
  481. *
  482. */
  483. static void iscsi_connected ( struct tcp_connection *conn ) {
  484. struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
  485. /* Set connected flag and reset retry count */
  486. iscsi->status |= ISCSI_STATUS_CONNECTED;
  487. iscsi->retry_count = 0;
  488. /* Prepare to receive PDUs. */
  489. iscsi->rx_state = ISCSI_RX_BHS;
  490. iscsi->rx_offset = 0;
  491. /* Start logging in */
  492. iscsi_start_login ( iscsi, 1 );
  493. }
  494. /** iSCSI TCP operations */
  495. static struct tcp_operations iscsi_tcp_operations = {
  496. .closed = iscsi_closed,
  497. .connected = iscsi_connected,
  498. .acked = iscsi_acked,
  499. .newdata = iscsi_newdata,
  500. .senddata = iscsi_senddata,
  501. };
  502. /**
  503. * Issue SCSI command via iSCSI session
  504. *
  505. * @v iscsi iSCSI session
  506. * @v command SCSI command
  507. * @ret rc Return status code
  508. */
  509. int iscsi_issue ( struct iscsi_session *iscsi,
  510. struct scsi_command *command ) {
  511. iscsi->command = command;
  512. iscsi->status &= ~( ISCSI_STATUS_DONE | ISCSI_STATUS_ERR );
  513. if ( iscsi->status & ISCSI_STATUS_CONNECTED ) {
  514. iscsi_start_command ( iscsi );
  515. } else {
  516. iscsi->tcp.tcp_op = &iscsi_tcp_operations;
  517. tcp_connect ( &iscsi->tcp );
  518. }
  519. while ( ! ( iscsi->status & ISCSI_STATUS_DONE ) ) {
  520. step();
  521. }
  522. iscsi->command = NULL;
  523. return ( ( iscsi->status & ISCSI_STATUS_ERR ) ? -EIO : 0 );
  524. }