xref: /linux/drivers/infiniband/ulp/iser/iscsi_iser.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
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.
83ee07d27SOr Gerlitz  * Copyright (c) 2013-2014 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 
657c0f6ba6SLinus Torvalds #include <linux/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 
77c1d786e6SRoi Dayan MODULE_DESCRIPTION("iSER (iSCSI Extensions for RDMA) Datamover");
7865e7ae7bSOr Gerlitz MODULE_LICENSE("Dual BSD/GPL");
7965e7ae7bSOr Gerlitz MODULE_AUTHOR("Alex Nezhinsky, Dan Bar Dov, Or Gerlitz");
8065e7ae7bSOr Gerlitz 
81*80602acaSBart Van Assche static const struct scsi_host_template iscsi_iser_sht;
82db0a6cbdSJenny Falkovich static struct iscsi_transport iscsi_iser_transport;
83db0a6cbdSJenny Falkovich static struct scsi_transport_template *iscsi_iser_scsi_transport;
84b73c3adaSAriel Nahum static struct workqueue_struct *release_wq;
85c8c16d3bSVladimir Neyelov static DEFINE_MUTEX(unbind_iser_conn_mutex);
8665e7ae7bSOr Gerlitz struct iser_global ig;
8765e7ae7bSOr Gerlitz 
88db0a6cbdSJenny Falkovich int iser_debug_level = 0;
89db0a6cbdSJenny Falkovich module_param_named(debug_level, iser_debug_level, int, S_IRUGO | S_IWUSR);
90db0a6cbdSJenny Falkovich MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0 (default:disabled)");
91db0a6cbdSJenny Falkovich 
92429c7613SMax Gurtovoy static int iscsi_iser_set(const char *val, const struct kernel_param *kp);
93429c7613SMax Gurtovoy static const struct kernel_param_ops iscsi_iser_size_ops = {
94429c7613SMax Gurtovoy 	.set = iscsi_iser_set,
95429c7613SMax Gurtovoy 	.get = param_get_uint,
96429c7613SMax Gurtovoy };
97429c7613SMax Gurtovoy 
98db0a6cbdSJenny Falkovich static unsigned int iscsi_max_lun = 512;
99429c7613SMax Gurtovoy module_param_cb(max_lun, &iscsi_iser_size_ops, &iscsi_max_lun, S_IRUGO);
100429c7613SMax Gurtovoy MODULE_PARM_DESC(max_lun, "Max LUNs to allow per session, should > 0 (default:512)");
101db0a6cbdSJenny Falkovich 
102df749cdcSSagi Grimberg unsigned int iser_max_sectors = ISER_DEF_MAX_SECTORS;
1036bd898baSMax Gurtovoy module_param_cb(max_sectors, &iscsi_iser_size_ops, &iser_max_sectors,
1046bd898baSMax Gurtovoy 		S_IRUGO | S_IWUSR);
1056bd898baSMax Gurtovoy MODULE_PARM_DESC(max_sectors, "Max number of sectors in a single scsi command, should > 0 (default:1024)");
106df749cdcSSagi Grimberg 
1073cffd930SSagi Grimberg bool iser_always_reg = true;
1083cffd930SSagi Grimberg module_param_named(always_register, iser_always_reg, bool, S_IRUGO);
1093cffd930SSagi Grimberg MODULE_PARM_DESC(always_register,
1103cffd930SSagi Grimberg 		 "Always register memory, even for continuous memory regions (default:true)");
1113cffd930SSagi Grimberg 
112db0a6cbdSJenny Falkovich bool iser_pi_enable = false;
113db0a6cbdSJenny Falkovich module_param_named(pi_enable, iser_pi_enable, bool, S_IRUGO);
114db0a6cbdSJenny Falkovich MODULE_PARM_DESC(pi_enable, "Enable T10-PI offload support (default:disabled)");
115db0a6cbdSJenny Falkovich 
iscsi_iser_set(const char * val,const struct kernel_param * kp)116429c7613SMax Gurtovoy static int iscsi_iser_set(const char *val, const struct kernel_param *kp)
117429c7613SMax Gurtovoy {
118429c7613SMax Gurtovoy 	int ret;
119429c7613SMax Gurtovoy 	unsigned int n = 0;
120429c7613SMax Gurtovoy 
121429c7613SMax Gurtovoy 	ret = kstrtouint(val, 10, &n);
122429c7613SMax Gurtovoy 	if (ret != 0 || n == 0)
123429c7613SMax Gurtovoy 		return -EINVAL;
124429c7613SMax Gurtovoy 
125429c7613SMax Gurtovoy 	return param_set_uint(val, kp);
126429c7613SMax Gurtovoy }
127429c7613SMax Gurtovoy 
128dc05ac36SSagi Grimberg /*
12968a5e604SGeliang Tang  * iscsi_iser_recv() - Process a successful recv completion
130dc05ac36SSagi Grimberg  * @conn:         iscsi connection
131dc05ac36SSagi Grimberg  * @hdr:          iscsi header
132dc05ac36SSagi Grimberg  * @rx_data:      buffer containing receive data payload
133dc05ac36SSagi Grimberg  * @rx_data_len:  length of rx_data
134dc05ac36SSagi Grimberg  *
135dc05ac36SSagi Grimberg  * Notes: In case of data length errors or iscsi PDU completion failures
136dc05ac36SSagi Grimberg  *        this routine will signal iscsi layer of connection failure.
137dc05ac36SSagi Grimberg  */
iscsi_iser_recv(struct iscsi_conn * conn,struct iscsi_hdr * hdr,char * rx_data,int rx_data_len)138ca2770c6SMax Gurtovoy void iscsi_iser_recv(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
139dc05ac36SSagi Grimberg 		     char *rx_data, int rx_data_len)
14065e7ae7bSOr Gerlitz {
14165e7ae7bSOr Gerlitz 	int rc = 0;
14265e7ae7bSOr Gerlitz 	int datalen;
14365e7ae7bSOr Gerlitz 
14465e7ae7bSOr Gerlitz 	/* verify PDU length */
14565e7ae7bSOr Gerlitz 	datalen = ntoh24(hdr->dlength);
146200ae1a0SOr Gerlitz 	if (datalen > rx_data_len || (datalen + 4) < rx_data_len) {
147200ae1a0SOr Gerlitz 		iser_err("wrong datalen %d (hdr), %d (IB)\n",
14865e7ae7bSOr Gerlitz 			datalen, rx_data_len);
14965e7ae7bSOr Gerlitz 		rc = ISCSI_ERR_DATALEN;
15065e7ae7bSOr Gerlitz 		goto error;
15165e7ae7bSOr Gerlitz 	}
15265e7ae7bSOr Gerlitz 
153200ae1a0SOr Gerlitz 	if (datalen != rx_data_len)
154200ae1a0SOr Gerlitz 		iser_dbg("aligned datalen (%d) hdr, %d (IB)\n",
155200ae1a0SOr Gerlitz 			datalen, rx_data_len);
156200ae1a0SOr Gerlitz 
15765e7ae7bSOr Gerlitz 	rc = iscsi_complete_pdu(conn, hdr, rx_data, rx_data_len);
15865e7ae7bSOr Gerlitz 	if (rc && rc != ISCSI_ERR_NO_SCSI_CMD)
15965e7ae7bSOr Gerlitz 		goto error;
16065e7ae7bSOr Gerlitz 
16165e7ae7bSOr Gerlitz 	return;
16265e7ae7bSOr Gerlitz error:
16365e7ae7bSOr Gerlitz 	iscsi_conn_failure(conn, rc);
16465e7ae7bSOr Gerlitz }
16565e7ae7bSOr Gerlitz 
166dc05ac36SSagi Grimberg /**
167dc05ac36SSagi Grimberg  * iscsi_iser_pdu_alloc() - allocate an iscsi-iser PDU
168dc05ac36SSagi Grimberg  * @task:     iscsi task
169dc05ac36SSagi Grimberg  * @opcode:   iscsi command opcode
170dc05ac36SSagi Grimberg  *
171dc05ac36SSagi Grimberg  * Netes: This routine can't fail, just assign iscsi task
172dc05ac36SSagi Grimberg  *        hdr and max hdr size.
173dc05ac36SSagi Grimberg  */
iscsi_iser_pdu_alloc(struct iscsi_task * task,uint8_t opcode)174ca2770c6SMax Gurtovoy static int iscsi_iser_pdu_alloc(struct iscsi_task *task, uint8_t opcode)
1750f9c7449SMike Christie {
1760f9c7449SMike Christie 	struct iscsi_iser_task *iser_task = task->dd_data;
1770f9c7449SMike Christie 
1780f9c7449SMike Christie 	task->hdr = (struct iscsi_hdr *)&iser_task->desc.iscsi_header;
1790f9c7449SMike Christie 	task->hdr_max = sizeof(iser_task->desc.iscsi_header);
180dc05ac36SSagi Grimberg 
1810f9c7449SMike Christie 	return 0;
1820f9c7449SMike Christie }
18365e7ae7bSOr Gerlitz 
1847414dde0SSagi Grimberg /**
1857414dde0SSagi Grimberg  * iser_initialize_task_headers() - Initialize task headers
1867414dde0SSagi Grimberg  * @task:       iscsi task
1877414dde0SSagi Grimberg  * @tx_desc:    iser tx descriptor
1887414dde0SSagi Grimberg  *
1897414dde0SSagi Grimberg  * Notes:
1907414dde0SSagi Grimberg  * This routine may race with iser teardown flow for scsi
1917414dde0SSagi Grimberg  * error handling TMFs. So for TMF we should acquire the
1927414dde0SSagi Grimberg  * state mutex to avoid dereferencing the IB device which
1937414dde0SSagi Grimberg  * may have already been terminated.
1947414dde0SSagi Grimberg  */
iser_initialize_task_headers(struct iscsi_task * task,struct iser_tx_desc * tx_desc)195ca2770c6SMax Gurtovoy int iser_initialize_task_headers(struct iscsi_task *task,
196f19624aaSOr Gerlitz 				 struct iser_tx_desc *tx_desc)
197f19624aaSOr Gerlitz {
1985716af6eSSagi Grimberg 	struct iser_conn *iser_conn = task->conn->dd_data;
199a4ee3539SSagi Grimberg 	struct iser_device *device = iser_conn->ib_conn.device;
200f19624aaSOr Gerlitz 	struct iscsi_iser_task *iser_task = task->dd_data;
201f19624aaSOr Gerlitz 	u64 dma_addr;
2027414dde0SSagi Grimberg 
2030583531bSSebastian Andrzej Siewior 	if (unlikely(iser_conn->state != ISER_CONN_UP))
2040583531bSSebastian Andrzej Siewior 		return -ENODEV;
205f19624aaSOr Gerlitz 
206f19624aaSOr Gerlitz 	dma_addr = ib_dma_map_single(device->ib_device, (void *)tx_desc,
207f19624aaSOr Gerlitz 				ISER_HEADERS_LEN, DMA_TO_DEVICE);
2080583531bSSebastian Andrzej Siewior 	if (ib_dma_mapping_error(device->ib_device, dma_addr))
2090583531bSSebastian Andrzej Siewior 		return -ENOMEM;
210f19624aaSOr Gerlitz 
211b9294f8bSIsrael Rukshin 	tx_desc->inv_wr.next = NULL;
212b9294f8bSIsrael Rukshin 	tx_desc->reg_wr.wr.next = NULL;
2138d5944d8SSagi Grimberg 	tx_desc->mapped = true;
214f19624aaSOr Gerlitz 	tx_desc->dma_addr = dma_addr;
215f19624aaSOr Gerlitz 	tx_desc->tx_sg[0].addr   = tx_desc->dma_addr;
216f19624aaSOr Gerlitz 	tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
217256b7ad2SJason Gunthorpe 	tx_desc->tx_sg[0].lkey   = device->pd->local_dma_lkey;
218f19624aaSOr Gerlitz 
2195716af6eSSagi Grimberg 	iser_task->iser_conn = iser_conn;
2207414dde0SSagi Grimberg 
2210583531bSSebastian Andrzej Siewior 	return 0;
222f19624aaSOr Gerlitz }
223dc05ac36SSagi Grimberg 
22465e7ae7bSOr Gerlitz /**
225dc05ac36SSagi Grimberg  * iscsi_iser_task_init() - Initialize iscsi-iser task
2262261ec3dSMike Christie  * @task: iscsi task
22765e7ae7bSOr Gerlitz  *
2282261ec3dSMike Christie  * Initialize the task for the scsi command or mgmt command.
229dc05ac36SSagi Grimberg  *
230dc05ac36SSagi Grimberg  * Return: Returns zero on success or -ENOMEM when failing
231dc05ac36SSagi Grimberg  *         to init task headers (dma mapping error).
2322747fdb2SMike Christie  */
iscsi_iser_task_init(struct iscsi_task * task)233ca2770c6SMax Gurtovoy static int iscsi_iser_task_init(struct iscsi_task *task)
23465e7ae7bSOr Gerlitz {
2352261ec3dSMike Christie 	struct iscsi_iser_task *iser_task = task->dd_data;
2367414dde0SSagi Grimberg 	int ret;
23765e7ae7bSOr Gerlitz 
2387414dde0SSagi Grimberg 	ret = iser_initialize_task_headers(task, &iser_task->desc);
2397414dde0SSagi Grimberg 	if (ret) {
2407414dde0SSagi Grimberg 		iser_err("Failed to init task %p, err = %d\n",
2417414dde0SSagi Grimberg 			 iser_task, ret);
2427414dde0SSagi Grimberg 		return ret;
2437414dde0SSagi Grimberg 	}
244f19624aaSOr Gerlitz 
2452261ec3dSMike Christie 	/* mgmt task */
246f19624aaSOr Gerlitz 	if (!task->sc)
2472747fdb2SMike Christie 		return 0;
2482747fdb2SMike Christie 
2492261ec3dSMike Christie 	iser_task->command_sent = 0;
2502261ec3dSMike Christie 	iser_task_rdma_init(iser_task);
251177e31bdSSagi Grimberg 	iser_task->sc = task->sc;
252177e31bdSSagi Grimberg 
253a8ac6311SOlaf Kirch 	return 0;
25465e7ae7bSOr Gerlitz }
25565e7ae7bSOr Gerlitz 
25665e7ae7bSOr Gerlitz /**
257dc05ac36SSagi Grimberg  * iscsi_iser_mtask_xmit() - xmit management (immediate) task
25865e7ae7bSOr Gerlitz  * @conn: iscsi connection
2592261ec3dSMike Christie  * @task: task management task
26065e7ae7bSOr Gerlitz  *
26165e7ae7bSOr Gerlitz  * Notes:
26265e7ae7bSOr Gerlitz  *	The function can return -EAGAIN in which case caller must
26365e7ae7bSOr Gerlitz  *	call it again later, or recover. '0' return code means successful
26465e7ae7bSOr Gerlitz  *	xmit.
26565e7ae7bSOr Gerlitz  *
26665e7ae7bSOr Gerlitz  **/
iscsi_iser_mtask_xmit(struct iscsi_conn * conn,struct iscsi_task * task)267ca2770c6SMax Gurtovoy static int iscsi_iser_mtask_xmit(struct iscsi_conn *conn,
268ca2770c6SMax Gurtovoy 				 struct iscsi_task *task)
26965e7ae7bSOr Gerlitz {
27065e7ae7bSOr Gerlitz 	int error = 0;
27165e7ae7bSOr Gerlitz 
272962b4b52SOr Gerlitz 	iser_dbg("mtask xmit [cid %d itt 0x%x]\n", conn->id, task->itt);
27365e7ae7bSOr Gerlitz 
2742261ec3dSMike Christie 	error = iser_send_control(conn, task);
27565e7ae7bSOr Gerlitz 
2762261ec3dSMike Christie 	/* since iser xmits control with zero copy, tasks can not be recycled
27765e7ae7bSOr Gerlitz 	 * right after sending them.
27865e7ae7bSOr Gerlitz 	 * The recycling scheme is based on whether a response is expected
2792261ec3dSMike Christie 	 * - if yes, the task is recycled at iscsi_complete_pdu
2802261ec3dSMike Christie 	 * - if no,  the task is recycled at iser_snd_completion
28165e7ae7bSOr Gerlitz 	 */
28265e7ae7bSOr Gerlitz 	return error;
28365e7ae7bSOr Gerlitz }
28465e7ae7bSOr Gerlitz 
iscsi_iser_task_xmit_unsol_data(struct iscsi_conn * conn,struct iscsi_task * task)285ca2770c6SMax Gurtovoy static int iscsi_iser_task_xmit_unsol_data(struct iscsi_conn *conn,
2862261ec3dSMike Christie 					   struct iscsi_task *task)
28765e7ae7bSOr Gerlitz {
2880f9c7449SMike Christie 	struct iscsi_r2t_info *r2t = &task->unsol_r2t;
28965e7ae7bSOr Gerlitz 	struct iscsi_data hdr;
29065e7ae7bSOr Gerlitz 	int error = 0;
29165e7ae7bSOr Gerlitz 
29265e7ae7bSOr Gerlitz 	/* Send data-out PDUs while there's still unsolicited data to send */
2930f9c7449SMike Christie 	while (iscsi_task_has_unsol_data(task)) {
2940f9c7449SMike Christie 		iscsi_prep_data_out_pdu(task, r2t, &hdr);
29548a237a2SMike Christie 		iser_dbg("Sending data-out: itt 0x%x, data count %d\n",
2960f9c7449SMike Christie 			   hdr.itt, r2t->data_count);
29765e7ae7bSOr Gerlitz 
29865e7ae7bSOr Gerlitz 		/* the buffer description has been passed with the command */
29965e7ae7bSOr Gerlitz 		/* Send the command */
3002261ec3dSMike Christie 		error = iser_send_data_out(conn, task, &hdr);
30165e7ae7bSOr Gerlitz 		if (error) {
3020f9c7449SMike Christie 			r2t->datasn--;
3032747fdb2SMike Christie 			goto iscsi_iser_task_xmit_unsol_data_exit;
30465e7ae7bSOr Gerlitz 		}
3050f9c7449SMike Christie 		r2t->sent += r2t->data_count;
30648a237a2SMike Christie 		iser_dbg("Need to send %d more as data-out PDUs\n",
3070f9c7449SMike Christie 			   r2t->data_length - r2t->sent);
30865e7ae7bSOr Gerlitz 	}
30965e7ae7bSOr Gerlitz 
3102747fdb2SMike Christie iscsi_iser_task_xmit_unsol_data_exit:
31165e7ae7bSOr Gerlitz 	return error;
31265e7ae7bSOr Gerlitz }
31365e7ae7bSOr Gerlitz 
314dc05ac36SSagi Grimberg /**
315dc05ac36SSagi Grimberg  * iscsi_iser_task_xmit() - xmit iscsi-iser task
316dc05ac36SSagi Grimberg  * @task: iscsi task
317dc05ac36SSagi Grimberg  *
318dc05ac36SSagi Grimberg  * Return: zero on success or escalates $error on failure.
319dc05ac36SSagi Grimberg  */
iscsi_iser_task_xmit(struct iscsi_task * task)320ca2770c6SMax Gurtovoy static int iscsi_iser_task_xmit(struct iscsi_task *task)
32165e7ae7bSOr Gerlitz {
3222261ec3dSMike Christie 	struct iscsi_conn *conn = task->conn;
3232261ec3dSMike Christie 	struct iscsi_iser_task *iser_task = task->dd_data;
32465e7ae7bSOr Gerlitz 	int error = 0;
32565e7ae7bSOr Gerlitz 
3262261ec3dSMike Christie 	if (!task->sc)
3272261ec3dSMike Christie 		return iscsi_iser_mtask_xmit(conn, task);
3282747fdb2SMike Christie 
3292261ec3dSMike Christie 	if (task->sc->sc_data_direction == DMA_TO_DEVICE) {
3302261ec3dSMike Christie 		BUG_ON(scsi_bufflen(task->sc) == 0);
33177a23c21SMike Christie 
33248a237a2SMike Christie 		iser_dbg("cmd [itt %x total %d imm %d unsol_data %d\n",
3332261ec3dSMike Christie 			   task->itt, scsi_bufflen(task->sc),
3340f9c7449SMike Christie 			   task->imm_count, task->unsol_r2t.data_length);
33577a23c21SMike Christie 	}
33677a23c21SMike Christie 
337962b4b52SOr Gerlitz 	iser_dbg("ctask xmit [cid %d itt 0x%x]\n",
3382261ec3dSMike Christie 		   conn->id, task->itt);
33965e7ae7bSOr Gerlitz 
34065e7ae7bSOr Gerlitz 	/* Send the cmd PDU */
3412261ec3dSMike Christie 	if (!iser_task->command_sent) {
3422261ec3dSMike Christie 		error = iser_send_command(conn, task);
34365e7ae7bSOr Gerlitz 		if (error)
3442747fdb2SMike Christie 			goto iscsi_iser_task_xmit_exit;
3452261ec3dSMike Christie 		iser_task->command_sent = 1;
34665e7ae7bSOr Gerlitz 	}
34765e7ae7bSOr Gerlitz 
34865e7ae7bSOr Gerlitz 	/* Send unsolicited data-out PDU(s) if necessary */
3490f9c7449SMike Christie 	if (iscsi_task_has_unsol_data(task))
3502261ec3dSMike Christie 		error = iscsi_iser_task_xmit_unsol_data(conn, task);
35165e7ae7bSOr Gerlitz 
3522747fdb2SMike Christie  iscsi_iser_task_xmit_exit:
35365e7ae7bSOr Gerlitz 	return error;
35465e7ae7bSOr Gerlitz }
35565e7ae7bSOr Gerlitz 
356dc05ac36SSagi Grimberg /**
357dc05ac36SSagi Grimberg  * iscsi_iser_cleanup_task() - cleanup an iscsi-iser task
358dc05ac36SSagi Grimberg  * @task: iscsi task
359dc05ac36SSagi Grimberg  *
360dc05ac36SSagi Grimberg  * Notes: In case the RDMA device is already NULL (might have
361dc05ac36SSagi Grimberg  *        been removed in DEVICE_REMOVAL CM event it will bail-out
362dc05ac36SSagi Grimberg  *        without doing dma unmapping.
363dc05ac36SSagi Grimberg  */
iscsi_iser_cleanup_task(struct iscsi_task * task)3640f9c7449SMike Christie static void iscsi_iser_cleanup_task(struct iscsi_task *task)
36565e7ae7bSOr Gerlitz {
3662261ec3dSMike Christie 	struct iscsi_iser_task *iser_task = task->dd_data;
36752439540SOr Gerlitz 	struct iser_tx_desc *tx_desc = &iser_task->desc;
3685716af6eSSagi Grimberg 	struct iser_conn *iser_conn = task->conn->dd_data;
369a4ee3539SSagi Grimberg 	struct iser_device *device = iser_conn->ib_conn.device;
37052439540SOr Gerlitz 
3713a940dafSSagi Grimberg 	/* DEVICE_REMOVAL event might have already released the device */
3723a940dafSSagi Grimberg 	if (!device)
3733a940dafSSagi Grimberg 		return;
3743a940dafSSagi Grimberg 
3758d5944d8SSagi Grimberg 	if (likely(tx_desc->mapped)) {
3768d5944d8SSagi Grimberg 		ib_dma_unmap_single(device->ib_device, tx_desc->dma_addr,
3778d5944d8SSagi Grimberg 				    ISER_HEADERS_LEN, DMA_TO_DEVICE);
3788d5944d8SSagi Grimberg 		tx_desc->mapped = false;
3798d5944d8SSagi Grimberg 	}
38065e7ae7bSOr Gerlitz 
381b3cd5050SMike Christie 	/* mgmt tasks do not need special cleanup */
382b3cd5050SMike Christie 	if (!task->sc)
3832747fdb2SMike Christie 		return;
38465e7ae7bSOr Gerlitz 
3852261ec3dSMike Christie 	if (iser_task->status == ISER_TASK_STATUS_STARTED) {
3862261ec3dSMike Christie 		iser_task->status = ISER_TASK_STATUS_COMPLETED;
3872261ec3dSMike Christie 		iser_task_rdma_finalize(iser_task);
38865e7ae7bSOr Gerlitz 	}
38965e7ae7bSOr Gerlitz }
39065e7ae7bSOr Gerlitz 
391dc05ac36SSagi Grimberg /**
392dc05ac36SSagi Grimberg  * iscsi_iser_check_protection() - check protection information status of task.
393dc05ac36SSagi Grimberg  * @task:     iscsi task
394dc05ac36SSagi Grimberg  * @sector:   error sector if exsists (output)
395dc05ac36SSagi Grimberg  *
396dc05ac36SSagi Grimberg  * Return: zero if no data-integrity errors have occured
397dc05ac36SSagi Grimberg  *         0x1: data-integrity error occured in the guard-block
398dc05ac36SSagi Grimberg  *         0x2: data-integrity error occured in the reference tag
399dc05ac36SSagi Grimberg  *         0x3: data-integrity error occured in the application tag
400dc05ac36SSagi Grimberg  *
401dc05ac36SSagi Grimberg  *         In addition the error sector is marked.
402dc05ac36SSagi Grimberg  */
iscsi_iser_check_protection(struct iscsi_task * task,sector_t * sector)403ca2770c6SMax Gurtovoy static u8 iscsi_iser_check_protection(struct iscsi_task *task, sector_t *sector)
4040a7a08adSSagi Grimberg {
4050a7a08adSSagi Grimberg 	struct iscsi_iser_task *iser_task = task->dd_data;
406a7b287bfSIsrael Rukshin 	enum iser_data_dir dir = iser_task->dir[ISER_DIR_IN] ?
407a7b287bfSIsrael Rukshin 					ISER_DIR_IN : ISER_DIR_OUT;
4080a7a08adSSagi Grimberg 
409a7b287bfSIsrael Rukshin 	return iser_check_task_pi_status(iser_task, dir, sector);
4100a7a08adSSagi Grimberg }
4110a7a08adSSagi Grimberg 
412dc05ac36SSagi Grimberg /**
413dc05ac36SSagi Grimberg  * iscsi_iser_conn_create() - create a new iscsi-iser connection
414dc05ac36SSagi Grimberg  * @cls_session: iscsi class connection
415dc05ac36SSagi Grimberg  * @conn_idx:    connection index within the session (for MCS)
416dc05ac36SSagi Grimberg  *
417dc05ac36SSagi Grimberg  * Return: iscsi_cls_conn when iscsi_conn_setup succeeds or NULL
418dc05ac36SSagi Grimberg  *         otherwise.
419dc05ac36SSagi Grimberg  */
42065e7ae7bSOr Gerlitz static struct iscsi_cls_conn *
iscsi_iser_conn_create(struct iscsi_cls_session * cls_session,uint32_t conn_idx)421dc05ac36SSagi Grimberg iscsi_iser_conn_create(struct iscsi_cls_session *cls_session,
422dc05ac36SSagi Grimberg 		       uint32_t conn_idx)
42365e7ae7bSOr Gerlitz {
42465e7ae7bSOr Gerlitz 	struct iscsi_conn *conn;
42565e7ae7bSOr Gerlitz 	struct iscsi_cls_conn *cls_conn;
42665e7ae7bSOr Gerlitz 
4274667f5dfSAriel Nahum 	cls_conn = iscsi_conn_setup(cls_session, 0, conn_idx);
42865e7ae7bSOr Gerlitz 	if (!cls_conn)
42965e7ae7bSOr Gerlitz 		return NULL;
43065e7ae7bSOr Gerlitz 	conn = cls_conn->dd_data;
43165e7ae7bSOr Gerlitz 
43265e7ae7bSOr Gerlitz 	/*
43365e7ae7bSOr Gerlitz 	 * due to issues with the login code re iser sematics
43465e7ae7bSOr Gerlitz 	 * this not set in iscsi_conn_setup - FIXME
43565e7ae7bSOr Gerlitz 	 */
436bcc60c38SOr Gerlitz 	conn->max_recv_dlength = ISER_RECV_DATA_SEG_LEN;
43765e7ae7bSOr Gerlitz 
43865e7ae7bSOr Gerlitz 	return cls_conn;
43965e7ae7bSOr Gerlitz }
44065e7ae7bSOr Gerlitz 
441dc05ac36SSagi Grimberg /**
442dc05ac36SSagi Grimberg  * iscsi_iser_conn_bind() - bind iscsi and iser connection structures
443dc05ac36SSagi Grimberg  * @cls_session:     iscsi class session
444dc05ac36SSagi Grimberg  * @cls_conn:        iscsi class connection
445dc05ac36SSagi Grimberg  * @transport_eph:   transport end-point handle
446dc05ac36SSagi Grimberg  * @is_leading:      indicate if this is the session leading connection (MCS)
447dc05ac36SSagi Grimberg  *
448dc05ac36SSagi Grimberg  * Return: zero on success, $error if iscsi_conn_bind fails and
4491b381f6fSLuca Ceresoli  *         -EINVAL in case end-point doesn't exists anymore or iser connection
450dc05ac36SSagi Grimberg  *         state is not UP (teardown already started).
451dc05ac36SSagi Grimberg  */
iscsi_iser_conn_bind(struct iscsi_cls_session * cls_session,struct iscsi_cls_conn * cls_conn,uint64_t transport_eph,int is_leading)452ca2770c6SMax Gurtovoy static int iscsi_iser_conn_bind(struct iscsi_cls_session *cls_session,
453dc05ac36SSagi Grimberg 				struct iscsi_cls_conn *cls_conn,
454ca2770c6SMax Gurtovoy 				uint64_t transport_eph, int is_leading)
45565e7ae7bSOr Gerlitz {
45665e7ae7bSOr Gerlitz 	struct iscsi_conn *conn = cls_conn->dd_data;
4575716af6eSSagi Grimberg 	struct iser_conn *iser_conn;
458412eeafaSMike Christie 	struct iscsi_endpoint *ep;
45965e7ae7bSOr Gerlitz 	int error;
46065e7ae7bSOr Gerlitz 
46165e7ae7bSOr Gerlitz 	error = iscsi_conn_bind(cls_session, cls_conn, is_leading);
46265e7ae7bSOr Gerlitz 	if (error)
46365e7ae7bSOr Gerlitz 		return error;
46465e7ae7bSOr Gerlitz 
46565e7ae7bSOr Gerlitz 	/* the transport ep handle comes from user space so it must be
46665e7ae7bSOr Gerlitz 	 * verified against the global ib connections list */
467412eeafaSMike Christie 	ep = iscsi_lookup_endpoint(transport_eph);
468412eeafaSMike Christie 	if (!ep) {
46965e7ae7bSOr Gerlitz 		iser_err("can't bind eph %llx\n",
47065e7ae7bSOr Gerlitz 			 (unsigned long long)transport_eph);
47165e7ae7bSOr Gerlitz 		return -EINVAL;
47265e7ae7bSOr Gerlitz 	}
4735716af6eSSagi Grimberg 	iser_conn = ep->dd_data;
474412eeafaSMike Christie 
4755716af6eSSagi Grimberg 	mutex_lock(&iser_conn->state_mutex);
4765716af6eSSagi Grimberg 	if (iser_conn->state != ISER_CONN_UP) {
47791eb1df3SSagi Grimberg 		error = -EINVAL;
47891eb1df3SSagi Grimberg 		iser_err("iser_conn %p state is %d, teardown started\n",
4795716af6eSSagi Grimberg 			 iser_conn, iser_conn->state);
48091eb1df3SSagi Grimberg 		goto out;
48191eb1df3SSagi Grimberg 	}
48291eb1df3SSagi Grimberg 
4835716af6eSSagi Grimberg 	error = iser_alloc_rx_descriptors(iser_conn, conn->session);
48491eb1df3SSagi Grimberg 	if (error)
48591eb1df3SSagi Grimberg 		goto out;
48689e984e2SOr Gerlitz 
48765e7ae7bSOr Gerlitz 	/* binds the iSER connection retrieved from the previously
48865e7ae7bSOr Gerlitz 	 * connected ep_handle to the iSCSI layer connection. exchanges
48965e7ae7bSOr Gerlitz 	 * connection pointers */
4905716af6eSSagi Grimberg 	iser_info("binding iscsi conn %p to iser_conn %p\n", conn, iser_conn);
4914667f5dfSAriel Nahum 
4925716af6eSSagi Grimberg 	conn->dd_data = iser_conn;
4935716af6eSSagi Grimberg 	iser_conn->iscsi_conn = conn;
4944667f5dfSAriel Nahum 
49591eb1df3SSagi Grimberg out:
4969e5fe170SMike Christie 	iscsi_put_endpoint(ep);
4975716af6eSSagi Grimberg 	mutex_unlock(&iser_conn->state_mutex);
49891eb1df3SSagi Grimberg 	return error;
49965e7ae7bSOr Gerlitz }
50065e7ae7bSOr Gerlitz 
501dc05ac36SSagi Grimberg /**
502dc05ac36SSagi Grimberg  * iscsi_iser_conn_start() - start iscsi-iser connection
503dc05ac36SSagi Grimberg  * @cls_conn: iscsi class connection
504dc05ac36SSagi Grimberg  *
505dc05ac36SSagi Grimberg  * Notes: Here iser intialize (or re-initialize) stop_completion as
506dc05ac36SSagi Grimberg  *        from this point iscsi must call conn_stop in session/connection
507dc05ac36SSagi Grimberg  *        teardown so iser transport must wait for it.
508dc05ac36SSagi Grimberg  */
iscsi_iser_conn_start(struct iscsi_cls_conn * cls_conn)509ca2770c6SMax Gurtovoy static int iscsi_iser_conn_start(struct iscsi_cls_conn *cls_conn)
510b73c3adaSAriel Nahum {
511b73c3adaSAriel Nahum 	struct iscsi_conn *iscsi_conn;
5125716af6eSSagi Grimberg 	struct iser_conn *iser_conn;
513b73c3adaSAriel Nahum 
514b73c3adaSAriel Nahum 	iscsi_conn = cls_conn->dd_data;
5155716af6eSSagi Grimberg 	iser_conn = iscsi_conn->dd_data;
5165716af6eSSagi Grimberg 	reinit_completion(&iser_conn->stop_completion);
517b73c3adaSAriel Nahum 
518b73c3adaSAriel Nahum 	return iscsi_conn_start(cls_conn);
519b73c3adaSAriel Nahum }
520b73c3adaSAriel Nahum 
521dc05ac36SSagi Grimberg /**
522dc05ac36SSagi Grimberg  * iscsi_iser_conn_stop() - stop iscsi-iser connection
523dc05ac36SSagi Grimberg  * @cls_conn:  iscsi class connection
524dc05ac36SSagi Grimberg  * @flag:      indicate if recover or terminate (passed as is)
525dc05ac36SSagi Grimberg  *
526dc05ac36SSagi Grimberg  * Notes: Calling iscsi_conn_stop might theoretically race with
527dc05ac36SSagi Grimberg  *        DEVICE_REMOVAL event and dereference a previously freed RDMA device
528dc05ac36SSagi Grimberg  *        handle, so we call it under iser the state lock to protect against
529dc05ac36SSagi Grimberg  *        this kind of race.
530dc05ac36SSagi Grimberg  */
iscsi_iser_conn_stop(struct iscsi_cls_conn * cls_conn,int flag)531ca2770c6SMax Gurtovoy static void iscsi_iser_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
532b40977d9SMike Christie {
533b40977d9SMike Christie 	struct iscsi_conn *conn = cls_conn->dd_data;
5345716af6eSSagi Grimberg 	struct iser_conn *iser_conn = conn->dd_data;
535b40977d9SMike Christie 
536bba0a3c9SAriel Nahum 	iser_info("stopping iscsi_conn: %p, iser_conn: %p\n", conn, iser_conn);
537b73c3adaSAriel Nahum 
538913e5bf4SMike Christie 	/*
539913e5bf4SMike Christie 	 * Userspace may have goofed up and not bound the connection or
540913e5bf4SMike Christie 	 * might have only partially setup the connection.
541913e5bf4SMike Christie 	 */
5425716af6eSSagi Grimberg 	if (iser_conn) {
543ec370e2bSAriel Nahum 		mutex_lock(&iser_conn->state_mutex);
544c8c16d3bSVladimir Neyelov 		mutex_lock(&unbind_iser_conn_mutex);
545ec370e2bSAriel Nahum 		iser_conn_terminate(iser_conn);
546f0caef6dSSagi Grimberg 		iscsi_conn_stop(cls_conn, flag);
547ec370e2bSAriel Nahum 
548ec370e2bSAriel Nahum 		/* unbind */
549ec370e2bSAriel Nahum 		iser_conn->iscsi_conn = NULL;
5504667f5dfSAriel Nahum 		conn->dd_data = NULL;
551c8c16d3bSVladimir Neyelov 		mutex_unlock(&unbind_iser_conn_mutex);
552ec370e2bSAriel Nahum 
5535716af6eSSagi Grimberg 		complete(&iser_conn->stop_completion);
554ec370e2bSAriel Nahum 		mutex_unlock(&iser_conn->state_mutex);
5553a940dafSSagi Grimberg 	} else {
5563a940dafSSagi Grimberg 		iscsi_conn_stop(cls_conn, flag);
557b73c3adaSAriel Nahum 	}
558b40977d9SMike Christie }
559b40977d9SMike Christie 
560dc05ac36SSagi Grimberg /**
561dc05ac36SSagi Grimberg  * iscsi_iser_session_destroy() - destroy iscsi-iser session
562dc05ac36SSagi Grimberg  * @cls_session: iscsi class session
563dc05ac36SSagi Grimberg  *
564dc05ac36SSagi Grimberg  * Removes and free iscsi host.
565dc05ac36SSagi Grimberg  */
iscsi_iser_session_destroy(struct iscsi_cls_session * cls_session)566ca2770c6SMax Gurtovoy static void iscsi_iser_session_destroy(struct iscsi_cls_session *cls_session)
56775613521SMike Christie {
56875613521SMike Christie 	struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
56975613521SMike Christie 
570e5bd7b54SMike Christie 	iscsi_session_teardown(cls_session);
57131500e90SMike Christie 	iscsi_host_remove(shost, false);
572a4804cd6SMike Christie 	iscsi_host_free(shost);
57375613521SMike Christie }
57465e7ae7bSOr Gerlitz 
iser_dif_prot_caps(int prot_caps)575ca2770c6SMax Gurtovoy static inline unsigned int iser_dif_prot_caps(int prot_caps)
5766192d4e6SSagi Grimberg {
577877745b4SMax Gurtovoy 	int ret = 0;
578877745b4SMax Gurtovoy 
579877745b4SMax Gurtovoy 	if (prot_caps & IB_PROT_T10DIF_TYPE_1)
580877745b4SMax Gurtovoy 		ret |= SHOST_DIF_TYPE1_PROTECTION |
581877745b4SMax Gurtovoy 		       SHOST_DIX_TYPE0_PROTECTION |
582877745b4SMax Gurtovoy 		       SHOST_DIX_TYPE1_PROTECTION;
583877745b4SMax Gurtovoy 	if (prot_caps & IB_PROT_T10DIF_TYPE_2)
584877745b4SMax Gurtovoy 		ret |= SHOST_DIF_TYPE2_PROTECTION |
585877745b4SMax Gurtovoy 		       SHOST_DIX_TYPE2_PROTECTION;
586877745b4SMax Gurtovoy 	if (prot_caps & IB_PROT_T10DIF_TYPE_3)
587877745b4SMax Gurtovoy 		ret |= SHOST_DIF_TYPE3_PROTECTION |
588877745b4SMax Gurtovoy 		       SHOST_DIX_TYPE3_PROTECTION;
589877745b4SMax Gurtovoy 
590877745b4SMax Gurtovoy 	return ret;
5916192d4e6SSagi Grimberg }
5926192d4e6SSagi Grimberg 
593dc05ac36SSagi Grimberg /**
594dc05ac36SSagi Grimberg  * iscsi_iser_session_create() - create an iscsi-iser session
595dc05ac36SSagi Grimberg  * @ep:             iscsi end-point handle
596dc05ac36SSagi Grimberg  * @cmds_max:       maximum commands in this session
597dc05ac36SSagi Grimberg  * @qdepth:         session command queue depth
598dc05ac36SSagi Grimberg  * @initial_cmdsn:  initiator command sequnce number
599dc05ac36SSagi Grimberg  *
600dc05ac36SSagi Grimberg  * Allocates and adds a scsi host, expose DIF supprot if
601dc05ac36SSagi Grimberg  * exists, and sets up an iscsi session.
602dc05ac36SSagi Grimberg  */
60365e7ae7bSOr Gerlitz static struct iscsi_cls_session *
iscsi_iser_session_create(struct iscsi_endpoint * ep,uint16_t cmds_max,uint16_t qdepth,uint32_t initial_cmdsn)604412eeafaSMike Christie iscsi_iser_session_create(struct iscsi_endpoint *ep,
6051548271eSMike Christie 			  uint16_t cmds_max, uint16_t qdepth,
6065e7facb7SMike Christie 			  uint32_t initial_cmdsn)
60765e7ae7bSOr Gerlitz {
60865e7ae7bSOr Gerlitz 	struct iscsi_cls_session *cls_session;
609412eeafaSMike Christie 	struct Scsi_Host *shost;
6105716af6eSSagi Grimberg 	struct iser_conn *iser_conn = NULL;
611a4ee3539SSagi Grimberg 	struct ib_conn *ib_conn;
61209a4460bSChristoph Hellwig 	struct ib_device *ib_dev;
6139c674815SChristoph Hellwig 	u32 max_fr_sectors;
61475613521SMike Christie 
615962b4b52SOr Gerlitz 	shost = iscsi_host_alloc(&iscsi_iser_sht, 0, 0);
61675613521SMike Christie 	if (!shost)
61775613521SMike Christie 		return NULL;
61875613521SMike Christie 	shost->transportt = iscsi_iser_scsi_transport;
619b7f04513SShlomo Pongratz 	shost->cmd_per_lun = qdepth;
62075613521SMike Christie 	shost->max_lun = iscsi_max_lun;
62175613521SMike Christie 	shost->max_id = 0;
62275613521SMike Christie 	shost->max_channel = 0;
62375613521SMike Christie 	shost->max_cmd_len = 16;
62475613521SMike Christie 
625412eeafaSMike Christie 	/*
626412eeafaSMike Christie 	 * older userspace tools (before 2.0-870) did not pass us
627412eeafaSMike Christie 	 * the leading conn's ep so this will be NULL;
628412eeafaSMike Christie 	 */
6296192d4e6SSagi Grimberg 	if (ep) {
6305716af6eSSagi Grimberg 		iser_conn = ep->dd_data;
631df749cdcSSagi Grimberg 		shost->sg_tablesize = iser_conn->scsi_sg_tablesize;
63268348441SSagi Grimberg 		shost->can_queue = min_t(u16, cmds_max, iser_conn->max_cmds);
6333f562a0bSAriel Nahum 
6343f562a0bSAriel Nahum 		mutex_lock(&iser_conn->state_mutex);
6353f562a0bSAriel Nahum 		if (iser_conn->state != ISER_CONN_UP) {
6363f562a0bSAriel Nahum 			iser_err("iser conn %p already started teardown\n",
6373f562a0bSAriel Nahum 				 iser_conn);
6383f562a0bSAriel Nahum 			mutex_unlock(&iser_conn->state_mutex);
6393f562a0bSAriel Nahum 			goto free_host;
6403f562a0bSAriel Nahum 		}
6413f562a0bSAriel Nahum 
642a4ee3539SSagi Grimberg 		ib_conn = &iser_conn->ib_conn;
64309a4460bSChristoph Hellwig 		ib_dev = ib_conn->device->ib_device;
644a4ee3539SSagi Grimberg 		if (ib_conn->pi_support) {
64509a4460bSChristoph Hellwig 			u32 sig_caps = ib_dev->attrs.sig_prot_cap;
6466192d4e6SSagi Grimberg 
6477718cf03SMax Gurtovoy 			shost->sg_prot_tablesize = shost->sg_tablesize;
6486192d4e6SSagi Grimberg 			scsi_host_set_prot(shost, iser_dif_prot_caps(sig_caps));
6495bb6e543SSagi Grimberg 			scsi_host_set_guard(shost, SHOST_DIX_GUARD_IP |
6505bb6e543SSagi Grimberg 						   SHOST_DIX_GUARD_CRC);
6516192d4e6SSagi Grimberg 		}
6523f562a0bSAriel Nahum 
653e945c653SJason Gunthorpe 		if (!(ib_dev->attrs.kernel_cap_flags & IBK_SG_GAPS_REG))
6546eeff06dSMax Gurtovoy 			shost->virt_boundary_mask = SZ_4K - 1;
65509a4460bSChristoph Hellwig 
65609a4460bSChristoph Hellwig 		if (iscsi_host_add(shost, ib_dev->dev.parent)) {
6573f562a0bSAriel Nahum 			mutex_unlock(&iser_conn->state_mutex);
6583f562a0bSAriel Nahum 			goto free_host;
6593f562a0bSAriel Nahum 		}
6603f562a0bSAriel Nahum 		mutex_unlock(&iser_conn->state_mutex);
661f4641ef7SMinh Tran 	} else {
66268348441SSagi Grimberg 		shost->can_queue = min_t(u16, cmds_max, ISER_DEF_XMIT_CMDS_MAX);
6633f562a0bSAriel Nahum 		if (iscsi_host_add(shost, NULL))
66475613521SMike Christie 			goto free_host;
6653f562a0bSAriel Nahum 	}
66665e7ae7bSOr Gerlitz 
667434dda42SSergey Gorenko 	max_fr_sectors = (shost->sg_tablesize * PAGE_SIZE) >> 9;
6689c674815SChristoph Hellwig 	shost->max_sectors = min(iser_max_sectors, max_fr_sectors);
6699c674815SChristoph Hellwig 
67083236f01SMax Gurtovoy 	iser_dbg("iser_conn %p, sg_tablesize %u, max_sectors %u\n",
67183236f01SMax Gurtovoy 		 iser_conn, shost->sg_tablesize,
67283236f01SMax Gurtovoy 		 shost->max_sectors);
67383236f01SMax Gurtovoy 
674434dda42SSergey Gorenko 	if (shost->max_sectors < iser_max_sectors)
675434dda42SSergey Gorenko 		iser_warn("max_sectors was reduced from %u to %u\n",
676434dda42SSergey Gorenko 			  iser_max_sectors, shost->max_sectors);
677434dda42SSergey Gorenko 
67875613521SMike Christie 	cls_session = iscsi_session_setup(&iscsi_iser_transport, shost,
67968348441SSagi Grimberg 					  shost->can_queue, 0,
6802261ec3dSMike Christie 					  sizeof(struct iscsi_iser_task),
6817970634bSMike Christie 					  initial_cmdsn, 0);
68265e7ae7bSOr Gerlitz 	if (!cls_session)
68375613521SMike Christie 		goto remove_host;
68465e7ae7bSOr Gerlitz 
68565e7ae7bSOr Gerlitz 	return cls_session;
68675613521SMike Christie 
68775613521SMike Christie remove_host:
68831500e90SMike Christie 	iscsi_host_remove(shost, false);
68975613521SMike Christie free_host:
690a4804cd6SMike Christie 	iscsi_host_free(shost);
69175613521SMike Christie 	return NULL;
69265e7ae7bSOr Gerlitz }
69365e7ae7bSOr Gerlitz 
iscsi_iser_set_param(struct iscsi_cls_conn * cls_conn,enum iscsi_param param,char * buf,int buflen)694ca2770c6SMax Gurtovoy static int iscsi_iser_set_param(struct iscsi_cls_conn *cls_conn,
695358ff019SMike Christie 				enum iscsi_param param, char *buf, int buflen)
69665e7ae7bSOr Gerlitz {
697358ff019SMike Christie 	int value;
69865e7ae7bSOr Gerlitz 
69965e7ae7bSOr Gerlitz 	switch (param) {
70065e7ae7bSOr Gerlitz 	case ISCSI_PARAM_MAX_RECV_DLENGTH:
70165e7ae7bSOr Gerlitz 		/* TBD */
70265e7ae7bSOr Gerlitz 		break;
70365e7ae7bSOr Gerlitz 	case ISCSI_PARAM_HDRDGST_EN:
704358ff019SMike Christie 		sscanf(buf, "%d", &value);
70565e7ae7bSOr Gerlitz 		if (value) {
706e7eeffa4SRoi Dayan 			iser_err("DataDigest wasn't negotiated to None\n");
70765e7ae7bSOr Gerlitz 			return -EPROTO;
70865e7ae7bSOr Gerlitz 		}
70965e7ae7bSOr Gerlitz 		break;
71065e7ae7bSOr Gerlitz 	case ISCSI_PARAM_DATADGST_EN:
711358ff019SMike Christie 		sscanf(buf, "%d", &value);
71265e7ae7bSOr Gerlitz 		if (value) {
713e7eeffa4SRoi Dayan 			iser_err("DataDigest wasn't negotiated to None\n");
71465e7ae7bSOr Gerlitz 			return -EPROTO;
71565e7ae7bSOr Gerlitz 		}
71665e7ae7bSOr Gerlitz 		break;
71765e7ae7bSOr Gerlitz 	case ISCSI_PARAM_IFMARKER_EN:
718358ff019SMike Christie 		sscanf(buf, "%d", &value);
71965e7ae7bSOr Gerlitz 		if (value) {
720e7eeffa4SRoi Dayan 			iser_err("IFMarker wasn't negotiated to No\n");
72165e7ae7bSOr Gerlitz 			return -EPROTO;
72265e7ae7bSOr Gerlitz 		}
72365e7ae7bSOr Gerlitz 		break;
72465e7ae7bSOr Gerlitz 	case ISCSI_PARAM_OFMARKER_EN:
725358ff019SMike Christie 		sscanf(buf, "%d", &value);
72665e7ae7bSOr Gerlitz 		if (value) {
727e7eeffa4SRoi Dayan 			iser_err("OFMarker wasn't negotiated to No\n");
72865e7ae7bSOr Gerlitz 			return -EPROTO;
72965e7ae7bSOr Gerlitz 		}
73065e7ae7bSOr Gerlitz 		break;
73165e7ae7bSOr Gerlitz 	default:
732358ff019SMike Christie 		return iscsi_set_param(cls_conn, param, buf, buflen);
73365e7ae7bSOr Gerlitz 	}
73465e7ae7bSOr Gerlitz 
73565e7ae7bSOr Gerlitz 	return 0;
73665e7ae7bSOr Gerlitz }
73765e7ae7bSOr Gerlitz 
738dc05ac36SSagi Grimberg /**
7392988ca08SMauro Carvalho Chehab  * iscsi_iser_conn_get_stats() - get iscsi connection statistics
740dc05ac36SSagi Grimberg  * @cls_conn:    iscsi class connection
741dc05ac36SSagi Grimberg  * @stats:       iscsi stats to output
742dc05ac36SSagi Grimberg  *
743dc05ac36SSagi Grimberg  * Output connection statistics.
744dc05ac36SSagi Grimberg  */
iscsi_iser_conn_get_stats(struct iscsi_cls_conn * cls_conn,struct iscsi_stats * stats)745ca2770c6SMax Gurtovoy static void iscsi_iser_conn_get_stats(struct iscsi_cls_conn *cls_conn,
746ca2770c6SMax Gurtovoy 				      struct iscsi_stats *stats)
74765e7ae7bSOr Gerlitz {
74865e7ae7bSOr Gerlitz 	struct iscsi_conn *conn = cls_conn->dd_data;
74965e7ae7bSOr Gerlitz 
75065e7ae7bSOr Gerlitz 	stats->txdata_octets = conn->txdata_octets;
75165e7ae7bSOr Gerlitz 	stats->rxdata_octets = conn->rxdata_octets;
75265e7ae7bSOr Gerlitz 	stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
75365e7ae7bSOr Gerlitz 	stats->dataout_pdus = conn->dataout_pdus_cnt;
75465e7ae7bSOr Gerlitz 	stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
75565e7ae7bSOr Gerlitz 	stats->datain_pdus = conn->datain_pdus_cnt; /* always 0 */
75665e7ae7bSOr Gerlitz 	stats->r2t_pdus = conn->r2t_pdus_cnt; /* always 0 */
75765e7ae7bSOr Gerlitz 	stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
75865e7ae7bSOr Gerlitz 	stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
759dd0107a0SSagi Grimberg 	stats->custom_length = 0;
76065e7ae7bSOr Gerlitz }
76165e7ae7bSOr Gerlitz 
iscsi_iser_get_ep_param(struct iscsi_endpoint * ep,enum iscsi_param param,char * buf)7627c53c6f8SMike Christie static int iscsi_iser_get_ep_param(struct iscsi_endpoint *ep,
7637c53c6f8SMike Christie 				   enum iscsi_param param, char *buf)
7647c53c6f8SMike Christie {
7655716af6eSSagi Grimberg 	struct iser_conn *iser_conn = ep->dd_data;
7667c53c6f8SMike Christie 
7677c53c6f8SMike Christie 	switch (param) {
7687c53c6f8SMike Christie 	case ISCSI_PARAM_CONN_PORT:
7697c53c6f8SMike Christie 	case ISCSI_PARAM_CONN_ADDRESS:
770a4ee3539SSagi Grimberg 		if (!iser_conn || !iser_conn->ib_conn.cma_id)
7717c53c6f8SMike Christie 			return -ENOTCONN;
7727c53c6f8SMike Christie 
7737c53c6f8SMike Christie 		return iscsi_conn_get_addr_param((struct sockaddr_storage *)
774a4ee3539SSagi Grimberg 				&iser_conn->ib_conn.cma_id->route.addr.dst_addr,
7757c53c6f8SMike Christie 				param, buf);
7767c53c6f8SMike Christie 	default:
7779513ea4fSColin Ian King 		break;
7787c53c6f8SMike Christie 	}
7799513ea4fSColin Ian King 	return -ENOSYS;
7807c53c6f8SMike Christie }
7817c53c6f8SMike Christie 
782dc05ac36SSagi Grimberg /**
783dc05ac36SSagi Grimberg  * iscsi_iser_ep_connect() - Initiate iSER connection establishment
784dc05ac36SSagi Grimberg  * @shost:          scsi_host
785dc05ac36SSagi Grimberg  * @dst_addr:       destination address
786df130f87Srd.dunlab@gmail.com  * @non_blocking:   indicate if routine can block
787dc05ac36SSagi Grimberg  *
788dc05ac36SSagi Grimberg  * Allocate an iscsi endpoint, an iser_conn structure and bind them.
789dc05ac36SSagi Grimberg  * After that start RDMA connection establishment via rdma_cm. We
790dc05ac36SSagi Grimberg  * don't allocate iser_conn embedded in iscsi_endpoint since in teardown
791dc05ac36SSagi Grimberg  * the endpoint will be destroyed at ep_disconnect while iser_conn will
792dc05ac36SSagi Grimberg  * cleanup its resources asynchronuously.
793dc05ac36SSagi Grimberg  *
794dc05ac36SSagi Grimberg  * Return: iscsi_endpoint created by iscsi layer or ERR_PTR(error)
795dc05ac36SSagi Grimberg  *         if fails.
796dc05ac36SSagi Grimberg  */
iscsi_iser_ep_connect(struct Scsi_Host * shost,struct sockaddr * dst_addr,int non_blocking)797ca2770c6SMax Gurtovoy static struct iscsi_endpoint *iscsi_iser_ep_connect(struct Scsi_Host *shost,
798ca2770c6SMax Gurtovoy 						    struct sockaddr *dst_addr,
79910eb0f01SMike Christie 						    int non_blocking)
80065e7ae7bSOr Gerlitz {
80165e7ae7bSOr Gerlitz 	int err;
8025716af6eSSagi Grimberg 	struct iser_conn *iser_conn;
803412eeafaSMike Christie 	struct iscsi_endpoint *ep;
80465e7ae7bSOr Gerlitz 
8050a690758SAriel Nahum 	ep = iscsi_create_endpoint(0);
806412eeafaSMike Christie 	if (!ep)
807412eeafaSMike Christie 		return ERR_PTR(-ENOMEM);
80865e7ae7bSOr Gerlitz 
8095716af6eSSagi Grimberg 	iser_conn = kzalloc(sizeof(*iser_conn), GFP_KERNEL);
8105716af6eSSagi Grimberg 	if (!iser_conn) {
8110a690758SAriel Nahum 		err = -ENOMEM;
8120a690758SAriel Nahum 		goto failure;
8130a690758SAriel Nahum 	}
8140a690758SAriel Nahum 
8155716af6eSSagi Grimberg 	ep->dd_data = iser_conn;
8165716af6eSSagi Grimberg 	iser_conn->ep = ep;
8175716af6eSSagi Grimberg 	iser_conn_init(iser_conn);
81865e7ae7bSOr Gerlitz 
8195716af6eSSagi Grimberg 	err = iser_connect(iser_conn, NULL, dst_addr, non_blocking);
8207d9c0de4SOr Gerlitz 	if (err)
8210a690758SAriel Nahum 		goto failure;
8227d9c0de4SOr Gerlitz 
823412eeafaSMike Christie 	return ep;
8240a690758SAriel Nahum failure:
8250a690758SAriel Nahum 	iscsi_destroy_endpoint(ep);
8260a690758SAriel Nahum 	return ERR_PTR(err);
82765e7ae7bSOr Gerlitz }
82865e7ae7bSOr Gerlitz 
829dc05ac36SSagi Grimberg /**
830dc05ac36SSagi Grimberg  * iscsi_iser_ep_poll() - poll for iser connection establishment to complete
831dc05ac36SSagi Grimberg  * @ep:            iscsi endpoint (created at ep_connect)
832dc05ac36SSagi Grimberg  * @timeout_ms:    polling timeout allowed in ms.
833dc05ac36SSagi Grimberg  *
834dc05ac36SSagi Grimberg  * This routine boils down to waiting for up_completion signaling
835dc05ac36SSagi Grimberg  * that cma_id got CONNECTED event.
836dc05ac36SSagi Grimberg  *
837dc05ac36SSagi Grimberg  * Return: 1 if succeeded in connection establishment, 0 if timeout expired
838dc05ac36SSagi Grimberg  *         (libiscsi will retry will kick in) or -1 if interrupted by signal
839dc05ac36SSagi Grimberg  *         or more likely iser connection state transitioned to TEMINATING or
840dc05ac36SSagi Grimberg  *         DOWN during the wait period.
841dc05ac36SSagi Grimberg  */
iscsi_iser_ep_poll(struct iscsi_endpoint * ep,int timeout_ms)842ca2770c6SMax Gurtovoy static int iscsi_iser_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
84365e7ae7bSOr Gerlitz {
84474ce897bSSagi Grimberg 	struct iser_conn *iser_conn = ep->dd_data;
84565e7ae7bSOr Gerlitz 	int rc;
84665e7ae7bSOr Gerlitz 
8475716af6eSSagi Grimberg 	rc = wait_for_completion_interruptible_timeout(&iser_conn->up_completion,
84865e7ae7bSOr Gerlitz 						       msecs_to_jiffies(timeout_ms));
84965e7ae7bSOr Gerlitz 	/* if conn establishment failed, return error code to iscsi */
850504130c0SAriel Nahum 	if (rc == 0) {
8515716af6eSSagi Grimberg 		mutex_lock(&iser_conn->state_mutex);
8525716af6eSSagi Grimberg 		if (iser_conn->state == ISER_CONN_TERMINATING ||
8535716af6eSSagi Grimberg 		    iser_conn->state == ISER_CONN_DOWN)
85465e7ae7bSOr Gerlitz 			rc = -1;
8555716af6eSSagi Grimberg 		mutex_unlock(&iser_conn->state_mutex);
856504130c0SAriel Nahum 	}
85765e7ae7bSOr Gerlitz 
85874ce897bSSagi Grimberg 	iser_info("iser conn %p rc = %d\n", iser_conn, rc);
85965e7ae7bSOr Gerlitz 
86065e7ae7bSOr Gerlitz 	if (rc > 0)
861a9a08845SLinus Torvalds 		return 1; /* success, this is the equivalent of EPOLLOUT */
86265e7ae7bSOr Gerlitz 	else if (!rc)
86365e7ae7bSOr Gerlitz 		return 0; /* timeout */
86465e7ae7bSOr Gerlitz 	else
86565e7ae7bSOr Gerlitz 		return rc; /* signal */
86665e7ae7bSOr Gerlitz }
86765e7ae7bSOr Gerlitz 
868dc05ac36SSagi Grimberg /**
869dc05ac36SSagi Grimberg  * iscsi_iser_ep_disconnect() - Initiate connection teardown process
870dc05ac36SSagi Grimberg  * @ep:    iscsi endpoint handle
871dc05ac36SSagi Grimberg  *
872dc05ac36SSagi Grimberg  * This routine is not blocked by iser and RDMA termination process
873dc05ac36SSagi Grimberg  * completion as we queue a deffered work for iser/RDMA destruction
874dc05ac36SSagi Grimberg  * and cleanup or actually call it immediately in case we didn't pass
875dc05ac36SSagi Grimberg  * iscsi conn bind/start stage, thus it is safe.
876dc05ac36SSagi Grimberg  */
iscsi_iser_ep_disconnect(struct iscsi_endpoint * ep)877ca2770c6SMax Gurtovoy static void iscsi_iser_ep_disconnect(struct iscsi_endpoint *ep)
87865e7ae7bSOr Gerlitz {
87974ce897bSSagi Grimberg 	struct iser_conn *iser_conn = ep->dd_data;
88065e7ae7bSOr Gerlitz 
88174ce897bSSagi Grimberg 	iser_info("ep %p iser conn %p\n", ep, iser_conn);
8825716af6eSSagi Grimberg 
8835716af6eSSagi Grimberg 	mutex_lock(&iser_conn->state_mutex);
8845716af6eSSagi Grimberg 	iser_conn_terminate(iser_conn);
885b73c3adaSAriel Nahum 
886b73c3adaSAriel Nahum 	/*
8879a6d3234SAriel Nahum 	 * if iser_conn and iscsi_conn are bound, we must wait for
8889a6d3234SAriel Nahum 	 * iscsi_conn_stop and flush errors completion before freeing
8899a6d3234SAriel Nahum 	 * the iser resources. Otherwise we are safe to free resources
8909a6d3234SAriel Nahum 	 * immediately.
891b73c3adaSAriel Nahum 	 */
8925716af6eSSagi Grimberg 	if (iser_conn->iscsi_conn) {
8935716af6eSSagi Grimberg 		INIT_WORK(&iser_conn->release_work, iser_release_work);
8945716af6eSSagi Grimberg 		queue_work(release_wq, &iser_conn->release_work);
8955716af6eSSagi Grimberg 		mutex_unlock(&iser_conn->state_mutex);
896b73c3adaSAriel Nahum 	} else {
8975716af6eSSagi Grimberg 		iser_conn->state = ISER_CONN_DOWN;
8985716af6eSSagi Grimberg 		mutex_unlock(&iser_conn->state_mutex);
8995716af6eSSagi Grimberg 		iser_conn_release(iser_conn);
900b73c3adaSAriel Nahum 	}
9011156cc80SSagi Grimberg 
9020a690758SAriel Nahum 	iscsi_destroy_endpoint(ep);
90365e7ae7bSOr Gerlitz }
90465e7ae7bSOr Gerlitz 
iser_attr_is_visible(int param_type,int param)905587a1f16SAl Viro static umode_t iser_attr_is_visible(int param_type, int param)
9063128c6c7SMike Christie {
9073128c6c7SMike Christie 	switch (param_type) {
908f27fb2efSMike Christie 	case ISCSI_HOST_PARAM:
909f27fb2efSMike Christie 		switch (param) {
910f27fb2efSMike Christie 		case ISCSI_HOST_PARAM_NETDEV_NAME:
911f27fb2efSMike Christie 		case ISCSI_HOST_PARAM_HWADDRESS:
912f27fb2efSMike Christie 		case ISCSI_HOST_PARAM_INITIATOR_NAME:
913f27fb2efSMike Christie 			return S_IRUGO;
914f27fb2efSMike Christie 		default:
915f27fb2efSMike Christie 			return 0;
916f27fb2efSMike Christie 		}
9173128c6c7SMike Christie 	case ISCSI_PARAM:
9183128c6c7SMike Christie 		switch (param) {
9193128c6c7SMike Christie 		case ISCSI_PARAM_MAX_RECV_DLENGTH:
9203128c6c7SMike Christie 		case ISCSI_PARAM_MAX_XMIT_DLENGTH:
9213128c6c7SMike Christie 		case ISCSI_PARAM_HDRDGST_EN:
9223128c6c7SMike Christie 		case ISCSI_PARAM_DATADGST_EN:
9233128c6c7SMike Christie 		case ISCSI_PARAM_CONN_ADDRESS:
9243128c6c7SMike Christie 		case ISCSI_PARAM_CONN_PORT:
9253128c6c7SMike Christie 		case ISCSI_PARAM_EXP_STATSN:
9263128c6c7SMike Christie 		case ISCSI_PARAM_PERSISTENT_ADDRESS:
9273128c6c7SMike Christie 		case ISCSI_PARAM_PERSISTENT_PORT:
9283128c6c7SMike Christie 		case ISCSI_PARAM_PING_TMO:
9293128c6c7SMike Christie 		case ISCSI_PARAM_RECV_TMO:
9301d063c17SMike Christie 		case ISCSI_PARAM_INITIAL_R2T_EN:
9311d063c17SMike Christie 		case ISCSI_PARAM_MAX_R2T:
9321d063c17SMike Christie 		case ISCSI_PARAM_IMM_DATA_EN:
9331d063c17SMike Christie 		case ISCSI_PARAM_FIRST_BURST:
9341d063c17SMike Christie 		case ISCSI_PARAM_MAX_BURST:
9351d063c17SMike Christie 		case ISCSI_PARAM_PDU_INORDER_EN:
9361d063c17SMike Christie 		case ISCSI_PARAM_DATASEQ_INORDER_EN:
9371d063c17SMike Christie 		case ISCSI_PARAM_TARGET_NAME:
9381d063c17SMike Christie 		case ISCSI_PARAM_TPGT:
9391d063c17SMike Christie 		case ISCSI_PARAM_USERNAME:
9401d063c17SMike Christie 		case ISCSI_PARAM_PASSWORD:
9411d063c17SMike Christie 		case ISCSI_PARAM_USERNAME_IN:
9421d063c17SMike Christie 		case ISCSI_PARAM_PASSWORD_IN:
9431d063c17SMike Christie 		case ISCSI_PARAM_FAST_ABORT:
9441d063c17SMike Christie 		case ISCSI_PARAM_ABORT_TMO:
9451d063c17SMike Christie 		case ISCSI_PARAM_LU_RESET_TMO:
9461d063c17SMike Christie 		case ISCSI_PARAM_TGT_RESET_TMO:
9471d063c17SMike Christie 		case ISCSI_PARAM_IFACE_NAME:
9481d063c17SMike Christie 		case ISCSI_PARAM_INITIATOR_NAME:
9496a06a4b8SOr Gerlitz 		case ISCSI_PARAM_DISCOVERY_SESS:
9503128c6c7SMike Christie 			return S_IRUGO;
9513128c6c7SMike Christie 		default:
9523128c6c7SMike Christie 			return 0;
9533128c6c7SMike Christie 		}
9543128c6c7SMike Christie 	}
9553128c6c7SMike Christie 
9563128c6c7SMike Christie 	return 0;
9573128c6c7SMike Christie }
9583128c6c7SMike Christie 
959*80602acaSBart Van Assche static const struct scsi_host_template iscsi_iser_sht = {
9607974392cSMike Christie 	.module                 = THIS_MODULE,
961c1d786e6SRoi Dayan 	.name                   = "iSCSI Initiator over iSER",
96265e7ae7bSOr Gerlitz 	.queuecommand           = iscsi_queuecommand,
963db5ed4dfSChristoph Hellwig 	.change_queue_depth	= scsi_change_queue_depth,
964df749cdcSSagi Grimberg 	.sg_tablesize           = ISCSI_ISER_DEF_SG_TABLESIZE,
965e28f3d5bSMike Christie 	.cmd_per_lun            = ISER_DEF_CMD_PER_LUN,
966b6a05c82SChristoph Hellwig 	.eh_timed_out		= iscsi_eh_cmd_timed_out,
96765e7ae7bSOr Gerlitz 	.eh_abort_handler       = iscsi_eh_abort,
96890c18f3cSErez Zilber 	.eh_device_reset_handler= iscsi_eh_device_reset,
969309ce156SJayamohan Kallickal 	.eh_target_reset_handler = iscsi_eh_recover_target,
9706b5d6c44SMike Christie 	.target_alloc		= iscsi_target_alloc,
97165e7ae7bSOr Gerlitz 	.proc_name              = "iscsi_iser",
97265e7ae7bSOr Gerlitz 	.this_id                = -1,
973c40ecc12SChristoph Hellwig 	.track_queue_depth	= 1,
974db22de3eSBart Van Assche 	.cmd_size		= sizeof(struct iscsi_cmd),
97565e7ae7bSOr Gerlitz };
97665e7ae7bSOr Gerlitz 
97765e7ae7bSOr Gerlitz static struct iscsi_transport iscsi_iser_transport = {
97865e7ae7bSOr Gerlitz 	.owner                  = THIS_MODULE,
97965e7ae7bSOr Gerlitz 	.name                   = "iser",
9806a06a4b8SOr Gerlitz 	.caps                   = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_TEXT_NEGO,
98165e7ae7bSOr Gerlitz 	/* session management */
98265e7ae7bSOr Gerlitz 	.create_session         = iscsi_iser_session_create,
98375613521SMike Christie 	.destroy_session        = iscsi_iser_session_destroy,
98465e7ae7bSOr Gerlitz 	/* connection management */
98565e7ae7bSOr Gerlitz 	.create_conn            = iscsi_iser_conn_create,
98665e7ae7bSOr Gerlitz 	.bind_conn              = iscsi_iser_conn_bind,
987891e2639SMike Christie 	.unbind_conn		= iscsi_conn_unbind,
988b73c3adaSAriel Nahum 	.destroy_conn           = iscsi_conn_teardown,
9893128c6c7SMike Christie 	.attr_is_visible	= iser_attr_is_visible,
990358ff019SMike Christie 	.set_param              = iscsi_iser_set_param,
991358ff019SMike Christie 	.get_conn_param		= iscsi_conn_get_param,
9927c53c6f8SMike Christie 	.get_ep_param		= iscsi_iser_get_ep_param,
993358ff019SMike Christie 	.get_session_param	= iscsi_session_get_param,
994b73c3adaSAriel Nahum 	.start_conn             = iscsi_iser_conn_start,
995b40977d9SMike Christie 	.stop_conn              = iscsi_iser_conn_stop,
9960801c242SMike Christie 	/* iscsi host params */
9970801c242SMike Christie 	.get_host_param		= iscsi_host_get_param,
9980801c242SMike Christie 	.set_host_param		= iscsi_host_set_param,
99965e7ae7bSOr Gerlitz 	/* IO */
100065e7ae7bSOr Gerlitz 	.send_pdu		= iscsi_conn_send_pdu,
100165e7ae7bSOr Gerlitz 	.get_stats		= iscsi_iser_conn_get_stats,
10022747fdb2SMike Christie 	.init_task		= iscsi_iser_task_init,
10032747fdb2SMike Christie 	.xmit_task		= iscsi_iser_task_xmit,
10042747fdb2SMike Christie 	.cleanup_task		= iscsi_iser_cleanup_task,
10050f9c7449SMike Christie 	.alloc_pdu		= iscsi_iser_pdu_alloc,
10060a7a08adSSagi Grimberg 	.check_protection	= iscsi_iser_check_protection,
100765e7ae7bSOr Gerlitz 	/* recovery */
100865e7ae7bSOr Gerlitz 	.session_recovery_timedout = iscsi_session_recovery_timedout,
100965e7ae7bSOr Gerlitz 
101065e7ae7bSOr Gerlitz 	.ep_connect             = iscsi_iser_ep_connect,
101165e7ae7bSOr Gerlitz 	.ep_poll                = iscsi_iser_ep_poll,
101265e7ae7bSOr Gerlitz 	.ep_disconnect          = iscsi_iser_ep_disconnect
101365e7ae7bSOr Gerlitz };
101465e7ae7bSOr Gerlitz 
iser_init(void)101565e7ae7bSOr Gerlitz static int __init iser_init(void)
101665e7ae7bSOr Gerlitz {
101765e7ae7bSOr Gerlitz 	int err;
101865e7ae7bSOr Gerlitz 
101965e7ae7bSOr Gerlitz 	iser_dbg("Starting iSER datamover...\n");
102065e7ae7bSOr Gerlitz 
102165e7ae7bSOr Gerlitz 	memset(&ig, 0, sizeof(struct iser_global));
102265e7ae7bSOr Gerlitz 
102365e7ae7bSOr Gerlitz 	ig.desc_cache = kmem_cache_create("iser_descriptors",
1024f19624aaSOr Gerlitz 					  sizeof(struct iser_tx_desc),
102565e7ae7bSOr Gerlitz 					  0, SLAB_HWCACHE_ALIGN,
102620c2df83SPaul Mundt 					  NULL);
102765e7ae7bSOr Gerlitz 	if (ig.desc_cache == NULL)
102865e7ae7bSOr Gerlitz 		return -ENOMEM;
102965e7ae7bSOr Gerlitz 
103065e7ae7bSOr Gerlitz 	/* device init is called only after the first addr resolution */
103165e7ae7bSOr Gerlitz 	mutex_init(&ig.device_list_mutex);
103265e7ae7bSOr Gerlitz 	INIT_LIST_HEAD(&ig.device_list);
103365e7ae7bSOr Gerlitz 	mutex_init(&ig.connlist_mutex);
103465e7ae7bSOr Gerlitz 	INIT_LIST_HEAD(&ig.connlist);
103565e7ae7bSOr Gerlitz 
1036b73c3adaSAriel Nahum 	release_wq = alloc_workqueue("release workqueue", 0, 0);
1037b73c3adaSAriel Nahum 	if (!release_wq) {
1038b73c3adaSAriel Nahum 		iser_err("failed to allocate release workqueue\n");
103908ff089bSRoi Dayan 		err = -ENOMEM;
104008ff089bSRoi Dayan 		goto err_alloc_wq;
1041b73c3adaSAriel Nahum 	}
1042b73c3adaSAriel Nahum 
104375613521SMike Christie 	iscsi_iser_scsi_transport = iscsi_register_transport(
104475613521SMike Christie 							&iscsi_iser_transport);
104575613521SMike Christie 	if (!iscsi_iser_scsi_transport) {
104665e7ae7bSOr Gerlitz 		iser_err("iscsi_register_transport failed\n");
104765e7ae7bSOr Gerlitz 		err = -EINVAL;
104808ff089bSRoi Dayan 		goto err_reg;
104965e7ae7bSOr Gerlitz 	}
105065e7ae7bSOr Gerlitz 
105165e7ae7bSOr Gerlitz 	return 0;
105265e7ae7bSOr Gerlitz 
105308ff089bSRoi Dayan err_reg:
105408ff089bSRoi Dayan 	destroy_workqueue(release_wq);
105508ff089bSRoi Dayan err_alloc_wq:
105665e7ae7bSOr Gerlitz 	kmem_cache_destroy(ig.desc_cache);
105765e7ae7bSOr Gerlitz 
105865e7ae7bSOr Gerlitz 	return err;
105965e7ae7bSOr Gerlitz }
106065e7ae7bSOr Gerlitz 
iser_exit(void)106165e7ae7bSOr Gerlitz static void __exit iser_exit(void)
106265e7ae7bSOr Gerlitz {
10635716af6eSSagi Grimberg 	struct iser_conn *iser_conn, *n;
1064b73c3adaSAriel Nahum 	int connlist_empty;
1065b73c3adaSAriel Nahum 
106665e7ae7bSOr Gerlitz 	iser_dbg("Removing iSER datamover...\n");
1067b73c3adaSAriel Nahum 	destroy_workqueue(release_wq);
1068b73c3adaSAriel Nahum 
1069b73c3adaSAriel Nahum 	mutex_lock(&ig.connlist_mutex);
1070b73c3adaSAriel Nahum 	connlist_empty = list_empty(&ig.connlist);
1071b73c3adaSAriel Nahum 	mutex_unlock(&ig.connlist_mutex);
1072b73c3adaSAriel Nahum 
1073b73c3adaSAriel Nahum 	if (!connlist_empty) {
1074b73c3adaSAriel Nahum 		iser_err("Error cleanup stage completed but we still have iser "
10751156cc80SSagi Grimberg 			 "connections, destroying them anyway\n");
10765716af6eSSagi Grimberg 		list_for_each_entry_safe(iser_conn, n, &ig.connlist,
10775716af6eSSagi Grimberg 					 conn_list) {
10785716af6eSSagi Grimberg 			iser_conn_release(iser_conn);
1079b73c3adaSAriel Nahum 		}
1080b73c3adaSAriel Nahum 	}
1081b73c3adaSAriel Nahum 
108265e7ae7bSOr Gerlitz 	iscsi_unregister_transport(&iscsi_iser_transport);
108365e7ae7bSOr Gerlitz 	kmem_cache_destroy(ig.desc_cache);
108465e7ae7bSOr Gerlitz }
108565e7ae7bSOr Gerlitz 
108665e7ae7bSOr Gerlitz module_init(iser_init);
108765e7ae7bSOr Gerlitz module_exit(iser_exit);
1088