blob: 2b4d613a347491295a51932699264c6edcd4bb51 [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
Steve Wise77a5db12016-10-26 12:36:40 -070060static const char * const ibcm_rej_reason_strs[] = {
61 [IB_CM_REJ_NO_QP] = "no QP",
62 [IB_CM_REJ_NO_EEC] = "no EEC",
63 [IB_CM_REJ_NO_RESOURCES] = "no resources",
64 [IB_CM_REJ_TIMEOUT] = "timeout",
65 [IB_CM_REJ_UNSUPPORTED] = "unsupported",
66 [IB_CM_REJ_INVALID_COMM_ID] = "invalid comm ID",
67 [IB_CM_REJ_INVALID_COMM_INSTANCE] = "invalid comm instance",
68 [IB_CM_REJ_INVALID_SERVICE_ID] = "invalid service ID",
69 [IB_CM_REJ_INVALID_TRANSPORT_TYPE] = "invalid transport type",
70 [IB_CM_REJ_STALE_CONN] = "stale conn",
71 [IB_CM_REJ_RDC_NOT_EXIST] = "RDC not exist",
72 [IB_CM_REJ_INVALID_GID] = "invalid GID",
73 [IB_CM_REJ_INVALID_LID] = "invalid LID",
74 [IB_CM_REJ_INVALID_SL] = "invalid SL",
75 [IB_CM_REJ_INVALID_TRAFFIC_CLASS] = "invalid traffic class",
76 [IB_CM_REJ_INVALID_HOP_LIMIT] = "invalid hop limit",
77 [IB_CM_REJ_INVALID_PACKET_RATE] = "invalid packet rate",
78 [IB_CM_REJ_INVALID_ALT_GID] = "invalid alt GID",
79 [IB_CM_REJ_INVALID_ALT_LID] = "invalid alt LID",
80 [IB_CM_REJ_INVALID_ALT_SL] = "invalid alt SL",
81 [IB_CM_REJ_INVALID_ALT_TRAFFIC_CLASS] = "invalid alt traffic class",
82 [IB_CM_REJ_INVALID_ALT_HOP_LIMIT] = "invalid alt hop limit",
83 [IB_CM_REJ_INVALID_ALT_PACKET_RATE] = "invalid alt packet rate",
84 [IB_CM_REJ_PORT_CM_REDIRECT] = "port CM redirect",
85 [IB_CM_REJ_PORT_REDIRECT] = "port redirect",
86 [IB_CM_REJ_INVALID_MTU] = "invalid MTU",
87 [IB_CM_REJ_INSUFFICIENT_RESP_RESOURCES] = "insufficient resp resources",
88 [IB_CM_REJ_CONSUMER_DEFINED] = "consumer defined",
89 [IB_CM_REJ_INVALID_RNR_RETRY] = "invalid RNR retry",
90 [IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID] = "duplicate local comm ID",
91 [IB_CM_REJ_INVALID_CLASS_VERSION] = "invalid class version",
92 [IB_CM_REJ_INVALID_FLOW_LABEL] = "invalid flow label",
93 [IB_CM_REJ_INVALID_ALT_FLOW_LABEL] = "invalid alt flow label",
94};
95
96const char *__attribute_const__ ibcm_reject_msg(int reason)
97{
98 size_t index = reason;
99
100 if (index < ARRAY_SIZE(ibcm_rej_reason_strs) &&
101 ibcm_rej_reason_strs[index])
102 return ibcm_rej_reason_strs[index];
103 else
104 return "unrecognized reason";
105}
106EXPORT_SYMBOL(ibcm_reject_msg);
107
Hal Rosenstocka9770492005-07-27 11:45:40 -0700108static void cm_add_one(struct ib_device *device);
Haggai Eran7c1eb452015-07-30 17:50:14 +0300109static void cm_remove_one(struct ib_device *device, void *client_data);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700110
111static struct ib_client cm_client = {
112 .name = "cm",
113 .add = cm_add_one,
114 .remove = cm_remove_one
115};
116
117static struct ib_cm {
118 spinlock_t lock;
119 struct list_head device_list;
120 rwlock_t device_lock;
121 struct rb_root listen_service_table;
122 u64 listen_service_id;
123 /* struct rb_root peer_service_table; todo: fix peer to peer */
124 struct rb_root remote_qp_table;
125 struct rb_root remote_id_table;
126 struct rb_root remote_sidr_table;
127 struct idr local_id_table;
Sean Heftyf06d2652006-08-28 15:15:18 -0700128 __be32 random_id_operand;
Sean Hefty85753292006-10-04 11:29:59 -0700129 struct list_head timewait_list;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700130 struct workqueue_struct *wq;
Mark Bloch9db0ff52016-10-27 16:36:27 +0300131 /* Sync on cm change port state */
132 spinlock_t state_lock;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700133} cm;
134
Sean Hefty9af57b72007-07-16 21:49:35 -0700135/* Counter indexes ordered by attribute ID */
136enum {
137 CM_REQ_COUNTER,
138 CM_MRA_COUNTER,
139 CM_REJ_COUNTER,
140 CM_REP_COUNTER,
141 CM_RTU_COUNTER,
142 CM_DREQ_COUNTER,
143 CM_DREP_COUNTER,
144 CM_SIDR_REQ_COUNTER,
145 CM_SIDR_REP_COUNTER,
146 CM_LAP_COUNTER,
147 CM_APR_COUNTER,
148 CM_ATTR_COUNT,
149 CM_ATTR_ID_OFFSET = 0x0010,
150};
151
152enum {
153 CM_XMIT,
154 CM_XMIT_RETRIES,
155 CM_RECV,
156 CM_RECV_DUPLICATES,
157 CM_COUNTER_GROUPS
158};
159
160static char const counter_group_names[CM_COUNTER_GROUPS]
161 [sizeof("cm_rx_duplicates")] = {
162 "cm_tx_msgs", "cm_tx_retries",
163 "cm_rx_msgs", "cm_rx_duplicates"
164};
165
166struct cm_counter_group {
167 struct kobject obj;
168 atomic_long_t counter[CM_ATTR_COUNT];
169};
170
171struct cm_counter_attribute {
172 struct attribute attr;
173 int index;
174};
175
176#define CM_COUNTER_ATTR(_name, _index) \
177struct cm_counter_attribute cm_##_name##_counter_attr = { \
Parag Warudkar01e8ef12008-10-18 20:28:50 -0700178 .attr = { .name = __stringify(_name), .mode = 0444 }, \
Sean Hefty9af57b72007-07-16 21:49:35 -0700179 .index = _index \
180}
181
182static CM_COUNTER_ATTR(req, CM_REQ_COUNTER);
183static CM_COUNTER_ATTR(mra, CM_MRA_COUNTER);
184static CM_COUNTER_ATTR(rej, CM_REJ_COUNTER);
185static CM_COUNTER_ATTR(rep, CM_REP_COUNTER);
186static CM_COUNTER_ATTR(rtu, CM_RTU_COUNTER);
187static CM_COUNTER_ATTR(dreq, CM_DREQ_COUNTER);
188static CM_COUNTER_ATTR(drep, CM_DREP_COUNTER);
189static CM_COUNTER_ATTR(sidr_req, CM_SIDR_REQ_COUNTER);
190static CM_COUNTER_ATTR(sidr_rep, CM_SIDR_REP_COUNTER);
191static CM_COUNTER_ATTR(lap, CM_LAP_COUNTER);
192static CM_COUNTER_ATTR(apr, CM_APR_COUNTER);
193
194static struct attribute *cm_counter_default_attrs[] = {
195 &cm_req_counter_attr.attr,
196 &cm_mra_counter_attr.attr,
197 &cm_rej_counter_attr.attr,
198 &cm_rep_counter_attr.attr,
199 &cm_rtu_counter_attr.attr,
200 &cm_dreq_counter_attr.attr,
201 &cm_drep_counter_attr.attr,
202 &cm_sidr_req_counter_attr.attr,
203 &cm_sidr_rep_counter_attr.attr,
204 &cm_lap_counter_attr.attr,
205 &cm_apr_counter_attr.attr,
206 NULL
207};
208
Hal Rosenstocka9770492005-07-27 11:45:40 -0700209struct cm_port {
210 struct cm_device *cm_dev;
211 struct ib_mad_agent *mad_agent;
Sean Hefty9af57b72007-07-16 21:49:35 -0700212 struct kobject port_obj;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700213 u8 port_num;
Mark Bloch9db0ff52016-10-27 16:36:27 +0300214 struct list_head cm_priv_prim_list;
215 struct list_head cm_priv_altr_list;
Sean Hefty9af57b72007-07-16 21:49:35 -0700216 struct cm_counter_group counter_group[CM_COUNTER_GROUPS];
Hal Rosenstocka9770492005-07-27 11:45:40 -0700217};
218
219struct cm_device {
220 struct list_head list;
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +0300221 struct ib_device *ib_device;
Greg Kroah-Hartman110cf372008-05-27 10:17:53 +0300222 struct device *device;
Sean Hefty1d846122007-06-18 11:09:37 -0700223 u8 ack_delay;
Erez Shitritbe4b4992015-06-25 17:13:22 +0300224 int going_down;
Sean Hefty9af57b72007-07-16 21:49:35 -0700225 struct cm_port *port[0];
Hal Rosenstocka9770492005-07-27 11:45:40 -0700226};
227
228struct cm_av {
229 struct cm_port *port;
230 union ib_gid dgid;
Dasaratharaman Chandramouli90898852017-04-29 14:41:18 -0400231 struct rdma_ah_attr ah_attr;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700232 u16 pkey_index;
Sean Hefty1d846122007-06-18 11:09:37 -0700233 u8 timeout;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700234};
235
236struct cm_work {
David Howellsc4028952006-11-22 14:57:56 +0000237 struct delayed_work work;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700238 struct list_head list;
239 struct cm_port *port;
240 struct ib_mad_recv_wc *mad_recv_wc; /* Received MADs */
Sean Hefty97f52eb2005-08-13 21:05:57 -0700241 __be32 local_id; /* Established / timewait */
242 __be32 remote_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700243 struct ib_cm_event cm_event;
Dasaratharaman Chandramoulic2f8fc42017-04-27 19:05:58 -0400244 struct sa_path_rec path[0];
Hal Rosenstocka9770492005-07-27 11:45:40 -0700245};
246
247struct cm_timewait_info {
248 struct cm_work work; /* Must be first. */
Sean Hefty85753292006-10-04 11:29:59 -0700249 struct list_head list;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700250 struct rb_node remote_qp_node;
251 struct rb_node remote_id_node;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700252 __be64 remote_ca_guid;
253 __be32 remote_qpn;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700254 u8 inserted_remote_qp;
255 u8 inserted_remote_id;
256};
257
258struct cm_id_private {
259 struct ib_cm_id id;
260
261 struct rb_node service_node;
262 struct rb_node sidr_id_node;
Sean Hefty87fd1a12006-03-02 16:50:37 -0800263 spinlock_t lock; /* Do not acquire inside cm.lock */
Sean Hefty1b52fa982006-05-12 14:57:52 -0700264 struct completion comp;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700265 atomic_t refcount;
Haggai Eran067b1712015-07-30 17:50:18 +0300266 /* Number of clients sharing this ib_cm_id. Only valid for listeners.
267 * Protected by the cm.lock spinlock. */
268 int listen_sharecount;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700269
270 struct ib_mad_send_buf *msg;
271 struct cm_timewait_info *timewait_info;
272 /* todo: use alternate port on send failure */
273 struct cm_av av;
274 struct cm_av alt_av;
275
276 void *private_data;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700277 __be64 tid;
278 __be32 local_qpn;
279 __be32 remote_qpn;
Sean Heftyae7971a2005-10-24 12:33:56 -0700280 enum ib_qp_type qp_type;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700281 __be32 sq_psn;
282 __be32 rq_psn;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700283 int timeout_ms;
284 enum ib_mtu path_mtu;
Sean Heftye1444b52006-11-28 14:57:13 -0800285 __be16 pkey;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700286 u8 private_data_len;
287 u8 max_cm_retries;
288 u8 peer_to_peer;
289 u8 responder_resources;
290 u8 initiator_depth;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700291 u8 retry_count;
292 u8 rnr_retry_count;
293 u8 service_timeout;
Sean Hefty1d846122007-06-18 11:09:37 -0700294 u8 target_ack_delay;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700295
Mark Bloch9db0ff52016-10-27 16:36:27 +0300296 struct list_head prim_list;
297 struct list_head altr_list;
298 /* Indicates that the send port mad is registered and av is set */
299 int prim_send_port_not_ready;
300 int altr_send_port_not_ready;
301
Hal Rosenstocka9770492005-07-27 11:45:40 -0700302 struct list_head work_list;
303 atomic_t work_count;
304};
305
David Howellsc4028952006-11-22 14:57:56 +0000306static void cm_work_handler(struct work_struct *work);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700307
308static inline void cm_deref_id(struct cm_id_private *cm_id_priv)
309{
310 if (atomic_dec_and_test(&cm_id_priv->refcount))
Sean Hefty1b52fa982006-05-12 14:57:52 -0700311 complete(&cm_id_priv->comp);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700312}
313
314static int cm_alloc_msg(struct cm_id_private *cm_id_priv,
315 struct ib_mad_send_buf **msg)
316{
317 struct ib_mad_agent *mad_agent;
318 struct ib_mad_send_buf *m;
319 struct ib_ah *ah;
Mark Bloch9db0ff52016-10-27 16:36:27 +0300320 struct cm_av *av;
321 unsigned long flags, flags2;
322 int ret = 0;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700323
Mark Bloch9db0ff52016-10-27 16:36:27 +0300324 /* don't let the port to be released till the agent is down */
325 spin_lock_irqsave(&cm.state_lock, flags2);
326 spin_lock_irqsave(&cm.lock, flags);
327 if (!cm_id_priv->prim_send_port_not_ready)
328 av = &cm_id_priv->av;
329 else if (!cm_id_priv->altr_send_port_not_ready &&
330 (cm_id_priv->alt_av.port))
331 av = &cm_id_priv->alt_av;
332 else {
333 pr_info("%s: not valid CM id\n", __func__);
334 ret = -ENODEV;
335 spin_unlock_irqrestore(&cm.lock, flags);
336 goto out;
337 }
338 spin_unlock_irqrestore(&cm.lock, flags);
339 /* Make sure the port haven't released the mad yet */
Hal Rosenstocka9770492005-07-27 11:45:40 -0700340 mad_agent = cm_id_priv->av.port->mad_agent;
Mark Bloch9db0ff52016-10-27 16:36:27 +0300341 if (!mad_agent) {
342 pr_info("%s: not a valid MAD agent\n", __func__);
343 ret = -ENODEV;
344 goto out;
345 }
Dasaratharaman Chandramouli0a18cfe2017-04-29 14:41:19 -0400346 ah = rdma_create_ah(mad_agent->qp->pd, &av->ah_attr);
Mark Bloch9db0ff52016-10-27 16:36:27 +0300347 if (IS_ERR(ah)) {
348 ret = PTR_ERR(ah);
349 goto out;
350 }
Hal Rosenstocka9770492005-07-27 11:45:40 -0700351
Roland Dreier3cd96562006-09-22 15:22:46 -0700352 m = ib_create_send_mad(mad_agent, cm_id_priv->id.remote_cm_qpn,
Mark Bloch9db0ff52016-10-27 16:36:27 +0300353 av->pkey_index,
Sean Hefty34816ad2005-10-25 10:51:39 -0700354 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
Ira Weinyda2dfaa2015-06-06 14:38:28 -0400355 GFP_ATOMIC,
356 IB_MGMT_BASE_VERSION);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700357 if (IS_ERR(m)) {
Dasaratharaman Chandramouli36523152017-04-29 14:41:22 -0400358 rdma_destroy_ah(ah);
Mark Bloch9db0ff52016-10-27 16:36:27 +0300359 ret = PTR_ERR(m);
360 goto out;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700361 }
362
363 /* Timeout set by caller if response is expected. */
Sean Hefty34816ad2005-10-25 10:51:39 -0700364 m->ah = ah;
365 m->retries = cm_id_priv->max_cm_retries;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700366
367 atomic_inc(&cm_id_priv->refcount);
368 m->context[0] = cm_id_priv;
369 *msg = m;
Mark Bloch9db0ff52016-10-27 16:36:27 +0300370
371out:
372 spin_unlock_irqrestore(&cm.state_lock, flags2);
373 return ret;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700374}
375
376static int cm_alloc_response_msg(struct cm_port *port,
377 struct ib_mad_recv_wc *mad_recv_wc,
378 struct ib_mad_send_buf **msg)
379{
380 struct ib_mad_send_buf *m;
381 struct ib_ah *ah;
382
383 ah = ib_create_ah_from_wc(port->mad_agent->qp->pd, mad_recv_wc->wc,
384 mad_recv_wc->recv_buf.grh, port->port_num);
385 if (IS_ERR(ah))
386 return PTR_ERR(ah);
387
388 m = ib_create_send_mad(port->mad_agent, 1, mad_recv_wc->wc->pkey_index,
Sean Hefty34816ad2005-10-25 10:51:39 -0700389 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
Ira Weinyda2dfaa2015-06-06 14:38:28 -0400390 GFP_ATOMIC,
391 IB_MGMT_BASE_VERSION);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700392 if (IS_ERR(m)) {
Dasaratharaman Chandramouli36523152017-04-29 14:41:22 -0400393 rdma_destroy_ah(ah);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700394 return PTR_ERR(m);
395 }
Sean Hefty34816ad2005-10-25 10:51:39 -0700396 m->ah = ah;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700397 *msg = m;
398 return 0;
399}
400
401static void cm_free_msg(struct ib_mad_send_buf *msg)
402{
Dasaratharaman Chandramouli36523152017-04-29 14:41:22 -0400403 rdma_destroy_ah(msg->ah);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700404 if (msg->context[0])
405 cm_deref_id(msg->context[0]);
406 ib_free_send_mad(msg);
407}
408
409static void * cm_copy_private_data(const void *private_data,
410 u8 private_data_len)
411{
412 void *data;
413
414 if (!private_data || !private_data_len)
415 return NULL;
416
Eric Sesterhennbed8bdf2006-10-23 22:17:21 +0200417 data = kmemdup(private_data, private_data_len, GFP_KERNEL);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700418 if (!data)
419 return ERR_PTR(-ENOMEM);
420
Hal Rosenstocka9770492005-07-27 11:45:40 -0700421 return data;
422}
423
424static void cm_set_private_data(struct cm_id_private *cm_id_priv,
425 void *private_data, u8 private_data_len)
426{
427 if (cm_id_priv->private_data && cm_id_priv->private_data_len)
428 kfree(cm_id_priv->private_data);
429
430 cm_id_priv->private_data = private_data;
431 cm_id_priv->private_data_len = private_data_len;
432}
433
Sean Heftyca222c62006-06-17 20:37:40 -0700434static void cm_init_av_for_response(struct cm_port *port, struct ib_wc *wc,
435 struct ib_grh *grh, struct cm_av *av)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700436{
437 av->port = port;
438 av->pkey_index = wc->pkey_index;
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +0300439 ib_init_ah_from_wc(port->cm_dev->ib_device, port->port_num, wc,
Sean Heftyca222c62006-06-17 20:37:40 -0700440 grh, &av->ah_attr);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700441}
442
Dasaratharaman Chandramoulic2f8fc42017-04-27 19:05:58 -0400443static int cm_init_av_by_path(struct sa_path_rec *path, struct cm_av *av,
Mark Bloch9db0ff52016-10-27 16:36:27 +0300444 struct cm_id_private *cm_id_priv)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700445{
446 struct cm_device *cm_dev;
447 struct cm_port *port = NULL;
448 unsigned long flags;
449 int ret;
450 u8 p;
Matan Barakc2c6ff12015-10-15 18:38:48 +0300451 struct net_device *ndev = ib_get_ndev_from_path(path);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700452
453 read_lock_irqsave(&cm.device_lock, flags);
454 list_for_each_entry(cm_dev, &cm.device_list, list) {
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +0300455 if (!ib_find_cached_gid(cm_dev->ib_device, &path->sgid,
Dasaratharaman Chandramoulidfa834e12017-04-27 19:05:59 -0400456 sa_conv_pathrec_to_gid_type(path),
457 ndev, &p, NULL)) {
Sean Hefty9af57b72007-07-16 21:49:35 -0700458 port = cm_dev->port[p-1];
Hal Rosenstocka9770492005-07-27 11:45:40 -0700459 break;
460 }
461 }
462 read_unlock_irqrestore(&cm.device_lock, flags);
463
Matan Barakc2c6ff12015-10-15 18:38:48 +0300464 if (ndev)
465 dev_put(ndev);
466
Hal Rosenstocka9770492005-07-27 11:45:40 -0700467 if (!port)
468 return -EINVAL;
469
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +0300470 ret = ib_find_cached_pkey(cm_dev->ib_device, port->port_num,
Hal Rosenstocka9770492005-07-27 11:45:40 -0700471 be16_to_cpu(path->pkey), &av->pkey_index);
472 if (ret)
473 return ret;
474
475 av->port = port;
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +0300476 ib_init_ah_from_path(cm_dev->ib_device, port->port_num, path,
Sean Heftyca222c62006-06-17 20:37:40 -0700477 &av->ah_attr);
Sean Hefty1d846122007-06-18 11:09:37 -0700478 av->timeout = path->packet_life_time + 1;
Matan Barakdd5f03b2013-12-12 18:03:11 +0200479
Mark Bloch9db0ff52016-10-27 16:36:27 +0300480 spin_lock_irqsave(&cm.lock, flags);
481 if (&cm_id_priv->av == av)
482 list_add_tail(&cm_id_priv->prim_list, &port->cm_priv_prim_list);
483 else if (&cm_id_priv->alt_av == av)
484 list_add_tail(&cm_id_priv->altr_list, &port->cm_priv_altr_list);
485 else
486 ret = -EINVAL;
487
488 spin_unlock_irqrestore(&cm.lock, flags);
489
490 return ret;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700491}
492
493static int cm_alloc_id(struct cm_id_private *cm_id_priv)
494{
495 unsigned long flags;
Tejun Heo3b069c52013-02-27 17:04:16 -0800496 int id;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700497
Tejun Heo3b069c52013-02-27 17:04:16 -0800498 idr_preload(GFP_KERNEL);
499 spin_lock_irqsave(&cm.lock, flags);
500
Zhao Hongjiangab626d12013-11-15 14:16:37 -0800501 id = idr_alloc_cyclic(&cm.local_id_table, cm_id_priv, 0, 0, GFP_NOWAIT);
Tejun Heo3b069c52013-02-27 17:04:16 -0800502
503 spin_unlock_irqrestore(&cm.lock, flags);
504 idr_preload_end();
Sean Heftyf06d2652006-08-28 15:15:18 -0700505
Al Viro1b90c132008-03-29 03:10:28 +0000506 cm_id_priv->id.local_id = (__force __be32)id ^ cm.random_id_operand;
Tejun Heo3b069c52013-02-27 17:04:16 -0800507 return id < 0 ? id : 0;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700508}
509
Sean Hefty97f52eb2005-08-13 21:05:57 -0700510static void cm_free_id(__be32 local_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700511{
Sean Hefty24be6e82007-06-18 11:09:36 -0700512 spin_lock_irq(&cm.lock);
Sean Heftyf06d2652006-08-28 15:15:18 -0700513 idr_remove(&cm.local_id_table,
514 (__force int) (local_id ^ cm.random_id_operand));
Sean Hefty24be6e82007-06-18 11:09:36 -0700515 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700516}
517
Sean Hefty97f52eb2005-08-13 21:05:57 -0700518static struct cm_id_private * cm_get_id(__be32 local_id, __be32 remote_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700519{
520 struct cm_id_private *cm_id_priv;
521
Sean Heftyf06d2652006-08-28 15:15:18 -0700522 cm_id_priv = idr_find(&cm.local_id_table,
523 (__force int) (local_id ^ cm.random_id_operand));
Hal Rosenstocka9770492005-07-27 11:45:40 -0700524 if (cm_id_priv) {
525 if (cm_id_priv->id.remote_id == remote_id)
526 atomic_inc(&cm_id_priv->refcount);
527 else
528 cm_id_priv = NULL;
529 }
530
531 return cm_id_priv;
532}
533
Sean Hefty97f52eb2005-08-13 21:05:57 -0700534static struct cm_id_private * cm_acquire_id(__be32 local_id, __be32 remote_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700535{
536 struct cm_id_private *cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700537
Sean Hefty24be6e82007-06-18 11:09:36 -0700538 spin_lock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700539 cm_id_priv = cm_get_id(local_id, remote_id);
Sean Hefty24be6e82007-06-18 11:09:36 -0700540 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700541
542 return cm_id_priv;
543}
544
Roland Dreiera88f4882008-04-16 21:01:07 -0700545/*
546 * Trivial helpers to strip endian annotation and compare; the
547 * endianness doesn't actually matter since we just need a stable
548 * order for the RB tree.
549 */
550static int be32_lt(__be32 a, __be32 b)
551{
552 return (__force u32) a < (__force u32) b;
553}
554
555static int be32_gt(__be32 a, __be32 b)
556{
557 return (__force u32) a > (__force u32) b;
558}
559
560static int be64_lt(__be64 a, __be64 b)
561{
562 return (__force u64) a < (__force u64) b;
563}
564
565static int be64_gt(__be64 a, __be64 b)
566{
567 return (__force u64) a > (__force u64) b;
568}
569
Hal Rosenstocka9770492005-07-27 11:45:40 -0700570static struct cm_id_private * cm_insert_listen(struct cm_id_private *cm_id_priv)
571{
572 struct rb_node **link = &cm.listen_service_table.rb_node;
573 struct rb_node *parent = NULL;
574 struct cm_id_private *cur_cm_id_priv;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700575 __be64 service_id = cm_id_priv->id.service_id;
576 __be64 service_mask = cm_id_priv->id.service_mask;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700577
578 while (*link) {
579 parent = *link;
580 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
581 service_node);
582 if ((cur_cm_id_priv->id.service_mask & service_id) ==
Sean Hefty07d357d2005-10-17 15:37:43 -0700583 (service_mask & cur_cm_id_priv->id.service_id) &&
Haggai Eran73fec7f2015-07-30 17:50:26 +0300584 (cm_id_priv->id.device == cur_cm_id_priv->id.device))
Sean Hefty07d357d2005-10-17 15:37:43 -0700585 return cur_cm_id_priv;
586
587 if (cm_id_priv->id.device < cur_cm_id_priv->id.device)
588 link = &(*link)->rb_left;
589 else if (cm_id_priv->id.device > cur_cm_id_priv->id.device)
590 link = &(*link)->rb_right;
Roland Dreiera88f4882008-04-16 21:01:07 -0700591 else if (be64_lt(service_id, cur_cm_id_priv->id.service_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700592 link = &(*link)->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700593 else if (be64_gt(service_id, cur_cm_id_priv->id.service_id))
Sean Hefty6e61d042006-06-17 20:37:28 -0700594 link = &(*link)->rb_right;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700595 else
596 link = &(*link)->rb_right;
597 }
598 rb_link_node(&cm_id_priv->service_node, parent, link);
599 rb_insert_color(&cm_id_priv->service_node, &cm.listen_service_table);
600 return NULL;
601}
602
Sean Hefty07d357d2005-10-17 15:37:43 -0700603static struct cm_id_private * cm_find_listen(struct ib_device *device,
Haggai Eran73fec7f2015-07-30 17:50:26 +0300604 __be64 service_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700605{
606 struct rb_node *node = cm.listen_service_table.rb_node;
607 struct cm_id_private *cm_id_priv;
608
609 while (node) {
610 cm_id_priv = rb_entry(node, struct cm_id_private, service_node);
611 if ((cm_id_priv->id.service_mask & service_id) ==
Sean Hefty07d357d2005-10-17 15:37:43 -0700612 cm_id_priv->id.service_id &&
Haggai Eran73fec7f2015-07-30 17:50:26 +0300613 (cm_id_priv->id.device == device))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700614 return cm_id_priv;
Sean Hefty07d357d2005-10-17 15:37:43 -0700615
616 if (device < cm_id_priv->id.device)
617 node = node->rb_left;
618 else if (device > cm_id_priv->id.device)
619 node = node->rb_right;
Roland Dreiera88f4882008-04-16 21:01:07 -0700620 else if (be64_lt(service_id, cm_id_priv->id.service_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700621 node = node->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700622 else if (be64_gt(service_id, cm_id_priv->id.service_id))
Sean Hefty6e61d042006-06-17 20:37:28 -0700623 node = node->rb_right;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700624 else
625 node = node->rb_right;
626 }
627 return NULL;
628}
629
630static struct cm_timewait_info * cm_insert_remote_id(struct cm_timewait_info
631 *timewait_info)
632{
633 struct rb_node **link = &cm.remote_id_table.rb_node;
634 struct rb_node *parent = NULL;
635 struct cm_timewait_info *cur_timewait_info;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700636 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
637 __be32 remote_id = timewait_info->work.remote_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700638
639 while (*link) {
640 parent = *link;
641 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
642 remote_id_node);
Roland Dreiera88f4882008-04-16 21:01:07 -0700643 if (be32_lt(remote_id, cur_timewait_info->work.remote_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700644 link = &(*link)->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700645 else if (be32_gt(remote_id, cur_timewait_info->work.remote_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700646 link = &(*link)->rb_right;
Roland Dreiera88f4882008-04-16 21:01:07 -0700647 else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700648 link = &(*link)->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700649 else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700650 link = &(*link)->rb_right;
651 else
652 return cur_timewait_info;
653 }
654 timewait_info->inserted_remote_id = 1;
655 rb_link_node(&timewait_info->remote_id_node, parent, link);
656 rb_insert_color(&timewait_info->remote_id_node, &cm.remote_id_table);
657 return NULL;
658}
659
Sean Hefty97f52eb2005-08-13 21:05:57 -0700660static struct cm_timewait_info * cm_find_remote_id(__be64 remote_ca_guid,
661 __be32 remote_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700662{
663 struct rb_node *node = cm.remote_id_table.rb_node;
664 struct cm_timewait_info *timewait_info;
665
666 while (node) {
667 timewait_info = rb_entry(node, struct cm_timewait_info,
668 remote_id_node);
Roland Dreiera88f4882008-04-16 21:01:07 -0700669 if (be32_lt(remote_id, timewait_info->work.remote_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700670 node = node->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700671 else if (be32_gt(remote_id, timewait_info->work.remote_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700672 node = node->rb_right;
Roland Dreiera88f4882008-04-16 21:01:07 -0700673 else if (be64_lt(remote_ca_guid, timewait_info->remote_ca_guid))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700674 node = node->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700675 else if (be64_gt(remote_ca_guid, timewait_info->remote_ca_guid))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700676 node = node->rb_right;
677 else
678 return timewait_info;
679 }
680 return NULL;
681}
682
683static struct cm_timewait_info * cm_insert_remote_qpn(struct cm_timewait_info
684 *timewait_info)
685{
686 struct rb_node **link = &cm.remote_qp_table.rb_node;
687 struct rb_node *parent = NULL;
688 struct cm_timewait_info *cur_timewait_info;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700689 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
690 __be32 remote_qpn = timewait_info->remote_qpn;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700691
692 while (*link) {
693 parent = *link;
694 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
695 remote_qp_node);
Roland Dreiera88f4882008-04-16 21:01:07 -0700696 if (be32_lt(remote_qpn, cur_timewait_info->remote_qpn))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700697 link = &(*link)->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700698 else if (be32_gt(remote_qpn, cur_timewait_info->remote_qpn))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700699 link = &(*link)->rb_right;
Roland Dreiera88f4882008-04-16 21:01:07 -0700700 else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700701 link = &(*link)->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700702 else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700703 link = &(*link)->rb_right;
704 else
705 return cur_timewait_info;
706 }
707 timewait_info->inserted_remote_qp = 1;
708 rb_link_node(&timewait_info->remote_qp_node, parent, link);
709 rb_insert_color(&timewait_info->remote_qp_node, &cm.remote_qp_table);
710 return NULL;
711}
712
713static struct cm_id_private * cm_insert_remote_sidr(struct cm_id_private
714 *cm_id_priv)
715{
716 struct rb_node **link = &cm.remote_sidr_table.rb_node;
717 struct rb_node *parent = NULL;
718 struct cm_id_private *cur_cm_id_priv;
719 union ib_gid *port_gid = &cm_id_priv->av.dgid;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700720 __be32 remote_id = cm_id_priv->id.remote_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700721
722 while (*link) {
723 parent = *link;
724 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
725 sidr_id_node);
Roland Dreiera88f4882008-04-16 21:01:07 -0700726 if (be32_lt(remote_id, cur_cm_id_priv->id.remote_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700727 link = &(*link)->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700728 else if (be32_gt(remote_id, cur_cm_id_priv->id.remote_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700729 link = &(*link)->rb_right;
730 else {
731 int cmp;
732 cmp = memcmp(port_gid, &cur_cm_id_priv->av.dgid,
733 sizeof *port_gid);
734 if (cmp < 0)
735 link = &(*link)->rb_left;
736 else if (cmp > 0)
737 link = &(*link)->rb_right;
738 else
739 return cur_cm_id_priv;
740 }
741 }
742 rb_link_node(&cm_id_priv->sidr_id_node, parent, link);
743 rb_insert_color(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
744 return NULL;
745}
746
747static void cm_reject_sidr_req(struct cm_id_private *cm_id_priv,
748 enum ib_cm_sidr_status status)
749{
750 struct ib_cm_sidr_rep_param param;
751
752 memset(&param, 0, sizeof param);
753 param.status = status;
754 ib_send_cm_sidr_rep(&cm_id_priv->id, &param);
755}
756
Sean Hefty07d357d2005-10-17 15:37:43 -0700757struct ib_cm_id *ib_create_cm_id(struct ib_device *device,
758 ib_cm_handler cm_handler,
Hal Rosenstocka9770492005-07-27 11:45:40 -0700759 void *context)
760{
761 struct cm_id_private *cm_id_priv;
762 int ret;
763
Roland Dreierde6eb662005-11-02 07:23:14 -0800764 cm_id_priv = kzalloc(sizeof *cm_id_priv, GFP_KERNEL);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700765 if (!cm_id_priv)
766 return ERR_PTR(-ENOMEM);
767
Hal Rosenstocka9770492005-07-27 11:45:40 -0700768 cm_id_priv->id.state = IB_CM_IDLE;
Sean Hefty07d357d2005-10-17 15:37:43 -0700769 cm_id_priv->id.device = device;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700770 cm_id_priv->id.cm_handler = cm_handler;
771 cm_id_priv->id.context = context;
John Kingman354ba392005-09-09 18:23:32 -0700772 cm_id_priv->id.remote_cm_qpn = 1;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700773 ret = cm_alloc_id(cm_id_priv);
774 if (ret)
775 goto error;
776
777 spin_lock_init(&cm_id_priv->lock);
Sean Hefty1b52fa982006-05-12 14:57:52 -0700778 init_completion(&cm_id_priv->comp);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700779 INIT_LIST_HEAD(&cm_id_priv->work_list);
Mark Bloch9db0ff52016-10-27 16:36:27 +0300780 INIT_LIST_HEAD(&cm_id_priv->prim_list);
781 INIT_LIST_HEAD(&cm_id_priv->altr_list);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700782 atomic_set(&cm_id_priv->work_count, -1);
783 atomic_set(&cm_id_priv->refcount, 1);
784 return &cm_id_priv->id;
785
786error:
787 kfree(cm_id_priv);
788 return ERR_PTR(-ENOMEM);
789}
790EXPORT_SYMBOL(ib_create_cm_id);
791
792static struct cm_work * cm_dequeue_work(struct cm_id_private *cm_id_priv)
793{
794 struct cm_work *work;
795
796 if (list_empty(&cm_id_priv->work_list))
797 return NULL;
798
799 work = list_entry(cm_id_priv->work_list.next, struct cm_work, list);
800 list_del(&work->list);
801 return work;
802}
803
804static void cm_free_work(struct cm_work *work)
805{
806 if (work->mad_recv_wc)
807 ib_free_recv_mad(work->mad_recv_wc);
808 kfree(work);
809}
810
811static inline int cm_convert_to_ms(int iba_time)
812{
813 /* approximate conversion to ms from 4.096us x 2^iba_time */
814 return 1 << max(iba_time - 8, 0);
815}
816
Sean Hefty1d846122007-06-18 11:09:37 -0700817/*
818 * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time
819 * Because of how ack_timeout is stored, adding one doubles the timeout.
820 * To avoid large timeouts, select the max(ack_delay, life_time + 1), and
821 * increment it (round up) only if the other is within 50%.
822 */
823static u8 cm_ack_timeout(u8 ca_ack_delay, u8 packet_life_time)
824{
825 int ack_timeout = packet_life_time + 1;
826
827 if (ack_timeout >= ca_ack_delay)
828 ack_timeout += (ca_ack_delay >= (ack_timeout - 1));
829 else
830 ack_timeout = ca_ack_delay +
831 (ack_timeout >= (ca_ack_delay - 1));
832
833 return min(31, ack_timeout);
834}
835
Hal Rosenstocka9770492005-07-27 11:45:40 -0700836static void cm_cleanup_timewait(struct cm_timewait_info *timewait_info)
837{
Hal Rosenstocka9770492005-07-27 11:45:40 -0700838 if (timewait_info->inserted_remote_id) {
839 rb_erase(&timewait_info->remote_id_node, &cm.remote_id_table);
840 timewait_info->inserted_remote_id = 0;
841 }
842
843 if (timewait_info->inserted_remote_qp) {
844 rb_erase(&timewait_info->remote_qp_node, &cm.remote_qp_table);
845 timewait_info->inserted_remote_qp = 0;
846 }
Hal Rosenstocka9770492005-07-27 11:45:40 -0700847}
848
Sean Hefty97f52eb2005-08-13 21:05:57 -0700849static struct cm_timewait_info * cm_create_timewait_info(__be32 local_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700850{
851 struct cm_timewait_info *timewait_info;
852
Roland Dreierde6eb662005-11-02 07:23:14 -0800853 timewait_info = kzalloc(sizeof *timewait_info, GFP_KERNEL);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700854 if (!timewait_info)
855 return ERR_PTR(-ENOMEM);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700856
857 timewait_info->work.local_id = local_id;
David Howellsc4028952006-11-22 14:57:56 +0000858 INIT_DELAYED_WORK(&timewait_info->work.work, cm_work_handler);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700859 timewait_info->work.cm_event.event = IB_CM_TIMEWAIT_EXIT;
860 return timewait_info;
861}
862
863static void cm_enter_timewait(struct cm_id_private *cm_id_priv)
864{
865 int wait_time;
Sean Hefty85753292006-10-04 11:29:59 -0700866 unsigned long flags;
Erez Shitritbe4b4992015-06-25 17:13:22 +0300867 struct cm_device *cm_dev;
868
869 cm_dev = ib_get_client_data(cm_id_priv->id.device, &cm_client);
870 if (!cm_dev)
871 return;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700872
Sean Hefty85753292006-10-04 11:29:59 -0700873 spin_lock_irqsave(&cm.lock, flags);
Michael S. Tsirkina70d0592006-08-28 16:32:50 +0300874 cm_cleanup_timewait(cm_id_priv->timewait_info);
Sean Hefty85753292006-10-04 11:29:59 -0700875 list_add_tail(&cm_id_priv->timewait_info->list, &cm.timewait_list);
876 spin_unlock_irqrestore(&cm.lock, flags);
Michael S. Tsirkina70d0592006-08-28 16:32:50 +0300877
Hal Rosenstocka9770492005-07-27 11:45:40 -0700878 /*
879 * The cm_id could be destroyed by the user before we exit timewait.
880 * To protect against this, we search for the cm_id after exiting
881 * timewait before notifying the user that we've exited timewait.
882 */
883 cm_id_priv->id.state = IB_CM_TIMEWAIT;
Sean Hefty1d846122007-06-18 11:09:37 -0700884 wait_time = cm_convert_to_ms(cm_id_priv->av.timeout);
Erez Shitritbe4b4992015-06-25 17:13:22 +0300885
886 /* Check if the device started its remove_one */
Bart Van Assche4bfdf632016-01-01 13:17:46 +0100887 spin_lock_irqsave(&cm.lock, flags);
Erez Shitritbe4b4992015-06-25 17:13:22 +0300888 if (!cm_dev->going_down)
889 queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work,
890 msecs_to_jiffies(wait_time));
Bart Van Assche4bfdf632016-01-01 13:17:46 +0100891 spin_unlock_irqrestore(&cm.lock, flags);
Erez Shitritbe4b4992015-06-25 17:13:22 +0300892
Hal Rosenstocka9770492005-07-27 11:45:40 -0700893 cm_id_priv->timewait_info = NULL;
894}
895
896static void cm_reset_to_idle(struct cm_id_private *cm_id_priv)
897{
Sean Hefty85753292006-10-04 11:29:59 -0700898 unsigned long flags;
899
Hal Rosenstocka9770492005-07-27 11:45:40 -0700900 cm_id_priv->id.state = IB_CM_IDLE;
901 if (cm_id_priv->timewait_info) {
Sean Hefty85753292006-10-04 11:29:59 -0700902 spin_lock_irqsave(&cm.lock, flags);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700903 cm_cleanup_timewait(cm_id_priv->timewait_info);
Sean Hefty85753292006-10-04 11:29:59 -0700904 spin_unlock_irqrestore(&cm.lock, flags);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700905 kfree(cm_id_priv->timewait_info);
906 cm_id_priv->timewait_info = NULL;
907 }
908}
909
Michael S. Tsirkin04c33542006-07-14 00:23:52 -0700910static void cm_destroy_id(struct ib_cm_id *cm_id, int err)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700911{
912 struct cm_id_private *cm_id_priv;
913 struct cm_work *work;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700914
915 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
916retest:
Sean Hefty24be6e82007-06-18 11:09:36 -0700917 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700918 switch (cm_id->state) {
919 case IB_CM_LISTEN:
Sean Hefty24be6e82007-06-18 11:09:36 -0700920 spin_unlock_irq(&cm_id_priv->lock);
Haggai Eran067b1712015-07-30 17:50:18 +0300921
Sean Hefty24be6e82007-06-18 11:09:36 -0700922 spin_lock_irq(&cm.lock);
Haggai Eran067b1712015-07-30 17:50:18 +0300923 if (--cm_id_priv->listen_sharecount > 0) {
924 /* The id is still shared. */
925 cm_deref_id(cm_id_priv);
926 spin_unlock_irq(&cm.lock);
927 return;
928 }
Hal Rosenstocka9770492005-07-27 11:45:40 -0700929 rb_erase(&cm_id_priv->service_node, &cm.listen_service_table);
Sean Hefty24be6e82007-06-18 11:09:36 -0700930 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700931 break;
932 case IB_CM_SIDR_REQ_SENT:
933 cm_id->state = IB_CM_IDLE;
Sean Hefty34816ad2005-10-25 10:51:39 -0700934 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Sean Hefty24be6e82007-06-18 11:09:36 -0700935 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700936 break;
937 case IB_CM_SIDR_REQ_RCVD:
Sean Hefty24be6e82007-06-18 11:09:36 -0700938 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700939 cm_reject_sidr_req(cm_id_priv, IB_SIDR_REJECT);
Doron Tsur0ca81a22015-10-11 15:58:17 +0300940 spin_lock_irq(&cm.lock);
941 if (!RB_EMPTY_NODE(&cm_id_priv->sidr_id_node))
942 rb_erase(&cm_id_priv->sidr_id_node,
943 &cm.remote_sidr_table);
944 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700945 break;
946 case IB_CM_REQ_SENT:
Ted Kimc29ed5a2015-05-14 12:49:01 -0700947 case IB_CM_MRA_REQ_RCVD:
Sean Hefty227eca82005-11-30 10:00:25 -0800948 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Sean Hefty24be6e82007-06-18 11:09:36 -0700949 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty227eca82005-11-30 10:00:25 -0800950 ib_send_cm_rej(cm_id, IB_CM_REJ_TIMEOUT,
Sean Heftye971b8c2007-02-22 11:37:38 -0800951 &cm_id_priv->id.device->node_guid,
952 sizeof cm_id_priv->id.device->node_guid,
Sean Hefty227eca82005-11-30 10:00:25 -0800953 NULL, 0);
954 break;
Michael S. Tsirkin04c33542006-07-14 00:23:52 -0700955 case IB_CM_REQ_RCVD:
956 if (err == -ENOMEM) {
957 /* Do not reject to allow future retries. */
958 cm_reset_to_idle(cm_id_priv);
Sean Hefty24be6e82007-06-18 11:09:36 -0700959 spin_unlock_irq(&cm_id_priv->lock);
Michael S. Tsirkin04c33542006-07-14 00:23:52 -0700960 } else {
Sean Hefty24be6e82007-06-18 11:09:36 -0700961 spin_unlock_irq(&cm_id_priv->lock);
Michael S. Tsirkin04c33542006-07-14 00:23:52 -0700962 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
963 NULL, 0, NULL, 0);
964 }
965 break;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700966 case IB_CM_REP_SENT:
967 case IB_CM_MRA_REP_RCVD:
Sean Hefty34816ad2005-10-25 10:51:39 -0700968 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700969 /* Fall through */
Hal Rosenstocka9770492005-07-27 11:45:40 -0700970 case IB_CM_MRA_REQ_SENT:
971 case IB_CM_REP_RCVD:
972 case IB_CM_MRA_REP_SENT:
Sean Hefty24be6e82007-06-18 11:09:36 -0700973 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty227eca82005-11-30 10:00:25 -0800974 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
975 NULL, 0, NULL, 0);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700976 break;
977 case IB_CM_ESTABLISHED:
Sean Hefty24be6e82007-06-18 11:09:36 -0700978 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty2622e182011-05-30 22:30:46 -0700979 if (cm_id_priv->qp_type == IB_QPT_XRC_TGT)
980 break;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700981 ib_send_cm_dreq(cm_id, NULL, 0);
982 goto retest;
983 case IB_CM_DREQ_SENT:
Sean Hefty34816ad2005-10-25 10:51:39 -0700984 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700985 cm_enter_timewait(cm_id_priv);
Sean Hefty24be6e82007-06-18 11:09:36 -0700986 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700987 break;
988 case IB_CM_DREQ_RCVD:
Sean Hefty24be6e82007-06-18 11:09:36 -0700989 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700990 ib_send_cm_drep(cm_id, NULL, 0);
991 break;
992 default:
Sean Hefty24be6e82007-06-18 11:09:36 -0700993 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700994 break;
995 }
996
Mark Bloch9db0ff52016-10-27 16:36:27 +0300997 spin_lock_irq(&cm.lock);
998 if (!list_empty(&cm_id_priv->altr_list) &&
999 (!cm_id_priv->altr_send_port_not_ready))
1000 list_del(&cm_id_priv->altr_list);
1001 if (!list_empty(&cm_id_priv->prim_list) &&
1002 (!cm_id_priv->prim_send_port_not_ready))
1003 list_del(&cm_id_priv->prim_list);
1004 spin_unlock_irq(&cm.lock);
1005
Hal Rosenstocka9770492005-07-27 11:45:40 -07001006 cm_free_id(cm_id->local_id);
Sean Hefty1b52fa982006-05-12 14:57:52 -07001007 cm_deref_id(cm_id_priv);
1008 wait_for_completion(&cm_id_priv->comp);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001009 while ((work = cm_dequeue_work(cm_id_priv)) != NULL)
1010 cm_free_work(work);
Sean Hefty6e61d042006-06-17 20:37:28 -07001011 kfree(cm_id_priv->private_data);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001012 kfree(cm_id_priv);
1013}
Michael S. Tsirkin04c33542006-07-14 00:23:52 -07001014
1015void ib_destroy_cm_id(struct ib_cm_id *cm_id)
1016{
1017 cm_destroy_id(cm_id, 0);
1018}
Hal Rosenstocka9770492005-07-27 11:45:40 -07001019EXPORT_SYMBOL(ib_destroy_cm_id);
1020
Haggai Eran067b1712015-07-30 17:50:18 +03001021/**
1022 * __ib_cm_listen - Initiates listening on the specified service ID for
1023 * connection and service ID resolution requests.
1024 * @cm_id: Connection identifier associated with the listen request.
1025 * @service_id: Service identifier matched against incoming connection
1026 * and service ID resolution requests. The service ID should be specified
1027 * network-byte order. If set to IB_CM_ASSIGN_SERVICE_ID, the CM will
1028 * assign a service ID to the caller.
1029 * @service_mask: Mask applied to service ID used to listen across a
1030 * range of service IDs. If set to 0, the service ID is matched
1031 * exactly. This parameter is ignored if %service_id is set to
1032 * IB_CM_ASSIGN_SERVICE_ID.
Haggai Eran067b1712015-07-30 17:50:18 +03001033 */
1034static int __ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id,
Haggai Eran73fec7f2015-07-30 17:50:26 +03001035 __be64 service_mask)
Hal Rosenstocka9770492005-07-27 11:45:40 -07001036{
1037 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001038 int ret = 0;
1039
Harvey Harrison9c3da092009-01-17 17:11:57 -08001040 service_mask = service_mask ? service_mask : ~cpu_to_be64(0);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001041 service_id &= service_mask;
1042 if ((service_id & IB_SERVICE_ID_AGN_MASK) == IB_CM_ASSIGN_SERVICE_ID &&
1043 (service_id != IB_CM_ASSIGN_SERVICE_ID))
1044 return -EINVAL;
1045
1046 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
Sean Hefty6e61d042006-06-17 20:37:28 -07001047 if (cm_id->state != IB_CM_IDLE)
1048 return -EINVAL;
1049
Hal Rosenstocka9770492005-07-27 11:45:40 -07001050 cm_id->state = IB_CM_LISTEN;
Haggai Eran067b1712015-07-30 17:50:18 +03001051 ++cm_id_priv->listen_sharecount;
Haggai Eran73fec7f2015-07-30 17:50:26 +03001052
Hal Rosenstocka9770492005-07-27 11:45:40 -07001053 if (service_id == IB_CM_ASSIGN_SERVICE_ID) {
Sean Hefty97f52eb2005-08-13 21:05:57 -07001054 cm_id->service_id = cpu_to_be64(cm.listen_service_id++);
Harvey Harrison9c3da092009-01-17 17:11:57 -08001055 cm_id->service_mask = ~cpu_to_be64(0);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001056 } else {
1057 cm_id->service_id = service_id;
1058 cm_id->service_mask = service_mask;
1059 }
1060 cur_cm_id_priv = cm_insert_listen(cm_id_priv);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001061
1062 if (cur_cm_id_priv) {
1063 cm_id->state = IB_CM_IDLE;
Haggai Eran067b1712015-07-30 17:50:18 +03001064 --cm_id_priv->listen_sharecount;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001065 ret = -EBUSY;
1066 }
1067 return ret;
1068}
Haggai Eran067b1712015-07-30 17:50:18 +03001069
Haggai Eran73fec7f2015-07-30 17:50:26 +03001070int ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id, __be64 service_mask)
Haggai Eran067b1712015-07-30 17:50:18 +03001071{
Haggai Eran73fec7f2015-07-30 17:50:26 +03001072 unsigned long flags;
1073 int ret;
1074
1075 spin_lock_irqsave(&cm.lock, flags);
1076 ret = __ib_cm_listen(cm_id, service_id, service_mask);
1077 spin_unlock_irqrestore(&cm.lock, flags);
1078
1079 return ret;
Haggai Eran067b1712015-07-30 17:50:18 +03001080}
Hal Rosenstocka9770492005-07-27 11:45:40 -07001081EXPORT_SYMBOL(ib_cm_listen);
1082
Haggai Eran067b1712015-07-30 17:50:18 +03001083/**
1084 * Create a new listening ib_cm_id and listen on the given service ID.
1085 *
1086 * If there's an existing ID listening on that same device and service ID,
1087 * return it.
1088 *
1089 * @device: Device associated with the cm_id. All related communication will
1090 * be associated with the specified device.
1091 * @cm_handler: Callback invoked to notify the user of CM events.
1092 * @service_id: Service identifier matched against incoming connection
1093 * and service ID resolution requests. The service ID should be specified
1094 * network-byte order. If set to IB_CM_ASSIGN_SERVICE_ID, the CM will
1095 * assign a service ID to the caller.
1096 *
1097 * Callers should call ib_destroy_cm_id when done with the listener ID.
1098 */
1099struct ib_cm_id *ib_cm_insert_listen(struct ib_device *device,
1100 ib_cm_handler cm_handler,
1101 __be64 service_id)
1102{
1103 struct cm_id_private *cm_id_priv;
1104 struct ib_cm_id *cm_id;
1105 unsigned long flags;
1106 int err = 0;
1107
1108 /* Create an ID in advance, since the creation may sleep */
1109 cm_id = ib_create_cm_id(device, cm_handler, NULL);
1110 if (IS_ERR(cm_id))
1111 return cm_id;
1112
1113 spin_lock_irqsave(&cm.lock, flags);
1114
1115 if (service_id == IB_CM_ASSIGN_SERVICE_ID)
1116 goto new_id;
1117
1118 /* Find an existing ID */
Haggai Eran73fec7f2015-07-30 17:50:26 +03001119 cm_id_priv = cm_find_listen(device, service_id);
Haggai Eran067b1712015-07-30 17:50:18 +03001120 if (cm_id_priv) {
1121 if (cm_id->cm_handler != cm_handler || cm_id->context) {
1122 /* Sharing an ib_cm_id with different handlers is not
1123 * supported */
1124 spin_unlock_irqrestore(&cm.lock, flags);
1125 return ERR_PTR(-EINVAL);
1126 }
1127 atomic_inc(&cm_id_priv->refcount);
1128 ++cm_id_priv->listen_sharecount;
1129 spin_unlock_irqrestore(&cm.lock, flags);
1130
1131 ib_destroy_cm_id(cm_id);
1132 cm_id = &cm_id_priv->id;
1133 return cm_id;
1134 }
1135
1136new_id:
1137 /* Use newly created ID */
Haggai Eran73fec7f2015-07-30 17:50:26 +03001138 err = __ib_cm_listen(cm_id, service_id, 0);
Haggai Eran067b1712015-07-30 17:50:18 +03001139
1140 spin_unlock_irqrestore(&cm.lock, flags);
1141
1142 if (err) {
1143 ib_destroy_cm_id(cm_id);
1144 return ERR_PTR(err);
1145 }
1146 return cm_id;
1147}
1148EXPORT_SYMBOL(ib_cm_insert_listen);
1149
Sean Hefty97f52eb2005-08-13 21:05:57 -07001150static __be64 cm_form_tid(struct cm_id_private *cm_id_priv,
1151 enum cm_msg_sequence msg_seq)
Hal Rosenstocka9770492005-07-27 11:45:40 -07001152{
1153 u64 hi_tid, low_tid;
1154
1155 hi_tid = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32;
Sean Hefty97f52eb2005-08-13 21:05:57 -07001156 low_tid = (u64) ((__force u32)cm_id_priv->id.local_id |
1157 (msg_seq << 30));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001158 return cpu_to_be64(hi_tid | low_tid);
1159}
1160
1161static void cm_format_mad_hdr(struct ib_mad_hdr *hdr,
Sean Hefty97f52eb2005-08-13 21:05:57 -07001162 __be16 attr_id, __be64 tid)
Hal Rosenstocka9770492005-07-27 11:45:40 -07001163{
1164 hdr->base_version = IB_MGMT_BASE_VERSION;
1165 hdr->mgmt_class = IB_MGMT_CLASS_CM;
1166 hdr->class_version = IB_CM_CLASS_VERSION;
1167 hdr->method = IB_MGMT_METHOD_SEND;
1168 hdr->attr_id = attr_id;
1169 hdr->tid = tid;
1170}
1171
1172static void cm_format_req(struct cm_req_msg *req_msg,
1173 struct cm_id_private *cm_id_priv,
1174 struct ib_cm_req_param *param)
1175{
Dasaratharaman Chandramoulic2f8fc42017-04-27 19:05:58 -04001176 struct sa_path_rec *pri_path = param->primary_path;
1177 struct sa_path_rec *alt_path = param->alternate_path;
Sean Hefty3971c9f2007-12-10 15:53:25 -08001178
Hal Rosenstocka9770492005-07-27 11:45:40 -07001179 cm_format_mad_hdr(&req_msg->hdr, CM_REQ_ATTR_ID,
1180 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_REQ));
1181
1182 req_msg->local_comm_id = cm_id_priv->id.local_id;
1183 req_msg->service_id = param->service_id;
Sean Heftye971b8c2007-02-22 11:37:38 -08001184 req_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001185 cm_req_set_local_qpn(req_msg, cpu_to_be32(param->qp_num));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001186 cm_req_set_init_depth(req_msg, param->initiator_depth);
1187 cm_req_set_remote_resp_timeout(req_msg,
1188 param->remote_cm_response_timeout);
1189 cm_req_set_qp_type(req_msg, param->qp_type);
1190 cm_req_set_flow_ctrl(req_msg, param->flow_control);
1191 cm_req_set_starting_psn(req_msg, cpu_to_be32(param->starting_psn));
1192 cm_req_set_local_resp_timeout(req_msg,
1193 param->local_cm_response_timeout);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001194 req_msg->pkey = param->primary_path->pkey;
1195 cm_req_set_path_mtu(req_msg, param->primary_path->mtu);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001196 cm_req_set_max_cm_retries(req_msg, param->max_cm_retries);
Sean Heftyd26a3602011-05-13 10:46:20 -07001197
1198 if (param->qp_type != IB_QPT_XRC_INI) {
1199 cm_req_set_resp_res(req_msg, param->responder_resources);
1200 cm_req_set_retry_count(req_msg, param->retry_count);
1201 cm_req_set_rnr_retry_count(req_msg, param->rnr_retry_count);
1202 cm_req_set_srq(req_msg, param->srq);
1203 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07001204
Sean Hefty3971c9f2007-12-10 15:53:25 -08001205 if (pri_path->hop_limit <= 1) {
Dasaratharaman Chandramouli57520752017-04-27 19:06:01 -04001206 req_msg->primary_local_lid =
1207 htons(ntohl(sa_path_get_slid(pri_path)));
1208 req_msg->primary_remote_lid =
1209 htons(ntohl(sa_path_get_dlid(pri_path)));
Sean Hefty3971c9f2007-12-10 15:53:25 -08001210 } else {
1211 /* Work-around until there's a way to obtain remote LID info */
1212 req_msg->primary_local_lid = IB_LID_PERMISSIVE;
1213 req_msg->primary_remote_lid = IB_LID_PERMISSIVE;
1214 }
1215 req_msg->primary_local_gid = pri_path->sgid;
1216 req_msg->primary_remote_gid = pri_path->dgid;
1217 cm_req_set_primary_flow_label(req_msg, pri_path->flow_label);
1218 cm_req_set_primary_packet_rate(req_msg, pri_path->rate);
1219 req_msg->primary_traffic_class = pri_path->traffic_class;
1220 req_msg->primary_hop_limit = pri_path->hop_limit;
1221 cm_req_set_primary_sl(req_msg, pri_path->sl);
1222 cm_req_set_primary_subnet_local(req_msg, (pri_path->hop_limit <= 1));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001223 cm_req_set_primary_local_ack_timeout(req_msg,
Sean Hefty1d846122007-06-18 11:09:37 -07001224 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
Sean Hefty3971c9f2007-12-10 15:53:25 -08001225 pri_path->packet_life_time));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001226
Sean Hefty3971c9f2007-12-10 15:53:25 -08001227 if (alt_path) {
1228 if (alt_path->hop_limit <= 1) {
Dasaratharaman Chandramouli57520752017-04-27 19:06:01 -04001229 req_msg->alt_local_lid =
1230 htons(ntohl(sa_path_get_slid(alt_path)));
1231 req_msg->alt_remote_lid =
1232 htons(ntohl(sa_path_get_dlid(alt_path)));
Sean Hefty3971c9f2007-12-10 15:53:25 -08001233 } else {
1234 req_msg->alt_local_lid = IB_LID_PERMISSIVE;
1235 req_msg->alt_remote_lid = IB_LID_PERMISSIVE;
1236 }
1237 req_msg->alt_local_gid = alt_path->sgid;
1238 req_msg->alt_remote_gid = alt_path->dgid;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001239 cm_req_set_alt_flow_label(req_msg,
Sean Hefty3971c9f2007-12-10 15:53:25 -08001240 alt_path->flow_label);
1241 cm_req_set_alt_packet_rate(req_msg, alt_path->rate);
1242 req_msg->alt_traffic_class = alt_path->traffic_class;
1243 req_msg->alt_hop_limit = alt_path->hop_limit;
1244 cm_req_set_alt_sl(req_msg, alt_path->sl);
1245 cm_req_set_alt_subnet_local(req_msg, (alt_path->hop_limit <= 1));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001246 cm_req_set_alt_local_ack_timeout(req_msg,
Sean Hefty1d846122007-06-18 11:09:37 -07001247 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
Sean Hefty3971c9f2007-12-10 15:53:25 -08001248 alt_path->packet_life_time));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001249 }
1250
1251 if (param->private_data && param->private_data_len)
1252 memcpy(req_msg->private_data, param->private_data,
1253 param->private_data_len);
1254}
1255
Arjan van de Ven858119e2006-01-14 13:20:43 -08001256static int cm_validate_req_param(struct ib_cm_req_param *param)
Hal Rosenstocka9770492005-07-27 11:45:40 -07001257{
1258 /* peer-to-peer not supported */
1259 if (param->peer_to_peer)
1260 return -EINVAL;
1261
1262 if (!param->primary_path)
1263 return -EINVAL;
1264
Sean Heftyd26a3602011-05-13 10:46:20 -07001265 if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC &&
1266 param->qp_type != IB_QPT_XRC_INI)
Hal Rosenstocka9770492005-07-27 11:45:40 -07001267 return -EINVAL;
1268
1269 if (param->private_data &&
1270 param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE)
1271 return -EINVAL;
1272
1273 if (param->alternate_path &&
1274 (param->alternate_path->pkey != param->primary_path->pkey ||
1275 param->alternate_path->mtu != param->primary_path->mtu))
1276 return -EINVAL;
1277
1278 return 0;
1279}
1280
1281int ib_send_cm_req(struct ib_cm_id *cm_id,
1282 struct ib_cm_req_param *param)
1283{
1284 struct cm_id_private *cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001285 struct cm_req_msg *req_msg;
1286 unsigned long flags;
1287 int ret;
1288
1289 ret = cm_validate_req_param(param);
1290 if (ret)
1291 return ret;
1292
1293 /* Verify that we're not in timewait. */
1294 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1295 spin_lock_irqsave(&cm_id_priv->lock, flags);
1296 if (cm_id->state != IB_CM_IDLE) {
1297 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1298 ret = -EINVAL;
1299 goto out;
1300 }
1301 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1302
1303 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1304 id.local_id);
Sean Hefty75df23e2006-07-25 19:52:01 +03001305 if (IS_ERR(cm_id_priv->timewait_info)) {
1306 ret = PTR_ERR(cm_id_priv->timewait_info);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001307 goto out;
Sean Hefty75df23e2006-07-25 19:52:01 +03001308 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07001309
Mark Bloch9db0ff52016-10-27 16:36:27 +03001310 ret = cm_init_av_by_path(param->primary_path, &cm_id_priv->av,
1311 cm_id_priv);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001312 if (ret)
1313 goto error1;
1314 if (param->alternate_path) {
1315 ret = cm_init_av_by_path(param->alternate_path,
Mark Bloch9db0ff52016-10-27 16:36:27 +03001316 &cm_id_priv->alt_av, cm_id_priv);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001317 if (ret)
1318 goto error1;
1319 }
1320 cm_id->service_id = param->service_id;
Harvey Harrison9c3da092009-01-17 17:11:57 -08001321 cm_id->service_mask = ~cpu_to_be64(0);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001322 cm_id_priv->timeout_ms = cm_convert_to_ms(
1323 param->primary_path->packet_life_time) * 2 +
1324 cm_convert_to_ms(
1325 param->remote_cm_response_timeout);
1326 cm_id_priv->max_cm_retries = param->max_cm_retries;
1327 cm_id_priv->initiator_depth = param->initiator_depth;
1328 cm_id_priv->responder_resources = param->responder_resources;
1329 cm_id_priv->retry_count = param->retry_count;
1330 cm_id_priv->path_mtu = param->primary_path->mtu;
Sean Heftye1444b52006-11-28 14:57:13 -08001331 cm_id_priv->pkey = param->primary_path->pkey;
Sean Heftyae7971a2005-10-24 12:33:56 -07001332 cm_id_priv->qp_type = param->qp_type;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001333
1334 ret = cm_alloc_msg(cm_id_priv, &cm_id_priv->msg);
1335 if (ret)
1336 goto error1;
1337
1338 req_msg = (struct cm_req_msg *) cm_id_priv->msg->mad;
1339 cm_format_req(req_msg, cm_id_priv, param);
1340 cm_id_priv->tid = req_msg->hdr.tid;
Sean Hefty34816ad2005-10-25 10:51:39 -07001341 cm_id_priv->msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001342 cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT;
1343
1344 cm_id_priv->local_qpn = cm_req_get_local_qpn(req_msg);
1345 cm_id_priv->rq_psn = cm_req_get_starting_psn(req_msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001346
1347 spin_lock_irqsave(&cm_id_priv->lock, flags);
Sean Hefty34816ad2005-10-25 10:51:39 -07001348 ret = ib_post_send_mad(cm_id_priv->msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001349 if (ret) {
1350 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1351 goto error2;
1352 }
1353 BUG_ON(cm_id->state != IB_CM_IDLE);
1354 cm_id->state = IB_CM_REQ_SENT;
1355 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1356 return 0;
1357
1358error2: cm_free_msg(cm_id_priv->msg);
1359error1: kfree(cm_id_priv->timewait_info);
1360out: return ret;
1361}
1362EXPORT_SYMBOL(ib_send_cm_req);
1363
1364static int cm_issue_rej(struct cm_port *port,
1365 struct ib_mad_recv_wc *mad_recv_wc,
1366 enum ib_cm_rej_reason reason,
1367 enum cm_msg_response msg_rejected,
1368 void *ari, u8 ari_length)
1369{
1370 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001371 struct cm_rej_msg *rej_msg, *rcv_msg;
1372 int ret;
1373
1374 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
1375 if (ret)
1376 return ret;
1377
1378 /* We just need common CM header information. Cast to any message. */
1379 rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad;
1380 rej_msg = (struct cm_rej_msg *) msg->mad;
1381
1382 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid);
1383 rej_msg->remote_comm_id = rcv_msg->local_comm_id;
1384 rej_msg->local_comm_id = rcv_msg->remote_comm_id;
1385 cm_rej_set_msg_rejected(rej_msg, msg_rejected);
Sean Hefty97f52eb2005-08-13 21:05:57 -07001386 rej_msg->reason = cpu_to_be16(reason);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001387
1388 if (ari && ari_length) {
1389 cm_rej_set_reject_info_len(rej_msg, ari_length);
1390 memcpy(rej_msg->ari, ari, ari_length);
1391 }
1392
Sean Hefty34816ad2005-10-25 10:51:39 -07001393 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001394 if (ret)
1395 cm_free_msg(msg);
1396
1397 return ret;
1398}
1399
Sean Hefty97f52eb2005-08-13 21:05:57 -07001400static inline int cm_is_active_peer(__be64 local_ca_guid, __be64 remote_ca_guid,
1401 __be32 local_qpn, __be32 remote_qpn)
Hal Rosenstocka9770492005-07-27 11:45:40 -07001402{
1403 return (be64_to_cpu(local_ca_guid) > be64_to_cpu(remote_ca_guid) ||
1404 ((local_ca_guid == remote_ca_guid) &&
1405 (be32_to_cpu(local_qpn) > be32_to_cpu(remote_qpn))));
1406}
1407
Arjan van de Ven858119e2006-01-14 13:20:43 -08001408static void cm_format_paths_from_req(struct cm_req_msg *req_msg,
Dasaratharaman Chandramoulic2f8fc42017-04-27 19:05:58 -04001409 struct sa_path_rec *primary_path,
1410 struct sa_path_rec *alt_path)
Hal Rosenstocka9770492005-07-27 11:45:40 -07001411{
Hal Rosenstocka9770492005-07-27 11:45:40 -07001412 primary_path->dgid = req_msg->primary_local_gid;
1413 primary_path->sgid = req_msg->primary_remote_gid;
Dasaratharaman Chandramouli57520752017-04-27 19:06:01 -04001414 sa_path_set_dlid(primary_path,
1415 htonl(ntohs(req_msg->primary_local_lid)));
1416 sa_path_set_slid(primary_path,
1417 htonl(ntohs(req_msg->primary_remote_lid)));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001418 primary_path->flow_label = cm_req_get_primary_flow_label(req_msg);
1419 primary_path->hop_limit = req_msg->primary_hop_limit;
1420 primary_path->traffic_class = req_msg->primary_traffic_class;
1421 primary_path->reversible = 1;
1422 primary_path->pkey = req_msg->pkey;
1423 primary_path->sl = cm_req_get_primary_sl(req_msg);
1424 primary_path->mtu_selector = IB_SA_EQ;
1425 primary_path->mtu = cm_req_get_path_mtu(req_msg);
1426 primary_path->rate_selector = IB_SA_EQ;
1427 primary_path->rate = cm_req_get_primary_packet_rate(req_msg);
1428 primary_path->packet_life_time_selector = IB_SA_EQ;
1429 primary_path->packet_life_time =
1430 cm_req_get_primary_local_ack_timeout(req_msg);
1431 primary_path->packet_life_time -= (primary_path->packet_life_time > 0);
Majd Dibbinyd3957b862017-05-21 19:09:54 +03001432 primary_path->service_id = req_msg->service_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001433
1434 if (req_msg->alt_local_lid) {
Hal Rosenstocka9770492005-07-27 11:45:40 -07001435 alt_path->dgid = req_msg->alt_local_gid;
1436 alt_path->sgid = req_msg->alt_remote_gid;
Dasaratharaman Chandramouli57520752017-04-27 19:06:01 -04001437 sa_path_set_dlid(alt_path,
1438 htonl(ntohs(req_msg->alt_local_lid)));
1439 sa_path_set_slid(alt_path,
1440 htonl(ntohs(req_msg->alt_remote_lid)));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001441 alt_path->flow_label = cm_req_get_alt_flow_label(req_msg);
1442 alt_path->hop_limit = req_msg->alt_hop_limit;
1443 alt_path->traffic_class = req_msg->alt_traffic_class;
1444 alt_path->reversible = 1;
1445 alt_path->pkey = req_msg->pkey;
1446 alt_path->sl = cm_req_get_alt_sl(req_msg);
1447 alt_path->mtu_selector = IB_SA_EQ;
1448 alt_path->mtu = cm_req_get_path_mtu(req_msg);
1449 alt_path->rate_selector = IB_SA_EQ;
1450 alt_path->rate = cm_req_get_alt_packet_rate(req_msg);
1451 alt_path->packet_life_time_selector = IB_SA_EQ;
1452 alt_path->packet_life_time =
1453 cm_req_get_alt_local_ack_timeout(req_msg);
1454 alt_path->packet_life_time -= (alt_path->packet_life_time > 0);
Majd Dibbinyd3957b862017-05-21 19:09:54 +03001455 alt_path->service_id = req_msg->service_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001456 }
1457}
1458
Haggai Eran24cad9a2015-07-30 17:50:21 +03001459static u16 cm_get_bth_pkey(struct cm_work *work)
1460{
1461 struct ib_device *ib_dev = work->port->cm_dev->ib_device;
1462 u8 port_num = work->port->port_num;
1463 u16 pkey_index = work->mad_recv_wc->wc->pkey_index;
1464 u16 pkey;
1465 int ret;
1466
1467 ret = ib_get_cached_pkey(ib_dev, port_num, pkey_index, &pkey);
1468 if (ret) {
1469 dev_warn_ratelimited(&ib_dev->dev, "ib_cm: Couldn't retrieve pkey for incoming request (port %d, pkey index %d). %d\n",
1470 port_num, pkey_index, ret);
1471 return 0;
1472 }
1473
1474 return pkey;
1475}
1476
Hal Rosenstocka9770492005-07-27 11:45:40 -07001477static void cm_format_req_event(struct cm_work *work,
1478 struct cm_id_private *cm_id_priv,
1479 struct ib_cm_id *listen_id)
1480{
1481 struct cm_req_msg *req_msg;
1482 struct ib_cm_req_event_param *param;
1483
1484 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1485 param = &work->cm_event.param.req_rcvd;
1486 param->listen_id = listen_id;
Haggai Eran24cad9a2015-07-30 17:50:21 +03001487 param->bth_pkey = cm_get_bth_pkey(work);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001488 param->port = cm_id_priv->av.port->port_num;
1489 param->primary_path = &work->path[0];
1490 if (req_msg->alt_local_lid)
1491 param->alternate_path = &work->path[1];
1492 else
1493 param->alternate_path = NULL;
1494 param->remote_ca_guid = req_msg->local_ca_guid;
1495 param->remote_qkey = be32_to_cpu(req_msg->local_qkey);
1496 param->remote_qpn = be32_to_cpu(cm_req_get_local_qpn(req_msg));
1497 param->qp_type = cm_req_get_qp_type(req_msg);
1498 param->starting_psn = be32_to_cpu(cm_req_get_starting_psn(req_msg));
1499 param->responder_resources = cm_req_get_init_depth(req_msg);
1500 param->initiator_depth = cm_req_get_resp_res(req_msg);
1501 param->local_cm_response_timeout =
1502 cm_req_get_remote_resp_timeout(req_msg);
1503 param->flow_control = cm_req_get_flow_ctrl(req_msg);
1504 param->remote_cm_response_timeout =
1505 cm_req_get_local_resp_timeout(req_msg);
1506 param->retry_count = cm_req_get_retry_count(req_msg);
1507 param->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1508 param->srq = cm_req_get_srq(req_msg);
1509 work->cm_event.private_data = &req_msg->private_data;
1510}
1511
1512static void cm_process_work(struct cm_id_private *cm_id_priv,
1513 struct cm_work *work)
1514{
Hal Rosenstocka9770492005-07-27 11:45:40 -07001515 int ret;
1516
1517 /* We will typically only have the current event to report. */
1518 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
1519 cm_free_work(work);
1520
1521 while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) {
Sean Hefty24be6e82007-06-18 11:09:36 -07001522 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001523 work = cm_dequeue_work(cm_id_priv);
Sean Hefty24be6e82007-06-18 11:09:36 -07001524 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001525 BUG_ON(!work);
1526 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id,
1527 &work->cm_event);
1528 cm_free_work(work);
1529 }
1530 cm_deref_id(cm_id_priv);
1531 if (ret)
Michael S. Tsirkin04c33542006-07-14 00:23:52 -07001532 cm_destroy_id(&cm_id_priv->id, ret);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001533}
1534
1535static void cm_format_mra(struct cm_mra_msg *mra_msg,
1536 struct cm_id_private *cm_id_priv,
1537 enum cm_msg_response msg_mraed, u8 service_timeout,
1538 const void *private_data, u8 private_data_len)
1539{
1540 cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid);
1541 cm_mra_set_msg_mraed(mra_msg, msg_mraed);
1542 mra_msg->local_comm_id = cm_id_priv->id.local_id;
1543 mra_msg->remote_comm_id = cm_id_priv->id.remote_id;
1544 cm_mra_set_service_timeout(mra_msg, service_timeout);
1545
1546 if (private_data && private_data_len)
1547 memcpy(mra_msg->private_data, private_data, private_data_len);
1548}
1549
1550static void cm_format_rej(struct cm_rej_msg *rej_msg,
1551 struct cm_id_private *cm_id_priv,
1552 enum ib_cm_rej_reason reason,
1553 void *ari,
1554 u8 ari_length,
1555 const void *private_data,
1556 u8 private_data_len)
1557{
1558 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid);
1559 rej_msg->remote_comm_id = cm_id_priv->id.remote_id;
1560
1561 switch(cm_id_priv->id.state) {
1562 case IB_CM_REQ_RCVD:
1563 rej_msg->local_comm_id = 0;
1564 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1565 break;
1566 case IB_CM_MRA_REQ_SENT:
1567 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1568 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1569 break;
1570 case IB_CM_REP_RCVD:
1571 case IB_CM_MRA_REP_SENT:
1572 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1573 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REP);
1574 break;
1575 default:
1576 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1577 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_OTHER);
1578 break;
1579 }
1580
Sean Hefty97f52eb2005-08-13 21:05:57 -07001581 rej_msg->reason = cpu_to_be16(reason);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001582 if (ari && ari_length) {
1583 cm_rej_set_reject_info_len(rej_msg, ari_length);
1584 memcpy(rej_msg->ari, ari, ari_length);
1585 }
1586
1587 if (private_data && private_data_len)
1588 memcpy(rej_msg->private_data, private_data, private_data_len);
1589}
1590
1591static void cm_dup_req_handler(struct cm_work *work,
1592 struct cm_id_private *cm_id_priv)
1593{
1594 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001595 int ret;
1596
Sean Hefty9af57b72007-07-16 21:49:35 -07001597 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1598 counter[CM_REQ_COUNTER]);
1599
Hal Rosenstocka9770492005-07-27 11:45:40 -07001600 /* Quick state check to discard duplicate REQs. */
1601 if (cm_id_priv->id.state == IB_CM_REQ_RCVD)
1602 return;
1603
1604 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1605 if (ret)
1606 return;
1607
Sean Hefty24be6e82007-06-18 11:09:36 -07001608 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001609 switch (cm_id_priv->id.state) {
1610 case IB_CM_MRA_REQ_SENT:
1611 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1612 CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout,
1613 cm_id_priv->private_data,
1614 cm_id_priv->private_data_len);
1615 break;
1616 case IB_CM_TIMEWAIT:
1617 cm_format_rej((struct cm_rej_msg *) msg->mad, cm_id_priv,
1618 IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0);
1619 break;
1620 default:
1621 goto unlock;
1622 }
Sean Hefty24be6e82007-06-18 11:09:36 -07001623 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001624
Sean Hefty34816ad2005-10-25 10:51:39 -07001625 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001626 if (ret)
1627 goto free;
1628 return;
1629
Sean Hefty24be6e82007-06-18 11:09:36 -07001630unlock: spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001631free: cm_free_msg(msg);
1632}
1633
1634static struct cm_id_private * cm_match_req(struct cm_work *work,
1635 struct cm_id_private *cm_id_priv)
1636{
1637 struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv;
1638 struct cm_timewait_info *timewait_info;
1639 struct cm_req_msg *req_msg;
Hans Westgaard Ry9315bc92016-10-28 13:14:29 +02001640 struct ib_cm_id *cm_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001641
1642 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1643
Sean Heftyd998ccc2007-05-21 17:38:02 -07001644 /* Check for possible duplicate REQ. */
Sean Hefty24be6e82007-06-18 11:09:36 -07001645 spin_lock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001646 timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001647 if (timewait_info) {
1648 cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
1649 timewait_info->work.remote_id);
Sean Hefty24be6e82007-06-18 11:09:36 -07001650 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001651 if (cur_cm_id_priv) {
1652 cm_dup_req_handler(work, cur_cm_id_priv);
1653 cm_deref_id(cur_cm_id_priv);
Sean Heftyd998ccc2007-05-21 17:38:02 -07001654 }
1655 return NULL;
1656 }
1657
1658 /* Check for stale connections. */
1659 timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
1660 if (timewait_info) {
1661 cm_cleanup_timewait(cm_id_priv->timewait_info);
Hans Westgaard Ry9315bc92016-10-28 13:14:29 +02001662 cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
1663 timewait_info->work.remote_id);
1664
Sean Hefty24be6e82007-06-18 11:09:36 -07001665 spin_unlock_irq(&cm.lock);
Sean Heftyd998ccc2007-05-21 17:38:02 -07001666 cm_issue_rej(work->port, work->mad_recv_wc,
1667 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ,
1668 NULL, 0);
Hans Westgaard Ry9315bc92016-10-28 13:14:29 +02001669 if (cur_cm_id_priv) {
1670 cm_id = &cur_cm_id_priv->id;
1671 ib_send_cm_dreq(cm_id, NULL, 0);
1672 cm_deref_id(cur_cm_id_priv);
1673 }
Sean Heftyd998ccc2007-05-21 17:38:02 -07001674 return NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001675 }
1676
1677 /* Find matching listen request. */
Sean Hefty07d357d2005-10-17 15:37:43 -07001678 listen_cm_id_priv = cm_find_listen(cm_id_priv->id.device,
Haggai Eran73fec7f2015-07-30 17:50:26 +03001679 req_msg->service_id);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001680 if (!listen_cm_id_priv) {
Sean Hefty85753292006-10-04 11:29:59 -07001681 cm_cleanup_timewait(cm_id_priv->timewait_info);
Sean Hefty24be6e82007-06-18 11:09:36 -07001682 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001683 cm_issue_rej(work->port, work->mad_recv_wc,
1684 IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ,
1685 NULL, 0);
Sean Hefty85753292006-10-04 11:29:59 -07001686 goto out;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001687 }
1688 atomic_inc(&listen_cm_id_priv->refcount);
1689 atomic_inc(&cm_id_priv->refcount);
1690 cm_id_priv->id.state = IB_CM_REQ_RCVD;
1691 atomic_inc(&cm_id_priv->work_count);
Sean Hefty24be6e82007-06-18 11:09:36 -07001692 spin_unlock_irq(&cm.lock);
Sean Hefty85753292006-10-04 11:29:59 -07001693out:
Hal Rosenstocka9770492005-07-27 11:45:40 -07001694 return listen_cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001695}
1696
Sean Hefty3971c9f2007-12-10 15:53:25 -08001697/*
1698 * Work-around for inter-subnet connections. If the LIDs are permissive,
1699 * we need to override the LID/SL data in the REQ with the LID information
1700 * in the work completion.
1701 */
1702static void cm_process_routed_req(struct cm_req_msg *req_msg, struct ib_wc *wc)
1703{
1704 if (!cm_req_get_primary_subnet_local(req_msg)) {
1705 if (req_msg->primary_local_lid == IB_LID_PERMISSIVE) {
1706 req_msg->primary_local_lid = cpu_to_be16(wc->slid);
1707 cm_req_set_primary_sl(req_msg, wc->sl);
1708 }
1709
1710 if (req_msg->primary_remote_lid == IB_LID_PERMISSIVE)
1711 req_msg->primary_remote_lid = cpu_to_be16(wc->dlid_path_bits);
1712 }
1713
1714 if (!cm_req_get_alt_subnet_local(req_msg)) {
1715 if (req_msg->alt_local_lid == IB_LID_PERMISSIVE) {
1716 req_msg->alt_local_lid = cpu_to_be16(wc->slid);
1717 cm_req_set_alt_sl(req_msg, wc->sl);
1718 }
1719
1720 if (req_msg->alt_remote_lid == IB_LID_PERMISSIVE)
1721 req_msg->alt_remote_lid = cpu_to_be16(wc->dlid_path_bits);
1722 }
1723}
1724
Hal Rosenstocka9770492005-07-27 11:45:40 -07001725static int cm_req_handler(struct cm_work *work)
1726{
1727 struct ib_cm_id *cm_id;
1728 struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
1729 struct cm_req_msg *req_msg;
Matan Barakcb57bb82015-12-23 14:56:48 +02001730 union ib_gid gid;
1731 struct ib_gid_attr gid_attr;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04001732 const struct ib_global_route *grh;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001733 int ret;
1734
1735 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1736
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03001737 cm_id = ib_create_cm_id(work->port->cm_dev->ib_device, NULL, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001738 if (IS_ERR(cm_id))
1739 return PTR_ERR(cm_id);
1740
1741 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1742 cm_id_priv->id.remote_id = req_msg->local_comm_id;
1743 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
Sean Heftyca222c62006-06-17 20:37:40 -07001744 work->mad_recv_wc->recv_buf.grh,
Hal Rosenstocka9770492005-07-27 11:45:40 -07001745 &cm_id_priv->av);
1746 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1747 id.local_id);
1748 if (IS_ERR(cm_id_priv->timewait_info)) {
1749 ret = PTR_ERR(cm_id_priv->timewait_info);
Sean Hefty76842402006-08-28 11:57:42 -07001750 goto destroy;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001751 }
1752 cm_id_priv->timewait_info->work.remote_id = req_msg->local_comm_id;
1753 cm_id_priv->timewait_info->remote_ca_guid = req_msg->local_ca_guid;
1754 cm_id_priv->timewait_info->remote_qpn = cm_req_get_local_qpn(req_msg);
1755
1756 listen_cm_id_priv = cm_match_req(work, cm_id_priv);
1757 if (!listen_cm_id_priv) {
1758 ret = -EINVAL;
Sean Hefty76842402006-08-28 11:57:42 -07001759 kfree(cm_id_priv->timewait_info);
1760 goto destroy;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001761 }
1762
1763 cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
1764 cm_id_priv->id.context = listen_cm_id_priv->id.context;
1765 cm_id_priv->id.service_id = req_msg->service_id;
Harvey Harrison9c3da092009-01-17 17:11:57 -08001766 cm_id_priv->id.service_mask = ~cpu_to_be64(0);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001767
Sean Hefty3971c9f2007-12-10 15:53:25 -08001768 cm_process_routed_req(req_msg, work->mad_recv_wc->wc);
Matan Barakdd5f03b2013-12-12 18:03:11 +02001769
Dasaratharaman Chandramouli9fdca4d2017-04-27 19:06:00 -04001770 memset(&work->path[0], 0, sizeof(work->path[0]));
1771 memset(&work->path[1], 0, sizeof(work->path[1]));
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04001772 grh = rdma_ah_read_grh(&cm_id_priv->av.ah_attr);
Matan Barakcb57bb82015-12-23 14:56:48 +02001773 ret = ib_get_cached_gid(work->port->cm_dev->ib_device,
1774 work->port->port_num,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04001775 grh->sgid_index,
Matan Barakcb57bb82015-12-23 14:56:48 +02001776 &gid, &gid_attr);
1777 if (!ret) {
Matan Barak20029832015-12-23 14:56:53 +02001778 if (gid_attr.ndev) {
Dasaratharaman Chandramoulidfa834e12017-04-27 19:05:59 -04001779 work->path[0].rec_type =
1780 sa_conv_gid_to_pathrec_type(gid_attr.gid_type);
Dasaratharaman Chandramouli9fdca4d2017-04-27 19:06:00 -04001781 sa_path_set_ifindex(&work->path[0],
1782 gid_attr.ndev->ifindex);
1783 sa_path_set_ndev(&work->path[0],
1784 dev_net(gid_attr.ndev));
1785 dev_put(gid_attr.ndev);
Dasaratharaman Chandramoulidfa834e12017-04-27 19:05:59 -04001786 } else {
1787 work->path[0].rec_type = SA_PATH_REC_TYPE_IB;
Matan Barak20029832015-12-23 14:56:53 +02001788 }
Dasaratharaman Chandramouli9fdca4d2017-04-27 19:06:00 -04001789 if (req_msg->alt_local_lid)
1790 work->path[1].rec_type = work->path[0].rec_type;
1791 cm_format_paths_from_req(req_msg, &work->path[0],
1792 &work->path[1]);
1793 if (cm_id_priv->av.ah_attr.type == RDMA_AH_ATTR_TYPE_ROCE)
1794 sa_path_set_dmac(&work->path[0],
1795 cm_id_priv->av.ah_attr.roce.dmac);
1796 work->path[0].hop_limit = grh->hop_limit;
Mark Bloch9db0ff52016-10-27 16:36:27 +03001797 ret = cm_init_av_by_path(&work->path[0], &cm_id_priv->av,
1798 cm_id_priv);
Matan Barakcb57bb82015-12-23 14:56:48 +02001799 }
Sean Hefty76842402006-08-28 11:57:42 -07001800 if (ret) {
Matan Barakcb57bb82015-12-23 14:56:48 +02001801 int err = ib_get_cached_gid(work->port->cm_dev->ib_device,
1802 work->port->port_num, 0,
1803 &work->path[0].sgid,
1804 &gid_attr);
Matan Barak20029832015-12-23 14:56:53 +02001805 if (!err && gid_attr.ndev) {
Dasaratharaman Chandramoulidfa834e12017-04-27 19:05:59 -04001806 work->path[0].rec_type =
1807 sa_conv_gid_to_pathrec_type(gid_attr.gid_type);
Dasaratharaman Chandramouli9fdca4d2017-04-27 19:06:00 -04001808 sa_path_set_ifindex(&work->path[0],
1809 gid_attr.ndev->ifindex);
1810 sa_path_set_ndev(&work->path[0],
1811 dev_net(gid_attr.ndev));
1812 dev_put(gid_attr.ndev);
Dasaratharaman Chandramoulidfa834e12017-04-27 19:05:59 -04001813 } else {
1814 work->path[0].rec_type = SA_PATH_REC_TYPE_IB;
Matan Barak20029832015-12-23 14:56:53 +02001815 }
Dasaratharaman Chandramoulidfa834e12017-04-27 19:05:59 -04001816 if (req_msg->alt_local_lid)
1817 work->path[1].rec_type = work->path[0].rec_type;
Sean Hefty76842402006-08-28 11:57:42 -07001818 ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_GID,
1819 &work->path[0].sgid, sizeof work->path[0].sgid,
1820 NULL, 0);
1821 goto rejected;
1822 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07001823 if (req_msg->alt_local_lid) {
Mark Bloch9db0ff52016-10-27 16:36:27 +03001824 ret = cm_init_av_by_path(&work->path[1], &cm_id_priv->alt_av,
1825 cm_id_priv);
Sean Hefty76842402006-08-28 11:57:42 -07001826 if (ret) {
1827 ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_ALT_GID,
1828 &work->path[0].sgid,
1829 sizeof work->path[0].sgid, NULL, 0);
1830 goto rejected;
1831 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07001832 }
1833 cm_id_priv->tid = req_msg->hdr.tid;
1834 cm_id_priv->timeout_ms = cm_convert_to_ms(
1835 cm_req_get_local_resp_timeout(req_msg));
1836 cm_id_priv->max_cm_retries = cm_req_get_max_cm_retries(req_msg);
1837 cm_id_priv->remote_qpn = cm_req_get_local_qpn(req_msg);
1838 cm_id_priv->initiator_depth = cm_req_get_resp_res(req_msg);
1839 cm_id_priv->responder_resources = cm_req_get_init_depth(req_msg);
1840 cm_id_priv->path_mtu = cm_req_get_path_mtu(req_msg);
Sean Heftye1444b52006-11-28 14:57:13 -08001841 cm_id_priv->pkey = req_msg->pkey;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001842 cm_id_priv->sq_psn = cm_req_get_starting_psn(req_msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001843 cm_id_priv->retry_count = cm_req_get_retry_count(req_msg);
1844 cm_id_priv->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
Sean Heftyae7971a2005-10-24 12:33:56 -07001845 cm_id_priv->qp_type = cm_req_get_qp_type(req_msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001846
1847 cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
1848 cm_process_work(cm_id_priv, work);
1849 cm_deref_id(listen_cm_id_priv);
1850 return 0;
1851
Sean Hefty76842402006-08-28 11:57:42 -07001852rejected:
1853 atomic_dec(&cm_id_priv->refcount);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001854 cm_deref_id(listen_cm_id_priv);
Sean Hefty76842402006-08-28 11:57:42 -07001855destroy:
1856 ib_destroy_cm_id(cm_id);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001857 return ret;
1858}
1859
1860static void cm_format_rep(struct cm_rep_msg *rep_msg,
1861 struct cm_id_private *cm_id_priv,
1862 struct ib_cm_rep_param *param)
1863{
1864 cm_format_mad_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid);
1865 rep_msg->local_comm_id = cm_id_priv->id.local_id;
1866 rep_msg->remote_comm_id = cm_id_priv->id.remote_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001867 cm_rep_set_starting_psn(rep_msg, cpu_to_be32(param->starting_psn));
1868 rep_msg->resp_resources = param->responder_resources;
Sean Hefty1d846122007-06-18 11:09:37 -07001869 cm_rep_set_target_ack_delay(rep_msg,
1870 cm_id_priv->av.port->cm_dev->ack_delay);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001871 cm_rep_set_failover(rep_msg, param->failover_accepted);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001872 cm_rep_set_rnr_retry_count(rep_msg, param->rnr_retry_count);
Sean Heftye971b8c2007-02-22 11:37:38 -08001873 rep_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001874
Sean Heftyd26a3602011-05-13 10:46:20 -07001875 if (cm_id_priv->qp_type != IB_QPT_XRC_TGT) {
1876 rep_msg->initiator_depth = param->initiator_depth;
1877 cm_rep_set_flow_ctrl(rep_msg, param->flow_control);
1878 cm_rep_set_srq(rep_msg, param->srq);
Sean Heftyef700442011-08-02 11:08:22 -07001879 cm_rep_set_local_qpn(rep_msg, cpu_to_be32(param->qp_num));
Sean Heftyd26a3602011-05-13 10:46:20 -07001880 } else {
1881 cm_rep_set_srq(rep_msg, 1);
Sean Heftyef700442011-08-02 11:08:22 -07001882 cm_rep_set_local_eecn(rep_msg, cpu_to_be32(param->qp_num));
Sean Heftyd26a3602011-05-13 10:46:20 -07001883 }
1884
Hal Rosenstocka9770492005-07-27 11:45:40 -07001885 if (param->private_data && param->private_data_len)
1886 memcpy(rep_msg->private_data, param->private_data,
1887 param->private_data_len);
1888}
1889
1890int ib_send_cm_rep(struct ib_cm_id *cm_id,
1891 struct ib_cm_rep_param *param)
1892{
1893 struct cm_id_private *cm_id_priv;
1894 struct ib_mad_send_buf *msg;
1895 struct cm_rep_msg *rep_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001896 unsigned long flags;
1897 int ret;
1898
1899 if (param->private_data &&
1900 param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE)
1901 return -EINVAL;
1902
1903 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1904 spin_lock_irqsave(&cm_id_priv->lock, flags);
1905 if (cm_id->state != IB_CM_REQ_RCVD &&
1906 cm_id->state != IB_CM_MRA_REQ_SENT) {
1907 ret = -EINVAL;
1908 goto out;
1909 }
1910
1911 ret = cm_alloc_msg(cm_id_priv, &msg);
1912 if (ret)
1913 goto out;
1914
1915 rep_msg = (struct cm_rep_msg *) msg->mad;
1916 cm_format_rep(rep_msg, cm_id_priv, param);
Sean Hefty34816ad2005-10-25 10:51:39 -07001917 msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001918 msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT;
1919
Sean Hefty34816ad2005-10-25 10:51:39 -07001920 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001921 if (ret) {
1922 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1923 cm_free_msg(msg);
1924 return ret;
1925 }
1926
1927 cm_id->state = IB_CM_REP_SENT;
1928 cm_id_priv->msg = msg;
1929 cm_id_priv->initiator_depth = param->initiator_depth;
1930 cm_id_priv->responder_resources = param->responder_resources;
1931 cm_id_priv->rq_psn = cm_rep_get_starting_psn(rep_msg);
Sean Heftyef700442011-08-02 11:08:22 -07001932 cm_id_priv->local_qpn = cpu_to_be32(param->qp_num & 0xFFFFFF);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001933
1934out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1935 return ret;
1936}
1937EXPORT_SYMBOL(ib_send_cm_rep);
1938
1939static void cm_format_rtu(struct cm_rtu_msg *rtu_msg,
1940 struct cm_id_private *cm_id_priv,
1941 const void *private_data,
1942 u8 private_data_len)
1943{
1944 cm_format_mad_hdr(&rtu_msg->hdr, CM_RTU_ATTR_ID, cm_id_priv->tid);
1945 rtu_msg->local_comm_id = cm_id_priv->id.local_id;
1946 rtu_msg->remote_comm_id = cm_id_priv->id.remote_id;
1947
1948 if (private_data && private_data_len)
1949 memcpy(rtu_msg->private_data, private_data, private_data_len);
1950}
1951
1952int ib_send_cm_rtu(struct ib_cm_id *cm_id,
1953 const void *private_data,
1954 u8 private_data_len)
1955{
1956 struct cm_id_private *cm_id_priv;
1957 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001958 unsigned long flags;
1959 void *data;
1960 int ret;
1961
1962 if (private_data && private_data_len > IB_CM_RTU_PRIVATE_DATA_SIZE)
1963 return -EINVAL;
1964
1965 data = cm_copy_private_data(private_data, private_data_len);
1966 if (IS_ERR(data))
1967 return PTR_ERR(data);
1968
1969 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1970 spin_lock_irqsave(&cm_id_priv->lock, flags);
1971 if (cm_id->state != IB_CM_REP_RCVD &&
1972 cm_id->state != IB_CM_MRA_REP_SENT) {
1973 ret = -EINVAL;
1974 goto error;
1975 }
1976
1977 ret = cm_alloc_msg(cm_id_priv, &msg);
1978 if (ret)
1979 goto error;
1980
1981 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1982 private_data, private_data_len);
1983
Sean Hefty34816ad2005-10-25 10:51:39 -07001984 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001985 if (ret) {
1986 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1987 cm_free_msg(msg);
1988 kfree(data);
1989 return ret;
1990 }
1991
1992 cm_id->state = IB_CM_ESTABLISHED;
1993 cm_set_private_data(cm_id_priv, data, private_data_len);
1994 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1995 return 0;
1996
1997error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1998 kfree(data);
1999 return ret;
2000}
2001EXPORT_SYMBOL(ib_send_cm_rtu);
2002
Sean Heftyef700442011-08-02 11:08:22 -07002003static void cm_format_rep_event(struct cm_work *work, enum ib_qp_type qp_type)
Hal Rosenstocka9770492005-07-27 11:45:40 -07002004{
2005 struct cm_rep_msg *rep_msg;
2006 struct ib_cm_rep_event_param *param;
2007
2008 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
2009 param = &work->cm_event.param.rep_rcvd;
2010 param->remote_ca_guid = rep_msg->local_ca_guid;
2011 param->remote_qkey = be32_to_cpu(rep_msg->local_qkey);
Sean Heftyef700442011-08-02 11:08:22 -07002012 param->remote_qpn = be32_to_cpu(cm_rep_get_qpn(rep_msg, qp_type));
Hal Rosenstocka9770492005-07-27 11:45:40 -07002013 param->starting_psn = be32_to_cpu(cm_rep_get_starting_psn(rep_msg));
2014 param->responder_resources = rep_msg->initiator_depth;
2015 param->initiator_depth = rep_msg->resp_resources;
2016 param->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
2017 param->failover_accepted = cm_rep_get_failover(rep_msg);
2018 param->flow_control = cm_rep_get_flow_ctrl(rep_msg);
2019 param->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
2020 param->srq = cm_rep_get_srq(rep_msg);
2021 work->cm_event.private_data = &rep_msg->private_data;
2022}
2023
2024static void cm_dup_rep_handler(struct cm_work *work)
2025{
2026 struct cm_id_private *cm_id_priv;
2027 struct cm_rep_msg *rep_msg;
2028 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002029 int ret;
2030
2031 rep_msg = (struct cm_rep_msg *) work->mad_recv_wc->recv_buf.mad;
2032 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id,
2033 rep_msg->local_comm_id);
2034 if (!cm_id_priv)
2035 return;
2036
Sean Hefty9af57b72007-07-16 21:49:35 -07002037 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2038 counter[CM_REP_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002039 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
2040 if (ret)
2041 goto deref;
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_ESTABLISHED)
2045 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
2046 cm_id_priv->private_data,
2047 cm_id_priv->private_data_len);
2048 else if (cm_id_priv->id.state == IB_CM_MRA_REP_SENT)
2049 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2050 CM_MSG_RESPONSE_REP, cm_id_priv->service_timeout,
2051 cm_id_priv->private_data,
2052 cm_id_priv->private_data_len);
2053 else
2054 goto unlock;
Sean Hefty24be6e82007-06-18 11:09:36 -07002055 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002056
Sean Hefty34816ad2005-10-25 10:51:39 -07002057 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002058 if (ret)
2059 goto free;
2060 goto deref;
2061
Sean Hefty24be6e82007-06-18 11:09:36 -07002062unlock: spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002063free: cm_free_msg(msg);
2064deref: cm_deref_id(cm_id_priv);
2065}
2066
2067static int cm_rep_handler(struct cm_work *work)
2068{
2069 struct cm_id_private *cm_id_priv;
2070 struct cm_rep_msg *rep_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002071 int ret;
Hans Westgaard Ry9315bc92016-10-28 13:14:29 +02002072 struct cm_id_private *cur_cm_id_priv;
2073 struct ib_cm_id *cm_id;
2074 struct cm_timewait_info *timewait_info;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002075
2076 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
2077 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id, 0);
2078 if (!cm_id_priv) {
2079 cm_dup_rep_handler(work);
2080 return -EINVAL;
2081 }
2082
Sean Heftyef700442011-08-02 11:08:22 -07002083 cm_format_rep_event(work, cm_id_priv->qp_type);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002084
Sean Hefty24be6e82007-06-18 11:09:36 -07002085 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002086 switch (cm_id_priv->id.state) {
2087 case IB_CM_REQ_SENT:
2088 case IB_CM_MRA_REQ_RCVD:
2089 break;
2090 default:
Sean Hefty24be6e82007-06-18 11:09:36 -07002091 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002092 ret = -EINVAL;
2093 goto error;
2094 }
Sean Hefty87fd1a12006-03-02 16:50:37 -08002095
2096 cm_id_priv->timewait_info->work.remote_id = rep_msg->local_comm_id;
2097 cm_id_priv->timewait_info->remote_ca_guid = rep_msg->local_ca_guid;
Sean Heftyef700442011-08-02 11:08:22 -07002098 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 -08002099
2100 spin_lock(&cm.lock);
2101 /* Check for duplicate REP. */
2102 if (cm_insert_remote_id(cm_id_priv->timewait_info)) {
2103 spin_unlock(&cm.lock);
Sean Hefty24be6e82007-06-18 11:09:36 -07002104 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty87fd1a12006-03-02 16:50:37 -08002105 ret = -EINVAL;
2106 goto error;
2107 }
2108 /* Check for a stale connection. */
Hans Westgaard Ry9315bc92016-10-28 13:14:29 +02002109 timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
2110 if (timewait_info) {
Sean Hefty87fd1a12006-03-02 16:50:37 -08002111 rb_erase(&cm_id_priv->timewait_info->remote_id_node,
2112 &cm.remote_id_table);
2113 cm_id_priv->timewait_info->inserted_remote_id = 0;
Hans Westgaard Ry9315bc92016-10-28 13:14:29 +02002114 cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
2115 timewait_info->work.remote_id);
2116
Sean Hefty87fd1a12006-03-02 16:50:37 -08002117 spin_unlock(&cm.lock);
Sean Hefty24be6e82007-06-18 11:09:36 -07002118 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty87fd1a12006-03-02 16:50:37 -08002119 cm_issue_rej(work->port, work->mad_recv_wc,
2120 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP,
2121 NULL, 0);
2122 ret = -EINVAL;
Hans Westgaard Ry9315bc92016-10-28 13:14:29 +02002123 if (cur_cm_id_priv) {
2124 cm_id = &cur_cm_id_priv->id;
2125 ib_send_cm_dreq(cm_id, NULL, 0);
2126 cm_deref_id(cur_cm_id_priv);
2127 }
2128
Sean Hefty87fd1a12006-03-02 16:50:37 -08002129 goto error;
2130 }
2131 spin_unlock(&cm.lock);
2132
Hal Rosenstocka9770492005-07-27 11:45:40 -07002133 cm_id_priv->id.state = IB_CM_REP_RCVD;
2134 cm_id_priv->id.remote_id = rep_msg->local_comm_id;
Sean Heftyef700442011-08-02 11:08:22 -07002135 cm_id_priv->remote_qpn = cm_rep_get_qpn(rep_msg, cm_id_priv->qp_type);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002136 cm_id_priv->initiator_depth = rep_msg->resp_resources;
2137 cm_id_priv->responder_resources = rep_msg->initiator_depth;
2138 cm_id_priv->sq_psn = cm_rep_get_starting_psn(rep_msg);
2139 cm_id_priv->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
Sean Hefty1d846122007-06-18 11:09:37 -07002140 cm_id_priv->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
2141 cm_id_priv->av.timeout =
2142 cm_ack_timeout(cm_id_priv->target_ack_delay,
2143 cm_id_priv->av.timeout - 1);
2144 cm_id_priv->alt_av.timeout =
2145 cm_ack_timeout(cm_id_priv->target_ack_delay,
2146 cm_id_priv->alt_av.timeout - 1);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002147
2148 /* todo: handle peer_to_peer */
2149
Sean Hefty34816ad2005-10-25 10:51:39 -07002150 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002151 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2152 if (!ret)
2153 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002154 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002155
2156 if (ret)
2157 cm_process_work(cm_id_priv, work);
2158 else
2159 cm_deref_id(cm_id_priv);
2160 return 0;
2161
Sean Hefty87fd1a12006-03-02 16:50:37 -08002162error:
Hal Rosenstocka9770492005-07-27 11:45:40 -07002163 cm_deref_id(cm_id_priv);
2164 return ret;
2165}
2166
2167static int cm_establish_handler(struct cm_work *work)
2168{
2169 struct cm_id_private *cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002170 int ret;
2171
Sean Heftye1444b52006-11-28 14:57:13 -08002172 /* See comment in cm_establish about lookup. */
Hal Rosenstocka9770492005-07-27 11:45:40 -07002173 cm_id_priv = cm_acquire_id(work->local_id, work->remote_id);
2174 if (!cm_id_priv)
2175 return -EINVAL;
2176
Sean Hefty24be6e82007-06-18 11:09:36 -07002177 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002178 if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
Sean Hefty24be6e82007-06-18 11:09:36 -07002179 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002180 goto out;
2181 }
2182
Sean Hefty34816ad2005-10-25 10:51:39 -07002183 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002184 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2185 if (!ret)
2186 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002187 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002188
2189 if (ret)
2190 cm_process_work(cm_id_priv, work);
2191 else
2192 cm_deref_id(cm_id_priv);
2193 return 0;
2194out:
2195 cm_deref_id(cm_id_priv);
2196 return -EINVAL;
2197}
2198
2199static int cm_rtu_handler(struct cm_work *work)
2200{
2201 struct cm_id_private *cm_id_priv;
2202 struct cm_rtu_msg *rtu_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002203 int ret;
2204
2205 rtu_msg = (struct cm_rtu_msg *)work->mad_recv_wc->recv_buf.mad;
2206 cm_id_priv = cm_acquire_id(rtu_msg->remote_comm_id,
2207 rtu_msg->local_comm_id);
2208 if (!cm_id_priv)
2209 return -EINVAL;
2210
2211 work->cm_event.private_data = &rtu_msg->private_data;
2212
Sean Hefty24be6e82007-06-18 11:09:36 -07002213 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002214 if (cm_id_priv->id.state != IB_CM_REP_SENT &&
2215 cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) {
Sean Hefty24be6e82007-06-18 11:09:36 -07002216 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty9af57b72007-07-16 21:49:35 -07002217 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2218 counter[CM_RTU_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002219 goto out;
2220 }
2221 cm_id_priv->id.state = IB_CM_ESTABLISHED;
2222
Sean Hefty34816ad2005-10-25 10:51:39 -07002223 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002224 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2225 if (!ret)
2226 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002227 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002228
2229 if (ret)
2230 cm_process_work(cm_id_priv, work);
2231 else
2232 cm_deref_id(cm_id_priv);
2233 return 0;
2234out:
2235 cm_deref_id(cm_id_priv);
2236 return -EINVAL;
2237}
2238
2239static void cm_format_dreq(struct cm_dreq_msg *dreq_msg,
2240 struct cm_id_private *cm_id_priv,
2241 const void *private_data,
2242 u8 private_data_len)
2243{
2244 cm_format_mad_hdr(&dreq_msg->hdr, CM_DREQ_ATTR_ID,
2245 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_DREQ));
2246 dreq_msg->local_comm_id = cm_id_priv->id.local_id;
2247 dreq_msg->remote_comm_id = cm_id_priv->id.remote_id;
2248 cm_dreq_set_remote_qpn(dreq_msg, cm_id_priv->remote_qpn);
2249
2250 if (private_data && private_data_len)
2251 memcpy(dreq_msg->private_data, private_data, private_data_len);
2252}
2253
2254int ib_send_cm_dreq(struct ib_cm_id *cm_id,
2255 const void *private_data,
2256 u8 private_data_len)
2257{
2258 struct cm_id_private *cm_id_priv;
2259 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002260 unsigned long flags;
2261 int ret;
2262
2263 if (private_data && private_data_len > IB_CM_DREQ_PRIVATE_DATA_SIZE)
2264 return -EINVAL;
2265
2266 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2267 spin_lock_irqsave(&cm_id_priv->lock, flags);
2268 if (cm_id->state != IB_CM_ESTABLISHED) {
2269 ret = -EINVAL;
2270 goto out;
2271 }
2272
Sean Hefty8d8ac862011-03-03 23:31:06 +00002273 if (cm_id->lap_state == IB_CM_LAP_SENT ||
2274 cm_id->lap_state == IB_CM_MRA_LAP_RCVD)
2275 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2276
Hal Rosenstocka9770492005-07-27 11:45:40 -07002277 ret = cm_alloc_msg(cm_id_priv, &msg);
2278 if (ret) {
2279 cm_enter_timewait(cm_id_priv);
2280 goto out;
2281 }
2282
2283 cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv,
2284 private_data, private_data_len);
Sean Hefty34816ad2005-10-25 10:51:39 -07002285 msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002286 msg->context[1] = (void *) (unsigned long) IB_CM_DREQ_SENT;
2287
Sean Hefty34816ad2005-10-25 10:51:39 -07002288 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002289 if (ret) {
2290 cm_enter_timewait(cm_id_priv);
2291 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2292 cm_free_msg(msg);
2293 return ret;
2294 }
2295
2296 cm_id->state = IB_CM_DREQ_SENT;
2297 cm_id_priv->msg = msg;
2298out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2299 return ret;
2300}
2301EXPORT_SYMBOL(ib_send_cm_dreq);
2302
2303static void cm_format_drep(struct cm_drep_msg *drep_msg,
2304 struct cm_id_private *cm_id_priv,
2305 const void *private_data,
2306 u8 private_data_len)
2307{
2308 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, cm_id_priv->tid);
2309 drep_msg->local_comm_id = cm_id_priv->id.local_id;
2310 drep_msg->remote_comm_id = cm_id_priv->id.remote_id;
2311
2312 if (private_data && private_data_len)
2313 memcpy(drep_msg->private_data, private_data, private_data_len);
2314}
2315
2316int ib_send_cm_drep(struct ib_cm_id *cm_id,
2317 const void *private_data,
2318 u8 private_data_len)
2319{
2320 struct cm_id_private *cm_id_priv;
2321 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002322 unsigned long flags;
2323 void *data;
2324 int ret;
2325
2326 if (private_data && private_data_len > IB_CM_DREP_PRIVATE_DATA_SIZE)
2327 return -EINVAL;
2328
2329 data = cm_copy_private_data(private_data, private_data_len);
2330 if (IS_ERR(data))
2331 return PTR_ERR(data);
2332
2333 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2334 spin_lock_irqsave(&cm_id_priv->lock, flags);
2335 if (cm_id->state != IB_CM_DREQ_RCVD) {
2336 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2337 kfree(data);
2338 return -EINVAL;
2339 }
2340
2341 cm_set_private_data(cm_id_priv, data, private_data_len);
2342 cm_enter_timewait(cm_id_priv);
2343
2344 ret = cm_alloc_msg(cm_id_priv, &msg);
2345 if (ret)
2346 goto out;
2347
2348 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
2349 private_data, private_data_len);
2350
Sean Hefty34816ad2005-10-25 10:51:39 -07002351 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002352 if (ret) {
2353 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2354 cm_free_msg(msg);
2355 return ret;
2356 }
2357
2358out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2359 return ret;
2360}
2361EXPORT_SYMBOL(ib_send_cm_drep);
2362
Sean Hefty82a9c162006-10-04 11:37:25 -07002363static int cm_issue_drep(struct cm_port *port,
2364 struct ib_mad_recv_wc *mad_recv_wc)
2365{
2366 struct ib_mad_send_buf *msg = NULL;
2367 struct cm_dreq_msg *dreq_msg;
2368 struct cm_drep_msg *drep_msg;
2369 int ret;
2370
2371 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
2372 if (ret)
2373 return ret;
2374
2375 dreq_msg = (struct cm_dreq_msg *) mad_recv_wc->recv_buf.mad;
2376 drep_msg = (struct cm_drep_msg *) msg->mad;
2377
2378 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, dreq_msg->hdr.tid);
2379 drep_msg->remote_comm_id = dreq_msg->local_comm_id;
2380 drep_msg->local_comm_id = dreq_msg->remote_comm_id;
2381
2382 ret = ib_post_send_mad(msg, NULL);
2383 if (ret)
2384 cm_free_msg(msg);
2385
2386 return ret;
2387}
2388
Hal Rosenstocka9770492005-07-27 11:45:40 -07002389static int cm_dreq_handler(struct cm_work *work)
2390{
2391 struct cm_id_private *cm_id_priv;
2392 struct cm_dreq_msg *dreq_msg;
2393 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002394 int ret;
2395
2396 dreq_msg = (struct cm_dreq_msg *)work->mad_recv_wc->recv_buf.mad;
2397 cm_id_priv = cm_acquire_id(dreq_msg->remote_comm_id,
2398 dreq_msg->local_comm_id);
Sean Hefty82a9c162006-10-04 11:37:25 -07002399 if (!cm_id_priv) {
Sean Hefty9af57b72007-07-16 21:49:35 -07002400 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2401 counter[CM_DREQ_COUNTER]);
Sean Hefty82a9c162006-10-04 11:37:25 -07002402 cm_issue_drep(work->port, work->mad_recv_wc);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002403 return -EINVAL;
Sean Hefty82a9c162006-10-04 11:37:25 -07002404 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07002405
2406 work->cm_event.private_data = &dreq_msg->private_data;
2407
Sean Hefty24be6e82007-06-18 11:09:36 -07002408 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002409 if (cm_id_priv->local_qpn != cm_dreq_get_remote_qpn(dreq_msg))
2410 goto unlock;
2411
2412 switch (cm_id_priv->id.state) {
2413 case IB_CM_REP_SENT:
2414 case IB_CM_DREQ_SENT:
Sean Hefty34816ad2005-10-25 10:51:39 -07002415 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002416 break;
2417 case IB_CM_ESTABLISHED:
Sean Hefty8d8ac862011-03-03 23:31:06 +00002418 if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT ||
2419 cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
2420 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2421 break;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002422 case IB_CM_MRA_REP_RCVD:
2423 break;
2424 case IB_CM_TIMEWAIT:
Sean Hefty9af57b72007-07-16 21:49:35 -07002425 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2426 counter[CM_DREQ_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002427 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2428 goto unlock;
2429
2430 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
2431 cm_id_priv->private_data,
2432 cm_id_priv->private_data_len);
Sean Hefty24be6e82007-06-18 11:09:36 -07002433 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002434
Sean Hefty34816ad2005-10-25 10:51:39 -07002435 if (ib_post_send_mad(msg, NULL))
Hal Rosenstocka9770492005-07-27 11:45:40 -07002436 cm_free_msg(msg);
2437 goto deref;
Sean Hefty9af57b72007-07-16 21:49:35 -07002438 case IB_CM_DREQ_RCVD:
2439 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2440 counter[CM_DREQ_COUNTER]);
2441 goto unlock;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002442 default:
2443 goto unlock;
2444 }
2445 cm_id_priv->id.state = IB_CM_DREQ_RCVD;
2446 cm_id_priv->tid = dreq_msg->hdr.tid;
2447 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2448 if (!ret)
2449 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002450 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002451
2452 if (ret)
2453 cm_process_work(cm_id_priv, work);
2454 else
2455 cm_deref_id(cm_id_priv);
2456 return 0;
2457
Sean Hefty24be6e82007-06-18 11:09:36 -07002458unlock: spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002459deref: cm_deref_id(cm_id_priv);
2460 return -EINVAL;
2461}
2462
2463static int cm_drep_handler(struct cm_work *work)
2464{
2465 struct cm_id_private *cm_id_priv;
2466 struct cm_drep_msg *drep_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002467 int ret;
2468
2469 drep_msg = (struct cm_drep_msg *)work->mad_recv_wc->recv_buf.mad;
2470 cm_id_priv = cm_acquire_id(drep_msg->remote_comm_id,
2471 drep_msg->local_comm_id);
2472 if (!cm_id_priv)
2473 return -EINVAL;
2474
2475 work->cm_event.private_data = &drep_msg->private_data;
2476
Sean Hefty24be6e82007-06-18 11:09:36 -07002477 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002478 if (cm_id_priv->id.state != IB_CM_DREQ_SENT &&
2479 cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
Sean Hefty24be6e82007-06-18 11:09:36 -07002480 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002481 goto out;
2482 }
2483 cm_enter_timewait(cm_id_priv);
2484
Sean Hefty34816ad2005-10-25 10:51:39 -07002485 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002486 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2487 if (!ret)
2488 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002489 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002490
2491 if (ret)
2492 cm_process_work(cm_id_priv, work);
2493 else
2494 cm_deref_id(cm_id_priv);
2495 return 0;
2496out:
2497 cm_deref_id(cm_id_priv);
2498 return -EINVAL;
2499}
2500
2501int ib_send_cm_rej(struct ib_cm_id *cm_id,
2502 enum ib_cm_rej_reason reason,
2503 void *ari,
2504 u8 ari_length,
2505 const void *private_data,
2506 u8 private_data_len)
2507{
2508 struct cm_id_private *cm_id_priv;
2509 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002510 unsigned long flags;
2511 int ret;
2512
2513 if ((private_data && private_data_len > IB_CM_REJ_PRIVATE_DATA_SIZE) ||
2514 (ari && ari_length > IB_CM_REJ_ARI_LENGTH))
2515 return -EINVAL;
2516
2517 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2518
2519 spin_lock_irqsave(&cm_id_priv->lock, flags);
2520 switch (cm_id->state) {
2521 case IB_CM_REQ_SENT:
2522 case IB_CM_MRA_REQ_RCVD:
2523 case IB_CM_REQ_RCVD:
2524 case IB_CM_MRA_REQ_SENT:
2525 case IB_CM_REP_RCVD:
2526 case IB_CM_MRA_REP_SENT:
2527 ret = cm_alloc_msg(cm_id_priv, &msg);
2528 if (!ret)
2529 cm_format_rej((struct cm_rej_msg *) msg->mad,
2530 cm_id_priv, reason, ari, ari_length,
2531 private_data, private_data_len);
2532
2533 cm_reset_to_idle(cm_id_priv);
2534 break;
2535 case IB_CM_REP_SENT:
2536 case IB_CM_MRA_REP_RCVD:
2537 ret = cm_alloc_msg(cm_id_priv, &msg);
2538 if (!ret)
2539 cm_format_rej((struct cm_rej_msg *) msg->mad,
2540 cm_id_priv, reason, ari, ari_length,
2541 private_data, private_data_len);
2542
2543 cm_enter_timewait(cm_id_priv);
2544 break;
2545 default:
2546 ret = -EINVAL;
2547 goto out;
2548 }
2549
2550 if (ret)
2551 goto out;
2552
Sean Hefty34816ad2005-10-25 10:51:39 -07002553 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002554 if (ret)
2555 cm_free_msg(msg);
2556
2557out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2558 return ret;
2559}
2560EXPORT_SYMBOL(ib_send_cm_rej);
2561
2562static void cm_format_rej_event(struct cm_work *work)
2563{
2564 struct cm_rej_msg *rej_msg;
2565 struct ib_cm_rej_event_param *param;
2566
2567 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2568 param = &work->cm_event.param.rej_rcvd;
2569 param->ari = rej_msg->ari;
2570 param->ari_length = cm_rej_get_reject_info_len(rej_msg);
Sean Hefty97f52eb2005-08-13 21:05:57 -07002571 param->reason = __be16_to_cpu(rej_msg->reason);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002572 work->cm_event.private_data = &rej_msg->private_data;
2573}
2574
2575static struct cm_id_private * cm_acquire_rejected_id(struct cm_rej_msg *rej_msg)
2576{
2577 struct cm_timewait_info *timewait_info;
2578 struct cm_id_private *cm_id_priv;
Sean Hefty97f52eb2005-08-13 21:05:57 -07002579 __be32 remote_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002580
2581 remote_id = rej_msg->local_comm_id;
2582
Sean Hefty97f52eb2005-08-13 21:05:57 -07002583 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_TIMEOUT) {
Sean Hefty24be6e82007-06-18 11:09:36 -07002584 spin_lock_irq(&cm.lock);
Sean Hefty97f52eb2005-08-13 21:05:57 -07002585 timewait_info = cm_find_remote_id( *((__be64 *) rej_msg->ari),
Hal Rosenstocka9770492005-07-27 11:45:40 -07002586 remote_id);
2587 if (!timewait_info) {
Sean Hefty24be6e82007-06-18 11:09:36 -07002588 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002589 return NULL;
2590 }
Sean Heftyf06d2652006-08-28 15:15:18 -07002591 cm_id_priv = idr_find(&cm.local_id_table, (__force int)
2592 (timewait_info->work.local_id ^
2593 cm.random_id_operand));
Hal Rosenstocka9770492005-07-27 11:45:40 -07002594 if (cm_id_priv) {
2595 if (cm_id_priv->id.remote_id == remote_id)
2596 atomic_inc(&cm_id_priv->refcount);
2597 else
2598 cm_id_priv = NULL;
2599 }
Sean Hefty24be6e82007-06-18 11:09:36 -07002600 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002601 } else if (cm_rej_get_msg_rejected(rej_msg) == CM_MSG_RESPONSE_REQ)
2602 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, 0);
2603 else
2604 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, remote_id);
2605
2606 return cm_id_priv;
2607}
2608
2609static int cm_rej_handler(struct cm_work *work)
2610{
2611 struct cm_id_private *cm_id_priv;
2612 struct cm_rej_msg *rej_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002613 int ret;
2614
2615 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2616 cm_id_priv = cm_acquire_rejected_id(rej_msg);
2617 if (!cm_id_priv)
2618 return -EINVAL;
2619
2620 cm_format_rej_event(work);
2621
Sean Hefty24be6e82007-06-18 11:09:36 -07002622 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002623 switch (cm_id_priv->id.state) {
2624 case IB_CM_REQ_SENT:
2625 case IB_CM_MRA_REQ_RCVD:
2626 case IB_CM_REP_SENT:
2627 case IB_CM_MRA_REP_RCVD:
Sean Hefty34816ad2005-10-25 10:51:39 -07002628 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002629 /* fall through */
2630 case IB_CM_REQ_RCVD:
2631 case IB_CM_MRA_REQ_SENT:
Sean Hefty97f52eb2005-08-13 21:05:57 -07002632 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_STALE_CONN)
Hal Rosenstocka9770492005-07-27 11:45:40 -07002633 cm_enter_timewait(cm_id_priv);
2634 else
2635 cm_reset_to_idle(cm_id_priv);
2636 break;
2637 case IB_CM_DREQ_SENT:
Sean Hefty34816ad2005-10-25 10:51:39 -07002638 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002639 /* fall through */
2640 case IB_CM_REP_RCVD:
2641 case IB_CM_MRA_REP_SENT:
Hal Rosenstocka9770492005-07-27 11:45:40 -07002642 cm_enter_timewait(cm_id_priv);
2643 break;
Sean Hefty8d8ac862011-03-03 23:31:06 +00002644 case IB_CM_ESTABLISHED:
2645 if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT ||
2646 cm_id_priv->id.lap_state == IB_CM_LAP_SENT) {
2647 if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT)
2648 ib_cancel_mad(cm_id_priv->av.port->mad_agent,
2649 cm_id_priv->msg);
2650 cm_enter_timewait(cm_id_priv);
2651 break;
2652 }
2653 /* fall through */
Hal Rosenstocka9770492005-07-27 11:45:40 -07002654 default:
Sean Hefty24be6e82007-06-18 11:09:36 -07002655 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002656 ret = -EINVAL;
2657 goto out;
2658 }
2659
2660 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2661 if (!ret)
2662 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002663 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002664
2665 if (ret)
2666 cm_process_work(cm_id_priv, work);
2667 else
2668 cm_deref_id(cm_id_priv);
2669 return 0;
2670out:
2671 cm_deref_id(cm_id_priv);
2672 return -EINVAL;
2673}
2674
2675int ib_send_cm_mra(struct ib_cm_id *cm_id,
2676 u8 service_timeout,
2677 const void *private_data,
2678 u8 private_data_len)
2679{
2680 struct cm_id_private *cm_id_priv;
2681 struct ib_mad_send_buf *msg;
Sean Heftyde98b692007-08-01 13:49:53 -07002682 enum ib_cm_state cm_state;
2683 enum ib_cm_lap_state lap_state;
2684 enum cm_msg_response msg_response;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002685 void *data;
2686 unsigned long flags;
2687 int ret;
2688
2689 if (private_data && private_data_len > IB_CM_MRA_PRIVATE_DATA_SIZE)
2690 return -EINVAL;
2691
2692 data = cm_copy_private_data(private_data, private_data_len);
2693 if (IS_ERR(data))
2694 return PTR_ERR(data);
2695
2696 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2697
2698 spin_lock_irqsave(&cm_id_priv->lock, flags);
2699 switch(cm_id_priv->id.state) {
2700 case IB_CM_REQ_RCVD:
Sean Heftyde98b692007-08-01 13:49:53 -07002701 cm_state = IB_CM_MRA_REQ_SENT;
2702 lap_state = cm_id->lap_state;
2703 msg_response = CM_MSG_RESPONSE_REQ;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002704 break;
2705 case IB_CM_REP_RCVD:
Sean Heftyde98b692007-08-01 13:49:53 -07002706 cm_state = IB_CM_MRA_REP_SENT;
2707 lap_state = cm_id->lap_state;
2708 msg_response = CM_MSG_RESPONSE_REP;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002709 break;
2710 case IB_CM_ESTABLISHED:
Sean Hefty50a025c2010-07-21 23:36:52 +00002711 if (cm_id->lap_state == IB_CM_LAP_RCVD) {
2712 cm_state = cm_id->state;
2713 lap_state = IB_CM_MRA_LAP_SENT;
2714 msg_response = CM_MSG_RESPONSE_OTHER;
2715 break;
2716 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07002717 default:
2718 ret = -EINVAL;
2719 goto error1;
2720 }
Sean Heftyde98b692007-08-01 13:49:53 -07002721
2722 if (!(service_timeout & IB_CM_MRA_FLAG_DELAY)) {
2723 ret = cm_alloc_msg(cm_id_priv, &msg);
2724 if (ret)
2725 goto error1;
2726
2727 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2728 msg_response, service_timeout,
2729 private_data, private_data_len);
2730 ret = ib_post_send_mad(msg, NULL);
2731 if (ret)
2732 goto error2;
2733 }
2734
2735 cm_id->state = cm_state;
2736 cm_id->lap_state = lap_state;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002737 cm_id_priv->service_timeout = service_timeout;
2738 cm_set_private_data(cm_id_priv, data, private_data_len);
2739 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2740 return 0;
2741
2742error1: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2743 kfree(data);
2744 return ret;
2745
2746error2: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2747 kfree(data);
2748 cm_free_msg(msg);
2749 return ret;
2750}
2751EXPORT_SYMBOL(ib_send_cm_mra);
2752
2753static struct cm_id_private * cm_acquire_mraed_id(struct cm_mra_msg *mra_msg)
2754{
2755 switch (cm_mra_get_msg_mraed(mra_msg)) {
2756 case CM_MSG_RESPONSE_REQ:
2757 return cm_acquire_id(mra_msg->remote_comm_id, 0);
2758 case CM_MSG_RESPONSE_REP:
2759 case CM_MSG_RESPONSE_OTHER:
2760 return cm_acquire_id(mra_msg->remote_comm_id,
2761 mra_msg->local_comm_id);
2762 default:
2763 return NULL;
2764 }
2765}
2766
2767static int cm_mra_handler(struct cm_work *work)
2768{
2769 struct cm_id_private *cm_id_priv;
2770 struct cm_mra_msg *mra_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002771 int timeout, ret;
2772
2773 mra_msg = (struct cm_mra_msg *)work->mad_recv_wc->recv_buf.mad;
2774 cm_id_priv = cm_acquire_mraed_id(mra_msg);
2775 if (!cm_id_priv)
2776 return -EINVAL;
2777
2778 work->cm_event.private_data = &mra_msg->private_data;
2779 work->cm_event.param.mra_rcvd.service_timeout =
2780 cm_mra_get_service_timeout(mra_msg);
2781 timeout = cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg)) +
Sean Hefty1d846122007-06-18 11:09:37 -07002782 cm_convert_to_ms(cm_id_priv->av.timeout);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002783
Sean Hefty24be6e82007-06-18 11:09:36 -07002784 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002785 switch (cm_id_priv->id.state) {
2786 case IB_CM_REQ_SENT:
2787 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REQ ||
2788 ib_modify_mad(cm_id_priv->av.port->mad_agent,
Sean Hefty34816ad2005-10-25 10:51:39 -07002789 cm_id_priv->msg, timeout))
Hal Rosenstocka9770492005-07-27 11:45:40 -07002790 goto out;
2791 cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD;
2792 break;
2793 case IB_CM_REP_SENT:
2794 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REP ||
2795 ib_modify_mad(cm_id_priv->av.port->mad_agent,
Sean Hefty34816ad2005-10-25 10:51:39 -07002796 cm_id_priv->msg, timeout))
Hal Rosenstocka9770492005-07-27 11:45:40 -07002797 goto out;
2798 cm_id_priv->id.state = IB_CM_MRA_REP_RCVD;
2799 break;
2800 case IB_CM_ESTABLISHED:
2801 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_OTHER ||
2802 cm_id_priv->id.lap_state != IB_CM_LAP_SENT ||
2803 ib_modify_mad(cm_id_priv->av.port->mad_agent,
Sean Hefty9af57b72007-07-16 21:49:35 -07002804 cm_id_priv->msg, timeout)) {
2805 if (cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
2806 atomic_long_inc(&work->port->
2807 counter_group[CM_RECV_DUPLICATES].
2808 counter[CM_MRA_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002809 goto out;
Sean Hefty9af57b72007-07-16 21:49:35 -07002810 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07002811 cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD;
2812 break;
Sean Hefty9af57b72007-07-16 21:49:35 -07002813 case IB_CM_MRA_REQ_RCVD:
2814 case IB_CM_MRA_REP_RCVD:
2815 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2816 counter[CM_MRA_COUNTER]);
2817 /* fall through */
Hal Rosenstocka9770492005-07-27 11:45:40 -07002818 default:
2819 goto out;
2820 }
2821
2822 cm_id_priv->msg->context[1] = (void *) (unsigned long)
2823 cm_id_priv->id.state;
2824 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2825 if (!ret)
2826 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002827 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002828
2829 if (ret)
2830 cm_process_work(cm_id_priv, work);
2831 else
2832 cm_deref_id(cm_id_priv);
2833 return 0;
2834out:
Sean Hefty24be6e82007-06-18 11:09:36 -07002835 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002836 cm_deref_id(cm_id_priv);
2837 return -EINVAL;
2838}
2839
2840static void cm_format_lap(struct cm_lap_msg *lap_msg,
2841 struct cm_id_private *cm_id_priv,
Dasaratharaman Chandramoulic2f8fc42017-04-27 19:05:58 -04002842 struct sa_path_rec *alternate_path,
Hal Rosenstocka9770492005-07-27 11:45:40 -07002843 const void *private_data,
2844 u8 private_data_len)
2845{
2846 cm_format_mad_hdr(&lap_msg->hdr, CM_LAP_ATTR_ID,
2847 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_LAP));
2848 lap_msg->local_comm_id = cm_id_priv->id.local_id;
2849 lap_msg->remote_comm_id = cm_id_priv->id.remote_id;
2850 cm_lap_set_remote_qpn(lap_msg, cm_id_priv->remote_qpn);
2851 /* todo: need remote CM response timeout */
2852 cm_lap_set_remote_resp_timeout(lap_msg, 0x1F);
Dasaratharaman Chandramouli57520752017-04-27 19:06:01 -04002853 lap_msg->alt_local_lid =
2854 htons(ntohl(sa_path_get_slid(alternate_path)));
2855 lap_msg->alt_remote_lid =
2856 htons(ntohl(sa_path_get_dlid(alternate_path)));
Hal Rosenstocka9770492005-07-27 11:45:40 -07002857 lap_msg->alt_local_gid = alternate_path->sgid;
2858 lap_msg->alt_remote_gid = alternate_path->dgid;
2859 cm_lap_set_flow_label(lap_msg, alternate_path->flow_label);
2860 cm_lap_set_traffic_class(lap_msg, alternate_path->traffic_class);
2861 lap_msg->alt_hop_limit = alternate_path->hop_limit;
2862 cm_lap_set_packet_rate(lap_msg, alternate_path->rate);
2863 cm_lap_set_sl(lap_msg, alternate_path->sl);
2864 cm_lap_set_subnet_local(lap_msg, 1); /* local only... */
2865 cm_lap_set_local_ack_timeout(lap_msg,
Sean Hefty1d846122007-06-18 11:09:37 -07002866 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
2867 alternate_path->packet_life_time));
Hal Rosenstocka9770492005-07-27 11:45:40 -07002868
2869 if (private_data && private_data_len)
2870 memcpy(lap_msg->private_data, private_data, private_data_len);
2871}
2872
2873int ib_send_cm_lap(struct ib_cm_id *cm_id,
Dasaratharaman Chandramoulic2f8fc42017-04-27 19:05:58 -04002874 struct sa_path_rec *alternate_path,
Hal Rosenstocka9770492005-07-27 11:45:40 -07002875 const void *private_data,
2876 u8 private_data_len)
2877{
2878 struct cm_id_private *cm_id_priv;
2879 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002880 unsigned long flags;
2881 int ret;
2882
2883 if (private_data && private_data_len > IB_CM_LAP_PRIVATE_DATA_SIZE)
2884 return -EINVAL;
2885
2886 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2887 spin_lock_irqsave(&cm_id_priv->lock, flags);
2888 if (cm_id->state != IB_CM_ESTABLISHED ||
Sean Heftye1444b52006-11-28 14:57:13 -08002889 (cm_id->lap_state != IB_CM_LAP_UNINIT &&
2890 cm_id->lap_state != IB_CM_LAP_IDLE)) {
Hal Rosenstocka9770492005-07-27 11:45:40 -07002891 ret = -EINVAL;
2892 goto out;
2893 }
2894
Mark Bloch9db0ff52016-10-27 16:36:27 +03002895 ret = cm_init_av_by_path(alternate_path, &cm_id_priv->alt_av,
2896 cm_id_priv);
Sean Heftye1444b52006-11-28 14:57:13 -08002897 if (ret)
2898 goto out;
Sean Hefty1d846122007-06-18 11:09:37 -07002899 cm_id_priv->alt_av.timeout =
2900 cm_ack_timeout(cm_id_priv->target_ack_delay,
2901 cm_id_priv->alt_av.timeout - 1);
Sean Heftye1444b52006-11-28 14:57:13 -08002902
Hal Rosenstocka9770492005-07-27 11:45:40 -07002903 ret = cm_alloc_msg(cm_id_priv, &msg);
2904 if (ret)
2905 goto out;
2906
2907 cm_format_lap((struct cm_lap_msg *) msg->mad, cm_id_priv,
2908 alternate_path, private_data, private_data_len);
Sean Hefty34816ad2005-10-25 10:51:39 -07002909 msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002910 msg->context[1] = (void *) (unsigned long) IB_CM_ESTABLISHED;
2911
Sean Hefty34816ad2005-10-25 10:51:39 -07002912 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002913 if (ret) {
2914 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2915 cm_free_msg(msg);
2916 return ret;
2917 }
2918
2919 cm_id->lap_state = IB_CM_LAP_SENT;
2920 cm_id_priv->msg = msg;
2921
2922out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2923 return ret;
2924}
2925EXPORT_SYMBOL(ib_send_cm_lap);
2926
Sean Heftye1444b52006-11-28 14:57:13 -08002927static void cm_format_path_from_lap(struct cm_id_private *cm_id_priv,
Dasaratharaman Chandramoulic2f8fc42017-04-27 19:05:58 -04002928 struct sa_path_rec *path,
Hal Rosenstocka9770492005-07-27 11:45:40 -07002929 struct cm_lap_msg *lap_msg)
2930{
2931 memset(path, 0, sizeof *path);
Dasaratharaman Chandramouli9fdca4d2017-04-27 19:06:00 -04002932 path->rec_type = SA_PATH_REC_TYPE_IB;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002933 path->dgid = lap_msg->alt_local_gid;
2934 path->sgid = lap_msg->alt_remote_gid;
Dasaratharaman Chandramouli57520752017-04-27 19:06:01 -04002935 sa_path_set_dlid(path, htonl(ntohs(lap_msg->alt_local_lid)));
2936 sa_path_set_slid(path, htonl(ntohs(lap_msg->alt_remote_lid)));
Hal Rosenstocka9770492005-07-27 11:45:40 -07002937 path->flow_label = cm_lap_get_flow_label(lap_msg);
2938 path->hop_limit = lap_msg->alt_hop_limit;
2939 path->traffic_class = cm_lap_get_traffic_class(lap_msg);
2940 path->reversible = 1;
Sean Heftye1444b52006-11-28 14:57:13 -08002941 path->pkey = cm_id_priv->pkey;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002942 path->sl = cm_lap_get_sl(lap_msg);
2943 path->mtu_selector = IB_SA_EQ;
Sean Heftye1444b52006-11-28 14:57:13 -08002944 path->mtu = cm_id_priv->path_mtu;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002945 path->rate_selector = IB_SA_EQ;
2946 path->rate = cm_lap_get_packet_rate(lap_msg);
2947 path->packet_life_time_selector = IB_SA_EQ;
2948 path->packet_life_time = cm_lap_get_local_ack_timeout(lap_msg);
2949 path->packet_life_time -= (path->packet_life_time > 0);
2950}
2951
2952static int cm_lap_handler(struct cm_work *work)
2953{
2954 struct cm_id_private *cm_id_priv;
2955 struct cm_lap_msg *lap_msg;
2956 struct ib_cm_lap_event_param *param;
2957 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002958 int ret;
2959
2960 /* todo: verify LAP request and send reject APR if invalid. */
2961 lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad;
2962 cm_id_priv = cm_acquire_id(lap_msg->remote_comm_id,
2963 lap_msg->local_comm_id);
2964 if (!cm_id_priv)
2965 return -EINVAL;
2966
2967 param = &work->cm_event.param.lap_rcvd;
2968 param->alternate_path = &work->path[0];
Sean Heftye1444b52006-11-28 14:57:13 -08002969 cm_format_path_from_lap(cm_id_priv, param->alternate_path, lap_msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002970 work->cm_event.private_data = &lap_msg->private_data;
2971
Sean Hefty24be6e82007-06-18 11:09:36 -07002972 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002973 if (cm_id_priv->id.state != IB_CM_ESTABLISHED)
2974 goto unlock;
2975
2976 switch (cm_id_priv->id.lap_state) {
Sean Heftye1444b52006-11-28 14:57:13 -08002977 case IB_CM_LAP_UNINIT:
Hal Rosenstocka9770492005-07-27 11:45:40 -07002978 case IB_CM_LAP_IDLE:
2979 break;
2980 case IB_CM_MRA_LAP_SENT:
Sean Hefty9af57b72007-07-16 21:49:35 -07002981 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2982 counter[CM_LAP_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002983 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2984 goto unlock;
2985
2986 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2987 CM_MSG_RESPONSE_OTHER,
2988 cm_id_priv->service_timeout,
2989 cm_id_priv->private_data,
2990 cm_id_priv->private_data_len);
Sean Hefty24be6e82007-06-18 11:09:36 -07002991 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002992
Sean Hefty34816ad2005-10-25 10:51:39 -07002993 if (ib_post_send_mad(msg, NULL))
Hal Rosenstocka9770492005-07-27 11:45:40 -07002994 cm_free_msg(msg);
2995 goto deref;
Sean Hefty9af57b72007-07-16 21:49:35 -07002996 case IB_CM_LAP_RCVD:
2997 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2998 counter[CM_LAP_COUNTER]);
2999 goto unlock;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003000 default:
3001 goto unlock;
3002 }
3003
3004 cm_id_priv->id.lap_state = IB_CM_LAP_RCVD;
3005 cm_id_priv->tid = lap_msg->hdr.tid;
Sean Heftye1444b52006-11-28 14:57:13 -08003006 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
3007 work->mad_recv_wc->recv_buf.grh,
3008 &cm_id_priv->av);
Mark Bloch9db0ff52016-10-27 16:36:27 +03003009 cm_init_av_by_path(param->alternate_path, &cm_id_priv->alt_av,
3010 cm_id_priv);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003011 ret = atomic_inc_and_test(&cm_id_priv->work_count);
3012 if (!ret)
3013 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07003014 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003015
3016 if (ret)
3017 cm_process_work(cm_id_priv, work);
3018 else
3019 cm_deref_id(cm_id_priv);
3020 return 0;
3021
Sean Hefty24be6e82007-06-18 11:09:36 -07003022unlock: spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003023deref: cm_deref_id(cm_id_priv);
3024 return -EINVAL;
3025}
3026
3027static void cm_format_apr(struct cm_apr_msg *apr_msg,
3028 struct cm_id_private *cm_id_priv,
3029 enum ib_cm_apr_status status,
3030 void *info,
3031 u8 info_length,
3032 const void *private_data,
3033 u8 private_data_len)
3034{
3035 cm_format_mad_hdr(&apr_msg->hdr, CM_APR_ATTR_ID, cm_id_priv->tid);
3036 apr_msg->local_comm_id = cm_id_priv->id.local_id;
3037 apr_msg->remote_comm_id = cm_id_priv->id.remote_id;
3038 apr_msg->ap_status = (u8) status;
3039
3040 if (info && info_length) {
3041 apr_msg->info_length = info_length;
3042 memcpy(apr_msg->info, info, info_length);
3043 }
3044
3045 if (private_data && private_data_len)
3046 memcpy(apr_msg->private_data, private_data, private_data_len);
3047}
3048
3049int ib_send_cm_apr(struct ib_cm_id *cm_id,
3050 enum ib_cm_apr_status status,
3051 void *info,
3052 u8 info_length,
3053 const void *private_data,
3054 u8 private_data_len)
3055{
3056 struct cm_id_private *cm_id_priv;
3057 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003058 unsigned long flags;
3059 int ret;
3060
3061 if ((private_data && private_data_len > IB_CM_APR_PRIVATE_DATA_SIZE) ||
3062 (info && info_length > IB_CM_APR_INFO_LENGTH))
3063 return -EINVAL;
3064
3065 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3066 spin_lock_irqsave(&cm_id_priv->lock, flags);
3067 if (cm_id->state != IB_CM_ESTABLISHED ||
3068 (cm_id->lap_state != IB_CM_LAP_RCVD &&
3069 cm_id->lap_state != IB_CM_MRA_LAP_SENT)) {
3070 ret = -EINVAL;
3071 goto out;
3072 }
3073
3074 ret = cm_alloc_msg(cm_id_priv, &msg);
3075 if (ret)
3076 goto out;
3077
3078 cm_format_apr((struct cm_apr_msg *) msg->mad, cm_id_priv, status,
3079 info, info_length, private_data, private_data_len);
Sean Hefty34816ad2005-10-25 10:51:39 -07003080 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003081 if (ret) {
3082 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3083 cm_free_msg(msg);
3084 return ret;
3085 }
3086
3087 cm_id->lap_state = IB_CM_LAP_IDLE;
3088out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3089 return ret;
3090}
3091EXPORT_SYMBOL(ib_send_cm_apr);
3092
3093static int cm_apr_handler(struct cm_work *work)
3094{
3095 struct cm_id_private *cm_id_priv;
3096 struct cm_apr_msg *apr_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003097 int ret;
3098
3099 apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad;
3100 cm_id_priv = cm_acquire_id(apr_msg->remote_comm_id,
3101 apr_msg->local_comm_id);
3102 if (!cm_id_priv)
3103 return -EINVAL; /* Unmatched reply. */
3104
3105 work->cm_event.param.apr_rcvd.ap_status = apr_msg->ap_status;
3106 work->cm_event.param.apr_rcvd.apr_info = &apr_msg->info;
3107 work->cm_event.param.apr_rcvd.info_len = apr_msg->info_length;
3108 work->cm_event.private_data = &apr_msg->private_data;
3109
Sean Hefty24be6e82007-06-18 11:09:36 -07003110 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003111 if (cm_id_priv->id.state != IB_CM_ESTABLISHED ||
3112 (cm_id_priv->id.lap_state != IB_CM_LAP_SENT &&
3113 cm_id_priv->id.lap_state != IB_CM_MRA_LAP_RCVD)) {
Sean Hefty24be6e82007-06-18 11:09:36 -07003114 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003115 goto out;
3116 }
3117 cm_id_priv->id.lap_state = IB_CM_LAP_IDLE;
Sean Hefty34816ad2005-10-25 10:51:39 -07003118 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003119 cm_id_priv->msg = NULL;
3120
3121 ret = atomic_inc_and_test(&cm_id_priv->work_count);
3122 if (!ret)
3123 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07003124 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003125
3126 if (ret)
3127 cm_process_work(cm_id_priv, work);
3128 else
3129 cm_deref_id(cm_id_priv);
3130 return 0;
3131out:
3132 cm_deref_id(cm_id_priv);
3133 return -EINVAL;
3134}
3135
3136static int cm_timewait_handler(struct cm_work *work)
3137{
3138 struct cm_timewait_info *timewait_info;
3139 struct cm_id_private *cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003140 int ret;
3141
3142 timewait_info = (struct cm_timewait_info *)work;
Sean Hefty85753292006-10-04 11:29:59 -07003143 spin_lock_irq(&cm.lock);
3144 list_del(&timewait_info->list);
3145 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003146
3147 cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
3148 timewait_info->work.remote_id);
3149 if (!cm_id_priv)
3150 return -EINVAL;
3151
Sean Hefty85753292006-10-04 11:29:59 -07003152 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003153 if (cm_id_priv->id.state != IB_CM_TIMEWAIT ||
3154 cm_id_priv->remote_qpn != timewait_info->remote_qpn) {
Sean Hefty85753292006-10-04 11:29:59 -07003155 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003156 goto out;
3157 }
3158 cm_id_priv->id.state = IB_CM_IDLE;
3159 ret = atomic_inc_and_test(&cm_id_priv->work_count);
3160 if (!ret)
3161 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty85753292006-10-04 11:29:59 -07003162 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003163
3164 if (ret)
3165 cm_process_work(cm_id_priv, work);
3166 else
3167 cm_deref_id(cm_id_priv);
3168 return 0;
3169out:
3170 cm_deref_id(cm_id_priv);
3171 return -EINVAL;
3172}
3173
3174static void cm_format_sidr_req(struct cm_sidr_req_msg *sidr_req_msg,
3175 struct cm_id_private *cm_id_priv,
3176 struct ib_cm_sidr_req_param *param)
3177{
3178 cm_format_mad_hdr(&sidr_req_msg->hdr, CM_SIDR_REQ_ATTR_ID,
3179 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_SIDR));
3180 sidr_req_msg->request_id = cm_id_priv->id.local_id;
Roland Dreiera88f4882008-04-16 21:01:07 -07003181 sidr_req_msg->pkey = param->path->pkey;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003182 sidr_req_msg->service_id = param->service_id;
3183
3184 if (param->private_data && param->private_data_len)
3185 memcpy(sidr_req_msg->private_data, param->private_data,
3186 param->private_data_len);
3187}
3188
3189int ib_send_cm_sidr_req(struct ib_cm_id *cm_id,
3190 struct ib_cm_sidr_req_param *param)
3191{
3192 struct cm_id_private *cm_id_priv;
3193 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003194 unsigned long flags;
3195 int ret;
3196
3197 if (!param->path || (param->private_data &&
3198 param->private_data_len > IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE))
3199 return -EINVAL;
3200
3201 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
Mark Bloch9db0ff52016-10-27 16:36:27 +03003202 ret = cm_init_av_by_path(param->path, &cm_id_priv->av, cm_id_priv);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003203 if (ret)
3204 goto out;
3205
3206 cm_id->service_id = param->service_id;
Harvey Harrison9c3da092009-01-17 17:11:57 -08003207 cm_id->service_mask = ~cpu_to_be64(0);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003208 cm_id_priv->timeout_ms = param->timeout_ms;
3209 cm_id_priv->max_cm_retries = param->max_cm_retries;
3210 ret = cm_alloc_msg(cm_id_priv, &msg);
3211 if (ret)
3212 goto out;
3213
3214 cm_format_sidr_req((struct cm_sidr_req_msg *) msg->mad, cm_id_priv,
3215 param);
Sean Hefty34816ad2005-10-25 10:51:39 -07003216 msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003217 msg->context[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT;
3218
3219 spin_lock_irqsave(&cm_id_priv->lock, flags);
3220 if (cm_id->state == IB_CM_IDLE)
Sean Hefty34816ad2005-10-25 10:51:39 -07003221 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003222 else
3223 ret = -EINVAL;
3224
3225 if (ret) {
3226 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3227 cm_free_msg(msg);
3228 goto out;
3229 }
3230 cm_id->state = IB_CM_SIDR_REQ_SENT;
3231 cm_id_priv->msg = msg;
3232 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3233out:
3234 return ret;
3235}
3236EXPORT_SYMBOL(ib_send_cm_sidr_req);
3237
3238static void cm_format_sidr_req_event(struct cm_work *work,
3239 struct ib_cm_id *listen_id)
3240{
3241 struct cm_sidr_req_msg *sidr_req_msg;
3242 struct ib_cm_sidr_req_event_param *param;
3243
3244 sidr_req_msg = (struct cm_sidr_req_msg *)
3245 work->mad_recv_wc->recv_buf.mad;
3246 param = &work->cm_event.param.sidr_req_rcvd;
Sean Hefty97f52eb2005-08-13 21:05:57 -07003247 param->pkey = __be16_to_cpu(sidr_req_msg->pkey);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003248 param->listen_id = listen_id;
Haggai Eran15865e7d2015-07-30 17:50:17 +03003249 param->service_id = sidr_req_msg->service_id;
Haggai Eran24cad9a2015-07-30 17:50:21 +03003250 param->bth_pkey = cm_get_bth_pkey(work);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003251 param->port = work->port->port_num;
3252 work->cm_event.private_data = &sidr_req_msg->private_data;
3253}
3254
3255static int cm_sidr_req_handler(struct cm_work *work)
3256{
3257 struct ib_cm_id *cm_id;
3258 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
3259 struct cm_sidr_req_msg *sidr_req_msg;
3260 struct ib_wc *wc;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003261
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003262 cm_id = ib_create_cm_id(work->port->cm_dev->ib_device, NULL, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003263 if (IS_ERR(cm_id))
3264 return PTR_ERR(cm_id);
3265 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3266
3267 /* Record SGID/SLID and request ID for lookup. */
3268 sidr_req_msg = (struct cm_sidr_req_msg *)
3269 work->mad_recv_wc->recv_buf.mad;
3270 wc = work->mad_recv_wc->wc;
Sean Hefty97f52eb2005-08-13 21:05:57 -07003271 cm_id_priv->av.dgid.global.subnet_prefix = cpu_to_be64(wc->slid);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003272 cm_id_priv->av.dgid.global.interface_id = 0;
3273 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
Sean Heftyca222c62006-06-17 20:37:40 -07003274 work->mad_recv_wc->recv_buf.grh,
Hal Rosenstocka9770492005-07-27 11:45:40 -07003275 &cm_id_priv->av);
3276 cm_id_priv->id.remote_id = sidr_req_msg->request_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003277 cm_id_priv->tid = sidr_req_msg->hdr.tid;
3278 atomic_inc(&cm_id_priv->work_count);
3279
Sean Hefty24be6e82007-06-18 11:09:36 -07003280 spin_lock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003281 cur_cm_id_priv = cm_insert_remote_sidr(cm_id_priv);
3282 if (cur_cm_id_priv) {
Sean Hefty24be6e82007-06-18 11:09:36 -07003283 spin_unlock_irq(&cm.lock);
Sean Hefty9af57b72007-07-16 21:49:35 -07003284 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
3285 counter[CM_SIDR_REQ_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003286 goto out; /* Duplicate message. */
3287 }
Sean Hefty29c27312007-07-02 14:36:19 -07003288 cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD;
Sean Hefty07d357d2005-10-17 15:37:43 -07003289 cur_cm_id_priv = cm_find_listen(cm_id->device,
Haggai Eran73fec7f2015-07-30 17:50:26 +03003290 sidr_req_msg->service_id);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003291 if (!cur_cm_id_priv) {
Sean Hefty24be6e82007-06-18 11:09:36 -07003292 spin_unlock_irq(&cm.lock);
Sean Hefty6164c8c2007-07-02 16:14:14 -07003293 cm_reject_sidr_req(cm_id_priv, IB_SIDR_UNSUPPORTED);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003294 goto out; /* No match. */
3295 }
3296 atomic_inc(&cur_cm_id_priv->refcount);
Sean Hefty29963432011-02-23 08:17:40 -08003297 atomic_inc(&cm_id_priv->refcount);
Sean Hefty24be6e82007-06-18 11:09:36 -07003298 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003299
3300 cm_id_priv->id.cm_handler = cur_cm_id_priv->id.cm_handler;
3301 cm_id_priv->id.context = cur_cm_id_priv->id.context;
3302 cm_id_priv->id.service_id = sidr_req_msg->service_id;
Harvey Harrison9c3da092009-01-17 17:11:57 -08003303 cm_id_priv->id.service_mask = ~cpu_to_be64(0);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003304
3305 cm_format_sidr_req_event(work, &cur_cm_id_priv->id);
3306 cm_process_work(cm_id_priv, work);
3307 cm_deref_id(cur_cm_id_priv);
3308 return 0;
3309out:
3310 ib_destroy_cm_id(&cm_id_priv->id);
3311 return -EINVAL;
3312}
3313
3314static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg,
3315 struct cm_id_private *cm_id_priv,
3316 struct ib_cm_sidr_rep_param *param)
3317{
3318 cm_format_mad_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID,
3319 cm_id_priv->tid);
3320 sidr_rep_msg->request_id = cm_id_priv->id.remote_id;
3321 sidr_rep_msg->status = param->status;
3322 cm_sidr_rep_set_qpn(sidr_rep_msg, cpu_to_be32(param->qp_num));
3323 sidr_rep_msg->service_id = cm_id_priv->id.service_id;
3324 sidr_rep_msg->qkey = cpu_to_be32(param->qkey);
3325
3326 if (param->info && param->info_length)
3327 memcpy(sidr_rep_msg->info, param->info, param->info_length);
3328
3329 if (param->private_data && param->private_data_len)
3330 memcpy(sidr_rep_msg->private_data, param->private_data,
3331 param->private_data_len);
3332}
3333
3334int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id,
3335 struct ib_cm_sidr_rep_param *param)
3336{
3337 struct cm_id_private *cm_id_priv;
3338 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003339 unsigned long flags;
3340 int ret;
3341
3342 if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) ||
3343 (param->private_data &&
3344 param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE))
3345 return -EINVAL;
3346
3347 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3348 spin_lock_irqsave(&cm_id_priv->lock, flags);
3349 if (cm_id->state != IB_CM_SIDR_REQ_RCVD) {
3350 ret = -EINVAL;
3351 goto error;
3352 }
3353
3354 ret = cm_alloc_msg(cm_id_priv, &msg);
3355 if (ret)
3356 goto error;
3357
3358 cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv,
3359 param);
Sean Hefty34816ad2005-10-25 10:51:39 -07003360 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003361 if (ret) {
3362 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3363 cm_free_msg(msg);
3364 return ret;
3365 }
3366 cm_id->state = IB_CM_IDLE;
3367 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3368
3369 spin_lock_irqsave(&cm.lock, flags);
Doron Tsur0ca81a22015-10-11 15:58:17 +03003370 if (!RB_EMPTY_NODE(&cm_id_priv->sidr_id_node)) {
3371 rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
3372 RB_CLEAR_NODE(&cm_id_priv->sidr_id_node);
3373 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07003374 spin_unlock_irqrestore(&cm.lock, flags);
3375 return 0;
3376
3377error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3378 return ret;
3379}
3380EXPORT_SYMBOL(ib_send_cm_sidr_rep);
3381
3382static void cm_format_sidr_rep_event(struct cm_work *work)
3383{
3384 struct cm_sidr_rep_msg *sidr_rep_msg;
3385 struct ib_cm_sidr_rep_event_param *param;
3386
3387 sidr_rep_msg = (struct cm_sidr_rep_msg *)
3388 work->mad_recv_wc->recv_buf.mad;
3389 param = &work->cm_event.param.sidr_rep_rcvd;
3390 param->status = sidr_rep_msg->status;
3391 param->qkey = be32_to_cpu(sidr_rep_msg->qkey);
3392 param->qpn = be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg));
3393 param->info = &sidr_rep_msg->info;
3394 param->info_len = sidr_rep_msg->info_length;
3395 work->cm_event.private_data = &sidr_rep_msg->private_data;
3396}
3397
3398static int cm_sidr_rep_handler(struct cm_work *work)
3399{
3400 struct cm_sidr_rep_msg *sidr_rep_msg;
3401 struct cm_id_private *cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003402
3403 sidr_rep_msg = (struct cm_sidr_rep_msg *)
3404 work->mad_recv_wc->recv_buf.mad;
3405 cm_id_priv = cm_acquire_id(sidr_rep_msg->request_id, 0);
3406 if (!cm_id_priv)
3407 return -EINVAL; /* Unmatched reply. */
3408
Sean Hefty24be6e82007-06-18 11:09:36 -07003409 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003410 if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) {
Sean Hefty24be6e82007-06-18 11:09:36 -07003411 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003412 goto out;
3413 }
3414 cm_id_priv->id.state = IB_CM_IDLE;
Sean Hefty34816ad2005-10-25 10:51:39 -07003415 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Sean Hefty24be6e82007-06-18 11:09:36 -07003416 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003417
3418 cm_format_sidr_rep_event(work);
3419 cm_process_work(cm_id_priv, work);
3420 return 0;
3421out:
3422 cm_deref_id(cm_id_priv);
3423 return -EINVAL;
3424}
3425
3426static void cm_process_send_error(struct ib_mad_send_buf *msg,
3427 enum ib_wc_status wc_status)
3428{
3429 struct cm_id_private *cm_id_priv;
3430 struct ib_cm_event cm_event;
3431 enum ib_cm_state state;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003432 int ret;
3433
3434 memset(&cm_event, 0, sizeof cm_event);
3435 cm_id_priv = msg->context[0];
3436
3437 /* Discard old sends or ones without a response. */
Sean Hefty24be6e82007-06-18 11:09:36 -07003438 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003439 state = (enum ib_cm_state) (unsigned long) msg->context[1];
3440 if (msg != cm_id_priv->msg || state != cm_id_priv->id.state)
3441 goto discard;
3442
Moni Shoua498683c2017-01-05 16:16:35 +02003443 pr_debug_ratelimited("CM: failed sending MAD in state %d. (%s)\n",
3444 state, ib_wc_status_msg(wc_status));
Hal Rosenstocka9770492005-07-27 11:45:40 -07003445 switch (state) {
3446 case IB_CM_REQ_SENT:
3447 case IB_CM_MRA_REQ_RCVD:
3448 cm_reset_to_idle(cm_id_priv);
3449 cm_event.event = IB_CM_REQ_ERROR;
3450 break;
3451 case IB_CM_REP_SENT:
3452 case IB_CM_MRA_REP_RCVD:
3453 cm_reset_to_idle(cm_id_priv);
3454 cm_event.event = IB_CM_REP_ERROR;
3455 break;
3456 case IB_CM_DREQ_SENT:
3457 cm_enter_timewait(cm_id_priv);
3458 cm_event.event = IB_CM_DREQ_ERROR;
3459 break;
3460 case IB_CM_SIDR_REQ_SENT:
3461 cm_id_priv->id.state = IB_CM_IDLE;
3462 cm_event.event = IB_CM_SIDR_REQ_ERROR;
3463 break;
3464 default:
3465 goto discard;
3466 }
Sean Hefty24be6e82007-06-18 11:09:36 -07003467 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003468 cm_event.param.send_status = wc_status;
3469
3470 /* No other events can occur on the cm_id at this point. */
3471 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event);
3472 cm_free_msg(msg);
3473 if (ret)
3474 ib_destroy_cm_id(&cm_id_priv->id);
3475 return;
3476discard:
Sean Hefty24be6e82007-06-18 11:09:36 -07003477 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003478 cm_free_msg(msg);
3479}
3480
3481static void cm_send_handler(struct ib_mad_agent *mad_agent,
3482 struct ib_mad_send_wc *mad_send_wc)
3483{
Sean Hefty34816ad2005-10-25 10:51:39 -07003484 struct ib_mad_send_buf *msg = mad_send_wc->send_buf;
Sean Hefty9af57b72007-07-16 21:49:35 -07003485 struct cm_port *port;
3486 u16 attr_index;
3487
3488 port = mad_agent->context;
3489 attr_index = be16_to_cpu(((struct ib_mad_hdr *)
3490 msg->mad)->attr_id) - CM_ATTR_ID_OFFSET;
3491
3492 /*
3493 * If the send was in response to a received message (context[0] is not
3494 * set to a cm_id), and is not a REJ, then it is a send that was
3495 * manually retried.
3496 */
3497 if (!msg->context[0] && (attr_index != CM_REJ_COUNTER))
3498 msg->retries = 1;
3499
3500 atomic_long_add(1 + msg->retries,
3501 &port->counter_group[CM_XMIT].counter[attr_index]);
3502 if (msg->retries)
3503 atomic_long_add(msg->retries,
3504 &port->counter_group[CM_XMIT_RETRIES].
3505 counter[attr_index]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003506
3507 switch (mad_send_wc->status) {
3508 case IB_WC_SUCCESS:
3509 case IB_WC_WR_FLUSH_ERR:
3510 cm_free_msg(msg);
3511 break;
3512 default:
3513 if (msg->context[0] && msg->context[1])
3514 cm_process_send_error(msg, mad_send_wc->status);
3515 else
3516 cm_free_msg(msg);
3517 break;
3518 }
3519}
3520
David Howellsc4028952006-11-22 14:57:56 +00003521static void cm_work_handler(struct work_struct *_work)
Hal Rosenstocka9770492005-07-27 11:45:40 -07003522{
David Howellsc4028952006-11-22 14:57:56 +00003523 struct cm_work *work = container_of(_work, struct cm_work, work.work);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003524 int ret;
3525
3526 switch (work->cm_event.event) {
3527 case IB_CM_REQ_RECEIVED:
3528 ret = cm_req_handler(work);
3529 break;
3530 case IB_CM_MRA_RECEIVED:
3531 ret = cm_mra_handler(work);
3532 break;
3533 case IB_CM_REJ_RECEIVED:
3534 ret = cm_rej_handler(work);
3535 break;
3536 case IB_CM_REP_RECEIVED:
3537 ret = cm_rep_handler(work);
3538 break;
3539 case IB_CM_RTU_RECEIVED:
3540 ret = cm_rtu_handler(work);
3541 break;
3542 case IB_CM_USER_ESTABLISHED:
3543 ret = cm_establish_handler(work);
3544 break;
3545 case IB_CM_DREQ_RECEIVED:
3546 ret = cm_dreq_handler(work);
3547 break;
3548 case IB_CM_DREP_RECEIVED:
3549 ret = cm_drep_handler(work);
3550 break;
3551 case IB_CM_SIDR_REQ_RECEIVED:
3552 ret = cm_sidr_req_handler(work);
3553 break;
3554 case IB_CM_SIDR_REP_RECEIVED:
3555 ret = cm_sidr_rep_handler(work);
3556 break;
3557 case IB_CM_LAP_RECEIVED:
3558 ret = cm_lap_handler(work);
3559 break;
3560 case IB_CM_APR_RECEIVED:
3561 ret = cm_apr_handler(work);
3562 break;
3563 case IB_CM_TIMEWAIT_EXIT:
3564 ret = cm_timewait_handler(work);
3565 break;
3566 default:
3567 ret = -EINVAL;
3568 break;
3569 }
3570 if (ret)
3571 cm_free_work(work);
3572}
3573
Sean Heftye1444b52006-11-28 14:57:13 -08003574static int cm_establish(struct ib_cm_id *cm_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -07003575{
3576 struct cm_id_private *cm_id_priv;
3577 struct cm_work *work;
3578 unsigned long flags;
3579 int ret = 0;
Erez Shitritbe4b4992015-06-25 17:13:22 +03003580 struct cm_device *cm_dev;
3581
3582 cm_dev = ib_get_client_data(cm_id->device, &cm_client);
3583 if (!cm_dev)
3584 return -ENODEV;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003585
3586 work = kmalloc(sizeof *work, GFP_ATOMIC);
3587 if (!work)
3588 return -ENOMEM;
3589
3590 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3591 spin_lock_irqsave(&cm_id_priv->lock, flags);
3592 switch (cm_id->state)
3593 {
3594 case IB_CM_REP_SENT:
3595 case IB_CM_MRA_REP_RCVD:
3596 cm_id->state = IB_CM_ESTABLISHED;
3597 break;
3598 case IB_CM_ESTABLISHED:
3599 ret = -EISCONN;
3600 break;
3601 default:
3602 ret = -EINVAL;
3603 break;
3604 }
3605 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3606
3607 if (ret) {
3608 kfree(work);
3609 goto out;
3610 }
3611
3612 /*
3613 * The CM worker thread may try to destroy the cm_id before it
3614 * can execute this work item. To prevent potential deadlock,
3615 * we need to find the cm_id once we're in the context of the
3616 * worker thread, rather than holding a reference on it.
3617 */
David Howellsc4028952006-11-22 14:57:56 +00003618 INIT_DELAYED_WORK(&work->work, cm_work_handler);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003619 work->local_id = cm_id->local_id;
3620 work->remote_id = cm_id->remote_id;
3621 work->mad_recv_wc = NULL;
3622 work->cm_event.event = IB_CM_USER_ESTABLISHED;
Erez Shitritbe4b4992015-06-25 17:13:22 +03003623
3624 /* Check if the device started its remove_one */
Bart Van Assche943f44d2016-03-25 08:33:16 -07003625 spin_lock_irqsave(&cm.lock, flags);
Erez Shitritbe4b4992015-06-25 17:13:22 +03003626 if (!cm_dev->going_down) {
3627 queue_delayed_work(cm.wq, &work->work, 0);
3628 } else {
3629 kfree(work);
3630 ret = -ENODEV;
3631 }
Bart Van Assche943f44d2016-03-25 08:33:16 -07003632 spin_unlock_irqrestore(&cm.lock, flags);
Erez Shitritbe4b4992015-06-25 17:13:22 +03003633
Hal Rosenstocka9770492005-07-27 11:45:40 -07003634out:
3635 return ret;
3636}
Sean Heftye1444b52006-11-28 14:57:13 -08003637
3638static int cm_migrate(struct ib_cm_id *cm_id)
3639{
3640 struct cm_id_private *cm_id_priv;
Mark Bloch9db0ff52016-10-27 16:36:27 +03003641 struct cm_av tmp_av;
Sean Heftye1444b52006-11-28 14:57:13 -08003642 unsigned long flags;
Mark Bloch9db0ff52016-10-27 16:36:27 +03003643 int tmp_send_port_not_ready;
Sean Heftye1444b52006-11-28 14:57:13 -08003644 int ret = 0;
3645
3646 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3647 spin_lock_irqsave(&cm_id_priv->lock, flags);
3648 if (cm_id->state == IB_CM_ESTABLISHED &&
3649 (cm_id->lap_state == IB_CM_LAP_UNINIT ||
3650 cm_id->lap_state == IB_CM_LAP_IDLE)) {
3651 cm_id->lap_state = IB_CM_LAP_IDLE;
Mark Bloch9db0ff52016-10-27 16:36:27 +03003652 /* Swap address vector */
3653 tmp_av = cm_id_priv->av;
Sean Heftye1444b52006-11-28 14:57:13 -08003654 cm_id_priv->av = cm_id_priv->alt_av;
Mark Bloch9db0ff52016-10-27 16:36:27 +03003655 cm_id_priv->alt_av = tmp_av;
3656 /* Swap port send ready state */
3657 tmp_send_port_not_ready = cm_id_priv->prim_send_port_not_ready;
3658 cm_id_priv->prim_send_port_not_ready = cm_id_priv->altr_send_port_not_ready;
3659 cm_id_priv->altr_send_port_not_ready = tmp_send_port_not_ready;
Sean Heftye1444b52006-11-28 14:57:13 -08003660 } else
3661 ret = -EINVAL;
3662 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3663
3664 return ret;
3665}
3666
3667int ib_cm_notify(struct ib_cm_id *cm_id, enum ib_event_type event)
3668{
3669 int ret;
3670
3671 switch (event) {
3672 case IB_EVENT_COMM_EST:
3673 ret = cm_establish(cm_id);
3674 break;
3675 case IB_EVENT_PATH_MIG:
3676 ret = cm_migrate(cm_id);
3677 break;
3678 default:
3679 ret = -EINVAL;
3680 }
3681 return ret;
3682}
3683EXPORT_SYMBOL(ib_cm_notify);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003684
3685static void cm_recv_handler(struct ib_mad_agent *mad_agent,
Christoph Hellwigca281262016-01-04 14:15:58 +01003686 struct ib_mad_send_buf *send_buf,
Hal Rosenstocka9770492005-07-27 11:45:40 -07003687 struct ib_mad_recv_wc *mad_recv_wc)
3688{
Sean Hefty9af57b72007-07-16 21:49:35 -07003689 struct cm_port *port = mad_agent->context;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003690 struct cm_work *work;
3691 enum ib_cm_event_type event;
Sean Hefty9af57b72007-07-16 21:49:35 -07003692 u16 attr_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003693 int paths = 0;
Erez Shitritbe4b4992015-06-25 17:13:22 +03003694 int going_down = 0;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003695
3696 switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) {
3697 case CM_REQ_ATTR_ID:
3698 paths = 1 + (((struct cm_req_msg *) mad_recv_wc->recv_buf.mad)->
3699 alt_local_lid != 0);
3700 event = IB_CM_REQ_RECEIVED;
3701 break;
3702 case CM_MRA_ATTR_ID:
3703 event = IB_CM_MRA_RECEIVED;
3704 break;
3705 case CM_REJ_ATTR_ID:
3706 event = IB_CM_REJ_RECEIVED;
3707 break;
3708 case CM_REP_ATTR_ID:
3709 event = IB_CM_REP_RECEIVED;
3710 break;
3711 case CM_RTU_ATTR_ID:
3712 event = IB_CM_RTU_RECEIVED;
3713 break;
3714 case CM_DREQ_ATTR_ID:
3715 event = IB_CM_DREQ_RECEIVED;
3716 break;
3717 case CM_DREP_ATTR_ID:
3718 event = IB_CM_DREP_RECEIVED;
3719 break;
3720 case CM_SIDR_REQ_ATTR_ID:
3721 event = IB_CM_SIDR_REQ_RECEIVED;
3722 break;
3723 case CM_SIDR_REP_ATTR_ID:
3724 event = IB_CM_SIDR_REP_RECEIVED;
3725 break;
3726 case CM_LAP_ATTR_ID:
3727 paths = 1;
3728 event = IB_CM_LAP_RECEIVED;
3729 break;
3730 case CM_APR_ATTR_ID:
3731 event = IB_CM_APR_RECEIVED;
3732 break;
3733 default:
3734 ib_free_recv_mad(mad_recv_wc);
3735 return;
3736 }
3737
Sean Hefty9af57b72007-07-16 21:49:35 -07003738 attr_id = be16_to_cpu(mad_recv_wc->recv_buf.mad->mad_hdr.attr_id);
3739 atomic_long_inc(&port->counter_group[CM_RECV].
3740 counter[attr_id - CM_ATTR_ID_OFFSET]);
3741
Dasaratharaman Chandramoulic2f8fc42017-04-27 19:05:58 -04003742 work = kmalloc(sizeof(*work) + sizeof(struct sa_path_rec) * paths,
Hal Rosenstocka9770492005-07-27 11:45:40 -07003743 GFP_KERNEL);
3744 if (!work) {
3745 ib_free_recv_mad(mad_recv_wc);
3746 return;
3747 }
3748
David Howellsc4028952006-11-22 14:57:56 +00003749 INIT_DELAYED_WORK(&work->work, cm_work_handler);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003750 work->cm_event.event = event;
3751 work->mad_recv_wc = mad_recv_wc;
Sean Hefty9af57b72007-07-16 21:49:35 -07003752 work->port = port;
Erez Shitritbe4b4992015-06-25 17:13:22 +03003753
3754 /* Check if the device started its remove_one */
3755 spin_lock_irq(&cm.lock);
3756 if (!port->cm_dev->going_down)
3757 queue_delayed_work(cm.wq, &work->work, 0);
3758 else
3759 going_down = 1;
3760 spin_unlock_irq(&cm.lock);
3761
3762 if (going_down) {
3763 kfree(work);
3764 ib_free_recv_mad(mad_recv_wc);
3765 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07003766}
3767
3768static int cm_init_qp_init_attr(struct cm_id_private *cm_id_priv,
3769 struct ib_qp_attr *qp_attr,
3770 int *qp_attr_mask)
3771{
3772 unsigned long flags;
3773 int ret;
3774
3775 spin_lock_irqsave(&cm_id_priv->lock, flags);
3776 switch (cm_id_priv->id.state) {
3777 case IB_CM_REQ_SENT:
3778 case IB_CM_MRA_REQ_RCVD:
3779 case IB_CM_REQ_RCVD:
3780 case IB_CM_MRA_REQ_SENT:
3781 case IB_CM_REP_RCVD:
3782 case IB_CM_MRA_REP_SENT:
3783 case IB_CM_REP_SENT:
3784 case IB_CM_MRA_REP_RCVD:
3785 case IB_CM_ESTABLISHED:
3786 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS |
3787 IB_QP_PKEY_INDEX | IB_QP_PORT;
Dotan Barake31353ea2006-10-24 13:35:27 -07003788 qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003789 if (cm_id_priv->responder_resources)
Sean Heftyc1f250c2006-08-28 11:55:52 -07003790 qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ |
3791 IB_ACCESS_REMOTE_ATOMIC;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003792 qp_attr->pkey_index = cm_id_priv->av.pkey_index;
3793 qp_attr->port_num = cm_id_priv->av.port->port_num;
3794 ret = 0;
3795 break;
3796 default:
3797 ret = -EINVAL;
3798 break;
3799 }
3800 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3801 return ret;
3802}
3803
3804static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv,
3805 struct ib_qp_attr *qp_attr,
3806 int *qp_attr_mask)
3807{
3808 unsigned long flags;
3809 int ret;
3810
3811 spin_lock_irqsave(&cm_id_priv->lock, flags);
3812 switch (cm_id_priv->id.state) {
3813 case IB_CM_REQ_RCVD:
3814 case IB_CM_MRA_REQ_SENT:
3815 case IB_CM_REP_RCVD:
3816 case IB_CM_MRA_REP_SENT:
3817 case IB_CM_REP_SENT:
3818 case IB_CM_MRA_REP_RCVD:
3819 case IB_CM_ESTABLISHED:
3820 *qp_attr_mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU |
Sean Heftyae7971a2005-10-24 12:33:56 -07003821 IB_QP_DEST_QPN | IB_QP_RQ_PSN;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003822 qp_attr->ah_attr = cm_id_priv->av.ah_attr;
3823 qp_attr->path_mtu = cm_id_priv->path_mtu;
3824 qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn);
3825 qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn);
Sean Heftyd26a3602011-05-13 10:46:20 -07003826 if (cm_id_priv->qp_type == IB_QPT_RC ||
3827 cm_id_priv->qp_type == IB_QPT_XRC_TGT) {
Sean Heftyae7971a2005-10-24 12:33:56 -07003828 *qp_attr_mask |= IB_QP_MAX_DEST_RD_ATOMIC |
3829 IB_QP_MIN_RNR_TIMER;
3830 qp_attr->max_dest_rd_atomic =
3831 cm_id_priv->responder_resources;
3832 qp_attr->min_rnr_timer = 0;
3833 }
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003834 if (rdma_ah_get_dlid(&cm_id_priv->alt_av.ah_attr)) {
Hal Rosenstocka9770492005-07-27 11:45:40 -07003835 *qp_attr_mask |= IB_QP_ALT_PATH;
Sean Hefty0d8fdfd2006-06-30 14:10:14 -07003836 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
Sean Heftye1444b52006-11-28 14:57:13 -08003837 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
Sean Hefty1d846122007-06-18 11:09:37 -07003838 qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003839 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3840 }
3841 ret = 0;
3842 break;
3843 default:
3844 ret = -EINVAL;
3845 break;
3846 }
3847 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3848 return ret;
3849}
3850
3851static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv,
3852 struct ib_qp_attr *qp_attr,
3853 int *qp_attr_mask)
3854{
3855 unsigned long flags;
3856 int ret;
3857
3858 spin_lock_irqsave(&cm_id_priv->lock, flags);
3859 switch (cm_id_priv->id.state) {
Sean Hefty0fe313b2006-11-30 16:37:15 -08003860 /* Allow transition to RTS before sending REP */
3861 case IB_CM_REQ_RCVD:
3862 case IB_CM_MRA_REQ_SENT:
3863
Hal Rosenstocka9770492005-07-27 11:45:40 -07003864 case IB_CM_REP_RCVD:
3865 case IB_CM_MRA_REP_SENT:
3866 case IB_CM_REP_SENT:
3867 case IB_CM_MRA_REP_RCVD:
3868 case IB_CM_ESTABLISHED:
Sean Heftye1444b52006-11-28 14:57:13 -08003869 if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT) {
3870 *qp_attr_mask = IB_QP_STATE | IB_QP_SQ_PSN;
3871 qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn);
Sean Heftyd26a3602011-05-13 10:46:20 -07003872 switch (cm_id_priv->qp_type) {
3873 case IB_QPT_RC:
3874 case IB_QPT_XRC_INI:
3875 *qp_attr_mask |= IB_QP_RETRY_CNT | IB_QP_RNR_RETRY |
Sean Heftye1444b52006-11-28 14:57:13 -08003876 IB_QP_MAX_QP_RD_ATOMIC;
Sean Heftye1444b52006-11-28 14:57:13 -08003877 qp_attr->retry_cnt = cm_id_priv->retry_count;
3878 qp_attr->rnr_retry = cm_id_priv->rnr_retry_count;
Sean Heftyd26a3602011-05-13 10:46:20 -07003879 qp_attr->max_rd_atomic = cm_id_priv->initiator_depth;
3880 /* fall through */
3881 case IB_QPT_XRC_TGT:
3882 *qp_attr_mask |= IB_QP_TIMEOUT;
3883 qp_attr->timeout = cm_id_priv->av.timeout;
3884 break;
3885 default:
3886 break;
Sean Heftye1444b52006-11-28 14:57:13 -08003887 }
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003888 if (rdma_ah_get_dlid(&cm_id_priv->alt_av.ah_attr)) {
Sean Heftye1444b52006-11-28 14:57:13 -08003889 *qp_attr_mask |= IB_QP_PATH_MIG_STATE;
3890 qp_attr->path_mig_state = IB_MIG_REARM;
3891 }
3892 } else {
3893 *qp_attr_mask = IB_QP_ALT_PATH | IB_QP_PATH_MIG_STATE;
3894 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
3895 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
Sean Hefty1d846122007-06-18 11:09:37 -07003896 qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
Sean Heftye1444b52006-11-28 14:57:13 -08003897 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003898 qp_attr->path_mig_state = IB_MIG_REARM;
3899 }
3900 ret = 0;
3901 break;
3902 default:
3903 ret = -EINVAL;
3904 break;
3905 }
3906 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3907 return ret;
3908}
3909
3910int ib_cm_init_qp_attr(struct ib_cm_id *cm_id,
3911 struct ib_qp_attr *qp_attr,
3912 int *qp_attr_mask)
3913{
3914 struct cm_id_private *cm_id_priv;
3915 int ret;
3916
3917 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3918 switch (qp_attr->qp_state) {
3919 case IB_QPS_INIT:
3920 ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask);
3921 break;
3922 case IB_QPS_RTR:
3923 ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask);
3924 break;
3925 case IB_QPS_RTS:
3926 ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask);
3927 break;
3928 default:
3929 ret = -EINVAL;
3930 break;
3931 }
3932 return ret;
3933}
3934EXPORT_SYMBOL(ib_cm_init_qp_attr);
3935
Sean Hefty9af57b72007-07-16 21:49:35 -07003936static ssize_t cm_show_counter(struct kobject *obj, struct attribute *attr,
3937 char *buf)
3938{
3939 struct cm_counter_group *group;
3940 struct cm_counter_attribute *cm_attr;
3941
3942 group = container_of(obj, struct cm_counter_group, obj);
3943 cm_attr = container_of(attr, struct cm_counter_attribute, attr);
3944
3945 return sprintf(buf, "%ld\n",
3946 atomic_long_read(&group->counter[cm_attr->index]));
3947}
3948
Emese Revfy52cf25d2010-01-19 02:58:23 +01003949static const struct sysfs_ops cm_counter_ops = {
Sean Hefty9af57b72007-07-16 21:49:35 -07003950 .show = cm_show_counter
3951};
3952
3953static struct kobj_type cm_counter_obj_type = {
3954 .sysfs_ops = &cm_counter_ops,
3955 .default_attrs = cm_counter_default_attrs
3956};
3957
3958static void cm_release_port_obj(struct kobject *obj)
3959{
3960 struct cm_port *cm_port;
3961
Sean Hefty9af57b72007-07-16 21:49:35 -07003962 cm_port = container_of(obj, struct cm_port, port_obj);
3963 kfree(cm_port);
3964}
3965
3966static struct kobj_type cm_port_obj_type = {
3967 .release = cm_release_port_obj
3968};
3969
Al Viro2c9ede52011-07-23 20:24:48 -04003970static char *cm_devnode(struct device *dev, umode_t *mode)
Roland Dreierc3af0982011-05-23 11:21:47 -07003971{
Goldwyn Rodriguesb2bc4782011-07-04 09:26:57 -07003972 if (mode)
3973 *mode = 0666;
Roland Dreierc3af0982011-05-23 11:21:47 -07003974 return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
3975}
3976
Sean Hefty9af57b72007-07-16 21:49:35 -07003977struct class cm_class = {
Roland Dreierc3af0982011-05-23 11:21:47 -07003978 .owner = THIS_MODULE,
Sean Hefty9af57b72007-07-16 21:49:35 -07003979 .name = "infiniband_cm",
Roland Dreierc3af0982011-05-23 11:21:47 -07003980 .devnode = cm_devnode,
Sean Hefty9af57b72007-07-16 21:49:35 -07003981};
3982EXPORT_SYMBOL(cm_class);
3983
Sean Hefty9af57b72007-07-16 21:49:35 -07003984static int cm_create_port_fs(struct cm_port *port)
3985{
3986 int i, ret;
3987
3988 ret = kobject_init_and_add(&port->port_obj, &cm_port_obj_type,
Greg Kroah-Hartman110cf372008-05-27 10:17:53 +03003989 &port->cm_dev->device->kobj,
Sean Hefty9af57b72007-07-16 21:49:35 -07003990 "%d", port->port_num);
3991 if (ret) {
3992 kfree(port);
3993 return ret;
3994 }
3995
3996 for (i = 0; i < CM_COUNTER_GROUPS; i++) {
3997 ret = kobject_init_and_add(&port->counter_group[i].obj,
3998 &cm_counter_obj_type,
Roland Dreier7c7a9bc2008-02-12 14:38:27 -08003999 &port->port_obj,
Sean Hefty9af57b72007-07-16 21:49:35 -07004000 "%s", counter_group_names[i]);
4001 if (ret)
4002 goto error;
4003 }
4004
4005 return 0;
4006
4007error:
4008 while (i--)
Roland Dreier7c7a9bc2008-02-12 14:38:27 -08004009 kobject_put(&port->counter_group[i].obj);
4010 kobject_put(&port->port_obj);
Sean Hefty9af57b72007-07-16 21:49:35 -07004011 return ret;
4012
4013}
4014
4015static void cm_remove_port_fs(struct cm_port *port)
4016{
4017 int i;
4018
4019 for (i = 0; i < CM_COUNTER_GROUPS; i++)
Roland Dreier7c7a9bc2008-02-12 14:38:27 -08004020 kobject_put(&port->counter_group[i].obj);
Sean Hefty9af57b72007-07-16 21:49:35 -07004021
Roland Dreier7c7a9bc2008-02-12 14:38:27 -08004022 kobject_put(&port->port_obj);
Sean Hefty9af57b72007-07-16 21:49:35 -07004023}
4024
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03004025static void cm_add_one(struct ib_device *ib_device)
Hal Rosenstocka9770492005-07-27 11:45:40 -07004026{
4027 struct cm_device *cm_dev;
4028 struct cm_port *port;
4029 struct ib_mad_reg_req reg_req = {
4030 .mgmt_class = IB_MGMT_CLASS_CM,
Ira Weiny0f29b462014-08-08 19:00:55 -04004031 .mgmt_class_version = IB_CM_CLASS_VERSION,
Hal Rosenstocka9770492005-07-27 11:45:40 -07004032 };
4033 struct ib_port_modify port_modify = {
4034 .set_port_cap_mask = IB_PORT_CM_SUP
4035 };
4036 unsigned long flags;
4037 int ret;
Michael Wang091e6a42015-05-05 14:50:21 +02004038 int count = 0;
Hal Rosenstocka9770492005-07-27 11:45:40 -07004039 u8 i;
4040
Sean Hefty9af57b72007-07-16 21:49:35 -07004041 cm_dev = kzalloc(sizeof(*cm_dev) + sizeof(*port) *
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03004042 ib_device->phys_port_cnt, GFP_KERNEL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07004043 if (!cm_dev)
4044 return;
4045
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03004046 cm_dev->ib_device = ib_device;
Or Gerlitz86bee4c2015-12-18 10:59:45 +02004047 cm_dev->ack_delay = ib_device->attrs.local_ca_ack_delay;
Erez Shitritbe4b4992015-06-25 17:13:22 +03004048 cm_dev->going_down = 0;
Greg Kroah-Hartman91bd4182008-07-21 20:03:34 -07004049 cm_dev->device = device_create(&cm_class, &ib_device->dev,
4050 MKDEV(0, 0), NULL,
4051 "%s", ib_device->name);
Jani Nikula3e340c02010-03-11 16:17:28 +00004052 if (IS_ERR(cm_dev->device)) {
Sean Hefty9af57b72007-07-16 21:49:35 -07004053 kfree(cm_dev);
4054 return;
4055 }
4056
Hal Rosenstocka9770492005-07-27 11:45:40 -07004057 set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask);
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03004058 for (i = 1; i <= ib_device->phys_port_cnt; i++) {
Michael Wang72219cea2015-05-05 14:50:34 +02004059 if (!rdma_cap_ib_cm(ib_device, i))
Michael Wang091e6a42015-05-05 14:50:21 +02004060 continue;
4061
Sean Hefty9af57b72007-07-16 21:49:35 -07004062 port = kzalloc(sizeof *port, GFP_KERNEL);
4063 if (!port)
4064 goto error1;
4065
4066 cm_dev->port[i-1] = port;
Hal Rosenstocka9770492005-07-27 11:45:40 -07004067 port->cm_dev = cm_dev;
4068 port->port_num = i;
Sean Hefty9af57b72007-07-16 21:49:35 -07004069
Mark Bloch9db0ff52016-10-27 16:36:27 +03004070 INIT_LIST_HEAD(&port->cm_priv_prim_list);
4071 INIT_LIST_HEAD(&port->cm_priv_altr_list);
4072
Sean Hefty9af57b72007-07-16 21:49:35 -07004073 ret = cm_create_port_fs(port);
4074 if (ret)
4075 goto error1;
4076
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03004077 port->mad_agent = ib_register_mad_agent(ib_device, i,
Hal Rosenstocka9770492005-07-27 11:45:40 -07004078 IB_QPT_GSI,
4079 &reg_req,
4080 0,
4081 cm_send_handler,
4082 cm_recv_handler,
Ira Weiny0f29b462014-08-08 19:00:55 -04004083 port,
4084 0);
Hal Rosenstocka9770492005-07-27 11:45:40 -07004085 if (IS_ERR(port->mad_agent))
Sean Hefty9af57b72007-07-16 21:49:35 -07004086 goto error2;
Hal Rosenstocka9770492005-07-27 11:45:40 -07004087
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03004088 ret = ib_modify_port(ib_device, i, 0, &port_modify);
Hal Rosenstocka9770492005-07-27 11:45:40 -07004089 if (ret)
Sean Hefty9af57b72007-07-16 21:49:35 -07004090 goto error3;
Michael Wang091e6a42015-05-05 14:50:21 +02004091
4092 count++;
Hal Rosenstocka9770492005-07-27 11:45:40 -07004093 }
Michael Wang091e6a42015-05-05 14:50:21 +02004094
4095 if (!count)
4096 goto free;
4097
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03004098 ib_set_client_data(ib_device, &cm_client, cm_dev);
Hal Rosenstocka9770492005-07-27 11:45:40 -07004099
4100 write_lock_irqsave(&cm.device_lock, flags);
4101 list_add_tail(&cm_dev->list, &cm.device_list);
4102 write_unlock_irqrestore(&cm.device_lock, flags);
4103 return;
4104
Sean Hefty9af57b72007-07-16 21:49:35 -07004105error3:
Sean Heftycf311cd2006-01-10 07:39:34 -08004106 ib_unregister_mad_agent(port->mad_agent);
Sean Hefty9af57b72007-07-16 21:49:35 -07004107error2:
4108 cm_remove_port_fs(port);
Sean Heftycf311cd2006-01-10 07:39:34 -08004109error1:
Hal Rosenstocka9770492005-07-27 11:45:40 -07004110 port_modify.set_port_cap_mask = 0;
4111 port_modify.clr_port_cap_mask = IB_PORT_CM_SUP;
4112 while (--i) {
Michael Wang72219cea2015-05-05 14:50:34 +02004113 if (!rdma_cap_ib_cm(ib_device, i))
Michael Wang091e6a42015-05-05 14:50:21 +02004114 continue;
4115
Sean Hefty9af57b72007-07-16 21:49:35 -07004116 port = cm_dev->port[i-1];
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03004117 ib_modify_port(ib_device, port->port_num, 0, &port_modify);
Hal Rosenstocka9770492005-07-27 11:45:40 -07004118 ib_unregister_mad_agent(port->mad_agent);
Sean Hefty9af57b72007-07-16 21:49:35 -07004119 cm_remove_port_fs(port);
Hal Rosenstocka9770492005-07-27 11:45:40 -07004120 }
Michael Wang091e6a42015-05-05 14:50:21 +02004121free:
Greg Kroah-Hartman110cf372008-05-27 10:17:53 +03004122 device_unregister(cm_dev->device);
Hefty, Seana7e80ce2008-09-30 10:36:54 -07004123 kfree(cm_dev);
Hal Rosenstocka9770492005-07-27 11:45:40 -07004124}
4125
Haggai Eran7c1eb452015-07-30 17:50:14 +03004126static void cm_remove_one(struct ib_device *ib_device, void *client_data)
Hal Rosenstocka9770492005-07-27 11:45:40 -07004127{
Haggai Eran7c1eb452015-07-30 17:50:14 +03004128 struct cm_device *cm_dev = client_data;
Hal Rosenstocka9770492005-07-27 11:45:40 -07004129 struct cm_port *port;
Mark Bloch9db0ff52016-10-27 16:36:27 +03004130 struct cm_id_private *cm_id_priv;
4131 struct ib_mad_agent *cur_mad_agent;
Hal Rosenstocka9770492005-07-27 11:45:40 -07004132 struct ib_port_modify port_modify = {
4133 .clr_port_cap_mask = IB_PORT_CM_SUP
4134 };
4135 unsigned long flags;
4136 int i;
4137
Hal Rosenstocka9770492005-07-27 11:45:40 -07004138 if (!cm_dev)
4139 return;
4140
4141 write_lock_irqsave(&cm.device_lock, flags);
4142 list_del(&cm_dev->list);
4143 write_unlock_irqrestore(&cm.device_lock, flags);
4144
Erez Shitritbe4b4992015-06-25 17:13:22 +03004145 spin_lock_irq(&cm.lock);
4146 cm_dev->going_down = 1;
4147 spin_unlock_irq(&cm.lock);
4148
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03004149 for (i = 1; i <= ib_device->phys_port_cnt; i++) {
Michael Wang72219cea2015-05-05 14:50:34 +02004150 if (!rdma_cap_ib_cm(ib_device, i))
Michael Wang091e6a42015-05-05 14:50:21 +02004151 continue;
4152
Sean Hefty9af57b72007-07-16 21:49:35 -07004153 port = cm_dev->port[i-1];
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03004154 ib_modify_port(ib_device, port->port_num, 0, &port_modify);
Mark Bloch9db0ff52016-10-27 16:36:27 +03004155 /* Mark all the cm_id's as not valid */
4156 spin_lock_irq(&cm.lock);
4157 list_for_each_entry(cm_id_priv, &port->cm_priv_altr_list, altr_list)
4158 cm_id_priv->altr_send_port_not_ready = 1;
4159 list_for_each_entry(cm_id_priv, &port->cm_priv_prim_list, prim_list)
4160 cm_id_priv->prim_send_port_not_ready = 1;
4161 spin_unlock_irq(&cm.lock);
Erez Shitritbe4b4992015-06-25 17:13:22 +03004162 /*
4163 * We flush the queue here after the going_down set, this
4164 * verify that no new works will be queued in the recv handler,
4165 * after that we can call the unregister_mad_agent
4166 */
Sean Hefty84ba2842008-02-22 10:40:45 -08004167 flush_workqueue(cm.wq);
Mark Bloch9db0ff52016-10-27 16:36:27 +03004168 spin_lock_irq(&cm.state_lock);
4169 cur_mad_agent = port->mad_agent;
4170 port->mad_agent = NULL;
4171 spin_unlock_irq(&cm.state_lock);
4172 ib_unregister_mad_agent(cur_mad_agent);
Sean Hefty9af57b72007-07-16 21:49:35 -07004173 cm_remove_port_fs(port);
Hal Rosenstocka9770492005-07-27 11:45:40 -07004174 }
Mark Bloch9db0ff52016-10-27 16:36:27 +03004175
Greg Kroah-Hartman110cf372008-05-27 10:17:53 +03004176 device_unregister(cm_dev->device);
Hefty, Seana7e80ce2008-09-30 10:36:54 -07004177 kfree(cm_dev);
Hal Rosenstocka9770492005-07-27 11:45:40 -07004178}
4179
4180static int __init ib_cm_init(void)
4181{
4182 int ret;
4183
4184 memset(&cm, 0, sizeof cm);
4185 INIT_LIST_HEAD(&cm.device_list);
4186 rwlock_init(&cm.device_lock);
4187 spin_lock_init(&cm.lock);
Mark Bloch9db0ff52016-10-27 16:36:27 +03004188 spin_lock_init(&cm.state_lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07004189 cm.listen_service_table = RB_ROOT;
Harvey Harrison9c3da092009-01-17 17:11:57 -08004190 cm.listen_service_id = be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID);
Hal Rosenstocka9770492005-07-27 11:45:40 -07004191 cm.remote_id_table = RB_ROOT;
4192 cm.remote_qp_table = RB_ROOT;
4193 cm.remote_sidr_table = RB_ROOT;
4194 idr_init(&cm.local_id_table);
Sean Heftyf06d2652006-08-28 15:15:18 -07004195 get_random_bytes(&cm.random_id_operand, sizeof cm.random_id_operand);
Sean Hefty85753292006-10-04 11:29:59 -07004196 INIT_LIST_HEAD(&cm.timewait_list);
Hal Rosenstocka9770492005-07-27 11:45:40 -07004197
Sean Hefty9af57b72007-07-16 21:49:35 -07004198 ret = class_register(&cm_class);
Dotan Barak87d4abd2012-07-11 15:39:31 +00004199 if (ret) {
Sean Hefty9af57b72007-07-16 21:49:35 -07004200 ret = -ENOMEM;
4201 goto error1;
4202 }
4203
Dotan Barak87d4abd2012-07-11 15:39:31 +00004204 cm.wq = create_workqueue("ib_cm");
4205 if (!cm.wq) {
4206 ret = -ENOMEM;
4207 goto error2;
4208 }
4209
Hal Rosenstocka9770492005-07-27 11:45:40 -07004210 ret = ib_register_client(&cm_client);
4211 if (ret)
Dotan Barak87d4abd2012-07-11 15:39:31 +00004212 goto error3;
Hal Rosenstocka9770492005-07-27 11:45:40 -07004213
4214 return 0;
Dotan Barak87d4abd2012-07-11 15:39:31 +00004215error3:
Hal Rosenstocka9770492005-07-27 11:45:40 -07004216 destroy_workqueue(cm.wq);
Dotan Barak87d4abd2012-07-11 15:39:31 +00004217error2:
Sean Hefty9af57b72007-07-16 21:49:35 -07004218 class_unregister(&cm_class);
Dotan Barak87d4abd2012-07-11 15:39:31 +00004219error1:
4220 idr_destroy(&cm.local_id_table);
Hal Rosenstocka9770492005-07-27 11:45:40 -07004221 return ret;
4222}
4223
4224static void __exit ib_cm_cleanup(void)
4225{
Sean Hefty85753292006-10-04 11:29:59 -07004226 struct cm_timewait_info *timewait_info, *tmp;
4227
4228 spin_lock_irq(&cm.lock);
4229 list_for_each_entry(timewait_info, &cm.timewait_list, list)
4230 cancel_delayed_work(&timewait_info->work.work);
4231 spin_unlock_irq(&cm.lock);
4232
Sean Hefty84ba2842008-02-22 10:40:45 -08004233 ib_unregister_client(&cm_client);
Hal Rosenstocka9770492005-07-27 11:45:40 -07004234 destroy_workqueue(cm.wq);
Sean Hefty85753292006-10-04 11:29:59 -07004235
4236 list_for_each_entry_safe(timewait_info, tmp, &cm.timewait_list, list) {
4237 list_del(&timewait_info->list);
4238 kfree(timewait_info);
4239 }
4240
Sean Hefty9af57b72007-07-16 21:49:35 -07004241 class_unregister(&cm_class);
Roland Dreier5d7edb32005-10-24 10:53:25 -07004242 idr_destroy(&cm.local_id_table);
Hal Rosenstocka9770492005-07-27 11:45:40 -07004243}
4244
4245module_init(ib_cm_init);
4246module_exit(ib_cm_cleanup);
4247