Quellcode durchsuchen

Provide a SCSI device interface to the iSCSI protocol

tags/v0.9.3
Michael Brown vor 18 Jahren
Ursprung
Commit
5009f8d6a2
2 geänderte Dateien mit 692 neuen und 35 gelöschten Zeilen
  1. 101
    35
      src/include/gpxe/iscsi.h
  2. 591
    0
      src/net/tcp/iscsi.c

+ 101
- 35
src/include/gpxe/iscsi.h Datei anzeigen

@@ -81,6 +81,60 @@ struct iscsi_bhs_common {
81 81
 /** Final PDU of a sequence */
82 82
 #define ISCSI_FLAG_FINAL 0x80
83 83
 
84
+/**
85
+ * iSCSI basic header segment common request fields
86
+ *
87
+ */
88
+struct iscsi_bhs_common_request {
89
+	/** Opcode */
90
+	uint8_t opcode;
91
+	/** Flags */
92
+	uint8_t flags;
93
+	/** Fields specific to the PDU type */
94
+	uint8_t other_a[2];
95
+	/** Segment lengths */
96
+	union iscsi_segment_lengths lengths;
97
+	/** Fields specific to the PDU type */
98
+	uint8_t other_b[8];
99
+	/** Initiator Task Tag */
100
+	uint32_t itt;
101
+	/** Fields specific to the PDU type */
102
+	uint8_t other_c[4];
103
+	/** Command sequence number */
104
+	uint32_t cmdsn;
105
+	/** Expected status sequence number */
106
+	uint32_t expstatsn;
107
+	/** Fields specific to the PDU type */
108
+	uint8_t other_d[16];
109
+};
110
+
111
+/**
112
+ * iSCSI basic header segment common request fields
113
+ *
114
+ */
115
+struct iscsi_bhs_common_response {
116
+	/** Opcode */
117
+	uint8_t opcode;
118
+	/** Flags */
119
+	uint8_t flags;
120
+	/** Fields specific to the PDU type */
121
+	uint8_t other_a[2];
122
+	/** Segment lengths */
123
+	union iscsi_segment_lengths lengths;
124
+	/** Fields specific to the PDU type */
125
+	uint8_t other_b[8];
126
+	/** Initiator Task Tag */
127
+	uint32_t itt;
128
+	/** Fields specific to the PDU type */
129
+	uint8_t other_c[4];
130
+	/** Status sequence number */
131
+	uint32_t statsn;
132
+	/** Expected command sequence number */
133
+	uint32_t expcmdsn;
134
+	/** Fields specific to the PDU type */
135
+	uint8_t other_d[16];
136
+};
137
+
84 138
 /**
85 139
  * iSCSI login request basic header segment
86 140
  *
@@ -203,7 +257,7 @@ struct iscsi_bhs_scsi_command {
203 257
 	/** Segment lengths */
204 258
 	union iscsi_segment_lengths lengths;
205 259
 	/** SCSI Logical Unit Number */
206
-	uint8_t lun[8];
260
+	uint64_t lun;
207 261
 	/** Initiator Task Tag */
208 262
 	uint32_t itt;
209 263
 	/** Expected data transfer length */
@@ -290,7 +344,7 @@ struct iscsi_bhs_data_in {
290 344
 	/** Segment lengths */
291 345
 	union iscsi_segment_lengths lengths;
292 346
 	/** Logical Unit Number */
293
-	uint8_t lun[8];
347
+	uint64_t lun;
294 348
 	/** Initiator Task Tag */
295 349
 	uint32_t itt;
296 350
 	/** Target Transfer Tag */
@@ -329,6 +383,8 @@ struct iscsi_bhs_data_in {
329 383
  */
330 384
 union iscsi_bhs {
331 385
 	struct iscsi_bhs_common common;
386
+	struct iscsi_bhs_common_request common_request;
387
+	struct iscsi_bhs_common_response common_response;
332 388
 	struct iscsi_bhs_login_request login_request;
333 389
 	struct iscsi_bhs_login_response login_response;
334 390
 	struct iscsi_bhs_scsi_command scsi_command;
@@ -339,12 +395,10 @@ union iscsi_bhs {
339 395
 
340 396
 /** State */
341 397
 enum iscsi_state {
342
-	/** In the process of logging in */
343
-	ISCSI_STATE_FAILED = -1,
344
-	ISCSI_STATE_NOT_CONNECTED = 0,
345 398
 	ISCSI_STATE_IDLE,
346
-	ISCSI_STATE_LOGGING_IN,
347
-	ISCSI_STATE_READING_DATA,
399
+	ISCSI_STATE_LOGIN,
400
+	ISCSI_STATE_LOGIN_CONT,
401
+	ISCSI_STATE_COMMAND,
348 402
 };
349 403
 
350 404
 /** State of an iSCSI TX engine */
@@ -377,40 +431,32 @@ enum iscsi_rx_state {
377 431
 struct iscsi_session {
378 432
 	/** TCP connection for this session */
379 433
 	struct tcp_connection tcp;
434
+	/** Session status
435
+	 *
436
+	 * This is the bitwise-OR of zero or more ISCSI_STATUS_XXX
437
+	 * constants.
438
+	 */
439
+	int status;
440
+	/** Retry count
441
+	 *
442
+	 * Number of times that the connection has been retried.
443
+	 * Reset upon a successful connection.
444
+	 */
445
+	int retry_count;
380 446
 
381 447
 	/** Initiator IQN */
382 448
 	const char *initiator;
383 449
 	/** Target IQN */
384 450
 	const char *target;
451
+	/** Logical Unit Number (LUN) */
452
+	uint64_t lun;
385 453
 
386
-	/** Block size in bytes */
387
-	size_t block_size;
388
-	/** Starting block number of the current data transfer */
389
-	unsigned long block_start;
390
-	/** Block count of the current data transfer */
391
-	unsigned long block_count;
392
-	/** Block read callback function
393
-	 *
394
-	 * Note that this may be called several times, since it is
395
-	 * called per-packet rather than per-block.
396
-	 */
397
-	void ( * block_read_callback ) ( void *private, const void *data,
398
-					 unsigned long offset, size_t len );
399
-	/** Block read callback private data
400
-	 *
401
-	 * This is passed to block_read_callback()
402
-	 */
403
-	void *block_read_private;
404
-
405
-	/** State of the session */
406
-	enum iscsi_state state;
407 454
 	/** Target session identifying handle
408 455
 	 *
409 456
 	 * This is assigned by the target when we first log in, and
410 457
 	 * must be reused on subsequent login attempts.
411 458
 	 */
412 459
 	uint16_t tsih;
413
-
414 460
 	/** Initiator task tag
415 461
 	 *
416 462
 	 * This is the tag of the current command.  It is incremented
@@ -447,14 +493,34 @@ struct iscsi_session {
447 493
 	enum iscsi_rx_state rx_state;
448 494
 	/** Byte offset within the current RX state */
449 495
 	size_t rx_offset;
496
+
497
+	/** Current SCSI command
498
+	 *
499
+	 * Set to NULL when command is complete.
500
+	 */
501
+	struct scsi_command *command;
450 502
 };
451 503
 
452
-static inline int iscsi_busy ( struct iscsi_session *iscsi ) {
453
-	return ( iscsi->state > ISCSI_STATE_IDLE );
454
-}
504
+/** Session is currently connected */
505
+#define ISCSI_STATUS_CONNECTED 0x01
506
+
507
+/** Session has completed */
508
+#define ISCSI_STATUS_DONE 0x02
509
+
510
+/** Session failed */
511
+#define ISCSI_STATUS_ERR 0x04
512
+
513
+/** Maximum number of retries at connecting */
514
+#define ISCSI_MAX_RETRIES 2
515
+
516
+/** An iSCSI device */
517
+struct iscsi_device {
518
+	/** SCSI device interface */
519
+	struct scsi_device scsi;
520
+	/** iSCSI protocol instance */
521
+	struct iscsi_session iscsi;
522
+};
455 523
 
456
-static inline int iscsi_error ( struct iscsi_session *iscsi ) {
457
-	return ( iscsi->state == ISCSI_STATE_FAILED );
458
-}
524
+extern int init_iscsidev ( struct iscsi_device *iscsidev );
459 525
 
460 526
 #endif /* _GPXE_ISCSI_H */

+ 591
- 0
src/net/tcp/iscsi.c Datei anzeigen

@@ -0,0 +1,591 @@
1
+#include <stddef.h>
2
+#include <string.h>
3
+#include <vsprintf.h>
4
+#include <errno.h>
5
+#include <assert.h>
6
+#include <byteswap.h>
7
+#include <gpxe/scsi.h>
8
+#include <gpxe/process.h>
9
+#include <gpxe/iscsi.h>
10
+
11
+/** @file
12
+ *
13
+ * iSCSI protocol
14
+ *
15
+ */
16
+
17
+static void iscsi_start_tx ( struct iscsi_session *iscsi );
18
+
19
+/****************************************************************************
20
+ *
21
+ * iSCSI SCSI command issuing
22
+ *
23
+ */
24
+
25
+/**
26
+ * Build iSCSI SCSI command BHS
27
+ *
28
+ * @v iscsi		iSCSI session
29
+ */
30
+static void iscsi_start_command ( struct iscsi_session *iscsi ) {
31
+	struct iscsi_bhs_scsi_command *command = &iscsi->tx_bhs.scsi_command;
32
+
33
+	assert ( ! ( iscsi->command->data_in && iscsi->command->data_out ) );
34
+
35
+	/* Construct BHS and initiate transmission */
36
+	iscsi_start_tx ( iscsi );
37
+	command->opcode = ISCSI_OPCODE_SCSI_COMMAND;
38
+	command->flags = ( ISCSI_FLAG_FINAL |
39
+			   ISCSI_COMMAND_ATTR_SIMPLE );
40
+	if ( iscsi->command->data_in )
41
+		command->flags |= ISCSI_COMMAND_FLAG_READ;
42
+	if ( iscsi->command->data_out )
43
+		command->flags |= ISCSI_COMMAND_FLAG_WRITE;
44
+	ISCSI_SET_LENGTHS ( command->lengths, 0, iscsi->command->data_out_len);
45
+	command->lun = iscsi->lun;
46
+	command->itt = htonl ( iscsi->itt );
47
+	command->exp_len = htonl ( iscsi->command->data_in_len );
48
+	memcpy ( &command->cdb, &iscsi->command->cdb, sizeof ( command->cdb ));
49
+}
50
+
51
+/**
52
+ * Send iSCSI SCSI command data
53
+ *
54
+ * @v iscsi		iSCSI session
55
+ */
56
+static void iscsi_tx_command ( struct iscsi_session *iscsi ) {
57
+	tcp_send ( &iscsi->tcp, iscsi->command->data_out + iscsi->tx_offset,
58
+		   iscsi->command->data_out_len - iscsi->tx_offset );
59
+}
60
+
61
+/**
62
+ * Receive data segment of an iSCSI data-in PDU
63
+ *
64
+ * @v iscsi		iSCSI session
65
+ * @v data		Received data
66
+ * @v len		Length of received data
67
+ * @v remaining		Data remaining after this data
68
+ * 
69
+ */
70
+static void iscsi_rx_data_in ( struct iscsi_session *iscsi, void *data,
71
+			       size_t len, size_t remaining ) {
72
+	struct iscsi_bhs_data_in *data_in = &iscsi->rx_bhs.data_in;
73
+	unsigned long offset;
74
+
75
+	/* Copy data to data-in buffer */
76
+	offset = ntohl ( data_in->offset ) + iscsi->rx_offset;
77
+	assert ( iscsi->command != NULL );
78
+	assert ( iscsi->command->data_in != NULL );
79
+	assert ( ( offset + len ) <= iscsi->command->data_in_len );
80
+	memcpy ( ( iscsi->command->data_in + offset ), data, len );
81
+
82
+	/* If this is the end, flag as complete */
83
+	if ( ( data_in->flags & ISCSI_FLAG_FINAL ) && ( remaining == 0 ) )
84
+		iscsi->status |= ISCSI_STATUS_DONE;
85
+}
86
+
87
+/****************************************************************************
88
+ *
89
+ * iSCSI login
90
+ *
91
+ */
92
+
93
+/**
94
+ * Build iSCSI login request strings
95
+ *
96
+ * @v iscsi		iSCSI session
97
+ *
98
+ * These are the initial set of strings sent in the first login
99
+ * request PDU.
100
+ */
101
+static int iscsi_build_login_request_strings ( struct iscsi_session *iscsi,
102
+					       void *data, size_t len ) {
103
+	return snprintf ( data, len,
104
+			  "InitiatorName=%s%c"
105
+			  "TargetName=%s%c"
106
+			  "MaxRecvDataSegmentLength=512%c"
107
+			  "SessionType=Normal%c"
108
+			  "DataDigest=None%c"
109
+			  "HeaderDigest=None%c",
110
+			  iscsi->initiator, 0, iscsi->target, 0,
111
+			  0, 0, 0, 0 );
112
+}
113
+
114
+/**
115
+ * Build iSCSI login request BHS
116
+ *
117
+ * @v iscsi		iSCSI session
118
+ * @v first		Login request is the first request of a session
119
+ */
120
+static void iscsi_start_login ( struct iscsi_session *iscsi, int first ) {
121
+	struct iscsi_bhs_login_request *request = &iscsi->tx_bhs.login_request;
122
+	int len;
123
+
124
+	/* Construct BHS and initiate transmission */
125
+	iscsi_start_tx ( iscsi );
126
+	request->opcode = ( ISCSI_OPCODE_LOGIN_REQUEST |
127
+			    ISCSI_FLAG_IMMEDIATE );
128
+	request->flags = ( ISCSI_LOGIN_FLAG_TRANSITION |
129
+			   ISCSI_LOGIN_CSG_OPERATIONAL_NEGOTIATION |
130
+			   ISCSI_LOGIN_NSG_FULL_FEATURE_PHASE );
131
+	/* version_max and version_min left as zero */
132
+	if ( first ) {
133
+		len = iscsi_build_login_request_strings ( iscsi, NULL, 0 );
134
+		ISCSI_SET_LENGTHS ( request->lengths, 0, len );
135
+	}
136
+	request->isid_iana_en = htonl ( ISCSI_ISID_IANA |
137
+					IANA_EN_FEN_SYSTEMS );
138
+	/* isid_iana_qual left as zero */
139
+	request->tsih = htons ( iscsi->tsih );
140
+	/* itt left as zero */
141
+	/* cid left as zero */
142
+}
143
+
144
+/**
145
+ * Transmit data segment of an iSCSI login request PDU
146
+ *
147
+ * @v iscsi		iSCSI session
148
+ *
149
+ * For login requests, the data segment consists of the login strings.
150
+ */
151
+static void iscsi_tx_login_request ( struct iscsi_session *iscsi ) {
152
+	int len;
153
+
154
+	len = iscsi_build_login_request_strings ( iscsi, tcp_buffer,
155
+						  tcp_buflen );
156
+	tcp_send ( &iscsi->tcp, tcp_buffer + iscsi->tx_offset,
157
+		   len - iscsi->tx_offset );
158
+}
159
+
160
+/**
161
+ * Receive data segment of an iSCSI login response PDU
162
+ *
163
+ * @v iscsi		iSCSI session
164
+ * @v data		Received data
165
+ * @v len		Length of received data
166
+ * @v remaining		Data remaining after this data
167
+ * 
168
+ */
169
+static void iscsi_rx_login_response ( struct iscsi_session *iscsi,
170
+				      void *data __unused,
171
+				      size_t len __unused,
172
+				      size_t remaining __unused ) {
173
+	struct iscsi_bhs_login_response *response
174
+		= &iscsi->rx_bhs.login_response;
175
+
176
+	/* Check for fatal errors */
177
+	if ( response->status_class != 0 ) {
178
+		printf ( "iSCSI login failure: class %02x detail %02x\n",
179
+			 response->status_class, response->status_detail );
180
+		iscsi->status |= ( ISCSI_STATUS_DONE | ISCSI_STATUS_ERR );
181
+		tcp_close ( &iscsi->tcp );
182
+		return;
183
+	}
184
+
185
+	/* If server did not transition, send back another login
186
+	 * request without any login strings.
187
+	 */
188
+	if ( ! ( response->flags & ISCSI_LOGIN_FLAG_TRANSITION ) ) {
189
+		iscsi_start_login ( iscsi, 0 );
190
+		return;
191
+	}
192
+
193
+	/* Record TSIH for future reference */
194
+	iscsi->tsih = ntohl ( response->tsih );
195
+	
196
+	/* Send the SCSI command */
197
+	iscsi_start_command ( iscsi );
198
+}
199
+
200
+/****************************************************************************
201
+ *
202
+ * iSCSI to TCP interface
203
+ *
204
+ */
205
+
206
+static inline struct iscsi_session *
207
+tcp_to_iscsi ( struct tcp_connection *conn ) {
208
+	return container_of ( conn, struct iscsi_session, tcp );
209
+}
210
+
211
+/**
212
+ * Start up a new TX PDU
213
+ *
214
+ * @v iscsi		iSCSI session
215
+ *
216
+ * This initiates the process of sending a new PDU.  Only one PDU may
217
+ * be in transit at any one time.
218
+ */
219
+static void iscsi_start_tx ( struct iscsi_session *iscsi ) {
220
+	assert ( iscsi->tx_state == ISCSI_TX_IDLE );
221
+	
222
+	/* Initialise TX BHS */
223
+	memset ( &iscsi->tx_bhs, 0, sizeof ( iscsi->tx_bhs ) );
224
+	iscsi->tx_bhs.common_request.cmdsn = htonl ( iscsi->cmdsn );
225
+	iscsi->tx_bhs.common_request.expstatsn = htonl ( iscsi->statsn + 1 );
226
+
227
+	/* Flag TX engine to start transmitting */
228
+	iscsi->tx_state = ISCSI_TX_BHS;
229
+	iscsi->tx_offset = 0;
230
+}
231
+
232
+/**
233
+ * Transmit data segment of an iSCSI PDU
234
+ *
235
+ * @v iscsi		iSCSI session
236
+ * 
237
+ * Handle transmission of part of a PDU data segment.  iscsi::tx_bhs
238
+ * will be valid when this is called.
239
+ */
240
+static void iscsi_tx_data ( struct iscsi_session *iscsi ) {
241
+	struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
242
+
243
+	switch ( common->opcode & ISCSI_OPCODE_MASK ) {
244
+	case ISCSI_OPCODE_SCSI_COMMAND:
245
+		iscsi_tx_command ( iscsi );
246
+		break;
247
+	case ISCSI_OPCODE_LOGIN_REQUEST:
248
+		iscsi_tx_login_request ( iscsi );
249
+		break;
250
+	default:
251
+		assert ( 0 );
252
+		break;
253
+	}
254
+}
255
+
256
+/**
257
+ * Handle TCP ACKs
258
+ *
259
+ * @v iscsi		iSCSI session
260
+ * 
261
+ * Updates iscsi->tx_offset and, if applicable, transitions to the
262
+ * next TX state.
263
+ */
264
+static void iscsi_acked ( struct tcp_connection *conn, size_t len ) {
265
+	struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
266
+	struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
267
+	size_t max_tx_offset;
268
+	enum iscsi_tx_state next_state;
269
+	
270
+	iscsi->tx_offset += len;
271
+	while ( 1 ) {
272
+		switch ( iscsi->tx_state ) {
273
+		case ISCSI_TX_BHS:
274
+			max_tx_offset = sizeof ( iscsi->tx_bhs );
275
+			next_state = ISCSI_TX_AHS;
276
+			break;
277
+		case ISCSI_TX_AHS:
278
+			max_tx_offset = 4 * ISCSI_AHS_LEN ( common->lengths );
279
+			next_state = ISCSI_TX_DATA;
280
+			break;
281
+		case ISCSI_TX_DATA:
282
+			max_tx_offset = ISCSI_DATA_LEN ( common->lengths );
283
+			next_state = ISCSI_TX_DATA_PADDING;
284
+			break;
285
+		case ISCSI_TX_DATA_PADDING:
286
+			max_tx_offset = ISCSI_DATA_PAD_LEN ( common->lengths );
287
+			next_state = ISCSI_TX_IDLE;
288
+			break;
289
+		case ISCSI_TX_IDLE:
290
+			return;
291
+		default:
292
+			assert ( 0 );
293
+			return;
294
+		}
295
+		assert ( iscsi->tx_offset <= max_tx_offset );
296
+
297
+		/* If the whole of the current portion has not yet
298
+		 * been acked, stay in this state for now.
299
+		 */
300
+		if ( iscsi->tx_offset != max_tx_offset )
301
+			return;
302
+		
303
+		iscsi->tx_state = next_state;
304
+		iscsi->tx_offset = 0;
305
+	}
306
+}
307
+
308
+/**
309
+ * Transmit iSCSI PDU
310
+ *
311
+ * @v iscsi		iSCSI session
312
+ * 
313
+ * Constructs data to be sent for the current TX state
314
+ */
315
+static void iscsi_senddata ( struct tcp_connection *conn ) {
316
+	struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
317
+	struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
318
+	static const char pad[] = { '\0', '\0', '\0' };
319
+
320
+	switch ( iscsi->tx_state ) {
321
+	case ISCSI_TX_IDLE:
322
+		/* Nothing to send */
323
+		break;
324
+	case ISCSI_TX_BHS:
325
+		tcp_send ( conn, &iscsi->tx_bhs.bytes[iscsi->tx_offset],
326
+			   ( sizeof ( iscsi->tx_bhs ) - iscsi->tx_offset ) );
327
+		break;
328
+	case ISCSI_TX_AHS:
329
+		/* We don't yet have an AHS transmission mechanism */
330
+		assert ( 0 );
331
+		break;
332
+	case ISCSI_TX_DATA:
333
+		iscsi_tx_data ( iscsi );
334
+		break;
335
+	case ISCSI_TX_DATA_PADDING:
336
+		tcp_send ( conn, pad, ( ISCSI_DATA_PAD_LEN ( common->lengths )
337
+					- iscsi->tx_offset ) );
338
+		break;
339
+	default:
340
+		assert ( 0 );
341
+		break;
342
+	}
343
+}
344
+
345
+/**
346
+ * Receive data segment of an iSCSI PDU
347
+ *
348
+ * @v iscsi		iSCSI session
349
+ * @v data		Received data
350
+ * @v len		Length of received data
351
+ * @v remaining		Data remaining after this data
352
+ *
353
+ * Handle processing of part of a PDU data segment.  iscsi::rx_bhs
354
+ * will be valid when this is called.
355
+ */
356
+static void iscsi_rx_data ( struct iscsi_session *iscsi, void *data,
357
+			    size_t len, size_t remaining ) {
358
+	struct iscsi_bhs_common_response *response
359
+		= &iscsi->rx_bhs.common_response;
360
+
361
+	/* Update cmdsn and statsn */
362
+	iscsi->cmdsn = ntohl ( response->expcmdsn );
363
+	iscsi->statsn = ntohl ( response->statsn );
364
+
365
+	/* Increment itt when we receive a final response */
366
+	if ( response->flags & ISCSI_FLAG_FINAL )
367
+		iscsi->itt++;
368
+
369
+	switch ( response->opcode & ISCSI_OPCODE_MASK ) {
370
+	case ISCSI_OPCODE_LOGIN_RESPONSE:
371
+		iscsi_rx_login_response ( iscsi, data, len, remaining );
372
+		break;
373
+	case ISCSI_OPCODE_DATA_IN:
374
+		iscsi_rx_data_in ( iscsi, data, len, remaining );
375
+		break;
376
+	default:
377
+		printf ( "Unknown iSCSI opcode %02x\n", response->opcode );
378
+		iscsi->status |= ( ISCSI_STATUS_DONE | ISCSI_STATUS_ERR );
379
+		break;
380
+	}
381
+}
382
+
383
+/**
384
+ * Discard portion of an iSCSI PDU.
385
+ *
386
+ * @v iscsi		iSCSI session
387
+ * @v data		Received data
388
+ * @v len		Length of received data
389
+ * @v remaining		Data remaining after this data
390
+ *
391
+ * This discards data from a portion of a received PDU.
392
+ */
393
+static void iscsi_rx_discard ( struct iscsi_session *iscsi __unused,
394
+			       void *data __unused, size_t len __unused,
395
+			       size_t remaining __unused ) {
396
+	/* Do nothing */
397
+}
398
+
399
+/**
400
+ * Receive basic header segment of an iSCSI PDU
401
+ *
402
+ * @v iscsi		iSCSI session
403
+ * @v data		Received data
404
+ * @v len		Length of received data
405
+ * @v remaining		Data remaining after this data
406
+ *
407
+ * This fills in iscsi::rx_bhs with the data from the BHS portion of
408
+ * the received PDU.
409
+ */
410
+static void iscsi_rx_bhs ( struct iscsi_session *iscsi, void *data,
411
+			   size_t len, size_t remaining __unused ) {
412
+	memcpy ( &iscsi->rx_bhs.bytes[iscsi->rx_offset], data, len );
413
+}
414
+
415
+/**
416
+ * Receive new data
417
+ *
418
+ * @v tcp		TCP connection
419
+ * @v data		Received data
420
+ * @v len		Length of received data
421
+ *
422
+ * This handles received PDUs.  The receive strategy is to fill in
423
+ * iscsi::rx_bhs with the contents of the BHS portion of the PDU,
424
+ * throw away any AHS portion, and then process each part of the data
425
+ * portion as it arrives.  The data processing routine therefore
426
+ * always has a full copy of the BHS available, even for portions of
427
+ * the data in different packets to the BHS.
428
+ */
429
+static void iscsi_newdata ( struct tcp_connection *conn, void *data,
430
+			    size_t len ) {
431
+	struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
432
+	struct iscsi_bhs_common *common = &iscsi->rx_bhs.common;
433
+	void ( *process ) ( struct iscsi_session *iscsi, void *data,
434
+			    size_t len, size_t remaining );
435
+	size_t max_rx_offset;
436
+	enum iscsi_rx_state next_state;
437
+	size_t frag_len;
438
+	size_t remaining;
439
+
440
+	while ( 1 ) {
441
+		switch ( iscsi->rx_state ) {
442
+		case ISCSI_RX_BHS:
443
+			process = iscsi_rx_bhs;
444
+			max_rx_offset = sizeof ( iscsi->rx_bhs );
445
+			next_state = ISCSI_RX_AHS;			
446
+			break;
447
+		case ISCSI_RX_AHS:
448
+			process = iscsi_rx_discard;
449
+			max_rx_offset = 4 * ISCSI_AHS_LEN ( common->lengths );
450
+			next_state = ISCSI_RX_DATA;
451
+			break;
452
+		case ISCSI_RX_DATA:
453
+			process = iscsi_rx_data;
454
+			max_rx_offset = ISCSI_DATA_LEN ( common->lengths );
455
+			next_state = ISCSI_RX_DATA_PADDING;
456
+			break;
457
+		case ISCSI_RX_DATA_PADDING:
458
+			process = iscsi_rx_discard;
459
+			max_rx_offset = ISCSI_DATA_PAD_LEN ( common->lengths );
460
+			next_state = ISCSI_RX_BHS;
461
+			break;
462
+		default:
463
+			assert ( 0 );
464
+			return;
465
+		}
466
+
467
+		frag_len = max_rx_offset - iscsi->rx_offset;
468
+		if ( frag_len > len )
469
+			frag_len = len;
470
+		remaining = max_rx_offset - iscsi->rx_offset - frag_len;
471
+		process ( iscsi, data, frag_len, remaining );
472
+
473
+		iscsi->rx_offset += frag_len;
474
+		data += frag_len;
475
+		len -= frag_len;
476
+
477
+		/* If all the data for this state has not yet been
478
+		 * received, stay in this state for now.
479
+		 */
480
+		if ( iscsi->rx_offset != max_rx_offset )
481
+			return;
482
+
483
+		iscsi->rx_state = next_state;
484
+		iscsi->rx_offset = 0;
485
+	}
486
+}
487
+
488
+/**
489
+ * Handle TCP connection closure
490
+ *
491
+ * @v conn		TCP connection
492
+ * @v status		Error code, if any
493
+ *
494
+ */
495
+static void iscsi_closed ( struct tcp_connection *conn, int status __unused ) {
496
+	struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
497
+
498
+	/* Clear connected flag */
499
+	iscsi->status &= ~ISCSI_STATUS_CONNECTED;
500
+
501
+	/* Retry connection if within the retry limit, otherwise fail */
502
+	if ( ++iscsi->retry_count <= ISCSI_MAX_RETRIES ) {
503
+		tcp_connect ( conn );
504
+	} else {
505
+		iscsi->status |= ( ISCSI_STATUS_DONE | ISCSI_STATUS_ERR );
506
+	}
507
+}
508
+
509
+/**
510
+ * Handle TCP connection opening
511
+ *
512
+ * @v conn		TCP connection
513
+ *
514
+ */
515
+static void iscsi_connected ( struct tcp_connection *conn ) {
516
+	struct iscsi_session *iscsi = tcp_to_iscsi ( conn );
517
+
518
+	/* Set connected flag and reset retry count */
519
+	iscsi->status |= ISCSI_STATUS_CONNECTED;
520
+	iscsi->retry_count = 0;
521
+
522
+	/* Prepare to receive PDUs. */
523
+	iscsi->rx_state = ISCSI_RX_BHS;
524
+	iscsi->rx_offset = 0;
525
+
526
+	/* Start logging in */
527
+	iscsi_start_login ( iscsi, 1 );
528
+}
529
+
530
+/** iSCSI TCP operations */
531
+static struct tcp_operations iscsi_tcp_operations = {
532
+	.closed		= iscsi_closed,
533
+	.connected	= iscsi_connected,
534
+	.acked		= iscsi_acked,
535
+	.newdata	= iscsi_newdata,
536
+	.senddata	= iscsi_senddata,
537
+};
538
+
539
+/**
540
+ * Issue SCSI command via iSCSI session
541
+ *
542
+ * @v iscsi		iSCSI session
543
+ * @v command		SCSI command
544
+ * @ret rc		Return status code
545
+ */
546
+static int iscsi_command ( struct iscsi_session *iscsi,
547
+			   struct scsi_command *command ) {
548
+	iscsi->command = command;
549
+	iscsi->status &= ~( ISCSI_STATUS_DONE | ISCSI_STATUS_ERR );
550
+
551
+	if ( iscsi->status & ISCSI_STATUS_CONNECTED ) {
552
+		iscsi_start_command ( iscsi );
553
+	} else {
554
+		iscsi->tcp.tcp_op = &iscsi_tcp_operations;
555
+		tcp_connect ( &iscsi->tcp );
556
+	}
557
+
558
+	while ( ! ( iscsi->status & ISCSI_STATUS_DONE ) ) {
559
+		step();
560
+	}
561
+
562
+	iscsi->command = NULL;
563
+
564
+	return ( ( iscsi->status & ISCSI_STATUS_ERR ) ? -EIO : 0 );	
565
+}
566
+
567
+/**
568
+ * Issue SCSI command via iSCSI device
569
+ *
570
+ * @v scsi		SCSI device
571
+ * @v command		SCSI command
572
+ * @ret rc		Return status code
573
+ */
574
+static int iscsi_scsi_command ( struct scsi_device *scsi,
575
+				struct scsi_command *command ) {
576
+	struct iscsi_device *iscsidev
577
+		= container_of ( scsi, struct iscsi_device, scsi );
578
+
579
+	return iscsi_command ( &iscsidev->iscsi, command );
580
+}
581
+
582
+/**
583
+ * Initialise iSCSI device
584
+ *
585
+ * @v iscsidev		iSCSI device
586
+ */
587
+int init_iscsidev ( struct iscsi_device *iscsidev ) {
588
+	iscsidev->scsi.command = iscsi_scsi_command;
589
+	iscsidev->scsi.lun = iscsidev->iscsi.lun;
590
+	return init_scsidev ( &iscsidev->scsi );
591
+}

Laden…
Abbrechen
Speichern