blob: 9a43cb07f294a80ec4d5cc0143053e566ac53866 [file] [log] [blame]
Or Gerlitz65e7ae72006-05-11 10:00:44 +03001/*
2 * iSCSI Initiator over iSER Data-Path
3 *
4 * Copyright (C) 2004 Dmitry Yusupov
5 * Copyright (C) 2004 Alex Aizman
6 * Copyright (C) 2005 Mike Christie
7 * Copyright (c) 2005, 2006 Voltaire, Inc. All rights reserved.
8 * maintained by openib-general@openib.org
9 *
10 * This software is available to you under a choice of one of two
11 * licenses. You may choose to be licensed under the terms of the GNU
12 * General Public License (GPL) Version 2, available from the file
13 * COPYING in the main directory of this source tree, or the
14 * OpenIB.org BSD license below:
15 *
16 * Redistribution and use in source and binary forms, with or
17 * without modification, are permitted provided that the following
18 * conditions are met:
19 *
20 * - Redistributions of source code must retain the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer.
23 *
24 * - Redistributions in binary form must reproduce the above
25 * copyright notice, this list of conditions and the following
26 * disclaimer in the documentation and/or other materials
27 * provided with the distribution.
28 *
29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
33 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
34 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
35 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 * SOFTWARE.
37 *
38 * Credits:
39 * Christoph Hellwig
40 * FUJITA Tomonori
41 * Arne Redlich
42 * Zhenyu Wang
43 * Modified by:
44 * Erez Zilber
Or Gerlitz65e7ae72006-05-11 10:00:44 +030045 */
46
47#include <linux/types.h>
48#include <linux/list.h>
49#include <linux/hardirq.h>
50#include <linux/kfifo.h>
51#include <linux/blkdev.h>
52#include <linux/init.h>
53#include <linux/ioctl.h>
Or Gerlitz65e7ae72006-05-11 10:00:44 +030054#include <linux/cdev.h>
55#include <linux/in.h>
56#include <linux/net.h>
57#include <linux/scatterlist.h>
58#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090059#include <linux/slab.h>
Paul Gortmakere4dd23d2011-05-27 15:35:46 -040060#include <linux/module.h>
Or Gerlitz65e7ae72006-05-11 10:00:44 +030061
62#include <net/sock.h>
63
64#include <asm/uaccess.h>
65
66#include <scsi/scsi_cmnd.h>
67#include <scsi/scsi_device.h>
68#include <scsi/scsi_eh.h>
69#include <scsi/scsi_tcq.h>
70#include <scsi/scsi_host.h>
71#include <scsi/scsi.h>
72#include <scsi/scsi_transport_iscsi.h>
73
74#include "iscsi_iser.h"
75
Mike Christie75613522008-05-21 15:53:59 -050076static struct scsi_host_template iscsi_iser_sht;
77static struct iscsi_transport iscsi_iser_transport;
78static struct scsi_transport_template *iscsi_iser_scsi_transport;
79
Or Gerlitz65e7ae72006-05-11 10:00:44 +030080static unsigned int iscsi_max_lun = 512;
81module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
82
83int iser_debug_level = 0;
84
85MODULE_DESCRIPTION("iSER (iSCSI Extensions for RDMA) Datamover "
86 "v" DRV_VER " (" DRV_DATE ")");
87MODULE_LICENSE("Dual BSD/GPL");
88MODULE_AUTHOR("Alex Nezhinsky, Dan Bar Dov, Or Gerlitz");
89
90module_param_named(debug_level, iser_debug_level, int, 0644);
91MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0 (default:disabled)");
92
93struct iser_global ig;
94
95void
96iscsi_iser_recv(struct iscsi_conn *conn,
97 struct iscsi_hdr *hdr, char *rx_data, int rx_data_len)
98{
99 int rc = 0;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300100 int datalen;
101 int ahslen;
102
103 /* verify PDU length */
104 datalen = ntoh24(hdr->dlength);
Or Gerlitz200ae1a2011-08-01 21:14:09 +0000105 if (datalen > rx_data_len || (datalen + 4) < rx_data_len) {
106 iser_err("wrong datalen %d (hdr), %d (IB)\n",
107 datalen, rx_data_len);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300108 rc = ISCSI_ERR_DATALEN;
109 goto error;
110 }
111
Or Gerlitz200ae1a2011-08-01 21:14:09 +0000112 if (datalen != rx_data_len)
113 iser_dbg("aligned datalen (%d) hdr, %d (IB)\n",
114 datalen, rx_data_len);
115
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300116 /* read AHS */
117 ahslen = hdr->hlength * 4;
118
Mike Christie0af967f2008-05-21 15:54:04 -0500119 rc = iscsi_complete_pdu(conn, hdr, rx_data, rx_data_len);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300120 if (rc && rc != ISCSI_ERR_NO_SCSI_CMD)
121 goto error;
122
123 return;
124error:
125 iscsi_conn_failure(conn, rc);
126}
127
Mike Christie2ff79d52008-12-02 00:32:14 -0600128static int iscsi_iser_pdu_alloc(struct iscsi_task *task, uint8_t opcode)
Mike Christie0f9c7442008-12-02 00:32:06 -0600129{
130 struct iscsi_iser_task *iser_task = task->dd_data;
131
132 task->hdr = (struct iscsi_hdr *)&iser_task->desc.iscsi_header;
133 task->hdr_max = sizeof(iser_task->desc.iscsi_header);
134 return 0;
135}
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300136
Or Gerlitzf19624a2010-02-08 13:19:56 +0000137int iser_initialize_task_headers(struct iscsi_task *task,
138 struct iser_tx_desc *tx_desc)
139{
140 struct iscsi_iser_conn *iser_conn = task->conn->dd_data;
141 struct iser_device *device = iser_conn->ib_conn->device;
142 struct iscsi_iser_task *iser_task = task->dd_data;
143 u64 dma_addr;
144
145 dma_addr = ib_dma_map_single(device->ib_device, (void *)tx_desc,
146 ISER_HEADERS_LEN, DMA_TO_DEVICE);
147 if (ib_dma_mapping_error(device->ib_device, dma_addr))
148 return -ENOMEM;
149
150 tx_desc->dma_addr = dma_addr;
151 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
152 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
153 tx_desc->tx_sg[0].lkey = device->mr->lkey;
154
Or Gerlitzf19624a2010-02-08 13:19:56 +0000155 iser_task->iser_conn = iser_conn;
156 return 0;
157}
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300158/**
Mike Christie2261ec32008-05-21 15:54:11 -0500159 * iscsi_iser_task_init - Initialize task
160 * @task: iscsi task
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300161 *
Mike Christie2261ec32008-05-21 15:54:11 -0500162 * Initialize the task for the scsi command or mgmt command.
Mike Christie2747fdb2008-05-21 15:54:08 -0500163 */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600164static int
Mike Christie2261ec32008-05-21 15:54:11 -0500165iscsi_iser_task_init(struct iscsi_task *task)
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300166{
Mike Christie2261ec32008-05-21 15:54:11 -0500167 struct iscsi_iser_task *iser_task = task->dd_data;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300168
Or Gerlitz52439542011-11-04 00:21:27 +0200169 if (iser_initialize_task_headers(task, &iser_task->desc))
Or Gerlitzf19624a2010-02-08 13:19:56 +0000170 return -ENOMEM;
171
Mike Christie2261ec32008-05-21 15:54:11 -0500172 /* mgmt task */
Or Gerlitzf19624a2010-02-08 13:19:56 +0000173 if (!task->sc)
Mike Christie2747fdb2008-05-21 15:54:08 -0500174 return 0;
Mike Christie2747fdb2008-05-21 15:54:08 -0500175
Mike Christie2261ec32008-05-21 15:54:11 -0500176 iser_task->command_sent = 0;
Mike Christie2261ec32008-05-21 15:54:11 -0500177 iser_task_rdma_init(iser_task);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600178 return 0;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300179}
180
181/**
Mike Christie2261ec32008-05-21 15:54:11 -0500182 * iscsi_iser_mtask_xmit - xmit management(immediate) task
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300183 * @conn: iscsi connection
Mike Christie2261ec32008-05-21 15:54:11 -0500184 * @task: task management task
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300185 *
186 * Notes:
187 * The function can return -EAGAIN in which case caller must
188 * call it again later, or recover. '0' return code means successful
189 * xmit.
190 *
191 **/
192static int
Mike Christie2261ec32008-05-21 15:54:11 -0500193iscsi_iser_mtask_xmit(struct iscsi_conn *conn, struct iscsi_task *task)
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300194{
195 int error = 0;
196
Or Gerlitz962b4b52010-02-08 13:22:34 +0000197 iser_dbg("mtask xmit [cid %d itt 0x%x]\n", conn->id, task->itt);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300198
Mike Christie2261ec32008-05-21 15:54:11 -0500199 error = iser_send_control(conn, task);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300200
Mike Christie2261ec32008-05-21 15:54:11 -0500201 /* since iser xmits control with zero copy, tasks can not be recycled
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300202 * right after sending them.
203 * The recycling scheme is based on whether a response is expected
Mike Christie2261ec32008-05-21 15:54:11 -0500204 * - if yes, the task is recycled at iscsi_complete_pdu
205 * - if no, the task is recycled at iser_snd_completion
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300206 */
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300207 return error;
208}
209
210static int
Mike Christie2747fdb2008-05-21 15:54:08 -0500211iscsi_iser_task_xmit_unsol_data(struct iscsi_conn *conn,
Mike Christie2261ec32008-05-21 15:54:11 -0500212 struct iscsi_task *task)
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300213{
Mike Christie0f9c7442008-12-02 00:32:06 -0600214 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
215 struct iscsi_data hdr;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300216 int error = 0;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300217
218 /* Send data-out PDUs while there's still unsolicited data to send */
Mike Christie0f9c7442008-12-02 00:32:06 -0600219 while (iscsi_task_has_unsol_data(task)) {
220 iscsi_prep_data_out_pdu(task, r2t, &hdr);
Mike Christie48a237a2009-03-05 14:45:57 -0600221 iser_dbg("Sending data-out: itt 0x%x, data count %d\n",
Mike Christie0f9c7442008-12-02 00:32:06 -0600222 hdr.itt, r2t->data_count);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300223
224 /* the buffer description has been passed with the command */
225 /* Send the command */
Mike Christie2261ec32008-05-21 15:54:11 -0500226 error = iser_send_data_out(conn, task, &hdr);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300227 if (error) {
Mike Christie0f9c7442008-12-02 00:32:06 -0600228 r2t->datasn--;
Mike Christie2747fdb2008-05-21 15:54:08 -0500229 goto iscsi_iser_task_xmit_unsol_data_exit;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300230 }
Mike Christie0f9c7442008-12-02 00:32:06 -0600231 r2t->sent += r2t->data_count;
Mike Christie48a237a2009-03-05 14:45:57 -0600232 iser_dbg("Need to send %d more as data-out PDUs\n",
Mike Christie0f9c7442008-12-02 00:32:06 -0600233 r2t->data_length - r2t->sent);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300234 }
235
Mike Christie2747fdb2008-05-21 15:54:08 -0500236iscsi_iser_task_xmit_unsol_data_exit:
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300237 return error;
238}
239
240static int
Mike Christie2261ec32008-05-21 15:54:11 -0500241iscsi_iser_task_xmit(struct iscsi_task *task)
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300242{
Mike Christie2261ec32008-05-21 15:54:11 -0500243 struct iscsi_conn *conn = task->conn;
244 struct iscsi_iser_task *iser_task = task->dd_data;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300245 int error = 0;
246
Mike Christie2261ec32008-05-21 15:54:11 -0500247 if (!task->sc)
248 return iscsi_iser_mtask_xmit(conn, task);
Mike Christie2747fdb2008-05-21 15:54:08 -0500249
Mike Christie2261ec32008-05-21 15:54:11 -0500250 if (task->sc->sc_data_direction == DMA_TO_DEVICE) {
251 BUG_ON(scsi_bufflen(task->sc) == 0);
Mike Christie77a23c22007-05-30 12:57:18 -0500252
Mike Christie48a237a2009-03-05 14:45:57 -0600253 iser_dbg("cmd [itt %x total %d imm %d unsol_data %d\n",
Mike Christie2261ec32008-05-21 15:54:11 -0500254 task->itt, scsi_bufflen(task->sc),
Mike Christie0f9c7442008-12-02 00:32:06 -0600255 task->imm_count, task->unsol_r2t.data_length);
Mike Christie77a23c22007-05-30 12:57:18 -0500256 }
257
Or Gerlitz962b4b52010-02-08 13:22:34 +0000258 iser_dbg("ctask xmit [cid %d itt 0x%x]\n",
Mike Christie2261ec32008-05-21 15:54:11 -0500259 conn->id, task->itt);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300260
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300261 /* Send the cmd PDU */
Mike Christie2261ec32008-05-21 15:54:11 -0500262 if (!iser_task->command_sent) {
263 error = iser_send_command(conn, task);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300264 if (error)
Mike Christie2747fdb2008-05-21 15:54:08 -0500265 goto iscsi_iser_task_xmit_exit;
Mike Christie2261ec32008-05-21 15:54:11 -0500266 iser_task->command_sent = 1;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300267 }
268
269 /* Send unsolicited data-out PDU(s) if necessary */
Mike Christie0f9c7442008-12-02 00:32:06 -0600270 if (iscsi_task_has_unsol_data(task))
Mike Christie2261ec32008-05-21 15:54:11 -0500271 error = iscsi_iser_task_xmit_unsol_data(conn, task);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300272
Mike Christie2747fdb2008-05-21 15:54:08 -0500273 iscsi_iser_task_xmit_exit:
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300274 return error;
275}
276
Mike Christie0f9c7442008-12-02 00:32:06 -0600277static void iscsi_iser_cleanup_task(struct iscsi_task *task)
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300278{
Mike Christie2261ec32008-05-21 15:54:11 -0500279 struct iscsi_iser_task *iser_task = task->dd_data;
Or Gerlitz52439542011-11-04 00:21:27 +0200280 struct iser_tx_desc *tx_desc = &iser_task->desc;
281
282 struct iscsi_iser_conn *iser_conn = task->conn->dd_data;
283 struct iser_device *device = iser_conn->ib_conn->device;
284
285 ib_dma_unmap_single(device->ib_device,
286 tx_desc->dma_addr, ISER_HEADERS_LEN, DMA_TO_DEVICE);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300287
Mike Christieb3cd5052009-05-13 17:57:49 -0500288 /* mgmt tasks do not need special cleanup */
289 if (!task->sc)
Mike Christie2747fdb2008-05-21 15:54:08 -0500290 return;
291
Mike Christie2261ec32008-05-21 15:54:11 -0500292 if (iser_task->status == ISER_TASK_STATUS_STARTED) {
293 iser_task->status = ISER_TASK_STATUS_COMPLETED;
294 iser_task_rdma_finalize(iser_task);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300295 }
296}
297
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300298static struct iscsi_cls_conn *
299iscsi_iser_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
300{
301 struct iscsi_conn *conn;
302 struct iscsi_cls_conn *cls_conn;
303 struct iscsi_iser_conn *iser_conn;
304
Mike Christie5d91e202008-05-21 15:54:01 -0500305 cls_conn = iscsi_conn_setup(cls_session, sizeof(*iser_conn), conn_idx);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300306 if (!cls_conn)
307 return NULL;
308 conn = cls_conn->dd_data;
309
310 /*
311 * due to issues with the login code re iser sematics
312 * this not set in iscsi_conn_setup - FIXME
313 */
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000314 conn->max_recv_dlength = ISER_RECV_DATA_SEG_LEN;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300315
Mike Christie5d91e202008-05-21 15:54:01 -0500316 iser_conn = conn->dd_data;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300317 conn->dd_data = iser_conn;
318 iser_conn->iscsi_conn = conn;
319
320 return cls_conn;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300321}
322
323static void
324iscsi_iser_conn_destroy(struct iscsi_cls_conn *cls_conn)
325{
326 struct iscsi_conn *conn = cls_conn->dd_data;
327 struct iscsi_iser_conn *iser_conn = conn->dd_data;
Mike Christieb40977d2008-05-21 15:54:03 -0500328 struct iser_conn *ib_conn = iser_conn->ib_conn;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300329
330 iscsi_conn_teardown(cls_conn);
Mike Christieb40977d2008-05-21 15:54:03 -0500331 /*
332 * Userspace will normally call the stop callback and
333 * already have freed the ib_conn, but if it goofed up then
334 * we free it here.
335 */
336 if (ib_conn) {
337 ib_conn->iser_conn = NULL;
Or Gerlitz39ff05d2010-05-05 17:31:44 +0300338 iser_conn_put(ib_conn, 1); /* deref iscsi/ib conn unbinding */
Mike Christieb40977d2008-05-21 15:54:03 -0500339 }
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300340}
341
342static int
343iscsi_iser_conn_bind(struct iscsi_cls_session *cls_session,
344 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
345 int is_leading)
346{
347 struct iscsi_conn *conn = cls_conn->dd_data;
348 struct iscsi_iser_conn *iser_conn;
349 struct iser_conn *ib_conn;
Mike Christie412eeaf2008-05-21 15:54:14 -0500350 struct iscsi_endpoint *ep;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300351 int error;
352
353 error = iscsi_conn_bind(cls_session, cls_conn, is_leading);
354 if (error)
355 return error;
356
357 /* the transport ep handle comes from user space so it must be
358 * verified against the global ib connections list */
Mike Christie412eeaf2008-05-21 15:54:14 -0500359 ep = iscsi_lookup_endpoint(transport_eph);
360 if (!ep) {
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300361 iser_err("can't bind eph %llx\n",
362 (unsigned long long)transport_eph);
363 return -EINVAL;
364 }
Mike Christie412eeaf2008-05-21 15:54:14 -0500365 ib_conn = ep->dd_data;
366
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300367 /* binds the iSER connection retrieved from the previously
368 * connected ep_handle to the iSCSI layer connection. exchanges
369 * connection pointers */
Or Gerlitz39ff05d2010-05-05 17:31:44 +0300370 iser_err("binding iscsi/iser conn %p %p to ib_conn %p\n",
371 conn, conn->dd_data, ib_conn);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300372 iser_conn = conn->dd_data;
373 ib_conn->iser_conn = iser_conn;
374 iser_conn->ib_conn = ib_conn;
Or Gerlitz39ff05d2010-05-05 17:31:44 +0300375 iser_conn_get(ib_conn); /* ref iscsi/ib conn binding */
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300376 return 0;
377}
378
Mike Christieb40977d2008-05-21 15:54:03 -0500379static void
380iscsi_iser_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
381{
382 struct iscsi_conn *conn = cls_conn->dd_data;
383 struct iscsi_iser_conn *iser_conn = conn->dd_data;
384 struct iser_conn *ib_conn = iser_conn->ib_conn;
385
Mike Christieb40977d2008-05-21 15:54:03 -0500386 /*
Mike Christie913e5bf2008-05-21 15:54:18 -0500387 * Userspace may have goofed up and not bound the connection or
388 * might have only partially setup the connection.
Mike Christieb40977d2008-05-21 15:54:03 -0500389 */
Mike Christie913e5bf2008-05-21 15:54:18 -0500390 if (ib_conn) {
391 iscsi_conn_stop(cls_conn, flag);
392 /*
393 * There is no unbind event so the stop callback
394 * must release the ref from the bind.
395 */
Or Gerlitz39ff05d2010-05-05 17:31:44 +0300396 iser_conn_put(ib_conn, 1); /* deref iscsi/ib conn unbinding */
Mike Christie913e5bf2008-05-21 15:54:18 -0500397 }
Mike Christieb40977d2008-05-21 15:54:03 -0500398 iser_conn->ib_conn = NULL;
399}
400
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300401static int
402iscsi_iser_conn_start(struct iscsi_cls_conn *cls_conn)
403{
404 struct iscsi_conn *conn = cls_conn->dd_data;
405 int err;
406
Erez Zilber2e7a7422006-10-22 10:28:38 +0200407 err = iser_conn_set_full_featured_mode(conn);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300408 if (err)
409 return err;
410
Erez Zilber2e7a7422006-10-22 10:28:38 +0200411 return iscsi_conn_start(cls_conn);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300412}
413
Mike Christie75613522008-05-21 15:53:59 -0500414static void iscsi_iser_session_destroy(struct iscsi_cls_session *cls_session)
415{
416 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
417
Mike Christiee5bd7b52008-09-24 11:46:10 -0500418 iscsi_session_teardown(cls_session);
Mike Christiea4804cd2008-05-21 15:54:00 -0500419 iscsi_host_remove(shost);
420 iscsi_host_free(shost);
Mike Christie75613522008-05-21 15:53:59 -0500421}
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300422
423static struct iscsi_cls_session *
Mike Christie412eeaf2008-05-21 15:54:14 -0500424iscsi_iser_session_create(struct iscsi_endpoint *ep,
Mike Christie75613522008-05-21 15:53:59 -0500425 uint16_t cmds_max, uint16_t qdepth,
Mike Christie5e7facb2009-03-05 14:46:06 -0600426 uint32_t initial_cmdsn)
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300427{
428 struct iscsi_cls_session *cls_session;
429 struct iscsi_session *session;
Mike Christie412eeaf2008-05-21 15:54:14 -0500430 struct Scsi_Host *shost;
Mike Christie412eeaf2008-05-21 15:54:14 -0500431 struct iser_conn *ib_conn;
Mike Christie75613522008-05-21 15:53:59 -0500432
Or Gerlitz962b4b52010-02-08 13:22:34 +0000433 shost = iscsi_host_alloc(&iscsi_iser_sht, 0, 0);
Mike Christie75613522008-05-21 15:53:59 -0500434 if (!shost)
435 return NULL;
436 shost->transportt = iscsi_iser_scsi_transport;
437 shost->max_lun = iscsi_max_lun;
438 shost->max_id = 0;
439 shost->max_channel = 0;
440 shost->max_cmd_len = 16;
441
Mike Christie412eeaf2008-05-21 15:54:14 -0500442 /*
443 * older userspace tools (before 2.0-870) did not pass us
444 * the leading conn's ep so this will be NULL;
445 */
446 if (ep)
447 ib_conn = ep->dd_data;
448
449 if (iscsi_host_add(shost,
450 ep ? ib_conn->device->ib_device->dma_device : NULL))
Mike Christie75613522008-05-21 15:53:59 -0500451 goto free_host;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300452
Mike Christie15482712007-05-30 12:57:19 -0500453 /*
454 * we do not support setting can_queue cmd_per_lun from userspace yet
455 * because we preallocate so many resources
456 */
Mike Christie75613522008-05-21 15:53:59 -0500457 cls_session = iscsi_session_setup(&iscsi_iser_transport, shost,
Jayamohan Kallickalb8b9e1b82009-09-22 08:21:22 +0530458 ISCSI_DEF_XMIT_CMDS_MAX, 0,
Mike Christie2261ec32008-05-21 15:54:11 -0500459 sizeof(struct iscsi_iser_task),
Mike Christie79706342008-05-21 15:54:12 -0500460 initial_cmdsn, 0);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300461 if (!cls_session)
Mike Christie75613522008-05-21 15:53:59 -0500462 goto remove_host;
463 session = cls_session->dd_data;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300464
Mike Christie2747fdb2008-05-21 15:54:08 -0500465 shost->can_queue = session->scsi_cmds_max;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300466 return cls_session;
Mike Christie75613522008-05-21 15:53:59 -0500467
468remove_host:
Mike Christiea4804cd2008-05-21 15:54:00 -0500469 iscsi_host_remove(shost);
Mike Christie75613522008-05-21 15:53:59 -0500470free_host:
Mike Christiea4804cd2008-05-21 15:54:00 -0500471 iscsi_host_free(shost);
Mike Christie75613522008-05-21 15:53:59 -0500472 return NULL;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300473}
474
475static int
Mike Christie358ff012006-06-28 12:00:25 -0500476iscsi_iser_set_param(struct iscsi_cls_conn *cls_conn,
477 enum iscsi_param param, char *buf, int buflen)
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300478{
Mike Christie358ff012006-06-28 12:00:25 -0500479 int value;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300480
481 switch (param) {
482 case ISCSI_PARAM_MAX_RECV_DLENGTH:
483 /* TBD */
484 break;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300485 case ISCSI_PARAM_HDRDGST_EN:
Mike Christie358ff012006-06-28 12:00:25 -0500486 sscanf(buf, "%d", &value);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300487 if (value) {
488 printk(KERN_ERR "DataDigest wasn't negotiated to None");
489 return -EPROTO;
490 }
491 break;
492 case ISCSI_PARAM_DATADGST_EN:
Mike Christie358ff012006-06-28 12:00:25 -0500493 sscanf(buf, "%d", &value);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300494 if (value) {
495 printk(KERN_ERR "DataDigest wasn't negotiated to None");
496 return -EPROTO;
497 }
498 break;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300499 case ISCSI_PARAM_IFMARKER_EN:
Mike Christie358ff012006-06-28 12:00:25 -0500500 sscanf(buf, "%d", &value);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300501 if (value) {
502 printk(KERN_ERR "IFMarker wasn't negotiated to No");
503 return -EPROTO;
504 }
505 break;
506 case ISCSI_PARAM_OFMARKER_EN:
Mike Christie358ff012006-06-28 12:00:25 -0500507 sscanf(buf, "%d", &value);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300508 if (value) {
509 printk(KERN_ERR "OFMarker wasn't negotiated to No");
510 return -EPROTO;
511 }
512 break;
513 default:
Mike Christie358ff012006-06-28 12:00:25 -0500514 return iscsi_set_param(cls_conn, param, buf, buflen);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300515 }
516
517 return 0;
518}
519
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300520static void
521iscsi_iser_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
522{
523 struct iscsi_conn *conn = cls_conn->dd_data;
524
525 stats->txdata_octets = conn->txdata_octets;
526 stats->rxdata_octets = conn->rxdata_octets;
527 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
528 stats->dataout_pdus = conn->dataout_pdus_cnt;
529 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
530 stats->datain_pdus = conn->datain_pdus_cnt; /* always 0 */
531 stats->r2t_pdus = conn->r2t_pdus_cnt; /* always 0 */
532 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
533 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
Eli Dorfman87528222008-04-29 13:46:52 -0700534 stats->custom_length = 4;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300535 strcpy(stats->custom[0].desc, "qp_tx_queue_full");
536 stats->custom[0].value = 0; /* TB iser_conn->qp_tx_queue_full; */
537 strcpy(stats->custom[1].desc, "fmr_map_not_avail");
538 stats->custom[1].value = 0; /* TB iser_conn->fmr_map_not_avail */;
539 strcpy(stats->custom[2].desc, "eh_abort_cnt");
540 stats->custom[2].value = conn->eh_abort_cnt;
Eli Dorfman87528222008-04-29 13:46:52 -0700541 strcpy(stats->custom[3].desc, "fmr_unalign_cnt");
542 stats->custom[3].value = conn->fmr_unalign_cnt;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300543}
544
Mike Christie7c53c6f2011-02-16 15:04:40 -0600545static int iscsi_iser_get_ep_param(struct iscsi_endpoint *ep,
546 enum iscsi_param param, char *buf)
547{
548 struct iser_conn *ib_conn = ep->dd_data;
549 int len;
550
551 switch (param) {
552 case ISCSI_PARAM_CONN_PORT:
553 case ISCSI_PARAM_CONN_ADDRESS:
554 if (!ib_conn || !ib_conn->cma_id)
555 return -ENOTCONN;
556
557 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
558 &ib_conn->cma_id->route.addr.dst_addr,
559 param, buf);
560 break;
561 default:
562 return -ENOSYS;
563 }
564
565 return len;
566}
567
Mike Christie412eeaf2008-05-21 15:54:14 -0500568static struct iscsi_endpoint *
Mike Christie10eb0f02009-05-13 17:57:38 -0500569iscsi_iser_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
570 int non_blocking)
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300571{
572 int err;
573 struct iser_conn *ib_conn;
Mike Christie412eeaf2008-05-21 15:54:14 -0500574 struct iscsi_endpoint *ep;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300575
Mike Christie412eeaf2008-05-21 15:54:14 -0500576 ep = iscsi_create_endpoint(sizeof(*ib_conn));
577 if (!ep)
578 return ERR_PTR(-ENOMEM);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300579
Mike Christie412eeaf2008-05-21 15:54:14 -0500580 ib_conn = ep->dd_data;
581 ib_conn->ep = ep;
582 iser_conn_init(ib_conn);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300583
Mike Christie412eeaf2008-05-21 15:54:14 -0500584 err = iser_connect(ib_conn, NULL, (struct sockaddr_in *)dst_addr,
585 non_blocking);
586 if (err) {
587 iscsi_destroy_endpoint(ep);
588 return ERR_PTR(err);
589 }
590 return ep;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300591}
592
593static int
Mike Christie412eeaf2008-05-21 15:54:14 -0500594iscsi_iser_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300595{
Mike Christie412eeaf2008-05-21 15:54:14 -0500596 struct iser_conn *ib_conn;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300597 int rc;
598
Mike Christie412eeaf2008-05-21 15:54:14 -0500599 ib_conn = ep->dd_data;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300600 rc = wait_event_interruptible_timeout(ib_conn->wait,
601 ib_conn->state == ISER_CONN_UP,
602 msecs_to_jiffies(timeout_ms));
603
604 /* if conn establishment failed, return error code to iscsi */
605 if (!rc &&
606 (ib_conn->state == ISER_CONN_TERMINATING ||
607 ib_conn->state == ISER_CONN_DOWN))
608 rc = -1;
609
610 iser_err("ib conn %p rc = %d\n", ib_conn, rc);
611
612 if (rc > 0)
613 return 1; /* success, this is the equivalent of POLLOUT */
614 else if (!rc)
615 return 0; /* timeout */
616 else
617 return rc; /* signal */
618}
619
620static void
Mike Christie412eeaf2008-05-21 15:54:14 -0500621iscsi_iser_ep_disconnect(struct iscsi_endpoint *ep)
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300622{
Mike Christie1c834692006-07-24 15:47:26 -0500623 struct iser_conn *ib_conn;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300624
Mike Christie412eeaf2008-05-21 15:54:14 -0500625 ib_conn = ep->dd_data;
Mike Christieb40977d2008-05-21 15:54:03 -0500626 if (ib_conn->iser_conn)
627 /*
628 * Must suspend xmit path if the ep is bound to the
629 * iscsi_conn, so we know we are not accessing the ib_conn
630 * when we free it.
631 *
632 * This may not be bound if the ep poll failed.
633 */
634 iscsi_suspend_tx(ib_conn->iser_conn->iscsi_conn);
635
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300636
637 iser_err("ib conn %p state %d\n",ib_conn, ib_conn->state);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300638 iser_conn_terminate(ib_conn);
639}
640
Al Viro587a1f12011-07-23 23:11:19 -0400641static umode_t iser_attr_is_visible(int param_type, int param)
Mike Christie3128c6c2011-07-25 13:48:42 -0500642{
643 switch (param_type) {
Mike Christief27fb2e2011-07-25 13:48:45 -0500644 case ISCSI_HOST_PARAM:
645 switch (param) {
646 case ISCSI_HOST_PARAM_NETDEV_NAME:
647 case ISCSI_HOST_PARAM_HWADDRESS:
648 case ISCSI_HOST_PARAM_INITIATOR_NAME:
649 return S_IRUGO;
650 default:
651 return 0;
652 }
Mike Christie3128c6c2011-07-25 13:48:42 -0500653 case ISCSI_PARAM:
654 switch (param) {
655 case ISCSI_PARAM_MAX_RECV_DLENGTH:
656 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
657 case ISCSI_PARAM_HDRDGST_EN:
658 case ISCSI_PARAM_DATADGST_EN:
659 case ISCSI_PARAM_CONN_ADDRESS:
660 case ISCSI_PARAM_CONN_PORT:
661 case ISCSI_PARAM_EXP_STATSN:
662 case ISCSI_PARAM_PERSISTENT_ADDRESS:
663 case ISCSI_PARAM_PERSISTENT_PORT:
664 case ISCSI_PARAM_PING_TMO:
665 case ISCSI_PARAM_RECV_TMO:
Mike Christie1d063c12011-07-25 13:48:43 -0500666 case ISCSI_PARAM_INITIAL_R2T_EN:
667 case ISCSI_PARAM_MAX_R2T:
668 case ISCSI_PARAM_IMM_DATA_EN:
669 case ISCSI_PARAM_FIRST_BURST:
670 case ISCSI_PARAM_MAX_BURST:
671 case ISCSI_PARAM_PDU_INORDER_EN:
672 case ISCSI_PARAM_DATASEQ_INORDER_EN:
673 case ISCSI_PARAM_TARGET_NAME:
674 case ISCSI_PARAM_TPGT:
675 case ISCSI_PARAM_USERNAME:
676 case ISCSI_PARAM_PASSWORD:
677 case ISCSI_PARAM_USERNAME_IN:
678 case ISCSI_PARAM_PASSWORD_IN:
679 case ISCSI_PARAM_FAST_ABORT:
680 case ISCSI_PARAM_ABORT_TMO:
681 case ISCSI_PARAM_LU_RESET_TMO:
682 case ISCSI_PARAM_TGT_RESET_TMO:
683 case ISCSI_PARAM_IFACE_NAME:
684 case ISCSI_PARAM_INITIATOR_NAME:
Mike Christie3128c6c2011-07-25 13:48:42 -0500685 return S_IRUGO;
686 default:
687 return 0;
688 }
689 }
690
691 return 0;
692}
693
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300694static struct scsi_host_template iscsi_iser_sht = {
Mike Christie79743922007-07-26 12:46:46 -0500695 .module = THIS_MODULE,
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300696 .name = "iSCSI Initiator over iSER, v." DRV_VER,
697 .queuecommand = iscsi_queuecommand,
Erez Zilber64106272008-01-17 11:53:17 +0200698 .change_queue_depth = iscsi_change_queue_depth,
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300699 .sg_tablesize = ISCSI_ISER_SG_TABLESIZE,
Erez Zilber8072ec22006-09-11 12:20:54 +0300700 .max_sectors = 1024,
Mike Christiee28f3d52009-03-05 14:46:01 -0600701 .cmd_per_lun = ISER_DEF_CMD_PER_LUN,
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300702 .eh_abort_handler = iscsi_eh_abort,
Erez Zilber90c18f32008-01-22 12:06:25 +0200703 .eh_device_reset_handler= iscsi_eh_device_reset,
Jayamohan Kallickal309ce152010-02-20 08:02:10 +0530704 .eh_target_reset_handler = iscsi_eh_recover_target,
Mike Christie6b5d6c42009-04-21 15:32:32 -0500705 .target_alloc = iscsi_target_alloc,
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300706 .use_clustering = DISABLE_CLUSTERING,
707 .proc_name = "iscsi_iser",
708 .this_id = -1,
709};
710
711static struct iscsi_transport iscsi_iser_transport = {
712 .owner = THIS_MODULE,
713 .name = "iser",
714 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T,
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300715 /* session management */
716 .create_session = iscsi_iser_session_create,
Mike Christie75613522008-05-21 15:53:59 -0500717 .destroy_session = iscsi_iser_session_destroy,
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300718 /* connection management */
719 .create_conn = iscsi_iser_conn_create,
720 .bind_conn = iscsi_iser_conn_bind,
721 .destroy_conn = iscsi_iser_conn_destroy,
Mike Christie3128c6c2011-07-25 13:48:42 -0500722 .attr_is_visible = iser_attr_is_visible,
Mike Christie358ff012006-06-28 12:00:25 -0500723 .set_param = iscsi_iser_set_param,
724 .get_conn_param = iscsi_conn_get_param,
Mike Christie7c53c6f2011-02-16 15:04:40 -0600725 .get_ep_param = iscsi_iser_get_ep_param,
Mike Christie358ff012006-06-28 12:00:25 -0500726 .get_session_param = iscsi_session_get_param,
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300727 .start_conn = iscsi_iser_conn_start,
Mike Christieb40977d2008-05-21 15:54:03 -0500728 .stop_conn = iscsi_iser_conn_stop,
Mike Christie0801c242007-05-30 12:57:12 -0500729 /* iscsi host params */
730 .get_host_param = iscsi_host_get_param,
731 .set_host_param = iscsi_host_set_param,
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300732 /* IO */
733 .send_pdu = iscsi_conn_send_pdu,
734 .get_stats = iscsi_iser_conn_get_stats,
Mike Christie2747fdb2008-05-21 15:54:08 -0500735 .init_task = iscsi_iser_task_init,
736 .xmit_task = iscsi_iser_task_xmit,
737 .cleanup_task = iscsi_iser_cleanup_task,
Mike Christie0f9c7442008-12-02 00:32:06 -0600738 .alloc_pdu = iscsi_iser_pdu_alloc,
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300739 /* recovery */
740 .session_recovery_timedout = iscsi_session_recovery_timedout,
741
742 .ep_connect = iscsi_iser_ep_connect,
743 .ep_poll = iscsi_iser_ep_poll,
744 .ep_disconnect = iscsi_iser_ep_disconnect
745};
746
747static int __init iser_init(void)
748{
749 int err;
750
751 iser_dbg("Starting iSER datamover...\n");
752
753 if (iscsi_max_lun < 1) {
754 printk(KERN_ERR "Invalid max_lun value of %u\n", iscsi_max_lun);
755 return -EINVAL;
756 }
757
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300758 memset(&ig, 0, sizeof(struct iser_global));
759
760 ig.desc_cache = kmem_cache_create("iser_descriptors",
Or Gerlitzf19624a2010-02-08 13:19:56 +0000761 sizeof(struct iser_tx_desc),
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300762 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +0900763 NULL);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300764 if (ig.desc_cache == NULL)
765 return -ENOMEM;
766
767 /* device init is called only after the first addr resolution */
768 mutex_init(&ig.device_list_mutex);
769 INIT_LIST_HEAD(&ig.device_list);
770 mutex_init(&ig.connlist_mutex);
771 INIT_LIST_HEAD(&ig.connlist);
772
Mike Christie75613522008-05-21 15:53:59 -0500773 iscsi_iser_scsi_transport = iscsi_register_transport(
774 &iscsi_iser_transport);
775 if (!iscsi_iser_scsi_transport) {
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300776 iser_err("iscsi_register_transport failed\n");
777 err = -EINVAL;
778 goto register_transport_failure;
779 }
780
781 return 0;
782
783register_transport_failure:
784 kmem_cache_destroy(ig.desc_cache);
785
786 return err;
787}
788
789static void __exit iser_exit(void)
790{
791 iser_dbg("Removing iSER datamover...\n");
792 iscsi_unregister_transport(&iscsi_iser_transport);
793 kmem_cache_destroy(ig.desc_cache);
794}
795
796module_init(iser_init);
797module_exit(iser_exit);