2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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.
26 #include <gpxe/scsi.h>
27 #include <gpxe/process.h>
28 #include <gpxe/uaccess.h>
29 #include <gpxe/iscsi.h>
37 static void iscsi_start_tx ( struct iscsi_session *iscsi );
38 static void iscsi_start_data_out ( struct iscsi_session *iscsi,
39 unsigned int datasn );
42 * Receive PDU data into buffer
44 * @v iscsi iSCSI session
45 * @v data Data to receive
46 * @v len Length of data
47 * @ret rc Return status code
49 * This can be used when the RX PDU type handler wishes to buffer up
50 * all received data and process the PDU as a single unit. The caller
51 * is repsonsible for calling iscsi_rx_buffered_data_done() after
52 * processing the data.
54 static int iscsi_rx_buffered_data ( struct iscsi_session *iscsi,
55 const void *data, size_t len ) {
57 /* Allocate buffer on first call */
58 if ( ! iscsi->rx_buffer ) {
59 iscsi->rx_buffer = malloc ( iscsi->rx_len );
60 if ( ! iscsi->rx_buffer )
64 /* Copy data to buffer */
65 assert ( ( iscsi->rx_offset + len ) <= iscsi->rx_len );
66 memcpy ( ( iscsi->rx_buffer + iscsi->rx_offset ), data, len );
72 * Finish receiving PDU data into buffer
74 * @v iscsi iSCSI session
76 static void iscsi_rx_buffered_data_done ( struct iscsi_session *iscsi ) {
77 free ( iscsi->rx_buffer );
78 iscsi->rx_buffer = NULL;
82 * Mark iSCSI operation as complete
84 * @v iscsi iSCSI session
85 * @v rc Return status code
87 * Note that iscsi_done() will not close the connection, and must
88 * therefore be called only when the internal state machines are in an
89 * appropriate state, otherwise bad things may happen on the next call
90 * to iscsi_issue(). The general rule is to call iscsi_done() only at
91 * the end of receiving a PDU; at this point the TX and RX engines
92 * should both be idle.
94 static void iscsi_done ( struct iscsi_session *iscsi, int rc ) {
96 /* Clear current SCSI command */
97 iscsi->command = NULL;
99 /* Free any dynamically allocated memory */
100 chap_finish ( &iscsi->chap );
101 iscsi_rx_buffered_data_done ( iscsi );
103 /* Mark asynchronous operation as complete */
104 async_done ( &iscsi->aop, rc );
107 /****************************************************************************
109 * iSCSI SCSI command issuing
114 * Build iSCSI SCSI command BHS
116 * @v iscsi iSCSI session
118 * We don't currently support bidirectional commands (i.e. with both
119 * Data-In and Data-Out segments); these would require providing code
120 * to generate an AHS, and there doesn't seem to be any need for it at
123 static void iscsi_start_command ( struct iscsi_session *iscsi ) {
124 struct iscsi_bhs_scsi_command *command = &iscsi->tx_bhs.scsi_command;
126 assert ( ! ( iscsi->command->data_in && iscsi->command->data_out ) );
128 /* Construct BHS and initiate transmission */
129 iscsi_start_tx ( iscsi );
130 command->opcode = ISCSI_OPCODE_SCSI_COMMAND;
131 command->flags = ( ISCSI_FLAG_FINAL |
132 ISCSI_COMMAND_ATTR_SIMPLE );
133 if ( iscsi->command->data_in )
134 command->flags |= ISCSI_COMMAND_FLAG_READ;
135 if ( iscsi->command->data_out )
136 command->flags |= ISCSI_COMMAND_FLAG_WRITE;
137 /* lengths left as zero */
138 command->lun = iscsi->lun;
139 command->itt = htonl ( ++iscsi->itt );
140 command->exp_len = htonl ( iscsi->command->data_in_len |
141 iscsi->command->data_out_len );
142 command->cmdsn = htonl ( iscsi->cmdsn );
143 command->expstatsn = htonl ( iscsi->statsn + 1 );
144 memcpy ( &command->cdb, &iscsi->command->cdb, sizeof ( command->cdb ));
145 DBG ( "iSCSI %p start " SCSI_CDB_FORMAT " %s %#x\n",
146 iscsi, SCSI_CDB_DATA ( command->cdb ),
147 ( iscsi->command->data_in ? "in" : "out" ),
148 ( iscsi->command->data_in ?
149 iscsi->command->data_in_len : iscsi->command->data_out_len ) );
153 * Receive data segment of an iSCSI SCSI response PDU
155 * @v iscsi iSCSI session
156 * @v data Received data
157 * @v len Length of received data
158 * @v remaining Data remaining after this data
161 static void iscsi_rx_scsi_response ( struct iscsi_session *iscsi, void *data,
162 size_t len, size_t remaining ) {
163 struct iscsi_bhs_scsi_response *response
164 = &iscsi->rx_bhs.scsi_response;
167 /* Capture the sense response code as it floats past, if present */
168 sense_offset = ISCSI_SENSE_RESPONSE_CODE_OFFSET - iscsi->rx_offset;
169 if ( ( sense_offset >= 0 ) && len ) {
170 iscsi->command->sense_response =
171 * ( ( char * ) data + sense_offset );
174 /* Wait for whole SCSI response to arrive */
178 /* Record SCSI status code */
179 iscsi->command->status = response->status;
181 /* Mark as completed, with error if applicable */
182 if ( response->response == ISCSI_RESPONSE_COMMAND_COMPLETE ) {
183 iscsi_done ( iscsi, 0 );
185 iscsi_done ( iscsi, -EIO );
190 * Receive data segment of an iSCSI data-in PDU
192 * @v iscsi iSCSI session
193 * @v data Received data
194 * @v len Length of received data
195 * @v remaining Data remaining after this data
198 static void iscsi_rx_data_in ( struct iscsi_session *iscsi, void *data,
199 size_t len, size_t remaining __unused ) {
200 struct iscsi_bhs_data_in *data_in = &iscsi->rx_bhs.data_in;
201 unsigned long offset;
203 /* Copy data to data-in buffer */
204 offset = ntohl ( data_in->offset ) + iscsi->rx_offset;
205 assert ( iscsi->command != NULL );
206 assert ( iscsi->command->data_in );
207 assert ( ( offset + len ) <= iscsi->command->data_in_len );
208 copy_to_user ( iscsi->command->data_in, offset, data, len );
210 /* Record SCSI status, if present */
211 if ( data_in->flags & ISCSI_DATA_FLAG_STATUS )
212 iscsi->command->status = data_in->status;
214 /* If this is the end, flag as complete */
215 if ( ( offset + len ) == iscsi->command->data_in_len ) {
216 assert ( data_in->flags & ISCSI_FLAG_FINAL );
217 assert ( remaining == 0 );
218 iscsi_done ( iscsi, 0 );
223 * Receive data segment of an iSCSI R2T PDU
225 * @v iscsi iSCSI session
226 * @v data Received data
227 * @v len Length of received data
228 * @v remaining Data remaining after this data
231 static void iscsi_rx_r2t ( struct iscsi_session *iscsi, void *data __unused,
232 size_t len __unused, size_t remaining __unused ) {
233 struct iscsi_bhs_r2t *r2t = &iscsi->rx_bhs.r2t;
235 /* Record transfer parameters and trigger first data-out */
236 iscsi->ttt = ntohl ( r2t->ttt );
237 iscsi->transfer_offset = ntohl ( r2t->offset );
238 iscsi->transfer_len = ntohl ( r2t->len );
239 iscsi_start_data_out ( iscsi, 0 );
243 * Build iSCSI data-out BHS
245 * @v iscsi iSCSI session
246 * @v datasn Data sequence number within the transfer
249 static void iscsi_start_data_out ( struct iscsi_session *iscsi,
250 unsigned int datasn ) {
251 struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
252 unsigned long offset;
253 unsigned long remaining;
256 /* We always send 512-byte Data-Out PDUs; this removes the
257 * need to worry about the target's MaxRecvDataSegmentLength.
259 offset = datasn * 512;
260 remaining = iscsi->transfer_len - offset;
265 /* Construct BHS and initiate transmission */
266 iscsi_start_tx ( iscsi );
267 data_out->opcode = ISCSI_OPCODE_DATA_OUT;
268 if ( len == remaining )
269 data_out->flags = ( ISCSI_FLAG_FINAL );
270 ISCSI_SET_LENGTHS ( data_out->lengths, 0, len );
271 data_out->lun = iscsi->lun;
272 data_out->itt = htonl ( iscsi->itt );
273 data_out->ttt = htonl ( iscsi->ttt );
274 data_out->expstatsn = htonl ( iscsi->statsn + 1 );
275 data_out->datasn = htonl ( datasn );
276 data_out->offset = htonl ( iscsi->transfer_offset + offset );
277 DBG ( "iSCSI %p start data out DataSN %#x len %#lx\n",
278 iscsi, datasn, len );
282 * Complete iSCSI data-out PDU transmission
284 * @v iscsi iSCSI session
287 static void iscsi_data_out_done ( struct iscsi_session *iscsi ) {
288 struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
290 /* If we haven't reached the end of the sequence, start
291 * sending the next data-out PDU.
293 if ( ! ( data_out->flags & ISCSI_FLAG_FINAL ) )
294 iscsi_start_data_out ( iscsi, ntohl ( data_out->datasn ) + 1 );
298 * Send iSCSI data-out data segment
300 * @v iscsi iSCSI session
301 * @v buf Temporary data buffer
302 * @v len Length of temporary data buffer
304 static void iscsi_tx_data_out ( struct iscsi_session *iscsi,
305 void *buf, size_t len ) {
306 struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
307 unsigned long offset;
308 unsigned long remaining;
310 offset = ( iscsi->transfer_offset + ntohl ( data_out->offset ) +
312 remaining = ( iscsi->tx_len - iscsi->tx_offset );
313 assert ( iscsi->command != NULL );
314 assert ( iscsi->command->data_out );
315 assert ( ( offset + remaining ) <= iscsi->command->data_out_len );
317 if ( remaining < len )
319 copy_from_user ( buf, iscsi->command->data_out, offset, len );
321 tcp_send ( &iscsi->tcp, buf, len );
324 /****************************************************************************
331 * Version of snprintf() that accepts a signed buffer size
333 * @v buf Buffer into which to write the string
334 * @v size Size of buffer
335 * @v fmt Format string
336 * @v args Arguments corresponding to the format string
337 * @ret len Length of formatted string
339 * This is a utility function for iscsi_build_login_request_strings().
341 static int ssnprintf ( char *buf, ssize_t ssize, const char *fmt, ... ) {
345 /* Treat negative buffer size as zero buffer size */
349 /* Hand off to vsnprintf */
350 va_start ( args, fmt );
351 len = vsnprintf ( buf, ssize, fmt, args );
357 * Build iSCSI login request strings
359 * @v iscsi iSCSI session
361 * These are the initial set of strings sent in the first login
362 * request PDU. We want the following settings:
366 * MaxConnections is irrelevant; we make only one connection anyway
368 * ImmediateData is irrelevant; we never send immediate data
369 * MaxRecvDataSegmentLength=8192 (default; we don't care)
370 * MaxBurstLength=262144 (default; we don't care)
371 * FirstBurstLength=262144 (default; we don't care)
372 * DefaultTime2Wait=0 [2]
373 * DefaultTime2Retain=0 [2]
374 * MaxOutstandingR2T=1
376 * DataSequenceInOrder=Yes
377 * ErrorRecoveryLevel=0
379 * [1] InitialR2T has an OR resolution function, so the target may
380 * force us to use it. We therefore simplify our logic by always
383 * [2] These ensure that we can safely start a new task once we have
384 * reconnected after a failure, without having to manually tidy up
387 static int iscsi_build_login_request_strings ( struct iscsi_session *iscsi,
388 void *data, size_t len ) {
389 unsigned int used = 0;
392 if ( iscsi->status & ISCSI_STATUS_STRINGS_SECURITY ) {
393 used += ssnprintf ( data + used, len - used,
396 "SessionType=Normal%c"
397 "AuthMethod=CHAP,None%c",
398 iscsi->initiator_iqn, 0,
399 iscsi->target_iqn, 0, 0, 0 );
402 if ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_ALGORITHM ) {
403 used += ssnprintf ( data + used, len - used, "CHAP_A=5%c", 0 );
406 if ( ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_RESPONSE ) &&
408 used += ssnprintf ( data + used, len - used,
409 "CHAP_N=%s%cCHAP_R=0x",
410 iscsi->username, 0 );
411 for ( i = 0 ; i < iscsi->chap.response_len ; i++ ) {
412 used += ssnprintf ( data + used, len - used, "%02x",
413 iscsi->chap.response[i] );
415 used += ssnprintf ( data + used, len - used, "%c", 0 );
418 if ( iscsi->status & ISCSI_STATUS_STRINGS_OPERATIONAL ) {
419 used += ssnprintf ( data + used, len - used,
420 "HeaderDigest=None%c"
423 "DefaultTime2Wait=0%c"
424 "DefaultTime2Retain=0%c"
425 "MaxOutstandingR2T=1%c"
426 "DataPDUInOrder=Yes%c"
427 "DataSequenceInOrder=Yes%c"
428 "ErrorRecoveryLevel=0%c",
429 0, 0, 0, 0, 0, 0, 0, 0, 0 );
436 * Build iSCSI login request BHS
438 * @v iscsi iSCSI session
440 static void iscsi_start_login ( struct iscsi_session *iscsi ) {
441 struct iscsi_bhs_login_request *request = &iscsi->tx_bhs.login_request;
444 /* Construct BHS and initiate transmission */
445 iscsi_start_tx ( iscsi );
446 request->opcode = ( ISCSI_OPCODE_LOGIN_REQUEST |
447 ISCSI_FLAG_IMMEDIATE );
448 request->flags = ( ( iscsi->status & ISCSI_STATUS_PHASE_MASK ) |
449 ISCSI_LOGIN_FLAG_TRANSITION );
450 /* version_max and version_min left as zero */
451 len = iscsi_build_login_request_strings ( iscsi, NULL, 0 );
452 ISCSI_SET_LENGTHS ( request->lengths, 0, len );
453 request->isid_iana_en = htonl ( ISCSI_ISID_IANA |
454 IANA_EN_FEN_SYSTEMS );
455 /* isid_iana_qual left as zero */
456 request->tsih = htons ( iscsi->tsih );
457 request->itt = htonl ( iscsi->itt );
458 /* cid left as zero */
459 request->cmdsn = htonl ( iscsi->cmdsn );
460 request->expstatsn = htonl ( iscsi->statsn + 1 );
464 * Complete iSCSI login request PDU transmission
466 * @v iscsi iSCSI session
469 static void iscsi_login_request_done ( struct iscsi_session *iscsi ) {
471 /* Clear any "strings to send" flags */
472 iscsi->status &= ~ISCSI_STATUS_STRINGS_MASK;
476 * Transmit data segment of an iSCSI login request PDU
478 * @v iscsi iSCSI session
479 * @v buf Temporary data buffer
480 * @v len Length of temporary data buffer
482 * For login requests, the data segment consists of the login strings.
484 static void iscsi_tx_login_request ( struct iscsi_session *iscsi,
485 void *buf, size_t len ) {
486 len = iscsi_build_login_request_strings ( iscsi, buf, len );
487 tcp_send ( &iscsi->tcp, buf + iscsi->tx_offset,
488 len - iscsi->tx_offset );
492 * Handle iSCSI TargetAddress text value
494 * @v iscsi iSCSI session
495 * @v value TargetAddress value
497 static void iscsi_handle_targetaddress_value ( struct iscsi_session *iscsi,
498 const char *value ) {
499 struct in_addr address;
500 struct sockaddr_in *sin = ( struct sockaddr_in * ) &iscsi->target;
502 if ( inet_aton ( value, &address ) == 0 ) {
503 DBG ( "iSCSI %p received invalid TargetAddress \"%s\"\n",
508 DBG ( "iSCSI %p will redirect to %s\n", iscsi, value );
509 sin->sin_addr = address;
513 * Handle iSCSI AuthMethod text value
515 * @v iscsi iSCSI session
516 * @v value AuthMethod value
518 static void iscsi_handle_authmethod_value ( struct iscsi_session *iscsi,
519 const char *value ) {
521 /* If server requests CHAP, send the CHAP_A string */
522 if ( strcmp ( value, "CHAP" ) == 0 ) {
523 DBG ( "iSCSI %p initiating CHAP authentication\n", iscsi );
524 iscsi->status |= ISCSI_STATUS_STRINGS_CHAP_ALGORITHM;
529 * Handle iSCSI CHAP_A text value
531 * @v iscsi iSCSI session
532 * @v value CHAP_A value
534 static void iscsi_handle_chap_a_value ( struct iscsi_session *iscsi,
535 const char *value ) {
538 /* We only ever offer "5" (i.e. MD5) as an algorithm, so if
539 * the server responds with anything else it is a protocol
542 if ( strcmp ( value, "5" ) != 0 ) {
543 DBG ( "iSCSI %p got invalid CHAP algorithm \"%s\"\n",
547 /* Prepare for CHAP with MD5 */
548 if ( ( rc = chap_init ( &iscsi->chap, &md5_algorithm ) ) != 0 ) {
549 DBG ( "iSCSI %p could not initialise CHAP\n", iscsi );
550 iscsi_done ( iscsi, rc );
555 * Handle iSCSI CHAP_I text value
557 * @v iscsi iSCSI session
558 * @v value CHAP_I value
560 static void iscsi_handle_chap_i_value ( struct iscsi_session *iscsi,
561 const char *value ) {
562 unsigned int identifier;
565 /* The CHAP identifier is an integer value */
566 identifier = strtoul ( value, &endp, 0 );
567 if ( *endp != '\0' ) {
568 DBG ( "iSCSI %p saw invalid CHAP identifier \"%s\"\n",
572 /* Identifier and secret are the first two components of the
575 chap_set_identifier ( &iscsi->chap, identifier );
576 if ( iscsi->password ) {
577 chap_update ( &iscsi->chap, iscsi->password,
578 strlen ( iscsi->password ) );
583 * Handle iSCSI CHAP_C text value
585 * @v iscsi iSCSI session
586 * @v value CHAP_C value
588 static void iscsi_handle_chap_c_value ( struct iscsi_session *iscsi,
589 const char *value ) {
594 /* Check and strip leading "0x" */
595 if ( ( value[0] != '0' ) || ( value[1] != 'x' ) ) {
596 DBG ( "iSCSI %p saw invalid CHAP challenge \"%s\"\n",
601 /* Process challenge an octet at a time */
602 for ( ; ( value[0] && value[1] ) ; value += 2 ) {
603 memcpy ( buf, value, 2 );
605 byte = strtoul ( buf, &endp, 16 );
606 if ( *endp != '\0' ) {
607 DBG ( "iSCSI %p saw invalid CHAP challenge byte "
608 "\"%s\"\n", iscsi, buf );
610 chap_update ( &iscsi->chap, &byte, sizeof ( byte ) );
613 /* Build CHAP response */
614 DBG ( "iSCSI %p sending CHAP response\n", iscsi );
615 chap_respond ( &iscsi->chap );
616 iscsi->status |= ISCSI_STATUS_STRINGS_CHAP_RESPONSE;
619 /** An iSCSI text string that we want to handle */
620 struct iscsi_string_type {
623 * This is the portion up to and including the "=" sign,
624 * e.g. "InitiatorName=", "CHAP_A=", etc.
627 /** Handle iSCSI string value
629 * @v iscsi iSCSI session
630 * @v value iSCSI string value
632 void ( * handle_value ) ( struct iscsi_session *iscsi,
636 /** iSCSI text strings that we want to handle */
637 struct iscsi_string_type iscsi_string_types[] = {
638 { "TargetAddress=", iscsi_handle_targetaddress_value },
639 { "AuthMethod=", iscsi_handle_authmethod_value },
640 { "CHAP_A=", iscsi_handle_chap_a_value },
641 { "CHAP_I=", iscsi_handle_chap_i_value },
642 { "CHAP_C=", iscsi_handle_chap_c_value },
647 * Handle iSCSI string
649 * @v iscsi iSCSI session
650 * @v string iSCSI string (in "key=value" format)
652 static void iscsi_handle_string ( struct iscsi_session *iscsi,
653 const char *string ) {
654 struct iscsi_string_type *type;
657 for ( type = iscsi_string_types ; type->key ; type++ ) {
658 key_len = strlen ( type->key );
659 if ( strncmp ( string, type->key, key_len ) == 0 ) {
660 DBG ( "iSCSI %p handling %s\n", iscsi, string );
661 type->handle_value ( iscsi, ( string + key_len ) );
665 DBG ( "iSCSI %p ignoring %s\n", iscsi, string );
669 * Handle iSCSI strings
671 * @v iscsi iSCSI session
672 * @v string iSCSI string buffer
673 * @v len Length of string buffer
675 static void iscsi_handle_strings ( struct iscsi_session *iscsi,
676 const char *strings, size_t len ) {
679 /* Handle each string in turn, taking care not to overrun the
680 * data buffer in case of badly-terminated data.
683 string_len = ( strnlen ( strings, len ) + 1 );
684 if ( string_len > len )
686 iscsi_handle_string ( iscsi, strings );
687 strings += string_len;
693 * Receive data segment of an iSCSI login response PDU
695 * @v iscsi iSCSI session
696 * @v data Received data
697 * @v len Length of received data
698 * @v remaining Data remaining after this data
701 static void iscsi_rx_login_response ( struct iscsi_session *iscsi, void *data,
702 size_t len, size_t remaining ) {
703 struct iscsi_bhs_login_response *response
704 = &iscsi->rx_bhs.login_response;
707 /* Buffer up the PDU data */
708 if ( ( rc = iscsi_rx_buffered_data ( iscsi, data, len ) ) != 0 ) {
709 DBG ( "iSCSI %p could not buffer login response\n", iscsi );
710 iscsi_done ( iscsi, rc );
716 /* Process string data and discard string buffer */
717 iscsi_handle_strings ( iscsi, iscsi->rx_buffer, iscsi->rx_len );
718 iscsi_rx_buffered_data_done ( iscsi );
720 /* Check for login redirection */
721 if ( response->status_class == ISCSI_STATUS_REDIRECT ) {
722 DBG ( "iSCSI %p redirecting to new server\n", iscsi );
723 tcp_close ( &iscsi->tcp );
725 if ( ( rc = tcp_connect ( &iscsi->tcp, &iscsi->target,
727 DBG ( "iSCSI %p could not open TCP connection\n",
729 iscsi_done ( iscsi, rc );
734 /* Check for fatal errors */
735 if ( response->status_class != 0 ) {
736 DBG ( "iSCSI login failure: class %02x detail %02x\n",
737 response->status_class, response->status_detail );
738 iscsi_done ( iscsi, -EPERM );
742 /* Handle login transitions */
743 if ( response->flags & ISCSI_LOGIN_FLAG_TRANSITION ) {
744 switch ( response->flags & ISCSI_LOGIN_NSG_MASK ) {
745 case ISCSI_LOGIN_NSG_OPERATIONAL_NEGOTIATION:
747 ( ISCSI_STATUS_OPERATIONAL_NEGOTIATION_PHASE |
748 ISCSI_STATUS_STRINGS_OPERATIONAL );
750 case ISCSI_LOGIN_NSG_FULL_FEATURE_PHASE:
751 iscsi->status = ISCSI_STATUS_FULL_FEATURE_PHASE;
754 DBG ( "iSCSI %p got invalid response flags %02x\n",
755 iscsi, response->flags );
756 iscsi_done ( iscsi, -EIO );
761 /* Send next login request PDU if we haven't reached the full
764 if ( ( iscsi->status & ISCSI_STATUS_PHASE_MASK ) !=
765 ISCSI_STATUS_FULL_FEATURE_PHASE ) {
766 iscsi_start_login ( iscsi );
770 /* Record TSIH for future reference */
771 iscsi->tsih = ntohl ( response->tsih );
773 /* Send the actual SCSI command */
774 iscsi_start_command ( iscsi );
777 /****************************************************************************
779 * iSCSI to TCP interface
783 static inline struct iscsi_session *
784 tcp_to_iscsi ( struct tcp_application *app ) {
785 return container_of ( app, struct iscsi_session, tcp );
789 * Start up a new TX PDU
791 * @v iscsi iSCSI session
793 * This initiates the process of sending a new PDU. Only one PDU may
794 * be in transit at any one time.
796 static void iscsi_start_tx ( struct iscsi_session *iscsi ) {
797 assert ( iscsi->tx_state == ISCSI_TX_IDLE );
799 /* Initialise TX BHS */
800 memset ( &iscsi->tx_bhs, 0, sizeof ( iscsi->tx_bhs ) );
802 /* Flag TX engine to start transmitting */
803 iscsi->tx_state = ISCSI_TX_BHS;
804 iscsi->tx_offset = 0;
808 * Transmit data segment of an iSCSI PDU
810 * @v iscsi iSCSI session
811 * @v buf Temporary data buffer
812 * @v len Length of temporary data buffer
814 * Handle transmission of part of a PDU data segment. iscsi::tx_bhs
815 * will be valid when this is called.
817 static void iscsi_tx_data ( struct iscsi_session *iscsi,
818 void *buf, size_t len ) {
819 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
821 switch ( common->opcode & ISCSI_OPCODE_MASK ) {
822 case ISCSI_OPCODE_DATA_OUT:
823 iscsi_tx_data_out ( iscsi, buf, len );
825 case ISCSI_OPCODE_LOGIN_REQUEST:
826 iscsi_tx_login_request ( iscsi, buf, len );
835 * Complete iSCSI PDU transmission
837 * @v iscsi iSCSI session
839 * Called when a PDU has been completely transmitted and the TX state
840 * machine is about to enter the idle state. iscsi::tx_bhs will be
841 * valid for the just-completed PDU when this is called.
843 static void iscsi_tx_done ( struct iscsi_session *iscsi ) {
844 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
846 switch ( common->opcode & ISCSI_OPCODE_MASK ) {
847 case ISCSI_OPCODE_DATA_OUT:
848 iscsi_data_out_done ( iscsi );
849 case ISCSI_OPCODE_LOGIN_REQUEST:
850 iscsi_login_request_done ( iscsi );
860 * @v iscsi iSCSI session
862 * Updates iscsi->tx_offset and, if applicable, transitions to the
865 static void iscsi_acked ( struct tcp_application *app, size_t len ) {
866 struct iscsi_session *iscsi = tcp_to_iscsi ( app );
867 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
868 enum iscsi_tx_state next_state;
870 iscsi->tx_offset += len;
872 switch ( iscsi->tx_state ) {
874 iscsi->tx_len = sizeof ( iscsi->tx_bhs );
875 next_state = ISCSI_TX_AHS;
878 iscsi->tx_len = 4 * ISCSI_AHS_LEN ( common->lengths );
879 next_state = ISCSI_TX_DATA;
882 iscsi->tx_len = ISCSI_DATA_LEN ( common->lengths );
883 next_state = ISCSI_TX_DATA_PADDING;
885 case ISCSI_TX_DATA_PADDING:
886 iscsi->tx_len = ISCSI_DATA_PAD_LEN ( common->lengths );
887 next_state = ISCSI_TX_IDLE;
895 assert ( iscsi->tx_offset <= iscsi->tx_len );
897 /* If the whole of the current portion has not yet
898 * been acked, stay in this state for now.
900 if ( iscsi->tx_offset != iscsi->tx_len )
903 /* Move to next state. Call iscsi_tx_done() when PDU
904 * transmission is complete.
906 iscsi->tx_state = next_state;
907 iscsi->tx_offset = 0;
908 if ( next_state == ISCSI_TX_IDLE )
909 iscsi_tx_done ( iscsi );
916 * @v iscsi iSCSI session
917 * @v buf Temporary data buffer
918 * @v len Length of temporary data buffer
920 * Constructs data to be sent for the current TX state
922 static void iscsi_senddata ( struct tcp_application *app,
923 void *buf, size_t len ) {
924 struct iscsi_session *iscsi = tcp_to_iscsi ( app );
925 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
926 static const char pad[] = { '\0', '\0', '\0' };
928 switch ( iscsi->tx_state ) {
930 /* Nothing to send */
933 tcp_send ( app, &iscsi->tx_bhs.bytes[iscsi->tx_offset],
934 ( sizeof ( iscsi->tx_bhs ) - iscsi->tx_offset ) );
937 /* We don't yet have an AHS transmission mechanism */
941 iscsi_tx_data ( iscsi, buf, len );
943 case ISCSI_TX_DATA_PADDING:
944 tcp_send ( app, pad, ( ISCSI_DATA_PAD_LEN ( common->lengths )
945 - iscsi->tx_offset ) );
954 * Receive data segment of an iSCSI PDU
956 * @v iscsi iSCSI session
957 * @v data Received data
958 * @v len Length of received data
959 * @v remaining Data remaining after this data
961 * Handle processing of part of a PDU data segment. iscsi::rx_bhs
962 * will be valid when this is called.
964 static void iscsi_rx_data ( struct iscsi_session *iscsi, void *data,
965 size_t len, size_t remaining ) {
966 struct iscsi_bhs_common_response *response
967 = &iscsi->rx_bhs.common_response;
969 /* Update cmdsn and statsn */
970 iscsi->cmdsn = ntohl ( response->expcmdsn );
971 iscsi->statsn = ntohl ( response->statsn );
973 switch ( response->opcode & ISCSI_OPCODE_MASK ) {
974 case ISCSI_OPCODE_LOGIN_RESPONSE:
975 iscsi_rx_login_response ( iscsi, data, len, remaining );
977 case ISCSI_OPCODE_SCSI_RESPONSE:
978 iscsi_rx_scsi_response ( iscsi, data, len, remaining );
980 case ISCSI_OPCODE_DATA_IN:
981 iscsi_rx_data_in ( iscsi, data, len, remaining );
983 case ISCSI_OPCODE_R2T:
984 iscsi_rx_r2t ( iscsi, data, len, remaining );
989 printf ( "Unknown iSCSI opcode %02x\n", response->opcode );
990 iscsi_done ( iscsi, -EOPNOTSUPP );
996 * Discard portion of an iSCSI PDU.
998 * @v iscsi iSCSI session
999 * @v data Received data
1000 * @v len Length of received data
1001 * @v remaining Data remaining after this data
1003 * This discards data from a portion of a received PDU.
1005 static void iscsi_rx_discard ( struct iscsi_session *iscsi __unused,
1006 void *data __unused, size_t len __unused,
1007 size_t remaining __unused ) {
1012 * Receive basic header segment of an iSCSI PDU
1014 * @v iscsi iSCSI session
1015 * @v data Received data
1016 * @v len Length of received data
1017 * @v remaining Data remaining after this data
1019 * This fills in iscsi::rx_bhs with the data from the BHS portion of
1022 static void iscsi_rx_bhs ( struct iscsi_session *iscsi, void *data,
1023 size_t len, size_t remaining __unused ) {
1024 memcpy ( &iscsi->rx_bhs.bytes[iscsi->rx_offset], data, len );
1025 if ( ( iscsi->rx_offset + len ) >= sizeof ( iscsi->rx_bhs ) ) {
1026 DBG ( "iSCSI %p received PDU opcode %#x len %#lx\n",
1027 iscsi, iscsi->rx_bhs.common.opcode,
1028 ISCSI_DATA_LEN ( iscsi->rx_bhs.common.lengths ) );
1035 * @v tcp TCP application
1036 * @v data Received data
1037 * @v len Length of received data
1039 * This handles received PDUs. The receive strategy is to fill in
1040 * iscsi::rx_bhs with the contents of the BHS portion of the PDU,
1041 * throw away any AHS portion, and then process each part of the data
1042 * portion as it arrives. The data processing routine therefore
1043 * always has a full copy of the BHS available, even for portions of
1044 * the data in different packets to the BHS.
1046 static void iscsi_newdata ( struct tcp_application *app, void *data,
1048 struct iscsi_session *iscsi = tcp_to_iscsi ( app );
1049 struct iscsi_bhs_common *common = &iscsi->rx_bhs.common;
1050 void ( *process ) ( struct iscsi_session *iscsi, void *data,
1051 size_t len, size_t remaining );
1052 enum iscsi_rx_state next_state;
1057 switch ( iscsi->rx_state ) {
1059 process = iscsi_rx_bhs;
1060 iscsi->rx_len = sizeof ( iscsi->rx_bhs );
1061 next_state = ISCSI_RX_AHS;
1064 process = iscsi_rx_discard;
1065 iscsi->rx_len = 4 * ISCSI_AHS_LEN ( common->lengths );
1066 next_state = ISCSI_RX_DATA;
1069 process = iscsi_rx_data;
1070 iscsi->rx_len = ISCSI_DATA_LEN ( common->lengths );
1071 next_state = ISCSI_RX_DATA_PADDING;
1073 case ISCSI_RX_DATA_PADDING:
1074 process = iscsi_rx_discard;
1075 iscsi->rx_len = ISCSI_DATA_PAD_LEN ( common->lengths );
1076 next_state = ISCSI_RX_BHS;
1083 frag_len = iscsi->rx_len - iscsi->rx_offset;
1084 if ( frag_len > len )
1086 remaining = iscsi->rx_len - iscsi->rx_offset - frag_len;
1087 process ( iscsi, data, frag_len, remaining );
1089 iscsi->rx_offset += frag_len;
1093 /* If all the data for this state has not yet been
1094 * received, stay in this state for now.
1096 if ( iscsi->rx_offset != iscsi->rx_len )
1099 iscsi->rx_state = next_state;
1100 iscsi->rx_offset = 0;
1105 * Handle TCP connection closure
1107 * @v app TCP application
1108 * @v status Error code, if any
1111 static void iscsi_closed ( struct tcp_application *app, int status ) {
1112 struct iscsi_session *iscsi = tcp_to_iscsi ( app );
1115 /* Clear session status */
1118 /* Retry connection if within the retry limit, otherwise fail */
1119 if ( ++iscsi->retry_count <= ISCSI_MAX_RETRIES ) {
1120 DBG ( "iSCSI %p retrying connection\n", iscsi );
1121 if ( ( rc = tcp_connect ( app, &iscsi->target, 0 ) ) != 0 ) {
1122 DBG ( "iSCSI %p could not open TCP connection\n",
1124 iscsi_done ( iscsi, rc );
1127 printf ( "iSCSI %p retry count exceeded\n", iscsi );
1128 iscsi_done ( iscsi, status );
1133 * Handle TCP connection opening
1135 * @v app TCP application
1138 static void iscsi_connected ( struct tcp_application *app ) {
1139 struct iscsi_session *iscsi = tcp_to_iscsi ( app );
1141 /* Set connected flag and reset retry count */
1142 iscsi->status = ( ISCSI_STATUS_SECURITY_NEGOTIATION_PHASE |
1143 ISCSI_STATUS_STRINGS_SECURITY );
1144 iscsi->retry_count = 0;
1146 /* Prepare to receive PDUs. */
1147 iscsi->rx_state = ISCSI_RX_BHS;
1148 iscsi->rx_offset = 0;
1150 /* Assign fresh initiator task tag */
1153 /* Start logging in */
1154 iscsi_start_login ( iscsi );
1157 /** iSCSI TCP operations */
1158 static struct tcp_operations iscsi_tcp_operations = {
1159 .closed = iscsi_closed,
1160 .connected = iscsi_connected,
1161 .acked = iscsi_acked,
1162 .newdata = iscsi_newdata,
1163 .senddata = iscsi_senddata,
1167 * Issue SCSI command via iSCSI session
1169 * @v iscsi iSCSI session
1170 * @v command SCSI command
1171 * @ret aop Asynchronous operation for this SCSI command
1173 struct async_operation * iscsi_issue ( struct iscsi_session *iscsi,
1174 struct scsi_command *command ) {
1177 assert ( iscsi->command == NULL );
1178 iscsi->command = command;
1180 if ( iscsi->status ) {
1181 if ( ( iscsi->status & ISCSI_STATUS_PHASE_MASK ) ==
1182 ISCSI_STATUS_FULL_FEATURE_PHASE ) {
1183 /* Session already open: issue command */
1184 iscsi_start_command ( iscsi );
1185 tcp_senddata ( &iscsi->tcp );
1187 /* Session failed to reach full feature phase:
1188 * abort immediately rather than retrying the
1191 iscsi_done ( iscsi, -EPERM );
1194 /* Session not open: initiate login */
1195 iscsi->tcp.tcp_op = &iscsi_tcp_operations;
1196 if ( ( rc = tcp_connect ( &iscsi->tcp, &iscsi->target,
1198 DBG ( "iSCSI %p could not open TCP connection\n",
1200 iscsi_done ( iscsi, rc );
1208 * Close down iSCSI session
1210 * @v iscsi iSCSI session
1211 * @ret aop Asynchronous operation
1213 void iscsi_shutdown ( struct iscsi_session *iscsi ) {
1215 tcp_close ( &iscsi->tcp );