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); 32065e7ae7bSOr Gerlitz kfree(iser_conn); 32165e7ae7bSOr Gerlitz } 32265e7ae7bSOr Gerlitz 32365e7ae7bSOr Gerlitz static int 32465e7ae7bSOr Gerlitz iscsi_iser_conn_bind(struct iscsi_cls_session *cls_session, 32565e7ae7bSOr Gerlitz struct iscsi_cls_conn *cls_conn, uint64_t transport_eph, 32665e7ae7bSOr Gerlitz int is_leading) 32765e7ae7bSOr Gerlitz { 32865e7ae7bSOr Gerlitz struct iscsi_conn *conn = cls_conn->dd_data; 32965e7ae7bSOr Gerlitz struct iscsi_iser_conn *iser_conn; 33065e7ae7bSOr Gerlitz struct iser_conn *ib_conn; 33165e7ae7bSOr Gerlitz int error; 33265e7ae7bSOr Gerlitz 33365e7ae7bSOr Gerlitz error = iscsi_conn_bind(cls_session, cls_conn, is_leading); 33465e7ae7bSOr Gerlitz if (error) 33565e7ae7bSOr Gerlitz return error; 33665e7ae7bSOr Gerlitz 33765e7ae7bSOr Gerlitz /* the transport ep handle comes from user space so it must be 33865e7ae7bSOr Gerlitz * verified against the global ib connections list */ 33965e7ae7bSOr Gerlitz ib_conn = iscsi_iser_ib_conn_lookup(transport_eph); 34065e7ae7bSOr Gerlitz if (!ib_conn) { 34165e7ae7bSOr Gerlitz iser_err("can't bind eph %llx\n", 34265e7ae7bSOr Gerlitz (unsigned long long)transport_eph); 34365e7ae7bSOr Gerlitz return -EINVAL; 34465e7ae7bSOr Gerlitz } 34565e7ae7bSOr Gerlitz /* binds the iSER connection retrieved from the previously 34665e7ae7bSOr Gerlitz * connected ep_handle to the iSCSI layer connection. exchanges 34765e7ae7bSOr Gerlitz * connection pointers */ 34865e7ae7bSOr Gerlitz iser_err("binding iscsi conn %p to iser_conn %p\n",conn,ib_conn); 34965e7ae7bSOr Gerlitz iser_conn = conn->dd_data; 35065e7ae7bSOr Gerlitz ib_conn->iser_conn = iser_conn; 35165e7ae7bSOr Gerlitz iser_conn->ib_conn = ib_conn; 35265e7ae7bSOr Gerlitz 35365e7ae7bSOr Gerlitz conn->recv_lock = &iser_conn->lock; 35465e7ae7bSOr Gerlitz 35565e7ae7bSOr Gerlitz return 0; 35665e7ae7bSOr Gerlitz } 35765e7ae7bSOr Gerlitz 35865e7ae7bSOr Gerlitz static int 35965e7ae7bSOr Gerlitz iscsi_iser_conn_start(struct iscsi_cls_conn *cls_conn) 36065e7ae7bSOr Gerlitz { 36165e7ae7bSOr Gerlitz struct iscsi_conn *conn = cls_conn->dd_data; 36265e7ae7bSOr Gerlitz int err; 36365e7ae7bSOr Gerlitz 36465e7ae7bSOr Gerlitz err = iscsi_conn_start(cls_conn); 36565e7ae7bSOr Gerlitz if (err) 36665e7ae7bSOr Gerlitz return err; 36765e7ae7bSOr Gerlitz 36865e7ae7bSOr Gerlitz return iser_conn_set_full_featured_mode(conn); 36965e7ae7bSOr Gerlitz } 37065e7ae7bSOr Gerlitz 37165e7ae7bSOr Gerlitz static struct iscsi_transport iscsi_iser_transport; 37265e7ae7bSOr Gerlitz 37365e7ae7bSOr Gerlitz static struct iscsi_cls_session * 37465e7ae7bSOr Gerlitz iscsi_iser_session_create(struct iscsi_transport *iscsit, 37565e7ae7bSOr Gerlitz struct scsi_transport_template *scsit, 37665e7ae7bSOr Gerlitz uint32_t initial_cmdsn, uint32_t *hostno) 37765e7ae7bSOr Gerlitz { 37865e7ae7bSOr Gerlitz struct iscsi_cls_session *cls_session; 37965e7ae7bSOr Gerlitz struct iscsi_session *session; 38065e7ae7bSOr Gerlitz int i; 38165e7ae7bSOr Gerlitz uint32_t hn; 38265e7ae7bSOr Gerlitz struct iscsi_cmd_task *ctask; 38365e7ae7bSOr Gerlitz struct iscsi_mgmt_task *mtask; 38465e7ae7bSOr Gerlitz struct iscsi_iser_cmd_task *iser_ctask; 38565e7ae7bSOr Gerlitz struct iser_desc *desc; 38665e7ae7bSOr Gerlitz 38765e7ae7bSOr Gerlitz cls_session = iscsi_session_setup(iscsit, scsit, 38865e7ae7bSOr Gerlitz sizeof(struct iscsi_iser_cmd_task), 38965e7ae7bSOr Gerlitz sizeof(struct iser_desc), 39065e7ae7bSOr Gerlitz initial_cmdsn, &hn); 39165e7ae7bSOr Gerlitz if (!cls_session) 39265e7ae7bSOr Gerlitz return NULL; 39365e7ae7bSOr Gerlitz 39465e7ae7bSOr Gerlitz *hostno = hn; 39565e7ae7bSOr Gerlitz session = class_to_transport_session(cls_session); 39665e7ae7bSOr Gerlitz 39765e7ae7bSOr Gerlitz /* libiscsi setup itts, data and pool so just set desc fields */ 39865e7ae7bSOr Gerlitz for (i = 0; i < session->cmds_max; i++) { 39965e7ae7bSOr Gerlitz ctask = session->cmds[i]; 40065e7ae7bSOr Gerlitz iser_ctask = ctask->dd_data; 40165e7ae7bSOr Gerlitz ctask->hdr = (struct iscsi_cmd *)&iser_ctask->desc.iscsi_header; 40265e7ae7bSOr Gerlitz } 40365e7ae7bSOr Gerlitz 40465e7ae7bSOr Gerlitz for (i = 0; i < session->mgmtpool_max; i++) { 40565e7ae7bSOr Gerlitz mtask = session->mgmt_cmds[i]; 40665e7ae7bSOr Gerlitz desc = mtask->dd_data; 40765e7ae7bSOr Gerlitz mtask->hdr = &desc->iscsi_header; 40865e7ae7bSOr Gerlitz desc->data = mtask->data; 40965e7ae7bSOr Gerlitz } 41065e7ae7bSOr Gerlitz 41165e7ae7bSOr Gerlitz return cls_session; 41265e7ae7bSOr Gerlitz } 41365e7ae7bSOr Gerlitz 41465e7ae7bSOr Gerlitz static int 415358ff019SMike Christie iscsi_iser_set_param(struct iscsi_cls_conn *cls_conn, 416358ff019SMike Christie enum iscsi_param param, char *buf, int buflen) 41765e7ae7bSOr Gerlitz { 418358ff019SMike Christie int value; 41965e7ae7bSOr Gerlitz 42065e7ae7bSOr Gerlitz switch (param) { 42165e7ae7bSOr Gerlitz case ISCSI_PARAM_MAX_RECV_DLENGTH: 42265e7ae7bSOr Gerlitz /* TBD */ 42365e7ae7bSOr Gerlitz break; 42465e7ae7bSOr Gerlitz case ISCSI_PARAM_HDRDGST_EN: 425358ff019SMike Christie sscanf(buf, "%d", &value); 42665e7ae7bSOr Gerlitz if (value) { 42765e7ae7bSOr Gerlitz printk(KERN_ERR "DataDigest wasn't negotiated to None"); 42865e7ae7bSOr Gerlitz return -EPROTO; 42965e7ae7bSOr Gerlitz } 43065e7ae7bSOr Gerlitz break; 43165e7ae7bSOr Gerlitz case ISCSI_PARAM_DATADGST_EN: 432358ff019SMike Christie sscanf(buf, "%d", &value); 43365e7ae7bSOr Gerlitz if (value) { 43465e7ae7bSOr Gerlitz printk(KERN_ERR "DataDigest wasn't negotiated to None"); 43565e7ae7bSOr Gerlitz return -EPROTO; 43665e7ae7bSOr Gerlitz } 43765e7ae7bSOr Gerlitz break; 43865e7ae7bSOr Gerlitz case ISCSI_PARAM_IFMARKER_EN: 439358ff019SMike Christie sscanf(buf, "%d", &value); 44065e7ae7bSOr Gerlitz if (value) { 44165e7ae7bSOr Gerlitz printk(KERN_ERR "IFMarker wasn't negotiated to No"); 44265e7ae7bSOr Gerlitz return -EPROTO; 44365e7ae7bSOr Gerlitz } 44465e7ae7bSOr Gerlitz break; 44565e7ae7bSOr Gerlitz case ISCSI_PARAM_OFMARKER_EN: 446358ff019SMike Christie sscanf(buf, "%d", &value); 44765e7ae7bSOr Gerlitz if (value) { 44865e7ae7bSOr Gerlitz printk(KERN_ERR "OFMarker wasn't negotiated to No"); 44965e7ae7bSOr Gerlitz return -EPROTO; 45065e7ae7bSOr Gerlitz } 45165e7ae7bSOr Gerlitz break; 45265e7ae7bSOr Gerlitz default: 453358ff019SMike Christie return iscsi_set_param(cls_conn, param, buf, buflen); 45465e7ae7bSOr Gerlitz } 45565e7ae7bSOr Gerlitz 45665e7ae7bSOr Gerlitz return 0; 45765e7ae7bSOr Gerlitz } 45865e7ae7bSOr Gerlitz 45965e7ae7bSOr Gerlitz static void 46065e7ae7bSOr Gerlitz iscsi_iser_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats) 46165e7ae7bSOr Gerlitz { 46265e7ae7bSOr Gerlitz struct iscsi_conn *conn = cls_conn->dd_data; 46365e7ae7bSOr Gerlitz 46465e7ae7bSOr Gerlitz stats->txdata_octets = conn->txdata_octets; 46565e7ae7bSOr Gerlitz stats->rxdata_octets = conn->rxdata_octets; 46665e7ae7bSOr Gerlitz stats->scsicmd_pdus = conn->scsicmd_pdus_cnt; 46765e7ae7bSOr Gerlitz stats->dataout_pdus = conn->dataout_pdus_cnt; 46865e7ae7bSOr Gerlitz stats->scsirsp_pdus = conn->scsirsp_pdus_cnt; 46965e7ae7bSOr Gerlitz stats->datain_pdus = conn->datain_pdus_cnt; /* always 0 */ 47065e7ae7bSOr Gerlitz stats->r2t_pdus = conn->r2t_pdus_cnt; /* always 0 */ 47165e7ae7bSOr Gerlitz stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt; 47265e7ae7bSOr Gerlitz stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt; 47365e7ae7bSOr Gerlitz stats->custom_length = 3; 47465e7ae7bSOr Gerlitz strcpy(stats->custom[0].desc, "qp_tx_queue_full"); 47565e7ae7bSOr Gerlitz stats->custom[0].value = 0; /* TB iser_conn->qp_tx_queue_full; */ 47665e7ae7bSOr Gerlitz strcpy(stats->custom[1].desc, "fmr_map_not_avail"); 47765e7ae7bSOr Gerlitz stats->custom[1].value = 0; /* TB iser_conn->fmr_map_not_avail */; 47865e7ae7bSOr Gerlitz strcpy(stats->custom[2].desc, "eh_abort_cnt"); 47965e7ae7bSOr Gerlitz stats->custom[2].value = conn->eh_abort_cnt; 48065e7ae7bSOr Gerlitz } 48165e7ae7bSOr Gerlitz 48265e7ae7bSOr Gerlitz static int 48365e7ae7bSOr Gerlitz iscsi_iser_ep_connect(struct sockaddr *dst_addr, int non_blocking, 48465e7ae7bSOr Gerlitz __u64 *ep_handle) 48565e7ae7bSOr Gerlitz { 48665e7ae7bSOr Gerlitz int err; 48765e7ae7bSOr Gerlitz struct iser_conn *ib_conn; 48865e7ae7bSOr Gerlitz 48965e7ae7bSOr Gerlitz err = iser_conn_init(&ib_conn); 49065e7ae7bSOr Gerlitz if (err) 49165e7ae7bSOr Gerlitz goto out; 49265e7ae7bSOr Gerlitz 49365e7ae7bSOr Gerlitz err = iser_connect(ib_conn, NULL, (struct sockaddr_in *)dst_addr, non_blocking); 49465e7ae7bSOr Gerlitz if (!err) 49565e7ae7bSOr Gerlitz *ep_handle = (__u64)(unsigned long)ib_conn; 49665e7ae7bSOr Gerlitz 49765e7ae7bSOr Gerlitz out: 49865e7ae7bSOr Gerlitz return err; 49965e7ae7bSOr Gerlitz } 50065e7ae7bSOr Gerlitz 50165e7ae7bSOr Gerlitz static int 50265e7ae7bSOr Gerlitz iscsi_iser_ep_poll(__u64 ep_handle, int timeout_ms) 50365e7ae7bSOr Gerlitz { 50465e7ae7bSOr Gerlitz struct iser_conn *ib_conn = iscsi_iser_ib_conn_lookup(ep_handle); 50565e7ae7bSOr Gerlitz int rc; 50665e7ae7bSOr Gerlitz 50765e7ae7bSOr Gerlitz if (!ib_conn) 50865e7ae7bSOr Gerlitz return -EINVAL; 50965e7ae7bSOr Gerlitz 51065e7ae7bSOr Gerlitz rc = wait_event_interruptible_timeout(ib_conn->wait, 51165e7ae7bSOr Gerlitz ib_conn->state == ISER_CONN_UP, 51265e7ae7bSOr Gerlitz msecs_to_jiffies(timeout_ms)); 51365e7ae7bSOr Gerlitz 51465e7ae7bSOr Gerlitz /* if conn establishment failed, return error code to iscsi */ 51565e7ae7bSOr Gerlitz if (!rc && 51665e7ae7bSOr Gerlitz (ib_conn->state == ISER_CONN_TERMINATING || 51765e7ae7bSOr Gerlitz ib_conn->state == ISER_CONN_DOWN)) 51865e7ae7bSOr Gerlitz rc = -1; 51965e7ae7bSOr Gerlitz 52065e7ae7bSOr Gerlitz iser_err("ib conn %p rc = %d\n", ib_conn, rc); 52165e7ae7bSOr Gerlitz 52265e7ae7bSOr Gerlitz if (rc > 0) 52365e7ae7bSOr Gerlitz return 1; /* success, this is the equivalent of POLLOUT */ 52465e7ae7bSOr Gerlitz else if (!rc) 52565e7ae7bSOr Gerlitz return 0; /* timeout */ 52665e7ae7bSOr Gerlitz else 52765e7ae7bSOr Gerlitz return rc; /* signal */ 52865e7ae7bSOr Gerlitz } 52965e7ae7bSOr Gerlitz 53065e7ae7bSOr Gerlitz static void 53165e7ae7bSOr Gerlitz iscsi_iser_ep_disconnect(__u64 ep_handle) 53265e7ae7bSOr Gerlitz { 5331c83469dSMike Christie struct iser_conn *ib_conn; 53465e7ae7bSOr Gerlitz 5351c83469dSMike Christie ib_conn = iscsi_iser_ib_conn_lookup(ep_handle); 53665e7ae7bSOr Gerlitz if (!ib_conn) 53765e7ae7bSOr Gerlitz return; 53865e7ae7bSOr Gerlitz 53965e7ae7bSOr Gerlitz iser_err("ib conn %p state %d\n",ib_conn, ib_conn->state); 54065e7ae7bSOr Gerlitz iser_conn_terminate(ib_conn); 54165e7ae7bSOr Gerlitz } 54265e7ae7bSOr Gerlitz 54365e7ae7bSOr Gerlitz static struct scsi_host_template iscsi_iser_sht = { 54465e7ae7bSOr Gerlitz .name = "iSCSI Initiator over iSER, v." DRV_VER, 54565e7ae7bSOr Gerlitz .queuecommand = iscsi_queuecommand, 54665e7ae7bSOr Gerlitz .can_queue = ISCSI_XMIT_CMDS_MAX - 1, 54765e7ae7bSOr Gerlitz .sg_tablesize = ISCSI_ISER_SG_TABLESIZE, 54865e7ae7bSOr Gerlitz .cmd_per_lun = ISCSI_MAX_CMD_PER_LUN, 54965e7ae7bSOr Gerlitz .eh_abort_handler = iscsi_eh_abort, 55065e7ae7bSOr Gerlitz .eh_host_reset_handler = iscsi_eh_host_reset, 55165e7ae7bSOr Gerlitz .use_clustering = DISABLE_CLUSTERING, 55265e7ae7bSOr Gerlitz .proc_name = "iscsi_iser", 55365e7ae7bSOr Gerlitz .this_id = -1, 55465e7ae7bSOr Gerlitz }; 55565e7ae7bSOr Gerlitz 55665e7ae7bSOr Gerlitz static struct iscsi_transport iscsi_iser_transport = { 55765e7ae7bSOr Gerlitz .owner = THIS_MODULE, 55865e7ae7bSOr Gerlitz .name = "iser", 55965e7ae7bSOr Gerlitz .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T, 56065e7ae7bSOr Gerlitz .param_mask = ISCSI_MAX_RECV_DLENGTH | 56165e7ae7bSOr Gerlitz ISCSI_MAX_XMIT_DLENGTH | 56265e7ae7bSOr Gerlitz ISCSI_HDRDGST_EN | 56365e7ae7bSOr Gerlitz ISCSI_DATADGST_EN | 56465e7ae7bSOr Gerlitz ISCSI_INITIAL_R2T_EN | 56565e7ae7bSOr Gerlitz ISCSI_MAX_R2T | 56665e7ae7bSOr Gerlitz ISCSI_IMM_DATA_EN | 56765e7ae7bSOr Gerlitz ISCSI_FIRST_BURST | 56865e7ae7bSOr Gerlitz ISCSI_MAX_BURST | 56965e7ae7bSOr Gerlitz ISCSI_PDU_INORDER_EN | 570358ff019SMike Christie ISCSI_DATASEQ_INORDER_EN | 571358ff019SMike Christie ISCSI_EXP_STATSN | 572358ff019SMike Christie ISCSI_PERSISTENT_PORT | 573358ff019SMike Christie ISCSI_PERSISTENT_ADDRESS | 574358ff019SMike Christie ISCSI_TARGET_NAME | 575358ff019SMike Christie ISCSI_TPGT, 57665e7ae7bSOr Gerlitz .host_template = &iscsi_iser_sht, 57765e7ae7bSOr Gerlitz .conndata_size = sizeof(struct iscsi_conn), 57865e7ae7bSOr Gerlitz .max_lun = ISCSI_ISER_MAX_LUN, 57965e7ae7bSOr Gerlitz .max_cmd_len = ISCSI_ISER_MAX_CMD_LEN, 58065e7ae7bSOr Gerlitz /* session management */ 58165e7ae7bSOr Gerlitz .create_session = iscsi_iser_session_create, 58265e7ae7bSOr Gerlitz .destroy_session = iscsi_session_teardown, 58365e7ae7bSOr Gerlitz /* connection management */ 58465e7ae7bSOr Gerlitz .create_conn = iscsi_iser_conn_create, 58565e7ae7bSOr Gerlitz .bind_conn = iscsi_iser_conn_bind, 58665e7ae7bSOr Gerlitz .destroy_conn = iscsi_iser_conn_destroy, 587358ff019SMike Christie .set_param = iscsi_iser_set_param, 588358ff019SMike Christie .get_conn_param = iscsi_conn_get_param, 589358ff019SMike Christie .get_session_param = iscsi_session_get_param, 59065e7ae7bSOr Gerlitz .start_conn = iscsi_iser_conn_start, 59165e7ae7bSOr Gerlitz .stop_conn = iscsi_conn_stop, 59265e7ae7bSOr Gerlitz /* IO */ 59365e7ae7bSOr Gerlitz .send_pdu = iscsi_conn_send_pdu, 59465e7ae7bSOr Gerlitz .get_stats = iscsi_iser_conn_get_stats, 59565e7ae7bSOr Gerlitz .init_cmd_task = iscsi_iser_cmd_init, 59665e7ae7bSOr Gerlitz .xmit_cmd_task = iscsi_iser_ctask_xmit, 59765e7ae7bSOr Gerlitz .xmit_mgmt_task = iscsi_iser_mtask_xmit, 59865e7ae7bSOr Gerlitz .cleanup_cmd_task = iscsi_iser_cleanup_ctask, 59965e7ae7bSOr Gerlitz /* recovery */ 60065e7ae7bSOr Gerlitz .session_recovery_timedout = iscsi_session_recovery_timedout, 60165e7ae7bSOr Gerlitz 60265e7ae7bSOr Gerlitz .ep_connect = iscsi_iser_ep_connect, 60365e7ae7bSOr Gerlitz .ep_poll = iscsi_iser_ep_poll, 60465e7ae7bSOr Gerlitz .ep_disconnect = iscsi_iser_ep_disconnect 60565e7ae7bSOr Gerlitz }; 60665e7ae7bSOr Gerlitz 60765e7ae7bSOr Gerlitz static int __init iser_init(void) 60865e7ae7bSOr Gerlitz { 60965e7ae7bSOr Gerlitz int err; 61065e7ae7bSOr Gerlitz 61165e7ae7bSOr Gerlitz iser_dbg("Starting iSER datamover...\n"); 61265e7ae7bSOr Gerlitz 61365e7ae7bSOr Gerlitz if (iscsi_max_lun < 1) { 61465e7ae7bSOr Gerlitz printk(KERN_ERR "Invalid max_lun value of %u\n", iscsi_max_lun); 61565e7ae7bSOr Gerlitz return -EINVAL; 61665e7ae7bSOr Gerlitz } 61765e7ae7bSOr Gerlitz 61865e7ae7bSOr Gerlitz iscsi_iser_transport.max_lun = iscsi_max_lun; 61965e7ae7bSOr Gerlitz 62065e7ae7bSOr Gerlitz memset(&ig, 0, sizeof(struct iser_global)); 62165e7ae7bSOr Gerlitz 62265e7ae7bSOr Gerlitz ig.desc_cache = kmem_cache_create("iser_descriptors", 62365e7ae7bSOr Gerlitz sizeof (struct iser_desc), 62465e7ae7bSOr Gerlitz 0, SLAB_HWCACHE_ALIGN, 62565e7ae7bSOr Gerlitz NULL, NULL); 62665e7ae7bSOr Gerlitz if (ig.desc_cache == NULL) 62765e7ae7bSOr Gerlitz return -ENOMEM; 62865e7ae7bSOr Gerlitz 62965e7ae7bSOr Gerlitz /* device init is called only after the first addr resolution */ 63065e7ae7bSOr Gerlitz mutex_init(&ig.device_list_mutex); 63165e7ae7bSOr Gerlitz INIT_LIST_HEAD(&ig.device_list); 63265e7ae7bSOr Gerlitz mutex_init(&ig.connlist_mutex); 63365e7ae7bSOr Gerlitz INIT_LIST_HEAD(&ig.connlist); 63465e7ae7bSOr Gerlitz 63565e7ae7bSOr Gerlitz if (!iscsi_register_transport(&iscsi_iser_transport)) { 63665e7ae7bSOr Gerlitz iser_err("iscsi_register_transport failed\n"); 63765e7ae7bSOr Gerlitz err = -EINVAL; 63865e7ae7bSOr Gerlitz goto register_transport_failure; 63965e7ae7bSOr Gerlitz } 64065e7ae7bSOr Gerlitz 64165e7ae7bSOr Gerlitz return 0; 64265e7ae7bSOr Gerlitz 64365e7ae7bSOr Gerlitz register_transport_failure: 64465e7ae7bSOr Gerlitz kmem_cache_destroy(ig.desc_cache); 64565e7ae7bSOr Gerlitz 64665e7ae7bSOr Gerlitz return err; 64765e7ae7bSOr Gerlitz } 64865e7ae7bSOr Gerlitz 64965e7ae7bSOr Gerlitz static void __exit iser_exit(void) 65065e7ae7bSOr Gerlitz { 65165e7ae7bSOr Gerlitz iser_dbg("Removing iSER datamover...\n"); 65265e7ae7bSOr Gerlitz iscsi_unregister_transport(&iscsi_iser_transport); 65365e7ae7bSOr Gerlitz kmem_cache_destroy(ig.desc_cache); 65465e7ae7bSOr Gerlitz } 65565e7ae7bSOr Gerlitz 65665e7ae7bSOr Gerlitz module_init(iser_init); 65765e7ae7bSOr Gerlitz module_exit(iser_exit); 658