2 * iSCSI Initiator over iSER Data-Path
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
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:
16 * Redistribution and use in source and binary forms, with or
17 * without modification, are permitted provided that the following
20 * - Redistributions of source code must retain the above
21 * copyright notice, this list of conditions and the following
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.
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
47 * $Id: iscsi_iser.c 6965 2006-05-07 11:36:20Z ogerlitz $
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>
59 #include <linux/net.h>
60 #include <linux/scatterlist.h>
61 #include <linux/delay.h>
65 #include <asm/uaccess.h>
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>
75 #include "iscsi_iser.h"
77 static unsigned int iscsi_max_lun = 512;
78 module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
80 int iser_debug_level = 0;
82 MODULE_DESCRIPTION("iSER (iSCSI Extensions for RDMA) Datamover "
83 "v" DRV_VER " (" DRV_DATE ")");
84 MODULE_LICENSE("Dual BSD/GPL");
85 MODULE_AUTHOR("Alex Nezhinsky, Dan Bar Dov, Or Gerlitz");
87 module_param_named(debug_level, iser_debug_level, int, 0644);
88 MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0 (default:disabled)");
90 struct iser_global ig;
93 iscsi_iser_recv(struct iscsi_conn *conn,
94 struct iscsi_hdr *hdr, char *rx_data, int rx_data_len)
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;
111 ahslen = hdr->hlength * 4;
113 /* verify itt (itt encoding: age+cid+itt) */
114 rc = iscsi_verify_itt(conn, hdr, &ret_itt);
117 rc = iscsi_complete_pdu(conn, hdr, rx_data, rx_data_len);
119 if (rc && rc != ISCSI_ERR_NO_SCSI_CMD)
124 iscsi_conn_failure(conn, rc);
129 * iscsi_iser_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
133 iscsi_iser_cmd_init(struct iscsi_cmd_task *ctask)
135 struct iscsi_iser_conn *iser_conn = ctask->conn->dd_data;
136 struct iscsi_iser_cmd_task *iser_ctask = ctask->dd_data;
138 iser_ctask->command_sent = 0;
139 iser_ctask->iser_conn = iser_conn;
140 iser_ctask_rdma_init(iser_ctask);
144 * iscsi_mtask_xmit - xmit management(immediate) task
145 * @conn: iscsi connection
146 * @mtask: task management task
149 * The function can return -EAGAIN in which case caller must
150 * call it again later, or recover. '0' return code means successful
155 iscsi_iser_mtask_xmit(struct iscsi_conn *conn,
156 struct iscsi_mgmt_task *mtask)
160 debug_scsi("mtask deq [cid %d itt 0x%x]\n", conn->id, mtask->itt);
162 error = iser_send_control(conn, mtask);
164 /* since iser xmits control with zero copy, mtasks can not be recycled
165 * right after sending them.
166 * The recycling scheme is based on whether a response is expected
167 * - if yes, the mtask is recycled at iscsi_complete_pdu
168 * - if no, the mtask is recycled at iser_snd_completion
170 if (error && error != -ENOBUFS)
171 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
177 iscsi_iser_ctask_xmit_unsol_data(struct iscsi_conn *conn,
178 struct iscsi_cmd_task *ctask)
180 struct iscsi_data hdr;
183 /* Send data-out PDUs while there's still unsolicited data to send */
184 while (ctask->unsol_count > 0) {
185 iscsi_prep_unsolicit_data_pdu(ctask, &hdr);
186 debug_scsi("Sending data-out: itt 0x%x, data count %d\n",
187 hdr.itt, ctask->data_count);
189 /* the buffer description has been passed with the command */
190 /* Send the command */
191 error = iser_send_data_out(conn, ctask, &hdr);
193 ctask->unsol_datasn--;
194 goto iscsi_iser_ctask_xmit_unsol_data_exit;
196 ctask->unsol_count -= ctask->data_count;
197 debug_scsi("Need to send %d more as data-out PDUs\n",
201 iscsi_iser_ctask_xmit_unsol_data_exit:
206 iscsi_iser_ctask_xmit(struct iscsi_conn *conn,
207 struct iscsi_cmd_task *ctask)
209 struct iscsi_iser_cmd_task *iser_ctask = ctask->dd_data;
212 if (ctask->sc->sc_data_direction == DMA_TO_DEVICE) {
213 BUG_ON(scsi_bufflen(ctask->sc) == 0);
215 debug_scsi("cmd [itt %x total %d imm %d unsol_data %d\n",
216 ctask->itt, scsi_bufflen(ctask->sc),
217 ctask->imm_count, ctask->unsol_count);
220 debug_scsi("ctask deq [cid %d itt 0x%x]\n",
221 conn->id, ctask->itt);
223 /* Send the cmd PDU */
224 if (!iser_ctask->command_sent) {
225 error = iser_send_command(conn, ctask);
227 goto iscsi_iser_ctask_xmit_exit;
228 iser_ctask->command_sent = 1;
231 /* Send unsolicited data-out PDU(s) if necessary */
232 if (ctask->unsol_count)
233 error = iscsi_iser_ctask_xmit_unsol_data(conn, ctask);
235 iscsi_iser_ctask_xmit_exit:
236 if (error && error != -ENOBUFS)
237 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
242 iscsi_iser_cleanup_ctask(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
244 struct iscsi_iser_cmd_task *iser_ctask = ctask->dd_data;
246 if (iser_ctask->status == ISER_TASK_STATUS_STARTED) {
247 iser_ctask->status = ISER_TASK_STATUS_COMPLETED;
248 iser_ctask_rdma_finalize(iser_ctask);
252 static struct iser_conn *
253 iscsi_iser_ib_conn_lookup(__u64 ep_handle)
255 struct iser_conn *ib_conn;
256 struct iser_conn *uib_conn = (struct iser_conn *)(unsigned long)ep_handle;
258 mutex_lock(&ig.connlist_mutex);
259 list_for_each_entry(ib_conn, &ig.connlist, conn_list) {
260 if (ib_conn == uib_conn) {
261 mutex_unlock(&ig.connlist_mutex);
265 mutex_unlock(&ig.connlist_mutex);
266 iser_err("no conn exists for eph %llx\n",(unsigned long long)ep_handle);
270 static struct iscsi_cls_conn *
271 iscsi_iser_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
273 struct iscsi_conn *conn;
274 struct iscsi_cls_conn *cls_conn;
275 struct iscsi_iser_conn *iser_conn;
277 cls_conn = iscsi_conn_setup(cls_session, conn_idx);
280 conn = cls_conn->dd_data;
283 * due to issues with the login code re iser sematics
284 * this not set in iscsi_conn_setup - FIXME
286 conn->max_recv_dlength = 128;
288 iser_conn = kzalloc(sizeof(*iser_conn), GFP_KERNEL);
290 goto conn_alloc_fail;
292 /* currently this is the only field which need to be initiated */
293 rwlock_init(&iser_conn->lock);
295 conn->dd_data = iser_conn;
296 iser_conn->iscsi_conn = conn;
301 iscsi_conn_teardown(cls_conn);
306 iscsi_iser_conn_destroy(struct iscsi_cls_conn *cls_conn)
308 struct iscsi_conn *conn = cls_conn->dd_data;
309 struct iscsi_iser_conn *iser_conn = conn->dd_data;
311 iscsi_conn_teardown(cls_conn);
312 if (iser_conn->ib_conn)
313 iser_conn->ib_conn->iser_conn = NULL;
318 iscsi_iser_conn_bind(struct iscsi_cls_session *cls_session,
319 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
322 struct iscsi_conn *conn = cls_conn->dd_data;
323 struct iscsi_iser_conn *iser_conn;
324 struct iser_conn *ib_conn;
327 error = iscsi_conn_bind(cls_session, cls_conn, is_leading);
331 /* the transport ep handle comes from user space so it must be
332 * verified against the global ib connections list */
333 ib_conn = iscsi_iser_ib_conn_lookup(transport_eph);
335 iser_err("can't bind eph %llx\n",
336 (unsigned long long)transport_eph);
339 /* binds the iSER connection retrieved from the previously
340 * connected ep_handle to the iSCSI layer connection. exchanges
341 * connection pointers */
342 iser_err("binding iscsi conn %p to iser_conn %p\n",conn,ib_conn);
343 iser_conn = conn->dd_data;
344 ib_conn->iser_conn = iser_conn;
345 iser_conn->ib_conn = ib_conn;
347 conn->recv_lock = &iser_conn->lock;
353 iscsi_iser_conn_start(struct iscsi_cls_conn *cls_conn)
355 struct iscsi_conn *conn = cls_conn->dd_data;
358 err = iser_conn_set_full_featured_mode(conn);
362 return iscsi_conn_start(cls_conn);
365 static struct iscsi_transport iscsi_iser_transport;
367 static struct iscsi_cls_session *
368 iscsi_iser_session_create(struct iscsi_transport *iscsit,
369 struct scsi_transport_template *scsit,
370 uint16_t cmds_max, uint16_t qdepth,
371 uint32_t initial_cmdsn, uint32_t *hostno)
373 struct iscsi_cls_session *cls_session;
374 struct iscsi_session *session;
377 struct iscsi_cmd_task *ctask;
378 struct iscsi_mgmt_task *mtask;
379 struct iscsi_iser_cmd_task *iser_ctask;
380 struct iser_desc *desc;
383 * we do not support setting can_queue cmd_per_lun from userspace yet
384 * because we preallocate so many resources
386 cls_session = iscsi_session_setup(iscsit, scsit,
387 ISCSI_DEF_XMIT_CMDS_MAX,
388 ISCSI_MAX_CMD_PER_LUN,
389 sizeof(struct iscsi_iser_cmd_task),
390 sizeof(struct iser_desc),
396 session = class_to_transport_session(cls_session);
398 /* libiscsi setup itts, data and pool so just set desc fields */
399 for (i = 0; i < session->cmds_max; i++) {
400 ctask = session->cmds[i];
401 iser_ctask = ctask->dd_data;
402 ctask->hdr = (struct iscsi_cmd *)&iser_ctask->desc.iscsi_header;
405 for (i = 0; i < session->mgmtpool_max; i++) {
406 mtask = session->mgmt_cmds[i];
407 desc = mtask->dd_data;
408 mtask->hdr = &desc->iscsi_header;
409 desc->data = mtask->data;
416 iscsi_iser_set_param(struct iscsi_cls_conn *cls_conn,
417 enum iscsi_param param, char *buf, int buflen)
422 case ISCSI_PARAM_MAX_RECV_DLENGTH:
425 case ISCSI_PARAM_HDRDGST_EN:
426 sscanf(buf, "%d", &value);
428 printk(KERN_ERR "DataDigest wasn't negotiated to None");
432 case ISCSI_PARAM_DATADGST_EN:
433 sscanf(buf, "%d", &value);
435 printk(KERN_ERR "DataDigest wasn't negotiated to None");
439 case ISCSI_PARAM_IFMARKER_EN:
440 sscanf(buf, "%d", &value);
442 printk(KERN_ERR "IFMarker wasn't negotiated to No");
446 case ISCSI_PARAM_OFMARKER_EN:
447 sscanf(buf, "%d", &value);
449 printk(KERN_ERR "OFMarker wasn't negotiated to No");
454 return iscsi_set_param(cls_conn, param, buf, buflen);
461 iscsi_iser_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
463 struct iscsi_conn *conn = cls_conn->dd_data;
465 stats->txdata_octets = conn->txdata_octets;
466 stats->rxdata_octets = conn->rxdata_octets;
467 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
468 stats->dataout_pdus = conn->dataout_pdus_cnt;
469 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
470 stats->datain_pdus = conn->datain_pdus_cnt; /* always 0 */
471 stats->r2t_pdus = conn->r2t_pdus_cnt; /* always 0 */
472 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
473 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
474 stats->custom_length = 3;
475 strcpy(stats->custom[0].desc, "qp_tx_queue_full");
476 stats->custom[0].value = 0; /* TB iser_conn->qp_tx_queue_full; */
477 strcpy(stats->custom[1].desc, "fmr_map_not_avail");
478 stats->custom[1].value = 0; /* TB iser_conn->fmr_map_not_avail */;
479 strcpy(stats->custom[2].desc, "eh_abort_cnt");
480 stats->custom[2].value = conn->eh_abort_cnt;
484 iscsi_iser_ep_connect(struct sockaddr *dst_addr, int non_blocking,
488 struct iser_conn *ib_conn;
490 err = iser_conn_init(&ib_conn);
494 err = iser_connect(ib_conn, NULL, (struct sockaddr_in *)dst_addr, non_blocking);
496 *ep_handle = (__u64)(unsigned long)ib_conn;
503 iscsi_iser_ep_poll(__u64 ep_handle, int timeout_ms)
505 struct iser_conn *ib_conn = iscsi_iser_ib_conn_lookup(ep_handle);
511 rc = wait_event_interruptible_timeout(ib_conn->wait,
512 ib_conn->state == ISER_CONN_UP,
513 msecs_to_jiffies(timeout_ms));
515 /* if conn establishment failed, return error code to iscsi */
517 (ib_conn->state == ISER_CONN_TERMINATING ||
518 ib_conn->state == ISER_CONN_DOWN))
521 iser_err("ib conn %p rc = %d\n", ib_conn, rc);
524 return 1; /* success, this is the equivalent of POLLOUT */
526 return 0; /* timeout */
528 return rc; /* signal */
532 iscsi_iser_ep_disconnect(__u64 ep_handle)
534 struct iser_conn *ib_conn;
536 ib_conn = iscsi_iser_ib_conn_lookup(ep_handle);
540 iser_err("ib conn %p state %d\n",ib_conn, ib_conn->state);
541 iser_conn_terminate(ib_conn);
544 static struct scsi_host_template iscsi_iser_sht = {
545 .module = THIS_MODULE,
546 .name = "iSCSI Initiator over iSER, v." DRV_VER,
547 .queuecommand = iscsi_queuecommand,
548 .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
549 .sg_tablesize = ISCSI_ISER_SG_TABLESIZE,
551 .cmd_per_lun = ISCSI_MAX_CMD_PER_LUN,
552 .eh_abort_handler = iscsi_eh_abort,
553 .eh_host_reset_handler = iscsi_eh_host_reset,
554 .use_clustering = DISABLE_CLUSTERING,
555 .proc_name = "iscsi_iser",
559 static struct iscsi_transport iscsi_iser_transport = {
560 .owner = THIS_MODULE,
562 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T,
563 .param_mask = ISCSI_MAX_RECV_DLENGTH |
564 ISCSI_MAX_XMIT_DLENGTH |
567 ISCSI_INITIAL_R2T_EN |
572 ISCSI_PDU_INORDER_EN |
573 ISCSI_DATASEQ_INORDER_EN |
575 ISCSI_PERSISTENT_PORT |
576 ISCSI_PERSISTENT_ADDRESS |
577 ISCSI_TARGET_NAME | ISCSI_TPGT |
578 ISCSI_USERNAME | ISCSI_PASSWORD |
579 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN,
580 .host_param_mask = ISCSI_HOST_HWADDRESS |
581 ISCSI_HOST_NETDEV_NAME |
582 ISCSI_HOST_INITIATOR_NAME,
583 .host_template = &iscsi_iser_sht,
584 .conndata_size = sizeof(struct iscsi_conn),
585 .max_lun = ISCSI_ISER_MAX_LUN,
586 .max_cmd_len = ISCSI_ISER_MAX_CMD_LEN,
587 /* session management */
588 .create_session = iscsi_iser_session_create,
589 .destroy_session = iscsi_session_teardown,
590 /* connection management */
591 .create_conn = iscsi_iser_conn_create,
592 .bind_conn = iscsi_iser_conn_bind,
593 .destroy_conn = iscsi_iser_conn_destroy,
594 .set_param = iscsi_iser_set_param,
595 .get_conn_param = iscsi_conn_get_param,
596 .get_session_param = iscsi_session_get_param,
597 .start_conn = iscsi_iser_conn_start,
598 .stop_conn = iscsi_conn_stop,
599 /* iscsi host params */
600 .get_host_param = iscsi_host_get_param,
601 .set_host_param = iscsi_host_set_param,
603 .send_pdu = iscsi_conn_send_pdu,
604 .get_stats = iscsi_iser_conn_get_stats,
605 .init_cmd_task = iscsi_iser_cmd_init,
606 .xmit_cmd_task = iscsi_iser_ctask_xmit,
607 .xmit_mgmt_task = iscsi_iser_mtask_xmit,
608 .cleanup_cmd_task = iscsi_iser_cleanup_ctask,
610 .session_recovery_timedout = iscsi_session_recovery_timedout,
612 .ep_connect = iscsi_iser_ep_connect,
613 .ep_poll = iscsi_iser_ep_poll,
614 .ep_disconnect = iscsi_iser_ep_disconnect
617 static int __init iser_init(void)
621 iser_dbg("Starting iSER datamover...\n");
623 if (iscsi_max_lun < 1) {
624 printk(KERN_ERR "Invalid max_lun value of %u\n", iscsi_max_lun);
628 iscsi_iser_transport.max_lun = iscsi_max_lun;
630 memset(&ig, 0, sizeof(struct iser_global));
632 ig.desc_cache = kmem_cache_create("iser_descriptors",
633 sizeof (struct iser_desc),
634 0, SLAB_HWCACHE_ALIGN,
636 if (ig.desc_cache == NULL)
639 /* device init is called only after the first addr resolution */
640 mutex_init(&ig.device_list_mutex);
641 INIT_LIST_HEAD(&ig.device_list);
642 mutex_init(&ig.connlist_mutex);
643 INIT_LIST_HEAD(&ig.connlist);
645 if (!iscsi_register_transport(&iscsi_iser_transport)) {
646 iser_err("iscsi_register_transport failed\n");
648 goto register_transport_failure;
653 register_transport_failure:
654 kmem_cache_destroy(ig.desc_cache);
659 static void __exit iser_exit(void)
661 iser_dbg("Removing iSER datamover...\n");
662 iscsi_unregister_transport(&iscsi_iser_transport);
663 kmem_cache_destroy(ig.desc_cache);
666 module_init(iser_init);
667 module_exit(iser_exit);