blob: 526310768d9a2be5698780d28a22c1d4c769b72f [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
155 iser_task->headers_initialized = 1;
156 iser_task->iser_conn = iser_conn;
157 return 0;
158}
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300159/**
Mike Christie2261ec32008-05-21 15:54:11 -0500160 * iscsi_iser_task_init - Initialize task
161 * @task: iscsi task
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300162 *
Mike Christie2261ec32008-05-21 15:54:11 -0500163 * Initialize the task for the scsi command or mgmt command.
Mike Christie2747fdb2008-05-21 15:54:08 -0500164 */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600165static int
Mike Christie2261ec32008-05-21 15:54:11 -0500166iscsi_iser_task_init(struct iscsi_task *task)
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300167{
Mike Christie2261ec32008-05-21 15:54:11 -0500168 struct iscsi_iser_task *iser_task = task->dd_data;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300169
Or Gerlitzf19624a2010-02-08 13:19:56 +0000170 if (!iser_task->headers_initialized)
171 if (iser_initialize_task_headers(task, &iser_task->desc))
172 return -ENOMEM;
173
Mike Christie2261ec32008-05-21 15:54:11 -0500174 /* mgmt task */
Or Gerlitzf19624a2010-02-08 13:19:56 +0000175 if (!task->sc)
Mike Christie2747fdb2008-05-21 15:54:08 -0500176 return 0;
Mike Christie2747fdb2008-05-21 15:54:08 -0500177
Mike Christie2261ec32008-05-21 15:54:11 -0500178 iser_task->command_sent = 0;
Mike Christie2261ec32008-05-21 15:54:11 -0500179 iser_task_rdma_init(iser_task);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600180 return 0;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300181}
182
183/**
Mike Christie2261ec32008-05-21 15:54:11 -0500184 * iscsi_iser_mtask_xmit - xmit management(immediate) task
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300185 * @conn: iscsi connection
Mike Christie2261ec32008-05-21 15:54:11 -0500186 * @task: task management task
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300187 *
188 * Notes:
189 * The function can return -EAGAIN in which case caller must
190 * call it again later, or recover. '0' return code means successful
191 * xmit.
192 *
193 **/
194static int
Mike Christie2261ec32008-05-21 15:54:11 -0500195iscsi_iser_mtask_xmit(struct iscsi_conn *conn, struct iscsi_task *task)
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300196{
197 int error = 0;
198
Or Gerlitz962b4b52010-02-08 13:22:34 +0000199 iser_dbg("mtask xmit [cid %d itt 0x%x]\n", conn->id, task->itt);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300200
Mike Christie2261ec32008-05-21 15:54:11 -0500201 error = iser_send_control(conn, task);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300202
Mike Christie2261ec32008-05-21 15:54:11 -0500203 /* since iser xmits control with zero copy, tasks can not be recycled
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300204 * right after sending them.
205 * The recycling scheme is based on whether a response is expected
Mike Christie2261ec32008-05-21 15:54:11 -0500206 * - if yes, the task is recycled at iscsi_complete_pdu
207 * - if no, the task is recycled at iser_snd_completion
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300208 */
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300209 return error;
210}
211
212static int
Mike Christie2747fdb2008-05-21 15:54:08 -0500213iscsi_iser_task_xmit_unsol_data(struct iscsi_conn *conn,
Mike Christie2261ec32008-05-21 15:54:11 -0500214 struct iscsi_task *task)
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300215{
Mike Christie0f9c7442008-12-02 00:32:06 -0600216 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
217 struct iscsi_data hdr;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300218 int error = 0;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300219
220 /* Send data-out PDUs while there's still unsolicited data to send */
Mike Christie0f9c7442008-12-02 00:32:06 -0600221 while (iscsi_task_has_unsol_data(task)) {
222 iscsi_prep_data_out_pdu(task, r2t, &hdr);
Mike Christie48a237a2009-03-05 14:45:57 -0600223 iser_dbg("Sending data-out: itt 0x%x, data count %d\n",
Mike Christie0f9c7442008-12-02 00:32:06 -0600224 hdr.itt, r2t->data_count);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300225
226 /* the buffer description has been passed with the command */
227 /* Send the command */
Mike Christie2261ec32008-05-21 15:54:11 -0500228 error = iser_send_data_out(conn, task, &hdr);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300229 if (error) {
Mike Christie0f9c7442008-12-02 00:32:06 -0600230 r2t->datasn--;
Mike Christie2747fdb2008-05-21 15:54:08 -0500231 goto iscsi_iser_task_xmit_unsol_data_exit;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300232 }
Mike Christie0f9c7442008-12-02 00:32:06 -0600233 r2t->sent += r2t->data_count;
Mike Christie48a237a2009-03-05 14:45:57 -0600234 iser_dbg("Need to send %d more as data-out PDUs\n",
Mike Christie0f9c7442008-12-02 00:32:06 -0600235 r2t->data_length - r2t->sent);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300236 }
237
Mike Christie2747fdb2008-05-21 15:54:08 -0500238iscsi_iser_task_xmit_unsol_data_exit:
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300239 return error;
240}
241
242static int
Mike Christie2261ec32008-05-21 15:54:11 -0500243iscsi_iser_task_xmit(struct iscsi_task *task)
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300244{
Mike Christie2261ec32008-05-21 15:54:11 -0500245 struct iscsi_conn *conn = task->conn;
246 struct iscsi_iser_task *iser_task = task->dd_data;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300247 int error = 0;
248
Mike Christie2261ec32008-05-21 15:54:11 -0500249 if (!task->sc)
250 return iscsi_iser_mtask_xmit(conn, task);
Mike Christie2747fdb2008-05-21 15:54:08 -0500251
Mike Christie2261ec32008-05-21 15:54:11 -0500252 if (task->sc->sc_data_direction == DMA_TO_DEVICE) {
253 BUG_ON(scsi_bufflen(task->sc) == 0);
Mike Christie77a23c22007-05-30 12:57:18 -0500254
Mike Christie48a237a2009-03-05 14:45:57 -0600255 iser_dbg("cmd [itt %x total %d imm %d unsol_data %d\n",
Mike Christie2261ec32008-05-21 15:54:11 -0500256 task->itt, scsi_bufflen(task->sc),
Mike Christie0f9c7442008-12-02 00:32:06 -0600257 task->imm_count, task->unsol_r2t.data_length);
Mike Christie77a23c22007-05-30 12:57:18 -0500258 }
259
Or Gerlitz962b4b52010-02-08 13:22:34 +0000260 iser_dbg("ctask xmit [cid %d itt 0x%x]\n",
Mike Christie2261ec32008-05-21 15:54:11 -0500261 conn->id, task->itt);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300262
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300263 /* Send the cmd PDU */
Mike Christie2261ec32008-05-21 15:54:11 -0500264 if (!iser_task->command_sent) {
265 error = iser_send_command(conn, task);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300266 if (error)
Mike Christie2747fdb2008-05-21 15:54:08 -0500267 goto iscsi_iser_task_xmit_exit;
Mike Christie2261ec32008-05-21 15:54:11 -0500268 iser_task->command_sent = 1;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300269 }
270
271 /* Send unsolicited data-out PDU(s) if necessary */
Mike Christie0f9c7442008-12-02 00:32:06 -0600272 if (iscsi_task_has_unsol_data(task))
Mike Christie2261ec32008-05-21 15:54:11 -0500273 error = iscsi_iser_task_xmit_unsol_data(conn, task);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300274
Mike Christie2747fdb2008-05-21 15:54:08 -0500275 iscsi_iser_task_xmit_exit:
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300276 return error;
277}
278
Mike Christie0f9c7442008-12-02 00:32:06 -0600279static void iscsi_iser_cleanup_task(struct iscsi_task *task)
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300280{
Mike Christie2261ec32008-05-21 15:54:11 -0500281 struct iscsi_iser_task *iser_task = task->dd_data;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300282
Mike Christieb3cd5052009-05-13 17:57:49 -0500283 /* mgmt tasks do not need special cleanup */
284 if (!task->sc)
Mike Christie2747fdb2008-05-21 15:54:08 -0500285 return;
286
Mike Christie2261ec32008-05-21 15:54:11 -0500287 if (iser_task->status == ISER_TASK_STATUS_STARTED) {
288 iser_task->status = ISER_TASK_STATUS_COMPLETED;
289 iser_task_rdma_finalize(iser_task);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300290 }
291}
292
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300293static struct iscsi_cls_conn *
294iscsi_iser_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
295{
296 struct iscsi_conn *conn;
297 struct iscsi_cls_conn *cls_conn;
298 struct iscsi_iser_conn *iser_conn;
299
Mike Christie5d91e202008-05-21 15:54:01 -0500300 cls_conn = iscsi_conn_setup(cls_session, sizeof(*iser_conn), conn_idx);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300301 if (!cls_conn)
302 return NULL;
303 conn = cls_conn->dd_data;
304
305 /*
306 * due to issues with the login code re iser sematics
307 * this not set in iscsi_conn_setup - FIXME
308 */
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000309 conn->max_recv_dlength = ISER_RECV_DATA_SEG_LEN;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300310
Mike Christie5d91e202008-05-21 15:54:01 -0500311 iser_conn = conn->dd_data;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300312 conn->dd_data = iser_conn;
313 iser_conn->iscsi_conn = conn;
314
315 return cls_conn;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300316}
317
318static void
319iscsi_iser_conn_destroy(struct iscsi_cls_conn *cls_conn)
320{
321 struct iscsi_conn *conn = cls_conn->dd_data;
322 struct iscsi_iser_conn *iser_conn = conn->dd_data;
Mike Christieb40977d2008-05-21 15:54:03 -0500323 struct iser_conn *ib_conn = iser_conn->ib_conn;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300324
325 iscsi_conn_teardown(cls_conn);
Mike Christieb40977d2008-05-21 15:54:03 -0500326 /*
327 * Userspace will normally call the stop callback and
328 * already have freed the ib_conn, but if it goofed up then
329 * we free it here.
330 */
331 if (ib_conn) {
332 ib_conn->iser_conn = NULL;
Or Gerlitz39ff05d2010-05-05 17:31:44 +0300333 iser_conn_put(ib_conn, 1); /* deref iscsi/ib conn unbinding */
Mike Christieb40977d2008-05-21 15:54:03 -0500334 }
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300335}
336
337static int
338iscsi_iser_conn_bind(struct iscsi_cls_session *cls_session,
339 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
340 int is_leading)
341{
342 struct iscsi_conn *conn = cls_conn->dd_data;
343 struct iscsi_iser_conn *iser_conn;
344 struct iser_conn *ib_conn;
Mike Christie412eeaf2008-05-21 15:54:14 -0500345 struct iscsi_endpoint *ep;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300346 int error;
347
348 error = iscsi_conn_bind(cls_session, cls_conn, is_leading);
349 if (error)
350 return error;
351
352 /* the transport ep handle comes from user space so it must be
353 * verified against the global ib connections list */
Mike Christie412eeaf2008-05-21 15:54:14 -0500354 ep = iscsi_lookup_endpoint(transport_eph);
355 if (!ep) {
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300356 iser_err("can't bind eph %llx\n",
357 (unsigned long long)transport_eph);
358 return -EINVAL;
359 }
Mike Christie412eeaf2008-05-21 15:54:14 -0500360 ib_conn = ep->dd_data;
361
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300362 /* binds the iSER connection retrieved from the previously
363 * connected ep_handle to the iSCSI layer connection. exchanges
364 * connection pointers */
Or Gerlitz39ff05d2010-05-05 17:31:44 +0300365 iser_err("binding iscsi/iser conn %p %p to ib_conn %p\n",
366 conn, conn->dd_data, ib_conn);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300367 iser_conn = conn->dd_data;
368 ib_conn->iser_conn = iser_conn;
369 iser_conn->ib_conn = ib_conn;
Or Gerlitz39ff05d2010-05-05 17:31:44 +0300370 iser_conn_get(ib_conn); /* ref iscsi/ib conn binding */
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300371 return 0;
372}
373
Mike Christieb40977d2008-05-21 15:54:03 -0500374static void
375iscsi_iser_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
376{
377 struct iscsi_conn *conn = cls_conn->dd_data;
378 struct iscsi_iser_conn *iser_conn = conn->dd_data;
379 struct iser_conn *ib_conn = iser_conn->ib_conn;
380
Mike Christieb40977d2008-05-21 15:54:03 -0500381 /*
Mike Christie913e5bf2008-05-21 15:54:18 -0500382 * Userspace may have goofed up and not bound the connection or
383 * might have only partially setup the connection.
Mike Christieb40977d2008-05-21 15:54:03 -0500384 */
Mike Christie913e5bf2008-05-21 15:54:18 -0500385 if (ib_conn) {
386 iscsi_conn_stop(cls_conn, flag);
387 /*
388 * There is no unbind event so the stop callback
389 * must release the ref from the bind.
390 */
Or Gerlitz39ff05d2010-05-05 17:31:44 +0300391 iser_conn_put(ib_conn, 1); /* deref iscsi/ib conn unbinding */
Mike Christie913e5bf2008-05-21 15:54:18 -0500392 }
Mike Christieb40977d2008-05-21 15:54:03 -0500393 iser_conn->ib_conn = NULL;
394}
395
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300396static int
397iscsi_iser_conn_start(struct iscsi_cls_conn *cls_conn)
398{
399 struct iscsi_conn *conn = cls_conn->dd_data;
400 int err;
401
Erez Zilber2e7a7422006-10-22 10:28:38 +0200402 err = iser_conn_set_full_featured_mode(conn);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300403 if (err)
404 return err;
405
Erez Zilber2e7a7422006-10-22 10:28:38 +0200406 return iscsi_conn_start(cls_conn);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300407}
408
Mike Christie75613522008-05-21 15:53:59 -0500409static void iscsi_iser_session_destroy(struct iscsi_cls_session *cls_session)
410{
411 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
412
Mike Christiee5bd7b52008-09-24 11:46:10 -0500413 iscsi_session_teardown(cls_session);
Mike Christiea4804cd2008-05-21 15:54:00 -0500414 iscsi_host_remove(shost);
415 iscsi_host_free(shost);
Mike Christie75613522008-05-21 15:53:59 -0500416}
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300417
418static struct iscsi_cls_session *
Mike Christie412eeaf2008-05-21 15:54:14 -0500419iscsi_iser_session_create(struct iscsi_endpoint *ep,
Mike Christie75613522008-05-21 15:53:59 -0500420 uint16_t cmds_max, uint16_t qdepth,
Mike Christie5e7facb2009-03-05 14:46:06 -0600421 uint32_t initial_cmdsn)
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300422{
423 struct iscsi_cls_session *cls_session;
424 struct iscsi_session *session;
Mike Christie412eeaf2008-05-21 15:54:14 -0500425 struct Scsi_Host *shost;
Mike Christie412eeaf2008-05-21 15:54:14 -0500426 struct iser_conn *ib_conn;
Mike Christie75613522008-05-21 15:53:59 -0500427
Or Gerlitz962b4b52010-02-08 13:22:34 +0000428 shost = iscsi_host_alloc(&iscsi_iser_sht, 0, 0);
Mike Christie75613522008-05-21 15:53:59 -0500429 if (!shost)
430 return NULL;
431 shost->transportt = iscsi_iser_scsi_transport;
432 shost->max_lun = iscsi_max_lun;
433 shost->max_id = 0;
434 shost->max_channel = 0;
435 shost->max_cmd_len = 16;
436
Mike Christie412eeaf2008-05-21 15:54:14 -0500437 /*
438 * older userspace tools (before 2.0-870) did not pass us
439 * the leading conn's ep so this will be NULL;
440 */
441 if (ep)
442 ib_conn = ep->dd_data;
443
444 if (iscsi_host_add(shost,
445 ep ? ib_conn->device->ib_device->dma_device : NULL))
Mike Christie75613522008-05-21 15:53:59 -0500446 goto free_host;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300447
Mike Christie15482712007-05-30 12:57:19 -0500448 /*
449 * we do not support setting can_queue cmd_per_lun from userspace yet
450 * because we preallocate so many resources
451 */
Mike Christie75613522008-05-21 15:53:59 -0500452 cls_session = iscsi_session_setup(&iscsi_iser_transport, shost,
Jayamohan Kallickalb8b9e1b82009-09-22 08:21:22 +0530453 ISCSI_DEF_XMIT_CMDS_MAX, 0,
Mike Christie2261ec32008-05-21 15:54:11 -0500454 sizeof(struct iscsi_iser_task),
Mike Christie79706342008-05-21 15:54:12 -0500455 initial_cmdsn, 0);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300456 if (!cls_session)
Mike Christie75613522008-05-21 15:53:59 -0500457 goto remove_host;
458 session = cls_session->dd_data;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300459
Mike Christie2747fdb2008-05-21 15:54:08 -0500460 shost->can_queue = session->scsi_cmds_max;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300461 return cls_session;
Mike Christie75613522008-05-21 15:53:59 -0500462
463remove_host:
Mike Christiea4804cd2008-05-21 15:54:00 -0500464 iscsi_host_remove(shost);
Mike Christie75613522008-05-21 15:53:59 -0500465free_host:
Mike Christiea4804cd2008-05-21 15:54:00 -0500466 iscsi_host_free(shost);
Mike Christie75613522008-05-21 15:53:59 -0500467 return NULL;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300468}
469
470static int
Mike Christie358ff012006-06-28 12:00:25 -0500471iscsi_iser_set_param(struct iscsi_cls_conn *cls_conn,
472 enum iscsi_param param, char *buf, int buflen)
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300473{
Mike Christie358ff012006-06-28 12:00:25 -0500474 int value;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300475
476 switch (param) {
477 case ISCSI_PARAM_MAX_RECV_DLENGTH:
478 /* TBD */
479 break;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300480 case ISCSI_PARAM_HDRDGST_EN:
Mike Christie358ff012006-06-28 12:00:25 -0500481 sscanf(buf, "%d", &value);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300482 if (value) {
483 printk(KERN_ERR "DataDigest wasn't negotiated to None");
484 return -EPROTO;
485 }
486 break;
487 case ISCSI_PARAM_DATADGST_EN:
Mike Christie358ff012006-06-28 12:00:25 -0500488 sscanf(buf, "%d", &value);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300489 if (value) {
490 printk(KERN_ERR "DataDigest wasn't negotiated to None");
491 return -EPROTO;
492 }
493 break;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300494 case ISCSI_PARAM_IFMARKER_EN:
Mike Christie358ff012006-06-28 12:00:25 -0500495 sscanf(buf, "%d", &value);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300496 if (value) {
497 printk(KERN_ERR "IFMarker wasn't negotiated to No");
498 return -EPROTO;
499 }
500 break;
501 case ISCSI_PARAM_OFMARKER_EN:
Mike Christie358ff012006-06-28 12:00:25 -0500502 sscanf(buf, "%d", &value);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300503 if (value) {
504 printk(KERN_ERR "OFMarker wasn't negotiated to No");
505 return -EPROTO;
506 }
507 break;
508 default:
Mike Christie358ff012006-06-28 12:00:25 -0500509 return iscsi_set_param(cls_conn, param, buf, buflen);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300510 }
511
512 return 0;
513}
514
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300515static void
516iscsi_iser_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
517{
518 struct iscsi_conn *conn = cls_conn->dd_data;
519
520 stats->txdata_octets = conn->txdata_octets;
521 stats->rxdata_octets = conn->rxdata_octets;
522 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
523 stats->dataout_pdus = conn->dataout_pdus_cnt;
524 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
525 stats->datain_pdus = conn->datain_pdus_cnt; /* always 0 */
526 stats->r2t_pdus = conn->r2t_pdus_cnt; /* always 0 */
527 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
528 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
Eli Dorfman87528222008-04-29 13:46:52 -0700529 stats->custom_length = 4;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300530 strcpy(stats->custom[0].desc, "qp_tx_queue_full");
531 stats->custom[0].value = 0; /* TB iser_conn->qp_tx_queue_full; */
532 strcpy(stats->custom[1].desc, "fmr_map_not_avail");
533 stats->custom[1].value = 0; /* TB iser_conn->fmr_map_not_avail */;
534 strcpy(stats->custom[2].desc, "eh_abort_cnt");
535 stats->custom[2].value = conn->eh_abort_cnt;
Eli Dorfman87528222008-04-29 13:46:52 -0700536 strcpy(stats->custom[3].desc, "fmr_unalign_cnt");
537 stats->custom[3].value = conn->fmr_unalign_cnt;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300538}
539
Mike Christie7c53c6f2011-02-16 15:04:40 -0600540static int iscsi_iser_get_ep_param(struct iscsi_endpoint *ep,
541 enum iscsi_param param, char *buf)
542{
543 struct iser_conn *ib_conn = ep->dd_data;
544 int len;
545
546 switch (param) {
547 case ISCSI_PARAM_CONN_PORT:
548 case ISCSI_PARAM_CONN_ADDRESS:
549 if (!ib_conn || !ib_conn->cma_id)
550 return -ENOTCONN;
551
552 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
553 &ib_conn->cma_id->route.addr.dst_addr,
554 param, buf);
555 break;
556 default:
557 return -ENOSYS;
558 }
559
560 return len;
561}
562
Mike Christie412eeaf2008-05-21 15:54:14 -0500563static struct iscsi_endpoint *
Mike Christie10eb0f02009-05-13 17:57:38 -0500564iscsi_iser_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
565 int non_blocking)
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300566{
567 int err;
568 struct iser_conn *ib_conn;
Mike Christie412eeaf2008-05-21 15:54:14 -0500569 struct iscsi_endpoint *ep;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300570
Mike Christie412eeaf2008-05-21 15:54:14 -0500571 ep = iscsi_create_endpoint(sizeof(*ib_conn));
572 if (!ep)
573 return ERR_PTR(-ENOMEM);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300574
Mike Christie412eeaf2008-05-21 15:54:14 -0500575 ib_conn = ep->dd_data;
576 ib_conn->ep = ep;
577 iser_conn_init(ib_conn);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300578
Mike Christie412eeaf2008-05-21 15:54:14 -0500579 err = iser_connect(ib_conn, NULL, (struct sockaddr_in *)dst_addr,
580 non_blocking);
581 if (err) {
582 iscsi_destroy_endpoint(ep);
583 return ERR_PTR(err);
584 }
585 return ep;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300586}
587
588static int
Mike Christie412eeaf2008-05-21 15:54:14 -0500589iscsi_iser_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300590{
Mike Christie412eeaf2008-05-21 15:54:14 -0500591 struct iser_conn *ib_conn;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300592 int rc;
593
Mike Christie412eeaf2008-05-21 15:54:14 -0500594 ib_conn = ep->dd_data;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300595 rc = wait_event_interruptible_timeout(ib_conn->wait,
596 ib_conn->state == ISER_CONN_UP,
597 msecs_to_jiffies(timeout_ms));
598
599 /* if conn establishment failed, return error code to iscsi */
600 if (!rc &&
601 (ib_conn->state == ISER_CONN_TERMINATING ||
602 ib_conn->state == ISER_CONN_DOWN))
603 rc = -1;
604
605 iser_err("ib conn %p rc = %d\n", ib_conn, rc);
606
607 if (rc > 0)
608 return 1; /* success, this is the equivalent of POLLOUT */
609 else if (!rc)
610 return 0; /* timeout */
611 else
612 return rc; /* signal */
613}
614
615static void
Mike Christie412eeaf2008-05-21 15:54:14 -0500616iscsi_iser_ep_disconnect(struct iscsi_endpoint *ep)
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300617{
Mike Christie1c834692006-07-24 15:47:26 -0500618 struct iser_conn *ib_conn;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300619
Mike Christie412eeaf2008-05-21 15:54:14 -0500620 ib_conn = ep->dd_data;
Mike Christieb40977d2008-05-21 15:54:03 -0500621 if (ib_conn->iser_conn)
622 /*
623 * Must suspend xmit path if the ep is bound to the
624 * iscsi_conn, so we know we are not accessing the ib_conn
625 * when we free it.
626 *
627 * This may not be bound if the ep poll failed.
628 */
629 iscsi_suspend_tx(ib_conn->iser_conn->iscsi_conn);
630
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300631
632 iser_err("ib conn %p state %d\n",ib_conn, ib_conn->state);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300633 iser_conn_terminate(ib_conn);
634}
635
Mike Christie3128c6c2011-07-25 13:48:42 -0500636static mode_t iser_attr_is_visible(int param_type, int param)
637{
638 switch (param_type) {
Mike Christief27fb2e2011-07-25 13:48:45 -0500639 case ISCSI_HOST_PARAM:
640 switch (param) {
641 case ISCSI_HOST_PARAM_NETDEV_NAME:
642 case ISCSI_HOST_PARAM_HWADDRESS:
643 case ISCSI_HOST_PARAM_INITIATOR_NAME:
644 return S_IRUGO;
645 default:
646 return 0;
647 }
Mike Christie3128c6c2011-07-25 13:48:42 -0500648 case ISCSI_PARAM:
649 switch (param) {
650 case ISCSI_PARAM_MAX_RECV_DLENGTH:
651 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
652 case ISCSI_PARAM_HDRDGST_EN:
653 case ISCSI_PARAM_DATADGST_EN:
654 case ISCSI_PARAM_CONN_ADDRESS:
655 case ISCSI_PARAM_CONN_PORT:
656 case ISCSI_PARAM_EXP_STATSN:
657 case ISCSI_PARAM_PERSISTENT_ADDRESS:
658 case ISCSI_PARAM_PERSISTENT_PORT:
659 case ISCSI_PARAM_PING_TMO:
660 case ISCSI_PARAM_RECV_TMO:
Mike Christie1d063c12011-07-25 13:48:43 -0500661 case ISCSI_PARAM_INITIAL_R2T_EN:
662 case ISCSI_PARAM_MAX_R2T:
663 case ISCSI_PARAM_IMM_DATA_EN:
664 case ISCSI_PARAM_FIRST_BURST:
665 case ISCSI_PARAM_MAX_BURST:
666 case ISCSI_PARAM_PDU_INORDER_EN:
667 case ISCSI_PARAM_DATASEQ_INORDER_EN:
668 case ISCSI_PARAM_TARGET_NAME:
669 case ISCSI_PARAM_TPGT:
670 case ISCSI_PARAM_USERNAME:
671 case ISCSI_PARAM_PASSWORD:
672 case ISCSI_PARAM_USERNAME_IN:
673 case ISCSI_PARAM_PASSWORD_IN:
674 case ISCSI_PARAM_FAST_ABORT:
675 case ISCSI_PARAM_ABORT_TMO:
676 case ISCSI_PARAM_LU_RESET_TMO:
677 case ISCSI_PARAM_TGT_RESET_TMO:
678 case ISCSI_PARAM_IFACE_NAME:
679 case ISCSI_PARAM_INITIATOR_NAME:
Mike Christie3128c6c2011-07-25 13:48:42 -0500680 return S_IRUGO;
681 default:
682 return 0;
683 }
684 }
685
686 return 0;
687}
688
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300689static struct scsi_host_template iscsi_iser_sht = {
Mike Christie79743922007-07-26 12:46:46 -0500690 .module = THIS_MODULE,
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300691 .name = "iSCSI Initiator over iSER, v." DRV_VER,
692 .queuecommand = iscsi_queuecommand,
Erez Zilber64106272008-01-17 11:53:17 +0200693 .change_queue_depth = iscsi_change_queue_depth,
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300694 .sg_tablesize = ISCSI_ISER_SG_TABLESIZE,
Erez Zilber8072ec22006-09-11 12:20:54 +0300695 .max_sectors = 1024,
Mike Christiee28f3d52009-03-05 14:46:01 -0600696 .cmd_per_lun = ISER_DEF_CMD_PER_LUN,
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300697 .eh_abort_handler = iscsi_eh_abort,
Erez Zilber90c18f32008-01-22 12:06:25 +0200698 .eh_device_reset_handler= iscsi_eh_device_reset,
Jayamohan Kallickal309ce152010-02-20 08:02:10 +0530699 .eh_target_reset_handler = iscsi_eh_recover_target,
Mike Christie6b5d6c42009-04-21 15:32:32 -0500700 .target_alloc = iscsi_target_alloc,
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300701 .use_clustering = DISABLE_CLUSTERING,
702 .proc_name = "iscsi_iser",
703 .this_id = -1,
704};
705
706static struct iscsi_transport iscsi_iser_transport = {
707 .owner = THIS_MODULE,
708 .name = "iser",
709 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T,
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300710 /* session management */
711 .create_session = iscsi_iser_session_create,
Mike Christie75613522008-05-21 15:53:59 -0500712 .destroy_session = iscsi_iser_session_destroy,
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300713 /* connection management */
714 .create_conn = iscsi_iser_conn_create,
715 .bind_conn = iscsi_iser_conn_bind,
716 .destroy_conn = iscsi_iser_conn_destroy,
Mike Christie3128c6c2011-07-25 13:48:42 -0500717 .attr_is_visible = iser_attr_is_visible,
Mike Christie358ff012006-06-28 12:00:25 -0500718 .set_param = iscsi_iser_set_param,
719 .get_conn_param = iscsi_conn_get_param,
Mike Christie7c53c6f2011-02-16 15:04:40 -0600720 .get_ep_param = iscsi_iser_get_ep_param,
Mike Christie358ff012006-06-28 12:00:25 -0500721 .get_session_param = iscsi_session_get_param,
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300722 .start_conn = iscsi_iser_conn_start,
Mike Christieb40977d2008-05-21 15:54:03 -0500723 .stop_conn = iscsi_iser_conn_stop,
Mike Christie0801c242007-05-30 12:57:12 -0500724 /* iscsi host params */
725 .get_host_param = iscsi_host_get_param,
726 .set_host_param = iscsi_host_set_param,
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300727 /* IO */
728 .send_pdu = iscsi_conn_send_pdu,
729 .get_stats = iscsi_iser_conn_get_stats,
Mike Christie2747fdb2008-05-21 15:54:08 -0500730 .init_task = iscsi_iser_task_init,
731 .xmit_task = iscsi_iser_task_xmit,
732 .cleanup_task = iscsi_iser_cleanup_task,
Mike Christie0f9c7442008-12-02 00:32:06 -0600733 .alloc_pdu = iscsi_iser_pdu_alloc,
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300734 /* recovery */
735 .session_recovery_timedout = iscsi_session_recovery_timedout,
736
737 .ep_connect = iscsi_iser_ep_connect,
738 .ep_poll = iscsi_iser_ep_poll,
739 .ep_disconnect = iscsi_iser_ep_disconnect
740};
741
742static int __init iser_init(void)
743{
744 int err;
745
746 iser_dbg("Starting iSER datamover...\n");
747
748 if (iscsi_max_lun < 1) {
749 printk(KERN_ERR "Invalid max_lun value of %u\n", iscsi_max_lun);
750 return -EINVAL;
751 }
752
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300753 memset(&ig, 0, sizeof(struct iser_global));
754
755 ig.desc_cache = kmem_cache_create("iser_descriptors",
Or Gerlitzf19624a2010-02-08 13:19:56 +0000756 sizeof(struct iser_tx_desc),
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300757 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +0900758 NULL);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300759 if (ig.desc_cache == NULL)
760 return -ENOMEM;
761
762 /* device init is called only after the first addr resolution */
763 mutex_init(&ig.device_list_mutex);
764 INIT_LIST_HEAD(&ig.device_list);
765 mutex_init(&ig.connlist_mutex);
766 INIT_LIST_HEAD(&ig.connlist);
767
Mike Christie75613522008-05-21 15:53:59 -0500768 iscsi_iser_scsi_transport = iscsi_register_transport(
769 &iscsi_iser_transport);
770 if (!iscsi_iser_scsi_transport) {
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300771 iser_err("iscsi_register_transport failed\n");
772 err = -EINVAL;
773 goto register_transport_failure;
774 }
775
776 return 0;
777
778register_transport_failure:
779 kmem_cache_destroy(ig.desc_cache);
780
781 return err;
782}
783
784static void __exit iser_exit(void)
785{
786 iser_dbg("Removing iSER datamover...\n");
787 iscsi_unregister_transport(&iscsi_iser_transport);
788 kmem_cache_destroy(ig.desc_cache);
789}
790
791module_init(iser_init);
792module_exit(iser_exit);