blob: 08edbaf892239dae733379f4a39701f0a1dda254 [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
45 *
46 *
47 * $Id: iscsi_iser.c 6965 2006-05-07 11:36:20Z ogerlitz $
48 */
49
50#include <linux/types.h>
51#include <linux/list.h>
52#include <linux/hardirq.h>
53#include <linux/kfifo.h>
54#include <linux/blkdev.h>
55#include <linux/init.h>
56#include <linux/ioctl.h>
Or Gerlitz65e7ae72006-05-11 10:00:44 +030057#include <linux/cdev.h>
58#include <linux/in.h>
59#include <linux/net.h>
60#include <linux/scatterlist.h>
61#include <linux/delay.h>
62
63#include <net/sock.h>
64
65#include <asm/uaccess.h>
66
67#include <scsi/scsi_cmnd.h>
68#include <scsi/scsi_device.h>
69#include <scsi/scsi_eh.h>
70#include <scsi/scsi_tcq.h>
71#include <scsi/scsi_host.h>
72#include <scsi/scsi.h>
73#include <scsi/scsi_transport_iscsi.h>
74
75#include "iscsi_iser.h"
76
Mike Christie75613522008-05-21 15:53:59 -050077static struct scsi_host_template iscsi_iser_sht;
78static struct iscsi_transport iscsi_iser_transport;
79static struct scsi_transport_template *iscsi_iser_scsi_transport;
80
Or Gerlitz65e7ae72006-05-11 10:00:44 +030081static unsigned int iscsi_max_lun = 512;
82module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
83
84int iser_debug_level = 0;
85
86MODULE_DESCRIPTION("iSER (iSCSI Extensions for RDMA) Datamover "
87 "v" DRV_VER " (" DRV_DATE ")");
88MODULE_LICENSE("Dual BSD/GPL");
89MODULE_AUTHOR("Alex Nezhinsky, Dan Bar Dov, Or Gerlitz");
90
91module_param_named(debug_level, iser_debug_level, int, 0644);
92MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0 (default:disabled)");
93
94struct iser_global ig;
95
96void
97iscsi_iser_recv(struct iscsi_conn *conn,
98 struct iscsi_hdr *hdr, char *rx_data, int rx_data_len)
99{
100 int rc = 0;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300101 int datalen;
102 int ahslen;
103
104 /* verify PDU length */
105 datalen = ntoh24(hdr->dlength);
106 if (datalen != rx_data_len) {
107 printk(KERN_ERR "iscsi_iser: datalen %d (hdr) != %d (IB) \n",
108 datalen, rx_data_len);
109 rc = ISCSI_ERR_DATALEN;
110 goto error;
111 }
112
113 /* read AHS */
114 ahslen = hdr->hlength * 4;
115
Mike Christie0af967f2008-05-21 15:54:04 -0500116 rc = iscsi_complete_pdu(conn, hdr, rx_data, rx_data_len);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300117 if (rc && rc != ISCSI_ERR_NO_SCSI_CMD)
118 goto error;
119
120 return;
121error:
122 iscsi_conn_failure(conn, rc);
123}
124
125
126/**
Mike Christie2261ec32008-05-21 15:54:11 -0500127 * iscsi_iser_task_init - Initialize task
128 * @task: iscsi task
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300129 *
Mike Christie2261ec32008-05-21 15:54:11 -0500130 * Initialize the task for the scsi command or mgmt command.
Mike Christie2747fdb2008-05-21 15:54:08 -0500131 */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600132static int
Mike Christie2261ec32008-05-21 15:54:11 -0500133iscsi_iser_task_init(struct iscsi_task *task)
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300134{
Mike Christie2261ec32008-05-21 15:54:11 -0500135 struct iscsi_iser_conn *iser_conn = task->conn->dd_data;
136 struct iscsi_iser_task *iser_task = task->dd_data;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300137
Mike Christie2261ec32008-05-21 15:54:11 -0500138 /* mgmt task */
139 if (!task->sc) {
140 iser_task->desc.data = task->data;
Mike Christie2747fdb2008-05-21 15:54:08 -0500141 return 0;
142 }
143
Mike Christie2261ec32008-05-21 15:54:11 -0500144 iser_task->command_sent = 0;
145 iser_task->iser_conn = iser_conn;
146 iser_task_rdma_init(iser_task);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600147 return 0;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300148}
149
150/**
Mike Christie2261ec32008-05-21 15:54:11 -0500151 * iscsi_iser_mtask_xmit - xmit management(immediate) task
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300152 * @conn: iscsi connection
Mike Christie2261ec32008-05-21 15:54:11 -0500153 * @task: task management task
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300154 *
155 * Notes:
156 * The function can return -EAGAIN in which case caller must
157 * call it again later, or recover. '0' return code means successful
158 * xmit.
159 *
160 **/
161static int
Mike Christie2261ec32008-05-21 15:54:11 -0500162iscsi_iser_mtask_xmit(struct iscsi_conn *conn, struct iscsi_task *task)
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300163{
164 int error = 0;
165
Mike Christie2261ec32008-05-21 15:54:11 -0500166 debug_scsi("task deq [cid %d itt 0x%x]\n", conn->id, task->itt);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300167
Mike Christie2261ec32008-05-21 15:54:11 -0500168 error = iser_send_control(conn, task);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300169
Mike Christie2261ec32008-05-21 15:54:11 -0500170 /* since iser xmits control with zero copy, tasks can not be recycled
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300171 * right after sending them.
172 * The recycling scheme is based on whether a response is expected
Mike Christie2261ec32008-05-21 15:54:11 -0500173 * - if yes, the task is recycled at iscsi_complete_pdu
174 * - if no, the task is recycled at iser_snd_completion
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300175 */
Erez Zilberf0938402007-01-07 12:28:02 +0200176 if (error && error != -ENOBUFS)
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300177 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
178
179 return error;
180}
181
182static int
Mike Christie2747fdb2008-05-21 15:54:08 -0500183iscsi_iser_task_xmit_unsol_data(struct iscsi_conn *conn,
Mike Christie2261ec32008-05-21 15:54:11 -0500184 struct iscsi_task *task)
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300185{
186 struct iscsi_data hdr;
187 int error = 0;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300188
189 /* Send data-out PDUs while there's still unsolicited data to send */
Mike Christie2261ec32008-05-21 15:54:11 -0500190 while (task->unsol_count > 0) {
191 iscsi_prep_unsolicit_data_pdu(task, &hdr);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300192 debug_scsi("Sending data-out: itt 0x%x, data count %d\n",
Mike Christie2261ec32008-05-21 15:54:11 -0500193 hdr.itt, task->data_count);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300194
195 /* the buffer description has been passed with the command */
196 /* Send the command */
Mike Christie2261ec32008-05-21 15:54:11 -0500197 error = iser_send_data_out(conn, task, &hdr);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300198 if (error) {
Mike Christie2261ec32008-05-21 15:54:11 -0500199 task->unsol_datasn--;
Mike Christie2747fdb2008-05-21 15:54:08 -0500200 goto iscsi_iser_task_xmit_unsol_data_exit;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300201 }
Mike Christie2261ec32008-05-21 15:54:11 -0500202 task->unsol_count -= task->data_count;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300203 debug_scsi("Need to send %d more as data-out PDUs\n",
Mike Christie2261ec32008-05-21 15:54:11 -0500204 task->unsol_count);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300205 }
206
Mike Christie2747fdb2008-05-21 15:54:08 -0500207iscsi_iser_task_xmit_unsol_data_exit:
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300208 return error;
209}
210
211static int
Mike Christie2261ec32008-05-21 15:54:11 -0500212iscsi_iser_task_xmit(struct iscsi_task *task)
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300213{
Mike Christie2261ec32008-05-21 15:54:11 -0500214 struct iscsi_conn *conn = task->conn;
215 struct iscsi_iser_task *iser_task = task->dd_data;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300216 int error = 0;
217
Mike Christie2261ec32008-05-21 15:54:11 -0500218 if (!task->sc)
219 return iscsi_iser_mtask_xmit(conn, task);
Mike Christie2747fdb2008-05-21 15:54:08 -0500220
Mike Christie2261ec32008-05-21 15:54:11 -0500221 if (task->sc->sc_data_direction == DMA_TO_DEVICE) {
222 BUG_ON(scsi_bufflen(task->sc) == 0);
Mike Christie77a23c22007-05-30 12:57:18 -0500223
224 debug_scsi("cmd [itt %x total %d imm %d unsol_data %d\n",
Mike Christie2261ec32008-05-21 15:54:11 -0500225 task->itt, scsi_bufflen(task->sc),
226 task->imm_count, task->unsol_count);
Mike Christie77a23c22007-05-30 12:57:18 -0500227 }
228
Mike Christie2261ec32008-05-21 15:54:11 -0500229 debug_scsi("task deq [cid %d itt 0x%x]\n",
230 conn->id, task->itt);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300231
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300232 /* Send the cmd PDU */
Mike Christie2261ec32008-05-21 15:54:11 -0500233 if (!iser_task->command_sent) {
234 error = iser_send_command(conn, task);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300235 if (error)
Mike Christie2747fdb2008-05-21 15:54:08 -0500236 goto iscsi_iser_task_xmit_exit;
Mike Christie2261ec32008-05-21 15:54:11 -0500237 iser_task->command_sent = 1;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300238 }
239
240 /* Send unsolicited data-out PDU(s) if necessary */
Mike Christie2261ec32008-05-21 15:54:11 -0500241 if (task->unsol_count)
242 error = iscsi_iser_task_xmit_unsol_data(conn, task);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300243
Mike Christie2747fdb2008-05-21 15:54:08 -0500244 iscsi_iser_task_xmit_exit:
Erez Zilberf0938402007-01-07 12:28:02 +0200245 if (error && error != -ENOBUFS)
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300246 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
247 return error;
248}
249
250static void
Mike Christie2261ec32008-05-21 15:54:11 -0500251iscsi_iser_cleanup_task(struct iscsi_conn *conn, struct iscsi_task *task)
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300252{
Mike Christie2261ec32008-05-21 15:54:11 -0500253 struct iscsi_iser_task *iser_task = task->dd_data;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300254
Mike Christie2747fdb2008-05-21 15:54:08 -0500255 /* mgmt tasks do not need special cleanup */
Mike Christie2261ec32008-05-21 15:54:11 -0500256 if (!task->sc)
Mike Christie2747fdb2008-05-21 15:54:08 -0500257 return;
258
Mike Christie2261ec32008-05-21 15:54:11 -0500259 if (iser_task->status == ISER_TASK_STATUS_STARTED) {
260 iser_task->status = ISER_TASK_STATUS_COMPLETED;
261 iser_task_rdma_finalize(iser_task);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300262 }
263}
264
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300265static struct iscsi_cls_conn *
266iscsi_iser_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
267{
268 struct iscsi_conn *conn;
269 struct iscsi_cls_conn *cls_conn;
270 struct iscsi_iser_conn *iser_conn;
271
Mike Christie5d91e202008-05-21 15:54:01 -0500272 cls_conn = iscsi_conn_setup(cls_session, sizeof(*iser_conn), conn_idx);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300273 if (!cls_conn)
274 return NULL;
275 conn = cls_conn->dd_data;
276
277 /*
278 * due to issues with the login code re iser sematics
279 * this not set in iscsi_conn_setup - FIXME
280 */
281 conn->max_recv_dlength = 128;
282
Mike Christie5d91e202008-05-21 15:54:01 -0500283 iser_conn = conn->dd_data;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300284 /* currently this is the only field which need to be initiated */
285 rwlock_init(&iser_conn->lock);
286
287 conn->dd_data = iser_conn;
288 iser_conn->iscsi_conn = conn;
289
290 return cls_conn;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300291}
292
293static void
294iscsi_iser_conn_destroy(struct iscsi_cls_conn *cls_conn)
295{
296 struct iscsi_conn *conn = cls_conn->dd_data;
297 struct iscsi_iser_conn *iser_conn = conn->dd_data;
Mike Christieb40977d2008-05-21 15:54:03 -0500298 struct iser_conn *ib_conn = iser_conn->ib_conn;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300299
Mike Christie5d91e202008-05-21 15:54:01 -0500300 iscsi_conn_teardown(cls_conn);
Mike Christieb40977d2008-05-21 15:54:03 -0500301 /*
302 * Userspace will normally call the stop callback and
303 * already have freed the ib_conn, but if it goofed up then
304 * we free it here.
305 */
306 if (ib_conn) {
307 ib_conn->iser_conn = NULL;
308 iser_conn_put(ib_conn);
309 }
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300310}
311
312static int
313iscsi_iser_conn_bind(struct iscsi_cls_session *cls_session,
314 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
315 int is_leading)
316{
317 struct iscsi_conn *conn = cls_conn->dd_data;
318 struct iscsi_iser_conn *iser_conn;
319 struct iser_conn *ib_conn;
Mike Christie412eeaf2008-05-21 15:54:14 -0500320 struct iscsi_endpoint *ep;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300321 int error;
322
323 error = iscsi_conn_bind(cls_session, cls_conn, is_leading);
324 if (error)
325 return error;
326
327 /* the transport ep handle comes from user space so it must be
328 * verified against the global ib connections list */
Mike Christie412eeaf2008-05-21 15:54:14 -0500329 ep = iscsi_lookup_endpoint(transport_eph);
330 if (!ep) {
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300331 iser_err("can't bind eph %llx\n",
332 (unsigned long long)transport_eph);
333 return -EINVAL;
334 }
Mike Christie412eeaf2008-05-21 15:54:14 -0500335 ib_conn = ep->dd_data;
336
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300337 /* binds the iSER connection retrieved from the previously
338 * connected ep_handle to the iSCSI layer connection. exchanges
339 * connection pointers */
340 iser_err("binding iscsi conn %p to iser_conn %p\n",conn,ib_conn);
341 iser_conn = conn->dd_data;
342 ib_conn->iser_conn = iser_conn;
343 iser_conn->ib_conn = ib_conn;
Mike Christieb40977d2008-05-21 15:54:03 -0500344 iser_conn_get(ib_conn);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300345
346 conn->recv_lock = &iser_conn->lock;
347
348 return 0;
349}
350
Mike Christieb40977d2008-05-21 15:54:03 -0500351static void
352iscsi_iser_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
353{
354 struct iscsi_conn *conn = cls_conn->dd_data;
355 struct iscsi_iser_conn *iser_conn = conn->dd_data;
356 struct iser_conn *ib_conn = iser_conn->ib_conn;
357
358 iscsi_conn_stop(cls_conn, flag);
359 /*
360 * There is no unbind event so the stop callback
361 * must release the ref from the bind.
362 */
363 iser_conn_put(ib_conn);
364 iser_conn->ib_conn = NULL;
365}
366
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300367static int
368iscsi_iser_conn_start(struct iscsi_cls_conn *cls_conn)
369{
370 struct iscsi_conn *conn = cls_conn->dd_data;
371 int err;
372
Erez Zilber2e7a7422006-10-22 10:28:38 +0200373 err = iser_conn_set_full_featured_mode(conn);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300374 if (err)
375 return err;
376
Erez Zilber2e7a7422006-10-22 10:28:38 +0200377 return iscsi_conn_start(cls_conn);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300378}
379
Mike Christie75613522008-05-21 15:53:59 -0500380static void iscsi_iser_session_destroy(struct iscsi_cls_session *cls_session)
381{
382 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
383
Mike Christiea4804cd2008-05-21 15:54:00 -0500384 iscsi_host_remove(shost);
385 iscsi_host_free(shost);
Mike Christie75613522008-05-21 15:53:59 -0500386}
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300387
388static struct iscsi_cls_session *
Mike Christie412eeaf2008-05-21 15:54:14 -0500389iscsi_iser_session_create(struct iscsi_endpoint *ep,
Mike Christie75613522008-05-21 15:53:59 -0500390 uint16_t cmds_max, uint16_t qdepth,
391 uint32_t initial_cmdsn, uint32_t *hostno)
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300392{
393 struct iscsi_cls_session *cls_session;
394 struct iscsi_session *session;
Mike Christie412eeaf2008-05-21 15:54:14 -0500395 struct Scsi_Host *shost;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300396 int i;
Mike Christie2261ec32008-05-21 15:54:11 -0500397 struct iscsi_task *task;
398 struct iscsi_iser_task *iser_task;
Mike Christie412eeaf2008-05-21 15:54:14 -0500399 struct iser_conn *ib_conn;
Mike Christie75613522008-05-21 15:53:59 -0500400
Mike Christiea4804cd2008-05-21 15:54:00 -0500401 shost = iscsi_host_alloc(&iscsi_iser_sht, 0, ISCSI_MAX_CMD_PER_LUN);
Mike Christie75613522008-05-21 15:53:59 -0500402 if (!shost)
403 return NULL;
404 shost->transportt = iscsi_iser_scsi_transport;
405 shost->max_lun = iscsi_max_lun;
406 shost->max_id = 0;
407 shost->max_channel = 0;
408 shost->max_cmd_len = 16;
409
Mike Christie412eeaf2008-05-21 15:54:14 -0500410 /*
411 * older userspace tools (before 2.0-870) did not pass us
412 * the leading conn's ep so this will be NULL;
413 */
414 if (ep)
415 ib_conn = ep->dd_data;
416
417 if (iscsi_host_add(shost,
418 ep ? ib_conn->device->ib_device->dma_device : NULL))
Mike Christie75613522008-05-21 15:53:59 -0500419 goto free_host;
420 *hostno = shost->host_no;
421
Mike Christie15482712007-05-30 12:57:19 -0500422 /*
423 * we do not support setting can_queue cmd_per_lun from userspace yet
424 * because we preallocate so many resources
425 */
Mike Christie75613522008-05-21 15:53:59 -0500426 cls_session = iscsi_session_setup(&iscsi_iser_transport, shost,
Mike Christie15482712007-05-30 12:57:19 -0500427 ISCSI_DEF_XMIT_CMDS_MAX,
Mike Christie2261ec32008-05-21 15:54:11 -0500428 sizeof(struct iscsi_iser_task),
Mike Christie79706342008-05-21 15:54:12 -0500429 initial_cmdsn, 0);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300430 if (!cls_session)
Mike Christie75613522008-05-21 15:53:59 -0500431 goto remove_host;
432 session = cls_session->dd_data;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300433
Mike Christie2747fdb2008-05-21 15:54:08 -0500434 shost->can_queue = session->scsi_cmds_max;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300435 /* libiscsi setup itts, data and pool so just set desc fields */
436 for (i = 0; i < session->cmds_max; i++) {
Mike Christie2261ec32008-05-21 15:54:11 -0500437 task = session->cmds[i];
438 iser_task = task->dd_data;
439 task->hdr = (struct iscsi_cmd *)&iser_task->desc.iscsi_header;
440 task->hdr_max = sizeof(iser_task->desc.iscsi_header);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300441 }
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300442 return cls_session;
Mike Christie75613522008-05-21 15:53:59 -0500443
444remove_host:
Mike Christiea4804cd2008-05-21 15:54:00 -0500445 iscsi_host_remove(shost);
Mike Christie75613522008-05-21 15:53:59 -0500446free_host:
Mike Christiea4804cd2008-05-21 15:54:00 -0500447 iscsi_host_free(shost);
Mike Christie75613522008-05-21 15:53:59 -0500448 return NULL;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300449}
450
451static int
Mike Christie358ff012006-06-28 12:00:25 -0500452iscsi_iser_set_param(struct iscsi_cls_conn *cls_conn,
453 enum iscsi_param param, char *buf, int buflen)
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300454{
Mike Christie358ff012006-06-28 12:00:25 -0500455 int value;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300456
457 switch (param) {
458 case ISCSI_PARAM_MAX_RECV_DLENGTH:
459 /* TBD */
460 break;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300461 case ISCSI_PARAM_HDRDGST_EN:
Mike Christie358ff012006-06-28 12:00:25 -0500462 sscanf(buf, "%d", &value);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300463 if (value) {
464 printk(KERN_ERR "DataDigest wasn't negotiated to None");
465 return -EPROTO;
466 }
467 break;
468 case ISCSI_PARAM_DATADGST_EN:
Mike Christie358ff012006-06-28 12:00:25 -0500469 sscanf(buf, "%d", &value);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300470 if (value) {
471 printk(KERN_ERR "DataDigest wasn't negotiated to None");
472 return -EPROTO;
473 }
474 break;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300475 case ISCSI_PARAM_IFMARKER_EN:
Mike Christie358ff012006-06-28 12:00:25 -0500476 sscanf(buf, "%d", &value);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300477 if (value) {
478 printk(KERN_ERR "IFMarker wasn't negotiated to No");
479 return -EPROTO;
480 }
481 break;
482 case ISCSI_PARAM_OFMARKER_EN:
Mike Christie358ff012006-06-28 12:00:25 -0500483 sscanf(buf, "%d", &value);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300484 if (value) {
485 printk(KERN_ERR "OFMarker wasn't negotiated to No");
486 return -EPROTO;
487 }
488 break;
489 default:
Mike Christie358ff012006-06-28 12:00:25 -0500490 return iscsi_set_param(cls_conn, param, buf, buflen);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300491 }
492
493 return 0;
494}
495
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300496static void
497iscsi_iser_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
498{
499 struct iscsi_conn *conn = cls_conn->dd_data;
500
501 stats->txdata_octets = conn->txdata_octets;
502 stats->rxdata_octets = conn->rxdata_octets;
503 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
504 stats->dataout_pdus = conn->dataout_pdus_cnt;
505 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
506 stats->datain_pdus = conn->datain_pdus_cnt; /* always 0 */
507 stats->r2t_pdus = conn->r2t_pdus_cnt; /* always 0 */
508 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
509 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
Eli Dorfman87528222008-04-29 13:46:52 -0700510 stats->custom_length = 4;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300511 strcpy(stats->custom[0].desc, "qp_tx_queue_full");
512 stats->custom[0].value = 0; /* TB iser_conn->qp_tx_queue_full; */
513 strcpy(stats->custom[1].desc, "fmr_map_not_avail");
514 stats->custom[1].value = 0; /* TB iser_conn->fmr_map_not_avail */;
515 strcpy(stats->custom[2].desc, "eh_abort_cnt");
516 stats->custom[2].value = conn->eh_abort_cnt;
Eli Dorfman87528222008-04-29 13:46:52 -0700517 strcpy(stats->custom[3].desc, "fmr_unalign_cnt");
518 stats->custom[3].value = conn->fmr_unalign_cnt;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300519}
520
Mike Christie412eeaf2008-05-21 15:54:14 -0500521static struct iscsi_endpoint *
522iscsi_iser_ep_connect(struct sockaddr *dst_addr, int non_blocking)
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300523{
524 int err;
525 struct iser_conn *ib_conn;
Mike Christie412eeaf2008-05-21 15:54:14 -0500526 struct iscsi_endpoint *ep;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300527
Mike Christie412eeaf2008-05-21 15:54:14 -0500528 ep = iscsi_create_endpoint(sizeof(*ib_conn));
529 if (!ep)
530 return ERR_PTR(-ENOMEM);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300531
Mike Christie412eeaf2008-05-21 15:54:14 -0500532 ib_conn = ep->dd_data;
533 ib_conn->ep = ep;
534 iser_conn_init(ib_conn);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300535
Mike Christie412eeaf2008-05-21 15:54:14 -0500536 err = iser_connect(ib_conn, NULL, (struct sockaddr_in *)dst_addr,
537 non_blocking);
538 if (err) {
539 iscsi_destroy_endpoint(ep);
540 return ERR_PTR(err);
541 }
542 return ep;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300543}
544
545static int
Mike Christie412eeaf2008-05-21 15:54:14 -0500546iscsi_iser_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300547{
Mike Christie412eeaf2008-05-21 15:54:14 -0500548 struct iser_conn *ib_conn;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300549 int rc;
550
Mike Christie412eeaf2008-05-21 15:54:14 -0500551 ib_conn = ep->dd_data;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300552 rc = wait_event_interruptible_timeout(ib_conn->wait,
553 ib_conn->state == ISER_CONN_UP,
554 msecs_to_jiffies(timeout_ms));
555
556 /* if conn establishment failed, return error code to iscsi */
557 if (!rc &&
558 (ib_conn->state == ISER_CONN_TERMINATING ||
559 ib_conn->state == ISER_CONN_DOWN))
560 rc = -1;
561
562 iser_err("ib conn %p rc = %d\n", ib_conn, rc);
563
564 if (rc > 0)
565 return 1; /* success, this is the equivalent of POLLOUT */
566 else if (!rc)
567 return 0; /* timeout */
568 else
569 return rc; /* signal */
570}
571
572static void
Mike Christie412eeaf2008-05-21 15:54:14 -0500573iscsi_iser_ep_disconnect(struct iscsi_endpoint *ep)
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300574{
Mike Christie1c834692006-07-24 15:47:26 -0500575 struct iser_conn *ib_conn;
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300576
Mike Christie412eeaf2008-05-21 15:54:14 -0500577 ib_conn = ep->dd_data;
Mike Christieb40977d2008-05-21 15:54:03 -0500578 if (ib_conn->iser_conn)
579 /*
580 * Must suspend xmit path if the ep is bound to the
581 * iscsi_conn, so we know we are not accessing the ib_conn
582 * when we free it.
583 *
584 * This may not be bound if the ep poll failed.
585 */
586 iscsi_suspend_tx(ib_conn->iser_conn->iscsi_conn);
587
588
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300589 iser_err("ib conn %p state %d\n",ib_conn, ib_conn->state);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300590 iser_conn_terminate(ib_conn);
591}
592
593static struct scsi_host_template iscsi_iser_sht = {
Mike Christie79743922007-07-26 12:46:46 -0500594 .module = THIS_MODULE,
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300595 .name = "iSCSI Initiator over iSER, v." DRV_VER,
596 .queuecommand = iscsi_queuecommand,
Erez Zilber64106272008-01-17 11:53:17 +0200597 .change_queue_depth = iscsi_change_queue_depth,
Mike Christie15482712007-05-30 12:57:19 -0500598 .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300599 .sg_tablesize = ISCSI_ISER_SG_TABLESIZE,
Erez Zilber8072ec22006-09-11 12:20:54 +0300600 .max_sectors = 1024,
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300601 .cmd_per_lun = ISCSI_MAX_CMD_PER_LUN,
602 .eh_abort_handler = iscsi_eh_abort,
Erez Zilber90c18f32008-01-22 12:06:25 +0200603 .eh_device_reset_handler= iscsi_eh_device_reset,
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300604 .eh_host_reset_handler = iscsi_eh_host_reset,
605 .use_clustering = DISABLE_CLUSTERING,
606 .proc_name = "iscsi_iser",
607 .this_id = -1,
608};
609
610static struct iscsi_transport iscsi_iser_transport = {
611 .owner = THIS_MODULE,
612 .name = "iser",
613 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T,
614 .param_mask = ISCSI_MAX_RECV_DLENGTH |
615 ISCSI_MAX_XMIT_DLENGTH |
616 ISCSI_HDRDGST_EN |
617 ISCSI_DATADGST_EN |
618 ISCSI_INITIAL_R2T_EN |
619 ISCSI_MAX_R2T |
620 ISCSI_IMM_DATA_EN |
621 ISCSI_FIRST_BURST |
622 ISCSI_MAX_BURST |
623 ISCSI_PDU_INORDER_EN |
Mike Christie358ff012006-06-28 12:00:25 -0500624 ISCSI_DATASEQ_INORDER_EN |
625 ISCSI_EXP_STATSN |
626 ISCSI_PERSISTENT_PORT |
627 ISCSI_PERSISTENT_ADDRESS |
Mike Christieb2c64162007-05-30 12:57:16 -0500628 ISCSI_TARGET_NAME | ISCSI_TPGT |
629 ISCSI_USERNAME | ISCSI_PASSWORD |
Mike Christief6d51802007-12-13 12:43:30 -0600630 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
631 ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
Mike Christie88dfd342008-05-21 15:54:16 -0500632 ISCSI_PING_TMO | ISCSI_RECV_TMO |
633 ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
Mike Christie8ad57812007-05-30 12:57:13 -0500634 .host_param_mask = ISCSI_HOST_HWADDRESS |
Mike Christied8196ed2007-05-30 12:57:25 -0500635 ISCSI_HOST_NETDEV_NAME |
Mike Christie8ad57812007-05-30 12:57:13 -0500636 ISCSI_HOST_INITIATOR_NAME,
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300637 /* session management */
638 .create_session = iscsi_iser_session_create,
Mike Christie75613522008-05-21 15:53:59 -0500639 .destroy_session = iscsi_iser_session_destroy,
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300640 /* connection management */
641 .create_conn = iscsi_iser_conn_create,
642 .bind_conn = iscsi_iser_conn_bind,
643 .destroy_conn = iscsi_iser_conn_destroy,
Mike Christie358ff012006-06-28 12:00:25 -0500644 .set_param = iscsi_iser_set_param,
645 .get_conn_param = iscsi_conn_get_param,
646 .get_session_param = iscsi_session_get_param,
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300647 .start_conn = iscsi_iser_conn_start,
Mike Christieb40977d2008-05-21 15:54:03 -0500648 .stop_conn = iscsi_iser_conn_stop,
Mike Christie0801c242007-05-30 12:57:12 -0500649 /* iscsi host params */
650 .get_host_param = iscsi_host_get_param,
651 .set_host_param = iscsi_host_set_param,
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300652 /* IO */
653 .send_pdu = iscsi_conn_send_pdu,
654 .get_stats = iscsi_iser_conn_get_stats,
Mike Christie2747fdb2008-05-21 15:54:08 -0500655 .init_task = iscsi_iser_task_init,
656 .xmit_task = iscsi_iser_task_xmit,
657 .cleanup_task = iscsi_iser_cleanup_task,
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300658 /* recovery */
659 .session_recovery_timedout = iscsi_session_recovery_timedout,
660
661 .ep_connect = iscsi_iser_ep_connect,
662 .ep_poll = iscsi_iser_ep_poll,
663 .ep_disconnect = iscsi_iser_ep_disconnect
664};
665
666static int __init iser_init(void)
667{
668 int err;
669
670 iser_dbg("Starting iSER datamover...\n");
671
672 if (iscsi_max_lun < 1) {
673 printk(KERN_ERR "Invalid max_lun value of %u\n", iscsi_max_lun);
674 return -EINVAL;
675 }
676
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300677 memset(&ig, 0, sizeof(struct iser_global));
678
679 ig.desc_cache = kmem_cache_create("iser_descriptors",
680 sizeof (struct iser_desc),
681 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +0900682 NULL);
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300683 if (ig.desc_cache == NULL)
684 return -ENOMEM;
685
686 /* device init is called only after the first addr resolution */
687 mutex_init(&ig.device_list_mutex);
688 INIT_LIST_HEAD(&ig.device_list);
689 mutex_init(&ig.connlist_mutex);
690 INIT_LIST_HEAD(&ig.connlist);
691
Mike Christie75613522008-05-21 15:53:59 -0500692 iscsi_iser_scsi_transport = iscsi_register_transport(
693 &iscsi_iser_transport);
694 if (!iscsi_iser_scsi_transport) {
Or Gerlitz65e7ae72006-05-11 10:00:44 +0300695 iser_err("iscsi_register_transport failed\n");
696 err = -EINVAL;
697 goto register_transport_failure;
698 }
699
700 return 0;
701
702register_transport_failure:
703 kmem_cache_destroy(ig.desc_cache);
704
705 return err;
706}
707
708static void __exit iser_exit(void)
709{
710 iser_dbg("Removing iSER datamover...\n");
711 iscsi_unregister_transport(&iscsi_iser_transport);
712 kmem_cache_destroy(ig.desc_cache);
713}
714
715module_init(iser_init);
716module_exit(iser_exit);