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 * Copyright (c) 2013-2014 Mellanox Technologies. All rights reserved.
9 * maintained by openib-general@openib.org
11 * This software is available to you under a choice of one of two
12 * licenses. You may choose to be licensed under the terms of the GNU
13 * General Public License (GPL) Version 2, available from the file
14 * COPYING in the main directory of this source tree, or the
15 * OpenIB.org BSD license below:
17 * Redistribution and use in source and binary forms, with or
18 * without modification, are permitted provided that the following
21 * - Redistributions of source code must retain the above
22 * copyright notice, this list of conditions and the following
25 * - Redistributions in binary form must reproduce the above
26 * copyright notice, this list of conditions and the following
27 * disclaimer in the documentation and/or other materials
28 * provided with the distribution.
30 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
33 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
34 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
35 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
36 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
48 #include <linux/types.h>
49 #include <linux/list.h>
50 #include <linux/hardirq.h>
51 #include <linux/kfifo.h>
52 #include <linux/blkdev.h>
53 #include <linux/init.h>
54 #include <linux/ioctl.h>
55 #include <linux/cdev.h>
57 #include <linux/net.h>
58 #include <linux/scatterlist.h>
59 #include <linux/delay.h>
60 #include <linux/slab.h>
61 #include <linux/module.h>
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 struct scsi_host_template iscsi_iser_sht;
78 static struct iscsi_transport iscsi_iser_transport;
79 static struct scsi_transport_template *iscsi_iser_scsi_transport;
81 static unsigned int iscsi_max_lun = 512;
82 module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
84 int iser_debug_level = 0;
85 bool iser_pi_enable = false;
86 int iser_pi_guard = 1;
88 MODULE_DESCRIPTION("iSER (iSCSI Extensions for RDMA) Datamover");
89 MODULE_LICENSE("Dual BSD/GPL");
90 MODULE_AUTHOR("Alex Nezhinsky, Dan Bar Dov, Or Gerlitz");
91 MODULE_VERSION(DRV_VER);
93 module_param_named(debug_level, iser_debug_level, int, 0644);
94 MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0 (default:disabled)");
96 module_param_named(pi_enable, iser_pi_enable, bool, 0644);
97 MODULE_PARM_DESC(pi_enable, "Enable T10-PI offload support (default:disabled)");
99 module_param_named(pi_guard, iser_pi_guard, int, 0644);
100 MODULE_PARM_DESC(pi_guard, "T10-PI guard_type [deprecated]");
102 static struct workqueue_struct *release_wq;
103 struct iser_global ig;
106 * iscsi_iser_recv() - Process a successfull recv completion
107 * @conn: iscsi connection
109 * @rx_data: buffer containing receive data payload
110 * @rx_data_len: length of rx_data
112 * Notes: In case of data length errors or iscsi PDU completion failures
113 * this routine will signal iscsi layer of connection failure.
116 iscsi_iser_recv(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
117 char *rx_data, int rx_data_len)
123 /* verify PDU length */
124 datalen = ntoh24(hdr->dlength);
125 if (datalen > rx_data_len || (datalen + 4) < rx_data_len) {
126 iser_err("wrong datalen %d (hdr), %d (IB)\n",
127 datalen, rx_data_len);
128 rc = ISCSI_ERR_DATALEN;
132 if (datalen != rx_data_len)
133 iser_dbg("aligned datalen (%d) hdr, %d (IB)\n",
134 datalen, rx_data_len);
137 ahslen = hdr->hlength * 4;
139 rc = iscsi_complete_pdu(conn, hdr, rx_data, rx_data_len);
140 if (rc && rc != ISCSI_ERR_NO_SCSI_CMD)
145 iscsi_conn_failure(conn, rc);
149 * iscsi_iser_pdu_alloc() - allocate an iscsi-iser PDU
151 * @opcode: iscsi command opcode
153 * Netes: This routine can't fail, just assign iscsi task
154 * hdr and max hdr size.
157 iscsi_iser_pdu_alloc(struct iscsi_task *task, uint8_t opcode)
159 struct iscsi_iser_task *iser_task = task->dd_data;
161 task->hdr = (struct iscsi_hdr *)&iser_task->desc.iscsi_header;
162 task->hdr_max = sizeof(iser_task->desc.iscsi_header);
168 * iser_initialize_task_headers() - Initialize task headers
170 * @tx_desc: iser tx descriptor
173 * This routine may race with iser teardown flow for scsi
174 * error handling TMFs. So for TMF we should acquire the
175 * state mutex to avoid dereferencing the IB device which
176 * may have already been terminated.
179 iser_initialize_task_headers(struct iscsi_task *task,
180 struct iser_tx_desc *tx_desc)
182 struct iser_conn *iser_conn = task->conn->dd_data;
183 struct iser_device *device = iser_conn->ib_conn.device;
184 struct iscsi_iser_task *iser_task = task->dd_data;
186 const bool mgmt_task = !task->sc && !in_interrupt();
189 if (unlikely(mgmt_task))
190 mutex_lock(&iser_conn->state_mutex);
192 if (unlikely(iser_conn->state != ISER_CONN_UP)) {
197 dma_addr = ib_dma_map_single(device->ib_device, (void *)tx_desc,
198 ISER_HEADERS_LEN, DMA_TO_DEVICE);
199 if (ib_dma_mapping_error(device->ib_device, dma_addr)) {
204 tx_desc->mapped = true;
205 tx_desc->dma_addr = dma_addr;
206 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
207 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
208 tx_desc->tx_sg[0].lkey = device->mr->lkey;
210 iser_task->iser_conn = iser_conn;
212 if (unlikely(mgmt_task))
213 mutex_unlock(&iser_conn->state_mutex);
219 * iscsi_iser_task_init() - Initialize iscsi-iser task
222 * Initialize the task for the scsi command or mgmt command.
224 * Return: Returns zero on success or -ENOMEM when failing
225 * to init task headers (dma mapping error).
228 iscsi_iser_task_init(struct iscsi_task *task)
230 struct iscsi_iser_task *iser_task = task->dd_data;
233 ret = iser_initialize_task_headers(task, &iser_task->desc);
235 iser_err("Failed to init task %p, err = %d\n",
244 iser_task->command_sent = 0;
245 iser_task_rdma_init(iser_task);
246 iser_task->sc = task->sc;
252 * iscsi_iser_mtask_xmit() - xmit management (immediate) task
253 * @conn: iscsi connection
254 * @task: task management task
257 * The function can return -EAGAIN in which case caller must
258 * call it again later, or recover. '0' return code means successful
263 iscsi_iser_mtask_xmit(struct iscsi_conn *conn, struct iscsi_task *task)
267 iser_dbg("mtask xmit [cid %d itt 0x%x]\n", conn->id, task->itt);
269 error = iser_send_control(conn, task);
271 /* since iser xmits control with zero copy, tasks can not be recycled
272 * right after sending them.
273 * The recycling scheme is based on whether a response is expected
274 * - if yes, the task is recycled at iscsi_complete_pdu
275 * - if no, the task is recycled at iser_snd_completion
281 iscsi_iser_task_xmit_unsol_data(struct iscsi_conn *conn,
282 struct iscsi_task *task)
284 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
285 struct iscsi_data hdr;
288 /* Send data-out PDUs while there's still unsolicited data to send */
289 while (iscsi_task_has_unsol_data(task)) {
290 iscsi_prep_data_out_pdu(task, r2t, &hdr);
291 iser_dbg("Sending data-out: itt 0x%x, data count %d\n",
292 hdr.itt, r2t->data_count);
294 /* the buffer description has been passed with the command */
295 /* Send the command */
296 error = iser_send_data_out(conn, task, &hdr);
299 goto iscsi_iser_task_xmit_unsol_data_exit;
301 r2t->sent += r2t->data_count;
302 iser_dbg("Need to send %d more as data-out PDUs\n",
303 r2t->data_length - r2t->sent);
306 iscsi_iser_task_xmit_unsol_data_exit:
311 * iscsi_iser_task_xmit() - xmit iscsi-iser task
314 * Return: zero on success or escalates $error on failure.
317 iscsi_iser_task_xmit(struct iscsi_task *task)
319 struct iscsi_conn *conn = task->conn;
320 struct iscsi_iser_task *iser_task = task->dd_data;
324 return iscsi_iser_mtask_xmit(conn, task);
326 if (task->sc->sc_data_direction == DMA_TO_DEVICE) {
327 BUG_ON(scsi_bufflen(task->sc) == 0);
329 iser_dbg("cmd [itt %x total %d imm %d unsol_data %d\n",
330 task->itt, scsi_bufflen(task->sc),
331 task->imm_count, task->unsol_r2t.data_length);
334 iser_dbg("ctask xmit [cid %d itt 0x%x]\n",
335 conn->id, task->itt);
337 /* Send the cmd PDU */
338 if (!iser_task->command_sent) {
339 error = iser_send_command(conn, task);
341 goto iscsi_iser_task_xmit_exit;
342 iser_task->command_sent = 1;
345 /* Send unsolicited data-out PDU(s) if necessary */
346 if (iscsi_task_has_unsol_data(task))
347 error = iscsi_iser_task_xmit_unsol_data(conn, task);
349 iscsi_iser_task_xmit_exit:
354 * iscsi_iser_cleanup_task() - cleanup an iscsi-iser task
357 * Notes: In case the RDMA device is already NULL (might have
358 * been removed in DEVICE_REMOVAL CM event it will bail-out
359 * without doing dma unmapping.
361 static void iscsi_iser_cleanup_task(struct iscsi_task *task)
363 struct iscsi_iser_task *iser_task = task->dd_data;
364 struct iser_tx_desc *tx_desc = &iser_task->desc;
365 struct iser_conn *iser_conn = task->conn->dd_data;
366 struct iser_device *device = iser_conn->ib_conn.device;
368 /* DEVICE_REMOVAL event might have already released the device */
372 if (likely(tx_desc->mapped)) {
373 ib_dma_unmap_single(device->ib_device, tx_desc->dma_addr,
374 ISER_HEADERS_LEN, DMA_TO_DEVICE);
375 tx_desc->mapped = false;
378 /* mgmt tasks do not need special cleanup */
382 if (iser_task->status == ISER_TASK_STATUS_STARTED) {
383 iser_task->status = ISER_TASK_STATUS_COMPLETED;
384 iser_task_rdma_finalize(iser_task);
389 * iscsi_iser_check_protection() - check protection information status of task.
391 * @sector: error sector if exsists (output)
393 * Return: zero if no data-integrity errors have occured
394 * 0x1: data-integrity error occured in the guard-block
395 * 0x2: data-integrity error occured in the reference tag
396 * 0x3: data-integrity error occured in the application tag
398 * In addition the error sector is marked.
401 iscsi_iser_check_protection(struct iscsi_task *task, sector_t *sector)
403 struct iscsi_iser_task *iser_task = task->dd_data;
405 if (iser_task->dir[ISER_DIR_IN])
406 return iser_check_task_pi_status(iser_task, ISER_DIR_IN,
409 return iser_check_task_pi_status(iser_task, ISER_DIR_OUT,
414 * iscsi_iser_conn_create() - create a new iscsi-iser connection
415 * @cls_session: iscsi class connection
416 * @conn_idx: connection index within the session (for MCS)
418 * Return: iscsi_cls_conn when iscsi_conn_setup succeeds or NULL
421 static struct iscsi_cls_conn *
422 iscsi_iser_conn_create(struct iscsi_cls_session *cls_session,
425 struct iscsi_conn *conn;
426 struct iscsi_cls_conn *cls_conn;
428 cls_conn = iscsi_conn_setup(cls_session, 0, conn_idx);
431 conn = cls_conn->dd_data;
434 * due to issues with the login code re iser sematics
435 * this not set in iscsi_conn_setup - FIXME
437 conn->max_recv_dlength = ISER_RECV_DATA_SEG_LEN;
443 * iscsi_iser_conn_bind() - bind iscsi and iser connection structures
444 * @cls_session: iscsi class session
445 * @cls_conn: iscsi class connection
446 * @transport_eph: transport end-point handle
447 * @is_leading: indicate if this is the session leading connection (MCS)
449 * Return: zero on success, $error if iscsi_conn_bind fails and
450 * -EINVAL in case end-point doesn't exsits anymore or iser connection
451 * state is not UP (teardown already started).
454 iscsi_iser_conn_bind(struct iscsi_cls_session *cls_session,
455 struct iscsi_cls_conn *cls_conn,
456 uint64_t transport_eph,
459 struct iscsi_conn *conn = cls_conn->dd_data;
460 struct iser_conn *iser_conn;
461 struct iscsi_endpoint *ep;
464 error = iscsi_conn_bind(cls_session, cls_conn, is_leading);
468 /* the transport ep handle comes from user space so it must be
469 * verified against the global ib connections list */
470 ep = iscsi_lookup_endpoint(transport_eph);
472 iser_err("can't bind eph %llx\n",
473 (unsigned long long)transport_eph);
476 iser_conn = ep->dd_data;
478 mutex_lock(&iser_conn->state_mutex);
479 if (iser_conn->state != ISER_CONN_UP) {
481 iser_err("iser_conn %p state is %d, teardown started\n",
482 iser_conn, iser_conn->state);
486 error = iser_alloc_rx_descriptors(iser_conn, conn->session);
490 /* binds the iSER connection retrieved from the previously
491 * connected ep_handle to the iSCSI layer connection. exchanges
492 * connection pointers */
493 iser_info("binding iscsi conn %p to iser_conn %p\n", conn, iser_conn);
495 conn->dd_data = iser_conn;
496 iser_conn->iscsi_conn = conn;
499 mutex_unlock(&iser_conn->state_mutex);
504 * iscsi_iser_conn_start() - start iscsi-iser connection
505 * @cls_conn: iscsi class connection
507 * Notes: Here iser intialize (or re-initialize) stop_completion as
508 * from this point iscsi must call conn_stop in session/connection
509 * teardown so iser transport must wait for it.
512 iscsi_iser_conn_start(struct iscsi_cls_conn *cls_conn)
514 struct iscsi_conn *iscsi_conn;
515 struct iser_conn *iser_conn;
517 iscsi_conn = cls_conn->dd_data;
518 iser_conn = iscsi_conn->dd_data;
519 reinit_completion(&iser_conn->stop_completion);
521 return iscsi_conn_start(cls_conn);
525 * iscsi_iser_conn_stop() - stop iscsi-iser connection
526 * @cls_conn: iscsi class connection
527 * @flag: indicate if recover or terminate (passed as is)
529 * Notes: Calling iscsi_conn_stop might theoretically race with
530 * DEVICE_REMOVAL event and dereference a previously freed RDMA device
531 * handle, so we call it under iser the state lock to protect against
535 iscsi_iser_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
537 struct iscsi_conn *conn = cls_conn->dd_data;
538 struct iser_conn *iser_conn = conn->dd_data;
540 iser_info("stopping iscsi_conn: %p, iser_conn: %p\n", conn, iser_conn);
543 * Userspace may have goofed up and not bound the connection or
544 * might have only partially setup the connection.
547 mutex_lock(&iser_conn->state_mutex);
548 iser_conn_terminate(iser_conn);
549 iscsi_conn_stop(cls_conn, flag);
552 iser_conn->iscsi_conn = NULL;
553 conn->dd_data = NULL;
555 complete(&iser_conn->stop_completion);
556 mutex_unlock(&iser_conn->state_mutex);
558 iscsi_conn_stop(cls_conn, flag);
563 * iscsi_iser_session_destroy() - destroy iscsi-iser session
564 * @cls_session: iscsi class session
566 * Removes and free iscsi host.
569 iscsi_iser_session_destroy(struct iscsi_cls_session *cls_session)
571 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
573 iscsi_session_teardown(cls_session);
574 iscsi_host_remove(shost);
575 iscsi_host_free(shost);
578 static inline unsigned int
579 iser_dif_prot_caps(int prot_caps)
581 return ((prot_caps & IB_PROT_T10DIF_TYPE_1) ?
582 SHOST_DIF_TYPE1_PROTECTION | SHOST_DIX_TYPE0_PROTECTION |
583 SHOST_DIX_TYPE1_PROTECTION : 0) |
584 ((prot_caps & IB_PROT_T10DIF_TYPE_2) ?
585 SHOST_DIF_TYPE2_PROTECTION | SHOST_DIX_TYPE2_PROTECTION : 0) |
586 ((prot_caps & IB_PROT_T10DIF_TYPE_3) ?
587 SHOST_DIF_TYPE3_PROTECTION | SHOST_DIX_TYPE3_PROTECTION : 0);
591 * iscsi_iser_session_create() - create an iscsi-iser session
592 * @ep: iscsi end-point handle
593 * @cmds_max: maximum commands in this session
594 * @qdepth: session command queue depth
595 * @initial_cmdsn: initiator command sequnce number
597 * Allocates and adds a scsi host, expose DIF supprot if
598 * exists, and sets up an iscsi session.
600 static struct iscsi_cls_session *
601 iscsi_iser_session_create(struct iscsi_endpoint *ep,
602 uint16_t cmds_max, uint16_t qdepth,
603 uint32_t initial_cmdsn)
605 struct iscsi_cls_session *cls_session;
606 struct iscsi_session *session;
607 struct Scsi_Host *shost;
608 struct iser_conn *iser_conn = NULL;
609 struct ib_conn *ib_conn;
612 shost = iscsi_host_alloc(&iscsi_iser_sht, 0, 0);
615 shost->transportt = iscsi_iser_scsi_transport;
616 shost->cmd_per_lun = qdepth;
617 shost->max_lun = iscsi_max_lun;
619 shost->max_channel = 0;
620 shost->max_cmd_len = 16;
623 * older userspace tools (before 2.0-870) did not pass us
624 * the leading conn's ep so this will be NULL;
627 iser_conn = ep->dd_data;
628 max_cmds = iser_conn->max_cmds;
630 mutex_lock(&iser_conn->state_mutex);
631 if (iser_conn->state != ISER_CONN_UP) {
632 iser_err("iser conn %p already started teardown\n",
634 mutex_unlock(&iser_conn->state_mutex);
638 ib_conn = &iser_conn->ib_conn;
639 if (ib_conn->pi_support) {
640 u32 sig_caps = ib_conn->device->dev_attr.sig_prot_cap;
642 scsi_host_set_prot(shost, iser_dif_prot_caps(sig_caps));
643 scsi_host_set_guard(shost, SHOST_DIX_GUARD_IP |
644 SHOST_DIX_GUARD_CRC);
647 if (iscsi_host_add(shost,
648 ib_conn->device->ib_device->dma_device)) {
649 mutex_unlock(&iser_conn->state_mutex);
652 mutex_unlock(&iser_conn->state_mutex);
654 max_cmds = ISER_DEF_XMIT_CMDS_MAX;
655 if (iscsi_host_add(shost, NULL))
659 if (cmds_max > max_cmds) {
660 iser_info("cmds_max changed from %u to %u\n",
665 cls_session = iscsi_session_setup(&iscsi_iser_transport, shost,
667 sizeof(struct iscsi_iser_task),
671 session = cls_session->dd_data;
673 shost->can_queue = session->scsi_cmds_max;
677 iscsi_host_remove(shost);
679 iscsi_host_free(shost);
684 iscsi_iser_set_param(struct iscsi_cls_conn *cls_conn,
685 enum iscsi_param param, char *buf, int buflen)
690 case ISCSI_PARAM_MAX_RECV_DLENGTH:
693 case ISCSI_PARAM_HDRDGST_EN:
694 sscanf(buf, "%d", &value);
696 iser_err("DataDigest wasn't negotiated to None\n");
700 case ISCSI_PARAM_DATADGST_EN:
701 sscanf(buf, "%d", &value);
703 iser_err("DataDigest wasn't negotiated to None\n");
707 case ISCSI_PARAM_IFMARKER_EN:
708 sscanf(buf, "%d", &value);
710 iser_err("IFMarker wasn't negotiated to No\n");
714 case ISCSI_PARAM_OFMARKER_EN:
715 sscanf(buf, "%d", &value);
717 iser_err("OFMarker wasn't negotiated to No\n");
722 return iscsi_set_param(cls_conn, param, buf, buflen);
729 * iscsi_iser_set_param() - set class connection parameter
730 * @cls_conn: iscsi class connection
731 * @stats: iscsi stats to output
733 * Output connection statistics.
736 iscsi_iser_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
738 struct iscsi_conn *conn = cls_conn->dd_data;
740 stats->txdata_octets = conn->txdata_octets;
741 stats->rxdata_octets = conn->rxdata_octets;
742 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
743 stats->dataout_pdus = conn->dataout_pdus_cnt;
744 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
745 stats->datain_pdus = conn->datain_pdus_cnt; /* always 0 */
746 stats->r2t_pdus = conn->r2t_pdus_cnt; /* always 0 */
747 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
748 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
749 stats->custom_length = 4;
750 strcpy(stats->custom[0].desc, "qp_tx_queue_full");
751 stats->custom[0].value = 0; /* TB iser_conn->qp_tx_queue_full; */
752 strcpy(stats->custom[1].desc, "fmr_map_not_avail");
753 stats->custom[1].value = 0; /* TB iser_conn->fmr_map_not_avail */;
754 strcpy(stats->custom[2].desc, "eh_abort_cnt");
755 stats->custom[2].value = conn->eh_abort_cnt;
756 strcpy(stats->custom[3].desc, "fmr_unalign_cnt");
757 stats->custom[3].value = conn->fmr_unalign_cnt;
760 static int iscsi_iser_get_ep_param(struct iscsi_endpoint *ep,
761 enum iscsi_param param, char *buf)
763 struct iser_conn *iser_conn = ep->dd_data;
767 case ISCSI_PARAM_CONN_PORT:
768 case ISCSI_PARAM_CONN_ADDRESS:
769 if (!iser_conn || !iser_conn->ib_conn.cma_id)
772 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
773 &iser_conn->ib_conn.cma_id->route.addr.dst_addr,
784 * iscsi_iser_ep_connect() - Initiate iSER connection establishment
786 * @dst_addr: destination address
787 * @non-blocking: indicate if routine can block
789 * Allocate an iscsi endpoint, an iser_conn structure and bind them.
790 * After that start RDMA connection establishment via rdma_cm. We
791 * don't allocate iser_conn embedded in iscsi_endpoint since in teardown
792 * the endpoint will be destroyed at ep_disconnect while iser_conn will
793 * cleanup its resources asynchronuously.
795 * Return: iscsi_endpoint created by iscsi layer or ERR_PTR(error)
798 static struct iscsi_endpoint *
799 iscsi_iser_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
803 struct iser_conn *iser_conn;
804 struct iscsi_endpoint *ep;
806 ep = iscsi_create_endpoint(0);
808 return ERR_PTR(-ENOMEM);
810 iser_conn = kzalloc(sizeof(*iser_conn), GFP_KERNEL);
816 ep->dd_data = iser_conn;
818 iser_conn_init(iser_conn);
820 err = iser_connect(iser_conn, NULL, dst_addr, non_blocking);
826 iscsi_destroy_endpoint(ep);
831 * iscsi_iser_ep_poll() - poll for iser connection establishment to complete
832 * @ep: iscsi endpoint (created at ep_connect)
833 * @timeout_ms: polling timeout allowed in ms.
835 * This routine boils down to waiting for up_completion signaling
836 * that cma_id got CONNECTED event.
838 * Return: 1 if succeeded in connection establishment, 0 if timeout expired
839 * (libiscsi will retry will kick in) or -1 if interrupted by signal
840 * or more likely iser connection state transitioned to TEMINATING or
841 * DOWN during the wait period.
844 iscsi_iser_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
846 struct iser_conn *iser_conn;
849 iser_conn = ep->dd_data;
850 rc = wait_for_completion_interruptible_timeout(&iser_conn->up_completion,
851 msecs_to_jiffies(timeout_ms));
852 /* if conn establishment failed, return error code to iscsi */
854 mutex_lock(&iser_conn->state_mutex);
855 if (iser_conn->state == ISER_CONN_TERMINATING ||
856 iser_conn->state == ISER_CONN_DOWN)
858 mutex_unlock(&iser_conn->state_mutex);
861 iser_info("ib conn %p rc = %d\n", iser_conn, rc);
864 return 1; /* success, this is the equivalent of POLLOUT */
866 return 0; /* timeout */
868 return rc; /* signal */
872 * iscsi_iser_ep_disconnect() - Initiate connection teardown process
873 * @ep: iscsi endpoint handle
875 * This routine is not blocked by iser and RDMA termination process
876 * completion as we queue a deffered work for iser/RDMA destruction
877 * and cleanup or actually call it immediately in case we didn't pass
878 * iscsi conn bind/start stage, thus it is safe.
881 iscsi_iser_ep_disconnect(struct iscsi_endpoint *ep)
883 struct iser_conn *iser_conn;
885 iser_conn = ep->dd_data;
886 iser_info("ep %p iser conn %p state %d\n",
887 ep, iser_conn, iser_conn->state);
889 mutex_lock(&iser_conn->state_mutex);
890 iser_conn_terminate(iser_conn);
893 * if iser_conn and iscsi_conn are bound, we must wait for
894 * iscsi_conn_stop and flush errors completion before freeing
895 * the iser resources. Otherwise we are safe to free resources
898 if (iser_conn->iscsi_conn) {
899 INIT_WORK(&iser_conn->release_work, iser_release_work);
900 queue_work(release_wq, &iser_conn->release_work);
901 mutex_unlock(&iser_conn->state_mutex);
903 iser_conn->state = ISER_CONN_DOWN;
904 mutex_unlock(&iser_conn->state_mutex);
905 iser_conn_release(iser_conn);
907 iscsi_destroy_endpoint(ep);
910 static umode_t iser_attr_is_visible(int param_type, int param)
912 switch (param_type) {
913 case ISCSI_HOST_PARAM:
915 case ISCSI_HOST_PARAM_NETDEV_NAME:
916 case ISCSI_HOST_PARAM_HWADDRESS:
917 case ISCSI_HOST_PARAM_INITIATOR_NAME:
924 case ISCSI_PARAM_MAX_RECV_DLENGTH:
925 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
926 case ISCSI_PARAM_HDRDGST_EN:
927 case ISCSI_PARAM_DATADGST_EN:
928 case ISCSI_PARAM_CONN_ADDRESS:
929 case ISCSI_PARAM_CONN_PORT:
930 case ISCSI_PARAM_EXP_STATSN:
931 case ISCSI_PARAM_PERSISTENT_ADDRESS:
932 case ISCSI_PARAM_PERSISTENT_PORT:
933 case ISCSI_PARAM_PING_TMO:
934 case ISCSI_PARAM_RECV_TMO:
935 case ISCSI_PARAM_INITIAL_R2T_EN:
936 case ISCSI_PARAM_MAX_R2T:
937 case ISCSI_PARAM_IMM_DATA_EN:
938 case ISCSI_PARAM_FIRST_BURST:
939 case ISCSI_PARAM_MAX_BURST:
940 case ISCSI_PARAM_PDU_INORDER_EN:
941 case ISCSI_PARAM_DATASEQ_INORDER_EN:
942 case ISCSI_PARAM_TARGET_NAME:
943 case ISCSI_PARAM_TPGT:
944 case ISCSI_PARAM_USERNAME:
945 case ISCSI_PARAM_PASSWORD:
946 case ISCSI_PARAM_USERNAME_IN:
947 case ISCSI_PARAM_PASSWORD_IN:
948 case ISCSI_PARAM_FAST_ABORT:
949 case ISCSI_PARAM_ABORT_TMO:
950 case ISCSI_PARAM_LU_RESET_TMO:
951 case ISCSI_PARAM_TGT_RESET_TMO:
952 case ISCSI_PARAM_IFACE_NAME:
953 case ISCSI_PARAM_INITIATOR_NAME:
954 case ISCSI_PARAM_DISCOVERY_SESS:
964 static struct scsi_host_template iscsi_iser_sht = {
965 .module = THIS_MODULE,
966 .name = "iSCSI Initiator over iSER",
967 .queuecommand = iscsi_queuecommand,
968 .change_queue_depth = scsi_change_queue_depth,
969 .sg_tablesize = ISCSI_ISER_SG_TABLESIZE,
971 .cmd_per_lun = ISER_DEF_CMD_PER_LUN,
972 .eh_abort_handler = iscsi_eh_abort,
973 .eh_device_reset_handler= iscsi_eh_device_reset,
974 .eh_target_reset_handler = iscsi_eh_recover_target,
975 .target_alloc = iscsi_target_alloc,
976 .use_clustering = DISABLE_CLUSTERING,
977 .proc_name = "iscsi_iser",
979 .track_queue_depth = 1,
982 static struct iscsi_transport iscsi_iser_transport = {
983 .owner = THIS_MODULE,
985 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_TEXT_NEGO,
986 /* session management */
987 .create_session = iscsi_iser_session_create,
988 .destroy_session = iscsi_iser_session_destroy,
989 /* connection management */
990 .create_conn = iscsi_iser_conn_create,
991 .bind_conn = iscsi_iser_conn_bind,
992 .destroy_conn = iscsi_conn_teardown,
993 .attr_is_visible = iser_attr_is_visible,
994 .set_param = iscsi_iser_set_param,
995 .get_conn_param = iscsi_conn_get_param,
996 .get_ep_param = iscsi_iser_get_ep_param,
997 .get_session_param = iscsi_session_get_param,
998 .start_conn = iscsi_iser_conn_start,
999 .stop_conn = iscsi_iser_conn_stop,
1000 /* iscsi host params */
1001 .get_host_param = iscsi_host_get_param,
1002 .set_host_param = iscsi_host_set_param,
1004 .send_pdu = iscsi_conn_send_pdu,
1005 .get_stats = iscsi_iser_conn_get_stats,
1006 .init_task = iscsi_iser_task_init,
1007 .xmit_task = iscsi_iser_task_xmit,
1008 .cleanup_task = iscsi_iser_cleanup_task,
1009 .alloc_pdu = iscsi_iser_pdu_alloc,
1010 .check_protection = iscsi_iser_check_protection,
1012 .session_recovery_timedout = iscsi_session_recovery_timedout,
1014 .ep_connect = iscsi_iser_ep_connect,
1015 .ep_poll = iscsi_iser_ep_poll,
1016 .ep_disconnect = iscsi_iser_ep_disconnect
1019 static int __init iser_init(void)
1023 iser_dbg("Starting iSER datamover...\n");
1025 if (iscsi_max_lun < 1) {
1026 iser_err("Invalid max_lun value of %u\n", iscsi_max_lun);
1030 memset(&ig, 0, sizeof(struct iser_global));
1032 ig.desc_cache = kmem_cache_create("iser_descriptors",
1033 sizeof(struct iser_tx_desc),
1034 0, SLAB_HWCACHE_ALIGN,
1036 if (ig.desc_cache == NULL)
1039 /* device init is called only after the first addr resolution */
1040 mutex_init(&ig.device_list_mutex);
1041 INIT_LIST_HEAD(&ig.device_list);
1042 mutex_init(&ig.connlist_mutex);
1043 INIT_LIST_HEAD(&ig.connlist);
1045 release_wq = alloc_workqueue("release workqueue", 0, 0);
1047 iser_err("failed to allocate release workqueue\n");
1051 iscsi_iser_scsi_transport = iscsi_register_transport(
1052 &iscsi_iser_transport);
1053 if (!iscsi_iser_scsi_transport) {
1054 iser_err("iscsi_register_transport failed\n");
1056 goto register_transport_failure;
1061 register_transport_failure:
1062 kmem_cache_destroy(ig.desc_cache);
1067 static void __exit iser_exit(void)
1069 struct iser_conn *iser_conn, *n;
1072 iser_dbg("Removing iSER datamover...\n");
1073 destroy_workqueue(release_wq);
1075 mutex_lock(&ig.connlist_mutex);
1076 connlist_empty = list_empty(&ig.connlist);
1077 mutex_unlock(&ig.connlist_mutex);
1079 if (!connlist_empty) {
1080 iser_err("Error cleanup stage completed but we still have iser "
1081 "connections, destroying them anyway.\n");
1082 list_for_each_entry_safe(iser_conn, n, &ig.connlist,
1084 iser_conn_release(iser_conn);
1088 iscsi_unregister_transport(&iscsi_iser_transport);
1089 kmem_cache_destroy(ig.desc_cache);
1092 module_init(iser_init);
1093 module_exit(iser_exit);