blob: d883a322fc655827d1514429f4c0e36c2c09abab [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;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700182};
183
184struct cm_work {
David Howellsc4028952006-11-22 14:57:56 +0000185 struct delayed_work work;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700186 struct list_head list;
187 struct cm_port *port;
188 struct ib_mad_recv_wc *mad_recv_wc; /* Received MADs */
Sean Hefty97f52eb2005-08-13 21:05:57 -0700189 __be32 local_id; /* Established / timewait */
190 __be32 remote_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700191 struct ib_cm_event cm_event;
192 struct ib_sa_path_rec path[0];
193};
194
195struct cm_timewait_info {
196 struct cm_work work; /* Must be first. */
Sean Hefty85753292006-10-04 11:29:59 -0700197 struct list_head list;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700198 struct rb_node remote_qp_node;
199 struct rb_node remote_id_node;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700200 __be64 remote_ca_guid;
201 __be32 remote_qpn;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700202 u8 inserted_remote_qp;
203 u8 inserted_remote_id;
204};
205
206struct cm_id_private {
207 struct ib_cm_id id;
208
209 struct rb_node service_node;
210 struct rb_node sidr_id_node;
Sean Hefty87fd1a12006-03-02 16:50:37 -0800211 spinlock_t lock; /* Do not acquire inside cm.lock */
Sean Hefty1b52fa982006-05-12 14:57:52 -0700212 struct completion comp;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700213 atomic_t refcount;
Haggai Eran067b1712015-07-30 17:50:18 +0300214 /* Number of clients sharing this ib_cm_id. Only valid for listeners.
215 * Protected by the cm.lock spinlock. */
216 int listen_sharecount;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700217
218 struct ib_mad_send_buf *msg;
219 struct cm_timewait_info *timewait_info;
220 /* todo: use alternate port on send failure */
221 struct cm_av av;
222 struct cm_av alt_av;
223
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;
Matan Barakc2c6ff12015-10-15 18:38:48 +0300362 struct net_device *ndev = ib_get_ndev_from_path(path);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700363
364 read_lock_irqsave(&cm.device_lock, flags);
365 list_for_each_entry(cm_dev, &cm.device_list, list) {
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +0300366 if (!ib_find_cached_gid(cm_dev->ib_device, &path->sgid,
Matan Barakcb57bb82015-12-23 14:56:48 +0200367 path->gid_type, ndev, &p, NULL)) {
Sean Hefty9af57b72007-07-16 21:49:35 -0700368 port = cm_dev->port[p-1];
Hal Rosenstocka9770492005-07-27 11:45:40 -0700369 break;
370 }
371 }
372 read_unlock_irqrestore(&cm.device_lock, flags);
373
Matan Barakc2c6ff12015-10-15 18:38:48 +0300374 if (ndev)
375 dev_put(ndev);
376
Hal Rosenstocka9770492005-07-27 11:45:40 -0700377 if (!port)
378 return -EINVAL;
379
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +0300380 ret = ib_find_cached_pkey(cm_dev->ib_device, port->port_num,
Hal Rosenstocka9770492005-07-27 11:45:40 -0700381 be16_to_cpu(path->pkey), &av->pkey_index);
382 if (ret)
383 return ret;
384
385 av->port = port;
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +0300386 ib_init_ah_from_path(cm_dev->ib_device, port->port_num, path,
Sean Heftyca222c62006-06-17 20:37:40 -0700387 &av->ah_attr);
Sean Hefty1d846122007-06-18 11:09:37 -0700388 av->timeout = path->packet_life_time + 1;
Matan Barakdd5f03b2013-12-12 18:03:11 +0200389
Hal Rosenstocka9770492005-07-27 11:45:40 -0700390 return 0;
391}
392
393static int cm_alloc_id(struct cm_id_private *cm_id_priv)
394{
395 unsigned long flags;
Tejun Heo3b069c52013-02-27 17:04:16 -0800396 int id;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700397
Tejun Heo3b069c52013-02-27 17:04:16 -0800398 idr_preload(GFP_KERNEL);
399 spin_lock_irqsave(&cm.lock, flags);
400
Zhao Hongjiangab626d12013-11-15 14:16:37 -0800401 id = idr_alloc_cyclic(&cm.local_id_table, cm_id_priv, 0, 0, GFP_NOWAIT);
Tejun Heo3b069c52013-02-27 17:04:16 -0800402
403 spin_unlock_irqrestore(&cm.lock, flags);
404 idr_preload_end();
Sean Heftyf06d2652006-08-28 15:15:18 -0700405
Al Viro1b90c132008-03-29 03:10:28 +0000406 cm_id_priv->id.local_id = (__force __be32)id ^ cm.random_id_operand;
Tejun Heo3b069c52013-02-27 17:04:16 -0800407 return id < 0 ? id : 0;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700408}
409
Sean Hefty97f52eb2005-08-13 21:05:57 -0700410static void cm_free_id(__be32 local_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700411{
Sean Hefty24be6e82007-06-18 11:09:36 -0700412 spin_lock_irq(&cm.lock);
Sean Heftyf06d2652006-08-28 15:15:18 -0700413 idr_remove(&cm.local_id_table,
414 (__force int) (local_id ^ cm.random_id_operand));
Sean Hefty24be6e82007-06-18 11:09:36 -0700415 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700416}
417
Sean Hefty97f52eb2005-08-13 21:05:57 -0700418static struct cm_id_private * cm_get_id(__be32 local_id, __be32 remote_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700419{
420 struct cm_id_private *cm_id_priv;
421
Sean Heftyf06d2652006-08-28 15:15:18 -0700422 cm_id_priv = idr_find(&cm.local_id_table,
423 (__force int) (local_id ^ cm.random_id_operand));
Hal Rosenstocka9770492005-07-27 11:45:40 -0700424 if (cm_id_priv) {
425 if (cm_id_priv->id.remote_id == remote_id)
426 atomic_inc(&cm_id_priv->refcount);
427 else
428 cm_id_priv = NULL;
429 }
430
431 return cm_id_priv;
432}
433
Sean Hefty97f52eb2005-08-13 21:05:57 -0700434static struct cm_id_private * cm_acquire_id(__be32 local_id, __be32 remote_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700435{
436 struct cm_id_private *cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700437
Sean Hefty24be6e82007-06-18 11:09:36 -0700438 spin_lock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700439 cm_id_priv = cm_get_id(local_id, remote_id);
Sean Hefty24be6e82007-06-18 11:09:36 -0700440 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700441
442 return cm_id_priv;
443}
444
Roland Dreiera88f4882008-04-16 21:01:07 -0700445/*
446 * Trivial helpers to strip endian annotation and compare; the
447 * endianness doesn't actually matter since we just need a stable
448 * order for the RB tree.
449 */
450static int be32_lt(__be32 a, __be32 b)
451{
452 return (__force u32) a < (__force u32) b;
453}
454
455static int be32_gt(__be32 a, __be32 b)
456{
457 return (__force u32) a > (__force u32) b;
458}
459
460static int be64_lt(__be64 a, __be64 b)
461{
462 return (__force u64) a < (__force u64) b;
463}
464
465static int be64_gt(__be64 a, __be64 b)
466{
467 return (__force u64) a > (__force u64) b;
468}
469
Hal Rosenstocka9770492005-07-27 11:45:40 -0700470static struct cm_id_private * cm_insert_listen(struct cm_id_private *cm_id_priv)
471{
472 struct rb_node **link = &cm.listen_service_table.rb_node;
473 struct rb_node *parent = NULL;
474 struct cm_id_private *cur_cm_id_priv;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700475 __be64 service_id = cm_id_priv->id.service_id;
476 __be64 service_mask = cm_id_priv->id.service_mask;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700477
478 while (*link) {
479 parent = *link;
480 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
481 service_node);
482 if ((cur_cm_id_priv->id.service_mask & service_id) ==
Sean Hefty07d357d2005-10-17 15:37:43 -0700483 (service_mask & cur_cm_id_priv->id.service_id) &&
Haggai Eran73fec7f2015-07-30 17:50:26 +0300484 (cm_id_priv->id.device == cur_cm_id_priv->id.device))
Sean Hefty07d357d2005-10-17 15:37:43 -0700485 return cur_cm_id_priv;
486
487 if (cm_id_priv->id.device < cur_cm_id_priv->id.device)
488 link = &(*link)->rb_left;
489 else if (cm_id_priv->id.device > cur_cm_id_priv->id.device)
490 link = &(*link)->rb_right;
Roland Dreiera88f4882008-04-16 21:01:07 -0700491 else if (be64_lt(service_id, cur_cm_id_priv->id.service_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700492 link = &(*link)->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700493 else if (be64_gt(service_id, cur_cm_id_priv->id.service_id))
Sean Hefty6e61d042006-06-17 20:37:28 -0700494 link = &(*link)->rb_right;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700495 else
496 link = &(*link)->rb_right;
497 }
498 rb_link_node(&cm_id_priv->service_node, parent, link);
499 rb_insert_color(&cm_id_priv->service_node, &cm.listen_service_table);
500 return NULL;
501}
502
Sean Hefty07d357d2005-10-17 15:37:43 -0700503static struct cm_id_private * cm_find_listen(struct ib_device *device,
Haggai Eran73fec7f2015-07-30 17:50:26 +0300504 __be64 service_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700505{
506 struct rb_node *node = cm.listen_service_table.rb_node;
507 struct cm_id_private *cm_id_priv;
508
509 while (node) {
510 cm_id_priv = rb_entry(node, struct cm_id_private, service_node);
511 if ((cm_id_priv->id.service_mask & service_id) ==
Sean Hefty07d357d2005-10-17 15:37:43 -0700512 cm_id_priv->id.service_id &&
Haggai Eran73fec7f2015-07-30 17:50:26 +0300513 (cm_id_priv->id.device == device))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700514 return cm_id_priv;
Sean Hefty07d357d2005-10-17 15:37:43 -0700515
516 if (device < cm_id_priv->id.device)
517 node = node->rb_left;
518 else if (device > cm_id_priv->id.device)
519 node = node->rb_right;
Roland Dreiera88f4882008-04-16 21:01:07 -0700520 else if (be64_lt(service_id, cm_id_priv->id.service_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700521 node = node->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700522 else if (be64_gt(service_id, cm_id_priv->id.service_id))
Sean Hefty6e61d042006-06-17 20:37:28 -0700523 node = node->rb_right;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700524 else
525 node = node->rb_right;
526 }
527 return NULL;
528}
529
530static struct cm_timewait_info * cm_insert_remote_id(struct cm_timewait_info
531 *timewait_info)
532{
533 struct rb_node **link = &cm.remote_id_table.rb_node;
534 struct rb_node *parent = NULL;
535 struct cm_timewait_info *cur_timewait_info;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700536 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
537 __be32 remote_id = timewait_info->work.remote_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700538
539 while (*link) {
540 parent = *link;
541 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
542 remote_id_node);
Roland Dreiera88f4882008-04-16 21:01:07 -0700543 if (be32_lt(remote_id, cur_timewait_info->work.remote_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700544 link = &(*link)->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700545 else if (be32_gt(remote_id, cur_timewait_info->work.remote_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700546 link = &(*link)->rb_right;
Roland Dreiera88f4882008-04-16 21:01:07 -0700547 else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700548 link = &(*link)->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700549 else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700550 link = &(*link)->rb_right;
551 else
552 return cur_timewait_info;
553 }
554 timewait_info->inserted_remote_id = 1;
555 rb_link_node(&timewait_info->remote_id_node, parent, link);
556 rb_insert_color(&timewait_info->remote_id_node, &cm.remote_id_table);
557 return NULL;
558}
559
Sean Hefty97f52eb2005-08-13 21:05:57 -0700560static struct cm_timewait_info * cm_find_remote_id(__be64 remote_ca_guid,
561 __be32 remote_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700562{
563 struct rb_node *node = cm.remote_id_table.rb_node;
564 struct cm_timewait_info *timewait_info;
565
566 while (node) {
567 timewait_info = rb_entry(node, struct cm_timewait_info,
568 remote_id_node);
Roland Dreiera88f4882008-04-16 21:01:07 -0700569 if (be32_lt(remote_id, timewait_info->work.remote_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700570 node = node->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700571 else if (be32_gt(remote_id, timewait_info->work.remote_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700572 node = node->rb_right;
Roland Dreiera88f4882008-04-16 21:01:07 -0700573 else if (be64_lt(remote_ca_guid, timewait_info->remote_ca_guid))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700574 node = node->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700575 else if (be64_gt(remote_ca_guid, timewait_info->remote_ca_guid))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700576 node = node->rb_right;
577 else
578 return timewait_info;
579 }
580 return NULL;
581}
582
583static struct cm_timewait_info * cm_insert_remote_qpn(struct cm_timewait_info
584 *timewait_info)
585{
586 struct rb_node **link = &cm.remote_qp_table.rb_node;
587 struct rb_node *parent = NULL;
588 struct cm_timewait_info *cur_timewait_info;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700589 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
590 __be32 remote_qpn = timewait_info->remote_qpn;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700591
592 while (*link) {
593 parent = *link;
594 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
595 remote_qp_node);
Roland Dreiera88f4882008-04-16 21:01:07 -0700596 if (be32_lt(remote_qpn, cur_timewait_info->remote_qpn))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700597 link = &(*link)->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700598 else if (be32_gt(remote_qpn, cur_timewait_info->remote_qpn))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700599 link = &(*link)->rb_right;
Roland Dreiera88f4882008-04-16 21:01:07 -0700600 else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700601 link = &(*link)->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700602 else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700603 link = &(*link)->rb_right;
604 else
605 return cur_timewait_info;
606 }
607 timewait_info->inserted_remote_qp = 1;
608 rb_link_node(&timewait_info->remote_qp_node, parent, link);
609 rb_insert_color(&timewait_info->remote_qp_node, &cm.remote_qp_table);
610 return NULL;
611}
612
613static struct cm_id_private * cm_insert_remote_sidr(struct cm_id_private
614 *cm_id_priv)
615{
616 struct rb_node **link = &cm.remote_sidr_table.rb_node;
617 struct rb_node *parent = NULL;
618 struct cm_id_private *cur_cm_id_priv;
619 union ib_gid *port_gid = &cm_id_priv->av.dgid;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700620 __be32 remote_id = cm_id_priv->id.remote_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700621
622 while (*link) {
623 parent = *link;
624 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
625 sidr_id_node);
Roland Dreiera88f4882008-04-16 21:01:07 -0700626 if (be32_lt(remote_id, cur_cm_id_priv->id.remote_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700627 link = &(*link)->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700628 else if (be32_gt(remote_id, cur_cm_id_priv->id.remote_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700629 link = &(*link)->rb_right;
630 else {
631 int cmp;
632 cmp = memcmp(port_gid, &cur_cm_id_priv->av.dgid,
633 sizeof *port_gid);
634 if (cmp < 0)
635 link = &(*link)->rb_left;
636 else if (cmp > 0)
637 link = &(*link)->rb_right;
638 else
639 return cur_cm_id_priv;
640 }
641 }
642 rb_link_node(&cm_id_priv->sidr_id_node, parent, link);
643 rb_insert_color(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
644 return NULL;
645}
646
647static void cm_reject_sidr_req(struct cm_id_private *cm_id_priv,
648 enum ib_cm_sidr_status status)
649{
650 struct ib_cm_sidr_rep_param param;
651
652 memset(&param, 0, sizeof param);
653 param.status = status;
654 ib_send_cm_sidr_rep(&cm_id_priv->id, &param);
655}
656
Sean Hefty07d357d2005-10-17 15:37:43 -0700657struct ib_cm_id *ib_create_cm_id(struct ib_device *device,
658 ib_cm_handler cm_handler,
Hal Rosenstocka9770492005-07-27 11:45:40 -0700659 void *context)
660{
661 struct cm_id_private *cm_id_priv;
662 int ret;
663
Roland Dreierde6eb662005-11-02 07:23:14 -0800664 cm_id_priv = kzalloc(sizeof *cm_id_priv, GFP_KERNEL);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700665 if (!cm_id_priv)
666 return ERR_PTR(-ENOMEM);
667
Hal Rosenstocka9770492005-07-27 11:45:40 -0700668 cm_id_priv->id.state = IB_CM_IDLE;
Sean Hefty07d357d2005-10-17 15:37:43 -0700669 cm_id_priv->id.device = device;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700670 cm_id_priv->id.cm_handler = cm_handler;
671 cm_id_priv->id.context = context;
John Kingman354ba392005-09-09 18:23:32 -0700672 cm_id_priv->id.remote_cm_qpn = 1;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700673 ret = cm_alloc_id(cm_id_priv);
674 if (ret)
675 goto error;
676
677 spin_lock_init(&cm_id_priv->lock);
Sean Hefty1b52fa982006-05-12 14:57:52 -0700678 init_completion(&cm_id_priv->comp);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700679 INIT_LIST_HEAD(&cm_id_priv->work_list);
680 atomic_set(&cm_id_priv->work_count, -1);
681 atomic_set(&cm_id_priv->refcount, 1);
682 return &cm_id_priv->id;
683
684error:
685 kfree(cm_id_priv);
686 return ERR_PTR(-ENOMEM);
687}
688EXPORT_SYMBOL(ib_create_cm_id);
689
690static struct cm_work * cm_dequeue_work(struct cm_id_private *cm_id_priv)
691{
692 struct cm_work *work;
693
694 if (list_empty(&cm_id_priv->work_list))
695 return NULL;
696
697 work = list_entry(cm_id_priv->work_list.next, struct cm_work, list);
698 list_del(&work->list);
699 return work;
700}
701
702static void cm_free_work(struct cm_work *work)
703{
704 if (work->mad_recv_wc)
705 ib_free_recv_mad(work->mad_recv_wc);
706 kfree(work);
707}
708
709static inline int cm_convert_to_ms(int iba_time)
710{
711 /* approximate conversion to ms from 4.096us x 2^iba_time */
712 return 1 << max(iba_time - 8, 0);
713}
714
Sean Hefty1d846122007-06-18 11:09:37 -0700715/*
716 * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time
717 * Because of how ack_timeout is stored, adding one doubles the timeout.
718 * To avoid large timeouts, select the max(ack_delay, life_time + 1), and
719 * increment it (round up) only if the other is within 50%.
720 */
721static u8 cm_ack_timeout(u8 ca_ack_delay, u8 packet_life_time)
722{
723 int ack_timeout = packet_life_time + 1;
724
725 if (ack_timeout >= ca_ack_delay)
726 ack_timeout += (ca_ack_delay >= (ack_timeout - 1));
727 else
728 ack_timeout = ca_ack_delay +
729 (ack_timeout >= (ca_ack_delay - 1));
730
731 return min(31, ack_timeout);
732}
733
Hal Rosenstocka9770492005-07-27 11:45:40 -0700734static void cm_cleanup_timewait(struct cm_timewait_info *timewait_info)
735{
Hal Rosenstocka9770492005-07-27 11:45:40 -0700736 if (timewait_info->inserted_remote_id) {
737 rb_erase(&timewait_info->remote_id_node, &cm.remote_id_table);
738 timewait_info->inserted_remote_id = 0;
739 }
740
741 if (timewait_info->inserted_remote_qp) {
742 rb_erase(&timewait_info->remote_qp_node, &cm.remote_qp_table);
743 timewait_info->inserted_remote_qp = 0;
744 }
Hal Rosenstocka9770492005-07-27 11:45:40 -0700745}
746
Sean Hefty97f52eb2005-08-13 21:05:57 -0700747static struct cm_timewait_info * cm_create_timewait_info(__be32 local_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700748{
749 struct cm_timewait_info *timewait_info;
750
Roland Dreierde6eb662005-11-02 07:23:14 -0800751 timewait_info = kzalloc(sizeof *timewait_info, GFP_KERNEL);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700752 if (!timewait_info)
753 return ERR_PTR(-ENOMEM);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700754
755 timewait_info->work.local_id = local_id;
David Howellsc4028952006-11-22 14:57:56 +0000756 INIT_DELAYED_WORK(&timewait_info->work.work, cm_work_handler);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700757 timewait_info->work.cm_event.event = IB_CM_TIMEWAIT_EXIT;
758 return timewait_info;
759}
760
761static void cm_enter_timewait(struct cm_id_private *cm_id_priv)
762{
763 int wait_time;
Sean Hefty85753292006-10-04 11:29:59 -0700764 unsigned long flags;
Erez Shitritbe4b4992015-06-25 17:13:22 +0300765 struct cm_device *cm_dev;
766
767 cm_dev = ib_get_client_data(cm_id_priv->id.device, &cm_client);
768 if (!cm_dev)
769 return;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700770
Sean Hefty85753292006-10-04 11:29:59 -0700771 spin_lock_irqsave(&cm.lock, flags);
Michael S. Tsirkina70d0592006-08-28 16:32:50 +0300772 cm_cleanup_timewait(cm_id_priv->timewait_info);
Sean Hefty85753292006-10-04 11:29:59 -0700773 list_add_tail(&cm_id_priv->timewait_info->list, &cm.timewait_list);
774 spin_unlock_irqrestore(&cm.lock, flags);
Michael S. Tsirkina70d0592006-08-28 16:32:50 +0300775
Hal Rosenstocka9770492005-07-27 11:45:40 -0700776 /*
777 * The cm_id could be destroyed by the user before we exit timewait.
778 * To protect against this, we search for the cm_id after exiting
779 * timewait before notifying the user that we've exited timewait.
780 */
781 cm_id_priv->id.state = IB_CM_TIMEWAIT;
Sean Hefty1d846122007-06-18 11:09:37 -0700782 wait_time = cm_convert_to_ms(cm_id_priv->av.timeout);
Erez Shitritbe4b4992015-06-25 17:13:22 +0300783
784 /* Check if the device started its remove_one */
785 spin_lock_irq(&cm.lock);
786 if (!cm_dev->going_down)
787 queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work,
788 msecs_to_jiffies(wait_time));
789 spin_unlock_irq(&cm.lock);
790
Hal Rosenstocka9770492005-07-27 11:45:40 -0700791 cm_id_priv->timewait_info = NULL;
792}
793
794static void cm_reset_to_idle(struct cm_id_private *cm_id_priv)
795{
Sean Hefty85753292006-10-04 11:29:59 -0700796 unsigned long flags;
797
Hal Rosenstocka9770492005-07-27 11:45:40 -0700798 cm_id_priv->id.state = IB_CM_IDLE;
799 if (cm_id_priv->timewait_info) {
Sean Hefty85753292006-10-04 11:29:59 -0700800 spin_lock_irqsave(&cm.lock, flags);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700801 cm_cleanup_timewait(cm_id_priv->timewait_info);
Sean Hefty85753292006-10-04 11:29:59 -0700802 spin_unlock_irqrestore(&cm.lock, flags);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700803 kfree(cm_id_priv->timewait_info);
804 cm_id_priv->timewait_info = NULL;
805 }
806}
807
Michael S. Tsirkin04c33542006-07-14 00:23:52 -0700808static void cm_destroy_id(struct ib_cm_id *cm_id, int err)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700809{
810 struct cm_id_private *cm_id_priv;
811 struct cm_work *work;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700812
813 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
814retest:
Sean Hefty24be6e82007-06-18 11:09:36 -0700815 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700816 switch (cm_id->state) {
817 case IB_CM_LISTEN:
Sean Hefty24be6e82007-06-18 11:09:36 -0700818 spin_unlock_irq(&cm_id_priv->lock);
Haggai Eran067b1712015-07-30 17:50:18 +0300819
Sean Hefty24be6e82007-06-18 11:09:36 -0700820 spin_lock_irq(&cm.lock);
Haggai Eran067b1712015-07-30 17:50:18 +0300821 if (--cm_id_priv->listen_sharecount > 0) {
822 /* The id is still shared. */
823 cm_deref_id(cm_id_priv);
824 spin_unlock_irq(&cm.lock);
825 return;
826 }
Hal Rosenstocka9770492005-07-27 11:45:40 -0700827 rb_erase(&cm_id_priv->service_node, &cm.listen_service_table);
Sean Hefty24be6e82007-06-18 11:09:36 -0700828 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700829 break;
830 case IB_CM_SIDR_REQ_SENT:
831 cm_id->state = IB_CM_IDLE;
Sean Hefty34816ad2005-10-25 10:51:39 -0700832 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Sean Hefty24be6e82007-06-18 11:09:36 -0700833 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700834 break;
835 case IB_CM_SIDR_REQ_RCVD:
Sean Hefty24be6e82007-06-18 11:09:36 -0700836 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700837 cm_reject_sidr_req(cm_id_priv, IB_SIDR_REJECT);
Doron Tsur0ca81a22015-10-11 15:58:17 +0300838 spin_lock_irq(&cm.lock);
839 if (!RB_EMPTY_NODE(&cm_id_priv->sidr_id_node))
840 rb_erase(&cm_id_priv->sidr_id_node,
841 &cm.remote_sidr_table);
842 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700843 break;
844 case IB_CM_REQ_SENT:
Ted Kimc29ed5a2015-05-14 12:49:01 -0700845 case IB_CM_MRA_REQ_RCVD:
Sean Hefty227eca82005-11-30 10:00:25 -0800846 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Sean Hefty24be6e82007-06-18 11:09:36 -0700847 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty227eca82005-11-30 10:00:25 -0800848 ib_send_cm_rej(cm_id, IB_CM_REJ_TIMEOUT,
Sean Heftye971b8c2007-02-22 11:37:38 -0800849 &cm_id_priv->id.device->node_guid,
850 sizeof cm_id_priv->id.device->node_guid,
Sean Hefty227eca82005-11-30 10:00:25 -0800851 NULL, 0);
852 break;
Michael S. Tsirkin04c33542006-07-14 00:23:52 -0700853 case IB_CM_REQ_RCVD:
854 if (err == -ENOMEM) {
855 /* Do not reject to allow future retries. */
856 cm_reset_to_idle(cm_id_priv);
Sean Hefty24be6e82007-06-18 11:09:36 -0700857 spin_unlock_irq(&cm_id_priv->lock);
Michael S. Tsirkin04c33542006-07-14 00:23:52 -0700858 } else {
Sean Hefty24be6e82007-06-18 11:09:36 -0700859 spin_unlock_irq(&cm_id_priv->lock);
Michael S. Tsirkin04c33542006-07-14 00:23:52 -0700860 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
861 NULL, 0, NULL, 0);
862 }
863 break;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700864 case IB_CM_REP_SENT:
865 case IB_CM_MRA_REP_RCVD:
Sean Hefty34816ad2005-10-25 10:51:39 -0700866 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700867 /* Fall through */
Hal Rosenstocka9770492005-07-27 11:45:40 -0700868 case IB_CM_MRA_REQ_SENT:
869 case IB_CM_REP_RCVD:
870 case IB_CM_MRA_REP_SENT:
Sean Hefty24be6e82007-06-18 11:09:36 -0700871 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty227eca82005-11-30 10:00:25 -0800872 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
873 NULL, 0, NULL, 0);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700874 break;
875 case IB_CM_ESTABLISHED:
Sean Hefty24be6e82007-06-18 11:09:36 -0700876 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty2622e182011-05-30 22:30:46 -0700877 if (cm_id_priv->qp_type == IB_QPT_XRC_TGT)
878 break;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700879 ib_send_cm_dreq(cm_id, NULL, 0);
880 goto retest;
881 case IB_CM_DREQ_SENT:
Sean Hefty34816ad2005-10-25 10:51:39 -0700882 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700883 cm_enter_timewait(cm_id_priv);
Sean Hefty24be6e82007-06-18 11:09:36 -0700884 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700885 break;
886 case IB_CM_DREQ_RCVD:
Sean Hefty24be6e82007-06-18 11:09:36 -0700887 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700888 ib_send_cm_drep(cm_id, NULL, 0);
889 break;
890 default:
Sean Hefty24be6e82007-06-18 11:09:36 -0700891 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700892 break;
893 }
894
895 cm_free_id(cm_id->local_id);
Sean Hefty1b52fa982006-05-12 14:57:52 -0700896 cm_deref_id(cm_id_priv);
897 wait_for_completion(&cm_id_priv->comp);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700898 while ((work = cm_dequeue_work(cm_id_priv)) != NULL)
899 cm_free_work(work);
Sean Hefty6e61d042006-06-17 20:37:28 -0700900 kfree(cm_id_priv->private_data);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700901 kfree(cm_id_priv);
902}
Michael S. Tsirkin04c33542006-07-14 00:23:52 -0700903
904void ib_destroy_cm_id(struct ib_cm_id *cm_id)
905{
906 cm_destroy_id(cm_id, 0);
907}
Hal Rosenstocka9770492005-07-27 11:45:40 -0700908EXPORT_SYMBOL(ib_destroy_cm_id);
909
Haggai Eran067b1712015-07-30 17:50:18 +0300910/**
911 * __ib_cm_listen - Initiates listening on the specified service ID for
912 * connection and service ID resolution requests.
913 * @cm_id: Connection identifier associated with the listen request.
914 * @service_id: Service identifier matched against incoming connection
915 * and service ID resolution requests. The service ID should be specified
916 * network-byte order. If set to IB_CM_ASSIGN_SERVICE_ID, the CM will
917 * assign a service ID to the caller.
918 * @service_mask: Mask applied to service ID used to listen across a
919 * range of service IDs. If set to 0, the service ID is matched
920 * exactly. This parameter is ignored if %service_id is set to
921 * IB_CM_ASSIGN_SERVICE_ID.
Haggai Eran067b1712015-07-30 17:50:18 +0300922 */
923static int __ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id,
Haggai Eran73fec7f2015-07-30 17:50:26 +0300924 __be64 service_mask)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700925{
926 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700927 int ret = 0;
928
Harvey Harrison9c3da092009-01-17 17:11:57 -0800929 service_mask = service_mask ? service_mask : ~cpu_to_be64(0);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700930 service_id &= service_mask;
931 if ((service_id & IB_SERVICE_ID_AGN_MASK) == IB_CM_ASSIGN_SERVICE_ID &&
932 (service_id != IB_CM_ASSIGN_SERVICE_ID))
933 return -EINVAL;
934
935 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
Sean Hefty6e61d042006-06-17 20:37:28 -0700936 if (cm_id->state != IB_CM_IDLE)
937 return -EINVAL;
938
Hal Rosenstocka9770492005-07-27 11:45:40 -0700939 cm_id->state = IB_CM_LISTEN;
Haggai Eran067b1712015-07-30 17:50:18 +0300940 ++cm_id_priv->listen_sharecount;
Haggai Eran73fec7f2015-07-30 17:50:26 +0300941
Hal Rosenstocka9770492005-07-27 11:45:40 -0700942 if (service_id == IB_CM_ASSIGN_SERVICE_ID) {
Sean Hefty97f52eb2005-08-13 21:05:57 -0700943 cm_id->service_id = cpu_to_be64(cm.listen_service_id++);
Harvey Harrison9c3da092009-01-17 17:11:57 -0800944 cm_id->service_mask = ~cpu_to_be64(0);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700945 } else {
946 cm_id->service_id = service_id;
947 cm_id->service_mask = service_mask;
948 }
949 cur_cm_id_priv = cm_insert_listen(cm_id_priv);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700950
951 if (cur_cm_id_priv) {
952 cm_id->state = IB_CM_IDLE;
Haggai Eran067b1712015-07-30 17:50:18 +0300953 --cm_id_priv->listen_sharecount;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700954 ret = -EBUSY;
955 }
956 return ret;
957}
Haggai Eran067b1712015-07-30 17:50:18 +0300958
Haggai Eran73fec7f2015-07-30 17:50:26 +0300959int ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id, __be64 service_mask)
Haggai Eran067b1712015-07-30 17:50:18 +0300960{
Haggai Eran73fec7f2015-07-30 17:50:26 +0300961 unsigned long flags;
962 int ret;
963
964 spin_lock_irqsave(&cm.lock, flags);
965 ret = __ib_cm_listen(cm_id, service_id, service_mask);
966 spin_unlock_irqrestore(&cm.lock, flags);
967
968 return ret;
Haggai Eran067b1712015-07-30 17:50:18 +0300969}
Hal Rosenstocka9770492005-07-27 11:45:40 -0700970EXPORT_SYMBOL(ib_cm_listen);
971
Haggai Eran067b1712015-07-30 17:50:18 +0300972/**
973 * Create a new listening ib_cm_id and listen on the given service ID.
974 *
975 * If there's an existing ID listening on that same device and service ID,
976 * return it.
977 *
978 * @device: Device associated with the cm_id. All related communication will
979 * be associated with the specified device.
980 * @cm_handler: Callback invoked to notify the user of CM events.
981 * @service_id: Service identifier matched against incoming connection
982 * and service ID resolution requests. The service ID should be specified
983 * network-byte order. If set to IB_CM_ASSIGN_SERVICE_ID, the CM will
984 * assign a service ID to the caller.
985 *
986 * Callers should call ib_destroy_cm_id when done with the listener ID.
987 */
988struct ib_cm_id *ib_cm_insert_listen(struct ib_device *device,
989 ib_cm_handler cm_handler,
990 __be64 service_id)
991{
992 struct cm_id_private *cm_id_priv;
993 struct ib_cm_id *cm_id;
994 unsigned long flags;
995 int err = 0;
996
997 /* Create an ID in advance, since the creation may sleep */
998 cm_id = ib_create_cm_id(device, cm_handler, NULL);
999 if (IS_ERR(cm_id))
1000 return cm_id;
1001
1002 spin_lock_irqsave(&cm.lock, flags);
1003
1004 if (service_id == IB_CM_ASSIGN_SERVICE_ID)
1005 goto new_id;
1006
1007 /* Find an existing ID */
Haggai Eran73fec7f2015-07-30 17:50:26 +03001008 cm_id_priv = cm_find_listen(device, service_id);
Haggai Eran067b1712015-07-30 17:50:18 +03001009 if (cm_id_priv) {
1010 if (cm_id->cm_handler != cm_handler || cm_id->context) {
1011 /* Sharing an ib_cm_id with different handlers is not
1012 * supported */
1013 spin_unlock_irqrestore(&cm.lock, flags);
1014 return ERR_PTR(-EINVAL);
1015 }
1016 atomic_inc(&cm_id_priv->refcount);
1017 ++cm_id_priv->listen_sharecount;
1018 spin_unlock_irqrestore(&cm.lock, flags);
1019
1020 ib_destroy_cm_id(cm_id);
1021 cm_id = &cm_id_priv->id;
1022 return cm_id;
1023 }
1024
1025new_id:
1026 /* Use newly created ID */
Haggai Eran73fec7f2015-07-30 17:50:26 +03001027 err = __ib_cm_listen(cm_id, service_id, 0);
Haggai Eran067b1712015-07-30 17:50:18 +03001028
1029 spin_unlock_irqrestore(&cm.lock, flags);
1030
1031 if (err) {
1032 ib_destroy_cm_id(cm_id);
1033 return ERR_PTR(err);
1034 }
1035 return cm_id;
1036}
1037EXPORT_SYMBOL(ib_cm_insert_listen);
1038
Sean Hefty97f52eb2005-08-13 21:05:57 -07001039static __be64 cm_form_tid(struct cm_id_private *cm_id_priv,
1040 enum cm_msg_sequence msg_seq)
Hal Rosenstocka9770492005-07-27 11:45:40 -07001041{
1042 u64 hi_tid, low_tid;
1043
1044 hi_tid = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32;
Sean Hefty97f52eb2005-08-13 21:05:57 -07001045 low_tid = (u64) ((__force u32)cm_id_priv->id.local_id |
1046 (msg_seq << 30));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001047 return cpu_to_be64(hi_tid | low_tid);
1048}
1049
1050static void cm_format_mad_hdr(struct ib_mad_hdr *hdr,
Sean Hefty97f52eb2005-08-13 21:05:57 -07001051 __be16 attr_id, __be64 tid)
Hal Rosenstocka9770492005-07-27 11:45:40 -07001052{
1053 hdr->base_version = IB_MGMT_BASE_VERSION;
1054 hdr->mgmt_class = IB_MGMT_CLASS_CM;
1055 hdr->class_version = IB_CM_CLASS_VERSION;
1056 hdr->method = IB_MGMT_METHOD_SEND;
1057 hdr->attr_id = attr_id;
1058 hdr->tid = tid;
1059}
1060
1061static void cm_format_req(struct cm_req_msg *req_msg,
1062 struct cm_id_private *cm_id_priv,
1063 struct ib_cm_req_param *param)
1064{
Sean Hefty3971c9f2007-12-10 15:53:25 -08001065 struct ib_sa_path_rec *pri_path = param->primary_path;
1066 struct ib_sa_path_rec *alt_path = param->alternate_path;
1067
Hal Rosenstocka9770492005-07-27 11:45:40 -07001068 cm_format_mad_hdr(&req_msg->hdr, CM_REQ_ATTR_ID,
1069 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_REQ));
1070
1071 req_msg->local_comm_id = cm_id_priv->id.local_id;
1072 req_msg->service_id = param->service_id;
Sean Heftye971b8c2007-02-22 11:37:38 -08001073 req_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001074 cm_req_set_local_qpn(req_msg, cpu_to_be32(param->qp_num));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001075 cm_req_set_init_depth(req_msg, param->initiator_depth);
1076 cm_req_set_remote_resp_timeout(req_msg,
1077 param->remote_cm_response_timeout);
1078 cm_req_set_qp_type(req_msg, param->qp_type);
1079 cm_req_set_flow_ctrl(req_msg, param->flow_control);
1080 cm_req_set_starting_psn(req_msg, cpu_to_be32(param->starting_psn));
1081 cm_req_set_local_resp_timeout(req_msg,
1082 param->local_cm_response_timeout);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001083 req_msg->pkey = param->primary_path->pkey;
1084 cm_req_set_path_mtu(req_msg, param->primary_path->mtu);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001085 cm_req_set_max_cm_retries(req_msg, param->max_cm_retries);
Sean Heftyd26a3602011-05-13 10:46:20 -07001086
1087 if (param->qp_type != IB_QPT_XRC_INI) {
1088 cm_req_set_resp_res(req_msg, param->responder_resources);
1089 cm_req_set_retry_count(req_msg, param->retry_count);
1090 cm_req_set_rnr_retry_count(req_msg, param->rnr_retry_count);
1091 cm_req_set_srq(req_msg, param->srq);
1092 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07001093
Sean Hefty3971c9f2007-12-10 15:53:25 -08001094 if (pri_path->hop_limit <= 1) {
1095 req_msg->primary_local_lid = pri_path->slid;
1096 req_msg->primary_remote_lid = pri_path->dlid;
1097 } else {
1098 /* Work-around until there's a way to obtain remote LID info */
1099 req_msg->primary_local_lid = IB_LID_PERMISSIVE;
1100 req_msg->primary_remote_lid = IB_LID_PERMISSIVE;
1101 }
1102 req_msg->primary_local_gid = pri_path->sgid;
1103 req_msg->primary_remote_gid = pri_path->dgid;
1104 cm_req_set_primary_flow_label(req_msg, pri_path->flow_label);
1105 cm_req_set_primary_packet_rate(req_msg, pri_path->rate);
1106 req_msg->primary_traffic_class = pri_path->traffic_class;
1107 req_msg->primary_hop_limit = pri_path->hop_limit;
1108 cm_req_set_primary_sl(req_msg, pri_path->sl);
1109 cm_req_set_primary_subnet_local(req_msg, (pri_path->hop_limit <= 1));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001110 cm_req_set_primary_local_ack_timeout(req_msg,
Sean Hefty1d846122007-06-18 11:09:37 -07001111 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
Sean Hefty3971c9f2007-12-10 15:53:25 -08001112 pri_path->packet_life_time));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001113
Sean Hefty3971c9f2007-12-10 15:53:25 -08001114 if (alt_path) {
1115 if (alt_path->hop_limit <= 1) {
1116 req_msg->alt_local_lid = alt_path->slid;
1117 req_msg->alt_remote_lid = alt_path->dlid;
1118 } else {
1119 req_msg->alt_local_lid = IB_LID_PERMISSIVE;
1120 req_msg->alt_remote_lid = IB_LID_PERMISSIVE;
1121 }
1122 req_msg->alt_local_gid = alt_path->sgid;
1123 req_msg->alt_remote_gid = alt_path->dgid;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001124 cm_req_set_alt_flow_label(req_msg,
Sean Hefty3971c9f2007-12-10 15:53:25 -08001125 alt_path->flow_label);
1126 cm_req_set_alt_packet_rate(req_msg, alt_path->rate);
1127 req_msg->alt_traffic_class = alt_path->traffic_class;
1128 req_msg->alt_hop_limit = alt_path->hop_limit;
1129 cm_req_set_alt_sl(req_msg, alt_path->sl);
1130 cm_req_set_alt_subnet_local(req_msg, (alt_path->hop_limit <= 1));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001131 cm_req_set_alt_local_ack_timeout(req_msg,
Sean Hefty1d846122007-06-18 11:09:37 -07001132 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
Sean Hefty3971c9f2007-12-10 15:53:25 -08001133 alt_path->packet_life_time));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001134 }
1135
1136 if (param->private_data && param->private_data_len)
1137 memcpy(req_msg->private_data, param->private_data,
1138 param->private_data_len);
1139}
1140
Arjan van de Ven858119e2006-01-14 13:20:43 -08001141static int cm_validate_req_param(struct ib_cm_req_param *param)
Hal Rosenstocka9770492005-07-27 11:45:40 -07001142{
1143 /* peer-to-peer not supported */
1144 if (param->peer_to_peer)
1145 return -EINVAL;
1146
1147 if (!param->primary_path)
1148 return -EINVAL;
1149
Sean Heftyd26a3602011-05-13 10:46:20 -07001150 if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC &&
1151 param->qp_type != IB_QPT_XRC_INI)
Hal Rosenstocka9770492005-07-27 11:45:40 -07001152 return -EINVAL;
1153
1154 if (param->private_data &&
1155 param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE)
1156 return -EINVAL;
1157
1158 if (param->alternate_path &&
1159 (param->alternate_path->pkey != param->primary_path->pkey ||
1160 param->alternate_path->mtu != param->primary_path->mtu))
1161 return -EINVAL;
1162
1163 return 0;
1164}
1165
1166int ib_send_cm_req(struct ib_cm_id *cm_id,
1167 struct ib_cm_req_param *param)
1168{
1169 struct cm_id_private *cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001170 struct cm_req_msg *req_msg;
1171 unsigned long flags;
1172 int ret;
1173
1174 ret = cm_validate_req_param(param);
1175 if (ret)
1176 return ret;
1177
1178 /* Verify that we're not in timewait. */
1179 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1180 spin_lock_irqsave(&cm_id_priv->lock, flags);
1181 if (cm_id->state != IB_CM_IDLE) {
1182 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1183 ret = -EINVAL;
1184 goto out;
1185 }
1186 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1187
1188 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1189 id.local_id);
Sean Hefty75df23e2006-07-25 19:52:01 +03001190 if (IS_ERR(cm_id_priv->timewait_info)) {
1191 ret = PTR_ERR(cm_id_priv->timewait_info);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001192 goto out;
Sean Hefty75df23e2006-07-25 19:52:01 +03001193 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07001194
1195 ret = cm_init_av_by_path(param->primary_path, &cm_id_priv->av);
1196 if (ret)
1197 goto error1;
1198 if (param->alternate_path) {
1199 ret = cm_init_av_by_path(param->alternate_path,
1200 &cm_id_priv->alt_av);
1201 if (ret)
1202 goto error1;
1203 }
1204 cm_id->service_id = param->service_id;
Harvey Harrison9c3da092009-01-17 17:11:57 -08001205 cm_id->service_mask = ~cpu_to_be64(0);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001206 cm_id_priv->timeout_ms = cm_convert_to_ms(
1207 param->primary_path->packet_life_time) * 2 +
1208 cm_convert_to_ms(
1209 param->remote_cm_response_timeout);
1210 cm_id_priv->max_cm_retries = param->max_cm_retries;
1211 cm_id_priv->initiator_depth = param->initiator_depth;
1212 cm_id_priv->responder_resources = param->responder_resources;
1213 cm_id_priv->retry_count = param->retry_count;
1214 cm_id_priv->path_mtu = param->primary_path->mtu;
Sean Heftye1444b52006-11-28 14:57:13 -08001215 cm_id_priv->pkey = param->primary_path->pkey;
Sean Heftyae7971a2005-10-24 12:33:56 -07001216 cm_id_priv->qp_type = param->qp_type;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001217
1218 ret = cm_alloc_msg(cm_id_priv, &cm_id_priv->msg);
1219 if (ret)
1220 goto error1;
1221
1222 req_msg = (struct cm_req_msg *) cm_id_priv->msg->mad;
1223 cm_format_req(req_msg, cm_id_priv, param);
1224 cm_id_priv->tid = req_msg->hdr.tid;
Sean Hefty34816ad2005-10-25 10:51:39 -07001225 cm_id_priv->msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001226 cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT;
1227
1228 cm_id_priv->local_qpn = cm_req_get_local_qpn(req_msg);
1229 cm_id_priv->rq_psn = cm_req_get_starting_psn(req_msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001230
1231 spin_lock_irqsave(&cm_id_priv->lock, flags);
Sean Hefty34816ad2005-10-25 10:51:39 -07001232 ret = ib_post_send_mad(cm_id_priv->msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001233 if (ret) {
1234 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1235 goto error2;
1236 }
1237 BUG_ON(cm_id->state != IB_CM_IDLE);
1238 cm_id->state = IB_CM_REQ_SENT;
1239 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1240 return 0;
1241
1242error2: cm_free_msg(cm_id_priv->msg);
1243error1: kfree(cm_id_priv->timewait_info);
1244out: return ret;
1245}
1246EXPORT_SYMBOL(ib_send_cm_req);
1247
1248static int cm_issue_rej(struct cm_port *port,
1249 struct ib_mad_recv_wc *mad_recv_wc,
1250 enum ib_cm_rej_reason reason,
1251 enum cm_msg_response msg_rejected,
1252 void *ari, u8 ari_length)
1253{
1254 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001255 struct cm_rej_msg *rej_msg, *rcv_msg;
1256 int ret;
1257
1258 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
1259 if (ret)
1260 return ret;
1261
1262 /* We just need common CM header information. Cast to any message. */
1263 rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad;
1264 rej_msg = (struct cm_rej_msg *) msg->mad;
1265
1266 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid);
1267 rej_msg->remote_comm_id = rcv_msg->local_comm_id;
1268 rej_msg->local_comm_id = rcv_msg->remote_comm_id;
1269 cm_rej_set_msg_rejected(rej_msg, msg_rejected);
Sean Hefty97f52eb2005-08-13 21:05:57 -07001270 rej_msg->reason = cpu_to_be16(reason);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001271
1272 if (ari && ari_length) {
1273 cm_rej_set_reject_info_len(rej_msg, ari_length);
1274 memcpy(rej_msg->ari, ari, ari_length);
1275 }
1276
Sean Hefty34816ad2005-10-25 10:51:39 -07001277 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001278 if (ret)
1279 cm_free_msg(msg);
1280
1281 return ret;
1282}
1283
Sean Hefty97f52eb2005-08-13 21:05:57 -07001284static inline int cm_is_active_peer(__be64 local_ca_guid, __be64 remote_ca_guid,
1285 __be32 local_qpn, __be32 remote_qpn)
Hal Rosenstocka9770492005-07-27 11:45:40 -07001286{
1287 return (be64_to_cpu(local_ca_guid) > be64_to_cpu(remote_ca_guid) ||
1288 ((local_ca_guid == remote_ca_guid) &&
1289 (be32_to_cpu(local_qpn) > be32_to_cpu(remote_qpn))));
1290}
1291
Arjan van de Ven858119e2006-01-14 13:20:43 -08001292static void cm_format_paths_from_req(struct cm_req_msg *req_msg,
Hal Rosenstocka9770492005-07-27 11:45:40 -07001293 struct ib_sa_path_rec *primary_path,
1294 struct ib_sa_path_rec *alt_path)
1295{
1296 memset(primary_path, 0, sizeof *primary_path);
1297 primary_path->dgid = req_msg->primary_local_gid;
1298 primary_path->sgid = req_msg->primary_remote_gid;
1299 primary_path->dlid = req_msg->primary_local_lid;
1300 primary_path->slid = req_msg->primary_remote_lid;
1301 primary_path->flow_label = cm_req_get_primary_flow_label(req_msg);
1302 primary_path->hop_limit = req_msg->primary_hop_limit;
1303 primary_path->traffic_class = req_msg->primary_traffic_class;
1304 primary_path->reversible = 1;
1305 primary_path->pkey = req_msg->pkey;
1306 primary_path->sl = cm_req_get_primary_sl(req_msg);
1307 primary_path->mtu_selector = IB_SA_EQ;
1308 primary_path->mtu = cm_req_get_path_mtu(req_msg);
1309 primary_path->rate_selector = IB_SA_EQ;
1310 primary_path->rate = cm_req_get_primary_packet_rate(req_msg);
1311 primary_path->packet_life_time_selector = IB_SA_EQ;
1312 primary_path->packet_life_time =
1313 cm_req_get_primary_local_ack_timeout(req_msg);
1314 primary_path->packet_life_time -= (primary_path->packet_life_time > 0);
Haggai Eran15865e7d2015-07-30 17:50:17 +03001315 primary_path->service_id = req_msg->service_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001316
1317 if (req_msg->alt_local_lid) {
1318 memset(alt_path, 0, sizeof *alt_path);
1319 alt_path->dgid = req_msg->alt_local_gid;
1320 alt_path->sgid = req_msg->alt_remote_gid;
1321 alt_path->dlid = req_msg->alt_local_lid;
1322 alt_path->slid = req_msg->alt_remote_lid;
1323 alt_path->flow_label = cm_req_get_alt_flow_label(req_msg);
1324 alt_path->hop_limit = req_msg->alt_hop_limit;
1325 alt_path->traffic_class = req_msg->alt_traffic_class;
1326 alt_path->reversible = 1;
1327 alt_path->pkey = req_msg->pkey;
1328 alt_path->sl = cm_req_get_alt_sl(req_msg);
1329 alt_path->mtu_selector = IB_SA_EQ;
1330 alt_path->mtu = cm_req_get_path_mtu(req_msg);
1331 alt_path->rate_selector = IB_SA_EQ;
1332 alt_path->rate = cm_req_get_alt_packet_rate(req_msg);
1333 alt_path->packet_life_time_selector = IB_SA_EQ;
1334 alt_path->packet_life_time =
1335 cm_req_get_alt_local_ack_timeout(req_msg);
1336 alt_path->packet_life_time -= (alt_path->packet_life_time > 0);
Haggai Eran15865e7d2015-07-30 17:50:17 +03001337 alt_path->service_id = req_msg->service_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001338 }
1339}
1340
Haggai Eran24cad9a2015-07-30 17:50:21 +03001341static u16 cm_get_bth_pkey(struct cm_work *work)
1342{
1343 struct ib_device *ib_dev = work->port->cm_dev->ib_device;
1344 u8 port_num = work->port->port_num;
1345 u16 pkey_index = work->mad_recv_wc->wc->pkey_index;
1346 u16 pkey;
1347 int ret;
1348
1349 ret = ib_get_cached_pkey(ib_dev, port_num, pkey_index, &pkey);
1350 if (ret) {
1351 dev_warn_ratelimited(&ib_dev->dev, "ib_cm: Couldn't retrieve pkey for incoming request (port %d, pkey index %d). %d\n",
1352 port_num, pkey_index, ret);
1353 return 0;
1354 }
1355
1356 return pkey;
1357}
1358
Hal Rosenstocka9770492005-07-27 11:45:40 -07001359static void cm_format_req_event(struct cm_work *work,
1360 struct cm_id_private *cm_id_priv,
1361 struct ib_cm_id *listen_id)
1362{
1363 struct cm_req_msg *req_msg;
1364 struct ib_cm_req_event_param *param;
1365
1366 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1367 param = &work->cm_event.param.req_rcvd;
1368 param->listen_id = listen_id;
Haggai Eran24cad9a2015-07-30 17:50:21 +03001369 param->bth_pkey = cm_get_bth_pkey(work);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001370 param->port = cm_id_priv->av.port->port_num;
1371 param->primary_path = &work->path[0];
1372 if (req_msg->alt_local_lid)
1373 param->alternate_path = &work->path[1];
1374 else
1375 param->alternate_path = NULL;
1376 param->remote_ca_guid = req_msg->local_ca_guid;
1377 param->remote_qkey = be32_to_cpu(req_msg->local_qkey);
1378 param->remote_qpn = be32_to_cpu(cm_req_get_local_qpn(req_msg));
1379 param->qp_type = cm_req_get_qp_type(req_msg);
1380 param->starting_psn = be32_to_cpu(cm_req_get_starting_psn(req_msg));
1381 param->responder_resources = cm_req_get_init_depth(req_msg);
1382 param->initiator_depth = cm_req_get_resp_res(req_msg);
1383 param->local_cm_response_timeout =
1384 cm_req_get_remote_resp_timeout(req_msg);
1385 param->flow_control = cm_req_get_flow_ctrl(req_msg);
1386 param->remote_cm_response_timeout =
1387 cm_req_get_local_resp_timeout(req_msg);
1388 param->retry_count = cm_req_get_retry_count(req_msg);
1389 param->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1390 param->srq = cm_req_get_srq(req_msg);
1391 work->cm_event.private_data = &req_msg->private_data;
1392}
1393
1394static void cm_process_work(struct cm_id_private *cm_id_priv,
1395 struct cm_work *work)
1396{
Hal Rosenstocka9770492005-07-27 11:45:40 -07001397 int ret;
1398
1399 /* We will typically only have the current event to report. */
1400 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
1401 cm_free_work(work);
1402
1403 while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) {
Sean Hefty24be6e82007-06-18 11:09:36 -07001404 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001405 work = cm_dequeue_work(cm_id_priv);
Sean Hefty24be6e82007-06-18 11:09:36 -07001406 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001407 BUG_ON(!work);
1408 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id,
1409 &work->cm_event);
1410 cm_free_work(work);
1411 }
1412 cm_deref_id(cm_id_priv);
1413 if (ret)
Michael S. Tsirkin04c33542006-07-14 00:23:52 -07001414 cm_destroy_id(&cm_id_priv->id, ret);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001415}
1416
1417static void cm_format_mra(struct cm_mra_msg *mra_msg,
1418 struct cm_id_private *cm_id_priv,
1419 enum cm_msg_response msg_mraed, u8 service_timeout,
1420 const void *private_data, u8 private_data_len)
1421{
1422 cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid);
1423 cm_mra_set_msg_mraed(mra_msg, msg_mraed);
1424 mra_msg->local_comm_id = cm_id_priv->id.local_id;
1425 mra_msg->remote_comm_id = cm_id_priv->id.remote_id;
1426 cm_mra_set_service_timeout(mra_msg, service_timeout);
1427
1428 if (private_data && private_data_len)
1429 memcpy(mra_msg->private_data, private_data, private_data_len);
1430}
1431
1432static void cm_format_rej(struct cm_rej_msg *rej_msg,
1433 struct cm_id_private *cm_id_priv,
1434 enum ib_cm_rej_reason reason,
1435 void *ari,
1436 u8 ari_length,
1437 const void *private_data,
1438 u8 private_data_len)
1439{
1440 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid);
1441 rej_msg->remote_comm_id = cm_id_priv->id.remote_id;
1442
1443 switch(cm_id_priv->id.state) {
1444 case IB_CM_REQ_RCVD:
1445 rej_msg->local_comm_id = 0;
1446 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1447 break;
1448 case IB_CM_MRA_REQ_SENT:
1449 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1450 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1451 break;
1452 case IB_CM_REP_RCVD:
1453 case IB_CM_MRA_REP_SENT:
1454 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1455 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REP);
1456 break;
1457 default:
1458 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1459 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_OTHER);
1460 break;
1461 }
1462
Sean Hefty97f52eb2005-08-13 21:05:57 -07001463 rej_msg->reason = cpu_to_be16(reason);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001464 if (ari && ari_length) {
1465 cm_rej_set_reject_info_len(rej_msg, ari_length);
1466 memcpy(rej_msg->ari, ari, ari_length);
1467 }
1468
1469 if (private_data && private_data_len)
1470 memcpy(rej_msg->private_data, private_data, private_data_len);
1471}
1472
1473static void cm_dup_req_handler(struct cm_work *work,
1474 struct cm_id_private *cm_id_priv)
1475{
1476 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001477 int ret;
1478
Sean Hefty9af57b72007-07-16 21:49:35 -07001479 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1480 counter[CM_REQ_COUNTER]);
1481
Hal Rosenstocka9770492005-07-27 11:45:40 -07001482 /* Quick state check to discard duplicate REQs. */
1483 if (cm_id_priv->id.state == IB_CM_REQ_RCVD)
1484 return;
1485
1486 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1487 if (ret)
1488 return;
1489
Sean Hefty24be6e82007-06-18 11:09:36 -07001490 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001491 switch (cm_id_priv->id.state) {
1492 case IB_CM_MRA_REQ_SENT:
1493 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1494 CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout,
1495 cm_id_priv->private_data,
1496 cm_id_priv->private_data_len);
1497 break;
1498 case IB_CM_TIMEWAIT:
1499 cm_format_rej((struct cm_rej_msg *) msg->mad, cm_id_priv,
1500 IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0);
1501 break;
1502 default:
1503 goto unlock;
1504 }
Sean Hefty24be6e82007-06-18 11:09:36 -07001505 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001506
Sean Hefty34816ad2005-10-25 10:51:39 -07001507 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001508 if (ret)
1509 goto free;
1510 return;
1511
Sean Hefty24be6e82007-06-18 11:09:36 -07001512unlock: spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001513free: cm_free_msg(msg);
1514}
1515
1516static struct cm_id_private * cm_match_req(struct cm_work *work,
1517 struct cm_id_private *cm_id_priv)
1518{
1519 struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv;
1520 struct cm_timewait_info *timewait_info;
1521 struct cm_req_msg *req_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001522
1523 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1524
Sean Heftyd998ccc2007-05-21 17:38:02 -07001525 /* Check for possible duplicate REQ. */
Sean Hefty24be6e82007-06-18 11:09:36 -07001526 spin_lock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001527 timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001528 if (timewait_info) {
1529 cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
1530 timewait_info->work.remote_id);
Sean Hefty24be6e82007-06-18 11:09:36 -07001531 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001532 if (cur_cm_id_priv) {
1533 cm_dup_req_handler(work, cur_cm_id_priv);
1534 cm_deref_id(cur_cm_id_priv);
Sean Heftyd998ccc2007-05-21 17:38:02 -07001535 }
1536 return NULL;
1537 }
1538
1539 /* Check for stale connections. */
1540 timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
1541 if (timewait_info) {
1542 cm_cleanup_timewait(cm_id_priv->timewait_info);
Sean Hefty24be6e82007-06-18 11:09:36 -07001543 spin_unlock_irq(&cm.lock);
Sean Heftyd998ccc2007-05-21 17:38:02 -07001544 cm_issue_rej(work->port, work->mad_recv_wc,
1545 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ,
1546 NULL, 0);
1547 return NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001548 }
1549
1550 /* Find matching listen request. */
Sean Hefty07d357d2005-10-17 15:37:43 -07001551 listen_cm_id_priv = cm_find_listen(cm_id_priv->id.device,
Haggai Eran73fec7f2015-07-30 17:50:26 +03001552 req_msg->service_id);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001553 if (!listen_cm_id_priv) {
Sean Hefty85753292006-10-04 11:29:59 -07001554 cm_cleanup_timewait(cm_id_priv->timewait_info);
Sean Hefty24be6e82007-06-18 11:09:36 -07001555 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001556 cm_issue_rej(work->port, work->mad_recv_wc,
1557 IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ,
1558 NULL, 0);
Sean Hefty85753292006-10-04 11:29:59 -07001559 goto out;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001560 }
1561 atomic_inc(&listen_cm_id_priv->refcount);
1562 atomic_inc(&cm_id_priv->refcount);
1563 cm_id_priv->id.state = IB_CM_REQ_RCVD;
1564 atomic_inc(&cm_id_priv->work_count);
Sean Hefty24be6e82007-06-18 11:09:36 -07001565 spin_unlock_irq(&cm.lock);
Sean Hefty85753292006-10-04 11:29:59 -07001566out:
Hal Rosenstocka9770492005-07-27 11:45:40 -07001567 return listen_cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001568}
1569
Sean Hefty3971c9f2007-12-10 15:53:25 -08001570/*
1571 * Work-around for inter-subnet connections. If the LIDs are permissive,
1572 * we need to override the LID/SL data in the REQ with the LID information
1573 * in the work completion.
1574 */
1575static void cm_process_routed_req(struct cm_req_msg *req_msg, struct ib_wc *wc)
1576{
1577 if (!cm_req_get_primary_subnet_local(req_msg)) {
1578 if (req_msg->primary_local_lid == IB_LID_PERMISSIVE) {
1579 req_msg->primary_local_lid = cpu_to_be16(wc->slid);
1580 cm_req_set_primary_sl(req_msg, wc->sl);
1581 }
1582
1583 if (req_msg->primary_remote_lid == IB_LID_PERMISSIVE)
1584 req_msg->primary_remote_lid = cpu_to_be16(wc->dlid_path_bits);
1585 }
1586
1587 if (!cm_req_get_alt_subnet_local(req_msg)) {
1588 if (req_msg->alt_local_lid == IB_LID_PERMISSIVE) {
1589 req_msg->alt_local_lid = cpu_to_be16(wc->slid);
1590 cm_req_set_alt_sl(req_msg, wc->sl);
1591 }
1592
1593 if (req_msg->alt_remote_lid == IB_LID_PERMISSIVE)
1594 req_msg->alt_remote_lid = cpu_to_be16(wc->dlid_path_bits);
1595 }
1596}
1597
Hal Rosenstocka9770492005-07-27 11:45:40 -07001598static int cm_req_handler(struct cm_work *work)
1599{
1600 struct ib_cm_id *cm_id;
1601 struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
1602 struct cm_req_msg *req_msg;
Matan Barakcb57bb82015-12-23 14:56:48 +02001603 union ib_gid gid;
1604 struct ib_gid_attr gid_attr;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001605 int ret;
1606
1607 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1608
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03001609 cm_id = ib_create_cm_id(work->port->cm_dev->ib_device, NULL, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001610 if (IS_ERR(cm_id))
1611 return PTR_ERR(cm_id);
1612
1613 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1614 cm_id_priv->id.remote_id = req_msg->local_comm_id;
1615 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
Sean Heftyca222c62006-06-17 20:37:40 -07001616 work->mad_recv_wc->recv_buf.grh,
Hal Rosenstocka9770492005-07-27 11:45:40 -07001617 &cm_id_priv->av);
1618 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1619 id.local_id);
1620 if (IS_ERR(cm_id_priv->timewait_info)) {
1621 ret = PTR_ERR(cm_id_priv->timewait_info);
Sean Hefty76842402006-08-28 11:57:42 -07001622 goto destroy;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001623 }
1624 cm_id_priv->timewait_info->work.remote_id = req_msg->local_comm_id;
1625 cm_id_priv->timewait_info->remote_ca_guid = req_msg->local_ca_guid;
1626 cm_id_priv->timewait_info->remote_qpn = cm_req_get_local_qpn(req_msg);
1627
1628 listen_cm_id_priv = cm_match_req(work, cm_id_priv);
1629 if (!listen_cm_id_priv) {
1630 ret = -EINVAL;
Sean Hefty76842402006-08-28 11:57:42 -07001631 kfree(cm_id_priv->timewait_info);
1632 goto destroy;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001633 }
1634
1635 cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
1636 cm_id_priv->id.context = listen_cm_id_priv->id.context;
1637 cm_id_priv->id.service_id = req_msg->service_id;
Harvey Harrison9c3da092009-01-17 17:11:57 -08001638 cm_id_priv->id.service_mask = ~cpu_to_be64(0);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001639
Sean Hefty3971c9f2007-12-10 15:53:25 -08001640 cm_process_routed_req(req_msg, work->mad_recv_wc->wc);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001641 cm_format_paths_from_req(req_msg, &work->path[0], &work->path[1]);
Matan Barakdd5f03b2013-12-12 18:03:11 +02001642
1643 memcpy(work->path[0].dmac, cm_id_priv->av.ah_attr.dmac, ETH_ALEN);
Matan Barakcb57bb82015-12-23 14:56:48 +02001644 ret = ib_get_cached_gid(work->port->cm_dev->ib_device,
1645 work->port->port_num,
1646 cm_id_priv->av.ah_attr.grh.sgid_index,
1647 &gid, &gid_attr);
1648 if (!ret) {
1649 if (gid_attr.ndev)
1650 dev_put(gid_attr.ndev);
1651 work->path[0].gid_type = gid_attr.gid_type;
1652 ret = cm_init_av_by_path(&work->path[0], &cm_id_priv->av);
1653 }
Sean Hefty76842402006-08-28 11:57:42 -07001654 if (ret) {
Matan Barakcb57bb82015-12-23 14:56:48 +02001655 int err = ib_get_cached_gid(work->port->cm_dev->ib_device,
1656 work->port->port_num, 0,
1657 &work->path[0].sgid,
1658 &gid_attr);
1659 if (!err && gid_attr.ndev)
1660 dev_put(gid_attr.ndev);
1661 work->path[0].gid_type = gid_attr.gid_type;
Sean Hefty76842402006-08-28 11:57:42 -07001662 ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_GID,
1663 &work->path[0].sgid, sizeof work->path[0].sgid,
1664 NULL, 0);
1665 goto rejected;
1666 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07001667 if (req_msg->alt_local_lid) {
1668 ret = cm_init_av_by_path(&work->path[1], &cm_id_priv->alt_av);
Sean Hefty76842402006-08-28 11:57:42 -07001669 if (ret) {
1670 ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_ALT_GID,
1671 &work->path[0].sgid,
1672 sizeof work->path[0].sgid, NULL, 0);
1673 goto rejected;
1674 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07001675 }
1676 cm_id_priv->tid = req_msg->hdr.tid;
1677 cm_id_priv->timeout_ms = cm_convert_to_ms(
1678 cm_req_get_local_resp_timeout(req_msg));
1679 cm_id_priv->max_cm_retries = cm_req_get_max_cm_retries(req_msg);
1680 cm_id_priv->remote_qpn = cm_req_get_local_qpn(req_msg);
1681 cm_id_priv->initiator_depth = cm_req_get_resp_res(req_msg);
1682 cm_id_priv->responder_resources = cm_req_get_init_depth(req_msg);
1683 cm_id_priv->path_mtu = cm_req_get_path_mtu(req_msg);
Sean Heftye1444b52006-11-28 14:57:13 -08001684 cm_id_priv->pkey = req_msg->pkey;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001685 cm_id_priv->sq_psn = cm_req_get_starting_psn(req_msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001686 cm_id_priv->retry_count = cm_req_get_retry_count(req_msg);
1687 cm_id_priv->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
Sean Heftyae7971a2005-10-24 12:33:56 -07001688 cm_id_priv->qp_type = cm_req_get_qp_type(req_msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001689
1690 cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
1691 cm_process_work(cm_id_priv, work);
1692 cm_deref_id(listen_cm_id_priv);
1693 return 0;
1694
Sean Hefty76842402006-08-28 11:57:42 -07001695rejected:
1696 atomic_dec(&cm_id_priv->refcount);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001697 cm_deref_id(listen_cm_id_priv);
Sean Hefty76842402006-08-28 11:57:42 -07001698destroy:
1699 ib_destroy_cm_id(cm_id);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001700 return ret;
1701}
1702
1703static void cm_format_rep(struct cm_rep_msg *rep_msg,
1704 struct cm_id_private *cm_id_priv,
1705 struct ib_cm_rep_param *param)
1706{
1707 cm_format_mad_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid);
1708 rep_msg->local_comm_id = cm_id_priv->id.local_id;
1709 rep_msg->remote_comm_id = cm_id_priv->id.remote_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001710 cm_rep_set_starting_psn(rep_msg, cpu_to_be32(param->starting_psn));
1711 rep_msg->resp_resources = param->responder_resources;
Sean Hefty1d846122007-06-18 11:09:37 -07001712 cm_rep_set_target_ack_delay(rep_msg,
1713 cm_id_priv->av.port->cm_dev->ack_delay);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001714 cm_rep_set_failover(rep_msg, param->failover_accepted);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001715 cm_rep_set_rnr_retry_count(rep_msg, param->rnr_retry_count);
Sean Heftye971b8c2007-02-22 11:37:38 -08001716 rep_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001717
Sean Heftyd26a3602011-05-13 10:46:20 -07001718 if (cm_id_priv->qp_type != IB_QPT_XRC_TGT) {
1719 rep_msg->initiator_depth = param->initiator_depth;
1720 cm_rep_set_flow_ctrl(rep_msg, param->flow_control);
1721 cm_rep_set_srq(rep_msg, param->srq);
Sean Heftyef700442011-08-02 11:08:22 -07001722 cm_rep_set_local_qpn(rep_msg, cpu_to_be32(param->qp_num));
Sean Heftyd26a3602011-05-13 10:46:20 -07001723 } else {
1724 cm_rep_set_srq(rep_msg, 1);
Sean Heftyef700442011-08-02 11:08:22 -07001725 cm_rep_set_local_eecn(rep_msg, cpu_to_be32(param->qp_num));
Sean Heftyd26a3602011-05-13 10:46:20 -07001726 }
1727
Hal Rosenstocka9770492005-07-27 11:45:40 -07001728 if (param->private_data && param->private_data_len)
1729 memcpy(rep_msg->private_data, param->private_data,
1730 param->private_data_len);
1731}
1732
1733int ib_send_cm_rep(struct ib_cm_id *cm_id,
1734 struct ib_cm_rep_param *param)
1735{
1736 struct cm_id_private *cm_id_priv;
1737 struct ib_mad_send_buf *msg;
1738 struct cm_rep_msg *rep_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001739 unsigned long flags;
1740 int ret;
1741
1742 if (param->private_data &&
1743 param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE)
1744 return -EINVAL;
1745
1746 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1747 spin_lock_irqsave(&cm_id_priv->lock, flags);
1748 if (cm_id->state != IB_CM_REQ_RCVD &&
1749 cm_id->state != IB_CM_MRA_REQ_SENT) {
1750 ret = -EINVAL;
1751 goto out;
1752 }
1753
1754 ret = cm_alloc_msg(cm_id_priv, &msg);
1755 if (ret)
1756 goto out;
1757
1758 rep_msg = (struct cm_rep_msg *) msg->mad;
1759 cm_format_rep(rep_msg, cm_id_priv, param);
Sean Hefty34816ad2005-10-25 10:51:39 -07001760 msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001761 msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT;
1762
Sean Hefty34816ad2005-10-25 10:51:39 -07001763 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001764 if (ret) {
1765 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1766 cm_free_msg(msg);
1767 return ret;
1768 }
1769
1770 cm_id->state = IB_CM_REP_SENT;
1771 cm_id_priv->msg = msg;
1772 cm_id_priv->initiator_depth = param->initiator_depth;
1773 cm_id_priv->responder_resources = param->responder_resources;
1774 cm_id_priv->rq_psn = cm_rep_get_starting_psn(rep_msg);
Sean Heftyef700442011-08-02 11:08:22 -07001775 cm_id_priv->local_qpn = cpu_to_be32(param->qp_num & 0xFFFFFF);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001776
1777out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1778 return ret;
1779}
1780EXPORT_SYMBOL(ib_send_cm_rep);
1781
1782static void cm_format_rtu(struct cm_rtu_msg *rtu_msg,
1783 struct cm_id_private *cm_id_priv,
1784 const void *private_data,
1785 u8 private_data_len)
1786{
1787 cm_format_mad_hdr(&rtu_msg->hdr, CM_RTU_ATTR_ID, cm_id_priv->tid);
1788 rtu_msg->local_comm_id = cm_id_priv->id.local_id;
1789 rtu_msg->remote_comm_id = cm_id_priv->id.remote_id;
1790
1791 if (private_data && private_data_len)
1792 memcpy(rtu_msg->private_data, private_data, private_data_len);
1793}
1794
1795int ib_send_cm_rtu(struct ib_cm_id *cm_id,
1796 const void *private_data,
1797 u8 private_data_len)
1798{
1799 struct cm_id_private *cm_id_priv;
1800 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001801 unsigned long flags;
1802 void *data;
1803 int ret;
1804
1805 if (private_data && private_data_len > IB_CM_RTU_PRIVATE_DATA_SIZE)
1806 return -EINVAL;
1807
1808 data = cm_copy_private_data(private_data, private_data_len);
1809 if (IS_ERR(data))
1810 return PTR_ERR(data);
1811
1812 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1813 spin_lock_irqsave(&cm_id_priv->lock, flags);
1814 if (cm_id->state != IB_CM_REP_RCVD &&
1815 cm_id->state != IB_CM_MRA_REP_SENT) {
1816 ret = -EINVAL;
1817 goto error;
1818 }
1819
1820 ret = cm_alloc_msg(cm_id_priv, &msg);
1821 if (ret)
1822 goto error;
1823
1824 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1825 private_data, private_data_len);
1826
Sean Hefty34816ad2005-10-25 10:51:39 -07001827 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001828 if (ret) {
1829 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1830 cm_free_msg(msg);
1831 kfree(data);
1832 return ret;
1833 }
1834
1835 cm_id->state = IB_CM_ESTABLISHED;
1836 cm_set_private_data(cm_id_priv, data, private_data_len);
1837 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1838 return 0;
1839
1840error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1841 kfree(data);
1842 return ret;
1843}
1844EXPORT_SYMBOL(ib_send_cm_rtu);
1845
Sean Heftyef700442011-08-02 11:08:22 -07001846static void cm_format_rep_event(struct cm_work *work, enum ib_qp_type qp_type)
Hal Rosenstocka9770492005-07-27 11:45:40 -07001847{
1848 struct cm_rep_msg *rep_msg;
1849 struct ib_cm_rep_event_param *param;
1850
1851 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1852 param = &work->cm_event.param.rep_rcvd;
1853 param->remote_ca_guid = rep_msg->local_ca_guid;
1854 param->remote_qkey = be32_to_cpu(rep_msg->local_qkey);
Sean Heftyef700442011-08-02 11:08:22 -07001855 param->remote_qpn = be32_to_cpu(cm_rep_get_qpn(rep_msg, qp_type));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001856 param->starting_psn = be32_to_cpu(cm_rep_get_starting_psn(rep_msg));
1857 param->responder_resources = rep_msg->initiator_depth;
1858 param->initiator_depth = rep_msg->resp_resources;
1859 param->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1860 param->failover_accepted = cm_rep_get_failover(rep_msg);
1861 param->flow_control = cm_rep_get_flow_ctrl(rep_msg);
1862 param->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1863 param->srq = cm_rep_get_srq(rep_msg);
1864 work->cm_event.private_data = &rep_msg->private_data;
1865}
1866
1867static void cm_dup_rep_handler(struct cm_work *work)
1868{
1869 struct cm_id_private *cm_id_priv;
1870 struct cm_rep_msg *rep_msg;
1871 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001872 int ret;
1873
1874 rep_msg = (struct cm_rep_msg *) work->mad_recv_wc->recv_buf.mad;
1875 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id,
1876 rep_msg->local_comm_id);
1877 if (!cm_id_priv)
1878 return;
1879
Sean Hefty9af57b72007-07-16 21:49:35 -07001880 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1881 counter[CM_REP_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001882 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1883 if (ret)
1884 goto deref;
1885
Sean Hefty24be6e82007-06-18 11:09:36 -07001886 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001887 if (cm_id_priv->id.state == IB_CM_ESTABLISHED)
1888 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1889 cm_id_priv->private_data,
1890 cm_id_priv->private_data_len);
1891 else if (cm_id_priv->id.state == IB_CM_MRA_REP_SENT)
1892 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1893 CM_MSG_RESPONSE_REP, cm_id_priv->service_timeout,
1894 cm_id_priv->private_data,
1895 cm_id_priv->private_data_len);
1896 else
1897 goto unlock;
Sean Hefty24be6e82007-06-18 11:09:36 -07001898 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001899
Sean Hefty34816ad2005-10-25 10:51:39 -07001900 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001901 if (ret)
1902 goto free;
1903 goto deref;
1904
Sean Hefty24be6e82007-06-18 11:09:36 -07001905unlock: spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001906free: cm_free_msg(msg);
1907deref: cm_deref_id(cm_id_priv);
1908}
1909
1910static int cm_rep_handler(struct cm_work *work)
1911{
1912 struct cm_id_private *cm_id_priv;
1913 struct cm_rep_msg *rep_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001914 int ret;
1915
1916 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1917 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id, 0);
1918 if (!cm_id_priv) {
1919 cm_dup_rep_handler(work);
1920 return -EINVAL;
1921 }
1922
Sean Heftyef700442011-08-02 11:08:22 -07001923 cm_format_rep_event(work, cm_id_priv->qp_type);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001924
Sean Hefty24be6e82007-06-18 11:09:36 -07001925 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001926 switch (cm_id_priv->id.state) {
1927 case IB_CM_REQ_SENT:
1928 case IB_CM_MRA_REQ_RCVD:
1929 break;
1930 default:
Sean Hefty24be6e82007-06-18 11:09:36 -07001931 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001932 ret = -EINVAL;
1933 goto error;
1934 }
Sean Hefty87fd1a12006-03-02 16:50:37 -08001935
1936 cm_id_priv->timewait_info->work.remote_id = rep_msg->local_comm_id;
1937 cm_id_priv->timewait_info->remote_ca_guid = rep_msg->local_ca_guid;
Sean Heftyef700442011-08-02 11:08:22 -07001938 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 -08001939
1940 spin_lock(&cm.lock);
1941 /* Check for duplicate REP. */
1942 if (cm_insert_remote_id(cm_id_priv->timewait_info)) {
1943 spin_unlock(&cm.lock);
Sean Hefty24be6e82007-06-18 11:09:36 -07001944 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty87fd1a12006-03-02 16:50:37 -08001945 ret = -EINVAL;
1946 goto error;
1947 }
1948 /* Check for a stale connection. */
1949 if (cm_insert_remote_qpn(cm_id_priv->timewait_info)) {
1950 rb_erase(&cm_id_priv->timewait_info->remote_id_node,
1951 &cm.remote_id_table);
1952 cm_id_priv->timewait_info->inserted_remote_id = 0;
1953 spin_unlock(&cm.lock);
Sean Hefty24be6e82007-06-18 11:09:36 -07001954 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty87fd1a12006-03-02 16:50:37 -08001955 cm_issue_rej(work->port, work->mad_recv_wc,
1956 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP,
1957 NULL, 0);
1958 ret = -EINVAL;
1959 goto error;
1960 }
1961 spin_unlock(&cm.lock);
1962
Hal Rosenstocka9770492005-07-27 11:45:40 -07001963 cm_id_priv->id.state = IB_CM_REP_RCVD;
1964 cm_id_priv->id.remote_id = rep_msg->local_comm_id;
Sean Heftyef700442011-08-02 11:08:22 -07001965 cm_id_priv->remote_qpn = cm_rep_get_qpn(rep_msg, cm_id_priv->qp_type);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001966 cm_id_priv->initiator_depth = rep_msg->resp_resources;
1967 cm_id_priv->responder_resources = rep_msg->initiator_depth;
1968 cm_id_priv->sq_psn = cm_rep_get_starting_psn(rep_msg);
1969 cm_id_priv->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
Sean Hefty1d846122007-06-18 11:09:37 -07001970 cm_id_priv->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1971 cm_id_priv->av.timeout =
1972 cm_ack_timeout(cm_id_priv->target_ack_delay,
1973 cm_id_priv->av.timeout - 1);
1974 cm_id_priv->alt_av.timeout =
1975 cm_ack_timeout(cm_id_priv->target_ack_delay,
1976 cm_id_priv->alt_av.timeout - 1);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001977
1978 /* todo: handle peer_to_peer */
1979
Sean Hefty34816ad2005-10-25 10:51:39 -07001980 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001981 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1982 if (!ret)
1983 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07001984 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001985
1986 if (ret)
1987 cm_process_work(cm_id_priv, work);
1988 else
1989 cm_deref_id(cm_id_priv);
1990 return 0;
1991
Sean Hefty87fd1a12006-03-02 16:50:37 -08001992error:
Hal Rosenstocka9770492005-07-27 11:45:40 -07001993 cm_deref_id(cm_id_priv);
1994 return ret;
1995}
1996
1997static int cm_establish_handler(struct cm_work *work)
1998{
1999 struct cm_id_private *cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002000 int ret;
2001
Sean Heftye1444b52006-11-28 14:57:13 -08002002 /* See comment in cm_establish about lookup. */
Hal Rosenstocka9770492005-07-27 11:45:40 -07002003 cm_id_priv = cm_acquire_id(work->local_id, work->remote_id);
2004 if (!cm_id_priv)
2005 return -EINVAL;
2006
Sean Hefty24be6e82007-06-18 11:09:36 -07002007 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002008 if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
Sean Hefty24be6e82007-06-18 11:09:36 -07002009 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002010 goto out;
2011 }
2012
Sean Hefty34816ad2005-10-25 10:51:39 -07002013 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002014 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2015 if (!ret)
2016 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002017 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002018
2019 if (ret)
2020 cm_process_work(cm_id_priv, work);
2021 else
2022 cm_deref_id(cm_id_priv);
2023 return 0;
2024out:
2025 cm_deref_id(cm_id_priv);
2026 return -EINVAL;
2027}
2028
2029static int cm_rtu_handler(struct cm_work *work)
2030{
2031 struct cm_id_private *cm_id_priv;
2032 struct cm_rtu_msg *rtu_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002033 int ret;
2034
2035 rtu_msg = (struct cm_rtu_msg *)work->mad_recv_wc->recv_buf.mad;
2036 cm_id_priv = cm_acquire_id(rtu_msg->remote_comm_id,
2037 rtu_msg->local_comm_id);
2038 if (!cm_id_priv)
2039 return -EINVAL;
2040
2041 work->cm_event.private_data = &rtu_msg->private_data;
2042
Sean Hefty24be6e82007-06-18 11:09:36 -07002043 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002044 if (cm_id_priv->id.state != IB_CM_REP_SENT &&
2045 cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) {
Sean Hefty24be6e82007-06-18 11:09:36 -07002046 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty9af57b72007-07-16 21:49:35 -07002047 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2048 counter[CM_RTU_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002049 goto out;
2050 }
2051 cm_id_priv->id.state = IB_CM_ESTABLISHED;
2052
Sean Hefty34816ad2005-10-25 10:51:39 -07002053 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002054 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2055 if (!ret)
2056 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002057 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002058
2059 if (ret)
2060 cm_process_work(cm_id_priv, work);
2061 else
2062 cm_deref_id(cm_id_priv);
2063 return 0;
2064out:
2065 cm_deref_id(cm_id_priv);
2066 return -EINVAL;
2067}
2068
2069static void cm_format_dreq(struct cm_dreq_msg *dreq_msg,
2070 struct cm_id_private *cm_id_priv,
2071 const void *private_data,
2072 u8 private_data_len)
2073{
2074 cm_format_mad_hdr(&dreq_msg->hdr, CM_DREQ_ATTR_ID,
2075 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_DREQ));
2076 dreq_msg->local_comm_id = cm_id_priv->id.local_id;
2077 dreq_msg->remote_comm_id = cm_id_priv->id.remote_id;
2078 cm_dreq_set_remote_qpn(dreq_msg, cm_id_priv->remote_qpn);
2079
2080 if (private_data && private_data_len)
2081 memcpy(dreq_msg->private_data, private_data, private_data_len);
2082}
2083
2084int ib_send_cm_dreq(struct ib_cm_id *cm_id,
2085 const void *private_data,
2086 u8 private_data_len)
2087{
2088 struct cm_id_private *cm_id_priv;
2089 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002090 unsigned long flags;
2091 int ret;
2092
2093 if (private_data && private_data_len > IB_CM_DREQ_PRIVATE_DATA_SIZE)
2094 return -EINVAL;
2095
2096 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2097 spin_lock_irqsave(&cm_id_priv->lock, flags);
2098 if (cm_id->state != IB_CM_ESTABLISHED) {
2099 ret = -EINVAL;
2100 goto out;
2101 }
2102
Sean Hefty8d8ac862011-03-03 23:31:06 +00002103 if (cm_id->lap_state == IB_CM_LAP_SENT ||
2104 cm_id->lap_state == IB_CM_MRA_LAP_RCVD)
2105 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2106
Hal Rosenstocka9770492005-07-27 11:45:40 -07002107 ret = cm_alloc_msg(cm_id_priv, &msg);
2108 if (ret) {
2109 cm_enter_timewait(cm_id_priv);
2110 goto out;
2111 }
2112
2113 cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv,
2114 private_data, private_data_len);
Sean Hefty34816ad2005-10-25 10:51:39 -07002115 msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002116 msg->context[1] = (void *) (unsigned long) IB_CM_DREQ_SENT;
2117
Sean Hefty34816ad2005-10-25 10:51:39 -07002118 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002119 if (ret) {
2120 cm_enter_timewait(cm_id_priv);
2121 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2122 cm_free_msg(msg);
2123 return ret;
2124 }
2125
2126 cm_id->state = IB_CM_DREQ_SENT;
2127 cm_id_priv->msg = msg;
2128out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2129 return ret;
2130}
2131EXPORT_SYMBOL(ib_send_cm_dreq);
2132
2133static void cm_format_drep(struct cm_drep_msg *drep_msg,
2134 struct cm_id_private *cm_id_priv,
2135 const void *private_data,
2136 u8 private_data_len)
2137{
2138 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, cm_id_priv->tid);
2139 drep_msg->local_comm_id = cm_id_priv->id.local_id;
2140 drep_msg->remote_comm_id = cm_id_priv->id.remote_id;
2141
2142 if (private_data && private_data_len)
2143 memcpy(drep_msg->private_data, private_data, private_data_len);
2144}
2145
2146int ib_send_cm_drep(struct ib_cm_id *cm_id,
2147 const void *private_data,
2148 u8 private_data_len)
2149{
2150 struct cm_id_private *cm_id_priv;
2151 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002152 unsigned long flags;
2153 void *data;
2154 int ret;
2155
2156 if (private_data && private_data_len > IB_CM_DREP_PRIVATE_DATA_SIZE)
2157 return -EINVAL;
2158
2159 data = cm_copy_private_data(private_data, private_data_len);
2160 if (IS_ERR(data))
2161 return PTR_ERR(data);
2162
2163 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2164 spin_lock_irqsave(&cm_id_priv->lock, flags);
2165 if (cm_id->state != IB_CM_DREQ_RCVD) {
2166 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2167 kfree(data);
2168 return -EINVAL;
2169 }
2170
2171 cm_set_private_data(cm_id_priv, data, private_data_len);
2172 cm_enter_timewait(cm_id_priv);
2173
2174 ret = cm_alloc_msg(cm_id_priv, &msg);
2175 if (ret)
2176 goto out;
2177
2178 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
2179 private_data, private_data_len);
2180
Sean Hefty34816ad2005-10-25 10:51:39 -07002181 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002182 if (ret) {
2183 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2184 cm_free_msg(msg);
2185 return ret;
2186 }
2187
2188out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2189 return ret;
2190}
2191EXPORT_SYMBOL(ib_send_cm_drep);
2192
Sean Hefty82a9c162006-10-04 11:37:25 -07002193static int cm_issue_drep(struct cm_port *port,
2194 struct ib_mad_recv_wc *mad_recv_wc)
2195{
2196 struct ib_mad_send_buf *msg = NULL;
2197 struct cm_dreq_msg *dreq_msg;
2198 struct cm_drep_msg *drep_msg;
2199 int ret;
2200
2201 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
2202 if (ret)
2203 return ret;
2204
2205 dreq_msg = (struct cm_dreq_msg *) mad_recv_wc->recv_buf.mad;
2206 drep_msg = (struct cm_drep_msg *) msg->mad;
2207
2208 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, dreq_msg->hdr.tid);
2209 drep_msg->remote_comm_id = dreq_msg->local_comm_id;
2210 drep_msg->local_comm_id = dreq_msg->remote_comm_id;
2211
2212 ret = ib_post_send_mad(msg, NULL);
2213 if (ret)
2214 cm_free_msg(msg);
2215
2216 return ret;
2217}
2218
Hal Rosenstocka9770492005-07-27 11:45:40 -07002219static int cm_dreq_handler(struct cm_work *work)
2220{
2221 struct cm_id_private *cm_id_priv;
2222 struct cm_dreq_msg *dreq_msg;
2223 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002224 int ret;
2225
2226 dreq_msg = (struct cm_dreq_msg *)work->mad_recv_wc->recv_buf.mad;
2227 cm_id_priv = cm_acquire_id(dreq_msg->remote_comm_id,
2228 dreq_msg->local_comm_id);
Sean Hefty82a9c162006-10-04 11:37:25 -07002229 if (!cm_id_priv) {
Sean Hefty9af57b72007-07-16 21:49:35 -07002230 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2231 counter[CM_DREQ_COUNTER]);
Sean Hefty82a9c162006-10-04 11:37:25 -07002232 cm_issue_drep(work->port, work->mad_recv_wc);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002233 return -EINVAL;
Sean Hefty82a9c162006-10-04 11:37:25 -07002234 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07002235
2236 work->cm_event.private_data = &dreq_msg->private_data;
2237
Sean Hefty24be6e82007-06-18 11:09:36 -07002238 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002239 if (cm_id_priv->local_qpn != cm_dreq_get_remote_qpn(dreq_msg))
2240 goto unlock;
2241
2242 switch (cm_id_priv->id.state) {
2243 case IB_CM_REP_SENT:
2244 case IB_CM_DREQ_SENT:
Sean Hefty34816ad2005-10-25 10:51:39 -07002245 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002246 break;
2247 case IB_CM_ESTABLISHED:
Sean Hefty8d8ac862011-03-03 23:31:06 +00002248 if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT ||
2249 cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
2250 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2251 break;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002252 case IB_CM_MRA_REP_RCVD:
2253 break;
2254 case IB_CM_TIMEWAIT:
Sean Hefty9af57b72007-07-16 21:49:35 -07002255 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2256 counter[CM_DREQ_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002257 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2258 goto unlock;
2259
2260 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
2261 cm_id_priv->private_data,
2262 cm_id_priv->private_data_len);
Sean Hefty24be6e82007-06-18 11:09:36 -07002263 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002264
Sean Hefty34816ad2005-10-25 10:51:39 -07002265 if (ib_post_send_mad(msg, NULL))
Hal Rosenstocka9770492005-07-27 11:45:40 -07002266 cm_free_msg(msg);
2267 goto deref;
Sean Hefty9af57b72007-07-16 21:49:35 -07002268 case IB_CM_DREQ_RCVD:
2269 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2270 counter[CM_DREQ_COUNTER]);
2271 goto unlock;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002272 default:
2273 goto unlock;
2274 }
2275 cm_id_priv->id.state = IB_CM_DREQ_RCVD;
2276 cm_id_priv->tid = dreq_msg->hdr.tid;
2277 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2278 if (!ret)
2279 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002280 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002281
2282 if (ret)
2283 cm_process_work(cm_id_priv, work);
2284 else
2285 cm_deref_id(cm_id_priv);
2286 return 0;
2287
Sean Hefty24be6e82007-06-18 11:09:36 -07002288unlock: spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002289deref: cm_deref_id(cm_id_priv);
2290 return -EINVAL;
2291}
2292
2293static int cm_drep_handler(struct cm_work *work)
2294{
2295 struct cm_id_private *cm_id_priv;
2296 struct cm_drep_msg *drep_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002297 int ret;
2298
2299 drep_msg = (struct cm_drep_msg *)work->mad_recv_wc->recv_buf.mad;
2300 cm_id_priv = cm_acquire_id(drep_msg->remote_comm_id,
2301 drep_msg->local_comm_id);
2302 if (!cm_id_priv)
2303 return -EINVAL;
2304
2305 work->cm_event.private_data = &drep_msg->private_data;
2306
Sean Hefty24be6e82007-06-18 11:09:36 -07002307 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002308 if (cm_id_priv->id.state != IB_CM_DREQ_SENT &&
2309 cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
Sean Hefty24be6e82007-06-18 11:09:36 -07002310 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002311 goto out;
2312 }
2313 cm_enter_timewait(cm_id_priv);
2314
Sean Hefty34816ad2005-10-25 10:51:39 -07002315 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002316 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2317 if (!ret)
2318 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002319 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002320
2321 if (ret)
2322 cm_process_work(cm_id_priv, work);
2323 else
2324 cm_deref_id(cm_id_priv);
2325 return 0;
2326out:
2327 cm_deref_id(cm_id_priv);
2328 return -EINVAL;
2329}
2330
2331int ib_send_cm_rej(struct ib_cm_id *cm_id,
2332 enum ib_cm_rej_reason reason,
2333 void *ari,
2334 u8 ari_length,
2335 const void *private_data,
2336 u8 private_data_len)
2337{
2338 struct cm_id_private *cm_id_priv;
2339 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002340 unsigned long flags;
2341 int ret;
2342
2343 if ((private_data && private_data_len > IB_CM_REJ_PRIVATE_DATA_SIZE) ||
2344 (ari && ari_length > IB_CM_REJ_ARI_LENGTH))
2345 return -EINVAL;
2346
2347 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2348
2349 spin_lock_irqsave(&cm_id_priv->lock, flags);
2350 switch (cm_id->state) {
2351 case IB_CM_REQ_SENT:
2352 case IB_CM_MRA_REQ_RCVD:
2353 case IB_CM_REQ_RCVD:
2354 case IB_CM_MRA_REQ_SENT:
2355 case IB_CM_REP_RCVD:
2356 case IB_CM_MRA_REP_SENT:
2357 ret = cm_alloc_msg(cm_id_priv, &msg);
2358 if (!ret)
2359 cm_format_rej((struct cm_rej_msg *) msg->mad,
2360 cm_id_priv, reason, ari, ari_length,
2361 private_data, private_data_len);
2362
2363 cm_reset_to_idle(cm_id_priv);
2364 break;
2365 case IB_CM_REP_SENT:
2366 case IB_CM_MRA_REP_RCVD:
2367 ret = cm_alloc_msg(cm_id_priv, &msg);
2368 if (!ret)
2369 cm_format_rej((struct cm_rej_msg *) msg->mad,
2370 cm_id_priv, reason, ari, ari_length,
2371 private_data, private_data_len);
2372
2373 cm_enter_timewait(cm_id_priv);
2374 break;
2375 default:
2376 ret = -EINVAL;
2377 goto out;
2378 }
2379
2380 if (ret)
2381 goto out;
2382
Sean Hefty34816ad2005-10-25 10:51:39 -07002383 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002384 if (ret)
2385 cm_free_msg(msg);
2386
2387out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2388 return ret;
2389}
2390EXPORT_SYMBOL(ib_send_cm_rej);
2391
2392static void cm_format_rej_event(struct cm_work *work)
2393{
2394 struct cm_rej_msg *rej_msg;
2395 struct ib_cm_rej_event_param *param;
2396
2397 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2398 param = &work->cm_event.param.rej_rcvd;
2399 param->ari = rej_msg->ari;
2400 param->ari_length = cm_rej_get_reject_info_len(rej_msg);
Sean Hefty97f52eb2005-08-13 21:05:57 -07002401 param->reason = __be16_to_cpu(rej_msg->reason);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002402 work->cm_event.private_data = &rej_msg->private_data;
2403}
2404
2405static struct cm_id_private * cm_acquire_rejected_id(struct cm_rej_msg *rej_msg)
2406{
2407 struct cm_timewait_info *timewait_info;
2408 struct cm_id_private *cm_id_priv;
Sean Hefty97f52eb2005-08-13 21:05:57 -07002409 __be32 remote_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002410
2411 remote_id = rej_msg->local_comm_id;
2412
Sean Hefty97f52eb2005-08-13 21:05:57 -07002413 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_TIMEOUT) {
Sean Hefty24be6e82007-06-18 11:09:36 -07002414 spin_lock_irq(&cm.lock);
Sean Hefty97f52eb2005-08-13 21:05:57 -07002415 timewait_info = cm_find_remote_id( *((__be64 *) rej_msg->ari),
Hal Rosenstocka9770492005-07-27 11:45:40 -07002416 remote_id);
2417 if (!timewait_info) {
Sean Hefty24be6e82007-06-18 11:09:36 -07002418 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002419 return NULL;
2420 }
Sean Heftyf06d2652006-08-28 15:15:18 -07002421 cm_id_priv = idr_find(&cm.local_id_table, (__force int)
2422 (timewait_info->work.local_id ^
2423 cm.random_id_operand));
Hal Rosenstocka9770492005-07-27 11:45:40 -07002424 if (cm_id_priv) {
2425 if (cm_id_priv->id.remote_id == remote_id)
2426 atomic_inc(&cm_id_priv->refcount);
2427 else
2428 cm_id_priv = NULL;
2429 }
Sean Hefty24be6e82007-06-18 11:09:36 -07002430 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002431 } else if (cm_rej_get_msg_rejected(rej_msg) == CM_MSG_RESPONSE_REQ)
2432 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, 0);
2433 else
2434 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, remote_id);
2435
2436 return cm_id_priv;
2437}
2438
2439static int cm_rej_handler(struct cm_work *work)
2440{
2441 struct cm_id_private *cm_id_priv;
2442 struct cm_rej_msg *rej_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002443 int ret;
2444
2445 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2446 cm_id_priv = cm_acquire_rejected_id(rej_msg);
2447 if (!cm_id_priv)
2448 return -EINVAL;
2449
2450 cm_format_rej_event(work);
2451
Sean Hefty24be6e82007-06-18 11:09:36 -07002452 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002453 switch (cm_id_priv->id.state) {
2454 case IB_CM_REQ_SENT:
2455 case IB_CM_MRA_REQ_RCVD:
2456 case IB_CM_REP_SENT:
2457 case IB_CM_MRA_REP_RCVD:
Sean Hefty34816ad2005-10-25 10:51:39 -07002458 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002459 /* fall through */
2460 case IB_CM_REQ_RCVD:
2461 case IB_CM_MRA_REQ_SENT:
Sean Hefty97f52eb2005-08-13 21:05:57 -07002462 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_STALE_CONN)
Hal Rosenstocka9770492005-07-27 11:45:40 -07002463 cm_enter_timewait(cm_id_priv);
2464 else
2465 cm_reset_to_idle(cm_id_priv);
2466 break;
2467 case IB_CM_DREQ_SENT:
Sean Hefty34816ad2005-10-25 10:51:39 -07002468 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002469 /* fall through */
2470 case IB_CM_REP_RCVD:
2471 case IB_CM_MRA_REP_SENT:
Hal Rosenstocka9770492005-07-27 11:45:40 -07002472 cm_enter_timewait(cm_id_priv);
2473 break;
Sean Hefty8d8ac862011-03-03 23:31:06 +00002474 case IB_CM_ESTABLISHED:
2475 if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT ||
2476 cm_id_priv->id.lap_state == IB_CM_LAP_SENT) {
2477 if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT)
2478 ib_cancel_mad(cm_id_priv->av.port->mad_agent,
2479 cm_id_priv->msg);
2480 cm_enter_timewait(cm_id_priv);
2481 break;
2482 }
2483 /* fall through */
Hal Rosenstocka9770492005-07-27 11:45:40 -07002484 default:
Sean Hefty24be6e82007-06-18 11:09:36 -07002485 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002486 ret = -EINVAL;
2487 goto out;
2488 }
2489
2490 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2491 if (!ret)
2492 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002493 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002494
2495 if (ret)
2496 cm_process_work(cm_id_priv, work);
2497 else
2498 cm_deref_id(cm_id_priv);
2499 return 0;
2500out:
2501 cm_deref_id(cm_id_priv);
2502 return -EINVAL;
2503}
2504
2505int ib_send_cm_mra(struct ib_cm_id *cm_id,
2506 u8 service_timeout,
2507 const void *private_data,
2508 u8 private_data_len)
2509{
2510 struct cm_id_private *cm_id_priv;
2511 struct ib_mad_send_buf *msg;
Sean Heftyde98b692007-08-01 13:49:53 -07002512 enum ib_cm_state cm_state;
2513 enum ib_cm_lap_state lap_state;
2514 enum cm_msg_response msg_response;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002515 void *data;
2516 unsigned long flags;
2517 int ret;
2518
2519 if (private_data && private_data_len > IB_CM_MRA_PRIVATE_DATA_SIZE)
2520 return -EINVAL;
2521
2522 data = cm_copy_private_data(private_data, private_data_len);
2523 if (IS_ERR(data))
2524 return PTR_ERR(data);
2525
2526 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2527
2528 spin_lock_irqsave(&cm_id_priv->lock, flags);
2529 switch(cm_id_priv->id.state) {
2530 case IB_CM_REQ_RCVD:
Sean Heftyde98b692007-08-01 13:49:53 -07002531 cm_state = IB_CM_MRA_REQ_SENT;
2532 lap_state = cm_id->lap_state;
2533 msg_response = CM_MSG_RESPONSE_REQ;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002534 break;
2535 case IB_CM_REP_RCVD:
Sean Heftyde98b692007-08-01 13:49:53 -07002536 cm_state = IB_CM_MRA_REP_SENT;
2537 lap_state = cm_id->lap_state;
2538 msg_response = CM_MSG_RESPONSE_REP;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002539 break;
2540 case IB_CM_ESTABLISHED:
Sean Hefty50a025c2010-07-21 23:36:52 +00002541 if (cm_id->lap_state == IB_CM_LAP_RCVD) {
2542 cm_state = cm_id->state;
2543 lap_state = IB_CM_MRA_LAP_SENT;
2544 msg_response = CM_MSG_RESPONSE_OTHER;
2545 break;
2546 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07002547 default:
2548 ret = -EINVAL;
2549 goto error1;
2550 }
Sean Heftyde98b692007-08-01 13:49:53 -07002551
2552 if (!(service_timeout & IB_CM_MRA_FLAG_DELAY)) {
2553 ret = cm_alloc_msg(cm_id_priv, &msg);
2554 if (ret)
2555 goto error1;
2556
2557 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2558 msg_response, service_timeout,
2559 private_data, private_data_len);
2560 ret = ib_post_send_mad(msg, NULL);
2561 if (ret)
2562 goto error2;
2563 }
2564
2565 cm_id->state = cm_state;
2566 cm_id->lap_state = lap_state;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002567 cm_id_priv->service_timeout = service_timeout;
2568 cm_set_private_data(cm_id_priv, data, private_data_len);
2569 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2570 return 0;
2571
2572error1: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2573 kfree(data);
2574 return ret;
2575
2576error2: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2577 kfree(data);
2578 cm_free_msg(msg);
2579 return ret;
2580}
2581EXPORT_SYMBOL(ib_send_cm_mra);
2582
2583static struct cm_id_private * cm_acquire_mraed_id(struct cm_mra_msg *mra_msg)
2584{
2585 switch (cm_mra_get_msg_mraed(mra_msg)) {
2586 case CM_MSG_RESPONSE_REQ:
2587 return cm_acquire_id(mra_msg->remote_comm_id, 0);
2588 case CM_MSG_RESPONSE_REP:
2589 case CM_MSG_RESPONSE_OTHER:
2590 return cm_acquire_id(mra_msg->remote_comm_id,
2591 mra_msg->local_comm_id);
2592 default:
2593 return NULL;
2594 }
2595}
2596
2597static int cm_mra_handler(struct cm_work *work)
2598{
2599 struct cm_id_private *cm_id_priv;
2600 struct cm_mra_msg *mra_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002601 int timeout, ret;
2602
2603 mra_msg = (struct cm_mra_msg *)work->mad_recv_wc->recv_buf.mad;
2604 cm_id_priv = cm_acquire_mraed_id(mra_msg);
2605 if (!cm_id_priv)
2606 return -EINVAL;
2607
2608 work->cm_event.private_data = &mra_msg->private_data;
2609 work->cm_event.param.mra_rcvd.service_timeout =
2610 cm_mra_get_service_timeout(mra_msg);
2611 timeout = cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg)) +
Sean Hefty1d846122007-06-18 11:09:37 -07002612 cm_convert_to_ms(cm_id_priv->av.timeout);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002613
Sean Hefty24be6e82007-06-18 11:09:36 -07002614 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002615 switch (cm_id_priv->id.state) {
2616 case IB_CM_REQ_SENT:
2617 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REQ ||
2618 ib_modify_mad(cm_id_priv->av.port->mad_agent,
Sean Hefty34816ad2005-10-25 10:51:39 -07002619 cm_id_priv->msg, timeout))
Hal Rosenstocka9770492005-07-27 11:45:40 -07002620 goto out;
2621 cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD;
2622 break;
2623 case IB_CM_REP_SENT:
2624 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REP ||
2625 ib_modify_mad(cm_id_priv->av.port->mad_agent,
Sean Hefty34816ad2005-10-25 10:51:39 -07002626 cm_id_priv->msg, timeout))
Hal Rosenstocka9770492005-07-27 11:45:40 -07002627 goto out;
2628 cm_id_priv->id.state = IB_CM_MRA_REP_RCVD;
2629 break;
2630 case IB_CM_ESTABLISHED:
2631 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_OTHER ||
2632 cm_id_priv->id.lap_state != IB_CM_LAP_SENT ||
2633 ib_modify_mad(cm_id_priv->av.port->mad_agent,
Sean Hefty9af57b72007-07-16 21:49:35 -07002634 cm_id_priv->msg, timeout)) {
2635 if (cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
2636 atomic_long_inc(&work->port->
2637 counter_group[CM_RECV_DUPLICATES].
2638 counter[CM_MRA_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002639 goto out;
Sean Hefty9af57b72007-07-16 21:49:35 -07002640 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07002641 cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD;
2642 break;
Sean Hefty9af57b72007-07-16 21:49:35 -07002643 case IB_CM_MRA_REQ_RCVD:
2644 case IB_CM_MRA_REP_RCVD:
2645 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2646 counter[CM_MRA_COUNTER]);
2647 /* fall through */
Hal Rosenstocka9770492005-07-27 11:45:40 -07002648 default:
2649 goto out;
2650 }
2651
2652 cm_id_priv->msg->context[1] = (void *) (unsigned long)
2653 cm_id_priv->id.state;
2654 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2655 if (!ret)
2656 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002657 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002658
2659 if (ret)
2660 cm_process_work(cm_id_priv, work);
2661 else
2662 cm_deref_id(cm_id_priv);
2663 return 0;
2664out:
Sean Hefty24be6e82007-06-18 11:09:36 -07002665 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002666 cm_deref_id(cm_id_priv);
2667 return -EINVAL;
2668}
2669
2670static void cm_format_lap(struct cm_lap_msg *lap_msg,
2671 struct cm_id_private *cm_id_priv,
2672 struct ib_sa_path_rec *alternate_path,
2673 const void *private_data,
2674 u8 private_data_len)
2675{
2676 cm_format_mad_hdr(&lap_msg->hdr, CM_LAP_ATTR_ID,
2677 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_LAP));
2678 lap_msg->local_comm_id = cm_id_priv->id.local_id;
2679 lap_msg->remote_comm_id = cm_id_priv->id.remote_id;
2680 cm_lap_set_remote_qpn(lap_msg, cm_id_priv->remote_qpn);
2681 /* todo: need remote CM response timeout */
2682 cm_lap_set_remote_resp_timeout(lap_msg, 0x1F);
2683 lap_msg->alt_local_lid = alternate_path->slid;
2684 lap_msg->alt_remote_lid = alternate_path->dlid;
2685 lap_msg->alt_local_gid = alternate_path->sgid;
2686 lap_msg->alt_remote_gid = alternate_path->dgid;
2687 cm_lap_set_flow_label(lap_msg, alternate_path->flow_label);
2688 cm_lap_set_traffic_class(lap_msg, alternate_path->traffic_class);
2689 lap_msg->alt_hop_limit = alternate_path->hop_limit;
2690 cm_lap_set_packet_rate(lap_msg, alternate_path->rate);
2691 cm_lap_set_sl(lap_msg, alternate_path->sl);
2692 cm_lap_set_subnet_local(lap_msg, 1); /* local only... */
2693 cm_lap_set_local_ack_timeout(lap_msg,
Sean Hefty1d846122007-06-18 11:09:37 -07002694 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
2695 alternate_path->packet_life_time));
Hal Rosenstocka9770492005-07-27 11:45:40 -07002696
2697 if (private_data && private_data_len)
2698 memcpy(lap_msg->private_data, private_data, private_data_len);
2699}
2700
2701int ib_send_cm_lap(struct ib_cm_id *cm_id,
2702 struct ib_sa_path_rec *alternate_path,
2703 const void *private_data,
2704 u8 private_data_len)
2705{
2706 struct cm_id_private *cm_id_priv;
2707 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002708 unsigned long flags;
2709 int ret;
2710
2711 if (private_data && private_data_len > IB_CM_LAP_PRIVATE_DATA_SIZE)
2712 return -EINVAL;
2713
2714 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2715 spin_lock_irqsave(&cm_id_priv->lock, flags);
2716 if (cm_id->state != IB_CM_ESTABLISHED ||
Sean Heftye1444b52006-11-28 14:57:13 -08002717 (cm_id->lap_state != IB_CM_LAP_UNINIT &&
2718 cm_id->lap_state != IB_CM_LAP_IDLE)) {
Hal Rosenstocka9770492005-07-27 11:45:40 -07002719 ret = -EINVAL;
2720 goto out;
2721 }
2722
Sean Heftye1444b52006-11-28 14:57:13 -08002723 ret = cm_init_av_by_path(alternate_path, &cm_id_priv->alt_av);
2724 if (ret)
2725 goto out;
Sean Hefty1d846122007-06-18 11:09:37 -07002726 cm_id_priv->alt_av.timeout =
2727 cm_ack_timeout(cm_id_priv->target_ack_delay,
2728 cm_id_priv->alt_av.timeout - 1);
Sean Heftye1444b52006-11-28 14:57:13 -08002729
Hal Rosenstocka9770492005-07-27 11:45:40 -07002730 ret = cm_alloc_msg(cm_id_priv, &msg);
2731 if (ret)
2732 goto out;
2733
2734 cm_format_lap((struct cm_lap_msg *) msg->mad, cm_id_priv,
2735 alternate_path, private_data, private_data_len);
Sean Hefty34816ad2005-10-25 10:51:39 -07002736 msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002737 msg->context[1] = (void *) (unsigned long) IB_CM_ESTABLISHED;
2738
Sean Hefty34816ad2005-10-25 10:51:39 -07002739 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002740 if (ret) {
2741 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2742 cm_free_msg(msg);
2743 return ret;
2744 }
2745
2746 cm_id->lap_state = IB_CM_LAP_SENT;
2747 cm_id_priv->msg = msg;
2748
2749out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2750 return ret;
2751}
2752EXPORT_SYMBOL(ib_send_cm_lap);
2753
Sean Heftye1444b52006-11-28 14:57:13 -08002754static void cm_format_path_from_lap(struct cm_id_private *cm_id_priv,
2755 struct ib_sa_path_rec *path,
Hal Rosenstocka9770492005-07-27 11:45:40 -07002756 struct cm_lap_msg *lap_msg)
2757{
2758 memset(path, 0, sizeof *path);
2759 path->dgid = lap_msg->alt_local_gid;
2760 path->sgid = lap_msg->alt_remote_gid;
2761 path->dlid = lap_msg->alt_local_lid;
2762 path->slid = lap_msg->alt_remote_lid;
2763 path->flow_label = cm_lap_get_flow_label(lap_msg);
2764 path->hop_limit = lap_msg->alt_hop_limit;
2765 path->traffic_class = cm_lap_get_traffic_class(lap_msg);
2766 path->reversible = 1;
Sean Heftye1444b52006-11-28 14:57:13 -08002767 path->pkey = cm_id_priv->pkey;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002768 path->sl = cm_lap_get_sl(lap_msg);
2769 path->mtu_selector = IB_SA_EQ;
Sean Heftye1444b52006-11-28 14:57:13 -08002770 path->mtu = cm_id_priv->path_mtu;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002771 path->rate_selector = IB_SA_EQ;
2772 path->rate = cm_lap_get_packet_rate(lap_msg);
2773 path->packet_life_time_selector = IB_SA_EQ;
2774 path->packet_life_time = cm_lap_get_local_ack_timeout(lap_msg);
2775 path->packet_life_time -= (path->packet_life_time > 0);
2776}
2777
2778static int cm_lap_handler(struct cm_work *work)
2779{
2780 struct cm_id_private *cm_id_priv;
2781 struct cm_lap_msg *lap_msg;
2782 struct ib_cm_lap_event_param *param;
2783 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002784 int ret;
2785
2786 /* todo: verify LAP request and send reject APR if invalid. */
2787 lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad;
2788 cm_id_priv = cm_acquire_id(lap_msg->remote_comm_id,
2789 lap_msg->local_comm_id);
2790 if (!cm_id_priv)
2791 return -EINVAL;
2792
2793 param = &work->cm_event.param.lap_rcvd;
2794 param->alternate_path = &work->path[0];
Sean Heftye1444b52006-11-28 14:57:13 -08002795 cm_format_path_from_lap(cm_id_priv, param->alternate_path, lap_msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002796 work->cm_event.private_data = &lap_msg->private_data;
2797
Sean Hefty24be6e82007-06-18 11:09:36 -07002798 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002799 if (cm_id_priv->id.state != IB_CM_ESTABLISHED)
2800 goto unlock;
2801
2802 switch (cm_id_priv->id.lap_state) {
Sean Heftye1444b52006-11-28 14:57:13 -08002803 case IB_CM_LAP_UNINIT:
Hal Rosenstocka9770492005-07-27 11:45:40 -07002804 case IB_CM_LAP_IDLE:
2805 break;
2806 case IB_CM_MRA_LAP_SENT:
Sean Hefty9af57b72007-07-16 21:49:35 -07002807 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2808 counter[CM_LAP_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002809 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2810 goto unlock;
2811
2812 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2813 CM_MSG_RESPONSE_OTHER,
2814 cm_id_priv->service_timeout,
2815 cm_id_priv->private_data,
2816 cm_id_priv->private_data_len);
Sean Hefty24be6e82007-06-18 11:09:36 -07002817 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002818
Sean Hefty34816ad2005-10-25 10:51:39 -07002819 if (ib_post_send_mad(msg, NULL))
Hal Rosenstocka9770492005-07-27 11:45:40 -07002820 cm_free_msg(msg);
2821 goto deref;
Sean Hefty9af57b72007-07-16 21:49:35 -07002822 case IB_CM_LAP_RCVD:
2823 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2824 counter[CM_LAP_COUNTER]);
2825 goto unlock;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002826 default:
2827 goto unlock;
2828 }
2829
2830 cm_id_priv->id.lap_state = IB_CM_LAP_RCVD;
2831 cm_id_priv->tid = lap_msg->hdr.tid;
Sean Heftye1444b52006-11-28 14:57:13 -08002832 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
2833 work->mad_recv_wc->recv_buf.grh,
2834 &cm_id_priv->av);
2835 cm_init_av_by_path(param->alternate_path, &cm_id_priv->alt_av);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002836 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2837 if (!ret)
2838 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002839 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002840
2841 if (ret)
2842 cm_process_work(cm_id_priv, work);
2843 else
2844 cm_deref_id(cm_id_priv);
2845 return 0;
2846
Sean Hefty24be6e82007-06-18 11:09:36 -07002847unlock: spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002848deref: cm_deref_id(cm_id_priv);
2849 return -EINVAL;
2850}
2851
2852static void cm_format_apr(struct cm_apr_msg *apr_msg,
2853 struct cm_id_private *cm_id_priv,
2854 enum ib_cm_apr_status status,
2855 void *info,
2856 u8 info_length,
2857 const void *private_data,
2858 u8 private_data_len)
2859{
2860 cm_format_mad_hdr(&apr_msg->hdr, CM_APR_ATTR_ID, cm_id_priv->tid);
2861 apr_msg->local_comm_id = cm_id_priv->id.local_id;
2862 apr_msg->remote_comm_id = cm_id_priv->id.remote_id;
2863 apr_msg->ap_status = (u8) status;
2864
2865 if (info && info_length) {
2866 apr_msg->info_length = info_length;
2867 memcpy(apr_msg->info, info, info_length);
2868 }
2869
2870 if (private_data && private_data_len)
2871 memcpy(apr_msg->private_data, private_data, private_data_len);
2872}
2873
2874int ib_send_cm_apr(struct ib_cm_id *cm_id,
2875 enum ib_cm_apr_status status,
2876 void *info,
2877 u8 info_length,
2878 const void *private_data,
2879 u8 private_data_len)
2880{
2881 struct cm_id_private *cm_id_priv;
2882 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002883 unsigned long flags;
2884 int ret;
2885
2886 if ((private_data && private_data_len > IB_CM_APR_PRIVATE_DATA_SIZE) ||
2887 (info && info_length > IB_CM_APR_INFO_LENGTH))
2888 return -EINVAL;
2889
2890 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2891 spin_lock_irqsave(&cm_id_priv->lock, flags);
2892 if (cm_id->state != IB_CM_ESTABLISHED ||
2893 (cm_id->lap_state != IB_CM_LAP_RCVD &&
2894 cm_id->lap_state != IB_CM_MRA_LAP_SENT)) {
2895 ret = -EINVAL;
2896 goto out;
2897 }
2898
2899 ret = cm_alloc_msg(cm_id_priv, &msg);
2900 if (ret)
2901 goto out;
2902
2903 cm_format_apr((struct cm_apr_msg *) msg->mad, cm_id_priv, status,
2904 info, info_length, private_data, private_data_len);
Sean Hefty34816ad2005-10-25 10:51:39 -07002905 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002906 if (ret) {
2907 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2908 cm_free_msg(msg);
2909 return ret;
2910 }
2911
2912 cm_id->lap_state = IB_CM_LAP_IDLE;
2913out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2914 return ret;
2915}
2916EXPORT_SYMBOL(ib_send_cm_apr);
2917
2918static int cm_apr_handler(struct cm_work *work)
2919{
2920 struct cm_id_private *cm_id_priv;
2921 struct cm_apr_msg *apr_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002922 int ret;
2923
2924 apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad;
2925 cm_id_priv = cm_acquire_id(apr_msg->remote_comm_id,
2926 apr_msg->local_comm_id);
2927 if (!cm_id_priv)
2928 return -EINVAL; /* Unmatched reply. */
2929
2930 work->cm_event.param.apr_rcvd.ap_status = apr_msg->ap_status;
2931 work->cm_event.param.apr_rcvd.apr_info = &apr_msg->info;
2932 work->cm_event.param.apr_rcvd.info_len = apr_msg->info_length;
2933 work->cm_event.private_data = &apr_msg->private_data;
2934
Sean Hefty24be6e82007-06-18 11:09:36 -07002935 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002936 if (cm_id_priv->id.state != IB_CM_ESTABLISHED ||
2937 (cm_id_priv->id.lap_state != IB_CM_LAP_SENT &&
2938 cm_id_priv->id.lap_state != IB_CM_MRA_LAP_RCVD)) {
Sean Hefty24be6e82007-06-18 11:09:36 -07002939 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002940 goto out;
2941 }
2942 cm_id_priv->id.lap_state = IB_CM_LAP_IDLE;
Sean Hefty34816ad2005-10-25 10:51:39 -07002943 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002944 cm_id_priv->msg = NULL;
2945
2946 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2947 if (!ret)
2948 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002949 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002950
2951 if (ret)
2952 cm_process_work(cm_id_priv, work);
2953 else
2954 cm_deref_id(cm_id_priv);
2955 return 0;
2956out:
2957 cm_deref_id(cm_id_priv);
2958 return -EINVAL;
2959}
2960
2961static int cm_timewait_handler(struct cm_work *work)
2962{
2963 struct cm_timewait_info *timewait_info;
2964 struct cm_id_private *cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002965 int ret;
2966
2967 timewait_info = (struct cm_timewait_info *)work;
Sean Hefty85753292006-10-04 11:29:59 -07002968 spin_lock_irq(&cm.lock);
2969 list_del(&timewait_info->list);
2970 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002971
2972 cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
2973 timewait_info->work.remote_id);
2974 if (!cm_id_priv)
2975 return -EINVAL;
2976
Sean Hefty85753292006-10-04 11:29:59 -07002977 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002978 if (cm_id_priv->id.state != IB_CM_TIMEWAIT ||
2979 cm_id_priv->remote_qpn != timewait_info->remote_qpn) {
Sean Hefty85753292006-10-04 11:29:59 -07002980 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002981 goto out;
2982 }
2983 cm_id_priv->id.state = IB_CM_IDLE;
2984 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2985 if (!ret)
2986 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty85753292006-10-04 11:29:59 -07002987 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002988
2989 if (ret)
2990 cm_process_work(cm_id_priv, work);
2991 else
2992 cm_deref_id(cm_id_priv);
2993 return 0;
2994out:
2995 cm_deref_id(cm_id_priv);
2996 return -EINVAL;
2997}
2998
2999static void cm_format_sidr_req(struct cm_sidr_req_msg *sidr_req_msg,
3000 struct cm_id_private *cm_id_priv,
3001 struct ib_cm_sidr_req_param *param)
3002{
3003 cm_format_mad_hdr(&sidr_req_msg->hdr, CM_SIDR_REQ_ATTR_ID,
3004 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_SIDR));
3005 sidr_req_msg->request_id = cm_id_priv->id.local_id;
Roland Dreiera88f4882008-04-16 21:01:07 -07003006 sidr_req_msg->pkey = param->path->pkey;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003007 sidr_req_msg->service_id = param->service_id;
3008
3009 if (param->private_data && param->private_data_len)
3010 memcpy(sidr_req_msg->private_data, param->private_data,
3011 param->private_data_len);
3012}
3013
3014int ib_send_cm_sidr_req(struct ib_cm_id *cm_id,
3015 struct ib_cm_sidr_req_param *param)
3016{
3017 struct cm_id_private *cm_id_priv;
3018 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003019 unsigned long flags;
3020 int ret;
3021
3022 if (!param->path || (param->private_data &&
3023 param->private_data_len > IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE))
3024 return -EINVAL;
3025
3026 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3027 ret = cm_init_av_by_path(param->path, &cm_id_priv->av);
3028 if (ret)
3029 goto out;
3030
3031 cm_id->service_id = param->service_id;
Harvey Harrison9c3da092009-01-17 17:11:57 -08003032 cm_id->service_mask = ~cpu_to_be64(0);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003033 cm_id_priv->timeout_ms = param->timeout_ms;
3034 cm_id_priv->max_cm_retries = param->max_cm_retries;
3035 ret = cm_alloc_msg(cm_id_priv, &msg);
3036 if (ret)
3037 goto out;
3038
3039 cm_format_sidr_req((struct cm_sidr_req_msg *) msg->mad, cm_id_priv,
3040 param);
Sean Hefty34816ad2005-10-25 10:51:39 -07003041 msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003042 msg->context[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT;
3043
3044 spin_lock_irqsave(&cm_id_priv->lock, flags);
3045 if (cm_id->state == IB_CM_IDLE)
Sean Hefty34816ad2005-10-25 10:51:39 -07003046 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003047 else
3048 ret = -EINVAL;
3049
3050 if (ret) {
3051 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3052 cm_free_msg(msg);
3053 goto out;
3054 }
3055 cm_id->state = IB_CM_SIDR_REQ_SENT;
3056 cm_id_priv->msg = msg;
3057 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3058out:
3059 return ret;
3060}
3061EXPORT_SYMBOL(ib_send_cm_sidr_req);
3062
3063static void cm_format_sidr_req_event(struct cm_work *work,
3064 struct ib_cm_id *listen_id)
3065{
3066 struct cm_sidr_req_msg *sidr_req_msg;
3067 struct ib_cm_sidr_req_event_param *param;
3068
3069 sidr_req_msg = (struct cm_sidr_req_msg *)
3070 work->mad_recv_wc->recv_buf.mad;
3071 param = &work->cm_event.param.sidr_req_rcvd;
Sean Hefty97f52eb2005-08-13 21:05:57 -07003072 param->pkey = __be16_to_cpu(sidr_req_msg->pkey);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003073 param->listen_id = listen_id;
Haggai Eran15865e7d2015-07-30 17:50:17 +03003074 param->service_id = sidr_req_msg->service_id;
Haggai Eran24cad9a2015-07-30 17:50:21 +03003075 param->bth_pkey = cm_get_bth_pkey(work);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003076 param->port = work->port->port_num;
3077 work->cm_event.private_data = &sidr_req_msg->private_data;
3078}
3079
3080static int cm_sidr_req_handler(struct cm_work *work)
3081{
3082 struct ib_cm_id *cm_id;
3083 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
3084 struct cm_sidr_req_msg *sidr_req_msg;
3085 struct ib_wc *wc;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003086
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003087 cm_id = ib_create_cm_id(work->port->cm_dev->ib_device, NULL, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003088 if (IS_ERR(cm_id))
3089 return PTR_ERR(cm_id);
3090 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3091
3092 /* Record SGID/SLID and request ID for lookup. */
3093 sidr_req_msg = (struct cm_sidr_req_msg *)
3094 work->mad_recv_wc->recv_buf.mad;
3095 wc = work->mad_recv_wc->wc;
Sean Hefty97f52eb2005-08-13 21:05:57 -07003096 cm_id_priv->av.dgid.global.subnet_prefix = cpu_to_be64(wc->slid);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003097 cm_id_priv->av.dgid.global.interface_id = 0;
3098 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
Sean Heftyca222c62006-06-17 20:37:40 -07003099 work->mad_recv_wc->recv_buf.grh,
Hal Rosenstocka9770492005-07-27 11:45:40 -07003100 &cm_id_priv->av);
3101 cm_id_priv->id.remote_id = sidr_req_msg->request_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003102 cm_id_priv->tid = sidr_req_msg->hdr.tid;
3103 atomic_inc(&cm_id_priv->work_count);
3104
Sean Hefty24be6e82007-06-18 11:09:36 -07003105 spin_lock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003106 cur_cm_id_priv = cm_insert_remote_sidr(cm_id_priv);
3107 if (cur_cm_id_priv) {
Sean Hefty24be6e82007-06-18 11:09:36 -07003108 spin_unlock_irq(&cm.lock);
Sean Hefty9af57b72007-07-16 21:49:35 -07003109 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
3110 counter[CM_SIDR_REQ_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003111 goto out; /* Duplicate message. */
3112 }
Sean Hefty29c27312007-07-02 14:36:19 -07003113 cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD;
Sean Hefty07d357d2005-10-17 15:37:43 -07003114 cur_cm_id_priv = cm_find_listen(cm_id->device,
Haggai Eran73fec7f2015-07-30 17:50:26 +03003115 sidr_req_msg->service_id);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003116 if (!cur_cm_id_priv) {
Sean Hefty24be6e82007-06-18 11:09:36 -07003117 spin_unlock_irq(&cm.lock);
Sean Hefty6164c8c2007-07-02 16:14:14 -07003118 cm_reject_sidr_req(cm_id_priv, IB_SIDR_UNSUPPORTED);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003119 goto out; /* No match. */
3120 }
3121 atomic_inc(&cur_cm_id_priv->refcount);
Sean Hefty29963432011-02-23 08:17:40 -08003122 atomic_inc(&cm_id_priv->refcount);
Sean Hefty24be6e82007-06-18 11:09:36 -07003123 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003124
3125 cm_id_priv->id.cm_handler = cur_cm_id_priv->id.cm_handler;
3126 cm_id_priv->id.context = cur_cm_id_priv->id.context;
3127 cm_id_priv->id.service_id = sidr_req_msg->service_id;
Harvey Harrison9c3da092009-01-17 17:11:57 -08003128 cm_id_priv->id.service_mask = ~cpu_to_be64(0);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003129
3130 cm_format_sidr_req_event(work, &cur_cm_id_priv->id);
3131 cm_process_work(cm_id_priv, work);
3132 cm_deref_id(cur_cm_id_priv);
3133 return 0;
3134out:
3135 ib_destroy_cm_id(&cm_id_priv->id);
3136 return -EINVAL;
3137}
3138
3139static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg,
3140 struct cm_id_private *cm_id_priv,
3141 struct ib_cm_sidr_rep_param *param)
3142{
3143 cm_format_mad_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID,
3144 cm_id_priv->tid);
3145 sidr_rep_msg->request_id = cm_id_priv->id.remote_id;
3146 sidr_rep_msg->status = param->status;
3147 cm_sidr_rep_set_qpn(sidr_rep_msg, cpu_to_be32(param->qp_num));
3148 sidr_rep_msg->service_id = cm_id_priv->id.service_id;
3149 sidr_rep_msg->qkey = cpu_to_be32(param->qkey);
3150
3151 if (param->info && param->info_length)
3152 memcpy(sidr_rep_msg->info, param->info, param->info_length);
3153
3154 if (param->private_data && param->private_data_len)
3155 memcpy(sidr_rep_msg->private_data, param->private_data,
3156 param->private_data_len);
3157}
3158
3159int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id,
3160 struct ib_cm_sidr_rep_param *param)
3161{
3162 struct cm_id_private *cm_id_priv;
3163 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003164 unsigned long flags;
3165 int ret;
3166
3167 if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) ||
3168 (param->private_data &&
3169 param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE))
3170 return -EINVAL;
3171
3172 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3173 spin_lock_irqsave(&cm_id_priv->lock, flags);
3174 if (cm_id->state != IB_CM_SIDR_REQ_RCVD) {
3175 ret = -EINVAL;
3176 goto error;
3177 }
3178
3179 ret = cm_alloc_msg(cm_id_priv, &msg);
3180 if (ret)
3181 goto error;
3182
3183 cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv,
3184 param);
Sean Hefty34816ad2005-10-25 10:51:39 -07003185 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003186 if (ret) {
3187 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3188 cm_free_msg(msg);
3189 return ret;
3190 }
3191 cm_id->state = IB_CM_IDLE;
3192 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3193
3194 spin_lock_irqsave(&cm.lock, flags);
Doron Tsur0ca81a22015-10-11 15:58:17 +03003195 if (!RB_EMPTY_NODE(&cm_id_priv->sidr_id_node)) {
3196 rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
3197 RB_CLEAR_NODE(&cm_id_priv->sidr_id_node);
3198 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07003199 spin_unlock_irqrestore(&cm.lock, flags);
3200 return 0;
3201
3202error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3203 return ret;
3204}
3205EXPORT_SYMBOL(ib_send_cm_sidr_rep);
3206
3207static void cm_format_sidr_rep_event(struct cm_work *work)
3208{
3209 struct cm_sidr_rep_msg *sidr_rep_msg;
3210 struct ib_cm_sidr_rep_event_param *param;
3211
3212 sidr_rep_msg = (struct cm_sidr_rep_msg *)
3213 work->mad_recv_wc->recv_buf.mad;
3214 param = &work->cm_event.param.sidr_rep_rcvd;
3215 param->status = sidr_rep_msg->status;
3216 param->qkey = be32_to_cpu(sidr_rep_msg->qkey);
3217 param->qpn = be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg));
3218 param->info = &sidr_rep_msg->info;
3219 param->info_len = sidr_rep_msg->info_length;
3220 work->cm_event.private_data = &sidr_rep_msg->private_data;
3221}
3222
3223static int cm_sidr_rep_handler(struct cm_work *work)
3224{
3225 struct cm_sidr_rep_msg *sidr_rep_msg;
3226 struct cm_id_private *cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003227
3228 sidr_rep_msg = (struct cm_sidr_rep_msg *)
3229 work->mad_recv_wc->recv_buf.mad;
3230 cm_id_priv = cm_acquire_id(sidr_rep_msg->request_id, 0);
3231 if (!cm_id_priv)
3232 return -EINVAL; /* Unmatched reply. */
3233
Sean Hefty24be6e82007-06-18 11:09:36 -07003234 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003235 if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) {
Sean Hefty24be6e82007-06-18 11:09:36 -07003236 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003237 goto out;
3238 }
3239 cm_id_priv->id.state = IB_CM_IDLE;
Sean Hefty34816ad2005-10-25 10:51:39 -07003240 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Sean Hefty24be6e82007-06-18 11:09:36 -07003241 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003242
3243 cm_format_sidr_rep_event(work);
3244 cm_process_work(cm_id_priv, work);
3245 return 0;
3246out:
3247 cm_deref_id(cm_id_priv);
3248 return -EINVAL;
3249}
3250
3251static void cm_process_send_error(struct ib_mad_send_buf *msg,
3252 enum ib_wc_status wc_status)
3253{
3254 struct cm_id_private *cm_id_priv;
3255 struct ib_cm_event cm_event;
3256 enum ib_cm_state state;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003257 int ret;
3258
3259 memset(&cm_event, 0, sizeof cm_event);
3260 cm_id_priv = msg->context[0];
3261
3262 /* Discard old sends or ones without a response. */
Sean Hefty24be6e82007-06-18 11:09:36 -07003263 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003264 state = (enum ib_cm_state) (unsigned long) msg->context[1];
3265 if (msg != cm_id_priv->msg || state != cm_id_priv->id.state)
3266 goto discard;
3267
3268 switch (state) {
3269 case IB_CM_REQ_SENT:
3270 case IB_CM_MRA_REQ_RCVD:
3271 cm_reset_to_idle(cm_id_priv);
3272 cm_event.event = IB_CM_REQ_ERROR;
3273 break;
3274 case IB_CM_REP_SENT:
3275 case IB_CM_MRA_REP_RCVD:
3276 cm_reset_to_idle(cm_id_priv);
3277 cm_event.event = IB_CM_REP_ERROR;
3278 break;
3279 case IB_CM_DREQ_SENT:
3280 cm_enter_timewait(cm_id_priv);
3281 cm_event.event = IB_CM_DREQ_ERROR;
3282 break;
3283 case IB_CM_SIDR_REQ_SENT:
3284 cm_id_priv->id.state = IB_CM_IDLE;
3285 cm_event.event = IB_CM_SIDR_REQ_ERROR;
3286 break;
3287 default:
3288 goto discard;
3289 }
Sean Hefty24be6e82007-06-18 11:09:36 -07003290 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003291 cm_event.param.send_status = wc_status;
3292
3293 /* No other events can occur on the cm_id at this point. */
3294 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event);
3295 cm_free_msg(msg);
3296 if (ret)
3297 ib_destroy_cm_id(&cm_id_priv->id);
3298 return;
3299discard:
Sean Hefty24be6e82007-06-18 11:09:36 -07003300 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003301 cm_free_msg(msg);
3302}
3303
3304static void cm_send_handler(struct ib_mad_agent *mad_agent,
3305 struct ib_mad_send_wc *mad_send_wc)
3306{
Sean Hefty34816ad2005-10-25 10:51:39 -07003307 struct ib_mad_send_buf *msg = mad_send_wc->send_buf;
Sean Hefty9af57b72007-07-16 21:49:35 -07003308 struct cm_port *port;
3309 u16 attr_index;
3310
3311 port = mad_agent->context;
3312 attr_index = be16_to_cpu(((struct ib_mad_hdr *)
3313 msg->mad)->attr_id) - CM_ATTR_ID_OFFSET;
3314
3315 /*
3316 * If the send was in response to a received message (context[0] is not
3317 * set to a cm_id), and is not a REJ, then it is a send that was
3318 * manually retried.
3319 */
3320 if (!msg->context[0] && (attr_index != CM_REJ_COUNTER))
3321 msg->retries = 1;
3322
3323 atomic_long_add(1 + msg->retries,
3324 &port->counter_group[CM_XMIT].counter[attr_index]);
3325 if (msg->retries)
3326 atomic_long_add(msg->retries,
3327 &port->counter_group[CM_XMIT_RETRIES].
3328 counter[attr_index]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003329
3330 switch (mad_send_wc->status) {
3331 case IB_WC_SUCCESS:
3332 case IB_WC_WR_FLUSH_ERR:
3333 cm_free_msg(msg);
3334 break;
3335 default:
3336 if (msg->context[0] && msg->context[1])
3337 cm_process_send_error(msg, mad_send_wc->status);
3338 else
3339 cm_free_msg(msg);
3340 break;
3341 }
3342}
3343
David Howellsc4028952006-11-22 14:57:56 +00003344static void cm_work_handler(struct work_struct *_work)
Hal Rosenstocka9770492005-07-27 11:45:40 -07003345{
David Howellsc4028952006-11-22 14:57:56 +00003346 struct cm_work *work = container_of(_work, struct cm_work, work.work);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003347 int ret;
3348
3349 switch (work->cm_event.event) {
3350 case IB_CM_REQ_RECEIVED:
3351 ret = cm_req_handler(work);
3352 break;
3353 case IB_CM_MRA_RECEIVED:
3354 ret = cm_mra_handler(work);
3355 break;
3356 case IB_CM_REJ_RECEIVED:
3357 ret = cm_rej_handler(work);
3358 break;
3359 case IB_CM_REP_RECEIVED:
3360 ret = cm_rep_handler(work);
3361 break;
3362 case IB_CM_RTU_RECEIVED:
3363 ret = cm_rtu_handler(work);
3364 break;
3365 case IB_CM_USER_ESTABLISHED:
3366 ret = cm_establish_handler(work);
3367 break;
3368 case IB_CM_DREQ_RECEIVED:
3369 ret = cm_dreq_handler(work);
3370 break;
3371 case IB_CM_DREP_RECEIVED:
3372 ret = cm_drep_handler(work);
3373 break;
3374 case IB_CM_SIDR_REQ_RECEIVED:
3375 ret = cm_sidr_req_handler(work);
3376 break;
3377 case IB_CM_SIDR_REP_RECEIVED:
3378 ret = cm_sidr_rep_handler(work);
3379 break;
3380 case IB_CM_LAP_RECEIVED:
3381 ret = cm_lap_handler(work);
3382 break;
3383 case IB_CM_APR_RECEIVED:
3384 ret = cm_apr_handler(work);
3385 break;
3386 case IB_CM_TIMEWAIT_EXIT:
3387 ret = cm_timewait_handler(work);
3388 break;
3389 default:
3390 ret = -EINVAL;
3391 break;
3392 }
3393 if (ret)
3394 cm_free_work(work);
3395}
3396
Sean Heftye1444b52006-11-28 14:57:13 -08003397static int cm_establish(struct ib_cm_id *cm_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -07003398{
3399 struct cm_id_private *cm_id_priv;
3400 struct cm_work *work;
3401 unsigned long flags;
3402 int ret = 0;
Erez Shitritbe4b4992015-06-25 17:13:22 +03003403 struct cm_device *cm_dev;
3404
3405 cm_dev = ib_get_client_data(cm_id->device, &cm_client);
3406 if (!cm_dev)
3407 return -ENODEV;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003408
3409 work = kmalloc(sizeof *work, GFP_ATOMIC);
3410 if (!work)
3411 return -ENOMEM;
3412
3413 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3414 spin_lock_irqsave(&cm_id_priv->lock, flags);
3415 switch (cm_id->state)
3416 {
3417 case IB_CM_REP_SENT:
3418 case IB_CM_MRA_REP_RCVD:
3419 cm_id->state = IB_CM_ESTABLISHED;
3420 break;
3421 case IB_CM_ESTABLISHED:
3422 ret = -EISCONN;
3423 break;
3424 default:
3425 ret = -EINVAL;
3426 break;
3427 }
3428 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3429
3430 if (ret) {
3431 kfree(work);
3432 goto out;
3433 }
3434
3435 /*
3436 * The CM worker thread may try to destroy the cm_id before it
3437 * can execute this work item. To prevent potential deadlock,
3438 * we need to find the cm_id once we're in the context of the
3439 * worker thread, rather than holding a reference on it.
3440 */
David Howellsc4028952006-11-22 14:57:56 +00003441 INIT_DELAYED_WORK(&work->work, cm_work_handler);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003442 work->local_id = cm_id->local_id;
3443 work->remote_id = cm_id->remote_id;
3444 work->mad_recv_wc = NULL;
3445 work->cm_event.event = IB_CM_USER_ESTABLISHED;
Erez Shitritbe4b4992015-06-25 17:13:22 +03003446
3447 /* Check if the device started its remove_one */
3448 spin_lock_irq(&cm.lock);
3449 if (!cm_dev->going_down) {
3450 queue_delayed_work(cm.wq, &work->work, 0);
3451 } else {
3452 kfree(work);
3453 ret = -ENODEV;
3454 }
3455 spin_unlock_irq(&cm.lock);
3456
Hal Rosenstocka9770492005-07-27 11:45:40 -07003457out:
3458 return ret;
3459}
Sean Heftye1444b52006-11-28 14:57:13 -08003460
3461static int cm_migrate(struct ib_cm_id *cm_id)
3462{
3463 struct cm_id_private *cm_id_priv;
3464 unsigned long flags;
3465 int ret = 0;
3466
3467 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3468 spin_lock_irqsave(&cm_id_priv->lock, flags);
3469 if (cm_id->state == IB_CM_ESTABLISHED &&
3470 (cm_id->lap_state == IB_CM_LAP_UNINIT ||
3471 cm_id->lap_state == IB_CM_LAP_IDLE)) {
3472 cm_id->lap_state = IB_CM_LAP_IDLE;
3473 cm_id_priv->av = cm_id_priv->alt_av;
3474 } else
3475 ret = -EINVAL;
3476 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3477
3478 return ret;
3479}
3480
3481int ib_cm_notify(struct ib_cm_id *cm_id, enum ib_event_type event)
3482{
3483 int ret;
3484
3485 switch (event) {
3486 case IB_EVENT_COMM_EST:
3487 ret = cm_establish(cm_id);
3488 break;
3489 case IB_EVENT_PATH_MIG:
3490 ret = cm_migrate(cm_id);
3491 break;
3492 default:
3493 ret = -EINVAL;
3494 }
3495 return ret;
3496}
3497EXPORT_SYMBOL(ib_cm_notify);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003498
3499static void cm_recv_handler(struct ib_mad_agent *mad_agent,
3500 struct ib_mad_recv_wc *mad_recv_wc)
3501{
Sean Hefty9af57b72007-07-16 21:49:35 -07003502 struct cm_port *port = mad_agent->context;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003503 struct cm_work *work;
3504 enum ib_cm_event_type event;
Sean Hefty9af57b72007-07-16 21:49:35 -07003505 u16 attr_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003506 int paths = 0;
Erez Shitritbe4b4992015-06-25 17:13:22 +03003507 int going_down = 0;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003508
3509 switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) {
3510 case CM_REQ_ATTR_ID:
3511 paths = 1 + (((struct cm_req_msg *) mad_recv_wc->recv_buf.mad)->
3512 alt_local_lid != 0);
3513 event = IB_CM_REQ_RECEIVED;
3514 break;
3515 case CM_MRA_ATTR_ID:
3516 event = IB_CM_MRA_RECEIVED;
3517 break;
3518 case CM_REJ_ATTR_ID:
3519 event = IB_CM_REJ_RECEIVED;
3520 break;
3521 case CM_REP_ATTR_ID:
3522 event = IB_CM_REP_RECEIVED;
3523 break;
3524 case CM_RTU_ATTR_ID:
3525 event = IB_CM_RTU_RECEIVED;
3526 break;
3527 case CM_DREQ_ATTR_ID:
3528 event = IB_CM_DREQ_RECEIVED;
3529 break;
3530 case CM_DREP_ATTR_ID:
3531 event = IB_CM_DREP_RECEIVED;
3532 break;
3533 case CM_SIDR_REQ_ATTR_ID:
3534 event = IB_CM_SIDR_REQ_RECEIVED;
3535 break;
3536 case CM_SIDR_REP_ATTR_ID:
3537 event = IB_CM_SIDR_REP_RECEIVED;
3538 break;
3539 case CM_LAP_ATTR_ID:
3540 paths = 1;
3541 event = IB_CM_LAP_RECEIVED;
3542 break;
3543 case CM_APR_ATTR_ID:
3544 event = IB_CM_APR_RECEIVED;
3545 break;
3546 default:
3547 ib_free_recv_mad(mad_recv_wc);
3548 return;
3549 }
3550
Sean Hefty9af57b72007-07-16 21:49:35 -07003551 attr_id = be16_to_cpu(mad_recv_wc->recv_buf.mad->mad_hdr.attr_id);
3552 atomic_long_inc(&port->counter_group[CM_RECV].
3553 counter[attr_id - CM_ATTR_ID_OFFSET]);
3554
Hal Rosenstocka9770492005-07-27 11:45:40 -07003555 work = kmalloc(sizeof *work + sizeof(struct ib_sa_path_rec) * paths,
3556 GFP_KERNEL);
3557 if (!work) {
3558 ib_free_recv_mad(mad_recv_wc);
3559 return;
3560 }
3561
David Howellsc4028952006-11-22 14:57:56 +00003562 INIT_DELAYED_WORK(&work->work, cm_work_handler);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003563 work->cm_event.event = event;
3564 work->mad_recv_wc = mad_recv_wc;
Sean Hefty9af57b72007-07-16 21:49:35 -07003565 work->port = port;
Erez Shitritbe4b4992015-06-25 17:13:22 +03003566
3567 /* Check if the device started its remove_one */
3568 spin_lock_irq(&cm.lock);
3569 if (!port->cm_dev->going_down)
3570 queue_delayed_work(cm.wq, &work->work, 0);
3571 else
3572 going_down = 1;
3573 spin_unlock_irq(&cm.lock);
3574
3575 if (going_down) {
3576 kfree(work);
3577 ib_free_recv_mad(mad_recv_wc);
3578 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07003579}
3580
3581static int cm_init_qp_init_attr(struct cm_id_private *cm_id_priv,
3582 struct ib_qp_attr *qp_attr,
3583 int *qp_attr_mask)
3584{
3585 unsigned long flags;
3586 int ret;
3587
3588 spin_lock_irqsave(&cm_id_priv->lock, flags);
3589 switch (cm_id_priv->id.state) {
3590 case IB_CM_REQ_SENT:
3591 case IB_CM_MRA_REQ_RCVD:
3592 case IB_CM_REQ_RCVD:
3593 case IB_CM_MRA_REQ_SENT:
3594 case IB_CM_REP_RCVD:
3595 case IB_CM_MRA_REP_SENT:
3596 case IB_CM_REP_SENT:
3597 case IB_CM_MRA_REP_RCVD:
3598 case IB_CM_ESTABLISHED:
3599 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS |
3600 IB_QP_PKEY_INDEX | IB_QP_PORT;
Dotan Barake31353ea2006-10-24 13:35:27 -07003601 qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003602 if (cm_id_priv->responder_resources)
Sean Heftyc1f250c2006-08-28 11:55:52 -07003603 qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ |
3604 IB_ACCESS_REMOTE_ATOMIC;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003605 qp_attr->pkey_index = cm_id_priv->av.pkey_index;
3606 qp_attr->port_num = cm_id_priv->av.port->port_num;
3607 ret = 0;
3608 break;
3609 default:
3610 ret = -EINVAL;
3611 break;
3612 }
3613 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3614 return ret;
3615}
3616
3617static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv,
3618 struct ib_qp_attr *qp_attr,
3619 int *qp_attr_mask)
3620{
3621 unsigned long flags;
3622 int ret;
3623
3624 spin_lock_irqsave(&cm_id_priv->lock, flags);
3625 switch (cm_id_priv->id.state) {
3626 case IB_CM_REQ_RCVD:
3627 case IB_CM_MRA_REQ_SENT:
3628 case IB_CM_REP_RCVD:
3629 case IB_CM_MRA_REP_SENT:
3630 case IB_CM_REP_SENT:
3631 case IB_CM_MRA_REP_RCVD:
3632 case IB_CM_ESTABLISHED:
3633 *qp_attr_mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU |
Sean Heftyae7971a2005-10-24 12:33:56 -07003634 IB_QP_DEST_QPN | IB_QP_RQ_PSN;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003635 qp_attr->ah_attr = cm_id_priv->av.ah_attr;
3636 qp_attr->path_mtu = cm_id_priv->path_mtu;
3637 qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn);
3638 qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn);
Sean Heftyd26a3602011-05-13 10:46:20 -07003639 if (cm_id_priv->qp_type == IB_QPT_RC ||
3640 cm_id_priv->qp_type == IB_QPT_XRC_TGT) {
Sean Heftyae7971a2005-10-24 12:33:56 -07003641 *qp_attr_mask |= IB_QP_MAX_DEST_RD_ATOMIC |
3642 IB_QP_MIN_RNR_TIMER;
3643 qp_attr->max_dest_rd_atomic =
3644 cm_id_priv->responder_resources;
3645 qp_attr->min_rnr_timer = 0;
3646 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07003647 if (cm_id_priv->alt_av.ah_attr.dlid) {
3648 *qp_attr_mask |= IB_QP_ALT_PATH;
Sean Hefty0d8fdfd2006-06-30 14:10:14 -07003649 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
Sean Heftye1444b52006-11-28 14:57:13 -08003650 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
Sean Hefty1d846122007-06-18 11:09:37 -07003651 qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003652 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3653 }
3654 ret = 0;
3655 break;
3656 default:
3657 ret = -EINVAL;
3658 break;
3659 }
3660 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3661 return ret;
3662}
3663
3664static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv,
3665 struct ib_qp_attr *qp_attr,
3666 int *qp_attr_mask)
3667{
3668 unsigned long flags;
3669 int ret;
3670
3671 spin_lock_irqsave(&cm_id_priv->lock, flags);
3672 switch (cm_id_priv->id.state) {
Sean Hefty0fe313b2006-11-30 16:37:15 -08003673 /* Allow transition to RTS before sending REP */
3674 case IB_CM_REQ_RCVD:
3675 case IB_CM_MRA_REQ_SENT:
3676
Hal Rosenstocka9770492005-07-27 11:45:40 -07003677 case IB_CM_REP_RCVD:
3678 case IB_CM_MRA_REP_SENT:
3679 case IB_CM_REP_SENT:
3680 case IB_CM_MRA_REP_RCVD:
3681 case IB_CM_ESTABLISHED:
Sean Heftye1444b52006-11-28 14:57:13 -08003682 if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT) {
3683 *qp_attr_mask = IB_QP_STATE | IB_QP_SQ_PSN;
3684 qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn);
Sean Heftyd26a3602011-05-13 10:46:20 -07003685 switch (cm_id_priv->qp_type) {
3686 case IB_QPT_RC:
3687 case IB_QPT_XRC_INI:
3688 *qp_attr_mask |= IB_QP_RETRY_CNT | IB_QP_RNR_RETRY |
Sean Heftye1444b52006-11-28 14:57:13 -08003689 IB_QP_MAX_QP_RD_ATOMIC;
Sean Heftye1444b52006-11-28 14:57:13 -08003690 qp_attr->retry_cnt = cm_id_priv->retry_count;
3691 qp_attr->rnr_retry = cm_id_priv->rnr_retry_count;
Sean Heftyd26a3602011-05-13 10:46:20 -07003692 qp_attr->max_rd_atomic = cm_id_priv->initiator_depth;
3693 /* fall through */
3694 case IB_QPT_XRC_TGT:
3695 *qp_attr_mask |= IB_QP_TIMEOUT;
3696 qp_attr->timeout = cm_id_priv->av.timeout;
3697 break;
3698 default:
3699 break;
Sean Heftye1444b52006-11-28 14:57:13 -08003700 }
3701 if (cm_id_priv->alt_av.ah_attr.dlid) {
3702 *qp_attr_mask |= IB_QP_PATH_MIG_STATE;
3703 qp_attr->path_mig_state = IB_MIG_REARM;
3704 }
3705 } else {
3706 *qp_attr_mask = IB_QP_ALT_PATH | IB_QP_PATH_MIG_STATE;
3707 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
3708 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
Sean Hefty1d846122007-06-18 11:09:37 -07003709 qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
Sean Heftye1444b52006-11-28 14:57:13 -08003710 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003711 qp_attr->path_mig_state = IB_MIG_REARM;
3712 }
3713 ret = 0;
3714 break;
3715 default:
3716 ret = -EINVAL;
3717 break;
3718 }
3719 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3720 return ret;
3721}
3722
3723int ib_cm_init_qp_attr(struct ib_cm_id *cm_id,
3724 struct ib_qp_attr *qp_attr,
3725 int *qp_attr_mask)
3726{
3727 struct cm_id_private *cm_id_priv;
3728 int ret;
3729
3730 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3731 switch (qp_attr->qp_state) {
3732 case IB_QPS_INIT:
3733 ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask);
3734 break;
3735 case IB_QPS_RTR:
3736 ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask);
3737 break;
3738 case IB_QPS_RTS:
3739 ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask);
3740 break;
3741 default:
3742 ret = -EINVAL;
3743 break;
3744 }
3745 return ret;
3746}
3747EXPORT_SYMBOL(ib_cm_init_qp_attr);
3748
Sean Hefty9af57b72007-07-16 21:49:35 -07003749static ssize_t cm_show_counter(struct kobject *obj, struct attribute *attr,
3750 char *buf)
3751{
3752 struct cm_counter_group *group;
3753 struct cm_counter_attribute *cm_attr;
3754
3755 group = container_of(obj, struct cm_counter_group, obj);
3756 cm_attr = container_of(attr, struct cm_counter_attribute, attr);
3757
3758 return sprintf(buf, "%ld\n",
3759 atomic_long_read(&group->counter[cm_attr->index]));
3760}
3761
Emese Revfy52cf25d2010-01-19 02:58:23 +01003762static const struct sysfs_ops cm_counter_ops = {
Sean Hefty9af57b72007-07-16 21:49:35 -07003763 .show = cm_show_counter
3764};
3765
3766static struct kobj_type cm_counter_obj_type = {
3767 .sysfs_ops = &cm_counter_ops,
3768 .default_attrs = cm_counter_default_attrs
3769};
3770
3771static void cm_release_port_obj(struct kobject *obj)
3772{
3773 struct cm_port *cm_port;
3774
Sean Hefty9af57b72007-07-16 21:49:35 -07003775 cm_port = container_of(obj, struct cm_port, port_obj);
3776 kfree(cm_port);
3777}
3778
3779static struct kobj_type cm_port_obj_type = {
3780 .release = cm_release_port_obj
3781};
3782
Al Viro2c9ede52011-07-23 20:24:48 -04003783static char *cm_devnode(struct device *dev, umode_t *mode)
Roland Dreierc3af0982011-05-23 11:21:47 -07003784{
Goldwyn Rodriguesb2bc4782011-07-04 09:26:57 -07003785 if (mode)
3786 *mode = 0666;
Roland Dreierc3af0982011-05-23 11:21:47 -07003787 return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
3788}
3789
Sean Hefty9af57b72007-07-16 21:49:35 -07003790struct class cm_class = {
Roland Dreierc3af0982011-05-23 11:21:47 -07003791 .owner = THIS_MODULE,
Sean Hefty9af57b72007-07-16 21:49:35 -07003792 .name = "infiniband_cm",
Roland Dreierc3af0982011-05-23 11:21:47 -07003793 .devnode = cm_devnode,
Sean Hefty9af57b72007-07-16 21:49:35 -07003794};
3795EXPORT_SYMBOL(cm_class);
3796
Sean Hefty9af57b72007-07-16 21:49:35 -07003797static int cm_create_port_fs(struct cm_port *port)
3798{
3799 int i, ret;
3800
3801 ret = kobject_init_and_add(&port->port_obj, &cm_port_obj_type,
Greg Kroah-Hartman110cf372008-05-27 10:17:53 +03003802 &port->cm_dev->device->kobj,
Sean Hefty9af57b72007-07-16 21:49:35 -07003803 "%d", port->port_num);
3804 if (ret) {
3805 kfree(port);
3806 return ret;
3807 }
3808
3809 for (i = 0; i < CM_COUNTER_GROUPS; i++) {
3810 ret = kobject_init_and_add(&port->counter_group[i].obj,
3811 &cm_counter_obj_type,
Roland Dreier7c7a9bc2008-02-12 14:38:27 -08003812 &port->port_obj,
Sean Hefty9af57b72007-07-16 21:49:35 -07003813 "%s", counter_group_names[i]);
3814 if (ret)
3815 goto error;
3816 }
3817
3818 return 0;
3819
3820error:
3821 while (i--)
Roland Dreier7c7a9bc2008-02-12 14:38:27 -08003822 kobject_put(&port->counter_group[i].obj);
3823 kobject_put(&port->port_obj);
Sean Hefty9af57b72007-07-16 21:49:35 -07003824 return ret;
3825
3826}
3827
3828static void cm_remove_port_fs(struct cm_port *port)
3829{
3830 int i;
3831
3832 for (i = 0; i < CM_COUNTER_GROUPS; i++)
Roland Dreier7c7a9bc2008-02-12 14:38:27 -08003833 kobject_put(&port->counter_group[i].obj);
Sean Hefty9af57b72007-07-16 21:49:35 -07003834
Roland Dreier7c7a9bc2008-02-12 14:38:27 -08003835 kobject_put(&port->port_obj);
Sean Hefty9af57b72007-07-16 21:49:35 -07003836}
3837
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003838static void cm_add_one(struct ib_device *ib_device)
Hal Rosenstocka9770492005-07-27 11:45:40 -07003839{
3840 struct cm_device *cm_dev;
3841 struct cm_port *port;
3842 struct ib_mad_reg_req reg_req = {
3843 .mgmt_class = IB_MGMT_CLASS_CM,
Ira Weiny0f29b462014-08-08 19:00:55 -04003844 .mgmt_class_version = IB_CM_CLASS_VERSION,
Hal Rosenstocka9770492005-07-27 11:45:40 -07003845 };
3846 struct ib_port_modify port_modify = {
3847 .set_port_cap_mask = IB_PORT_CM_SUP
3848 };
3849 unsigned long flags;
3850 int ret;
Michael Wang091e6a42015-05-05 14:50:21 +02003851 int count = 0;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003852 u8 i;
3853
Sean Hefty9af57b72007-07-16 21:49:35 -07003854 cm_dev = kzalloc(sizeof(*cm_dev) + sizeof(*port) *
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003855 ib_device->phys_port_cnt, GFP_KERNEL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003856 if (!cm_dev)
3857 return;
3858
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003859 cm_dev->ib_device = ib_device;
Or Gerlitz86bee4c2015-12-18 10:59:45 +02003860 cm_dev->ack_delay = ib_device->attrs.local_ca_ack_delay;
Erez Shitritbe4b4992015-06-25 17:13:22 +03003861 cm_dev->going_down = 0;
Greg Kroah-Hartman91bd4182008-07-21 20:03:34 -07003862 cm_dev->device = device_create(&cm_class, &ib_device->dev,
3863 MKDEV(0, 0), NULL,
3864 "%s", ib_device->name);
Jani Nikula3e340c02010-03-11 16:17:28 +00003865 if (IS_ERR(cm_dev->device)) {
Sean Hefty9af57b72007-07-16 21:49:35 -07003866 kfree(cm_dev);
3867 return;
3868 }
3869
Hal Rosenstocka9770492005-07-27 11:45:40 -07003870 set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask);
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003871 for (i = 1; i <= ib_device->phys_port_cnt; i++) {
Michael Wang72219cea2015-05-05 14:50:34 +02003872 if (!rdma_cap_ib_cm(ib_device, i))
Michael Wang091e6a42015-05-05 14:50:21 +02003873 continue;
3874
Sean Hefty9af57b72007-07-16 21:49:35 -07003875 port = kzalloc(sizeof *port, GFP_KERNEL);
3876 if (!port)
3877 goto error1;
3878
3879 cm_dev->port[i-1] = port;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003880 port->cm_dev = cm_dev;
3881 port->port_num = i;
Sean Hefty9af57b72007-07-16 21:49:35 -07003882
3883 ret = cm_create_port_fs(port);
3884 if (ret)
3885 goto error1;
3886
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003887 port->mad_agent = ib_register_mad_agent(ib_device, i,
Hal Rosenstocka9770492005-07-27 11:45:40 -07003888 IB_QPT_GSI,
3889 &reg_req,
3890 0,
3891 cm_send_handler,
3892 cm_recv_handler,
Ira Weiny0f29b462014-08-08 19:00:55 -04003893 port,
3894 0);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003895 if (IS_ERR(port->mad_agent))
Sean Hefty9af57b72007-07-16 21:49:35 -07003896 goto error2;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003897
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003898 ret = ib_modify_port(ib_device, i, 0, &port_modify);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003899 if (ret)
Sean Hefty9af57b72007-07-16 21:49:35 -07003900 goto error3;
Michael Wang091e6a42015-05-05 14:50:21 +02003901
3902 count++;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003903 }
Michael Wang091e6a42015-05-05 14:50:21 +02003904
3905 if (!count)
3906 goto free;
3907
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003908 ib_set_client_data(ib_device, &cm_client, cm_dev);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003909
3910 write_lock_irqsave(&cm.device_lock, flags);
3911 list_add_tail(&cm_dev->list, &cm.device_list);
3912 write_unlock_irqrestore(&cm.device_lock, flags);
3913 return;
3914
Sean Hefty9af57b72007-07-16 21:49:35 -07003915error3:
Sean Heftycf311cd2006-01-10 07:39:34 -08003916 ib_unregister_mad_agent(port->mad_agent);
Sean Hefty9af57b72007-07-16 21:49:35 -07003917error2:
3918 cm_remove_port_fs(port);
Sean Heftycf311cd2006-01-10 07:39:34 -08003919error1:
Hal Rosenstocka9770492005-07-27 11:45:40 -07003920 port_modify.set_port_cap_mask = 0;
3921 port_modify.clr_port_cap_mask = IB_PORT_CM_SUP;
3922 while (--i) {
Michael Wang72219cea2015-05-05 14:50:34 +02003923 if (!rdma_cap_ib_cm(ib_device, i))
Michael Wang091e6a42015-05-05 14:50:21 +02003924 continue;
3925
Sean Hefty9af57b72007-07-16 21:49:35 -07003926 port = cm_dev->port[i-1];
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003927 ib_modify_port(ib_device, port->port_num, 0, &port_modify);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003928 ib_unregister_mad_agent(port->mad_agent);
Sean Hefty9af57b72007-07-16 21:49:35 -07003929 cm_remove_port_fs(port);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003930 }
Michael Wang091e6a42015-05-05 14:50:21 +02003931free:
Greg Kroah-Hartman110cf372008-05-27 10:17:53 +03003932 device_unregister(cm_dev->device);
Hefty, Seana7e80ce2008-09-30 10:36:54 -07003933 kfree(cm_dev);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003934}
3935
Haggai Eran7c1eb452015-07-30 17:50:14 +03003936static void cm_remove_one(struct ib_device *ib_device, void *client_data)
Hal Rosenstocka9770492005-07-27 11:45:40 -07003937{
Haggai Eran7c1eb452015-07-30 17:50:14 +03003938 struct cm_device *cm_dev = client_data;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003939 struct cm_port *port;
3940 struct ib_port_modify port_modify = {
3941 .clr_port_cap_mask = IB_PORT_CM_SUP
3942 };
3943 unsigned long flags;
3944 int i;
3945
Hal Rosenstocka9770492005-07-27 11:45:40 -07003946 if (!cm_dev)
3947 return;
3948
3949 write_lock_irqsave(&cm.device_lock, flags);
3950 list_del(&cm_dev->list);
3951 write_unlock_irqrestore(&cm.device_lock, flags);
3952
Erez Shitritbe4b4992015-06-25 17:13:22 +03003953 spin_lock_irq(&cm.lock);
3954 cm_dev->going_down = 1;
3955 spin_unlock_irq(&cm.lock);
3956
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003957 for (i = 1; i <= ib_device->phys_port_cnt; i++) {
Michael Wang72219cea2015-05-05 14:50:34 +02003958 if (!rdma_cap_ib_cm(ib_device, i))
Michael Wang091e6a42015-05-05 14:50:21 +02003959 continue;
3960
Sean Hefty9af57b72007-07-16 21:49:35 -07003961 port = cm_dev->port[i-1];
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003962 ib_modify_port(ib_device, port->port_num, 0, &port_modify);
Erez Shitritbe4b4992015-06-25 17:13:22 +03003963 /*
3964 * We flush the queue here after the going_down set, this
3965 * verify that no new works will be queued in the recv handler,
3966 * after that we can call the unregister_mad_agent
3967 */
Sean Hefty84ba2842008-02-22 10:40:45 -08003968 flush_workqueue(cm.wq);
Erez Shitritbe4b4992015-06-25 17:13:22 +03003969 ib_unregister_mad_agent(port->mad_agent);
Sean Hefty9af57b72007-07-16 21:49:35 -07003970 cm_remove_port_fs(port);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003971 }
Greg Kroah-Hartman110cf372008-05-27 10:17:53 +03003972 device_unregister(cm_dev->device);
Hefty, Seana7e80ce2008-09-30 10:36:54 -07003973 kfree(cm_dev);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003974}
3975
3976static int __init ib_cm_init(void)
3977{
3978 int ret;
3979
3980 memset(&cm, 0, sizeof cm);
3981 INIT_LIST_HEAD(&cm.device_list);
3982 rwlock_init(&cm.device_lock);
3983 spin_lock_init(&cm.lock);
3984 cm.listen_service_table = RB_ROOT;
Harvey Harrison9c3da092009-01-17 17:11:57 -08003985 cm.listen_service_id = be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003986 cm.remote_id_table = RB_ROOT;
3987 cm.remote_qp_table = RB_ROOT;
3988 cm.remote_sidr_table = RB_ROOT;
3989 idr_init(&cm.local_id_table);
Sean Heftyf06d2652006-08-28 15:15:18 -07003990 get_random_bytes(&cm.random_id_operand, sizeof cm.random_id_operand);
Sean Hefty85753292006-10-04 11:29:59 -07003991 INIT_LIST_HEAD(&cm.timewait_list);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003992
Sean Hefty9af57b72007-07-16 21:49:35 -07003993 ret = class_register(&cm_class);
Dotan Barak87d4abd2012-07-11 15:39:31 +00003994 if (ret) {
Sean Hefty9af57b72007-07-16 21:49:35 -07003995 ret = -ENOMEM;
3996 goto error1;
3997 }
3998
Dotan Barak87d4abd2012-07-11 15:39:31 +00003999 cm.wq = create_workqueue("ib_cm");
4000 if (!cm.wq) {
4001 ret = -ENOMEM;
4002 goto error2;
4003 }
4004
Hal Rosenstocka9770492005-07-27 11:45:40 -07004005 ret = ib_register_client(&cm_client);
4006 if (ret)
Dotan Barak87d4abd2012-07-11 15:39:31 +00004007 goto error3;
Hal Rosenstocka9770492005-07-27 11:45:40 -07004008
4009 return 0;
Dotan Barak87d4abd2012-07-11 15:39:31 +00004010error3:
Hal Rosenstocka9770492005-07-27 11:45:40 -07004011 destroy_workqueue(cm.wq);
Dotan Barak87d4abd2012-07-11 15:39:31 +00004012error2:
Sean Hefty9af57b72007-07-16 21:49:35 -07004013 class_unregister(&cm_class);
Dotan Barak87d4abd2012-07-11 15:39:31 +00004014error1:
4015 idr_destroy(&cm.local_id_table);
Hal Rosenstocka9770492005-07-27 11:45:40 -07004016 return ret;
4017}
4018
4019static void __exit ib_cm_cleanup(void)
4020{
Sean Hefty85753292006-10-04 11:29:59 -07004021 struct cm_timewait_info *timewait_info, *tmp;
4022
4023 spin_lock_irq(&cm.lock);
4024 list_for_each_entry(timewait_info, &cm.timewait_list, list)
4025 cancel_delayed_work(&timewait_info->work.work);
4026 spin_unlock_irq(&cm.lock);
4027
Sean Hefty84ba2842008-02-22 10:40:45 -08004028 ib_unregister_client(&cm_client);
Hal Rosenstocka9770492005-07-27 11:45:40 -07004029 destroy_workqueue(cm.wq);
Sean Hefty85753292006-10-04 11:29:59 -07004030
4031 list_for_each_entry_safe(timewait_info, tmp, &cm.timewait_list, list) {
4032 list_del(&timewait_info->list);
4033 kfree(timewait_info);
4034 }
4035
Sean Hefty9af57b72007-07-16 21:49:35 -07004036 class_unregister(&cm_class);
Roland Dreier5d7edb32005-10-24 10:53:25 -07004037 idr_destroy(&cm.local_id_table);
Hal Rosenstocka9770492005-07-27 11:45:40 -07004038}
4039
4040module_init(ib_cm_init);
4041module_exit(ib_cm_cleanup);
4042