xref: /linux/drivers/scsi/libiscsi.c (revision 63f75cc8a7e6ce453e38a7b90cdcae83d63f1ea7)
1 /*
2  * iSCSI lib functions
3  *
4  * Copyright (C) 2006 Red Hat, Inc.  All rights reserved.
5  * Copyright (C) 2004 - 2006 Mike Christie
6  * Copyright (C) 2004 - 2005 Dmitry Yusupov
7  * Copyright (C) 2004 - 2005 Alex Aizman
8  * maintained by open-iscsi@googlegroups.com
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23  */
24 #include <linux/types.h>
25 #include <linux/mutex.h>
26 #include <linux/kfifo.h>
27 #include <linux/delay.h>
28 #include <net/tcp.h>
29 #include <scsi/scsi_cmnd.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_eh.h>
32 #include <scsi/scsi_tcq.h>
33 #include <scsi/scsi_host.h>
34 #include <scsi/scsi.h>
35 #include <scsi/iscsi_proto.h>
36 #include <scsi/scsi_transport.h>
37 #include <scsi/scsi_transport_iscsi.h>
38 #include <scsi/libiscsi.h>
39 
40 struct iscsi_session *
41 class_to_transport_session(struct iscsi_cls_session *cls_session)
42 {
43 	struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
44 	return iscsi_hostdata(shost->hostdata);
45 }
46 EXPORT_SYMBOL_GPL(class_to_transport_session);
47 
48 #define INVALID_SN_DELTA	0xffff
49 
50 int
51 iscsi_check_assign_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
52 {
53 	uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
54 	uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
55 
56 	if (max_cmdsn < exp_cmdsn -1 &&
57 	    max_cmdsn > exp_cmdsn - INVALID_SN_DELTA)
58 		return ISCSI_ERR_MAX_CMDSN;
59 	if (max_cmdsn > session->max_cmdsn ||
60 	    max_cmdsn < session->max_cmdsn - INVALID_SN_DELTA)
61 		session->max_cmdsn = max_cmdsn;
62 	if (exp_cmdsn > session->exp_cmdsn ||
63 	    exp_cmdsn < session->exp_cmdsn - INVALID_SN_DELTA)
64 		session->exp_cmdsn = exp_cmdsn;
65 
66 	return 0;
67 }
68 EXPORT_SYMBOL_GPL(iscsi_check_assign_cmdsn);
69 
70 void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
71 				   struct iscsi_data *hdr,
72 				   int transport_data_cnt)
73 {
74 	struct iscsi_conn *conn = ctask->conn;
75 
76 	memset(hdr, 0, sizeof(struct iscsi_data));
77 	hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
78 	hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
79 	ctask->unsol_datasn++;
80 	hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
81 	memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
82 
83 	hdr->itt = ctask->hdr->itt;
84 	hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
85 
86 	hdr->offset = cpu_to_be32(ctask->total_length -
87 				  transport_data_cnt -
88 				  ctask->unsol_count);
89 
90 	if (ctask->unsol_count > conn->max_xmit_dlength) {
91 		hton24(hdr->dlength, conn->max_xmit_dlength);
92 		ctask->data_count = conn->max_xmit_dlength;
93 		hdr->flags = 0;
94 	} else {
95 		hton24(hdr->dlength, ctask->unsol_count);
96 		ctask->data_count = ctask->unsol_count;
97 		hdr->flags = ISCSI_FLAG_CMD_FINAL;
98 	}
99 }
100 EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
101 
102 /**
103  * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
104  * @ctask: iscsi cmd task
105  *
106  * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
107  * fields like dlength or final based on how much data it sends
108  */
109 static void iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
110 {
111 	struct iscsi_conn *conn = ctask->conn;
112 	struct iscsi_session *session = conn->session;
113 	struct iscsi_cmd *hdr = ctask->hdr;
114 	struct scsi_cmnd *sc = ctask->sc;
115 
116         hdr->opcode = ISCSI_OP_SCSI_CMD;
117         hdr->flags = ISCSI_ATTR_SIMPLE;
118         int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
119         hdr->itt = ctask->itt | (conn->id << ISCSI_CID_SHIFT) |
120                          (session->age << ISCSI_AGE_SHIFT);
121         hdr->data_length = cpu_to_be32(sc->request_bufflen);
122         hdr->cmdsn = cpu_to_be32(session->cmdsn);
123         session->cmdsn++;
124         hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
125         memcpy(hdr->cdb, sc->cmnd, sc->cmd_len);
126         memset(&hdr->cdb[sc->cmd_len], 0, MAX_COMMAND_SIZE - sc->cmd_len);
127 
128 	if (sc->sc_data_direction == DMA_TO_DEVICE) {
129 		hdr->flags |= ISCSI_FLAG_CMD_WRITE;
130 		/*
131 		 * Write counters:
132 		 *
133 		 *	imm_count	bytes to be sent right after
134 		 *			SCSI PDU Header
135 		 *
136 		 *	unsol_count	bytes(as Data-Out) to be sent
137 		 *			without	R2T ack right after
138 		 *			immediate data
139 		 *
140 		 *	r2t_data_count	bytes to be sent via R2T ack's
141 		 *
142 		 *      pad_count       bytes to be sent as zero-padding
143 		 */
144 		ctask->imm_count = 0;
145 		ctask->unsol_count = 0;
146 		ctask->unsol_datasn = 0;
147 
148 		if (session->imm_data_en) {
149 			if (ctask->total_length >= session->first_burst)
150 				ctask->imm_count = min(session->first_burst,
151 							conn->max_xmit_dlength);
152 			else
153 				ctask->imm_count = min(ctask->total_length,
154 							conn->max_xmit_dlength);
155 			hton24(ctask->hdr->dlength, ctask->imm_count);
156 		} else
157 			zero_data(ctask->hdr->dlength);
158 
159 		if (!session->initial_r2t_en)
160 			ctask->unsol_count = min(session->first_burst,
161 				ctask->total_length) - ctask->imm_count;
162 		if (!ctask->unsol_count)
163 			/* No unsolicit Data-Out's */
164 			ctask->hdr->flags |= ISCSI_FLAG_CMD_FINAL;
165 	} else {
166 		ctask->datasn = 0;
167 		hdr->flags |= ISCSI_FLAG_CMD_FINAL;
168 		zero_data(hdr->dlength);
169 
170 		if (sc->sc_data_direction == DMA_FROM_DEVICE)
171 			hdr->flags |= ISCSI_FLAG_CMD_READ;
172 	}
173 
174 	conn->scsicmd_pdus_cnt++;
175 }
176 EXPORT_SYMBOL_GPL(iscsi_prep_scsi_cmd_pdu);
177 
178 /**
179  * iscsi_complete_command - return command back to scsi-ml
180  * @session: iscsi session
181  * @ctask: iscsi cmd task
182  *
183  * Must be called with session lock.
184  * This function returns the scsi command to scsi-ml and returns
185  * the cmd task to the pool of available cmd tasks.
186  */
187 static void iscsi_complete_command(struct iscsi_session *session,
188 				   struct iscsi_cmd_task *ctask)
189 {
190 	struct scsi_cmnd *sc = ctask->sc;
191 
192 	ctask->state = ISCSI_TASK_COMPLETED;
193 	ctask->sc = NULL;
194 	list_del_init(&ctask->running);
195 	__kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
196 	sc->scsi_done(sc);
197 }
198 
199 /**
200  * iscsi_cmd_rsp - SCSI Command Response processing
201  * @conn: iscsi connection
202  * @hdr: iscsi header
203  * @ctask: scsi command task
204  * @data: cmd data buffer
205  * @datalen: len of buffer
206  *
207  * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
208  * then completes the command and task.
209  **/
210 static int iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
211 			      struct iscsi_cmd_task *ctask, char *data,
212 			      int datalen)
213 {
214 	int rc;
215 	struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
216 	struct iscsi_session *session = conn->session;
217 	struct scsi_cmnd *sc = ctask->sc;
218 
219 	rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
220 	if (rc) {
221 		sc->result = DID_ERROR << 16;
222 		goto out;
223 	}
224 
225 	conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
226 
227 	sc->result = (DID_OK << 16) | rhdr->cmd_status;
228 
229 	if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
230 		sc->result = DID_ERROR << 16;
231 		goto out;
232 	}
233 
234 	if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
235 		int senselen;
236 
237 		if (datalen < 2) {
238 invalid_datalen:
239 			printk(KERN_ERR "iscsi: Got CHECK_CONDITION but "
240 			       "invalid data buffer size of %d\n", datalen);
241 			sc->result = DID_BAD_TARGET << 16;
242 			goto out;
243 		}
244 
245 		senselen = (data[0] << 8) | data[1];
246 		if (datalen < senselen)
247 			goto invalid_datalen;
248 
249 		memcpy(sc->sense_buffer, data + 2,
250 		       min(senselen, SCSI_SENSE_BUFFERSIZE));
251 		debug_scsi("copied %d bytes of sense\n",
252 			   min(senselen, SCSI_SENSE_BUFFERSIZE));
253 	}
254 
255 	if (sc->sc_data_direction == DMA_TO_DEVICE)
256 		goto out;
257 
258 	if (rhdr->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
259 		int res_count = be32_to_cpu(rhdr->residual_count);
260 
261 		if (res_count > 0 && res_count <= sc->request_bufflen)
262 			sc->resid = res_count;
263 		else
264 			sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
265 	} else if (rhdr->flags & ISCSI_FLAG_CMD_BIDI_UNDERFLOW)
266 		sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
267 	else if (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW)
268 		sc->resid = be32_to_cpu(rhdr->residual_count);
269 
270 out:
271 	debug_scsi("done [sc %lx res %d itt 0x%x]\n",
272 		   (long)sc, sc->result, ctask->itt);
273 	conn->scsirsp_pdus_cnt++;
274 
275 	iscsi_complete_command(conn->session, ctask);
276 	return rc;
277 }
278 
279 static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
280 {
281 	struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
282 
283 	conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
284 	conn->tmfrsp_pdus_cnt++;
285 
286 	if (conn->tmabort_state != TMABORT_INITIAL)
287 		return;
288 
289 	if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
290 		conn->tmabort_state = TMABORT_SUCCESS;
291 	else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
292 		conn->tmabort_state = TMABORT_NOT_FOUND;
293 	else
294 		conn->tmabort_state = TMABORT_FAILED;
295 	wake_up(&conn->ehwait);
296 }
297 
298 /**
299  * __iscsi_complete_pdu - complete pdu
300  * @conn: iscsi conn
301  * @hdr: iscsi header
302  * @data: data buffer
303  * @datalen: len of data buffer
304  *
305  * Completes pdu processing by freeing any resources allocated at
306  * queuecommand or send generic. session lock must be held and verify
307  * itt must have been called.
308  */
309 int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
310 			 char *data, int datalen)
311 {
312 	struct iscsi_session *session = conn->session;
313 	int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
314 	struct iscsi_cmd_task *ctask;
315 	struct iscsi_mgmt_task *mtask;
316 	uint32_t itt;
317 
318 	if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG))
319 		itt = hdr->itt & ISCSI_ITT_MASK;
320 	else
321 		itt = hdr->itt;
322 
323 	if (itt < session->cmds_max) {
324 		ctask = session->cmds[itt];
325 
326 		debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
327 			   opcode, conn->id, ctask->itt, datalen);
328 
329 		switch(opcode) {
330 		case ISCSI_OP_SCSI_CMD_RSP:
331 			BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
332 			rc = iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
333 						datalen);
334 			break;
335 		case ISCSI_OP_SCSI_DATA_IN:
336 			BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
337 			if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
338 				conn->scsirsp_pdus_cnt++;
339 				iscsi_complete_command(session, ctask);
340 			}
341 			break;
342 		case ISCSI_OP_R2T:
343 			/* LLD handles this for now */
344 			break;
345 		default:
346 			rc = ISCSI_ERR_BAD_OPCODE;
347 			break;
348 		}
349 	} else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
350 		   itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
351 		mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
352 
353 		debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
354 			   opcode, conn->id, mtask->itt, datalen);
355 
356 		rc = iscsi_check_assign_cmdsn(session,
357 					      (struct iscsi_nopin*)hdr);
358 		if (rc)
359 			goto done;
360 
361 		switch(opcode) {
362 		case ISCSI_OP_LOGOUT_RSP:
363 			conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
364 			/* fall through */
365 		case ISCSI_OP_LOGIN_RSP:
366 		case ISCSI_OP_TEXT_RSP:
367 			/*
368 			 * login related PDU's exp_statsn is handled in
369 			 * userspace
370 			 */
371 			rc = iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen);
372 			list_del(&mtask->running);
373 			if (conn->login_mtask != mtask)
374 				__kfifo_put(session->mgmtpool.queue,
375 					    (void*)&mtask, sizeof(void*));
376 			break;
377 		case ISCSI_OP_SCSI_TMFUNC_RSP:
378 			if (datalen) {
379 				rc = ISCSI_ERR_PROTO;
380 				break;
381 			}
382 
383 			iscsi_tmf_rsp(conn, hdr);
384 			break;
385 		case ISCSI_OP_NOOP_IN:
386 			if (hdr->ttt != ISCSI_RESERVED_TAG) {
387 				rc = ISCSI_ERR_PROTO;
388 				break;
389 			}
390 			conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
391 
392 			rc = iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen);
393 			list_del(&mtask->running);
394 			if (conn->login_mtask != mtask)
395 				__kfifo_put(session->mgmtpool.queue,
396 					    (void*)&mtask, sizeof(void*));
397 			break;
398 		default:
399 			rc = ISCSI_ERR_BAD_OPCODE;
400 			break;
401 		}
402 	} else if (itt == ISCSI_RESERVED_TAG) {
403 		switch(opcode) {
404 		case ISCSI_OP_NOOP_IN:
405 			if (!datalen) {
406 				rc = iscsi_check_assign_cmdsn(session,
407 						 (struct iscsi_nopin*)hdr);
408 				if (!rc && hdr->ttt != ISCSI_RESERVED_TAG)
409 					rc = iscsi_recv_pdu(conn->cls_conn,
410 							    hdr, NULL, 0);
411 			} else
412 				rc = ISCSI_ERR_PROTO;
413 			break;
414 		case ISCSI_OP_REJECT:
415 			/* we need sth like iscsi_reject_rsp()*/
416 		case ISCSI_OP_ASYNC_EVENT:
417 			conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
418 			/* we need sth like iscsi_async_event_rsp() */
419 			rc = ISCSI_ERR_BAD_OPCODE;
420 			break;
421 		default:
422 			rc = ISCSI_ERR_BAD_OPCODE;
423 			break;
424 		}
425 	} else
426 		rc = ISCSI_ERR_BAD_ITT;
427 
428 done:
429 	return rc;
430 }
431 EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
432 
433 int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
434 		       char *data, int datalen)
435 {
436 	int rc;
437 
438 	spin_lock(&conn->session->lock);
439 	rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
440 	spin_unlock(&conn->session->lock);
441 	return rc;
442 }
443 EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
444 
445 /* verify itt (itt encoding: age+cid+itt) */
446 int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
447 		     uint32_t *ret_itt)
448 {
449 	struct iscsi_session *session = conn->session;
450 	struct iscsi_cmd_task *ctask;
451 	uint32_t itt;
452 
453 	if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
454 		if ((hdr->itt & ISCSI_AGE_MASK) !=
455 		    (session->age << ISCSI_AGE_SHIFT)) {
456 			printk(KERN_ERR "iscsi: received itt %x expected "
457 				"session age (%x)\n", hdr->itt,
458 				session->age & ISCSI_AGE_MASK);
459 			return ISCSI_ERR_BAD_ITT;
460 		}
461 
462 		if ((hdr->itt & ISCSI_CID_MASK) !=
463 		    (conn->id << ISCSI_CID_SHIFT)) {
464 			printk(KERN_ERR "iscsi: received itt %x, expected "
465 				"CID (%x)\n", hdr->itt, conn->id);
466 			return ISCSI_ERR_BAD_ITT;
467 		}
468 		itt = hdr->itt & ISCSI_ITT_MASK;
469 	} else
470 		itt = hdr->itt;
471 
472 	if (itt < session->cmds_max) {
473 		ctask = session->cmds[itt];
474 
475 		if (!ctask->sc) {
476 			printk(KERN_INFO "iscsi: dropping ctask with "
477 			       "itt 0x%x\n", ctask->itt);
478 			/* force drop */
479 			return ISCSI_ERR_NO_SCSI_CMD;
480 		}
481 
482 		if (ctask->sc->SCp.phase != session->age) {
483 			printk(KERN_ERR "iscsi: ctask's session age %d, "
484 				"expected %d\n", ctask->sc->SCp.phase,
485 				session->age);
486 			return ISCSI_ERR_SESSION_FAILED;
487 		}
488 	}
489 
490 	*ret_itt = itt;
491 	return 0;
492 }
493 EXPORT_SYMBOL_GPL(iscsi_verify_itt);
494 
495 void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
496 {
497 	struct iscsi_session *session = conn->session;
498 	unsigned long flags;
499 
500 	spin_lock_irqsave(&session->lock, flags);
501 	if (session->state == ISCSI_STATE_FAILED) {
502 		spin_unlock_irqrestore(&session->lock, flags);
503 		return;
504 	}
505 
506 	if (conn->stop_stage == 0)
507 		session->state = ISCSI_STATE_FAILED;
508 	spin_unlock_irqrestore(&session->lock, flags);
509 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
510 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
511 	iscsi_conn_error(conn->cls_conn, err);
512 }
513 EXPORT_SYMBOL_GPL(iscsi_conn_failure);
514 
515 /**
516  * iscsi_data_xmit - xmit any command into the scheduled connection
517  * @conn: iscsi connection
518  *
519  * Notes:
520  *	The function can return -EAGAIN in which case the caller must
521  *	re-schedule it again later or recover. '0' return code means
522  *	successful xmit.
523  **/
524 static int iscsi_data_xmit(struct iscsi_conn *conn)
525 {
526 	struct iscsi_transport *tt;
527 	int rc = 0;
528 
529 	if (unlikely(conn->suspend_tx)) {
530 		debug_scsi("conn %d Tx suspended!\n", conn->id);
531 		return -ENODATA;
532 	}
533 	tt = conn->session->tt;
534 
535 	/*
536 	 * Transmit in the following order:
537 	 *
538 	 * 1) un-finished xmit (ctask or mtask)
539 	 * 2) immediate control PDUs
540 	 * 3) write data
541 	 * 4) SCSI commands
542 	 * 5) non-immediate control PDUs
543 	 *
544 	 * No need to lock around __kfifo_get as long as
545 	 * there's one producer and one consumer.
546 	 */
547 
548 	BUG_ON(conn->ctask && conn->mtask);
549 
550 	if (conn->ctask) {
551 		rc = tt->xmit_cmd_task(conn, conn->ctask);
552 		if (rc)
553 			goto again;
554 		/* done with this in-progress ctask */
555 		conn->ctask = NULL;
556 	}
557 	if (conn->mtask) {
558 		rc = tt->xmit_mgmt_task(conn, conn->mtask);
559 	        if (rc)
560 		        goto again;
561 		/* done with this in-progress mtask */
562 		conn->mtask = NULL;
563 	}
564 
565 	/* process immediate first */
566         if (unlikely(__kfifo_len(conn->immqueue))) {
567 	        while (__kfifo_get(conn->immqueue, (void*)&conn->mtask,
568 			           sizeof(void*))) {
569 			spin_lock_bh(&conn->session->lock);
570 			list_add_tail(&conn->mtask->running,
571 				      &conn->mgmt_run_list);
572 			spin_unlock_bh(&conn->session->lock);
573 			rc = tt->xmit_mgmt_task(conn, conn->mtask);
574 		        if (rc)
575 			        goto again;
576 	        }
577 		/* done with this mtask */
578 		conn->mtask = NULL;
579 	}
580 
581 	/* process command queue */
582 	spin_lock_bh(&conn->session->lock);
583 	while (!list_empty(&conn->xmitqueue)) {
584 		/*
585 		 * iscsi tcp may readd the task to the xmitqueue to send
586 		 * write data
587 		 */
588 		conn->ctask = list_entry(conn->xmitqueue.next,
589 					 struct iscsi_cmd_task, running);
590 		conn->ctask->state = ISCSI_TASK_RUNNING;
591 		list_move_tail(conn->xmitqueue.next, &conn->run_list);
592 		spin_unlock_bh(&conn->session->lock);
593 
594 		rc = tt->xmit_cmd_task(conn, conn->ctask);
595 		if (rc)
596 			goto again;
597 		spin_lock_bh(&conn->session->lock);
598 	}
599 	spin_unlock_bh(&conn->session->lock);
600 	/* done with this ctask */
601 	conn->ctask = NULL;
602 
603 	/* process the rest control plane PDUs, if any */
604         if (unlikely(__kfifo_len(conn->mgmtqueue))) {
605 	        while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
606 			           sizeof(void*))) {
607 			spin_lock_bh(&conn->session->lock);
608 			list_add_tail(&conn->mtask->running,
609 				      &conn->mgmt_run_list);
610 			spin_unlock_bh(&conn->session->lock);
611 		        rc = tt->xmit_mgmt_task(conn, conn->mtask);
612 			if (rc)
613 			        goto again;
614 	        }
615 		/* done with this mtask */
616 		conn->mtask = NULL;
617 	}
618 
619 	return -ENODATA;
620 
621 again:
622 	if (unlikely(conn->suspend_tx))
623 		return -ENODATA;
624 
625 	return rc;
626 }
627 
628 static void iscsi_xmitworker(void *data)
629 {
630 	struct iscsi_conn *conn = data;
631 	int rc;
632 	/*
633 	 * serialize Xmit worker on a per-connection basis.
634 	 */
635 	mutex_lock(&conn->xmitmutex);
636 	do {
637 		rc = iscsi_data_xmit(conn);
638 	} while (rc >= 0 || rc == -EAGAIN);
639 	mutex_unlock(&conn->xmitmutex);
640 }
641 
642 enum {
643 	FAILURE_BAD_HOST = 1,
644 	FAILURE_SESSION_FAILED,
645 	FAILURE_SESSION_FREED,
646 	FAILURE_WINDOW_CLOSED,
647 	FAILURE_SESSION_TERMINATE,
648 	FAILURE_SESSION_IN_RECOVERY,
649 	FAILURE_SESSION_RECOVERY_TIMEOUT,
650 };
651 
652 int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
653 {
654 	struct Scsi_Host *host;
655 	int reason = 0;
656 	struct iscsi_session *session;
657 	struct iscsi_conn *conn;
658 	struct iscsi_cmd_task *ctask = NULL;
659 
660 	sc->scsi_done = done;
661 	sc->result = 0;
662 
663 	host = sc->device->host;
664 	session = iscsi_hostdata(host->hostdata);
665 
666 	spin_lock(&session->lock);
667 
668 	/*
669 	 * ISCSI_STATE_FAILED is a temp. state. The recovery
670 	 * code will decide what is best to do with command queued
671 	 * during this time
672 	 */
673 	if (session->state != ISCSI_STATE_LOGGED_IN &&
674 	    session->state != ISCSI_STATE_FAILED) {
675 		/*
676 		 * to handle the race between when we set the recovery state
677 		 * and block the session we requeue here (commands could
678 		 * be entering our queuecommand while a block is starting
679 		 * up because the block code is not locked)
680 		 */
681 		if (session->state == ISCSI_STATE_IN_RECOVERY) {
682 			reason = FAILURE_SESSION_IN_RECOVERY;
683 			goto reject;
684 		}
685 
686 		if (session->state == ISCSI_STATE_RECOVERY_FAILED)
687 			reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
688 		else if (session->state == ISCSI_STATE_TERMINATE)
689 			reason = FAILURE_SESSION_TERMINATE;
690 		else
691 			reason = FAILURE_SESSION_FREED;
692 		goto fault;
693 	}
694 
695 	/*
696 	 * Check for iSCSI window and take care of CmdSN wrap-around
697 	 */
698 	if ((int)(session->max_cmdsn - session->cmdsn) < 0) {
699 		reason = FAILURE_WINDOW_CLOSED;
700 		goto reject;
701 	}
702 
703 	conn = session->leadconn;
704 
705 	__kfifo_get(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
706 	sc->SCp.phase = session->age;
707 	sc->SCp.ptr = (char *)ctask;
708 
709 	ctask->state = ISCSI_TASK_PENDING;
710 	ctask->mtask = NULL;
711 	ctask->conn = conn;
712 	ctask->sc = sc;
713 	INIT_LIST_HEAD(&ctask->running);
714 	ctask->total_length = sc->request_bufflen;
715 	iscsi_prep_scsi_cmd_pdu(ctask);
716 
717 	session->tt->init_cmd_task(ctask);
718 
719 	list_add_tail(&ctask->running, &conn->xmitqueue);
720 	debug_scsi(
721 	       "ctask enq [%s cid %d sc %lx itt 0x%x len %d cmdsn %d win %d]\n",
722 		sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
723 		conn->id, (long)sc, ctask->itt, sc->request_bufflen,
724 		session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
725 	spin_unlock(&session->lock);
726 
727 	scsi_queue_work(host, &conn->xmitwork);
728 	return 0;
729 
730 reject:
731 	spin_unlock(&session->lock);
732 	debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
733 	return SCSI_MLQUEUE_HOST_BUSY;
734 
735 fault:
736 	spin_unlock(&session->lock);
737 	printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
738 	       sc->cmnd[0], reason);
739 	sc->result = (DID_NO_CONNECT << 16);
740 	sc->resid = sc->request_bufflen;
741 	sc->scsi_done(sc);
742 	return 0;
743 }
744 EXPORT_SYMBOL_GPL(iscsi_queuecommand);
745 
746 int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
747 {
748 	if (depth > ISCSI_MAX_CMD_PER_LUN)
749 		depth = ISCSI_MAX_CMD_PER_LUN;
750 	scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
751 	return sdev->queue_depth;
752 }
753 EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
754 
755 static int
756 iscsi_conn_send_generic(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
757 			char *data, uint32_t data_size)
758 {
759 	struct iscsi_session *session = conn->session;
760 	struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
761 	struct iscsi_mgmt_task *mtask;
762 
763 	spin_lock_bh(&session->lock);
764 	if (session->state == ISCSI_STATE_TERMINATE) {
765 		spin_unlock_bh(&session->lock);
766 		return -EPERM;
767 	}
768 	if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
769 	    hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
770 		/*
771 		 * Login and Text are sent serially, in
772 		 * request-followed-by-response sequence.
773 		 * Same mtask can be used. Same ITT must be used.
774 		 * Note that login_mtask is preallocated at conn_create().
775 		 */
776 		mtask = conn->login_mtask;
777 	else {
778 		BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
779 		BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
780 
781 		nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
782 		if (!__kfifo_get(session->mgmtpool.queue,
783 				 (void*)&mtask, sizeof(void*))) {
784 			spin_unlock_bh(&session->lock);
785 			return -ENOSPC;
786 		}
787 	}
788 
789 	/*
790 	 * pre-format CmdSN for outgoing PDU.
791 	 */
792 	if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
793 		hdr->itt = mtask->itt | (conn->id << ISCSI_CID_SHIFT) |
794 			   (session->age << ISCSI_AGE_SHIFT);
795 		nop->cmdsn = cpu_to_be32(session->cmdsn);
796 		if (conn->c_stage == ISCSI_CONN_STARTED &&
797 		    !(hdr->opcode & ISCSI_OP_IMMEDIATE))
798 			session->cmdsn++;
799 	} else
800 		/* do not advance CmdSN */
801 		nop->cmdsn = cpu_to_be32(session->cmdsn);
802 
803 	if (data_size) {
804 		memcpy(mtask->data, data, data_size);
805 		mtask->data_count = data_size;
806 	} else
807 		mtask->data_count = 0;
808 
809 	INIT_LIST_HEAD(&mtask->running);
810 	memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
811 	if (session->tt->init_mgmt_task)
812 		session->tt->init_mgmt_task(conn, mtask, data, data_size);
813 	spin_unlock_bh(&session->lock);
814 
815 	debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
816 		   hdr->opcode, hdr->itt, data_size);
817 
818 	/*
819 	 * since send_pdu() could be called at least from two contexts,
820 	 * we need to serialize __kfifo_put, so we don't have to take
821 	 * additional lock on fast data-path
822 	 */
823         if (hdr->opcode & ISCSI_OP_IMMEDIATE)
824 	        __kfifo_put(conn->immqueue, (void*)&mtask, sizeof(void*));
825 	else
826 	        __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
827 
828 	scsi_queue_work(session->host, &conn->xmitwork);
829 	return 0;
830 }
831 
832 int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
833 			char *data, uint32_t data_size)
834 {
835 	struct iscsi_conn *conn = cls_conn->dd_data;
836 	int rc;
837 
838 	mutex_lock(&conn->xmitmutex);
839 	rc = iscsi_conn_send_generic(conn, hdr, data, data_size);
840 	mutex_unlock(&conn->xmitmutex);
841 
842 	return rc;
843 }
844 EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
845 
846 void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
847 {
848 	struct iscsi_session *session = class_to_transport_session(cls_session);
849 	struct iscsi_conn *conn = session->leadconn;
850 
851 	spin_lock_bh(&session->lock);
852 	if (session->state != ISCSI_STATE_LOGGED_IN) {
853 		session->state = ISCSI_STATE_RECOVERY_FAILED;
854 		if (conn)
855 			wake_up(&conn->ehwait);
856 	}
857 	spin_unlock_bh(&session->lock);
858 }
859 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
860 
861 int iscsi_eh_host_reset(struct scsi_cmnd *sc)
862 {
863 	struct Scsi_Host *host = sc->device->host;
864 	struct iscsi_session *session = iscsi_hostdata(host->hostdata);
865 	struct iscsi_conn *conn = session->leadconn;
866 	int fail_session = 0;
867 
868 	spin_lock_bh(&session->lock);
869 	if (session->state == ISCSI_STATE_TERMINATE) {
870 failed:
871 		debug_scsi("failing host reset: session terminated "
872 			   "[CID %d age %d]", conn->id, session->age);
873 		spin_unlock_bh(&session->lock);
874 		return FAILED;
875 	}
876 
877 	if (sc->SCp.phase == session->age) {
878 		debug_scsi("failing connection CID %d due to SCSI host reset",
879 			   conn->id);
880 		fail_session = 1;
881 	}
882 	spin_unlock_bh(&session->lock);
883 
884 	/*
885 	 * we drop the lock here but the leadconn cannot be destoyed while
886 	 * we are in the scsi eh
887 	 */
888 	if (fail_session)
889 		iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
890 
891 	debug_scsi("iscsi_eh_host_reset wait for relogin\n");
892 	wait_event_interruptible(conn->ehwait,
893 				 session->state == ISCSI_STATE_TERMINATE ||
894 				 session->state == ISCSI_STATE_LOGGED_IN ||
895 				 session->state == ISCSI_STATE_RECOVERY_FAILED);
896 	if (signal_pending(current))
897 		flush_signals(current);
898 
899 	spin_lock_bh(&session->lock);
900 	if (session->state == ISCSI_STATE_LOGGED_IN)
901 		printk(KERN_INFO "iscsi: host reset succeeded\n");
902 	else
903 		goto failed;
904 	spin_unlock_bh(&session->lock);
905 
906 	return SUCCESS;
907 }
908 EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
909 
910 static void iscsi_tmabort_timedout(unsigned long data)
911 {
912 	struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
913 	struct iscsi_conn *conn = ctask->conn;
914 	struct iscsi_session *session = conn->session;
915 
916 	spin_lock(&session->lock);
917 	if (conn->tmabort_state == TMABORT_INITIAL) {
918 		conn->tmabort_state = TMABORT_TIMEDOUT;
919 		debug_scsi("tmabort timedout [sc %p itt 0x%x]\n",
920 			ctask->sc, ctask->itt);
921 		/* unblock eh_abort() */
922 		wake_up(&conn->ehwait);
923 	}
924 	spin_unlock(&session->lock);
925 }
926 
927 /* must be called with the mutex lock */
928 static int iscsi_exec_abort_task(struct scsi_cmnd *sc,
929 				 struct iscsi_cmd_task *ctask)
930 {
931 	struct iscsi_conn *conn = ctask->conn;
932 	struct iscsi_session *session = conn->session;
933 	struct iscsi_tm *hdr = &conn->tmhdr;
934 	int rc;
935 
936 	/*
937 	 * ctask timed out but session is OK requests must be serialized.
938 	 */
939 	memset(hdr, 0, sizeof(struct iscsi_tm));
940 	hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
941 	hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
942 	hdr->flags |= ISCSI_FLAG_CMD_FINAL;
943 	memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
944 	hdr->rtt = ctask->hdr->itt;
945 	hdr->refcmdsn = ctask->hdr->cmdsn;
946 
947 	rc = iscsi_conn_send_generic(conn, (struct iscsi_hdr *)hdr,
948 				     NULL, 0);
949 	if (rc) {
950 		iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
951 		debug_scsi("abort sent failure [itt 0x%x] %d", ctask->itt, rc);
952 		return rc;
953 	}
954 
955 	debug_scsi("abort sent [itt 0x%x]\n", ctask->itt);
956 
957 	spin_lock_bh(&session->lock);
958 	ctask->mtask = (struct iscsi_mgmt_task *)
959 			session->mgmt_cmds[(hdr->itt & ISCSI_ITT_MASK) -
960 					ISCSI_MGMT_ITT_OFFSET];
961 
962 	if (conn->tmabort_state == TMABORT_INITIAL) {
963 		conn->tmfcmd_pdus_cnt++;
964 		conn->tmabort_timer.expires = 10*HZ + jiffies;
965 		conn->tmabort_timer.function = iscsi_tmabort_timedout;
966 		conn->tmabort_timer.data = (unsigned long)ctask;
967 		add_timer(&conn->tmabort_timer);
968 		debug_scsi("abort set timeout [itt 0x%x]", ctask->itt);
969 	}
970 	spin_unlock_bh(&session->lock);
971 	mutex_unlock(&conn->xmitmutex);
972 
973 	/*
974 	 * block eh thread until:
975 	 *
976 	 * 1) abort response
977 	 * 2) abort timeout
978 	 * 3) session is terminated or restarted or userspace has
979 	 * given up on recovery
980 	 */
981 	wait_event_interruptible(conn->ehwait,
982 				 sc->SCp.phase != session->age ||
983 				 session->state != ISCSI_STATE_LOGGED_IN ||
984 				 conn->tmabort_state != TMABORT_INITIAL);
985 	if (signal_pending(current))
986 		flush_signals(current);
987 	del_timer_sync(&conn->tmabort_timer);
988 
989 	mutex_lock(&conn->xmitmutex);
990 	return 0;
991 }
992 
993 /*
994  * xmit mutex and session lock must be held
995  */
996 static struct iscsi_mgmt_task *
997 iscsi_remove_mgmt_task(struct kfifo *fifo, uint32_t itt)
998 {
999 	int i, nr_tasks = __kfifo_len(fifo) / sizeof(void*);
1000 	struct iscsi_mgmt_task *task;
1001 
1002 	debug_scsi("searching %d tasks\n", nr_tasks);
1003 
1004 	for (i = 0; i < nr_tasks; i++) {
1005 		__kfifo_get(fifo, (void*)&task, sizeof(void*));
1006 		debug_scsi("check task %u\n", task->itt);
1007 
1008 		if (task->itt == itt) {
1009 			debug_scsi("matched task\n");
1010 			return task;
1011 		}
1012 
1013 		__kfifo_put(fifo, (void*)&task, sizeof(void*));
1014 	}
1015 	return NULL;
1016 }
1017 
1018 static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task *ctask)
1019 {
1020 	struct iscsi_conn *conn = ctask->conn;
1021 	struct iscsi_session *session = conn->session;
1022 
1023 	if (!ctask->mtask)
1024 		return -EINVAL;
1025 
1026 	if (!iscsi_remove_mgmt_task(conn->immqueue, ctask->mtask->itt))
1027 		list_del(&ctask->mtask->running);
1028 	__kfifo_put(session->mgmtpool.queue, (void*)&ctask->mtask,
1029 		    sizeof(void*));
1030 	ctask->mtask = NULL;
1031 	return 0;
1032 }
1033 
1034 /*
1035  * session lock and xmitmutex must be held
1036  */
1037 static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1038 			 int err)
1039 {
1040 	struct scsi_cmnd *sc;
1041 
1042 	sc = ctask->sc;
1043 	if (!sc)
1044 		return;
1045 
1046 	conn->session->tt->cleanup_cmd_task(conn, ctask);
1047 	iscsi_ctask_mtask_cleanup(ctask);
1048 
1049 	sc->result = err;
1050 	sc->resid = sc->request_bufflen;
1051 	iscsi_complete_command(conn->session, ctask);
1052 }
1053 
1054 int iscsi_eh_abort(struct scsi_cmnd *sc)
1055 {
1056 	struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
1057 	struct iscsi_conn *conn = ctask->conn;
1058 	struct iscsi_session *session = conn->session;
1059 	int rc;
1060 
1061 	conn->eh_abort_cnt++;
1062 	debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
1063 
1064 	mutex_lock(&conn->xmitmutex);
1065 	spin_lock_bh(&session->lock);
1066 
1067 	/*
1068 	 * If we are not logged in or we have started a new session
1069 	 * then let the host reset code handle this
1070 	 */
1071 	if (session->state != ISCSI_STATE_LOGGED_IN ||
1072 	    sc->SCp.phase != session->age)
1073 		goto failed;
1074 
1075 	/* ctask completed before time out */
1076 	if (!ctask->sc) {
1077 		spin_unlock_bh(&session->lock);
1078 		debug_scsi("sc completed while abort in progress\n");
1079 		goto success_rel_mutex;
1080 	}
1081 
1082 	/* what should we do here ? */
1083 	if (conn->ctask == ctask) {
1084 		printk(KERN_INFO "iscsi: sc %p itt 0x%x partially sent. "
1085 		       "Failing abort\n", sc, ctask->itt);
1086 		goto failed;
1087 	}
1088 
1089 	if (ctask->state == ISCSI_TASK_PENDING)
1090 		goto success_cleanup;
1091 
1092 	conn->tmabort_state = TMABORT_INITIAL;
1093 
1094 	spin_unlock_bh(&session->lock);
1095 	rc = iscsi_exec_abort_task(sc, ctask);
1096 	spin_lock_bh(&session->lock);
1097 
1098 	if (rc || sc->SCp.phase != session->age ||
1099 	    session->state != ISCSI_STATE_LOGGED_IN)
1100 		goto failed;
1101 	iscsi_ctask_mtask_cleanup(ctask);
1102 
1103 	switch (conn->tmabort_state) {
1104 	case TMABORT_SUCCESS:
1105 		goto success_cleanup;
1106 	case TMABORT_NOT_FOUND:
1107 		if (!ctask->sc) {
1108 			/* ctask completed before tmf abort response */
1109 			spin_unlock_bh(&session->lock);
1110 			debug_scsi("sc completed while abort in progress\n");
1111 			goto success_rel_mutex;
1112 		}
1113 		/* fall through */
1114 	default:
1115 		/* timedout or failed */
1116 		spin_unlock_bh(&session->lock);
1117 		iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1118 		spin_lock_bh(&session->lock);
1119 		goto failed;
1120 	}
1121 
1122 success_cleanup:
1123 	debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
1124 	spin_unlock_bh(&session->lock);
1125 
1126 	/*
1127 	 * clean up task if aborted. we have the xmitmutex so grab
1128 	 * the recv lock as a writer
1129 	 */
1130 	write_lock_bh(conn->recv_lock);
1131 	spin_lock(&session->lock);
1132 	fail_command(conn, ctask, DID_ABORT << 16);
1133 	spin_unlock(&session->lock);
1134 	write_unlock_bh(conn->recv_lock);
1135 
1136 success_rel_mutex:
1137 	mutex_unlock(&conn->xmitmutex);
1138 	return SUCCESS;
1139 
1140 failed:
1141 	spin_unlock_bh(&session->lock);
1142 	mutex_unlock(&conn->xmitmutex);
1143 
1144 	debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
1145 	return FAILED;
1146 }
1147 EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1148 
1149 int
1150 iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
1151 {
1152 	int i;
1153 
1154 	*items = kmalloc(max * sizeof(void*), GFP_KERNEL);
1155 	if (*items == NULL)
1156 		return -ENOMEM;
1157 
1158 	q->max = max;
1159 	q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
1160 	if (q->pool == NULL) {
1161 		kfree(*items);
1162 		return -ENOMEM;
1163 	}
1164 
1165 	q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1166 			      GFP_KERNEL, NULL);
1167 	if (q->queue == ERR_PTR(-ENOMEM)) {
1168 		kfree(q->pool);
1169 		kfree(*items);
1170 		return -ENOMEM;
1171 	}
1172 
1173 	for (i = 0; i < max; i++) {
1174 		q->pool[i] = kmalloc(item_size, GFP_KERNEL);
1175 		if (q->pool[i] == NULL) {
1176 			int j;
1177 
1178 			for (j = 0; j < i; j++)
1179 				kfree(q->pool[j]);
1180 
1181 			kfifo_free(q->queue);
1182 			kfree(q->pool);
1183 			kfree(*items);
1184 			return -ENOMEM;
1185 		}
1186 		memset(q->pool[i], 0, item_size);
1187 		(*items)[i] = q->pool[i];
1188 		__kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1189 	}
1190 	return 0;
1191 }
1192 EXPORT_SYMBOL_GPL(iscsi_pool_init);
1193 
1194 void iscsi_pool_free(struct iscsi_queue *q, void **items)
1195 {
1196 	int i;
1197 
1198 	for (i = 0; i < q->max; i++)
1199 		kfree(items[i]);
1200 	kfree(q->pool);
1201 	kfree(items);
1202 }
1203 EXPORT_SYMBOL_GPL(iscsi_pool_free);
1204 
1205 /*
1206  * iSCSI Session's hostdata organization:
1207  *
1208  *    *------------------* <== hostdata_session(host->hostdata)
1209  *    | ptr to class sess|
1210  *    |------------------| <== iscsi_hostdata(host->hostdata)
1211  *    | iscsi_session    |
1212  *    *------------------*
1213  */
1214 
1215 #define hostdata_privsize(_sz)	(sizeof(unsigned long) + _sz + \
1216 				 _sz % sizeof(unsigned long))
1217 
1218 #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1219 
1220 /**
1221  * iscsi_session_setup - create iscsi cls session and host and session
1222  * @scsit: scsi transport template
1223  * @iscsit: iscsi transport template
1224  * @initial_cmdsn: initial CmdSN
1225  * @hostno: host no allocated
1226  *
1227  * This can be used by software iscsi_transports that allocate
1228  * a session per scsi host.
1229  **/
1230 struct iscsi_cls_session *
1231 iscsi_session_setup(struct iscsi_transport *iscsit,
1232 		    struct scsi_transport_template *scsit,
1233 		    int cmd_task_size, int mgmt_task_size,
1234 		    uint32_t initial_cmdsn, uint32_t *hostno)
1235 {
1236 	struct Scsi_Host *shost;
1237 	struct iscsi_session *session;
1238 	struct iscsi_cls_session *cls_session;
1239 	int cmd_i;
1240 
1241 	shost = scsi_host_alloc(iscsit->host_template,
1242 				hostdata_privsize(sizeof(*session)));
1243 	if (!shost)
1244 		return NULL;
1245 
1246 	shost->max_id = 1;
1247 	shost->max_channel = 0;
1248 	shost->max_lun = iscsit->max_lun;
1249 	shost->max_cmd_len = iscsit->max_cmd_len;
1250 	shost->transportt = scsit;
1251 	shost->transportt->create_work_queue = 1;
1252 	*hostno = shost->host_no;
1253 
1254 	session = iscsi_hostdata(shost->hostdata);
1255 	memset(session, 0, sizeof(struct iscsi_session));
1256 	session->host = shost;
1257 	session->state = ISCSI_STATE_FREE;
1258 	session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
1259 	session->cmds_max = ISCSI_XMIT_CMDS_MAX;
1260 	session->cmdsn = initial_cmdsn;
1261 	session->exp_cmdsn = initial_cmdsn + 1;
1262 	session->max_cmdsn = initial_cmdsn + 1;
1263 	session->max_r2t = 1;
1264 	session->tt = iscsit;
1265 
1266 	/* initialize SCSI PDU commands pool */
1267 	if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
1268 			    (void***)&session->cmds,
1269 			    cmd_task_size + sizeof(struct iscsi_cmd_task)))
1270 		goto cmdpool_alloc_fail;
1271 
1272 	/* pre-format cmds pool with ITT */
1273 	for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1274 		struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1275 
1276 		if (cmd_task_size)
1277 			ctask->dd_data = &ctask[1];
1278 		ctask->itt = cmd_i;
1279 		INIT_LIST_HEAD(&ctask->running);
1280 	}
1281 
1282 	spin_lock_init(&session->lock);
1283 	INIT_LIST_HEAD(&session->connections);
1284 
1285 	/* initialize immediate command pool */
1286 	if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
1287 			   (void***)&session->mgmt_cmds,
1288 			   mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
1289 		goto mgmtpool_alloc_fail;
1290 
1291 
1292 	/* pre-format immediate cmds pool with ITT */
1293 	for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1294 		struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1295 
1296 		if (mgmt_task_size)
1297 			mtask->dd_data = &mtask[1];
1298 		mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
1299 		INIT_LIST_HEAD(&mtask->running);
1300 	}
1301 
1302 	if (scsi_add_host(shost, NULL))
1303 		goto add_host_fail;
1304 
1305 	if (!try_module_get(iscsit->owner))
1306 		goto cls_session_fail;
1307 
1308 	cls_session = iscsi_create_session(shost, iscsit, 0);
1309 	if (!cls_session)
1310 		goto module_put;
1311 	*(unsigned long*)shost->hostdata = (unsigned long)cls_session;
1312 
1313 	return cls_session;
1314 
1315 module_put:
1316 	module_put(iscsit->owner);
1317 cls_session_fail:
1318 	scsi_remove_host(shost);
1319 add_host_fail:
1320 	iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1321 mgmtpool_alloc_fail:
1322 	iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1323 cmdpool_alloc_fail:
1324 	scsi_host_put(shost);
1325 	return NULL;
1326 }
1327 EXPORT_SYMBOL_GPL(iscsi_session_setup);
1328 
1329 /**
1330  * iscsi_session_teardown - destroy session, host, and cls_session
1331  * shost: scsi host
1332  *
1333  * This can be used by software iscsi_transports that allocate
1334  * a session per scsi host.
1335  **/
1336 void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
1337 {
1338 	struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1339 	struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
1340 	struct module *owner = cls_session->transport->owner;
1341 
1342 	scsi_remove_host(shost);
1343 
1344 	iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1345 	iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1346 
1347 	iscsi_destroy_session(cls_session);
1348 	scsi_host_put(shost);
1349 	module_put(owner);
1350 }
1351 EXPORT_SYMBOL_GPL(iscsi_session_teardown);
1352 
1353 /**
1354  * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1355  * @cls_session: iscsi_cls_session
1356  * @conn_idx: cid
1357  **/
1358 struct iscsi_cls_conn *
1359 iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1360 {
1361 	struct iscsi_session *session = class_to_transport_session(cls_session);
1362 	struct iscsi_conn *conn;
1363 	struct iscsi_cls_conn *cls_conn;
1364 	char *data;
1365 
1366 	cls_conn = iscsi_create_conn(cls_session, conn_idx);
1367 	if (!cls_conn)
1368 		return NULL;
1369 	conn = cls_conn->dd_data;
1370 	memset(conn, 0, sizeof(*conn));
1371 
1372 	conn->session = session;
1373 	conn->cls_conn = cls_conn;
1374 	conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
1375 	conn->id = conn_idx;
1376 	conn->exp_statsn = 0;
1377 	conn->tmabort_state = TMABORT_INITIAL;
1378 	INIT_LIST_HEAD(&conn->run_list);
1379 	INIT_LIST_HEAD(&conn->mgmt_run_list);
1380 	INIT_LIST_HEAD(&conn->xmitqueue);
1381 
1382 	/* initialize general immediate & non-immediate PDU commands queue */
1383 	conn->immqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
1384 			                GFP_KERNEL, NULL);
1385 	if (conn->immqueue == ERR_PTR(-ENOMEM))
1386 		goto immqueue_alloc_fail;
1387 
1388 	conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
1389 			                GFP_KERNEL, NULL);
1390 	if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
1391 		goto mgmtqueue_alloc_fail;
1392 
1393 	INIT_WORK(&conn->xmitwork, iscsi_xmitworker, conn);
1394 
1395 	/* allocate login_mtask used for the login/text sequences */
1396 	spin_lock_bh(&session->lock);
1397 	if (!__kfifo_get(session->mgmtpool.queue,
1398                          (void*)&conn->login_mtask,
1399 			 sizeof(void*))) {
1400 		spin_unlock_bh(&session->lock);
1401 		goto login_mtask_alloc_fail;
1402 	}
1403 	spin_unlock_bh(&session->lock);
1404 
1405 	data = kmalloc(DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH, GFP_KERNEL);
1406 	if (!data)
1407 		goto login_mtask_data_alloc_fail;
1408 	conn->login_mtask->data = data;
1409 
1410 	init_timer(&conn->tmabort_timer);
1411 	mutex_init(&conn->xmitmutex);
1412 	init_waitqueue_head(&conn->ehwait);
1413 
1414 	return cls_conn;
1415 
1416 login_mtask_data_alloc_fail:
1417 	__kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1418 		    sizeof(void*));
1419 login_mtask_alloc_fail:
1420 	kfifo_free(conn->mgmtqueue);
1421 mgmtqueue_alloc_fail:
1422 	kfifo_free(conn->immqueue);
1423 immqueue_alloc_fail:
1424 	iscsi_destroy_conn(cls_conn);
1425 	return NULL;
1426 }
1427 EXPORT_SYMBOL_GPL(iscsi_conn_setup);
1428 
1429 /**
1430  * iscsi_conn_teardown - teardown iscsi connection
1431  * cls_conn: iscsi class connection
1432  *
1433  * TODO: we may need to make this into a two step process
1434  * like scsi-mls remove + put host
1435  */
1436 void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
1437 {
1438 	struct iscsi_conn *conn = cls_conn->dd_data;
1439 	struct iscsi_session *session = conn->session;
1440 	unsigned long flags;
1441 
1442 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1443 	mutex_lock(&conn->xmitmutex);
1444 
1445 	spin_lock_bh(&session->lock);
1446 	conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
1447 	if (session->leadconn == conn) {
1448 		/*
1449 		 * leading connection? then give up on recovery.
1450 		 */
1451 		session->state = ISCSI_STATE_TERMINATE;
1452 		wake_up(&conn->ehwait);
1453 	}
1454 	spin_unlock_bh(&session->lock);
1455 
1456 	mutex_unlock(&conn->xmitmutex);
1457 
1458 	/*
1459 	 * Block until all in-progress commands for this connection
1460 	 * time out or fail.
1461 	 */
1462 	for (;;) {
1463 		spin_lock_irqsave(session->host->host_lock, flags);
1464 		if (!session->host->host_busy) { /* OK for ERL == 0 */
1465 			spin_unlock_irqrestore(session->host->host_lock, flags);
1466 			break;
1467 		}
1468 		spin_unlock_irqrestore(session->host->host_lock, flags);
1469 		msleep_interruptible(500);
1470 		printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
1471 		       "host_failed %d\n", session->host->host_busy,
1472 		       session->host->host_failed);
1473 		/*
1474 		 * force eh_abort() to unblock
1475 		 */
1476 		wake_up(&conn->ehwait);
1477 	}
1478 
1479 	spin_lock_bh(&session->lock);
1480 	kfree(conn->login_mtask->data);
1481 	__kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1482 		    sizeof(void*));
1483 	list_del(&conn->item);
1484 	if (list_empty(&session->connections))
1485 		session->leadconn = NULL;
1486 	if (session->leadconn && session->leadconn == conn)
1487 		session->leadconn = container_of(session->connections.next,
1488 			struct iscsi_conn, item);
1489 
1490 	if (session->leadconn == NULL)
1491 		/* no connections exits.. reset sequencing */
1492 		session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1;
1493 	spin_unlock_bh(&session->lock);
1494 
1495 	kfifo_free(conn->immqueue);
1496 	kfifo_free(conn->mgmtqueue);
1497 
1498 	iscsi_destroy_conn(cls_conn);
1499 }
1500 EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
1501 
1502 int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
1503 {
1504 	struct iscsi_conn *conn = cls_conn->dd_data;
1505 	struct iscsi_session *session = conn->session;
1506 
1507 	if (session == NULL) {
1508 		printk(KERN_ERR "iscsi: can't start unbound connection\n");
1509 		return -EPERM;
1510 	}
1511 
1512 	spin_lock_bh(&session->lock);
1513 	conn->c_stage = ISCSI_CONN_STARTED;
1514 	session->state = ISCSI_STATE_LOGGED_IN;
1515 
1516 	switch(conn->stop_stage) {
1517 	case STOP_CONN_RECOVER:
1518 		/*
1519 		 * unblock eh_abort() if it is blocked. re-try all
1520 		 * commands after successful recovery
1521 		 */
1522 		conn->stop_stage = 0;
1523 		conn->tmabort_state = TMABORT_INITIAL;
1524 		session->age++;
1525 		spin_unlock_bh(&session->lock);
1526 
1527 		iscsi_unblock_session(session_to_cls(session));
1528 		wake_up(&conn->ehwait);
1529 		return 0;
1530 	case STOP_CONN_TERM:
1531 		conn->stop_stage = 0;
1532 		break;
1533 	default:
1534 		break;
1535 	}
1536 	spin_unlock_bh(&session->lock);
1537 
1538 	return 0;
1539 }
1540 EXPORT_SYMBOL_GPL(iscsi_conn_start);
1541 
1542 static void
1543 flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
1544 {
1545 	struct iscsi_mgmt_task *mtask, *tmp;
1546 
1547 	/* handle pending */
1548 	while (__kfifo_get(conn->immqueue, (void*)&mtask, sizeof(void*)) ||
1549 	       __kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) {
1550 		if (mtask == conn->login_mtask)
1551 			continue;
1552 		debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
1553 		__kfifo_put(session->mgmtpool.queue, (void*)&mtask,
1554 			    sizeof(void*));
1555 	}
1556 
1557 	/* handle running */
1558 	list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
1559 		debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
1560 		list_del(&mtask->running);
1561 
1562 		if (mtask == conn->login_mtask)
1563 			continue;
1564 		__kfifo_put(session->mgmtpool.queue, (void*)&mtask,
1565 			   sizeof(void*));
1566 	}
1567 
1568 	conn->mtask = NULL;
1569 }
1570 
1571 /* Fail commands. Mutex and session lock held and recv side suspended */
1572 static void fail_all_commands(struct iscsi_conn *conn)
1573 {
1574 	struct iscsi_cmd_task *ctask, *tmp;
1575 
1576 	/* flush pending */
1577 	list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
1578 		debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc,
1579 			   ctask->itt);
1580 		fail_command(conn, ctask, DID_BUS_BUSY << 16);
1581 	}
1582 
1583 	/* fail all other running */
1584 	list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
1585 		debug_scsi("failing in progress sc %p itt 0x%x\n",
1586 			   ctask->sc, ctask->itt);
1587 		fail_command(conn, ctask, DID_BUS_BUSY << 16);
1588 	}
1589 
1590 	conn->ctask = NULL;
1591 }
1592 
1593 static void iscsi_start_session_recovery(struct iscsi_session *session,
1594 					 struct iscsi_conn *conn, int flag)
1595 {
1596 	int old_stop_stage;
1597 
1598 	spin_lock_bh(&session->lock);
1599 	if (conn->stop_stage == STOP_CONN_TERM) {
1600 		spin_unlock_bh(&session->lock);
1601 		return;
1602 	}
1603 
1604 	/*
1605 	 * When this is called for the in_login state, we only want to clean
1606 	 * up the login task and connection. We do not need to block and set
1607 	 * the recovery state again
1608 	 */
1609 	if (flag == STOP_CONN_TERM)
1610 		session->state = ISCSI_STATE_TERMINATE;
1611 	else if (conn->stop_stage != STOP_CONN_RECOVER)
1612 		session->state = ISCSI_STATE_IN_RECOVERY;
1613 
1614 	old_stop_stage = conn->stop_stage;
1615 	conn->stop_stage = flag;
1616 	conn->c_stage = ISCSI_CONN_STOPPED;
1617 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1618 	spin_unlock_bh(&session->lock);
1619 
1620 	write_lock_bh(conn->recv_lock);
1621 	set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1622 	write_unlock_bh(conn->recv_lock);
1623 
1624 	mutex_lock(&conn->xmitmutex);
1625 	/*
1626 	 * for connection level recovery we should not calculate
1627 	 * header digest. conn->hdr_size used for optimization
1628 	 * in hdr_extract() and will be re-negotiated at
1629 	 * set_param() time.
1630 	 */
1631 	if (flag == STOP_CONN_RECOVER) {
1632 		conn->hdrdgst_en = 0;
1633 		conn->datadgst_en = 0;
1634 		if (session->state == ISCSI_STATE_IN_RECOVERY &&
1635 		    old_stop_stage != STOP_CONN_RECOVER) {
1636 			debug_scsi("blocking session\n");
1637 			iscsi_block_session(session_to_cls(session));
1638 		}
1639 	}
1640 
1641 	/*
1642 	 * flush queues.
1643 	 */
1644 	spin_lock_bh(&session->lock);
1645 	fail_all_commands(conn);
1646 	flush_control_queues(session, conn);
1647 	spin_unlock_bh(&session->lock);
1648 
1649 	mutex_unlock(&conn->xmitmutex);
1650 }
1651 
1652 void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1653 {
1654 	struct iscsi_conn *conn = cls_conn->dd_data;
1655 	struct iscsi_session *session = conn->session;
1656 
1657 	switch (flag) {
1658 	case STOP_CONN_RECOVER:
1659 	case STOP_CONN_TERM:
1660 		iscsi_start_session_recovery(session, conn, flag);
1661 		break;
1662 	default:
1663 		printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
1664 	}
1665 }
1666 EXPORT_SYMBOL_GPL(iscsi_conn_stop);
1667 
1668 int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
1669 		    struct iscsi_cls_conn *cls_conn, int is_leading)
1670 {
1671 	struct iscsi_session *session = class_to_transport_session(cls_session);
1672 	struct iscsi_conn *tmp = ERR_PTR(-EEXIST), *conn = cls_conn->dd_data;
1673 
1674 	/* lookup for existing connection */
1675 	spin_lock_bh(&session->lock);
1676 	list_for_each_entry(tmp, &session->connections, item) {
1677 		if (tmp == conn) {
1678 			if (conn->c_stage != ISCSI_CONN_STOPPED ||
1679 			    conn->stop_stage == STOP_CONN_TERM) {
1680 				printk(KERN_ERR "iscsi: can't bind "
1681 				       "non-stopped connection (%d:%d)\n",
1682 				       conn->c_stage, conn->stop_stage);
1683 				spin_unlock_bh(&session->lock);
1684 				return -EIO;
1685 			}
1686 			break;
1687 		}
1688 	}
1689 	if (tmp != conn) {
1690 		/* bind new iSCSI connection to session */
1691 		conn->session = session;
1692 		list_add(&conn->item, &session->connections);
1693 	}
1694 	spin_unlock_bh(&session->lock);
1695 
1696 	if (is_leading)
1697 		session->leadconn = conn;
1698 
1699 	/*
1700 	 * Unblock xmitworker(), Login Phase will pass through.
1701 	 */
1702 	clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1703 	clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1704 	return 0;
1705 }
1706 EXPORT_SYMBOL_GPL(iscsi_conn_bind);
1707 
1708 
1709 int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
1710 		    enum iscsi_param param, char *buf, int buflen)
1711 {
1712 	struct iscsi_conn *conn = cls_conn->dd_data;
1713 	struct iscsi_session *session = conn->session;
1714 	uint32_t value;
1715 
1716 	switch(param) {
1717 	case ISCSI_PARAM_MAX_RECV_DLENGTH:
1718 		sscanf(buf, "%d", &conn->max_recv_dlength);
1719 		break;
1720 	case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1721 		sscanf(buf, "%d", &conn->max_xmit_dlength);
1722 		break;
1723 	case ISCSI_PARAM_HDRDGST_EN:
1724 		sscanf(buf, "%d", &conn->hdrdgst_en);
1725 		break;
1726 	case ISCSI_PARAM_DATADGST_EN:
1727 		sscanf(buf, "%d", &conn->datadgst_en);
1728 		break;
1729 	case ISCSI_PARAM_INITIAL_R2T_EN:
1730 		sscanf(buf, "%d", &session->initial_r2t_en);
1731 		break;
1732 	case ISCSI_PARAM_MAX_R2T:
1733 		sscanf(buf, "%d", &session->max_r2t);
1734 		break;
1735 	case ISCSI_PARAM_IMM_DATA_EN:
1736 		sscanf(buf, "%d", &session->imm_data_en);
1737 		break;
1738 	case ISCSI_PARAM_FIRST_BURST:
1739 		sscanf(buf, "%d", &session->first_burst);
1740 		break;
1741 	case ISCSI_PARAM_MAX_BURST:
1742 		sscanf(buf, "%d", &session->max_burst);
1743 		break;
1744 	case ISCSI_PARAM_PDU_INORDER_EN:
1745 		sscanf(buf, "%d", &session->pdu_inorder_en);
1746 		break;
1747 	case ISCSI_PARAM_DATASEQ_INORDER_EN:
1748 		sscanf(buf, "%d", &session->dataseq_inorder_en);
1749 		break;
1750 	case ISCSI_PARAM_ERL:
1751 		sscanf(buf, "%d", &session->erl);
1752 		break;
1753 	case ISCSI_PARAM_IFMARKER_EN:
1754 		sscanf(buf, "%d", &value);
1755 		BUG_ON(value);
1756 		break;
1757 	case ISCSI_PARAM_OFMARKER_EN:
1758 		sscanf(buf, "%d", &value);
1759 		BUG_ON(value);
1760 		break;
1761 	case ISCSI_PARAM_EXP_STATSN:
1762 		sscanf(buf, "%u", &conn->exp_statsn);
1763 		break;
1764 	case ISCSI_PARAM_TARGET_NAME:
1765 		/* this should not change between logins */
1766 		if (session->targetname)
1767 			break;
1768 
1769 		session->targetname = kstrdup(buf, GFP_KERNEL);
1770 		if (!session->targetname)
1771 			return -ENOMEM;
1772 		break;
1773 	case ISCSI_PARAM_TPGT:
1774 		sscanf(buf, "%d", &session->tpgt);
1775 		break;
1776 	case ISCSI_PARAM_PERSISTENT_PORT:
1777 		sscanf(buf, "%d", &conn->persistent_port);
1778 		break;
1779 	case ISCSI_PARAM_PERSISTENT_ADDRESS:
1780 		/*
1781 		 * this is the address returned in discovery so it should
1782 		 * not change between logins.
1783 		 */
1784 		if (conn->persistent_address)
1785 			break;
1786 
1787 		conn->persistent_address = kstrdup(buf, GFP_KERNEL);
1788 		if (!conn->persistent_address)
1789 			return -ENOMEM;
1790 		break;
1791 	default:
1792 		return -ENOSYS;
1793 	}
1794 
1795 	return 0;
1796 }
1797 EXPORT_SYMBOL_GPL(iscsi_set_param);
1798 
1799 int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
1800 			    enum iscsi_param param, char *buf)
1801 {
1802 	struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1803 	struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
1804 	int len;
1805 
1806 	switch(param) {
1807 	case ISCSI_PARAM_INITIAL_R2T_EN:
1808 		len = sprintf(buf, "%d\n", session->initial_r2t_en);
1809 		break;
1810 	case ISCSI_PARAM_MAX_R2T:
1811 		len = sprintf(buf, "%hu\n", session->max_r2t);
1812 		break;
1813 	case ISCSI_PARAM_IMM_DATA_EN:
1814 		len = sprintf(buf, "%d\n", session->imm_data_en);
1815 		break;
1816 	case ISCSI_PARAM_FIRST_BURST:
1817 		len = sprintf(buf, "%u\n", session->first_burst);
1818 		break;
1819 	case ISCSI_PARAM_MAX_BURST:
1820 		len = sprintf(buf, "%u\n", session->max_burst);
1821 		break;
1822 	case ISCSI_PARAM_PDU_INORDER_EN:
1823 		len = sprintf(buf, "%d\n", session->pdu_inorder_en);
1824 		break;
1825 	case ISCSI_PARAM_DATASEQ_INORDER_EN:
1826 		len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
1827 		break;
1828 	case ISCSI_PARAM_ERL:
1829 		len = sprintf(buf, "%d\n", session->erl);
1830 		break;
1831 	case ISCSI_PARAM_TARGET_NAME:
1832 		len = sprintf(buf, "%s\n", session->targetname);
1833 		break;
1834 	case ISCSI_PARAM_TPGT:
1835 		len = sprintf(buf, "%d\n", session->tpgt);
1836 		break;
1837 	default:
1838 		return -ENOSYS;
1839 	}
1840 
1841 	return len;
1842 }
1843 EXPORT_SYMBOL_GPL(iscsi_session_get_param);
1844 
1845 int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
1846 			 enum iscsi_param param, char *buf)
1847 {
1848 	struct iscsi_conn *conn = cls_conn->dd_data;
1849 	int len;
1850 
1851 	switch(param) {
1852 	case ISCSI_PARAM_MAX_RECV_DLENGTH:
1853 		len = sprintf(buf, "%u\n", conn->max_recv_dlength);
1854 		break;
1855 	case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1856 		len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
1857 		break;
1858 	case ISCSI_PARAM_HDRDGST_EN:
1859 		len = sprintf(buf, "%d\n", conn->hdrdgst_en);
1860 		break;
1861 	case ISCSI_PARAM_DATADGST_EN:
1862 		len = sprintf(buf, "%d\n", conn->datadgst_en);
1863 		break;
1864 	case ISCSI_PARAM_IFMARKER_EN:
1865 		len = sprintf(buf, "%d\n", conn->ifmarker_en);
1866 		break;
1867 	case ISCSI_PARAM_OFMARKER_EN:
1868 		len = sprintf(buf, "%d\n", conn->ofmarker_en);
1869 		break;
1870 	case ISCSI_PARAM_EXP_STATSN:
1871 		len = sprintf(buf, "%u\n", conn->exp_statsn);
1872 		break;
1873 	case ISCSI_PARAM_PERSISTENT_PORT:
1874 		len = sprintf(buf, "%d\n", conn->persistent_port);
1875 		break;
1876 	case ISCSI_PARAM_PERSISTENT_ADDRESS:
1877 		len = sprintf(buf, "%s\n", conn->persistent_address);
1878 		break;
1879 	default:
1880 		return -ENOSYS;
1881 	}
1882 
1883 	return len;
1884 }
1885 EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
1886 
1887 MODULE_AUTHOR("Mike Christie");
1888 MODULE_DESCRIPTION("iSCSI library functions");
1889 MODULE_LICENSE("GPL");
1890