GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / infiniband / core / iwcm.c
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
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
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
152 static void put_work(struct iwcm_work *work)
153 {
154         list_add(&work->free_list, &work->cm_id->work_free_list);
155 }
156
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
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  */
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
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  */
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
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
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
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
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  */
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  */
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  */
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  */
450 void iw_destroy_cm_id(struct iw_cm_id *cm_id)
451 {
452         destroy_cm_id(cm_id);
453 }
454 EXPORT_SYMBOL(iw_destroy_cm_id);
455
456 /**
457  * iw_cm_check_wildcard - If IP address is 0 then use original
458  * @pm_addr: sockaddr containing the ip to check for wildcard
459  * @cm_addr: sockaddr containing the actual IP address
460  * @cm_outaddr: sockaddr to set IP addr which leaving port
461  *
462  *  Checks the pm_addr for wildcard and then sets cm_outaddr's
463  *  IP to the actual (cm_addr).
464  */
465 static void iw_cm_check_wildcard(struct sockaddr_storage *pm_addr,
466                                  struct sockaddr_storage *cm_addr,
467                                  struct sockaddr_storage *cm_outaddr)
468 {
469         if (pm_addr->ss_family == AF_INET) {
470                 struct sockaddr_in *pm4_addr = (struct sockaddr_in *)pm_addr;
471
472                 if (pm4_addr->sin_addr.s_addr == htonl(INADDR_ANY)) {
473                         struct sockaddr_in *cm4_addr =
474                                 (struct sockaddr_in *)cm_addr;
475                         struct sockaddr_in *cm4_outaddr =
476                                 (struct sockaddr_in *)cm_outaddr;
477
478                         cm4_outaddr->sin_addr = cm4_addr->sin_addr;
479                 }
480         } else {
481                 struct sockaddr_in6 *pm6_addr = (struct sockaddr_in6 *)pm_addr;
482
483                 if (ipv6_addr_type(&pm6_addr->sin6_addr) == IPV6_ADDR_ANY) {
484                         struct sockaddr_in6 *cm6_addr =
485                                 (struct sockaddr_in6 *)cm_addr;
486                         struct sockaddr_in6 *cm6_outaddr =
487                                 (struct sockaddr_in6 *)cm_outaddr;
488
489                         cm6_outaddr->sin6_addr = cm6_addr->sin6_addr;
490                 }
491         }
492 }
493
494 /**
495  * iw_cm_map - Use portmapper to map the ports
496  * @cm_id: connection manager pointer
497  * @active: Indicates the active side when true
498  * returns nonzero for error only if iwpm_create_mapinfo() fails
499  *
500  * Tries to add a mapping for a port using the Portmapper. If
501  * successful in mapping the IP/Port it will check the remote
502  * mapped IP address for a wildcard IP address and replace the
503  * zero IP address with the remote_addr.
504  */
505 static int iw_cm_map(struct iw_cm_id *cm_id, bool active)
506 {
507         struct iwpm_dev_data pm_reg_msg;
508         struct iwpm_sa_data pm_msg;
509         int status;
510
511         cm_id->m_local_addr = cm_id->local_addr;
512         cm_id->m_remote_addr = cm_id->remote_addr;
513
514         memcpy(pm_reg_msg.dev_name, cm_id->device->name,
515                sizeof(pm_reg_msg.dev_name));
516         memcpy(pm_reg_msg.if_name, cm_id->device->iwcm->ifname,
517                sizeof(pm_reg_msg.if_name));
518
519         if (iwpm_register_pid(&pm_reg_msg, RDMA_NL_IWCM) ||
520             !iwpm_valid_pid())
521                 return 0;
522
523         cm_id->mapped = true;
524         pm_msg.loc_addr = cm_id->local_addr;
525         pm_msg.rem_addr = cm_id->remote_addr;
526         if (active)
527                 status = iwpm_add_and_query_mapping(&pm_msg,
528                                                     RDMA_NL_IWCM);
529         else
530                 status = iwpm_add_mapping(&pm_msg, RDMA_NL_IWCM);
531
532         if (!status) {
533                 cm_id->m_local_addr = pm_msg.mapped_loc_addr;
534                 if (active) {
535                         cm_id->m_remote_addr = pm_msg.mapped_rem_addr;
536                         iw_cm_check_wildcard(&pm_msg.mapped_rem_addr,
537                                              &cm_id->remote_addr,
538                                              &cm_id->m_remote_addr);
539                 }
540         }
541
542         return iwpm_create_mapinfo(&cm_id->local_addr,
543                                    &cm_id->m_local_addr,
544                                    RDMA_NL_IWCM);
545 }
546
547 /*
548  * CM_ID <-- LISTEN
549  *
550  * Start listening for connect requests. Generates one CONNECT_REQUEST
551  * event for each inbound connect request.
552  */
553 int iw_cm_listen(struct iw_cm_id *cm_id, int backlog)
554 {
555         struct iwcm_id_private *cm_id_priv;
556         unsigned long flags;
557         int ret;
558
559         cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
560
561         if (!backlog)
562                 backlog = default_backlog;
563
564         ret = alloc_work_entries(cm_id_priv, backlog);
565         if (ret)
566                 return ret;
567
568         spin_lock_irqsave(&cm_id_priv->lock, flags);
569         switch (cm_id_priv->state) {
570         case IW_CM_STATE_IDLE:
571                 cm_id_priv->state = IW_CM_STATE_LISTEN;
572                 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
573                 ret = iw_cm_map(cm_id, false);
574                 if (!ret)
575                         ret = cm_id->device->iwcm->create_listen(cm_id, backlog);
576                 if (ret)
577                         cm_id_priv->state = IW_CM_STATE_IDLE;
578                 spin_lock_irqsave(&cm_id_priv->lock, flags);
579                 break;
580         default:
581                 ret = -EINVAL;
582         }
583         spin_unlock_irqrestore(&cm_id_priv->lock, flags);
584
585         return ret;
586 }
587 EXPORT_SYMBOL(iw_cm_listen);
588
589 /*
590  * CM_ID <-- IDLE
591  *
592  * Rejects an inbound connection request. No events are generated.
593  */
594 int iw_cm_reject(struct iw_cm_id *cm_id,
595                  const void *private_data,
596                  u8 private_data_len)
597 {
598         struct iwcm_id_private *cm_id_priv;
599         unsigned long flags;
600         int ret;
601
602         cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
603         set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
604
605         spin_lock_irqsave(&cm_id_priv->lock, flags);
606         if (cm_id_priv->state != IW_CM_STATE_CONN_RECV) {
607                 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
608                 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
609                 wake_up_all(&cm_id_priv->connect_wait);
610                 return -EINVAL;
611         }
612         cm_id_priv->state = IW_CM_STATE_IDLE;
613         spin_unlock_irqrestore(&cm_id_priv->lock, flags);
614
615         ret = cm_id->device->iwcm->reject(cm_id, private_data,
616                                           private_data_len);
617
618         clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
619         wake_up_all(&cm_id_priv->connect_wait);
620
621         return ret;
622 }
623 EXPORT_SYMBOL(iw_cm_reject);
624
625 /*
626  * CM_ID <-- ESTABLISHED
627  *
628  * Accepts an inbound connection request and generates an ESTABLISHED
629  * event. Callers of iw_cm_disconnect and iw_destroy_cm_id will block
630  * until the ESTABLISHED event is received from the provider.
631  */
632 int iw_cm_accept(struct iw_cm_id *cm_id,
633                  struct iw_cm_conn_param *iw_param)
634 {
635         struct iwcm_id_private *cm_id_priv;
636         struct ib_qp *qp;
637         unsigned long flags;
638         int ret;
639
640         cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
641         set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
642
643         spin_lock_irqsave(&cm_id_priv->lock, flags);
644         if (cm_id_priv->state != IW_CM_STATE_CONN_RECV) {
645                 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
646                 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
647                 wake_up_all(&cm_id_priv->connect_wait);
648                 return -EINVAL;
649         }
650         /* Get the ib_qp given the QPN */
651         qp = cm_id->device->iwcm->get_qp(cm_id->device, iw_param->qpn);
652         if (!qp) {
653                 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
654                 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
655                 wake_up_all(&cm_id_priv->connect_wait);
656                 return -EINVAL;
657         }
658         cm_id->device->iwcm->add_ref(qp);
659         cm_id_priv->qp = qp;
660         spin_unlock_irqrestore(&cm_id_priv->lock, flags);
661
662         ret = cm_id->device->iwcm->accept(cm_id, iw_param);
663         if (ret) {
664                 /* An error on accept precludes provider events */
665                 BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_RECV);
666                 cm_id_priv->state = IW_CM_STATE_IDLE;
667                 spin_lock_irqsave(&cm_id_priv->lock, flags);
668                 if (cm_id_priv->qp) {
669                         cm_id->device->iwcm->rem_ref(qp);
670                         cm_id_priv->qp = NULL;
671                 }
672                 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
673                 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
674                 wake_up_all(&cm_id_priv->connect_wait);
675         }
676
677         return ret;
678 }
679 EXPORT_SYMBOL(iw_cm_accept);
680
681 /*
682  * Active Side: CM_ID <-- CONN_SENT
683  *
684  * If successful, results in the generation of a CONNECT_REPLY
685  * event. iw_cm_disconnect and iw_cm_destroy will block until the
686  * CONNECT_REPLY event is received from the provider.
687  */
688 int iw_cm_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *iw_param)
689 {
690         struct iwcm_id_private *cm_id_priv;
691         int ret;
692         unsigned long flags;
693         struct ib_qp *qp;
694
695         cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
696
697         ret = alloc_work_entries(cm_id_priv, 4);
698         if (ret)
699                 return ret;
700
701         set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
702         spin_lock_irqsave(&cm_id_priv->lock, flags);
703
704         if (cm_id_priv->state != IW_CM_STATE_IDLE) {
705                 ret = -EINVAL;
706                 goto err;
707         }
708
709         /* Get the ib_qp given the QPN */
710         qp = cm_id->device->iwcm->get_qp(cm_id->device, iw_param->qpn);
711         if (!qp) {
712                 ret = -EINVAL;
713                 goto err;
714         }
715         cm_id->device->iwcm->add_ref(qp);
716         cm_id_priv->qp = qp;
717         cm_id_priv->state = IW_CM_STATE_CONN_SENT;
718         spin_unlock_irqrestore(&cm_id_priv->lock, flags);
719
720         ret = iw_cm_map(cm_id, true);
721         if (!ret)
722                 ret = cm_id->device->iwcm->connect(cm_id, iw_param);
723         if (!ret)
724                 return 0;       /* success */
725
726         spin_lock_irqsave(&cm_id_priv->lock, flags);
727         if (cm_id_priv->qp) {
728                 cm_id->device->iwcm->rem_ref(qp);
729                 cm_id_priv->qp = NULL;
730         }
731         cm_id_priv->state = IW_CM_STATE_IDLE;
732 err:
733         spin_unlock_irqrestore(&cm_id_priv->lock, flags);
734         clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
735         wake_up_all(&cm_id_priv->connect_wait);
736         return ret;
737 }
738 EXPORT_SYMBOL(iw_cm_connect);
739
740 /*
741  * Passive Side: new CM_ID <-- CONN_RECV
742  *
743  * Handles an inbound connect request. The function creates a new
744  * iw_cm_id to represent the new connection and inherits the client
745  * callback function and other attributes from the listening parent.
746  *
747  * The work item contains a pointer to the listen_cm_id and the event. The
748  * listen_cm_id contains the client cm_handler, context and
749  * device. These are copied when the device is cloned. The event
750  * contains the new four tuple.
751  *
752  * An error on the child should not affect the parent, so this
753  * function does not return a value.
754  */
755 static void cm_conn_req_handler(struct iwcm_id_private *listen_id_priv,
756                                 struct iw_cm_event *iw_event)
757 {
758         unsigned long flags;
759         struct iw_cm_id *cm_id;
760         struct iwcm_id_private *cm_id_priv;
761         int ret;
762
763         /*
764          * The provider should never generate a connection request
765          * event with a bad status.
766          */
767         BUG_ON(iw_event->status);
768
769         cm_id = iw_create_cm_id(listen_id_priv->id.device,
770                                 listen_id_priv->id.cm_handler,
771                                 listen_id_priv->id.context);
772         /* If the cm_id could not be created, ignore the request */
773         if (IS_ERR(cm_id))
774                 goto out;
775
776         cm_id->provider_data = iw_event->provider_data;
777         cm_id->m_local_addr = iw_event->local_addr;
778         cm_id->m_remote_addr = iw_event->remote_addr;
779         cm_id->local_addr = listen_id_priv->id.local_addr;
780
781         ret = iwpm_get_remote_info(&listen_id_priv->id.m_local_addr,
782                                    &iw_event->remote_addr,
783                                    &cm_id->remote_addr,
784                                    RDMA_NL_IWCM);
785         if (ret) {
786                 cm_id->remote_addr = iw_event->remote_addr;
787         } else {
788                 iw_cm_check_wildcard(&listen_id_priv->id.m_local_addr,
789                                      &iw_event->local_addr,
790                                      &cm_id->local_addr);
791                 iw_event->local_addr = cm_id->local_addr;
792                 iw_event->remote_addr = cm_id->remote_addr;
793         }
794
795         cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
796         cm_id_priv->state = IW_CM_STATE_CONN_RECV;
797
798         /*
799          * We could be destroying the listening id. If so, ignore this
800          * upcall.
801          */
802         spin_lock_irqsave(&listen_id_priv->lock, flags);
803         if (listen_id_priv->state != IW_CM_STATE_LISTEN) {
804                 spin_unlock_irqrestore(&listen_id_priv->lock, flags);
805                 iw_cm_reject(cm_id, NULL, 0);
806                 iw_destroy_cm_id(cm_id);
807                 goto out;
808         }
809         spin_unlock_irqrestore(&listen_id_priv->lock, flags);
810
811         ret = alloc_work_entries(cm_id_priv, 3);
812         if (ret) {
813                 iw_cm_reject(cm_id, NULL, 0);
814                 iw_destroy_cm_id(cm_id);
815                 goto out;
816         }
817
818         /* Call the client CM handler */
819         ret = cm_id->cm_handler(cm_id, iw_event);
820         if (ret) {
821                 iw_cm_reject(cm_id, NULL, 0);
822                 iw_destroy_cm_id(cm_id);
823         }
824
825 out:
826         if (iw_event->private_data_len)
827                 kfree(iw_event->private_data);
828 }
829
830 /*
831  * Passive Side: CM_ID <-- ESTABLISHED
832  *
833  * The provider generated an ESTABLISHED event which means that
834  * the MPA negotion has completed successfully and we are now in MPA
835  * FPDU mode.
836  *
837  * This event can only be received in the CONN_RECV state. If the
838  * remote peer closed, the ESTABLISHED event would be received followed
839  * by the CLOSE event. If the app closes, it will block until we wake
840  * it up after processing this event.
841  */
842 static int cm_conn_est_handler(struct iwcm_id_private *cm_id_priv,
843                                struct iw_cm_event *iw_event)
844 {
845         unsigned long flags;
846         int ret;
847
848         spin_lock_irqsave(&cm_id_priv->lock, flags);
849
850         /*
851          * We clear the CONNECT_WAIT bit here to allow the callback
852          * function to call iw_cm_disconnect. Calling iw_destroy_cm_id
853          * from a callback handler is not allowed.
854          */
855         clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
856         BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_RECV);
857         cm_id_priv->state = IW_CM_STATE_ESTABLISHED;
858         spin_unlock_irqrestore(&cm_id_priv->lock, flags);
859         ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event);
860         wake_up_all(&cm_id_priv->connect_wait);
861
862         return ret;
863 }
864
865 /*
866  * Active Side: CM_ID <-- ESTABLISHED
867  *
868  * The app has called connect and is waiting for the established event to
869  * post it's requests to the server. This event will wake up anyone
870  * blocked in iw_cm_disconnect or iw_destroy_id.
871  */
872 static int cm_conn_rep_handler(struct iwcm_id_private *cm_id_priv,
873                                struct iw_cm_event *iw_event)
874 {
875         unsigned long flags;
876         int ret;
877
878         spin_lock_irqsave(&cm_id_priv->lock, flags);
879         /*
880          * Clear the connect wait bit so a callback function calling
881          * iw_cm_disconnect will not wait and deadlock this thread
882          */
883         clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
884         BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_SENT);
885         if (iw_event->status == 0) {
886                 cm_id_priv->id.m_local_addr = iw_event->local_addr;
887                 cm_id_priv->id.m_remote_addr = iw_event->remote_addr;
888                 iw_event->local_addr = cm_id_priv->id.local_addr;
889                 iw_event->remote_addr = cm_id_priv->id.remote_addr;
890                 cm_id_priv->state = IW_CM_STATE_ESTABLISHED;
891         } else {
892                 /* REJECTED or RESET */
893                 cm_id_priv->id.device->iwcm->rem_ref(cm_id_priv->qp);
894                 cm_id_priv->qp = NULL;
895                 cm_id_priv->state = IW_CM_STATE_IDLE;
896         }
897         spin_unlock_irqrestore(&cm_id_priv->lock, flags);
898         ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event);
899
900         if (iw_event->private_data_len)
901                 kfree(iw_event->private_data);
902
903         /* Wake up waiters on connect complete */
904         wake_up_all(&cm_id_priv->connect_wait);
905
906         return ret;
907 }
908
909 /*
910  * CM_ID <-- CLOSING
911  *
912  * If in the ESTABLISHED state, move to CLOSING.
913  */
914 static void cm_disconnect_handler(struct iwcm_id_private *cm_id_priv,
915                                   struct iw_cm_event *iw_event)
916 {
917         unsigned long flags;
918
919         spin_lock_irqsave(&cm_id_priv->lock, flags);
920         if (cm_id_priv->state == IW_CM_STATE_ESTABLISHED)
921                 cm_id_priv->state = IW_CM_STATE_CLOSING;
922         spin_unlock_irqrestore(&cm_id_priv->lock, flags);
923 }
924
925 /*
926  * CM_ID <-- IDLE
927  *
928  * If in the ESTBLISHED or CLOSING states, the QP will have have been
929  * moved by the provider to the ERR state. Disassociate the CM_ID from
930  * the QP,  move to IDLE, and remove the 'connected' reference.
931  *
932  * If in some other state, the cm_id was destroyed asynchronously.
933  * This is the last reference that will result in waking up
934  * the app thread blocked in iw_destroy_cm_id.
935  */
936 static int cm_close_handler(struct iwcm_id_private *cm_id_priv,
937                                   struct iw_cm_event *iw_event)
938 {
939         unsigned long flags;
940         int ret = 0;
941         spin_lock_irqsave(&cm_id_priv->lock, flags);
942
943         if (cm_id_priv->qp) {
944                 cm_id_priv->id.device->iwcm->rem_ref(cm_id_priv->qp);
945                 cm_id_priv->qp = NULL;
946         }
947         switch (cm_id_priv->state) {
948         case IW_CM_STATE_ESTABLISHED:
949         case IW_CM_STATE_CLOSING:
950                 cm_id_priv->state = IW_CM_STATE_IDLE;
951                 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
952                 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event);
953                 spin_lock_irqsave(&cm_id_priv->lock, flags);
954                 break;
955         case IW_CM_STATE_DESTROYING:
956                 break;
957         default:
958                 BUG();
959         }
960         spin_unlock_irqrestore(&cm_id_priv->lock, flags);
961
962         return ret;
963 }
964
965 static int process_event(struct iwcm_id_private *cm_id_priv,
966                          struct iw_cm_event *iw_event)
967 {
968         int ret = 0;
969
970         switch (iw_event->event) {
971         case IW_CM_EVENT_CONNECT_REQUEST:
972                 cm_conn_req_handler(cm_id_priv, iw_event);
973                 break;
974         case IW_CM_EVENT_CONNECT_REPLY:
975                 ret = cm_conn_rep_handler(cm_id_priv, iw_event);
976                 break;
977         case IW_CM_EVENT_ESTABLISHED:
978                 ret = cm_conn_est_handler(cm_id_priv, iw_event);
979                 break;
980         case IW_CM_EVENT_DISCONNECT:
981                 cm_disconnect_handler(cm_id_priv, iw_event);
982                 break;
983         case IW_CM_EVENT_CLOSE:
984                 ret = cm_close_handler(cm_id_priv, iw_event);
985                 break;
986         default:
987                 BUG();
988         }
989
990         return ret;
991 }
992
993 /*
994  * Process events on the work_list for the cm_id. If the callback
995  * function requests that the cm_id be deleted, a flag is set in the
996  * cm_id flags to indicate that when the last reference is
997  * removed, the cm_id is to be destroyed. This is necessary to
998  * distinguish between an object that will be destroyed by the app
999  * thread asleep on the destroy_comp list vs. an object destroyed
1000  * here synchronously when the last reference is removed.
1001  */
1002 static void cm_work_handler(struct work_struct *_work)
1003 {
1004         struct iwcm_work *work = container_of(_work, struct iwcm_work, work);
1005         struct iw_cm_event levent;
1006         struct iwcm_id_private *cm_id_priv = work->cm_id;
1007         unsigned long flags;
1008         int empty;
1009         int ret = 0;
1010
1011         spin_lock_irqsave(&cm_id_priv->lock, flags);
1012         empty = list_empty(&cm_id_priv->work_list);
1013         while (!empty) {
1014                 work = list_entry(cm_id_priv->work_list.next,
1015                                   struct iwcm_work, list);
1016                 list_del_init(&work->list);
1017                 empty = list_empty(&cm_id_priv->work_list);
1018                 levent = work->event;
1019                 put_work(work);
1020                 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1021
1022                 if (!test_bit(IWCM_F_DROP_EVENTS, &cm_id_priv->flags)) {
1023                         ret = process_event(cm_id_priv, &levent);
1024                         if (ret)
1025                                 destroy_cm_id(&cm_id_priv->id);
1026                 } else
1027                         pr_debug("dropping event %d\n", levent.event);
1028                 if (iwcm_deref_id(cm_id_priv))
1029                         return;
1030                 if (empty)
1031                         return;
1032                 spin_lock_irqsave(&cm_id_priv->lock, flags);
1033         }
1034         spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1035 }
1036
1037 /*
1038  * This function is called on interrupt context. Schedule events on
1039  * the iwcm_wq thread to allow callback functions to downcall into
1040  * the CM and/or block.  Events are queued to a per-CM_ID
1041  * work_list. If this is the first event on the work_list, the work
1042  * element is also queued on the iwcm_wq thread.
1043  *
1044  * Each event holds a reference on the cm_id. Until the last posted
1045  * event has been delivered and processed, the cm_id cannot be
1046  * deleted.
1047  *
1048  * Returns:
1049  *            0 - the event was handled.
1050  *      -ENOMEM - the event was not handled due to lack of resources.
1051  */
1052 static int cm_event_handler(struct iw_cm_id *cm_id,
1053                              struct iw_cm_event *iw_event)
1054 {
1055         struct iwcm_work *work;
1056         struct iwcm_id_private *cm_id_priv;
1057         unsigned long flags;
1058         int ret = 0;
1059
1060         cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
1061
1062         spin_lock_irqsave(&cm_id_priv->lock, flags);
1063         work = get_work(cm_id_priv);
1064         if (!work) {
1065                 ret = -ENOMEM;
1066                 goto out;
1067         }
1068
1069         INIT_WORK(&work->work, cm_work_handler);
1070         work->cm_id = cm_id_priv;
1071         work->event = *iw_event;
1072
1073         if ((work->event.event == IW_CM_EVENT_CONNECT_REQUEST ||
1074              work->event.event == IW_CM_EVENT_CONNECT_REPLY) &&
1075             work->event.private_data_len) {
1076                 ret = copy_private_data(&work->event);
1077                 if (ret) {
1078                         put_work(work);
1079                         goto out;
1080                 }
1081         }
1082
1083         atomic_inc(&cm_id_priv->refcount);
1084         if (list_empty(&cm_id_priv->work_list)) {
1085                 list_add_tail(&work->list, &cm_id_priv->work_list);
1086                 queue_work(iwcm_wq, &work->work);
1087         } else
1088                 list_add_tail(&work->list, &cm_id_priv->work_list);
1089 out:
1090         spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1091         return ret;
1092 }
1093
1094 static int iwcm_init_qp_init_attr(struct iwcm_id_private *cm_id_priv,
1095                                   struct ib_qp_attr *qp_attr,
1096                                   int *qp_attr_mask)
1097 {
1098         unsigned long flags;
1099         int ret;
1100
1101         spin_lock_irqsave(&cm_id_priv->lock, flags);
1102         switch (cm_id_priv->state) {
1103         case IW_CM_STATE_IDLE:
1104         case IW_CM_STATE_CONN_SENT:
1105         case IW_CM_STATE_CONN_RECV:
1106         case IW_CM_STATE_ESTABLISHED:
1107                 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
1108                 qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE|
1109                                            IB_ACCESS_REMOTE_READ;
1110                 ret = 0;
1111                 break;
1112         default:
1113                 ret = -EINVAL;
1114                 break;
1115         }
1116         spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1117         return ret;
1118 }
1119
1120 static int iwcm_init_qp_rts_attr(struct iwcm_id_private *cm_id_priv,
1121                                   struct ib_qp_attr *qp_attr,
1122                                   int *qp_attr_mask)
1123 {
1124         unsigned long flags;
1125         int ret;
1126
1127         spin_lock_irqsave(&cm_id_priv->lock, flags);
1128         switch (cm_id_priv->state) {
1129         case IW_CM_STATE_IDLE:
1130         case IW_CM_STATE_CONN_SENT:
1131         case IW_CM_STATE_CONN_RECV:
1132         case IW_CM_STATE_ESTABLISHED:
1133                 *qp_attr_mask = 0;
1134                 ret = 0;
1135                 break;
1136         default:
1137                 ret = -EINVAL;
1138                 break;
1139         }
1140         spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1141         return ret;
1142 }
1143
1144 int iw_cm_init_qp_attr(struct iw_cm_id *cm_id,
1145                        struct ib_qp_attr *qp_attr,
1146                        int *qp_attr_mask)
1147 {
1148         struct iwcm_id_private *cm_id_priv;
1149         int ret;
1150
1151         cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
1152         switch (qp_attr->qp_state) {
1153         case IB_QPS_INIT:
1154         case IB_QPS_RTR:
1155                 ret = iwcm_init_qp_init_attr(cm_id_priv,
1156                                              qp_attr, qp_attr_mask);
1157                 break;
1158         case IB_QPS_RTS:
1159                 ret = iwcm_init_qp_rts_attr(cm_id_priv,
1160                                             qp_attr, qp_attr_mask);
1161                 break;
1162         default:
1163                 ret = -EINVAL;
1164                 break;
1165         }
1166         return ret;
1167 }
1168 EXPORT_SYMBOL(iw_cm_init_qp_attr);
1169
1170 static int __init iw_cm_init(void)
1171 {
1172         int ret;
1173
1174         ret = iwpm_init(RDMA_NL_IWCM);
1175         if (ret)
1176                 return ret;
1177
1178         iwcm_wq = alloc_ordered_workqueue("iw_cm_wq", 0);
1179         if (!iwcm_wq)
1180                 goto err_alloc;
1181
1182         iwcm_ctl_table_hdr = register_net_sysctl(&init_net, "net/iw_cm",
1183                                                  iwcm_ctl_table);
1184         if (!iwcm_ctl_table_hdr) {
1185                 pr_err("iw_cm: couldn't register sysctl paths\n");
1186                 goto err_sysctl;
1187         }
1188
1189         rdma_nl_register(RDMA_NL_IWCM, iwcm_nl_cb_table);
1190         return 0;
1191
1192 err_sysctl:
1193         destroy_workqueue(iwcm_wq);
1194 err_alloc:
1195         iwpm_exit(RDMA_NL_IWCM);
1196         return -ENOMEM;
1197 }
1198
1199 static void __exit iw_cm_cleanup(void)
1200 {
1201         rdma_nl_unregister(RDMA_NL_IWCM);
1202         unregister_net_sysctl_table(iwcm_ctl_table_hdr);
1203         destroy_workqueue(iwcm_wq);
1204         iwpm_exit(RDMA_NL_IWCM);
1205 }
1206
1207 MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_IWCM, 2);
1208
1209 module_init(iw_cm_init);
1210 module_exit(iw_cm_cleanup);