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