1 /*
2  * iSCSI Initiator over iSER Data-Path
3  *
4  * Copyright (C) 2004 Dmitry Yusupov
5  * Copyright (C) 2004 Alex Aizman
6  * Copyright (C) 2005 Mike Christie
7  * Copyright (c) 2005, 2006 Voltaire, Inc. All rights reserved.
8  * Copyright (c) 2013-2014 Mellanox Technologies. All rights reserved.
9  * maintained by openib-general@openib.org
10  *
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:
16  *
17  *     Redistribution and use in source and binary forms, with or
18  *     without modification, are permitted provided that the following
19  *     conditions are met:
20  *
21  *	- Redistributions of source code must retain the above
22  *	  copyright notice, this list of conditions and the following
23  *	  disclaimer.
24  *
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.
29  *
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
37  * SOFTWARE.
38  *
39  * Credits:
40  *	Christoph Hellwig
41  *	FUJITA Tomonori
42  *	Arne Redlich
43  *	Zhenyu Wang
44  * Modified by:
45  *      Erez Zilber
46  */
47 
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>
56 #include <linux/in.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>
62 
63 #include <net/sock.h>
64 
65 #include <asm/uaccess.h>
66 
67 #include <scsi/scsi_cmnd.h>
68 #include <scsi/scsi_device.h>
69 #include <scsi/scsi_eh.h>
70 #include <scsi/scsi_tcq.h>
71 #include <scsi/scsi_host.h>
72 #include <scsi/scsi.h>
73 #include <scsi/scsi_transport_iscsi.h>
74 
75 #include "iscsi_iser.h"
76 
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;
80 
81 static unsigned int iscsi_max_lun = 512;
82 module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
83 
84 int iser_debug_level = 0;
85 bool iser_pi_enable = false;
86 int iser_pi_guard = 1;
87 
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);
92 
93 module_param_named(debug_level, iser_debug_level, int, 0644);
94 MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0 (default:disabled)");
95 
96 module_param_named(pi_enable, iser_pi_enable, bool, 0644);
97 MODULE_PARM_DESC(pi_enable, "Enable T10-PI offload support (default:disabled)");
98 
99 module_param_named(pi_guard, iser_pi_guard, int, 0644);
100 MODULE_PARM_DESC(pi_guard, "T10-PI guard_type [deprecated]");
101 
102 static struct workqueue_struct *release_wq;
103 struct iser_global ig;
104 
105 /*
106  * iscsi_iser_recv() - Process a successfull recv completion
107  * @conn:         iscsi connection
108  * @hdr:          iscsi header
109  * @rx_data:      buffer containing receive data payload
110  * @rx_data_len:  length of rx_data
111  *
112  * Notes: In case of data length errors or iscsi PDU completion failures
113  *        this routine will signal iscsi layer of connection failure.
114  */
115 void
iscsi_iser_recv(struct iscsi_conn * conn,struct iscsi_hdr * hdr,char * rx_data,int rx_data_len)116 iscsi_iser_recv(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
117 		char *rx_data, int rx_data_len)
118 {
119 	int rc = 0;
120 	int datalen;
121 	int ahslen;
122 
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;
129 		goto error;
130 	}
131 
132 	if (datalen != rx_data_len)
133 		iser_dbg("aligned datalen (%d) hdr, %d (IB)\n",
134 			datalen, rx_data_len);
135 
136 	/* read AHS */
137 	ahslen = hdr->hlength * 4;
138 
139 	rc = iscsi_complete_pdu(conn, hdr, rx_data, rx_data_len);
140 	if (rc && rc != ISCSI_ERR_NO_SCSI_CMD)
141 		goto error;
142 
143 	return;
144 error:
145 	iscsi_conn_failure(conn, rc);
146 }
147 
148 /**
149  * iscsi_iser_pdu_alloc() - allocate an iscsi-iser PDU
150  * @task:     iscsi task
151  * @opcode:   iscsi command opcode
152  *
153  * Netes: This routine can't fail, just assign iscsi task
154  *        hdr and max hdr size.
155  */
156 static int
iscsi_iser_pdu_alloc(struct iscsi_task * task,uint8_t opcode)157 iscsi_iser_pdu_alloc(struct iscsi_task *task, uint8_t opcode)
158 {
159 	struct iscsi_iser_task *iser_task = task->dd_data;
160 
161 	task->hdr = (struct iscsi_hdr *)&iser_task->desc.iscsi_header;
162 	task->hdr_max = sizeof(iser_task->desc.iscsi_header);
163 
164 	return 0;
165 }
166 
167 /**
168  * iser_initialize_task_headers() - Initialize task headers
169  * @task:       iscsi task
170  * @tx_desc:    iser tx descriptor
171  *
172  * Notes:
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.
177  */
178 int
iser_initialize_task_headers(struct iscsi_task * task,struct iser_tx_desc * tx_desc)179 iser_initialize_task_headers(struct iscsi_task *task,
180 			     struct iser_tx_desc *tx_desc)
181 {
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;
185 	u64 dma_addr;
186 	const bool mgmt_task = !task->sc && !in_interrupt();
187 	int ret = 0;
188 
189 	if (unlikely(mgmt_task))
190 		mutex_lock(&iser_conn->state_mutex);
191 
192 	if (unlikely(iser_conn->state != ISER_CONN_UP)) {
193 		ret = -ENODEV;
194 		goto out;
195 	}
196 
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)) {
200 		ret = -ENOMEM;
201 		goto out;
202 	}
203 
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;
209 
210 	iser_task->iser_conn = iser_conn;
211 out:
212 	if (unlikely(mgmt_task))
213 		mutex_unlock(&iser_conn->state_mutex);
214 
215 	return ret;
216 }
217 
218 /**
219  * iscsi_iser_task_init() - Initialize iscsi-iser task
220  * @task: iscsi task
221  *
222  * Initialize the task for the scsi command or mgmt command.
223  *
224  * Return: Returns zero on success or -ENOMEM when failing
225  *         to init task headers (dma mapping error).
226  */
227 static int
iscsi_iser_task_init(struct iscsi_task * task)228 iscsi_iser_task_init(struct iscsi_task *task)
229 {
230 	struct iscsi_iser_task *iser_task = task->dd_data;
231 	int ret;
232 
233 	ret = iser_initialize_task_headers(task, &iser_task->desc);
234 	if (ret) {
235 		iser_err("Failed to init task %p, err = %d\n",
236 			 iser_task, ret);
237 		return ret;
238 	}
239 
240 	/* mgmt task */
241 	if (!task->sc)
242 		return 0;
243 
244 	iser_task->command_sent = 0;
245 	iser_task_rdma_init(iser_task);
246 	iser_task->sc = task->sc;
247 
248 	return 0;
249 }
250 
251 /**
252  * iscsi_iser_mtask_xmit() - xmit management (immediate) task
253  * @conn: iscsi connection
254  * @task: task management task
255  *
256  * Notes:
257  *	The function can return -EAGAIN in which case caller must
258  *	call it again later, or recover. '0' return code means successful
259  *	xmit.
260  *
261  **/
262 static int
iscsi_iser_mtask_xmit(struct iscsi_conn * conn,struct iscsi_task * task)263 iscsi_iser_mtask_xmit(struct iscsi_conn *conn, struct iscsi_task *task)
264 {
265 	int error = 0;
266 
267 	iser_dbg("mtask xmit [cid %d itt 0x%x]\n", conn->id, task->itt);
268 
269 	error = iser_send_control(conn, task);
270 
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
276 	 */
277 	return error;
278 }
279 
280 static int
iscsi_iser_task_xmit_unsol_data(struct iscsi_conn * conn,struct iscsi_task * task)281 iscsi_iser_task_xmit_unsol_data(struct iscsi_conn *conn,
282 				 struct iscsi_task *task)
283 {
284 	struct iscsi_r2t_info *r2t = &task->unsol_r2t;
285 	struct iscsi_data hdr;
286 	int error = 0;
287 
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);
293 
294 		/* the buffer description has been passed with the command */
295 		/* Send the command */
296 		error = iser_send_data_out(conn, task, &hdr);
297 		if (error) {
298 			r2t->datasn--;
299 			goto iscsi_iser_task_xmit_unsol_data_exit;
300 		}
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);
304 	}
305 
306 iscsi_iser_task_xmit_unsol_data_exit:
307 	return error;
308 }
309 
310 /**
311  * iscsi_iser_task_xmit() - xmit iscsi-iser task
312  * @task: iscsi task
313  *
314  * Return: zero on success or escalates $error on failure.
315  */
316 static int
iscsi_iser_task_xmit(struct iscsi_task * task)317 iscsi_iser_task_xmit(struct iscsi_task *task)
318 {
319 	struct iscsi_conn *conn = task->conn;
320 	struct iscsi_iser_task *iser_task = task->dd_data;
321 	int error = 0;
322 
323 	if (!task->sc)
324 		return iscsi_iser_mtask_xmit(conn, task);
325 
326 	if (task->sc->sc_data_direction == DMA_TO_DEVICE) {
327 		BUG_ON(scsi_bufflen(task->sc) == 0);
328 
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);
332 	}
333 
334 	iser_dbg("ctask xmit [cid %d itt 0x%x]\n",
335 		   conn->id, task->itt);
336 
337 	/* Send the cmd PDU */
338 	if (!iser_task->command_sent) {
339 		error = iser_send_command(conn, task);
340 		if (error)
341 			goto iscsi_iser_task_xmit_exit;
342 		iser_task->command_sent = 1;
343 	}
344 
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);
348 
349  iscsi_iser_task_xmit_exit:
350 	return error;
351 }
352 
353 /**
354  * iscsi_iser_cleanup_task() - cleanup an iscsi-iser task
355  * @task: iscsi task
356  *
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.
360  */
iscsi_iser_cleanup_task(struct iscsi_task * task)361 static void iscsi_iser_cleanup_task(struct iscsi_task *task)
362 {
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;
367 
368 	/* DEVICE_REMOVAL event might have already released the device */
369 	if (!device)
370 		return;
371 
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;
376 	}
377 
378 	/* mgmt tasks do not need special cleanup */
379 	if (!task->sc)
380 		return;
381 
382 	if (iser_task->status == ISER_TASK_STATUS_STARTED) {
383 		iser_task->status = ISER_TASK_STATUS_COMPLETED;
384 		iser_task_rdma_finalize(iser_task);
385 	}
386 }
387 
388 /**
389  * iscsi_iser_check_protection() - check protection information status of task.
390  * @task:     iscsi task
391  * @sector:   error sector if exsists (output)
392  *
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
397  *
398  *         In addition the error sector is marked.
399  */
400 static u8
iscsi_iser_check_protection(struct iscsi_task * task,sector_t * sector)401 iscsi_iser_check_protection(struct iscsi_task *task, sector_t *sector)
402 {
403 	struct iscsi_iser_task *iser_task = task->dd_data;
404 
405 	if (iser_task->dir[ISER_DIR_IN])
406 		return iser_check_task_pi_status(iser_task, ISER_DIR_IN,
407 						 sector);
408 	else
409 		return iser_check_task_pi_status(iser_task, ISER_DIR_OUT,
410 						 sector);
411 }
412 
413 /**
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)
417  *
418  * Return: iscsi_cls_conn when iscsi_conn_setup succeeds or NULL
419  *         otherwise.
420  */
421 static struct iscsi_cls_conn *
iscsi_iser_conn_create(struct iscsi_cls_session * cls_session,uint32_t conn_idx)422 iscsi_iser_conn_create(struct iscsi_cls_session *cls_session,
423 		       uint32_t conn_idx)
424 {
425 	struct iscsi_conn *conn;
426 	struct iscsi_cls_conn *cls_conn;
427 
428 	cls_conn = iscsi_conn_setup(cls_session, 0, conn_idx);
429 	if (!cls_conn)
430 		return NULL;
431 	conn = cls_conn->dd_data;
432 
433 	/*
434 	 * due to issues with the login code re iser sematics
435 	 * this not set in iscsi_conn_setup - FIXME
436 	 */
437 	conn->max_recv_dlength = ISER_RECV_DATA_SEG_LEN;
438 
439 	return cls_conn;
440 }
441 
442 /**
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)
448  *
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).
452  */
453 static int
iscsi_iser_conn_bind(struct iscsi_cls_session * cls_session,struct iscsi_cls_conn * cls_conn,uint64_t transport_eph,int is_leading)454 iscsi_iser_conn_bind(struct iscsi_cls_session *cls_session,
455 		     struct iscsi_cls_conn *cls_conn,
456 		     uint64_t transport_eph,
457 		     int is_leading)
458 {
459 	struct iscsi_conn *conn = cls_conn->dd_data;
460 	struct iser_conn *iser_conn;
461 	struct iscsi_endpoint *ep;
462 	int error;
463 
464 	error = iscsi_conn_bind(cls_session, cls_conn, is_leading);
465 	if (error)
466 		return error;
467 
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);
471 	if (!ep) {
472 		iser_err("can't bind eph %llx\n",
473 			 (unsigned long long)transport_eph);
474 		return -EINVAL;
475 	}
476 	iser_conn = ep->dd_data;
477 
478 	mutex_lock(&iser_conn->state_mutex);
479 	if (iser_conn->state != ISER_CONN_UP) {
480 		error = -EINVAL;
481 		iser_err("iser_conn %p state is %d, teardown started\n",
482 			 iser_conn, iser_conn->state);
483 		goto out;
484 	}
485 
486 	error = iser_alloc_rx_descriptors(iser_conn, conn->session);
487 	if (error)
488 		goto out;
489 
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);
494 
495 	conn->dd_data = iser_conn;
496 	iser_conn->iscsi_conn = conn;
497 
498 out:
499 	mutex_unlock(&iser_conn->state_mutex);
500 	return error;
501 }
502 
503 /**
504  * iscsi_iser_conn_start() - start iscsi-iser connection
505  * @cls_conn: iscsi class connection
506  *
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.
510  */
511 static int
iscsi_iser_conn_start(struct iscsi_cls_conn * cls_conn)512 iscsi_iser_conn_start(struct iscsi_cls_conn *cls_conn)
513 {
514 	struct iscsi_conn *iscsi_conn;
515 	struct iser_conn *iser_conn;
516 
517 	iscsi_conn = cls_conn->dd_data;
518 	iser_conn = iscsi_conn->dd_data;
519 	reinit_completion(&iser_conn->stop_completion);
520 
521 	return iscsi_conn_start(cls_conn);
522 }
523 
524 /**
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)
528  *
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
532  *        this kind of race.
533  */
534 static void
iscsi_iser_conn_stop(struct iscsi_cls_conn * cls_conn,int flag)535 iscsi_iser_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
536 {
537 	struct iscsi_conn *conn = cls_conn->dd_data;
538 	struct iser_conn *iser_conn = conn->dd_data;
539 
540 	iser_info("stopping iscsi_conn: %p, iser_conn: %p\n", conn, iser_conn);
541 
542 	/*
543 	 * Userspace may have goofed up and not bound the connection or
544 	 * might have only partially setup the connection.
545 	 */
546 	if (iser_conn) {
547 		mutex_lock(&iser_conn->state_mutex);
548 		iser_conn_terminate(iser_conn);
549 		iscsi_conn_stop(cls_conn, flag);
550 
551 		/* unbind */
552 		iser_conn->iscsi_conn = NULL;
553 		conn->dd_data = NULL;
554 
555 		complete(&iser_conn->stop_completion);
556 		mutex_unlock(&iser_conn->state_mutex);
557 	} else {
558 		iscsi_conn_stop(cls_conn, flag);
559 	}
560 }
561 
562 /**
563  * iscsi_iser_session_destroy() - destroy iscsi-iser session
564  * @cls_session: iscsi class session
565  *
566  * Removes and free iscsi host.
567  */
568 static void
iscsi_iser_session_destroy(struct iscsi_cls_session * cls_session)569 iscsi_iser_session_destroy(struct iscsi_cls_session *cls_session)
570 {
571 	struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
572 
573 	iscsi_session_teardown(cls_session);
574 	iscsi_host_remove(shost);
575 	iscsi_host_free(shost);
576 }
577 
578 static inline unsigned int
iser_dif_prot_caps(int prot_caps)579 iser_dif_prot_caps(int prot_caps)
580 {
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);
588 }
589 
590 /**
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
596  *
597  * Allocates and adds a scsi host, expose DIF supprot if
598  * exists, and sets up an iscsi session.
599  */
600 static struct iscsi_cls_session *
iscsi_iser_session_create(struct iscsi_endpoint * ep,uint16_t cmds_max,uint16_t qdepth,uint32_t initial_cmdsn)601 iscsi_iser_session_create(struct iscsi_endpoint *ep,
602 			  uint16_t cmds_max, uint16_t qdepth,
603 			  uint32_t initial_cmdsn)
604 {
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;
610 	u16 max_cmds;
611 
612 	shost = iscsi_host_alloc(&iscsi_iser_sht, 0, 0);
613 	if (!shost)
614 		return NULL;
615 	shost->transportt = iscsi_iser_scsi_transport;
616 	shost->cmd_per_lun = qdepth;
617 	shost->max_lun = iscsi_max_lun;
618 	shost->max_id = 0;
619 	shost->max_channel = 0;
620 	shost->max_cmd_len = 16;
621 
622 	/*
623 	 * older userspace tools (before 2.0-870) did not pass us
624 	 * the leading conn's ep so this will be NULL;
625 	 */
626 	if (ep) {
627 		iser_conn = ep->dd_data;
628 		max_cmds = iser_conn->max_cmds;
629 
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",
633 				 iser_conn);
634 			mutex_unlock(&iser_conn->state_mutex);
635 			goto free_host;
636 		}
637 
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;
641 
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);
645 		}
646 
647 		if (iscsi_host_add(shost,
648 				   ib_conn->device->ib_device->dma_device)) {
649 			mutex_unlock(&iser_conn->state_mutex);
650 			goto free_host;
651 		}
652 		mutex_unlock(&iser_conn->state_mutex);
653 	} else {
654 		max_cmds = ISER_DEF_XMIT_CMDS_MAX;
655 		if (iscsi_host_add(shost, NULL))
656 			goto free_host;
657 	}
658 
659 	if (cmds_max > max_cmds) {
660 		iser_info("cmds_max changed from %u to %u\n",
661 			  cmds_max, max_cmds);
662 		cmds_max = max_cmds;
663 	}
664 
665 	cls_session = iscsi_session_setup(&iscsi_iser_transport, shost,
666 					  cmds_max, 0,
667 					  sizeof(struct iscsi_iser_task),
668 					  initial_cmdsn, 0);
669 	if (!cls_session)
670 		goto remove_host;
671 	session = cls_session->dd_data;
672 
673 	shost->can_queue = session->scsi_cmds_max;
674 	return cls_session;
675 
676 remove_host:
677 	iscsi_host_remove(shost);
678 free_host:
679 	iscsi_host_free(shost);
680 	return NULL;
681 }
682 
683 static int
iscsi_iser_set_param(struct iscsi_cls_conn * cls_conn,enum iscsi_param param,char * buf,int buflen)684 iscsi_iser_set_param(struct iscsi_cls_conn *cls_conn,
685 		     enum iscsi_param param, char *buf, int buflen)
686 {
687 	int value;
688 
689 	switch (param) {
690 	case ISCSI_PARAM_MAX_RECV_DLENGTH:
691 		/* TBD */
692 		break;
693 	case ISCSI_PARAM_HDRDGST_EN:
694 		sscanf(buf, "%d", &value);
695 		if (value) {
696 			iser_err("DataDigest wasn't negotiated to None\n");
697 			return -EPROTO;
698 		}
699 		break;
700 	case ISCSI_PARAM_DATADGST_EN:
701 		sscanf(buf, "%d", &value);
702 		if (value) {
703 			iser_err("DataDigest wasn't negotiated to None\n");
704 			return -EPROTO;
705 		}
706 		break;
707 	case ISCSI_PARAM_IFMARKER_EN:
708 		sscanf(buf, "%d", &value);
709 		if (value) {
710 			iser_err("IFMarker wasn't negotiated to No\n");
711 			return -EPROTO;
712 		}
713 		break;
714 	case ISCSI_PARAM_OFMARKER_EN:
715 		sscanf(buf, "%d", &value);
716 		if (value) {
717 			iser_err("OFMarker wasn't negotiated to No\n");
718 			return -EPROTO;
719 		}
720 		break;
721 	default:
722 		return iscsi_set_param(cls_conn, param, buf, buflen);
723 	}
724 
725 	return 0;
726 }
727 
728 /**
729  * iscsi_iser_set_param() - set class connection parameter
730  * @cls_conn:    iscsi class connection
731  * @stats:       iscsi stats to output
732  *
733  * Output connection statistics.
734  */
735 static void
iscsi_iser_conn_get_stats(struct iscsi_cls_conn * cls_conn,struct iscsi_stats * stats)736 iscsi_iser_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
737 {
738 	struct iscsi_conn *conn = cls_conn->dd_data;
739 
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;
758 }
759 
iscsi_iser_get_ep_param(struct iscsi_endpoint * ep,enum iscsi_param param,char * buf)760 static int iscsi_iser_get_ep_param(struct iscsi_endpoint *ep,
761 				   enum iscsi_param param, char *buf)
762 {
763 	struct iser_conn *iser_conn = ep->dd_data;
764 	int len;
765 
766 	switch (param) {
767 	case ISCSI_PARAM_CONN_PORT:
768 	case ISCSI_PARAM_CONN_ADDRESS:
769 		if (!iser_conn || !iser_conn->ib_conn.cma_id)
770 			return -ENOTCONN;
771 
772 		return iscsi_conn_get_addr_param((struct sockaddr_storage *)
773 				&iser_conn->ib_conn.cma_id->route.addr.dst_addr,
774 				param, buf);
775 		break;
776 	default:
777 		return -ENOSYS;
778 	}
779 
780 	return len;
781 }
782 
783 /**
784  * iscsi_iser_ep_connect() - Initiate iSER connection establishment
785  * @shost:          scsi_host
786  * @dst_addr:       destination address
787  * @non-blocking:   indicate if routine can block
788  *
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.
794  *
795  * Return: iscsi_endpoint created by iscsi layer or ERR_PTR(error)
796  *         if fails.
797  */
798 static struct iscsi_endpoint *
iscsi_iser_ep_connect(struct Scsi_Host * shost,struct sockaddr * dst_addr,int non_blocking)799 iscsi_iser_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
800 		      int non_blocking)
801 {
802 	int err;
803 	struct iser_conn *iser_conn;
804 	struct iscsi_endpoint *ep;
805 
806 	ep = iscsi_create_endpoint(0);
807 	if (!ep)
808 		return ERR_PTR(-ENOMEM);
809 
810 	iser_conn = kzalloc(sizeof(*iser_conn), GFP_KERNEL);
811 	if (!iser_conn) {
812 		err = -ENOMEM;
813 		goto failure;
814 	}
815 
816 	ep->dd_data = iser_conn;
817 	iser_conn->ep = ep;
818 	iser_conn_init(iser_conn);
819 
820 	err = iser_connect(iser_conn, NULL, dst_addr, non_blocking);
821 	if (err)
822 		goto failure;
823 
824 	return ep;
825 failure:
826 	iscsi_destroy_endpoint(ep);
827 	return ERR_PTR(err);
828 }
829 
830 /**
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.
834  *
835  * This routine boils down to waiting for up_completion signaling
836  * that cma_id got CONNECTED event.
837  *
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.
842  */
843 static int
iscsi_iser_ep_poll(struct iscsi_endpoint * ep,int timeout_ms)844 iscsi_iser_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
845 {
846 	struct iser_conn *iser_conn;
847 	int rc;
848 
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 */
853 	if (rc == 0) {
854 		mutex_lock(&iser_conn->state_mutex);
855 		if (iser_conn->state == ISER_CONN_TERMINATING ||
856 		    iser_conn->state == ISER_CONN_DOWN)
857 			rc = -1;
858 		mutex_unlock(&iser_conn->state_mutex);
859 	}
860 
861 	iser_info("ib conn %p rc = %d\n", iser_conn, rc);
862 
863 	if (rc > 0)
864 		return 1; /* success, this is the equivalent of POLLOUT */
865 	else if (!rc)
866 		return 0; /* timeout */
867 	else
868 		return rc; /* signal */
869 }
870 
871 /**
872  * iscsi_iser_ep_disconnect() - Initiate connection teardown process
873  * @ep:    iscsi endpoint handle
874  *
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.
879  */
880 static void
iscsi_iser_ep_disconnect(struct iscsi_endpoint * ep)881 iscsi_iser_ep_disconnect(struct iscsi_endpoint *ep)
882 {
883 	struct iser_conn *iser_conn;
884 
885 	iser_conn = ep->dd_data;
886 	iser_info("ep %p iser conn %p state %d\n",
887 		  ep, iser_conn, iser_conn->state);
888 
889 	mutex_lock(&iser_conn->state_mutex);
890 	iser_conn_terminate(iser_conn);
891 
892 	/*
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
896 	 * immediately.
897 	 */
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);
902 	} else {
903 		iser_conn->state = ISER_CONN_DOWN;
904 		mutex_unlock(&iser_conn->state_mutex);
905 		iser_conn_release(iser_conn);
906 	}
907 	iscsi_destroy_endpoint(ep);
908 }
909 
iser_attr_is_visible(int param_type,int param)910 static umode_t iser_attr_is_visible(int param_type, int param)
911 {
912 	switch (param_type) {
913 	case ISCSI_HOST_PARAM:
914 		switch (param) {
915 		case ISCSI_HOST_PARAM_NETDEV_NAME:
916 		case ISCSI_HOST_PARAM_HWADDRESS:
917 		case ISCSI_HOST_PARAM_INITIATOR_NAME:
918 			return S_IRUGO;
919 		default:
920 			return 0;
921 		}
922 	case ISCSI_PARAM:
923 		switch (param) {
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:
955 			return S_IRUGO;
956 		default:
957 			return 0;
958 		}
959 	}
960 
961 	return 0;
962 }
963 
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,
970 	.max_sectors		= 1024,
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",
978 	.this_id                = -1,
979 	.track_queue_depth	= 1,
980 };
981 
982 static struct iscsi_transport iscsi_iser_transport = {
983 	.owner                  = THIS_MODULE,
984 	.name                   = "iser",
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,
1003 	/* IO */
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,
1011 	/* recovery */
1012 	.session_recovery_timedout = iscsi_session_recovery_timedout,
1013 
1014 	.ep_connect             = iscsi_iser_ep_connect,
1015 	.ep_poll                = iscsi_iser_ep_poll,
1016 	.ep_disconnect          = iscsi_iser_ep_disconnect
1017 };
1018 
iser_init(void)1019 static int __init iser_init(void)
1020 {
1021 	int err;
1022 
1023 	iser_dbg("Starting iSER datamover...\n");
1024 
1025 	if (iscsi_max_lun < 1) {
1026 		iser_err("Invalid max_lun value of %u\n", iscsi_max_lun);
1027 		return -EINVAL;
1028 	}
1029 
1030 	memset(&ig, 0, sizeof(struct iser_global));
1031 
1032 	ig.desc_cache = kmem_cache_create("iser_descriptors",
1033 					  sizeof(struct iser_tx_desc),
1034 					  0, SLAB_HWCACHE_ALIGN,
1035 					  NULL);
1036 	if (ig.desc_cache == NULL)
1037 		return -ENOMEM;
1038 
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);
1044 
1045 	release_wq = alloc_workqueue("release workqueue", 0, 0);
1046 	if (!release_wq) {
1047 		iser_err("failed to allocate release workqueue\n");
1048 		return -ENOMEM;
1049 	}
1050 
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");
1055 		err = -EINVAL;
1056 		goto register_transport_failure;
1057 	}
1058 
1059 	return 0;
1060 
1061 register_transport_failure:
1062 	kmem_cache_destroy(ig.desc_cache);
1063 
1064 	return err;
1065 }
1066 
iser_exit(void)1067 static void __exit iser_exit(void)
1068 {
1069 	struct iser_conn *iser_conn, *n;
1070 	int connlist_empty;
1071 
1072 	iser_dbg("Removing iSER datamover...\n");
1073 	destroy_workqueue(release_wq);
1074 
1075 	mutex_lock(&ig.connlist_mutex);
1076 	connlist_empty = list_empty(&ig.connlist);
1077 	mutex_unlock(&ig.connlist_mutex);
1078 
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,
1083 					 conn_list) {
1084 			iser_conn_release(iser_conn);
1085 		}
1086 	}
1087 
1088 	iscsi_unregister_transport(&iscsi_iser_transport);
1089 	kmem_cache_destroy(ig.desc_cache);
1090 }
1091 
1092 module_init(iser_init);
1093 module_exit(iser_exit);
1094