• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * iSCSI lib functions
4  *
5  * Copyright (C) 2006 Red Hat, Inc.  All rights reserved.
6  * Copyright (C) 2004 - 2006 Mike Christie
7  * Copyright (C) 2004 - 2005 Dmitry Yusupov
8  * Copyright (C) 2004 - 2005 Alex Aizman
9  * maintained by open-iscsi@googlegroups.com
10  */
11 #include <linux/types.h>
12 #include <linux/kfifo.h>
13 #include <linux/delay.h>
14 #include <linux/log2.h>
15 #include <linux/slab.h>
16 #include <linux/sched/signal.h>
17 #include <linux/module.h>
18 #include <asm/unaligned.h>
19 #include <net/tcp.h>
20 #include <scsi/scsi_cmnd.h>
21 #include <scsi/scsi_device.h>
22 #include <scsi/scsi_eh.h>
23 #include <scsi/scsi_tcq.h>
24 #include <scsi/scsi_host.h>
25 #include <scsi/scsi.h>
26 #include <scsi/iscsi_proto.h>
27 #include <scsi/scsi_transport.h>
28 #include <scsi/scsi_transport_iscsi.h>
29 #include <scsi/libiscsi.h>
30 #include <trace/events/iscsi.h>
31 
32 static int iscsi_dbg_lib_conn;
33 module_param_named(debug_libiscsi_conn, iscsi_dbg_lib_conn, int,
34 		   S_IRUGO | S_IWUSR);
35 MODULE_PARM_DESC(debug_libiscsi_conn,
36 		 "Turn on debugging for connections in libiscsi module. "
37 		 "Set to 1 to turn on, and zero to turn off. Default is off.");
38 
39 static int iscsi_dbg_lib_session;
40 module_param_named(debug_libiscsi_session, iscsi_dbg_lib_session, int,
41 		   S_IRUGO | S_IWUSR);
42 MODULE_PARM_DESC(debug_libiscsi_session,
43 		 "Turn on debugging for sessions in libiscsi module. "
44 		 "Set to 1 to turn on, and zero to turn off. Default is off.");
45 
46 static int iscsi_dbg_lib_eh;
47 module_param_named(debug_libiscsi_eh, iscsi_dbg_lib_eh, int,
48 		   S_IRUGO | S_IWUSR);
49 MODULE_PARM_DESC(debug_libiscsi_eh,
50 		 "Turn on debugging for error handling in libiscsi module. "
51 		 "Set to 1 to turn on, and zero to turn off. Default is off.");
52 
53 #define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...)			\
54 	do {							\
55 		if (iscsi_dbg_lib_conn)				\
56 			iscsi_conn_printk(KERN_INFO, _conn,	\
57 					     "%s " dbg_fmt,	\
58 					     __func__, ##arg);	\
59 		iscsi_dbg_trace(trace_iscsi_dbg_conn,		\
60 				&(_conn)->cls_conn->dev,	\
61 				"%s " dbg_fmt, __func__, ##arg);\
62 	} while (0);
63 
64 #define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...)			\
65 	do {								\
66 		if (iscsi_dbg_lib_session)				\
67 			iscsi_session_printk(KERN_INFO, _session,	\
68 					     "%s " dbg_fmt,		\
69 					     __func__, ##arg);		\
70 		iscsi_dbg_trace(trace_iscsi_dbg_session, 		\
71 				&(_session)->cls_session->dev,		\
72 				"%s " dbg_fmt, __func__, ##arg);	\
73 	} while (0);
74 
75 #define ISCSI_DBG_EH(_session, dbg_fmt, arg...)				\
76 	do {								\
77 		if (iscsi_dbg_lib_eh)					\
78 			iscsi_session_printk(KERN_INFO, _session,	\
79 					     "%s " dbg_fmt,		\
80 					     __func__, ##arg);		\
81 		iscsi_dbg_trace(trace_iscsi_dbg_eh,			\
82 				&(_session)->cls_session->dev,		\
83 				"%s " dbg_fmt, __func__, ##arg);	\
84 	} while (0);
85 
iscsi_conn_queue_work(struct iscsi_conn * conn)86 inline void iscsi_conn_queue_work(struct iscsi_conn *conn)
87 {
88 	struct Scsi_Host *shost = conn->session->host;
89 	struct iscsi_host *ihost = shost_priv(shost);
90 
91 	if (ihost->workq)
92 		queue_work(ihost->workq, &conn->xmitwork);
93 }
94 EXPORT_SYMBOL_GPL(iscsi_conn_queue_work);
95 
__iscsi_update_cmdsn(struct iscsi_session * session,uint32_t exp_cmdsn,uint32_t max_cmdsn)96 static void __iscsi_update_cmdsn(struct iscsi_session *session,
97 				 uint32_t exp_cmdsn, uint32_t max_cmdsn)
98 {
99 	/*
100 	 * standard specifies this check for when to update expected and
101 	 * max sequence numbers
102 	 */
103 	if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
104 		return;
105 
106 	if (exp_cmdsn != session->exp_cmdsn &&
107 	    !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
108 		session->exp_cmdsn = exp_cmdsn;
109 
110 	if (max_cmdsn != session->max_cmdsn &&
111 	    !iscsi_sna_lt(max_cmdsn, session->max_cmdsn))
112 		session->max_cmdsn = max_cmdsn;
113 }
114 
iscsi_update_cmdsn(struct iscsi_session * session,struct iscsi_nopin * hdr)115 void iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
116 {
117 	__iscsi_update_cmdsn(session, be32_to_cpu(hdr->exp_cmdsn),
118 			     be32_to_cpu(hdr->max_cmdsn));
119 }
120 EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
121 
122 /**
123  * iscsi_prep_data_out_pdu - initialize Data-Out
124  * @task: scsi command task
125  * @r2t: R2T info
126  * @hdr: iscsi data in pdu
127  *
128  * Notes:
129  *	Initialize Data-Out within this R2T sequence and finds
130  *	proper data_offset within this SCSI command.
131  *
132  *	This function is called with connection lock taken.
133  **/
iscsi_prep_data_out_pdu(struct iscsi_task * task,struct iscsi_r2t_info * r2t,struct iscsi_data * hdr)134 void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
135 			   struct iscsi_data *hdr)
136 {
137 	struct iscsi_conn *conn = task->conn;
138 	unsigned int left = r2t->data_length - r2t->sent;
139 
140 	task->hdr_len = sizeof(struct iscsi_data);
141 
142 	memset(hdr, 0, sizeof(struct iscsi_data));
143 	hdr->ttt = r2t->ttt;
144 	hdr->datasn = cpu_to_be32(r2t->datasn);
145 	r2t->datasn++;
146 	hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
147 	hdr->lun = task->lun;
148 	hdr->itt = task->hdr_itt;
149 	hdr->exp_statsn = r2t->exp_statsn;
150 	hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
151 	if (left > conn->max_xmit_dlength) {
152 		hton24(hdr->dlength, conn->max_xmit_dlength);
153 		r2t->data_count = conn->max_xmit_dlength;
154 		hdr->flags = 0;
155 	} else {
156 		hton24(hdr->dlength, left);
157 		r2t->data_count = left;
158 		hdr->flags = ISCSI_FLAG_CMD_FINAL;
159 	}
160 	conn->dataout_pdus_cnt++;
161 }
162 EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
163 
iscsi_add_hdr(struct iscsi_task * task,unsigned len)164 static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
165 {
166 	unsigned exp_len = task->hdr_len + len;
167 
168 	if (exp_len > task->hdr_max) {
169 		WARN_ON(1);
170 		return -EINVAL;
171 	}
172 
173 	WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
174 	task->hdr_len = exp_len;
175 	return 0;
176 }
177 
178 /*
179  * make an extended cdb AHS
180  */
iscsi_prep_ecdb_ahs(struct iscsi_task * task)181 static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
182 {
183 	struct scsi_cmnd *cmd = task->sc;
184 	unsigned rlen, pad_len;
185 	unsigned short ahslength;
186 	struct iscsi_ecdb_ahdr *ecdb_ahdr;
187 	int rc;
188 
189 	ecdb_ahdr = iscsi_next_hdr(task);
190 	rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
191 
192 	BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
193 	ahslength = rlen + sizeof(ecdb_ahdr->reserved);
194 
195 	pad_len = iscsi_padding(rlen);
196 
197 	rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
198 	                   sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
199 	if (rc)
200 		return rc;
201 
202 	if (pad_len)
203 		memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
204 
205 	ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
206 	ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
207 	ecdb_ahdr->reserved = 0;
208 	memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
209 
210 	ISCSI_DBG_SESSION(task->conn->session,
211 			  "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
212 		          "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
213 		          "%u\n", cmd->cmd_len, rlen, pad_len, ahslength,
214 		          task->hdr_len);
215 	return 0;
216 }
217 
218 /**
219  * iscsi_check_tmf_restrictions - check if a task is affected by TMF
220  * @task: iscsi task
221  * @opcode: opcode to check for
222  *
223  * During TMF a task has to be checked if it's affected.
224  * All unrelated I/O can be passed through, but I/O to the
225  * affected LUN should be restricted.
226  * If 'fast_abort' is set we won't be sending any I/O to the
227  * affected LUN.
228  * Otherwise the target is waiting for all TTTs to be completed,
229  * so we have to send all outstanding Data-Out PDUs to the target.
230  */
iscsi_check_tmf_restrictions(struct iscsi_task * task,int opcode)231 static int iscsi_check_tmf_restrictions(struct iscsi_task *task, int opcode)
232 {
233 	struct iscsi_session *session = task->conn->session;
234 	struct iscsi_tm *tmf = &session->tmhdr;
235 	u64 hdr_lun;
236 
237 	if (session->tmf_state == TMF_INITIAL)
238 		return 0;
239 
240 	if ((tmf->opcode & ISCSI_OPCODE_MASK) != ISCSI_OP_SCSI_TMFUNC)
241 		return 0;
242 
243 	switch (ISCSI_TM_FUNC_VALUE(tmf)) {
244 	case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
245 		/*
246 		 * Allow PDUs for unrelated LUNs
247 		 */
248 		hdr_lun = scsilun_to_int(&tmf->lun);
249 		if (hdr_lun != task->sc->device->lun)
250 			return 0;
251 		fallthrough;
252 	case ISCSI_TM_FUNC_TARGET_WARM_RESET:
253 		/*
254 		 * Fail all SCSI cmd PDUs
255 		 */
256 		if (opcode != ISCSI_OP_SCSI_DATA_OUT) {
257 			iscsi_session_printk(KERN_INFO, session,
258 					     "task [op %x itt 0x%x/0x%x] rejected.\n",
259 					     opcode, task->itt, task->hdr_itt);
260 			return -EACCES;
261 		}
262 		/*
263 		 * And also all data-out PDUs in response to R2T
264 		 * if fast_abort is set.
265 		 */
266 		if (session->fast_abort) {
267 			iscsi_session_printk(KERN_INFO, session,
268 					     "task [op %x itt 0x%x/0x%x] fast abort.\n",
269 					     opcode, task->itt, task->hdr_itt);
270 			return -EACCES;
271 		}
272 		break;
273 	case ISCSI_TM_FUNC_ABORT_TASK:
274 		/*
275 		 * the caller has already checked if the task
276 		 * they want to abort was in the pending queue so if
277 		 * we are here the cmd pdu has gone out already, and
278 		 * we will only hit this for data-outs
279 		 */
280 		if (opcode == ISCSI_OP_SCSI_DATA_OUT &&
281 		    task->hdr_itt == tmf->rtt) {
282 			ISCSI_DBG_SESSION(session,
283 					  "Preventing task %x/%x from sending "
284 					  "data-out due to abort task in "
285 					  "progress\n", task->itt,
286 					  task->hdr_itt);
287 			return -EACCES;
288 		}
289 		break;
290 	}
291 
292 	return 0;
293 }
294 
295 /**
296  * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
297  * @task: iscsi task
298  *
299  * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
300  * fields like dlength or final based on how much data it sends
301  */
iscsi_prep_scsi_cmd_pdu(struct iscsi_task * task)302 static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
303 {
304 	struct iscsi_conn *conn = task->conn;
305 	struct iscsi_session *session = conn->session;
306 	struct scsi_cmnd *sc = task->sc;
307 	struct iscsi_scsi_req *hdr;
308 	unsigned hdrlength, cmd_len, transfer_length;
309 	itt_t itt;
310 	int rc;
311 
312 	rc = iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_CMD);
313 	if (rc)
314 		return rc;
315 
316 	if (conn->session->tt->alloc_pdu) {
317 		rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
318 		if (rc)
319 			return rc;
320 	}
321 	hdr = (struct iscsi_scsi_req *)task->hdr;
322 	itt = hdr->itt;
323 	memset(hdr, 0, sizeof(*hdr));
324 
325 	if (session->tt->parse_pdu_itt)
326 		hdr->itt = task->hdr_itt = itt;
327 	else
328 		hdr->itt = task->hdr_itt = build_itt(task->itt,
329 						     task->conn->session->age);
330 	task->hdr_len = 0;
331 	rc = iscsi_add_hdr(task, sizeof(*hdr));
332 	if (rc)
333 		return rc;
334 	hdr->opcode = ISCSI_OP_SCSI_CMD;
335 	hdr->flags = ISCSI_ATTR_SIMPLE;
336 	int_to_scsilun(sc->device->lun, &hdr->lun);
337 	task->lun = hdr->lun;
338 	hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
339 	cmd_len = sc->cmd_len;
340 	if (cmd_len < ISCSI_CDB_SIZE)
341 		memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
342 	else if (cmd_len > ISCSI_CDB_SIZE) {
343 		rc = iscsi_prep_ecdb_ahs(task);
344 		if (rc)
345 			return rc;
346 		cmd_len = ISCSI_CDB_SIZE;
347 	}
348 	memcpy(hdr->cdb, sc->cmnd, cmd_len);
349 
350 	task->imm_count = 0;
351 	if (scsi_get_prot_op(sc) != SCSI_PROT_NORMAL)
352 		task->protected = true;
353 
354 	transfer_length = scsi_transfer_length(sc);
355 	hdr->data_length = cpu_to_be32(transfer_length);
356 	if (sc->sc_data_direction == DMA_TO_DEVICE) {
357 		struct iscsi_r2t_info *r2t = &task->unsol_r2t;
358 
359 		hdr->flags |= ISCSI_FLAG_CMD_WRITE;
360 		/*
361 		 * Write counters:
362 		 *
363 		 *	imm_count	bytes to be sent right after
364 		 *			SCSI PDU Header
365 		 *
366 		 *	unsol_count	bytes(as Data-Out) to be sent
367 		 *			without	R2T ack right after
368 		 *			immediate data
369 		 *
370 		 *	r2t data_length bytes to be sent via R2T ack's
371 		 *
372 		 *      pad_count       bytes to be sent as zero-padding
373 		 */
374 		memset(r2t, 0, sizeof(*r2t));
375 
376 		if (session->imm_data_en) {
377 			if (transfer_length >= session->first_burst)
378 				task->imm_count = min(session->first_burst,
379 							conn->max_xmit_dlength);
380 			else
381 				task->imm_count = min(transfer_length,
382 						      conn->max_xmit_dlength);
383 			hton24(hdr->dlength, task->imm_count);
384 		} else
385 			zero_data(hdr->dlength);
386 
387 		if (!session->initial_r2t_en) {
388 			r2t->data_length = min(session->first_burst,
389 					       transfer_length) -
390 					       task->imm_count;
391 			r2t->data_offset = task->imm_count;
392 			r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
393 			r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
394 		}
395 
396 		if (!task->unsol_r2t.data_length)
397 			/* No unsolicit Data-Out's */
398 			hdr->flags |= ISCSI_FLAG_CMD_FINAL;
399 	} else {
400 		hdr->flags |= ISCSI_FLAG_CMD_FINAL;
401 		zero_data(hdr->dlength);
402 
403 		if (sc->sc_data_direction == DMA_FROM_DEVICE)
404 			hdr->flags |= ISCSI_FLAG_CMD_READ;
405 	}
406 
407 	/* calculate size of additional header segments (AHSs) */
408 	hdrlength = task->hdr_len - sizeof(*hdr);
409 
410 	WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
411 	hdrlength /= ISCSI_PAD_LEN;
412 
413 	WARN_ON(hdrlength >= 256);
414 	hdr->hlength = hdrlength & 0xFF;
415 	hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
416 
417 	if (session->tt->init_task && session->tt->init_task(task))
418 		return -EIO;
419 
420 	task->state = ISCSI_TASK_RUNNING;
421 	session->cmdsn++;
422 
423 	conn->scsicmd_pdus_cnt++;
424 	ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x "
425 			  "itt 0x%x len %d cmdsn %d win %d]\n",
426 			  sc->sc_data_direction == DMA_TO_DEVICE ?
427 			  "write" : "read", conn->id, sc, sc->cmnd[0],
428 			  task->itt, transfer_length,
429 			  session->cmdsn,
430 			  session->max_cmdsn - session->exp_cmdsn + 1);
431 	return 0;
432 }
433 
434 /**
435  * iscsi_free_task - free a task
436  * @task: iscsi cmd task
437  *
438  * Must be called with session back_lock.
439  * This function returns the scsi command to scsi-ml or cleans
440  * up mgmt tasks then returns the task to the pool.
441  */
iscsi_free_task(struct iscsi_task * task)442 static void iscsi_free_task(struct iscsi_task *task)
443 {
444 	struct iscsi_conn *conn = task->conn;
445 	struct iscsi_session *session = conn->session;
446 	struct scsi_cmnd *sc = task->sc;
447 	int oldstate = task->state;
448 
449 	ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n",
450 			  task->itt, task->state, task->sc);
451 
452 	session->tt->cleanup_task(task);
453 	task->state = ISCSI_TASK_FREE;
454 	task->sc = NULL;
455 	/*
456 	 * login task is preallocated so do not free
457 	 */
458 	if (conn->login_task == task)
459 		return;
460 
461 	kfifo_in(&session->cmdpool.queue, (void*)&task, sizeof(void*));
462 
463 	if (sc) {
464 		/* SCSI eh reuses commands to verify us */
465 		sc->SCp.ptr = NULL;
466 		/*
467 		 * queue command may call this to free the task, so
468 		 * it will decide how to return sc to scsi-ml.
469 		 */
470 		if (oldstate != ISCSI_TASK_REQUEUE_SCSIQ)
471 			sc->scsi_done(sc);
472 	}
473 }
474 
__iscsi_get_task(struct iscsi_task * task)475 void __iscsi_get_task(struct iscsi_task *task)
476 {
477 	refcount_inc(&task->refcount);
478 }
479 EXPORT_SYMBOL_GPL(__iscsi_get_task);
480 
__iscsi_put_task(struct iscsi_task * task)481 void __iscsi_put_task(struct iscsi_task *task)
482 {
483 	if (refcount_dec_and_test(&task->refcount))
484 		iscsi_free_task(task);
485 }
486 EXPORT_SYMBOL_GPL(__iscsi_put_task);
487 
iscsi_put_task(struct iscsi_task * task)488 void iscsi_put_task(struct iscsi_task *task)
489 {
490 	struct iscsi_session *session = task->conn->session;
491 
492 	/* regular RX path uses back_lock */
493 	spin_lock_bh(&session->back_lock);
494 	__iscsi_put_task(task);
495 	spin_unlock_bh(&session->back_lock);
496 }
497 EXPORT_SYMBOL_GPL(iscsi_put_task);
498 
499 /**
500  * iscsi_complete_task - finish a task
501  * @task: iscsi cmd task
502  * @state: state to complete task with
503  *
504  * Must be called with session back_lock.
505  */
iscsi_complete_task(struct iscsi_task * task,int state)506 static void iscsi_complete_task(struct iscsi_task *task, int state)
507 {
508 	struct iscsi_conn *conn = task->conn;
509 
510 	ISCSI_DBG_SESSION(conn->session,
511 			  "complete task itt 0x%x state %d sc %p\n",
512 			  task->itt, task->state, task->sc);
513 	if (task->state == ISCSI_TASK_COMPLETED ||
514 	    task->state == ISCSI_TASK_ABRT_TMF ||
515 	    task->state == ISCSI_TASK_ABRT_SESS_RECOV ||
516 	    task->state == ISCSI_TASK_REQUEUE_SCSIQ)
517 		return;
518 	WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
519 	task->state = state;
520 
521 	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 
531 	if (READ_ONCE(conn->ping_task) == task)
532 		WRITE_ONCE(conn->ping_task, NULL);
533 
534 	/* release get from queueing */
535 	__iscsi_put_task(task);
536 }
537 
538 /**
539  * iscsi_complete_scsi_task - finish scsi task normally
540  * @task: iscsi task for scsi cmd
541  * @exp_cmdsn: expected cmd sn in cpu format
542  * @max_cmdsn: max cmd sn in cpu format
543  *
544  * This is used when drivers do not need or cannot perform
545  * lower level pdu processing.
546  *
547  * Called with session back_lock
548  */
iscsi_complete_scsi_task(struct iscsi_task * task,uint32_t exp_cmdsn,uint32_t max_cmdsn)549 void iscsi_complete_scsi_task(struct iscsi_task *task,
550 			      uint32_t exp_cmdsn, uint32_t max_cmdsn)
551 {
552 	struct iscsi_conn *conn = task->conn;
553 
554 	ISCSI_DBG_SESSION(conn->session, "[itt 0x%x]\n", task->itt);
555 
556 	conn->last_recv = jiffies;
557 	__iscsi_update_cmdsn(conn->session, exp_cmdsn, max_cmdsn);
558 	iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
559 }
560 EXPORT_SYMBOL_GPL(iscsi_complete_scsi_task);
561 
562 
563 /*
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  */
fail_scsi_task(struct iscsi_task * task,int err)568 static 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;
597 	scsi_set_resid(sc, scsi_bufflen(sc));
598 
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 
iscsi_prep_mgmt_task(struct iscsi_conn * conn,struct iscsi_task * task)605 static 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 
651 static struct iscsi_task *
__iscsi_conn_send_pdu(struct iscsi_conn * conn,struct iscsi_hdr * hdr,char * data,uint32_t data_size)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 
736 	if (unlikely(READ_ONCE(conn->ping_task) == INVALID_SCSI_TASK))
737 		WRITE_ONCE(conn->ping_task, task);
738 
739 	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 
754 free_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 
iscsi_conn_send_pdu(struct iscsi_cls_conn * cls_conn,struct iscsi_hdr * hdr,char * data,uint32_t data_size)762 int 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 }
775 EXPORT_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
786  * then completes the command and task. called under back_lock
787  **/
iscsi_scsi_cmd_rsp(struct iscsi_conn * conn,struct iscsi_hdr * hdr,struct iscsi_task * task,char * data,int datalen)788 static 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) {
834 invalid_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)) {
855 		sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
856 	}
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 	}
870 out:
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
882  *
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
885  **/
886 static void
iscsi_data_in_rsp(struct iscsi_conn * conn,struct iscsi_hdr * hdr,struct iscsi_task * task)887 iscsi_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 ||
905 		     res_count <= sc->sdb.length))
906 			scsi_set_resid(sc, res_count);
907 		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 
iscsi_tmf_rsp(struct iscsi_conn * conn,struct iscsi_hdr * hdr)918 static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
919 {
920 	struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
921 	struct iscsi_session *session = conn->session;
922 
923 	conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
924 	conn->tmfrsp_pdus_cnt++;
925 
926 	if (session->tmf_state != TMF_QUEUED)
927 		return;
928 
929 	if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
930 		session->tmf_state = TMF_SUCCESS;
931 	else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
932 		session->tmf_state = TMF_NOT_FOUND;
933 	else
934 		session->tmf_state = TMF_FAILED;
935 	wake_up(&session->ehwait);
936 }
937 
iscsi_send_nopout(struct iscsi_conn * conn,struct iscsi_nopin * rhdr)938 static int iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
939 {
940         struct iscsi_nopout hdr;
941 	struct iscsi_task *task;
942 
943 	if (!rhdr) {
944 		if (READ_ONCE(conn->ping_task))
945 			return -EINVAL;
946 		WRITE_ONCE(conn->ping_task, INVALID_SCSI_TASK);
947 	}
948 
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) {
962 		if (!rhdr)
963 			WRITE_ONCE(conn->ping_task, NULL);
964 		iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
965 		return -EIO;
966 	} else if (!rhdr) {
967 		/* only track our nops */
968 		conn->last_ping = jiffies;
969 	}
970 
971 	return 0;
972 }
973 
974 /**
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  **/
iscsi_nop_out_rsp(struct iscsi_task * task,struct iscsi_nopin * nop,char * data,int datalen)984 static 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 
990 	if (READ_ONCE(conn->ping_task) != task) {
991 		/*
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 
iscsi_handle_reject(struct iscsi_conn * conn,struct iscsi_hdr * hdr,char * data,int datalen)1004 static 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  */
iscsi_itt_to_task(struct iscsi_conn * conn,itt_t itt)1097 struct 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 }
1114 EXPORT_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  */
__iscsi_complete_pdu(struct iscsi_conn * conn,struct iscsi_hdr * hdr,char * data,int datalen)1127 int __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 
1258 out:
1259 	return rc;
1260 recv_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 }
1266 EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
1267 
iscsi_complete_pdu(struct iscsi_conn * conn,struct iscsi_hdr * hdr,char * data,int datalen)1268 int 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 }
1278 EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
1279 
iscsi_verify_itt(struct iscsi_conn * conn,itt_t itt)1280 int 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 }
1310 EXPORT_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  */
iscsi_itt_to_ctask(struct iscsi_conn * conn,itt_t itt)1321 struct 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 }
1341 EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
1342 
iscsi_session_failure(struct iscsi_session * session,enum iscsi_err err)1343 void iscsi_session_failure(struct iscsi_session *session,
1344 			   enum iscsi_err err)
1345 {
1346 	struct iscsi_conn *conn;
1347 
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 
1355 	iscsi_get_conn(conn->cls_conn);
1356 	spin_unlock_bh(&session->frwd_lock);
1357 	/*
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);
1366 	iscsi_put_conn(conn->cls_conn);
1367 }
1368 EXPORT_SYMBOL_GPL(iscsi_session_failure);
1369 
iscsi_set_conn_failed(struct iscsi_conn * conn)1370 static bool iscsi_set_conn_failed(struct iscsi_conn *conn)
1371 {
1372 	struct iscsi_session *session = conn->session;
1373 
1374 	if (session->state == ISCSI_STATE_FAILED)
1375 		return false;
1376 
1377 	if (conn->stop_stage == 0)
1378 		session->state = ISCSI_STATE_FAILED;
1379 
1380 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1381 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1382 	return true;
1383 }
1384 
iscsi_conn_failure(struct iscsi_conn * conn,enum iscsi_err err)1385 void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1386 {
1387 	struct iscsi_session *session = conn->session;
1388 	bool needs_evt;
1389 
1390 	spin_lock_bh(&session->frwd_lock);
1391 	needs_evt = iscsi_set_conn_failed(conn);
1392 	spin_unlock_bh(&session->frwd_lock);
1393 
1394 	if (needs_evt)
1395 		iscsi_conn_error_event(conn->cls_conn, err);
1396 }
1397 EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1398 
iscsi_check_cmdsn_window_closed(struct iscsi_conn * conn)1399 static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1400 {
1401 	struct iscsi_session *session = conn->session;
1402 
1403 	/*
1404 	 * Check for iSCSI window and take care of CmdSN wrap-around
1405 	 */
1406 	if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
1407 		ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
1408 				  "%u MaxCmdSN %u CmdSN %u/%u\n",
1409 				  session->exp_cmdsn, session->max_cmdsn,
1410 				  session->cmdsn, session->queued_cmdsn);
1411 		return -ENOSPC;
1412 	}
1413 	return 0;
1414 }
1415 
iscsi_xmit_task(struct iscsi_conn * conn)1416 static int iscsi_xmit_task(struct iscsi_conn *conn)
1417 {
1418 	struct iscsi_task *task = conn->task;
1419 	int rc;
1420 
1421 	if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx))
1422 		return -ENODATA;
1423 
1424 	spin_lock_bh(&conn->session->back_lock);
1425 	if (conn->task == NULL) {
1426 		spin_unlock_bh(&conn->session->back_lock);
1427 		return -ENODATA;
1428 	}
1429 	__iscsi_get_task(task);
1430 	spin_unlock_bh(&conn->session->back_lock);
1431 	spin_unlock_bh(&conn->session->frwd_lock);
1432 	rc = conn->session->tt->xmit_task(task);
1433 	spin_lock_bh(&conn->session->frwd_lock);
1434 	if (!rc) {
1435 		/* done with this task */
1436 		task->last_xfer = jiffies;
1437 		conn->task = NULL;
1438 	}
1439 	/* regular RX path uses back_lock */
1440 	spin_lock(&conn->session->back_lock);
1441 	__iscsi_put_task(task);
1442 	spin_unlock(&conn->session->back_lock);
1443 	return rc;
1444 }
1445 
1446 /**
1447  * iscsi_requeue_task - requeue task to run from session workqueue
1448  * @task: task to requeue
1449  *
1450  * LLDs that need to run a task from the session workqueue should call
1451  * this. The session frwd_lock must be held. This should only be called
1452  * by software drivers.
1453  */
iscsi_requeue_task(struct iscsi_task * task)1454 void iscsi_requeue_task(struct iscsi_task *task)
1455 {
1456 	struct iscsi_conn *conn = task->conn;
1457 
1458 	/*
1459 	 * this may be on the requeue list already if the xmit_task callout
1460 	 * is handling the r2ts while we are adding new ones
1461 	 */
1462 	spin_lock_bh(&conn->taskqueuelock);
1463 	if (list_empty(&task->running))
1464 		list_add_tail(&task->running, &conn->requeue);
1465 	spin_unlock_bh(&conn->taskqueuelock);
1466 	iscsi_conn_queue_work(conn);
1467 }
1468 EXPORT_SYMBOL_GPL(iscsi_requeue_task);
1469 
1470 /**
1471  * iscsi_data_xmit - xmit any command into the scheduled connection
1472  * @conn: iscsi connection
1473  *
1474  * Notes:
1475  *	The function can return -EAGAIN in which case the caller must
1476  *	re-schedule it again later or recover. '0' return code means
1477  *	successful xmit.
1478  **/
iscsi_data_xmit(struct iscsi_conn * conn)1479 static int iscsi_data_xmit(struct iscsi_conn *conn)
1480 {
1481 	struct iscsi_task *task;
1482 	int rc = 0;
1483 
1484 	spin_lock_bh(&conn->session->frwd_lock);
1485 	if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1486 		ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
1487 		spin_unlock_bh(&conn->session->frwd_lock);
1488 		return -ENODATA;
1489 	}
1490 
1491 	if (conn->task) {
1492 		rc = iscsi_xmit_task(conn);
1493 	        if (rc)
1494 		        goto done;
1495 	}
1496 
1497 	/*
1498 	 * process mgmt pdus like nops before commands since we should
1499 	 * only have one nop-out as a ping from us and targets should not
1500 	 * overflow us with nop-ins
1501 	 */
1502 	spin_lock_bh(&conn->taskqueuelock);
1503 check_mgmt:
1504 	while (!list_empty(&conn->mgmtqueue)) {
1505 		conn->task = list_entry(conn->mgmtqueue.next,
1506 					 struct iscsi_task, running);
1507 		list_del_init(&conn->task->running);
1508 		spin_unlock_bh(&conn->taskqueuelock);
1509 		if (iscsi_prep_mgmt_task(conn, conn->task)) {
1510 			/* regular RX path uses back_lock */
1511 			spin_lock_bh(&conn->session->back_lock);
1512 			__iscsi_put_task(conn->task);
1513 			spin_unlock_bh(&conn->session->back_lock);
1514 			conn->task = NULL;
1515 			spin_lock_bh(&conn->taskqueuelock);
1516 			continue;
1517 		}
1518 		rc = iscsi_xmit_task(conn);
1519 		if (rc)
1520 			goto done;
1521 		spin_lock_bh(&conn->taskqueuelock);
1522 	}
1523 
1524 	/* process pending command queue */
1525 	while (!list_empty(&conn->cmdqueue)) {
1526 		conn->task = list_entry(conn->cmdqueue.next, struct iscsi_task,
1527 					running);
1528 		list_del_init(&conn->task->running);
1529 		spin_unlock_bh(&conn->taskqueuelock);
1530 		if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
1531 			fail_scsi_task(conn->task, DID_IMM_RETRY);
1532 			spin_lock_bh(&conn->taskqueuelock);
1533 			continue;
1534 		}
1535 		rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1536 		if (rc) {
1537 			if (rc == -ENOMEM || rc == -EACCES)
1538 				fail_scsi_task(conn->task, DID_IMM_RETRY);
1539 			else
1540 				fail_scsi_task(conn->task, DID_ABORT);
1541 			spin_lock_bh(&conn->taskqueuelock);
1542 			continue;
1543 		}
1544 		rc = iscsi_xmit_task(conn);
1545 		if (rc)
1546 			goto done;
1547 		/*
1548 		 * we could continuously get new task requests so
1549 		 * we need to check the mgmt queue for nops that need to
1550 		 * be sent to aviod starvation
1551 		 */
1552 		spin_lock_bh(&conn->taskqueuelock);
1553 		if (!list_empty(&conn->mgmtqueue))
1554 			goto check_mgmt;
1555 	}
1556 
1557 	while (!list_empty(&conn->requeue)) {
1558 		/*
1559 		 * we always do fastlogout - conn stop code will clean up.
1560 		 */
1561 		if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1562 			break;
1563 
1564 		task = list_entry(conn->requeue.next, struct iscsi_task,
1565 				  running);
1566 		if (iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_DATA_OUT))
1567 			break;
1568 
1569 		conn->task = task;
1570 		list_del_init(&conn->task->running);
1571 		conn->task->state = ISCSI_TASK_RUNNING;
1572 		spin_unlock_bh(&conn->taskqueuelock);
1573 		rc = iscsi_xmit_task(conn);
1574 		if (rc)
1575 			goto done;
1576 		spin_lock_bh(&conn->taskqueuelock);
1577 		if (!list_empty(&conn->mgmtqueue))
1578 			goto check_mgmt;
1579 	}
1580 	spin_unlock_bh(&conn->taskqueuelock);
1581 	spin_unlock_bh(&conn->session->frwd_lock);
1582 	return -ENODATA;
1583 
1584 done:
1585 	spin_unlock_bh(&conn->session->frwd_lock);
1586 	return rc;
1587 }
1588 
iscsi_xmitworker(struct work_struct * work)1589 static void iscsi_xmitworker(struct work_struct *work)
1590 {
1591 	struct iscsi_conn *conn =
1592 		container_of(work, struct iscsi_conn, xmitwork);
1593 	int rc;
1594 	/*
1595 	 * serialize Xmit worker on a per-connection basis.
1596 	 */
1597 	do {
1598 		rc = iscsi_data_xmit(conn);
1599 	} while (rc >= 0 || rc == -EAGAIN);
1600 }
1601 
iscsi_alloc_task(struct iscsi_conn * conn,struct scsi_cmnd * sc)1602 static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1603 						  struct scsi_cmnd *sc)
1604 {
1605 	struct iscsi_task *task;
1606 
1607 	if (!kfifo_out(&conn->session->cmdpool.queue,
1608 			 (void *) &task, sizeof(void *)))
1609 		return NULL;
1610 
1611 	sc->SCp.phase = conn->session->age;
1612 	sc->SCp.ptr = (char *) task;
1613 
1614 	refcount_set(&task->refcount, 1);
1615 	task->state = ISCSI_TASK_PENDING;
1616 	task->conn = conn;
1617 	task->sc = sc;
1618 	task->have_checked_conn = false;
1619 	task->last_timeout = jiffies;
1620 	task->last_xfer = jiffies;
1621 	task->protected = false;
1622 	INIT_LIST_HEAD(&task->running);
1623 	return task;
1624 }
1625 
1626 enum {
1627 	FAILURE_BAD_HOST = 1,
1628 	FAILURE_SESSION_FAILED,
1629 	FAILURE_SESSION_FREED,
1630 	FAILURE_WINDOW_CLOSED,
1631 	FAILURE_OOM,
1632 	FAILURE_SESSION_TERMINATE,
1633 	FAILURE_SESSION_IN_RECOVERY,
1634 	FAILURE_SESSION_RECOVERY_TIMEOUT,
1635 	FAILURE_SESSION_LOGGING_OUT,
1636 	FAILURE_SESSION_NOT_READY,
1637 };
1638 
iscsi_queuecommand(struct Scsi_Host * host,struct scsi_cmnd * sc)1639 int iscsi_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *sc)
1640 {
1641 	struct iscsi_cls_session *cls_session;
1642 	struct iscsi_host *ihost;
1643 	int reason = 0;
1644 	struct iscsi_session *session;
1645 	struct iscsi_conn *conn;
1646 	struct iscsi_task *task = NULL;
1647 
1648 	sc->result = 0;
1649 	sc->SCp.ptr = NULL;
1650 
1651 	ihost = shost_priv(host);
1652 
1653 	cls_session = starget_to_session(scsi_target(sc->device));
1654 	session = cls_session->dd_data;
1655 	spin_lock_bh(&session->frwd_lock);
1656 
1657 	reason = iscsi_session_chkready(cls_session);
1658 	if (reason) {
1659 		sc->result = reason;
1660 		goto fault;
1661 	}
1662 
1663 	if (session->state != ISCSI_STATE_LOGGED_IN) {
1664 		/*
1665 		 * to handle the race between when we set the recovery state
1666 		 * and block the session we requeue here (commands could
1667 		 * be entering our queuecommand while a block is starting
1668 		 * up because the block code is not locked)
1669 		 */
1670 		switch (session->state) {
1671 		case ISCSI_STATE_FAILED:
1672 			/*
1673 			 * cmds should fail during shutdown, if the session
1674 			 * state is bad, allowing completion to happen
1675 			 */
1676 			if (unlikely(system_state != SYSTEM_RUNNING)) {
1677 				reason = FAILURE_SESSION_FAILED;
1678 				sc->result = DID_NO_CONNECT << 16;
1679 				break;
1680 			}
1681 			fallthrough;
1682 		case ISCSI_STATE_IN_RECOVERY:
1683 			reason = FAILURE_SESSION_IN_RECOVERY;
1684 			sc->result = DID_IMM_RETRY << 16;
1685 			break;
1686 		case ISCSI_STATE_LOGGING_OUT:
1687 			reason = FAILURE_SESSION_LOGGING_OUT;
1688 			sc->result = DID_IMM_RETRY << 16;
1689 			break;
1690 		case ISCSI_STATE_RECOVERY_FAILED:
1691 			reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
1692 			sc->result = DID_TRANSPORT_FAILFAST << 16;
1693 			break;
1694 		case ISCSI_STATE_TERMINATE:
1695 			reason = FAILURE_SESSION_TERMINATE;
1696 			sc->result = DID_NO_CONNECT << 16;
1697 			break;
1698 		default:
1699 			reason = FAILURE_SESSION_FREED;
1700 			sc->result = DID_NO_CONNECT << 16;
1701 		}
1702 		goto fault;
1703 	}
1704 
1705 	conn = session->leadconn;
1706 	if (!conn) {
1707 		reason = FAILURE_SESSION_FREED;
1708 		sc->result = DID_NO_CONNECT << 16;
1709 		goto fault;
1710 	}
1711 
1712 	if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1713 		reason = FAILURE_SESSION_IN_RECOVERY;
1714 		sc->result = DID_REQUEUE << 16;
1715 		goto fault;
1716 	}
1717 
1718 	if (iscsi_check_cmdsn_window_closed(conn)) {
1719 		reason = FAILURE_WINDOW_CLOSED;
1720 		goto reject;
1721 	}
1722 
1723 	task = iscsi_alloc_task(conn, sc);
1724 	if (!task) {
1725 		reason = FAILURE_OOM;
1726 		goto reject;
1727 	}
1728 
1729 	if (!ihost->workq) {
1730 		reason = iscsi_prep_scsi_cmd_pdu(task);
1731 		if (reason) {
1732 			if (reason == -ENOMEM ||  reason == -EACCES) {
1733 				reason = FAILURE_OOM;
1734 				goto prepd_reject;
1735 			} else {
1736 				sc->result = DID_ABORT << 16;
1737 				goto prepd_fault;
1738 			}
1739 		}
1740 		if (session->tt->xmit_task(task)) {
1741 			session->cmdsn--;
1742 			reason = FAILURE_SESSION_NOT_READY;
1743 			goto prepd_reject;
1744 		}
1745 	} else {
1746 		spin_lock_bh(&conn->taskqueuelock);
1747 		list_add_tail(&task->running, &conn->cmdqueue);
1748 		spin_unlock_bh(&conn->taskqueuelock);
1749 		iscsi_conn_queue_work(conn);
1750 	}
1751 
1752 	session->queued_cmdsn++;
1753 	spin_unlock_bh(&session->frwd_lock);
1754 	return 0;
1755 
1756 prepd_reject:
1757 	spin_lock_bh(&session->back_lock);
1758 	iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
1759 	spin_unlock_bh(&session->back_lock);
1760 reject:
1761 	spin_unlock_bh(&session->frwd_lock);
1762 	ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
1763 			  sc->cmnd[0], reason);
1764 	return SCSI_MLQUEUE_TARGET_BUSY;
1765 
1766 prepd_fault:
1767 	spin_lock_bh(&session->back_lock);
1768 	iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
1769 	spin_unlock_bh(&session->back_lock);
1770 fault:
1771 	spin_unlock_bh(&session->frwd_lock);
1772 	ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
1773 			  sc->cmnd[0], reason);
1774 	scsi_set_resid(sc, scsi_bufflen(sc));
1775 	sc->scsi_done(sc);
1776 	return 0;
1777 }
1778 EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1779 
iscsi_target_alloc(struct scsi_target * starget)1780 int iscsi_target_alloc(struct scsi_target *starget)
1781 {
1782 	struct iscsi_cls_session *cls_session = starget_to_session(starget);
1783 	struct iscsi_session *session = cls_session->dd_data;
1784 
1785 	starget->can_queue = session->scsi_cmds_max;
1786 	return 0;
1787 }
1788 EXPORT_SYMBOL_GPL(iscsi_target_alloc);
1789 
iscsi_tmf_timedout(struct timer_list * t)1790 static void iscsi_tmf_timedout(struct timer_list *t)
1791 {
1792 	struct iscsi_session *session = from_timer(session, t, tmf_timer);
1793 
1794 	spin_lock(&session->frwd_lock);
1795 	if (session->tmf_state == TMF_QUEUED) {
1796 		session->tmf_state = TMF_TIMEDOUT;
1797 		ISCSI_DBG_EH(session, "tmf timedout\n");
1798 		/* unblock eh_abort() */
1799 		wake_up(&session->ehwait);
1800 	}
1801 	spin_unlock(&session->frwd_lock);
1802 }
1803 
iscsi_exec_task_mgmt_fn(struct iscsi_conn * conn,struct iscsi_tm * hdr,int age,int timeout)1804 static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
1805 				   struct iscsi_tm *hdr, int age,
1806 				   int timeout)
1807 	__must_hold(&session->frwd_lock)
1808 {
1809 	struct iscsi_session *session = conn->session;
1810 	struct iscsi_task *task;
1811 
1812 	task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1813 				      NULL, 0);
1814 	if (!task) {
1815 		spin_unlock_bh(&session->frwd_lock);
1816 		iscsi_conn_printk(KERN_ERR, conn, "Could not send TMF.\n");
1817 		iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1818 		spin_lock_bh(&session->frwd_lock);
1819 		return -EPERM;
1820 	}
1821 	conn->tmfcmd_pdus_cnt++;
1822 	session->tmf_timer.expires = timeout * HZ + jiffies;
1823 	add_timer(&session->tmf_timer);
1824 	ISCSI_DBG_EH(session, "tmf set timeout\n");
1825 
1826 	spin_unlock_bh(&session->frwd_lock);
1827 	mutex_unlock(&session->eh_mutex);
1828 
1829 	/*
1830 	 * block eh thread until:
1831 	 *
1832 	 * 1) tmf response
1833 	 * 2) tmf timeout
1834 	 * 3) session is terminated or restarted or userspace has
1835 	 * given up on recovery
1836 	 */
1837 	wait_event_interruptible(session->ehwait, age != session->age ||
1838 				 session->state != ISCSI_STATE_LOGGED_IN ||
1839 				 session->tmf_state != TMF_QUEUED);
1840 	if (signal_pending(current))
1841 		flush_signals(current);
1842 	del_timer_sync(&session->tmf_timer);
1843 
1844 	mutex_lock(&session->eh_mutex);
1845 	spin_lock_bh(&session->frwd_lock);
1846 	/* if the session drops it will clean up the task */
1847 	if (age != session->age ||
1848 	    session->state != ISCSI_STATE_LOGGED_IN)
1849 		return -ENOTCONN;
1850 	return 0;
1851 }
1852 
1853 /*
1854  * Fail commands. session lock held and recv side suspended and xmit
1855  * thread flushed
1856  */
fail_scsi_tasks(struct iscsi_conn * conn,u64 lun,int error)1857 static void fail_scsi_tasks(struct iscsi_conn *conn, u64 lun, int error)
1858 {
1859 	struct iscsi_task *task;
1860 	int i;
1861 
1862 	for (i = 0; i < conn->session->cmds_max; i++) {
1863 		task = conn->session->cmds[i];
1864 		if (!task->sc || task->state == ISCSI_TASK_FREE)
1865 			continue;
1866 
1867 		if (lun != -1 && lun != task->sc->device->lun)
1868 			continue;
1869 
1870 		ISCSI_DBG_SESSION(conn->session,
1871 				  "failing sc %p itt 0x%x state %d\n",
1872 				  task->sc, task->itt, task->state);
1873 		fail_scsi_task(task, error);
1874 	}
1875 }
1876 
1877 /**
1878  * iscsi_suspend_queue - suspend iscsi_queuecommand
1879  * @conn: iscsi conn to stop queueing IO on
1880  *
1881  * This grabs the session frwd_lock to make sure no one is in
1882  * xmit_task/queuecommand, and then sets suspend to prevent
1883  * new commands from being queued. This only needs to be called
1884  * by offload drivers that need to sync a path like ep disconnect
1885  * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi
1886  * will call iscsi_start_tx and iscsi_unblock_session when in FFP.
1887  */
iscsi_suspend_queue(struct iscsi_conn * conn)1888 void iscsi_suspend_queue(struct iscsi_conn *conn)
1889 {
1890 	spin_lock_bh(&conn->session->frwd_lock);
1891 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1892 	spin_unlock_bh(&conn->session->frwd_lock);
1893 }
1894 EXPORT_SYMBOL_GPL(iscsi_suspend_queue);
1895 
1896 /**
1897  * iscsi_suspend_tx - suspend iscsi_data_xmit
1898  * @conn: iscsi conn tp stop processing IO on.
1899  *
1900  * This function sets the suspend bit to prevent iscsi_data_xmit
1901  * from sending new IO, and if work is queued on the xmit thread
1902  * it will wait for it to be completed.
1903  */
iscsi_suspend_tx(struct iscsi_conn * conn)1904 void iscsi_suspend_tx(struct iscsi_conn *conn)
1905 {
1906 	struct Scsi_Host *shost = conn->session->host;
1907 	struct iscsi_host *ihost = shost_priv(shost);
1908 
1909 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1910 	if (ihost->workq)
1911 		flush_workqueue(ihost->workq);
1912 }
1913 EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
1914 
iscsi_start_tx(struct iscsi_conn * conn)1915 static void iscsi_start_tx(struct iscsi_conn *conn)
1916 {
1917 	clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1918 	iscsi_conn_queue_work(conn);
1919 }
1920 
1921 /*
1922  * We want to make sure a ping is in flight. It has timed out.
1923  * And we are not busy processing a pdu that is making
1924  * progress but got started before the ping and is taking a while
1925  * to complete so the ping is just stuck behind it in a queue.
1926  */
iscsi_has_ping_timed_out(struct iscsi_conn * conn)1927 static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
1928 {
1929 	if (READ_ONCE(conn->ping_task) &&
1930 	    time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1931 			   (conn->ping_timeout * HZ), jiffies))
1932 		return 1;
1933 	else
1934 		return 0;
1935 }
1936 
iscsi_eh_cmd_timed_out(struct scsi_cmnd * sc)1937 enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
1938 {
1939 	enum blk_eh_timer_return rc = BLK_EH_DONE;
1940 	struct iscsi_task *task = NULL, *running_task;
1941 	struct iscsi_cls_session *cls_session;
1942 	struct iscsi_session *session;
1943 	struct iscsi_conn *conn;
1944 	int i;
1945 
1946 	cls_session = starget_to_session(scsi_target(sc->device));
1947 	session = cls_session->dd_data;
1948 
1949 	ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc);
1950 
1951 	spin_lock_bh(&session->frwd_lock);
1952 	task = (struct iscsi_task *)sc->SCp.ptr;
1953 	if (!task) {
1954 		/*
1955 		 * Raced with completion. Blk layer has taken ownership
1956 		 * so let timeout code complete it now.
1957 		 */
1958 		rc = BLK_EH_DONE;
1959 		goto done;
1960 	}
1961 
1962 	if (session->state != ISCSI_STATE_LOGGED_IN) {
1963 		/*
1964 		 * During shutdown, if session is prematurely disconnected,
1965 		 * recovery won't happen and there will be hung cmds. Not
1966 		 * handling cmds would trigger EH, also bad in this case.
1967 		 * Instead, handle cmd, allow completion to happen and let
1968 		 * upper layer to deal with the result.
1969 		 */
1970 		if (unlikely(system_state != SYSTEM_RUNNING)) {
1971 			sc->result = DID_NO_CONNECT << 16;
1972 			ISCSI_DBG_EH(session, "sc on shutdown, handled\n");
1973 			rc = BLK_EH_DONE;
1974 			goto done;
1975 		}
1976 		/*
1977 		 * We are probably in the middle of iscsi recovery so let
1978 		 * that complete and handle the error.
1979 		 */
1980 		rc = BLK_EH_RESET_TIMER;
1981 		goto done;
1982 	}
1983 
1984 	conn = session->leadconn;
1985 	if (!conn) {
1986 		/* In the middle of shuting down */
1987 		rc = BLK_EH_RESET_TIMER;
1988 		goto done;
1989 	}
1990 
1991 	/*
1992 	 * If we have sent (at least queued to the network layer) a pdu or
1993 	 * recvd one for the task since the last timeout ask for
1994 	 * more time. If on the next timeout we have not made progress
1995 	 * we can check if it is the task or connection when we send the
1996 	 * nop as a ping.
1997 	 */
1998 	if (time_after(task->last_xfer, task->last_timeout)) {
1999 		ISCSI_DBG_EH(session, "Command making progress. Asking "
2000 			     "scsi-ml for more time to complete. "
2001 			     "Last data xfer at %lu. Last timeout was at "
2002 			     "%lu\n.", task->last_xfer, task->last_timeout);
2003 		task->have_checked_conn = false;
2004 		rc = BLK_EH_RESET_TIMER;
2005 		goto done;
2006 	}
2007 
2008 	if (!conn->recv_timeout && !conn->ping_timeout)
2009 		goto done;
2010 	/*
2011 	 * if the ping timedout then we are in the middle of cleaning up
2012 	 * and can let the iscsi eh handle it
2013 	 */
2014 	if (iscsi_has_ping_timed_out(conn)) {
2015 		rc = BLK_EH_RESET_TIMER;
2016 		goto done;
2017 	}
2018 
2019 	for (i = 0; i < conn->session->cmds_max; i++) {
2020 		running_task = conn->session->cmds[i];
2021 		if (!running_task->sc || running_task == task ||
2022 		     running_task->state != ISCSI_TASK_RUNNING)
2023 			continue;
2024 
2025 		/*
2026 		 * Only check if cmds started before this one have made
2027 		 * progress, or this could never fail
2028 		 */
2029 		if (time_after(running_task->sc->jiffies_at_alloc,
2030 			       task->sc->jiffies_at_alloc))
2031 			continue;
2032 
2033 		if (time_after(running_task->last_xfer, task->last_timeout)) {
2034 			/*
2035 			 * This task has not made progress, but a task
2036 			 * started before us has transferred data since
2037 			 * we started/last-checked. We could be queueing
2038 			 * too many tasks or the LU is bad.
2039 			 *
2040 			 * If the device is bad the cmds ahead of us on
2041 			 * other devs will complete, and this loop will
2042 			 * eventually fail starting the scsi eh.
2043 			 */
2044 			ISCSI_DBG_EH(session, "Command has not made progress "
2045 				     "but commands ahead of it have. "
2046 				     "Asking scsi-ml for more time to "
2047 				     "complete. Our last xfer vs running task "
2048 				     "last xfer %lu/%lu. Last check %lu.\n",
2049 				     task->last_xfer, running_task->last_xfer,
2050 				     task->last_timeout);
2051 			rc = BLK_EH_RESET_TIMER;
2052 			goto done;
2053 		}
2054 	}
2055 
2056 	/* Assumes nop timeout is shorter than scsi cmd timeout */
2057 	if (task->have_checked_conn)
2058 		goto done;
2059 
2060 	/*
2061 	 * Checking the transport already or nop from a cmd timeout still
2062 	 * running
2063 	 */
2064 	if (READ_ONCE(conn->ping_task)) {
2065 		task->have_checked_conn = true;
2066 		rc = BLK_EH_RESET_TIMER;
2067 		goto done;
2068 	}
2069 
2070 	/* Make sure there is a transport check done */
2071 	iscsi_send_nopout(conn, NULL);
2072 	task->have_checked_conn = true;
2073 	rc = BLK_EH_RESET_TIMER;
2074 
2075 done:
2076 	if (task)
2077 		task->last_timeout = jiffies;
2078 	spin_unlock_bh(&session->frwd_lock);
2079 	ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
2080 		     "timer reset" : "shutdown or nh");
2081 	return rc;
2082 }
2083 EXPORT_SYMBOL_GPL(iscsi_eh_cmd_timed_out);
2084 
iscsi_check_transport_timeouts(struct timer_list * t)2085 static void iscsi_check_transport_timeouts(struct timer_list *t)
2086 {
2087 	struct iscsi_conn *conn = from_timer(conn, t, transport_timer);
2088 	struct iscsi_session *session = conn->session;
2089 	unsigned long recv_timeout, next_timeout = 0, last_recv;
2090 
2091 	spin_lock(&session->frwd_lock);
2092 	if (session->state != ISCSI_STATE_LOGGED_IN)
2093 		goto done;
2094 
2095 	recv_timeout = conn->recv_timeout;
2096 	if (!recv_timeout)
2097 		goto done;
2098 
2099 	recv_timeout *= HZ;
2100 	last_recv = conn->last_recv;
2101 
2102 	if (iscsi_has_ping_timed_out(conn)) {
2103 		iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
2104 				  "expired, recv timeout %d, last rx %lu, "
2105 				  "last ping %lu, now %lu\n",
2106 				  conn->ping_timeout, conn->recv_timeout,
2107 				  last_recv, conn->last_ping, jiffies);
2108 		spin_unlock(&session->frwd_lock);
2109 		iscsi_conn_failure(conn, ISCSI_ERR_NOP_TIMEDOUT);
2110 		return;
2111 	}
2112 
2113 	if (time_before_eq(last_recv + recv_timeout, jiffies)) {
2114 		/* send a ping to try to provoke some traffic */
2115 		ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
2116 		if (iscsi_send_nopout(conn, NULL))
2117 			next_timeout = jiffies + (1 * HZ);
2118 		else
2119 			next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
2120 	} else
2121 		next_timeout = last_recv + recv_timeout;
2122 
2123 	ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
2124 	mod_timer(&conn->transport_timer, next_timeout);
2125 done:
2126 	spin_unlock(&session->frwd_lock);
2127 }
2128 
2129 /**
2130  * iscsi_conn_unbind - prevent queueing to conn.
2131  * @cls_conn: iscsi conn ep is bound to.
2132  * @is_active: is the conn in use for boot or is this for EH/termination
2133  *
2134  * This must be called by drivers implementing the ep_disconnect callout.
2135  * It disables queueing to the connection from libiscsi in preparation for
2136  * an ep_disconnect call.
2137  */
iscsi_conn_unbind(struct iscsi_cls_conn * cls_conn,bool is_active)2138 void iscsi_conn_unbind(struct iscsi_cls_conn *cls_conn, bool is_active)
2139 {
2140 	struct iscsi_session *session;
2141 	struct iscsi_conn *conn;
2142 
2143 	if (!cls_conn)
2144 		return;
2145 
2146 	conn = cls_conn->dd_data;
2147 	session = conn->session;
2148 	/*
2149 	 * Wait for iscsi_eh calls to exit. We don't wait for the tmf to
2150 	 * complete or timeout. The caller just wants to know what's running
2151 	 * is everything that needs to be cleaned up, and no cmds will be
2152 	 * queued.
2153 	 */
2154 	mutex_lock(&session->eh_mutex);
2155 
2156 	iscsi_suspend_queue(conn);
2157 	iscsi_suspend_tx(conn);
2158 
2159 	spin_lock_bh(&session->frwd_lock);
2160 	if (!is_active) {
2161 		/*
2162 		 * if logout timed out before userspace could even send a PDU
2163 		 * the state might still be in ISCSI_STATE_LOGGED_IN and
2164 		 * allowing new cmds and TMFs.
2165 		 */
2166 		if (session->state == ISCSI_STATE_LOGGED_IN)
2167 			iscsi_set_conn_failed(conn);
2168 	}
2169 	spin_unlock_bh(&session->frwd_lock);
2170 	mutex_unlock(&session->eh_mutex);
2171 }
2172 EXPORT_SYMBOL_GPL(iscsi_conn_unbind);
2173 
iscsi_prep_abort_task_pdu(struct iscsi_task * task,struct iscsi_tm * hdr)2174 static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
2175 				      struct iscsi_tm *hdr)
2176 {
2177 	memset(hdr, 0, sizeof(*hdr));
2178 	hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2179 	hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
2180 	hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2181 	hdr->lun = task->lun;
2182 	hdr->rtt = task->hdr_itt;
2183 	hdr->refcmdsn = task->cmdsn;
2184 }
2185 
iscsi_eh_abort(struct scsi_cmnd * sc)2186 int iscsi_eh_abort(struct scsi_cmnd *sc)
2187 {
2188 	struct iscsi_cls_session *cls_session;
2189 	struct iscsi_session *session;
2190 	struct iscsi_conn *conn;
2191 	struct iscsi_task *task;
2192 	struct iscsi_tm *hdr;
2193 	int age;
2194 
2195 	cls_session = starget_to_session(scsi_target(sc->device));
2196 	session = cls_session->dd_data;
2197 
2198 	ISCSI_DBG_EH(session, "aborting sc %p\n", sc);
2199 
2200 	mutex_lock(&session->eh_mutex);
2201 	spin_lock_bh(&session->frwd_lock);
2202 	/*
2203 	 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
2204 	 * got the command.
2205 	 */
2206 	if (!sc->SCp.ptr) {
2207 		ISCSI_DBG_EH(session, "sc never reached iscsi layer or "
2208 				      "it completed.\n");
2209 		spin_unlock_bh(&session->frwd_lock);
2210 		mutex_unlock(&session->eh_mutex);
2211 		return SUCCESS;
2212 	}
2213 
2214 	/*
2215 	 * If we are not logged in or we have started a new session
2216 	 * then let the host reset code handle this
2217 	 */
2218 	if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
2219 	    sc->SCp.phase != session->age) {
2220 		spin_unlock_bh(&session->frwd_lock);
2221 		mutex_unlock(&session->eh_mutex);
2222 		ISCSI_DBG_EH(session, "failing abort due to dropped "
2223 				  "session.\n");
2224 		return FAILED;
2225 	}
2226 
2227 	conn = session->leadconn;
2228 	conn->eh_abort_cnt++;
2229 	age = session->age;
2230 
2231 	task = (struct iscsi_task *)sc->SCp.ptr;
2232 	ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n",
2233 		     sc, task->itt);
2234 
2235 	/* task completed before time out */
2236 	if (!task->sc) {
2237 		ISCSI_DBG_EH(session, "sc completed while abort in progress\n");
2238 		goto success;
2239 	}
2240 
2241 	if (task->state == ISCSI_TASK_PENDING) {
2242 		fail_scsi_task(task, DID_ABORT);
2243 		goto success;
2244 	}
2245 
2246 	/* only have one tmf outstanding at a time */
2247 	if (session->tmf_state != TMF_INITIAL)
2248 		goto failed;
2249 	session->tmf_state = TMF_QUEUED;
2250 
2251 	hdr = &session->tmhdr;
2252 	iscsi_prep_abort_task_pdu(task, hdr);
2253 
2254 	if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout))
2255 		goto failed;
2256 
2257 	switch (session->tmf_state) {
2258 	case TMF_SUCCESS:
2259 		spin_unlock_bh(&session->frwd_lock);
2260 		/*
2261 		 * stop tx side incase the target had sent a abort rsp but
2262 		 * the initiator was still writing out data.
2263 		 */
2264 		iscsi_suspend_tx(conn);
2265 		/*
2266 		 * we do not stop the recv side because targets have been
2267 		 * good and have never sent us a successful tmf response
2268 		 * then sent more data for the cmd.
2269 		 */
2270 		spin_lock_bh(&session->frwd_lock);
2271 		fail_scsi_task(task, DID_ABORT);
2272 		session->tmf_state = TMF_INITIAL;
2273 		memset(hdr, 0, sizeof(*hdr));
2274 		spin_unlock_bh(&session->frwd_lock);
2275 		iscsi_start_tx(conn);
2276 		goto success_unlocked;
2277 	case TMF_TIMEDOUT:
2278 		spin_unlock_bh(&session->frwd_lock);
2279 		iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2280 		goto failed_unlocked;
2281 	case TMF_NOT_FOUND:
2282 		if (!sc->SCp.ptr) {
2283 			session->tmf_state = TMF_INITIAL;
2284 			memset(hdr, 0, sizeof(*hdr));
2285 			/* task completed before tmf abort response */
2286 			ISCSI_DBG_EH(session, "sc completed while abort	in "
2287 					      "progress\n");
2288 			goto success;
2289 		}
2290 		fallthrough;
2291 	default:
2292 		session->tmf_state = TMF_INITIAL;
2293 		goto failed;
2294 	}
2295 
2296 success:
2297 	spin_unlock_bh(&session->frwd_lock);
2298 success_unlocked:
2299 	ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n",
2300 		     sc, task->itt);
2301 	mutex_unlock(&session->eh_mutex);
2302 	return SUCCESS;
2303 
2304 failed:
2305 	spin_unlock_bh(&session->frwd_lock);
2306 failed_unlocked:
2307 	ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc,
2308 		     task ? task->itt : 0);
2309 	mutex_unlock(&session->eh_mutex);
2310 	return FAILED;
2311 }
2312 EXPORT_SYMBOL_GPL(iscsi_eh_abort);
2313 
iscsi_prep_lun_reset_pdu(struct scsi_cmnd * sc,struct iscsi_tm * hdr)2314 static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2315 {
2316 	memset(hdr, 0, sizeof(*hdr));
2317 	hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2318 	hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2319 	hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2320 	int_to_scsilun(sc->device->lun, &hdr->lun);
2321 	hdr->rtt = RESERVED_ITT;
2322 }
2323 
iscsi_eh_device_reset(struct scsi_cmnd * sc)2324 int iscsi_eh_device_reset(struct scsi_cmnd *sc)
2325 {
2326 	struct iscsi_cls_session *cls_session;
2327 	struct iscsi_session *session;
2328 	struct iscsi_conn *conn;
2329 	struct iscsi_tm *hdr;
2330 	int rc = FAILED;
2331 
2332 	cls_session = starget_to_session(scsi_target(sc->device));
2333 	session = cls_session->dd_data;
2334 
2335 	ISCSI_DBG_EH(session, "LU Reset [sc %p lun %llu]\n", sc,
2336 		     sc->device->lun);
2337 
2338 	mutex_lock(&session->eh_mutex);
2339 	spin_lock_bh(&session->frwd_lock);
2340 	/*
2341 	 * Just check if we are not logged in. We cannot check for
2342 	 * the phase because the reset could come from a ioctl.
2343 	 */
2344 	if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2345 		goto unlock;
2346 	conn = session->leadconn;
2347 
2348 	/* only have one tmf outstanding at a time */
2349 	if (session->tmf_state != TMF_INITIAL)
2350 		goto unlock;
2351 	session->tmf_state = TMF_QUEUED;
2352 
2353 	hdr = &session->tmhdr;
2354 	iscsi_prep_lun_reset_pdu(sc, hdr);
2355 
2356 	if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
2357 				    session->lu_reset_timeout)) {
2358 		rc = FAILED;
2359 		goto unlock;
2360 	}
2361 
2362 	switch (session->tmf_state) {
2363 	case TMF_SUCCESS:
2364 		break;
2365 	case TMF_TIMEDOUT:
2366 		spin_unlock_bh(&session->frwd_lock);
2367 		iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2368 		goto done;
2369 	default:
2370 		session->tmf_state = TMF_INITIAL;
2371 		goto unlock;
2372 	}
2373 
2374 	rc = SUCCESS;
2375 	spin_unlock_bh(&session->frwd_lock);
2376 
2377 	iscsi_suspend_tx(conn);
2378 
2379 	spin_lock_bh(&session->frwd_lock);
2380 	memset(hdr, 0, sizeof(*hdr));
2381 	fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
2382 	session->tmf_state = TMF_INITIAL;
2383 	spin_unlock_bh(&session->frwd_lock);
2384 
2385 	iscsi_start_tx(conn);
2386 	goto done;
2387 
2388 unlock:
2389 	spin_unlock_bh(&session->frwd_lock);
2390 done:
2391 	ISCSI_DBG_EH(session, "dev reset result = %s\n",
2392 		     rc == SUCCESS ? "SUCCESS" : "FAILED");
2393 	mutex_unlock(&session->eh_mutex);
2394 	return rc;
2395 }
2396 EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
2397 
iscsi_session_recovery_timedout(struct iscsi_cls_session * cls_session)2398 void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
2399 {
2400 	struct iscsi_session *session = cls_session->dd_data;
2401 
2402 	spin_lock_bh(&session->frwd_lock);
2403 	if (session->state != ISCSI_STATE_LOGGED_IN) {
2404 		session->state = ISCSI_STATE_RECOVERY_FAILED;
2405 		wake_up(&session->ehwait);
2406 	}
2407 	spin_unlock_bh(&session->frwd_lock);
2408 }
2409 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
2410 
2411 /**
2412  * iscsi_eh_session_reset - drop session and attempt relogin
2413  * @sc: scsi command
2414  *
2415  * This function will wait for a relogin, session termination from
2416  * userspace, or a recovery/replacement timeout.
2417  */
iscsi_eh_session_reset(struct scsi_cmnd * sc)2418 int iscsi_eh_session_reset(struct scsi_cmnd *sc)
2419 {
2420 	struct iscsi_cls_session *cls_session;
2421 	struct iscsi_session *session;
2422 	struct iscsi_conn *conn;
2423 
2424 	cls_session = starget_to_session(scsi_target(sc->device));
2425 	session = cls_session->dd_data;
2426 	conn = session->leadconn;
2427 
2428 	mutex_lock(&session->eh_mutex);
2429 	spin_lock_bh(&session->frwd_lock);
2430 	if (session->state == ISCSI_STATE_TERMINATE) {
2431 failed:
2432 		ISCSI_DBG_EH(session,
2433 			     "failing session reset: Could not log back into "
2434 			     "%s [age %d]\n", session->targetname,
2435 			     session->age);
2436 		spin_unlock_bh(&session->frwd_lock);
2437 		mutex_unlock(&session->eh_mutex);
2438 		return FAILED;
2439 	}
2440 
2441 	spin_unlock_bh(&session->frwd_lock);
2442 	mutex_unlock(&session->eh_mutex);
2443 	/*
2444 	 * we drop the lock here but the leadconn cannot be destoyed while
2445 	 * we are in the scsi eh
2446 	 */
2447 	iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2448 
2449 	ISCSI_DBG_EH(session, "wait for relogin\n");
2450 	wait_event_interruptible(session->ehwait,
2451 				 session->state == ISCSI_STATE_TERMINATE ||
2452 				 session->state == ISCSI_STATE_LOGGED_IN ||
2453 				 session->state == ISCSI_STATE_RECOVERY_FAILED);
2454 	if (signal_pending(current))
2455 		flush_signals(current);
2456 
2457 	mutex_lock(&session->eh_mutex);
2458 	spin_lock_bh(&session->frwd_lock);
2459 	if (session->state == ISCSI_STATE_LOGGED_IN) {
2460 		ISCSI_DBG_EH(session,
2461 			     "session reset succeeded for %s,%s\n",
2462 			     session->targetname, conn->persistent_address);
2463 	} else
2464 		goto failed;
2465 	spin_unlock_bh(&session->frwd_lock);
2466 	mutex_unlock(&session->eh_mutex);
2467 	return SUCCESS;
2468 }
2469 EXPORT_SYMBOL_GPL(iscsi_eh_session_reset);
2470 
iscsi_prep_tgt_reset_pdu(struct scsi_cmnd * sc,struct iscsi_tm * hdr)2471 static void iscsi_prep_tgt_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2472 {
2473 	memset(hdr, 0, sizeof(*hdr));
2474 	hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2475 	hdr->flags = ISCSI_TM_FUNC_TARGET_WARM_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2476 	hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2477 	hdr->rtt = RESERVED_ITT;
2478 }
2479 
2480 /**
2481  * iscsi_eh_target_reset - reset target
2482  * @sc: scsi command
2483  *
2484  * This will attempt to send a warm target reset.
2485  */
iscsi_eh_target_reset(struct scsi_cmnd * sc)2486 static int iscsi_eh_target_reset(struct scsi_cmnd *sc)
2487 {
2488 	struct iscsi_cls_session *cls_session;
2489 	struct iscsi_session *session;
2490 	struct iscsi_conn *conn;
2491 	struct iscsi_tm *hdr;
2492 	int rc = FAILED;
2493 
2494 	cls_session = starget_to_session(scsi_target(sc->device));
2495 	session = cls_session->dd_data;
2496 
2497 	ISCSI_DBG_EH(session, "tgt Reset [sc %p tgt %s]\n", sc,
2498 		     session->targetname);
2499 
2500 	mutex_lock(&session->eh_mutex);
2501 	spin_lock_bh(&session->frwd_lock);
2502 	/*
2503 	 * Just check if we are not logged in. We cannot check for
2504 	 * the phase because the reset could come from a ioctl.
2505 	 */
2506 	if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2507 		goto unlock;
2508 	conn = session->leadconn;
2509 
2510 	/* only have one tmf outstanding at a time */
2511 	if (session->tmf_state != TMF_INITIAL)
2512 		goto unlock;
2513 	session->tmf_state = TMF_QUEUED;
2514 
2515 	hdr = &session->tmhdr;
2516 	iscsi_prep_tgt_reset_pdu(sc, hdr);
2517 
2518 	if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
2519 				    session->tgt_reset_timeout)) {
2520 		rc = FAILED;
2521 		goto unlock;
2522 	}
2523 
2524 	switch (session->tmf_state) {
2525 	case TMF_SUCCESS:
2526 		break;
2527 	case TMF_TIMEDOUT:
2528 		spin_unlock_bh(&session->frwd_lock);
2529 		iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2530 		goto done;
2531 	default:
2532 		session->tmf_state = TMF_INITIAL;
2533 		goto unlock;
2534 	}
2535 
2536 	rc = SUCCESS;
2537 	spin_unlock_bh(&session->frwd_lock);
2538 
2539 	iscsi_suspend_tx(conn);
2540 
2541 	spin_lock_bh(&session->frwd_lock);
2542 	memset(hdr, 0, sizeof(*hdr));
2543 	fail_scsi_tasks(conn, -1, DID_ERROR);
2544 	session->tmf_state = TMF_INITIAL;
2545 	spin_unlock_bh(&session->frwd_lock);
2546 
2547 	iscsi_start_tx(conn);
2548 	goto done;
2549 
2550 unlock:
2551 	spin_unlock_bh(&session->frwd_lock);
2552 done:
2553 	ISCSI_DBG_EH(session, "tgt %s reset result = %s\n", session->targetname,
2554 		     rc == SUCCESS ? "SUCCESS" : "FAILED");
2555 	mutex_unlock(&session->eh_mutex);
2556 	return rc;
2557 }
2558 
2559 /**
2560  * iscsi_eh_recover_target - reset target and possibly the session
2561  * @sc: scsi command
2562  *
2563  * This will attempt to send a warm target reset. If that fails,
2564  * we will escalate to ERL0 session recovery.
2565  */
iscsi_eh_recover_target(struct scsi_cmnd * sc)2566 int iscsi_eh_recover_target(struct scsi_cmnd *sc)
2567 {
2568 	int rc;
2569 
2570 	rc = iscsi_eh_target_reset(sc);
2571 	if (rc == FAILED)
2572 		rc = iscsi_eh_session_reset(sc);
2573 	return rc;
2574 }
2575 EXPORT_SYMBOL_GPL(iscsi_eh_recover_target);
2576 
2577 /*
2578  * Pre-allocate a pool of @max items of @item_size. By default, the pool
2579  * should be accessed via kfifo_{get,put} on q->queue.
2580  * Optionally, the caller can obtain the array of object pointers
2581  * by passing in a non-NULL @items pointer
2582  */
2583 int
iscsi_pool_init(struct iscsi_pool * q,int max,void *** items,int item_size)2584 iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
2585 {
2586 	int i, num_arrays = 1;
2587 
2588 	memset(q, 0, sizeof(*q));
2589 
2590 	q->max = max;
2591 
2592 	/* If the user passed an items pointer, he wants a copy of
2593 	 * the array. */
2594 	if (items)
2595 		num_arrays++;
2596 	q->pool = kvcalloc(num_arrays * max, sizeof(void *), GFP_KERNEL);
2597 	if (q->pool == NULL)
2598 		return -ENOMEM;
2599 
2600 	kfifo_init(&q->queue, (void*)q->pool, max * sizeof(void*));
2601 
2602 	for (i = 0; i < max; i++) {
2603 		q->pool[i] = kzalloc(item_size, GFP_KERNEL);
2604 		if (q->pool[i] == NULL) {
2605 			q->max = i;
2606 			goto enomem;
2607 		}
2608 		kfifo_in(&q->queue, (void*)&q->pool[i], sizeof(void*));
2609 	}
2610 
2611 	if (items) {
2612 		*items = q->pool + max;
2613 		memcpy(*items, q->pool, max * sizeof(void *));
2614 	}
2615 
2616 	return 0;
2617 
2618 enomem:
2619 	iscsi_pool_free(q);
2620 	return -ENOMEM;
2621 }
2622 EXPORT_SYMBOL_GPL(iscsi_pool_init);
2623 
iscsi_pool_free(struct iscsi_pool * q)2624 void iscsi_pool_free(struct iscsi_pool *q)
2625 {
2626 	int i;
2627 
2628 	for (i = 0; i < q->max; i++)
2629 		kfree(q->pool[i]);
2630 	kvfree(q->pool);
2631 }
2632 EXPORT_SYMBOL_GPL(iscsi_pool_free);
2633 
2634 /**
2635  * iscsi_host_add - add host to system
2636  * @shost: scsi host
2637  * @pdev: parent device
2638  *
2639  * This should be called by partial offload and software iscsi drivers
2640  * to add a host to the system.
2641  */
iscsi_host_add(struct Scsi_Host * shost,struct device * pdev)2642 int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
2643 {
2644 	if (!shost->can_queue)
2645 		shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
2646 
2647 	if (!shost->cmd_per_lun)
2648 		shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
2649 
2650 	return scsi_add_host(shost, pdev);
2651 }
2652 EXPORT_SYMBOL_GPL(iscsi_host_add);
2653 
2654 /**
2655  * iscsi_host_alloc - allocate a host and driver data
2656  * @sht: scsi host template
2657  * @dd_data_size: driver host data size
2658  * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
2659  *
2660  * This should be called by partial offload and software iscsi drivers.
2661  * To access the driver specific memory use the iscsi_host_priv() macro.
2662  */
iscsi_host_alloc(struct scsi_host_template * sht,int dd_data_size,bool xmit_can_sleep)2663 struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
2664 				   int dd_data_size, bool xmit_can_sleep)
2665 {
2666 	struct Scsi_Host *shost;
2667 	struct iscsi_host *ihost;
2668 
2669 	shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2670 	if (!shost)
2671 		return NULL;
2672 	ihost = shost_priv(shost);
2673 
2674 	if (xmit_can_sleep) {
2675 		snprintf(ihost->workq_name, sizeof(ihost->workq_name),
2676 			"iscsi_q_%d", shost->host_no);
2677 		ihost->workq = alloc_workqueue("%s",
2678 			WQ_SYSFS | __WQ_LEGACY | WQ_MEM_RECLAIM | WQ_UNBOUND,
2679 			1, ihost->workq_name);
2680 		if (!ihost->workq)
2681 			goto free_host;
2682 	}
2683 
2684 	spin_lock_init(&ihost->lock);
2685 	ihost->state = ISCSI_HOST_SETUP;
2686 	ihost->num_sessions = 0;
2687 	init_waitqueue_head(&ihost->session_removal_wq);
2688 	return shost;
2689 
2690 free_host:
2691 	scsi_host_put(shost);
2692 	return NULL;
2693 }
2694 EXPORT_SYMBOL_GPL(iscsi_host_alloc);
2695 
iscsi_notify_host_removed(struct iscsi_cls_session * cls_session)2696 static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2697 {
2698 	iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
2699 }
2700 
2701 /**
2702  * iscsi_host_remove - remove host and sessions
2703  * @shost: scsi host
2704  *
2705  * If there are any sessions left, this will initiate the removal and wait
2706  * for the completion.
2707  */
iscsi_host_remove(struct Scsi_Host * shost)2708 void iscsi_host_remove(struct Scsi_Host *shost)
2709 {
2710 	struct iscsi_host *ihost = shost_priv(shost);
2711 	unsigned long flags;
2712 
2713 	spin_lock_irqsave(&ihost->lock, flags);
2714 	ihost->state = ISCSI_HOST_REMOVED;
2715 	spin_unlock_irqrestore(&ihost->lock, flags);
2716 
2717 	iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2718 	wait_event_interruptible(ihost->session_removal_wq,
2719 				 ihost->num_sessions == 0);
2720 	if (signal_pending(current))
2721 		flush_signals(current);
2722 
2723 	scsi_remove_host(shost);
2724 	if (ihost->workq)
2725 		destroy_workqueue(ihost->workq);
2726 }
2727 EXPORT_SYMBOL_GPL(iscsi_host_remove);
2728 
iscsi_host_free(struct Scsi_Host * shost)2729 void iscsi_host_free(struct Scsi_Host *shost)
2730 {
2731 	struct iscsi_host *ihost = shost_priv(shost);
2732 
2733 	kfree(ihost->netdev);
2734 	kfree(ihost->hwaddress);
2735 	kfree(ihost->initiatorname);
2736 	scsi_host_put(shost);
2737 }
2738 EXPORT_SYMBOL_GPL(iscsi_host_free);
2739 
iscsi_host_dec_session_cnt(struct Scsi_Host * shost)2740 static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2741 {
2742 	struct iscsi_host *ihost = shost_priv(shost);
2743 	unsigned long flags;
2744 
2745 	shost = scsi_host_get(shost);
2746 	if (!shost) {
2747 		printk(KERN_ERR "Invalid state. Cannot notify host removal "
2748 		      "of session teardown event because host already "
2749 		      "removed.\n");
2750 		return;
2751 	}
2752 
2753 	spin_lock_irqsave(&ihost->lock, flags);
2754 	ihost->num_sessions--;
2755 	if (ihost->num_sessions == 0)
2756 		wake_up(&ihost->session_removal_wq);
2757 	spin_unlock_irqrestore(&ihost->lock, flags);
2758 	scsi_host_put(shost);
2759 }
2760 
2761 /**
2762  * iscsi_session_setup - create iscsi cls session and host and session
2763  * @iscsit: iscsi transport template
2764  * @shost: scsi host
2765  * @cmds_max: session can queue
2766  * @dd_size: private driver data size, added to session allocation size
2767  * @cmd_task_size: LLD task private data size
2768  * @initial_cmdsn: initial CmdSN
2769  * @id: target ID to add to this session
2770  *
2771  * This can be used by software iscsi_transports that allocate
2772  * a session per scsi host.
2773  *
2774  * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2775  * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2776  * for nop handling and login/logout requests.
2777  */
2778 struct iscsi_cls_session *
iscsi_session_setup(struct iscsi_transport * iscsit,struct Scsi_Host * shost,uint16_t cmds_max,int dd_size,int cmd_task_size,uint32_t initial_cmdsn,unsigned int id)2779 iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
2780 		    uint16_t cmds_max, int dd_size, int cmd_task_size,
2781 		    uint32_t initial_cmdsn, unsigned int id)
2782 {
2783 	struct iscsi_host *ihost = shost_priv(shost);
2784 	struct iscsi_session *session;
2785 	struct iscsi_cls_session *cls_session;
2786 	int cmd_i, scsi_cmds, total_cmds = cmds_max;
2787 	unsigned long flags;
2788 
2789 	spin_lock_irqsave(&ihost->lock, flags);
2790 	if (ihost->state == ISCSI_HOST_REMOVED) {
2791 		spin_unlock_irqrestore(&ihost->lock, flags);
2792 		return NULL;
2793 	}
2794 	ihost->num_sessions++;
2795 	spin_unlock_irqrestore(&ihost->lock, flags);
2796 
2797 	if (!total_cmds)
2798 		total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
2799 	/*
2800 	 * The iscsi layer needs some tasks for nop handling and tmfs,
2801 	 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2802 	 * + 1 command for scsi IO.
2803 	 */
2804 	if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2805 		printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2806 		       "must be a power of two that is at least %d.\n",
2807 		       total_cmds, ISCSI_TOTAL_CMDS_MIN);
2808 		goto dec_session_count;
2809 	}
2810 
2811 	if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2812 		printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2813 		       "must be a power of 2 less than or equal to %d.\n",
2814 		       cmds_max, ISCSI_TOTAL_CMDS_MAX);
2815 		total_cmds = ISCSI_TOTAL_CMDS_MAX;
2816 	}
2817 
2818 	if (!is_power_of_2(total_cmds)) {
2819 		printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2820 		       "must be a power of 2.\n", total_cmds);
2821 		total_cmds = rounddown_pow_of_two(total_cmds);
2822 		if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2823 			goto dec_session_count;
2824 		printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2825 		       total_cmds);
2826 	}
2827 	scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
2828 
2829 	cls_session = iscsi_alloc_session(shost, iscsit,
2830 					  sizeof(struct iscsi_session) +
2831 					  dd_size);
2832 	if (!cls_session)
2833 		goto dec_session_count;
2834 	session = cls_session->dd_data;
2835 	session->cls_session = cls_session;
2836 	session->host = shost;
2837 	session->state = ISCSI_STATE_FREE;
2838 	session->fast_abort = 1;
2839 	session->tgt_reset_timeout = 30;
2840 	session->lu_reset_timeout = 15;
2841 	session->abort_timeout = 10;
2842 	session->scsi_cmds_max = scsi_cmds;
2843 	session->cmds_max = total_cmds;
2844 	session->queued_cmdsn = session->cmdsn = initial_cmdsn;
2845 	session->exp_cmdsn = initial_cmdsn + 1;
2846 	session->max_cmdsn = initial_cmdsn + 1;
2847 	session->max_r2t = 1;
2848 	session->tt = iscsit;
2849 	session->dd_data = cls_session->dd_data + sizeof(*session);
2850 
2851 	session->tmf_state = TMF_INITIAL;
2852 	timer_setup(&session->tmf_timer, iscsi_tmf_timedout, 0);
2853 	mutex_init(&session->eh_mutex);
2854 
2855 	spin_lock_init(&session->frwd_lock);
2856 	spin_lock_init(&session->back_lock);
2857 
2858 	/* initialize SCSI PDU commands pool */
2859 	if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2860 			    (void***)&session->cmds,
2861 			    cmd_task_size + sizeof(struct iscsi_task)))
2862 		goto cmdpool_alloc_fail;
2863 
2864 	/* pre-format cmds pool with ITT */
2865 	for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2866 		struct iscsi_task *task = session->cmds[cmd_i];
2867 
2868 		if (cmd_task_size)
2869 			task->dd_data = &task[1];
2870 		task->itt = cmd_i;
2871 		task->state = ISCSI_TASK_FREE;
2872 		INIT_LIST_HEAD(&task->running);
2873 	}
2874 
2875 	if (!try_module_get(iscsit->owner))
2876 		goto module_get_fail;
2877 
2878 	if (iscsi_add_session(cls_session, id))
2879 		goto cls_session_fail;
2880 
2881 	return cls_session;
2882 
2883 cls_session_fail:
2884 	module_put(iscsit->owner);
2885 module_get_fail:
2886 	iscsi_pool_free(&session->cmdpool);
2887 cmdpool_alloc_fail:
2888 	iscsi_free_session(cls_session);
2889 dec_session_count:
2890 	iscsi_host_dec_session_cnt(shost);
2891 	return NULL;
2892 }
2893 EXPORT_SYMBOL_GPL(iscsi_session_setup);
2894 
2895 /**
2896  * iscsi_session_teardown - destroy session, host, and cls_session
2897  * @cls_session: iscsi session
2898  */
iscsi_session_teardown(struct iscsi_cls_session * cls_session)2899 void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2900 {
2901 	struct iscsi_session *session = cls_session->dd_data;
2902 	struct module *owner = cls_session->transport->owner;
2903 	struct Scsi_Host *shost = session->host;
2904 
2905 	iscsi_pool_free(&session->cmdpool);
2906 
2907 	iscsi_remove_session(cls_session);
2908 
2909 	kfree(session->password);
2910 	kfree(session->password_in);
2911 	kfree(session->username);
2912 	kfree(session->username_in);
2913 	kfree(session->targetname);
2914 	kfree(session->targetalias);
2915 	kfree(session->initiatorname);
2916 	kfree(session->boot_root);
2917 	kfree(session->boot_nic);
2918 	kfree(session->boot_target);
2919 	kfree(session->ifacename);
2920 	kfree(session->portal_type);
2921 	kfree(session->discovery_parent_type);
2922 
2923 	iscsi_free_session(cls_session);
2924 
2925 	iscsi_host_dec_session_cnt(shost);
2926 	module_put(owner);
2927 }
2928 EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2929 
2930 /**
2931  * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2932  * @cls_session: iscsi_cls_session
2933  * @dd_size: private driver data size
2934  * @conn_idx: cid
2935  */
2936 struct iscsi_cls_conn *
iscsi_conn_setup(struct iscsi_cls_session * cls_session,int dd_size,uint32_t conn_idx)2937 iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2938 		 uint32_t conn_idx)
2939 {
2940 	struct iscsi_session *session = cls_session->dd_data;
2941 	struct iscsi_conn *conn;
2942 	struct iscsi_cls_conn *cls_conn;
2943 	char *data;
2944 
2945 	cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2946 				     conn_idx);
2947 	if (!cls_conn)
2948 		return NULL;
2949 	conn = cls_conn->dd_data;
2950 	memset(conn, 0, sizeof(*conn) + dd_size);
2951 
2952 	conn->dd_data = cls_conn->dd_data + sizeof(*conn);
2953 	conn->session = session;
2954 	conn->cls_conn = cls_conn;
2955 	conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2956 	conn->id = conn_idx;
2957 	conn->exp_statsn = 0;
2958 
2959 	timer_setup(&conn->transport_timer, iscsi_check_transport_timeouts, 0);
2960 
2961 	INIT_LIST_HEAD(&conn->mgmtqueue);
2962 	INIT_LIST_HEAD(&conn->cmdqueue);
2963 	INIT_LIST_HEAD(&conn->requeue);
2964 	spin_lock_init(&conn->taskqueuelock);
2965 	INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
2966 
2967 	/* allocate login_task used for the login/text sequences */
2968 	spin_lock_bh(&session->frwd_lock);
2969 	if (!kfifo_out(&session->cmdpool.queue,
2970                          (void*)&conn->login_task,
2971 			 sizeof(void*))) {
2972 		spin_unlock_bh(&session->frwd_lock);
2973 		goto login_task_alloc_fail;
2974 	}
2975 	spin_unlock_bh(&session->frwd_lock);
2976 
2977 	data = (char *) __get_free_pages(GFP_KERNEL,
2978 					 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
2979 	if (!data)
2980 		goto login_task_data_alloc_fail;
2981 	conn->login_task->data = conn->data = data;
2982 
2983 	init_waitqueue_head(&session->ehwait);
2984 
2985 	return cls_conn;
2986 
2987 login_task_data_alloc_fail:
2988 	kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
2989 		    sizeof(void*));
2990 login_task_alloc_fail:
2991 	iscsi_destroy_conn(cls_conn);
2992 	return NULL;
2993 }
2994 EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2995 
2996 /**
2997  * iscsi_conn_teardown - teardown iscsi connection
2998  * @cls_conn: iscsi class connection
2999  *
3000  * TODO: we may need to make this into a two step process
3001  * like scsi-mls remove + put host
3002  */
iscsi_conn_teardown(struct iscsi_cls_conn * cls_conn)3003 void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
3004 {
3005 	struct iscsi_conn *conn = cls_conn->dd_data;
3006 	struct iscsi_session *session = conn->session;
3007 	char *tmp_persistent_address = conn->persistent_address;
3008 	char *tmp_local_ipaddr = conn->local_ipaddr;
3009 
3010 	del_timer_sync(&conn->transport_timer);
3011 
3012 	mutex_lock(&session->eh_mutex);
3013 	spin_lock_bh(&session->frwd_lock);
3014 	conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
3015 	if (session->leadconn == conn) {
3016 		/*
3017 		 * leading connection? then give up on recovery.
3018 		 */
3019 		session->state = ISCSI_STATE_TERMINATE;
3020 		wake_up(&session->ehwait);
3021 	}
3022 	spin_unlock_bh(&session->frwd_lock);
3023 
3024 	/* flush queued up work because we free the connection below */
3025 	iscsi_suspend_tx(conn);
3026 
3027 	spin_lock_bh(&session->frwd_lock);
3028 	free_pages((unsigned long) conn->data,
3029 		   get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
3030 	/* regular RX path uses back_lock */
3031 	spin_lock_bh(&session->back_lock);
3032 	kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
3033 		    sizeof(void*));
3034 	spin_unlock_bh(&session->back_lock);
3035 	if (session->leadconn == conn)
3036 		session->leadconn = NULL;
3037 	spin_unlock_bh(&session->frwd_lock);
3038 	mutex_unlock(&session->eh_mutex);
3039 
3040 	iscsi_destroy_conn(cls_conn);
3041 	kfree(tmp_persistent_address);
3042 	kfree(tmp_local_ipaddr);
3043 }
3044 EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
3045 
iscsi_conn_start(struct iscsi_cls_conn * cls_conn)3046 int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
3047 {
3048 	struct iscsi_conn *conn = cls_conn->dd_data;
3049 	struct iscsi_session *session = conn->session;
3050 
3051 	if (!session) {
3052 		iscsi_conn_printk(KERN_ERR, conn,
3053 				  "can't start unbound connection\n");
3054 		return -EPERM;
3055 	}
3056 
3057 	if ((session->imm_data_en || !session->initial_r2t_en) &&
3058 	     session->first_burst > session->max_burst) {
3059 		iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
3060 				  "first_burst %d max_burst %d\n",
3061 				  session->first_burst, session->max_burst);
3062 		return -EINVAL;
3063 	}
3064 
3065 	if (conn->ping_timeout && !conn->recv_timeout) {
3066 		iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
3067 				  "zero. Using 5 seconds\n.");
3068 		conn->recv_timeout = 5;
3069 	}
3070 
3071 	if (conn->recv_timeout && !conn->ping_timeout) {
3072 		iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
3073 				  "zero. Using 5 seconds.\n");
3074 		conn->ping_timeout = 5;
3075 	}
3076 
3077 	spin_lock_bh(&session->frwd_lock);
3078 	conn->c_stage = ISCSI_CONN_STARTED;
3079 	session->state = ISCSI_STATE_LOGGED_IN;
3080 	session->queued_cmdsn = session->cmdsn;
3081 
3082 	conn->last_recv = jiffies;
3083 	conn->last_ping = jiffies;
3084 	if (conn->recv_timeout && conn->ping_timeout)
3085 		mod_timer(&conn->transport_timer,
3086 			  jiffies + (conn->recv_timeout * HZ));
3087 
3088 	switch(conn->stop_stage) {
3089 	case STOP_CONN_RECOVER:
3090 		/*
3091 		 * unblock eh_abort() if it is blocked. re-try all
3092 		 * commands after successful recovery
3093 		 */
3094 		conn->stop_stage = 0;
3095 		session->tmf_state = TMF_INITIAL;
3096 		session->age++;
3097 		if (session->age == 16)
3098 			session->age = 0;
3099 		break;
3100 	case STOP_CONN_TERM:
3101 		conn->stop_stage = 0;
3102 		break;
3103 	default:
3104 		break;
3105 	}
3106 	spin_unlock_bh(&session->frwd_lock);
3107 
3108 	iscsi_unblock_session(session->cls_session);
3109 	wake_up(&session->ehwait);
3110 	return 0;
3111 }
3112 EXPORT_SYMBOL_GPL(iscsi_conn_start);
3113 
3114 static void
fail_mgmt_tasks(struct iscsi_session * session,struct iscsi_conn * conn)3115 fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
3116 {
3117 	struct iscsi_task *task;
3118 	int i, state;
3119 
3120 	for (i = 0; i < conn->session->cmds_max; i++) {
3121 		task = conn->session->cmds[i];
3122 		if (task->sc)
3123 			continue;
3124 
3125 		if (task->state == ISCSI_TASK_FREE)
3126 			continue;
3127 
3128 		ISCSI_DBG_SESSION(conn->session,
3129 				  "failing mgmt itt 0x%x state %d\n",
3130 				  task->itt, task->state);
3131 		state = ISCSI_TASK_ABRT_SESS_RECOV;
3132 		if (task->state == ISCSI_TASK_PENDING)
3133 			state = ISCSI_TASK_COMPLETED;
3134 		spin_lock_bh(&session->back_lock);
3135 		iscsi_complete_task(task, state);
3136 		spin_unlock_bh(&session->back_lock);
3137 	}
3138 }
3139 
iscsi_conn_stop(struct iscsi_cls_conn * cls_conn,int flag)3140 void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
3141 {
3142 	struct iscsi_conn *conn = cls_conn->dd_data;
3143 	struct iscsi_session *session = conn->session;
3144 	int old_stop_stage;
3145 
3146 	mutex_lock(&session->eh_mutex);
3147 	spin_lock_bh(&session->frwd_lock);
3148 	if (conn->stop_stage == STOP_CONN_TERM) {
3149 		spin_unlock_bh(&session->frwd_lock);
3150 		mutex_unlock(&session->eh_mutex);
3151 		return;
3152 	}
3153 
3154 	/*
3155 	 * When this is called for the in_login state, we only want to clean
3156 	 * up the login task and connection. We do not need to block and set
3157 	 * the recovery state again
3158 	 */
3159 	if (flag == STOP_CONN_TERM)
3160 		session->state = ISCSI_STATE_TERMINATE;
3161 	else if (conn->stop_stage != STOP_CONN_RECOVER)
3162 		session->state = ISCSI_STATE_IN_RECOVERY;
3163 
3164 	old_stop_stage = conn->stop_stage;
3165 	conn->stop_stage = flag;
3166 	spin_unlock_bh(&session->frwd_lock);
3167 
3168 	del_timer_sync(&conn->transport_timer);
3169 	iscsi_suspend_tx(conn);
3170 
3171 	spin_lock_bh(&session->frwd_lock);
3172 	conn->c_stage = ISCSI_CONN_STOPPED;
3173 	spin_unlock_bh(&session->frwd_lock);
3174 
3175 	/*
3176 	 * for connection level recovery we should not calculate
3177 	 * header digest. conn->hdr_size used for optimization
3178 	 * in hdr_extract() and will be re-negotiated at
3179 	 * set_param() time.
3180 	 */
3181 	if (flag == STOP_CONN_RECOVER) {
3182 		conn->hdrdgst_en = 0;
3183 		conn->datadgst_en = 0;
3184 		if (session->state == ISCSI_STATE_IN_RECOVERY &&
3185 		    old_stop_stage != STOP_CONN_RECOVER) {
3186 			ISCSI_DBG_SESSION(session, "blocking session\n");
3187 			iscsi_block_session(session->cls_session);
3188 		}
3189 	}
3190 
3191 	/*
3192 	 * flush queues.
3193 	 */
3194 	spin_lock_bh(&session->frwd_lock);
3195 	fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
3196 	fail_mgmt_tasks(session, conn);
3197 	memset(&session->tmhdr, 0, sizeof(session->tmhdr));
3198 	spin_unlock_bh(&session->frwd_lock);
3199 	mutex_unlock(&session->eh_mutex);
3200 }
3201 EXPORT_SYMBOL_GPL(iscsi_conn_stop);
3202 
iscsi_conn_bind(struct iscsi_cls_session * cls_session,struct iscsi_cls_conn * cls_conn,int is_leading)3203 int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
3204 		    struct iscsi_cls_conn *cls_conn, int is_leading)
3205 {
3206 	struct iscsi_session *session = cls_session->dd_data;
3207 	struct iscsi_conn *conn = cls_conn->dd_data;
3208 
3209 	spin_lock_bh(&session->frwd_lock);
3210 	if (is_leading)
3211 		session->leadconn = conn;
3212 	spin_unlock_bh(&session->frwd_lock);
3213 
3214 	/*
3215 	 * Unblock xmitworker(), Login Phase will pass through.
3216 	 */
3217 	clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
3218 	clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
3219 	return 0;
3220 }
3221 EXPORT_SYMBOL_GPL(iscsi_conn_bind);
3222 
iscsi_switch_str_param(char ** param,char * new_val_buf)3223 int iscsi_switch_str_param(char **param, char *new_val_buf)
3224 {
3225 	char *new_val;
3226 
3227 	if (*param) {
3228 		if (!strcmp(*param, new_val_buf))
3229 			return 0;
3230 	}
3231 
3232 	new_val = kstrdup(new_val_buf, GFP_NOIO);
3233 	if (!new_val)
3234 		return -ENOMEM;
3235 
3236 	kfree(*param);
3237 	*param = new_val;
3238 	return 0;
3239 }
3240 EXPORT_SYMBOL_GPL(iscsi_switch_str_param);
3241 
iscsi_set_param(struct iscsi_cls_conn * cls_conn,enum iscsi_param param,char * buf,int buflen)3242 int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
3243 		    enum iscsi_param param, char *buf, int buflen)
3244 {
3245 	struct iscsi_conn *conn = cls_conn->dd_data;
3246 	struct iscsi_session *session = conn->session;
3247 	int val;
3248 
3249 	switch(param) {
3250 	case ISCSI_PARAM_FAST_ABORT:
3251 		sscanf(buf, "%d", &session->fast_abort);
3252 		break;
3253 	case ISCSI_PARAM_ABORT_TMO:
3254 		sscanf(buf, "%d", &session->abort_timeout);
3255 		break;
3256 	case ISCSI_PARAM_LU_RESET_TMO:
3257 		sscanf(buf, "%d", &session->lu_reset_timeout);
3258 		break;
3259 	case ISCSI_PARAM_TGT_RESET_TMO:
3260 		sscanf(buf, "%d", &session->tgt_reset_timeout);
3261 		break;
3262 	case ISCSI_PARAM_PING_TMO:
3263 		sscanf(buf, "%d", &conn->ping_timeout);
3264 		break;
3265 	case ISCSI_PARAM_RECV_TMO:
3266 		sscanf(buf, "%d", &conn->recv_timeout);
3267 		break;
3268 	case ISCSI_PARAM_MAX_RECV_DLENGTH:
3269 		sscanf(buf, "%d", &conn->max_recv_dlength);
3270 		break;
3271 	case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3272 		sscanf(buf, "%d", &conn->max_xmit_dlength);
3273 		break;
3274 	case ISCSI_PARAM_HDRDGST_EN:
3275 		sscanf(buf, "%d", &conn->hdrdgst_en);
3276 		break;
3277 	case ISCSI_PARAM_DATADGST_EN:
3278 		sscanf(buf, "%d", &conn->datadgst_en);
3279 		break;
3280 	case ISCSI_PARAM_INITIAL_R2T_EN:
3281 		sscanf(buf, "%d", &session->initial_r2t_en);
3282 		break;
3283 	case ISCSI_PARAM_MAX_R2T:
3284 		sscanf(buf, "%hu", &session->max_r2t);
3285 		break;
3286 	case ISCSI_PARAM_IMM_DATA_EN:
3287 		sscanf(buf, "%d", &session->imm_data_en);
3288 		break;
3289 	case ISCSI_PARAM_FIRST_BURST:
3290 		sscanf(buf, "%d", &session->first_burst);
3291 		break;
3292 	case ISCSI_PARAM_MAX_BURST:
3293 		sscanf(buf, "%d", &session->max_burst);
3294 		break;
3295 	case ISCSI_PARAM_PDU_INORDER_EN:
3296 		sscanf(buf, "%d", &session->pdu_inorder_en);
3297 		break;
3298 	case ISCSI_PARAM_DATASEQ_INORDER_EN:
3299 		sscanf(buf, "%d", &session->dataseq_inorder_en);
3300 		break;
3301 	case ISCSI_PARAM_ERL:
3302 		sscanf(buf, "%d", &session->erl);
3303 		break;
3304 	case ISCSI_PARAM_EXP_STATSN:
3305 		sscanf(buf, "%u", &conn->exp_statsn);
3306 		break;
3307 	case ISCSI_PARAM_USERNAME:
3308 		return iscsi_switch_str_param(&session->username, buf);
3309 	case ISCSI_PARAM_USERNAME_IN:
3310 		return iscsi_switch_str_param(&session->username_in, buf);
3311 	case ISCSI_PARAM_PASSWORD:
3312 		return iscsi_switch_str_param(&session->password, buf);
3313 	case ISCSI_PARAM_PASSWORD_IN:
3314 		return iscsi_switch_str_param(&session->password_in, buf);
3315 	case ISCSI_PARAM_TARGET_NAME:
3316 		return iscsi_switch_str_param(&session->targetname, buf);
3317 	case ISCSI_PARAM_TARGET_ALIAS:
3318 		return iscsi_switch_str_param(&session->targetalias, buf);
3319 	case ISCSI_PARAM_TPGT:
3320 		sscanf(buf, "%d", &session->tpgt);
3321 		break;
3322 	case ISCSI_PARAM_PERSISTENT_PORT:
3323 		sscanf(buf, "%d", &conn->persistent_port);
3324 		break;
3325 	case ISCSI_PARAM_PERSISTENT_ADDRESS:
3326 		return iscsi_switch_str_param(&conn->persistent_address, buf);
3327 	case ISCSI_PARAM_IFACE_NAME:
3328 		return iscsi_switch_str_param(&session->ifacename, buf);
3329 	case ISCSI_PARAM_INITIATOR_NAME:
3330 		return iscsi_switch_str_param(&session->initiatorname, buf);
3331 	case ISCSI_PARAM_BOOT_ROOT:
3332 		return iscsi_switch_str_param(&session->boot_root, buf);
3333 	case ISCSI_PARAM_BOOT_NIC:
3334 		return iscsi_switch_str_param(&session->boot_nic, buf);
3335 	case ISCSI_PARAM_BOOT_TARGET:
3336 		return iscsi_switch_str_param(&session->boot_target, buf);
3337 	case ISCSI_PARAM_PORTAL_TYPE:
3338 		return iscsi_switch_str_param(&session->portal_type, buf);
3339 	case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
3340 		return iscsi_switch_str_param(&session->discovery_parent_type,
3341 					      buf);
3342 	case ISCSI_PARAM_DISCOVERY_SESS:
3343 		sscanf(buf, "%d", &val);
3344 		session->discovery_sess = !!val;
3345 		break;
3346 	case ISCSI_PARAM_LOCAL_IPADDR:
3347 		return iscsi_switch_str_param(&conn->local_ipaddr, buf);
3348 	default:
3349 		return -ENOSYS;
3350 	}
3351 
3352 	return 0;
3353 }
3354 EXPORT_SYMBOL_GPL(iscsi_set_param);
3355 
iscsi_session_get_param(struct iscsi_cls_session * cls_session,enum iscsi_param param,char * buf)3356 int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
3357 			    enum iscsi_param param, char *buf)
3358 {
3359 	struct iscsi_session *session = cls_session->dd_data;
3360 	int len;
3361 
3362 	switch(param) {
3363 	case ISCSI_PARAM_FAST_ABORT:
3364 		len = sysfs_emit(buf, "%d\n", session->fast_abort);
3365 		break;
3366 	case ISCSI_PARAM_ABORT_TMO:
3367 		len = sysfs_emit(buf, "%d\n", session->abort_timeout);
3368 		break;
3369 	case ISCSI_PARAM_LU_RESET_TMO:
3370 		len = sysfs_emit(buf, "%d\n", session->lu_reset_timeout);
3371 		break;
3372 	case ISCSI_PARAM_TGT_RESET_TMO:
3373 		len = sysfs_emit(buf, "%d\n", session->tgt_reset_timeout);
3374 		break;
3375 	case ISCSI_PARAM_INITIAL_R2T_EN:
3376 		len = sysfs_emit(buf, "%d\n", session->initial_r2t_en);
3377 		break;
3378 	case ISCSI_PARAM_MAX_R2T:
3379 		len = sysfs_emit(buf, "%hu\n", session->max_r2t);
3380 		break;
3381 	case ISCSI_PARAM_IMM_DATA_EN:
3382 		len = sysfs_emit(buf, "%d\n", session->imm_data_en);
3383 		break;
3384 	case ISCSI_PARAM_FIRST_BURST:
3385 		len = sysfs_emit(buf, "%u\n", session->first_burst);
3386 		break;
3387 	case ISCSI_PARAM_MAX_BURST:
3388 		len = sysfs_emit(buf, "%u\n", session->max_burst);
3389 		break;
3390 	case ISCSI_PARAM_PDU_INORDER_EN:
3391 		len = sysfs_emit(buf, "%d\n", session->pdu_inorder_en);
3392 		break;
3393 	case ISCSI_PARAM_DATASEQ_INORDER_EN:
3394 		len = sysfs_emit(buf, "%d\n", session->dataseq_inorder_en);
3395 		break;
3396 	case ISCSI_PARAM_DEF_TASKMGMT_TMO:
3397 		len = sysfs_emit(buf, "%d\n", session->def_taskmgmt_tmo);
3398 		break;
3399 	case ISCSI_PARAM_ERL:
3400 		len = sysfs_emit(buf, "%d\n", session->erl);
3401 		break;
3402 	case ISCSI_PARAM_TARGET_NAME:
3403 		len = sysfs_emit(buf, "%s\n", session->targetname);
3404 		break;
3405 	case ISCSI_PARAM_TARGET_ALIAS:
3406 		len = sysfs_emit(buf, "%s\n", session->targetalias);
3407 		break;
3408 	case ISCSI_PARAM_TPGT:
3409 		len = sysfs_emit(buf, "%d\n", session->tpgt);
3410 		break;
3411 	case ISCSI_PARAM_USERNAME:
3412 		len = sysfs_emit(buf, "%s\n", session->username);
3413 		break;
3414 	case ISCSI_PARAM_USERNAME_IN:
3415 		len = sysfs_emit(buf, "%s\n", session->username_in);
3416 		break;
3417 	case ISCSI_PARAM_PASSWORD:
3418 		len = sysfs_emit(buf, "%s\n", session->password);
3419 		break;
3420 	case ISCSI_PARAM_PASSWORD_IN:
3421 		len = sysfs_emit(buf, "%s\n", session->password_in);
3422 		break;
3423 	case ISCSI_PARAM_IFACE_NAME:
3424 		len = sysfs_emit(buf, "%s\n", session->ifacename);
3425 		break;
3426 	case ISCSI_PARAM_INITIATOR_NAME:
3427 		len = sysfs_emit(buf, "%s\n", session->initiatorname);
3428 		break;
3429 	case ISCSI_PARAM_BOOT_ROOT:
3430 		len = sysfs_emit(buf, "%s\n", session->boot_root);
3431 		break;
3432 	case ISCSI_PARAM_BOOT_NIC:
3433 		len = sysfs_emit(buf, "%s\n", session->boot_nic);
3434 		break;
3435 	case ISCSI_PARAM_BOOT_TARGET:
3436 		len = sysfs_emit(buf, "%s\n", session->boot_target);
3437 		break;
3438 	case ISCSI_PARAM_AUTO_SND_TGT_DISABLE:
3439 		len = sysfs_emit(buf, "%u\n", session->auto_snd_tgt_disable);
3440 		break;
3441 	case ISCSI_PARAM_DISCOVERY_SESS:
3442 		len = sysfs_emit(buf, "%u\n", session->discovery_sess);
3443 		break;
3444 	case ISCSI_PARAM_PORTAL_TYPE:
3445 		len = sysfs_emit(buf, "%s\n", session->portal_type);
3446 		break;
3447 	case ISCSI_PARAM_CHAP_AUTH_EN:
3448 		len = sysfs_emit(buf, "%u\n", session->chap_auth_en);
3449 		break;
3450 	case ISCSI_PARAM_DISCOVERY_LOGOUT_EN:
3451 		len = sysfs_emit(buf, "%u\n", session->discovery_logout_en);
3452 		break;
3453 	case ISCSI_PARAM_BIDI_CHAP_EN:
3454 		len = sysfs_emit(buf, "%u\n", session->bidi_chap_en);
3455 		break;
3456 	case ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL:
3457 		len = sysfs_emit(buf, "%u\n", session->discovery_auth_optional);
3458 		break;
3459 	case ISCSI_PARAM_DEF_TIME2WAIT:
3460 		len = sysfs_emit(buf, "%d\n", session->time2wait);
3461 		break;
3462 	case ISCSI_PARAM_DEF_TIME2RETAIN:
3463 		len = sysfs_emit(buf, "%d\n", session->time2retain);
3464 		break;
3465 	case ISCSI_PARAM_TSID:
3466 		len = sysfs_emit(buf, "%u\n", session->tsid);
3467 		break;
3468 	case ISCSI_PARAM_ISID:
3469 		len = sysfs_emit(buf, "%02x%02x%02x%02x%02x%02x\n",
3470 			      session->isid[0], session->isid[1],
3471 			      session->isid[2], session->isid[3],
3472 			      session->isid[4], session->isid[5]);
3473 		break;
3474 	case ISCSI_PARAM_DISCOVERY_PARENT_IDX:
3475 		len = sysfs_emit(buf, "%u\n", session->discovery_parent_idx);
3476 		break;
3477 	case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
3478 		if (session->discovery_parent_type)
3479 			len = sysfs_emit(buf, "%s\n",
3480 				      session->discovery_parent_type);
3481 		else
3482 			len = sysfs_emit(buf, "\n");
3483 		break;
3484 	default:
3485 		return -ENOSYS;
3486 	}
3487 
3488 	return len;
3489 }
3490 EXPORT_SYMBOL_GPL(iscsi_session_get_param);
3491 
iscsi_conn_get_addr_param(struct sockaddr_storage * addr,enum iscsi_param param,char * buf)3492 int iscsi_conn_get_addr_param(struct sockaddr_storage *addr,
3493 			      enum iscsi_param param, char *buf)
3494 {
3495 	struct sockaddr_in6 *sin6 = NULL;
3496 	struct sockaddr_in *sin = NULL;
3497 	int len;
3498 
3499 	switch (addr->ss_family) {
3500 	case AF_INET:
3501 		sin = (struct sockaddr_in *)addr;
3502 		break;
3503 	case AF_INET6:
3504 		sin6 = (struct sockaddr_in6 *)addr;
3505 		break;
3506 	default:
3507 		return -EINVAL;
3508 	}
3509 
3510 	switch (param) {
3511 	case ISCSI_PARAM_CONN_ADDRESS:
3512 	case ISCSI_HOST_PARAM_IPADDRESS:
3513 		if (sin)
3514 			len = sysfs_emit(buf, "%pI4\n", &sin->sin_addr.s_addr);
3515 		else
3516 			len = sysfs_emit(buf, "%pI6\n", &sin6->sin6_addr);
3517 		break;
3518 	case ISCSI_PARAM_CONN_PORT:
3519 	case ISCSI_PARAM_LOCAL_PORT:
3520 		if (sin)
3521 			len = sysfs_emit(buf, "%hu\n", be16_to_cpu(sin->sin_port));
3522 		else
3523 			len = sysfs_emit(buf, "%hu\n",
3524 				      be16_to_cpu(sin6->sin6_port));
3525 		break;
3526 	default:
3527 		return -EINVAL;
3528 	}
3529 
3530 	return len;
3531 }
3532 EXPORT_SYMBOL_GPL(iscsi_conn_get_addr_param);
3533 
iscsi_conn_get_param(struct iscsi_cls_conn * cls_conn,enum iscsi_param param,char * buf)3534 int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
3535 			 enum iscsi_param param, char *buf)
3536 {
3537 	struct iscsi_conn *conn = cls_conn->dd_data;
3538 	int len;
3539 
3540 	switch(param) {
3541 	case ISCSI_PARAM_PING_TMO:
3542 		len = sysfs_emit(buf, "%u\n", conn->ping_timeout);
3543 		break;
3544 	case ISCSI_PARAM_RECV_TMO:
3545 		len = sysfs_emit(buf, "%u\n", conn->recv_timeout);
3546 		break;
3547 	case ISCSI_PARAM_MAX_RECV_DLENGTH:
3548 		len = sysfs_emit(buf, "%u\n", conn->max_recv_dlength);
3549 		break;
3550 	case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3551 		len = sysfs_emit(buf, "%u\n", conn->max_xmit_dlength);
3552 		break;
3553 	case ISCSI_PARAM_HDRDGST_EN:
3554 		len = sysfs_emit(buf, "%d\n", conn->hdrdgst_en);
3555 		break;
3556 	case ISCSI_PARAM_DATADGST_EN:
3557 		len = sysfs_emit(buf, "%d\n", conn->datadgst_en);
3558 		break;
3559 	case ISCSI_PARAM_IFMARKER_EN:
3560 		len = sysfs_emit(buf, "%d\n", conn->ifmarker_en);
3561 		break;
3562 	case ISCSI_PARAM_OFMARKER_EN:
3563 		len = sysfs_emit(buf, "%d\n", conn->ofmarker_en);
3564 		break;
3565 	case ISCSI_PARAM_EXP_STATSN:
3566 		len = sysfs_emit(buf, "%u\n", conn->exp_statsn);
3567 		break;
3568 	case ISCSI_PARAM_PERSISTENT_PORT:
3569 		len = sysfs_emit(buf, "%d\n", conn->persistent_port);
3570 		break;
3571 	case ISCSI_PARAM_PERSISTENT_ADDRESS:
3572 		len = sysfs_emit(buf, "%s\n", conn->persistent_address);
3573 		break;
3574 	case ISCSI_PARAM_STATSN:
3575 		len = sysfs_emit(buf, "%u\n", conn->statsn);
3576 		break;
3577 	case ISCSI_PARAM_MAX_SEGMENT_SIZE:
3578 		len = sysfs_emit(buf, "%u\n", conn->max_segment_size);
3579 		break;
3580 	case ISCSI_PARAM_KEEPALIVE_TMO:
3581 		len = sysfs_emit(buf, "%u\n", conn->keepalive_tmo);
3582 		break;
3583 	case ISCSI_PARAM_LOCAL_PORT:
3584 		len = sysfs_emit(buf, "%u\n", conn->local_port);
3585 		break;
3586 	case ISCSI_PARAM_TCP_TIMESTAMP_STAT:
3587 		len = sysfs_emit(buf, "%u\n", conn->tcp_timestamp_stat);
3588 		break;
3589 	case ISCSI_PARAM_TCP_NAGLE_DISABLE:
3590 		len = sysfs_emit(buf, "%u\n", conn->tcp_nagle_disable);
3591 		break;
3592 	case ISCSI_PARAM_TCP_WSF_DISABLE:
3593 		len = sysfs_emit(buf, "%u\n", conn->tcp_wsf_disable);
3594 		break;
3595 	case ISCSI_PARAM_TCP_TIMER_SCALE:
3596 		len = sysfs_emit(buf, "%u\n", conn->tcp_timer_scale);
3597 		break;
3598 	case ISCSI_PARAM_TCP_TIMESTAMP_EN:
3599 		len = sysfs_emit(buf, "%u\n", conn->tcp_timestamp_en);
3600 		break;
3601 	case ISCSI_PARAM_IP_FRAGMENT_DISABLE:
3602 		len = sysfs_emit(buf, "%u\n", conn->fragment_disable);
3603 		break;
3604 	case ISCSI_PARAM_IPV4_TOS:
3605 		len = sysfs_emit(buf, "%u\n", conn->ipv4_tos);
3606 		break;
3607 	case ISCSI_PARAM_IPV6_TC:
3608 		len = sysfs_emit(buf, "%u\n", conn->ipv6_traffic_class);
3609 		break;
3610 	case ISCSI_PARAM_IPV6_FLOW_LABEL:
3611 		len = sysfs_emit(buf, "%u\n", conn->ipv6_flow_label);
3612 		break;
3613 	case ISCSI_PARAM_IS_FW_ASSIGNED_IPV6:
3614 		len = sysfs_emit(buf, "%u\n", conn->is_fw_assigned_ipv6);
3615 		break;
3616 	case ISCSI_PARAM_TCP_XMIT_WSF:
3617 		len = sysfs_emit(buf, "%u\n", conn->tcp_xmit_wsf);
3618 		break;
3619 	case ISCSI_PARAM_TCP_RECV_WSF:
3620 		len = sysfs_emit(buf, "%u\n", conn->tcp_recv_wsf);
3621 		break;
3622 	case ISCSI_PARAM_LOCAL_IPADDR:
3623 		len = sysfs_emit(buf, "%s\n", conn->local_ipaddr);
3624 		break;
3625 	default:
3626 		return -ENOSYS;
3627 	}
3628 
3629 	return len;
3630 }
3631 EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
3632 
iscsi_host_get_param(struct Scsi_Host * shost,enum iscsi_host_param param,char * buf)3633 int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3634 			 char *buf)
3635 {
3636 	struct iscsi_host *ihost = shost_priv(shost);
3637 	int len;
3638 
3639 	switch (param) {
3640 	case ISCSI_HOST_PARAM_NETDEV_NAME:
3641 		len = sysfs_emit(buf, "%s\n", ihost->netdev);
3642 		break;
3643 	case ISCSI_HOST_PARAM_HWADDRESS:
3644 		len = sysfs_emit(buf, "%s\n", ihost->hwaddress);
3645 		break;
3646 	case ISCSI_HOST_PARAM_INITIATOR_NAME:
3647 		len = sysfs_emit(buf, "%s\n", ihost->initiatorname);
3648 		break;
3649 	default:
3650 		return -ENOSYS;
3651 	}
3652 
3653 	return len;
3654 }
3655 EXPORT_SYMBOL_GPL(iscsi_host_get_param);
3656 
iscsi_host_set_param(struct Scsi_Host * shost,enum iscsi_host_param param,char * buf,int buflen)3657 int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3658 			 char *buf, int buflen)
3659 {
3660 	struct iscsi_host *ihost = shost_priv(shost);
3661 
3662 	switch (param) {
3663 	case ISCSI_HOST_PARAM_NETDEV_NAME:
3664 		return iscsi_switch_str_param(&ihost->netdev, buf);
3665 	case ISCSI_HOST_PARAM_HWADDRESS:
3666 		return iscsi_switch_str_param(&ihost->hwaddress, buf);
3667 	case ISCSI_HOST_PARAM_INITIATOR_NAME:
3668 		return iscsi_switch_str_param(&ihost->initiatorname, buf);
3669 	default:
3670 		return -ENOSYS;
3671 	}
3672 
3673 	return 0;
3674 }
3675 EXPORT_SYMBOL_GPL(iscsi_host_set_param);
3676 
3677 MODULE_AUTHOR("Mike Christie");
3678 MODULE_DESCRIPTION("iSCSI library functions");
3679 MODULE_LICENSE("GPL");
3680