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 );
108 * Mark iSCSI operation as complete, and close TCP connection
110 * @v iscsi iSCSI session
111 * @v rc Return status code
113 static void iscsi_close ( struct iscsi_session *iscsi, int rc ) {
115 /* Clear session status */
118 /* Close TCP connection */
119 tcp_close ( &iscsi->tcp );
121 /* Mark iSCSI operation as complete */
122 iscsi_done ( iscsi, rc );
125 /****************************************************************************
127 * iSCSI SCSI command issuing
132 * Build iSCSI SCSI command BHS
134 * @v iscsi iSCSI session
136 * We don't currently support bidirectional commands (i.e. with both
137 * Data-In and Data-Out segments); these would require providing code
138 * to generate an AHS, and there doesn't seem to be any need for it at
141 static void iscsi_start_command ( struct iscsi_session *iscsi ) {
142 struct iscsi_bhs_scsi_command *command = &iscsi->tx_bhs.scsi_command;
144 assert ( ! ( iscsi->command->data_in && iscsi->command->data_out ) );
146 /* Construct BHS and initiate transmission */
147 iscsi_start_tx ( iscsi );
148 command->opcode = ISCSI_OPCODE_SCSI_COMMAND;
149 command->flags = ( ISCSI_FLAG_FINAL |
150 ISCSI_COMMAND_ATTR_SIMPLE );
151 if ( iscsi->command->data_in )
152 command->flags |= ISCSI_COMMAND_FLAG_READ;
153 if ( iscsi->command->data_out )
154 command->flags |= ISCSI_COMMAND_FLAG_WRITE;
155 /* lengths left as zero */
156 command->lun = iscsi->lun;
157 command->itt = htonl ( ++iscsi->itt );
158 command->exp_len = htonl ( iscsi->command->data_in_len |
159 iscsi->command->data_out_len );
160 command->cmdsn = htonl ( iscsi->cmdsn );
161 command->expstatsn = htonl ( iscsi->statsn + 1 );
162 memcpy ( &command->cdb, &iscsi->command->cdb, sizeof ( command->cdb ));
163 DBG ( "iSCSI %p start " SCSI_CDB_FORMAT " %s %#x\n",
164 iscsi, SCSI_CDB_DATA ( command->cdb ),
165 ( iscsi->command->data_in ? "in" : "out" ),
166 ( iscsi->command->data_in ?
167 iscsi->command->data_in_len : iscsi->command->data_out_len ) );
171 * Receive data segment of an iSCSI SCSI response PDU
173 * @v iscsi iSCSI session
174 * @v data Received data
175 * @v len Length of received data
176 * @v remaining Data remaining after this data
179 static void iscsi_rx_scsi_response ( struct iscsi_session *iscsi, void *data,
180 size_t len, size_t remaining ) {
181 struct iscsi_bhs_scsi_response *response
182 = &iscsi->rx_bhs.scsi_response;
185 /* Capture the sense response code as it floats past, if present */
186 sense_offset = ISCSI_SENSE_RESPONSE_CODE_OFFSET - iscsi->rx_offset;
187 if ( ( sense_offset >= 0 ) && len ) {
188 iscsi->command->sense_response =
189 * ( ( char * ) data + sense_offset );
192 /* Wait for whole SCSI response to arrive */
196 /* Record SCSI status code */
197 iscsi->command->status = response->status;
199 /* Mark as completed, with error if applicable */
200 if ( response->response == ISCSI_RESPONSE_COMMAND_COMPLETE ) {
201 iscsi_done ( iscsi, 0 );
203 iscsi_done ( iscsi, -EIO );
208 * Receive data segment of an iSCSI data-in PDU
210 * @v iscsi iSCSI session
211 * @v data Received data
212 * @v len Length of received data
213 * @v remaining Data remaining after this data
216 static void iscsi_rx_data_in ( struct iscsi_session *iscsi, void *data,
217 size_t len, size_t remaining __unused ) {
218 struct iscsi_bhs_data_in *data_in = &iscsi->rx_bhs.data_in;
219 unsigned long offset;
221 /* Copy data to data-in buffer */
222 offset = ntohl ( data_in->offset ) + iscsi->rx_offset;
223 assert ( iscsi->command != NULL );
224 assert ( iscsi->command->data_in );
225 assert ( ( offset + len ) <= iscsi->command->data_in_len );
226 copy_to_user ( iscsi->command->data_in, offset, data, len );
228 /* Record SCSI status, if present */
229 if ( data_in->flags & ISCSI_DATA_FLAG_STATUS )
230 iscsi->command->status = data_in->status;
232 /* If this is the end, flag as complete */
233 if ( ( offset + len ) == iscsi->command->data_in_len ) {
234 assert ( data_in->flags & ISCSI_FLAG_FINAL );
235 assert ( remaining == 0 );
236 iscsi_done ( iscsi, 0 );
241 * Receive data segment of an iSCSI R2T PDU
243 * @v iscsi iSCSI session
244 * @v data Received data
245 * @v len Length of received data
246 * @v remaining Data remaining after this data
249 static void iscsi_rx_r2t ( struct iscsi_session *iscsi, void *data __unused,
250 size_t len __unused, size_t remaining __unused ) {
251 struct iscsi_bhs_r2t *r2t = &iscsi->rx_bhs.r2t;
253 /* Record transfer parameters and trigger first data-out */
254 iscsi->ttt = ntohl ( r2t->ttt );
255 iscsi->transfer_offset = ntohl ( r2t->offset );
256 iscsi->transfer_len = ntohl ( r2t->len );
257 iscsi_start_data_out ( iscsi, 0 );
261 * Build iSCSI data-out BHS
263 * @v iscsi iSCSI session
264 * @v datasn Data sequence number within the transfer
267 static void iscsi_start_data_out ( struct iscsi_session *iscsi,
268 unsigned int datasn ) {
269 struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
270 unsigned long offset;
271 unsigned long remaining;
274 /* We always send 512-byte Data-Out PDUs; this removes the
275 * need to worry about the target's MaxRecvDataSegmentLength.
277 offset = datasn * 512;
278 remaining = iscsi->transfer_len - offset;
283 /* Construct BHS and initiate transmission */
284 iscsi_start_tx ( iscsi );
285 data_out->opcode = ISCSI_OPCODE_DATA_OUT;
286 if ( len == remaining )
287 data_out->flags = ( ISCSI_FLAG_FINAL );
288 ISCSI_SET_LENGTHS ( data_out->lengths, 0, len );
289 data_out->lun = iscsi->lun;
290 data_out->itt = htonl ( iscsi->itt );
291 data_out->ttt = htonl ( iscsi->ttt );
292 data_out->expstatsn = htonl ( iscsi->statsn + 1 );
293 data_out->datasn = htonl ( datasn );
294 data_out->offset = htonl ( iscsi->transfer_offset + offset );
295 DBG ( "iSCSI %p start data out DataSN %#x len %#lx\n",
296 iscsi, datasn, len );
300 * Complete iSCSI data-out PDU transmission
302 * @v iscsi iSCSI session
305 static void iscsi_data_out_done ( struct iscsi_session *iscsi ) {
306 struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
308 /* If we haven't reached the end of the sequence, start
309 * sending the next data-out PDU.
311 if ( ! ( data_out->flags & ISCSI_FLAG_FINAL ) )
312 iscsi_start_data_out ( iscsi, ntohl ( data_out->datasn ) + 1 );
316 * Send iSCSI data-out data segment
318 * @v iscsi iSCSI session
319 * @v buf Temporary data buffer
320 * @v len Length of temporary data buffer
322 static void iscsi_tx_data_out ( struct iscsi_session *iscsi,
323 void *buf, size_t len ) {
324 struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
325 unsigned long offset;
326 unsigned long remaining;
328 offset = ( iscsi->transfer_offset + ntohl ( data_out->offset ) +
330 remaining = ( iscsi->tx_len - iscsi->tx_offset );
331 assert ( iscsi->command != NULL );
332 assert ( iscsi->command->data_out );
333 assert ( ( offset + remaining ) <= iscsi->command->data_out_len );
335 if ( remaining < len )
337 copy_from_user ( buf, iscsi->command->data_out, offset, len );
339 tcp_send ( &iscsi->tcp, buf, len );
342 /****************************************************************************
349 * Version of snprintf() that accepts a signed buffer size
351 * @v buf Buffer into which to write the string
352 * @v size Size of buffer
353 * @v fmt Format string
354 * @v args Arguments corresponding to the format string
355 * @ret len Length of formatted string
357 * This is a utility function for iscsi_build_login_request_strings().
359 static int ssnprintf ( char *buf, ssize_t ssize, const char *fmt, ... ) {
363 /* Treat negative buffer size as zero buffer size */
367 /* Hand off to vsnprintf */
368 va_start ( args, fmt );
369 len = vsnprintf ( buf, ssize, fmt, args );
375 * Build iSCSI login request strings
377 * @v iscsi iSCSI session
379 * These are the initial set of strings sent in the first login
380 * request PDU. We want the following settings:
384 * MaxConnections is irrelevant; we make only one connection anyway
386 * ImmediateData is irrelevant; we never send immediate data
387 * MaxRecvDataSegmentLength=8192 (default; we don't care)
388 * MaxBurstLength=262144 (default; we don't care)
389 * FirstBurstLength=262144 (default; we don't care)
390 * DefaultTime2Wait=0 [2]
391 * DefaultTime2Retain=0 [2]
392 * MaxOutstandingR2T=1
394 * DataSequenceInOrder=Yes
395 * ErrorRecoveryLevel=0
397 * [1] InitialR2T has an OR resolution function, so the target may
398 * force us to use it. We therefore simplify our logic by always
401 * [2] These ensure that we can safely start a new task once we have
402 * reconnected after a failure, without having to manually tidy up
405 static int iscsi_build_login_request_strings ( struct iscsi_session *iscsi,
406 void *data, size_t len ) {
407 unsigned int used = 0;
410 if ( iscsi->status & ISCSI_STATUS_STRINGS_SECURITY ) {
411 used += ssnprintf ( data + used, len - used,
414 "SessionType=Normal%c"
415 "AuthMethod=CHAP,None%c",
416 iscsi->initiator, 0, iscsi->target, 0,
420 if ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_ALGORITHM ) {
421 used += ssnprintf ( data + used, len - used, "CHAP_A=5%c", 0 );
424 if ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_RESPONSE ) {
425 used += ssnprintf ( data + used, len - used,
426 "CHAP_N=%s%cCHAP_R=0x",
427 iscsi->username, 0 );
428 for ( i = 0 ; i < iscsi->chap.response_len ; i++ ) {
429 used += ssnprintf ( data + used, len - used, "%02x",
430 iscsi->chap.response[i] );
432 used += ssnprintf ( data + used, len - used, "%c", 0 );
435 if ( iscsi->status & ISCSI_STATUS_STRINGS_OPERATIONAL ) {
436 used += ssnprintf ( data + used, len - used,
437 "HeaderDigest=None%c"
440 "DefaultTime2Wait=0%c"
441 "DefaultTime2Retain=0%c"
442 "MaxOutstandingR2T=1%c"
443 "DataPDUInOrder=Yes%c"
444 "DataSequenceInOrder=Yes%c"
445 "ErrorRecoveryLevel=0%c",
446 0, 0, 0, 0, 0, 0, 0, 0, 0 );
453 * Build iSCSI login request BHS
455 * @v iscsi iSCSI session
457 static void iscsi_start_login ( struct iscsi_session *iscsi ) {
458 struct iscsi_bhs_login_request *request = &iscsi->tx_bhs.login_request;
461 /* Construct BHS and initiate transmission */
462 iscsi_start_tx ( iscsi );
463 request->opcode = ( ISCSI_OPCODE_LOGIN_REQUEST |
464 ISCSI_FLAG_IMMEDIATE );
465 request->flags = ( ( iscsi->status & ISCSI_STATUS_PHASE_MASK ) |
466 ISCSI_LOGIN_FLAG_TRANSITION );
467 /* version_max and version_min left as zero */
468 len = iscsi_build_login_request_strings ( iscsi, NULL, 0 );
469 ISCSI_SET_LENGTHS ( request->lengths, 0, len );
470 request->isid_iana_en = htonl ( ISCSI_ISID_IANA |
471 IANA_EN_FEN_SYSTEMS );
472 /* isid_iana_qual left as zero */
473 request->tsih = htons ( iscsi->tsih );
474 request->itt = htonl ( iscsi->itt );
475 /* cid left as zero */
476 request->cmdsn = htonl ( iscsi->cmdsn );
477 request->expstatsn = htonl ( iscsi->statsn + 1 );
481 * Complete iSCSI login request PDU transmission
483 * @v iscsi iSCSI session
486 static void iscsi_login_request_done ( struct iscsi_session *iscsi ) {
488 /* Clear any "strings to send" flags */
489 iscsi->status &= ~ISCSI_STATUS_STRINGS_MASK;
493 * Transmit data segment of an iSCSI login request PDU
495 * @v iscsi iSCSI session
496 * @v buf Temporary data buffer
497 * @v len Length of temporary data buffer
499 * For login requests, the data segment consists of the login strings.
501 static void iscsi_tx_login_request ( struct iscsi_session *iscsi,
502 void *buf, size_t len ) {
503 len = iscsi_build_login_request_strings ( iscsi, buf, len );
504 tcp_send ( &iscsi->tcp, buf + iscsi->tx_offset,
505 len - iscsi->tx_offset );
509 * Handle iSCSI TargetAddress text value
511 * @v iscsi iSCSI session
512 * @v value TargetAddress value
514 static void iscsi_handle_targetaddress_value ( struct iscsi_session *iscsi,
515 const char *value ) {
516 struct in_addr address;
517 struct sockaddr_in *sin = ( struct sockaddr_in * ) &iscsi->tcp.peer;
519 if ( inet_aton ( value, &address ) == 0 ) {
520 DBG ( "iSCSI %p received invalid TargetAddress \"%s\"\n",
525 DBG ( "iSCSI %p will redirect to %s\n", iscsi, value );
526 sin->sin_addr = address;
530 * Handle iSCSI AuthMethod text value
532 * @v iscsi iSCSI session
533 * @v value AuthMethod value
535 static void iscsi_handle_authmethod_value ( struct iscsi_session *iscsi,
536 const char *value ) {
538 /* If server requests CHAP, send the CHAP_A string */
539 if ( strcmp ( value, "CHAP" ) == 0 ) {
540 DBG ( "iSCSI %p initiating CHAP authentication\n", iscsi );
541 iscsi->status |= ISCSI_STATUS_STRINGS_CHAP_ALGORITHM;
546 * Handle iSCSI CHAP_A text value
548 * @v iscsi iSCSI session
549 * @v value CHAP_A value
551 static void iscsi_handle_chap_a_value ( struct iscsi_session *iscsi,
552 const char *value ) {
555 /* We only ever offer "5" (i.e. MD5) as an algorithm, so if
556 * the server responds with anything else it is a protocol
559 if ( strcmp ( value, "5" ) != 0 ) {
560 DBG ( "iSCSI %p got invalid CHAP algorithm \"%s\"\n",
564 /* Prepare for CHAP with MD5 */
565 if ( ( rc = chap_init ( &iscsi->chap, &md5_algorithm ) ) != 0 ) {
566 DBG ( "iSCSI %p could not initialise CHAP\n", iscsi );
567 iscsi_close ( iscsi, rc );
572 * Handle iSCSI CHAP_I text value
574 * @v iscsi iSCSI session
575 * @v value CHAP_I value
577 static void iscsi_handle_chap_i_value ( struct iscsi_session *iscsi,
578 const char *value ) {
579 unsigned int identifier;
582 /* The CHAP identifier is an integer value */
583 identifier = strtoul ( value, &endp, 0 );
584 if ( *endp != '\0' ) {
585 DBG ( "iSCSI %p saw invalid CHAP identifier \"%s\"\n",
589 /* Identifier and secret are the first two components of the
592 chap_set_identifier ( &iscsi->chap, identifier );
593 chap_update ( &iscsi->chap, iscsi->password,
594 strlen ( iscsi->password ) );
598 * Handle iSCSI CHAP_C text value
600 * @v iscsi iSCSI session
601 * @v value CHAP_C value
603 static void iscsi_handle_chap_c_value ( struct iscsi_session *iscsi,
604 const char *value ) {
609 /* Check and strip leading "0x" */
610 if ( ( value[0] != '0' ) || ( value[1] != 'x' ) ) {
611 DBG ( "iSCSI %p saw invalid CHAP challenge \"%s\"\n",
616 /* Process challenge an octet at a time */
617 for ( ; ( value[0] && value[1] ) ; value += 2 ) {
618 memcpy ( buf, value, 2 );
620 byte = strtoul ( buf, &endp, 16 );
621 if ( *endp != '\0' ) {
622 DBG ( "iSCSI %p saw invalid CHAP challenge byte "
623 "\"%s\"\n", iscsi, buf );
625 chap_update ( &iscsi->chap, &byte, sizeof ( byte ) );
628 /* Build CHAP response */
629 DBG ( "iSCSI %p sending CHAP response\n", iscsi );
630 chap_respond ( &iscsi->chap );
631 iscsi->status |= ISCSI_STATUS_STRINGS_CHAP_RESPONSE;
634 /** An iSCSI text string that we want to handle */
635 struct iscsi_string_type {
638 * This is the portion up to and including the "=" sign,
639 * e.g. "InitiatorName=", "CHAP_A=", etc.
642 /** Handle iSCSI string value
644 * @v iscsi iSCSI session
645 * @v value iSCSI string value
647 void ( * handle_value ) ( struct iscsi_session *iscsi,
651 /** iSCSI text strings that we want to handle */
652 struct iscsi_string_type iscsi_string_types[] = {
653 { "TargetAddress=", iscsi_handle_targetaddress_value },
654 { "AuthMethod=", iscsi_handle_authmethod_value },
655 { "CHAP_A=", iscsi_handle_chap_a_value },
656 { "CHAP_I=", iscsi_handle_chap_i_value },
657 { "CHAP_C=", iscsi_handle_chap_c_value },
662 * Handle iSCSI string
664 * @v iscsi iSCSI session
665 * @v string iSCSI string (in "key=value" format)
667 static void iscsi_handle_string ( struct iscsi_session *iscsi,
668 const char *string ) {
669 struct iscsi_string_type *type;
672 for ( type = iscsi_string_types ; type->key ; type++ ) {
673 key_len = strlen ( type->key );
674 if ( strncmp ( string, type->key, key_len ) == 0 ) {
675 DBG ( "iSCSI %p handling %s\n", iscsi, string );
676 type->handle_value ( iscsi, ( string + key_len ) );
680 DBG ( "iSCSI %p ignoring %s\n", iscsi, string );
684 * Handle iSCSI strings
686 * @v iscsi iSCSI session
687 * @v string iSCSI string buffer
688 * @v len Length of string buffer
690 static void iscsi_handle_strings ( struct iscsi_session *iscsi,
691 const char *strings, size_t len ) {
694 /* Handle each string in turn, taking care not to overrun the
695 * data buffer in case of badly-terminated data.
698 string_len = ( strnlen ( strings, len ) + 1 );
699 if ( string_len > len )
701 iscsi_handle_string ( iscsi, strings );
702 strings += string_len;
708 * Receive data segment of an iSCSI login response PDU
710 * @v iscsi iSCSI session
711 * @v data Received data
712 * @v len Length of received data
713 * @v remaining Data remaining after this data
716 static void iscsi_rx_login_response ( struct iscsi_session *iscsi, void *data,
717 size_t len, size_t remaining ) {
718 struct iscsi_bhs_login_response *response
719 = &iscsi->rx_bhs.login_response;
722 /* Buffer up the PDU data */
723 if ( ( rc = iscsi_rx_buffered_data ( iscsi, data, len ) ) != 0 ) {
724 DBG ( "iSCSI %p could not buffer login response\n", iscsi );
725 iscsi_close ( iscsi, rc );
731 /* Process string data and discard string buffer */
732 iscsi_handle_strings ( iscsi, iscsi->rx_buffer, iscsi->rx_len );
733 iscsi_rx_buffered_data_done ( iscsi );
735 /* Check for login redirection */
736 if ( response->status_class == ISCSI_STATUS_REDIRECT ) {
737 DBG ( "iSCSI %p redirecting to new server\n", iscsi );
738 iscsi_close ( iscsi, -EINPROGRESS );
739 tcp_connect ( &iscsi->tcp );
743 /* Check for fatal errors */
744 if ( response->status_class != 0 ) {
745 printf ( "iSCSI login failure: class %02x detail %02x\n",
746 response->status_class, response->status_detail );
747 iscsi_close ( iscsi, -EPERM );
751 /* Handle login transitions */
752 if ( response->flags & ISCSI_LOGIN_FLAG_TRANSITION ) {
753 switch ( response->flags & ISCSI_LOGIN_NSG_MASK ) {
754 case ISCSI_LOGIN_NSG_OPERATIONAL_NEGOTIATION:
756 ( ISCSI_STATUS_OPERATIONAL_NEGOTIATION_PHASE |
757 ISCSI_STATUS_STRINGS_OPERATIONAL );
759 case ISCSI_LOGIN_NSG_FULL_FEATURE_PHASE:
760 iscsi->status = ISCSI_STATUS_FULL_FEATURE_PHASE;
763 DBG ( "iSCSI %p got invalid response flags %02x\n",
764 iscsi, response->flags );
765 iscsi_close ( iscsi, -EIO );
770 /* Send next login request PDU if we haven't reached the full
773 if ( ( iscsi->status & ISCSI_STATUS_PHASE_MASK ) !=
774 ISCSI_STATUS_FULL_FEATURE_PHASE ) {
775 iscsi_start_login ( iscsi );
779 /* Record TSIH for future reference */
780 iscsi->tsih = ntohl ( response->tsih );
782 /* Send the actual SCSI command */
783 iscsi_start_command ( iscsi );
786 /****************************************************************************
788 * iSCSI to TCP interface
792 static inline struct iscsi_session *
793 tcp_to_iscsi ( struct tcp_connection *conn ) {
794 return container_of ( conn, struct iscsi_session, tcp );
798 * Start up a new TX PDU
800 * @v iscsi iSCSI session
802 * This initiates the process of sending a new PDU. Only one PDU may
803 * be in transit at any one time.
805 static void iscsi_start_tx ( struct iscsi_session *iscsi ) {
806 assert ( iscsi->tx_state == ISCSI_TX_IDLE );
808 /* Initialise TX BHS */
809 memset ( &iscsi->tx_bhs, 0, sizeof ( iscsi->tx_bhs ) );
811 /* Flag TX engine to start transmitting */
812 iscsi->tx_state = ISCSI_TX_BHS;
813 iscsi->tx_offset = 0;
817 * Transmit data segment of an iSCSI PDU
819 * @v iscsi iSCSI session
820 * @v buf Temporary data buffer
821 * @v len Length of temporary data buffer
823 * Handle transmission of part of a PDU data segment. iscsi::tx_bhs
824 * will be valid when this is called.
826 static void iscsi_tx_data ( struct iscsi_session *iscsi,
827 void *buf, size_t len ) {
828 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
830 switch ( common->opcode & ISCSI_OPCODE_MASK ) {
831 case ISCSI_OPCODE_DATA_OUT:
832 iscsi_tx_data_out ( iscsi, buf, len );
834 case ISCSI_OPCODE_LOGIN_REQUEST:
835 iscsi_tx_login_request ( iscsi, buf, len );
844 * Complete iSCSI PDU transmission
846 * @v iscsi iSCSI session
848 * Called when a PDU has been completely transmitted and the TX state
849 * machine is about to enter the idle state. iscsi::tx_bhs will be
850 * valid for the just-completed PDU when this is called.
852 static void iscsi_tx_done ( struct iscsi_session *iscsi ) {
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_data_out_done ( iscsi );
858 case ISCSI_OPCODE_LOGIN_REQUEST:
859 iscsi_login_request_done ( iscsi );
869 * @v iscsi iSCSI session
871 * Updates iscsi->tx_offset and, if applicable, transitions to the
874 static void iscsi_acked ( struct tcp_connection *conn, size_t len ) {
875 struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
876 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
877 enum iscsi_tx_state next_state;
879 iscsi->tx_offset += len;
881 switch ( iscsi->tx_state ) {
883 iscsi->tx_len = sizeof ( iscsi->tx_bhs );
884 next_state = ISCSI_TX_AHS;
887 iscsi->tx_len = 4 * ISCSI_AHS_LEN ( common->lengths );
888 next_state = ISCSI_TX_DATA;
891 iscsi->tx_len = ISCSI_DATA_LEN ( common->lengths );
892 next_state = ISCSI_TX_DATA_PADDING;
894 case ISCSI_TX_DATA_PADDING:
895 iscsi->tx_len = ISCSI_DATA_PAD_LEN ( common->lengths );
896 next_state = ISCSI_TX_IDLE;
904 assert ( iscsi->tx_offset <= iscsi->tx_len );
906 /* If the whole of the current portion has not yet
907 * been acked, stay in this state for now.
909 if ( iscsi->tx_offset != iscsi->tx_len )
912 /* Move to next state. Call iscsi_tx_done() when PDU
913 * transmission is complete.
915 iscsi->tx_state = next_state;
916 iscsi->tx_offset = 0;
917 if ( next_state == ISCSI_TX_IDLE )
918 iscsi_tx_done ( iscsi );
925 * @v iscsi iSCSI session
926 * @v buf Temporary data buffer
927 * @v len Length of temporary data buffer
929 * Constructs data to be sent for the current TX state
931 static void iscsi_senddata ( struct tcp_connection *conn,
932 void *buf, size_t len ) {
933 struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
934 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
935 static const char pad[] = { '\0', '\0', '\0' };
937 switch ( iscsi->tx_state ) {
939 /* Nothing to send */
942 tcp_send ( conn, &iscsi->tx_bhs.bytes[iscsi->tx_offset],
943 ( sizeof ( iscsi->tx_bhs ) - iscsi->tx_offset ) );
946 /* We don't yet have an AHS transmission mechanism */
950 iscsi_tx_data ( iscsi, buf, len );
952 case ISCSI_TX_DATA_PADDING:
953 tcp_send ( conn, pad, ( ISCSI_DATA_PAD_LEN ( common->lengths )
954 - iscsi->tx_offset ) );
963 * Receive data segment of an iSCSI PDU
965 * @v iscsi iSCSI session
966 * @v data Received data
967 * @v len Length of received data
968 * @v remaining Data remaining after this data
970 * Handle processing of part of a PDU data segment. iscsi::rx_bhs
971 * will be valid when this is called.
973 static void iscsi_rx_data ( struct iscsi_session *iscsi, void *data,
974 size_t len, size_t remaining ) {
975 struct iscsi_bhs_common_response *response
976 = &iscsi->rx_bhs.common_response;
978 /* Update cmdsn and statsn */
979 iscsi->cmdsn = ntohl ( response->expcmdsn );
980 iscsi->statsn = ntohl ( response->statsn );
982 switch ( response->opcode & ISCSI_OPCODE_MASK ) {
983 case ISCSI_OPCODE_LOGIN_RESPONSE:
984 iscsi_rx_login_response ( iscsi, data, len, remaining );
986 case ISCSI_OPCODE_SCSI_RESPONSE:
987 iscsi_rx_scsi_response ( iscsi, data, len, remaining );
989 case ISCSI_OPCODE_DATA_IN:
990 iscsi_rx_data_in ( iscsi, data, len, remaining );
992 case ISCSI_OPCODE_R2T:
993 iscsi_rx_r2t ( iscsi, data, len, remaining );
998 printf ( "Unknown iSCSI opcode %02x\n", response->opcode );
999 iscsi_done ( iscsi, -EOPNOTSUPP );
1005 * Discard portion of an iSCSI PDU.
1007 * @v iscsi iSCSI session
1008 * @v data Received data
1009 * @v len Length of received data
1010 * @v remaining Data remaining after this data
1012 * This discards data from a portion of a received PDU.
1014 static void iscsi_rx_discard ( struct iscsi_session *iscsi __unused,
1015 void *data __unused, size_t len __unused,
1016 size_t remaining __unused ) {
1021 * Receive basic header segment of an iSCSI PDU
1023 * @v iscsi iSCSI session
1024 * @v data Received data
1025 * @v len Length of received data
1026 * @v remaining Data remaining after this data
1028 * This fills in iscsi::rx_bhs with the data from the BHS portion of
1031 static void iscsi_rx_bhs ( struct iscsi_session *iscsi, void *data,
1032 size_t len, size_t remaining __unused ) {
1033 memcpy ( &iscsi->rx_bhs.bytes[iscsi->rx_offset], data, len );
1034 if ( ( iscsi->rx_offset + len ) >= sizeof ( iscsi->rx_bhs ) ) {
1035 DBG ( "iSCSI %p received PDU opcode %#x len %#lx\n",
1036 iscsi, iscsi->rx_bhs.common.opcode,
1037 ISCSI_DATA_LEN ( iscsi->rx_bhs.common.lengths ) );
1044 * @v tcp TCP connection
1045 * @v data Received data
1046 * @v len Length of received data
1048 * This handles received PDUs. The receive strategy is to fill in
1049 * iscsi::rx_bhs with the contents of the BHS portion of the PDU,
1050 * throw away any AHS portion, and then process each part of the data
1051 * portion as it arrives. The data processing routine therefore
1052 * always has a full copy of the BHS available, even for portions of
1053 * the data in different packets to the BHS.
1055 static void iscsi_newdata ( struct tcp_connection *conn, void *data,
1057 struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
1058 struct iscsi_bhs_common *common = &iscsi->rx_bhs.common;
1059 void ( *process ) ( struct iscsi_session *iscsi, void *data,
1060 size_t len, size_t remaining );
1061 enum iscsi_rx_state next_state;
1066 switch ( iscsi->rx_state ) {
1068 process = iscsi_rx_bhs;
1069 iscsi->rx_len = sizeof ( iscsi->rx_bhs );
1070 next_state = ISCSI_RX_AHS;
1073 process = iscsi_rx_discard;
1074 iscsi->rx_len = 4 * ISCSI_AHS_LEN ( common->lengths );
1075 next_state = ISCSI_RX_DATA;
1078 process = iscsi_rx_data;
1079 iscsi->rx_len = ISCSI_DATA_LEN ( common->lengths );
1080 next_state = ISCSI_RX_DATA_PADDING;
1082 case ISCSI_RX_DATA_PADDING:
1083 process = iscsi_rx_discard;
1084 iscsi->rx_len = ISCSI_DATA_PAD_LEN ( common->lengths );
1085 next_state = ISCSI_RX_BHS;
1092 frag_len = iscsi->rx_len - iscsi->rx_offset;
1093 if ( frag_len > len )
1095 remaining = iscsi->rx_len - iscsi->rx_offset - frag_len;
1096 process ( iscsi, data, frag_len, remaining );
1098 iscsi->rx_offset += frag_len;
1102 /* If all the data for this state has not yet been
1103 * received, stay in this state for now.
1105 if ( iscsi->rx_offset != iscsi->rx_len )
1108 iscsi->rx_state = next_state;
1109 iscsi->rx_offset = 0;
1114 * Handle TCP connection closure
1116 * @v conn TCP connection
1117 * @v status Error code, if any
1120 static void iscsi_closed ( struct tcp_connection *conn, int status ) {
1121 struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
1123 /* Clear session status */
1126 /* Retry connection if within the retry limit, otherwise fail */
1127 if ( ++iscsi->retry_count <= ISCSI_MAX_RETRIES ) {
1128 tcp_connect ( conn );
1130 printf ( "iSCSI retry count exceeded\n" );
1131 iscsi_done ( iscsi, status );
1136 * Handle TCP connection opening
1138 * @v conn TCP connection
1141 static void iscsi_connected ( struct tcp_connection *conn ) {
1142 struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
1144 /* Set connected flag and reset retry count */
1145 iscsi->status = ( ISCSI_STATUS_SECURITY_NEGOTIATION_PHASE |
1146 ISCSI_STATUS_STRINGS_SECURITY );
1147 iscsi->retry_count = 0;
1149 /* Prepare to receive PDUs. */
1150 iscsi->rx_state = ISCSI_RX_BHS;
1151 iscsi->rx_offset = 0;
1153 /* Assign fresh initiator task tag */
1156 /* Start logging in */
1157 iscsi_start_login ( iscsi );
1160 /** iSCSI TCP operations */
1161 static struct tcp_operations iscsi_tcp_operations = {
1162 .closed = iscsi_closed,
1163 .connected = iscsi_connected,
1164 .acked = iscsi_acked,
1165 .newdata = iscsi_newdata,
1166 .senddata = iscsi_senddata,
1170 * Issue SCSI command via iSCSI session
1172 * @v iscsi iSCSI session
1173 * @v command SCSI command
1174 * @ret aop Asynchronous operation for this SCSI command
1176 struct async_operation * iscsi_issue ( struct iscsi_session *iscsi,
1177 struct scsi_command *command ) {
1178 assert ( iscsi->command == NULL );
1179 iscsi->command = command;
1181 if ( iscsi->status ) {
1182 iscsi_start_command ( iscsi );
1183 tcp_senddata ( &iscsi->tcp );
1185 iscsi->tcp.tcp_op = &iscsi_tcp_operations;
1186 tcp_connect ( &iscsi->tcp );