• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2004, 2005 Intel Corporation.  All rights reserved.
3  * Copyright (c) 2004 Topspin Corporation.  All rights reserved.
4  * Copyright (c) 2004, 2005 Voltaire Corporation.  All rights reserved.
5  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
6  * Copyright (c) 2005 Open Grid Computing, Inc. All rights reserved.
7  * Copyright (c) 2005 Network Appliance, Inc. All rights reserved.
8  *
9  * This software is available to you under a choice of one of two
10  * licenses.  You may choose to be licensed under the terms of the GNU
11  * General Public License (GPL) Version 2, available from the file
12  * COPYING in the main directory of this source tree, or the
13  * OpenIB.org BSD license below:
14  *
15  *     Redistribution and use in source and binary forms, with or
16  *     without modification, are permitted provided that the following
17  *     conditions are met:
18  *
19  *      - Redistributions of source code must retain the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer.
22  *
23  *      - Redistributions in binary form must reproduce the above
24  *        copyright notice, this list of conditions and the following
25  *        disclaimer in the documentation and/or other materials
26  *        provided with the distribution.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
32  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
33  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35  * SOFTWARE.
36  *
37  */
38 #include <linux/dma-mapping.h>
39 #include <linux/err.h>
40 #include <linux/idr.h>
41 #include <linux/interrupt.h>
42 #include <linux/rbtree.h>
43 #include <linux/sched.h>
44 #include <linux/spinlock.h>
45 #include <linux/workqueue.h>
46 #include <linux/completion.h>
47 #include <linux/slab.h>
48 #include <linux/module.h>
49 #include <linux/sysctl.h>
50 
51 #include <rdma/iw_cm.h>
52 #include <rdma/ib_addr.h>
53 #include <rdma/iw_portmap.h>
54 #include <rdma/rdma_netlink.h>
55 
56 #include "iwcm.h"
57 
58 MODULE_AUTHOR("Tom Tucker");
59 MODULE_DESCRIPTION("iWARP CM");
60 MODULE_LICENSE("Dual BSD/GPL");
61 
62 static const char * const iwcm_rej_reason_strs[] = {
63 	[ECONNRESET]			= "reset by remote host",
64 	[ECONNREFUSED]			= "refused by remote application",
65 	[ETIMEDOUT]			= "setup timeout",
66 };
67 
iwcm_reject_msg(int reason)68 const char *__attribute_const__ iwcm_reject_msg(int reason)
69 {
70 	size_t index;
71 
72 	/* iWARP uses negative errnos */
73 	index = -reason;
74 
75 	if (index < ARRAY_SIZE(iwcm_rej_reason_strs) &&
76 	    iwcm_rej_reason_strs[index])
77 		return iwcm_rej_reason_strs[index];
78 	else
79 		return "unrecognized reason";
80 }
81 EXPORT_SYMBOL(iwcm_reject_msg);
82 
83 static struct rdma_nl_cbs iwcm_nl_cb_table[RDMA_NL_IWPM_NUM_OPS] = {
84 	[RDMA_NL_IWPM_REG_PID] = {.dump = iwpm_register_pid_cb},
85 	[RDMA_NL_IWPM_ADD_MAPPING] = {.dump = iwpm_add_mapping_cb},
86 	[RDMA_NL_IWPM_QUERY_MAPPING] = {.dump = iwpm_add_and_query_mapping_cb},
87 	[RDMA_NL_IWPM_REMOTE_INFO] = {.dump = iwpm_remote_info_cb},
88 	[RDMA_NL_IWPM_HANDLE_ERR] = {.dump = iwpm_mapping_error_cb},
89 	[RDMA_NL_IWPM_MAPINFO] = {.dump = iwpm_mapping_info_cb},
90 	[RDMA_NL_IWPM_MAPINFO_NUM] = {.dump = iwpm_ack_mapping_info_cb},
91 	[RDMA_NL_IWPM_HELLO] = {.dump = iwpm_hello_cb}
92 };
93 
94 static struct workqueue_struct *iwcm_wq;
95 struct iwcm_work {
96 	struct work_struct work;
97 	struct iwcm_id_private *cm_id;
98 	struct list_head list;
99 	struct iw_cm_event event;
100 	struct list_head free_list;
101 };
102 
103 static unsigned int default_backlog = 256;
104 
105 static struct ctl_table_header *iwcm_ctl_table_hdr;
106 static struct ctl_table iwcm_ctl_table[] = {
107 	{
108 		.procname	= "default_backlog",
109 		.data		= &default_backlog,
110 		.maxlen		= sizeof(default_backlog),
111 		.mode		= 0644,
112 		.proc_handler	= proc_dointvec,
113 	},
114 };
115 
116 /*
117  * The following services provide a mechanism for pre-allocating iwcm_work
118  * elements.  The design pre-allocates them  based on the cm_id type:
119  *	LISTENING IDS: 	Get enough elements preallocated to handle the
120  *			listen backlog.
121  *	ACTIVE IDS:	4: CONNECT_REPLY, ESTABLISHED, DISCONNECT, CLOSE
122  *	PASSIVE IDS:	3: ESTABLISHED, DISCONNECT, CLOSE
123  *
124  * Allocating them in connect and listen avoids having to deal
125  * with allocation failures on the event upcall from the provider (which
126  * is called in the interrupt context).
127  *
128  * One exception is when creating the cm_id for incoming connection requests.
129  * There are two cases:
130  * 1) in the event upcall, cm_event_handler(), for a listening cm_id.  If
131  *    the backlog is exceeded, then no more connection request events will
132  *    be processed.  cm_event_handler() returns -ENOMEM in this case.  Its up
133  *    to the provider to reject the connection request.
134  * 2) in the connection request workqueue handler, cm_conn_req_handler().
135  *    If work elements cannot be allocated for the new connect request cm_id,
136  *    then IWCM will call the provider reject method.  This is ok since
137  *    cm_conn_req_handler() runs in the workqueue thread context.
138  */
139 
get_work(struct iwcm_id_private * cm_id_priv)140 static struct iwcm_work *get_work(struct iwcm_id_private *cm_id_priv)
141 {
142 	struct iwcm_work *work;
143 
144 	if (list_empty(&cm_id_priv->work_free_list))
145 		return NULL;
146 	work = list_first_entry(&cm_id_priv->work_free_list, struct iwcm_work,
147 				free_list);
148 	list_del_init(&work->free_list);
149 	return work;
150 }
151 
put_work(struct iwcm_work * work)152 static void put_work(struct iwcm_work *work)
153 {
154 	list_add(&work->free_list, &work->cm_id->work_free_list);
155 }
156 
dealloc_work_entries(struct iwcm_id_private * cm_id_priv)157 static void dealloc_work_entries(struct iwcm_id_private *cm_id_priv)
158 {
159 	struct list_head *e, *tmp;
160 
161 	list_for_each_safe(e, tmp, &cm_id_priv->work_free_list) {
162 		list_del(e);
163 		kfree(list_entry(e, struct iwcm_work, free_list));
164 	}
165 }
166 
alloc_work_entries(struct iwcm_id_private * cm_id_priv,int count)167 static int alloc_work_entries(struct iwcm_id_private *cm_id_priv, int count)
168 {
169 	struct iwcm_work *work;
170 
171 	BUG_ON(!list_empty(&cm_id_priv->work_free_list));
172 	while (count--) {
173 		work = kmalloc(sizeof(struct iwcm_work), GFP_KERNEL);
174 		if (!work) {
175 			dealloc_work_entries(cm_id_priv);
176 			return -ENOMEM;
177 		}
178 		work->cm_id = cm_id_priv;
179 		INIT_LIST_HEAD(&work->list);
180 		put_work(work);
181 	}
182 	return 0;
183 }
184 
185 /*
186  * Save private data from incoming connection requests to
187  * iw_cm_event, so the low level driver doesn't have to. Adjust
188  * the event ptr to point to the local copy.
189  */
copy_private_data(struct iw_cm_event * event)190 static int copy_private_data(struct iw_cm_event *event)
191 {
192 	void *p;
193 
194 	p = kmemdup(event->private_data, event->private_data_len, GFP_ATOMIC);
195 	if (!p)
196 		return -ENOMEM;
197 	event->private_data = p;
198 	return 0;
199 }
200 
free_cm_id(struct iwcm_id_private * cm_id_priv)201 static void free_cm_id(struct iwcm_id_private *cm_id_priv)
202 {
203 	dealloc_work_entries(cm_id_priv);
204 	kfree(cm_id_priv);
205 }
206 
207 /*
208  * Release a reference on cm_id. If the last reference is being
209  * released, free the cm_id and return 'true'.
210  */
iwcm_deref_id(struct iwcm_id_private * cm_id_priv)211 static bool iwcm_deref_id(struct iwcm_id_private *cm_id_priv)
212 {
213 	if (refcount_dec_and_test(&cm_id_priv->refcount)) {
214 		BUG_ON(!list_empty(&cm_id_priv->work_list));
215 		free_cm_id(cm_id_priv);
216 		return true;
217 	}
218 
219 	return false;
220 }
221 
add_ref(struct iw_cm_id * cm_id)222 static void add_ref(struct iw_cm_id *cm_id)
223 {
224 	struct iwcm_id_private *cm_id_priv;
225 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
226 	refcount_inc(&cm_id_priv->refcount);
227 }
228 
rem_ref(struct iw_cm_id * cm_id)229 static void rem_ref(struct iw_cm_id *cm_id)
230 {
231 	struct iwcm_id_private *cm_id_priv;
232 
233 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
234 
235 	(void)iwcm_deref_id(cm_id_priv);
236 }
237 
238 static int cm_event_handler(struct iw_cm_id *cm_id, struct iw_cm_event *event);
239 
iw_create_cm_id(struct ib_device * device,iw_cm_handler cm_handler,void * context)240 struct iw_cm_id *iw_create_cm_id(struct ib_device *device,
241 				 iw_cm_handler cm_handler,
242 				 void *context)
243 {
244 	struct iwcm_id_private *cm_id_priv;
245 
246 	cm_id_priv = kzalloc(sizeof(*cm_id_priv), GFP_KERNEL);
247 	if (!cm_id_priv)
248 		return ERR_PTR(-ENOMEM);
249 
250 	cm_id_priv->state = IW_CM_STATE_IDLE;
251 	cm_id_priv->id.device = device;
252 	cm_id_priv->id.cm_handler = cm_handler;
253 	cm_id_priv->id.context = context;
254 	cm_id_priv->id.event_handler = cm_event_handler;
255 	cm_id_priv->id.add_ref = add_ref;
256 	cm_id_priv->id.rem_ref = rem_ref;
257 	spin_lock_init(&cm_id_priv->lock);
258 	refcount_set(&cm_id_priv->refcount, 1);
259 	init_waitqueue_head(&cm_id_priv->connect_wait);
260 	init_completion(&cm_id_priv->destroy_comp);
261 	INIT_LIST_HEAD(&cm_id_priv->work_list);
262 	INIT_LIST_HEAD(&cm_id_priv->work_free_list);
263 
264 	return &cm_id_priv->id;
265 }
266 EXPORT_SYMBOL(iw_create_cm_id);
267 
268 
iwcm_modify_qp_err(struct ib_qp * qp)269 static int iwcm_modify_qp_err(struct ib_qp *qp)
270 {
271 	struct ib_qp_attr qp_attr;
272 
273 	if (!qp)
274 		return -EINVAL;
275 
276 	qp_attr.qp_state = IB_QPS_ERR;
277 	return ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
278 }
279 
280 /*
281  * This is really the RDMAC CLOSING state. It is most similar to the
282  * IB SQD QP state.
283  */
iwcm_modify_qp_sqd(struct ib_qp * qp)284 static int iwcm_modify_qp_sqd(struct ib_qp *qp)
285 {
286 	struct ib_qp_attr qp_attr;
287 
288 	BUG_ON(qp == NULL);
289 	qp_attr.qp_state = IB_QPS_SQD;
290 	return ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
291 }
292 
293 /*
294  * CM_ID <-- CLOSING
295  *
296  * Block if a passive or active connection is currently being processed. Then
297  * process the event as follows:
298  * - If we are ESTABLISHED, move to CLOSING and modify the QP state
299  *   based on the abrupt flag
300  * - If the connection is already in the CLOSING or IDLE state, the peer is
301  *   disconnecting concurrently with us and we've already seen the
302  *   DISCONNECT event -- ignore the request and return 0
303  * - Disconnect on a listening endpoint returns -EINVAL
304  */
iw_cm_disconnect(struct iw_cm_id * cm_id,int abrupt)305 int iw_cm_disconnect(struct iw_cm_id *cm_id, int abrupt)
306 {
307 	struct iwcm_id_private *cm_id_priv;
308 	unsigned long flags;
309 	int ret = 0;
310 	struct ib_qp *qp = NULL;
311 
312 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
313 	/* Wait if we're currently in a connect or accept downcall */
314 	wait_event(cm_id_priv->connect_wait,
315 		   !test_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags));
316 
317 	spin_lock_irqsave(&cm_id_priv->lock, flags);
318 	switch (cm_id_priv->state) {
319 	case IW_CM_STATE_ESTABLISHED:
320 		cm_id_priv->state = IW_CM_STATE_CLOSING;
321 
322 		/* QP could be <nul> for user-mode client */
323 		if (cm_id_priv->qp)
324 			qp = cm_id_priv->qp;
325 		else
326 			ret = -EINVAL;
327 		break;
328 	case IW_CM_STATE_LISTEN:
329 		ret = -EINVAL;
330 		break;
331 	case IW_CM_STATE_CLOSING:
332 		/* remote peer closed first */
333 	case IW_CM_STATE_IDLE:
334 		/* accept or connect returned !0 */
335 		break;
336 	case IW_CM_STATE_CONN_RECV:
337 		/*
338 		 * App called disconnect before/without calling accept after
339 		 * connect_request event delivered.
340 		 */
341 		break;
342 	case IW_CM_STATE_CONN_SENT:
343 		/* Can only get here if wait above fails */
344 	default:
345 		BUG();
346 	}
347 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
348 
349 	if (qp) {
350 		if (abrupt)
351 			ret = iwcm_modify_qp_err(qp);
352 		else
353 			ret = iwcm_modify_qp_sqd(qp);
354 
355 		/*
356 		 * If both sides are disconnecting the QP could
357 		 * already be in ERR or SQD states
358 		 */
359 		ret = 0;
360 	}
361 
362 	return ret;
363 }
364 EXPORT_SYMBOL(iw_cm_disconnect);
365 
366 /*
367  * CM_ID <-- DESTROYING
368  *
369  * Clean up all resources associated with the connection.
370  */
destroy_cm_id(struct iw_cm_id * cm_id)371 static void destroy_cm_id(struct iw_cm_id *cm_id)
372 {
373 	struct iwcm_id_private *cm_id_priv;
374 	struct ib_qp *qp;
375 	unsigned long flags;
376 
377 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
378 	/*
379 	 * Wait if we're currently in a connect or accept downcall. A
380 	 * listening endpoint should never block here.
381 	 */
382 	wait_event(cm_id_priv->connect_wait,
383 		   !test_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags));
384 
385 	/*
386 	 * Since we're deleting the cm_id, drop any events that
387 	 * might arrive before the last dereference.
388 	 */
389 	set_bit(IWCM_F_DROP_EVENTS, &cm_id_priv->flags);
390 
391 	spin_lock_irqsave(&cm_id_priv->lock, flags);
392 	qp = cm_id_priv->qp;
393 	cm_id_priv->qp = NULL;
394 
395 	switch (cm_id_priv->state) {
396 	case IW_CM_STATE_LISTEN:
397 		cm_id_priv->state = IW_CM_STATE_DESTROYING;
398 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
399 		/* destroy the listening endpoint */
400 		cm_id->device->ops.iw_destroy_listen(cm_id);
401 		spin_lock_irqsave(&cm_id_priv->lock, flags);
402 		break;
403 	case IW_CM_STATE_ESTABLISHED:
404 		cm_id_priv->state = IW_CM_STATE_DESTROYING;
405 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
406 		/* Abrupt close of the connection */
407 		(void)iwcm_modify_qp_err(qp);
408 		spin_lock_irqsave(&cm_id_priv->lock, flags);
409 		break;
410 	case IW_CM_STATE_IDLE:
411 	case IW_CM_STATE_CLOSING:
412 		cm_id_priv->state = IW_CM_STATE_DESTROYING;
413 		break;
414 	case IW_CM_STATE_CONN_RECV:
415 		/*
416 		 * App called destroy before/without calling accept after
417 		 * receiving connection request event notification or
418 		 * returned non zero from the event callback function.
419 		 * In either case, must tell the provider to reject.
420 		 */
421 		cm_id_priv->state = IW_CM_STATE_DESTROYING;
422 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
423 		cm_id->device->ops.iw_reject(cm_id, NULL, 0);
424 		spin_lock_irqsave(&cm_id_priv->lock, flags);
425 		break;
426 	case IW_CM_STATE_CONN_SENT:
427 	case IW_CM_STATE_DESTROYING:
428 	default:
429 		BUG();
430 		break;
431 	}
432 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
433 	if (qp)
434 		cm_id_priv->id.device->ops.iw_rem_ref(qp);
435 
436 	if (cm_id->mapped) {
437 		iwpm_remove_mapinfo(&cm_id->local_addr, &cm_id->m_local_addr);
438 		iwpm_remove_mapping(&cm_id->local_addr, RDMA_NL_IWCM);
439 	}
440 }
441 
442 /*
443  * Destroy cm_id. If the cm_id still has other references, wait for all
444  * references to be released on the cm_id and then release the initial
445  * reference taken by iw_create_cm_id.
446  */
iw_destroy_cm_id(struct iw_cm_id * cm_id)447 void iw_destroy_cm_id(struct iw_cm_id *cm_id)
448 {
449 	struct iwcm_id_private *cm_id_priv;
450 
451 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
452 	destroy_cm_id(cm_id);
453 	if (refcount_read(&cm_id_priv->refcount) > 1)
454 		flush_workqueue(iwcm_wq);
455 	iwcm_deref_id(cm_id_priv);
456 }
457 EXPORT_SYMBOL(iw_destroy_cm_id);
458 
459 /**
460  * iw_cm_check_wildcard - If IP address is 0 then use original
461  * @pm_addr: sockaddr containing the ip to check for wildcard
462  * @cm_addr: sockaddr containing the actual IP address
463  * @cm_outaddr: sockaddr to set IP addr which leaving port
464  *
465  *  Checks the pm_addr for wildcard and then sets cm_outaddr's
466  *  IP to the actual (cm_addr).
467  */
iw_cm_check_wildcard(struct sockaddr_storage * pm_addr,struct sockaddr_storage * cm_addr,struct sockaddr_storage * cm_outaddr)468 static void iw_cm_check_wildcard(struct sockaddr_storage *pm_addr,
469 				 struct sockaddr_storage *cm_addr,
470 				 struct sockaddr_storage *cm_outaddr)
471 {
472 	if (pm_addr->ss_family == AF_INET) {
473 		struct sockaddr_in *pm4_addr = (struct sockaddr_in *)pm_addr;
474 
475 		if (pm4_addr->sin_addr.s_addr == htonl(INADDR_ANY)) {
476 			struct sockaddr_in *cm4_addr =
477 				(struct sockaddr_in *)cm_addr;
478 			struct sockaddr_in *cm4_outaddr =
479 				(struct sockaddr_in *)cm_outaddr;
480 
481 			cm4_outaddr->sin_addr = cm4_addr->sin_addr;
482 		}
483 	} else {
484 		struct sockaddr_in6 *pm6_addr = (struct sockaddr_in6 *)pm_addr;
485 
486 		if (ipv6_addr_type(&pm6_addr->sin6_addr) == IPV6_ADDR_ANY) {
487 			struct sockaddr_in6 *cm6_addr =
488 				(struct sockaddr_in6 *)cm_addr;
489 			struct sockaddr_in6 *cm6_outaddr =
490 				(struct sockaddr_in6 *)cm_outaddr;
491 
492 			cm6_outaddr->sin6_addr = cm6_addr->sin6_addr;
493 		}
494 	}
495 }
496 
497 /**
498  * iw_cm_map - Use portmapper to map the ports
499  * @cm_id: connection manager pointer
500  * @active: Indicates the active side when true
501  * returns nonzero for error only if iwpm_create_mapinfo() fails
502  *
503  * Tries to add a mapping for a port using the Portmapper. If
504  * successful in mapping the IP/Port it will check the remote
505  * mapped IP address for a wildcard IP address and replace the
506  * zero IP address with the remote_addr.
507  */
iw_cm_map(struct iw_cm_id * cm_id,bool active)508 static int iw_cm_map(struct iw_cm_id *cm_id, bool active)
509 {
510 	const char *devname = dev_name(&cm_id->device->dev);
511 	const char *ifname = cm_id->device->iw_ifname;
512 	struct iwpm_dev_data pm_reg_msg = {};
513 	struct iwpm_sa_data pm_msg;
514 	int status;
515 
516 	if (strlen(devname) >= sizeof(pm_reg_msg.dev_name) ||
517 	    strlen(ifname) >= sizeof(pm_reg_msg.if_name))
518 		return -EINVAL;
519 
520 	cm_id->m_local_addr = cm_id->local_addr;
521 	cm_id->m_remote_addr = cm_id->remote_addr;
522 
523 	strcpy(pm_reg_msg.dev_name, devname);
524 	strcpy(pm_reg_msg.if_name, ifname);
525 
526 	if (iwpm_register_pid(&pm_reg_msg, RDMA_NL_IWCM) ||
527 	    !iwpm_valid_pid())
528 		return 0;
529 
530 	cm_id->mapped = true;
531 	pm_msg.loc_addr = cm_id->local_addr;
532 	pm_msg.rem_addr = cm_id->remote_addr;
533 	pm_msg.flags = (cm_id->device->iw_driver_flags & IW_F_NO_PORT_MAP) ?
534 		       IWPM_FLAGS_NO_PORT_MAP : 0;
535 	if (active)
536 		status = iwpm_add_and_query_mapping(&pm_msg,
537 						    RDMA_NL_IWCM);
538 	else
539 		status = iwpm_add_mapping(&pm_msg, RDMA_NL_IWCM);
540 
541 	if (!status) {
542 		cm_id->m_local_addr = pm_msg.mapped_loc_addr;
543 		if (active) {
544 			cm_id->m_remote_addr = pm_msg.mapped_rem_addr;
545 			iw_cm_check_wildcard(&pm_msg.mapped_rem_addr,
546 					     &cm_id->remote_addr,
547 					     &cm_id->m_remote_addr);
548 		}
549 	}
550 
551 	return iwpm_create_mapinfo(&cm_id->local_addr,
552 				   &cm_id->m_local_addr,
553 				   RDMA_NL_IWCM, pm_msg.flags);
554 }
555 
556 /*
557  * CM_ID <-- LISTEN
558  *
559  * Start listening for connect requests. Generates one CONNECT_REQUEST
560  * event for each inbound connect request.
561  */
iw_cm_listen(struct iw_cm_id * cm_id,int backlog)562 int iw_cm_listen(struct iw_cm_id *cm_id, int backlog)
563 {
564 	struct iwcm_id_private *cm_id_priv;
565 	unsigned long flags;
566 	int ret;
567 
568 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
569 
570 	if (!backlog)
571 		backlog = default_backlog;
572 
573 	ret = alloc_work_entries(cm_id_priv, backlog);
574 	if (ret)
575 		return ret;
576 
577 	spin_lock_irqsave(&cm_id_priv->lock, flags);
578 	switch (cm_id_priv->state) {
579 	case IW_CM_STATE_IDLE:
580 		cm_id_priv->state = IW_CM_STATE_LISTEN;
581 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
582 		ret = iw_cm_map(cm_id, false);
583 		if (!ret)
584 			ret = cm_id->device->ops.iw_create_listen(cm_id,
585 								  backlog);
586 		if (ret)
587 			cm_id_priv->state = IW_CM_STATE_IDLE;
588 		spin_lock_irqsave(&cm_id_priv->lock, flags);
589 		break;
590 	default:
591 		ret = -EINVAL;
592 	}
593 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
594 
595 	return ret;
596 }
597 EXPORT_SYMBOL(iw_cm_listen);
598 
599 /*
600  * CM_ID <-- IDLE
601  *
602  * Rejects an inbound connection request. No events are generated.
603  */
iw_cm_reject(struct iw_cm_id * cm_id,const void * private_data,u8 private_data_len)604 int iw_cm_reject(struct iw_cm_id *cm_id,
605 		 const void *private_data,
606 		 u8 private_data_len)
607 {
608 	struct iwcm_id_private *cm_id_priv;
609 	unsigned long flags;
610 	int ret;
611 
612 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
613 	set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
614 
615 	spin_lock_irqsave(&cm_id_priv->lock, flags);
616 	if (cm_id_priv->state != IW_CM_STATE_CONN_RECV) {
617 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
618 		clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
619 		wake_up_all(&cm_id_priv->connect_wait);
620 		return -EINVAL;
621 	}
622 	cm_id_priv->state = IW_CM_STATE_IDLE;
623 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
624 
625 	ret = cm_id->device->ops.iw_reject(cm_id, private_data,
626 					  private_data_len);
627 
628 	clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
629 	wake_up_all(&cm_id_priv->connect_wait);
630 
631 	return ret;
632 }
633 EXPORT_SYMBOL(iw_cm_reject);
634 
635 /*
636  * CM_ID <-- ESTABLISHED
637  *
638  * Accepts an inbound connection request and generates an ESTABLISHED
639  * event. Callers of iw_cm_disconnect and iw_destroy_cm_id will block
640  * until the ESTABLISHED event is received from the provider.
641  */
iw_cm_accept(struct iw_cm_id * cm_id,struct iw_cm_conn_param * iw_param)642 int iw_cm_accept(struct iw_cm_id *cm_id,
643 		 struct iw_cm_conn_param *iw_param)
644 {
645 	struct iwcm_id_private *cm_id_priv;
646 	struct ib_qp *qp;
647 	unsigned long flags;
648 	int ret;
649 
650 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
651 	set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
652 
653 	spin_lock_irqsave(&cm_id_priv->lock, flags);
654 	if (cm_id_priv->state != IW_CM_STATE_CONN_RECV) {
655 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
656 		clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
657 		wake_up_all(&cm_id_priv->connect_wait);
658 		return -EINVAL;
659 	}
660 	/* Get the ib_qp given the QPN */
661 	qp = cm_id->device->ops.iw_get_qp(cm_id->device, iw_param->qpn);
662 	if (!qp) {
663 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
664 		clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
665 		wake_up_all(&cm_id_priv->connect_wait);
666 		return -EINVAL;
667 	}
668 	cm_id->device->ops.iw_add_ref(qp);
669 	cm_id_priv->qp = qp;
670 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
671 
672 	ret = cm_id->device->ops.iw_accept(cm_id, iw_param);
673 	if (ret) {
674 		/* An error on accept precludes provider events */
675 		BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_RECV);
676 		cm_id_priv->state = IW_CM_STATE_IDLE;
677 		spin_lock_irqsave(&cm_id_priv->lock, flags);
678 		qp = cm_id_priv->qp;
679 		cm_id_priv->qp = NULL;
680 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
681 		if (qp)
682 			cm_id->device->ops.iw_rem_ref(qp);
683 		clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
684 		wake_up_all(&cm_id_priv->connect_wait);
685 	}
686 
687 	return ret;
688 }
689 EXPORT_SYMBOL(iw_cm_accept);
690 
691 /*
692  * Active Side: CM_ID <-- CONN_SENT
693  *
694  * If successful, results in the generation of a CONNECT_REPLY
695  * event. iw_cm_disconnect and iw_cm_destroy will block until the
696  * CONNECT_REPLY event is received from the provider.
697  */
iw_cm_connect(struct iw_cm_id * cm_id,struct iw_cm_conn_param * iw_param)698 int iw_cm_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *iw_param)
699 {
700 	struct iwcm_id_private *cm_id_priv;
701 	int ret;
702 	unsigned long flags;
703 	struct ib_qp *qp = NULL;
704 
705 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
706 
707 	ret = alloc_work_entries(cm_id_priv, 4);
708 	if (ret)
709 		return ret;
710 
711 	set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
712 	spin_lock_irqsave(&cm_id_priv->lock, flags);
713 
714 	if (cm_id_priv->state != IW_CM_STATE_IDLE) {
715 		ret = -EINVAL;
716 		goto err;
717 	}
718 
719 	/* Get the ib_qp given the QPN */
720 	qp = cm_id->device->ops.iw_get_qp(cm_id->device, iw_param->qpn);
721 	if (!qp) {
722 		ret = -EINVAL;
723 		goto err;
724 	}
725 	cm_id->device->ops.iw_add_ref(qp);
726 	cm_id_priv->qp = qp;
727 	cm_id_priv->state = IW_CM_STATE_CONN_SENT;
728 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
729 
730 	ret = iw_cm_map(cm_id, true);
731 	if (!ret)
732 		ret = cm_id->device->ops.iw_connect(cm_id, iw_param);
733 	if (!ret)
734 		return 0;	/* success */
735 
736 	spin_lock_irqsave(&cm_id_priv->lock, flags);
737 	qp = cm_id_priv->qp;
738 	cm_id_priv->qp = NULL;
739 	cm_id_priv->state = IW_CM_STATE_IDLE;
740 err:
741 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
742 	if (qp)
743 		cm_id->device->ops.iw_rem_ref(qp);
744 	clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
745 	wake_up_all(&cm_id_priv->connect_wait);
746 	return ret;
747 }
748 EXPORT_SYMBOL(iw_cm_connect);
749 
750 /*
751  * Passive Side: new CM_ID <-- CONN_RECV
752  *
753  * Handles an inbound connect request. The function creates a new
754  * iw_cm_id to represent the new connection and inherits the client
755  * callback function and other attributes from the listening parent.
756  *
757  * The work item contains a pointer to the listen_cm_id and the event. The
758  * listen_cm_id contains the client cm_handler, context and
759  * device. These are copied when the device is cloned. The event
760  * contains the new four tuple.
761  *
762  * An error on the child should not affect the parent, so this
763  * function does not return a value.
764  */
cm_conn_req_handler(struct iwcm_id_private * listen_id_priv,struct iw_cm_event * iw_event)765 static void cm_conn_req_handler(struct iwcm_id_private *listen_id_priv,
766 				struct iw_cm_event *iw_event)
767 {
768 	unsigned long flags;
769 	struct iw_cm_id *cm_id;
770 	struct iwcm_id_private *cm_id_priv;
771 	int ret;
772 
773 	/*
774 	 * The provider should never generate a connection request
775 	 * event with a bad status.
776 	 */
777 	BUG_ON(iw_event->status);
778 
779 	cm_id = iw_create_cm_id(listen_id_priv->id.device,
780 				listen_id_priv->id.cm_handler,
781 				listen_id_priv->id.context);
782 	/* If the cm_id could not be created, ignore the request */
783 	if (IS_ERR(cm_id))
784 		goto out;
785 
786 	cm_id->provider_data = iw_event->provider_data;
787 	cm_id->m_local_addr = iw_event->local_addr;
788 	cm_id->m_remote_addr = iw_event->remote_addr;
789 	cm_id->local_addr = listen_id_priv->id.local_addr;
790 
791 	ret = iwpm_get_remote_info(&listen_id_priv->id.m_local_addr,
792 				   &iw_event->remote_addr,
793 				   &cm_id->remote_addr,
794 				   RDMA_NL_IWCM);
795 	if (ret) {
796 		cm_id->remote_addr = iw_event->remote_addr;
797 	} else {
798 		iw_cm_check_wildcard(&listen_id_priv->id.m_local_addr,
799 				     &iw_event->local_addr,
800 				     &cm_id->local_addr);
801 		iw_event->local_addr = cm_id->local_addr;
802 		iw_event->remote_addr = cm_id->remote_addr;
803 	}
804 
805 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
806 	cm_id_priv->state = IW_CM_STATE_CONN_RECV;
807 
808 	/*
809 	 * We could be destroying the listening id. If so, ignore this
810 	 * upcall.
811 	 */
812 	spin_lock_irqsave(&listen_id_priv->lock, flags);
813 	if (listen_id_priv->state != IW_CM_STATE_LISTEN) {
814 		spin_unlock_irqrestore(&listen_id_priv->lock, flags);
815 		iw_cm_reject(cm_id, NULL, 0);
816 		iw_destroy_cm_id(cm_id);
817 		goto out;
818 	}
819 	spin_unlock_irqrestore(&listen_id_priv->lock, flags);
820 
821 	ret = alloc_work_entries(cm_id_priv, 3);
822 	if (ret) {
823 		iw_cm_reject(cm_id, NULL, 0);
824 		iw_destroy_cm_id(cm_id);
825 		goto out;
826 	}
827 
828 	/* Call the client CM handler */
829 	ret = cm_id->cm_handler(cm_id, iw_event);
830 	if (ret) {
831 		iw_cm_reject(cm_id, NULL, 0);
832 		iw_destroy_cm_id(cm_id);
833 	}
834 
835 out:
836 	if (iw_event->private_data_len)
837 		kfree(iw_event->private_data);
838 }
839 
840 /*
841  * Passive Side: CM_ID <-- ESTABLISHED
842  *
843  * The provider generated an ESTABLISHED event which means that
844  * the MPA negotion has completed successfully and we are now in MPA
845  * FPDU mode.
846  *
847  * This event can only be received in the CONN_RECV state. If the
848  * remote peer closed, the ESTABLISHED event would be received followed
849  * by the CLOSE event. If the app closes, it will block until we wake
850  * it up after processing this event.
851  */
cm_conn_est_handler(struct iwcm_id_private * cm_id_priv,struct iw_cm_event * iw_event)852 static int cm_conn_est_handler(struct iwcm_id_private *cm_id_priv,
853 			       struct iw_cm_event *iw_event)
854 {
855 	unsigned long flags;
856 	int ret;
857 
858 	spin_lock_irqsave(&cm_id_priv->lock, flags);
859 
860 	/*
861 	 * We clear the CONNECT_WAIT bit here to allow the callback
862 	 * function to call iw_cm_disconnect. Calling iw_destroy_cm_id
863 	 * from a callback handler is not allowed.
864 	 */
865 	clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
866 	BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_RECV);
867 	cm_id_priv->state = IW_CM_STATE_ESTABLISHED;
868 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
869 	ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event);
870 	wake_up_all(&cm_id_priv->connect_wait);
871 
872 	return ret;
873 }
874 
875 /*
876  * Active Side: CM_ID <-- ESTABLISHED
877  *
878  * The app has called connect and is waiting for the established event to
879  * post it's requests to the server. This event will wake up anyone
880  * blocked in iw_cm_disconnect or iw_destroy_id.
881  */
cm_conn_rep_handler(struct iwcm_id_private * cm_id_priv,struct iw_cm_event * iw_event)882 static int cm_conn_rep_handler(struct iwcm_id_private *cm_id_priv,
883 			       struct iw_cm_event *iw_event)
884 {
885 	struct ib_qp *qp = NULL;
886 	unsigned long flags;
887 	int ret;
888 
889 	spin_lock_irqsave(&cm_id_priv->lock, flags);
890 	/*
891 	 * Clear the connect wait bit so a callback function calling
892 	 * iw_cm_disconnect will not wait and deadlock this thread
893 	 */
894 	clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
895 	BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_SENT);
896 	if (iw_event->status == 0) {
897 		cm_id_priv->id.m_local_addr = iw_event->local_addr;
898 		cm_id_priv->id.m_remote_addr = iw_event->remote_addr;
899 		iw_event->local_addr = cm_id_priv->id.local_addr;
900 		iw_event->remote_addr = cm_id_priv->id.remote_addr;
901 		cm_id_priv->state = IW_CM_STATE_ESTABLISHED;
902 	} else {
903 		/* REJECTED or RESET */
904 		qp = cm_id_priv->qp;
905 		cm_id_priv->qp = NULL;
906 		cm_id_priv->state = IW_CM_STATE_IDLE;
907 	}
908 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
909 	if (qp)
910 		cm_id_priv->id.device->ops.iw_rem_ref(qp);
911 	ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event);
912 
913 	if (iw_event->private_data_len)
914 		kfree(iw_event->private_data);
915 
916 	/* Wake up waiters on connect complete */
917 	wake_up_all(&cm_id_priv->connect_wait);
918 
919 	return ret;
920 }
921 
922 /*
923  * CM_ID <-- CLOSING
924  *
925  * If in the ESTABLISHED state, move to CLOSING.
926  */
cm_disconnect_handler(struct iwcm_id_private * cm_id_priv,struct iw_cm_event * iw_event)927 static void cm_disconnect_handler(struct iwcm_id_private *cm_id_priv,
928 				  struct iw_cm_event *iw_event)
929 {
930 	unsigned long flags;
931 
932 	spin_lock_irqsave(&cm_id_priv->lock, flags);
933 	if (cm_id_priv->state == IW_CM_STATE_ESTABLISHED)
934 		cm_id_priv->state = IW_CM_STATE_CLOSING;
935 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
936 }
937 
938 /*
939  * CM_ID <-- IDLE
940  *
941  * If in the ESTBLISHED or CLOSING states, the QP will have have been
942  * moved by the provider to the ERR state. Disassociate the CM_ID from
943  * the QP,  move to IDLE, and remove the 'connected' reference.
944  *
945  * If in some other state, the cm_id was destroyed asynchronously.
946  * This is the last reference that will result in waking up
947  * the app thread blocked in iw_destroy_cm_id.
948  */
cm_close_handler(struct iwcm_id_private * cm_id_priv,struct iw_cm_event * iw_event)949 static int cm_close_handler(struct iwcm_id_private *cm_id_priv,
950 				  struct iw_cm_event *iw_event)
951 {
952 	struct ib_qp *qp;
953 	unsigned long flags;
954 	int ret = 0, notify_event = 0;
955 	spin_lock_irqsave(&cm_id_priv->lock, flags);
956 	qp = cm_id_priv->qp;
957 	cm_id_priv->qp = NULL;
958 
959 	switch (cm_id_priv->state) {
960 	case IW_CM_STATE_ESTABLISHED:
961 	case IW_CM_STATE_CLOSING:
962 		cm_id_priv->state = IW_CM_STATE_IDLE;
963 		notify_event = 1;
964 		break;
965 	case IW_CM_STATE_DESTROYING:
966 		break;
967 	default:
968 		BUG();
969 	}
970 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
971 
972 	if (qp)
973 		cm_id_priv->id.device->ops.iw_rem_ref(qp);
974 	if (notify_event)
975 		ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event);
976 	return ret;
977 }
978 
process_event(struct iwcm_id_private * cm_id_priv,struct iw_cm_event * iw_event)979 static int process_event(struct iwcm_id_private *cm_id_priv,
980 			 struct iw_cm_event *iw_event)
981 {
982 	int ret = 0;
983 
984 	switch (iw_event->event) {
985 	case IW_CM_EVENT_CONNECT_REQUEST:
986 		cm_conn_req_handler(cm_id_priv, iw_event);
987 		break;
988 	case IW_CM_EVENT_CONNECT_REPLY:
989 		ret = cm_conn_rep_handler(cm_id_priv, iw_event);
990 		break;
991 	case IW_CM_EVENT_ESTABLISHED:
992 		ret = cm_conn_est_handler(cm_id_priv, iw_event);
993 		break;
994 	case IW_CM_EVENT_DISCONNECT:
995 		cm_disconnect_handler(cm_id_priv, iw_event);
996 		break;
997 	case IW_CM_EVENT_CLOSE:
998 		ret = cm_close_handler(cm_id_priv, iw_event);
999 		break;
1000 	default:
1001 		BUG();
1002 	}
1003 
1004 	return ret;
1005 }
1006 
1007 /*
1008  * Process events on the work_list for the cm_id. If the callback
1009  * function requests that the cm_id be deleted, a flag is set in the
1010  * cm_id flags to indicate that when the last reference is
1011  * removed, the cm_id is to be destroyed. This is necessary to
1012  * distinguish between an object that will be destroyed by the app
1013  * thread asleep on the destroy_comp list vs. an object destroyed
1014  * here synchronously when the last reference is removed.
1015  */
cm_work_handler(struct work_struct * _work)1016 static void cm_work_handler(struct work_struct *_work)
1017 {
1018 	struct iwcm_work *work = container_of(_work, struct iwcm_work, work);
1019 	struct iw_cm_event levent;
1020 	struct iwcm_id_private *cm_id_priv = work->cm_id;
1021 	unsigned long flags;
1022 	int ret = 0;
1023 
1024 	spin_lock_irqsave(&cm_id_priv->lock, flags);
1025 	while (!list_empty(&cm_id_priv->work_list)) {
1026 		work = list_first_entry(&cm_id_priv->work_list,
1027 					struct iwcm_work, list);
1028 		list_del_init(&work->list);
1029 		levent = work->event;
1030 		put_work(work);
1031 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1032 
1033 		if (!test_bit(IWCM_F_DROP_EVENTS, &cm_id_priv->flags)) {
1034 			ret = process_event(cm_id_priv, &levent);
1035 			if (ret) {
1036 				destroy_cm_id(&cm_id_priv->id);
1037 				WARN_ON_ONCE(iwcm_deref_id(cm_id_priv));
1038 			}
1039 		} else
1040 			pr_debug("dropping event %d\n", levent.event);
1041 		if (iwcm_deref_id(cm_id_priv))
1042 			return;
1043 		spin_lock_irqsave(&cm_id_priv->lock, flags);
1044 	}
1045 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1046 }
1047 
1048 /*
1049  * This function is called on interrupt context. Schedule events on
1050  * the iwcm_wq thread to allow callback functions to downcall into
1051  * the CM and/or block.  Events are queued to a per-CM_ID
1052  * work_list. If this is the first event on the work_list, the work
1053  * element is also queued on the iwcm_wq thread.
1054  *
1055  * Each event holds a reference on the cm_id. Until the last posted
1056  * event has been delivered and processed, the cm_id cannot be
1057  * deleted.
1058  *
1059  * Returns:
1060  * 	      0	- the event was handled.
1061  *	-ENOMEM	- the event was not handled due to lack of resources.
1062  */
cm_event_handler(struct iw_cm_id * cm_id,struct iw_cm_event * iw_event)1063 static int cm_event_handler(struct iw_cm_id *cm_id,
1064 			     struct iw_cm_event *iw_event)
1065 {
1066 	struct iwcm_work *work;
1067 	struct iwcm_id_private *cm_id_priv;
1068 	unsigned long flags;
1069 	int ret = 0;
1070 
1071 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
1072 
1073 	spin_lock_irqsave(&cm_id_priv->lock, flags);
1074 	work = get_work(cm_id_priv);
1075 	if (!work) {
1076 		ret = -ENOMEM;
1077 		goto out;
1078 	}
1079 
1080 	INIT_WORK(&work->work, cm_work_handler);
1081 	work->cm_id = cm_id_priv;
1082 	work->event = *iw_event;
1083 
1084 	if ((work->event.event == IW_CM_EVENT_CONNECT_REQUEST ||
1085 	     work->event.event == IW_CM_EVENT_CONNECT_REPLY) &&
1086 	    work->event.private_data_len) {
1087 		ret = copy_private_data(&work->event);
1088 		if (ret) {
1089 			put_work(work);
1090 			goto out;
1091 		}
1092 	}
1093 
1094 	refcount_inc(&cm_id_priv->refcount);
1095 	list_add_tail(&work->list, &cm_id_priv->work_list);
1096 	queue_work(iwcm_wq, &work->work);
1097 out:
1098 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1099 	return ret;
1100 }
1101 
iwcm_init_qp_init_attr(struct iwcm_id_private * cm_id_priv,struct ib_qp_attr * qp_attr,int * qp_attr_mask)1102 static int iwcm_init_qp_init_attr(struct iwcm_id_private *cm_id_priv,
1103 				  struct ib_qp_attr *qp_attr,
1104 				  int *qp_attr_mask)
1105 {
1106 	unsigned long flags;
1107 	int ret;
1108 
1109 	spin_lock_irqsave(&cm_id_priv->lock, flags);
1110 	switch (cm_id_priv->state) {
1111 	case IW_CM_STATE_IDLE:
1112 	case IW_CM_STATE_CONN_SENT:
1113 	case IW_CM_STATE_CONN_RECV:
1114 	case IW_CM_STATE_ESTABLISHED:
1115 		*qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
1116 		qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE|
1117 					   IB_ACCESS_REMOTE_READ;
1118 		ret = 0;
1119 		break;
1120 	default:
1121 		ret = -EINVAL;
1122 		break;
1123 	}
1124 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1125 	return ret;
1126 }
1127 
iwcm_init_qp_rts_attr(struct iwcm_id_private * cm_id_priv,struct ib_qp_attr * qp_attr,int * qp_attr_mask)1128 static int iwcm_init_qp_rts_attr(struct iwcm_id_private *cm_id_priv,
1129 				  struct ib_qp_attr *qp_attr,
1130 				  int *qp_attr_mask)
1131 {
1132 	unsigned long flags;
1133 	int ret;
1134 
1135 	spin_lock_irqsave(&cm_id_priv->lock, flags);
1136 	switch (cm_id_priv->state) {
1137 	case IW_CM_STATE_IDLE:
1138 	case IW_CM_STATE_CONN_SENT:
1139 	case IW_CM_STATE_CONN_RECV:
1140 	case IW_CM_STATE_ESTABLISHED:
1141 		*qp_attr_mask = 0;
1142 		ret = 0;
1143 		break;
1144 	default:
1145 		ret = -EINVAL;
1146 		break;
1147 	}
1148 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1149 	return ret;
1150 }
1151 
iw_cm_init_qp_attr(struct iw_cm_id * cm_id,struct ib_qp_attr * qp_attr,int * qp_attr_mask)1152 int iw_cm_init_qp_attr(struct iw_cm_id *cm_id,
1153 		       struct ib_qp_attr *qp_attr,
1154 		       int *qp_attr_mask)
1155 {
1156 	struct iwcm_id_private *cm_id_priv;
1157 	int ret;
1158 
1159 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
1160 	switch (qp_attr->qp_state) {
1161 	case IB_QPS_INIT:
1162 	case IB_QPS_RTR:
1163 		ret = iwcm_init_qp_init_attr(cm_id_priv,
1164 					     qp_attr, qp_attr_mask);
1165 		break;
1166 	case IB_QPS_RTS:
1167 		ret = iwcm_init_qp_rts_attr(cm_id_priv,
1168 					    qp_attr, qp_attr_mask);
1169 		break;
1170 	default:
1171 		ret = -EINVAL;
1172 		break;
1173 	}
1174 	return ret;
1175 }
1176 EXPORT_SYMBOL(iw_cm_init_qp_attr);
1177 
iw_cm_init(void)1178 static int __init iw_cm_init(void)
1179 {
1180 	int ret;
1181 
1182 	ret = iwpm_init(RDMA_NL_IWCM);
1183 	if (ret)
1184 		return ret;
1185 
1186 	iwcm_wq = alloc_ordered_workqueue("iw_cm_wq", WQ_MEM_RECLAIM);
1187 	if (!iwcm_wq)
1188 		goto err_alloc;
1189 
1190 	iwcm_ctl_table_hdr = register_net_sysctl(&init_net, "net/iw_cm",
1191 						 iwcm_ctl_table);
1192 	if (!iwcm_ctl_table_hdr) {
1193 		pr_err("iw_cm: couldn't register sysctl paths\n");
1194 		goto err_sysctl;
1195 	}
1196 
1197 	rdma_nl_register(RDMA_NL_IWCM, iwcm_nl_cb_table);
1198 	return 0;
1199 
1200 err_sysctl:
1201 	destroy_workqueue(iwcm_wq);
1202 err_alloc:
1203 	iwpm_exit(RDMA_NL_IWCM);
1204 	return -ENOMEM;
1205 }
1206 
iw_cm_cleanup(void)1207 static void __exit iw_cm_cleanup(void)
1208 {
1209 	rdma_nl_unregister(RDMA_NL_IWCM);
1210 	unregister_net_sysctl_table(iwcm_ctl_table_hdr);
1211 	destroy_workqueue(iwcm_wq);
1212 	iwpm_exit(RDMA_NL_IWCM);
1213 }
1214 
1215 MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_IWCM, 2);
1216 
1217 module_init(iw_cm_init);
1218 module_exit(iw_cm_cleanup);
1219