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