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