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