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