blob: eeba6180711cd35b7080fe379bf7dbf9c4eba8df [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000010 */
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>
David Brazdil0f672f62019-12-10 10:32:29 +000030#include <trace/events/iscsi.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000031
32static int iscsi_dbg_lib_conn;
33module_param_named(debug_libiscsi_conn, iscsi_dbg_lib_conn, int,
34 S_IRUGO | S_IWUSR);
35MODULE_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
39static int iscsi_dbg_lib_session;
40module_param_named(debug_libiscsi_session, iscsi_dbg_lib_session, int,
41 S_IRUGO | S_IWUSR);
42MODULE_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
46static int iscsi_dbg_lib_eh;
47module_param_named(debug_libiscsi_eh, iscsi_dbg_lib_eh, int,
48 S_IRUGO | S_IWUSR);
49MODULE_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); \
David Brazdil0f672f62019-12-10 10:32:29 +000059 iscsi_dbg_trace(trace_iscsi_dbg_conn, \
60 &(_conn)->cls_conn->dev, \
61 "%s " dbg_fmt, __func__, ##arg);\
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000062 } 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); \
David Brazdil0f672f62019-12-10 10:32:29 +000070 iscsi_dbg_trace(trace_iscsi_dbg_session, \
71 &(_session)->cls_session->dev, \
72 "%s " dbg_fmt, __func__, ##arg); \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000073 } 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); \
David Brazdil0f672f62019-12-10 10:32:29 +000081 iscsi_dbg_trace(trace_iscsi_dbg_eh, \
82 &(_session)->cls_session->dev, \
83 "%s " dbg_fmt, __func__, ##arg); \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000084 } while (0);
85
86inline 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}
94EXPORT_SYMBOL_GPL(iscsi_conn_queue_work);
95
96static 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
115void 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}
120EXPORT_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 **/
134void 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}
162EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
163
164static 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 */
181static 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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000218/**
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 */
231static int iscsi_check_tmf_restrictions(struct iscsi_task *task, int opcode)
232{
Olivier Deprez0e641232021-09-23 10:07:05 +0200233 struct iscsi_session *session = task->conn->session;
234 struct iscsi_tm *tmf = &session->tmhdr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000235 u64 hdr_lun;
236
Olivier Deprez0e641232021-09-23 10:07:05 +0200237 if (session->tmf_state == TMF_INITIAL)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000238 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 /* fall through */
252 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
253 /*
254 * Fail all SCSI cmd PDUs
255 */
256 if (opcode != ISCSI_OP_SCSI_DATA_OUT) {
Olivier Deprez0e641232021-09-23 10:07:05 +0200257 iscsi_session_printk(KERN_INFO, session,
258 "task [op %x itt 0x%x/0x%x] rejected.\n",
259 opcode, task->itt, task->hdr_itt);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000260 return -EACCES;
261 }
262 /*
263 * And also all data-out PDUs in response to R2T
264 * if fast_abort is set.
265 */
Olivier Deprez0e641232021-09-23 10:07:05 +0200266 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);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000270 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) {
Olivier Deprez0e641232021-09-23 10:07:05 +0200282 ISCSI_DBG_SESSION(session,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000283 "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 */
302static 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;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000351 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 "
David Brazdil0f672f62019-12-10 10:32:29 +0000425 "itt 0x%x len %d cmdsn %d win %d]\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000426 sc->sc_data_direction == DMA_TO_DEVICE ?
427 "write" : "read", conn->id, sc, sc->cmnd[0],
428 task->itt, transfer_length,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000429 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 */
442static 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
475void __iscsi_get_task(struct iscsi_task *task)
476{
477 refcount_inc(&task->refcount);
478}
479EXPORT_SYMBOL_GPL(__iscsi_get_task);
480
481void __iscsi_put_task(struct iscsi_task *task)
482{
483 if (refcount_dec_and_test(&task->refcount))
484 iscsi_free_task(task);
485}
486EXPORT_SYMBOL_GPL(__iscsi_put_task);
487
488void 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}
497EXPORT_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 */
506static 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 spin_lock_bh(&conn->taskqueuelock);
522 if (!list_empty(&task->running)) {
523 pr_debug_once("%s while task on list", __func__);
524 list_del_init(&task->running);
525 }
526 spin_unlock_bh(&conn->taskqueuelock);
527
528 if (conn->task == task)
529 conn->task = NULL;
530
Olivier Deprez0e641232021-09-23 10:07:05 +0200531 if (READ_ONCE(conn->ping_task) == task)
532 WRITE_ONCE(conn->ping_task, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000533
534 /* release get from queueing */
535 __iscsi_put_task(task);
536}
537
538/**
539 * iscsi_complete_scsi_task - finish scsi task normally
540 * @task: iscsi task for scsi cmd
541 * @exp_cmdsn: expected cmd sn in cpu format
542 * @max_cmdsn: max cmd sn in cpu format
543 *
544 * This is used when drivers do not need or cannot perform
545 * lower level pdu processing.
546 *
547 * Called with session back_lock
548 */
549void iscsi_complete_scsi_task(struct iscsi_task *task,
550 uint32_t exp_cmdsn, uint32_t max_cmdsn)
551{
552 struct iscsi_conn *conn = task->conn;
553
554 ISCSI_DBG_SESSION(conn->session, "[itt 0x%x]\n", task->itt);
555
556 conn->last_recv = jiffies;
557 __iscsi_update_cmdsn(conn->session, exp_cmdsn, max_cmdsn);
558 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
559}
560EXPORT_SYMBOL_GPL(iscsi_complete_scsi_task);
561
562
563/*
564 * session back_lock must be held and if not called for a task that is
565 * still pending or from the xmit thread, then xmit thread must
566 * be suspended.
567 */
568static void fail_scsi_task(struct iscsi_task *task, int err)
569{
570 struct iscsi_conn *conn = task->conn;
571 struct scsi_cmnd *sc;
572 int state;
573
574 /*
575 * if a command completes and we get a successful tmf response
576 * we will hit this because the scsi eh abort code does not take
577 * a ref to the task.
578 */
579 sc = task->sc;
580 if (!sc)
581 return;
582
583 if (task->state == ISCSI_TASK_PENDING) {
584 /*
585 * cmd never made it to the xmit thread, so we should not count
586 * the cmd in the sequencing
587 */
588 conn->session->queued_cmdsn--;
589 /* it was never sent so just complete like normal */
590 state = ISCSI_TASK_COMPLETED;
591 } else if (err == DID_TRANSPORT_DISRUPTED)
592 state = ISCSI_TASK_ABRT_SESS_RECOV;
593 else
594 state = ISCSI_TASK_ABRT_TMF;
595
596 sc->result = err << 16;
David Brazdil0f672f62019-12-10 10:32:29 +0000597 scsi_set_resid(sc, scsi_bufflen(sc));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000598
599 /* regular RX path uses back_lock */
600 spin_lock_bh(&conn->session->back_lock);
601 iscsi_complete_task(task, state);
602 spin_unlock_bh(&conn->session->back_lock);
603}
604
605static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
606 struct iscsi_task *task)
607{
608 struct iscsi_session *session = conn->session;
609 struct iscsi_hdr *hdr = task->hdr;
610 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
611 uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
612
613 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
614 return -ENOTCONN;
615
616 if (opcode != ISCSI_OP_LOGIN && opcode != ISCSI_OP_TEXT)
617 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
618 /*
619 * pre-format CmdSN for outgoing PDU.
620 */
621 nop->cmdsn = cpu_to_be32(session->cmdsn);
622 if (hdr->itt != RESERVED_ITT) {
623 /*
624 * TODO: We always use immediate for normal session pdus.
625 * If we start to send tmfs or nops as non-immediate then
626 * we should start checking the cmdsn numbers for mgmt tasks.
627 *
628 * During discovery sessions iscsid sends TEXT as non immediate,
629 * but we always only send one PDU at a time.
630 */
631 if (conn->c_stage == ISCSI_CONN_STARTED &&
632 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
633 session->queued_cmdsn++;
634 session->cmdsn++;
635 }
636 }
637
638 if (session->tt->init_task && session->tt->init_task(task))
639 return -EIO;
640
641 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
642 session->state = ISCSI_STATE_LOGGING_OUT;
643
644 task->state = ISCSI_TASK_RUNNING;
645 ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
646 "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
647 hdr->itt, task->data_count);
648 return 0;
649}
650
651static struct iscsi_task *
652__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
653 char *data, uint32_t data_size)
654{
655 struct iscsi_session *session = conn->session;
656 struct iscsi_host *ihost = shost_priv(session->host);
657 uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
658 struct iscsi_task *task;
659 itt_t itt;
660
661 if (session->state == ISCSI_STATE_TERMINATE)
662 return NULL;
663
664 if (opcode == ISCSI_OP_LOGIN || opcode == ISCSI_OP_TEXT) {
665 /*
666 * Login and Text are sent serially, in
667 * request-followed-by-response sequence.
668 * Same task can be used. Same ITT must be used.
669 * Note that login_task is preallocated at conn_create().
670 */
671 if (conn->login_task->state != ISCSI_TASK_FREE) {
672 iscsi_conn_printk(KERN_ERR, conn, "Login/Text in "
673 "progress. Cannot start new task.\n");
674 return NULL;
675 }
676
677 if (data_size > ISCSI_DEF_MAX_RECV_SEG_LEN) {
678 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);
679 return NULL;
680 }
681
682 task = conn->login_task;
683 } else {
684 if (session->state != ISCSI_STATE_LOGGED_IN)
685 return NULL;
686
687 if (data_size != 0) {
688 iscsi_conn_printk(KERN_ERR, conn, "Can not send data buffer of len %u for op 0x%x\n", data_size, opcode);
689 return NULL;
690 }
691
692 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
693 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
694
695 if (!kfifo_out(&session->cmdpool.queue,
696 (void*)&task, sizeof(void*)))
697 return NULL;
698 }
699 /*
700 * released in complete pdu for task we expect a response for, and
701 * released by the lld when it has transmitted the task for
702 * pdus we do not expect a response for.
703 */
704 refcount_set(&task->refcount, 1);
705 task->conn = conn;
706 task->sc = NULL;
707 INIT_LIST_HEAD(&task->running);
708 task->state = ISCSI_TASK_PENDING;
709
710 if (data_size) {
711 memcpy(task->data, data, data_size);
712 task->data_count = data_size;
713 } else
714 task->data_count = 0;
715
716 if (conn->session->tt->alloc_pdu) {
717 if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
718 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
719 "pdu for mgmt task.\n");
720 goto free_task;
721 }
722 }
723
724 itt = task->hdr->itt;
725 task->hdr_len = sizeof(struct iscsi_hdr);
726 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
727
728 if (hdr->itt != RESERVED_ITT) {
729 if (session->tt->parse_pdu_itt)
730 task->hdr->itt = itt;
731 else
732 task->hdr->itt = build_itt(task->itt,
733 task->conn->session->age);
734 }
735
Olivier Deprez0e641232021-09-23 10:07:05 +0200736 if (unlikely(READ_ONCE(conn->ping_task) == INVALID_SCSI_TASK))
737 WRITE_ONCE(conn->ping_task, task);
738
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000739 if (!ihost->workq) {
740 if (iscsi_prep_mgmt_task(conn, task))
741 goto free_task;
742
743 if (session->tt->xmit_task(task))
744 goto free_task;
745 } else {
746 spin_lock_bh(&conn->taskqueuelock);
747 list_add_tail(&task->running, &conn->mgmtqueue);
748 spin_unlock_bh(&conn->taskqueuelock);
749 iscsi_conn_queue_work(conn);
750 }
751
752 return task;
753
754free_task:
755 /* regular RX path uses back_lock */
756 spin_lock(&session->back_lock);
757 __iscsi_put_task(task);
758 spin_unlock(&session->back_lock);
759 return NULL;
760}
761
762int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
763 char *data, uint32_t data_size)
764{
765 struct iscsi_conn *conn = cls_conn->dd_data;
766 struct iscsi_session *session = conn->session;
767 int err = 0;
768
769 spin_lock_bh(&session->frwd_lock);
770 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
771 err = -EPERM;
772 spin_unlock_bh(&session->frwd_lock);
773 return err;
774}
775EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
776
777/**
778 * iscsi_cmd_rsp - SCSI Command Response processing
779 * @conn: iscsi connection
780 * @hdr: iscsi header
781 * @task: scsi command task
782 * @data: cmd data buffer
783 * @datalen: len of buffer
784 *
785 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
David Brazdil0f672f62019-12-10 10:32:29 +0000786 * then completes the command and task. called under back_lock
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000787 **/
788static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
789 struct iscsi_task *task, char *data,
790 int datalen)
791{
792 struct iscsi_scsi_rsp *rhdr = (struct iscsi_scsi_rsp *)hdr;
793 struct iscsi_session *session = conn->session;
794 struct scsi_cmnd *sc = task->sc;
795
796 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
797 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
798
799 sc->result = (DID_OK << 16) | rhdr->cmd_status;
800
801 if (task->protected) {
802 sector_t sector;
803 u8 ascq;
804
805 /**
806 * Transports that didn't implement check_protection
807 * callback but still published T10-PI support to scsi-mid
808 * deserve this BUG_ON.
809 **/
810 BUG_ON(!session->tt->check_protection);
811
812 ascq = session->tt->check_protection(task, &sector);
813 if (ascq) {
814 sc->result = DRIVER_SENSE << 24 |
815 SAM_STAT_CHECK_CONDITION;
816 scsi_build_sense_buffer(1, sc->sense_buffer,
817 ILLEGAL_REQUEST, 0x10, ascq);
818 scsi_set_sense_information(sc->sense_buffer,
819 SCSI_SENSE_BUFFERSIZE,
820 sector);
821 goto out;
822 }
823 }
824
825 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
826 sc->result = DID_ERROR << 16;
827 goto out;
828 }
829
830 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
831 uint16_t senselen;
832
833 if (datalen < 2) {
834invalid_datalen:
835 iscsi_conn_printk(KERN_ERR, conn,
836 "Got CHECK_CONDITION but invalid data "
837 "buffer size of %d\n", datalen);
838 sc->result = DID_BAD_TARGET << 16;
839 goto out;
840 }
841
842 senselen = get_unaligned_be16(data);
843 if (datalen < senselen)
844 goto invalid_datalen;
845
846 memcpy(sc->sense_buffer, data + 2,
847 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
848 ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
849 min_t(uint16_t, senselen,
850 SCSI_SENSE_BUFFERSIZE));
851 }
852
853 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
854 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000855 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000856 }
857
858 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
859 ISCSI_FLAG_CMD_OVERFLOW)) {
860 int res_count = be32_to_cpu(rhdr->residual_count);
861
862 if (res_count > 0 &&
863 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
864 res_count <= scsi_bufflen(sc)))
865 /* write side for bidi or uni-io set_resid */
866 scsi_set_resid(sc, res_count);
867 else
868 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
869 }
870out:
871 ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
872 sc, sc->result, task->itt);
873 conn->scsirsp_pdus_cnt++;
874 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
875}
876
877/**
878 * iscsi_data_in_rsp - SCSI Data-In Response processing
879 * @conn: iscsi connection
880 * @hdr: iscsi pdu
881 * @task: scsi command task
David Brazdil0f672f62019-12-10 10:32:29 +0000882 *
883 * iscsi_data_in_rsp sets up the scsi_cmnd fields based on the data received
884 * then completes the command and task. called under back_lock
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000885 **/
886static void
887iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
888 struct iscsi_task *task)
889{
890 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
891 struct scsi_cmnd *sc = task->sc;
892
893 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
894 return;
895
896 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
897 sc->result = (DID_OK << 16) | rhdr->cmd_status;
898 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
899 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
900 ISCSI_FLAG_DATA_OVERFLOW)) {
901 int res_count = be32_to_cpu(rhdr->residual_count);
902
903 if (res_count > 0 &&
904 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
David Brazdil0f672f62019-12-10 10:32:29 +0000905 res_count <= sc->sdb.length))
906 scsi_set_resid(sc, res_count);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000907 else
908 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
909 }
910
911 ISCSI_DBG_SESSION(conn->session, "data in with status done "
912 "[sc %p res %d itt 0x%x]\n",
913 sc, sc->result, task->itt);
914 conn->scsirsp_pdus_cnt++;
915 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
916}
917
918static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
919{
920 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
Olivier Deprez0e641232021-09-23 10:07:05 +0200921 struct iscsi_session *session = conn->session;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000922
923 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
924 conn->tmfrsp_pdus_cnt++;
925
Olivier Deprez0e641232021-09-23 10:07:05 +0200926 if (session->tmf_state != TMF_QUEUED)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000927 return;
928
929 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
Olivier Deprez0e641232021-09-23 10:07:05 +0200930 session->tmf_state = TMF_SUCCESS;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000931 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
Olivier Deprez0e641232021-09-23 10:07:05 +0200932 session->tmf_state = TMF_NOT_FOUND;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000933 else
Olivier Deprez0e641232021-09-23 10:07:05 +0200934 session->tmf_state = TMF_FAILED;
935 wake_up(&session->ehwait);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000936}
937
938static int iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
939{
940 struct iscsi_nopout hdr;
941 struct iscsi_task *task;
942
Olivier Deprez0e641232021-09-23 10:07:05 +0200943 if (!rhdr) {
944 if (READ_ONCE(conn->ping_task))
945 return -EINVAL;
946 WRITE_ONCE(conn->ping_task, INVALID_SCSI_TASK);
947 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000948
949 memset(&hdr, 0, sizeof(struct iscsi_nopout));
950 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
951 hdr.flags = ISCSI_FLAG_CMD_FINAL;
952
953 if (rhdr) {
954 hdr.lun = rhdr->lun;
955 hdr.ttt = rhdr->ttt;
956 hdr.itt = RESERVED_ITT;
957 } else
958 hdr.ttt = RESERVED_ITT;
959
960 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
961 if (!task) {
Olivier Deprez0e641232021-09-23 10:07:05 +0200962 if (!rhdr)
963 WRITE_ONCE(conn->ping_task, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000964 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
965 return -EIO;
966 } else if (!rhdr) {
967 /* only track our nops */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000968 conn->last_ping = jiffies;
969 }
970
971 return 0;
972}
973
David Brazdil0f672f62019-12-10 10:32:29 +0000974/**
975 * iscsi_nop_out_rsp - SCSI NOP Response processing
976 * @task: scsi command task
977 * @nop: the nop structure
978 * @data: where to put the data
979 * @datalen: length of data
980 *
981 * iscsi_nop_out_rsp handles nop response from use or
982 * from user space. called under back_lock
983 **/
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000984static int iscsi_nop_out_rsp(struct iscsi_task *task,
985 struct iscsi_nopin *nop, char *data, int datalen)
986{
987 struct iscsi_conn *conn = task->conn;
988 int rc = 0;
989
Olivier Deprez0e641232021-09-23 10:07:05 +0200990 if (READ_ONCE(conn->ping_task) != task) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000991 /*
992 * If this is not in response to one of our
993 * nops then it must be from userspace.
994 */
995 if (iscsi_recv_pdu(conn->cls_conn, (struct iscsi_hdr *)nop,
996 data, datalen))
997 rc = ISCSI_ERR_CONN_FAILED;
998 } else
999 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
1000 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1001 return rc;
1002}
1003
1004static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1005 char *data, int datalen)
1006{
1007 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
1008 struct iscsi_hdr rejected_pdu;
1009 int opcode, rc = 0;
1010
1011 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
1012
1013 if (ntoh24(reject->dlength) > datalen ||
1014 ntoh24(reject->dlength) < sizeof(struct iscsi_hdr)) {
1015 iscsi_conn_printk(KERN_ERR, conn, "Cannot handle rejected "
1016 "pdu. Invalid data length (pdu dlength "
1017 "%u, datalen %d\n", ntoh24(reject->dlength),
1018 datalen);
1019 return ISCSI_ERR_PROTO;
1020 }
1021 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
1022 opcode = rejected_pdu.opcode & ISCSI_OPCODE_MASK;
1023
1024 switch (reject->reason) {
1025 case ISCSI_REASON_DATA_DIGEST_ERROR:
1026 iscsi_conn_printk(KERN_ERR, conn,
1027 "pdu (op 0x%x itt 0x%x) rejected "
1028 "due to DataDigest error.\n",
1029 opcode, rejected_pdu.itt);
1030 break;
1031 case ISCSI_REASON_IMM_CMD_REJECT:
1032 iscsi_conn_printk(KERN_ERR, conn,
1033 "pdu (op 0x%x itt 0x%x) rejected. Too many "
1034 "immediate commands.\n",
1035 opcode, rejected_pdu.itt);
1036 /*
1037 * We only send one TMF at a time so if the target could not
1038 * handle it, then it should get fixed (RFC mandates that
1039 * a target can handle one immediate TMF per conn).
1040 *
1041 * For nops-outs, we could have sent more than one if
1042 * the target is sending us lots of nop-ins
1043 */
1044 if (opcode != ISCSI_OP_NOOP_OUT)
1045 return 0;
1046
1047 if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG)) {
1048 /*
1049 * nop-out in response to target's nop-out rejected.
1050 * Just resend.
1051 */
1052 /* In RX path we are under back lock */
1053 spin_unlock(&conn->session->back_lock);
1054 spin_lock(&conn->session->frwd_lock);
1055 iscsi_send_nopout(conn,
1056 (struct iscsi_nopin*)&rejected_pdu);
1057 spin_unlock(&conn->session->frwd_lock);
1058 spin_lock(&conn->session->back_lock);
1059 } else {
1060 struct iscsi_task *task;
1061 /*
1062 * Our nop as ping got dropped. We know the target
1063 * and transport are ok so just clean up
1064 */
1065 task = iscsi_itt_to_task(conn, rejected_pdu.itt);
1066 if (!task) {
1067 iscsi_conn_printk(KERN_ERR, conn,
1068 "Invalid pdu reject. Could "
1069 "not lookup rejected task.\n");
1070 rc = ISCSI_ERR_BAD_ITT;
1071 } else
1072 rc = iscsi_nop_out_rsp(task,
1073 (struct iscsi_nopin*)&rejected_pdu,
1074 NULL, 0);
1075 }
1076 break;
1077 default:
1078 iscsi_conn_printk(KERN_ERR, conn,
1079 "pdu (op 0x%x itt 0x%x) rejected. Reason "
1080 "code 0x%x\n", rejected_pdu.opcode,
1081 rejected_pdu.itt, reject->reason);
1082 break;
1083 }
1084 return rc;
1085}
1086
1087/**
1088 * iscsi_itt_to_task - look up task by itt
1089 * @conn: iscsi connection
1090 * @itt: itt
1091 *
1092 * This should be used for mgmt tasks like login and nops, or if
1093 * the LDD's itt space does not include the session age.
1094 *
1095 * The session back_lock must be held.
1096 */
1097struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
1098{
1099 struct iscsi_session *session = conn->session;
1100 int i;
1101
1102 if (itt == RESERVED_ITT)
1103 return NULL;
1104
1105 if (session->tt->parse_pdu_itt)
1106 session->tt->parse_pdu_itt(conn, itt, &i, NULL);
1107 else
1108 i = get_itt(itt);
1109 if (i >= session->cmds_max)
1110 return NULL;
1111
1112 return session->cmds[i];
1113}
1114EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
1115
1116/**
1117 * __iscsi_complete_pdu - complete pdu
1118 * @conn: iscsi conn
1119 * @hdr: iscsi header
1120 * @data: data buffer
1121 * @datalen: len of data buffer
1122 *
1123 * Completes pdu processing by freeing any resources allocated at
1124 * queuecommand or send generic. session back_lock must be held and verify
1125 * itt must have been called.
1126 */
1127int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1128 char *data, int datalen)
1129{
1130 struct iscsi_session *session = conn->session;
1131 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
1132 struct iscsi_task *task;
1133 uint32_t itt;
1134
1135 conn->last_recv = jiffies;
1136 rc = iscsi_verify_itt(conn, hdr->itt);
1137 if (rc)
1138 return rc;
1139
1140 if (hdr->itt != RESERVED_ITT)
1141 itt = get_itt(hdr->itt);
1142 else
1143 itt = ~0U;
1144
1145 ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
1146 opcode, conn->id, itt, datalen);
1147
1148 if (itt == ~0U) {
1149 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1150
1151 switch(opcode) {
1152 case ISCSI_OP_NOOP_IN:
1153 if (datalen) {
1154 rc = ISCSI_ERR_PROTO;
1155 break;
1156 }
1157
1158 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
1159 break;
1160
1161 /* In RX path we are under back lock */
1162 spin_unlock(&session->back_lock);
1163 spin_lock(&session->frwd_lock);
1164 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
1165 spin_unlock(&session->frwd_lock);
1166 spin_lock(&session->back_lock);
1167 break;
1168 case ISCSI_OP_REJECT:
1169 rc = iscsi_handle_reject(conn, hdr, data, datalen);
1170 break;
1171 case ISCSI_OP_ASYNC_EVENT:
1172 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1173 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1174 rc = ISCSI_ERR_CONN_FAILED;
1175 break;
1176 default:
1177 rc = ISCSI_ERR_BAD_OPCODE;
1178 break;
1179 }
1180 goto out;
1181 }
1182
1183 switch(opcode) {
1184 case ISCSI_OP_SCSI_CMD_RSP:
1185 case ISCSI_OP_SCSI_DATA_IN:
1186 task = iscsi_itt_to_ctask(conn, hdr->itt);
1187 if (!task)
1188 return ISCSI_ERR_BAD_ITT;
1189 task->last_xfer = jiffies;
1190 break;
1191 case ISCSI_OP_R2T:
1192 /*
1193 * LLD handles R2Ts if they need to.
1194 */
1195 return 0;
1196 case ISCSI_OP_LOGOUT_RSP:
1197 case ISCSI_OP_LOGIN_RSP:
1198 case ISCSI_OP_TEXT_RSP:
1199 case ISCSI_OP_SCSI_TMFUNC_RSP:
1200 case ISCSI_OP_NOOP_IN:
1201 task = iscsi_itt_to_task(conn, hdr->itt);
1202 if (!task)
1203 return ISCSI_ERR_BAD_ITT;
1204 break;
1205 default:
1206 return ISCSI_ERR_BAD_OPCODE;
1207 }
1208
1209 switch(opcode) {
1210 case ISCSI_OP_SCSI_CMD_RSP:
1211 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
1212 break;
1213 case ISCSI_OP_SCSI_DATA_IN:
1214 iscsi_data_in_rsp(conn, hdr, task);
1215 break;
1216 case ISCSI_OP_LOGOUT_RSP:
1217 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1218 if (datalen) {
1219 rc = ISCSI_ERR_PROTO;
1220 break;
1221 }
1222 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1223 goto recv_pdu;
1224 case ISCSI_OP_LOGIN_RSP:
1225 case ISCSI_OP_TEXT_RSP:
1226 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1227 /*
1228 * login related PDU's exp_statsn is handled in
1229 * userspace
1230 */
1231 goto recv_pdu;
1232 case ISCSI_OP_SCSI_TMFUNC_RSP:
1233 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1234 if (datalen) {
1235 rc = ISCSI_ERR_PROTO;
1236 break;
1237 }
1238
1239 iscsi_tmf_rsp(conn, hdr);
1240 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1241 break;
1242 case ISCSI_OP_NOOP_IN:
1243 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1244 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
1245 rc = ISCSI_ERR_PROTO;
1246 break;
1247 }
1248 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1249
1250 rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr,
1251 data, datalen);
1252 break;
1253 default:
1254 rc = ISCSI_ERR_BAD_OPCODE;
1255 break;
1256 }
1257
1258out:
1259 return rc;
1260recv_pdu:
1261 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1262 rc = ISCSI_ERR_CONN_FAILED;
1263 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1264 return rc;
1265}
1266EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
1267
1268int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1269 char *data, int datalen)
1270{
1271 int rc;
1272
1273 spin_lock(&conn->session->back_lock);
1274 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
1275 spin_unlock(&conn->session->back_lock);
1276 return rc;
1277}
1278EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
1279
1280int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
1281{
1282 struct iscsi_session *session = conn->session;
1283 int age = 0, i = 0;
1284
1285 if (itt == RESERVED_ITT)
1286 return 0;
1287
1288 if (session->tt->parse_pdu_itt)
1289 session->tt->parse_pdu_itt(conn, itt, &i, &age);
1290 else {
1291 i = get_itt(itt);
1292 age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
1293 }
1294
1295 if (age != session->age) {
1296 iscsi_conn_printk(KERN_ERR, conn,
1297 "received itt %x expected session age (%x)\n",
1298 (__force u32)itt, session->age);
1299 return ISCSI_ERR_BAD_ITT;
1300 }
1301
1302 if (i >= session->cmds_max) {
1303 iscsi_conn_printk(KERN_ERR, conn,
1304 "received invalid itt index %u (max cmds "
1305 "%u.\n", i, session->cmds_max);
1306 return ISCSI_ERR_BAD_ITT;
1307 }
1308 return 0;
1309}
1310EXPORT_SYMBOL_GPL(iscsi_verify_itt);
1311
1312/**
1313 * iscsi_itt_to_ctask - look up ctask by itt
1314 * @conn: iscsi connection
1315 * @itt: itt
1316 *
1317 * This should be used for cmd tasks.
1318 *
1319 * The session back_lock must be held.
1320 */
1321struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
1322{
1323 struct iscsi_task *task;
1324
1325 if (iscsi_verify_itt(conn, itt))
1326 return NULL;
1327
1328 task = iscsi_itt_to_task(conn, itt);
1329 if (!task || !task->sc)
1330 return NULL;
1331
1332 if (task->sc->SCp.phase != conn->session->age) {
1333 iscsi_session_printk(KERN_ERR, conn->session,
1334 "task's session age %d, expected %d\n",
1335 task->sc->SCp.phase, conn->session->age);
1336 return NULL;
1337 }
1338
1339 return task;
1340}
1341EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
1342
1343void iscsi_session_failure(struct iscsi_session *session,
1344 enum iscsi_err err)
1345{
1346 struct iscsi_conn *conn;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001347
1348 spin_lock_bh(&session->frwd_lock);
1349 conn = session->leadconn;
1350 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1351 spin_unlock_bh(&session->frwd_lock);
1352 return;
1353 }
1354
Olivier Deprez0e641232021-09-23 10:07:05 +02001355 iscsi_get_conn(conn->cls_conn);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001356 spin_unlock_bh(&session->frwd_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001357 /*
1358 * if the host is being removed bypass the connection
1359 * recovery initialization because we are going to kill
1360 * the session.
1361 */
1362 if (err == ISCSI_ERR_INVALID_HOST)
1363 iscsi_conn_error_event(conn->cls_conn, err);
1364 else
1365 iscsi_conn_failure(conn, err);
Olivier Deprez0e641232021-09-23 10:07:05 +02001366 iscsi_put_conn(conn->cls_conn);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001367}
1368EXPORT_SYMBOL_GPL(iscsi_session_failure);
1369
1370void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1371{
1372 struct iscsi_session *session = conn->session;
1373
1374 spin_lock_bh(&session->frwd_lock);
1375 if (session->state == ISCSI_STATE_FAILED) {
1376 spin_unlock_bh(&session->frwd_lock);
1377 return;
1378 }
1379
1380 if (conn->stop_stage == 0)
1381 session->state = ISCSI_STATE_FAILED;
1382 spin_unlock_bh(&session->frwd_lock);
1383
1384 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1385 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1386 iscsi_conn_error_event(conn->cls_conn, err);
1387}
1388EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1389
1390static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1391{
1392 struct iscsi_session *session = conn->session;
1393
1394 /*
1395 * Check for iSCSI window and take care of CmdSN wrap-around
1396 */
1397 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
1398 ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
1399 "%u MaxCmdSN %u CmdSN %u/%u\n",
1400 session->exp_cmdsn, session->max_cmdsn,
1401 session->cmdsn, session->queued_cmdsn);
1402 return -ENOSPC;
1403 }
1404 return 0;
1405}
1406
1407static int iscsi_xmit_task(struct iscsi_conn *conn)
1408{
1409 struct iscsi_task *task = conn->task;
1410 int rc;
1411
1412 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx))
1413 return -ENODATA;
1414
David Brazdil0f672f62019-12-10 10:32:29 +00001415 spin_lock_bh(&conn->session->back_lock);
1416 if (conn->task == NULL) {
1417 spin_unlock_bh(&conn->session->back_lock);
1418 return -ENODATA;
1419 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001420 __iscsi_get_task(task);
David Brazdil0f672f62019-12-10 10:32:29 +00001421 spin_unlock_bh(&conn->session->back_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001422 spin_unlock_bh(&conn->session->frwd_lock);
1423 rc = conn->session->tt->xmit_task(task);
1424 spin_lock_bh(&conn->session->frwd_lock);
1425 if (!rc) {
1426 /* done with this task */
1427 task->last_xfer = jiffies;
1428 conn->task = NULL;
1429 }
1430 /* regular RX path uses back_lock */
1431 spin_lock(&conn->session->back_lock);
1432 __iscsi_put_task(task);
1433 spin_unlock(&conn->session->back_lock);
1434 return rc;
1435}
1436
1437/**
1438 * iscsi_requeue_task - requeue task to run from session workqueue
1439 * @task: task to requeue
1440 *
1441 * LLDs that need to run a task from the session workqueue should call
1442 * this. The session frwd_lock must be held. This should only be called
1443 * by software drivers.
1444 */
1445void iscsi_requeue_task(struct iscsi_task *task)
1446{
1447 struct iscsi_conn *conn = task->conn;
1448
1449 /*
1450 * this may be on the requeue list already if the xmit_task callout
1451 * is handling the r2ts while we are adding new ones
1452 */
1453 spin_lock_bh(&conn->taskqueuelock);
1454 if (list_empty(&task->running))
1455 list_add_tail(&task->running, &conn->requeue);
1456 spin_unlock_bh(&conn->taskqueuelock);
1457 iscsi_conn_queue_work(conn);
1458}
1459EXPORT_SYMBOL_GPL(iscsi_requeue_task);
1460
1461/**
1462 * iscsi_data_xmit - xmit any command into the scheduled connection
1463 * @conn: iscsi connection
1464 *
1465 * Notes:
1466 * The function can return -EAGAIN in which case the caller must
1467 * re-schedule it again later or recover. '0' return code means
1468 * successful xmit.
1469 **/
1470static int iscsi_data_xmit(struct iscsi_conn *conn)
1471{
1472 struct iscsi_task *task;
1473 int rc = 0;
1474
1475 spin_lock_bh(&conn->session->frwd_lock);
1476 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1477 ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
1478 spin_unlock_bh(&conn->session->frwd_lock);
1479 return -ENODATA;
1480 }
1481
1482 if (conn->task) {
1483 rc = iscsi_xmit_task(conn);
1484 if (rc)
1485 goto done;
1486 }
1487
1488 /*
1489 * process mgmt pdus like nops before commands since we should
1490 * only have one nop-out as a ping from us and targets should not
1491 * overflow us with nop-ins
1492 */
1493 spin_lock_bh(&conn->taskqueuelock);
1494check_mgmt:
1495 while (!list_empty(&conn->mgmtqueue)) {
1496 conn->task = list_entry(conn->mgmtqueue.next,
1497 struct iscsi_task, running);
1498 list_del_init(&conn->task->running);
1499 spin_unlock_bh(&conn->taskqueuelock);
1500 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1501 /* regular RX path uses back_lock */
1502 spin_lock_bh(&conn->session->back_lock);
1503 __iscsi_put_task(conn->task);
1504 spin_unlock_bh(&conn->session->back_lock);
1505 conn->task = NULL;
1506 spin_lock_bh(&conn->taskqueuelock);
1507 continue;
1508 }
1509 rc = iscsi_xmit_task(conn);
1510 if (rc)
1511 goto done;
1512 spin_lock_bh(&conn->taskqueuelock);
1513 }
1514
1515 /* process pending command queue */
1516 while (!list_empty(&conn->cmdqueue)) {
1517 conn->task = list_entry(conn->cmdqueue.next, struct iscsi_task,
1518 running);
1519 list_del_init(&conn->task->running);
1520 spin_unlock_bh(&conn->taskqueuelock);
1521 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
1522 fail_scsi_task(conn->task, DID_IMM_RETRY);
1523 spin_lock_bh(&conn->taskqueuelock);
1524 continue;
1525 }
1526 rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1527 if (rc) {
Olivier Deprez0e641232021-09-23 10:07:05 +02001528 if (rc == -ENOMEM || rc == -EACCES)
1529 fail_scsi_task(conn->task, DID_IMM_RETRY);
1530 else
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001531 fail_scsi_task(conn->task, DID_ABORT);
1532 spin_lock_bh(&conn->taskqueuelock);
1533 continue;
1534 }
1535 rc = iscsi_xmit_task(conn);
1536 if (rc)
1537 goto done;
1538 /*
1539 * we could continuously get new task requests so
1540 * we need to check the mgmt queue for nops that need to
1541 * be sent to aviod starvation
1542 */
1543 spin_lock_bh(&conn->taskqueuelock);
1544 if (!list_empty(&conn->mgmtqueue))
1545 goto check_mgmt;
1546 }
1547
1548 while (!list_empty(&conn->requeue)) {
1549 /*
1550 * we always do fastlogout - conn stop code will clean up.
1551 */
1552 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1553 break;
1554
1555 task = list_entry(conn->requeue.next, struct iscsi_task,
1556 running);
1557 if (iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_DATA_OUT))
1558 break;
1559
1560 conn->task = task;
1561 list_del_init(&conn->task->running);
1562 conn->task->state = ISCSI_TASK_RUNNING;
1563 spin_unlock_bh(&conn->taskqueuelock);
1564 rc = iscsi_xmit_task(conn);
1565 if (rc)
1566 goto done;
1567 spin_lock_bh(&conn->taskqueuelock);
1568 if (!list_empty(&conn->mgmtqueue))
1569 goto check_mgmt;
1570 }
1571 spin_unlock_bh(&conn->taskqueuelock);
1572 spin_unlock_bh(&conn->session->frwd_lock);
1573 return -ENODATA;
1574
1575done:
1576 spin_unlock_bh(&conn->session->frwd_lock);
1577 return rc;
1578}
1579
1580static void iscsi_xmitworker(struct work_struct *work)
1581{
1582 struct iscsi_conn *conn =
1583 container_of(work, struct iscsi_conn, xmitwork);
1584 int rc;
1585 /*
1586 * serialize Xmit worker on a per-connection basis.
1587 */
1588 do {
1589 rc = iscsi_data_xmit(conn);
1590 } while (rc >= 0 || rc == -EAGAIN);
1591}
1592
1593static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1594 struct scsi_cmnd *sc)
1595{
1596 struct iscsi_task *task;
1597
1598 if (!kfifo_out(&conn->session->cmdpool.queue,
1599 (void *) &task, sizeof(void *)))
1600 return NULL;
1601
1602 sc->SCp.phase = conn->session->age;
1603 sc->SCp.ptr = (char *) task;
1604
1605 refcount_set(&task->refcount, 1);
1606 task->state = ISCSI_TASK_PENDING;
1607 task->conn = conn;
1608 task->sc = sc;
1609 task->have_checked_conn = false;
1610 task->last_timeout = jiffies;
1611 task->last_xfer = jiffies;
1612 task->protected = false;
1613 INIT_LIST_HEAD(&task->running);
1614 return task;
1615}
1616
1617enum {
1618 FAILURE_BAD_HOST = 1,
1619 FAILURE_SESSION_FAILED,
1620 FAILURE_SESSION_FREED,
1621 FAILURE_WINDOW_CLOSED,
1622 FAILURE_OOM,
1623 FAILURE_SESSION_TERMINATE,
1624 FAILURE_SESSION_IN_RECOVERY,
1625 FAILURE_SESSION_RECOVERY_TIMEOUT,
1626 FAILURE_SESSION_LOGGING_OUT,
1627 FAILURE_SESSION_NOT_READY,
1628};
1629
1630int iscsi_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *sc)
1631{
1632 struct iscsi_cls_session *cls_session;
1633 struct iscsi_host *ihost;
1634 int reason = 0;
1635 struct iscsi_session *session;
1636 struct iscsi_conn *conn;
1637 struct iscsi_task *task = NULL;
1638
1639 sc->result = 0;
1640 sc->SCp.ptr = NULL;
1641
1642 ihost = shost_priv(host);
1643
1644 cls_session = starget_to_session(scsi_target(sc->device));
1645 session = cls_session->dd_data;
1646 spin_lock_bh(&session->frwd_lock);
1647
1648 reason = iscsi_session_chkready(cls_session);
1649 if (reason) {
1650 sc->result = reason;
1651 goto fault;
1652 }
1653
1654 if (session->state != ISCSI_STATE_LOGGED_IN) {
1655 /*
1656 * to handle the race between when we set the recovery state
1657 * and block the session we requeue here (commands could
1658 * be entering our queuecommand while a block is starting
1659 * up because the block code is not locked)
1660 */
1661 switch (session->state) {
1662 case ISCSI_STATE_FAILED:
1663 /*
1664 * cmds should fail during shutdown, if the session
1665 * state is bad, allowing completion to happen
1666 */
1667 if (unlikely(system_state != SYSTEM_RUNNING)) {
1668 reason = FAILURE_SESSION_FAILED;
1669 sc->result = DID_NO_CONNECT << 16;
1670 break;
1671 }
1672 /* fall through */
1673 case ISCSI_STATE_IN_RECOVERY:
1674 reason = FAILURE_SESSION_IN_RECOVERY;
1675 sc->result = DID_IMM_RETRY << 16;
1676 break;
1677 case ISCSI_STATE_LOGGING_OUT:
1678 reason = FAILURE_SESSION_LOGGING_OUT;
1679 sc->result = DID_IMM_RETRY << 16;
1680 break;
1681 case ISCSI_STATE_RECOVERY_FAILED:
1682 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
1683 sc->result = DID_TRANSPORT_FAILFAST << 16;
1684 break;
1685 case ISCSI_STATE_TERMINATE:
1686 reason = FAILURE_SESSION_TERMINATE;
1687 sc->result = DID_NO_CONNECT << 16;
1688 break;
1689 default:
1690 reason = FAILURE_SESSION_FREED;
1691 sc->result = DID_NO_CONNECT << 16;
1692 }
1693 goto fault;
1694 }
1695
1696 conn = session->leadconn;
1697 if (!conn) {
1698 reason = FAILURE_SESSION_FREED;
1699 sc->result = DID_NO_CONNECT << 16;
1700 goto fault;
1701 }
1702
1703 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1704 reason = FAILURE_SESSION_IN_RECOVERY;
1705 sc->result = DID_REQUEUE << 16;
1706 goto fault;
1707 }
1708
1709 if (iscsi_check_cmdsn_window_closed(conn)) {
1710 reason = FAILURE_WINDOW_CLOSED;
1711 goto reject;
1712 }
1713
1714 task = iscsi_alloc_task(conn, sc);
1715 if (!task) {
1716 reason = FAILURE_OOM;
1717 goto reject;
1718 }
1719
1720 if (!ihost->workq) {
1721 reason = iscsi_prep_scsi_cmd_pdu(task);
1722 if (reason) {
1723 if (reason == -ENOMEM || reason == -EACCES) {
1724 reason = FAILURE_OOM;
1725 goto prepd_reject;
1726 } else {
1727 sc->result = DID_ABORT << 16;
1728 goto prepd_fault;
1729 }
1730 }
1731 if (session->tt->xmit_task(task)) {
1732 session->cmdsn--;
1733 reason = FAILURE_SESSION_NOT_READY;
1734 goto prepd_reject;
1735 }
1736 } else {
1737 spin_lock_bh(&conn->taskqueuelock);
1738 list_add_tail(&task->running, &conn->cmdqueue);
1739 spin_unlock_bh(&conn->taskqueuelock);
1740 iscsi_conn_queue_work(conn);
1741 }
1742
1743 session->queued_cmdsn++;
1744 spin_unlock_bh(&session->frwd_lock);
1745 return 0;
1746
1747prepd_reject:
David Brazdil0f672f62019-12-10 10:32:29 +00001748 spin_lock_bh(&session->back_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001749 iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
David Brazdil0f672f62019-12-10 10:32:29 +00001750 spin_unlock_bh(&session->back_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001751reject:
1752 spin_unlock_bh(&session->frwd_lock);
1753 ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
1754 sc->cmnd[0], reason);
1755 return SCSI_MLQUEUE_TARGET_BUSY;
1756
1757prepd_fault:
David Brazdil0f672f62019-12-10 10:32:29 +00001758 spin_lock_bh(&session->back_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001759 iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
David Brazdil0f672f62019-12-10 10:32:29 +00001760 spin_unlock_bh(&session->back_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001761fault:
1762 spin_unlock_bh(&session->frwd_lock);
1763 ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
1764 sc->cmnd[0], reason);
David Brazdil0f672f62019-12-10 10:32:29 +00001765 scsi_set_resid(sc, scsi_bufflen(sc));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001766 sc->scsi_done(sc);
1767 return 0;
1768}
1769EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1770
1771int iscsi_target_alloc(struct scsi_target *starget)
1772{
1773 struct iscsi_cls_session *cls_session = starget_to_session(starget);
1774 struct iscsi_session *session = cls_session->dd_data;
1775
1776 starget->can_queue = session->scsi_cmds_max;
1777 return 0;
1778}
1779EXPORT_SYMBOL_GPL(iscsi_target_alloc);
1780
1781static void iscsi_tmf_timedout(struct timer_list *t)
1782{
Olivier Deprez0e641232021-09-23 10:07:05 +02001783 struct iscsi_session *session = from_timer(session, t, tmf_timer);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001784
1785 spin_lock(&session->frwd_lock);
Olivier Deprez0e641232021-09-23 10:07:05 +02001786 if (session->tmf_state == TMF_QUEUED) {
1787 session->tmf_state = TMF_TIMEDOUT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001788 ISCSI_DBG_EH(session, "tmf timedout\n");
1789 /* unblock eh_abort() */
Olivier Deprez0e641232021-09-23 10:07:05 +02001790 wake_up(&session->ehwait);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001791 }
1792 spin_unlock(&session->frwd_lock);
1793}
1794
1795static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
1796 struct iscsi_tm *hdr, int age,
1797 int timeout)
1798 __must_hold(&session->frwd_lock)
1799{
1800 struct iscsi_session *session = conn->session;
1801 struct iscsi_task *task;
1802
1803 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1804 NULL, 0);
1805 if (!task) {
1806 spin_unlock_bh(&session->frwd_lock);
1807 iscsi_conn_printk(KERN_ERR, conn, "Could not send TMF.\n");
1808 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1809 spin_lock_bh(&session->frwd_lock);
1810 return -EPERM;
1811 }
1812 conn->tmfcmd_pdus_cnt++;
Olivier Deprez0e641232021-09-23 10:07:05 +02001813 session->tmf_timer.expires = timeout * HZ + jiffies;
1814 add_timer(&session->tmf_timer);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001815 ISCSI_DBG_EH(session, "tmf set timeout\n");
1816
1817 spin_unlock_bh(&session->frwd_lock);
1818 mutex_unlock(&session->eh_mutex);
1819
1820 /*
1821 * block eh thread until:
1822 *
1823 * 1) tmf response
1824 * 2) tmf timeout
1825 * 3) session is terminated or restarted or userspace has
1826 * given up on recovery
1827 */
Olivier Deprez0e641232021-09-23 10:07:05 +02001828 wait_event_interruptible(session->ehwait, age != session->age ||
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001829 session->state != ISCSI_STATE_LOGGED_IN ||
Olivier Deprez0e641232021-09-23 10:07:05 +02001830 session->tmf_state != TMF_QUEUED);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001831 if (signal_pending(current))
1832 flush_signals(current);
Olivier Deprez0e641232021-09-23 10:07:05 +02001833 del_timer_sync(&session->tmf_timer);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001834
1835 mutex_lock(&session->eh_mutex);
1836 spin_lock_bh(&session->frwd_lock);
1837 /* if the session drops it will clean up the task */
1838 if (age != session->age ||
1839 session->state != ISCSI_STATE_LOGGED_IN)
1840 return -ENOTCONN;
1841 return 0;
1842}
1843
1844/*
1845 * Fail commands. session lock held and recv side suspended and xmit
1846 * thread flushed
1847 */
1848static void fail_scsi_tasks(struct iscsi_conn *conn, u64 lun, int error)
1849{
1850 struct iscsi_task *task;
1851 int i;
1852
1853 for (i = 0; i < conn->session->cmds_max; i++) {
1854 task = conn->session->cmds[i];
1855 if (!task->sc || task->state == ISCSI_TASK_FREE)
1856 continue;
1857
1858 if (lun != -1 && lun != task->sc->device->lun)
1859 continue;
1860
1861 ISCSI_DBG_SESSION(conn->session,
1862 "failing sc %p itt 0x%x state %d\n",
1863 task->sc, task->itt, task->state);
1864 fail_scsi_task(task, error);
1865 }
1866}
1867
1868/**
1869 * iscsi_suspend_queue - suspend iscsi_queuecommand
1870 * @conn: iscsi conn to stop queueing IO on
1871 *
1872 * This grabs the session frwd_lock to make sure no one is in
1873 * xmit_task/queuecommand, and then sets suspend to prevent
1874 * new commands from being queued. This only needs to be called
1875 * by offload drivers that need to sync a path like ep disconnect
1876 * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi
1877 * will call iscsi_start_tx and iscsi_unblock_session when in FFP.
1878 */
1879void iscsi_suspend_queue(struct iscsi_conn *conn)
1880{
1881 spin_lock_bh(&conn->session->frwd_lock);
1882 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1883 spin_unlock_bh(&conn->session->frwd_lock);
1884}
1885EXPORT_SYMBOL_GPL(iscsi_suspend_queue);
1886
1887/**
1888 * iscsi_suspend_tx - suspend iscsi_data_xmit
1889 * @conn: iscsi conn tp stop processing IO on.
1890 *
1891 * This function sets the suspend bit to prevent iscsi_data_xmit
1892 * from sending new IO, and if work is queued on the xmit thread
1893 * it will wait for it to be completed.
1894 */
1895void iscsi_suspend_tx(struct iscsi_conn *conn)
1896{
1897 struct Scsi_Host *shost = conn->session->host;
1898 struct iscsi_host *ihost = shost_priv(shost);
1899
1900 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1901 if (ihost->workq)
1902 flush_workqueue(ihost->workq);
1903}
1904EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
1905
1906static void iscsi_start_tx(struct iscsi_conn *conn)
1907{
1908 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1909 iscsi_conn_queue_work(conn);
1910}
1911
1912/*
1913 * We want to make sure a ping is in flight. It has timed out.
1914 * And we are not busy processing a pdu that is making
1915 * progress but got started before the ping and is taking a while
1916 * to complete so the ping is just stuck behind it in a queue.
1917 */
1918static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
1919{
Olivier Deprez0e641232021-09-23 10:07:05 +02001920 if (READ_ONCE(conn->ping_task) &&
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001921 time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1922 (conn->ping_timeout * HZ), jiffies))
1923 return 1;
1924 else
1925 return 0;
1926}
1927
1928enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
1929{
1930 enum blk_eh_timer_return rc = BLK_EH_DONE;
1931 struct iscsi_task *task = NULL, *running_task;
1932 struct iscsi_cls_session *cls_session;
1933 struct iscsi_session *session;
1934 struct iscsi_conn *conn;
1935 int i;
1936
1937 cls_session = starget_to_session(scsi_target(sc->device));
1938 session = cls_session->dd_data;
1939
1940 ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc);
1941
Olivier Deprez0e641232021-09-23 10:07:05 +02001942 spin_lock_bh(&session->frwd_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001943 task = (struct iscsi_task *)sc->SCp.ptr;
1944 if (!task) {
1945 /*
1946 * Raced with completion. Blk layer has taken ownership
1947 * so let timeout code complete it now.
1948 */
1949 rc = BLK_EH_DONE;
1950 goto done;
1951 }
1952
1953 if (session->state != ISCSI_STATE_LOGGED_IN) {
1954 /*
1955 * During shutdown, if session is prematurely disconnected,
1956 * recovery won't happen and there will be hung cmds. Not
1957 * handling cmds would trigger EH, also bad in this case.
1958 * Instead, handle cmd, allow completion to happen and let
1959 * upper layer to deal with the result.
1960 */
1961 if (unlikely(system_state != SYSTEM_RUNNING)) {
1962 sc->result = DID_NO_CONNECT << 16;
1963 ISCSI_DBG_EH(session, "sc on shutdown, handled\n");
1964 rc = BLK_EH_DONE;
1965 goto done;
1966 }
1967 /*
1968 * We are probably in the middle of iscsi recovery so let
1969 * that complete and handle the error.
1970 */
1971 rc = BLK_EH_RESET_TIMER;
1972 goto done;
1973 }
1974
1975 conn = session->leadconn;
1976 if (!conn) {
1977 /* In the middle of shuting down */
1978 rc = BLK_EH_RESET_TIMER;
1979 goto done;
1980 }
1981
1982 /*
1983 * If we have sent (at least queued to the network layer) a pdu or
1984 * recvd one for the task since the last timeout ask for
1985 * more time. If on the next timeout we have not made progress
1986 * we can check if it is the task or connection when we send the
1987 * nop as a ping.
1988 */
1989 if (time_after(task->last_xfer, task->last_timeout)) {
1990 ISCSI_DBG_EH(session, "Command making progress. Asking "
1991 "scsi-ml for more time to complete. "
1992 "Last data xfer at %lu. Last timeout was at "
1993 "%lu\n.", task->last_xfer, task->last_timeout);
1994 task->have_checked_conn = false;
1995 rc = BLK_EH_RESET_TIMER;
1996 goto done;
1997 }
1998
1999 if (!conn->recv_timeout && !conn->ping_timeout)
2000 goto done;
2001 /*
2002 * if the ping timedout then we are in the middle of cleaning up
2003 * and can let the iscsi eh handle it
2004 */
2005 if (iscsi_has_ping_timed_out(conn)) {
2006 rc = BLK_EH_RESET_TIMER;
2007 goto done;
2008 }
2009
2010 for (i = 0; i < conn->session->cmds_max; i++) {
2011 running_task = conn->session->cmds[i];
2012 if (!running_task->sc || running_task == task ||
2013 running_task->state != ISCSI_TASK_RUNNING)
2014 continue;
2015
2016 /*
2017 * Only check if cmds started before this one have made
2018 * progress, or this could never fail
2019 */
2020 if (time_after(running_task->sc->jiffies_at_alloc,
2021 task->sc->jiffies_at_alloc))
2022 continue;
2023
2024 if (time_after(running_task->last_xfer, task->last_timeout)) {
2025 /*
2026 * This task has not made progress, but a task
2027 * started before us has transferred data since
2028 * we started/last-checked. We could be queueing
2029 * too many tasks or the LU is bad.
2030 *
2031 * If the device is bad the cmds ahead of us on
2032 * other devs will complete, and this loop will
2033 * eventually fail starting the scsi eh.
2034 */
2035 ISCSI_DBG_EH(session, "Command has not made progress "
2036 "but commands ahead of it have. "
2037 "Asking scsi-ml for more time to "
2038 "complete. Our last xfer vs running task "
2039 "last xfer %lu/%lu. Last check %lu.\n",
2040 task->last_xfer, running_task->last_xfer,
2041 task->last_timeout);
2042 rc = BLK_EH_RESET_TIMER;
2043 goto done;
2044 }
2045 }
2046
2047 /* Assumes nop timeout is shorter than scsi cmd timeout */
2048 if (task->have_checked_conn)
2049 goto done;
2050
2051 /*
2052 * Checking the transport already or nop from a cmd timeout still
2053 * running
2054 */
Olivier Deprez0e641232021-09-23 10:07:05 +02002055 if (READ_ONCE(conn->ping_task)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002056 task->have_checked_conn = true;
2057 rc = BLK_EH_RESET_TIMER;
2058 goto done;
2059 }
2060
2061 /* Make sure there is a transport check done */
2062 iscsi_send_nopout(conn, NULL);
2063 task->have_checked_conn = true;
2064 rc = BLK_EH_RESET_TIMER;
2065
2066done:
2067 if (task)
2068 task->last_timeout = jiffies;
Olivier Deprez0e641232021-09-23 10:07:05 +02002069 spin_unlock_bh(&session->frwd_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002070 ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
2071 "timer reset" : "shutdown or nh");
2072 return rc;
2073}
2074EXPORT_SYMBOL_GPL(iscsi_eh_cmd_timed_out);
2075
2076static void iscsi_check_transport_timeouts(struct timer_list *t)
2077{
2078 struct iscsi_conn *conn = from_timer(conn, t, transport_timer);
2079 struct iscsi_session *session = conn->session;
2080 unsigned long recv_timeout, next_timeout = 0, last_recv;
2081
2082 spin_lock(&session->frwd_lock);
2083 if (session->state != ISCSI_STATE_LOGGED_IN)
2084 goto done;
2085
2086 recv_timeout = conn->recv_timeout;
2087 if (!recv_timeout)
2088 goto done;
2089
2090 recv_timeout *= HZ;
2091 last_recv = conn->last_recv;
2092
2093 if (iscsi_has_ping_timed_out(conn)) {
2094 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
2095 "expired, recv timeout %d, last rx %lu, "
2096 "last ping %lu, now %lu\n",
2097 conn->ping_timeout, conn->recv_timeout,
2098 last_recv, conn->last_ping, jiffies);
2099 spin_unlock(&session->frwd_lock);
2100 iscsi_conn_failure(conn, ISCSI_ERR_NOP_TIMEDOUT);
2101 return;
2102 }
2103
2104 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
2105 /* send a ping to try to provoke some traffic */
2106 ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
2107 if (iscsi_send_nopout(conn, NULL))
2108 next_timeout = jiffies + (1 * HZ);
2109 else
2110 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
2111 } else
2112 next_timeout = last_recv + recv_timeout;
2113
2114 ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
2115 mod_timer(&conn->transport_timer, next_timeout);
2116done:
2117 spin_unlock(&session->frwd_lock);
2118}
2119
2120static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
2121 struct iscsi_tm *hdr)
2122{
2123 memset(hdr, 0, sizeof(*hdr));
2124 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2125 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
2126 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2127 hdr->lun = task->lun;
2128 hdr->rtt = task->hdr_itt;
2129 hdr->refcmdsn = task->cmdsn;
2130}
2131
2132int iscsi_eh_abort(struct scsi_cmnd *sc)
2133{
2134 struct iscsi_cls_session *cls_session;
2135 struct iscsi_session *session;
2136 struct iscsi_conn *conn;
2137 struct iscsi_task *task;
2138 struct iscsi_tm *hdr;
2139 int age;
2140
2141 cls_session = starget_to_session(scsi_target(sc->device));
2142 session = cls_session->dd_data;
2143
2144 ISCSI_DBG_EH(session, "aborting sc %p\n", sc);
2145
2146 mutex_lock(&session->eh_mutex);
2147 spin_lock_bh(&session->frwd_lock);
2148 /*
2149 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
2150 * got the command.
2151 */
2152 if (!sc->SCp.ptr) {
2153 ISCSI_DBG_EH(session, "sc never reached iscsi layer or "
2154 "it completed.\n");
2155 spin_unlock_bh(&session->frwd_lock);
2156 mutex_unlock(&session->eh_mutex);
2157 return SUCCESS;
2158 }
2159
2160 /*
2161 * If we are not logged in or we have started a new session
2162 * then let the host reset code handle this
2163 */
2164 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
2165 sc->SCp.phase != session->age) {
2166 spin_unlock_bh(&session->frwd_lock);
2167 mutex_unlock(&session->eh_mutex);
2168 ISCSI_DBG_EH(session, "failing abort due to dropped "
2169 "session.\n");
2170 return FAILED;
2171 }
2172
2173 conn = session->leadconn;
2174 conn->eh_abort_cnt++;
2175 age = session->age;
2176
2177 task = (struct iscsi_task *)sc->SCp.ptr;
2178 ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n",
2179 sc, task->itt);
2180
2181 /* task completed before time out */
2182 if (!task->sc) {
2183 ISCSI_DBG_EH(session, "sc completed while abort in progress\n");
2184 goto success;
2185 }
2186
2187 if (task->state == ISCSI_TASK_PENDING) {
2188 fail_scsi_task(task, DID_ABORT);
2189 goto success;
2190 }
2191
2192 /* only have one tmf outstanding at a time */
Olivier Deprez0e641232021-09-23 10:07:05 +02002193 if (session->tmf_state != TMF_INITIAL)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002194 goto failed;
Olivier Deprez0e641232021-09-23 10:07:05 +02002195 session->tmf_state = TMF_QUEUED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002196
Olivier Deprez0e641232021-09-23 10:07:05 +02002197 hdr = &session->tmhdr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002198 iscsi_prep_abort_task_pdu(task, hdr);
2199
2200 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout))
2201 goto failed;
2202
Olivier Deprez0e641232021-09-23 10:07:05 +02002203 switch (session->tmf_state) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002204 case TMF_SUCCESS:
2205 spin_unlock_bh(&session->frwd_lock);
2206 /*
2207 * stop tx side incase the target had sent a abort rsp but
2208 * the initiator was still writing out data.
2209 */
2210 iscsi_suspend_tx(conn);
2211 /*
2212 * we do not stop the recv side because targets have been
2213 * good and have never sent us a successful tmf response
2214 * then sent more data for the cmd.
2215 */
2216 spin_lock_bh(&session->frwd_lock);
2217 fail_scsi_task(task, DID_ABORT);
Olivier Deprez0e641232021-09-23 10:07:05 +02002218 session->tmf_state = TMF_INITIAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002219 memset(hdr, 0, sizeof(*hdr));
2220 spin_unlock_bh(&session->frwd_lock);
2221 iscsi_start_tx(conn);
2222 goto success_unlocked;
2223 case TMF_TIMEDOUT:
2224 spin_unlock_bh(&session->frwd_lock);
2225 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2226 goto failed_unlocked;
2227 case TMF_NOT_FOUND:
2228 if (!sc->SCp.ptr) {
Olivier Deprez0e641232021-09-23 10:07:05 +02002229 session->tmf_state = TMF_INITIAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002230 memset(hdr, 0, sizeof(*hdr));
2231 /* task completed before tmf abort response */
2232 ISCSI_DBG_EH(session, "sc completed while abort in "
2233 "progress\n");
2234 goto success;
2235 }
2236 /* fall through */
2237 default:
Olivier Deprez0e641232021-09-23 10:07:05 +02002238 session->tmf_state = TMF_INITIAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002239 goto failed;
2240 }
2241
2242success:
2243 spin_unlock_bh(&session->frwd_lock);
2244success_unlocked:
2245 ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n",
2246 sc, task->itt);
2247 mutex_unlock(&session->eh_mutex);
2248 return SUCCESS;
2249
2250failed:
2251 spin_unlock_bh(&session->frwd_lock);
2252failed_unlocked:
2253 ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc,
2254 task ? task->itt : 0);
2255 mutex_unlock(&session->eh_mutex);
2256 return FAILED;
2257}
2258EXPORT_SYMBOL_GPL(iscsi_eh_abort);
2259
2260static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2261{
2262 memset(hdr, 0, sizeof(*hdr));
2263 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2264 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2265 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2266 int_to_scsilun(sc->device->lun, &hdr->lun);
2267 hdr->rtt = RESERVED_ITT;
2268}
2269
2270int iscsi_eh_device_reset(struct scsi_cmnd *sc)
2271{
2272 struct iscsi_cls_session *cls_session;
2273 struct iscsi_session *session;
2274 struct iscsi_conn *conn;
2275 struct iscsi_tm *hdr;
2276 int rc = FAILED;
2277
2278 cls_session = starget_to_session(scsi_target(sc->device));
2279 session = cls_session->dd_data;
2280
2281 ISCSI_DBG_EH(session, "LU Reset [sc %p lun %llu]\n", sc,
2282 sc->device->lun);
2283
2284 mutex_lock(&session->eh_mutex);
2285 spin_lock_bh(&session->frwd_lock);
2286 /*
2287 * Just check if we are not logged in. We cannot check for
2288 * the phase because the reset could come from a ioctl.
2289 */
2290 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2291 goto unlock;
2292 conn = session->leadconn;
2293
2294 /* only have one tmf outstanding at a time */
Olivier Deprez0e641232021-09-23 10:07:05 +02002295 if (session->tmf_state != TMF_INITIAL)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002296 goto unlock;
Olivier Deprez0e641232021-09-23 10:07:05 +02002297 session->tmf_state = TMF_QUEUED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002298
Olivier Deprez0e641232021-09-23 10:07:05 +02002299 hdr = &session->tmhdr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002300 iscsi_prep_lun_reset_pdu(sc, hdr);
2301
2302 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
2303 session->lu_reset_timeout)) {
2304 rc = FAILED;
2305 goto unlock;
2306 }
2307
Olivier Deprez0e641232021-09-23 10:07:05 +02002308 switch (session->tmf_state) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002309 case TMF_SUCCESS:
2310 break;
2311 case TMF_TIMEDOUT:
2312 spin_unlock_bh(&session->frwd_lock);
2313 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2314 goto done;
2315 default:
Olivier Deprez0e641232021-09-23 10:07:05 +02002316 session->tmf_state = TMF_INITIAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002317 goto unlock;
2318 }
2319
2320 rc = SUCCESS;
2321 spin_unlock_bh(&session->frwd_lock);
2322
2323 iscsi_suspend_tx(conn);
2324
2325 spin_lock_bh(&session->frwd_lock);
2326 memset(hdr, 0, sizeof(*hdr));
2327 fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
Olivier Deprez0e641232021-09-23 10:07:05 +02002328 session->tmf_state = TMF_INITIAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002329 spin_unlock_bh(&session->frwd_lock);
2330
2331 iscsi_start_tx(conn);
2332 goto done;
2333
2334unlock:
2335 spin_unlock_bh(&session->frwd_lock);
2336done:
2337 ISCSI_DBG_EH(session, "dev reset result = %s\n",
2338 rc == SUCCESS ? "SUCCESS" : "FAILED");
2339 mutex_unlock(&session->eh_mutex);
2340 return rc;
2341}
2342EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
2343
2344void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
2345{
2346 struct iscsi_session *session = cls_session->dd_data;
2347
2348 spin_lock_bh(&session->frwd_lock);
2349 if (session->state != ISCSI_STATE_LOGGED_IN) {
2350 session->state = ISCSI_STATE_RECOVERY_FAILED;
Olivier Deprez0e641232021-09-23 10:07:05 +02002351 wake_up(&session->ehwait);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002352 }
2353 spin_unlock_bh(&session->frwd_lock);
2354}
2355EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
2356
2357/**
2358 * iscsi_eh_session_reset - drop session and attempt relogin
2359 * @sc: scsi command
2360 *
2361 * This function will wait for a relogin, session termination from
2362 * userspace, or a recovery/replacement timeout.
2363 */
2364int iscsi_eh_session_reset(struct scsi_cmnd *sc)
2365{
2366 struct iscsi_cls_session *cls_session;
2367 struct iscsi_session *session;
2368 struct iscsi_conn *conn;
2369
2370 cls_session = starget_to_session(scsi_target(sc->device));
2371 session = cls_session->dd_data;
2372 conn = session->leadconn;
2373
2374 mutex_lock(&session->eh_mutex);
2375 spin_lock_bh(&session->frwd_lock);
2376 if (session->state == ISCSI_STATE_TERMINATE) {
2377failed:
2378 ISCSI_DBG_EH(session,
2379 "failing session reset: Could not log back into "
2380 "%s [age %d]\n", session->targetname,
2381 session->age);
2382 spin_unlock_bh(&session->frwd_lock);
2383 mutex_unlock(&session->eh_mutex);
2384 return FAILED;
2385 }
2386
2387 spin_unlock_bh(&session->frwd_lock);
2388 mutex_unlock(&session->eh_mutex);
2389 /*
2390 * we drop the lock here but the leadconn cannot be destoyed while
2391 * we are in the scsi eh
2392 */
2393 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2394
2395 ISCSI_DBG_EH(session, "wait for relogin\n");
Olivier Deprez0e641232021-09-23 10:07:05 +02002396 wait_event_interruptible(session->ehwait,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002397 session->state == ISCSI_STATE_TERMINATE ||
2398 session->state == ISCSI_STATE_LOGGED_IN ||
2399 session->state == ISCSI_STATE_RECOVERY_FAILED);
2400 if (signal_pending(current))
2401 flush_signals(current);
2402
2403 mutex_lock(&session->eh_mutex);
2404 spin_lock_bh(&session->frwd_lock);
2405 if (session->state == ISCSI_STATE_LOGGED_IN) {
2406 ISCSI_DBG_EH(session,
2407 "session reset succeeded for %s,%s\n",
2408 session->targetname, conn->persistent_address);
2409 } else
2410 goto failed;
2411 spin_unlock_bh(&session->frwd_lock);
2412 mutex_unlock(&session->eh_mutex);
2413 return SUCCESS;
2414}
2415EXPORT_SYMBOL_GPL(iscsi_eh_session_reset);
2416
2417static void iscsi_prep_tgt_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2418{
2419 memset(hdr, 0, sizeof(*hdr));
2420 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2421 hdr->flags = ISCSI_TM_FUNC_TARGET_WARM_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2422 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2423 hdr->rtt = RESERVED_ITT;
2424}
2425
2426/**
2427 * iscsi_eh_target_reset - reset target
2428 * @sc: scsi command
2429 *
2430 * This will attempt to send a warm target reset.
2431 */
2432static int iscsi_eh_target_reset(struct scsi_cmnd *sc)
2433{
2434 struct iscsi_cls_session *cls_session;
2435 struct iscsi_session *session;
2436 struct iscsi_conn *conn;
2437 struct iscsi_tm *hdr;
2438 int rc = FAILED;
2439
2440 cls_session = starget_to_session(scsi_target(sc->device));
2441 session = cls_session->dd_data;
2442
2443 ISCSI_DBG_EH(session, "tgt Reset [sc %p tgt %s]\n", sc,
2444 session->targetname);
2445
2446 mutex_lock(&session->eh_mutex);
2447 spin_lock_bh(&session->frwd_lock);
2448 /*
2449 * Just check if we are not logged in. We cannot check for
2450 * the phase because the reset could come from a ioctl.
2451 */
2452 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2453 goto unlock;
2454 conn = session->leadconn;
2455
2456 /* only have one tmf outstanding at a time */
Olivier Deprez0e641232021-09-23 10:07:05 +02002457 if (session->tmf_state != TMF_INITIAL)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002458 goto unlock;
Olivier Deprez0e641232021-09-23 10:07:05 +02002459 session->tmf_state = TMF_QUEUED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002460
Olivier Deprez0e641232021-09-23 10:07:05 +02002461 hdr = &session->tmhdr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002462 iscsi_prep_tgt_reset_pdu(sc, hdr);
2463
2464 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
2465 session->tgt_reset_timeout)) {
2466 rc = FAILED;
2467 goto unlock;
2468 }
2469
Olivier Deprez0e641232021-09-23 10:07:05 +02002470 switch (session->tmf_state) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002471 case TMF_SUCCESS:
2472 break;
2473 case TMF_TIMEDOUT:
2474 spin_unlock_bh(&session->frwd_lock);
2475 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2476 goto done;
2477 default:
Olivier Deprez0e641232021-09-23 10:07:05 +02002478 session->tmf_state = TMF_INITIAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002479 goto unlock;
2480 }
2481
2482 rc = SUCCESS;
2483 spin_unlock_bh(&session->frwd_lock);
2484
2485 iscsi_suspend_tx(conn);
2486
2487 spin_lock_bh(&session->frwd_lock);
2488 memset(hdr, 0, sizeof(*hdr));
2489 fail_scsi_tasks(conn, -1, DID_ERROR);
Olivier Deprez0e641232021-09-23 10:07:05 +02002490 session->tmf_state = TMF_INITIAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002491 spin_unlock_bh(&session->frwd_lock);
2492
2493 iscsi_start_tx(conn);
2494 goto done;
2495
2496unlock:
2497 spin_unlock_bh(&session->frwd_lock);
2498done:
2499 ISCSI_DBG_EH(session, "tgt %s reset result = %s\n", session->targetname,
2500 rc == SUCCESS ? "SUCCESS" : "FAILED");
2501 mutex_unlock(&session->eh_mutex);
2502 return rc;
2503}
2504
2505/**
2506 * iscsi_eh_recover_target - reset target and possibly the session
2507 * @sc: scsi command
2508 *
2509 * This will attempt to send a warm target reset. If that fails,
2510 * we will escalate to ERL0 session recovery.
2511 */
2512int iscsi_eh_recover_target(struct scsi_cmnd *sc)
2513{
2514 int rc;
2515
2516 rc = iscsi_eh_target_reset(sc);
2517 if (rc == FAILED)
2518 rc = iscsi_eh_session_reset(sc);
2519 return rc;
2520}
2521EXPORT_SYMBOL_GPL(iscsi_eh_recover_target);
2522
2523/*
2524 * Pre-allocate a pool of @max items of @item_size. By default, the pool
2525 * should be accessed via kfifo_{get,put} on q->queue.
2526 * Optionally, the caller can obtain the array of object pointers
2527 * by passing in a non-NULL @items pointer
2528 */
2529int
2530iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
2531{
2532 int i, num_arrays = 1;
2533
2534 memset(q, 0, sizeof(*q));
2535
2536 q->max = max;
2537
2538 /* If the user passed an items pointer, he wants a copy of
2539 * the array. */
2540 if (items)
2541 num_arrays++;
2542 q->pool = kvcalloc(num_arrays * max, sizeof(void *), GFP_KERNEL);
2543 if (q->pool == NULL)
2544 return -ENOMEM;
2545
2546 kfifo_init(&q->queue, (void*)q->pool, max * sizeof(void*));
2547
2548 for (i = 0; i < max; i++) {
2549 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
2550 if (q->pool[i] == NULL) {
2551 q->max = i;
2552 goto enomem;
2553 }
2554 kfifo_in(&q->queue, (void*)&q->pool[i], sizeof(void*));
2555 }
2556
2557 if (items) {
2558 *items = q->pool + max;
2559 memcpy(*items, q->pool, max * sizeof(void *));
2560 }
2561
2562 return 0;
2563
2564enomem:
2565 iscsi_pool_free(q);
2566 return -ENOMEM;
2567}
2568EXPORT_SYMBOL_GPL(iscsi_pool_init);
2569
2570void iscsi_pool_free(struct iscsi_pool *q)
2571{
2572 int i;
2573
2574 for (i = 0; i < q->max; i++)
2575 kfree(q->pool[i]);
2576 kvfree(q->pool);
2577}
2578EXPORT_SYMBOL_GPL(iscsi_pool_free);
2579
2580/**
2581 * iscsi_host_add - add host to system
2582 * @shost: scsi host
2583 * @pdev: parent device
2584 *
2585 * This should be called by partial offload and software iscsi drivers
2586 * to add a host to the system.
2587 */
2588int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
2589{
2590 if (!shost->can_queue)
2591 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
2592
2593 if (!shost->cmd_per_lun)
2594 shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
2595
2596 return scsi_add_host(shost, pdev);
2597}
2598EXPORT_SYMBOL_GPL(iscsi_host_add);
2599
2600/**
2601 * iscsi_host_alloc - allocate a host and driver data
2602 * @sht: scsi host template
2603 * @dd_data_size: driver host data size
2604 * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
2605 *
2606 * This should be called by partial offload and software iscsi drivers.
2607 * To access the driver specific memory use the iscsi_host_priv() macro.
2608 */
2609struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
2610 int dd_data_size, bool xmit_can_sleep)
2611{
2612 struct Scsi_Host *shost;
2613 struct iscsi_host *ihost;
2614
2615 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2616 if (!shost)
2617 return NULL;
2618 ihost = shost_priv(shost);
2619
2620 if (xmit_can_sleep) {
2621 snprintf(ihost->workq_name, sizeof(ihost->workq_name),
2622 "iscsi_q_%d", shost->host_no);
2623 ihost->workq = create_singlethread_workqueue(ihost->workq_name);
2624 if (!ihost->workq)
2625 goto free_host;
2626 }
2627
2628 spin_lock_init(&ihost->lock);
2629 ihost->state = ISCSI_HOST_SETUP;
2630 ihost->num_sessions = 0;
2631 init_waitqueue_head(&ihost->session_removal_wq);
2632 return shost;
2633
2634free_host:
2635 scsi_host_put(shost);
2636 return NULL;
2637}
2638EXPORT_SYMBOL_GPL(iscsi_host_alloc);
2639
2640static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2641{
2642 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
2643}
2644
2645/**
2646 * iscsi_host_remove - remove host and sessions
2647 * @shost: scsi host
2648 *
2649 * If there are any sessions left, this will initiate the removal and wait
2650 * for the completion.
2651 */
2652void iscsi_host_remove(struct Scsi_Host *shost)
2653{
2654 struct iscsi_host *ihost = shost_priv(shost);
2655 unsigned long flags;
2656
2657 spin_lock_irqsave(&ihost->lock, flags);
2658 ihost->state = ISCSI_HOST_REMOVED;
2659 spin_unlock_irqrestore(&ihost->lock, flags);
2660
2661 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2662 wait_event_interruptible(ihost->session_removal_wq,
2663 ihost->num_sessions == 0);
2664 if (signal_pending(current))
2665 flush_signals(current);
2666
2667 scsi_remove_host(shost);
2668 if (ihost->workq)
2669 destroy_workqueue(ihost->workq);
2670}
2671EXPORT_SYMBOL_GPL(iscsi_host_remove);
2672
2673void iscsi_host_free(struct Scsi_Host *shost)
2674{
2675 struct iscsi_host *ihost = shost_priv(shost);
2676
2677 kfree(ihost->netdev);
2678 kfree(ihost->hwaddress);
2679 kfree(ihost->initiatorname);
2680 scsi_host_put(shost);
2681}
2682EXPORT_SYMBOL_GPL(iscsi_host_free);
2683
2684static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2685{
2686 struct iscsi_host *ihost = shost_priv(shost);
2687 unsigned long flags;
2688
2689 shost = scsi_host_get(shost);
2690 if (!shost) {
2691 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2692 "of session teardown event because host already "
2693 "removed.\n");
2694 return;
2695 }
2696
2697 spin_lock_irqsave(&ihost->lock, flags);
2698 ihost->num_sessions--;
2699 if (ihost->num_sessions == 0)
2700 wake_up(&ihost->session_removal_wq);
2701 spin_unlock_irqrestore(&ihost->lock, flags);
2702 scsi_host_put(shost);
2703}
2704
2705/**
2706 * iscsi_session_setup - create iscsi cls session and host and session
2707 * @iscsit: iscsi transport template
2708 * @shost: scsi host
2709 * @cmds_max: session can queue
2710 * @dd_size: private driver data size, added to session allocation size
2711 * @cmd_task_size: LLD task private data size
2712 * @initial_cmdsn: initial CmdSN
2713 * @id: target ID to add to this session
2714 *
2715 * This can be used by software iscsi_transports that allocate
2716 * a session per scsi host.
2717 *
2718 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2719 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2720 * for nop handling and login/logout requests.
2721 */
2722struct iscsi_cls_session *
2723iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
2724 uint16_t cmds_max, int dd_size, int cmd_task_size,
2725 uint32_t initial_cmdsn, unsigned int id)
2726{
2727 struct iscsi_host *ihost = shost_priv(shost);
2728 struct iscsi_session *session;
2729 struct iscsi_cls_session *cls_session;
2730 int cmd_i, scsi_cmds, total_cmds = cmds_max;
2731 unsigned long flags;
2732
2733 spin_lock_irqsave(&ihost->lock, flags);
2734 if (ihost->state == ISCSI_HOST_REMOVED) {
2735 spin_unlock_irqrestore(&ihost->lock, flags);
2736 return NULL;
2737 }
2738 ihost->num_sessions++;
2739 spin_unlock_irqrestore(&ihost->lock, flags);
2740
2741 if (!total_cmds)
2742 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
2743 /*
2744 * The iscsi layer needs some tasks for nop handling and tmfs,
2745 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2746 * + 1 command for scsi IO.
2747 */
2748 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2749 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2750 "must be a power of two that is at least %d.\n",
2751 total_cmds, ISCSI_TOTAL_CMDS_MIN);
2752 goto dec_session_count;
2753 }
2754
2755 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2756 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2757 "must be a power of 2 less than or equal to %d.\n",
2758 cmds_max, ISCSI_TOTAL_CMDS_MAX);
2759 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2760 }
2761
2762 if (!is_power_of_2(total_cmds)) {
2763 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2764 "must be a power of 2.\n", total_cmds);
2765 total_cmds = rounddown_pow_of_two(total_cmds);
2766 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2767 return NULL;
2768 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2769 total_cmds);
2770 }
2771 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
2772
2773 cls_session = iscsi_alloc_session(shost, iscsit,
2774 sizeof(struct iscsi_session) +
2775 dd_size);
2776 if (!cls_session)
2777 goto dec_session_count;
2778 session = cls_session->dd_data;
2779 session->cls_session = cls_session;
2780 session->host = shost;
2781 session->state = ISCSI_STATE_FREE;
2782 session->fast_abort = 1;
2783 session->tgt_reset_timeout = 30;
2784 session->lu_reset_timeout = 15;
2785 session->abort_timeout = 10;
2786 session->scsi_cmds_max = scsi_cmds;
2787 session->cmds_max = total_cmds;
2788 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
2789 session->exp_cmdsn = initial_cmdsn + 1;
2790 session->max_cmdsn = initial_cmdsn + 1;
2791 session->max_r2t = 1;
2792 session->tt = iscsit;
2793 session->dd_data = cls_session->dd_data + sizeof(*session);
2794
Olivier Deprez0e641232021-09-23 10:07:05 +02002795 session->tmf_state = TMF_INITIAL;
2796 timer_setup(&session->tmf_timer, iscsi_tmf_timedout, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002797 mutex_init(&session->eh_mutex);
Olivier Deprez0e641232021-09-23 10:07:05 +02002798
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002799 spin_lock_init(&session->frwd_lock);
2800 spin_lock_init(&session->back_lock);
2801
2802 /* initialize SCSI PDU commands pool */
2803 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2804 (void***)&session->cmds,
2805 cmd_task_size + sizeof(struct iscsi_task)))
2806 goto cmdpool_alloc_fail;
2807
2808 /* pre-format cmds pool with ITT */
2809 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2810 struct iscsi_task *task = session->cmds[cmd_i];
2811
2812 if (cmd_task_size)
2813 task->dd_data = &task[1];
2814 task->itt = cmd_i;
2815 task->state = ISCSI_TASK_FREE;
2816 INIT_LIST_HEAD(&task->running);
2817 }
2818
2819 if (!try_module_get(iscsit->owner))
2820 goto module_get_fail;
2821
2822 if (iscsi_add_session(cls_session, id))
2823 goto cls_session_fail;
2824
2825 return cls_session;
2826
2827cls_session_fail:
2828 module_put(iscsit->owner);
2829module_get_fail:
2830 iscsi_pool_free(&session->cmdpool);
2831cmdpool_alloc_fail:
2832 iscsi_free_session(cls_session);
2833dec_session_count:
2834 iscsi_host_dec_session_cnt(shost);
2835 return NULL;
2836}
2837EXPORT_SYMBOL_GPL(iscsi_session_setup);
2838
2839/**
2840 * iscsi_session_teardown - destroy session, host, and cls_session
2841 * @cls_session: iscsi session
2842 */
2843void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2844{
2845 struct iscsi_session *session = cls_session->dd_data;
2846 struct module *owner = cls_session->transport->owner;
2847 struct Scsi_Host *shost = session->host;
2848
2849 iscsi_pool_free(&session->cmdpool);
2850
2851 iscsi_remove_session(cls_session);
2852
2853 kfree(session->password);
2854 kfree(session->password_in);
2855 kfree(session->username);
2856 kfree(session->username_in);
2857 kfree(session->targetname);
2858 kfree(session->targetalias);
2859 kfree(session->initiatorname);
2860 kfree(session->boot_root);
2861 kfree(session->boot_nic);
2862 kfree(session->boot_target);
2863 kfree(session->ifacename);
2864 kfree(session->portal_type);
2865 kfree(session->discovery_parent_type);
2866
2867 iscsi_free_session(cls_session);
2868
2869 iscsi_host_dec_session_cnt(shost);
2870 module_put(owner);
2871}
2872EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2873
2874/**
2875 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2876 * @cls_session: iscsi_cls_session
2877 * @dd_size: private driver data size
2878 * @conn_idx: cid
2879 */
2880struct iscsi_cls_conn *
2881iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2882 uint32_t conn_idx)
2883{
2884 struct iscsi_session *session = cls_session->dd_data;
2885 struct iscsi_conn *conn;
2886 struct iscsi_cls_conn *cls_conn;
2887 char *data;
2888
2889 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2890 conn_idx);
2891 if (!cls_conn)
2892 return NULL;
2893 conn = cls_conn->dd_data;
2894 memset(conn, 0, sizeof(*conn) + dd_size);
2895
2896 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
2897 conn->session = session;
2898 conn->cls_conn = cls_conn;
2899 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2900 conn->id = conn_idx;
2901 conn->exp_statsn = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002902
2903 timer_setup(&conn->transport_timer, iscsi_check_transport_timeouts, 0);
2904
2905 INIT_LIST_HEAD(&conn->mgmtqueue);
2906 INIT_LIST_HEAD(&conn->cmdqueue);
2907 INIT_LIST_HEAD(&conn->requeue);
2908 spin_lock_init(&conn->taskqueuelock);
2909 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
2910
2911 /* allocate login_task used for the login/text sequences */
2912 spin_lock_bh(&session->frwd_lock);
2913 if (!kfifo_out(&session->cmdpool.queue,
2914 (void*)&conn->login_task,
2915 sizeof(void*))) {
2916 spin_unlock_bh(&session->frwd_lock);
2917 goto login_task_alloc_fail;
2918 }
2919 spin_unlock_bh(&session->frwd_lock);
2920
2921 data = (char *) __get_free_pages(GFP_KERNEL,
2922 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
2923 if (!data)
2924 goto login_task_data_alloc_fail;
2925 conn->login_task->data = conn->data = data;
2926
Olivier Deprez0e641232021-09-23 10:07:05 +02002927 init_waitqueue_head(&session->ehwait);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002928
2929 return cls_conn;
2930
2931login_task_data_alloc_fail:
2932 kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
2933 sizeof(void*));
2934login_task_alloc_fail:
2935 iscsi_destroy_conn(cls_conn);
2936 return NULL;
2937}
2938EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2939
2940/**
2941 * iscsi_conn_teardown - teardown iscsi connection
2942 * @cls_conn: iscsi class connection
2943 *
2944 * TODO: we may need to make this into a two step process
2945 * like scsi-mls remove + put host
2946 */
2947void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2948{
2949 struct iscsi_conn *conn = cls_conn->dd_data;
2950 struct iscsi_session *session = conn->session;
2951
2952 del_timer_sync(&conn->transport_timer);
2953
2954 mutex_lock(&session->eh_mutex);
2955 spin_lock_bh(&session->frwd_lock);
2956 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2957 if (session->leadconn == conn) {
2958 /*
2959 * leading connection? then give up on recovery.
2960 */
2961 session->state = ISCSI_STATE_TERMINATE;
Olivier Deprez0e641232021-09-23 10:07:05 +02002962 wake_up(&session->ehwait);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002963 }
2964 spin_unlock_bh(&session->frwd_lock);
2965
2966 /* flush queued up work because we free the connection below */
2967 iscsi_suspend_tx(conn);
2968
2969 spin_lock_bh(&session->frwd_lock);
2970 free_pages((unsigned long) conn->data,
2971 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
2972 kfree(conn->persistent_address);
2973 kfree(conn->local_ipaddr);
2974 /* regular RX path uses back_lock */
2975 spin_lock_bh(&session->back_lock);
2976 kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
2977 sizeof(void*));
2978 spin_unlock_bh(&session->back_lock);
2979 if (session->leadconn == conn)
2980 session->leadconn = NULL;
2981 spin_unlock_bh(&session->frwd_lock);
2982 mutex_unlock(&session->eh_mutex);
2983
2984 iscsi_destroy_conn(cls_conn);
2985}
2986EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2987
2988int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2989{
2990 struct iscsi_conn *conn = cls_conn->dd_data;
2991 struct iscsi_session *session = conn->session;
2992
2993 if (!session) {
2994 iscsi_conn_printk(KERN_ERR, conn,
2995 "can't start unbound connection\n");
2996 return -EPERM;
2997 }
2998
2999 if ((session->imm_data_en || !session->initial_r2t_en) &&
3000 session->first_burst > session->max_burst) {
3001 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
3002 "first_burst %d max_burst %d\n",
3003 session->first_burst, session->max_burst);
3004 return -EINVAL;
3005 }
3006
3007 if (conn->ping_timeout && !conn->recv_timeout) {
3008 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
3009 "zero. Using 5 seconds\n.");
3010 conn->recv_timeout = 5;
3011 }
3012
3013 if (conn->recv_timeout && !conn->ping_timeout) {
3014 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
3015 "zero. Using 5 seconds.\n");
3016 conn->ping_timeout = 5;
3017 }
3018
3019 spin_lock_bh(&session->frwd_lock);
3020 conn->c_stage = ISCSI_CONN_STARTED;
3021 session->state = ISCSI_STATE_LOGGED_IN;
3022 session->queued_cmdsn = session->cmdsn;
3023
3024 conn->last_recv = jiffies;
3025 conn->last_ping = jiffies;
3026 if (conn->recv_timeout && conn->ping_timeout)
3027 mod_timer(&conn->transport_timer,
3028 jiffies + (conn->recv_timeout * HZ));
3029
3030 switch(conn->stop_stage) {
3031 case STOP_CONN_RECOVER:
3032 /*
3033 * unblock eh_abort() if it is blocked. re-try all
3034 * commands after successful recovery
3035 */
3036 conn->stop_stage = 0;
Olivier Deprez0e641232021-09-23 10:07:05 +02003037 session->tmf_state = TMF_INITIAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003038 session->age++;
3039 if (session->age == 16)
3040 session->age = 0;
3041 break;
3042 case STOP_CONN_TERM:
3043 conn->stop_stage = 0;
3044 break;
3045 default:
3046 break;
3047 }
3048 spin_unlock_bh(&session->frwd_lock);
3049
3050 iscsi_unblock_session(session->cls_session);
Olivier Deprez0e641232021-09-23 10:07:05 +02003051 wake_up(&session->ehwait);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003052 return 0;
3053}
3054EXPORT_SYMBOL_GPL(iscsi_conn_start);
3055
3056static void
3057fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
3058{
3059 struct iscsi_task *task;
3060 int i, state;
3061
3062 for (i = 0; i < conn->session->cmds_max; i++) {
3063 task = conn->session->cmds[i];
3064 if (task->sc)
3065 continue;
3066
3067 if (task->state == ISCSI_TASK_FREE)
3068 continue;
3069
3070 ISCSI_DBG_SESSION(conn->session,
3071 "failing mgmt itt 0x%x state %d\n",
3072 task->itt, task->state);
3073 state = ISCSI_TASK_ABRT_SESS_RECOV;
3074 if (task->state == ISCSI_TASK_PENDING)
3075 state = ISCSI_TASK_COMPLETED;
David Brazdil0f672f62019-12-10 10:32:29 +00003076 spin_lock_bh(&session->back_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003077 iscsi_complete_task(task, state);
David Brazdil0f672f62019-12-10 10:32:29 +00003078 spin_unlock_bh(&session->back_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003079 }
3080}
3081
3082static void iscsi_start_session_recovery(struct iscsi_session *session,
3083 struct iscsi_conn *conn, int flag)
3084{
3085 int old_stop_stage;
3086
3087 mutex_lock(&session->eh_mutex);
3088 spin_lock_bh(&session->frwd_lock);
3089 if (conn->stop_stage == STOP_CONN_TERM) {
3090 spin_unlock_bh(&session->frwd_lock);
3091 mutex_unlock(&session->eh_mutex);
3092 return;
3093 }
3094
3095 /*
3096 * When this is called for the in_login state, we only want to clean
3097 * up the login task and connection. We do not need to block and set
3098 * the recovery state again
3099 */
3100 if (flag == STOP_CONN_TERM)
3101 session->state = ISCSI_STATE_TERMINATE;
3102 else if (conn->stop_stage != STOP_CONN_RECOVER)
3103 session->state = ISCSI_STATE_IN_RECOVERY;
3104
3105 old_stop_stage = conn->stop_stage;
3106 conn->stop_stage = flag;
3107 spin_unlock_bh(&session->frwd_lock);
3108
3109 del_timer_sync(&conn->transport_timer);
3110 iscsi_suspend_tx(conn);
3111
3112 spin_lock_bh(&session->frwd_lock);
3113 conn->c_stage = ISCSI_CONN_STOPPED;
3114 spin_unlock_bh(&session->frwd_lock);
3115
3116 /*
3117 * for connection level recovery we should not calculate
3118 * header digest. conn->hdr_size used for optimization
3119 * in hdr_extract() and will be re-negotiated at
3120 * set_param() time.
3121 */
3122 if (flag == STOP_CONN_RECOVER) {
3123 conn->hdrdgst_en = 0;
3124 conn->datadgst_en = 0;
3125 if (session->state == ISCSI_STATE_IN_RECOVERY &&
3126 old_stop_stage != STOP_CONN_RECOVER) {
3127 ISCSI_DBG_SESSION(session, "blocking session\n");
3128 iscsi_block_session(session->cls_session);
3129 }
3130 }
3131
3132 /*
3133 * flush queues.
3134 */
3135 spin_lock_bh(&session->frwd_lock);
3136 fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
3137 fail_mgmt_tasks(session, conn);
Olivier Deprez0e641232021-09-23 10:07:05 +02003138 memset(&session->tmhdr, 0, sizeof(session->tmhdr));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003139 spin_unlock_bh(&session->frwd_lock);
3140 mutex_unlock(&session->eh_mutex);
3141}
3142
3143void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
3144{
3145 struct iscsi_conn *conn = cls_conn->dd_data;
3146 struct iscsi_session *session = conn->session;
3147
3148 switch (flag) {
3149 case STOP_CONN_RECOVER:
3150 case STOP_CONN_TERM:
3151 iscsi_start_session_recovery(session, conn, flag);
3152 break;
3153 default:
3154 iscsi_conn_printk(KERN_ERR, conn,
3155 "invalid stop flag %d\n", flag);
3156 }
3157}
3158EXPORT_SYMBOL_GPL(iscsi_conn_stop);
3159
3160int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
3161 struct iscsi_cls_conn *cls_conn, int is_leading)
3162{
3163 struct iscsi_session *session = cls_session->dd_data;
3164 struct iscsi_conn *conn = cls_conn->dd_data;
3165
3166 spin_lock_bh(&session->frwd_lock);
3167 if (is_leading)
3168 session->leadconn = conn;
3169 spin_unlock_bh(&session->frwd_lock);
3170
3171 /*
3172 * Unblock xmitworker(), Login Phase will pass through.
3173 */
3174 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
3175 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
3176 return 0;
3177}
3178EXPORT_SYMBOL_GPL(iscsi_conn_bind);
3179
3180int iscsi_switch_str_param(char **param, char *new_val_buf)
3181{
3182 char *new_val;
3183
3184 if (*param) {
3185 if (!strcmp(*param, new_val_buf))
3186 return 0;
3187 }
3188
3189 new_val = kstrdup(new_val_buf, GFP_NOIO);
3190 if (!new_val)
3191 return -ENOMEM;
3192
3193 kfree(*param);
3194 *param = new_val;
3195 return 0;
3196}
3197EXPORT_SYMBOL_GPL(iscsi_switch_str_param);
3198
3199int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
3200 enum iscsi_param param, char *buf, int buflen)
3201{
3202 struct iscsi_conn *conn = cls_conn->dd_data;
3203 struct iscsi_session *session = conn->session;
3204 int val;
3205
3206 switch(param) {
3207 case ISCSI_PARAM_FAST_ABORT:
3208 sscanf(buf, "%d", &session->fast_abort);
3209 break;
3210 case ISCSI_PARAM_ABORT_TMO:
3211 sscanf(buf, "%d", &session->abort_timeout);
3212 break;
3213 case ISCSI_PARAM_LU_RESET_TMO:
3214 sscanf(buf, "%d", &session->lu_reset_timeout);
3215 break;
3216 case ISCSI_PARAM_TGT_RESET_TMO:
3217 sscanf(buf, "%d", &session->tgt_reset_timeout);
3218 break;
3219 case ISCSI_PARAM_PING_TMO:
3220 sscanf(buf, "%d", &conn->ping_timeout);
3221 break;
3222 case ISCSI_PARAM_RECV_TMO:
3223 sscanf(buf, "%d", &conn->recv_timeout);
3224 break;
3225 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3226 sscanf(buf, "%d", &conn->max_recv_dlength);
3227 break;
3228 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3229 sscanf(buf, "%d", &conn->max_xmit_dlength);
3230 break;
3231 case ISCSI_PARAM_HDRDGST_EN:
3232 sscanf(buf, "%d", &conn->hdrdgst_en);
3233 break;
3234 case ISCSI_PARAM_DATADGST_EN:
3235 sscanf(buf, "%d", &conn->datadgst_en);
3236 break;
3237 case ISCSI_PARAM_INITIAL_R2T_EN:
3238 sscanf(buf, "%d", &session->initial_r2t_en);
3239 break;
3240 case ISCSI_PARAM_MAX_R2T:
3241 sscanf(buf, "%hu", &session->max_r2t);
3242 break;
3243 case ISCSI_PARAM_IMM_DATA_EN:
3244 sscanf(buf, "%d", &session->imm_data_en);
3245 break;
3246 case ISCSI_PARAM_FIRST_BURST:
3247 sscanf(buf, "%d", &session->first_burst);
3248 break;
3249 case ISCSI_PARAM_MAX_BURST:
3250 sscanf(buf, "%d", &session->max_burst);
3251 break;
3252 case ISCSI_PARAM_PDU_INORDER_EN:
3253 sscanf(buf, "%d", &session->pdu_inorder_en);
3254 break;
3255 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3256 sscanf(buf, "%d", &session->dataseq_inorder_en);
3257 break;
3258 case ISCSI_PARAM_ERL:
3259 sscanf(buf, "%d", &session->erl);
3260 break;
3261 case ISCSI_PARAM_EXP_STATSN:
3262 sscanf(buf, "%u", &conn->exp_statsn);
3263 break;
3264 case ISCSI_PARAM_USERNAME:
3265 return iscsi_switch_str_param(&session->username, buf);
3266 case ISCSI_PARAM_USERNAME_IN:
3267 return iscsi_switch_str_param(&session->username_in, buf);
3268 case ISCSI_PARAM_PASSWORD:
3269 return iscsi_switch_str_param(&session->password, buf);
3270 case ISCSI_PARAM_PASSWORD_IN:
3271 return iscsi_switch_str_param(&session->password_in, buf);
3272 case ISCSI_PARAM_TARGET_NAME:
3273 return iscsi_switch_str_param(&session->targetname, buf);
3274 case ISCSI_PARAM_TARGET_ALIAS:
3275 return iscsi_switch_str_param(&session->targetalias, buf);
3276 case ISCSI_PARAM_TPGT:
3277 sscanf(buf, "%d", &session->tpgt);
3278 break;
3279 case ISCSI_PARAM_PERSISTENT_PORT:
3280 sscanf(buf, "%d", &conn->persistent_port);
3281 break;
3282 case ISCSI_PARAM_PERSISTENT_ADDRESS:
3283 return iscsi_switch_str_param(&conn->persistent_address, buf);
3284 case ISCSI_PARAM_IFACE_NAME:
3285 return iscsi_switch_str_param(&session->ifacename, buf);
3286 case ISCSI_PARAM_INITIATOR_NAME:
3287 return iscsi_switch_str_param(&session->initiatorname, buf);
3288 case ISCSI_PARAM_BOOT_ROOT:
3289 return iscsi_switch_str_param(&session->boot_root, buf);
3290 case ISCSI_PARAM_BOOT_NIC:
3291 return iscsi_switch_str_param(&session->boot_nic, buf);
3292 case ISCSI_PARAM_BOOT_TARGET:
3293 return iscsi_switch_str_param(&session->boot_target, buf);
3294 case ISCSI_PARAM_PORTAL_TYPE:
3295 return iscsi_switch_str_param(&session->portal_type, buf);
3296 case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
3297 return iscsi_switch_str_param(&session->discovery_parent_type,
3298 buf);
3299 case ISCSI_PARAM_DISCOVERY_SESS:
3300 sscanf(buf, "%d", &val);
3301 session->discovery_sess = !!val;
3302 break;
3303 case ISCSI_PARAM_LOCAL_IPADDR:
3304 return iscsi_switch_str_param(&conn->local_ipaddr, buf);
3305 default:
3306 return -ENOSYS;
3307 }
3308
3309 return 0;
3310}
3311EXPORT_SYMBOL_GPL(iscsi_set_param);
3312
3313int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
3314 enum iscsi_param param, char *buf)
3315{
3316 struct iscsi_session *session = cls_session->dd_data;
3317 int len;
3318
3319 switch(param) {
3320 case ISCSI_PARAM_FAST_ABORT:
Olivier Deprez0e641232021-09-23 10:07:05 +02003321 len = sysfs_emit(buf, "%d\n", session->fast_abort);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003322 break;
3323 case ISCSI_PARAM_ABORT_TMO:
Olivier Deprez0e641232021-09-23 10:07:05 +02003324 len = sysfs_emit(buf, "%d\n", session->abort_timeout);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003325 break;
3326 case ISCSI_PARAM_LU_RESET_TMO:
Olivier Deprez0e641232021-09-23 10:07:05 +02003327 len = sysfs_emit(buf, "%d\n", session->lu_reset_timeout);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003328 break;
3329 case ISCSI_PARAM_TGT_RESET_TMO:
Olivier Deprez0e641232021-09-23 10:07:05 +02003330 len = sysfs_emit(buf, "%d\n", session->tgt_reset_timeout);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003331 break;
3332 case ISCSI_PARAM_INITIAL_R2T_EN:
Olivier Deprez0e641232021-09-23 10:07:05 +02003333 len = sysfs_emit(buf, "%d\n", session->initial_r2t_en);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003334 break;
3335 case ISCSI_PARAM_MAX_R2T:
Olivier Deprez0e641232021-09-23 10:07:05 +02003336 len = sysfs_emit(buf, "%hu\n", session->max_r2t);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003337 break;
3338 case ISCSI_PARAM_IMM_DATA_EN:
Olivier Deprez0e641232021-09-23 10:07:05 +02003339 len = sysfs_emit(buf, "%d\n", session->imm_data_en);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003340 break;
3341 case ISCSI_PARAM_FIRST_BURST:
Olivier Deprez0e641232021-09-23 10:07:05 +02003342 len = sysfs_emit(buf, "%u\n", session->first_burst);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003343 break;
3344 case ISCSI_PARAM_MAX_BURST:
Olivier Deprez0e641232021-09-23 10:07:05 +02003345 len = sysfs_emit(buf, "%u\n", session->max_burst);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003346 break;
3347 case ISCSI_PARAM_PDU_INORDER_EN:
Olivier Deprez0e641232021-09-23 10:07:05 +02003348 len = sysfs_emit(buf, "%d\n", session->pdu_inorder_en);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003349 break;
3350 case ISCSI_PARAM_DATASEQ_INORDER_EN:
Olivier Deprez0e641232021-09-23 10:07:05 +02003351 len = sysfs_emit(buf, "%d\n", session->dataseq_inorder_en);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003352 break;
3353 case ISCSI_PARAM_DEF_TASKMGMT_TMO:
Olivier Deprez0e641232021-09-23 10:07:05 +02003354 len = sysfs_emit(buf, "%d\n", session->def_taskmgmt_tmo);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003355 break;
3356 case ISCSI_PARAM_ERL:
Olivier Deprez0e641232021-09-23 10:07:05 +02003357 len = sysfs_emit(buf, "%d\n", session->erl);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003358 break;
3359 case ISCSI_PARAM_TARGET_NAME:
Olivier Deprez0e641232021-09-23 10:07:05 +02003360 len = sysfs_emit(buf, "%s\n", session->targetname);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003361 break;
3362 case ISCSI_PARAM_TARGET_ALIAS:
Olivier Deprez0e641232021-09-23 10:07:05 +02003363 len = sysfs_emit(buf, "%s\n", session->targetalias);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003364 break;
3365 case ISCSI_PARAM_TPGT:
Olivier Deprez0e641232021-09-23 10:07:05 +02003366 len = sysfs_emit(buf, "%d\n", session->tpgt);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003367 break;
3368 case ISCSI_PARAM_USERNAME:
Olivier Deprez0e641232021-09-23 10:07:05 +02003369 len = sysfs_emit(buf, "%s\n", session->username);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003370 break;
3371 case ISCSI_PARAM_USERNAME_IN:
Olivier Deprez0e641232021-09-23 10:07:05 +02003372 len = sysfs_emit(buf, "%s\n", session->username_in);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003373 break;
3374 case ISCSI_PARAM_PASSWORD:
Olivier Deprez0e641232021-09-23 10:07:05 +02003375 len = sysfs_emit(buf, "%s\n", session->password);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003376 break;
3377 case ISCSI_PARAM_PASSWORD_IN:
Olivier Deprez0e641232021-09-23 10:07:05 +02003378 len = sysfs_emit(buf, "%s\n", session->password_in);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003379 break;
3380 case ISCSI_PARAM_IFACE_NAME:
Olivier Deprez0e641232021-09-23 10:07:05 +02003381 len = sysfs_emit(buf, "%s\n", session->ifacename);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003382 break;
3383 case ISCSI_PARAM_INITIATOR_NAME:
Olivier Deprez0e641232021-09-23 10:07:05 +02003384 len = sysfs_emit(buf, "%s\n", session->initiatorname);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003385 break;
3386 case ISCSI_PARAM_BOOT_ROOT:
Olivier Deprez0e641232021-09-23 10:07:05 +02003387 len = sysfs_emit(buf, "%s\n", session->boot_root);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003388 break;
3389 case ISCSI_PARAM_BOOT_NIC:
Olivier Deprez0e641232021-09-23 10:07:05 +02003390 len = sysfs_emit(buf, "%s\n", session->boot_nic);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003391 break;
3392 case ISCSI_PARAM_BOOT_TARGET:
Olivier Deprez0e641232021-09-23 10:07:05 +02003393 len = sysfs_emit(buf, "%s\n", session->boot_target);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003394 break;
3395 case ISCSI_PARAM_AUTO_SND_TGT_DISABLE:
Olivier Deprez0e641232021-09-23 10:07:05 +02003396 len = sysfs_emit(buf, "%u\n", session->auto_snd_tgt_disable);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003397 break;
3398 case ISCSI_PARAM_DISCOVERY_SESS:
Olivier Deprez0e641232021-09-23 10:07:05 +02003399 len = sysfs_emit(buf, "%u\n", session->discovery_sess);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003400 break;
3401 case ISCSI_PARAM_PORTAL_TYPE:
Olivier Deprez0e641232021-09-23 10:07:05 +02003402 len = sysfs_emit(buf, "%s\n", session->portal_type);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003403 break;
3404 case ISCSI_PARAM_CHAP_AUTH_EN:
Olivier Deprez0e641232021-09-23 10:07:05 +02003405 len = sysfs_emit(buf, "%u\n", session->chap_auth_en);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003406 break;
3407 case ISCSI_PARAM_DISCOVERY_LOGOUT_EN:
Olivier Deprez0e641232021-09-23 10:07:05 +02003408 len = sysfs_emit(buf, "%u\n", session->discovery_logout_en);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003409 break;
3410 case ISCSI_PARAM_BIDI_CHAP_EN:
Olivier Deprez0e641232021-09-23 10:07:05 +02003411 len = sysfs_emit(buf, "%u\n", session->bidi_chap_en);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003412 break;
3413 case ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL:
Olivier Deprez0e641232021-09-23 10:07:05 +02003414 len = sysfs_emit(buf, "%u\n", session->discovery_auth_optional);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003415 break;
3416 case ISCSI_PARAM_DEF_TIME2WAIT:
Olivier Deprez0e641232021-09-23 10:07:05 +02003417 len = sysfs_emit(buf, "%d\n", session->time2wait);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003418 break;
3419 case ISCSI_PARAM_DEF_TIME2RETAIN:
Olivier Deprez0e641232021-09-23 10:07:05 +02003420 len = sysfs_emit(buf, "%d\n", session->time2retain);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003421 break;
3422 case ISCSI_PARAM_TSID:
Olivier Deprez0e641232021-09-23 10:07:05 +02003423 len = sysfs_emit(buf, "%u\n", session->tsid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003424 break;
3425 case ISCSI_PARAM_ISID:
Olivier Deprez0e641232021-09-23 10:07:05 +02003426 len = sysfs_emit(buf, "%02x%02x%02x%02x%02x%02x\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003427 session->isid[0], session->isid[1],
3428 session->isid[2], session->isid[3],
3429 session->isid[4], session->isid[5]);
3430 break;
3431 case ISCSI_PARAM_DISCOVERY_PARENT_IDX:
Olivier Deprez0e641232021-09-23 10:07:05 +02003432 len = sysfs_emit(buf, "%u\n", session->discovery_parent_idx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003433 break;
3434 case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
3435 if (session->discovery_parent_type)
Olivier Deprez0e641232021-09-23 10:07:05 +02003436 len = sysfs_emit(buf, "%s\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003437 session->discovery_parent_type);
3438 else
Olivier Deprez0e641232021-09-23 10:07:05 +02003439 len = sysfs_emit(buf, "\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003440 break;
3441 default:
3442 return -ENOSYS;
3443 }
3444
3445 return len;
3446}
3447EXPORT_SYMBOL_GPL(iscsi_session_get_param);
3448
3449int iscsi_conn_get_addr_param(struct sockaddr_storage *addr,
3450 enum iscsi_param param, char *buf)
3451{
3452 struct sockaddr_in6 *sin6 = NULL;
3453 struct sockaddr_in *sin = NULL;
3454 int len;
3455
3456 switch (addr->ss_family) {
3457 case AF_INET:
3458 sin = (struct sockaddr_in *)addr;
3459 break;
3460 case AF_INET6:
3461 sin6 = (struct sockaddr_in6 *)addr;
3462 break;
3463 default:
3464 return -EINVAL;
3465 }
3466
3467 switch (param) {
3468 case ISCSI_PARAM_CONN_ADDRESS:
3469 case ISCSI_HOST_PARAM_IPADDRESS:
3470 if (sin)
Olivier Deprez0e641232021-09-23 10:07:05 +02003471 len = sysfs_emit(buf, "%pI4\n", &sin->sin_addr.s_addr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003472 else
Olivier Deprez0e641232021-09-23 10:07:05 +02003473 len = sysfs_emit(buf, "%pI6\n", &sin6->sin6_addr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003474 break;
3475 case ISCSI_PARAM_CONN_PORT:
3476 case ISCSI_PARAM_LOCAL_PORT:
3477 if (sin)
Olivier Deprez0e641232021-09-23 10:07:05 +02003478 len = sysfs_emit(buf, "%hu\n", be16_to_cpu(sin->sin_port));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003479 else
Olivier Deprez0e641232021-09-23 10:07:05 +02003480 len = sysfs_emit(buf, "%hu\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003481 be16_to_cpu(sin6->sin6_port));
3482 break;
3483 default:
3484 return -EINVAL;
3485 }
3486
3487 return len;
3488}
3489EXPORT_SYMBOL_GPL(iscsi_conn_get_addr_param);
3490
3491int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
3492 enum iscsi_param param, char *buf)
3493{
3494 struct iscsi_conn *conn = cls_conn->dd_data;
3495 int len;
3496
3497 switch(param) {
3498 case ISCSI_PARAM_PING_TMO:
Olivier Deprez0e641232021-09-23 10:07:05 +02003499 len = sysfs_emit(buf, "%u\n", conn->ping_timeout);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003500 break;
3501 case ISCSI_PARAM_RECV_TMO:
Olivier Deprez0e641232021-09-23 10:07:05 +02003502 len = sysfs_emit(buf, "%u\n", conn->recv_timeout);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003503 break;
3504 case ISCSI_PARAM_MAX_RECV_DLENGTH:
Olivier Deprez0e641232021-09-23 10:07:05 +02003505 len = sysfs_emit(buf, "%u\n", conn->max_recv_dlength);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003506 break;
3507 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
Olivier Deprez0e641232021-09-23 10:07:05 +02003508 len = sysfs_emit(buf, "%u\n", conn->max_xmit_dlength);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003509 break;
3510 case ISCSI_PARAM_HDRDGST_EN:
Olivier Deprez0e641232021-09-23 10:07:05 +02003511 len = sysfs_emit(buf, "%d\n", conn->hdrdgst_en);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003512 break;
3513 case ISCSI_PARAM_DATADGST_EN:
Olivier Deprez0e641232021-09-23 10:07:05 +02003514 len = sysfs_emit(buf, "%d\n", conn->datadgst_en);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003515 break;
3516 case ISCSI_PARAM_IFMARKER_EN:
Olivier Deprez0e641232021-09-23 10:07:05 +02003517 len = sysfs_emit(buf, "%d\n", conn->ifmarker_en);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003518 break;
3519 case ISCSI_PARAM_OFMARKER_EN:
Olivier Deprez0e641232021-09-23 10:07:05 +02003520 len = sysfs_emit(buf, "%d\n", conn->ofmarker_en);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003521 break;
3522 case ISCSI_PARAM_EXP_STATSN:
Olivier Deprez0e641232021-09-23 10:07:05 +02003523 len = sysfs_emit(buf, "%u\n", conn->exp_statsn);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003524 break;
3525 case ISCSI_PARAM_PERSISTENT_PORT:
Olivier Deprez0e641232021-09-23 10:07:05 +02003526 len = sysfs_emit(buf, "%d\n", conn->persistent_port);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003527 break;
3528 case ISCSI_PARAM_PERSISTENT_ADDRESS:
Olivier Deprez0e641232021-09-23 10:07:05 +02003529 len = sysfs_emit(buf, "%s\n", conn->persistent_address);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003530 break;
3531 case ISCSI_PARAM_STATSN:
Olivier Deprez0e641232021-09-23 10:07:05 +02003532 len = sysfs_emit(buf, "%u\n", conn->statsn);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003533 break;
3534 case ISCSI_PARAM_MAX_SEGMENT_SIZE:
Olivier Deprez0e641232021-09-23 10:07:05 +02003535 len = sysfs_emit(buf, "%u\n", conn->max_segment_size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003536 break;
3537 case ISCSI_PARAM_KEEPALIVE_TMO:
Olivier Deprez0e641232021-09-23 10:07:05 +02003538 len = sysfs_emit(buf, "%u\n", conn->keepalive_tmo);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003539 break;
3540 case ISCSI_PARAM_LOCAL_PORT:
Olivier Deprez0e641232021-09-23 10:07:05 +02003541 len = sysfs_emit(buf, "%u\n", conn->local_port);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003542 break;
3543 case ISCSI_PARAM_TCP_TIMESTAMP_STAT:
Olivier Deprez0e641232021-09-23 10:07:05 +02003544 len = sysfs_emit(buf, "%u\n", conn->tcp_timestamp_stat);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003545 break;
3546 case ISCSI_PARAM_TCP_NAGLE_DISABLE:
Olivier Deprez0e641232021-09-23 10:07:05 +02003547 len = sysfs_emit(buf, "%u\n", conn->tcp_nagle_disable);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003548 break;
3549 case ISCSI_PARAM_TCP_WSF_DISABLE:
Olivier Deprez0e641232021-09-23 10:07:05 +02003550 len = sysfs_emit(buf, "%u\n", conn->tcp_wsf_disable);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003551 break;
3552 case ISCSI_PARAM_TCP_TIMER_SCALE:
Olivier Deprez0e641232021-09-23 10:07:05 +02003553 len = sysfs_emit(buf, "%u\n", conn->tcp_timer_scale);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003554 break;
3555 case ISCSI_PARAM_TCP_TIMESTAMP_EN:
Olivier Deprez0e641232021-09-23 10:07:05 +02003556 len = sysfs_emit(buf, "%u\n", conn->tcp_timestamp_en);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003557 break;
3558 case ISCSI_PARAM_IP_FRAGMENT_DISABLE:
Olivier Deprez0e641232021-09-23 10:07:05 +02003559 len = sysfs_emit(buf, "%u\n", conn->fragment_disable);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003560 break;
3561 case ISCSI_PARAM_IPV4_TOS:
Olivier Deprez0e641232021-09-23 10:07:05 +02003562 len = sysfs_emit(buf, "%u\n", conn->ipv4_tos);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003563 break;
3564 case ISCSI_PARAM_IPV6_TC:
Olivier Deprez0e641232021-09-23 10:07:05 +02003565 len = sysfs_emit(buf, "%u\n", conn->ipv6_traffic_class);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003566 break;
3567 case ISCSI_PARAM_IPV6_FLOW_LABEL:
Olivier Deprez0e641232021-09-23 10:07:05 +02003568 len = sysfs_emit(buf, "%u\n", conn->ipv6_flow_label);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003569 break;
3570 case ISCSI_PARAM_IS_FW_ASSIGNED_IPV6:
Olivier Deprez0e641232021-09-23 10:07:05 +02003571 len = sysfs_emit(buf, "%u\n", conn->is_fw_assigned_ipv6);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003572 break;
3573 case ISCSI_PARAM_TCP_XMIT_WSF:
Olivier Deprez0e641232021-09-23 10:07:05 +02003574 len = sysfs_emit(buf, "%u\n", conn->tcp_xmit_wsf);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003575 break;
3576 case ISCSI_PARAM_TCP_RECV_WSF:
Olivier Deprez0e641232021-09-23 10:07:05 +02003577 len = sysfs_emit(buf, "%u\n", conn->tcp_recv_wsf);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003578 break;
3579 case ISCSI_PARAM_LOCAL_IPADDR:
Olivier Deprez0e641232021-09-23 10:07:05 +02003580 len = sysfs_emit(buf, "%s\n", conn->local_ipaddr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003581 break;
3582 default:
3583 return -ENOSYS;
3584 }
3585
3586 return len;
3587}
3588EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
3589
3590int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3591 char *buf)
3592{
3593 struct iscsi_host *ihost = shost_priv(shost);
3594 int len;
3595
3596 switch (param) {
3597 case ISCSI_HOST_PARAM_NETDEV_NAME:
Olivier Deprez0e641232021-09-23 10:07:05 +02003598 len = sysfs_emit(buf, "%s\n", ihost->netdev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003599 break;
3600 case ISCSI_HOST_PARAM_HWADDRESS:
Olivier Deprez0e641232021-09-23 10:07:05 +02003601 len = sysfs_emit(buf, "%s\n", ihost->hwaddress);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003602 break;
3603 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Olivier Deprez0e641232021-09-23 10:07:05 +02003604 len = sysfs_emit(buf, "%s\n", ihost->initiatorname);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003605 break;
3606 default:
3607 return -ENOSYS;
3608 }
3609
3610 return len;
3611}
3612EXPORT_SYMBOL_GPL(iscsi_host_get_param);
3613
3614int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3615 char *buf, int buflen)
3616{
3617 struct iscsi_host *ihost = shost_priv(shost);
3618
3619 switch (param) {
3620 case ISCSI_HOST_PARAM_NETDEV_NAME:
3621 return iscsi_switch_str_param(&ihost->netdev, buf);
3622 case ISCSI_HOST_PARAM_HWADDRESS:
3623 return iscsi_switch_str_param(&ihost->hwaddress, buf);
3624 case ISCSI_HOST_PARAM_INITIATOR_NAME:
3625 return iscsi_switch_str_param(&ihost->initiatorname, buf);
3626 default:
3627 return -ENOSYS;
3628 }
3629
3630 return 0;
3631}
3632EXPORT_SYMBOL_GPL(iscsi_host_set_param);
3633
3634MODULE_AUTHOR("Mike Christie");
3635MODULE_DESCRIPTION("iSCSI library functions");
3636MODULE_LICENSE("GPL");