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