]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/infiniband/ulp/iser/iscsi_iser.c
IB/iser: Add/Fix kernel doc style descriptions in iscsi_iser.h
[mirror_ubuntu-artful-kernel.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.
3ee07d27 8 * Copyright (c) 2013-2014 Mellanox Technologies. All rights reserved.
65e7ae7b
OG
9 * maintained by openib-general@openib.org
10 *
11 * This software is available to you under a choice of one of two
12 * licenses. You may choose to be licensed under the terms of the GNU
13 * General Public License (GPL) Version 2, available from the file
14 * COPYING in the main directory of this source tree, or the
15 * OpenIB.org BSD license below:
16 *
17 * Redistribution and use in source and binary forms, with or
18 * without modification, are permitted provided that the following
19 * conditions are met:
20 *
21 * - Redistributions of source code must retain the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer.
24 *
25 * - Redistributions in binary form must reproduce the above
26 * copyright notice, this list of conditions and the following
27 * disclaimer in the documentation and/or other materials
28 * provided with the distribution.
29 *
30 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
33 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
34 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
35 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
36 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37 * SOFTWARE.
38 *
39 * Credits:
40 * Christoph Hellwig
41 * FUJITA Tomonori
42 * Arne Redlich
43 * Zhenyu Wang
44 * Modified by:
45 * Erez Zilber
65e7ae7b
OG
46 */
47
48#include <linux/types.h>
49#include <linux/list.h>
50#include <linux/hardirq.h>
51#include <linux/kfifo.h>
52#include <linux/blkdev.h>
53#include <linux/init.h>
54#include <linux/ioctl.h>
65e7ae7b
OG
55#include <linux/cdev.h>
56#include <linux/in.h>
57#include <linux/net.h>
58#include <linux/scatterlist.h>
59#include <linux/delay.h>
5a0e3ad6 60#include <linux/slab.h>
e4dd23d7 61#include <linux/module.h>
65e7ae7b
OG
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
75613521
MC
77static struct scsi_host_template iscsi_iser_sht;
78static struct iscsi_transport iscsi_iser_transport;
79static struct scsi_transport_template *iscsi_iser_scsi_transport;
80
65e7ae7b
OG
81static unsigned int iscsi_max_lun = 512;
82module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
83
84int iser_debug_level = 0;
7f733847
AT
85bool iser_pi_enable = false;
86int iser_pi_guard = 0;
65e7ae7b 87
c1d786e6 88MODULE_DESCRIPTION("iSER (iSCSI Extensions for RDMA) Datamover");
65e7ae7b
OG
89MODULE_LICENSE("Dual BSD/GPL");
90MODULE_AUTHOR("Alex Nezhinsky, Dan Bar Dov, Or Gerlitz");
c1d786e6 91MODULE_VERSION(DRV_VER);
65e7ae7b
OG
92
93module_param_named(debug_level, iser_debug_level, int, 0644);
94MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0 (default:disabled)");
95
7f733847
AT
96module_param_named(pi_enable, iser_pi_enable, bool, 0644);
97MODULE_PARM_DESC(pi_enable, "Enable T10-PI offload support (default:disabled)");
98
99module_param_named(pi_guard, iser_pi_guard, int, 0644);
100MODULE_PARM_DESC(pi_guard, "T10-PI guard_type, 0:CRC|1:IP_CSUM (default:CRC)");
101
b73c3ada 102static struct workqueue_struct *release_wq;
65e7ae7b
OG
103struct iser_global ig;
104
105void
106iscsi_iser_recv(struct iscsi_conn *conn,
107 struct iscsi_hdr *hdr, char *rx_data, int rx_data_len)
108{
109 int rc = 0;
65e7ae7b
OG
110 int datalen;
111 int ahslen;
112
113 /* verify PDU length */
114 datalen = ntoh24(hdr->dlength);
200ae1a0
OG
115 if (datalen > rx_data_len || (datalen + 4) < rx_data_len) {
116 iser_err("wrong datalen %d (hdr), %d (IB)\n",
117 datalen, rx_data_len);
65e7ae7b
OG
118 rc = ISCSI_ERR_DATALEN;
119 goto error;
120 }
121
200ae1a0
OG
122 if (datalen != rx_data_len)
123 iser_dbg("aligned datalen (%d) hdr, %d (IB)\n",
124 datalen, rx_data_len);
125
65e7ae7b
OG
126 /* read AHS */
127 ahslen = hdr->hlength * 4;
128
0af967f5 129 rc = iscsi_complete_pdu(conn, hdr, rx_data, rx_data_len);
65e7ae7b
OG
130 if (rc && rc != ISCSI_ERR_NO_SCSI_CMD)
131 goto error;
132
133 return;
134error:
135 iscsi_conn_failure(conn, rc);
136}
137
2ff79d52 138static int iscsi_iser_pdu_alloc(struct iscsi_task *task, uint8_t opcode)
0f9c7449
MC
139{
140 struct iscsi_iser_task *iser_task = task->dd_data;
141
142 task->hdr = (struct iscsi_hdr *)&iser_task->desc.iscsi_header;
143 task->hdr_max = sizeof(iser_task->desc.iscsi_header);
144 return 0;
145}
65e7ae7b 146
f19624aa
OG
147int iser_initialize_task_headers(struct iscsi_task *task,
148 struct iser_tx_desc *tx_desc)
149{
5716af6e 150 struct iser_conn *iser_conn = task->conn->dd_data;
a4ee3539 151 struct iser_device *device = iser_conn->ib_conn.device;
f19624aa
OG
152 struct iscsi_iser_task *iser_task = task->dd_data;
153 u64 dma_addr;
154
155 dma_addr = ib_dma_map_single(device->ib_device, (void *)tx_desc,
156 ISER_HEADERS_LEN, DMA_TO_DEVICE);
157 if (ib_dma_mapping_error(device->ib_device, dma_addr))
158 return -ENOMEM;
159
160 tx_desc->dma_addr = dma_addr;
161 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
162 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
163 tx_desc->tx_sg[0].lkey = device->mr->lkey;
164
5716af6e 165 iser_task->iser_conn = iser_conn;
f19624aa
OG
166 return 0;
167}
65e7ae7b 168/**
2261ec3d
MC
169 * iscsi_iser_task_init - Initialize task
170 * @task: iscsi task
65e7ae7b 171 *
2261ec3d 172 * Initialize the task for the scsi command or mgmt command.
2747fdb2 173 */
a8ac6311 174static int
2261ec3d 175iscsi_iser_task_init(struct iscsi_task *task)
65e7ae7b 176{
2261ec3d 177 struct iscsi_iser_task *iser_task = task->dd_data;
65e7ae7b 178
52439540 179 if (iser_initialize_task_headers(task, &iser_task->desc))
f19624aa
OG
180 return -ENOMEM;
181
2261ec3d 182 /* mgmt task */
f19624aa 183 if (!task->sc)
2747fdb2 184 return 0;
65e7ae7b 185
2261ec3d 186 iser_task->command_sent = 0;
2261ec3d 187 iser_task_rdma_init(iser_task);
177e31bd
SG
188 iser_task->sc = task->sc;
189
a8ac6311 190 return 0;
65e7ae7b
OG
191}
192
193/**
2261ec3d 194 * iscsi_iser_mtask_xmit - xmit management(immediate) task
65e7ae7b 195 * @conn: iscsi connection
2261ec3d 196 * @task: task management task
65e7ae7b
OG
197 *
198 * Notes:
199 * The function can return -EAGAIN in which case caller must
200 * call it again later, or recover. '0' return code means successful
201 * xmit.
202 *
203 **/
204static int
2261ec3d 205iscsi_iser_mtask_xmit(struct iscsi_conn *conn, struct iscsi_task *task)
65e7ae7b
OG
206{
207 int error = 0;
208
962b4b52 209 iser_dbg("mtask xmit [cid %d itt 0x%x]\n", conn->id, task->itt);
65e7ae7b 210
2261ec3d 211 error = iser_send_control(conn, task);
65e7ae7b 212
2261ec3d 213 /* since iser xmits control with zero copy, tasks can not be recycled
65e7ae7b
OG
214 * right after sending them.
215 * The recycling scheme is based on whether a response is expected
2261ec3d
MC
216 * - if yes, the task is recycled at iscsi_complete_pdu
217 * - if no, the task is recycled at iser_snd_completion
65e7ae7b 218 */
65e7ae7b
OG
219 return error;
220}
221
222static int
2747fdb2 223iscsi_iser_task_xmit_unsol_data(struct iscsi_conn *conn,
2261ec3d 224 struct iscsi_task *task)
65e7ae7b 225{
0f9c7449
MC
226 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
227 struct iscsi_data hdr;
65e7ae7b 228 int error = 0;
65e7ae7b
OG
229
230 /* Send data-out PDUs while there's still unsolicited data to send */
0f9c7449
MC
231 while (iscsi_task_has_unsol_data(task)) {
232 iscsi_prep_data_out_pdu(task, r2t, &hdr);
48a237a2 233 iser_dbg("Sending data-out: itt 0x%x, data count %d\n",
0f9c7449 234 hdr.itt, r2t->data_count);
65e7ae7b
OG
235
236 /* the buffer description has been passed with the command */
237 /* Send the command */
2261ec3d 238 error = iser_send_data_out(conn, task, &hdr);
65e7ae7b 239 if (error) {
0f9c7449 240 r2t->datasn--;
2747fdb2 241 goto iscsi_iser_task_xmit_unsol_data_exit;
65e7ae7b 242 }
0f9c7449 243 r2t->sent += r2t->data_count;
48a237a2 244 iser_dbg("Need to send %d more as data-out PDUs\n",
0f9c7449 245 r2t->data_length - r2t->sent);
65e7ae7b
OG
246 }
247
2747fdb2 248iscsi_iser_task_xmit_unsol_data_exit:
65e7ae7b
OG
249 return error;
250}
251
252static int
2261ec3d 253iscsi_iser_task_xmit(struct iscsi_task *task)
65e7ae7b 254{
2261ec3d
MC
255 struct iscsi_conn *conn = task->conn;
256 struct iscsi_iser_task *iser_task = task->dd_data;
65e7ae7b
OG
257 int error = 0;
258
2261ec3d
MC
259 if (!task->sc)
260 return iscsi_iser_mtask_xmit(conn, task);
2747fdb2 261
2261ec3d
MC
262 if (task->sc->sc_data_direction == DMA_TO_DEVICE) {
263 BUG_ON(scsi_bufflen(task->sc) == 0);
77a23c21 264
48a237a2 265 iser_dbg("cmd [itt %x total %d imm %d unsol_data %d\n",
2261ec3d 266 task->itt, scsi_bufflen(task->sc),
0f9c7449 267 task->imm_count, task->unsol_r2t.data_length);
77a23c21
MC
268 }
269
962b4b52 270 iser_dbg("ctask xmit [cid %d itt 0x%x]\n",
2261ec3d 271 conn->id, task->itt);
65e7ae7b 272
65e7ae7b 273 /* Send the cmd PDU */
2261ec3d
MC
274 if (!iser_task->command_sent) {
275 error = iser_send_command(conn, task);
65e7ae7b 276 if (error)
2747fdb2 277 goto iscsi_iser_task_xmit_exit;
2261ec3d 278 iser_task->command_sent = 1;
65e7ae7b
OG
279 }
280
281 /* Send unsolicited data-out PDU(s) if necessary */
0f9c7449 282 if (iscsi_task_has_unsol_data(task))
2261ec3d 283 error = iscsi_iser_task_xmit_unsol_data(conn, task);
65e7ae7b 284
2747fdb2 285 iscsi_iser_task_xmit_exit:
65e7ae7b
OG
286 return error;
287}
288
0f9c7449 289static void iscsi_iser_cleanup_task(struct iscsi_task *task)
65e7ae7b 290{
2261ec3d 291 struct iscsi_iser_task *iser_task = task->dd_data;
4667f5df 292 struct iser_tx_desc *tx_desc = &iser_task->desc;
5716af6e 293 struct iser_conn *iser_conn = task->conn->dd_data;
a4ee3539 294 struct iser_device *device = iser_conn->ib_conn.device;
52439540 295
3a940daf
SG
296 /* DEVICE_REMOVAL event might have already released the device */
297 if (!device)
298 return;
299
52439540
OG
300 ib_dma_unmap_single(device->ib_device,
301 tx_desc->dma_addr, ISER_HEADERS_LEN, DMA_TO_DEVICE);
65e7ae7b 302
b3cd5050
MC
303 /* mgmt tasks do not need special cleanup */
304 if (!task->sc)
2747fdb2 305 return;
65e7ae7b 306
2261ec3d
MC
307 if (iser_task->status == ISER_TASK_STATUS_STARTED) {
308 iser_task->status = ISER_TASK_STATUS_COMPLETED;
309 iser_task_rdma_finalize(iser_task);
65e7ae7b 310 }
65e7ae7b
OG
311}
312
0a7a08ad
SG
313static u8 iscsi_iser_check_protection(struct iscsi_task *task, sector_t *sector)
314{
315 struct iscsi_iser_task *iser_task = task->dd_data;
316
317 if (iser_task->dir[ISER_DIR_IN])
318 return iser_check_task_pi_status(iser_task, ISER_DIR_IN,
319 sector);
320 else
321 return iser_check_task_pi_status(iser_task, ISER_DIR_OUT,
322 sector);
323}
324
65e7ae7b
OG
325static struct iscsi_cls_conn *
326iscsi_iser_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
327{
328 struct iscsi_conn *conn;
329 struct iscsi_cls_conn *cls_conn;
65e7ae7b 330
4667f5df 331 cls_conn = iscsi_conn_setup(cls_session, 0, conn_idx);
65e7ae7b
OG
332 if (!cls_conn)
333 return NULL;
334 conn = cls_conn->dd_data;
335
336 /*
337 * due to issues with the login code re iser sematics
338 * this not set in iscsi_conn_setup - FIXME
339 */
bcc60c38 340 conn->max_recv_dlength = ISER_RECV_DATA_SEG_LEN;
65e7ae7b 341
65e7ae7b 342 return cls_conn;
65e7ae7b
OG
343}
344
65e7ae7b
OG
345static int
346iscsi_iser_conn_bind(struct iscsi_cls_session *cls_session,
347 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
348 int is_leading)
349{
350 struct iscsi_conn *conn = cls_conn->dd_data;
5716af6e 351 struct iser_conn *iser_conn;
412eeafa 352 struct iscsi_endpoint *ep;
65e7ae7b
OG
353 int error;
354
355 error = iscsi_conn_bind(cls_session, cls_conn, is_leading);
356 if (error)
357 return error;
358
359 /* the transport ep handle comes from user space so it must be
360 * verified against the global ib connections list */
412eeafa
MC
361 ep = iscsi_lookup_endpoint(transport_eph);
362 if (!ep) {
65e7ae7b
OG
363 iser_err("can't bind eph %llx\n",
364 (unsigned long long)transport_eph);
365 return -EINVAL;
366 }
5716af6e 367 iser_conn = ep->dd_data;
412eeafa 368
5716af6e
SG
369 mutex_lock(&iser_conn->state_mutex);
370 if (iser_conn->state != ISER_CONN_UP) {
91eb1df3
SG
371 error = -EINVAL;
372 iser_err("iser_conn %p state is %d, teardown started\n",
5716af6e 373 iser_conn, iser_conn->state);
91eb1df3
SG
374 goto out;
375 }
376
5716af6e 377 error = iser_alloc_rx_descriptors(iser_conn, conn->session);
91eb1df3
SG
378 if (error)
379 goto out;
89e984e2 380
65e7ae7b
OG
381 /* binds the iSER connection retrieved from the previously
382 * connected ep_handle to the iSCSI layer connection. exchanges
383 * connection pointers */
5716af6e 384 iser_info("binding iscsi conn %p to iser_conn %p\n", conn, iser_conn);
4667f5df 385
5716af6e
SG
386 conn->dd_data = iser_conn;
387 iser_conn->iscsi_conn = conn;
4667f5df 388
91eb1df3 389out:
5716af6e 390 mutex_unlock(&iser_conn->state_mutex);
91eb1df3 391 return error;
65e7ae7b 392}
65e7ae7b 393
b73c3ada
AN
394static int
395iscsi_iser_conn_start(struct iscsi_cls_conn *cls_conn)
396{
397 struct iscsi_conn *iscsi_conn;
5716af6e 398 struct iser_conn *iser_conn;
b73c3ada
AN
399
400 iscsi_conn = cls_conn->dd_data;
5716af6e
SG
401 iser_conn = iscsi_conn->dd_data;
402 reinit_completion(&iser_conn->stop_completion);
b73c3ada
AN
403
404 return iscsi_conn_start(cls_conn);
405}
406
b40977d9
MC
407static void
408iscsi_iser_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
409{
410 struct iscsi_conn *conn = cls_conn->dd_data;
5716af6e 411 struct iser_conn *iser_conn = conn->dd_data;
65e7ae7b 412
bba0a3c9 413 iser_info("stopping iscsi_conn: %p, iser_conn: %p\n", conn, iser_conn);
b73c3ada 414
b40977d9 415 /*
913e5bf4
MC
416 * Userspace may have goofed up and not bound the connection or
417 * might have only partially setup the connection.
b40977d9 418 */
5716af6e 419 if (iser_conn) {
ec370e2b 420 mutex_lock(&iser_conn->state_mutex);
3a940daf 421 iscsi_conn_stop(cls_conn, flag);
ec370e2b
AN
422 iser_conn_terminate(iser_conn);
423
424 /* unbind */
425 iser_conn->iscsi_conn = NULL;
b73c3ada 426 conn->dd_data = NULL;
ec370e2b 427
5716af6e 428 complete(&iser_conn->stop_completion);
ec370e2b 429 mutex_unlock(&iser_conn->state_mutex);
3a940daf
SG
430 } else {
431 iscsi_conn_stop(cls_conn, flag);
913e5bf4 432 }
65e7ae7b
OG
433}
434
75613521
MC
435static void iscsi_iser_session_destroy(struct iscsi_cls_session *cls_session)
436{
437 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
438
e5bd7b54 439 iscsi_session_teardown(cls_session);
a4804cd6
MC
440 iscsi_host_remove(shost);
441 iscsi_host_free(shost);
75613521 442}
65e7ae7b 443
6192d4e6
SG
444static inline unsigned int
445iser_dif_prot_caps(int prot_caps)
446{
447 return ((prot_caps & IB_PROT_T10DIF_TYPE_1) ? SHOST_DIF_TYPE1_PROTECTION |
448 SHOST_DIX_TYPE1_PROTECTION : 0) |
449 ((prot_caps & IB_PROT_T10DIF_TYPE_2) ? SHOST_DIF_TYPE2_PROTECTION |
450 SHOST_DIX_TYPE2_PROTECTION : 0) |
451 ((prot_caps & IB_PROT_T10DIF_TYPE_3) ? SHOST_DIF_TYPE3_PROTECTION |
452 SHOST_DIX_TYPE3_PROTECTION : 0);
453}
454
65e7ae7b 455static struct iscsi_cls_session *
412eeafa 456iscsi_iser_session_create(struct iscsi_endpoint *ep,
75613521 457 uint16_t cmds_max, uint16_t qdepth,
5e7facb7 458 uint32_t initial_cmdsn)
65e7ae7b
OG
459{
460 struct iscsi_cls_session *cls_session;
461 struct iscsi_session *session;
412eeafa 462 struct Scsi_Host *shost;
5716af6e 463 struct iser_conn *iser_conn = NULL;
a4ee3539 464 struct ib_conn *ib_conn;
75613521 465
962b4b52 466 shost = iscsi_host_alloc(&iscsi_iser_sht, 0, 0);
75613521
MC
467 if (!shost)
468 return NULL;
469 shost->transportt = iscsi_iser_scsi_transport;
b7f04513 470 shost->cmd_per_lun = qdepth;
75613521
MC
471 shost->max_lun = iscsi_max_lun;
472 shost->max_id = 0;
473 shost->max_channel = 0;
474 shost->max_cmd_len = 16;
475
412eeafa
MC
476 /*
477 * older userspace tools (before 2.0-870) did not pass us
478 * the leading conn's ep so this will be NULL;
479 */
6192d4e6 480 if (ep) {
5716af6e 481 iser_conn = ep->dd_data;
a4ee3539
SG
482 ib_conn = &iser_conn->ib_conn;
483 if (ib_conn->pi_support) {
484 u32 sig_caps = ib_conn->device->dev_attr.sig_prot_cap;
6192d4e6
SG
485
486 scsi_host_set_prot(shost, iser_dif_prot_caps(sig_caps));
487 if (iser_pi_guard)
488 scsi_host_set_guard(shost, SHOST_DIX_GUARD_IP);
489 else
490 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
491 }
492 }
412eeafa 493
5716af6e 494 if (iscsi_host_add(shost, ep ?
a4ee3539 495 ib_conn->device->ib_device->dma_device : NULL))
75613521 496 goto free_host;
65e7ae7b 497
b7f04513
SP
498 if (cmds_max > ISER_DEF_XMIT_CMDS_MAX) {
499 iser_info("cmds_max changed from %u to %u\n",
500 cmds_max, ISER_DEF_XMIT_CMDS_MAX);
501 cmds_max = ISER_DEF_XMIT_CMDS_MAX;
502 }
503
75613521 504 cls_session = iscsi_session_setup(&iscsi_iser_transport, shost,
b7f04513 505 cmds_max, 0,
2261ec3d 506 sizeof(struct iscsi_iser_task),
7970634b 507 initial_cmdsn, 0);
65e7ae7b 508 if (!cls_session)
75613521
MC
509 goto remove_host;
510 session = cls_session->dd_data;
65e7ae7b 511
2747fdb2 512 shost->can_queue = session->scsi_cmds_max;
65e7ae7b 513 return cls_session;
75613521
MC
514
515remove_host:
a4804cd6 516 iscsi_host_remove(shost);
75613521 517free_host:
a4804cd6 518 iscsi_host_free(shost);
75613521 519 return NULL;
65e7ae7b
OG
520}
521
522static int
358ff019
MC
523iscsi_iser_set_param(struct iscsi_cls_conn *cls_conn,
524 enum iscsi_param param, char *buf, int buflen)
65e7ae7b 525{
358ff019 526 int value;
65e7ae7b
OG
527
528 switch (param) {
529 case ISCSI_PARAM_MAX_RECV_DLENGTH:
530 /* TBD */
531 break;
65e7ae7b 532 case ISCSI_PARAM_HDRDGST_EN:
358ff019 533 sscanf(buf, "%d", &value);
65e7ae7b 534 if (value) {
e7eeffa4 535 iser_err("DataDigest wasn't negotiated to None\n");
65e7ae7b
OG
536 return -EPROTO;
537 }
538 break;
539 case ISCSI_PARAM_DATADGST_EN:
358ff019 540 sscanf(buf, "%d", &value);
65e7ae7b 541 if (value) {
e7eeffa4 542 iser_err("DataDigest wasn't negotiated to None\n");
65e7ae7b
OG
543 return -EPROTO;
544 }
545 break;
65e7ae7b 546 case ISCSI_PARAM_IFMARKER_EN:
358ff019 547 sscanf(buf, "%d", &value);
65e7ae7b 548 if (value) {
e7eeffa4 549 iser_err("IFMarker wasn't negotiated to No\n");
65e7ae7b
OG
550 return -EPROTO;
551 }
552 break;
553 case ISCSI_PARAM_OFMARKER_EN:
358ff019 554 sscanf(buf, "%d", &value);
65e7ae7b 555 if (value) {
e7eeffa4 556 iser_err("OFMarker wasn't negotiated to No\n");
65e7ae7b
OG
557 return -EPROTO;
558 }
559 break;
560 default:
358ff019 561 return iscsi_set_param(cls_conn, param, buf, buflen);
65e7ae7b
OG
562 }
563
564 return 0;
565}
566
65e7ae7b
OG
567static void
568iscsi_iser_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
569{
570 struct iscsi_conn *conn = cls_conn->dd_data;
571
572 stats->txdata_octets = conn->txdata_octets;
573 stats->rxdata_octets = conn->rxdata_octets;
574 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
575 stats->dataout_pdus = conn->dataout_pdus_cnt;
576 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
577 stats->datain_pdus = conn->datain_pdus_cnt; /* always 0 */
578 stats->r2t_pdus = conn->r2t_pdus_cnt; /* always 0 */
579 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
580 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
87528227 581 stats->custom_length = 4;
65e7ae7b
OG
582 strcpy(stats->custom[0].desc, "qp_tx_queue_full");
583 stats->custom[0].value = 0; /* TB iser_conn->qp_tx_queue_full; */
584 strcpy(stats->custom[1].desc, "fmr_map_not_avail");
585 stats->custom[1].value = 0; /* TB iser_conn->fmr_map_not_avail */;
586 strcpy(stats->custom[2].desc, "eh_abort_cnt");
587 stats->custom[2].value = conn->eh_abort_cnt;
87528227
ED
588 strcpy(stats->custom[3].desc, "fmr_unalign_cnt");
589 stats->custom[3].value = conn->fmr_unalign_cnt;
65e7ae7b
OG
590}
591
7c53c6f8
MC
592static int iscsi_iser_get_ep_param(struct iscsi_endpoint *ep,
593 enum iscsi_param param, char *buf)
594{
5716af6e 595 struct iser_conn *iser_conn = ep->dd_data;
7c53c6f8
MC
596 int len;
597
598 switch (param) {
599 case ISCSI_PARAM_CONN_PORT:
600 case ISCSI_PARAM_CONN_ADDRESS:
a4ee3539 601 if (!iser_conn || !iser_conn->ib_conn.cma_id)
7c53c6f8
MC
602 return -ENOTCONN;
603
604 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
a4ee3539
SG
605 &iser_conn->ib_conn.cma_id->route.addr.dst_addr,
606 param, buf);
7c53c6f8
MC
607 break;
608 default:
609 return -ENOSYS;
610 }
611
612 return len;
613}
614
412eeafa 615static struct iscsi_endpoint *
10eb0f01
MC
616iscsi_iser_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
617 int non_blocking)
65e7ae7b
OG
618{
619 int err;
5716af6e 620 struct iser_conn *iser_conn;
412eeafa 621 struct iscsi_endpoint *ep;
65e7ae7b 622
0a690758 623 ep = iscsi_create_endpoint(0);
412eeafa
MC
624 if (!ep)
625 return ERR_PTR(-ENOMEM);
65e7ae7b 626
5716af6e
SG
627 iser_conn = kzalloc(sizeof(*iser_conn), GFP_KERNEL);
628 if (!iser_conn) {
0a690758
AN
629 err = -ENOMEM;
630 goto failure;
631 }
632
5716af6e
SG
633 ep->dd_data = iser_conn;
634 iser_conn->ep = ep;
635 iser_conn_init(iser_conn);
65e7ae7b 636
5716af6e 637 err = iser_connect(iser_conn, NULL, dst_addr, non_blocking);
7d9c0de4 638 if (err)
0a690758 639 goto failure;
7d9c0de4 640
412eeafa 641 return ep;
0a690758
AN
642failure:
643 iscsi_destroy_endpoint(ep);
644 return ERR_PTR(err);
65e7ae7b
OG
645}
646
647static int
412eeafa 648iscsi_iser_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
65e7ae7b 649{
5716af6e 650 struct iser_conn *iser_conn;
65e7ae7b
OG
651 int rc;
652
5716af6e
SG
653 iser_conn = ep->dd_data;
654 rc = wait_for_completion_interruptible_timeout(&iser_conn->up_completion,
9a6d3234 655 msecs_to_jiffies(timeout_ms));
65e7ae7b 656 /* if conn establishment failed, return error code to iscsi */
504130c0 657 if (rc == 0) {
5716af6e
SG
658 mutex_lock(&iser_conn->state_mutex);
659 if (iser_conn->state == ISER_CONN_TERMINATING ||
660 iser_conn->state == ISER_CONN_DOWN)
504130c0 661 rc = -1;
5716af6e 662 mutex_unlock(&iser_conn->state_mutex);
504130c0 663 }
65e7ae7b 664
5716af6e 665 iser_info("ib conn %p rc = %d\n", iser_conn, rc);
65e7ae7b
OG
666
667 if (rc > 0)
668 return 1; /* success, this is the equivalent of POLLOUT */
669 else if (!rc)
670 return 0; /* timeout */
671 else
672 return rc; /* signal */
673}
674
675static void
412eeafa 676iscsi_iser_ep_disconnect(struct iscsi_endpoint *ep)
65e7ae7b 677{
5716af6e 678 struct iser_conn *iser_conn;
65e7ae7b 679
5716af6e
SG
680 iser_conn = ep->dd_data;
681 iser_info("ep %p iser conn %p state %d\n",
682 ep, iser_conn, iser_conn->state);
683
684 mutex_lock(&iser_conn->state_mutex);
685 iser_conn_terminate(iser_conn);
b73c3ada
AN
686
687 /*
9a6d3234
AN
688 * if iser_conn and iscsi_conn are bound, we must wait for
689 * iscsi_conn_stop and flush errors completion before freeing
690 * the iser resources. Otherwise we are safe to free resources
691 * immediately.
b73c3ada 692 */
5716af6e
SG
693 if (iser_conn->iscsi_conn) {
694 INIT_WORK(&iser_conn->release_work, iser_release_work);
695 queue_work(release_wq, &iser_conn->release_work);
696 mutex_unlock(&iser_conn->state_mutex);
b73c3ada 697 } else {
5716af6e
SG
698 iser_conn->state = ISER_CONN_DOWN;
699 mutex_unlock(&iser_conn->state_mutex);
700 iser_conn_release(iser_conn);
b73c3ada 701 }
0a690758 702 iscsi_destroy_endpoint(ep);
65e7ae7b
OG
703}
704
587a1f16 705static umode_t iser_attr_is_visible(int param_type, int param)
3128c6c7
MC
706{
707 switch (param_type) {
f27fb2ef
MC
708 case ISCSI_HOST_PARAM:
709 switch (param) {
710 case ISCSI_HOST_PARAM_NETDEV_NAME:
711 case ISCSI_HOST_PARAM_HWADDRESS:
712 case ISCSI_HOST_PARAM_INITIATOR_NAME:
713 return S_IRUGO;
714 default:
715 return 0;
716 }
3128c6c7
MC
717 case ISCSI_PARAM:
718 switch (param) {
719 case ISCSI_PARAM_MAX_RECV_DLENGTH:
720 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
721 case ISCSI_PARAM_HDRDGST_EN:
722 case ISCSI_PARAM_DATADGST_EN:
723 case ISCSI_PARAM_CONN_ADDRESS:
724 case ISCSI_PARAM_CONN_PORT:
725 case ISCSI_PARAM_EXP_STATSN:
726 case ISCSI_PARAM_PERSISTENT_ADDRESS:
727 case ISCSI_PARAM_PERSISTENT_PORT:
728 case ISCSI_PARAM_PING_TMO:
729 case ISCSI_PARAM_RECV_TMO:
1d063c17
MC
730 case ISCSI_PARAM_INITIAL_R2T_EN:
731 case ISCSI_PARAM_MAX_R2T:
732 case ISCSI_PARAM_IMM_DATA_EN:
733 case ISCSI_PARAM_FIRST_BURST:
734 case ISCSI_PARAM_MAX_BURST:
735 case ISCSI_PARAM_PDU_INORDER_EN:
736 case ISCSI_PARAM_DATASEQ_INORDER_EN:
737 case ISCSI_PARAM_TARGET_NAME:
738 case ISCSI_PARAM_TPGT:
739 case ISCSI_PARAM_USERNAME:
740 case ISCSI_PARAM_PASSWORD:
741 case ISCSI_PARAM_USERNAME_IN:
742 case ISCSI_PARAM_PASSWORD_IN:
743 case ISCSI_PARAM_FAST_ABORT:
744 case ISCSI_PARAM_ABORT_TMO:
745 case ISCSI_PARAM_LU_RESET_TMO:
746 case ISCSI_PARAM_TGT_RESET_TMO:
747 case ISCSI_PARAM_IFACE_NAME:
748 case ISCSI_PARAM_INITIATOR_NAME:
6a06a4b8 749 case ISCSI_PARAM_DISCOVERY_SESS:
3128c6c7
MC
750 return S_IRUGO;
751 default:
752 return 0;
753 }
754 }
755
756 return 0;
757}
758
65e7ae7b 759static struct scsi_host_template iscsi_iser_sht = {
7974392c 760 .module = THIS_MODULE,
c1d786e6 761 .name = "iSCSI Initiator over iSER",
65e7ae7b 762 .queuecommand = iscsi_queuecommand,
6410627e 763 .change_queue_depth = iscsi_change_queue_depth,
65e7ae7b 764 .sg_tablesize = ISCSI_ISER_SG_TABLESIZE,
8072ec2f 765 .max_sectors = 1024,
e28f3d5b 766 .cmd_per_lun = ISER_DEF_CMD_PER_LUN,
65e7ae7b 767 .eh_abort_handler = iscsi_eh_abort,
90c18f3c 768 .eh_device_reset_handler= iscsi_eh_device_reset,
309ce156 769 .eh_target_reset_handler = iscsi_eh_recover_target,
6b5d6c44 770 .target_alloc = iscsi_target_alloc,
65e7ae7b
OG
771 .use_clustering = DISABLE_CLUSTERING,
772 .proc_name = "iscsi_iser",
773 .this_id = -1,
774};
775
776static struct iscsi_transport iscsi_iser_transport = {
777 .owner = THIS_MODULE,
778 .name = "iser",
6a06a4b8 779 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_TEXT_NEGO,
65e7ae7b
OG
780 /* session management */
781 .create_session = iscsi_iser_session_create,
75613521 782 .destroy_session = iscsi_iser_session_destroy,
65e7ae7b
OG
783 /* connection management */
784 .create_conn = iscsi_iser_conn_create,
785 .bind_conn = iscsi_iser_conn_bind,
b73c3ada 786 .destroy_conn = iscsi_conn_teardown,
3128c6c7 787 .attr_is_visible = iser_attr_is_visible,
358ff019
MC
788 .set_param = iscsi_iser_set_param,
789 .get_conn_param = iscsi_conn_get_param,
7c53c6f8 790 .get_ep_param = iscsi_iser_get_ep_param,
358ff019 791 .get_session_param = iscsi_session_get_param,
b73c3ada 792 .start_conn = iscsi_iser_conn_start,
b40977d9 793 .stop_conn = iscsi_iser_conn_stop,
0801c242
MC
794 /* iscsi host params */
795 .get_host_param = iscsi_host_get_param,
796 .set_host_param = iscsi_host_set_param,
65e7ae7b
OG
797 /* IO */
798 .send_pdu = iscsi_conn_send_pdu,
799 .get_stats = iscsi_iser_conn_get_stats,
2747fdb2
MC
800 .init_task = iscsi_iser_task_init,
801 .xmit_task = iscsi_iser_task_xmit,
802 .cleanup_task = iscsi_iser_cleanup_task,
0f9c7449 803 .alloc_pdu = iscsi_iser_pdu_alloc,
0a7a08ad 804 .check_protection = iscsi_iser_check_protection,
65e7ae7b
OG
805 /* recovery */
806 .session_recovery_timedout = iscsi_session_recovery_timedout,
807
808 .ep_connect = iscsi_iser_ep_connect,
809 .ep_poll = iscsi_iser_ep_poll,
810 .ep_disconnect = iscsi_iser_ep_disconnect
811};
812
813static int __init iser_init(void)
814{
815 int err;
816
817 iser_dbg("Starting iSER datamover...\n");
818
819 if (iscsi_max_lun < 1) {
4f363882 820 iser_err("Invalid max_lun value of %u\n", iscsi_max_lun);
65e7ae7b
OG
821 return -EINVAL;
822 }
823
65e7ae7b
OG
824 memset(&ig, 0, sizeof(struct iser_global));
825
826 ig.desc_cache = kmem_cache_create("iser_descriptors",
f19624aa 827 sizeof(struct iser_tx_desc),
65e7ae7b 828 0, SLAB_HWCACHE_ALIGN,
20c2df83 829 NULL);
65e7ae7b
OG
830 if (ig.desc_cache == NULL)
831 return -ENOMEM;
832
833 /* device init is called only after the first addr resolution */
834 mutex_init(&ig.device_list_mutex);
835 INIT_LIST_HEAD(&ig.device_list);
836 mutex_init(&ig.connlist_mutex);
837 INIT_LIST_HEAD(&ig.connlist);
838
b73c3ada
AN
839 release_wq = alloc_workqueue("release workqueue", 0, 0);
840 if (!release_wq) {
841 iser_err("failed to allocate release workqueue\n");
842 return -ENOMEM;
843 }
844
75613521
MC
845 iscsi_iser_scsi_transport = iscsi_register_transport(
846 &iscsi_iser_transport);
847 if (!iscsi_iser_scsi_transport) {
65e7ae7b
OG
848 iser_err("iscsi_register_transport failed\n");
849 err = -EINVAL;
850 goto register_transport_failure;
851 }
852
853 return 0;
854
855register_transport_failure:
856 kmem_cache_destroy(ig.desc_cache);
857
858 return err;
859}
860
861static void __exit iser_exit(void)
862{
5716af6e 863 struct iser_conn *iser_conn, *n;
b73c3ada
AN
864 int connlist_empty;
865
65e7ae7b 866 iser_dbg("Removing iSER datamover...\n");
b73c3ada
AN
867 destroy_workqueue(release_wq);
868
869 mutex_lock(&ig.connlist_mutex);
870 connlist_empty = list_empty(&ig.connlist);
871 mutex_unlock(&ig.connlist_mutex);
872
873 if (!connlist_empty) {
874 iser_err("Error cleanup stage completed but we still have iser "
875 "connections, destroying them anyway.\n");
5716af6e
SG
876 list_for_each_entry_safe(iser_conn, n, &ig.connlist,
877 conn_list) {
878 iser_conn_release(iser_conn);
b73c3ada
AN
879 }
880 }
881
65e7ae7b
OG
882 iscsi_unregister_transport(&iscsi_iser_transport);
883 kmem_cache_destroy(ig.desc_cache);
884}
885
886module_init(iser_init);
887module_exit(iser_exit);