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
263 * ImmediateData is irrelevant; we never send immediate data
264 * MaxRecvDataSegmentLength=8192 (default; we don't care)
265 * MaxBurstLength=262144 (default; we don't care)
266 * FirstBurstLength=262144 (default; we don't care)
267 * DefaultTime2Wait=0 [2]
268 * DefaultTime2Retain=0 [2]
269 * MaxOutstandingR2T=1
271 * DataSequenceInOrder=Yes
272 * ErrorRecoveryLevel=0
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"
288 "HeaderDigest=None%c"
291 "DefaultTime2Wait=0%c"
292 "DefaultTime2Retain=0%c"
293 "MaxOutstandingR2T=1%c"
294 "DataPDUInOrder=Yes%c"
295 "DataSequenceInOrder=Yes%c"
296 "ErrorRecoveryLevel=0%c",
297 iscsi->initiator, 0, iscsi->target, 0,
298 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
302 * Build iSCSI login request BHS
304 * @v iscsi iSCSI session
305 * @v first Login request is the first in a sequence
307 static void iscsi_start_login ( struct iscsi_session *iscsi, int first ) {
308 struct iscsi_bhs_login_request *request = &iscsi->tx_bhs.login_request;
311 /* Construct BHS and initiate transmission */
312 iscsi_start_tx ( iscsi );
313 request->opcode = ( ISCSI_OPCODE_LOGIN_REQUEST |
314 ISCSI_FLAG_IMMEDIATE );
315 request->flags = ( ISCSI_LOGIN_FLAG_TRANSITION |
316 ISCSI_LOGIN_CSG_OPERATIONAL_NEGOTIATION |
317 ISCSI_LOGIN_NSG_FULL_FEATURE_PHASE );
318 /* version_max and version_min left as zero */
320 len = iscsi_build_login_request_strings ( iscsi, NULL, 0 );
321 ISCSI_SET_LENGTHS ( request->lengths, 0, len );
323 request->isid_iana_en = htonl ( ISCSI_ISID_IANA |
324 IANA_EN_FEN_SYSTEMS );
325 /* isid_iana_qual left as zero */
326 request->tsih = htons ( iscsi->tsih );
329 request->itt = htonl ( iscsi->itt );
330 /* cid left as zero */
331 request->cmdsn = htonl ( iscsi->cmdsn );
332 request->expstatsn = htonl ( iscsi->statsn + 1 );
336 * Transmit data segment of an iSCSI login request PDU
338 * @v iscsi iSCSI session
340 * For login requests, the data segment consists of the login strings.
342 static void iscsi_tx_login_request ( struct iscsi_session *iscsi ) {
345 len = iscsi_build_login_request_strings ( iscsi, tcp_buffer,
347 tcp_send ( &iscsi->tcp, tcp_buffer + iscsi->tx_offset,
348 len - iscsi->tx_offset );
352 * Receive data segment of an iSCSI login response PDU
354 * @v iscsi iSCSI session
355 * @v data Received data
356 * @v len Length of received data
357 * @v remaining Data remaining after this data
360 static void iscsi_rx_login_response ( struct iscsi_session *iscsi,
363 size_t remaining __unused ) {
364 struct iscsi_bhs_login_response *response
365 = &iscsi->rx_bhs.login_response;
367 /* Check for fatal errors */
368 if ( response->status_class != 0 ) {
369 printf ( "iSCSI login failure: class %02x detail %02x\n",
370 response->status_class, response->status_detail );
371 iscsi->status |= ( ISCSI_STATUS_DONE | ISCSI_STATUS_ERR );
372 tcp_close ( &iscsi->tcp );
376 /* If server did not transition, send back another login
377 * request without any login strings.
379 if ( ! ( response->flags & ISCSI_LOGIN_FLAG_TRANSITION ) ) {
380 iscsi_start_login ( iscsi, 0 );
384 /* Record TSIH for future reference */
385 iscsi->tsih = ntohl ( response->tsih );
387 /* Send the SCSI command */
388 iscsi_start_command ( iscsi );
391 /****************************************************************************
393 * iSCSI to TCP interface
397 static inline struct iscsi_session *
398 tcp_to_iscsi ( struct tcp_connection *conn ) {
399 return container_of ( conn, struct iscsi_session, tcp );
403 * Start up a new TX PDU
405 * @v iscsi iSCSI session
407 * This initiates the process of sending a new PDU. Only one PDU may
408 * be in transit at any one time.
410 static void iscsi_start_tx ( struct iscsi_session *iscsi ) {
411 assert ( iscsi->tx_state == ISCSI_TX_IDLE );
413 /* Initialise TX BHS */
414 memset ( &iscsi->tx_bhs, 0, sizeof ( iscsi->tx_bhs ) );
416 /* Flag TX engine to start transmitting */
417 iscsi->tx_state = ISCSI_TX_BHS;
418 iscsi->tx_offset = 0;
422 * Transmit data segment of an iSCSI PDU
424 * @v iscsi iSCSI session
426 * Handle transmission of part of a PDU data segment. iscsi::tx_bhs
427 * will be valid when this is called.
429 static void iscsi_tx_data ( struct iscsi_session *iscsi ) {
430 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
432 switch ( common->opcode & ISCSI_OPCODE_MASK ) {
433 case ISCSI_OPCODE_DATA_OUT:
434 iscsi_tx_data_out ( iscsi );
436 case ISCSI_OPCODE_LOGIN_REQUEST:
437 iscsi_tx_login_request ( iscsi );
446 * Complete iSCSI PDU transmission
448 * @v iscsi iSCSI session
450 * Called when a PDU has been completely transmitted and the TX state
451 * machine is about to enter the idle state. iscsi::tx_bhs will be
452 * valid for the just-completed PDU when this is called.
454 static void iscsi_tx_done ( struct iscsi_session *iscsi ) {
455 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
457 switch ( common->opcode & ISCSI_OPCODE_MASK ) {
458 case ISCSI_OPCODE_DATA_OUT:
459 iscsi_data_out_done ( iscsi );
469 * @v iscsi iSCSI session
471 * Updates iscsi->tx_offset and, if applicable, transitions to the
474 static void iscsi_acked ( struct tcp_connection *conn, size_t len ) {
475 struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
476 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
477 size_t max_tx_offset;
478 enum iscsi_tx_state next_state;
480 iscsi->tx_offset += len;
482 switch ( iscsi->tx_state ) {
484 max_tx_offset = sizeof ( iscsi->tx_bhs );
485 next_state = ISCSI_TX_AHS;
488 max_tx_offset = 4 * ISCSI_AHS_LEN ( common->lengths );
489 next_state = ISCSI_TX_DATA;
492 max_tx_offset = ISCSI_DATA_LEN ( common->lengths );
493 next_state = ISCSI_TX_DATA_PADDING;
495 case ISCSI_TX_DATA_PADDING:
496 max_tx_offset = ISCSI_DATA_PAD_LEN ( common->lengths );
497 next_state = ISCSI_TX_IDLE;
505 assert ( iscsi->tx_offset <= max_tx_offset );
507 /* If the whole of the current portion has not yet
508 * been acked, stay in this state for now.
510 if ( iscsi->tx_offset != max_tx_offset )
513 /* Move to next state. Call iscsi_tx_done() when PDU
514 * transmission is complete.
516 iscsi->tx_state = next_state;
517 iscsi->tx_offset = 0;
518 if ( next_state == ISCSI_TX_IDLE )
519 iscsi_tx_done ( iscsi );
526 * @v iscsi iSCSI session
528 * Constructs data to be sent for the current TX state
530 static void iscsi_senddata ( struct tcp_connection *conn ) {
531 struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
532 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
533 static const char pad[] = { '\0', '\0', '\0' };
535 switch ( iscsi->tx_state ) {
537 /* Nothing to send */
540 tcp_send ( conn, &iscsi->tx_bhs.bytes[iscsi->tx_offset],
541 ( sizeof ( iscsi->tx_bhs ) - iscsi->tx_offset ) );
544 /* We don't yet have an AHS transmission mechanism */
548 iscsi_tx_data ( iscsi );
550 case ISCSI_TX_DATA_PADDING:
551 tcp_send ( conn, pad, ( ISCSI_DATA_PAD_LEN ( common->lengths )
552 - iscsi->tx_offset ) );
561 * Receive data segment of an iSCSI PDU
563 * @v iscsi iSCSI session
564 * @v data Received data
565 * @v len Length of received data
566 * @v remaining Data remaining after this data
568 * Handle processing of part of a PDU data segment. iscsi::rx_bhs
569 * will be valid when this is called.
571 static void iscsi_rx_data ( struct iscsi_session *iscsi, void *data,
572 size_t len, size_t remaining ) {
573 struct iscsi_bhs_common_response *response
574 = &iscsi->rx_bhs.common_response;
576 /* Update cmdsn and statsn */
577 iscsi->cmdsn = ntohl ( response->expcmdsn );
578 iscsi->statsn = ntohl ( response->statsn );
580 switch ( response->opcode & ISCSI_OPCODE_MASK ) {
581 case ISCSI_OPCODE_LOGIN_RESPONSE:
582 iscsi_rx_login_response ( iscsi, data, len, remaining );
584 case ISCSI_OPCODE_SCSI_RESPONSE:
585 iscsi_rx_scsi_response ( iscsi, data, len, remaining );
587 case ISCSI_OPCODE_DATA_IN:
588 iscsi_rx_data_in ( iscsi, data, len, remaining );
590 case ISCSI_OPCODE_R2T:
591 iscsi_rx_r2t ( iscsi, data, len, remaining );
594 printf ( "Unknown iSCSI opcode %02x\n", response->opcode );
595 iscsi->status |= ( ISCSI_STATUS_DONE | ISCSI_STATUS_ERR );
601 * Discard portion of an iSCSI PDU.
603 * @v iscsi iSCSI session
604 * @v data Received data
605 * @v len Length of received data
606 * @v remaining Data remaining after this data
608 * This discards data from a portion of a received PDU.
610 static void iscsi_rx_discard ( struct iscsi_session *iscsi __unused,
611 void *data __unused, size_t len __unused,
612 size_t remaining __unused ) {
617 * Receive basic header segment of an iSCSI PDU
619 * @v iscsi iSCSI session
620 * @v data Received data
621 * @v len Length of received data
622 * @v remaining Data remaining after this data
624 * This fills in iscsi::rx_bhs with the data from the BHS portion of
627 static void iscsi_rx_bhs ( struct iscsi_session *iscsi, void *data,
628 size_t len, size_t remaining __unused ) {
629 memcpy ( &iscsi->rx_bhs.bytes[iscsi->rx_offset], data, len );
635 * @v tcp TCP connection
636 * @v data Received data
637 * @v len Length of received data
639 * This handles received PDUs. The receive strategy is to fill in
640 * iscsi::rx_bhs with the contents of the BHS portion of the PDU,
641 * throw away any AHS portion, and then process each part of the data
642 * portion as it arrives. The data processing routine therefore
643 * always has a full copy of the BHS available, even for portions of
644 * the data in different packets to the BHS.
646 static void iscsi_newdata ( struct tcp_connection *conn, void *data,
648 struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
649 struct iscsi_bhs_common *common = &iscsi->rx_bhs.common;
650 void ( *process ) ( struct iscsi_session *iscsi, void *data,
651 size_t len, size_t remaining );
652 size_t max_rx_offset;
653 enum iscsi_rx_state next_state;
658 switch ( iscsi->rx_state ) {
660 process = iscsi_rx_bhs;
661 max_rx_offset = sizeof ( iscsi->rx_bhs );
662 next_state = ISCSI_RX_AHS;
665 process = iscsi_rx_discard;
666 max_rx_offset = 4 * ISCSI_AHS_LEN ( common->lengths );
667 next_state = ISCSI_RX_DATA;
670 process = iscsi_rx_data;
671 max_rx_offset = ISCSI_DATA_LEN ( common->lengths );
672 next_state = ISCSI_RX_DATA_PADDING;
674 case ISCSI_RX_DATA_PADDING:
675 process = iscsi_rx_discard;
676 max_rx_offset = ISCSI_DATA_PAD_LEN ( common->lengths );
677 next_state = ISCSI_RX_BHS;
684 frag_len = max_rx_offset - iscsi->rx_offset;
685 if ( frag_len > len )
687 remaining = max_rx_offset - iscsi->rx_offset - frag_len;
688 process ( iscsi, data, frag_len, remaining );
690 iscsi->rx_offset += frag_len;
694 /* If all the data for this state has not yet been
695 * received, stay in this state for now.
697 if ( iscsi->rx_offset != max_rx_offset )
700 iscsi->rx_state = next_state;
701 iscsi->rx_offset = 0;
706 * Handle TCP connection closure
708 * @v conn TCP connection
709 * @v status Error code, if any
712 static void iscsi_closed ( struct tcp_connection *conn, int status __unused ) {
713 struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
715 /* Clear connected flag */
716 iscsi->status &= ~ISCSI_STATUS_CONNECTED;
718 /* Retry connection if within the retry limit, otherwise fail */
719 if ( ++iscsi->retry_count <= ISCSI_MAX_RETRIES ) {
720 tcp_connect ( conn );
722 printf ( "iSCSI retry count exceeded\n" );
723 iscsi->status |= ( ISCSI_STATUS_DONE | ISCSI_STATUS_ERR );
728 * Handle TCP connection opening
730 * @v conn TCP connection
733 static void iscsi_connected ( struct tcp_connection *conn ) {
734 struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
736 /* Set connected flag and reset retry count */
737 iscsi->status |= ISCSI_STATUS_CONNECTED;
738 iscsi->retry_count = 0;
740 /* Prepare to receive PDUs. */
741 iscsi->rx_state = ISCSI_RX_BHS;
742 iscsi->rx_offset = 0;
744 /* Start logging in */
745 iscsi_start_login ( iscsi, 1 );
748 /** iSCSI TCP operations */
749 static struct tcp_operations iscsi_tcp_operations = {
750 .closed = iscsi_closed,
751 .connected = iscsi_connected,
752 .acked = iscsi_acked,
753 .newdata = iscsi_newdata,
754 .senddata = iscsi_senddata,
758 * Issue SCSI command via iSCSI session
760 * @v iscsi iSCSI session
761 * @v command SCSI command
762 * @ret rc Return status code
764 int iscsi_issue ( struct iscsi_session *iscsi,
765 struct scsi_command *command ) {
766 iscsi->command = command;
767 iscsi->status &= ~( ISCSI_STATUS_DONE | ISCSI_STATUS_ERR );
769 if ( iscsi->status & ISCSI_STATUS_CONNECTED ) {
770 iscsi_start_command ( iscsi );
771 tcp_kick ( &iscsi->tcp );
773 iscsi->tcp.tcp_op = &iscsi_tcp_operations;
774 tcp_connect ( &iscsi->tcp );
777 while ( ! ( iscsi->status & ISCSI_STATUS_DONE ) ) {
781 iscsi->command = NULL;
783 return ( ( iscsi->status & ISCSI_STATUS_ERR ) ? -EIO : 0 );