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 * Close iSCSI connection
84 * @v iscsi iSCSI session
86 static void iscsi_close ( struct iscsi_session *iscsi ) {
88 /* Close TCP connection */
89 tcp_close ( &iscsi->tcp );
91 /* Clear connection status */
94 /* Reset TX and RX state machines */
95 iscsi->tx_state = ISCSI_TX_IDLE;
96 iscsi->rx_state = ISCSI_RX_BHS;
98 /* Free any dynamically allocated memory */
99 chap_finish ( &iscsi->chap );
100 iscsi_rx_buffered_data_done ( iscsi );
104 * Mark iSCSI operation as complete
106 * @v iscsi iSCSI session
107 * @v rc Return status code
109 * Note that iscsi_done() will not close the connection, and must
110 * therefore be called only when the internal state machines are in an
111 * appropriate state, otherwise bad things may happen on the next call
112 * to iscsi_issue(). The general rule is to call iscsi_done() only at
113 * the end of receiving a PDU; at this point the TX and RX engines
114 * should both be idle.
116 static void iscsi_done ( struct iscsi_session *iscsi, int rc ) {
118 assert ( iscsi->tx_state == ISCSI_TX_IDLE );
120 /* Clear current SCSI command */
121 iscsi->command = NULL;
123 /* Mark asynchronous operation as complete */
124 async_done ( &iscsi->async, rc );
127 /****************************************************************************
129 * iSCSI SCSI command issuing
134 * Build iSCSI SCSI command BHS
136 * @v iscsi iSCSI session
138 * We don't currently support bidirectional commands (i.e. with both
139 * Data-In and Data-Out segments); these would require providing code
140 * to generate an AHS, and there doesn't seem to be any need for it at
143 static void iscsi_start_command ( struct iscsi_session *iscsi ) {
144 struct iscsi_bhs_scsi_command *command = &iscsi->tx_bhs.scsi_command;
146 assert ( ! ( iscsi->command->data_in && iscsi->command->data_out ) );
148 /* Construct BHS and initiate transmission */
149 iscsi_start_tx ( iscsi );
150 command->opcode = ISCSI_OPCODE_SCSI_COMMAND;
151 command->flags = ( ISCSI_FLAG_FINAL |
152 ISCSI_COMMAND_ATTR_SIMPLE );
153 if ( iscsi->command->data_in )
154 command->flags |= ISCSI_COMMAND_FLAG_READ;
155 if ( iscsi->command->data_out )
156 command->flags |= ISCSI_COMMAND_FLAG_WRITE;
157 /* lengths left as zero */
158 command->lun = iscsi->lun;
159 command->itt = htonl ( ++iscsi->itt );
160 command->exp_len = htonl ( iscsi->command->data_in_len |
161 iscsi->command->data_out_len );
162 command->cmdsn = htonl ( iscsi->cmdsn );
163 command->expstatsn = htonl ( iscsi->statsn + 1 );
164 memcpy ( &command->cdb, &iscsi->command->cdb, sizeof ( command->cdb ));
165 DBGC ( iscsi, "iSCSI %p start " SCSI_CDB_FORMAT " %s %#x\n",
166 iscsi, SCSI_CDB_DATA ( command->cdb ),
167 ( iscsi->command->data_in ? "in" : "out" ),
168 ( iscsi->command->data_in ?
169 iscsi->command->data_in_len : iscsi->command->data_out_len ));
173 * Receive data segment of an iSCSI SCSI response PDU
175 * @v iscsi iSCSI session
176 * @v data Received data
177 * @v len Length of received data
178 * @v remaining Data remaining after this data
181 static void iscsi_rx_scsi_response ( struct iscsi_session *iscsi, void *data,
182 size_t len, size_t remaining ) {
183 struct iscsi_bhs_scsi_response *response
184 = &iscsi->rx_bhs.scsi_response;
187 /* Capture the sense response code as it floats past, if present */
188 sense_offset = ISCSI_SENSE_RESPONSE_CODE_OFFSET - iscsi->rx_offset;
189 if ( ( sense_offset >= 0 ) && len ) {
190 iscsi->command->sense_response =
191 * ( ( char * ) data + sense_offset );
194 /* Wait for whole SCSI response to arrive */
198 /* Record SCSI status code */
199 iscsi->command->status = response->status;
201 /* Mark as completed, with error if applicable */
202 if ( response->response == ISCSI_RESPONSE_COMMAND_COMPLETE ) {
203 iscsi_done ( iscsi, 0 );
205 iscsi_done ( iscsi, -EIO );
210 * Receive data segment of an iSCSI data-in PDU
212 * @v iscsi iSCSI session
213 * @v data Received data
214 * @v len Length of received data
215 * @v remaining Data remaining after this data
218 static void iscsi_rx_data_in ( struct iscsi_session *iscsi, void *data,
219 size_t len, size_t remaining __unused ) {
220 struct iscsi_bhs_data_in *data_in = &iscsi->rx_bhs.data_in;
221 unsigned long offset;
223 /* Copy data to data-in buffer */
224 offset = ntohl ( data_in->offset ) + iscsi->rx_offset;
225 assert ( iscsi->command != NULL );
226 assert ( iscsi->command->data_in );
227 assert ( ( offset + len ) <= iscsi->command->data_in_len );
228 copy_to_user ( iscsi->command->data_in, offset, data, len );
230 /* Record SCSI status, if present */
231 if ( data_in->flags & ISCSI_DATA_FLAG_STATUS )
232 iscsi->command->status = data_in->status;
234 /* If this is the end, flag as complete */
235 if ( ( offset + len ) == iscsi->command->data_in_len ) {
236 assert ( data_in->flags & ISCSI_FLAG_FINAL );
237 assert ( remaining == 0 );
238 iscsi_done ( iscsi, 0 );
243 * Receive data segment of an iSCSI R2T PDU
245 * @v iscsi iSCSI session
246 * @v data Received data
247 * @v len Length of received data
248 * @v remaining Data remaining after this data
251 static void iscsi_rx_r2t ( struct iscsi_session *iscsi, void *data __unused,
252 size_t len __unused, size_t remaining __unused ) {
253 struct iscsi_bhs_r2t *r2t = &iscsi->rx_bhs.r2t;
255 /* Record transfer parameters and trigger first data-out */
256 iscsi->ttt = ntohl ( r2t->ttt );
257 iscsi->transfer_offset = ntohl ( r2t->offset );
258 iscsi->transfer_len = ntohl ( r2t->len );
259 iscsi_start_data_out ( iscsi, 0 );
263 * Build iSCSI data-out BHS
265 * @v iscsi iSCSI session
266 * @v datasn Data sequence number within the transfer
269 static void iscsi_start_data_out ( struct iscsi_session *iscsi,
270 unsigned int datasn ) {
271 struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
272 unsigned long offset;
273 unsigned long remaining;
276 /* We always send 512-byte Data-Out PDUs; this removes the
277 * need to worry about the target's MaxRecvDataSegmentLength.
279 offset = datasn * 512;
280 remaining = iscsi->transfer_len - offset;
285 /* Construct BHS and initiate transmission */
286 iscsi_start_tx ( iscsi );
287 data_out->opcode = ISCSI_OPCODE_DATA_OUT;
288 if ( len == remaining )
289 data_out->flags = ( ISCSI_FLAG_FINAL );
290 ISCSI_SET_LENGTHS ( data_out->lengths, 0, len );
291 data_out->lun = iscsi->lun;
292 data_out->itt = htonl ( iscsi->itt );
293 data_out->ttt = htonl ( iscsi->ttt );
294 data_out->expstatsn = htonl ( iscsi->statsn + 1 );
295 data_out->datasn = htonl ( datasn );
296 data_out->offset = htonl ( iscsi->transfer_offset + offset );
297 DBGC ( iscsi, "iSCSI %p start data out DataSN %#x len %#lx\n",
298 iscsi, datasn, len );
302 * Complete iSCSI data-out PDU transmission
304 * @v iscsi iSCSI session
307 static void iscsi_data_out_done ( struct iscsi_session *iscsi ) {
308 struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
310 /* If we haven't reached the end of the sequence, start
311 * sending the next data-out PDU.
313 if ( ! ( data_out->flags & ISCSI_FLAG_FINAL ) )
314 iscsi_start_data_out ( iscsi, ntohl ( data_out->datasn ) + 1 );
318 * Send iSCSI data-out data segment
320 * @v iscsi iSCSI session
321 * @v buf Temporary data buffer
322 * @v len Length of temporary data buffer
324 static void iscsi_tx_data_out ( struct iscsi_session *iscsi,
325 void *buf, size_t len ) {
326 struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
327 unsigned long offset;
328 unsigned long remaining;
330 offset = ( iscsi->transfer_offset + ntohl ( data_out->offset ) +
332 remaining = ( iscsi->tx_len - iscsi->tx_offset );
333 assert ( iscsi->command != NULL );
334 assert ( iscsi->command->data_out );
335 assert ( ( offset + remaining ) <= iscsi->command->data_out_len );
337 if ( remaining < len )
339 copy_from_user ( buf, iscsi->command->data_out, offset, len );
341 tcp_send ( &iscsi->tcp, buf, len );
344 /****************************************************************************
351 * Version of snprintf() that accepts a signed buffer size
353 * @v buf Buffer into which to write the string
354 * @v size Size of buffer
355 * @v fmt Format string
356 * @v args Arguments corresponding to the format string
357 * @ret len Length of formatted string
359 * This is a utility function for iscsi_build_login_request_strings().
361 static int ssnprintf ( char *buf, ssize_t ssize, const char *fmt, ... ) {
365 /* Treat negative buffer size as zero buffer size */
369 /* Hand off to vsnprintf */
370 va_start ( args, fmt );
371 len = vsnprintf ( buf, ssize, fmt, args );
377 * Build iSCSI login request strings
379 * @v iscsi iSCSI session
381 * These are the initial set of strings sent in the first login
382 * request PDU. We want the following settings:
386 * MaxConnections is irrelevant; we make only one connection anyway
388 * ImmediateData is irrelevant; we never send immediate data
389 * MaxRecvDataSegmentLength=8192 (default; we don't care)
390 * MaxBurstLength=262144 (default; we don't care)
391 * FirstBurstLength=262144 (default; we don't care)
392 * DefaultTime2Wait=0 [2]
393 * DefaultTime2Retain=0 [2]
394 * MaxOutstandingR2T=1
396 * DataSequenceInOrder=Yes
397 * ErrorRecoveryLevel=0
399 * [1] InitialR2T has an OR resolution function, so the target may
400 * force us to use it. We therefore simplify our logic by always
403 * [2] These ensure that we can safely start a new task once we have
404 * reconnected after a failure, without having to manually tidy up
407 static int iscsi_build_login_request_strings ( struct iscsi_session *iscsi,
408 void *data, size_t len ) {
409 unsigned int used = 0;
412 if ( iscsi->status & ISCSI_STATUS_STRINGS_SECURITY ) {
413 used += ssnprintf ( data + used, len - used,
416 "SessionType=Normal%c"
417 "AuthMethod=CHAP,None%c",
418 iscsi->initiator_iqn, 0,
419 iscsi->target_iqn, 0, 0, 0 );
422 if ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_ALGORITHM ) {
423 used += ssnprintf ( data + used, len - used, "CHAP_A=5%c", 0 );
426 if ( ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_RESPONSE ) &&
428 used += ssnprintf ( data + used, len - used,
429 "CHAP_N=%s%cCHAP_R=0x",
430 iscsi->username, 0 );
431 for ( i = 0 ; i < iscsi->chap.response_len ; i++ ) {
432 used += ssnprintf ( data + used, len - used, "%02x",
433 iscsi->chap.response[i] );
435 used += ssnprintf ( data + used, len - used, "%c", 0 );
438 if ( iscsi->status & ISCSI_STATUS_STRINGS_OPERATIONAL ) {
439 used += ssnprintf ( data + used, len - used,
440 "HeaderDigest=None%c"
443 "DefaultTime2Wait=0%c"
444 "DefaultTime2Retain=0%c"
445 "MaxOutstandingR2T=1%c"
446 "DataPDUInOrder=Yes%c"
447 "DataSequenceInOrder=Yes%c"
448 "ErrorRecoveryLevel=0%c",
449 0, 0, 0, 0, 0, 0, 0, 0, 0 );
456 * Build iSCSI login request BHS
458 * @v iscsi iSCSI session
460 static void iscsi_start_login ( struct iscsi_session *iscsi ) {
461 struct iscsi_bhs_login_request *request = &iscsi->tx_bhs.login_request;
464 /* Construct BHS and initiate transmission */
465 iscsi_start_tx ( iscsi );
466 request->opcode = ( ISCSI_OPCODE_LOGIN_REQUEST |
467 ISCSI_FLAG_IMMEDIATE );
468 request->flags = ( ( iscsi->status & ISCSI_STATUS_PHASE_MASK ) |
469 ISCSI_LOGIN_FLAG_TRANSITION );
470 /* version_max and version_min left as zero */
471 len = iscsi_build_login_request_strings ( iscsi, NULL, 0 );
472 ISCSI_SET_LENGTHS ( request->lengths, 0, len );
473 request->isid_iana_en = htonl ( ISCSI_ISID_IANA |
474 IANA_EN_FEN_SYSTEMS );
475 /* isid_iana_qual left as zero */
476 request->tsih = htons ( iscsi->tsih );
477 request->itt = htonl ( iscsi->itt );
478 /* cid left as zero */
479 request->cmdsn = htonl ( iscsi->cmdsn );
480 request->expstatsn = htonl ( iscsi->statsn + 1 );
484 * Complete iSCSI login request PDU transmission
486 * @v iscsi iSCSI session
489 static void iscsi_login_request_done ( struct iscsi_session *iscsi ) {
491 /* Clear any "strings to send" flags */
492 iscsi->status &= ~ISCSI_STATUS_STRINGS_MASK;
494 /* Free any dynamically allocated storage used for login */
495 chap_finish ( &iscsi->chap );
499 * Transmit data segment of an iSCSI login request PDU
501 * @v iscsi iSCSI session
502 * @v buf Temporary data buffer
503 * @v len Length of temporary data buffer
505 * For login requests, the data segment consists of the login strings.
507 static void iscsi_tx_login_request ( struct iscsi_session *iscsi,
508 void *buf, size_t len ) {
509 len = iscsi_build_login_request_strings ( iscsi, buf, len );
510 tcp_send ( &iscsi->tcp, buf + iscsi->tx_offset,
511 len - iscsi->tx_offset );
515 * Handle iSCSI TargetAddress text value
517 * @v iscsi iSCSI session
518 * @v value TargetAddress value
520 static void iscsi_handle_targetaddress_value ( struct iscsi_session *iscsi,
521 const char *value ) {
522 struct in_addr address;
523 struct sockaddr_in *sin = ( struct sockaddr_in * ) &iscsi->target;
525 if ( inet_aton ( value, &address ) == 0 ) {
526 DBGC ( iscsi, "iSCSI %p received invalid TargetAddress "
527 "\"%s\"\n", iscsi, value );
531 DBGC ( iscsi, "iSCSI %p will redirect to %s\n", iscsi, value );
532 sin->sin_addr = address;
536 * Handle iSCSI AuthMethod text value
538 * @v iscsi iSCSI session
539 * @v value AuthMethod value
541 static void iscsi_handle_authmethod_value ( struct iscsi_session *iscsi,
542 const char *value ) {
544 /* If server requests CHAP, send the CHAP_A string */
545 if ( strcmp ( value, "CHAP" ) == 0 ) {
546 DBGC ( iscsi, "iSCSI %p initiating CHAP authentication\n",
548 iscsi->status |= ISCSI_STATUS_STRINGS_CHAP_ALGORITHM;
553 * Handle iSCSI CHAP_A text value
555 * @v iscsi iSCSI session
556 * @v value CHAP_A value
558 static void iscsi_handle_chap_a_value ( struct iscsi_session *iscsi,
559 const char *value ) {
562 /* We only ever offer "5" (i.e. MD5) as an algorithm, so if
563 * the server responds with anything else it is a protocol
566 if ( strcmp ( value, "5" ) != 0 ) {
567 DBGC ( iscsi, "iSCSI %p got invalid CHAP algorithm \"%s\"\n",
571 /* Prepare for CHAP with MD5 */
572 if ( ( rc = chap_init ( &iscsi->chap, &md5_algorithm ) ) != 0 ) {
573 DBGC ( iscsi, "iSCSI %p could not initialise CHAP: %s\n",
574 iscsi, strerror ( rc ) );
575 iscsi_close ( iscsi );
576 iscsi_done ( iscsi, rc );
581 * Handle iSCSI CHAP_I text value
583 * @v iscsi iSCSI session
584 * @v value CHAP_I value
586 static void iscsi_handle_chap_i_value ( struct iscsi_session *iscsi,
587 const char *value ) {
588 unsigned int identifier;
591 /* The CHAP identifier is an integer value */
592 identifier = strtoul ( value, &endp, 0 );
593 if ( *endp != '\0' ) {
594 DBGC ( iscsi, "iSCSI %p saw invalid CHAP identifier \"%s\"\n",
598 /* Identifier and secret are the first two components of the
601 chap_set_identifier ( &iscsi->chap, identifier );
602 if ( iscsi->password ) {
603 chap_update ( &iscsi->chap, iscsi->password,
604 strlen ( iscsi->password ) );
609 * Handle iSCSI CHAP_C text value
611 * @v iscsi iSCSI session
612 * @v value CHAP_C value
614 static void iscsi_handle_chap_c_value ( struct iscsi_session *iscsi,
615 const char *value ) {
620 /* Check and strip leading "0x" */
621 if ( ( value[0] != '0' ) || ( value[1] != 'x' ) ) {
622 DBGC ( iscsi, "iSCSI %p saw invalid CHAP challenge \"%s\"\n",
627 /* Process challenge an octet at a time */
628 for ( ; ( value[0] && value[1] ) ; value += 2 ) {
629 memcpy ( buf, value, 2 );
631 byte = strtoul ( buf, &endp, 16 );
632 if ( *endp != '\0' ) {
633 DBGC ( iscsi, "iSCSI %p saw invalid CHAP challenge "
634 "byte \"%s\"\n", iscsi, buf );
636 chap_update ( &iscsi->chap, &byte, sizeof ( byte ) );
639 /* Build CHAP response */
640 DBGC ( iscsi, "iSCSI %p sending CHAP response\n", iscsi );
641 chap_respond ( &iscsi->chap );
642 iscsi->status |= ISCSI_STATUS_STRINGS_CHAP_RESPONSE;
645 /** An iSCSI text string that we want to handle */
646 struct iscsi_string_type {
649 * This is the portion up to and including the "=" sign,
650 * e.g. "InitiatorName=", "CHAP_A=", etc.
653 /** Handle iSCSI string value
655 * @v iscsi iSCSI session
656 * @v value iSCSI string value
658 void ( * handle_value ) ( struct iscsi_session *iscsi,
662 /** iSCSI text strings that we want to handle */
663 struct iscsi_string_type iscsi_string_types[] = {
664 { "TargetAddress=", iscsi_handle_targetaddress_value },
665 { "AuthMethod=", iscsi_handle_authmethod_value },
666 { "CHAP_A=", iscsi_handle_chap_a_value },
667 { "CHAP_I=", iscsi_handle_chap_i_value },
668 { "CHAP_C=", iscsi_handle_chap_c_value },
673 * Handle iSCSI string
675 * @v iscsi iSCSI session
676 * @v string iSCSI string (in "key=value" format)
678 static void iscsi_handle_string ( struct iscsi_session *iscsi,
679 const char *string ) {
680 struct iscsi_string_type *type;
683 for ( type = iscsi_string_types ; type->key ; type++ ) {
684 key_len = strlen ( type->key );
685 if ( strncmp ( string, type->key, key_len ) == 0 ) {
686 DBGC ( iscsi, "iSCSI %p handling %s\n",
688 type->handle_value ( iscsi, ( string + key_len ) );
692 DBGC ( iscsi, "iSCSI %p ignoring %s\n", iscsi, string );
696 * Handle iSCSI strings
698 * @v iscsi iSCSI session
699 * @v string iSCSI string buffer
700 * @v len Length of string buffer
702 static void iscsi_handle_strings ( struct iscsi_session *iscsi,
703 const char *strings, size_t len ) {
706 /* Handle each string in turn, taking care not to overrun the
707 * data buffer in case of badly-terminated data.
710 string_len = ( strnlen ( strings, len ) + 1 );
711 if ( string_len > len )
713 iscsi_handle_string ( iscsi, strings );
714 strings += string_len;
720 * Receive data segment of an iSCSI login response PDU
722 * @v iscsi iSCSI session
723 * @v data Received data
724 * @v len Length of received data
725 * @v remaining Data remaining after this data
728 static void iscsi_rx_login_response ( struct iscsi_session *iscsi, void *data,
729 size_t len, size_t remaining ) {
730 struct iscsi_bhs_login_response *response
731 = &iscsi->rx_bhs.login_response;
734 /* Buffer up the PDU data */
735 if ( ( rc = iscsi_rx_buffered_data ( iscsi, data, len ) ) != 0 ) {
736 DBGC ( iscsi, "iSCSI %p could not buffer login response: %s\n",
737 iscsi, strerror ( rc ) );
738 iscsi_close ( iscsi );
739 iscsi_done ( iscsi, rc );
745 /* Process string data and discard string buffer */
746 iscsi_handle_strings ( iscsi, iscsi->rx_buffer, iscsi->rx_len );
747 iscsi_rx_buffered_data_done ( iscsi );
749 /* Check for login redirection */
750 if ( response->status_class == ISCSI_STATUS_REDIRECT ) {
751 DBGC ( iscsi, "iSCSI %p redirecting to new server\n", iscsi );
752 iscsi_close ( iscsi );
753 if ( ( rc = tcp_connect ( &iscsi->tcp, &iscsi->target,
755 DBGC ( iscsi, "iSCSI %p could not open TCP "
756 "connection: %s\n", iscsi, strerror ( rc ) );
757 iscsi_done ( iscsi, rc );
762 /* Check for fatal errors */
763 if ( response->status_class != 0 ) {
764 DBGC ( iscsi, "iSCSI login failure: class %02x detail %02x\n",
765 response->status_class, response->status_detail );
766 iscsi->instant_rc = -EPERM;
767 iscsi_close ( iscsi );
768 iscsi_done ( iscsi, -EPERM );
772 /* Handle login transitions */
773 if ( response->flags & ISCSI_LOGIN_FLAG_TRANSITION ) {
774 switch ( response->flags & ISCSI_LOGIN_NSG_MASK ) {
775 case ISCSI_LOGIN_NSG_OPERATIONAL_NEGOTIATION:
777 ( ISCSI_STATUS_OPERATIONAL_NEGOTIATION_PHASE |
778 ISCSI_STATUS_STRINGS_OPERATIONAL );
780 case ISCSI_LOGIN_NSG_FULL_FEATURE_PHASE:
781 iscsi->status = ISCSI_STATUS_FULL_FEATURE_PHASE;
784 DBGC ( iscsi, "iSCSI %p got invalid response flags "
785 "%02x\n", iscsi, response->flags );
786 iscsi_close ( iscsi );
787 iscsi_done ( iscsi, -EIO );
792 /* Send next login request PDU if we haven't reached the full
795 if ( ( iscsi->status & ISCSI_STATUS_PHASE_MASK ) !=
796 ISCSI_STATUS_FULL_FEATURE_PHASE ) {
797 iscsi_start_login ( iscsi );
801 /* Reset retry count */
802 iscsi->retry_count = 0;
804 /* Record TSIH for future reference */
805 iscsi->tsih = ntohl ( response->tsih );
807 /* Send the actual SCSI command */
808 iscsi_start_command ( iscsi );
811 /****************************************************************************
813 * iSCSI to TCP interface
817 static inline struct iscsi_session *
818 tcp_to_iscsi ( struct tcp_application *app ) {
819 return container_of ( app, struct iscsi_session, tcp );
823 * Start up a new TX PDU
825 * @v iscsi iSCSI session
827 * This initiates the process of sending a new PDU. Only one PDU may
828 * be in transit at any one time.
830 static void iscsi_start_tx ( struct iscsi_session *iscsi ) {
831 assert ( iscsi->tx_state == ISCSI_TX_IDLE );
833 /* Initialise TX BHS */
834 memset ( &iscsi->tx_bhs, 0, sizeof ( iscsi->tx_bhs ) );
836 /* Flag TX engine to start transmitting */
837 iscsi->tx_state = ISCSI_TX_BHS;
838 iscsi->tx_offset = 0;
842 * Transmit data segment of an iSCSI PDU
844 * @v iscsi iSCSI session
845 * @v buf Temporary data buffer
846 * @v len Length of temporary data buffer
848 * Handle transmission of part of a PDU data segment. iscsi::tx_bhs
849 * will be valid when this is called.
851 static void iscsi_tx_data ( struct iscsi_session *iscsi,
852 void *buf, size_t len ) {
853 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
855 switch ( common->opcode & ISCSI_OPCODE_MASK ) {
856 case ISCSI_OPCODE_DATA_OUT:
857 iscsi_tx_data_out ( iscsi, buf, len );
859 case ISCSI_OPCODE_LOGIN_REQUEST:
860 iscsi_tx_login_request ( iscsi, buf, len );
869 * Complete iSCSI PDU transmission
871 * @v iscsi iSCSI session
873 * Called when a PDU has been completely transmitted and the TX state
874 * machine is about to enter the idle state. iscsi::tx_bhs will be
875 * valid for the just-completed PDU when this is called.
877 static void iscsi_tx_done ( struct iscsi_session *iscsi ) {
878 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
880 switch ( common->opcode & ISCSI_OPCODE_MASK ) {
881 case ISCSI_OPCODE_DATA_OUT:
882 iscsi_data_out_done ( iscsi );
883 case ISCSI_OPCODE_LOGIN_REQUEST:
884 iscsi_login_request_done ( iscsi );
894 * @v iscsi iSCSI session
896 * Updates iscsi->tx_offset and, if applicable, transitions to the
899 static void iscsi_acked ( struct tcp_application *app, size_t len ) {
900 struct iscsi_session *iscsi = tcp_to_iscsi ( app );
901 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
902 enum iscsi_tx_state next_state;
904 iscsi->tx_offset += len;
906 switch ( iscsi->tx_state ) {
908 iscsi->tx_len = sizeof ( iscsi->tx_bhs );
909 next_state = ISCSI_TX_AHS;
912 iscsi->tx_len = 4 * ISCSI_AHS_LEN ( common->lengths );
913 next_state = ISCSI_TX_DATA;
916 iscsi->tx_len = ISCSI_DATA_LEN ( common->lengths );
917 next_state = ISCSI_TX_DATA_PADDING;
919 case ISCSI_TX_DATA_PADDING:
920 iscsi->tx_len = ISCSI_DATA_PAD_LEN ( common->lengths );
921 next_state = ISCSI_TX_IDLE;
929 assert ( iscsi->tx_offset <= iscsi->tx_len );
931 /* If the whole of the current portion has not yet
932 * been acked, stay in this state for now.
934 if ( iscsi->tx_offset != iscsi->tx_len )
937 /* Move to next state. Call iscsi_tx_done() when PDU
938 * transmission is complete.
940 iscsi->tx_state = next_state;
941 iscsi->tx_offset = 0;
942 if ( next_state == ISCSI_TX_IDLE )
943 iscsi_tx_done ( iscsi );
950 * @v iscsi iSCSI session
951 * @v buf Temporary data buffer
952 * @v len Length of temporary data buffer
954 * Constructs data to be sent for the current TX state
956 static void iscsi_senddata ( struct tcp_application *app,
957 void *buf, size_t len ) {
958 struct iscsi_session *iscsi = tcp_to_iscsi ( app );
959 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
960 static const char pad[] = { '\0', '\0', '\0' };
962 switch ( iscsi->tx_state ) {
964 /* Nothing to send */
967 tcp_send ( app, &iscsi->tx_bhs.bytes[iscsi->tx_offset],
968 ( sizeof ( iscsi->tx_bhs ) - iscsi->tx_offset ) );
971 /* We don't yet have an AHS transmission mechanism */
975 iscsi_tx_data ( iscsi, buf, len );
977 case ISCSI_TX_DATA_PADDING:
978 tcp_send ( app, pad, ( ISCSI_DATA_PAD_LEN ( common->lengths )
979 - iscsi->tx_offset ) );
988 * Receive data segment of an iSCSI PDU
990 * @v iscsi iSCSI session
991 * @v data Received data
992 * @v len Length of received data
993 * @v remaining Data remaining after this data
995 * Handle processing of part of a PDU data segment. iscsi::rx_bhs
996 * will be valid when this is called.
998 static void iscsi_rx_data ( struct iscsi_session *iscsi, void *data,
999 size_t len, size_t remaining ) {
1000 struct iscsi_bhs_common_response *response
1001 = &iscsi->rx_bhs.common_response;
1003 /* Update cmdsn and statsn */
1004 iscsi->cmdsn = ntohl ( response->expcmdsn );
1005 iscsi->statsn = ntohl ( response->statsn );
1007 switch ( response->opcode & ISCSI_OPCODE_MASK ) {
1008 case ISCSI_OPCODE_LOGIN_RESPONSE:
1009 iscsi_rx_login_response ( iscsi, data, len, remaining );
1011 case ISCSI_OPCODE_SCSI_RESPONSE:
1012 iscsi_rx_scsi_response ( iscsi, data, len, remaining );
1014 case ISCSI_OPCODE_DATA_IN:
1015 iscsi_rx_data_in ( iscsi, data, len, remaining );
1017 case ISCSI_OPCODE_R2T:
1018 iscsi_rx_r2t ( iscsi, data, len, remaining );
1023 DBGC ( iscsi, "iSCSI %p unknown opcode %02x\n", iscsi,
1025 iscsi_close ( iscsi );
1026 iscsi_done ( iscsi, -EOPNOTSUPP );
1032 * Discard portion of an iSCSI PDU.
1034 * @v iscsi iSCSI session
1035 * @v data Received data
1036 * @v len Length of received data
1037 * @v remaining Data remaining after this data
1039 * This discards data from a portion of a received PDU.
1041 static void iscsi_rx_discard ( struct iscsi_session *iscsi __unused,
1042 void *data __unused, size_t len __unused,
1043 size_t remaining __unused ) {
1048 * Receive basic header segment of an iSCSI PDU
1050 * @v iscsi iSCSI session
1051 * @v data Received data
1052 * @v len Length of received data
1053 * @v remaining Data remaining after this data
1055 * This fills in iscsi::rx_bhs with the data from the BHS portion of
1058 static void iscsi_rx_bhs ( struct iscsi_session *iscsi, void *data,
1059 size_t len, size_t remaining __unused ) {
1060 memcpy ( &iscsi->rx_bhs.bytes[iscsi->rx_offset], data, len );
1061 if ( ( iscsi->rx_offset + len ) >= sizeof ( iscsi->rx_bhs ) ) {
1062 DBGC ( iscsi, "iSCSI %p received PDU opcode %#x len %#lx\n",
1063 iscsi, iscsi->rx_bhs.common.opcode,
1064 ISCSI_DATA_LEN ( iscsi->rx_bhs.common.lengths ) );
1071 * @v tcp TCP application
1072 * @v data Received data
1073 * @v len Length of received data
1075 * This handles received PDUs. The receive strategy is to fill in
1076 * iscsi::rx_bhs with the contents of the BHS portion of the PDU,
1077 * throw away any AHS portion, and then process each part of the data
1078 * portion as it arrives. The data processing routine therefore
1079 * always has a full copy of the BHS available, even for portions of
1080 * the data in different packets to the BHS.
1082 static void iscsi_newdata ( struct tcp_application *app, void *data,
1084 struct iscsi_session *iscsi = tcp_to_iscsi ( app );
1085 struct iscsi_bhs_common *common = &iscsi->rx_bhs.common;
1086 void ( *process ) ( struct iscsi_session *iscsi, void *data,
1087 size_t len, size_t remaining );
1088 enum iscsi_rx_state next_state;
1093 switch ( iscsi->rx_state ) {
1095 process = iscsi_rx_bhs;
1096 iscsi->rx_len = sizeof ( iscsi->rx_bhs );
1097 next_state = ISCSI_RX_AHS;
1100 process = iscsi_rx_discard;
1101 iscsi->rx_len = 4 * ISCSI_AHS_LEN ( common->lengths );
1102 next_state = ISCSI_RX_DATA;
1105 process = iscsi_rx_data;
1106 iscsi->rx_len = ISCSI_DATA_LEN ( common->lengths );
1107 next_state = ISCSI_RX_DATA_PADDING;
1109 case ISCSI_RX_DATA_PADDING:
1110 process = iscsi_rx_discard;
1111 iscsi->rx_len = ISCSI_DATA_PAD_LEN ( common->lengths );
1112 next_state = ISCSI_RX_BHS;
1119 frag_len = iscsi->rx_len - iscsi->rx_offset;
1120 if ( frag_len > len )
1122 remaining = iscsi->rx_len - iscsi->rx_offset - frag_len;
1123 process ( iscsi, data, frag_len, remaining );
1125 iscsi->rx_offset += frag_len;
1129 /* If all the data for this state has not yet been
1130 * received, stay in this state for now.
1132 if ( iscsi->rx_offset != iscsi->rx_len )
1135 iscsi->rx_state = next_state;
1136 iscsi->rx_offset = 0;
1141 * Handle TCP connection closure
1143 * @v app TCP application
1144 * @v status Error code, if any
1147 static void iscsi_closed ( struct tcp_application *app, int status ) {
1148 struct iscsi_session *iscsi = tcp_to_iscsi ( app );
1151 /* Even a graceful close counts as an error for iSCSI */
1153 status = -ECONNRESET;
1155 /* Close session cleanly */
1156 iscsi_close ( iscsi );
1158 /* Retry connection if within the retry limit, otherwise fail */
1159 if ( ++iscsi->retry_count <= ISCSI_MAX_RETRIES ) {
1160 DBGC ( iscsi, "iSCSI %p retrying connection (retry #%d)\n",
1161 iscsi, iscsi->retry_count );
1162 if ( ( rc = tcp_connect ( app, &iscsi->target, 0 ) ) != 0 ) {
1163 DBGC ( iscsi, "iSCSI %p could not open TCP "
1164 "connection: %s\n", iscsi, strerror ( rc ) );
1165 iscsi_done ( iscsi, rc );
1168 DBGC ( iscsi, "iSCSI %p retry count exceeded\n", iscsi );
1169 iscsi->instant_rc = status;
1170 iscsi_done ( iscsi, status );
1175 * Handle TCP connection opening
1177 * @v app TCP application
1180 static void iscsi_connected ( struct tcp_application *app ) {
1181 struct iscsi_session *iscsi = tcp_to_iscsi ( app );
1183 assert ( iscsi->rx_state == ISCSI_RX_BHS );
1184 assert ( iscsi->rx_offset == 0 );
1186 /* Enter security negotiation phase */
1187 iscsi->status = ( ISCSI_STATUS_SECURITY_NEGOTIATION_PHASE |
1188 ISCSI_STATUS_STRINGS_SECURITY );
1190 /* Assign fresh initiator task tag */
1193 /* Start logging in */
1194 iscsi_start_login ( iscsi );
1197 /** iSCSI TCP operations */
1198 static struct tcp_operations iscsi_tcp_operations = {
1199 .closed = iscsi_closed,
1200 .connected = iscsi_connected,
1201 .acked = iscsi_acked,
1202 .newdata = iscsi_newdata,
1203 .senddata = iscsi_senddata,
1207 * Issue SCSI command via iSCSI session
1209 * @v iscsi iSCSI session
1210 * @v command SCSI command
1211 * @v parent Parent asynchronous operation
1212 * @ret rc Return status code
1214 int iscsi_issue ( struct iscsi_session *iscsi, struct scsi_command *command,
1215 struct async *parent ) {
1218 assert ( iscsi->command == NULL );
1219 iscsi->command = command;
1221 if ( iscsi->instant_rc ) {
1222 /* Abort immediately rather than retrying */
1223 return iscsi->instant_rc;
1224 } else if ( iscsi->status ) {
1225 /* Session already open: issue command */
1226 iscsi_start_command ( iscsi );
1227 tcp_senddata ( &iscsi->tcp );
1229 /* Session not open: initiate login */
1230 iscsi->tcp.tcp_op = &iscsi_tcp_operations;
1231 if ( ( rc = tcp_connect ( &iscsi->tcp, &iscsi->target,
1233 DBGC ( iscsi, "iSCSI %p could not open TCP "
1234 "connection: %s\n", iscsi, strerror ( rc ) );
1239 async_init ( &iscsi->async, &default_async_operations, parent );
1244 * Close down iSCSI session
1246 * @v iscsi iSCSI session
1247 * @ret aop Asynchronous operation
1249 void iscsi_shutdown ( struct iscsi_session *iscsi ) {
1250 iscsi_close ( iscsi );