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