• 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 };
92 
93 static struct workqueue_struct *iwcm_wq;
94 struct iwcm_work {
95 	struct work_struct work;
96 	struct iwcm_id_private *cm_id;
97 	struct list_head list;
98 	struct iw_cm_event event;
99 	struct list_head free_list;
100 };
101 
102 static unsigned int default_backlog = 256;
103 
104 static struct ctl_table_header *iwcm_ctl_table_hdr;
105 static struct ctl_table iwcm_ctl_table[] = {
106 	{
107 		.procname	= "default_backlog",
108 		.data		= &default_backlog,
109 		.maxlen		= sizeof(default_backlog),
110 		.mode		= 0644,
111 		.proc_handler	= proc_dointvec,
112 	},
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_entry(cm_id_priv->work_free_list.next, 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 1.
210  */
iwcm_deref_id(struct iwcm_id_private * cm_id_priv)211 static int iwcm_deref_id(struct iwcm_id_private *cm_id_priv)
212 {
213 	BUG_ON(atomic_read(&cm_id_priv->refcount)==0);
214 	if (atomic_dec_and_test(&cm_id_priv->refcount)) {
215 		BUG_ON(!list_empty(&cm_id_priv->work_list));
216 		free_cm_id(cm_id_priv);
217 		return 1;
218 	}
219 
220 	return 0;
221 }
222 
add_ref(struct iw_cm_id * cm_id)223 static void add_ref(struct iw_cm_id *cm_id)
224 {
225 	struct iwcm_id_private *cm_id_priv;
226 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
227 	atomic_inc(&cm_id_priv->refcount);
228 }
229 
rem_ref(struct iw_cm_id * cm_id)230 static void rem_ref(struct iw_cm_id *cm_id)
231 {
232 	struct iwcm_id_private *cm_id_priv;
233 
234 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
235 
236 	(void)iwcm_deref_id(cm_id_priv);
237 }
238 
239 static int cm_event_handler(struct iw_cm_id *cm_id, struct iw_cm_event *event);
240 
iw_create_cm_id(struct ib_device * device,iw_cm_handler cm_handler,void * context)241 struct iw_cm_id *iw_create_cm_id(struct ib_device *device,
242 				 iw_cm_handler cm_handler,
243 				 void *context)
244 {
245 	struct iwcm_id_private *cm_id_priv;
246 
247 	cm_id_priv = kzalloc(sizeof(*cm_id_priv), GFP_KERNEL);
248 	if (!cm_id_priv)
249 		return ERR_PTR(-ENOMEM);
250 
251 	cm_id_priv->state = IW_CM_STATE_IDLE;
252 	cm_id_priv->id.device = device;
253 	cm_id_priv->id.cm_handler = cm_handler;
254 	cm_id_priv->id.context = context;
255 	cm_id_priv->id.event_handler = cm_event_handler;
256 	cm_id_priv->id.add_ref = add_ref;
257 	cm_id_priv->id.rem_ref = rem_ref;
258 	spin_lock_init(&cm_id_priv->lock);
259 	atomic_set(&cm_id_priv->refcount, 1);
260 	init_waitqueue_head(&cm_id_priv->connect_wait);
261 	init_completion(&cm_id_priv->destroy_comp);
262 	INIT_LIST_HEAD(&cm_id_priv->work_list);
263 	INIT_LIST_HEAD(&cm_id_priv->work_free_list);
264 
265 	return &cm_id_priv->id;
266 }
267 EXPORT_SYMBOL(iw_create_cm_id);
268 
269 
iwcm_modify_qp_err(struct ib_qp * qp)270 static int iwcm_modify_qp_err(struct ib_qp *qp)
271 {
272 	struct ib_qp_attr qp_attr;
273 
274 	if (!qp)
275 		return -EINVAL;
276 
277 	qp_attr.qp_state = IB_QPS_ERR;
278 	return ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
279 }
280 
281 /*
282  * This is really the RDMAC CLOSING state. It is most similar to the
283  * IB SQD QP state.
284  */
iwcm_modify_qp_sqd(struct ib_qp * qp)285 static int iwcm_modify_qp_sqd(struct ib_qp *qp)
286 {
287 	struct ib_qp_attr qp_attr;
288 
289 	BUG_ON(qp == NULL);
290 	qp_attr.qp_state = IB_QPS_SQD;
291 	return ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
292 }
293 
294 /*
295  * CM_ID <-- CLOSING
296  *
297  * Block if a passive or active connection is currently being processed. Then
298  * process the event as follows:
299  * - If we are ESTABLISHED, move to CLOSING and modify the QP state
300  *   based on the abrupt flag
301  * - If the connection is already in the CLOSING or IDLE state, the peer is
302  *   disconnecting concurrently with us and we've already seen the
303  *   DISCONNECT event -- ignore the request and return 0
304  * - Disconnect on a listening endpoint returns -EINVAL
305  */
iw_cm_disconnect(struct iw_cm_id * cm_id,int abrupt)306 int iw_cm_disconnect(struct iw_cm_id *cm_id, int abrupt)
307 {
308 	struct iwcm_id_private *cm_id_priv;
309 	unsigned long flags;
310 	int ret = 0;
311 	struct ib_qp *qp = NULL;
312 
313 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
314 	/* Wait if we're currently in a connect or accept downcall */
315 	wait_event(cm_id_priv->connect_wait,
316 		   !test_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags));
317 
318 	spin_lock_irqsave(&cm_id_priv->lock, flags);
319 	switch (cm_id_priv->state) {
320 	case IW_CM_STATE_ESTABLISHED:
321 		cm_id_priv->state = IW_CM_STATE_CLOSING;
322 
323 		/* QP could be <nul> for user-mode client */
324 		if (cm_id_priv->qp)
325 			qp = cm_id_priv->qp;
326 		else
327 			ret = -EINVAL;
328 		break;
329 	case IW_CM_STATE_LISTEN:
330 		ret = -EINVAL;
331 		break;
332 	case IW_CM_STATE_CLOSING:
333 		/* remote peer closed first */
334 	case IW_CM_STATE_IDLE:
335 		/* accept or connect returned !0 */
336 		break;
337 	case IW_CM_STATE_CONN_RECV:
338 		/*
339 		 * App called disconnect before/without calling accept after
340 		 * connect_request event delivered.
341 		 */
342 		break;
343 	case IW_CM_STATE_CONN_SENT:
344 		/* Can only get here if wait above fails */
345 	default:
346 		BUG();
347 	}
348 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
349 
350 	if (qp) {
351 		if (abrupt)
352 			ret = iwcm_modify_qp_err(qp);
353 		else
354 			ret = iwcm_modify_qp_sqd(qp);
355 
356 		/*
357 		 * If both sides are disconnecting the QP could
358 		 * already be in ERR or SQD states
359 		 */
360 		ret = 0;
361 	}
362 
363 	return ret;
364 }
365 EXPORT_SYMBOL(iw_cm_disconnect);
366 
367 /*
368  * CM_ID <-- DESTROYING
369  *
370  * Clean up all resources associated with the connection and release
371  * the initial reference taken by iw_create_cm_id.
372  */
destroy_cm_id(struct iw_cm_id * cm_id)373 static void destroy_cm_id(struct iw_cm_id *cm_id)
374 {
375 	struct iwcm_id_private *cm_id_priv;
376 	unsigned long flags;
377 
378 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
379 	/*
380 	 * Wait if we're currently in a connect or accept downcall. A
381 	 * listening endpoint should never block here.
382 	 */
383 	wait_event(cm_id_priv->connect_wait,
384 		   !test_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags));
385 
386 	/*
387 	 * Since we're deleting the cm_id, drop any events that
388 	 * might arrive before the last dereference.
389 	 */
390 	set_bit(IWCM_F_DROP_EVENTS, &cm_id_priv->flags);
391 
392 	spin_lock_irqsave(&cm_id_priv->lock, flags);
393 	switch (cm_id_priv->state) {
394 	case IW_CM_STATE_LISTEN:
395 		cm_id_priv->state = IW_CM_STATE_DESTROYING;
396 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
397 		/* destroy the listening endpoint */
398 		cm_id->device->iwcm->destroy_listen(cm_id);
399 		spin_lock_irqsave(&cm_id_priv->lock, flags);
400 		break;
401 	case IW_CM_STATE_ESTABLISHED:
402 		cm_id_priv->state = IW_CM_STATE_DESTROYING;
403 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
404 		/* Abrupt close of the connection */
405 		(void)iwcm_modify_qp_err(cm_id_priv->qp);
406 		spin_lock_irqsave(&cm_id_priv->lock, flags);
407 		break;
408 	case IW_CM_STATE_IDLE:
409 	case IW_CM_STATE_CLOSING:
410 		cm_id_priv->state = IW_CM_STATE_DESTROYING;
411 		break;
412 	case IW_CM_STATE_CONN_RECV:
413 		/*
414 		 * App called destroy before/without calling accept after
415 		 * receiving connection request event notification or
416 		 * returned non zero from the event callback function.
417 		 * In either case, must tell the provider to reject.
418 		 */
419 		cm_id_priv->state = IW_CM_STATE_DESTROYING;
420 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
421 		cm_id->device->iwcm->reject(cm_id, NULL, 0);
422 		spin_lock_irqsave(&cm_id_priv->lock, flags);
423 		break;
424 	case IW_CM_STATE_CONN_SENT:
425 	case IW_CM_STATE_DESTROYING:
426 	default:
427 		BUG();
428 		break;
429 	}
430 	if (cm_id_priv->qp) {
431 		cm_id_priv->id.device->iwcm->rem_ref(cm_id_priv->qp);
432 		cm_id_priv->qp = NULL;
433 	}
434 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
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 	(void)iwcm_deref_id(cm_id_priv);
442 }
443 
444 /*
445  * This function is only called by the application thread and cannot
446  * be called by the event thread. The function will wait for all
447  * references to be released on the cm_id and then kfree the cm_id
448  * object.
449  */
iw_destroy_cm_id(struct iw_cm_id * cm_id)450 void iw_destroy_cm_id(struct iw_cm_id *cm_id)
451 {
452 	struct iwcm_id_private *cm_id_priv;
453 
454 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
455 	destroy_cm_id(cm_id);
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 	struct iwpm_dev_data pm_reg_msg;
511 	struct iwpm_sa_data pm_msg;
512 	int status;
513 
514 	cm_id->m_local_addr = cm_id->local_addr;
515 	cm_id->m_remote_addr = cm_id->remote_addr;
516 
517 	memcpy(pm_reg_msg.dev_name, cm_id->device->name,
518 	       sizeof(pm_reg_msg.dev_name));
519 	memcpy(pm_reg_msg.if_name, cm_id->device->iwcm->ifname,
520 	       sizeof(pm_reg_msg.if_name));
521 
522 	if (iwpm_register_pid(&pm_reg_msg, RDMA_NL_IWCM) ||
523 	    !iwpm_valid_pid())
524 		return 0;
525 
526 	cm_id->mapped = true;
527 	pm_msg.loc_addr = cm_id->local_addr;
528 	pm_msg.rem_addr = cm_id->remote_addr;
529 	if (active)
530 		status = iwpm_add_and_query_mapping(&pm_msg,
531 						    RDMA_NL_IWCM);
532 	else
533 		status = iwpm_add_mapping(&pm_msg, RDMA_NL_IWCM);
534 
535 	if (!status) {
536 		cm_id->m_local_addr = pm_msg.mapped_loc_addr;
537 		if (active) {
538 			cm_id->m_remote_addr = pm_msg.mapped_rem_addr;
539 			iw_cm_check_wildcard(&pm_msg.mapped_rem_addr,
540 					     &cm_id->remote_addr,
541 					     &cm_id->m_remote_addr);
542 		}
543 	}
544 
545 	return iwpm_create_mapinfo(&cm_id->local_addr,
546 				   &cm_id->m_local_addr,
547 				   RDMA_NL_IWCM);
548 }
549 
550 /*
551  * CM_ID <-- LISTEN
552  *
553  * Start listening for connect requests. Generates one CONNECT_REQUEST
554  * event for each inbound connect request.
555  */
iw_cm_listen(struct iw_cm_id * cm_id,int backlog)556 int iw_cm_listen(struct iw_cm_id *cm_id, int backlog)
557 {
558 	struct iwcm_id_private *cm_id_priv;
559 	unsigned long flags;
560 	int ret;
561 
562 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
563 
564 	if (!backlog)
565 		backlog = default_backlog;
566 
567 	ret = alloc_work_entries(cm_id_priv, backlog);
568 	if (ret)
569 		return ret;
570 
571 	spin_lock_irqsave(&cm_id_priv->lock, flags);
572 	switch (cm_id_priv->state) {
573 	case IW_CM_STATE_IDLE:
574 		cm_id_priv->state = IW_CM_STATE_LISTEN;
575 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
576 		ret = iw_cm_map(cm_id, false);
577 		if (!ret)
578 			ret = cm_id->device->iwcm->create_listen(cm_id, backlog);
579 		if (ret)
580 			cm_id_priv->state = IW_CM_STATE_IDLE;
581 		spin_lock_irqsave(&cm_id_priv->lock, flags);
582 		break;
583 	default:
584 		ret = -EINVAL;
585 	}
586 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
587 
588 	return ret;
589 }
590 EXPORT_SYMBOL(iw_cm_listen);
591 
592 /*
593  * CM_ID <-- IDLE
594  *
595  * Rejects an inbound connection request. No events are generated.
596  */
iw_cm_reject(struct iw_cm_id * cm_id,const void * private_data,u8 private_data_len)597 int iw_cm_reject(struct iw_cm_id *cm_id,
598 		 const void *private_data,
599 		 u8 private_data_len)
600 {
601 	struct iwcm_id_private *cm_id_priv;
602 	unsigned long flags;
603 	int ret;
604 
605 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
606 	set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
607 
608 	spin_lock_irqsave(&cm_id_priv->lock, flags);
609 	if (cm_id_priv->state != IW_CM_STATE_CONN_RECV) {
610 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
611 		clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
612 		wake_up_all(&cm_id_priv->connect_wait);
613 		return -EINVAL;
614 	}
615 	cm_id_priv->state = IW_CM_STATE_IDLE;
616 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
617 
618 	ret = cm_id->device->iwcm->reject(cm_id, private_data,
619 					  private_data_len);
620 
621 	clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
622 	wake_up_all(&cm_id_priv->connect_wait);
623 
624 	return ret;
625 }
626 EXPORT_SYMBOL(iw_cm_reject);
627 
628 /*
629  * CM_ID <-- ESTABLISHED
630  *
631  * Accepts an inbound connection request and generates an ESTABLISHED
632  * event. Callers of iw_cm_disconnect and iw_destroy_cm_id will block
633  * until the ESTABLISHED event is received from the provider.
634  */
iw_cm_accept(struct iw_cm_id * cm_id,struct iw_cm_conn_param * iw_param)635 int iw_cm_accept(struct iw_cm_id *cm_id,
636 		 struct iw_cm_conn_param *iw_param)
637 {
638 	struct iwcm_id_private *cm_id_priv;
639 	struct ib_qp *qp;
640 	unsigned long flags;
641 	int ret;
642 
643 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
644 	set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
645 
646 	spin_lock_irqsave(&cm_id_priv->lock, flags);
647 	if (cm_id_priv->state != IW_CM_STATE_CONN_RECV) {
648 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
649 		clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
650 		wake_up_all(&cm_id_priv->connect_wait);
651 		return -EINVAL;
652 	}
653 	/* Get the ib_qp given the QPN */
654 	qp = cm_id->device->iwcm->get_qp(cm_id->device, iw_param->qpn);
655 	if (!qp) {
656 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
657 		clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
658 		wake_up_all(&cm_id_priv->connect_wait);
659 		return -EINVAL;
660 	}
661 	cm_id->device->iwcm->add_ref(qp);
662 	cm_id_priv->qp = qp;
663 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
664 
665 	ret = cm_id->device->iwcm->accept(cm_id, iw_param);
666 	if (ret) {
667 		/* An error on accept precludes provider events */
668 		BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_RECV);
669 		cm_id_priv->state = IW_CM_STATE_IDLE;
670 		spin_lock_irqsave(&cm_id_priv->lock, flags);
671 		if (cm_id_priv->qp) {
672 			cm_id->device->iwcm->rem_ref(qp);
673 			cm_id_priv->qp = NULL;
674 		}
675 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
676 		clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
677 		wake_up_all(&cm_id_priv->connect_wait);
678 	}
679 
680 	return ret;
681 }
682 EXPORT_SYMBOL(iw_cm_accept);
683 
684 /*
685  * Active Side: CM_ID <-- CONN_SENT
686  *
687  * If successful, results in the generation of a CONNECT_REPLY
688  * event. iw_cm_disconnect and iw_cm_destroy will block until the
689  * CONNECT_REPLY event is received from the provider.
690  */
iw_cm_connect(struct iw_cm_id * cm_id,struct iw_cm_conn_param * iw_param)691 int iw_cm_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *iw_param)
692 {
693 	struct iwcm_id_private *cm_id_priv;
694 	int ret;
695 	unsigned long flags;
696 	struct ib_qp *qp;
697 
698 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
699 
700 	ret = alloc_work_entries(cm_id_priv, 4);
701 	if (ret)
702 		return ret;
703 
704 	set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
705 	spin_lock_irqsave(&cm_id_priv->lock, flags);
706 
707 	if (cm_id_priv->state != IW_CM_STATE_IDLE) {
708 		ret = -EINVAL;
709 		goto err;
710 	}
711 
712 	/* Get the ib_qp given the QPN */
713 	qp = cm_id->device->iwcm->get_qp(cm_id->device, iw_param->qpn);
714 	if (!qp) {
715 		ret = -EINVAL;
716 		goto err;
717 	}
718 	cm_id->device->iwcm->add_ref(qp);
719 	cm_id_priv->qp = qp;
720 	cm_id_priv->state = IW_CM_STATE_CONN_SENT;
721 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
722 
723 	ret = iw_cm_map(cm_id, true);
724 	if (!ret)
725 		ret = cm_id->device->iwcm->connect(cm_id, iw_param);
726 	if (!ret)
727 		return 0;	/* success */
728 
729 	spin_lock_irqsave(&cm_id_priv->lock, flags);
730 	if (cm_id_priv->qp) {
731 		cm_id->device->iwcm->rem_ref(qp);
732 		cm_id_priv->qp = NULL;
733 	}
734 	cm_id_priv->state = IW_CM_STATE_IDLE;
735 err:
736 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
737 	clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
738 	wake_up_all(&cm_id_priv->connect_wait);
739 	return ret;
740 }
741 EXPORT_SYMBOL(iw_cm_connect);
742 
743 /*
744  * Passive Side: new CM_ID <-- CONN_RECV
745  *
746  * Handles an inbound connect request. The function creates a new
747  * iw_cm_id to represent the new connection and inherits the client
748  * callback function and other attributes from the listening parent.
749  *
750  * The work item contains a pointer to the listen_cm_id and the event. The
751  * listen_cm_id contains the client cm_handler, context and
752  * device. These are copied when the device is cloned. The event
753  * contains the new four tuple.
754  *
755  * An error on the child should not affect the parent, so this
756  * function does not return a value.
757  */
cm_conn_req_handler(struct iwcm_id_private * listen_id_priv,struct iw_cm_event * iw_event)758 static void cm_conn_req_handler(struct iwcm_id_private *listen_id_priv,
759 				struct iw_cm_event *iw_event)
760 {
761 	unsigned long flags;
762 	struct iw_cm_id *cm_id;
763 	struct iwcm_id_private *cm_id_priv;
764 	int ret;
765 
766 	/*
767 	 * The provider should never generate a connection request
768 	 * event with a bad status.
769 	 */
770 	BUG_ON(iw_event->status);
771 
772 	cm_id = iw_create_cm_id(listen_id_priv->id.device,
773 				listen_id_priv->id.cm_handler,
774 				listen_id_priv->id.context);
775 	/* If the cm_id could not be created, ignore the request */
776 	if (IS_ERR(cm_id))
777 		goto out;
778 
779 	cm_id->provider_data = iw_event->provider_data;
780 	cm_id->m_local_addr = iw_event->local_addr;
781 	cm_id->m_remote_addr = iw_event->remote_addr;
782 	cm_id->local_addr = listen_id_priv->id.local_addr;
783 
784 	ret = iwpm_get_remote_info(&listen_id_priv->id.m_local_addr,
785 				   &iw_event->remote_addr,
786 				   &cm_id->remote_addr,
787 				   RDMA_NL_IWCM);
788 	if (ret) {
789 		cm_id->remote_addr = iw_event->remote_addr;
790 	} else {
791 		iw_cm_check_wildcard(&listen_id_priv->id.m_local_addr,
792 				     &iw_event->local_addr,
793 				     &cm_id->local_addr);
794 		iw_event->local_addr = cm_id->local_addr;
795 		iw_event->remote_addr = cm_id->remote_addr;
796 	}
797 
798 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
799 	cm_id_priv->state = IW_CM_STATE_CONN_RECV;
800 
801 	/*
802 	 * We could be destroying the listening id. If so, ignore this
803 	 * upcall.
804 	 */
805 	spin_lock_irqsave(&listen_id_priv->lock, flags);
806 	if (listen_id_priv->state != IW_CM_STATE_LISTEN) {
807 		spin_unlock_irqrestore(&listen_id_priv->lock, flags);
808 		iw_cm_reject(cm_id, NULL, 0);
809 		iw_destroy_cm_id(cm_id);
810 		goto out;
811 	}
812 	spin_unlock_irqrestore(&listen_id_priv->lock, flags);
813 
814 	ret = alloc_work_entries(cm_id_priv, 3);
815 	if (ret) {
816 		iw_cm_reject(cm_id, NULL, 0);
817 		iw_destroy_cm_id(cm_id);
818 		goto out;
819 	}
820 
821 	/* Call the client CM handler */
822 	ret = cm_id->cm_handler(cm_id, iw_event);
823 	if (ret) {
824 		iw_cm_reject(cm_id, NULL, 0);
825 		iw_destroy_cm_id(cm_id);
826 	}
827 
828 out:
829 	if (iw_event->private_data_len)
830 		kfree(iw_event->private_data);
831 }
832 
833 /*
834  * Passive Side: CM_ID <-- ESTABLISHED
835  *
836  * The provider generated an ESTABLISHED event which means that
837  * the MPA negotion has completed successfully and we are now in MPA
838  * FPDU mode.
839  *
840  * This event can only be received in the CONN_RECV state. If the
841  * remote peer closed, the ESTABLISHED event would be received followed
842  * by the CLOSE event. If the app closes, it will block until we wake
843  * it up after processing this event.
844  */
cm_conn_est_handler(struct iwcm_id_private * cm_id_priv,struct iw_cm_event * iw_event)845 static int cm_conn_est_handler(struct iwcm_id_private *cm_id_priv,
846 			       struct iw_cm_event *iw_event)
847 {
848 	unsigned long flags;
849 	int ret;
850 
851 	spin_lock_irqsave(&cm_id_priv->lock, flags);
852 
853 	/*
854 	 * We clear the CONNECT_WAIT bit here to allow the callback
855 	 * function to call iw_cm_disconnect. Calling iw_destroy_cm_id
856 	 * from a callback handler is not allowed.
857 	 */
858 	clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
859 	BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_RECV);
860 	cm_id_priv->state = IW_CM_STATE_ESTABLISHED;
861 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
862 	ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event);
863 	wake_up_all(&cm_id_priv->connect_wait);
864 
865 	return ret;
866 }
867 
868 /*
869  * Active Side: CM_ID <-- ESTABLISHED
870  *
871  * The app has called connect and is waiting for the established event to
872  * post it's requests to the server. This event will wake up anyone
873  * blocked in iw_cm_disconnect or iw_destroy_id.
874  */
cm_conn_rep_handler(struct iwcm_id_private * cm_id_priv,struct iw_cm_event * iw_event)875 static int cm_conn_rep_handler(struct iwcm_id_private *cm_id_priv,
876 			       struct iw_cm_event *iw_event)
877 {
878 	unsigned long flags;
879 	int ret;
880 
881 	spin_lock_irqsave(&cm_id_priv->lock, flags);
882 	/*
883 	 * Clear the connect wait bit so a callback function calling
884 	 * iw_cm_disconnect will not wait and deadlock this thread
885 	 */
886 	clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
887 	BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_SENT);
888 	if (iw_event->status == 0) {
889 		cm_id_priv->id.m_local_addr = iw_event->local_addr;
890 		cm_id_priv->id.m_remote_addr = iw_event->remote_addr;
891 		iw_event->local_addr = cm_id_priv->id.local_addr;
892 		iw_event->remote_addr = cm_id_priv->id.remote_addr;
893 		cm_id_priv->state = IW_CM_STATE_ESTABLISHED;
894 	} else {
895 		/* REJECTED or RESET */
896 		cm_id_priv->id.device->iwcm->rem_ref(cm_id_priv->qp);
897 		cm_id_priv->qp = NULL;
898 		cm_id_priv->state = IW_CM_STATE_IDLE;
899 	}
900 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
901 	ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event);
902 
903 	if (iw_event->private_data_len)
904 		kfree(iw_event->private_data);
905 
906 	/* Wake up waiters on connect complete */
907 	wake_up_all(&cm_id_priv->connect_wait);
908 
909 	return ret;
910 }
911 
912 /*
913  * CM_ID <-- CLOSING
914  *
915  * If in the ESTABLISHED state, move to CLOSING.
916  */
cm_disconnect_handler(struct iwcm_id_private * cm_id_priv,struct iw_cm_event * iw_event)917 static void cm_disconnect_handler(struct iwcm_id_private *cm_id_priv,
918 				  struct iw_cm_event *iw_event)
919 {
920 	unsigned long flags;
921 
922 	spin_lock_irqsave(&cm_id_priv->lock, flags);
923 	if (cm_id_priv->state == IW_CM_STATE_ESTABLISHED)
924 		cm_id_priv->state = IW_CM_STATE_CLOSING;
925 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
926 }
927 
928 /*
929  * CM_ID <-- IDLE
930  *
931  * If in the ESTBLISHED or CLOSING states, the QP will have have been
932  * moved by the provider to the ERR state. Disassociate the CM_ID from
933  * the QP,  move to IDLE, and remove the 'connected' reference.
934  *
935  * If in some other state, the cm_id was destroyed asynchronously.
936  * This is the last reference that will result in waking up
937  * the app thread blocked in iw_destroy_cm_id.
938  */
cm_close_handler(struct iwcm_id_private * cm_id_priv,struct iw_cm_event * iw_event)939 static int cm_close_handler(struct iwcm_id_private *cm_id_priv,
940 				  struct iw_cm_event *iw_event)
941 {
942 	unsigned long flags;
943 	int ret = 0;
944 	spin_lock_irqsave(&cm_id_priv->lock, flags);
945 
946 	if (cm_id_priv->qp) {
947 		cm_id_priv->id.device->iwcm->rem_ref(cm_id_priv->qp);
948 		cm_id_priv->qp = NULL;
949 	}
950 	switch (cm_id_priv->state) {
951 	case IW_CM_STATE_ESTABLISHED:
952 	case IW_CM_STATE_CLOSING:
953 		cm_id_priv->state = IW_CM_STATE_IDLE;
954 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
955 		ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event);
956 		spin_lock_irqsave(&cm_id_priv->lock, flags);
957 		break;
958 	case IW_CM_STATE_DESTROYING:
959 		break;
960 	default:
961 		BUG();
962 	}
963 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
964 
965 	return ret;
966 }
967 
process_event(struct iwcm_id_private * cm_id_priv,struct iw_cm_event * iw_event)968 static int process_event(struct iwcm_id_private *cm_id_priv,
969 			 struct iw_cm_event *iw_event)
970 {
971 	int ret = 0;
972 
973 	switch (iw_event->event) {
974 	case IW_CM_EVENT_CONNECT_REQUEST:
975 		cm_conn_req_handler(cm_id_priv, iw_event);
976 		break;
977 	case IW_CM_EVENT_CONNECT_REPLY:
978 		ret = cm_conn_rep_handler(cm_id_priv, iw_event);
979 		break;
980 	case IW_CM_EVENT_ESTABLISHED:
981 		ret = cm_conn_est_handler(cm_id_priv, iw_event);
982 		break;
983 	case IW_CM_EVENT_DISCONNECT:
984 		cm_disconnect_handler(cm_id_priv, iw_event);
985 		break;
986 	case IW_CM_EVENT_CLOSE:
987 		ret = cm_close_handler(cm_id_priv, iw_event);
988 		break;
989 	default:
990 		BUG();
991 	}
992 
993 	return ret;
994 }
995 
996 /*
997  * Process events on the work_list for the cm_id. If the callback
998  * function requests that the cm_id be deleted, a flag is set in the
999  * cm_id flags to indicate that when the last reference is
1000  * removed, the cm_id is to be destroyed. This is necessary to
1001  * distinguish between an object that will be destroyed by the app
1002  * thread asleep on the destroy_comp list vs. an object destroyed
1003  * here synchronously when the last reference is removed.
1004  */
cm_work_handler(struct work_struct * _work)1005 static void cm_work_handler(struct work_struct *_work)
1006 {
1007 	struct iwcm_work *work = container_of(_work, struct iwcm_work, work);
1008 	struct iw_cm_event levent;
1009 	struct iwcm_id_private *cm_id_priv = work->cm_id;
1010 	unsigned long flags;
1011 	int empty;
1012 	int ret = 0;
1013 
1014 	spin_lock_irqsave(&cm_id_priv->lock, flags);
1015 	empty = list_empty(&cm_id_priv->work_list);
1016 	while (!empty) {
1017 		work = list_entry(cm_id_priv->work_list.next,
1018 				  struct iwcm_work, list);
1019 		list_del_init(&work->list);
1020 		empty = list_empty(&cm_id_priv->work_list);
1021 		levent = work->event;
1022 		put_work(work);
1023 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1024 
1025 		if (!test_bit(IWCM_F_DROP_EVENTS, &cm_id_priv->flags)) {
1026 			ret = process_event(cm_id_priv, &levent);
1027 			if (ret)
1028 				destroy_cm_id(&cm_id_priv->id);
1029 		} else
1030 			pr_debug("dropping event %d\n", levent.event);
1031 		if (iwcm_deref_id(cm_id_priv))
1032 			return;
1033 		if (empty)
1034 			return;
1035 		spin_lock_irqsave(&cm_id_priv->lock, flags);
1036 	}
1037 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1038 }
1039 
1040 /*
1041  * This function is called on interrupt context. Schedule events on
1042  * the iwcm_wq thread to allow callback functions to downcall into
1043  * the CM and/or block.  Events are queued to a per-CM_ID
1044  * work_list. If this is the first event on the work_list, the work
1045  * element is also queued on the iwcm_wq thread.
1046  *
1047  * Each event holds a reference on the cm_id. Until the last posted
1048  * event has been delivered and processed, the cm_id cannot be
1049  * deleted.
1050  *
1051  * Returns:
1052  * 	      0	- the event was handled.
1053  *	-ENOMEM	- the event was not handled due to lack of resources.
1054  */
cm_event_handler(struct iw_cm_id * cm_id,struct iw_cm_event * iw_event)1055 static int cm_event_handler(struct iw_cm_id *cm_id,
1056 			     struct iw_cm_event *iw_event)
1057 {
1058 	struct iwcm_work *work;
1059 	struct iwcm_id_private *cm_id_priv;
1060 	unsigned long flags;
1061 	int ret = 0;
1062 
1063 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
1064 
1065 	spin_lock_irqsave(&cm_id_priv->lock, flags);
1066 	work = get_work(cm_id_priv);
1067 	if (!work) {
1068 		ret = -ENOMEM;
1069 		goto out;
1070 	}
1071 
1072 	INIT_WORK(&work->work, cm_work_handler);
1073 	work->cm_id = cm_id_priv;
1074 	work->event = *iw_event;
1075 
1076 	if ((work->event.event == IW_CM_EVENT_CONNECT_REQUEST ||
1077 	     work->event.event == IW_CM_EVENT_CONNECT_REPLY) &&
1078 	    work->event.private_data_len) {
1079 		ret = copy_private_data(&work->event);
1080 		if (ret) {
1081 			put_work(work);
1082 			goto out;
1083 		}
1084 	}
1085 
1086 	atomic_inc(&cm_id_priv->refcount);
1087 	if (list_empty(&cm_id_priv->work_list)) {
1088 		list_add_tail(&work->list, &cm_id_priv->work_list);
1089 		queue_work(iwcm_wq, &work->work);
1090 	} else
1091 		list_add_tail(&work->list, &cm_id_priv->work_list);
1092 out:
1093 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1094 	return ret;
1095 }
1096 
iwcm_init_qp_init_attr(struct iwcm_id_private * cm_id_priv,struct ib_qp_attr * qp_attr,int * qp_attr_mask)1097 static int iwcm_init_qp_init_attr(struct iwcm_id_private *cm_id_priv,
1098 				  struct ib_qp_attr *qp_attr,
1099 				  int *qp_attr_mask)
1100 {
1101 	unsigned long flags;
1102 	int ret;
1103 
1104 	spin_lock_irqsave(&cm_id_priv->lock, flags);
1105 	switch (cm_id_priv->state) {
1106 	case IW_CM_STATE_IDLE:
1107 	case IW_CM_STATE_CONN_SENT:
1108 	case IW_CM_STATE_CONN_RECV:
1109 	case IW_CM_STATE_ESTABLISHED:
1110 		*qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
1111 		qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE|
1112 					   IB_ACCESS_REMOTE_READ;
1113 		ret = 0;
1114 		break;
1115 	default:
1116 		ret = -EINVAL;
1117 		break;
1118 	}
1119 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1120 	return ret;
1121 }
1122 
iwcm_init_qp_rts_attr(struct iwcm_id_private * cm_id_priv,struct ib_qp_attr * qp_attr,int * qp_attr_mask)1123 static int iwcm_init_qp_rts_attr(struct iwcm_id_private *cm_id_priv,
1124 				  struct ib_qp_attr *qp_attr,
1125 				  int *qp_attr_mask)
1126 {
1127 	unsigned long flags;
1128 	int ret;
1129 
1130 	spin_lock_irqsave(&cm_id_priv->lock, flags);
1131 	switch (cm_id_priv->state) {
1132 	case IW_CM_STATE_IDLE:
1133 	case IW_CM_STATE_CONN_SENT:
1134 	case IW_CM_STATE_CONN_RECV:
1135 	case IW_CM_STATE_ESTABLISHED:
1136 		*qp_attr_mask = 0;
1137 		ret = 0;
1138 		break;
1139 	default:
1140 		ret = -EINVAL;
1141 		break;
1142 	}
1143 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1144 	return ret;
1145 }
1146 
iw_cm_init_qp_attr(struct iw_cm_id * cm_id,struct ib_qp_attr * qp_attr,int * qp_attr_mask)1147 int iw_cm_init_qp_attr(struct iw_cm_id *cm_id,
1148 		       struct ib_qp_attr *qp_attr,
1149 		       int *qp_attr_mask)
1150 {
1151 	struct iwcm_id_private *cm_id_priv;
1152 	int ret;
1153 
1154 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
1155 	switch (qp_attr->qp_state) {
1156 	case IB_QPS_INIT:
1157 	case IB_QPS_RTR:
1158 		ret = iwcm_init_qp_init_attr(cm_id_priv,
1159 					     qp_attr, qp_attr_mask);
1160 		break;
1161 	case IB_QPS_RTS:
1162 		ret = iwcm_init_qp_rts_attr(cm_id_priv,
1163 					    qp_attr, qp_attr_mask);
1164 		break;
1165 	default:
1166 		ret = -EINVAL;
1167 		break;
1168 	}
1169 	return ret;
1170 }
1171 EXPORT_SYMBOL(iw_cm_init_qp_attr);
1172 
iw_cm_init(void)1173 static int __init iw_cm_init(void)
1174 {
1175 	int ret;
1176 
1177 	ret = iwpm_init(RDMA_NL_IWCM);
1178 	if (ret)
1179 		pr_err("iw_cm: couldn't init iwpm\n");
1180 	else
1181 		rdma_nl_register(RDMA_NL_IWCM, iwcm_nl_cb_table);
1182 	iwcm_wq = alloc_ordered_workqueue("iw_cm_wq", 0);
1183 	if (!iwcm_wq)
1184 		return -ENOMEM;
1185 
1186 	iwcm_ctl_table_hdr = register_net_sysctl(&init_net, "net/iw_cm",
1187 						 iwcm_ctl_table);
1188 	if (!iwcm_ctl_table_hdr) {
1189 		pr_err("iw_cm: couldn't register sysctl paths\n");
1190 		destroy_workqueue(iwcm_wq);
1191 		return -ENOMEM;
1192 	}
1193 
1194 	return 0;
1195 }
1196 
iw_cm_cleanup(void)1197 static void __exit iw_cm_cleanup(void)
1198 {
1199 	unregister_net_sysctl_table(iwcm_ctl_table_hdr);
1200 	destroy_workqueue(iwcm_wq);
1201 	rdma_nl_unregister(RDMA_NL_IWCM);
1202 	iwpm_exit(RDMA_NL_IWCM);
1203 }
1204 
1205 MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_IWCM, 2);
1206 
1207 module_init(iw_cm_init);
1208 module_exit(iw_cm_cleanup);
1209