Commit | Line | Data |
---|---|---|
a977049d | 1 | /* |
f06d2653 | 2 | * Copyright (c) 2004-2006 Intel Corporation. All rights reserved. |
a977049d HR |
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 | * | |
6e61d04f | 35 | * $Id: cm.c 4311 2005-12-05 18:42:01Z sean.hefty $ |
a977049d | 36 | */ |
1b52fa98 SH |
37 | |
38 | #include <linux/completion.h> | |
a977049d HR |
39 | #include <linux/dma-mapping.h> |
40 | #include <linux/err.h> | |
41 | #include <linux/idr.h> | |
42 | #include <linux/interrupt.h> | |
f06d2653 | 43 | #include <linux/random.h> |
a977049d HR |
44 | #include <linux/rbtree.h> |
45 | #include <linux/spinlock.h> | |
46 | #include <linux/workqueue.h> | |
47 | ||
a4d61e84 RD |
48 | #include <rdma/ib_cache.h> |
49 | #include <rdma/ib_cm.h> | |
a977049d HR |
50 | #include "cm_msgs.h" |
51 | ||
52 | MODULE_AUTHOR("Sean Hefty"); | |
53 | MODULE_DESCRIPTION("InfiniBand CM"); | |
54 | MODULE_LICENSE("Dual BSD/GPL"); | |
55 | ||
56 | static void cm_add_one(struct ib_device *device); | |
57 | static void cm_remove_one(struct ib_device *device); | |
58 | ||
59 | static struct ib_client cm_client = { | |
60 | .name = "cm", | |
61 | .add = cm_add_one, | |
62 | .remove = cm_remove_one | |
63 | }; | |
64 | ||
65 | static struct ib_cm { | |
66 | spinlock_t lock; | |
67 | struct list_head device_list; | |
68 | rwlock_t device_lock; | |
69 | struct rb_root listen_service_table; | |
70 | u64 listen_service_id; | |
71 | /* struct rb_root peer_service_table; todo: fix peer to peer */ | |
72 | struct rb_root remote_qp_table; | |
73 | struct rb_root remote_id_table; | |
74 | struct rb_root remote_sidr_table; | |
75 | struct idr local_id_table; | |
f06d2653 | 76 | __be32 random_id_operand; |
8575329d | 77 | struct list_head timewait_list; |
a977049d HR |
78 | struct workqueue_struct *wq; |
79 | } cm; | |
80 | ||
81 | struct cm_port { | |
82 | struct cm_device *cm_dev; | |
83 | struct ib_mad_agent *mad_agent; | |
84 | u8 port_num; | |
85 | }; | |
86 | ||
87 | struct cm_device { | |
88 | struct list_head list; | |
89 | struct ib_device *device; | |
a977049d HR |
90 | struct cm_port port[0]; |
91 | }; | |
92 | ||
93 | struct cm_av { | |
94 | struct cm_port *port; | |
95 | union ib_gid dgid; | |
96 | struct ib_ah_attr ah_attr; | |
97 | u16 pkey_index; | |
98 | u8 packet_life_time; | |
99 | }; | |
100 | ||
101 | struct cm_work { | |
c4028958 | 102 | struct delayed_work work; |
a977049d HR |
103 | struct list_head list; |
104 | struct cm_port *port; | |
105 | struct ib_mad_recv_wc *mad_recv_wc; /* Received MADs */ | |
97f52eb4 SH |
106 | __be32 local_id; /* Established / timewait */ |
107 | __be32 remote_id; | |
a977049d HR |
108 | struct ib_cm_event cm_event; |
109 | struct ib_sa_path_rec path[0]; | |
110 | }; | |
111 | ||
112 | struct cm_timewait_info { | |
113 | struct cm_work work; /* Must be first. */ | |
8575329d | 114 | struct list_head list; |
a977049d HR |
115 | struct rb_node remote_qp_node; |
116 | struct rb_node remote_id_node; | |
97f52eb4 SH |
117 | __be64 remote_ca_guid; |
118 | __be32 remote_qpn; | |
a977049d HR |
119 | u8 inserted_remote_qp; |
120 | u8 inserted_remote_id; | |
121 | }; | |
122 | ||
123 | struct cm_id_private { | |
124 | struct ib_cm_id id; | |
125 | ||
126 | struct rb_node service_node; | |
127 | struct rb_node sidr_id_node; | |
87fd1a11 | 128 | spinlock_t lock; /* Do not acquire inside cm.lock */ |
1b52fa98 | 129 | struct completion comp; |
a977049d HR |
130 | atomic_t refcount; |
131 | ||
132 | struct ib_mad_send_buf *msg; | |
133 | struct cm_timewait_info *timewait_info; | |
134 | /* todo: use alternate port on send failure */ | |
135 | struct cm_av av; | |
136 | struct cm_av alt_av; | |
6e61d04f | 137 | struct ib_cm_compare_data *compare_data; |
a977049d HR |
138 | |
139 | void *private_data; | |
97f52eb4 SH |
140 | __be64 tid; |
141 | __be32 local_qpn; | |
142 | __be32 remote_qpn; | |
ae7971a7 | 143 | enum ib_qp_type qp_type; |
97f52eb4 SH |
144 | __be32 sq_psn; |
145 | __be32 rq_psn; | |
a977049d HR |
146 | int timeout_ms; |
147 | enum ib_mtu path_mtu; | |
e1444b5a | 148 | __be16 pkey; |
a977049d HR |
149 | u8 private_data_len; |
150 | u8 max_cm_retries; | |
151 | u8 peer_to_peer; | |
152 | u8 responder_resources; | |
153 | u8 initiator_depth; | |
a977049d HR |
154 | u8 retry_count; |
155 | u8 rnr_retry_count; | |
156 | u8 service_timeout; | |
157 | ||
158 | struct list_head work_list; | |
159 | atomic_t work_count; | |
160 | }; | |
161 | ||
c4028958 | 162 | static void cm_work_handler(struct work_struct *work); |
a977049d HR |
163 | |
164 | static inline void cm_deref_id(struct cm_id_private *cm_id_priv) | |
165 | { | |
166 | if (atomic_dec_and_test(&cm_id_priv->refcount)) | |
1b52fa98 | 167 | complete(&cm_id_priv->comp); |
a977049d HR |
168 | } |
169 | ||
170 | static int cm_alloc_msg(struct cm_id_private *cm_id_priv, | |
171 | struct ib_mad_send_buf **msg) | |
172 | { | |
173 | struct ib_mad_agent *mad_agent; | |
174 | struct ib_mad_send_buf *m; | |
175 | struct ib_ah *ah; | |
176 | ||
177 | mad_agent = cm_id_priv->av.port->mad_agent; | |
178 | ah = ib_create_ah(mad_agent->qp->pd, &cm_id_priv->av.ah_attr); | |
179 | if (IS_ERR(ah)) | |
180 | return PTR_ERR(ah); | |
181 | ||
3cd96564 | 182 | m = ib_create_send_mad(mad_agent, cm_id_priv->id.remote_cm_qpn, |
354ba39c | 183 | cm_id_priv->av.pkey_index, |
34816ad9 | 184 | 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA, |
a977049d HR |
185 | GFP_ATOMIC); |
186 | if (IS_ERR(m)) { | |
187 | ib_destroy_ah(ah); | |
188 | return PTR_ERR(m); | |
189 | } | |
190 | ||
191 | /* Timeout set by caller if response is expected. */ | |
34816ad9 SH |
192 | m->ah = ah; |
193 | m->retries = cm_id_priv->max_cm_retries; | |
a977049d HR |
194 | |
195 | atomic_inc(&cm_id_priv->refcount); | |
196 | m->context[0] = cm_id_priv; | |
197 | *msg = m; | |
198 | return 0; | |
199 | } | |
200 | ||
201 | static int cm_alloc_response_msg(struct cm_port *port, | |
202 | struct ib_mad_recv_wc *mad_recv_wc, | |
203 | struct ib_mad_send_buf **msg) | |
204 | { | |
205 | struct ib_mad_send_buf *m; | |
206 | struct ib_ah *ah; | |
207 | ||
208 | ah = ib_create_ah_from_wc(port->mad_agent->qp->pd, mad_recv_wc->wc, | |
209 | mad_recv_wc->recv_buf.grh, port->port_num); | |
210 | if (IS_ERR(ah)) | |
211 | return PTR_ERR(ah); | |
212 | ||
213 | m = ib_create_send_mad(port->mad_agent, 1, mad_recv_wc->wc->pkey_index, | |
34816ad9 | 214 | 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA, |
a977049d HR |
215 | GFP_ATOMIC); |
216 | if (IS_ERR(m)) { | |
217 | ib_destroy_ah(ah); | |
218 | return PTR_ERR(m); | |
219 | } | |
34816ad9 | 220 | m->ah = ah; |
a977049d HR |
221 | *msg = m; |
222 | return 0; | |
223 | } | |
224 | ||
225 | static void cm_free_msg(struct ib_mad_send_buf *msg) | |
226 | { | |
34816ad9 | 227 | ib_destroy_ah(msg->ah); |
a977049d HR |
228 | if (msg->context[0]) |
229 | cm_deref_id(msg->context[0]); | |
230 | ib_free_send_mad(msg); | |
231 | } | |
232 | ||
233 | static void * cm_copy_private_data(const void *private_data, | |
234 | u8 private_data_len) | |
235 | { | |
236 | void *data; | |
237 | ||
238 | if (!private_data || !private_data_len) | |
239 | return NULL; | |
240 | ||
bed8bdfd | 241 | data = kmemdup(private_data, private_data_len, GFP_KERNEL); |
a977049d HR |
242 | if (!data) |
243 | return ERR_PTR(-ENOMEM); | |
244 | ||
a977049d HR |
245 | return data; |
246 | } | |
247 | ||
248 | static void cm_set_private_data(struct cm_id_private *cm_id_priv, | |
249 | void *private_data, u8 private_data_len) | |
250 | { | |
251 | if (cm_id_priv->private_data && cm_id_priv->private_data_len) | |
252 | kfree(cm_id_priv->private_data); | |
253 | ||
254 | cm_id_priv->private_data = private_data; | |
255 | cm_id_priv->private_data_len = private_data_len; | |
256 | } | |
257 | ||
ca222c6b SH |
258 | static void cm_init_av_for_response(struct cm_port *port, struct ib_wc *wc, |
259 | struct ib_grh *grh, struct cm_av *av) | |
a977049d HR |
260 | { |
261 | av->port = port; | |
262 | av->pkey_index = wc->pkey_index; | |
ca222c6b SH |
263 | ib_init_ah_from_wc(port->cm_dev->device, port->port_num, wc, |
264 | grh, &av->ah_attr); | |
a977049d HR |
265 | } |
266 | ||
267 | static int cm_init_av_by_path(struct ib_sa_path_rec *path, struct cm_av *av) | |
268 | { | |
269 | struct cm_device *cm_dev; | |
270 | struct cm_port *port = NULL; | |
271 | unsigned long flags; | |
272 | int ret; | |
273 | u8 p; | |
274 | ||
275 | read_lock_irqsave(&cm.device_lock, flags); | |
276 | list_for_each_entry(cm_dev, &cm.device_list, list) { | |
277 | if (!ib_find_cached_gid(cm_dev->device, &path->sgid, | |
278 | &p, NULL)) { | |
279 | port = &cm_dev->port[p-1]; | |
280 | break; | |
281 | } | |
282 | } | |
283 | read_unlock_irqrestore(&cm.device_lock, flags); | |
284 | ||
285 | if (!port) | |
286 | return -EINVAL; | |
287 | ||
288 | ret = ib_find_cached_pkey(cm_dev->device, port->port_num, | |
289 | be16_to_cpu(path->pkey), &av->pkey_index); | |
290 | if (ret) | |
291 | return ret; | |
292 | ||
293 | av->port = port; | |
ca222c6b SH |
294 | ib_init_ah_from_path(cm_dev->device, port->port_num, path, |
295 | &av->ah_attr); | |
a977049d HR |
296 | av->packet_life_time = path->packet_life_time; |
297 | return 0; | |
298 | } | |
299 | ||
300 | static int cm_alloc_id(struct cm_id_private *cm_id_priv) | |
301 | { | |
302 | unsigned long flags; | |
f06d2653 | 303 | int ret, id; |
de1bb1a6 | 304 | static int next_id; |
a977049d HR |
305 | |
306 | do { | |
307 | spin_lock_irqsave(&cm.lock, flags); | |
f06d2653 | 308 | ret = idr_get_new_above(&cm.local_id_table, cm_id_priv, |
9f81036c MT |
309 | next_id, &id); |
310 | if (!ret) | |
311 | next_id = ((unsigned) id + 1) & MAX_ID_MASK; | |
a977049d HR |
312 | spin_unlock_irqrestore(&cm.lock, flags); |
313 | } while( (ret == -EAGAIN) && idr_pre_get(&cm.local_id_table, GFP_KERNEL) ); | |
f06d2653 SH |
314 | |
315 | cm_id_priv->id.local_id = (__force __be32) (id ^ cm.random_id_operand); | |
a977049d HR |
316 | return ret; |
317 | } | |
318 | ||
97f52eb4 | 319 | static void cm_free_id(__be32 local_id) |
a977049d | 320 | { |
24be6e81 | 321 | spin_lock_irq(&cm.lock); |
f06d2653 SH |
322 | idr_remove(&cm.local_id_table, |
323 | (__force int) (local_id ^ cm.random_id_operand)); | |
24be6e81 | 324 | spin_unlock_irq(&cm.lock); |
a977049d HR |
325 | } |
326 | ||
97f52eb4 | 327 | static struct cm_id_private * cm_get_id(__be32 local_id, __be32 remote_id) |
a977049d HR |
328 | { |
329 | struct cm_id_private *cm_id_priv; | |
330 | ||
f06d2653 SH |
331 | cm_id_priv = idr_find(&cm.local_id_table, |
332 | (__force int) (local_id ^ cm.random_id_operand)); | |
a977049d HR |
333 | if (cm_id_priv) { |
334 | if (cm_id_priv->id.remote_id == remote_id) | |
335 | atomic_inc(&cm_id_priv->refcount); | |
336 | else | |
337 | cm_id_priv = NULL; | |
338 | } | |
339 | ||
340 | return cm_id_priv; | |
341 | } | |
342 | ||
97f52eb4 | 343 | static struct cm_id_private * cm_acquire_id(__be32 local_id, __be32 remote_id) |
a977049d HR |
344 | { |
345 | struct cm_id_private *cm_id_priv; | |
a977049d | 346 | |
24be6e81 | 347 | spin_lock_irq(&cm.lock); |
a977049d | 348 | cm_id_priv = cm_get_id(local_id, remote_id); |
24be6e81 | 349 | spin_unlock_irq(&cm.lock); |
a977049d HR |
350 | |
351 | return cm_id_priv; | |
352 | } | |
353 | ||
6e61d04f SH |
354 | static void cm_mask_copy(u8 *dst, u8 *src, u8 *mask) |
355 | { | |
356 | int i; | |
357 | ||
358 | for (i = 0; i < IB_CM_COMPARE_SIZE / sizeof(unsigned long); i++) | |
359 | ((unsigned long *) dst)[i] = ((unsigned long *) src)[i] & | |
360 | ((unsigned long *) mask)[i]; | |
361 | } | |
362 | ||
363 | static int cm_compare_data(struct ib_cm_compare_data *src_data, | |
364 | struct ib_cm_compare_data *dst_data) | |
365 | { | |
366 | u8 src[IB_CM_COMPARE_SIZE]; | |
367 | u8 dst[IB_CM_COMPARE_SIZE]; | |
368 | ||
369 | if (!src_data || !dst_data) | |
370 | return 0; | |
371 | ||
372 | cm_mask_copy(src, src_data->data, dst_data->mask); | |
373 | cm_mask_copy(dst, dst_data->data, src_data->mask); | |
374 | return memcmp(src, dst, IB_CM_COMPARE_SIZE); | |
375 | } | |
376 | ||
377 | static int cm_compare_private_data(u8 *private_data, | |
378 | struct ib_cm_compare_data *dst_data) | |
379 | { | |
380 | u8 src[IB_CM_COMPARE_SIZE]; | |
381 | ||
382 | if (!dst_data) | |
383 | return 0; | |
384 | ||
385 | cm_mask_copy(src, private_data, dst_data->mask); | |
386 | return memcmp(src, dst_data->data, IB_CM_COMPARE_SIZE); | |
387 | } | |
388 | ||
a977049d HR |
389 | static struct cm_id_private * cm_insert_listen(struct cm_id_private *cm_id_priv) |
390 | { | |
391 | struct rb_node **link = &cm.listen_service_table.rb_node; | |
392 | struct rb_node *parent = NULL; | |
393 | struct cm_id_private *cur_cm_id_priv; | |
97f52eb4 SH |
394 | __be64 service_id = cm_id_priv->id.service_id; |
395 | __be64 service_mask = cm_id_priv->id.service_mask; | |
6e61d04f | 396 | int data_cmp; |
a977049d HR |
397 | |
398 | while (*link) { | |
399 | parent = *link; | |
400 | cur_cm_id_priv = rb_entry(parent, struct cm_id_private, | |
401 | service_node); | |
6e61d04f SH |
402 | data_cmp = cm_compare_data(cm_id_priv->compare_data, |
403 | cur_cm_id_priv->compare_data); | |
a977049d | 404 | if ((cur_cm_id_priv->id.service_mask & service_id) == |
07d357d0 | 405 | (service_mask & cur_cm_id_priv->id.service_id) && |
6e61d04f SH |
406 | (cm_id_priv->id.device == cur_cm_id_priv->id.device) && |
407 | !data_cmp) | |
07d357d0 SH |
408 | return cur_cm_id_priv; |
409 | ||
410 | if (cm_id_priv->id.device < cur_cm_id_priv->id.device) | |
411 | link = &(*link)->rb_left; | |
412 | else if (cm_id_priv->id.device > cur_cm_id_priv->id.device) | |
413 | link = &(*link)->rb_right; | |
414 | else if (service_id < cur_cm_id_priv->id.service_id) | |
a977049d | 415 | link = &(*link)->rb_left; |
6e61d04f SH |
416 | else if (service_id > cur_cm_id_priv->id.service_id) |
417 | link = &(*link)->rb_right; | |
418 | else if (data_cmp < 0) | |
419 | link = &(*link)->rb_left; | |
a977049d HR |
420 | else |
421 | link = &(*link)->rb_right; | |
422 | } | |
423 | rb_link_node(&cm_id_priv->service_node, parent, link); | |
424 | rb_insert_color(&cm_id_priv->service_node, &cm.listen_service_table); | |
425 | return NULL; | |
426 | } | |
427 | ||
07d357d0 | 428 | static struct cm_id_private * cm_find_listen(struct ib_device *device, |
6e61d04f SH |
429 | __be64 service_id, |
430 | u8 *private_data) | |
a977049d HR |
431 | { |
432 | struct rb_node *node = cm.listen_service_table.rb_node; | |
433 | struct cm_id_private *cm_id_priv; | |
6e61d04f | 434 | int data_cmp; |
a977049d HR |
435 | |
436 | while (node) { | |
437 | cm_id_priv = rb_entry(node, struct cm_id_private, service_node); | |
6e61d04f SH |
438 | data_cmp = cm_compare_private_data(private_data, |
439 | cm_id_priv->compare_data); | |
a977049d | 440 | if ((cm_id_priv->id.service_mask & service_id) == |
07d357d0 | 441 | cm_id_priv->id.service_id && |
6e61d04f | 442 | (cm_id_priv->id.device == device) && !data_cmp) |
a977049d | 443 | return cm_id_priv; |
07d357d0 SH |
444 | |
445 | if (device < cm_id_priv->id.device) | |
446 | node = node->rb_left; | |
447 | else if (device > cm_id_priv->id.device) | |
448 | node = node->rb_right; | |
449 | else if (service_id < cm_id_priv->id.service_id) | |
a977049d | 450 | node = node->rb_left; |
6e61d04f SH |
451 | else if (service_id > cm_id_priv->id.service_id) |
452 | node = node->rb_right; | |
453 | else if (data_cmp < 0) | |
454 | node = node->rb_left; | |
a977049d HR |
455 | else |
456 | node = node->rb_right; | |
457 | } | |
458 | return NULL; | |
459 | } | |
460 | ||
461 | static struct cm_timewait_info * cm_insert_remote_id(struct cm_timewait_info | |
462 | *timewait_info) | |
463 | { | |
464 | struct rb_node **link = &cm.remote_id_table.rb_node; | |
465 | struct rb_node *parent = NULL; | |
466 | struct cm_timewait_info *cur_timewait_info; | |
97f52eb4 SH |
467 | __be64 remote_ca_guid = timewait_info->remote_ca_guid; |
468 | __be32 remote_id = timewait_info->work.remote_id; | |
a977049d HR |
469 | |
470 | while (*link) { | |
471 | parent = *link; | |
472 | cur_timewait_info = rb_entry(parent, struct cm_timewait_info, | |
473 | remote_id_node); | |
474 | if (remote_id < cur_timewait_info->work.remote_id) | |
475 | link = &(*link)->rb_left; | |
476 | else if (remote_id > cur_timewait_info->work.remote_id) | |
477 | link = &(*link)->rb_right; | |
478 | else if (remote_ca_guid < cur_timewait_info->remote_ca_guid) | |
479 | link = &(*link)->rb_left; | |
480 | else if (remote_ca_guid > cur_timewait_info->remote_ca_guid) | |
481 | link = &(*link)->rb_right; | |
482 | else | |
483 | return cur_timewait_info; | |
484 | } | |
485 | timewait_info->inserted_remote_id = 1; | |
486 | rb_link_node(&timewait_info->remote_id_node, parent, link); | |
487 | rb_insert_color(&timewait_info->remote_id_node, &cm.remote_id_table); | |
488 | return NULL; | |
489 | } | |
490 | ||
97f52eb4 SH |
491 | static struct cm_timewait_info * cm_find_remote_id(__be64 remote_ca_guid, |
492 | __be32 remote_id) | |
a977049d HR |
493 | { |
494 | struct rb_node *node = cm.remote_id_table.rb_node; | |
495 | struct cm_timewait_info *timewait_info; | |
496 | ||
497 | while (node) { | |
498 | timewait_info = rb_entry(node, struct cm_timewait_info, | |
499 | remote_id_node); | |
500 | if (remote_id < timewait_info->work.remote_id) | |
501 | node = node->rb_left; | |
502 | else if (remote_id > timewait_info->work.remote_id) | |
503 | node = node->rb_right; | |
504 | else if (remote_ca_guid < timewait_info->remote_ca_guid) | |
505 | node = node->rb_left; | |
506 | else if (remote_ca_guid > timewait_info->remote_ca_guid) | |
507 | node = node->rb_right; | |
508 | else | |
509 | return timewait_info; | |
510 | } | |
511 | return NULL; | |
512 | } | |
513 | ||
514 | static struct cm_timewait_info * cm_insert_remote_qpn(struct cm_timewait_info | |
515 | *timewait_info) | |
516 | { | |
517 | struct rb_node **link = &cm.remote_qp_table.rb_node; | |
518 | struct rb_node *parent = NULL; | |
519 | struct cm_timewait_info *cur_timewait_info; | |
97f52eb4 SH |
520 | __be64 remote_ca_guid = timewait_info->remote_ca_guid; |
521 | __be32 remote_qpn = timewait_info->remote_qpn; | |
a977049d HR |
522 | |
523 | while (*link) { | |
524 | parent = *link; | |
525 | cur_timewait_info = rb_entry(parent, struct cm_timewait_info, | |
526 | remote_qp_node); | |
527 | if (remote_qpn < cur_timewait_info->remote_qpn) | |
528 | link = &(*link)->rb_left; | |
529 | else if (remote_qpn > cur_timewait_info->remote_qpn) | |
530 | link = &(*link)->rb_right; | |
531 | else if (remote_ca_guid < cur_timewait_info->remote_ca_guid) | |
532 | link = &(*link)->rb_left; | |
533 | else if (remote_ca_guid > cur_timewait_info->remote_ca_guid) | |
534 | link = &(*link)->rb_right; | |
535 | else | |
536 | return cur_timewait_info; | |
537 | } | |
538 | timewait_info->inserted_remote_qp = 1; | |
539 | rb_link_node(&timewait_info->remote_qp_node, parent, link); | |
540 | rb_insert_color(&timewait_info->remote_qp_node, &cm.remote_qp_table); | |
541 | return NULL; | |
542 | } | |
543 | ||
544 | static struct cm_id_private * cm_insert_remote_sidr(struct cm_id_private | |
545 | *cm_id_priv) | |
546 | { | |
547 | struct rb_node **link = &cm.remote_sidr_table.rb_node; | |
548 | struct rb_node *parent = NULL; | |
549 | struct cm_id_private *cur_cm_id_priv; | |
550 | union ib_gid *port_gid = &cm_id_priv->av.dgid; | |
97f52eb4 | 551 | __be32 remote_id = cm_id_priv->id.remote_id; |
a977049d HR |
552 | |
553 | while (*link) { | |
554 | parent = *link; | |
555 | cur_cm_id_priv = rb_entry(parent, struct cm_id_private, | |
556 | sidr_id_node); | |
557 | if (remote_id < cur_cm_id_priv->id.remote_id) | |
558 | link = &(*link)->rb_left; | |
559 | else if (remote_id > cur_cm_id_priv->id.remote_id) | |
560 | link = &(*link)->rb_right; | |
561 | else { | |
562 | int cmp; | |
563 | cmp = memcmp(port_gid, &cur_cm_id_priv->av.dgid, | |
564 | sizeof *port_gid); | |
565 | if (cmp < 0) | |
566 | link = &(*link)->rb_left; | |
567 | else if (cmp > 0) | |
568 | link = &(*link)->rb_right; | |
569 | else | |
570 | return cur_cm_id_priv; | |
571 | } | |
572 | } | |
573 | rb_link_node(&cm_id_priv->sidr_id_node, parent, link); | |
574 | rb_insert_color(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table); | |
575 | return NULL; | |
576 | } | |
577 | ||
578 | static void cm_reject_sidr_req(struct cm_id_private *cm_id_priv, | |
579 | enum ib_cm_sidr_status status) | |
580 | { | |
581 | struct ib_cm_sidr_rep_param param; | |
582 | ||
583 | memset(¶m, 0, sizeof param); | |
584 | param.status = status; | |
585 | ib_send_cm_sidr_rep(&cm_id_priv->id, ¶m); | |
586 | } | |
587 | ||
07d357d0 SH |
588 | struct ib_cm_id *ib_create_cm_id(struct ib_device *device, |
589 | ib_cm_handler cm_handler, | |
a977049d HR |
590 | void *context) |
591 | { | |
592 | struct cm_id_private *cm_id_priv; | |
593 | int ret; | |
594 | ||
de6eb66b | 595 | cm_id_priv = kzalloc(sizeof *cm_id_priv, GFP_KERNEL); |
a977049d HR |
596 | if (!cm_id_priv) |
597 | return ERR_PTR(-ENOMEM); | |
598 | ||
a977049d | 599 | cm_id_priv->id.state = IB_CM_IDLE; |
07d357d0 | 600 | cm_id_priv->id.device = device; |
a977049d HR |
601 | cm_id_priv->id.cm_handler = cm_handler; |
602 | cm_id_priv->id.context = context; | |
354ba39c | 603 | cm_id_priv->id.remote_cm_qpn = 1; |
a977049d HR |
604 | ret = cm_alloc_id(cm_id_priv); |
605 | if (ret) | |
606 | goto error; | |
607 | ||
608 | spin_lock_init(&cm_id_priv->lock); | |
1b52fa98 | 609 | init_completion(&cm_id_priv->comp); |
a977049d HR |
610 | INIT_LIST_HEAD(&cm_id_priv->work_list); |
611 | atomic_set(&cm_id_priv->work_count, -1); | |
612 | atomic_set(&cm_id_priv->refcount, 1); | |
613 | return &cm_id_priv->id; | |
614 | ||
615 | error: | |
616 | kfree(cm_id_priv); | |
617 | return ERR_PTR(-ENOMEM); | |
618 | } | |
619 | EXPORT_SYMBOL(ib_create_cm_id); | |
620 | ||
621 | static struct cm_work * cm_dequeue_work(struct cm_id_private *cm_id_priv) | |
622 | { | |
623 | struct cm_work *work; | |
624 | ||
625 | if (list_empty(&cm_id_priv->work_list)) | |
626 | return NULL; | |
627 | ||
628 | work = list_entry(cm_id_priv->work_list.next, struct cm_work, list); | |
629 | list_del(&work->list); | |
630 | return work; | |
631 | } | |
632 | ||
633 | static void cm_free_work(struct cm_work *work) | |
634 | { | |
635 | if (work->mad_recv_wc) | |
636 | ib_free_recv_mad(work->mad_recv_wc); | |
637 | kfree(work); | |
638 | } | |
639 | ||
640 | static inline int cm_convert_to_ms(int iba_time) | |
641 | { | |
642 | /* approximate conversion to ms from 4.096us x 2^iba_time */ | |
643 | return 1 << max(iba_time - 8, 0); | |
644 | } | |
645 | ||
646 | static void cm_cleanup_timewait(struct cm_timewait_info *timewait_info) | |
647 | { | |
a977049d HR |
648 | if (timewait_info->inserted_remote_id) { |
649 | rb_erase(&timewait_info->remote_id_node, &cm.remote_id_table); | |
650 | timewait_info->inserted_remote_id = 0; | |
651 | } | |
652 | ||
653 | if (timewait_info->inserted_remote_qp) { | |
654 | rb_erase(&timewait_info->remote_qp_node, &cm.remote_qp_table); | |
655 | timewait_info->inserted_remote_qp = 0; | |
656 | } | |
a977049d HR |
657 | } |
658 | ||
97f52eb4 | 659 | static struct cm_timewait_info * cm_create_timewait_info(__be32 local_id) |
a977049d HR |
660 | { |
661 | struct cm_timewait_info *timewait_info; | |
662 | ||
de6eb66b | 663 | timewait_info = kzalloc(sizeof *timewait_info, GFP_KERNEL); |
a977049d HR |
664 | if (!timewait_info) |
665 | return ERR_PTR(-ENOMEM); | |
a977049d HR |
666 | |
667 | timewait_info->work.local_id = local_id; | |
c4028958 | 668 | INIT_DELAYED_WORK(&timewait_info->work.work, cm_work_handler); |
a977049d HR |
669 | timewait_info->work.cm_event.event = IB_CM_TIMEWAIT_EXIT; |
670 | return timewait_info; | |
671 | } | |
672 | ||
673 | static void cm_enter_timewait(struct cm_id_private *cm_id_priv) | |
674 | { | |
675 | int wait_time; | |
8575329d | 676 | unsigned long flags; |
a977049d | 677 | |
8575329d | 678 | spin_lock_irqsave(&cm.lock, flags); |
a70d0590 | 679 | cm_cleanup_timewait(cm_id_priv->timewait_info); |
8575329d SH |
680 | list_add_tail(&cm_id_priv->timewait_info->list, &cm.timewait_list); |
681 | spin_unlock_irqrestore(&cm.lock, flags); | |
a70d0590 | 682 | |
a977049d HR |
683 | /* |
684 | * The cm_id could be destroyed by the user before we exit timewait. | |
685 | * To protect against this, we search for the cm_id after exiting | |
686 | * timewait before notifying the user that we've exited timewait. | |
687 | */ | |
688 | cm_id_priv->id.state = IB_CM_TIMEWAIT; | |
e1444b5a | 689 | wait_time = cm_convert_to_ms(cm_id_priv->av.packet_life_time + 1); |
a977049d HR |
690 | queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work, |
691 | msecs_to_jiffies(wait_time)); | |
692 | cm_id_priv->timewait_info = NULL; | |
693 | } | |
694 | ||
695 | static void cm_reset_to_idle(struct cm_id_private *cm_id_priv) | |
696 | { | |
8575329d SH |
697 | unsigned long flags; |
698 | ||
a977049d HR |
699 | cm_id_priv->id.state = IB_CM_IDLE; |
700 | if (cm_id_priv->timewait_info) { | |
8575329d | 701 | spin_lock_irqsave(&cm.lock, flags); |
a977049d | 702 | cm_cleanup_timewait(cm_id_priv->timewait_info); |
8575329d | 703 | spin_unlock_irqrestore(&cm.lock, flags); |
a977049d HR |
704 | kfree(cm_id_priv->timewait_info); |
705 | cm_id_priv->timewait_info = NULL; | |
706 | } | |
707 | } | |
708 | ||
04c33543 | 709 | static void cm_destroy_id(struct ib_cm_id *cm_id, int err) |
a977049d HR |
710 | { |
711 | struct cm_id_private *cm_id_priv; | |
712 | struct cm_work *work; | |
a977049d HR |
713 | |
714 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); | |
715 | retest: | |
24be6e81 | 716 | spin_lock_irq(&cm_id_priv->lock); |
a977049d HR |
717 | switch (cm_id->state) { |
718 | case IB_CM_LISTEN: | |
719 | cm_id->state = IB_CM_IDLE; | |
24be6e81 SH |
720 | spin_unlock_irq(&cm_id_priv->lock); |
721 | spin_lock_irq(&cm.lock); | |
a977049d | 722 | rb_erase(&cm_id_priv->service_node, &cm.listen_service_table); |
24be6e81 | 723 | spin_unlock_irq(&cm.lock); |
a977049d HR |
724 | break; |
725 | case IB_CM_SIDR_REQ_SENT: | |
726 | cm_id->state = IB_CM_IDLE; | |
34816ad9 | 727 | ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); |
24be6e81 | 728 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
729 | break; |
730 | case IB_CM_SIDR_REQ_RCVD: | |
24be6e81 | 731 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
732 | cm_reject_sidr_req(cm_id_priv, IB_SIDR_REJECT); |
733 | break; | |
734 | case IB_CM_REQ_SENT: | |
227eca83 | 735 | ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); |
24be6e81 | 736 | spin_unlock_irq(&cm_id_priv->lock); |
227eca83 | 737 | ib_send_cm_rej(cm_id, IB_CM_REJ_TIMEOUT, |
e971b8cd SH |
738 | &cm_id_priv->id.device->node_guid, |
739 | sizeof cm_id_priv->id.device->node_guid, | |
227eca83 SH |
740 | NULL, 0); |
741 | break; | |
04c33543 MT |
742 | case IB_CM_REQ_RCVD: |
743 | if (err == -ENOMEM) { | |
744 | /* Do not reject to allow future retries. */ | |
745 | cm_reset_to_idle(cm_id_priv); | |
24be6e81 | 746 | spin_unlock_irq(&cm_id_priv->lock); |
04c33543 | 747 | } else { |
24be6e81 | 748 | spin_unlock_irq(&cm_id_priv->lock); |
04c33543 MT |
749 | ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, |
750 | NULL, 0, NULL, 0); | |
751 | } | |
752 | break; | |
a977049d HR |
753 | case IB_CM_MRA_REQ_RCVD: |
754 | case IB_CM_REP_SENT: | |
755 | case IB_CM_MRA_REP_RCVD: | |
34816ad9 | 756 | ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); |
a977049d | 757 | /* Fall through */ |
a977049d HR |
758 | case IB_CM_MRA_REQ_SENT: |
759 | case IB_CM_REP_RCVD: | |
760 | case IB_CM_MRA_REP_SENT: | |
24be6e81 | 761 | spin_unlock_irq(&cm_id_priv->lock); |
227eca83 SH |
762 | ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, |
763 | NULL, 0, NULL, 0); | |
a977049d HR |
764 | break; |
765 | case IB_CM_ESTABLISHED: | |
24be6e81 | 766 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
767 | ib_send_cm_dreq(cm_id, NULL, 0); |
768 | goto retest; | |
769 | case IB_CM_DREQ_SENT: | |
34816ad9 | 770 | ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); |
a977049d | 771 | cm_enter_timewait(cm_id_priv); |
24be6e81 | 772 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
773 | break; |
774 | case IB_CM_DREQ_RCVD: | |
24be6e81 | 775 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
776 | ib_send_cm_drep(cm_id, NULL, 0); |
777 | break; | |
778 | default: | |
24be6e81 | 779 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
780 | break; |
781 | } | |
782 | ||
783 | cm_free_id(cm_id->local_id); | |
1b52fa98 SH |
784 | cm_deref_id(cm_id_priv); |
785 | wait_for_completion(&cm_id_priv->comp); | |
a977049d HR |
786 | while ((work = cm_dequeue_work(cm_id_priv)) != NULL) |
787 | cm_free_work(work); | |
6e61d04f SH |
788 | kfree(cm_id_priv->compare_data); |
789 | kfree(cm_id_priv->private_data); | |
a977049d HR |
790 | kfree(cm_id_priv); |
791 | } | |
04c33543 MT |
792 | |
793 | void ib_destroy_cm_id(struct ib_cm_id *cm_id) | |
794 | { | |
795 | cm_destroy_id(cm_id, 0); | |
796 | } | |
a977049d HR |
797 | EXPORT_SYMBOL(ib_destroy_cm_id); |
798 | ||
6e61d04f SH |
799 | int ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id, __be64 service_mask, |
800 | struct ib_cm_compare_data *compare_data) | |
a977049d HR |
801 | { |
802 | struct cm_id_private *cm_id_priv, *cur_cm_id_priv; | |
803 | unsigned long flags; | |
804 | int ret = 0; | |
805 | ||
97f52eb4 SH |
806 | service_mask = service_mask ? service_mask : |
807 | __constant_cpu_to_be64(~0ULL); | |
a977049d HR |
808 | service_id &= service_mask; |
809 | if ((service_id & IB_SERVICE_ID_AGN_MASK) == IB_CM_ASSIGN_SERVICE_ID && | |
810 | (service_id != IB_CM_ASSIGN_SERVICE_ID)) | |
811 | return -EINVAL; | |
812 | ||
813 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); | |
6e61d04f SH |
814 | if (cm_id->state != IB_CM_IDLE) |
815 | return -EINVAL; | |
816 | ||
817 | if (compare_data) { | |
818 | cm_id_priv->compare_data = kzalloc(sizeof *compare_data, | |
819 | GFP_KERNEL); | |
820 | if (!cm_id_priv->compare_data) | |
821 | return -ENOMEM; | |
822 | cm_mask_copy(cm_id_priv->compare_data->data, | |
823 | compare_data->data, compare_data->mask); | |
824 | memcpy(cm_id_priv->compare_data->mask, compare_data->mask, | |
825 | IB_CM_COMPARE_SIZE); | |
826 | } | |
a977049d HR |
827 | |
828 | cm_id->state = IB_CM_LISTEN; | |
829 | ||
830 | spin_lock_irqsave(&cm.lock, flags); | |
831 | if (service_id == IB_CM_ASSIGN_SERVICE_ID) { | |
97f52eb4 SH |
832 | cm_id->service_id = cpu_to_be64(cm.listen_service_id++); |
833 | cm_id->service_mask = __constant_cpu_to_be64(~0ULL); | |
a977049d HR |
834 | } else { |
835 | cm_id->service_id = service_id; | |
836 | cm_id->service_mask = service_mask; | |
837 | } | |
838 | cur_cm_id_priv = cm_insert_listen(cm_id_priv); | |
839 | spin_unlock_irqrestore(&cm.lock, flags); | |
840 | ||
841 | if (cur_cm_id_priv) { | |
842 | cm_id->state = IB_CM_IDLE; | |
6e61d04f SH |
843 | kfree(cm_id_priv->compare_data); |
844 | cm_id_priv->compare_data = NULL; | |
a977049d HR |
845 | ret = -EBUSY; |
846 | } | |
847 | return ret; | |
848 | } | |
849 | EXPORT_SYMBOL(ib_cm_listen); | |
850 | ||
97f52eb4 SH |
851 | static __be64 cm_form_tid(struct cm_id_private *cm_id_priv, |
852 | enum cm_msg_sequence msg_seq) | |
a977049d HR |
853 | { |
854 | u64 hi_tid, low_tid; | |
855 | ||
856 | hi_tid = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32; | |
97f52eb4 SH |
857 | low_tid = (u64) ((__force u32)cm_id_priv->id.local_id | |
858 | (msg_seq << 30)); | |
a977049d HR |
859 | return cpu_to_be64(hi_tid | low_tid); |
860 | } | |
861 | ||
862 | static void cm_format_mad_hdr(struct ib_mad_hdr *hdr, | |
97f52eb4 | 863 | __be16 attr_id, __be64 tid) |
a977049d HR |
864 | { |
865 | hdr->base_version = IB_MGMT_BASE_VERSION; | |
866 | hdr->mgmt_class = IB_MGMT_CLASS_CM; | |
867 | hdr->class_version = IB_CM_CLASS_VERSION; | |
868 | hdr->method = IB_MGMT_METHOD_SEND; | |
869 | hdr->attr_id = attr_id; | |
870 | hdr->tid = tid; | |
871 | } | |
872 | ||
873 | static void cm_format_req(struct cm_req_msg *req_msg, | |
874 | struct cm_id_private *cm_id_priv, | |
875 | struct ib_cm_req_param *param) | |
876 | { | |
877 | cm_format_mad_hdr(&req_msg->hdr, CM_REQ_ATTR_ID, | |
878 | cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_REQ)); | |
879 | ||
880 | req_msg->local_comm_id = cm_id_priv->id.local_id; | |
881 | req_msg->service_id = param->service_id; | |
e971b8cd | 882 | req_msg->local_ca_guid = cm_id_priv->id.device->node_guid; |
a977049d HR |
883 | cm_req_set_local_qpn(req_msg, cpu_to_be32(param->qp_num)); |
884 | cm_req_set_resp_res(req_msg, param->responder_resources); | |
885 | cm_req_set_init_depth(req_msg, param->initiator_depth); | |
886 | cm_req_set_remote_resp_timeout(req_msg, | |
887 | param->remote_cm_response_timeout); | |
888 | cm_req_set_qp_type(req_msg, param->qp_type); | |
889 | cm_req_set_flow_ctrl(req_msg, param->flow_control); | |
890 | cm_req_set_starting_psn(req_msg, cpu_to_be32(param->starting_psn)); | |
891 | cm_req_set_local_resp_timeout(req_msg, | |
892 | param->local_cm_response_timeout); | |
893 | cm_req_set_retry_count(req_msg, param->retry_count); | |
894 | req_msg->pkey = param->primary_path->pkey; | |
895 | cm_req_set_path_mtu(req_msg, param->primary_path->mtu); | |
896 | cm_req_set_rnr_retry_count(req_msg, param->rnr_retry_count); | |
897 | cm_req_set_max_cm_retries(req_msg, param->max_cm_retries); | |
898 | cm_req_set_srq(req_msg, param->srq); | |
899 | ||
900 | req_msg->primary_local_lid = param->primary_path->slid; | |
901 | req_msg->primary_remote_lid = param->primary_path->dlid; | |
902 | req_msg->primary_local_gid = param->primary_path->sgid; | |
903 | req_msg->primary_remote_gid = param->primary_path->dgid; | |
904 | cm_req_set_primary_flow_label(req_msg, param->primary_path->flow_label); | |
905 | cm_req_set_primary_packet_rate(req_msg, param->primary_path->rate); | |
906 | req_msg->primary_traffic_class = param->primary_path->traffic_class; | |
907 | req_msg->primary_hop_limit = param->primary_path->hop_limit; | |
908 | cm_req_set_primary_sl(req_msg, param->primary_path->sl); | |
909 | cm_req_set_primary_subnet_local(req_msg, 1); /* local only... */ | |
910 | cm_req_set_primary_local_ack_timeout(req_msg, | |
911 | min(31, param->primary_path->packet_life_time + 1)); | |
912 | ||
913 | if (param->alternate_path) { | |
914 | req_msg->alt_local_lid = param->alternate_path->slid; | |
915 | req_msg->alt_remote_lid = param->alternate_path->dlid; | |
916 | req_msg->alt_local_gid = param->alternate_path->sgid; | |
917 | req_msg->alt_remote_gid = param->alternate_path->dgid; | |
918 | cm_req_set_alt_flow_label(req_msg, | |
919 | param->alternate_path->flow_label); | |
920 | cm_req_set_alt_packet_rate(req_msg, param->alternate_path->rate); | |
921 | req_msg->alt_traffic_class = param->alternate_path->traffic_class; | |
922 | req_msg->alt_hop_limit = param->alternate_path->hop_limit; | |
923 | cm_req_set_alt_sl(req_msg, param->alternate_path->sl); | |
924 | cm_req_set_alt_subnet_local(req_msg, 1); /* local only... */ | |
925 | cm_req_set_alt_local_ack_timeout(req_msg, | |
926 | min(31, param->alternate_path->packet_life_time + 1)); | |
927 | } | |
928 | ||
929 | if (param->private_data && param->private_data_len) | |
930 | memcpy(req_msg->private_data, param->private_data, | |
931 | param->private_data_len); | |
932 | } | |
933 | ||
858119e1 | 934 | static int cm_validate_req_param(struct ib_cm_req_param *param) |
a977049d HR |
935 | { |
936 | /* peer-to-peer not supported */ | |
937 | if (param->peer_to_peer) | |
938 | return -EINVAL; | |
939 | ||
940 | if (!param->primary_path) | |
941 | return -EINVAL; | |
942 | ||
943 | if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC) | |
944 | return -EINVAL; | |
945 | ||
946 | if (param->private_data && | |
947 | param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE) | |
948 | return -EINVAL; | |
949 | ||
950 | if (param->alternate_path && | |
951 | (param->alternate_path->pkey != param->primary_path->pkey || | |
952 | param->alternate_path->mtu != param->primary_path->mtu)) | |
953 | return -EINVAL; | |
954 | ||
955 | return 0; | |
956 | } | |
957 | ||
958 | int ib_send_cm_req(struct ib_cm_id *cm_id, | |
959 | struct ib_cm_req_param *param) | |
960 | { | |
961 | struct cm_id_private *cm_id_priv; | |
a977049d HR |
962 | struct cm_req_msg *req_msg; |
963 | unsigned long flags; | |
964 | int ret; | |
965 | ||
966 | ret = cm_validate_req_param(param); | |
967 | if (ret) | |
968 | return ret; | |
969 | ||
970 | /* Verify that we're not in timewait. */ | |
971 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); | |
972 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
973 | if (cm_id->state != IB_CM_IDLE) { | |
974 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
975 | ret = -EINVAL; | |
976 | goto out; | |
977 | } | |
978 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
979 | ||
980 | cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv-> | |
981 | id.local_id); | |
75df23e2 SH |
982 | if (IS_ERR(cm_id_priv->timewait_info)) { |
983 | ret = PTR_ERR(cm_id_priv->timewait_info); | |
a977049d | 984 | goto out; |
75df23e2 | 985 | } |
a977049d HR |
986 | |
987 | ret = cm_init_av_by_path(param->primary_path, &cm_id_priv->av); | |
988 | if (ret) | |
989 | goto error1; | |
990 | if (param->alternate_path) { | |
991 | ret = cm_init_av_by_path(param->alternate_path, | |
992 | &cm_id_priv->alt_av); | |
993 | if (ret) | |
994 | goto error1; | |
995 | } | |
996 | cm_id->service_id = param->service_id; | |
97f52eb4 | 997 | cm_id->service_mask = __constant_cpu_to_be64(~0ULL); |
a977049d HR |
998 | cm_id_priv->timeout_ms = cm_convert_to_ms( |
999 | param->primary_path->packet_life_time) * 2 + | |
1000 | cm_convert_to_ms( | |
1001 | param->remote_cm_response_timeout); | |
1002 | cm_id_priv->max_cm_retries = param->max_cm_retries; | |
1003 | cm_id_priv->initiator_depth = param->initiator_depth; | |
1004 | cm_id_priv->responder_resources = param->responder_resources; | |
1005 | cm_id_priv->retry_count = param->retry_count; | |
1006 | cm_id_priv->path_mtu = param->primary_path->mtu; | |
e1444b5a | 1007 | cm_id_priv->pkey = param->primary_path->pkey; |
ae7971a7 | 1008 | cm_id_priv->qp_type = param->qp_type; |
a977049d HR |
1009 | |
1010 | ret = cm_alloc_msg(cm_id_priv, &cm_id_priv->msg); | |
1011 | if (ret) | |
1012 | goto error1; | |
1013 | ||
1014 | req_msg = (struct cm_req_msg *) cm_id_priv->msg->mad; | |
1015 | cm_format_req(req_msg, cm_id_priv, param); | |
1016 | cm_id_priv->tid = req_msg->hdr.tid; | |
34816ad9 | 1017 | cm_id_priv->msg->timeout_ms = cm_id_priv->timeout_ms; |
a977049d HR |
1018 | cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT; |
1019 | ||
1020 | cm_id_priv->local_qpn = cm_req_get_local_qpn(req_msg); | |
1021 | cm_id_priv->rq_psn = cm_req_get_starting_psn(req_msg); | |
a977049d HR |
1022 | |
1023 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
34816ad9 | 1024 | ret = ib_post_send_mad(cm_id_priv->msg, NULL); |
a977049d HR |
1025 | if (ret) { |
1026 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
1027 | goto error2; | |
1028 | } | |
1029 | BUG_ON(cm_id->state != IB_CM_IDLE); | |
1030 | cm_id->state = IB_CM_REQ_SENT; | |
1031 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
1032 | return 0; | |
1033 | ||
1034 | error2: cm_free_msg(cm_id_priv->msg); | |
1035 | error1: kfree(cm_id_priv->timewait_info); | |
1036 | out: return ret; | |
1037 | } | |
1038 | EXPORT_SYMBOL(ib_send_cm_req); | |
1039 | ||
1040 | static int cm_issue_rej(struct cm_port *port, | |
1041 | struct ib_mad_recv_wc *mad_recv_wc, | |
1042 | enum ib_cm_rej_reason reason, | |
1043 | enum cm_msg_response msg_rejected, | |
1044 | void *ari, u8 ari_length) | |
1045 | { | |
1046 | struct ib_mad_send_buf *msg = NULL; | |
a977049d HR |
1047 | struct cm_rej_msg *rej_msg, *rcv_msg; |
1048 | int ret; | |
1049 | ||
1050 | ret = cm_alloc_response_msg(port, mad_recv_wc, &msg); | |
1051 | if (ret) | |
1052 | return ret; | |
1053 | ||
1054 | /* We just need common CM header information. Cast to any message. */ | |
1055 | rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad; | |
1056 | rej_msg = (struct cm_rej_msg *) msg->mad; | |
1057 | ||
1058 | cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid); | |
1059 | rej_msg->remote_comm_id = rcv_msg->local_comm_id; | |
1060 | rej_msg->local_comm_id = rcv_msg->remote_comm_id; | |
1061 | cm_rej_set_msg_rejected(rej_msg, msg_rejected); | |
97f52eb4 | 1062 | rej_msg->reason = cpu_to_be16(reason); |
a977049d HR |
1063 | |
1064 | if (ari && ari_length) { | |
1065 | cm_rej_set_reject_info_len(rej_msg, ari_length); | |
1066 | memcpy(rej_msg->ari, ari, ari_length); | |
1067 | } | |
1068 | ||
34816ad9 | 1069 | ret = ib_post_send_mad(msg, NULL); |
a977049d HR |
1070 | if (ret) |
1071 | cm_free_msg(msg); | |
1072 | ||
1073 | return ret; | |
1074 | } | |
1075 | ||
97f52eb4 SH |
1076 | static inline int cm_is_active_peer(__be64 local_ca_guid, __be64 remote_ca_guid, |
1077 | __be32 local_qpn, __be32 remote_qpn) | |
a977049d HR |
1078 | { |
1079 | return (be64_to_cpu(local_ca_guid) > be64_to_cpu(remote_ca_guid) || | |
1080 | ((local_ca_guid == remote_ca_guid) && | |
1081 | (be32_to_cpu(local_qpn) > be32_to_cpu(remote_qpn)))); | |
1082 | } | |
1083 | ||
858119e1 | 1084 | static void cm_format_paths_from_req(struct cm_req_msg *req_msg, |
a977049d HR |
1085 | struct ib_sa_path_rec *primary_path, |
1086 | struct ib_sa_path_rec *alt_path) | |
1087 | { | |
1088 | memset(primary_path, 0, sizeof *primary_path); | |
1089 | primary_path->dgid = req_msg->primary_local_gid; | |
1090 | primary_path->sgid = req_msg->primary_remote_gid; | |
1091 | primary_path->dlid = req_msg->primary_local_lid; | |
1092 | primary_path->slid = req_msg->primary_remote_lid; | |
1093 | primary_path->flow_label = cm_req_get_primary_flow_label(req_msg); | |
1094 | primary_path->hop_limit = req_msg->primary_hop_limit; | |
1095 | primary_path->traffic_class = req_msg->primary_traffic_class; | |
1096 | primary_path->reversible = 1; | |
1097 | primary_path->pkey = req_msg->pkey; | |
1098 | primary_path->sl = cm_req_get_primary_sl(req_msg); | |
1099 | primary_path->mtu_selector = IB_SA_EQ; | |
1100 | primary_path->mtu = cm_req_get_path_mtu(req_msg); | |
1101 | primary_path->rate_selector = IB_SA_EQ; | |
1102 | primary_path->rate = cm_req_get_primary_packet_rate(req_msg); | |
1103 | primary_path->packet_life_time_selector = IB_SA_EQ; | |
1104 | primary_path->packet_life_time = | |
1105 | cm_req_get_primary_local_ack_timeout(req_msg); | |
1106 | primary_path->packet_life_time -= (primary_path->packet_life_time > 0); | |
1107 | ||
1108 | if (req_msg->alt_local_lid) { | |
1109 | memset(alt_path, 0, sizeof *alt_path); | |
1110 | alt_path->dgid = req_msg->alt_local_gid; | |
1111 | alt_path->sgid = req_msg->alt_remote_gid; | |
1112 | alt_path->dlid = req_msg->alt_local_lid; | |
1113 | alt_path->slid = req_msg->alt_remote_lid; | |
1114 | alt_path->flow_label = cm_req_get_alt_flow_label(req_msg); | |
1115 | alt_path->hop_limit = req_msg->alt_hop_limit; | |
1116 | alt_path->traffic_class = req_msg->alt_traffic_class; | |
1117 | alt_path->reversible = 1; | |
1118 | alt_path->pkey = req_msg->pkey; | |
1119 | alt_path->sl = cm_req_get_alt_sl(req_msg); | |
1120 | alt_path->mtu_selector = IB_SA_EQ; | |
1121 | alt_path->mtu = cm_req_get_path_mtu(req_msg); | |
1122 | alt_path->rate_selector = IB_SA_EQ; | |
1123 | alt_path->rate = cm_req_get_alt_packet_rate(req_msg); | |
1124 | alt_path->packet_life_time_selector = IB_SA_EQ; | |
1125 | alt_path->packet_life_time = | |
1126 | cm_req_get_alt_local_ack_timeout(req_msg); | |
1127 | alt_path->packet_life_time -= (alt_path->packet_life_time > 0); | |
1128 | } | |
1129 | } | |
1130 | ||
1131 | static void cm_format_req_event(struct cm_work *work, | |
1132 | struct cm_id_private *cm_id_priv, | |
1133 | struct ib_cm_id *listen_id) | |
1134 | { | |
1135 | struct cm_req_msg *req_msg; | |
1136 | struct ib_cm_req_event_param *param; | |
1137 | ||
1138 | req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad; | |
1139 | param = &work->cm_event.param.req_rcvd; | |
1140 | param->listen_id = listen_id; | |
a977049d HR |
1141 | param->port = cm_id_priv->av.port->port_num; |
1142 | param->primary_path = &work->path[0]; | |
1143 | if (req_msg->alt_local_lid) | |
1144 | param->alternate_path = &work->path[1]; | |
1145 | else | |
1146 | param->alternate_path = NULL; | |
1147 | param->remote_ca_guid = req_msg->local_ca_guid; | |
1148 | param->remote_qkey = be32_to_cpu(req_msg->local_qkey); | |
1149 | param->remote_qpn = be32_to_cpu(cm_req_get_local_qpn(req_msg)); | |
1150 | param->qp_type = cm_req_get_qp_type(req_msg); | |
1151 | param->starting_psn = be32_to_cpu(cm_req_get_starting_psn(req_msg)); | |
1152 | param->responder_resources = cm_req_get_init_depth(req_msg); | |
1153 | param->initiator_depth = cm_req_get_resp_res(req_msg); | |
1154 | param->local_cm_response_timeout = | |
1155 | cm_req_get_remote_resp_timeout(req_msg); | |
1156 | param->flow_control = cm_req_get_flow_ctrl(req_msg); | |
1157 | param->remote_cm_response_timeout = | |
1158 | cm_req_get_local_resp_timeout(req_msg); | |
1159 | param->retry_count = cm_req_get_retry_count(req_msg); | |
1160 | param->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg); | |
1161 | param->srq = cm_req_get_srq(req_msg); | |
1162 | work->cm_event.private_data = &req_msg->private_data; | |
1163 | } | |
1164 | ||
1165 | static void cm_process_work(struct cm_id_private *cm_id_priv, | |
1166 | struct cm_work *work) | |
1167 | { | |
a977049d HR |
1168 | int ret; |
1169 | ||
1170 | /* We will typically only have the current event to report. */ | |
1171 | ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event); | |
1172 | cm_free_work(work); | |
1173 | ||
1174 | while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) { | |
24be6e81 | 1175 | spin_lock_irq(&cm_id_priv->lock); |
a977049d | 1176 | work = cm_dequeue_work(cm_id_priv); |
24be6e81 | 1177 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
1178 | BUG_ON(!work); |
1179 | ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, | |
1180 | &work->cm_event); | |
1181 | cm_free_work(work); | |
1182 | } | |
1183 | cm_deref_id(cm_id_priv); | |
1184 | if (ret) | |
04c33543 | 1185 | cm_destroy_id(&cm_id_priv->id, ret); |
a977049d HR |
1186 | } |
1187 | ||
1188 | static void cm_format_mra(struct cm_mra_msg *mra_msg, | |
1189 | struct cm_id_private *cm_id_priv, | |
1190 | enum cm_msg_response msg_mraed, u8 service_timeout, | |
1191 | const void *private_data, u8 private_data_len) | |
1192 | { | |
1193 | cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid); | |
1194 | cm_mra_set_msg_mraed(mra_msg, msg_mraed); | |
1195 | mra_msg->local_comm_id = cm_id_priv->id.local_id; | |
1196 | mra_msg->remote_comm_id = cm_id_priv->id.remote_id; | |
1197 | cm_mra_set_service_timeout(mra_msg, service_timeout); | |
1198 | ||
1199 | if (private_data && private_data_len) | |
1200 | memcpy(mra_msg->private_data, private_data, private_data_len); | |
1201 | } | |
1202 | ||
1203 | static void cm_format_rej(struct cm_rej_msg *rej_msg, | |
1204 | struct cm_id_private *cm_id_priv, | |
1205 | enum ib_cm_rej_reason reason, | |
1206 | void *ari, | |
1207 | u8 ari_length, | |
1208 | const void *private_data, | |
1209 | u8 private_data_len) | |
1210 | { | |
1211 | cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid); | |
1212 | rej_msg->remote_comm_id = cm_id_priv->id.remote_id; | |
1213 | ||
1214 | switch(cm_id_priv->id.state) { | |
1215 | case IB_CM_REQ_RCVD: | |
1216 | rej_msg->local_comm_id = 0; | |
1217 | cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ); | |
1218 | break; | |
1219 | case IB_CM_MRA_REQ_SENT: | |
1220 | rej_msg->local_comm_id = cm_id_priv->id.local_id; | |
1221 | cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ); | |
1222 | break; | |
1223 | case IB_CM_REP_RCVD: | |
1224 | case IB_CM_MRA_REP_SENT: | |
1225 | rej_msg->local_comm_id = cm_id_priv->id.local_id; | |
1226 | cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REP); | |
1227 | break; | |
1228 | default: | |
1229 | rej_msg->local_comm_id = cm_id_priv->id.local_id; | |
1230 | cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_OTHER); | |
1231 | break; | |
1232 | } | |
1233 | ||
97f52eb4 | 1234 | rej_msg->reason = cpu_to_be16(reason); |
a977049d HR |
1235 | if (ari && ari_length) { |
1236 | cm_rej_set_reject_info_len(rej_msg, ari_length); | |
1237 | memcpy(rej_msg->ari, ari, ari_length); | |
1238 | } | |
1239 | ||
1240 | if (private_data && private_data_len) | |
1241 | memcpy(rej_msg->private_data, private_data, private_data_len); | |
1242 | } | |
1243 | ||
1244 | static void cm_dup_req_handler(struct cm_work *work, | |
1245 | struct cm_id_private *cm_id_priv) | |
1246 | { | |
1247 | struct ib_mad_send_buf *msg = NULL; | |
a977049d HR |
1248 | int ret; |
1249 | ||
1250 | /* Quick state check to discard duplicate REQs. */ | |
1251 | if (cm_id_priv->id.state == IB_CM_REQ_RCVD) | |
1252 | return; | |
1253 | ||
1254 | ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg); | |
1255 | if (ret) | |
1256 | return; | |
1257 | ||
24be6e81 | 1258 | spin_lock_irq(&cm_id_priv->lock); |
a977049d HR |
1259 | switch (cm_id_priv->id.state) { |
1260 | case IB_CM_MRA_REQ_SENT: | |
1261 | cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv, | |
1262 | CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout, | |
1263 | cm_id_priv->private_data, | |
1264 | cm_id_priv->private_data_len); | |
1265 | break; | |
1266 | case IB_CM_TIMEWAIT: | |
1267 | cm_format_rej((struct cm_rej_msg *) msg->mad, cm_id_priv, | |
1268 | IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0); | |
1269 | break; | |
1270 | default: | |
1271 | goto unlock; | |
1272 | } | |
24be6e81 | 1273 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d | 1274 | |
34816ad9 | 1275 | ret = ib_post_send_mad(msg, NULL); |
a977049d HR |
1276 | if (ret) |
1277 | goto free; | |
1278 | return; | |
1279 | ||
24be6e81 | 1280 | unlock: spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
1281 | free: cm_free_msg(msg); |
1282 | } | |
1283 | ||
1284 | static struct cm_id_private * cm_match_req(struct cm_work *work, | |
1285 | struct cm_id_private *cm_id_priv) | |
1286 | { | |
1287 | struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv; | |
1288 | struct cm_timewait_info *timewait_info; | |
1289 | struct cm_req_msg *req_msg; | |
a977049d HR |
1290 | |
1291 | req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad; | |
1292 | ||
d998ccce | 1293 | /* Check for possible duplicate REQ. */ |
24be6e81 | 1294 | spin_lock_irq(&cm.lock); |
a977049d | 1295 | timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info); |
a977049d HR |
1296 | if (timewait_info) { |
1297 | cur_cm_id_priv = cm_get_id(timewait_info->work.local_id, | |
1298 | timewait_info->work.remote_id); | |
24be6e81 | 1299 | spin_unlock_irq(&cm.lock); |
a977049d HR |
1300 | if (cur_cm_id_priv) { |
1301 | cm_dup_req_handler(work, cur_cm_id_priv); | |
1302 | cm_deref_id(cur_cm_id_priv); | |
d998ccce SH |
1303 | } |
1304 | return NULL; | |
1305 | } | |
1306 | ||
1307 | /* Check for stale connections. */ | |
1308 | timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info); | |
1309 | if (timewait_info) { | |
1310 | cm_cleanup_timewait(cm_id_priv->timewait_info); | |
24be6e81 | 1311 | spin_unlock_irq(&cm.lock); |
d998ccce SH |
1312 | cm_issue_rej(work->port, work->mad_recv_wc, |
1313 | IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ, | |
1314 | NULL, 0); | |
1315 | return NULL; | |
a977049d HR |
1316 | } |
1317 | ||
1318 | /* Find matching listen request. */ | |
07d357d0 | 1319 | listen_cm_id_priv = cm_find_listen(cm_id_priv->id.device, |
6e61d04f SH |
1320 | req_msg->service_id, |
1321 | req_msg->private_data); | |
a977049d | 1322 | if (!listen_cm_id_priv) { |
8575329d | 1323 | cm_cleanup_timewait(cm_id_priv->timewait_info); |
24be6e81 | 1324 | spin_unlock_irq(&cm.lock); |
a977049d HR |
1325 | cm_issue_rej(work->port, work->mad_recv_wc, |
1326 | IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ, | |
1327 | NULL, 0); | |
8575329d | 1328 | goto out; |
a977049d HR |
1329 | } |
1330 | atomic_inc(&listen_cm_id_priv->refcount); | |
1331 | atomic_inc(&cm_id_priv->refcount); | |
1332 | cm_id_priv->id.state = IB_CM_REQ_RCVD; | |
1333 | atomic_inc(&cm_id_priv->work_count); | |
24be6e81 | 1334 | spin_unlock_irq(&cm.lock); |
8575329d | 1335 | out: |
a977049d | 1336 | return listen_cm_id_priv; |
a977049d HR |
1337 | } |
1338 | ||
1339 | static int cm_req_handler(struct cm_work *work) | |
1340 | { | |
1341 | struct ib_cm_id *cm_id; | |
1342 | struct cm_id_private *cm_id_priv, *listen_cm_id_priv; | |
1343 | struct cm_req_msg *req_msg; | |
1344 | int ret; | |
1345 | ||
1346 | req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad; | |
1347 | ||
07d357d0 | 1348 | cm_id = ib_create_cm_id(work->port->cm_dev->device, NULL, NULL); |
a977049d HR |
1349 | if (IS_ERR(cm_id)) |
1350 | return PTR_ERR(cm_id); | |
1351 | ||
1352 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); | |
1353 | cm_id_priv->id.remote_id = req_msg->local_comm_id; | |
1354 | cm_init_av_for_response(work->port, work->mad_recv_wc->wc, | |
ca222c6b | 1355 | work->mad_recv_wc->recv_buf.grh, |
a977049d HR |
1356 | &cm_id_priv->av); |
1357 | cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv-> | |
1358 | id.local_id); | |
1359 | if (IS_ERR(cm_id_priv->timewait_info)) { | |
1360 | ret = PTR_ERR(cm_id_priv->timewait_info); | |
76842405 | 1361 | goto destroy; |
a977049d HR |
1362 | } |
1363 | cm_id_priv->timewait_info->work.remote_id = req_msg->local_comm_id; | |
1364 | cm_id_priv->timewait_info->remote_ca_guid = req_msg->local_ca_guid; | |
1365 | cm_id_priv->timewait_info->remote_qpn = cm_req_get_local_qpn(req_msg); | |
1366 | ||
1367 | listen_cm_id_priv = cm_match_req(work, cm_id_priv); | |
1368 | if (!listen_cm_id_priv) { | |
1369 | ret = -EINVAL; | |
76842405 SH |
1370 | kfree(cm_id_priv->timewait_info); |
1371 | goto destroy; | |
a977049d HR |
1372 | } |
1373 | ||
1374 | cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler; | |
1375 | cm_id_priv->id.context = listen_cm_id_priv->id.context; | |
1376 | cm_id_priv->id.service_id = req_msg->service_id; | |
97f52eb4 | 1377 | cm_id_priv->id.service_mask = __constant_cpu_to_be64(~0ULL); |
a977049d HR |
1378 | |
1379 | cm_format_paths_from_req(req_msg, &work->path[0], &work->path[1]); | |
1380 | ret = cm_init_av_by_path(&work->path[0], &cm_id_priv->av); | |
76842405 SH |
1381 | if (ret) { |
1382 | ib_get_cached_gid(work->port->cm_dev->device, | |
1383 | work->port->port_num, 0, &work->path[0].sgid); | |
1384 | ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_GID, | |
1385 | &work->path[0].sgid, sizeof work->path[0].sgid, | |
1386 | NULL, 0); | |
1387 | goto rejected; | |
1388 | } | |
a977049d HR |
1389 | if (req_msg->alt_local_lid) { |
1390 | ret = cm_init_av_by_path(&work->path[1], &cm_id_priv->alt_av); | |
76842405 SH |
1391 | if (ret) { |
1392 | ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_ALT_GID, | |
1393 | &work->path[0].sgid, | |
1394 | sizeof work->path[0].sgid, NULL, 0); | |
1395 | goto rejected; | |
1396 | } | |
a977049d HR |
1397 | } |
1398 | cm_id_priv->tid = req_msg->hdr.tid; | |
1399 | cm_id_priv->timeout_ms = cm_convert_to_ms( | |
1400 | cm_req_get_local_resp_timeout(req_msg)); | |
1401 | cm_id_priv->max_cm_retries = cm_req_get_max_cm_retries(req_msg); | |
1402 | cm_id_priv->remote_qpn = cm_req_get_local_qpn(req_msg); | |
1403 | cm_id_priv->initiator_depth = cm_req_get_resp_res(req_msg); | |
1404 | cm_id_priv->responder_resources = cm_req_get_init_depth(req_msg); | |
1405 | cm_id_priv->path_mtu = cm_req_get_path_mtu(req_msg); | |
e1444b5a | 1406 | cm_id_priv->pkey = req_msg->pkey; |
a977049d | 1407 | cm_id_priv->sq_psn = cm_req_get_starting_psn(req_msg); |
a977049d HR |
1408 | cm_id_priv->retry_count = cm_req_get_retry_count(req_msg); |
1409 | cm_id_priv->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg); | |
ae7971a7 | 1410 | cm_id_priv->qp_type = cm_req_get_qp_type(req_msg); |
a977049d HR |
1411 | |
1412 | cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id); | |
1413 | cm_process_work(cm_id_priv, work); | |
1414 | cm_deref_id(listen_cm_id_priv); | |
1415 | return 0; | |
1416 | ||
76842405 SH |
1417 | rejected: |
1418 | atomic_dec(&cm_id_priv->refcount); | |
a977049d | 1419 | cm_deref_id(listen_cm_id_priv); |
76842405 SH |
1420 | destroy: |
1421 | ib_destroy_cm_id(cm_id); | |
a977049d HR |
1422 | return ret; |
1423 | } | |
1424 | ||
1425 | static void cm_format_rep(struct cm_rep_msg *rep_msg, | |
1426 | struct cm_id_private *cm_id_priv, | |
1427 | struct ib_cm_rep_param *param) | |
1428 | { | |
1429 | cm_format_mad_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid); | |
1430 | rep_msg->local_comm_id = cm_id_priv->id.local_id; | |
1431 | rep_msg->remote_comm_id = cm_id_priv->id.remote_id; | |
1432 | cm_rep_set_local_qpn(rep_msg, cpu_to_be32(param->qp_num)); | |
1433 | cm_rep_set_starting_psn(rep_msg, cpu_to_be32(param->starting_psn)); | |
1434 | rep_msg->resp_resources = param->responder_resources; | |
1435 | rep_msg->initiator_depth = param->initiator_depth; | |
1436 | cm_rep_set_target_ack_delay(rep_msg, param->target_ack_delay); | |
1437 | cm_rep_set_failover(rep_msg, param->failover_accepted); | |
1438 | cm_rep_set_flow_ctrl(rep_msg, param->flow_control); | |
1439 | cm_rep_set_rnr_retry_count(rep_msg, param->rnr_retry_count); | |
1440 | cm_rep_set_srq(rep_msg, param->srq); | |
e971b8cd | 1441 | rep_msg->local_ca_guid = cm_id_priv->id.device->node_guid; |
a977049d HR |
1442 | |
1443 | if (param->private_data && param->private_data_len) | |
1444 | memcpy(rep_msg->private_data, param->private_data, | |
1445 | param->private_data_len); | |
1446 | } | |
1447 | ||
1448 | int ib_send_cm_rep(struct ib_cm_id *cm_id, | |
1449 | struct ib_cm_rep_param *param) | |
1450 | { | |
1451 | struct cm_id_private *cm_id_priv; | |
1452 | struct ib_mad_send_buf *msg; | |
1453 | struct cm_rep_msg *rep_msg; | |
a977049d HR |
1454 | unsigned long flags; |
1455 | int ret; | |
1456 | ||
1457 | if (param->private_data && | |
1458 | param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE) | |
1459 | return -EINVAL; | |
1460 | ||
1461 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); | |
1462 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
1463 | if (cm_id->state != IB_CM_REQ_RCVD && | |
1464 | cm_id->state != IB_CM_MRA_REQ_SENT) { | |
1465 | ret = -EINVAL; | |
1466 | goto out; | |
1467 | } | |
1468 | ||
1469 | ret = cm_alloc_msg(cm_id_priv, &msg); | |
1470 | if (ret) | |
1471 | goto out; | |
1472 | ||
1473 | rep_msg = (struct cm_rep_msg *) msg->mad; | |
1474 | cm_format_rep(rep_msg, cm_id_priv, param); | |
34816ad9 | 1475 | msg->timeout_ms = cm_id_priv->timeout_ms; |
a977049d HR |
1476 | msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT; |
1477 | ||
34816ad9 | 1478 | ret = ib_post_send_mad(msg, NULL); |
a977049d HR |
1479 | if (ret) { |
1480 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
1481 | cm_free_msg(msg); | |
1482 | return ret; | |
1483 | } | |
1484 | ||
1485 | cm_id->state = IB_CM_REP_SENT; | |
1486 | cm_id_priv->msg = msg; | |
1487 | cm_id_priv->initiator_depth = param->initiator_depth; | |
1488 | cm_id_priv->responder_resources = param->responder_resources; | |
1489 | cm_id_priv->rq_psn = cm_rep_get_starting_psn(rep_msg); | |
1490 | cm_id_priv->local_qpn = cm_rep_get_local_qpn(rep_msg); | |
1491 | ||
1492 | out: spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
1493 | return ret; | |
1494 | } | |
1495 | EXPORT_SYMBOL(ib_send_cm_rep); | |
1496 | ||
1497 | static void cm_format_rtu(struct cm_rtu_msg *rtu_msg, | |
1498 | struct cm_id_private *cm_id_priv, | |
1499 | const void *private_data, | |
1500 | u8 private_data_len) | |
1501 | { | |
1502 | cm_format_mad_hdr(&rtu_msg->hdr, CM_RTU_ATTR_ID, cm_id_priv->tid); | |
1503 | rtu_msg->local_comm_id = cm_id_priv->id.local_id; | |
1504 | rtu_msg->remote_comm_id = cm_id_priv->id.remote_id; | |
1505 | ||
1506 | if (private_data && private_data_len) | |
1507 | memcpy(rtu_msg->private_data, private_data, private_data_len); | |
1508 | } | |
1509 | ||
1510 | int ib_send_cm_rtu(struct ib_cm_id *cm_id, | |
1511 | const void *private_data, | |
1512 | u8 private_data_len) | |
1513 | { | |
1514 | struct cm_id_private *cm_id_priv; | |
1515 | struct ib_mad_send_buf *msg; | |
a977049d HR |
1516 | unsigned long flags; |
1517 | void *data; | |
1518 | int ret; | |
1519 | ||
1520 | if (private_data && private_data_len > IB_CM_RTU_PRIVATE_DATA_SIZE) | |
1521 | return -EINVAL; | |
1522 | ||
1523 | data = cm_copy_private_data(private_data, private_data_len); | |
1524 | if (IS_ERR(data)) | |
1525 | return PTR_ERR(data); | |
1526 | ||
1527 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); | |
1528 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
1529 | if (cm_id->state != IB_CM_REP_RCVD && | |
1530 | cm_id->state != IB_CM_MRA_REP_SENT) { | |
1531 | ret = -EINVAL; | |
1532 | goto error; | |
1533 | } | |
1534 | ||
1535 | ret = cm_alloc_msg(cm_id_priv, &msg); | |
1536 | if (ret) | |
1537 | goto error; | |
1538 | ||
1539 | cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv, | |
1540 | private_data, private_data_len); | |
1541 | ||
34816ad9 | 1542 | ret = ib_post_send_mad(msg, NULL); |
a977049d HR |
1543 | if (ret) { |
1544 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
1545 | cm_free_msg(msg); | |
1546 | kfree(data); | |
1547 | return ret; | |
1548 | } | |
1549 | ||
1550 | cm_id->state = IB_CM_ESTABLISHED; | |
1551 | cm_set_private_data(cm_id_priv, data, private_data_len); | |
1552 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
1553 | return 0; | |
1554 | ||
1555 | error: spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
1556 | kfree(data); | |
1557 | return ret; | |
1558 | } | |
1559 | EXPORT_SYMBOL(ib_send_cm_rtu); | |
1560 | ||
1561 | static void cm_format_rep_event(struct cm_work *work) | |
1562 | { | |
1563 | struct cm_rep_msg *rep_msg; | |
1564 | struct ib_cm_rep_event_param *param; | |
1565 | ||
1566 | rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad; | |
1567 | param = &work->cm_event.param.rep_rcvd; | |
1568 | param->remote_ca_guid = rep_msg->local_ca_guid; | |
1569 | param->remote_qkey = be32_to_cpu(rep_msg->local_qkey); | |
1570 | param->remote_qpn = be32_to_cpu(cm_rep_get_local_qpn(rep_msg)); | |
1571 | param->starting_psn = be32_to_cpu(cm_rep_get_starting_psn(rep_msg)); | |
1572 | param->responder_resources = rep_msg->initiator_depth; | |
1573 | param->initiator_depth = rep_msg->resp_resources; | |
1574 | param->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg); | |
1575 | param->failover_accepted = cm_rep_get_failover(rep_msg); | |
1576 | param->flow_control = cm_rep_get_flow_ctrl(rep_msg); | |
1577 | param->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg); | |
1578 | param->srq = cm_rep_get_srq(rep_msg); | |
1579 | work->cm_event.private_data = &rep_msg->private_data; | |
1580 | } | |
1581 | ||
1582 | static void cm_dup_rep_handler(struct cm_work *work) | |
1583 | { | |
1584 | struct cm_id_private *cm_id_priv; | |
1585 | struct cm_rep_msg *rep_msg; | |
1586 | struct ib_mad_send_buf *msg = NULL; | |
a977049d HR |
1587 | int ret; |
1588 | ||
1589 | rep_msg = (struct cm_rep_msg *) work->mad_recv_wc->recv_buf.mad; | |
1590 | cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id, | |
1591 | rep_msg->local_comm_id); | |
1592 | if (!cm_id_priv) | |
1593 | return; | |
1594 | ||
1595 | ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg); | |
1596 | if (ret) | |
1597 | goto deref; | |
1598 | ||
24be6e81 | 1599 | spin_lock_irq(&cm_id_priv->lock); |
a977049d HR |
1600 | if (cm_id_priv->id.state == IB_CM_ESTABLISHED) |
1601 | cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv, | |
1602 | cm_id_priv->private_data, | |
1603 | cm_id_priv->private_data_len); | |
1604 | else if (cm_id_priv->id.state == IB_CM_MRA_REP_SENT) | |
1605 | cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv, | |
1606 | CM_MSG_RESPONSE_REP, cm_id_priv->service_timeout, | |
1607 | cm_id_priv->private_data, | |
1608 | cm_id_priv->private_data_len); | |
1609 | else | |
1610 | goto unlock; | |
24be6e81 | 1611 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d | 1612 | |
34816ad9 | 1613 | ret = ib_post_send_mad(msg, NULL); |
a977049d HR |
1614 | if (ret) |
1615 | goto free; | |
1616 | goto deref; | |
1617 | ||
24be6e81 | 1618 | unlock: spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
1619 | free: cm_free_msg(msg); |
1620 | deref: cm_deref_id(cm_id_priv); | |
1621 | } | |
1622 | ||
1623 | static int cm_rep_handler(struct cm_work *work) | |
1624 | { | |
1625 | struct cm_id_private *cm_id_priv; | |
1626 | struct cm_rep_msg *rep_msg; | |
a977049d HR |
1627 | int ret; |
1628 | ||
1629 | rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad; | |
1630 | cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id, 0); | |
1631 | if (!cm_id_priv) { | |
1632 | cm_dup_rep_handler(work); | |
1633 | return -EINVAL; | |
1634 | } | |
1635 | ||
87fd1a11 SH |
1636 | cm_format_rep_event(work); |
1637 | ||
24be6e81 | 1638 | spin_lock_irq(&cm_id_priv->lock); |
87fd1a11 SH |
1639 | switch (cm_id_priv->id.state) { |
1640 | case IB_CM_REQ_SENT: | |
1641 | case IB_CM_MRA_REQ_RCVD: | |
1642 | break; | |
1643 | default: | |
24be6e81 | 1644 | spin_unlock_irq(&cm_id_priv->lock); |
87fd1a11 SH |
1645 | ret = -EINVAL; |
1646 | goto error; | |
1647 | } | |
1648 | ||
a977049d HR |
1649 | cm_id_priv->timewait_info->work.remote_id = rep_msg->local_comm_id; |
1650 | cm_id_priv->timewait_info->remote_ca_guid = rep_msg->local_ca_guid; | |
1651 | cm_id_priv->timewait_info->remote_qpn = cm_rep_get_local_qpn(rep_msg); | |
1652 | ||
87fd1a11 | 1653 | spin_lock(&cm.lock); |
a977049d HR |
1654 | /* Check for duplicate REP. */ |
1655 | if (cm_insert_remote_id(cm_id_priv->timewait_info)) { | |
87fd1a11 | 1656 | spin_unlock(&cm.lock); |
24be6e81 | 1657 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
1658 | ret = -EINVAL; |
1659 | goto error; | |
1660 | } | |
1661 | /* Check for a stale connection. */ | |
1662 | if (cm_insert_remote_qpn(cm_id_priv->timewait_info)) { | |
87fd1a11 SH |
1663 | rb_erase(&cm_id_priv->timewait_info->remote_id_node, |
1664 | &cm.remote_id_table); | |
1665 | cm_id_priv->timewait_info->inserted_remote_id = 0; | |
1666 | spin_unlock(&cm.lock); | |
24be6e81 | 1667 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
1668 | cm_issue_rej(work->port, work->mad_recv_wc, |
1669 | IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP, | |
1670 | NULL, 0); | |
1671 | ret = -EINVAL; | |
1672 | goto error; | |
1673 | } | |
87fd1a11 | 1674 | spin_unlock(&cm.lock); |
a977049d | 1675 | |
a977049d HR |
1676 | cm_id_priv->id.state = IB_CM_REP_RCVD; |
1677 | cm_id_priv->id.remote_id = rep_msg->local_comm_id; | |
1678 | cm_id_priv->remote_qpn = cm_rep_get_local_qpn(rep_msg); | |
1679 | cm_id_priv->initiator_depth = rep_msg->resp_resources; | |
1680 | cm_id_priv->responder_resources = rep_msg->initiator_depth; | |
1681 | cm_id_priv->sq_psn = cm_rep_get_starting_psn(rep_msg); | |
1682 | cm_id_priv->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg); | |
1683 | ||
1684 | /* todo: handle peer_to_peer */ | |
1685 | ||
34816ad9 | 1686 | ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); |
a977049d HR |
1687 | ret = atomic_inc_and_test(&cm_id_priv->work_count); |
1688 | if (!ret) | |
1689 | list_add_tail(&work->list, &cm_id_priv->work_list); | |
24be6e81 | 1690 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
1691 | |
1692 | if (ret) | |
1693 | cm_process_work(cm_id_priv, work); | |
1694 | else | |
1695 | cm_deref_id(cm_id_priv); | |
1696 | return 0; | |
1697 | ||
87fd1a11 | 1698 | error: |
a977049d HR |
1699 | cm_deref_id(cm_id_priv); |
1700 | return ret; | |
1701 | } | |
1702 | ||
1703 | static int cm_establish_handler(struct cm_work *work) | |
1704 | { | |
1705 | struct cm_id_private *cm_id_priv; | |
a977049d HR |
1706 | int ret; |
1707 | ||
e1444b5a | 1708 | /* See comment in cm_establish about lookup. */ |
a977049d HR |
1709 | cm_id_priv = cm_acquire_id(work->local_id, work->remote_id); |
1710 | if (!cm_id_priv) | |
1711 | return -EINVAL; | |
1712 | ||
24be6e81 | 1713 | spin_lock_irq(&cm_id_priv->lock); |
a977049d | 1714 | if (cm_id_priv->id.state != IB_CM_ESTABLISHED) { |
24be6e81 | 1715 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
1716 | goto out; |
1717 | } | |
1718 | ||
34816ad9 | 1719 | ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); |
a977049d HR |
1720 | ret = atomic_inc_and_test(&cm_id_priv->work_count); |
1721 | if (!ret) | |
1722 | list_add_tail(&work->list, &cm_id_priv->work_list); | |
24be6e81 | 1723 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
1724 | |
1725 | if (ret) | |
1726 | cm_process_work(cm_id_priv, work); | |
1727 | else | |
1728 | cm_deref_id(cm_id_priv); | |
1729 | return 0; | |
1730 | out: | |
1731 | cm_deref_id(cm_id_priv); | |
1732 | return -EINVAL; | |
1733 | } | |
1734 | ||
1735 | static int cm_rtu_handler(struct cm_work *work) | |
1736 | { | |
1737 | struct cm_id_private *cm_id_priv; | |
1738 | struct cm_rtu_msg *rtu_msg; | |
a977049d HR |
1739 | int ret; |
1740 | ||
1741 | rtu_msg = (struct cm_rtu_msg *)work->mad_recv_wc->recv_buf.mad; | |
1742 | cm_id_priv = cm_acquire_id(rtu_msg->remote_comm_id, | |
1743 | rtu_msg->local_comm_id); | |
1744 | if (!cm_id_priv) | |
1745 | return -EINVAL; | |
1746 | ||
1747 | work->cm_event.private_data = &rtu_msg->private_data; | |
1748 | ||
24be6e81 | 1749 | spin_lock_irq(&cm_id_priv->lock); |
a977049d HR |
1750 | if (cm_id_priv->id.state != IB_CM_REP_SENT && |
1751 | cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) { | |
24be6e81 | 1752 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
1753 | goto out; |
1754 | } | |
1755 | cm_id_priv->id.state = IB_CM_ESTABLISHED; | |
1756 | ||
34816ad9 | 1757 | ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); |
a977049d HR |
1758 | ret = atomic_inc_and_test(&cm_id_priv->work_count); |
1759 | if (!ret) | |
1760 | list_add_tail(&work->list, &cm_id_priv->work_list); | |
24be6e81 | 1761 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
1762 | |
1763 | if (ret) | |
1764 | cm_process_work(cm_id_priv, work); | |
1765 | else | |
1766 | cm_deref_id(cm_id_priv); | |
1767 | return 0; | |
1768 | out: | |
1769 | cm_deref_id(cm_id_priv); | |
1770 | return -EINVAL; | |
1771 | } | |
1772 | ||
1773 | static void cm_format_dreq(struct cm_dreq_msg *dreq_msg, | |
1774 | struct cm_id_private *cm_id_priv, | |
1775 | const void *private_data, | |
1776 | u8 private_data_len) | |
1777 | { | |
1778 | cm_format_mad_hdr(&dreq_msg->hdr, CM_DREQ_ATTR_ID, | |
1779 | cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_DREQ)); | |
1780 | dreq_msg->local_comm_id = cm_id_priv->id.local_id; | |
1781 | dreq_msg->remote_comm_id = cm_id_priv->id.remote_id; | |
1782 | cm_dreq_set_remote_qpn(dreq_msg, cm_id_priv->remote_qpn); | |
1783 | ||
1784 | if (private_data && private_data_len) | |
1785 | memcpy(dreq_msg->private_data, private_data, private_data_len); | |
1786 | } | |
1787 | ||
1788 | int ib_send_cm_dreq(struct ib_cm_id *cm_id, | |
1789 | const void *private_data, | |
1790 | u8 private_data_len) | |
1791 | { | |
1792 | struct cm_id_private *cm_id_priv; | |
1793 | struct ib_mad_send_buf *msg; | |
a977049d HR |
1794 | unsigned long flags; |
1795 | int ret; | |
1796 | ||
1797 | if (private_data && private_data_len > IB_CM_DREQ_PRIVATE_DATA_SIZE) | |
1798 | return -EINVAL; | |
1799 | ||
1800 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); | |
1801 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
1802 | if (cm_id->state != IB_CM_ESTABLISHED) { | |
1803 | ret = -EINVAL; | |
1804 | goto out; | |
1805 | } | |
1806 | ||
1807 | ret = cm_alloc_msg(cm_id_priv, &msg); | |
1808 | if (ret) { | |
1809 | cm_enter_timewait(cm_id_priv); | |
1810 | goto out; | |
1811 | } | |
1812 | ||
1813 | cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv, | |
1814 | private_data, private_data_len); | |
34816ad9 | 1815 | msg->timeout_ms = cm_id_priv->timeout_ms; |
a977049d HR |
1816 | msg->context[1] = (void *) (unsigned long) IB_CM_DREQ_SENT; |
1817 | ||
34816ad9 | 1818 | ret = ib_post_send_mad(msg, NULL); |
a977049d HR |
1819 | if (ret) { |
1820 | cm_enter_timewait(cm_id_priv); | |
1821 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
1822 | cm_free_msg(msg); | |
1823 | return ret; | |
1824 | } | |
1825 | ||
1826 | cm_id->state = IB_CM_DREQ_SENT; | |
1827 | cm_id_priv->msg = msg; | |
1828 | out: spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
1829 | return ret; | |
1830 | } | |
1831 | EXPORT_SYMBOL(ib_send_cm_dreq); | |
1832 | ||
1833 | static void cm_format_drep(struct cm_drep_msg *drep_msg, | |
1834 | struct cm_id_private *cm_id_priv, | |
1835 | const void *private_data, | |
1836 | u8 private_data_len) | |
1837 | { | |
1838 | cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, cm_id_priv->tid); | |
1839 | drep_msg->local_comm_id = cm_id_priv->id.local_id; | |
1840 | drep_msg->remote_comm_id = cm_id_priv->id.remote_id; | |
1841 | ||
1842 | if (private_data && private_data_len) | |
1843 | memcpy(drep_msg->private_data, private_data, private_data_len); | |
1844 | } | |
1845 | ||
1846 | int ib_send_cm_drep(struct ib_cm_id *cm_id, | |
1847 | const void *private_data, | |
1848 | u8 private_data_len) | |
1849 | { | |
1850 | struct cm_id_private *cm_id_priv; | |
1851 | struct ib_mad_send_buf *msg; | |
a977049d HR |
1852 | unsigned long flags; |
1853 | void *data; | |
1854 | int ret; | |
1855 | ||
1856 | if (private_data && private_data_len > IB_CM_DREP_PRIVATE_DATA_SIZE) | |
1857 | return -EINVAL; | |
1858 | ||
1859 | data = cm_copy_private_data(private_data, private_data_len); | |
1860 | if (IS_ERR(data)) | |
1861 | return PTR_ERR(data); | |
1862 | ||
1863 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); | |
1864 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
1865 | if (cm_id->state != IB_CM_DREQ_RCVD) { | |
1866 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
1867 | kfree(data); | |
1868 | return -EINVAL; | |
1869 | } | |
1870 | ||
1871 | cm_set_private_data(cm_id_priv, data, private_data_len); | |
1872 | cm_enter_timewait(cm_id_priv); | |
1873 | ||
1874 | ret = cm_alloc_msg(cm_id_priv, &msg); | |
1875 | if (ret) | |
1876 | goto out; | |
1877 | ||
1878 | cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv, | |
1879 | private_data, private_data_len); | |
1880 | ||
34816ad9 | 1881 | ret = ib_post_send_mad(msg, NULL); |
a977049d HR |
1882 | if (ret) { |
1883 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
1884 | cm_free_msg(msg); | |
1885 | return ret; | |
1886 | } | |
1887 | ||
1888 | out: spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
1889 | return ret; | |
1890 | } | |
1891 | EXPORT_SYMBOL(ib_send_cm_drep); | |
1892 | ||
82a9c16a SH |
1893 | static int cm_issue_drep(struct cm_port *port, |
1894 | struct ib_mad_recv_wc *mad_recv_wc) | |
1895 | { | |
1896 | struct ib_mad_send_buf *msg = NULL; | |
1897 | struct cm_dreq_msg *dreq_msg; | |
1898 | struct cm_drep_msg *drep_msg; | |
1899 | int ret; | |
1900 | ||
1901 | ret = cm_alloc_response_msg(port, mad_recv_wc, &msg); | |
1902 | if (ret) | |
1903 | return ret; | |
1904 | ||
1905 | dreq_msg = (struct cm_dreq_msg *) mad_recv_wc->recv_buf.mad; | |
1906 | drep_msg = (struct cm_drep_msg *) msg->mad; | |
1907 | ||
1908 | cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, dreq_msg->hdr.tid); | |
1909 | drep_msg->remote_comm_id = dreq_msg->local_comm_id; | |
1910 | drep_msg->local_comm_id = dreq_msg->remote_comm_id; | |
1911 | ||
1912 | ret = ib_post_send_mad(msg, NULL); | |
1913 | if (ret) | |
1914 | cm_free_msg(msg); | |
1915 | ||
1916 | return ret; | |
1917 | } | |
1918 | ||
a977049d HR |
1919 | static int cm_dreq_handler(struct cm_work *work) |
1920 | { | |
1921 | struct cm_id_private *cm_id_priv; | |
1922 | struct cm_dreq_msg *dreq_msg; | |
1923 | struct ib_mad_send_buf *msg = NULL; | |
a977049d HR |
1924 | int ret; |
1925 | ||
1926 | dreq_msg = (struct cm_dreq_msg *)work->mad_recv_wc->recv_buf.mad; | |
1927 | cm_id_priv = cm_acquire_id(dreq_msg->remote_comm_id, | |
1928 | dreq_msg->local_comm_id); | |
82a9c16a SH |
1929 | if (!cm_id_priv) { |
1930 | cm_issue_drep(work->port, work->mad_recv_wc); | |
a977049d | 1931 | return -EINVAL; |
82a9c16a | 1932 | } |
a977049d HR |
1933 | |
1934 | work->cm_event.private_data = &dreq_msg->private_data; | |
1935 | ||
24be6e81 | 1936 | spin_lock_irq(&cm_id_priv->lock); |
a977049d HR |
1937 | if (cm_id_priv->local_qpn != cm_dreq_get_remote_qpn(dreq_msg)) |
1938 | goto unlock; | |
1939 | ||
1940 | switch (cm_id_priv->id.state) { | |
1941 | case IB_CM_REP_SENT: | |
1942 | case IB_CM_DREQ_SENT: | |
34816ad9 | 1943 | ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); |
a977049d HR |
1944 | break; |
1945 | case IB_CM_ESTABLISHED: | |
1946 | case IB_CM_MRA_REP_RCVD: | |
1947 | break; | |
1948 | case IB_CM_TIMEWAIT: | |
1949 | if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg)) | |
1950 | goto unlock; | |
1951 | ||
1952 | cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv, | |
1953 | cm_id_priv->private_data, | |
1954 | cm_id_priv->private_data_len); | |
24be6e81 | 1955 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d | 1956 | |
34816ad9 | 1957 | if (ib_post_send_mad(msg, NULL)) |
a977049d HR |
1958 | cm_free_msg(msg); |
1959 | goto deref; | |
1960 | default: | |
1961 | goto unlock; | |
1962 | } | |
1963 | cm_id_priv->id.state = IB_CM_DREQ_RCVD; | |
1964 | cm_id_priv->tid = dreq_msg->hdr.tid; | |
1965 | ret = atomic_inc_and_test(&cm_id_priv->work_count); | |
1966 | if (!ret) | |
1967 | list_add_tail(&work->list, &cm_id_priv->work_list); | |
24be6e81 | 1968 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
1969 | |
1970 | if (ret) | |
1971 | cm_process_work(cm_id_priv, work); | |
1972 | else | |
1973 | cm_deref_id(cm_id_priv); | |
1974 | return 0; | |
1975 | ||
24be6e81 | 1976 | unlock: spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
1977 | deref: cm_deref_id(cm_id_priv); |
1978 | return -EINVAL; | |
1979 | } | |
1980 | ||
1981 | static int cm_drep_handler(struct cm_work *work) | |
1982 | { | |
1983 | struct cm_id_private *cm_id_priv; | |
1984 | struct cm_drep_msg *drep_msg; | |
a977049d HR |
1985 | int ret; |
1986 | ||
1987 | drep_msg = (struct cm_drep_msg *)work->mad_recv_wc->recv_buf.mad; | |
1988 | cm_id_priv = cm_acquire_id(drep_msg->remote_comm_id, | |
1989 | drep_msg->local_comm_id); | |
1990 | if (!cm_id_priv) | |
1991 | return -EINVAL; | |
1992 | ||
1993 | work->cm_event.private_data = &drep_msg->private_data; | |
1994 | ||
24be6e81 | 1995 | spin_lock_irq(&cm_id_priv->lock); |
a977049d HR |
1996 | if (cm_id_priv->id.state != IB_CM_DREQ_SENT && |
1997 | cm_id_priv->id.state != IB_CM_DREQ_RCVD) { | |
24be6e81 | 1998 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
1999 | goto out; |
2000 | } | |
2001 | cm_enter_timewait(cm_id_priv); | |
2002 | ||
34816ad9 | 2003 | ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); |
a977049d HR |
2004 | ret = atomic_inc_and_test(&cm_id_priv->work_count); |
2005 | if (!ret) | |
2006 | list_add_tail(&work->list, &cm_id_priv->work_list); | |
24be6e81 | 2007 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
2008 | |
2009 | if (ret) | |
2010 | cm_process_work(cm_id_priv, work); | |
2011 | else | |
2012 | cm_deref_id(cm_id_priv); | |
2013 | return 0; | |
2014 | out: | |
2015 | cm_deref_id(cm_id_priv); | |
2016 | return -EINVAL; | |
2017 | } | |
2018 | ||
2019 | int ib_send_cm_rej(struct ib_cm_id *cm_id, | |
2020 | enum ib_cm_rej_reason reason, | |
2021 | void *ari, | |
2022 | u8 ari_length, | |
2023 | const void *private_data, | |
2024 | u8 private_data_len) | |
2025 | { | |
2026 | struct cm_id_private *cm_id_priv; | |
2027 | struct ib_mad_send_buf *msg; | |
a977049d HR |
2028 | unsigned long flags; |
2029 | int ret; | |
2030 | ||
2031 | if ((private_data && private_data_len > IB_CM_REJ_PRIVATE_DATA_SIZE) || | |
2032 | (ari && ari_length > IB_CM_REJ_ARI_LENGTH)) | |
2033 | return -EINVAL; | |
2034 | ||
2035 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); | |
2036 | ||
2037 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
2038 | switch (cm_id->state) { | |
2039 | case IB_CM_REQ_SENT: | |
2040 | case IB_CM_MRA_REQ_RCVD: | |
2041 | case IB_CM_REQ_RCVD: | |
2042 | case IB_CM_MRA_REQ_SENT: | |
2043 | case IB_CM_REP_RCVD: | |
2044 | case IB_CM_MRA_REP_SENT: | |
2045 | ret = cm_alloc_msg(cm_id_priv, &msg); | |
2046 | if (!ret) | |
2047 | cm_format_rej((struct cm_rej_msg *) msg->mad, | |
2048 | cm_id_priv, reason, ari, ari_length, | |
2049 | private_data, private_data_len); | |
2050 | ||
2051 | cm_reset_to_idle(cm_id_priv); | |
2052 | break; | |
2053 | case IB_CM_REP_SENT: | |
2054 | case IB_CM_MRA_REP_RCVD: | |
2055 | ret = cm_alloc_msg(cm_id_priv, &msg); | |
2056 | if (!ret) | |
2057 | cm_format_rej((struct cm_rej_msg *) msg->mad, | |
2058 | cm_id_priv, reason, ari, ari_length, | |
2059 | private_data, private_data_len); | |
2060 | ||
2061 | cm_enter_timewait(cm_id_priv); | |
2062 | break; | |
2063 | default: | |
2064 | ret = -EINVAL; | |
2065 | goto out; | |
2066 | } | |
2067 | ||
2068 | if (ret) | |
2069 | goto out; | |
2070 | ||
34816ad9 | 2071 | ret = ib_post_send_mad(msg, NULL); |
a977049d HR |
2072 | if (ret) |
2073 | cm_free_msg(msg); | |
2074 | ||
2075 | out: spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
2076 | return ret; | |
2077 | } | |
2078 | EXPORT_SYMBOL(ib_send_cm_rej); | |
2079 | ||
2080 | static void cm_format_rej_event(struct cm_work *work) | |
2081 | { | |
2082 | struct cm_rej_msg *rej_msg; | |
2083 | struct ib_cm_rej_event_param *param; | |
2084 | ||
2085 | rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad; | |
2086 | param = &work->cm_event.param.rej_rcvd; | |
2087 | param->ari = rej_msg->ari; | |
2088 | param->ari_length = cm_rej_get_reject_info_len(rej_msg); | |
97f52eb4 | 2089 | param->reason = __be16_to_cpu(rej_msg->reason); |
a977049d HR |
2090 | work->cm_event.private_data = &rej_msg->private_data; |
2091 | } | |
2092 | ||
2093 | static struct cm_id_private * cm_acquire_rejected_id(struct cm_rej_msg *rej_msg) | |
2094 | { | |
2095 | struct cm_timewait_info *timewait_info; | |
2096 | struct cm_id_private *cm_id_priv; | |
97f52eb4 | 2097 | __be32 remote_id; |
a977049d HR |
2098 | |
2099 | remote_id = rej_msg->local_comm_id; | |
2100 | ||
97f52eb4 | 2101 | if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_TIMEOUT) { |
24be6e81 | 2102 | spin_lock_irq(&cm.lock); |
97f52eb4 | 2103 | timewait_info = cm_find_remote_id( *((__be64 *) rej_msg->ari), |
a977049d HR |
2104 | remote_id); |
2105 | if (!timewait_info) { | |
24be6e81 | 2106 | spin_unlock_irq(&cm.lock); |
a977049d HR |
2107 | return NULL; |
2108 | } | |
f06d2653 SH |
2109 | cm_id_priv = idr_find(&cm.local_id_table, (__force int) |
2110 | (timewait_info->work.local_id ^ | |
2111 | cm.random_id_operand)); | |
a977049d HR |
2112 | if (cm_id_priv) { |
2113 | if (cm_id_priv->id.remote_id == remote_id) | |
2114 | atomic_inc(&cm_id_priv->refcount); | |
2115 | else | |
2116 | cm_id_priv = NULL; | |
2117 | } | |
24be6e81 | 2118 | spin_unlock_irq(&cm.lock); |
a977049d HR |
2119 | } else if (cm_rej_get_msg_rejected(rej_msg) == CM_MSG_RESPONSE_REQ) |
2120 | cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, 0); | |
2121 | else | |
2122 | cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, remote_id); | |
2123 | ||
2124 | return cm_id_priv; | |
2125 | } | |
2126 | ||
2127 | static int cm_rej_handler(struct cm_work *work) | |
2128 | { | |
2129 | struct cm_id_private *cm_id_priv; | |
2130 | struct cm_rej_msg *rej_msg; | |
a977049d HR |
2131 | int ret; |
2132 | ||
2133 | rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad; | |
2134 | cm_id_priv = cm_acquire_rejected_id(rej_msg); | |
2135 | if (!cm_id_priv) | |
2136 | return -EINVAL; | |
2137 | ||
2138 | cm_format_rej_event(work); | |
2139 | ||
24be6e81 | 2140 | spin_lock_irq(&cm_id_priv->lock); |
a977049d HR |
2141 | switch (cm_id_priv->id.state) { |
2142 | case IB_CM_REQ_SENT: | |
2143 | case IB_CM_MRA_REQ_RCVD: | |
2144 | case IB_CM_REP_SENT: | |
2145 | case IB_CM_MRA_REP_RCVD: | |
34816ad9 | 2146 | ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); |
a977049d HR |
2147 | /* fall through */ |
2148 | case IB_CM_REQ_RCVD: | |
2149 | case IB_CM_MRA_REQ_SENT: | |
97f52eb4 | 2150 | if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_STALE_CONN) |
a977049d HR |
2151 | cm_enter_timewait(cm_id_priv); |
2152 | else | |
2153 | cm_reset_to_idle(cm_id_priv); | |
2154 | break; | |
2155 | case IB_CM_DREQ_SENT: | |
34816ad9 | 2156 | ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); |
a977049d HR |
2157 | /* fall through */ |
2158 | case IB_CM_REP_RCVD: | |
2159 | case IB_CM_MRA_REP_SENT: | |
2160 | case IB_CM_ESTABLISHED: | |
2161 | cm_enter_timewait(cm_id_priv); | |
2162 | break; | |
2163 | default: | |
24be6e81 | 2164 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
2165 | ret = -EINVAL; |
2166 | goto out; | |
2167 | } | |
2168 | ||
2169 | ret = atomic_inc_and_test(&cm_id_priv->work_count); | |
2170 | if (!ret) | |
2171 | list_add_tail(&work->list, &cm_id_priv->work_list); | |
24be6e81 | 2172 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
2173 | |
2174 | if (ret) | |
2175 | cm_process_work(cm_id_priv, work); | |
2176 | else | |
2177 | cm_deref_id(cm_id_priv); | |
2178 | return 0; | |
2179 | out: | |
2180 | cm_deref_id(cm_id_priv); | |
2181 | return -EINVAL; | |
2182 | } | |
2183 | ||
2184 | int ib_send_cm_mra(struct ib_cm_id *cm_id, | |
2185 | u8 service_timeout, | |
2186 | const void *private_data, | |
2187 | u8 private_data_len) | |
2188 | { | |
2189 | struct cm_id_private *cm_id_priv; | |
2190 | struct ib_mad_send_buf *msg; | |
a977049d HR |
2191 | void *data; |
2192 | unsigned long flags; | |
2193 | int ret; | |
2194 | ||
2195 | if (private_data && private_data_len > IB_CM_MRA_PRIVATE_DATA_SIZE) | |
2196 | return -EINVAL; | |
2197 | ||
2198 | data = cm_copy_private_data(private_data, private_data_len); | |
2199 | if (IS_ERR(data)) | |
2200 | return PTR_ERR(data); | |
2201 | ||
2202 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); | |
2203 | ||
2204 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
2205 | switch(cm_id_priv->id.state) { | |
2206 | case IB_CM_REQ_RCVD: | |
2207 | ret = cm_alloc_msg(cm_id_priv, &msg); | |
2208 | if (ret) | |
2209 | goto error1; | |
2210 | ||
2211 | cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv, | |
2212 | CM_MSG_RESPONSE_REQ, service_timeout, | |
2213 | private_data, private_data_len); | |
34816ad9 | 2214 | ret = ib_post_send_mad(msg, NULL); |
a977049d HR |
2215 | if (ret) |
2216 | goto error2; | |
2217 | cm_id->state = IB_CM_MRA_REQ_SENT; | |
2218 | break; | |
2219 | case IB_CM_REP_RCVD: | |
2220 | ret = cm_alloc_msg(cm_id_priv, &msg); | |
2221 | if (ret) | |
2222 | goto error1; | |
2223 | ||
2224 | cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv, | |
2225 | CM_MSG_RESPONSE_REP, service_timeout, | |
2226 | private_data, private_data_len); | |
34816ad9 | 2227 | ret = ib_post_send_mad(msg, NULL); |
a977049d HR |
2228 | if (ret) |
2229 | goto error2; | |
2230 | cm_id->state = IB_CM_MRA_REP_SENT; | |
2231 | break; | |
2232 | case IB_CM_ESTABLISHED: | |
2233 | ret = cm_alloc_msg(cm_id_priv, &msg); | |
2234 | if (ret) | |
2235 | goto error1; | |
2236 | ||
2237 | cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv, | |
2238 | CM_MSG_RESPONSE_OTHER, service_timeout, | |
2239 | private_data, private_data_len); | |
34816ad9 | 2240 | ret = ib_post_send_mad(msg, NULL); |
a977049d HR |
2241 | if (ret) |
2242 | goto error2; | |
2243 | cm_id->lap_state = IB_CM_MRA_LAP_SENT; | |
2244 | break; | |
2245 | default: | |
2246 | ret = -EINVAL; | |
2247 | goto error1; | |
2248 | } | |
2249 | cm_id_priv->service_timeout = service_timeout; | |
2250 | cm_set_private_data(cm_id_priv, data, private_data_len); | |
2251 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
2252 | return 0; | |
2253 | ||
2254 | error1: spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
2255 | kfree(data); | |
2256 | return ret; | |
2257 | ||
2258 | error2: spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
2259 | kfree(data); | |
2260 | cm_free_msg(msg); | |
2261 | return ret; | |
2262 | } | |
2263 | EXPORT_SYMBOL(ib_send_cm_mra); | |
2264 | ||
2265 | static struct cm_id_private * cm_acquire_mraed_id(struct cm_mra_msg *mra_msg) | |
2266 | { | |
2267 | switch (cm_mra_get_msg_mraed(mra_msg)) { | |
2268 | case CM_MSG_RESPONSE_REQ: | |
2269 | return cm_acquire_id(mra_msg->remote_comm_id, 0); | |
2270 | case CM_MSG_RESPONSE_REP: | |
2271 | case CM_MSG_RESPONSE_OTHER: | |
2272 | return cm_acquire_id(mra_msg->remote_comm_id, | |
2273 | mra_msg->local_comm_id); | |
2274 | default: | |
2275 | return NULL; | |
2276 | } | |
2277 | } | |
2278 | ||
2279 | static int cm_mra_handler(struct cm_work *work) | |
2280 | { | |
2281 | struct cm_id_private *cm_id_priv; | |
2282 | struct cm_mra_msg *mra_msg; | |
a977049d HR |
2283 | int timeout, ret; |
2284 | ||
2285 | mra_msg = (struct cm_mra_msg *)work->mad_recv_wc->recv_buf.mad; | |
2286 | cm_id_priv = cm_acquire_mraed_id(mra_msg); | |
2287 | if (!cm_id_priv) | |
2288 | return -EINVAL; | |
2289 | ||
2290 | work->cm_event.private_data = &mra_msg->private_data; | |
2291 | work->cm_event.param.mra_rcvd.service_timeout = | |
2292 | cm_mra_get_service_timeout(mra_msg); | |
2293 | timeout = cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg)) + | |
2294 | cm_convert_to_ms(cm_id_priv->av.packet_life_time); | |
2295 | ||
24be6e81 | 2296 | spin_lock_irq(&cm_id_priv->lock); |
a977049d HR |
2297 | switch (cm_id_priv->id.state) { |
2298 | case IB_CM_REQ_SENT: | |
2299 | if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REQ || | |
2300 | ib_modify_mad(cm_id_priv->av.port->mad_agent, | |
34816ad9 | 2301 | cm_id_priv->msg, timeout)) |
a977049d HR |
2302 | goto out; |
2303 | cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD; | |
2304 | break; | |
2305 | case IB_CM_REP_SENT: | |
2306 | if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REP || | |
2307 | ib_modify_mad(cm_id_priv->av.port->mad_agent, | |
34816ad9 | 2308 | cm_id_priv->msg, timeout)) |
a977049d HR |
2309 | goto out; |
2310 | cm_id_priv->id.state = IB_CM_MRA_REP_RCVD; | |
2311 | break; | |
2312 | case IB_CM_ESTABLISHED: | |
2313 | if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_OTHER || | |
2314 | cm_id_priv->id.lap_state != IB_CM_LAP_SENT || | |
2315 | ib_modify_mad(cm_id_priv->av.port->mad_agent, | |
34816ad9 | 2316 | cm_id_priv->msg, timeout)) |
a977049d HR |
2317 | goto out; |
2318 | cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD; | |
2319 | break; | |
2320 | default: | |
2321 | goto out; | |
2322 | } | |
2323 | ||
2324 | cm_id_priv->msg->context[1] = (void *) (unsigned long) | |
2325 | cm_id_priv->id.state; | |
2326 | ret = atomic_inc_and_test(&cm_id_priv->work_count); | |
2327 | if (!ret) | |
2328 | list_add_tail(&work->list, &cm_id_priv->work_list); | |
24be6e81 | 2329 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
2330 | |
2331 | if (ret) | |
2332 | cm_process_work(cm_id_priv, work); | |
2333 | else | |
2334 | cm_deref_id(cm_id_priv); | |
2335 | return 0; | |
2336 | out: | |
24be6e81 | 2337 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
2338 | cm_deref_id(cm_id_priv); |
2339 | return -EINVAL; | |
2340 | } | |
2341 | ||
2342 | static void cm_format_lap(struct cm_lap_msg *lap_msg, | |
2343 | struct cm_id_private *cm_id_priv, | |
2344 | struct ib_sa_path_rec *alternate_path, | |
2345 | const void *private_data, | |
2346 | u8 private_data_len) | |
2347 | { | |
2348 | cm_format_mad_hdr(&lap_msg->hdr, CM_LAP_ATTR_ID, | |
2349 | cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_LAP)); | |
2350 | lap_msg->local_comm_id = cm_id_priv->id.local_id; | |
2351 | lap_msg->remote_comm_id = cm_id_priv->id.remote_id; | |
2352 | cm_lap_set_remote_qpn(lap_msg, cm_id_priv->remote_qpn); | |
2353 | /* todo: need remote CM response timeout */ | |
2354 | cm_lap_set_remote_resp_timeout(lap_msg, 0x1F); | |
2355 | lap_msg->alt_local_lid = alternate_path->slid; | |
2356 | lap_msg->alt_remote_lid = alternate_path->dlid; | |
2357 | lap_msg->alt_local_gid = alternate_path->sgid; | |
2358 | lap_msg->alt_remote_gid = alternate_path->dgid; | |
2359 | cm_lap_set_flow_label(lap_msg, alternate_path->flow_label); | |
2360 | cm_lap_set_traffic_class(lap_msg, alternate_path->traffic_class); | |
2361 | lap_msg->alt_hop_limit = alternate_path->hop_limit; | |
2362 | cm_lap_set_packet_rate(lap_msg, alternate_path->rate); | |
2363 | cm_lap_set_sl(lap_msg, alternate_path->sl); | |
2364 | cm_lap_set_subnet_local(lap_msg, 1); /* local only... */ | |
2365 | cm_lap_set_local_ack_timeout(lap_msg, | |
2366 | min(31, alternate_path->packet_life_time + 1)); | |
2367 | ||
2368 | if (private_data && private_data_len) | |
2369 | memcpy(lap_msg->private_data, private_data, private_data_len); | |
2370 | } | |
2371 | ||
2372 | int ib_send_cm_lap(struct ib_cm_id *cm_id, | |
2373 | struct ib_sa_path_rec *alternate_path, | |
2374 | const void *private_data, | |
2375 | u8 private_data_len) | |
2376 | { | |
2377 | struct cm_id_private *cm_id_priv; | |
2378 | struct ib_mad_send_buf *msg; | |
a977049d HR |
2379 | unsigned long flags; |
2380 | int ret; | |
2381 | ||
2382 | if (private_data && private_data_len > IB_CM_LAP_PRIVATE_DATA_SIZE) | |
2383 | return -EINVAL; | |
2384 | ||
2385 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); | |
2386 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
2387 | if (cm_id->state != IB_CM_ESTABLISHED || | |
e1444b5a SH |
2388 | (cm_id->lap_state != IB_CM_LAP_UNINIT && |
2389 | cm_id->lap_state != IB_CM_LAP_IDLE)) { | |
a977049d HR |
2390 | ret = -EINVAL; |
2391 | goto out; | |
2392 | } | |
2393 | ||
e1444b5a SH |
2394 | ret = cm_init_av_by_path(alternate_path, &cm_id_priv->alt_av); |
2395 | if (ret) | |
2396 | goto out; | |
2397 | ||
a977049d HR |
2398 | ret = cm_alloc_msg(cm_id_priv, &msg); |
2399 | if (ret) | |
2400 | goto out; | |
2401 | ||
2402 | cm_format_lap((struct cm_lap_msg *) msg->mad, cm_id_priv, | |
2403 | alternate_path, private_data, private_data_len); | |
34816ad9 | 2404 | msg->timeout_ms = cm_id_priv->timeout_ms; |
a977049d HR |
2405 | msg->context[1] = (void *) (unsigned long) IB_CM_ESTABLISHED; |
2406 | ||
34816ad9 | 2407 | ret = ib_post_send_mad(msg, NULL); |
a977049d HR |
2408 | if (ret) { |
2409 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
2410 | cm_free_msg(msg); | |
2411 | return ret; | |
2412 | } | |
2413 | ||
2414 | cm_id->lap_state = IB_CM_LAP_SENT; | |
2415 | cm_id_priv->msg = msg; | |
2416 | ||
2417 | out: spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
2418 | return ret; | |
2419 | } | |
2420 | EXPORT_SYMBOL(ib_send_cm_lap); | |
2421 | ||
e1444b5a SH |
2422 | static void cm_format_path_from_lap(struct cm_id_private *cm_id_priv, |
2423 | struct ib_sa_path_rec *path, | |
a977049d HR |
2424 | struct cm_lap_msg *lap_msg) |
2425 | { | |
2426 | memset(path, 0, sizeof *path); | |
2427 | path->dgid = lap_msg->alt_local_gid; | |
2428 | path->sgid = lap_msg->alt_remote_gid; | |
2429 | path->dlid = lap_msg->alt_local_lid; | |
2430 | path->slid = lap_msg->alt_remote_lid; | |
2431 | path->flow_label = cm_lap_get_flow_label(lap_msg); | |
2432 | path->hop_limit = lap_msg->alt_hop_limit; | |
2433 | path->traffic_class = cm_lap_get_traffic_class(lap_msg); | |
2434 | path->reversible = 1; | |
e1444b5a | 2435 | path->pkey = cm_id_priv->pkey; |
a977049d HR |
2436 | path->sl = cm_lap_get_sl(lap_msg); |
2437 | path->mtu_selector = IB_SA_EQ; | |
e1444b5a | 2438 | path->mtu = cm_id_priv->path_mtu; |
a977049d HR |
2439 | path->rate_selector = IB_SA_EQ; |
2440 | path->rate = cm_lap_get_packet_rate(lap_msg); | |
2441 | path->packet_life_time_selector = IB_SA_EQ; | |
2442 | path->packet_life_time = cm_lap_get_local_ack_timeout(lap_msg); | |
2443 | path->packet_life_time -= (path->packet_life_time > 0); | |
2444 | } | |
2445 | ||
2446 | static int cm_lap_handler(struct cm_work *work) | |
2447 | { | |
2448 | struct cm_id_private *cm_id_priv; | |
2449 | struct cm_lap_msg *lap_msg; | |
2450 | struct ib_cm_lap_event_param *param; | |
2451 | struct ib_mad_send_buf *msg = NULL; | |
a977049d HR |
2452 | int ret; |
2453 | ||
2454 | /* todo: verify LAP request and send reject APR if invalid. */ | |
2455 | lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad; | |
2456 | cm_id_priv = cm_acquire_id(lap_msg->remote_comm_id, | |
2457 | lap_msg->local_comm_id); | |
2458 | if (!cm_id_priv) | |
2459 | return -EINVAL; | |
2460 | ||
2461 | param = &work->cm_event.param.lap_rcvd; | |
2462 | param->alternate_path = &work->path[0]; | |
e1444b5a | 2463 | cm_format_path_from_lap(cm_id_priv, param->alternate_path, lap_msg); |
a977049d HR |
2464 | work->cm_event.private_data = &lap_msg->private_data; |
2465 | ||
24be6e81 | 2466 | spin_lock_irq(&cm_id_priv->lock); |
a977049d HR |
2467 | if (cm_id_priv->id.state != IB_CM_ESTABLISHED) |
2468 | goto unlock; | |
2469 | ||
2470 | switch (cm_id_priv->id.lap_state) { | |
e1444b5a | 2471 | case IB_CM_LAP_UNINIT: |
a977049d HR |
2472 | case IB_CM_LAP_IDLE: |
2473 | break; | |
2474 | case IB_CM_MRA_LAP_SENT: | |
2475 | if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg)) | |
2476 | goto unlock; | |
2477 | ||
2478 | cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv, | |
2479 | CM_MSG_RESPONSE_OTHER, | |
2480 | cm_id_priv->service_timeout, | |
2481 | cm_id_priv->private_data, | |
2482 | cm_id_priv->private_data_len); | |
24be6e81 | 2483 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d | 2484 | |
34816ad9 | 2485 | if (ib_post_send_mad(msg, NULL)) |
a977049d HR |
2486 | cm_free_msg(msg); |
2487 | goto deref; | |
2488 | default: | |
2489 | goto unlock; | |
2490 | } | |
2491 | ||
2492 | cm_id_priv->id.lap_state = IB_CM_LAP_RCVD; | |
2493 | cm_id_priv->tid = lap_msg->hdr.tid; | |
e1444b5a SH |
2494 | cm_init_av_for_response(work->port, work->mad_recv_wc->wc, |
2495 | work->mad_recv_wc->recv_buf.grh, | |
2496 | &cm_id_priv->av); | |
2497 | cm_init_av_by_path(param->alternate_path, &cm_id_priv->alt_av); | |
a977049d HR |
2498 | ret = atomic_inc_and_test(&cm_id_priv->work_count); |
2499 | if (!ret) | |
2500 | list_add_tail(&work->list, &cm_id_priv->work_list); | |
24be6e81 | 2501 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
2502 | |
2503 | if (ret) | |
2504 | cm_process_work(cm_id_priv, work); | |
2505 | else | |
2506 | cm_deref_id(cm_id_priv); | |
2507 | return 0; | |
2508 | ||
24be6e81 | 2509 | unlock: spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
2510 | deref: cm_deref_id(cm_id_priv); |
2511 | return -EINVAL; | |
2512 | } | |
2513 | ||
2514 | static void cm_format_apr(struct cm_apr_msg *apr_msg, | |
2515 | struct cm_id_private *cm_id_priv, | |
2516 | enum ib_cm_apr_status status, | |
2517 | void *info, | |
2518 | u8 info_length, | |
2519 | const void *private_data, | |
2520 | u8 private_data_len) | |
2521 | { | |
2522 | cm_format_mad_hdr(&apr_msg->hdr, CM_APR_ATTR_ID, cm_id_priv->tid); | |
2523 | apr_msg->local_comm_id = cm_id_priv->id.local_id; | |
2524 | apr_msg->remote_comm_id = cm_id_priv->id.remote_id; | |
2525 | apr_msg->ap_status = (u8) status; | |
2526 | ||
2527 | if (info && info_length) { | |
2528 | apr_msg->info_length = info_length; | |
2529 | memcpy(apr_msg->info, info, info_length); | |
2530 | } | |
2531 | ||
2532 | if (private_data && private_data_len) | |
2533 | memcpy(apr_msg->private_data, private_data, private_data_len); | |
2534 | } | |
2535 | ||
2536 | int ib_send_cm_apr(struct ib_cm_id *cm_id, | |
2537 | enum ib_cm_apr_status status, | |
2538 | void *info, | |
2539 | u8 info_length, | |
2540 | const void *private_data, | |
2541 | u8 private_data_len) | |
2542 | { | |
2543 | struct cm_id_private *cm_id_priv; | |
2544 | struct ib_mad_send_buf *msg; | |
a977049d HR |
2545 | unsigned long flags; |
2546 | int ret; | |
2547 | ||
2548 | if ((private_data && private_data_len > IB_CM_APR_PRIVATE_DATA_SIZE) || | |
2549 | (info && info_length > IB_CM_APR_INFO_LENGTH)) | |
2550 | return -EINVAL; | |
2551 | ||
2552 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); | |
2553 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
2554 | if (cm_id->state != IB_CM_ESTABLISHED || | |
2555 | (cm_id->lap_state != IB_CM_LAP_RCVD && | |
2556 | cm_id->lap_state != IB_CM_MRA_LAP_SENT)) { | |
2557 | ret = -EINVAL; | |
2558 | goto out; | |
2559 | } | |
2560 | ||
2561 | ret = cm_alloc_msg(cm_id_priv, &msg); | |
2562 | if (ret) | |
2563 | goto out; | |
2564 | ||
2565 | cm_format_apr((struct cm_apr_msg *) msg->mad, cm_id_priv, status, | |
2566 | info, info_length, private_data, private_data_len); | |
34816ad9 | 2567 | ret = ib_post_send_mad(msg, NULL); |
a977049d HR |
2568 | if (ret) { |
2569 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
2570 | cm_free_msg(msg); | |
2571 | return ret; | |
2572 | } | |
2573 | ||
2574 | cm_id->lap_state = IB_CM_LAP_IDLE; | |
2575 | out: spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
2576 | return ret; | |
2577 | } | |
2578 | EXPORT_SYMBOL(ib_send_cm_apr); | |
2579 | ||
2580 | static int cm_apr_handler(struct cm_work *work) | |
2581 | { | |
2582 | struct cm_id_private *cm_id_priv; | |
2583 | struct cm_apr_msg *apr_msg; | |
a977049d HR |
2584 | int ret; |
2585 | ||
2586 | apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad; | |
2587 | cm_id_priv = cm_acquire_id(apr_msg->remote_comm_id, | |
2588 | apr_msg->local_comm_id); | |
2589 | if (!cm_id_priv) | |
2590 | return -EINVAL; /* Unmatched reply. */ | |
2591 | ||
2592 | work->cm_event.param.apr_rcvd.ap_status = apr_msg->ap_status; | |
2593 | work->cm_event.param.apr_rcvd.apr_info = &apr_msg->info; | |
2594 | work->cm_event.param.apr_rcvd.info_len = apr_msg->info_length; | |
2595 | work->cm_event.private_data = &apr_msg->private_data; | |
2596 | ||
24be6e81 | 2597 | spin_lock_irq(&cm_id_priv->lock); |
a977049d HR |
2598 | if (cm_id_priv->id.state != IB_CM_ESTABLISHED || |
2599 | (cm_id_priv->id.lap_state != IB_CM_LAP_SENT && | |
2600 | cm_id_priv->id.lap_state != IB_CM_MRA_LAP_RCVD)) { | |
24be6e81 | 2601 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
2602 | goto out; |
2603 | } | |
2604 | cm_id_priv->id.lap_state = IB_CM_LAP_IDLE; | |
34816ad9 | 2605 | ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); |
a977049d HR |
2606 | cm_id_priv->msg = NULL; |
2607 | ||
2608 | ret = atomic_inc_and_test(&cm_id_priv->work_count); | |
2609 | if (!ret) | |
2610 | list_add_tail(&work->list, &cm_id_priv->work_list); | |
24be6e81 | 2611 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
2612 | |
2613 | if (ret) | |
2614 | cm_process_work(cm_id_priv, work); | |
2615 | else | |
2616 | cm_deref_id(cm_id_priv); | |
2617 | return 0; | |
2618 | out: | |
2619 | cm_deref_id(cm_id_priv); | |
2620 | return -EINVAL; | |
2621 | } | |
2622 | ||
2623 | static int cm_timewait_handler(struct cm_work *work) | |
2624 | { | |
2625 | struct cm_timewait_info *timewait_info; | |
2626 | struct cm_id_private *cm_id_priv; | |
a977049d HR |
2627 | int ret; |
2628 | ||
2629 | timewait_info = (struct cm_timewait_info *)work; | |
8575329d SH |
2630 | spin_lock_irq(&cm.lock); |
2631 | list_del(&timewait_info->list); | |
2632 | spin_unlock_irq(&cm.lock); | |
a977049d HR |
2633 | |
2634 | cm_id_priv = cm_acquire_id(timewait_info->work.local_id, | |
2635 | timewait_info->work.remote_id); | |
2636 | if (!cm_id_priv) | |
2637 | return -EINVAL; | |
2638 | ||
8575329d | 2639 | spin_lock_irq(&cm_id_priv->lock); |
a977049d HR |
2640 | if (cm_id_priv->id.state != IB_CM_TIMEWAIT || |
2641 | cm_id_priv->remote_qpn != timewait_info->remote_qpn) { | |
8575329d | 2642 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
2643 | goto out; |
2644 | } | |
2645 | cm_id_priv->id.state = IB_CM_IDLE; | |
2646 | ret = atomic_inc_and_test(&cm_id_priv->work_count); | |
2647 | if (!ret) | |
2648 | list_add_tail(&work->list, &cm_id_priv->work_list); | |
8575329d | 2649 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
2650 | |
2651 | if (ret) | |
2652 | cm_process_work(cm_id_priv, work); | |
2653 | else | |
2654 | cm_deref_id(cm_id_priv); | |
2655 | return 0; | |
2656 | out: | |
2657 | cm_deref_id(cm_id_priv); | |
2658 | return -EINVAL; | |
2659 | } | |
2660 | ||
2661 | static void cm_format_sidr_req(struct cm_sidr_req_msg *sidr_req_msg, | |
2662 | struct cm_id_private *cm_id_priv, | |
2663 | struct ib_cm_sidr_req_param *param) | |
2664 | { | |
2665 | cm_format_mad_hdr(&sidr_req_msg->hdr, CM_SIDR_REQ_ATTR_ID, | |
2666 | cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_SIDR)); | |
2667 | sidr_req_msg->request_id = cm_id_priv->id.local_id; | |
75af9088 | 2668 | sidr_req_msg->pkey = cpu_to_be16(param->path->pkey); |
a977049d HR |
2669 | sidr_req_msg->service_id = param->service_id; |
2670 | ||
2671 | if (param->private_data && param->private_data_len) | |
2672 | memcpy(sidr_req_msg->private_data, param->private_data, | |
2673 | param->private_data_len); | |
2674 | } | |
2675 | ||
2676 | int ib_send_cm_sidr_req(struct ib_cm_id *cm_id, | |
2677 | struct ib_cm_sidr_req_param *param) | |
2678 | { | |
2679 | struct cm_id_private *cm_id_priv; | |
2680 | struct ib_mad_send_buf *msg; | |
a977049d HR |
2681 | unsigned long flags; |
2682 | int ret; | |
2683 | ||
2684 | if (!param->path || (param->private_data && | |
2685 | param->private_data_len > IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE)) | |
2686 | return -EINVAL; | |
2687 | ||
2688 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); | |
2689 | ret = cm_init_av_by_path(param->path, &cm_id_priv->av); | |
2690 | if (ret) | |
2691 | goto out; | |
2692 | ||
2693 | cm_id->service_id = param->service_id; | |
97f52eb4 | 2694 | cm_id->service_mask = __constant_cpu_to_be64(~0ULL); |
a977049d HR |
2695 | cm_id_priv->timeout_ms = param->timeout_ms; |
2696 | cm_id_priv->max_cm_retries = param->max_cm_retries; | |
2697 | ret = cm_alloc_msg(cm_id_priv, &msg); | |
2698 | if (ret) | |
2699 | goto out; | |
2700 | ||
2701 | cm_format_sidr_req((struct cm_sidr_req_msg *) msg->mad, cm_id_priv, | |
2702 | param); | |
34816ad9 | 2703 | msg->timeout_ms = cm_id_priv->timeout_ms; |
a977049d HR |
2704 | msg->context[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT; |
2705 | ||
2706 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
2707 | if (cm_id->state == IB_CM_IDLE) | |
34816ad9 | 2708 | ret = ib_post_send_mad(msg, NULL); |
a977049d HR |
2709 | else |
2710 | ret = -EINVAL; | |
2711 | ||
2712 | if (ret) { | |
2713 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
2714 | cm_free_msg(msg); | |
2715 | goto out; | |
2716 | } | |
2717 | cm_id->state = IB_CM_SIDR_REQ_SENT; | |
2718 | cm_id_priv->msg = msg; | |
2719 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
2720 | out: | |
2721 | return ret; | |
2722 | } | |
2723 | EXPORT_SYMBOL(ib_send_cm_sidr_req); | |
2724 | ||
2725 | static void cm_format_sidr_req_event(struct cm_work *work, | |
2726 | struct ib_cm_id *listen_id) | |
2727 | { | |
2728 | struct cm_sidr_req_msg *sidr_req_msg; | |
2729 | struct ib_cm_sidr_req_event_param *param; | |
2730 | ||
2731 | sidr_req_msg = (struct cm_sidr_req_msg *) | |
2732 | work->mad_recv_wc->recv_buf.mad; | |
2733 | param = &work->cm_event.param.sidr_req_rcvd; | |
97f52eb4 | 2734 | param->pkey = __be16_to_cpu(sidr_req_msg->pkey); |
a977049d | 2735 | param->listen_id = listen_id; |
a977049d HR |
2736 | param->port = work->port->port_num; |
2737 | work->cm_event.private_data = &sidr_req_msg->private_data; | |
2738 | } | |
2739 | ||
2740 | static int cm_sidr_req_handler(struct cm_work *work) | |
2741 | { | |
2742 | struct ib_cm_id *cm_id; | |
2743 | struct cm_id_private *cm_id_priv, *cur_cm_id_priv; | |
2744 | struct cm_sidr_req_msg *sidr_req_msg; | |
2745 | struct ib_wc *wc; | |
a977049d | 2746 | |
07d357d0 | 2747 | cm_id = ib_create_cm_id(work->port->cm_dev->device, NULL, NULL); |
a977049d HR |
2748 | if (IS_ERR(cm_id)) |
2749 | return PTR_ERR(cm_id); | |
2750 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); | |
2751 | ||
2752 | /* Record SGID/SLID and request ID for lookup. */ | |
2753 | sidr_req_msg = (struct cm_sidr_req_msg *) | |
2754 | work->mad_recv_wc->recv_buf.mad; | |
2755 | wc = work->mad_recv_wc->wc; | |
97f52eb4 | 2756 | cm_id_priv->av.dgid.global.subnet_prefix = cpu_to_be64(wc->slid); |
a977049d HR |
2757 | cm_id_priv->av.dgid.global.interface_id = 0; |
2758 | cm_init_av_for_response(work->port, work->mad_recv_wc->wc, | |
ca222c6b | 2759 | work->mad_recv_wc->recv_buf.grh, |
a977049d HR |
2760 | &cm_id_priv->av); |
2761 | cm_id_priv->id.remote_id = sidr_req_msg->request_id; | |
2762 | cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD; | |
2763 | cm_id_priv->tid = sidr_req_msg->hdr.tid; | |
2764 | atomic_inc(&cm_id_priv->work_count); | |
2765 | ||
24be6e81 | 2766 | spin_lock_irq(&cm.lock); |
a977049d HR |
2767 | cur_cm_id_priv = cm_insert_remote_sidr(cm_id_priv); |
2768 | if (cur_cm_id_priv) { | |
24be6e81 | 2769 | spin_unlock_irq(&cm.lock); |
a977049d HR |
2770 | goto out; /* Duplicate message. */ |
2771 | } | |
07d357d0 | 2772 | cur_cm_id_priv = cm_find_listen(cm_id->device, |
6e61d04f SH |
2773 | sidr_req_msg->service_id, |
2774 | sidr_req_msg->private_data); | |
a977049d HR |
2775 | if (!cur_cm_id_priv) { |
2776 | rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table); | |
24be6e81 | 2777 | spin_unlock_irq(&cm.lock); |
a977049d HR |
2778 | /* todo: reply with no match */ |
2779 | goto out; /* No match. */ | |
2780 | } | |
2781 | atomic_inc(&cur_cm_id_priv->refcount); | |
24be6e81 | 2782 | spin_unlock_irq(&cm.lock); |
a977049d HR |
2783 | |
2784 | cm_id_priv->id.cm_handler = cur_cm_id_priv->id.cm_handler; | |
2785 | cm_id_priv->id.context = cur_cm_id_priv->id.context; | |
2786 | cm_id_priv->id.service_id = sidr_req_msg->service_id; | |
97f52eb4 | 2787 | cm_id_priv->id.service_mask = __constant_cpu_to_be64(~0ULL); |
a977049d HR |
2788 | |
2789 | cm_format_sidr_req_event(work, &cur_cm_id_priv->id); | |
2790 | cm_process_work(cm_id_priv, work); | |
2791 | cm_deref_id(cur_cm_id_priv); | |
2792 | return 0; | |
2793 | out: | |
2794 | ib_destroy_cm_id(&cm_id_priv->id); | |
2795 | return -EINVAL; | |
2796 | } | |
2797 | ||
2798 | static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg, | |
2799 | struct cm_id_private *cm_id_priv, | |
2800 | struct ib_cm_sidr_rep_param *param) | |
2801 | { | |
2802 | cm_format_mad_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID, | |
2803 | cm_id_priv->tid); | |
2804 | sidr_rep_msg->request_id = cm_id_priv->id.remote_id; | |
2805 | sidr_rep_msg->status = param->status; | |
2806 | cm_sidr_rep_set_qpn(sidr_rep_msg, cpu_to_be32(param->qp_num)); | |
2807 | sidr_rep_msg->service_id = cm_id_priv->id.service_id; | |
2808 | sidr_rep_msg->qkey = cpu_to_be32(param->qkey); | |
2809 | ||
2810 | if (param->info && param->info_length) | |
2811 | memcpy(sidr_rep_msg->info, param->info, param->info_length); | |
2812 | ||
2813 | if (param->private_data && param->private_data_len) | |
2814 | memcpy(sidr_rep_msg->private_data, param->private_data, | |
2815 | param->private_data_len); | |
2816 | } | |
2817 | ||
2818 | int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id, | |
2819 | struct ib_cm_sidr_rep_param *param) | |
2820 | { | |
2821 | struct cm_id_private *cm_id_priv; | |
2822 | struct ib_mad_send_buf *msg; | |
a977049d HR |
2823 | unsigned long flags; |
2824 | int ret; | |
2825 | ||
2826 | if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) || | |
2827 | (param->private_data && | |
2828 | param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE)) | |
2829 | return -EINVAL; | |
2830 | ||
2831 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); | |
2832 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
2833 | if (cm_id->state != IB_CM_SIDR_REQ_RCVD) { | |
2834 | ret = -EINVAL; | |
2835 | goto error; | |
2836 | } | |
2837 | ||
2838 | ret = cm_alloc_msg(cm_id_priv, &msg); | |
2839 | if (ret) | |
2840 | goto error; | |
2841 | ||
2842 | cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv, | |
2843 | param); | |
34816ad9 | 2844 | ret = ib_post_send_mad(msg, NULL); |
a977049d HR |
2845 | if (ret) { |
2846 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
2847 | cm_free_msg(msg); | |
2848 | return ret; | |
2849 | } | |
2850 | cm_id->state = IB_CM_IDLE; | |
2851 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
2852 | ||
2853 | spin_lock_irqsave(&cm.lock, flags); | |
2854 | rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table); | |
2855 | spin_unlock_irqrestore(&cm.lock, flags); | |
2856 | return 0; | |
2857 | ||
2858 | error: spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
2859 | return ret; | |
2860 | } | |
2861 | EXPORT_SYMBOL(ib_send_cm_sidr_rep); | |
2862 | ||
2863 | static void cm_format_sidr_rep_event(struct cm_work *work) | |
2864 | { | |
2865 | struct cm_sidr_rep_msg *sidr_rep_msg; | |
2866 | struct ib_cm_sidr_rep_event_param *param; | |
2867 | ||
2868 | sidr_rep_msg = (struct cm_sidr_rep_msg *) | |
2869 | work->mad_recv_wc->recv_buf.mad; | |
2870 | param = &work->cm_event.param.sidr_rep_rcvd; | |
2871 | param->status = sidr_rep_msg->status; | |
2872 | param->qkey = be32_to_cpu(sidr_rep_msg->qkey); | |
2873 | param->qpn = be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg)); | |
2874 | param->info = &sidr_rep_msg->info; | |
2875 | param->info_len = sidr_rep_msg->info_length; | |
2876 | work->cm_event.private_data = &sidr_rep_msg->private_data; | |
2877 | } | |
2878 | ||
2879 | static int cm_sidr_rep_handler(struct cm_work *work) | |
2880 | { | |
2881 | struct cm_sidr_rep_msg *sidr_rep_msg; | |
2882 | struct cm_id_private *cm_id_priv; | |
a977049d HR |
2883 | |
2884 | sidr_rep_msg = (struct cm_sidr_rep_msg *) | |
2885 | work->mad_recv_wc->recv_buf.mad; | |
2886 | cm_id_priv = cm_acquire_id(sidr_rep_msg->request_id, 0); | |
2887 | if (!cm_id_priv) | |
2888 | return -EINVAL; /* Unmatched reply. */ | |
2889 | ||
24be6e81 | 2890 | spin_lock_irq(&cm_id_priv->lock); |
a977049d | 2891 | if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) { |
24be6e81 | 2892 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
2893 | goto out; |
2894 | } | |
2895 | cm_id_priv->id.state = IB_CM_IDLE; | |
34816ad9 | 2896 | ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); |
24be6e81 | 2897 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
2898 | |
2899 | cm_format_sidr_rep_event(work); | |
2900 | cm_process_work(cm_id_priv, work); | |
2901 | return 0; | |
2902 | out: | |
2903 | cm_deref_id(cm_id_priv); | |
2904 | return -EINVAL; | |
2905 | } | |
2906 | ||
2907 | static void cm_process_send_error(struct ib_mad_send_buf *msg, | |
2908 | enum ib_wc_status wc_status) | |
2909 | { | |
2910 | struct cm_id_private *cm_id_priv; | |
2911 | struct ib_cm_event cm_event; | |
2912 | enum ib_cm_state state; | |
a977049d HR |
2913 | int ret; |
2914 | ||
2915 | memset(&cm_event, 0, sizeof cm_event); | |
2916 | cm_id_priv = msg->context[0]; | |
2917 | ||
2918 | /* Discard old sends or ones without a response. */ | |
24be6e81 | 2919 | spin_lock_irq(&cm_id_priv->lock); |
a977049d HR |
2920 | state = (enum ib_cm_state) (unsigned long) msg->context[1]; |
2921 | if (msg != cm_id_priv->msg || state != cm_id_priv->id.state) | |
2922 | goto discard; | |
2923 | ||
2924 | switch (state) { | |
2925 | case IB_CM_REQ_SENT: | |
2926 | case IB_CM_MRA_REQ_RCVD: | |
2927 | cm_reset_to_idle(cm_id_priv); | |
2928 | cm_event.event = IB_CM_REQ_ERROR; | |
2929 | break; | |
2930 | case IB_CM_REP_SENT: | |
2931 | case IB_CM_MRA_REP_RCVD: | |
2932 | cm_reset_to_idle(cm_id_priv); | |
2933 | cm_event.event = IB_CM_REP_ERROR; | |
2934 | break; | |
2935 | case IB_CM_DREQ_SENT: | |
2936 | cm_enter_timewait(cm_id_priv); | |
2937 | cm_event.event = IB_CM_DREQ_ERROR; | |
2938 | break; | |
2939 | case IB_CM_SIDR_REQ_SENT: | |
2940 | cm_id_priv->id.state = IB_CM_IDLE; | |
2941 | cm_event.event = IB_CM_SIDR_REQ_ERROR; | |
2942 | break; | |
2943 | default: | |
2944 | goto discard; | |
2945 | } | |
24be6e81 | 2946 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
2947 | cm_event.param.send_status = wc_status; |
2948 | ||
2949 | /* No other events can occur on the cm_id at this point. */ | |
2950 | ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event); | |
2951 | cm_free_msg(msg); | |
2952 | if (ret) | |
2953 | ib_destroy_cm_id(&cm_id_priv->id); | |
2954 | return; | |
2955 | discard: | |
24be6e81 | 2956 | spin_unlock_irq(&cm_id_priv->lock); |
a977049d HR |
2957 | cm_free_msg(msg); |
2958 | } | |
2959 | ||
2960 | static void cm_send_handler(struct ib_mad_agent *mad_agent, | |
2961 | struct ib_mad_send_wc *mad_send_wc) | |
2962 | { | |
34816ad9 | 2963 | struct ib_mad_send_buf *msg = mad_send_wc->send_buf; |
a977049d HR |
2964 | |
2965 | switch (mad_send_wc->status) { | |
2966 | case IB_WC_SUCCESS: | |
2967 | case IB_WC_WR_FLUSH_ERR: | |
2968 | cm_free_msg(msg); | |
2969 | break; | |
2970 | default: | |
2971 | if (msg->context[0] && msg->context[1]) | |
2972 | cm_process_send_error(msg, mad_send_wc->status); | |
2973 | else | |
2974 | cm_free_msg(msg); | |
2975 | break; | |
2976 | } | |
2977 | } | |
2978 | ||
c4028958 | 2979 | static void cm_work_handler(struct work_struct *_work) |
a977049d | 2980 | { |
c4028958 | 2981 | struct cm_work *work = container_of(_work, struct cm_work, work.work); |
a977049d HR |
2982 | int ret; |
2983 | ||
2984 | switch (work->cm_event.event) { | |
2985 | case IB_CM_REQ_RECEIVED: | |
2986 | ret = cm_req_handler(work); | |
2987 | break; | |
2988 | case IB_CM_MRA_RECEIVED: | |
2989 | ret = cm_mra_handler(work); | |
2990 | break; | |
2991 | case IB_CM_REJ_RECEIVED: | |
2992 | ret = cm_rej_handler(work); | |
2993 | break; | |
2994 | case IB_CM_REP_RECEIVED: | |
2995 | ret = cm_rep_handler(work); | |
2996 | break; | |
2997 | case IB_CM_RTU_RECEIVED: | |
2998 | ret = cm_rtu_handler(work); | |
2999 | break; | |
3000 | case IB_CM_USER_ESTABLISHED: | |
3001 | ret = cm_establish_handler(work); | |
3002 | break; | |
3003 | case IB_CM_DREQ_RECEIVED: | |
3004 | ret = cm_dreq_handler(work); | |
3005 | break; | |
3006 | case IB_CM_DREP_RECEIVED: | |
3007 | ret = cm_drep_handler(work); | |
3008 | break; | |
3009 | case IB_CM_SIDR_REQ_RECEIVED: | |
3010 | ret = cm_sidr_req_handler(work); | |
3011 | break; | |
3012 | case IB_CM_SIDR_REP_RECEIVED: | |
3013 | ret = cm_sidr_rep_handler(work); | |
3014 | break; | |
3015 | case IB_CM_LAP_RECEIVED: | |
3016 | ret = cm_lap_handler(work); | |
3017 | break; | |
3018 | case IB_CM_APR_RECEIVED: | |
3019 | ret = cm_apr_handler(work); | |
3020 | break; | |
3021 | case IB_CM_TIMEWAIT_EXIT: | |
3022 | ret = cm_timewait_handler(work); | |
3023 | break; | |
3024 | default: | |
3025 | ret = -EINVAL; | |
3026 | break; | |
3027 | } | |
3028 | if (ret) | |
3029 | cm_free_work(work); | |
3030 | } | |
3031 | ||
e1444b5a | 3032 | static int cm_establish(struct ib_cm_id *cm_id) |
a977049d HR |
3033 | { |
3034 | struct cm_id_private *cm_id_priv; | |
3035 | struct cm_work *work; | |
3036 | unsigned long flags; | |
3037 | int ret = 0; | |
3038 | ||
3039 | work = kmalloc(sizeof *work, GFP_ATOMIC); | |
3040 | if (!work) | |
3041 | return -ENOMEM; | |
3042 | ||
3043 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); | |
3044 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
3045 | switch (cm_id->state) | |
3046 | { | |
3047 | case IB_CM_REP_SENT: | |
3048 | case IB_CM_MRA_REP_RCVD: | |
3049 | cm_id->state = IB_CM_ESTABLISHED; | |
3050 | break; | |
3051 | case IB_CM_ESTABLISHED: | |
3052 | ret = -EISCONN; | |
3053 | break; | |
3054 | default: | |
3055 | ret = -EINVAL; | |
3056 | break; | |
3057 | } | |
3058 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
3059 | ||
3060 | if (ret) { | |
3061 | kfree(work); | |
3062 | goto out; | |
3063 | } | |
3064 | ||
3065 | /* | |
3066 | * The CM worker thread may try to destroy the cm_id before it | |
3067 | * can execute this work item. To prevent potential deadlock, | |
3068 | * we need to find the cm_id once we're in the context of the | |
3069 | * worker thread, rather than holding a reference on it. | |
3070 | */ | |
c4028958 | 3071 | INIT_DELAYED_WORK(&work->work, cm_work_handler); |
a977049d HR |
3072 | work->local_id = cm_id->local_id; |
3073 | work->remote_id = cm_id->remote_id; | |
3074 | work->mad_recv_wc = NULL; | |
3075 | work->cm_event.event = IB_CM_USER_ESTABLISHED; | |
c4028958 | 3076 | queue_delayed_work(cm.wq, &work->work, 0); |
a977049d HR |
3077 | out: |
3078 | return ret; | |
3079 | } | |
e1444b5a SH |
3080 | |
3081 | static int cm_migrate(struct ib_cm_id *cm_id) | |
3082 | { | |
3083 | struct cm_id_private *cm_id_priv; | |
3084 | unsigned long flags; | |
3085 | int ret = 0; | |
3086 | ||
3087 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); | |
3088 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
3089 | if (cm_id->state == IB_CM_ESTABLISHED && | |
3090 | (cm_id->lap_state == IB_CM_LAP_UNINIT || | |
3091 | cm_id->lap_state == IB_CM_LAP_IDLE)) { | |
3092 | cm_id->lap_state = IB_CM_LAP_IDLE; | |
3093 | cm_id_priv->av = cm_id_priv->alt_av; | |
3094 | } else | |
3095 | ret = -EINVAL; | |
3096 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
3097 | ||
3098 | return ret; | |
3099 | } | |
3100 | ||
3101 | int ib_cm_notify(struct ib_cm_id *cm_id, enum ib_event_type event) | |
3102 | { | |
3103 | int ret; | |
3104 | ||
3105 | switch (event) { | |
3106 | case IB_EVENT_COMM_EST: | |
3107 | ret = cm_establish(cm_id); | |
3108 | break; | |
3109 | case IB_EVENT_PATH_MIG: | |
3110 | ret = cm_migrate(cm_id); | |
3111 | break; | |
3112 | default: | |
3113 | ret = -EINVAL; | |
3114 | } | |
3115 | return ret; | |
3116 | } | |
3117 | EXPORT_SYMBOL(ib_cm_notify); | |
a977049d HR |
3118 | |
3119 | static void cm_recv_handler(struct ib_mad_agent *mad_agent, | |
3120 | struct ib_mad_recv_wc *mad_recv_wc) | |
3121 | { | |
3122 | struct cm_work *work; | |
3123 | enum ib_cm_event_type event; | |
3124 | int paths = 0; | |
3125 | ||
3126 | switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) { | |
3127 | case CM_REQ_ATTR_ID: | |
3128 | paths = 1 + (((struct cm_req_msg *) mad_recv_wc->recv_buf.mad)-> | |
3129 | alt_local_lid != 0); | |
3130 | event = IB_CM_REQ_RECEIVED; | |
3131 | break; | |
3132 | case CM_MRA_ATTR_ID: | |
3133 | event = IB_CM_MRA_RECEIVED; | |
3134 | break; | |
3135 | case CM_REJ_ATTR_ID: | |
3136 | event = IB_CM_REJ_RECEIVED; | |
3137 | break; | |
3138 | case CM_REP_ATTR_ID: | |
3139 | event = IB_CM_REP_RECEIVED; | |
3140 | break; | |
3141 | case CM_RTU_ATTR_ID: | |
3142 | event = IB_CM_RTU_RECEIVED; | |
3143 | break; | |
3144 | case CM_DREQ_ATTR_ID: | |
3145 | event = IB_CM_DREQ_RECEIVED; | |
3146 | break; | |
3147 | case CM_DREP_ATTR_ID: | |
3148 | event = IB_CM_DREP_RECEIVED; | |
3149 | break; | |
3150 | case CM_SIDR_REQ_ATTR_ID: | |
3151 | event = IB_CM_SIDR_REQ_RECEIVED; | |
3152 | break; | |
3153 | case CM_SIDR_REP_ATTR_ID: | |
3154 | event = IB_CM_SIDR_REP_RECEIVED; | |
3155 | break; | |
3156 | case CM_LAP_ATTR_ID: | |
3157 | paths = 1; | |
3158 | event = IB_CM_LAP_RECEIVED; | |
3159 | break; | |
3160 | case CM_APR_ATTR_ID: | |
3161 | event = IB_CM_APR_RECEIVED; | |
3162 | break; | |
3163 | default: | |
3164 | ib_free_recv_mad(mad_recv_wc); | |
3165 | return; | |
3166 | } | |
3167 | ||
3168 | work = kmalloc(sizeof *work + sizeof(struct ib_sa_path_rec) * paths, | |
3169 | GFP_KERNEL); | |
3170 | if (!work) { | |
3171 | ib_free_recv_mad(mad_recv_wc); | |
3172 | return; | |
3173 | } | |
3174 | ||
c4028958 | 3175 | INIT_DELAYED_WORK(&work->work, cm_work_handler); |
a977049d HR |
3176 | work->cm_event.event = event; |
3177 | work->mad_recv_wc = mad_recv_wc; | |
3178 | work->port = (struct cm_port *)mad_agent->context; | |
c4028958 | 3179 | queue_delayed_work(cm.wq, &work->work, 0); |
a977049d HR |
3180 | } |
3181 | ||
3182 | static int cm_init_qp_init_attr(struct cm_id_private *cm_id_priv, | |
3183 | struct ib_qp_attr *qp_attr, | |
3184 | int *qp_attr_mask) | |
3185 | { | |
3186 | unsigned long flags; | |
3187 | int ret; | |
3188 | ||
3189 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
3190 | switch (cm_id_priv->id.state) { | |
3191 | case IB_CM_REQ_SENT: | |
3192 | case IB_CM_MRA_REQ_RCVD: | |
3193 | case IB_CM_REQ_RCVD: | |
3194 | case IB_CM_MRA_REQ_SENT: | |
3195 | case IB_CM_REP_RCVD: | |
3196 | case IB_CM_MRA_REP_SENT: | |
3197 | case IB_CM_REP_SENT: | |
3198 | case IB_CM_MRA_REP_RCVD: | |
3199 | case IB_CM_ESTABLISHED: | |
3200 | *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS | | |
3201 | IB_QP_PKEY_INDEX | IB_QP_PORT; | |
e31353ea | 3202 | qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE; |
a977049d | 3203 | if (cm_id_priv->responder_resources) |
c1f250c0 SH |
3204 | qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ | |
3205 | IB_ACCESS_REMOTE_ATOMIC; | |
a977049d HR |
3206 | qp_attr->pkey_index = cm_id_priv->av.pkey_index; |
3207 | qp_attr->port_num = cm_id_priv->av.port->port_num; | |
3208 | ret = 0; | |
3209 | break; | |
3210 | default: | |
3211 | ret = -EINVAL; | |
3212 | break; | |
3213 | } | |
3214 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
3215 | return ret; | |
3216 | } | |
3217 | ||
3218 | static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv, | |
3219 | struct ib_qp_attr *qp_attr, | |
3220 | int *qp_attr_mask) | |
3221 | { | |
3222 | unsigned long flags; | |
3223 | int ret; | |
3224 | ||
3225 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
3226 | switch (cm_id_priv->id.state) { | |
3227 | case IB_CM_REQ_RCVD: | |
3228 | case IB_CM_MRA_REQ_SENT: | |
3229 | case IB_CM_REP_RCVD: | |
3230 | case IB_CM_MRA_REP_SENT: | |
3231 | case IB_CM_REP_SENT: | |
3232 | case IB_CM_MRA_REP_RCVD: | |
3233 | case IB_CM_ESTABLISHED: | |
3234 | *qp_attr_mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU | | |
ae7971a7 | 3235 | IB_QP_DEST_QPN | IB_QP_RQ_PSN; |
a977049d HR |
3236 | qp_attr->ah_attr = cm_id_priv->av.ah_attr; |
3237 | qp_attr->path_mtu = cm_id_priv->path_mtu; | |
3238 | qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn); | |
3239 | qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn); | |
ae7971a7 SH |
3240 | if (cm_id_priv->qp_type == IB_QPT_RC) { |
3241 | *qp_attr_mask |= IB_QP_MAX_DEST_RD_ATOMIC | | |
3242 | IB_QP_MIN_RNR_TIMER; | |
3243 | qp_attr->max_dest_rd_atomic = | |
3244 | cm_id_priv->responder_resources; | |
3245 | qp_attr->min_rnr_timer = 0; | |
3246 | } | |
a977049d HR |
3247 | if (cm_id_priv->alt_av.ah_attr.dlid) { |
3248 | *qp_attr_mask |= IB_QP_ALT_PATH; | |
0d8fdfd7 | 3249 | qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num; |
e1444b5a SH |
3250 | qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index; |
3251 | qp_attr->alt_timeout = | |
3252 | cm_id_priv->alt_av.packet_life_time + 1; | |
a977049d HR |
3253 | qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr; |
3254 | } | |
3255 | ret = 0; | |
3256 | break; | |
3257 | default: | |
3258 | ret = -EINVAL; | |
3259 | break; | |
3260 | } | |
3261 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
3262 | return ret; | |
3263 | } | |
3264 | ||
3265 | static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv, | |
3266 | struct ib_qp_attr *qp_attr, | |
3267 | int *qp_attr_mask) | |
3268 | { | |
3269 | unsigned long flags; | |
3270 | int ret; | |
3271 | ||
3272 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
3273 | switch (cm_id_priv->id.state) { | |
0fe313b0 SH |
3274 | /* Allow transition to RTS before sending REP */ |
3275 | case IB_CM_REQ_RCVD: | |
3276 | case IB_CM_MRA_REQ_SENT: | |
3277 | ||
a977049d HR |
3278 | case IB_CM_REP_RCVD: |
3279 | case IB_CM_MRA_REP_SENT: | |
3280 | case IB_CM_REP_SENT: | |
3281 | case IB_CM_MRA_REP_RCVD: | |
3282 | case IB_CM_ESTABLISHED: | |
e1444b5a SH |
3283 | if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT) { |
3284 | *qp_attr_mask = IB_QP_STATE | IB_QP_SQ_PSN; | |
3285 | qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn); | |
3286 | if (cm_id_priv->qp_type == IB_QPT_RC) { | |
3287 | *qp_attr_mask |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT | | |
3288 | IB_QP_RNR_RETRY | | |
3289 | IB_QP_MAX_QP_RD_ATOMIC; | |
3290 | qp_attr->timeout = | |
3291 | cm_id_priv->av.packet_life_time + 1; | |
3292 | qp_attr->retry_cnt = cm_id_priv->retry_count; | |
3293 | qp_attr->rnr_retry = cm_id_priv->rnr_retry_count; | |
3294 | qp_attr->max_rd_atomic = | |
3295 | cm_id_priv->initiator_depth; | |
3296 | } | |
3297 | if (cm_id_priv->alt_av.ah_attr.dlid) { | |
3298 | *qp_attr_mask |= IB_QP_PATH_MIG_STATE; | |
3299 | qp_attr->path_mig_state = IB_MIG_REARM; | |
3300 | } | |
3301 | } else { | |
3302 | *qp_attr_mask = IB_QP_ALT_PATH | IB_QP_PATH_MIG_STATE; | |
3303 | qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num; | |
3304 | qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index; | |
3305 | qp_attr->alt_timeout = | |
3306 | cm_id_priv->alt_av.packet_life_time + 1; | |
3307 | qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr; | |
a977049d HR |
3308 | qp_attr->path_mig_state = IB_MIG_REARM; |
3309 | } | |
3310 | ret = 0; | |
3311 | break; | |
3312 | default: | |
3313 | ret = -EINVAL; | |
3314 | break; | |
3315 | } | |
3316 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
3317 | return ret; | |
3318 | } | |
3319 | ||
3320 | int ib_cm_init_qp_attr(struct ib_cm_id *cm_id, | |
3321 | struct ib_qp_attr *qp_attr, | |
3322 | int *qp_attr_mask) | |
3323 | { | |
3324 | struct cm_id_private *cm_id_priv; | |
3325 | int ret; | |
3326 | ||
3327 | cm_id_priv = container_of(cm_id, struct cm_id_private, id); | |
3328 | switch (qp_attr->qp_state) { | |
3329 | case IB_QPS_INIT: | |
3330 | ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask); | |
3331 | break; | |
3332 | case IB_QPS_RTR: | |
3333 | ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask); | |
3334 | break; | |
3335 | case IB_QPS_RTS: | |
3336 | ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask); | |
3337 | break; | |
3338 | default: | |
3339 | ret = -EINVAL; | |
3340 | break; | |
3341 | } | |
3342 | return ret; | |
3343 | } | |
3344 | EXPORT_SYMBOL(ib_cm_init_qp_attr); | |
3345 | ||
a977049d HR |
3346 | static void cm_add_one(struct ib_device *device) |
3347 | { | |
3348 | struct cm_device *cm_dev; | |
3349 | struct cm_port *port; | |
3350 | struct ib_mad_reg_req reg_req = { | |
3351 | .mgmt_class = IB_MGMT_CLASS_CM, | |
3352 | .mgmt_class_version = IB_CM_CLASS_VERSION | |
3353 | }; | |
3354 | struct ib_port_modify port_modify = { | |
3355 | .set_port_cap_mask = IB_PORT_CM_SUP | |
3356 | }; | |
3357 | unsigned long flags; | |
3358 | int ret; | |
3359 | u8 i; | |
3360 | ||
07ebafba TT |
3361 | if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB) |
3362 | return; | |
3363 | ||
a977049d HR |
3364 | cm_dev = kmalloc(sizeof(*cm_dev) + sizeof(*port) * |
3365 | device->phys_port_cnt, GFP_KERNEL); | |
3366 | if (!cm_dev) | |
3367 | return; | |
3368 | ||
3369 | cm_dev->device = device; | |
a977049d HR |
3370 | |
3371 | set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask); | |
3372 | for (i = 1; i <= device->phys_port_cnt; i++) { | |
3373 | port = &cm_dev->port[i-1]; | |
3374 | port->cm_dev = cm_dev; | |
3375 | port->port_num = i; | |
3376 | port->mad_agent = ib_register_mad_agent(device, i, | |
3377 | IB_QPT_GSI, | |
3378 | ®_req, | |
3379 | 0, | |
3380 | cm_send_handler, | |
3381 | cm_recv_handler, | |
3382 | port); | |
3383 | if (IS_ERR(port->mad_agent)) | |
cf311cd4 | 3384 | goto error1; |
a977049d HR |
3385 | |
3386 | ret = ib_modify_port(device, i, 0, &port_modify); | |
3387 | if (ret) | |
cf311cd4 | 3388 | goto error2; |
a977049d HR |
3389 | } |
3390 | ib_set_client_data(device, &cm_client, cm_dev); | |
3391 | ||
3392 | write_lock_irqsave(&cm.device_lock, flags); | |
3393 | list_add_tail(&cm_dev->list, &cm.device_list); | |
3394 | write_unlock_irqrestore(&cm.device_lock, flags); | |
3395 | return; | |
3396 | ||
a977049d | 3397 | error2: |
cf311cd4 SH |
3398 | ib_unregister_mad_agent(port->mad_agent); |
3399 | error1: | |
a977049d HR |
3400 | port_modify.set_port_cap_mask = 0; |
3401 | port_modify.clr_port_cap_mask = IB_PORT_CM_SUP; | |
3402 | while (--i) { | |
3403 | port = &cm_dev->port[i-1]; | |
3404 | ib_modify_port(device, port->port_num, 0, &port_modify); | |
3405 | ib_unregister_mad_agent(port->mad_agent); | |
3406 | } | |
a977049d HR |
3407 | kfree(cm_dev); |
3408 | } | |
3409 | ||
3410 | static void cm_remove_one(struct ib_device *device) | |
3411 | { | |
3412 | struct cm_device *cm_dev; | |
3413 | struct cm_port *port; | |
3414 | struct ib_port_modify port_modify = { | |
3415 | .clr_port_cap_mask = IB_PORT_CM_SUP | |
3416 | }; | |
3417 | unsigned long flags; | |
3418 | int i; | |
3419 | ||
3420 | cm_dev = ib_get_client_data(device, &cm_client); | |
3421 | if (!cm_dev) | |
3422 | return; | |
3423 | ||
3424 | write_lock_irqsave(&cm.device_lock, flags); | |
3425 | list_del(&cm_dev->list); | |
3426 | write_unlock_irqrestore(&cm.device_lock, flags); | |
3427 | ||
3428 | for (i = 1; i <= device->phys_port_cnt; i++) { | |
3429 | port = &cm_dev->port[i-1]; | |
3430 | ib_modify_port(device, port->port_num, 0, &port_modify); | |
3431 | ib_unregister_mad_agent(port->mad_agent); | |
3432 | } | |
3433 | kfree(cm_dev); | |
3434 | } | |
3435 | ||
3436 | static int __init ib_cm_init(void) | |
3437 | { | |
3438 | int ret; | |
3439 | ||
3440 | memset(&cm, 0, sizeof cm); | |
3441 | INIT_LIST_HEAD(&cm.device_list); | |
3442 | rwlock_init(&cm.device_lock); | |
3443 | spin_lock_init(&cm.lock); | |
3444 | cm.listen_service_table = RB_ROOT; | |
3445 | cm.listen_service_id = __constant_be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID); | |
3446 | cm.remote_id_table = RB_ROOT; | |
3447 | cm.remote_qp_table = RB_ROOT; | |
3448 | cm.remote_sidr_table = RB_ROOT; | |
3449 | idr_init(&cm.local_id_table); | |
f06d2653 | 3450 | get_random_bytes(&cm.random_id_operand, sizeof cm.random_id_operand); |
a977049d | 3451 | idr_pre_get(&cm.local_id_table, GFP_KERNEL); |
8575329d | 3452 | INIT_LIST_HEAD(&cm.timewait_list); |
a977049d HR |
3453 | |
3454 | cm.wq = create_workqueue("ib_cm"); | |
3455 | if (!cm.wq) | |
3456 | return -ENOMEM; | |
3457 | ||
3458 | ret = ib_register_client(&cm_client); | |
3459 | if (ret) | |
3460 | goto error; | |
3461 | ||
3462 | return 0; | |
3463 | error: | |
3464 | destroy_workqueue(cm.wq); | |
3465 | return ret; | |
3466 | } | |
3467 | ||
3468 | static void __exit ib_cm_cleanup(void) | |
3469 | { | |
8575329d SH |
3470 | struct cm_timewait_info *timewait_info, *tmp; |
3471 | ||
3472 | spin_lock_irq(&cm.lock); | |
3473 | list_for_each_entry(timewait_info, &cm.timewait_list, list) | |
3474 | cancel_delayed_work(&timewait_info->work.work); | |
3475 | spin_unlock_irq(&cm.lock); | |
3476 | ||
a977049d | 3477 | destroy_workqueue(cm.wq); |
8575329d SH |
3478 | |
3479 | list_for_each_entry_safe(timewait_info, tmp, &cm.timewait_list, list) { | |
3480 | list_del(&timewait_info->list); | |
3481 | kfree(timewait_info); | |
3482 | } | |
3483 | ||
a977049d | 3484 | ib_unregister_client(&cm_client); |
5d7edb3c | 3485 | idr_destroy(&cm.local_id_table); |
a977049d HR |
3486 | } |
3487 | ||
3488 | module_init(ib_cm_init); | |
3489 | module_exit(ib_cm_cleanup); | |
3490 |