Loading...
1// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2/*
3 * Copyright (c) 2004-2007 Intel Corporation. All rights reserved.
4 * Copyright (c) 2004 Topspin Corporation. All rights reserved.
5 * Copyright (c) 2004, 2005 Voltaire Corporation. All rights reserved.
6 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
7 * Copyright (c) 2019, Mellanox Technologies inc. All rights reserved.
8 */
9
10#include <linux/completion.h>
11#include <linux/dma-mapping.h>
12#include <linux/device.h>
13#include <linux/module.h>
14#include <linux/err.h>
15#include <linux/idr.h>
16#include <linux/interrupt.h>
17#include <linux/random.h>
18#include <linux/rbtree.h>
19#include <linux/spinlock.h>
20#include <linux/slab.h>
21#include <linux/sysfs.h>
22#include <linux/workqueue.h>
23#include <linux/kdev_t.h>
24#include <linux/etherdevice.h>
25
26#include <rdma/ib_cache.h>
27#include <rdma/ib_cm.h>
28#include <rdma/ib_sysfs.h>
29#include "cm_msgs.h"
30#include "core_priv.h"
31#include "cm_trace.h"
32
33MODULE_AUTHOR("Sean Hefty");
34MODULE_DESCRIPTION("InfiniBand CM");
35MODULE_LICENSE("Dual BSD/GPL");
36
37#define CM_DESTROY_ID_WAIT_TIMEOUT 10000 /* msecs */
38#define CM_DIRECT_RETRY_CTX ((void *) 1UL)
39
40static const char * const ibcm_rej_reason_strs[] = {
41 [IB_CM_REJ_NO_QP] = "no QP",
42 [IB_CM_REJ_NO_EEC] = "no EEC",
43 [IB_CM_REJ_NO_RESOURCES] = "no resources",
44 [IB_CM_REJ_TIMEOUT] = "timeout",
45 [IB_CM_REJ_UNSUPPORTED] = "unsupported",
46 [IB_CM_REJ_INVALID_COMM_ID] = "invalid comm ID",
47 [IB_CM_REJ_INVALID_COMM_INSTANCE] = "invalid comm instance",
48 [IB_CM_REJ_INVALID_SERVICE_ID] = "invalid service ID",
49 [IB_CM_REJ_INVALID_TRANSPORT_TYPE] = "invalid transport type",
50 [IB_CM_REJ_STALE_CONN] = "stale conn",
51 [IB_CM_REJ_RDC_NOT_EXIST] = "RDC not exist",
52 [IB_CM_REJ_INVALID_GID] = "invalid GID",
53 [IB_CM_REJ_INVALID_LID] = "invalid LID",
54 [IB_CM_REJ_INVALID_SL] = "invalid SL",
55 [IB_CM_REJ_INVALID_TRAFFIC_CLASS] = "invalid traffic class",
56 [IB_CM_REJ_INVALID_HOP_LIMIT] = "invalid hop limit",
57 [IB_CM_REJ_INVALID_PACKET_RATE] = "invalid packet rate",
58 [IB_CM_REJ_INVALID_ALT_GID] = "invalid alt GID",
59 [IB_CM_REJ_INVALID_ALT_LID] = "invalid alt LID",
60 [IB_CM_REJ_INVALID_ALT_SL] = "invalid alt SL",
61 [IB_CM_REJ_INVALID_ALT_TRAFFIC_CLASS] = "invalid alt traffic class",
62 [IB_CM_REJ_INVALID_ALT_HOP_LIMIT] = "invalid alt hop limit",
63 [IB_CM_REJ_INVALID_ALT_PACKET_RATE] = "invalid alt packet rate",
64 [IB_CM_REJ_PORT_CM_REDIRECT] = "port CM redirect",
65 [IB_CM_REJ_PORT_REDIRECT] = "port redirect",
66 [IB_CM_REJ_INVALID_MTU] = "invalid MTU",
67 [IB_CM_REJ_INSUFFICIENT_RESP_RESOURCES] = "insufficient resp resources",
68 [IB_CM_REJ_CONSUMER_DEFINED] = "consumer defined",
69 [IB_CM_REJ_INVALID_RNR_RETRY] = "invalid RNR retry",
70 [IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID] = "duplicate local comm ID",
71 [IB_CM_REJ_INVALID_CLASS_VERSION] = "invalid class version",
72 [IB_CM_REJ_INVALID_FLOW_LABEL] = "invalid flow label",
73 [IB_CM_REJ_INVALID_ALT_FLOW_LABEL] = "invalid alt flow label",
74 [IB_CM_REJ_VENDOR_OPTION_NOT_SUPPORTED] =
75 "vendor option is not supported",
76};
77
78const char *__attribute_const__ ibcm_reject_msg(int reason)
79{
80 size_t index = reason;
81
82 if (index < ARRAY_SIZE(ibcm_rej_reason_strs) &&
83 ibcm_rej_reason_strs[index])
84 return ibcm_rej_reason_strs[index];
85 else
86 return "unrecognized reason";
87}
88EXPORT_SYMBOL(ibcm_reject_msg);
89
90struct cm_id_private;
91struct cm_work;
92static int cm_add_one(struct ib_device *device);
93static void cm_remove_one(struct ib_device *device, void *client_data);
94static void cm_process_work(struct cm_id_private *cm_id_priv,
95 struct cm_work *work);
96static int cm_send_sidr_rep_locked(struct cm_id_private *cm_id_priv,
97 struct ib_cm_sidr_rep_param *param);
98static void cm_issue_dreq(struct cm_id_private *cm_id_priv);
99static int cm_send_drep_locked(struct cm_id_private *cm_id_priv,
100 void *private_data, u8 private_data_len);
101static int cm_send_rej_locked(struct cm_id_private *cm_id_priv,
102 enum ib_cm_rej_reason reason, void *ari,
103 u8 ari_length, const void *private_data,
104 u8 private_data_len);
105
106static struct ib_client cm_client = {
107 .name = "cm",
108 .add = cm_add_one,
109 .remove = cm_remove_one
110};
111
112static struct ib_cm {
113 spinlock_t lock;
114 struct list_head device_list;
115 rwlock_t device_lock;
116 struct rb_root listen_service_table;
117 u64 listen_service_id;
118 /* struct rb_root peer_service_table; todo: fix peer to peer */
119 struct rb_root remote_qp_table;
120 struct rb_root remote_id_table;
121 struct rb_root remote_sidr_table;
122 struct xarray local_id_table;
123 u32 local_id_next;
124 __be32 random_id_operand;
125 struct list_head timewait_list;
126 struct workqueue_struct *wq;
127} cm;
128
129/* Counter indexes ordered by attribute ID */
130enum {
131 CM_REQ_COUNTER,
132 CM_MRA_COUNTER,
133 CM_REJ_COUNTER,
134 CM_REP_COUNTER,
135 CM_RTU_COUNTER,
136 CM_DREQ_COUNTER,
137 CM_DREP_COUNTER,
138 CM_SIDR_REQ_COUNTER,
139 CM_SIDR_REP_COUNTER,
140 CM_LAP_COUNTER,
141 CM_APR_COUNTER,
142 CM_ATTR_COUNT,
143 CM_ATTR_ID_OFFSET = 0x0010,
144};
145
146enum {
147 CM_XMIT,
148 CM_XMIT_RETRIES,
149 CM_RECV,
150 CM_RECV_DUPLICATES,
151 CM_COUNTER_GROUPS
152};
153
154struct cm_counter_attribute {
155 struct ib_port_attribute attr;
156 unsigned short group;
157 unsigned short index;
158};
159
160struct cm_port {
161 struct cm_device *cm_dev;
162 struct ib_mad_agent *mad_agent;
163 u32 port_num;
164 atomic_long_t counters[CM_COUNTER_GROUPS][CM_ATTR_COUNT];
165};
166
167struct cm_device {
168 struct kref kref;
169 struct list_head list;
170 spinlock_t mad_agent_lock;
171 struct ib_device *ib_device;
172 u8 ack_delay;
173 int going_down;
174 struct cm_port *port[];
175};
176
177struct cm_av {
178 struct cm_port *port;
179 struct rdma_ah_attr ah_attr;
180 u16 dlid_datapath;
181 u16 pkey_index;
182 u8 timeout;
183};
184
185struct cm_work {
186 struct delayed_work work;
187 struct list_head list;
188 struct cm_port *port;
189 struct ib_mad_recv_wc *mad_recv_wc; /* Received MADs */
190 __be32 local_id; /* Established / timewait */
191 __be32 remote_id;
192 struct ib_cm_event cm_event;
193 struct sa_path_rec path[];
194};
195
196struct cm_timewait_info {
197 struct cm_work work;
198 struct list_head list;
199 struct rb_node remote_qp_node;
200 struct rb_node remote_id_node;
201 __be64 remote_ca_guid;
202 __be32 remote_qpn;
203 u8 inserted_remote_qp;
204 u8 inserted_remote_id;
205};
206
207struct cm_id_private {
208 struct ib_cm_id id;
209
210 struct rb_node service_node;
211 struct rb_node sidr_id_node;
212 u32 sidr_slid;
213 spinlock_t lock; /* Do not acquire inside cm.lock */
214 struct completion comp;
215 refcount_t refcount;
216 /* Number of clients sharing this ib_cm_id. Only valid for listeners.
217 * Protected by the cm.lock spinlock.
218 */
219 int listen_sharecount;
220 struct rcu_head rcu;
221
222 struct ib_mad_send_buf *msg;
223 struct cm_timewait_info *timewait_info;
224 /* todo: use alternate port on send failure */
225 struct cm_av av;
226 struct cm_av alt_av;
227
228 void *private_data;
229 __be64 tid;
230 __be32 local_qpn;
231 __be32 remote_qpn;
232 enum ib_qp_type qp_type;
233 __be32 sq_psn;
234 __be32 rq_psn;
235 int timeout_ms;
236 enum ib_mtu path_mtu;
237 __be16 pkey;
238 u8 private_data_len;
239 u8 max_cm_retries;
240 u8 responder_resources;
241 u8 initiator_depth;
242 u8 retry_count;
243 u8 rnr_retry_count;
244 u8 service_timeout;
245 u8 target_ack_delay;
246
247 struct list_head work_list;
248 atomic_t work_count;
249
250 struct rdma_ucm_ece ece;
251};
252
253static void cm_dev_release(struct kref *kref)
254{
255 struct cm_device *cm_dev = container_of(kref, struct cm_device, kref);
256 u32 i;
257
258 rdma_for_each_port(cm_dev->ib_device, i)
259 kfree(cm_dev->port[i - 1]);
260
261 kfree(cm_dev);
262}
263
264static void cm_device_put(struct cm_device *cm_dev)
265{
266 kref_put(&cm_dev->kref, cm_dev_release);
267}
268
269static void cm_work_handler(struct work_struct *work);
270
271static inline void cm_deref_id(struct cm_id_private *cm_id_priv)
272{
273 if (refcount_dec_and_test(&cm_id_priv->refcount))
274 complete(&cm_id_priv->comp);
275}
276
277static struct ib_mad_send_buf *cm_alloc_msg(struct cm_id_private *cm_id_priv)
278{
279 struct ib_mad_agent *mad_agent;
280 struct ib_mad_send_buf *m;
281 struct ib_ah *ah;
282
283 lockdep_assert_held(&cm_id_priv->lock);
284
285 if (!cm_id_priv->av.port)
286 return ERR_PTR(-EINVAL);
287
288 spin_lock(&cm_id_priv->av.port->cm_dev->mad_agent_lock);
289 mad_agent = cm_id_priv->av.port->mad_agent;
290 if (!mad_agent) {
291 m = ERR_PTR(-EINVAL);
292 goto out;
293 }
294
295 ah = rdma_create_ah(mad_agent->qp->pd, &cm_id_priv->av.ah_attr, 0);
296 if (IS_ERR(ah)) {
297 m = ERR_CAST(ah);
298 goto out;
299 }
300
301 m = ib_create_send_mad(mad_agent, cm_id_priv->id.remote_cm_qpn,
302 cm_id_priv->av.pkey_index,
303 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
304 GFP_ATOMIC,
305 IB_MGMT_BASE_VERSION);
306 if (IS_ERR(m)) {
307 rdma_destroy_ah(ah, 0);
308 goto out;
309 }
310
311 m->ah = ah;
312
313out:
314 spin_unlock(&cm_id_priv->av.port->cm_dev->mad_agent_lock);
315 return m;
316}
317
318static void cm_free_msg(struct ib_mad_send_buf *msg)
319{
320 if (msg->ah)
321 rdma_destroy_ah(msg->ah, 0);
322 ib_free_send_mad(msg);
323}
324
325static struct ib_mad_send_buf *
326cm_alloc_priv_msg(struct cm_id_private *cm_id_priv, enum ib_cm_state state)
327{
328 struct ib_mad_send_buf *msg;
329
330 lockdep_assert_held(&cm_id_priv->lock);
331
332 msg = cm_alloc_msg(cm_id_priv);
333 if (IS_ERR(msg))
334 return msg;
335
336 cm_id_priv->msg = msg;
337 refcount_inc(&cm_id_priv->refcount);
338 msg->context[0] = cm_id_priv;
339 msg->context[1] = (void *) (unsigned long) state;
340
341 msg->retries = cm_id_priv->max_cm_retries;
342 msg->timeout_ms = cm_id_priv->timeout_ms;
343
344 return msg;
345}
346
347static void cm_free_priv_msg(struct ib_mad_send_buf *msg)
348{
349 struct cm_id_private *cm_id_priv = msg->context[0];
350
351 lockdep_assert_held(&cm_id_priv->lock);
352
353 if (!WARN_ON(cm_id_priv->msg != msg))
354 cm_id_priv->msg = NULL;
355
356 if (msg->ah)
357 rdma_destroy_ah(msg->ah, 0);
358 cm_deref_id(cm_id_priv);
359 ib_free_send_mad(msg);
360}
361
362static struct ib_mad_send_buf *
363cm_alloc_response_msg_no_ah(struct cm_port *port,
364 struct ib_mad_recv_wc *mad_recv_wc,
365 bool direct_retry)
366{
367 struct ib_mad_send_buf *m;
368
369 m = ib_create_send_mad(port->mad_agent, 1, mad_recv_wc->wc->pkey_index,
370 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
371 GFP_ATOMIC, IB_MGMT_BASE_VERSION);
372 if (!IS_ERR(m))
373 m->context[0] = direct_retry ? CM_DIRECT_RETRY_CTX : NULL;
374
375 return m;
376}
377
378static int cm_create_response_msg_ah(struct cm_port *port,
379 struct ib_mad_recv_wc *mad_recv_wc,
380 struct ib_mad_send_buf *msg)
381{
382 struct ib_ah *ah;
383
384 ah = ib_create_ah_from_wc(port->mad_agent->qp->pd, mad_recv_wc->wc,
385 mad_recv_wc->recv_buf.grh, port->port_num);
386 if (IS_ERR(ah))
387 return PTR_ERR(ah);
388
389 msg->ah = ah;
390 return 0;
391}
392
393static int cm_alloc_response_msg(struct cm_port *port,
394 struct ib_mad_recv_wc *mad_recv_wc,
395 bool direct_retry,
396 struct ib_mad_send_buf **msg)
397{
398 struct ib_mad_send_buf *m;
399 int ret;
400
401 m = cm_alloc_response_msg_no_ah(port, mad_recv_wc, direct_retry);
402 if (IS_ERR(m))
403 return PTR_ERR(m);
404
405 ret = cm_create_response_msg_ah(port, mad_recv_wc, m);
406 if (ret) {
407 ib_free_send_mad(m);
408 return ret;
409 }
410
411 *msg = m;
412 return 0;
413}
414
415static void *cm_copy_private_data(const void *private_data, u8 private_data_len)
416{
417 void *data;
418
419 if (!private_data || !private_data_len)
420 return NULL;
421
422 data = kmemdup(private_data, private_data_len, GFP_KERNEL);
423 if (!data)
424 return ERR_PTR(-ENOMEM);
425
426 return data;
427}
428
429static void cm_set_private_data(struct cm_id_private *cm_id_priv,
430 void *private_data, u8 private_data_len)
431{
432 if (cm_id_priv->private_data && cm_id_priv->private_data_len)
433 kfree(cm_id_priv->private_data);
434
435 cm_id_priv->private_data = private_data;
436 cm_id_priv->private_data_len = private_data_len;
437}
438
439static void cm_set_av_port(struct cm_av *av, struct cm_port *port)
440{
441 struct cm_port *old_port = av->port;
442
443 if (old_port == port)
444 return;
445
446 av->port = port;
447 if (old_port)
448 cm_device_put(old_port->cm_dev);
449 if (port)
450 kref_get(&port->cm_dev->kref);
451}
452
453static void cm_init_av_for_lap(struct cm_port *port, struct ib_wc *wc,
454 struct rdma_ah_attr *ah_attr, struct cm_av *av)
455{
456 cm_set_av_port(av, port);
457 av->pkey_index = wc->pkey_index;
458 rdma_move_ah_attr(&av->ah_attr, ah_attr);
459}
460
461static int cm_init_av_for_response(struct cm_port *port, struct ib_wc *wc,
462 struct ib_grh *grh, struct cm_av *av)
463{
464 cm_set_av_port(av, port);
465 av->pkey_index = wc->pkey_index;
466 return ib_init_ah_attr_from_wc(port->cm_dev->ib_device,
467 port->port_num, wc,
468 grh, &av->ah_attr);
469}
470
471static struct cm_port *
472get_cm_port_from_path(struct sa_path_rec *path, const struct ib_gid_attr *attr)
473{
474 struct cm_device *cm_dev;
475 struct cm_port *port = NULL;
476 unsigned long flags;
477
478 if (attr) {
479 read_lock_irqsave(&cm.device_lock, flags);
480 list_for_each_entry(cm_dev, &cm.device_list, list) {
481 if (cm_dev->ib_device == attr->device) {
482 port = cm_dev->port[attr->port_num - 1];
483 break;
484 }
485 }
486 read_unlock_irqrestore(&cm.device_lock, flags);
487 } else {
488 /* SGID attribute can be NULL in following
489 * conditions.
490 * (a) Alternative path
491 * (b) IB link layer without GRH
492 * (c) LAP send messages
493 */
494 read_lock_irqsave(&cm.device_lock, flags);
495 list_for_each_entry(cm_dev, &cm.device_list, list) {
496 attr = rdma_find_gid(cm_dev->ib_device,
497 &path->sgid,
498 sa_conv_pathrec_to_gid_type(path),
499 NULL);
500 if (!IS_ERR(attr)) {
501 port = cm_dev->port[attr->port_num - 1];
502 break;
503 }
504 }
505 read_unlock_irqrestore(&cm.device_lock, flags);
506 if (port)
507 rdma_put_gid_attr(attr);
508 }
509 return port;
510}
511
512static int cm_init_av_by_path(struct sa_path_rec *path,
513 const struct ib_gid_attr *sgid_attr,
514 struct cm_av *av)
515{
516 struct rdma_ah_attr new_ah_attr;
517 struct cm_device *cm_dev;
518 struct cm_port *port;
519 int ret;
520
521 port = get_cm_port_from_path(path, sgid_attr);
522 if (!port)
523 return -EINVAL;
524 cm_dev = port->cm_dev;
525
526 ret = ib_find_cached_pkey(cm_dev->ib_device, port->port_num,
527 be16_to_cpu(path->pkey), &av->pkey_index);
528 if (ret)
529 return ret;
530
531 cm_set_av_port(av, port);
532
533 /*
534 * av->ah_attr might be initialized based on wc or during
535 * request processing time which might have reference to sgid_attr.
536 * So initialize a new ah_attr on stack.
537 * If initialization fails, old ah_attr is used for sending any
538 * responses. If initialization is successful, than new ah_attr
539 * is used by overwriting the old one. So that right ah_attr
540 * can be used to return an error response.
541 */
542 ret = ib_init_ah_attr_from_path(cm_dev->ib_device, port->port_num, path,
543 &new_ah_attr, sgid_attr);
544 if (ret)
545 return ret;
546
547 av->timeout = path->packet_life_time + 1;
548 rdma_move_ah_attr(&av->ah_attr, &new_ah_attr);
549 return 0;
550}
551
552/* Move av created by cm_init_av_by_path(), so av.dgid is not moved */
553static void cm_move_av_from_path(struct cm_av *dest, struct cm_av *src)
554{
555 cm_set_av_port(dest, src->port);
556 cm_set_av_port(src, NULL);
557 dest->pkey_index = src->pkey_index;
558 rdma_move_ah_attr(&dest->ah_attr, &src->ah_attr);
559 dest->timeout = src->timeout;
560}
561
562static void cm_destroy_av(struct cm_av *av)
563{
564 rdma_destroy_ah_attr(&av->ah_attr);
565 cm_set_av_port(av, NULL);
566}
567
568static u32 cm_local_id(__be32 local_id)
569{
570 return (__force u32) (local_id ^ cm.random_id_operand);
571}
572
573static struct cm_id_private *cm_acquire_id(__be32 local_id, __be32 remote_id)
574{
575 struct cm_id_private *cm_id_priv;
576
577 rcu_read_lock();
578 cm_id_priv = xa_load(&cm.local_id_table, cm_local_id(local_id));
579 if (!cm_id_priv || cm_id_priv->id.remote_id != remote_id ||
580 !refcount_inc_not_zero(&cm_id_priv->refcount))
581 cm_id_priv = NULL;
582 rcu_read_unlock();
583
584 return cm_id_priv;
585}
586
587/*
588 * Trivial helpers to strip endian annotation and compare; the
589 * endianness doesn't actually matter since we just need a stable
590 * order for the RB tree.
591 */
592static int be32_lt(__be32 a, __be32 b)
593{
594 return (__force u32) a < (__force u32) b;
595}
596
597static int be32_gt(__be32 a, __be32 b)
598{
599 return (__force u32) a > (__force u32) b;
600}
601
602static int be64_lt(__be64 a, __be64 b)
603{
604 return (__force u64) a < (__force u64) b;
605}
606
607static int be64_gt(__be64 a, __be64 b)
608{
609 return (__force u64) a > (__force u64) b;
610}
611
612/*
613 * Inserts a new cm_id_priv into the listen_service_table. Returns cm_id_priv
614 * if the new ID was inserted, NULL if it could not be inserted due to a
615 * collision, or the existing cm_id_priv ready for shared usage.
616 */
617static struct cm_id_private *cm_insert_listen(struct cm_id_private *cm_id_priv,
618 ib_cm_handler shared_handler)
619{
620 struct rb_node **link = &cm.listen_service_table.rb_node;
621 struct rb_node *parent = NULL;
622 struct cm_id_private *cur_cm_id_priv;
623 __be64 service_id = cm_id_priv->id.service_id;
624 unsigned long flags;
625
626 spin_lock_irqsave(&cm.lock, flags);
627 while (*link) {
628 parent = *link;
629 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
630 service_node);
631
632 if (cm_id_priv->id.device < cur_cm_id_priv->id.device)
633 link = &(*link)->rb_left;
634 else if (cm_id_priv->id.device > cur_cm_id_priv->id.device)
635 link = &(*link)->rb_right;
636 else if (be64_lt(service_id, cur_cm_id_priv->id.service_id))
637 link = &(*link)->rb_left;
638 else if (be64_gt(service_id, cur_cm_id_priv->id.service_id))
639 link = &(*link)->rb_right;
640 else {
641 /*
642 * Sharing an ib_cm_id with different handlers is not
643 * supported
644 */
645 if (cur_cm_id_priv->id.cm_handler != shared_handler ||
646 cur_cm_id_priv->id.context ||
647 WARN_ON(!cur_cm_id_priv->id.cm_handler)) {
648 spin_unlock_irqrestore(&cm.lock, flags);
649 return NULL;
650 }
651 refcount_inc(&cur_cm_id_priv->refcount);
652 cur_cm_id_priv->listen_sharecount++;
653 spin_unlock_irqrestore(&cm.lock, flags);
654 return cur_cm_id_priv;
655 }
656 }
657 cm_id_priv->listen_sharecount++;
658 rb_link_node(&cm_id_priv->service_node, parent, link);
659 rb_insert_color(&cm_id_priv->service_node, &cm.listen_service_table);
660 spin_unlock_irqrestore(&cm.lock, flags);
661 return cm_id_priv;
662}
663
664static struct cm_id_private *cm_find_listen(struct ib_device *device,
665 __be64 service_id)
666{
667 struct rb_node *node = cm.listen_service_table.rb_node;
668 struct cm_id_private *cm_id_priv;
669
670 while (node) {
671 cm_id_priv = rb_entry(node, struct cm_id_private, service_node);
672
673 if (device < cm_id_priv->id.device)
674 node = node->rb_left;
675 else if (device > cm_id_priv->id.device)
676 node = node->rb_right;
677 else if (be64_lt(service_id, cm_id_priv->id.service_id))
678 node = node->rb_left;
679 else if (be64_gt(service_id, cm_id_priv->id.service_id))
680 node = node->rb_right;
681 else {
682 refcount_inc(&cm_id_priv->refcount);
683 return cm_id_priv;
684 }
685 }
686 return NULL;
687}
688
689static struct cm_timewait_info *
690cm_insert_remote_id(struct cm_timewait_info *timewait_info)
691{
692 struct rb_node **link = &cm.remote_id_table.rb_node;
693 struct rb_node *parent = NULL;
694 struct cm_timewait_info *cur_timewait_info;
695 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
696 __be32 remote_id = timewait_info->work.remote_id;
697
698 while (*link) {
699 parent = *link;
700 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
701 remote_id_node);
702 if (be32_lt(remote_id, cur_timewait_info->work.remote_id))
703 link = &(*link)->rb_left;
704 else if (be32_gt(remote_id, cur_timewait_info->work.remote_id))
705 link = &(*link)->rb_right;
706 else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
707 link = &(*link)->rb_left;
708 else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
709 link = &(*link)->rb_right;
710 else
711 return cur_timewait_info;
712 }
713 timewait_info->inserted_remote_id = 1;
714 rb_link_node(&timewait_info->remote_id_node, parent, link);
715 rb_insert_color(&timewait_info->remote_id_node, &cm.remote_id_table);
716 return NULL;
717}
718
719static struct cm_id_private *cm_find_remote_id(__be64 remote_ca_guid,
720 __be32 remote_id)
721{
722 struct rb_node *node = cm.remote_id_table.rb_node;
723 struct cm_timewait_info *timewait_info;
724 struct cm_id_private *res = NULL;
725
726 spin_lock_irq(&cm.lock);
727 while (node) {
728 timewait_info = rb_entry(node, struct cm_timewait_info,
729 remote_id_node);
730 if (be32_lt(remote_id, timewait_info->work.remote_id))
731 node = node->rb_left;
732 else if (be32_gt(remote_id, timewait_info->work.remote_id))
733 node = node->rb_right;
734 else if (be64_lt(remote_ca_guid, timewait_info->remote_ca_guid))
735 node = node->rb_left;
736 else if (be64_gt(remote_ca_guid, timewait_info->remote_ca_guid))
737 node = node->rb_right;
738 else {
739 res = cm_acquire_id(timewait_info->work.local_id,
740 timewait_info->work.remote_id);
741 break;
742 }
743 }
744 spin_unlock_irq(&cm.lock);
745 return res;
746}
747
748static struct cm_timewait_info *
749cm_insert_remote_qpn(struct cm_timewait_info *timewait_info)
750{
751 struct rb_node **link = &cm.remote_qp_table.rb_node;
752 struct rb_node *parent = NULL;
753 struct cm_timewait_info *cur_timewait_info;
754 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
755 __be32 remote_qpn = timewait_info->remote_qpn;
756
757 while (*link) {
758 parent = *link;
759 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
760 remote_qp_node);
761 if (be32_lt(remote_qpn, cur_timewait_info->remote_qpn))
762 link = &(*link)->rb_left;
763 else if (be32_gt(remote_qpn, cur_timewait_info->remote_qpn))
764 link = &(*link)->rb_right;
765 else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
766 link = &(*link)->rb_left;
767 else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
768 link = &(*link)->rb_right;
769 else
770 return cur_timewait_info;
771 }
772 timewait_info->inserted_remote_qp = 1;
773 rb_link_node(&timewait_info->remote_qp_node, parent, link);
774 rb_insert_color(&timewait_info->remote_qp_node, &cm.remote_qp_table);
775 return NULL;
776}
777
778static struct cm_id_private *
779cm_insert_remote_sidr(struct cm_id_private *cm_id_priv)
780{
781 struct rb_node **link = &cm.remote_sidr_table.rb_node;
782 struct rb_node *parent = NULL;
783 struct cm_id_private *cur_cm_id_priv;
784 __be32 remote_id = cm_id_priv->id.remote_id;
785
786 while (*link) {
787 parent = *link;
788 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
789 sidr_id_node);
790 if (be32_lt(remote_id, cur_cm_id_priv->id.remote_id))
791 link = &(*link)->rb_left;
792 else if (be32_gt(remote_id, cur_cm_id_priv->id.remote_id))
793 link = &(*link)->rb_right;
794 else {
795 if (cur_cm_id_priv->sidr_slid < cm_id_priv->sidr_slid)
796 link = &(*link)->rb_left;
797 else if (cur_cm_id_priv->sidr_slid > cm_id_priv->sidr_slid)
798 link = &(*link)->rb_right;
799 else
800 return cur_cm_id_priv;
801 }
802 }
803 rb_link_node(&cm_id_priv->sidr_id_node, parent, link);
804 rb_insert_color(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
805 return NULL;
806}
807
808static struct cm_id_private *cm_alloc_id_priv(struct ib_device *device,
809 ib_cm_handler cm_handler,
810 void *context)
811{
812 struct cm_id_private *cm_id_priv;
813 u32 id;
814 int ret;
815
816 cm_id_priv = kzalloc(sizeof *cm_id_priv, GFP_KERNEL);
817 if (!cm_id_priv)
818 return ERR_PTR(-ENOMEM);
819
820 cm_id_priv->id.state = IB_CM_IDLE;
821 cm_id_priv->id.device = device;
822 cm_id_priv->id.cm_handler = cm_handler;
823 cm_id_priv->id.context = context;
824 cm_id_priv->id.remote_cm_qpn = 1;
825
826 RB_CLEAR_NODE(&cm_id_priv->service_node);
827 RB_CLEAR_NODE(&cm_id_priv->sidr_id_node);
828 spin_lock_init(&cm_id_priv->lock);
829 init_completion(&cm_id_priv->comp);
830 INIT_LIST_HEAD(&cm_id_priv->work_list);
831 atomic_set(&cm_id_priv->work_count, -1);
832 refcount_set(&cm_id_priv->refcount, 1);
833
834 ret = xa_alloc_cyclic(&cm.local_id_table, &id, NULL, xa_limit_32b,
835 &cm.local_id_next, GFP_KERNEL);
836 if (ret < 0)
837 goto error;
838 cm_id_priv->id.local_id = (__force __be32)id ^ cm.random_id_operand;
839
840 return cm_id_priv;
841
842error:
843 kfree(cm_id_priv);
844 return ERR_PTR(ret);
845}
846
847/*
848 * Make the ID visible to the MAD handlers and other threads that use the
849 * xarray.
850 */
851static void cm_finalize_id(struct cm_id_private *cm_id_priv)
852{
853 xa_store(&cm.local_id_table, cm_local_id(cm_id_priv->id.local_id),
854 cm_id_priv, GFP_ATOMIC);
855}
856
857struct ib_cm_id *ib_create_cm_id(struct ib_device *device,
858 ib_cm_handler cm_handler,
859 void *context)
860{
861 struct cm_id_private *cm_id_priv;
862
863 cm_id_priv = cm_alloc_id_priv(device, cm_handler, context);
864 if (IS_ERR(cm_id_priv))
865 return ERR_CAST(cm_id_priv);
866
867 cm_finalize_id(cm_id_priv);
868 return &cm_id_priv->id;
869}
870EXPORT_SYMBOL(ib_create_cm_id);
871
872static struct cm_work *cm_dequeue_work(struct cm_id_private *cm_id_priv)
873{
874 struct cm_work *work;
875
876 if (list_empty(&cm_id_priv->work_list))
877 return NULL;
878
879 work = list_entry(cm_id_priv->work_list.next, struct cm_work, list);
880 list_del(&work->list);
881 return work;
882}
883
884static void cm_free_work(struct cm_work *work)
885{
886 if (work->mad_recv_wc)
887 ib_free_recv_mad(work->mad_recv_wc);
888 kfree(work);
889}
890
891static void cm_queue_work_unlock(struct cm_id_private *cm_id_priv,
892 struct cm_work *work)
893 __releases(&cm_id_priv->lock)
894{
895 bool immediate;
896
897 /*
898 * To deliver the event to the user callback we have the drop the
899 * spinlock, however, we need to ensure that the user callback is single
900 * threaded and receives events in the temporal order. If there are
901 * already events being processed then thread new events onto a list,
902 * the thread currently processing will pick them up.
903 */
904 immediate = atomic_inc_and_test(&cm_id_priv->work_count);
905 if (!immediate) {
906 list_add_tail(&work->list, &cm_id_priv->work_list);
907 /*
908 * This routine always consumes incoming reference. Once queued
909 * to the work_list then a reference is held by the thread
910 * currently running cm_process_work() and this reference is not
911 * needed.
912 */
913 cm_deref_id(cm_id_priv);
914 }
915 spin_unlock_irq(&cm_id_priv->lock);
916
917 if (immediate)
918 cm_process_work(cm_id_priv, work);
919}
920
921static inline int cm_convert_to_ms(int iba_time)
922{
923 /* approximate conversion to ms from 4.096us x 2^iba_time */
924 return 1 << max(iba_time - 8, 0);
925}
926
927/*
928 * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time
929 * Because of how ack_timeout is stored, adding one doubles the timeout.
930 * To avoid large timeouts, select the max(ack_delay, life_time + 1), and
931 * increment it (round up) only if the other is within 50%.
932 */
933static u8 cm_ack_timeout(u8 ca_ack_delay, u8 packet_life_time)
934{
935 int ack_timeout = packet_life_time + 1;
936
937 if (ack_timeout >= ca_ack_delay)
938 ack_timeout += (ca_ack_delay >= (ack_timeout - 1));
939 else
940 ack_timeout = ca_ack_delay +
941 (ack_timeout >= (ca_ack_delay - 1));
942
943 return min(31, ack_timeout);
944}
945
946static void cm_remove_remote(struct cm_id_private *cm_id_priv)
947{
948 struct cm_timewait_info *timewait_info = cm_id_priv->timewait_info;
949
950 if (timewait_info->inserted_remote_id) {
951 rb_erase(&timewait_info->remote_id_node, &cm.remote_id_table);
952 timewait_info->inserted_remote_id = 0;
953 }
954
955 if (timewait_info->inserted_remote_qp) {
956 rb_erase(&timewait_info->remote_qp_node, &cm.remote_qp_table);
957 timewait_info->inserted_remote_qp = 0;
958 }
959}
960
961static struct cm_timewait_info *cm_create_timewait_info(__be32 local_id)
962{
963 struct cm_timewait_info *timewait_info;
964
965 timewait_info = kzalloc(sizeof *timewait_info, GFP_KERNEL);
966 if (!timewait_info)
967 return ERR_PTR(-ENOMEM);
968
969 timewait_info->work.local_id = local_id;
970 INIT_DELAYED_WORK(&timewait_info->work.work, cm_work_handler);
971 timewait_info->work.cm_event.event = IB_CM_TIMEWAIT_EXIT;
972 return timewait_info;
973}
974
975static void cm_enter_timewait(struct cm_id_private *cm_id_priv)
976{
977 int wait_time;
978 unsigned long flags;
979 struct cm_device *cm_dev;
980
981 lockdep_assert_held(&cm_id_priv->lock);
982
983 cm_dev = ib_get_client_data(cm_id_priv->id.device, &cm_client);
984 if (!cm_dev)
985 return;
986
987 spin_lock_irqsave(&cm.lock, flags);
988 cm_remove_remote(cm_id_priv);
989 list_add_tail(&cm_id_priv->timewait_info->list, &cm.timewait_list);
990 spin_unlock_irqrestore(&cm.lock, flags);
991
992 /*
993 * The cm_id could be destroyed by the user before we exit timewait.
994 * To protect against this, we search for the cm_id after exiting
995 * timewait before notifying the user that we've exited timewait.
996 */
997 cm_id_priv->id.state = IB_CM_TIMEWAIT;
998 wait_time = cm_convert_to_ms(cm_id_priv->av.timeout);
999
1000 /* Check if the device started its remove_one */
1001 spin_lock_irqsave(&cm.lock, flags);
1002 if (!cm_dev->going_down)
1003 queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work,
1004 msecs_to_jiffies(wait_time));
1005 spin_unlock_irqrestore(&cm.lock, flags);
1006
1007 /*
1008 * The timewait_info is converted into a work and gets freed during
1009 * cm_free_work() in cm_timewait_handler().
1010 */
1011 BUILD_BUG_ON(offsetof(struct cm_timewait_info, work) != 0);
1012 cm_id_priv->timewait_info = NULL;
1013}
1014
1015static void cm_reset_to_idle(struct cm_id_private *cm_id_priv)
1016{
1017 unsigned long flags;
1018
1019 lockdep_assert_held(&cm_id_priv->lock);
1020
1021 cm_id_priv->id.state = IB_CM_IDLE;
1022 if (cm_id_priv->timewait_info) {
1023 spin_lock_irqsave(&cm.lock, flags);
1024 cm_remove_remote(cm_id_priv);
1025 spin_unlock_irqrestore(&cm.lock, flags);
1026 kfree(cm_id_priv->timewait_info);
1027 cm_id_priv->timewait_info = NULL;
1028 }
1029}
1030
1031static noinline void cm_destroy_id_wait_timeout(struct ib_cm_id *cm_id,
1032 enum ib_cm_state old_state)
1033{
1034 struct cm_id_private *cm_id_priv;
1035
1036 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1037 pr_err("%s: cm_id=%p timed out. state %d -> %d, refcnt=%d\n", __func__,
1038 cm_id, old_state, cm_id->state, refcount_read(&cm_id_priv->refcount));
1039}
1040
1041static void cm_destroy_id(struct ib_cm_id *cm_id, int err)
1042{
1043 struct cm_id_private *cm_id_priv;
1044 enum ib_cm_state old_state;
1045 struct cm_work *work;
1046 int ret;
1047
1048 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1049 spin_lock_irq(&cm_id_priv->lock);
1050 old_state = cm_id->state;
1051retest:
1052 switch (cm_id->state) {
1053 case IB_CM_LISTEN:
1054 spin_lock(&cm.lock);
1055 if (--cm_id_priv->listen_sharecount > 0) {
1056 /* The id is still shared. */
1057 WARN_ON(refcount_read(&cm_id_priv->refcount) == 1);
1058 spin_unlock(&cm.lock);
1059 spin_unlock_irq(&cm_id_priv->lock);
1060 cm_deref_id(cm_id_priv);
1061 return;
1062 }
1063 cm_id->state = IB_CM_IDLE;
1064 rb_erase(&cm_id_priv->service_node, &cm.listen_service_table);
1065 RB_CLEAR_NODE(&cm_id_priv->service_node);
1066 spin_unlock(&cm.lock);
1067 break;
1068 case IB_CM_SIDR_REQ_SENT:
1069 cm_id->state = IB_CM_IDLE;
1070 ib_cancel_mad(cm_id_priv->msg);
1071 break;
1072 case IB_CM_SIDR_REQ_RCVD:
1073 cm_send_sidr_rep_locked(cm_id_priv,
1074 &(struct ib_cm_sidr_rep_param){
1075 .status = IB_SIDR_REJECT });
1076 /* cm_send_sidr_rep_locked will not move to IDLE if it fails */
1077 cm_id->state = IB_CM_IDLE;
1078 break;
1079 case IB_CM_REQ_SENT:
1080 case IB_CM_MRA_REQ_RCVD:
1081 ib_cancel_mad(cm_id_priv->msg);
1082 cm_send_rej_locked(cm_id_priv, IB_CM_REJ_TIMEOUT,
1083 &cm_id_priv->id.device->node_guid,
1084 sizeof(cm_id_priv->id.device->node_guid),
1085 NULL, 0);
1086 break;
1087 case IB_CM_REQ_RCVD:
1088 if (err == -ENOMEM) {
1089 /* Do not reject to allow future retries. */
1090 cm_reset_to_idle(cm_id_priv);
1091 } else {
1092 cm_send_rej_locked(cm_id_priv,
1093 IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
1094 NULL, 0);
1095 }
1096 break;
1097 case IB_CM_REP_SENT:
1098 case IB_CM_MRA_REP_RCVD:
1099 ib_cancel_mad(cm_id_priv->msg);
1100 cm_send_rej_locked(cm_id_priv, IB_CM_REJ_CONSUMER_DEFINED, NULL,
1101 0, NULL, 0);
1102 goto retest;
1103 case IB_CM_MRA_REQ_SENT:
1104 case IB_CM_REP_RCVD:
1105 case IB_CM_MRA_REP_SENT:
1106 cm_send_rej_locked(cm_id_priv, IB_CM_REJ_CONSUMER_DEFINED, NULL,
1107 0, NULL, 0);
1108 break;
1109 case IB_CM_ESTABLISHED:
1110 if (cm_id_priv->qp_type == IB_QPT_XRC_TGT) {
1111 cm_id->state = IB_CM_IDLE;
1112 break;
1113 }
1114 cm_issue_dreq(cm_id_priv);
1115 cm_enter_timewait(cm_id_priv);
1116 goto retest;
1117 case IB_CM_DREQ_SENT:
1118 ib_cancel_mad(cm_id_priv->msg);
1119 cm_enter_timewait(cm_id_priv);
1120 goto retest;
1121 case IB_CM_DREQ_RCVD:
1122 cm_send_drep_locked(cm_id_priv, NULL, 0);
1123 WARN_ON(cm_id->state != IB_CM_TIMEWAIT);
1124 goto retest;
1125 case IB_CM_TIMEWAIT:
1126 /*
1127 * The cm_acquire_id in cm_timewait_handler will stop working
1128 * once we do xa_erase below, so just move to idle here for
1129 * consistency.
1130 */
1131 cm_id->state = IB_CM_IDLE;
1132 break;
1133 case IB_CM_IDLE:
1134 break;
1135 }
1136 WARN_ON(cm_id->state != IB_CM_IDLE);
1137
1138 spin_lock(&cm.lock);
1139 /* Required for cleanup paths related cm_req_handler() */
1140 if (cm_id_priv->timewait_info) {
1141 cm_remove_remote(cm_id_priv);
1142 kfree(cm_id_priv->timewait_info);
1143 cm_id_priv->timewait_info = NULL;
1144 }
1145
1146 WARN_ON(cm_id_priv->listen_sharecount);
1147 WARN_ON(!RB_EMPTY_NODE(&cm_id_priv->service_node));
1148 if (!RB_EMPTY_NODE(&cm_id_priv->sidr_id_node))
1149 rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
1150 spin_unlock(&cm.lock);
1151 spin_unlock_irq(&cm_id_priv->lock);
1152
1153 xa_erase(&cm.local_id_table, cm_local_id(cm_id->local_id));
1154 cm_deref_id(cm_id_priv);
1155 do {
1156 ret = wait_for_completion_timeout(&cm_id_priv->comp,
1157 msecs_to_jiffies(
1158 CM_DESTROY_ID_WAIT_TIMEOUT));
1159 if (!ret) /* timeout happened */
1160 cm_destroy_id_wait_timeout(cm_id, old_state);
1161 } while (!ret);
1162
1163 while ((work = cm_dequeue_work(cm_id_priv)) != NULL)
1164 cm_free_work(work);
1165
1166 cm_destroy_av(&cm_id_priv->av);
1167 cm_destroy_av(&cm_id_priv->alt_av);
1168 kfree(cm_id_priv->private_data);
1169 kfree_rcu(cm_id_priv, rcu);
1170}
1171
1172void ib_destroy_cm_id(struct ib_cm_id *cm_id)
1173{
1174 cm_destroy_id(cm_id, 0);
1175}
1176EXPORT_SYMBOL(ib_destroy_cm_id);
1177
1178static int cm_init_listen(struct cm_id_private *cm_id_priv, __be64 service_id)
1179{
1180 if ((service_id & IB_SERVICE_ID_AGN_MASK) == IB_CM_ASSIGN_SERVICE_ID &&
1181 (service_id != IB_CM_ASSIGN_SERVICE_ID))
1182 return -EINVAL;
1183
1184 if (service_id == IB_CM_ASSIGN_SERVICE_ID)
1185 cm_id_priv->id.service_id = cpu_to_be64(cm.listen_service_id++);
1186 else
1187 cm_id_priv->id.service_id = service_id;
1188
1189 return 0;
1190}
1191
1192/**
1193 * ib_cm_listen - Initiates listening on the specified service ID for
1194 * connection and service ID resolution requests.
1195 * @cm_id: Connection identifier associated with the listen request.
1196 * @service_id: Service identifier matched against incoming connection
1197 * and service ID resolution requests. The service ID should be specified
1198 * network-byte order. If set to IB_CM_ASSIGN_SERVICE_ID, the CM will
1199 * assign a service ID to the caller.
1200 */
1201int ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id)
1202{
1203 struct cm_id_private *cm_id_priv =
1204 container_of(cm_id, struct cm_id_private, id);
1205 unsigned long flags;
1206 int ret;
1207
1208 spin_lock_irqsave(&cm_id_priv->lock, flags);
1209 if (cm_id_priv->id.state != IB_CM_IDLE) {
1210 ret = -EINVAL;
1211 goto out;
1212 }
1213
1214 ret = cm_init_listen(cm_id_priv, service_id);
1215 if (ret)
1216 goto out;
1217
1218 if (!cm_insert_listen(cm_id_priv, NULL)) {
1219 ret = -EBUSY;
1220 goto out;
1221 }
1222
1223 cm_id_priv->id.state = IB_CM_LISTEN;
1224 ret = 0;
1225
1226out:
1227 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1228 return ret;
1229}
1230EXPORT_SYMBOL(ib_cm_listen);
1231
1232/**
1233 * ib_cm_insert_listen - Create a new listening ib_cm_id and listen on
1234 * the given service ID.
1235 *
1236 * If there's an existing ID listening on that same device and service ID,
1237 * return it.
1238 *
1239 * @device: Device associated with the cm_id. All related communication will
1240 * be associated with the specified device.
1241 * @cm_handler: Callback invoked to notify the user of CM events.
1242 * @service_id: Service identifier matched against incoming connection
1243 * and service ID resolution requests. The service ID should be specified
1244 * network-byte order. If set to IB_CM_ASSIGN_SERVICE_ID, the CM will
1245 * assign a service ID to the caller.
1246 *
1247 * Callers should call ib_destroy_cm_id when done with the listener ID.
1248 */
1249struct ib_cm_id *ib_cm_insert_listen(struct ib_device *device,
1250 ib_cm_handler cm_handler,
1251 __be64 service_id)
1252{
1253 struct cm_id_private *listen_id_priv;
1254 struct cm_id_private *cm_id_priv;
1255 int err = 0;
1256
1257 /* Create an ID in advance, since the creation may sleep */
1258 cm_id_priv = cm_alloc_id_priv(device, cm_handler, NULL);
1259 if (IS_ERR(cm_id_priv))
1260 return ERR_CAST(cm_id_priv);
1261
1262 err = cm_init_listen(cm_id_priv, service_id);
1263 if (err) {
1264 ib_destroy_cm_id(&cm_id_priv->id);
1265 return ERR_PTR(err);
1266 }
1267
1268 spin_lock_irq(&cm_id_priv->lock);
1269 listen_id_priv = cm_insert_listen(cm_id_priv, cm_handler);
1270 if (listen_id_priv != cm_id_priv) {
1271 spin_unlock_irq(&cm_id_priv->lock);
1272 ib_destroy_cm_id(&cm_id_priv->id);
1273 if (!listen_id_priv)
1274 return ERR_PTR(-EINVAL);
1275 return &listen_id_priv->id;
1276 }
1277 cm_id_priv->id.state = IB_CM_LISTEN;
1278 spin_unlock_irq(&cm_id_priv->lock);
1279
1280 /*
1281 * A listen ID does not need to be in the xarray since it does not
1282 * receive mads, is not placed in the remote_id or remote_qpn rbtree,
1283 * and does not enter timewait.
1284 */
1285
1286 return &cm_id_priv->id;
1287}
1288EXPORT_SYMBOL(ib_cm_insert_listen);
1289
1290static __be64 cm_form_tid(struct cm_id_private *cm_id_priv)
1291{
1292 u64 hi_tid = 0, low_tid;
1293
1294 lockdep_assert_held(&cm_id_priv->lock);
1295
1296 low_tid = (u64)cm_id_priv->id.local_id;
1297 if (!cm_id_priv->av.port)
1298 return cpu_to_be64(low_tid);
1299
1300 spin_lock(&cm_id_priv->av.port->cm_dev->mad_agent_lock);
1301 if (cm_id_priv->av.port->mad_agent)
1302 hi_tid = ((u64)cm_id_priv->av.port->mad_agent->hi_tid) << 32;
1303 spin_unlock(&cm_id_priv->av.port->cm_dev->mad_agent_lock);
1304 return cpu_to_be64(hi_tid | low_tid);
1305}
1306
1307static void cm_format_mad_hdr(struct ib_mad_hdr *hdr,
1308 __be16 attr_id, __be64 tid)
1309{
1310 hdr->base_version = IB_MGMT_BASE_VERSION;
1311 hdr->mgmt_class = IB_MGMT_CLASS_CM;
1312 hdr->class_version = IB_CM_CLASS_VERSION;
1313 hdr->method = IB_MGMT_METHOD_SEND;
1314 hdr->attr_id = attr_id;
1315 hdr->tid = tid;
1316}
1317
1318static void cm_format_mad_ece_hdr(struct ib_mad_hdr *hdr, __be16 attr_id,
1319 __be64 tid, u32 attr_mod)
1320{
1321 cm_format_mad_hdr(hdr, attr_id, tid);
1322 hdr->attr_mod = cpu_to_be32(attr_mod);
1323}
1324
1325static void cm_format_req(struct cm_req_msg *req_msg,
1326 struct cm_id_private *cm_id_priv,
1327 struct ib_cm_req_param *param)
1328{
1329 struct sa_path_rec *pri_path = param->primary_path;
1330 struct sa_path_rec *alt_path = param->alternate_path;
1331 bool pri_ext = false;
1332 __be16 lid;
1333
1334 if (pri_path->rec_type == SA_PATH_REC_TYPE_OPA)
1335 pri_ext = opa_is_extended_lid(pri_path->opa.dlid,
1336 pri_path->opa.slid);
1337
1338 cm_format_mad_ece_hdr(&req_msg->hdr, CM_REQ_ATTR_ID,
1339 cm_form_tid(cm_id_priv), param->ece.attr_mod);
1340
1341 IBA_SET(CM_REQ_LOCAL_COMM_ID, req_msg,
1342 be32_to_cpu(cm_id_priv->id.local_id));
1343 IBA_SET(CM_REQ_SERVICE_ID, req_msg, be64_to_cpu(param->service_id));
1344 IBA_SET(CM_REQ_LOCAL_CA_GUID, req_msg,
1345 be64_to_cpu(cm_id_priv->id.device->node_guid));
1346 IBA_SET(CM_REQ_LOCAL_QPN, req_msg, param->qp_num);
1347 IBA_SET(CM_REQ_INITIATOR_DEPTH, req_msg, param->initiator_depth);
1348 IBA_SET(CM_REQ_REMOTE_CM_RESPONSE_TIMEOUT, req_msg,
1349 param->remote_cm_response_timeout);
1350 cm_req_set_qp_type(req_msg, param->qp_type);
1351 IBA_SET(CM_REQ_END_TO_END_FLOW_CONTROL, req_msg, param->flow_control);
1352 IBA_SET(CM_REQ_STARTING_PSN, req_msg, param->starting_psn);
1353 IBA_SET(CM_REQ_LOCAL_CM_RESPONSE_TIMEOUT, req_msg,
1354 param->local_cm_response_timeout);
1355 IBA_SET(CM_REQ_PARTITION_KEY, req_msg,
1356 be16_to_cpu(param->primary_path->pkey));
1357 IBA_SET(CM_REQ_PATH_PACKET_PAYLOAD_MTU, req_msg,
1358 param->primary_path->mtu);
1359 IBA_SET(CM_REQ_MAX_CM_RETRIES, req_msg, param->max_cm_retries);
1360
1361 if (param->qp_type != IB_QPT_XRC_INI) {
1362 IBA_SET(CM_REQ_RESPONDER_RESOURCES, req_msg,
1363 param->responder_resources);
1364 IBA_SET(CM_REQ_RETRY_COUNT, req_msg, param->retry_count);
1365 IBA_SET(CM_REQ_RNR_RETRY_COUNT, req_msg,
1366 param->rnr_retry_count);
1367 IBA_SET(CM_REQ_SRQ, req_msg, param->srq);
1368 }
1369
1370 *IBA_GET_MEM_PTR(CM_REQ_PRIMARY_LOCAL_PORT_GID, req_msg) =
1371 pri_path->sgid;
1372 *IBA_GET_MEM_PTR(CM_REQ_PRIMARY_REMOTE_PORT_GID, req_msg) =
1373 pri_path->dgid;
1374 if (pri_ext) {
1375 IBA_GET_MEM_PTR(CM_REQ_PRIMARY_LOCAL_PORT_GID, req_msg)
1376 ->global.interface_id =
1377 OPA_MAKE_ID(be32_to_cpu(pri_path->opa.slid));
1378 IBA_GET_MEM_PTR(CM_REQ_PRIMARY_REMOTE_PORT_GID, req_msg)
1379 ->global.interface_id =
1380 OPA_MAKE_ID(be32_to_cpu(pri_path->opa.dlid));
1381 }
1382 if (pri_path->hop_limit <= 1) {
1383 IBA_SET(CM_REQ_PRIMARY_LOCAL_PORT_LID, req_msg,
1384 be16_to_cpu(pri_ext ? 0 :
1385 htons(ntohl(sa_path_get_slid(
1386 pri_path)))));
1387 IBA_SET(CM_REQ_PRIMARY_REMOTE_PORT_LID, req_msg,
1388 be16_to_cpu(pri_ext ? 0 :
1389 htons(ntohl(sa_path_get_dlid(
1390 pri_path)))));
1391 } else {
1392
1393 if (param->primary_path_inbound) {
1394 lid = param->primary_path_inbound->ib.dlid;
1395 IBA_SET(CM_REQ_PRIMARY_LOCAL_PORT_LID, req_msg,
1396 be16_to_cpu(lid));
1397 } else
1398 IBA_SET(CM_REQ_PRIMARY_LOCAL_PORT_LID, req_msg,
1399 be16_to_cpu(IB_LID_PERMISSIVE));
1400
1401 /* Work-around until there's a way to obtain remote LID info */
1402 IBA_SET(CM_REQ_PRIMARY_REMOTE_PORT_LID, req_msg,
1403 be16_to_cpu(IB_LID_PERMISSIVE));
1404 }
1405 IBA_SET(CM_REQ_PRIMARY_FLOW_LABEL, req_msg,
1406 be32_to_cpu(pri_path->flow_label));
1407 IBA_SET(CM_REQ_PRIMARY_PACKET_RATE, req_msg, pri_path->rate);
1408 IBA_SET(CM_REQ_PRIMARY_TRAFFIC_CLASS, req_msg, pri_path->traffic_class);
1409 IBA_SET(CM_REQ_PRIMARY_HOP_LIMIT, req_msg, pri_path->hop_limit);
1410 IBA_SET(CM_REQ_PRIMARY_SL, req_msg, pri_path->sl);
1411 IBA_SET(CM_REQ_PRIMARY_SUBNET_LOCAL, req_msg,
1412 (pri_path->hop_limit <= 1));
1413 IBA_SET(CM_REQ_PRIMARY_LOCAL_ACK_TIMEOUT, req_msg,
1414 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
1415 pri_path->packet_life_time));
1416
1417 if (alt_path) {
1418 bool alt_ext = false;
1419
1420 if (alt_path->rec_type == SA_PATH_REC_TYPE_OPA)
1421 alt_ext = opa_is_extended_lid(alt_path->opa.dlid,
1422 alt_path->opa.slid);
1423
1424 *IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_LOCAL_PORT_GID, req_msg) =
1425 alt_path->sgid;
1426 *IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_REMOTE_PORT_GID, req_msg) =
1427 alt_path->dgid;
1428 if (alt_ext) {
1429 IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_LOCAL_PORT_GID,
1430 req_msg)
1431 ->global.interface_id =
1432 OPA_MAKE_ID(be32_to_cpu(alt_path->opa.slid));
1433 IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_REMOTE_PORT_GID,
1434 req_msg)
1435 ->global.interface_id =
1436 OPA_MAKE_ID(be32_to_cpu(alt_path->opa.dlid));
1437 }
1438 if (alt_path->hop_limit <= 1) {
1439 IBA_SET(CM_REQ_ALTERNATE_LOCAL_PORT_LID, req_msg,
1440 be16_to_cpu(
1441 alt_ext ? 0 :
1442 htons(ntohl(sa_path_get_slid(
1443 alt_path)))));
1444 IBA_SET(CM_REQ_ALTERNATE_REMOTE_PORT_LID, req_msg,
1445 be16_to_cpu(
1446 alt_ext ? 0 :
1447 htons(ntohl(sa_path_get_dlid(
1448 alt_path)))));
1449 } else {
1450 IBA_SET(CM_REQ_ALTERNATE_LOCAL_PORT_LID, req_msg,
1451 be16_to_cpu(IB_LID_PERMISSIVE));
1452 IBA_SET(CM_REQ_ALTERNATE_REMOTE_PORT_LID, req_msg,
1453 be16_to_cpu(IB_LID_PERMISSIVE));
1454 }
1455 IBA_SET(CM_REQ_ALTERNATE_FLOW_LABEL, req_msg,
1456 be32_to_cpu(alt_path->flow_label));
1457 IBA_SET(CM_REQ_ALTERNATE_PACKET_RATE, req_msg, alt_path->rate);
1458 IBA_SET(CM_REQ_ALTERNATE_TRAFFIC_CLASS, req_msg,
1459 alt_path->traffic_class);
1460 IBA_SET(CM_REQ_ALTERNATE_HOP_LIMIT, req_msg,
1461 alt_path->hop_limit);
1462 IBA_SET(CM_REQ_ALTERNATE_SL, req_msg, alt_path->sl);
1463 IBA_SET(CM_REQ_ALTERNATE_SUBNET_LOCAL, req_msg,
1464 (alt_path->hop_limit <= 1));
1465 IBA_SET(CM_REQ_ALTERNATE_LOCAL_ACK_TIMEOUT, req_msg,
1466 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
1467 alt_path->packet_life_time));
1468 }
1469 IBA_SET(CM_REQ_VENDOR_ID, req_msg, param->ece.vendor_id);
1470
1471 if (param->private_data && param->private_data_len)
1472 IBA_SET_MEM(CM_REQ_PRIVATE_DATA, req_msg, param->private_data,
1473 param->private_data_len);
1474}
1475
1476static int cm_validate_req_param(struct ib_cm_req_param *param)
1477{
1478 if (!param->primary_path)
1479 return -EINVAL;
1480
1481 if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC &&
1482 param->qp_type != IB_QPT_XRC_INI)
1483 return -EINVAL;
1484
1485 if (param->private_data &&
1486 param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE)
1487 return -EINVAL;
1488
1489 if (param->alternate_path &&
1490 (param->alternate_path->pkey != param->primary_path->pkey ||
1491 param->alternate_path->mtu != param->primary_path->mtu))
1492 return -EINVAL;
1493
1494 return 0;
1495}
1496
1497int ib_send_cm_req(struct ib_cm_id *cm_id,
1498 struct ib_cm_req_param *param)
1499{
1500 struct cm_av av = {}, alt_av = {};
1501 struct cm_id_private *cm_id_priv;
1502 struct ib_mad_send_buf *msg;
1503 struct cm_req_msg *req_msg;
1504 unsigned long flags;
1505 int ret;
1506
1507 ret = cm_validate_req_param(param);
1508 if (ret)
1509 return ret;
1510
1511 /* Verify that we're not in timewait. */
1512 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1513 spin_lock_irqsave(&cm_id_priv->lock, flags);
1514 if (cm_id->state != IB_CM_IDLE || WARN_ON(cm_id_priv->timewait_info)) {
1515 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1516 return -EINVAL;
1517 }
1518 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1519
1520 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1521 id.local_id);
1522 if (IS_ERR(cm_id_priv->timewait_info)) {
1523 ret = PTR_ERR(cm_id_priv->timewait_info);
1524 cm_id_priv->timewait_info = NULL;
1525 return ret;
1526 }
1527
1528 ret = cm_init_av_by_path(param->primary_path,
1529 param->ppath_sgid_attr, &av);
1530 if (ret)
1531 return ret;
1532 if (param->alternate_path) {
1533 ret = cm_init_av_by_path(param->alternate_path, NULL,
1534 &alt_av);
1535 if (ret) {
1536 cm_destroy_av(&av);
1537 return ret;
1538 }
1539 }
1540 cm_id->service_id = param->service_id;
1541 cm_id_priv->timeout_ms = cm_convert_to_ms(
1542 param->primary_path->packet_life_time) * 2 +
1543 cm_convert_to_ms(
1544 param->remote_cm_response_timeout);
1545 cm_id_priv->max_cm_retries = param->max_cm_retries;
1546 cm_id_priv->initiator_depth = param->initiator_depth;
1547 cm_id_priv->responder_resources = param->responder_resources;
1548 cm_id_priv->retry_count = param->retry_count;
1549 cm_id_priv->path_mtu = param->primary_path->mtu;
1550 cm_id_priv->pkey = param->primary_path->pkey;
1551 cm_id_priv->qp_type = param->qp_type;
1552
1553 spin_lock_irqsave(&cm_id_priv->lock, flags);
1554
1555 cm_move_av_from_path(&cm_id_priv->av, &av);
1556 if (param->primary_path_outbound)
1557 cm_id_priv->av.dlid_datapath =
1558 be16_to_cpu(param->primary_path_outbound->ib.dlid);
1559
1560 if (param->alternate_path)
1561 cm_move_av_from_path(&cm_id_priv->alt_av, &alt_av);
1562
1563 msg = cm_alloc_priv_msg(cm_id_priv, IB_CM_REQ_SENT);
1564 if (IS_ERR(msg)) {
1565 ret = PTR_ERR(msg);
1566 goto out_unlock;
1567 }
1568
1569 req_msg = (struct cm_req_msg *)msg->mad;
1570 cm_format_req(req_msg, cm_id_priv, param);
1571 cm_id_priv->tid = req_msg->hdr.tid;
1572
1573 cm_id_priv->local_qpn = cpu_to_be32(IBA_GET(CM_REQ_LOCAL_QPN, req_msg));
1574 cm_id_priv->rq_psn = cpu_to_be32(IBA_GET(CM_REQ_STARTING_PSN, req_msg));
1575
1576 trace_icm_send_req(&cm_id_priv->id);
1577 ret = ib_post_send_mad(msg, NULL);
1578 if (ret)
1579 goto out_free;
1580 BUG_ON(cm_id->state != IB_CM_IDLE);
1581 cm_id->state = IB_CM_REQ_SENT;
1582 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1583 return 0;
1584out_free:
1585 cm_free_priv_msg(msg);
1586out_unlock:
1587 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1588 return ret;
1589}
1590EXPORT_SYMBOL(ib_send_cm_req);
1591
1592static int cm_issue_rej(struct cm_port *port,
1593 struct ib_mad_recv_wc *mad_recv_wc,
1594 enum ib_cm_rej_reason reason,
1595 enum cm_msg_response msg_rejected,
1596 void *ari, u8 ari_length)
1597{
1598 struct ib_mad_send_buf *msg = NULL;
1599 struct cm_rej_msg *rej_msg, *rcv_msg;
1600 int ret;
1601
1602 ret = cm_alloc_response_msg(port, mad_recv_wc, false, &msg);
1603 if (ret)
1604 return ret;
1605
1606 /* We just need common CM header information. Cast to any message. */
1607 rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad;
1608 rej_msg = (struct cm_rej_msg *) msg->mad;
1609
1610 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid);
1611 IBA_SET(CM_REJ_REMOTE_COMM_ID, rej_msg,
1612 IBA_GET(CM_REJ_LOCAL_COMM_ID, rcv_msg));
1613 IBA_SET(CM_REJ_LOCAL_COMM_ID, rej_msg,
1614 IBA_GET(CM_REJ_REMOTE_COMM_ID, rcv_msg));
1615 IBA_SET(CM_REJ_MESSAGE_REJECTED, rej_msg, msg_rejected);
1616 IBA_SET(CM_REJ_REASON, rej_msg, reason);
1617
1618 if (ari && ari_length) {
1619 IBA_SET(CM_REJ_REJECTED_INFO_LENGTH, rej_msg, ari_length);
1620 IBA_SET_MEM(CM_REJ_ARI, rej_msg, ari, ari_length);
1621 }
1622
1623 trace_icm_issue_rej(
1624 IBA_GET(CM_REJ_LOCAL_COMM_ID, rcv_msg),
1625 IBA_GET(CM_REJ_REMOTE_COMM_ID, rcv_msg));
1626 ret = ib_post_send_mad(msg, NULL);
1627 if (ret)
1628 cm_free_msg(msg);
1629
1630 return ret;
1631}
1632
1633static bool cm_req_has_alt_path(struct cm_req_msg *req_msg)
1634{
1635 return ((cpu_to_be16(
1636 IBA_GET(CM_REQ_ALTERNATE_LOCAL_PORT_LID, req_msg))) ||
1637 (ib_is_opa_gid(IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_LOCAL_PORT_GID,
1638 req_msg))));
1639}
1640
1641static void cm_path_set_rec_type(struct ib_device *ib_device, u32 port_num,
1642 struct sa_path_rec *path, union ib_gid *gid)
1643{
1644 if (ib_is_opa_gid(gid) && rdma_cap_opa_ah(ib_device, port_num))
1645 path->rec_type = SA_PATH_REC_TYPE_OPA;
1646 else
1647 path->rec_type = SA_PATH_REC_TYPE_IB;
1648}
1649
1650static void cm_format_path_lid_from_req(struct cm_req_msg *req_msg,
1651 struct sa_path_rec *primary_path,
1652 struct sa_path_rec *alt_path,
1653 struct ib_wc *wc)
1654{
1655 u32 lid;
1656
1657 if (primary_path->rec_type != SA_PATH_REC_TYPE_OPA) {
1658 sa_path_set_dlid(primary_path, wc->slid);
1659 sa_path_set_slid(primary_path,
1660 IBA_GET(CM_REQ_PRIMARY_REMOTE_PORT_LID,
1661 req_msg));
1662 } else {
1663 lid = opa_get_lid_from_gid(IBA_GET_MEM_PTR(
1664 CM_REQ_PRIMARY_LOCAL_PORT_GID, req_msg));
1665 sa_path_set_dlid(primary_path, lid);
1666
1667 lid = opa_get_lid_from_gid(IBA_GET_MEM_PTR(
1668 CM_REQ_PRIMARY_REMOTE_PORT_GID, req_msg));
1669 sa_path_set_slid(primary_path, lid);
1670 }
1671
1672 if (!cm_req_has_alt_path(req_msg))
1673 return;
1674
1675 if (alt_path->rec_type != SA_PATH_REC_TYPE_OPA) {
1676 sa_path_set_dlid(alt_path,
1677 IBA_GET(CM_REQ_ALTERNATE_LOCAL_PORT_LID,
1678 req_msg));
1679 sa_path_set_slid(alt_path,
1680 IBA_GET(CM_REQ_ALTERNATE_REMOTE_PORT_LID,
1681 req_msg));
1682 } else {
1683 lid = opa_get_lid_from_gid(IBA_GET_MEM_PTR(
1684 CM_REQ_ALTERNATE_LOCAL_PORT_GID, req_msg));
1685 sa_path_set_dlid(alt_path, lid);
1686
1687 lid = opa_get_lid_from_gid(IBA_GET_MEM_PTR(
1688 CM_REQ_ALTERNATE_REMOTE_PORT_GID, req_msg));
1689 sa_path_set_slid(alt_path, lid);
1690 }
1691}
1692
1693static void cm_format_paths_from_req(struct cm_req_msg *req_msg,
1694 struct sa_path_rec *primary_path,
1695 struct sa_path_rec *alt_path,
1696 struct ib_wc *wc)
1697{
1698 primary_path->dgid =
1699 *IBA_GET_MEM_PTR(CM_REQ_PRIMARY_LOCAL_PORT_GID, req_msg);
1700 primary_path->sgid =
1701 *IBA_GET_MEM_PTR(CM_REQ_PRIMARY_REMOTE_PORT_GID, req_msg);
1702 primary_path->flow_label =
1703 cpu_to_be32(IBA_GET(CM_REQ_PRIMARY_FLOW_LABEL, req_msg));
1704 primary_path->hop_limit = IBA_GET(CM_REQ_PRIMARY_HOP_LIMIT, req_msg);
1705 primary_path->traffic_class =
1706 IBA_GET(CM_REQ_PRIMARY_TRAFFIC_CLASS, req_msg);
1707 primary_path->reversible = 1;
1708 primary_path->pkey =
1709 cpu_to_be16(IBA_GET(CM_REQ_PARTITION_KEY, req_msg));
1710 primary_path->sl = IBA_GET(CM_REQ_PRIMARY_SL, req_msg);
1711 primary_path->mtu_selector = IB_SA_EQ;
1712 primary_path->mtu = IBA_GET(CM_REQ_PATH_PACKET_PAYLOAD_MTU, req_msg);
1713 primary_path->rate_selector = IB_SA_EQ;
1714 primary_path->rate = IBA_GET(CM_REQ_PRIMARY_PACKET_RATE, req_msg);
1715 primary_path->packet_life_time_selector = IB_SA_EQ;
1716 primary_path->packet_life_time =
1717 IBA_GET(CM_REQ_PRIMARY_LOCAL_ACK_TIMEOUT, req_msg);
1718 primary_path->packet_life_time -= (primary_path->packet_life_time > 0);
1719 primary_path->service_id =
1720 cpu_to_be64(IBA_GET(CM_REQ_SERVICE_ID, req_msg));
1721 if (sa_path_is_roce(primary_path))
1722 primary_path->roce.route_resolved = false;
1723
1724 if (cm_req_has_alt_path(req_msg)) {
1725 alt_path->dgid = *IBA_GET_MEM_PTR(
1726 CM_REQ_ALTERNATE_LOCAL_PORT_GID, req_msg);
1727 alt_path->sgid = *IBA_GET_MEM_PTR(
1728 CM_REQ_ALTERNATE_REMOTE_PORT_GID, req_msg);
1729 alt_path->flow_label = cpu_to_be32(
1730 IBA_GET(CM_REQ_ALTERNATE_FLOW_LABEL, req_msg));
1731 alt_path->hop_limit =
1732 IBA_GET(CM_REQ_ALTERNATE_HOP_LIMIT, req_msg);
1733 alt_path->traffic_class =
1734 IBA_GET(CM_REQ_ALTERNATE_TRAFFIC_CLASS, req_msg);
1735 alt_path->reversible = 1;
1736 alt_path->pkey =
1737 cpu_to_be16(IBA_GET(CM_REQ_PARTITION_KEY, req_msg));
1738 alt_path->sl = IBA_GET(CM_REQ_ALTERNATE_SL, req_msg);
1739 alt_path->mtu_selector = IB_SA_EQ;
1740 alt_path->mtu =
1741 IBA_GET(CM_REQ_PATH_PACKET_PAYLOAD_MTU, req_msg);
1742 alt_path->rate_selector = IB_SA_EQ;
1743 alt_path->rate = IBA_GET(CM_REQ_ALTERNATE_PACKET_RATE, req_msg);
1744 alt_path->packet_life_time_selector = IB_SA_EQ;
1745 alt_path->packet_life_time =
1746 IBA_GET(CM_REQ_ALTERNATE_LOCAL_ACK_TIMEOUT, req_msg);
1747 alt_path->packet_life_time -= (alt_path->packet_life_time > 0);
1748 alt_path->service_id =
1749 cpu_to_be64(IBA_GET(CM_REQ_SERVICE_ID, req_msg));
1750
1751 if (sa_path_is_roce(alt_path))
1752 alt_path->roce.route_resolved = false;
1753 }
1754 cm_format_path_lid_from_req(req_msg, primary_path, alt_path, wc);
1755}
1756
1757static u16 cm_get_bth_pkey(struct cm_work *work)
1758{
1759 struct ib_device *ib_dev = work->port->cm_dev->ib_device;
1760 u32 port_num = work->port->port_num;
1761 u16 pkey_index = work->mad_recv_wc->wc->pkey_index;
1762 u16 pkey;
1763 int ret;
1764
1765 ret = ib_get_cached_pkey(ib_dev, port_num, pkey_index, &pkey);
1766 if (ret) {
1767 dev_warn_ratelimited(&ib_dev->dev, "ib_cm: Couldn't retrieve pkey for incoming request (port %u, pkey index %u). %d\n",
1768 port_num, pkey_index, ret);
1769 return 0;
1770 }
1771
1772 return pkey;
1773}
1774
1775/**
1776 * cm_opa_to_ib_sgid - Convert OPA SGID to IB SGID
1777 * ULPs (such as IPoIB) do not understand OPA GIDs and will
1778 * reject them as the local_gid will not match the sgid. Therefore,
1779 * change the pathrec's SGID to an IB SGID.
1780 *
1781 * @work: Work completion
1782 * @path: Path record
1783 */
1784static void cm_opa_to_ib_sgid(struct cm_work *work,
1785 struct sa_path_rec *path)
1786{
1787 struct ib_device *dev = work->port->cm_dev->ib_device;
1788 u32 port_num = work->port->port_num;
1789
1790 if (rdma_cap_opa_ah(dev, port_num) &&
1791 (ib_is_opa_gid(&path->sgid))) {
1792 union ib_gid sgid;
1793
1794 if (rdma_query_gid(dev, port_num, 0, &sgid)) {
1795 dev_warn(&dev->dev,
1796 "Error updating sgid in CM request\n");
1797 return;
1798 }
1799
1800 path->sgid = sgid;
1801 }
1802}
1803
1804static void cm_format_req_event(struct cm_work *work,
1805 struct cm_id_private *cm_id_priv,
1806 struct ib_cm_id *listen_id)
1807{
1808 struct cm_req_msg *req_msg;
1809 struct ib_cm_req_event_param *param;
1810
1811 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1812 param = &work->cm_event.param.req_rcvd;
1813 param->listen_id = listen_id;
1814 param->bth_pkey = cm_get_bth_pkey(work);
1815 param->port = cm_id_priv->av.port->port_num;
1816 param->primary_path = &work->path[0];
1817 cm_opa_to_ib_sgid(work, param->primary_path);
1818 if (cm_req_has_alt_path(req_msg)) {
1819 param->alternate_path = &work->path[1];
1820 cm_opa_to_ib_sgid(work, param->alternate_path);
1821 } else {
1822 param->alternate_path = NULL;
1823 }
1824 param->remote_ca_guid =
1825 cpu_to_be64(IBA_GET(CM_REQ_LOCAL_CA_GUID, req_msg));
1826 param->remote_qkey = IBA_GET(CM_REQ_LOCAL_Q_KEY, req_msg);
1827 param->remote_qpn = IBA_GET(CM_REQ_LOCAL_QPN, req_msg);
1828 param->qp_type = cm_req_get_qp_type(req_msg);
1829 param->starting_psn = IBA_GET(CM_REQ_STARTING_PSN, req_msg);
1830 param->responder_resources = IBA_GET(CM_REQ_INITIATOR_DEPTH, req_msg);
1831 param->initiator_depth = IBA_GET(CM_REQ_RESPONDER_RESOURCES, req_msg);
1832 param->local_cm_response_timeout =
1833 IBA_GET(CM_REQ_REMOTE_CM_RESPONSE_TIMEOUT, req_msg);
1834 param->flow_control = IBA_GET(CM_REQ_END_TO_END_FLOW_CONTROL, req_msg);
1835 param->remote_cm_response_timeout =
1836 IBA_GET(CM_REQ_LOCAL_CM_RESPONSE_TIMEOUT, req_msg);
1837 param->retry_count = IBA_GET(CM_REQ_RETRY_COUNT, req_msg);
1838 param->rnr_retry_count = IBA_GET(CM_REQ_RNR_RETRY_COUNT, req_msg);
1839 param->srq = IBA_GET(CM_REQ_SRQ, req_msg);
1840 param->ppath_sgid_attr = cm_id_priv->av.ah_attr.grh.sgid_attr;
1841 param->ece.vendor_id = IBA_GET(CM_REQ_VENDOR_ID, req_msg);
1842 param->ece.attr_mod = be32_to_cpu(req_msg->hdr.attr_mod);
1843
1844 work->cm_event.private_data =
1845 IBA_GET_MEM_PTR(CM_REQ_PRIVATE_DATA, req_msg);
1846}
1847
1848static void cm_process_work(struct cm_id_private *cm_id_priv,
1849 struct cm_work *work)
1850{
1851 int ret;
1852
1853 /* We will typically only have the current event to report. */
1854 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
1855 cm_free_work(work);
1856
1857 while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) {
1858 spin_lock_irq(&cm_id_priv->lock);
1859 work = cm_dequeue_work(cm_id_priv);
1860 spin_unlock_irq(&cm_id_priv->lock);
1861 if (!work)
1862 return;
1863
1864 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id,
1865 &work->cm_event);
1866 cm_free_work(work);
1867 }
1868 cm_deref_id(cm_id_priv);
1869 if (ret)
1870 cm_destroy_id(&cm_id_priv->id, ret);
1871}
1872
1873static void cm_format_mra(struct cm_mra_msg *mra_msg,
1874 struct cm_id_private *cm_id_priv,
1875 enum cm_msg_response msg_mraed, u8 service_timeout,
1876 const void *private_data, u8 private_data_len)
1877{
1878 cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid);
1879 IBA_SET(CM_MRA_MESSAGE_MRAED, mra_msg, msg_mraed);
1880 IBA_SET(CM_MRA_LOCAL_COMM_ID, mra_msg,
1881 be32_to_cpu(cm_id_priv->id.local_id));
1882 IBA_SET(CM_MRA_REMOTE_COMM_ID, mra_msg,
1883 be32_to_cpu(cm_id_priv->id.remote_id));
1884 IBA_SET(CM_MRA_SERVICE_TIMEOUT, mra_msg, service_timeout);
1885
1886 if (private_data && private_data_len)
1887 IBA_SET_MEM(CM_MRA_PRIVATE_DATA, mra_msg, private_data,
1888 private_data_len);
1889}
1890
1891static void cm_format_rej(struct cm_rej_msg *rej_msg,
1892 struct cm_id_private *cm_id_priv,
1893 enum ib_cm_rej_reason reason, void *ari,
1894 u8 ari_length, const void *private_data,
1895 u8 private_data_len, enum ib_cm_state state)
1896{
1897 lockdep_assert_held(&cm_id_priv->lock);
1898
1899 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid);
1900 IBA_SET(CM_REJ_REMOTE_COMM_ID, rej_msg,
1901 be32_to_cpu(cm_id_priv->id.remote_id));
1902
1903 switch (state) {
1904 case IB_CM_REQ_RCVD:
1905 IBA_SET(CM_REJ_LOCAL_COMM_ID, rej_msg, be32_to_cpu(0));
1906 IBA_SET(CM_REJ_MESSAGE_REJECTED, rej_msg, CM_MSG_RESPONSE_REQ);
1907 break;
1908 case IB_CM_MRA_REQ_SENT:
1909 IBA_SET(CM_REJ_LOCAL_COMM_ID, rej_msg,
1910 be32_to_cpu(cm_id_priv->id.local_id));
1911 IBA_SET(CM_REJ_MESSAGE_REJECTED, rej_msg, CM_MSG_RESPONSE_REQ);
1912 break;
1913 case IB_CM_REP_RCVD:
1914 case IB_CM_MRA_REP_SENT:
1915 IBA_SET(CM_REJ_LOCAL_COMM_ID, rej_msg,
1916 be32_to_cpu(cm_id_priv->id.local_id));
1917 IBA_SET(CM_REJ_MESSAGE_REJECTED, rej_msg, CM_MSG_RESPONSE_REP);
1918 break;
1919 default:
1920 IBA_SET(CM_REJ_LOCAL_COMM_ID, rej_msg,
1921 be32_to_cpu(cm_id_priv->id.local_id));
1922 IBA_SET(CM_REJ_MESSAGE_REJECTED, rej_msg,
1923 CM_MSG_RESPONSE_OTHER);
1924 break;
1925 }
1926
1927 IBA_SET(CM_REJ_REASON, rej_msg, reason);
1928 if (ari && ari_length) {
1929 IBA_SET(CM_REJ_REJECTED_INFO_LENGTH, rej_msg, ari_length);
1930 IBA_SET_MEM(CM_REJ_ARI, rej_msg, ari, ari_length);
1931 }
1932
1933 if (private_data && private_data_len)
1934 IBA_SET_MEM(CM_REJ_PRIVATE_DATA, rej_msg, private_data,
1935 private_data_len);
1936}
1937
1938static void cm_dup_req_handler(struct cm_work *work,
1939 struct cm_id_private *cm_id_priv)
1940{
1941 struct ib_mad_send_buf *msg = NULL;
1942 int ret;
1943
1944 atomic_long_inc(
1945 &work->port->counters[CM_RECV_DUPLICATES][CM_REQ_COUNTER]);
1946
1947 /* Quick state check to discard duplicate REQs. */
1948 spin_lock_irq(&cm_id_priv->lock);
1949 if (cm_id_priv->id.state == IB_CM_REQ_RCVD) {
1950 spin_unlock_irq(&cm_id_priv->lock);
1951 return;
1952 }
1953 spin_unlock_irq(&cm_id_priv->lock);
1954
1955 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, true, &msg);
1956 if (ret)
1957 return;
1958
1959 spin_lock_irq(&cm_id_priv->lock);
1960 switch (cm_id_priv->id.state) {
1961 case IB_CM_MRA_REQ_SENT:
1962 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1963 CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout,
1964 cm_id_priv->private_data,
1965 cm_id_priv->private_data_len);
1966 break;
1967 case IB_CM_TIMEWAIT:
1968 cm_format_rej((struct cm_rej_msg *)msg->mad, cm_id_priv,
1969 IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0,
1970 IB_CM_TIMEWAIT);
1971 break;
1972 default:
1973 goto unlock;
1974 }
1975 spin_unlock_irq(&cm_id_priv->lock);
1976
1977 trace_icm_send_dup_req(&cm_id_priv->id);
1978 ret = ib_post_send_mad(msg, NULL);
1979 if (ret)
1980 goto free;
1981 return;
1982
1983unlock: spin_unlock_irq(&cm_id_priv->lock);
1984free: cm_free_msg(msg);
1985}
1986
1987static struct cm_id_private *cm_match_req(struct cm_work *work,
1988 struct cm_id_private *cm_id_priv)
1989{
1990 struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv;
1991 struct cm_timewait_info *timewait_info;
1992 struct cm_req_msg *req_msg;
1993
1994 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1995
1996 /* Check for possible duplicate REQ. */
1997 spin_lock_irq(&cm.lock);
1998 timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info);
1999 if (timewait_info) {
2000 cur_cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
2001 timewait_info->work.remote_id);
2002 spin_unlock_irq(&cm.lock);
2003 if (cur_cm_id_priv) {
2004 cm_dup_req_handler(work, cur_cm_id_priv);
2005 cm_deref_id(cur_cm_id_priv);
2006 }
2007 return NULL;
2008 }
2009
2010 /* Check for stale connections. */
2011 timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
2012 if (timewait_info) {
2013 cm_remove_remote(cm_id_priv);
2014 cur_cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
2015 timewait_info->work.remote_id);
2016
2017 spin_unlock_irq(&cm.lock);
2018 cm_issue_rej(work->port, work->mad_recv_wc,
2019 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ,
2020 NULL, 0);
2021 if (cur_cm_id_priv) {
2022 ib_send_cm_dreq(&cur_cm_id_priv->id, NULL, 0);
2023 cm_deref_id(cur_cm_id_priv);
2024 }
2025 return NULL;
2026 }
2027
2028 /* Find matching listen request. */
2029 listen_cm_id_priv = cm_find_listen(
2030 cm_id_priv->id.device,
2031 cpu_to_be64(IBA_GET(CM_REQ_SERVICE_ID, req_msg)));
2032 if (!listen_cm_id_priv) {
2033 cm_remove_remote(cm_id_priv);
2034 spin_unlock_irq(&cm.lock);
2035 cm_issue_rej(work->port, work->mad_recv_wc,
2036 IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ,
2037 NULL, 0);
2038 return NULL;
2039 }
2040 spin_unlock_irq(&cm.lock);
2041 return listen_cm_id_priv;
2042}
2043
2044/*
2045 * Work-around for inter-subnet connections. If the LIDs are permissive,
2046 * we need to override the LID/SL data in the REQ with the LID information
2047 * in the work completion.
2048 */
2049static void cm_process_routed_req(struct cm_req_msg *req_msg, struct ib_wc *wc)
2050{
2051 if (!IBA_GET(CM_REQ_PRIMARY_SUBNET_LOCAL, req_msg)) {
2052 if (cpu_to_be16(IBA_GET(CM_REQ_PRIMARY_LOCAL_PORT_LID,
2053 req_msg)) == IB_LID_PERMISSIVE) {
2054 IBA_SET(CM_REQ_PRIMARY_LOCAL_PORT_LID, req_msg,
2055 be16_to_cpu(ib_lid_be16(wc->slid)));
2056 IBA_SET(CM_REQ_PRIMARY_SL, req_msg, wc->sl);
2057 }
2058
2059 if (cpu_to_be16(IBA_GET(CM_REQ_PRIMARY_REMOTE_PORT_LID,
2060 req_msg)) == IB_LID_PERMISSIVE)
2061 IBA_SET(CM_REQ_PRIMARY_REMOTE_PORT_LID, req_msg,
2062 wc->dlid_path_bits);
2063 }
2064
2065 if (!IBA_GET(CM_REQ_ALTERNATE_SUBNET_LOCAL, req_msg)) {
2066 if (cpu_to_be16(IBA_GET(CM_REQ_ALTERNATE_LOCAL_PORT_LID,
2067 req_msg)) == IB_LID_PERMISSIVE) {
2068 IBA_SET(CM_REQ_ALTERNATE_LOCAL_PORT_LID, req_msg,
2069 be16_to_cpu(ib_lid_be16(wc->slid)));
2070 IBA_SET(CM_REQ_ALTERNATE_SL, req_msg, wc->sl);
2071 }
2072
2073 if (cpu_to_be16(IBA_GET(CM_REQ_ALTERNATE_REMOTE_PORT_LID,
2074 req_msg)) == IB_LID_PERMISSIVE)
2075 IBA_SET(CM_REQ_ALTERNATE_REMOTE_PORT_LID, req_msg,
2076 wc->dlid_path_bits);
2077 }
2078}
2079
2080static int cm_req_handler(struct cm_work *work)
2081{
2082 struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
2083 struct cm_req_msg *req_msg;
2084 const struct ib_global_route *grh;
2085 const struct ib_gid_attr *gid_attr;
2086 int ret;
2087
2088 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
2089
2090 cm_id_priv =
2091 cm_alloc_id_priv(work->port->cm_dev->ib_device, NULL, NULL);
2092 if (IS_ERR(cm_id_priv))
2093 return PTR_ERR(cm_id_priv);
2094
2095 cm_id_priv->id.remote_id =
2096 cpu_to_be32(IBA_GET(CM_REQ_LOCAL_COMM_ID, req_msg));
2097 cm_id_priv->id.service_id =
2098 cpu_to_be64(IBA_GET(CM_REQ_SERVICE_ID, req_msg));
2099 cm_id_priv->tid = req_msg->hdr.tid;
2100 cm_id_priv->timeout_ms = cm_convert_to_ms(
2101 IBA_GET(CM_REQ_LOCAL_CM_RESPONSE_TIMEOUT, req_msg));
2102 cm_id_priv->max_cm_retries = IBA_GET(CM_REQ_MAX_CM_RETRIES, req_msg);
2103 cm_id_priv->remote_qpn =
2104 cpu_to_be32(IBA_GET(CM_REQ_LOCAL_QPN, req_msg));
2105 cm_id_priv->initiator_depth =
2106 IBA_GET(CM_REQ_RESPONDER_RESOURCES, req_msg);
2107 cm_id_priv->responder_resources =
2108 IBA_GET(CM_REQ_INITIATOR_DEPTH, req_msg);
2109 cm_id_priv->path_mtu = IBA_GET(CM_REQ_PATH_PACKET_PAYLOAD_MTU, req_msg);
2110 cm_id_priv->pkey = cpu_to_be16(IBA_GET(CM_REQ_PARTITION_KEY, req_msg));
2111 cm_id_priv->sq_psn = cpu_to_be32(IBA_GET(CM_REQ_STARTING_PSN, req_msg));
2112 cm_id_priv->retry_count = IBA_GET(CM_REQ_RETRY_COUNT, req_msg);
2113 cm_id_priv->rnr_retry_count = IBA_GET(CM_REQ_RNR_RETRY_COUNT, req_msg);
2114 cm_id_priv->qp_type = cm_req_get_qp_type(req_msg);
2115
2116 ret = cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
2117 work->mad_recv_wc->recv_buf.grh,
2118 &cm_id_priv->av);
2119 if (ret)
2120 goto destroy;
2121 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
2122 id.local_id);
2123 if (IS_ERR(cm_id_priv->timewait_info)) {
2124 ret = PTR_ERR(cm_id_priv->timewait_info);
2125 cm_id_priv->timewait_info = NULL;
2126 goto destroy;
2127 }
2128 cm_id_priv->timewait_info->work.remote_id = cm_id_priv->id.remote_id;
2129 cm_id_priv->timewait_info->remote_ca_guid =
2130 cpu_to_be64(IBA_GET(CM_REQ_LOCAL_CA_GUID, req_msg));
2131 cm_id_priv->timewait_info->remote_qpn = cm_id_priv->remote_qpn;
2132
2133 /*
2134 * Note that the ID pointer is not in the xarray at this point,
2135 * so this set is only visible to the local thread.
2136 */
2137 cm_id_priv->id.state = IB_CM_REQ_RCVD;
2138
2139 listen_cm_id_priv = cm_match_req(work, cm_id_priv);
2140 if (!listen_cm_id_priv) {
2141 trace_icm_no_listener_err(&cm_id_priv->id);
2142 cm_id_priv->id.state = IB_CM_IDLE;
2143 ret = -EINVAL;
2144 goto destroy;
2145 }
2146
2147 memset(&work->path[0], 0, sizeof(work->path[0]));
2148 if (cm_req_has_alt_path(req_msg))
2149 memset(&work->path[1], 0, sizeof(work->path[1]));
2150 grh = rdma_ah_read_grh(&cm_id_priv->av.ah_attr);
2151 gid_attr = grh->sgid_attr;
2152
2153 if (cm_id_priv->av.ah_attr.type == RDMA_AH_ATTR_TYPE_ROCE) {
2154 work->path[0].rec_type =
2155 sa_conv_gid_to_pathrec_type(gid_attr->gid_type);
2156 } else {
2157 cm_process_routed_req(req_msg, work->mad_recv_wc->wc);
2158 cm_path_set_rec_type(
2159 work->port->cm_dev->ib_device, work->port->port_num,
2160 &work->path[0],
2161 IBA_GET_MEM_PTR(CM_REQ_PRIMARY_LOCAL_PORT_GID,
2162 req_msg));
2163 }
2164 if (cm_req_has_alt_path(req_msg))
2165 work->path[1].rec_type = work->path[0].rec_type;
2166 cm_format_paths_from_req(req_msg, &work->path[0],
2167 &work->path[1], work->mad_recv_wc->wc);
2168 if (cm_id_priv->av.ah_attr.type == RDMA_AH_ATTR_TYPE_ROCE)
2169 sa_path_set_dmac(&work->path[0],
2170 cm_id_priv->av.ah_attr.roce.dmac);
2171 work->path[0].hop_limit = grh->hop_limit;
2172
2173 /* This destroy call is needed to pair with cm_init_av_for_response */
2174 cm_destroy_av(&cm_id_priv->av);
2175 ret = cm_init_av_by_path(&work->path[0], gid_attr, &cm_id_priv->av);
2176 if (ret) {
2177 int err;
2178
2179 err = rdma_query_gid(work->port->cm_dev->ib_device,
2180 work->port->port_num, 0,
2181 &work->path[0].sgid);
2182 if (err)
2183 ib_send_cm_rej(&cm_id_priv->id, IB_CM_REJ_INVALID_GID,
2184 NULL, 0, NULL, 0);
2185 else
2186 ib_send_cm_rej(&cm_id_priv->id, IB_CM_REJ_INVALID_GID,
2187 &work->path[0].sgid,
2188 sizeof(work->path[0].sgid),
2189 NULL, 0);
2190 goto rejected;
2191 }
2192 if (cm_id_priv->av.ah_attr.type == RDMA_AH_ATTR_TYPE_IB)
2193 cm_id_priv->av.dlid_datapath =
2194 IBA_GET(CM_REQ_PRIMARY_LOCAL_PORT_LID, req_msg);
2195
2196 if (cm_req_has_alt_path(req_msg)) {
2197 ret = cm_init_av_by_path(&work->path[1], NULL,
2198 &cm_id_priv->alt_av);
2199 if (ret) {
2200 ib_send_cm_rej(&cm_id_priv->id,
2201 IB_CM_REJ_INVALID_ALT_GID,
2202 &work->path[0].sgid,
2203 sizeof(work->path[0].sgid), NULL, 0);
2204 goto rejected;
2205 }
2206 }
2207
2208 cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
2209 cm_id_priv->id.context = listen_cm_id_priv->id.context;
2210 cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
2211
2212 /* Now MAD handlers can see the new ID */
2213 spin_lock_irq(&cm_id_priv->lock);
2214 cm_finalize_id(cm_id_priv);
2215
2216 /* Refcount belongs to the event, pairs with cm_process_work() */
2217 refcount_inc(&cm_id_priv->refcount);
2218 cm_queue_work_unlock(cm_id_priv, work);
2219 /*
2220 * Since this ID was just created and was not made visible to other MAD
2221 * handlers until the cm_finalize_id() above we know that the
2222 * cm_process_work() will deliver the event and the listen_cm_id
2223 * embedded in the event can be derefed here.
2224 */
2225 cm_deref_id(listen_cm_id_priv);
2226 return 0;
2227
2228rejected:
2229 cm_deref_id(listen_cm_id_priv);
2230destroy:
2231 ib_destroy_cm_id(&cm_id_priv->id);
2232 return ret;
2233}
2234
2235static void cm_format_rep(struct cm_rep_msg *rep_msg,
2236 struct cm_id_private *cm_id_priv,
2237 struct ib_cm_rep_param *param)
2238{
2239 cm_format_mad_ece_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid,
2240 param->ece.attr_mod);
2241 IBA_SET(CM_REP_LOCAL_COMM_ID, rep_msg,
2242 be32_to_cpu(cm_id_priv->id.local_id));
2243 IBA_SET(CM_REP_REMOTE_COMM_ID, rep_msg,
2244 be32_to_cpu(cm_id_priv->id.remote_id));
2245 IBA_SET(CM_REP_STARTING_PSN, rep_msg, param->starting_psn);
2246 IBA_SET(CM_REP_RESPONDER_RESOURCES, rep_msg,
2247 param->responder_resources);
2248 IBA_SET(CM_REP_TARGET_ACK_DELAY, rep_msg,
2249 cm_id_priv->av.port->cm_dev->ack_delay);
2250 IBA_SET(CM_REP_FAILOVER_ACCEPTED, rep_msg, param->failover_accepted);
2251 IBA_SET(CM_REP_RNR_RETRY_COUNT, rep_msg, param->rnr_retry_count);
2252 IBA_SET(CM_REP_LOCAL_CA_GUID, rep_msg,
2253 be64_to_cpu(cm_id_priv->id.device->node_guid));
2254
2255 if (cm_id_priv->qp_type != IB_QPT_XRC_TGT) {
2256 IBA_SET(CM_REP_INITIATOR_DEPTH, rep_msg,
2257 param->initiator_depth);
2258 IBA_SET(CM_REP_END_TO_END_FLOW_CONTROL, rep_msg,
2259 param->flow_control);
2260 IBA_SET(CM_REP_SRQ, rep_msg, param->srq);
2261 IBA_SET(CM_REP_LOCAL_QPN, rep_msg, param->qp_num);
2262 } else {
2263 IBA_SET(CM_REP_SRQ, rep_msg, 1);
2264 IBA_SET(CM_REP_LOCAL_EE_CONTEXT_NUMBER, rep_msg, param->qp_num);
2265 }
2266
2267 IBA_SET(CM_REP_VENDOR_ID_L, rep_msg, param->ece.vendor_id);
2268 IBA_SET(CM_REP_VENDOR_ID_M, rep_msg, param->ece.vendor_id >> 8);
2269 IBA_SET(CM_REP_VENDOR_ID_H, rep_msg, param->ece.vendor_id >> 16);
2270
2271 if (param->private_data && param->private_data_len)
2272 IBA_SET_MEM(CM_REP_PRIVATE_DATA, rep_msg, param->private_data,
2273 param->private_data_len);
2274}
2275
2276int ib_send_cm_rep(struct ib_cm_id *cm_id,
2277 struct ib_cm_rep_param *param)
2278{
2279 struct cm_id_private *cm_id_priv;
2280 struct ib_mad_send_buf *msg;
2281 struct cm_rep_msg *rep_msg;
2282 unsigned long flags;
2283 int ret;
2284
2285 if (param->private_data &&
2286 param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE)
2287 return -EINVAL;
2288
2289 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2290 spin_lock_irqsave(&cm_id_priv->lock, flags);
2291 if (cm_id->state != IB_CM_REQ_RCVD &&
2292 cm_id->state != IB_CM_MRA_REQ_SENT) {
2293 trace_icm_send_rep_err(cm_id_priv->id.local_id, cm_id->state);
2294 ret = -EINVAL;
2295 goto out;
2296 }
2297
2298 msg = cm_alloc_priv_msg(cm_id_priv, IB_CM_REP_SENT);
2299 if (IS_ERR(msg)) {
2300 ret = PTR_ERR(msg);
2301 goto out;
2302 }
2303
2304 rep_msg = (struct cm_rep_msg *) msg->mad;
2305 cm_format_rep(rep_msg, cm_id_priv, param);
2306
2307 trace_icm_send_rep(cm_id);
2308 ret = ib_post_send_mad(msg, NULL);
2309 if (ret)
2310 goto out_free;
2311
2312 cm_id->state = IB_CM_REP_SENT;
2313 cm_id_priv->initiator_depth = param->initiator_depth;
2314 cm_id_priv->responder_resources = param->responder_resources;
2315 cm_id_priv->rq_psn = cpu_to_be32(IBA_GET(CM_REP_STARTING_PSN, rep_msg));
2316 WARN_ONCE(param->qp_num & 0xFF000000,
2317 "IBTA declares QPN to be 24 bits, but it is 0x%X\n",
2318 param->qp_num);
2319 cm_id_priv->local_qpn = cpu_to_be32(param->qp_num & 0xFFFFFF);
2320 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2321 return 0;
2322
2323out_free:
2324 cm_free_priv_msg(msg);
2325out:
2326 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2327 return ret;
2328}
2329EXPORT_SYMBOL(ib_send_cm_rep);
2330
2331static void cm_format_rtu(struct cm_rtu_msg *rtu_msg,
2332 struct cm_id_private *cm_id_priv,
2333 const void *private_data,
2334 u8 private_data_len)
2335{
2336 cm_format_mad_hdr(&rtu_msg->hdr, CM_RTU_ATTR_ID, cm_id_priv->tid);
2337 IBA_SET(CM_RTU_LOCAL_COMM_ID, rtu_msg,
2338 be32_to_cpu(cm_id_priv->id.local_id));
2339 IBA_SET(CM_RTU_REMOTE_COMM_ID, rtu_msg,
2340 be32_to_cpu(cm_id_priv->id.remote_id));
2341
2342 if (private_data && private_data_len)
2343 IBA_SET_MEM(CM_RTU_PRIVATE_DATA, rtu_msg, private_data,
2344 private_data_len);
2345}
2346
2347int ib_send_cm_rtu(struct ib_cm_id *cm_id,
2348 const void *private_data,
2349 u8 private_data_len)
2350{
2351 struct cm_id_private *cm_id_priv;
2352 struct ib_mad_send_buf *msg;
2353 unsigned long flags;
2354 void *data;
2355 int ret;
2356
2357 if (private_data && private_data_len > IB_CM_RTU_PRIVATE_DATA_SIZE)
2358 return -EINVAL;
2359
2360 data = cm_copy_private_data(private_data, private_data_len);
2361 if (IS_ERR(data))
2362 return PTR_ERR(data);
2363
2364 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2365 spin_lock_irqsave(&cm_id_priv->lock, flags);
2366 if (cm_id->state != IB_CM_REP_RCVD &&
2367 cm_id->state != IB_CM_MRA_REP_SENT) {
2368 trace_icm_send_cm_rtu_err(cm_id);
2369 ret = -EINVAL;
2370 goto error;
2371 }
2372
2373 msg = cm_alloc_msg(cm_id_priv);
2374 if (IS_ERR(msg)) {
2375 ret = PTR_ERR(msg);
2376 goto error;
2377 }
2378
2379 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
2380 private_data, private_data_len);
2381
2382 trace_icm_send_rtu(cm_id);
2383 ret = ib_post_send_mad(msg, NULL);
2384 if (ret) {
2385 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2386 cm_free_msg(msg);
2387 kfree(data);
2388 return ret;
2389 }
2390
2391 cm_id->state = IB_CM_ESTABLISHED;
2392 cm_set_private_data(cm_id_priv, data, private_data_len);
2393 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2394 return 0;
2395
2396error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2397 kfree(data);
2398 return ret;
2399}
2400EXPORT_SYMBOL(ib_send_cm_rtu);
2401
2402static void cm_format_rep_event(struct cm_work *work, enum ib_qp_type qp_type)
2403{
2404 struct cm_rep_msg *rep_msg;
2405 struct ib_cm_rep_event_param *param;
2406
2407 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
2408 param = &work->cm_event.param.rep_rcvd;
2409 param->remote_ca_guid =
2410 cpu_to_be64(IBA_GET(CM_REP_LOCAL_CA_GUID, rep_msg));
2411 param->remote_qkey = IBA_GET(CM_REP_LOCAL_Q_KEY, rep_msg);
2412 param->remote_qpn = be32_to_cpu(cm_rep_get_qpn(rep_msg, qp_type));
2413 param->starting_psn = IBA_GET(CM_REP_STARTING_PSN, rep_msg);
2414 param->responder_resources = IBA_GET(CM_REP_INITIATOR_DEPTH, rep_msg);
2415 param->initiator_depth = IBA_GET(CM_REP_RESPONDER_RESOURCES, rep_msg);
2416 param->target_ack_delay = IBA_GET(CM_REP_TARGET_ACK_DELAY, rep_msg);
2417 param->failover_accepted = IBA_GET(CM_REP_FAILOVER_ACCEPTED, rep_msg);
2418 param->flow_control = IBA_GET(CM_REP_END_TO_END_FLOW_CONTROL, rep_msg);
2419 param->rnr_retry_count = IBA_GET(CM_REP_RNR_RETRY_COUNT, rep_msg);
2420 param->srq = IBA_GET(CM_REP_SRQ, rep_msg);
2421 param->ece.vendor_id = IBA_GET(CM_REP_VENDOR_ID_H, rep_msg) << 16;
2422 param->ece.vendor_id |= IBA_GET(CM_REP_VENDOR_ID_M, rep_msg) << 8;
2423 param->ece.vendor_id |= IBA_GET(CM_REP_VENDOR_ID_L, rep_msg);
2424 param->ece.attr_mod = be32_to_cpu(rep_msg->hdr.attr_mod);
2425
2426 work->cm_event.private_data =
2427 IBA_GET_MEM_PTR(CM_REP_PRIVATE_DATA, rep_msg);
2428}
2429
2430static void cm_dup_rep_handler(struct cm_work *work)
2431{
2432 struct cm_id_private *cm_id_priv;
2433 struct cm_rep_msg *rep_msg;
2434 struct ib_mad_send_buf *msg = NULL;
2435 int ret;
2436
2437 rep_msg = (struct cm_rep_msg *) work->mad_recv_wc->recv_buf.mad;
2438 cm_id_priv = cm_acquire_id(
2439 cpu_to_be32(IBA_GET(CM_REP_REMOTE_COMM_ID, rep_msg)),
2440 cpu_to_be32(IBA_GET(CM_REP_LOCAL_COMM_ID, rep_msg)));
2441 if (!cm_id_priv)
2442 return;
2443
2444 atomic_long_inc(
2445 &work->port->counters[CM_RECV_DUPLICATES][CM_REP_COUNTER]);
2446 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, true, &msg);
2447 if (ret)
2448 goto deref;
2449
2450 spin_lock_irq(&cm_id_priv->lock);
2451 if (cm_id_priv->id.state == IB_CM_ESTABLISHED)
2452 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
2453 cm_id_priv->private_data,
2454 cm_id_priv->private_data_len);
2455 else if (cm_id_priv->id.state == IB_CM_MRA_REP_SENT)
2456 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2457 CM_MSG_RESPONSE_REP, cm_id_priv->service_timeout,
2458 cm_id_priv->private_data,
2459 cm_id_priv->private_data_len);
2460 else
2461 goto unlock;
2462 spin_unlock_irq(&cm_id_priv->lock);
2463
2464 trace_icm_send_dup_rep(&cm_id_priv->id);
2465 ret = ib_post_send_mad(msg, NULL);
2466 if (ret)
2467 goto free;
2468 goto deref;
2469
2470unlock: spin_unlock_irq(&cm_id_priv->lock);
2471free: cm_free_msg(msg);
2472deref: cm_deref_id(cm_id_priv);
2473}
2474
2475static int cm_rep_handler(struct cm_work *work)
2476{
2477 struct cm_id_private *cm_id_priv;
2478 struct cm_rep_msg *rep_msg;
2479 int ret;
2480 struct cm_id_private *cur_cm_id_priv;
2481 struct cm_timewait_info *timewait_info;
2482
2483 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
2484 cm_id_priv = cm_acquire_id(
2485 cpu_to_be32(IBA_GET(CM_REP_REMOTE_COMM_ID, rep_msg)), 0);
2486 if (!cm_id_priv) {
2487 cm_dup_rep_handler(work);
2488 trace_icm_remote_no_priv_err(
2489 IBA_GET(CM_REP_REMOTE_COMM_ID, rep_msg));
2490 return -EINVAL;
2491 }
2492
2493 cm_format_rep_event(work, cm_id_priv->qp_type);
2494
2495 spin_lock_irq(&cm_id_priv->lock);
2496 switch (cm_id_priv->id.state) {
2497 case IB_CM_REQ_SENT:
2498 case IB_CM_MRA_REQ_RCVD:
2499 break;
2500 default:
2501 ret = -EINVAL;
2502 trace_icm_rep_unknown_err(
2503 IBA_GET(CM_REP_LOCAL_COMM_ID, rep_msg),
2504 IBA_GET(CM_REP_REMOTE_COMM_ID, rep_msg),
2505 cm_id_priv->id.state);
2506 spin_unlock_irq(&cm_id_priv->lock);
2507 goto error;
2508 }
2509
2510 cm_id_priv->timewait_info->work.remote_id =
2511 cpu_to_be32(IBA_GET(CM_REP_LOCAL_COMM_ID, rep_msg));
2512 cm_id_priv->timewait_info->remote_ca_guid =
2513 cpu_to_be64(IBA_GET(CM_REP_LOCAL_CA_GUID, rep_msg));
2514 cm_id_priv->timewait_info->remote_qpn = cm_rep_get_qpn(rep_msg, cm_id_priv->qp_type);
2515
2516 spin_lock(&cm.lock);
2517 /* Check for duplicate REP. */
2518 if (cm_insert_remote_id(cm_id_priv->timewait_info)) {
2519 spin_unlock(&cm.lock);
2520 spin_unlock_irq(&cm_id_priv->lock);
2521 ret = -EINVAL;
2522 trace_icm_insert_failed_err(
2523 IBA_GET(CM_REP_REMOTE_COMM_ID, rep_msg));
2524 goto error;
2525 }
2526 /* Check for a stale connection. */
2527 timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
2528 if (timewait_info) {
2529 cm_remove_remote(cm_id_priv);
2530 cur_cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
2531 timewait_info->work.remote_id);
2532
2533 spin_unlock(&cm.lock);
2534 spin_unlock_irq(&cm_id_priv->lock);
2535 cm_issue_rej(work->port, work->mad_recv_wc,
2536 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP,
2537 NULL, 0);
2538 ret = -EINVAL;
2539 trace_icm_staleconn_err(
2540 IBA_GET(CM_REP_LOCAL_COMM_ID, rep_msg),
2541 IBA_GET(CM_REP_REMOTE_COMM_ID, rep_msg));
2542
2543 if (cur_cm_id_priv) {
2544 ib_send_cm_dreq(&cur_cm_id_priv->id, NULL, 0);
2545 cm_deref_id(cur_cm_id_priv);
2546 }
2547
2548 goto error;
2549 }
2550 spin_unlock(&cm.lock);
2551
2552 cm_id_priv->id.state = IB_CM_REP_RCVD;
2553 cm_id_priv->id.remote_id =
2554 cpu_to_be32(IBA_GET(CM_REP_LOCAL_COMM_ID, rep_msg));
2555 cm_id_priv->remote_qpn = cm_rep_get_qpn(rep_msg, cm_id_priv->qp_type);
2556 cm_id_priv->initiator_depth =
2557 IBA_GET(CM_REP_RESPONDER_RESOURCES, rep_msg);
2558 cm_id_priv->responder_resources =
2559 IBA_GET(CM_REP_INITIATOR_DEPTH, rep_msg);
2560 cm_id_priv->sq_psn = cpu_to_be32(IBA_GET(CM_REP_STARTING_PSN, rep_msg));
2561 cm_id_priv->rnr_retry_count = IBA_GET(CM_REP_RNR_RETRY_COUNT, rep_msg);
2562 cm_id_priv->target_ack_delay =
2563 IBA_GET(CM_REP_TARGET_ACK_DELAY, rep_msg);
2564 cm_id_priv->av.timeout =
2565 cm_ack_timeout(cm_id_priv->target_ack_delay,
2566 cm_id_priv->av.timeout - 1);
2567 cm_id_priv->alt_av.timeout =
2568 cm_ack_timeout(cm_id_priv->target_ack_delay,
2569 cm_id_priv->alt_av.timeout - 1);
2570
2571 ib_cancel_mad(cm_id_priv->msg);
2572 cm_queue_work_unlock(cm_id_priv, work);
2573 return 0;
2574
2575error:
2576 cm_deref_id(cm_id_priv);
2577 return ret;
2578}
2579
2580static int cm_establish_handler(struct cm_work *work)
2581{
2582 struct cm_id_private *cm_id_priv;
2583
2584 /* See comment in cm_establish about lookup. */
2585 cm_id_priv = cm_acquire_id(work->local_id, work->remote_id);
2586 if (!cm_id_priv)
2587 return -EINVAL;
2588
2589 spin_lock_irq(&cm_id_priv->lock);
2590 if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
2591 spin_unlock_irq(&cm_id_priv->lock);
2592 goto out;
2593 }
2594
2595 ib_cancel_mad(cm_id_priv->msg);
2596 cm_queue_work_unlock(cm_id_priv, work);
2597 return 0;
2598out:
2599 cm_deref_id(cm_id_priv);
2600 return -EINVAL;
2601}
2602
2603static int cm_rtu_handler(struct cm_work *work)
2604{
2605 struct cm_id_private *cm_id_priv;
2606 struct cm_rtu_msg *rtu_msg;
2607
2608 rtu_msg = (struct cm_rtu_msg *)work->mad_recv_wc->recv_buf.mad;
2609 cm_id_priv = cm_acquire_id(
2610 cpu_to_be32(IBA_GET(CM_RTU_REMOTE_COMM_ID, rtu_msg)),
2611 cpu_to_be32(IBA_GET(CM_RTU_LOCAL_COMM_ID, rtu_msg)));
2612 if (!cm_id_priv)
2613 return -EINVAL;
2614
2615 work->cm_event.private_data =
2616 IBA_GET_MEM_PTR(CM_RTU_PRIVATE_DATA, rtu_msg);
2617
2618 spin_lock_irq(&cm_id_priv->lock);
2619 if (cm_id_priv->id.state != IB_CM_REP_SENT &&
2620 cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) {
2621 spin_unlock_irq(&cm_id_priv->lock);
2622 atomic_long_inc(&work->port->counters[CM_RECV_DUPLICATES]
2623 [CM_RTU_COUNTER]);
2624 goto out;
2625 }
2626 cm_id_priv->id.state = IB_CM_ESTABLISHED;
2627
2628 ib_cancel_mad(cm_id_priv->msg);
2629 cm_queue_work_unlock(cm_id_priv, work);
2630 return 0;
2631out:
2632 cm_deref_id(cm_id_priv);
2633 return -EINVAL;
2634}
2635
2636static void cm_format_dreq(struct cm_dreq_msg *dreq_msg,
2637 struct cm_id_private *cm_id_priv,
2638 const void *private_data,
2639 u8 private_data_len)
2640{
2641 cm_format_mad_hdr(&dreq_msg->hdr, CM_DREQ_ATTR_ID,
2642 cm_form_tid(cm_id_priv));
2643 IBA_SET(CM_DREQ_LOCAL_COMM_ID, dreq_msg,
2644 be32_to_cpu(cm_id_priv->id.local_id));
2645 IBA_SET(CM_DREQ_REMOTE_COMM_ID, dreq_msg,
2646 be32_to_cpu(cm_id_priv->id.remote_id));
2647 IBA_SET(CM_DREQ_REMOTE_QPN_EECN, dreq_msg,
2648 be32_to_cpu(cm_id_priv->remote_qpn));
2649
2650 if (private_data && private_data_len)
2651 IBA_SET_MEM(CM_DREQ_PRIVATE_DATA, dreq_msg, private_data,
2652 private_data_len);
2653}
2654
2655static void cm_issue_dreq(struct cm_id_private *cm_id_priv)
2656{
2657 struct ib_mad_send_buf *msg;
2658 int ret;
2659
2660 lockdep_assert_held(&cm_id_priv->lock);
2661
2662 msg = cm_alloc_msg(cm_id_priv);
2663 if (IS_ERR(msg))
2664 return;
2665
2666 cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv, NULL, 0);
2667
2668 trace_icm_send_dreq(&cm_id_priv->id);
2669 ret = ib_post_send_mad(msg, NULL);
2670 if (ret)
2671 cm_free_msg(msg);
2672}
2673
2674int ib_send_cm_dreq(struct ib_cm_id *cm_id, const void *private_data,
2675 u8 private_data_len)
2676{
2677 struct cm_id_private *cm_id_priv =
2678 container_of(cm_id, struct cm_id_private, id);
2679 struct ib_mad_send_buf *msg;
2680 unsigned long flags;
2681 int ret;
2682
2683 if (private_data && private_data_len > IB_CM_DREQ_PRIVATE_DATA_SIZE)
2684 return -EINVAL;
2685
2686 spin_lock_irqsave(&cm_id_priv->lock, flags);
2687 if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
2688 trace_icm_dreq_skipped(&cm_id_priv->id);
2689 ret = -EINVAL;
2690 goto unlock;
2691 }
2692
2693 if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT ||
2694 cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
2695 ib_cancel_mad(cm_id_priv->msg);
2696
2697 msg = cm_alloc_priv_msg(cm_id_priv, IB_CM_DREQ_SENT);
2698 if (IS_ERR(msg)) {
2699 cm_enter_timewait(cm_id_priv);
2700 ret = PTR_ERR(msg);
2701 goto unlock;
2702 }
2703
2704 cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv,
2705 private_data, private_data_len);
2706
2707 trace_icm_send_dreq(&cm_id_priv->id);
2708 ret = ib_post_send_mad(msg, NULL);
2709 if (ret) {
2710 cm_enter_timewait(cm_id_priv);
2711 cm_free_priv_msg(msg);
2712 goto unlock;
2713 }
2714
2715 cm_id_priv->id.state = IB_CM_DREQ_SENT;
2716unlock:
2717 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2718 return ret;
2719}
2720EXPORT_SYMBOL(ib_send_cm_dreq);
2721
2722static void cm_format_drep(struct cm_drep_msg *drep_msg,
2723 struct cm_id_private *cm_id_priv,
2724 const void *private_data,
2725 u8 private_data_len)
2726{
2727 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, cm_id_priv->tid);
2728 IBA_SET(CM_DREP_LOCAL_COMM_ID, drep_msg,
2729 be32_to_cpu(cm_id_priv->id.local_id));
2730 IBA_SET(CM_DREP_REMOTE_COMM_ID, drep_msg,
2731 be32_to_cpu(cm_id_priv->id.remote_id));
2732
2733 if (private_data && private_data_len)
2734 IBA_SET_MEM(CM_DREP_PRIVATE_DATA, drep_msg, private_data,
2735 private_data_len);
2736}
2737
2738static int cm_send_drep_locked(struct cm_id_private *cm_id_priv,
2739 void *private_data, u8 private_data_len)
2740{
2741 struct ib_mad_send_buf *msg;
2742 int ret;
2743
2744 lockdep_assert_held(&cm_id_priv->lock);
2745
2746 if (private_data && private_data_len > IB_CM_DREP_PRIVATE_DATA_SIZE)
2747 return -EINVAL;
2748
2749 if (cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
2750 trace_icm_send_drep_err(&cm_id_priv->id);
2751 kfree(private_data);
2752 return -EINVAL;
2753 }
2754
2755 cm_set_private_data(cm_id_priv, private_data, private_data_len);
2756 cm_enter_timewait(cm_id_priv);
2757
2758 msg = cm_alloc_msg(cm_id_priv);
2759 if (IS_ERR(msg))
2760 return PTR_ERR(msg);
2761
2762 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
2763 private_data, private_data_len);
2764
2765 trace_icm_send_drep(&cm_id_priv->id);
2766 ret = ib_post_send_mad(msg, NULL);
2767 if (ret) {
2768 cm_free_msg(msg);
2769 return ret;
2770 }
2771 return 0;
2772}
2773
2774int ib_send_cm_drep(struct ib_cm_id *cm_id, const void *private_data,
2775 u8 private_data_len)
2776{
2777 struct cm_id_private *cm_id_priv =
2778 container_of(cm_id, struct cm_id_private, id);
2779 unsigned long flags;
2780 void *data;
2781 int ret;
2782
2783 data = cm_copy_private_data(private_data, private_data_len);
2784 if (IS_ERR(data))
2785 return PTR_ERR(data);
2786
2787 spin_lock_irqsave(&cm_id_priv->lock, flags);
2788 ret = cm_send_drep_locked(cm_id_priv, data, private_data_len);
2789 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2790 return ret;
2791}
2792EXPORT_SYMBOL(ib_send_cm_drep);
2793
2794static int cm_issue_drep(struct cm_port *port,
2795 struct ib_mad_recv_wc *mad_recv_wc)
2796{
2797 struct ib_mad_send_buf *msg = NULL;
2798 struct cm_dreq_msg *dreq_msg;
2799 struct cm_drep_msg *drep_msg;
2800 int ret;
2801
2802 ret = cm_alloc_response_msg(port, mad_recv_wc, true, &msg);
2803 if (ret)
2804 return ret;
2805
2806 dreq_msg = (struct cm_dreq_msg *) mad_recv_wc->recv_buf.mad;
2807 drep_msg = (struct cm_drep_msg *) msg->mad;
2808
2809 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, dreq_msg->hdr.tid);
2810 IBA_SET(CM_DREP_REMOTE_COMM_ID, drep_msg,
2811 IBA_GET(CM_DREQ_LOCAL_COMM_ID, dreq_msg));
2812 IBA_SET(CM_DREP_LOCAL_COMM_ID, drep_msg,
2813 IBA_GET(CM_DREQ_REMOTE_COMM_ID, dreq_msg));
2814
2815 trace_icm_issue_drep(
2816 IBA_GET(CM_DREQ_LOCAL_COMM_ID, dreq_msg),
2817 IBA_GET(CM_DREQ_REMOTE_COMM_ID, dreq_msg));
2818 ret = ib_post_send_mad(msg, NULL);
2819 if (ret)
2820 cm_free_msg(msg);
2821
2822 return ret;
2823}
2824
2825static int cm_dreq_handler(struct cm_work *work)
2826{
2827 struct cm_id_private *cm_id_priv;
2828 struct cm_dreq_msg *dreq_msg;
2829 struct ib_mad_send_buf *msg = NULL;
2830
2831 dreq_msg = (struct cm_dreq_msg *)work->mad_recv_wc->recv_buf.mad;
2832 cm_id_priv = cm_acquire_id(
2833 cpu_to_be32(IBA_GET(CM_DREQ_REMOTE_COMM_ID, dreq_msg)),
2834 cpu_to_be32(IBA_GET(CM_DREQ_LOCAL_COMM_ID, dreq_msg)));
2835 if (!cm_id_priv) {
2836 atomic_long_inc(&work->port->counters[CM_RECV_DUPLICATES]
2837 [CM_DREQ_COUNTER]);
2838 cm_issue_drep(work->port, work->mad_recv_wc);
2839 trace_icm_no_priv_err(
2840 IBA_GET(CM_DREQ_LOCAL_COMM_ID, dreq_msg),
2841 IBA_GET(CM_DREQ_REMOTE_COMM_ID, dreq_msg));
2842 return -EINVAL;
2843 }
2844
2845 work->cm_event.private_data =
2846 IBA_GET_MEM_PTR(CM_DREQ_PRIVATE_DATA, dreq_msg);
2847
2848 spin_lock_irq(&cm_id_priv->lock);
2849 if (cm_id_priv->local_qpn !=
2850 cpu_to_be32(IBA_GET(CM_DREQ_REMOTE_QPN_EECN, dreq_msg)))
2851 goto unlock;
2852
2853 switch (cm_id_priv->id.state) {
2854 case IB_CM_REP_SENT:
2855 case IB_CM_DREQ_SENT:
2856 case IB_CM_MRA_REP_RCVD:
2857 ib_cancel_mad(cm_id_priv->msg);
2858 break;
2859 case IB_CM_ESTABLISHED:
2860 if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT ||
2861 cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
2862 ib_cancel_mad(cm_id_priv->msg);
2863 break;
2864 case IB_CM_TIMEWAIT:
2865 atomic_long_inc(&work->port->counters[CM_RECV_DUPLICATES]
2866 [CM_DREQ_COUNTER]);
2867 msg = cm_alloc_response_msg_no_ah(work->port, work->mad_recv_wc,
2868 true);
2869 if (IS_ERR(msg))
2870 goto unlock;
2871
2872 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
2873 cm_id_priv->private_data,
2874 cm_id_priv->private_data_len);
2875 spin_unlock_irq(&cm_id_priv->lock);
2876
2877 if (cm_create_response_msg_ah(work->port, work->mad_recv_wc, msg) ||
2878 ib_post_send_mad(msg, NULL))
2879 cm_free_msg(msg);
2880 goto deref;
2881 case IB_CM_DREQ_RCVD:
2882 atomic_long_inc(&work->port->counters[CM_RECV_DUPLICATES]
2883 [CM_DREQ_COUNTER]);
2884 goto unlock;
2885 default:
2886 trace_icm_dreq_unknown_err(&cm_id_priv->id);
2887 goto unlock;
2888 }
2889 cm_id_priv->id.state = IB_CM_DREQ_RCVD;
2890 cm_id_priv->tid = dreq_msg->hdr.tid;
2891 cm_queue_work_unlock(cm_id_priv, work);
2892 return 0;
2893
2894unlock: spin_unlock_irq(&cm_id_priv->lock);
2895deref: cm_deref_id(cm_id_priv);
2896 return -EINVAL;
2897}
2898
2899static int cm_drep_handler(struct cm_work *work)
2900{
2901 struct cm_id_private *cm_id_priv;
2902 struct cm_drep_msg *drep_msg;
2903
2904 drep_msg = (struct cm_drep_msg *)work->mad_recv_wc->recv_buf.mad;
2905 cm_id_priv = cm_acquire_id(
2906 cpu_to_be32(IBA_GET(CM_DREP_REMOTE_COMM_ID, drep_msg)),
2907 cpu_to_be32(IBA_GET(CM_DREP_LOCAL_COMM_ID, drep_msg)));
2908 if (!cm_id_priv)
2909 return -EINVAL;
2910
2911 work->cm_event.private_data =
2912 IBA_GET_MEM_PTR(CM_DREP_PRIVATE_DATA, drep_msg);
2913
2914 spin_lock_irq(&cm_id_priv->lock);
2915 if (cm_id_priv->id.state != IB_CM_DREQ_SENT &&
2916 cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
2917 spin_unlock_irq(&cm_id_priv->lock);
2918 goto out;
2919 }
2920 cm_enter_timewait(cm_id_priv);
2921
2922 ib_cancel_mad(cm_id_priv->msg);
2923 cm_queue_work_unlock(cm_id_priv, work);
2924 return 0;
2925out:
2926 cm_deref_id(cm_id_priv);
2927 return -EINVAL;
2928}
2929
2930static int cm_send_rej_locked(struct cm_id_private *cm_id_priv,
2931 enum ib_cm_rej_reason reason, void *ari,
2932 u8 ari_length, const void *private_data,
2933 u8 private_data_len)
2934{
2935 enum ib_cm_state state = cm_id_priv->id.state;
2936 struct ib_mad_send_buf *msg;
2937 int ret;
2938
2939 lockdep_assert_held(&cm_id_priv->lock);
2940
2941 if ((private_data && private_data_len > IB_CM_REJ_PRIVATE_DATA_SIZE) ||
2942 (ari && ari_length > IB_CM_REJ_ARI_LENGTH))
2943 return -EINVAL;
2944
2945 trace_icm_send_rej(&cm_id_priv->id, reason);
2946
2947 switch (state) {
2948 case IB_CM_REQ_SENT:
2949 case IB_CM_MRA_REQ_RCVD:
2950 case IB_CM_REQ_RCVD:
2951 case IB_CM_MRA_REQ_SENT:
2952 case IB_CM_REP_RCVD:
2953 case IB_CM_MRA_REP_SENT:
2954 cm_reset_to_idle(cm_id_priv);
2955 msg = cm_alloc_msg(cm_id_priv);
2956 if (IS_ERR(msg))
2957 return PTR_ERR(msg);
2958 cm_format_rej((struct cm_rej_msg *)msg->mad, cm_id_priv, reason,
2959 ari, ari_length, private_data, private_data_len,
2960 state);
2961 break;
2962 case IB_CM_REP_SENT:
2963 case IB_CM_MRA_REP_RCVD:
2964 cm_enter_timewait(cm_id_priv);
2965 msg = cm_alloc_msg(cm_id_priv);
2966 if (IS_ERR(msg))
2967 return PTR_ERR(msg);
2968 cm_format_rej((struct cm_rej_msg *)msg->mad, cm_id_priv, reason,
2969 ari, ari_length, private_data, private_data_len,
2970 state);
2971 break;
2972 default:
2973 trace_icm_send_unknown_rej_err(&cm_id_priv->id);
2974 return -EINVAL;
2975 }
2976
2977 ret = ib_post_send_mad(msg, NULL);
2978 if (ret) {
2979 cm_free_msg(msg);
2980 return ret;
2981 }
2982
2983 return 0;
2984}
2985
2986int ib_send_cm_rej(struct ib_cm_id *cm_id, enum ib_cm_rej_reason reason,
2987 void *ari, u8 ari_length, const void *private_data,
2988 u8 private_data_len)
2989{
2990 struct cm_id_private *cm_id_priv =
2991 container_of(cm_id, struct cm_id_private, id);
2992 unsigned long flags;
2993 int ret;
2994
2995 spin_lock_irqsave(&cm_id_priv->lock, flags);
2996 ret = cm_send_rej_locked(cm_id_priv, reason, ari, ari_length,
2997 private_data, private_data_len);
2998 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2999 return ret;
3000}
3001EXPORT_SYMBOL(ib_send_cm_rej);
3002
3003static void cm_format_rej_event(struct cm_work *work)
3004{
3005 struct cm_rej_msg *rej_msg;
3006 struct ib_cm_rej_event_param *param;
3007
3008 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
3009 param = &work->cm_event.param.rej_rcvd;
3010 param->ari = IBA_GET_MEM_PTR(CM_REJ_ARI, rej_msg);
3011 param->ari_length = IBA_GET(CM_REJ_REJECTED_INFO_LENGTH, rej_msg);
3012 param->reason = IBA_GET(CM_REJ_REASON, rej_msg);
3013 work->cm_event.private_data =
3014 IBA_GET_MEM_PTR(CM_REJ_PRIVATE_DATA, rej_msg);
3015}
3016
3017static struct cm_id_private *cm_acquire_rejected_id(struct cm_rej_msg *rej_msg)
3018{
3019 struct cm_id_private *cm_id_priv;
3020 __be32 remote_id;
3021
3022 remote_id = cpu_to_be32(IBA_GET(CM_REJ_LOCAL_COMM_ID, rej_msg));
3023
3024 if (IBA_GET(CM_REJ_REASON, rej_msg) == IB_CM_REJ_TIMEOUT) {
3025 cm_id_priv = cm_find_remote_id(
3026 *((__be64 *)IBA_GET_MEM_PTR(CM_REJ_ARI, rej_msg)),
3027 remote_id);
3028 } else if (IBA_GET(CM_REJ_MESSAGE_REJECTED, rej_msg) ==
3029 CM_MSG_RESPONSE_REQ)
3030 cm_id_priv = cm_acquire_id(
3031 cpu_to_be32(IBA_GET(CM_REJ_REMOTE_COMM_ID, rej_msg)),
3032 0);
3033 else
3034 cm_id_priv = cm_acquire_id(
3035 cpu_to_be32(IBA_GET(CM_REJ_REMOTE_COMM_ID, rej_msg)),
3036 remote_id);
3037
3038 return cm_id_priv;
3039}
3040
3041static int cm_rej_handler(struct cm_work *work)
3042{
3043 struct cm_id_private *cm_id_priv;
3044 struct cm_rej_msg *rej_msg;
3045
3046 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
3047 cm_id_priv = cm_acquire_rejected_id(rej_msg);
3048 if (!cm_id_priv)
3049 return -EINVAL;
3050
3051 cm_format_rej_event(work);
3052
3053 spin_lock_irq(&cm_id_priv->lock);
3054 switch (cm_id_priv->id.state) {
3055 case IB_CM_REQ_SENT:
3056 case IB_CM_MRA_REQ_RCVD:
3057 case IB_CM_REP_SENT:
3058 case IB_CM_MRA_REP_RCVD:
3059 ib_cancel_mad(cm_id_priv->msg);
3060 fallthrough;
3061 case IB_CM_REQ_RCVD:
3062 case IB_CM_MRA_REQ_SENT:
3063 if (IBA_GET(CM_REJ_REASON, rej_msg) == IB_CM_REJ_STALE_CONN)
3064 cm_enter_timewait(cm_id_priv);
3065 else
3066 cm_reset_to_idle(cm_id_priv);
3067 break;
3068 case IB_CM_DREQ_SENT:
3069 ib_cancel_mad(cm_id_priv->msg);
3070 fallthrough;
3071 case IB_CM_REP_RCVD:
3072 case IB_CM_MRA_REP_SENT:
3073 cm_enter_timewait(cm_id_priv);
3074 break;
3075 case IB_CM_ESTABLISHED:
3076 if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT ||
3077 cm_id_priv->id.lap_state == IB_CM_LAP_SENT) {
3078 if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT)
3079 ib_cancel_mad(cm_id_priv->msg);
3080 cm_enter_timewait(cm_id_priv);
3081 break;
3082 }
3083 fallthrough;
3084 default:
3085 trace_icm_rej_unknown_err(&cm_id_priv->id);
3086 spin_unlock_irq(&cm_id_priv->lock);
3087 goto out;
3088 }
3089
3090 cm_queue_work_unlock(cm_id_priv, work);
3091 return 0;
3092out:
3093 cm_deref_id(cm_id_priv);
3094 return -EINVAL;
3095}
3096
3097int ib_send_cm_mra(struct ib_cm_id *cm_id,
3098 u8 service_timeout,
3099 const void *private_data,
3100 u8 private_data_len)
3101{
3102 struct cm_id_private *cm_id_priv;
3103 struct ib_mad_send_buf *msg;
3104 enum ib_cm_state cm_state;
3105 enum ib_cm_lap_state lap_state;
3106 enum cm_msg_response msg_response;
3107 void *data;
3108 unsigned long flags;
3109 int ret;
3110
3111 if (private_data && private_data_len > IB_CM_MRA_PRIVATE_DATA_SIZE)
3112 return -EINVAL;
3113
3114 data = cm_copy_private_data(private_data, private_data_len);
3115 if (IS_ERR(data))
3116 return PTR_ERR(data);
3117
3118 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3119
3120 spin_lock_irqsave(&cm_id_priv->lock, flags);
3121 switch (cm_id_priv->id.state) {
3122 case IB_CM_REQ_RCVD:
3123 cm_state = IB_CM_MRA_REQ_SENT;
3124 lap_state = cm_id->lap_state;
3125 msg_response = CM_MSG_RESPONSE_REQ;
3126 break;
3127 case IB_CM_REP_RCVD:
3128 cm_state = IB_CM_MRA_REP_SENT;
3129 lap_state = cm_id->lap_state;
3130 msg_response = CM_MSG_RESPONSE_REP;
3131 break;
3132 case IB_CM_ESTABLISHED:
3133 if (cm_id->lap_state == IB_CM_LAP_RCVD) {
3134 cm_state = cm_id->state;
3135 lap_state = IB_CM_MRA_LAP_SENT;
3136 msg_response = CM_MSG_RESPONSE_OTHER;
3137 break;
3138 }
3139 fallthrough;
3140 default:
3141 trace_icm_send_mra_unknown_err(&cm_id_priv->id);
3142 ret = -EINVAL;
3143 goto error_unlock;
3144 }
3145
3146 if (!(service_timeout & IB_CM_MRA_FLAG_DELAY)) {
3147 msg = cm_alloc_msg(cm_id_priv);
3148 if (IS_ERR(msg)) {
3149 ret = PTR_ERR(msg);
3150 goto error_unlock;
3151 }
3152
3153 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
3154 msg_response, service_timeout,
3155 private_data, private_data_len);
3156 trace_icm_send_mra(cm_id);
3157 ret = ib_post_send_mad(msg, NULL);
3158 if (ret)
3159 goto error_free_msg;
3160 }
3161
3162 cm_id->state = cm_state;
3163 cm_id->lap_state = lap_state;
3164 cm_id_priv->service_timeout = service_timeout;
3165 cm_set_private_data(cm_id_priv, data, private_data_len);
3166 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3167 return 0;
3168
3169error_free_msg:
3170 cm_free_msg(msg);
3171error_unlock:
3172 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3173 kfree(data);
3174 return ret;
3175}
3176EXPORT_SYMBOL(ib_send_cm_mra);
3177
3178static struct cm_id_private *cm_acquire_mraed_id(struct cm_mra_msg *mra_msg)
3179{
3180 switch (IBA_GET(CM_MRA_MESSAGE_MRAED, mra_msg)) {
3181 case CM_MSG_RESPONSE_REQ:
3182 return cm_acquire_id(
3183 cpu_to_be32(IBA_GET(CM_MRA_REMOTE_COMM_ID, mra_msg)),
3184 0);
3185 case CM_MSG_RESPONSE_REP:
3186 case CM_MSG_RESPONSE_OTHER:
3187 return cm_acquire_id(
3188 cpu_to_be32(IBA_GET(CM_MRA_REMOTE_COMM_ID, mra_msg)),
3189 cpu_to_be32(IBA_GET(CM_MRA_LOCAL_COMM_ID, mra_msg)));
3190 default:
3191 return NULL;
3192 }
3193}
3194
3195static int cm_mra_handler(struct cm_work *work)
3196{
3197 struct cm_id_private *cm_id_priv;
3198 struct cm_mra_msg *mra_msg;
3199 int timeout;
3200
3201 mra_msg = (struct cm_mra_msg *)work->mad_recv_wc->recv_buf.mad;
3202 cm_id_priv = cm_acquire_mraed_id(mra_msg);
3203 if (!cm_id_priv)
3204 return -EINVAL;
3205
3206 work->cm_event.private_data =
3207 IBA_GET_MEM_PTR(CM_MRA_PRIVATE_DATA, mra_msg);
3208 work->cm_event.param.mra_rcvd.service_timeout =
3209 IBA_GET(CM_MRA_SERVICE_TIMEOUT, mra_msg);
3210 timeout = cm_convert_to_ms(IBA_GET(CM_MRA_SERVICE_TIMEOUT, mra_msg)) +
3211 cm_convert_to_ms(cm_id_priv->av.timeout);
3212
3213 spin_lock_irq(&cm_id_priv->lock);
3214 switch (cm_id_priv->id.state) {
3215 case IB_CM_REQ_SENT:
3216 if (IBA_GET(CM_MRA_MESSAGE_MRAED, mra_msg) !=
3217 CM_MSG_RESPONSE_REQ ||
3218 ib_modify_mad(cm_id_priv->msg, timeout))
3219 goto out;
3220 cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD;
3221 break;
3222 case IB_CM_REP_SENT:
3223 if (IBA_GET(CM_MRA_MESSAGE_MRAED, mra_msg) !=
3224 CM_MSG_RESPONSE_REP ||
3225 ib_modify_mad(cm_id_priv->msg, timeout))
3226 goto out;
3227 cm_id_priv->id.state = IB_CM_MRA_REP_RCVD;
3228 break;
3229 case IB_CM_ESTABLISHED:
3230 if (IBA_GET(CM_MRA_MESSAGE_MRAED, mra_msg) !=
3231 CM_MSG_RESPONSE_OTHER ||
3232 cm_id_priv->id.lap_state != IB_CM_LAP_SENT ||
3233 ib_modify_mad(cm_id_priv->msg, timeout)) {
3234 if (cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
3235 atomic_long_inc(
3236 &work->port->counters[CM_RECV_DUPLICATES]
3237 [CM_MRA_COUNTER]);
3238 goto out;
3239 }
3240 cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD;
3241 break;
3242 case IB_CM_MRA_REQ_RCVD:
3243 case IB_CM_MRA_REP_RCVD:
3244 atomic_long_inc(&work->port->counters[CM_RECV_DUPLICATES]
3245 [CM_MRA_COUNTER]);
3246 fallthrough;
3247 default:
3248 trace_icm_mra_unknown_err(&cm_id_priv->id);
3249 goto out;
3250 }
3251
3252 cm_id_priv->msg->context[1] = (void *) (unsigned long)
3253 cm_id_priv->id.state;
3254 cm_queue_work_unlock(cm_id_priv, work);
3255 return 0;
3256out:
3257 spin_unlock_irq(&cm_id_priv->lock);
3258 cm_deref_id(cm_id_priv);
3259 return -EINVAL;
3260}
3261
3262static void cm_format_path_lid_from_lap(struct cm_lap_msg *lap_msg,
3263 struct sa_path_rec *path)
3264{
3265 u32 lid;
3266
3267 if (path->rec_type != SA_PATH_REC_TYPE_OPA) {
3268 sa_path_set_dlid(path, IBA_GET(CM_LAP_ALTERNATE_LOCAL_PORT_LID,
3269 lap_msg));
3270 sa_path_set_slid(path, IBA_GET(CM_LAP_ALTERNATE_REMOTE_PORT_LID,
3271 lap_msg));
3272 } else {
3273 lid = opa_get_lid_from_gid(IBA_GET_MEM_PTR(
3274 CM_LAP_ALTERNATE_LOCAL_PORT_GID, lap_msg));
3275 sa_path_set_dlid(path, lid);
3276
3277 lid = opa_get_lid_from_gid(IBA_GET_MEM_PTR(
3278 CM_LAP_ALTERNATE_REMOTE_PORT_GID, lap_msg));
3279 sa_path_set_slid(path, lid);
3280 }
3281}
3282
3283static void cm_format_path_from_lap(struct cm_id_private *cm_id_priv,
3284 struct sa_path_rec *path,
3285 struct cm_lap_msg *lap_msg)
3286{
3287 path->dgid = *IBA_GET_MEM_PTR(CM_LAP_ALTERNATE_LOCAL_PORT_GID, lap_msg);
3288 path->sgid =
3289 *IBA_GET_MEM_PTR(CM_LAP_ALTERNATE_REMOTE_PORT_GID, lap_msg);
3290 path->flow_label =
3291 cpu_to_be32(IBA_GET(CM_LAP_ALTERNATE_FLOW_LABEL, lap_msg));
3292 path->hop_limit = IBA_GET(CM_LAP_ALTERNATE_HOP_LIMIT, lap_msg);
3293 path->traffic_class = IBA_GET(CM_LAP_ALTERNATE_TRAFFIC_CLASS, lap_msg);
3294 path->reversible = 1;
3295 path->pkey = cm_id_priv->pkey;
3296 path->sl = IBA_GET(CM_LAP_ALTERNATE_SL, lap_msg);
3297 path->mtu_selector = IB_SA_EQ;
3298 path->mtu = cm_id_priv->path_mtu;
3299 path->rate_selector = IB_SA_EQ;
3300 path->rate = IBA_GET(CM_LAP_ALTERNATE_PACKET_RATE, lap_msg);
3301 path->packet_life_time_selector = IB_SA_EQ;
3302 path->packet_life_time =
3303 IBA_GET(CM_LAP_ALTERNATE_LOCAL_ACK_TIMEOUT, lap_msg);
3304 path->packet_life_time -= (path->packet_life_time > 0);
3305 cm_format_path_lid_from_lap(lap_msg, path);
3306}
3307
3308static int cm_lap_handler(struct cm_work *work)
3309{
3310 struct cm_id_private *cm_id_priv;
3311 struct cm_lap_msg *lap_msg;
3312 struct ib_cm_lap_event_param *param;
3313 struct ib_mad_send_buf *msg = NULL;
3314 struct rdma_ah_attr ah_attr;
3315 struct cm_av alt_av = {};
3316 int ret;
3317
3318 /* Currently Alternate path messages are not supported for
3319 * RoCE link layer.
3320 */
3321 if (rdma_protocol_roce(work->port->cm_dev->ib_device,
3322 work->port->port_num))
3323 return -EINVAL;
3324
3325 /* todo: verify LAP request and send reject APR if invalid. */
3326 lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad;
3327 cm_id_priv = cm_acquire_id(
3328 cpu_to_be32(IBA_GET(CM_LAP_REMOTE_COMM_ID, lap_msg)),
3329 cpu_to_be32(IBA_GET(CM_LAP_LOCAL_COMM_ID, lap_msg)));
3330 if (!cm_id_priv)
3331 return -EINVAL;
3332
3333 param = &work->cm_event.param.lap_rcvd;
3334 memset(&work->path[0], 0, sizeof(work->path[1]));
3335 cm_path_set_rec_type(work->port->cm_dev->ib_device,
3336 work->port->port_num, &work->path[0],
3337 IBA_GET_MEM_PTR(CM_LAP_ALTERNATE_LOCAL_PORT_GID,
3338 lap_msg));
3339 param->alternate_path = &work->path[0];
3340 cm_format_path_from_lap(cm_id_priv, param->alternate_path, lap_msg);
3341 work->cm_event.private_data =
3342 IBA_GET_MEM_PTR(CM_LAP_PRIVATE_DATA, lap_msg);
3343
3344 ret = ib_init_ah_attr_from_wc(work->port->cm_dev->ib_device,
3345 work->port->port_num,
3346 work->mad_recv_wc->wc,
3347 work->mad_recv_wc->recv_buf.grh,
3348 &ah_attr);
3349 if (ret)
3350 goto deref;
3351
3352 ret = cm_init_av_by_path(param->alternate_path, NULL, &alt_av);
3353 if (ret) {
3354 rdma_destroy_ah_attr(&ah_attr);
3355 goto deref;
3356 }
3357
3358 spin_lock_irq(&cm_id_priv->lock);
3359 cm_init_av_for_lap(work->port, work->mad_recv_wc->wc,
3360 &ah_attr, &cm_id_priv->av);
3361 cm_move_av_from_path(&cm_id_priv->alt_av, &alt_av);
3362
3363 if (cm_id_priv->id.state != IB_CM_ESTABLISHED)
3364 goto unlock;
3365
3366 switch (cm_id_priv->id.lap_state) {
3367 case IB_CM_LAP_UNINIT:
3368 case IB_CM_LAP_IDLE:
3369 break;
3370 case IB_CM_MRA_LAP_SENT:
3371 atomic_long_inc(&work->port->counters[CM_RECV_DUPLICATES]
3372 [CM_LAP_COUNTER]);
3373 msg = cm_alloc_response_msg_no_ah(work->port, work->mad_recv_wc,
3374 true);
3375 if (IS_ERR(msg))
3376 goto unlock;
3377
3378 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
3379 CM_MSG_RESPONSE_OTHER,
3380 cm_id_priv->service_timeout,
3381 cm_id_priv->private_data,
3382 cm_id_priv->private_data_len);
3383 spin_unlock_irq(&cm_id_priv->lock);
3384
3385 if (cm_create_response_msg_ah(work->port, work->mad_recv_wc, msg) ||
3386 ib_post_send_mad(msg, NULL))
3387 cm_free_msg(msg);
3388 goto deref;
3389 case IB_CM_LAP_RCVD:
3390 atomic_long_inc(&work->port->counters[CM_RECV_DUPLICATES]
3391 [CM_LAP_COUNTER]);
3392 goto unlock;
3393 default:
3394 goto unlock;
3395 }
3396
3397 cm_id_priv->id.lap_state = IB_CM_LAP_RCVD;
3398 cm_id_priv->tid = lap_msg->hdr.tid;
3399 cm_queue_work_unlock(cm_id_priv, work);
3400 return 0;
3401
3402unlock: spin_unlock_irq(&cm_id_priv->lock);
3403deref: cm_deref_id(cm_id_priv);
3404 return -EINVAL;
3405}
3406
3407static int cm_apr_handler(struct cm_work *work)
3408{
3409 struct cm_id_private *cm_id_priv;
3410 struct cm_apr_msg *apr_msg;
3411
3412 /* Currently Alternate path messages are not supported for
3413 * RoCE link layer.
3414 */
3415 if (rdma_protocol_roce(work->port->cm_dev->ib_device,
3416 work->port->port_num))
3417 return -EINVAL;
3418
3419 apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad;
3420 cm_id_priv = cm_acquire_id(
3421 cpu_to_be32(IBA_GET(CM_APR_REMOTE_COMM_ID, apr_msg)),
3422 cpu_to_be32(IBA_GET(CM_APR_LOCAL_COMM_ID, apr_msg)));
3423 if (!cm_id_priv)
3424 return -EINVAL; /* Unmatched reply. */
3425
3426 work->cm_event.param.apr_rcvd.ap_status =
3427 IBA_GET(CM_APR_AR_STATUS, apr_msg);
3428 work->cm_event.param.apr_rcvd.apr_info =
3429 IBA_GET_MEM_PTR(CM_APR_ADDITIONAL_INFORMATION, apr_msg);
3430 work->cm_event.param.apr_rcvd.info_len =
3431 IBA_GET(CM_APR_ADDITIONAL_INFORMATION_LENGTH, apr_msg);
3432 work->cm_event.private_data =
3433 IBA_GET_MEM_PTR(CM_APR_PRIVATE_DATA, apr_msg);
3434
3435 spin_lock_irq(&cm_id_priv->lock);
3436 if (cm_id_priv->id.state != IB_CM_ESTABLISHED ||
3437 (cm_id_priv->id.lap_state != IB_CM_LAP_SENT &&
3438 cm_id_priv->id.lap_state != IB_CM_MRA_LAP_RCVD)) {
3439 spin_unlock_irq(&cm_id_priv->lock);
3440 goto out;
3441 }
3442 cm_id_priv->id.lap_state = IB_CM_LAP_IDLE;
3443 ib_cancel_mad(cm_id_priv->msg);
3444 cm_queue_work_unlock(cm_id_priv, work);
3445 return 0;
3446out:
3447 cm_deref_id(cm_id_priv);
3448 return -EINVAL;
3449}
3450
3451static int cm_timewait_handler(struct cm_work *work)
3452{
3453 struct cm_timewait_info *timewait_info;
3454 struct cm_id_private *cm_id_priv;
3455
3456 timewait_info = container_of(work, struct cm_timewait_info, work);
3457 spin_lock_irq(&cm.lock);
3458 list_del(&timewait_info->list);
3459 spin_unlock_irq(&cm.lock);
3460
3461 cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
3462 timewait_info->work.remote_id);
3463 if (!cm_id_priv)
3464 return -EINVAL;
3465
3466 spin_lock_irq(&cm_id_priv->lock);
3467 if (cm_id_priv->id.state != IB_CM_TIMEWAIT ||
3468 cm_id_priv->remote_qpn != timewait_info->remote_qpn) {
3469 spin_unlock_irq(&cm_id_priv->lock);
3470 goto out;
3471 }
3472 cm_id_priv->id.state = IB_CM_IDLE;
3473 cm_queue_work_unlock(cm_id_priv, work);
3474 return 0;
3475out:
3476 cm_deref_id(cm_id_priv);
3477 return -EINVAL;
3478}
3479
3480static void cm_format_sidr_req(struct cm_sidr_req_msg *sidr_req_msg,
3481 struct cm_id_private *cm_id_priv,
3482 struct ib_cm_sidr_req_param *param)
3483{
3484 cm_format_mad_hdr(&sidr_req_msg->hdr, CM_SIDR_REQ_ATTR_ID,
3485 cm_form_tid(cm_id_priv));
3486 IBA_SET(CM_SIDR_REQ_REQUESTID, sidr_req_msg,
3487 be32_to_cpu(cm_id_priv->id.local_id));
3488 IBA_SET(CM_SIDR_REQ_PARTITION_KEY, sidr_req_msg,
3489 be16_to_cpu(param->path->pkey));
3490 IBA_SET(CM_SIDR_REQ_SERVICEID, sidr_req_msg,
3491 be64_to_cpu(param->service_id));
3492
3493 if (param->private_data && param->private_data_len)
3494 IBA_SET_MEM(CM_SIDR_REQ_PRIVATE_DATA, sidr_req_msg,
3495 param->private_data, param->private_data_len);
3496}
3497
3498int ib_send_cm_sidr_req(struct ib_cm_id *cm_id,
3499 struct ib_cm_sidr_req_param *param)
3500{
3501 struct cm_id_private *cm_id_priv;
3502 struct ib_mad_send_buf *msg;
3503 struct cm_av av = {};
3504 unsigned long flags;
3505 int ret;
3506
3507 if (!param->path || (param->private_data &&
3508 param->private_data_len > IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE))
3509 return -EINVAL;
3510
3511 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3512 ret = cm_init_av_by_path(param->path, param->sgid_attr, &av);
3513 if (ret)
3514 return ret;
3515
3516 spin_lock_irqsave(&cm_id_priv->lock, flags);
3517 cm_move_av_from_path(&cm_id_priv->av, &av);
3518 cm_id->service_id = param->service_id;
3519 cm_id_priv->timeout_ms = param->timeout_ms;
3520 cm_id_priv->max_cm_retries = param->max_cm_retries;
3521 if (cm_id->state != IB_CM_IDLE) {
3522 ret = -EINVAL;
3523 goto out_unlock;
3524 }
3525
3526 msg = cm_alloc_priv_msg(cm_id_priv, IB_CM_SIDR_REQ_SENT);
3527 if (IS_ERR(msg)) {
3528 ret = PTR_ERR(msg);
3529 goto out_unlock;
3530 }
3531
3532 cm_format_sidr_req((struct cm_sidr_req_msg *)msg->mad, cm_id_priv,
3533 param);
3534
3535 trace_icm_send_sidr_req(&cm_id_priv->id);
3536 ret = ib_post_send_mad(msg, NULL);
3537 if (ret)
3538 goto out_free;
3539 cm_id->state = IB_CM_SIDR_REQ_SENT;
3540 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3541 return 0;
3542out_free:
3543 cm_free_priv_msg(msg);
3544out_unlock:
3545 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3546 return ret;
3547}
3548EXPORT_SYMBOL(ib_send_cm_sidr_req);
3549
3550static void cm_format_sidr_req_event(struct cm_work *work,
3551 const struct cm_id_private *rx_cm_id,
3552 struct ib_cm_id *listen_id)
3553{
3554 struct cm_sidr_req_msg *sidr_req_msg;
3555 struct ib_cm_sidr_req_event_param *param;
3556
3557 sidr_req_msg = (struct cm_sidr_req_msg *)
3558 work->mad_recv_wc->recv_buf.mad;
3559 param = &work->cm_event.param.sidr_req_rcvd;
3560 param->pkey = IBA_GET(CM_SIDR_REQ_PARTITION_KEY, sidr_req_msg);
3561 param->listen_id = listen_id;
3562 param->service_id =
3563 cpu_to_be64(IBA_GET(CM_SIDR_REQ_SERVICEID, sidr_req_msg));
3564 param->bth_pkey = cm_get_bth_pkey(work);
3565 param->port = work->port->port_num;
3566 param->sgid_attr = rx_cm_id->av.ah_attr.grh.sgid_attr;
3567 work->cm_event.private_data =
3568 IBA_GET_MEM_PTR(CM_SIDR_REQ_PRIVATE_DATA, sidr_req_msg);
3569}
3570
3571static int cm_sidr_req_handler(struct cm_work *work)
3572{
3573 struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
3574 struct cm_sidr_req_msg *sidr_req_msg;
3575 struct ib_wc *wc;
3576 int ret;
3577
3578 cm_id_priv =
3579 cm_alloc_id_priv(work->port->cm_dev->ib_device, NULL, NULL);
3580 if (IS_ERR(cm_id_priv))
3581 return PTR_ERR(cm_id_priv);
3582
3583 /* Record SGID/SLID and request ID for lookup. */
3584 sidr_req_msg = (struct cm_sidr_req_msg *)
3585 work->mad_recv_wc->recv_buf.mad;
3586
3587 cm_id_priv->id.remote_id =
3588 cpu_to_be32(IBA_GET(CM_SIDR_REQ_REQUESTID, sidr_req_msg));
3589 cm_id_priv->id.service_id =
3590 cpu_to_be64(IBA_GET(CM_SIDR_REQ_SERVICEID, sidr_req_msg));
3591 cm_id_priv->tid = sidr_req_msg->hdr.tid;
3592
3593 wc = work->mad_recv_wc->wc;
3594 cm_id_priv->sidr_slid = wc->slid;
3595 ret = cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
3596 work->mad_recv_wc->recv_buf.grh,
3597 &cm_id_priv->av);
3598 if (ret)
3599 goto out;
3600
3601 spin_lock_irq(&cm.lock);
3602 listen_cm_id_priv = cm_insert_remote_sidr(cm_id_priv);
3603 if (listen_cm_id_priv) {
3604 spin_unlock_irq(&cm.lock);
3605 atomic_long_inc(&work->port->counters[CM_RECV_DUPLICATES]
3606 [CM_SIDR_REQ_COUNTER]);
3607 goto out; /* Duplicate message. */
3608 }
3609 cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD;
3610 listen_cm_id_priv = cm_find_listen(cm_id_priv->id.device,
3611 cm_id_priv->id.service_id);
3612 if (!listen_cm_id_priv) {
3613 spin_unlock_irq(&cm.lock);
3614 ib_send_cm_sidr_rep(&cm_id_priv->id,
3615 &(struct ib_cm_sidr_rep_param){
3616 .status = IB_SIDR_UNSUPPORTED });
3617 goto out; /* No match. */
3618 }
3619 spin_unlock_irq(&cm.lock);
3620
3621 cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
3622 cm_id_priv->id.context = listen_cm_id_priv->id.context;
3623
3624 /*
3625 * A SIDR ID does not need to be in the xarray since it does not receive
3626 * mads, is not placed in the remote_id or remote_qpn rbtree, and does
3627 * not enter timewait.
3628 */
3629
3630 cm_format_sidr_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
3631 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
3632 cm_free_work(work);
3633 /*
3634 * A pointer to the listen_cm_id is held in the event, so this deref
3635 * must be after the event is delivered above.
3636 */
3637 cm_deref_id(listen_cm_id_priv);
3638 if (ret)
3639 cm_destroy_id(&cm_id_priv->id, ret);
3640 return 0;
3641out:
3642 ib_destroy_cm_id(&cm_id_priv->id);
3643 return -EINVAL;
3644}
3645
3646static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg,
3647 struct cm_id_private *cm_id_priv,
3648 struct ib_cm_sidr_rep_param *param)
3649{
3650 cm_format_mad_ece_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID,
3651 cm_id_priv->tid, param->ece.attr_mod);
3652 IBA_SET(CM_SIDR_REP_REQUESTID, sidr_rep_msg,
3653 be32_to_cpu(cm_id_priv->id.remote_id));
3654 IBA_SET(CM_SIDR_REP_STATUS, sidr_rep_msg, param->status);
3655 IBA_SET(CM_SIDR_REP_QPN, sidr_rep_msg, param->qp_num);
3656 IBA_SET(CM_SIDR_REP_SERVICEID, sidr_rep_msg,
3657 be64_to_cpu(cm_id_priv->id.service_id));
3658 IBA_SET(CM_SIDR_REP_Q_KEY, sidr_rep_msg, param->qkey);
3659 IBA_SET(CM_SIDR_REP_VENDOR_ID_L, sidr_rep_msg,
3660 param->ece.vendor_id & 0xFF);
3661 IBA_SET(CM_SIDR_REP_VENDOR_ID_H, sidr_rep_msg,
3662 (param->ece.vendor_id >> 8) & 0xFF);
3663
3664 if (param->info && param->info_length)
3665 IBA_SET_MEM(CM_SIDR_REP_ADDITIONAL_INFORMATION, sidr_rep_msg,
3666 param->info, param->info_length);
3667
3668 if (param->private_data && param->private_data_len)
3669 IBA_SET_MEM(CM_SIDR_REP_PRIVATE_DATA, sidr_rep_msg,
3670 param->private_data, param->private_data_len);
3671}
3672
3673static int cm_send_sidr_rep_locked(struct cm_id_private *cm_id_priv,
3674 struct ib_cm_sidr_rep_param *param)
3675{
3676 struct ib_mad_send_buf *msg;
3677 unsigned long flags;
3678 int ret;
3679
3680 lockdep_assert_held(&cm_id_priv->lock);
3681
3682 if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) ||
3683 (param->private_data &&
3684 param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE))
3685 return -EINVAL;
3686
3687 if (cm_id_priv->id.state != IB_CM_SIDR_REQ_RCVD)
3688 return -EINVAL;
3689
3690 msg = cm_alloc_msg(cm_id_priv);
3691 if (IS_ERR(msg))
3692 return PTR_ERR(msg);
3693
3694 cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv,
3695 param);
3696 trace_icm_send_sidr_rep(&cm_id_priv->id);
3697 ret = ib_post_send_mad(msg, NULL);
3698 if (ret) {
3699 cm_free_msg(msg);
3700 return ret;
3701 }
3702 cm_id_priv->id.state = IB_CM_IDLE;
3703 spin_lock_irqsave(&cm.lock, flags);
3704 if (!RB_EMPTY_NODE(&cm_id_priv->sidr_id_node)) {
3705 rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
3706 RB_CLEAR_NODE(&cm_id_priv->sidr_id_node);
3707 }
3708 spin_unlock_irqrestore(&cm.lock, flags);
3709 return 0;
3710}
3711
3712int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id,
3713 struct ib_cm_sidr_rep_param *param)
3714{
3715 struct cm_id_private *cm_id_priv =
3716 container_of(cm_id, struct cm_id_private, id);
3717 unsigned long flags;
3718 int ret;
3719
3720 spin_lock_irqsave(&cm_id_priv->lock, flags);
3721 ret = cm_send_sidr_rep_locked(cm_id_priv, param);
3722 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3723 return ret;
3724}
3725EXPORT_SYMBOL(ib_send_cm_sidr_rep);
3726
3727static void cm_format_sidr_rep_event(struct cm_work *work,
3728 const struct cm_id_private *cm_id_priv)
3729{
3730 struct cm_sidr_rep_msg *sidr_rep_msg;
3731 struct ib_cm_sidr_rep_event_param *param;
3732
3733 sidr_rep_msg = (struct cm_sidr_rep_msg *)
3734 work->mad_recv_wc->recv_buf.mad;
3735 param = &work->cm_event.param.sidr_rep_rcvd;
3736 param->status = IBA_GET(CM_SIDR_REP_STATUS, sidr_rep_msg);
3737 param->qkey = IBA_GET(CM_SIDR_REP_Q_KEY, sidr_rep_msg);
3738 param->qpn = IBA_GET(CM_SIDR_REP_QPN, sidr_rep_msg);
3739 param->info = IBA_GET_MEM_PTR(CM_SIDR_REP_ADDITIONAL_INFORMATION,
3740 sidr_rep_msg);
3741 param->info_len = IBA_GET(CM_SIDR_REP_ADDITIONAL_INFORMATION_LENGTH,
3742 sidr_rep_msg);
3743 param->sgid_attr = cm_id_priv->av.ah_attr.grh.sgid_attr;
3744 work->cm_event.private_data =
3745 IBA_GET_MEM_PTR(CM_SIDR_REP_PRIVATE_DATA, sidr_rep_msg);
3746}
3747
3748static int cm_sidr_rep_handler(struct cm_work *work)
3749{
3750 struct cm_sidr_rep_msg *sidr_rep_msg;
3751 struct cm_id_private *cm_id_priv;
3752
3753 sidr_rep_msg = (struct cm_sidr_rep_msg *)
3754 work->mad_recv_wc->recv_buf.mad;
3755 cm_id_priv = cm_acquire_id(
3756 cpu_to_be32(IBA_GET(CM_SIDR_REP_REQUESTID, sidr_rep_msg)), 0);
3757 if (!cm_id_priv)
3758 return -EINVAL; /* Unmatched reply. */
3759
3760 spin_lock_irq(&cm_id_priv->lock);
3761 if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) {
3762 spin_unlock_irq(&cm_id_priv->lock);
3763 goto out;
3764 }
3765 cm_id_priv->id.state = IB_CM_IDLE;
3766 ib_cancel_mad(cm_id_priv->msg);
3767 spin_unlock_irq(&cm_id_priv->lock);
3768
3769 cm_format_sidr_rep_event(work, cm_id_priv);
3770 cm_process_work(cm_id_priv, work);
3771 return 0;
3772out:
3773 cm_deref_id(cm_id_priv);
3774 return -EINVAL;
3775}
3776
3777static void cm_process_send_error(struct cm_id_private *cm_id_priv,
3778 struct ib_mad_send_buf *msg,
3779 enum ib_wc_status wc_status)
3780{
3781 enum ib_cm_state state = (unsigned long) msg->context[1];
3782 struct ib_cm_event cm_event = {};
3783 int ret;
3784
3785 /* Discard old sends. */
3786 spin_lock_irq(&cm_id_priv->lock);
3787 if (msg != cm_id_priv->msg) {
3788 spin_unlock_irq(&cm_id_priv->lock);
3789 cm_free_priv_msg(msg);
3790 return;
3791 }
3792 cm_free_priv_msg(msg);
3793
3794 if (state != cm_id_priv->id.state || wc_status == IB_WC_SUCCESS ||
3795 wc_status == IB_WC_WR_FLUSH_ERR)
3796 goto out_unlock;
3797
3798 trace_icm_mad_send_err(state, wc_status);
3799 switch (state) {
3800 case IB_CM_REQ_SENT:
3801 case IB_CM_MRA_REQ_RCVD:
3802 cm_reset_to_idle(cm_id_priv);
3803 cm_event.event = IB_CM_REQ_ERROR;
3804 break;
3805 case IB_CM_REP_SENT:
3806 case IB_CM_MRA_REP_RCVD:
3807 cm_reset_to_idle(cm_id_priv);
3808 cm_event.event = IB_CM_REP_ERROR;
3809 break;
3810 case IB_CM_DREQ_SENT:
3811 cm_enter_timewait(cm_id_priv);
3812 cm_event.event = IB_CM_DREQ_ERROR;
3813 break;
3814 case IB_CM_SIDR_REQ_SENT:
3815 cm_id_priv->id.state = IB_CM_IDLE;
3816 cm_event.event = IB_CM_SIDR_REQ_ERROR;
3817 break;
3818 default:
3819 goto out_unlock;
3820 }
3821 spin_unlock_irq(&cm_id_priv->lock);
3822 cm_event.param.send_status = wc_status;
3823
3824 /* No other events can occur on the cm_id at this point. */
3825 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event);
3826 if (ret)
3827 ib_destroy_cm_id(&cm_id_priv->id);
3828 return;
3829out_unlock:
3830 spin_unlock_irq(&cm_id_priv->lock);
3831}
3832
3833static void cm_send_handler(struct ib_mad_agent *mad_agent,
3834 struct ib_mad_send_wc *mad_send_wc)
3835{
3836 struct ib_mad_send_buf *msg = mad_send_wc->send_buf;
3837 struct cm_id_private *cm_id_priv;
3838 struct cm_port *port;
3839 u16 attr_index;
3840
3841 port = mad_agent->context;
3842 attr_index = be16_to_cpu(((struct ib_mad_hdr *)
3843 msg->mad)->attr_id) - CM_ATTR_ID_OFFSET;
3844
3845 if (msg->context[0] == CM_DIRECT_RETRY_CTX) {
3846 msg->retries = 1;
3847 cm_id_priv = NULL;
3848 } else {
3849 cm_id_priv = msg->context[0];
3850 }
3851
3852 atomic_long_add(1 + msg->retries, &port->counters[CM_XMIT][attr_index]);
3853 if (msg->retries)
3854 atomic_long_add(msg->retries,
3855 &port->counters[CM_XMIT_RETRIES][attr_index]);
3856
3857 if (cm_id_priv)
3858 cm_process_send_error(cm_id_priv, msg, mad_send_wc->status);
3859 else
3860 cm_free_msg(msg);
3861}
3862
3863static void cm_work_handler(struct work_struct *_work)
3864{
3865 struct cm_work *work = container_of(_work, struct cm_work, work.work);
3866 int ret;
3867
3868 switch (work->cm_event.event) {
3869 case IB_CM_REQ_RECEIVED:
3870 ret = cm_req_handler(work);
3871 break;
3872 case IB_CM_MRA_RECEIVED:
3873 ret = cm_mra_handler(work);
3874 break;
3875 case IB_CM_REJ_RECEIVED:
3876 ret = cm_rej_handler(work);
3877 break;
3878 case IB_CM_REP_RECEIVED:
3879 ret = cm_rep_handler(work);
3880 break;
3881 case IB_CM_RTU_RECEIVED:
3882 ret = cm_rtu_handler(work);
3883 break;
3884 case IB_CM_USER_ESTABLISHED:
3885 ret = cm_establish_handler(work);
3886 break;
3887 case IB_CM_DREQ_RECEIVED:
3888 ret = cm_dreq_handler(work);
3889 break;
3890 case IB_CM_DREP_RECEIVED:
3891 ret = cm_drep_handler(work);
3892 break;
3893 case IB_CM_SIDR_REQ_RECEIVED:
3894 ret = cm_sidr_req_handler(work);
3895 break;
3896 case IB_CM_SIDR_REP_RECEIVED:
3897 ret = cm_sidr_rep_handler(work);
3898 break;
3899 case IB_CM_LAP_RECEIVED:
3900 ret = cm_lap_handler(work);
3901 break;
3902 case IB_CM_APR_RECEIVED:
3903 ret = cm_apr_handler(work);
3904 break;
3905 case IB_CM_TIMEWAIT_EXIT:
3906 ret = cm_timewait_handler(work);
3907 break;
3908 default:
3909 trace_icm_handler_err(work->cm_event.event);
3910 ret = -EINVAL;
3911 break;
3912 }
3913 if (ret)
3914 cm_free_work(work);
3915}
3916
3917static int cm_establish(struct ib_cm_id *cm_id)
3918{
3919 struct cm_id_private *cm_id_priv;
3920 struct cm_work *work;
3921 unsigned long flags;
3922 int ret = 0;
3923 struct cm_device *cm_dev;
3924
3925 cm_dev = ib_get_client_data(cm_id->device, &cm_client);
3926 if (!cm_dev)
3927 return -ENODEV;
3928
3929 work = kmalloc(sizeof *work, GFP_ATOMIC);
3930 if (!work)
3931 return -ENOMEM;
3932
3933 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3934 spin_lock_irqsave(&cm_id_priv->lock, flags);
3935 switch (cm_id->state) {
3936 case IB_CM_REP_SENT:
3937 case IB_CM_MRA_REP_RCVD:
3938 cm_id->state = IB_CM_ESTABLISHED;
3939 break;
3940 case IB_CM_ESTABLISHED:
3941 ret = -EISCONN;
3942 break;
3943 default:
3944 trace_icm_establish_err(cm_id);
3945 ret = -EINVAL;
3946 break;
3947 }
3948 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3949
3950 if (ret) {
3951 kfree(work);
3952 goto out;
3953 }
3954
3955 /*
3956 * The CM worker thread may try to destroy the cm_id before it
3957 * can execute this work item. To prevent potential deadlock,
3958 * we need to find the cm_id once we're in the context of the
3959 * worker thread, rather than holding a reference on it.
3960 */
3961 INIT_DELAYED_WORK(&work->work, cm_work_handler);
3962 work->local_id = cm_id->local_id;
3963 work->remote_id = cm_id->remote_id;
3964 work->mad_recv_wc = NULL;
3965 work->cm_event.event = IB_CM_USER_ESTABLISHED;
3966
3967 /* Check if the device started its remove_one */
3968 spin_lock_irqsave(&cm.lock, flags);
3969 if (!cm_dev->going_down) {
3970 queue_delayed_work(cm.wq, &work->work, 0);
3971 } else {
3972 kfree(work);
3973 ret = -ENODEV;
3974 }
3975 spin_unlock_irqrestore(&cm.lock, flags);
3976
3977out:
3978 return ret;
3979}
3980
3981static int cm_migrate(struct ib_cm_id *cm_id)
3982{
3983 struct cm_id_private *cm_id_priv;
3984 unsigned long flags;
3985 int ret = 0;
3986
3987 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3988 spin_lock_irqsave(&cm_id_priv->lock, flags);
3989 if (cm_id->state == IB_CM_ESTABLISHED &&
3990 (cm_id->lap_state == IB_CM_LAP_UNINIT ||
3991 cm_id->lap_state == IB_CM_LAP_IDLE)) {
3992 cm_id->lap_state = IB_CM_LAP_IDLE;
3993 cm_id_priv->av = cm_id_priv->alt_av;
3994 } else
3995 ret = -EINVAL;
3996 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3997
3998 return ret;
3999}
4000
4001int ib_cm_notify(struct ib_cm_id *cm_id, enum ib_event_type event)
4002{
4003 int ret;
4004
4005 switch (event) {
4006 case IB_EVENT_COMM_EST:
4007 ret = cm_establish(cm_id);
4008 break;
4009 case IB_EVENT_PATH_MIG:
4010 ret = cm_migrate(cm_id);
4011 break;
4012 default:
4013 ret = -EINVAL;
4014 }
4015 return ret;
4016}
4017EXPORT_SYMBOL(ib_cm_notify);
4018
4019static void cm_recv_handler(struct ib_mad_agent *mad_agent,
4020 struct ib_mad_send_buf *send_buf,
4021 struct ib_mad_recv_wc *mad_recv_wc)
4022{
4023 struct cm_port *port = mad_agent->context;
4024 struct cm_work *work;
4025 enum ib_cm_event_type event;
4026 bool alt_path = false;
4027 u16 attr_id;
4028 int paths = 0;
4029 int going_down = 0;
4030
4031 switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) {
4032 case CM_REQ_ATTR_ID:
4033 alt_path = cm_req_has_alt_path((struct cm_req_msg *)
4034 mad_recv_wc->recv_buf.mad);
4035 paths = 1 + (alt_path != 0);
4036 event = IB_CM_REQ_RECEIVED;
4037 break;
4038 case CM_MRA_ATTR_ID:
4039 event = IB_CM_MRA_RECEIVED;
4040 break;
4041 case CM_REJ_ATTR_ID:
4042 event = IB_CM_REJ_RECEIVED;
4043 break;
4044 case CM_REP_ATTR_ID:
4045 event = IB_CM_REP_RECEIVED;
4046 break;
4047 case CM_RTU_ATTR_ID:
4048 event = IB_CM_RTU_RECEIVED;
4049 break;
4050 case CM_DREQ_ATTR_ID:
4051 event = IB_CM_DREQ_RECEIVED;
4052 break;
4053 case CM_DREP_ATTR_ID:
4054 event = IB_CM_DREP_RECEIVED;
4055 break;
4056 case CM_SIDR_REQ_ATTR_ID:
4057 event = IB_CM_SIDR_REQ_RECEIVED;
4058 break;
4059 case CM_SIDR_REP_ATTR_ID:
4060 event = IB_CM_SIDR_REP_RECEIVED;
4061 break;
4062 case CM_LAP_ATTR_ID:
4063 paths = 1;
4064 event = IB_CM_LAP_RECEIVED;
4065 break;
4066 case CM_APR_ATTR_ID:
4067 event = IB_CM_APR_RECEIVED;
4068 break;
4069 default:
4070 ib_free_recv_mad(mad_recv_wc);
4071 return;
4072 }
4073
4074 attr_id = be16_to_cpu(mad_recv_wc->recv_buf.mad->mad_hdr.attr_id);
4075 atomic_long_inc(&port->counters[CM_RECV][attr_id - CM_ATTR_ID_OFFSET]);
4076
4077 work = kmalloc(struct_size(work, path, paths), GFP_KERNEL);
4078 if (!work) {
4079 ib_free_recv_mad(mad_recv_wc);
4080 return;
4081 }
4082
4083 INIT_DELAYED_WORK(&work->work, cm_work_handler);
4084 work->cm_event.event = event;
4085 work->mad_recv_wc = mad_recv_wc;
4086 work->port = port;
4087
4088 /* Check if the device started its remove_one */
4089 spin_lock_irq(&cm.lock);
4090 if (!port->cm_dev->going_down)
4091 queue_delayed_work(cm.wq, &work->work, 0);
4092 else
4093 going_down = 1;
4094 spin_unlock_irq(&cm.lock);
4095
4096 if (going_down) {
4097 kfree(work);
4098 ib_free_recv_mad(mad_recv_wc);
4099 }
4100}
4101
4102static int cm_init_qp_init_attr(struct cm_id_private *cm_id_priv,
4103 struct ib_qp_attr *qp_attr,
4104 int *qp_attr_mask)
4105{
4106 unsigned long flags;
4107 int ret;
4108
4109 spin_lock_irqsave(&cm_id_priv->lock, flags);
4110 switch (cm_id_priv->id.state) {
4111 case IB_CM_REQ_SENT:
4112 case IB_CM_MRA_REQ_RCVD:
4113 case IB_CM_REQ_RCVD:
4114 case IB_CM_MRA_REQ_SENT:
4115 case IB_CM_REP_RCVD:
4116 case IB_CM_MRA_REP_SENT:
4117 case IB_CM_REP_SENT:
4118 case IB_CM_MRA_REP_RCVD:
4119 case IB_CM_ESTABLISHED:
4120 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS |
4121 IB_QP_PKEY_INDEX | IB_QP_PORT;
4122 qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE;
4123 if (cm_id_priv->responder_resources) {
4124 struct ib_device *ib_dev = cm_id_priv->id.device;
4125 u64 support_flush = ib_dev->attrs.device_cap_flags &
4126 (IB_DEVICE_FLUSH_GLOBAL | IB_DEVICE_FLUSH_PERSISTENT);
4127 u32 flushable = support_flush ?
4128 (IB_ACCESS_FLUSH_GLOBAL |
4129 IB_ACCESS_FLUSH_PERSISTENT) : 0;
4130
4131 qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ |
4132 IB_ACCESS_REMOTE_ATOMIC |
4133 flushable;
4134 }
4135 qp_attr->pkey_index = cm_id_priv->av.pkey_index;
4136 if (cm_id_priv->av.port)
4137 qp_attr->port_num = cm_id_priv->av.port->port_num;
4138 ret = 0;
4139 break;
4140 default:
4141 trace_icm_qp_init_err(&cm_id_priv->id);
4142 ret = -EINVAL;
4143 break;
4144 }
4145 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
4146 return ret;
4147}
4148
4149static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv,
4150 struct ib_qp_attr *qp_attr,
4151 int *qp_attr_mask)
4152{
4153 unsigned long flags;
4154 int ret;
4155
4156 spin_lock_irqsave(&cm_id_priv->lock, flags);
4157 switch (cm_id_priv->id.state) {
4158 case IB_CM_REQ_RCVD:
4159 case IB_CM_MRA_REQ_SENT:
4160 case IB_CM_REP_RCVD:
4161 case IB_CM_MRA_REP_SENT:
4162 case IB_CM_REP_SENT:
4163 case IB_CM_MRA_REP_RCVD:
4164 case IB_CM_ESTABLISHED:
4165 *qp_attr_mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU |
4166 IB_QP_DEST_QPN | IB_QP_RQ_PSN;
4167 qp_attr->ah_attr = cm_id_priv->av.ah_attr;
4168 if ((qp_attr->ah_attr.type == RDMA_AH_ATTR_TYPE_IB) &&
4169 cm_id_priv->av.dlid_datapath &&
4170 (cm_id_priv->av.dlid_datapath != 0xffff))
4171 qp_attr->ah_attr.ib.dlid = cm_id_priv->av.dlid_datapath;
4172 qp_attr->path_mtu = cm_id_priv->path_mtu;
4173 qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn);
4174 qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn);
4175 if (cm_id_priv->qp_type == IB_QPT_RC ||
4176 cm_id_priv->qp_type == IB_QPT_XRC_TGT) {
4177 *qp_attr_mask |= IB_QP_MAX_DEST_RD_ATOMIC |
4178 IB_QP_MIN_RNR_TIMER;
4179 qp_attr->max_dest_rd_atomic =
4180 cm_id_priv->responder_resources;
4181 qp_attr->min_rnr_timer = 0;
4182 }
4183 if (rdma_ah_get_dlid(&cm_id_priv->alt_av.ah_attr) &&
4184 cm_id_priv->alt_av.port) {
4185 *qp_attr_mask |= IB_QP_ALT_PATH;
4186 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
4187 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
4188 qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
4189 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
4190 }
4191 ret = 0;
4192 break;
4193 default:
4194 trace_icm_qp_rtr_err(&cm_id_priv->id);
4195 ret = -EINVAL;
4196 break;
4197 }
4198 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
4199 return ret;
4200}
4201
4202static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv,
4203 struct ib_qp_attr *qp_attr,
4204 int *qp_attr_mask)
4205{
4206 unsigned long flags;
4207 int ret;
4208
4209 spin_lock_irqsave(&cm_id_priv->lock, flags);
4210 switch (cm_id_priv->id.state) {
4211 /* Allow transition to RTS before sending REP */
4212 case IB_CM_REQ_RCVD:
4213 case IB_CM_MRA_REQ_SENT:
4214
4215 case IB_CM_REP_RCVD:
4216 case IB_CM_MRA_REP_SENT:
4217 case IB_CM_REP_SENT:
4218 case IB_CM_MRA_REP_RCVD:
4219 case IB_CM_ESTABLISHED:
4220 if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT) {
4221 *qp_attr_mask = IB_QP_STATE | IB_QP_SQ_PSN;
4222 qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn);
4223 switch (cm_id_priv->qp_type) {
4224 case IB_QPT_RC:
4225 case IB_QPT_XRC_INI:
4226 *qp_attr_mask |= IB_QP_RETRY_CNT | IB_QP_RNR_RETRY |
4227 IB_QP_MAX_QP_RD_ATOMIC;
4228 qp_attr->retry_cnt = cm_id_priv->retry_count;
4229 qp_attr->rnr_retry = cm_id_priv->rnr_retry_count;
4230 qp_attr->max_rd_atomic = cm_id_priv->initiator_depth;
4231 fallthrough;
4232 case IB_QPT_XRC_TGT:
4233 *qp_attr_mask |= IB_QP_TIMEOUT;
4234 qp_attr->timeout = cm_id_priv->av.timeout;
4235 break;
4236 default:
4237 break;
4238 }
4239 if (rdma_ah_get_dlid(&cm_id_priv->alt_av.ah_attr)) {
4240 *qp_attr_mask |= IB_QP_PATH_MIG_STATE;
4241 qp_attr->path_mig_state = IB_MIG_REARM;
4242 }
4243 } else {
4244 *qp_attr_mask = IB_QP_ALT_PATH | IB_QP_PATH_MIG_STATE;
4245 if (cm_id_priv->alt_av.port)
4246 qp_attr->alt_port_num =
4247 cm_id_priv->alt_av.port->port_num;
4248 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
4249 qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
4250 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
4251 qp_attr->path_mig_state = IB_MIG_REARM;
4252 }
4253 ret = 0;
4254 break;
4255 default:
4256 trace_icm_qp_rts_err(&cm_id_priv->id);
4257 ret = -EINVAL;
4258 break;
4259 }
4260 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
4261 return ret;
4262}
4263
4264int ib_cm_init_qp_attr(struct ib_cm_id *cm_id,
4265 struct ib_qp_attr *qp_attr,
4266 int *qp_attr_mask)
4267{
4268 struct cm_id_private *cm_id_priv;
4269 int ret;
4270
4271 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
4272 switch (qp_attr->qp_state) {
4273 case IB_QPS_INIT:
4274 ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask);
4275 break;
4276 case IB_QPS_RTR:
4277 ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask);
4278 break;
4279 case IB_QPS_RTS:
4280 ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask);
4281 break;
4282 default:
4283 ret = -EINVAL;
4284 break;
4285 }
4286 return ret;
4287}
4288EXPORT_SYMBOL(ib_cm_init_qp_attr);
4289
4290static ssize_t cm_show_counter(struct ib_device *ibdev, u32 port_num,
4291 struct ib_port_attribute *attr, char *buf)
4292{
4293 struct cm_counter_attribute *cm_attr =
4294 container_of(attr, struct cm_counter_attribute, attr);
4295 struct cm_device *cm_dev = ib_get_client_data(ibdev, &cm_client);
4296
4297 if (WARN_ON(!cm_dev))
4298 return -EINVAL;
4299
4300 return sysfs_emit(
4301 buf, "%ld\n",
4302 atomic_long_read(
4303 &cm_dev->port[port_num - 1]
4304 ->counters[cm_attr->group][cm_attr->index]));
4305}
4306
4307#define CM_COUNTER_ATTR(_name, _group, _index) \
4308 { \
4309 .attr = __ATTR(_name, 0444, cm_show_counter, NULL), \
4310 .group = _group, .index = _index \
4311 }
4312
4313#define CM_COUNTER_GROUP(_group, _name) \
4314 static struct cm_counter_attribute cm_counter_attr_##_group[] = { \
4315 CM_COUNTER_ATTR(req, _group, CM_REQ_COUNTER), \
4316 CM_COUNTER_ATTR(mra, _group, CM_MRA_COUNTER), \
4317 CM_COUNTER_ATTR(rej, _group, CM_REJ_COUNTER), \
4318 CM_COUNTER_ATTR(rep, _group, CM_REP_COUNTER), \
4319 CM_COUNTER_ATTR(rtu, _group, CM_RTU_COUNTER), \
4320 CM_COUNTER_ATTR(dreq, _group, CM_DREQ_COUNTER), \
4321 CM_COUNTER_ATTR(drep, _group, CM_DREP_COUNTER), \
4322 CM_COUNTER_ATTR(sidr_req, _group, CM_SIDR_REQ_COUNTER), \
4323 CM_COUNTER_ATTR(sidr_rep, _group, CM_SIDR_REP_COUNTER), \
4324 CM_COUNTER_ATTR(lap, _group, CM_LAP_COUNTER), \
4325 CM_COUNTER_ATTR(apr, _group, CM_APR_COUNTER), \
4326 }; \
4327 static struct attribute *cm_counter_attrs_##_group[] = { \
4328 &cm_counter_attr_##_group[0].attr.attr, \
4329 &cm_counter_attr_##_group[1].attr.attr, \
4330 &cm_counter_attr_##_group[2].attr.attr, \
4331 &cm_counter_attr_##_group[3].attr.attr, \
4332 &cm_counter_attr_##_group[4].attr.attr, \
4333 &cm_counter_attr_##_group[5].attr.attr, \
4334 &cm_counter_attr_##_group[6].attr.attr, \
4335 &cm_counter_attr_##_group[7].attr.attr, \
4336 &cm_counter_attr_##_group[8].attr.attr, \
4337 &cm_counter_attr_##_group[9].attr.attr, \
4338 &cm_counter_attr_##_group[10].attr.attr, \
4339 NULL, \
4340 }; \
4341 static const struct attribute_group cm_counter_group_##_group = { \
4342 .name = _name, \
4343 .attrs = cm_counter_attrs_##_group, \
4344 };
4345
4346CM_COUNTER_GROUP(CM_XMIT, "cm_tx_msgs")
4347CM_COUNTER_GROUP(CM_XMIT_RETRIES, "cm_tx_retries")
4348CM_COUNTER_GROUP(CM_RECV, "cm_rx_msgs")
4349CM_COUNTER_GROUP(CM_RECV_DUPLICATES, "cm_rx_duplicates")
4350
4351static const struct attribute_group *cm_counter_groups[] = {
4352 &cm_counter_group_CM_XMIT,
4353 &cm_counter_group_CM_XMIT_RETRIES,
4354 &cm_counter_group_CM_RECV,
4355 &cm_counter_group_CM_RECV_DUPLICATES,
4356 NULL,
4357};
4358
4359static int cm_add_one(struct ib_device *ib_device)
4360{
4361 struct cm_device *cm_dev;
4362 struct cm_port *port;
4363 struct ib_mad_reg_req reg_req = {
4364 .mgmt_class = IB_MGMT_CLASS_CM,
4365 .mgmt_class_version = IB_CM_CLASS_VERSION,
4366 };
4367 struct ib_port_modify port_modify = {
4368 .set_port_cap_mask = IB_PORT_CM_SUP
4369 };
4370 unsigned long flags;
4371 int ret;
4372 int count = 0;
4373 u32 i;
4374
4375 cm_dev = kzalloc(struct_size(cm_dev, port, ib_device->phys_port_cnt),
4376 GFP_KERNEL);
4377 if (!cm_dev)
4378 return -ENOMEM;
4379
4380 kref_init(&cm_dev->kref);
4381 spin_lock_init(&cm_dev->mad_agent_lock);
4382 cm_dev->ib_device = ib_device;
4383 cm_dev->ack_delay = ib_device->attrs.local_ca_ack_delay;
4384 cm_dev->going_down = 0;
4385
4386 ib_set_client_data(ib_device, &cm_client, cm_dev);
4387
4388 set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask);
4389 rdma_for_each_port (ib_device, i) {
4390 if (!rdma_cap_ib_cm(ib_device, i))
4391 continue;
4392
4393 port = kzalloc(sizeof *port, GFP_KERNEL);
4394 if (!port) {
4395 ret = -ENOMEM;
4396 goto error1;
4397 }
4398
4399 cm_dev->port[i-1] = port;
4400 port->cm_dev = cm_dev;
4401 port->port_num = i;
4402
4403 ret = ib_port_register_client_groups(ib_device, i,
4404 cm_counter_groups);
4405 if (ret)
4406 goto error1;
4407
4408 port->mad_agent = ib_register_mad_agent(ib_device, i,
4409 IB_QPT_GSI,
4410 ®_req,
4411 0,
4412 cm_send_handler,
4413 cm_recv_handler,
4414 port,
4415 0);
4416 if (IS_ERR(port->mad_agent)) {
4417 ret = PTR_ERR(port->mad_agent);
4418 goto error2;
4419 }
4420
4421 ret = ib_modify_port(ib_device, i, 0, &port_modify);
4422 if (ret)
4423 goto error3;
4424
4425 count++;
4426 }
4427
4428 if (!count) {
4429 ret = -EOPNOTSUPP;
4430 goto free;
4431 }
4432
4433 write_lock_irqsave(&cm.device_lock, flags);
4434 list_add_tail(&cm_dev->list, &cm.device_list);
4435 write_unlock_irqrestore(&cm.device_lock, flags);
4436 return 0;
4437
4438error3:
4439 ib_unregister_mad_agent(port->mad_agent);
4440error2:
4441 ib_port_unregister_client_groups(ib_device, i, cm_counter_groups);
4442error1:
4443 port_modify.set_port_cap_mask = 0;
4444 port_modify.clr_port_cap_mask = IB_PORT_CM_SUP;
4445 while (--i) {
4446 if (!rdma_cap_ib_cm(ib_device, i))
4447 continue;
4448
4449 port = cm_dev->port[i-1];
4450 ib_modify_port(ib_device, port->port_num, 0, &port_modify);
4451 ib_unregister_mad_agent(port->mad_agent);
4452 ib_port_unregister_client_groups(ib_device, i,
4453 cm_counter_groups);
4454 }
4455free:
4456 cm_device_put(cm_dev);
4457 return ret;
4458}
4459
4460static void cm_remove_one(struct ib_device *ib_device, void *client_data)
4461{
4462 struct cm_device *cm_dev = client_data;
4463 struct cm_port *port;
4464 struct ib_port_modify port_modify = {
4465 .clr_port_cap_mask = IB_PORT_CM_SUP
4466 };
4467 unsigned long flags;
4468 u32 i;
4469
4470 write_lock_irqsave(&cm.device_lock, flags);
4471 list_del(&cm_dev->list);
4472 write_unlock_irqrestore(&cm.device_lock, flags);
4473
4474 spin_lock_irq(&cm.lock);
4475 cm_dev->going_down = 1;
4476 spin_unlock_irq(&cm.lock);
4477
4478 rdma_for_each_port (ib_device, i) {
4479 struct ib_mad_agent *mad_agent;
4480
4481 if (!rdma_cap_ib_cm(ib_device, i))
4482 continue;
4483
4484 port = cm_dev->port[i-1];
4485 mad_agent = port->mad_agent;
4486 ib_modify_port(ib_device, port->port_num, 0, &port_modify);
4487 /*
4488 * We flush the queue here after the going_down set, this
4489 * verify that no new works will be queued in the recv handler,
4490 * after that we can call the unregister_mad_agent
4491 */
4492 flush_workqueue(cm.wq);
4493 /*
4494 * The above ensures no call paths from the work are running,
4495 * the remaining paths all take the mad_agent_lock.
4496 */
4497 spin_lock(&cm_dev->mad_agent_lock);
4498 port->mad_agent = NULL;
4499 spin_unlock(&cm_dev->mad_agent_lock);
4500 ib_unregister_mad_agent(mad_agent);
4501 ib_port_unregister_client_groups(ib_device, i,
4502 cm_counter_groups);
4503 }
4504
4505 cm_device_put(cm_dev);
4506}
4507
4508static int __init ib_cm_init(void)
4509{
4510 int ret;
4511
4512 INIT_LIST_HEAD(&cm.device_list);
4513 rwlock_init(&cm.device_lock);
4514 spin_lock_init(&cm.lock);
4515 cm.listen_service_table = RB_ROOT;
4516 cm.listen_service_id = be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID);
4517 cm.remote_id_table = RB_ROOT;
4518 cm.remote_qp_table = RB_ROOT;
4519 cm.remote_sidr_table = RB_ROOT;
4520 xa_init_flags(&cm.local_id_table, XA_FLAGS_ALLOC);
4521 get_random_bytes(&cm.random_id_operand, sizeof cm.random_id_operand);
4522 INIT_LIST_HEAD(&cm.timewait_list);
4523
4524 cm.wq = alloc_workqueue("ib_cm", 0, 1);
4525 if (!cm.wq) {
4526 ret = -ENOMEM;
4527 goto error2;
4528 }
4529
4530 ret = ib_register_client(&cm_client);
4531 if (ret)
4532 goto error3;
4533
4534 return 0;
4535error3:
4536 destroy_workqueue(cm.wq);
4537error2:
4538 return ret;
4539}
4540
4541static void __exit ib_cm_cleanup(void)
4542{
4543 struct cm_timewait_info *timewait_info, *tmp;
4544
4545 spin_lock_irq(&cm.lock);
4546 list_for_each_entry(timewait_info, &cm.timewait_list, list)
4547 cancel_delayed_work(&timewait_info->work.work);
4548 spin_unlock_irq(&cm.lock);
4549
4550 ib_unregister_client(&cm_client);
4551 destroy_workqueue(cm.wq);
4552
4553 list_for_each_entry_safe(timewait_info, tmp, &cm.timewait_list, list) {
4554 list_del(&timewait_info->list);
4555 kfree(timewait_info);
4556 }
4557
4558 WARN_ON(!xa_empty(&cm.local_id_table));
4559}
4560
4561module_init(ib_cm_init);
4562module_exit(ib_cm_cleanup);
1/*
2 * Copyright (c) 2004-2007 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 *
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
12 *
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
15 * conditions are met:
16 *
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer.
20 *
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 * SOFTWARE.
34 */
35
36#include <linux/completion.h>
37#include <linux/dma-mapping.h>
38#include <linux/device.h>
39#include <linux/err.h>
40#include <linux/idr.h>
41#include <linux/interrupt.h>
42#include <linux/random.h>
43#include <linux/rbtree.h>
44#include <linux/spinlock.h>
45#include <linux/slab.h>
46#include <linux/sysfs.h>
47#include <linux/workqueue.h>
48#include <linux/kdev_t.h>
49
50#include <rdma/ib_cache.h>
51#include <rdma/ib_cm.h>
52#include "cm_msgs.h"
53
54MODULE_AUTHOR("Sean Hefty");
55MODULE_DESCRIPTION("InfiniBand CM");
56MODULE_LICENSE("Dual BSD/GPL");
57
58static void cm_add_one(struct ib_device *device);
59static void cm_remove_one(struct ib_device *device);
60
61static struct ib_client cm_client = {
62 .name = "cm",
63 .add = cm_add_one,
64 .remove = cm_remove_one
65};
66
67static struct ib_cm {
68 spinlock_t lock;
69 struct list_head device_list;
70 rwlock_t device_lock;
71 struct rb_root listen_service_table;
72 u64 listen_service_id;
73 /* struct rb_root peer_service_table; todo: fix peer to peer */
74 struct rb_root remote_qp_table;
75 struct rb_root remote_id_table;
76 struct rb_root remote_sidr_table;
77 struct idr local_id_table;
78 __be32 random_id_operand;
79 struct list_head timewait_list;
80 struct workqueue_struct *wq;
81} cm;
82
83/* Counter indexes ordered by attribute ID */
84enum {
85 CM_REQ_COUNTER,
86 CM_MRA_COUNTER,
87 CM_REJ_COUNTER,
88 CM_REP_COUNTER,
89 CM_RTU_COUNTER,
90 CM_DREQ_COUNTER,
91 CM_DREP_COUNTER,
92 CM_SIDR_REQ_COUNTER,
93 CM_SIDR_REP_COUNTER,
94 CM_LAP_COUNTER,
95 CM_APR_COUNTER,
96 CM_ATTR_COUNT,
97 CM_ATTR_ID_OFFSET = 0x0010,
98};
99
100enum {
101 CM_XMIT,
102 CM_XMIT_RETRIES,
103 CM_RECV,
104 CM_RECV_DUPLICATES,
105 CM_COUNTER_GROUPS
106};
107
108static char const counter_group_names[CM_COUNTER_GROUPS]
109 [sizeof("cm_rx_duplicates")] = {
110 "cm_tx_msgs", "cm_tx_retries",
111 "cm_rx_msgs", "cm_rx_duplicates"
112};
113
114struct cm_counter_group {
115 struct kobject obj;
116 atomic_long_t counter[CM_ATTR_COUNT];
117};
118
119struct cm_counter_attribute {
120 struct attribute attr;
121 int index;
122};
123
124#define CM_COUNTER_ATTR(_name, _index) \
125struct cm_counter_attribute cm_##_name##_counter_attr = { \
126 .attr = { .name = __stringify(_name), .mode = 0444 }, \
127 .index = _index \
128}
129
130static CM_COUNTER_ATTR(req, CM_REQ_COUNTER);
131static CM_COUNTER_ATTR(mra, CM_MRA_COUNTER);
132static CM_COUNTER_ATTR(rej, CM_REJ_COUNTER);
133static CM_COUNTER_ATTR(rep, CM_REP_COUNTER);
134static CM_COUNTER_ATTR(rtu, CM_RTU_COUNTER);
135static CM_COUNTER_ATTR(dreq, CM_DREQ_COUNTER);
136static CM_COUNTER_ATTR(drep, CM_DREP_COUNTER);
137static CM_COUNTER_ATTR(sidr_req, CM_SIDR_REQ_COUNTER);
138static CM_COUNTER_ATTR(sidr_rep, CM_SIDR_REP_COUNTER);
139static CM_COUNTER_ATTR(lap, CM_LAP_COUNTER);
140static CM_COUNTER_ATTR(apr, CM_APR_COUNTER);
141
142static struct attribute *cm_counter_default_attrs[] = {
143 &cm_req_counter_attr.attr,
144 &cm_mra_counter_attr.attr,
145 &cm_rej_counter_attr.attr,
146 &cm_rep_counter_attr.attr,
147 &cm_rtu_counter_attr.attr,
148 &cm_dreq_counter_attr.attr,
149 &cm_drep_counter_attr.attr,
150 &cm_sidr_req_counter_attr.attr,
151 &cm_sidr_rep_counter_attr.attr,
152 &cm_lap_counter_attr.attr,
153 &cm_apr_counter_attr.attr,
154 NULL
155};
156
157struct cm_port {
158 struct cm_device *cm_dev;
159 struct ib_mad_agent *mad_agent;
160 struct kobject port_obj;
161 u8 port_num;
162 struct cm_counter_group counter_group[CM_COUNTER_GROUPS];
163};
164
165struct cm_device {
166 struct list_head list;
167 struct ib_device *ib_device;
168 struct device *device;
169 u8 ack_delay;
170 struct cm_port *port[0];
171};
172
173struct cm_av {
174 struct cm_port *port;
175 union ib_gid dgid;
176 struct ib_ah_attr ah_attr;
177 u16 pkey_index;
178 u8 timeout;
179};
180
181struct cm_work {
182 struct delayed_work work;
183 struct list_head list;
184 struct cm_port *port;
185 struct ib_mad_recv_wc *mad_recv_wc; /* Received MADs */
186 __be32 local_id; /* Established / timewait */
187 __be32 remote_id;
188 struct ib_cm_event cm_event;
189 struct ib_sa_path_rec path[0];
190};
191
192struct cm_timewait_info {
193 struct cm_work work; /* Must be first. */
194 struct list_head list;
195 struct rb_node remote_qp_node;
196 struct rb_node remote_id_node;
197 __be64 remote_ca_guid;
198 __be32 remote_qpn;
199 u8 inserted_remote_qp;
200 u8 inserted_remote_id;
201};
202
203struct cm_id_private {
204 struct ib_cm_id id;
205
206 struct rb_node service_node;
207 struct rb_node sidr_id_node;
208 spinlock_t lock; /* Do not acquire inside cm.lock */
209 struct completion comp;
210 atomic_t refcount;
211
212 struct ib_mad_send_buf *msg;
213 struct cm_timewait_info *timewait_info;
214 /* todo: use alternate port on send failure */
215 struct cm_av av;
216 struct cm_av alt_av;
217 struct ib_cm_compare_data *compare_data;
218
219 void *private_data;
220 __be64 tid;
221 __be32 local_qpn;
222 __be32 remote_qpn;
223 enum ib_qp_type qp_type;
224 __be32 sq_psn;
225 __be32 rq_psn;
226 int timeout_ms;
227 enum ib_mtu path_mtu;
228 __be16 pkey;
229 u8 private_data_len;
230 u8 max_cm_retries;
231 u8 peer_to_peer;
232 u8 responder_resources;
233 u8 initiator_depth;
234 u8 retry_count;
235 u8 rnr_retry_count;
236 u8 service_timeout;
237 u8 target_ack_delay;
238
239 struct list_head work_list;
240 atomic_t work_count;
241};
242
243static void cm_work_handler(struct work_struct *work);
244
245static inline void cm_deref_id(struct cm_id_private *cm_id_priv)
246{
247 if (atomic_dec_and_test(&cm_id_priv->refcount))
248 complete(&cm_id_priv->comp);
249}
250
251static int cm_alloc_msg(struct cm_id_private *cm_id_priv,
252 struct ib_mad_send_buf **msg)
253{
254 struct ib_mad_agent *mad_agent;
255 struct ib_mad_send_buf *m;
256 struct ib_ah *ah;
257
258 mad_agent = cm_id_priv->av.port->mad_agent;
259 ah = ib_create_ah(mad_agent->qp->pd, &cm_id_priv->av.ah_attr);
260 if (IS_ERR(ah))
261 return PTR_ERR(ah);
262
263 m = ib_create_send_mad(mad_agent, cm_id_priv->id.remote_cm_qpn,
264 cm_id_priv->av.pkey_index,
265 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
266 GFP_ATOMIC);
267 if (IS_ERR(m)) {
268 ib_destroy_ah(ah);
269 return PTR_ERR(m);
270 }
271
272 /* Timeout set by caller if response is expected. */
273 m->ah = ah;
274 m->retries = cm_id_priv->max_cm_retries;
275
276 atomic_inc(&cm_id_priv->refcount);
277 m->context[0] = cm_id_priv;
278 *msg = m;
279 return 0;
280}
281
282static int cm_alloc_response_msg(struct cm_port *port,
283 struct ib_mad_recv_wc *mad_recv_wc,
284 struct ib_mad_send_buf **msg)
285{
286 struct ib_mad_send_buf *m;
287 struct ib_ah *ah;
288
289 ah = ib_create_ah_from_wc(port->mad_agent->qp->pd, mad_recv_wc->wc,
290 mad_recv_wc->recv_buf.grh, port->port_num);
291 if (IS_ERR(ah))
292 return PTR_ERR(ah);
293
294 m = ib_create_send_mad(port->mad_agent, 1, mad_recv_wc->wc->pkey_index,
295 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
296 GFP_ATOMIC);
297 if (IS_ERR(m)) {
298 ib_destroy_ah(ah);
299 return PTR_ERR(m);
300 }
301 m->ah = ah;
302 *msg = m;
303 return 0;
304}
305
306static void cm_free_msg(struct ib_mad_send_buf *msg)
307{
308 ib_destroy_ah(msg->ah);
309 if (msg->context[0])
310 cm_deref_id(msg->context[0]);
311 ib_free_send_mad(msg);
312}
313
314static void * cm_copy_private_data(const void *private_data,
315 u8 private_data_len)
316{
317 void *data;
318
319 if (!private_data || !private_data_len)
320 return NULL;
321
322 data = kmemdup(private_data, private_data_len, GFP_KERNEL);
323 if (!data)
324 return ERR_PTR(-ENOMEM);
325
326 return data;
327}
328
329static void cm_set_private_data(struct cm_id_private *cm_id_priv,
330 void *private_data, u8 private_data_len)
331{
332 if (cm_id_priv->private_data && cm_id_priv->private_data_len)
333 kfree(cm_id_priv->private_data);
334
335 cm_id_priv->private_data = private_data;
336 cm_id_priv->private_data_len = private_data_len;
337}
338
339static void cm_init_av_for_response(struct cm_port *port, struct ib_wc *wc,
340 struct ib_grh *grh, struct cm_av *av)
341{
342 av->port = port;
343 av->pkey_index = wc->pkey_index;
344 ib_init_ah_from_wc(port->cm_dev->ib_device, port->port_num, wc,
345 grh, &av->ah_attr);
346}
347
348static int cm_init_av_by_path(struct ib_sa_path_rec *path, struct cm_av *av)
349{
350 struct cm_device *cm_dev;
351 struct cm_port *port = NULL;
352 unsigned long flags;
353 int ret;
354 u8 p;
355
356 read_lock_irqsave(&cm.device_lock, flags);
357 list_for_each_entry(cm_dev, &cm.device_list, list) {
358 if (!ib_find_cached_gid(cm_dev->ib_device, &path->sgid,
359 &p, NULL)) {
360 port = cm_dev->port[p-1];
361 break;
362 }
363 }
364 read_unlock_irqrestore(&cm.device_lock, flags);
365
366 if (!port)
367 return -EINVAL;
368
369 ret = ib_find_cached_pkey(cm_dev->ib_device, port->port_num,
370 be16_to_cpu(path->pkey), &av->pkey_index);
371 if (ret)
372 return ret;
373
374 av->port = port;
375 ib_init_ah_from_path(cm_dev->ib_device, port->port_num, path,
376 &av->ah_attr);
377 av->timeout = path->packet_life_time + 1;
378 return 0;
379}
380
381static int cm_alloc_id(struct cm_id_private *cm_id_priv)
382{
383 unsigned long flags;
384 int ret, id;
385 static int next_id;
386
387 do {
388 spin_lock_irqsave(&cm.lock, flags);
389 ret = idr_get_new_above(&cm.local_id_table, cm_id_priv,
390 next_id, &id);
391 if (!ret)
392 next_id = ((unsigned) id + 1) & MAX_ID_MASK;
393 spin_unlock_irqrestore(&cm.lock, flags);
394 } while( (ret == -EAGAIN) && idr_pre_get(&cm.local_id_table, GFP_KERNEL) );
395
396 cm_id_priv->id.local_id = (__force __be32)id ^ cm.random_id_operand;
397 return ret;
398}
399
400static void cm_free_id(__be32 local_id)
401{
402 spin_lock_irq(&cm.lock);
403 idr_remove(&cm.local_id_table,
404 (__force int) (local_id ^ cm.random_id_operand));
405 spin_unlock_irq(&cm.lock);
406}
407
408static struct cm_id_private * cm_get_id(__be32 local_id, __be32 remote_id)
409{
410 struct cm_id_private *cm_id_priv;
411
412 cm_id_priv = idr_find(&cm.local_id_table,
413 (__force int) (local_id ^ cm.random_id_operand));
414 if (cm_id_priv) {
415 if (cm_id_priv->id.remote_id == remote_id)
416 atomic_inc(&cm_id_priv->refcount);
417 else
418 cm_id_priv = NULL;
419 }
420
421 return cm_id_priv;
422}
423
424static struct cm_id_private * cm_acquire_id(__be32 local_id, __be32 remote_id)
425{
426 struct cm_id_private *cm_id_priv;
427
428 spin_lock_irq(&cm.lock);
429 cm_id_priv = cm_get_id(local_id, remote_id);
430 spin_unlock_irq(&cm.lock);
431
432 return cm_id_priv;
433}
434
435static void cm_mask_copy(u8 *dst, u8 *src, u8 *mask)
436{
437 int i;
438
439 for (i = 0; i < IB_CM_COMPARE_SIZE / sizeof(unsigned long); i++)
440 ((unsigned long *) dst)[i] = ((unsigned long *) src)[i] &
441 ((unsigned long *) mask)[i];
442}
443
444static int cm_compare_data(struct ib_cm_compare_data *src_data,
445 struct ib_cm_compare_data *dst_data)
446{
447 u8 src[IB_CM_COMPARE_SIZE];
448 u8 dst[IB_CM_COMPARE_SIZE];
449
450 if (!src_data || !dst_data)
451 return 0;
452
453 cm_mask_copy(src, src_data->data, dst_data->mask);
454 cm_mask_copy(dst, dst_data->data, src_data->mask);
455 return memcmp(src, dst, IB_CM_COMPARE_SIZE);
456}
457
458static int cm_compare_private_data(u8 *private_data,
459 struct ib_cm_compare_data *dst_data)
460{
461 u8 src[IB_CM_COMPARE_SIZE];
462
463 if (!dst_data)
464 return 0;
465
466 cm_mask_copy(src, private_data, dst_data->mask);
467 return memcmp(src, dst_data->data, IB_CM_COMPARE_SIZE);
468}
469
470/*
471 * Trivial helpers to strip endian annotation and compare; the
472 * endianness doesn't actually matter since we just need a stable
473 * order for the RB tree.
474 */
475static int be32_lt(__be32 a, __be32 b)
476{
477 return (__force u32) a < (__force u32) b;
478}
479
480static int be32_gt(__be32 a, __be32 b)
481{
482 return (__force u32) a > (__force u32) b;
483}
484
485static int be64_lt(__be64 a, __be64 b)
486{
487 return (__force u64) a < (__force u64) b;
488}
489
490static int be64_gt(__be64 a, __be64 b)
491{
492 return (__force u64) a > (__force u64) b;
493}
494
495static struct cm_id_private * cm_insert_listen(struct cm_id_private *cm_id_priv)
496{
497 struct rb_node **link = &cm.listen_service_table.rb_node;
498 struct rb_node *parent = NULL;
499 struct cm_id_private *cur_cm_id_priv;
500 __be64 service_id = cm_id_priv->id.service_id;
501 __be64 service_mask = cm_id_priv->id.service_mask;
502 int data_cmp;
503
504 while (*link) {
505 parent = *link;
506 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
507 service_node);
508 data_cmp = cm_compare_data(cm_id_priv->compare_data,
509 cur_cm_id_priv->compare_data);
510 if ((cur_cm_id_priv->id.service_mask & service_id) ==
511 (service_mask & cur_cm_id_priv->id.service_id) &&
512 (cm_id_priv->id.device == cur_cm_id_priv->id.device) &&
513 !data_cmp)
514 return cur_cm_id_priv;
515
516 if (cm_id_priv->id.device < cur_cm_id_priv->id.device)
517 link = &(*link)->rb_left;
518 else if (cm_id_priv->id.device > cur_cm_id_priv->id.device)
519 link = &(*link)->rb_right;
520 else if (be64_lt(service_id, cur_cm_id_priv->id.service_id))
521 link = &(*link)->rb_left;
522 else if (be64_gt(service_id, cur_cm_id_priv->id.service_id))
523 link = &(*link)->rb_right;
524 else if (data_cmp < 0)
525 link = &(*link)->rb_left;
526 else
527 link = &(*link)->rb_right;
528 }
529 rb_link_node(&cm_id_priv->service_node, parent, link);
530 rb_insert_color(&cm_id_priv->service_node, &cm.listen_service_table);
531 return NULL;
532}
533
534static struct cm_id_private * cm_find_listen(struct ib_device *device,
535 __be64 service_id,
536 u8 *private_data)
537{
538 struct rb_node *node = cm.listen_service_table.rb_node;
539 struct cm_id_private *cm_id_priv;
540 int data_cmp;
541
542 while (node) {
543 cm_id_priv = rb_entry(node, struct cm_id_private, service_node);
544 data_cmp = cm_compare_private_data(private_data,
545 cm_id_priv->compare_data);
546 if ((cm_id_priv->id.service_mask & service_id) ==
547 cm_id_priv->id.service_id &&
548 (cm_id_priv->id.device == device) && !data_cmp)
549 return cm_id_priv;
550
551 if (device < cm_id_priv->id.device)
552 node = node->rb_left;
553 else if (device > cm_id_priv->id.device)
554 node = node->rb_right;
555 else if (be64_lt(service_id, cm_id_priv->id.service_id))
556 node = node->rb_left;
557 else if (be64_gt(service_id, cm_id_priv->id.service_id))
558 node = node->rb_right;
559 else if (data_cmp < 0)
560 node = node->rb_left;
561 else
562 node = node->rb_right;
563 }
564 return NULL;
565}
566
567static struct cm_timewait_info * cm_insert_remote_id(struct cm_timewait_info
568 *timewait_info)
569{
570 struct rb_node **link = &cm.remote_id_table.rb_node;
571 struct rb_node *parent = NULL;
572 struct cm_timewait_info *cur_timewait_info;
573 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
574 __be32 remote_id = timewait_info->work.remote_id;
575
576 while (*link) {
577 parent = *link;
578 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
579 remote_id_node);
580 if (be32_lt(remote_id, cur_timewait_info->work.remote_id))
581 link = &(*link)->rb_left;
582 else if (be32_gt(remote_id, cur_timewait_info->work.remote_id))
583 link = &(*link)->rb_right;
584 else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
585 link = &(*link)->rb_left;
586 else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
587 link = &(*link)->rb_right;
588 else
589 return cur_timewait_info;
590 }
591 timewait_info->inserted_remote_id = 1;
592 rb_link_node(&timewait_info->remote_id_node, parent, link);
593 rb_insert_color(&timewait_info->remote_id_node, &cm.remote_id_table);
594 return NULL;
595}
596
597static struct cm_timewait_info * cm_find_remote_id(__be64 remote_ca_guid,
598 __be32 remote_id)
599{
600 struct rb_node *node = cm.remote_id_table.rb_node;
601 struct cm_timewait_info *timewait_info;
602
603 while (node) {
604 timewait_info = rb_entry(node, struct cm_timewait_info,
605 remote_id_node);
606 if (be32_lt(remote_id, timewait_info->work.remote_id))
607 node = node->rb_left;
608 else if (be32_gt(remote_id, timewait_info->work.remote_id))
609 node = node->rb_right;
610 else if (be64_lt(remote_ca_guid, timewait_info->remote_ca_guid))
611 node = node->rb_left;
612 else if (be64_gt(remote_ca_guid, timewait_info->remote_ca_guid))
613 node = node->rb_right;
614 else
615 return timewait_info;
616 }
617 return NULL;
618}
619
620static struct cm_timewait_info * cm_insert_remote_qpn(struct cm_timewait_info
621 *timewait_info)
622{
623 struct rb_node **link = &cm.remote_qp_table.rb_node;
624 struct rb_node *parent = NULL;
625 struct cm_timewait_info *cur_timewait_info;
626 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
627 __be32 remote_qpn = timewait_info->remote_qpn;
628
629 while (*link) {
630 parent = *link;
631 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
632 remote_qp_node);
633 if (be32_lt(remote_qpn, cur_timewait_info->remote_qpn))
634 link = &(*link)->rb_left;
635 else if (be32_gt(remote_qpn, cur_timewait_info->remote_qpn))
636 link = &(*link)->rb_right;
637 else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
638 link = &(*link)->rb_left;
639 else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
640 link = &(*link)->rb_right;
641 else
642 return cur_timewait_info;
643 }
644 timewait_info->inserted_remote_qp = 1;
645 rb_link_node(&timewait_info->remote_qp_node, parent, link);
646 rb_insert_color(&timewait_info->remote_qp_node, &cm.remote_qp_table);
647 return NULL;
648}
649
650static struct cm_id_private * cm_insert_remote_sidr(struct cm_id_private
651 *cm_id_priv)
652{
653 struct rb_node **link = &cm.remote_sidr_table.rb_node;
654 struct rb_node *parent = NULL;
655 struct cm_id_private *cur_cm_id_priv;
656 union ib_gid *port_gid = &cm_id_priv->av.dgid;
657 __be32 remote_id = cm_id_priv->id.remote_id;
658
659 while (*link) {
660 parent = *link;
661 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
662 sidr_id_node);
663 if (be32_lt(remote_id, cur_cm_id_priv->id.remote_id))
664 link = &(*link)->rb_left;
665 else if (be32_gt(remote_id, cur_cm_id_priv->id.remote_id))
666 link = &(*link)->rb_right;
667 else {
668 int cmp;
669 cmp = memcmp(port_gid, &cur_cm_id_priv->av.dgid,
670 sizeof *port_gid);
671 if (cmp < 0)
672 link = &(*link)->rb_left;
673 else if (cmp > 0)
674 link = &(*link)->rb_right;
675 else
676 return cur_cm_id_priv;
677 }
678 }
679 rb_link_node(&cm_id_priv->sidr_id_node, parent, link);
680 rb_insert_color(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
681 return NULL;
682}
683
684static void cm_reject_sidr_req(struct cm_id_private *cm_id_priv,
685 enum ib_cm_sidr_status status)
686{
687 struct ib_cm_sidr_rep_param param;
688
689 memset(¶m, 0, sizeof param);
690 param.status = status;
691 ib_send_cm_sidr_rep(&cm_id_priv->id, ¶m);
692}
693
694struct ib_cm_id *ib_create_cm_id(struct ib_device *device,
695 ib_cm_handler cm_handler,
696 void *context)
697{
698 struct cm_id_private *cm_id_priv;
699 int ret;
700
701 cm_id_priv = kzalloc(sizeof *cm_id_priv, GFP_KERNEL);
702 if (!cm_id_priv)
703 return ERR_PTR(-ENOMEM);
704
705 cm_id_priv->id.state = IB_CM_IDLE;
706 cm_id_priv->id.device = device;
707 cm_id_priv->id.cm_handler = cm_handler;
708 cm_id_priv->id.context = context;
709 cm_id_priv->id.remote_cm_qpn = 1;
710 ret = cm_alloc_id(cm_id_priv);
711 if (ret)
712 goto error;
713
714 spin_lock_init(&cm_id_priv->lock);
715 init_completion(&cm_id_priv->comp);
716 INIT_LIST_HEAD(&cm_id_priv->work_list);
717 atomic_set(&cm_id_priv->work_count, -1);
718 atomic_set(&cm_id_priv->refcount, 1);
719 return &cm_id_priv->id;
720
721error:
722 kfree(cm_id_priv);
723 return ERR_PTR(-ENOMEM);
724}
725EXPORT_SYMBOL(ib_create_cm_id);
726
727static struct cm_work * cm_dequeue_work(struct cm_id_private *cm_id_priv)
728{
729 struct cm_work *work;
730
731 if (list_empty(&cm_id_priv->work_list))
732 return NULL;
733
734 work = list_entry(cm_id_priv->work_list.next, struct cm_work, list);
735 list_del(&work->list);
736 return work;
737}
738
739static void cm_free_work(struct cm_work *work)
740{
741 if (work->mad_recv_wc)
742 ib_free_recv_mad(work->mad_recv_wc);
743 kfree(work);
744}
745
746static inline int cm_convert_to_ms(int iba_time)
747{
748 /* approximate conversion to ms from 4.096us x 2^iba_time */
749 return 1 << max(iba_time - 8, 0);
750}
751
752/*
753 * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time
754 * Because of how ack_timeout is stored, adding one doubles the timeout.
755 * To avoid large timeouts, select the max(ack_delay, life_time + 1), and
756 * increment it (round up) only if the other is within 50%.
757 */
758static u8 cm_ack_timeout(u8 ca_ack_delay, u8 packet_life_time)
759{
760 int ack_timeout = packet_life_time + 1;
761
762 if (ack_timeout >= ca_ack_delay)
763 ack_timeout += (ca_ack_delay >= (ack_timeout - 1));
764 else
765 ack_timeout = ca_ack_delay +
766 (ack_timeout >= (ca_ack_delay - 1));
767
768 return min(31, ack_timeout);
769}
770
771static void cm_cleanup_timewait(struct cm_timewait_info *timewait_info)
772{
773 if (timewait_info->inserted_remote_id) {
774 rb_erase(&timewait_info->remote_id_node, &cm.remote_id_table);
775 timewait_info->inserted_remote_id = 0;
776 }
777
778 if (timewait_info->inserted_remote_qp) {
779 rb_erase(&timewait_info->remote_qp_node, &cm.remote_qp_table);
780 timewait_info->inserted_remote_qp = 0;
781 }
782}
783
784static struct cm_timewait_info * cm_create_timewait_info(__be32 local_id)
785{
786 struct cm_timewait_info *timewait_info;
787
788 timewait_info = kzalloc(sizeof *timewait_info, GFP_KERNEL);
789 if (!timewait_info)
790 return ERR_PTR(-ENOMEM);
791
792 timewait_info->work.local_id = local_id;
793 INIT_DELAYED_WORK(&timewait_info->work.work, cm_work_handler);
794 timewait_info->work.cm_event.event = IB_CM_TIMEWAIT_EXIT;
795 return timewait_info;
796}
797
798static void cm_enter_timewait(struct cm_id_private *cm_id_priv)
799{
800 int wait_time;
801 unsigned long flags;
802
803 spin_lock_irqsave(&cm.lock, flags);
804 cm_cleanup_timewait(cm_id_priv->timewait_info);
805 list_add_tail(&cm_id_priv->timewait_info->list, &cm.timewait_list);
806 spin_unlock_irqrestore(&cm.lock, flags);
807
808 /*
809 * The cm_id could be destroyed by the user before we exit timewait.
810 * To protect against this, we search for the cm_id after exiting
811 * timewait before notifying the user that we've exited timewait.
812 */
813 cm_id_priv->id.state = IB_CM_TIMEWAIT;
814 wait_time = cm_convert_to_ms(cm_id_priv->av.timeout);
815 queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work,
816 msecs_to_jiffies(wait_time));
817 cm_id_priv->timewait_info = NULL;
818}
819
820static void cm_reset_to_idle(struct cm_id_private *cm_id_priv)
821{
822 unsigned long flags;
823
824 cm_id_priv->id.state = IB_CM_IDLE;
825 if (cm_id_priv->timewait_info) {
826 spin_lock_irqsave(&cm.lock, flags);
827 cm_cleanup_timewait(cm_id_priv->timewait_info);
828 spin_unlock_irqrestore(&cm.lock, flags);
829 kfree(cm_id_priv->timewait_info);
830 cm_id_priv->timewait_info = NULL;
831 }
832}
833
834static void cm_destroy_id(struct ib_cm_id *cm_id, int err)
835{
836 struct cm_id_private *cm_id_priv;
837 struct cm_work *work;
838
839 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
840retest:
841 spin_lock_irq(&cm_id_priv->lock);
842 switch (cm_id->state) {
843 case IB_CM_LISTEN:
844 cm_id->state = IB_CM_IDLE;
845 spin_unlock_irq(&cm_id_priv->lock);
846 spin_lock_irq(&cm.lock);
847 rb_erase(&cm_id_priv->service_node, &cm.listen_service_table);
848 spin_unlock_irq(&cm.lock);
849 break;
850 case IB_CM_SIDR_REQ_SENT:
851 cm_id->state = IB_CM_IDLE;
852 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
853 spin_unlock_irq(&cm_id_priv->lock);
854 break;
855 case IB_CM_SIDR_REQ_RCVD:
856 spin_unlock_irq(&cm_id_priv->lock);
857 cm_reject_sidr_req(cm_id_priv, IB_SIDR_REJECT);
858 break;
859 case IB_CM_REQ_SENT:
860 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
861 spin_unlock_irq(&cm_id_priv->lock);
862 ib_send_cm_rej(cm_id, IB_CM_REJ_TIMEOUT,
863 &cm_id_priv->id.device->node_guid,
864 sizeof cm_id_priv->id.device->node_guid,
865 NULL, 0);
866 break;
867 case IB_CM_REQ_RCVD:
868 if (err == -ENOMEM) {
869 /* Do not reject to allow future retries. */
870 cm_reset_to_idle(cm_id_priv);
871 spin_unlock_irq(&cm_id_priv->lock);
872 } else {
873 spin_unlock_irq(&cm_id_priv->lock);
874 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
875 NULL, 0, NULL, 0);
876 }
877 break;
878 case IB_CM_MRA_REQ_RCVD:
879 case IB_CM_REP_SENT:
880 case IB_CM_MRA_REP_RCVD:
881 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
882 /* Fall through */
883 case IB_CM_MRA_REQ_SENT:
884 case IB_CM_REP_RCVD:
885 case IB_CM_MRA_REP_SENT:
886 spin_unlock_irq(&cm_id_priv->lock);
887 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
888 NULL, 0, NULL, 0);
889 break;
890 case IB_CM_ESTABLISHED:
891 spin_unlock_irq(&cm_id_priv->lock);
892 ib_send_cm_dreq(cm_id, NULL, 0);
893 goto retest;
894 case IB_CM_DREQ_SENT:
895 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
896 cm_enter_timewait(cm_id_priv);
897 spin_unlock_irq(&cm_id_priv->lock);
898 break;
899 case IB_CM_DREQ_RCVD:
900 spin_unlock_irq(&cm_id_priv->lock);
901 ib_send_cm_drep(cm_id, NULL, 0);
902 break;
903 default:
904 spin_unlock_irq(&cm_id_priv->lock);
905 break;
906 }
907
908 cm_free_id(cm_id->local_id);
909 cm_deref_id(cm_id_priv);
910 wait_for_completion(&cm_id_priv->comp);
911 while ((work = cm_dequeue_work(cm_id_priv)) != NULL)
912 cm_free_work(work);
913 kfree(cm_id_priv->compare_data);
914 kfree(cm_id_priv->private_data);
915 kfree(cm_id_priv);
916}
917
918void ib_destroy_cm_id(struct ib_cm_id *cm_id)
919{
920 cm_destroy_id(cm_id, 0);
921}
922EXPORT_SYMBOL(ib_destroy_cm_id);
923
924int ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id, __be64 service_mask,
925 struct ib_cm_compare_data *compare_data)
926{
927 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
928 unsigned long flags;
929 int ret = 0;
930
931 service_mask = service_mask ? service_mask : ~cpu_to_be64(0);
932 service_id &= service_mask;
933 if ((service_id & IB_SERVICE_ID_AGN_MASK) == IB_CM_ASSIGN_SERVICE_ID &&
934 (service_id != IB_CM_ASSIGN_SERVICE_ID))
935 return -EINVAL;
936
937 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
938 if (cm_id->state != IB_CM_IDLE)
939 return -EINVAL;
940
941 if (compare_data) {
942 cm_id_priv->compare_data = kzalloc(sizeof *compare_data,
943 GFP_KERNEL);
944 if (!cm_id_priv->compare_data)
945 return -ENOMEM;
946 cm_mask_copy(cm_id_priv->compare_data->data,
947 compare_data->data, compare_data->mask);
948 memcpy(cm_id_priv->compare_data->mask, compare_data->mask,
949 IB_CM_COMPARE_SIZE);
950 }
951
952 cm_id->state = IB_CM_LISTEN;
953
954 spin_lock_irqsave(&cm.lock, flags);
955 if (service_id == IB_CM_ASSIGN_SERVICE_ID) {
956 cm_id->service_id = cpu_to_be64(cm.listen_service_id++);
957 cm_id->service_mask = ~cpu_to_be64(0);
958 } else {
959 cm_id->service_id = service_id;
960 cm_id->service_mask = service_mask;
961 }
962 cur_cm_id_priv = cm_insert_listen(cm_id_priv);
963 spin_unlock_irqrestore(&cm.lock, flags);
964
965 if (cur_cm_id_priv) {
966 cm_id->state = IB_CM_IDLE;
967 kfree(cm_id_priv->compare_data);
968 cm_id_priv->compare_data = NULL;
969 ret = -EBUSY;
970 }
971 return ret;
972}
973EXPORT_SYMBOL(ib_cm_listen);
974
975static __be64 cm_form_tid(struct cm_id_private *cm_id_priv,
976 enum cm_msg_sequence msg_seq)
977{
978 u64 hi_tid, low_tid;
979
980 hi_tid = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32;
981 low_tid = (u64) ((__force u32)cm_id_priv->id.local_id |
982 (msg_seq << 30));
983 return cpu_to_be64(hi_tid | low_tid);
984}
985
986static void cm_format_mad_hdr(struct ib_mad_hdr *hdr,
987 __be16 attr_id, __be64 tid)
988{
989 hdr->base_version = IB_MGMT_BASE_VERSION;
990 hdr->mgmt_class = IB_MGMT_CLASS_CM;
991 hdr->class_version = IB_CM_CLASS_VERSION;
992 hdr->method = IB_MGMT_METHOD_SEND;
993 hdr->attr_id = attr_id;
994 hdr->tid = tid;
995}
996
997static void cm_format_req(struct cm_req_msg *req_msg,
998 struct cm_id_private *cm_id_priv,
999 struct ib_cm_req_param *param)
1000{
1001 struct ib_sa_path_rec *pri_path = param->primary_path;
1002 struct ib_sa_path_rec *alt_path = param->alternate_path;
1003
1004 cm_format_mad_hdr(&req_msg->hdr, CM_REQ_ATTR_ID,
1005 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_REQ));
1006
1007 req_msg->local_comm_id = cm_id_priv->id.local_id;
1008 req_msg->service_id = param->service_id;
1009 req_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
1010 cm_req_set_local_qpn(req_msg, cpu_to_be32(param->qp_num));
1011 cm_req_set_resp_res(req_msg, param->responder_resources);
1012 cm_req_set_init_depth(req_msg, param->initiator_depth);
1013 cm_req_set_remote_resp_timeout(req_msg,
1014 param->remote_cm_response_timeout);
1015 cm_req_set_qp_type(req_msg, param->qp_type);
1016 cm_req_set_flow_ctrl(req_msg, param->flow_control);
1017 cm_req_set_starting_psn(req_msg, cpu_to_be32(param->starting_psn));
1018 cm_req_set_local_resp_timeout(req_msg,
1019 param->local_cm_response_timeout);
1020 cm_req_set_retry_count(req_msg, param->retry_count);
1021 req_msg->pkey = param->primary_path->pkey;
1022 cm_req_set_path_mtu(req_msg, param->primary_path->mtu);
1023 cm_req_set_rnr_retry_count(req_msg, param->rnr_retry_count);
1024 cm_req_set_max_cm_retries(req_msg, param->max_cm_retries);
1025 cm_req_set_srq(req_msg, param->srq);
1026
1027 if (pri_path->hop_limit <= 1) {
1028 req_msg->primary_local_lid = pri_path->slid;
1029 req_msg->primary_remote_lid = pri_path->dlid;
1030 } else {
1031 /* Work-around until there's a way to obtain remote LID info */
1032 req_msg->primary_local_lid = IB_LID_PERMISSIVE;
1033 req_msg->primary_remote_lid = IB_LID_PERMISSIVE;
1034 }
1035 req_msg->primary_local_gid = pri_path->sgid;
1036 req_msg->primary_remote_gid = pri_path->dgid;
1037 cm_req_set_primary_flow_label(req_msg, pri_path->flow_label);
1038 cm_req_set_primary_packet_rate(req_msg, pri_path->rate);
1039 req_msg->primary_traffic_class = pri_path->traffic_class;
1040 req_msg->primary_hop_limit = pri_path->hop_limit;
1041 cm_req_set_primary_sl(req_msg, pri_path->sl);
1042 cm_req_set_primary_subnet_local(req_msg, (pri_path->hop_limit <= 1));
1043 cm_req_set_primary_local_ack_timeout(req_msg,
1044 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
1045 pri_path->packet_life_time));
1046
1047 if (alt_path) {
1048 if (alt_path->hop_limit <= 1) {
1049 req_msg->alt_local_lid = alt_path->slid;
1050 req_msg->alt_remote_lid = alt_path->dlid;
1051 } else {
1052 req_msg->alt_local_lid = IB_LID_PERMISSIVE;
1053 req_msg->alt_remote_lid = IB_LID_PERMISSIVE;
1054 }
1055 req_msg->alt_local_gid = alt_path->sgid;
1056 req_msg->alt_remote_gid = alt_path->dgid;
1057 cm_req_set_alt_flow_label(req_msg,
1058 alt_path->flow_label);
1059 cm_req_set_alt_packet_rate(req_msg, alt_path->rate);
1060 req_msg->alt_traffic_class = alt_path->traffic_class;
1061 req_msg->alt_hop_limit = alt_path->hop_limit;
1062 cm_req_set_alt_sl(req_msg, alt_path->sl);
1063 cm_req_set_alt_subnet_local(req_msg, (alt_path->hop_limit <= 1));
1064 cm_req_set_alt_local_ack_timeout(req_msg,
1065 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
1066 alt_path->packet_life_time));
1067 }
1068
1069 if (param->private_data && param->private_data_len)
1070 memcpy(req_msg->private_data, param->private_data,
1071 param->private_data_len);
1072}
1073
1074static int cm_validate_req_param(struct ib_cm_req_param *param)
1075{
1076 /* peer-to-peer not supported */
1077 if (param->peer_to_peer)
1078 return -EINVAL;
1079
1080 if (!param->primary_path)
1081 return -EINVAL;
1082
1083 if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC)
1084 return -EINVAL;
1085
1086 if (param->private_data &&
1087 param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE)
1088 return -EINVAL;
1089
1090 if (param->alternate_path &&
1091 (param->alternate_path->pkey != param->primary_path->pkey ||
1092 param->alternate_path->mtu != param->primary_path->mtu))
1093 return -EINVAL;
1094
1095 return 0;
1096}
1097
1098int ib_send_cm_req(struct ib_cm_id *cm_id,
1099 struct ib_cm_req_param *param)
1100{
1101 struct cm_id_private *cm_id_priv;
1102 struct cm_req_msg *req_msg;
1103 unsigned long flags;
1104 int ret;
1105
1106 ret = cm_validate_req_param(param);
1107 if (ret)
1108 return ret;
1109
1110 /* Verify that we're not in timewait. */
1111 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1112 spin_lock_irqsave(&cm_id_priv->lock, flags);
1113 if (cm_id->state != IB_CM_IDLE) {
1114 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1115 ret = -EINVAL;
1116 goto out;
1117 }
1118 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1119
1120 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1121 id.local_id);
1122 if (IS_ERR(cm_id_priv->timewait_info)) {
1123 ret = PTR_ERR(cm_id_priv->timewait_info);
1124 goto out;
1125 }
1126
1127 ret = cm_init_av_by_path(param->primary_path, &cm_id_priv->av);
1128 if (ret)
1129 goto error1;
1130 if (param->alternate_path) {
1131 ret = cm_init_av_by_path(param->alternate_path,
1132 &cm_id_priv->alt_av);
1133 if (ret)
1134 goto error1;
1135 }
1136 cm_id->service_id = param->service_id;
1137 cm_id->service_mask = ~cpu_to_be64(0);
1138 cm_id_priv->timeout_ms = cm_convert_to_ms(
1139 param->primary_path->packet_life_time) * 2 +
1140 cm_convert_to_ms(
1141 param->remote_cm_response_timeout);
1142 cm_id_priv->max_cm_retries = param->max_cm_retries;
1143 cm_id_priv->initiator_depth = param->initiator_depth;
1144 cm_id_priv->responder_resources = param->responder_resources;
1145 cm_id_priv->retry_count = param->retry_count;
1146 cm_id_priv->path_mtu = param->primary_path->mtu;
1147 cm_id_priv->pkey = param->primary_path->pkey;
1148 cm_id_priv->qp_type = param->qp_type;
1149
1150 ret = cm_alloc_msg(cm_id_priv, &cm_id_priv->msg);
1151 if (ret)
1152 goto error1;
1153
1154 req_msg = (struct cm_req_msg *) cm_id_priv->msg->mad;
1155 cm_format_req(req_msg, cm_id_priv, param);
1156 cm_id_priv->tid = req_msg->hdr.tid;
1157 cm_id_priv->msg->timeout_ms = cm_id_priv->timeout_ms;
1158 cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT;
1159
1160 cm_id_priv->local_qpn = cm_req_get_local_qpn(req_msg);
1161 cm_id_priv->rq_psn = cm_req_get_starting_psn(req_msg);
1162
1163 spin_lock_irqsave(&cm_id_priv->lock, flags);
1164 ret = ib_post_send_mad(cm_id_priv->msg, NULL);
1165 if (ret) {
1166 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1167 goto error2;
1168 }
1169 BUG_ON(cm_id->state != IB_CM_IDLE);
1170 cm_id->state = IB_CM_REQ_SENT;
1171 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1172 return 0;
1173
1174error2: cm_free_msg(cm_id_priv->msg);
1175error1: kfree(cm_id_priv->timewait_info);
1176out: return ret;
1177}
1178EXPORT_SYMBOL(ib_send_cm_req);
1179
1180static int cm_issue_rej(struct cm_port *port,
1181 struct ib_mad_recv_wc *mad_recv_wc,
1182 enum ib_cm_rej_reason reason,
1183 enum cm_msg_response msg_rejected,
1184 void *ari, u8 ari_length)
1185{
1186 struct ib_mad_send_buf *msg = NULL;
1187 struct cm_rej_msg *rej_msg, *rcv_msg;
1188 int ret;
1189
1190 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
1191 if (ret)
1192 return ret;
1193
1194 /* We just need common CM header information. Cast to any message. */
1195 rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad;
1196 rej_msg = (struct cm_rej_msg *) msg->mad;
1197
1198 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid);
1199 rej_msg->remote_comm_id = rcv_msg->local_comm_id;
1200 rej_msg->local_comm_id = rcv_msg->remote_comm_id;
1201 cm_rej_set_msg_rejected(rej_msg, msg_rejected);
1202 rej_msg->reason = cpu_to_be16(reason);
1203
1204 if (ari && ari_length) {
1205 cm_rej_set_reject_info_len(rej_msg, ari_length);
1206 memcpy(rej_msg->ari, ari, ari_length);
1207 }
1208
1209 ret = ib_post_send_mad(msg, NULL);
1210 if (ret)
1211 cm_free_msg(msg);
1212
1213 return ret;
1214}
1215
1216static inline int cm_is_active_peer(__be64 local_ca_guid, __be64 remote_ca_guid,
1217 __be32 local_qpn, __be32 remote_qpn)
1218{
1219 return (be64_to_cpu(local_ca_guid) > be64_to_cpu(remote_ca_guid) ||
1220 ((local_ca_guid == remote_ca_guid) &&
1221 (be32_to_cpu(local_qpn) > be32_to_cpu(remote_qpn))));
1222}
1223
1224static void cm_format_paths_from_req(struct cm_req_msg *req_msg,
1225 struct ib_sa_path_rec *primary_path,
1226 struct ib_sa_path_rec *alt_path)
1227{
1228 memset(primary_path, 0, sizeof *primary_path);
1229 primary_path->dgid = req_msg->primary_local_gid;
1230 primary_path->sgid = req_msg->primary_remote_gid;
1231 primary_path->dlid = req_msg->primary_local_lid;
1232 primary_path->slid = req_msg->primary_remote_lid;
1233 primary_path->flow_label = cm_req_get_primary_flow_label(req_msg);
1234 primary_path->hop_limit = req_msg->primary_hop_limit;
1235 primary_path->traffic_class = req_msg->primary_traffic_class;
1236 primary_path->reversible = 1;
1237 primary_path->pkey = req_msg->pkey;
1238 primary_path->sl = cm_req_get_primary_sl(req_msg);
1239 primary_path->mtu_selector = IB_SA_EQ;
1240 primary_path->mtu = cm_req_get_path_mtu(req_msg);
1241 primary_path->rate_selector = IB_SA_EQ;
1242 primary_path->rate = cm_req_get_primary_packet_rate(req_msg);
1243 primary_path->packet_life_time_selector = IB_SA_EQ;
1244 primary_path->packet_life_time =
1245 cm_req_get_primary_local_ack_timeout(req_msg);
1246 primary_path->packet_life_time -= (primary_path->packet_life_time > 0);
1247
1248 if (req_msg->alt_local_lid) {
1249 memset(alt_path, 0, sizeof *alt_path);
1250 alt_path->dgid = req_msg->alt_local_gid;
1251 alt_path->sgid = req_msg->alt_remote_gid;
1252 alt_path->dlid = req_msg->alt_local_lid;
1253 alt_path->slid = req_msg->alt_remote_lid;
1254 alt_path->flow_label = cm_req_get_alt_flow_label(req_msg);
1255 alt_path->hop_limit = req_msg->alt_hop_limit;
1256 alt_path->traffic_class = req_msg->alt_traffic_class;
1257 alt_path->reversible = 1;
1258 alt_path->pkey = req_msg->pkey;
1259 alt_path->sl = cm_req_get_alt_sl(req_msg);
1260 alt_path->mtu_selector = IB_SA_EQ;
1261 alt_path->mtu = cm_req_get_path_mtu(req_msg);
1262 alt_path->rate_selector = IB_SA_EQ;
1263 alt_path->rate = cm_req_get_alt_packet_rate(req_msg);
1264 alt_path->packet_life_time_selector = IB_SA_EQ;
1265 alt_path->packet_life_time =
1266 cm_req_get_alt_local_ack_timeout(req_msg);
1267 alt_path->packet_life_time -= (alt_path->packet_life_time > 0);
1268 }
1269}
1270
1271static void cm_format_req_event(struct cm_work *work,
1272 struct cm_id_private *cm_id_priv,
1273 struct ib_cm_id *listen_id)
1274{
1275 struct cm_req_msg *req_msg;
1276 struct ib_cm_req_event_param *param;
1277
1278 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1279 param = &work->cm_event.param.req_rcvd;
1280 param->listen_id = listen_id;
1281 param->port = cm_id_priv->av.port->port_num;
1282 param->primary_path = &work->path[0];
1283 if (req_msg->alt_local_lid)
1284 param->alternate_path = &work->path[1];
1285 else
1286 param->alternate_path = NULL;
1287 param->remote_ca_guid = req_msg->local_ca_guid;
1288 param->remote_qkey = be32_to_cpu(req_msg->local_qkey);
1289 param->remote_qpn = be32_to_cpu(cm_req_get_local_qpn(req_msg));
1290 param->qp_type = cm_req_get_qp_type(req_msg);
1291 param->starting_psn = be32_to_cpu(cm_req_get_starting_psn(req_msg));
1292 param->responder_resources = cm_req_get_init_depth(req_msg);
1293 param->initiator_depth = cm_req_get_resp_res(req_msg);
1294 param->local_cm_response_timeout =
1295 cm_req_get_remote_resp_timeout(req_msg);
1296 param->flow_control = cm_req_get_flow_ctrl(req_msg);
1297 param->remote_cm_response_timeout =
1298 cm_req_get_local_resp_timeout(req_msg);
1299 param->retry_count = cm_req_get_retry_count(req_msg);
1300 param->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1301 param->srq = cm_req_get_srq(req_msg);
1302 work->cm_event.private_data = &req_msg->private_data;
1303}
1304
1305static void cm_process_work(struct cm_id_private *cm_id_priv,
1306 struct cm_work *work)
1307{
1308 int ret;
1309
1310 /* We will typically only have the current event to report. */
1311 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
1312 cm_free_work(work);
1313
1314 while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) {
1315 spin_lock_irq(&cm_id_priv->lock);
1316 work = cm_dequeue_work(cm_id_priv);
1317 spin_unlock_irq(&cm_id_priv->lock);
1318 BUG_ON(!work);
1319 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id,
1320 &work->cm_event);
1321 cm_free_work(work);
1322 }
1323 cm_deref_id(cm_id_priv);
1324 if (ret)
1325 cm_destroy_id(&cm_id_priv->id, ret);
1326}
1327
1328static void cm_format_mra(struct cm_mra_msg *mra_msg,
1329 struct cm_id_private *cm_id_priv,
1330 enum cm_msg_response msg_mraed, u8 service_timeout,
1331 const void *private_data, u8 private_data_len)
1332{
1333 cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid);
1334 cm_mra_set_msg_mraed(mra_msg, msg_mraed);
1335 mra_msg->local_comm_id = cm_id_priv->id.local_id;
1336 mra_msg->remote_comm_id = cm_id_priv->id.remote_id;
1337 cm_mra_set_service_timeout(mra_msg, service_timeout);
1338
1339 if (private_data && private_data_len)
1340 memcpy(mra_msg->private_data, private_data, private_data_len);
1341}
1342
1343static void cm_format_rej(struct cm_rej_msg *rej_msg,
1344 struct cm_id_private *cm_id_priv,
1345 enum ib_cm_rej_reason reason,
1346 void *ari,
1347 u8 ari_length,
1348 const void *private_data,
1349 u8 private_data_len)
1350{
1351 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid);
1352 rej_msg->remote_comm_id = cm_id_priv->id.remote_id;
1353
1354 switch(cm_id_priv->id.state) {
1355 case IB_CM_REQ_RCVD:
1356 rej_msg->local_comm_id = 0;
1357 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1358 break;
1359 case IB_CM_MRA_REQ_SENT:
1360 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1361 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1362 break;
1363 case IB_CM_REP_RCVD:
1364 case IB_CM_MRA_REP_SENT:
1365 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1366 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REP);
1367 break;
1368 default:
1369 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1370 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_OTHER);
1371 break;
1372 }
1373
1374 rej_msg->reason = cpu_to_be16(reason);
1375 if (ari && ari_length) {
1376 cm_rej_set_reject_info_len(rej_msg, ari_length);
1377 memcpy(rej_msg->ari, ari, ari_length);
1378 }
1379
1380 if (private_data && private_data_len)
1381 memcpy(rej_msg->private_data, private_data, private_data_len);
1382}
1383
1384static void cm_dup_req_handler(struct cm_work *work,
1385 struct cm_id_private *cm_id_priv)
1386{
1387 struct ib_mad_send_buf *msg = NULL;
1388 int ret;
1389
1390 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1391 counter[CM_REQ_COUNTER]);
1392
1393 /* Quick state check to discard duplicate REQs. */
1394 if (cm_id_priv->id.state == IB_CM_REQ_RCVD)
1395 return;
1396
1397 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1398 if (ret)
1399 return;
1400
1401 spin_lock_irq(&cm_id_priv->lock);
1402 switch (cm_id_priv->id.state) {
1403 case IB_CM_MRA_REQ_SENT:
1404 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1405 CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout,
1406 cm_id_priv->private_data,
1407 cm_id_priv->private_data_len);
1408 break;
1409 case IB_CM_TIMEWAIT:
1410 cm_format_rej((struct cm_rej_msg *) msg->mad, cm_id_priv,
1411 IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0);
1412 break;
1413 default:
1414 goto unlock;
1415 }
1416 spin_unlock_irq(&cm_id_priv->lock);
1417
1418 ret = ib_post_send_mad(msg, NULL);
1419 if (ret)
1420 goto free;
1421 return;
1422
1423unlock: spin_unlock_irq(&cm_id_priv->lock);
1424free: cm_free_msg(msg);
1425}
1426
1427static struct cm_id_private * cm_match_req(struct cm_work *work,
1428 struct cm_id_private *cm_id_priv)
1429{
1430 struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv;
1431 struct cm_timewait_info *timewait_info;
1432 struct cm_req_msg *req_msg;
1433
1434 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1435
1436 /* Check for possible duplicate REQ. */
1437 spin_lock_irq(&cm.lock);
1438 timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info);
1439 if (timewait_info) {
1440 cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
1441 timewait_info->work.remote_id);
1442 spin_unlock_irq(&cm.lock);
1443 if (cur_cm_id_priv) {
1444 cm_dup_req_handler(work, cur_cm_id_priv);
1445 cm_deref_id(cur_cm_id_priv);
1446 }
1447 return NULL;
1448 }
1449
1450 /* Check for stale connections. */
1451 timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
1452 if (timewait_info) {
1453 cm_cleanup_timewait(cm_id_priv->timewait_info);
1454 spin_unlock_irq(&cm.lock);
1455 cm_issue_rej(work->port, work->mad_recv_wc,
1456 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ,
1457 NULL, 0);
1458 return NULL;
1459 }
1460
1461 /* Find matching listen request. */
1462 listen_cm_id_priv = cm_find_listen(cm_id_priv->id.device,
1463 req_msg->service_id,
1464 req_msg->private_data);
1465 if (!listen_cm_id_priv) {
1466 cm_cleanup_timewait(cm_id_priv->timewait_info);
1467 spin_unlock_irq(&cm.lock);
1468 cm_issue_rej(work->port, work->mad_recv_wc,
1469 IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ,
1470 NULL, 0);
1471 goto out;
1472 }
1473 atomic_inc(&listen_cm_id_priv->refcount);
1474 atomic_inc(&cm_id_priv->refcount);
1475 cm_id_priv->id.state = IB_CM_REQ_RCVD;
1476 atomic_inc(&cm_id_priv->work_count);
1477 spin_unlock_irq(&cm.lock);
1478out:
1479 return listen_cm_id_priv;
1480}
1481
1482/*
1483 * Work-around for inter-subnet connections. If the LIDs are permissive,
1484 * we need to override the LID/SL data in the REQ with the LID information
1485 * in the work completion.
1486 */
1487static void cm_process_routed_req(struct cm_req_msg *req_msg, struct ib_wc *wc)
1488{
1489 if (!cm_req_get_primary_subnet_local(req_msg)) {
1490 if (req_msg->primary_local_lid == IB_LID_PERMISSIVE) {
1491 req_msg->primary_local_lid = cpu_to_be16(wc->slid);
1492 cm_req_set_primary_sl(req_msg, wc->sl);
1493 }
1494
1495 if (req_msg->primary_remote_lid == IB_LID_PERMISSIVE)
1496 req_msg->primary_remote_lid = cpu_to_be16(wc->dlid_path_bits);
1497 }
1498
1499 if (!cm_req_get_alt_subnet_local(req_msg)) {
1500 if (req_msg->alt_local_lid == IB_LID_PERMISSIVE) {
1501 req_msg->alt_local_lid = cpu_to_be16(wc->slid);
1502 cm_req_set_alt_sl(req_msg, wc->sl);
1503 }
1504
1505 if (req_msg->alt_remote_lid == IB_LID_PERMISSIVE)
1506 req_msg->alt_remote_lid = cpu_to_be16(wc->dlid_path_bits);
1507 }
1508}
1509
1510static int cm_req_handler(struct cm_work *work)
1511{
1512 struct ib_cm_id *cm_id;
1513 struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
1514 struct cm_req_msg *req_msg;
1515 int ret;
1516
1517 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1518
1519 cm_id = ib_create_cm_id(work->port->cm_dev->ib_device, NULL, NULL);
1520 if (IS_ERR(cm_id))
1521 return PTR_ERR(cm_id);
1522
1523 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1524 cm_id_priv->id.remote_id = req_msg->local_comm_id;
1525 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
1526 work->mad_recv_wc->recv_buf.grh,
1527 &cm_id_priv->av);
1528 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1529 id.local_id);
1530 if (IS_ERR(cm_id_priv->timewait_info)) {
1531 ret = PTR_ERR(cm_id_priv->timewait_info);
1532 goto destroy;
1533 }
1534 cm_id_priv->timewait_info->work.remote_id = req_msg->local_comm_id;
1535 cm_id_priv->timewait_info->remote_ca_guid = req_msg->local_ca_guid;
1536 cm_id_priv->timewait_info->remote_qpn = cm_req_get_local_qpn(req_msg);
1537
1538 listen_cm_id_priv = cm_match_req(work, cm_id_priv);
1539 if (!listen_cm_id_priv) {
1540 ret = -EINVAL;
1541 kfree(cm_id_priv->timewait_info);
1542 goto destroy;
1543 }
1544
1545 cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
1546 cm_id_priv->id.context = listen_cm_id_priv->id.context;
1547 cm_id_priv->id.service_id = req_msg->service_id;
1548 cm_id_priv->id.service_mask = ~cpu_to_be64(0);
1549
1550 cm_process_routed_req(req_msg, work->mad_recv_wc->wc);
1551 cm_format_paths_from_req(req_msg, &work->path[0], &work->path[1]);
1552 ret = cm_init_av_by_path(&work->path[0], &cm_id_priv->av);
1553 if (ret) {
1554 ib_get_cached_gid(work->port->cm_dev->ib_device,
1555 work->port->port_num, 0, &work->path[0].sgid);
1556 ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_GID,
1557 &work->path[0].sgid, sizeof work->path[0].sgid,
1558 NULL, 0);
1559 goto rejected;
1560 }
1561 if (req_msg->alt_local_lid) {
1562 ret = cm_init_av_by_path(&work->path[1], &cm_id_priv->alt_av);
1563 if (ret) {
1564 ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_ALT_GID,
1565 &work->path[0].sgid,
1566 sizeof work->path[0].sgid, NULL, 0);
1567 goto rejected;
1568 }
1569 }
1570 cm_id_priv->tid = req_msg->hdr.tid;
1571 cm_id_priv->timeout_ms = cm_convert_to_ms(
1572 cm_req_get_local_resp_timeout(req_msg));
1573 cm_id_priv->max_cm_retries = cm_req_get_max_cm_retries(req_msg);
1574 cm_id_priv->remote_qpn = cm_req_get_local_qpn(req_msg);
1575 cm_id_priv->initiator_depth = cm_req_get_resp_res(req_msg);
1576 cm_id_priv->responder_resources = cm_req_get_init_depth(req_msg);
1577 cm_id_priv->path_mtu = cm_req_get_path_mtu(req_msg);
1578 cm_id_priv->pkey = req_msg->pkey;
1579 cm_id_priv->sq_psn = cm_req_get_starting_psn(req_msg);
1580 cm_id_priv->retry_count = cm_req_get_retry_count(req_msg);
1581 cm_id_priv->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1582 cm_id_priv->qp_type = cm_req_get_qp_type(req_msg);
1583
1584 cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
1585 cm_process_work(cm_id_priv, work);
1586 cm_deref_id(listen_cm_id_priv);
1587 return 0;
1588
1589rejected:
1590 atomic_dec(&cm_id_priv->refcount);
1591 cm_deref_id(listen_cm_id_priv);
1592destroy:
1593 ib_destroy_cm_id(cm_id);
1594 return ret;
1595}
1596
1597static void cm_format_rep(struct cm_rep_msg *rep_msg,
1598 struct cm_id_private *cm_id_priv,
1599 struct ib_cm_rep_param *param)
1600{
1601 cm_format_mad_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid);
1602 rep_msg->local_comm_id = cm_id_priv->id.local_id;
1603 rep_msg->remote_comm_id = cm_id_priv->id.remote_id;
1604 cm_rep_set_local_qpn(rep_msg, cpu_to_be32(param->qp_num));
1605 cm_rep_set_starting_psn(rep_msg, cpu_to_be32(param->starting_psn));
1606 rep_msg->resp_resources = param->responder_resources;
1607 rep_msg->initiator_depth = param->initiator_depth;
1608 cm_rep_set_target_ack_delay(rep_msg,
1609 cm_id_priv->av.port->cm_dev->ack_delay);
1610 cm_rep_set_failover(rep_msg, param->failover_accepted);
1611 cm_rep_set_flow_ctrl(rep_msg, param->flow_control);
1612 cm_rep_set_rnr_retry_count(rep_msg, param->rnr_retry_count);
1613 cm_rep_set_srq(rep_msg, param->srq);
1614 rep_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
1615
1616 if (param->private_data && param->private_data_len)
1617 memcpy(rep_msg->private_data, param->private_data,
1618 param->private_data_len);
1619}
1620
1621int ib_send_cm_rep(struct ib_cm_id *cm_id,
1622 struct ib_cm_rep_param *param)
1623{
1624 struct cm_id_private *cm_id_priv;
1625 struct ib_mad_send_buf *msg;
1626 struct cm_rep_msg *rep_msg;
1627 unsigned long flags;
1628 int ret;
1629
1630 if (param->private_data &&
1631 param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE)
1632 return -EINVAL;
1633
1634 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1635 spin_lock_irqsave(&cm_id_priv->lock, flags);
1636 if (cm_id->state != IB_CM_REQ_RCVD &&
1637 cm_id->state != IB_CM_MRA_REQ_SENT) {
1638 ret = -EINVAL;
1639 goto out;
1640 }
1641
1642 ret = cm_alloc_msg(cm_id_priv, &msg);
1643 if (ret)
1644 goto out;
1645
1646 rep_msg = (struct cm_rep_msg *) msg->mad;
1647 cm_format_rep(rep_msg, cm_id_priv, param);
1648 msg->timeout_ms = cm_id_priv->timeout_ms;
1649 msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT;
1650
1651 ret = ib_post_send_mad(msg, NULL);
1652 if (ret) {
1653 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1654 cm_free_msg(msg);
1655 return ret;
1656 }
1657
1658 cm_id->state = IB_CM_REP_SENT;
1659 cm_id_priv->msg = msg;
1660 cm_id_priv->initiator_depth = param->initiator_depth;
1661 cm_id_priv->responder_resources = param->responder_resources;
1662 cm_id_priv->rq_psn = cm_rep_get_starting_psn(rep_msg);
1663 cm_id_priv->local_qpn = cm_rep_get_local_qpn(rep_msg);
1664
1665out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1666 return ret;
1667}
1668EXPORT_SYMBOL(ib_send_cm_rep);
1669
1670static void cm_format_rtu(struct cm_rtu_msg *rtu_msg,
1671 struct cm_id_private *cm_id_priv,
1672 const void *private_data,
1673 u8 private_data_len)
1674{
1675 cm_format_mad_hdr(&rtu_msg->hdr, CM_RTU_ATTR_ID, cm_id_priv->tid);
1676 rtu_msg->local_comm_id = cm_id_priv->id.local_id;
1677 rtu_msg->remote_comm_id = cm_id_priv->id.remote_id;
1678
1679 if (private_data && private_data_len)
1680 memcpy(rtu_msg->private_data, private_data, private_data_len);
1681}
1682
1683int ib_send_cm_rtu(struct ib_cm_id *cm_id,
1684 const void *private_data,
1685 u8 private_data_len)
1686{
1687 struct cm_id_private *cm_id_priv;
1688 struct ib_mad_send_buf *msg;
1689 unsigned long flags;
1690 void *data;
1691 int ret;
1692
1693 if (private_data && private_data_len > IB_CM_RTU_PRIVATE_DATA_SIZE)
1694 return -EINVAL;
1695
1696 data = cm_copy_private_data(private_data, private_data_len);
1697 if (IS_ERR(data))
1698 return PTR_ERR(data);
1699
1700 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1701 spin_lock_irqsave(&cm_id_priv->lock, flags);
1702 if (cm_id->state != IB_CM_REP_RCVD &&
1703 cm_id->state != IB_CM_MRA_REP_SENT) {
1704 ret = -EINVAL;
1705 goto error;
1706 }
1707
1708 ret = cm_alloc_msg(cm_id_priv, &msg);
1709 if (ret)
1710 goto error;
1711
1712 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1713 private_data, private_data_len);
1714
1715 ret = ib_post_send_mad(msg, NULL);
1716 if (ret) {
1717 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1718 cm_free_msg(msg);
1719 kfree(data);
1720 return ret;
1721 }
1722
1723 cm_id->state = IB_CM_ESTABLISHED;
1724 cm_set_private_data(cm_id_priv, data, private_data_len);
1725 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1726 return 0;
1727
1728error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1729 kfree(data);
1730 return ret;
1731}
1732EXPORT_SYMBOL(ib_send_cm_rtu);
1733
1734static void cm_format_rep_event(struct cm_work *work)
1735{
1736 struct cm_rep_msg *rep_msg;
1737 struct ib_cm_rep_event_param *param;
1738
1739 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1740 param = &work->cm_event.param.rep_rcvd;
1741 param->remote_ca_guid = rep_msg->local_ca_guid;
1742 param->remote_qkey = be32_to_cpu(rep_msg->local_qkey);
1743 param->remote_qpn = be32_to_cpu(cm_rep_get_local_qpn(rep_msg));
1744 param->starting_psn = be32_to_cpu(cm_rep_get_starting_psn(rep_msg));
1745 param->responder_resources = rep_msg->initiator_depth;
1746 param->initiator_depth = rep_msg->resp_resources;
1747 param->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1748 param->failover_accepted = cm_rep_get_failover(rep_msg);
1749 param->flow_control = cm_rep_get_flow_ctrl(rep_msg);
1750 param->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1751 param->srq = cm_rep_get_srq(rep_msg);
1752 work->cm_event.private_data = &rep_msg->private_data;
1753}
1754
1755static void cm_dup_rep_handler(struct cm_work *work)
1756{
1757 struct cm_id_private *cm_id_priv;
1758 struct cm_rep_msg *rep_msg;
1759 struct ib_mad_send_buf *msg = NULL;
1760 int ret;
1761
1762 rep_msg = (struct cm_rep_msg *) work->mad_recv_wc->recv_buf.mad;
1763 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id,
1764 rep_msg->local_comm_id);
1765 if (!cm_id_priv)
1766 return;
1767
1768 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1769 counter[CM_REP_COUNTER]);
1770 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1771 if (ret)
1772 goto deref;
1773
1774 spin_lock_irq(&cm_id_priv->lock);
1775 if (cm_id_priv->id.state == IB_CM_ESTABLISHED)
1776 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1777 cm_id_priv->private_data,
1778 cm_id_priv->private_data_len);
1779 else if (cm_id_priv->id.state == IB_CM_MRA_REP_SENT)
1780 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1781 CM_MSG_RESPONSE_REP, cm_id_priv->service_timeout,
1782 cm_id_priv->private_data,
1783 cm_id_priv->private_data_len);
1784 else
1785 goto unlock;
1786 spin_unlock_irq(&cm_id_priv->lock);
1787
1788 ret = ib_post_send_mad(msg, NULL);
1789 if (ret)
1790 goto free;
1791 goto deref;
1792
1793unlock: spin_unlock_irq(&cm_id_priv->lock);
1794free: cm_free_msg(msg);
1795deref: cm_deref_id(cm_id_priv);
1796}
1797
1798static int cm_rep_handler(struct cm_work *work)
1799{
1800 struct cm_id_private *cm_id_priv;
1801 struct cm_rep_msg *rep_msg;
1802 int ret;
1803
1804 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1805 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id, 0);
1806 if (!cm_id_priv) {
1807 cm_dup_rep_handler(work);
1808 return -EINVAL;
1809 }
1810
1811 cm_format_rep_event(work);
1812
1813 spin_lock_irq(&cm_id_priv->lock);
1814 switch (cm_id_priv->id.state) {
1815 case IB_CM_REQ_SENT:
1816 case IB_CM_MRA_REQ_RCVD:
1817 break;
1818 default:
1819 spin_unlock_irq(&cm_id_priv->lock);
1820 ret = -EINVAL;
1821 goto error;
1822 }
1823
1824 cm_id_priv->timewait_info->work.remote_id = rep_msg->local_comm_id;
1825 cm_id_priv->timewait_info->remote_ca_guid = rep_msg->local_ca_guid;
1826 cm_id_priv->timewait_info->remote_qpn = cm_rep_get_local_qpn(rep_msg);
1827
1828 spin_lock(&cm.lock);
1829 /* Check for duplicate REP. */
1830 if (cm_insert_remote_id(cm_id_priv->timewait_info)) {
1831 spin_unlock(&cm.lock);
1832 spin_unlock_irq(&cm_id_priv->lock);
1833 ret = -EINVAL;
1834 goto error;
1835 }
1836 /* Check for a stale connection. */
1837 if (cm_insert_remote_qpn(cm_id_priv->timewait_info)) {
1838 rb_erase(&cm_id_priv->timewait_info->remote_id_node,
1839 &cm.remote_id_table);
1840 cm_id_priv->timewait_info->inserted_remote_id = 0;
1841 spin_unlock(&cm.lock);
1842 spin_unlock_irq(&cm_id_priv->lock);
1843 cm_issue_rej(work->port, work->mad_recv_wc,
1844 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP,
1845 NULL, 0);
1846 ret = -EINVAL;
1847 goto error;
1848 }
1849 spin_unlock(&cm.lock);
1850
1851 cm_id_priv->id.state = IB_CM_REP_RCVD;
1852 cm_id_priv->id.remote_id = rep_msg->local_comm_id;
1853 cm_id_priv->remote_qpn = cm_rep_get_local_qpn(rep_msg);
1854 cm_id_priv->initiator_depth = rep_msg->resp_resources;
1855 cm_id_priv->responder_resources = rep_msg->initiator_depth;
1856 cm_id_priv->sq_psn = cm_rep_get_starting_psn(rep_msg);
1857 cm_id_priv->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1858 cm_id_priv->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1859 cm_id_priv->av.timeout =
1860 cm_ack_timeout(cm_id_priv->target_ack_delay,
1861 cm_id_priv->av.timeout - 1);
1862 cm_id_priv->alt_av.timeout =
1863 cm_ack_timeout(cm_id_priv->target_ack_delay,
1864 cm_id_priv->alt_av.timeout - 1);
1865
1866 /* todo: handle peer_to_peer */
1867
1868 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1869 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1870 if (!ret)
1871 list_add_tail(&work->list, &cm_id_priv->work_list);
1872 spin_unlock_irq(&cm_id_priv->lock);
1873
1874 if (ret)
1875 cm_process_work(cm_id_priv, work);
1876 else
1877 cm_deref_id(cm_id_priv);
1878 return 0;
1879
1880error:
1881 cm_deref_id(cm_id_priv);
1882 return ret;
1883}
1884
1885static int cm_establish_handler(struct cm_work *work)
1886{
1887 struct cm_id_private *cm_id_priv;
1888 int ret;
1889
1890 /* See comment in cm_establish about lookup. */
1891 cm_id_priv = cm_acquire_id(work->local_id, work->remote_id);
1892 if (!cm_id_priv)
1893 return -EINVAL;
1894
1895 spin_lock_irq(&cm_id_priv->lock);
1896 if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
1897 spin_unlock_irq(&cm_id_priv->lock);
1898 goto out;
1899 }
1900
1901 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1902 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1903 if (!ret)
1904 list_add_tail(&work->list, &cm_id_priv->work_list);
1905 spin_unlock_irq(&cm_id_priv->lock);
1906
1907 if (ret)
1908 cm_process_work(cm_id_priv, work);
1909 else
1910 cm_deref_id(cm_id_priv);
1911 return 0;
1912out:
1913 cm_deref_id(cm_id_priv);
1914 return -EINVAL;
1915}
1916
1917static int cm_rtu_handler(struct cm_work *work)
1918{
1919 struct cm_id_private *cm_id_priv;
1920 struct cm_rtu_msg *rtu_msg;
1921 int ret;
1922
1923 rtu_msg = (struct cm_rtu_msg *)work->mad_recv_wc->recv_buf.mad;
1924 cm_id_priv = cm_acquire_id(rtu_msg->remote_comm_id,
1925 rtu_msg->local_comm_id);
1926 if (!cm_id_priv)
1927 return -EINVAL;
1928
1929 work->cm_event.private_data = &rtu_msg->private_data;
1930
1931 spin_lock_irq(&cm_id_priv->lock);
1932 if (cm_id_priv->id.state != IB_CM_REP_SENT &&
1933 cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) {
1934 spin_unlock_irq(&cm_id_priv->lock);
1935 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1936 counter[CM_RTU_COUNTER]);
1937 goto out;
1938 }
1939 cm_id_priv->id.state = IB_CM_ESTABLISHED;
1940
1941 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1942 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1943 if (!ret)
1944 list_add_tail(&work->list, &cm_id_priv->work_list);
1945 spin_unlock_irq(&cm_id_priv->lock);
1946
1947 if (ret)
1948 cm_process_work(cm_id_priv, work);
1949 else
1950 cm_deref_id(cm_id_priv);
1951 return 0;
1952out:
1953 cm_deref_id(cm_id_priv);
1954 return -EINVAL;
1955}
1956
1957static void cm_format_dreq(struct cm_dreq_msg *dreq_msg,
1958 struct cm_id_private *cm_id_priv,
1959 const void *private_data,
1960 u8 private_data_len)
1961{
1962 cm_format_mad_hdr(&dreq_msg->hdr, CM_DREQ_ATTR_ID,
1963 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_DREQ));
1964 dreq_msg->local_comm_id = cm_id_priv->id.local_id;
1965 dreq_msg->remote_comm_id = cm_id_priv->id.remote_id;
1966 cm_dreq_set_remote_qpn(dreq_msg, cm_id_priv->remote_qpn);
1967
1968 if (private_data && private_data_len)
1969 memcpy(dreq_msg->private_data, private_data, private_data_len);
1970}
1971
1972int ib_send_cm_dreq(struct ib_cm_id *cm_id,
1973 const void *private_data,
1974 u8 private_data_len)
1975{
1976 struct cm_id_private *cm_id_priv;
1977 struct ib_mad_send_buf *msg;
1978 unsigned long flags;
1979 int ret;
1980
1981 if (private_data && private_data_len > IB_CM_DREQ_PRIVATE_DATA_SIZE)
1982 return -EINVAL;
1983
1984 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1985 spin_lock_irqsave(&cm_id_priv->lock, flags);
1986 if (cm_id->state != IB_CM_ESTABLISHED) {
1987 ret = -EINVAL;
1988 goto out;
1989 }
1990
1991 if (cm_id->lap_state == IB_CM_LAP_SENT ||
1992 cm_id->lap_state == IB_CM_MRA_LAP_RCVD)
1993 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
1994
1995 ret = cm_alloc_msg(cm_id_priv, &msg);
1996 if (ret) {
1997 cm_enter_timewait(cm_id_priv);
1998 goto out;
1999 }
2000
2001 cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv,
2002 private_data, private_data_len);
2003 msg->timeout_ms = cm_id_priv->timeout_ms;
2004 msg->context[1] = (void *) (unsigned long) IB_CM_DREQ_SENT;
2005
2006 ret = ib_post_send_mad(msg, NULL);
2007 if (ret) {
2008 cm_enter_timewait(cm_id_priv);
2009 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2010 cm_free_msg(msg);
2011 return ret;
2012 }
2013
2014 cm_id->state = IB_CM_DREQ_SENT;
2015 cm_id_priv->msg = msg;
2016out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2017 return ret;
2018}
2019EXPORT_SYMBOL(ib_send_cm_dreq);
2020
2021static void cm_format_drep(struct cm_drep_msg *drep_msg,
2022 struct cm_id_private *cm_id_priv,
2023 const void *private_data,
2024 u8 private_data_len)
2025{
2026 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, cm_id_priv->tid);
2027 drep_msg->local_comm_id = cm_id_priv->id.local_id;
2028 drep_msg->remote_comm_id = cm_id_priv->id.remote_id;
2029
2030 if (private_data && private_data_len)
2031 memcpy(drep_msg->private_data, private_data, private_data_len);
2032}
2033
2034int ib_send_cm_drep(struct ib_cm_id *cm_id,
2035 const void *private_data,
2036 u8 private_data_len)
2037{
2038 struct cm_id_private *cm_id_priv;
2039 struct ib_mad_send_buf *msg;
2040 unsigned long flags;
2041 void *data;
2042 int ret;
2043
2044 if (private_data && private_data_len > IB_CM_DREP_PRIVATE_DATA_SIZE)
2045 return -EINVAL;
2046
2047 data = cm_copy_private_data(private_data, private_data_len);
2048 if (IS_ERR(data))
2049 return PTR_ERR(data);
2050
2051 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2052 spin_lock_irqsave(&cm_id_priv->lock, flags);
2053 if (cm_id->state != IB_CM_DREQ_RCVD) {
2054 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2055 kfree(data);
2056 return -EINVAL;
2057 }
2058
2059 cm_set_private_data(cm_id_priv, data, private_data_len);
2060 cm_enter_timewait(cm_id_priv);
2061
2062 ret = cm_alloc_msg(cm_id_priv, &msg);
2063 if (ret)
2064 goto out;
2065
2066 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
2067 private_data, private_data_len);
2068
2069 ret = ib_post_send_mad(msg, NULL);
2070 if (ret) {
2071 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2072 cm_free_msg(msg);
2073 return ret;
2074 }
2075
2076out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2077 return ret;
2078}
2079EXPORT_SYMBOL(ib_send_cm_drep);
2080
2081static int cm_issue_drep(struct cm_port *port,
2082 struct ib_mad_recv_wc *mad_recv_wc)
2083{
2084 struct ib_mad_send_buf *msg = NULL;
2085 struct cm_dreq_msg *dreq_msg;
2086 struct cm_drep_msg *drep_msg;
2087 int ret;
2088
2089 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
2090 if (ret)
2091 return ret;
2092
2093 dreq_msg = (struct cm_dreq_msg *) mad_recv_wc->recv_buf.mad;
2094 drep_msg = (struct cm_drep_msg *) msg->mad;
2095
2096 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, dreq_msg->hdr.tid);
2097 drep_msg->remote_comm_id = dreq_msg->local_comm_id;
2098 drep_msg->local_comm_id = dreq_msg->remote_comm_id;
2099
2100 ret = ib_post_send_mad(msg, NULL);
2101 if (ret)
2102 cm_free_msg(msg);
2103
2104 return ret;
2105}
2106
2107static int cm_dreq_handler(struct cm_work *work)
2108{
2109 struct cm_id_private *cm_id_priv;
2110 struct cm_dreq_msg *dreq_msg;
2111 struct ib_mad_send_buf *msg = NULL;
2112 int ret;
2113
2114 dreq_msg = (struct cm_dreq_msg *)work->mad_recv_wc->recv_buf.mad;
2115 cm_id_priv = cm_acquire_id(dreq_msg->remote_comm_id,
2116 dreq_msg->local_comm_id);
2117 if (!cm_id_priv) {
2118 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2119 counter[CM_DREQ_COUNTER]);
2120 cm_issue_drep(work->port, work->mad_recv_wc);
2121 return -EINVAL;
2122 }
2123
2124 work->cm_event.private_data = &dreq_msg->private_data;
2125
2126 spin_lock_irq(&cm_id_priv->lock);
2127 if (cm_id_priv->local_qpn != cm_dreq_get_remote_qpn(dreq_msg))
2128 goto unlock;
2129
2130 switch (cm_id_priv->id.state) {
2131 case IB_CM_REP_SENT:
2132 case IB_CM_DREQ_SENT:
2133 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2134 break;
2135 case IB_CM_ESTABLISHED:
2136 if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT ||
2137 cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
2138 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2139 break;
2140 case IB_CM_MRA_REP_RCVD:
2141 break;
2142 case IB_CM_TIMEWAIT:
2143 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2144 counter[CM_DREQ_COUNTER]);
2145 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2146 goto unlock;
2147
2148 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
2149 cm_id_priv->private_data,
2150 cm_id_priv->private_data_len);
2151 spin_unlock_irq(&cm_id_priv->lock);
2152
2153 if (ib_post_send_mad(msg, NULL))
2154 cm_free_msg(msg);
2155 goto deref;
2156 case IB_CM_DREQ_RCVD:
2157 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2158 counter[CM_DREQ_COUNTER]);
2159 goto unlock;
2160 default:
2161 goto unlock;
2162 }
2163 cm_id_priv->id.state = IB_CM_DREQ_RCVD;
2164 cm_id_priv->tid = dreq_msg->hdr.tid;
2165 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2166 if (!ret)
2167 list_add_tail(&work->list, &cm_id_priv->work_list);
2168 spin_unlock_irq(&cm_id_priv->lock);
2169
2170 if (ret)
2171 cm_process_work(cm_id_priv, work);
2172 else
2173 cm_deref_id(cm_id_priv);
2174 return 0;
2175
2176unlock: spin_unlock_irq(&cm_id_priv->lock);
2177deref: cm_deref_id(cm_id_priv);
2178 return -EINVAL;
2179}
2180
2181static int cm_drep_handler(struct cm_work *work)
2182{
2183 struct cm_id_private *cm_id_priv;
2184 struct cm_drep_msg *drep_msg;
2185 int ret;
2186
2187 drep_msg = (struct cm_drep_msg *)work->mad_recv_wc->recv_buf.mad;
2188 cm_id_priv = cm_acquire_id(drep_msg->remote_comm_id,
2189 drep_msg->local_comm_id);
2190 if (!cm_id_priv)
2191 return -EINVAL;
2192
2193 work->cm_event.private_data = &drep_msg->private_data;
2194
2195 spin_lock_irq(&cm_id_priv->lock);
2196 if (cm_id_priv->id.state != IB_CM_DREQ_SENT &&
2197 cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
2198 spin_unlock_irq(&cm_id_priv->lock);
2199 goto out;
2200 }
2201 cm_enter_timewait(cm_id_priv);
2202
2203 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2204 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2205 if (!ret)
2206 list_add_tail(&work->list, &cm_id_priv->work_list);
2207 spin_unlock_irq(&cm_id_priv->lock);
2208
2209 if (ret)
2210 cm_process_work(cm_id_priv, work);
2211 else
2212 cm_deref_id(cm_id_priv);
2213 return 0;
2214out:
2215 cm_deref_id(cm_id_priv);
2216 return -EINVAL;
2217}
2218
2219int ib_send_cm_rej(struct ib_cm_id *cm_id,
2220 enum ib_cm_rej_reason reason,
2221 void *ari,
2222 u8 ari_length,
2223 const void *private_data,
2224 u8 private_data_len)
2225{
2226 struct cm_id_private *cm_id_priv;
2227 struct ib_mad_send_buf *msg;
2228 unsigned long flags;
2229 int ret;
2230
2231 if ((private_data && private_data_len > IB_CM_REJ_PRIVATE_DATA_SIZE) ||
2232 (ari && ari_length > IB_CM_REJ_ARI_LENGTH))
2233 return -EINVAL;
2234
2235 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2236
2237 spin_lock_irqsave(&cm_id_priv->lock, flags);
2238 switch (cm_id->state) {
2239 case IB_CM_REQ_SENT:
2240 case IB_CM_MRA_REQ_RCVD:
2241 case IB_CM_REQ_RCVD:
2242 case IB_CM_MRA_REQ_SENT:
2243 case IB_CM_REP_RCVD:
2244 case IB_CM_MRA_REP_SENT:
2245 ret = cm_alloc_msg(cm_id_priv, &msg);
2246 if (!ret)
2247 cm_format_rej((struct cm_rej_msg *) msg->mad,
2248 cm_id_priv, reason, ari, ari_length,
2249 private_data, private_data_len);
2250
2251 cm_reset_to_idle(cm_id_priv);
2252 break;
2253 case IB_CM_REP_SENT:
2254 case IB_CM_MRA_REP_RCVD:
2255 ret = cm_alloc_msg(cm_id_priv, &msg);
2256 if (!ret)
2257 cm_format_rej((struct cm_rej_msg *) msg->mad,
2258 cm_id_priv, reason, ari, ari_length,
2259 private_data, private_data_len);
2260
2261 cm_enter_timewait(cm_id_priv);
2262 break;
2263 default:
2264 ret = -EINVAL;
2265 goto out;
2266 }
2267
2268 if (ret)
2269 goto out;
2270
2271 ret = ib_post_send_mad(msg, NULL);
2272 if (ret)
2273 cm_free_msg(msg);
2274
2275out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2276 return ret;
2277}
2278EXPORT_SYMBOL(ib_send_cm_rej);
2279
2280static void cm_format_rej_event(struct cm_work *work)
2281{
2282 struct cm_rej_msg *rej_msg;
2283 struct ib_cm_rej_event_param *param;
2284
2285 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2286 param = &work->cm_event.param.rej_rcvd;
2287 param->ari = rej_msg->ari;
2288 param->ari_length = cm_rej_get_reject_info_len(rej_msg);
2289 param->reason = __be16_to_cpu(rej_msg->reason);
2290 work->cm_event.private_data = &rej_msg->private_data;
2291}
2292
2293static struct cm_id_private * cm_acquire_rejected_id(struct cm_rej_msg *rej_msg)
2294{
2295 struct cm_timewait_info *timewait_info;
2296 struct cm_id_private *cm_id_priv;
2297 __be32 remote_id;
2298
2299 remote_id = rej_msg->local_comm_id;
2300
2301 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_TIMEOUT) {
2302 spin_lock_irq(&cm.lock);
2303 timewait_info = cm_find_remote_id( *((__be64 *) rej_msg->ari),
2304 remote_id);
2305 if (!timewait_info) {
2306 spin_unlock_irq(&cm.lock);
2307 return NULL;
2308 }
2309 cm_id_priv = idr_find(&cm.local_id_table, (__force int)
2310 (timewait_info->work.local_id ^
2311 cm.random_id_operand));
2312 if (cm_id_priv) {
2313 if (cm_id_priv->id.remote_id == remote_id)
2314 atomic_inc(&cm_id_priv->refcount);
2315 else
2316 cm_id_priv = NULL;
2317 }
2318 spin_unlock_irq(&cm.lock);
2319 } else if (cm_rej_get_msg_rejected(rej_msg) == CM_MSG_RESPONSE_REQ)
2320 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, 0);
2321 else
2322 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, remote_id);
2323
2324 return cm_id_priv;
2325}
2326
2327static int cm_rej_handler(struct cm_work *work)
2328{
2329 struct cm_id_private *cm_id_priv;
2330 struct cm_rej_msg *rej_msg;
2331 int ret;
2332
2333 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2334 cm_id_priv = cm_acquire_rejected_id(rej_msg);
2335 if (!cm_id_priv)
2336 return -EINVAL;
2337
2338 cm_format_rej_event(work);
2339
2340 spin_lock_irq(&cm_id_priv->lock);
2341 switch (cm_id_priv->id.state) {
2342 case IB_CM_REQ_SENT:
2343 case IB_CM_MRA_REQ_RCVD:
2344 case IB_CM_REP_SENT:
2345 case IB_CM_MRA_REP_RCVD:
2346 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2347 /* fall through */
2348 case IB_CM_REQ_RCVD:
2349 case IB_CM_MRA_REQ_SENT:
2350 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_STALE_CONN)
2351 cm_enter_timewait(cm_id_priv);
2352 else
2353 cm_reset_to_idle(cm_id_priv);
2354 break;
2355 case IB_CM_DREQ_SENT:
2356 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2357 /* fall through */
2358 case IB_CM_REP_RCVD:
2359 case IB_CM_MRA_REP_SENT:
2360 cm_enter_timewait(cm_id_priv);
2361 break;
2362 case IB_CM_ESTABLISHED:
2363 if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT ||
2364 cm_id_priv->id.lap_state == IB_CM_LAP_SENT) {
2365 if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT)
2366 ib_cancel_mad(cm_id_priv->av.port->mad_agent,
2367 cm_id_priv->msg);
2368 cm_enter_timewait(cm_id_priv);
2369 break;
2370 }
2371 /* fall through */
2372 default:
2373 spin_unlock_irq(&cm_id_priv->lock);
2374 ret = -EINVAL;
2375 goto out;
2376 }
2377
2378 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2379 if (!ret)
2380 list_add_tail(&work->list, &cm_id_priv->work_list);
2381 spin_unlock_irq(&cm_id_priv->lock);
2382
2383 if (ret)
2384 cm_process_work(cm_id_priv, work);
2385 else
2386 cm_deref_id(cm_id_priv);
2387 return 0;
2388out:
2389 cm_deref_id(cm_id_priv);
2390 return -EINVAL;
2391}
2392
2393int ib_send_cm_mra(struct ib_cm_id *cm_id,
2394 u8 service_timeout,
2395 const void *private_data,
2396 u8 private_data_len)
2397{
2398 struct cm_id_private *cm_id_priv;
2399 struct ib_mad_send_buf *msg;
2400 enum ib_cm_state cm_state;
2401 enum ib_cm_lap_state lap_state;
2402 enum cm_msg_response msg_response;
2403 void *data;
2404 unsigned long flags;
2405 int ret;
2406
2407 if (private_data && private_data_len > IB_CM_MRA_PRIVATE_DATA_SIZE)
2408 return -EINVAL;
2409
2410 data = cm_copy_private_data(private_data, private_data_len);
2411 if (IS_ERR(data))
2412 return PTR_ERR(data);
2413
2414 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2415
2416 spin_lock_irqsave(&cm_id_priv->lock, flags);
2417 switch(cm_id_priv->id.state) {
2418 case IB_CM_REQ_RCVD:
2419 cm_state = IB_CM_MRA_REQ_SENT;
2420 lap_state = cm_id->lap_state;
2421 msg_response = CM_MSG_RESPONSE_REQ;
2422 break;
2423 case IB_CM_REP_RCVD:
2424 cm_state = IB_CM_MRA_REP_SENT;
2425 lap_state = cm_id->lap_state;
2426 msg_response = CM_MSG_RESPONSE_REP;
2427 break;
2428 case IB_CM_ESTABLISHED:
2429 if (cm_id->lap_state == IB_CM_LAP_RCVD) {
2430 cm_state = cm_id->state;
2431 lap_state = IB_CM_MRA_LAP_SENT;
2432 msg_response = CM_MSG_RESPONSE_OTHER;
2433 break;
2434 }
2435 default:
2436 ret = -EINVAL;
2437 goto error1;
2438 }
2439
2440 if (!(service_timeout & IB_CM_MRA_FLAG_DELAY)) {
2441 ret = cm_alloc_msg(cm_id_priv, &msg);
2442 if (ret)
2443 goto error1;
2444
2445 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2446 msg_response, service_timeout,
2447 private_data, private_data_len);
2448 ret = ib_post_send_mad(msg, NULL);
2449 if (ret)
2450 goto error2;
2451 }
2452
2453 cm_id->state = cm_state;
2454 cm_id->lap_state = lap_state;
2455 cm_id_priv->service_timeout = service_timeout;
2456 cm_set_private_data(cm_id_priv, data, private_data_len);
2457 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2458 return 0;
2459
2460error1: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2461 kfree(data);
2462 return ret;
2463
2464error2: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2465 kfree(data);
2466 cm_free_msg(msg);
2467 return ret;
2468}
2469EXPORT_SYMBOL(ib_send_cm_mra);
2470
2471static struct cm_id_private * cm_acquire_mraed_id(struct cm_mra_msg *mra_msg)
2472{
2473 switch (cm_mra_get_msg_mraed(mra_msg)) {
2474 case CM_MSG_RESPONSE_REQ:
2475 return cm_acquire_id(mra_msg->remote_comm_id, 0);
2476 case CM_MSG_RESPONSE_REP:
2477 case CM_MSG_RESPONSE_OTHER:
2478 return cm_acquire_id(mra_msg->remote_comm_id,
2479 mra_msg->local_comm_id);
2480 default:
2481 return NULL;
2482 }
2483}
2484
2485static int cm_mra_handler(struct cm_work *work)
2486{
2487 struct cm_id_private *cm_id_priv;
2488 struct cm_mra_msg *mra_msg;
2489 int timeout, ret;
2490
2491 mra_msg = (struct cm_mra_msg *)work->mad_recv_wc->recv_buf.mad;
2492 cm_id_priv = cm_acquire_mraed_id(mra_msg);
2493 if (!cm_id_priv)
2494 return -EINVAL;
2495
2496 work->cm_event.private_data = &mra_msg->private_data;
2497 work->cm_event.param.mra_rcvd.service_timeout =
2498 cm_mra_get_service_timeout(mra_msg);
2499 timeout = cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg)) +
2500 cm_convert_to_ms(cm_id_priv->av.timeout);
2501
2502 spin_lock_irq(&cm_id_priv->lock);
2503 switch (cm_id_priv->id.state) {
2504 case IB_CM_REQ_SENT:
2505 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REQ ||
2506 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2507 cm_id_priv->msg, timeout))
2508 goto out;
2509 cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD;
2510 break;
2511 case IB_CM_REP_SENT:
2512 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REP ||
2513 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2514 cm_id_priv->msg, timeout))
2515 goto out;
2516 cm_id_priv->id.state = IB_CM_MRA_REP_RCVD;
2517 break;
2518 case IB_CM_ESTABLISHED:
2519 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_OTHER ||
2520 cm_id_priv->id.lap_state != IB_CM_LAP_SENT ||
2521 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2522 cm_id_priv->msg, timeout)) {
2523 if (cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
2524 atomic_long_inc(&work->port->
2525 counter_group[CM_RECV_DUPLICATES].
2526 counter[CM_MRA_COUNTER]);
2527 goto out;
2528 }
2529 cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD;
2530 break;
2531 case IB_CM_MRA_REQ_RCVD:
2532 case IB_CM_MRA_REP_RCVD:
2533 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2534 counter[CM_MRA_COUNTER]);
2535 /* fall through */
2536 default:
2537 goto out;
2538 }
2539
2540 cm_id_priv->msg->context[1] = (void *) (unsigned long)
2541 cm_id_priv->id.state;
2542 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2543 if (!ret)
2544 list_add_tail(&work->list, &cm_id_priv->work_list);
2545 spin_unlock_irq(&cm_id_priv->lock);
2546
2547 if (ret)
2548 cm_process_work(cm_id_priv, work);
2549 else
2550 cm_deref_id(cm_id_priv);
2551 return 0;
2552out:
2553 spin_unlock_irq(&cm_id_priv->lock);
2554 cm_deref_id(cm_id_priv);
2555 return -EINVAL;
2556}
2557
2558static void cm_format_lap(struct cm_lap_msg *lap_msg,
2559 struct cm_id_private *cm_id_priv,
2560 struct ib_sa_path_rec *alternate_path,
2561 const void *private_data,
2562 u8 private_data_len)
2563{
2564 cm_format_mad_hdr(&lap_msg->hdr, CM_LAP_ATTR_ID,
2565 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_LAP));
2566 lap_msg->local_comm_id = cm_id_priv->id.local_id;
2567 lap_msg->remote_comm_id = cm_id_priv->id.remote_id;
2568 cm_lap_set_remote_qpn(lap_msg, cm_id_priv->remote_qpn);
2569 /* todo: need remote CM response timeout */
2570 cm_lap_set_remote_resp_timeout(lap_msg, 0x1F);
2571 lap_msg->alt_local_lid = alternate_path->slid;
2572 lap_msg->alt_remote_lid = alternate_path->dlid;
2573 lap_msg->alt_local_gid = alternate_path->sgid;
2574 lap_msg->alt_remote_gid = alternate_path->dgid;
2575 cm_lap_set_flow_label(lap_msg, alternate_path->flow_label);
2576 cm_lap_set_traffic_class(lap_msg, alternate_path->traffic_class);
2577 lap_msg->alt_hop_limit = alternate_path->hop_limit;
2578 cm_lap_set_packet_rate(lap_msg, alternate_path->rate);
2579 cm_lap_set_sl(lap_msg, alternate_path->sl);
2580 cm_lap_set_subnet_local(lap_msg, 1); /* local only... */
2581 cm_lap_set_local_ack_timeout(lap_msg,
2582 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
2583 alternate_path->packet_life_time));
2584
2585 if (private_data && private_data_len)
2586 memcpy(lap_msg->private_data, private_data, private_data_len);
2587}
2588
2589int ib_send_cm_lap(struct ib_cm_id *cm_id,
2590 struct ib_sa_path_rec *alternate_path,
2591 const void *private_data,
2592 u8 private_data_len)
2593{
2594 struct cm_id_private *cm_id_priv;
2595 struct ib_mad_send_buf *msg;
2596 unsigned long flags;
2597 int ret;
2598
2599 if (private_data && private_data_len > IB_CM_LAP_PRIVATE_DATA_SIZE)
2600 return -EINVAL;
2601
2602 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2603 spin_lock_irqsave(&cm_id_priv->lock, flags);
2604 if (cm_id->state != IB_CM_ESTABLISHED ||
2605 (cm_id->lap_state != IB_CM_LAP_UNINIT &&
2606 cm_id->lap_state != IB_CM_LAP_IDLE)) {
2607 ret = -EINVAL;
2608 goto out;
2609 }
2610
2611 ret = cm_init_av_by_path(alternate_path, &cm_id_priv->alt_av);
2612 if (ret)
2613 goto out;
2614 cm_id_priv->alt_av.timeout =
2615 cm_ack_timeout(cm_id_priv->target_ack_delay,
2616 cm_id_priv->alt_av.timeout - 1);
2617
2618 ret = cm_alloc_msg(cm_id_priv, &msg);
2619 if (ret)
2620 goto out;
2621
2622 cm_format_lap((struct cm_lap_msg *) msg->mad, cm_id_priv,
2623 alternate_path, private_data, private_data_len);
2624 msg->timeout_ms = cm_id_priv->timeout_ms;
2625 msg->context[1] = (void *) (unsigned long) IB_CM_ESTABLISHED;
2626
2627 ret = ib_post_send_mad(msg, NULL);
2628 if (ret) {
2629 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2630 cm_free_msg(msg);
2631 return ret;
2632 }
2633
2634 cm_id->lap_state = IB_CM_LAP_SENT;
2635 cm_id_priv->msg = msg;
2636
2637out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2638 return ret;
2639}
2640EXPORT_SYMBOL(ib_send_cm_lap);
2641
2642static void cm_format_path_from_lap(struct cm_id_private *cm_id_priv,
2643 struct ib_sa_path_rec *path,
2644 struct cm_lap_msg *lap_msg)
2645{
2646 memset(path, 0, sizeof *path);
2647 path->dgid = lap_msg->alt_local_gid;
2648 path->sgid = lap_msg->alt_remote_gid;
2649 path->dlid = lap_msg->alt_local_lid;
2650 path->slid = lap_msg->alt_remote_lid;
2651 path->flow_label = cm_lap_get_flow_label(lap_msg);
2652 path->hop_limit = lap_msg->alt_hop_limit;
2653 path->traffic_class = cm_lap_get_traffic_class(lap_msg);
2654 path->reversible = 1;
2655 path->pkey = cm_id_priv->pkey;
2656 path->sl = cm_lap_get_sl(lap_msg);
2657 path->mtu_selector = IB_SA_EQ;
2658 path->mtu = cm_id_priv->path_mtu;
2659 path->rate_selector = IB_SA_EQ;
2660 path->rate = cm_lap_get_packet_rate(lap_msg);
2661 path->packet_life_time_selector = IB_SA_EQ;
2662 path->packet_life_time = cm_lap_get_local_ack_timeout(lap_msg);
2663 path->packet_life_time -= (path->packet_life_time > 0);
2664}
2665
2666static int cm_lap_handler(struct cm_work *work)
2667{
2668 struct cm_id_private *cm_id_priv;
2669 struct cm_lap_msg *lap_msg;
2670 struct ib_cm_lap_event_param *param;
2671 struct ib_mad_send_buf *msg = NULL;
2672 int ret;
2673
2674 /* todo: verify LAP request and send reject APR if invalid. */
2675 lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad;
2676 cm_id_priv = cm_acquire_id(lap_msg->remote_comm_id,
2677 lap_msg->local_comm_id);
2678 if (!cm_id_priv)
2679 return -EINVAL;
2680
2681 param = &work->cm_event.param.lap_rcvd;
2682 param->alternate_path = &work->path[0];
2683 cm_format_path_from_lap(cm_id_priv, param->alternate_path, lap_msg);
2684 work->cm_event.private_data = &lap_msg->private_data;
2685
2686 spin_lock_irq(&cm_id_priv->lock);
2687 if (cm_id_priv->id.state != IB_CM_ESTABLISHED)
2688 goto unlock;
2689
2690 switch (cm_id_priv->id.lap_state) {
2691 case IB_CM_LAP_UNINIT:
2692 case IB_CM_LAP_IDLE:
2693 break;
2694 case IB_CM_MRA_LAP_SENT:
2695 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2696 counter[CM_LAP_COUNTER]);
2697 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2698 goto unlock;
2699
2700 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2701 CM_MSG_RESPONSE_OTHER,
2702 cm_id_priv->service_timeout,
2703 cm_id_priv->private_data,
2704 cm_id_priv->private_data_len);
2705 spin_unlock_irq(&cm_id_priv->lock);
2706
2707 if (ib_post_send_mad(msg, NULL))
2708 cm_free_msg(msg);
2709 goto deref;
2710 case IB_CM_LAP_RCVD:
2711 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2712 counter[CM_LAP_COUNTER]);
2713 goto unlock;
2714 default:
2715 goto unlock;
2716 }
2717
2718 cm_id_priv->id.lap_state = IB_CM_LAP_RCVD;
2719 cm_id_priv->tid = lap_msg->hdr.tid;
2720 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
2721 work->mad_recv_wc->recv_buf.grh,
2722 &cm_id_priv->av);
2723 cm_init_av_by_path(param->alternate_path, &cm_id_priv->alt_av);
2724 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2725 if (!ret)
2726 list_add_tail(&work->list, &cm_id_priv->work_list);
2727 spin_unlock_irq(&cm_id_priv->lock);
2728
2729 if (ret)
2730 cm_process_work(cm_id_priv, work);
2731 else
2732 cm_deref_id(cm_id_priv);
2733 return 0;
2734
2735unlock: spin_unlock_irq(&cm_id_priv->lock);
2736deref: cm_deref_id(cm_id_priv);
2737 return -EINVAL;
2738}
2739
2740static void cm_format_apr(struct cm_apr_msg *apr_msg,
2741 struct cm_id_private *cm_id_priv,
2742 enum ib_cm_apr_status status,
2743 void *info,
2744 u8 info_length,
2745 const void *private_data,
2746 u8 private_data_len)
2747{
2748 cm_format_mad_hdr(&apr_msg->hdr, CM_APR_ATTR_ID, cm_id_priv->tid);
2749 apr_msg->local_comm_id = cm_id_priv->id.local_id;
2750 apr_msg->remote_comm_id = cm_id_priv->id.remote_id;
2751 apr_msg->ap_status = (u8) status;
2752
2753 if (info && info_length) {
2754 apr_msg->info_length = info_length;
2755 memcpy(apr_msg->info, info, info_length);
2756 }
2757
2758 if (private_data && private_data_len)
2759 memcpy(apr_msg->private_data, private_data, private_data_len);
2760}
2761
2762int ib_send_cm_apr(struct ib_cm_id *cm_id,
2763 enum ib_cm_apr_status status,
2764 void *info,
2765 u8 info_length,
2766 const void *private_data,
2767 u8 private_data_len)
2768{
2769 struct cm_id_private *cm_id_priv;
2770 struct ib_mad_send_buf *msg;
2771 unsigned long flags;
2772 int ret;
2773
2774 if ((private_data && private_data_len > IB_CM_APR_PRIVATE_DATA_SIZE) ||
2775 (info && info_length > IB_CM_APR_INFO_LENGTH))
2776 return -EINVAL;
2777
2778 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2779 spin_lock_irqsave(&cm_id_priv->lock, flags);
2780 if (cm_id->state != IB_CM_ESTABLISHED ||
2781 (cm_id->lap_state != IB_CM_LAP_RCVD &&
2782 cm_id->lap_state != IB_CM_MRA_LAP_SENT)) {
2783 ret = -EINVAL;
2784 goto out;
2785 }
2786
2787 ret = cm_alloc_msg(cm_id_priv, &msg);
2788 if (ret)
2789 goto out;
2790
2791 cm_format_apr((struct cm_apr_msg *) msg->mad, cm_id_priv, status,
2792 info, info_length, private_data, private_data_len);
2793 ret = ib_post_send_mad(msg, NULL);
2794 if (ret) {
2795 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2796 cm_free_msg(msg);
2797 return ret;
2798 }
2799
2800 cm_id->lap_state = IB_CM_LAP_IDLE;
2801out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2802 return ret;
2803}
2804EXPORT_SYMBOL(ib_send_cm_apr);
2805
2806static int cm_apr_handler(struct cm_work *work)
2807{
2808 struct cm_id_private *cm_id_priv;
2809 struct cm_apr_msg *apr_msg;
2810 int ret;
2811
2812 apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad;
2813 cm_id_priv = cm_acquire_id(apr_msg->remote_comm_id,
2814 apr_msg->local_comm_id);
2815 if (!cm_id_priv)
2816 return -EINVAL; /* Unmatched reply. */
2817
2818 work->cm_event.param.apr_rcvd.ap_status = apr_msg->ap_status;
2819 work->cm_event.param.apr_rcvd.apr_info = &apr_msg->info;
2820 work->cm_event.param.apr_rcvd.info_len = apr_msg->info_length;
2821 work->cm_event.private_data = &apr_msg->private_data;
2822
2823 spin_lock_irq(&cm_id_priv->lock);
2824 if (cm_id_priv->id.state != IB_CM_ESTABLISHED ||
2825 (cm_id_priv->id.lap_state != IB_CM_LAP_SENT &&
2826 cm_id_priv->id.lap_state != IB_CM_MRA_LAP_RCVD)) {
2827 spin_unlock_irq(&cm_id_priv->lock);
2828 goto out;
2829 }
2830 cm_id_priv->id.lap_state = IB_CM_LAP_IDLE;
2831 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2832 cm_id_priv->msg = NULL;
2833
2834 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2835 if (!ret)
2836 list_add_tail(&work->list, &cm_id_priv->work_list);
2837 spin_unlock_irq(&cm_id_priv->lock);
2838
2839 if (ret)
2840 cm_process_work(cm_id_priv, work);
2841 else
2842 cm_deref_id(cm_id_priv);
2843 return 0;
2844out:
2845 cm_deref_id(cm_id_priv);
2846 return -EINVAL;
2847}
2848
2849static int cm_timewait_handler(struct cm_work *work)
2850{
2851 struct cm_timewait_info *timewait_info;
2852 struct cm_id_private *cm_id_priv;
2853 int ret;
2854
2855 timewait_info = (struct cm_timewait_info *)work;
2856 spin_lock_irq(&cm.lock);
2857 list_del(&timewait_info->list);
2858 spin_unlock_irq(&cm.lock);
2859
2860 cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
2861 timewait_info->work.remote_id);
2862 if (!cm_id_priv)
2863 return -EINVAL;
2864
2865 spin_lock_irq(&cm_id_priv->lock);
2866 if (cm_id_priv->id.state != IB_CM_TIMEWAIT ||
2867 cm_id_priv->remote_qpn != timewait_info->remote_qpn) {
2868 spin_unlock_irq(&cm_id_priv->lock);
2869 goto out;
2870 }
2871 cm_id_priv->id.state = IB_CM_IDLE;
2872 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2873 if (!ret)
2874 list_add_tail(&work->list, &cm_id_priv->work_list);
2875 spin_unlock_irq(&cm_id_priv->lock);
2876
2877 if (ret)
2878 cm_process_work(cm_id_priv, work);
2879 else
2880 cm_deref_id(cm_id_priv);
2881 return 0;
2882out:
2883 cm_deref_id(cm_id_priv);
2884 return -EINVAL;
2885}
2886
2887static void cm_format_sidr_req(struct cm_sidr_req_msg *sidr_req_msg,
2888 struct cm_id_private *cm_id_priv,
2889 struct ib_cm_sidr_req_param *param)
2890{
2891 cm_format_mad_hdr(&sidr_req_msg->hdr, CM_SIDR_REQ_ATTR_ID,
2892 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_SIDR));
2893 sidr_req_msg->request_id = cm_id_priv->id.local_id;
2894 sidr_req_msg->pkey = param->path->pkey;
2895 sidr_req_msg->service_id = param->service_id;
2896
2897 if (param->private_data && param->private_data_len)
2898 memcpy(sidr_req_msg->private_data, param->private_data,
2899 param->private_data_len);
2900}
2901
2902int ib_send_cm_sidr_req(struct ib_cm_id *cm_id,
2903 struct ib_cm_sidr_req_param *param)
2904{
2905 struct cm_id_private *cm_id_priv;
2906 struct ib_mad_send_buf *msg;
2907 unsigned long flags;
2908 int ret;
2909
2910 if (!param->path || (param->private_data &&
2911 param->private_data_len > IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE))
2912 return -EINVAL;
2913
2914 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2915 ret = cm_init_av_by_path(param->path, &cm_id_priv->av);
2916 if (ret)
2917 goto out;
2918
2919 cm_id->service_id = param->service_id;
2920 cm_id->service_mask = ~cpu_to_be64(0);
2921 cm_id_priv->timeout_ms = param->timeout_ms;
2922 cm_id_priv->max_cm_retries = param->max_cm_retries;
2923 ret = cm_alloc_msg(cm_id_priv, &msg);
2924 if (ret)
2925 goto out;
2926
2927 cm_format_sidr_req((struct cm_sidr_req_msg *) msg->mad, cm_id_priv,
2928 param);
2929 msg->timeout_ms = cm_id_priv->timeout_ms;
2930 msg->context[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT;
2931
2932 spin_lock_irqsave(&cm_id_priv->lock, flags);
2933 if (cm_id->state == IB_CM_IDLE)
2934 ret = ib_post_send_mad(msg, NULL);
2935 else
2936 ret = -EINVAL;
2937
2938 if (ret) {
2939 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2940 cm_free_msg(msg);
2941 goto out;
2942 }
2943 cm_id->state = IB_CM_SIDR_REQ_SENT;
2944 cm_id_priv->msg = msg;
2945 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2946out:
2947 return ret;
2948}
2949EXPORT_SYMBOL(ib_send_cm_sidr_req);
2950
2951static void cm_format_sidr_req_event(struct cm_work *work,
2952 struct ib_cm_id *listen_id)
2953{
2954 struct cm_sidr_req_msg *sidr_req_msg;
2955 struct ib_cm_sidr_req_event_param *param;
2956
2957 sidr_req_msg = (struct cm_sidr_req_msg *)
2958 work->mad_recv_wc->recv_buf.mad;
2959 param = &work->cm_event.param.sidr_req_rcvd;
2960 param->pkey = __be16_to_cpu(sidr_req_msg->pkey);
2961 param->listen_id = listen_id;
2962 param->port = work->port->port_num;
2963 work->cm_event.private_data = &sidr_req_msg->private_data;
2964}
2965
2966static int cm_sidr_req_handler(struct cm_work *work)
2967{
2968 struct ib_cm_id *cm_id;
2969 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
2970 struct cm_sidr_req_msg *sidr_req_msg;
2971 struct ib_wc *wc;
2972
2973 cm_id = ib_create_cm_id(work->port->cm_dev->ib_device, NULL, NULL);
2974 if (IS_ERR(cm_id))
2975 return PTR_ERR(cm_id);
2976 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2977
2978 /* Record SGID/SLID and request ID for lookup. */
2979 sidr_req_msg = (struct cm_sidr_req_msg *)
2980 work->mad_recv_wc->recv_buf.mad;
2981 wc = work->mad_recv_wc->wc;
2982 cm_id_priv->av.dgid.global.subnet_prefix = cpu_to_be64(wc->slid);
2983 cm_id_priv->av.dgid.global.interface_id = 0;
2984 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
2985 work->mad_recv_wc->recv_buf.grh,
2986 &cm_id_priv->av);
2987 cm_id_priv->id.remote_id = sidr_req_msg->request_id;
2988 cm_id_priv->tid = sidr_req_msg->hdr.tid;
2989 atomic_inc(&cm_id_priv->work_count);
2990
2991 spin_lock_irq(&cm.lock);
2992 cur_cm_id_priv = cm_insert_remote_sidr(cm_id_priv);
2993 if (cur_cm_id_priv) {
2994 spin_unlock_irq(&cm.lock);
2995 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2996 counter[CM_SIDR_REQ_COUNTER]);
2997 goto out; /* Duplicate message. */
2998 }
2999 cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD;
3000 cur_cm_id_priv = cm_find_listen(cm_id->device,
3001 sidr_req_msg->service_id,
3002 sidr_req_msg->private_data);
3003 if (!cur_cm_id_priv) {
3004 spin_unlock_irq(&cm.lock);
3005 cm_reject_sidr_req(cm_id_priv, IB_SIDR_UNSUPPORTED);
3006 goto out; /* No match. */
3007 }
3008 atomic_inc(&cur_cm_id_priv->refcount);
3009 atomic_inc(&cm_id_priv->refcount);
3010 spin_unlock_irq(&cm.lock);
3011
3012 cm_id_priv->id.cm_handler = cur_cm_id_priv->id.cm_handler;
3013 cm_id_priv->id.context = cur_cm_id_priv->id.context;
3014 cm_id_priv->id.service_id = sidr_req_msg->service_id;
3015 cm_id_priv->id.service_mask = ~cpu_to_be64(0);
3016
3017 cm_format_sidr_req_event(work, &cur_cm_id_priv->id);
3018 cm_process_work(cm_id_priv, work);
3019 cm_deref_id(cur_cm_id_priv);
3020 return 0;
3021out:
3022 ib_destroy_cm_id(&cm_id_priv->id);
3023 return -EINVAL;
3024}
3025
3026static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg,
3027 struct cm_id_private *cm_id_priv,
3028 struct ib_cm_sidr_rep_param *param)
3029{
3030 cm_format_mad_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID,
3031 cm_id_priv->tid);
3032 sidr_rep_msg->request_id = cm_id_priv->id.remote_id;
3033 sidr_rep_msg->status = param->status;
3034 cm_sidr_rep_set_qpn(sidr_rep_msg, cpu_to_be32(param->qp_num));
3035 sidr_rep_msg->service_id = cm_id_priv->id.service_id;
3036 sidr_rep_msg->qkey = cpu_to_be32(param->qkey);
3037
3038 if (param->info && param->info_length)
3039 memcpy(sidr_rep_msg->info, param->info, param->info_length);
3040
3041 if (param->private_data && param->private_data_len)
3042 memcpy(sidr_rep_msg->private_data, param->private_data,
3043 param->private_data_len);
3044}
3045
3046int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id,
3047 struct ib_cm_sidr_rep_param *param)
3048{
3049 struct cm_id_private *cm_id_priv;
3050 struct ib_mad_send_buf *msg;
3051 unsigned long flags;
3052 int ret;
3053
3054 if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) ||
3055 (param->private_data &&
3056 param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE))
3057 return -EINVAL;
3058
3059 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3060 spin_lock_irqsave(&cm_id_priv->lock, flags);
3061 if (cm_id->state != IB_CM_SIDR_REQ_RCVD) {
3062 ret = -EINVAL;
3063 goto error;
3064 }
3065
3066 ret = cm_alloc_msg(cm_id_priv, &msg);
3067 if (ret)
3068 goto error;
3069
3070 cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv,
3071 param);
3072 ret = ib_post_send_mad(msg, NULL);
3073 if (ret) {
3074 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3075 cm_free_msg(msg);
3076 return ret;
3077 }
3078 cm_id->state = IB_CM_IDLE;
3079 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3080
3081 spin_lock_irqsave(&cm.lock, flags);
3082 rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
3083 spin_unlock_irqrestore(&cm.lock, flags);
3084 return 0;
3085
3086error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3087 return ret;
3088}
3089EXPORT_SYMBOL(ib_send_cm_sidr_rep);
3090
3091static void cm_format_sidr_rep_event(struct cm_work *work)
3092{
3093 struct cm_sidr_rep_msg *sidr_rep_msg;
3094 struct ib_cm_sidr_rep_event_param *param;
3095
3096 sidr_rep_msg = (struct cm_sidr_rep_msg *)
3097 work->mad_recv_wc->recv_buf.mad;
3098 param = &work->cm_event.param.sidr_rep_rcvd;
3099 param->status = sidr_rep_msg->status;
3100 param->qkey = be32_to_cpu(sidr_rep_msg->qkey);
3101 param->qpn = be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg));
3102 param->info = &sidr_rep_msg->info;
3103 param->info_len = sidr_rep_msg->info_length;
3104 work->cm_event.private_data = &sidr_rep_msg->private_data;
3105}
3106
3107static int cm_sidr_rep_handler(struct cm_work *work)
3108{
3109 struct cm_sidr_rep_msg *sidr_rep_msg;
3110 struct cm_id_private *cm_id_priv;
3111
3112 sidr_rep_msg = (struct cm_sidr_rep_msg *)
3113 work->mad_recv_wc->recv_buf.mad;
3114 cm_id_priv = cm_acquire_id(sidr_rep_msg->request_id, 0);
3115 if (!cm_id_priv)
3116 return -EINVAL; /* Unmatched reply. */
3117
3118 spin_lock_irq(&cm_id_priv->lock);
3119 if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) {
3120 spin_unlock_irq(&cm_id_priv->lock);
3121 goto out;
3122 }
3123 cm_id_priv->id.state = IB_CM_IDLE;
3124 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
3125 spin_unlock_irq(&cm_id_priv->lock);
3126
3127 cm_format_sidr_rep_event(work);
3128 cm_process_work(cm_id_priv, work);
3129 return 0;
3130out:
3131 cm_deref_id(cm_id_priv);
3132 return -EINVAL;
3133}
3134
3135static void cm_process_send_error(struct ib_mad_send_buf *msg,
3136 enum ib_wc_status wc_status)
3137{
3138 struct cm_id_private *cm_id_priv;
3139 struct ib_cm_event cm_event;
3140 enum ib_cm_state state;
3141 int ret;
3142
3143 memset(&cm_event, 0, sizeof cm_event);
3144 cm_id_priv = msg->context[0];
3145
3146 /* Discard old sends or ones without a response. */
3147 spin_lock_irq(&cm_id_priv->lock);
3148 state = (enum ib_cm_state) (unsigned long) msg->context[1];
3149 if (msg != cm_id_priv->msg || state != cm_id_priv->id.state)
3150 goto discard;
3151
3152 switch (state) {
3153 case IB_CM_REQ_SENT:
3154 case IB_CM_MRA_REQ_RCVD:
3155 cm_reset_to_idle(cm_id_priv);
3156 cm_event.event = IB_CM_REQ_ERROR;
3157 break;
3158 case IB_CM_REP_SENT:
3159 case IB_CM_MRA_REP_RCVD:
3160 cm_reset_to_idle(cm_id_priv);
3161 cm_event.event = IB_CM_REP_ERROR;
3162 break;
3163 case IB_CM_DREQ_SENT:
3164 cm_enter_timewait(cm_id_priv);
3165 cm_event.event = IB_CM_DREQ_ERROR;
3166 break;
3167 case IB_CM_SIDR_REQ_SENT:
3168 cm_id_priv->id.state = IB_CM_IDLE;
3169 cm_event.event = IB_CM_SIDR_REQ_ERROR;
3170 break;
3171 default:
3172 goto discard;
3173 }
3174 spin_unlock_irq(&cm_id_priv->lock);
3175 cm_event.param.send_status = wc_status;
3176
3177 /* No other events can occur on the cm_id at this point. */
3178 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event);
3179 cm_free_msg(msg);
3180 if (ret)
3181 ib_destroy_cm_id(&cm_id_priv->id);
3182 return;
3183discard:
3184 spin_unlock_irq(&cm_id_priv->lock);
3185 cm_free_msg(msg);
3186}
3187
3188static void cm_send_handler(struct ib_mad_agent *mad_agent,
3189 struct ib_mad_send_wc *mad_send_wc)
3190{
3191 struct ib_mad_send_buf *msg = mad_send_wc->send_buf;
3192 struct cm_port *port;
3193 u16 attr_index;
3194
3195 port = mad_agent->context;
3196 attr_index = be16_to_cpu(((struct ib_mad_hdr *)
3197 msg->mad)->attr_id) - CM_ATTR_ID_OFFSET;
3198
3199 /*
3200 * If the send was in response to a received message (context[0] is not
3201 * set to a cm_id), and is not a REJ, then it is a send that was
3202 * manually retried.
3203 */
3204 if (!msg->context[0] && (attr_index != CM_REJ_COUNTER))
3205 msg->retries = 1;
3206
3207 atomic_long_add(1 + msg->retries,
3208 &port->counter_group[CM_XMIT].counter[attr_index]);
3209 if (msg->retries)
3210 atomic_long_add(msg->retries,
3211 &port->counter_group[CM_XMIT_RETRIES].
3212 counter[attr_index]);
3213
3214 switch (mad_send_wc->status) {
3215 case IB_WC_SUCCESS:
3216 case IB_WC_WR_FLUSH_ERR:
3217 cm_free_msg(msg);
3218 break;
3219 default:
3220 if (msg->context[0] && msg->context[1])
3221 cm_process_send_error(msg, mad_send_wc->status);
3222 else
3223 cm_free_msg(msg);
3224 break;
3225 }
3226}
3227
3228static void cm_work_handler(struct work_struct *_work)
3229{
3230 struct cm_work *work = container_of(_work, struct cm_work, work.work);
3231 int ret;
3232
3233 switch (work->cm_event.event) {
3234 case IB_CM_REQ_RECEIVED:
3235 ret = cm_req_handler(work);
3236 break;
3237 case IB_CM_MRA_RECEIVED:
3238 ret = cm_mra_handler(work);
3239 break;
3240 case IB_CM_REJ_RECEIVED:
3241 ret = cm_rej_handler(work);
3242 break;
3243 case IB_CM_REP_RECEIVED:
3244 ret = cm_rep_handler(work);
3245 break;
3246 case IB_CM_RTU_RECEIVED:
3247 ret = cm_rtu_handler(work);
3248 break;
3249 case IB_CM_USER_ESTABLISHED:
3250 ret = cm_establish_handler(work);
3251 break;
3252 case IB_CM_DREQ_RECEIVED:
3253 ret = cm_dreq_handler(work);
3254 break;
3255 case IB_CM_DREP_RECEIVED:
3256 ret = cm_drep_handler(work);
3257 break;
3258 case IB_CM_SIDR_REQ_RECEIVED:
3259 ret = cm_sidr_req_handler(work);
3260 break;
3261 case IB_CM_SIDR_REP_RECEIVED:
3262 ret = cm_sidr_rep_handler(work);
3263 break;
3264 case IB_CM_LAP_RECEIVED:
3265 ret = cm_lap_handler(work);
3266 break;
3267 case IB_CM_APR_RECEIVED:
3268 ret = cm_apr_handler(work);
3269 break;
3270 case IB_CM_TIMEWAIT_EXIT:
3271 ret = cm_timewait_handler(work);
3272 break;
3273 default:
3274 ret = -EINVAL;
3275 break;
3276 }
3277 if (ret)
3278 cm_free_work(work);
3279}
3280
3281static int cm_establish(struct ib_cm_id *cm_id)
3282{
3283 struct cm_id_private *cm_id_priv;
3284 struct cm_work *work;
3285 unsigned long flags;
3286 int ret = 0;
3287
3288 work = kmalloc(sizeof *work, GFP_ATOMIC);
3289 if (!work)
3290 return -ENOMEM;
3291
3292 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3293 spin_lock_irqsave(&cm_id_priv->lock, flags);
3294 switch (cm_id->state)
3295 {
3296 case IB_CM_REP_SENT:
3297 case IB_CM_MRA_REP_RCVD:
3298 cm_id->state = IB_CM_ESTABLISHED;
3299 break;
3300 case IB_CM_ESTABLISHED:
3301 ret = -EISCONN;
3302 break;
3303 default:
3304 ret = -EINVAL;
3305 break;
3306 }
3307 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3308
3309 if (ret) {
3310 kfree(work);
3311 goto out;
3312 }
3313
3314 /*
3315 * The CM worker thread may try to destroy the cm_id before it
3316 * can execute this work item. To prevent potential deadlock,
3317 * we need to find the cm_id once we're in the context of the
3318 * worker thread, rather than holding a reference on it.
3319 */
3320 INIT_DELAYED_WORK(&work->work, cm_work_handler);
3321 work->local_id = cm_id->local_id;
3322 work->remote_id = cm_id->remote_id;
3323 work->mad_recv_wc = NULL;
3324 work->cm_event.event = IB_CM_USER_ESTABLISHED;
3325 queue_delayed_work(cm.wq, &work->work, 0);
3326out:
3327 return ret;
3328}
3329
3330static int cm_migrate(struct ib_cm_id *cm_id)
3331{
3332 struct cm_id_private *cm_id_priv;
3333 unsigned long flags;
3334 int ret = 0;
3335
3336 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3337 spin_lock_irqsave(&cm_id_priv->lock, flags);
3338 if (cm_id->state == IB_CM_ESTABLISHED &&
3339 (cm_id->lap_state == IB_CM_LAP_UNINIT ||
3340 cm_id->lap_state == IB_CM_LAP_IDLE)) {
3341 cm_id->lap_state = IB_CM_LAP_IDLE;
3342 cm_id_priv->av = cm_id_priv->alt_av;
3343 } else
3344 ret = -EINVAL;
3345 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3346
3347 return ret;
3348}
3349
3350int ib_cm_notify(struct ib_cm_id *cm_id, enum ib_event_type event)
3351{
3352 int ret;
3353
3354 switch (event) {
3355 case IB_EVENT_COMM_EST:
3356 ret = cm_establish(cm_id);
3357 break;
3358 case IB_EVENT_PATH_MIG:
3359 ret = cm_migrate(cm_id);
3360 break;
3361 default:
3362 ret = -EINVAL;
3363 }
3364 return ret;
3365}
3366EXPORT_SYMBOL(ib_cm_notify);
3367
3368static void cm_recv_handler(struct ib_mad_agent *mad_agent,
3369 struct ib_mad_recv_wc *mad_recv_wc)
3370{
3371 struct cm_port *port = mad_agent->context;
3372 struct cm_work *work;
3373 enum ib_cm_event_type event;
3374 u16 attr_id;
3375 int paths = 0;
3376
3377 switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) {
3378 case CM_REQ_ATTR_ID:
3379 paths = 1 + (((struct cm_req_msg *) mad_recv_wc->recv_buf.mad)->
3380 alt_local_lid != 0);
3381 event = IB_CM_REQ_RECEIVED;
3382 break;
3383 case CM_MRA_ATTR_ID:
3384 event = IB_CM_MRA_RECEIVED;
3385 break;
3386 case CM_REJ_ATTR_ID:
3387 event = IB_CM_REJ_RECEIVED;
3388 break;
3389 case CM_REP_ATTR_ID:
3390 event = IB_CM_REP_RECEIVED;
3391 break;
3392 case CM_RTU_ATTR_ID:
3393 event = IB_CM_RTU_RECEIVED;
3394 break;
3395 case CM_DREQ_ATTR_ID:
3396 event = IB_CM_DREQ_RECEIVED;
3397 break;
3398 case CM_DREP_ATTR_ID:
3399 event = IB_CM_DREP_RECEIVED;
3400 break;
3401 case CM_SIDR_REQ_ATTR_ID:
3402 event = IB_CM_SIDR_REQ_RECEIVED;
3403 break;
3404 case CM_SIDR_REP_ATTR_ID:
3405 event = IB_CM_SIDR_REP_RECEIVED;
3406 break;
3407 case CM_LAP_ATTR_ID:
3408 paths = 1;
3409 event = IB_CM_LAP_RECEIVED;
3410 break;
3411 case CM_APR_ATTR_ID:
3412 event = IB_CM_APR_RECEIVED;
3413 break;
3414 default:
3415 ib_free_recv_mad(mad_recv_wc);
3416 return;
3417 }
3418
3419 attr_id = be16_to_cpu(mad_recv_wc->recv_buf.mad->mad_hdr.attr_id);
3420 atomic_long_inc(&port->counter_group[CM_RECV].
3421 counter[attr_id - CM_ATTR_ID_OFFSET]);
3422
3423 work = kmalloc(sizeof *work + sizeof(struct ib_sa_path_rec) * paths,
3424 GFP_KERNEL);
3425 if (!work) {
3426 ib_free_recv_mad(mad_recv_wc);
3427 return;
3428 }
3429
3430 INIT_DELAYED_WORK(&work->work, cm_work_handler);
3431 work->cm_event.event = event;
3432 work->mad_recv_wc = mad_recv_wc;
3433 work->port = port;
3434 queue_delayed_work(cm.wq, &work->work, 0);
3435}
3436
3437static int cm_init_qp_init_attr(struct cm_id_private *cm_id_priv,
3438 struct ib_qp_attr *qp_attr,
3439 int *qp_attr_mask)
3440{
3441 unsigned long flags;
3442 int ret;
3443
3444 spin_lock_irqsave(&cm_id_priv->lock, flags);
3445 switch (cm_id_priv->id.state) {
3446 case IB_CM_REQ_SENT:
3447 case IB_CM_MRA_REQ_RCVD:
3448 case IB_CM_REQ_RCVD:
3449 case IB_CM_MRA_REQ_SENT:
3450 case IB_CM_REP_RCVD:
3451 case IB_CM_MRA_REP_SENT:
3452 case IB_CM_REP_SENT:
3453 case IB_CM_MRA_REP_RCVD:
3454 case IB_CM_ESTABLISHED:
3455 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS |
3456 IB_QP_PKEY_INDEX | IB_QP_PORT;
3457 qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE;
3458 if (cm_id_priv->responder_resources)
3459 qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ |
3460 IB_ACCESS_REMOTE_ATOMIC;
3461 qp_attr->pkey_index = cm_id_priv->av.pkey_index;
3462 qp_attr->port_num = cm_id_priv->av.port->port_num;
3463 ret = 0;
3464 break;
3465 default:
3466 ret = -EINVAL;
3467 break;
3468 }
3469 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3470 return ret;
3471}
3472
3473static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv,
3474 struct ib_qp_attr *qp_attr,
3475 int *qp_attr_mask)
3476{
3477 unsigned long flags;
3478 int ret;
3479
3480 spin_lock_irqsave(&cm_id_priv->lock, flags);
3481 switch (cm_id_priv->id.state) {
3482 case IB_CM_REQ_RCVD:
3483 case IB_CM_MRA_REQ_SENT:
3484 case IB_CM_REP_RCVD:
3485 case IB_CM_MRA_REP_SENT:
3486 case IB_CM_REP_SENT:
3487 case IB_CM_MRA_REP_RCVD:
3488 case IB_CM_ESTABLISHED:
3489 *qp_attr_mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU |
3490 IB_QP_DEST_QPN | IB_QP_RQ_PSN;
3491 qp_attr->ah_attr = cm_id_priv->av.ah_attr;
3492 qp_attr->path_mtu = cm_id_priv->path_mtu;
3493 qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn);
3494 qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn);
3495 if (cm_id_priv->qp_type == IB_QPT_RC) {
3496 *qp_attr_mask |= IB_QP_MAX_DEST_RD_ATOMIC |
3497 IB_QP_MIN_RNR_TIMER;
3498 qp_attr->max_dest_rd_atomic =
3499 cm_id_priv->responder_resources;
3500 qp_attr->min_rnr_timer = 0;
3501 }
3502 if (cm_id_priv->alt_av.ah_attr.dlid) {
3503 *qp_attr_mask |= IB_QP_ALT_PATH;
3504 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
3505 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
3506 qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
3507 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3508 }
3509 ret = 0;
3510 break;
3511 default:
3512 ret = -EINVAL;
3513 break;
3514 }
3515 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3516 return ret;
3517}
3518
3519static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv,
3520 struct ib_qp_attr *qp_attr,
3521 int *qp_attr_mask)
3522{
3523 unsigned long flags;
3524 int ret;
3525
3526 spin_lock_irqsave(&cm_id_priv->lock, flags);
3527 switch (cm_id_priv->id.state) {
3528 /* Allow transition to RTS before sending REP */
3529 case IB_CM_REQ_RCVD:
3530 case IB_CM_MRA_REQ_SENT:
3531
3532 case IB_CM_REP_RCVD:
3533 case IB_CM_MRA_REP_SENT:
3534 case IB_CM_REP_SENT:
3535 case IB_CM_MRA_REP_RCVD:
3536 case IB_CM_ESTABLISHED:
3537 if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT) {
3538 *qp_attr_mask = IB_QP_STATE | IB_QP_SQ_PSN;
3539 qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn);
3540 if (cm_id_priv->qp_type == IB_QPT_RC) {
3541 *qp_attr_mask |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT |
3542 IB_QP_RNR_RETRY |
3543 IB_QP_MAX_QP_RD_ATOMIC;
3544 qp_attr->timeout = cm_id_priv->av.timeout;
3545 qp_attr->retry_cnt = cm_id_priv->retry_count;
3546 qp_attr->rnr_retry = cm_id_priv->rnr_retry_count;
3547 qp_attr->max_rd_atomic =
3548 cm_id_priv->initiator_depth;
3549 }
3550 if (cm_id_priv->alt_av.ah_attr.dlid) {
3551 *qp_attr_mask |= IB_QP_PATH_MIG_STATE;
3552 qp_attr->path_mig_state = IB_MIG_REARM;
3553 }
3554 } else {
3555 *qp_attr_mask = IB_QP_ALT_PATH | IB_QP_PATH_MIG_STATE;
3556 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
3557 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
3558 qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
3559 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3560 qp_attr->path_mig_state = IB_MIG_REARM;
3561 }
3562 ret = 0;
3563 break;
3564 default:
3565 ret = -EINVAL;
3566 break;
3567 }
3568 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3569 return ret;
3570}
3571
3572int ib_cm_init_qp_attr(struct ib_cm_id *cm_id,
3573 struct ib_qp_attr *qp_attr,
3574 int *qp_attr_mask)
3575{
3576 struct cm_id_private *cm_id_priv;
3577 int ret;
3578
3579 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3580 switch (qp_attr->qp_state) {
3581 case IB_QPS_INIT:
3582 ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask);
3583 break;
3584 case IB_QPS_RTR:
3585 ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask);
3586 break;
3587 case IB_QPS_RTS:
3588 ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask);
3589 break;
3590 default:
3591 ret = -EINVAL;
3592 break;
3593 }
3594 return ret;
3595}
3596EXPORT_SYMBOL(ib_cm_init_qp_attr);
3597
3598static void cm_get_ack_delay(struct cm_device *cm_dev)
3599{
3600 struct ib_device_attr attr;
3601
3602 if (ib_query_device(cm_dev->ib_device, &attr))
3603 cm_dev->ack_delay = 0; /* acks will rely on packet life time */
3604 else
3605 cm_dev->ack_delay = attr.local_ca_ack_delay;
3606}
3607
3608static ssize_t cm_show_counter(struct kobject *obj, struct attribute *attr,
3609 char *buf)
3610{
3611 struct cm_counter_group *group;
3612 struct cm_counter_attribute *cm_attr;
3613
3614 group = container_of(obj, struct cm_counter_group, obj);
3615 cm_attr = container_of(attr, struct cm_counter_attribute, attr);
3616
3617 return sprintf(buf, "%ld\n",
3618 atomic_long_read(&group->counter[cm_attr->index]));
3619}
3620
3621static const struct sysfs_ops cm_counter_ops = {
3622 .show = cm_show_counter
3623};
3624
3625static struct kobj_type cm_counter_obj_type = {
3626 .sysfs_ops = &cm_counter_ops,
3627 .default_attrs = cm_counter_default_attrs
3628};
3629
3630static void cm_release_port_obj(struct kobject *obj)
3631{
3632 struct cm_port *cm_port;
3633
3634 cm_port = container_of(obj, struct cm_port, port_obj);
3635 kfree(cm_port);
3636}
3637
3638static struct kobj_type cm_port_obj_type = {
3639 .release = cm_release_port_obj
3640};
3641
3642static char *cm_devnode(struct device *dev, mode_t *mode)
3643{
3644 if (mode)
3645 *mode = 0666;
3646 return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
3647}
3648
3649struct class cm_class = {
3650 .owner = THIS_MODULE,
3651 .name = "infiniband_cm",
3652 .devnode = cm_devnode,
3653};
3654EXPORT_SYMBOL(cm_class);
3655
3656static int cm_create_port_fs(struct cm_port *port)
3657{
3658 int i, ret;
3659
3660 ret = kobject_init_and_add(&port->port_obj, &cm_port_obj_type,
3661 &port->cm_dev->device->kobj,
3662 "%d", port->port_num);
3663 if (ret) {
3664 kfree(port);
3665 return ret;
3666 }
3667
3668 for (i = 0; i < CM_COUNTER_GROUPS; i++) {
3669 ret = kobject_init_and_add(&port->counter_group[i].obj,
3670 &cm_counter_obj_type,
3671 &port->port_obj,
3672 "%s", counter_group_names[i]);
3673 if (ret)
3674 goto error;
3675 }
3676
3677 return 0;
3678
3679error:
3680 while (i--)
3681 kobject_put(&port->counter_group[i].obj);
3682 kobject_put(&port->port_obj);
3683 return ret;
3684
3685}
3686
3687static void cm_remove_port_fs(struct cm_port *port)
3688{
3689 int i;
3690
3691 for (i = 0; i < CM_COUNTER_GROUPS; i++)
3692 kobject_put(&port->counter_group[i].obj);
3693
3694 kobject_put(&port->port_obj);
3695}
3696
3697static void cm_add_one(struct ib_device *ib_device)
3698{
3699 struct cm_device *cm_dev;
3700 struct cm_port *port;
3701 struct ib_mad_reg_req reg_req = {
3702 .mgmt_class = IB_MGMT_CLASS_CM,
3703 .mgmt_class_version = IB_CM_CLASS_VERSION
3704 };
3705 struct ib_port_modify port_modify = {
3706 .set_port_cap_mask = IB_PORT_CM_SUP
3707 };
3708 unsigned long flags;
3709 int ret;
3710 u8 i;
3711
3712 if (rdma_node_get_transport(ib_device->node_type) != RDMA_TRANSPORT_IB)
3713 return;
3714
3715 cm_dev = kzalloc(sizeof(*cm_dev) + sizeof(*port) *
3716 ib_device->phys_port_cnt, GFP_KERNEL);
3717 if (!cm_dev)
3718 return;
3719
3720 cm_dev->ib_device = ib_device;
3721 cm_get_ack_delay(cm_dev);
3722
3723 cm_dev->device = device_create(&cm_class, &ib_device->dev,
3724 MKDEV(0, 0), NULL,
3725 "%s", ib_device->name);
3726 if (IS_ERR(cm_dev->device)) {
3727 kfree(cm_dev);
3728 return;
3729 }
3730
3731 set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask);
3732 for (i = 1; i <= ib_device->phys_port_cnt; i++) {
3733 port = kzalloc(sizeof *port, GFP_KERNEL);
3734 if (!port)
3735 goto error1;
3736
3737 cm_dev->port[i-1] = port;
3738 port->cm_dev = cm_dev;
3739 port->port_num = i;
3740
3741 ret = cm_create_port_fs(port);
3742 if (ret)
3743 goto error1;
3744
3745 port->mad_agent = ib_register_mad_agent(ib_device, i,
3746 IB_QPT_GSI,
3747 ®_req,
3748 0,
3749 cm_send_handler,
3750 cm_recv_handler,
3751 port);
3752 if (IS_ERR(port->mad_agent))
3753 goto error2;
3754
3755 ret = ib_modify_port(ib_device, i, 0, &port_modify);
3756 if (ret)
3757 goto error3;
3758 }
3759 ib_set_client_data(ib_device, &cm_client, cm_dev);
3760
3761 write_lock_irqsave(&cm.device_lock, flags);
3762 list_add_tail(&cm_dev->list, &cm.device_list);
3763 write_unlock_irqrestore(&cm.device_lock, flags);
3764 return;
3765
3766error3:
3767 ib_unregister_mad_agent(port->mad_agent);
3768error2:
3769 cm_remove_port_fs(port);
3770error1:
3771 port_modify.set_port_cap_mask = 0;
3772 port_modify.clr_port_cap_mask = IB_PORT_CM_SUP;
3773 while (--i) {
3774 port = cm_dev->port[i-1];
3775 ib_modify_port(ib_device, port->port_num, 0, &port_modify);
3776 ib_unregister_mad_agent(port->mad_agent);
3777 cm_remove_port_fs(port);
3778 }
3779 device_unregister(cm_dev->device);
3780 kfree(cm_dev);
3781}
3782
3783static void cm_remove_one(struct ib_device *ib_device)
3784{
3785 struct cm_device *cm_dev;
3786 struct cm_port *port;
3787 struct ib_port_modify port_modify = {
3788 .clr_port_cap_mask = IB_PORT_CM_SUP
3789 };
3790 unsigned long flags;
3791 int i;
3792
3793 cm_dev = ib_get_client_data(ib_device, &cm_client);
3794 if (!cm_dev)
3795 return;
3796
3797 write_lock_irqsave(&cm.device_lock, flags);
3798 list_del(&cm_dev->list);
3799 write_unlock_irqrestore(&cm.device_lock, flags);
3800
3801 for (i = 1; i <= ib_device->phys_port_cnt; i++) {
3802 port = cm_dev->port[i-1];
3803 ib_modify_port(ib_device, port->port_num, 0, &port_modify);
3804 ib_unregister_mad_agent(port->mad_agent);
3805 flush_workqueue(cm.wq);
3806 cm_remove_port_fs(port);
3807 }
3808 device_unregister(cm_dev->device);
3809 kfree(cm_dev);
3810}
3811
3812static int __init ib_cm_init(void)
3813{
3814 int ret;
3815
3816 memset(&cm, 0, sizeof cm);
3817 INIT_LIST_HEAD(&cm.device_list);
3818 rwlock_init(&cm.device_lock);
3819 spin_lock_init(&cm.lock);
3820 cm.listen_service_table = RB_ROOT;
3821 cm.listen_service_id = be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID);
3822 cm.remote_id_table = RB_ROOT;
3823 cm.remote_qp_table = RB_ROOT;
3824 cm.remote_sidr_table = RB_ROOT;
3825 idr_init(&cm.local_id_table);
3826 get_random_bytes(&cm.random_id_operand, sizeof cm.random_id_operand);
3827 idr_pre_get(&cm.local_id_table, GFP_KERNEL);
3828 INIT_LIST_HEAD(&cm.timewait_list);
3829
3830 ret = class_register(&cm_class);
3831 if (ret)
3832 return -ENOMEM;
3833
3834 cm.wq = create_workqueue("ib_cm");
3835 if (!cm.wq) {
3836 ret = -ENOMEM;
3837 goto error1;
3838 }
3839
3840 ret = ib_register_client(&cm_client);
3841 if (ret)
3842 goto error2;
3843
3844 return 0;
3845error2:
3846 destroy_workqueue(cm.wq);
3847error1:
3848 class_unregister(&cm_class);
3849 return ret;
3850}
3851
3852static void __exit ib_cm_cleanup(void)
3853{
3854 struct cm_timewait_info *timewait_info, *tmp;
3855
3856 spin_lock_irq(&cm.lock);
3857 list_for_each_entry(timewait_info, &cm.timewait_list, list)
3858 cancel_delayed_work(&timewait_info->work.work);
3859 spin_unlock_irq(&cm.lock);
3860
3861 ib_unregister_client(&cm_client);
3862 destroy_workqueue(cm.wq);
3863
3864 list_for_each_entry_safe(timewait_info, tmp, &cm.timewait_list, list) {
3865 list_del(&timewait_info->list);
3866 kfree(timewait_info);
3867 }
3868
3869 class_unregister(&cm_class);
3870 idr_destroy(&cm.local_id_table);
3871}
3872
3873module_init(ib_cm_init);
3874module_exit(ib_cm_cleanup);
3875