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 ));
78 DBG ( "iSCSI %p start " SCSI_CDB_FORMAT " %s %#x\n",
79 iscsi, SCSI_CDB_DATA ( command->cdb ),
80 ( iscsi->command->data_in ? "in" : "out" ),
81 ( iscsi->command->data_in ?
82 iscsi->command->data_in_len : iscsi->command->data_out_len ) );
86 * Receive data segment of an iSCSI SCSI response PDU
88 * @v iscsi iSCSI session
89 * @v data Received data
90 * @v len Length of received data
91 * @v remaining Data remaining after this data
94 static void iscsi_rx_scsi_response ( struct iscsi_session *iscsi, void *data,
95 size_t len, size_t remaining ) {
96 struct iscsi_bhs_scsi_response *response
97 = &iscsi->rx_bhs.scsi_response;
100 /* Capture the sense response code as it floats past, if present */
101 sense_offset = ISCSI_SENSE_RESPONSE_CODE_OFFSET - iscsi->rx_offset;
102 if ( ( sense_offset >= 0 ) && len ) {
103 iscsi->command->sense_response =
104 * ( ( char * ) data + sense_offset );
107 /* Wait for whole SCSI response to arrive */
111 /* Record SCSI status code */
112 iscsi->command->status = response->status;
114 /* Mark as completed, with error if applicable */
115 iscsi->status |= ISCSI_STATUS_DONE;
116 if ( response->response != ISCSI_RESPONSE_COMMAND_COMPLETE )
117 iscsi->status |= ISCSI_STATUS_ERR;
121 * Receive data segment of an iSCSI data-in PDU
123 * @v iscsi iSCSI session
124 * @v data Received data
125 * @v len Length of received data
126 * @v remaining Data remaining after this data
129 static void iscsi_rx_data_in ( struct iscsi_session *iscsi, void *data,
130 size_t len, size_t remaining __unused ) {
131 struct iscsi_bhs_data_in *data_in = &iscsi->rx_bhs.data_in;
132 unsigned long offset;
134 /* Copy data to data-in buffer */
135 offset = ntohl ( data_in->offset ) + iscsi->rx_offset;
136 assert ( iscsi->command != NULL );
137 assert ( iscsi->command->data_in );
138 assert ( ( offset + len ) <= iscsi->command->data_in_len );
139 copy_to_user ( iscsi->command->data_in, offset, data, len );
141 /* Record SCSI status, if present */
142 if ( data_in->flags & ISCSI_DATA_FLAG_STATUS )
143 iscsi->command->status = data_in->status;
145 /* If this is the end, flag as complete */
146 if ( ( offset + len ) == iscsi->command->data_in_len ) {
147 assert ( data_in->flags & ISCSI_FLAG_FINAL );
148 assert ( remaining == 0 );
149 iscsi->status |= ISCSI_STATUS_DONE;
154 * Receive data segment of an iSCSI R2T PDU
156 * @v iscsi iSCSI session
157 * @v data Received data
158 * @v len Length of received data
159 * @v remaining Data remaining after this data
162 static void iscsi_rx_r2t ( struct iscsi_session *iscsi, void *data __unused,
163 size_t len __unused, size_t remaining __unused ) {
164 struct iscsi_bhs_r2t *r2t = &iscsi->rx_bhs.r2t;
166 /* Record transfer parameters and trigger first data-out */
167 iscsi->ttt = ntohl ( r2t->ttt );
168 iscsi->transfer_offset = ntohl ( r2t->offset );
169 iscsi->transfer_len = ntohl ( r2t->len );
170 iscsi_start_data_out ( iscsi, 0 );
174 * Build iSCSI data-out BHS
176 * @v iscsi iSCSI session
177 * @v datasn Data sequence number within the transfer
180 static void iscsi_start_data_out ( struct iscsi_session *iscsi,
181 unsigned int datasn ) {
182 struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
183 unsigned long offset;
184 unsigned long remaining;
187 /* We always send 512-byte Data-Out PDUs; this removes the
188 * need to worry about the target's MaxRecvDataSegmentLength.
190 offset = datasn * 512;
191 remaining = iscsi->transfer_len - offset;
196 /* Construct BHS and initiate transmission */
197 iscsi_start_tx ( iscsi );
198 data_out->opcode = ISCSI_OPCODE_DATA_OUT;
199 if ( len == remaining )
200 data_out->flags = ( ISCSI_FLAG_FINAL );
201 ISCSI_SET_LENGTHS ( data_out->lengths, 0, len );
202 data_out->lun = iscsi->lun;
203 data_out->itt = htonl ( iscsi->itt );
204 data_out->ttt = htonl ( iscsi->ttt );
205 data_out->expstatsn = htonl ( iscsi->statsn + 1 );
206 data_out->datasn = htonl ( datasn );
207 data_out->offset = htonl ( iscsi->transfer_offset + offset );
208 DBG ( "iSCSI %p start data out DataSN %#x len %#lx\n",
209 iscsi, datasn, len );
213 * Complete iSCSI data-out PDU transmission
215 * @v iscsi iSCSI session
218 static void iscsi_data_out_done ( struct iscsi_session *iscsi ) {
219 struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
221 /* If we haven't reached the end of the sequence, start
222 * sending the next data-out PDU.
224 if ( ! ( data_out->flags & ISCSI_FLAG_FINAL ) )
225 iscsi_start_data_out ( iscsi, ntohl ( data_out->datasn ) + 1 );
229 * Send iSCSI data-out data segment
231 * @v iscsi iSCSI session
232 * @v buf Temporary data buffer
233 * @v len Length of temporary data buffer
235 static void iscsi_tx_data_out ( struct iscsi_session *iscsi,
236 void *buf, size_t len ) {
237 struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
238 unsigned long offset;
239 unsigned long remaining;
241 offset = ( iscsi->transfer_offset + ntohl ( data_out->offset ) +
243 remaining = ( ISCSI_DATA_LEN ( data_out->lengths ) - iscsi->tx_offset);
244 assert ( iscsi->command != NULL );
245 assert ( iscsi->command->data_out );
246 assert ( ( offset + remaining ) <= iscsi->command->data_out_len );
248 if ( remaining < len )
250 copy_from_user ( buf, iscsi->command->data_out, offset, len );
252 tcp_send ( &iscsi->tcp, buf, len );
255 /****************************************************************************
262 * Build iSCSI login request strings
264 * @v iscsi iSCSI session
266 * These are the initial set of strings sent in the first login
267 * request PDU. We want the following settings:
271 * MaxConnections is irrelevant; we make only one connection anyway
273 * ImmediateData is irrelevant; we never send immediate data
274 * MaxRecvDataSegmentLength=8192 (default; we don't care)
275 * MaxBurstLength=262144 (default; we don't care)
276 * FirstBurstLength=262144 (default; we don't care)
277 * DefaultTime2Wait=0 [2]
278 * DefaultTime2Retain=0 [2]
279 * MaxOutstandingR2T=1
281 * DataSequenceInOrder=Yes
282 * ErrorRecoveryLevel=0
284 * [1] InitialR2T has an OR resolution function, so the target may
285 * force us to use it. We therefore simplify our logic by always
288 * [2] These ensure that we can safely start a new task once we have
289 * reconnected after a failure, without having to manually tidy up
292 static int iscsi_build_login_request_strings ( struct iscsi_session *iscsi,
293 void *data, size_t len ) {
294 struct iscsi_bhs_login_request *request = &iscsi->tx_bhs.login_request;
296 switch ( request->flags & ISCSI_LOGIN_CSG_MASK ) {
297 case ISCSI_LOGIN_CSG_SECURITY_NEGOTIATION:
298 return snprintf ( data, len,
301 "SessionType=Normal%c"
302 "AuthMethod=CHAP,None%c"
304 iscsi->initiator, 0, iscsi->target, 0,
306 case ISCSI_LOGIN_CSG_OPERATIONAL_NEGOTIATION:
307 return snprintf ( data, len,
308 "HeaderDigest=None%c"
311 "DefaultTime2Wait=0%c"
312 "DefaultTime2Retain=0%c"
313 "MaxOutstandingR2T=1%c"
314 "DataPDUInOrder=Yes%c"
315 "DataSequenceInOrder=Yes%c"
316 "ErrorRecoveryLevel=0%c",
317 0, 0, 0, 0, 0, 0, 0, 0, 0 );
325 * Build iSCSI login request BHS
327 * @v iscsi iSCSI session
328 * @v stage Current stage of iSCSI login
329 * @v send_strings Send login strings with this login request
331 static void iscsi_start_login ( struct iscsi_session *iscsi,
332 int stage, int send_strings ) {
333 struct iscsi_bhs_login_request *request = &iscsi->tx_bhs.login_request;
336 /* Construct BHS and initiate transmission */
337 iscsi_start_tx ( iscsi );
338 request->opcode = ( ISCSI_OPCODE_LOGIN_REQUEST |
339 ISCSI_FLAG_IMMEDIATE );
340 request->flags = ( ISCSI_LOGIN_FLAG_TRANSITION | stage );
342 /* version_max and version_min left as zero */
343 if ( send_strings ) {
344 len = iscsi_build_login_request_strings ( iscsi, NULL, 0 );
345 ISCSI_SET_LENGTHS ( request->lengths, 0, len );
347 request->isid_iana_en = htonl ( ISCSI_ISID_IANA |
348 IANA_EN_FEN_SYSTEMS );
349 /* isid_iana_qual left as zero */
350 request->tsih = htons ( iscsi->tsih );
351 request->itt = htonl ( iscsi->itt );
352 /* cid left as zero */
353 request->cmdsn = htonl ( iscsi->cmdsn );
354 request->expstatsn = htonl ( iscsi->statsn + 1 );
358 * Transmit data segment of an iSCSI login request PDU
360 * @v iscsi iSCSI session
361 * @v buf Temporary data buffer
362 * @v len Length of temporary data buffer
364 * For login requests, the data segment consists of the login strings.
366 static void iscsi_tx_login_request ( struct iscsi_session *iscsi,
367 void *buf, size_t len ) {
368 len = iscsi_build_login_request_strings ( iscsi, buf, len );
369 tcp_send ( &iscsi->tcp, buf + iscsi->tx_offset,
370 len - iscsi->tx_offset );
374 * Receive data segment of an iSCSI login response PDU
376 * @v iscsi iSCSI session
377 * @v data Received data
378 * @v len Length of received data
379 * @v remaining Data remaining after this data
382 static void iscsi_rx_login_response ( struct iscsi_session *iscsi,
385 size_t remaining __unused ) {
386 struct iscsi_bhs_login_response *response
387 = &iscsi->rx_bhs.login_response;
389 /* Check for fatal errors */
390 if ( response->status_class != 0 ) {
391 printf ( "iSCSI login failure: class %02x detail %02x\n",
392 response->status_class, response->status_detail );
393 iscsi->status |= ( ISCSI_STATUS_DONE | ISCSI_STATUS_ERR );
394 tcp_close ( &iscsi->tcp );
398 /* If server did not transition, send back another login
399 * request without any login strings.
401 if ( ! ( response->flags & ISCSI_LOGIN_FLAG_TRANSITION ) ) {
402 iscsi_start_login ( iscsi, ( response->flags &
403 ISCSI_LOGIN_STAGE_MASK ), 0 );
407 /* If we are transitioning to the operational phase, send the
408 * operational phase login request.
410 if ( ( response->flags & ISCSI_LOGIN_NSG_MASK ) ==
411 ISCSI_LOGIN_NSG_OPERATIONAL_NEGOTIATION ) {
412 iscsi_start_login ( iscsi, ISCSI_LOGIN_STAGE_OP, 1 );
416 /* Record TSIH for future reference */
417 iscsi->tsih = ntohl ( response->tsih );
419 /* Send the SCSI command */
420 iscsi_start_command ( iscsi );
423 /****************************************************************************
425 * iSCSI to TCP interface
429 static inline struct iscsi_session *
430 tcp_to_iscsi ( struct tcp_connection *conn ) {
431 return container_of ( conn, struct iscsi_session, tcp );
435 * Start up a new TX PDU
437 * @v iscsi iSCSI session
439 * This initiates the process of sending a new PDU. Only one PDU may
440 * be in transit at any one time.
442 static void iscsi_start_tx ( struct iscsi_session *iscsi ) {
443 assert ( iscsi->tx_state == ISCSI_TX_IDLE );
445 /* Initialise TX BHS */
446 memset ( &iscsi->tx_bhs, 0, sizeof ( iscsi->tx_bhs ) );
448 /* Flag TX engine to start transmitting */
449 iscsi->tx_state = ISCSI_TX_BHS;
450 iscsi->tx_offset = 0;
454 * Transmit data segment of an iSCSI PDU
456 * @v iscsi iSCSI session
457 * @v buf Temporary data buffer
458 * @v len Length of temporary data buffer
460 * Handle transmission of part of a PDU data segment. iscsi::tx_bhs
461 * will be valid when this is called.
463 static void iscsi_tx_data ( struct iscsi_session *iscsi,
464 void *buf, size_t len ) {
465 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
467 switch ( common->opcode & ISCSI_OPCODE_MASK ) {
468 case ISCSI_OPCODE_DATA_OUT:
469 iscsi_tx_data_out ( iscsi, buf, len );
471 case ISCSI_OPCODE_LOGIN_REQUEST:
472 iscsi_tx_login_request ( iscsi, buf, len );
481 * Complete iSCSI PDU transmission
483 * @v iscsi iSCSI session
485 * Called when a PDU has been completely transmitted and the TX state
486 * machine is about to enter the idle state. iscsi::tx_bhs will be
487 * valid for the just-completed PDU when this is called.
489 static void iscsi_tx_done ( struct iscsi_session *iscsi ) {
490 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
492 switch ( common->opcode & ISCSI_OPCODE_MASK ) {
493 case ISCSI_OPCODE_DATA_OUT:
494 iscsi_data_out_done ( iscsi );
504 * @v iscsi iSCSI session
506 * Updates iscsi->tx_offset and, if applicable, transitions to the
509 static void iscsi_acked ( struct tcp_connection *conn, size_t len ) {
510 struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
511 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
512 size_t max_tx_offset;
513 enum iscsi_tx_state next_state;
515 iscsi->tx_offset += len;
517 switch ( iscsi->tx_state ) {
519 max_tx_offset = sizeof ( iscsi->tx_bhs );
520 next_state = ISCSI_TX_AHS;
523 max_tx_offset = 4 * ISCSI_AHS_LEN ( common->lengths );
524 next_state = ISCSI_TX_DATA;
527 max_tx_offset = ISCSI_DATA_LEN ( common->lengths );
528 next_state = ISCSI_TX_DATA_PADDING;
530 case ISCSI_TX_DATA_PADDING:
531 max_tx_offset = ISCSI_DATA_PAD_LEN ( common->lengths );
532 next_state = ISCSI_TX_IDLE;
540 assert ( iscsi->tx_offset <= max_tx_offset );
542 /* If the whole of the current portion has not yet
543 * been acked, stay in this state for now.
545 if ( iscsi->tx_offset != max_tx_offset )
548 /* Move to next state. Call iscsi_tx_done() when PDU
549 * transmission is complete.
551 iscsi->tx_state = next_state;
552 iscsi->tx_offset = 0;
553 if ( next_state == ISCSI_TX_IDLE )
554 iscsi_tx_done ( iscsi );
561 * @v iscsi iSCSI session
562 * @v buf Temporary data buffer
563 * @v len Length of temporary data buffer
565 * Constructs data to be sent for the current TX state
567 static void iscsi_senddata ( struct tcp_connection *conn,
568 void *buf, size_t len ) {
569 struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
570 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
571 static const char pad[] = { '\0', '\0', '\0' };
573 switch ( iscsi->tx_state ) {
575 /* Nothing to send */
578 tcp_send ( conn, &iscsi->tx_bhs.bytes[iscsi->tx_offset],
579 ( sizeof ( iscsi->tx_bhs ) - iscsi->tx_offset ) );
582 /* We don't yet have an AHS transmission mechanism */
586 iscsi_tx_data ( iscsi, buf, len );
588 case ISCSI_TX_DATA_PADDING:
589 tcp_send ( conn, pad, ( ISCSI_DATA_PAD_LEN ( common->lengths )
590 - iscsi->tx_offset ) );
599 * Receive data segment of an iSCSI PDU
601 * @v iscsi iSCSI session
602 * @v data Received data
603 * @v len Length of received data
604 * @v remaining Data remaining after this data
606 * Handle processing of part of a PDU data segment. iscsi::rx_bhs
607 * will be valid when this is called.
609 static void iscsi_rx_data ( struct iscsi_session *iscsi, void *data,
610 size_t len, size_t remaining ) {
611 struct iscsi_bhs_common_response *response
612 = &iscsi->rx_bhs.common_response;
614 /* Update cmdsn and statsn */
615 iscsi->cmdsn = ntohl ( response->expcmdsn );
616 iscsi->statsn = ntohl ( response->statsn );
618 switch ( response->opcode & ISCSI_OPCODE_MASK ) {
619 case ISCSI_OPCODE_LOGIN_RESPONSE:
620 iscsi_rx_login_response ( iscsi, data, len, remaining );
622 case ISCSI_OPCODE_SCSI_RESPONSE:
623 iscsi_rx_scsi_response ( iscsi, data, len, remaining );
625 case ISCSI_OPCODE_DATA_IN:
626 iscsi_rx_data_in ( iscsi, data, len, remaining );
628 case ISCSI_OPCODE_R2T:
629 iscsi_rx_r2t ( iscsi, data, len, remaining );
632 printf ( "Unknown iSCSI opcode %02x\n", response->opcode );
633 iscsi->status |= ( ISCSI_STATUS_DONE | ISCSI_STATUS_ERR );
639 * Discard portion of an iSCSI PDU.
641 * @v iscsi iSCSI session
642 * @v data Received data
643 * @v len Length of received data
644 * @v remaining Data remaining after this data
646 * This discards data from a portion of a received PDU.
648 static void iscsi_rx_discard ( struct iscsi_session *iscsi __unused,
649 void *data __unused, size_t len __unused,
650 size_t remaining __unused ) {
655 * Receive basic header segment of an iSCSI PDU
657 * @v iscsi iSCSI session
658 * @v data Received data
659 * @v len Length of received data
660 * @v remaining Data remaining after this data
662 * This fills in iscsi::rx_bhs with the data from the BHS portion of
665 static void iscsi_rx_bhs ( struct iscsi_session *iscsi, void *data,
666 size_t len, size_t remaining __unused ) {
667 memcpy ( &iscsi->rx_bhs.bytes[iscsi->rx_offset], data, len );
668 if ( ( iscsi->rx_offset + len ) >= sizeof ( iscsi->rx_bhs ) ) {
669 DBG ( "iSCSI %p received PDU opcode %#x len %#lx\n",
670 iscsi, iscsi->rx_bhs.common.opcode,
671 ISCSI_DATA_LEN ( iscsi->rx_bhs.common.lengths ) );
678 * @v tcp TCP connection
679 * @v data Received data
680 * @v len Length of received data
682 * This handles received PDUs. The receive strategy is to fill in
683 * iscsi::rx_bhs with the contents of the BHS portion of the PDU,
684 * throw away any AHS portion, and then process each part of the data
685 * portion as it arrives. The data processing routine therefore
686 * always has a full copy of the BHS available, even for portions of
687 * the data in different packets to the BHS.
689 static void iscsi_newdata ( struct tcp_connection *conn, void *data,
691 struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
692 struct iscsi_bhs_common *common = &iscsi->rx_bhs.common;
693 void ( *process ) ( struct iscsi_session *iscsi, void *data,
694 size_t len, size_t remaining );
695 size_t max_rx_offset;
696 enum iscsi_rx_state next_state;
701 switch ( iscsi->rx_state ) {
703 process = iscsi_rx_bhs;
704 max_rx_offset = sizeof ( iscsi->rx_bhs );
705 next_state = ISCSI_RX_AHS;
708 process = iscsi_rx_discard;
709 max_rx_offset = 4 * ISCSI_AHS_LEN ( common->lengths );
710 next_state = ISCSI_RX_DATA;
713 process = iscsi_rx_data;
714 max_rx_offset = ISCSI_DATA_LEN ( common->lengths );
715 next_state = ISCSI_RX_DATA_PADDING;
717 case ISCSI_RX_DATA_PADDING:
718 process = iscsi_rx_discard;
719 max_rx_offset = ISCSI_DATA_PAD_LEN ( common->lengths );
720 next_state = ISCSI_RX_BHS;
727 frag_len = max_rx_offset - iscsi->rx_offset;
728 if ( frag_len > len )
730 remaining = max_rx_offset - iscsi->rx_offset - frag_len;
731 process ( iscsi, data, frag_len, remaining );
733 iscsi->rx_offset += frag_len;
737 /* If all the data for this state has not yet been
738 * received, stay in this state for now.
740 if ( iscsi->rx_offset != max_rx_offset )
743 iscsi->rx_state = next_state;
744 iscsi->rx_offset = 0;
749 * Handle TCP connection closure
751 * @v conn TCP connection
752 * @v status Error code, if any
755 static void iscsi_closed ( struct tcp_connection *conn, int status __unused ) {
756 struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
758 /* Clear connected flag */
759 iscsi->status &= ~ISCSI_STATUS_CONNECTED;
761 /* Retry connection if within the retry limit, otherwise fail */
762 if ( ++iscsi->retry_count <= ISCSI_MAX_RETRIES ) {
763 tcp_connect ( conn );
765 printf ( "iSCSI retry count exceeded\n" );
766 iscsi->status |= ( ISCSI_STATUS_DONE | ISCSI_STATUS_ERR );
771 * Handle TCP connection opening
773 * @v conn TCP connection
776 static void iscsi_connected ( struct tcp_connection *conn ) {
777 struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
779 /* Set connected flag and reset retry count */
780 iscsi->status |= ISCSI_STATUS_CONNECTED;
781 iscsi->retry_count = 0;
783 /* Prepare to receive PDUs. */
784 iscsi->rx_state = ISCSI_RX_BHS;
785 iscsi->rx_offset = 0;
787 /* Assign fresh initiator task tag */
790 /* Start logging in */
791 iscsi_start_login ( iscsi, ISCSI_LOGIN_STAGE_SEC, 1 );
794 /** iSCSI TCP operations */
795 static struct tcp_operations iscsi_tcp_operations = {
796 .closed = iscsi_closed,
797 .connected = iscsi_connected,
798 .acked = iscsi_acked,
799 .newdata = iscsi_newdata,
800 .senddata = iscsi_senddata,
804 * Issue SCSI command via iSCSI session
806 * @v iscsi iSCSI session
807 * @v command SCSI command
808 * @ret rc Return status code
810 int iscsi_issue ( struct iscsi_session *iscsi,
811 struct scsi_command *command ) {
812 iscsi->command = command;
813 iscsi->status &= ~( ISCSI_STATUS_DONE | ISCSI_STATUS_ERR );
815 if ( iscsi->status & ISCSI_STATUS_CONNECTED ) {
816 iscsi_start_command ( iscsi );
817 tcp_senddata ( &iscsi->tcp );
819 iscsi->tcp.tcp_op = &iscsi_tcp_operations;
820 tcp_connect ( &iscsi->tcp );
823 while ( ! ( iscsi->status & ISCSI_STATUS_DONE ) ) {
827 iscsi->command = NULL;
829 return ( ( iscsi->status & ISCSI_STATUS_ERR ) ? -EIO : 0 );