File Index
Files and directories
Sign in
FILE

iscsi_iser.c

File preview with syntax highlighting.

linux-6.19.14/drivers/infiniband/ulp/iser/iscsi_iser.c C
   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 * Copyright (c) 2013-2014 Mellanox Technologies. All rights reserved.
   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
  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>
  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>
  60#include <linux/slab.h>
  61#include <linux/module.h>
  62
  63#include <net/sock.h>
  64
  65#include <linux/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
  77MODULE_DESCRIPTION("iSER (iSCSI Extensions for RDMA) Datamover");
  78MODULE_LICENSE("Dual BSD/GPL");
  79MODULE_AUTHOR("Alex Nezhinsky, Dan Bar Dov, Or Gerlitz");
  80
  81static const struct scsi_host_template iscsi_iser_sht;
  82static struct iscsi_transport iscsi_iser_transport;
  83static struct scsi_transport_template *iscsi_iser_scsi_transport;
  84static struct workqueue_struct *release_wq;
  85static DEFINE_MUTEX(unbind_iser_conn_mutex);
  86struct iser_global ig;
  87
  88int iser_debug_level = 0;
  89module_param_named(debug_level, iser_debug_level, int, S_IRUGO | S_IWUSR);
  90MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0 (default:disabled)");
  91
  92static int iscsi_iser_set(const char *val, const struct kernel_param *kp);
  93static const struct kernel_param_ops iscsi_iser_size_ops = {
  94	.set = iscsi_iser_set,
  95	.get = param_get_uint,
  96};
  97
  98static unsigned int iscsi_max_lun = 512;
  99module_param_cb(max_lun, &iscsi_iser_size_ops, &iscsi_max_lun, S_IRUGO);
 100MODULE_PARM_DESC(max_lun, "Max LUNs to allow per session, should > 0 (default:512)");
 101
 102unsigned int iser_max_sectors = ISER_DEF_MAX_SECTORS;
 103module_param_cb(max_sectors, &iscsi_iser_size_ops, &iser_max_sectors,
 104		S_IRUGO | S_IWUSR);
 105MODULE_PARM_DESC(max_sectors, "Max number of sectors in a single scsi command, should > 0 (default:1024)");
 106
 107bool iser_always_reg = true;
 108module_param_named(always_register, iser_always_reg, bool, S_IRUGO);
 109MODULE_PARM_DESC(always_register,
 110		 "Always register memory, even for continuous memory regions (default:true)");
 111
 112bool iser_pi_enable = false;
 113module_param_named(pi_enable, iser_pi_enable, bool, S_IRUGO);
 114MODULE_PARM_DESC(pi_enable, "Enable T10-PI offload support (default:disabled)");
 115
 116static int iscsi_iser_set(const char *val, const struct kernel_param *kp)
 117{
 118	int ret;
 119	unsigned int n = 0;
 120
 121	ret = kstrtouint(val, 10, &n);
 122	if (ret != 0 || n == 0)
 123		return -EINVAL;
 124
 125	return param_set_uint(val, kp);
 126}
 127
 128/*
 129 * iscsi_iser_recv() - Process a successful recv completion
 130 * @conn:         iscsi connection
 131 * @hdr:          iscsi header
 132 * @rx_data:      buffer containing receive data payload
 133 * @rx_data_len:  length of rx_data
 134 *
 135 * Notes: In case of data length errors or iscsi PDU completion failures
 136 *        this routine will signal iscsi layer of connection failure.
 137 */
 138void iscsi_iser_recv(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
 139		     char *rx_data, int rx_data_len)
 140{
 141	int rc = 0;
 142	int datalen;
 143
 144	/* verify PDU length */
 145	datalen = ntoh24(hdr->dlength);
 146	if (datalen > rx_data_len || (datalen + 4) < rx_data_len) {
 147		iser_err("wrong datalen %d (hdr), %d (IB)\n",
 148			datalen, rx_data_len);
 149		rc = ISCSI_ERR_DATALEN;
 150		goto error;
 151	}
 152
 153	if (datalen != rx_data_len)
 154		iser_dbg("aligned datalen (%d) hdr, %d (IB)\n",
 155			datalen, rx_data_len);
 156
 157	rc = iscsi_complete_pdu(conn, hdr, rx_data, rx_data_len);
 158	if (rc && rc != ISCSI_ERR_NO_SCSI_CMD)
 159		goto error;
 160
 161	return;
 162error:
 163	iscsi_conn_failure(conn, rc);
 164}
 165
 166/**
 167 * iscsi_iser_pdu_alloc() - allocate an iscsi-iser PDU
 168 * @task:     iscsi task
 169 * @opcode:   iscsi command opcode
 170 *
 171 * Netes: This routine can't fail, just assign iscsi task
 172 *        hdr and max hdr size.
 173 */
 174static int iscsi_iser_pdu_alloc(struct iscsi_task *task, uint8_t opcode)
 175{
 176	struct iscsi_iser_task *iser_task = task->dd_data;
 177
 178	task->hdr = (struct iscsi_hdr *)&iser_task->desc.iscsi_header;
 179	task->hdr_max = sizeof(iser_task->desc.iscsi_header);
 180
 181	return 0;
 182}
 183
 184/**
 185 * iser_initialize_task_headers() - Initialize task headers
 186 * @task:       iscsi task
 187 * @tx_desc:    iser tx descriptor
 188 *
 189 * Notes:
 190 * This routine may race with iser teardown flow for scsi
 191 * error handling TMFs. So for TMF we should acquire the
 192 * state mutex to avoid dereferencing the IB device which
 193 * may have already been terminated.
 194 */
 195int iser_initialize_task_headers(struct iscsi_task *task,
 196				 struct iser_tx_desc *tx_desc)
 197{
 198	struct iser_conn *iser_conn = task->conn->dd_data;
 199	struct iser_device *device = iser_conn->ib_conn.device;
 200	struct iscsi_iser_task *iser_task = task->dd_data;
 201	u64 dma_addr;
 202
 203	if (unlikely(iser_conn->state != ISER_CONN_UP))
 204		return -ENODEV;
 205
 206	dma_addr = ib_dma_map_single(device->ib_device, (void *)tx_desc,
 207				ISER_HEADERS_LEN, DMA_TO_DEVICE);
 208	if (ib_dma_mapping_error(device->ib_device, dma_addr))
 209		return -ENOMEM;
 210
 211	tx_desc->inv_wr.next = NULL;
 212	tx_desc->reg_wr.wr.next = NULL;
 213	tx_desc->mapped = true;
 214	tx_desc->dma_addr = dma_addr;
 215	tx_desc->tx_sg[0].addr   = tx_desc->dma_addr;
 216	tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
 217	tx_desc->tx_sg[0].lkey   = device->pd->local_dma_lkey;
 218
 219	iser_task->iser_conn = iser_conn;
 220
 221	return 0;
 222}
 223
 224/**
 225 * iscsi_iser_task_init() - Initialize iscsi-iser task
 226 * @task: iscsi task
 227 *
 228 * Initialize the task for the scsi command or mgmt command.
 229 *
 230 * Return: Returns zero on success or -ENOMEM when failing
 231 *         to init task headers (dma mapping error).
 232 */
 233static int iscsi_iser_task_init(struct iscsi_task *task)
 234{
 235	struct iscsi_iser_task *iser_task = task->dd_data;
 236	int ret;
 237
 238	ret = iser_initialize_task_headers(task, &iser_task->desc);
 239	if (ret) {
 240		iser_err("Failed to init task %p, err = %d\n",
 241			 iser_task, ret);
 242		return ret;
 243	}
 244
 245	/* mgmt task */
 246	if (!task->sc)
 247		return 0;
 248
 249	iser_task->command_sent = 0;
 250	iser_task_rdma_init(iser_task);
 251	iser_task->sc = task->sc;
 252
 253	return 0;
 254}
 255
 256/**
 257 * iscsi_iser_mtask_xmit() - xmit management (immediate) task
 258 * @conn: iscsi connection
 259 * @task: task management task
 260 *
 261 * Notes:
 262 *	The function can return -EAGAIN in which case caller must
 263 *	call it again later, or recover. '0' return code means successful
 264 *	xmit.
 265 *
 266 **/
 267static int iscsi_iser_mtask_xmit(struct iscsi_conn *conn,
 268				 struct iscsi_task *task)
 269{
 270	iser_dbg("mtask xmit [cid %d itt 0x%x]\n", conn->id, task->itt);
 271
 272	/* since iser xmits control with zero copy, tasks can not be recycled
 273	 * right after sending them.
 274	 * The recycling scheme is based on whether a response is expected
 275	 * - if yes, the task is recycled at iscsi_complete_pdu
 276	 * - if no,  the task is recycled at iser_snd_completion
 277	 */
 278	return iser_send_control(conn, task);
 279}
 280
 281static int iscsi_iser_task_xmit_unsol_data(struct iscsi_conn *conn,
 282					   struct iscsi_task *task)
 283{
 284	struct iscsi_r2t_info *r2t = &task->unsol_r2t;
 285	struct iscsi_data hdr;
 286	int error = 0;
 287
 288	/* Send data-out PDUs while there's still unsolicited data to send */
 289	while (iscsi_task_has_unsol_data(task)) {
 290		iscsi_prep_data_out_pdu(task, r2t, &hdr);
 291		iser_dbg("Sending data-out: itt 0x%x, data count %d\n",
 292			   hdr.itt, r2t->data_count);
 293
 294		/* the buffer description has been passed with the command */
 295		/* Send the command */
 296		error = iser_send_data_out(conn, task, &hdr);
 297		if (error) {
 298			r2t->datasn--;
 299			goto iscsi_iser_task_xmit_unsol_data_exit;
 300		}
 301		r2t->sent += r2t->data_count;
 302		iser_dbg("Need to send %d more as data-out PDUs\n",
 303			   r2t->data_length - r2t->sent);
 304	}
 305
 306iscsi_iser_task_xmit_unsol_data_exit:
 307	return error;
 308}
 309
 310/**
 311 * iscsi_iser_task_xmit() - xmit iscsi-iser task
 312 * @task: iscsi task
 313 *
 314 * Return: zero on success or escalates $error on failure.
 315 */
 316static int iscsi_iser_task_xmit(struct iscsi_task *task)
 317{
 318	struct iscsi_conn *conn = task->conn;
 319	struct iscsi_iser_task *iser_task = task->dd_data;
 320	int error = 0;
 321
 322	if (!task->sc)
 323		return iscsi_iser_mtask_xmit(conn, task);
 324
 325	if (task->sc->sc_data_direction == DMA_TO_DEVICE) {
 326		BUG_ON(scsi_bufflen(task->sc) == 0);
 327
 328		iser_dbg("cmd [itt %x total %d imm %d unsol_data %d\n",
 329			   task->itt, scsi_bufflen(task->sc),
 330			   task->imm_count, task->unsol_r2t.data_length);
 331	}
 332
 333	iser_dbg("ctask xmit [cid %d itt 0x%x]\n",
 334		   conn->id, task->itt);
 335
 336	/* Send the cmd PDU */
 337	if (!iser_task->command_sent) {
 338		error = iser_send_command(conn, task);
 339		if (error)
 340			goto iscsi_iser_task_xmit_exit;
 341		iser_task->command_sent = 1;
 342	}
 343
 344	/* Send unsolicited data-out PDU(s) if necessary */
 345	if (iscsi_task_has_unsol_data(task))
 346		error = iscsi_iser_task_xmit_unsol_data(conn, task);
 347
 348 iscsi_iser_task_xmit_exit:
 349	return error;
 350}
 351
 352/**
 353 * iscsi_iser_cleanup_task() - cleanup an iscsi-iser task
 354 * @task: iscsi task
 355 *
 356 * Notes: In case the RDMA device is already NULL (might have
 357 *        been removed in DEVICE_REMOVAL CM event it will bail-out
 358 *        without doing dma unmapping.
 359 */
 360static void iscsi_iser_cleanup_task(struct iscsi_task *task)
 361{
 362	struct iscsi_iser_task *iser_task = task->dd_data;
 363	struct iser_tx_desc *tx_desc = &iser_task->desc;
 364	struct iser_conn *iser_conn = task->conn->dd_data;
 365	struct iser_device *device = iser_conn->ib_conn.device;
 366
 367	/* DEVICE_REMOVAL event might have already released the device */
 368	if (!device)
 369		return;
 370
 371	if (likely(tx_desc->mapped)) {
 372		ib_dma_unmap_single(device->ib_device, tx_desc->dma_addr,
 373				    ISER_HEADERS_LEN, DMA_TO_DEVICE);
 374		tx_desc->mapped = false;
 375	}
 376
 377	/* mgmt tasks do not need special cleanup */
 378	if (!task->sc)
 379		return;
 380
 381	if (iser_task->status == ISER_TASK_STATUS_STARTED) {
 382		iser_task->status = ISER_TASK_STATUS_COMPLETED;
 383		iser_task_rdma_finalize(iser_task);
 384	}
 385}
 386
 387/**
 388 * iscsi_iser_check_protection() - check protection information status of task.
 389 * @task:     iscsi task
 390 * @sector:   error sector if exsists (output)
 391 *
 392 * Return: zero if no data-integrity errors have occurred
 393 *         0x1: data-integrity error occurred in the guard-block
 394 *         0x2: data-integrity error occurred in the reference tag
 395 *         0x3: data-integrity error occurred in the application tag
 396 *
 397 *         In addition the error sector is marked.
 398 */
 399static u8 iscsi_iser_check_protection(struct iscsi_task *task, sector_t *sector)
 400{
 401	struct iscsi_iser_task *iser_task = task->dd_data;
 402	enum iser_data_dir dir = iser_task->dir[ISER_DIR_IN] ?
 403					ISER_DIR_IN : ISER_DIR_OUT;
 404
 405	return iser_check_task_pi_status(iser_task, dir, sector);
 406}
 407
 408/**
 409 * iscsi_iser_conn_create() - create a new iscsi-iser connection
 410 * @cls_session: iscsi class connection
 411 * @conn_idx:    connection index within the session (for MCS)
 412 *
 413 * Return: iscsi_cls_conn when iscsi_conn_setup succeeds or NULL
 414 *         otherwise.
 415 */
 416static struct iscsi_cls_conn *
 417iscsi_iser_conn_create(struct iscsi_cls_session *cls_session,
 418		       uint32_t conn_idx)
 419{
 420	struct iscsi_conn *conn;
 421	struct iscsi_cls_conn *cls_conn;
 422
 423	cls_conn = iscsi_conn_setup(cls_session, 0, conn_idx);
 424	if (!cls_conn)
 425		return NULL;
 426	conn = cls_conn->dd_data;
 427
 428	/*
 429	 * due to issues with the login code re iser sematics
 430	 * this not set in iscsi_conn_setup - FIXME
 431	 */
 432	conn->max_recv_dlength = ISER_RECV_DATA_SEG_LEN;
 433
 434	return cls_conn;
 435}
 436
 437/**
 438 * iscsi_iser_conn_bind() - bind iscsi and iser connection structures
 439 * @cls_session:     iscsi class session
 440 * @cls_conn:        iscsi class connection
 441 * @transport_eph:   transport end-point handle
 442 * @is_leading:      indicate if this is the session leading connection (MCS)
 443 *
 444 * Return: zero on success, $error if iscsi_conn_bind fails and
 445 *         -EINVAL in case end-point doesn't exists anymore or iser connection
 446 *         state is not UP (teardown already started).
 447 */
 448static int iscsi_iser_conn_bind(struct iscsi_cls_session *cls_session,
 449				struct iscsi_cls_conn *cls_conn,
 450				uint64_t transport_eph, int is_leading)
 451{
 452	struct iscsi_conn *conn = cls_conn->dd_data;
 453	struct iser_conn *iser_conn;
 454	struct iscsi_endpoint *ep;
 455	int error;
 456
 457	error = iscsi_conn_bind(cls_session, cls_conn, is_leading);
 458	if (error)
 459		return error;
 460
 461	/* the transport ep handle comes from user space so it must be
 462	 * verified against the global ib connections list */
 463	ep = iscsi_lookup_endpoint(transport_eph);
 464	if (!ep) {
 465		iser_err("can't bind eph %llx\n",
 466			 (unsigned long long)transport_eph);
 467		return -EINVAL;
 468	}
 469	iser_conn = ep->dd_data;
 470
 471	mutex_lock(&iser_conn->state_mutex);
 472	if (iser_conn->state != ISER_CONN_UP) {
 473		error = -EINVAL;
 474		iser_err("iser_conn %p state is %d, teardown started\n",
 475			 iser_conn, iser_conn->state);
 476		goto out;
 477	}
 478
 479	error = iser_alloc_rx_descriptors(iser_conn, conn->session);
 480	if (error)
 481		goto out;
 482
 483	/* binds the iSER connection retrieved from the previously
 484	 * connected ep_handle to the iSCSI layer connection. exchanges
 485	 * connection pointers */
 486	iser_info("binding iscsi conn %p to iser_conn %p\n", conn, iser_conn);
 487
 488	conn->dd_data = iser_conn;
 489	iser_conn->iscsi_conn = conn;
 490
 491out:
 492	iscsi_put_endpoint(ep);
 493	mutex_unlock(&iser_conn->state_mutex);
 494	return error;
 495}
 496
 497/**
 498 * iscsi_iser_conn_start() - start iscsi-iser connection
 499 * @cls_conn: iscsi class connection
 500 *
 501 * Notes: Here iser intialize (or re-initialize) stop_completion as
 502 *        from this point iscsi must call conn_stop in session/connection
 503 *        teardown so iser transport must wait for it.
 504 */
 505static int iscsi_iser_conn_start(struct iscsi_cls_conn *cls_conn)
 506{
 507	struct iscsi_conn *iscsi_conn;
 508	struct iser_conn *iser_conn;
 509
 510	iscsi_conn = cls_conn->dd_data;
 511	iser_conn = iscsi_conn->dd_data;
 512	reinit_completion(&iser_conn->stop_completion);
 513
 514	return iscsi_conn_start(cls_conn);
 515}
 516
 517/**
 518 * iscsi_iser_conn_stop() - stop iscsi-iser connection
 519 * @cls_conn:  iscsi class connection
 520 * @flag:      indicate if recover or terminate (passed as is)
 521 *
 522 * Notes: Calling iscsi_conn_stop might theoretically race with
 523 *        DEVICE_REMOVAL event and dereference a previously freed RDMA device
 524 *        handle, so we call it under iser the state lock to protect against
 525 *        this kind of race.
 526 */
 527static void iscsi_iser_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
 528{
 529	struct iscsi_conn *conn = cls_conn->dd_data;
 530	struct iser_conn *iser_conn = conn->dd_data;
 531
 532	iser_info("stopping iscsi_conn: %p, iser_conn: %p\n", conn, iser_conn);
 533
 534	/*
 535	 * Userspace may have goofed up and not bound the connection or
 536	 * might have only partially setup the connection.
 537	 */
 538	if (iser_conn) {
 539		mutex_lock(&iser_conn->state_mutex);
 540		mutex_lock(&unbind_iser_conn_mutex);
 541		iser_conn_terminate(iser_conn);
 542		iscsi_conn_stop(cls_conn, flag);
 543
 544		/* unbind */
 545		iser_conn->iscsi_conn = NULL;
 546		conn->dd_data = NULL;
 547		mutex_unlock(&unbind_iser_conn_mutex);
 548
 549		complete(&iser_conn->stop_completion);
 550		mutex_unlock(&iser_conn->state_mutex);
 551	} else {
 552		iscsi_conn_stop(cls_conn, flag);
 553	}
 554}
 555
 556/**
 557 * iscsi_iser_session_destroy() - destroy iscsi-iser session
 558 * @cls_session: iscsi class session
 559 *
 560 * Removes and free iscsi host.
 561 */
 562static void iscsi_iser_session_destroy(struct iscsi_cls_session *cls_session)
 563{
 564	struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
 565
 566	iscsi_session_teardown(cls_session);
 567	iscsi_host_remove(shost, false);
 568	iscsi_host_free(shost);
 569}
 570
 571static inline unsigned int iser_dif_prot_caps(int prot_caps)
 572{
 573	int ret = 0;
 574
 575	if (prot_caps & IB_PROT_T10DIF_TYPE_1)
 576		ret |= SHOST_DIF_TYPE1_PROTECTION |
 577		       SHOST_DIX_TYPE0_PROTECTION |
 578		       SHOST_DIX_TYPE1_PROTECTION;
 579	if (prot_caps & IB_PROT_T10DIF_TYPE_2)
 580		ret |= SHOST_DIF_TYPE2_PROTECTION |
 581		       SHOST_DIX_TYPE2_PROTECTION;
 582	if (prot_caps & IB_PROT_T10DIF_TYPE_3)
 583		ret |= SHOST_DIF_TYPE3_PROTECTION |
 584		       SHOST_DIX_TYPE3_PROTECTION;
 585
 586	return ret;
 587}
 588
 589/**
 590 * iscsi_iser_session_create() - create an iscsi-iser session
 591 * @ep:             iscsi end-point handle
 592 * @cmds_max:       maximum commands in this session
 593 * @qdepth:         session command queue depth
 594 * @initial_cmdsn:  initiator command sequnce number
 595 *
 596 * Allocates and adds a scsi host, expose DIF supprot if
 597 * exists, and sets up an iscsi session.
 598 */
 599static struct iscsi_cls_session *
 600iscsi_iser_session_create(struct iscsi_endpoint *ep,
 601			  uint16_t cmds_max, uint16_t qdepth,
 602			  uint32_t initial_cmdsn)
 603{
 604	struct iscsi_cls_session *cls_session;
 605	struct Scsi_Host *shost;
 606	struct iser_conn *iser_conn = NULL;
 607	struct ib_conn *ib_conn;
 608	struct ib_device *ib_dev;
 609	u32 max_fr_sectors;
 610
 611	shost = iscsi_host_alloc(&iscsi_iser_sht, 0, 0);
 612	if (!shost)
 613		return NULL;
 614	shost->transportt = iscsi_iser_scsi_transport;
 615	shost->cmd_per_lun = qdepth;
 616	shost->max_lun = iscsi_max_lun;
 617	shost->max_id = 0;
 618	shost->max_channel = 0;
 619	shost->max_cmd_len = 16;
 620
 621	/*
 622	 * older userspace tools (before 2.0-870) did not pass us
 623	 * the leading conn's ep so this will be NULL;
 624	 */
 625	if (ep) {
 626		iser_conn = ep->dd_data;
 627		shost->sg_tablesize = iser_conn->scsi_sg_tablesize;
 628		shost->can_queue = min_t(u16, cmds_max, iser_conn->max_cmds);
 629
 630		mutex_lock(&iser_conn->state_mutex);
 631		if (iser_conn->state != ISER_CONN_UP) {
 632			iser_err("iser conn %p already started teardown\n",
 633				 iser_conn);
 634			mutex_unlock(&iser_conn->state_mutex);
 635			goto free_host;
 636		}
 637
 638		ib_conn = &iser_conn->ib_conn;
 639		ib_dev = ib_conn->device->ib_device;
 640		if (ib_conn->pi_support) {
 641			u32 sig_caps = ib_dev->attrs.sig_prot_cap;
 642
 643			shost->sg_prot_tablesize = shost->sg_tablesize;
 644			scsi_host_set_prot(shost, iser_dif_prot_caps(sig_caps));
 645			scsi_host_set_guard(shost, SHOST_DIX_GUARD_IP |
 646						   SHOST_DIX_GUARD_CRC);
 647		}
 648
 649		if (!(ib_dev->attrs.kernel_cap_flags & IBK_SG_GAPS_REG))
 650			shost->virt_boundary_mask = SZ_4K - 1;
 651
 652		if (iscsi_host_add(shost, ib_dev->dev.parent)) {
 653			mutex_unlock(&iser_conn->state_mutex);
 654			goto free_host;
 655		}
 656		mutex_unlock(&iser_conn->state_mutex);
 657	} else {
 658		shost->can_queue = min_t(u16, cmds_max, ISER_DEF_XMIT_CMDS_MAX);
 659		if (iscsi_host_add(shost, NULL))
 660			goto free_host;
 661	}
 662
 663	max_fr_sectors = (shost->sg_tablesize * PAGE_SIZE) >> 9;
 664	shost->max_sectors = min(iser_max_sectors, max_fr_sectors);
 665
 666	iser_dbg("iser_conn %p, sg_tablesize %u, max_sectors %u\n",
 667		 iser_conn, shost->sg_tablesize,
 668		 shost->max_sectors);
 669
 670	if (shost->max_sectors < iser_max_sectors)
 671		iser_warn("max_sectors was reduced from %u to %u\n",
 672			  iser_max_sectors, shost->max_sectors);
 673
 674	cls_session = iscsi_session_setup(&iscsi_iser_transport, shost,
 675					  shost->can_queue, 0,
 676					  sizeof(struct iscsi_iser_task),
 677					  initial_cmdsn, 0);
 678	if (!cls_session)
 679		goto remove_host;
 680
 681	return cls_session;
 682
 683remove_host:
 684	iscsi_host_remove(shost, false);
 685free_host:
 686	iscsi_host_free(shost);
 687	return NULL;
 688}
 689
 690static int iscsi_iser_set_param(struct iscsi_cls_conn *cls_conn,
 691				enum iscsi_param param, char *buf, int buflen)
 692{
 693	int value;
 694
 695	switch (param) {
 696	case ISCSI_PARAM_MAX_RECV_DLENGTH:
 697		/* TBD */
 698		break;
 699	case ISCSI_PARAM_HDRDGST_EN:
 700		sscanf(buf, "%d", &value);
 701		if (value) {
 702			iser_err("DataDigest wasn't negotiated to None\n");
 703			return -EPROTO;
 704		}
 705		break;
 706	case ISCSI_PARAM_DATADGST_EN:
 707		sscanf(buf, "%d", &value);
 708		if (value) {
 709			iser_err("DataDigest wasn't negotiated to None\n");
 710			return -EPROTO;
 711		}
 712		break;
 713	case ISCSI_PARAM_IFMARKER_EN:
 714		sscanf(buf, "%d", &value);
 715		if (value) {
 716			iser_err("IFMarker wasn't negotiated to No\n");
 717			return -EPROTO;
 718		}
 719		break;
 720	case ISCSI_PARAM_OFMARKER_EN:
 721		sscanf(buf, "%d", &value);
 722		if (value) {
 723			iser_err("OFMarker wasn't negotiated to No\n");
 724			return -EPROTO;
 725		}
 726		break;
 727	default:
 728		return iscsi_set_param(cls_conn, param, buf, buflen);
 729	}
 730
 731	return 0;
 732}
 733
 734/**
 735 * iscsi_iser_conn_get_stats() - get iscsi connection statistics
 736 * @cls_conn:    iscsi class connection
 737 * @stats:       iscsi stats to output
 738 *
 739 * Output connection statistics.
 740 */
 741static void iscsi_iser_conn_get_stats(struct iscsi_cls_conn *cls_conn,
 742				      struct iscsi_stats *stats)
 743{
 744	struct iscsi_conn *conn = cls_conn->dd_data;
 745
 746	stats->txdata_octets = conn->txdata_octets;
 747	stats->rxdata_octets = conn->rxdata_octets;
 748	stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
 749	stats->dataout_pdus = conn->dataout_pdus_cnt;
 750	stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
 751	stats->datain_pdus = conn->datain_pdus_cnt; /* always 0 */
 752	stats->r2t_pdus = conn->r2t_pdus_cnt; /* always 0 */
 753	stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
 754	stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
 755	stats->custom_length = 0;
 756}
 757
 758static int iscsi_iser_get_ep_param(struct iscsi_endpoint *ep,
 759				   enum iscsi_param param, char *buf)
 760{
 761	struct iser_conn *iser_conn = ep->dd_data;
 762
 763	switch (param) {
 764	case ISCSI_PARAM_CONN_PORT:
 765	case ISCSI_PARAM_CONN_ADDRESS:
 766		if (!iser_conn || !iser_conn->ib_conn.cma_id)
 767			return -ENOTCONN;
 768
 769		return iscsi_conn_get_addr_param((struct sockaddr_storage *)
 770				&iser_conn->ib_conn.cma_id->route.addr.dst_addr,
 771				param, buf);
 772	default:
 773		break;
 774	}
 775	return -ENOSYS;
 776}
 777
 778/**
 779 * iscsi_iser_ep_connect() - Initiate iSER connection establishment
 780 * @shost:          scsi_host
 781 * @dst_addr:       destination address
 782 * @non_blocking:   indicate if routine can block
 783 *
 784 * Allocate an iscsi endpoint, an iser_conn structure and bind them.
 785 * After that start RDMA connection establishment via rdma_cm. We
 786 * don't allocate iser_conn embedded in iscsi_endpoint since in teardown
 787 * the endpoint will be destroyed at ep_disconnect while iser_conn will
 788 * cleanup its resources asynchronuously.
 789 *
 790 * Return: iscsi_endpoint created by iscsi layer or ERR_PTR(error)
 791 *         if fails.
 792 */
 793static struct iscsi_endpoint *iscsi_iser_ep_connect(struct Scsi_Host *shost,
 794						    struct sockaddr *dst_addr,
 795						    int non_blocking)
 796{
 797	int err;
 798	struct iser_conn *iser_conn;
 799	struct iscsi_endpoint *ep;
 800
 801	ep = iscsi_create_endpoint(0);
 802	if (!ep)
 803		return ERR_PTR(-ENOMEM);
 804
 805	iser_conn = kzalloc(sizeof(*iser_conn), GFP_KERNEL);
 806	if (!iser_conn) {
 807		err = -ENOMEM;
 808		goto failure;
 809	}
 810
 811	ep->dd_data = iser_conn;
 812	iser_conn->ep = ep;
 813	iser_conn_init(iser_conn);
 814
 815	err = iser_connect(iser_conn, NULL, dst_addr, non_blocking);
 816	if (err)
 817		goto failure;
 818
 819	return ep;
 820failure:
 821	iscsi_destroy_endpoint(ep);
 822	return ERR_PTR(err);
 823}
 824
 825/**
 826 * iscsi_iser_ep_poll() - poll for iser connection establishment to complete
 827 * @ep:            iscsi endpoint (created at ep_connect)
 828 * @timeout_ms:    polling timeout allowed in ms.
 829 *
 830 * This routine boils down to waiting for up_completion signaling
 831 * that cma_id got CONNECTED event.
 832 *
 833 * Return: 1 if succeeded in connection establishment, 0 if timeout expired
 834 *         (libiscsi will retry will kick in) or -1 if interrupted by signal
 835 *         or more likely iser connection state transitioned to TEMINATING or
 836 *         DOWN during the wait period.
 837 */
 838static int iscsi_iser_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
 839{
 840	struct iser_conn *iser_conn = ep->dd_data;
 841	int rc;
 842
 843	rc = wait_for_completion_interruptible_timeout(&iser_conn->up_completion,
 844						       msecs_to_jiffies(timeout_ms));
 845	/* if conn establishment failed, return error code to iscsi */
 846	if (rc == 0) {
 847		mutex_lock(&iser_conn->state_mutex);
 848		if (iser_conn->state == ISER_CONN_TERMINATING ||
 849		    iser_conn->state == ISER_CONN_DOWN)
 850			rc = -1;
 851		mutex_unlock(&iser_conn->state_mutex);
 852	}
 853
 854	iser_info("iser conn %p rc = %d\n", iser_conn, rc);
 855
 856	if (rc > 0)
 857		return 1; /* success, this is the equivalent of EPOLLOUT */
 858	else if (!rc)
 859		return 0; /* timeout */
 860	else
 861		return rc; /* signal */
 862}
 863
 864/**
 865 * iscsi_iser_ep_disconnect() - Initiate connection teardown process
 866 * @ep:    iscsi endpoint handle
 867 *
 868 * This routine is not blocked by iser and RDMA termination process
 869 * completion as we queue a deffered work for iser/RDMA destruction
 870 * and cleanup or actually call it immediately in case we didn't pass
 871 * iscsi conn bind/start stage, thus it is safe.
 872 */
 873static void iscsi_iser_ep_disconnect(struct iscsi_endpoint *ep)
 874{
 875	struct iser_conn *iser_conn = ep->dd_data;
 876
 877	iser_info("ep %p iser conn %p\n", ep, iser_conn);
 878
 879	mutex_lock(&iser_conn->state_mutex);
 880	iser_conn_terminate(iser_conn);
 881
 882	/*
 883	 * if iser_conn and iscsi_conn are bound, we must wait for
 884	 * iscsi_conn_stop and flush errors completion before freeing
 885	 * the iser resources. Otherwise we are safe to free resources
 886	 * immediately.
 887	 */
 888	if (iser_conn->iscsi_conn) {
 889		INIT_WORK(&iser_conn->release_work, iser_release_work);
 890		queue_work(release_wq, &iser_conn->release_work);
 891		mutex_unlock(&iser_conn->state_mutex);
 892	} else {
 893		iser_conn->state = ISER_CONN_DOWN;
 894		mutex_unlock(&iser_conn->state_mutex);
 895		iser_conn_release(iser_conn);
 896	}
 897
 898	iscsi_destroy_endpoint(ep);
 899}
 900
 901static umode_t iser_attr_is_visible(int param_type, int param)
 902{
 903	switch (param_type) {
 904	case ISCSI_HOST_PARAM:
 905		switch (param) {
 906		case ISCSI_HOST_PARAM_NETDEV_NAME:
 907		case ISCSI_HOST_PARAM_HWADDRESS:
 908		case ISCSI_HOST_PARAM_INITIATOR_NAME:
 909			return S_IRUGO;
 910		default:
 911			return 0;
 912		}
 913	case ISCSI_PARAM:
 914		switch (param) {
 915		case ISCSI_PARAM_MAX_RECV_DLENGTH:
 916		case ISCSI_PARAM_MAX_XMIT_DLENGTH:
 917		case ISCSI_PARAM_HDRDGST_EN:
 918		case ISCSI_PARAM_DATADGST_EN:
 919		case ISCSI_PARAM_CONN_ADDRESS:
 920		case ISCSI_PARAM_CONN_PORT:
 921		case ISCSI_PARAM_EXP_STATSN:
 922		case ISCSI_PARAM_PERSISTENT_ADDRESS:
 923		case ISCSI_PARAM_PERSISTENT_PORT:
 924		case ISCSI_PARAM_PING_TMO:
 925		case ISCSI_PARAM_RECV_TMO:
 926		case ISCSI_PARAM_INITIAL_R2T_EN:
 927		case ISCSI_PARAM_MAX_R2T:
 928		case ISCSI_PARAM_IMM_DATA_EN:
 929		case ISCSI_PARAM_FIRST_BURST:
 930		case ISCSI_PARAM_MAX_BURST:
 931		case ISCSI_PARAM_PDU_INORDER_EN:
 932		case ISCSI_PARAM_DATASEQ_INORDER_EN:
 933		case ISCSI_PARAM_TARGET_NAME:
 934		case ISCSI_PARAM_TPGT:
 935		case ISCSI_PARAM_USERNAME:
 936		case ISCSI_PARAM_PASSWORD:
 937		case ISCSI_PARAM_USERNAME_IN:
 938		case ISCSI_PARAM_PASSWORD_IN:
 939		case ISCSI_PARAM_FAST_ABORT:
 940		case ISCSI_PARAM_ABORT_TMO:
 941		case ISCSI_PARAM_LU_RESET_TMO:
 942		case ISCSI_PARAM_TGT_RESET_TMO:
 943		case ISCSI_PARAM_IFACE_NAME:
 944		case ISCSI_PARAM_INITIATOR_NAME:
 945		case ISCSI_PARAM_DISCOVERY_SESS:
 946			return S_IRUGO;
 947		default:
 948			return 0;
 949		}
 950	}
 951
 952	return 0;
 953}
 954
 955static const struct scsi_host_template iscsi_iser_sht = {
 956	.module                 = THIS_MODULE,
 957	.name                   = "iSCSI Initiator over iSER",
 958	.queuecommand           = iscsi_queuecommand,
 959	.change_queue_depth	= scsi_change_queue_depth,
 960	.sg_tablesize           = ISCSI_ISER_DEF_SG_TABLESIZE,
 961	.cmd_per_lun            = ISER_DEF_CMD_PER_LUN,
 962	.eh_timed_out		= iscsi_eh_cmd_timed_out,
 963	.eh_abort_handler       = iscsi_eh_abort,
 964	.eh_device_reset_handler= iscsi_eh_device_reset,
 965	.eh_target_reset_handler = iscsi_eh_recover_target,
 966	.target_alloc		= iscsi_target_alloc,
 967	.proc_name              = "iscsi_iser",
 968	.this_id                = -1,
 969	.track_queue_depth	= 1,
 970	.cmd_size		= sizeof(struct iscsi_cmd),
 971};
 972
 973static struct iscsi_transport iscsi_iser_transport = {
 974	.owner                  = THIS_MODULE,
 975	.name                   = "iser",
 976	.caps                   = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_TEXT_NEGO,
 977	/* session management */
 978	.create_session         = iscsi_iser_session_create,
 979	.destroy_session        = iscsi_iser_session_destroy,
 980	/* connection management */
 981	.create_conn            = iscsi_iser_conn_create,
 982	.bind_conn              = iscsi_iser_conn_bind,
 983	.unbind_conn		= iscsi_conn_unbind,
 984	.destroy_conn           = iscsi_conn_teardown,
 985	.attr_is_visible	= iser_attr_is_visible,
 986	.set_param              = iscsi_iser_set_param,
 987	.get_conn_param		= iscsi_conn_get_param,
 988	.get_ep_param		= iscsi_iser_get_ep_param,
 989	.get_session_param	= iscsi_session_get_param,
 990	.start_conn             = iscsi_iser_conn_start,
 991	.stop_conn              = iscsi_iser_conn_stop,
 992	/* iscsi host params */
 993	.get_host_param		= iscsi_host_get_param,
 994	.set_host_param		= iscsi_host_set_param,
 995	/* IO */
 996	.send_pdu		= iscsi_conn_send_pdu,
 997	.get_stats		= iscsi_iser_conn_get_stats,
 998	.init_task		= iscsi_iser_task_init,
 999	.xmit_task		= iscsi_iser_task_xmit,
1000	.cleanup_task		= iscsi_iser_cleanup_task,
1001	.alloc_pdu		= iscsi_iser_pdu_alloc,
1002	.check_protection	= iscsi_iser_check_protection,
1003	/* recovery */
1004	.session_recovery_timedout = iscsi_session_recovery_timedout,
1005
1006	.ep_connect             = iscsi_iser_ep_connect,
1007	.ep_poll                = iscsi_iser_ep_poll,
1008	.ep_disconnect          = iscsi_iser_ep_disconnect
1009};
1010
1011static int __init iser_init(void)
1012{
1013	int err;
1014
1015	iser_dbg("Starting iSER datamover...\n");
1016
1017	memset(&ig, 0, sizeof(struct iser_global));
1018
1019	ig.desc_cache = kmem_cache_create("iser_descriptors",
1020					  sizeof(struct iser_tx_desc),
1021					  0, SLAB_HWCACHE_ALIGN,
1022					  NULL);
1023	if (ig.desc_cache == NULL)
1024		return -ENOMEM;
1025
1026	/* device init is called only after the first addr resolution */
1027	mutex_init(&ig.device_list_mutex);
1028	INIT_LIST_HEAD(&ig.device_list);
1029	mutex_init(&ig.connlist_mutex);
1030	INIT_LIST_HEAD(&ig.connlist);
1031
1032	release_wq = alloc_workqueue("release workqueue", WQ_PERCPU, 0);
1033	if (!release_wq) {
1034		iser_err("failed to allocate release workqueue\n");
1035		err = -ENOMEM;
1036		goto err_alloc_wq;
1037	}
1038
1039	iscsi_iser_scsi_transport = iscsi_register_transport(
1040							&iscsi_iser_transport);
1041	if (!iscsi_iser_scsi_transport) {
1042		iser_err("iscsi_register_transport failed\n");
1043		err = -EINVAL;
1044		goto err_reg;
1045	}
1046
1047	return 0;
1048
1049err_reg:
1050	destroy_workqueue(release_wq);
1051err_alloc_wq:
1052	kmem_cache_destroy(ig.desc_cache);
1053
1054	return err;
1055}
1056
1057static void __exit iser_exit(void)
1058{
1059	struct iser_conn *iser_conn, *n;
1060	int connlist_empty;
1061
1062	iser_dbg("Removing iSER datamover...\n");
1063	destroy_workqueue(release_wq);
1064
1065	mutex_lock(&ig.connlist_mutex);
1066	connlist_empty = list_empty(&ig.connlist);
1067	mutex_unlock(&ig.connlist_mutex);
1068
1069	if (!connlist_empty) {
1070		iser_err("Error cleanup stage completed but we still have iser "
1071			 "connections, destroying them anyway\n");
1072		list_for_each_entry_safe(iser_conn, n, &ig.connlist,
1073					 conn_list) {
1074			iser_conn_release(iser_conn);
1075		}
1076	}
1077
1078	iscsi_unregister_transport(&iscsi_iser_transport);
1079	kmem_cache_destroy(ig.desc_cache);
1080}
1081
1082module_init(iser_init);
1083module_exit(iser_exit);
~/ Read-only file preview.