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