]>
Commit | Line | Data |
---|---|---|
7ba24713 AA |
1 | /* |
2 | * iSCSI Initiator over TCP/IP Data-Path | |
3 | * | |
4 | * Copyright (C) 2004 Dmitry Yusupov | |
5 | * Copyright (C) 2004 Alex Aizman | |
5bb0b55a MC |
6 | * Copyright (C) 2005 - 2006 Mike Christie |
7 | * Copyright (C) 2006 Red Hat, Inc. All rights reserved. | |
7ba24713 AA |
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> | |
4e7aba73 | 32 | #include <linux/file.h> |
7ba24713 AA |
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> | |
d1d81c01 | 40 | #include <scsi/scsi_device.h> |
7ba24713 AA |
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("Dmitry Yusupov <dmitry_yus@yahoo.com>, " | |
48 | "Alex Aizman <itn780@yahoo.com>"); | |
49 | MODULE_DESCRIPTION("iSCSI/TCP data-path"); | |
50 | MODULE_LICENSE("GPL"); | |
da32dd68 | 51 | #undef DEBUG_TCP |
7ba24713 AA |
52 | #define DEBUG_ASSERT |
53 | ||
54 | #ifdef DEBUG_TCP | |
5bb0b55a | 55 | #define debug_tcp(fmt...) printk(KERN_INFO "tcp: " fmt) |
7ba24713 AA |
56 | #else |
57 | #define debug_tcp(fmt...) | |
58 | #endif | |
59 | ||
7ba24713 AA |
60 | #ifndef DEBUG_ASSERT |
61 | #ifdef BUG_ON | |
62 | #undef BUG_ON | |
63 | #endif | |
64 | #define BUG_ON(expr) | |
65 | #endif | |
66 | ||
7ba24713 AA |
67 | static unsigned int iscsi_max_lun = 512; |
68 | module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO); | |
69 | ||
da32dd68 | 70 | static int iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn, |
a8ac6311 | 71 | struct iscsi_segment *segment); |
7ba24713 | 72 | |
da32dd68 | 73 | /* |
a8ac6311 | 74 | * Scatterlist handling: inside the iscsi_segment, we |
da32dd68 OK |
75 | * remember an index into the scatterlist, and set data/size |
76 | * to the current scatterlist entry. For highmem pages, we | |
77 | * kmap as needed. | |
78 | * | |
79 | * Note that the page is unmapped when we return from | |
80 | * TCP's data_ready handler, so we may end up mapping and | |
81 | * unmapping the same page repeatedly. The whole reason | |
82 | * for this is that we shouldn't keep the page mapped | |
83 | * outside the softirq. | |
84 | */ | |
85 | ||
86 | /** | |
a8ac6311 OK |
87 | * iscsi_tcp_segment_init_sg - init indicated scatterlist entry |
88 | * @segment: the buffer object | |
89 | * @sg: scatterlist | |
da32dd68 OK |
90 | * @offset: byte offset into that sg entry |
91 | * | |
a8ac6311 | 92 | * This function sets up the segment so that subsequent |
da32dd68 OK |
93 | * data is copied to the indicated sg entry, at the given |
94 | * offset. | |
95 | */ | |
96 | static inline void | |
a8ac6311 OK |
97 | iscsi_tcp_segment_init_sg(struct iscsi_segment *segment, |
98 | struct scatterlist *sg, unsigned int offset) | |
da32dd68 | 99 | { |
a8ac6311 OK |
100 | segment->sg = sg; |
101 | segment->sg_offset = offset; | |
102 | segment->size = min(sg->length - offset, | |
103 | segment->total_size - segment->total_copied); | |
104 | segment->data = NULL; | |
da32dd68 OK |
105 | } |
106 | ||
107 | /** | |
a8ac6311 OK |
108 | * iscsi_tcp_segment_map - map the current S/G page |
109 | * @segment: iscsi_segment | |
110 | * @recv: 1 if called from recv path | |
da32dd68 OK |
111 | * |
112 | * We only need to possibly kmap data if scatter lists are being used, | |
113 | * because the iscsi passthrough and internal IO paths will never use high | |
114 | * mem pages. | |
115 | */ | |
116 | static inline void | |
a8ac6311 | 117 | iscsi_tcp_segment_map(struct iscsi_segment *segment, int recv) |
da32dd68 OK |
118 | { |
119 | struct scatterlist *sg; | |
120 | ||
a8ac6311 | 121 | if (segment->data != NULL || !segment->sg) |
da32dd68 OK |
122 | return; |
123 | ||
a8ac6311 OK |
124 | sg = segment->sg; |
125 | BUG_ON(segment->sg_mapped); | |
da32dd68 | 126 | BUG_ON(sg->length == 0); |
a8ac6311 OK |
127 | |
128 | /* | |
129 | * If the page count is greater than one it is ok to send | |
130 | * to the network layer's zero copy send path. If not we | |
131 | * have to go the slow sendmsg path. We always map for the | |
132 | * recv path. | |
133 | */ | |
134 | if (page_count(sg_page(sg)) >= 1 && !recv) | |
135 | return; | |
136 | ||
137 | debug_tcp("iscsi_tcp_segment_map %s %p\n", recv ? "recv" : "xmit", | |
138 | segment); | |
139 | segment->sg_mapped = kmap_atomic(sg_page(sg), KM_SOFTIRQ0); | |
140 | segment->data = segment->sg_mapped + sg->offset + segment->sg_offset; | |
da32dd68 OK |
141 | } |
142 | ||
143 | static inline void | |
a8ac6311 | 144 | iscsi_tcp_segment_unmap(struct iscsi_segment *segment) |
da32dd68 | 145 | { |
a8ac6311 OK |
146 | debug_tcp("iscsi_tcp_segment_unmap %p\n", segment); |
147 | ||
148 | if (segment->sg_mapped) { | |
149 | debug_tcp("iscsi_tcp_segment_unmap valid\n"); | |
150 | kunmap_atomic(segment->sg_mapped, KM_SOFTIRQ0); | |
151 | segment->sg_mapped = NULL; | |
152 | segment->data = NULL; | |
da32dd68 OK |
153 | } |
154 | } | |
155 | ||
156 | /* | |
157 | * Splice the digest buffer into the buffer | |
158 | */ | |
159 | static inline void | |
a8ac6311 | 160 | iscsi_tcp_segment_splice_digest(struct iscsi_segment *segment, void *digest) |
da32dd68 | 161 | { |
a8ac6311 OK |
162 | segment->data = digest; |
163 | segment->digest_len = ISCSI_DIGEST_SIZE; | |
164 | segment->total_size += ISCSI_DIGEST_SIZE; | |
165 | segment->size = ISCSI_DIGEST_SIZE; | |
166 | segment->copied = 0; | |
167 | segment->sg = NULL; | |
168 | segment->hash = NULL; | |
da32dd68 OK |
169 | } |
170 | ||
171 | /** | |
a8ac6311 OK |
172 | * iscsi_tcp_segment_done - check whether the segment is complete |
173 | * @segment: iscsi segment to check | |
174 | * @recv: set to one of this is called from the recv path | |
175 | * @copied: number of bytes copied | |
da32dd68 | 176 | * |
a8ac6311 | 177 | * Check if we're done receiving this segment. If the receive |
da32dd68 OK |
178 | * buffer is full but we expect more data, move on to the |
179 | * next entry in the scatterlist. | |
180 | * | |
181 | * If the amount of data we received isn't a multiple of 4, | |
182 | * we will transparently receive the pad bytes, too. | |
183 | * | |
184 | * This function must be re-entrant. | |
185 | */ | |
7ba24713 | 186 | static inline int |
a8ac6311 | 187 | iscsi_tcp_segment_done(struct iscsi_segment *segment, int recv, unsigned copied) |
7ba24713 | 188 | { |
da32dd68 | 189 | static unsigned char padbuf[ISCSI_PAD_LEN]; |
a8ac6311 | 190 | struct scatterlist sg; |
004d6530 | 191 | unsigned int pad; |
da32dd68 | 192 | |
a8ac6311 OK |
193 | debug_tcp("copied %u %u size %u %s\n", segment->copied, copied, |
194 | segment->size, recv ? "recv" : "xmit"); | |
195 | if (segment->hash && copied) { | |
196 | /* | |
197 | * If a segment is kmapd we must unmap it before sending | |
198 | * to the crypto layer since that will try to kmap it again. | |
199 | */ | |
200 | iscsi_tcp_segment_unmap(segment); | |
201 | ||
202 | if (!segment->data) { | |
203 | sg_init_table(&sg, 1); | |
204 | sg_set_page(&sg, sg_page(segment->sg), copied, | |
205 | segment->copied + segment->sg_offset + | |
206 | segment->sg->offset); | |
207 | } else | |
208 | sg_init_one(&sg, segment->data + segment->copied, | |
209 | copied); | |
210 | crypto_hash_update(segment->hash, &sg, copied); | |
211 | } | |
212 | ||
213 | segment->copied += copied; | |
214 | if (segment->copied < segment->size) { | |
215 | iscsi_tcp_segment_map(segment, recv); | |
da32dd68 OK |
216 | return 0; |
217 | } | |
7ba24713 | 218 | |
a8ac6311 OK |
219 | segment->total_copied += segment->copied; |
220 | segment->copied = 0; | |
221 | segment->size = 0; | |
7ba24713 | 222 | |
da32dd68 | 223 | /* Unmap the current scatterlist page, if there is one. */ |
a8ac6311 | 224 | iscsi_tcp_segment_unmap(segment); |
da32dd68 OK |
225 | |
226 | /* Do we have more scatterlist entries? */ | |
a8ac6311 OK |
227 | debug_tcp("total copied %u total size %u\n", segment->total_copied, |
228 | segment->total_size); | |
229 | if (segment->total_copied < segment->total_size) { | |
da32dd68 | 230 | /* Proceed to the next entry in the scatterlist. */ |
a8ac6311 OK |
231 | iscsi_tcp_segment_init_sg(segment, sg_next(segment->sg), |
232 | 0); | |
233 | iscsi_tcp_segment_map(segment, recv); | |
234 | BUG_ON(segment->size == 0); | |
da32dd68 OK |
235 | return 0; |
236 | } | |
237 | ||
238 | /* Do we need to handle padding? */ | |
a8ac6311 | 239 | pad = iscsi_padding(segment->total_copied); |
004d6530 | 240 | if (pad != 0) { |
da32dd68 | 241 | debug_tcp("consume %d pad bytes\n", pad); |
a8ac6311 OK |
242 | segment->total_size += pad; |
243 | segment->size = pad; | |
244 | segment->data = padbuf; | |
da32dd68 OK |
245 | return 0; |
246 | } | |
247 | ||
248 | /* | |
a8ac6311 | 249 | * Set us up for transferring the data digest. hdr digest |
da32dd68 OK |
250 | * is completely handled in hdr done function. |
251 | */ | |
a8ac6311 OK |
252 | if (segment->hash) { |
253 | crypto_hash_final(segment->hash, segment->digest); | |
254 | iscsi_tcp_segment_splice_digest(segment, | |
255 | recv ? segment->recv_digest : segment->digest); | |
256 | return 0; | |
da32dd68 | 257 | } |
7ba24713 | 258 | |
da32dd68 OK |
259 | return 1; |
260 | } | |
7ba24713 | 261 | |
da32dd68 | 262 | /** |
a8ac6311 | 263 | * iscsi_tcp_xmit_segment - transmit segment |
da32dd68 | 264 | * @tcp_conn: the iSCSI TCP connection |
a8ac6311 OK |
265 | * @segment: the buffer to transmnit |
266 | * | |
267 | * This function transmits as much of the buffer as | |
268 | * the network layer will accept, and returns the number of | |
269 | * bytes transmitted. | |
270 | * | |
271 | * If CRC hashing is enabled, the function will compute the | |
272 | * hash as it goes. When the entire segment has been transmitted, | |
273 | * it will retrieve the hash value and send it as well. | |
274 | */ | |
275 | static int | |
276 | iscsi_tcp_xmit_segment(struct iscsi_tcp_conn *tcp_conn, | |
277 | struct iscsi_segment *segment) | |
278 | { | |
279 | struct socket *sk = tcp_conn->sock; | |
280 | unsigned int copied = 0; | |
281 | int r = 0; | |
282 | ||
283 | while (!iscsi_tcp_segment_done(segment, 0, r)) { | |
284 | struct scatterlist *sg; | |
285 | unsigned int offset, copy; | |
286 | int flags = 0; | |
287 | ||
288 | r = 0; | |
289 | offset = segment->copied; | |
290 | copy = segment->size - offset; | |
291 | ||
292 | if (segment->total_copied + segment->size < segment->total_size) | |
293 | flags |= MSG_MORE; | |
294 | ||
295 | /* Use sendpage if we can; else fall back to sendmsg */ | |
296 | if (!segment->data) { | |
297 | sg = segment->sg; | |
298 | offset += segment->sg_offset + sg->offset; | |
299 | r = tcp_conn->sendpage(sk, sg_page(sg), offset, copy, | |
300 | flags); | |
301 | } else { | |
302 | struct msghdr msg = { .msg_flags = flags }; | |
303 | struct kvec iov = { | |
304 | .iov_base = segment->data + offset, | |
305 | .iov_len = copy | |
306 | }; | |
307 | ||
308 | r = kernel_sendmsg(sk, &msg, &iov, 1, copy); | |
309 | } | |
310 | ||
311 | if (r < 0) { | |
312 | iscsi_tcp_segment_unmap(segment); | |
313 | if (copied || r == -EAGAIN) | |
314 | break; | |
315 | return r; | |
316 | } | |
317 | copied += r; | |
318 | } | |
319 | return copied; | |
320 | } | |
321 | ||
322 | /** | |
323 | * iscsi_tcp_segment_recv - copy data to segment | |
324 | * @tcp_conn: the iSCSI TCP connection | |
325 | * @segment: the buffer to copy to | |
da32dd68 OK |
326 | * @ptr: data pointer |
327 | * @len: amount of data available | |
328 | * | |
329 | * This function copies up to @len bytes to the | |
330 | * given buffer, and returns the number of bytes | |
331 | * consumed, which can actually be less than @len. | |
332 | * | |
333 | * If hash digest is enabled, the function will update the | |
334 | * hash while copying. | |
335 | * Combining these two operations doesn't buy us a lot (yet), | |
336 | * but in the future we could implement combined copy+crc, | |
337 | * just way we do for network layer checksums. | |
338 | */ | |
339 | static int | |
a8ac6311 OK |
340 | iscsi_tcp_segment_recv(struct iscsi_tcp_conn *tcp_conn, |
341 | struct iscsi_segment *segment, const void *ptr, | |
342 | unsigned int len) | |
da32dd68 | 343 | { |
a8ac6311 | 344 | unsigned int copy = 0, copied = 0; |
7ba24713 | 345 | |
a8ac6311 OK |
346 | while (!iscsi_tcp_segment_done(segment, 1, copy)) { |
347 | if (copied == len) { | |
348 | debug_tcp("iscsi_tcp_segment_recv copied %d bytes\n", | |
349 | len); | |
350 | break; | |
da32dd68 | 351 | } |
a8ac6311 OK |
352 | |
353 | copy = min(len - copied, segment->size - segment->copied); | |
354 | debug_tcp("iscsi_tcp_segment_recv copying %d\n", copy); | |
355 | memcpy(segment->data + segment->copied, ptr + copied, copy); | |
da32dd68 OK |
356 | copied += copy; |
357 | } | |
da32dd68 OK |
358 | return copied; |
359 | } | |
360 | ||
361 | static inline void | |
362 | iscsi_tcp_dgst_header(struct hash_desc *hash, const void *hdr, size_t hdrlen, | |
363 | unsigned char digest[ISCSI_DIGEST_SIZE]) | |
364 | { | |
365 | struct scatterlist sg; | |
7ba24713 | 366 | |
da32dd68 OK |
367 | sg_init_one(&sg, hdr, hdrlen); |
368 | crypto_hash_digest(hash, &sg, hdrlen, digest); | |
369 | } | |
370 | ||
371 | static inline int | |
372 | iscsi_tcp_dgst_verify(struct iscsi_tcp_conn *tcp_conn, | |
a8ac6311 | 373 | struct iscsi_segment *segment) |
da32dd68 | 374 | { |
a8ac6311 | 375 | if (!segment->digest_len) |
da32dd68 OK |
376 | return 1; |
377 | ||
a8ac6311 OK |
378 | if (memcmp(segment->recv_digest, segment->digest, |
379 | segment->digest_len)) { | |
da32dd68 OK |
380 | debug_scsi("digest mismatch\n"); |
381 | return 0; | |
382 | } | |
383 | ||
384 | return 1; | |
385 | } | |
386 | ||
387 | /* | |
a8ac6311 | 388 | * Helper function to set up segment buffer |
da32dd68 OK |
389 | */ |
390 | static inline void | |
a8ac6311 OK |
391 | __iscsi_segment_init(struct iscsi_segment *segment, size_t size, |
392 | iscsi_segment_done_fn_t *done, struct hash_desc *hash) | |
da32dd68 | 393 | { |
a8ac6311 OK |
394 | memset(segment, 0, sizeof(*segment)); |
395 | segment->total_size = size; | |
396 | segment->done = done; | |
da32dd68 OK |
397 | |
398 | if (hash) { | |
a8ac6311 | 399 | segment->hash = hash; |
da32dd68 OK |
400 | crypto_hash_init(hash); |
401 | } | |
402 | } | |
403 | ||
404 | static inline void | |
a8ac6311 OK |
405 | iscsi_segment_init_linear(struct iscsi_segment *segment, void *data, |
406 | size_t size, iscsi_segment_done_fn_t *done, | |
407 | struct hash_desc *hash) | |
da32dd68 | 408 | { |
a8ac6311 OK |
409 | __iscsi_segment_init(segment, size, done, hash); |
410 | segment->data = data; | |
411 | segment->size = size; | |
da32dd68 OK |
412 | } |
413 | ||
414 | static inline int | |
a8ac6311 OK |
415 | iscsi_segment_seek_sg(struct iscsi_segment *segment, |
416 | struct scatterlist *sg_list, unsigned int sg_count, | |
417 | unsigned int offset, size_t size, | |
418 | iscsi_segment_done_fn_t *done, struct hash_desc *hash) | |
da32dd68 | 419 | { |
a8ac6311 | 420 | struct scatterlist *sg; |
da32dd68 OK |
421 | unsigned int i; |
422 | ||
a8ac6311 OK |
423 | debug_scsi("iscsi_segment_seek_sg offset %u size %llu\n", |
424 | offset, size); | |
425 | __iscsi_segment_init(segment, size, done, hash); | |
426 | for_each_sg(sg_list, sg, sg_count, i) { | |
427 | debug_scsi("sg %d, len %u offset %u\n", i, sg->length, | |
428 | sg->offset); | |
429 | if (offset < sg->length) { | |
430 | iscsi_tcp_segment_init_sg(segment, sg, offset); | |
da32dd68 | 431 | return 0; |
7ba24713 | 432 | } |
a8ac6311 | 433 | offset -= sg->length; |
7ba24713 AA |
434 | } |
435 | ||
da32dd68 OK |
436 | return ISCSI_ERR_DATA_OFFSET; |
437 | } | |
438 | ||
439 | /** | |
a8ac6311 | 440 | * iscsi_tcp_hdr_recv_prep - prep segment for hdr reception |
da32dd68 OK |
441 | * @tcp_conn: iscsi connection to prep for |
442 | * | |
443 | * This function always passes NULL for the hash argument, because when this | |
444 | * function is called we do not yet know the final size of the header and want | |
445 | * to delay the digest processing until we know that. | |
446 | */ | |
447 | static void | |
448 | iscsi_tcp_hdr_recv_prep(struct iscsi_tcp_conn *tcp_conn) | |
449 | { | |
450 | debug_tcp("iscsi_tcp_hdr_recv_prep(%p%s)\n", tcp_conn, | |
451 | tcp_conn->iscsi_conn->hdrdgst_en ? ", digest enabled" : ""); | |
a8ac6311 | 452 | iscsi_segment_init_linear(&tcp_conn->in.segment, |
da32dd68 OK |
453 | tcp_conn->in.hdr_buf, sizeof(struct iscsi_hdr), |
454 | iscsi_tcp_hdr_recv_done, NULL); | |
455 | } | |
456 | ||
457 | /* | |
458 | * Handle incoming reply to any other type of command | |
459 | */ | |
460 | static int | |
461 | iscsi_tcp_data_recv_done(struct iscsi_tcp_conn *tcp_conn, | |
a8ac6311 | 462 | struct iscsi_segment *segment) |
da32dd68 OK |
463 | { |
464 | struct iscsi_conn *conn = tcp_conn->iscsi_conn; | |
465 | int rc = 0; | |
466 | ||
a8ac6311 | 467 | if (!iscsi_tcp_dgst_verify(tcp_conn, segment)) |
da32dd68 OK |
468 | return ISCSI_ERR_DATA_DGST; |
469 | ||
470 | rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, | |
471 | conn->data, tcp_conn->in.datalen); | |
472 | if (rc) | |
473 | return rc; | |
474 | ||
475 | iscsi_tcp_hdr_recv_prep(tcp_conn); | |
7ba24713 AA |
476 | return 0; |
477 | } | |
478 | ||
da32dd68 OK |
479 | static void |
480 | iscsi_tcp_data_recv_prep(struct iscsi_tcp_conn *tcp_conn) | |
481 | { | |
482 | struct iscsi_conn *conn = tcp_conn->iscsi_conn; | |
483 | struct hash_desc *rx_hash = NULL; | |
484 | ||
485 | if (conn->datadgst_en) | |
486 | rx_hash = &tcp_conn->rx_hash; | |
487 | ||
a8ac6311 | 488 | iscsi_segment_init_linear(&tcp_conn->in.segment, |
da32dd68 OK |
489 | conn->data, tcp_conn->in.datalen, |
490 | iscsi_tcp_data_recv_done, rx_hash); | |
491 | } | |
492 | ||
30a6c652 MC |
493 | /* |
494 | * must be called with session lock | |
495 | */ | |
496 | static void | |
b6c395ed | 497 | iscsi_tcp_cleanup_ctask(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) |
7ba24713 | 498 | { |
5bb0b55a | 499 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; |
b6c395ed | 500 | struct iscsi_r2t_info *r2t; |
7ba24713 | 501 | |
b6c395ed MC |
502 | /* flush ctask's r2t queues */ |
503 | while (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) { | |
504 | __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t, | |
505 | sizeof(void*)); | |
506 | debug_scsi("iscsi_tcp_cleanup_ctask pending r2t dropped\n"); | |
507 | } | |
508 | ||
a8ac6311 OK |
509 | r2t = tcp_ctask->r2t; |
510 | if (r2t != NULL) { | |
511 | __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t, | |
512 | sizeof(void*)); | |
513 | tcp_ctask->r2t = NULL; | |
514 | } | |
7ba24713 AA |
515 | } |
516 | ||
517 | /** | |
518 | * iscsi_data_rsp - SCSI Data-In Response processing | |
519 | * @conn: iscsi connection | |
520 | * @ctask: scsi command task | |
521 | **/ | |
522 | static int | |
523 | iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) | |
524 | { | |
5bb0b55a MC |
525 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
526 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; | |
527 | struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr; | |
7ba24713 | 528 | struct iscsi_session *session = conn->session; |
857ae0bd | 529 | struct scsi_cmnd *sc = ctask->sc; |
7ba24713 AA |
530 | int datasn = be32_to_cpu(rhdr->datasn); |
531 | ||
77a23c21 | 532 | iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr); |
5bb0b55a | 533 | if (tcp_conn->in.datalen == 0) |
7ba24713 AA |
534 | return 0; |
535 | ||
d473cc7f MC |
536 | if (tcp_ctask->exp_datasn != datasn) { |
537 | debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->datasn(%d)\n", | |
538 | __FUNCTION__, tcp_ctask->exp_datasn, datasn); | |
7ba24713 | 539 | return ISCSI_ERR_DATASN; |
d473cc7f | 540 | } |
7ba24713 | 541 | |
d473cc7f | 542 | tcp_ctask->exp_datasn++; |
7ba24713 | 543 | |
5bb0b55a | 544 | tcp_ctask->data_offset = be32_to_cpu(rhdr->offset); |
1c138991 | 545 | if (tcp_ctask->data_offset + tcp_conn->in.datalen > scsi_bufflen(sc)) { |
857ae0bd MC |
546 | debug_tcp("%s: data_offset(%d) + data_len(%d) > total_length_in(%d)\n", |
547 | __FUNCTION__, tcp_ctask->data_offset, | |
1c138991 | 548 | tcp_conn->in.datalen, scsi_bufflen(sc)); |
7ba24713 | 549 | return ISCSI_ERR_DATA_OFFSET; |
857ae0bd | 550 | } |
7ba24713 AA |
551 | |
552 | if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) { | |
7207fea4 | 553 | sc->result = (DID_OK << 16) | rhdr->cmd_status; |
7ba24713 | 554 | conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1; |
7207fea4 BH |
555 | if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW | |
556 | ISCSI_FLAG_DATA_OVERFLOW)) { | |
7ba24713 AA |
557 | int res_count = be32_to_cpu(rhdr->residual_count); |
558 | ||
559 | if (res_count > 0 && | |
7207fea4 BH |
560 | (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW || |
561 | res_count <= scsi_bufflen(sc))) | |
1c138991 | 562 | scsi_set_resid(sc, res_count); |
7207fea4 | 563 | else |
7ba24713 AA |
564 | sc->result = (DID_BAD_TARGET << 16) | |
565 | rhdr->cmd_status; | |
7207fea4 | 566 | } |
7ba24713 AA |
567 | } |
568 | ||
569 | conn->datain_pdus_cnt++; | |
570 | return 0; | |
571 | } | |
572 | ||
573 | /** | |
574 | * iscsi_solicit_data_init - initialize first Data-Out | |
575 | * @conn: iscsi connection | |
576 | * @ctask: scsi command task | |
577 | * @r2t: R2T info | |
578 | * | |
579 | * Notes: | |
580 | * Initialize first Data-Out within this R2T sequence and finds | |
581 | * proper data_offset within this SCSI command. | |
582 | * | |
583 | * This function is called with connection lock taken. | |
584 | **/ | |
585 | static void | |
586 | iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask, | |
587 | struct iscsi_r2t_info *r2t) | |
588 | { | |
589 | struct iscsi_data *hdr; | |
7ba24713 | 590 | |
ffbfe925 | 591 | hdr = &r2t->dtask.hdr; |
7ba24713 AA |
592 | memset(hdr, 0, sizeof(struct iscsi_data)); |
593 | hdr->ttt = r2t->ttt; | |
594 | hdr->datasn = cpu_to_be32(r2t->solicit_datasn); | |
595 | r2t->solicit_datasn++; | |
596 | hdr->opcode = ISCSI_OP_SCSI_DATA_OUT; | |
5bb0b55a MC |
597 | memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun)); |
598 | hdr->itt = ctask->hdr->itt; | |
7ba24713 AA |
599 | hdr->exp_statsn = r2t->exp_statsn; |
600 | hdr->offset = cpu_to_be32(r2t->data_offset); | |
601 | if (r2t->data_length > conn->max_xmit_dlength) { | |
602 | hton24(hdr->dlength, conn->max_xmit_dlength); | |
603 | r2t->data_count = conn->max_xmit_dlength; | |
604 | hdr->flags = 0; | |
605 | } else { | |
606 | hton24(hdr->dlength, r2t->data_length); | |
607 | r2t->data_count = r2t->data_length; | |
608 | hdr->flags = ISCSI_FLAG_CMD_FINAL; | |
609 | } | |
610 | conn->dataout_pdus_cnt++; | |
611 | ||
612 | r2t->sent = 0; | |
7ba24713 AA |
613 | } |
614 | ||
615 | /** | |
616 | * iscsi_r2t_rsp - iSCSI R2T Response processing | |
617 | * @conn: iscsi connection | |
618 | * @ctask: scsi command task | |
619 | **/ | |
620 | static int | |
621 | iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) | |
622 | { | |
623 | struct iscsi_r2t_info *r2t; | |
624 | struct iscsi_session *session = conn->session; | |
5bb0b55a MC |
625 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; |
626 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; | |
627 | struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr; | |
7ba24713 AA |
628 | int r2tsn = be32_to_cpu(rhdr->r2tsn); |
629 | int rc; | |
630 | ||
98a9416a MC |
631 | if (tcp_conn->in.datalen) { |
632 | printk(KERN_ERR "iscsi_tcp: invalid R2t with datalen %d\n", | |
633 | tcp_conn->in.datalen); | |
7ba24713 | 634 | return ISCSI_ERR_DATALEN; |
98a9416a | 635 | } |
7ba24713 | 636 | |
d473cc7f MC |
637 | if (tcp_ctask->exp_datasn != r2tsn){ |
638 | debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->r2tsn(%d)\n", | |
639 | __FUNCTION__, tcp_ctask->exp_datasn, r2tsn); | |
7ba24713 | 640 | return ISCSI_ERR_R2TSN; |
d473cc7f | 641 | } |
7ba24713 | 642 | |
7ba24713 AA |
643 | /* fill-in new R2T associated with the task */ |
644 | spin_lock(&session->lock); | |
77a23c21 MC |
645 | iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr); |
646 | ||
843c0a8a | 647 | if (!ctask->sc || session->state != ISCSI_STATE_LOGGED_IN) { |
7ba24713 AA |
648 | printk(KERN_INFO "iscsi_tcp: dropping R2T itt %d in " |
649 | "recovery...\n", ctask->itt); | |
650 | spin_unlock(&session->lock); | |
651 | return 0; | |
652 | } | |
b6c395ed | 653 | |
5bb0b55a | 654 | rc = __kfifo_get(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*)); |
7ba24713 AA |
655 | BUG_ON(!rc); |
656 | ||
657 | r2t->exp_statsn = rhdr->statsn; | |
658 | r2t->data_length = be32_to_cpu(rhdr->data_length); | |
98a9416a MC |
659 | if (r2t->data_length == 0) { |
660 | printk(KERN_ERR "iscsi_tcp: invalid R2T with zero data len\n"); | |
7ba24713 AA |
661 | spin_unlock(&session->lock); |
662 | return ISCSI_ERR_DATALEN; | |
663 | } | |
664 | ||
98a9416a MC |
665 | if (r2t->data_length > session->max_burst) |
666 | debug_scsi("invalid R2T with data len %u and max burst %u." | |
667 | "Attempting to execute request.\n", | |
668 | r2t->data_length, session->max_burst); | |
669 | ||
7ba24713 | 670 | r2t->data_offset = be32_to_cpu(rhdr->data_offset); |
1c138991 | 671 | if (r2t->data_offset + r2t->data_length > scsi_bufflen(ctask->sc)) { |
7ba24713 | 672 | spin_unlock(&session->lock); |
98a9416a MC |
673 | printk(KERN_ERR "iscsi_tcp: invalid R2T with data len %u at " |
674 | "offset %u and total length %d\n", r2t->data_length, | |
1c138991 | 675 | r2t->data_offset, scsi_bufflen(ctask->sc)); |
7ba24713 AA |
676 | return ISCSI_ERR_DATALEN; |
677 | } | |
678 | ||
679 | r2t->ttt = rhdr->ttt; /* no flip */ | |
680 | r2t->solicit_datasn = 0; | |
681 | ||
682 | iscsi_solicit_data_init(conn, ctask, r2t); | |
683 | ||
d473cc7f | 684 | tcp_ctask->exp_datasn = r2tsn + 1; |
5bb0b55a | 685 | __kfifo_put(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*)); |
7ba24713 | 686 | conn->r2t_pdus_cnt++; |
843c0a8a MC |
687 | |
688 | iscsi_requeue_ctask(ctask); | |
7ba24713 AA |
689 | spin_unlock(&session->lock); |
690 | ||
691 | return 0; | |
692 | } | |
693 | ||
da32dd68 OK |
694 | /* |
695 | * Handle incoming reply to DataIn command | |
696 | */ | |
697 | static int | |
698 | iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn, | |
a8ac6311 | 699 | struct iscsi_segment *segment) |
da32dd68 OK |
700 | { |
701 | struct iscsi_conn *conn = tcp_conn->iscsi_conn; | |
702 | struct iscsi_hdr *hdr = tcp_conn->in.hdr; | |
703 | int rc; | |
704 | ||
a8ac6311 | 705 | if (!iscsi_tcp_dgst_verify(tcp_conn, segment)) |
da32dd68 OK |
706 | return ISCSI_ERR_DATA_DGST; |
707 | ||
708 | /* check for non-exceptional status */ | |
709 | if (hdr->flags & ISCSI_FLAG_DATA_STATUS) { | |
710 | rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0); | |
711 | if (rc) | |
712 | return rc; | |
713 | } | |
714 | ||
715 | iscsi_tcp_hdr_recv_prep(tcp_conn); | |
716 | return 0; | |
717 | } | |
718 | ||
719 | /** | |
720 | * iscsi_tcp_hdr_dissect - process PDU header | |
721 | * @conn: iSCSI connection | |
722 | * @hdr: PDU header | |
723 | * | |
724 | * This function analyzes the header of the PDU received, | |
725 | * and performs several sanity checks. If the PDU is accompanied | |
726 | * by data, the receive buffer is set up to copy the incoming data | |
727 | * to the correct location. | |
728 | */ | |
7ba24713 | 729 | static int |
da32dd68 | 730 | iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr) |
7ba24713 | 731 | { |
5bb0b55a | 732 | int rc = 0, opcode, ahslen; |
7ba24713 | 733 | struct iscsi_session *session = conn->session; |
5bb0b55a | 734 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
da32dd68 OK |
735 | struct iscsi_cmd_task *ctask; |
736 | uint32_t itt; | |
7ba24713 AA |
737 | |
738 | /* verify PDU length */ | |
5bb0b55a MC |
739 | tcp_conn->in.datalen = ntoh24(hdr->dlength); |
740 | if (tcp_conn->in.datalen > conn->max_recv_dlength) { | |
7ba24713 | 741 | printk(KERN_ERR "iscsi_tcp: datalen %d > %d\n", |
5bb0b55a | 742 | tcp_conn->in.datalen, conn->max_recv_dlength); |
7ba24713 AA |
743 | return ISCSI_ERR_DATALEN; |
744 | } | |
7ba24713 | 745 | |
da32dd68 OK |
746 | /* Additional header segments. So far, we don't |
747 | * process additional headers. | |
748 | */ | |
5bb0b55a | 749 | ahslen = hdr->hlength << 2; |
7ba24713 | 750 | |
5bb0b55a | 751 | opcode = hdr->opcode & ISCSI_OPCODE_MASK; |
7ba24713 | 752 | /* verify itt (itt encoding: age+cid+itt) */ |
5bb0b55a MC |
753 | rc = iscsi_verify_itt(conn, hdr, &itt); |
754 | if (rc == ISCSI_ERR_NO_SCSI_CMD) { | |
da32dd68 | 755 | /* XXX: what does this do? */ |
5bb0b55a MC |
756 | tcp_conn->in.datalen = 0; /* force drop */ |
757 | return 0; | |
758 | } else if (rc) | |
759 | return rc; | |
7ba24713 | 760 | |
da32dd68 OK |
761 | debug_tcp("opcode 0x%x ahslen %d datalen %d\n", |
762 | opcode, ahslen, tcp_conn->in.datalen); | |
7ba24713 | 763 | |
5bb0b55a MC |
764 | switch(opcode) { |
765 | case ISCSI_OP_SCSI_DATA_IN: | |
da32dd68 OK |
766 | ctask = session->cmds[itt]; |
767 | rc = iscsi_data_rsp(conn, ctask); | |
275fd7d1 MC |
768 | if (rc) |
769 | return rc; | |
da32dd68 OK |
770 | if (tcp_conn->in.datalen) { |
771 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; | |
772 | struct hash_desc *rx_hash = NULL; | |
773 | ||
774 | /* | |
775 | * Setup copy of Data-In into the Scsi_Cmnd | |
776 | * Scatterlist case: | |
a8ac6311 | 777 | * We set up the iscsi_segment to point to the next |
da32dd68 OK |
778 | * scatterlist entry to copy to. As we go along, |
779 | * we move on to the next scatterlist entry and | |
780 | * update the digest per-entry. | |
781 | */ | |
782 | if (conn->datadgst_en) | |
783 | rx_hash = &tcp_conn->rx_hash; | |
784 | ||
785 | debug_tcp("iscsi_tcp_begin_data_in(%p, offset=%d, " | |
786 | "datalen=%d)\n", tcp_conn, | |
787 | tcp_ctask->data_offset, | |
788 | tcp_conn->in.datalen); | |
a8ac6311 OK |
789 | return iscsi_segment_seek_sg(&tcp_conn->in.segment, |
790 | scsi_sglist(ctask->sc), | |
791 | scsi_sg_count(ctask->sc), | |
792 | tcp_ctask->data_offset, | |
793 | tcp_conn->in.datalen, | |
794 | iscsi_tcp_process_data_in, | |
795 | rx_hash); | |
da32dd68 | 796 | } |
5bb0b55a MC |
797 | /* fall through */ |
798 | case ISCSI_OP_SCSI_CMD_RSP: | |
da32dd68 OK |
799 | if (tcp_conn->in.datalen) { |
800 | iscsi_tcp_data_recv_prep(tcp_conn); | |
801 | return 0; | |
802 | } | |
803 | rc = iscsi_complete_pdu(conn, hdr, NULL, 0); | |
5bb0b55a MC |
804 | break; |
805 | case ISCSI_OP_R2T: | |
da32dd68 | 806 | ctask = session->cmds[itt]; |
5bb0b55a MC |
807 | if (ahslen) |
808 | rc = ISCSI_ERR_AHSLEN; | |
da32dd68 OK |
809 | else if (ctask->sc->sc_data_direction == DMA_TO_DEVICE) |
810 | rc = iscsi_r2t_rsp(conn, ctask); | |
5bb0b55a MC |
811 | else |
812 | rc = ISCSI_ERR_PROTO; | |
813 | break; | |
814 | case ISCSI_OP_LOGIN_RSP: | |
815 | case ISCSI_OP_TEXT_RSP: | |
5bb0b55a MC |
816 | case ISCSI_OP_REJECT: |
817 | case ISCSI_OP_ASYNC_EVENT: | |
c8dc1e52 MC |
818 | /* |
819 | * It is possible that we could get a PDU with a buffer larger | |
820 | * than 8K, but there are no targets that currently do this. | |
821 | * For now we fail until we find a vendor that needs it | |
822 | */ | |
da32dd68 | 823 | if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) { |
c8dc1e52 MC |
824 | printk(KERN_ERR "iscsi_tcp: received buffer of len %u " |
825 | "but conn buffer is only %u (opcode %0x)\n", | |
826 | tcp_conn->in.datalen, | |
bf32ed33 | 827 | ISCSI_DEF_MAX_RECV_SEG_LEN, opcode); |
c8dc1e52 MC |
828 | rc = ISCSI_ERR_PROTO; |
829 | break; | |
830 | } | |
831 | ||
da32dd68 OK |
832 | /* If there's data coming in with the response, |
833 | * receive it to the connection's buffer. | |
834 | */ | |
835 | if (tcp_conn->in.datalen) { | |
836 | iscsi_tcp_data_recv_prep(tcp_conn); | |
837 | return 0; | |
838 | } | |
5bb0b55a | 839 | /* fall through */ |
c8dc1e52 MC |
840 | case ISCSI_OP_LOGOUT_RSP: |
841 | case ISCSI_OP_NOOP_IN: | |
5bb0b55a MC |
842 | case ISCSI_OP_SCSI_TMFUNC_RSP: |
843 | rc = iscsi_complete_pdu(conn, hdr, NULL, 0); | |
844 | break; | |
845 | default: | |
846 | rc = ISCSI_ERR_BAD_OPCODE; | |
847 | break; | |
848 | } | |
7ba24713 | 849 | |
da32dd68 OK |
850 | if (rc == 0) { |
851 | /* Anything that comes with data should have | |
852 | * been handled above. */ | |
853 | if (tcp_conn->in.datalen) | |
854 | return ISCSI_ERR_PROTO; | |
855 | iscsi_tcp_hdr_recv_prep(tcp_conn); | |
7ba24713 AA |
856 | } |
857 | ||
da32dd68 | 858 | return rc; |
7ba24713 AA |
859 | } |
860 | ||
da32dd68 OK |
861 | /** |
862 | * iscsi_tcp_hdr_recv_done - process PDU header | |
863 | * | |
864 | * This is the callback invoked when the PDU header has | |
865 | * been received. If the header is followed by additional | |
866 | * header segments, we go back for more data. | |
867 | */ | |
868 | static int | |
869 | iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn, | |
a8ac6311 | 870 | struct iscsi_segment *segment) |
7ba24713 | 871 | { |
da32dd68 OK |
872 | struct iscsi_conn *conn = tcp_conn->iscsi_conn; |
873 | struct iscsi_hdr *hdr; | |
7ba24713 | 874 | |
da32dd68 OK |
875 | /* Check if there are additional header segments |
876 | * *prior* to computing the digest, because we | |
877 | * may need to go back to the caller for more. | |
878 | */ | |
879 | hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf; | |
a8ac6311 | 880 | if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) { |
da32dd68 OK |
881 | /* Bump the header length - the caller will |
882 | * just loop around and get the AHS for us, and | |
883 | * call again. */ | |
884 | unsigned int ahslen = hdr->hlength << 2; | |
885 | ||
886 | /* Make sure we don't overflow */ | |
887 | if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf)) | |
888 | return ISCSI_ERR_AHSLEN; | |
889 | ||
a8ac6311 OK |
890 | segment->total_size += ahslen; |
891 | segment->size += ahslen; | |
da32dd68 | 892 | return 0; |
7ba24713 AA |
893 | } |
894 | ||
da32dd68 OK |
895 | /* We're done processing the header. See if we're doing |
896 | * header digests; if so, set up the recv_digest buffer | |
897 | * and go back for more. */ | |
898 | if (conn->hdrdgst_en) { | |
a8ac6311 OK |
899 | if (segment->digest_len == 0) { |
900 | iscsi_tcp_segment_splice_digest(segment, | |
901 | segment->recv_digest); | |
da32dd68 | 902 | return 0; |
7ba24713 | 903 | } |
da32dd68 | 904 | iscsi_tcp_dgst_header(&tcp_conn->rx_hash, hdr, |
a8ac6311 OK |
905 | segment->total_copied - ISCSI_DIGEST_SIZE, |
906 | segment->digest); | |
7ba24713 | 907 | |
a8ac6311 | 908 | if (!iscsi_tcp_dgst_verify(tcp_conn, segment)) |
da32dd68 | 909 | return ISCSI_ERR_HDR_DGST; |
7ba24713 | 910 | } |
da32dd68 OK |
911 | |
912 | tcp_conn->in.hdr = hdr; | |
913 | return iscsi_tcp_hdr_dissect(conn, hdr); | |
7ba24713 AA |
914 | } |
915 | ||
916 | /** | |
da32dd68 | 917 | * iscsi_tcp_recv - TCP receive in sendfile fashion |
7ba24713 AA |
918 | * @rd_desc: read descriptor |
919 | * @skb: socket buffer | |
920 | * @offset: offset in skb | |
921 | * @len: skb->len - offset | |
922 | **/ | |
923 | static int | |
da32dd68 OK |
924 | iscsi_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, |
925 | unsigned int offset, size_t len) | |
7ba24713 | 926 | { |
7ba24713 | 927 | struct iscsi_conn *conn = rd_desc->arg.data; |
5bb0b55a | 928 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
a8ac6311 | 929 | struct iscsi_segment *segment = &tcp_conn->in.segment; |
da32dd68 OK |
930 | struct skb_seq_state seq; |
931 | unsigned int consumed = 0; | |
932 | int rc = 0; | |
7ba24713 | 933 | |
da32dd68 | 934 | debug_tcp("in %d bytes\n", skb->len - offset); |
7ba24713 AA |
935 | |
936 | if (unlikely(conn->suspend_rx)) { | |
937 | debug_tcp("conn %d Rx suspended!\n", conn->id); | |
938 | return 0; | |
939 | } | |
940 | ||
da32dd68 OK |
941 | skb_prepare_seq_read(skb, offset, skb->len, &seq); |
942 | while (1) { | |
943 | unsigned int avail; | |
944 | const u8 *ptr; | |
7ba24713 | 945 | |
da32dd68 | 946 | avail = skb_seq_read(consumed, &ptr, &seq); |
a8ac6311 OK |
947 | if (avail == 0) { |
948 | debug_tcp("no more data avail. Consumed %d\n", | |
949 | consumed); | |
da32dd68 | 950 | break; |
a8ac6311 OK |
951 | } |
952 | BUG_ON(segment->copied >= segment->size); | |
da32dd68 OK |
953 | |
954 | debug_tcp("skb %p ptr=%p avail=%u\n", skb, ptr, avail); | |
a8ac6311 | 955 | rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail); |
da32dd68 OK |
956 | BUG_ON(rc == 0); |
957 | consumed += rc; | |
958 | ||
a8ac6311 OK |
959 | if (segment->total_copied >= segment->total_size) { |
960 | debug_tcp("segment done\n"); | |
961 | rc = segment->done(tcp_conn, segment); | |
da32dd68 OK |
962 | if (rc != 0) { |
963 | skb_abort_seq_read(&seq); | |
964 | goto error; | |
dbdb016d | 965 | } |
7ba24713 | 966 | |
da32dd68 | 967 | /* The done() functions sets up the |
a8ac6311 | 968 | * next segment. */ |
7ba24713 | 969 | } |
7ba24713 | 970 | } |
a8ac6311 | 971 | skb_abort_seq_read(&seq); |
da32dd68 OK |
972 | conn->rxdata_octets += consumed; |
973 | return consumed; | |
7ba24713 | 974 | |
da32dd68 OK |
975 | error: |
976 | debug_tcp("Error receiving PDU, errno=%d\n", rc); | |
977 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); | |
978 | return 0; | |
7ba24713 AA |
979 | } |
980 | ||
981 | static void | |
982 | iscsi_tcp_data_ready(struct sock *sk, int flag) | |
983 | { | |
984 | struct iscsi_conn *conn = sk->sk_user_data; | |
da32dd68 | 985 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
7ba24713 AA |
986 | read_descriptor_t rd_desc; |
987 | ||
988 | read_lock(&sk->sk_callback_lock); | |
989 | ||
665b44ae | 990 | /* |
da32dd68 | 991 | * Use rd_desc to pass 'conn' to iscsi_tcp_recv. |
665b44ae | 992 | * We set count to 1 because we want the network layer to |
da32dd68 | 993 | * hand us all the skbs that are available. iscsi_tcp_recv |
665b44ae MC |
994 | * handled pdus that cross buffers or pdus that still need data. |
995 | */ | |
7ba24713 | 996 | rd_desc.arg.data = conn; |
665b44ae | 997 | rd_desc.count = 1; |
da32dd68 | 998 | tcp_read_sock(sk, &rd_desc, iscsi_tcp_recv); |
7ba24713 AA |
999 | |
1000 | read_unlock(&sk->sk_callback_lock); | |
da32dd68 OK |
1001 | |
1002 | /* If we had to (atomically) map a highmem page, | |
1003 | * unmap it now. */ | |
a8ac6311 | 1004 | iscsi_tcp_segment_unmap(&tcp_conn->in.segment); |
7ba24713 AA |
1005 | } |
1006 | ||
1007 | static void | |
1008 | iscsi_tcp_state_change(struct sock *sk) | |
1009 | { | |
5bb0b55a | 1010 | struct iscsi_tcp_conn *tcp_conn; |
7ba24713 AA |
1011 | struct iscsi_conn *conn; |
1012 | struct iscsi_session *session; | |
1013 | void (*old_state_change)(struct sock *); | |
1014 | ||
1015 | read_lock(&sk->sk_callback_lock); | |
1016 | ||
1017 | conn = (struct iscsi_conn*)sk->sk_user_data; | |
1018 | session = conn->session; | |
1019 | ||
e6273993 MC |
1020 | if ((sk->sk_state == TCP_CLOSE_WAIT || |
1021 | sk->sk_state == TCP_CLOSE) && | |
1022 | !atomic_read(&sk->sk_rmem_alloc)) { | |
7ba24713 AA |
1023 | debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n"); |
1024 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); | |
1025 | } | |
1026 | ||
5bb0b55a MC |
1027 | tcp_conn = conn->dd_data; |
1028 | old_state_change = tcp_conn->old_state_change; | |
7ba24713 AA |
1029 | |
1030 | read_unlock(&sk->sk_callback_lock); | |
1031 | ||
1032 | old_state_change(sk); | |
1033 | } | |
1034 | ||
1035 | /** | |
1036 | * iscsi_write_space - Called when more output buffer space is available | |
1037 | * @sk: socket space is available for | |
1038 | **/ | |
1039 | static void | |
1040 | iscsi_write_space(struct sock *sk) | |
1041 | { | |
1042 | struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data; | |
5bb0b55a MC |
1043 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
1044 | ||
1045 | tcp_conn->old_write_space(sk); | |
7ba24713 | 1046 | debug_tcp("iscsi_write_space: cid %d\n", conn->id); |
55e3299d | 1047 | scsi_queue_work(conn->session->host, &conn->xmitwork); |
7ba24713 AA |
1048 | } |
1049 | ||
1050 | static void | |
1051 | iscsi_conn_set_callbacks(struct iscsi_conn *conn) | |
1052 | { | |
5bb0b55a MC |
1053 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
1054 | struct sock *sk = tcp_conn->sock->sk; | |
7ba24713 AA |
1055 | |
1056 | /* assign new callbacks */ | |
1057 | write_lock_bh(&sk->sk_callback_lock); | |
1058 | sk->sk_user_data = conn; | |
5bb0b55a MC |
1059 | tcp_conn->old_data_ready = sk->sk_data_ready; |
1060 | tcp_conn->old_state_change = sk->sk_state_change; | |
1061 | tcp_conn->old_write_space = sk->sk_write_space; | |
7ba24713 AA |
1062 | sk->sk_data_ready = iscsi_tcp_data_ready; |
1063 | sk->sk_state_change = iscsi_tcp_state_change; | |
1064 | sk->sk_write_space = iscsi_write_space; | |
1065 | write_unlock_bh(&sk->sk_callback_lock); | |
1066 | } | |
1067 | ||
1068 | static void | |
1c83469d | 1069 | iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn) |
7ba24713 | 1070 | { |
5bb0b55a | 1071 | struct sock *sk = tcp_conn->sock->sk; |
7ba24713 AA |
1072 | |
1073 | /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */ | |
1074 | write_lock_bh(&sk->sk_callback_lock); | |
1075 | sk->sk_user_data = NULL; | |
5bb0b55a MC |
1076 | sk->sk_data_ready = tcp_conn->old_data_ready; |
1077 | sk->sk_state_change = tcp_conn->old_state_change; | |
1078 | sk->sk_write_space = tcp_conn->old_write_space; | |
7ba24713 AA |
1079 | sk->sk_no_check = 0; |
1080 | write_unlock_bh(&sk->sk_callback_lock); | |
1081 | } | |
1082 | ||
1083 | /** | |
a8ac6311 OK |
1084 | * iscsi_xmit - TCP transmit |
1085 | **/ | |
1086 | static int | |
1087 | iscsi_xmit(struct iscsi_conn *conn) | |
7ba24713 | 1088 | { |
5bb0b55a | 1089 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
a8ac6311 OK |
1090 | struct iscsi_segment *segment = &tcp_conn->out.segment; |
1091 | unsigned int consumed = 0; | |
1092 | int rc = 0; | |
7ba24713 | 1093 | |
a8ac6311 OK |
1094 | while (1) { |
1095 | rc = iscsi_tcp_xmit_segment(tcp_conn, segment); | |
1096 | if (rc < 0) | |
1097 | goto error; | |
1098 | if (rc == 0) | |
1099 | break; | |
1100 | ||
1101 | consumed += rc; | |
1102 | ||
1103 | if (segment->total_copied >= segment->total_size) { | |
1104 | if (segment->done != NULL) { | |
1105 | rc = segment->done(tcp_conn, segment); | |
1106 | if (rc < 0) | |
1107 | goto error; | |
1108 | } | |
1109 | } | |
3219e529 MC |
1110 | } |
1111 | ||
a8ac6311 OK |
1112 | debug_tcp("xmit %d bytes\n", consumed); |
1113 | ||
1114 | conn->txdata_octets += consumed; | |
1115 | return consumed; | |
1116 | ||
1117 | error: | |
1118 | /* Transmit error. We could initiate error recovery | |
1119 | * here. */ | |
1120 | debug_tcp("Error sending PDU, errno=%d\n", rc); | |
1121 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); | |
1122 | return rc; | |
7ba24713 AA |
1123 | } |
1124 | ||
1125 | /** | |
a8ac6311 OK |
1126 | * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit |
1127 | */ | |
7ba24713 | 1128 | static inline int |
a8ac6311 | 1129 | iscsi_tcp_xmit_qlen(struct iscsi_conn *conn) |
7ba24713 | 1130 | { |
a8ac6311 OK |
1131 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
1132 | struct iscsi_segment *segment = &tcp_conn->out.segment; | |
7ba24713 | 1133 | |
a8ac6311 | 1134 | return segment->total_copied - segment->total_size; |
7ba24713 AA |
1135 | } |
1136 | ||
7ba24713 | 1137 | static inline int |
a8ac6311 | 1138 | iscsi_tcp_flush(struct iscsi_conn *conn) |
7ba24713 | 1139 | { |
a8ac6311 OK |
1140 | int rc; |
1141 | ||
1142 | while (iscsi_tcp_xmit_qlen(conn)) { | |
1143 | rc = iscsi_xmit(conn); | |
1144 | if (rc == 0) | |
7ba24713 | 1145 | return -EAGAIN; |
a8ac6311 OK |
1146 | if (rc < 0) |
1147 | return rc; | |
3219e529 | 1148 | } |
7ba24713 | 1149 | |
a8ac6311 | 1150 | return 0; |
7ba24713 AA |
1151 | } |
1152 | ||
a8ac6311 OK |
1153 | /* |
1154 | * This is called when we're done sending the header. | |
1155 | * Simply copy the data_segment to the send segment, and return. | |
1156 | */ | |
1157 | static int | |
1158 | iscsi_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn, | |
1159 | struct iscsi_segment *segment) | |
7ba24713 | 1160 | { |
a8ac6311 OK |
1161 | tcp_conn->out.segment = tcp_conn->out.data_segment; |
1162 | debug_tcp("Header done. Next segment size %u total_size %u\n", | |
1163 | tcp_conn->out.segment.size, tcp_conn->out.segment.total_size); | |
1164 | return 0; | |
1165 | } | |
1166 | ||
1167 | static void | |
1168 | iscsi_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr, size_t hdrlen) | |
1169 | { | |
1170 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; | |
1171 | ||
1172 | debug_tcp("%s(%p%s)\n", __FUNCTION__, tcp_conn, | |
1173 | conn->hdrdgst_en? ", digest enabled" : ""); | |
1174 | ||
1175 | /* Clear the data segment - needs to be filled in by the | |
1176 | * caller using iscsi_tcp_send_data_prep() */ | |
1177 | memset(&tcp_conn->out.data_segment, 0, sizeof(struct iscsi_segment)); | |
1178 | ||
1179 | /* If header digest is enabled, compute the CRC and | |
1180 | * place the digest into the same buffer. We make | |
1181 | * sure that both iscsi_tcp_ctask and mtask have | |
1182 | * sufficient room. | |
1183 | */ | |
1184 | if (conn->hdrdgst_en) { | |
1185 | iscsi_tcp_dgst_header(&tcp_conn->tx_hash, hdr, hdrlen, | |
1186 | hdr + hdrlen); | |
1187 | hdrlen += ISCSI_DIGEST_SIZE; | |
1188 | } | |
1189 | ||
1190 | /* Remember header pointer for later, when we need | |
1191 | * to decide whether there's a payload to go along | |
1192 | * with the header. */ | |
1193 | tcp_conn->out.hdr = hdr; | |
1194 | ||
1195 | iscsi_segment_init_linear(&tcp_conn->out.segment, hdr, hdrlen, | |
1196 | iscsi_tcp_send_hdr_done, NULL); | |
1197 | } | |
1198 | ||
1199 | /* | |
1200 | * Prepare the send buffer for the payload data. | |
1201 | * Padding and checksumming will all be taken care | |
1202 | * of by the iscsi_segment routines. | |
1203 | */ | |
1204 | static int | |
1205 | iscsi_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg, | |
1206 | unsigned int count, unsigned int offset, | |
1207 | unsigned int len) | |
1208 | { | |
1209 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; | |
1210 | struct hash_desc *tx_hash = NULL; | |
1211 | unsigned int hdr_spec_len; | |
1212 | ||
1213 | debug_tcp("%s(%p, offset=%d, datalen=%d%s)\n", __FUNCTION__, | |
1214 | tcp_conn, offset, len, | |
1215 | conn->datadgst_en? ", digest enabled" : ""); | |
1216 | ||
1217 | /* Make sure the datalen matches what the caller | |
1218 | said he would send. */ | |
1219 | hdr_spec_len = ntoh24(tcp_conn->out.hdr->dlength); | |
1220 | WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len)); | |
1221 | ||
1222 | if (conn->datadgst_en) | |
1223 | tx_hash = &tcp_conn->tx_hash; | |
1224 | ||
1225 | return iscsi_segment_seek_sg(&tcp_conn->out.data_segment, | |
1226 | sg, count, offset, len, | |
1227 | NULL, tx_hash); | |
1228 | } | |
1229 | ||
1230 | static void | |
1231 | iscsi_tcp_send_linear_data_prepare(struct iscsi_conn *conn, void *data, | |
1232 | size_t len) | |
1233 | { | |
1234 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; | |
1235 | struct hash_desc *tx_hash = NULL; | |
1236 | unsigned int hdr_spec_len; | |
1237 | ||
1238 | debug_tcp("%s(%p, datalen=%d%s)\n", __FUNCTION__, tcp_conn, len, | |
1239 | conn->datadgst_en? ", digest enabled" : ""); | |
1240 | ||
1241 | /* Make sure the datalen matches what the caller | |
1242 | said he would send. */ | |
1243 | hdr_spec_len = ntoh24(tcp_conn->out.hdr->dlength); | |
1244 | WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len)); | |
1245 | ||
1246 | if (conn->datadgst_en) | |
1247 | tx_hash = &tcp_conn->tx_hash; | |
1248 | ||
1249 | iscsi_segment_init_linear(&tcp_conn->out.data_segment, | |
1250 | data, len, NULL, tx_hash); | |
7ba24713 AA |
1251 | } |
1252 | ||
7ba24713 AA |
1253 | /** |
1254 | * iscsi_solicit_data_cont - initialize next Data-Out | |
1255 | * @conn: iscsi connection | |
1256 | * @ctask: scsi command task | |
1257 | * @r2t: R2T info | |
1258 | * @left: bytes left to transfer | |
1259 | * | |
1260 | * Notes: | |
1261 | * Initialize next Data-Out within this R2T sequence and continue | |
1262 | * to process next Scatter-Gather element(if any) of this SCSI command. | |
1263 | * | |
1264 | * Called under connection lock. | |
1265 | **/ | |
a8ac6311 | 1266 | static int |
7ba24713 | 1267 | iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask, |
a8ac6311 | 1268 | struct iscsi_r2t_info *r2t) |
7ba24713 AA |
1269 | { |
1270 | struct iscsi_data *hdr; | |
a8ac6311 OK |
1271 | int new_offset, left; |
1272 | ||
1273 | BUG_ON(r2t->data_length - r2t->sent < 0); | |
1274 | left = r2t->data_length - r2t->sent; | |
1275 | if (left == 0) | |
1276 | return 0; | |
7ba24713 | 1277 | |
ffbfe925 | 1278 | hdr = &r2t->dtask.hdr; |
7ba24713 AA |
1279 | memset(hdr, 0, sizeof(struct iscsi_data)); |
1280 | hdr->ttt = r2t->ttt; | |
1281 | hdr->datasn = cpu_to_be32(r2t->solicit_datasn); | |
1282 | r2t->solicit_datasn++; | |
1283 | hdr->opcode = ISCSI_OP_SCSI_DATA_OUT; | |
5bb0b55a MC |
1284 | memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun)); |
1285 | hdr->itt = ctask->hdr->itt; | |
7ba24713 AA |
1286 | hdr->exp_statsn = r2t->exp_statsn; |
1287 | new_offset = r2t->data_offset + r2t->sent; | |
1288 | hdr->offset = cpu_to_be32(new_offset); | |
1289 | if (left > conn->max_xmit_dlength) { | |
1290 | hton24(hdr->dlength, conn->max_xmit_dlength); | |
1291 | r2t->data_count = conn->max_xmit_dlength; | |
1292 | } else { | |
1293 | hton24(hdr->dlength, left); | |
1294 | r2t->data_count = left; | |
1295 | hdr->flags = ISCSI_FLAG_CMD_FINAL; | |
1296 | } | |
7ba24713 | 1297 | |
a8ac6311 OK |
1298 | conn->dataout_pdus_cnt++; |
1299 | return 1; | |
7ba24713 AA |
1300 | } |
1301 | ||
1302 | /** | |
a8ac6311 | 1303 | * iscsi_tcp_ctask - Initialize iSCSI SCSI_READ or SCSI_WRITE commands |
7ba24713 AA |
1304 | * @conn: iscsi connection |
1305 | * @ctask: scsi command task | |
1306 | * @sc: scsi command | |
1307 | **/ | |
a8ac6311 OK |
1308 | static int |
1309 | iscsi_tcp_ctask_init(struct iscsi_cmd_task *ctask) | |
7ba24713 | 1310 | { |
5bb0b55a | 1311 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; |
a8ac6311 OK |
1312 | struct iscsi_conn *conn = ctask->conn; |
1313 | struct scsi_cmnd *sc = ctask->sc; | |
1314 | int err; | |
7ba24713 | 1315 | |
5bb0b55a | 1316 | BUG_ON(__kfifo_len(tcp_ctask->r2tqueue)); |
a8ac6311 OK |
1317 | tcp_ctask->sent = 0; |
1318 | tcp_ctask->exp_datasn = 0; | |
1319 | ||
1320 | /* Prepare PDU, optionally w/ immediate data */ | |
1321 | debug_scsi("ctask deq [cid %d itt 0x%x imm %d unsol %d]\n", | |
1322 | conn->id, ctask->itt, ctask->imm_count, | |
1323 | ctask->unsol_count); | |
1324 | iscsi_tcp_send_hdr_prep(conn, ctask->hdr, ctask->hdr_len); | |
1325 | ||
1326 | if (!ctask->imm_count) | |
1327 | return 0; | |
1328 | ||
1329 | /* If we have immediate data, attach a payload */ | |
1330 | err = iscsi_tcp_send_data_prep(conn, scsi_sglist(sc), scsi_sg_count(sc), | |
1331 | 0, ctask->imm_count); | |
1332 | if (err) | |
1333 | return err; | |
1334 | tcp_ctask->sent += ctask->imm_count; | |
1335 | ctask->imm_count = 0; | |
1336 | return 0; | |
7ba24713 AA |
1337 | } |
1338 | ||
1339 | /** | |
5bb0b55a | 1340 | * iscsi_tcp_mtask_xmit - xmit management(immediate) task |
7ba24713 AA |
1341 | * @conn: iscsi connection |
1342 | * @mtask: task management task | |
1343 | * | |
1344 | * Notes: | |
1345 | * The function can return -EAGAIN in which case caller must | |
1346 | * call it again later, or recover. '0' return code means successful | |
1347 | * xmit. | |
7ba24713 AA |
1348 | **/ |
1349 | static int | |
5bb0b55a | 1350 | iscsi_tcp_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask) |
7ba24713 | 1351 | { |
3219e529 | 1352 | int rc; |
7ba24713 | 1353 | |
a8ac6311 OK |
1354 | /* Flush any pending data first. */ |
1355 | rc = iscsi_tcp_flush(conn); | |
1356 | if (rc < 0) | |
1357 | return rc; | |
7ba24713 | 1358 | |
b4377356 | 1359 | if (mtask->hdr->itt == RESERVED_ITT) { |
5bb0b55a MC |
1360 | struct iscsi_session *session = conn->session; |
1361 | ||
1362 | spin_lock_bh(&session->lock); | |
b3a7ea8d | 1363 | iscsi_free_mgmt_task(conn, mtask); |
5bb0b55a MC |
1364 | spin_unlock_bh(&session->lock); |
1365 | } | |
a8ac6311 | 1366 | |
7ba24713 AA |
1367 | return 0; |
1368 | } | |
1369 | ||
a8ac6311 OK |
1370 | /* |
1371 | * iscsi_tcp_ctask_xmit - xmit normal PDU task | |
1372 | * @conn: iscsi connection | |
1373 | * @ctask: iscsi command task | |
1374 | * | |
1375 | * We're expected to return 0 when everything was transmitted succesfully, | |
1376 | * -EAGAIN if there's still data in the queue, or != 0 for any other kind | |
1377 | * of error. | |
1378 | */ | |
218432c6 | 1379 | static int |
a8ac6311 | 1380 | iscsi_tcp_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) |
7ba24713 | 1381 | { |
218432c6 | 1382 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; |
a8ac6311 | 1383 | struct scsi_cmnd *sc = ctask->sc; |
218432c6 | 1384 | int rc = 0; |
3219e529 | 1385 | |
a8ac6311 OK |
1386 | flush: |
1387 | /* Flush any pending data first. */ | |
1388 | rc = iscsi_tcp_flush(conn); | |
1389 | if (rc < 0) | |
62f38300 MC |
1390 | return rc; |
1391 | ||
a8ac6311 OK |
1392 | /* Are we done already? */ |
1393 | if (sc->sc_data_direction != DMA_TO_DEVICE) | |
1394 | return 0; | |
7ba24713 | 1395 | |
a8ac6311 OK |
1396 | if (ctask->unsol_count != 0) { |
1397 | struct iscsi_data *hdr = &tcp_ctask->unsol_dtask.hdr; | |
dd8c0d95 | 1398 | |
a8ac6311 OK |
1399 | /* Prepare a header for the unsolicited PDU. |
1400 | * The amount of data we want to send will be | |
1401 | * in ctask->data_count. | |
1402 | * FIXME: return the data count instead. | |
1403 | */ | |
1404 | iscsi_prep_unsolicit_data_pdu(ctask, hdr); | |
7ba24713 | 1405 | |
a8ac6311 OK |
1406 | debug_tcp("unsol dout [itt 0x%x doff %d dlen %d]\n", |
1407 | ctask->itt, tcp_ctask->sent, ctask->data_count); | |
7ba24713 | 1408 | |
a8ac6311 OK |
1409 | iscsi_tcp_send_hdr_prep(conn, hdr, sizeof(*hdr)); |
1410 | rc = iscsi_tcp_send_data_prep(conn, scsi_sglist(sc), | |
1411 | scsi_sg_count(sc), | |
1412 | tcp_ctask->sent, | |
1413 | ctask->data_count); | |
62f38300 | 1414 | if (rc) |
a8ac6311 OK |
1415 | goto fail; |
1416 | tcp_ctask->sent += ctask->data_count; | |
1417 | ctask->unsol_count -= ctask->data_count; | |
1418 | goto flush; | |
1419 | } else { | |
1420 | struct iscsi_session *session = conn->session; | |
1421 | struct iscsi_r2t_info *r2t; | |
7ba24713 | 1422 | |
a8ac6311 | 1423 | /* All unsolicited PDUs sent. Check for solicited PDUs. |
7ba24713 | 1424 | */ |
a8ac6311 OK |
1425 | spin_lock_bh(&session->lock); |
1426 | r2t = tcp_ctask->r2t; | |
1427 | if (r2t != NULL) { | |
1428 | /* Continue with this R2T? */ | |
1429 | if (!iscsi_solicit_data_cont(conn, ctask, r2t)) { | |
1430 | debug_scsi(" done with r2t %p\n", r2t); | |
1431 | ||
1432 | __kfifo_put(tcp_ctask->r2tpool.queue, | |
1433 | (void*)&r2t, sizeof(void*)); | |
1434 | tcp_ctask->r2t = r2t = NULL; | |
1435 | } | |
7ba24713 | 1436 | } |
7ba24713 | 1437 | |
a8ac6311 | 1438 | if (r2t == NULL) { |
62f38300 MC |
1439 | __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t, |
1440 | sizeof(void*)); | |
a8ac6311 | 1441 | r2t = tcp_ctask->r2t; |
db37c505 | 1442 | } |
a8ac6311 | 1443 | spin_unlock_bh(&session->lock); |
7ba24713 | 1444 | |
a8ac6311 OK |
1445 | /* Waiting for more R2Ts to arrive. */ |
1446 | if (r2t == NULL) { | |
1447 | debug_tcp("no R2Ts yet\n"); | |
1448 | return 0; | |
7ba24713 | 1449 | } |
7ba24713 | 1450 | |
a8ac6311 OK |
1451 | debug_scsi("sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n", |
1452 | r2t, r2t->solicit_datasn - 1, ctask->itt, | |
1453 | r2t->data_offset + r2t->sent, r2t->data_count); | |
7ba24713 | 1454 | |
a8ac6311 OK |
1455 | iscsi_tcp_send_hdr_prep(conn, &r2t->dtask.hdr, |
1456 | sizeof(struct iscsi_hdr)); | |
7ba24713 | 1457 | |
a8ac6311 OK |
1458 | rc = iscsi_tcp_send_data_prep(conn, scsi_sglist(sc), |
1459 | scsi_sg_count(sc), | |
1460 | r2t->data_offset + r2t->sent, | |
1461 | r2t->data_count); | |
62f38300 | 1462 | if (rc) |
a8ac6311 OK |
1463 | goto fail; |
1464 | tcp_ctask->sent += r2t->data_count; | |
1465 | r2t->sent += r2t->data_count; | |
1466 | goto flush; | |
7ba24713 | 1467 | } |
7ba24713 | 1468 | return 0; |
a8ac6311 OK |
1469 | fail: |
1470 | iscsi_conn_failure(conn, rc); | |
1471 | return -EIO; | |
7ba24713 AA |
1472 | } |
1473 | ||
5bb0b55a MC |
1474 | static struct iscsi_cls_conn * |
1475 | iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx) | |
7ba24713 | 1476 | { |
5bb0b55a MC |
1477 | struct iscsi_conn *conn; |
1478 | struct iscsi_cls_conn *cls_conn; | |
1479 | struct iscsi_tcp_conn *tcp_conn; | |
7ba24713 | 1480 | |
5bb0b55a MC |
1481 | cls_conn = iscsi_conn_setup(cls_session, conn_idx); |
1482 | if (!cls_conn) | |
1483 | return NULL; | |
1484 | conn = cls_conn->dd_data; | |
7ba24713 | 1485 | /* |
5bb0b55a MC |
1486 | * due to strange issues with iser these are not set |
1487 | * in iscsi_conn_setup | |
7ba24713 | 1488 | */ |
bf32ed33 | 1489 | conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN; |
7ba24713 | 1490 | |
5bb0b55a MC |
1491 | tcp_conn = kzalloc(sizeof(*tcp_conn), GFP_KERNEL); |
1492 | if (!tcp_conn) | |
1493 | goto tcp_conn_alloc_fail; | |
7ba24713 | 1494 | |
5bb0b55a MC |
1495 | conn->dd_data = tcp_conn; |
1496 | tcp_conn->iscsi_conn = conn; | |
7ba24713 | 1497 | |
c9802cd9 JB |
1498 | tcp_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0, |
1499 | CRYPTO_ALG_ASYNC); | |
1500 | tcp_conn->tx_hash.flags = 0; | |
0f238418 MC |
1501 | if (IS_ERR(tcp_conn->tx_hash.tfm)) { |
1502 | printk(KERN_ERR "Could not create connection due to crc32c " | |
1503 | "loading error %ld. Make sure the crc32c module is " | |
1504 | "built as a module or into the kernel\n", | |
1505 | PTR_ERR(tcp_conn->tx_hash.tfm)); | |
dd8c0d95 | 1506 | goto free_tcp_conn; |
0f238418 | 1507 | } |
dd8c0d95 | 1508 | |
c9802cd9 JB |
1509 | tcp_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0, |
1510 | CRYPTO_ALG_ASYNC); | |
1511 | tcp_conn->rx_hash.flags = 0; | |
0f238418 MC |
1512 | if (IS_ERR(tcp_conn->rx_hash.tfm)) { |
1513 | printk(KERN_ERR "Could not create connection due to crc32c " | |
1514 | "loading error %ld. Make sure the crc32c module is " | |
1515 | "built as a module or into the kernel\n", | |
1516 | PTR_ERR(tcp_conn->rx_hash.tfm)); | |
dd8c0d95 | 1517 | goto free_tx_tfm; |
0f238418 | 1518 | } |
dd8c0d95 | 1519 | |
5bb0b55a | 1520 | return cls_conn; |
7ba24713 | 1521 | |
dd8c0d95 | 1522 | free_tx_tfm: |
c9802cd9 | 1523 | crypto_free_hash(tcp_conn->tx_hash.tfm); |
dd8c0d95 MC |
1524 | free_tcp_conn: |
1525 | kfree(tcp_conn); | |
5bb0b55a MC |
1526 | tcp_conn_alloc_fail: |
1527 | iscsi_conn_teardown(cls_conn); | |
1528 | return NULL; | |
7ba24713 AA |
1529 | } |
1530 | ||
1c83469d MC |
1531 | static void |
1532 | iscsi_tcp_release_conn(struct iscsi_conn *conn) | |
1533 | { | |
22236961 | 1534 | struct iscsi_session *session = conn->session; |
1c83469d | 1535 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
22236961 | 1536 | struct socket *sock = tcp_conn->sock; |
1c83469d | 1537 | |
22236961 | 1538 | if (!sock) |
1c83469d MC |
1539 | return; |
1540 | ||
22236961 | 1541 | sock_hold(sock->sk); |
1c83469d | 1542 | iscsi_conn_restore_callbacks(tcp_conn); |
22236961 | 1543 | sock_put(sock->sk); |
1c83469d | 1544 | |
22236961 | 1545 | spin_lock_bh(&session->lock); |
1c83469d MC |
1546 | tcp_conn->sock = NULL; |
1547 | conn->recv_lock = NULL; | |
22236961 MC |
1548 | spin_unlock_bh(&session->lock); |
1549 | sockfd_put(sock); | |
1c83469d MC |
1550 | } |
1551 | ||
7ba24713 | 1552 | static void |
5bb0b55a | 1553 | iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn) |
7ba24713 | 1554 | { |
5bb0b55a MC |
1555 | struct iscsi_conn *conn = cls_conn->dd_data; |
1556 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; | |
7ba24713 | 1557 | |
1c83469d | 1558 | iscsi_tcp_release_conn(conn); |
5bb0b55a | 1559 | iscsi_conn_teardown(cls_conn); |
7ba24713 | 1560 | |
534284a0 PW |
1561 | if (tcp_conn->tx_hash.tfm) |
1562 | crypto_free_hash(tcp_conn->tx_hash.tfm); | |
1563 | if (tcp_conn->rx_hash.tfm) | |
1564 | crypto_free_hash(tcp_conn->rx_hash.tfm); | |
7ba24713 | 1565 | |
5bb0b55a MC |
1566 | kfree(tcp_conn); |
1567 | } | |
7ba24713 | 1568 | |
1c83469d MC |
1569 | static void |
1570 | iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag) | |
1571 | { | |
1572 | struct iscsi_conn *conn = cls_conn->dd_data; | |
1573 | ||
1574 | iscsi_conn_stop(cls_conn, flag); | |
1575 | iscsi_tcp_release_conn(conn); | |
1576 | } | |
1577 | ||
22236961 MC |
1578 | static int iscsi_tcp_get_addr(struct iscsi_conn *conn, struct socket *sock, |
1579 | char *buf, int *port, | |
1580 | int (*getname)(struct socket *, struct sockaddr *, | |
1581 | int *addrlen)) | |
1582 | { | |
1583 | struct sockaddr_storage *addr; | |
1584 | struct sockaddr_in6 *sin6; | |
1585 | struct sockaddr_in *sin; | |
1586 | int rc = 0, len; | |
1587 | ||
a9204879 | 1588 | addr = kmalloc(sizeof(*addr), GFP_KERNEL); |
22236961 MC |
1589 | if (!addr) |
1590 | return -ENOMEM; | |
1591 | ||
1592 | if (getname(sock, (struct sockaddr *) addr, &len)) { | |
1593 | rc = -ENODEV; | |
1594 | goto free_addr; | |
1595 | } | |
1596 | ||
1597 | switch (addr->ss_family) { | |
1598 | case AF_INET: | |
1599 | sin = (struct sockaddr_in *)addr; | |
1600 | spin_lock_bh(&conn->session->lock); | |
1601 | sprintf(buf, NIPQUAD_FMT, NIPQUAD(sin->sin_addr.s_addr)); | |
1602 | *port = be16_to_cpu(sin->sin_port); | |
1603 | spin_unlock_bh(&conn->session->lock); | |
1604 | break; | |
1605 | case AF_INET6: | |
1606 | sin6 = (struct sockaddr_in6 *)addr; | |
1607 | spin_lock_bh(&conn->session->lock); | |
1608 | sprintf(buf, NIP6_FMT, NIP6(sin6->sin6_addr)); | |
1609 | *port = be16_to_cpu(sin6->sin6_port); | |
1610 | spin_unlock_bh(&conn->session->lock); | |
1611 | break; | |
1612 | } | |
1613 | free_addr: | |
1614 | kfree(addr); | |
1615 | return rc; | |
1616 | } | |
1617 | ||
5bb0b55a MC |
1618 | static int |
1619 | iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session, | |
264faaaa | 1620 | struct iscsi_cls_conn *cls_conn, uint64_t transport_eph, |
5bb0b55a MC |
1621 | int is_leading) |
1622 | { | |
1623 | struct iscsi_conn *conn = cls_conn->dd_data; | |
1624 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; | |
1625 | struct sock *sk; | |
1626 | struct socket *sock; | |
1627 | int err; | |
7ba24713 | 1628 | |
5bb0b55a | 1629 | /* lookup for existing socket */ |
264faaaa | 1630 | sock = sockfd_lookup((int)transport_eph, &err); |
5bb0b55a MC |
1631 | if (!sock) { |
1632 | printk(KERN_ERR "iscsi_tcp: sockfd_lookup failed %d\n", err); | |
1633 | return -EEXIST; | |
7ba24713 | 1634 | } |
22236961 MC |
1635 | /* |
1636 | * copy these values now because if we drop the session | |
1637 | * userspace may still want to query the values since we will | |
1638 | * be using them for the reconnect | |
1639 | */ | |
1640 | err = iscsi_tcp_get_addr(conn, sock, conn->portal_address, | |
1641 | &conn->portal_port, kernel_getpeername); | |
1642 | if (err) | |
1643 | goto free_socket; | |
1644 | ||
1645 | err = iscsi_tcp_get_addr(conn, sock, conn->local_address, | |
1646 | &conn->local_port, kernel_getsockname); | |
1647 | if (err) | |
1648 | goto free_socket; | |
7ba24713 | 1649 | |
5bb0b55a MC |
1650 | err = iscsi_conn_bind(cls_session, cls_conn, is_leading); |
1651 | if (err) | |
22236961 | 1652 | goto free_socket; |
7ba24713 | 1653 | |
67a61114 MC |
1654 | /* bind iSCSI connection and socket */ |
1655 | tcp_conn->sock = sock; | |
7ba24713 | 1656 | |
67a61114 MC |
1657 | /* setup Socket parameters */ |
1658 | sk = sock->sk; | |
1659 | sk->sk_reuse = 1; | |
1660 | sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */ | |
1661 | sk->sk_allocation = GFP_ATOMIC; | |
7ba24713 | 1662 | |
67a61114 | 1663 | /* FIXME: disable Nagle's algorithm */ |
7ba24713 | 1664 | |
67a61114 MC |
1665 | /* |
1666 | * Intercept TCP callbacks for sendfile like receive | |
1667 | * processing. | |
1668 | */ | |
1669 | conn->recv_lock = &sk->sk_callback_lock; | |
1670 | iscsi_conn_set_callbacks(conn); | |
1671 | tcp_conn->sendpage = tcp_conn->sock->ops->sendpage; | |
1672 | /* | |
1673 | * set receive state machine into initial state | |
1674 | */ | |
da32dd68 | 1675 | iscsi_tcp_hdr_recv_prep(tcp_conn); |
7ba24713 | 1676 | return 0; |
22236961 MC |
1677 | |
1678 | free_socket: | |
1679 | sockfd_put(sock); | |
1680 | return err; | |
7ba24713 AA |
1681 | } |
1682 | ||
5bb0b55a | 1683 | /* called with host lock */ |
30a6c652 | 1684 | static void |
a8ac6311 | 1685 | iscsi_tcp_mtask_init(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask) |
7ba24713 | 1686 | { |
a8ac6311 OK |
1687 | debug_scsi("mtask deq [cid %d itt 0x%x]\n", conn->id, mtask->itt); |
1688 | ||
1689 | /* Prepare PDU, optionally w/ immediate data */ | |
1690 | iscsi_tcp_send_hdr_prep(conn, mtask->hdr, sizeof(*mtask->hdr)); | |
1691 | ||
1692 | /* If we have immediate data, attach a payload */ | |
1693 | if (mtask->data_count) | |
1694 | iscsi_tcp_send_linear_data_prepare(conn, mtask->data, | |
1695 | mtask->data_count); | |
7ba24713 AA |
1696 | } |
1697 | ||
1698 | static int | |
1699 | iscsi_r2tpool_alloc(struct iscsi_session *session) | |
1700 | { | |
1701 | int i; | |
1702 | int cmd_i; | |
1703 | ||
1704 | /* | |
1705 | * initialize per-task: R2T pool and xmit queue | |
1706 | */ | |
1707 | for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) { | |
1708 | struct iscsi_cmd_task *ctask = session->cmds[cmd_i]; | |
5bb0b55a | 1709 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; |
7ba24713 AA |
1710 | |
1711 | /* | |
1712 | * pre-allocated x4 as much r2ts to handle race when | |
1713 | * target acks DataOut faster than we data_xmit() queues | |
1714 | * could replenish r2tqueue. | |
1715 | */ | |
1716 | ||
1717 | /* R2T pool */ | |
6320377f | 1718 | if (iscsi_pool_init(&tcp_ctask->r2tpool, session->max_r2t * 4, NULL, |
5bb0b55a | 1719 | sizeof(struct iscsi_r2t_info))) { |
7ba24713 AA |
1720 | goto r2t_alloc_fail; |
1721 | } | |
1722 | ||
1723 | /* R2T xmit queue */ | |
5bb0b55a | 1724 | tcp_ctask->r2tqueue = kfifo_alloc( |
7ba24713 | 1725 | session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL); |
5bb0b55a | 1726 | if (tcp_ctask->r2tqueue == ERR_PTR(-ENOMEM)) { |
6320377f | 1727 | iscsi_pool_free(&tcp_ctask->r2tpool); |
7ba24713 AA |
1728 | goto r2t_alloc_fail; |
1729 | } | |
7ba24713 AA |
1730 | } |
1731 | ||
1732 | return 0; | |
1733 | ||
1734 | r2t_alloc_fail: | |
1735 | for (i = 0; i < cmd_i; i++) { | |
5bb0b55a MC |
1736 | struct iscsi_cmd_task *ctask = session->cmds[i]; |
1737 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; | |
1738 | ||
5bb0b55a | 1739 | kfifo_free(tcp_ctask->r2tqueue); |
6320377f | 1740 | iscsi_pool_free(&tcp_ctask->r2tpool); |
7ba24713 AA |
1741 | } |
1742 | return -ENOMEM; | |
1743 | } | |
1744 | ||
1745 | static void | |
1746 | iscsi_r2tpool_free(struct iscsi_session *session) | |
1747 | { | |
1748 | int i; | |
1749 | ||
1750 | for (i = 0; i < session->cmds_max; i++) { | |
5bb0b55a MC |
1751 | struct iscsi_cmd_task *ctask = session->cmds[i]; |
1752 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; | |
7ba24713 | 1753 | |
5bb0b55a | 1754 | kfifo_free(tcp_ctask->r2tqueue); |
6320377f | 1755 | iscsi_pool_free(&tcp_ctask->r2tpool); |
7ba24713 | 1756 | } |
7ba24713 AA |
1757 | } |
1758 | ||
1759 | static int | |
7b7232f3 | 1760 | iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param, |
5c75b7fc | 1761 | char *buf, int buflen) |
7ba24713 | 1762 | { |
7b7232f3 | 1763 | struct iscsi_conn *conn = cls_conn->dd_data; |
7ba24713 | 1764 | struct iscsi_session *session = conn->session; |
5bb0b55a | 1765 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
5c75b7fc | 1766 | int value; |
7ba24713 | 1767 | |
7ba24713 | 1768 | switch(param) { |
7ba24713 | 1769 | case ISCSI_PARAM_HDRDGST_EN: |
5c75b7fc | 1770 | iscsi_set_param(cls_conn, param, buf, buflen); |
7ba24713 AA |
1771 | break; |
1772 | case ISCSI_PARAM_DATADGST_EN: | |
5c75b7fc | 1773 | iscsi_set_param(cls_conn, param, buf, buflen); |
5bb0b55a MC |
1774 | tcp_conn->sendpage = conn->datadgst_en ? |
1775 | sock_no_sendpage : tcp_conn->sock->ops->sendpage; | |
7ba24713 | 1776 | break; |
7ba24713 | 1777 | case ISCSI_PARAM_MAX_R2T: |
5c75b7fc | 1778 | sscanf(buf, "%d", &value); |
7ba24713 AA |
1779 | if (session->max_r2t == roundup_pow_of_two(value)) |
1780 | break; | |
1781 | iscsi_r2tpool_free(session); | |
5c75b7fc | 1782 | iscsi_set_param(cls_conn, param, buf, buflen); |
7ba24713 AA |
1783 | if (session->max_r2t & (session->max_r2t - 1)) |
1784 | session->max_r2t = roundup_pow_of_two(session->max_r2t); | |
1785 | if (iscsi_r2tpool_alloc(session)) | |
1786 | return -ENOMEM; | |
1787 | break; | |
7ba24713 | 1788 | default: |
5c75b7fc | 1789 | return iscsi_set_param(cls_conn, param, buf, buflen); |
7ba24713 AA |
1790 | } |
1791 | ||
1792 | return 0; | |
1793 | } | |
1794 | ||
1795 | static int | |
5c75b7fc MC |
1796 | iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn, |
1797 | enum iscsi_param param, char *buf) | |
7b8631b5 | 1798 | { |
7b7232f3 | 1799 | struct iscsi_conn *conn = cls_conn->dd_data; |
5c75b7fc | 1800 | int len; |
7b8631b5 MC |
1801 | |
1802 | switch(param) { | |
fd7255f5 | 1803 | case ISCSI_PARAM_CONN_PORT: |
22236961 MC |
1804 | spin_lock_bh(&conn->session->lock); |
1805 | len = sprintf(buf, "%hu\n", conn->portal_port); | |
1806 | spin_unlock_bh(&conn->session->lock); | |
8d2860b3 | 1807 | break; |
fd7255f5 | 1808 | case ISCSI_PARAM_CONN_ADDRESS: |
22236961 MC |
1809 | spin_lock_bh(&conn->session->lock); |
1810 | len = sprintf(buf, "%s\n", conn->portal_address); | |
1811 | spin_unlock_bh(&conn->session->lock); | |
fd7255f5 MC |
1812 | break; |
1813 | default: | |
5c75b7fc | 1814 | return iscsi_conn_get_param(cls_conn, param, buf); |
fd7255f5 MC |
1815 | } |
1816 | ||
1817 | return len; | |
1818 | } | |
1819 | ||
22236961 MC |
1820 | static int |
1821 | iscsi_tcp_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param, | |
1822 | char *buf) | |
1823 | { | |
1824 | struct iscsi_session *session = iscsi_hostdata(shost->hostdata); | |
1825 | int len; | |
1826 | ||
1827 | switch (param) { | |
1828 | case ISCSI_HOST_PARAM_IPADDRESS: | |
1829 | spin_lock_bh(&session->lock); | |
1830 | if (!session->leadconn) | |
1831 | len = -ENODEV; | |
1832 | else | |
1833 | len = sprintf(buf, "%s\n", | |
1834 | session->leadconn->local_address); | |
1835 | spin_unlock_bh(&session->lock); | |
1836 | break; | |
1837 | default: | |
1838 | return iscsi_host_get_param(shost, param, buf); | |
1839 | } | |
1840 | return len; | |
1841 | } | |
1842 | ||
7ba24713 | 1843 | static void |
7b7232f3 | 1844 | iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats) |
7ba24713 | 1845 | { |
7b7232f3 | 1846 | struct iscsi_conn *conn = cls_conn->dd_data; |
5bb0b55a | 1847 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
7ba24713 AA |
1848 | |
1849 | stats->txdata_octets = conn->txdata_octets; | |
1850 | stats->rxdata_octets = conn->rxdata_octets; | |
1851 | stats->scsicmd_pdus = conn->scsicmd_pdus_cnt; | |
1852 | stats->dataout_pdus = conn->dataout_pdus_cnt; | |
1853 | stats->scsirsp_pdus = conn->scsirsp_pdus_cnt; | |
1854 | stats->datain_pdus = conn->datain_pdus_cnt; | |
1855 | stats->r2t_pdus = conn->r2t_pdus_cnt; | |
1856 | stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt; | |
1857 | stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt; | |
1858 | stats->custom_length = 3; | |
1859 | strcpy(stats->custom[0].desc, "tx_sendpage_failures"); | |
5bb0b55a | 1860 | stats->custom[0].value = tcp_conn->sendpage_failures_cnt; |
7ba24713 | 1861 | strcpy(stats->custom[1].desc, "rx_discontiguous_hdr"); |
5bb0b55a | 1862 | stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt; |
7ba24713 AA |
1863 | strcpy(stats->custom[2].desc, "eh_abort_cnt"); |
1864 | stats->custom[2].value = conn->eh_abort_cnt; | |
1865 | } | |
1866 | ||
5bb0b55a MC |
1867 | static struct iscsi_cls_session * |
1868 | iscsi_tcp_session_create(struct iscsi_transport *iscsit, | |
1869 | struct scsi_transport_template *scsit, | |
1548271e | 1870 | uint16_t cmds_max, uint16_t qdepth, |
5bb0b55a | 1871 | uint32_t initial_cmdsn, uint32_t *hostno) |
7ba24713 | 1872 | { |
5bb0b55a MC |
1873 | struct iscsi_cls_session *cls_session; |
1874 | struct iscsi_session *session; | |
1875 | uint32_t hn; | |
1876 | int cmd_i; | |
7ba24713 | 1877 | |
1548271e | 1878 | cls_session = iscsi_session_setup(iscsit, scsit, cmds_max, qdepth, |
5bb0b55a MC |
1879 | sizeof(struct iscsi_tcp_cmd_task), |
1880 | sizeof(struct iscsi_tcp_mgmt_task), | |
1881 | initial_cmdsn, &hn); | |
1882 | if (!cls_session) | |
1883 | return NULL; | |
1884 | *hostno = hn; | |
7ba24713 | 1885 | |
5bb0b55a MC |
1886 | session = class_to_transport_session(cls_session); |
1887 | for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) { | |
1888 | struct iscsi_cmd_task *ctask = session->cmds[cmd_i]; | |
1889 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; | |
1890 | ||
004d6530 BH |
1891 | ctask->hdr = &tcp_ctask->hdr.cmd_hdr; |
1892 | ctask->hdr_max = sizeof(tcp_ctask->hdr) - ISCSI_DIGEST_SIZE; | |
5bb0b55a MC |
1893 | } |
1894 | ||
1895 | for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) { | |
1896 | struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i]; | |
1897 | struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data; | |
1898 | ||
a8ac6311 | 1899 | mtask->hdr = (struct iscsi_hdr *) &tcp_mtask->hdr; |
5bb0b55a MC |
1900 | } |
1901 | ||
1902 | if (iscsi_r2tpool_alloc(class_to_transport_session(cls_session))) | |
1903 | goto r2tpool_alloc_fail; | |
1904 | ||
1905 | return cls_session; | |
1906 | ||
1907 | r2tpool_alloc_fail: | |
1908 | iscsi_session_teardown(cls_session); | |
1909 | return NULL; | |
1910 | } | |
1911 | ||
1912 | static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session) | |
1913 | { | |
5bb0b55a MC |
1914 | iscsi_r2tpool_free(class_to_transport_session(cls_session)); |
1915 | iscsi_session_teardown(cls_session); | |
7ba24713 AA |
1916 | } |
1917 | ||
d1d81c01 MC |
1918 | static int iscsi_tcp_slave_configure(struct scsi_device *sdev) |
1919 | { | |
b6d44fe9 | 1920 | blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY); |
d1d81c01 MC |
1921 | blk_queue_dma_alignment(sdev->request_queue, 0); |
1922 | return 0; | |
1923 | } | |
1924 | ||
5bb0b55a | 1925 | static struct scsi_host_template iscsi_sht = { |
7974392c | 1926 | .module = THIS_MODULE, |
f4246b33 | 1927 | .name = "iSCSI Initiator over TCP/IP", |
5bb0b55a MC |
1928 | .queuecommand = iscsi_queuecommand, |
1929 | .change_queue_depth = iscsi_change_queue_depth, | |
1548271e | 1930 | .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1, |
5bb0b55a | 1931 | .sg_tablesize = ISCSI_SG_TABLESIZE, |
8231f0ed | 1932 | .max_sectors = 0xFFFF, |
5bb0b55a MC |
1933 | .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN, |
1934 | .eh_abort_handler = iscsi_eh_abort, | |
843c0a8a | 1935 | .eh_device_reset_handler= iscsi_eh_device_reset, |
5bb0b55a MC |
1936 | .eh_host_reset_handler = iscsi_eh_host_reset, |
1937 | .use_clustering = DISABLE_CLUSTERING, | |
d1d81c01 | 1938 | .slave_configure = iscsi_tcp_slave_configure, |
5bb0b55a MC |
1939 | .proc_name = "iscsi_tcp", |
1940 | .this_id = -1, | |
1941 | }; | |
1942 | ||
7ba24713 AA |
1943 | static struct iscsi_transport iscsi_tcp_transport = { |
1944 | .owner = THIS_MODULE, | |
1945 | .name = "tcp", | |
1946 | .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST | |
1947 | | CAP_DATADGST, | |
fd7255f5 MC |
1948 | .param_mask = ISCSI_MAX_RECV_DLENGTH | |
1949 | ISCSI_MAX_XMIT_DLENGTH | | |
1950 | ISCSI_HDRDGST_EN | | |
1951 | ISCSI_DATADGST_EN | | |
1952 | ISCSI_INITIAL_R2T_EN | | |
1953 | ISCSI_MAX_R2T | | |
1954 | ISCSI_IMM_DATA_EN | | |
1955 | ISCSI_FIRST_BURST | | |
1956 | ISCSI_MAX_BURST | | |
1957 | ISCSI_PDU_INORDER_EN | | |
1958 | ISCSI_DATASEQ_INORDER_EN | | |
1959 | ISCSI_ERL | | |
1960 | ISCSI_CONN_PORT | | |
8d2860b3 | 1961 | ISCSI_CONN_ADDRESS | |
5c75b7fc MC |
1962 | ISCSI_EXP_STATSN | |
1963 | ISCSI_PERSISTENT_PORT | | |
1964 | ISCSI_PERSISTENT_ADDRESS | | |
b2c64167 MC |
1965 | ISCSI_TARGET_NAME | ISCSI_TPGT | |
1966 | ISCSI_USERNAME | ISCSI_PASSWORD | | |
843c0a8a | 1967 | ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN | |
f6d5180c MC |
1968 | ISCSI_FAST_ABORT | ISCSI_ABORT_TMO | |
1969 | ISCSI_LU_RESET_TMO | | |
1970 | ISCSI_PING_TMO | ISCSI_RECV_TMO, | |
22236961 | 1971 | .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS | |
d8196ed2 MC |
1972 | ISCSI_HOST_INITIATOR_NAME | |
1973 | ISCSI_HOST_NETDEV_NAME, | |
7ba24713 | 1974 | .host_template = &iscsi_sht, |
7b8631b5 | 1975 | .conndata_size = sizeof(struct iscsi_conn), |
7ba24713 AA |
1976 | .max_conn = 1, |
1977 | .max_cmd_len = ISCSI_TCP_MAX_CMD_LEN, | |
5bb0b55a MC |
1978 | /* session management */ |
1979 | .create_session = iscsi_tcp_session_create, | |
1980 | .destroy_session = iscsi_tcp_session_destroy, | |
1981 | /* connection management */ | |
1982 | .create_conn = iscsi_tcp_conn_create, | |
1983 | .bind_conn = iscsi_tcp_conn_bind, | |
1984 | .destroy_conn = iscsi_tcp_conn_destroy, | |
7ba24713 | 1985 | .set_param = iscsi_conn_set_param, |
5c75b7fc | 1986 | .get_conn_param = iscsi_tcp_conn_get_param, |
7b8631b5 | 1987 | .get_session_param = iscsi_session_get_param, |
7ba24713 | 1988 | .start_conn = iscsi_conn_start, |
1c83469d | 1989 | .stop_conn = iscsi_tcp_conn_stop, |
0801c242 | 1990 | /* iscsi host params */ |
22236961 | 1991 | .get_host_param = iscsi_tcp_host_get_param, |
0801c242 | 1992 | .set_host_param = iscsi_host_set_param, |
5bb0b55a | 1993 | /* IO */ |
7ba24713 AA |
1994 | .send_pdu = iscsi_conn_send_pdu, |
1995 | .get_stats = iscsi_conn_get_stats, | |
a8ac6311 OK |
1996 | .init_cmd_task = iscsi_tcp_ctask_init, |
1997 | .init_mgmt_task = iscsi_tcp_mtask_init, | |
5bb0b55a MC |
1998 | .xmit_cmd_task = iscsi_tcp_ctask_xmit, |
1999 | .xmit_mgmt_task = iscsi_tcp_mtask_xmit, | |
2000 | .cleanup_cmd_task = iscsi_tcp_cleanup_ctask, | |
2001 | /* recovery */ | |
30a6c652 | 2002 | .session_recovery_timedout = iscsi_session_recovery_timedout, |
7ba24713 AA |
2003 | }; |
2004 | ||
2005 | static int __init | |
2006 | iscsi_tcp_init(void) | |
2007 | { | |
7ba24713 | 2008 | if (iscsi_max_lun < 1) { |
be2df72e OG |
2009 | printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n", |
2010 | iscsi_max_lun); | |
7ba24713 AA |
2011 | return -EINVAL; |
2012 | } | |
2013 | iscsi_tcp_transport.max_lun = iscsi_max_lun; | |
2014 | ||
7b8631b5 | 2015 | if (!iscsi_register_transport(&iscsi_tcp_transport)) |
ffbfe925 | 2016 | return -ENODEV; |
7ba24713 | 2017 | |
7b8631b5 | 2018 | return 0; |
7ba24713 AA |
2019 | } |
2020 | ||
2021 | static void __exit | |
2022 | iscsi_tcp_exit(void) | |
2023 | { | |
2024 | iscsi_unregister_transport(&iscsi_tcp_transport); | |
7ba24713 AA |
2025 | } |
2026 | ||
2027 | module_init(iscsi_tcp_init); | |
2028 | module_exit(iscsi_tcp_exit); |