1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3 *
4 * Copyright (c) 2004, 2005 Intel Corporation. All rights reserved.
5 * Copyright (c) 2004 Topspin Corporation. All rights reserved.
6 * Copyright (c) 2004, 2005 Voltaire Corporation. All rights reserved.
7 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
8 * Copyright (c) 2005 Open Grid Computing, Inc. All rights reserved.
9 * Copyright (c) 2005 Network Appliance, Inc. All rights reserved.
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
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42
43 #include <linux/dma-mapping.h>
44 #include <linux/err.h>
45 #include <linux/idr.h>
46 #include <linux/interrupt.h>
47 #include <linux/rbtree.h>
48 #include <linux/sched.h>
49 #include <linux/spinlock.h>
50 #include <linux/workqueue.h>
51 #include <linux/completion.h>
52 #include <linux/slab.h>
53 #include <linux/module.h>
54 #include <linux/wait.h>
55
56 #include <rdma/iw_cm.h>
57 #include <rdma/ib_addr.h>
58 #include <rdma/iw_portmap.h>
59
60 #include "iwcm.h"
61
62 MODULE_AUTHOR("Tom Tucker");
63 MODULE_DESCRIPTION("iWARP CM");
64 MODULE_LICENSE("Dual BSD/GPL");
65
66 static const char * const iwcm_rej_reason_strs[] = {
67 [ECONNRESET] = "reset by remote host",
68 [ECONNREFUSED] = "refused by remote application",
69 [ETIMEDOUT] = "setup timeout",
70 };
71
iwcm_reject_msg(int reason)72 const char *__attribute_const__ iwcm_reject_msg(int reason)
73 {
74 size_t index;
75
76 /* iWARP uses negative errnos */
77 index = -reason;
78
79 if (index < ARRAY_SIZE(iwcm_rej_reason_strs) &&
80 iwcm_rej_reason_strs[index])
81 return iwcm_rej_reason_strs[index];
82 else
83 return "unrecognized reason";
84 }
85 EXPORT_SYMBOL(iwcm_reject_msg);
86
87 static struct workqueue_struct *iwcm_wq;
88 struct iwcm_work {
89 struct work_struct work;
90 struct iwcm_id_private *cm_id;
91 struct list_head list;
92 struct iw_cm_event event;
93 struct list_head free_list;
94 };
95
96 static unsigned int default_backlog = 256;
97
98 /*
99 * The following services provide a mechanism for pre-allocating iwcm_work
100 * elements. The design pre-allocates them based on the cm_id type:
101 * LISTENING IDS: Get enough elements preallocated to handle the
102 * listen backlog.
103 * ACTIVE IDS: 4: CONNECT_REPLY, ESTABLISHED, DISCONNECT, CLOSE
104 * PASSIVE IDS: 3: ESTABLISHED, DISCONNECT, CLOSE
105 *
106 * Allocating them in connect and listen avoids having to deal
107 * with allocation failures on the event upcall from the provider (which
108 * is called in the interrupt context).
109 *
110 * One exception is when creating the cm_id for incoming connection requests.
111 * There are two cases:
112 * 1) in the event upcall, cm_event_handler(), for a listening cm_id. If
113 * the backlog is exceeded, then no more connection request events will
114 * be processed. cm_event_handler() returns -ENOMEM in this case. Its up
115 * to the provider to reject the connection request.
116 * 2) in the connection request workqueue handler, cm_conn_req_handler().
117 * If work elements cannot be allocated for the new connect request cm_id,
118 * then IWCM will call the provider reject method. This is ok since
119 * cm_conn_req_handler() runs in the workqueue thread context.
120 */
121
get_work(struct iwcm_id_private * cm_id_priv)122 static struct iwcm_work *get_work(struct iwcm_id_private *cm_id_priv)
123 {
124 struct iwcm_work *work;
125
126 if (list_empty(&cm_id_priv->work_free_list))
127 return NULL;
128 work = list_entry(cm_id_priv->work_free_list.next, struct iwcm_work,
129 free_list);
130 list_del_init(&work->free_list);
131 return work;
132 }
133
put_work(struct iwcm_work * work)134 static void put_work(struct iwcm_work *work)
135 {
136 list_add(&work->free_list, &work->cm_id->work_free_list);
137 }
138
dealloc_work_entries(struct iwcm_id_private * cm_id_priv)139 static void dealloc_work_entries(struct iwcm_id_private *cm_id_priv)
140 {
141 struct list_head *e, *tmp;
142
143 list_for_each_safe(e, tmp, &cm_id_priv->work_free_list)
144 kfree(list_entry(e, struct iwcm_work, free_list));
145 }
146
alloc_work_entries(struct iwcm_id_private * cm_id_priv,int count)147 static int alloc_work_entries(struct iwcm_id_private *cm_id_priv, int count)
148 {
149 struct iwcm_work *work;
150
151 BUG_ON(!list_empty(&cm_id_priv->work_free_list));
152 while (count--) {
153 work = kmalloc(sizeof(struct iwcm_work), GFP_KERNEL);
154 if (!work) {
155 dealloc_work_entries(cm_id_priv);
156 return -ENOMEM;
157 }
158 work->cm_id = cm_id_priv;
159 INIT_LIST_HEAD(&work->list);
160 put_work(work);
161 }
162 return 0;
163 }
164
165 /*
166 * Save private data from incoming connection requests to
167 * iw_cm_event, so the low level driver doesn't have to. Adjust
168 * the event ptr to point to the local copy.
169 */
copy_private_data(struct iw_cm_event * event)170 static int copy_private_data(struct iw_cm_event *event)
171 {
172 void *p;
173
174 p = kmemdup(event->private_data, event->private_data_len, GFP_ATOMIC);
175 if (!p)
176 return -ENOMEM;
177 event->private_data = p;
178 return 0;
179 }
180
free_cm_id(struct iwcm_id_private * cm_id_priv)181 static void free_cm_id(struct iwcm_id_private *cm_id_priv)
182 {
183 dealloc_work_entries(cm_id_priv);
184 kfree(cm_id_priv);
185 }
186
187 /*
188 * Release a reference on cm_id. If the last reference is being
189 * released, free the cm_id and return 1.
190 */
iwcm_deref_id(struct iwcm_id_private * cm_id_priv)191 static int iwcm_deref_id(struct iwcm_id_private *cm_id_priv)
192 {
193 BUG_ON(atomic_read(&cm_id_priv->refcount)==0);
194 if (atomic_dec_and_test(&cm_id_priv->refcount)) {
195 BUG_ON(!list_empty(&cm_id_priv->work_list));
196 free_cm_id(cm_id_priv);
197 return 1;
198 }
199
200 return 0;
201 }
202
add_ref(struct iw_cm_id * cm_id)203 static void add_ref(struct iw_cm_id *cm_id)
204 {
205 struct iwcm_id_private *cm_id_priv;
206 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
207 atomic_inc(&cm_id_priv->refcount);
208 }
209
rem_ref(struct iw_cm_id * cm_id)210 static void rem_ref(struct iw_cm_id *cm_id)
211 {
212 struct iwcm_id_private *cm_id_priv;
213
214 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
215
216 (void)iwcm_deref_id(cm_id_priv);
217 }
218
219 static int cm_event_handler(struct iw_cm_id *cm_id, struct iw_cm_event *event);
220
iw_create_cm_id(struct ib_device * device,iw_cm_handler cm_handler,void * context)221 struct iw_cm_id *iw_create_cm_id(struct ib_device *device,
222 iw_cm_handler cm_handler,
223 void *context)
224 {
225 struct iwcm_id_private *cm_id_priv;
226
227 cm_id_priv = kzalloc(sizeof(*cm_id_priv), GFP_KERNEL);
228 if (!cm_id_priv)
229 return ERR_PTR(-ENOMEM);
230
231 cm_id_priv->state = IW_CM_STATE_IDLE;
232 cm_id_priv->id.device = device;
233 cm_id_priv->id.cm_handler = cm_handler;
234 cm_id_priv->id.context = context;
235 cm_id_priv->id.event_handler = cm_event_handler;
236 cm_id_priv->id.add_ref = add_ref;
237 cm_id_priv->id.rem_ref = rem_ref;
238 spin_lock_init(&cm_id_priv->lock);
239 atomic_set(&cm_id_priv->refcount, 1);
240 init_waitqueue_head(&cm_id_priv->connect_wait);
241 init_completion(&cm_id_priv->destroy_comp);
242 INIT_LIST_HEAD(&cm_id_priv->work_list);
243 INIT_LIST_HEAD(&cm_id_priv->work_free_list);
244
245 return &cm_id_priv->id;
246 }
247 EXPORT_SYMBOL(iw_create_cm_id);
248
249
iwcm_modify_qp_err(struct ib_qp * qp)250 static int iwcm_modify_qp_err(struct ib_qp *qp)
251 {
252 struct ib_qp_attr qp_attr;
253
254 if (!qp)
255 return -EINVAL;
256
257 qp_attr.qp_state = IB_QPS_ERR;
258 return ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
259 }
260
261 /*
262 * This is really the RDMAC CLOSING state. It is most similar to the
263 * IB SQD QP state.
264 */
iwcm_modify_qp_sqd(struct ib_qp * qp)265 static int iwcm_modify_qp_sqd(struct ib_qp *qp)
266 {
267 struct ib_qp_attr qp_attr;
268
269 BUG_ON(qp == NULL);
270 qp_attr.qp_state = IB_QPS_SQD;
271 return ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
272 }
273
274 /*
275 * CM_ID <-- CLOSING
276 *
277 * Block if a passive or active connection is currently being processed. Then
278 * process the event as follows:
279 * - If we are ESTABLISHED, move to CLOSING and modify the QP state
280 * based on the abrupt flag
281 * - If the connection is already in the CLOSING or IDLE state, the peer is
282 * disconnecting concurrently with us and we've already seen the
283 * DISCONNECT event -- ignore the request and return 0
284 * - Disconnect on a listening endpoint returns -EINVAL
285 */
iw_cm_disconnect(struct iw_cm_id * cm_id,int abrupt)286 int iw_cm_disconnect(struct iw_cm_id *cm_id, int abrupt)
287 {
288 struct iwcm_id_private *cm_id_priv;
289 unsigned long flags;
290 int ret = 0;
291 struct ib_qp *qp = NULL;
292
293 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
294 /* Wait if we're currently in a connect or accept downcall */
295 wait_event(cm_id_priv->connect_wait,
296 !test_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags));
297
298 spin_lock_irqsave(&cm_id_priv->lock, flags);
299 switch (cm_id_priv->state) {
300 case IW_CM_STATE_ESTABLISHED:
301 cm_id_priv->state = IW_CM_STATE_CLOSING;
302
303 /* QP could be <nul> for user-mode client */
304 if (cm_id_priv->qp)
305 qp = cm_id_priv->qp;
306 else
307 ret = -EINVAL;
308 break;
309 case IW_CM_STATE_LISTEN:
310 ret = -EINVAL;
311 break;
312 case IW_CM_STATE_CLOSING:
313 /* remote peer closed first */
314 case IW_CM_STATE_IDLE:
315 /* accept or connect returned !0 */
316 break;
317 case IW_CM_STATE_CONN_RECV:
318 /*
319 * App called disconnect before/without calling accept after
320 * connect_request event delivered.
321 */
322 break;
323 case IW_CM_STATE_CONN_SENT:
324 /* Can only get here if wait above fails */
325 default:
326 BUG();
327 }
328 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
329
330 if (qp) {
331 if (abrupt)
332 (void) iwcm_modify_qp_err(qp);
333 else
334 (void) iwcm_modify_qp_sqd(qp);
335
336 /*
337 * If both sides are disconnecting the QP could
338 * already be in ERR or SQD states
339 */
340 ret = 0;
341 }
342
343 return ret;
344 }
345 EXPORT_SYMBOL(iw_cm_disconnect);
346
347 /*
348 * CM_ID <-- DESTROYING
349 *
350 * Clean up all resources associated with the connection and release
351 * the initial reference taken by iw_create_cm_id.
352 */
destroy_cm_id(struct iw_cm_id * cm_id)353 static void destroy_cm_id(struct iw_cm_id *cm_id)
354 {
355 struct iwcm_id_private *cm_id_priv;
356 unsigned long flags;
357
358 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
359 /*
360 * Wait if we're currently in a connect or accept downcall. A
361 * listening endpoint should never block here.
362 */
363 wait_event(cm_id_priv->connect_wait,
364 !test_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags));
365
366 /*
367 * Since we're deleting the cm_id, drop any events that
368 * might arrive before the last dereference.
369 */
370 set_bit(IWCM_F_DROP_EVENTS, &cm_id_priv->flags);
371
372 spin_lock_irqsave(&cm_id_priv->lock, flags);
373 switch (cm_id_priv->state) {
374 case IW_CM_STATE_LISTEN:
375 cm_id_priv->state = IW_CM_STATE_DESTROYING;
376 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
377 /* destroy the listening endpoint */
378 cm_id->device->iwcm->destroy_listen(cm_id);
379 spin_lock_irqsave(&cm_id_priv->lock, flags);
380 break;
381 case IW_CM_STATE_ESTABLISHED:
382 cm_id_priv->state = IW_CM_STATE_DESTROYING;
383 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
384 /* Abrupt close of the connection */
385 (void)iwcm_modify_qp_err(cm_id_priv->qp);
386 spin_lock_irqsave(&cm_id_priv->lock, flags);
387 break;
388 case IW_CM_STATE_IDLE:
389 case IW_CM_STATE_CLOSING:
390 cm_id_priv->state = IW_CM_STATE_DESTROYING;
391 break;
392 case IW_CM_STATE_CONN_RECV:
393 /*
394 * App called destroy before/without calling accept after
395 * receiving connection request event notification or
396 * returned non zero from the event callback function.
397 * In either case, must tell the provider to reject.
398 */
399 cm_id_priv->state = IW_CM_STATE_DESTROYING;
400 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
401 cm_id->device->iwcm->reject(cm_id, NULL, 0);
402 spin_lock_irqsave(&cm_id_priv->lock, flags);
403 break;
404 case IW_CM_STATE_CONN_SENT:
405 case IW_CM_STATE_DESTROYING:
406 default:
407 BUG();
408 break;
409 }
410 if (cm_id_priv->qp) {
411 cm_id_priv->id.device->iwcm->rem_ref(cm_id_priv->qp);
412 cm_id_priv->qp = NULL;
413 }
414 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
415
416 (void)iwcm_deref_id(cm_id_priv);
417 }
418
419 /*
420 * This function is only called by the application thread and cannot
421 * be called by the event thread. The function will wait for all
422 * references to be released on the cm_id and then kfree the cm_id
423 * object.
424 */
iw_destroy_cm_id(struct iw_cm_id * cm_id)425 void iw_destroy_cm_id(struct iw_cm_id *cm_id)
426 {
427 struct iwcm_id_private *cm_id_priv;
428
429 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
430 destroy_cm_id(cm_id);
431 }
432 EXPORT_SYMBOL(iw_destroy_cm_id);
433
434 /**
435 * iw_cm_map - Use portmapper to map the ports
436 * @cm_id: connection manager pointer
437 * @active: Indicates the active side when true
438 * returns nonzero for error only if iwpm_create_mapinfo() fails
439 *
440 * Tries to add a mapping for a port using the Portmapper. If
441 * successful in mapping the IP/Port it will check the remote
442 * mapped IP address for a wildcard IP address and replace the
443 * zero IP address with the remote_addr.
444 */
iw_cm_map(struct iw_cm_id * cm_id,bool active)445 static int iw_cm_map(struct iw_cm_id *cm_id, bool active)
446 {
447 cm_id->m_local_addr = cm_id->local_addr;
448 cm_id->m_remote_addr = cm_id->remote_addr;
449
450 return 0;
451 }
452
453 /*
454 * CM_ID <-- LISTEN
455 *
456 * Start listening for connect requests. Generates one CONNECT_REQUEST
457 * event for each inbound connect request.
458 */
iw_cm_listen(struct iw_cm_id * cm_id,int backlog)459 int iw_cm_listen(struct iw_cm_id *cm_id, int backlog)
460 {
461 struct iwcm_id_private *cm_id_priv;
462 unsigned long flags;
463 int ret;
464
465 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
466
467 if (!backlog)
468 backlog = default_backlog;
469
470 ret = alloc_work_entries(cm_id_priv, backlog);
471 if (ret)
472 return ret;
473
474 spin_lock_irqsave(&cm_id_priv->lock, flags);
475 switch (cm_id_priv->state) {
476 case IW_CM_STATE_IDLE:
477 cm_id_priv->state = IW_CM_STATE_LISTEN;
478 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
479 ret = iw_cm_map(cm_id, false);
480 if (!ret)
481 ret = cm_id->device->iwcm->create_listen(cm_id, backlog);
482 if (ret)
483 cm_id_priv->state = IW_CM_STATE_IDLE;
484 spin_lock_irqsave(&cm_id_priv->lock, flags);
485 break;
486 default:
487 ret = -EINVAL;
488 }
489 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
490
491 return ret;
492 }
493 EXPORT_SYMBOL(iw_cm_listen);
494
495 /*
496 * CM_ID <-- IDLE
497 *
498 * Rejects an inbound connection request. No events are generated.
499 */
iw_cm_reject(struct iw_cm_id * cm_id,const void * private_data,u8 private_data_len)500 int iw_cm_reject(struct iw_cm_id *cm_id,
501 const void *private_data,
502 u8 private_data_len)
503 {
504 struct iwcm_id_private *cm_id_priv;
505 unsigned long flags;
506 int ret;
507
508 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
509 set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
510
511 spin_lock_irqsave(&cm_id_priv->lock, flags);
512 if (cm_id_priv->state != IW_CM_STATE_CONN_RECV) {
513 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
514 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
515 wake_up_all(&cm_id_priv->connect_wait);
516 return -EINVAL;
517 }
518 cm_id_priv->state = IW_CM_STATE_IDLE;
519 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
520
521 ret = cm_id->device->iwcm->reject(cm_id, private_data,
522 private_data_len);
523
524 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
525 wake_up_all(&cm_id_priv->connect_wait);
526
527 return ret;
528 }
529 EXPORT_SYMBOL(iw_cm_reject);
530
531 /*
532 * CM_ID <-- ESTABLISHED
533 *
534 * Accepts an inbound connection request and generates an ESTABLISHED
535 * event. Callers of iw_cm_disconnect and iw_destroy_cm_id will block
536 * until the ESTABLISHED event is received from the provider.
537 */
iw_cm_accept(struct iw_cm_id * cm_id,struct iw_cm_conn_param * iw_param)538 int iw_cm_accept(struct iw_cm_id *cm_id,
539 struct iw_cm_conn_param *iw_param)
540 {
541 struct iwcm_id_private *cm_id_priv;
542 struct ib_qp *qp;
543 unsigned long flags;
544 int ret;
545
546 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
547 set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
548
549 spin_lock_irqsave(&cm_id_priv->lock, flags);
550 if (cm_id_priv->state != IW_CM_STATE_CONN_RECV) {
551 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
552 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
553 wake_up_all(&cm_id_priv->connect_wait);
554 return -EINVAL;
555 }
556 /* Get the ib_qp given the QPN */
557 qp = cm_id->device->iwcm->get_qp(cm_id->device, iw_param->qpn);
558 if (!qp) {
559 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
560 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
561 wake_up_all(&cm_id_priv->connect_wait);
562 return -EINVAL;
563 }
564 cm_id->device->iwcm->add_ref(qp);
565 cm_id_priv->qp = qp;
566 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
567
568 ret = cm_id->device->iwcm->accept(cm_id, iw_param);
569 if (ret) {
570 /* An error on accept precludes provider events */
571 BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_RECV);
572 cm_id_priv->state = IW_CM_STATE_IDLE;
573 spin_lock_irqsave(&cm_id_priv->lock, flags);
574 if (cm_id_priv->qp) {
575 cm_id->device->iwcm->rem_ref(qp);
576 cm_id_priv->qp = NULL;
577 }
578 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
579 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
580 wake_up_all(&cm_id_priv->connect_wait);
581 }
582
583 return ret;
584 }
585 EXPORT_SYMBOL(iw_cm_accept);
586
587 /*
588 * Active Side: CM_ID <-- CONN_SENT
589 *
590 * If successful, results in the generation of a CONNECT_REPLY
591 * event. iw_cm_disconnect and iw_cm_destroy will block until the
592 * CONNECT_REPLY event is received from the provider.
593 */
iw_cm_connect(struct iw_cm_id * cm_id,struct iw_cm_conn_param * iw_param)594 int iw_cm_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *iw_param)
595 {
596 struct iwcm_id_private *cm_id_priv;
597 int ret;
598 unsigned long flags;
599 struct ib_qp *qp;
600
601 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
602
603 ret = alloc_work_entries(cm_id_priv, 4);
604 if (ret)
605 return ret;
606
607 set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
608 spin_lock_irqsave(&cm_id_priv->lock, flags);
609
610 if (cm_id_priv->state != IW_CM_STATE_IDLE) {
611 ret = -EINVAL;
612 goto err;
613 }
614
615 /* Get the ib_qp given the QPN */
616 qp = cm_id->device->iwcm->get_qp(cm_id->device, iw_param->qpn);
617 if (!qp) {
618 ret = -EINVAL;
619 goto err;
620 }
621 cm_id->device->iwcm->add_ref(qp);
622 cm_id_priv->qp = qp;
623 cm_id_priv->state = IW_CM_STATE_CONN_SENT;
624 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
625
626 ret = iw_cm_map(cm_id, true);
627 if (!ret)
628 ret = cm_id->device->iwcm->connect(cm_id, iw_param);
629 if (!ret)
630 return 0; /* success */
631
632 spin_lock_irqsave(&cm_id_priv->lock, flags);
633 if (cm_id_priv->qp) {
634 cm_id->device->iwcm->rem_ref(qp);
635 cm_id_priv->qp = NULL;
636 }
637 cm_id_priv->state = IW_CM_STATE_IDLE;
638 err:
639 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
640 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
641 wake_up_all(&cm_id_priv->connect_wait);
642 return ret;
643 }
644 EXPORT_SYMBOL(iw_cm_connect);
645
646 /*
647 * Passive Side: new CM_ID <-- CONN_RECV
648 *
649 * Handles an inbound connect request. The function creates a new
650 * iw_cm_id to represent the new connection and inherits the client
651 * callback function and other attributes from the listening parent.
652 *
653 * The work item contains a pointer to the listen_cm_id and the event. The
654 * listen_cm_id contains the client cm_handler, context and
655 * device. These are copied when the device is cloned. The event
656 * contains the new four tuple.
657 *
658 * An error on the child should not affect the parent, so this
659 * function does not return a value.
660 */
cm_conn_req_handler(struct iwcm_id_private * listen_id_priv,struct iw_cm_event * iw_event)661 static void cm_conn_req_handler(struct iwcm_id_private *listen_id_priv,
662 struct iw_cm_event *iw_event)
663 {
664 unsigned long flags;
665 struct iw_cm_id *cm_id;
666 struct iwcm_id_private *cm_id_priv;
667 int ret;
668
669 /*
670 * The provider should never generate a connection request
671 * event with a bad status.
672 */
673 BUG_ON(iw_event->status);
674
675 cm_id = iw_create_cm_id(listen_id_priv->id.device,
676 listen_id_priv->id.cm_handler,
677 listen_id_priv->id.context);
678 /* If the cm_id could not be created, ignore the request */
679 if (IS_ERR(cm_id))
680 goto out;
681
682 cm_id->provider_data = iw_event->provider_data;
683 cm_id->m_local_addr = iw_event->local_addr;
684 cm_id->m_remote_addr = iw_event->remote_addr;
685 cm_id->local_addr = listen_id_priv->id.local_addr;
686 cm_id->remote_addr = iw_event->remote_addr;
687 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
688 cm_id_priv->state = IW_CM_STATE_CONN_RECV;
689
690 /*
691 * We could be destroying the listening id. If so, ignore this
692 * upcall.
693 */
694 spin_lock_irqsave(&listen_id_priv->lock, flags);
695 if (listen_id_priv->state != IW_CM_STATE_LISTEN) {
696 spin_unlock_irqrestore(&listen_id_priv->lock, flags);
697 iw_cm_reject(cm_id, NULL, 0);
698 iw_destroy_cm_id(cm_id);
699 goto out;
700 }
701 spin_unlock_irqrestore(&listen_id_priv->lock, flags);
702
703 ret = alloc_work_entries(cm_id_priv, 3);
704 if (ret) {
705 iw_cm_reject(cm_id, NULL, 0);
706 iw_destroy_cm_id(cm_id);
707 goto out;
708 }
709
710 /* Call the client CM handler */
711 ret = cm_id->cm_handler(cm_id, iw_event);
712 if (ret) {
713 iw_cm_reject(cm_id, NULL, 0);
714 iw_destroy_cm_id(cm_id);
715 }
716
717 out:
718 if (iw_event->private_data_len)
719 kfree(iw_event->private_data);
720 }
721
722 /*
723 * Passive Side: CM_ID <-- ESTABLISHED
724 *
725 * The provider generated an ESTABLISHED event which means that
726 * the MPA negotion has completed successfully and we are now in MPA
727 * FPDU mode.
728 *
729 * This event can only be received in the CONN_RECV state. If the
730 * remote peer closed, the ESTABLISHED event would be received followed
731 * by the CLOSE event. If the app closes, it will block until we wake
732 * it up after processing this event.
733 */
cm_conn_est_handler(struct iwcm_id_private * cm_id_priv,struct iw_cm_event * iw_event)734 static int cm_conn_est_handler(struct iwcm_id_private *cm_id_priv,
735 struct iw_cm_event *iw_event)
736 {
737 unsigned long flags;
738 int ret;
739
740 spin_lock_irqsave(&cm_id_priv->lock, flags);
741
742 /*
743 * We clear the CONNECT_WAIT bit here to allow the callback
744 * function to call iw_cm_disconnect. Calling iw_destroy_cm_id
745 * from a callback handler is not allowed.
746 */
747 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
748 BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_RECV);
749 cm_id_priv->state = IW_CM_STATE_ESTABLISHED;
750 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
751 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event);
752 wake_up_all(&cm_id_priv->connect_wait);
753
754 return ret;
755 }
756
757 /*
758 * Active Side: CM_ID <-- ESTABLISHED
759 *
760 * The app has called connect and is waiting for the established event to
761 * post it's requests to the server. This event will wake up anyone
762 * blocked in iw_cm_disconnect or iw_destroy_id.
763 */
cm_conn_rep_handler(struct iwcm_id_private * cm_id_priv,struct iw_cm_event * iw_event)764 static int cm_conn_rep_handler(struct iwcm_id_private *cm_id_priv,
765 struct iw_cm_event *iw_event)
766 {
767 unsigned long flags;
768 int ret;
769
770 spin_lock_irqsave(&cm_id_priv->lock, flags);
771 /*
772 * Clear the connect wait bit so a callback function calling
773 * iw_cm_disconnect will not wait and deadlock this thread
774 */
775 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
776 BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_SENT);
777 if (iw_event->status == 0) {
778 cm_id_priv->id.m_local_addr = iw_event->local_addr;
779 cm_id_priv->id.m_remote_addr = iw_event->remote_addr;
780 iw_event->local_addr = cm_id_priv->id.local_addr;
781 iw_event->remote_addr = cm_id_priv->id.remote_addr;
782 cm_id_priv->state = IW_CM_STATE_ESTABLISHED;
783 } else {
784 /* REJECTED or RESET */
785 cm_id_priv->id.device->iwcm->rem_ref(cm_id_priv->qp);
786 cm_id_priv->qp = NULL;
787 cm_id_priv->state = IW_CM_STATE_IDLE;
788 }
789 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
790 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event);
791
792 if (iw_event->private_data_len)
793 kfree(iw_event->private_data);
794
795 /* Wake up waiters on connect complete */
796 wake_up_all(&cm_id_priv->connect_wait);
797
798 return ret;
799 }
800
801 /*
802 * CM_ID <-- CLOSING
803 *
804 * If in the ESTABLISHED state, move to CLOSING.
805 */
cm_disconnect_handler(struct iwcm_id_private * cm_id_priv,struct iw_cm_event * iw_event)806 static void cm_disconnect_handler(struct iwcm_id_private *cm_id_priv,
807 struct iw_cm_event *iw_event)
808 {
809 unsigned long flags;
810
811 spin_lock_irqsave(&cm_id_priv->lock, flags);
812 if (cm_id_priv->state == IW_CM_STATE_ESTABLISHED)
813 cm_id_priv->state = IW_CM_STATE_CLOSING;
814 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
815 }
816
817 /*
818 * CM_ID <-- IDLE
819 *
820 * If in the ESTBLISHED or CLOSING states, the QP will have have been
821 * moved by the provider to the ERR state. Disassociate the CM_ID from
822 * the QP, move to IDLE, and remove the 'connected' reference.
823 *
824 * If in some other state, the cm_id was destroyed asynchronously.
825 * This is the last reference that will result in waking up
826 * the app thread blocked in iw_destroy_cm_id.
827 */
cm_close_handler(struct iwcm_id_private * cm_id_priv,struct iw_cm_event * iw_event)828 static int cm_close_handler(struct iwcm_id_private *cm_id_priv,
829 struct iw_cm_event *iw_event)
830 {
831 unsigned long flags;
832 int ret = 0;
833 spin_lock_irqsave(&cm_id_priv->lock, flags);
834
835 if (cm_id_priv->qp) {
836 cm_id_priv->id.device->iwcm->rem_ref(cm_id_priv->qp);
837 cm_id_priv->qp = NULL;
838 }
839 switch (cm_id_priv->state) {
840 case IW_CM_STATE_ESTABLISHED:
841 case IW_CM_STATE_CLOSING:
842 cm_id_priv->state = IW_CM_STATE_IDLE;
843 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
844 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event);
845 spin_lock_irqsave(&cm_id_priv->lock, flags);
846 break;
847 case IW_CM_STATE_DESTROYING:
848 break;
849 default:
850 BUG();
851 }
852 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
853
854 return ret;
855 }
856
process_event(struct iwcm_id_private * cm_id_priv,struct iw_cm_event * iw_event)857 static int process_event(struct iwcm_id_private *cm_id_priv,
858 struct iw_cm_event *iw_event)
859 {
860 int ret = 0;
861
862 switch (iw_event->event) {
863 case IW_CM_EVENT_CONNECT_REQUEST:
864 cm_conn_req_handler(cm_id_priv, iw_event);
865 break;
866 case IW_CM_EVENT_CONNECT_REPLY:
867 ret = cm_conn_rep_handler(cm_id_priv, iw_event);
868 break;
869 case IW_CM_EVENT_ESTABLISHED:
870 ret = cm_conn_est_handler(cm_id_priv, iw_event);
871 break;
872 case IW_CM_EVENT_DISCONNECT:
873 cm_disconnect_handler(cm_id_priv, iw_event);
874 break;
875 case IW_CM_EVENT_CLOSE:
876 ret = cm_close_handler(cm_id_priv, iw_event);
877 break;
878 default:
879 BUG();
880 }
881
882 return ret;
883 }
884
885 /*
886 * Process events on the work_list for the cm_id. If the callback
887 * function requests that the cm_id be deleted, a flag is set in the
888 * cm_id flags to indicate that when the last reference is
889 * removed, the cm_id is to be destroyed. This is necessary to
890 * distinguish between an object that will be destroyed by the app
891 * thread asleep on the destroy_comp list vs. an object destroyed
892 * here synchronously when the last reference is removed.
893 */
cm_work_handler(struct work_struct * _work)894 static void cm_work_handler(struct work_struct *_work)
895 {
896 struct iwcm_work *work = container_of(_work, struct iwcm_work, work);
897 struct iw_cm_event levent;
898 struct iwcm_id_private *cm_id_priv = work->cm_id;
899 unsigned long flags;
900 int empty;
901 int ret = 0;
902
903 spin_lock_irqsave(&cm_id_priv->lock, flags);
904 empty = list_empty(&cm_id_priv->work_list);
905 while (!empty) {
906 work = list_entry(cm_id_priv->work_list.next,
907 struct iwcm_work, list);
908 list_del_init(&work->list);
909 empty = list_empty(&cm_id_priv->work_list);
910 levent = work->event;
911 put_work(work);
912 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
913
914 if (!test_bit(IWCM_F_DROP_EVENTS, &cm_id_priv->flags)) {
915 ret = process_event(cm_id_priv, &levent);
916 if (ret)
917 destroy_cm_id(&cm_id_priv->id);
918 } else
919 pr_debug("dropping event %d\n", levent.event);
920 if (iwcm_deref_id(cm_id_priv))
921 return;
922 if (empty)
923 return;
924 spin_lock_irqsave(&cm_id_priv->lock, flags);
925 }
926 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
927 }
928
929 /*
930 * This function is called on interrupt context. Schedule events on
931 * the iwcm_wq thread to allow callback functions to downcall into
932 * the CM and/or block. Events are queued to a per-CM_ID
933 * work_list. If this is the first event on the work_list, the work
934 * element is also queued on the iwcm_wq thread.
935 *
936 * Each event holds a reference on the cm_id. Until the last posted
937 * event has been delivered and processed, the cm_id cannot be
938 * deleted.
939 *
940 * Returns:
941 * 0 - the event was handled.
942 * -ENOMEM - the event was not handled due to lack of resources.
943 */
cm_event_handler(struct iw_cm_id * cm_id,struct iw_cm_event * iw_event)944 static int cm_event_handler(struct iw_cm_id *cm_id,
945 struct iw_cm_event *iw_event)
946 {
947 struct iwcm_work *work;
948 struct iwcm_id_private *cm_id_priv;
949 unsigned long flags;
950 int ret = 0;
951
952 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
953
954 spin_lock_irqsave(&cm_id_priv->lock, flags);
955 work = get_work(cm_id_priv);
956 if (!work) {
957 ret = -ENOMEM;
958 goto out;
959 }
960
961 INIT_WORK(&work->work, cm_work_handler);
962 work->cm_id = cm_id_priv;
963 work->event = *iw_event;
964
965 if ((work->event.event == IW_CM_EVENT_CONNECT_REQUEST ||
966 work->event.event == IW_CM_EVENT_CONNECT_REPLY) &&
967 work->event.private_data_len) {
968 ret = copy_private_data(&work->event);
969 if (ret) {
970 put_work(work);
971 goto out;
972 }
973 }
974
975 atomic_inc(&cm_id_priv->refcount);
976 if (list_empty(&cm_id_priv->work_list)) {
977 list_add_tail(&work->list, &cm_id_priv->work_list);
978 queue_work(iwcm_wq, &work->work);
979 } else
980 list_add_tail(&work->list, &cm_id_priv->work_list);
981 out:
982 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
983 return ret;
984 }
985
iwcm_init_qp_init_attr(struct iwcm_id_private * cm_id_priv,struct ib_qp_attr * qp_attr,int * qp_attr_mask)986 static int iwcm_init_qp_init_attr(struct iwcm_id_private *cm_id_priv,
987 struct ib_qp_attr *qp_attr,
988 int *qp_attr_mask)
989 {
990 unsigned long flags;
991 int ret;
992
993 spin_lock_irqsave(&cm_id_priv->lock, flags);
994 switch (cm_id_priv->state) {
995 case IW_CM_STATE_IDLE:
996 case IW_CM_STATE_CONN_SENT:
997 case IW_CM_STATE_CONN_RECV:
998 case IW_CM_STATE_ESTABLISHED:
999 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
1000 qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE|
1001 IB_ACCESS_REMOTE_READ;
1002 ret = 0;
1003 break;
1004 default:
1005 ret = -EINVAL;
1006 break;
1007 }
1008 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1009 return ret;
1010 }
1011
iwcm_init_qp_rts_attr(struct iwcm_id_private * cm_id_priv,struct ib_qp_attr * qp_attr,int * qp_attr_mask)1012 static int iwcm_init_qp_rts_attr(struct iwcm_id_private *cm_id_priv,
1013 struct ib_qp_attr *qp_attr,
1014 int *qp_attr_mask)
1015 {
1016 unsigned long flags;
1017 int ret;
1018
1019 spin_lock_irqsave(&cm_id_priv->lock, flags);
1020 switch (cm_id_priv->state) {
1021 case IW_CM_STATE_IDLE:
1022 case IW_CM_STATE_CONN_SENT:
1023 case IW_CM_STATE_CONN_RECV:
1024 case IW_CM_STATE_ESTABLISHED:
1025 *qp_attr_mask = 0;
1026 ret = 0;
1027 break;
1028 default:
1029 ret = -EINVAL;
1030 break;
1031 }
1032 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1033 return ret;
1034 }
1035
iw_cm_init_qp_attr(struct iw_cm_id * cm_id,struct ib_qp_attr * qp_attr,int * qp_attr_mask)1036 int iw_cm_init_qp_attr(struct iw_cm_id *cm_id,
1037 struct ib_qp_attr *qp_attr,
1038 int *qp_attr_mask)
1039 {
1040 struct iwcm_id_private *cm_id_priv;
1041 int ret;
1042
1043 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
1044 switch (qp_attr->qp_state) {
1045 case IB_QPS_INIT:
1046 case IB_QPS_RTR:
1047 ret = iwcm_init_qp_init_attr(cm_id_priv,
1048 qp_attr, qp_attr_mask);
1049 break;
1050 case IB_QPS_RTS:
1051 ret = iwcm_init_qp_rts_attr(cm_id_priv,
1052 qp_attr, qp_attr_mask);
1053 break;
1054 default:
1055 ret = -EINVAL;
1056 break;
1057 }
1058 return ret;
1059 }
1060 EXPORT_SYMBOL(iw_cm_init_qp_attr);
1061
iw_cm_init(void)1062 static int __init iw_cm_init(void)
1063 {
1064 iwcm_wq = alloc_ordered_workqueue("iw_cm_wq", WQ_MEM_RECLAIM);
1065 if (!iwcm_wq)
1066 return -ENOMEM;
1067
1068 return 0;
1069 }
1070
iw_cm_cleanup(void)1071 static void __exit iw_cm_cleanup(void)
1072 {
1073 destroy_workqueue(iwcm_wq);
1074 }
1075
1076 module_init_order(iw_cm_init, SI_ORDER_FIRST);
1077 module_exit_order(iw_cm_cleanup, SI_ORDER_FIRST);
1078