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.
828f292e8SOr Gerlitz  * Copyright (c) 2013 Mellanox Technologies. All rights reserved.
965e7ae7bSOr Gerlitz  * maintained by openib-general@openib.org
1065e7ae7bSOr Gerlitz  *
1165e7ae7bSOr Gerlitz  * This software is available to you under a choice of one of two
1265e7ae7bSOr Gerlitz  * licenses.  You may choose to be licensed under the terms of the GNU
1365e7ae7bSOr Gerlitz  * General Public License (GPL) Version 2, available from the file
1465e7ae7bSOr Gerlitz  * COPYING in the main directory of this source tree, or the
1565e7ae7bSOr Gerlitz  * OpenIB.org BSD license below:
1665e7ae7bSOr Gerlitz  *
1765e7ae7bSOr Gerlitz  *     Redistribution and use in source and binary forms, with or
1865e7ae7bSOr Gerlitz  *     without modification, are permitted provided that the following
1965e7ae7bSOr Gerlitz  *     conditions are met:
2065e7ae7bSOr Gerlitz  *
2165e7ae7bSOr Gerlitz  *	- Redistributions of source code must retain the above
2265e7ae7bSOr Gerlitz  *	  copyright notice, this list of conditions and the following
2365e7ae7bSOr Gerlitz  *	  disclaimer.
2465e7ae7bSOr Gerlitz  *
2565e7ae7bSOr Gerlitz  *	- Redistributions in binary form must reproduce the above
2665e7ae7bSOr Gerlitz  *	  copyright notice, this list of conditions and the following
2765e7ae7bSOr Gerlitz  *	  disclaimer in the documentation and/or other materials
2865e7ae7bSOr Gerlitz  *	  provided with the distribution.
2965e7ae7bSOr Gerlitz  *
3065e7ae7bSOr Gerlitz  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
3165e7ae7bSOr Gerlitz  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
3265e7ae7bSOr Gerlitz  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
3365e7ae7bSOr Gerlitz  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
3465e7ae7bSOr Gerlitz  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
3565e7ae7bSOr Gerlitz  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
3665e7ae7bSOr Gerlitz  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3765e7ae7bSOr Gerlitz  * SOFTWARE.
3865e7ae7bSOr Gerlitz  *
3965e7ae7bSOr Gerlitz  * Credits:
4065e7ae7bSOr Gerlitz  *	Christoph Hellwig
4165e7ae7bSOr Gerlitz  *	FUJITA Tomonori
4265e7ae7bSOr Gerlitz  *	Arne Redlich
4365e7ae7bSOr Gerlitz  *	Zhenyu Wang
4465e7ae7bSOr Gerlitz  * Modified by:
4565e7ae7bSOr Gerlitz  *      Erez Zilber
4665e7ae7bSOr Gerlitz  */
4765e7ae7bSOr Gerlitz 
4865e7ae7bSOr Gerlitz #include <linux/types.h>
4965e7ae7bSOr Gerlitz #include <linux/list.h>
5065e7ae7bSOr Gerlitz #include <linux/hardirq.h>
5165e7ae7bSOr Gerlitz #include <linux/kfifo.h>
5265e7ae7bSOr Gerlitz #include <linux/blkdev.h>
5365e7ae7bSOr Gerlitz #include <linux/init.h>
5465e7ae7bSOr Gerlitz #include <linux/ioctl.h>
5565e7ae7bSOr Gerlitz #include <linux/cdev.h>
5665e7ae7bSOr Gerlitz #include <linux/in.h>
5765e7ae7bSOr Gerlitz #include <linux/net.h>
5865e7ae7bSOr Gerlitz #include <linux/scatterlist.h>
5965e7ae7bSOr Gerlitz #include <linux/delay.h>
605a0e3ad6STejun Heo #include <linux/slab.h>
61e4dd23d7SPaul Gortmaker #include <linux/module.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 
7775613521SMike Christie static struct scsi_host_template iscsi_iser_sht;
7875613521SMike Christie static struct iscsi_transport iscsi_iser_transport;
7975613521SMike Christie static struct scsi_transport_template *iscsi_iser_scsi_transport;
8075613521SMike Christie 
8165e7ae7bSOr Gerlitz static unsigned int iscsi_max_lun = 512;
8265e7ae7bSOr Gerlitz module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
8365e7ae7bSOr Gerlitz 
8465e7ae7bSOr Gerlitz int iser_debug_level = 0;
8565e7ae7bSOr Gerlitz 
86c1d786e6SRoi Dayan MODULE_DESCRIPTION("iSER (iSCSI Extensions for RDMA) Datamover");
8765e7ae7bSOr Gerlitz MODULE_LICENSE("Dual BSD/GPL");
8865e7ae7bSOr Gerlitz MODULE_AUTHOR("Alex Nezhinsky, Dan Bar Dov, Or Gerlitz");
89c1d786e6SRoi Dayan MODULE_VERSION(DRV_VER);
9065e7ae7bSOr Gerlitz 
9165e7ae7bSOr Gerlitz module_param_named(debug_level, iser_debug_level, int, 0644);
9265e7ae7bSOr Gerlitz MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0 (default:disabled)");
9365e7ae7bSOr Gerlitz 
9465e7ae7bSOr Gerlitz struct iser_global ig;
9565e7ae7bSOr Gerlitz 
9665e7ae7bSOr Gerlitz void
9765e7ae7bSOr Gerlitz iscsi_iser_recv(struct iscsi_conn *conn,
9865e7ae7bSOr Gerlitz 		struct iscsi_hdr *hdr, char *rx_data, int rx_data_len)
9965e7ae7bSOr Gerlitz {
10065e7ae7bSOr Gerlitz 	int rc = 0;
10165e7ae7bSOr Gerlitz 	int datalen;
10265e7ae7bSOr Gerlitz 	int ahslen;
10365e7ae7bSOr Gerlitz 
10465e7ae7bSOr Gerlitz 	/* verify PDU length */
10565e7ae7bSOr Gerlitz 	datalen = ntoh24(hdr->dlength);
106200ae1a0SOr Gerlitz 	if (datalen > rx_data_len || (datalen + 4) < rx_data_len) {
107200ae1a0SOr Gerlitz 		iser_err("wrong datalen %d (hdr), %d (IB)\n",
10865e7ae7bSOr Gerlitz 			datalen, rx_data_len);
10965e7ae7bSOr Gerlitz 		rc = ISCSI_ERR_DATALEN;
11065e7ae7bSOr Gerlitz 		goto error;
11165e7ae7bSOr Gerlitz 	}
11265e7ae7bSOr Gerlitz 
113200ae1a0SOr Gerlitz 	if (datalen != rx_data_len)
114200ae1a0SOr Gerlitz 		iser_dbg("aligned datalen (%d) hdr, %d (IB)\n",
115200ae1a0SOr Gerlitz 			datalen, rx_data_len);
116200ae1a0SOr Gerlitz 
11765e7ae7bSOr Gerlitz 	/* read AHS */
11865e7ae7bSOr Gerlitz 	ahslen = hdr->hlength * 4;
11965e7ae7bSOr Gerlitz 
12065e7ae7bSOr Gerlitz 	rc = iscsi_complete_pdu(conn, hdr, rx_data, rx_data_len);
12165e7ae7bSOr Gerlitz 	if (rc && rc != ISCSI_ERR_NO_SCSI_CMD)
12265e7ae7bSOr Gerlitz 		goto error;
12365e7ae7bSOr Gerlitz 
12465e7ae7bSOr Gerlitz 	return;
12565e7ae7bSOr Gerlitz error:
12665e7ae7bSOr Gerlitz 	iscsi_conn_failure(conn, rc);
12765e7ae7bSOr Gerlitz }
12865e7ae7bSOr Gerlitz 
1292ff79d52SMike Christie static int iscsi_iser_pdu_alloc(struct iscsi_task *task, uint8_t opcode)
1300f9c7449SMike Christie {
1310f9c7449SMike Christie 	struct iscsi_iser_task *iser_task = task->dd_data;
1320f9c7449SMike Christie 
1330f9c7449SMike Christie 	task->hdr = (struct iscsi_hdr *)&iser_task->desc.iscsi_header;
1340f9c7449SMike Christie 	task->hdr_max = sizeof(iser_task->desc.iscsi_header);
1350f9c7449SMike Christie 	return 0;
1360f9c7449SMike Christie }
13765e7ae7bSOr Gerlitz 
138f19624aaSOr Gerlitz int iser_initialize_task_headers(struct iscsi_task *task,
139f19624aaSOr Gerlitz 						struct iser_tx_desc *tx_desc)
140f19624aaSOr Gerlitz {
141f19624aaSOr Gerlitz 	struct iscsi_iser_conn *iser_conn = task->conn->dd_data;
142f19624aaSOr Gerlitz 	struct iser_device     *device    = iser_conn->ib_conn->device;
143f19624aaSOr Gerlitz 	struct iscsi_iser_task *iser_task = task->dd_data;
144f19624aaSOr Gerlitz 	u64 dma_addr;
145f19624aaSOr Gerlitz 
146f19624aaSOr Gerlitz 	dma_addr = ib_dma_map_single(device->ib_device, (void *)tx_desc,
147f19624aaSOr Gerlitz 				ISER_HEADERS_LEN, DMA_TO_DEVICE);
148f19624aaSOr Gerlitz 	if (ib_dma_mapping_error(device->ib_device, dma_addr))
149f19624aaSOr Gerlitz 		return -ENOMEM;
150f19624aaSOr Gerlitz 
151f19624aaSOr Gerlitz 	tx_desc->dma_addr = dma_addr;
152f19624aaSOr Gerlitz 	tx_desc->tx_sg[0].addr   = tx_desc->dma_addr;
153f19624aaSOr Gerlitz 	tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
154f19624aaSOr Gerlitz 	tx_desc->tx_sg[0].lkey   = device->mr->lkey;
155f19624aaSOr Gerlitz 
156f19624aaSOr Gerlitz 	iser_task->iser_conn		= iser_conn;
157f19624aaSOr Gerlitz 	return 0;
158f19624aaSOr Gerlitz }
15965e7ae7bSOr Gerlitz /**
1602261ec3dSMike Christie  * iscsi_iser_task_init - Initialize task
1612261ec3dSMike Christie  * @task: iscsi task
16265e7ae7bSOr Gerlitz  *
1632261ec3dSMike Christie  * Initialize the task for the scsi command or mgmt command.
1642747fdb2SMike Christie  */
165a8ac6311SOlaf Kirch static int
1662261ec3dSMike Christie iscsi_iser_task_init(struct iscsi_task *task)
16765e7ae7bSOr Gerlitz {
1682261ec3dSMike Christie 	struct iscsi_iser_task *iser_task = task->dd_data;
16965e7ae7bSOr Gerlitz 
170f19624aaSOr Gerlitz 	if (iser_initialize_task_headers(task, &iser_task->desc))
171f19624aaSOr Gerlitz 			return -ENOMEM;
172f19624aaSOr Gerlitz 
1732261ec3dSMike Christie 	/* mgmt task */
174f19624aaSOr Gerlitz 	if (!task->sc)
1752747fdb2SMike Christie 		return 0;
1762747fdb2SMike Christie 
1772261ec3dSMike Christie 	iser_task->command_sent = 0;
1782261ec3dSMike Christie 	iser_task_rdma_init(iser_task);
179a8ac6311SOlaf Kirch 	return 0;
18065e7ae7bSOr Gerlitz }
18165e7ae7bSOr Gerlitz 
18265e7ae7bSOr Gerlitz /**
1832261ec3dSMike Christie  * iscsi_iser_mtask_xmit - xmit management(immediate) task
18465e7ae7bSOr Gerlitz  * @conn: iscsi connection
1852261ec3dSMike Christie  * @task: task management task
18665e7ae7bSOr Gerlitz  *
18765e7ae7bSOr Gerlitz  * Notes:
18865e7ae7bSOr Gerlitz  *	The function can return -EAGAIN in which case caller must
18965e7ae7bSOr Gerlitz  *	call it again later, or recover. '0' return code means successful
19065e7ae7bSOr Gerlitz  *	xmit.
19165e7ae7bSOr Gerlitz  *
19265e7ae7bSOr Gerlitz  **/
19365e7ae7bSOr Gerlitz static int
1942261ec3dSMike Christie iscsi_iser_mtask_xmit(struct iscsi_conn *conn, struct iscsi_task *task)
19565e7ae7bSOr Gerlitz {
19665e7ae7bSOr Gerlitz 	int error = 0;
19765e7ae7bSOr Gerlitz 
198962b4b52SOr Gerlitz 	iser_dbg("mtask xmit [cid %d itt 0x%x]\n", conn->id, task->itt);
19965e7ae7bSOr Gerlitz 
2002261ec3dSMike Christie 	error = iser_send_control(conn, task);
20165e7ae7bSOr Gerlitz 
2022261ec3dSMike Christie 	/* since iser xmits control with zero copy, tasks can not be recycled
20365e7ae7bSOr Gerlitz 	 * right after sending them.
20465e7ae7bSOr Gerlitz 	 * The recycling scheme is based on whether a response is expected
2052261ec3dSMike Christie 	 * - if yes, the task is recycled at iscsi_complete_pdu
2062261ec3dSMike Christie 	 * - if no,  the task is recycled at iser_snd_completion
20765e7ae7bSOr Gerlitz 	 */
20865e7ae7bSOr Gerlitz 	return error;
20965e7ae7bSOr Gerlitz }
21065e7ae7bSOr Gerlitz 
21165e7ae7bSOr Gerlitz static int
2122747fdb2SMike Christie iscsi_iser_task_xmit_unsol_data(struct iscsi_conn *conn,
2132261ec3dSMike Christie 				 struct iscsi_task *task)
21465e7ae7bSOr Gerlitz {
2150f9c7449SMike Christie 	struct iscsi_r2t_info *r2t = &task->unsol_r2t;
21665e7ae7bSOr Gerlitz 	struct iscsi_data hdr;
21765e7ae7bSOr Gerlitz 	int error = 0;
21865e7ae7bSOr Gerlitz 
21965e7ae7bSOr Gerlitz 	/* Send data-out PDUs while there's still unsolicited data to send */
2200f9c7449SMike Christie 	while (iscsi_task_has_unsol_data(task)) {
2210f9c7449SMike Christie 		iscsi_prep_data_out_pdu(task, r2t, &hdr);
22248a237a2SMike Christie 		iser_dbg("Sending data-out: itt 0x%x, data count %d\n",
2230f9c7449SMike Christie 			   hdr.itt, r2t->data_count);
22465e7ae7bSOr Gerlitz 
22565e7ae7bSOr Gerlitz 		/* the buffer description has been passed with the command */
22665e7ae7bSOr Gerlitz 		/* Send the command */
2272261ec3dSMike Christie 		error = iser_send_data_out(conn, task, &hdr);
22865e7ae7bSOr Gerlitz 		if (error) {
2290f9c7449SMike Christie 			r2t->datasn--;
2302747fdb2SMike Christie 			goto iscsi_iser_task_xmit_unsol_data_exit;
23165e7ae7bSOr Gerlitz 		}
2320f9c7449SMike Christie 		r2t->sent += r2t->data_count;
23348a237a2SMike Christie 		iser_dbg("Need to send %d more as data-out PDUs\n",
2340f9c7449SMike Christie 			   r2t->data_length - r2t->sent);
23565e7ae7bSOr Gerlitz 	}
23665e7ae7bSOr Gerlitz 
2372747fdb2SMike Christie iscsi_iser_task_xmit_unsol_data_exit:
23865e7ae7bSOr Gerlitz 	return error;
23965e7ae7bSOr Gerlitz }
24065e7ae7bSOr Gerlitz 
24165e7ae7bSOr Gerlitz static int
2422261ec3dSMike Christie iscsi_iser_task_xmit(struct iscsi_task *task)
24365e7ae7bSOr Gerlitz {
2442261ec3dSMike Christie 	struct iscsi_conn *conn = task->conn;
2452261ec3dSMike Christie 	struct iscsi_iser_task *iser_task = task->dd_data;
24665e7ae7bSOr Gerlitz 	int error = 0;
24765e7ae7bSOr Gerlitz 
2482261ec3dSMike Christie 	if (!task->sc)
2492261ec3dSMike Christie 		return iscsi_iser_mtask_xmit(conn, task);
2502747fdb2SMike Christie 
2512261ec3dSMike Christie 	if (task->sc->sc_data_direction == DMA_TO_DEVICE) {
2522261ec3dSMike Christie 		BUG_ON(scsi_bufflen(task->sc) == 0);
25377a23c21SMike Christie 
25448a237a2SMike Christie 		iser_dbg("cmd [itt %x total %d imm %d unsol_data %d\n",
2552261ec3dSMike Christie 			   task->itt, scsi_bufflen(task->sc),
2560f9c7449SMike Christie 			   task->imm_count, task->unsol_r2t.data_length);
25777a23c21SMike Christie 	}
25877a23c21SMike Christie 
259962b4b52SOr Gerlitz 	iser_dbg("ctask xmit [cid %d itt 0x%x]\n",
2602261ec3dSMike Christie 		   conn->id, task->itt);
26165e7ae7bSOr Gerlitz 
26265e7ae7bSOr Gerlitz 	/* Send the cmd PDU */
2632261ec3dSMike Christie 	if (!iser_task->command_sent) {
2642261ec3dSMike Christie 		error = iser_send_command(conn, task);
26565e7ae7bSOr Gerlitz 		if (error)
2662747fdb2SMike Christie 			goto iscsi_iser_task_xmit_exit;
2672261ec3dSMike Christie 		iser_task->command_sent = 1;
26865e7ae7bSOr Gerlitz 	}
26965e7ae7bSOr Gerlitz 
27065e7ae7bSOr Gerlitz 	/* Send unsolicited data-out PDU(s) if necessary */
2710f9c7449SMike Christie 	if (iscsi_task_has_unsol_data(task))
2722261ec3dSMike Christie 		error = iscsi_iser_task_xmit_unsol_data(conn, task);
27365e7ae7bSOr Gerlitz 
2742747fdb2SMike Christie  iscsi_iser_task_xmit_exit:
27565e7ae7bSOr Gerlitz 	return error;
27665e7ae7bSOr Gerlitz }
27765e7ae7bSOr Gerlitz 
2780f9c7449SMike Christie static void iscsi_iser_cleanup_task(struct iscsi_task *task)
27965e7ae7bSOr Gerlitz {
2802261ec3dSMike Christie 	struct iscsi_iser_task *iser_task = task->dd_data;
28152439540SOr Gerlitz 	struct iser_tx_desc	*tx_desc = &iser_task->desc;
28252439540SOr Gerlitz 
28352439540SOr Gerlitz 	struct iscsi_iser_conn *iser_conn = task->conn->dd_data;
28452439540SOr Gerlitz 	struct iser_device     *device    = iser_conn->ib_conn->device;
28552439540SOr Gerlitz 
28652439540SOr Gerlitz 	ib_dma_unmap_single(device->ib_device,
28752439540SOr Gerlitz 		tx_desc->dma_addr, ISER_HEADERS_LEN, DMA_TO_DEVICE);
28865e7ae7bSOr Gerlitz 
289b3cd5050SMike Christie 	/* mgmt tasks do not need special cleanup */
290b3cd5050SMike Christie 	if (!task->sc)
2912747fdb2SMike Christie 		return;
29265e7ae7bSOr Gerlitz 
2932261ec3dSMike Christie 	if (iser_task->status == ISER_TASK_STATUS_STARTED) {
2942261ec3dSMike Christie 		iser_task->status = ISER_TASK_STATUS_COMPLETED;
2952261ec3dSMike Christie 		iser_task_rdma_finalize(iser_task);
29665e7ae7bSOr Gerlitz 	}
29765e7ae7bSOr Gerlitz }
29865e7ae7bSOr Gerlitz 
29965e7ae7bSOr Gerlitz static struct iscsi_cls_conn *
30065e7ae7bSOr Gerlitz iscsi_iser_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
30165e7ae7bSOr Gerlitz {
30265e7ae7bSOr Gerlitz 	struct iscsi_conn *conn;
30365e7ae7bSOr Gerlitz 	struct iscsi_cls_conn *cls_conn;
30465e7ae7bSOr Gerlitz 	struct iscsi_iser_conn *iser_conn;
30565e7ae7bSOr Gerlitz 
3065d91e209SMike Christie 	cls_conn = iscsi_conn_setup(cls_session, sizeof(*iser_conn), conn_idx);
30765e7ae7bSOr Gerlitz 	if (!cls_conn)
30865e7ae7bSOr Gerlitz 		return NULL;
30965e7ae7bSOr Gerlitz 	conn = cls_conn->dd_data;
31065e7ae7bSOr Gerlitz 
31165e7ae7bSOr Gerlitz 	/*
31265e7ae7bSOr Gerlitz 	 * due to issues with the login code re iser sematics
31365e7ae7bSOr Gerlitz 	 * this not set in iscsi_conn_setup - FIXME
31465e7ae7bSOr Gerlitz 	 */
315bcc60c38SOr Gerlitz 	conn->max_recv_dlength = ISER_RECV_DATA_SEG_LEN;
31665e7ae7bSOr Gerlitz 
3175d91e209SMike Christie 	iser_conn = conn->dd_data;
31865e7ae7bSOr Gerlitz 	conn->dd_data = iser_conn;
31965e7ae7bSOr Gerlitz 	iser_conn->iscsi_conn = conn;
32065e7ae7bSOr Gerlitz 
32165e7ae7bSOr Gerlitz 	return cls_conn;
32265e7ae7bSOr Gerlitz }
32365e7ae7bSOr Gerlitz 
32465e7ae7bSOr Gerlitz static void
32565e7ae7bSOr Gerlitz iscsi_iser_conn_destroy(struct iscsi_cls_conn *cls_conn)
32665e7ae7bSOr Gerlitz {
32765e7ae7bSOr Gerlitz 	struct iscsi_conn *conn = cls_conn->dd_data;
32865e7ae7bSOr Gerlitz 	struct iscsi_iser_conn *iser_conn = conn->dd_data;
329b40977d9SMike Christie 	struct iser_conn *ib_conn = iser_conn->ib_conn;
33065e7ae7bSOr Gerlitz 
33165e7ae7bSOr Gerlitz 	iscsi_conn_teardown(cls_conn);
332b40977d9SMike Christie 	/*
333b40977d9SMike Christie 	 * Userspace will normally call the stop callback and
334b40977d9SMike Christie 	 * already have freed the ib_conn, but if it goofed up then
335b40977d9SMike Christie 	 * we free it here.
336b40977d9SMike Christie 	 */
337b40977d9SMike Christie 	if (ib_conn) {
338b40977d9SMike Christie 		ib_conn->iser_conn = NULL;
33939ff05dbSOr Gerlitz 		iser_conn_put(ib_conn, 1); /* deref iscsi/ib conn unbinding */
340b40977d9SMike Christie 	}
34165e7ae7bSOr Gerlitz }
34265e7ae7bSOr Gerlitz 
34365e7ae7bSOr Gerlitz static int
34465e7ae7bSOr Gerlitz iscsi_iser_conn_bind(struct iscsi_cls_session *cls_session,
34565e7ae7bSOr Gerlitz 		     struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
34665e7ae7bSOr Gerlitz 		     int is_leading)
34765e7ae7bSOr Gerlitz {
34865e7ae7bSOr Gerlitz 	struct iscsi_conn *conn = cls_conn->dd_data;
34965e7ae7bSOr Gerlitz 	struct iscsi_iser_conn *iser_conn;
35065e7ae7bSOr Gerlitz 	struct iser_conn *ib_conn;
351412eeafaSMike Christie 	struct iscsi_endpoint *ep;
35265e7ae7bSOr Gerlitz 	int error;
35365e7ae7bSOr Gerlitz 
35465e7ae7bSOr Gerlitz 	error = iscsi_conn_bind(cls_session, cls_conn, is_leading);
35565e7ae7bSOr Gerlitz 	if (error)
35665e7ae7bSOr Gerlitz 		return error;
35765e7ae7bSOr Gerlitz 
35865e7ae7bSOr Gerlitz 	/* the transport ep handle comes from user space so it must be
35965e7ae7bSOr Gerlitz 	 * verified against the global ib connections list */
360412eeafaSMike Christie 	ep = iscsi_lookup_endpoint(transport_eph);
361412eeafaSMike Christie 	if (!ep) {
36265e7ae7bSOr Gerlitz 		iser_err("can't bind eph %llx\n",
36365e7ae7bSOr Gerlitz 			 (unsigned long long)transport_eph);
36465e7ae7bSOr Gerlitz 		return -EINVAL;
36565e7ae7bSOr Gerlitz 	}
366412eeafaSMike Christie 	ib_conn = ep->dd_data;
367412eeafaSMike Christie 
36889e984e2SOr Gerlitz 	if (iser_alloc_rx_descriptors(ib_conn))
36989e984e2SOr Gerlitz 		return -ENOMEM;
37089e984e2SOr Gerlitz 
37165e7ae7bSOr Gerlitz 	/* binds the iSER connection retrieved from the previously
37265e7ae7bSOr Gerlitz 	 * connected ep_handle to the iSCSI layer connection. exchanges
37365e7ae7bSOr Gerlitz 	 * connection pointers */
3744f363882SRoi Dayan 	iser_info("binding iscsi/iser conn %p %p to ib_conn %p\n",
37539ff05dbSOr Gerlitz 		  conn, conn->dd_data, ib_conn);
37665e7ae7bSOr Gerlitz 	iser_conn = conn->dd_data;
37765e7ae7bSOr Gerlitz 	ib_conn->iser_conn = iser_conn;
37865e7ae7bSOr Gerlitz 	iser_conn->ib_conn  = ib_conn;
37939ff05dbSOr Gerlitz 	iser_conn_get(ib_conn); /* ref iscsi/ib conn binding */
38065e7ae7bSOr Gerlitz 	return 0;
38165e7ae7bSOr Gerlitz }
38265e7ae7bSOr Gerlitz 
383b40977d9SMike Christie static void
384b40977d9SMike Christie iscsi_iser_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
385b40977d9SMike Christie {
386b40977d9SMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
387b40977d9SMike Christie 	struct iscsi_iser_conn *iser_conn = conn->dd_data;
388b40977d9SMike Christie 	struct iser_conn *ib_conn = iser_conn->ib_conn;
389b40977d9SMike Christie 
390913e5bf4SMike Christie 	/*
391913e5bf4SMike Christie 	 * Userspace may have goofed up and not bound the connection or
392913e5bf4SMike Christie 	 * might have only partially setup the connection.
393913e5bf4SMike Christie 	 */
394913e5bf4SMike Christie 	if (ib_conn) {
395b40977d9SMike Christie 		iscsi_conn_stop(cls_conn, flag);
396b40977d9SMike Christie 		/*
397b40977d9SMike Christie 		 * There is no unbind event so the stop callback
398b40977d9SMike Christie 		 * must release the ref from the bind.
399b40977d9SMike Christie 		 */
40039ff05dbSOr Gerlitz 		iser_conn_put(ib_conn, 1); /* deref iscsi/ib conn unbinding */
401913e5bf4SMike Christie 	}
402b40977d9SMike Christie 	iser_conn->ib_conn = NULL;
403b40977d9SMike Christie }
404b40977d9SMike Christie 
40575613521SMike Christie static void iscsi_iser_session_destroy(struct iscsi_cls_session *cls_session)
40675613521SMike Christie {
40775613521SMike Christie 	struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
40875613521SMike Christie 
409e5bd7b54SMike Christie 	iscsi_session_teardown(cls_session);
410a4804cd6SMike Christie 	iscsi_host_remove(shost);
411a4804cd6SMike Christie 	iscsi_host_free(shost);
41275613521SMike Christie }
41365e7ae7bSOr Gerlitz 
41465e7ae7bSOr Gerlitz static struct iscsi_cls_session *
415412eeafaSMike Christie iscsi_iser_session_create(struct iscsi_endpoint *ep,
4161548271eSMike Christie 			  uint16_t cmds_max, uint16_t qdepth,
4175e7facb7SMike Christie 			  uint32_t initial_cmdsn)
41865e7ae7bSOr Gerlitz {
41965e7ae7bSOr Gerlitz 	struct iscsi_cls_session *cls_session;
42065e7ae7bSOr Gerlitz 	struct iscsi_session *session;
421412eeafaSMike Christie 	struct Scsi_Host *shost;
422412eeafaSMike Christie 	struct iser_conn *ib_conn;
42375613521SMike Christie 
424962b4b52SOr Gerlitz 	shost = iscsi_host_alloc(&iscsi_iser_sht, 0, 0);
42575613521SMike Christie 	if (!shost)
42675613521SMike Christie 		return NULL;
42775613521SMike Christie 	shost->transportt = iscsi_iser_scsi_transport;
42875613521SMike Christie 	shost->max_lun = iscsi_max_lun;
42975613521SMike Christie 	shost->max_id = 0;
43075613521SMike Christie 	shost->max_channel = 0;
43175613521SMike Christie 	shost->max_cmd_len = 16;
43275613521SMike Christie 
433412eeafaSMike Christie 	/*
434412eeafaSMike Christie 	 * older userspace tools (before 2.0-870) did not pass us
435412eeafaSMike Christie 	 * the leading conn's ep so this will be NULL;
436412eeafaSMike Christie 	 */
437412eeafaSMike Christie 	if (ep)
438412eeafaSMike Christie 		ib_conn = ep->dd_data;
439412eeafaSMike Christie 
440412eeafaSMike Christie 	if (iscsi_host_add(shost,
441412eeafaSMike Christie 			   ep ? ib_conn->device->ib_device->dma_device : NULL))
44275613521SMike Christie 		goto free_host;
44365e7ae7bSOr Gerlitz 
4441548271eSMike Christie 	/*
4451548271eSMike Christie 	 * we do not support setting can_queue cmd_per_lun from userspace yet
4461548271eSMike Christie 	 * because we preallocate so many resources
4471548271eSMike Christie 	 */
44875613521SMike Christie 	cls_session = iscsi_session_setup(&iscsi_iser_transport, shost,
449b8b9e1b8SJayamohan Kallickal 					  ISCSI_DEF_XMIT_CMDS_MAX, 0,
4502261ec3dSMike Christie 					  sizeof(struct iscsi_iser_task),
4517970634bSMike Christie 					  initial_cmdsn, 0);
45265e7ae7bSOr Gerlitz 	if (!cls_session)
45375613521SMike Christie 		goto remove_host;
45475613521SMike Christie 	session = cls_session->dd_data;
45565e7ae7bSOr Gerlitz 
4562747fdb2SMike Christie 	shost->can_queue = session->scsi_cmds_max;
45765e7ae7bSOr Gerlitz 	return cls_session;
45875613521SMike Christie 
45975613521SMike Christie remove_host:
460a4804cd6SMike Christie 	iscsi_host_remove(shost);
46175613521SMike Christie free_host:
462a4804cd6SMike Christie 	iscsi_host_free(shost);
46375613521SMike Christie 	return NULL;
46465e7ae7bSOr Gerlitz }
46565e7ae7bSOr Gerlitz 
46665e7ae7bSOr Gerlitz static int
467358ff019SMike Christie iscsi_iser_set_param(struct iscsi_cls_conn *cls_conn,
468358ff019SMike Christie 		     enum iscsi_param param, char *buf, int buflen)
46965e7ae7bSOr Gerlitz {
470358ff019SMike Christie 	int value;
47165e7ae7bSOr Gerlitz 
47265e7ae7bSOr Gerlitz 	switch (param) {
47365e7ae7bSOr Gerlitz 	case ISCSI_PARAM_MAX_RECV_DLENGTH:
47465e7ae7bSOr Gerlitz 		/* TBD */
47565e7ae7bSOr Gerlitz 		break;
47665e7ae7bSOr Gerlitz 	case ISCSI_PARAM_HDRDGST_EN:
477358ff019SMike Christie 		sscanf(buf, "%d", &value);
47865e7ae7bSOr Gerlitz 		if (value) {
4794f363882SRoi Dayan 			iser_err("DataDigest wasn't negotiated to None");
48065e7ae7bSOr Gerlitz 			return -EPROTO;
48165e7ae7bSOr Gerlitz 		}
48265e7ae7bSOr Gerlitz 		break;
48365e7ae7bSOr Gerlitz 	case ISCSI_PARAM_DATADGST_EN:
484358ff019SMike Christie 		sscanf(buf, "%d", &value);
48565e7ae7bSOr Gerlitz 		if (value) {
4864f363882SRoi Dayan 			iser_err("DataDigest wasn't negotiated to None");
48765e7ae7bSOr Gerlitz 			return -EPROTO;
48865e7ae7bSOr Gerlitz 		}
48965e7ae7bSOr Gerlitz 		break;
49065e7ae7bSOr Gerlitz 	case ISCSI_PARAM_IFMARKER_EN:
491358ff019SMike Christie 		sscanf(buf, "%d", &value);
49265e7ae7bSOr Gerlitz 		if (value) {
4934f363882SRoi Dayan 			iser_err("IFMarker wasn't negotiated to No");
49465e7ae7bSOr Gerlitz 			return -EPROTO;
49565e7ae7bSOr Gerlitz 		}
49665e7ae7bSOr Gerlitz 		break;
49765e7ae7bSOr Gerlitz 	case ISCSI_PARAM_OFMARKER_EN:
498358ff019SMike Christie 		sscanf(buf, "%d", &value);
49965e7ae7bSOr Gerlitz 		if (value) {
5004f363882SRoi Dayan 			iser_err("OFMarker wasn't negotiated to No");
50165e7ae7bSOr Gerlitz 			return -EPROTO;
50265e7ae7bSOr Gerlitz 		}
50365e7ae7bSOr Gerlitz 		break;
50465e7ae7bSOr Gerlitz 	default:
505358ff019SMike Christie 		return iscsi_set_param(cls_conn, param, buf, buflen);
50665e7ae7bSOr Gerlitz 	}
50765e7ae7bSOr Gerlitz 
50865e7ae7bSOr Gerlitz 	return 0;
50965e7ae7bSOr Gerlitz }
51065e7ae7bSOr Gerlitz 
51165e7ae7bSOr Gerlitz static void
51265e7ae7bSOr Gerlitz iscsi_iser_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
51365e7ae7bSOr Gerlitz {
51465e7ae7bSOr Gerlitz 	struct iscsi_conn *conn = cls_conn->dd_data;
51565e7ae7bSOr Gerlitz 
51665e7ae7bSOr Gerlitz 	stats->txdata_octets = conn->txdata_octets;
51765e7ae7bSOr Gerlitz 	stats->rxdata_octets = conn->rxdata_octets;
51865e7ae7bSOr Gerlitz 	stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
51965e7ae7bSOr Gerlitz 	stats->dataout_pdus = conn->dataout_pdus_cnt;
52065e7ae7bSOr Gerlitz 	stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
52165e7ae7bSOr Gerlitz 	stats->datain_pdus = conn->datain_pdus_cnt; /* always 0 */
52265e7ae7bSOr Gerlitz 	stats->r2t_pdus = conn->r2t_pdus_cnt; /* always 0 */
52365e7ae7bSOr Gerlitz 	stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
52465e7ae7bSOr Gerlitz 	stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
52587528227SEli Dorfman 	stats->custom_length = 4;
52665e7ae7bSOr Gerlitz 	strcpy(stats->custom[0].desc, "qp_tx_queue_full");
52765e7ae7bSOr Gerlitz 	stats->custom[0].value = 0; /* TB iser_conn->qp_tx_queue_full; */
52865e7ae7bSOr Gerlitz 	strcpy(stats->custom[1].desc, "fmr_map_not_avail");
52965e7ae7bSOr Gerlitz 	stats->custom[1].value = 0; /* TB iser_conn->fmr_map_not_avail */;
53065e7ae7bSOr Gerlitz 	strcpy(stats->custom[2].desc, "eh_abort_cnt");
53165e7ae7bSOr Gerlitz 	stats->custom[2].value = conn->eh_abort_cnt;
53287528227SEli Dorfman 	strcpy(stats->custom[3].desc, "fmr_unalign_cnt");
53387528227SEli Dorfman 	stats->custom[3].value = conn->fmr_unalign_cnt;
53465e7ae7bSOr Gerlitz }
53565e7ae7bSOr Gerlitz 
5367c53c6f8SMike Christie static int iscsi_iser_get_ep_param(struct iscsi_endpoint *ep,
5377c53c6f8SMike Christie 				   enum iscsi_param param, char *buf)
5387c53c6f8SMike Christie {
5397c53c6f8SMike Christie 	struct iser_conn *ib_conn = ep->dd_data;
5407c53c6f8SMike Christie 	int len;
5417c53c6f8SMike Christie 
5427c53c6f8SMike Christie 	switch (param) {
5437c53c6f8SMike Christie 	case ISCSI_PARAM_CONN_PORT:
5447c53c6f8SMike Christie 	case ISCSI_PARAM_CONN_ADDRESS:
5457c53c6f8SMike Christie 		if (!ib_conn || !ib_conn->cma_id)
5467c53c6f8SMike Christie 			return -ENOTCONN;
5477c53c6f8SMike Christie 
5487c53c6f8SMike Christie 		return iscsi_conn_get_addr_param((struct sockaddr_storage *)
5497c53c6f8SMike Christie 					&ib_conn->cma_id->route.addr.dst_addr,
5507c53c6f8SMike Christie 					param, buf);
5517c53c6f8SMike Christie 		break;
5527c53c6f8SMike Christie 	default:
5537c53c6f8SMike Christie 		return -ENOSYS;
5547c53c6f8SMike Christie 	}
5557c53c6f8SMike Christie 
5567c53c6f8SMike Christie 	return len;
5577c53c6f8SMike Christie }
5587c53c6f8SMike Christie 
559412eeafaSMike Christie static struct iscsi_endpoint *
56010eb0f01SMike Christie iscsi_iser_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
56110eb0f01SMike Christie 		      int non_blocking)
56265e7ae7bSOr Gerlitz {
56365e7ae7bSOr Gerlitz 	int err;
56465e7ae7bSOr Gerlitz 	struct iser_conn *ib_conn;
565412eeafaSMike Christie 	struct iscsi_endpoint *ep;
56665e7ae7bSOr Gerlitz 
567412eeafaSMike Christie 	ep = iscsi_create_endpoint(sizeof(*ib_conn));
568412eeafaSMike Christie 	if (!ep)
569412eeafaSMike Christie 		return ERR_PTR(-ENOMEM);
57065e7ae7bSOr Gerlitz 
571412eeafaSMike Christie 	ib_conn = ep->dd_data;
572412eeafaSMike Christie 	ib_conn->ep = ep;
573412eeafaSMike Christie 	iser_conn_init(ib_conn);
57465e7ae7bSOr Gerlitz 
575412eeafaSMike Christie 	err = iser_connect(ib_conn, NULL, (struct sockaddr_in *)dst_addr,
576412eeafaSMike Christie 			   non_blocking);
5777d9c0de4SOr Gerlitz 	if (err)
578412eeafaSMike Christie 		return ERR_PTR(err);
5797d9c0de4SOr Gerlitz 
580412eeafaSMike Christie 	return ep;
58165e7ae7bSOr Gerlitz }
58265e7ae7bSOr Gerlitz 
58365e7ae7bSOr Gerlitz static int
584412eeafaSMike Christie iscsi_iser_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
58565e7ae7bSOr Gerlitz {
586412eeafaSMike Christie 	struct iser_conn *ib_conn;
58765e7ae7bSOr Gerlitz 	int rc;
58865e7ae7bSOr Gerlitz 
589412eeafaSMike Christie 	ib_conn = ep->dd_data;
59065e7ae7bSOr Gerlitz 	rc = wait_event_interruptible_timeout(ib_conn->wait,
59165e7ae7bSOr Gerlitz 			     ib_conn->state == ISER_CONN_UP,
59265e7ae7bSOr Gerlitz 			     msecs_to_jiffies(timeout_ms));
59365e7ae7bSOr Gerlitz 
59465e7ae7bSOr Gerlitz 	/* if conn establishment failed, return error code to iscsi */
59565e7ae7bSOr Gerlitz 	if (!rc &&
59665e7ae7bSOr Gerlitz 	    (ib_conn->state == ISER_CONN_TERMINATING ||
59765e7ae7bSOr Gerlitz 	     ib_conn->state == ISER_CONN_DOWN))
59865e7ae7bSOr Gerlitz 		rc = -1;
59965e7ae7bSOr Gerlitz 
6004f363882SRoi Dayan 	iser_info("ib conn %p rc = %d\n", ib_conn, rc);
60165e7ae7bSOr Gerlitz 
60265e7ae7bSOr Gerlitz 	if (rc > 0)
60365e7ae7bSOr Gerlitz 		return 1; /* success, this is the equivalent of POLLOUT */
60465e7ae7bSOr Gerlitz 	else if (!rc)
60565e7ae7bSOr Gerlitz 		return 0; /* timeout */
60665e7ae7bSOr Gerlitz 	else
60765e7ae7bSOr Gerlitz 		return rc; /* signal */
60865e7ae7bSOr Gerlitz }
60965e7ae7bSOr Gerlitz 
61065e7ae7bSOr Gerlitz static void
611412eeafaSMike Christie iscsi_iser_ep_disconnect(struct iscsi_endpoint *ep)
61265e7ae7bSOr Gerlitz {
6131c83469dSMike Christie 	struct iser_conn *ib_conn;
61465e7ae7bSOr Gerlitz 
615412eeafaSMike Christie 	ib_conn = ep->dd_data;
616b40977d9SMike Christie 	if (ib_conn->iser_conn)
617b40977d9SMike Christie 		/*
618b40977d9SMike Christie 		 * Must suspend xmit path if the ep is bound to the
619b40977d9SMike Christie 		 * iscsi_conn, so we know we are not accessing the ib_conn
620b40977d9SMike Christie 		 * when we free it.
621b40977d9SMike Christie 		 *
622b40977d9SMike Christie 		 * This may not be bound if the ep poll failed.
623b40977d9SMike Christie 		 */
624b40977d9SMike Christie 		iscsi_suspend_tx(ib_conn->iser_conn->iscsi_conn);
625b40977d9SMike Christie 
62665e7ae7bSOr Gerlitz 
6274f363882SRoi Dayan 	iser_info("ib conn %p state %d\n", ib_conn, ib_conn->state);
62865e7ae7bSOr Gerlitz 	iser_conn_terminate(ib_conn);
62965e7ae7bSOr Gerlitz }
63065e7ae7bSOr Gerlitz 
631587a1f16SAl Viro static umode_t iser_attr_is_visible(int param_type, int param)
6323128c6c7SMike Christie {
6333128c6c7SMike Christie 	switch (param_type) {
634f27fb2efSMike Christie 	case ISCSI_HOST_PARAM:
635f27fb2efSMike Christie 		switch (param) {
636f27fb2efSMike Christie 		case ISCSI_HOST_PARAM_NETDEV_NAME:
637f27fb2efSMike Christie 		case ISCSI_HOST_PARAM_HWADDRESS:
638f27fb2efSMike Christie 		case ISCSI_HOST_PARAM_INITIATOR_NAME:
639f27fb2efSMike Christie 			return S_IRUGO;
640f27fb2efSMike Christie 		default:
641f27fb2efSMike Christie 			return 0;
642f27fb2efSMike Christie 		}
6433128c6c7SMike Christie 	case ISCSI_PARAM:
6443128c6c7SMike Christie 		switch (param) {
6453128c6c7SMike Christie 		case ISCSI_PARAM_MAX_RECV_DLENGTH:
6463128c6c7SMike Christie 		case ISCSI_PARAM_MAX_XMIT_DLENGTH:
6473128c6c7SMike Christie 		case ISCSI_PARAM_HDRDGST_EN:
6483128c6c7SMike Christie 		case ISCSI_PARAM_DATADGST_EN:
6493128c6c7SMike Christie 		case ISCSI_PARAM_CONN_ADDRESS:
6503128c6c7SMike Christie 		case ISCSI_PARAM_CONN_PORT:
6513128c6c7SMike Christie 		case ISCSI_PARAM_EXP_STATSN:
6523128c6c7SMike Christie 		case ISCSI_PARAM_PERSISTENT_ADDRESS:
6533128c6c7SMike Christie 		case ISCSI_PARAM_PERSISTENT_PORT:
6543128c6c7SMike Christie 		case ISCSI_PARAM_PING_TMO:
6553128c6c7SMike Christie 		case ISCSI_PARAM_RECV_TMO:
6561d063c17SMike Christie 		case ISCSI_PARAM_INITIAL_R2T_EN:
6571d063c17SMike Christie 		case ISCSI_PARAM_MAX_R2T:
6581d063c17SMike Christie 		case ISCSI_PARAM_IMM_DATA_EN:
6591d063c17SMike Christie 		case ISCSI_PARAM_FIRST_BURST:
6601d063c17SMike Christie 		case ISCSI_PARAM_MAX_BURST:
6611d063c17SMike Christie 		case ISCSI_PARAM_PDU_INORDER_EN:
6621d063c17SMike Christie 		case ISCSI_PARAM_DATASEQ_INORDER_EN:
6631d063c17SMike Christie 		case ISCSI_PARAM_TARGET_NAME:
6641d063c17SMike Christie 		case ISCSI_PARAM_TPGT:
6651d063c17SMike Christie 		case ISCSI_PARAM_USERNAME:
6661d063c17SMike Christie 		case ISCSI_PARAM_PASSWORD:
6671d063c17SMike Christie 		case ISCSI_PARAM_USERNAME_IN:
6681d063c17SMike Christie 		case ISCSI_PARAM_PASSWORD_IN:
6691d063c17SMike Christie 		case ISCSI_PARAM_FAST_ABORT:
6701d063c17SMike Christie 		case ISCSI_PARAM_ABORT_TMO:
6711d063c17SMike Christie 		case ISCSI_PARAM_LU_RESET_TMO:
6721d063c17SMike Christie 		case ISCSI_PARAM_TGT_RESET_TMO:
6731d063c17SMike Christie 		case ISCSI_PARAM_IFACE_NAME:
6741d063c17SMike Christie 		case ISCSI_PARAM_INITIATOR_NAME:
6753128c6c7SMike Christie 			return S_IRUGO;
6763128c6c7SMike Christie 		default:
6773128c6c7SMike Christie 			return 0;
6783128c6c7SMike Christie 		}
6793128c6c7SMike Christie 	}
6803128c6c7SMike Christie 
6813128c6c7SMike Christie 	return 0;
6823128c6c7SMike Christie }
6833128c6c7SMike Christie 
68465e7ae7bSOr Gerlitz static struct scsi_host_template iscsi_iser_sht = {
6857974392cSMike Christie 	.module                 = THIS_MODULE,
686c1d786e6SRoi Dayan 	.name                   = "iSCSI Initiator over iSER",
68765e7ae7bSOr Gerlitz 	.queuecommand           = iscsi_queuecommand,
6886410627eSErez Zilber 	.change_queue_depth	= iscsi_change_queue_depth,
68965e7ae7bSOr Gerlitz 	.sg_tablesize           = ISCSI_ISER_SG_TABLESIZE,
6908072ec2fSErez Zilber 	.max_sectors		= 1024,
691e28f3d5bSMike Christie 	.cmd_per_lun            = ISER_DEF_CMD_PER_LUN,
69265e7ae7bSOr Gerlitz 	.eh_abort_handler       = iscsi_eh_abort,
69390c18f3cSErez Zilber 	.eh_device_reset_handler= iscsi_eh_device_reset,
694309ce156SJayamohan Kallickal 	.eh_target_reset_handler = iscsi_eh_recover_target,
6956b5d6c44SMike Christie 	.target_alloc		= iscsi_target_alloc,
69665e7ae7bSOr Gerlitz 	.use_clustering         = DISABLE_CLUSTERING,
69765e7ae7bSOr Gerlitz 	.proc_name              = "iscsi_iser",
69865e7ae7bSOr Gerlitz 	.this_id                = -1,
69965e7ae7bSOr Gerlitz };
70065e7ae7bSOr Gerlitz 
70165e7ae7bSOr Gerlitz static struct iscsi_transport iscsi_iser_transport = {
70265e7ae7bSOr Gerlitz 	.owner                  = THIS_MODULE,
70365e7ae7bSOr Gerlitz 	.name                   = "iser",
70465e7ae7bSOr Gerlitz 	.caps                   = CAP_RECOVERY_L0 | CAP_MULTI_R2T,
70565e7ae7bSOr Gerlitz 	/* session management */
70665e7ae7bSOr Gerlitz 	.create_session         = iscsi_iser_session_create,
70775613521SMike Christie 	.destroy_session        = iscsi_iser_session_destroy,
70865e7ae7bSOr Gerlitz 	/* connection management */
70965e7ae7bSOr Gerlitz 	.create_conn            = iscsi_iser_conn_create,
71065e7ae7bSOr Gerlitz 	.bind_conn              = iscsi_iser_conn_bind,
71165e7ae7bSOr Gerlitz 	.destroy_conn           = iscsi_iser_conn_destroy,
7123128c6c7SMike Christie 	.attr_is_visible	= iser_attr_is_visible,
713358ff019SMike Christie 	.set_param              = iscsi_iser_set_param,
714358ff019SMike Christie 	.get_conn_param		= iscsi_conn_get_param,
7157c53c6f8SMike Christie 	.get_ep_param		= iscsi_iser_get_ep_param,
716358ff019SMike Christie 	.get_session_param	= iscsi_session_get_param,
71789e984e2SOr Gerlitz 	.start_conn             = iscsi_conn_start,
718b40977d9SMike Christie 	.stop_conn              = iscsi_iser_conn_stop,
7190801c242SMike Christie 	/* iscsi host params */
7200801c242SMike Christie 	.get_host_param		= iscsi_host_get_param,
7210801c242SMike Christie 	.set_host_param		= iscsi_host_set_param,
72265e7ae7bSOr Gerlitz 	/* IO */
72365e7ae7bSOr Gerlitz 	.send_pdu		= iscsi_conn_send_pdu,
72465e7ae7bSOr Gerlitz 	.get_stats		= iscsi_iser_conn_get_stats,
7252747fdb2SMike Christie 	.init_task		= iscsi_iser_task_init,
7262747fdb2SMike Christie 	.xmit_task		= iscsi_iser_task_xmit,
7272747fdb2SMike Christie 	.cleanup_task		= iscsi_iser_cleanup_task,
7280f9c7449SMike Christie 	.alloc_pdu		= iscsi_iser_pdu_alloc,
72965e7ae7bSOr Gerlitz 	/* recovery */
73065e7ae7bSOr Gerlitz 	.session_recovery_timedout = iscsi_session_recovery_timedout,
73165e7ae7bSOr Gerlitz 
73265e7ae7bSOr Gerlitz 	.ep_connect             = iscsi_iser_ep_connect,
73365e7ae7bSOr Gerlitz 	.ep_poll                = iscsi_iser_ep_poll,
73465e7ae7bSOr Gerlitz 	.ep_disconnect          = iscsi_iser_ep_disconnect
73565e7ae7bSOr Gerlitz };
73665e7ae7bSOr Gerlitz 
73765e7ae7bSOr Gerlitz static int __init iser_init(void)
73865e7ae7bSOr Gerlitz {
73965e7ae7bSOr Gerlitz 	int err;
74065e7ae7bSOr Gerlitz 
74165e7ae7bSOr Gerlitz 	iser_dbg("Starting iSER datamover...\n");
74265e7ae7bSOr Gerlitz 
74365e7ae7bSOr Gerlitz 	if (iscsi_max_lun < 1) {
7444f363882SRoi Dayan 		iser_err("Invalid max_lun value of %u\n", iscsi_max_lun);
74565e7ae7bSOr Gerlitz 		return -EINVAL;
74665e7ae7bSOr Gerlitz 	}
74765e7ae7bSOr Gerlitz 
74865e7ae7bSOr Gerlitz 	memset(&ig, 0, sizeof(struct iser_global));
74965e7ae7bSOr Gerlitz 
75065e7ae7bSOr Gerlitz 	ig.desc_cache = kmem_cache_create("iser_descriptors",
751f19624aaSOr Gerlitz 					  sizeof(struct iser_tx_desc),
75265e7ae7bSOr Gerlitz 					  0, SLAB_HWCACHE_ALIGN,
75320c2df83SPaul Mundt 					  NULL);
75465e7ae7bSOr Gerlitz 	if (ig.desc_cache == NULL)
75565e7ae7bSOr Gerlitz 		return -ENOMEM;
75665e7ae7bSOr Gerlitz 
75765e7ae7bSOr Gerlitz 	/* device init is called only after the first addr resolution */
75865e7ae7bSOr Gerlitz 	mutex_init(&ig.device_list_mutex);
75965e7ae7bSOr Gerlitz 	INIT_LIST_HEAD(&ig.device_list);
76065e7ae7bSOr Gerlitz 	mutex_init(&ig.connlist_mutex);
76165e7ae7bSOr Gerlitz 	INIT_LIST_HEAD(&ig.connlist);
76265e7ae7bSOr Gerlitz 
76375613521SMike Christie 	iscsi_iser_scsi_transport = iscsi_register_transport(
76475613521SMike Christie 							&iscsi_iser_transport);
76575613521SMike Christie 	if (!iscsi_iser_scsi_transport) {
76665e7ae7bSOr Gerlitz 		iser_err("iscsi_register_transport failed\n");
76765e7ae7bSOr Gerlitz 		err = -EINVAL;
76865e7ae7bSOr Gerlitz 		goto register_transport_failure;
76965e7ae7bSOr Gerlitz 	}
77065e7ae7bSOr Gerlitz 
77165e7ae7bSOr Gerlitz 	return 0;
77265e7ae7bSOr Gerlitz 
77365e7ae7bSOr Gerlitz register_transport_failure:
77465e7ae7bSOr Gerlitz 	kmem_cache_destroy(ig.desc_cache);
77565e7ae7bSOr Gerlitz 
77665e7ae7bSOr Gerlitz 	return err;
77765e7ae7bSOr Gerlitz }
77865e7ae7bSOr Gerlitz 
77965e7ae7bSOr Gerlitz static void __exit iser_exit(void)
78065e7ae7bSOr Gerlitz {
78165e7ae7bSOr Gerlitz 	iser_dbg("Removing iSER datamover...\n");
78265e7ae7bSOr Gerlitz 	iscsi_unregister_transport(&iscsi_iser_transport);
78365e7ae7bSOr Gerlitz 	kmem_cache_destroy(ig.desc_cache);
78465e7ae7bSOr Gerlitz }
78565e7ae7bSOr Gerlitz 
78665e7ae7bSOr Gerlitz module_init(iser_init);
78765e7ae7bSOr Gerlitz module_exit(iser_exit);
788