8 #include <gpxe/process.h>
9 #include <gpxe/iscsi.h>
17 static void iscsi_start_tx ( struct iscsi_session *iscsi );
19 /****************************************************************************
21 * iSCSI SCSI command issuing
26 * Build iSCSI SCSI command BHS
28 * @v iscsi iSCSI session
30 static void iscsi_start_command ( struct iscsi_session *iscsi ) {
31 struct iscsi_bhs_scsi_command *command = &iscsi->tx_bhs.scsi_command;
33 assert ( ! ( iscsi->command->data_in && iscsi->command->data_out ) );
35 /* Construct BHS and initiate transmission */
36 iscsi_start_tx ( iscsi );
37 command->opcode = ISCSI_OPCODE_SCSI_COMMAND;
38 command->flags = ( ISCSI_FLAG_FINAL |
39 ISCSI_COMMAND_ATTR_SIMPLE );
40 if ( iscsi->command->data_in )
41 command->flags |= ISCSI_COMMAND_FLAG_READ;
42 if ( iscsi->command->data_out )
43 command->flags |= ISCSI_COMMAND_FLAG_WRITE;
44 ISCSI_SET_LENGTHS ( command->lengths, 0, iscsi->command->data_out_len);
45 command->lun = iscsi->lun;
46 command->itt = htonl ( iscsi->itt );
47 command->exp_len = htonl ( iscsi->command->data_in_len );
48 memcpy ( &command->cdb, &iscsi->command->cdb, sizeof ( command->cdb ));
52 * Send iSCSI SCSI command data
54 * @v iscsi iSCSI session
56 static void iscsi_tx_command ( struct iscsi_session *iscsi ) {
57 tcp_send ( &iscsi->tcp, iscsi->command->data_out + iscsi->tx_offset,
58 iscsi->command->data_out_len - iscsi->tx_offset );
62 * Receive data segment of an iSCSI data-in PDU
64 * @v iscsi iSCSI session
65 * @v data Received data
66 * @v len Length of received data
67 * @v remaining Data remaining after this data
70 static void iscsi_rx_data_in ( struct iscsi_session *iscsi, void *data,
71 size_t len, size_t remaining ) {
72 struct iscsi_bhs_data_in *data_in = &iscsi->rx_bhs.data_in;
75 /* Copy data to data-in buffer */
76 offset = ntohl ( data_in->offset ) + iscsi->rx_offset;
77 assert ( iscsi->command != NULL );
78 assert ( iscsi->command->data_in != NULL );
79 assert ( ( offset + len ) <= iscsi->command->data_in_len );
80 memcpy ( ( iscsi->command->data_in + offset ), data, len );
82 /* If this is the end, flag as complete */
83 if ( ( data_in->flags & ISCSI_FLAG_FINAL ) && ( remaining == 0 ) )
84 iscsi->status |= ISCSI_STATUS_DONE;
87 /****************************************************************************
94 * Build iSCSI login request strings
96 * @v iscsi iSCSI session
98 * These are the initial set of strings sent in the first login
101 static int iscsi_build_login_request_strings ( struct iscsi_session *iscsi,
102 void *data, size_t len ) {
103 return snprintf ( data, len,
106 "MaxRecvDataSegmentLength=512%c"
107 "SessionType=Normal%c"
109 "HeaderDigest=None%c",
110 iscsi->initiator, 0, iscsi->target, 0,
115 * Build iSCSI login request BHS
117 * @v iscsi iSCSI session
118 * @v first Login request is the first request of a session
120 static void iscsi_start_login ( struct iscsi_session *iscsi, int first ) {
121 struct iscsi_bhs_login_request *request = &iscsi->tx_bhs.login_request;
124 /* Construct BHS and initiate transmission */
125 iscsi_start_tx ( iscsi );
126 request->opcode = ( ISCSI_OPCODE_LOGIN_REQUEST |
127 ISCSI_FLAG_IMMEDIATE );
128 request->flags = ( ISCSI_LOGIN_FLAG_TRANSITION |
129 ISCSI_LOGIN_CSG_OPERATIONAL_NEGOTIATION |
130 ISCSI_LOGIN_NSG_FULL_FEATURE_PHASE );
131 /* version_max and version_min left as zero */
133 len = iscsi_build_login_request_strings ( iscsi, NULL, 0 );
134 ISCSI_SET_LENGTHS ( request->lengths, 0, len );
136 request->isid_iana_en = htonl ( ISCSI_ISID_IANA |
137 IANA_EN_FEN_SYSTEMS );
138 /* isid_iana_qual left as zero */
139 request->tsih = htons ( iscsi->tsih );
140 /* itt left as zero */
141 /* cid left as zero */
145 * Transmit data segment of an iSCSI login request PDU
147 * @v iscsi iSCSI session
149 * For login requests, the data segment consists of the login strings.
151 static void iscsi_tx_login_request ( struct iscsi_session *iscsi ) {
154 len = iscsi_build_login_request_strings ( iscsi, tcp_buffer,
156 tcp_send ( &iscsi->tcp, tcp_buffer + iscsi->tx_offset,
157 len - iscsi->tx_offset );
161 * Receive data segment of an iSCSI login response PDU
163 * @v iscsi iSCSI session
164 * @v data Received data
165 * @v len Length of received data
166 * @v remaining Data remaining after this data
169 static void iscsi_rx_login_response ( struct iscsi_session *iscsi,
172 size_t remaining __unused ) {
173 struct iscsi_bhs_login_response *response
174 = &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 );
185 /* If server did not transition, send back another login
186 * request without any login strings.
188 if ( ! ( response->flags & ISCSI_LOGIN_FLAG_TRANSITION ) ) {
189 iscsi_start_login ( iscsi, 0 );
193 /* Record TSIH for future reference */
194 iscsi->tsih = ntohl ( response->tsih );
196 /* Send the SCSI command */
197 iscsi_start_command ( iscsi );
200 /****************************************************************************
202 * iSCSI to TCP interface
206 static inline struct iscsi_session *
207 tcp_to_iscsi ( struct tcp_connection *conn ) {
208 return container_of ( conn, struct iscsi_session, tcp );
212 * Start up a new TX PDU
214 * @v iscsi iSCSI session
216 * This initiates the process of sending a new PDU. Only one PDU may
217 * be in transit at any one time.
219 static void iscsi_start_tx ( struct iscsi_session *iscsi ) {
220 assert ( iscsi->tx_state == ISCSI_TX_IDLE );
222 /* Initialise TX BHS */
223 memset ( &iscsi->tx_bhs, 0, sizeof ( iscsi->tx_bhs ) );
224 iscsi->tx_bhs.common_request.cmdsn = htonl ( iscsi->cmdsn );
225 iscsi->tx_bhs.common_request.expstatsn = htonl ( iscsi->statsn + 1 );
227 /* Flag TX engine to start transmitting */
228 iscsi->tx_state = ISCSI_TX_BHS;
229 iscsi->tx_offset = 0;
233 * Transmit data segment of an iSCSI PDU
235 * @v iscsi iSCSI session
237 * Handle transmission of part of a PDU data segment. iscsi::tx_bhs
238 * will be valid when this is called.
240 static void iscsi_tx_data ( struct iscsi_session *iscsi ) {
241 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
243 switch ( common->opcode & ISCSI_OPCODE_MASK ) {
244 case ISCSI_OPCODE_SCSI_COMMAND:
245 iscsi_tx_command ( iscsi );
247 case ISCSI_OPCODE_LOGIN_REQUEST:
248 iscsi_tx_login_request ( iscsi );
259 * @v iscsi iSCSI session
261 * Updates iscsi->tx_offset and, if applicable, transitions to the
264 static void iscsi_acked ( struct tcp_connection *conn, size_t len ) {
265 struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
266 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
267 size_t max_tx_offset;
268 enum iscsi_tx_state next_state;
270 iscsi->tx_offset += len;
272 switch ( iscsi->tx_state ) {
274 max_tx_offset = sizeof ( iscsi->tx_bhs );
275 next_state = ISCSI_TX_AHS;
278 max_tx_offset = 4 * ISCSI_AHS_LEN ( common->lengths );
279 next_state = ISCSI_TX_DATA;
282 max_tx_offset = ISCSI_DATA_LEN ( common->lengths );
283 next_state = ISCSI_TX_DATA_PADDING;
285 case ISCSI_TX_DATA_PADDING:
286 max_tx_offset = ISCSI_DATA_PAD_LEN ( common->lengths );
287 next_state = ISCSI_TX_IDLE;
295 assert ( iscsi->tx_offset <= max_tx_offset );
297 /* If the whole of the current portion has not yet
298 * been acked, stay in this state for now.
300 if ( iscsi->tx_offset != max_tx_offset )
303 iscsi->tx_state = next_state;
304 iscsi->tx_offset = 0;
311 * @v iscsi iSCSI session
313 * Constructs data to be sent for the current TX state
315 static void iscsi_senddata ( struct tcp_connection *conn ) {
316 struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
317 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
318 static const char pad[] = { '\0', '\0', '\0' };
320 switch ( iscsi->tx_state ) {
322 /* Nothing to send */
325 tcp_send ( conn, &iscsi->tx_bhs.bytes[iscsi->tx_offset],
326 ( sizeof ( iscsi->tx_bhs ) - iscsi->tx_offset ) );
329 /* We don't yet have an AHS transmission mechanism */
333 iscsi_tx_data ( iscsi );
335 case ISCSI_TX_DATA_PADDING:
336 tcp_send ( conn, pad, ( ISCSI_DATA_PAD_LEN ( common->lengths )
337 - iscsi->tx_offset ) );
346 * Receive data segment of an iSCSI PDU
348 * @v iscsi iSCSI session
349 * @v data Received data
350 * @v len Length of received data
351 * @v remaining Data remaining after this data
353 * Handle processing of part of a PDU data segment. iscsi::rx_bhs
354 * will be valid when this is called.
356 static void iscsi_rx_data ( struct iscsi_session *iscsi, void *data,
357 size_t len, size_t remaining ) {
358 struct iscsi_bhs_common_response *response
359 = &iscsi->rx_bhs.common_response;
361 /* Update cmdsn and statsn */
362 iscsi->cmdsn = ntohl ( response->expcmdsn );
363 iscsi->statsn = ntohl ( response->statsn );
365 /* Increment itt when we receive a final response */
366 if ( response->flags & ISCSI_FLAG_FINAL )
369 switch ( response->opcode & ISCSI_OPCODE_MASK ) {
370 case ISCSI_OPCODE_LOGIN_RESPONSE:
371 iscsi_rx_login_response ( iscsi, data, len, remaining );
373 case ISCSI_OPCODE_DATA_IN:
374 iscsi_rx_data_in ( iscsi, data, len, remaining );
377 printf ( "Unknown iSCSI opcode %02x\n", response->opcode );
378 iscsi->status |= ( ISCSI_STATUS_DONE | ISCSI_STATUS_ERR );
384 * Discard portion of an iSCSI PDU.
386 * @v iscsi iSCSI session
387 * @v data Received data
388 * @v len Length of received data
389 * @v remaining Data remaining after this data
391 * This discards data from a portion of a received PDU.
393 static void iscsi_rx_discard ( struct iscsi_session *iscsi __unused,
394 void *data __unused, size_t len __unused,
395 size_t remaining __unused ) {
400 * Receive basic header segment of an iSCSI PDU
402 * @v iscsi iSCSI session
403 * @v data Received data
404 * @v len Length of received data
405 * @v remaining Data remaining after this data
407 * This fills in iscsi::rx_bhs with the data from the BHS portion of
410 static void iscsi_rx_bhs ( struct iscsi_session *iscsi, void *data,
411 size_t len, size_t remaining __unused ) {
412 memcpy ( &iscsi->rx_bhs.bytes[iscsi->rx_offset], data, len );
418 * @v tcp TCP connection
419 * @v data Received data
420 * @v len Length of received data
422 * This handles received PDUs. The receive strategy is to fill in
423 * iscsi::rx_bhs with the contents of the BHS portion of the PDU,
424 * throw away any AHS portion, and then process each part of the data
425 * portion as it arrives. The data processing routine therefore
426 * always has a full copy of the BHS available, even for portions of
427 * the data in different packets to the BHS.
429 static void iscsi_newdata ( struct tcp_connection *conn, void *data,
431 struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
432 struct iscsi_bhs_common *common = &iscsi->rx_bhs.common;
433 void ( *process ) ( struct iscsi_session *iscsi, void *data,
434 size_t len, size_t remaining );
435 size_t max_rx_offset;
436 enum iscsi_rx_state next_state;
441 switch ( iscsi->rx_state ) {
443 process = iscsi_rx_bhs;
444 max_rx_offset = sizeof ( iscsi->rx_bhs );
445 next_state = ISCSI_RX_AHS;
448 process = iscsi_rx_discard;
449 max_rx_offset = 4 * ISCSI_AHS_LEN ( common->lengths );
450 next_state = ISCSI_RX_DATA;
453 process = iscsi_rx_data;
454 max_rx_offset = ISCSI_DATA_LEN ( common->lengths );
455 next_state = ISCSI_RX_DATA_PADDING;
457 case ISCSI_RX_DATA_PADDING:
458 process = iscsi_rx_discard;
459 max_rx_offset = ISCSI_DATA_PAD_LEN ( common->lengths );
460 next_state = ISCSI_RX_BHS;
467 frag_len = max_rx_offset - iscsi->rx_offset;
468 if ( frag_len > len )
470 remaining = max_rx_offset - iscsi->rx_offset - frag_len;
471 process ( iscsi, data, frag_len, remaining );
473 iscsi->rx_offset += frag_len;
477 /* If all the data for this state has not yet been
478 * received, stay in this state for now.
480 if ( iscsi->rx_offset != max_rx_offset )
483 iscsi->rx_state = next_state;
484 iscsi->rx_offset = 0;
489 * Handle TCP connection closure
491 * @v conn TCP connection
492 * @v status Error code, if any
495 static void iscsi_closed ( struct tcp_connection *conn, int status __unused ) {
496 struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
498 /* Clear connected flag */
499 iscsi->status &= ~ISCSI_STATUS_CONNECTED;
501 /* Retry connection if within the retry limit, otherwise fail */
502 if ( ++iscsi->retry_count <= ISCSI_MAX_RETRIES ) {
503 tcp_connect ( conn );
505 iscsi->status |= ( ISCSI_STATUS_DONE | ISCSI_STATUS_ERR );
510 * Handle TCP connection opening
512 * @v conn TCP connection
515 static void iscsi_connected ( struct tcp_connection *conn ) {
516 struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
518 /* Set connected flag and reset retry count */
519 iscsi->status |= ISCSI_STATUS_CONNECTED;
520 iscsi->retry_count = 0;
522 /* Prepare to receive PDUs. */
523 iscsi->rx_state = ISCSI_RX_BHS;
524 iscsi->rx_offset = 0;
526 /* Start logging in */
527 iscsi_start_login ( iscsi, 1 );
530 /** iSCSI TCP operations */
531 static struct tcp_operations iscsi_tcp_operations = {
532 .closed = iscsi_closed,
533 .connected = iscsi_connected,
534 .acked = iscsi_acked,
535 .newdata = iscsi_newdata,
536 .senddata = iscsi_senddata,
540 * Issue SCSI command via iSCSI session
542 * @v iscsi iSCSI session
543 * @v command SCSI command
544 * @ret rc Return status code
546 static int iscsi_command ( struct iscsi_session *iscsi,
547 struct scsi_command *command ) {
548 iscsi->command = command;
549 iscsi->status &= ~( ISCSI_STATUS_DONE | ISCSI_STATUS_ERR );
551 if ( iscsi->status & ISCSI_STATUS_CONNECTED ) {
552 iscsi_start_command ( iscsi );
554 iscsi->tcp.tcp_op = &iscsi_tcp_operations;
555 tcp_connect ( &iscsi->tcp );
558 while ( ! ( iscsi->status & ISCSI_STATUS_DONE ) ) {
562 iscsi->command = NULL;
564 return ( ( iscsi->status & ISCSI_STATUS_ERR ) ? -EIO : 0 );
568 * Issue SCSI command via iSCSI device
570 * @v scsi SCSI device
571 * @v command SCSI command
572 * @ret rc Return status code
574 static int iscsi_scsi_command ( struct scsi_device *scsi,
575 struct scsi_command *command ) {
576 struct iscsi_device *iscsidev
577 = container_of ( scsi, struct iscsi_device, scsi );
579 return iscsi_command ( &iscsidev->iscsi, command );
583 * Initialise iSCSI device
585 * @v iscsidev iSCSI device
587 int init_iscsidev ( struct iscsi_device *iscsidev ) {
588 iscsidev->scsi.command = iscsi_scsi_command;
589 iscsidev->scsi.lun = iscsidev->iscsi.lun;
590 return init_scsidev ( &iscsidev->scsi );