165e7ae7bSOr Gerlitz /* 265e7ae7bSOr Gerlitz * iSCSI Initiator over iSER Data-Path 365e7ae7bSOr Gerlitz * 465e7ae7bSOr Gerlitz * Copyright (C) 2004 Dmitry Yusupov 565e7ae7bSOr Gerlitz * Copyright (C) 2004 Alex Aizman 665e7ae7bSOr Gerlitz * Copyright (C) 2005 Mike Christie 765e7ae7bSOr Gerlitz * Copyright (c) 2005, 2006 Voltaire, Inc. All rights reserved. 865e7ae7bSOr Gerlitz * maintained by openib-general@openib.org 965e7ae7bSOr Gerlitz * 1065e7ae7bSOr Gerlitz * This software is available to you under a choice of one of two 1165e7ae7bSOr Gerlitz * licenses. You may choose to be licensed under the terms of the GNU 1265e7ae7bSOr Gerlitz * General Public License (GPL) Version 2, available from the file 1365e7ae7bSOr Gerlitz * COPYING in the main directory of this source tree, or the 1465e7ae7bSOr Gerlitz * OpenIB.org BSD license below: 1565e7ae7bSOr Gerlitz * 1665e7ae7bSOr Gerlitz * Redistribution and use in source and binary forms, with or 1765e7ae7bSOr Gerlitz * without modification, are permitted provided that the following 1865e7ae7bSOr Gerlitz * conditions are met: 1965e7ae7bSOr Gerlitz * 2065e7ae7bSOr Gerlitz * - Redistributions of source code must retain the above 2165e7ae7bSOr Gerlitz * copyright notice, this list of conditions and the following 2265e7ae7bSOr Gerlitz * disclaimer. 2365e7ae7bSOr Gerlitz * 2465e7ae7bSOr Gerlitz * - Redistributions in binary form must reproduce the above 2565e7ae7bSOr Gerlitz * copyright notice, this list of conditions and the following 2665e7ae7bSOr Gerlitz * disclaimer in the documentation and/or other materials 2765e7ae7bSOr Gerlitz * provided with the distribution. 2865e7ae7bSOr Gerlitz * 2965e7ae7bSOr Gerlitz * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 3065e7ae7bSOr Gerlitz * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 3165e7ae7bSOr Gerlitz * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 3265e7ae7bSOr Gerlitz * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 3365e7ae7bSOr Gerlitz * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 3465e7ae7bSOr Gerlitz * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 3565e7ae7bSOr Gerlitz * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 3665e7ae7bSOr Gerlitz * SOFTWARE. 3765e7ae7bSOr Gerlitz * 3865e7ae7bSOr Gerlitz * Credits: 3965e7ae7bSOr Gerlitz * Christoph Hellwig 4065e7ae7bSOr Gerlitz * FUJITA Tomonori 4165e7ae7bSOr Gerlitz * Arne Redlich 4265e7ae7bSOr Gerlitz * Zhenyu Wang 4365e7ae7bSOr Gerlitz * Modified by: 4465e7ae7bSOr Gerlitz * Erez Zilber 4565e7ae7bSOr Gerlitz * 4665e7ae7bSOr Gerlitz * 4765e7ae7bSOr Gerlitz * $Id: iscsi_iser.c 6965 2006-05-07 11:36:20Z ogerlitz $ 4865e7ae7bSOr Gerlitz */ 4965e7ae7bSOr Gerlitz 5065e7ae7bSOr Gerlitz #include <linux/types.h> 5165e7ae7bSOr Gerlitz #include <linux/list.h> 5265e7ae7bSOr Gerlitz #include <linux/hardirq.h> 5365e7ae7bSOr Gerlitz #include <linux/kfifo.h> 5465e7ae7bSOr Gerlitz #include <linux/blkdev.h> 5565e7ae7bSOr Gerlitz #include <linux/init.h> 5665e7ae7bSOr Gerlitz #include <linux/ioctl.h> 5765e7ae7bSOr Gerlitz #include <linux/cdev.h> 5865e7ae7bSOr Gerlitz #include <linux/in.h> 5965e7ae7bSOr Gerlitz #include <linux/net.h> 6065e7ae7bSOr Gerlitz #include <linux/scatterlist.h> 6165e7ae7bSOr Gerlitz #include <linux/delay.h> 6265e7ae7bSOr Gerlitz 6365e7ae7bSOr Gerlitz #include <net/sock.h> 6465e7ae7bSOr Gerlitz 6565e7ae7bSOr Gerlitz #include <asm/uaccess.h> 6665e7ae7bSOr Gerlitz 6765e7ae7bSOr Gerlitz #include <scsi/scsi_cmnd.h> 6865e7ae7bSOr Gerlitz #include <scsi/scsi_device.h> 6965e7ae7bSOr Gerlitz #include <scsi/scsi_eh.h> 7065e7ae7bSOr Gerlitz #include <scsi/scsi_tcq.h> 7165e7ae7bSOr Gerlitz #include <scsi/scsi_host.h> 7265e7ae7bSOr Gerlitz #include <scsi/scsi.h> 7365e7ae7bSOr Gerlitz #include <scsi/scsi_transport_iscsi.h> 7465e7ae7bSOr Gerlitz 7565e7ae7bSOr Gerlitz #include "iscsi_iser.h" 7665e7ae7bSOr Gerlitz 7765e7ae7bSOr Gerlitz static unsigned int iscsi_max_lun = 512; 7865e7ae7bSOr Gerlitz module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO); 7965e7ae7bSOr Gerlitz 8065e7ae7bSOr Gerlitz int iser_debug_level = 0; 8165e7ae7bSOr Gerlitz 8265e7ae7bSOr Gerlitz MODULE_DESCRIPTION("iSER (iSCSI Extensions for RDMA) Datamover " 8365e7ae7bSOr Gerlitz "v" DRV_VER " (" DRV_DATE ")"); 8465e7ae7bSOr Gerlitz MODULE_LICENSE("Dual BSD/GPL"); 8565e7ae7bSOr Gerlitz MODULE_AUTHOR("Alex Nezhinsky, Dan Bar Dov, Or Gerlitz"); 8665e7ae7bSOr Gerlitz 8765e7ae7bSOr Gerlitz module_param_named(debug_level, iser_debug_level, int, 0644); 8865e7ae7bSOr Gerlitz MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0 (default:disabled)"); 8965e7ae7bSOr Gerlitz 9065e7ae7bSOr Gerlitz struct iser_global ig; 9165e7ae7bSOr Gerlitz 9265e7ae7bSOr Gerlitz void 9365e7ae7bSOr Gerlitz iscsi_iser_recv(struct iscsi_conn *conn, 9465e7ae7bSOr Gerlitz struct iscsi_hdr *hdr, char *rx_data, int rx_data_len) 9565e7ae7bSOr Gerlitz { 9665e7ae7bSOr Gerlitz int rc = 0; 9765e7ae7bSOr Gerlitz uint32_t ret_itt; 9865e7ae7bSOr Gerlitz int datalen; 9965e7ae7bSOr Gerlitz int ahslen; 10065e7ae7bSOr Gerlitz 10165e7ae7bSOr Gerlitz /* verify PDU length */ 10265e7ae7bSOr Gerlitz datalen = ntoh24(hdr->dlength); 10365e7ae7bSOr Gerlitz if (datalen != rx_data_len) { 10465e7ae7bSOr Gerlitz printk(KERN_ERR "iscsi_iser: datalen %d (hdr) != %d (IB) \n", 10565e7ae7bSOr Gerlitz datalen, rx_data_len); 10665e7ae7bSOr Gerlitz rc = ISCSI_ERR_DATALEN; 10765e7ae7bSOr Gerlitz goto error; 10865e7ae7bSOr Gerlitz } 10965e7ae7bSOr Gerlitz 11065e7ae7bSOr Gerlitz /* read AHS */ 11165e7ae7bSOr Gerlitz ahslen = hdr->hlength * 4; 11265e7ae7bSOr Gerlitz 11365e7ae7bSOr Gerlitz /* verify itt (itt encoding: age+cid+itt) */ 11465e7ae7bSOr Gerlitz rc = iscsi_verify_itt(conn, hdr, &ret_itt); 11565e7ae7bSOr Gerlitz 11665e7ae7bSOr Gerlitz if (!rc) 11765e7ae7bSOr Gerlitz rc = iscsi_complete_pdu(conn, hdr, rx_data, rx_data_len); 11865e7ae7bSOr Gerlitz 11965e7ae7bSOr Gerlitz if (rc && rc != ISCSI_ERR_NO_SCSI_CMD) 12065e7ae7bSOr Gerlitz goto error; 12165e7ae7bSOr Gerlitz 12265e7ae7bSOr Gerlitz return; 12365e7ae7bSOr Gerlitz error: 12465e7ae7bSOr Gerlitz iscsi_conn_failure(conn, rc); 12565e7ae7bSOr Gerlitz } 12665e7ae7bSOr Gerlitz 12765e7ae7bSOr Gerlitz 12865e7ae7bSOr Gerlitz /** 12965e7ae7bSOr Gerlitz * iscsi_iser_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands 13065e7ae7bSOr Gerlitz * 13165e7ae7bSOr Gerlitz **/ 13265e7ae7bSOr Gerlitz static void 13365e7ae7bSOr Gerlitz iscsi_iser_cmd_init(struct iscsi_cmd_task *ctask) 13465e7ae7bSOr Gerlitz { 13565e7ae7bSOr Gerlitz struct iscsi_iser_conn *iser_conn = ctask->conn->dd_data; 13665e7ae7bSOr Gerlitz struct iscsi_iser_cmd_task *iser_ctask = ctask->dd_data; 13765e7ae7bSOr Gerlitz struct scsi_cmnd *sc = ctask->sc; 13865e7ae7bSOr Gerlitz 13965e7ae7bSOr Gerlitz iser_ctask->command_sent = 0; 14065e7ae7bSOr Gerlitz iser_ctask->iser_conn = iser_conn; 14165e7ae7bSOr Gerlitz 14265e7ae7bSOr Gerlitz if (sc->sc_data_direction == DMA_TO_DEVICE) { 14365e7ae7bSOr Gerlitz BUG_ON(ctask->total_length == 0); 14465e7ae7bSOr Gerlitz 145ffd0436eSMike Christie debug_scsi("cmd [itt %x total %d imm %d unsol_data %d\n", 14665e7ae7bSOr Gerlitz ctask->itt, ctask->total_length, ctask->imm_count, 147ffd0436eSMike Christie ctask->unsol_count); 148ffd0436eSMike Christie } 14965e7ae7bSOr Gerlitz 15065e7ae7bSOr Gerlitz iser_ctask_rdma_init(iser_ctask); 15165e7ae7bSOr Gerlitz } 15265e7ae7bSOr Gerlitz 15365e7ae7bSOr Gerlitz /** 15465e7ae7bSOr Gerlitz * iscsi_mtask_xmit - xmit management(immediate) task 15565e7ae7bSOr Gerlitz * @conn: iscsi connection 15665e7ae7bSOr Gerlitz * @mtask: task management task 15765e7ae7bSOr Gerlitz * 15865e7ae7bSOr Gerlitz * Notes: 15965e7ae7bSOr Gerlitz * The function can return -EAGAIN in which case caller must 16065e7ae7bSOr Gerlitz * call it again later, or recover. '0' return code means successful 16165e7ae7bSOr Gerlitz * xmit. 16265e7ae7bSOr Gerlitz * 16365e7ae7bSOr Gerlitz **/ 16465e7ae7bSOr Gerlitz static int 16565e7ae7bSOr Gerlitz iscsi_iser_mtask_xmit(struct iscsi_conn *conn, 16665e7ae7bSOr Gerlitz struct iscsi_mgmt_task *mtask) 16765e7ae7bSOr Gerlitz { 16865e7ae7bSOr Gerlitz int error = 0; 16965e7ae7bSOr Gerlitz 17065e7ae7bSOr Gerlitz debug_scsi("mtask deq [cid %d itt 0x%x]\n", conn->id, mtask->itt); 17165e7ae7bSOr Gerlitz 17265e7ae7bSOr Gerlitz error = iser_send_control(conn, mtask); 17365e7ae7bSOr Gerlitz 17465e7ae7bSOr Gerlitz /* since iser xmits control with zero copy, mtasks can not be recycled 17565e7ae7bSOr Gerlitz * right after sending them. 17665e7ae7bSOr Gerlitz * The recycling scheme is based on whether a response is expected 17765e7ae7bSOr Gerlitz * - if yes, the mtask is recycled at iscsi_complete_pdu 17865e7ae7bSOr Gerlitz * - if no, the mtask is recycled at iser_snd_completion 17965e7ae7bSOr Gerlitz */ 18065e7ae7bSOr Gerlitz if (error && error != -EAGAIN) 18165e7ae7bSOr Gerlitz iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); 18265e7ae7bSOr Gerlitz 18365e7ae7bSOr Gerlitz return error; 18465e7ae7bSOr Gerlitz } 18565e7ae7bSOr Gerlitz 18665e7ae7bSOr Gerlitz static int 18765e7ae7bSOr Gerlitz iscsi_iser_ctask_xmit_unsol_data(struct iscsi_conn *conn, 18865e7ae7bSOr Gerlitz struct iscsi_cmd_task *ctask) 18965e7ae7bSOr Gerlitz { 19065e7ae7bSOr Gerlitz struct iscsi_data hdr; 19165e7ae7bSOr Gerlitz int error = 0; 19265e7ae7bSOr Gerlitz 19365e7ae7bSOr Gerlitz /* Send data-out PDUs while there's still unsolicited data to send */ 19465e7ae7bSOr Gerlitz while (ctask->unsol_count > 0) { 195ffd0436eSMike Christie iscsi_prep_unsolicit_data_pdu(ctask, &hdr); 19665e7ae7bSOr Gerlitz debug_scsi("Sending data-out: itt 0x%x, data count %d\n", 19765e7ae7bSOr Gerlitz hdr.itt, ctask->data_count); 19865e7ae7bSOr Gerlitz 19965e7ae7bSOr Gerlitz /* the buffer description has been passed with the command */ 20065e7ae7bSOr Gerlitz /* Send the command */ 20165e7ae7bSOr Gerlitz error = iser_send_data_out(conn, ctask, &hdr); 20265e7ae7bSOr Gerlitz if (error) { 20365e7ae7bSOr Gerlitz ctask->unsol_datasn--; 20465e7ae7bSOr Gerlitz goto iscsi_iser_ctask_xmit_unsol_data_exit; 20565e7ae7bSOr Gerlitz } 20665e7ae7bSOr Gerlitz ctask->unsol_count -= ctask->data_count; 20765e7ae7bSOr Gerlitz debug_scsi("Need to send %d more as data-out PDUs\n", 20865e7ae7bSOr Gerlitz ctask->unsol_count); 20965e7ae7bSOr Gerlitz } 21065e7ae7bSOr Gerlitz 21165e7ae7bSOr Gerlitz iscsi_iser_ctask_xmit_unsol_data_exit: 21265e7ae7bSOr Gerlitz return error; 21365e7ae7bSOr Gerlitz } 21465e7ae7bSOr Gerlitz 21565e7ae7bSOr Gerlitz static int 21665e7ae7bSOr Gerlitz iscsi_iser_ctask_xmit(struct iscsi_conn *conn, 21765e7ae7bSOr Gerlitz struct iscsi_cmd_task *ctask) 21865e7ae7bSOr Gerlitz { 21965e7ae7bSOr Gerlitz struct iscsi_iser_cmd_task *iser_ctask = ctask->dd_data; 22065e7ae7bSOr Gerlitz int error = 0; 22165e7ae7bSOr Gerlitz 22265e7ae7bSOr Gerlitz debug_scsi("ctask deq [cid %d itt 0x%x]\n", 22365e7ae7bSOr Gerlitz conn->id, ctask->itt); 22465e7ae7bSOr Gerlitz 22565e7ae7bSOr Gerlitz /* 22665e7ae7bSOr Gerlitz * serialize with TMF AbortTask 22765e7ae7bSOr Gerlitz */ 22865e7ae7bSOr Gerlitz if (ctask->mtask) 22965e7ae7bSOr Gerlitz return error; 23065e7ae7bSOr Gerlitz 23165e7ae7bSOr Gerlitz /* Send the cmd PDU */ 23265e7ae7bSOr Gerlitz if (!iser_ctask->command_sent) { 23365e7ae7bSOr Gerlitz error = iser_send_command(conn, ctask); 23465e7ae7bSOr Gerlitz if (error) 23565e7ae7bSOr Gerlitz goto iscsi_iser_ctask_xmit_exit; 23665e7ae7bSOr Gerlitz iser_ctask->command_sent = 1; 23765e7ae7bSOr Gerlitz } 23865e7ae7bSOr Gerlitz 23965e7ae7bSOr Gerlitz /* Send unsolicited data-out PDU(s) if necessary */ 24065e7ae7bSOr Gerlitz if (ctask->unsol_count) 24165e7ae7bSOr Gerlitz error = iscsi_iser_ctask_xmit_unsol_data(conn, ctask); 24265e7ae7bSOr Gerlitz 24365e7ae7bSOr Gerlitz iscsi_iser_ctask_xmit_exit: 24465e7ae7bSOr Gerlitz if (error && error != -EAGAIN) 24565e7ae7bSOr Gerlitz iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); 24665e7ae7bSOr Gerlitz return error; 24765e7ae7bSOr Gerlitz } 24865e7ae7bSOr Gerlitz 24965e7ae7bSOr Gerlitz static void 25065e7ae7bSOr Gerlitz iscsi_iser_cleanup_ctask(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) 25165e7ae7bSOr Gerlitz { 25265e7ae7bSOr Gerlitz struct iscsi_iser_cmd_task *iser_ctask = ctask->dd_data; 25365e7ae7bSOr Gerlitz 25465e7ae7bSOr Gerlitz if (iser_ctask->status == ISER_TASK_STATUS_STARTED) { 25565e7ae7bSOr Gerlitz iser_ctask->status = ISER_TASK_STATUS_COMPLETED; 25665e7ae7bSOr Gerlitz iser_ctask_rdma_finalize(iser_ctask); 25765e7ae7bSOr Gerlitz } 25865e7ae7bSOr Gerlitz } 25965e7ae7bSOr Gerlitz 26065e7ae7bSOr Gerlitz static struct iser_conn * 26165e7ae7bSOr Gerlitz iscsi_iser_ib_conn_lookup(__u64 ep_handle) 26265e7ae7bSOr Gerlitz { 26365e7ae7bSOr Gerlitz struct iser_conn *ib_conn; 26465e7ae7bSOr Gerlitz struct iser_conn *uib_conn = (struct iser_conn *)(unsigned long)ep_handle; 26565e7ae7bSOr Gerlitz 26665e7ae7bSOr Gerlitz mutex_lock(&ig.connlist_mutex); 26765e7ae7bSOr Gerlitz list_for_each_entry(ib_conn, &ig.connlist, conn_list) { 26865e7ae7bSOr Gerlitz if (ib_conn == uib_conn) { 26965e7ae7bSOr Gerlitz mutex_unlock(&ig.connlist_mutex); 27065e7ae7bSOr Gerlitz return ib_conn; 27165e7ae7bSOr Gerlitz } 27265e7ae7bSOr Gerlitz } 27365e7ae7bSOr Gerlitz mutex_unlock(&ig.connlist_mutex); 27465e7ae7bSOr Gerlitz iser_err("no conn exists for eph %llx\n",(unsigned long long)ep_handle); 27565e7ae7bSOr Gerlitz return NULL; 27665e7ae7bSOr Gerlitz } 27765e7ae7bSOr Gerlitz 27865e7ae7bSOr Gerlitz static struct iscsi_cls_conn * 27965e7ae7bSOr Gerlitz iscsi_iser_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx) 28065e7ae7bSOr Gerlitz { 28165e7ae7bSOr Gerlitz struct iscsi_conn *conn; 28265e7ae7bSOr Gerlitz struct iscsi_cls_conn *cls_conn; 28365e7ae7bSOr Gerlitz struct iscsi_iser_conn *iser_conn; 28465e7ae7bSOr Gerlitz 28565e7ae7bSOr Gerlitz cls_conn = iscsi_conn_setup(cls_session, conn_idx); 28665e7ae7bSOr Gerlitz if (!cls_conn) 28765e7ae7bSOr Gerlitz return NULL; 28865e7ae7bSOr Gerlitz conn = cls_conn->dd_data; 28965e7ae7bSOr Gerlitz 29065e7ae7bSOr Gerlitz /* 29165e7ae7bSOr Gerlitz * due to issues with the login code re iser sematics 29265e7ae7bSOr Gerlitz * this not set in iscsi_conn_setup - FIXME 29365e7ae7bSOr Gerlitz */ 29465e7ae7bSOr Gerlitz conn->max_recv_dlength = 128; 29565e7ae7bSOr Gerlitz 29665e7ae7bSOr Gerlitz iser_conn = kzalloc(sizeof(*iser_conn), GFP_KERNEL); 29765e7ae7bSOr Gerlitz if (!iser_conn) 29865e7ae7bSOr Gerlitz goto conn_alloc_fail; 29965e7ae7bSOr Gerlitz 30065e7ae7bSOr Gerlitz /* currently this is the only field which need to be initiated */ 30165e7ae7bSOr Gerlitz rwlock_init(&iser_conn->lock); 30265e7ae7bSOr Gerlitz 30365e7ae7bSOr Gerlitz conn->dd_data = iser_conn; 30465e7ae7bSOr Gerlitz iser_conn->iscsi_conn = conn; 30565e7ae7bSOr Gerlitz 30665e7ae7bSOr Gerlitz return cls_conn; 30765e7ae7bSOr Gerlitz 30865e7ae7bSOr Gerlitz conn_alloc_fail: 30965e7ae7bSOr Gerlitz iscsi_conn_teardown(cls_conn); 31065e7ae7bSOr Gerlitz return NULL; 31165e7ae7bSOr Gerlitz } 31265e7ae7bSOr Gerlitz 31365e7ae7bSOr Gerlitz static void 31465e7ae7bSOr Gerlitz iscsi_iser_conn_destroy(struct iscsi_cls_conn *cls_conn) 31565e7ae7bSOr Gerlitz { 31665e7ae7bSOr Gerlitz struct iscsi_conn *conn = cls_conn->dd_data; 31765e7ae7bSOr Gerlitz struct iscsi_iser_conn *iser_conn = conn->dd_data; 31865e7ae7bSOr Gerlitz 31965e7ae7bSOr Gerlitz iscsi_conn_teardown(cls_conn); 32087e8df7aSErez Zilber if (iser_conn->ib_conn) 32187e8df7aSErez Zilber iser_conn->ib_conn->iser_conn = NULL; 32265e7ae7bSOr Gerlitz kfree(iser_conn); 32365e7ae7bSOr Gerlitz } 32465e7ae7bSOr Gerlitz 32565e7ae7bSOr Gerlitz static int 32665e7ae7bSOr Gerlitz iscsi_iser_conn_bind(struct iscsi_cls_session *cls_session, 32765e7ae7bSOr Gerlitz struct iscsi_cls_conn *cls_conn, uint64_t transport_eph, 32865e7ae7bSOr Gerlitz int is_leading) 32965e7ae7bSOr Gerlitz { 33065e7ae7bSOr Gerlitz struct iscsi_conn *conn = cls_conn->dd_data; 33165e7ae7bSOr Gerlitz struct iscsi_iser_conn *iser_conn; 33265e7ae7bSOr Gerlitz struct iser_conn *ib_conn; 33365e7ae7bSOr Gerlitz int error; 33465e7ae7bSOr Gerlitz 33565e7ae7bSOr Gerlitz error = iscsi_conn_bind(cls_session, cls_conn, is_leading); 33665e7ae7bSOr Gerlitz if (error) 33765e7ae7bSOr Gerlitz return error; 33865e7ae7bSOr Gerlitz 33965e7ae7bSOr Gerlitz /* the transport ep handle comes from user space so it must be 34065e7ae7bSOr Gerlitz * verified against the global ib connections list */ 34165e7ae7bSOr Gerlitz ib_conn = iscsi_iser_ib_conn_lookup(transport_eph); 34265e7ae7bSOr Gerlitz if (!ib_conn) { 34365e7ae7bSOr Gerlitz iser_err("can't bind eph %llx\n", 34465e7ae7bSOr Gerlitz (unsigned long long)transport_eph); 34565e7ae7bSOr Gerlitz return -EINVAL; 34665e7ae7bSOr Gerlitz } 34765e7ae7bSOr Gerlitz /* binds the iSER connection retrieved from the previously 34865e7ae7bSOr Gerlitz * connected ep_handle to the iSCSI layer connection. exchanges 34965e7ae7bSOr Gerlitz * connection pointers */ 35065e7ae7bSOr Gerlitz iser_err("binding iscsi conn %p to iser_conn %p\n",conn,ib_conn); 35165e7ae7bSOr Gerlitz iser_conn = conn->dd_data; 35265e7ae7bSOr Gerlitz ib_conn->iser_conn = iser_conn; 35365e7ae7bSOr Gerlitz iser_conn->ib_conn = ib_conn; 35465e7ae7bSOr Gerlitz 35565e7ae7bSOr Gerlitz conn->recv_lock = &iser_conn->lock; 35665e7ae7bSOr Gerlitz 35765e7ae7bSOr Gerlitz return 0; 35865e7ae7bSOr Gerlitz } 35965e7ae7bSOr Gerlitz 36065e7ae7bSOr Gerlitz static int 36165e7ae7bSOr Gerlitz iscsi_iser_conn_start(struct iscsi_cls_conn *cls_conn) 36265e7ae7bSOr Gerlitz { 36365e7ae7bSOr Gerlitz struct iscsi_conn *conn = cls_conn->dd_data; 36465e7ae7bSOr Gerlitz int err; 36565e7ae7bSOr Gerlitz 36665e7ae7bSOr Gerlitz err = iscsi_conn_start(cls_conn); 36765e7ae7bSOr Gerlitz if (err) 36865e7ae7bSOr Gerlitz return err; 36965e7ae7bSOr Gerlitz 37065e7ae7bSOr Gerlitz return iser_conn_set_full_featured_mode(conn); 37165e7ae7bSOr Gerlitz } 37265e7ae7bSOr Gerlitz 37365e7ae7bSOr Gerlitz static struct iscsi_transport iscsi_iser_transport; 37465e7ae7bSOr Gerlitz 37565e7ae7bSOr Gerlitz static struct iscsi_cls_session * 37665e7ae7bSOr Gerlitz iscsi_iser_session_create(struct iscsi_transport *iscsit, 37765e7ae7bSOr Gerlitz struct scsi_transport_template *scsit, 37865e7ae7bSOr Gerlitz uint32_t initial_cmdsn, uint32_t *hostno) 37965e7ae7bSOr Gerlitz { 38065e7ae7bSOr Gerlitz struct iscsi_cls_session *cls_session; 38165e7ae7bSOr Gerlitz struct iscsi_session *session; 38265e7ae7bSOr Gerlitz int i; 38365e7ae7bSOr Gerlitz uint32_t hn; 38465e7ae7bSOr Gerlitz struct iscsi_cmd_task *ctask; 38565e7ae7bSOr Gerlitz struct iscsi_mgmt_task *mtask; 38665e7ae7bSOr Gerlitz struct iscsi_iser_cmd_task *iser_ctask; 38765e7ae7bSOr Gerlitz struct iser_desc *desc; 38865e7ae7bSOr Gerlitz 38965e7ae7bSOr Gerlitz cls_session = iscsi_session_setup(iscsit, scsit, 39065e7ae7bSOr Gerlitz sizeof(struct iscsi_iser_cmd_task), 39165e7ae7bSOr Gerlitz sizeof(struct iser_desc), 39265e7ae7bSOr Gerlitz initial_cmdsn, &hn); 39365e7ae7bSOr Gerlitz if (!cls_session) 39465e7ae7bSOr Gerlitz return NULL; 39565e7ae7bSOr Gerlitz 39665e7ae7bSOr Gerlitz *hostno = hn; 39765e7ae7bSOr Gerlitz session = class_to_transport_session(cls_session); 39865e7ae7bSOr Gerlitz 39965e7ae7bSOr Gerlitz /* libiscsi setup itts, data and pool so just set desc fields */ 40065e7ae7bSOr Gerlitz for (i = 0; i < session->cmds_max; i++) { 40165e7ae7bSOr Gerlitz ctask = session->cmds[i]; 40265e7ae7bSOr Gerlitz iser_ctask = ctask->dd_data; 40365e7ae7bSOr Gerlitz ctask->hdr = (struct iscsi_cmd *)&iser_ctask->desc.iscsi_header; 40465e7ae7bSOr Gerlitz } 40565e7ae7bSOr Gerlitz 40665e7ae7bSOr Gerlitz for (i = 0; i < session->mgmtpool_max; i++) { 40765e7ae7bSOr Gerlitz mtask = session->mgmt_cmds[i]; 40865e7ae7bSOr Gerlitz desc = mtask->dd_data; 40965e7ae7bSOr Gerlitz mtask->hdr = &desc->iscsi_header; 41065e7ae7bSOr Gerlitz desc->data = mtask->data; 41165e7ae7bSOr Gerlitz } 41265e7ae7bSOr Gerlitz 41365e7ae7bSOr Gerlitz return cls_session; 41465e7ae7bSOr Gerlitz } 41565e7ae7bSOr Gerlitz 41665e7ae7bSOr Gerlitz static int 417358ff019SMike Christie iscsi_iser_set_param(struct iscsi_cls_conn *cls_conn, 418358ff019SMike Christie enum iscsi_param param, char *buf, int buflen) 41965e7ae7bSOr Gerlitz { 420358ff019SMike Christie int value; 42165e7ae7bSOr Gerlitz 42265e7ae7bSOr Gerlitz switch (param) { 42365e7ae7bSOr Gerlitz case ISCSI_PARAM_MAX_RECV_DLENGTH: 42465e7ae7bSOr Gerlitz /* TBD */ 42565e7ae7bSOr Gerlitz break; 42665e7ae7bSOr Gerlitz case ISCSI_PARAM_HDRDGST_EN: 427358ff019SMike Christie sscanf(buf, "%d", &value); 42865e7ae7bSOr Gerlitz if (value) { 42965e7ae7bSOr Gerlitz printk(KERN_ERR "DataDigest wasn't negotiated to None"); 43065e7ae7bSOr Gerlitz return -EPROTO; 43165e7ae7bSOr Gerlitz } 43265e7ae7bSOr Gerlitz break; 43365e7ae7bSOr Gerlitz case ISCSI_PARAM_DATADGST_EN: 434358ff019SMike Christie sscanf(buf, "%d", &value); 43565e7ae7bSOr Gerlitz if (value) { 43665e7ae7bSOr Gerlitz printk(KERN_ERR "DataDigest wasn't negotiated to None"); 43765e7ae7bSOr Gerlitz return -EPROTO; 43865e7ae7bSOr Gerlitz } 43965e7ae7bSOr Gerlitz break; 44065e7ae7bSOr Gerlitz case ISCSI_PARAM_IFMARKER_EN: 441358ff019SMike Christie sscanf(buf, "%d", &value); 44265e7ae7bSOr Gerlitz if (value) { 44365e7ae7bSOr Gerlitz printk(KERN_ERR "IFMarker wasn't negotiated to No"); 44465e7ae7bSOr Gerlitz return -EPROTO; 44565e7ae7bSOr Gerlitz } 44665e7ae7bSOr Gerlitz break; 44765e7ae7bSOr Gerlitz case ISCSI_PARAM_OFMARKER_EN: 448358ff019SMike Christie sscanf(buf, "%d", &value); 44965e7ae7bSOr Gerlitz if (value) { 45065e7ae7bSOr Gerlitz printk(KERN_ERR "OFMarker wasn't negotiated to No"); 45165e7ae7bSOr Gerlitz return -EPROTO; 45265e7ae7bSOr Gerlitz } 45365e7ae7bSOr Gerlitz break; 45465e7ae7bSOr Gerlitz default: 455358ff019SMike Christie return iscsi_set_param(cls_conn, param, buf, buflen); 45665e7ae7bSOr Gerlitz } 45765e7ae7bSOr Gerlitz 45865e7ae7bSOr Gerlitz return 0; 45965e7ae7bSOr Gerlitz } 46065e7ae7bSOr Gerlitz 46165e7ae7bSOr Gerlitz static void 46265e7ae7bSOr Gerlitz iscsi_iser_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats) 46365e7ae7bSOr Gerlitz { 46465e7ae7bSOr Gerlitz struct iscsi_conn *conn = cls_conn->dd_data; 46565e7ae7bSOr Gerlitz 46665e7ae7bSOr Gerlitz stats->txdata_octets = conn->txdata_octets; 46765e7ae7bSOr Gerlitz stats->rxdata_octets = conn->rxdata_octets; 46865e7ae7bSOr Gerlitz stats->scsicmd_pdus = conn->scsicmd_pdus_cnt; 46965e7ae7bSOr Gerlitz stats->dataout_pdus = conn->dataout_pdus_cnt; 47065e7ae7bSOr Gerlitz stats->scsirsp_pdus = conn->scsirsp_pdus_cnt; 47165e7ae7bSOr Gerlitz stats->datain_pdus = conn->datain_pdus_cnt; /* always 0 */ 47265e7ae7bSOr Gerlitz stats->r2t_pdus = conn->r2t_pdus_cnt; /* always 0 */ 47365e7ae7bSOr Gerlitz stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt; 47465e7ae7bSOr Gerlitz stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt; 47565e7ae7bSOr Gerlitz stats->custom_length = 3; 47665e7ae7bSOr Gerlitz strcpy(stats->custom[0].desc, "qp_tx_queue_full"); 47765e7ae7bSOr Gerlitz stats->custom[0].value = 0; /* TB iser_conn->qp_tx_queue_full; */ 47865e7ae7bSOr Gerlitz strcpy(stats->custom[1].desc, "fmr_map_not_avail"); 47965e7ae7bSOr Gerlitz stats->custom[1].value = 0; /* TB iser_conn->fmr_map_not_avail */; 48065e7ae7bSOr Gerlitz strcpy(stats->custom[2].desc, "eh_abort_cnt"); 48165e7ae7bSOr Gerlitz stats->custom[2].value = conn->eh_abort_cnt; 48265e7ae7bSOr Gerlitz } 48365e7ae7bSOr Gerlitz 48465e7ae7bSOr Gerlitz static int 48565e7ae7bSOr Gerlitz iscsi_iser_ep_connect(struct sockaddr *dst_addr, int non_blocking, 48665e7ae7bSOr Gerlitz __u64 *ep_handle) 48765e7ae7bSOr Gerlitz { 48865e7ae7bSOr Gerlitz int err; 48965e7ae7bSOr Gerlitz struct iser_conn *ib_conn; 49065e7ae7bSOr Gerlitz 49165e7ae7bSOr Gerlitz err = iser_conn_init(&ib_conn); 49265e7ae7bSOr Gerlitz if (err) 49365e7ae7bSOr Gerlitz goto out; 49465e7ae7bSOr Gerlitz 49565e7ae7bSOr Gerlitz err = iser_connect(ib_conn, NULL, (struct sockaddr_in *)dst_addr, non_blocking); 49665e7ae7bSOr Gerlitz if (!err) 49765e7ae7bSOr Gerlitz *ep_handle = (__u64)(unsigned long)ib_conn; 49865e7ae7bSOr Gerlitz 49965e7ae7bSOr Gerlitz out: 50065e7ae7bSOr Gerlitz return err; 50165e7ae7bSOr Gerlitz } 50265e7ae7bSOr Gerlitz 50365e7ae7bSOr Gerlitz static int 50465e7ae7bSOr Gerlitz iscsi_iser_ep_poll(__u64 ep_handle, int timeout_ms) 50565e7ae7bSOr Gerlitz { 50665e7ae7bSOr Gerlitz struct iser_conn *ib_conn = iscsi_iser_ib_conn_lookup(ep_handle); 50765e7ae7bSOr Gerlitz int rc; 50865e7ae7bSOr Gerlitz 50965e7ae7bSOr Gerlitz if (!ib_conn) 51065e7ae7bSOr Gerlitz return -EINVAL; 51165e7ae7bSOr Gerlitz 51265e7ae7bSOr Gerlitz rc = wait_event_interruptible_timeout(ib_conn->wait, 51365e7ae7bSOr Gerlitz ib_conn->state == ISER_CONN_UP, 51465e7ae7bSOr Gerlitz msecs_to_jiffies(timeout_ms)); 51565e7ae7bSOr Gerlitz 51665e7ae7bSOr Gerlitz /* if conn establishment failed, return error code to iscsi */ 51765e7ae7bSOr Gerlitz if (!rc && 51865e7ae7bSOr Gerlitz (ib_conn->state == ISER_CONN_TERMINATING || 51965e7ae7bSOr Gerlitz ib_conn->state == ISER_CONN_DOWN)) 52065e7ae7bSOr Gerlitz rc = -1; 52165e7ae7bSOr Gerlitz 52265e7ae7bSOr Gerlitz iser_err("ib conn %p rc = %d\n", ib_conn, rc); 52365e7ae7bSOr Gerlitz 52465e7ae7bSOr Gerlitz if (rc > 0) 52565e7ae7bSOr Gerlitz return 1; /* success, this is the equivalent of POLLOUT */ 52665e7ae7bSOr Gerlitz else if (!rc) 52765e7ae7bSOr Gerlitz return 0; /* timeout */ 52865e7ae7bSOr Gerlitz else 52965e7ae7bSOr Gerlitz return rc; /* signal */ 53065e7ae7bSOr Gerlitz } 53165e7ae7bSOr Gerlitz 53265e7ae7bSOr Gerlitz static void 53365e7ae7bSOr Gerlitz iscsi_iser_ep_disconnect(__u64 ep_handle) 53465e7ae7bSOr Gerlitz { 5351c83469dSMike Christie struct iser_conn *ib_conn; 53665e7ae7bSOr Gerlitz 5371c83469dSMike Christie ib_conn = iscsi_iser_ib_conn_lookup(ep_handle); 53865e7ae7bSOr Gerlitz if (!ib_conn) 53965e7ae7bSOr Gerlitz return; 54065e7ae7bSOr Gerlitz 54165e7ae7bSOr Gerlitz iser_err("ib conn %p state %d\n",ib_conn, ib_conn->state); 54265e7ae7bSOr Gerlitz iser_conn_terminate(ib_conn); 54365e7ae7bSOr Gerlitz } 54465e7ae7bSOr Gerlitz 54565e7ae7bSOr Gerlitz static struct scsi_host_template iscsi_iser_sht = { 54665e7ae7bSOr Gerlitz .name = "iSCSI Initiator over iSER, v." DRV_VER, 54765e7ae7bSOr Gerlitz .queuecommand = iscsi_queuecommand, 54865e7ae7bSOr Gerlitz .can_queue = ISCSI_XMIT_CMDS_MAX - 1, 54965e7ae7bSOr Gerlitz .sg_tablesize = ISCSI_ISER_SG_TABLESIZE, 5508072ec2fSErez Zilber .max_sectors = 1024, 55165e7ae7bSOr Gerlitz .cmd_per_lun = ISCSI_MAX_CMD_PER_LUN, 55265e7ae7bSOr Gerlitz .eh_abort_handler = iscsi_eh_abort, 55365e7ae7bSOr Gerlitz .eh_host_reset_handler = iscsi_eh_host_reset, 55465e7ae7bSOr Gerlitz .use_clustering = DISABLE_CLUSTERING, 55565e7ae7bSOr Gerlitz .proc_name = "iscsi_iser", 55665e7ae7bSOr Gerlitz .this_id = -1, 55765e7ae7bSOr Gerlitz }; 55865e7ae7bSOr Gerlitz 55965e7ae7bSOr Gerlitz static struct iscsi_transport iscsi_iser_transport = { 56065e7ae7bSOr Gerlitz .owner = THIS_MODULE, 56165e7ae7bSOr Gerlitz .name = "iser", 56265e7ae7bSOr Gerlitz .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T, 56365e7ae7bSOr Gerlitz .param_mask = ISCSI_MAX_RECV_DLENGTH | 56465e7ae7bSOr Gerlitz ISCSI_MAX_XMIT_DLENGTH | 56565e7ae7bSOr Gerlitz ISCSI_HDRDGST_EN | 56665e7ae7bSOr Gerlitz ISCSI_DATADGST_EN | 56765e7ae7bSOr Gerlitz ISCSI_INITIAL_R2T_EN | 56865e7ae7bSOr Gerlitz ISCSI_MAX_R2T | 56965e7ae7bSOr Gerlitz ISCSI_IMM_DATA_EN | 57065e7ae7bSOr Gerlitz ISCSI_FIRST_BURST | 57165e7ae7bSOr Gerlitz ISCSI_MAX_BURST | 57265e7ae7bSOr Gerlitz ISCSI_PDU_INORDER_EN | 573358ff019SMike Christie ISCSI_DATASEQ_INORDER_EN | 574358ff019SMike Christie ISCSI_EXP_STATSN | 575358ff019SMike Christie ISCSI_PERSISTENT_PORT | 576358ff019SMike Christie ISCSI_PERSISTENT_ADDRESS | 577358ff019SMike Christie ISCSI_TARGET_NAME | 578358ff019SMike Christie ISCSI_TPGT, 57965e7ae7bSOr Gerlitz .host_template = &iscsi_iser_sht, 58065e7ae7bSOr Gerlitz .conndata_size = sizeof(struct iscsi_conn), 58165e7ae7bSOr Gerlitz .max_lun = ISCSI_ISER_MAX_LUN, 58265e7ae7bSOr Gerlitz .max_cmd_len = ISCSI_ISER_MAX_CMD_LEN, 58365e7ae7bSOr Gerlitz /* session management */ 58465e7ae7bSOr Gerlitz .create_session = iscsi_iser_session_create, 58565e7ae7bSOr Gerlitz .destroy_session = iscsi_session_teardown, 58665e7ae7bSOr Gerlitz /* connection management */ 58765e7ae7bSOr Gerlitz .create_conn = iscsi_iser_conn_create, 58865e7ae7bSOr Gerlitz .bind_conn = iscsi_iser_conn_bind, 58965e7ae7bSOr Gerlitz .destroy_conn = iscsi_iser_conn_destroy, 590358ff019SMike Christie .set_param = iscsi_iser_set_param, 591358ff019SMike Christie .get_conn_param = iscsi_conn_get_param, 592358ff019SMike Christie .get_session_param = iscsi_session_get_param, 59365e7ae7bSOr Gerlitz .start_conn = iscsi_iser_conn_start, 59465e7ae7bSOr Gerlitz .stop_conn = iscsi_conn_stop, 59565e7ae7bSOr Gerlitz /* IO */ 59665e7ae7bSOr Gerlitz .send_pdu = iscsi_conn_send_pdu, 59765e7ae7bSOr Gerlitz .get_stats = iscsi_iser_conn_get_stats, 59865e7ae7bSOr Gerlitz .init_cmd_task = iscsi_iser_cmd_init, 59965e7ae7bSOr Gerlitz .xmit_cmd_task = iscsi_iser_ctask_xmit, 60065e7ae7bSOr Gerlitz .xmit_mgmt_task = iscsi_iser_mtask_xmit, 60165e7ae7bSOr Gerlitz .cleanup_cmd_task = iscsi_iser_cleanup_ctask, 60265e7ae7bSOr Gerlitz /* recovery */ 60365e7ae7bSOr Gerlitz .session_recovery_timedout = iscsi_session_recovery_timedout, 60465e7ae7bSOr Gerlitz 60565e7ae7bSOr Gerlitz .ep_connect = iscsi_iser_ep_connect, 60665e7ae7bSOr Gerlitz .ep_poll = iscsi_iser_ep_poll, 60765e7ae7bSOr Gerlitz .ep_disconnect = iscsi_iser_ep_disconnect 60865e7ae7bSOr Gerlitz }; 60965e7ae7bSOr Gerlitz 61065e7ae7bSOr Gerlitz static int __init iser_init(void) 61165e7ae7bSOr Gerlitz { 61265e7ae7bSOr Gerlitz int err; 61365e7ae7bSOr Gerlitz 61465e7ae7bSOr Gerlitz iser_dbg("Starting iSER datamover...\n"); 61565e7ae7bSOr Gerlitz 61665e7ae7bSOr Gerlitz if (iscsi_max_lun < 1) { 61765e7ae7bSOr Gerlitz printk(KERN_ERR "Invalid max_lun value of %u\n", iscsi_max_lun); 61865e7ae7bSOr Gerlitz return -EINVAL; 61965e7ae7bSOr Gerlitz } 62065e7ae7bSOr Gerlitz 62165e7ae7bSOr Gerlitz iscsi_iser_transport.max_lun = iscsi_max_lun; 62265e7ae7bSOr Gerlitz 62365e7ae7bSOr Gerlitz memset(&ig, 0, sizeof(struct iser_global)); 62465e7ae7bSOr Gerlitz 62565e7ae7bSOr Gerlitz ig.desc_cache = kmem_cache_create("iser_descriptors", 62665e7ae7bSOr Gerlitz sizeof (struct iser_desc), 62765e7ae7bSOr Gerlitz 0, SLAB_HWCACHE_ALIGN, 62865e7ae7bSOr Gerlitz NULL, NULL); 62965e7ae7bSOr Gerlitz if (ig.desc_cache == NULL) 63065e7ae7bSOr Gerlitz return -ENOMEM; 63165e7ae7bSOr Gerlitz 63265e7ae7bSOr Gerlitz /* device init is called only after the first addr resolution */ 63365e7ae7bSOr Gerlitz mutex_init(&ig.device_list_mutex); 63465e7ae7bSOr Gerlitz INIT_LIST_HEAD(&ig.device_list); 63565e7ae7bSOr Gerlitz mutex_init(&ig.connlist_mutex); 63665e7ae7bSOr Gerlitz INIT_LIST_HEAD(&ig.connlist); 63765e7ae7bSOr Gerlitz 63865e7ae7bSOr Gerlitz if (!iscsi_register_transport(&iscsi_iser_transport)) { 63965e7ae7bSOr Gerlitz iser_err("iscsi_register_transport failed\n"); 64065e7ae7bSOr Gerlitz err = -EINVAL; 64165e7ae7bSOr Gerlitz goto register_transport_failure; 64265e7ae7bSOr Gerlitz } 64365e7ae7bSOr Gerlitz 64465e7ae7bSOr Gerlitz return 0; 64565e7ae7bSOr Gerlitz 64665e7ae7bSOr Gerlitz register_transport_failure: 64765e7ae7bSOr Gerlitz kmem_cache_destroy(ig.desc_cache); 64865e7ae7bSOr Gerlitz 64965e7ae7bSOr Gerlitz return err; 65065e7ae7bSOr Gerlitz } 65165e7ae7bSOr Gerlitz 65265e7ae7bSOr Gerlitz static void __exit iser_exit(void) 65365e7ae7bSOr Gerlitz { 65465e7ae7bSOr Gerlitz iser_dbg("Removing iSER datamover...\n"); 65565e7ae7bSOr Gerlitz iscsi_unregister_transport(&iscsi_iser_transport); 65665e7ae7bSOr Gerlitz kmem_cache_destroy(ig.desc_cache); 65765e7ae7bSOr Gerlitz } 65865e7ae7bSOr Gerlitz 65965e7ae7bSOr Gerlitz module_init(iser_init); 66065e7ae7bSOr Gerlitz module_exit(iser_exit); 661