1/* 2 * iSCSI Initiator over iSER Data-Path 3 * 4 * Copyright (C) 2004 Dmitry Yusupov 5 * Copyright (C) 2004 Alex Aizman 6 * Copyright (C) 2005 Mike Christie 7 * Copyright (c) 2005, 2006 Voltaire, Inc. All rights reserved. 8 * maintained by openib-general@openib.org 9 * 10 * This software is available to you under a choice of one of two 11 * licenses. You may choose to be licensed under the terms of the GNU 12 * General Public License (GPL) Version 2, available from the file 13 * COPYING in the main directory of this source tree, or the 14 * OpenIB.org BSD license below: 15 * 16 * Redistribution and use in source and binary forms, with or 17 * without modification, are permitted provided that the following 18 * conditions are met: 19 * 20 * - Redistributions of source code must retain the above 21 * copyright notice, this list of conditions and the following 22 * disclaimer. 23 * 24 * - Redistributions in binary form must reproduce the above 25 * copyright notice, this list of conditions and the following 26 * disclaimer in the documentation and/or other materials 27 * provided with the distribution. 28 * 29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 30 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 31 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 32 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 33 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 34 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 35 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 36 * SOFTWARE. 37 * 38 * Credits: 39 * Christoph Hellwig 40 * FUJITA Tomonori 41 * Arne Redlich 42 * Zhenyu Wang 43 * Modified by: 44 * Erez Zilber 45 * 46 * 47 * $Id: iscsi_iser.c,v 1.1.1.1 2007/08/03 18:52:32 Exp $ 48 */ 49 50#include <linux/types.h> 51#include <linux/list.h> 52#include <linux/hardirq.h> 53#include <linux/kfifo.h> 54#include <linux/blkdev.h> 55#include <linux/init.h> 56#include <linux/ioctl.h> 57#include <linux/cdev.h> 58#include <linux/in.h> 59#include <linux/net.h> 60#include <linux/scatterlist.h> 61#include <linux/delay.h> 62 63#include <net/sock.h> 64 65#include <asm/uaccess.h> 66 67#include <scsi/scsi_cmnd.h> 68#include <scsi/scsi_device.h> 69#include <scsi/scsi_eh.h> 70#include <scsi/scsi_tcq.h> 71#include <scsi/scsi_host.h> 72#include <scsi/scsi.h> 73#include <scsi/scsi_transport_iscsi.h> 74 75#include "iscsi_iser.h" 76 77static unsigned int iscsi_max_lun = 512; 78module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO); 79 80int iser_debug_level = 0; 81 82MODULE_DESCRIPTION("iSER (iSCSI Extensions for RDMA) Datamover " 83 "v" DRV_VER " (" DRV_DATE ")"); 84MODULE_LICENSE("Dual BSD/GPL"); 85MODULE_AUTHOR("Alex Nezhinsky, Dan Bar Dov, Or Gerlitz"); 86 87module_param_named(debug_level, iser_debug_level, int, 0644); 88MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0 (default:disabled)"); 89 90struct iser_global ig; 91 92void 93iscsi_iser_recv(struct iscsi_conn *conn, 94 struct iscsi_hdr *hdr, char *rx_data, int rx_data_len) 95{ 96 int rc = 0; 97 uint32_t ret_itt; 98 int datalen; 99 int ahslen; 100 101 /* verify PDU length */ 102 datalen = ntoh24(hdr->dlength); 103 if (datalen != rx_data_len) { 104 printk(KERN_ERR "iscsi_iser: datalen %d (hdr) != %d (IB) \n", 105 datalen, rx_data_len); 106 rc = ISCSI_ERR_DATALEN; 107 goto error; 108 } 109 110 /* read AHS */ 111 ahslen = hdr->hlength * 4; 112 113 /* verify itt (itt encoding: age+cid+itt) */ 114 rc = iscsi_verify_itt(conn, hdr, &ret_itt); 115 116 if (!rc) 117 rc = iscsi_complete_pdu(conn, hdr, rx_data, rx_data_len); 118 119 if (rc && rc != ISCSI_ERR_NO_SCSI_CMD) 120 goto error; 121 122 return; 123error: 124 iscsi_conn_failure(conn, rc); 125} 126 127 128/** 129 * iscsi_iser_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands 130 * 131 **/ 132static void 133iscsi_iser_cmd_init(struct iscsi_cmd_task *ctask) 134{ 135 struct iscsi_iser_conn *iser_conn = ctask->conn->dd_data; 136 struct iscsi_iser_cmd_task *iser_ctask = ctask->dd_data; 137 struct scsi_cmnd *sc = ctask->sc; 138 139 iser_ctask->command_sent = 0; 140 iser_ctask->iser_conn = iser_conn; 141 142 if (sc->sc_data_direction == DMA_TO_DEVICE) { 143 BUG_ON(ctask->total_length == 0); 144 145 debug_scsi("cmd [itt %x total %d imm %d unsol_data %d\n", 146 ctask->itt, ctask->total_length, ctask->imm_count, 147 ctask->unsol_count); 148 } 149 150 iser_ctask_rdma_init(iser_ctask); 151} 152 153/** 154 * iscsi_mtask_xmit - xmit management(immediate) task 155 * @conn: iscsi connection 156 * @mtask: task management task 157 * 158 * Notes: 159 * The function can return -EAGAIN in which case caller must 160 * call it again later, or recover. '0' return code means successful 161 * xmit. 162 * 163 **/ 164static int 165iscsi_iser_mtask_xmit(struct iscsi_conn *conn, 166 struct iscsi_mgmt_task *mtask) 167{ 168 int error = 0; 169 170 debug_scsi("mtask deq [cid %d itt 0x%x]\n", conn->id, mtask->itt); 171 172 error = iser_send_control(conn, mtask); 173 174 /* since iser xmits control with zero copy, mtasks can not be recycled 175 * right after sending them. 176 * The recycling scheme is based on whether a response is expected 177 * - if yes, the mtask is recycled at iscsi_complete_pdu 178 * - if no, the mtask is recycled at iser_snd_completion 179 */ 180 if (error && error != -ENOBUFS) 181 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); 182 183 return error; 184} 185 186static int 187iscsi_iser_ctask_xmit_unsol_data(struct iscsi_conn *conn, 188 struct iscsi_cmd_task *ctask) 189{ 190 struct iscsi_data hdr; 191 int error = 0; 192 193 /* Send data-out PDUs while there's still unsolicited data to send */ 194 while (ctask->unsol_count > 0) { 195 iscsi_prep_unsolicit_data_pdu(ctask, &hdr); 196 debug_scsi("Sending data-out: itt 0x%x, data count %d\n", 197 hdr.itt, ctask->data_count); 198 199 /* the buffer description has been passed with the command */ 200 /* Send the command */ 201 error = iser_send_data_out(conn, ctask, &hdr); 202 if (error) { 203 ctask->unsol_datasn--; 204 goto iscsi_iser_ctask_xmit_unsol_data_exit; 205 } 206 ctask->unsol_count -= ctask->data_count; 207 debug_scsi("Need to send %d more as data-out PDUs\n", 208 ctask->unsol_count); 209 } 210 211iscsi_iser_ctask_xmit_unsol_data_exit: 212 return error; 213} 214 215static int 216iscsi_iser_ctask_xmit(struct iscsi_conn *conn, 217 struct iscsi_cmd_task *ctask) 218{ 219 struct iscsi_iser_cmd_task *iser_ctask = ctask->dd_data; 220 int error = 0; 221 222 debug_scsi("ctask deq [cid %d itt 0x%x]\n", 223 conn->id, ctask->itt); 224 225 /* 226 * serialize with TMF AbortTask 227 */ 228 if (ctask->mtask) 229 return error; 230 231 /* Send the cmd PDU */ 232 if (!iser_ctask->command_sent) { 233 error = iser_send_command(conn, ctask); 234 if (error) 235 goto iscsi_iser_ctask_xmit_exit; 236 iser_ctask->command_sent = 1; 237 } 238 239 /* Send unsolicited data-out PDU(s) if necessary */ 240 if (ctask->unsol_count) 241 error = iscsi_iser_ctask_xmit_unsol_data(conn, ctask); 242 243 iscsi_iser_ctask_xmit_exit: 244 if (error && error != -ENOBUFS) 245 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); 246 return error; 247} 248 249static void 250iscsi_iser_cleanup_ctask(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) 251{ 252 struct iscsi_iser_cmd_task *iser_ctask = ctask->dd_data; 253 254 if (iser_ctask->status == ISER_TASK_STATUS_STARTED) { 255 iser_ctask->status = ISER_TASK_STATUS_COMPLETED; 256 iser_ctask_rdma_finalize(iser_ctask); 257 } 258} 259 260static struct iser_conn * 261iscsi_iser_ib_conn_lookup(__u64 ep_handle) 262{ 263 struct iser_conn *ib_conn; 264 struct iser_conn *uib_conn = (struct iser_conn *)(unsigned long)ep_handle; 265 266 mutex_lock(&ig.connlist_mutex); 267 list_for_each_entry(ib_conn, &ig.connlist, conn_list) { 268 if (ib_conn == uib_conn) { 269 mutex_unlock(&ig.connlist_mutex); 270 return ib_conn; 271 } 272 } 273 mutex_unlock(&ig.connlist_mutex); 274 iser_err("no conn exists for eph %llx\n",(unsigned long long)ep_handle); 275 return NULL; 276} 277 278static struct iscsi_cls_conn * 279iscsi_iser_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx) 280{ 281 struct iscsi_conn *conn; 282 struct iscsi_cls_conn *cls_conn; 283 struct iscsi_iser_conn *iser_conn; 284 285 cls_conn = iscsi_conn_setup(cls_session, conn_idx); 286 if (!cls_conn) 287 return NULL; 288 conn = cls_conn->dd_data; 289 290 conn->max_recv_dlength = 128; 291 292 iser_conn = kzalloc(sizeof(*iser_conn), GFP_KERNEL); 293 if (!iser_conn) 294 goto conn_alloc_fail; 295 296 /* currently this is the only field which need to be initiated */ 297 rwlock_init(&iser_conn->lock); 298 299 conn->dd_data = iser_conn; 300 iser_conn->iscsi_conn = conn; 301 302 return cls_conn; 303 304conn_alloc_fail: 305 iscsi_conn_teardown(cls_conn); 306 return NULL; 307} 308 309static void 310iscsi_iser_conn_destroy(struct iscsi_cls_conn *cls_conn) 311{ 312 struct iscsi_conn *conn = cls_conn->dd_data; 313 struct iscsi_iser_conn *iser_conn = conn->dd_data; 314 315 iscsi_conn_teardown(cls_conn); 316 if (iser_conn->ib_conn) 317 iser_conn->ib_conn->iser_conn = NULL; 318 kfree(iser_conn); 319} 320 321static int 322iscsi_iser_conn_bind(struct iscsi_cls_session *cls_session, 323 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph, 324 int is_leading) 325{ 326 struct iscsi_conn *conn = cls_conn->dd_data; 327 struct iscsi_iser_conn *iser_conn; 328 struct iser_conn *ib_conn; 329 int error; 330 331 error = iscsi_conn_bind(cls_session, cls_conn, is_leading); 332 if (error) 333 return error; 334 335 /* the transport ep handle comes from user space so it must be 336 * verified against the global ib connections list */ 337 ib_conn = iscsi_iser_ib_conn_lookup(transport_eph); 338 if (!ib_conn) { 339 iser_err("can't bind eph %llx\n", 340 (unsigned long long)transport_eph); 341 return -EINVAL; 342 } 343 /* binds the iSER connection retrieved from the previously 344 * connected ep_handle to the iSCSI layer connection. exchanges 345 * connection pointers */ 346 iser_err("binding iscsi conn %p to iser_conn %p\n",conn,ib_conn); 347 iser_conn = conn->dd_data; 348 ib_conn->iser_conn = iser_conn; 349 iser_conn->ib_conn = ib_conn; 350 351 conn->recv_lock = &iser_conn->lock; 352 353 return 0; 354} 355 356static int 357iscsi_iser_conn_start(struct iscsi_cls_conn *cls_conn) 358{ 359 struct iscsi_conn *conn = cls_conn->dd_data; 360 int err; 361 362 err = iser_conn_set_full_featured_mode(conn); 363 if (err) 364 return err; 365 366 return iscsi_conn_start(cls_conn); 367} 368 369static struct iscsi_transport iscsi_iser_transport; 370 371static struct iscsi_cls_session * 372iscsi_iser_session_create(struct iscsi_transport *iscsit, 373 struct scsi_transport_template *scsit, 374 uint32_t initial_cmdsn, uint32_t *hostno) 375{ 376 struct iscsi_cls_session *cls_session; 377 struct iscsi_session *session; 378 int i; 379 uint32_t hn; 380 struct iscsi_cmd_task *ctask; 381 struct iscsi_mgmt_task *mtask; 382 struct iscsi_iser_cmd_task *iser_ctask; 383 struct iser_desc *desc; 384 385 cls_session = iscsi_session_setup(iscsit, scsit, 386 sizeof(struct iscsi_iser_cmd_task), 387 sizeof(struct iser_desc), 388 initial_cmdsn, &hn); 389 if (!cls_session) 390 return NULL; 391 392 *hostno = hn; 393 session = class_to_transport_session(cls_session); 394 395 /* libiscsi setup itts, data and pool so just set desc fields */ 396 for (i = 0; i < session->cmds_max; i++) { 397 ctask = session->cmds[i]; 398 iser_ctask = ctask->dd_data; 399 ctask->hdr = (struct iscsi_cmd *)&iser_ctask->desc.iscsi_header; 400 } 401 402 for (i = 0; i < session->mgmtpool_max; i++) { 403 mtask = session->mgmt_cmds[i]; 404 desc = mtask->dd_data; 405 mtask->hdr = &desc->iscsi_header; 406 desc->data = mtask->data; 407 } 408 409 return cls_session; 410} 411 412static int 413iscsi_iser_set_param(struct iscsi_cls_conn *cls_conn, 414 enum iscsi_param param, char *buf, int buflen) 415{ 416 int value; 417 418 switch (param) { 419 case ISCSI_PARAM_MAX_RECV_DLENGTH: 420 /* TBD */ 421 break; 422 case ISCSI_PARAM_HDRDGST_EN: 423 sscanf(buf, "%d", &value); 424 if (value) { 425 printk(KERN_ERR "DataDigest wasn't negotiated to None"); 426 return -EPROTO; 427 } 428 break; 429 case ISCSI_PARAM_DATADGST_EN: 430 sscanf(buf, "%d", &value); 431 if (value) { 432 printk(KERN_ERR "DataDigest wasn't negotiated to None"); 433 return -EPROTO; 434 } 435 break; 436 case ISCSI_PARAM_IFMARKER_EN: 437 sscanf(buf, "%d", &value); 438 if (value) { 439 printk(KERN_ERR "IFMarker wasn't negotiated to No"); 440 return -EPROTO; 441 } 442 break; 443 case ISCSI_PARAM_OFMARKER_EN: 444 sscanf(buf, "%d", &value); 445 if (value) { 446 printk(KERN_ERR "OFMarker wasn't negotiated to No"); 447 return -EPROTO; 448 } 449 break; 450 default: 451 return iscsi_set_param(cls_conn, param, buf, buflen); 452 } 453 454 return 0; 455} 456 457static void 458iscsi_iser_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats) 459{ 460 struct iscsi_conn *conn = cls_conn->dd_data; 461 462 stats->txdata_octets = conn->txdata_octets; 463 stats->rxdata_octets = conn->rxdata_octets; 464 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt; 465 stats->dataout_pdus = conn->dataout_pdus_cnt; 466 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt; 467 stats->datain_pdus = conn->datain_pdus_cnt; /* always 0 */ 468 stats->r2t_pdus = conn->r2t_pdus_cnt; /* always 0 */ 469 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt; 470 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt; 471 stats->custom_length = 3; 472 strcpy(stats->custom[0].desc, "qp_tx_queue_full"); 473 stats->custom[0].value = 0; /* TB iser_conn->qp_tx_queue_full; */ 474 strcpy(stats->custom[1].desc, "fmr_map_not_avail"); 475 stats->custom[1].value = 0; /* TB iser_conn->fmr_map_not_avail */; 476 strcpy(stats->custom[2].desc, "eh_abort_cnt"); 477 stats->custom[2].value = conn->eh_abort_cnt; 478} 479 480static int 481iscsi_iser_ep_connect(struct sockaddr *dst_addr, int non_blocking, 482 __u64 *ep_handle) 483{ 484 int err; 485 struct iser_conn *ib_conn; 486 487 err = iser_conn_init(&ib_conn); 488 if (err) 489 goto out; 490 491 err = iser_connect(ib_conn, NULL, (struct sockaddr_in *)dst_addr, non_blocking); 492 if (!err) 493 *ep_handle = (__u64)(unsigned long)ib_conn; 494 495out: 496 return err; 497} 498 499static int 500iscsi_iser_ep_poll(__u64 ep_handle, int timeout_ms) 501{ 502 struct iser_conn *ib_conn = iscsi_iser_ib_conn_lookup(ep_handle); 503 int rc; 504 505 if (!ib_conn) 506 return -EINVAL; 507 508 rc = wait_event_interruptible_timeout(ib_conn->wait, 509 ib_conn->state == ISER_CONN_UP, 510 msecs_to_jiffies(timeout_ms)); 511 512 /* if conn establishment failed, return error code to iscsi */ 513 if (!rc && 514 (ib_conn->state == ISER_CONN_TERMINATING || 515 ib_conn->state == ISER_CONN_DOWN)) 516 rc = -1; 517 518 iser_err("ib conn %p rc = %d\n", ib_conn, rc); 519 520 if (rc > 0) 521 return 1; /* success, this is the equivalent of POLLOUT */ 522 else if (!rc) 523 return 0; /* timeout */ 524 else 525 return rc; /* signal */ 526} 527 528static void 529iscsi_iser_ep_disconnect(__u64 ep_handle) 530{ 531 struct iser_conn *ib_conn; 532 533 ib_conn = iscsi_iser_ib_conn_lookup(ep_handle); 534 if (!ib_conn) 535 return; 536 537 iser_err("ib conn %p state %d\n",ib_conn, ib_conn->state); 538 iser_conn_terminate(ib_conn); 539} 540 541static struct scsi_host_template iscsi_iser_sht = { 542 .name = "iSCSI Initiator over iSER, v." DRV_VER, 543 .queuecommand = iscsi_queuecommand, 544 .can_queue = ISCSI_XMIT_CMDS_MAX - 1, 545 .sg_tablesize = ISCSI_ISER_SG_TABLESIZE, 546 .max_sectors = 1024, 547 .cmd_per_lun = ISCSI_MAX_CMD_PER_LUN, 548 .eh_abort_handler = iscsi_eh_abort, 549 .eh_host_reset_handler = iscsi_eh_host_reset, 550 .use_clustering = DISABLE_CLUSTERING, 551 .proc_name = "iscsi_iser", 552 .this_id = -1, 553}; 554 555static struct iscsi_transport iscsi_iser_transport = { 556 .owner = THIS_MODULE, 557 .name = "iser", 558 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T, 559 .param_mask = ISCSI_MAX_RECV_DLENGTH | 560 ISCSI_MAX_XMIT_DLENGTH | 561 ISCSI_HDRDGST_EN | 562 ISCSI_DATADGST_EN | 563 ISCSI_INITIAL_R2T_EN | 564 ISCSI_MAX_R2T | 565 ISCSI_IMM_DATA_EN | 566 ISCSI_FIRST_BURST | 567 ISCSI_MAX_BURST | 568 ISCSI_PDU_INORDER_EN | 569 ISCSI_DATASEQ_INORDER_EN | 570 ISCSI_EXP_STATSN | 571 ISCSI_PERSISTENT_PORT | 572 ISCSI_PERSISTENT_ADDRESS | 573 ISCSI_TARGET_NAME | 574 ISCSI_TPGT, 575 .host_template = &iscsi_iser_sht, 576 .conndata_size = sizeof(struct iscsi_conn), 577 .max_lun = ISCSI_ISER_MAX_LUN, 578 .max_cmd_len = ISCSI_ISER_MAX_CMD_LEN, 579 /* session management */ 580 .create_session = iscsi_iser_session_create, 581 .destroy_session = iscsi_session_teardown, 582 /* connection management */ 583 .create_conn = iscsi_iser_conn_create, 584 .bind_conn = iscsi_iser_conn_bind, 585 .destroy_conn = iscsi_iser_conn_destroy, 586 .set_param = iscsi_iser_set_param, 587 .get_conn_param = iscsi_conn_get_param, 588 .get_session_param = iscsi_session_get_param, 589 .start_conn = iscsi_iser_conn_start, 590 .stop_conn = iscsi_conn_stop, 591 /* IO */ 592 .send_pdu = iscsi_conn_send_pdu, 593 .get_stats = iscsi_iser_conn_get_stats, 594 .init_cmd_task = iscsi_iser_cmd_init, 595 .xmit_cmd_task = iscsi_iser_ctask_xmit, 596 .xmit_mgmt_task = iscsi_iser_mtask_xmit, 597 .cleanup_cmd_task = iscsi_iser_cleanup_ctask, 598 /* recovery */ 599 .session_recovery_timedout = iscsi_session_recovery_timedout, 600 601 .ep_connect = iscsi_iser_ep_connect, 602 .ep_poll = iscsi_iser_ep_poll, 603 .ep_disconnect = iscsi_iser_ep_disconnect 604}; 605 606static int __init iser_init(void) 607{ 608 int err; 609 610 iser_dbg("Starting iSER datamover...\n"); 611 612 if (iscsi_max_lun < 1) { 613 printk(KERN_ERR "Invalid max_lun value of %u\n", iscsi_max_lun); 614 return -EINVAL; 615 } 616 617 iscsi_iser_transport.max_lun = iscsi_max_lun; 618 619 memset(&ig, 0, sizeof(struct iser_global)); 620 621 ig.desc_cache = kmem_cache_create("iser_descriptors", 622 sizeof (struct iser_desc), 623 0, SLAB_HWCACHE_ALIGN, 624 NULL, NULL); 625 if (ig.desc_cache == NULL) 626 return -ENOMEM; 627 628 /* device init is called only after the first addr resolution */ 629 mutex_init(&ig.device_list_mutex); 630 INIT_LIST_HEAD(&ig.device_list); 631 mutex_init(&ig.connlist_mutex); 632 INIT_LIST_HEAD(&ig.connlist); 633 634 if (!iscsi_register_transport(&iscsi_iser_transport)) { 635 iser_err("iscsi_register_transport failed\n"); 636 err = -EINVAL; 637 goto register_transport_failure; 638 } 639 640 return 0; 641 642register_transport_failure: 643 kmem_cache_destroy(ig.desc_cache); 644 645 return err; 646} 647 648static void __exit iser_exit(void) 649{ 650 iser_dbg("Removing iSER datamover...\n"); 651 iscsi_unregister_transport(&iscsi_iser_transport); 652 kmem_cache_destroy(ig.desc_cache); 653} 654 655module_init(iser_init); 656module_exit(iser_exit); 657