blob: 93e9e2f34fc6a658905619eaaa8b73b9dae18f39 [file] [log] [blame]
Hal Rosenstocka9770492005-07-27 11:45:40 -07001/*
Sean Hefty9af57b72007-07-16 21:49:35 -07002 * Copyright (c) 2004-2007 Intel Corporation. All rights reserved.
Hal Rosenstocka9770492005-07-27 11:45:40 -07003 * 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.
Hal Rosenstocka9770492005-07-27 11:45:40 -070034 */
Sean Hefty1b52fa982006-05-12 14:57:52 -070035
36#include <linux/completion.h>
Hal Rosenstocka9770492005-07-27 11:45:40 -070037#include <linux/dma-mapping.h>
Sean Hefty9af57b72007-07-16 21:49:35 -070038#include <linux/device.h>
Paul Gortmakere4dd23d2011-05-27 15:35:46 -040039#include <linux/module.h>
Hal Rosenstocka9770492005-07-27 11:45:40 -070040#include <linux/err.h>
41#include <linux/idr.h>
42#include <linux/interrupt.h>
Sean Heftyf06d2652006-08-28 15:15:18 -070043#include <linux/random.h>
Hal Rosenstocka9770492005-07-27 11:45:40 -070044#include <linux/rbtree.h>
45#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/slab.h>
Sean Hefty9af57b72007-07-16 21:49:35 -070047#include <linux/sysfs.h>
Hal Rosenstocka9770492005-07-27 11:45:40 -070048#include <linux/workqueue.h>
Greg Kroah-Hartman110cf372008-05-27 10:17:53 +030049#include <linux/kdev_t.h>
Matan Barakdd5f03b2013-12-12 18:03:11 +020050#include <linux/etherdevice.h>
Hal Rosenstocka9770492005-07-27 11:45:40 -070051
Roland Dreiera4d61e82005-08-25 13:40:04 -070052#include <rdma/ib_cache.h>
53#include <rdma/ib_cm.h>
Hal Rosenstocka9770492005-07-27 11:45:40 -070054#include "cm_msgs.h"
55
56MODULE_AUTHOR("Sean Hefty");
57MODULE_DESCRIPTION("InfiniBand CM");
58MODULE_LICENSE("Dual BSD/GPL");
59
60static void cm_add_one(struct ib_device *device);
Haggai Eran7c1eb452015-07-30 17:50:14 +030061static void cm_remove_one(struct ib_device *device, void *client_data);
Hal Rosenstocka9770492005-07-27 11:45:40 -070062
63static struct ib_client cm_client = {
64 .name = "cm",
65 .add = cm_add_one,
66 .remove = cm_remove_one
67};
68
69static 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;
Sean Heftyf06d2652006-08-28 15:15:18 -070080 __be32 random_id_operand;
Sean Hefty85753292006-10-04 11:29:59 -070081 struct list_head timewait_list;
Hal Rosenstocka9770492005-07-27 11:45:40 -070082 struct workqueue_struct *wq;
83} cm;
84
Sean Hefty9af57b72007-07-16 21:49:35 -070085/* Counter indexes ordered by attribute ID */
86enum {
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
102enum {
103 CM_XMIT,
104 CM_XMIT_RETRIES,
105 CM_RECV,
106 CM_RECV_DUPLICATES,
107 CM_COUNTER_GROUPS
108};
109
110static 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
116struct cm_counter_group {
117 struct kobject obj;
118 atomic_long_t counter[CM_ATTR_COUNT];
119};
120
121struct cm_counter_attribute {
122 struct attribute attr;
123 int index;
124};
125
126#define CM_COUNTER_ATTR(_name, _index) \
127struct cm_counter_attribute cm_##_name##_counter_attr = { \
Parag Warudkar01e8ef12008-10-18 20:28:50 -0700128 .attr = { .name = __stringify(_name), .mode = 0444 }, \
Sean Hefty9af57b72007-07-16 21:49:35 -0700129 .index = _index \
130}
131
132static CM_COUNTER_ATTR(req, CM_REQ_COUNTER);
133static CM_COUNTER_ATTR(mra, CM_MRA_COUNTER);
134static CM_COUNTER_ATTR(rej, CM_REJ_COUNTER);
135static CM_COUNTER_ATTR(rep, CM_REP_COUNTER);
136static CM_COUNTER_ATTR(rtu, CM_RTU_COUNTER);
137static CM_COUNTER_ATTR(dreq, CM_DREQ_COUNTER);
138static CM_COUNTER_ATTR(drep, CM_DREP_COUNTER);
139static CM_COUNTER_ATTR(sidr_req, CM_SIDR_REQ_COUNTER);
140static CM_COUNTER_ATTR(sidr_rep, CM_SIDR_REP_COUNTER);
141static CM_COUNTER_ATTR(lap, CM_LAP_COUNTER);
142static CM_COUNTER_ATTR(apr, CM_APR_COUNTER);
143
144static 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
Hal Rosenstocka9770492005-07-27 11:45:40 -0700159struct cm_port {
160 struct cm_device *cm_dev;
161 struct ib_mad_agent *mad_agent;
Sean Hefty9af57b72007-07-16 21:49:35 -0700162 struct kobject port_obj;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700163 u8 port_num;
Sean Hefty9af57b72007-07-16 21:49:35 -0700164 struct cm_counter_group counter_group[CM_COUNTER_GROUPS];
Hal Rosenstocka9770492005-07-27 11:45:40 -0700165};
166
167struct cm_device {
168 struct list_head list;
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +0300169 struct ib_device *ib_device;
Greg Kroah-Hartman110cf372008-05-27 10:17:53 +0300170 struct device *device;
Sean Hefty1d846122007-06-18 11:09:37 -0700171 u8 ack_delay;
Erez Shitritbe4b4992015-06-25 17:13:22 +0300172 int going_down;
Sean Hefty9af57b72007-07-16 21:49:35 -0700173 struct cm_port *port[0];
Hal Rosenstocka9770492005-07-27 11:45:40 -0700174};
175
176struct cm_av {
177 struct cm_port *port;
178 union ib_gid dgid;
179 struct ib_ah_attr ah_attr;
180 u16 pkey_index;
Sean Hefty1d846122007-06-18 11:09:37 -0700181 u8 timeout;
Matan Barakdd5f03b2013-12-12 18:03:11 +0200182 u8 valid;
183 u8 smac[ETH_ALEN];
Hal Rosenstocka9770492005-07-27 11:45:40 -0700184};
185
186struct cm_work {
David Howellsc4028952006-11-22 14:57:56 +0000187 struct delayed_work work;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700188 struct list_head list;
189 struct cm_port *port;
190 struct ib_mad_recv_wc *mad_recv_wc; /* Received MADs */
Sean Hefty97f52eb2005-08-13 21:05:57 -0700191 __be32 local_id; /* Established / timewait */
192 __be32 remote_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700193 struct ib_cm_event cm_event;
194 struct ib_sa_path_rec path[0];
195};
196
197struct cm_timewait_info {
198 struct cm_work work; /* Must be first. */
Sean Hefty85753292006-10-04 11:29:59 -0700199 struct list_head list;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700200 struct rb_node remote_qp_node;
201 struct rb_node remote_id_node;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700202 __be64 remote_ca_guid;
203 __be32 remote_qpn;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700204 u8 inserted_remote_qp;
205 u8 inserted_remote_id;
206};
207
208struct cm_id_private {
209 struct ib_cm_id id;
210
211 struct rb_node service_node;
212 struct rb_node sidr_id_node;
Sean Hefty87fd1a12006-03-02 16:50:37 -0800213 spinlock_t lock; /* Do not acquire inside cm.lock */
Sean Hefty1b52fa982006-05-12 14:57:52 -0700214 struct completion comp;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700215 atomic_t refcount;
216
217 struct ib_mad_send_buf *msg;
218 struct cm_timewait_info *timewait_info;
219 /* todo: use alternate port on send failure */
220 struct cm_av av;
221 struct cm_av alt_av;
Sean Hefty6e61d042006-06-17 20:37:28 -0700222 struct ib_cm_compare_data *compare_data;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700223
224 void *private_data;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700225 __be64 tid;
226 __be32 local_qpn;
227 __be32 remote_qpn;
Sean Heftyae7971a2005-10-24 12:33:56 -0700228 enum ib_qp_type qp_type;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700229 __be32 sq_psn;
230 __be32 rq_psn;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700231 int timeout_ms;
232 enum ib_mtu path_mtu;
Sean Heftye1444b52006-11-28 14:57:13 -0800233 __be16 pkey;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700234 u8 private_data_len;
235 u8 max_cm_retries;
236 u8 peer_to_peer;
237 u8 responder_resources;
238 u8 initiator_depth;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700239 u8 retry_count;
240 u8 rnr_retry_count;
241 u8 service_timeout;
Sean Hefty1d846122007-06-18 11:09:37 -0700242 u8 target_ack_delay;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700243
244 struct list_head work_list;
245 atomic_t work_count;
246};
247
David Howellsc4028952006-11-22 14:57:56 +0000248static void cm_work_handler(struct work_struct *work);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700249
250static inline void cm_deref_id(struct cm_id_private *cm_id_priv)
251{
252 if (atomic_dec_and_test(&cm_id_priv->refcount))
Sean Hefty1b52fa982006-05-12 14:57:52 -0700253 complete(&cm_id_priv->comp);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700254}
255
256static int cm_alloc_msg(struct cm_id_private *cm_id_priv,
257 struct ib_mad_send_buf **msg)
258{
259 struct ib_mad_agent *mad_agent;
260 struct ib_mad_send_buf *m;
261 struct ib_ah *ah;
262
263 mad_agent = cm_id_priv->av.port->mad_agent;
264 ah = ib_create_ah(mad_agent->qp->pd, &cm_id_priv->av.ah_attr);
265 if (IS_ERR(ah))
266 return PTR_ERR(ah);
267
Roland Dreier3cd96562006-09-22 15:22:46 -0700268 m = ib_create_send_mad(mad_agent, cm_id_priv->id.remote_cm_qpn,
John Kingman354ba392005-09-09 18:23:32 -0700269 cm_id_priv->av.pkey_index,
Sean Hefty34816ad2005-10-25 10:51:39 -0700270 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
Ira Weinyda2dfaa2015-06-06 14:38:28 -0400271 GFP_ATOMIC,
272 IB_MGMT_BASE_VERSION);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700273 if (IS_ERR(m)) {
274 ib_destroy_ah(ah);
275 return PTR_ERR(m);
276 }
277
278 /* Timeout set by caller if response is expected. */
Sean Hefty34816ad2005-10-25 10:51:39 -0700279 m->ah = ah;
280 m->retries = cm_id_priv->max_cm_retries;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700281
282 atomic_inc(&cm_id_priv->refcount);
283 m->context[0] = cm_id_priv;
284 *msg = m;
285 return 0;
286}
287
288static int cm_alloc_response_msg(struct cm_port *port,
289 struct ib_mad_recv_wc *mad_recv_wc,
290 struct ib_mad_send_buf **msg)
291{
292 struct ib_mad_send_buf *m;
293 struct ib_ah *ah;
294
295 ah = ib_create_ah_from_wc(port->mad_agent->qp->pd, mad_recv_wc->wc,
296 mad_recv_wc->recv_buf.grh, port->port_num);
297 if (IS_ERR(ah))
298 return PTR_ERR(ah);
299
300 m = ib_create_send_mad(port->mad_agent, 1, mad_recv_wc->wc->pkey_index,
Sean Hefty34816ad2005-10-25 10:51:39 -0700301 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
Ira Weinyda2dfaa2015-06-06 14:38:28 -0400302 GFP_ATOMIC,
303 IB_MGMT_BASE_VERSION);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700304 if (IS_ERR(m)) {
305 ib_destroy_ah(ah);
306 return PTR_ERR(m);
307 }
Sean Hefty34816ad2005-10-25 10:51:39 -0700308 m->ah = ah;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700309 *msg = m;
310 return 0;
311}
312
313static void cm_free_msg(struct ib_mad_send_buf *msg)
314{
Sean Hefty34816ad2005-10-25 10:51:39 -0700315 ib_destroy_ah(msg->ah);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700316 if (msg->context[0])
317 cm_deref_id(msg->context[0]);
318 ib_free_send_mad(msg);
319}
320
321static void * cm_copy_private_data(const void *private_data,
322 u8 private_data_len)
323{
324 void *data;
325
326 if (!private_data || !private_data_len)
327 return NULL;
328
Eric Sesterhennbed8bdf2006-10-23 22:17:21 +0200329 data = kmemdup(private_data, private_data_len, GFP_KERNEL);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700330 if (!data)
331 return ERR_PTR(-ENOMEM);
332
Hal Rosenstocka9770492005-07-27 11:45:40 -0700333 return data;
334}
335
336static void cm_set_private_data(struct cm_id_private *cm_id_priv,
337 void *private_data, u8 private_data_len)
338{
339 if (cm_id_priv->private_data && cm_id_priv->private_data_len)
340 kfree(cm_id_priv->private_data);
341
342 cm_id_priv->private_data = private_data;
343 cm_id_priv->private_data_len = private_data_len;
344}
345
Sean Heftyca222c62006-06-17 20:37:40 -0700346static void cm_init_av_for_response(struct cm_port *port, struct ib_wc *wc,
347 struct ib_grh *grh, struct cm_av *av)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700348{
349 av->port = port;
350 av->pkey_index = wc->pkey_index;
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +0300351 ib_init_ah_from_wc(port->cm_dev->ib_device, port->port_num, wc,
Sean Heftyca222c62006-06-17 20:37:40 -0700352 grh, &av->ah_attr);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700353}
354
355static int cm_init_av_by_path(struct ib_sa_path_rec *path, struct cm_av *av)
356{
357 struct cm_device *cm_dev;
358 struct cm_port *port = NULL;
359 unsigned long flags;
360 int ret;
361 u8 p;
362
363 read_lock_irqsave(&cm.device_lock, flags);
364 list_for_each_entry(cm_dev, &cm.device_list, list) {
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +0300365 if (!ib_find_cached_gid(cm_dev->ib_device, &path->sgid,
Hal Rosenstocka9770492005-07-27 11:45:40 -0700366 &p, NULL)) {
Sean Hefty9af57b72007-07-16 21:49:35 -0700367 port = cm_dev->port[p-1];
Hal Rosenstocka9770492005-07-27 11:45:40 -0700368 break;
369 }
370 }
371 read_unlock_irqrestore(&cm.device_lock, flags);
372
373 if (!port)
374 return -EINVAL;
375
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +0300376 ret = ib_find_cached_pkey(cm_dev->ib_device, port->port_num,
Hal Rosenstocka9770492005-07-27 11:45:40 -0700377 be16_to_cpu(path->pkey), &av->pkey_index);
378 if (ret)
379 return ret;
380
381 av->port = port;
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +0300382 ib_init_ah_from_path(cm_dev->ib_device, port->port_num, path,
Sean Heftyca222c62006-06-17 20:37:40 -0700383 &av->ah_attr);
Sean Hefty1d846122007-06-18 11:09:37 -0700384 av->timeout = path->packet_life_time + 1;
Matan Barakdd5f03b2013-12-12 18:03:11 +0200385 memcpy(av->smac, path->smac, sizeof(av->smac));
386
387 av->valid = 1;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700388 return 0;
389}
390
391static int cm_alloc_id(struct cm_id_private *cm_id_priv)
392{
393 unsigned long flags;
Tejun Heo3b069c52013-02-27 17:04:16 -0800394 int id;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700395
Tejun Heo3b069c52013-02-27 17:04:16 -0800396 idr_preload(GFP_KERNEL);
397 spin_lock_irqsave(&cm.lock, flags);
398
Zhao Hongjiangab626d12013-11-15 14:16:37 -0800399 id = idr_alloc_cyclic(&cm.local_id_table, cm_id_priv, 0, 0, GFP_NOWAIT);
Tejun Heo3b069c52013-02-27 17:04:16 -0800400
401 spin_unlock_irqrestore(&cm.lock, flags);
402 idr_preload_end();
Sean Heftyf06d2652006-08-28 15:15:18 -0700403
Al Viro1b90c132008-03-29 03:10:28 +0000404 cm_id_priv->id.local_id = (__force __be32)id ^ cm.random_id_operand;
Tejun Heo3b069c52013-02-27 17:04:16 -0800405 return id < 0 ? id : 0;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700406}
407
Sean Hefty97f52eb2005-08-13 21:05:57 -0700408static void cm_free_id(__be32 local_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700409{
Sean Hefty24be6e82007-06-18 11:09:36 -0700410 spin_lock_irq(&cm.lock);
Sean Heftyf06d2652006-08-28 15:15:18 -0700411 idr_remove(&cm.local_id_table,
412 (__force int) (local_id ^ cm.random_id_operand));
Sean Hefty24be6e82007-06-18 11:09:36 -0700413 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700414}
415
Sean Hefty97f52eb2005-08-13 21:05:57 -0700416static struct cm_id_private * cm_get_id(__be32 local_id, __be32 remote_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700417{
418 struct cm_id_private *cm_id_priv;
419
Sean Heftyf06d2652006-08-28 15:15:18 -0700420 cm_id_priv = idr_find(&cm.local_id_table,
421 (__force int) (local_id ^ cm.random_id_operand));
Hal Rosenstocka9770492005-07-27 11:45:40 -0700422 if (cm_id_priv) {
423 if (cm_id_priv->id.remote_id == remote_id)
424 atomic_inc(&cm_id_priv->refcount);
425 else
426 cm_id_priv = NULL;
427 }
428
429 return cm_id_priv;
430}
431
Sean Hefty97f52eb2005-08-13 21:05:57 -0700432static struct cm_id_private * cm_acquire_id(__be32 local_id, __be32 remote_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700433{
434 struct cm_id_private *cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700435
Sean Hefty24be6e82007-06-18 11:09:36 -0700436 spin_lock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700437 cm_id_priv = cm_get_id(local_id, remote_id);
Sean Hefty24be6e82007-06-18 11:09:36 -0700438 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700439
440 return cm_id_priv;
441}
442
David Ahern0d0f7382015-05-03 09:48:26 -0400443static void cm_mask_copy(u32 *dst, const u32 *src, const u32 *mask)
Sean Hefty6e61d042006-06-17 20:37:28 -0700444{
445 int i;
446
David Ahern0d0f7382015-05-03 09:48:26 -0400447 for (i = 0; i < IB_CM_COMPARE_SIZE; i++)
448 dst[i] = src[i] & mask[i];
Sean Hefty6e61d042006-06-17 20:37:28 -0700449}
450
451static int cm_compare_data(struct ib_cm_compare_data *src_data,
452 struct ib_cm_compare_data *dst_data)
453{
David Ahern0d0f7382015-05-03 09:48:26 -0400454 u32 src[IB_CM_COMPARE_SIZE];
455 u32 dst[IB_CM_COMPARE_SIZE];
Sean Hefty6e61d042006-06-17 20:37:28 -0700456
457 if (!src_data || !dst_data)
458 return 0;
459
460 cm_mask_copy(src, src_data->data, dst_data->mask);
461 cm_mask_copy(dst, dst_data->data, src_data->mask);
David Ahern0d0f7382015-05-03 09:48:26 -0400462 return memcmp(src, dst, sizeof(src));
Sean Hefty6e61d042006-06-17 20:37:28 -0700463}
464
David Ahern0d0f7382015-05-03 09:48:26 -0400465static int cm_compare_private_data(u32 *private_data,
Sean Hefty6e61d042006-06-17 20:37:28 -0700466 struct ib_cm_compare_data *dst_data)
467{
David Ahern0d0f7382015-05-03 09:48:26 -0400468 u32 src[IB_CM_COMPARE_SIZE];
Sean Hefty6e61d042006-06-17 20:37:28 -0700469
470 if (!dst_data)
471 return 0;
472
473 cm_mask_copy(src, private_data, dst_data->mask);
David Ahern0d0f7382015-05-03 09:48:26 -0400474 return memcmp(src, dst_data->data, sizeof(src));
Sean Hefty6e61d042006-06-17 20:37:28 -0700475}
476
Roland Dreiera88f4882008-04-16 21:01:07 -0700477/*
478 * Trivial helpers to strip endian annotation and compare; the
479 * endianness doesn't actually matter since we just need a stable
480 * order for the RB tree.
481 */
482static int be32_lt(__be32 a, __be32 b)
483{
484 return (__force u32) a < (__force u32) b;
485}
486
487static int be32_gt(__be32 a, __be32 b)
488{
489 return (__force u32) a > (__force u32) b;
490}
491
492static int be64_lt(__be64 a, __be64 b)
493{
494 return (__force u64) a < (__force u64) b;
495}
496
497static int be64_gt(__be64 a, __be64 b)
498{
499 return (__force u64) a > (__force u64) b;
500}
501
Hal Rosenstocka9770492005-07-27 11:45:40 -0700502static struct cm_id_private * cm_insert_listen(struct cm_id_private *cm_id_priv)
503{
504 struct rb_node **link = &cm.listen_service_table.rb_node;
505 struct rb_node *parent = NULL;
506 struct cm_id_private *cur_cm_id_priv;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700507 __be64 service_id = cm_id_priv->id.service_id;
508 __be64 service_mask = cm_id_priv->id.service_mask;
Sean Hefty6e61d042006-06-17 20:37:28 -0700509 int data_cmp;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700510
511 while (*link) {
512 parent = *link;
513 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
514 service_node);
Sean Hefty6e61d042006-06-17 20:37:28 -0700515 data_cmp = cm_compare_data(cm_id_priv->compare_data,
516 cur_cm_id_priv->compare_data);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700517 if ((cur_cm_id_priv->id.service_mask & service_id) ==
Sean Hefty07d357d2005-10-17 15:37:43 -0700518 (service_mask & cur_cm_id_priv->id.service_id) &&
Sean Hefty6e61d042006-06-17 20:37:28 -0700519 (cm_id_priv->id.device == cur_cm_id_priv->id.device) &&
520 !data_cmp)
Sean Hefty07d357d2005-10-17 15:37:43 -0700521 return cur_cm_id_priv;
522
523 if (cm_id_priv->id.device < cur_cm_id_priv->id.device)
524 link = &(*link)->rb_left;
525 else if (cm_id_priv->id.device > cur_cm_id_priv->id.device)
526 link = &(*link)->rb_right;
Roland Dreiera88f4882008-04-16 21:01:07 -0700527 else if (be64_lt(service_id, cur_cm_id_priv->id.service_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700528 link = &(*link)->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700529 else if (be64_gt(service_id, cur_cm_id_priv->id.service_id))
Sean Hefty6e61d042006-06-17 20:37:28 -0700530 link = &(*link)->rb_right;
531 else if (data_cmp < 0)
532 link = &(*link)->rb_left;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700533 else
534 link = &(*link)->rb_right;
535 }
536 rb_link_node(&cm_id_priv->service_node, parent, link);
537 rb_insert_color(&cm_id_priv->service_node, &cm.listen_service_table);
538 return NULL;
539}
540
Sean Hefty07d357d2005-10-17 15:37:43 -0700541static struct cm_id_private * cm_find_listen(struct ib_device *device,
Sean Hefty6e61d042006-06-17 20:37:28 -0700542 __be64 service_id,
David Ahern0d0f7382015-05-03 09:48:26 -0400543 u32 *private_data)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700544{
545 struct rb_node *node = cm.listen_service_table.rb_node;
546 struct cm_id_private *cm_id_priv;
Sean Hefty6e61d042006-06-17 20:37:28 -0700547 int data_cmp;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700548
549 while (node) {
550 cm_id_priv = rb_entry(node, struct cm_id_private, service_node);
Sean Hefty6e61d042006-06-17 20:37:28 -0700551 data_cmp = cm_compare_private_data(private_data,
552 cm_id_priv->compare_data);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700553 if ((cm_id_priv->id.service_mask & service_id) ==
Sean Hefty07d357d2005-10-17 15:37:43 -0700554 cm_id_priv->id.service_id &&
Sean Hefty6e61d042006-06-17 20:37:28 -0700555 (cm_id_priv->id.device == device) && !data_cmp)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700556 return cm_id_priv;
Sean Hefty07d357d2005-10-17 15:37:43 -0700557
558 if (device < cm_id_priv->id.device)
559 node = node->rb_left;
560 else if (device > cm_id_priv->id.device)
561 node = node->rb_right;
Roland Dreiera88f4882008-04-16 21:01:07 -0700562 else if (be64_lt(service_id, cm_id_priv->id.service_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700563 node = node->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700564 else if (be64_gt(service_id, cm_id_priv->id.service_id))
Sean Hefty6e61d042006-06-17 20:37:28 -0700565 node = node->rb_right;
566 else if (data_cmp < 0)
567 node = node->rb_left;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700568 else
569 node = node->rb_right;
570 }
571 return NULL;
572}
573
574static struct cm_timewait_info * cm_insert_remote_id(struct cm_timewait_info
575 *timewait_info)
576{
577 struct rb_node **link = &cm.remote_id_table.rb_node;
578 struct rb_node *parent = NULL;
579 struct cm_timewait_info *cur_timewait_info;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700580 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
581 __be32 remote_id = timewait_info->work.remote_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700582
583 while (*link) {
584 parent = *link;
585 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
586 remote_id_node);
Roland Dreiera88f4882008-04-16 21:01:07 -0700587 if (be32_lt(remote_id, cur_timewait_info->work.remote_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700588 link = &(*link)->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700589 else if (be32_gt(remote_id, cur_timewait_info->work.remote_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700590 link = &(*link)->rb_right;
Roland Dreiera88f4882008-04-16 21:01:07 -0700591 else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700592 link = &(*link)->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700593 else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700594 link = &(*link)->rb_right;
595 else
596 return cur_timewait_info;
597 }
598 timewait_info->inserted_remote_id = 1;
599 rb_link_node(&timewait_info->remote_id_node, parent, link);
600 rb_insert_color(&timewait_info->remote_id_node, &cm.remote_id_table);
601 return NULL;
602}
603
Sean Hefty97f52eb2005-08-13 21:05:57 -0700604static struct cm_timewait_info * cm_find_remote_id(__be64 remote_ca_guid,
605 __be32 remote_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700606{
607 struct rb_node *node = cm.remote_id_table.rb_node;
608 struct cm_timewait_info *timewait_info;
609
610 while (node) {
611 timewait_info = rb_entry(node, struct cm_timewait_info,
612 remote_id_node);
Roland Dreiera88f4882008-04-16 21:01:07 -0700613 if (be32_lt(remote_id, timewait_info->work.remote_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700614 node = node->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700615 else if (be32_gt(remote_id, timewait_info->work.remote_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700616 node = node->rb_right;
Roland Dreiera88f4882008-04-16 21:01:07 -0700617 else if (be64_lt(remote_ca_guid, timewait_info->remote_ca_guid))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700618 node = node->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700619 else if (be64_gt(remote_ca_guid, timewait_info->remote_ca_guid))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700620 node = node->rb_right;
621 else
622 return timewait_info;
623 }
624 return NULL;
625}
626
627static struct cm_timewait_info * cm_insert_remote_qpn(struct cm_timewait_info
628 *timewait_info)
629{
630 struct rb_node **link = &cm.remote_qp_table.rb_node;
631 struct rb_node *parent = NULL;
632 struct cm_timewait_info *cur_timewait_info;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700633 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
634 __be32 remote_qpn = timewait_info->remote_qpn;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700635
636 while (*link) {
637 parent = *link;
638 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
639 remote_qp_node);
Roland Dreiera88f4882008-04-16 21:01:07 -0700640 if (be32_lt(remote_qpn, cur_timewait_info->remote_qpn))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700641 link = &(*link)->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700642 else if (be32_gt(remote_qpn, cur_timewait_info->remote_qpn))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700643 link = &(*link)->rb_right;
Roland Dreiera88f4882008-04-16 21:01:07 -0700644 else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700645 link = &(*link)->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700646 else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700647 link = &(*link)->rb_right;
648 else
649 return cur_timewait_info;
650 }
651 timewait_info->inserted_remote_qp = 1;
652 rb_link_node(&timewait_info->remote_qp_node, parent, link);
653 rb_insert_color(&timewait_info->remote_qp_node, &cm.remote_qp_table);
654 return NULL;
655}
656
657static struct cm_id_private * cm_insert_remote_sidr(struct cm_id_private
658 *cm_id_priv)
659{
660 struct rb_node **link = &cm.remote_sidr_table.rb_node;
661 struct rb_node *parent = NULL;
662 struct cm_id_private *cur_cm_id_priv;
663 union ib_gid *port_gid = &cm_id_priv->av.dgid;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700664 __be32 remote_id = cm_id_priv->id.remote_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700665
666 while (*link) {
667 parent = *link;
668 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
669 sidr_id_node);
Roland Dreiera88f4882008-04-16 21:01:07 -0700670 if (be32_lt(remote_id, cur_cm_id_priv->id.remote_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700671 link = &(*link)->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700672 else if (be32_gt(remote_id, cur_cm_id_priv->id.remote_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700673 link = &(*link)->rb_right;
674 else {
675 int cmp;
676 cmp = memcmp(port_gid, &cur_cm_id_priv->av.dgid,
677 sizeof *port_gid);
678 if (cmp < 0)
679 link = &(*link)->rb_left;
680 else if (cmp > 0)
681 link = &(*link)->rb_right;
682 else
683 return cur_cm_id_priv;
684 }
685 }
686 rb_link_node(&cm_id_priv->sidr_id_node, parent, link);
687 rb_insert_color(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
688 return NULL;
689}
690
691static void cm_reject_sidr_req(struct cm_id_private *cm_id_priv,
692 enum ib_cm_sidr_status status)
693{
694 struct ib_cm_sidr_rep_param param;
695
696 memset(&param, 0, sizeof param);
697 param.status = status;
698 ib_send_cm_sidr_rep(&cm_id_priv->id, &param);
699}
700
Sean Hefty07d357d2005-10-17 15:37:43 -0700701struct ib_cm_id *ib_create_cm_id(struct ib_device *device,
702 ib_cm_handler cm_handler,
Hal Rosenstocka9770492005-07-27 11:45:40 -0700703 void *context)
704{
705 struct cm_id_private *cm_id_priv;
706 int ret;
707
Roland Dreierde6eb662005-11-02 07:23:14 -0800708 cm_id_priv = kzalloc(sizeof *cm_id_priv, GFP_KERNEL);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700709 if (!cm_id_priv)
710 return ERR_PTR(-ENOMEM);
711
Hal Rosenstocka9770492005-07-27 11:45:40 -0700712 cm_id_priv->id.state = IB_CM_IDLE;
Sean Hefty07d357d2005-10-17 15:37:43 -0700713 cm_id_priv->id.device = device;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700714 cm_id_priv->id.cm_handler = cm_handler;
715 cm_id_priv->id.context = context;
John Kingman354ba392005-09-09 18:23:32 -0700716 cm_id_priv->id.remote_cm_qpn = 1;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700717 ret = cm_alloc_id(cm_id_priv);
718 if (ret)
719 goto error;
720
721 spin_lock_init(&cm_id_priv->lock);
Sean Hefty1b52fa982006-05-12 14:57:52 -0700722 init_completion(&cm_id_priv->comp);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700723 INIT_LIST_HEAD(&cm_id_priv->work_list);
724 atomic_set(&cm_id_priv->work_count, -1);
725 atomic_set(&cm_id_priv->refcount, 1);
726 return &cm_id_priv->id;
727
728error:
729 kfree(cm_id_priv);
730 return ERR_PTR(-ENOMEM);
731}
732EXPORT_SYMBOL(ib_create_cm_id);
733
734static struct cm_work * cm_dequeue_work(struct cm_id_private *cm_id_priv)
735{
736 struct cm_work *work;
737
738 if (list_empty(&cm_id_priv->work_list))
739 return NULL;
740
741 work = list_entry(cm_id_priv->work_list.next, struct cm_work, list);
742 list_del(&work->list);
743 return work;
744}
745
746static void cm_free_work(struct cm_work *work)
747{
748 if (work->mad_recv_wc)
749 ib_free_recv_mad(work->mad_recv_wc);
750 kfree(work);
751}
752
753static inline int cm_convert_to_ms(int iba_time)
754{
755 /* approximate conversion to ms from 4.096us x 2^iba_time */
756 return 1 << max(iba_time - 8, 0);
757}
758
Sean Hefty1d846122007-06-18 11:09:37 -0700759/*
760 * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time
761 * Because of how ack_timeout is stored, adding one doubles the timeout.
762 * To avoid large timeouts, select the max(ack_delay, life_time + 1), and
763 * increment it (round up) only if the other is within 50%.
764 */
765static u8 cm_ack_timeout(u8 ca_ack_delay, u8 packet_life_time)
766{
767 int ack_timeout = packet_life_time + 1;
768
769 if (ack_timeout >= ca_ack_delay)
770 ack_timeout += (ca_ack_delay >= (ack_timeout - 1));
771 else
772 ack_timeout = ca_ack_delay +
773 (ack_timeout >= (ca_ack_delay - 1));
774
775 return min(31, ack_timeout);
776}
777
Hal Rosenstocka9770492005-07-27 11:45:40 -0700778static void cm_cleanup_timewait(struct cm_timewait_info *timewait_info)
779{
Hal Rosenstocka9770492005-07-27 11:45:40 -0700780 if (timewait_info->inserted_remote_id) {
781 rb_erase(&timewait_info->remote_id_node, &cm.remote_id_table);
782 timewait_info->inserted_remote_id = 0;
783 }
784
785 if (timewait_info->inserted_remote_qp) {
786 rb_erase(&timewait_info->remote_qp_node, &cm.remote_qp_table);
787 timewait_info->inserted_remote_qp = 0;
788 }
Hal Rosenstocka9770492005-07-27 11:45:40 -0700789}
790
Sean Hefty97f52eb2005-08-13 21:05:57 -0700791static struct cm_timewait_info * cm_create_timewait_info(__be32 local_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700792{
793 struct cm_timewait_info *timewait_info;
794
Roland Dreierde6eb662005-11-02 07:23:14 -0800795 timewait_info = kzalloc(sizeof *timewait_info, GFP_KERNEL);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700796 if (!timewait_info)
797 return ERR_PTR(-ENOMEM);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700798
799 timewait_info->work.local_id = local_id;
David Howellsc4028952006-11-22 14:57:56 +0000800 INIT_DELAYED_WORK(&timewait_info->work.work, cm_work_handler);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700801 timewait_info->work.cm_event.event = IB_CM_TIMEWAIT_EXIT;
802 return timewait_info;
803}
804
805static void cm_enter_timewait(struct cm_id_private *cm_id_priv)
806{
807 int wait_time;
Sean Hefty85753292006-10-04 11:29:59 -0700808 unsigned long flags;
Erez Shitritbe4b4992015-06-25 17:13:22 +0300809 struct cm_device *cm_dev;
810
811 cm_dev = ib_get_client_data(cm_id_priv->id.device, &cm_client);
812 if (!cm_dev)
813 return;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700814
Sean Hefty85753292006-10-04 11:29:59 -0700815 spin_lock_irqsave(&cm.lock, flags);
Michael S. Tsirkina70d0592006-08-28 16:32:50 +0300816 cm_cleanup_timewait(cm_id_priv->timewait_info);
Sean Hefty85753292006-10-04 11:29:59 -0700817 list_add_tail(&cm_id_priv->timewait_info->list, &cm.timewait_list);
818 spin_unlock_irqrestore(&cm.lock, flags);
Michael S. Tsirkina70d0592006-08-28 16:32:50 +0300819
Hal Rosenstocka9770492005-07-27 11:45:40 -0700820 /*
821 * The cm_id could be destroyed by the user before we exit timewait.
822 * To protect against this, we search for the cm_id after exiting
823 * timewait before notifying the user that we've exited timewait.
824 */
825 cm_id_priv->id.state = IB_CM_TIMEWAIT;
Sean Hefty1d846122007-06-18 11:09:37 -0700826 wait_time = cm_convert_to_ms(cm_id_priv->av.timeout);
Erez Shitritbe4b4992015-06-25 17:13:22 +0300827
828 /* Check if the device started its remove_one */
829 spin_lock_irq(&cm.lock);
830 if (!cm_dev->going_down)
831 queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work,
832 msecs_to_jiffies(wait_time));
833 spin_unlock_irq(&cm.lock);
834
Hal Rosenstocka9770492005-07-27 11:45:40 -0700835 cm_id_priv->timewait_info = NULL;
836}
837
838static void cm_reset_to_idle(struct cm_id_private *cm_id_priv)
839{
Sean Hefty85753292006-10-04 11:29:59 -0700840 unsigned long flags;
841
Hal Rosenstocka9770492005-07-27 11:45:40 -0700842 cm_id_priv->id.state = IB_CM_IDLE;
843 if (cm_id_priv->timewait_info) {
Sean Hefty85753292006-10-04 11:29:59 -0700844 spin_lock_irqsave(&cm.lock, flags);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700845 cm_cleanup_timewait(cm_id_priv->timewait_info);
Sean Hefty85753292006-10-04 11:29:59 -0700846 spin_unlock_irqrestore(&cm.lock, flags);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700847 kfree(cm_id_priv->timewait_info);
848 cm_id_priv->timewait_info = NULL;
849 }
850}
851
Michael S. Tsirkin04c33542006-07-14 00:23:52 -0700852static void cm_destroy_id(struct ib_cm_id *cm_id, int err)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700853{
854 struct cm_id_private *cm_id_priv;
855 struct cm_work *work;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700856
857 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
858retest:
Sean Hefty24be6e82007-06-18 11:09:36 -0700859 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700860 switch (cm_id->state) {
861 case IB_CM_LISTEN:
862 cm_id->state = IB_CM_IDLE;
Sean Hefty24be6e82007-06-18 11:09:36 -0700863 spin_unlock_irq(&cm_id_priv->lock);
864 spin_lock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700865 rb_erase(&cm_id_priv->service_node, &cm.listen_service_table);
Sean Hefty24be6e82007-06-18 11:09:36 -0700866 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700867 break;
868 case IB_CM_SIDR_REQ_SENT:
869 cm_id->state = IB_CM_IDLE;
Sean Hefty34816ad2005-10-25 10:51:39 -0700870 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Sean Hefty24be6e82007-06-18 11:09:36 -0700871 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700872 break;
873 case IB_CM_SIDR_REQ_RCVD:
Sean Hefty24be6e82007-06-18 11:09:36 -0700874 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700875 cm_reject_sidr_req(cm_id_priv, IB_SIDR_REJECT);
876 break;
877 case IB_CM_REQ_SENT:
Ted Kimc29ed5a2015-05-14 12:49:01 -0700878 case IB_CM_MRA_REQ_RCVD:
Sean Hefty227eca82005-11-30 10:00:25 -0800879 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Sean Hefty24be6e82007-06-18 11:09:36 -0700880 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty227eca82005-11-30 10:00:25 -0800881 ib_send_cm_rej(cm_id, IB_CM_REJ_TIMEOUT,
Sean Heftye971b8c2007-02-22 11:37:38 -0800882 &cm_id_priv->id.device->node_guid,
883 sizeof cm_id_priv->id.device->node_guid,
Sean Hefty227eca82005-11-30 10:00:25 -0800884 NULL, 0);
885 break;
Michael S. Tsirkin04c33542006-07-14 00:23:52 -0700886 case IB_CM_REQ_RCVD:
887 if (err == -ENOMEM) {
888 /* Do not reject to allow future retries. */
889 cm_reset_to_idle(cm_id_priv);
Sean Hefty24be6e82007-06-18 11:09:36 -0700890 spin_unlock_irq(&cm_id_priv->lock);
Michael S. Tsirkin04c33542006-07-14 00:23:52 -0700891 } else {
Sean Hefty24be6e82007-06-18 11:09:36 -0700892 spin_unlock_irq(&cm_id_priv->lock);
Michael S. Tsirkin04c33542006-07-14 00:23:52 -0700893 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
894 NULL, 0, NULL, 0);
895 }
896 break;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700897 case IB_CM_REP_SENT:
898 case IB_CM_MRA_REP_RCVD:
Sean Hefty34816ad2005-10-25 10:51:39 -0700899 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700900 /* Fall through */
Hal Rosenstocka9770492005-07-27 11:45:40 -0700901 case IB_CM_MRA_REQ_SENT:
902 case IB_CM_REP_RCVD:
903 case IB_CM_MRA_REP_SENT:
Sean Hefty24be6e82007-06-18 11:09:36 -0700904 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty227eca82005-11-30 10:00:25 -0800905 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
906 NULL, 0, NULL, 0);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700907 break;
908 case IB_CM_ESTABLISHED:
Sean Hefty24be6e82007-06-18 11:09:36 -0700909 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty2622e182011-05-30 22:30:46 -0700910 if (cm_id_priv->qp_type == IB_QPT_XRC_TGT)
911 break;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700912 ib_send_cm_dreq(cm_id, NULL, 0);
913 goto retest;
914 case IB_CM_DREQ_SENT:
Sean Hefty34816ad2005-10-25 10:51:39 -0700915 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700916 cm_enter_timewait(cm_id_priv);
Sean Hefty24be6e82007-06-18 11:09:36 -0700917 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700918 break;
919 case IB_CM_DREQ_RCVD:
Sean Hefty24be6e82007-06-18 11:09:36 -0700920 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700921 ib_send_cm_drep(cm_id, NULL, 0);
922 break;
923 default:
Sean Hefty24be6e82007-06-18 11:09:36 -0700924 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700925 break;
926 }
927
928 cm_free_id(cm_id->local_id);
Sean Hefty1b52fa982006-05-12 14:57:52 -0700929 cm_deref_id(cm_id_priv);
930 wait_for_completion(&cm_id_priv->comp);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700931 while ((work = cm_dequeue_work(cm_id_priv)) != NULL)
932 cm_free_work(work);
Sean Hefty6e61d042006-06-17 20:37:28 -0700933 kfree(cm_id_priv->compare_data);
934 kfree(cm_id_priv->private_data);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700935 kfree(cm_id_priv);
936}
Michael S. Tsirkin04c33542006-07-14 00:23:52 -0700937
938void ib_destroy_cm_id(struct ib_cm_id *cm_id)
939{
940 cm_destroy_id(cm_id, 0);
941}
Hal Rosenstocka9770492005-07-27 11:45:40 -0700942EXPORT_SYMBOL(ib_destroy_cm_id);
943
Sean Hefty6e61d042006-06-17 20:37:28 -0700944int ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id, __be64 service_mask,
945 struct ib_cm_compare_data *compare_data)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700946{
947 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
948 unsigned long flags;
949 int ret = 0;
950
Harvey Harrison9c3da092009-01-17 17:11:57 -0800951 service_mask = service_mask ? service_mask : ~cpu_to_be64(0);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700952 service_id &= service_mask;
953 if ((service_id & IB_SERVICE_ID_AGN_MASK) == IB_CM_ASSIGN_SERVICE_ID &&
954 (service_id != IB_CM_ASSIGN_SERVICE_ID))
955 return -EINVAL;
956
957 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
Sean Hefty6e61d042006-06-17 20:37:28 -0700958 if (cm_id->state != IB_CM_IDLE)
959 return -EINVAL;
960
961 if (compare_data) {
962 cm_id_priv->compare_data = kzalloc(sizeof *compare_data,
963 GFP_KERNEL);
964 if (!cm_id_priv->compare_data)
965 return -ENOMEM;
966 cm_mask_copy(cm_id_priv->compare_data->data,
967 compare_data->data, compare_data->mask);
968 memcpy(cm_id_priv->compare_data->mask, compare_data->mask,
David Ahern0d0f7382015-05-03 09:48:26 -0400969 sizeof(compare_data->mask));
Sean Hefty6e61d042006-06-17 20:37:28 -0700970 }
Hal Rosenstocka9770492005-07-27 11:45:40 -0700971
972 cm_id->state = IB_CM_LISTEN;
973
974 spin_lock_irqsave(&cm.lock, flags);
975 if (service_id == IB_CM_ASSIGN_SERVICE_ID) {
Sean Hefty97f52eb2005-08-13 21:05:57 -0700976 cm_id->service_id = cpu_to_be64(cm.listen_service_id++);
Harvey Harrison9c3da092009-01-17 17:11:57 -0800977 cm_id->service_mask = ~cpu_to_be64(0);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700978 } else {
979 cm_id->service_id = service_id;
980 cm_id->service_mask = service_mask;
981 }
982 cur_cm_id_priv = cm_insert_listen(cm_id_priv);
983 spin_unlock_irqrestore(&cm.lock, flags);
984
985 if (cur_cm_id_priv) {
986 cm_id->state = IB_CM_IDLE;
Sean Hefty6e61d042006-06-17 20:37:28 -0700987 kfree(cm_id_priv->compare_data);
988 cm_id_priv->compare_data = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700989 ret = -EBUSY;
990 }
991 return ret;
992}
993EXPORT_SYMBOL(ib_cm_listen);
994
Sean Hefty97f52eb2005-08-13 21:05:57 -0700995static __be64 cm_form_tid(struct cm_id_private *cm_id_priv,
996 enum cm_msg_sequence msg_seq)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700997{
998 u64 hi_tid, low_tid;
999
1000 hi_tid = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32;
Sean Hefty97f52eb2005-08-13 21:05:57 -07001001 low_tid = (u64) ((__force u32)cm_id_priv->id.local_id |
1002 (msg_seq << 30));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001003 return cpu_to_be64(hi_tid | low_tid);
1004}
1005
1006static void cm_format_mad_hdr(struct ib_mad_hdr *hdr,
Sean Hefty97f52eb2005-08-13 21:05:57 -07001007 __be16 attr_id, __be64 tid)
Hal Rosenstocka9770492005-07-27 11:45:40 -07001008{
1009 hdr->base_version = IB_MGMT_BASE_VERSION;
1010 hdr->mgmt_class = IB_MGMT_CLASS_CM;
1011 hdr->class_version = IB_CM_CLASS_VERSION;
1012 hdr->method = IB_MGMT_METHOD_SEND;
1013 hdr->attr_id = attr_id;
1014 hdr->tid = tid;
1015}
1016
1017static void cm_format_req(struct cm_req_msg *req_msg,
1018 struct cm_id_private *cm_id_priv,
1019 struct ib_cm_req_param *param)
1020{
Sean Hefty3971c9f2007-12-10 15:53:25 -08001021 struct ib_sa_path_rec *pri_path = param->primary_path;
1022 struct ib_sa_path_rec *alt_path = param->alternate_path;
1023
Hal Rosenstocka9770492005-07-27 11:45:40 -07001024 cm_format_mad_hdr(&req_msg->hdr, CM_REQ_ATTR_ID,
1025 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_REQ));
1026
1027 req_msg->local_comm_id = cm_id_priv->id.local_id;
1028 req_msg->service_id = param->service_id;
Sean Heftye971b8c2007-02-22 11:37:38 -08001029 req_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001030 cm_req_set_local_qpn(req_msg, cpu_to_be32(param->qp_num));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001031 cm_req_set_init_depth(req_msg, param->initiator_depth);
1032 cm_req_set_remote_resp_timeout(req_msg,
1033 param->remote_cm_response_timeout);
1034 cm_req_set_qp_type(req_msg, param->qp_type);
1035 cm_req_set_flow_ctrl(req_msg, param->flow_control);
1036 cm_req_set_starting_psn(req_msg, cpu_to_be32(param->starting_psn));
1037 cm_req_set_local_resp_timeout(req_msg,
1038 param->local_cm_response_timeout);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001039 req_msg->pkey = param->primary_path->pkey;
1040 cm_req_set_path_mtu(req_msg, param->primary_path->mtu);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001041 cm_req_set_max_cm_retries(req_msg, param->max_cm_retries);
Sean Heftyd26a3602011-05-13 10:46:20 -07001042
1043 if (param->qp_type != IB_QPT_XRC_INI) {
1044 cm_req_set_resp_res(req_msg, param->responder_resources);
1045 cm_req_set_retry_count(req_msg, param->retry_count);
1046 cm_req_set_rnr_retry_count(req_msg, param->rnr_retry_count);
1047 cm_req_set_srq(req_msg, param->srq);
1048 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07001049
Sean Hefty3971c9f2007-12-10 15:53:25 -08001050 if (pri_path->hop_limit <= 1) {
1051 req_msg->primary_local_lid = pri_path->slid;
1052 req_msg->primary_remote_lid = pri_path->dlid;
1053 } else {
1054 /* Work-around until there's a way to obtain remote LID info */
1055 req_msg->primary_local_lid = IB_LID_PERMISSIVE;
1056 req_msg->primary_remote_lid = IB_LID_PERMISSIVE;
1057 }
1058 req_msg->primary_local_gid = pri_path->sgid;
1059 req_msg->primary_remote_gid = pri_path->dgid;
1060 cm_req_set_primary_flow_label(req_msg, pri_path->flow_label);
1061 cm_req_set_primary_packet_rate(req_msg, pri_path->rate);
1062 req_msg->primary_traffic_class = pri_path->traffic_class;
1063 req_msg->primary_hop_limit = pri_path->hop_limit;
1064 cm_req_set_primary_sl(req_msg, pri_path->sl);
1065 cm_req_set_primary_subnet_local(req_msg, (pri_path->hop_limit <= 1));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001066 cm_req_set_primary_local_ack_timeout(req_msg,
Sean Hefty1d846122007-06-18 11:09:37 -07001067 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
Sean Hefty3971c9f2007-12-10 15:53:25 -08001068 pri_path->packet_life_time));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001069
Sean Hefty3971c9f2007-12-10 15:53:25 -08001070 if (alt_path) {
1071 if (alt_path->hop_limit <= 1) {
1072 req_msg->alt_local_lid = alt_path->slid;
1073 req_msg->alt_remote_lid = alt_path->dlid;
1074 } else {
1075 req_msg->alt_local_lid = IB_LID_PERMISSIVE;
1076 req_msg->alt_remote_lid = IB_LID_PERMISSIVE;
1077 }
1078 req_msg->alt_local_gid = alt_path->sgid;
1079 req_msg->alt_remote_gid = alt_path->dgid;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001080 cm_req_set_alt_flow_label(req_msg,
Sean Hefty3971c9f2007-12-10 15:53:25 -08001081 alt_path->flow_label);
1082 cm_req_set_alt_packet_rate(req_msg, alt_path->rate);
1083 req_msg->alt_traffic_class = alt_path->traffic_class;
1084 req_msg->alt_hop_limit = alt_path->hop_limit;
1085 cm_req_set_alt_sl(req_msg, alt_path->sl);
1086 cm_req_set_alt_subnet_local(req_msg, (alt_path->hop_limit <= 1));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001087 cm_req_set_alt_local_ack_timeout(req_msg,
Sean Hefty1d846122007-06-18 11:09:37 -07001088 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
Sean Hefty3971c9f2007-12-10 15:53:25 -08001089 alt_path->packet_life_time));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001090 }
1091
1092 if (param->private_data && param->private_data_len)
1093 memcpy(req_msg->private_data, param->private_data,
1094 param->private_data_len);
1095}
1096
Arjan van de Ven858119e2006-01-14 13:20:43 -08001097static int cm_validate_req_param(struct ib_cm_req_param *param)
Hal Rosenstocka9770492005-07-27 11:45:40 -07001098{
1099 /* peer-to-peer not supported */
1100 if (param->peer_to_peer)
1101 return -EINVAL;
1102
1103 if (!param->primary_path)
1104 return -EINVAL;
1105
Sean Heftyd26a3602011-05-13 10:46:20 -07001106 if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC &&
1107 param->qp_type != IB_QPT_XRC_INI)
Hal Rosenstocka9770492005-07-27 11:45:40 -07001108 return -EINVAL;
1109
1110 if (param->private_data &&
1111 param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE)
1112 return -EINVAL;
1113
1114 if (param->alternate_path &&
1115 (param->alternate_path->pkey != param->primary_path->pkey ||
1116 param->alternate_path->mtu != param->primary_path->mtu))
1117 return -EINVAL;
1118
1119 return 0;
1120}
1121
1122int ib_send_cm_req(struct ib_cm_id *cm_id,
1123 struct ib_cm_req_param *param)
1124{
1125 struct cm_id_private *cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001126 struct cm_req_msg *req_msg;
1127 unsigned long flags;
1128 int ret;
1129
1130 ret = cm_validate_req_param(param);
1131 if (ret)
1132 return ret;
1133
1134 /* Verify that we're not in timewait. */
1135 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1136 spin_lock_irqsave(&cm_id_priv->lock, flags);
1137 if (cm_id->state != IB_CM_IDLE) {
1138 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1139 ret = -EINVAL;
1140 goto out;
1141 }
1142 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1143
1144 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1145 id.local_id);
Sean Hefty75df23e2006-07-25 19:52:01 +03001146 if (IS_ERR(cm_id_priv->timewait_info)) {
1147 ret = PTR_ERR(cm_id_priv->timewait_info);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001148 goto out;
Sean Hefty75df23e2006-07-25 19:52:01 +03001149 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07001150
1151 ret = cm_init_av_by_path(param->primary_path, &cm_id_priv->av);
1152 if (ret)
1153 goto error1;
1154 if (param->alternate_path) {
1155 ret = cm_init_av_by_path(param->alternate_path,
1156 &cm_id_priv->alt_av);
1157 if (ret)
1158 goto error1;
1159 }
1160 cm_id->service_id = param->service_id;
Harvey Harrison9c3da092009-01-17 17:11:57 -08001161 cm_id->service_mask = ~cpu_to_be64(0);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001162 cm_id_priv->timeout_ms = cm_convert_to_ms(
1163 param->primary_path->packet_life_time) * 2 +
1164 cm_convert_to_ms(
1165 param->remote_cm_response_timeout);
1166 cm_id_priv->max_cm_retries = param->max_cm_retries;
1167 cm_id_priv->initiator_depth = param->initiator_depth;
1168 cm_id_priv->responder_resources = param->responder_resources;
1169 cm_id_priv->retry_count = param->retry_count;
1170 cm_id_priv->path_mtu = param->primary_path->mtu;
Sean Heftye1444b52006-11-28 14:57:13 -08001171 cm_id_priv->pkey = param->primary_path->pkey;
Sean Heftyae7971a2005-10-24 12:33:56 -07001172 cm_id_priv->qp_type = param->qp_type;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001173
1174 ret = cm_alloc_msg(cm_id_priv, &cm_id_priv->msg);
1175 if (ret)
1176 goto error1;
1177
1178 req_msg = (struct cm_req_msg *) cm_id_priv->msg->mad;
1179 cm_format_req(req_msg, cm_id_priv, param);
1180 cm_id_priv->tid = req_msg->hdr.tid;
Sean Hefty34816ad2005-10-25 10:51:39 -07001181 cm_id_priv->msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001182 cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT;
1183
1184 cm_id_priv->local_qpn = cm_req_get_local_qpn(req_msg);
1185 cm_id_priv->rq_psn = cm_req_get_starting_psn(req_msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001186
1187 spin_lock_irqsave(&cm_id_priv->lock, flags);
Sean Hefty34816ad2005-10-25 10:51:39 -07001188 ret = ib_post_send_mad(cm_id_priv->msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001189 if (ret) {
1190 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1191 goto error2;
1192 }
1193 BUG_ON(cm_id->state != IB_CM_IDLE);
1194 cm_id->state = IB_CM_REQ_SENT;
1195 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1196 return 0;
1197
1198error2: cm_free_msg(cm_id_priv->msg);
1199error1: kfree(cm_id_priv->timewait_info);
1200out: return ret;
1201}
1202EXPORT_SYMBOL(ib_send_cm_req);
1203
1204static int cm_issue_rej(struct cm_port *port,
1205 struct ib_mad_recv_wc *mad_recv_wc,
1206 enum ib_cm_rej_reason reason,
1207 enum cm_msg_response msg_rejected,
1208 void *ari, u8 ari_length)
1209{
1210 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001211 struct cm_rej_msg *rej_msg, *rcv_msg;
1212 int ret;
1213
1214 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
1215 if (ret)
1216 return ret;
1217
1218 /* We just need common CM header information. Cast to any message. */
1219 rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad;
1220 rej_msg = (struct cm_rej_msg *) msg->mad;
1221
1222 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid);
1223 rej_msg->remote_comm_id = rcv_msg->local_comm_id;
1224 rej_msg->local_comm_id = rcv_msg->remote_comm_id;
1225 cm_rej_set_msg_rejected(rej_msg, msg_rejected);
Sean Hefty97f52eb2005-08-13 21:05:57 -07001226 rej_msg->reason = cpu_to_be16(reason);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001227
1228 if (ari && ari_length) {
1229 cm_rej_set_reject_info_len(rej_msg, ari_length);
1230 memcpy(rej_msg->ari, ari, ari_length);
1231 }
1232
Sean Hefty34816ad2005-10-25 10:51:39 -07001233 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001234 if (ret)
1235 cm_free_msg(msg);
1236
1237 return ret;
1238}
1239
Sean Hefty97f52eb2005-08-13 21:05:57 -07001240static inline int cm_is_active_peer(__be64 local_ca_guid, __be64 remote_ca_guid,
1241 __be32 local_qpn, __be32 remote_qpn)
Hal Rosenstocka9770492005-07-27 11:45:40 -07001242{
1243 return (be64_to_cpu(local_ca_guid) > be64_to_cpu(remote_ca_guid) ||
1244 ((local_ca_guid == remote_ca_guid) &&
1245 (be32_to_cpu(local_qpn) > be32_to_cpu(remote_qpn))));
1246}
1247
Arjan van de Ven858119e2006-01-14 13:20:43 -08001248static void cm_format_paths_from_req(struct cm_req_msg *req_msg,
Hal Rosenstocka9770492005-07-27 11:45:40 -07001249 struct ib_sa_path_rec *primary_path,
1250 struct ib_sa_path_rec *alt_path)
1251{
1252 memset(primary_path, 0, sizeof *primary_path);
1253 primary_path->dgid = req_msg->primary_local_gid;
1254 primary_path->sgid = req_msg->primary_remote_gid;
1255 primary_path->dlid = req_msg->primary_local_lid;
1256 primary_path->slid = req_msg->primary_remote_lid;
1257 primary_path->flow_label = cm_req_get_primary_flow_label(req_msg);
1258 primary_path->hop_limit = req_msg->primary_hop_limit;
1259 primary_path->traffic_class = req_msg->primary_traffic_class;
1260 primary_path->reversible = 1;
1261 primary_path->pkey = req_msg->pkey;
1262 primary_path->sl = cm_req_get_primary_sl(req_msg);
1263 primary_path->mtu_selector = IB_SA_EQ;
1264 primary_path->mtu = cm_req_get_path_mtu(req_msg);
1265 primary_path->rate_selector = IB_SA_EQ;
1266 primary_path->rate = cm_req_get_primary_packet_rate(req_msg);
1267 primary_path->packet_life_time_selector = IB_SA_EQ;
1268 primary_path->packet_life_time =
1269 cm_req_get_primary_local_ack_timeout(req_msg);
1270 primary_path->packet_life_time -= (primary_path->packet_life_time > 0);
Haggai Eran15865e7d2015-07-30 17:50:17 +03001271 primary_path->service_id = req_msg->service_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001272
1273 if (req_msg->alt_local_lid) {
1274 memset(alt_path, 0, sizeof *alt_path);
1275 alt_path->dgid = req_msg->alt_local_gid;
1276 alt_path->sgid = req_msg->alt_remote_gid;
1277 alt_path->dlid = req_msg->alt_local_lid;
1278 alt_path->slid = req_msg->alt_remote_lid;
1279 alt_path->flow_label = cm_req_get_alt_flow_label(req_msg);
1280 alt_path->hop_limit = req_msg->alt_hop_limit;
1281 alt_path->traffic_class = req_msg->alt_traffic_class;
1282 alt_path->reversible = 1;
1283 alt_path->pkey = req_msg->pkey;
1284 alt_path->sl = cm_req_get_alt_sl(req_msg);
1285 alt_path->mtu_selector = IB_SA_EQ;
1286 alt_path->mtu = cm_req_get_path_mtu(req_msg);
1287 alt_path->rate_selector = IB_SA_EQ;
1288 alt_path->rate = cm_req_get_alt_packet_rate(req_msg);
1289 alt_path->packet_life_time_selector = IB_SA_EQ;
1290 alt_path->packet_life_time =
1291 cm_req_get_alt_local_ack_timeout(req_msg);
1292 alt_path->packet_life_time -= (alt_path->packet_life_time > 0);
Haggai Eran15865e7d2015-07-30 17:50:17 +03001293 alt_path->service_id = req_msg->service_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001294 }
1295}
1296
1297static void cm_format_req_event(struct cm_work *work,
1298 struct cm_id_private *cm_id_priv,
1299 struct ib_cm_id *listen_id)
1300{
1301 struct cm_req_msg *req_msg;
1302 struct ib_cm_req_event_param *param;
1303
1304 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1305 param = &work->cm_event.param.req_rcvd;
1306 param->listen_id = listen_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001307 param->port = cm_id_priv->av.port->port_num;
1308 param->primary_path = &work->path[0];
1309 if (req_msg->alt_local_lid)
1310 param->alternate_path = &work->path[1];
1311 else
1312 param->alternate_path = NULL;
1313 param->remote_ca_guid = req_msg->local_ca_guid;
1314 param->remote_qkey = be32_to_cpu(req_msg->local_qkey);
1315 param->remote_qpn = be32_to_cpu(cm_req_get_local_qpn(req_msg));
1316 param->qp_type = cm_req_get_qp_type(req_msg);
1317 param->starting_psn = be32_to_cpu(cm_req_get_starting_psn(req_msg));
1318 param->responder_resources = cm_req_get_init_depth(req_msg);
1319 param->initiator_depth = cm_req_get_resp_res(req_msg);
1320 param->local_cm_response_timeout =
1321 cm_req_get_remote_resp_timeout(req_msg);
1322 param->flow_control = cm_req_get_flow_ctrl(req_msg);
1323 param->remote_cm_response_timeout =
1324 cm_req_get_local_resp_timeout(req_msg);
1325 param->retry_count = cm_req_get_retry_count(req_msg);
1326 param->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1327 param->srq = cm_req_get_srq(req_msg);
1328 work->cm_event.private_data = &req_msg->private_data;
1329}
1330
1331static void cm_process_work(struct cm_id_private *cm_id_priv,
1332 struct cm_work *work)
1333{
Hal Rosenstocka9770492005-07-27 11:45:40 -07001334 int ret;
1335
1336 /* We will typically only have the current event to report. */
1337 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
1338 cm_free_work(work);
1339
1340 while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) {
Sean Hefty24be6e82007-06-18 11:09:36 -07001341 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001342 work = cm_dequeue_work(cm_id_priv);
Sean Hefty24be6e82007-06-18 11:09:36 -07001343 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001344 BUG_ON(!work);
1345 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id,
1346 &work->cm_event);
1347 cm_free_work(work);
1348 }
1349 cm_deref_id(cm_id_priv);
1350 if (ret)
Michael S. Tsirkin04c33542006-07-14 00:23:52 -07001351 cm_destroy_id(&cm_id_priv->id, ret);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001352}
1353
1354static void cm_format_mra(struct cm_mra_msg *mra_msg,
1355 struct cm_id_private *cm_id_priv,
1356 enum cm_msg_response msg_mraed, u8 service_timeout,
1357 const void *private_data, u8 private_data_len)
1358{
1359 cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid);
1360 cm_mra_set_msg_mraed(mra_msg, msg_mraed);
1361 mra_msg->local_comm_id = cm_id_priv->id.local_id;
1362 mra_msg->remote_comm_id = cm_id_priv->id.remote_id;
1363 cm_mra_set_service_timeout(mra_msg, service_timeout);
1364
1365 if (private_data && private_data_len)
1366 memcpy(mra_msg->private_data, private_data, private_data_len);
1367}
1368
1369static void cm_format_rej(struct cm_rej_msg *rej_msg,
1370 struct cm_id_private *cm_id_priv,
1371 enum ib_cm_rej_reason reason,
1372 void *ari,
1373 u8 ari_length,
1374 const void *private_data,
1375 u8 private_data_len)
1376{
1377 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid);
1378 rej_msg->remote_comm_id = cm_id_priv->id.remote_id;
1379
1380 switch(cm_id_priv->id.state) {
1381 case IB_CM_REQ_RCVD:
1382 rej_msg->local_comm_id = 0;
1383 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1384 break;
1385 case IB_CM_MRA_REQ_SENT:
1386 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1387 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1388 break;
1389 case IB_CM_REP_RCVD:
1390 case IB_CM_MRA_REP_SENT:
1391 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1392 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REP);
1393 break;
1394 default:
1395 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1396 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_OTHER);
1397 break;
1398 }
1399
Sean Hefty97f52eb2005-08-13 21:05:57 -07001400 rej_msg->reason = cpu_to_be16(reason);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001401 if (ari && ari_length) {
1402 cm_rej_set_reject_info_len(rej_msg, ari_length);
1403 memcpy(rej_msg->ari, ari, ari_length);
1404 }
1405
1406 if (private_data && private_data_len)
1407 memcpy(rej_msg->private_data, private_data, private_data_len);
1408}
1409
1410static void cm_dup_req_handler(struct cm_work *work,
1411 struct cm_id_private *cm_id_priv)
1412{
1413 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001414 int ret;
1415
Sean Hefty9af57b72007-07-16 21:49:35 -07001416 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1417 counter[CM_REQ_COUNTER]);
1418
Hal Rosenstocka9770492005-07-27 11:45:40 -07001419 /* Quick state check to discard duplicate REQs. */
1420 if (cm_id_priv->id.state == IB_CM_REQ_RCVD)
1421 return;
1422
1423 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1424 if (ret)
1425 return;
1426
Sean Hefty24be6e82007-06-18 11:09:36 -07001427 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001428 switch (cm_id_priv->id.state) {
1429 case IB_CM_MRA_REQ_SENT:
1430 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1431 CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout,
1432 cm_id_priv->private_data,
1433 cm_id_priv->private_data_len);
1434 break;
1435 case IB_CM_TIMEWAIT:
1436 cm_format_rej((struct cm_rej_msg *) msg->mad, cm_id_priv,
1437 IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0);
1438 break;
1439 default:
1440 goto unlock;
1441 }
Sean Hefty24be6e82007-06-18 11:09:36 -07001442 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001443
Sean Hefty34816ad2005-10-25 10:51:39 -07001444 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001445 if (ret)
1446 goto free;
1447 return;
1448
Sean Hefty24be6e82007-06-18 11:09:36 -07001449unlock: spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001450free: cm_free_msg(msg);
1451}
1452
1453static struct cm_id_private * cm_match_req(struct cm_work *work,
1454 struct cm_id_private *cm_id_priv)
1455{
1456 struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv;
1457 struct cm_timewait_info *timewait_info;
1458 struct cm_req_msg *req_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001459
1460 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1461
Sean Heftyd998ccc2007-05-21 17:38:02 -07001462 /* Check for possible duplicate REQ. */
Sean Hefty24be6e82007-06-18 11:09:36 -07001463 spin_lock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001464 timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001465 if (timewait_info) {
1466 cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
1467 timewait_info->work.remote_id);
Sean Hefty24be6e82007-06-18 11:09:36 -07001468 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001469 if (cur_cm_id_priv) {
1470 cm_dup_req_handler(work, cur_cm_id_priv);
1471 cm_deref_id(cur_cm_id_priv);
Sean Heftyd998ccc2007-05-21 17:38:02 -07001472 }
1473 return NULL;
1474 }
1475
1476 /* Check for stale connections. */
1477 timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
1478 if (timewait_info) {
1479 cm_cleanup_timewait(cm_id_priv->timewait_info);
Sean Hefty24be6e82007-06-18 11:09:36 -07001480 spin_unlock_irq(&cm.lock);
Sean Heftyd998ccc2007-05-21 17:38:02 -07001481 cm_issue_rej(work->port, work->mad_recv_wc,
1482 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ,
1483 NULL, 0);
1484 return NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001485 }
1486
1487 /* Find matching listen request. */
Sean Hefty07d357d2005-10-17 15:37:43 -07001488 listen_cm_id_priv = cm_find_listen(cm_id_priv->id.device,
Sean Hefty6e61d042006-06-17 20:37:28 -07001489 req_msg->service_id,
1490 req_msg->private_data);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001491 if (!listen_cm_id_priv) {
Sean Hefty85753292006-10-04 11:29:59 -07001492 cm_cleanup_timewait(cm_id_priv->timewait_info);
Sean Hefty24be6e82007-06-18 11:09:36 -07001493 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001494 cm_issue_rej(work->port, work->mad_recv_wc,
1495 IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ,
1496 NULL, 0);
Sean Hefty85753292006-10-04 11:29:59 -07001497 goto out;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001498 }
1499 atomic_inc(&listen_cm_id_priv->refcount);
1500 atomic_inc(&cm_id_priv->refcount);
1501 cm_id_priv->id.state = IB_CM_REQ_RCVD;
1502 atomic_inc(&cm_id_priv->work_count);
Sean Hefty24be6e82007-06-18 11:09:36 -07001503 spin_unlock_irq(&cm.lock);
Sean Hefty85753292006-10-04 11:29:59 -07001504out:
Hal Rosenstocka9770492005-07-27 11:45:40 -07001505 return listen_cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001506}
1507
Sean Hefty3971c9f2007-12-10 15:53:25 -08001508/*
1509 * Work-around for inter-subnet connections. If the LIDs are permissive,
1510 * we need to override the LID/SL data in the REQ with the LID information
1511 * in the work completion.
1512 */
1513static void cm_process_routed_req(struct cm_req_msg *req_msg, struct ib_wc *wc)
1514{
1515 if (!cm_req_get_primary_subnet_local(req_msg)) {
1516 if (req_msg->primary_local_lid == IB_LID_PERMISSIVE) {
1517 req_msg->primary_local_lid = cpu_to_be16(wc->slid);
1518 cm_req_set_primary_sl(req_msg, wc->sl);
1519 }
1520
1521 if (req_msg->primary_remote_lid == IB_LID_PERMISSIVE)
1522 req_msg->primary_remote_lid = cpu_to_be16(wc->dlid_path_bits);
1523 }
1524
1525 if (!cm_req_get_alt_subnet_local(req_msg)) {
1526 if (req_msg->alt_local_lid == IB_LID_PERMISSIVE) {
1527 req_msg->alt_local_lid = cpu_to_be16(wc->slid);
1528 cm_req_set_alt_sl(req_msg, wc->sl);
1529 }
1530
1531 if (req_msg->alt_remote_lid == IB_LID_PERMISSIVE)
1532 req_msg->alt_remote_lid = cpu_to_be16(wc->dlid_path_bits);
1533 }
1534}
1535
Hal Rosenstocka9770492005-07-27 11:45:40 -07001536static int cm_req_handler(struct cm_work *work)
1537{
1538 struct ib_cm_id *cm_id;
1539 struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
1540 struct cm_req_msg *req_msg;
1541 int ret;
1542
1543 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1544
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03001545 cm_id = ib_create_cm_id(work->port->cm_dev->ib_device, NULL, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001546 if (IS_ERR(cm_id))
1547 return PTR_ERR(cm_id);
1548
1549 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1550 cm_id_priv->id.remote_id = req_msg->local_comm_id;
1551 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
Sean Heftyca222c62006-06-17 20:37:40 -07001552 work->mad_recv_wc->recv_buf.grh,
Hal Rosenstocka9770492005-07-27 11:45:40 -07001553 &cm_id_priv->av);
1554 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1555 id.local_id);
1556 if (IS_ERR(cm_id_priv->timewait_info)) {
1557 ret = PTR_ERR(cm_id_priv->timewait_info);
Sean Hefty76842402006-08-28 11:57:42 -07001558 goto destroy;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001559 }
1560 cm_id_priv->timewait_info->work.remote_id = req_msg->local_comm_id;
1561 cm_id_priv->timewait_info->remote_ca_guid = req_msg->local_ca_guid;
1562 cm_id_priv->timewait_info->remote_qpn = cm_req_get_local_qpn(req_msg);
1563
1564 listen_cm_id_priv = cm_match_req(work, cm_id_priv);
1565 if (!listen_cm_id_priv) {
1566 ret = -EINVAL;
Sean Hefty76842402006-08-28 11:57:42 -07001567 kfree(cm_id_priv->timewait_info);
1568 goto destroy;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001569 }
1570
1571 cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
1572 cm_id_priv->id.context = listen_cm_id_priv->id.context;
1573 cm_id_priv->id.service_id = req_msg->service_id;
Harvey Harrison9c3da092009-01-17 17:11:57 -08001574 cm_id_priv->id.service_mask = ~cpu_to_be64(0);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001575
Sean Hefty3971c9f2007-12-10 15:53:25 -08001576 cm_process_routed_req(req_msg, work->mad_recv_wc->wc);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001577 cm_format_paths_from_req(req_msg, &work->path[0], &work->path[1]);
Matan Barakdd5f03b2013-12-12 18:03:11 +02001578
1579 memcpy(work->path[0].dmac, cm_id_priv->av.ah_attr.dmac, ETH_ALEN);
1580 work->path[0].vlan_id = cm_id_priv->av.ah_attr.vlan_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001581 ret = cm_init_av_by_path(&work->path[0], &cm_id_priv->av);
Sean Hefty76842402006-08-28 11:57:42 -07001582 if (ret) {
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03001583 ib_get_cached_gid(work->port->cm_dev->ib_device,
Sean Hefty76842402006-08-28 11:57:42 -07001584 work->port->port_num, 0, &work->path[0].sgid);
1585 ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_GID,
1586 &work->path[0].sgid, sizeof work->path[0].sgid,
1587 NULL, 0);
1588 goto rejected;
1589 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07001590 if (req_msg->alt_local_lid) {
1591 ret = cm_init_av_by_path(&work->path[1], &cm_id_priv->alt_av);
Sean Hefty76842402006-08-28 11:57:42 -07001592 if (ret) {
1593 ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_ALT_GID,
1594 &work->path[0].sgid,
1595 sizeof work->path[0].sgid, NULL, 0);
1596 goto rejected;
1597 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07001598 }
1599 cm_id_priv->tid = req_msg->hdr.tid;
1600 cm_id_priv->timeout_ms = cm_convert_to_ms(
1601 cm_req_get_local_resp_timeout(req_msg));
1602 cm_id_priv->max_cm_retries = cm_req_get_max_cm_retries(req_msg);
1603 cm_id_priv->remote_qpn = cm_req_get_local_qpn(req_msg);
1604 cm_id_priv->initiator_depth = cm_req_get_resp_res(req_msg);
1605 cm_id_priv->responder_resources = cm_req_get_init_depth(req_msg);
1606 cm_id_priv->path_mtu = cm_req_get_path_mtu(req_msg);
Sean Heftye1444b52006-11-28 14:57:13 -08001607 cm_id_priv->pkey = req_msg->pkey;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001608 cm_id_priv->sq_psn = cm_req_get_starting_psn(req_msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001609 cm_id_priv->retry_count = cm_req_get_retry_count(req_msg);
1610 cm_id_priv->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
Sean Heftyae7971a2005-10-24 12:33:56 -07001611 cm_id_priv->qp_type = cm_req_get_qp_type(req_msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001612
1613 cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
1614 cm_process_work(cm_id_priv, work);
1615 cm_deref_id(listen_cm_id_priv);
1616 return 0;
1617
Sean Hefty76842402006-08-28 11:57:42 -07001618rejected:
1619 atomic_dec(&cm_id_priv->refcount);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001620 cm_deref_id(listen_cm_id_priv);
Sean Hefty76842402006-08-28 11:57:42 -07001621destroy:
1622 ib_destroy_cm_id(cm_id);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001623 return ret;
1624}
1625
1626static void cm_format_rep(struct cm_rep_msg *rep_msg,
1627 struct cm_id_private *cm_id_priv,
1628 struct ib_cm_rep_param *param)
1629{
1630 cm_format_mad_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid);
1631 rep_msg->local_comm_id = cm_id_priv->id.local_id;
1632 rep_msg->remote_comm_id = cm_id_priv->id.remote_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001633 cm_rep_set_starting_psn(rep_msg, cpu_to_be32(param->starting_psn));
1634 rep_msg->resp_resources = param->responder_resources;
Sean Hefty1d846122007-06-18 11:09:37 -07001635 cm_rep_set_target_ack_delay(rep_msg,
1636 cm_id_priv->av.port->cm_dev->ack_delay);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001637 cm_rep_set_failover(rep_msg, param->failover_accepted);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001638 cm_rep_set_rnr_retry_count(rep_msg, param->rnr_retry_count);
Sean Heftye971b8c2007-02-22 11:37:38 -08001639 rep_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001640
Sean Heftyd26a3602011-05-13 10:46:20 -07001641 if (cm_id_priv->qp_type != IB_QPT_XRC_TGT) {
1642 rep_msg->initiator_depth = param->initiator_depth;
1643 cm_rep_set_flow_ctrl(rep_msg, param->flow_control);
1644 cm_rep_set_srq(rep_msg, param->srq);
Sean Heftyef700442011-08-02 11:08:22 -07001645 cm_rep_set_local_qpn(rep_msg, cpu_to_be32(param->qp_num));
Sean Heftyd26a3602011-05-13 10:46:20 -07001646 } else {
1647 cm_rep_set_srq(rep_msg, 1);
Sean Heftyef700442011-08-02 11:08:22 -07001648 cm_rep_set_local_eecn(rep_msg, cpu_to_be32(param->qp_num));
Sean Heftyd26a3602011-05-13 10:46:20 -07001649 }
1650
Hal Rosenstocka9770492005-07-27 11:45:40 -07001651 if (param->private_data && param->private_data_len)
1652 memcpy(rep_msg->private_data, param->private_data,
1653 param->private_data_len);
1654}
1655
1656int ib_send_cm_rep(struct ib_cm_id *cm_id,
1657 struct ib_cm_rep_param *param)
1658{
1659 struct cm_id_private *cm_id_priv;
1660 struct ib_mad_send_buf *msg;
1661 struct cm_rep_msg *rep_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001662 unsigned long flags;
1663 int ret;
1664
1665 if (param->private_data &&
1666 param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE)
1667 return -EINVAL;
1668
1669 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1670 spin_lock_irqsave(&cm_id_priv->lock, flags);
1671 if (cm_id->state != IB_CM_REQ_RCVD &&
1672 cm_id->state != IB_CM_MRA_REQ_SENT) {
1673 ret = -EINVAL;
1674 goto out;
1675 }
1676
1677 ret = cm_alloc_msg(cm_id_priv, &msg);
1678 if (ret)
1679 goto out;
1680
1681 rep_msg = (struct cm_rep_msg *) msg->mad;
1682 cm_format_rep(rep_msg, cm_id_priv, param);
Sean Hefty34816ad2005-10-25 10:51:39 -07001683 msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001684 msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT;
1685
Sean Hefty34816ad2005-10-25 10:51:39 -07001686 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001687 if (ret) {
1688 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1689 cm_free_msg(msg);
1690 return ret;
1691 }
1692
1693 cm_id->state = IB_CM_REP_SENT;
1694 cm_id_priv->msg = msg;
1695 cm_id_priv->initiator_depth = param->initiator_depth;
1696 cm_id_priv->responder_resources = param->responder_resources;
1697 cm_id_priv->rq_psn = cm_rep_get_starting_psn(rep_msg);
Sean Heftyef700442011-08-02 11:08:22 -07001698 cm_id_priv->local_qpn = cpu_to_be32(param->qp_num & 0xFFFFFF);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001699
1700out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1701 return ret;
1702}
1703EXPORT_SYMBOL(ib_send_cm_rep);
1704
1705static void cm_format_rtu(struct cm_rtu_msg *rtu_msg,
1706 struct cm_id_private *cm_id_priv,
1707 const void *private_data,
1708 u8 private_data_len)
1709{
1710 cm_format_mad_hdr(&rtu_msg->hdr, CM_RTU_ATTR_ID, cm_id_priv->tid);
1711 rtu_msg->local_comm_id = cm_id_priv->id.local_id;
1712 rtu_msg->remote_comm_id = cm_id_priv->id.remote_id;
1713
1714 if (private_data && private_data_len)
1715 memcpy(rtu_msg->private_data, private_data, private_data_len);
1716}
1717
1718int ib_send_cm_rtu(struct ib_cm_id *cm_id,
1719 const void *private_data,
1720 u8 private_data_len)
1721{
1722 struct cm_id_private *cm_id_priv;
1723 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001724 unsigned long flags;
1725 void *data;
1726 int ret;
1727
1728 if (private_data && private_data_len > IB_CM_RTU_PRIVATE_DATA_SIZE)
1729 return -EINVAL;
1730
1731 data = cm_copy_private_data(private_data, private_data_len);
1732 if (IS_ERR(data))
1733 return PTR_ERR(data);
1734
1735 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1736 spin_lock_irqsave(&cm_id_priv->lock, flags);
1737 if (cm_id->state != IB_CM_REP_RCVD &&
1738 cm_id->state != IB_CM_MRA_REP_SENT) {
1739 ret = -EINVAL;
1740 goto error;
1741 }
1742
1743 ret = cm_alloc_msg(cm_id_priv, &msg);
1744 if (ret)
1745 goto error;
1746
1747 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1748 private_data, private_data_len);
1749
Sean Hefty34816ad2005-10-25 10:51:39 -07001750 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001751 if (ret) {
1752 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1753 cm_free_msg(msg);
1754 kfree(data);
1755 return ret;
1756 }
1757
1758 cm_id->state = IB_CM_ESTABLISHED;
1759 cm_set_private_data(cm_id_priv, data, private_data_len);
1760 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1761 return 0;
1762
1763error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1764 kfree(data);
1765 return ret;
1766}
1767EXPORT_SYMBOL(ib_send_cm_rtu);
1768
Sean Heftyef700442011-08-02 11:08:22 -07001769static void cm_format_rep_event(struct cm_work *work, enum ib_qp_type qp_type)
Hal Rosenstocka9770492005-07-27 11:45:40 -07001770{
1771 struct cm_rep_msg *rep_msg;
1772 struct ib_cm_rep_event_param *param;
1773
1774 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1775 param = &work->cm_event.param.rep_rcvd;
1776 param->remote_ca_guid = rep_msg->local_ca_guid;
1777 param->remote_qkey = be32_to_cpu(rep_msg->local_qkey);
Sean Heftyef700442011-08-02 11:08:22 -07001778 param->remote_qpn = be32_to_cpu(cm_rep_get_qpn(rep_msg, qp_type));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001779 param->starting_psn = be32_to_cpu(cm_rep_get_starting_psn(rep_msg));
1780 param->responder_resources = rep_msg->initiator_depth;
1781 param->initiator_depth = rep_msg->resp_resources;
1782 param->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1783 param->failover_accepted = cm_rep_get_failover(rep_msg);
1784 param->flow_control = cm_rep_get_flow_ctrl(rep_msg);
1785 param->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1786 param->srq = cm_rep_get_srq(rep_msg);
1787 work->cm_event.private_data = &rep_msg->private_data;
1788}
1789
1790static void cm_dup_rep_handler(struct cm_work *work)
1791{
1792 struct cm_id_private *cm_id_priv;
1793 struct cm_rep_msg *rep_msg;
1794 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001795 int ret;
1796
1797 rep_msg = (struct cm_rep_msg *) work->mad_recv_wc->recv_buf.mad;
1798 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id,
1799 rep_msg->local_comm_id);
1800 if (!cm_id_priv)
1801 return;
1802
Sean Hefty9af57b72007-07-16 21:49:35 -07001803 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1804 counter[CM_REP_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001805 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1806 if (ret)
1807 goto deref;
1808
Sean Hefty24be6e82007-06-18 11:09:36 -07001809 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001810 if (cm_id_priv->id.state == IB_CM_ESTABLISHED)
1811 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1812 cm_id_priv->private_data,
1813 cm_id_priv->private_data_len);
1814 else if (cm_id_priv->id.state == IB_CM_MRA_REP_SENT)
1815 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1816 CM_MSG_RESPONSE_REP, cm_id_priv->service_timeout,
1817 cm_id_priv->private_data,
1818 cm_id_priv->private_data_len);
1819 else
1820 goto unlock;
Sean Hefty24be6e82007-06-18 11:09:36 -07001821 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001822
Sean Hefty34816ad2005-10-25 10:51:39 -07001823 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001824 if (ret)
1825 goto free;
1826 goto deref;
1827
Sean Hefty24be6e82007-06-18 11:09:36 -07001828unlock: spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001829free: cm_free_msg(msg);
1830deref: cm_deref_id(cm_id_priv);
1831}
1832
1833static int cm_rep_handler(struct cm_work *work)
1834{
1835 struct cm_id_private *cm_id_priv;
1836 struct cm_rep_msg *rep_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001837 int ret;
1838
1839 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1840 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id, 0);
1841 if (!cm_id_priv) {
1842 cm_dup_rep_handler(work);
1843 return -EINVAL;
1844 }
1845
Sean Heftyef700442011-08-02 11:08:22 -07001846 cm_format_rep_event(work, cm_id_priv->qp_type);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001847
Sean Hefty24be6e82007-06-18 11:09:36 -07001848 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001849 switch (cm_id_priv->id.state) {
1850 case IB_CM_REQ_SENT:
1851 case IB_CM_MRA_REQ_RCVD:
1852 break;
1853 default:
Sean Hefty24be6e82007-06-18 11:09:36 -07001854 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001855 ret = -EINVAL;
1856 goto error;
1857 }
Sean Hefty87fd1a12006-03-02 16:50:37 -08001858
1859 cm_id_priv->timewait_info->work.remote_id = rep_msg->local_comm_id;
1860 cm_id_priv->timewait_info->remote_ca_guid = rep_msg->local_ca_guid;
Sean Heftyef700442011-08-02 11:08:22 -07001861 cm_id_priv->timewait_info->remote_qpn = cm_rep_get_qpn(rep_msg, cm_id_priv->qp_type);
Sean Hefty87fd1a12006-03-02 16:50:37 -08001862
1863 spin_lock(&cm.lock);
1864 /* Check for duplicate REP. */
1865 if (cm_insert_remote_id(cm_id_priv->timewait_info)) {
1866 spin_unlock(&cm.lock);
Sean Hefty24be6e82007-06-18 11:09:36 -07001867 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty87fd1a12006-03-02 16:50:37 -08001868 ret = -EINVAL;
1869 goto error;
1870 }
1871 /* Check for a stale connection. */
1872 if (cm_insert_remote_qpn(cm_id_priv->timewait_info)) {
1873 rb_erase(&cm_id_priv->timewait_info->remote_id_node,
1874 &cm.remote_id_table);
1875 cm_id_priv->timewait_info->inserted_remote_id = 0;
1876 spin_unlock(&cm.lock);
Sean Hefty24be6e82007-06-18 11:09:36 -07001877 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty87fd1a12006-03-02 16:50:37 -08001878 cm_issue_rej(work->port, work->mad_recv_wc,
1879 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP,
1880 NULL, 0);
1881 ret = -EINVAL;
1882 goto error;
1883 }
1884 spin_unlock(&cm.lock);
1885
Hal Rosenstocka9770492005-07-27 11:45:40 -07001886 cm_id_priv->id.state = IB_CM_REP_RCVD;
1887 cm_id_priv->id.remote_id = rep_msg->local_comm_id;
Sean Heftyef700442011-08-02 11:08:22 -07001888 cm_id_priv->remote_qpn = cm_rep_get_qpn(rep_msg, cm_id_priv->qp_type);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001889 cm_id_priv->initiator_depth = rep_msg->resp_resources;
1890 cm_id_priv->responder_resources = rep_msg->initiator_depth;
1891 cm_id_priv->sq_psn = cm_rep_get_starting_psn(rep_msg);
1892 cm_id_priv->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
Sean Hefty1d846122007-06-18 11:09:37 -07001893 cm_id_priv->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1894 cm_id_priv->av.timeout =
1895 cm_ack_timeout(cm_id_priv->target_ack_delay,
1896 cm_id_priv->av.timeout - 1);
1897 cm_id_priv->alt_av.timeout =
1898 cm_ack_timeout(cm_id_priv->target_ack_delay,
1899 cm_id_priv->alt_av.timeout - 1);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001900
1901 /* todo: handle peer_to_peer */
1902
Sean Hefty34816ad2005-10-25 10:51:39 -07001903 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001904 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1905 if (!ret)
1906 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07001907 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001908
1909 if (ret)
1910 cm_process_work(cm_id_priv, work);
1911 else
1912 cm_deref_id(cm_id_priv);
1913 return 0;
1914
Sean Hefty87fd1a12006-03-02 16:50:37 -08001915error:
Hal Rosenstocka9770492005-07-27 11:45:40 -07001916 cm_deref_id(cm_id_priv);
1917 return ret;
1918}
1919
1920static int cm_establish_handler(struct cm_work *work)
1921{
1922 struct cm_id_private *cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001923 int ret;
1924
Sean Heftye1444b52006-11-28 14:57:13 -08001925 /* See comment in cm_establish about lookup. */
Hal Rosenstocka9770492005-07-27 11:45:40 -07001926 cm_id_priv = cm_acquire_id(work->local_id, work->remote_id);
1927 if (!cm_id_priv)
1928 return -EINVAL;
1929
Sean Hefty24be6e82007-06-18 11:09:36 -07001930 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001931 if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
Sean Hefty24be6e82007-06-18 11:09:36 -07001932 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001933 goto out;
1934 }
1935
Sean Hefty34816ad2005-10-25 10:51:39 -07001936 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001937 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1938 if (!ret)
1939 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07001940 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001941
1942 if (ret)
1943 cm_process_work(cm_id_priv, work);
1944 else
1945 cm_deref_id(cm_id_priv);
1946 return 0;
1947out:
1948 cm_deref_id(cm_id_priv);
1949 return -EINVAL;
1950}
1951
1952static int cm_rtu_handler(struct cm_work *work)
1953{
1954 struct cm_id_private *cm_id_priv;
1955 struct cm_rtu_msg *rtu_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001956 int ret;
1957
1958 rtu_msg = (struct cm_rtu_msg *)work->mad_recv_wc->recv_buf.mad;
1959 cm_id_priv = cm_acquire_id(rtu_msg->remote_comm_id,
1960 rtu_msg->local_comm_id);
1961 if (!cm_id_priv)
1962 return -EINVAL;
1963
1964 work->cm_event.private_data = &rtu_msg->private_data;
1965
Sean Hefty24be6e82007-06-18 11:09:36 -07001966 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001967 if (cm_id_priv->id.state != IB_CM_REP_SENT &&
1968 cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) {
Sean Hefty24be6e82007-06-18 11:09:36 -07001969 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty9af57b72007-07-16 21:49:35 -07001970 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1971 counter[CM_RTU_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001972 goto out;
1973 }
1974 cm_id_priv->id.state = IB_CM_ESTABLISHED;
1975
Sean Hefty34816ad2005-10-25 10:51:39 -07001976 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001977 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1978 if (!ret)
1979 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07001980 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001981
1982 if (ret)
1983 cm_process_work(cm_id_priv, work);
1984 else
1985 cm_deref_id(cm_id_priv);
1986 return 0;
1987out:
1988 cm_deref_id(cm_id_priv);
1989 return -EINVAL;
1990}
1991
1992static void cm_format_dreq(struct cm_dreq_msg *dreq_msg,
1993 struct cm_id_private *cm_id_priv,
1994 const void *private_data,
1995 u8 private_data_len)
1996{
1997 cm_format_mad_hdr(&dreq_msg->hdr, CM_DREQ_ATTR_ID,
1998 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_DREQ));
1999 dreq_msg->local_comm_id = cm_id_priv->id.local_id;
2000 dreq_msg->remote_comm_id = cm_id_priv->id.remote_id;
2001 cm_dreq_set_remote_qpn(dreq_msg, cm_id_priv->remote_qpn);
2002
2003 if (private_data && private_data_len)
2004 memcpy(dreq_msg->private_data, private_data, private_data_len);
2005}
2006
2007int ib_send_cm_dreq(struct ib_cm_id *cm_id,
2008 const void *private_data,
2009 u8 private_data_len)
2010{
2011 struct cm_id_private *cm_id_priv;
2012 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002013 unsigned long flags;
2014 int ret;
2015
2016 if (private_data && private_data_len > IB_CM_DREQ_PRIVATE_DATA_SIZE)
2017 return -EINVAL;
2018
2019 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2020 spin_lock_irqsave(&cm_id_priv->lock, flags);
2021 if (cm_id->state != IB_CM_ESTABLISHED) {
2022 ret = -EINVAL;
2023 goto out;
2024 }
2025
Sean Hefty8d8ac862011-03-03 23:31:06 +00002026 if (cm_id->lap_state == IB_CM_LAP_SENT ||
2027 cm_id->lap_state == IB_CM_MRA_LAP_RCVD)
2028 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2029
Hal Rosenstocka9770492005-07-27 11:45:40 -07002030 ret = cm_alloc_msg(cm_id_priv, &msg);
2031 if (ret) {
2032 cm_enter_timewait(cm_id_priv);
2033 goto out;
2034 }
2035
2036 cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv,
2037 private_data, private_data_len);
Sean Hefty34816ad2005-10-25 10:51:39 -07002038 msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002039 msg->context[1] = (void *) (unsigned long) IB_CM_DREQ_SENT;
2040
Sean Hefty34816ad2005-10-25 10:51:39 -07002041 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002042 if (ret) {
2043 cm_enter_timewait(cm_id_priv);
2044 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2045 cm_free_msg(msg);
2046 return ret;
2047 }
2048
2049 cm_id->state = IB_CM_DREQ_SENT;
2050 cm_id_priv->msg = msg;
2051out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2052 return ret;
2053}
2054EXPORT_SYMBOL(ib_send_cm_dreq);
2055
2056static void cm_format_drep(struct cm_drep_msg *drep_msg,
2057 struct cm_id_private *cm_id_priv,
2058 const void *private_data,
2059 u8 private_data_len)
2060{
2061 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, cm_id_priv->tid);
2062 drep_msg->local_comm_id = cm_id_priv->id.local_id;
2063 drep_msg->remote_comm_id = cm_id_priv->id.remote_id;
2064
2065 if (private_data && private_data_len)
2066 memcpy(drep_msg->private_data, private_data, private_data_len);
2067}
2068
2069int ib_send_cm_drep(struct ib_cm_id *cm_id,
2070 const void *private_data,
2071 u8 private_data_len)
2072{
2073 struct cm_id_private *cm_id_priv;
2074 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002075 unsigned long flags;
2076 void *data;
2077 int ret;
2078
2079 if (private_data && private_data_len > IB_CM_DREP_PRIVATE_DATA_SIZE)
2080 return -EINVAL;
2081
2082 data = cm_copy_private_data(private_data, private_data_len);
2083 if (IS_ERR(data))
2084 return PTR_ERR(data);
2085
2086 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2087 spin_lock_irqsave(&cm_id_priv->lock, flags);
2088 if (cm_id->state != IB_CM_DREQ_RCVD) {
2089 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2090 kfree(data);
2091 return -EINVAL;
2092 }
2093
2094 cm_set_private_data(cm_id_priv, data, private_data_len);
2095 cm_enter_timewait(cm_id_priv);
2096
2097 ret = cm_alloc_msg(cm_id_priv, &msg);
2098 if (ret)
2099 goto out;
2100
2101 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
2102 private_data, private_data_len);
2103
Sean Hefty34816ad2005-10-25 10:51:39 -07002104 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002105 if (ret) {
2106 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2107 cm_free_msg(msg);
2108 return ret;
2109 }
2110
2111out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2112 return ret;
2113}
2114EXPORT_SYMBOL(ib_send_cm_drep);
2115
Sean Hefty82a9c162006-10-04 11:37:25 -07002116static int cm_issue_drep(struct cm_port *port,
2117 struct ib_mad_recv_wc *mad_recv_wc)
2118{
2119 struct ib_mad_send_buf *msg = NULL;
2120 struct cm_dreq_msg *dreq_msg;
2121 struct cm_drep_msg *drep_msg;
2122 int ret;
2123
2124 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
2125 if (ret)
2126 return ret;
2127
2128 dreq_msg = (struct cm_dreq_msg *) mad_recv_wc->recv_buf.mad;
2129 drep_msg = (struct cm_drep_msg *) msg->mad;
2130
2131 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, dreq_msg->hdr.tid);
2132 drep_msg->remote_comm_id = dreq_msg->local_comm_id;
2133 drep_msg->local_comm_id = dreq_msg->remote_comm_id;
2134
2135 ret = ib_post_send_mad(msg, NULL);
2136 if (ret)
2137 cm_free_msg(msg);
2138
2139 return ret;
2140}
2141
Hal Rosenstocka9770492005-07-27 11:45:40 -07002142static int cm_dreq_handler(struct cm_work *work)
2143{
2144 struct cm_id_private *cm_id_priv;
2145 struct cm_dreq_msg *dreq_msg;
2146 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002147 int ret;
2148
2149 dreq_msg = (struct cm_dreq_msg *)work->mad_recv_wc->recv_buf.mad;
2150 cm_id_priv = cm_acquire_id(dreq_msg->remote_comm_id,
2151 dreq_msg->local_comm_id);
Sean Hefty82a9c162006-10-04 11:37:25 -07002152 if (!cm_id_priv) {
Sean Hefty9af57b72007-07-16 21:49:35 -07002153 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2154 counter[CM_DREQ_COUNTER]);
Sean Hefty82a9c162006-10-04 11:37:25 -07002155 cm_issue_drep(work->port, work->mad_recv_wc);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002156 return -EINVAL;
Sean Hefty82a9c162006-10-04 11:37:25 -07002157 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07002158
2159 work->cm_event.private_data = &dreq_msg->private_data;
2160
Sean Hefty24be6e82007-06-18 11:09:36 -07002161 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002162 if (cm_id_priv->local_qpn != cm_dreq_get_remote_qpn(dreq_msg))
2163 goto unlock;
2164
2165 switch (cm_id_priv->id.state) {
2166 case IB_CM_REP_SENT:
2167 case IB_CM_DREQ_SENT:
Sean Hefty34816ad2005-10-25 10:51:39 -07002168 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002169 break;
2170 case IB_CM_ESTABLISHED:
Sean Hefty8d8ac862011-03-03 23:31:06 +00002171 if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT ||
2172 cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
2173 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2174 break;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002175 case IB_CM_MRA_REP_RCVD:
2176 break;
2177 case IB_CM_TIMEWAIT:
Sean Hefty9af57b72007-07-16 21:49:35 -07002178 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2179 counter[CM_DREQ_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002180 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2181 goto unlock;
2182
2183 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
2184 cm_id_priv->private_data,
2185 cm_id_priv->private_data_len);
Sean Hefty24be6e82007-06-18 11:09:36 -07002186 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002187
Sean Hefty34816ad2005-10-25 10:51:39 -07002188 if (ib_post_send_mad(msg, NULL))
Hal Rosenstocka9770492005-07-27 11:45:40 -07002189 cm_free_msg(msg);
2190 goto deref;
Sean Hefty9af57b72007-07-16 21:49:35 -07002191 case IB_CM_DREQ_RCVD:
2192 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2193 counter[CM_DREQ_COUNTER]);
2194 goto unlock;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002195 default:
2196 goto unlock;
2197 }
2198 cm_id_priv->id.state = IB_CM_DREQ_RCVD;
2199 cm_id_priv->tid = dreq_msg->hdr.tid;
2200 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2201 if (!ret)
2202 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002203 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002204
2205 if (ret)
2206 cm_process_work(cm_id_priv, work);
2207 else
2208 cm_deref_id(cm_id_priv);
2209 return 0;
2210
Sean Hefty24be6e82007-06-18 11:09:36 -07002211unlock: spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002212deref: cm_deref_id(cm_id_priv);
2213 return -EINVAL;
2214}
2215
2216static int cm_drep_handler(struct cm_work *work)
2217{
2218 struct cm_id_private *cm_id_priv;
2219 struct cm_drep_msg *drep_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002220 int ret;
2221
2222 drep_msg = (struct cm_drep_msg *)work->mad_recv_wc->recv_buf.mad;
2223 cm_id_priv = cm_acquire_id(drep_msg->remote_comm_id,
2224 drep_msg->local_comm_id);
2225 if (!cm_id_priv)
2226 return -EINVAL;
2227
2228 work->cm_event.private_data = &drep_msg->private_data;
2229
Sean Hefty24be6e82007-06-18 11:09:36 -07002230 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002231 if (cm_id_priv->id.state != IB_CM_DREQ_SENT &&
2232 cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
Sean Hefty24be6e82007-06-18 11:09:36 -07002233 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002234 goto out;
2235 }
2236 cm_enter_timewait(cm_id_priv);
2237
Sean Hefty34816ad2005-10-25 10:51:39 -07002238 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002239 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2240 if (!ret)
2241 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002242 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002243
2244 if (ret)
2245 cm_process_work(cm_id_priv, work);
2246 else
2247 cm_deref_id(cm_id_priv);
2248 return 0;
2249out:
2250 cm_deref_id(cm_id_priv);
2251 return -EINVAL;
2252}
2253
2254int ib_send_cm_rej(struct ib_cm_id *cm_id,
2255 enum ib_cm_rej_reason reason,
2256 void *ari,
2257 u8 ari_length,
2258 const void *private_data,
2259 u8 private_data_len)
2260{
2261 struct cm_id_private *cm_id_priv;
2262 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002263 unsigned long flags;
2264 int ret;
2265
2266 if ((private_data && private_data_len > IB_CM_REJ_PRIVATE_DATA_SIZE) ||
2267 (ari && ari_length > IB_CM_REJ_ARI_LENGTH))
2268 return -EINVAL;
2269
2270 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2271
2272 spin_lock_irqsave(&cm_id_priv->lock, flags);
2273 switch (cm_id->state) {
2274 case IB_CM_REQ_SENT:
2275 case IB_CM_MRA_REQ_RCVD:
2276 case IB_CM_REQ_RCVD:
2277 case IB_CM_MRA_REQ_SENT:
2278 case IB_CM_REP_RCVD:
2279 case IB_CM_MRA_REP_SENT:
2280 ret = cm_alloc_msg(cm_id_priv, &msg);
2281 if (!ret)
2282 cm_format_rej((struct cm_rej_msg *) msg->mad,
2283 cm_id_priv, reason, ari, ari_length,
2284 private_data, private_data_len);
2285
2286 cm_reset_to_idle(cm_id_priv);
2287 break;
2288 case IB_CM_REP_SENT:
2289 case IB_CM_MRA_REP_RCVD:
2290 ret = cm_alloc_msg(cm_id_priv, &msg);
2291 if (!ret)
2292 cm_format_rej((struct cm_rej_msg *) msg->mad,
2293 cm_id_priv, reason, ari, ari_length,
2294 private_data, private_data_len);
2295
2296 cm_enter_timewait(cm_id_priv);
2297 break;
2298 default:
2299 ret = -EINVAL;
2300 goto out;
2301 }
2302
2303 if (ret)
2304 goto out;
2305
Sean Hefty34816ad2005-10-25 10:51:39 -07002306 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002307 if (ret)
2308 cm_free_msg(msg);
2309
2310out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2311 return ret;
2312}
2313EXPORT_SYMBOL(ib_send_cm_rej);
2314
2315static void cm_format_rej_event(struct cm_work *work)
2316{
2317 struct cm_rej_msg *rej_msg;
2318 struct ib_cm_rej_event_param *param;
2319
2320 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2321 param = &work->cm_event.param.rej_rcvd;
2322 param->ari = rej_msg->ari;
2323 param->ari_length = cm_rej_get_reject_info_len(rej_msg);
Sean Hefty97f52eb2005-08-13 21:05:57 -07002324 param->reason = __be16_to_cpu(rej_msg->reason);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002325 work->cm_event.private_data = &rej_msg->private_data;
2326}
2327
2328static struct cm_id_private * cm_acquire_rejected_id(struct cm_rej_msg *rej_msg)
2329{
2330 struct cm_timewait_info *timewait_info;
2331 struct cm_id_private *cm_id_priv;
Sean Hefty97f52eb2005-08-13 21:05:57 -07002332 __be32 remote_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002333
2334 remote_id = rej_msg->local_comm_id;
2335
Sean Hefty97f52eb2005-08-13 21:05:57 -07002336 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_TIMEOUT) {
Sean Hefty24be6e82007-06-18 11:09:36 -07002337 spin_lock_irq(&cm.lock);
Sean Hefty97f52eb2005-08-13 21:05:57 -07002338 timewait_info = cm_find_remote_id( *((__be64 *) rej_msg->ari),
Hal Rosenstocka9770492005-07-27 11:45:40 -07002339 remote_id);
2340 if (!timewait_info) {
Sean Hefty24be6e82007-06-18 11:09:36 -07002341 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002342 return NULL;
2343 }
Sean Heftyf06d2652006-08-28 15:15:18 -07002344 cm_id_priv = idr_find(&cm.local_id_table, (__force int)
2345 (timewait_info->work.local_id ^
2346 cm.random_id_operand));
Hal Rosenstocka9770492005-07-27 11:45:40 -07002347 if (cm_id_priv) {
2348 if (cm_id_priv->id.remote_id == remote_id)
2349 atomic_inc(&cm_id_priv->refcount);
2350 else
2351 cm_id_priv = NULL;
2352 }
Sean Hefty24be6e82007-06-18 11:09:36 -07002353 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002354 } else if (cm_rej_get_msg_rejected(rej_msg) == CM_MSG_RESPONSE_REQ)
2355 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, 0);
2356 else
2357 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, remote_id);
2358
2359 return cm_id_priv;
2360}
2361
2362static int cm_rej_handler(struct cm_work *work)
2363{
2364 struct cm_id_private *cm_id_priv;
2365 struct cm_rej_msg *rej_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002366 int ret;
2367
2368 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2369 cm_id_priv = cm_acquire_rejected_id(rej_msg);
2370 if (!cm_id_priv)
2371 return -EINVAL;
2372
2373 cm_format_rej_event(work);
2374
Sean Hefty24be6e82007-06-18 11:09:36 -07002375 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002376 switch (cm_id_priv->id.state) {
2377 case IB_CM_REQ_SENT:
2378 case IB_CM_MRA_REQ_RCVD:
2379 case IB_CM_REP_SENT:
2380 case IB_CM_MRA_REP_RCVD:
Sean Hefty34816ad2005-10-25 10:51:39 -07002381 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002382 /* fall through */
2383 case IB_CM_REQ_RCVD:
2384 case IB_CM_MRA_REQ_SENT:
Sean Hefty97f52eb2005-08-13 21:05:57 -07002385 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_STALE_CONN)
Hal Rosenstocka9770492005-07-27 11:45:40 -07002386 cm_enter_timewait(cm_id_priv);
2387 else
2388 cm_reset_to_idle(cm_id_priv);
2389 break;
2390 case IB_CM_DREQ_SENT:
Sean Hefty34816ad2005-10-25 10:51:39 -07002391 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002392 /* fall through */
2393 case IB_CM_REP_RCVD:
2394 case IB_CM_MRA_REP_SENT:
Hal Rosenstocka9770492005-07-27 11:45:40 -07002395 cm_enter_timewait(cm_id_priv);
2396 break;
Sean Hefty8d8ac862011-03-03 23:31:06 +00002397 case IB_CM_ESTABLISHED:
2398 if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT ||
2399 cm_id_priv->id.lap_state == IB_CM_LAP_SENT) {
2400 if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT)
2401 ib_cancel_mad(cm_id_priv->av.port->mad_agent,
2402 cm_id_priv->msg);
2403 cm_enter_timewait(cm_id_priv);
2404 break;
2405 }
2406 /* fall through */
Hal Rosenstocka9770492005-07-27 11:45:40 -07002407 default:
Sean Hefty24be6e82007-06-18 11:09:36 -07002408 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002409 ret = -EINVAL;
2410 goto out;
2411 }
2412
2413 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2414 if (!ret)
2415 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002416 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002417
2418 if (ret)
2419 cm_process_work(cm_id_priv, work);
2420 else
2421 cm_deref_id(cm_id_priv);
2422 return 0;
2423out:
2424 cm_deref_id(cm_id_priv);
2425 return -EINVAL;
2426}
2427
2428int ib_send_cm_mra(struct ib_cm_id *cm_id,
2429 u8 service_timeout,
2430 const void *private_data,
2431 u8 private_data_len)
2432{
2433 struct cm_id_private *cm_id_priv;
2434 struct ib_mad_send_buf *msg;
Sean Heftyde98b692007-08-01 13:49:53 -07002435 enum ib_cm_state cm_state;
2436 enum ib_cm_lap_state lap_state;
2437 enum cm_msg_response msg_response;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002438 void *data;
2439 unsigned long flags;
2440 int ret;
2441
2442 if (private_data && private_data_len > IB_CM_MRA_PRIVATE_DATA_SIZE)
2443 return -EINVAL;
2444
2445 data = cm_copy_private_data(private_data, private_data_len);
2446 if (IS_ERR(data))
2447 return PTR_ERR(data);
2448
2449 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2450
2451 spin_lock_irqsave(&cm_id_priv->lock, flags);
2452 switch(cm_id_priv->id.state) {
2453 case IB_CM_REQ_RCVD:
Sean Heftyde98b692007-08-01 13:49:53 -07002454 cm_state = IB_CM_MRA_REQ_SENT;
2455 lap_state = cm_id->lap_state;
2456 msg_response = CM_MSG_RESPONSE_REQ;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002457 break;
2458 case IB_CM_REP_RCVD:
Sean Heftyde98b692007-08-01 13:49:53 -07002459 cm_state = IB_CM_MRA_REP_SENT;
2460 lap_state = cm_id->lap_state;
2461 msg_response = CM_MSG_RESPONSE_REP;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002462 break;
2463 case IB_CM_ESTABLISHED:
Sean Hefty50a025c2010-07-21 23:36:52 +00002464 if (cm_id->lap_state == IB_CM_LAP_RCVD) {
2465 cm_state = cm_id->state;
2466 lap_state = IB_CM_MRA_LAP_SENT;
2467 msg_response = CM_MSG_RESPONSE_OTHER;
2468 break;
2469 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07002470 default:
2471 ret = -EINVAL;
2472 goto error1;
2473 }
Sean Heftyde98b692007-08-01 13:49:53 -07002474
2475 if (!(service_timeout & IB_CM_MRA_FLAG_DELAY)) {
2476 ret = cm_alloc_msg(cm_id_priv, &msg);
2477 if (ret)
2478 goto error1;
2479
2480 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2481 msg_response, service_timeout,
2482 private_data, private_data_len);
2483 ret = ib_post_send_mad(msg, NULL);
2484 if (ret)
2485 goto error2;
2486 }
2487
2488 cm_id->state = cm_state;
2489 cm_id->lap_state = lap_state;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002490 cm_id_priv->service_timeout = service_timeout;
2491 cm_set_private_data(cm_id_priv, data, private_data_len);
2492 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2493 return 0;
2494
2495error1: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2496 kfree(data);
2497 return ret;
2498
2499error2: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2500 kfree(data);
2501 cm_free_msg(msg);
2502 return ret;
2503}
2504EXPORT_SYMBOL(ib_send_cm_mra);
2505
2506static struct cm_id_private * cm_acquire_mraed_id(struct cm_mra_msg *mra_msg)
2507{
2508 switch (cm_mra_get_msg_mraed(mra_msg)) {
2509 case CM_MSG_RESPONSE_REQ:
2510 return cm_acquire_id(mra_msg->remote_comm_id, 0);
2511 case CM_MSG_RESPONSE_REP:
2512 case CM_MSG_RESPONSE_OTHER:
2513 return cm_acquire_id(mra_msg->remote_comm_id,
2514 mra_msg->local_comm_id);
2515 default:
2516 return NULL;
2517 }
2518}
2519
2520static int cm_mra_handler(struct cm_work *work)
2521{
2522 struct cm_id_private *cm_id_priv;
2523 struct cm_mra_msg *mra_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002524 int timeout, ret;
2525
2526 mra_msg = (struct cm_mra_msg *)work->mad_recv_wc->recv_buf.mad;
2527 cm_id_priv = cm_acquire_mraed_id(mra_msg);
2528 if (!cm_id_priv)
2529 return -EINVAL;
2530
2531 work->cm_event.private_data = &mra_msg->private_data;
2532 work->cm_event.param.mra_rcvd.service_timeout =
2533 cm_mra_get_service_timeout(mra_msg);
2534 timeout = cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg)) +
Sean Hefty1d846122007-06-18 11:09:37 -07002535 cm_convert_to_ms(cm_id_priv->av.timeout);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002536
Sean Hefty24be6e82007-06-18 11:09:36 -07002537 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002538 switch (cm_id_priv->id.state) {
2539 case IB_CM_REQ_SENT:
2540 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REQ ||
2541 ib_modify_mad(cm_id_priv->av.port->mad_agent,
Sean Hefty34816ad2005-10-25 10:51:39 -07002542 cm_id_priv->msg, timeout))
Hal Rosenstocka9770492005-07-27 11:45:40 -07002543 goto out;
2544 cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD;
2545 break;
2546 case IB_CM_REP_SENT:
2547 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REP ||
2548 ib_modify_mad(cm_id_priv->av.port->mad_agent,
Sean Hefty34816ad2005-10-25 10:51:39 -07002549 cm_id_priv->msg, timeout))
Hal Rosenstocka9770492005-07-27 11:45:40 -07002550 goto out;
2551 cm_id_priv->id.state = IB_CM_MRA_REP_RCVD;
2552 break;
2553 case IB_CM_ESTABLISHED:
2554 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_OTHER ||
2555 cm_id_priv->id.lap_state != IB_CM_LAP_SENT ||
2556 ib_modify_mad(cm_id_priv->av.port->mad_agent,
Sean Hefty9af57b72007-07-16 21:49:35 -07002557 cm_id_priv->msg, timeout)) {
2558 if (cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
2559 atomic_long_inc(&work->port->
2560 counter_group[CM_RECV_DUPLICATES].
2561 counter[CM_MRA_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002562 goto out;
Sean Hefty9af57b72007-07-16 21:49:35 -07002563 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07002564 cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD;
2565 break;
Sean Hefty9af57b72007-07-16 21:49:35 -07002566 case IB_CM_MRA_REQ_RCVD:
2567 case IB_CM_MRA_REP_RCVD:
2568 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2569 counter[CM_MRA_COUNTER]);
2570 /* fall through */
Hal Rosenstocka9770492005-07-27 11:45:40 -07002571 default:
2572 goto out;
2573 }
2574
2575 cm_id_priv->msg->context[1] = (void *) (unsigned long)
2576 cm_id_priv->id.state;
2577 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2578 if (!ret)
2579 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002580 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002581
2582 if (ret)
2583 cm_process_work(cm_id_priv, work);
2584 else
2585 cm_deref_id(cm_id_priv);
2586 return 0;
2587out:
Sean Hefty24be6e82007-06-18 11:09:36 -07002588 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002589 cm_deref_id(cm_id_priv);
2590 return -EINVAL;
2591}
2592
2593static void cm_format_lap(struct cm_lap_msg *lap_msg,
2594 struct cm_id_private *cm_id_priv,
2595 struct ib_sa_path_rec *alternate_path,
2596 const void *private_data,
2597 u8 private_data_len)
2598{
2599 cm_format_mad_hdr(&lap_msg->hdr, CM_LAP_ATTR_ID,
2600 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_LAP));
2601 lap_msg->local_comm_id = cm_id_priv->id.local_id;
2602 lap_msg->remote_comm_id = cm_id_priv->id.remote_id;
2603 cm_lap_set_remote_qpn(lap_msg, cm_id_priv->remote_qpn);
2604 /* todo: need remote CM response timeout */
2605 cm_lap_set_remote_resp_timeout(lap_msg, 0x1F);
2606 lap_msg->alt_local_lid = alternate_path->slid;
2607 lap_msg->alt_remote_lid = alternate_path->dlid;
2608 lap_msg->alt_local_gid = alternate_path->sgid;
2609 lap_msg->alt_remote_gid = alternate_path->dgid;
2610 cm_lap_set_flow_label(lap_msg, alternate_path->flow_label);
2611 cm_lap_set_traffic_class(lap_msg, alternate_path->traffic_class);
2612 lap_msg->alt_hop_limit = alternate_path->hop_limit;
2613 cm_lap_set_packet_rate(lap_msg, alternate_path->rate);
2614 cm_lap_set_sl(lap_msg, alternate_path->sl);
2615 cm_lap_set_subnet_local(lap_msg, 1); /* local only... */
2616 cm_lap_set_local_ack_timeout(lap_msg,
Sean Hefty1d846122007-06-18 11:09:37 -07002617 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
2618 alternate_path->packet_life_time));
Hal Rosenstocka9770492005-07-27 11:45:40 -07002619
2620 if (private_data && private_data_len)
2621 memcpy(lap_msg->private_data, private_data, private_data_len);
2622}
2623
2624int ib_send_cm_lap(struct ib_cm_id *cm_id,
2625 struct ib_sa_path_rec *alternate_path,
2626 const void *private_data,
2627 u8 private_data_len)
2628{
2629 struct cm_id_private *cm_id_priv;
2630 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002631 unsigned long flags;
2632 int ret;
2633
2634 if (private_data && private_data_len > IB_CM_LAP_PRIVATE_DATA_SIZE)
2635 return -EINVAL;
2636
2637 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2638 spin_lock_irqsave(&cm_id_priv->lock, flags);
2639 if (cm_id->state != IB_CM_ESTABLISHED ||
Sean Heftye1444b52006-11-28 14:57:13 -08002640 (cm_id->lap_state != IB_CM_LAP_UNINIT &&
2641 cm_id->lap_state != IB_CM_LAP_IDLE)) {
Hal Rosenstocka9770492005-07-27 11:45:40 -07002642 ret = -EINVAL;
2643 goto out;
2644 }
2645
Sean Heftye1444b52006-11-28 14:57:13 -08002646 ret = cm_init_av_by_path(alternate_path, &cm_id_priv->alt_av);
2647 if (ret)
2648 goto out;
Sean Hefty1d846122007-06-18 11:09:37 -07002649 cm_id_priv->alt_av.timeout =
2650 cm_ack_timeout(cm_id_priv->target_ack_delay,
2651 cm_id_priv->alt_av.timeout - 1);
Sean Heftye1444b52006-11-28 14:57:13 -08002652
Hal Rosenstocka9770492005-07-27 11:45:40 -07002653 ret = cm_alloc_msg(cm_id_priv, &msg);
2654 if (ret)
2655 goto out;
2656
2657 cm_format_lap((struct cm_lap_msg *) msg->mad, cm_id_priv,
2658 alternate_path, private_data, private_data_len);
Sean Hefty34816ad2005-10-25 10:51:39 -07002659 msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002660 msg->context[1] = (void *) (unsigned long) IB_CM_ESTABLISHED;
2661
Sean Hefty34816ad2005-10-25 10:51:39 -07002662 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002663 if (ret) {
2664 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2665 cm_free_msg(msg);
2666 return ret;
2667 }
2668
2669 cm_id->lap_state = IB_CM_LAP_SENT;
2670 cm_id_priv->msg = msg;
2671
2672out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2673 return ret;
2674}
2675EXPORT_SYMBOL(ib_send_cm_lap);
2676
Sean Heftye1444b52006-11-28 14:57:13 -08002677static void cm_format_path_from_lap(struct cm_id_private *cm_id_priv,
2678 struct ib_sa_path_rec *path,
Hal Rosenstocka9770492005-07-27 11:45:40 -07002679 struct cm_lap_msg *lap_msg)
2680{
2681 memset(path, 0, sizeof *path);
2682 path->dgid = lap_msg->alt_local_gid;
2683 path->sgid = lap_msg->alt_remote_gid;
2684 path->dlid = lap_msg->alt_local_lid;
2685 path->slid = lap_msg->alt_remote_lid;
2686 path->flow_label = cm_lap_get_flow_label(lap_msg);
2687 path->hop_limit = lap_msg->alt_hop_limit;
2688 path->traffic_class = cm_lap_get_traffic_class(lap_msg);
2689 path->reversible = 1;
Sean Heftye1444b52006-11-28 14:57:13 -08002690 path->pkey = cm_id_priv->pkey;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002691 path->sl = cm_lap_get_sl(lap_msg);
2692 path->mtu_selector = IB_SA_EQ;
Sean Heftye1444b52006-11-28 14:57:13 -08002693 path->mtu = cm_id_priv->path_mtu;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002694 path->rate_selector = IB_SA_EQ;
2695 path->rate = cm_lap_get_packet_rate(lap_msg);
2696 path->packet_life_time_selector = IB_SA_EQ;
2697 path->packet_life_time = cm_lap_get_local_ack_timeout(lap_msg);
2698 path->packet_life_time -= (path->packet_life_time > 0);
2699}
2700
2701static int cm_lap_handler(struct cm_work *work)
2702{
2703 struct cm_id_private *cm_id_priv;
2704 struct cm_lap_msg *lap_msg;
2705 struct ib_cm_lap_event_param *param;
2706 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002707 int ret;
2708
2709 /* todo: verify LAP request and send reject APR if invalid. */
2710 lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad;
2711 cm_id_priv = cm_acquire_id(lap_msg->remote_comm_id,
2712 lap_msg->local_comm_id);
2713 if (!cm_id_priv)
2714 return -EINVAL;
2715
2716 param = &work->cm_event.param.lap_rcvd;
2717 param->alternate_path = &work->path[0];
Sean Heftye1444b52006-11-28 14:57:13 -08002718 cm_format_path_from_lap(cm_id_priv, param->alternate_path, lap_msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002719 work->cm_event.private_data = &lap_msg->private_data;
2720
Sean Hefty24be6e82007-06-18 11:09:36 -07002721 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002722 if (cm_id_priv->id.state != IB_CM_ESTABLISHED)
2723 goto unlock;
2724
2725 switch (cm_id_priv->id.lap_state) {
Sean Heftye1444b52006-11-28 14:57:13 -08002726 case IB_CM_LAP_UNINIT:
Hal Rosenstocka9770492005-07-27 11:45:40 -07002727 case IB_CM_LAP_IDLE:
2728 break;
2729 case IB_CM_MRA_LAP_SENT:
Sean Hefty9af57b72007-07-16 21:49:35 -07002730 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2731 counter[CM_LAP_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002732 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2733 goto unlock;
2734
2735 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2736 CM_MSG_RESPONSE_OTHER,
2737 cm_id_priv->service_timeout,
2738 cm_id_priv->private_data,
2739 cm_id_priv->private_data_len);
Sean Hefty24be6e82007-06-18 11:09:36 -07002740 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002741
Sean Hefty34816ad2005-10-25 10:51:39 -07002742 if (ib_post_send_mad(msg, NULL))
Hal Rosenstocka9770492005-07-27 11:45:40 -07002743 cm_free_msg(msg);
2744 goto deref;
Sean Hefty9af57b72007-07-16 21:49:35 -07002745 case IB_CM_LAP_RCVD:
2746 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2747 counter[CM_LAP_COUNTER]);
2748 goto unlock;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002749 default:
2750 goto unlock;
2751 }
2752
2753 cm_id_priv->id.lap_state = IB_CM_LAP_RCVD;
2754 cm_id_priv->tid = lap_msg->hdr.tid;
Sean Heftye1444b52006-11-28 14:57:13 -08002755 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
2756 work->mad_recv_wc->recv_buf.grh,
2757 &cm_id_priv->av);
2758 cm_init_av_by_path(param->alternate_path, &cm_id_priv->alt_av);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002759 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2760 if (!ret)
2761 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002762 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002763
2764 if (ret)
2765 cm_process_work(cm_id_priv, work);
2766 else
2767 cm_deref_id(cm_id_priv);
2768 return 0;
2769
Sean Hefty24be6e82007-06-18 11:09:36 -07002770unlock: spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002771deref: cm_deref_id(cm_id_priv);
2772 return -EINVAL;
2773}
2774
2775static void cm_format_apr(struct cm_apr_msg *apr_msg,
2776 struct cm_id_private *cm_id_priv,
2777 enum ib_cm_apr_status status,
2778 void *info,
2779 u8 info_length,
2780 const void *private_data,
2781 u8 private_data_len)
2782{
2783 cm_format_mad_hdr(&apr_msg->hdr, CM_APR_ATTR_ID, cm_id_priv->tid);
2784 apr_msg->local_comm_id = cm_id_priv->id.local_id;
2785 apr_msg->remote_comm_id = cm_id_priv->id.remote_id;
2786 apr_msg->ap_status = (u8) status;
2787
2788 if (info && info_length) {
2789 apr_msg->info_length = info_length;
2790 memcpy(apr_msg->info, info, info_length);
2791 }
2792
2793 if (private_data && private_data_len)
2794 memcpy(apr_msg->private_data, private_data, private_data_len);
2795}
2796
2797int ib_send_cm_apr(struct ib_cm_id *cm_id,
2798 enum ib_cm_apr_status status,
2799 void *info,
2800 u8 info_length,
2801 const void *private_data,
2802 u8 private_data_len)
2803{
2804 struct cm_id_private *cm_id_priv;
2805 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002806 unsigned long flags;
2807 int ret;
2808
2809 if ((private_data && private_data_len > IB_CM_APR_PRIVATE_DATA_SIZE) ||
2810 (info && info_length > IB_CM_APR_INFO_LENGTH))
2811 return -EINVAL;
2812
2813 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2814 spin_lock_irqsave(&cm_id_priv->lock, flags);
2815 if (cm_id->state != IB_CM_ESTABLISHED ||
2816 (cm_id->lap_state != IB_CM_LAP_RCVD &&
2817 cm_id->lap_state != IB_CM_MRA_LAP_SENT)) {
2818 ret = -EINVAL;
2819 goto out;
2820 }
2821
2822 ret = cm_alloc_msg(cm_id_priv, &msg);
2823 if (ret)
2824 goto out;
2825
2826 cm_format_apr((struct cm_apr_msg *) msg->mad, cm_id_priv, status,
2827 info, info_length, private_data, private_data_len);
Sean Hefty34816ad2005-10-25 10:51:39 -07002828 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002829 if (ret) {
2830 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2831 cm_free_msg(msg);
2832 return ret;
2833 }
2834
2835 cm_id->lap_state = IB_CM_LAP_IDLE;
2836out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2837 return ret;
2838}
2839EXPORT_SYMBOL(ib_send_cm_apr);
2840
2841static int cm_apr_handler(struct cm_work *work)
2842{
2843 struct cm_id_private *cm_id_priv;
2844 struct cm_apr_msg *apr_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002845 int ret;
2846
2847 apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad;
2848 cm_id_priv = cm_acquire_id(apr_msg->remote_comm_id,
2849 apr_msg->local_comm_id);
2850 if (!cm_id_priv)
2851 return -EINVAL; /* Unmatched reply. */
2852
2853 work->cm_event.param.apr_rcvd.ap_status = apr_msg->ap_status;
2854 work->cm_event.param.apr_rcvd.apr_info = &apr_msg->info;
2855 work->cm_event.param.apr_rcvd.info_len = apr_msg->info_length;
2856 work->cm_event.private_data = &apr_msg->private_data;
2857
Sean Hefty24be6e82007-06-18 11:09:36 -07002858 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002859 if (cm_id_priv->id.state != IB_CM_ESTABLISHED ||
2860 (cm_id_priv->id.lap_state != IB_CM_LAP_SENT &&
2861 cm_id_priv->id.lap_state != IB_CM_MRA_LAP_RCVD)) {
Sean Hefty24be6e82007-06-18 11:09:36 -07002862 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002863 goto out;
2864 }
2865 cm_id_priv->id.lap_state = IB_CM_LAP_IDLE;
Sean Hefty34816ad2005-10-25 10:51:39 -07002866 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002867 cm_id_priv->msg = NULL;
2868
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);
Sean Hefty24be6e82007-06-18 11:09:36 -07002872 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002873
2874 if (ret)
2875 cm_process_work(cm_id_priv, work);
2876 else
2877 cm_deref_id(cm_id_priv);
2878 return 0;
2879out:
2880 cm_deref_id(cm_id_priv);
2881 return -EINVAL;
2882}
2883
2884static int cm_timewait_handler(struct cm_work *work)
2885{
2886 struct cm_timewait_info *timewait_info;
2887 struct cm_id_private *cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002888 int ret;
2889
2890 timewait_info = (struct cm_timewait_info *)work;
Sean Hefty85753292006-10-04 11:29:59 -07002891 spin_lock_irq(&cm.lock);
2892 list_del(&timewait_info->list);
2893 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002894
2895 cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
2896 timewait_info->work.remote_id);
2897 if (!cm_id_priv)
2898 return -EINVAL;
2899
Sean Hefty85753292006-10-04 11:29:59 -07002900 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002901 if (cm_id_priv->id.state != IB_CM_TIMEWAIT ||
2902 cm_id_priv->remote_qpn != timewait_info->remote_qpn) {
Sean Hefty85753292006-10-04 11:29:59 -07002903 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002904 goto out;
2905 }
2906 cm_id_priv->id.state = IB_CM_IDLE;
2907 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2908 if (!ret)
2909 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty85753292006-10-04 11:29:59 -07002910 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002911
2912 if (ret)
2913 cm_process_work(cm_id_priv, work);
2914 else
2915 cm_deref_id(cm_id_priv);
2916 return 0;
2917out:
2918 cm_deref_id(cm_id_priv);
2919 return -EINVAL;
2920}
2921
2922static void cm_format_sidr_req(struct cm_sidr_req_msg *sidr_req_msg,
2923 struct cm_id_private *cm_id_priv,
2924 struct ib_cm_sidr_req_param *param)
2925{
2926 cm_format_mad_hdr(&sidr_req_msg->hdr, CM_SIDR_REQ_ATTR_ID,
2927 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_SIDR));
2928 sidr_req_msg->request_id = cm_id_priv->id.local_id;
Roland Dreiera88f4882008-04-16 21:01:07 -07002929 sidr_req_msg->pkey = param->path->pkey;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002930 sidr_req_msg->service_id = param->service_id;
2931
2932 if (param->private_data && param->private_data_len)
2933 memcpy(sidr_req_msg->private_data, param->private_data,
2934 param->private_data_len);
2935}
2936
2937int ib_send_cm_sidr_req(struct ib_cm_id *cm_id,
2938 struct ib_cm_sidr_req_param *param)
2939{
2940 struct cm_id_private *cm_id_priv;
2941 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002942 unsigned long flags;
2943 int ret;
2944
2945 if (!param->path || (param->private_data &&
2946 param->private_data_len > IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE))
2947 return -EINVAL;
2948
2949 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2950 ret = cm_init_av_by_path(param->path, &cm_id_priv->av);
2951 if (ret)
2952 goto out;
2953
2954 cm_id->service_id = param->service_id;
Harvey Harrison9c3da092009-01-17 17:11:57 -08002955 cm_id->service_mask = ~cpu_to_be64(0);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002956 cm_id_priv->timeout_ms = param->timeout_ms;
2957 cm_id_priv->max_cm_retries = param->max_cm_retries;
2958 ret = cm_alloc_msg(cm_id_priv, &msg);
2959 if (ret)
2960 goto out;
2961
2962 cm_format_sidr_req((struct cm_sidr_req_msg *) msg->mad, cm_id_priv,
2963 param);
Sean Hefty34816ad2005-10-25 10:51:39 -07002964 msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002965 msg->context[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT;
2966
2967 spin_lock_irqsave(&cm_id_priv->lock, flags);
2968 if (cm_id->state == IB_CM_IDLE)
Sean Hefty34816ad2005-10-25 10:51:39 -07002969 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002970 else
2971 ret = -EINVAL;
2972
2973 if (ret) {
2974 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2975 cm_free_msg(msg);
2976 goto out;
2977 }
2978 cm_id->state = IB_CM_SIDR_REQ_SENT;
2979 cm_id_priv->msg = msg;
2980 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2981out:
2982 return ret;
2983}
2984EXPORT_SYMBOL(ib_send_cm_sidr_req);
2985
2986static void cm_format_sidr_req_event(struct cm_work *work,
2987 struct ib_cm_id *listen_id)
2988{
2989 struct cm_sidr_req_msg *sidr_req_msg;
2990 struct ib_cm_sidr_req_event_param *param;
2991
2992 sidr_req_msg = (struct cm_sidr_req_msg *)
2993 work->mad_recv_wc->recv_buf.mad;
2994 param = &work->cm_event.param.sidr_req_rcvd;
Sean Hefty97f52eb2005-08-13 21:05:57 -07002995 param->pkey = __be16_to_cpu(sidr_req_msg->pkey);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002996 param->listen_id = listen_id;
Haggai Eran15865e7d2015-07-30 17:50:17 +03002997 param->service_id = sidr_req_msg->service_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002998 param->port = work->port->port_num;
2999 work->cm_event.private_data = &sidr_req_msg->private_data;
3000}
3001
3002static int cm_sidr_req_handler(struct cm_work *work)
3003{
3004 struct ib_cm_id *cm_id;
3005 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
3006 struct cm_sidr_req_msg *sidr_req_msg;
3007 struct ib_wc *wc;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003008
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003009 cm_id = ib_create_cm_id(work->port->cm_dev->ib_device, NULL, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003010 if (IS_ERR(cm_id))
3011 return PTR_ERR(cm_id);
3012 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3013
3014 /* Record SGID/SLID and request ID for lookup. */
3015 sidr_req_msg = (struct cm_sidr_req_msg *)
3016 work->mad_recv_wc->recv_buf.mad;
3017 wc = work->mad_recv_wc->wc;
Sean Hefty97f52eb2005-08-13 21:05:57 -07003018 cm_id_priv->av.dgid.global.subnet_prefix = cpu_to_be64(wc->slid);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003019 cm_id_priv->av.dgid.global.interface_id = 0;
3020 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
Sean Heftyca222c62006-06-17 20:37:40 -07003021 work->mad_recv_wc->recv_buf.grh,
Hal Rosenstocka9770492005-07-27 11:45:40 -07003022 &cm_id_priv->av);
3023 cm_id_priv->id.remote_id = sidr_req_msg->request_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003024 cm_id_priv->tid = sidr_req_msg->hdr.tid;
3025 atomic_inc(&cm_id_priv->work_count);
3026
Sean Hefty24be6e82007-06-18 11:09:36 -07003027 spin_lock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003028 cur_cm_id_priv = cm_insert_remote_sidr(cm_id_priv);
3029 if (cur_cm_id_priv) {
Sean Hefty24be6e82007-06-18 11:09:36 -07003030 spin_unlock_irq(&cm.lock);
Sean Hefty9af57b72007-07-16 21:49:35 -07003031 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
3032 counter[CM_SIDR_REQ_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003033 goto out; /* Duplicate message. */
3034 }
Sean Hefty29c27312007-07-02 14:36:19 -07003035 cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD;
Sean Hefty07d357d2005-10-17 15:37:43 -07003036 cur_cm_id_priv = cm_find_listen(cm_id->device,
Sean Hefty6e61d042006-06-17 20:37:28 -07003037 sidr_req_msg->service_id,
3038 sidr_req_msg->private_data);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003039 if (!cur_cm_id_priv) {
Sean Hefty24be6e82007-06-18 11:09:36 -07003040 spin_unlock_irq(&cm.lock);
Sean Hefty6164c8c2007-07-02 16:14:14 -07003041 cm_reject_sidr_req(cm_id_priv, IB_SIDR_UNSUPPORTED);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003042 goto out; /* No match. */
3043 }
3044 atomic_inc(&cur_cm_id_priv->refcount);
Sean Hefty29963432011-02-23 08:17:40 -08003045 atomic_inc(&cm_id_priv->refcount);
Sean Hefty24be6e82007-06-18 11:09:36 -07003046 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003047
3048 cm_id_priv->id.cm_handler = cur_cm_id_priv->id.cm_handler;
3049 cm_id_priv->id.context = cur_cm_id_priv->id.context;
3050 cm_id_priv->id.service_id = sidr_req_msg->service_id;
Harvey Harrison9c3da092009-01-17 17:11:57 -08003051 cm_id_priv->id.service_mask = ~cpu_to_be64(0);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003052
3053 cm_format_sidr_req_event(work, &cur_cm_id_priv->id);
3054 cm_process_work(cm_id_priv, work);
3055 cm_deref_id(cur_cm_id_priv);
3056 return 0;
3057out:
3058 ib_destroy_cm_id(&cm_id_priv->id);
3059 return -EINVAL;
3060}
3061
3062static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg,
3063 struct cm_id_private *cm_id_priv,
3064 struct ib_cm_sidr_rep_param *param)
3065{
3066 cm_format_mad_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID,
3067 cm_id_priv->tid);
3068 sidr_rep_msg->request_id = cm_id_priv->id.remote_id;
3069 sidr_rep_msg->status = param->status;
3070 cm_sidr_rep_set_qpn(sidr_rep_msg, cpu_to_be32(param->qp_num));
3071 sidr_rep_msg->service_id = cm_id_priv->id.service_id;
3072 sidr_rep_msg->qkey = cpu_to_be32(param->qkey);
3073
3074 if (param->info && param->info_length)
3075 memcpy(sidr_rep_msg->info, param->info, param->info_length);
3076
3077 if (param->private_data && param->private_data_len)
3078 memcpy(sidr_rep_msg->private_data, param->private_data,
3079 param->private_data_len);
3080}
3081
3082int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id,
3083 struct ib_cm_sidr_rep_param *param)
3084{
3085 struct cm_id_private *cm_id_priv;
3086 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003087 unsigned long flags;
3088 int ret;
3089
3090 if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) ||
3091 (param->private_data &&
3092 param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE))
3093 return -EINVAL;
3094
3095 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3096 spin_lock_irqsave(&cm_id_priv->lock, flags);
3097 if (cm_id->state != IB_CM_SIDR_REQ_RCVD) {
3098 ret = -EINVAL;
3099 goto error;
3100 }
3101
3102 ret = cm_alloc_msg(cm_id_priv, &msg);
3103 if (ret)
3104 goto error;
3105
3106 cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv,
3107 param);
Sean Hefty34816ad2005-10-25 10:51:39 -07003108 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003109 if (ret) {
3110 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3111 cm_free_msg(msg);
3112 return ret;
3113 }
3114 cm_id->state = IB_CM_IDLE;
3115 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3116
3117 spin_lock_irqsave(&cm.lock, flags);
3118 rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
3119 spin_unlock_irqrestore(&cm.lock, flags);
3120 return 0;
3121
3122error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3123 return ret;
3124}
3125EXPORT_SYMBOL(ib_send_cm_sidr_rep);
3126
3127static void cm_format_sidr_rep_event(struct cm_work *work)
3128{
3129 struct cm_sidr_rep_msg *sidr_rep_msg;
3130 struct ib_cm_sidr_rep_event_param *param;
3131
3132 sidr_rep_msg = (struct cm_sidr_rep_msg *)
3133 work->mad_recv_wc->recv_buf.mad;
3134 param = &work->cm_event.param.sidr_rep_rcvd;
3135 param->status = sidr_rep_msg->status;
3136 param->qkey = be32_to_cpu(sidr_rep_msg->qkey);
3137 param->qpn = be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg));
3138 param->info = &sidr_rep_msg->info;
3139 param->info_len = sidr_rep_msg->info_length;
3140 work->cm_event.private_data = &sidr_rep_msg->private_data;
3141}
3142
3143static int cm_sidr_rep_handler(struct cm_work *work)
3144{
3145 struct cm_sidr_rep_msg *sidr_rep_msg;
3146 struct cm_id_private *cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003147
3148 sidr_rep_msg = (struct cm_sidr_rep_msg *)
3149 work->mad_recv_wc->recv_buf.mad;
3150 cm_id_priv = cm_acquire_id(sidr_rep_msg->request_id, 0);
3151 if (!cm_id_priv)
3152 return -EINVAL; /* Unmatched reply. */
3153
Sean Hefty24be6e82007-06-18 11:09:36 -07003154 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003155 if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) {
Sean Hefty24be6e82007-06-18 11:09:36 -07003156 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003157 goto out;
3158 }
3159 cm_id_priv->id.state = IB_CM_IDLE;
Sean Hefty34816ad2005-10-25 10:51:39 -07003160 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Sean Hefty24be6e82007-06-18 11:09:36 -07003161 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003162
3163 cm_format_sidr_rep_event(work);
3164 cm_process_work(cm_id_priv, work);
3165 return 0;
3166out:
3167 cm_deref_id(cm_id_priv);
3168 return -EINVAL;
3169}
3170
3171static void cm_process_send_error(struct ib_mad_send_buf *msg,
3172 enum ib_wc_status wc_status)
3173{
3174 struct cm_id_private *cm_id_priv;
3175 struct ib_cm_event cm_event;
3176 enum ib_cm_state state;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003177 int ret;
3178
3179 memset(&cm_event, 0, sizeof cm_event);
3180 cm_id_priv = msg->context[0];
3181
3182 /* Discard old sends or ones without a response. */
Sean Hefty24be6e82007-06-18 11:09:36 -07003183 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003184 state = (enum ib_cm_state) (unsigned long) msg->context[1];
3185 if (msg != cm_id_priv->msg || state != cm_id_priv->id.state)
3186 goto discard;
3187
3188 switch (state) {
3189 case IB_CM_REQ_SENT:
3190 case IB_CM_MRA_REQ_RCVD:
3191 cm_reset_to_idle(cm_id_priv);
3192 cm_event.event = IB_CM_REQ_ERROR;
3193 break;
3194 case IB_CM_REP_SENT:
3195 case IB_CM_MRA_REP_RCVD:
3196 cm_reset_to_idle(cm_id_priv);
3197 cm_event.event = IB_CM_REP_ERROR;
3198 break;
3199 case IB_CM_DREQ_SENT:
3200 cm_enter_timewait(cm_id_priv);
3201 cm_event.event = IB_CM_DREQ_ERROR;
3202 break;
3203 case IB_CM_SIDR_REQ_SENT:
3204 cm_id_priv->id.state = IB_CM_IDLE;
3205 cm_event.event = IB_CM_SIDR_REQ_ERROR;
3206 break;
3207 default:
3208 goto discard;
3209 }
Sean Hefty24be6e82007-06-18 11:09:36 -07003210 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003211 cm_event.param.send_status = wc_status;
3212
3213 /* No other events can occur on the cm_id at this point. */
3214 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event);
3215 cm_free_msg(msg);
3216 if (ret)
3217 ib_destroy_cm_id(&cm_id_priv->id);
3218 return;
3219discard:
Sean Hefty24be6e82007-06-18 11:09:36 -07003220 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003221 cm_free_msg(msg);
3222}
3223
3224static void cm_send_handler(struct ib_mad_agent *mad_agent,
3225 struct ib_mad_send_wc *mad_send_wc)
3226{
Sean Hefty34816ad2005-10-25 10:51:39 -07003227 struct ib_mad_send_buf *msg = mad_send_wc->send_buf;
Sean Hefty9af57b72007-07-16 21:49:35 -07003228 struct cm_port *port;
3229 u16 attr_index;
3230
3231 port = mad_agent->context;
3232 attr_index = be16_to_cpu(((struct ib_mad_hdr *)
3233 msg->mad)->attr_id) - CM_ATTR_ID_OFFSET;
3234
3235 /*
3236 * If the send was in response to a received message (context[0] is not
3237 * set to a cm_id), and is not a REJ, then it is a send that was
3238 * manually retried.
3239 */
3240 if (!msg->context[0] && (attr_index != CM_REJ_COUNTER))
3241 msg->retries = 1;
3242
3243 atomic_long_add(1 + msg->retries,
3244 &port->counter_group[CM_XMIT].counter[attr_index]);
3245 if (msg->retries)
3246 atomic_long_add(msg->retries,
3247 &port->counter_group[CM_XMIT_RETRIES].
3248 counter[attr_index]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003249
3250 switch (mad_send_wc->status) {
3251 case IB_WC_SUCCESS:
3252 case IB_WC_WR_FLUSH_ERR:
3253 cm_free_msg(msg);
3254 break;
3255 default:
3256 if (msg->context[0] && msg->context[1])
3257 cm_process_send_error(msg, mad_send_wc->status);
3258 else
3259 cm_free_msg(msg);
3260 break;
3261 }
3262}
3263
David Howellsc4028952006-11-22 14:57:56 +00003264static void cm_work_handler(struct work_struct *_work)
Hal Rosenstocka9770492005-07-27 11:45:40 -07003265{
David Howellsc4028952006-11-22 14:57:56 +00003266 struct cm_work *work = container_of(_work, struct cm_work, work.work);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003267 int ret;
3268
3269 switch (work->cm_event.event) {
3270 case IB_CM_REQ_RECEIVED:
3271 ret = cm_req_handler(work);
3272 break;
3273 case IB_CM_MRA_RECEIVED:
3274 ret = cm_mra_handler(work);
3275 break;
3276 case IB_CM_REJ_RECEIVED:
3277 ret = cm_rej_handler(work);
3278 break;
3279 case IB_CM_REP_RECEIVED:
3280 ret = cm_rep_handler(work);
3281 break;
3282 case IB_CM_RTU_RECEIVED:
3283 ret = cm_rtu_handler(work);
3284 break;
3285 case IB_CM_USER_ESTABLISHED:
3286 ret = cm_establish_handler(work);
3287 break;
3288 case IB_CM_DREQ_RECEIVED:
3289 ret = cm_dreq_handler(work);
3290 break;
3291 case IB_CM_DREP_RECEIVED:
3292 ret = cm_drep_handler(work);
3293 break;
3294 case IB_CM_SIDR_REQ_RECEIVED:
3295 ret = cm_sidr_req_handler(work);
3296 break;
3297 case IB_CM_SIDR_REP_RECEIVED:
3298 ret = cm_sidr_rep_handler(work);
3299 break;
3300 case IB_CM_LAP_RECEIVED:
3301 ret = cm_lap_handler(work);
3302 break;
3303 case IB_CM_APR_RECEIVED:
3304 ret = cm_apr_handler(work);
3305 break;
3306 case IB_CM_TIMEWAIT_EXIT:
3307 ret = cm_timewait_handler(work);
3308 break;
3309 default:
3310 ret = -EINVAL;
3311 break;
3312 }
3313 if (ret)
3314 cm_free_work(work);
3315}
3316
Sean Heftye1444b52006-11-28 14:57:13 -08003317static int cm_establish(struct ib_cm_id *cm_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -07003318{
3319 struct cm_id_private *cm_id_priv;
3320 struct cm_work *work;
3321 unsigned long flags;
3322 int ret = 0;
Erez Shitritbe4b4992015-06-25 17:13:22 +03003323 struct cm_device *cm_dev;
3324
3325 cm_dev = ib_get_client_data(cm_id->device, &cm_client);
3326 if (!cm_dev)
3327 return -ENODEV;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003328
3329 work = kmalloc(sizeof *work, GFP_ATOMIC);
3330 if (!work)
3331 return -ENOMEM;
3332
3333 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3334 spin_lock_irqsave(&cm_id_priv->lock, flags);
3335 switch (cm_id->state)
3336 {
3337 case IB_CM_REP_SENT:
3338 case IB_CM_MRA_REP_RCVD:
3339 cm_id->state = IB_CM_ESTABLISHED;
3340 break;
3341 case IB_CM_ESTABLISHED:
3342 ret = -EISCONN;
3343 break;
3344 default:
3345 ret = -EINVAL;
3346 break;
3347 }
3348 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3349
3350 if (ret) {
3351 kfree(work);
3352 goto out;
3353 }
3354
3355 /*
3356 * The CM worker thread may try to destroy the cm_id before it
3357 * can execute this work item. To prevent potential deadlock,
3358 * we need to find the cm_id once we're in the context of the
3359 * worker thread, rather than holding a reference on it.
3360 */
David Howellsc4028952006-11-22 14:57:56 +00003361 INIT_DELAYED_WORK(&work->work, cm_work_handler);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003362 work->local_id = cm_id->local_id;
3363 work->remote_id = cm_id->remote_id;
3364 work->mad_recv_wc = NULL;
3365 work->cm_event.event = IB_CM_USER_ESTABLISHED;
Erez Shitritbe4b4992015-06-25 17:13:22 +03003366
3367 /* Check if the device started its remove_one */
3368 spin_lock_irq(&cm.lock);
3369 if (!cm_dev->going_down) {
3370 queue_delayed_work(cm.wq, &work->work, 0);
3371 } else {
3372 kfree(work);
3373 ret = -ENODEV;
3374 }
3375 spin_unlock_irq(&cm.lock);
3376
Hal Rosenstocka9770492005-07-27 11:45:40 -07003377out:
3378 return ret;
3379}
Sean Heftye1444b52006-11-28 14:57:13 -08003380
3381static int cm_migrate(struct ib_cm_id *cm_id)
3382{
3383 struct cm_id_private *cm_id_priv;
3384 unsigned long flags;
3385 int ret = 0;
3386
3387 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3388 spin_lock_irqsave(&cm_id_priv->lock, flags);
3389 if (cm_id->state == IB_CM_ESTABLISHED &&
3390 (cm_id->lap_state == IB_CM_LAP_UNINIT ||
3391 cm_id->lap_state == IB_CM_LAP_IDLE)) {
3392 cm_id->lap_state = IB_CM_LAP_IDLE;
3393 cm_id_priv->av = cm_id_priv->alt_av;
3394 } else
3395 ret = -EINVAL;
3396 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3397
3398 return ret;
3399}
3400
3401int ib_cm_notify(struct ib_cm_id *cm_id, enum ib_event_type event)
3402{
3403 int ret;
3404
3405 switch (event) {
3406 case IB_EVENT_COMM_EST:
3407 ret = cm_establish(cm_id);
3408 break;
3409 case IB_EVENT_PATH_MIG:
3410 ret = cm_migrate(cm_id);
3411 break;
3412 default:
3413 ret = -EINVAL;
3414 }
3415 return ret;
3416}
3417EXPORT_SYMBOL(ib_cm_notify);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003418
3419static void cm_recv_handler(struct ib_mad_agent *mad_agent,
3420 struct ib_mad_recv_wc *mad_recv_wc)
3421{
Sean Hefty9af57b72007-07-16 21:49:35 -07003422 struct cm_port *port = mad_agent->context;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003423 struct cm_work *work;
3424 enum ib_cm_event_type event;
Sean Hefty9af57b72007-07-16 21:49:35 -07003425 u16 attr_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003426 int paths = 0;
Erez Shitritbe4b4992015-06-25 17:13:22 +03003427 int going_down = 0;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003428
3429 switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) {
3430 case CM_REQ_ATTR_ID:
3431 paths = 1 + (((struct cm_req_msg *) mad_recv_wc->recv_buf.mad)->
3432 alt_local_lid != 0);
3433 event = IB_CM_REQ_RECEIVED;
3434 break;
3435 case CM_MRA_ATTR_ID:
3436 event = IB_CM_MRA_RECEIVED;
3437 break;
3438 case CM_REJ_ATTR_ID:
3439 event = IB_CM_REJ_RECEIVED;
3440 break;
3441 case CM_REP_ATTR_ID:
3442 event = IB_CM_REP_RECEIVED;
3443 break;
3444 case CM_RTU_ATTR_ID:
3445 event = IB_CM_RTU_RECEIVED;
3446 break;
3447 case CM_DREQ_ATTR_ID:
3448 event = IB_CM_DREQ_RECEIVED;
3449 break;
3450 case CM_DREP_ATTR_ID:
3451 event = IB_CM_DREP_RECEIVED;
3452 break;
3453 case CM_SIDR_REQ_ATTR_ID:
3454 event = IB_CM_SIDR_REQ_RECEIVED;
3455 break;
3456 case CM_SIDR_REP_ATTR_ID:
3457 event = IB_CM_SIDR_REP_RECEIVED;
3458 break;
3459 case CM_LAP_ATTR_ID:
3460 paths = 1;
3461 event = IB_CM_LAP_RECEIVED;
3462 break;
3463 case CM_APR_ATTR_ID:
3464 event = IB_CM_APR_RECEIVED;
3465 break;
3466 default:
3467 ib_free_recv_mad(mad_recv_wc);
3468 return;
3469 }
3470
Sean Hefty9af57b72007-07-16 21:49:35 -07003471 attr_id = be16_to_cpu(mad_recv_wc->recv_buf.mad->mad_hdr.attr_id);
3472 atomic_long_inc(&port->counter_group[CM_RECV].
3473 counter[attr_id - CM_ATTR_ID_OFFSET]);
3474
Hal Rosenstocka9770492005-07-27 11:45:40 -07003475 work = kmalloc(sizeof *work + sizeof(struct ib_sa_path_rec) * paths,
3476 GFP_KERNEL);
3477 if (!work) {
3478 ib_free_recv_mad(mad_recv_wc);
3479 return;
3480 }
3481
David Howellsc4028952006-11-22 14:57:56 +00003482 INIT_DELAYED_WORK(&work->work, cm_work_handler);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003483 work->cm_event.event = event;
3484 work->mad_recv_wc = mad_recv_wc;
Sean Hefty9af57b72007-07-16 21:49:35 -07003485 work->port = port;
Erez Shitritbe4b4992015-06-25 17:13:22 +03003486
3487 /* Check if the device started its remove_one */
3488 spin_lock_irq(&cm.lock);
3489 if (!port->cm_dev->going_down)
3490 queue_delayed_work(cm.wq, &work->work, 0);
3491 else
3492 going_down = 1;
3493 spin_unlock_irq(&cm.lock);
3494
3495 if (going_down) {
3496 kfree(work);
3497 ib_free_recv_mad(mad_recv_wc);
3498 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07003499}
3500
3501static int cm_init_qp_init_attr(struct cm_id_private *cm_id_priv,
3502 struct ib_qp_attr *qp_attr,
3503 int *qp_attr_mask)
3504{
3505 unsigned long flags;
3506 int ret;
3507
3508 spin_lock_irqsave(&cm_id_priv->lock, flags);
3509 switch (cm_id_priv->id.state) {
3510 case IB_CM_REQ_SENT:
3511 case IB_CM_MRA_REQ_RCVD:
3512 case IB_CM_REQ_RCVD:
3513 case IB_CM_MRA_REQ_SENT:
3514 case IB_CM_REP_RCVD:
3515 case IB_CM_MRA_REP_SENT:
3516 case IB_CM_REP_SENT:
3517 case IB_CM_MRA_REP_RCVD:
3518 case IB_CM_ESTABLISHED:
3519 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS |
3520 IB_QP_PKEY_INDEX | IB_QP_PORT;
Dotan Barake31353ea2006-10-24 13:35:27 -07003521 qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003522 if (cm_id_priv->responder_resources)
Sean Heftyc1f250c2006-08-28 11:55:52 -07003523 qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ |
3524 IB_ACCESS_REMOTE_ATOMIC;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003525 qp_attr->pkey_index = cm_id_priv->av.pkey_index;
3526 qp_attr->port_num = cm_id_priv->av.port->port_num;
3527 ret = 0;
3528 break;
3529 default:
3530 ret = -EINVAL;
3531 break;
3532 }
3533 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3534 return ret;
3535}
3536
3537static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv,
3538 struct ib_qp_attr *qp_attr,
3539 int *qp_attr_mask)
3540{
3541 unsigned long flags;
3542 int ret;
3543
3544 spin_lock_irqsave(&cm_id_priv->lock, flags);
3545 switch (cm_id_priv->id.state) {
3546 case IB_CM_REQ_RCVD:
3547 case IB_CM_MRA_REQ_SENT:
3548 case IB_CM_REP_RCVD:
3549 case IB_CM_MRA_REP_SENT:
3550 case IB_CM_REP_SENT:
3551 case IB_CM_MRA_REP_RCVD:
3552 case IB_CM_ESTABLISHED:
3553 *qp_attr_mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU |
Sean Heftyae7971a2005-10-24 12:33:56 -07003554 IB_QP_DEST_QPN | IB_QP_RQ_PSN;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003555 qp_attr->ah_attr = cm_id_priv->av.ah_attr;
Wei Yongjun990acea2014-01-16 16:28:13 +08003556 if (!cm_id_priv->av.valid) {
3557 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
Matan Barakdd5f03b2013-12-12 18:03:11 +02003558 return -EINVAL;
Wei Yongjun990acea2014-01-16 16:28:13 +08003559 }
Matan Barakdd5f03b2013-12-12 18:03:11 +02003560 if (cm_id_priv->av.ah_attr.vlan_id != 0xffff) {
3561 qp_attr->vlan_id = cm_id_priv->av.ah_attr.vlan_id;
3562 *qp_attr_mask |= IB_QP_VID;
3563 }
3564 if (!is_zero_ether_addr(cm_id_priv->av.smac)) {
3565 memcpy(qp_attr->smac, cm_id_priv->av.smac,
3566 sizeof(qp_attr->smac));
3567 *qp_attr_mask |= IB_QP_SMAC;
3568 }
3569 if (cm_id_priv->alt_av.valid) {
3570 if (cm_id_priv->alt_av.ah_attr.vlan_id != 0xffff) {
3571 qp_attr->alt_vlan_id =
3572 cm_id_priv->alt_av.ah_attr.vlan_id;
3573 *qp_attr_mask |= IB_QP_ALT_VID;
3574 }
3575 if (!is_zero_ether_addr(cm_id_priv->alt_av.smac)) {
3576 memcpy(qp_attr->alt_smac,
3577 cm_id_priv->alt_av.smac,
3578 sizeof(qp_attr->alt_smac));
3579 *qp_attr_mask |= IB_QP_ALT_SMAC;
3580 }
3581 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07003582 qp_attr->path_mtu = cm_id_priv->path_mtu;
3583 qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn);
3584 qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn);
Sean Heftyd26a3602011-05-13 10:46:20 -07003585 if (cm_id_priv->qp_type == IB_QPT_RC ||
3586 cm_id_priv->qp_type == IB_QPT_XRC_TGT) {
Sean Heftyae7971a2005-10-24 12:33:56 -07003587 *qp_attr_mask |= IB_QP_MAX_DEST_RD_ATOMIC |
3588 IB_QP_MIN_RNR_TIMER;
3589 qp_attr->max_dest_rd_atomic =
3590 cm_id_priv->responder_resources;
3591 qp_attr->min_rnr_timer = 0;
3592 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07003593 if (cm_id_priv->alt_av.ah_attr.dlid) {
3594 *qp_attr_mask |= IB_QP_ALT_PATH;
Sean Hefty0d8fdfd2006-06-30 14:10:14 -07003595 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
Sean Heftye1444b52006-11-28 14:57:13 -08003596 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
Sean Hefty1d846122007-06-18 11:09:37 -07003597 qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003598 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3599 }
3600 ret = 0;
3601 break;
3602 default:
3603 ret = -EINVAL;
3604 break;
3605 }
3606 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3607 return ret;
3608}
3609
3610static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv,
3611 struct ib_qp_attr *qp_attr,
3612 int *qp_attr_mask)
3613{
3614 unsigned long flags;
3615 int ret;
3616
3617 spin_lock_irqsave(&cm_id_priv->lock, flags);
3618 switch (cm_id_priv->id.state) {
Sean Hefty0fe313b2006-11-30 16:37:15 -08003619 /* Allow transition to RTS before sending REP */
3620 case IB_CM_REQ_RCVD:
3621 case IB_CM_MRA_REQ_SENT:
3622
Hal Rosenstocka9770492005-07-27 11:45:40 -07003623 case IB_CM_REP_RCVD:
3624 case IB_CM_MRA_REP_SENT:
3625 case IB_CM_REP_SENT:
3626 case IB_CM_MRA_REP_RCVD:
3627 case IB_CM_ESTABLISHED:
Sean Heftye1444b52006-11-28 14:57:13 -08003628 if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT) {
3629 *qp_attr_mask = IB_QP_STATE | IB_QP_SQ_PSN;
3630 qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn);
Sean Heftyd26a3602011-05-13 10:46:20 -07003631 switch (cm_id_priv->qp_type) {
3632 case IB_QPT_RC:
3633 case IB_QPT_XRC_INI:
3634 *qp_attr_mask |= IB_QP_RETRY_CNT | IB_QP_RNR_RETRY |
Sean Heftye1444b52006-11-28 14:57:13 -08003635 IB_QP_MAX_QP_RD_ATOMIC;
Sean Heftye1444b52006-11-28 14:57:13 -08003636 qp_attr->retry_cnt = cm_id_priv->retry_count;
3637 qp_attr->rnr_retry = cm_id_priv->rnr_retry_count;
Sean Heftyd26a3602011-05-13 10:46:20 -07003638 qp_attr->max_rd_atomic = cm_id_priv->initiator_depth;
3639 /* fall through */
3640 case IB_QPT_XRC_TGT:
3641 *qp_attr_mask |= IB_QP_TIMEOUT;
3642 qp_attr->timeout = cm_id_priv->av.timeout;
3643 break;
3644 default:
3645 break;
Sean Heftye1444b52006-11-28 14:57:13 -08003646 }
3647 if (cm_id_priv->alt_av.ah_attr.dlid) {
3648 *qp_attr_mask |= IB_QP_PATH_MIG_STATE;
3649 qp_attr->path_mig_state = IB_MIG_REARM;
3650 }
3651 } else {
3652 *qp_attr_mask = IB_QP_ALT_PATH | IB_QP_PATH_MIG_STATE;
3653 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
3654 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
Sean Hefty1d846122007-06-18 11:09:37 -07003655 qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
Sean Heftye1444b52006-11-28 14:57:13 -08003656 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003657 qp_attr->path_mig_state = IB_MIG_REARM;
3658 }
3659 ret = 0;
3660 break;
3661 default:
3662 ret = -EINVAL;
3663 break;
3664 }
3665 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3666 return ret;
3667}
3668
3669int ib_cm_init_qp_attr(struct ib_cm_id *cm_id,
3670 struct ib_qp_attr *qp_attr,
3671 int *qp_attr_mask)
3672{
3673 struct cm_id_private *cm_id_priv;
3674 int ret;
3675
3676 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3677 switch (qp_attr->qp_state) {
3678 case IB_QPS_INIT:
3679 ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask);
3680 break;
3681 case IB_QPS_RTR:
3682 ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask);
3683 break;
3684 case IB_QPS_RTS:
3685 ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask);
3686 break;
3687 default:
3688 ret = -EINVAL;
3689 break;
3690 }
3691 return ret;
3692}
3693EXPORT_SYMBOL(ib_cm_init_qp_attr);
3694
Roland Dreier454a01e2007-07-17 18:37:43 -07003695static void cm_get_ack_delay(struct cm_device *cm_dev)
Sean Hefty1d846122007-06-18 11:09:37 -07003696{
3697 struct ib_device_attr attr;
3698
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003699 if (ib_query_device(cm_dev->ib_device, &attr))
Sean Hefty1d846122007-06-18 11:09:37 -07003700 cm_dev->ack_delay = 0; /* acks will rely on packet life time */
3701 else
3702 cm_dev->ack_delay = attr.local_ca_ack_delay;
3703}
3704
Sean Hefty9af57b72007-07-16 21:49:35 -07003705static ssize_t cm_show_counter(struct kobject *obj, struct attribute *attr,
3706 char *buf)
3707{
3708 struct cm_counter_group *group;
3709 struct cm_counter_attribute *cm_attr;
3710
3711 group = container_of(obj, struct cm_counter_group, obj);
3712 cm_attr = container_of(attr, struct cm_counter_attribute, attr);
3713
3714 return sprintf(buf, "%ld\n",
3715 atomic_long_read(&group->counter[cm_attr->index]));
3716}
3717
Emese Revfy52cf25d2010-01-19 02:58:23 +01003718static const struct sysfs_ops cm_counter_ops = {
Sean Hefty9af57b72007-07-16 21:49:35 -07003719 .show = cm_show_counter
3720};
3721
3722static struct kobj_type cm_counter_obj_type = {
3723 .sysfs_ops = &cm_counter_ops,
3724 .default_attrs = cm_counter_default_attrs
3725};
3726
3727static void cm_release_port_obj(struct kobject *obj)
3728{
3729 struct cm_port *cm_port;
3730
Sean Hefty9af57b72007-07-16 21:49:35 -07003731 cm_port = container_of(obj, struct cm_port, port_obj);
3732 kfree(cm_port);
3733}
3734
3735static struct kobj_type cm_port_obj_type = {
3736 .release = cm_release_port_obj
3737};
3738
Al Viro2c9ede52011-07-23 20:24:48 -04003739static char *cm_devnode(struct device *dev, umode_t *mode)
Roland Dreierc3af0982011-05-23 11:21:47 -07003740{
Goldwyn Rodriguesb2bc4782011-07-04 09:26:57 -07003741 if (mode)
3742 *mode = 0666;
Roland Dreierc3af0982011-05-23 11:21:47 -07003743 return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
3744}
3745
Sean Hefty9af57b72007-07-16 21:49:35 -07003746struct class cm_class = {
Roland Dreierc3af0982011-05-23 11:21:47 -07003747 .owner = THIS_MODULE,
Sean Hefty9af57b72007-07-16 21:49:35 -07003748 .name = "infiniband_cm",
Roland Dreierc3af0982011-05-23 11:21:47 -07003749 .devnode = cm_devnode,
Sean Hefty9af57b72007-07-16 21:49:35 -07003750};
3751EXPORT_SYMBOL(cm_class);
3752
Sean Hefty9af57b72007-07-16 21:49:35 -07003753static int cm_create_port_fs(struct cm_port *port)
3754{
3755 int i, ret;
3756
3757 ret = kobject_init_and_add(&port->port_obj, &cm_port_obj_type,
Greg Kroah-Hartman110cf372008-05-27 10:17:53 +03003758 &port->cm_dev->device->kobj,
Sean Hefty9af57b72007-07-16 21:49:35 -07003759 "%d", port->port_num);
3760 if (ret) {
3761 kfree(port);
3762 return ret;
3763 }
3764
3765 for (i = 0; i < CM_COUNTER_GROUPS; i++) {
3766 ret = kobject_init_and_add(&port->counter_group[i].obj,
3767 &cm_counter_obj_type,
Roland Dreier7c7a9bc2008-02-12 14:38:27 -08003768 &port->port_obj,
Sean Hefty9af57b72007-07-16 21:49:35 -07003769 "%s", counter_group_names[i]);
3770 if (ret)
3771 goto error;
3772 }
3773
3774 return 0;
3775
3776error:
3777 while (i--)
Roland Dreier7c7a9bc2008-02-12 14:38:27 -08003778 kobject_put(&port->counter_group[i].obj);
3779 kobject_put(&port->port_obj);
Sean Hefty9af57b72007-07-16 21:49:35 -07003780 return ret;
3781
3782}
3783
3784static void cm_remove_port_fs(struct cm_port *port)
3785{
3786 int i;
3787
3788 for (i = 0; i < CM_COUNTER_GROUPS; i++)
Roland Dreier7c7a9bc2008-02-12 14:38:27 -08003789 kobject_put(&port->counter_group[i].obj);
Sean Hefty9af57b72007-07-16 21:49:35 -07003790
Roland Dreier7c7a9bc2008-02-12 14:38:27 -08003791 kobject_put(&port->port_obj);
Sean Hefty9af57b72007-07-16 21:49:35 -07003792}
3793
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003794static void cm_add_one(struct ib_device *ib_device)
Hal Rosenstocka9770492005-07-27 11:45:40 -07003795{
3796 struct cm_device *cm_dev;
3797 struct cm_port *port;
3798 struct ib_mad_reg_req reg_req = {
3799 .mgmt_class = IB_MGMT_CLASS_CM,
Ira Weiny0f29b462014-08-08 19:00:55 -04003800 .mgmt_class_version = IB_CM_CLASS_VERSION,
Hal Rosenstocka9770492005-07-27 11:45:40 -07003801 };
3802 struct ib_port_modify port_modify = {
3803 .set_port_cap_mask = IB_PORT_CM_SUP
3804 };
3805 unsigned long flags;
3806 int ret;
Michael Wang091e6a42015-05-05 14:50:21 +02003807 int count = 0;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003808 u8 i;
3809
Sean Hefty9af57b72007-07-16 21:49:35 -07003810 cm_dev = kzalloc(sizeof(*cm_dev) + sizeof(*port) *
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003811 ib_device->phys_port_cnt, GFP_KERNEL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003812 if (!cm_dev)
3813 return;
3814
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003815 cm_dev->ib_device = ib_device;
Sean Hefty1d846122007-06-18 11:09:37 -07003816 cm_get_ack_delay(cm_dev);
Erez Shitritbe4b4992015-06-25 17:13:22 +03003817 cm_dev->going_down = 0;
Greg Kroah-Hartman91bd4182008-07-21 20:03:34 -07003818 cm_dev->device = device_create(&cm_class, &ib_device->dev,
3819 MKDEV(0, 0), NULL,
3820 "%s", ib_device->name);
Jani Nikula3e340c02010-03-11 16:17:28 +00003821 if (IS_ERR(cm_dev->device)) {
Sean Hefty9af57b72007-07-16 21:49:35 -07003822 kfree(cm_dev);
3823 return;
3824 }
3825
Hal Rosenstocka9770492005-07-27 11:45:40 -07003826 set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask);
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003827 for (i = 1; i <= ib_device->phys_port_cnt; i++) {
Michael Wang72219cea2015-05-05 14:50:34 +02003828 if (!rdma_cap_ib_cm(ib_device, i))
Michael Wang091e6a42015-05-05 14:50:21 +02003829 continue;
3830
Sean Hefty9af57b72007-07-16 21:49:35 -07003831 port = kzalloc(sizeof *port, GFP_KERNEL);
3832 if (!port)
3833 goto error1;
3834
3835 cm_dev->port[i-1] = port;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003836 port->cm_dev = cm_dev;
3837 port->port_num = i;
Sean Hefty9af57b72007-07-16 21:49:35 -07003838
3839 ret = cm_create_port_fs(port);
3840 if (ret)
3841 goto error1;
3842
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003843 port->mad_agent = ib_register_mad_agent(ib_device, i,
Hal Rosenstocka9770492005-07-27 11:45:40 -07003844 IB_QPT_GSI,
3845 &reg_req,
3846 0,
3847 cm_send_handler,
3848 cm_recv_handler,
Ira Weiny0f29b462014-08-08 19:00:55 -04003849 port,
3850 0);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003851 if (IS_ERR(port->mad_agent))
Sean Hefty9af57b72007-07-16 21:49:35 -07003852 goto error2;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003853
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003854 ret = ib_modify_port(ib_device, i, 0, &port_modify);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003855 if (ret)
Sean Hefty9af57b72007-07-16 21:49:35 -07003856 goto error3;
Michael Wang091e6a42015-05-05 14:50:21 +02003857
3858 count++;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003859 }
Michael Wang091e6a42015-05-05 14:50:21 +02003860
3861 if (!count)
3862 goto free;
3863
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003864 ib_set_client_data(ib_device, &cm_client, cm_dev);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003865
3866 write_lock_irqsave(&cm.device_lock, flags);
3867 list_add_tail(&cm_dev->list, &cm.device_list);
3868 write_unlock_irqrestore(&cm.device_lock, flags);
3869 return;
3870
Sean Hefty9af57b72007-07-16 21:49:35 -07003871error3:
Sean Heftycf311cd2006-01-10 07:39:34 -08003872 ib_unregister_mad_agent(port->mad_agent);
Sean Hefty9af57b72007-07-16 21:49:35 -07003873error2:
3874 cm_remove_port_fs(port);
Sean Heftycf311cd2006-01-10 07:39:34 -08003875error1:
Hal Rosenstocka9770492005-07-27 11:45:40 -07003876 port_modify.set_port_cap_mask = 0;
3877 port_modify.clr_port_cap_mask = IB_PORT_CM_SUP;
3878 while (--i) {
Michael Wang72219cea2015-05-05 14:50:34 +02003879 if (!rdma_cap_ib_cm(ib_device, i))
Michael Wang091e6a42015-05-05 14:50:21 +02003880 continue;
3881
Sean Hefty9af57b72007-07-16 21:49:35 -07003882 port = cm_dev->port[i-1];
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003883 ib_modify_port(ib_device, port->port_num, 0, &port_modify);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003884 ib_unregister_mad_agent(port->mad_agent);
Sean Hefty9af57b72007-07-16 21:49:35 -07003885 cm_remove_port_fs(port);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003886 }
Michael Wang091e6a42015-05-05 14:50:21 +02003887free:
Greg Kroah-Hartman110cf372008-05-27 10:17:53 +03003888 device_unregister(cm_dev->device);
Hefty, Seana7e80ce2008-09-30 10:36:54 -07003889 kfree(cm_dev);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003890}
3891
Haggai Eran7c1eb452015-07-30 17:50:14 +03003892static void cm_remove_one(struct ib_device *ib_device, void *client_data)
Hal Rosenstocka9770492005-07-27 11:45:40 -07003893{
Haggai Eran7c1eb452015-07-30 17:50:14 +03003894 struct cm_device *cm_dev = client_data;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003895 struct cm_port *port;
3896 struct ib_port_modify port_modify = {
3897 .clr_port_cap_mask = IB_PORT_CM_SUP
3898 };
3899 unsigned long flags;
3900 int i;
3901
Hal Rosenstocka9770492005-07-27 11:45:40 -07003902 if (!cm_dev)
3903 return;
3904
3905 write_lock_irqsave(&cm.device_lock, flags);
3906 list_del(&cm_dev->list);
3907 write_unlock_irqrestore(&cm.device_lock, flags);
3908
Erez Shitritbe4b4992015-06-25 17:13:22 +03003909 spin_lock_irq(&cm.lock);
3910 cm_dev->going_down = 1;
3911 spin_unlock_irq(&cm.lock);
3912
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003913 for (i = 1; i <= ib_device->phys_port_cnt; i++) {
Michael Wang72219cea2015-05-05 14:50:34 +02003914 if (!rdma_cap_ib_cm(ib_device, i))
Michael Wang091e6a42015-05-05 14:50:21 +02003915 continue;
3916
Sean Hefty9af57b72007-07-16 21:49:35 -07003917 port = cm_dev->port[i-1];
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003918 ib_modify_port(ib_device, port->port_num, 0, &port_modify);
Erez Shitritbe4b4992015-06-25 17:13:22 +03003919 /*
3920 * We flush the queue here after the going_down set, this
3921 * verify that no new works will be queued in the recv handler,
3922 * after that we can call the unregister_mad_agent
3923 */
Sean Hefty84ba2842008-02-22 10:40:45 -08003924 flush_workqueue(cm.wq);
Erez Shitritbe4b4992015-06-25 17:13:22 +03003925 ib_unregister_mad_agent(port->mad_agent);
Sean Hefty9af57b72007-07-16 21:49:35 -07003926 cm_remove_port_fs(port);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003927 }
Greg Kroah-Hartman110cf372008-05-27 10:17:53 +03003928 device_unregister(cm_dev->device);
Hefty, Seana7e80ce2008-09-30 10:36:54 -07003929 kfree(cm_dev);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003930}
3931
3932static int __init ib_cm_init(void)
3933{
3934 int ret;
3935
3936 memset(&cm, 0, sizeof cm);
3937 INIT_LIST_HEAD(&cm.device_list);
3938 rwlock_init(&cm.device_lock);
3939 spin_lock_init(&cm.lock);
3940 cm.listen_service_table = RB_ROOT;
Harvey Harrison9c3da092009-01-17 17:11:57 -08003941 cm.listen_service_id = be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003942 cm.remote_id_table = RB_ROOT;
3943 cm.remote_qp_table = RB_ROOT;
3944 cm.remote_sidr_table = RB_ROOT;
3945 idr_init(&cm.local_id_table);
Sean Heftyf06d2652006-08-28 15:15:18 -07003946 get_random_bytes(&cm.random_id_operand, sizeof cm.random_id_operand);
Sean Hefty85753292006-10-04 11:29:59 -07003947 INIT_LIST_HEAD(&cm.timewait_list);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003948
Sean Hefty9af57b72007-07-16 21:49:35 -07003949 ret = class_register(&cm_class);
Dotan Barak87d4abd2012-07-11 15:39:31 +00003950 if (ret) {
Sean Hefty9af57b72007-07-16 21:49:35 -07003951 ret = -ENOMEM;
3952 goto error1;
3953 }
3954
Dotan Barak87d4abd2012-07-11 15:39:31 +00003955 cm.wq = create_workqueue("ib_cm");
3956 if (!cm.wq) {
3957 ret = -ENOMEM;
3958 goto error2;
3959 }
3960
Hal Rosenstocka9770492005-07-27 11:45:40 -07003961 ret = ib_register_client(&cm_client);
3962 if (ret)
Dotan Barak87d4abd2012-07-11 15:39:31 +00003963 goto error3;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003964
3965 return 0;
Dotan Barak87d4abd2012-07-11 15:39:31 +00003966error3:
Hal Rosenstocka9770492005-07-27 11:45:40 -07003967 destroy_workqueue(cm.wq);
Dotan Barak87d4abd2012-07-11 15:39:31 +00003968error2:
Sean Hefty9af57b72007-07-16 21:49:35 -07003969 class_unregister(&cm_class);
Dotan Barak87d4abd2012-07-11 15:39:31 +00003970error1:
3971 idr_destroy(&cm.local_id_table);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003972 return ret;
3973}
3974
3975static void __exit ib_cm_cleanup(void)
3976{
Sean Hefty85753292006-10-04 11:29:59 -07003977 struct cm_timewait_info *timewait_info, *tmp;
3978
3979 spin_lock_irq(&cm.lock);
3980 list_for_each_entry(timewait_info, &cm.timewait_list, list)
3981 cancel_delayed_work(&timewait_info->work.work);
3982 spin_unlock_irq(&cm.lock);
3983
Sean Hefty84ba2842008-02-22 10:40:45 -08003984 ib_unregister_client(&cm_client);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003985 destroy_workqueue(cm.wq);
Sean Hefty85753292006-10-04 11:29:59 -07003986
3987 list_for_each_entry_safe(timewait_info, tmp, &cm.timewait_list, list) {
3988 list_del(&timewait_info->list);
3989 kfree(timewait_info);
3990 }
3991
Sean Hefty9af57b72007-07-16 21:49:35 -07003992 class_unregister(&cm_class);
Roland Dreier5d7edb32005-10-24 10:53:25 -07003993 idr_destroy(&cm.local_id_table);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003994}
3995
3996module_init(ib_cm_init);
3997module_exit(ib_cm_cleanup);
3998