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