Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * iSCSI Initiator over TCP/IP Data-Path |
| 3 | * |
| 4 | * Copyright (C) 2004 Dmitry Yusupov |
| 5 | * Copyright (C) 2004 Alex Aizman |
| 6 | * Copyright (C) 2005 Mike Christie |
| 7 | * maintained by open-iscsi@googlegroups.com |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published |
| 11 | * by the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * See the file COPYING included with this distribution for more details. |
| 20 | * |
| 21 | * Credits: |
| 22 | * Christoph Hellwig |
| 23 | * FUJITA Tomonori |
| 24 | * Arne Redlich |
| 25 | * Zhenyu Wang |
| 26 | */ |
| 27 | |
| 28 | #include <linux/types.h> |
| 29 | #include <linux/list.h> |
| 30 | #include <linux/inet.h> |
| 31 | #include <linux/blkdev.h> |
| 32 | #include <linux/crypto.h> |
| 33 | #include <linux/delay.h> |
| 34 | #include <linux/kfifo.h> |
| 35 | #include <linux/scatterlist.h> |
| 36 | #include <net/tcp.h> |
| 37 | #include <scsi/scsi_cmnd.h> |
| 38 | #include <scsi/scsi_device.h> |
| 39 | #include <scsi/scsi_eh.h> |
| 40 | #include <scsi/scsi_request.h> |
| 41 | #include <scsi/scsi_tcq.h> |
| 42 | #include <scsi/scsi_host.h> |
| 43 | #include <scsi/scsi.h> |
| 44 | #include <scsi/scsi_transport_iscsi.h> |
| 45 | |
| 46 | #include "iscsi_tcp.h" |
| 47 | |
| 48 | MODULE_AUTHOR("Dmitry Yusupov <dmitry_yus@yahoo.com>, " |
| 49 | "Alex Aizman <itn780@yahoo.com>"); |
| 50 | MODULE_DESCRIPTION("iSCSI/TCP data-path"); |
| 51 | MODULE_LICENSE("GPL"); |
Mike Christie | beb8abd | 2005-09-12 21:02:06 -0500 | [diff] [blame] | 52 | MODULE_VERSION("0:4.409"); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 53 | /* #define DEBUG_TCP */ |
| 54 | /* #define DEBUG_SCSI */ |
| 55 | #define DEBUG_ASSERT |
| 56 | |
| 57 | #ifdef DEBUG_TCP |
| 58 | #define debug_tcp(fmt...) printk(KERN_DEBUG "tcp: " fmt) |
| 59 | #else |
| 60 | #define debug_tcp(fmt...) |
| 61 | #endif |
| 62 | |
| 63 | #ifdef DEBUG_SCSI |
| 64 | #define debug_scsi(fmt...) printk(KERN_DEBUG "scsi: " fmt) |
| 65 | #else |
| 66 | #define debug_scsi(fmt...) |
| 67 | #endif |
| 68 | |
| 69 | #ifndef DEBUG_ASSERT |
| 70 | #ifdef BUG_ON |
| 71 | #undef BUG_ON |
| 72 | #endif |
| 73 | #define BUG_ON(expr) |
| 74 | #endif |
| 75 | |
| 76 | #define INVALID_SN_DELTA 0xffff |
| 77 | |
| 78 | static unsigned int iscsi_max_lun = 512; |
| 79 | module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO); |
| 80 | |
| 81 | /* global data */ |
| 82 | static kmem_cache_t *taskcache; |
| 83 | |
| 84 | static inline void |
| 85 | iscsi_buf_init_virt(struct iscsi_buf *ibuf, char *vbuf, int size) |
| 86 | { |
| 87 | sg_init_one(&ibuf->sg, (u8 *)vbuf, size); |
| 88 | ibuf->sent = 0; |
| 89 | } |
| 90 | |
| 91 | static inline void |
| 92 | iscsi_buf_init_iov(struct iscsi_buf *ibuf, char *vbuf, int size) |
| 93 | { |
| 94 | ibuf->sg.page = (void*)vbuf; |
| 95 | ibuf->sg.offset = (unsigned int)-1; |
| 96 | ibuf->sg.length = size; |
| 97 | ibuf->sent = 0; |
| 98 | } |
| 99 | |
| 100 | static inline void* |
| 101 | iscsi_buf_iov_base(struct iscsi_buf *ibuf) |
| 102 | { |
| 103 | return (char*)ibuf->sg.page + ibuf->sent; |
| 104 | } |
| 105 | |
| 106 | static inline void |
| 107 | iscsi_buf_init_sg(struct iscsi_buf *ibuf, struct scatterlist *sg) |
| 108 | { |
| 109 | /* |
| 110 | * Fastpath: sg element fits into single page |
| 111 | */ |
| 112 | if (sg->length + sg->offset <= PAGE_SIZE && page_count(sg->page) >= 2) { |
| 113 | ibuf->sg.page = sg->page; |
| 114 | ibuf->sg.offset = sg->offset; |
| 115 | ibuf->sg.length = sg->length; |
| 116 | } else |
| 117 | iscsi_buf_init_iov(ibuf, page_address(sg->page), sg->length); |
| 118 | ibuf->sent = 0; |
| 119 | } |
| 120 | |
| 121 | static inline int |
| 122 | iscsi_buf_left(struct iscsi_buf *ibuf) |
| 123 | { |
| 124 | int rc; |
| 125 | |
| 126 | rc = ibuf->sg.length - ibuf->sent; |
| 127 | BUG_ON(rc < 0); |
| 128 | return rc; |
| 129 | } |
| 130 | |
| 131 | static inline void |
Mike Christie | af97348 | 2005-09-12 21:01:32 -0500 | [diff] [blame] | 132 | iscsi_hdr_digest(struct iscsi_conn *conn, struct iscsi_buf *buf, |
| 133 | u8* crc) |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 134 | { |
Mike Christie | af97348 | 2005-09-12 21:01:32 -0500 | [diff] [blame] | 135 | crypto_digest_digest(conn->tx_tfm, &buf->sg, 1, crc); |
| 136 | buf->sg.length += sizeof(uint32_t); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | static void |
| 140 | iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err) |
| 141 | { |
| 142 | struct iscsi_session *session = conn->session; |
| 143 | unsigned long flags; |
| 144 | |
| 145 | spin_lock_irqsave(&session->lock, flags); |
| 146 | if (session->conn_cnt == 1 || session->leadconn == conn) |
| 147 | session->state = ISCSI_STATE_FAILED; |
| 148 | spin_unlock_irqrestore(&session->lock, flags); |
| 149 | set_bit(SUSPEND_BIT, &conn->suspend_tx); |
| 150 | set_bit(SUSPEND_BIT, &conn->suspend_rx); |
| 151 | iscsi_conn_error(iscsi_handle(conn), err); |
| 152 | } |
| 153 | |
| 154 | static inline int |
| 155 | iscsi_check_assign_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr) |
| 156 | { |
| 157 | uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn); |
| 158 | uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn); |
| 159 | |
| 160 | if (max_cmdsn < exp_cmdsn -1 && |
| 161 | max_cmdsn > exp_cmdsn - INVALID_SN_DELTA) |
| 162 | return ISCSI_ERR_MAX_CMDSN; |
| 163 | if (max_cmdsn > session->max_cmdsn || |
| 164 | max_cmdsn < session->max_cmdsn - INVALID_SN_DELTA) |
| 165 | session->max_cmdsn = max_cmdsn; |
| 166 | if (exp_cmdsn > session->exp_cmdsn || |
| 167 | exp_cmdsn < session->exp_cmdsn - INVALID_SN_DELTA) |
| 168 | session->exp_cmdsn = exp_cmdsn; |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | static inline int |
| 174 | iscsi_hdr_extract(struct iscsi_conn *conn) |
| 175 | { |
| 176 | struct sk_buff *skb = conn->in.skb; |
| 177 | |
| 178 | if (conn->in.copy >= conn->hdr_size && |
| 179 | conn->in_progress == IN_PROGRESS_WAIT_HEADER) { |
| 180 | /* |
| 181 | * Zero-copy PDU Header: using connection context |
| 182 | * to store header pointer. |
| 183 | */ |
| 184 | if (skb_shinfo(skb)->frag_list == NULL && |
| 185 | !skb_shinfo(skb)->nr_frags) |
| 186 | conn->in.hdr = (struct iscsi_hdr *) |
| 187 | ((char*)skb->data + conn->in.offset); |
| 188 | else { |
| 189 | /* ignoring return code since we checked |
| 190 | * in.copy before */ |
| 191 | skb_copy_bits(skb, conn->in.offset, |
| 192 | &conn->hdr, conn->hdr_size); |
| 193 | conn->in.hdr = &conn->hdr; |
| 194 | } |
| 195 | conn->in.offset += conn->hdr_size; |
| 196 | conn->in.copy -= conn->hdr_size; |
| 197 | } else { |
| 198 | int hdr_remains; |
| 199 | int copylen; |
| 200 | |
| 201 | /* |
| 202 | * PDU header scattered across SKB's, |
| 203 | * copying it... This'll happen quite rarely. |
| 204 | */ |
| 205 | |
| 206 | if (conn->in_progress == IN_PROGRESS_WAIT_HEADER) |
| 207 | conn->in.hdr_offset = 0; |
| 208 | |
| 209 | hdr_remains = conn->hdr_size - conn->in.hdr_offset; |
| 210 | BUG_ON(hdr_remains <= 0); |
| 211 | |
| 212 | copylen = min(conn->in.copy, hdr_remains); |
| 213 | skb_copy_bits(skb, conn->in.offset, |
| 214 | (char*)&conn->hdr + conn->in.hdr_offset, copylen); |
| 215 | |
| 216 | debug_tcp("PDU gather offset %d bytes %d in.offset %d " |
| 217 | "in.copy %d\n", conn->in.hdr_offset, copylen, |
| 218 | conn->in.offset, conn->in.copy); |
| 219 | |
| 220 | conn->in.offset += copylen; |
| 221 | conn->in.copy -= copylen; |
| 222 | if (copylen < hdr_remains) { |
| 223 | conn->in_progress = IN_PROGRESS_HEADER_GATHER; |
| 224 | conn->in.hdr_offset += copylen; |
| 225 | return -EAGAIN; |
| 226 | } |
| 227 | conn->in.hdr = &conn->hdr; |
| 228 | conn->discontiguous_hdr_cnt++; |
| 229 | conn->in_progress = IN_PROGRESS_WAIT_HEADER; |
| 230 | } |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | static inline void |
| 236 | iscsi_ctask_cleanup(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) |
| 237 | { |
| 238 | struct scsi_cmnd *sc = ctask->sc; |
| 239 | struct iscsi_session *session = conn->session; |
| 240 | |
| 241 | spin_lock(&session->lock); |
| 242 | if (unlikely(!sc)) { |
| 243 | spin_unlock(&session->lock); |
| 244 | return; |
| 245 | } |
| 246 | if (sc->sc_data_direction == DMA_TO_DEVICE) { |
| 247 | struct iscsi_data_task *dtask, *n; |
| 248 | /* WRITE: cleanup Data-Out's if any */ |
| 249 | spin_lock(&conn->lock); |
| 250 | list_for_each_entry_safe(dtask, n, &ctask->dataqueue, item) { |
| 251 | list_del(&dtask->item); |
| 252 | mempool_free(dtask, ctask->datapool); |
| 253 | } |
| 254 | spin_unlock(&conn->lock); |
| 255 | } |
| 256 | ctask->xmstate = XMSTATE_IDLE; |
| 257 | ctask->r2t = NULL; |
| 258 | ctask->sc = NULL; |
| 259 | __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*)); |
| 260 | spin_unlock(&session->lock); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * iscsi_cmd_rsp - SCSI Command Response processing |
| 265 | * @conn: iscsi connection |
| 266 | * @ctask: scsi command task |
| 267 | **/ |
| 268 | static int |
| 269 | iscsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) |
| 270 | { |
| 271 | int rc; |
| 272 | struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)conn->in.hdr; |
| 273 | struct iscsi_session *session = conn->session; |
| 274 | struct scsi_cmnd *sc = ctask->sc; |
| 275 | |
| 276 | rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr); |
| 277 | if (rc) { |
| 278 | sc->result = (DID_ERROR << 16); |
| 279 | goto out; |
| 280 | } |
| 281 | |
| 282 | conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1; |
| 283 | |
| 284 | sc->result = (DID_OK << 16) | rhdr->cmd_status; |
| 285 | |
| 286 | if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) { |
| 287 | sc->result = (DID_ERROR << 16); |
| 288 | goto out; |
| 289 | } |
| 290 | |
| 291 | if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION && conn->senselen) { |
| 292 | int sensecopy = min(conn->senselen, SCSI_SENSE_BUFFERSIZE); |
| 293 | |
| 294 | memcpy(sc->sense_buffer, conn->data + 2, sensecopy); |
| 295 | debug_scsi("copied %d bytes of sense\n", sensecopy); |
| 296 | } |
| 297 | |
| 298 | if (sc->sc_data_direction == DMA_TO_DEVICE) |
| 299 | goto out; |
| 300 | |
| 301 | if (rhdr->flags & ISCSI_FLAG_CMD_UNDERFLOW) { |
| 302 | int res_count = be32_to_cpu(rhdr->residual_count); |
| 303 | |
| 304 | if (res_count > 0 && res_count <= sc->request_bufflen) |
| 305 | sc->resid = res_count; |
| 306 | else |
| 307 | sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status; |
| 308 | } else if (rhdr->flags & ISCSI_FLAG_CMD_BIDI_UNDERFLOW) |
| 309 | sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status; |
| 310 | else if (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW) |
| 311 | sc->resid = be32_to_cpu(rhdr->residual_count); |
| 312 | |
| 313 | out: |
| 314 | debug_scsi("done [sc %lx res %d itt 0x%x]\n", |
| 315 | (long)sc, sc->result, ctask->itt); |
| 316 | conn->scsirsp_pdus_cnt++; |
| 317 | iscsi_ctask_cleanup(conn, ctask); |
| 318 | sc->scsi_done(sc); |
| 319 | return rc; |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * iscsi_data_rsp - SCSI Data-In Response processing |
| 324 | * @conn: iscsi connection |
| 325 | * @ctask: scsi command task |
| 326 | **/ |
| 327 | static int |
| 328 | iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) |
| 329 | { |
| 330 | int rc; |
| 331 | struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)conn->in.hdr; |
| 332 | struct iscsi_session *session = conn->session; |
| 333 | int datasn = be32_to_cpu(rhdr->datasn); |
| 334 | |
| 335 | rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr); |
| 336 | if (rc) |
| 337 | return rc; |
| 338 | /* |
| 339 | * setup Data-In byte counter (gets decremented..) |
| 340 | */ |
| 341 | ctask->data_count = conn->in.datalen; |
| 342 | |
| 343 | if (conn->in.datalen == 0) |
| 344 | return 0; |
| 345 | |
| 346 | if (ctask->datasn != datasn) |
| 347 | return ISCSI_ERR_DATASN; |
| 348 | |
| 349 | ctask->datasn++; |
| 350 | |
| 351 | ctask->data_offset = be32_to_cpu(rhdr->offset); |
| 352 | if (ctask->data_offset + conn->in.datalen > ctask->total_length) |
| 353 | return ISCSI_ERR_DATA_OFFSET; |
| 354 | |
| 355 | if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) { |
| 356 | struct scsi_cmnd *sc = ctask->sc; |
| 357 | |
| 358 | conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1; |
| 359 | if (rhdr->flags & ISCSI_FLAG_CMD_UNDERFLOW) { |
| 360 | int res_count = be32_to_cpu(rhdr->residual_count); |
| 361 | |
| 362 | if (res_count > 0 && |
| 363 | res_count <= sc->request_bufflen) { |
| 364 | sc->resid = res_count; |
| 365 | sc->result = (DID_OK << 16) | rhdr->cmd_status; |
| 366 | } else |
| 367 | sc->result = (DID_BAD_TARGET << 16) | |
| 368 | rhdr->cmd_status; |
| 369 | } else if (rhdr->flags & ISCSI_FLAG_CMD_BIDI_UNDERFLOW) |
| 370 | sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status; |
| 371 | else if (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW) { |
| 372 | sc->resid = be32_to_cpu(rhdr->residual_count); |
| 373 | sc->result = (DID_OK << 16) | rhdr->cmd_status; |
| 374 | } else |
| 375 | sc->result = (DID_OK << 16) | rhdr->cmd_status; |
| 376 | } |
| 377 | |
| 378 | conn->datain_pdus_cnt++; |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * iscsi_solicit_data_init - initialize first Data-Out |
| 384 | * @conn: iscsi connection |
| 385 | * @ctask: scsi command task |
| 386 | * @r2t: R2T info |
| 387 | * |
| 388 | * Notes: |
| 389 | * Initialize first Data-Out within this R2T sequence and finds |
| 390 | * proper data_offset within this SCSI command. |
| 391 | * |
| 392 | * This function is called with connection lock taken. |
| 393 | **/ |
| 394 | static void |
| 395 | iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask, |
| 396 | struct iscsi_r2t_info *r2t) |
| 397 | { |
| 398 | struct iscsi_data *hdr; |
| 399 | struct iscsi_data_task *dtask; |
| 400 | struct scsi_cmnd *sc = ctask->sc; |
| 401 | |
| 402 | dtask = mempool_alloc(ctask->datapool, GFP_ATOMIC); |
| 403 | BUG_ON(!dtask); |
| 404 | hdr = &dtask->hdr; |
| 405 | memset(hdr, 0, sizeof(struct iscsi_data)); |
| 406 | hdr->ttt = r2t->ttt; |
| 407 | hdr->datasn = cpu_to_be32(r2t->solicit_datasn); |
| 408 | r2t->solicit_datasn++; |
| 409 | hdr->opcode = ISCSI_OP_SCSI_DATA_OUT; |
| 410 | memcpy(hdr->lun, ctask->hdr.lun, sizeof(hdr->lun)); |
| 411 | hdr->itt = ctask->hdr.itt; |
| 412 | hdr->exp_statsn = r2t->exp_statsn; |
| 413 | hdr->offset = cpu_to_be32(r2t->data_offset); |
| 414 | if (r2t->data_length > conn->max_xmit_dlength) { |
| 415 | hton24(hdr->dlength, conn->max_xmit_dlength); |
| 416 | r2t->data_count = conn->max_xmit_dlength; |
| 417 | hdr->flags = 0; |
| 418 | } else { |
| 419 | hton24(hdr->dlength, r2t->data_length); |
| 420 | r2t->data_count = r2t->data_length; |
| 421 | hdr->flags = ISCSI_FLAG_CMD_FINAL; |
| 422 | } |
| 423 | conn->dataout_pdus_cnt++; |
| 424 | |
| 425 | r2t->sent = 0; |
| 426 | |
Mike Christie | af97348 | 2005-09-12 21:01:32 -0500 | [diff] [blame] | 427 | iscsi_buf_init_virt(&r2t->headbuf, (char*)hdr, |
| 428 | sizeof(struct iscsi_hdr)); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 429 | |
| 430 | r2t->dtask = dtask; |
| 431 | |
| 432 | if (sc->use_sg) { |
| 433 | int i, sg_count = 0; |
| 434 | struct scatterlist *sg = sc->request_buffer; |
| 435 | |
| 436 | r2t->sg = NULL; |
| 437 | for (i = 0; i < sc->use_sg; i++, sg += 1) { |
| 438 | /* FIXME: prefetch ? */ |
| 439 | if (sg_count + sg->length > r2t->data_offset) { |
| 440 | int page_offset; |
| 441 | |
| 442 | /* sg page found! */ |
| 443 | |
| 444 | /* offset within this page */ |
| 445 | page_offset = r2t->data_offset - sg_count; |
| 446 | |
| 447 | /* fill in this buffer */ |
| 448 | iscsi_buf_init_sg(&r2t->sendbuf, sg); |
| 449 | r2t->sendbuf.sg.offset += page_offset; |
| 450 | r2t->sendbuf.sg.length -= page_offset; |
| 451 | |
| 452 | /* xmit logic will continue with next one */ |
| 453 | r2t->sg = sg + 1; |
| 454 | break; |
| 455 | } |
| 456 | sg_count += sg->length; |
| 457 | } |
| 458 | BUG_ON(r2t->sg == NULL); |
| 459 | } else |
| 460 | iscsi_buf_init_iov(&ctask->sendbuf, |
| 461 | (char*)sc->request_buffer + r2t->data_offset, |
| 462 | r2t->data_count); |
| 463 | |
| 464 | list_add(&dtask->item, &ctask->dataqueue); |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * iscsi_r2t_rsp - iSCSI R2T Response processing |
| 469 | * @conn: iscsi connection |
| 470 | * @ctask: scsi command task |
| 471 | **/ |
| 472 | static int |
| 473 | iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) |
| 474 | { |
| 475 | struct iscsi_r2t_info *r2t; |
| 476 | struct iscsi_session *session = conn->session; |
| 477 | struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)conn->in.hdr; |
| 478 | int r2tsn = be32_to_cpu(rhdr->r2tsn); |
| 479 | int rc; |
| 480 | |
| 481 | if (conn->in.ahslen) |
| 482 | return ISCSI_ERR_AHSLEN; |
| 483 | |
| 484 | if (conn->in.datalen) |
| 485 | return ISCSI_ERR_DATALEN; |
| 486 | |
| 487 | if (ctask->exp_r2tsn && ctask->exp_r2tsn != r2tsn) |
| 488 | return ISCSI_ERR_R2TSN; |
| 489 | |
| 490 | rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr); |
| 491 | if (rc) |
| 492 | return rc; |
| 493 | |
| 494 | /* FIXME: use R2TSN to detect missing R2T */ |
| 495 | |
| 496 | /* fill-in new R2T associated with the task */ |
| 497 | spin_lock(&session->lock); |
| 498 | if (!ctask->sc || ctask->mtask || |
| 499 | session->state != ISCSI_STATE_LOGGED_IN) { |
| 500 | printk(KERN_INFO "iscsi_tcp: dropping R2T itt %d in " |
| 501 | "recovery...\n", ctask->itt); |
| 502 | spin_unlock(&session->lock); |
| 503 | return 0; |
| 504 | } |
| 505 | rc = __kfifo_get(ctask->r2tpool.queue, (void*)&r2t, sizeof(void*)); |
| 506 | BUG_ON(!rc); |
| 507 | |
| 508 | r2t->exp_statsn = rhdr->statsn; |
| 509 | r2t->data_length = be32_to_cpu(rhdr->data_length); |
| 510 | if (r2t->data_length == 0 || |
| 511 | r2t->data_length > session->max_burst) { |
| 512 | spin_unlock(&session->lock); |
| 513 | return ISCSI_ERR_DATALEN; |
| 514 | } |
| 515 | |
| 516 | r2t->data_offset = be32_to_cpu(rhdr->data_offset); |
| 517 | if (r2t->data_offset + r2t->data_length > ctask->total_length) { |
| 518 | spin_unlock(&session->lock); |
| 519 | return ISCSI_ERR_DATALEN; |
| 520 | } |
| 521 | |
| 522 | r2t->ttt = rhdr->ttt; /* no flip */ |
| 523 | r2t->solicit_datasn = 0; |
| 524 | |
| 525 | iscsi_solicit_data_init(conn, ctask, r2t); |
| 526 | |
| 527 | ctask->exp_r2tsn = r2tsn + 1; |
| 528 | ctask->xmstate |= XMSTATE_SOL_HDR; |
| 529 | __kfifo_put(ctask->r2tqueue, (void*)&r2t, sizeof(void*)); |
| 530 | __kfifo_put(conn->writequeue, (void*)&ctask, sizeof(void*)); |
| 531 | |
| 532 | schedule_work(&conn->xmitwork); |
| 533 | conn->r2t_pdus_cnt++; |
| 534 | spin_unlock(&session->lock); |
| 535 | |
| 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | static int |
| 540 | iscsi_hdr_recv(struct iscsi_conn *conn) |
| 541 | { |
| 542 | int rc = 0; |
| 543 | struct iscsi_hdr *hdr; |
| 544 | struct iscsi_cmd_task *ctask; |
| 545 | struct iscsi_session *session = conn->session; |
| 546 | uint32_t cdgst, rdgst = 0; |
| 547 | |
| 548 | hdr = conn->in.hdr; |
| 549 | |
| 550 | /* verify PDU length */ |
| 551 | conn->in.datalen = ntoh24(hdr->dlength); |
| 552 | if (conn->in.datalen > conn->max_recv_dlength) { |
| 553 | printk(KERN_ERR "iscsi_tcp: datalen %d > %d\n", |
| 554 | conn->in.datalen, conn->max_recv_dlength); |
| 555 | return ISCSI_ERR_DATALEN; |
| 556 | } |
| 557 | conn->data_copied = 0; |
| 558 | |
| 559 | /* read AHS */ |
Mike Christie | 02cf931 | 2005-09-12 21:01:41 -0500 | [diff] [blame] | 560 | conn->in.ahslen = hdr->hlength * 4; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 561 | conn->in.offset += conn->in.ahslen; |
| 562 | conn->in.copy -= conn->in.ahslen; |
| 563 | if (conn->in.copy < 0) { |
| 564 | printk(KERN_ERR "iscsi_tcp: can't handle AHS with length " |
| 565 | "%d bytes\n", conn->in.ahslen); |
| 566 | return ISCSI_ERR_AHSLEN; |
| 567 | } |
| 568 | |
| 569 | /* calculate read padding */ |
| 570 | conn->in.padding = conn->in.datalen & (ISCSI_PAD_LEN-1); |
| 571 | if (conn->in.padding) { |
| 572 | conn->in.padding = ISCSI_PAD_LEN - conn->in.padding; |
| 573 | debug_scsi("read padding %d bytes\n", conn->in.padding); |
| 574 | } |
| 575 | |
| 576 | if (conn->hdrdgst_en) { |
| 577 | struct scatterlist sg; |
| 578 | |
| 579 | sg_init_one(&sg, (u8 *)hdr, |
| 580 | sizeof(struct iscsi_hdr) + conn->in.ahslen); |
| 581 | crypto_digest_digest(conn->rx_tfm, &sg, 1, (u8 *)&cdgst); |
| 582 | rdgst = *(uint32_t*)((char*)hdr + sizeof(struct iscsi_hdr) + |
| 583 | conn->in.ahslen); |
| 584 | } |
| 585 | |
| 586 | /* save opcode for later */ |
Mike Christie | 0d2f165 | 2005-11-29 23:12:46 -0600 | [diff] [blame] | 587 | conn->in.opcode = hdr->opcode & ISCSI_OPCODE_MASK; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 588 | |
| 589 | /* verify itt (itt encoding: age+cid+itt) */ |
| 590 | if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) { |
| 591 | if ((hdr->itt & AGE_MASK) != |
| 592 | (session->age << AGE_SHIFT)) { |
| 593 | printk(KERN_ERR "iscsi_tcp: received itt %x expected " |
| 594 | "session age (%x)\n", hdr->itt, |
| 595 | session->age & AGE_MASK); |
| 596 | return ISCSI_ERR_BAD_ITT; |
| 597 | } |
| 598 | |
| 599 | if ((hdr->itt & CID_MASK) != (conn->id << CID_SHIFT)) { |
| 600 | printk(KERN_ERR "iscsi_tcp: received itt %x, expected " |
| 601 | "CID (%x)\n", hdr->itt, conn->id); |
| 602 | return ISCSI_ERR_BAD_ITT; |
| 603 | } |
| 604 | conn->in.itt = hdr->itt & ITT_MASK; |
| 605 | } else |
| 606 | conn->in.itt = hdr->itt; |
| 607 | |
| 608 | debug_tcp("opcode 0x%x offset %d copy %d ahslen %d datalen %d\n", |
| 609 | hdr->opcode, conn->in.offset, conn->in.copy, |
| 610 | conn->in.ahslen, conn->in.datalen); |
| 611 | |
| 612 | if (conn->in.itt < session->cmds_max) { |
| 613 | if (conn->hdrdgst_en && cdgst != rdgst) { |
| 614 | printk(KERN_ERR "iscsi_tcp: itt %x: hdrdgst error " |
| 615 | "recv 0x%x calc 0x%x\n", conn->in.itt, rdgst, |
| 616 | cdgst); |
| 617 | return ISCSI_ERR_HDR_DGST; |
| 618 | } |
| 619 | |
| 620 | ctask = (struct iscsi_cmd_task *)session->cmds[conn->in.itt]; |
| 621 | |
| 622 | if (!ctask->sc) { |
| 623 | printk(KERN_INFO "iscsi_tcp: dropping ctask with " |
| 624 | "itt 0x%x\n", ctask->itt); |
| 625 | conn->in.datalen = 0; /* force drop */ |
| 626 | return 0; |
| 627 | } |
| 628 | |
| 629 | if (ctask->sc->SCp.phase != session->age) { |
| 630 | printk(KERN_ERR "iscsi_tcp: ctask's session age %d, " |
| 631 | "expected %d\n", ctask->sc->SCp.phase, |
| 632 | session->age); |
| 633 | return ISCSI_ERR_SESSION_FAILED; |
| 634 | } |
| 635 | |
| 636 | conn->in.ctask = ctask; |
| 637 | |
| 638 | debug_scsi("rsp [op 0x%x cid %d sc %lx itt 0x%x len %d]\n", |
| 639 | hdr->opcode, conn->id, (long)ctask->sc, |
| 640 | ctask->itt, conn->in.datalen); |
| 641 | |
| 642 | switch(conn->in.opcode) { |
| 643 | case ISCSI_OP_SCSI_CMD_RSP: |
| 644 | BUG_ON((void*)ctask != ctask->sc->SCp.ptr); |
| 645 | if (ctask->hdr.flags & ISCSI_FLAG_CMD_WRITE) |
| 646 | rc = iscsi_cmd_rsp(conn, ctask); |
| 647 | else if (!conn->in.datalen) |
| 648 | rc = iscsi_cmd_rsp(conn, ctask); |
| 649 | else |
| 650 | /* |
| 651 | * got sense or response data; copying PDU |
| 652 | * Header to the connection's header |
| 653 | * placeholder |
| 654 | */ |
| 655 | memcpy(&conn->hdr, hdr, |
| 656 | sizeof(struct iscsi_hdr)); |
| 657 | break; |
| 658 | case ISCSI_OP_SCSI_DATA_IN: |
| 659 | BUG_ON((void*)ctask != ctask->sc->SCp.ptr); |
| 660 | /* save flags for non-exceptional status */ |
| 661 | conn->in.flags = hdr->flags; |
| 662 | /* save cmd_status for sense data */ |
| 663 | conn->in.cmd_status = |
| 664 | ((struct iscsi_data_rsp*)hdr)->cmd_status; |
| 665 | rc = iscsi_data_rsp(conn, ctask); |
| 666 | break; |
| 667 | case ISCSI_OP_R2T: |
| 668 | BUG_ON((void*)ctask != ctask->sc->SCp.ptr); |
| 669 | if (ctask->hdr.flags & ISCSI_FLAG_CMD_WRITE && |
| 670 | ctask->sc->sc_data_direction == DMA_TO_DEVICE) |
| 671 | rc = iscsi_r2t_rsp(conn, ctask); |
| 672 | else |
| 673 | rc = ISCSI_ERR_PROTO; |
| 674 | break; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 675 | default: |
| 676 | rc = ISCSI_ERR_BAD_OPCODE; |
| 677 | break; |
| 678 | } |
| 679 | } else if (conn->in.itt >= ISCSI_MGMT_ITT_OFFSET && |
| 680 | conn->in.itt < ISCSI_MGMT_ITT_OFFSET + |
| 681 | session->mgmtpool_max) { |
| 682 | struct iscsi_mgmt_task *mtask = (struct iscsi_mgmt_task *) |
| 683 | session->mgmt_cmds[conn->in.itt - |
| 684 | ISCSI_MGMT_ITT_OFFSET]; |
| 685 | |
| 686 | debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n", |
| 687 | conn->in.opcode, conn->id, mtask->itt, |
| 688 | conn->in.datalen); |
| 689 | |
| 690 | switch(conn->in.opcode) { |
| 691 | case ISCSI_OP_LOGIN_RSP: |
| 692 | case ISCSI_OP_TEXT_RSP: |
Mike Christie | 9974487 | 2005-09-12 21:02:04 -0500 | [diff] [blame] | 693 | case ISCSI_OP_LOGOUT_RSP: |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 694 | rc = iscsi_check_assign_cmdsn(session, |
| 695 | (struct iscsi_nopin*)hdr); |
| 696 | if (rc) |
| 697 | break; |
| 698 | |
| 699 | if (!conn->in.datalen) { |
| 700 | rc = iscsi_recv_pdu(iscsi_handle(conn), hdr, |
| 701 | NULL, 0); |
| 702 | if (conn->login_mtask != mtask) { |
| 703 | spin_lock(&session->lock); |
| 704 | __kfifo_put(session->mgmtpool.queue, |
| 705 | (void*)&mtask, sizeof(void*)); |
| 706 | spin_unlock(&session->lock); |
| 707 | } |
| 708 | } |
| 709 | break; |
| 710 | case ISCSI_OP_SCSI_TMFUNC_RSP: |
| 711 | rc = iscsi_check_assign_cmdsn(session, |
| 712 | (struct iscsi_nopin*)hdr); |
| 713 | if (rc) |
| 714 | break; |
| 715 | |
| 716 | if (conn->in.datalen || conn->in.ahslen) { |
| 717 | rc = ISCSI_ERR_PROTO; |
| 718 | break; |
| 719 | } |
| 720 | conn->tmfrsp_pdus_cnt++; |
| 721 | spin_lock(&session->lock); |
| 722 | if (conn->tmabort_state == TMABORT_INITIAL) { |
| 723 | __kfifo_put(session->mgmtpool.queue, |
| 724 | (void*)&mtask, sizeof(void*)); |
| 725 | conn->tmabort_state = |
| 726 | ((struct iscsi_tm_rsp *)hdr)-> |
Mike Christie | baebc49 | 2005-09-12 21:01:38 -0500 | [diff] [blame] | 727 | response == ISCSI_TMF_RSP_COMPLETE ? |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 728 | TMABORT_SUCCESS:TMABORT_FAILED; |
| 729 | /* unblock eh_abort() */ |
| 730 | wake_up(&conn->ehwait); |
| 731 | } |
| 732 | spin_unlock(&session->lock); |
| 733 | break; |
Mike Christie | 9974487 | 2005-09-12 21:02:04 -0500 | [diff] [blame] | 734 | case ISCSI_OP_NOOP_IN: |
| 735 | if (hdr->ttt != ISCSI_RESERVED_TAG) { |
| 736 | rc = ISCSI_ERR_PROTO; |
| 737 | break; |
| 738 | } |
| 739 | rc = iscsi_check_assign_cmdsn(session, |
| 740 | (struct iscsi_nopin*)hdr); |
| 741 | if (rc) |
| 742 | break; |
| 743 | conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; |
| 744 | |
| 745 | if (!conn->in.datalen) { |
| 746 | struct iscsi_mgmt_task *mtask; |
| 747 | |
| 748 | rc = iscsi_recv_pdu(iscsi_handle(conn), hdr, |
| 749 | NULL, 0); |
| 750 | mtask = (struct iscsi_mgmt_task *) |
| 751 | session->mgmt_cmds[conn->in.itt - |
| 752 | ISCSI_MGMT_ITT_OFFSET]; |
| 753 | if (conn->login_mtask != mtask) { |
| 754 | spin_lock(&session->lock); |
| 755 | __kfifo_put(session->mgmtpool.queue, |
| 756 | (void*)&mtask, sizeof(void*)); |
| 757 | spin_unlock(&session->lock); |
| 758 | } |
| 759 | } |
| 760 | break; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 761 | default: |
| 762 | rc = ISCSI_ERR_BAD_OPCODE; |
| 763 | break; |
| 764 | } |
| 765 | } else if (conn->in.itt == ISCSI_RESERVED_TAG) { |
Mike Christie | 9974487 | 2005-09-12 21:02:04 -0500 | [diff] [blame] | 766 | switch(conn->in.opcode) { |
| 767 | case ISCSI_OP_NOOP_IN: |
| 768 | if (!conn->in.datalen) { |
| 769 | rc = iscsi_check_assign_cmdsn(session, |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 770 | (struct iscsi_nopin*)hdr); |
Mike Christie | 9974487 | 2005-09-12 21:02:04 -0500 | [diff] [blame] | 771 | if (!rc && hdr->ttt != ISCSI_RESERVED_TAG) |
| 772 | rc = iscsi_recv_pdu(iscsi_handle(conn), |
| 773 | hdr, NULL, 0); |
| 774 | } else |
| 775 | rc = ISCSI_ERR_PROTO; |
| 776 | break; |
| 777 | case ISCSI_OP_REJECT: |
| 778 | /* we need sth like iscsi_reject_rsp()*/ |
| 779 | case ISCSI_OP_ASYNC_EVENT: |
| 780 | /* we need sth like iscsi_async_event_rsp() */ |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 781 | rc = ISCSI_ERR_BAD_OPCODE; |
Mike Christie | 9974487 | 2005-09-12 21:02:04 -0500 | [diff] [blame] | 782 | break; |
| 783 | default: |
| 784 | rc = ISCSI_ERR_BAD_OPCODE; |
| 785 | break; |
| 786 | } |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 787 | } else |
| 788 | rc = ISCSI_ERR_BAD_ITT; |
| 789 | |
| 790 | return rc; |
| 791 | } |
| 792 | |
| 793 | /** |
| 794 | * iscsi_ctask_copy - copy skb bits to the destanation cmd task |
| 795 | * @conn: iscsi connection |
| 796 | * @ctask: scsi command task |
| 797 | * @buf: buffer to copy to |
| 798 | * @buf_size: size of buffer |
| 799 | * @offset: offset within the buffer |
| 800 | * |
| 801 | * Notes: |
| 802 | * The function calls skb_copy_bits() and updates per-connection and |
| 803 | * per-cmd byte counters. |
| 804 | * |
| 805 | * Read counters (in bytes): |
| 806 | * |
| 807 | * conn->in.offset offset within in progress SKB |
| 808 | * conn->in.copy left to copy from in progress SKB |
| 809 | * including padding |
| 810 | * conn->in.copied copied already from in progress SKB |
| 811 | * conn->data_copied copied already from in progress buffer |
| 812 | * ctask->sent total bytes sent up to the MidLayer |
| 813 | * ctask->data_count left to copy from in progress Data-In |
| 814 | * buf_left left to copy from in progress buffer |
| 815 | **/ |
| 816 | static inline int |
| 817 | iscsi_ctask_copy(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask, |
| 818 | void *buf, int buf_size, int offset) |
| 819 | { |
| 820 | int buf_left = buf_size - (conn->data_copied + offset); |
| 821 | int size = min(conn->in.copy, buf_left); |
| 822 | int rc; |
| 823 | |
| 824 | size = min(size, ctask->data_count); |
| 825 | |
| 826 | debug_tcp("ctask_copy %d bytes at offset %d copied %d\n", |
| 827 | size, conn->in.offset, conn->in.copied); |
| 828 | |
| 829 | BUG_ON(size <= 0); |
| 830 | BUG_ON(ctask->sent + size > ctask->total_length); |
| 831 | |
| 832 | rc = skb_copy_bits(conn->in.skb, conn->in.offset, |
| 833 | (char*)buf + (offset + conn->data_copied), size); |
| 834 | /* must fit into skb->len */ |
| 835 | BUG_ON(rc); |
| 836 | |
| 837 | conn->in.offset += size; |
| 838 | conn->in.copy -= size; |
| 839 | conn->in.copied += size; |
| 840 | conn->data_copied += size; |
| 841 | ctask->sent += size; |
| 842 | ctask->data_count -= size; |
| 843 | |
| 844 | BUG_ON(conn->in.copy < 0); |
| 845 | BUG_ON(ctask->data_count < 0); |
| 846 | |
| 847 | if (buf_size != (conn->data_copied + offset)) { |
| 848 | if (!ctask->data_count) { |
| 849 | BUG_ON(buf_size - conn->data_copied < 0); |
| 850 | /* done with this PDU */ |
| 851 | return buf_size - conn->data_copied; |
| 852 | } |
| 853 | return -EAGAIN; |
| 854 | } |
| 855 | |
| 856 | /* done with this buffer or with both - PDU and buffer */ |
| 857 | conn->data_copied = 0; |
| 858 | return 0; |
| 859 | } |
| 860 | |
| 861 | /** |
| 862 | * iscsi_tcp_copy - copy skb bits to the destanation buffer |
| 863 | * @conn: iscsi connection |
| 864 | * @buf: buffer to copy to |
| 865 | * @buf_size: number of bytes to copy |
| 866 | * |
| 867 | * Notes: |
| 868 | * The function calls skb_copy_bits() and updates per-connection |
| 869 | * byte counters. |
| 870 | **/ |
| 871 | static inline int |
| 872 | iscsi_tcp_copy(struct iscsi_conn *conn, void *buf, int buf_size) |
| 873 | { |
| 874 | int buf_left = buf_size - conn->data_copied; |
| 875 | int size = min(conn->in.copy, buf_left); |
| 876 | int rc; |
| 877 | |
| 878 | debug_tcp("tcp_copy %d bytes at offset %d copied %d\n", |
| 879 | size, conn->in.offset, conn->data_copied); |
| 880 | BUG_ON(size <= 0); |
| 881 | |
| 882 | rc = skb_copy_bits(conn->in.skb, conn->in.offset, |
| 883 | (char*)buf + conn->data_copied, size); |
| 884 | BUG_ON(rc); |
| 885 | |
| 886 | conn->in.offset += size; |
| 887 | conn->in.copy -= size; |
| 888 | conn->in.copied += size; |
| 889 | conn->data_copied += size; |
| 890 | |
| 891 | if (buf_size != conn->data_copied) |
| 892 | return -EAGAIN; |
| 893 | |
| 894 | return 0; |
| 895 | } |
| 896 | |
| 897 | static inline void |
| 898 | partial_sg_digest_update(struct iscsi_conn *conn, struct scatterlist *sg, |
| 899 | int offset, int length) |
| 900 | { |
| 901 | struct scatterlist temp; |
| 902 | |
| 903 | memcpy(&temp, sg, sizeof(struct scatterlist)); |
| 904 | temp.offset = offset; |
| 905 | temp.length = length; |
| 906 | crypto_digest_update(conn->data_rx_tfm, &temp, 1); |
| 907 | } |
| 908 | |
| 909 | static int iscsi_scsi_data_in(struct iscsi_conn *conn) |
| 910 | { |
| 911 | struct iscsi_cmd_task *ctask = conn->in.ctask; |
| 912 | struct scsi_cmnd *sc = ctask->sc; |
| 913 | struct scatterlist tmp, *sg; |
| 914 | int i, offset, rc = 0; |
| 915 | |
| 916 | BUG_ON((void*)ctask != sc->SCp.ptr); |
| 917 | |
| 918 | /* |
| 919 | * copying Data-In into the Scsi_Cmnd |
| 920 | */ |
| 921 | if (!sc->use_sg) { |
| 922 | i = ctask->data_count; |
| 923 | rc = iscsi_ctask_copy(conn, ctask, sc->request_buffer, |
| 924 | sc->request_bufflen, ctask->data_offset); |
| 925 | if (rc == -EAGAIN) |
| 926 | return rc; |
| 927 | if (conn->datadgst_en) { |
| 928 | sg_init_one(&tmp, sc->request_buffer, i); |
| 929 | crypto_digest_update(conn->data_rx_tfm, &tmp, 1); |
| 930 | } |
| 931 | rc = 0; |
| 932 | goto done; |
| 933 | } |
| 934 | |
| 935 | offset = ctask->data_offset; |
| 936 | sg = sc->request_buffer; |
| 937 | |
| 938 | if (ctask->data_offset) |
| 939 | for (i = 0; i < ctask->sg_count; i++) |
| 940 | offset -= sg[i].length; |
| 941 | /* we've passed through partial sg*/ |
| 942 | if (offset < 0) |
| 943 | offset = 0; |
| 944 | |
| 945 | for (i = ctask->sg_count; i < sc->use_sg; i++) { |
| 946 | char *dest; |
| 947 | |
| 948 | dest = kmap_atomic(sg[i].page, KM_SOFTIRQ0); |
| 949 | rc = iscsi_ctask_copy(conn, ctask, dest + sg[i].offset, |
| 950 | sg[i].length, offset); |
| 951 | kunmap_atomic(dest, KM_SOFTIRQ0); |
| 952 | if (rc == -EAGAIN) |
| 953 | /* continue with the next SKB/PDU */ |
| 954 | return rc; |
| 955 | if (!rc) { |
| 956 | if (conn->datadgst_en) { |
| 957 | if (!offset) |
| 958 | crypto_digest_update(conn->data_rx_tfm, |
| 959 | &sg[i], 1); |
| 960 | else |
| 961 | partial_sg_digest_update(conn, &sg[i], |
| 962 | sg[i].offset + offset, |
| 963 | sg[i].length - offset); |
| 964 | } |
| 965 | offset = 0; |
| 966 | ctask->sg_count++; |
| 967 | } |
| 968 | |
| 969 | if (!ctask->data_count) { |
| 970 | if (rc && conn->datadgst_en) |
| 971 | /* |
| 972 | * data-in is complete, but buffer not... |
| 973 | */ |
| 974 | partial_sg_digest_update(conn, &sg[i], |
| 975 | sg[i].offset, sg[i].length-rc); |
| 976 | rc = 0; |
| 977 | break; |
| 978 | } |
| 979 | |
| 980 | if (!conn->in.copy) |
| 981 | return -EAGAIN; |
| 982 | } |
| 983 | BUG_ON(ctask->data_count); |
| 984 | |
| 985 | done: |
| 986 | /* check for non-exceptional status */ |
| 987 | if (conn->in.flags & ISCSI_FLAG_DATA_STATUS) { |
| 988 | debug_scsi("done [sc %lx res %d itt 0x%x]\n", |
| 989 | (long)sc, sc->result, ctask->itt); |
| 990 | conn->scsirsp_pdus_cnt++; |
| 991 | iscsi_ctask_cleanup(conn, ctask); |
| 992 | sc->scsi_done(sc); |
| 993 | } |
| 994 | |
| 995 | return rc; |
| 996 | } |
| 997 | |
| 998 | static int |
| 999 | iscsi_data_recv(struct iscsi_conn *conn) |
| 1000 | { |
| 1001 | struct iscsi_session *session = conn->session; |
| 1002 | int rc = 0; |
| 1003 | |
| 1004 | switch(conn->in.opcode) { |
| 1005 | case ISCSI_OP_SCSI_DATA_IN: |
| 1006 | rc = iscsi_scsi_data_in(conn); |
| 1007 | break; |
| 1008 | case ISCSI_OP_SCSI_CMD_RSP: { |
| 1009 | /* |
| 1010 | * SCSI Sense Data: |
| 1011 | * copying the entire Data Segment. |
| 1012 | */ |
| 1013 | if (iscsi_tcp_copy(conn, conn->data, conn->in.datalen)) { |
| 1014 | rc = -EAGAIN; |
| 1015 | goto exit; |
| 1016 | } |
| 1017 | |
| 1018 | /* |
| 1019 | * check for sense |
| 1020 | */ |
| 1021 | conn->in.hdr = &conn->hdr; |
| 1022 | conn->senselen = (conn->data[0] << 8) | conn->data[1]; |
| 1023 | rc = iscsi_cmd_rsp(conn, conn->in.ctask); |
| 1024 | } |
| 1025 | break; |
| 1026 | case ISCSI_OP_TEXT_RSP: |
| 1027 | case ISCSI_OP_LOGIN_RSP: |
| 1028 | case ISCSI_OP_NOOP_IN: { |
| 1029 | struct iscsi_mgmt_task *mtask = NULL; |
| 1030 | |
| 1031 | if (conn->in.itt != ISCSI_RESERVED_TAG) |
| 1032 | mtask = (struct iscsi_mgmt_task *) |
| 1033 | session->mgmt_cmds[conn->in.itt - |
| 1034 | ISCSI_MGMT_ITT_OFFSET]; |
| 1035 | |
| 1036 | /* |
| 1037 | * Collect data segment to the connection's data |
| 1038 | * placeholder |
| 1039 | */ |
| 1040 | if (iscsi_tcp_copy(conn, conn->data, conn->in.datalen)) { |
| 1041 | rc = -EAGAIN; |
| 1042 | goto exit; |
| 1043 | } |
| 1044 | |
| 1045 | rc = iscsi_recv_pdu(iscsi_handle(conn), conn->in.hdr, |
| 1046 | conn->data, conn->in.datalen); |
| 1047 | |
| 1048 | if (mtask && conn->login_mtask != mtask) { |
| 1049 | spin_lock(&session->lock); |
| 1050 | __kfifo_put(session->mgmtpool.queue, (void*)&mtask, |
| 1051 | sizeof(void*)); |
| 1052 | spin_unlock(&session->lock); |
| 1053 | } |
| 1054 | } |
| 1055 | break; |
| 1056 | default: |
| 1057 | BUG_ON(1); |
| 1058 | } |
| 1059 | exit: |
| 1060 | return rc; |
| 1061 | } |
| 1062 | |
| 1063 | /** |
| 1064 | * iscsi_tcp_data_recv - TCP receive in sendfile fashion |
| 1065 | * @rd_desc: read descriptor |
| 1066 | * @skb: socket buffer |
| 1067 | * @offset: offset in skb |
| 1068 | * @len: skb->len - offset |
| 1069 | **/ |
| 1070 | static int |
| 1071 | iscsi_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, |
| 1072 | unsigned int offset, size_t len) |
| 1073 | { |
| 1074 | int rc; |
| 1075 | struct iscsi_conn *conn = rd_desc->arg.data; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1076 | int processed; |
| 1077 | char pad[ISCSI_PAD_LEN]; |
| 1078 | struct scatterlist sg; |
| 1079 | |
| 1080 | /* |
| 1081 | * Save current SKB and its offset in the corresponding |
| 1082 | * connection context. |
| 1083 | */ |
Mike Christie | bb05234 | 2005-09-12 21:01:35 -0500 | [diff] [blame] | 1084 | conn->in.copy = skb->len - offset; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1085 | conn->in.offset = offset; |
| 1086 | conn->in.skb = skb; |
| 1087 | conn->in.len = conn->in.copy; |
| 1088 | BUG_ON(conn->in.copy <= 0); |
| 1089 | debug_tcp("in %d bytes\n", conn->in.copy); |
| 1090 | |
| 1091 | more: |
| 1092 | conn->in.copied = 0; |
| 1093 | rc = 0; |
| 1094 | |
| 1095 | if (unlikely(conn->suspend_rx)) { |
| 1096 | debug_tcp("conn %d Rx suspended!\n", conn->id); |
| 1097 | return 0; |
| 1098 | } |
| 1099 | |
| 1100 | if (conn->in_progress == IN_PROGRESS_WAIT_HEADER || |
| 1101 | conn->in_progress == IN_PROGRESS_HEADER_GATHER) { |
| 1102 | rc = iscsi_hdr_extract(conn); |
| 1103 | if (rc) { |
| 1104 | if (rc == -EAGAIN) |
| 1105 | goto nomore; |
| 1106 | else { |
| 1107 | iscsi_conn_failure(conn, rc); |
| 1108 | return 0; |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | /* |
| 1113 | * Verify and process incoming PDU header. |
| 1114 | */ |
| 1115 | rc = iscsi_hdr_recv(conn); |
| 1116 | if (!rc && conn->in.datalen) { |
| 1117 | if (conn->datadgst_en && |
| 1118 | conn->in.opcode == ISCSI_OP_SCSI_DATA_IN) { |
| 1119 | BUG_ON(!conn->data_rx_tfm); |
| 1120 | crypto_digest_init(conn->data_rx_tfm); |
| 1121 | } |
| 1122 | conn->in_progress = IN_PROGRESS_DATA_RECV; |
| 1123 | } else if (rc) { |
| 1124 | iscsi_conn_failure(conn, rc); |
| 1125 | return 0; |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | if (conn->in_progress == IN_PROGRESS_DDIGEST_RECV) { |
| 1130 | debug_tcp("extra data_recv offset %d copy %d\n", |
| 1131 | conn->in.offset, conn->in.copy); |
| 1132 | if (conn->in.opcode == ISCSI_OP_SCSI_DATA_IN) { |
| 1133 | uint32_t recv_digest; |
| 1134 | skb_copy_bits(conn->in.skb, conn->in.offset, |
| 1135 | &recv_digest, 4); |
| 1136 | conn->in.offset += 4; |
| 1137 | conn->in.copy -= 4; |
| 1138 | if (recv_digest != conn->in.datadgst) { |
| 1139 | debug_tcp("iscsi_tcp: data digest error!" |
| 1140 | "0x%x != 0x%x\n", recv_digest, |
| 1141 | conn->in.datadgst); |
| 1142 | iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST); |
| 1143 | return 0; |
| 1144 | } else { |
| 1145 | debug_tcp("iscsi_tcp: data digest match!" |
| 1146 | "0x%x == 0x%x\n", recv_digest, |
| 1147 | conn->in.datadgst); |
| 1148 | conn->in_progress = IN_PROGRESS_WAIT_HEADER; |
| 1149 | } |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | if (conn->in_progress == IN_PROGRESS_DATA_RECV && conn->in.copy) { |
| 1154 | |
| 1155 | debug_tcp("data_recv offset %d copy %d\n", |
| 1156 | conn->in.offset, conn->in.copy); |
| 1157 | |
| 1158 | rc = iscsi_data_recv(conn); |
| 1159 | if (rc) { |
| 1160 | if (rc == -EAGAIN) { |
| 1161 | rd_desc->count = conn->in.datalen - |
| 1162 | conn->in.ctask->data_count; |
| 1163 | goto again; |
| 1164 | } |
| 1165 | iscsi_conn_failure(conn, rc); |
| 1166 | return 0; |
| 1167 | } |
| 1168 | conn->in.copy -= conn->in.padding; |
| 1169 | conn->in.offset += conn->in.padding; |
| 1170 | if (conn->datadgst_en && |
| 1171 | conn->in.opcode == ISCSI_OP_SCSI_DATA_IN) { |
| 1172 | if (conn->in.padding) { |
| 1173 | debug_tcp("padding -> %d\n", conn->in.padding); |
| 1174 | memset(pad, 0, conn->in.padding); |
| 1175 | sg_init_one(&sg, pad, conn->in.padding); |
| 1176 | crypto_digest_update(conn->data_rx_tfm, &sg, 1); |
| 1177 | } |
| 1178 | crypto_digest_final(conn->data_rx_tfm, |
| 1179 | (u8 *) & conn->in.datadgst); |
| 1180 | debug_tcp("rx digest 0x%x\n", conn->in.datadgst); |
| 1181 | conn->in_progress = IN_PROGRESS_DDIGEST_RECV; |
| 1182 | } else |
| 1183 | conn->in_progress = IN_PROGRESS_WAIT_HEADER; |
| 1184 | } |
| 1185 | |
| 1186 | debug_tcp("f, processed %d from out of %d padding %d\n", |
| 1187 | conn->in.offset - offset, (int)len, conn->in.padding); |
| 1188 | BUG_ON(conn->in.offset - offset > len); |
| 1189 | |
| 1190 | if (conn->in.offset - offset != len) { |
| 1191 | debug_tcp("continue to process %d bytes\n", |
| 1192 | (int)len - (conn->in.offset - offset)); |
| 1193 | goto more; |
| 1194 | } |
| 1195 | |
| 1196 | nomore: |
| 1197 | processed = conn->in.offset - offset; |
| 1198 | BUG_ON(processed == 0); |
| 1199 | return processed; |
| 1200 | |
| 1201 | again: |
| 1202 | processed = conn->in.offset - offset; |
| 1203 | debug_tcp("c, processed %d from out of %d rd_desc_cnt %d\n", |
| 1204 | processed, (int)len, (int)rd_desc->count); |
| 1205 | BUG_ON(processed == 0); |
| 1206 | BUG_ON(processed > len); |
| 1207 | |
| 1208 | conn->rxdata_octets += processed; |
| 1209 | return processed; |
| 1210 | } |
| 1211 | |
| 1212 | static void |
| 1213 | iscsi_tcp_data_ready(struct sock *sk, int flag) |
| 1214 | { |
| 1215 | struct iscsi_conn *conn = sk->sk_user_data; |
| 1216 | read_descriptor_t rd_desc; |
| 1217 | |
| 1218 | read_lock(&sk->sk_callback_lock); |
| 1219 | |
| 1220 | /* use rd_desc to pass 'conn' to iscsi_tcp_data_recv */ |
| 1221 | rd_desc.arg.data = conn; |
| 1222 | rd_desc.count = 0; |
| 1223 | tcp_read_sock(sk, &rd_desc, iscsi_tcp_data_recv); |
| 1224 | |
| 1225 | read_unlock(&sk->sk_callback_lock); |
| 1226 | } |
| 1227 | |
| 1228 | static void |
| 1229 | iscsi_tcp_state_change(struct sock *sk) |
| 1230 | { |
| 1231 | struct iscsi_conn *conn; |
| 1232 | struct iscsi_session *session; |
| 1233 | void (*old_state_change)(struct sock *); |
| 1234 | |
| 1235 | read_lock(&sk->sk_callback_lock); |
| 1236 | |
| 1237 | conn = (struct iscsi_conn*)sk->sk_user_data; |
| 1238 | session = conn->session; |
| 1239 | |
Mike Christie | e627399 | 2005-11-29 23:12:49 -0600 | [diff] [blame^] | 1240 | if ((sk->sk_state == TCP_CLOSE_WAIT || |
| 1241 | sk->sk_state == TCP_CLOSE) && |
| 1242 | !atomic_read(&sk->sk_rmem_alloc)) { |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1243 | debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n"); |
| 1244 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); |
| 1245 | } |
| 1246 | |
| 1247 | old_state_change = conn->old_state_change; |
| 1248 | |
| 1249 | read_unlock(&sk->sk_callback_lock); |
| 1250 | |
| 1251 | old_state_change(sk); |
| 1252 | } |
| 1253 | |
| 1254 | /** |
| 1255 | * iscsi_write_space - Called when more output buffer space is available |
| 1256 | * @sk: socket space is available for |
| 1257 | **/ |
| 1258 | static void |
| 1259 | iscsi_write_space(struct sock *sk) |
| 1260 | { |
| 1261 | struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data; |
| 1262 | conn->old_write_space(sk); |
| 1263 | debug_tcp("iscsi_write_space: cid %d\n", conn->id); |
| 1264 | clear_bit(SUSPEND_BIT, &conn->suspend_tx); |
| 1265 | schedule_work(&conn->xmitwork); |
| 1266 | } |
| 1267 | |
| 1268 | static void |
| 1269 | iscsi_conn_set_callbacks(struct iscsi_conn *conn) |
| 1270 | { |
| 1271 | struct sock *sk = conn->sock->sk; |
| 1272 | |
| 1273 | /* assign new callbacks */ |
| 1274 | write_lock_bh(&sk->sk_callback_lock); |
| 1275 | sk->sk_user_data = conn; |
| 1276 | conn->old_data_ready = sk->sk_data_ready; |
| 1277 | conn->old_state_change = sk->sk_state_change; |
| 1278 | conn->old_write_space = sk->sk_write_space; |
| 1279 | sk->sk_data_ready = iscsi_tcp_data_ready; |
| 1280 | sk->sk_state_change = iscsi_tcp_state_change; |
| 1281 | sk->sk_write_space = iscsi_write_space; |
| 1282 | write_unlock_bh(&sk->sk_callback_lock); |
| 1283 | } |
| 1284 | |
| 1285 | static void |
| 1286 | iscsi_conn_restore_callbacks(struct iscsi_conn *conn) |
| 1287 | { |
| 1288 | struct sock *sk = conn->sock->sk; |
| 1289 | |
| 1290 | /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */ |
| 1291 | write_lock_bh(&sk->sk_callback_lock); |
| 1292 | sk->sk_user_data = NULL; |
| 1293 | sk->sk_data_ready = conn->old_data_ready; |
| 1294 | sk->sk_state_change = conn->old_state_change; |
| 1295 | sk->sk_write_space = conn->old_write_space; |
| 1296 | sk->sk_no_check = 0; |
| 1297 | write_unlock_bh(&sk->sk_callback_lock); |
| 1298 | } |
| 1299 | |
| 1300 | /** |
| 1301 | * iscsi_send - generic send routine |
| 1302 | * @sk: kernel's socket |
| 1303 | * @buf: buffer to write from |
| 1304 | * @size: actual size to write |
| 1305 | * @flags: socket's flags |
| 1306 | * |
| 1307 | * Notes: |
| 1308 | * depending on buffer will use tcp_sendpage() or tcp_sendmsg(). |
| 1309 | * buf->sg.offset == -1 tells us that buffer is non S/G and forces |
| 1310 | * to use tcp_sendmsg(). |
| 1311 | */ |
| 1312 | static inline int |
| 1313 | iscsi_send(struct socket *sk, struct iscsi_buf *buf, int size, int flags) |
| 1314 | { |
| 1315 | int res; |
| 1316 | |
| 1317 | if ((int)buf->sg.offset >= 0) { |
| 1318 | int offset = buf->sg.offset + buf->sent; |
| 1319 | |
| 1320 | /* tcp_sendpage */ |
| 1321 | res = sk->ops->sendpage(sk, buf->sg.page, offset, size, flags); |
| 1322 | } else { |
| 1323 | struct msghdr msg; |
| 1324 | |
| 1325 | buf->iov.iov_base = iscsi_buf_iov_base(buf); |
| 1326 | buf->iov.iov_len = size; |
| 1327 | |
| 1328 | memset(&msg, 0, sizeof(struct msghdr)); |
| 1329 | |
| 1330 | /* tcp_sendmsg */ |
| 1331 | res = kernel_sendmsg(sk, &msg, &buf->iov, 1, size); |
| 1332 | } |
| 1333 | |
| 1334 | return res; |
| 1335 | } |
| 1336 | |
| 1337 | /** |
| 1338 | * iscsi_sendhdr - send PDU Header via tcp_sendpage() |
| 1339 | * @conn: iscsi connection |
| 1340 | * @buf: buffer to write from |
| 1341 | * @datalen: lenght of data to be sent after the header |
| 1342 | * |
| 1343 | * Notes: |
| 1344 | * (Tx, Fast Path) |
| 1345 | **/ |
| 1346 | static inline int |
| 1347 | iscsi_sendhdr(struct iscsi_conn *conn, struct iscsi_buf *buf, int datalen) |
| 1348 | { |
| 1349 | struct socket *sk = conn->sock; |
| 1350 | int flags = 0; /* MSG_DONTWAIT; */ |
| 1351 | int res, size; |
| 1352 | |
| 1353 | size = buf->sg.length - buf->sent; |
| 1354 | BUG_ON(buf->sent + size > buf->sg.length); |
| 1355 | if (buf->sent + size != buf->sg.length || datalen) |
| 1356 | flags |= MSG_MORE; |
| 1357 | |
| 1358 | res = iscsi_send(sk, buf, size, flags); |
| 1359 | debug_tcp("sendhdr %d bytes, sent %d res %d\n", size, buf->sent, res); |
| 1360 | if (res >= 0) { |
| 1361 | conn->txdata_octets += res; |
| 1362 | buf->sent += res; |
| 1363 | if (size != res) |
| 1364 | return -EAGAIN; |
| 1365 | return 0; |
| 1366 | } else if (res == -EAGAIN) { |
| 1367 | conn->sendpage_failures_cnt++; |
| 1368 | set_bit(SUSPEND_BIT, &conn->suspend_tx); |
| 1369 | } else if (res == -EPIPE) |
| 1370 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); |
| 1371 | |
| 1372 | return res; |
| 1373 | } |
| 1374 | |
| 1375 | /** |
| 1376 | * iscsi_sendpage - send one page of iSCSI Data-Out. |
| 1377 | * @conn: iscsi connection |
| 1378 | * @buf: buffer to write from |
| 1379 | * @count: remaining data |
| 1380 | * @sent: number of bytes sent |
| 1381 | * |
| 1382 | * Notes: |
| 1383 | * (Tx, Fast Path) |
| 1384 | **/ |
| 1385 | static inline int |
| 1386 | iscsi_sendpage(struct iscsi_conn *conn, struct iscsi_buf *buf, |
| 1387 | int *count, int *sent) |
| 1388 | { |
| 1389 | struct socket *sk = conn->sock; |
| 1390 | int flags = 0; /* MSG_DONTWAIT; */ |
| 1391 | int res, size; |
| 1392 | |
| 1393 | size = buf->sg.length - buf->sent; |
| 1394 | BUG_ON(buf->sent + size > buf->sg.length); |
| 1395 | if (size > *count) |
| 1396 | size = *count; |
Mike Christie | b13941f | 2005-09-12 21:01:28 -0500 | [diff] [blame] | 1397 | if (buf->sent + size != buf->sg.length || *count != size) |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1398 | flags |= MSG_MORE; |
| 1399 | |
| 1400 | res = iscsi_send(sk, buf, size, flags); |
| 1401 | debug_tcp("sendpage: %d bytes, sent %d left %d sent %d res %d\n", |
| 1402 | size, buf->sent, *count, *sent, res); |
| 1403 | if (res >= 0) { |
| 1404 | conn->txdata_octets += res; |
| 1405 | buf->sent += res; |
| 1406 | *count -= res; |
| 1407 | *sent += res; |
| 1408 | if (size != res) |
| 1409 | return -EAGAIN; |
| 1410 | return 0; |
| 1411 | } else if (res == -EAGAIN) { |
| 1412 | conn->sendpage_failures_cnt++; |
| 1413 | set_bit(SUSPEND_BIT, &conn->suspend_tx); |
| 1414 | } else if (res == -EPIPE) |
| 1415 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); |
| 1416 | |
| 1417 | return res; |
| 1418 | } |
| 1419 | |
| 1420 | static inline void |
| 1421 | iscsi_data_digest_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) |
| 1422 | { |
| 1423 | BUG_ON(!conn->data_tx_tfm); |
| 1424 | crypto_digest_init(conn->data_tx_tfm); |
| 1425 | ctask->digest_count = 4; |
| 1426 | } |
| 1427 | |
| 1428 | static inline void |
| 1429 | iscsi_buf_data_digest_update(struct iscsi_conn *conn, struct iscsi_buf *buf) |
| 1430 | { |
| 1431 | struct scatterlist sg; |
| 1432 | |
| 1433 | if (buf->sg.offset != -1) |
| 1434 | crypto_digest_update(conn->data_tx_tfm, &buf->sg, 1); |
| 1435 | else { |
| 1436 | sg_init_one(&sg, (char *)buf->sg.page, buf->sg.length); |
| 1437 | crypto_digest_update(conn->data_tx_tfm, &sg, 1); |
| 1438 | } |
| 1439 | } |
| 1440 | |
| 1441 | static inline int |
| 1442 | iscsi_digest_final_send(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask, |
| 1443 | struct iscsi_buf *buf, uint32_t *digest, int final) |
| 1444 | { |
| 1445 | int rc = 0; |
| 1446 | int sent = 0; |
| 1447 | |
| 1448 | if (final) |
| 1449 | crypto_digest_final(conn->data_tx_tfm, (u8*)digest); |
| 1450 | |
| 1451 | iscsi_buf_init_virt(buf, (char*)digest, 4); |
| 1452 | rc = iscsi_sendpage(conn, buf, &ctask->digest_count, &sent); |
| 1453 | if (rc) { |
| 1454 | ctask->datadigest = *digest; |
| 1455 | ctask->xmstate |= XMSTATE_DATA_DIGEST; |
| 1456 | } else |
| 1457 | ctask->digest_count = 4; |
| 1458 | return rc; |
| 1459 | } |
| 1460 | |
| 1461 | /** |
| 1462 | * iscsi_solicit_data_cont - initialize next Data-Out |
| 1463 | * @conn: iscsi connection |
| 1464 | * @ctask: scsi command task |
| 1465 | * @r2t: R2T info |
| 1466 | * @left: bytes left to transfer |
| 1467 | * |
| 1468 | * Notes: |
| 1469 | * Initialize next Data-Out within this R2T sequence and continue |
| 1470 | * to process next Scatter-Gather element(if any) of this SCSI command. |
| 1471 | * |
| 1472 | * Called under connection lock. |
| 1473 | **/ |
| 1474 | static void |
| 1475 | iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask, |
| 1476 | struct iscsi_r2t_info *r2t, int left) |
| 1477 | { |
| 1478 | struct iscsi_data *hdr; |
| 1479 | struct iscsi_data_task *dtask; |
| 1480 | struct scsi_cmnd *sc = ctask->sc; |
| 1481 | int new_offset; |
| 1482 | |
| 1483 | dtask = mempool_alloc(ctask->datapool, GFP_ATOMIC); |
| 1484 | BUG_ON(!dtask); |
| 1485 | hdr = &dtask->hdr; |
| 1486 | memset(hdr, 0, sizeof(struct iscsi_data)); |
| 1487 | hdr->ttt = r2t->ttt; |
| 1488 | hdr->datasn = cpu_to_be32(r2t->solicit_datasn); |
| 1489 | r2t->solicit_datasn++; |
| 1490 | hdr->opcode = ISCSI_OP_SCSI_DATA_OUT; |
| 1491 | memcpy(hdr->lun, ctask->hdr.lun, sizeof(hdr->lun)); |
| 1492 | hdr->itt = ctask->hdr.itt; |
| 1493 | hdr->exp_statsn = r2t->exp_statsn; |
| 1494 | new_offset = r2t->data_offset + r2t->sent; |
| 1495 | hdr->offset = cpu_to_be32(new_offset); |
| 1496 | if (left > conn->max_xmit_dlength) { |
| 1497 | hton24(hdr->dlength, conn->max_xmit_dlength); |
| 1498 | r2t->data_count = conn->max_xmit_dlength; |
| 1499 | } else { |
| 1500 | hton24(hdr->dlength, left); |
| 1501 | r2t->data_count = left; |
| 1502 | hdr->flags = ISCSI_FLAG_CMD_FINAL; |
| 1503 | } |
| 1504 | conn->dataout_pdus_cnt++; |
| 1505 | |
Mike Christie | af97348 | 2005-09-12 21:01:32 -0500 | [diff] [blame] | 1506 | iscsi_buf_init_virt(&r2t->headbuf, (char*)hdr, |
| 1507 | sizeof(struct iscsi_hdr)); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1508 | |
| 1509 | r2t->dtask = dtask; |
| 1510 | |
| 1511 | if (sc->use_sg && !iscsi_buf_left(&r2t->sendbuf)) { |
| 1512 | BUG_ON(ctask->bad_sg == r2t->sg); |
| 1513 | iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg); |
| 1514 | r2t->sg += 1; |
| 1515 | } else |
| 1516 | iscsi_buf_init_iov(&ctask->sendbuf, |
| 1517 | (char*)sc->request_buffer + new_offset, |
| 1518 | r2t->data_count); |
| 1519 | |
| 1520 | list_add(&dtask->item, &ctask->dataqueue); |
| 1521 | } |
| 1522 | |
| 1523 | static void |
| 1524 | iscsi_unsolicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) |
| 1525 | { |
| 1526 | struct iscsi_data *hdr; |
| 1527 | struct iscsi_data_task *dtask; |
| 1528 | |
| 1529 | dtask = mempool_alloc(ctask->datapool, GFP_ATOMIC); |
| 1530 | BUG_ON(!dtask); |
| 1531 | hdr = &dtask->hdr; |
| 1532 | memset(hdr, 0, sizeof(struct iscsi_data)); |
| 1533 | hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG); |
| 1534 | hdr->datasn = cpu_to_be32(ctask->unsol_datasn); |
| 1535 | ctask->unsol_datasn++; |
| 1536 | hdr->opcode = ISCSI_OP_SCSI_DATA_OUT; |
| 1537 | memcpy(hdr->lun, ctask->hdr.lun, sizeof(hdr->lun)); |
| 1538 | hdr->itt = ctask->hdr.itt; |
| 1539 | hdr->exp_statsn = cpu_to_be32(conn->exp_statsn); |
| 1540 | hdr->offset = cpu_to_be32(ctask->total_length - |
| 1541 | ctask->r2t_data_count - |
| 1542 | ctask->unsol_count); |
| 1543 | if (ctask->unsol_count > conn->max_xmit_dlength) { |
| 1544 | hton24(hdr->dlength, conn->max_xmit_dlength); |
| 1545 | ctask->data_count = conn->max_xmit_dlength; |
| 1546 | hdr->flags = 0; |
| 1547 | } else { |
| 1548 | hton24(hdr->dlength, ctask->unsol_count); |
| 1549 | ctask->data_count = ctask->unsol_count; |
| 1550 | hdr->flags = ISCSI_FLAG_CMD_FINAL; |
| 1551 | } |
| 1552 | |
Mike Christie | af97348 | 2005-09-12 21:01:32 -0500 | [diff] [blame] | 1553 | iscsi_buf_init_virt(&ctask->headbuf, (char*)hdr, |
| 1554 | sizeof(struct iscsi_hdr)); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1555 | |
| 1556 | list_add(&dtask->item, &ctask->dataqueue); |
| 1557 | |
| 1558 | ctask->dtask = dtask; |
| 1559 | } |
| 1560 | |
| 1561 | /** |
| 1562 | * iscsi_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands |
| 1563 | * @conn: iscsi connection |
| 1564 | * @ctask: scsi command task |
| 1565 | * @sc: scsi command |
| 1566 | **/ |
| 1567 | static void |
| 1568 | iscsi_cmd_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask, |
| 1569 | struct scsi_cmnd *sc) |
| 1570 | { |
| 1571 | struct iscsi_session *session = conn->session; |
| 1572 | |
| 1573 | BUG_ON(__kfifo_len(ctask->r2tqueue)); |
| 1574 | |
| 1575 | ctask->sc = sc; |
| 1576 | ctask->conn = conn; |
| 1577 | ctask->hdr.opcode = ISCSI_OP_SCSI_CMD; |
| 1578 | ctask->hdr.flags = ISCSI_ATTR_SIMPLE; |
| 1579 | int_to_scsilun(sc->device->lun, (struct scsi_lun *)ctask->hdr.lun); |
| 1580 | ctask->hdr.itt = ctask->itt | (conn->id << CID_SHIFT) | |
| 1581 | (session->age << AGE_SHIFT); |
| 1582 | ctask->hdr.data_length = cpu_to_be32(sc->request_bufflen); |
| 1583 | ctask->hdr.cmdsn = cpu_to_be32(session->cmdsn); session->cmdsn++; |
| 1584 | ctask->hdr.exp_statsn = cpu_to_be32(conn->exp_statsn); |
| 1585 | memcpy(ctask->hdr.cdb, sc->cmnd, sc->cmd_len); |
| 1586 | memset(&ctask->hdr.cdb[sc->cmd_len], 0, MAX_COMMAND_SIZE - sc->cmd_len); |
| 1587 | |
| 1588 | ctask->mtask = NULL; |
| 1589 | ctask->sent = 0; |
| 1590 | ctask->sg_count = 0; |
| 1591 | |
| 1592 | ctask->total_length = sc->request_bufflen; |
| 1593 | |
| 1594 | if (sc->sc_data_direction == DMA_TO_DEVICE) { |
| 1595 | ctask->exp_r2tsn = 0; |
| 1596 | ctask->hdr.flags |= ISCSI_FLAG_CMD_WRITE; |
| 1597 | BUG_ON(ctask->total_length == 0); |
| 1598 | if (sc->use_sg) { |
| 1599 | struct scatterlist *sg = sc->request_buffer; |
| 1600 | |
| 1601 | iscsi_buf_init_sg(&ctask->sendbuf, |
| 1602 | &sg[ctask->sg_count++]); |
| 1603 | ctask->sg = sg; |
| 1604 | ctask->bad_sg = sg + sc->use_sg; |
| 1605 | } else { |
| 1606 | iscsi_buf_init_iov(&ctask->sendbuf, sc->request_buffer, |
| 1607 | sc->request_bufflen); |
| 1608 | } |
| 1609 | |
| 1610 | /* |
| 1611 | * Write counters: |
| 1612 | * |
| 1613 | * imm_count bytes to be sent right after |
| 1614 | * SCSI PDU Header |
| 1615 | * |
| 1616 | * unsol_count bytes(as Data-Out) to be sent |
| 1617 | * without R2T ack right after |
| 1618 | * immediate data |
| 1619 | * |
| 1620 | * r2t_data_count bytes to be sent via R2T ack's |
| 1621 | * |
| 1622 | * pad_count bytes to be sent as zero-padding |
| 1623 | */ |
| 1624 | ctask->imm_count = 0; |
| 1625 | ctask->unsol_count = 0; |
| 1626 | ctask->unsol_datasn = 0; |
| 1627 | ctask->xmstate = XMSTATE_W_HDR; |
| 1628 | /* calculate write padding */ |
| 1629 | ctask->pad_count = ctask->total_length & (ISCSI_PAD_LEN-1); |
| 1630 | if (ctask->pad_count) { |
| 1631 | ctask->pad_count = ISCSI_PAD_LEN - ctask->pad_count; |
| 1632 | debug_scsi("write padding %d bytes\n", |
| 1633 | ctask->pad_count); |
| 1634 | ctask->xmstate |= XMSTATE_W_PAD; |
| 1635 | } |
| 1636 | if (session->imm_data_en) { |
| 1637 | if (ctask->total_length >= session->first_burst) |
| 1638 | ctask->imm_count = min(session->first_burst, |
| 1639 | conn->max_xmit_dlength); |
| 1640 | else |
| 1641 | ctask->imm_count = min(ctask->total_length, |
| 1642 | conn->max_xmit_dlength); |
| 1643 | hton24(ctask->hdr.dlength, ctask->imm_count); |
| 1644 | ctask->xmstate |= XMSTATE_IMM_DATA; |
| 1645 | } else |
| 1646 | zero_data(ctask->hdr.dlength); |
| 1647 | |
| 1648 | if (!session->initial_r2t_en) |
| 1649 | ctask->unsol_count = min(session->first_burst, |
| 1650 | ctask->total_length) - ctask->imm_count; |
| 1651 | if (!ctask->unsol_count) |
| 1652 | /* No unsolicit Data-Out's */ |
| 1653 | ctask->hdr.flags |= ISCSI_FLAG_CMD_FINAL; |
| 1654 | else |
| 1655 | ctask->xmstate |= XMSTATE_UNS_HDR | XMSTATE_UNS_INIT; |
| 1656 | |
| 1657 | ctask->r2t_data_count = ctask->total_length - |
| 1658 | ctask->imm_count - |
| 1659 | ctask->unsol_count; |
| 1660 | |
| 1661 | debug_scsi("cmd [itt %x total %d imm %d imm_data %d " |
| 1662 | "r2t_data %d]\n", |
| 1663 | ctask->itt, ctask->total_length, ctask->imm_count, |
| 1664 | ctask->unsol_count, ctask->r2t_data_count); |
| 1665 | } else { |
| 1666 | ctask->hdr.flags |= ISCSI_FLAG_CMD_FINAL; |
| 1667 | if (sc->sc_data_direction == DMA_FROM_DEVICE) |
| 1668 | ctask->hdr.flags |= ISCSI_FLAG_CMD_READ; |
| 1669 | ctask->datasn = 0; |
| 1670 | ctask->xmstate = XMSTATE_R_HDR; |
| 1671 | zero_data(ctask->hdr.dlength); |
| 1672 | } |
| 1673 | |
Mike Christie | af97348 | 2005-09-12 21:01:32 -0500 | [diff] [blame] | 1674 | iscsi_buf_init_virt(&ctask->headbuf, (char*)&ctask->hdr, |
| 1675 | sizeof(struct iscsi_hdr)); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1676 | conn->scsicmd_pdus_cnt++; |
| 1677 | } |
| 1678 | |
| 1679 | /** |
| 1680 | * iscsi_mtask_xmit - xmit management(immediate) task |
| 1681 | * @conn: iscsi connection |
| 1682 | * @mtask: task management task |
| 1683 | * |
| 1684 | * Notes: |
| 1685 | * The function can return -EAGAIN in which case caller must |
| 1686 | * call it again later, or recover. '0' return code means successful |
| 1687 | * xmit. |
| 1688 | * |
| 1689 | * Management xmit state machine consists of two states: |
| 1690 | * IN_PROGRESS_IMM_HEAD - PDU Header xmit in progress |
| 1691 | * IN_PROGRESS_IMM_DATA - PDU Data xmit in progress |
| 1692 | **/ |
| 1693 | static int |
| 1694 | iscsi_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask) |
| 1695 | { |
| 1696 | |
| 1697 | debug_scsi("mtask deq [cid %d state %x itt 0x%x]\n", |
| 1698 | conn->id, mtask->xmstate, mtask->itt); |
| 1699 | |
| 1700 | if (mtask->xmstate & XMSTATE_IMM_HDR) { |
| 1701 | mtask->xmstate &= ~XMSTATE_IMM_HDR; |
| 1702 | if (mtask->data_count) |
| 1703 | mtask->xmstate |= XMSTATE_IMM_DATA; |
Mike Christie | af97348 | 2005-09-12 21:01:32 -0500 | [diff] [blame] | 1704 | if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE && |
| 1705 | conn->stop_stage != STOP_CONN_RECOVER && |
| 1706 | conn->hdrdgst_en) |
| 1707 | iscsi_hdr_digest(conn, &mtask->headbuf, |
| 1708 | (u8*)mtask->hdrext); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1709 | if (iscsi_sendhdr(conn, &mtask->headbuf, mtask->data_count)) { |
| 1710 | mtask->xmstate |= XMSTATE_IMM_HDR; |
| 1711 | if (mtask->data_count) |
| 1712 | mtask->xmstate &= ~XMSTATE_IMM_DATA; |
| 1713 | return -EAGAIN; |
| 1714 | } |
| 1715 | } |
| 1716 | |
| 1717 | if (mtask->xmstate & XMSTATE_IMM_DATA) { |
| 1718 | BUG_ON(!mtask->data_count); |
| 1719 | mtask->xmstate &= ~XMSTATE_IMM_DATA; |
| 1720 | /* FIXME: implement. |
| 1721 | * Virtual buffer could be spreaded across multiple pages... |
| 1722 | */ |
| 1723 | do { |
| 1724 | if (iscsi_sendpage(conn, &mtask->sendbuf, |
| 1725 | &mtask->data_count, &mtask->sent)) { |
| 1726 | mtask->xmstate |= XMSTATE_IMM_DATA; |
| 1727 | return -EAGAIN; |
| 1728 | } |
| 1729 | } while (mtask->data_count); |
| 1730 | } |
| 1731 | |
| 1732 | BUG_ON(mtask->xmstate != XMSTATE_IDLE); |
| 1733 | return 0; |
| 1734 | } |
| 1735 | |
| 1736 | static inline int |
| 1737 | handle_xmstate_r_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) |
| 1738 | { |
| 1739 | ctask->xmstate &= ~XMSTATE_R_HDR; |
Mike Christie | af97348 | 2005-09-12 21:01:32 -0500 | [diff] [blame] | 1740 | if (conn->hdrdgst_en) |
| 1741 | iscsi_hdr_digest(conn, &ctask->headbuf, (u8*)ctask->hdrext); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1742 | if (!iscsi_sendhdr(conn, &ctask->headbuf, 0)) { |
| 1743 | BUG_ON(ctask->xmstate != XMSTATE_IDLE); |
| 1744 | return 0; /* wait for Data-In */ |
| 1745 | } |
| 1746 | ctask->xmstate |= XMSTATE_R_HDR; |
| 1747 | return -EAGAIN; |
| 1748 | } |
| 1749 | |
| 1750 | static inline int |
| 1751 | handle_xmstate_w_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) |
| 1752 | { |
| 1753 | ctask->xmstate &= ~XMSTATE_W_HDR; |
Mike Christie | af97348 | 2005-09-12 21:01:32 -0500 | [diff] [blame] | 1754 | if (conn->hdrdgst_en) |
| 1755 | iscsi_hdr_digest(conn, &ctask->headbuf, (u8*)ctask->hdrext); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1756 | if (iscsi_sendhdr(conn, &ctask->headbuf, ctask->imm_count)) { |
| 1757 | ctask->xmstate |= XMSTATE_W_HDR; |
| 1758 | return -EAGAIN; |
| 1759 | } |
| 1760 | return 0; |
| 1761 | } |
| 1762 | |
| 1763 | static inline int |
| 1764 | handle_xmstate_data_digest(struct iscsi_conn *conn, |
| 1765 | struct iscsi_cmd_task *ctask) |
| 1766 | { |
| 1767 | ctask->xmstate &= ~XMSTATE_DATA_DIGEST; |
| 1768 | debug_tcp("resent data digest 0x%x\n", ctask->datadigest); |
| 1769 | if (iscsi_digest_final_send(conn, ctask, &ctask->immbuf, |
| 1770 | &ctask->datadigest, 0)) { |
| 1771 | ctask->xmstate |= XMSTATE_DATA_DIGEST; |
| 1772 | debug_tcp("resent data digest 0x%x fail!\n", |
| 1773 | ctask->datadigest); |
| 1774 | return -EAGAIN; |
| 1775 | } |
| 1776 | return 0; |
| 1777 | } |
| 1778 | |
| 1779 | static inline int |
| 1780 | handle_xmstate_imm_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) |
| 1781 | { |
| 1782 | BUG_ON(!ctask->imm_count); |
| 1783 | ctask->xmstate &= ~XMSTATE_IMM_DATA; |
| 1784 | |
| 1785 | if (conn->datadgst_en) { |
| 1786 | iscsi_data_digest_init(conn, ctask); |
| 1787 | ctask->immdigest = 0; |
| 1788 | } |
| 1789 | |
| 1790 | for (;;) { |
| 1791 | if (iscsi_sendpage(conn, &ctask->sendbuf, &ctask->imm_count, |
| 1792 | &ctask->sent)) { |
| 1793 | ctask->xmstate |= XMSTATE_IMM_DATA; |
| 1794 | if (conn->datadgst_en) { |
| 1795 | crypto_digest_final(conn->data_tx_tfm, |
| 1796 | (u8*)&ctask->immdigest); |
| 1797 | debug_tcp("tx imm sendpage fail 0x%x\n", |
| 1798 | ctask->datadigest); |
| 1799 | } |
| 1800 | return -EAGAIN; |
| 1801 | } |
| 1802 | if (conn->datadgst_en) |
| 1803 | iscsi_buf_data_digest_update(conn, &ctask->sendbuf); |
| 1804 | |
| 1805 | if (!ctask->imm_count) |
| 1806 | break; |
| 1807 | iscsi_buf_init_sg(&ctask->sendbuf, |
| 1808 | &ctask->sg[ctask->sg_count++]); |
| 1809 | } |
| 1810 | |
| 1811 | if (conn->datadgst_en && !(ctask->xmstate & XMSTATE_W_PAD)) { |
| 1812 | if (iscsi_digest_final_send(conn, ctask, &ctask->immbuf, |
| 1813 | &ctask->immdigest, 1)) { |
| 1814 | debug_tcp("sending imm digest 0x%x fail!\n", |
| 1815 | ctask->immdigest); |
| 1816 | return -EAGAIN; |
| 1817 | } |
| 1818 | debug_tcp("sending imm digest 0x%x\n", ctask->immdigest); |
| 1819 | } |
| 1820 | |
| 1821 | return 0; |
| 1822 | } |
| 1823 | |
| 1824 | static inline int |
| 1825 | handle_xmstate_uns_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) |
| 1826 | { |
| 1827 | struct iscsi_data_task *dtask; |
| 1828 | |
| 1829 | ctask->xmstate |= XMSTATE_UNS_DATA; |
| 1830 | if (ctask->xmstate & XMSTATE_UNS_INIT) { |
| 1831 | iscsi_unsolicit_data_init(conn, ctask); |
| 1832 | BUG_ON(!ctask->dtask); |
| 1833 | dtask = ctask->dtask; |
Mike Christie | af97348 | 2005-09-12 21:01:32 -0500 | [diff] [blame] | 1834 | if (conn->hdrdgst_en) |
| 1835 | iscsi_hdr_digest(conn, &ctask->headbuf, |
| 1836 | (u8*)dtask->hdrext); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1837 | ctask->xmstate &= ~XMSTATE_UNS_INIT; |
| 1838 | } |
| 1839 | if (iscsi_sendhdr(conn, &ctask->headbuf, ctask->data_count)) { |
| 1840 | ctask->xmstate &= ~XMSTATE_UNS_DATA; |
| 1841 | ctask->xmstate |= XMSTATE_UNS_HDR; |
| 1842 | return -EAGAIN; |
| 1843 | } |
| 1844 | |
| 1845 | debug_scsi("uns dout [itt 0x%x dlen %d sent %d]\n", |
| 1846 | ctask->itt, ctask->unsol_count, ctask->sent); |
| 1847 | return 0; |
| 1848 | } |
| 1849 | |
| 1850 | static inline int |
| 1851 | handle_xmstate_uns_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) |
| 1852 | { |
| 1853 | struct iscsi_data_task *dtask = ctask->dtask; |
| 1854 | |
| 1855 | BUG_ON(!ctask->data_count); |
| 1856 | ctask->xmstate &= ~XMSTATE_UNS_DATA; |
| 1857 | |
| 1858 | if (conn->datadgst_en) { |
| 1859 | iscsi_data_digest_init(conn, ctask); |
| 1860 | dtask->digest = 0; |
| 1861 | } |
| 1862 | |
| 1863 | for (;;) { |
| 1864 | int start = ctask->sent; |
| 1865 | |
| 1866 | if (iscsi_sendpage(conn, &ctask->sendbuf, &ctask->data_count, |
| 1867 | &ctask->sent)) { |
| 1868 | ctask->unsol_count -= ctask->sent - start; |
| 1869 | ctask->xmstate |= XMSTATE_UNS_DATA; |
| 1870 | /* will continue with this ctask later.. */ |
| 1871 | if (conn->datadgst_en) { |
| 1872 | crypto_digest_final(conn->data_tx_tfm, |
| 1873 | (u8 *)&dtask->digest); |
| 1874 | debug_tcp("tx uns data fail 0x%x\n", |
| 1875 | dtask->digest); |
| 1876 | } |
| 1877 | return -EAGAIN; |
| 1878 | } |
| 1879 | |
| 1880 | BUG_ON(ctask->sent > ctask->total_length); |
| 1881 | ctask->unsol_count -= ctask->sent - start; |
| 1882 | |
| 1883 | /* |
| 1884 | * XXX:we may run here with un-initial sendbuf. |
| 1885 | * so pass it |
| 1886 | */ |
| 1887 | if (conn->datadgst_en && ctask->sent - start > 0) |
| 1888 | iscsi_buf_data_digest_update(conn, &ctask->sendbuf); |
| 1889 | |
| 1890 | if (!ctask->data_count) |
| 1891 | break; |
| 1892 | iscsi_buf_init_sg(&ctask->sendbuf, |
| 1893 | &ctask->sg[ctask->sg_count++]); |
| 1894 | } |
| 1895 | BUG_ON(ctask->unsol_count < 0); |
| 1896 | |
| 1897 | /* |
| 1898 | * Done with the Data-Out. Next, check if we need |
| 1899 | * to send another unsolicited Data-Out. |
| 1900 | */ |
| 1901 | if (ctask->unsol_count) { |
| 1902 | if (conn->datadgst_en) { |
| 1903 | if (iscsi_digest_final_send(conn, ctask, |
| 1904 | &dtask->digestbuf, |
| 1905 | &dtask->digest, 1)) { |
| 1906 | debug_tcp("send uns digest 0x%x fail\n", |
| 1907 | dtask->digest); |
| 1908 | return -EAGAIN; |
| 1909 | } |
| 1910 | debug_tcp("sending uns digest 0x%x, more uns\n", |
| 1911 | dtask->digest); |
| 1912 | } |
| 1913 | ctask->xmstate |= XMSTATE_UNS_INIT; |
| 1914 | return 1; |
| 1915 | } |
| 1916 | |
| 1917 | if (conn->datadgst_en && !(ctask->xmstate & XMSTATE_W_PAD)) { |
| 1918 | if (iscsi_digest_final_send(conn, ctask, |
| 1919 | &dtask->digestbuf, |
| 1920 | &dtask->digest, 1)) { |
| 1921 | debug_tcp("send last uns digest 0x%x fail\n", |
| 1922 | dtask->digest); |
| 1923 | return -EAGAIN; |
| 1924 | } |
| 1925 | debug_tcp("sending uns digest 0x%x\n",dtask->digest); |
| 1926 | } |
| 1927 | |
| 1928 | return 0; |
| 1929 | } |
| 1930 | |
| 1931 | static inline int |
| 1932 | handle_xmstate_sol_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) |
| 1933 | { |
| 1934 | struct iscsi_session *session = conn->session; |
| 1935 | struct iscsi_r2t_info *r2t = ctask->r2t; |
| 1936 | struct iscsi_data_task *dtask = r2t->dtask; |
| 1937 | int left; |
| 1938 | |
| 1939 | ctask->xmstate &= ~XMSTATE_SOL_DATA; |
| 1940 | ctask->dtask = dtask; |
| 1941 | |
| 1942 | if (conn->datadgst_en) { |
| 1943 | iscsi_data_digest_init(conn, ctask); |
| 1944 | dtask->digest = 0; |
| 1945 | } |
| 1946 | solicit_again: |
| 1947 | /* |
| 1948 | * send Data-Out whitnin this R2T sequence. |
| 1949 | */ |
| 1950 | if (!r2t->data_count) |
| 1951 | goto data_out_done; |
| 1952 | |
| 1953 | if (iscsi_sendpage(conn, &r2t->sendbuf, &r2t->data_count, &r2t->sent)) { |
| 1954 | ctask->xmstate |= XMSTATE_SOL_DATA; |
| 1955 | /* will continue with this ctask later.. */ |
| 1956 | if (conn->datadgst_en) { |
| 1957 | crypto_digest_final(conn->data_tx_tfm, |
| 1958 | (u8 *)&dtask->digest); |
| 1959 | debug_tcp("r2t data send fail 0x%x\n", dtask->digest); |
| 1960 | } |
| 1961 | return -EAGAIN; |
| 1962 | } |
| 1963 | |
| 1964 | BUG_ON(r2t->data_count < 0); |
| 1965 | if (conn->datadgst_en) |
| 1966 | iscsi_buf_data_digest_update(conn, &r2t->sendbuf); |
| 1967 | |
| 1968 | if (r2t->data_count) { |
| 1969 | BUG_ON(ctask->sc->use_sg == 0); |
| 1970 | if (!iscsi_buf_left(&r2t->sendbuf)) { |
| 1971 | BUG_ON(ctask->bad_sg == r2t->sg); |
| 1972 | iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg); |
| 1973 | r2t->sg += 1; |
| 1974 | } |
| 1975 | goto solicit_again; |
| 1976 | } |
| 1977 | |
| 1978 | data_out_done: |
| 1979 | /* |
| 1980 | * Done with this Data-Out. Next, check if we have |
| 1981 | * to send another Data-Out for this R2T. |
| 1982 | */ |
| 1983 | BUG_ON(r2t->data_length - r2t->sent < 0); |
| 1984 | left = r2t->data_length - r2t->sent; |
| 1985 | if (left) { |
| 1986 | if (conn->datadgst_en) { |
| 1987 | if (iscsi_digest_final_send(conn, ctask, |
| 1988 | &dtask->digestbuf, |
| 1989 | &dtask->digest, 1)) { |
| 1990 | debug_tcp("send r2t data digest 0x%x" |
| 1991 | "fail\n", dtask->digest); |
| 1992 | return -EAGAIN; |
| 1993 | } |
| 1994 | debug_tcp("r2t data send digest 0x%x\n", |
| 1995 | dtask->digest); |
| 1996 | } |
| 1997 | iscsi_solicit_data_cont(conn, ctask, r2t, left); |
| 1998 | ctask->xmstate |= XMSTATE_SOL_DATA; |
| 1999 | ctask->xmstate &= ~XMSTATE_SOL_HDR; |
| 2000 | return 1; |
| 2001 | } |
| 2002 | |
| 2003 | /* |
| 2004 | * Done with this R2T. Check if there are more |
| 2005 | * outstanding R2Ts ready to be processed. |
| 2006 | */ |
| 2007 | BUG_ON(ctask->r2t_data_count - r2t->data_length < 0); |
| 2008 | if (conn->datadgst_en) { |
| 2009 | if (iscsi_digest_final_send(conn, ctask, &dtask->digestbuf, |
| 2010 | &dtask->digest, 1)) { |
| 2011 | debug_tcp("send last r2t data digest 0x%x" |
| 2012 | "fail\n", dtask->digest); |
| 2013 | return -EAGAIN; |
| 2014 | } |
| 2015 | debug_tcp("r2t done dout digest 0x%x\n", dtask->digest); |
| 2016 | } |
| 2017 | |
| 2018 | ctask->r2t_data_count -= r2t->data_length; |
| 2019 | ctask->r2t = NULL; |
| 2020 | spin_lock_bh(&session->lock); |
| 2021 | __kfifo_put(ctask->r2tpool.queue, (void*)&r2t, sizeof(void*)); |
| 2022 | spin_unlock_bh(&session->lock); |
| 2023 | if (__kfifo_get(ctask->r2tqueue, (void*)&r2t, sizeof(void*))) { |
| 2024 | ctask->r2t = r2t; |
| 2025 | ctask->xmstate |= XMSTATE_SOL_DATA; |
| 2026 | ctask->xmstate &= ~XMSTATE_SOL_HDR; |
| 2027 | return 1; |
| 2028 | } |
| 2029 | |
| 2030 | return 0; |
| 2031 | } |
| 2032 | |
| 2033 | static inline int |
| 2034 | handle_xmstate_w_pad(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) |
| 2035 | { |
| 2036 | struct iscsi_data_task *dtask = ctask->dtask; |
| 2037 | int sent; |
| 2038 | |
| 2039 | ctask->xmstate &= ~XMSTATE_W_PAD; |
| 2040 | iscsi_buf_init_virt(&ctask->sendbuf, (char*)&ctask->pad, |
| 2041 | ctask->pad_count); |
| 2042 | if (iscsi_sendpage(conn, &ctask->sendbuf, &ctask->pad_count, &sent)) { |
| 2043 | ctask->xmstate |= XMSTATE_W_PAD; |
| 2044 | return -EAGAIN; |
| 2045 | } |
| 2046 | |
| 2047 | if (conn->datadgst_en) { |
| 2048 | iscsi_buf_data_digest_update(conn, &ctask->sendbuf); |
| 2049 | /* imm data? */ |
| 2050 | if (!dtask) { |
| 2051 | if (iscsi_digest_final_send(conn, ctask, &ctask->immbuf, |
| 2052 | &ctask->immdigest, 1)) { |
| 2053 | debug_tcp("send padding digest 0x%x" |
| 2054 | "fail!\n", ctask->immdigest); |
| 2055 | return -EAGAIN; |
| 2056 | } |
| 2057 | debug_tcp("done with padding, digest 0x%x\n", |
| 2058 | ctask->datadigest); |
| 2059 | } else { |
| 2060 | if (iscsi_digest_final_send(conn, ctask, |
| 2061 | &dtask->digestbuf, |
| 2062 | &dtask->digest, 1)) { |
| 2063 | debug_tcp("send padding digest 0x%x" |
| 2064 | "fail\n", dtask->digest); |
| 2065 | return -EAGAIN; |
| 2066 | } |
| 2067 | debug_tcp("done with padding, digest 0x%x\n", |
| 2068 | dtask->digest); |
| 2069 | } |
| 2070 | } |
| 2071 | |
| 2072 | return 0; |
| 2073 | } |
| 2074 | |
| 2075 | static int |
| 2076 | iscsi_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) |
| 2077 | { |
| 2078 | int rc = 0; |
| 2079 | |
| 2080 | debug_scsi("ctask deq [cid %d xmstate %x itt 0x%x]\n", |
| 2081 | conn->id, ctask->xmstate, ctask->itt); |
| 2082 | |
| 2083 | /* |
| 2084 | * serialize with TMF AbortTask |
| 2085 | */ |
| 2086 | if (ctask->mtask) |
| 2087 | return rc; |
| 2088 | |
| 2089 | if (ctask->xmstate & XMSTATE_R_HDR) { |
| 2090 | rc = handle_xmstate_r_hdr(conn, ctask); |
| 2091 | return rc; |
| 2092 | } |
| 2093 | |
| 2094 | if (ctask->xmstate & XMSTATE_W_HDR) { |
| 2095 | rc = handle_xmstate_w_hdr(conn, ctask); |
| 2096 | if (rc) |
| 2097 | return rc; |
| 2098 | } |
| 2099 | |
| 2100 | /* XXX: for data digest xmit recover */ |
| 2101 | if (ctask->xmstate & XMSTATE_DATA_DIGEST) { |
| 2102 | rc = handle_xmstate_data_digest(conn, ctask); |
| 2103 | if (rc) |
| 2104 | return rc; |
| 2105 | } |
| 2106 | |
| 2107 | if (ctask->xmstate & XMSTATE_IMM_DATA) { |
| 2108 | rc = handle_xmstate_imm_data(conn, ctask); |
| 2109 | if (rc) |
| 2110 | return rc; |
| 2111 | } |
| 2112 | |
| 2113 | if (ctask->xmstate & XMSTATE_UNS_HDR) { |
| 2114 | BUG_ON(!ctask->unsol_count); |
| 2115 | ctask->xmstate &= ~XMSTATE_UNS_HDR; |
| 2116 | unsolicit_head_again: |
| 2117 | rc = handle_xmstate_uns_hdr(conn, ctask); |
| 2118 | if (rc) |
| 2119 | return rc; |
| 2120 | } |
| 2121 | |
| 2122 | if (ctask->xmstate & XMSTATE_UNS_DATA) { |
| 2123 | rc = handle_xmstate_uns_data(conn, ctask); |
| 2124 | if (rc == 1) |
| 2125 | goto unsolicit_head_again; |
| 2126 | else if (rc) |
| 2127 | return rc; |
| 2128 | goto done; |
| 2129 | } |
| 2130 | |
| 2131 | if (ctask->xmstate & XMSTATE_SOL_HDR) { |
| 2132 | struct iscsi_r2t_info *r2t; |
| 2133 | |
| 2134 | ctask->xmstate &= ~XMSTATE_SOL_HDR; |
| 2135 | ctask->xmstate |= XMSTATE_SOL_DATA; |
| 2136 | if (!ctask->r2t) |
| 2137 | __kfifo_get(ctask->r2tqueue, (void*)&ctask->r2t, |
| 2138 | sizeof(void*)); |
| 2139 | solicit_head_again: |
| 2140 | r2t = ctask->r2t; |
Mike Christie | af97348 | 2005-09-12 21:01:32 -0500 | [diff] [blame] | 2141 | if (conn->hdrdgst_en) |
| 2142 | iscsi_hdr_digest(conn, &r2t->headbuf, |
| 2143 | (u8*)r2t->dtask->hdrext); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 2144 | if (iscsi_sendhdr(conn, &r2t->headbuf, r2t->data_count)) { |
| 2145 | ctask->xmstate &= ~XMSTATE_SOL_DATA; |
| 2146 | ctask->xmstate |= XMSTATE_SOL_HDR; |
| 2147 | return -EAGAIN; |
| 2148 | } |
| 2149 | |
| 2150 | debug_scsi("sol dout [dsn %d itt 0x%x dlen %d sent %d]\n", |
| 2151 | r2t->solicit_datasn - 1, ctask->itt, r2t->data_count, |
| 2152 | r2t->sent); |
| 2153 | } |
| 2154 | |
| 2155 | if (ctask->xmstate & XMSTATE_SOL_DATA) { |
| 2156 | rc = handle_xmstate_sol_data(conn, ctask); |
| 2157 | if (rc == 1) |
| 2158 | goto solicit_head_again; |
| 2159 | if (rc) |
| 2160 | return rc; |
| 2161 | } |
| 2162 | |
| 2163 | done: |
| 2164 | /* |
| 2165 | * Last thing to check is whether we need to send write |
| 2166 | * padding. Note that we check for xmstate equality, not just the bit. |
| 2167 | */ |
| 2168 | if (ctask->xmstate == XMSTATE_W_PAD) |
| 2169 | rc = handle_xmstate_w_pad(conn, ctask); |
| 2170 | |
| 2171 | return rc; |
| 2172 | } |
| 2173 | |
| 2174 | /** |
| 2175 | * iscsi_data_xmit - xmit any command into the scheduled connection |
| 2176 | * @conn: iscsi connection |
| 2177 | * |
| 2178 | * Notes: |
| 2179 | * The function can return -EAGAIN in which case the caller must |
| 2180 | * re-schedule it again later or recover. '0' return code means |
| 2181 | * successful xmit. |
| 2182 | **/ |
| 2183 | static int |
| 2184 | iscsi_data_xmit(struct iscsi_conn *conn) |
| 2185 | { |
| 2186 | if (unlikely(conn->suspend_tx)) { |
| 2187 | debug_tcp("conn %d Tx suspended!\n", conn->id); |
| 2188 | return 0; |
| 2189 | } |
| 2190 | |
| 2191 | /* |
| 2192 | * Transmit in the following order: |
| 2193 | * |
| 2194 | * 1) un-finished xmit (ctask or mtask) |
| 2195 | * 2) immediate control PDUs |
| 2196 | * 3) write data |
| 2197 | * 4) SCSI commands |
| 2198 | * 5) non-immediate control PDUs |
| 2199 | * |
| 2200 | * No need to lock around __kfifo_get as long as |
| 2201 | * there's one producer and one consumer. |
| 2202 | */ |
| 2203 | |
| 2204 | BUG_ON(conn->ctask && conn->mtask); |
| 2205 | |
| 2206 | if (conn->ctask) { |
| 2207 | if (iscsi_ctask_xmit(conn, conn->ctask)) |
| 2208 | goto again; |
| 2209 | /* done with this in-progress ctask */ |
| 2210 | conn->ctask = NULL; |
| 2211 | } |
| 2212 | if (conn->mtask) { |
| 2213 | if (iscsi_mtask_xmit(conn, conn->mtask)) |
| 2214 | goto again; |
| 2215 | /* done with this in-progress mtask */ |
| 2216 | conn->mtask = NULL; |
| 2217 | } |
| 2218 | |
| 2219 | /* process immediate first */ |
| 2220 | if (unlikely(__kfifo_len(conn->immqueue))) { |
| 2221 | struct iscsi_session *session = conn->session; |
| 2222 | while (__kfifo_get(conn->immqueue, (void*)&conn->mtask, |
| 2223 | sizeof(void*))) { |
| 2224 | if (iscsi_mtask_xmit(conn, conn->mtask)) |
| 2225 | goto again; |
| 2226 | |
| 2227 | if (conn->mtask->hdr.itt == |
| 2228 | cpu_to_be32(ISCSI_RESERVED_TAG)) { |
| 2229 | spin_lock_bh(&session->lock); |
| 2230 | __kfifo_put(session->mgmtpool.queue, |
| 2231 | (void*)&conn->mtask, sizeof(void*)); |
| 2232 | spin_unlock_bh(&session->lock); |
| 2233 | } |
| 2234 | } |
| 2235 | /* done with this mtask */ |
| 2236 | conn->mtask = NULL; |
| 2237 | } |
| 2238 | |
| 2239 | /* process write queue */ |
| 2240 | while (__kfifo_get(conn->writequeue, (void*)&conn->ctask, |
| 2241 | sizeof(void*))) { |
| 2242 | if (iscsi_ctask_xmit(conn, conn->ctask)) |
| 2243 | goto again; |
| 2244 | } |
| 2245 | |
| 2246 | /* process command queue */ |
| 2247 | while (__kfifo_get(conn->xmitqueue, (void*)&conn->ctask, |
| 2248 | sizeof(void*))) { |
| 2249 | if (iscsi_ctask_xmit(conn, conn->ctask)) |
| 2250 | goto again; |
| 2251 | } |
| 2252 | /* done with this ctask */ |
| 2253 | conn->ctask = NULL; |
| 2254 | |
| 2255 | /* process the rest control plane PDUs, if any */ |
| 2256 | if (unlikely(__kfifo_len(conn->mgmtqueue))) { |
| 2257 | struct iscsi_session *session = conn->session; |
| 2258 | |
| 2259 | while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask, |
| 2260 | sizeof(void*))) { |
| 2261 | if (iscsi_mtask_xmit(conn, conn->mtask)) |
| 2262 | goto again; |
| 2263 | |
| 2264 | if (conn->mtask->hdr.itt == |
| 2265 | cpu_to_be32(ISCSI_RESERVED_TAG)) { |
| 2266 | spin_lock_bh(&session->lock); |
| 2267 | __kfifo_put(session->mgmtpool.queue, |
| 2268 | (void*)&conn->mtask, |
| 2269 | sizeof(void*)); |
| 2270 | spin_unlock_bh(&session->lock); |
| 2271 | } |
| 2272 | } |
| 2273 | /* done with this mtask */ |
| 2274 | conn->mtask = NULL; |
| 2275 | } |
| 2276 | |
| 2277 | return 0; |
| 2278 | |
| 2279 | again: |
| 2280 | if (unlikely(conn->suspend_tx)) |
| 2281 | return 0; |
| 2282 | |
| 2283 | return -EAGAIN; |
| 2284 | } |
| 2285 | |
| 2286 | static void |
| 2287 | iscsi_xmitworker(void *data) |
| 2288 | { |
| 2289 | struct iscsi_conn *conn = data; |
| 2290 | |
| 2291 | /* |
| 2292 | * serialize Xmit worker on a per-connection basis. |
| 2293 | */ |
| 2294 | down(&conn->xmitsema); |
| 2295 | if (iscsi_data_xmit(conn)) |
| 2296 | schedule_work(&conn->xmitwork); |
| 2297 | up(&conn->xmitsema); |
| 2298 | } |
| 2299 | |
| 2300 | #define FAILURE_BAD_HOST 1 |
| 2301 | #define FAILURE_SESSION_FAILED 2 |
| 2302 | #define FAILURE_SESSION_FREED 3 |
| 2303 | #define FAILURE_WINDOW_CLOSED 4 |
| 2304 | #define FAILURE_SESSION_TERMINATE 5 |
| 2305 | |
| 2306 | static int |
| 2307 | iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *)) |
| 2308 | { |
| 2309 | struct Scsi_Host *host; |
| 2310 | int reason = 0; |
| 2311 | struct iscsi_session *session; |
| 2312 | struct iscsi_conn *conn = NULL; |
| 2313 | struct iscsi_cmd_task *ctask = NULL; |
| 2314 | |
| 2315 | sc->scsi_done = done; |
| 2316 | sc->result = 0; |
| 2317 | |
| 2318 | host = sc->device->host; |
| 2319 | session = iscsi_hostdata(host->hostdata); |
| 2320 | BUG_ON(host != session->host); |
| 2321 | |
| 2322 | spin_lock(&session->lock); |
| 2323 | |
| 2324 | if (session->state != ISCSI_STATE_LOGGED_IN) { |
| 2325 | if (session->state == ISCSI_STATE_FAILED) { |
| 2326 | reason = FAILURE_SESSION_FAILED; |
| 2327 | goto reject; |
| 2328 | } else if (session->state == ISCSI_STATE_TERMINATE) { |
| 2329 | reason = FAILURE_SESSION_TERMINATE; |
| 2330 | goto fault; |
| 2331 | } |
| 2332 | reason = FAILURE_SESSION_FREED; |
| 2333 | goto fault; |
| 2334 | } |
| 2335 | |
| 2336 | /* |
| 2337 | * Check for iSCSI window and take care of CmdSN wrap-around |
| 2338 | */ |
| 2339 | if ((int)(session->max_cmdsn - session->cmdsn) < 0) { |
| 2340 | reason = FAILURE_WINDOW_CLOSED; |
| 2341 | goto reject; |
| 2342 | } |
| 2343 | |
| 2344 | conn = session->leadconn; |
| 2345 | |
| 2346 | __kfifo_get(session->cmdpool.queue, (void*)&ctask, sizeof(void*)); |
| 2347 | BUG_ON(ctask->sc); |
| 2348 | |
| 2349 | sc->SCp.phase = session->age; |
| 2350 | sc->SCp.ptr = (char*)ctask; |
| 2351 | iscsi_cmd_init(conn, ctask, sc); |
| 2352 | |
| 2353 | __kfifo_put(conn->xmitqueue, (void*)&ctask, sizeof(void*)); |
| 2354 | debug_scsi( |
| 2355 | "ctask enq [%s cid %d sc %lx itt 0x%x len %d cmdsn %d win %d]\n", |
| 2356 | sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read", |
| 2357 | conn->id, (long)sc, ctask->itt, sc->request_bufflen, |
| 2358 | session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1); |
| 2359 | spin_unlock(&session->lock); |
| 2360 | |
| 2361 | if (!in_interrupt() && !down_trylock(&conn->xmitsema)) { |
| 2362 | spin_unlock_irq(host->host_lock); |
| 2363 | if (iscsi_data_xmit(conn)) |
| 2364 | schedule_work(&conn->xmitwork); |
| 2365 | up(&conn->xmitsema); |
| 2366 | spin_lock_irq(host->host_lock); |
| 2367 | } else |
| 2368 | schedule_work(&conn->xmitwork); |
| 2369 | |
| 2370 | return 0; |
| 2371 | |
| 2372 | reject: |
| 2373 | spin_unlock(&session->lock); |
| 2374 | debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason); |
| 2375 | return SCSI_MLQUEUE_HOST_BUSY; |
| 2376 | |
| 2377 | fault: |
| 2378 | spin_unlock(&session->lock); |
| 2379 | printk(KERN_ERR "iscsi_tcp: cmd 0x%x is not queued (%d)\n", |
| 2380 | sc->cmnd[0], reason); |
| 2381 | sc->sense_buffer[0] = 0x70; |
| 2382 | sc->sense_buffer[2] = NOT_READY; |
| 2383 | sc->sense_buffer[7] = 0x6; |
| 2384 | sc->sense_buffer[12] = 0x08; |
| 2385 | sc->sense_buffer[13] = 0x00; |
| 2386 | sc->result = (DID_NO_CONNECT << 16); |
| 2387 | sc->resid = sc->request_bufflen; |
| 2388 | sc->scsi_done(sc); |
| 2389 | return 0; |
| 2390 | } |
| 2391 | |
| 2392 | static int |
| 2393 | iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size) |
| 2394 | { |
| 2395 | int i; |
| 2396 | |
| 2397 | *items = kmalloc(max * sizeof(void*), GFP_KERNEL); |
| 2398 | if (*items == NULL) |
| 2399 | return -ENOMEM; |
| 2400 | |
| 2401 | q->max = max; |
| 2402 | q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL); |
| 2403 | if (q->pool == NULL) { |
| 2404 | kfree(*items); |
| 2405 | return -ENOMEM; |
| 2406 | } |
| 2407 | |
| 2408 | q->queue = kfifo_init((void*)q->pool, max * sizeof(void*), |
| 2409 | GFP_KERNEL, NULL); |
| 2410 | if (q->queue == ERR_PTR(-ENOMEM)) { |
| 2411 | kfree(q->pool); |
| 2412 | kfree(*items); |
| 2413 | return -ENOMEM; |
| 2414 | } |
| 2415 | |
| 2416 | for (i = 0; i < max; i++) { |
| 2417 | q->pool[i] = kmalloc(item_size, GFP_KERNEL); |
| 2418 | if (q->pool[i] == NULL) { |
| 2419 | int j; |
| 2420 | |
| 2421 | for (j = 0; j < i; j++) |
| 2422 | kfree(q->pool[j]); |
| 2423 | |
| 2424 | kfifo_free(q->queue); |
| 2425 | kfree(q->pool); |
| 2426 | kfree(*items); |
| 2427 | return -ENOMEM; |
| 2428 | } |
| 2429 | memset(q->pool[i], 0, item_size); |
| 2430 | (*items)[i] = q->pool[i]; |
| 2431 | __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*)); |
| 2432 | } |
| 2433 | return 0; |
| 2434 | } |
| 2435 | |
| 2436 | static void |
| 2437 | iscsi_pool_free(struct iscsi_queue *q, void **items) |
| 2438 | { |
| 2439 | int i; |
| 2440 | |
| 2441 | for (i = 0; i < q->max; i++) |
| 2442 | kfree(items[i]); |
| 2443 | kfree(q->pool); |
| 2444 | kfree(items); |
| 2445 | } |
| 2446 | |
| 2447 | static iscsi_connh_t |
| 2448 | iscsi_conn_create(iscsi_sessionh_t sessionh, uint32_t conn_idx) |
| 2449 | { |
| 2450 | struct iscsi_session *session = iscsi_ptr(sessionh); |
| 2451 | struct iscsi_conn *conn = NULL; |
| 2452 | |
| 2453 | conn = kmalloc(sizeof(struct iscsi_conn), GFP_KERNEL); |
| 2454 | if (conn == NULL) |
| 2455 | goto conn_alloc_fail; |
| 2456 | memset(conn, 0, sizeof(struct iscsi_conn)); |
| 2457 | |
| 2458 | conn->c_stage = ISCSI_CONN_INITIAL_STAGE; |
| 2459 | conn->in_progress = IN_PROGRESS_WAIT_HEADER; |
| 2460 | conn->id = conn_idx; |
| 2461 | conn->exp_statsn = 0; |
| 2462 | conn->tmabort_state = TMABORT_INITIAL; |
| 2463 | |
| 2464 | /* initial operational parameters */ |
| 2465 | conn->hdr_size = sizeof(struct iscsi_hdr); |
| 2466 | conn->data_size = DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH; |
| 2467 | conn->max_recv_dlength = DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH; |
| 2468 | |
| 2469 | spin_lock_init(&conn->lock); |
| 2470 | |
| 2471 | /* initialize general xmit PDU commands queue */ |
| 2472 | conn->xmitqueue = kfifo_alloc(session->cmds_max * sizeof(void*), |
| 2473 | GFP_KERNEL, NULL); |
| 2474 | if (conn->xmitqueue == ERR_PTR(-ENOMEM)) |
| 2475 | goto xmitqueue_alloc_fail; |
| 2476 | |
| 2477 | /* initialize write response PDU commands queue */ |
| 2478 | conn->writequeue = kfifo_alloc(session->cmds_max * sizeof(void*), |
| 2479 | GFP_KERNEL, NULL); |
| 2480 | if (conn->writequeue == ERR_PTR(-ENOMEM)) |
| 2481 | goto writequeue_alloc_fail; |
| 2482 | |
| 2483 | /* initialize general immediate & non-immediate PDU commands queue */ |
| 2484 | conn->immqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*), |
| 2485 | GFP_KERNEL, NULL); |
| 2486 | if (conn->immqueue == ERR_PTR(-ENOMEM)) |
| 2487 | goto immqueue_alloc_fail; |
| 2488 | |
| 2489 | conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*), |
| 2490 | GFP_KERNEL, NULL); |
| 2491 | if (conn->mgmtqueue == ERR_PTR(-ENOMEM)) |
| 2492 | goto mgmtqueue_alloc_fail; |
| 2493 | |
| 2494 | INIT_WORK(&conn->xmitwork, iscsi_xmitworker, conn); |
| 2495 | |
| 2496 | /* allocate login_mtask used for the login/text sequences */ |
| 2497 | spin_lock_bh(&session->lock); |
| 2498 | if (!__kfifo_get(session->mgmtpool.queue, |
| 2499 | (void*)&conn->login_mtask, |
| 2500 | sizeof(void*))) { |
| 2501 | spin_unlock_bh(&session->lock); |
| 2502 | goto login_mtask_alloc_fail; |
| 2503 | } |
| 2504 | spin_unlock_bh(&session->lock); |
| 2505 | |
| 2506 | /* allocate initial PDU receive place holder */ |
| 2507 | if (conn->data_size <= PAGE_SIZE) |
| 2508 | conn->data = kmalloc(conn->data_size, GFP_KERNEL); |
| 2509 | else |
| 2510 | conn->data = (void*)__get_free_pages(GFP_KERNEL, |
| 2511 | get_order(conn->data_size)); |
| 2512 | if (!conn->data) |
| 2513 | goto max_recv_dlenght_alloc_fail; |
| 2514 | |
| 2515 | init_timer(&conn->tmabort_timer); |
| 2516 | init_MUTEX(&conn->xmitsema); |
| 2517 | init_waitqueue_head(&conn->ehwait); |
| 2518 | |
| 2519 | return iscsi_handle(conn); |
| 2520 | |
| 2521 | max_recv_dlenght_alloc_fail: |
| 2522 | spin_lock_bh(&session->lock); |
| 2523 | __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask, |
| 2524 | sizeof(void*)); |
| 2525 | spin_unlock_bh(&session->lock); |
| 2526 | login_mtask_alloc_fail: |
| 2527 | kfifo_free(conn->mgmtqueue); |
| 2528 | mgmtqueue_alloc_fail: |
| 2529 | kfifo_free(conn->immqueue); |
| 2530 | immqueue_alloc_fail: |
| 2531 | kfifo_free(conn->writequeue); |
| 2532 | writequeue_alloc_fail: |
| 2533 | kfifo_free(conn->xmitqueue); |
| 2534 | xmitqueue_alloc_fail: |
| 2535 | kfree(conn); |
| 2536 | conn_alloc_fail: |
| 2537 | return iscsi_handle(NULL); |
| 2538 | } |
| 2539 | |
| 2540 | static void |
| 2541 | iscsi_conn_destroy(iscsi_connh_t connh) |
| 2542 | { |
| 2543 | struct iscsi_conn *conn = iscsi_ptr(connh); |
| 2544 | struct iscsi_session *session = conn->session; |
| 2545 | |
| 2546 | down(&conn->xmitsema); |
| 2547 | set_bit(SUSPEND_BIT, &conn->suspend_tx); |
| 2548 | if (conn->c_stage == ISCSI_CONN_INITIAL_STAGE && conn->sock) { |
| 2549 | struct sock *sk = conn->sock->sk; |
| 2550 | |
| 2551 | /* |
| 2552 | * conn_start() has never been called! |
| 2553 | * need to cleanup the socket. |
| 2554 | */ |
| 2555 | write_lock_bh(&sk->sk_callback_lock); |
| 2556 | set_bit(SUSPEND_BIT, &conn->suspend_rx); |
| 2557 | write_unlock_bh(&sk->sk_callback_lock); |
| 2558 | |
| 2559 | sock_hold(conn->sock->sk); |
| 2560 | iscsi_conn_restore_callbacks(conn); |
| 2561 | sock_put(conn->sock->sk); |
| 2562 | sock_release(conn->sock); |
| 2563 | conn->sock = NULL; |
| 2564 | } |
| 2565 | |
| 2566 | spin_lock_bh(&session->lock); |
| 2567 | conn->c_stage = ISCSI_CONN_CLEANUP_WAIT; |
| 2568 | if (session->leadconn == conn) { |
| 2569 | /* |
| 2570 | * leading connection? then give up on recovery. |
| 2571 | */ |
| 2572 | session->state = ISCSI_STATE_TERMINATE; |
| 2573 | wake_up(&conn->ehwait); |
| 2574 | } |
| 2575 | spin_unlock_bh(&session->lock); |
| 2576 | |
| 2577 | up(&conn->xmitsema); |
| 2578 | |
| 2579 | /* |
| 2580 | * Block until all in-progress commands for this connection |
| 2581 | * time out or fail. |
| 2582 | */ |
| 2583 | for (;;) { |
| 2584 | spin_lock_bh(&conn->lock); |
| 2585 | if (!session->host->host_busy) { /* OK for ERL == 0 */ |
| 2586 | spin_unlock_bh(&conn->lock); |
| 2587 | break; |
| 2588 | } |
| 2589 | spin_unlock_bh(&conn->lock); |
| 2590 | msleep_interruptible(500); |
| 2591 | printk("conn_destroy(): host_busy %d host_failed %d\n", |
| 2592 | session->host->host_busy, session->host->host_failed); |
| 2593 | /* |
| 2594 | * force eh_abort() to unblock |
| 2595 | */ |
| 2596 | wake_up(&conn->ehwait); |
| 2597 | } |
| 2598 | |
| 2599 | /* now free crypto */ |
| 2600 | if (conn->hdrdgst_en || conn->datadgst_en) { |
| 2601 | if (conn->tx_tfm) |
| 2602 | crypto_free_tfm(conn->tx_tfm); |
| 2603 | if (conn->rx_tfm) |
| 2604 | crypto_free_tfm(conn->rx_tfm); |
| 2605 | if (conn->data_tx_tfm) |
| 2606 | crypto_free_tfm(conn->data_tx_tfm); |
| 2607 | if (conn->data_rx_tfm) |
| 2608 | crypto_free_tfm(conn->data_rx_tfm); |
| 2609 | } |
| 2610 | |
| 2611 | /* free conn->data, size = MaxRecvDataSegmentLength */ |
| 2612 | if (conn->data_size <= PAGE_SIZE) |
| 2613 | kfree(conn->data); |
| 2614 | else |
| 2615 | free_pages((unsigned long)conn->data, |
| 2616 | get_order(conn->data_size)); |
| 2617 | |
| 2618 | spin_lock_bh(&session->lock); |
| 2619 | __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask, |
| 2620 | sizeof(void*)); |
| 2621 | list_del(&conn->item); |
| 2622 | if (list_empty(&session->connections)) |
| 2623 | session->leadconn = NULL; |
| 2624 | if (session->leadconn && session->leadconn == conn) |
| 2625 | session->leadconn = container_of(session->connections.next, |
| 2626 | struct iscsi_conn, item); |
| 2627 | |
| 2628 | if (session->leadconn == NULL) |
| 2629 | /* none connections exits.. reset sequencing */ |
| 2630 | session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1; |
| 2631 | spin_unlock_bh(&session->lock); |
| 2632 | |
| 2633 | kfifo_free(conn->xmitqueue); |
| 2634 | kfifo_free(conn->writequeue); |
| 2635 | kfifo_free(conn->immqueue); |
| 2636 | kfifo_free(conn->mgmtqueue); |
| 2637 | kfree(conn); |
| 2638 | } |
| 2639 | |
| 2640 | static int |
| 2641 | iscsi_conn_bind(iscsi_sessionh_t sessionh, iscsi_connh_t connh, |
| 2642 | uint32_t transport_fd, int is_leading) |
| 2643 | { |
| 2644 | struct iscsi_session *session = iscsi_ptr(sessionh); |
| 2645 | struct iscsi_conn *tmp = ERR_PTR(-EEXIST), *conn = iscsi_ptr(connh); |
| 2646 | struct sock *sk; |
| 2647 | struct socket *sock; |
| 2648 | int err; |
| 2649 | |
| 2650 | /* lookup for existing socket */ |
| 2651 | sock = sockfd_lookup(transport_fd, &err); |
| 2652 | if (!sock) { |
| 2653 | printk(KERN_ERR "iscsi_tcp: sockfd_lookup failed %d\n", err); |
| 2654 | return -EEXIST; |
| 2655 | } |
| 2656 | |
| 2657 | /* lookup for existing connection */ |
| 2658 | spin_lock_bh(&session->lock); |
| 2659 | list_for_each_entry(tmp, &session->connections, item) { |
| 2660 | if (tmp == conn) { |
| 2661 | if (conn->c_stage != ISCSI_CONN_STOPPED || |
| 2662 | conn->stop_stage == STOP_CONN_TERM) { |
| 2663 | printk(KERN_ERR "iscsi_tcp: can't bind " |
| 2664 | "non-stopped connection (%d:%d)\n", |
| 2665 | conn->c_stage, conn->stop_stage); |
| 2666 | spin_unlock_bh(&session->lock); |
| 2667 | return -EIO; |
| 2668 | } |
| 2669 | break; |
| 2670 | } |
| 2671 | } |
| 2672 | if (tmp != conn) { |
| 2673 | /* bind new iSCSI connection to session */ |
| 2674 | conn->session = session; |
| 2675 | |
| 2676 | list_add(&conn->item, &session->connections); |
| 2677 | } |
| 2678 | spin_unlock_bh(&session->lock); |
| 2679 | |
| 2680 | if (conn->stop_stage != STOP_CONN_SUSPEND) { |
| 2681 | /* bind iSCSI connection and socket */ |
| 2682 | conn->sock = sock; |
| 2683 | |
| 2684 | /* setup Socket parameters */ |
| 2685 | sk = sock->sk; |
| 2686 | sk->sk_reuse = 1; |
| 2687 | sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */ |
| 2688 | sk->sk_allocation = GFP_ATOMIC; |
| 2689 | |
| 2690 | /* FIXME: disable Nagle's algorithm */ |
| 2691 | |
| 2692 | /* |
| 2693 | * Intercept TCP callbacks for sendfile like receive |
| 2694 | * processing. |
| 2695 | */ |
| 2696 | iscsi_conn_set_callbacks(conn); |
| 2697 | |
| 2698 | /* |
| 2699 | * set receive state machine into initial state |
| 2700 | */ |
| 2701 | conn->in_progress = IN_PROGRESS_WAIT_HEADER; |
| 2702 | } |
| 2703 | |
| 2704 | if (is_leading) |
| 2705 | session->leadconn = conn; |
| 2706 | |
| 2707 | /* |
| 2708 | * Unblock xmitworker(), Login Phase will pass through. |
| 2709 | */ |
| 2710 | clear_bit(SUSPEND_BIT, &conn->suspend_rx); |
| 2711 | clear_bit(SUSPEND_BIT, &conn->suspend_tx); |
| 2712 | |
| 2713 | return 0; |
| 2714 | } |
| 2715 | |
| 2716 | static int |
| 2717 | iscsi_conn_start(iscsi_connh_t connh) |
| 2718 | { |
| 2719 | struct iscsi_conn *conn = iscsi_ptr(connh); |
| 2720 | struct iscsi_session *session = conn->session; |
| 2721 | struct sock *sk; |
| 2722 | |
| 2723 | /* FF phase warming up... */ |
| 2724 | |
| 2725 | if (session == NULL) { |
| 2726 | printk(KERN_ERR "iscsi_tcp: can't start unbound connection\n"); |
| 2727 | return -EPERM; |
| 2728 | } |
| 2729 | |
| 2730 | sk = conn->sock->sk; |
| 2731 | |
| 2732 | write_lock_bh(&sk->sk_callback_lock); |
| 2733 | spin_lock_bh(&session->lock); |
| 2734 | conn->c_stage = ISCSI_CONN_STARTED; |
| 2735 | session->state = ISCSI_STATE_LOGGED_IN; |
| 2736 | |
| 2737 | switch(conn->stop_stage) { |
| 2738 | case STOP_CONN_RECOVER: |
| 2739 | /* |
| 2740 | * unblock eh_abort() if it is blocked. re-try all |
| 2741 | * commands after successful recovery |
| 2742 | */ |
| 2743 | session->conn_cnt++; |
| 2744 | conn->stop_stage = 0; |
| 2745 | conn->tmabort_state = TMABORT_INITIAL; |
| 2746 | session->age++; |
| 2747 | wake_up(&conn->ehwait); |
| 2748 | break; |
| 2749 | case STOP_CONN_TERM: |
| 2750 | session->conn_cnt++; |
| 2751 | conn->stop_stage = 0; |
| 2752 | break; |
| 2753 | case STOP_CONN_SUSPEND: |
| 2754 | conn->stop_stage = 0; |
| 2755 | clear_bit(SUSPEND_BIT, &conn->suspend_rx); |
| 2756 | clear_bit(SUSPEND_BIT, &conn->suspend_tx); |
| 2757 | break; |
| 2758 | default: |
| 2759 | break; |
| 2760 | } |
| 2761 | spin_unlock_bh(&session->lock); |
| 2762 | write_unlock_bh(&sk->sk_callback_lock); |
| 2763 | |
| 2764 | return 0; |
| 2765 | } |
| 2766 | |
| 2767 | static void |
| 2768 | iscsi_conn_stop(iscsi_connh_t connh, int flag) |
| 2769 | { |
| 2770 | struct iscsi_conn *conn = iscsi_ptr(connh); |
| 2771 | struct iscsi_session *session = conn->session; |
| 2772 | struct sock *sk; |
| 2773 | unsigned long flags; |
| 2774 | |
| 2775 | BUG_ON(!conn->sock); |
| 2776 | sk = conn->sock->sk; |
| 2777 | write_lock_bh(&sk->sk_callback_lock); |
| 2778 | set_bit(SUSPEND_BIT, &conn->suspend_rx); |
| 2779 | write_unlock_bh(&sk->sk_callback_lock); |
| 2780 | |
| 2781 | down(&conn->xmitsema); |
| 2782 | |
| 2783 | spin_lock_irqsave(session->host->host_lock, flags); |
| 2784 | spin_lock(&session->lock); |
| 2785 | conn->stop_stage = flag; |
| 2786 | conn->c_stage = ISCSI_CONN_STOPPED; |
| 2787 | set_bit(SUSPEND_BIT, &conn->suspend_tx); |
| 2788 | |
| 2789 | if (flag != STOP_CONN_SUSPEND) |
| 2790 | session->conn_cnt--; |
| 2791 | |
| 2792 | if (session->conn_cnt == 0 || session->leadconn == conn) |
| 2793 | session->state = ISCSI_STATE_FAILED; |
| 2794 | |
| 2795 | spin_unlock(&session->lock); |
| 2796 | spin_unlock_irqrestore(session->host->host_lock, flags); |
| 2797 | |
| 2798 | if (flag == STOP_CONN_TERM || flag == STOP_CONN_RECOVER) { |
| 2799 | struct iscsi_cmd_task *ctask; |
| 2800 | struct iscsi_mgmt_task *mtask; |
| 2801 | |
| 2802 | /* |
| 2803 | * Socket must go now. |
| 2804 | */ |
| 2805 | sock_hold(conn->sock->sk); |
| 2806 | iscsi_conn_restore_callbacks(conn); |
| 2807 | sock_put(conn->sock->sk); |
| 2808 | |
| 2809 | /* |
| 2810 | * flush xmit queues. |
| 2811 | */ |
| 2812 | spin_lock_bh(&session->lock); |
| 2813 | while (__kfifo_get(conn->writequeue, (void*)&ctask, |
| 2814 | sizeof(void*)) || |
| 2815 | __kfifo_get(conn->xmitqueue, (void*)&ctask, |
| 2816 | sizeof(void*))) { |
| 2817 | struct iscsi_r2t_info *r2t; |
| 2818 | |
| 2819 | /* |
| 2820 | * flush ctask's r2t queues |
| 2821 | */ |
| 2822 | while (__kfifo_get(ctask->r2tqueue, (void*)&r2t, |
| 2823 | sizeof(void*))) |
| 2824 | __kfifo_put(ctask->r2tpool.queue, (void*)&r2t, |
| 2825 | sizeof(void*)); |
| 2826 | |
| 2827 | spin_unlock_bh(&session->lock); |
| 2828 | local_bh_disable(); |
| 2829 | iscsi_ctask_cleanup(conn, ctask); |
| 2830 | local_bh_enable(); |
| 2831 | spin_lock_bh(&session->lock); |
| 2832 | } |
| 2833 | conn->ctask = NULL; |
| 2834 | while (__kfifo_get(conn->immqueue, (void*)&mtask, |
| 2835 | sizeof(void*)) || |
| 2836 | __kfifo_get(conn->mgmtqueue, (void*)&mtask, |
| 2837 | sizeof(void*))) { |
| 2838 | __kfifo_put(session->mgmtpool.queue, |
| 2839 | (void*)&mtask, sizeof(void*)); |
| 2840 | } |
| 2841 | conn->mtask = NULL; |
| 2842 | spin_unlock_bh(&session->lock); |
| 2843 | |
| 2844 | /* |
| 2845 | * release socket only after we stopped data_xmit() |
| 2846 | * activity and flushed all outstandings |
| 2847 | */ |
| 2848 | sock_release(conn->sock); |
| 2849 | conn->sock = NULL; |
| 2850 | |
| 2851 | /* |
| 2852 | * for connection level recovery we should not calculate |
| 2853 | * header digest. conn->hdr_size used for optimization |
| 2854 | * in hdr_extract() and will be re-negotiated at |
| 2855 | * set_param() time. |
| 2856 | */ |
| 2857 | if (flag == STOP_CONN_RECOVER) |
| 2858 | conn->hdr_size = sizeof(struct iscsi_hdr); |
| 2859 | } |
| 2860 | up(&conn->xmitsema); |
| 2861 | } |
| 2862 | |
| 2863 | static int |
| 2864 | iscsi_conn_send_generic(struct iscsi_conn *conn, struct iscsi_hdr *hdr, |
| 2865 | char *data, uint32_t data_size) |
| 2866 | { |
| 2867 | struct iscsi_session *session = conn->session; |
| 2868 | struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr; |
| 2869 | struct iscsi_mgmt_task *mtask; |
| 2870 | |
| 2871 | spin_lock_bh(&session->lock); |
| 2872 | if (session->state == ISCSI_STATE_TERMINATE) { |
| 2873 | spin_unlock_bh(&session->lock); |
| 2874 | return -EPERM; |
| 2875 | } |
| 2876 | if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) || |
| 2877 | hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE)) |
| 2878 | /* |
| 2879 | * Login and Text are sent serially, in |
| 2880 | * request-followed-by-response sequence. |
| 2881 | * Same mtask can be used. Same ITT must be used. |
| 2882 | * Note that login_mtask is preallocated at conn_create(). |
| 2883 | */ |
| 2884 | mtask = conn->login_mtask; |
| 2885 | else { |
| 2886 | BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE); |
| 2887 | BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED); |
| 2888 | |
| 2889 | if (!__kfifo_get(session->mgmtpool.queue, |
| 2890 | (void*)&mtask, sizeof(void*))) { |
| 2891 | spin_unlock_bh(&session->lock); |
| 2892 | return -ENOSPC; |
| 2893 | } |
| 2894 | } |
| 2895 | |
| 2896 | /* |
| 2897 | * pre-format CmdSN and ExpStatSN for outgoing PDU. |
| 2898 | */ |
| 2899 | if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) { |
| 2900 | hdr->itt = mtask->itt | (conn->id << CID_SHIFT) | |
| 2901 | (session->age << AGE_SHIFT); |
| 2902 | nop->cmdsn = cpu_to_be32(session->cmdsn); |
| 2903 | if (conn->c_stage == ISCSI_CONN_STARTED && |
| 2904 | !(hdr->opcode & ISCSI_OP_IMMEDIATE)) |
| 2905 | session->cmdsn++; |
| 2906 | } else |
| 2907 | /* do not advance CmdSN */ |
| 2908 | nop->cmdsn = cpu_to_be32(session->cmdsn); |
| 2909 | |
| 2910 | nop->exp_statsn = cpu_to_be32(conn->exp_statsn); |
| 2911 | |
| 2912 | memcpy(&mtask->hdr, hdr, sizeof(struct iscsi_hdr)); |
| 2913 | |
Mike Christie | af97348 | 2005-09-12 21:01:32 -0500 | [diff] [blame] | 2914 | iscsi_buf_init_virt(&mtask->headbuf, (char*)&mtask->hdr, |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 2915 | sizeof(struct iscsi_hdr)); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 2916 | |
| 2917 | spin_unlock_bh(&session->lock); |
| 2918 | |
| 2919 | if (data_size) { |
| 2920 | memcpy(mtask->data, data, data_size); |
| 2921 | mtask->data_count = data_size; |
| 2922 | } else |
| 2923 | mtask->data_count = 0; |
| 2924 | |
| 2925 | mtask->xmstate = XMSTATE_IMM_HDR; |
| 2926 | |
| 2927 | if (mtask->data_count) { |
| 2928 | iscsi_buf_init_iov(&mtask->sendbuf, (char*)mtask->data, |
| 2929 | mtask->data_count); |
| 2930 | } |
| 2931 | |
| 2932 | debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n", |
| 2933 | hdr->opcode, hdr->itt, data_size); |
| 2934 | |
| 2935 | /* |
| 2936 | * since send_pdu() could be called at least from two contexts, |
| 2937 | * we need to serialize __kfifo_put, so we don't have to take |
| 2938 | * additional lock on fast data-path |
| 2939 | */ |
| 2940 | if (hdr->opcode & ISCSI_OP_IMMEDIATE) |
| 2941 | __kfifo_put(conn->immqueue, (void*)&mtask, sizeof(void*)); |
| 2942 | else |
| 2943 | __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*)); |
| 2944 | |
| 2945 | schedule_work(&conn->xmitwork); |
| 2946 | |
| 2947 | return 0; |
| 2948 | } |
| 2949 | |
| 2950 | static int |
| 2951 | iscsi_eh_host_reset(struct scsi_cmnd *sc) |
| 2952 | { |
| 2953 | struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)sc->SCp.ptr; |
| 2954 | struct iscsi_conn *conn = ctask->conn; |
| 2955 | struct iscsi_session *session = conn->session; |
| 2956 | |
| 2957 | spin_lock_bh(&session->lock); |
| 2958 | if (session->state == ISCSI_STATE_TERMINATE) { |
| 2959 | debug_scsi("failing host reset: session terminated " |
| 2960 | "[CID %d age %d]", conn->id, session->age); |
| 2961 | spin_unlock_bh(&session->lock); |
| 2962 | return FAILED; |
| 2963 | } |
| 2964 | spin_unlock_bh(&session->lock); |
| 2965 | |
| 2966 | debug_scsi("failing connection CID %d due to SCSI host reset " |
| 2967 | "[itt 0x%x age %d]", conn->id, ctask->itt, |
| 2968 | session->age); |
| 2969 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); |
| 2970 | |
| 2971 | return SUCCESS; |
| 2972 | } |
| 2973 | |
| 2974 | static void |
| 2975 | iscsi_tmabort_timedout(unsigned long data) |
| 2976 | { |
| 2977 | struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data; |
| 2978 | struct iscsi_conn *conn = ctask->conn; |
| 2979 | struct iscsi_session *session = conn->session; |
| 2980 | |
| 2981 | spin_lock(&session->lock); |
| 2982 | if (conn->tmabort_state == TMABORT_INITIAL) { |
| 2983 | __kfifo_put(session->mgmtpool.queue, |
| 2984 | (void*)&ctask->mtask, sizeof(void*)); |
| 2985 | conn->tmabort_state = TMABORT_TIMEDOUT; |
| 2986 | debug_scsi("tmabort timedout [sc %lx itt 0x%x]\n", |
| 2987 | (long)ctask->sc, ctask->itt); |
| 2988 | /* unblock eh_abort() */ |
| 2989 | wake_up(&conn->ehwait); |
| 2990 | } |
| 2991 | spin_unlock(&session->lock); |
| 2992 | } |
| 2993 | |
| 2994 | static int |
| 2995 | iscsi_eh_abort(struct scsi_cmnd *sc) |
| 2996 | { |
| 2997 | int rc; |
| 2998 | struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)sc->SCp.ptr; |
| 2999 | struct iscsi_conn *conn = ctask->conn; |
| 3000 | struct iscsi_session *session = conn->session; |
| 3001 | |
| 3002 | conn->eh_abort_cnt++; |
| 3003 | debug_scsi("aborting [sc %lx itt 0x%x]\n", (long)sc, ctask->itt); |
| 3004 | |
| 3005 | /* |
| 3006 | * two cases for ERL=0 here: |
| 3007 | * |
| 3008 | * 1) connection-level failure; |
| 3009 | * 2) recovery due protocol error; |
| 3010 | */ |
| 3011 | down(&conn->xmitsema); |
| 3012 | spin_lock_bh(&session->lock); |
| 3013 | if (session->state != ISCSI_STATE_LOGGED_IN) { |
| 3014 | if (session->state == ISCSI_STATE_TERMINATE) { |
| 3015 | spin_unlock_bh(&session->lock); |
| 3016 | up(&conn->xmitsema); |
| 3017 | goto failed; |
| 3018 | } |
| 3019 | spin_unlock_bh(&session->lock); |
| 3020 | } else { |
| 3021 | struct iscsi_tm *hdr = &conn->tmhdr; |
| 3022 | |
| 3023 | /* |
| 3024 | * Still LOGGED_IN... |
| 3025 | */ |
| 3026 | |
| 3027 | if (!ctask->sc || sc->SCp.phase != session->age) { |
| 3028 | /* |
| 3029 | * 1) ctask completed before time out. But session |
| 3030 | * is still ok => Happy Retry. |
| 3031 | * 2) session was re-open during time out of ctask. |
| 3032 | */ |
| 3033 | spin_unlock_bh(&session->lock); |
| 3034 | up(&conn->xmitsema); |
| 3035 | goto success; |
| 3036 | } |
| 3037 | conn->tmabort_state = TMABORT_INITIAL; |
| 3038 | spin_unlock_bh(&session->lock); |
| 3039 | |
| 3040 | /* |
| 3041 | * ctask timed out but session is OK |
| 3042 | * ERL=0 requires task mgmt abort to be issued on each |
| 3043 | * failed command. requests must be serialized. |
| 3044 | */ |
| 3045 | memset(hdr, 0, sizeof(struct iscsi_tm)); |
| 3046 | hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE; |
| 3047 | hdr->flags = ISCSI_TM_FUNC_ABORT_TASK; |
| 3048 | hdr->flags |= ISCSI_FLAG_CMD_FINAL; |
| 3049 | memcpy(hdr->lun, ctask->hdr.lun, sizeof(hdr->lun)); |
| 3050 | hdr->rtt = ctask->hdr.itt; |
| 3051 | hdr->refcmdsn = ctask->hdr.cmdsn; |
| 3052 | |
| 3053 | rc = iscsi_conn_send_generic(conn, (struct iscsi_hdr *)hdr, |
| 3054 | NULL, 0); |
| 3055 | if (rc) { |
| 3056 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); |
| 3057 | debug_scsi("abort sent failure [itt 0x%x]", ctask->itt); |
| 3058 | } else { |
| 3059 | struct iscsi_r2t_info *r2t; |
| 3060 | |
| 3061 | /* |
| 3062 | * TMF abort vs. TMF response race logic |
| 3063 | */ |
| 3064 | spin_lock_bh(&session->lock); |
| 3065 | ctask->mtask = (struct iscsi_mgmt_task *) |
| 3066 | session->mgmt_cmds[(hdr->itt & ITT_MASK) - |
| 3067 | ISCSI_MGMT_ITT_OFFSET]; |
| 3068 | /* |
| 3069 | * have to flush r2tqueue to avoid r2t leaks |
| 3070 | */ |
| 3071 | while (__kfifo_get(ctask->r2tqueue, (void*)&r2t, |
| 3072 | sizeof(void*))) { |
| 3073 | __kfifo_put(ctask->r2tpool.queue, (void*)&r2t, |
| 3074 | sizeof(void*)); |
| 3075 | } |
| 3076 | if (conn->tmabort_state == TMABORT_INITIAL) { |
| 3077 | conn->tmfcmd_pdus_cnt++; |
| 3078 | conn->tmabort_timer.expires = 3*HZ + jiffies; |
| 3079 | conn->tmabort_timer.function = |
| 3080 | iscsi_tmabort_timedout; |
| 3081 | conn->tmabort_timer.data = (unsigned long)ctask; |
| 3082 | add_timer(&conn->tmabort_timer); |
| 3083 | debug_scsi("abort sent [itt 0x%x]", ctask->itt); |
| 3084 | } else { |
| 3085 | if (!ctask->sc || |
| 3086 | conn->tmabort_state == TMABORT_SUCCESS) { |
| 3087 | conn->tmabort_state = TMABORT_INITIAL; |
| 3088 | spin_unlock_bh(&session->lock); |
| 3089 | up(&conn->xmitsema); |
| 3090 | goto success; |
| 3091 | } |
| 3092 | conn->tmabort_state = TMABORT_INITIAL; |
| 3093 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); |
| 3094 | } |
| 3095 | spin_unlock_bh(&session->lock); |
| 3096 | } |
| 3097 | } |
| 3098 | up(&conn->xmitsema); |
| 3099 | |
| 3100 | |
| 3101 | /* |
| 3102 | * block eh thread until: |
| 3103 | * |
| 3104 | * 1) abort response; |
| 3105 | * 2) abort timeout; |
| 3106 | * 3) session re-opened; |
| 3107 | * 4) session terminated; |
| 3108 | */ |
| 3109 | for (;;) { |
| 3110 | int p_state = session->state; |
| 3111 | |
| 3112 | rc = wait_event_interruptible(conn->ehwait, |
| 3113 | (p_state == ISCSI_STATE_LOGGED_IN ? |
| 3114 | (session->state == ISCSI_STATE_TERMINATE || |
| 3115 | conn->tmabort_state != TMABORT_INITIAL) : |
| 3116 | (session->state == ISCSI_STATE_TERMINATE || |
| 3117 | session->state == ISCSI_STATE_LOGGED_IN))); |
| 3118 | if (rc) { |
| 3119 | /* shutdown.. */ |
| 3120 | session->state = ISCSI_STATE_TERMINATE; |
| 3121 | goto failed; |
| 3122 | } |
| 3123 | |
| 3124 | if (signal_pending(current)) |
| 3125 | flush_signals(current); |
| 3126 | |
| 3127 | if (session->state == ISCSI_STATE_TERMINATE) |
| 3128 | goto failed; |
| 3129 | |
| 3130 | spin_lock_bh(&session->lock); |
| 3131 | if (sc->SCp.phase == session->age && |
| 3132 | (conn->tmabort_state == TMABORT_TIMEDOUT || |
| 3133 | conn->tmabort_state == TMABORT_FAILED)) { |
| 3134 | conn->tmabort_state = TMABORT_INITIAL; |
| 3135 | if (!ctask->sc) { |
| 3136 | /* |
| 3137 | * ctask completed before tmf abort response or |
| 3138 | * time out. |
| 3139 | * But session is still ok => Happy Retry. |
| 3140 | */ |
| 3141 | spin_unlock_bh(&session->lock); |
| 3142 | break; |
| 3143 | } |
| 3144 | spin_unlock_bh(&session->lock); |
| 3145 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); |
| 3146 | continue; |
| 3147 | } |
| 3148 | spin_unlock_bh(&session->lock); |
| 3149 | break; |
| 3150 | } |
| 3151 | |
| 3152 | success: |
| 3153 | debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt); |
| 3154 | rc = SUCCESS; |
| 3155 | goto exit; |
| 3156 | |
| 3157 | failed: |
| 3158 | debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt); |
| 3159 | rc = FAILED; |
| 3160 | |
| 3161 | exit: |
| 3162 | del_timer_sync(&conn->tmabort_timer); |
| 3163 | |
| 3164 | down(&conn->xmitsema); |
| 3165 | if (conn->sock) { |
| 3166 | struct sock *sk = conn->sock->sk; |
| 3167 | |
| 3168 | write_lock_bh(&sk->sk_callback_lock); |
| 3169 | iscsi_ctask_cleanup(conn, ctask); |
| 3170 | write_unlock_bh(&sk->sk_callback_lock); |
| 3171 | } |
| 3172 | up(&conn->xmitsema); |
| 3173 | return rc; |
| 3174 | } |
| 3175 | |
| 3176 | static int |
| 3177 | iscsi_r2tpool_alloc(struct iscsi_session *session) |
| 3178 | { |
| 3179 | int i; |
| 3180 | int cmd_i; |
| 3181 | |
| 3182 | /* |
| 3183 | * initialize per-task: R2T pool and xmit queue |
| 3184 | */ |
| 3185 | for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) { |
| 3186 | struct iscsi_cmd_task *ctask = session->cmds[cmd_i]; |
| 3187 | |
| 3188 | /* |
| 3189 | * pre-allocated x4 as much r2ts to handle race when |
| 3190 | * target acks DataOut faster than we data_xmit() queues |
| 3191 | * could replenish r2tqueue. |
| 3192 | */ |
| 3193 | |
| 3194 | /* R2T pool */ |
| 3195 | if (iscsi_pool_init(&ctask->r2tpool, session->max_r2t * 4, |
| 3196 | (void***)&ctask->r2ts, sizeof(struct iscsi_r2t_info))) { |
| 3197 | goto r2t_alloc_fail; |
| 3198 | } |
| 3199 | |
| 3200 | /* R2T xmit queue */ |
| 3201 | ctask->r2tqueue = kfifo_alloc( |
| 3202 | session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL); |
| 3203 | if (ctask->r2tqueue == ERR_PTR(-ENOMEM)) { |
| 3204 | iscsi_pool_free(&ctask->r2tpool, (void**)ctask->r2ts); |
| 3205 | goto r2t_alloc_fail; |
| 3206 | } |
| 3207 | |
| 3208 | /* |
| 3209 | * number of |
| 3210 | * Data-Out PDU's within R2T-sequence can be quite big; |
| 3211 | * using mempool |
| 3212 | */ |
| 3213 | ctask->datapool = mempool_create(ISCSI_DTASK_DEFAULT_MAX, |
| 3214 | mempool_alloc_slab, mempool_free_slab, taskcache); |
| 3215 | if (ctask->datapool == NULL) { |
| 3216 | kfifo_free(ctask->r2tqueue); |
| 3217 | iscsi_pool_free(&ctask->r2tpool, (void**)ctask->r2ts); |
| 3218 | goto r2t_alloc_fail; |
| 3219 | } |
| 3220 | INIT_LIST_HEAD(&ctask->dataqueue); |
| 3221 | } |
| 3222 | |
| 3223 | return 0; |
| 3224 | |
| 3225 | r2t_alloc_fail: |
| 3226 | for (i = 0; i < cmd_i; i++) { |
| 3227 | mempool_destroy(session->cmds[i]->datapool); |
| 3228 | kfifo_free(session->cmds[i]->r2tqueue); |
| 3229 | iscsi_pool_free(&session->cmds[i]->r2tpool, |
| 3230 | (void**)session->cmds[i]->r2ts); |
| 3231 | } |
| 3232 | return -ENOMEM; |
| 3233 | } |
| 3234 | |
| 3235 | static void |
| 3236 | iscsi_r2tpool_free(struct iscsi_session *session) |
| 3237 | { |
| 3238 | int i; |
| 3239 | |
| 3240 | for (i = 0; i < session->cmds_max; i++) { |
| 3241 | mempool_destroy(session->cmds[i]->datapool); |
| 3242 | kfifo_free(session->cmds[i]->r2tqueue); |
| 3243 | iscsi_pool_free(&session->cmds[i]->r2tpool, |
| 3244 | (void**)session->cmds[i]->r2ts); |
| 3245 | } |
| 3246 | } |
| 3247 | |
| 3248 | static struct scsi_host_template iscsi_sht = { |
| 3249 | .name = "iSCSI Initiator over TCP/IP, v." |
| 3250 | ISCSI_VERSION_STR, |
| 3251 | .queuecommand = iscsi_queuecommand, |
| 3252 | .can_queue = ISCSI_XMIT_CMDS_MAX - 1, |
| 3253 | .sg_tablesize = ISCSI_SG_TABLESIZE, |
| 3254 | .cmd_per_lun = ISCSI_CMD_PER_LUN, |
| 3255 | .eh_abort_handler = iscsi_eh_abort, |
| 3256 | .eh_host_reset_handler = iscsi_eh_host_reset, |
| 3257 | .use_clustering = DISABLE_CLUSTERING, |
| 3258 | .proc_name = "iscsi_tcp", |
| 3259 | .this_id = -1, |
| 3260 | }; |
| 3261 | |
| 3262 | static iscsi_sessionh_t |
| 3263 | iscsi_session_create(uint32_t initial_cmdsn, struct Scsi_Host *host) |
| 3264 | { |
| 3265 | int cmd_i; |
| 3266 | struct iscsi_session *session; |
| 3267 | |
| 3268 | session = iscsi_hostdata(host->hostdata); |
| 3269 | memset(session, 0, sizeof(struct iscsi_session)); |
| 3270 | |
| 3271 | session->host = host; |
| 3272 | session->id = host->host_no; |
| 3273 | session->state = ISCSI_STATE_LOGGED_IN; |
| 3274 | session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX; |
| 3275 | session->cmds_max = ISCSI_XMIT_CMDS_MAX; |
| 3276 | session->cmdsn = initial_cmdsn; |
| 3277 | session->exp_cmdsn = initial_cmdsn + 1; |
| 3278 | session->max_cmdsn = initial_cmdsn + 1; |
| 3279 | session->max_r2t = 1; |
| 3280 | |
| 3281 | /* initialize SCSI PDU commands pool */ |
| 3282 | if (iscsi_pool_init(&session->cmdpool, session->cmds_max, |
| 3283 | (void***)&session->cmds, sizeof(struct iscsi_cmd_task))) |
| 3284 | goto cmdpool_alloc_fail; |
| 3285 | |
| 3286 | /* pre-format cmds pool with ITT */ |
| 3287 | for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) |
| 3288 | session->cmds[cmd_i]->itt = cmd_i; |
| 3289 | |
| 3290 | spin_lock_init(&session->lock); |
| 3291 | INIT_LIST_HEAD(&session->connections); |
| 3292 | |
| 3293 | /* initialize immediate command pool */ |
| 3294 | if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max, |
| 3295 | (void***)&session->mgmt_cmds, sizeof(struct iscsi_mgmt_task))) |
| 3296 | goto mgmtpool_alloc_fail; |
| 3297 | |
| 3298 | |
| 3299 | /* pre-format immediate cmds pool with ITT */ |
| 3300 | for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) { |
| 3301 | session->mgmt_cmds[cmd_i]->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i; |
| 3302 | session->mgmt_cmds[cmd_i]->data = kmalloc( |
| 3303 | DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH, GFP_KERNEL); |
| 3304 | if (!session->mgmt_cmds[cmd_i]->data) { |
| 3305 | int j; |
| 3306 | |
| 3307 | for (j = 0; j < cmd_i; j++) |
| 3308 | kfree(session->mgmt_cmds[j]->data); |
| 3309 | goto immdata_alloc_fail; |
| 3310 | } |
| 3311 | } |
| 3312 | |
| 3313 | if (iscsi_r2tpool_alloc(session)) |
| 3314 | goto r2tpool_alloc_fail; |
| 3315 | |
| 3316 | return iscsi_handle(session); |
| 3317 | |
| 3318 | r2tpool_alloc_fail: |
| 3319 | for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) |
| 3320 | kfree(session->mgmt_cmds[cmd_i]->data); |
| 3321 | iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds); |
| 3322 | immdata_alloc_fail: |
| 3323 | mgmtpool_alloc_fail: |
| 3324 | iscsi_pool_free(&session->cmdpool, (void**)session->cmds); |
| 3325 | cmdpool_alloc_fail: |
| 3326 | return iscsi_handle(NULL); |
| 3327 | } |
| 3328 | |
| 3329 | static void |
| 3330 | iscsi_session_destroy(iscsi_sessionh_t sessionh) |
| 3331 | { |
| 3332 | int cmd_i; |
| 3333 | struct iscsi_data_task *dtask, *n; |
| 3334 | struct iscsi_session *session = iscsi_ptr(sessionh); |
| 3335 | |
| 3336 | for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) { |
| 3337 | struct iscsi_cmd_task *ctask = session->cmds[cmd_i]; |
| 3338 | list_for_each_entry_safe(dtask, n, &ctask->dataqueue, item) { |
| 3339 | list_del(&dtask->item); |
| 3340 | mempool_free(dtask, ctask->datapool); |
| 3341 | } |
| 3342 | } |
| 3343 | |
| 3344 | for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) |
| 3345 | kfree(session->mgmt_cmds[cmd_i]->data); |
| 3346 | |
| 3347 | iscsi_r2tpool_free(session); |
| 3348 | iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds); |
| 3349 | iscsi_pool_free(&session->cmdpool, (void**)session->cmds); |
| 3350 | } |
| 3351 | |
| 3352 | static int |
| 3353 | iscsi_conn_set_param(iscsi_connh_t connh, enum iscsi_param param, |
| 3354 | uint32_t value) |
| 3355 | { |
| 3356 | struct iscsi_conn *conn = iscsi_ptr(connh); |
| 3357 | struct iscsi_session *session = conn->session; |
| 3358 | |
| 3359 | spin_lock_bh(&session->lock); |
| 3360 | if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE && |
| 3361 | conn->stop_stage != STOP_CONN_RECOVER) { |
| 3362 | printk(KERN_ERR "iscsi_tcp: can not change parameter [%d]\n", |
| 3363 | param); |
| 3364 | spin_unlock_bh(&session->lock); |
| 3365 | return 0; |
| 3366 | } |
| 3367 | spin_unlock_bh(&session->lock); |
| 3368 | |
| 3369 | switch(param) { |
| 3370 | case ISCSI_PARAM_MAX_RECV_DLENGTH: { |
| 3371 | char *saveptr = conn->data; |
| 3372 | int flags = GFP_KERNEL; |
| 3373 | |
| 3374 | if (conn->data_size >= value) { |
| 3375 | conn->max_recv_dlength = value; |
| 3376 | break; |
| 3377 | } |
| 3378 | |
| 3379 | spin_lock_bh(&session->lock); |
| 3380 | if (conn->stop_stage == STOP_CONN_RECOVER) |
| 3381 | flags = GFP_ATOMIC; |
| 3382 | spin_unlock_bh(&session->lock); |
| 3383 | |
| 3384 | if (value <= PAGE_SIZE) |
| 3385 | conn->data = kmalloc(value, flags); |
| 3386 | else |
| 3387 | conn->data = (void*)__get_free_pages(flags, |
| 3388 | get_order(value)); |
| 3389 | if (conn->data == NULL) { |
| 3390 | conn->data = saveptr; |
| 3391 | return -ENOMEM; |
| 3392 | } |
| 3393 | if (conn->data_size <= PAGE_SIZE) |
| 3394 | kfree(saveptr); |
| 3395 | else |
| 3396 | free_pages((unsigned long)saveptr, |
| 3397 | get_order(conn->data_size)); |
| 3398 | conn->max_recv_dlength = value; |
| 3399 | conn->data_size = value; |
| 3400 | } |
| 3401 | break; |
| 3402 | case ISCSI_PARAM_MAX_XMIT_DLENGTH: |
| 3403 | conn->max_xmit_dlength = value; |
| 3404 | break; |
| 3405 | case ISCSI_PARAM_HDRDGST_EN: |
| 3406 | conn->hdrdgst_en = value; |
| 3407 | conn->hdr_size = sizeof(struct iscsi_hdr); |
| 3408 | if (conn->hdrdgst_en) { |
| 3409 | conn->hdr_size += sizeof(__u32); |
| 3410 | if (!conn->tx_tfm) |
| 3411 | conn->tx_tfm = crypto_alloc_tfm("crc32c", 0); |
| 3412 | if (!conn->tx_tfm) |
| 3413 | return -ENOMEM; |
| 3414 | if (!conn->rx_tfm) |
| 3415 | conn->rx_tfm = crypto_alloc_tfm("crc32c", 0); |
| 3416 | if (!conn->rx_tfm) { |
| 3417 | crypto_free_tfm(conn->tx_tfm); |
| 3418 | return -ENOMEM; |
| 3419 | } |
| 3420 | } else { |
| 3421 | if (conn->tx_tfm) |
| 3422 | crypto_free_tfm(conn->tx_tfm); |
| 3423 | if (conn->rx_tfm) |
| 3424 | crypto_free_tfm(conn->rx_tfm); |
| 3425 | } |
| 3426 | break; |
| 3427 | case ISCSI_PARAM_DATADGST_EN: |
| 3428 | conn->datadgst_en = value; |
| 3429 | if (conn->datadgst_en) { |
| 3430 | if (!conn->data_tx_tfm) |
| 3431 | conn->data_tx_tfm = |
| 3432 | crypto_alloc_tfm("crc32c", 0); |
| 3433 | if (!conn->data_tx_tfm) |
| 3434 | return -ENOMEM; |
| 3435 | if (!conn->data_rx_tfm) |
| 3436 | conn->data_rx_tfm = |
| 3437 | crypto_alloc_tfm("crc32c", 0); |
| 3438 | if (!conn->data_rx_tfm) { |
| 3439 | crypto_free_tfm(conn->data_tx_tfm); |
| 3440 | return -ENOMEM; |
| 3441 | } |
| 3442 | } else { |
| 3443 | if (conn->data_tx_tfm) |
| 3444 | crypto_free_tfm(conn->data_tx_tfm); |
| 3445 | if (conn->data_rx_tfm) |
| 3446 | crypto_free_tfm(conn->data_rx_tfm); |
| 3447 | } |
| 3448 | break; |
| 3449 | case ISCSI_PARAM_INITIAL_R2T_EN: |
| 3450 | session->initial_r2t_en = value; |
| 3451 | break; |
| 3452 | case ISCSI_PARAM_MAX_R2T: |
| 3453 | if (session->max_r2t == roundup_pow_of_two(value)) |
| 3454 | break; |
| 3455 | iscsi_r2tpool_free(session); |
| 3456 | session->max_r2t = value; |
| 3457 | if (session->max_r2t & (session->max_r2t - 1)) |
| 3458 | session->max_r2t = roundup_pow_of_two(session->max_r2t); |
| 3459 | if (iscsi_r2tpool_alloc(session)) |
| 3460 | return -ENOMEM; |
| 3461 | break; |
| 3462 | case ISCSI_PARAM_IMM_DATA_EN: |
| 3463 | session->imm_data_en = value; |
| 3464 | break; |
| 3465 | case ISCSI_PARAM_FIRST_BURST: |
| 3466 | session->first_burst = value; |
| 3467 | break; |
| 3468 | case ISCSI_PARAM_MAX_BURST: |
| 3469 | session->max_burst = value; |
| 3470 | break; |
| 3471 | case ISCSI_PARAM_PDU_INORDER_EN: |
| 3472 | session->pdu_inorder_en = value; |
| 3473 | break; |
| 3474 | case ISCSI_PARAM_DATASEQ_INORDER_EN: |
| 3475 | session->dataseq_inorder_en = value; |
| 3476 | break; |
| 3477 | case ISCSI_PARAM_ERL: |
| 3478 | session->erl = value; |
| 3479 | break; |
| 3480 | case ISCSI_PARAM_IFMARKER_EN: |
| 3481 | BUG_ON(value); |
| 3482 | session->ifmarker_en = value; |
| 3483 | break; |
| 3484 | case ISCSI_PARAM_OFMARKER_EN: |
| 3485 | BUG_ON(value); |
| 3486 | session->ofmarker_en = value; |
| 3487 | break; |
| 3488 | default: |
| 3489 | break; |
| 3490 | } |
| 3491 | |
| 3492 | return 0; |
| 3493 | } |
| 3494 | |
| 3495 | static int |
| 3496 | iscsi_conn_get_param(iscsi_connh_t connh, enum iscsi_param param, |
| 3497 | uint32_t *value) |
| 3498 | { |
| 3499 | struct iscsi_conn *conn = iscsi_ptr(connh); |
| 3500 | struct iscsi_session *session = conn->session; |
| 3501 | |
| 3502 | switch(param) { |
| 3503 | case ISCSI_PARAM_MAX_RECV_DLENGTH: |
| 3504 | *value = conn->max_recv_dlength; |
| 3505 | break; |
| 3506 | case ISCSI_PARAM_MAX_XMIT_DLENGTH: |
| 3507 | *value = conn->max_xmit_dlength; |
| 3508 | break; |
| 3509 | case ISCSI_PARAM_HDRDGST_EN: |
| 3510 | *value = conn->hdrdgst_en; |
| 3511 | break; |
| 3512 | case ISCSI_PARAM_DATADGST_EN: |
| 3513 | *value = conn->datadgst_en; |
| 3514 | break; |
| 3515 | case ISCSI_PARAM_INITIAL_R2T_EN: |
| 3516 | *value = session->initial_r2t_en; |
| 3517 | break; |
| 3518 | case ISCSI_PARAM_MAX_R2T: |
| 3519 | *value = session->max_r2t; |
| 3520 | break; |
| 3521 | case ISCSI_PARAM_IMM_DATA_EN: |
| 3522 | *value = session->imm_data_en; |
| 3523 | break; |
| 3524 | case ISCSI_PARAM_FIRST_BURST: |
| 3525 | *value = session->first_burst; |
| 3526 | break; |
| 3527 | case ISCSI_PARAM_MAX_BURST: |
| 3528 | *value = session->max_burst; |
| 3529 | break; |
| 3530 | case ISCSI_PARAM_PDU_INORDER_EN: |
| 3531 | *value = session->pdu_inorder_en; |
| 3532 | break; |
| 3533 | case ISCSI_PARAM_DATASEQ_INORDER_EN: |
| 3534 | *value = session->dataseq_inorder_en; |
| 3535 | break; |
| 3536 | case ISCSI_PARAM_ERL: |
| 3537 | *value = session->erl; |
| 3538 | break; |
| 3539 | case ISCSI_PARAM_IFMARKER_EN: |
| 3540 | *value = session->ifmarker_en; |
| 3541 | break; |
| 3542 | case ISCSI_PARAM_OFMARKER_EN: |
| 3543 | *value = session->ofmarker_en; |
| 3544 | break; |
| 3545 | default: |
| 3546 | return ISCSI_ERR_PARAM_NOT_FOUND; |
| 3547 | } |
| 3548 | |
| 3549 | return 0; |
| 3550 | } |
| 3551 | |
| 3552 | static void |
| 3553 | iscsi_conn_get_stats(iscsi_connh_t connh, struct iscsi_stats *stats) |
| 3554 | { |
| 3555 | struct iscsi_conn *conn = iscsi_ptr(connh); |
| 3556 | |
| 3557 | stats->txdata_octets = conn->txdata_octets; |
| 3558 | stats->rxdata_octets = conn->rxdata_octets; |
| 3559 | stats->scsicmd_pdus = conn->scsicmd_pdus_cnt; |
| 3560 | stats->dataout_pdus = conn->dataout_pdus_cnt; |
| 3561 | stats->scsirsp_pdus = conn->scsirsp_pdus_cnt; |
| 3562 | stats->datain_pdus = conn->datain_pdus_cnt; |
| 3563 | stats->r2t_pdus = conn->r2t_pdus_cnt; |
| 3564 | stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt; |
| 3565 | stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt; |
| 3566 | stats->custom_length = 3; |
| 3567 | strcpy(stats->custom[0].desc, "tx_sendpage_failures"); |
| 3568 | stats->custom[0].value = conn->sendpage_failures_cnt; |
| 3569 | strcpy(stats->custom[1].desc, "rx_discontiguous_hdr"); |
| 3570 | stats->custom[1].value = conn->discontiguous_hdr_cnt; |
| 3571 | strcpy(stats->custom[2].desc, "eh_abort_cnt"); |
| 3572 | stats->custom[2].value = conn->eh_abort_cnt; |
| 3573 | } |
| 3574 | |
| 3575 | static int |
| 3576 | iscsi_conn_send_pdu(iscsi_connh_t connh, struct iscsi_hdr *hdr, char *data, |
| 3577 | uint32_t data_size) |
| 3578 | { |
| 3579 | struct iscsi_conn *conn = iscsi_ptr(connh); |
| 3580 | int rc; |
| 3581 | |
| 3582 | down(&conn->xmitsema); |
| 3583 | rc = iscsi_conn_send_generic(conn, hdr, data, data_size); |
| 3584 | up(&conn->xmitsema); |
| 3585 | |
| 3586 | return rc; |
| 3587 | } |
| 3588 | |
| 3589 | static struct iscsi_transport iscsi_tcp_transport = { |
| 3590 | .owner = THIS_MODULE, |
| 3591 | .name = "tcp", |
| 3592 | .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST |
| 3593 | | CAP_DATADGST, |
| 3594 | .host_template = &iscsi_sht, |
| 3595 | .hostdata_size = sizeof(struct iscsi_session), |
| 3596 | .max_conn = 1, |
| 3597 | .max_cmd_len = ISCSI_TCP_MAX_CMD_LEN, |
| 3598 | .create_session = iscsi_session_create, |
| 3599 | .destroy_session = iscsi_session_destroy, |
| 3600 | .create_conn = iscsi_conn_create, |
| 3601 | .bind_conn = iscsi_conn_bind, |
| 3602 | .destroy_conn = iscsi_conn_destroy, |
| 3603 | .set_param = iscsi_conn_set_param, |
| 3604 | .get_param = iscsi_conn_get_param, |
| 3605 | .start_conn = iscsi_conn_start, |
| 3606 | .stop_conn = iscsi_conn_stop, |
| 3607 | .send_pdu = iscsi_conn_send_pdu, |
| 3608 | .get_stats = iscsi_conn_get_stats, |
| 3609 | }; |
| 3610 | |
| 3611 | static int __init |
| 3612 | iscsi_tcp_init(void) |
| 3613 | { |
| 3614 | int error; |
| 3615 | |
| 3616 | if (iscsi_max_lun < 1) { |
| 3617 | printk(KERN_ERR "Invalid max_lun value of %u\n", iscsi_max_lun); |
| 3618 | return -EINVAL; |
| 3619 | } |
| 3620 | iscsi_tcp_transport.max_lun = iscsi_max_lun; |
| 3621 | |
| 3622 | taskcache = kmem_cache_create("iscsi_taskcache", |
| 3623 | sizeof(struct iscsi_data_task), 0, |
| 3624 | SLAB_HWCACHE_ALIGN | SLAB_NO_REAP, NULL, NULL); |
| 3625 | if (!taskcache) |
| 3626 | return -ENOMEM; |
| 3627 | |
| 3628 | error = iscsi_register_transport(&iscsi_tcp_transport); |
| 3629 | if (error) |
| 3630 | kmem_cache_destroy(taskcache); |
| 3631 | |
| 3632 | return error; |
| 3633 | } |
| 3634 | |
| 3635 | static void __exit |
| 3636 | iscsi_tcp_exit(void) |
| 3637 | { |
| 3638 | iscsi_unregister_transport(&iscsi_tcp_transport); |
| 3639 | kmem_cache_destroy(taskcache); |
| 3640 | } |
| 3641 | |
| 3642 | module_init(iscsi_tcp_init); |
| 3643 | module_exit(iscsi_tcp_exit); |