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.
25 #include <gpxe/scsi.h>
26 #include <gpxe/process.h>
27 #include <gpxe/uaccess.h>
28 #include <gpxe/iscsi.h>
36 static void iscsi_start_tx ( struct iscsi_session *iscsi );
37 static void iscsi_start_data_out ( struct iscsi_session *iscsi,
38 unsigned int datasn );
40 /****************************************************************************
42 * iSCSI SCSI command issuing
47 * Build iSCSI SCSI command BHS
49 * @v iscsi iSCSI session
51 * We don't currently support bidirectional commands (i.e. with both
52 * Data-In and Data-Out segments); these would require providing code
53 * to generate an AHS, and there doesn't seem to be any need for it at
56 static void iscsi_start_command ( struct iscsi_session *iscsi ) {
57 struct iscsi_bhs_scsi_command *command = &iscsi->tx_bhs.scsi_command;
59 assert ( ! ( iscsi->command->data_in && iscsi->command->data_out ) );
61 /* Construct BHS and initiate transmission */
62 iscsi_start_tx ( iscsi );
63 command->opcode = ISCSI_OPCODE_SCSI_COMMAND;
64 command->flags = ( ISCSI_FLAG_FINAL |
65 ISCSI_COMMAND_ATTR_SIMPLE );
66 if ( iscsi->command->data_in )
67 command->flags |= ISCSI_COMMAND_FLAG_READ;
68 if ( iscsi->command->data_out )
69 command->flags |= ISCSI_COMMAND_FLAG_WRITE;
70 /* lengths left as zero */
71 command->lun = iscsi->lun;
72 command->itt = htonl ( ++iscsi->itt );
73 command->exp_len = htonl ( iscsi->command->data_in_len |
74 iscsi->command->data_out_len );
75 command->cmdsn = htonl ( iscsi->cmdsn );
76 command->expstatsn = htonl ( iscsi->statsn + 1 );
77 memcpy ( &command->cdb, &iscsi->command->cdb, sizeof ( command->cdb ));
81 * Receive data segment of an iSCSI SCSI response PDU
83 * @v iscsi iSCSI session
84 * @v data Received data
85 * @v len Length of received data
86 * @v remaining Data remaining after this data
89 static void iscsi_rx_scsi_response ( struct iscsi_session *iscsi, void *data,
90 size_t len, size_t remaining ) {
91 struct iscsi_bhs_scsi_response *response
92 = &iscsi->rx_bhs.scsi_response;
95 /* Capture the sense response code as it floats past, if present */
96 sense_offset = ISCSI_SENSE_RESPONSE_CODE_OFFSET - iscsi->rx_offset;
97 if ( ( sense_offset >= 0 ) && len ) {
98 iscsi->command->sense_response =
99 * ( ( char * ) data + sense_offset );
102 /* Wait for whole SCSI response to arrive */
106 /* Record SCSI status code */
107 iscsi->command->status = response->status;
109 /* Mark as completed, with error if applicable */
110 iscsi->status |= ISCSI_STATUS_DONE;
111 if ( response->response != ISCSI_RESPONSE_COMMAND_COMPLETE )
112 iscsi->status |= ISCSI_STATUS_ERR;
116 * Receive data segment of an iSCSI data-in PDU
118 * @v iscsi iSCSI session
119 * @v data Received data
120 * @v len Length of received data
121 * @v remaining Data remaining after this data
124 static void iscsi_rx_data_in ( struct iscsi_session *iscsi, void *data,
125 size_t len, size_t remaining __unused ) {
126 struct iscsi_bhs_data_in *data_in = &iscsi->rx_bhs.data_in;
127 unsigned long offset;
129 /* Copy data to data-in buffer */
130 offset = ntohl ( data_in->offset ) + iscsi->rx_offset;
131 assert ( iscsi->command != NULL );
132 assert ( iscsi->command->data_in != NULL );
133 assert ( ( offset + len ) <= iscsi->command->data_in_len );
134 copy_to_user ( iscsi->command->data_in, offset, data, len );
136 /* Record SCSI status, if present */
137 if ( data_in->flags & ISCSI_DATA_FLAG_STATUS )
138 iscsi->command->status = data_in->status;
140 /* If this is the end, flag as complete */
141 if ( ( offset + len ) == iscsi->command->data_in_len ) {
142 assert ( data_in->flags & ISCSI_FLAG_FINAL );
143 assert ( remaining == 0 );
144 iscsi->status |= ISCSI_STATUS_DONE;
149 * Receive data segment of an iSCSI R2T PDU
151 * @v iscsi iSCSI session
152 * @v data Received data
153 * @v len Length of received data
154 * @v remaining Data remaining after this data
157 static void iscsi_rx_r2t ( struct iscsi_session *iscsi, void *data __unused,
158 size_t len __unused, size_t remaining __unused ) {
159 struct iscsi_bhs_r2t *r2t = &iscsi->rx_bhs.r2t;
161 /* Record transfer parameters and trigger first data-out */
162 iscsi->ttt = ntohl ( r2t->ttt );
163 iscsi->transfer_offset = ntohl ( r2t->offset );
164 iscsi->transfer_len = ntohl ( r2t->len );
165 iscsi_start_data_out ( iscsi, 0 );
169 * Build iSCSI data-out BHS
171 * @v iscsi iSCSI session
172 * @v datasn Data sequence number within the transfer
175 static void iscsi_start_data_out ( struct iscsi_session *iscsi,
176 unsigned int datasn ) {
177 struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
178 unsigned long offset;
179 unsigned long remaining;
182 /* We always send 512-byte Data-Out PDUs; this removes the
183 * need to worry about the target's MaxRecvDataSegmentLength.
185 offset = datasn * 512;
186 remaining = iscsi->transfer_len - offset;
191 /* Construct BHS and initiate transmission */
192 iscsi_start_tx ( iscsi );
193 data_out->opcode = ISCSI_OPCODE_DATA_OUT;
194 if ( len == remaining )
195 data_out->flags = ( ISCSI_FLAG_FINAL );
196 ISCSI_SET_LENGTHS ( data_out->lengths, 0, len );
197 data_out->lun = iscsi->lun;
198 data_out->itt = htonl ( iscsi->itt );
199 data_out->ttt = htonl ( iscsi->ttt );
200 data_out->expstatsn = htonl ( iscsi->statsn + 1 );
201 data_out->datasn = htonl ( datasn );
202 data_out->offset = htonl ( iscsi->transfer_offset + offset );
206 * Complete iSCSI data-out PDU transmission
208 * @v iscsi iSCSI session
211 static void iscsi_data_out_done ( struct iscsi_session *iscsi ) {
212 struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
214 /* If we haven't reached the end of the sequence, start
215 * sending the next data-out PDU.
217 if ( ! ( data_out->flags & ISCSI_FLAG_FINAL ) )
218 iscsi_start_data_out ( iscsi, ntohl ( data_out->datasn ) + 1 );
222 * Send iSCSI data-out data segment
224 * @v iscsi iSCSI session
226 static void iscsi_tx_data_out ( struct iscsi_session *iscsi ) {
227 struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
228 unsigned long offset;
231 offset = ( iscsi->transfer_offset + ntohl ( data_out->offset ) +
233 len = ( ISCSI_DATA_LEN ( data_out->lengths ) - iscsi->tx_offset );
234 assert ( iscsi->command != NULL );
235 assert ( iscsi->command->data_out != NULL );
236 assert ( ( offset + len ) <= iscsi->command->data_out_len );
238 if ( len > tcp_buflen )
240 copy_from_user ( tcp_buffer, iscsi->command->data_out, offset, len );
242 tcp_send ( &iscsi->tcp, tcp_buffer, len );
245 /****************************************************************************
252 * Build iSCSI login request strings
254 * @v iscsi iSCSI session
256 * These are the initial set of strings sent in the first login
257 * request PDU. We want the following settings:
261 * MaxConnections is irrelevant; we make only one connection anyway
262 * InitialR2T=Yes (default) [1]
263 * ImmediateData is irrelevant; we never send immediate data
264 * MaxRecvDataSegmentLength=8192 (default)
265 * MaxBurstLength=262144 (default)
266 * FirstBurstLength=262144 (default)
267 * DefaultTime2Wait=0 [2]
268 * DefaultTime2Retain=0 [2]
269 * MaxOutstandingR2T=1 (default)
270 * DataPDUInOrder=Yes (default)
271 * DataSequenceInOrder=Yes (default)
272 * ErrorRecoveryLevel=0 (default)
274 * [1] InitialR2T has an OR resolution function, so the target may
275 * force us to use it. We therefore simplify our logic by always
278 * [2] These ensure that we can safely start a new task once we have
279 * reconnected after a failure, without having to manually tidy up
282 static int iscsi_build_login_request_strings ( struct iscsi_session *iscsi,
283 void *data, size_t len ) {
284 return snprintf ( data, len,
287 "SessionType=Normal%c"
289 "HeaderDigest=None%c"
290 "DefaultTime2Wait=0%c"
291 "DefaultTime2Retain=0%c",
292 iscsi->initiator, 0, iscsi->target, 0,
297 * Build iSCSI login request BHS
299 * @v iscsi iSCSI session
300 * @v first Login request is the first in a sequence
302 static void iscsi_start_login ( struct iscsi_session *iscsi, int first ) {
303 struct iscsi_bhs_login_request *request = &iscsi->tx_bhs.login_request;
306 /* Construct BHS and initiate transmission */
307 iscsi_start_tx ( iscsi );
308 request->opcode = ( ISCSI_OPCODE_LOGIN_REQUEST |
309 ISCSI_FLAG_IMMEDIATE );
310 request->flags = ( ISCSI_LOGIN_FLAG_TRANSITION |
311 ISCSI_LOGIN_CSG_OPERATIONAL_NEGOTIATION |
312 ISCSI_LOGIN_NSG_FULL_FEATURE_PHASE );
313 /* version_max and version_min left as zero */
315 len = iscsi_build_login_request_strings ( iscsi, NULL, 0 );
316 ISCSI_SET_LENGTHS ( request->lengths, 0, len );
318 request->isid_iana_en = htonl ( ISCSI_ISID_IANA |
319 IANA_EN_FEN_SYSTEMS );
320 /* isid_iana_qual left as zero */
321 request->tsih = htons ( iscsi->tsih );
324 request->itt = htonl ( iscsi->itt );
325 /* cid left as zero */
326 request->cmdsn = htonl ( iscsi->cmdsn );
327 request->expstatsn = htonl ( iscsi->statsn + 1 );
331 * Transmit data segment of an iSCSI login request PDU
333 * @v iscsi iSCSI session
335 * For login requests, the data segment consists of the login strings.
337 static void iscsi_tx_login_request ( struct iscsi_session *iscsi ) {
340 len = iscsi_build_login_request_strings ( iscsi, tcp_buffer,
342 tcp_send ( &iscsi->tcp, tcp_buffer + iscsi->tx_offset,
343 len - iscsi->tx_offset );
347 * Receive data segment of an iSCSI login response PDU
349 * @v iscsi iSCSI session
350 * @v data Received data
351 * @v len Length of received data
352 * @v remaining Data remaining after this data
355 static void iscsi_rx_login_response ( struct iscsi_session *iscsi,
358 size_t remaining __unused ) {
359 struct iscsi_bhs_login_response *response
360 = &iscsi->rx_bhs.login_response;
362 /* Check for fatal errors */
363 if ( response->status_class != 0 ) {
364 printf ( "iSCSI login failure: class %02x detail %02x\n",
365 response->status_class, response->status_detail );
366 iscsi->status |= ( ISCSI_STATUS_DONE | ISCSI_STATUS_ERR );
367 tcp_close ( &iscsi->tcp );
371 /* If server did not transition, send back another login
372 * request without any login strings.
374 if ( ! ( response->flags & ISCSI_LOGIN_FLAG_TRANSITION ) ) {
375 iscsi_start_login ( iscsi, 0 );
379 /* Record TSIH for future reference */
380 iscsi->tsih = ntohl ( response->tsih );
382 /* Send the SCSI command */
383 iscsi_start_command ( iscsi );
386 /****************************************************************************
388 * iSCSI to TCP interface
392 static inline struct iscsi_session *
393 tcp_to_iscsi ( struct tcp_connection *conn ) {
394 return container_of ( conn, struct iscsi_session, tcp );
398 * Start up a new TX PDU
400 * @v iscsi iSCSI session
402 * This initiates the process of sending a new PDU. Only one PDU may
403 * be in transit at any one time.
405 static void iscsi_start_tx ( struct iscsi_session *iscsi ) {
406 assert ( iscsi->tx_state == ISCSI_TX_IDLE );
408 /* Initialise TX BHS */
409 memset ( &iscsi->tx_bhs, 0, sizeof ( iscsi->tx_bhs ) );
411 /* Flag TX engine to start transmitting */
412 iscsi->tx_state = ISCSI_TX_BHS;
413 iscsi->tx_offset = 0;
417 * Transmit data segment of an iSCSI PDU
419 * @v iscsi iSCSI session
421 * Handle transmission of part of a PDU data segment. iscsi::tx_bhs
422 * will be valid when this is called.
424 static void iscsi_tx_data ( struct iscsi_session *iscsi ) {
425 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
427 switch ( common->opcode & ISCSI_OPCODE_MASK ) {
428 case ISCSI_OPCODE_DATA_OUT:
429 iscsi_tx_data_out ( iscsi );
431 case ISCSI_OPCODE_LOGIN_REQUEST:
432 iscsi_tx_login_request ( iscsi );
441 * Complete iSCSI PDU transmission
443 * @v iscsi iSCSI session
445 * Called when a PDU has been completely transmitted and the TX state
446 * machine is about to enter the idle state. iscsi::tx_bhs will be
447 * valid for the just-completed PDU when this is called.
449 static void iscsi_tx_done ( struct iscsi_session *iscsi ) {
450 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
452 switch ( common->opcode & ISCSI_OPCODE_MASK ) {
453 case ISCSI_OPCODE_DATA_OUT:
454 iscsi_data_out_done ( iscsi );
464 * @v iscsi iSCSI session
466 * Updates iscsi->tx_offset and, if applicable, transitions to the
469 static void iscsi_acked ( struct tcp_connection *conn, size_t len ) {
470 struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
471 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
472 size_t max_tx_offset;
473 enum iscsi_tx_state next_state;
475 iscsi->tx_offset += len;
477 switch ( iscsi->tx_state ) {
479 max_tx_offset = sizeof ( iscsi->tx_bhs );
480 next_state = ISCSI_TX_AHS;
483 max_tx_offset = 4 * ISCSI_AHS_LEN ( common->lengths );
484 next_state = ISCSI_TX_DATA;
487 max_tx_offset = ISCSI_DATA_LEN ( common->lengths );
488 next_state = ISCSI_TX_DATA_PADDING;
490 case ISCSI_TX_DATA_PADDING:
491 max_tx_offset = ISCSI_DATA_PAD_LEN ( common->lengths );
492 next_state = ISCSI_TX_IDLE;
500 assert ( iscsi->tx_offset <= max_tx_offset );
502 /* If the whole of the current portion has not yet
503 * been acked, stay in this state for now.
505 if ( iscsi->tx_offset != max_tx_offset )
508 /* Move to next state. Call iscsi_tx_done() when PDU
509 * transmission is complete.
511 iscsi->tx_state = next_state;
512 iscsi->tx_offset = 0;
513 if ( next_state == ISCSI_TX_IDLE )
514 iscsi_tx_done ( iscsi );
521 * @v iscsi iSCSI session
523 * Constructs data to be sent for the current TX state
525 static void iscsi_senddata ( struct tcp_connection *conn ) {
526 struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
527 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
528 static const char pad[] = { '\0', '\0', '\0' };
530 switch ( iscsi->tx_state ) {
532 /* Nothing to send */
535 tcp_send ( conn, &iscsi->tx_bhs.bytes[iscsi->tx_offset],
536 ( sizeof ( iscsi->tx_bhs ) - iscsi->tx_offset ) );
539 /* We don't yet have an AHS transmission mechanism */
543 iscsi_tx_data ( iscsi );
545 case ISCSI_TX_DATA_PADDING:
546 tcp_send ( conn, pad, ( ISCSI_DATA_PAD_LEN ( common->lengths )
547 - iscsi->tx_offset ) );
556 * Receive data segment of an iSCSI PDU
558 * @v iscsi iSCSI session
559 * @v data Received data
560 * @v len Length of received data
561 * @v remaining Data remaining after this data
563 * Handle processing of part of a PDU data segment. iscsi::rx_bhs
564 * will be valid when this is called.
566 static void iscsi_rx_data ( struct iscsi_session *iscsi, void *data,
567 size_t len, size_t remaining ) {
568 struct iscsi_bhs_common_response *response
569 = &iscsi->rx_bhs.common_response;
571 /* Update cmdsn and statsn */
572 iscsi->cmdsn = ntohl ( response->expcmdsn );
573 iscsi->statsn = ntohl ( response->statsn );
575 switch ( response->opcode & ISCSI_OPCODE_MASK ) {
576 case ISCSI_OPCODE_LOGIN_RESPONSE:
577 iscsi_rx_login_response ( iscsi, data, len, remaining );
579 case ISCSI_OPCODE_SCSI_RESPONSE:
580 iscsi_rx_scsi_response ( iscsi, data, len, remaining );
582 case ISCSI_OPCODE_DATA_IN:
583 iscsi_rx_data_in ( iscsi, data, len, remaining );
585 case ISCSI_OPCODE_R2T:
586 iscsi_rx_r2t ( iscsi, data, len, remaining );
589 printf ( "Unknown iSCSI opcode %02x\n", response->opcode );
590 iscsi->status |= ( ISCSI_STATUS_DONE | ISCSI_STATUS_ERR );
596 * Discard portion of an iSCSI PDU.
598 * @v iscsi iSCSI session
599 * @v data Received data
600 * @v len Length of received data
601 * @v remaining Data remaining after this data
603 * This discards data from a portion of a received PDU.
605 static void iscsi_rx_discard ( struct iscsi_session *iscsi __unused,
606 void *data __unused, size_t len __unused,
607 size_t remaining __unused ) {
612 * Receive basic header segment of an iSCSI PDU
614 * @v iscsi iSCSI session
615 * @v data Received data
616 * @v len Length of received data
617 * @v remaining Data remaining after this data
619 * This fills in iscsi::rx_bhs with the data from the BHS portion of
622 static void iscsi_rx_bhs ( struct iscsi_session *iscsi, void *data,
623 size_t len, size_t remaining __unused ) {
624 memcpy ( &iscsi->rx_bhs.bytes[iscsi->rx_offset], data, len );
630 * @v tcp TCP connection
631 * @v data Received data
632 * @v len Length of received data
634 * This handles received PDUs. The receive strategy is to fill in
635 * iscsi::rx_bhs with the contents of the BHS portion of the PDU,
636 * throw away any AHS portion, and then process each part of the data
637 * portion as it arrives. The data processing routine therefore
638 * always has a full copy of the BHS available, even for portions of
639 * the data in different packets to the BHS.
641 static void iscsi_newdata ( struct tcp_connection *conn, void *data,
643 struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
644 struct iscsi_bhs_common *common = &iscsi->rx_bhs.common;
645 void ( *process ) ( struct iscsi_session *iscsi, void *data,
646 size_t len, size_t remaining );
647 size_t max_rx_offset;
648 enum iscsi_rx_state next_state;
653 switch ( iscsi->rx_state ) {
655 process = iscsi_rx_bhs;
656 max_rx_offset = sizeof ( iscsi->rx_bhs );
657 next_state = ISCSI_RX_AHS;
660 process = iscsi_rx_discard;
661 max_rx_offset = 4 * ISCSI_AHS_LEN ( common->lengths );
662 next_state = ISCSI_RX_DATA;
665 process = iscsi_rx_data;
666 max_rx_offset = ISCSI_DATA_LEN ( common->lengths );
667 next_state = ISCSI_RX_DATA_PADDING;
669 case ISCSI_RX_DATA_PADDING:
670 process = iscsi_rx_discard;
671 max_rx_offset = ISCSI_DATA_PAD_LEN ( common->lengths );
672 next_state = ISCSI_RX_BHS;
679 frag_len = max_rx_offset - iscsi->rx_offset;
680 if ( frag_len > len )
682 remaining = max_rx_offset - iscsi->rx_offset - frag_len;
683 process ( iscsi, data, frag_len, remaining );
685 iscsi->rx_offset += frag_len;
689 /* If all the data for this state has not yet been
690 * received, stay in this state for now.
692 if ( iscsi->rx_offset != max_rx_offset )
695 iscsi->rx_state = next_state;
696 iscsi->rx_offset = 0;
701 * Handle TCP connection closure
703 * @v conn TCP connection
704 * @v status Error code, if any
707 static void iscsi_closed ( struct tcp_connection *conn, int status __unused ) {
708 struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
710 /* Clear connected flag */
711 iscsi->status &= ~ISCSI_STATUS_CONNECTED;
713 /* Retry connection if within the retry limit, otherwise fail */
714 if ( ++iscsi->retry_count <= ISCSI_MAX_RETRIES ) {
715 tcp_connect ( conn );
717 printf ( "iSCSI retry count exceeded\n" );
718 iscsi->status |= ( ISCSI_STATUS_DONE | ISCSI_STATUS_ERR );
723 * Handle TCP connection opening
725 * @v conn TCP connection
728 static void iscsi_connected ( struct tcp_connection *conn ) {
729 struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
731 /* Set connected flag and reset retry count */
732 iscsi->status |= ISCSI_STATUS_CONNECTED;
733 iscsi->retry_count = 0;
735 /* Prepare to receive PDUs. */
736 iscsi->rx_state = ISCSI_RX_BHS;
737 iscsi->rx_offset = 0;
739 /* Start logging in */
740 iscsi_start_login ( iscsi, 1 );
743 /** iSCSI TCP operations */
744 static struct tcp_operations iscsi_tcp_operations = {
745 .closed = iscsi_closed,
746 .connected = iscsi_connected,
747 .acked = iscsi_acked,
748 .newdata = iscsi_newdata,
749 .senddata = iscsi_senddata,
753 * Issue SCSI command via iSCSI session
755 * @v iscsi iSCSI session
756 * @v command SCSI command
757 * @ret rc Return status code
759 int iscsi_issue ( struct iscsi_session *iscsi,
760 struct scsi_command *command ) {
761 iscsi->command = command;
762 iscsi->status &= ~( ISCSI_STATUS_DONE | ISCSI_STATUS_ERR );
764 if ( iscsi->status & ISCSI_STATUS_CONNECTED ) {
765 iscsi_start_command ( iscsi );
767 iscsi->tcp.tcp_op = &iscsi_tcp_operations;
768 tcp_connect ( &iscsi->tcp );
771 while ( ! ( iscsi->status & ISCSI_STATUS_DONE ) ) {
775 iscsi->command = NULL;
777 return ( ( iscsi->status & ISCSI_STATUS_ERR ) ? -EIO : 0 );