[SCSI] iscsi cls: sysfs group is_visible callout for conn attrs
[linux-2.6-block.git] / drivers / infiniband / ulp / iser / iscsi_iser.c
CommitLineData
65e7ae7b
OG
1/*
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
65e7ae7b
OG
45 */
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>
65e7ae7b
OG
54#include <linux/cdev.h>
55#include <linux/in.h>
56#include <linux/net.h>
57#include <linux/scatterlist.h>
58#include <linux/delay.h>
5a0e3ad6 59#include <linux/slab.h>
65e7ae7b
OG
60
61#include <net/sock.h>
62
63#include <asm/uaccess.h>
64
65#include <scsi/scsi_cmnd.h>
66#include <scsi/scsi_device.h>
67#include <scsi/scsi_eh.h>
68#include <scsi/scsi_tcq.h>
69#include <scsi/scsi_host.h>
70#include <scsi/scsi.h>
71#include <scsi/scsi_transport_iscsi.h>
72
73#include "iscsi_iser.h"
74
75613521
MC
75static struct scsi_host_template iscsi_iser_sht;
76static struct iscsi_transport iscsi_iser_transport;
77static struct scsi_transport_template *iscsi_iser_scsi_transport;
78
65e7ae7b
OG
79static unsigned int iscsi_max_lun = 512;
80module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
81
82int iser_debug_level = 0;
83
84MODULE_DESCRIPTION("iSER (iSCSI Extensions for RDMA) Datamover "
85 "v" DRV_VER " (" DRV_DATE ")");
86MODULE_LICENSE("Dual BSD/GPL");
87MODULE_AUTHOR("Alex Nezhinsky, Dan Bar Dov, Or Gerlitz");
88
89module_param_named(debug_level, iser_debug_level, int, 0644);
90MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0 (default:disabled)");
91
92struct iser_global ig;
93
94void
95iscsi_iser_recv(struct iscsi_conn *conn,
96 struct iscsi_hdr *hdr, char *rx_data, int rx_data_len)
97{
98 int rc = 0;
65e7ae7b
OG
99 int datalen;
100 int ahslen;
101
102 /* verify PDU length */
103 datalen = ntoh24(hdr->dlength);
200ae1a0
OG
104 if (datalen > rx_data_len || (datalen + 4) < rx_data_len) {
105 iser_err("wrong datalen %d (hdr), %d (IB)\n",
106 datalen, rx_data_len);
65e7ae7b
OG
107 rc = ISCSI_ERR_DATALEN;
108 goto error;
109 }
110
200ae1a0
OG
111 if (datalen != rx_data_len)
112 iser_dbg("aligned datalen (%d) hdr, %d (IB)\n",
113 datalen, rx_data_len);
114
65e7ae7b
OG
115 /* read AHS */
116 ahslen = hdr->hlength * 4;
117
0af967f5 118 rc = iscsi_complete_pdu(conn, hdr, rx_data, rx_data_len);
65e7ae7b
OG
119 if (rc && rc != ISCSI_ERR_NO_SCSI_CMD)
120 goto error;
121
122 return;
123error:
124 iscsi_conn_failure(conn, rc);
125}
126
2ff79d52 127static int iscsi_iser_pdu_alloc(struct iscsi_task *task, uint8_t opcode)
0f9c7449
MC
128{
129 struct iscsi_iser_task *iser_task = task->dd_data;
130
131 task->hdr = (struct iscsi_hdr *)&iser_task->desc.iscsi_header;
132 task->hdr_max = sizeof(iser_task->desc.iscsi_header);
133 return 0;
134}
65e7ae7b 135
f19624aa
OG
136int iser_initialize_task_headers(struct iscsi_task *task,
137 struct iser_tx_desc *tx_desc)
138{
139 struct iscsi_iser_conn *iser_conn = task->conn->dd_data;
140 struct iser_device *device = iser_conn->ib_conn->device;
141 struct iscsi_iser_task *iser_task = task->dd_data;
142 u64 dma_addr;
143
144 dma_addr = ib_dma_map_single(device->ib_device, (void *)tx_desc,
145 ISER_HEADERS_LEN, DMA_TO_DEVICE);
146 if (ib_dma_mapping_error(device->ib_device, dma_addr))
147 return -ENOMEM;
148
149 tx_desc->dma_addr = dma_addr;
150 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
151 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
152 tx_desc->tx_sg[0].lkey = device->mr->lkey;
153
154 iser_task->headers_initialized = 1;
155 iser_task->iser_conn = iser_conn;
156 return 0;
157}
65e7ae7b 158/**
2261ec3d
MC
159 * iscsi_iser_task_init - Initialize task
160 * @task: iscsi task
65e7ae7b 161 *
2261ec3d 162 * Initialize the task for the scsi command or mgmt command.
2747fdb2 163 */
a8ac6311 164static int
2261ec3d 165iscsi_iser_task_init(struct iscsi_task *task)
65e7ae7b 166{
2261ec3d 167 struct iscsi_iser_task *iser_task = task->dd_data;
65e7ae7b 168
f19624aa
OG
169 if (!iser_task->headers_initialized)
170 if (iser_initialize_task_headers(task, &iser_task->desc))
171 return -ENOMEM;
172
2261ec3d 173 /* mgmt task */
f19624aa 174 if (!task->sc)
2747fdb2 175 return 0;
65e7ae7b 176
2261ec3d 177 iser_task->command_sent = 0;
2261ec3d 178 iser_task_rdma_init(iser_task);
a8ac6311 179 return 0;
65e7ae7b
OG
180}
181
182/**
2261ec3d 183 * iscsi_iser_mtask_xmit - xmit management(immediate) task
65e7ae7b 184 * @conn: iscsi connection
2261ec3d 185 * @task: task management task
65e7ae7b
OG
186 *
187 * Notes:
188 * The function can return -EAGAIN in which case caller must
189 * call it again later, or recover. '0' return code means successful
190 * xmit.
191 *
192 **/
193static int
2261ec3d 194iscsi_iser_mtask_xmit(struct iscsi_conn *conn, struct iscsi_task *task)
65e7ae7b
OG
195{
196 int error = 0;
197
962b4b52 198 iser_dbg("mtask xmit [cid %d itt 0x%x]\n", conn->id, task->itt);
65e7ae7b 199
2261ec3d 200 error = iser_send_control(conn, task);
65e7ae7b 201
2261ec3d 202 /* since iser xmits control with zero copy, tasks can not be recycled
65e7ae7b
OG
203 * right after sending them.
204 * The recycling scheme is based on whether a response is expected
2261ec3d
MC
205 * - if yes, the task is recycled at iscsi_complete_pdu
206 * - if no, the task is recycled at iser_snd_completion
65e7ae7b 207 */
65e7ae7b
OG
208 return error;
209}
210
211static int
2747fdb2 212iscsi_iser_task_xmit_unsol_data(struct iscsi_conn *conn,
2261ec3d 213 struct iscsi_task *task)
65e7ae7b 214{
0f9c7449
MC
215 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
216 struct iscsi_data hdr;
65e7ae7b 217 int error = 0;
65e7ae7b
OG
218
219 /* Send data-out PDUs while there's still unsolicited data to send */
0f9c7449
MC
220 while (iscsi_task_has_unsol_data(task)) {
221 iscsi_prep_data_out_pdu(task, r2t, &hdr);
48a237a2 222 iser_dbg("Sending data-out: itt 0x%x, data count %d\n",
0f9c7449 223 hdr.itt, r2t->data_count);
65e7ae7b
OG
224
225 /* the buffer description has been passed with the command */
226 /* Send the command */
2261ec3d 227 error = iser_send_data_out(conn, task, &hdr);
65e7ae7b 228 if (error) {
0f9c7449 229 r2t->datasn--;
2747fdb2 230 goto iscsi_iser_task_xmit_unsol_data_exit;
65e7ae7b 231 }
0f9c7449 232 r2t->sent += r2t->data_count;
48a237a2 233 iser_dbg("Need to send %d more as data-out PDUs\n",
0f9c7449 234 r2t->data_length - r2t->sent);
65e7ae7b
OG
235 }
236
2747fdb2 237iscsi_iser_task_xmit_unsol_data_exit:
65e7ae7b
OG
238 return error;
239}
240
241static int
2261ec3d 242iscsi_iser_task_xmit(struct iscsi_task *task)
65e7ae7b 243{
2261ec3d
MC
244 struct iscsi_conn *conn = task->conn;
245 struct iscsi_iser_task *iser_task = task->dd_data;
65e7ae7b
OG
246 int error = 0;
247
2261ec3d
MC
248 if (!task->sc)
249 return iscsi_iser_mtask_xmit(conn, task);
2747fdb2 250
2261ec3d
MC
251 if (task->sc->sc_data_direction == DMA_TO_DEVICE) {
252 BUG_ON(scsi_bufflen(task->sc) == 0);
77a23c21 253
48a237a2 254 iser_dbg("cmd [itt %x total %d imm %d unsol_data %d\n",
2261ec3d 255 task->itt, scsi_bufflen(task->sc),
0f9c7449 256 task->imm_count, task->unsol_r2t.data_length);
77a23c21
MC
257 }
258
962b4b52 259 iser_dbg("ctask xmit [cid %d itt 0x%x]\n",
2261ec3d 260 conn->id, task->itt);
65e7ae7b 261
65e7ae7b 262 /* Send the cmd PDU */
2261ec3d
MC
263 if (!iser_task->command_sent) {
264 error = iser_send_command(conn, task);
65e7ae7b 265 if (error)
2747fdb2 266 goto iscsi_iser_task_xmit_exit;
2261ec3d 267 iser_task->command_sent = 1;
65e7ae7b
OG
268 }
269
270 /* Send unsolicited data-out PDU(s) if necessary */
0f9c7449 271 if (iscsi_task_has_unsol_data(task))
2261ec3d 272 error = iscsi_iser_task_xmit_unsol_data(conn, task);
65e7ae7b 273
2747fdb2 274 iscsi_iser_task_xmit_exit:
65e7ae7b
OG
275 return error;
276}
277
0f9c7449 278static void iscsi_iser_cleanup_task(struct iscsi_task *task)
65e7ae7b 279{
2261ec3d 280 struct iscsi_iser_task *iser_task = task->dd_data;
65e7ae7b 281
b3cd5050
MC
282 /* mgmt tasks do not need special cleanup */
283 if (!task->sc)
2747fdb2 284 return;
65e7ae7b 285
2261ec3d
MC
286 if (iser_task->status == ISER_TASK_STATUS_STARTED) {
287 iser_task->status = ISER_TASK_STATUS_COMPLETED;
288 iser_task_rdma_finalize(iser_task);
65e7ae7b 289 }
65e7ae7b
OG
290}
291
292static struct iscsi_cls_conn *
293iscsi_iser_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
294{
295 struct iscsi_conn *conn;
296 struct iscsi_cls_conn *cls_conn;
297 struct iscsi_iser_conn *iser_conn;
298
5d91e209 299 cls_conn = iscsi_conn_setup(cls_session, sizeof(*iser_conn), conn_idx);
65e7ae7b
OG
300 if (!cls_conn)
301 return NULL;
302 conn = cls_conn->dd_data;
303
304 /*
305 * due to issues with the login code re iser sematics
306 * this not set in iscsi_conn_setup - FIXME
307 */
bcc60c38 308 conn->max_recv_dlength = ISER_RECV_DATA_SEG_LEN;
65e7ae7b 309
5d91e209 310 iser_conn = conn->dd_data;
65e7ae7b
OG
311 conn->dd_data = iser_conn;
312 iser_conn->iscsi_conn = conn;
313
314 return cls_conn;
65e7ae7b
OG
315}
316
317static void
318iscsi_iser_conn_destroy(struct iscsi_cls_conn *cls_conn)
319{
320 struct iscsi_conn *conn = cls_conn->dd_data;
321 struct iscsi_iser_conn *iser_conn = conn->dd_data;
b40977d9 322 struct iser_conn *ib_conn = iser_conn->ib_conn;
65e7ae7b
OG
323
324 iscsi_conn_teardown(cls_conn);
b40977d9
MC
325 /*
326 * Userspace will normally call the stop callback and
327 * already have freed the ib_conn, but if it goofed up then
328 * we free it here.
329 */
330 if (ib_conn) {
331 ib_conn->iser_conn = NULL;
39ff05db 332 iser_conn_put(ib_conn, 1); /* deref iscsi/ib conn unbinding */
b40977d9 333 }
65e7ae7b
OG
334}
335
336static int
337iscsi_iser_conn_bind(struct iscsi_cls_session *cls_session,
338 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
339 int is_leading)
340{
341 struct iscsi_conn *conn = cls_conn->dd_data;
342 struct iscsi_iser_conn *iser_conn;
343 struct iser_conn *ib_conn;
412eeafa 344 struct iscsi_endpoint *ep;
65e7ae7b
OG
345 int error;
346
347 error = iscsi_conn_bind(cls_session, cls_conn, is_leading);
348 if (error)
349 return error;
350
351 /* the transport ep handle comes from user space so it must be
352 * verified against the global ib connections list */
412eeafa
MC
353 ep = iscsi_lookup_endpoint(transport_eph);
354 if (!ep) {
65e7ae7b
OG
355 iser_err("can't bind eph %llx\n",
356 (unsigned long long)transport_eph);
357 return -EINVAL;
358 }
412eeafa
MC
359 ib_conn = ep->dd_data;
360
65e7ae7b
OG
361 /* binds the iSER connection retrieved from the previously
362 * connected ep_handle to the iSCSI layer connection. exchanges
363 * connection pointers */
39ff05db
OG
364 iser_err("binding iscsi/iser conn %p %p to ib_conn %p\n",
365 conn, conn->dd_data, ib_conn);
65e7ae7b
OG
366 iser_conn = conn->dd_data;
367 ib_conn->iser_conn = iser_conn;
368 iser_conn->ib_conn = ib_conn;
39ff05db 369 iser_conn_get(ib_conn); /* ref iscsi/ib conn binding */
65e7ae7b
OG
370 return 0;
371}
65e7ae7b 372
b40977d9
MC
373static void
374iscsi_iser_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
375{
376 struct iscsi_conn *conn = cls_conn->dd_data;
377 struct iscsi_iser_conn *iser_conn = conn->dd_data;
378 struct iser_conn *ib_conn = iser_conn->ib_conn;
65e7ae7b 379
b40977d9 380 /*
913e5bf4
MC
381 * Userspace may have goofed up and not bound the connection or
382 * might have only partially setup the connection.
b40977d9 383 */
913e5bf4
MC
384 if (ib_conn) {
385 iscsi_conn_stop(cls_conn, flag);
386 /*
387 * There is no unbind event so the stop callback
388 * must release the ref from the bind.
389 */
39ff05db 390 iser_conn_put(ib_conn, 1); /* deref iscsi/ib conn unbinding */
913e5bf4 391 }
b40977d9 392 iser_conn->ib_conn = NULL;
65e7ae7b
OG
393}
394
395static int
396iscsi_iser_conn_start(struct iscsi_cls_conn *cls_conn)
397{
398 struct iscsi_conn *conn = cls_conn->dd_data;
399 int err;
400
2e7a7426 401 err = iser_conn_set_full_featured_mode(conn);
65e7ae7b
OG
402 if (err)
403 return err;
404
2e7a7426 405 return iscsi_conn_start(cls_conn);
65e7ae7b
OG
406}
407
75613521
MC
408static void iscsi_iser_session_destroy(struct iscsi_cls_session *cls_session)
409{
410 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
411
e5bd7b54 412 iscsi_session_teardown(cls_session);
a4804cd6
MC
413 iscsi_host_remove(shost);
414 iscsi_host_free(shost);
75613521 415}
65e7ae7b
OG
416
417static struct iscsi_cls_session *
412eeafa 418iscsi_iser_session_create(struct iscsi_endpoint *ep,
75613521 419 uint16_t cmds_max, uint16_t qdepth,
5e7facb7 420 uint32_t initial_cmdsn)
65e7ae7b
OG
421{
422 struct iscsi_cls_session *cls_session;
423 struct iscsi_session *session;
412eeafa 424 struct Scsi_Host *shost;
412eeafa 425 struct iser_conn *ib_conn;
75613521 426
962b4b52 427 shost = iscsi_host_alloc(&iscsi_iser_sht, 0, 0);
75613521
MC
428 if (!shost)
429 return NULL;
430 shost->transportt = iscsi_iser_scsi_transport;
431 shost->max_lun = iscsi_max_lun;
432 shost->max_id = 0;
433 shost->max_channel = 0;
434 shost->max_cmd_len = 16;
435
412eeafa
MC
436 /*
437 * older userspace tools (before 2.0-870) did not pass us
438 * the leading conn's ep so this will be NULL;
439 */
440 if (ep)
441 ib_conn = ep->dd_data;
442
443 if (iscsi_host_add(shost,
444 ep ? ib_conn->device->ib_device->dma_device : NULL))
75613521 445 goto free_host;
65e7ae7b 446
1548271e
MC
447 /*
448 * we do not support setting can_queue cmd_per_lun from userspace yet
449 * because we preallocate so many resources
450 */
75613521 451 cls_session = iscsi_session_setup(&iscsi_iser_transport, shost,
b8b9e1b8 452 ISCSI_DEF_XMIT_CMDS_MAX, 0,
2261ec3d 453 sizeof(struct iscsi_iser_task),
7970634b 454 initial_cmdsn, 0);
65e7ae7b 455 if (!cls_session)
75613521
MC
456 goto remove_host;
457 session = cls_session->dd_data;
65e7ae7b 458
2747fdb2 459 shost->can_queue = session->scsi_cmds_max;
65e7ae7b 460 return cls_session;
75613521
MC
461
462remove_host:
a4804cd6 463 iscsi_host_remove(shost);
75613521 464free_host:
a4804cd6 465 iscsi_host_free(shost);
75613521 466 return NULL;
65e7ae7b
OG
467}
468
469static int
358ff019
MC
470iscsi_iser_set_param(struct iscsi_cls_conn *cls_conn,
471 enum iscsi_param param, char *buf, int buflen)
65e7ae7b 472{
358ff019 473 int value;
65e7ae7b
OG
474
475 switch (param) {
476 case ISCSI_PARAM_MAX_RECV_DLENGTH:
477 /* TBD */
478 break;
65e7ae7b 479 case ISCSI_PARAM_HDRDGST_EN:
358ff019 480 sscanf(buf, "%d", &value);
65e7ae7b
OG
481 if (value) {
482 printk(KERN_ERR "DataDigest wasn't negotiated to None");
483 return -EPROTO;
484 }
485 break;
486 case ISCSI_PARAM_DATADGST_EN:
358ff019 487 sscanf(buf, "%d", &value);
65e7ae7b
OG
488 if (value) {
489 printk(KERN_ERR "DataDigest wasn't negotiated to None");
490 return -EPROTO;
491 }
492 break;
65e7ae7b 493 case ISCSI_PARAM_IFMARKER_EN:
358ff019 494 sscanf(buf, "%d", &value);
65e7ae7b
OG
495 if (value) {
496 printk(KERN_ERR "IFMarker wasn't negotiated to No");
497 return -EPROTO;
498 }
499 break;
500 case ISCSI_PARAM_OFMARKER_EN:
358ff019 501 sscanf(buf, "%d", &value);
65e7ae7b
OG
502 if (value) {
503 printk(KERN_ERR "OFMarker wasn't negotiated to No");
504 return -EPROTO;
505 }
506 break;
507 default:
358ff019 508 return iscsi_set_param(cls_conn, param, buf, buflen);
65e7ae7b
OG
509 }
510
511 return 0;
512}
513
65e7ae7b
OG
514static void
515iscsi_iser_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
516{
517 struct iscsi_conn *conn = cls_conn->dd_data;
518
519 stats->txdata_octets = conn->txdata_octets;
520 stats->rxdata_octets = conn->rxdata_octets;
521 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
522 stats->dataout_pdus = conn->dataout_pdus_cnt;
523 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
524 stats->datain_pdus = conn->datain_pdus_cnt; /* always 0 */
525 stats->r2t_pdus = conn->r2t_pdus_cnt; /* always 0 */
526 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
527 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
87528227 528 stats->custom_length = 4;
65e7ae7b
OG
529 strcpy(stats->custom[0].desc, "qp_tx_queue_full");
530 stats->custom[0].value = 0; /* TB iser_conn->qp_tx_queue_full; */
531 strcpy(stats->custom[1].desc, "fmr_map_not_avail");
532 stats->custom[1].value = 0; /* TB iser_conn->fmr_map_not_avail */;
533 strcpy(stats->custom[2].desc, "eh_abort_cnt");
534 stats->custom[2].value = conn->eh_abort_cnt;
87528227
ED
535 strcpy(stats->custom[3].desc, "fmr_unalign_cnt");
536 stats->custom[3].value = conn->fmr_unalign_cnt;
65e7ae7b
OG
537}
538
7c53c6f8
MC
539static int iscsi_iser_get_ep_param(struct iscsi_endpoint *ep,
540 enum iscsi_param param, char *buf)
541{
542 struct iser_conn *ib_conn = ep->dd_data;
543 int len;
544
545 switch (param) {
546 case ISCSI_PARAM_CONN_PORT:
547 case ISCSI_PARAM_CONN_ADDRESS:
548 if (!ib_conn || !ib_conn->cma_id)
549 return -ENOTCONN;
550
551 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
552 &ib_conn->cma_id->route.addr.dst_addr,
553 param, buf);
554 break;
555 default:
556 return -ENOSYS;
557 }
558
559 return len;
560}
561
412eeafa 562static struct iscsi_endpoint *
10eb0f01
MC
563iscsi_iser_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
564 int non_blocking)
65e7ae7b
OG
565{
566 int err;
567 struct iser_conn *ib_conn;
412eeafa 568 struct iscsi_endpoint *ep;
65e7ae7b 569
412eeafa
MC
570 ep = iscsi_create_endpoint(sizeof(*ib_conn));
571 if (!ep)
572 return ERR_PTR(-ENOMEM);
65e7ae7b 573
412eeafa
MC
574 ib_conn = ep->dd_data;
575 ib_conn->ep = ep;
576 iser_conn_init(ib_conn);
65e7ae7b 577
412eeafa
MC
578 err = iser_connect(ib_conn, NULL, (struct sockaddr_in *)dst_addr,
579 non_blocking);
580 if (err) {
581 iscsi_destroy_endpoint(ep);
582 return ERR_PTR(err);
583 }
584 return ep;
65e7ae7b
OG
585}
586
587static int
412eeafa 588iscsi_iser_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
65e7ae7b 589{
412eeafa 590 struct iser_conn *ib_conn;
65e7ae7b
OG
591 int rc;
592
412eeafa 593 ib_conn = ep->dd_data;
65e7ae7b
OG
594 rc = wait_event_interruptible_timeout(ib_conn->wait,
595 ib_conn->state == ISER_CONN_UP,
596 msecs_to_jiffies(timeout_ms));
597
598 /* if conn establishment failed, return error code to iscsi */
599 if (!rc &&
600 (ib_conn->state == ISER_CONN_TERMINATING ||
601 ib_conn->state == ISER_CONN_DOWN))
602 rc = -1;
603
604 iser_err("ib conn %p rc = %d\n", ib_conn, rc);
605
606 if (rc > 0)
607 return 1; /* success, this is the equivalent of POLLOUT */
608 else if (!rc)
609 return 0; /* timeout */
610 else
611 return rc; /* signal */
612}
613
614static void
412eeafa 615iscsi_iser_ep_disconnect(struct iscsi_endpoint *ep)
65e7ae7b 616{
1c83469d 617 struct iser_conn *ib_conn;
65e7ae7b 618
412eeafa 619 ib_conn = ep->dd_data;
b40977d9
MC
620 if (ib_conn->iser_conn)
621 /*
622 * Must suspend xmit path if the ep is bound to the
623 * iscsi_conn, so we know we are not accessing the ib_conn
624 * when we free it.
625 *
626 * This may not be bound if the ep poll failed.
627 */
628 iscsi_suspend_tx(ib_conn->iser_conn->iscsi_conn);
629
65e7ae7b
OG
630
631 iser_err("ib conn %p state %d\n",ib_conn, ib_conn->state);
65e7ae7b
OG
632 iser_conn_terminate(ib_conn);
633}
634
3128c6c7
MC
635static mode_t iser_attr_is_visible(int param_type, int param)
636{
637 switch (param_type) {
638 case ISCSI_PARAM:
639 switch (param) {
640 case ISCSI_PARAM_MAX_RECV_DLENGTH:
641 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
642 case ISCSI_PARAM_HDRDGST_EN:
643 case ISCSI_PARAM_DATADGST_EN:
644 case ISCSI_PARAM_CONN_ADDRESS:
645 case ISCSI_PARAM_CONN_PORT:
646 case ISCSI_PARAM_EXP_STATSN:
647 case ISCSI_PARAM_PERSISTENT_ADDRESS:
648 case ISCSI_PARAM_PERSISTENT_PORT:
649 case ISCSI_PARAM_PING_TMO:
650 case ISCSI_PARAM_RECV_TMO:
651 return S_IRUGO;
652 default:
653 return 0;
654 }
655 }
656
657 return 0;
658}
659
65e7ae7b 660static struct scsi_host_template iscsi_iser_sht = {
7974392c 661 .module = THIS_MODULE,
65e7ae7b
OG
662 .name = "iSCSI Initiator over iSER, v." DRV_VER,
663 .queuecommand = iscsi_queuecommand,
6410627e 664 .change_queue_depth = iscsi_change_queue_depth,
65e7ae7b 665 .sg_tablesize = ISCSI_ISER_SG_TABLESIZE,
8072ec2f 666 .max_sectors = 1024,
e28f3d5b 667 .cmd_per_lun = ISER_DEF_CMD_PER_LUN,
65e7ae7b 668 .eh_abort_handler = iscsi_eh_abort,
90c18f3c 669 .eh_device_reset_handler= iscsi_eh_device_reset,
309ce156 670 .eh_target_reset_handler = iscsi_eh_recover_target,
6b5d6c44 671 .target_alloc = iscsi_target_alloc,
65e7ae7b
OG
672 .use_clustering = DISABLE_CLUSTERING,
673 .proc_name = "iscsi_iser",
674 .this_id = -1,
675};
676
677static struct iscsi_transport iscsi_iser_transport = {
678 .owner = THIS_MODULE,
679 .name = "iser",
680 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T,
3128c6c7 681 .param_mask = ISCSI_INITIAL_R2T_EN |
65e7ae7b
OG
682 ISCSI_MAX_R2T |
683 ISCSI_IMM_DATA_EN |
684 ISCSI_FIRST_BURST |
685 ISCSI_MAX_BURST |
686 ISCSI_PDU_INORDER_EN |
358ff019 687 ISCSI_DATASEQ_INORDER_EN |
b2c64167
MC
688 ISCSI_TARGET_NAME | ISCSI_TPGT |
689 ISCSI_USERNAME | ISCSI_PASSWORD |
f6d5180c
MC
690 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
691 ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
b20d038d 692 ISCSI_LU_RESET_TMO | ISCSI_TGT_RESET_TMO |
88dfd340 693 ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
8ad5781a 694 .host_param_mask = ISCSI_HOST_HWADDRESS |
d8196ed2 695 ISCSI_HOST_NETDEV_NAME |
8ad5781a 696 ISCSI_HOST_INITIATOR_NAME,
65e7ae7b
OG
697 /* session management */
698 .create_session = iscsi_iser_session_create,
75613521 699 .destroy_session = iscsi_iser_session_destroy,
65e7ae7b
OG
700 /* connection management */
701 .create_conn = iscsi_iser_conn_create,
702 .bind_conn = iscsi_iser_conn_bind,
703 .destroy_conn = iscsi_iser_conn_destroy,
3128c6c7 704 .attr_is_visible = iser_attr_is_visible,
358ff019
MC
705 .set_param = iscsi_iser_set_param,
706 .get_conn_param = iscsi_conn_get_param,
7c53c6f8 707 .get_ep_param = iscsi_iser_get_ep_param,
358ff019 708 .get_session_param = iscsi_session_get_param,
65e7ae7b 709 .start_conn = iscsi_iser_conn_start,
b40977d9 710 .stop_conn = iscsi_iser_conn_stop,
0801c242
MC
711 /* iscsi host params */
712 .get_host_param = iscsi_host_get_param,
713 .set_host_param = iscsi_host_set_param,
65e7ae7b
OG
714 /* IO */
715 .send_pdu = iscsi_conn_send_pdu,
716 .get_stats = iscsi_iser_conn_get_stats,
2747fdb2
MC
717 .init_task = iscsi_iser_task_init,
718 .xmit_task = iscsi_iser_task_xmit,
719 .cleanup_task = iscsi_iser_cleanup_task,
0f9c7449 720 .alloc_pdu = iscsi_iser_pdu_alloc,
65e7ae7b
OG
721 /* recovery */
722 .session_recovery_timedout = iscsi_session_recovery_timedout,
723
724 .ep_connect = iscsi_iser_ep_connect,
725 .ep_poll = iscsi_iser_ep_poll,
726 .ep_disconnect = iscsi_iser_ep_disconnect
727};
728
729static int __init iser_init(void)
730{
731 int err;
732
733 iser_dbg("Starting iSER datamover...\n");
734
735 if (iscsi_max_lun < 1) {
736 printk(KERN_ERR "Invalid max_lun value of %u\n", iscsi_max_lun);
737 return -EINVAL;
738 }
739
65e7ae7b
OG
740 memset(&ig, 0, sizeof(struct iser_global));
741
742 ig.desc_cache = kmem_cache_create("iser_descriptors",
f19624aa 743 sizeof(struct iser_tx_desc),
65e7ae7b 744 0, SLAB_HWCACHE_ALIGN,
20c2df83 745 NULL);
65e7ae7b
OG
746 if (ig.desc_cache == NULL)
747 return -ENOMEM;
748
749 /* device init is called only after the first addr resolution */
750 mutex_init(&ig.device_list_mutex);
751 INIT_LIST_HEAD(&ig.device_list);
752 mutex_init(&ig.connlist_mutex);
753 INIT_LIST_HEAD(&ig.connlist);
754
75613521
MC
755 iscsi_iser_scsi_transport = iscsi_register_transport(
756 &iscsi_iser_transport);
757 if (!iscsi_iser_scsi_transport) {
65e7ae7b
OG
758 iser_err("iscsi_register_transport failed\n");
759 err = -EINVAL;
760 goto register_transport_failure;
761 }
762
763 return 0;
764
765register_transport_failure:
766 kmem_cache_destroy(ig.desc_cache);
767
768 return err;
769}
770
771static void __exit iser_exit(void)
772{
773 iser_dbg("Removing iSER datamover...\n");
774 iscsi_unregister_transport(&iscsi_iser_transport);
775 kmem_cache_destroy(ig.desc_cache);
776}
777
778module_init(iser_init);
779module_exit(iser_exit);