1 /* 2 * iSCSI lib functions 3 * 4 * Copyright (C) 2006 Red Hat, Inc. All rights reserved. 5 * Copyright (C) 2004 - 2006 Mike Christie 6 * Copyright (C) 2004 - 2005 Dmitry Yusupov 7 * Copyright (C) 2004 - 2005 Alex Aizman 8 * maintained by open-iscsi@googlegroups.com 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation; either version 2 of the License, or 13 * (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program; if not, write to the Free Software 22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 23 */ 24 #include <linux/types.h> 25 #include <linux/kfifo.h> 26 #include <linux/delay.h> 27 #include <linux/log2.h> 28 #include <linux/slab.h> 29 #include <linux/sched/signal.h> 30 #include <linux/module.h> 31 #include <asm/unaligned.h> 32 #include <net/tcp.h> 33 #include <scsi/scsi_cmnd.h> 34 #include <scsi/scsi_device.h> 35 #include <scsi/scsi_eh.h> 36 #include <scsi/scsi_tcq.h> 37 #include <scsi/scsi_host.h> 38 #include <scsi/scsi.h> 39 #include <scsi/iscsi_proto.h> 40 #include <scsi/scsi_transport.h> 41 #include <scsi/scsi_transport_iscsi.h> 42 #include <scsi/libiscsi.h> 43 #include <trace/events/iscsi.h> 44 45 static int iscsi_dbg_lib_conn; 46 module_param_named(debug_libiscsi_conn, iscsi_dbg_lib_conn, int, 47 S_IRUGO | S_IWUSR); 48 MODULE_PARM_DESC(debug_libiscsi_conn, 49 "Turn on debugging for connections in libiscsi module. " 50 "Set to 1 to turn on, and zero to turn off. Default is off."); 51 52 static int iscsi_dbg_lib_session; 53 module_param_named(debug_libiscsi_session, iscsi_dbg_lib_session, int, 54 S_IRUGO | S_IWUSR); 55 MODULE_PARM_DESC(debug_libiscsi_session, 56 "Turn on debugging for sessions in libiscsi module. " 57 "Set to 1 to turn on, and zero to turn off. Default is off."); 58 59 static int iscsi_dbg_lib_eh; 60 module_param_named(debug_libiscsi_eh, iscsi_dbg_lib_eh, int, 61 S_IRUGO | S_IWUSR); 62 MODULE_PARM_DESC(debug_libiscsi_eh, 63 "Turn on debugging for error handling in libiscsi module. " 64 "Set to 1 to turn on, and zero to turn off. Default is off."); 65 66 #define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \ 67 do { \ 68 if (iscsi_dbg_lib_conn) \ 69 iscsi_conn_printk(KERN_INFO, _conn, \ 70 "%s " dbg_fmt, \ 71 __func__, ##arg); \ 72 iscsi_dbg_trace(trace_iscsi_dbg_conn, \ 73 &(_conn)->cls_conn->dev, \ 74 "%s " dbg_fmt, __func__, ##arg);\ 75 } while (0); 76 77 #define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...) \ 78 do { \ 79 if (iscsi_dbg_lib_session) \ 80 iscsi_session_printk(KERN_INFO, _session, \ 81 "%s " dbg_fmt, \ 82 __func__, ##arg); \ 83 iscsi_dbg_trace(trace_iscsi_dbg_session, \ 84 &(_session)->cls_session->dev, \ 85 "%s " dbg_fmt, __func__, ##arg); \ 86 } while (0); 87 88 #define ISCSI_DBG_EH(_session, dbg_fmt, arg...) \ 89 do { \ 90 if (iscsi_dbg_lib_eh) \ 91 iscsi_session_printk(KERN_INFO, _session, \ 92 "%s " dbg_fmt, \ 93 __func__, ##arg); \ 94 iscsi_dbg_trace(trace_iscsi_dbg_eh, \ 95 &(_session)->cls_session->dev, \ 96 "%s " dbg_fmt, __func__, ##arg); \ 97 } while (0); 98 99 inline void iscsi_conn_queue_work(struct iscsi_conn *conn) 100 { 101 struct Scsi_Host *shost = conn->session->host; 102 struct iscsi_host *ihost = shost_priv(shost); 103 104 if (ihost->workq) 105 queue_work(ihost->workq, &conn->xmitwork); 106 } 107 EXPORT_SYMBOL_GPL(iscsi_conn_queue_work); 108 109 static void __iscsi_update_cmdsn(struct iscsi_session *session, 110 uint32_t exp_cmdsn, uint32_t max_cmdsn) 111 { 112 /* 113 * standard specifies this check for when to update expected and 114 * max sequence numbers 115 */ 116 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1)) 117 return; 118 119 if (exp_cmdsn != session->exp_cmdsn && 120 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn)) 121 session->exp_cmdsn = exp_cmdsn; 122 123 if (max_cmdsn != session->max_cmdsn && 124 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) 125 session->max_cmdsn = max_cmdsn; 126 } 127 128 void iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr) 129 { 130 __iscsi_update_cmdsn(session, be32_to_cpu(hdr->exp_cmdsn), 131 be32_to_cpu(hdr->max_cmdsn)); 132 } 133 EXPORT_SYMBOL_GPL(iscsi_update_cmdsn); 134 135 /** 136 * iscsi_prep_data_out_pdu - initialize Data-Out 137 * @task: scsi command task 138 * @r2t: R2T info 139 * @hdr: iscsi data in pdu 140 * 141 * Notes: 142 * Initialize Data-Out within this R2T sequence and finds 143 * proper data_offset within this SCSI command. 144 * 145 * This function is called with connection lock taken. 146 **/ 147 void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t, 148 struct iscsi_data *hdr) 149 { 150 struct iscsi_conn *conn = task->conn; 151 unsigned int left = r2t->data_length - r2t->sent; 152 153 task->hdr_len = sizeof(struct iscsi_data); 154 155 memset(hdr, 0, sizeof(struct iscsi_data)); 156 hdr->ttt = r2t->ttt; 157 hdr->datasn = cpu_to_be32(r2t->datasn); 158 r2t->datasn++; 159 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT; 160 hdr->lun = task->lun; 161 hdr->itt = task->hdr_itt; 162 hdr->exp_statsn = r2t->exp_statsn; 163 hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent); 164 if (left > conn->max_xmit_dlength) { 165 hton24(hdr->dlength, conn->max_xmit_dlength); 166 r2t->data_count = conn->max_xmit_dlength; 167 hdr->flags = 0; 168 } else { 169 hton24(hdr->dlength, left); 170 r2t->data_count = left; 171 hdr->flags = ISCSI_FLAG_CMD_FINAL; 172 } 173 conn->dataout_pdus_cnt++; 174 } 175 EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu); 176 177 static int iscsi_add_hdr(struct iscsi_task *task, unsigned len) 178 { 179 unsigned exp_len = task->hdr_len + len; 180 181 if (exp_len > task->hdr_max) { 182 WARN_ON(1); 183 return -EINVAL; 184 } 185 186 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */ 187 task->hdr_len = exp_len; 188 return 0; 189 } 190 191 /* 192 * make an extended cdb AHS 193 */ 194 static int iscsi_prep_ecdb_ahs(struct iscsi_task *task) 195 { 196 struct scsi_cmnd *cmd = task->sc; 197 unsigned rlen, pad_len; 198 unsigned short ahslength; 199 struct iscsi_ecdb_ahdr *ecdb_ahdr; 200 int rc; 201 202 ecdb_ahdr = iscsi_next_hdr(task); 203 rlen = cmd->cmd_len - ISCSI_CDB_SIZE; 204 205 BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb)); 206 ahslength = rlen + sizeof(ecdb_ahdr->reserved); 207 208 pad_len = iscsi_padding(rlen); 209 210 rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) + 211 sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len); 212 if (rc) 213 return rc; 214 215 if (pad_len) 216 memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len); 217 218 ecdb_ahdr->ahslength = cpu_to_be16(ahslength); 219 ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB; 220 ecdb_ahdr->reserved = 0; 221 memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen); 222 223 ISCSI_DBG_SESSION(task->conn->session, 224 "iscsi_prep_ecdb_ahs: varlen_cdb_len %d " 225 "rlen %d pad_len %d ahs_length %d iscsi_headers_size " 226 "%u\n", cmd->cmd_len, rlen, pad_len, ahslength, 227 task->hdr_len); 228 return 0; 229 } 230 231 /** 232 * iscsi_check_tmf_restrictions - check if a task is affected by TMF 233 * @task: iscsi task 234 * @opcode: opcode to check for 235 * 236 * During TMF a task has to be checked if it's affected. 237 * All unrelated I/O can be passed through, but I/O to the 238 * affected LUN should be restricted. 239 * If 'fast_abort' is set we won't be sending any I/O to the 240 * affected LUN. 241 * Otherwise the target is waiting for all TTTs to be completed, 242 * so we have to send all outstanding Data-Out PDUs to the target. 243 */ 244 static int iscsi_check_tmf_restrictions(struct iscsi_task *task, int opcode) 245 { 246 struct iscsi_conn *conn = task->conn; 247 struct iscsi_tm *tmf = &conn->tmhdr; 248 u64 hdr_lun; 249 250 if (conn->tmf_state == TMF_INITIAL) 251 return 0; 252 253 if ((tmf->opcode & ISCSI_OPCODE_MASK) != ISCSI_OP_SCSI_TMFUNC) 254 return 0; 255 256 switch (ISCSI_TM_FUNC_VALUE(tmf)) { 257 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET: 258 /* 259 * Allow PDUs for unrelated LUNs 260 */ 261 hdr_lun = scsilun_to_int(&tmf->lun); 262 if (hdr_lun != task->sc->device->lun) 263 return 0; 264 /* fall through */ 265 case ISCSI_TM_FUNC_TARGET_WARM_RESET: 266 /* 267 * Fail all SCSI cmd PDUs 268 */ 269 if (opcode != ISCSI_OP_SCSI_DATA_OUT) { 270 iscsi_conn_printk(KERN_INFO, conn, 271 "task [op %x itt " 272 "0x%x/0x%x] " 273 "rejected.\n", 274 opcode, task->itt, 275 task->hdr_itt); 276 return -EACCES; 277 } 278 /* 279 * And also all data-out PDUs in response to R2T 280 * if fast_abort is set. 281 */ 282 if (conn->session->fast_abort) { 283 iscsi_conn_printk(KERN_INFO, conn, 284 "task [op %x itt " 285 "0x%x/0x%x] fast abort.\n", 286 opcode, task->itt, 287 task->hdr_itt); 288 return -EACCES; 289 } 290 break; 291 case ISCSI_TM_FUNC_ABORT_TASK: 292 /* 293 * the caller has already checked if the task 294 * they want to abort was in the pending queue so if 295 * we are here the cmd pdu has gone out already, and 296 * we will only hit this for data-outs 297 */ 298 if (opcode == ISCSI_OP_SCSI_DATA_OUT && 299 task->hdr_itt == tmf->rtt) { 300 ISCSI_DBG_SESSION(conn->session, 301 "Preventing task %x/%x from sending " 302 "data-out due to abort task in " 303 "progress\n", task->itt, 304 task->hdr_itt); 305 return -EACCES; 306 } 307 break; 308 } 309 310 return 0; 311 } 312 313 /** 314 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu 315 * @task: iscsi task 316 * 317 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set 318 * fields like dlength or final based on how much data it sends 319 */ 320 static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task) 321 { 322 struct iscsi_conn *conn = task->conn; 323 struct iscsi_session *session = conn->session; 324 struct scsi_cmnd *sc = task->sc; 325 struct iscsi_scsi_req *hdr; 326 unsigned hdrlength, cmd_len, transfer_length; 327 itt_t itt; 328 int rc; 329 330 rc = iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_CMD); 331 if (rc) 332 return rc; 333 334 if (conn->session->tt->alloc_pdu) { 335 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD); 336 if (rc) 337 return rc; 338 } 339 hdr = (struct iscsi_scsi_req *)task->hdr; 340 itt = hdr->itt; 341 memset(hdr, 0, sizeof(*hdr)); 342 343 if (session->tt->parse_pdu_itt) 344 hdr->itt = task->hdr_itt = itt; 345 else 346 hdr->itt = task->hdr_itt = build_itt(task->itt, 347 task->conn->session->age); 348 task->hdr_len = 0; 349 rc = iscsi_add_hdr(task, sizeof(*hdr)); 350 if (rc) 351 return rc; 352 hdr->opcode = ISCSI_OP_SCSI_CMD; 353 hdr->flags = ISCSI_ATTR_SIMPLE; 354 int_to_scsilun(sc->device->lun, &hdr->lun); 355 task->lun = hdr->lun; 356 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn); 357 cmd_len = sc->cmd_len; 358 if (cmd_len < ISCSI_CDB_SIZE) 359 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len); 360 else if (cmd_len > ISCSI_CDB_SIZE) { 361 rc = iscsi_prep_ecdb_ahs(task); 362 if (rc) 363 return rc; 364 cmd_len = ISCSI_CDB_SIZE; 365 } 366 memcpy(hdr->cdb, sc->cmnd, cmd_len); 367 368 task->imm_count = 0; 369 if (scsi_get_prot_op(sc) != SCSI_PROT_NORMAL) 370 task->protected = true; 371 372 transfer_length = scsi_transfer_length(sc); 373 hdr->data_length = cpu_to_be32(transfer_length); 374 if (sc->sc_data_direction == DMA_TO_DEVICE) { 375 struct iscsi_r2t_info *r2t = &task->unsol_r2t; 376 377 hdr->flags |= ISCSI_FLAG_CMD_WRITE; 378 /* 379 * Write counters: 380 * 381 * imm_count bytes to be sent right after 382 * SCSI PDU Header 383 * 384 * unsol_count bytes(as Data-Out) to be sent 385 * without R2T ack right after 386 * immediate data 387 * 388 * r2t data_length bytes to be sent via R2T ack's 389 * 390 * pad_count bytes to be sent as zero-padding 391 */ 392 memset(r2t, 0, sizeof(*r2t)); 393 394 if (session->imm_data_en) { 395 if (transfer_length >= session->first_burst) 396 task->imm_count = min(session->first_burst, 397 conn->max_xmit_dlength); 398 else 399 task->imm_count = min(transfer_length, 400 conn->max_xmit_dlength); 401 hton24(hdr->dlength, task->imm_count); 402 } else 403 zero_data(hdr->dlength); 404 405 if (!session->initial_r2t_en) { 406 r2t->data_length = min(session->first_burst, 407 transfer_length) - 408 task->imm_count; 409 r2t->data_offset = task->imm_count; 410 r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG); 411 r2t->exp_statsn = cpu_to_be32(conn->exp_statsn); 412 } 413 414 if (!task->unsol_r2t.data_length) 415 /* No unsolicit Data-Out's */ 416 hdr->flags |= ISCSI_FLAG_CMD_FINAL; 417 } else { 418 hdr->flags |= ISCSI_FLAG_CMD_FINAL; 419 zero_data(hdr->dlength); 420 421 if (sc->sc_data_direction == DMA_FROM_DEVICE) 422 hdr->flags |= ISCSI_FLAG_CMD_READ; 423 } 424 425 /* calculate size of additional header segments (AHSs) */ 426 hdrlength = task->hdr_len - sizeof(*hdr); 427 428 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1)); 429 hdrlength /= ISCSI_PAD_LEN; 430 431 WARN_ON(hdrlength >= 256); 432 hdr->hlength = hdrlength & 0xFF; 433 hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn); 434 435 if (session->tt->init_task && session->tt->init_task(task)) 436 return -EIO; 437 438 task->state = ISCSI_TASK_RUNNING; 439 session->cmdsn++; 440 441 conn->scsicmd_pdus_cnt++; 442 ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x " 443 "itt 0x%x len %d cmdsn %d win %d]\n", 444 sc->sc_data_direction == DMA_TO_DEVICE ? 445 "write" : "read", conn->id, sc, sc->cmnd[0], 446 task->itt, transfer_length, 447 session->cmdsn, 448 session->max_cmdsn - session->exp_cmdsn + 1); 449 return 0; 450 } 451 452 /** 453 * iscsi_free_task - free a task 454 * @task: iscsi cmd task 455 * 456 * Must be called with session back_lock. 457 * This function returns the scsi command to scsi-ml or cleans 458 * up mgmt tasks then returns the task to the pool. 459 */ 460 static void iscsi_free_task(struct iscsi_task *task) 461 { 462 struct iscsi_conn *conn = task->conn; 463 struct iscsi_session *session = conn->session; 464 struct scsi_cmnd *sc = task->sc; 465 int oldstate = task->state; 466 467 ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n", 468 task->itt, task->state, task->sc); 469 470 session->tt->cleanup_task(task); 471 task->state = ISCSI_TASK_FREE; 472 task->sc = NULL; 473 /* 474 * login task is preallocated so do not free 475 */ 476 if (conn->login_task == task) 477 return; 478 479 kfifo_in(&session->cmdpool.queue, (void*)&task, sizeof(void*)); 480 481 if (sc) { 482 /* SCSI eh reuses commands to verify us */ 483 sc->SCp.ptr = NULL; 484 /* 485 * queue command may call this to free the task, so 486 * it will decide how to return sc to scsi-ml. 487 */ 488 if (oldstate != ISCSI_TASK_REQUEUE_SCSIQ) 489 sc->scsi_done(sc); 490 } 491 } 492 493 void __iscsi_get_task(struct iscsi_task *task) 494 { 495 refcount_inc(&task->refcount); 496 } 497 EXPORT_SYMBOL_GPL(__iscsi_get_task); 498 499 void __iscsi_put_task(struct iscsi_task *task) 500 { 501 if (refcount_dec_and_test(&task->refcount)) 502 iscsi_free_task(task); 503 } 504 EXPORT_SYMBOL_GPL(__iscsi_put_task); 505 506 void iscsi_put_task(struct iscsi_task *task) 507 { 508 struct iscsi_session *session = task->conn->session; 509 510 /* regular RX path uses back_lock */ 511 spin_lock_bh(&session->back_lock); 512 __iscsi_put_task(task); 513 spin_unlock_bh(&session->back_lock); 514 } 515 EXPORT_SYMBOL_GPL(iscsi_put_task); 516 517 /** 518 * iscsi_complete_task - finish a task 519 * @task: iscsi cmd task 520 * @state: state to complete task with 521 * 522 * Must be called with session back_lock. 523 */ 524 static void iscsi_complete_task(struct iscsi_task *task, int state) 525 { 526 struct iscsi_conn *conn = task->conn; 527 528 ISCSI_DBG_SESSION(conn->session, 529 "complete task itt 0x%x state %d sc %p\n", 530 task->itt, task->state, task->sc); 531 if (task->state == ISCSI_TASK_COMPLETED || 532 task->state == ISCSI_TASK_ABRT_TMF || 533 task->state == ISCSI_TASK_ABRT_SESS_RECOV || 534 task->state == ISCSI_TASK_REQUEUE_SCSIQ) 535 return; 536 WARN_ON_ONCE(task->state == ISCSI_TASK_FREE); 537 task->state = state; 538 539 spin_lock_bh(&conn->taskqueuelock); 540 if (!list_empty(&task->running)) { 541 pr_debug_once("%s while task on list", __func__); 542 list_del_init(&task->running); 543 } 544 spin_unlock_bh(&conn->taskqueuelock); 545 546 if (conn->task == task) 547 conn->task = NULL; 548 549 if (conn->ping_task == task) 550 conn->ping_task = NULL; 551 552 /* release get from queueing */ 553 __iscsi_put_task(task); 554 } 555 556 /** 557 * iscsi_complete_scsi_task - finish scsi task normally 558 * @task: iscsi task for scsi cmd 559 * @exp_cmdsn: expected cmd sn in cpu format 560 * @max_cmdsn: max cmd sn in cpu format 561 * 562 * This is used when drivers do not need or cannot perform 563 * lower level pdu processing. 564 * 565 * Called with session back_lock 566 */ 567 void iscsi_complete_scsi_task(struct iscsi_task *task, 568 uint32_t exp_cmdsn, uint32_t max_cmdsn) 569 { 570 struct iscsi_conn *conn = task->conn; 571 572 ISCSI_DBG_SESSION(conn->session, "[itt 0x%x]\n", task->itt); 573 574 conn->last_recv = jiffies; 575 __iscsi_update_cmdsn(conn->session, exp_cmdsn, max_cmdsn); 576 iscsi_complete_task(task, ISCSI_TASK_COMPLETED); 577 } 578 EXPORT_SYMBOL_GPL(iscsi_complete_scsi_task); 579 580 581 /* 582 * session back_lock must be held and if not called for a task that is 583 * still pending or from the xmit thread, then xmit thread must 584 * be suspended. 585 */ 586 static void fail_scsi_task(struct iscsi_task *task, int err) 587 { 588 struct iscsi_conn *conn = task->conn; 589 struct scsi_cmnd *sc; 590 int state; 591 592 /* 593 * if a command completes and we get a successful tmf response 594 * we will hit this because the scsi eh abort code does not take 595 * a ref to the task. 596 */ 597 sc = task->sc; 598 if (!sc) 599 return; 600 601 if (task->state == ISCSI_TASK_PENDING) { 602 /* 603 * cmd never made it to the xmit thread, so we should not count 604 * the cmd in the sequencing 605 */ 606 conn->session->queued_cmdsn--; 607 /* it was never sent so just complete like normal */ 608 state = ISCSI_TASK_COMPLETED; 609 } else if (err == DID_TRANSPORT_DISRUPTED) 610 state = ISCSI_TASK_ABRT_SESS_RECOV; 611 else 612 state = ISCSI_TASK_ABRT_TMF; 613 614 sc->result = err << 16; 615 scsi_set_resid(sc, scsi_bufflen(sc)); 616 617 /* regular RX path uses back_lock */ 618 spin_lock_bh(&conn->session->back_lock); 619 iscsi_complete_task(task, state); 620 spin_unlock_bh(&conn->session->back_lock); 621 } 622 623 static int iscsi_prep_mgmt_task(struct iscsi_conn *conn, 624 struct iscsi_task *task) 625 { 626 struct iscsi_session *session = conn->session; 627 struct iscsi_hdr *hdr = task->hdr; 628 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr; 629 uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK; 630 631 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) 632 return -ENOTCONN; 633 634 if (opcode != ISCSI_OP_LOGIN && opcode != ISCSI_OP_TEXT) 635 nop->exp_statsn = cpu_to_be32(conn->exp_statsn); 636 /* 637 * pre-format CmdSN for outgoing PDU. 638 */ 639 nop->cmdsn = cpu_to_be32(session->cmdsn); 640 if (hdr->itt != RESERVED_ITT) { 641 /* 642 * TODO: We always use immediate for normal session pdus. 643 * If we start to send tmfs or nops as non-immediate then 644 * we should start checking the cmdsn numbers for mgmt tasks. 645 * 646 * During discovery sessions iscsid sends TEXT as non immediate, 647 * but we always only send one PDU at a time. 648 */ 649 if (conn->c_stage == ISCSI_CONN_STARTED && 650 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) { 651 session->queued_cmdsn++; 652 session->cmdsn++; 653 } 654 } 655 656 if (session->tt->init_task && session->tt->init_task(task)) 657 return -EIO; 658 659 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT) 660 session->state = ISCSI_STATE_LOGGING_OUT; 661 662 task->state = ISCSI_TASK_RUNNING; 663 ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x " 664 "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK, 665 hdr->itt, task->data_count); 666 return 0; 667 } 668 669 static struct iscsi_task * 670 __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 671 char *data, uint32_t data_size) 672 { 673 struct iscsi_session *session = conn->session; 674 struct iscsi_host *ihost = shost_priv(session->host); 675 uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK; 676 struct iscsi_task *task; 677 itt_t itt; 678 679 if (session->state == ISCSI_STATE_TERMINATE) 680 return NULL; 681 682 if (opcode == ISCSI_OP_LOGIN || opcode == ISCSI_OP_TEXT) { 683 /* 684 * Login and Text are sent serially, in 685 * request-followed-by-response sequence. 686 * Same task can be used. Same ITT must be used. 687 * Note that login_task is preallocated at conn_create(). 688 */ 689 if (conn->login_task->state != ISCSI_TASK_FREE) { 690 iscsi_conn_printk(KERN_ERR, conn, "Login/Text in " 691 "progress. Cannot start new task.\n"); 692 return NULL; 693 } 694 695 if (data_size > ISCSI_DEF_MAX_RECV_SEG_LEN) { 696 iscsi_conn_printk(KERN_ERR, conn, "Invalid buffer len of %u for login task. Max len is %u\n", data_size, ISCSI_DEF_MAX_RECV_SEG_LEN); 697 return NULL; 698 } 699 700 task = conn->login_task; 701 } else { 702 if (session->state != ISCSI_STATE_LOGGED_IN) 703 return NULL; 704 705 if (data_size != 0) { 706 iscsi_conn_printk(KERN_ERR, conn, "Can not send data buffer of len %u for op 0x%x\n", data_size, opcode); 707 return NULL; 708 } 709 710 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE); 711 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED); 712 713 if (!kfifo_out(&session->cmdpool.queue, 714 (void*)&task, sizeof(void*))) 715 return NULL; 716 } 717 /* 718 * released in complete pdu for task we expect a response for, and 719 * released by the lld when it has transmitted the task for 720 * pdus we do not expect a response for. 721 */ 722 refcount_set(&task->refcount, 1); 723 task->conn = conn; 724 task->sc = NULL; 725 INIT_LIST_HEAD(&task->running); 726 task->state = ISCSI_TASK_PENDING; 727 728 if (data_size) { 729 memcpy(task->data, data, data_size); 730 task->data_count = data_size; 731 } else 732 task->data_count = 0; 733 734 if (conn->session->tt->alloc_pdu) { 735 if (conn->session->tt->alloc_pdu(task, hdr->opcode)) { 736 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate " 737 "pdu for mgmt task.\n"); 738 goto free_task; 739 } 740 } 741 742 itt = task->hdr->itt; 743 task->hdr_len = sizeof(struct iscsi_hdr); 744 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr)); 745 746 if (hdr->itt != RESERVED_ITT) { 747 if (session->tt->parse_pdu_itt) 748 task->hdr->itt = itt; 749 else 750 task->hdr->itt = build_itt(task->itt, 751 task->conn->session->age); 752 } 753 754 if (!ihost->workq) { 755 if (iscsi_prep_mgmt_task(conn, task)) 756 goto free_task; 757 758 if (session->tt->xmit_task(task)) 759 goto free_task; 760 } else { 761 spin_lock_bh(&conn->taskqueuelock); 762 list_add_tail(&task->running, &conn->mgmtqueue); 763 spin_unlock_bh(&conn->taskqueuelock); 764 iscsi_conn_queue_work(conn); 765 } 766 767 return task; 768 769 free_task: 770 /* regular RX path uses back_lock */ 771 spin_lock(&session->back_lock); 772 __iscsi_put_task(task); 773 spin_unlock(&session->back_lock); 774 return NULL; 775 } 776 777 int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr, 778 char *data, uint32_t data_size) 779 { 780 struct iscsi_conn *conn = cls_conn->dd_data; 781 struct iscsi_session *session = conn->session; 782 int err = 0; 783 784 spin_lock_bh(&session->frwd_lock); 785 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size)) 786 err = -EPERM; 787 spin_unlock_bh(&session->frwd_lock); 788 return err; 789 } 790 EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu); 791 792 /** 793 * iscsi_cmd_rsp - SCSI Command Response processing 794 * @conn: iscsi connection 795 * @hdr: iscsi header 796 * @task: scsi command task 797 * @data: cmd data buffer 798 * @datalen: len of buffer 799 * 800 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and 801 * then completes the command and task. 802 **/ 803 static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 804 struct iscsi_task *task, char *data, 805 int datalen) 806 { 807 struct iscsi_scsi_rsp *rhdr = (struct iscsi_scsi_rsp *)hdr; 808 struct iscsi_session *session = conn->session; 809 struct scsi_cmnd *sc = task->sc; 810 811 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr); 812 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1; 813 814 sc->result = (DID_OK << 16) | rhdr->cmd_status; 815 816 if (task->protected) { 817 sector_t sector; 818 u8 ascq; 819 820 /** 821 * Transports that didn't implement check_protection 822 * callback but still published T10-PI support to scsi-mid 823 * deserve this BUG_ON. 824 **/ 825 BUG_ON(!session->tt->check_protection); 826 827 ascq = session->tt->check_protection(task, §or); 828 if (ascq) { 829 sc->result = DRIVER_SENSE << 24 | 830 SAM_STAT_CHECK_CONDITION; 831 scsi_build_sense_buffer(1, sc->sense_buffer, 832 ILLEGAL_REQUEST, 0x10, ascq); 833 scsi_set_sense_information(sc->sense_buffer, 834 SCSI_SENSE_BUFFERSIZE, 835 sector); 836 goto out; 837 } 838 } 839 840 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) { 841 sc->result = DID_ERROR << 16; 842 goto out; 843 } 844 845 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) { 846 uint16_t senselen; 847 848 if (datalen < 2) { 849 invalid_datalen: 850 iscsi_conn_printk(KERN_ERR, conn, 851 "Got CHECK_CONDITION but invalid data " 852 "buffer size of %d\n", datalen); 853 sc->result = DID_BAD_TARGET << 16; 854 goto out; 855 } 856 857 senselen = get_unaligned_be16(data); 858 if (datalen < senselen) 859 goto invalid_datalen; 860 861 memcpy(sc->sense_buffer, data + 2, 862 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE)); 863 ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n", 864 min_t(uint16_t, senselen, 865 SCSI_SENSE_BUFFERSIZE)); 866 } 867 868 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW | 869 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) { 870 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status; 871 } 872 873 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW | 874 ISCSI_FLAG_CMD_OVERFLOW)) { 875 int res_count = be32_to_cpu(rhdr->residual_count); 876 877 if (res_count > 0 && 878 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW || 879 res_count <= scsi_bufflen(sc))) 880 /* write side for bidi or uni-io set_resid */ 881 scsi_set_resid(sc, res_count); 882 else 883 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status; 884 } 885 out: 886 ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n", 887 sc, sc->result, task->itt); 888 conn->scsirsp_pdus_cnt++; 889 iscsi_complete_task(task, ISCSI_TASK_COMPLETED); 890 } 891 892 /** 893 * iscsi_data_in_rsp - SCSI Data-In Response processing 894 * @conn: iscsi connection 895 * @hdr: iscsi pdu 896 * @task: scsi command task 897 **/ 898 static void 899 iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 900 struct iscsi_task *task) 901 { 902 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr; 903 struct scsi_cmnd *sc = task->sc; 904 905 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS)) 906 return; 907 908 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr); 909 sc->result = (DID_OK << 16) | rhdr->cmd_status; 910 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1; 911 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW | 912 ISCSI_FLAG_DATA_OVERFLOW)) { 913 int res_count = be32_to_cpu(rhdr->residual_count); 914 915 if (res_count > 0 && 916 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW || 917 res_count <= sc->sdb.length)) 918 sc->sdb.resid = res_count; 919 else 920 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status; 921 } 922 923 ISCSI_DBG_SESSION(conn->session, "data in with status done " 924 "[sc %p res %d itt 0x%x]\n", 925 sc, sc->result, task->itt); 926 conn->scsirsp_pdus_cnt++; 927 iscsi_complete_task(task, ISCSI_TASK_COMPLETED); 928 } 929 930 static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr) 931 { 932 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr; 933 934 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; 935 conn->tmfrsp_pdus_cnt++; 936 937 if (conn->tmf_state != TMF_QUEUED) 938 return; 939 940 if (tmf->response == ISCSI_TMF_RSP_COMPLETE) 941 conn->tmf_state = TMF_SUCCESS; 942 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK) 943 conn->tmf_state = TMF_NOT_FOUND; 944 else 945 conn->tmf_state = TMF_FAILED; 946 wake_up(&conn->ehwait); 947 } 948 949 static int iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr) 950 { 951 struct iscsi_nopout hdr; 952 struct iscsi_task *task; 953 954 if (!rhdr && conn->ping_task) 955 return -EINVAL; 956 957 memset(&hdr, 0, sizeof(struct iscsi_nopout)); 958 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE; 959 hdr.flags = ISCSI_FLAG_CMD_FINAL; 960 961 if (rhdr) { 962 hdr.lun = rhdr->lun; 963 hdr.ttt = rhdr->ttt; 964 hdr.itt = RESERVED_ITT; 965 } else 966 hdr.ttt = RESERVED_ITT; 967 968 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0); 969 if (!task) { 970 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n"); 971 return -EIO; 972 } else if (!rhdr) { 973 /* only track our nops */ 974 conn->ping_task = task; 975 conn->last_ping = jiffies; 976 } 977 978 return 0; 979 } 980 981 static int iscsi_nop_out_rsp(struct iscsi_task *task, 982 struct iscsi_nopin *nop, char *data, int datalen) 983 { 984 struct iscsi_conn *conn = task->conn; 985 int rc = 0; 986 987 if (conn->ping_task != task) { 988 /* 989 * If this is not in response to one of our 990 * nops then it must be from userspace. 991 */ 992 if (iscsi_recv_pdu(conn->cls_conn, (struct iscsi_hdr *)nop, 993 data, datalen)) 994 rc = ISCSI_ERR_CONN_FAILED; 995 } else 996 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout); 997 iscsi_complete_task(task, ISCSI_TASK_COMPLETED); 998 return rc; 999 } 1000 1001 static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 1002 char *data, int datalen) 1003 { 1004 struct iscsi_reject *reject = (struct iscsi_reject *)hdr; 1005 struct iscsi_hdr rejected_pdu; 1006 int opcode, rc = 0; 1007 1008 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1; 1009 1010 if (ntoh24(reject->dlength) > datalen || 1011 ntoh24(reject->dlength) < sizeof(struct iscsi_hdr)) { 1012 iscsi_conn_printk(KERN_ERR, conn, "Cannot handle rejected " 1013 "pdu. Invalid data length (pdu dlength " 1014 "%u, datalen %d\n", ntoh24(reject->dlength), 1015 datalen); 1016 return ISCSI_ERR_PROTO; 1017 } 1018 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr)); 1019 opcode = rejected_pdu.opcode & ISCSI_OPCODE_MASK; 1020 1021 switch (reject->reason) { 1022 case ISCSI_REASON_DATA_DIGEST_ERROR: 1023 iscsi_conn_printk(KERN_ERR, conn, 1024 "pdu (op 0x%x itt 0x%x) rejected " 1025 "due to DataDigest error.\n", 1026 opcode, rejected_pdu.itt); 1027 break; 1028 case ISCSI_REASON_IMM_CMD_REJECT: 1029 iscsi_conn_printk(KERN_ERR, conn, 1030 "pdu (op 0x%x itt 0x%x) rejected. Too many " 1031 "immediate commands.\n", 1032 opcode, rejected_pdu.itt); 1033 /* 1034 * We only send one TMF at a time so if the target could not 1035 * handle it, then it should get fixed (RFC mandates that 1036 * a target can handle one immediate TMF per conn). 1037 * 1038 * For nops-outs, we could have sent more than one if 1039 * the target is sending us lots of nop-ins 1040 */ 1041 if (opcode != ISCSI_OP_NOOP_OUT) 1042 return 0; 1043 1044 if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG)) { 1045 /* 1046 * nop-out in response to target's nop-out rejected. 1047 * Just resend. 1048 */ 1049 /* In RX path we are under back lock */ 1050 spin_unlock(&conn->session->back_lock); 1051 spin_lock(&conn->session->frwd_lock); 1052 iscsi_send_nopout(conn, 1053 (struct iscsi_nopin*)&rejected_pdu); 1054 spin_unlock(&conn->session->frwd_lock); 1055 spin_lock(&conn->session->back_lock); 1056 } else { 1057 struct iscsi_task *task; 1058 /* 1059 * Our nop as ping got dropped. We know the target 1060 * and transport are ok so just clean up 1061 */ 1062 task = iscsi_itt_to_task(conn, rejected_pdu.itt); 1063 if (!task) { 1064 iscsi_conn_printk(KERN_ERR, conn, 1065 "Invalid pdu reject. Could " 1066 "not lookup rejected task.\n"); 1067 rc = ISCSI_ERR_BAD_ITT; 1068 } else 1069 rc = iscsi_nop_out_rsp(task, 1070 (struct iscsi_nopin*)&rejected_pdu, 1071 NULL, 0); 1072 } 1073 break; 1074 default: 1075 iscsi_conn_printk(KERN_ERR, conn, 1076 "pdu (op 0x%x itt 0x%x) rejected. Reason " 1077 "code 0x%x\n", rejected_pdu.opcode, 1078 rejected_pdu.itt, reject->reason); 1079 break; 1080 } 1081 return rc; 1082 } 1083 1084 /** 1085 * iscsi_itt_to_task - look up task by itt 1086 * @conn: iscsi connection 1087 * @itt: itt 1088 * 1089 * This should be used for mgmt tasks like login and nops, or if 1090 * the LDD's itt space does not include the session age. 1091 * 1092 * The session back_lock must be held. 1093 */ 1094 struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt) 1095 { 1096 struct iscsi_session *session = conn->session; 1097 int i; 1098 1099 if (itt == RESERVED_ITT) 1100 return NULL; 1101 1102 if (session->tt->parse_pdu_itt) 1103 session->tt->parse_pdu_itt(conn, itt, &i, NULL); 1104 else 1105 i = get_itt(itt); 1106 if (i >= session->cmds_max) 1107 return NULL; 1108 1109 return session->cmds[i]; 1110 } 1111 EXPORT_SYMBOL_GPL(iscsi_itt_to_task); 1112 1113 /** 1114 * __iscsi_complete_pdu - complete pdu 1115 * @conn: iscsi conn 1116 * @hdr: iscsi header 1117 * @data: data buffer 1118 * @datalen: len of data buffer 1119 * 1120 * Completes pdu processing by freeing any resources allocated at 1121 * queuecommand or send generic. session back_lock must be held and verify 1122 * itt must have been called. 1123 */ 1124 int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 1125 char *data, int datalen) 1126 { 1127 struct iscsi_session *session = conn->session; 1128 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0; 1129 struct iscsi_task *task; 1130 uint32_t itt; 1131 1132 conn->last_recv = jiffies; 1133 rc = iscsi_verify_itt(conn, hdr->itt); 1134 if (rc) 1135 return rc; 1136 1137 if (hdr->itt != RESERVED_ITT) 1138 itt = get_itt(hdr->itt); 1139 else 1140 itt = ~0U; 1141 1142 ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n", 1143 opcode, conn->id, itt, datalen); 1144 1145 if (itt == ~0U) { 1146 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); 1147 1148 switch(opcode) { 1149 case ISCSI_OP_NOOP_IN: 1150 if (datalen) { 1151 rc = ISCSI_ERR_PROTO; 1152 break; 1153 } 1154 1155 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG)) 1156 break; 1157 1158 /* In RX path we are under back lock */ 1159 spin_unlock(&session->back_lock); 1160 spin_lock(&session->frwd_lock); 1161 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr); 1162 spin_unlock(&session->frwd_lock); 1163 spin_lock(&session->back_lock); 1164 break; 1165 case ISCSI_OP_REJECT: 1166 rc = iscsi_handle_reject(conn, hdr, data, datalen); 1167 break; 1168 case ISCSI_OP_ASYNC_EVENT: 1169 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; 1170 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen)) 1171 rc = ISCSI_ERR_CONN_FAILED; 1172 break; 1173 default: 1174 rc = ISCSI_ERR_BAD_OPCODE; 1175 break; 1176 } 1177 goto out; 1178 } 1179 1180 switch(opcode) { 1181 case ISCSI_OP_SCSI_CMD_RSP: 1182 case ISCSI_OP_SCSI_DATA_IN: 1183 task = iscsi_itt_to_ctask(conn, hdr->itt); 1184 if (!task) 1185 return ISCSI_ERR_BAD_ITT; 1186 task->last_xfer = jiffies; 1187 break; 1188 case ISCSI_OP_R2T: 1189 /* 1190 * LLD handles R2Ts if they need to. 1191 */ 1192 return 0; 1193 case ISCSI_OP_LOGOUT_RSP: 1194 case ISCSI_OP_LOGIN_RSP: 1195 case ISCSI_OP_TEXT_RSP: 1196 case ISCSI_OP_SCSI_TMFUNC_RSP: 1197 case ISCSI_OP_NOOP_IN: 1198 task = iscsi_itt_to_task(conn, hdr->itt); 1199 if (!task) 1200 return ISCSI_ERR_BAD_ITT; 1201 break; 1202 default: 1203 return ISCSI_ERR_BAD_OPCODE; 1204 } 1205 1206 switch(opcode) { 1207 case ISCSI_OP_SCSI_CMD_RSP: 1208 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen); 1209 break; 1210 case ISCSI_OP_SCSI_DATA_IN: 1211 iscsi_data_in_rsp(conn, hdr, task); 1212 break; 1213 case ISCSI_OP_LOGOUT_RSP: 1214 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); 1215 if (datalen) { 1216 rc = ISCSI_ERR_PROTO; 1217 break; 1218 } 1219 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; 1220 goto recv_pdu; 1221 case ISCSI_OP_LOGIN_RSP: 1222 case ISCSI_OP_TEXT_RSP: 1223 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); 1224 /* 1225 * login related PDU's exp_statsn is handled in 1226 * userspace 1227 */ 1228 goto recv_pdu; 1229 case ISCSI_OP_SCSI_TMFUNC_RSP: 1230 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); 1231 if (datalen) { 1232 rc = ISCSI_ERR_PROTO; 1233 break; 1234 } 1235 1236 iscsi_tmf_rsp(conn, hdr); 1237 iscsi_complete_task(task, ISCSI_TASK_COMPLETED); 1238 break; 1239 case ISCSI_OP_NOOP_IN: 1240 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); 1241 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) { 1242 rc = ISCSI_ERR_PROTO; 1243 break; 1244 } 1245 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; 1246 1247 rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr, 1248 data, datalen); 1249 break; 1250 default: 1251 rc = ISCSI_ERR_BAD_OPCODE; 1252 break; 1253 } 1254 1255 out: 1256 return rc; 1257 recv_pdu: 1258 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen)) 1259 rc = ISCSI_ERR_CONN_FAILED; 1260 iscsi_complete_task(task, ISCSI_TASK_COMPLETED); 1261 return rc; 1262 } 1263 EXPORT_SYMBOL_GPL(__iscsi_complete_pdu); 1264 1265 int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 1266 char *data, int datalen) 1267 { 1268 int rc; 1269 1270 spin_lock(&conn->session->back_lock); 1271 rc = __iscsi_complete_pdu(conn, hdr, data, datalen); 1272 spin_unlock(&conn->session->back_lock); 1273 return rc; 1274 } 1275 EXPORT_SYMBOL_GPL(iscsi_complete_pdu); 1276 1277 int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt) 1278 { 1279 struct iscsi_session *session = conn->session; 1280 int age = 0, i = 0; 1281 1282 if (itt == RESERVED_ITT) 1283 return 0; 1284 1285 if (session->tt->parse_pdu_itt) 1286 session->tt->parse_pdu_itt(conn, itt, &i, &age); 1287 else { 1288 i = get_itt(itt); 1289 age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK; 1290 } 1291 1292 if (age != session->age) { 1293 iscsi_conn_printk(KERN_ERR, conn, 1294 "received itt %x expected session age (%x)\n", 1295 (__force u32)itt, session->age); 1296 return ISCSI_ERR_BAD_ITT; 1297 } 1298 1299 if (i >= session->cmds_max) { 1300 iscsi_conn_printk(KERN_ERR, conn, 1301 "received invalid itt index %u (max cmds " 1302 "%u.\n", i, session->cmds_max); 1303 return ISCSI_ERR_BAD_ITT; 1304 } 1305 return 0; 1306 } 1307 EXPORT_SYMBOL_GPL(iscsi_verify_itt); 1308 1309 /** 1310 * iscsi_itt_to_ctask - look up ctask by itt 1311 * @conn: iscsi connection 1312 * @itt: itt 1313 * 1314 * This should be used for cmd tasks. 1315 * 1316 * The session back_lock must be held. 1317 */ 1318 struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt) 1319 { 1320 struct iscsi_task *task; 1321 1322 if (iscsi_verify_itt(conn, itt)) 1323 return NULL; 1324 1325 task = iscsi_itt_to_task(conn, itt); 1326 if (!task || !task->sc) 1327 return NULL; 1328 1329 if (task->sc->SCp.phase != conn->session->age) { 1330 iscsi_session_printk(KERN_ERR, conn->session, 1331 "task's session age %d, expected %d\n", 1332 task->sc->SCp.phase, conn->session->age); 1333 return NULL; 1334 } 1335 1336 return task; 1337 } 1338 EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask); 1339 1340 void iscsi_session_failure(struct iscsi_session *session, 1341 enum iscsi_err err) 1342 { 1343 struct iscsi_conn *conn; 1344 struct device *dev; 1345 1346 spin_lock_bh(&session->frwd_lock); 1347 conn = session->leadconn; 1348 if (session->state == ISCSI_STATE_TERMINATE || !conn) { 1349 spin_unlock_bh(&session->frwd_lock); 1350 return; 1351 } 1352 1353 dev = get_device(&conn->cls_conn->dev); 1354 spin_unlock_bh(&session->frwd_lock); 1355 if (!dev) 1356 return; 1357 /* 1358 * if the host is being removed bypass the connection 1359 * recovery initialization because we are going to kill 1360 * the session. 1361 */ 1362 if (err == ISCSI_ERR_INVALID_HOST) 1363 iscsi_conn_error_event(conn->cls_conn, err); 1364 else 1365 iscsi_conn_failure(conn, err); 1366 put_device(dev); 1367 } 1368 EXPORT_SYMBOL_GPL(iscsi_session_failure); 1369 1370 void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err) 1371 { 1372 struct iscsi_session *session = conn->session; 1373 1374 spin_lock_bh(&session->frwd_lock); 1375 if (session->state == ISCSI_STATE_FAILED) { 1376 spin_unlock_bh(&session->frwd_lock); 1377 return; 1378 } 1379 1380 if (conn->stop_stage == 0) 1381 session->state = ISCSI_STATE_FAILED; 1382 spin_unlock_bh(&session->frwd_lock); 1383 1384 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); 1385 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx); 1386 iscsi_conn_error_event(conn->cls_conn, err); 1387 } 1388 EXPORT_SYMBOL_GPL(iscsi_conn_failure); 1389 1390 static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn) 1391 { 1392 struct iscsi_session *session = conn->session; 1393 1394 /* 1395 * Check for iSCSI window and take care of CmdSN wrap-around 1396 */ 1397 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) { 1398 ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn " 1399 "%u MaxCmdSN %u CmdSN %u/%u\n", 1400 session->exp_cmdsn, session->max_cmdsn, 1401 session->cmdsn, session->queued_cmdsn); 1402 return -ENOSPC; 1403 } 1404 return 0; 1405 } 1406 1407 static int iscsi_xmit_task(struct iscsi_conn *conn) 1408 { 1409 struct iscsi_task *task = conn->task; 1410 int rc; 1411 1412 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) 1413 return -ENODATA; 1414 1415 __iscsi_get_task(task); 1416 spin_unlock_bh(&conn->session->frwd_lock); 1417 rc = conn->session->tt->xmit_task(task); 1418 spin_lock_bh(&conn->session->frwd_lock); 1419 if (!rc) { 1420 /* done with this task */ 1421 task->last_xfer = jiffies; 1422 conn->task = NULL; 1423 } 1424 /* regular RX path uses back_lock */ 1425 spin_lock(&conn->session->back_lock); 1426 __iscsi_put_task(task); 1427 spin_unlock(&conn->session->back_lock); 1428 return rc; 1429 } 1430 1431 /** 1432 * iscsi_requeue_task - requeue task to run from session workqueue 1433 * @task: task to requeue 1434 * 1435 * LLDs that need to run a task from the session workqueue should call 1436 * this. The session frwd_lock must be held. This should only be called 1437 * by software drivers. 1438 */ 1439 void iscsi_requeue_task(struct iscsi_task *task) 1440 { 1441 struct iscsi_conn *conn = task->conn; 1442 1443 /* 1444 * this may be on the requeue list already if the xmit_task callout 1445 * is handling the r2ts while we are adding new ones 1446 */ 1447 spin_lock_bh(&conn->taskqueuelock); 1448 if (list_empty(&task->running)) 1449 list_add_tail(&task->running, &conn->requeue); 1450 spin_unlock_bh(&conn->taskqueuelock); 1451 iscsi_conn_queue_work(conn); 1452 } 1453 EXPORT_SYMBOL_GPL(iscsi_requeue_task); 1454 1455 /** 1456 * iscsi_data_xmit - xmit any command into the scheduled connection 1457 * @conn: iscsi connection 1458 * 1459 * Notes: 1460 * The function can return -EAGAIN in which case the caller must 1461 * re-schedule it again later or recover. '0' return code means 1462 * successful xmit. 1463 **/ 1464 static int iscsi_data_xmit(struct iscsi_conn *conn) 1465 { 1466 struct iscsi_task *task; 1467 int rc = 0; 1468 1469 spin_lock_bh(&conn->session->frwd_lock); 1470 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) { 1471 ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n"); 1472 spin_unlock_bh(&conn->session->frwd_lock); 1473 return -ENODATA; 1474 } 1475 1476 if (conn->task) { 1477 rc = iscsi_xmit_task(conn); 1478 if (rc) 1479 goto done; 1480 } 1481 1482 /* 1483 * process mgmt pdus like nops before commands since we should 1484 * only have one nop-out as a ping from us and targets should not 1485 * overflow us with nop-ins 1486 */ 1487 spin_lock_bh(&conn->taskqueuelock); 1488 check_mgmt: 1489 while (!list_empty(&conn->mgmtqueue)) { 1490 conn->task = list_entry(conn->mgmtqueue.next, 1491 struct iscsi_task, running); 1492 list_del_init(&conn->task->running); 1493 spin_unlock_bh(&conn->taskqueuelock); 1494 if (iscsi_prep_mgmt_task(conn, conn->task)) { 1495 /* regular RX path uses back_lock */ 1496 spin_lock_bh(&conn->session->back_lock); 1497 __iscsi_put_task(conn->task); 1498 spin_unlock_bh(&conn->session->back_lock); 1499 conn->task = NULL; 1500 spin_lock_bh(&conn->taskqueuelock); 1501 continue; 1502 } 1503 rc = iscsi_xmit_task(conn); 1504 if (rc) 1505 goto done; 1506 spin_lock_bh(&conn->taskqueuelock); 1507 } 1508 1509 /* process pending command queue */ 1510 while (!list_empty(&conn->cmdqueue)) { 1511 conn->task = list_entry(conn->cmdqueue.next, struct iscsi_task, 1512 running); 1513 list_del_init(&conn->task->running); 1514 spin_unlock_bh(&conn->taskqueuelock); 1515 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) { 1516 fail_scsi_task(conn->task, DID_IMM_RETRY); 1517 spin_lock_bh(&conn->taskqueuelock); 1518 continue; 1519 } 1520 rc = iscsi_prep_scsi_cmd_pdu(conn->task); 1521 if (rc) { 1522 if (rc == -ENOMEM || rc == -EACCES) { 1523 spin_lock_bh(&conn->taskqueuelock); 1524 list_add_tail(&conn->task->running, 1525 &conn->cmdqueue); 1526 conn->task = NULL; 1527 spin_unlock_bh(&conn->taskqueuelock); 1528 goto done; 1529 } else 1530 fail_scsi_task(conn->task, DID_ABORT); 1531 spin_lock_bh(&conn->taskqueuelock); 1532 continue; 1533 } 1534 rc = iscsi_xmit_task(conn); 1535 if (rc) 1536 goto done; 1537 /* 1538 * we could continuously get new task requests so 1539 * we need to check the mgmt queue for nops that need to 1540 * be sent to aviod starvation 1541 */ 1542 spin_lock_bh(&conn->taskqueuelock); 1543 if (!list_empty(&conn->mgmtqueue)) 1544 goto check_mgmt; 1545 } 1546 1547 while (!list_empty(&conn->requeue)) { 1548 /* 1549 * we always do fastlogout - conn stop code will clean up. 1550 */ 1551 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) 1552 break; 1553 1554 task = list_entry(conn->requeue.next, struct iscsi_task, 1555 running); 1556 if (iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_DATA_OUT)) 1557 break; 1558 1559 conn->task = task; 1560 list_del_init(&conn->task->running); 1561 conn->task->state = ISCSI_TASK_RUNNING; 1562 spin_unlock_bh(&conn->taskqueuelock); 1563 rc = iscsi_xmit_task(conn); 1564 if (rc) 1565 goto done; 1566 spin_lock_bh(&conn->taskqueuelock); 1567 if (!list_empty(&conn->mgmtqueue)) 1568 goto check_mgmt; 1569 } 1570 spin_unlock_bh(&conn->taskqueuelock); 1571 spin_unlock_bh(&conn->session->frwd_lock); 1572 return -ENODATA; 1573 1574 done: 1575 spin_unlock_bh(&conn->session->frwd_lock); 1576 return rc; 1577 } 1578 1579 static void iscsi_xmitworker(struct work_struct *work) 1580 { 1581 struct iscsi_conn *conn = 1582 container_of(work, struct iscsi_conn, xmitwork); 1583 int rc; 1584 /* 1585 * serialize Xmit worker on a per-connection basis. 1586 */ 1587 do { 1588 rc = iscsi_data_xmit(conn); 1589 } while (rc >= 0 || rc == -EAGAIN); 1590 } 1591 1592 static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn, 1593 struct scsi_cmnd *sc) 1594 { 1595 struct iscsi_task *task; 1596 1597 if (!kfifo_out(&conn->session->cmdpool.queue, 1598 (void *) &task, sizeof(void *))) 1599 return NULL; 1600 1601 sc->SCp.phase = conn->session->age; 1602 sc->SCp.ptr = (char *) task; 1603 1604 refcount_set(&task->refcount, 1); 1605 task->state = ISCSI_TASK_PENDING; 1606 task->conn = conn; 1607 task->sc = sc; 1608 task->have_checked_conn = false; 1609 task->last_timeout = jiffies; 1610 task->last_xfer = jiffies; 1611 task->protected = false; 1612 INIT_LIST_HEAD(&task->running); 1613 return task; 1614 } 1615 1616 enum { 1617 FAILURE_BAD_HOST = 1, 1618 FAILURE_SESSION_FAILED, 1619 FAILURE_SESSION_FREED, 1620 FAILURE_WINDOW_CLOSED, 1621 FAILURE_OOM, 1622 FAILURE_SESSION_TERMINATE, 1623 FAILURE_SESSION_IN_RECOVERY, 1624 FAILURE_SESSION_RECOVERY_TIMEOUT, 1625 FAILURE_SESSION_LOGGING_OUT, 1626 FAILURE_SESSION_NOT_READY, 1627 }; 1628 1629 int iscsi_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *sc) 1630 { 1631 struct iscsi_cls_session *cls_session; 1632 struct iscsi_host *ihost; 1633 int reason = 0; 1634 struct iscsi_session *session; 1635 struct iscsi_conn *conn; 1636 struct iscsi_task *task = NULL; 1637 1638 sc->result = 0; 1639 sc->SCp.ptr = NULL; 1640 1641 ihost = shost_priv(host); 1642 1643 cls_session = starget_to_session(scsi_target(sc->device)); 1644 session = cls_session->dd_data; 1645 spin_lock_bh(&session->frwd_lock); 1646 1647 reason = iscsi_session_chkready(cls_session); 1648 if (reason) { 1649 sc->result = reason; 1650 goto fault; 1651 } 1652 1653 if (session->state != ISCSI_STATE_LOGGED_IN) { 1654 /* 1655 * to handle the race between when we set the recovery state 1656 * and block the session we requeue here (commands could 1657 * be entering our queuecommand while a block is starting 1658 * up because the block code is not locked) 1659 */ 1660 switch (session->state) { 1661 case ISCSI_STATE_FAILED: 1662 /* 1663 * cmds should fail during shutdown, if the session 1664 * state is bad, allowing completion to happen 1665 */ 1666 if (unlikely(system_state != SYSTEM_RUNNING)) { 1667 reason = FAILURE_SESSION_FAILED; 1668 sc->result = DID_NO_CONNECT << 16; 1669 break; 1670 } 1671 /* fall through */ 1672 case ISCSI_STATE_IN_RECOVERY: 1673 reason = FAILURE_SESSION_IN_RECOVERY; 1674 sc->result = DID_IMM_RETRY << 16; 1675 break; 1676 case ISCSI_STATE_LOGGING_OUT: 1677 reason = FAILURE_SESSION_LOGGING_OUT; 1678 sc->result = DID_IMM_RETRY << 16; 1679 break; 1680 case ISCSI_STATE_RECOVERY_FAILED: 1681 reason = FAILURE_SESSION_RECOVERY_TIMEOUT; 1682 sc->result = DID_TRANSPORT_FAILFAST << 16; 1683 break; 1684 case ISCSI_STATE_TERMINATE: 1685 reason = FAILURE_SESSION_TERMINATE; 1686 sc->result = DID_NO_CONNECT << 16; 1687 break; 1688 default: 1689 reason = FAILURE_SESSION_FREED; 1690 sc->result = DID_NO_CONNECT << 16; 1691 } 1692 goto fault; 1693 } 1694 1695 conn = session->leadconn; 1696 if (!conn) { 1697 reason = FAILURE_SESSION_FREED; 1698 sc->result = DID_NO_CONNECT << 16; 1699 goto fault; 1700 } 1701 1702 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) { 1703 reason = FAILURE_SESSION_IN_RECOVERY; 1704 sc->result = DID_REQUEUE << 16; 1705 goto fault; 1706 } 1707 1708 if (iscsi_check_cmdsn_window_closed(conn)) { 1709 reason = FAILURE_WINDOW_CLOSED; 1710 goto reject; 1711 } 1712 1713 task = iscsi_alloc_task(conn, sc); 1714 if (!task) { 1715 reason = FAILURE_OOM; 1716 goto reject; 1717 } 1718 1719 if (!ihost->workq) { 1720 reason = iscsi_prep_scsi_cmd_pdu(task); 1721 if (reason) { 1722 if (reason == -ENOMEM || reason == -EACCES) { 1723 reason = FAILURE_OOM; 1724 goto prepd_reject; 1725 } else { 1726 sc->result = DID_ABORT << 16; 1727 goto prepd_fault; 1728 } 1729 } 1730 if (session->tt->xmit_task(task)) { 1731 session->cmdsn--; 1732 reason = FAILURE_SESSION_NOT_READY; 1733 goto prepd_reject; 1734 } 1735 } else { 1736 spin_lock_bh(&conn->taskqueuelock); 1737 list_add_tail(&task->running, &conn->cmdqueue); 1738 spin_unlock_bh(&conn->taskqueuelock); 1739 iscsi_conn_queue_work(conn); 1740 } 1741 1742 session->queued_cmdsn++; 1743 spin_unlock_bh(&session->frwd_lock); 1744 return 0; 1745 1746 prepd_reject: 1747 iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ); 1748 reject: 1749 spin_unlock_bh(&session->frwd_lock); 1750 ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n", 1751 sc->cmnd[0], reason); 1752 return SCSI_MLQUEUE_TARGET_BUSY; 1753 1754 prepd_fault: 1755 iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ); 1756 fault: 1757 spin_unlock_bh(&session->frwd_lock); 1758 ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n", 1759 sc->cmnd[0], reason); 1760 scsi_set_resid(sc, scsi_bufflen(sc)); 1761 sc->scsi_done(sc); 1762 return 0; 1763 } 1764 EXPORT_SYMBOL_GPL(iscsi_queuecommand); 1765 1766 int iscsi_target_alloc(struct scsi_target *starget) 1767 { 1768 struct iscsi_cls_session *cls_session = starget_to_session(starget); 1769 struct iscsi_session *session = cls_session->dd_data; 1770 1771 starget->can_queue = session->scsi_cmds_max; 1772 return 0; 1773 } 1774 EXPORT_SYMBOL_GPL(iscsi_target_alloc); 1775 1776 static void iscsi_tmf_timedout(struct timer_list *t) 1777 { 1778 struct iscsi_conn *conn = from_timer(conn, t, tmf_timer); 1779 struct iscsi_session *session = conn->session; 1780 1781 spin_lock(&session->frwd_lock); 1782 if (conn->tmf_state == TMF_QUEUED) { 1783 conn->tmf_state = TMF_TIMEDOUT; 1784 ISCSI_DBG_EH(session, "tmf timedout\n"); 1785 /* unblock eh_abort() */ 1786 wake_up(&conn->ehwait); 1787 } 1788 spin_unlock(&session->frwd_lock); 1789 } 1790 1791 static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn, 1792 struct iscsi_tm *hdr, int age, 1793 int timeout) 1794 __must_hold(&session->frwd_lock) 1795 { 1796 struct iscsi_session *session = conn->session; 1797 struct iscsi_task *task; 1798 1799 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr, 1800 NULL, 0); 1801 if (!task) { 1802 spin_unlock_bh(&session->frwd_lock); 1803 iscsi_conn_printk(KERN_ERR, conn, "Could not send TMF.\n"); 1804 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); 1805 spin_lock_bh(&session->frwd_lock); 1806 return -EPERM; 1807 } 1808 conn->tmfcmd_pdus_cnt++; 1809 conn->tmf_timer.expires = timeout * HZ + jiffies; 1810 add_timer(&conn->tmf_timer); 1811 ISCSI_DBG_EH(session, "tmf set timeout\n"); 1812 1813 spin_unlock_bh(&session->frwd_lock); 1814 mutex_unlock(&session->eh_mutex); 1815 1816 /* 1817 * block eh thread until: 1818 * 1819 * 1) tmf response 1820 * 2) tmf timeout 1821 * 3) session is terminated or restarted or userspace has 1822 * given up on recovery 1823 */ 1824 wait_event_interruptible(conn->ehwait, age != session->age || 1825 session->state != ISCSI_STATE_LOGGED_IN || 1826 conn->tmf_state != TMF_QUEUED); 1827 if (signal_pending(current)) 1828 flush_signals(current); 1829 del_timer_sync(&conn->tmf_timer); 1830 1831 mutex_lock(&session->eh_mutex); 1832 spin_lock_bh(&session->frwd_lock); 1833 /* if the session drops it will clean up the task */ 1834 if (age != session->age || 1835 session->state != ISCSI_STATE_LOGGED_IN) 1836 return -ENOTCONN; 1837 return 0; 1838 } 1839 1840 /* 1841 * Fail commands. session lock held and recv side suspended and xmit 1842 * thread flushed 1843 */ 1844 static void fail_scsi_tasks(struct iscsi_conn *conn, u64 lun, int error) 1845 { 1846 struct iscsi_task *task; 1847 int i; 1848 1849 for (i = 0; i < conn->session->cmds_max; i++) { 1850 task = conn->session->cmds[i]; 1851 if (!task->sc || task->state == ISCSI_TASK_FREE) 1852 continue; 1853 1854 if (lun != -1 && lun != task->sc->device->lun) 1855 continue; 1856 1857 ISCSI_DBG_SESSION(conn->session, 1858 "failing sc %p itt 0x%x state %d\n", 1859 task->sc, task->itt, task->state); 1860 fail_scsi_task(task, error); 1861 } 1862 } 1863 1864 /** 1865 * iscsi_suspend_queue - suspend iscsi_queuecommand 1866 * @conn: iscsi conn to stop queueing IO on 1867 * 1868 * This grabs the session frwd_lock to make sure no one is in 1869 * xmit_task/queuecommand, and then sets suspend to prevent 1870 * new commands from being queued. This only needs to be called 1871 * by offload drivers that need to sync a path like ep disconnect 1872 * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi 1873 * will call iscsi_start_tx and iscsi_unblock_session when in FFP. 1874 */ 1875 void iscsi_suspend_queue(struct iscsi_conn *conn) 1876 { 1877 spin_lock_bh(&conn->session->frwd_lock); 1878 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); 1879 spin_unlock_bh(&conn->session->frwd_lock); 1880 } 1881 EXPORT_SYMBOL_GPL(iscsi_suspend_queue); 1882 1883 /** 1884 * iscsi_suspend_tx - suspend iscsi_data_xmit 1885 * @conn: iscsi conn tp stop processing IO on. 1886 * 1887 * This function sets the suspend bit to prevent iscsi_data_xmit 1888 * from sending new IO, and if work is queued on the xmit thread 1889 * it will wait for it to be completed. 1890 */ 1891 void iscsi_suspend_tx(struct iscsi_conn *conn) 1892 { 1893 struct Scsi_Host *shost = conn->session->host; 1894 struct iscsi_host *ihost = shost_priv(shost); 1895 1896 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); 1897 if (ihost->workq) 1898 flush_workqueue(ihost->workq); 1899 } 1900 EXPORT_SYMBOL_GPL(iscsi_suspend_tx); 1901 1902 static void iscsi_start_tx(struct iscsi_conn *conn) 1903 { 1904 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); 1905 iscsi_conn_queue_work(conn); 1906 } 1907 1908 /* 1909 * We want to make sure a ping is in flight. It has timed out. 1910 * And we are not busy processing a pdu that is making 1911 * progress but got started before the ping and is taking a while 1912 * to complete so the ping is just stuck behind it in a queue. 1913 */ 1914 static int iscsi_has_ping_timed_out(struct iscsi_conn *conn) 1915 { 1916 if (conn->ping_task && 1917 time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) + 1918 (conn->ping_timeout * HZ), jiffies)) 1919 return 1; 1920 else 1921 return 0; 1922 } 1923 1924 enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc) 1925 { 1926 enum blk_eh_timer_return rc = BLK_EH_DONE; 1927 struct iscsi_task *task = NULL, *running_task; 1928 struct iscsi_cls_session *cls_session; 1929 struct iscsi_session *session; 1930 struct iscsi_conn *conn; 1931 int i; 1932 1933 cls_session = starget_to_session(scsi_target(sc->device)); 1934 session = cls_session->dd_data; 1935 1936 ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc); 1937 1938 spin_lock(&session->frwd_lock); 1939 task = (struct iscsi_task *)sc->SCp.ptr; 1940 if (!task) { 1941 /* 1942 * Raced with completion. Blk layer has taken ownership 1943 * so let timeout code complete it now. 1944 */ 1945 rc = BLK_EH_DONE; 1946 goto done; 1947 } 1948 1949 if (session->state != ISCSI_STATE_LOGGED_IN) { 1950 /* 1951 * During shutdown, if session is prematurely disconnected, 1952 * recovery won't happen and there will be hung cmds. Not 1953 * handling cmds would trigger EH, also bad in this case. 1954 * Instead, handle cmd, allow completion to happen and let 1955 * upper layer to deal with the result. 1956 */ 1957 if (unlikely(system_state != SYSTEM_RUNNING)) { 1958 sc->result = DID_NO_CONNECT << 16; 1959 ISCSI_DBG_EH(session, "sc on shutdown, handled\n"); 1960 rc = BLK_EH_DONE; 1961 goto done; 1962 } 1963 /* 1964 * We are probably in the middle of iscsi recovery so let 1965 * that complete and handle the error. 1966 */ 1967 rc = BLK_EH_RESET_TIMER; 1968 goto done; 1969 } 1970 1971 conn = session->leadconn; 1972 if (!conn) { 1973 /* In the middle of shuting down */ 1974 rc = BLK_EH_RESET_TIMER; 1975 goto done; 1976 } 1977 1978 /* 1979 * If we have sent (at least queued to the network layer) a pdu or 1980 * recvd one for the task since the last timeout ask for 1981 * more time. If on the next timeout we have not made progress 1982 * we can check if it is the task or connection when we send the 1983 * nop as a ping. 1984 */ 1985 if (time_after(task->last_xfer, task->last_timeout)) { 1986 ISCSI_DBG_EH(session, "Command making progress. Asking " 1987 "scsi-ml for more time to complete. " 1988 "Last data xfer at %lu. Last timeout was at " 1989 "%lu\n.", task->last_xfer, task->last_timeout); 1990 task->have_checked_conn = false; 1991 rc = BLK_EH_RESET_TIMER; 1992 goto done; 1993 } 1994 1995 if (!conn->recv_timeout && !conn->ping_timeout) 1996 goto done; 1997 /* 1998 * if the ping timedout then we are in the middle of cleaning up 1999 * and can let the iscsi eh handle it 2000 */ 2001 if (iscsi_has_ping_timed_out(conn)) { 2002 rc = BLK_EH_RESET_TIMER; 2003 goto done; 2004 } 2005 2006 for (i = 0; i < conn->session->cmds_max; i++) { 2007 running_task = conn->session->cmds[i]; 2008 if (!running_task->sc || running_task == task || 2009 running_task->state != ISCSI_TASK_RUNNING) 2010 continue; 2011 2012 /* 2013 * Only check if cmds started before this one have made 2014 * progress, or this could never fail 2015 */ 2016 if (time_after(running_task->sc->jiffies_at_alloc, 2017 task->sc->jiffies_at_alloc)) 2018 continue; 2019 2020 if (time_after(running_task->last_xfer, task->last_timeout)) { 2021 /* 2022 * This task has not made progress, but a task 2023 * started before us has transferred data since 2024 * we started/last-checked. We could be queueing 2025 * too many tasks or the LU is bad. 2026 * 2027 * If the device is bad the cmds ahead of us on 2028 * other devs will complete, and this loop will 2029 * eventually fail starting the scsi eh. 2030 */ 2031 ISCSI_DBG_EH(session, "Command has not made progress " 2032 "but commands ahead of it have. " 2033 "Asking scsi-ml for more time to " 2034 "complete. Our last xfer vs running task " 2035 "last xfer %lu/%lu. Last check %lu.\n", 2036 task->last_xfer, running_task->last_xfer, 2037 task->last_timeout); 2038 rc = BLK_EH_RESET_TIMER; 2039 goto done; 2040 } 2041 } 2042 2043 /* Assumes nop timeout is shorter than scsi cmd timeout */ 2044 if (task->have_checked_conn) 2045 goto done; 2046 2047 /* 2048 * Checking the transport already or nop from a cmd timeout still 2049 * running 2050 */ 2051 if (conn->ping_task) { 2052 task->have_checked_conn = true; 2053 rc = BLK_EH_RESET_TIMER; 2054 goto done; 2055 } 2056 2057 /* Make sure there is a transport check done */ 2058 iscsi_send_nopout(conn, NULL); 2059 task->have_checked_conn = true; 2060 rc = BLK_EH_RESET_TIMER; 2061 2062 done: 2063 if (task) 2064 task->last_timeout = jiffies; 2065 spin_unlock(&session->frwd_lock); 2066 ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ? 2067 "timer reset" : "shutdown or nh"); 2068 return rc; 2069 } 2070 EXPORT_SYMBOL_GPL(iscsi_eh_cmd_timed_out); 2071 2072 static void iscsi_check_transport_timeouts(struct timer_list *t) 2073 { 2074 struct iscsi_conn *conn = from_timer(conn, t, transport_timer); 2075 struct iscsi_session *session = conn->session; 2076 unsigned long recv_timeout, next_timeout = 0, last_recv; 2077 2078 spin_lock(&session->frwd_lock); 2079 if (session->state != ISCSI_STATE_LOGGED_IN) 2080 goto done; 2081 2082 recv_timeout = conn->recv_timeout; 2083 if (!recv_timeout) 2084 goto done; 2085 2086 recv_timeout *= HZ; 2087 last_recv = conn->last_recv; 2088 2089 if (iscsi_has_ping_timed_out(conn)) { 2090 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs " 2091 "expired, recv timeout %d, last rx %lu, " 2092 "last ping %lu, now %lu\n", 2093 conn->ping_timeout, conn->recv_timeout, 2094 last_recv, conn->last_ping, jiffies); 2095 spin_unlock(&session->frwd_lock); 2096 iscsi_conn_failure(conn, ISCSI_ERR_NOP_TIMEDOUT); 2097 return; 2098 } 2099 2100 if (time_before_eq(last_recv + recv_timeout, jiffies)) { 2101 /* send a ping to try to provoke some traffic */ 2102 ISCSI_DBG_CONN(conn, "Sending nopout as ping\n"); 2103 if (iscsi_send_nopout(conn, NULL)) 2104 next_timeout = jiffies + (1 * HZ); 2105 else 2106 next_timeout = conn->last_ping + (conn->ping_timeout * HZ); 2107 } else 2108 next_timeout = last_recv + recv_timeout; 2109 2110 ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout); 2111 mod_timer(&conn->transport_timer, next_timeout); 2112 done: 2113 spin_unlock(&session->frwd_lock); 2114 } 2115 2116 static void iscsi_prep_abort_task_pdu(struct iscsi_task *task, 2117 struct iscsi_tm *hdr) 2118 { 2119 memset(hdr, 0, sizeof(*hdr)); 2120 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE; 2121 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK; 2122 hdr->flags |= ISCSI_FLAG_CMD_FINAL; 2123 hdr->lun = task->lun; 2124 hdr->rtt = task->hdr_itt; 2125 hdr->refcmdsn = task->cmdsn; 2126 } 2127 2128 int iscsi_eh_abort(struct scsi_cmnd *sc) 2129 { 2130 struct iscsi_cls_session *cls_session; 2131 struct iscsi_session *session; 2132 struct iscsi_conn *conn; 2133 struct iscsi_task *task; 2134 struct iscsi_tm *hdr; 2135 int age; 2136 2137 cls_session = starget_to_session(scsi_target(sc->device)); 2138 session = cls_session->dd_data; 2139 2140 ISCSI_DBG_EH(session, "aborting sc %p\n", sc); 2141 2142 mutex_lock(&session->eh_mutex); 2143 spin_lock_bh(&session->frwd_lock); 2144 /* 2145 * if session was ISCSI_STATE_IN_RECOVERY then we may not have 2146 * got the command. 2147 */ 2148 if (!sc->SCp.ptr) { 2149 ISCSI_DBG_EH(session, "sc never reached iscsi layer or " 2150 "it completed.\n"); 2151 spin_unlock_bh(&session->frwd_lock); 2152 mutex_unlock(&session->eh_mutex); 2153 return SUCCESS; 2154 } 2155 2156 /* 2157 * If we are not logged in or we have started a new session 2158 * then let the host reset code handle this 2159 */ 2160 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN || 2161 sc->SCp.phase != session->age) { 2162 spin_unlock_bh(&session->frwd_lock); 2163 mutex_unlock(&session->eh_mutex); 2164 ISCSI_DBG_EH(session, "failing abort due to dropped " 2165 "session.\n"); 2166 return FAILED; 2167 } 2168 2169 conn = session->leadconn; 2170 conn->eh_abort_cnt++; 2171 age = session->age; 2172 2173 task = (struct iscsi_task *)sc->SCp.ptr; 2174 ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n", 2175 sc, task->itt); 2176 2177 /* task completed before time out */ 2178 if (!task->sc) { 2179 ISCSI_DBG_EH(session, "sc completed while abort in progress\n"); 2180 goto success; 2181 } 2182 2183 if (task->state == ISCSI_TASK_PENDING) { 2184 fail_scsi_task(task, DID_ABORT); 2185 goto success; 2186 } 2187 2188 /* only have one tmf outstanding at a time */ 2189 if (conn->tmf_state != TMF_INITIAL) 2190 goto failed; 2191 conn->tmf_state = TMF_QUEUED; 2192 2193 hdr = &conn->tmhdr; 2194 iscsi_prep_abort_task_pdu(task, hdr); 2195 2196 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) 2197 goto failed; 2198 2199 switch (conn->tmf_state) { 2200 case TMF_SUCCESS: 2201 spin_unlock_bh(&session->frwd_lock); 2202 /* 2203 * stop tx side incase the target had sent a abort rsp but 2204 * the initiator was still writing out data. 2205 */ 2206 iscsi_suspend_tx(conn); 2207 /* 2208 * we do not stop the recv side because targets have been 2209 * good and have never sent us a successful tmf response 2210 * then sent more data for the cmd. 2211 */ 2212 spin_lock_bh(&session->frwd_lock); 2213 fail_scsi_task(task, DID_ABORT); 2214 conn->tmf_state = TMF_INITIAL; 2215 memset(hdr, 0, sizeof(*hdr)); 2216 spin_unlock_bh(&session->frwd_lock); 2217 iscsi_start_tx(conn); 2218 goto success_unlocked; 2219 case TMF_TIMEDOUT: 2220 spin_unlock_bh(&session->frwd_lock); 2221 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST); 2222 goto failed_unlocked; 2223 case TMF_NOT_FOUND: 2224 if (!sc->SCp.ptr) { 2225 conn->tmf_state = TMF_INITIAL; 2226 memset(hdr, 0, sizeof(*hdr)); 2227 /* task completed before tmf abort response */ 2228 ISCSI_DBG_EH(session, "sc completed while abort in " 2229 "progress\n"); 2230 goto success; 2231 } 2232 /* fall through */ 2233 default: 2234 conn->tmf_state = TMF_INITIAL; 2235 goto failed; 2236 } 2237 2238 success: 2239 spin_unlock_bh(&session->frwd_lock); 2240 success_unlocked: 2241 ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n", 2242 sc, task->itt); 2243 mutex_unlock(&session->eh_mutex); 2244 return SUCCESS; 2245 2246 failed: 2247 spin_unlock_bh(&session->frwd_lock); 2248 failed_unlocked: 2249 ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc, 2250 task ? task->itt : 0); 2251 mutex_unlock(&session->eh_mutex); 2252 return FAILED; 2253 } 2254 EXPORT_SYMBOL_GPL(iscsi_eh_abort); 2255 2256 static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr) 2257 { 2258 memset(hdr, 0, sizeof(*hdr)); 2259 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE; 2260 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK; 2261 hdr->flags |= ISCSI_FLAG_CMD_FINAL; 2262 int_to_scsilun(sc->device->lun, &hdr->lun); 2263 hdr->rtt = RESERVED_ITT; 2264 } 2265 2266 int iscsi_eh_device_reset(struct scsi_cmnd *sc) 2267 { 2268 struct iscsi_cls_session *cls_session; 2269 struct iscsi_session *session; 2270 struct iscsi_conn *conn; 2271 struct iscsi_tm *hdr; 2272 int rc = FAILED; 2273 2274 cls_session = starget_to_session(scsi_target(sc->device)); 2275 session = cls_session->dd_data; 2276 2277 ISCSI_DBG_EH(session, "LU Reset [sc %p lun %llu]\n", sc, 2278 sc->device->lun); 2279 2280 mutex_lock(&session->eh_mutex); 2281 spin_lock_bh(&session->frwd_lock); 2282 /* 2283 * Just check if we are not logged in. We cannot check for 2284 * the phase because the reset could come from a ioctl. 2285 */ 2286 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN) 2287 goto unlock; 2288 conn = session->leadconn; 2289 2290 /* only have one tmf outstanding at a time */ 2291 if (conn->tmf_state != TMF_INITIAL) 2292 goto unlock; 2293 conn->tmf_state = TMF_QUEUED; 2294 2295 hdr = &conn->tmhdr; 2296 iscsi_prep_lun_reset_pdu(sc, hdr); 2297 2298 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age, 2299 session->lu_reset_timeout)) { 2300 rc = FAILED; 2301 goto unlock; 2302 } 2303 2304 switch (conn->tmf_state) { 2305 case TMF_SUCCESS: 2306 break; 2307 case TMF_TIMEDOUT: 2308 spin_unlock_bh(&session->frwd_lock); 2309 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST); 2310 goto done; 2311 default: 2312 conn->tmf_state = TMF_INITIAL; 2313 goto unlock; 2314 } 2315 2316 rc = SUCCESS; 2317 spin_unlock_bh(&session->frwd_lock); 2318 2319 iscsi_suspend_tx(conn); 2320 2321 spin_lock_bh(&session->frwd_lock); 2322 memset(hdr, 0, sizeof(*hdr)); 2323 fail_scsi_tasks(conn, sc->device->lun, DID_ERROR); 2324 conn->tmf_state = TMF_INITIAL; 2325 spin_unlock_bh(&session->frwd_lock); 2326 2327 iscsi_start_tx(conn); 2328 goto done; 2329 2330 unlock: 2331 spin_unlock_bh(&session->frwd_lock); 2332 done: 2333 ISCSI_DBG_EH(session, "dev reset result = %s\n", 2334 rc == SUCCESS ? "SUCCESS" : "FAILED"); 2335 mutex_unlock(&session->eh_mutex); 2336 return rc; 2337 } 2338 EXPORT_SYMBOL_GPL(iscsi_eh_device_reset); 2339 2340 void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session) 2341 { 2342 struct iscsi_session *session = cls_session->dd_data; 2343 2344 spin_lock_bh(&session->frwd_lock); 2345 if (session->state != ISCSI_STATE_LOGGED_IN) { 2346 session->state = ISCSI_STATE_RECOVERY_FAILED; 2347 if (session->leadconn) 2348 wake_up(&session->leadconn->ehwait); 2349 } 2350 spin_unlock_bh(&session->frwd_lock); 2351 } 2352 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout); 2353 2354 /** 2355 * iscsi_eh_session_reset - drop session and attempt relogin 2356 * @sc: scsi command 2357 * 2358 * This function will wait for a relogin, session termination from 2359 * userspace, or a recovery/replacement timeout. 2360 */ 2361 int iscsi_eh_session_reset(struct scsi_cmnd *sc) 2362 { 2363 struct iscsi_cls_session *cls_session; 2364 struct iscsi_session *session; 2365 struct iscsi_conn *conn; 2366 2367 cls_session = starget_to_session(scsi_target(sc->device)); 2368 session = cls_session->dd_data; 2369 conn = session->leadconn; 2370 2371 mutex_lock(&session->eh_mutex); 2372 spin_lock_bh(&session->frwd_lock); 2373 if (session->state == ISCSI_STATE_TERMINATE) { 2374 failed: 2375 ISCSI_DBG_EH(session, 2376 "failing session reset: Could not log back into " 2377 "%s [age %d]\n", session->targetname, 2378 session->age); 2379 spin_unlock_bh(&session->frwd_lock); 2380 mutex_unlock(&session->eh_mutex); 2381 return FAILED; 2382 } 2383 2384 spin_unlock_bh(&session->frwd_lock); 2385 mutex_unlock(&session->eh_mutex); 2386 /* 2387 * we drop the lock here but the leadconn cannot be destoyed while 2388 * we are in the scsi eh 2389 */ 2390 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST); 2391 2392 ISCSI_DBG_EH(session, "wait for relogin\n"); 2393 wait_event_interruptible(conn->ehwait, 2394 session->state == ISCSI_STATE_TERMINATE || 2395 session->state == ISCSI_STATE_LOGGED_IN || 2396 session->state == ISCSI_STATE_RECOVERY_FAILED); 2397 if (signal_pending(current)) 2398 flush_signals(current); 2399 2400 mutex_lock(&session->eh_mutex); 2401 spin_lock_bh(&session->frwd_lock); 2402 if (session->state == ISCSI_STATE_LOGGED_IN) { 2403 ISCSI_DBG_EH(session, 2404 "session reset succeeded for %s,%s\n", 2405 session->targetname, conn->persistent_address); 2406 } else 2407 goto failed; 2408 spin_unlock_bh(&session->frwd_lock); 2409 mutex_unlock(&session->eh_mutex); 2410 return SUCCESS; 2411 } 2412 EXPORT_SYMBOL_GPL(iscsi_eh_session_reset); 2413 2414 static void iscsi_prep_tgt_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr) 2415 { 2416 memset(hdr, 0, sizeof(*hdr)); 2417 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE; 2418 hdr->flags = ISCSI_TM_FUNC_TARGET_WARM_RESET & ISCSI_FLAG_TM_FUNC_MASK; 2419 hdr->flags |= ISCSI_FLAG_CMD_FINAL; 2420 hdr->rtt = RESERVED_ITT; 2421 } 2422 2423 /** 2424 * iscsi_eh_target_reset - reset target 2425 * @sc: scsi command 2426 * 2427 * This will attempt to send a warm target reset. 2428 */ 2429 static int iscsi_eh_target_reset(struct scsi_cmnd *sc) 2430 { 2431 struct iscsi_cls_session *cls_session; 2432 struct iscsi_session *session; 2433 struct iscsi_conn *conn; 2434 struct iscsi_tm *hdr; 2435 int rc = FAILED; 2436 2437 cls_session = starget_to_session(scsi_target(sc->device)); 2438 session = cls_session->dd_data; 2439 2440 ISCSI_DBG_EH(session, "tgt Reset [sc %p tgt %s]\n", sc, 2441 session->targetname); 2442 2443 mutex_lock(&session->eh_mutex); 2444 spin_lock_bh(&session->frwd_lock); 2445 /* 2446 * Just check if we are not logged in. We cannot check for 2447 * the phase because the reset could come from a ioctl. 2448 */ 2449 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN) 2450 goto unlock; 2451 conn = session->leadconn; 2452 2453 /* only have one tmf outstanding at a time */ 2454 if (conn->tmf_state != TMF_INITIAL) 2455 goto unlock; 2456 conn->tmf_state = TMF_QUEUED; 2457 2458 hdr = &conn->tmhdr; 2459 iscsi_prep_tgt_reset_pdu(sc, hdr); 2460 2461 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age, 2462 session->tgt_reset_timeout)) { 2463 rc = FAILED; 2464 goto unlock; 2465 } 2466 2467 switch (conn->tmf_state) { 2468 case TMF_SUCCESS: 2469 break; 2470 case TMF_TIMEDOUT: 2471 spin_unlock_bh(&session->frwd_lock); 2472 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST); 2473 goto done; 2474 default: 2475 conn->tmf_state = TMF_INITIAL; 2476 goto unlock; 2477 } 2478 2479 rc = SUCCESS; 2480 spin_unlock_bh(&session->frwd_lock); 2481 2482 iscsi_suspend_tx(conn); 2483 2484 spin_lock_bh(&session->frwd_lock); 2485 memset(hdr, 0, sizeof(*hdr)); 2486 fail_scsi_tasks(conn, -1, DID_ERROR); 2487 conn->tmf_state = TMF_INITIAL; 2488 spin_unlock_bh(&session->frwd_lock); 2489 2490 iscsi_start_tx(conn); 2491 goto done; 2492 2493 unlock: 2494 spin_unlock_bh(&session->frwd_lock); 2495 done: 2496 ISCSI_DBG_EH(session, "tgt %s reset result = %s\n", session->targetname, 2497 rc == SUCCESS ? "SUCCESS" : "FAILED"); 2498 mutex_unlock(&session->eh_mutex); 2499 return rc; 2500 } 2501 2502 /** 2503 * iscsi_eh_recover_target - reset target and possibly the session 2504 * @sc: scsi command 2505 * 2506 * This will attempt to send a warm target reset. If that fails, 2507 * we will escalate to ERL0 session recovery. 2508 */ 2509 int iscsi_eh_recover_target(struct scsi_cmnd *sc) 2510 { 2511 int rc; 2512 2513 rc = iscsi_eh_target_reset(sc); 2514 if (rc == FAILED) 2515 rc = iscsi_eh_session_reset(sc); 2516 return rc; 2517 } 2518 EXPORT_SYMBOL_GPL(iscsi_eh_recover_target); 2519 2520 /* 2521 * Pre-allocate a pool of @max items of @item_size. By default, the pool 2522 * should be accessed via kfifo_{get,put} on q->queue. 2523 * Optionally, the caller can obtain the array of object pointers 2524 * by passing in a non-NULL @items pointer 2525 */ 2526 int 2527 iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size) 2528 { 2529 int i, num_arrays = 1; 2530 2531 memset(q, 0, sizeof(*q)); 2532 2533 q->max = max; 2534 2535 /* If the user passed an items pointer, he wants a copy of 2536 * the array. */ 2537 if (items) 2538 num_arrays++; 2539 q->pool = kvcalloc(num_arrays * max, sizeof(void *), GFP_KERNEL); 2540 if (q->pool == NULL) 2541 return -ENOMEM; 2542 2543 kfifo_init(&q->queue, (void*)q->pool, max * sizeof(void*)); 2544 2545 for (i = 0; i < max; i++) { 2546 q->pool[i] = kzalloc(item_size, GFP_KERNEL); 2547 if (q->pool[i] == NULL) { 2548 q->max = i; 2549 goto enomem; 2550 } 2551 kfifo_in(&q->queue, (void*)&q->pool[i], sizeof(void*)); 2552 } 2553 2554 if (items) { 2555 *items = q->pool + max; 2556 memcpy(*items, q->pool, max * sizeof(void *)); 2557 } 2558 2559 return 0; 2560 2561 enomem: 2562 iscsi_pool_free(q); 2563 return -ENOMEM; 2564 } 2565 EXPORT_SYMBOL_GPL(iscsi_pool_init); 2566 2567 void iscsi_pool_free(struct iscsi_pool *q) 2568 { 2569 int i; 2570 2571 for (i = 0; i < q->max; i++) 2572 kfree(q->pool[i]); 2573 kvfree(q->pool); 2574 } 2575 EXPORT_SYMBOL_GPL(iscsi_pool_free); 2576 2577 /** 2578 * iscsi_host_add - add host to system 2579 * @shost: scsi host 2580 * @pdev: parent device 2581 * 2582 * This should be called by partial offload and software iscsi drivers 2583 * to add a host to the system. 2584 */ 2585 int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev) 2586 { 2587 if (!shost->can_queue) 2588 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX; 2589 2590 if (!shost->cmd_per_lun) 2591 shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN; 2592 2593 return scsi_add_host(shost, pdev); 2594 } 2595 EXPORT_SYMBOL_GPL(iscsi_host_add); 2596 2597 /** 2598 * iscsi_host_alloc - allocate a host and driver data 2599 * @sht: scsi host template 2600 * @dd_data_size: driver host data size 2601 * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue 2602 * 2603 * This should be called by partial offload and software iscsi drivers. 2604 * To access the driver specific memory use the iscsi_host_priv() macro. 2605 */ 2606 struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht, 2607 int dd_data_size, bool xmit_can_sleep) 2608 { 2609 struct Scsi_Host *shost; 2610 struct iscsi_host *ihost; 2611 2612 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size); 2613 if (!shost) 2614 return NULL; 2615 ihost = shost_priv(shost); 2616 2617 if (xmit_can_sleep) { 2618 snprintf(ihost->workq_name, sizeof(ihost->workq_name), 2619 "iscsi_q_%d", shost->host_no); 2620 ihost->workq = create_singlethread_workqueue(ihost->workq_name); 2621 if (!ihost->workq) 2622 goto free_host; 2623 } 2624 2625 spin_lock_init(&ihost->lock); 2626 ihost->state = ISCSI_HOST_SETUP; 2627 ihost->num_sessions = 0; 2628 init_waitqueue_head(&ihost->session_removal_wq); 2629 return shost; 2630 2631 free_host: 2632 scsi_host_put(shost); 2633 return NULL; 2634 } 2635 EXPORT_SYMBOL_GPL(iscsi_host_alloc); 2636 2637 static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session) 2638 { 2639 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST); 2640 } 2641 2642 /** 2643 * iscsi_host_remove - remove host and sessions 2644 * @shost: scsi host 2645 * 2646 * If there are any sessions left, this will initiate the removal and wait 2647 * for the completion. 2648 */ 2649 void iscsi_host_remove(struct Scsi_Host *shost) 2650 { 2651 struct iscsi_host *ihost = shost_priv(shost); 2652 unsigned long flags; 2653 2654 spin_lock_irqsave(&ihost->lock, flags); 2655 ihost->state = ISCSI_HOST_REMOVED; 2656 spin_unlock_irqrestore(&ihost->lock, flags); 2657 2658 iscsi_host_for_each_session(shost, iscsi_notify_host_removed); 2659 wait_event_interruptible(ihost->session_removal_wq, 2660 ihost->num_sessions == 0); 2661 if (signal_pending(current)) 2662 flush_signals(current); 2663 2664 scsi_remove_host(shost); 2665 if (ihost->workq) 2666 destroy_workqueue(ihost->workq); 2667 } 2668 EXPORT_SYMBOL_GPL(iscsi_host_remove); 2669 2670 void iscsi_host_free(struct Scsi_Host *shost) 2671 { 2672 struct iscsi_host *ihost = shost_priv(shost); 2673 2674 kfree(ihost->netdev); 2675 kfree(ihost->hwaddress); 2676 kfree(ihost->initiatorname); 2677 scsi_host_put(shost); 2678 } 2679 EXPORT_SYMBOL_GPL(iscsi_host_free); 2680 2681 static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost) 2682 { 2683 struct iscsi_host *ihost = shost_priv(shost); 2684 unsigned long flags; 2685 2686 shost = scsi_host_get(shost); 2687 if (!shost) { 2688 printk(KERN_ERR "Invalid state. Cannot notify host removal " 2689 "of session teardown event because host already " 2690 "removed.\n"); 2691 return; 2692 } 2693 2694 spin_lock_irqsave(&ihost->lock, flags); 2695 ihost->num_sessions--; 2696 if (ihost->num_sessions == 0) 2697 wake_up(&ihost->session_removal_wq); 2698 spin_unlock_irqrestore(&ihost->lock, flags); 2699 scsi_host_put(shost); 2700 } 2701 2702 /** 2703 * iscsi_session_setup - create iscsi cls session and host and session 2704 * @iscsit: iscsi transport template 2705 * @shost: scsi host 2706 * @cmds_max: session can queue 2707 * @dd_size: private driver data size, added to session allocation size 2708 * @cmd_task_size: LLD task private data size 2709 * @initial_cmdsn: initial CmdSN 2710 * @id: target ID to add to this session 2711 * 2712 * This can be used by software iscsi_transports that allocate 2713 * a session per scsi host. 2714 * 2715 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of 2716 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks 2717 * for nop handling and login/logout requests. 2718 */ 2719 struct iscsi_cls_session * 2720 iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost, 2721 uint16_t cmds_max, int dd_size, int cmd_task_size, 2722 uint32_t initial_cmdsn, unsigned int id) 2723 { 2724 struct iscsi_host *ihost = shost_priv(shost); 2725 struct iscsi_session *session; 2726 struct iscsi_cls_session *cls_session; 2727 int cmd_i, scsi_cmds, total_cmds = cmds_max; 2728 unsigned long flags; 2729 2730 spin_lock_irqsave(&ihost->lock, flags); 2731 if (ihost->state == ISCSI_HOST_REMOVED) { 2732 spin_unlock_irqrestore(&ihost->lock, flags); 2733 return NULL; 2734 } 2735 ihost->num_sessions++; 2736 spin_unlock_irqrestore(&ihost->lock, flags); 2737 2738 if (!total_cmds) 2739 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX; 2740 /* 2741 * The iscsi layer needs some tasks for nop handling and tmfs, 2742 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX 2743 * + 1 command for scsi IO. 2744 */ 2745 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) { 2746 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue " 2747 "must be a power of two that is at least %d.\n", 2748 total_cmds, ISCSI_TOTAL_CMDS_MIN); 2749 goto dec_session_count; 2750 } 2751 2752 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) { 2753 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue " 2754 "must be a power of 2 less than or equal to %d.\n", 2755 cmds_max, ISCSI_TOTAL_CMDS_MAX); 2756 total_cmds = ISCSI_TOTAL_CMDS_MAX; 2757 } 2758 2759 if (!is_power_of_2(total_cmds)) { 2760 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue " 2761 "must be a power of 2.\n", total_cmds); 2762 total_cmds = rounddown_pow_of_two(total_cmds); 2763 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) 2764 return NULL; 2765 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n", 2766 total_cmds); 2767 } 2768 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX; 2769 2770 cls_session = iscsi_alloc_session(shost, iscsit, 2771 sizeof(struct iscsi_session) + 2772 dd_size); 2773 if (!cls_session) 2774 goto dec_session_count; 2775 session = cls_session->dd_data; 2776 session->cls_session = cls_session; 2777 session->host = shost; 2778 session->state = ISCSI_STATE_FREE; 2779 session->fast_abort = 1; 2780 session->tgt_reset_timeout = 30; 2781 session->lu_reset_timeout = 15; 2782 session->abort_timeout = 10; 2783 session->scsi_cmds_max = scsi_cmds; 2784 session->cmds_max = total_cmds; 2785 session->queued_cmdsn = session->cmdsn = initial_cmdsn; 2786 session->exp_cmdsn = initial_cmdsn + 1; 2787 session->max_cmdsn = initial_cmdsn + 1; 2788 session->max_r2t = 1; 2789 session->tt = iscsit; 2790 session->dd_data = cls_session->dd_data + sizeof(*session); 2791 2792 mutex_init(&session->eh_mutex); 2793 spin_lock_init(&session->frwd_lock); 2794 spin_lock_init(&session->back_lock); 2795 2796 /* initialize SCSI PDU commands pool */ 2797 if (iscsi_pool_init(&session->cmdpool, session->cmds_max, 2798 (void***)&session->cmds, 2799 cmd_task_size + sizeof(struct iscsi_task))) 2800 goto cmdpool_alloc_fail; 2801 2802 /* pre-format cmds pool with ITT */ 2803 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) { 2804 struct iscsi_task *task = session->cmds[cmd_i]; 2805 2806 if (cmd_task_size) 2807 task->dd_data = &task[1]; 2808 task->itt = cmd_i; 2809 task->state = ISCSI_TASK_FREE; 2810 INIT_LIST_HEAD(&task->running); 2811 } 2812 2813 if (!try_module_get(iscsit->owner)) 2814 goto module_get_fail; 2815 2816 if (iscsi_add_session(cls_session, id)) 2817 goto cls_session_fail; 2818 2819 return cls_session; 2820 2821 cls_session_fail: 2822 module_put(iscsit->owner); 2823 module_get_fail: 2824 iscsi_pool_free(&session->cmdpool); 2825 cmdpool_alloc_fail: 2826 iscsi_free_session(cls_session); 2827 dec_session_count: 2828 iscsi_host_dec_session_cnt(shost); 2829 return NULL; 2830 } 2831 EXPORT_SYMBOL_GPL(iscsi_session_setup); 2832 2833 /** 2834 * iscsi_session_teardown - destroy session, host, and cls_session 2835 * @cls_session: iscsi session 2836 */ 2837 void iscsi_session_teardown(struct iscsi_cls_session *cls_session) 2838 { 2839 struct iscsi_session *session = cls_session->dd_data; 2840 struct module *owner = cls_session->transport->owner; 2841 struct Scsi_Host *shost = session->host; 2842 2843 iscsi_pool_free(&session->cmdpool); 2844 2845 iscsi_remove_session(cls_session); 2846 2847 kfree(session->password); 2848 kfree(session->password_in); 2849 kfree(session->username); 2850 kfree(session->username_in); 2851 kfree(session->targetname); 2852 kfree(session->targetalias); 2853 kfree(session->initiatorname); 2854 kfree(session->boot_root); 2855 kfree(session->boot_nic); 2856 kfree(session->boot_target); 2857 kfree(session->ifacename); 2858 kfree(session->portal_type); 2859 kfree(session->discovery_parent_type); 2860 2861 iscsi_free_session(cls_session); 2862 2863 iscsi_host_dec_session_cnt(shost); 2864 module_put(owner); 2865 } 2866 EXPORT_SYMBOL_GPL(iscsi_session_teardown); 2867 2868 /** 2869 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn 2870 * @cls_session: iscsi_cls_session 2871 * @dd_size: private driver data size 2872 * @conn_idx: cid 2873 */ 2874 struct iscsi_cls_conn * 2875 iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size, 2876 uint32_t conn_idx) 2877 { 2878 struct iscsi_session *session = cls_session->dd_data; 2879 struct iscsi_conn *conn; 2880 struct iscsi_cls_conn *cls_conn; 2881 char *data; 2882 2883 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size, 2884 conn_idx); 2885 if (!cls_conn) 2886 return NULL; 2887 conn = cls_conn->dd_data; 2888 memset(conn, 0, sizeof(*conn) + dd_size); 2889 2890 conn->dd_data = cls_conn->dd_data + sizeof(*conn); 2891 conn->session = session; 2892 conn->cls_conn = cls_conn; 2893 conn->c_stage = ISCSI_CONN_INITIAL_STAGE; 2894 conn->id = conn_idx; 2895 conn->exp_statsn = 0; 2896 conn->tmf_state = TMF_INITIAL; 2897 2898 timer_setup(&conn->transport_timer, iscsi_check_transport_timeouts, 0); 2899 2900 INIT_LIST_HEAD(&conn->mgmtqueue); 2901 INIT_LIST_HEAD(&conn->cmdqueue); 2902 INIT_LIST_HEAD(&conn->requeue); 2903 spin_lock_init(&conn->taskqueuelock); 2904 INIT_WORK(&conn->xmitwork, iscsi_xmitworker); 2905 2906 /* allocate login_task used for the login/text sequences */ 2907 spin_lock_bh(&session->frwd_lock); 2908 if (!kfifo_out(&session->cmdpool.queue, 2909 (void*)&conn->login_task, 2910 sizeof(void*))) { 2911 spin_unlock_bh(&session->frwd_lock); 2912 goto login_task_alloc_fail; 2913 } 2914 spin_unlock_bh(&session->frwd_lock); 2915 2916 data = (char *) __get_free_pages(GFP_KERNEL, 2917 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN)); 2918 if (!data) 2919 goto login_task_data_alloc_fail; 2920 conn->login_task->data = conn->data = data; 2921 2922 timer_setup(&conn->tmf_timer, iscsi_tmf_timedout, 0); 2923 init_waitqueue_head(&conn->ehwait); 2924 2925 return cls_conn; 2926 2927 login_task_data_alloc_fail: 2928 kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task, 2929 sizeof(void*)); 2930 login_task_alloc_fail: 2931 iscsi_destroy_conn(cls_conn); 2932 return NULL; 2933 } 2934 EXPORT_SYMBOL_GPL(iscsi_conn_setup); 2935 2936 /** 2937 * iscsi_conn_teardown - teardown iscsi connection 2938 * @cls_conn: iscsi class connection 2939 * 2940 * TODO: we may need to make this into a two step process 2941 * like scsi-mls remove + put host 2942 */ 2943 void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn) 2944 { 2945 struct iscsi_conn *conn = cls_conn->dd_data; 2946 struct iscsi_session *session = conn->session; 2947 2948 del_timer_sync(&conn->transport_timer); 2949 2950 mutex_lock(&session->eh_mutex); 2951 spin_lock_bh(&session->frwd_lock); 2952 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT; 2953 if (session->leadconn == conn) { 2954 /* 2955 * leading connection? then give up on recovery. 2956 */ 2957 session->state = ISCSI_STATE_TERMINATE; 2958 wake_up(&conn->ehwait); 2959 } 2960 spin_unlock_bh(&session->frwd_lock); 2961 2962 /* flush queued up work because we free the connection below */ 2963 iscsi_suspend_tx(conn); 2964 2965 spin_lock_bh(&session->frwd_lock); 2966 free_pages((unsigned long) conn->data, 2967 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN)); 2968 kfree(conn->persistent_address); 2969 kfree(conn->local_ipaddr); 2970 /* regular RX path uses back_lock */ 2971 spin_lock_bh(&session->back_lock); 2972 kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task, 2973 sizeof(void*)); 2974 spin_unlock_bh(&session->back_lock); 2975 if (session->leadconn == conn) 2976 session->leadconn = NULL; 2977 spin_unlock_bh(&session->frwd_lock); 2978 mutex_unlock(&session->eh_mutex); 2979 2980 iscsi_destroy_conn(cls_conn); 2981 } 2982 EXPORT_SYMBOL_GPL(iscsi_conn_teardown); 2983 2984 int iscsi_conn_start(struct iscsi_cls_conn *cls_conn) 2985 { 2986 struct iscsi_conn *conn = cls_conn->dd_data; 2987 struct iscsi_session *session = conn->session; 2988 2989 if (!session) { 2990 iscsi_conn_printk(KERN_ERR, conn, 2991 "can't start unbound connection\n"); 2992 return -EPERM; 2993 } 2994 2995 if ((session->imm_data_en || !session->initial_r2t_en) && 2996 session->first_burst > session->max_burst) { 2997 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: " 2998 "first_burst %d max_burst %d\n", 2999 session->first_burst, session->max_burst); 3000 return -EINVAL; 3001 } 3002 3003 if (conn->ping_timeout && !conn->recv_timeout) { 3004 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of " 3005 "zero. Using 5 seconds\n."); 3006 conn->recv_timeout = 5; 3007 } 3008 3009 if (conn->recv_timeout && !conn->ping_timeout) { 3010 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of " 3011 "zero. Using 5 seconds.\n"); 3012 conn->ping_timeout = 5; 3013 } 3014 3015 spin_lock_bh(&session->frwd_lock); 3016 conn->c_stage = ISCSI_CONN_STARTED; 3017 session->state = ISCSI_STATE_LOGGED_IN; 3018 session->queued_cmdsn = session->cmdsn; 3019 3020 conn->last_recv = jiffies; 3021 conn->last_ping = jiffies; 3022 if (conn->recv_timeout && conn->ping_timeout) 3023 mod_timer(&conn->transport_timer, 3024 jiffies + (conn->recv_timeout * HZ)); 3025 3026 switch(conn->stop_stage) { 3027 case STOP_CONN_RECOVER: 3028 /* 3029 * unblock eh_abort() if it is blocked. re-try all 3030 * commands after successful recovery 3031 */ 3032 conn->stop_stage = 0; 3033 conn->tmf_state = TMF_INITIAL; 3034 session->age++; 3035 if (session->age == 16) 3036 session->age = 0; 3037 break; 3038 case STOP_CONN_TERM: 3039 conn->stop_stage = 0; 3040 break; 3041 default: 3042 break; 3043 } 3044 spin_unlock_bh(&session->frwd_lock); 3045 3046 iscsi_unblock_session(session->cls_session); 3047 wake_up(&conn->ehwait); 3048 return 0; 3049 } 3050 EXPORT_SYMBOL_GPL(iscsi_conn_start); 3051 3052 static void 3053 fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn) 3054 { 3055 struct iscsi_task *task; 3056 int i, state; 3057 3058 for (i = 0; i < conn->session->cmds_max; i++) { 3059 task = conn->session->cmds[i]; 3060 if (task->sc) 3061 continue; 3062 3063 if (task->state == ISCSI_TASK_FREE) 3064 continue; 3065 3066 ISCSI_DBG_SESSION(conn->session, 3067 "failing mgmt itt 0x%x state %d\n", 3068 task->itt, task->state); 3069 state = ISCSI_TASK_ABRT_SESS_RECOV; 3070 if (task->state == ISCSI_TASK_PENDING) 3071 state = ISCSI_TASK_COMPLETED; 3072 iscsi_complete_task(task, state); 3073 3074 } 3075 } 3076 3077 static void iscsi_start_session_recovery(struct iscsi_session *session, 3078 struct iscsi_conn *conn, int flag) 3079 { 3080 int old_stop_stage; 3081 3082 mutex_lock(&session->eh_mutex); 3083 spin_lock_bh(&session->frwd_lock); 3084 if (conn->stop_stage == STOP_CONN_TERM) { 3085 spin_unlock_bh(&session->frwd_lock); 3086 mutex_unlock(&session->eh_mutex); 3087 return; 3088 } 3089 3090 /* 3091 * When this is called for the in_login state, we only want to clean 3092 * up the login task and connection. We do not need to block and set 3093 * the recovery state again 3094 */ 3095 if (flag == STOP_CONN_TERM) 3096 session->state = ISCSI_STATE_TERMINATE; 3097 else if (conn->stop_stage != STOP_CONN_RECOVER) 3098 session->state = ISCSI_STATE_IN_RECOVERY; 3099 3100 old_stop_stage = conn->stop_stage; 3101 conn->stop_stage = flag; 3102 spin_unlock_bh(&session->frwd_lock); 3103 3104 del_timer_sync(&conn->transport_timer); 3105 iscsi_suspend_tx(conn); 3106 3107 spin_lock_bh(&session->frwd_lock); 3108 conn->c_stage = ISCSI_CONN_STOPPED; 3109 spin_unlock_bh(&session->frwd_lock); 3110 3111 /* 3112 * for connection level recovery we should not calculate 3113 * header digest. conn->hdr_size used for optimization 3114 * in hdr_extract() and will be re-negotiated at 3115 * set_param() time. 3116 */ 3117 if (flag == STOP_CONN_RECOVER) { 3118 conn->hdrdgst_en = 0; 3119 conn->datadgst_en = 0; 3120 if (session->state == ISCSI_STATE_IN_RECOVERY && 3121 old_stop_stage != STOP_CONN_RECOVER) { 3122 ISCSI_DBG_SESSION(session, "blocking session\n"); 3123 iscsi_block_session(session->cls_session); 3124 } 3125 } 3126 3127 /* 3128 * flush queues. 3129 */ 3130 spin_lock_bh(&session->frwd_lock); 3131 fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED); 3132 fail_mgmt_tasks(session, conn); 3133 memset(&conn->tmhdr, 0, sizeof(conn->tmhdr)); 3134 spin_unlock_bh(&session->frwd_lock); 3135 mutex_unlock(&session->eh_mutex); 3136 } 3137 3138 void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag) 3139 { 3140 struct iscsi_conn *conn = cls_conn->dd_data; 3141 struct iscsi_session *session = conn->session; 3142 3143 switch (flag) { 3144 case STOP_CONN_RECOVER: 3145 case STOP_CONN_TERM: 3146 iscsi_start_session_recovery(session, conn, flag); 3147 break; 3148 default: 3149 iscsi_conn_printk(KERN_ERR, conn, 3150 "invalid stop flag %d\n", flag); 3151 } 3152 } 3153 EXPORT_SYMBOL_GPL(iscsi_conn_stop); 3154 3155 int iscsi_conn_bind(struct iscsi_cls_session *cls_session, 3156 struct iscsi_cls_conn *cls_conn, int is_leading) 3157 { 3158 struct iscsi_session *session = cls_session->dd_data; 3159 struct iscsi_conn *conn = cls_conn->dd_data; 3160 3161 spin_lock_bh(&session->frwd_lock); 3162 if (is_leading) 3163 session->leadconn = conn; 3164 spin_unlock_bh(&session->frwd_lock); 3165 3166 /* 3167 * Unblock xmitworker(), Login Phase will pass through. 3168 */ 3169 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx); 3170 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); 3171 return 0; 3172 } 3173 EXPORT_SYMBOL_GPL(iscsi_conn_bind); 3174 3175 int iscsi_switch_str_param(char **param, char *new_val_buf) 3176 { 3177 char *new_val; 3178 3179 if (*param) { 3180 if (!strcmp(*param, new_val_buf)) 3181 return 0; 3182 } 3183 3184 new_val = kstrdup(new_val_buf, GFP_NOIO); 3185 if (!new_val) 3186 return -ENOMEM; 3187 3188 kfree(*param); 3189 *param = new_val; 3190 return 0; 3191 } 3192 EXPORT_SYMBOL_GPL(iscsi_switch_str_param); 3193 3194 int iscsi_set_param(struct iscsi_cls_conn *cls_conn, 3195 enum iscsi_param param, char *buf, int buflen) 3196 { 3197 struct iscsi_conn *conn = cls_conn->dd_data; 3198 struct iscsi_session *session = conn->session; 3199 int val; 3200 3201 switch(param) { 3202 case ISCSI_PARAM_FAST_ABORT: 3203 sscanf(buf, "%d", &session->fast_abort); 3204 break; 3205 case ISCSI_PARAM_ABORT_TMO: 3206 sscanf(buf, "%d", &session->abort_timeout); 3207 break; 3208 case ISCSI_PARAM_LU_RESET_TMO: 3209 sscanf(buf, "%d", &session->lu_reset_timeout); 3210 break; 3211 case ISCSI_PARAM_TGT_RESET_TMO: 3212 sscanf(buf, "%d", &session->tgt_reset_timeout); 3213 break; 3214 case ISCSI_PARAM_PING_TMO: 3215 sscanf(buf, "%d", &conn->ping_timeout); 3216 break; 3217 case ISCSI_PARAM_RECV_TMO: 3218 sscanf(buf, "%d", &conn->recv_timeout); 3219 break; 3220 case ISCSI_PARAM_MAX_RECV_DLENGTH: 3221 sscanf(buf, "%d", &conn->max_recv_dlength); 3222 break; 3223 case ISCSI_PARAM_MAX_XMIT_DLENGTH: 3224 sscanf(buf, "%d", &conn->max_xmit_dlength); 3225 break; 3226 case ISCSI_PARAM_HDRDGST_EN: 3227 sscanf(buf, "%d", &conn->hdrdgst_en); 3228 break; 3229 case ISCSI_PARAM_DATADGST_EN: 3230 sscanf(buf, "%d", &conn->datadgst_en); 3231 break; 3232 case ISCSI_PARAM_INITIAL_R2T_EN: 3233 sscanf(buf, "%d", &session->initial_r2t_en); 3234 break; 3235 case ISCSI_PARAM_MAX_R2T: 3236 sscanf(buf, "%hu", &session->max_r2t); 3237 break; 3238 case ISCSI_PARAM_IMM_DATA_EN: 3239 sscanf(buf, "%d", &session->imm_data_en); 3240 break; 3241 case ISCSI_PARAM_FIRST_BURST: 3242 sscanf(buf, "%d", &session->first_burst); 3243 break; 3244 case ISCSI_PARAM_MAX_BURST: 3245 sscanf(buf, "%d", &session->max_burst); 3246 break; 3247 case ISCSI_PARAM_PDU_INORDER_EN: 3248 sscanf(buf, "%d", &session->pdu_inorder_en); 3249 break; 3250 case ISCSI_PARAM_DATASEQ_INORDER_EN: 3251 sscanf(buf, "%d", &session->dataseq_inorder_en); 3252 break; 3253 case ISCSI_PARAM_ERL: 3254 sscanf(buf, "%d", &session->erl); 3255 break; 3256 case ISCSI_PARAM_EXP_STATSN: 3257 sscanf(buf, "%u", &conn->exp_statsn); 3258 break; 3259 case ISCSI_PARAM_USERNAME: 3260 return iscsi_switch_str_param(&session->username, buf); 3261 case ISCSI_PARAM_USERNAME_IN: 3262 return iscsi_switch_str_param(&session->username_in, buf); 3263 case ISCSI_PARAM_PASSWORD: 3264 return iscsi_switch_str_param(&session->password, buf); 3265 case ISCSI_PARAM_PASSWORD_IN: 3266 return iscsi_switch_str_param(&session->password_in, buf); 3267 case ISCSI_PARAM_TARGET_NAME: 3268 return iscsi_switch_str_param(&session->targetname, buf); 3269 case ISCSI_PARAM_TARGET_ALIAS: 3270 return iscsi_switch_str_param(&session->targetalias, buf); 3271 case ISCSI_PARAM_TPGT: 3272 sscanf(buf, "%d", &session->tpgt); 3273 break; 3274 case ISCSI_PARAM_PERSISTENT_PORT: 3275 sscanf(buf, "%d", &conn->persistent_port); 3276 break; 3277 case ISCSI_PARAM_PERSISTENT_ADDRESS: 3278 return iscsi_switch_str_param(&conn->persistent_address, buf); 3279 case ISCSI_PARAM_IFACE_NAME: 3280 return iscsi_switch_str_param(&session->ifacename, buf); 3281 case ISCSI_PARAM_INITIATOR_NAME: 3282 return iscsi_switch_str_param(&session->initiatorname, buf); 3283 case ISCSI_PARAM_BOOT_ROOT: 3284 return iscsi_switch_str_param(&session->boot_root, buf); 3285 case ISCSI_PARAM_BOOT_NIC: 3286 return iscsi_switch_str_param(&session->boot_nic, buf); 3287 case ISCSI_PARAM_BOOT_TARGET: 3288 return iscsi_switch_str_param(&session->boot_target, buf); 3289 case ISCSI_PARAM_PORTAL_TYPE: 3290 return iscsi_switch_str_param(&session->portal_type, buf); 3291 case ISCSI_PARAM_DISCOVERY_PARENT_TYPE: 3292 return iscsi_switch_str_param(&session->discovery_parent_type, 3293 buf); 3294 case ISCSI_PARAM_DISCOVERY_SESS: 3295 sscanf(buf, "%d", &val); 3296 session->discovery_sess = !!val; 3297 break; 3298 case ISCSI_PARAM_LOCAL_IPADDR: 3299 return iscsi_switch_str_param(&conn->local_ipaddr, buf); 3300 default: 3301 return -ENOSYS; 3302 } 3303 3304 return 0; 3305 } 3306 EXPORT_SYMBOL_GPL(iscsi_set_param); 3307 3308 int iscsi_session_get_param(struct iscsi_cls_session *cls_session, 3309 enum iscsi_param param, char *buf) 3310 { 3311 struct iscsi_session *session = cls_session->dd_data; 3312 int len; 3313 3314 switch(param) { 3315 case ISCSI_PARAM_FAST_ABORT: 3316 len = sprintf(buf, "%d\n", session->fast_abort); 3317 break; 3318 case ISCSI_PARAM_ABORT_TMO: 3319 len = sprintf(buf, "%d\n", session->abort_timeout); 3320 break; 3321 case ISCSI_PARAM_LU_RESET_TMO: 3322 len = sprintf(buf, "%d\n", session->lu_reset_timeout); 3323 break; 3324 case ISCSI_PARAM_TGT_RESET_TMO: 3325 len = sprintf(buf, "%d\n", session->tgt_reset_timeout); 3326 break; 3327 case ISCSI_PARAM_INITIAL_R2T_EN: 3328 len = sprintf(buf, "%d\n", session->initial_r2t_en); 3329 break; 3330 case ISCSI_PARAM_MAX_R2T: 3331 len = sprintf(buf, "%hu\n", session->max_r2t); 3332 break; 3333 case ISCSI_PARAM_IMM_DATA_EN: 3334 len = sprintf(buf, "%d\n", session->imm_data_en); 3335 break; 3336 case ISCSI_PARAM_FIRST_BURST: 3337 len = sprintf(buf, "%u\n", session->first_burst); 3338 break; 3339 case ISCSI_PARAM_MAX_BURST: 3340 len = sprintf(buf, "%u\n", session->max_burst); 3341 break; 3342 case ISCSI_PARAM_PDU_INORDER_EN: 3343 len = sprintf(buf, "%d\n", session->pdu_inorder_en); 3344 break; 3345 case ISCSI_PARAM_DATASEQ_INORDER_EN: 3346 len = sprintf(buf, "%d\n", session->dataseq_inorder_en); 3347 break; 3348 case ISCSI_PARAM_DEF_TASKMGMT_TMO: 3349 len = sprintf(buf, "%d\n", session->def_taskmgmt_tmo); 3350 break; 3351 case ISCSI_PARAM_ERL: 3352 len = sprintf(buf, "%d\n", session->erl); 3353 break; 3354 case ISCSI_PARAM_TARGET_NAME: 3355 len = sprintf(buf, "%s\n", session->targetname); 3356 break; 3357 case ISCSI_PARAM_TARGET_ALIAS: 3358 len = sprintf(buf, "%s\n", session->targetalias); 3359 break; 3360 case ISCSI_PARAM_TPGT: 3361 len = sprintf(buf, "%d\n", session->tpgt); 3362 break; 3363 case ISCSI_PARAM_USERNAME: 3364 len = sprintf(buf, "%s\n", session->username); 3365 break; 3366 case ISCSI_PARAM_USERNAME_IN: 3367 len = sprintf(buf, "%s\n", session->username_in); 3368 break; 3369 case ISCSI_PARAM_PASSWORD: 3370 len = sprintf(buf, "%s\n", session->password); 3371 break; 3372 case ISCSI_PARAM_PASSWORD_IN: 3373 len = sprintf(buf, "%s\n", session->password_in); 3374 break; 3375 case ISCSI_PARAM_IFACE_NAME: 3376 len = sprintf(buf, "%s\n", session->ifacename); 3377 break; 3378 case ISCSI_PARAM_INITIATOR_NAME: 3379 len = sprintf(buf, "%s\n", session->initiatorname); 3380 break; 3381 case ISCSI_PARAM_BOOT_ROOT: 3382 len = sprintf(buf, "%s\n", session->boot_root); 3383 break; 3384 case ISCSI_PARAM_BOOT_NIC: 3385 len = sprintf(buf, "%s\n", session->boot_nic); 3386 break; 3387 case ISCSI_PARAM_BOOT_TARGET: 3388 len = sprintf(buf, "%s\n", session->boot_target); 3389 break; 3390 case ISCSI_PARAM_AUTO_SND_TGT_DISABLE: 3391 len = sprintf(buf, "%u\n", session->auto_snd_tgt_disable); 3392 break; 3393 case ISCSI_PARAM_DISCOVERY_SESS: 3394 len = sprintf(buf, "%u\n", session->discovery_sess); 3395 break; 3396 case ISCSI_PARAM_PORTAL_TYPE: 3397 len = sprintf(buf, "%s\n", session->portal_type); 3398 break; 3399 case ISCSI_PARAM_CHAP_AUTH_EN: 3400 len = sprintf(buf, "%u\n", session->chap_auth_en); 3401 break; 3402 case ISCSI_PARAM_DISCOVERY_LOGOUT_EN: 3403 len = sprintf(buf, "%u\n", session->discovery_logout_en); 3404 break; 3405 case ISCSI_PARAM_BIDI_CHAP_EN: 3406 len = sprintf(buf, "%u\n", session->bidi_chap_en); 3407 break; 3408 case ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL: 3409 len = sprintf(buf, "%u\n", session->discovery_auth_optional); 3410 break; 3411 case ISCSI_PARAM_DEF_TIME2WAIT: 3412 len = sprintf(buf, "%d\n", session->time2wait); 3413 break; 3414 case ISCSI_PARAM_DEF_TIME2RETAIN: 3415 len = sprintf(buf, "%d\n", session->time2retain); 3416 break; 3417 case ISCSI_PARAM_TSID: 3418 len = sprintf(buf, "%u\n", session->tsid); 3419 break; 3420 case ISCSI_PARAM_ISID: 3421 len = sprintf(buf, "%02x%02x%02x%02x%02x%02x\n", 3422 session->isid[0], session->isid[1], 3423 session->isid[2], session->isid[3], 3424 session->isid[4], session->isid[5]); 3425 break; 3426 case ISCSI_PARAM_DISCOVERY_PARENT_IDX: 3427 len = sprintf(buf, "%u\n", session->discovery_parent_idx); 3428 break; 3429 case ISCSI_PARAM_DISCOVERY_PARENT_TYPE: 3430 if (session->discovery_parent_type) 3431 len = sprintf(buf, "%s\n", 3432 session->discovery_parent_type); 3433 else 3434 len = sprintf(buf, "\n"); 3435 break; 3436 default: 3437 return -ENOSYS; 3438 } 3439 3440 return len; 3441 } 3442 EXPORT_SYMBOL_GPL(iscsi_session_get_param); 3443 3444 int iscsi_conn_get_addr_param(struct sockaddr_storage *addr, 3445 enum iscsi_param param, char *buf) 3446 { 3447 struct sockaddr_in6 *sin6 = NULL; 3448 struct sockaddr_in *sin = NULL; 3449 int len; 3450 3451 switch (addr->ss_family) { 3452 case AF_INET: 3453 sin = (struct sockaddr_in *)addr; 3454 break; 3455 case AF_INET6: 3456 sin6 = (struct sockaddr_in6 *)addr; 3457 break; 3458 default: 3459 return -EINVAL; 3460 } 3461 3462 switch (param) { 3463 case ISCSI_PARAM_CONN_ADDRESS: 3464 case ISCSI_HOST_PARAM_IPADDRESS: 3465 if (sin) 3466 len = sprintf(buf, "%pI4\n", &sin->sin_addr.s_addr); 3467 else 3468 len = sprintf(buf, "%pI6\n", &sin6->sin6_addr); 3469 break; 3470 case ISCSI_PARAM_CONN_PORT: 3471 case ISCSI_PARAM_LOCAL_PORT: 3472 if (sin) 3473 len = sprintf(buf, "%hu\n", be16_to_cpu(sin->sin_port)); 3474 else 3475 len = sprintf(buf, "%hu\n", 3476 be16_to_cpu(sin6->sin6_port)); 3477 break; 3478 default: 3479 return -EINVAL; 3480 } 3481 3482 return len; 3483 } 3484 EXPORT_SYMBOL_GPL(iscsi_conn_get_addr_param); 3485 3486 int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn, 3487 enum iscsi_param param, char *buf) 3488 { 3489 struct iscsi_conn *conn = cls_conn->dd_data; 3490 int len; 3491 3492 switch(param) { 3493 case ISCSI_PARAM_PING_TMO: 3494 len = sprintf(buf, "%u\n", conn->ping_timeout); 3495 break; 3496 case ISCSI_PARAM_RECV_TMO: 3497 len = sprintf(buf, "%u\n", conn->recv_timeout); 3498 break; 3499 case ISCSI_PARAM_MAX_RECV_DLENGTH: 3500 len = sprintf(buf, "%u\n", conn->max_recv_dlength); 3501 break; 3502 case ISCSI_PARAM_MAX_XMIT_DLENGTH: 3503 len = sprintf(buf, "%u\n", conn->max_xmit_dlength); 3504 break; 3505 case ISCSI_PARAM_HDRDGST_EN: 3506 len = sprintf(buf, "%d\n", conn->hdrdgst_en); 3507 break; 3508 case ISCSI_PARAM_DATADGST_EN: 3509 len = sprintf(buf, "%d\n", conn->datadgst_en); 3510 break; 3511 case ISCSI_PARAM_IFMARKER_EN: 3512 len = sprintf(buf, "%d\n", conn->ifmarker_en); 3513 break; 3514 case ISCSI_PARAM_OFMARKER_EN: 3515 len = sprintf(buf, "%d\n", conn->ofmarker_en); 3516 break; 3517 case ISCSI_PARAM_EXP_STATSN: 3518 len = sprintf(buf, "%u\n", conn->exp_statsn); 3519 break; 3520 case ISCSI_PARAM_PERSISTENT_PORT: 3521 len = sprintf(buf, "%d\n", conn->persistent_port); 3522 break; 3523 case ISCSI_PARAM_PERSISTENT_ADDRESS: 3524 len = sprintf(buf, "%s\n", conn->persistent_address); 3525 break; 3526 case ISCSI_PARAM_STATSN: 3527 len = sprintf(buf, "%u\n", conn->statsn); 3528 break; 3529 case ISCSI_PARAM_MAX_SEGMENT_SIZE: 3530 len = sprintf(buf, "%u\n", conn->max_segment_size); 3531 break; 3532 case ISCSI_PARAM_KEEPALIVE_TMO: 3533 len = sprintf(buf, "%u\n", conn->keepalive_tmo); 3534 break; 3535 case ISCSI_PARAM_LOCAL_PORT: 3536 len = sprintf(buf, "%u\n", conn->local_port); 3537 break; 3538 case ISCSI_PARAM_TCP_TIMESTAMP_STAT: 3539 len = sprintf(buf, "%u\n", conn->tcp_timestamp_stat); 3540 break; 3541 case ISCSI_PARAM_TCP_NAGLE_DISABLE: 3542 len = sprintf(buf, "%u\n", conn->tcp_nagle_disable); 3543 break; 3544 case ISCSI_PARAM_TCP_WSF_DISABLE: 3545 len = sprintf(buf, "%u\n", conn->tcp_wsf_disable); 3546 break; 3547 case ISCSI_PARAM_TCP_TIMER_SCALE: 3548 len = sprintf(buf, "%u\n", conn->tcp_timer_scale); 3549 break; 3550 case ISCSI_PARAM_TCP_TIMESTAMP_EN: 3551 len = sprintf(buf, "%u\n", conn->tcp_timestamp_en); 3552 break; 3553 case ISCSI_PARAM_IP_FRAGMENT_DISABLE: 3554 len = sprintf(buf, "%u\n", conn->fragment_disable); 3555 break; 3556 case ISCSI_PARAM_IPV4_TOS: 3557 len = sprintf(buf, "%u\n", conn->ipv4_tos); 3558 break; 3559 case ISCSI_PARAM_IPV6_TC: 3560 len = sprintf(buf, "%u\n", conn->ipv6_traffic_class); 3561 break; 3562 case ISCSI_PARAM_IPV6_FLOW_LABEL: 3563 len = sprintf(buf, "%u\n", conn->ipv6_flow_label); 3564 break; 3565 case ISCSI_PARAM_IS_FW_ASSIGNED_IPV6: 3566 len = sprintf(buf, "%u\n", conn->is_fw_assigned_ipv6); 3567 break; 3568 case ISCSI_PARAM_TCP_XMIT_WSF: 3569 len = sprintf(buf, "%u\n", conn->tcp_xmit_wsf); 3570 break; 3571 case ISCSI_PARAM_TCP_RECV_WSF: 3572 len = sprintf(buf, "%u\n", conn->tcp_recv_wsf); 3573 break; 3574 case ISCSI_PARAM_LOCAL_IPADDR: 3575 len = sprintf(buf, "%s\n", conn->local_ipaddr); 3576 break; 3577 default: 3578 return -ENOSYS; 3579 } 3580 3581 return len; 3582 } 3583 EXPORT_SYMBOL_GPL(iscsi_conn_get_param); 3584 3585 int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param, 3586 char *buf) 3587 { 3588 struct iscsi_host *ihost = shost_priv(shost); 3589 int len; 3590 3591 switch (param) { 3592 case ISCSI_HOST_PARAM_NETDEV_NAME: 3593 len = sprintf(buf, "%s\n", ihost->netdev); 3594 break; 3595 case ISCSI_HOST_PARAM_HWADDRESS: 3596 len = sprintf(buf, "%s\n", ihost->hwaddress); 3597 break; 3598 case ISCSI_HOST_PARAM_INITIATOR_NAME: 3599 len = sprintf(buf, "%s\n", ihost->initiatorname); 3600 break; 3601 default: 3602 return -ENOSYS; 3603 } 3604 3605 return len; 3606 } 3607 EXPORT_SYMBOL_GPL(iscsi_host_get_param); 3608 3609 int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param, 3610 char *buf, int buflen) 3611 { 3612 struct iscsi_host *ihost = shost_priv(shost); 3613 3614 switch (param) { 3615 case ISCSI_HOST_PARAM_NETDEV_NAME: 3616 return iscsi_switch_str_param(&ihost->netdev, buf); 3617 case ISCSI_HOST_PARAM_HWADDRESS: 3618 return iscsi_switch_str_param(&ihost->hwaddress, buf); 3619 case ISCSI_HOST_PARAM_INITIATOR_NAME: 3620 return iscsi_switch_str_param(&ihost->initiatorname, buf); 3621 default: 3622 return -ENOSYS; 3623 } 3624 3625 return 0; 3626 } 3627 EXPORT_SYMBOL_GPL(iscsi_host_set_param); 3628 3629 MODULE_AUTHOR("Mike Christie"); 3630 MODULE_DESCRIPTION("iSCSI library functions"); 3631 MODULE_LICENSE("GPL"); 3632