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