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