xref: /linux/drivers/infiniband/ulp/iser/iscsi_iser.c (revision 5e8d780d745c1619aba81fe7166c5a4b5cad2b84)
1 /*
2  * iSCSI Initiator over iSER Data-Path
3  *
4  * Copyright (C) 2004 Dmitry Yusupov
5  * Copyright (C) 2004 Alex Aizman
6  * Copyright (C) 2005 Mike Christie
7  * Copyright (c) 2005, 2006 Voltaire, Inc. All rights reserved.
8  * maintained by openib-general@openib.org
9  *
10  * This software is available to you under a choice of one of two
11  * licenses.  You may choose to be licensed under the terms of the GNU
12  * General Public License (GPL) Version 2, available from the file
13  * COPYING in the main directory of this source tree, or the
14  * OpenIB.org BSD license below:
15  *
16  *     Redistribution and use in source and binary forms, with or
17  *     without modification, are permitted provided that the following
18  *     conditions are met:
19  *
20  *	- Redistributions of source code must retain the above
21  *	  copyright notice, this list of conditions and the following
22  *	  disclaimer.
23  *
24  *	- Redistributions in binary form must reproduce the above
25  *	  copyright notice, this list of conditions and the following
26  *	  disclaimer in the documentation and/or other materials
27  *	  provided with the distribution.
28  *
29  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
33  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
34  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
35  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36  * SOFTWARE.
37  *
38  * Credits:
39  *	Christoph Hellwig
40  *	FUJITA Tomonori
41  *	Arne Redlich
42  *	Zhenyu Wang
43  * Modified by:
44  *      Erez Zilber
45  *
46  *
47  * $Id: iscsi_iser.c 6965 2006-05-07 11:36:20Z ogerlitz $
48  */
49 
50 #include <linux/types.h>
51 #include <linux/list.h>
52 #include <linux/hardirq.h>
53 #include <linux/kfifo.h>
54 #include <linux/blkdev.h>
55 #include <linux/init.h>
56 #include <linux/ioctl.h>
57 #include <linux/cdev.h>
58 #include <linux/in.h>
59 #include <linux/net.h>
60 #include <linux/scatterlist.h>
61 #include <linux/delay.h>
62 
63 #include <net/sock.h>
64 
65 #include <asm/uaccess.h>
66 
67 #include <scsi/scsi_cmnd.h>
68 #include <scsi/scsi_device.h>
69 #include <scsi/scsi_eh.h>
70 #include <scsi/scsi_tcq.h>
71 #include <scsi/scsi_host.h>
72 #include <scsi/scsi.h>
73 #include <scsi/scsi_transport_iscsi.h>
74 
75 #include "iscsi_iser.h"
76 
77 static unsigned int iscsi_max_lun = 512;
78 module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
79 
80 int iser_debug_level = 0;
81 
82 MODULE_DESCRIPTION("iSER (iSCSI Extensions for RDMA) Datamover "
83 		   "v" DRV_VER " (" DRV_DATE ")");
84 MODULE_LICENSE("Dual BSD/GPL");
85 MODULE_AUTHOR("Alex Nezhinsky, Dan Bar Dov, Or Gerlitz");
86 
87 module_param_named(debug_level, iser_debug_level, int, 0644);
88 MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0 (default:disabled)");
89 
90 struct iser_global ig;
91 
92 void
93 iscsi_iser_recv(struct iscsi_conn *conn,
94 		struct iscsi_hdr *hdr, char *rx_data, int rx_data_len)
95 {
96 	int rc = 0;
97 	uint32_t ret_itt;
98 	int datalen;
99 	int ahslen;
100 
101 	/* verify PDU length */
102 	datalen = ntoh24(hdr->dlength);
103 	if (datalen != rx_data_len) {
104 		printk(KERN_ERR "iscsi_iser: datalen %d (hdr) != %d (IB) \n",
105 		       datalen, rx_data_len);
106 		rc = ISCSI_ERR_DATALEN;
107 		goto error;
108 	}
109 
110 	/* read AHS */
111 	ahslen = hdr->hlength * 4;
112 
113 	/* verify itt (itt encoding: age+cid+itt) */
114 	rc = iscsi_verify_itt(conn, hdr, &ret_itt);
115 
116 	if (!rc)
117 		rc = iscsi_complete_pdu(conn, hdr, rx_data, rx_data_len);
118 
119 	if (rc && rc != ISCSI_ERR_NO_SCSI_CMD)
120 		goto error;
121 
122 	return;
123 error:
124 	iscsi_conn_failure(conn, rc);
125 }
126 
127 
128 /**
129  * iscsi_iser_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
130  *
131  **/
132 static void
133 iscsi_iser_cmd_init(struct iscsi_cmd_task *ctask)
134 {
135 	struct iscsi_iser_conn     *iser_conn  = ctask->conn->dd_data;
136 	struct iscsi_iser_cmd_task *iser_ctask = ctask->dd_data;
137 	struct scsi_cmnd  *sc = ctask->sc;
138 
139 	iser_ctask->command_sent = 0;
140 	iser_ctask->iser_conn    = iser_conn;
141 
142 	if (sc->sc_data_direction == DMA_TO_DEVICE) {
143 		BUG_ON(ctask->total_length == 0);
144 		/* bytes to be sent via RDMA operations */
145 		iser_ctask->rdma_data_count = ctask->total_length -
146 					 ctask->imm_count -
147 					 ctask->unsol_count;
148 
149 		debug_scsi("cmd [itt %x total %d imm %d unsol_data %d "
150 			   "rdma_data %d]\n",
151 			   ctask->itt, ctask->total_length, ctask->imm_count,
152 			   ctask->unsol_count, iser_ctask->rdma_data_count);
153 	} else
154 		/* bytes to be sent via RDMA operations */
155 		iser_ctask->rdma_data_count = ctask->total_length;
156 
157 	iser_ctask_rdma_init(iser_ctask);
158 }
159 
160 /**
161  * iscsi_mtask_xmit - xmit management(immediate) task
162  * @conn: iscsi connection
163  * @mtask: task management task
164  *
165  * Notes:
166  *	The function can return -EAGAIN in which case caller must
167  *	call it again later, or recover. '0' return code means successful
168  *	xmit.
169  *
170  **/
171 static int
172 iscsi_iser_mtask_xmit(struct iscsi_conn *conn,
173 		      struct iscsi_mgmt_task *mtask)
174 {
175 	int error = 0;
176 
177 	debug_scsi("mtask deq [cid %d itt 0x%x]\n", conn->id, mtask->itt);
178 
179 	error = iser_send_control(conn, mtask);
180 
181 	/* since iser xmits control with zero copy, mtasks can not be recycled
182 	 * right after sending them.
183 	 * The recycling scheme is based on whether a response is expected
184 	 * - if yes, the mtask is recycled at iscsi_complete_pdu
185 	 * - if no,  the mtask is recycled at iser_snd_completion
186 	 */
187 	if (error && error != -EAGAIN)
188 		iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
189 
190 	return error;
191 }
192 
193 static int
194 iscsi_iser_ctask_xmit_unsol_data(struct iscsi_conn *conn,
195 				 struct iscsi_cmd_task *ctask)
196 {
197 	struct iscsi_data  hdr;
198 	int error = 0;
199 	struct iscsi_iser_cmd_task *iser_ctask = ctask->dd_data;
200 
201 	/* Send data-out PDUs while there's still unsolicited data to send */
202 	while (ctask->unsol_count > 0) {
203 		iscsi_prep_unsolicit_data_pdu(ctask, &hdr,
204 					      iser_ctask->rdma_data_count);
205 
206 		debug_scsi("Sending data-out: itt 0x%x, data count %d\n",
207 			   hdr.itt, ctask->data_count);
208 
209 		/* the buffer description has been passed with the command */
210 		/* Send the command */
211 		error = iser_send_data_out(conn, ctask, &hdr);
212 		if (error) {
213 			ctask->unsol_datasn--;
214 			goto iscsi_iser_ctask_xmit_unsol_data_exit;
215 		}
216 		ctask->unsol_count -= ctask->data_count;
217 		debug_scsi("Need to send %d more as data-out PDUs\n",
218 			   ctask->unsol_count);
219 	}
220 
221 iscsi_iser_ctask_xmit_unsol_data_exit:
222 	return error;
223 }
224 
225 static int
226 iscsi_iser_ctask_xmit(struct iscsi_conn *conn,
227 		      struct iscsi_cmd_task *ctask)
228 {
229 	struct iscsi_iser_cmd_task *iser_ctask = ctask->dd_data;
230 	int error = 0;
231 
232 	debug_scsi("ctask deq [cid %d itt 0x%x]\n",
233 		   conn->id, ctask->itt);
234 
235 	/*
236 	 * serialize with TMF AbortTask
237 	 */
238 	if (ctask->mtask)
239 		return error;
240 
241 	/* Send the cmd PDU */
242 	if (!iser_ctask->command_sent) {
243 		error = iser_send_command(conn, ctask);
244 		if (error)
245 			goto iscsi_iser_ctask_xmit_exit;
246 		iser_ctask->command_sent = 1;
247 	}
248 
249 	/* Send unsolicited data-out PDU(s) if necessary */
250 	if (ctask->unsol_count)
251 		error = iscsi_iser_ctask_xmit_unsol_data(conn, ctask);
252 
253  iscsi_iser_ctask_xmit_exit:
254 	if (error && error != -EAGAIN)
255 		iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
256 	return error;
257 }
258 
259 static void
260 iscsi_iser_cleanup_ctask(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
261 {
262 	struct iscsi_iser_cmd_task *iser_ctask = ctask->dd_data;
263 
264 	if (iser_ctask->status == ISER_TASK_STATUS_STARTED) {
265 		iser_ctask->status = ISER_TASK_STATUS_COMPLETED;
266 		iser_ctask_rdma_finalize(iser_ctask);
267 	}
268 }
269 
270 static struct iser_conn *
271 iscsi_iser_ib_conn_lookup(__u64 ep_handle)
272 {
273 	struct iser_conn *ib_conn;
274 	struct iser_conn *uib_conn = (struct iser_conn *)(unsigned long)ep_handle;
275 
276 	mutex_lock(&ig.connlist_mutex);
277 	list_for_each_entry(ib_conn, &ig.connlist, conn_list) {
278 		if (ib_conn == uib_conn) {
279 			mutex_unlock(&ig.connlist_mutex);
280 			return ib_conn;
281 		}
282 	}
283 	mutex_unlock(&ig.connlist_mutex);
284 	iser_err("no conn exists for eph %llx\n",(unsigned long long)ep_handle);
285 	return NULL;
286 }
287 
288 static struct iscsi_cls_conn *
289 iscsi_iser_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
290 {
291 	struct iscsi_conn *conn;
292 	struct iscsi_cls_conn *cls_conn;
293 	struct iscsi_iser_conn *iser_conn;
294 
295 	cls_conn = iscsi_conn_setup(cls_session, conn_idx);
296 	if (!cls_conn)
297 		return NULL;
298 	conn = cls_conn->dd_data;
299 
300 	/*
301 	 * due to issues with the login code re iser sematics
302 	 * this not set in iscsi_conn_setup - FIXME
303 	 */
304 	conn->max_recv_dlength = 128;
305 
306 	iser_conn = kzalloc(sizeof(*iser_conn), GFP_KERNEL);
307 	if (!iser_conn)
308 		goto conn_alloc_fail;
309 
310 	/* currently this is the only field which need to be initiated */
311 	rwlock_init(&iser_conn->lock);
312 
313 	conn->dd_data = iser_conn;
314 	iser_conn->iscsi_conn = conn;
315 
316 	return cls_conn;
317 
318 conn_alloc_fail:
319 	iscsi_conn_teardown(cls_conn);
320 	return NULL;
321 }
322 
323 static void
324 iscsi_iser_conn_destroy(struct iscsi_cls_conn *cls_conn)
325 {
326 	struct iscsi_conn *conn = cls_conn->dd_data;
327 	struct iscsi_iser_conn *iser_conn = conn->dd_data;
328 
329 	iscsi_conn_teardown(cls_conn);
330 	kfree(iser_conn);
331 }
332 
333 static int
334 iscsi_iser_conn_bind(struct iscsi_cls_session *cls_session,
335 		     struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
336 		     int is_leading)
337 {
338 	struct iscsi_conn *conn = cls_conn->dd_data;
339 	struct iscsi_iser_conn *iser_conn;
340 	struct iser_conn *ib_conn;
341 	int error;
342 
343 	error = iscsi_conn_bind(cls_session, cls_conn, is_leading);
344 	if (error)
345 		return error;
346 
347 	/* the transport ep handle comes from user space so it must be
348 	 * verified against the global ib connections list */
349 	ib_conn = iscsi_iser_ib_conn_lookup(transport_eph);
350 	if (!ib_conn) {
351 		iser_err("can't bind eph %llx\n",
352 			 (unsigned long long)transport_eph);
353 		return -EINVAL;
354 	}
355 	/* binds the iSER connection retrieved from the previously
356 	 * connected ep_handle to the iSCSI layer connection. exchanges
357 	 * connection pointers */
358 	iser_err("binding iscsi conn %p to iser_conn %p\n",conn,ib_conn);
359 	iser_conn = conn->dd_data;
360 	ib_conn->iser_conn = iser_conn;
361 	iser_conn->ib_conn  = ib_conn;
362 
363 	conn->recv_lock = &iser_conn->lock;
364 
365 	return 0;
366 }
367 
368 static int
369 iscsi_iser_conn_start(struct iscsi_cls_conn *cls_conn)
370 {
371 	struct iscsi_conn *conn = cls_conn->dd_data;
372 	int err;
373 
374 	err = iscsi_conn_start(cls_conn);
375 	if (err)
376 		return err;
377 
378 	return iser_conn_set_full_featured_mode(conn);
379 }
380 
381 static void
382 iscsi_iser_conn_terminate(struct iscsi_conn *conn)
383 {
384 	struct iscsi_iser_conn *iser_conn = conn->dd_data;
385 	struct iser_conn *ib_conn = iser_conn->ib_conn;
386 
387 	BUG_ON(!ib_conn);
388 	/* starts conn teardown process, waits until all previously   *
389 	 * posted buffers get flushed, deallocates all conn resources */
390 	iser_conn_terminate(ib_conn);
391 	iser_conn->ib_conn = NULL;
392 	conn->recv_lock = NULL;
393 }
394 
395 
396 static struct iscsi_transport iscsi_iser_transport;
397 
398 static struct iscsi_cls_session *
399 iscsi_iser_session_create(struct iscsi_transport *iscsit,
400 			 struct scsi_transport_template *scsit,
401 			  uint32_t initial_cmdsn, uint32_t *hostno)
402 {
403 	struct iscsi_cls_session *cls_session;
404 	struct iscsi_session *session;
405 	int i;
406 	uint32_t hn;
407 	struct iscsi_cmd_task  *ctask;
408 	struct iscsi_mgmt_task *mtask;
409 	struct iscsi_iser_cmd_task *iser_ctask;
410 	struct iser_desc *desc;
411 
412 	cls_session = iscsi_session_setup(iscsit, scsit,
413 					  sizeof(struct iscsi_iser_cmd_task),
414 					  sizeof(struct iser_desc),
415 					  initial_cmdsn, &hn);
416 	if (!cls_session)
417 	return NULL;
418 
419 	*hostno = hn;
420 	session = class_to_transport_session(cls_session);
421 
422 	/* libiscsi setup itts, data and pool so just set desc fields */
423 	for (i = 0; i < session->cmds_max; i++) {
424 		ctask      = session->cmds[i];
425 		iser_ctask = ctask->dd_data;
426 		ctask->hdr = (struct iscsi_cmd *)&iser_ctask->desc.iscsi_header;
427 	}
428 
429 	for (i = 0; i < session->mgmtpool_max; i++) {
430 		mtask      = session->mgmt_cmds[i];
431 		desc       = mtask->dd_data;
432 		mtask->hdr = &desc->iscsi_header;
433 		desc->data = mtask->data;
434 	}
435 
436 	return cls_session;
437 }
438 
439 static int
440 iscsi_iser_conn_set_param(struct iscsi_cls_conn *cls_conn,
441 			  enum iscsi_param param, uint32_t value)
442 {
443 	struct iscsi_conn *conn = cls_conn->dd_data;
444 	struct iscsi_session *session = conn->session;
445 
446 	spin_lock_bh(&session->lock);
447 	if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE &&
448 	    conn->stop_stage != STOP_CONN_RECOVER) {
449 		printk(KERN_ERR "iscsi_iser: can not change parameter [%d]\n",
450 		       param);
451 		spin_unlock_bh(&session->lock);
452 		return 0;
453 	}
454 	spin_unlock_bh(&session->lock);
455 
456 	switch (param) {
457 	case ISCSI_PARAM_MAX_RECV_DLENGTH:
458 		/* TBD */
459 		break;
460 	case ISCSI_PARAM_MAX_XMIT_DLENGTH:
461 		conn->max_xmit_dlength =  value;
462 		break;
463 	case ISCSI_PARAM_HDRDGST_EN:
464 		if (value) {
465 			printk(KERN_ERR "DataDigest wasn't negotiated to None");
466 			return -EPROTO;
467 		}
468 		break;
469 	case ISCSI_PARAM_DATADGST_EN:
470 		if (value) {
471 			printk(KERN_ERR "DataDigest wasn't negotiated to None");
472 			return -EPROTO;
473 		}
474 		break;
475 	case ISCSI_PARAM_INITIAL_R2T_EN:
476 		session->initial_r2t_en = value;
477 		break;
478 	case ISCSI_PARAM_IMM_DATA_EN:
479 		session->imm_data_en = value;
480 		break;
481 	case ISCSI_PARAM_FIRST_BURST:
482 		session->first_burst = value;
483 		break;
484 	case ISCSI_PARAM_MAX_BURST:
485 		session->max_burst = value;
486 		break;
487 	case ISCSI_PARAM_PDU_INORDER_EN:
488 		session->pdu_inorder_en = value;
489 		break;
490 	case ISCSI_PARAM_DATASEQ_INORDER_EN:
491 		session->dataseq_inorder_en = value;
492 		break;
493 	case ISCSI_PARAM_ERL:
494 		session->erl = value;
495 		break;
496 	case ISCSI_PARAM_IFMARKER_EN:
497 		if (value) {
498 			printk(KERN_ERR "IFMarker wasn't negotiated to No");
499 			return -EPROTO;
500 		}
501 		break;
502 	case ISCSI_PARAM_OFMARKER_EN:
503 		if (value) {
504 			printk(KERN_ERR "OFMarker wasn't negotiated to No");
505 			return -EPROTO;
506 		}
507 		break;
508 	default:
509 		break;
510 	}
511 
512 	return 0;
513 }
514 
515 static int
516 iscsi_iser_session_get_param(struct iscsi_cls_session *cls_session,
517 			     enum iscsi_param param, uint32_t *value)
518 {
519 	struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
520 	struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
521 
522 	switch (param) {
523 	case ISCSI_PARAM_INITIAL_R2T_EN:
524 		*value = session->initial_r2t_en;
525 		break;
526 	case ISCSI_PARAM_MAX_R2T:
527 		*value = session->max_r2t;
528 		break;
529 	case ISCSI_PARAM_IMM_DATA_EN:
530 		*value = session->imm_data_en;
531 		break;
532 	case ISCSI_PARAM_FIRST_BURST:
533 		*value = session->first_burst;
534 		break;
535 	case ISCSI_PARAM_MAX_BURST:
536 		*value = session->max_burst;
537 		break;
538 	case ISCSI_PARAM_PDU_INORDER_EN:
539 		*value = session->pdu_inorder_en;
540 		break;
541 	case ISCSI_PARAM_DATASEQ_INORDER_EN:
542 		*value = session->dataseq_inorder_en;
543 		break;
544 	case ISCSI_PARAM_ERL:
545 		*value = session->erl;
546 		break;
547 	case ISCSI_PARAM_IFMARKER_EN:
548 		*value = 0;
549 		break;
550 	case ISCSI_PARAM_OFMARKER_EN:
551 		*value = 0;
552 		break;
553 	default:
554 		return ISCSI_ERR_PARAM_NOT_FOUND;
555 	}
556 
557 	return 0;
558 }
559 
560 static int
561 iscsi_iser_conn_get_param(struct iscsi_cls_conn *cls_conn,
562 			  enum iscsi_param param, uint32_t *value)
563 {
564 	struct iscsi_conn *conn = cls_conn->dd_data;
565 
566 	switch(param) {
567 	case ISCSI_PARAM_MAX_RECV_DLENGTH:
568 		*value = conn->max_recv_dlength;
569 		break;
570 	case ISCSI_PARAM_MAX_XMIT_DLENGTH:
571 		*value = conn->max_xmit_dlength;
572 		break;
573 	case ISCSI_PARAM_HDRDGST_EN:
574 		*value = 0;
575 		break;
576 	case ISCSI_PARAM_DATADGST_EN:
577 		*value = 0;
578 		break;
579 	/*case ISCSI_PARAM_TARGET_RECV_DLENGTH:
580 		*value = conn->target_recv_dlength;
581 		break;
582 	case ISCSI_PARAM_INITIATOR_RECV_DLENGTH:
583 		*value = conn->initiator_recv_dlength;
584 		break;*/
585 	default:
586 		return ISCSI_ERR_PARAM_NOT_FOUND;
587 	}
588 
589 	return 0;
590 }
591 
592 
593 static void
594 iscsi_iser_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
595 {
596 	struct iscsi_conn *conn = cls_conn->dd_data;
597 
598 	stats->txdata_octets = conn->txdata_octets;
599 	stats->rxdata_octets = conn->rxdata_octets;
600 	stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
601 	stats->dataout_pdus = conn->dataout_pdus_cnt;
602 	stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
603 	stats->datain_pdus = conn->datain_pdus_cnt; /* always 0 */
604 	stats->r2t_pdus = conn->r2t_pdus_cnt; /* always 0 */
605 	stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
606 	stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
607 	stats->custom_length = 3;
608 	strcpy(stats->custom[0].desc, "qp_tx_queue_full");
609 	stats->custom[0].value = 0; /* TB iser_conn->qp_tx_queue_full; */
610 	strcpy(stats->custom[1].desc, "fmr_map_not_avail");
611 	stats->custom[1].value = 0; /* TB iser_conn->fmr_map_not_avail */;
612 	strcpy(stats->custom[2].desc, "eh_abort_cnt");
613 	stats->custom[2].value = conn->eh_abort_cnt;
614 }
615 
616 static int
617 iscsi_iser_ep_connect(struct sockaddr *dst_addr, int non_blocking,
618 		      __u64 *ep_handle)
619 {
620 	int err;
621 	struct iser_conn *ib_conn;
622 
623 	err = iser_conn_init(&ib_conn);
624 	if (err)
625 		goto out;
626 
627 	err = iser_connect(ib_conn, NULL, (struct sockaddr_in *)dst_addr, non_blocking);
628 	if (!err)
629 		*ep_handle = (__u64)(unsigned long)ib_conn;
630 
631 out:
632 	return err;
633 }
634 
635 static int
636 iscsi_iser_ep_poll(__u64 ep_handle, int timeout_ms)
637 {
638 	struct iser_conn *ib_conn = iscsi_iser_ib_conn_lookup(ep_handle);
639 	int rc;
640 
641 	if (!ib_conn)
642 		return -EINVAL;
643 
644 	rc = wait_event_interruptible_timeout(ib_conn->wait,
645 			     ib_conn->state == ISER_CONN_UP,
646 			     msecs_to_jiffies(timeout_ms));
647 
648 	/* if conn establishment failed, return error code to iscsi */
649 	if (!rc &&
650 	    (ib_conn->state == ISER_CONN_TERMINATING ||
651 	     ib_conn->state == ISER_CONN_DOWN))
652 		rc = -1;
653 
654 	iser_err("ib conn %p rc = %d\n", ib_conn, rc);
655 
656 	if (rc > 0)
657 		return 1; /* success, this is the equivalent of POLLOUT */
658 	else if (!rc)
659 		return 0; /* timeout */
660 	else
661 		return rc; /* signal */
662 }
663 
664 static void
665 iscsi_iser_ep_disconnect(__u64 ep_handle)
666 {
667 	struct iser_conn *ib_conn = iscsi_iser_ib_conn_lookup(ep_handle);
668 
669 	if (!ib_conn)
670 		return;
671 
672 	iser_err("ib conn %p state %d\n",ib_conn, ib_conn->state);
673 
674 	iser_conn_terminate(ib_conn);
675 }
676 
677 static struct scsi_host_template iscsi_iser_sht = {
678 	.name                   = "iSCSI Initiator over iSER, v." DRV_VER,
679 	.queuecommand           = iscsi_queuecommand,
680 	.can_queue		= ISCSI_XMIT_CMDS_MAX - 1,
681 	.sg_tablesize           = ISCSI_ISER_SG_TABLESIZE,
682 	.cmd_per_lun            = ISCSI_MAX_CMD_PER_LUN,
683 	.eh_abort_handler       = iscsi_eh_abort,
684 	.eh_host_reset_handler	= iscsi_eh_host_reset,
685 	.use_clustering         = DISABLE_CLUSTERING,
686 	.proc_name              = "iscsi_iser",
687 	.this_id                = -1,
688 };
689 
690 static struct iscsi_transport iscsi_iser_transport = {
691 	.owner                  = THIS_MODULE,
692 	.name                   = "iser",
693 	.caps                   = CAP_RECOVERY_L0 | CAP_MULTI_R2T,
694 	.param_mask		= ISCSI_MAX_RECV_DLENGTH |
695 				  ISCSI_MAX_XMIT_DLENGTH |
696 				  ISCSI_HDRDGST_EN |
697 				  ISCSI_DATADGST_EN |
698 				  ISCSI_INITIAL_R2T_EN |
699 				  ISCSI_MAX_R2T |
700 				  ISCSI_IMM_DATA_EN |
701 				  ISCSI_FIRST_BURST |
702 				  ISCSI_MAX_BURST |
703 				  ISCSI_PDU_INORDER_EN |
704 				  ISCSI_DATASEQ_INORDER_EN,
705 	.host_template          = &iscsi_iser_sht,
706 	.conndata_size		= sizeof(struct iscsi_conn),
707 	.max_lun                = ISCSI_ISER_MAX_LUN,
708 	.max_cmd_len            = ISCSI_ISER_MAX_CMD_LEN,
709 	/* session management */
710 	.create_session         = iscsi_iser_session_create,
711 	.destroy_session        = iscsi_session_teardown,
712 	/* connection management */
713 	.create_conn            = iscsi_iser_conn_create,
714 	.bind_conn              = iscsi_iser_conn_bind,
715 	.destroy_conn           = iscsi_iser_conn_destroy,
716 	.set_param              = iscsi_iser_conn_set_param,
717 	.get_conn_param		= iscsi_iser_conn_get_param,
718 	.get_session_param	= iscsi_iser_session_get_param,
719 	.start_conn             = iscsi_iser_conn_start,
720 	.stop_conn              = iscsi_conn_stop,
721 	/* these are called as part of conn recovery */
722 	.suspend_conn_recv	= NULL, /* FIXME is/how this relvant to iser? */
723 	.terminate_conn		= iscsi_iser_conn_terminate,
724 	/* IO */
725 	.send_pdu		= iscsi_conn_send_pdu,
726 	.get_stats		= iscsi_iser_conn_get_stats,
727 	.init_cmd_task		= iscsi_iser_cmd_init,
728 	.xmit_cmd_task		= iscsi_iser_ctask_xmit,
729 	.xmit_mgmt_task		= iscsi_iser_mtask_xmit,
730 	.cleanup_cmd_task	= iscsi_iser_cleanup_ctask,
731 	/* recovery */
732 	.session_recovery_timedout = iscsi_session_recovery_timedout,
733 
734 	.ep_connect             = iscsi_iser_ep_connect,
735 	.ep_poll                = iscsi_iser_ep_poll,
736 	.ep_disconnect          = iscsi_iser_ep_disconnect
737 };
738 
739 static int __init iser_init(void)
740 {
741 	int err;
742 
743 	iser_dbg("Starting iSER datamover...\n");
744 
745 	if (iscsi_max_lun < 1) {
746 		printk(KERN_ERR "Invalid max_lun value of %u\n", iscsi_max_lun);
747 		return -EINVAL;
748 	}
749 
750 	iscsi_iser_transport.max_lun = iscsi_max_lun;
751 
752 	memset(&ig, 0, sizeof(struct iser_global));
753 
754 	ig.desc_cache = kmem_cache_create("iser_descriptors",
755 					  sizeof (struct iser_desc),
756 					  0, SLAB_HWCACHE_ALIGN,
757 					  NULL, NULL);
758 	if (ig.desc_cache == NULL)
759 		return -ENOMEM;
760 
761 	/* device init is called only after the first addr resolution */
762 	mutex_init(&ig.device_list_mutex);
763 	INIT_LIST_HEAD(&ig.device_list);
764 	mutex_init(&ig.connlist_mutex);
765 	INIT_LIST_HEAD(&ig.connlist);
766 
767 	if (!iscsi_register_transport(&iscsi_iser_transport)) {
768 		iser_err("iscsi_register_transport failed\n");
769 		err = -EINVAL;
770 		goto register_transport_failure;
771 	}
772 
773 	return 0;
774 
775 register_transport_failure:
776 	kmem_cache_destroy(ig.desc_cache);
777 
778 	return err;
779 }
780 
781 static void __exit iser_exit(void)
782 {
783 	iser_dbg("Removing iSER datamover...\n");
784 	iscsi_unregister_transport(&iscsi_iser_transport);
785 	kmem_cache_destroy(ig.desc_cache);
786 }
787 
788 module_init(iser_init);
789 module_exit(iser_exit);
790