diff options
Diffstat (limited to 'drivers/scsi/libiscsi_tcp.c')
-rw-r--r-- | drivers/scsi/libiscsi_tcp.c | 1163 |
1 files changed, 1163 insertions, 0 deletions
diff --git a/drivers/scsi/libiscsi_tcp.c b/drivers/scsi/libiscsi_tcp.c new file mode 100644 index 000000000000..a745f91d2928 --- /dev/null +++ b/drivers/scsi/libiscsi_tcp.c | |||
@@ -0,0 +1,1163 @@ | |||
1 | /* | ||
2 | * iSCSI over TCP/IP Data-Path lib | ||
3 | * | ||
4 | * Copyright (C) 2004 Dmitry Yusupov | ||
5 | * Copyright (C) 2004 Alex Aizman | ||
6 | * Copyright (C) 2005 - 2006 Mike Christie | ||
7 | * Copyright (C) 2006 Red Hat, Inc. All rights reserved. | ||
8 | * maintained by open-iscsi@googlegroups.com | ||
9 | * | ||
10 | * This program is free software; you can redistribute it and/or modify | ||
11 | * it under the terms of the GNU General Public License as published | ||
12 | * by the Free Software Foundation; either version 2 of the License, or | ||
13 | * (at your option) any later version. | ||
14 | * | ||
15 | * This program is distributed in the hope that it will be useful, but | ||
16 | * WITHOUT ANY WARRANTY; without even the implied warranty of | ||
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
18 | * General Public License for more details. | ||
19 | * | ||
20 | * See the file COPYING included with this distribution for more details. | ||
21 | * | ||
22 | * Credits: | ||
23 | * Christoph Hellwig | ||
24 | * FUJITA Tomonori | ||
25 | * Arne Redlich | ||
26 | * Zhenyu Wang | ||
27 | */ | ||
28 | |||
29 | #include <linux/types.h> | ||
30 | #include <linux/list.h> | ||
31 | #include <linux/inet.h> | ||
32 | #include <linux/file.h> | ||
33 | #include <linux/blkdev.h> | ||
34 | #include <linux/crypto.h> | ||
35 | #include <linux/delay.h> | ||
36 | #include <linux/kfifo.h> | ||
37 | #include <linux/scatterlist.h> | ||
38 | #include <net/tcp.h> | ||
39 | #include <scsi/scsi_cmnd.h> | ||
40 | #include <scsi/scsi_device.h> | ||
41 | #include <scsi/scsi_host.h> | ||
42 | #include <scsi/scsi.h> | ||
43 | #include <scsi/scsi_transport_iscsi.h> | ||
44 | |||
45 | #include "iscsi_tcp.h" | ||
46 | |||
47 | MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, " | ||
48 | "Dmitry Yusupov <dmitry_yus@yahoo.com>, " | ||
49 | "Alex Aizman <itn780@yahoo.com>"); | ||
50 | MODULE_DESCRIPTION("iSCSI/TCP data-path"); | ||
51 | MODULE_LICENSE("GPL"); | ||
52 | #undef DEBUG_TCP | ||
53 | |||
54 | #ifdef DEBUG_TCP | ||
55 | #define debug_tcp(fmt...) printk(KERN_INFO "tcp: " fmt) | ||
56 | #else | ||
57 | #define debug_tcp(fmt...) | ||
58 | #endif | ||
59 | |||
60 | static int iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn, | ||
61 | struct iscsi_segment *segment); | ||
62 | |||
63 | /* | ||
64 | * Scatterlist handling: inside the iscsi_segment, we | ||
65 | * remember an index into the scatterlist, and set data/size | ||
66 | * to the current scatterlist entry. For highmem pages, we | ||
67 | * kmap as needed. | ||
68 | * | ||
69 | * Note that the page is unmapped when we return from | ||
70 | * TCP's data_ready handler, so we may end up mapping and | ||
71 | * unmapping the same page repeatedly. The whole reason | ||
72 | * for this is that we shouldn't keep the page mapped | ||
73 | * outside the softirq. | ||
74 | */ | ||
75 | |||
76 | /** | ||
77 | * iscsi_tcp_segment_init_sg - init indicated scatterlist entry | ||
78 | * @segment: the buffer object | ||
79 | * @sg: scatterlist | ||
80 | * @offset: byte offset into that sg entry | ||
81 | * | ||
82 | * This function sets up the segment so that subsequent | ||
83 | * data is copied to the indicated sg entry, at the given | ||
84 | * offset. | ||
85 | */ | ||
86 | static inline void | ||
87 | iscsi_tcp_segment_init_sg(struct iscsi_segment *segment, | ||
88 | struct scatterlist *sg, unsigned int offset) | ||
89 | { | ||
90 | segment->sg = sg; | ||
91 | segment->sg_offset = offset; | ||
92 | segment->size = min(sg->length - offset, | ||
93 | segment->total_size - segment->total_copied); | ||
94 | segment->data = NULL; | ||
95 | } | ||
96 | |||
97 | /** | ||
98 | * iscsi_tcp_segment_map - map the current S/G page | ||
99 | * @segment: iscsi_segment | ||
100 | * @recv: 1 if called from recv path | ||
101 | * | ||
102 | * We only need to possibly kmap data if scatter lists are being used, | ||
103 | * because the iscsi passthrough and internal IO paths will never use high | ||
104 | * mem pages. | ||
105 | */ | ||
106 | static void iscsi_tcp_segment_map(struct iscsi_segment *segment, int recv) | ||
107 | { | ||
108 | struct scatterlist *sg; | ||
109 | |||
110 | if (segment->data != NULL || !segment->sg) | ||
111 | return; | ||
112 | |||
113 | sg = segment->sg; | ||
114 | BUG_ON(segment->sg_mapped); | ||
115 | BUG_ON(sg->length == 0); | ||
116 | |||
117 | /* | ||
118 | * If the page count is greater than one it is ok to send | ||
119 | * to the network layer's zero copy send path. If not we | ||
120 | * have to go the slow sendmsg path. We always map for the | ||
121 | * recv path. | ||
122 | */ | ||
123 | if (page_count(sg_page(sg)) >= 1 && !recv) | ||
124 | return; | ||
125 | |||
126 | debug_tcp("iscsi_tcp_segment_map %s %p\n", recv ? "recv" : "xmit", | ||
127 | segment); | ||
128 | segment->sg_mapped = kmap_atomic(sg_page(sg), KM_SOFTIRQ0); | ||
129 | segment->data = segment->sg_mapped + sg->offset + segment->sg_offset; | ||
130 | } | ||
131 | |||
132 | void iscsi_tcp_segment_unmap(struct iscsi_segment *segment) | ||
133 | { | ||
134 | debug_tcp("iscsi_tcp_segment_unmap %p\n", segment); | ||
135 | |||
136 | if (segment->sg_mapped) { | ||
137 | debug_tcp("iscsi_tcp_segment_unmap valid\n"); | ||
138 | kunmap_atomic(segment->sg_mapped, KM_SOFTIRQ0); | ||
139 | segment->sg_mapped = NULL; | ||
140 | segment->data = NULL; | ||
141 | } | ||
142 | } | ||
143 | EXPORT_SYMBOL_GPL(iscsi_tcp_segment_unmap); | ||
144 | |||
145 | /* | ||
146 | * Splice the digest buffer into the buffer | ||
147 | */ | ||
148 | static inline void | ||
149 | iscsi_tcp_segment_splice_digest(struct iscsi_segment *segment, void *digest) | ||
150 | { | ||
151 | segment->data = digest; | ||
152 | segment->digest_len = ISCSI_DIGEST_SIZE; | ||
153 | segment->total_size += ISCSI_DIGEST_SIZE; | ||
154 | segment->size = ISCSI_DIGEST_SIZE; | ||
155 | segment->copied = 0; | ||
156 | segment->sg = NULL; | ||
157 | segment->hash = NULL; | ||
158 | } | ||
159 | |||
160 | /** | ||
161 | * iscsi_tcp_segment_done - check whether the segment is complete | ||
162 | * @tcp_conn: iscsi tcp connection | ||
163 | * @segment: iscsi segment to check | ||
164 | * @recv: set to one of this is called from the recv path | ||
165 | * @copied: number of bytes copied | ||
166 | * | ||
167 | * Check if we're done receiving this segment. If the receive | ||
168 | * buffer is full but we expect more data, move on to the | ||
169 | * next entry in the scatterlist. | ||
170 | * | ||
171 | * If the amount of data we received isn't a multiple of 4, | ||
172 | * we will transparently receive the pad bytes, too. | ||
173 | * | ||
174 | * This function must be re-entrant. | ||
175 | */ | ||
176 | int iscsi_tcp_segment_done(struct iscsi_tcp_conn *tcp_conn, | ||
177 | struct iscsi_segment *segment, int recv, | ||
178 | unsigned copied) | ||
179 | { | ||
180 | static unsigned char padbuf[ISCSI_PAD_LEN]; | ||
181 | struct scatterlist sg; | ||
182 | unsigned int pad; | ||
183 | |||
184 | debug_tcp("copied %u %u size %u %s\n", segment->copied, copied, | ||
185 | segment->size, recv ? "recv" : "xmit"); | ||
186 | if (segment->hash && copied) { | ||
187 | /* | ||
188 | * If a segment is kmapd we must unmap it before sending | ||
189 | * to the crypto layer since that will try to kmap it again. | ||
190 | */ | ||
191 | iscsi_tcp_segment_unmap(segment); | ||
192 | |||
193 | if (!segment->data) { | ||
194 | sg_init_table(&sg, 1); | ||
195 | sg_set_page(&sg, sg_page(segment->sg), copied, | ||
196 | segment->copied + segment->sg_offset + | ||
197 | segment->sg->offset); | ||
198 | } else | ||
199 | sg_init_one(&sg, segment->data + segment->copied, | ||
200 | copied); | ||
201 | crypto_hash_update(segment->hash, &sg, copied); | ||
202 | } | ||
203 | |||
204 | segment->copied += copied; | ||
205 | if (segment->copied < segment->size) { | ||
206 | iscsi_tcp_segment_map(segment, recv); | ||
207 | return 0; | ||
208 | } | ||
209 | |||
210 | segment->total_copied += segment->copied; | ||
211 | segment->copied = 0; | ||
212 | segment->size = 0; | ||
213 | |||
214 | /* Unmap the current scatterlist page, if there is one. */ | ||
215 | iscsi_tcp_segment_unmap(segment); | ||
216 | |||
217 | /* Do we have more scatterlist entries? */ | ||
218 | debug_tcp("total copied %u total size %u\n", segment->total_copied, | ||
219 | segment->total_size); | ||
220 | if (segment->total_copied < segment->total_size) { | ||
221 | /* Proceed to the next entry in the scatterlist. */ | ||
222 | iscsi_tcp_segment_init_sg(segment, sg_next(segment->sg), | ||
223 | 0); | ||
224 | iscsi_tcp_segment_map(segment, recv); | ||
225 | BUG_ON(segment->size == 0); | ||
226 | return 0; | ||
227 | } | ||
228 | |||
229 | /* Do we need to handle padding? */ | ||
230 | if (!(tcp_conn->iscsi_conn->session->tt->caps & CAP_PADDING_OFFLOAD)) { | ||
231 | pad = iscsi_padding(segment->total_copied); | ||
232 | if (pad != 0) { | ||
233 | debug_tcp("consume %d pad bytes\n", pad); | ||
234 | segment->total_size += pad; | ||
235 | segment->size = pad; | ||
236 | segment->data = padbuf; | ||
237 | return 0; | ||
238 | } | ||
239 | } | ||
240 | |||
241 | /* | ||
242 | * Set us up for transferring the data digest. hdr digest | ||
243 | * is completely handled in hdr done function. | ||
244 | */ | ||
245 | if (segment->hash) { | ||
246 | crypto_hash_final(segment->hash, segment->digest); | ||
247 | iscsi_tcp_segment_splice_digest(segment, | ||
248 | recv ? segment->recv_digest : segment->digest); | ||
249 | return 0; | ||
250 | } | ||
251 | |||
252 | return 1; | ||
253 | } | ||
254 | EXPORT_SYMBOL_GPL(iscsi_tcp_segment_done); | ||
255 | |||
256 | /** | ||
257 | * iscsi_tcp_segment_recv - copy data to segment | ||
258 | * @tcp_conn: the iSCSI TCP connection | ||
259 | * @segment: the buffer to copy to | ||
260 | * @ptr: data pointer | ||
261 | * @len: amount of data available | ||
262 | * | ||
263 | * This function copies up to @len bytes to the | ||
264 | * given buffer, and returns the number of bytes | ||
265 | * consumed, which can actually be less than @len. | ||
266 | * | ||
267 | * If hash digest is enabled, the function will update the | ||
268 | * hash while copying. | ||
269 | * Combining these two operations doesn't buy us a lot (yet), | ||
270 | * but in the future we could implement combined copy+crc, | ||
271 | * just way we do for network layer checksums. | ||
272 | */ | ||
273 | static int | ||
274 | iscsi_tcp_segment_recv(struct iscsi_tcp_conn *tcp_conn, | ||
275 | struct iscsi_segment *segment, const void *ptr, | ||
276 | unsigned int len) | ||
277 | { | ||
278 | unsigned int copy = 0, copied = 0; | ||
279 | |||
280 | while (!iscsi_tcp_segment_done(tcp_conn, segment, 1, copy)) { | ||
281 | if (copied == len) { | ||
282 | debug_tcp("iscsi_tcp_segment_recv copied %d bytes\n", | ||
283 | len); | ||
284 | break; | ||
285 | } | ||
286 | |||
287 | copy = min(len - copied, segment->size - segment->copied); | ||
288 | debug_tcp("iscsi_tcp_segment_recv copying %d\n", copy); | ||
289 | memcpy(segment->data + segment->copied, ptr + copied, copy); | ||
290 | copied += copy; | ||
291 | } | ||
292 | return copied; | ||
293 | } | ||
294 | |||
295 | inline void | ||
296 | iscsi_tcp_dgst_header(struct hash_desc *hash, const void *hdr, size_t hdrlen, | ||
297 | unsigned char digest[ISCSI_DIGEST_SIZE]) | ||
298 | { | ||
299 | struct scatterlist sg; | ||
300 | |||
301 | sg_init_one(&sg, hdr, hdrlen); | ||
302 | crypto_hash_digest(hash, &sg, hdrlen, digest); | ||
303 | } | ||
304 | EXPORT_SYMBOL_GPL(iscsi_tcp_dgst_header); | ||
305 | |||
306 | static inline int | ||
307 | iscsi_tcp_dgst_verify(struct iscsi_tcp_conn *tcp_conn, | ||
308 | struct iscsi_segment *segment) | ||
309 | { | ||
310 | if (!segment->digest_len) | ||
311 | return 1; | ||
312 | |||
313 | if (memcmp(segment->recv_digest, segment->digest, | ||
314 | segment->digest_len)) { | ||
315 | debug_scsi("digest mismatch\n"); | ||
316 | return 0; | ||
317 | } | ||
318 | |||
319 | return 1; | ||
320 | } | ||
321 | |||
322 | /* | ||
323 | * Helper function to set up segment buffer | ||
324 | */ | ||
325 | static inline void | ||
326 | __iscsi_segment_init(struct iscsi_segment *segment, size_t size, | ||
327 | iscsi_segment_done_fn_t *done, struct hash_desc *hash) | ||
328 | { | ||
329 | memset(segment, 0, sizeof(*segment)); | ||
330 | segment->total_size = size; | ||
331 | segment->done = done; | ||
332 | |||
333 | if (hash) { | ||
334 | segment->hash = hash; | ||
335 | crypto_hash_init(hash); | ||
336 | } | ||
337 | } | ||
338 | |||
339 | inline void | ||
340 | iscsi_segment_init_linear(struct iscsi_segment *segment, void *data, | ||
341 | size_t size, iscsi_segment_done_fn_t *done, | ||
342 | struct hash_desc *hash) | ||
343 | { | ||
344 | __iscsi_segment_init(segment, size, done, hash); | ||
345 | segment->data = data; | ||
346 | segment->size = size; | ||
347 | } | ||
348 | EXPORT_SYMBOL_GPL(iscsi_segment_init_linear); | ||
349 | |||
350 | inline int | ||
351 | iscsi_segment_seek_sg(struct iscsi_segment *segment, | ||
352 | struct scatterlist *sg_list, unsigned int sg_count, | ||
353 | unsigned int offset, size_t size, | ||
354 | iscsi_segment_done_fn_t *done, struct hash_desc *hash) | ||
355 | { | ||
356 | struct scatterlist *sg; | ||
357 | unsigned int i; | ||
358 | |||
359 | debug_scsi("iscsi_segment_seek_sg offset %u size %llu\n", | ||
360 | offset, size); | ||
361 | __iscsi_segment_init(segment, size, done, hash); | ||
362 | for_each_sg(sg_list, sg, sg_count, i) { | ||
363 | debug_scsi("sg %d, len %u offset %u\n", i, sg->length, | ||
364 | sg->offset); | ||
365 | if (offset < sg->length) { | ||
366 | iscsi_tcp_segment_init_sg(segment, sg, offset); | ||
367 | return 0; | ||
368 | } | ||
369 | offset -= sg->length; | ||
370 | } | ||
371 | |||
372 | return ISCSI_ERR_DATA_OFFSET; | ||
373 | } | ||
374 | EXPORT_SYMBOL_GPL(iscsi_segment_seek_sg); | ||
375 | |||
376 | /** | ||
377 | * iscsi_tcp_hdr_recv_prep - prep segment for hdr reception | ||
378 | * @tcp_conn: iscsi connection to prep for | ||
379 | * | ||
380 | * This function always passes NULL for the hash argument, because when this | ||
381 | * function is called we do not yet know the final size of the header and want | ||
382 | * to delay the digest processing until we know that. | ||
383 | */ | ||
384 | void iscsi_tcp_hdr_recv_prep(struct iscsi_tcp_conn *tcp_conn) | ||
385 | { | ||
386 | debug_tcp("iscsi_tcp_hdr_recv_prep(%p%s)\n", tcp_conn, | ||
387 | tcp_conn->iscsi_conn->hdrdgst_en ? ", digest enabled" : ""); | ||
388 | iscsi_segment_init_linear(&tcp_conn->in.segment, | ||
389 | tcp_conn->in.hdr_buf, sizeof(struct iscsi_hdr), | ||
390 | iscsi_tcp_hdr_recv_done, NULL); | ||
391 | } | ||
392 | EXPORT_SYMBOL_GPL(iscsi_tcp_hdr_recv_prep); | ||
393 | |||
394 | /* | ||
395 | * Handle incoming reply to any other type of command | ||
396 | */ | ||
397 | static int | ||
398 | iscsi_tcp_data_recv_done(struct iscsi_tcp_conn *tcp_conn, | ||
399 | struct iscsi_segment *segment) | ||
400 | { | ||
401 | struct iscsi_conn *conn = tcp_conn->iscsi_conn; | ||
402 | int rc = 0; | ||
403 | |||
404 | if (!iscsi_tcp_dgst_verify(tcp_conn, segment)) | ||
405 | return ISCSI_ERR_DATA_DGST; | ||
406 | |||
407 | rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, | ||
408 | conn->data, tcp_conn->in.datalen); | ||
409 | if (rc) | ||
410 | return rc; | ||
411 | |||
412 | iscsi_tcp_hdr_recv_prep(tcp_conn); | ||
413 | return 0; | ||
414 | } | ||
415 | |||
416 | static void | ||
417 | iscsi_tcp_data_recv_prep(struct iscsi_tcp_conn *tcp_conn) | ||
418 | { | ||
419 | struct iscsi_conn *conn = tcp_conn->iscsi_conn; | ||
420 | struct hash_desc *rx_hash = NULL; | ||
421 | |||
422 | if (conn->datadgst_en & | ||
423 | !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD)) | ||
424 | rx_hash = tcp_conn->rx_hash; | ||
425 | |||
426 | iscsi_segment_init_linear(&tcp_conn->in.segment, | ||
427 | conn->data, tcp_conn->in.datalen, | ||
428 | iscsi_tcp_data_recv_done, rx_hash); | ||
429 | } | ||
430 | |||
431 | /** | ||
432 | * iscsi_tcp_cleanup_task - free tcp_task resources | ||
433 | * @task: iscsi task | ||
434 | * | ||
435 | * must be called with session lock | ||
436 | */ | ||
437 | void iscsi_tcp_cleanup_task(struct iscsi_task *task) | ||
438 | { | ||
439 | struct iscsi_tcp_task *tcp_task = task->dd_data; | ||
440 | struct iscsi_r2t_info *r2t; | ||
441 | |||
442 | /* nothing to do for mgmt or pending tasks */ | ||
443 | if (!task->sc || task->state == ISCSI_TASK_PENDING) | ||
444 | return; | ||
445 | |||
446 | /* flush task's r2t queues */ | ||
447 | while (__kfifo_get(tcp_task->r2tqueue, (void*)&r2t, sizeof(void*))) { | ||
448 | __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t, | ||
449 | sizeof(void*)); | ||
450 | debug_scsi("iscsi_tcp_cleanup_task pending r2t dropped\n"); | ||
451 | } | ||
452 | |||
453 | r2t = tcp_task->r2t; | ||
454 | if (r2t != NULL) { | ||
455 | __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t, | ||
456 | sizeof(void*)); | ||
457 | tcp_task->r2t = NULL; | ||
458 | } | ||
459 | } | ||
460 | EXPORT_SYMBOL_GPL(iscsi_tcp_cleanup_task); | ||
461 | |||
462 | /** | ||
463 | * iscsi_tcp_data_in - SCSI Data-In Response processing | ||
464 | * @conn: iscsi connection | ||
465 | * @task: scsi command task | ||
466 | */ | ||
467 | static int iscsi_tcp_data_in(struct iscsi_conn *conn, struct iscsi_task *task) | ||
468 | { | ||
469 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; | ||
470 | struct iscsi_tcp_task *tcp_task = task->dd_data; | ||
471 | struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr; | ||
472 | int datasn = be32_to_cpu(rhdr->datasn); | ||
473 | unsigned total_in_length = scsi_in(task->sc)->length; | ||
474 | |||
475 | iscsi_update_cmdsn(conn->session, (struct iscsi_nopin*)rhdr); | ||
476 | if (tcp_conn->in.datalen == 0) | ||
477 | return 0; | ||
478 | |||
479 | if (tcp_task->exp_datasn != datasn) { | ||
480 | debug_tcp("%s: task->exp_datasn(%d) != rhdr->datasn(%d)\n", | ||
481 | __func__, tcp_task->exp_datasn, datasn); | ||
482 | return ISCSI_ERR_DATASN; | ||
483 | } | ||
484 | |||
485 | tcp_task->exp_datasn++; | ||
486 | |||
487 | tcp_task->data_offset = be32_to_cpu(rhdr->offset); | ||
488 | if (tcp_task->data_offset + tcp_conn->in.datalen > total_in_length) { | ||
489 | debug_tcp("%s: data_offset(%d) + data_len(%d) > total_length_in(%d)\n", | ||
490 | __func__, tcp_task->data_offset, | ||
491 | tcp_conn->in.datalen, total_in_length); | ||
492 | return ISCSI_ERR_DATA_OFFSET; | ||
493 | } | ||
494 | |||
495 | conn->datain_pdus_cnt++; | ||
496 | return 0; | ||
497 | } | ||
498 | |||
499 | /** | ||
500 | * iscsi_tcp_r2t_rsp - iSCSI R2T Response processing | ||
501 | * @conn: iscsi connection | ||
502 | * @task: scsi command task | ||
503 | */ | ||
504 | static int iscsi_tcp_r2t_rsp(struct iscsi_conn *conn, struct iscsi_task *task) | ||
505 | { | ||
506 | struct iscsi_session *session = conn->session; | ||
507 | struct iscsi_tcp_task *tcp_task = task->dd_data; | ||
508 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; | ||
509 | struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr; | ||
510 | struct iscsi_r2t_info *r2t; | ||
511 | int r2tsn = be32_to_cpu(rhdr->r2tsn); | ||
512 | int rc; | ||
513 | |||
514 | if (tcp_conn->in.datalen) { | ||
515 | iscsi_conn_printk(KERN_ERR, conn, | ||
516 | "invalid R2t with datalen %d\n", | ||
517 | tcp_conn->in.datalen); | ||
518 | return ISCSI_ERR_DATALEN; | ||
519 | } | ||
520 | |||
521 | if (tcp_task->exp_datasn != r2tsn){ | ||
522 | debug_tcp("%s: task->exp_datasn(%d) != rhdr->r2tsn(%d)\n", | ||
523 | __func__, tcp_task->exp_datasn, r2tsn); | ||
524 | return ISCSI_ERR_R2TSN; | ||
525 | } | ||
526 | |||
527 | /* fill-in new R2T associated with the task */ | ||
528 | iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr); | ||
529 | |||
530 | if (!task->sc || session->state != ISCSI_STATE_LOGGED_IN) { | ||
531 | iscsi_conn_printk(KERN_INFO, conn, | ||
532 | "dropping R2T itt %d in recovery.\n", | ||
533 | task->itt); | ||
534 | return 0; | ||
535 | } | ||
536 | |||
537 | rc = __kfifo_get(tcp_task->r2tpool.queue, (void*)&r2t, sizeof(void*)); | ||
538 | if (!rc) { | ||
539 | iscsi_conn_printk(KERN_ERR, conn, "Could not allocate R2T. " | ||
540 | "Target has sent more R2Ts than it " | ||
541 | "negotiated for or driver has has leaked.\n"); | ||
542 | return ISCSI_ERR_PROTO; | ||
543 | } | ||
544 | |||
545 | r2t->exp_statsn = rhdr->statsn; | ||
546 | r2t->data_length = be32_to_cpu(rhdr->data_length); | ||
547 | if (r2t->data_length == 0) { | ||
548 | iscsi_conn_printk(KERN_ERR, conn, | ||
549 | "invalid R2T with zero data len\n"); | ||
550 | __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t, | ||
551 | sizeof(void*)); | ||
552 | return ISCSI_ERR_DATALEN; | ||
553 | } | ||
554 | |||
555 | if (r2t->data_length > session->max_burst) | ||
556 | debug_scsi("invalid R2T with data len %u and max burst %u." | ||
557 | "Attempting to execute request.\n", | ||
558 | r2t->data_length, session->max_burst); | ||
559 | |||
560 | r2t->data_offset = be32_to_cpu(rhdr->data_offset); | ||
561 | if (r2t->data_offset + r2t->data_length > scsi_out(task->sc)->length) { | ||
562 | iscsi_conn_printk(KERN_ERR, conn, | ||
563 | "invalid R2T with data len %u at offset %u " | ||
564 | "and total length %d\n", r2t->data_length, | ||
565 | r2t->data_offset, scsi_out(task->sc)->length); | ||
566 | __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t, | ||
567 | sizeof(void*)); | ||
568 | return ISCSI_ERR_DATALEN; | ||
569 | } | ||
570 | |||
571 | r2t->ttt = rhdr->ttt; /* no flip */ | ||
572 | r2t->datasn = 0; | ||
573 | r2t->sent = 0; | ||
574 | |||
575 | tcp_task->exp_datasn = r2tsn + 1; | ||
576 | __kfifo_put(tcp_task->r2tqueue, (void*)&r2t, sizeof(void*)); | ||
577 | conn->r2t_pdus_cnt++; | ||
578 | |||
579 | iscsi_requeue_task(task); | ||
580 | return 0; | ||
581 | } | ||
582 | |||
583 | /* | ||
584 | * Handle incoming reply to DataIn command | ||
585 | */ | ||
586 | static int | ||
587 | iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn, | ||
588 | struct iscsi_segment *segment) | ||
589 | { | ||
590 | struct iscsi_conn *conn = tcp_conn->iscsi_conn; | ||
591 | struct iscsi_hdr *hdr = tcp_conn->in.hdr; | ||
592 | int rc; | ||
593 | |||
594 | if (!iscsi_tcp_dgst_verify(tcp_conn, segment)) | ||
595 | return ISCSI_ERR_DATA_DGST; | ||
596 | |||
597 | /* check for non-exceptional status */ | ||
598 | if (hdr->flags & ISCSI_FLAG_DATA_STATUS) { | ||
599 | rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0); | ||
600 | if (rc) | ||
601 | return rc; | ||
602 | } | ||
603 | |||
604 | iscsi_tcp_hdr_recv_prep(tcp_conn); | ||
605 | return 0; | ||
606 | } | ||
607 | |||
608 | /** | ||
609 | * iscsi_tcp_hdr_dissect - process PDU header | ||
610 | * @conn: iSCSI connection | ||
611 | * @hdr: PDU header | ||
612 | * | ||
613 | * This function analyzes the header of the PDU received, | ||
614 | * and performs several sanity checks. If the PDU is accompanied | ||
615 | * by data, the receive buffer is set up to copy the incoming data | ||
616 | * to the correct location. | ||
617 | */ | ||
618 | static int | ||
619 | iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr) | ||
620 | { | ||
621 | int rc = 0, opcode, ahslen; | ||
622 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; | ||
623 | struct iscsi_task *task; | ||
624 | |||
625 | /* verify PDU length */ | ||
626 | tcp_conn->in.datalen = ntoh24(hdr->dlength); | ||
627 | if (tcp_conn->in.datalen > conn->max_recv_dlength) { | ||
628 | iscsi_conn_printk(KERN_ERR, conn, | ||
629 | "iscsi_tcp: datalen %d > %d\n", | ||
630 | tcp_conn->in.datalen, conn->max_recv_dlength); | ||
631 | return ISCSI_ERR_DATALEN; | ||
632 | } | ||
633 | |||
634 | /* Additional header segments. So far, we don't | ||
635 | * process additional headers. | ||
636 | */ | ||
637 | ahslen = hdr->hlength << 2; | ||
638 | |||
639 | opcode = hdr->opcode & ISCSI_OPCODE_MASK; | ||
640 | /* verify itt (itt encoding: age+cid+itt) */ | ||
641 | rc = iscsi_verify_itt(conn, hdr->itt); | ||
642 | if (rc) | ||
643 | return rc; | ||
644 | |||
645 | debug_tcp("opcode 0x%x ahslen %d datalen %d\n", | ||
646 | opcode, ahslen, tcp_conn->in.datalen); | ||
647 | |||
648 | switch(opcode) { | ||
649 | case ISCSI_OP_SCSI_DATA_IN: | ||
650 | spin_lock(&conn->session->lock); | ||
651 | task = iscsi_itt_to_ctask(conn, hdr->itt); | ||
652 | if (!task) | ||
653 | rc = ISCSI_ERR_BAD_ITT; | ||
654 | else | ||
655 | rc = iscsi_tcp_data_in(conn, task); | ||
656 | if (rc) { | ||
657 | spin_unlock(&conn->session->lock); | ||
658 | break; | ||
659 | } | ||
660 | |||
661 | if (tcp_conn->in.datalen) { | ||
662 | struct iscsi_tcp_task *tcp_task = task->dd_data; | ||
663 | struct hash_desc *rx_hash = NULL; | ||
664 | struct scsi_data_buffer *sdb = scsi_in(task->sc); | ||
665 | |||
666 | /* | ||
667 | * Setup copy of Data-In into the Scsi_Cmnd | ||
668 | * Scatterlist case: | ||
669 | * We set up the iscsi_segment to point to the next | ||
670 | * scatterlist entry to copy to. As we go along, | ||
671 | * we move on to the next scatterlist entry and | ||
672 | * update the digest per-entry. | ||
673 | */ | ||
674 | if (conn->datadgst_en && | ||
675 | !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD)) | ||
676 | rx_hash = tcp_conn->rx_hash; | ||
677 | |||
678 | debug_tcp("iscsi_tcp_begin_data_in(%p, offset=%d, " | ||
679 | "datalen=%d)\n", tcp_conn, | ||
680 | tcp_task->data_offset, | ||
681 | tcp_conn->in.datalen); | ||
682 | rc = iscsi_segment_seek_sg(&tcp_conn->in.segment, | ||
683 | sdb->table.sgl, | ||
684 | sdb->table.nents, | ||
685 | tcp_task->data_offset, | ||
686 | tcp_conn->in.datalen, | ||
687 | iscsi_tcp_process_data_in, | ||
688 | rx_hash); | ||
689 | spin_unlock(&conn->session->lock); | ||
690 | return rc; | ||
691 | } | ||
692 | rc = __iscsi_complete_pdu(conn, hdr, NULL, 0); | ||
693 | spin_unlock(&conn->session->lock); | ||
694 | break; | ||
695 | case ISCSI_OP_SCSI_CMD_RSP: | ||
696 | if (tcp_conn->in.datalen) { | ||
697 | iscsi_tcp_data_recv_prep(tcp_conn); | ||
698 | return 0; | ||
699 | } | ||
700 | rc = iscsi_complete_pdu(conn, hdr, NULL, 0); | ||
701 | break; | ||
702 | case ISCSI_OP_R2T: | ||
703 | spin_lock(&conn->session->lock); | ||
704 | task = iscsi_itt_to_ctask(conn, hdr->itt); | ||
705 | if (!task) | ||
706 | rc = ISCSI_ERR_BAD_ITT; | ||
707 | else if (ahslen) | ||
708 | rc = ISCSI_ERR_AHSLEN; | ||
709 | else if (task->sc->sc_data_direction == DMA_TO_DEVICE) | ||
710 | rc = iscsi_tcp_r2t_rsp(conn, task); | ||
711 | else | ||
712 | rc = ISCSI_ERR_PROTO; | ||
713 | spin_unlock(&conn->session->lock); | ||
714 | break; | ||
715 | case ISCSI_OP_LOGIN_RSP: | ||
716 | case ISCSI_OP_TEXT_RSP: | ||
717 | case ISCSI_OP_REJECT: | ||
718 | case ISCSI_OP_ASYNC_EVENT: | ||
719 | /* | ||
720 | * It is possible that we could get a PDU with a buffer larger | ||
721 | * than 8K, but there are no targets that currently do this. | ||
722 | * For now we fail until we find a vendor that needs it | ||
723 | */ | ||
724 | if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) { | ||
725 | iscsi_conn_printk(KERN_ERR, conn, | ||
726 | "iscsi_tcp: received buffer of " | ||
727 | "len %u but conn buffer is only %u " | ||
728 | "(opcode %0x)\n", | ||
729 | tcp_conn->in.datalen, | ||
730 | ISCSI_DEF_MAX_RECV_SEG_LEN, opcode); | ||
731 | rc = ISCSI_ERR_PROTO; | ||
732 | break; | ||
733 | } | ||
734 | |||
735 | /* If there's data coming in with the response, | ||
736 | * receive it to the connection's buffer. | ||
737 | */ | ||
738 | if (tcp_conn->in.datalen) { | ||
739 | iscsi_tcp_data_recv_prep(tcp_conn); | ||
740 | return 0; | ||
741 | } | ||
742 | /* fall through */ | ||
743 | case ISCSI_OP_LOGOUT_RSP: | ||
744 | case ISCSI_OP_NOOP_IN: | ||
745 | case ISCSI_OP_SCSI_TMFUNC_RSP: | ||
746 | rc = iscsi_complete_pdu(conn, hdr, NULL, 0); | ||
747 | break; | ||
748 | default: | ||
749 | rc = ISCSI_ERR_BAD_OPCODE; | ||
750 | break; | ||
751 | } | ||
752 | |||
753 | if (rc == 0) { | ||
754 | /* Anything that comes with data should have | ||
755 | * been handled above. */ | ||
756 | if (tcp_conn->in.datalen) | ||
757 | return ISCSI_ERR_PROTO; | ||
758 | iscsi_tcp_hdr_recv_prep(tcp_conn); | ||
759 | } | ||
760 | |||
761 | return rc; | ||
762 | } | ||
763 | |||
764 | /** | ||
765 | * iscsi_tcp_hdr_recv_done - process PDU header | ||
766 | * | ||
767 | * This is the callback invoked when the PDU header has | ||
768 | * been received. If the header is followed by additional | ||
769 | * header segments, we go back for more data. | ||
770 | */ | ||
771 | static int | ||
772 | iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn, | ||
773 | struct iscsi_segment *segment) | ||
774 | { | ||
775 | struct iscsi_conn *conn = tcp_conn->iscsi_conn; | ||
776 | struct iscsi_hdr *hdr; | ||
777 | |||
778 | /* Check if there are additional header segments | ||
779 | * *prior* to computing the digest, because we | ||
780 | * may need to go back to the caller for more. | ||
781 | */ | ||
782 | hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf; | ||
783 | if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) { | ||
784 | /* Bump the header length - the caller will | ||
785 | * just loop around and get the AHS for us, and | ||
786 | * call again. */ | ||
787 | unsigned int ahslen = hdr->hlength << 2; | ||
788 | |||
789 | /* Make sure we don't overflow */ | ||
790 | if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf)) | ||
791 | return ISCSI_ERR_AHSLEN; | ||
792 | |||
793 | segment->total_size += ahslen; | ||
794 | segment->size += ahslen; | ||
795 | return 0; | ||
796 | } | ||
797 | |||
798 | /* We're done processing the header. See if we're doing | ||
799 | * header digests; if so, set up the recv_digest buffer | ||
800 | * and go back for more. */ | ||
801 | if (conn->hdrdgst_en && | ||
802 | !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD)) { | ||
803 | if (segment->digest_len == 0) { | ||
804 | /* | ||
805 | * Even if we offload the digest processing we | ||
806 | * splice it in so we can increment the skb/segment | ||
807 | * counters in preparation for the data segment. | ||
808 | */ | ||
809 | iscsi_tcp_segment_splice_digest(segment, | ||
810 | segment->recv_digest); | ||
811 | return 0; | ||
812 | } | ||
813 | |||
814 | iscsi_tcp_dgst_header(tcp_conn->rx_hash, hdr, | ||
815 | segment->total_copied - ISCSI_DIGEST_SIZE, | ||
816 | segment->digest); | ||
817 | |||
818 | if (!iscsi_tcp_dgst_verify(tcp_conn, segment)) | ||
819 | return ISCSI_ERR_HDR_DGST; | ||
820 | } | ||
821 | |||
822 | tcp_conn->in.hdr = hdr; | ||
823 | return iscsi_tcp_hdr_dissect(conn, hdr); | ||
824 | } | ||
825 | |||
826 | /** | ||
827 | * iscsi_tcp_recv_segment_is_hdr - tests if we are reading in a header | ||
828 | * @tcp_conn: iscsi tcp conn | ||
829 | * | ||
830 | * returns non zero if we are currently processing or setup to process | ||
831 | * a header. | ||
832 | */ | ||
833 | inline int iscsi_tcp_recv_segment_is_hdr(struct iscsi_tcp_conn *tcp_conn) | ||
834 | { | ||
835 | return tcp_conn->in.segment.done == iscsi_tcp_hdr_recv_done; | ||
836 | } | ||
837 | EXPORT_SYMBOL_GPL(iscsi_tcp_recv_segment_is_hdr); | ||
838 | |||
839 | /** | ||
840 | * iscsi_tcp_recv_skb - Process skb | ||
841 | * @conn: iscsi connection | ||
842 | * @skb: network buffer with header and/or data segment | ||
843 | * @offset: offset in skb | ||
844 | * @offload: bool indicating if transfer was offloaded | ||
845 | * | ||
846 | * Will return status of transfer in status. And will return | ||
847 | * number of bytes copied. | ||
848 | */ | ||
849 | int iscsi_tcp_recv_skb(struct iscsi_conn *conn, struct sk_buff *skb, | ||
850 | unsigned int offset, bool offloaded, int *status) | ||
851 | { | ||
852 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; | ||
853 | struct iscsi_segment *segment = &tcp_conn->in.segment; | ||
854 | struct skb_seq_state seq; | ||
855 | unsigned int consumed = 0; | ||
856 | int rc = 0; | ||
857 | |||
858 | debug_tcp("in %d bytes\n", skb->len - offset); | ||
859 | |||
860 | if (unlikely(conn->suspend_rx)) { | ||
861 | debug_tcp("conn %d Rx suspended!\n", conn->id); | ||
862 | *status = ISCSI_TCP_SUSPENDED; | ||
863 | return 0; | ||
864 | } | ||
865 | |||
866 | if (offloaded) { | ||
867 | segment->total_copied = segment->total_size; | ||
868 | goto segment_done; | ||
869 | } | ||
870 | |||
871 | skb_prepare_seq_read(skb, offset, skb->len, &seq); | ||
872 | while (1) { | ||
873 | unsigned int avail; | ||
874 | const u8 *ptr; | ||
875 | |||
876 | avail = skb_seq_read(consumed, &ptr, &seq); | ||
877 | if (avail == 0) { | ||
878 | debug_tcp("no more data avail. Consumed %d\n", | ||
879 | consumed); | ||
880 | *status = ISCSI_TCP_SKB_DONE; | ||
881 | skb_abort_seq_read(&seq); | ||
882 | goto skb_done; | ||
883 | } | ||
884 | BUG_ON(segment->copied >= segment->size); | ||
885 | |||
886 | debug_tcp("skb %p ptr=%p avail=%u\n", skb, ptr, avail); | ||
887 | rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail); | ||
888 | BUG_ON(rc == 0); | ||
889 | consumed += rc; | ||
890 | |||
891 | if (segment->total_copied >= segment->total_size) { | ||
892 | skb_abort_seq_read(&seq); | ||
893 | goto segment_done; | ||
894 | } | ||
895 | } | ||
896 | |||
897 | segment_done: | ||
898 | *status = ISCSI_TCP_SEGMENT_DONE; | ||
899 | debug_tcp("segment done\n"); | ||
900 | rc = segment->done(tcp_conn, segment); | ||
901 | if (rc != 0) { | ||
902 | *status = ISCSI_TCP_CONN_ERR; | ||
903 | debug_tcp("Error receiving PDU, errno=%d\n", rc); | ||
904 | iscsi_conn_failure(conn, rc); | ||
905 | return 0; | ||
906 | } | ||
907 | /* The done() functions sets up the next segment. */ | ||
908 | |||
909 | skb_done: | ||
910 | conn->rxdata_octets += consumed; | ||
911 | return consumed; | ||
912 | } | ||
913 | EXPORT_SYMBOL_GPL(iscsi_tcp_recv_skb); | ||
914 | |||
915 | /** | ||
916 | * iscsi_tcp_task_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands | ||
917 | * @conn: iscsi connection | ||
918 | * @task: scsi command task | ||
919 | * @sc: scsi command | ||
920 | */ | ||
921 | int iscsi_tcp_task_init(struct iscsi_task *task) | ||
922 | { | ||
923 | struct iscsi_tcp_task *tcp_task = task->dd_data; | ||
924 | struct iscsi_conn *conn = task->conn; | ||
925 | struct scsi_cmnd *sc = task->sc; | ||
926 | int err; | ||
927 | |||
928 | if (!sc) { | ||
929 | /* | ||
930 | * mgmt tasks do not have a scatterlist since they come | ||
931 | * in from the iscsi interface. | ||
932 | */ | ||
933 | debug_scsi("mtask deq [cid %d itt 0x%x]\n", conn->id, | ||
934 | task->itt); | ||
935 | |||
936 | return conn->session->tt->init_pdu(task, 0, task->data_count); | ||
937 | } | ||
938 | |||
939 | BUG_ON(__kfifo_len(tcp_task->r2tqueue)); | ||
940 | tcp_task->exp_datasn = 0; | ||
941 | |||
942 | /* Prepare PDU, optionally w/ immediate data */ | ||
943 | debug_scsi("task deq [cid %d itt 0x%x imm %d unsol %d]\n", | ||
944 | conn->id, task->itt, task->imm_count, | ||
945 | task->unsol_r2t.data_length); | ||
946 | |||
947 | err = conn->session->tt->init_pdu(task, 0, task->imm_count); | ||
948 | if (err) | ||
949 | return err; | ||
950 | task->imm_count = 0; | ||
951 | return 0; | ||
952 | } | ||
953 | EXPORT_SYMBOL_GPL(iscsi_tcp_task_init); | ||
954 | |||
955 | static struct iscsi_r2t_info *iscsi_tcp_get_curr_r2t(struct iscsi_task *task) | ||
956 | { | ||
957 | struct iscsi_session *session = task->conn->session; | ||
958 | struct iscsi_tcp_task *tcp_task = task->dd_data; | ||
959 | struct iscsi_r2t_info *r2t = NULL; | ||
960 | |||
961 | if (iscsi_task_has_unsol_data(task)) | ||
962 | r2t = &task->unsol_r2t; | ||
963 | else { | ||
964 | spin_lock_bh(&session->lock); | ||
965 | if (tcp_task->r2t) { | ||
966 | r2t = tcp_task->r2t; | ||
967 | /* Continue with this R2T? */ | ||
968 | if (r2t->data_length <= r2t->sent) { | ||
969 | debug_scsi(" done with r2t %p\n", r2t); | ||
970 | __kfifo_put(tcp_task->r2tpool.queue, | ||
971 | (void *)&tcp_task->r2t, | ||
972 | sizeof(void *)); | ||
973 | tcp_task->r2t = r2t = NULL; | ||
974 | } | ||
975 | } | ||
976 | |||
977 | if (r2t == NULL) { | ||
978 | __kfifo_get(tcp_task->r2tqueue, | ||
979 | (void *)&tcp_task->r2t, sizeof(void *)); | ||
980 | r2t = tcp_task->r2t; | ||
981 | } | ||
982 | spin_unlock_bh(&session->lock); | ||
983 | } | ||
984 | |||
985 | return r2t; | ||
986 | } | ||
987 | |||
988 | /** | ||
989 | * iscsi_tcp_task_xmit - xmit normal PDU task | ||
990 | * @task: iscsi command task | ||
991 | * | ||
992 | * We're expected to return 0 when everything was transmitted succesfully, | ||
993 | * -EAGAIN if there's still data in the queue, or != 0 for any other kind | ||
994 | * of error. | ||
995 | */ | ||
996 | int iscsi_tcp_task_xmit(struct iscsi_task *task) | ||
997 | { | ||
998 | struct iscsi_conn *conn = task->conn; | ||
999 | struct iscsi_session *session = conn->session; | ||
1000 | struct iscsi_r2t_info *r2t; | ||
1001 | int rc = 0; | ||
1002 | |||
1003 | flush: | ||
1004 | /* Flush any pending data first. */ | ||
1005 | rc = session->tt->xmit_pdu(task); | ||
1006 | if (rc < 0) | ||
1007 | return rc; | ||
1008 | |||
1009 | /* mgmt command */ | ||
1010 | if (!task->sc) { | ||
1011 | if (task->hdr->itt == RESERVED_ITT) | ||
1012 | iscsi_put_task(task); | ||
1013 | return 0; | ||
1014 | } | ||
1015 | |||
1016 | /* Are we done already? */ | ||
1017 | if (task->sc->sc_data_direction != DMA_TO_DEVICE) | ||
1018 | return 0; | ||
1019 | |||
1020 | r2t = iscsi_tcp_get_curr_r2t(task); | ||
1021 | if (r2t == NULL) { | ||
1022 | /* Waiting for more R2Ts to arrive. */ | ||
1023 | debug_tcp("no R2Ts yet\n"); | ||
1024 | return 0; | ||
1025 | } | ||
1026 | |||
1027 | rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_DATA_OUT); | ||
1028 | if (rc) | ||
1029 | return rc; | ||
1030 | iscsi_prep_data_out_pdu(task, r2t, (struct iscsi_data *) task->hdr); | ||
1031 | |||
1032 | debug_scsi("sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n", | ||
1033 | r2t, r2t->datasn - 1, task->hdr->itt, | ||
1034 | r2t->data_offset + r2t->sent, r2t->data_count); | ||
1035 | |||
1036 | rc = conn->session->tt->init_pdu(task, r2t->data_offset + r2t->sent, | ||
1037 | r2t->data_count); | ||
1038 | if (rc) | ||
1039 | return rc; | ||
1040 | r2t->sent += r2t->data_count; | ||
1041 | goto flush; | ||
1042 | } | ||
1043 | EXPORT_SYMBOL_GPL(iscsi_tcp_task_xmit); | ||
1044 | |||
1045 | struct iscsi_cls_conn * | ||
1046 | iscsi_tcp_conn_setup(struct iscsi_cls_session *cls_session, int dd_data_size, | ||
1047 | uint32_t conn_idx) | ||
1048 | |||
1049 | { | ||
1050 | struct iscsi_conn *conn; | ||
1051 | struct iscsi_cls_conn *cls_conn; | ||
1052 | struct iscsi_tcp_conn *tcp_conn; | ||
1053 | |||
1054 | cls_conn = iscsi_conn_setup(cls_session, sizeof(*tcp_conn), conn_idx); | ||
1055 | if (!cls_conn) | ||
1056 | return NULL; | ||
1057 | conn = cls_conn->dd_data; | ||
1058 | /* | ||
1059 | * due to strange issues with iser these are not set | ||
1060 | * in iscsi_conn_setup | ||
1061 | */ | ||
1062 | conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN; | ||
1063 | |||
1064 | tcp_conn = conn->dd_data; | ||
1065 | tcp_conn->iscsi_conn = conn; | ||
1066 | |||
1067 | tcp_conn->dd_data = kzalloc(dd_data_size, GFP_KERNEL); | ||
1068 | if (!tcp_conn->dd_data) { | ||
1069 | iscsi_conn_teardown(cls_conn); | ||
1070 | return NULL; | ||
1071 | } | ||
1072 | return cls_conn; | ||
1073 | } | ||
1074 | EXPORT_SYMBOL_GPL(iscsi_tcp_conn_setup); | ||
1075 | |||
1076 | void iscsi_tcp_conn_teardown(struct iscsi_cls_conn *cls_conn) | ||
1077 | { | ||
1078 | struct iscsi_conn *conn = cls_conn->dd_data; | ||
1079 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; | ||
1080 | |||
1081 | kfree(tcp_conn->dd_data); | ||
1082 | iscsi_conn_teardown(cls_conn); | ||
1083 | } | ||
1084 | EXPORT_SYMBOL_GPL(iscsi_tcp_conn_teardown); | ||
1085 | |||
1086 | int iscsi_tcp_r2tpool_alloc(struct iscsi_session *session) | ||
1087 | { | ||
1088 | int i; | ||
1089 | int cmd_i; | ||
1090 | |||
1091 | /* | ||
1092 | * initialize per-task: R2T pool and xmit queue | ||
1093 | */ | ||
1094 | for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) { | ||
1095 | struct iscsi_task *task = session->cmds[cmd_i]; | ||
1096 | struct iscsi_tcp_task *tcp_task = task->dd_data; | ||
1097 | |||
1098 | /* | ||
1099 | * pre-allocated x2 as much r2ts to handle race when | ||
1100 | * target acks DataOut faster than we data_xmit() queues | ||
1101 | * could replenish r2tqueue. | ||
1102 | */ | ||
1103 | |||
1104 | /* R2T pool */ | ||
1105 | if (iscsi_pool_init(&tcp_task->r2tpool, | ||
1106 | session->max_r2t * 2, NULL, | ||
1107 | sizeof(struct iscsi_r2t_info))) { | ||
1108 | goto r2t_alloc_fail; | ||
1109 | } | ||
1110 | |||
1111 | /* R2T xmit queue */ | ||
1112 | tcp_task->r2tqueue = kfifo_alloc( | ||
1113 | session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL); | ||
1114 | if (tcp_task->r2tqueue == ERR_PTR(-ENOMEM)) { | ||
1115 | iscsi_pool_free(&tcp_task->r2tpool); | ||
1116 | goto r2t_alloc_fail; | ||
1117 | } | ||
1118 | } | ||
1119 | |||
1120 | return 0; | ||
1121 | |||
1122 | r2t_alloc_fail: | ||
1123 | for (i = 0; i < cmd_i; i++) { | ||
1124 | struct iscsi_task *task = session->cmds[i]; | ||
1125 | struct iscsi_tcp_task *tcp_task = task->dd_data; | ||
1126 | |||
1127 | kfifo_free(tcp_task->r2tqueue); | ||
1128 | iscsi_pool_free(&tcp_task->r2tpool); | ||
1129 | } | ||
1130 | return -ENOMEM; | ||
1131 | } | ||
1132 | EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_alloc); | ||
1133 | |||
1134 | void iscsi_tcp_r2tpool_free(struct iscsi_session *session) | ||
1135 | { | ||
1136 | int i; | ||
1137 | |||
1138 | for (i = 0; i < session->cmds_max; i++) { | ||
1139 | struct iscsi_task *task = session->cmds[i]; | ||
1140 | struct iscsi_tcp_task *tcp_task = task->dd_data; | ||
1141 | |||
1142 | kfifo_free(tcp_task->r2tqueue); | ||
1143 | iscsi_pool_free(&tcp_task->r2tpool); | ||
1144 | } | ||
1145 | } | ||
1146 | EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_free); | ||
1147 | |||
1148 | void iscsi_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn, | ||
1149 | struct iscsi_stats *stats) | ||
1150 | { | ||
1151 | struct iscsi_conn *conn = cls_conn->dd_data; | ||
1152 | |||
1153 | stats->txdata_octets = conn->txdata_octets; | ||
1154 | stats->rxdata_octets = conn->rxdata_octets; | ||
1155 | stats->scsicmd_pdus = conn->scsicmd_pdus_cnt; | ||
1156 | stats->dataout_pdus = conn->dataout_pdus_cnt; | ||
1157 | stats->scsirsp_pdus = conn->scsirsp_pdus_cnt; | ||
1158 | stats->datain_pdus = conn->datain_pdus_cnt; | ||
1159 | stats->r2t_pdus = conn->r2t_pdus_cnt; | ||
1160 | stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt; | ||
1161 | stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt; | ||
1162 | } | ||
1163 | EXPORT_SYMBOL_GPL(iscsi_tcp_conn_get_stats); | ||