165e7ae7bSOr Gerlitz /*
265e7ae7bSOr Gerlitz  * iSCSI Initiator over iSER Data-Path
365e7ae7bSOr Gerlitz  *
465e7ae7bSOr Gerlitz  * Copyright (C) 2004 Dmitry Yusupov
565e7ae7bSOr Gerlitz  * Copyright (C) 2004 Alex Aizman
665e7ae7bSOr Gerlitz  * Copyright (C) 2005 Mike Christie
765e7ae7bSOr Gerlitz  * Copyright (c) 2005, 2006 Voltaire, Inc. All rights reserved.
865e7ae7bSOr Gerlitz  * maintained by openib-general@openib.org
965e7ae7bSOr Gerlitz  *
1065e7ae7bSOr Gerlitz  * This software is available to you under a choice of one of two
1165e7ae7bSOr Gerlitz  * licenses.  You may choose to be licensed under the terms of the GNU
1265e7ae7bSOr Gerlitz  * General Public License (GPL) Version 2, available from the file
1365e7ae7bSOr Gerlitz  * COPYING in the main directory of this source tree, or the
1465e7ae7bSOr Gerlitz  * OpenIB.org BSD license below:
1565e7ae7bSOr Gerlitz  *
1665e7ae7bSOr Gerlitz  *     Redistribution and use in source and binary forms, with or
1765e7ae7bSOr Gerlitz  *     without modification, are permitted provided that the following
1865e7ae7bSOr Gerlitz  *     conditions are met:
1965e7ae7bSOr Gerlitz  *
2065e7ae7bSOr Gerlitz  *	- Redistributions of source code must retain the above
2165e7ae7bSOr Gerlitz  *	  copyright notice, this list of conditions and the following
2265e7ae7bSOr Gerlitz  *	  disclaimer.
2365e7ae7bSOr Gerlitz  *
2465e7ae7bSOr Gerlitz  *	- Redistributions in binary form must reproduce the above
2565e7ae7bSOr Gerlitz  *	  copyright notice, this list of conditions and the following
2665e7ae7bSOr Gerlitz  *	  disclaimer in the documentation and/or other materials
2765e7ae7bSOr Gerlitz  *	  provided with the distribution.
2865e7ae7bSOr Gerlitz  *
2965e7ae7bSOr Gerlitz  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
3065e7ae7bSOr Gerlitz  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
3165e7ae7bSOr Gerlitz  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
3265e7ae7bSOr Gerlitz  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
3365e7ae7bSOr Gerlitz  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
3465e7ae7bSOr Gerlitz  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
3565e7ae7bSOr Gerlitz  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3665e7ae7bSOr Gerlitz  * SOFTWARE.
3765e7ae7bSOr Gerlitz  *
3865e7ae7bSOr Gerlitz  * Credits:
3965e7ae7bSOr Gerlitz  *	Christoph Hellwig
4065e7ae7bSOr Gerlitz  *	FUJITA Tomonori
4165e7ae7bSOr Gerlitz  *	Arne Redlich
4265e7ae7bSOr Gerlitz  *	Zhenyu Wang
4365e7ae7bSOr Gerlitz  * Modified by:
4465e7ae7bSOr Gerlitz  *      Erez Zilber
4565e7ae7bSOr Gerlitz  *
4665e7ae7bSOr Gerlitz  *
4765e7ae7bSOr Gerlitz  * $Id: iscsi_iser.c 6965 2006-05-07 11:36:20Z ogerlitz $
4865e7ae7bSOr Gerlitz  */
4965e7ae7bSOr Gerlitz 
5065e7ae7bSOr Gerlitz #include <linux/types.h>
5165e7ae7bSOr Gerlitz #include <linux/list.h>
5265e7ae7bSOr Gerlitz #include <linux/hardirq.h>
5365e7ae7bSOr Gerlitz #include <linux/kfifo.h>
5465e7ae7bSOr Gerlitz #include <linux/blkdev.h>
5565e7ae7bSOr Gerlitz #include <linux/init.h>
5665e7ae7bSOr Gerlitz #include <linux/ioctl.h>
5765e7ae7bSOr Gerlitz #include <linux/cdev.h>
5865e7ae7bSOr Gerlitz #include <linux/in.h>
5965e7ae7bSOr Gerlitz #include <linux/net.h>
6065e7ae7bSOr Gerlitz #include <linux/scatterlist.h>
6165e7ae7bSOr Gerlitz #include <linux/delay.h>
6265e7ae7bSOr Gerlitz 
6365e7ae7bSOr Gerlitz #include <net/sock.h>
6465e7ae7bSOr Gerlitz 
6565e7ae7bSOr Gerlitz #include <asm/uaccess.h>
6665e7ae7bSOr Gerlitz 
6765e7ae7bSOr Gerlitz #include <scsi/scsi_cmnd.h>
6865e7ae7bSOr Gerlitz #include <scsi/scsi_device.h>
6965e7ae7bSOr Gerlitz #include <scsi/scsi_eh.h>
7065e7ae7bSOr Gerlitz #include <scsi/scsi_tcq.h>
7165e7ae7bSOr Gerlitz #include <scsi/scsi_host.h>
7265e7ae7bSOr Gerlitz #include <scsi/scsi.h>
7365e7ae7bSOr Gerlitz #include <scsi/scsi_transport_iscsi.h>
7465e7ae7bSOr Gerlitz 
7565e7ae7bSOr Gerlitz #include "iscsi_iser.h"
7665e7ae7bSOr Gerlitz 
7765e7ae7bSOr Gerlitz static unsigned int iscsi_max_lun = 512;
7865e7ae7bSOr Gerlitz module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
7965e7ae7bSOr Gerlitz 
8065e7ae7bSOr Gerlitz int iser_debug_level = 0;
8165e7ae7bSOr Gerlitz 
8265e7ae7bSOr Gerlitz MODULE_DESCRIPTION("iSER (iSCSI Extensions for RDMA) Datamover "
8365e7ae7bSOr Gerlitz 		   "v" DRV_VER " (" DRV_DATE ")");
8465e7ae7bSOr Gerlitz MODULE_LICENSE("Dual BSD/GPL");
8565e7ae7bSOr Gerlitz MODULE_AUTHOR("Alex Nezhinsky, Dan Bar Dov, Or Gerlitz");
8665e7ae7bSOr Gerlitz 
8765e7ae7bSOr Gerlitz module_param_named(debug_level, iser_debug_level, int, 0644);
8865e7ae7bSOr Gerlitz MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0 (default:disabled)");
8965e7ae7bSOr Gerlitz 
9065e7ae7bSOr Gerlitz struct iser_global ig;
9165e7ae7bSOr Gerlitz 
9265e7ae7bSOr Gerlitz void
9365e7ae7bSOr Gerlitz iscsi_iser_recv(struct iscsi_conn *conn,
9465e7ae7bSOr Gerlitz 		struct iscsi_hdr *hdr, char *rx_data, int rx_data_len)
9565e7ae7bSOr Gerlitz {
9665e7ae7bSOr Gerlitz 	int rc = 0;
9765e7ae7bSOr Gerlitz 	uint32_t ret_itt;
9865e7ae7bSOr Gerlitz 	int datalen;
9965e7ae7bSOr Gerlitz 	int ahslen;
10065e7ae7bSOr Gerlitz 
10165e7ae7bSOr Gerlitz 	/* verify PDU length */
10265e7ae7bSOr Gerlitz 	datalen = ntoh24(hdr->dlength);
10365e7ae7bSOr Gerlitz 	if (datalen != rx_data_len) {
10465e7ae7bSOr Gerlitz 		printk(KERN_ERR "iscsi_iser: datalen %d (hdr) != %d (IB) \n",
10565e7ae7bSOr Gerlitz 		       datalen, rx_data_len);
10665e7ae7bSOr Gerlitz 		rc = ISCSI_ERR_DATALEN;
10765e7ae7bSOr Gerlitz 		goto error;
10865e7ae7bSOr Gerlitz 	}
10965e7ae7bSOr Gerlitz 
11065e7ae7bSOr Gerlitz 	/* read AHS */
11165e7ae7bSOr Gerlitz 	ahslen = hdr->hlength * 4;
11265e7ae7bSOr Gerlitz 
11365e7ae7bSOr Gerlitz 	/* verify itt (itt encoding: age+cid+itt) */
11465e7ae7bSOr Gerlitz 	rc = iscsi_verify_itt(conn, hdr, &ret_itt);
11565e7ae7bSOr Gerlitz 
11665e7ae7bSOr Gerlitz 	if (!rc)
11765e7ae7bSOr Gerlitz 		rc = iscsi_complete_pdu(conn, hdr, rx_data, rx_data_len);
11865e7ae7bSOr Gerlitz 
11965e7ae7bSOr Gerlitz 	if (rc && rc != ISCSI_ERR_NO_SCSI_CMD)
12065e7ae7bSOr Gerlitz 		goto error;
12165e7ae7bSOr Gerlitz 
12265e7ae7bSOr Gerlitz 	return;
12365e7ae7bSOr Gerlitz error:
12465e7ae7bSOr Gerlitz 	iscsi_conn_failure(conn, rc);
12565e7ae7bSOr Gerlitz }
12665e7ae7bSOr Gerlitz 
12765e7ae7bSOr Gerlitz 
12865e7ae7bSOr Gerlitz /**
12965e7ae7bSOr Gerlitz  * iscsi_iser_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
13065e7ae7bSOr Gerlitz  *
13165e7ae7bSOr Gerlitz  **/
13265e7ae7bSOr Gerlitz static void
13365e7ae7bSOr Gerlitz iscsi_iser_cmd_init(struct iscsi_cmd_task *ctask)
13465e7ae7bSOr Gerlitz {
13565e7ae7bSOr Gerlitz 	struct iscsi_iser_conn     *iser_conn  = ctask->conn->dd_data;
13665e7ae7bSOr Gerlitz 	struct iscsi_iser_cmd_task *iser_ctask = ctask->dd_data;
13765e7ae7bSOr Gerlitz 
13865e7ae7bSOr Gerlitz 	iser_ctask->command_sent = 0;
13965e7ae7bSOr Gerlitz 	iser_ctask->iser_conn    = iser_conn;
14065e7ae7bSOr Gerlitz 	iser_ctask_rdma_init(iser_ctask);
14165e7ae7bSOr Gerlitz }
14265e7ae7bSOr Gerlitz 
14365e7ae7bSOr Gerlitz /**
14465e7ae7bSOr Gerlitz  * iscsi_mtask_xmit - xmit management(immediate) task
14565e7ae7bSOr Gerlitz  * @conn: iscsi connection
14665e7ae7bSOr Gerlitz  * @mtask: task management task
14765e7ae7bSOr Gerlitz  *
14865e7ae7bSOr Gerlitz  * Notes:
14965e7ae7bSOr Gerlitz  *	The function can return -EAGAIN in which case caller must
15065e7ae7bSOr Gerlitz  *	call it again later, or recover. '0' return code means successful
15165e7ae7bSOr Gerlitz  *	xmit.
15265e7ae7bSOr Gerlitz  *
15365e7ae7bSOr Gerlitz  **/
15465e7ae7bSOr Gerlitz static int
15565e7ae7bSOr Gerlitz iscsi_iser_mtask_xmit(struct iscsi_conn *conn,
15665e7ae7bSOr Gerlitz 		      struct iscsi_mgmt_task *mtask)
15765e7ae7bSOr Gerlitz {
15865e7ae7bSOr Gerlitz 	int error = 0;
15965e7ae7bSOr Gerlitz 
16065e7ae7bSOr Gerlitz 	debug_scsi("mtask deq [cid %d itt 0x%x]\n", conn->id, mtask->itt);
16165e7ae7bSOr Gerlitz 
16265e7ae7bSOr Gerlitz 	error = iser_send_control(conn, mtask);
16365e7ae7bSOr Gerlitz 
16465e7ae7bSOr Gerlitz 	/* since iser xmits control with zero copy, mtasks can not be recycled
16565e7ae7bSOr Gerlitz 	 * right after sending them.
16665e7ae7bSOr Gerlitz 	 * The recycling scheme is based on whether a response is expected
16765e7ae7bSOr Gerlitz 	 * - if yes, the mtask is recycled at iscsi_complete_pdu
16865e7ae7bSOr Gerlitz 	 * - if no,  the mtask is recycled at iser_snd_completion
16965e7ae7bSOr Gerlitz 	 */
170f0938401SErez Zilber 	if (error && error != -ENOBUFS)
17165e7ae7bSOr Gerlitz 		iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
17265e7ae7bSOr Gerlitz 
17365e7ae7bSOr Gerlitz 	return error;
17465e7ae7bSOr Gerlitz }
17565e7ae7bSOr Gerlitz 
17665e7ae7bSOr Gerlitz static int
17765e7ae7bSOr Gerlitz iscsi_iser_ctask_xmit_unsol_data(struct iscsi_conn *conn,
17865e7ae7bSOr Gerlitz 				 struct iscsi_cmd_task *ctask)
17965e7ae7bSOr Gerlitz {
18065e7ae7bSOr Gerlitz 	struct iscsi_data  hdr;
18165e7ae7bSOr Gerlitz 	int error = 0;
18265e7ae7bSOr Gerlitz 
18365e7ae7bSOr Gerlitz 	/* Send data-out PDUs while there's still unsolicited data to send */
18465e7ae7bSOr Gerlitz 	while (ctask->unsol_count > 0) {
185ffd0436eSMike Christie 		iscsi_prep_unsolicit_data_pdu(ctask, &hdr);
18665e7ae7bSOr Gerlitz 		debug_scsi("Sending data-out: itt 0x%x, data count %d\n",
18765e7ae7bSOr Gerlitz 			   hdr.itt, ctask->data_count);
18865e7ae7bSOr Gerlitz 
18965e7ae7bSOr Gerlitz 		/* the buffer description has been passed with the command */
19065e7ae7bSOr Gerlitz 		/* Send the command */
19165e7ae7bSOr Gerlitz 		error = iser_send_data_out(conn, ctask, &hdr);
19265e7ae7bSOr Gerlitz 		if (error) {
19365e7ae7bSOr Gerlitz 			ctask->unsol_datasn--;
19465e7ae7bSOr Gerlitz 			goto iscsi_iser_ctask_xmit_unsol_data_exit;
19565e7ae7bSOr Gerlitz 		}
19665e7ae7bSOr Gerlitz 		ctask->unsol_count -= ctask->data_count;
19765e7ae7bSOr Gerlitz 		debug_scsi("Need to send %d more as data-out PDUs\n",
19865e7ae7bSOr Gerlitz 			   ctask->unsol_count);
19965e7ae7bSOr Gerlitz 	}
20065e7ae7bSOr Gerlitz 
20165e7ae7bSOr Gerlitz iscsi_iser_ctask_xmit_unsol_data_exit:
20265e7ae7bSOr Gerlitz 	return error;
20365e7ae7bSOr Gerlitz }
20465e7ae7bSOr Gerlitz 
20565e7ae7bSOr Gerlitz static int
20665e7ae7bSOr Gerlitz iscsi_iser_ctask_xmit(struct iscsi_conn *conn,
20765e7ae7bSOr Gerlitz 		      struct iscsi_cmd_task *ctask)
20865e7ae7bSOr Gerlitz {
20965e7ae7bSOr Gerlitz 	struct iscsi_iser_cmd_task *iser_ctask = ctask->dd_data;
21065e7ae7bSOr Gerlitz 	int error = 0;
21165e7ae7bSOr Gerlitz 
21277a23c21SMike Christie 	if (ctask->sc->sc_data_direction == DMA_TO_DEVICE) {
213da9c0c77SFUJITA Tomonori 		BUG_ON(scsi_bufflen(ctask->sc) == 0);
21477a23c21SMike Christie 
21577a23c21SMike Christie 		debug_scsi("cmd [itt %x total %d imm %d unsol_data %d\n",
216da9c0c77SFUJITA Tomonori 			   ctask->itt, scsi_bufflen(ctask->sc),
21777a23c21SMike Christie 			   ctask->imm_count, ctask->unsol_count);
21877a23c21SMike Christie 	}
21977a23c21SMike Christie 
22065e7ae7bSOr Gerlitz 	debug_scsi("ctask deq [cid %d itt 0x%x]\n",
22165e7ae7bSOr Gerlitz 		   conn->id, ctask->itt);
22265e7ae7bSOr Gerlitz 
22365e7ae7bSOr Gerlitz 	/*
22465e7ae7bSOr Gerlitz 	 * serialize with TMF AbortTask
22565e7ae7bSOr Gerlitz 	 */
22665e7ae7bSOr Gerlitz 	if (ctask->mtask)
22765e7ae7bSOr Gerlitz 		return error;
22865e7ae7bSOr Gerlitz 
22965e7ae7bSOr Gerlitz 	/* Send the cmd PDU */
23065e7ae7bSOr Gerlitz 	if (!iser_ctask->command_sent) {
23165e7ae7bSOr Gerlitz 		error = iser_send_command(conn, ctask);
23265e7ae7bSOr Gerlitz 		if (error)
23365e7ae7bSOr Gerlitz 			goto iscsi_iser_ctask_xmit_exit;
23465e7ae7bSOr Gerlitz 		iser_ctask->command_sent = 1;
23565e7ae7bSOr Gerlitz 	}
23665e7ae7bSOr Gerlitz 
23765e7ae7bSOr Gerlitz 	/* Send unsolicited data-out PDU(s) if necessary */
23865e7ae7bSOr Gerlitz 	if (ctask->unsol_count)
23965e7ae7bSOr Gerlitz 		error = iscsi_iser_ctask_xmit_unsol_data(conn, ctask);
24065e7ae7bSOr Gerlitz 
24165e7ae7bSOr Gerlitz  iscsi_iser_ctask_xmit_exit:
242f0938401SErez Zilber 	if (error && error != -ENOBUFS)
24365e7ae7bSOr Gerlitz 		iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
24465e7ae7bSOr Gerlitz 	return error;
24565e7ae7bSOr Gerlitz }
24665e7ae7bSOr Gerlitz 
24765e7ae7bSOr Gerlitz static void
24865e7ae7bSOr Gerlitz iscsi_iser_cleanup_ctask(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
24965e7ae7bSOr Gerlitz {
25065e7ae7bSOr Gerlitz 	struct iscsi_iser_cmd_task *iser_ctask = ctask->dd_data;
25165e7ae7bSOr Gerlitz 
25265e7ae7bSOr Gerlitz 	if (iser_ctask->status == ISER_TASK_STATUS_STARTED) {
25365e7ae7bSOr Gerlitz 		iser_ctask->status = ISER_TASK_STATUS_COMPLETED;
25465e7ae7bSOr Gerlitz 		iser_ctask_rdma_finalize(iser_ctask);
25565e7ae7bSOr Gerlitz 	}
25665e7ae7bSOr Gerlitz }
25765e7ae7bSOr Gerlitz 
25865e7ae7bSOr Gerlitz static struct iser_conn *
25965e7ae7bSOr Gerlitz iscsi_iser_ib_conn_lookup(__u64 ep_handle)
26065e7ae7bSOr Gerlitz {
26165e7ae7bSOr Gerlitz 	struct iser_conn *ib_conn;
26265e7ae7bSOr Gerlitz 	struct iser_conn *uib_conn = (struct iser_conn *)(unsigned long)ep_handle;
26365e7ae7bSOr Gerlitz 
26465e7ae7bSOr Gerlitz 	mutex_lock(&ig.connlist_mutex);
26565e7ae7bSOr Gerlitz 	list_for_each_entry(ib_conn, &ig.connlist, conn_list) {
26665e7ae7bSOr Gerlitz 		if (ib_conn == uib_conn) {
26765e7ae7bSOr Gerlitz 			mutex_unlock(&ig.connlist_mutex);
26865e7ae7bSOr Gerlitz 			return ib_conn;
26965e7ae7bSOr Gerlitz 		}
27065e7ae7bSOr Gerlitz 	}
27165e7ae7bSOr Gerlitz 	mutex_unlock(&ig.connlist_mutex);
27265e7ae7bSOr Gerlitz 	iser_err("no conn exists for eph %llx\n",(unsigned long long)ep_handle);
27365e7ae7bSOr Gerlitz 	return NULL;
27465e7ae7bSOr Gerlitz }
27565e7ae7bSOr Gerlitz 
27665e7ae7bSOr Gerlitz static struct iscsi_cls_conn *
27765e7ae7bSOr Gerlitz iscsi_iser_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
27865e7ae7bSOr Gerlitz {
27965e7ae7bSOr Gerlitz 	struct iscsi_conn *conn;
28065e7ae7bSOr Gerlitz 	struct iscsi_cls_conn *cls_conn;
28165e7ae7bSOr Gerlitz 	struct iscsi_iser_conn *iser_conn;
28265e7ae7bSOr Gerlitz 
28365e7ae7bSOr Gerlitz 	cls_conn = iscsi_conn_setup(cls_session, conn_idx);
28465e7ae7bSOr Gerlitz 	if (!cls_conn)
28565e7ae7bSOr Gerlitz 		return NULL;
28665e7ae7bSOr Gerlitz 	conn = cls_conn->dd_data;
28765e7ae7bSOr Gerlitz 
28865e7ae7bSOr Gerlitz 	/*
28965e7ae7bSOr Gerlitz 	 * due to issues with the login code re iser sematics
29065e7ae7bSOr Gerlitz 	 * this not set in iscsi_conn_setup - FIXME
29165e7ae7bSOr Gerlitz 	 */
29265e7ae7bSOr Gerlitz 	conn->max_recv_dlength = 128;
29365e7ae7bSOr Gerlitz 
29465e7ae7bSOr Gerlitz 	iser_conn = kzalloc(sizeof(*iser_conn), GFP_KERNEL);
29565e7ae7bSOr Gerlitz 	if (!iser_conn)
29665e7ae7bSOr Gerlitz 		goto conn_alloc_fail;
29765e7ae7bSOr Gerlitz 
29865e7ae7bSOr Gerlitz 	/* currently this is the only field which need to be initiated */
29965e7ae7bSOr Gerlitz 	rwlock_init(&iser_conn->lock);
30065e7ae7bSOr Gerlitz 
30165e7ae7bSOr Gerlitz 	conn->dd_data = iser_conn;
30265e7ae7bSOr Gerlitz 	iser_conn->iscsi_conn = conn;
30365e7ae7bSOr Gerlitz 
30465e7ae7bSOr Gerlitz 	return cls_conn;
30565e7ae7bSOr Gerlitz 
30665e7ae7bSOr Gerlitz conn_alloc_fail:
30765e7ae7bSOr Gerlitz 	iscsi_conn_teardown(cls_conn);
30865e7ae7bSOr Gerlitz 	return NULL;
30965e7ae7bSOr Gerlitz }
31065e7ae7bSOr Gerlitz 
31165e7ae7bSOr Gerlitz static void
31265e7ae7bSOr Gerlitz iscsi_iser_conn_destroy(struct iscsi_cls_conn *cls_conn)
31365e7ae7bSOr Gerlitz {
31465e7ae7bSOr Gerlitz 	struct iscsi_conn *conn = cls_conn->dd_data;
31565e7ae7bSOr Gerlitz 	struct iscsi_iser_conn *iser_conn = conn->dd_data;
31665e7ae7bSOr Gerlitz 
31765e7ae7bSOr Gerlitz 	iscsi_conn_teardown(cls_conn);
31887e8df7aSErez Zilber 	if (iser_conn->ib_conn)
31987e8df7aSErez Zilber 		iser_conn->ib_conn->iser_conn = NULL;
32065e7ae7bSOr Gerlitz 	kfree(iser_conn);
32165e7ae7bSOr Gerlitz }
32265e7ae7bSOr Gerlitz 
32365e7ae7bSOr Gerlitz static int
32465e7ae7bSOr Gerlitz iscsi_iser_conn_bind(struct iscsi_cls_session *cls_session,
32565e7ae7bSOr Gerlitz 		     struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
32665e7ae7bSOr Gerlitz 		     int is_leading)
32765e7ae7bSOr Gerlitz {
32865e7ae7bSOr Gerlitz 	struct iscsi_conn *conn = cls_conn->dd_data;
32965e7ae7bSOr Gerlitz 	struct iscsi_iser_conn *iser_conn;
33065e7ae7bSOr Gerlitz 	struct iser_conn *ib_conn;
33165e7ae7bSOr Gerlitz 	int error;
33265e7ae7bSOr Gerlitz 
33365e7ae7bSOr Gerlitz 	error = iscsi_conn_bind(cls_session, cls_conn, is_leading);
33465e7ae7bSOr Gerlitz 	if (error)
33565e7ae7bSOr Gerlitz 		return error;
33665e7ae7bSOr Gerlitz 
33765e7ae7bSOr Gerlitz 	/* the transport ep handle comes from user space so it must be
33865e7ae7bSOr Gerlitz 	 * verified against the global ib connections list */
33965e7ae7bSOr Gerlitz 	ib_conn = iscsi_iser_ib_conn_lookup(transport_eph);
34065e7ae7bSOr Gerlitz 	if (!ib_conn) {
34165e7ae7bSOr Gerlitz 		iser_err("can't bind eph %llx\n",
34265e7ae7bSOr Gerlitz 			 (unsigned long long)transport_eph);
34365e7ae7bSOr Gerlitz 		return -EINVAL;
34465e7ae7bSOr Gerlitz 	}
34565e7ae7bSOr Gerlitz 	/* binds the iSER connection retrieved from the previously
34665e7ae7bSOr Gerlitz 	 * connected ep_handle to the iSCSI layer connection. exchanges
34765e7ae7bSOr Gerlitz 	 * connection pointers */
34865e7ae7bSOr Gerlitz 	iser_err("binding iscsi conn %p to iser_conn %p\n",conn,ib_conn);
34965e7ae7bSOr Gerlitz 	iser_conn = conn->dd_data;
35065e7ae7bSOr Gerlitz 	ib_conn->iser_conn = iser_conn;
35165e7ae7bSOr Gerlitz 	iser_conn->ib_conn  = ib_conn;
35265e7ae7bSOr Gerlitz 
35365e7ae7bSOr Gerlitz 	conn->recv_lock = &iser_conn->lock;
35465e7ae7bSOr Gerlitz 
35565e7ae7bSOr Gerlitz 	return 0;
35665e7ae7bSOr Gerlitz }
35765e7ae7bSOr Gerlitz 
35865e7ae7bSOr Gerlitz static int
35965e7ae7bSOr Gerlitz iscsi_iser_conn_start(struct iscsi_cls_conn *cls_conn)
36065e7ae7bSOr Gerlitz {
36165e7ae7bSOr Gerlitz 	struct iscsi_conn *conn = cls_conn->dd_data;
36265e7ae7bSOr Gerlitz 	int err;
36365e7ae7bSOr Gerlitz 
3642e7a7426SErez Zilber 	err = iser_conn_set_full_featured_mode(conn);
36565e7ae7bSOr Gerlitz 	if (err)
36665e7ae7bSOr Gerlitz 		return err;
36765e7ae7bSOr Gerlitz 
3682e7a7426SErez Zilber 	return iscsi_conn_start(cls_conn);
36965e7ae7bSOr Gerlitz }
37065e7ae7bSOr Gerlitz 
37165e7ae7bSOr Gerlitz static struct iscsi_transport iscsi_iser_transport;
37265e7ae7bSOr Gerlitz 
37365e7ae7bSOr Gerlitz static struct iscsi_cls_session *
37465e7ae7bSOr Gerlitz iscsi_iser_session_create(struct iscsi_transport *iscsit,
37565e7ae7bSOr Gerlitz 			 struct scsi_transport_template *scsit,
3761548271eSMike Christie 			 uint16_t cmds_max, uint16_t qdepth,
37765e7ae7bSOr Gerlitz 			 uint32_t initial_cmdsn, uint32_t *hostno)
37865e7ae7bSOr Gerlitz {
37965e7ae7bSOr Gerlitz 	struct iscsi_cls_session *cls_session;
38065e7ae7bSOr Gerlitz 	struct iscsi_session *session;
38165e7ae7bSOr Gerlitz 	int i;
38265e7ae7bSOr Gerlitz 	uint32_t hn;
38365e7ae7bSOr Gerlitz 	struct iscsi_cmd_task  *ctask;
38465e7ae7bSOr Gerlitz 	struct iscsi_mgmt_task *mtask;
38565e7ae7bSOr Gerlitz 	struct iscsi_iser_cmd_task *iser_ctask;
38665e7ae7bSOr Gerlitz 	struct iser_desc *desc;
38765e7ae7bSOr Gerlitz 
3881548271eSMike Christie 	/*
3891548271eSMike Christie 	 * we do not support setting can_queue cmd_per_lun from userspace yet
3901548271eSMike Christie 	 * because we preallocate so many resources
3911548271eSMike Christie 	 */
39265e7ae7bSOr Gerlitz 	cls_session = iscsi_session_setup(iscsit, scsit,
3931548271eSMike Christie 					  ISCSI_DEF_XMIT_CMDS_MAX,
3941548271eSMike Christie 					  ISCSI_MAX_CMD_PER_LUN,
39565e7ae7bSOr Gerlitz 					  sizeof(struct iscsi_iser_cmd_task),
39665e7ae7bSOr Gerlitz 					  sizeof(struct iser_desc),
39765e7ae7bSOr Gerlitz 					  initial_cmdsn, &hn);
39865e7ae7bSOr Gerlitz 	if (!cls_session)
39965e7ae7bSOr Gerlitz 	return NULL;
40065e7ae7bSOr Gerlitz 
40165e7ae7bSOr Gerlitz 	*hostno = hn;
40265e7ae7bSOr Gerlitz 	session = class_to_transport_session(cls_session);
40365e7ae7bSOr Gerlitz 
40465e7ae7bSOr Gerlitz 	/* libiscsi setup itts, data and pool so just set desc fields */
40565e7ae7bSOr Gerlitz 	for (i = 0; i < session->cmds_max; i++) {
40665e7ae7bSOr Gerlitz 		ctask      = session->cmds[i];
40765e7ae7bSOr Gerlitz 		iser_ctask = ctask->dd_data;
40865e7ae7bSOr Gerlitz 		ctask->hdr = (struct iscsi_cmd *)&iser_ctask->desc.iscsi_header;
40965e7ae7bSOr Gerlitz 	}
41065e7ae7bSOr Gerlitz 
41165e7ae7bSOr Gerlitz 	for (i = 0; i < session->mgmtpool_max; i++) {
41265e7ae7bSOr Gerlitz 		mtask      = session->mgmt_cmds[i];
41365e7ae7bSOr Gerlitz 		desc       = mtask->dd_data;
41465e7ae7bSOr Gerlitz 		mtask->hdr = &desc->iscsi_header;
41565e7ae7bSOr Gerlitz 		desc->data = mtask->data;
41665e7ae7bSOr Gerlitz 	}
41765e7ae7bSOr Gerlitz 
41865e7ae7bSOr Gerlitz 	return cls_session;
41965e7ae7bSOr Gerlitz }
42065e7ae7bSOr Gerlitz 
42165e7ae7bSOr Gerlitz static int
422358ff019SMike Christie iscsi_iser_set_param(struct iscsi_cls_conn *cls_conn,
423358ff019SMike Christie 		     enum iscsi_param param, char *buf, int buflen)
42465e7ae7bSOr Gerlitz {
425358ff019SMike Christie 	int value;
42665e7ae7bSOr Gerlitz 
42765e7ae7bSOr Gerlitz 	switch (param) {
42865e7ae7bSOr Gerlitz 	case ISCSI_PARAM_MAX_RECV_DLENGTH:
42965e7ae7bSOr Gerlitz 		/* TBD */
43065e7ae7bSOr Gerlitz 		break;
43165e7ae7bSOr Gerlitz 	case ISCSI_PARAM_HDRDGST_EN:
432358ff019SMike Christie 		sscanf(buf, "%d", &value);
43365e7ae7bSOr Gerlitz 		if (value) {
43465e7ae7bSOr Gerlitz 			printk(KERN_ERR "DataDigest wasn't negotiated to None");
43565e7ae7bSOr Gerlitz 			return -EPROTO;
43665e7ae7bSOr Gerlitz 		}
43765e7ae7bSOr Gerlitz 		break;
43865e7ae7bSOr Gerlitz 	case ISCSI_PARAM_DATADGST_EN:
439358ff019SMike Christie 		sscanf(buf, "%d", &value);
44065e7ae7bSOr Gerlitz 		if (value) {
44165e7ae7bSOr Gerlitz 			printk(KERN_ERR "DataDigest wasn't negotiated to None");
44265e7ae7bSOr Gerlitz 			return -EPROTO;
44365e7ae7bSOr Gerlitz 		}
44465e7ae7bSOr Gerlitz 		break;
44565e7ae7bSOr Gerlitz 	case ISCSI_PARAM_IFMARKER_EN:
446358ff019SMike Christie 		sscanf(buf, "%d", &value);
44765e7ae7bSOr Gerlitz 		if (value) {
44865e7ae7bSOr Gerlitz 			printk(KERN_ERR "IFMarker wasn't negotiated to No");
44965e7ae7bSOr Gerlitz 			return -EPROTO;
45065e7ae7bSOr Gerlitz 		}
45165e7ae7bSOr Gerlitz 		break;
45265e7ae7bSOr Gerlitz 	case ISCSI_PARAM_OFMARKER_EN:
453358ff019SMike Christie 		sscanf(buf, "%d", &value);
45465e7ae7bSOr Gerlitz 		if (value) {
45565e7ae7bSOr Gerlitz 			printk(KERN_ERR "OFMarker wasn't negotiated to No");
45665e7ae7bSOr Gerlitz 			return -EPROTO;
45765e7ae7bSOr Gerlitz 		}
45865e7ae7bSOr Gerlitz 		break;
45965e7ae7bSOr Gerlitz 	default:
460358ff019SMike Christie 		return iscsi_set_param(cls_conn, param, buf, buflen);
46165e7ae7bSOr Gerlitz 	}
46265e7ae7bSOr Gerlitz 
46365e7ae7bSOr Gerlitz 	return 0;
46465e7ae7bSOr Gerlitz }
46565e7ae7bSOr Gerlitz 
46665e7ae7bSOr Gerlitz static void
46765e7ae7bSOr Gerlitz iscsi_iser_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
46865e7ae7bSOr Gerlitz {
46965e7ae7bSOr Gerlitz 	struct iscsi_conn *conn = cls_conn->dd_data;
47065e7ae7bSOr Gerlitz 
47165e7ae7bSOr Gerlitz 	stats->txdata_octets = conn->txdata_octets;
47265e7ae7bSOr Gerlitz 	stats->rxdata_octets = conn->rxdata_octets;
47365e7ae7bSOr Gerlitz 	stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
47465e7ae7bSOr Gerlitz 	stats->dataout_pdus = conn->dataout_pdus_cnt;
47565e7ae7bSOr Gerlitz 	stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
47665e7ae7bSOr Gerlitz 	stats->datain_pdus = conn->datain_pdus_cnt; /* always 0 */
47765e7ae7bSOr Gerlitz 	stats->r2t_pdus = conn->r2t_pdus_cnt; /* always 0 */
47865e7ae7bSOr Gerlitz 	stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
47965e7ae7bSOr Gerlitz 	stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
48065e7ae7bSOr Gerlitz 	stats->custom_length = 3;
48165e7ae7bSOr Gerlitz 	strcpy(stats->custom[0].desc, "qp_tx_queue_full");
48265e7ae7bSOr Gerlitz 	stats->custom[0].value = 0; /* TB iser_conn->qp_tx_queue_full; */
48365e7ae7bSOr Gerlitz 	strcpy(stats->custom[1].desc, "fmr_map_not_avail");
48465e7ae7bSOr Gerlitz 	stats->custom[1].value = 0; /* TB iser_conn->fmr_map_not_avail */;
48565e7ae7bSOr Gerlitz 	strcpy(stats->custom[2].desc, "eh_abort_cnt");
48665e7ae7bSOr Gerlitz 	stats->custom[2].value = conn->eh_abort_cnt;
48765e7ae7bSOr Gerlitz }
48865e7ae7bSOr Gerlitz 
48965e7ae7bSOr Gerlitz static int
49065e7ae7bSOr Gerlitz iscsi_iser_ep_connect(struct sockaddr *dst_addr, int non_blocking,
49165e7ae7bSOr Gerlitz 		      __u64 *ep_handle)
49265e7ae7bSOr Gerlitz {
49365e7ae7bSOr Gerlitz 	int err;
49465e7ae7bSOr Gerlitz 	struct iser_conn *ib_conn;
49565e7ae7bSOr Gerlitz 
49665e7ae7bSOr Gerlitz 	err = iser_conn_init(&ib_conn);
49765e7ae7bSOr Gerlitz 	if (err)
49865e7ae7bSOr Gerlitz 		goto out;
49965e7ae7bSOr Gerlitz 
50065e7ae7bSOr Gerlitz 	err = iser_connect(ib_conn, NULL, (struct sockaddr_in *)dst_addr, non_blocking);
50165e7ae7bSOr Gerlitz 	if (!err)
50265e7ae7bSOr Gerlitz 		*ep_handle = (__u64)(unsigned long)ib_conn;
50365e7ae7bSOr Gerlitz 
50465e7ae7bSOr Gerlitz out:
50565e7ae7bSOr Gerlitz 	return err;
50665e7ae7bSOr Gerlitz }
50765e7ae7bSOr Gerlitz 
50865e7ae7bSOr Gerlitz static int
50965e7ae7bSOr Gerlitz iscsi_iser_ep_poll(__u64 ep_handle, int timeout_ms)
51065e7ae7bSOr Gerlitz {
51165e7ae7bSOr Gerlitz 	struct iser_conn *ib_conn = iscsi_iser_ib_conn_lookup(ep_handle);
51265e7ae7bSOr Gerlitz 	int rc;
51365e7ae7bSOr Gerlitz 
51465e7ae7bSOr Gerlitz 	if (!ib_conn)
51565e7ae7bSOr Gerlitz 		return -EINVAL;
51665e7ae7bSOr Gerlitz 
51765e7ae7bSOr Gerlitz 	rc = wait_event_interruptible_timeout(ib_conn->wait,
51865e7ae7bSOr Gerlitz 			     ib_conn->state == ISER_CONN_UP,
51965e7ae7bSOr Gerlitz 			     msecs_to_jiffies(timeout_ms));
52065e7ae7bSOr Gerlitz 
52165e7ae7bSOr Gerlitz 	/* if conn establishment failed, return error code to iscsi */
52265e7ae7bSOr Gerlitz 	if (!rc &&
52365e7ae7bSOr Gerlitz 	    (ib_conn->state == ISER_CONN_TERMINATING ||
52465e7ae7bSOr Gerlitz 	     ib_conn->state == ISER_CONN_DOWN))
52565e7ae7bSOr Gerlitz 		rc = -1;
52665e7ae7bSOr Gerlitz 
52765e7ae7bSOr Gerlitz 	iser_err("ib conn %p rc = %d\n", ib_conn, rc);
52865e7ae7bSOr Gerlitz 
52965e7ae7bSOr Gerlitz 	if (rc > 0)
53065e7ae7bSOr Gerlitz 		return 1; /* success, this is the equivalent of POLLOUT */
53165e7ae7bSOr Gerlitz 	else if (!rc)
53265e7ae7bSOr Gerlitz 		return 0; /* timeout */
53365e7ae7bSOr Gerlitz 	else
53465e7ae7bSOr Gerlitz 		return rc; /* signal */
53565e7ae7bSOr Gerlitz }
53665e7ae7bSOr Gerlitz 
53765e7ae7bSOr Gerlitz static void
53865e7ae7bSOr Gerlitz iscsi_iser_ep_disconnect(__u64 ep_handle)
53965e7ae7bSOr Gerlitz {
5401c83469dSMike Christie 	struct iser_conn *ib_conn;
54165e7ae7bSOr Gerlitz 
5421c83469dSMike Christie 	ib_conn = iscsi_iser_ib_conn_lookup(ep_handle);
54365e7ae7bSOr Gerlitz 	if (!ib_conn)
54465e7ae7bSOr Gerlitz 		return;
54565e7ae7bSOr Gerlitz 
54665e7ae7bSOr Gerlitz 	iser_err("ib conn %p state %d\n",ib_conn, ib_conn->state);
54765e7ae7bSOr Gerlitz 	iser_conn_terminate(ib_conn);
54865e7ae7bSOr Gerlitz }
54965e7ae7bSOr Gerlitz 
55065e7ae7bSOr Gerlitz static struct scsi_host_template iscsi_iser_sht = {
55165e7ae7bSOr Gerlitz 	.name                   = "iSCSI Initiator over iSER, v." DRV_VER,
55265e7ae7bSOr Gerlitz 	.queuecommand           = iscsi_queuecommand,
5531548271eSMike Christie 	.can_queue		= ISCSI_DEF_XMIT_CMDS_MAX - 1,
55465e7ae7bSOr Gerlitz 	.sg_tablesize           = ISCSI_ISER_SG_TABLESIZE,
5558072ec2fSErez Zilber 	.max_sectors		= 1024,
55665e7ae7bSOr Gerlitz 	.cmd_per_lun            = ISCSI_MAX_CMD_PER_LUN,
55765e7ae7bSOr Gerlitz 	.eh_abort_handler       = iscsi_eh_abort,
55865e7ae7bSOr Gerlitz 	.eh_host_reset_handler	= iscsi_eh_host_reset,
55965e7ae7bSOr Gerlitz 	.use_clustering         = DISABLE_CLUSTERING,
56065e7ae7bSOr Gerlitz 	.proc_name              = "iscsi_iser",
56165e7ae7bSOr Gerlitz 	.this_id                = -1,
56265e7ae7bSOr Gerlitz };
56365e7ae7bSOr Gerlitz 
56465e7ae7bSOr Gerlitz static struct iscsi_transport iscsi_iser_transport = {
56565e7ae7bSOr Gerlitz 	.owner                  = THIS_MODULE,
56665e7ae7bSOr Gerlitz 	.name                   = "iser",
56765e7ae7bSOr Gerlitz 	.caps                   = CAP_RECOVERY_L0 | CAP_MULTI_R2T,
56865e7ae7bSOr Gerlitz 	.param_mask		= ISCSI_MAX_RECV_DLENGTH |
56965e7ae7bSOr Gerlitz 				  ISCSI_MAX_XMIT_DLENGTH |
57065e7ae7bSOr Gerlitz 				  ISCSI_HDRDGST_EN |
57165e7ae7bSOr Gerlitz 				  ISCSI_DATADGST_EN |
57265e7ae7bSOr Gerlitz 				  ISCSI_INITIAL_R2T_EN |
57365e7ae7bSOr Gerlitz 				  ISCSI_MAX_R2T |
57465e7ae7bSOr Gerlitz 				  ISCSI_IMM_DATA_EN |
57565e7ae7bSOr Gerlitz 				  ISCSI_FIRST_BURST |
57665e7ae7bSOr Gerlitz 				  ISCSI_MAX_BURST |
57765e7ae7bSOr Gerlitz 				  ISCSI_PDU_INORDER_EN |
578358ff019SMike Christie 				  ISCSI_DATASEQ_INORDER_EN |
579358ff019SMike Christie 				  ISCSI_EXP_STATSN |
580358ff019SMike Christie 				  ISCSI_PERSISTENT_PORT |
581358ff019SMike Christie 				  ISCSI_PERSISTENT_ADDRESS |
582b2c64167SMike Christie 				  ISCSI_TARGET_NAME | ISCSI_TPGT |
583b2c64167SMike Christie 				  ISCSI_USERNAME | ISCSI_PASSWORD |
584b2c64167SMike Christie 				  ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN,
5858ad5781aSMike Christie 	.host_param_mask	= ISCSI_HOST_HWADDRESS |
586d8196ed2SMike Christie 				  ISCSI_HOST_NETDEV_NAME |
5878ad5781aSMike Christie 				  ISCSI_HOST_INITIATOR_NAME,
58865e7ae7bSOr Gerlitz 	.host_template          = &iscsi_iser_sht,
58965e7ae7bSOr Gerlitz 	.conndata_size		= sizeof(struct iscsi_conn),
59065e7ae7bSOr Gerlitz 	.max_lun                = ISCSI_ISER_MAX_LUN,
59165e7ae7bSOr Gerlitz 	.max_cmd_len            = ISCSI_ISER_MAX_CMD_LEN,
59265e7ae7bSOr Gerlitz 	/* session management */
59365e7ae7bSOr Gerlitz 	.create_session         = iscsi_iser_session_create,
59465e7ae7bSOr Gerlitz 	.destroy_session        = iscsi_session_teardown,
59565e7ae7bSOr Gerlitz 	/* connection management */
59665e7ae7bSOr Gerlitz 	.create_conn            = iscsi_iser_conn_create,
59765e7ae7bSOr Gerlitz 	.bind_conn              = iscsi_iser_conn_bind,
59865e7ae7bSOr Gerlitz 	.destroy_conn           = iscsi_iser_conn_destroy,
599358ff019SMike Christie 	.set_param              = iscsi_iser_set_param,
600358ff019SMike Christie 	.get_conn_param		= iscsi_conn_get_param,
601358ff019SMike Christie 	.get_session_param	= iscsi_session_get_param,
60265e7ae7bSOr Gerlitz 	.start_conn             = iscsi_iser_conn_start,
60365e7ae7bSOr Gerlitz 	.stop_conn              = iscsi_conn_stop,
6040801c242SMike Christie 	/* iscsi host params */
6050801c242SMike Christie 	.get_host_param		= iscsi_host_get_param,
6060801c242SMike Christie 	.set_host_param		= iscsi_host_set_param,
60765e7ae7bSOr Gerlitz 	/* IO */
60865e7ae7bSOr Gerlitz 	.send_pdu		= iscsi_conn_send_pdu,
60965e7ae7bSOr Gerlitz 	.get_stats		= iscsi_iser_conn_get_stats,
61065e7ae7bSOr Gerlitz 	.init_cmd_task		= iscsi_iser_cmd_init,
61165e7ae7bSOr Gerlitz 	.xmit_cmd_task		= iscsi_iser_ctask_xmit,
61265e7ae7bSOr Gerlitz 	.xmit_mgmt_task		= iscsi_iser_mtask_xmit,
61365e7ae7bSOr Gerlitz 	.cleanup_cmd_task	= iscsi_iser_cleanup_ctask,
61465e7ae7bSOr Gerlitz 	/* recovery */
61565e7ae7bSOr Gerlitz 	.session_recovery_timedout = iscsi_session_recovery_timedout,
61665e7ae7bSOr Gerlitz 
61765e7ae7bSOr Gerlitz 	.ep_connect             = iscsi_iser_ep_connect,
61865e7ae7bSOr Gerlitz 	.ep_poll                = iscsi_iser_ep_poll,
61965e7ae7bSOr Gerlitz 	.ep_disconnect          = iscsi_iser_ep_disconnect
62065e7ae7bSOr Gerlitz };
62165e7ae7bSOr Gerlitz 
62265e7ae7bSOr Gerlitz static int __init iser_init(void)
62365e7ae7bSOr Gerlitz {
62465e7ae7bSOr Gerlitz 	int err;
62565e7ae7bSOr Gerlitz 
62665e7ae7bSOr Gerlitz 	iser_dbg("Starting iSER datamover...\n");
62765e7ae7bSOr Gerlitz 
62865e7ae7bSOr Gerlitz 	if (iscsi_max_lun < 1) {
62965e7ae7bSOr Gerlitz 		printk(KERN_ERR "Invalid max_lun value of %u\n", iscsi_max_lun);
63065e7ae7bSOr Gerlitz 		return -EINVAL;
63165e7ae7bSOr Gerlitz 	}
63265e7ae7bSOr Gerlitz 
63365e7ae7bSOr Gerlitz 	iscsi_iser_transport.max_lun = iscsi_max_lun;
63465e7ae7bSOr Gerlitz 
63565e7ae7bSOr Gerlitz 	memset(&ig, 0, sizeof(struct iser_global));
63665e7ae7bSOr Gerlitz 
63765e7ae7bSOr Gerlitz 	ig.desc_cache = kmem_cache_create("iser_descriptors",
63865e7ae7bSOr Gerlitz 					  sizeof (struct iser_desc),
63965e7ae7bSOr Gerlitz 					  0, SLAB_HWCACHE_ALIGN,
64065e7ae7bSOr Gerlitz 					  NULL, NULL);
64165e7ae7bSOr Gerlitz 	if (ig.desc_cache == NULL)
64265e7ae7bSOr Gerlitz 		return -ENOMEM;
64365e7ae7bSOr Gerlitz 
64465e7ae7bSOr Gerlitz 	/* device init is called only after the first addr resolution */
64565e7ae7bSOr Gerlitz 	mutex_init(&ig.device_list_mutex);
64665e7ae7bSOr Gerlitz 	INIT_LIST_HEAD(&ig.device_list);
64765e7ae7bSOr Gerlitz 	mutex_init(&ig.connlist_mutex);
64865e7ae7bSOr Gerlitz 	INIT_LIST_HEAD(&ig.connlist);
64965e7ae7bSOr Gerlitz 
65065e7ae7bSOr Gerlitz 	if (!iscsi_register_transport(&iscsi_iser_transport)) {
65165e7ae7bSOr Gerlitz 		iser_err("iscsi_register_transport failed\n");
65265e7ae7bSOr Gerlitz 		err = -EINVAL;
65365e7ae7bSOr Gerlitz 		goto register_transport_failure;
65465e7ae7bSOr Gerlitz 	}
65565e7ae7bSOr Gerlitz 
65665e7ae7bSOr Gerlitz 	return 0;
65765e7ae7bSOr Gerlitz 
65865e7ae7bSOr Gerlitz register_transport_failure:
65965e7ae7bSOr Gerlitz 	kmem_cache_destroy(ig.desc_cache);
66065e7ae7bSOr Gerlitz 
66165e7ae7bSOr Gerlitz 	return err;
66265e7ae7bSOr Gerlitz }
66365e7ae7bSOr Gerlitz 
66465e7ae7bSOr Gerlitz static void __exit iser_exit(void)
66565e7ae7bSOr Gerlitz {
66665e7ae7bSOr Gerlitz 	iser_dbg("Removing iSER datamover...\n");
66765e7ae7bSOr Gerlitz 	iscsi_unregister_transport(&iscsi_iser_transport);
66865e7ae7bSOr Gerlitz 	kmem_cache_destroy(ig.desc_cache);
66965e7ae7bSOr Gerlitz }
67065e7ae7bSOr Gerlitz 
67165e7ae7bSOr Gerlitz module_init(iser_init);
67265e7ae7bSOr Gerlitz module_exit(iser_exit);
673