blob: 6535f09dc575c1df7f6dc22c04be96ccc65130c1 [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;
231 struct ib_ah_attr ah_attr;
232 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;
244 struct ib_sa_path_rec path[0];
245};
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 }
346 ah = ib_create_ah(mad_agent->qp->pd, &av->ah_attr);
347 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)) {
358 ib_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)) {
393 ib_destroy_ah(ah);
394 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{
Sean Hefty34816ad2005-10-25 10:51:39 -0700403 ib_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
Mark Bloch9db0ff52016-10-27 16:36:27 +0300443static int cm_init_av_by_path(struct ib_sa_path_rec *path, struct cm_av *av,
444 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,
Matan Barakcb57bb82015-12-23 14:56:48 +0200456 path->gid_type, ndev, &p, NULL)) {
Sean Hefty9af57b72007-07-16 21:49:35 -0700457 port = cm_dev->port[p-1];
Hal Rosenstocka9770492005-07-27 11:45:40 -0700458 break;
459 }
460 }
461 read_unlock_irqrestore(&cm.device_lock, flags);
462
Matan Barakc2c6ff12015-10-15 18:38:48 +0300463 if (ndev)
464 dev_put(ndev);
465
Hal Rosenstocka9770492005-07-27 11:45:40 -0700466 if (!port)
467 return -EINVAL;
468
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +0300469 ret = ib_find_cached_pkey(cm_dev->ib_device, port->port_num,
Hal Rosenstocka9770492005-07-27 11:45:40 -0700470 be16_to_cpu(path->pkey), &av->pkey_index);
471 if (ret)
472 return ret;
473
474 av->port = port;
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +0300475 ib_init_ah_from_path(cm_dev->ib_device, port->port_num, path,
Sean Heftyca222c62006-06-17 20:37:40 -0700476 &av->ah_attr);
Sean Hefty1d846122007-06-18 11:09:37 -0700477 av->timeout = path->packet_life_time + 1;
Matan Barakdd5f03b2013-12-12 18:03:11 +0200478
Mark Bloch9db0ff52016-10-27 16:36:27 +0300479 spin_lock_irqsave(&cm.lock, flags);
480 if (&cm_id_priv->av == av)
481 list_add_tail(&cm_id_priv->prim_list, &port->cm_priv_prim_list);
482 else if (&cm_id_priv->alt_av == av)
483 list_add_tail(&cm_id_priv->altr_list, &port->cm_priv_altr_list);
484 else
485 ret = -EINVAL;
486
487 spin_unlock_irqrestore(&cm.lock, flags);
488
489 return ret;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700490}
491
492static int cm_alloc_id(struct cm_id_private *cm_id_priv)
493{
494 unsigned long flags;
Tejun Heo3b069c52013-02-27 17:04:16 -0800495 int id;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700496
Tejun Heo3b069c52013-02-27 17:04:16 -0800497 idr_preload(GFP_KERNEL);
498 spin_lock_irqsave(&cm.lock, flags);
499
Zhao Hongjiangab626d12013-11-15 14:16:37 -0800500 id = idr_alloc_cyclic(&cm.local_id_table, cm_id_priv, 0, 0, GFP_NOWAIT);
Tejun Heo3b069c52013-02-27 17:04:16 -0800501
502 spin_unlock_irqrestore(&cm.lock, flags);
503 idr_preload_end();
Sean Heftyf06d2652006-08-28 15:15:18 -0700504
Al Viro1b90c132008-03-29 03:10:28 +0000505 cm_id_priv->id.local_id = (__force __be32)id ^ cm.random_id_operand;
Tejun Heo3b069c52013-02-27 17:04:16 -0800506 return id < 0 ? id : 0;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700507}
508
Sean Hefty97f52eb2005-08-13 21:05:57 -0700509static void cm_free_id(__be32 local_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700510{
Sean Hefty24be6e82007-06-18 11:09:36 -0700511 spin_lock_irq(&cm.lock);
Sean Heftyf06d2652006-08-28 15:15:18 -0700512 idr_remove(&cm.local_id_table,
513 (__force int) (local_id ^ cm.random_id_operand));
Sean Hefty24be6e82007-06-18 11:09:36 -0700514 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700515}
516
Sean Hefty97f52eb2005-08-13 21:05:57 -0700517static struct cm_id_private * cm_get_id(__be32 local_id, __be32 remote_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700518{
519 struct cm_id_private *cm_id_priv;
520
Sean Heftyf06d2652006-08-28 15:15:18 -0700521 cm_id_priv = idr_find(&cm.local_id_table,
522 (__force int) (local_id ^ cm.random_id_operand));
Hal Rosenstocka9770492005-07-27 11:45:40 -0700523 if (cm_id_priv) {
524 if (cm_id_priv->id.remote_id == remote_id)
525 atomic_inc(&cm_id_priv->refcount);
526 else
527 cm_id_priv = NULL;
528 }
529
530 return cm_id_priv;
531}
532
Sean Hefty97f52eb2005-08-13 21:05:57 -0700533static struct cm_id_private * cm_acquire_id(__be32 local_id, __be32 remote_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700534{
535 struct cm_id_private *cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700536
Sean Hefty24be6e82007-06-18 11:09:36 -0700537 spin_lock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700538 cm_id_priv = cm_get_id(local_id, remote_id);
Sean Hefty24be6e82007-06-18 11:09:36 -0700539 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700540
541 return cm_id_priv;
542}
543
Roland Dreiera88f4882008-04-16 21:01:07 -0700544/*
545 * Trivial helpers to strip endian annotation and compare; the
546 * endianness doesn't actually matter since we just need a stable
547 * order for the RB tree.
548 */
549static int be32_lt(__be32 a, __be32 b)
550{
551 return (__force u32) a < (__force u32) b;
552}
553
554static int be32_gt(__be32 a, __be32 b)
555{
556 return (__force u32) a > (__force u32) b;
557}
558
559static int be64_lt(__be64 a, __be64 b)
560{
561 return (__force u64) a < (__force u64) b;
562}
563
564static int be64_gt(__be64 a, __be64 b)
565{
566 return (__force u64) a > (__force u64) b;
567}
568
Hal Rosenstocka9770492005-07-27 11:45:40 -0700569static struct cm_id_private * cm_insert_listen(struct cm_id_private *cm_id_priv)
570{
571 struct rb_node **link = &cm.listen_service_table.rb_node;
572 struct rb_node *parent = NULL;
573 struct cm_id_private *cur_cm_id_priv;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700574 __be64 service_id = cm_id_priv->id.service_id;
575 __be64 service_mask = cm_id_priv->id.service_mask;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700576
577 while (*link) {
578 parent = *link;
579 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
580 service_node);
581 if ((cur_cm_id_priv->id.service_mask & service_id) ==
Sean Hefty07d357d2005-10-17 15:37:43 -0700582 (service_mask & cur_cm_id_priv->id.service_id) &&
Haggai Eran73fec7f2015-07-30 17:50:26 +0300583 (cm_id_priv->id.device == cur_cm_id_priv->id.device))
Sean Hefty07d357d2005-10-17 15:37:43 -0700584 return cur_cm_id_priv;
585
586 if (cm_id_priv->id.device < cur_cm_id_priv->id.device)
587 link = &(*link)->rb_left;
588 else if (cm_id_priv->id.device > cur_cm_id_priv->id.device)
589 link = &(*link)->rb_right;
Roland Dreiera88f4882008-04-16 21:01:07 -0700590 else if (be64_lt(service_id, cur_cm_id_priv->id.service_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700591 link = &(*link)->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700592 else if (be64_gt(service_id, cur_cm_id_priv->id.service_id))
Sean Hefty6e61d042006-06-17 20:37:28 -0700593 link = &(*link)->rb_right;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700594 else
595 link = &(*link)->rb_right;
596 }
597 rb_link_node(&cm_id_priv->service_node, parent, link);
598 rb_insert_color(&cm_id_priv->service_node, &cm.listen_service_table);
599 return NULL;
600}
601
Sean Hefty07d357d2005-10-17 15:37:43 -0700602static struct cm_id_private * cm_find_listen(struct ib_device *device,
Haggai Eran73fec7f2015-07-30 17:50:26 +0300603 __be64 service_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700604{
605 struct rb_node *node = cm.listen_service_table.rb_node;
606 struct cm_id_private *cm_id_priv;
607
608 while (node) {
609 cm_id_priv = rb_entry(node, struct cm_id_private, service_node);
610 if ((cm_id_priv->id.service_mask & service_id) ==
Sean Hefty07d357d2005-10-17 15:37:43 -0700611 cm_id_priv->id.service_id &&
Haggai Eran73fec7f2015-07-30 17:50:26 +0300612 (cm_id_priv->id.device == device))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700613 return cm_id_priv;
Sean Hefty07d357d2005-10-17 15:37:43 -0700614
615 if (device < cm_id_priv->id.device)
616 node = node->rb_left;
617 else if (device > cm_id_priv->id.device)
618 node = node->rb_right;
Roland Dreiera88f4882008-04-16 21:01:07 -0700619 else if (be64_lt(service_id, cm_id_priv->id.service_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700620 node = node->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700621 else if (be64_gt(service_id, cm_id_priv->id.service_id))
Sean Hefty6e61d042006-06-17 20:37:28 -0700622 node = node->rb_right;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700623 else
624 node = node->rb_right;
625 }
626 return NULL;
627}
628
629static struct cm_timewait_info * cm_insert_remote_id(struct cm_timewait_info
630 *timewait_info)
631{
632 struct rb_node **link = &cm.remote_id_table.rb_node;
633 struct rb_node *parent = NULL;
634 struct cm_timewait_info *cur_timewait_info;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700635 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
636 __be32 remote_id = timewait_info->work.remote_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700637
638 while (*link) {
639 parent = *link;
640 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
641 remote_id_node);
Roland Dreiera88f4882008-04-16 21:01:07 -0700642 if (be32_lt(remote_id, cur_timewait_info->work.remote_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700643 link = &(*link)->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700644 else if (be32_gt(remote_id, cur_timewait_info->work.remote_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700645 link = &(*link)->rb_right;
Roland Dreiera88f4882008-04-16 21:01:07 -0700646 else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700647 link = &(*link)->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700648 else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700649 link = &(*link)->rb_right;
650 else
651 return cur_timewait_info;
652 }
653 timewait_info->inserted_remote_id = 1;
654 rb_link_node(&timewait_info->remote_id_node, parent, link);
655 rb_insert_color(&timewait_info->remote_id_node, &cm.remote_id_table);
656 return NULL;
657}
658
Sean Hefty97f52eb2005-08-13 21:05:57 -0700659static struct cm_timewait_info * cm_find_remote_id(__be64 remote_ca_guid,
660 __be32 remote_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700661{
662 struct rb_node *node = cm.remote_id_table.rb_node;
663 struct cm_timewait_info *timewait_info;
664
665 while (node) {
666 timewait_info = rb_entry(node, struct cm_timewait_info,
667 remote_id_node);
Roland Dreiera88f4882008-04-16 21:01:07 -0700668 if (be32_lt(remote_id, timewait_info->work.remote_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700669 node = node->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700670 else if (be32_gt(remote_id, timewait_info->work.remote_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700671 node = node->rb_right;
Roland Dreiera88f4882008-04-16 21:01:07 -0700672 else if (be64_lt(remote_ca_guid, timewait_info->remote_ca_guid))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700673 node = node->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700674 else if (be64_gt(remote_ca_guid, timewait_info->remote_ca_guid))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700675 node = node->rb_right;
676 else
677 return timewait_info;
678 }
679 return NULL;
680}
681
682static struct cm_timewait_info * cm_insert_remote_qpn(struct cm_timewait_info
683 *timewait_info)
684{
685 struct rb_node **link = &cm.remote_qp_table.rb_node;
686 struct rb_node *parent = NULL;
687 struct cm_timewait_info *cur_timewait_info;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700688 __be64 remote_ca_guid = timewait_info->remote_ca_guid;
689 __be32 remote_qpn = timewait_info->remote_qpn;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700690
691 while (*link) {
692 parent = *link;
693 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
694 remote_qp_node);
Roland Dreiera88f4882008-04-16 21:01:07 -0700695 if (be32_lt(remote_qpn, cur_timewait_info->remote_qpn))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700696 link = &(*link)->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700697 else if (be32_gt(remote_qpn, cur_timewait_info->remote_qpn))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700698 link = &(*link)->rb_right;
Roland Dreiera88f4882008-04-16 21:01:07 -0700699 else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700700 link = &(*link)->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700701 else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700702 link = &(*link)->rb_right;
703 else
704 return cur_timewait_info;
705 }
706 timewait_info->inserted_remote_qp = 1;
707 rb_link_node(&timewait_info->remote_qp_node, parent, link);
708 rb_insert_color(&timewait_info->remote_qp_node, &cm.remote_qp_table);
709 return NULL;
710}
711
712static struct cm_id_private * cm_insert_remote_sidr(struct cm_id_private
713 *cm_id_priv)
714{
715 struct rb_node **link = &cm.remote_sidr_table.rb_node;
716 struct rb_node *parent = NULL;
717 struct cm_id_private *cur_cm_id_priv;
718 union ib_gid *port_gid = &cm_id_priv->av.dgid;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700719 __be32 remote_id = cm_id_priv->id.remote_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700720
721 while (*link) {
722 parent = *link;
723 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
724 sidr_id_node);
Roland Dreiera88f4882008-04-16 21:01:07 -0700725 if (be32_lt(remote_id, cur_cm_id_priv->id.remote_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700726 link = &(*link)->rb_left;
Roland Dreiera88f4882008-04-16 21:01:07 -0700727 else if (be32_gt(remote_id, cur_cm_id_priv->id.remote_id))
Hal Rosenstocka9770492005-07-27 11:45:40 -0700728 link = &(*link)->rb_right;
729 else {
730 int cmp;
731 cmp = memcmp(port_gid, &cur_cm_id_priv->av.dgid,
732 sizeof *port_gid);
733 if (cmp < 0)
734 link = &(*link)->rb_left;
735 else if (cmp > 0)
736 link = &(*link)->rb_right;
737 else
738 return cur_cm_id_priv;
739 }
740 }
741 rb_link_node(&cm_id_priv->sidr_id_node, parent, link);
742 rb_insert_color(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
743 return NULL;
744}
745
746static void cm_reject_sidr_req(struct cm_id_private *cm_id_priv,
747 enum ib_cm_sidr_status status)
748{
749 struct ib_cm_sidr_rep_param param;
750
751 memset(&param, 0, sizeof param);
752 param.status = status;
753 ib_send_cm_sidr_rep(&cm_id_priv->id, &param);
754}
755
Sean Hefty07d357d2005-10-17 15:37:43 -0700756struct ib_cm_id *ib_create_cm_id(struct ib_device *device,
757 ib_cm_handler cm_handler,
Hal Rosenstocka9770492005-07-27 11:45:40 -0700758 void *context)
759{
760 struct cm_id_private *cm_id_priv;
761 int ret;
762
Roland Dreierde6eb662005-11-02 07:23:14 -0800763 cm_id_priv = kzalloc(sizeof *cm_id_priv, GFP_KERNEL);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700764 if (!cm_id_priv)
765 return ERR_PTR(-ENOMEM);
766
Hal Rosenstocka9770492005-07-27 11:45:40 -0700767 cm_id_priv->id.state = IB_CM_IDLE;
Sean Hefty07d357d2005-10-17 15:37:43 -0700768 cm_id_priv->id.device = device;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700769 cm_id_priv->id.cm_handler = cm_handler;
770 cm_id_priv->id.context = context;
John Kingman354ba392005-09-09 18:23:32 -0700771 cm_id_priv->id.remote_cm_qpn = 1;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700772 ret = cm_alloc_id(cm_id_priv);
773 if (ret)
774 goto error;
775
776 spin_lock_init(&cm_id_priv->lock);
Sean Hefty1b52fa982006-05-12 14:57:52 -0700777 init_completion(&cm_id_priv->comp);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700778 INIT_LIST_HEAD(&cm_id_priv->work_list);
Mark Bloch9db0ff52016-10-27 16:36:27 +0300779 INIT_LIST_HEAD(&cm_id_priv->prim_list);
780 INIT_LIST_HEAD(&cm_id_priv->altr_list);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700781 atomic_set(&cm_id_priv->work_count, -1);
782 atomic_set(&cm_id_priv->refcount, 1);
783 return &cm_id_priv->id;
784
785error:
786 kfree(cm_id_priv);
787 return ERR_PTR(-ENOMEM);
788}
789EXPORT_SYMBOL(ib_create_cm_id);
790
791static struct cm_work * cm_dequeue_work(struct cm_id_private *cm_id_priv)
792{
793 struct cm_work *work;
794
795 if (list_empty(&cm_id_priv->work_list))
796 return NULL;
797
798 work = list_entry(cm_id_priv->work_list.next, struct cm_work, list);
799 list_del(&work->list);
800 return work;
801}
802
803static void cm_free_work(struct cm_work *work)
804{
805 if (work->mad_recv_wc)
806 ib_free_recv_mad(work->mad_recv_wc);
807 kfree(work);
808}
809
810static inline int cm_convert_to_ms(int iba_time)
811{
812 /* approximate conversion to ms from 4.096us x 2^iba_time */
813 return 1 << max(iba_time - 8, 0);
814}
815
Sean Hefty1d846122007-06-18 11:09:37 -0700816/*
817 * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time
818 * Because of how ack_timeout is stored, adding one doubles the timeout.
819 * To avoid large timeouts, select the max(ack_delay, life_time + 1), and
820 * increment it (round up) only if the other is within 50%.
821 */
822static u8 cm_ack_timeout(u8 ca_ack_delay, u8 packet_life_time)
823{
824 int ack_timeout = packet_life_time + 1;
825
826 if (ack_timeout >= ca_ack_delay)
827 ack_timeout += (ca_ack_delay >= (ack_timeout - 1));
828 else
829 ack_timeout = ca_ack_delay +
830 (ack_timeout >= (ca_ack_delay - 1));
831
832 return min(31, ack_timeout);
833}
834
Hal Rosenstocka9770492005-07-27 11:45:40 -0700835static void cm_cleanup_timewait(struct cm_timewait_info *timewait_info)
836{
Hal Rosenstocka9770492005-07-27 11:45:40 -0700837 if (timewait_info->inserted_remote_id) {
838 rb_erase(&timewait_info->remote_id_node, &cm.remote_id_table);
839 timewait_info->inserted_remote_id = 0;
840 }
841
842 if (timewait_info->inserted_remote_qp) {
843 rb_erase(&timewait_info->remote_qp_node, &cm.remote_qp_table);
844 timewait_info->inserted_remote_qp = 0;
845 }
Hal Rosenstocka9770492005-07-27 11:45:40 -0700846}
847
Sean Hefty97f52eb2005-08-13 21:05:57 -0700848static struct cm_timewait_info * cm_create_timewait_info(__be32 local_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700849{
850 struct cm_timewait_info *timewait_info;
851
Roland Dreierde6eb662005-11-02 07:23:14 -0800852 timewait_info = kzalloc(sizeof *timewait_info, GFP_KERNEL);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700853 if (!timewait_info)
854 return ERR_PTR(-ENOMEM);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700855
856 timewait_info->work.local_id = local_id;
David Howellsc4028952006-11-22 14:57:56 +0000857 INIT_DELAYED_WORK(&timewait_info->work.work, cm_work_handler);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700858 timewait_info->work.cm_event.event = IB_CM_TIMEWAIT_EXIT;
859 return timewait_info;
860}
861
862static void cm_enter_timewait(struct cm_id_private *cm_id_priv)
863{
864 int wait_time;
Sean Hefty85753292006-10-04 11:29:59 -0700865 unsigned long flags;
Erez Shitritbe4b4992015-06-25 17:13:22 +0300866 struct cm_device *cm_dev;
867
868 cm_dev = ib_get_client_data(cm_id_priv->id.device, &cm_client);
869 if (!cm_dev)
870 return;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700871
Sean Hefty85753292006-10-04 11:29:59 -0700872 spin_lock_irqsave(&cm.lock, flags);
Michael S. Tsirkina70d0592006-08-28 16:32:50 +0300873 cm_cleanup_timewait(cm_id_priv->timewait_info);
Sean Hefty85753292006-10-04 11:29:59 -0700874 list_add_tail(&cm_id_priv->timewait_info->list, &cm.timewait_list);
875 spin_unlock_irqrestore(&cm.lock, flags);
Michael S. Tsirkina70d0592006-08-28 16:32:50 +0300876
Hal Rosenstocka9770492005-07-27 11:45:40 -0700877 /*
878 * The cm_id could be destroyed by the user before we exit timewait.
879 * To protect against this, we search for the cm_id after exiting
880 * timewait before notifying the user that we've exited timewait.
881 */
882 cm_id_priv->id.state = IB_CM_TIMEWAIT;
Sean Hefty1d846122007-06-18 11:09:37 -0700883 wait_time = cm_convert_to_ms(cm_id_priv->av.timeout);
Erez Shitritbe4b4992015-06-25 17:13:22 +0300884
885 /* Check if the device started its remove_one */
Bart Van Assche4bfdf632016-01-01 13:17:46 +0100886 spin_lock_irqsave(&cm.lock, flags);
Erez Shitritbe4b4992015-06-25 17:13:22 +0300887 if (!cm_dev->going_down)
888 queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work,
889 msecs_to_jiffies(wait_time));
Bart Van Assche4bfdf632016-01-01 13:17:46 +0100890 spin_unlock_irqrestore(&cm.lock, flags);
Erez Shitritbe4b4992015-06-25 17:13:22 +0300891
Hal Rosenstocka9770492005-07-27 11:45:40 -0700892 cm_id_priv->timewait_info = NULL;
893}
894
895static void cm_reset_to_idle(struct cm_id_private *cm_id_priv)
896{
Sean Hefty85753292006-10-04 11:29:59 -0700897 unsigned long flags;
898
Hal Rosenstocka9770492005-07-27 11:45:40 -0700899 cm_id_priv->id.state = IB_CM_IDLE;
900 if (cm_id_priv->timewait_info) {
Sean Hefty85753292006-10-04 11:29:59 -0700901 spin_lock_irqsave(&cm.lock, flags);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700902 cm_cleanup_timewait(cm_id_priv->timewait_info);
Sean Hefty85753292006-10-04 11:29:59 -0700903 spin_unlock_irqrestore(&cm.lock, flags);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700904 kfree(cm_id_priv->timewait_info);
905 cm_id_priv->timewait_info = NULL;
906 }
907}
908
Michael S. Tsirkin04c33542006-07-14 00:23:52 -0700909static void cm_destroy_id(struct ib_cm_id *cm_id, int err)
Hal Rosenstocka9770492005-07-27 11:45:40 -0700910{
911 struct cm_id_private *cm_id_priv;
912 struct cm_work *work;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700913
914 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
915retest:
Sean Hefty24be6e82007-06-18 11:09:36 -0700916 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700917 switch (cm_id->state) {
918 case IB_CM_LISTEN:
Sean Hefty24be6e82007-06-18 11:09:36 -0700919 spin_unlock_irq(&cm_id_priv->lock);
Haggai Eran067b1712015-07-30 17:50:18 +0300920
Sean Hefty24be6e82007-06-18 11:09:36 -0700921 spin_lock_irq(&cm.lock);
Haggai Eran067b1712015-07-30 17:50:18 +0300922 if (--cm_id_priv->listen_sharecount > 0) {
923 /* The id is still shared. */
924 cm_deref_id(cm_id_priv);
925 spin_unlock_irq(&cm.lock);
926 return;
927 }
Hal Rosenstocka9770492005-07-27 11:45:40 -0700928 rb_erase(&cm_id_priv->service_node, &cm.listen_service_table);
Sean Hefty24be6e82007-06-18 11:09:36 -0700929 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700930 break;
931 case IB_CM_SIDR_REQ_SENT:
932 cm_id->state = IB_CM_IDLE;
Sean Hefty34816ad2005-10-25 10:51:39 -0700933 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Sean Hefty24be6e82007-06-18 11:09:36 -0700934 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700935 break;
936 case IB_CM_SIDR_REQ_RCVD:
Sean Hefty24be6e82007-06-18 11:09:36 -0700937 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700938 cm_reject_sidr_req(cm_id_priv, IB_SIDR_REJECT);
Doron Tsur0ca81a22015-10-11 15:58:17 +0300939 spin_lock_irq(&cm.lock);
940 if (!RB_EMPTY_NODE(&cm_id_priv->sidr_id_node))
941 rb_erase(&cm_id_priv->sidr_id_node,
942 &cm.remote_sidr_table);
943 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700944 break;
945 case IB_CM_REQ_SENT:
Ted Kimc29ed5a2015-05-14 12:49:01 -0700946 case IB_CM_MRA_REQ_RCVD:
Sean Hefty227eca82005-11-30 10:00:25 -0800947 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Sean Hefty24be6e82007-06-18 11:09:36 -0700948 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty227eca82005-11-30 10:00:25 -0800949 ib_send_cm_rej(cm_id, IB_CM_REJ_TIMEOUT,
Sean Heftye971b8c2007-02-22 11:37:38 -0800950 &cm_id_priv->id.device->node_guid,
951 sizeof cm_id_priv->id.device->node_guid,
Sean Hefty227eca82005-11-30 10:00:25 -0800952 NULL, 0);
953 break;
Michael S. Tsirkin04c33542006-07-14 00:23:52 -0700954 case IB_CM_REQ_RCVD:
955 if (err == -ENOMEM) {
956 /* Do not reject to allow future retries. */
957 cm_reset_to_idle(cm_id_priv);
Sean Hefty24be6e82007-06-18 11:09:36 -0700958 spin_unlock_irq(&cm_id_priv->lock);
Michael S. Tsirkin04c33542006-07-14 00:23:52 -0700959 } else {
Sean Hefty24be6e82007-06-18 11:09:36 -0700960 spin_unlock_irq(&cm_id_priv->lock);
Michael S. Tsirkin04c33542006-07-14 00:23:52 -0700961 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
962 NULL, 0, NULL, 0);
963 }
964 break;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700965 case IB_CM_REP_SENT:
966 case IB_CM_MRA_REP_RCVD:
Sean Hefty34816ad2005-10-25 10:51:39 -0700967 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700968 /* Fall through */
Hal Rosenstocka9770492005-07-27 11:45:40 -0700969 case IB_CM_MRA_REQ_SENT:
970 case IB_CM_REP_RCVD:
971 case IB_CM_MRA_REP_SENT:
Sean Hefty24be6e82007-06-18 11:09:36 -0700972 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty227eca82005-11-30 10:00:25 -0800973 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
974 NULL, 0, NULL, 0);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700975 break;
976 case IB_CM_ESTABLISHED:
Sean Hefty24be6e82007-06-18 11:09:36 -0700977 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty2622e182011-05-30 22:30:46 -0700978 if (cm_id_priv->qp_type == IB_QPT_XRC_TGT)
979 break;
Hal Rosenstocka9770492005-07-27 11:45:40 -0700980 ib_send_cm_dreq(cm_id, NULL, 0);
981 goto retest;
982 case IB_CM_DREQ_SENT:
Sean Hefty34816ad2005-10-25 10:51:39 -0700983 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700984 cm_enter_timewait(cm_id_priv);
Sean Hefty24be6e82007-06-18 11:09:36 -0700985 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700986 break;
987 case IB_CM_DREQ_RCVD:
Sean Hefty24be6e82007-06-18 11:09:36 -0700988 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700989 ib_send_cm_drep(cm_id, NULL, 0);
990 break;
991 default:
Sean Hefty24be6e82007-06-18 11:09:36 -0700992 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -0700993 break;
994 }
995
Mark Bloch9db0ff52016-10-27 16:36:27 +0300996 spin_lock_irq(&cm.lock);
997 if (!list_empty(&cm_id_priv->altr_list) &&
998 (!cm_id_priv->altr_send_port_not_ready))
999 list_del(&cm_id_priv->altr_list);
1000 if (!list_empty(&cm_id_priv->prim_list) &&
1001 (!cm_id_priv->prim_send_port_not_ready))
1002 list_del(&cm_id_priv->prim_list);
1003 spin_unlock_irq(&cm.lock);
1004
Hal Rosenstocka9770492005-07-27 11:45:40 -07001005 cm_free_id(cm_id->local_id);
Sean Hefty1b52fa982006-05-12 14:57:52 -07001006 cm_deref_id(cm_id_priv);
1007 wait_for_completion(&cm_id_priv->comp);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001008 while ((work = cm_dequeue_work(cm_id_priv)) != NULL)
1009 cm_free_work(work);
Sean Hefty6e61d042006-06-17 20:37:28 -07001010 kfree(cm_id_priv->private_data);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001011 kfree(cm_id_priv);
1012}
Michael S. Tsirkin04c33542006-07-14 00:23:52 -07001013
1014void ib_destroy_cm_id(struct ib_cm_id *cm_id)
1015{
1016 cm_destroy_id(cm_id, 0);
1017}
Hal Rosenstocka9770492005-07-27 11:45:40 -07001018EXPORT_SYMBOL(ib_destroy_cm_id);
1019
Haggai Eran067b1712015-07-30 17:50:18 +03001020/**
1021 * __ib_cm_listen - Initiates listening on the specified service ID for
1022 * connection and service ID resolution requests.
1023 * @cm_id: Connection identifier associated with the listen request.
1024 * @service_id: Service identifier matched against incoming connection
1025 * and service ID resolution requests. The service ID should be specified
1026 * network-byte order. If set to IB_CM_ASSIGN_SERVICE_ID, the CM will
1027 * assign a service ID to the caller.
1028 * @service_mask: Mask applied to service ID used to listen across a
1029 * range of service IDs. If set to 0, the service ID is matched
1030 * exactly. This parameter is ignored if %service_id is set to
1031 * IB_CM_ASSIGN_SERVICE_ID.
Haggai Eran067b1712015-07-30 17:50:18 +03001032 */
1033static int __ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id,
Haggai Eran73fec7f2015-07-30 17:50:26 +03001034 __be64 service_mask)
Hal Rosenstocka9770492005-07-27 11:45:40 -07001035{
1036 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001037 int ret = 0;
1038
Harvey Harrison9c3da092009-01-17 17:11:57 -08001039 service_mask = service_mask ? service_mask : ~cpu_to_be64(0);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001040 service_id &= service_mask;
1041 if ((service_id & IB_SERVICE_ID_AGN_MASK) == IB_CM_ASSIGN_SERVICE_ID &&
1042 (service_id != IB_CM_ASSIGN_SERVICE_ID))
1043 return -EINVAL;
1044
1045 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
Sean Hefty6e61d042006-06-17 20:37:28 -07001046 if (cm_id->state != IB_CM_IDLE)
1047 return -EINVAL;
1048
Hal Rosenstocka9770492005-07-27 11:45:40 -07001049 cm_id->state = IB_CM_LISTEN;
Haggai Eran067b1712015-07-30 17:50:18 +03001050 ++cm_id_priv->listen_sharecount;
Haggai Eran73fec7f2015-07-30 17:50:26 +03001051
Hal Rosenstocka9770492005-07-27 11:45:40 -07001052 if (service_id == IB_CM_ASSIGN_SERVICE_ID) {
Sean Hefty97f52eb2005-08-13 21:05:57 -07001053 cm_id->service_id = cpu_to_be64(cm.listen_service_id++);
Harvey Harrison9c3da092009-01-17 17:11:57 -08001054 cm_id->service_mask = ~cpu_to_be64(0);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001055 } else {
1056 cm_id->service_id = service_id;
1057 cm_id->service_mask = service_mask;
1058 }
1059 cur_cm_id_priv = cm_insert_listen(cm_id_priv);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001060
1061 if (cur_cm_id_priv) {
1062 cm_id->state = IB_CM_IDLE;
Haggai Eran067b1712015-07-30 17:50:18 +03001063 --cm_id_priv->listen_sharecount;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001064 ret = -EBUSY;
1065 }
1066 return ret;
1067}
Haggai Eran067b1712015-07-30 17:50:18 +03001068
Haggai Eran73fec7f2015-07-30 17:50:26 +03001069int ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id, __be64 service_mask)
Haggai Eran067b1712015-07-30 17:50:18 +03001070{
Haggai Eran73fec7f2015-07-30 17:50:26 +03001071 unsigned long flags;
1072 int ret;
1073
1074 spin_lock_irqsave(&cm.lock, flags);
1075 ret = __ib_cm_listen(cm_id, service_id, service_mask);
1076 spin_unlock_irqrestore(&cm.lock, flags);
1077
1078 return ret;
Haggai Eran067b1712015-07-30 17:50:18 +03001079}
Hal Rosenstocka9770492005-07-27 11:45:40 -07001080EXPORT_SYMBOL(ib_cm_listen);
1081
Haggai Eran067b1712015-07-30 17:50:18 +03001082/**
1083 * Create a new listening ib_cm_id and listen on the given service ID.
1084 *
1085 * If there's an existing ID listening on that same device and service ID,
1086 * return it.
1087 *
1088 * @device: Device associated with the cm_id. All related communication will
1089 * be associated with the specified device.
1090 * @cm_handler: Callback invoked to notify the user of CM events.
1091 * @service_id: Service identifier matched against incoming connection
1092 * and service ID resolution requests. The service ID should be specified
1093 * network-byte order. If set to IB_CM_ASSIGN_SERVICE_ID, the CM will
1094 * assign a service ID to the caller.
1095 *
1096 * Callers should call ib_destroy_cm_id when done with the listener ID.
1097 */
1098struct ib_cm_id *ib_cm_insert_listen(struct ib_device *device,
1099 ib_cm_handler cm_handler,
1100 __be64 service_id)
1101{
1102 struct cm_id_private *cm_id_priv;
1103 struct ib_cm_id *cm_id;
1104 unsigned long flags;
1105 int err = 0;
1106
1107 /* Create an ID in advance, since the creation may sleep */
1108 cm_id = ib_create_cm_id(device, cm_handler, NULL);
1109 if (IS_ERR(cm_id))
1110 return cm_id;
1111
1112 spin_lock_irqsave(&cm.lock, flags);
1113
1114 if (service_id == IB_CM_ASSIGN_SERVICE_ID)
1115 goto new_id;
1116
1117 /* Find an existing ID */
Haggai Eran73fec7f2015-07-30 17:50:26 +03001118 cm_id_priv = cm_find_listen(device, service_id);
Haggai Eran067b1712015-07-30 17:50:18 +03001119 if (cm_id_priv) {
1120 if (cm_id->cm_handler != cm_handler || cm_id->context) {
1121 /* Sharing an ib_cm_id with different handlers is not
1122 * supported */
1123 spin_unlock_irqrestore(&cm.lock, flags);
1124 return ERR_PTR(-EINVAL);
1125 }
1126 atomic_inc(&cm_id_priv->refcount);
1127 ++cm_id_priv->listen_sharecount;
1128 spin_unlock_irqrestore(&cm.lock, flags);
1129
1130 ib_destroy_cm_id(cm_id);
1131 cm_id = &cm_id_priv->id;
1132 return cm_id;
1133 }
1134
1135new_id:
1136 /* Use newly created ID */
Haggai Eran73fec7f2015-07-30 17:50:26 +03001137 err = __ib_cm_listen(cm_id, service_id, 0);
Haggai Eran067b1712015-07-30 17:50:18 +03001138
1139 spin_unlock_irqrestore(&cm.lock, flags);
1140
1141 if (err) {
1142 ib_destroy_cm_id(cm_id);
1143 return ERR_PTR(err);
1144 }
1145 return cm_id;
1146}
1147EXPORT_SYMBOL(ib_cm_insert_listen);
1148
Sean Hefty97f52eb2005-08-13 21:05:57 -07001149static __be64 cm_form_tid(struct cm_id_private *cm_id_priv,
1150 enum cm_msg_sequence msg_seq)
Hal Rosenstocka9770492005-07-27 11:45:40 -07001151{
1152 u64 hi_tid, low_tid;
1153
1154 hi_tid = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32;
Sean Hefty97f52eb2005-08-13 21:05:57 -07001155 low_tid = (u64) ((__force u32)cm_id_priv->id.local_id |
1156 (msg_seq << 30));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001157 return cpu_to_be64(hi_tid | low_tid);
1158}
1159
1160static void cm_format_mad_hdr(struct ib_mad_hdr *hdr,
Sean Hefty97f52eb2005-08-13 21:05:57 -07001161 __be16 attr_id, __be64 tid)
Hal Rosenstocka9770492005-07-27 11:45:40 -07001162{
1163 hdr->base_version = IB_MGMT_BASE_VERSION;
1164 hdr->mgmt_class = IB_MGMT_CLASS_CM;
1165 hdr->class_version = IB_CM_CLASS_VERSION;
1166 hdr->method = IB_MGMT_METHOD_SEND;
1167 hdr->attr_id = attr_id;
1168 hdr->tid = tid;
1169}
1170
1171static void cm_format_req(struct cm_req_msg *req_msg,
1172 struct cm_id_private *cm_id_priv,
1173 struct ib_cm_req_param *param)
1174{
Sean Hefty3971c9f2007-12-10 15:53:25 -08001175 struct ib_sa_path_rec *pri_path = param->primary_path;
1176 struct ib_sa_path_rec *alt_path = param->alternate_path;
1177
Hal Rosenstocka9770492005-07-27 11:45:40 -07001178 cm_format_mad_hdr(&req_msg->hdr, CM_REQ_ATTR_ID,
1179 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_REQ));
1180
1181 req_msg->local_comm_id = cm_id_priv->id.local_id;
1182 req_msg->service_id = param->service_id;
Sean Heftye971b8c2007-02-22 11:37:38 -08001183 req_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001184 cm_req_set_local_qpn(req_msg, cpu_to_be32(param->qp_num));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001185 cm_req_set_init_depth(req_msg, param->initiator_depth);
1186 cm_req_set_remote_resp_timeout(req_msg,
1187 param->remote_cm_response_timeout);
1188 cm_req_set_qp_type(req_msg, param->qp_type);
1189 cm_req_set_flow_ctrl(req_msg, param->flow_control);
1190 cm_req_set_starting_psn(req_msg, cpu_to_be32(param->starting_psn));
1191 cm_req_set_local_resp_timeout(req_msg,
1192 param->local_cm_response_timeout);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001193 req_msg->pkey = param->primary_path->pkey;
1194 cm_req_set_path_mtu(req_msg, param->primary_path->mtu);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001195 cm_req_set_max_cm_retries(req_msg, param->max_cm_retries);
Sean Heftyd26a3602011-05-13 10:46:20 -07001196
1197 if (param->qp_type != IB_QPT_XRC_INI) {
1198 cm_req_set_resp_res(req_msg, param->responder_resources);
1199 cm_req_set_retry_count(req_msg, param->retry_count);
1200 cm_req_set_rnr_retry_count(req_msg, param->rnr_retry_count);
1201 cm_req_set_srq(req_msg, param->srq);
1202 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07001203
Sean Hefty3971c9f2007-12-10 15:53:25 -08001204 if (pri_path->hop_limit <= 1) {
1205 req_msg->primary_local_lid = pri_path->slid;
1206 req_msg->primary_remote_lid = pri_path->dlid;
1207 } else {
1208 /* Work-around until there's a way to obtain remote LID info */
1209 req_msg->primary_local_lid = IB_LID_PERMISSIVE;
1210 req_msg->primary_remote_lid = IB_LID_PERMISSIVE;
1211 }
1212 req_msg->primary_local_gid = pri_path->sgid;
1213 req_msg->primary_remote_gid = pri_path->dgid;
1214 cm_req_set_primary_flow_label(req_msg, pri_path->flow_label);
1215 cm_req_set_primary_packet_rate(req_msg, pri_path->rate);
1216 req_msg->primary_traffic_class = pri_path->traffic_class;
1217 req_msg->primary_hop_limit = pri_path->hop_limit;
1218 cm_req_set_primary_sl(req_msg, pri_path->sl);
1219 cm_req_set_primary_subnet_local(req_msg, (pri_path->hop_limit <= 1));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001220 cm_req_set_primary_local_ack_timeout(req_msg,
Sean Hefty1d846122007-06-18 11:09:37 -07001221 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
Sean Hefty3971c9f2007-12-10 15:53:25 -08001222 pri_path->packet_life_time));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001223
Sean Hefty3971c9f2007-12-10 15:53:25 -08001224 if (alt_path) {
1225 if (alt_path->hop_limit <= 1) {
1226 req_msg->alt_local_lid = alt_path->slid;
1227 req_msg->alt_remote_lid = alt_path->dlid;
1228 } else {
1229 req_msg->alt_local_lid = IB_LID_PERMISSIVE;
1230 req_msg->alt_remote_lid = IB_LID_PERMISSIVE;
1231 }
1232 req_msg->alt_local_gid = alt_path->sgid;
1233 req_msg->alt_remote_gid = alt_path->dgid;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001234 cm_req_set_alt_flow_label(req_msg,
Sean Hefty3971c9f2007-12-10 15:53:25 -08001235 alt_path->flow_label);
1236 cm_req_set_alt_packet_rate(req_msg, alt_path->rate);
1237 req_msg->alt_traffic_class = alt_path->traffic_class;
1238 req_msg->alt_hop_limit = alt_path->hop_limit;
1239 cm_req_set_alt_sl(req_msg, alt_path->sl);
1240 cm_req_set_alt_subnet_local(req_msg, (alt_path->hop_limit <= 1));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001241 cm_req_set_alt_local_ack_timeout(req_msg,
Sean Hefty1d846122007-06-18 11:09:37 -07001242 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
Sean Hefty3971c9f2007-12-10 15:53:25 -08001243 alt_path->packet_life_time));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001244 }
1245
1246 if (param->private_data && param->private_data_len)
1247 memcpy(req_msg->private_data, param->private_data,
1248 param->private_data_len);
1249}
1250
Arjan van de Ven858119e2006-01-14 13:20:43 -08001251static int cm_validate_req_param(struct ib_cm_req_param *param)
Hal Rosenstocka9770492005-07-27 11:45:40 -07001252{
1253 /* peer-to-peer not supported */
1254 if (param->peer_to_peer)
1255 return -EINVAL;
1256
1257 if (!param->primary_path)
1258 return -EINVAL;
1259
Sean Heftyd26a3602011-05-13 10:46:20 -07001260 if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC &&
1261 param->qp_type != IB_QPT_XRC_INI)
Hal Rosenstocka9770492005-07-27 11:45:40 -07001262 return -EINVAL;
1263
1264 if (param->private_data &&
1265 param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE)
1266 return -EINVAL;
1267
1268 if (param->alternate_path &&
1269 (param->alternate_path->pkey != param->primary_path->pkey ||
1270 param->alternate_path->mtu != param->primary_path->mtu))
1271 return -EINVAL;
1272
1273 return 0;
1274}
1275
1276int ib_send_cm_req(struct ib_cm_id *cm_id,
1277 struct ib_cm_req_param *param)
1278{
1279 struct cm_id_private *cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001280 struct cm_req_msg *req_msg;
1281 unsigned long flags;
1282 int ret;
1283
1284 ret = cm_validate_req_param(param);
1285 if (ret)
1286 return ret;
1287
1288 /* Verify that we're not in timewait. */
1289 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1290 spin_lock_irqsave(&cm_id_priv->lock, flags);
1291 if (cm_id->state != IB_CM_IDLE) {
1292 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1293 ret = -EINVAL;
1294 goto out;
1295 }
1296 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1297
1298 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1299 id.local_id);
Sean Hefty75df23e2006-07-25 19:52:01 +03001300 if (IS_ERR(cm_id_priv->timewait_info)) {
1301 ret = PTR_ERR(cm_id_priv->timewait_info);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001302 goto out;
Sean Hefty75df23e2006-07-25 19:52:01 +03001303 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07001304
Mark Bloch9db0ff52016-10-27 16:36:27 +03001305 ret = cm_init_av_by_path(param->primary_path, &cm_id_priv->av,
1306 cm_id_priv);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001307 if (ret)
1308 goto error1;
1309 if (param->alternate_path) {
1310 ret = cm_init_av_by_path(param->alternate_path,
Mark Bloch9db0ff52016-10-27 16:36:27 +03001311 &cm_id_priv->alt_av, cm_id_priv);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001312 if (ret)
1313 goto error1;
1314 }
1315 cm_id->service_id = param->service_id;
Harvey Harrison9c3da092009-01-17 17:11:57 -08001316 cm_id->service_mask = ~cpu_to_be64(0);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001317 cm_id_priv->timeout_ms = cm_convert_to_ms(
1318 param->primary_path->packet_life_time) * 2 +
1319 cm_convert_to_ms(
1320 param->remote_cm_response_timeout);
1321 cm_id_priv->max_cm_retries = param->max_cm_retries;
1322 cm_id_priv->initiator_depth = param->initiator_depth;
1323 cm_id_priv->responder_resources = param->responder_resources;
1324 cm_id_priv->retry_count = param->retry_count;
1325 cm_id_priv->path_mtu = param->primary_path->mtu;
Sean Heftye1444b52006-11-28 14:57:13 -08001326 cm_id_priv->pkey = param->primary_path->pkey;
Sean Heftyae7971a2005-10-24 12:33:56 -07001327 cm_id_priv->qp_type = param->qp_type;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001328
1329 ret = cm_alloc_msg(cm_id_priv, &cm_id_priv->msg);
1330 if (ret)
1331 goto error1;
1332
1333 req_msg = (struct cm_req_msg *) cm_id_priv->msg->mad;
1334 cm_format_req(req_msg, cm_id_priv, param);
1335 cm_id_priv->tid = req_msg->hdr.tid;
Sean Hefty34816ad2005-10-25 10:51:39 -07001336 cm_id_priv->msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001337 cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT;
1338
1339 cm_id_priv->local_qpn = cm_req_get_local_qpn(req_msg);
1340 cm_id_priv->rq_psn = cm_req_get_starting_psn(req_msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001341
1342 spin_lock_irqsave(&cm_id_priv->lock, flags);
Sean Hefty34816ad2005-10-25 10:51:39 -07001343 ret = ib_post_send_mad(cm_id_priv->msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001344 if (ret) {
1345 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1346 goto error2;
1347 }
1348 BUG_ON(cm_id->state != IB_CM_IDLE);
1349 cm_id->state = IB_CM_REQ_SENT;
1350 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1351 return 0;
1352
1353error2: cm_free_msg(cm_id_priv->msg);
1354error1: kfree(cm_id_priv->timewait_info);
1355out: return ret;
1356}
1357EXPORT_SYMBOL(ib_send_cm_req);
1358
1359static int cm_issue_rej(struct cm_port *port,
1360 struct ib_mad_recv_wc *mad_recv_wc,
1361 enum ib_cm_rej_reason reason,
1362 enum cm_msg_response msg_rejected,
1363 void *ari, u8 ari_length)
1364{
1365 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001366 struct cm_rej_msg *rej_msg, *rcv_msg;
1367 int ret;
1368
1369 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
1370 if (ret)
1371 return ret;
1372
1373 /* We just need common CM header information. Cast to any message. */
1374 rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad;
1375 rej_msg = (struct cm_rej_msg *) msg->mad;
1376
1377 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid);
1378 rej_msg->remote_comm_id = rcv_msg->local_comm_id;
1379 rej_msg->local_comm_id = rcv_msg->remote_comm_id;
1380 cm_rej_set_msg_rejected(rej_msg, msg_rejected);
Sean Hefty97f52eb2005-08-13 21:05:57 -07001381 rej_msg->reason = cpu_to_be16(reason);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001382
1383 if (ari && ari_length) {
1384 cm_rej_set_reject_info_len(rej_msg, ari_length);
1385 memcpy(rej_msg->ari, ari, ari_length);
1386 }
1387
Sean Hefty34816ad2005-10-25 10:51:39 -07001388 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001389 if (ret)
1390 cm_free_msg(msg);
1391
1392 return ret;
1393}
1394
Sean Hefty97f52eb2005-08-13 21:05:57 -07001395static inline int cm_is_active_peer(__be64 local_ca_guid, __be64 remote_ca_guid,
1396 __be32 local_qpn, __be32 remote_qpn)
Hal Rosenstocka9770492005-07-27 11:45:40 -07001397{
1398 return (be64_to_cpu(local_ca_guid) > be64_to_cpu(remote_ca_guid) ||
1399 ((local_ca_guid == remote_ca_guid) &&
1400 (be32_to_cpu(local_qpn) > be32_to_cpu(remote_qpn))));
1401}
1402
Arjan van de Ven858119e2006-01-14 13:20:43 -08001403static void cm_format_paths_from_req(struct cm_req_msg *req_msg,
Hal Rosenstocka9770492005-07-27 11:45:40 -07001404 struct ib_sa_path_rec *primary_path,
1405 struct ib_sa_path_rec *alt_path)
1406{
1407 memset(primary_path, 0, sizeof *primary_path);
1408 primary_path->dgid = req_msg->primary_local_gid;
1409 primary_path->sgid = req_msg->primary_remote_gid;
1410 primary_path->dlid = req_msg->primary_local_lid;
1411 primary_path->slid = req_msg->primary_remote_lid;
1412 primary_path->flow_label = cm_req_get_primary_flow_label(req_msg);
1413 primary_path->hop_limit = req_msg->primary_hop_limit;
1414 primary_path->traffic_class = req_msg->primary_traffic_class;
1415 primary_path->reversible = 1;
1416 primary_path->pkey = req_msg->pkey;
1417 primary_path->sl = cm_req_get_primary_sl(req_msg);
1418 primary_path->mtu_selector = IB_SA_EQ;
1419 primary_path->mtu = cm_req_get_path_mtu(req_msg);
1420 primary_path->rate_selector = IB_SA_EQ;
1421 primary_path->rate = cm_req_get_primary_packet_rate(req_msg);
1422 primary_path->packet_life_time_selector = IB_SA_EQ;
1423 primary_path->packet_life_time =
1424 cm_req_get_primary_local_ack_timeout(req_msg);
1425 primary_path->packet_life_time -= (primary_path->packet_life_time > 0);
Haggai Eran15865e7d2015-07-30 17:50:17 +03001426 primary_path->service_id = req_msg->service_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001427
1428 if (req_msg->alt_local_lid) {
1429 memset(alt_path, 0, sizeof *alt_path);
1430 alt_path->dgid = req_msg->alt_local_gid;
1431 alt_path->sgid = req_msg->alt_remote_gid;
1432 alt_path->dlid = req_msg->alt_local_lid;
1433 alt_path->slid = req_msg->alt_remote_lid;
1434 alt_path->flow_label = cm_req_get_alt_flow_label(req_msg);
1435 alt_path->hop_limit = req_msg->alt_hop_limit;
1436 alt_path->traffic_class = req_msg->alt_traffic_class;
1437 alt_path->reversible = 1;
1438 alt_path->pkey = req_msg->pkey;
1439 alt_path->sl = cm_req_get_alt_sl(req_msg);
1440 alt_path->mtu_selector = IB_SA_EQ;
1441 alt_path->mtu = cm_req_get_path_mtu(req_msg);
1442 alt_path->rate_selector = IB_SA_EQ;
1443 alt_path->rate = cm_req_get_alt_packet_rate(req_msg);
1444 alt_path->packet_life_time_selector = IB_SA_EQ;
1445 alt_path->packet_life_time =
1446 cm_req_get_alt_local_ack_timeout(req_msg);
1447 alt_path->packet_life_time -= (alt_path->packet_life_time > 0);
Haggai Eran15865e7d2015-07-30 17:50:17 +03001448 alt_path->service_id = req_msg->service_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001449 }
1450}
1451
Haggai Eran24cad9a2015-07-30 17:50:21 +03001452static u16 cm_get_bth_pkey(struct cm_work *work)
1453{
1454 struct ib_device *ib_dev = work->port->cm_dev->ib_device;
1455 u8 port_num = work->port->port_num;
1456 u16 pkey_index = work->mad_recv_wc->wc->pkey_index;
1457 u16 pkey;
1458 int ret;
1459
1460 ret = ib_get_cached_pkey(ib_dev, port_num, pkey_index, &pkey);
1461 if (ret) {
1462 dev_warn_ratelimited(&ib_dev->dev, "ib_cm: Couldn't retrieve pkey for incoming request (port %d, pkey index %d). %d\n",
1463 port_num, pkey_index, ret);
1464 return 0;
1465 }
1466
1467 return pkey;
1468}
1469
Hal Rosenstocka9770492005-07-27 11:45:40 -07001470static void cm_format_req_event(struct cm_work *work,
1471 struct cm_id_private *cm_id_priv,
1472 struct ib_cm_id *listen_id)
1473{
1474 struct cm_req_msg *req_msg;
1475 struct ib_cm_req_event_param *param;
1476
1477 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1478 param = &work->cm_event.param.req_rcvd;
1479 param->listen_id = listen_id;
Haggai Eran24cad9a2015-07-30 17:50:21 +03001480 param->bth_pkey = cm_get_bth_pkey(work);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001481 param->port = cm_id_priv->av.port->port_num;
1482 param->primary_path = &work->path[0];
1483 if (req_msg->alt_local_lid)
1484 param->alternate_path = &work->path[1];
1485 else
1486 param->alternate_path = NULL;
1487 param->remote_ca_guid = req_msg->local_ca_guid;
1488 param->remote_qkey = be32_to_cpu(req_msg->local_qkey);
1489 param->remote_qpn = be32_to_cpu(cm_req_get_local_qpn(req_msg));
1490 param->qp_type = cm_req_get_qp_type(req_msg);
1491 param->starting_psn = be32_to_cpu(cm_req_get_starting_psn(req_msg));
1492 param->responder_resources = cm_req_get_init_depth(req_msg);
1493 param->initiator_depth = cm_req_get_resp_res(req_msg);
1494 param->local_cm_response_timeout =
1495 cm_req_get_remote_resp_timeout(req_msg);
1496 param->flow_control = cm_req_get_flow_ctrl(req_msg);
1497 param->remote_cm_response_timeout =
1498 cm_req_get_local_resp_timeout(req_msg);
1499 param->retry_count = cm_req_get_retry_count(req_msg);
1500 param->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1501 param->srq = cm_req_get_srq(req_msg);
1502 work->cm_event.private_data = &req_msg->private_data;
1503}
1504
1505static void cm_process_work(struct cm_id_private *cm_id_priv,
1506 struct cm_work *work)
1507{
Hal Rosenstocka9770492005-07-27 11:45:40 -07001508 int ret;
1509
1510 /* We will typically only have the current event to report. */
1511 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
1512 cm_free_work(work);
1513
1514 while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) {
Sean Hefty24be6e82007-06-18 11:09:36 -07001515 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001516 work = cm_dequeue_work(cm_id_priv);
Sean Hefty24be6e82007-06-18 11:09:36 -07001517 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001518 BUG_ON(!work);
1519 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id,
1520 &work->cm_event);
1521 cm_free_work(work);
1522 }
1523 cm_deref_id(cm_id_priv);
1524 if (ret)
Michael S. Tsirkin04c33542006-07-14 00:23:52 -07001525 cm_destroy_id(&cm_id_priv->id, ret);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001526}
1527
1528static void cm_format_mra(struct cm_mra_msg *mra_msg,
1529 struct cm_id_private *cm_id_priv,
1530 enum cm_msg_response msg_mraed, u8 service_timeout,
1531 const void *private_data, u8 private_data_len)
1532{
1533 cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid);
1534 cm_mra_set_msg_mraed(mra_msg, msg_mraed);
1535 mra_msg->local_comm_id = cm_id_priv->id.local_id;
1536 mra_msg->remote_comm_id = cm_id_priv->id.remote_id;
1537 cm_mra_set_service_timeout(mra_msg, service_timeout);
1538
1539 if (private_data && private_data_len)
1540 memcpy(mra_msg->private_data, private_data, private_data_len);
1541}
1542
1543static void cm_format_rej(struct cm_rej_msg *rej_msg,
1544 struct cm_id_private *cm_id_priv,
1545 enum ib_cm_rej_reason reason,
1546 void *ari,
1547 u8 ari_length,
1548 const void *private_data,
1549 u8 private_data_len)
1550{
1551 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid);
1552 rej_msg->remote_comm_id = cm_id_priv->id.remote_id;
1553
1554 switch(cm_id_priv->id.state) {
1555 case IB_CM_REQ_RCVD:
1556 rej_msg->local_comm_id = 0;
1557 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1558 break;
1559 case IB_CM_MRA_REQ_SENT:
1560 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1561 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1562 break;
1563 case IB_CM_REP_RCVD:
1564 case IB_CM_MRA_REP_SENT:
1565 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1566 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REP);
1567 break;
1568 default:
1569 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1570 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_OTHER);
1571 break;
1572 }
1573
Sean Hefty97f52eb2005-08-13 21:05:57 -07001574 rej_msg->reason = cpu_to_be16(reason);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001575 if (ari && ari_length) {
1576 cm_rej_set_reject_info_len(rej_msg, ari_length);
1577 memcpy(rej_msg->ari, ari, ari_length);
1578 }
1579
1580 if (private_data && private_data_len)
1581 memcpy(rej_msg->private_data, private_data, private_data_len);
1582}
1583
1584static void cm_dup_req_handler(struct cm_work *work,
1585 struct cm_id_private *cm_id_priv)
1586{
1587 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001588 int ret;
1589
Sean Hefty9af57b72007-07-16 21:49:35 -07001590 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
1591 counter[CM_REQ_COUNTER]);
1592
Hal Rosenstocka9770492005-07-27 11:45:40 -07001593 /* Quick state check to discard duplicate REQs. */
1594 if (cm_id_priv->id.state == IB_CM_REQ_RCVD)
1595 return;
1596
1597 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1598 if (ret)
1599 return;
1600
Sean Hefty24be6e82007-06-18 11:09:36 -07001601 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001602 switch (cm_id_priv->id.state) {
1603 case IB_CM_MRA_REQ_SENT:
1604 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1605 CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout,
1606 cm_id_priv->private_data,
1607 cm_id_priv->private_data_len);
1608 break;
1609 case IB_CM_TIMEWAIT:
1610 cm_format_rej((struct cm_rej_msg *) msg->mad, cm_id_priv,
1611 IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0);
1612 break;
1613 default:
1614 goto unlock;
1615 }
Sean Hefty24be6e82007-06-18 11:09:36 -07001616 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001617
Sean Hefty34816ad2005-10-25 10:51:39 -07001618 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001619 if (ret)
1620 goto free;
1621 return;
1622
Sean Hefty24be6e82007-06-18 11:09:36 -07001623unlock: spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001624free: cm_free_msg(msg);
1625}
1626
1627static struct cm_id_private * cm_match_req(struct cm_work *work,
1628 struct cm_id_private *cm_id_priv)
1629{
1630 struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv;
1631 struct cm_timewait_info *timewait_info;
1632 struct cm_req_msg *req_msg;
Hans Westgaard Ry9315bc92016-10-28 13:14:29 +02001633 struct ib_cm_id *cm_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001634
1635 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1636
Sean Heftyd998ccc2007-05-21 17:38:02 -07001637 /* Check for possible duplicate REQ. */
Sean Hefty24be6e82007-06-18 11:09:36 -07001638 spin_lock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001639 timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001640 if (timewait_info) {
1641 cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
1642 timewait_info->work.remote_id);
Sean Hefty24be6e82007-06-18 11:09:36 -07001643 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001644 if (cur_cm_id_priv) {
1645 cm_dup_req_handler(work, cur_cm_id_priv);
1646 cm_deref_id(cur_cm_id_priv);
Sean Heftyd998ccc2007-05-21 17:38:02 -07001647 }
1648 return NULL;
1649 }
1650
1651 /* Check for stale connections. */
1652 timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
1653 if (timewait_info) {
1654 cm_cleanup_timewait(cm_id_priv->timewait_info);
Hans Westgaard Ry9315bc92016-10-28 13:14:29 +02001655 cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
1656 timewait_info->work.remote_id);
1657
Sean Hefty24be6e82007-06-18 11:09:36 -07001658 spin_unlock_irq(&cm.lock);
Sean Heftyd998ccc2007-05-21 17:38:02 -07001659 cm_issue_rej(work->port, work->mad_recv_wc,
1660 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ,
1661 NULL, 0);
Hans Westgaard Ry9315bc92016-10-28 13:14:29 +02001662 if (cur_cm_id_priv) {
1663 cm_id = &cur_cm_id_priv->id;
1664 ib_send_cm_dreq(cm_id, NULL, 0);
1665 cm_deref_id(cur_cm_id_priv);
1666 }
Sean Heftyd998ccc2007-05-21 17:38:02 -07001667 return NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001668 }
1669
1670 /* Find matching listen request. */
Sean Hefty07d357d2005-10-17 15:37:43 -07001671 listen_cm_id_priv = cm_find_listen(cm_id_priv->id.device,
Haggai Eran73fec7f2015-07-30 17:50:26 +03001672 req_msg->service_id);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001673 if (!listen_cm_id_priv) {
Sean Hefty85753292006-10-04 11:29:59 -07001674 cm_cleanup_timewait(cm_id_priv->timewait_info);
Sean Hefty24be6e82007-06-18 11:09:36 -07001675 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001676 cm_issue_rej(work->port, work->mad_recv_wc,
1677 IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ,
1678 NULL, 0);
Sean Hefty85753292006-10-04 11:29:59 -07001679 goto out;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001680 }
1681 atomic_inc(&listen_cm_id_priv->refcount);
1682 atomic_inc(&cm_id_priv->refcount);
1683 cm_id_priv->id.state = IB_CM_REQ_RCVD;
1684 atomic_inc(&cm_id_priv->work_count);
Sean Hefty24be6e82007-06-18 11:09:36 -07001685 spin_unlock_irq(&cm.lock);
Sean Hefty85753292006-10-04 11:29:59 -07001686out:
Hal Rosenstocka9770492005-07-27 11:45:40 -07001687 return listen_cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001688}
1689
Sean Hefty3971c9f2007-12-10 15:53:25 -08001690/*
1691 * Work-around for inter-subnet connections. If the LIDs are permissive,
1692 * we need to override the LID/SL data in the REQ with the LID information
1693 * in the work completion.
1694 */
1695static void cm_process_routed_req(struct cm_req_msg *req_msg, struct ib_wc *wc)
1696{
1697 if (!cm_req_get_primary_subnet_local(req_msg)) {
1698 if (req_msg->primary_local_lid == IB_LID_PERMISSIVE) {
1699 req_msg->primary_local_lid = cpu_to_be16(wc->slid);
1700 cm_req_set_primary_sl(req_msg, wc->sl);
1701 }
1702
1703 if (req_msg->primary_remote_lid == IB_LID_PERMISSIVE)
1704 req_msg->primary_remote_lid = cpu_to_be16(wc->dlid_path_bits);
1705 }
1706
1707 if (!cm_req_get_alt_subnet_local(req_msg)) {
1708 if (req_msg->alt_local_lid == IB_LID_PERMISSIVE) {
1709 req_msg->alt_local_lid = cpu_to_be16(wc->slid);
1710 cm_req_set_alt_sl(req_msg, wc->sl);
1711 }
1712
1713 if (req_msg->alt_remote_lid == IB_LID_PERMISSIVE)
1714 req_msg->alt_remote_lid = cpu_to_be16(wc->dlid_path_bits);
1715 }
1716}
1717
Hal Rosenstocka9770492005-07-27 11:45:40 -07001718static int cm_req_handler(struct cm_work *work)
1719{
1720 struct ib_cm_id *cm_id;
1721 struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
1722 struct cm_req_msg *req_msg;
Matan Barakcb57bb82015-12-23 14:56:48 +02001723 union ib_gid gid;
1724 struct ib_gid_attr gid_attr;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001725 int ret;
1726
1727 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1728
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03001729 cm_id = ib_create_cm_id(work->port->cm_dev->ib_device, NULL, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001730 if (IS_ERR(cm_id))
1731 return PTR_ERR(cm_id);
1732
1733 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1734 cm_id_priv->id.remote_id = req_msg->local_comm_id;
1735 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
Sean Heftyca222c62006-06-17 20:37:40 -07001736 work->mad_recv_wc->recv_buf.grh,
Hal Rosenstocka9770492005-07-27 11:45:40 -07001737 &cm_id_priv->av);
1738 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1739 id.local_id);
1740 if (IS_ERR(cm_id_priv->timewait_info)) {
1741 ret = PTR_ERR(cm_id_priv->timewait_info);
Sean Hefty76842402006-08-28 11:57:42 -07001742 goto destroy;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001743 }
1744 cm_id_priv->timewait_info->work.remote_id = req_msg->local_comm_id;
1745 cm_id_priv->timewait_info->remote_ca_guid = req_msg->local_ca_guid;
1746 cm_id_priv->timewait_info->remote_qpn = cm_req_get_local_qpn(req_msg);
1747
1748 listen_cm_id_priv = cm_match_req(work, cm_id_priv);
1749 if (!listen_cm_id_priv) {
1750 ret = -EINVAL;
Sean Hefty76842402006-08-28 11:57:42 -07001751 kfree(cm_id_priv->timewait_info);
1752 goto destroy;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001753 }
1754
1755 cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
1756 cm_id_priv->id.context = listen_cm_id_priv->id.context;
1757 cm_id_priv->id.service_id = req_msg->service_id;
Harvey Harrison9c3da092009-01-17 17:11:57 -08001758 cm_id_priv->id.service_mask = ~cpu_to_be64(0);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001759
Sean Hefty3971c9f2007-12-10 15:53:25 -08001760 cm_process_routed_req(req_msg, work->mad_recv_wc->wc);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001761 cm_format_paths_from_req(req_msg, &work->path[0], &work->path[1]);
Matan Barakdd5f03b2013-12-12 18:03:11 +02001762
1763 memcpy(work->path[0].dmac, cm_id_priv->av.ah_attr.dmac, ETH_ALEN);
Matan Barakc3efe752016-01-04 10:49:54 +02001764 work->path[0].hop_limit = cm_id_priv->av.ah_attr.grh.hop_limit;
Matan Barakcb57bb82015-12-23 14:56:48 +02001765 ret = ib_get_cached_gid(work->port->cm_dev->ib_device,
1766 work->port->port_num,
1767 cm_id_priv->av.ah_attr.grh.sgid_index,
1768 &gid, &gid_attr);
1769 if (!ret) {
Matan Barak20029832015-12-23 14:56:53 +02001770 if (gid_attr.ndev) {
1771 work->path[0].ifindex = gid_attr.ndev->ifindex;
1772 work->path[0].net = dev_net(gid_attr.ndev);
Matan Barakcb57bb82015-12-23 14:56:48 +02001773 dev_put(gid_attr.ndev);
Matan Barak20029832015-12-23 14:56:53 +02001774 }
Matan Barakcb57bb82015-12-23 14:56:48 +02001775 work->path[0].gid_type = gid_attr.gid_type;
Mark Bloch9db0ff52016-10-27 16:36:27 +03001776 ret = cm_init_av_by_path(&work->path[0], &cm_id_priv->av,
1777 cm_id_priv);
Matan Barakcb57bb82015-12-23 14:56:48 +02001778 }
Sean Hefty76842402006-08-28 11:57:42 -07001779 if (ret) {
Matan Barakcb57bb82015-12-23 14:56:48 +02001780 int err = ib_get_cached_gid(work->port->cm_dev->ib_device,
1781 work->port->port_num, 0,
1782 &work->path[0].sgid,
1783 &gid_attr);
Matan Barak20029832015-12-23 14:56:53 +02001784 if (!err && gid_attr.ndev) {
1785 work->path[0].ifindex = gid_attr.ndev->ifindex;
1786 work->path[0].net = dev_net(gid_attr.ndev);
Matan Barakcb57bb82015-12-23 14:56:48 +02001787 dev_put(gid_attr.ndev);
Matan Barak20029832015-12-23 14:56:53 +02001788 }
Matan Barakcb57bb82015-12-23 14:56:48 +02001789 work->path[0].gid_type = gid_attr.gid_type;
Sean Hefty76842402006-08-28 11:57:42 -07001790 ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_GID,
1791 &work->path[0].sgid, sizeof work->path[0].sgid,
1792 NULL, 0);
1793 goto rejected;
1794 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07001795 if (req_msg->alt_local_lid) {
Mark Bloch9db0ff52016-10-27 16:36:27 +03001796 ret = cm_init_av_by_path(&work->path[1], &cm_id_priv->alt_av,
1797 cm_id_priv);
Sean Hefty76842402006-08-28 11:57:42 -07001798 if (ret) {
1799 ib_send_cm_rej(cm_id, IB_CM_REJ_INVALID_ALT_GID,
1800 &work->path[0].sgid,
1801 sizeof work->path[0].sgid, NULL, 0);
1802 goto rejected;
1803 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07001804 }
1805 cm_id_priv->tid = req_msg->hdr.tid;
1806 cm_id_priv->timeout_ms = cm_convert_to_ms(
1807 cm_req_get_local_resp_timeout(req_msg));
1808 cm_id_priv->max_cm_retries = cm_req_get_max_cm_retries(req_msg);
1809 cm_id_priv->remote_qpn = cm_req_get_local_qpn(req_msg);
1810 cm_id_priv->initiator_depth = cm_req_get_resp_res(req_msg);
1811 cm_id_priv->responder_resources = cm_req_get_init_depth(req_msg);
1812 cm_id_priv->path_mtu = cm_req_get_path_mtu(req_msg);
Sean Heftye1444b52006-11-28 14:57:13 -08001813 cm_id_priv->pkey = req_msg->pkey;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001814 cm_id_priv->sq_psn = cm_req_get_starting_psn(req_msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001815 cm_id_priv->retry_count = cm_req_get_retry_count(req_msg);
1816 cm_id_priv->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
Sean Heftyae7971a2005-10-24 12:33:56 -07001817 cm_id_priv->qp_type = cm_req_get_qp_type(req_msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001818
1819 cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
1820 cm_process_work(cm_id_priv, work);
1821 cm_deref_id(listen_cm_id_priv);
1822 return 0;
1823
Sean Hefty76842402006-08-28 11:57:42 -07001824rejected:
1825 atomic_dec(&cm_id_priv->refcount);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001826 cm_deref_id(listen_cm_id_priv);
Sean Hefty76842402006-08-28 11:57:42 -07001827destroy:
1828 ib_destroy_cm_id(cm_id);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001829 return ret;
1830}
1831
1832static void cm_format_rep(struct cm_rep_msg *rep_msg,
1833 struct cm_id_private *cm_id_priv,
1834 struct ib_cm_rep_param *param)
1835{
1836 cm_format_mad_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid);
1837 rep_msg->local_comm_id = cm_id_priv->id.local_id;
1838 rep_msg->remote_comm_id = cm_id_priv->id.remote_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001839 cm_rep_set_starting_psn(rep_msg, cpu_to_be32(param->starting_psn));
1840 rep_msg->resp_resources = param->responder_resources;
Sean Hefty1d846122007-06-18 11:09:37 -07001841 cm_rep_set_target_ack_delay(rep_msg,
1842 cm_id_priv->av.port->cm_dev->ack_delay);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001843 cm_rep_set_failover(rep_msg, param->failover_accepted);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001844 cm_rep_set_rnr_retry_count(rep_msg, param->rnr_retry_count);
Sean Heftye971b8c2007-02-22 11:37:38 -08001845 rep_msg->local_ca_guid = cm_id_priv->id.device->node_guid;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001846
Sean Heftyd26a3602011-05-13 10:46:20 -07001847 if (cm_id_priv->qp_type != IB_QPT_XRC_TGT) {
1848 rep_msg->initiator_depth = param->initiator_depth;
1849 cm_rep_set_flow_ctrl(rep_msg, param->flow_control);
1850 cm_rep_set_srq(rep_msg, param->srq);
Sean Heftyef700442011-08-02 11:08:22 -07001851 cm_rep_set_local_qpn(rep_msg, cpu_to_be32(param->qp_num));
Sean Heftyd26a3602011-05-13 10:46:20 -07001852 } else {
1853 cm_rep_set_srq(rep_msg, 1);
Sean Heftyef700442011-08-02 11:08:22 -07001854 cm_rep_set_local_eecn(rep_msg, cpu_to_be32(param->qp_num));
Sean Heftyd26a3602011-05-13 10:46:20 -07001855 }
1856
Hal Rosenstocka9770492005-07-27 11:45:40 -07001857 if (param->private_data && param->private_data_len)
1858 memcpy(rep_msg->private_data, param->private_data,
1859 param->private_data_len);
1860}
1861
1862int ib_send_cm_rep(struct ib_cm_id *cm_id,
1863 struct ib_cm_rep_param *param)
1864{
1865 struct cm_id_private *cm_id_priv;
1866 struct ib_mad_send_buf *msg;
1867 struct cm_rep_msg *rep_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001868 unsigned long flags;
1869 int ret;
1870
1871 if (param->private_data &&
1872 param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE)
1873 return -EINVAL;
1874
1875 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1876 spin_lock_irqsave(&cm_id_priv->lock, flags);
1877 if (cm_id->state != IB_CM_REQ_RCVD &&
1878 cm_id->state != IB_CM_MRA_REQ_SENT) {
1879 ret = -EINVAL;
1880 goto out;
1881 }
1882
1883 ret = cm_alloc_msg(cm_id_priv, &msg);
1884 if (ret)
1885 goto out;
1886
1887 rep_msg = (struct cm_rep_msg *) msg->mad;
1888 cm_format_rep(rep_msg, cm_id_priv, param);
Sean Hefty34816ad2005-10-25 10:51:39 -07001889 msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001890 msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT;
1891
Sean Hefty34816ad2005-10-25 10:51:39 -07001892 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001893 if (ret) {
1894 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1895 cm_free_msg(msg);
1896 return ret;
1897 }
1898
1899 cm_id->state = IB_CM_REP_SENT;
1900 cm_id_priv->msg = msg;
1901 cm_id_priv->initiator_depth = param->initiator_depth;
1902 cm_id_priv->responder_resources = param->responder_resources;
1903 cm_id_priv->rq_psn = cm_rep_get_starting_psn(rep_msg);
Sean Heftyef700442011-08-02 11:08:22 -07001904 cm_id_priv->local_qpn = cpu_to_be32(param->qp_num & 0xFFFFFF);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001905
1906out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1907 return ret;
1908}
1909EXPORT_SYMBOL(ib_send_cm_rep);
1910
1911static void cm_format_rtu(struct cm_rtu_msg *rtu_msg,
1912 struct cm_id_private *cm_id_priv,
1913 const void *private_data,
1914 u8 private_data_len)
1915{
1916 cm_format_mad_hdr(&rtu_msg->hdr, CM_RTU_ATTR_ID, cm_id_priv->tid);
1917 rtu_msg->local_comm_id = cm_id_priv->id.local_id;
1918 rtu_msg->remote_comm_id = cm_id_priv->id.remote_id;
1919
1920 if (private_data && private_data_len)
1921 memcpy(rtu_msg->private_data, private_data, private_data_len);
1922}
1923
1924int ib_send_cm_rtu(struct ib_cm_id *cm_id,
1925 const void *private_data,
1926 u8 private_data_len)
1927{
1928 struct cm_id_private *cm_id_priv;
1929 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07001930 unsigned long flags;
1931 void *data;
1932 int ret;
1933
1934 if (private_data && private_data_len > IB_CM_RTU_PRIVATE_DATA_SIZE)
1935 return -EINVAL;
1936
1937 data = cm_copy_private_data(private_data, private_data_len);
1938 if (IS_ERR(data))
1939 return PTR_ERR(data);
1940
1941 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1942 spin_lock_irqsave(&cm_id_priv->lock, flags);
1943 if (cm_id->state != IB_CM_REP_RCVD &&
1944 cm_id->state != IB_CM_MRA_REP_SENT) {
1945 ret = -EINVAL;
1946 goto error;
1947 }
1948
1949 ret = cm_alloc_msg(cm_id_priv, &msg);
1950 if (ret)
1951 goto error;
1952
1953 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1954 private_data, private_data_len);
1955
Sean Hefty34816ad2005-10-25 10:51:39 -07001956 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07001957 if (ret) {
1958 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1959 cm_free_msg(msg);
1960 kfree(data);
1961 return ret;
1962 }
1963
1964 cm_id->state = IB_CM_ESTABLISHED;
1965 cm_set_private_data(cm_id_priv, data, private_data_len);
1966 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1967 return 0;
1968
1969error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1970 kfree(data);
1971 return ret;
1972}
1973EXPORT_SYMBOL(ib_send_cm_rtu);
1974
Sean Heftyef700442011-08-02 11:08:22 -07001975static void cm_format_rep_event(struct cm_work *work, enum ib_qp_type qp_type)
Hal Rosenstocka9770492005-07-27 11:45:40 -07001976{
1977 struct cm_rep_msg *rep_msg;
1978 struct ib_cm_rep_event_param *param;
1979
1980 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1981 param = &work->cm_event.param.rep_rcvd;
1982 param->remote_ca_guid = rep_msg->local_ca_guid;
1983 param->remote_qkey = be32_to_cpu(rep_msg->local_qkey);
Sean Heftyef700442011-08-02 11:08:22 -07001984 param->remote_qpn = be32_to_cpu(cm_rep_get_qpn(rep_msg, qp_type));
Hal Rosenstocka9770492005-07-27 11:45:40 -07001985 param->starting_psn = be32_to_cpu(cm_rep_get_starting_psn(rep_msg));
1986 param->responder_resources = rep_msg->initiator_depth;
1987 param->initiator_depth = rep_msg->resp_resources;
1988 param->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1989 param->failover_accepted = cm_rep_get_failover(rep_msg);
1990 param->flow_control = cm_rep_get_flow_ctrl(rep_msg);
1991 param->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1992 param->srq = cm_rep_get_srq(rep_msg);
1993 work->cm_event.private_data = &rep_msg->private_data;
1994}
1995
1996static void cm_dup_rep_handler(struct cm_work *work)
1997{
1998 struct cm_id_private *cm_id_priv;
1999 struct cm_rep_msg *rep_msg;
2000 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002001 int ret;
2002
2003 rep_msg = (struct cm_rep_msg *) work->mad_recv_wc->recv_buf.mad;
2004 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id,
2005 rep_msg->local_comm_id);
2006 if (!cm_id_priv)
2007 return;
2008
Sean Hefty9af57b72007-07-16 21:49:35 -07002009 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2010 counter[CM_REP_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002011 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
2012 if (ret)
2013 goto deref;
2014
Sean Hefty24be6e82007-06-18 11:09:36 -07002015 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002016 if (cm_id_priv->id.state == IB_CM_ESTABLISHED)
2017 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
2018 cm_id_priv->private_data,
2019 cm_id_priv->private_data_len);
2020 else if (cm_id_priv->id.state == IB_CM_MRA_REP_SENT)
2021 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2022 CM_MSG_RESPONSE_REP, cm_id_priv->service_timeout,
2023 cm_id_priv->private_data,
2024 cm_id_priv->private_data_len);
2025 else
2026 goto unlock;
Sean Hefty24be6e82007-06-18 11:09:36 -07002027 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002028
Sean Hefty34816ad2005-10-25 10:51:39 -07002029 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002030 if (ret)
2031 goto free;
2032 goto deref;
2033
Sean Hefty24be6e82007-06-18 11:09:36 -07002034unlock: spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002035free: cm_free_msg(msg);
2036deref: cm_deref_id(cm_id_priv);
2037}
2038
2039static int cm_rep_handler(struct cm_work *work)
2040{
2041 struct cm_id_private *cm_id_priv;
2042 struct cm_rep_msg *rep_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002043 int ret;
Hans Westgaard Ry9315bc92016-10-28 13:14:29 +02002044 struct cm_id_private *cur_cm_id_priv;
2045 struct ib_cm_id *cm_id;
2046 struct cm_timewait_info *timewait_info;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002047
2048 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
2049 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id, 0);
2050 if (!cm_id_priv) {
2051 cm_dup_rep_handler(work);
2052 return -EINVAL;
2053 }
2054
Sean Heftyef700442011-08-02 11:08:22 -07002055 cm_format_rep_event(work, cm_id_priv->qp_type);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002056
Sean Hefty24be6e82007-06-18 11:09:36 -07002057 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002058 switch (cm_id_priv->id.state) {
2059 case IB_CM_REQ_SENT:
2060 case IB_CM_MRA_REQ_RCVD:
2061 break;
2062 default:
Sean Hefty24be6e82007-06-18 11:09:36 -07002063 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002064 ret = -EINVAL;
2065 goto error;
2066 }
Sean Hefty87fd1a12006-03-02 16:50:37 -08002067
2068 cm_id_priv->timewait_info->work.remote_id = rep_msg->local_comm_id;
2069 cm_id_priv->timewait_info->remote_ca_guid = rep_msg->local_ca_guid;
Sean Heftyef700442011-08-02 11:08:22 -07002070 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 -08002071
2072 spin_lock(&cm.lock);
2073 /* Check for duplicate REP. */
2074 if (cm_insert_remote_id(cm_id_priv->timewait_info)) {
2075 spin_unlock(&cm.lock);
Sean Hefty24be6e82007-06-18 11:09:36 -07002076 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty87fd1a12006-03-02 16:50:37 -08002077 ret = -EINVAL;
2078 goto error;
2079 }
2080 /* Check for a stale connection. */
Hans Westgaard Ry9315bc92016-10-28 13:14:29 +02002081 timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
2082 if (timewait_info) {
Sean Hefty87fd1a12006-03-02 16:50:37 -08002083 rb_erase(&cm_id_priv->timewait_info->remote_id_node,
2084 &cm.remote_id_table);
2085 cm_id_priv->timewait_info->inserted_remote_id = 0;
Hans Westgaard Ry9315bc92016-10-28 13:14:29 +02002086 cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
2087 timewait_info->work.remote_id);
2088
Sean Hefty87fd1a12006-03-02 16:50:37 -08002089 spin_unlock(&cm.lock);
Sean Hefty24be6e82007-06-18 11:09:36 -07002090 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty87fd1a12006-03-02 16:50:37 -08002091 cm_issue_rej(work->port, work->mad_recv_wc,
2092 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP,
2093 NULL, 0);
2094 ret = -EINVAL;
Hans Westgaard Ry9315bc92016-10-28 13:14:29 +02002095 if (cur_cm_id_priv) {
2096 cm_id = &cur_cm_id_priv->id;
2097 ib_send_cm_dreq(cm_id, NULL, 0);
2098 cm_deref_id(cur_cm_id_priv);
2099 }
2100
Sean Hefty87fd1a12006-03-02 16:50:37 -08002101 goto error;
2102 }
2103 spin_unlock(&cm.lock);
2104
Hal Rosenstocka9770492005-07-27 11:45:40 -07002105 cm_id_priv->id.state = IB_CM_REP_RCVD;
2106 cm_id_priv->id.remote_id = rep_msg->local_comm_id;
Sean Heftyef700442011-08-02 11:08:22 -07002107 cm_id_priv->remote_qpn = cm_rep_get_qpn(rep_msg, cm_id_priv->qp_type);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002108 cm_id_priv->initiator_depth = rep_msg->resp_resources;
2109 cm_id_priv->responder_resources = rep_msg->initiator_depth;
2110 cm_id_priv->sq_psn = cm_rep_get_starting_psn(rep_msg);
2111 cm_id_priv->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
Sean Hefty1d846122007-06-18 11:09:37 -07002112 cm_id_priv->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
2113 cm_id_priv->av.timeout =
2114 cm_ack_timeout(cm_id_priv->target_ack_delay,
2115 cm_id_priv->av.timeout - 1);
2116 cm_id_priv->alt_av.timeout =
2117 cm_ack_timeout(cm_id_priv->target_ack_delay,
2118 cm_id_priv->alt_av.timeout - 1);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002119
2120 /* todo: handle peer_to_peer */
2121
Sean Hefty34816ad2005-10-25 10:51:39 -07002122 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002123 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2124 if (!ret)
2125 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002126 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002127
2128 if (ret)
2129 cm_process_work(cm_id_priv, work);
2130 else
2131 cm_deref_id(cm_id_priv);
2132 return 0;
2133
Sean Hefty87fd1a12006-03-02 16:50:37 -08002134error:
Hal Rosenstocka9770492005-07-27 11:45:40 -07002135 cm_deref_id(cm_id_priv);
2136 return ret;
2137}
2138
2139static int cm_establish_handler(struct cm_work *work)
2140{
2141 struct cm_id_private *cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002142 int ret;
2143
Sean Heftye1444b52006-11-28 14:57:13 -08002144 /* See comment in cm_establish about lookup. */
Hal Rosenstocka9770492005-07-27 11:45:40 -07002145 cm_id_priv = cm_acquire_id(work->local_id, work->remote_id);
2146 if (!cm_id_priv)
2147 return -EINVAL;
2148
Sean Hefty24be6e82007-06-18 11:09:36 -07002149 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002150 if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
Sean Hefty24be6e82007-06-18 11:09:36 -07002151 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002152 goto out;
2153 }
2154
Sean Hefty34816ad2005-10-25 10:51:39 -07002155 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002156 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2157 if (!ret)
2158 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002159 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002160
2161 if (ret)
2162 cm_process_work(cm_id_priv, work);
2163 else
2164 cm_deref_id(cm_id_priv);
2165 return 0;
2166out:
2167 cm_deref_id(cm_id_priv);
2168 return -EINVAL;
2169}
2170
2171static int cm_rtu_handler(struct cm_work *work)
2172{
2173 struct cm_id_private *cm_id_priv;
2174 struct cm_rtu_msg *rtu_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002175 int ret;
2176
2177 rtu_msg = (struct cm_rtu_msg *)work->mad_recv_wc->recv_buf.mad;
2178 cm_id_priv = cm_acquire_id(rtu_msg->remote_comm_id,
2179 rtu_msg->local_comm_id);
2180 if (!cm_id_priv)
2181 return -EINVAL;
2182
2183 work->cm_event.private_data = &rtu_msg->private_data;
2184
Sean Hefty24be6e82007-06-18 11:09:36 -07002185 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002186 if (cm_id_priv->id.state != IB_CM_REP_SENT &&
2187 cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) {
Sean Hefty24be6e82007-06-18 11:09:36 -07002188 spin_unlock_irq(&cm_id_priv->lock);
Sean Hefty9af57b72007-07-16 21:49:35 -07002189 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2190 counter[CM_RTU_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002191 goto out;
2192 }
2193 cm_id_priv->id.state = IB_CM_ESTABLISHED;
2194
Sean Hefty34816ad2005-10-25 10:51:39 -07002195 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002196 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2197 if (!ret)
2198 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002199 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002200
2201 if (ret)
2202 cm_process_work(cm_id_priv, work);
2203 else
2204 cm_deref_id(cm_id_priv);
2205 return 0;
2206out:
2207 cm_deref_id(cm_id_priv);
2208 return -EINVAL;
2209}
2210
2211static void cm_format_dreq(struct cm_dreq_msg *dreq_msg,
2212 struct cm_id_private *cm_id_priv,
2213 const void *private_data,
2214 u8 private_data_len)
2215{
2216 cm_format_mad_hdr(&dreq_msg->hdr, CM_DREQ_ATTR_ID,
2217 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_DREQ));
2218 dreq_msg->local_comm_id = cm_id_priv->id.local_id;
2219 dreq_msg->remote_comm_id = cm_id_priv->id.remote_id;
2220 cm_dreq_set_remote_qpn(dreq_msg, cm_id_priv->remote_qpn);
2221
2222 if (private_data && private_data_len)
2223 memcpy(dreq_msg->private_data, private_data, private_data_len);
2224}
2225
2226int ib_send_cm_dreq(struct ib_cm_id *cm_id,
2227 const void *private_data,
2228 u8 private_data_len)
2229{
2230 struct cm_id_private *cm_id_priv;
2231 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002232 unsigned long flags;
2233 int ret;
2234
2235 if (private_data && private_data_len > IB_CM_DREQ_PRIVATE_DATA_SIZE)
2236 return -EINVAL;
2237
2238 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2239 spin_lock_irqsave(&cm_id_priv->lock, flags);
2240 if (cm_id->state != IB_CM_ESTABLISHED) {
2241 ret = -EINVAL;
2242 goto out;
2243 }
2244
Sean Hefty8d8ac862011-03-03 23:31:06 +00002245 if (cm_id->lap_state == IB_CM_LAP_SENT ||
2246 cm_id->lap_state == IB_CM_MRA_LAP_RCVD)
2247 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2248
Hal Rosenstocka9770492005-07-27 11:45:40 -07002249 ret = cm_alloc_msg(cm_id_priv, &msg);
2250 if (ret) {
2251 cm_enter_timewait(cm_id_priv);
2252 goto out;
2253 }
2254
2255 cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv,
2256 private_data, private_data_len);
Sean Hefty34816ad2005-10-25 10:51:39 -07002257 msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002258 msg->context[1] = (void *) (unsigned long) IB_CM_DREQ_SENT;
2259
Sean Hefty34816ad2005-10-25 10:51:39 -07002260 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002261 if (ret) {
2262 cm_enter_timewait(cm_id_priv);
2263 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2264 cm_free_msg(msg);
2265 return ret;
2266 }
2267
2268 cm_id->state = IB_CM_DREQ_SENT;
2269 cm_id_priv->msg = msg;
2270out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2271 return ret;
2272}
2273EXPORT_SYMBOL(ib_send_cm_dreq);
2274
2275static void cm_format_drep(struct cm_drep_msg *drep_msg,
2276 struct cm_id_private *cm_id_priv,
2277 const void *private_data,
2278 u8 private_data_len)
2279{
2280 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, cm_id_priv->tid);
2281 drep_msg->local_comm_id = cm_id_priv->id.local_id;
2282 drep_msg->remote_comm_id = cm_id_priv->id.remote_id;
2283
2284 if (private_data && private_data_len)
2285 memcpy(drep_msg->private_data, private_data, private_data_len);
2286}
2287
2288int ib_send_cm_drep(struct ib_cm_id *cm_id,
2289 const void *private_data,
2290 u8 private_data_len)
2291{
2292 struct cm_id_private *cm_id_priv;
2293 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002294 unsigned long flags;
2295 void *data;
2296 int ret;
2297
2298 if (private_data && private_data_len > IB_CM_DREP_PRIVATE_DATA_SIZE)
2299 return -EINVAL;
2300
2301 data = cm_copy_private_data(private_data, private_data_len);
2302 if (IS_ERR(data))
2303 return PTR_ERR(data);
2304
2305 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2306 spin_lock_irqsave(&cm_id_priv->lock, flags);
2307 if (cm_id->state != IB_CM_DREQ_RCVD) {
2308 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2309 kfree(data);
2310 return -EINVAL;
2311 }
2312
2313 cm_set_private_data(cm_id_priv, data, private_data_len);
2314 cm_enter_timewait(cm_id_priv);
2315
2316 ret = cm_alloc_msg(cm_id_priv, &msg);
2317 if (ret)
2318 goto out;
2319
2320 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
2321 private_data, private_data_len);
2322
Sean Hefty34816ad2005-10-25 10:51:39 -07002323 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002324 if (ret) {
2325 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2326 cm_free_msg(msg);
2327 return ret;
2328 }
2329
2330out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2331 return ret;
2332}
2333EXPORT_SYMBOL(ib_send_cm_drep);
2334
Sean Hefty82a9c162006-10-04 11:37:25 -07002335static int cm_issue_drep(struct cm_port *port,
2336 struct ib_mad_recv_wc *mad_recv_wc)
2337{
2338 struct ib_mad_send_buf *msg = NULL;
2339 struct cm_dreq_msg *dreq_msg;
2340 struct cm_drep_msg *drep_msg;
2341 int ret;
2342
2343 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
2344 if (ret)
2345 return ret;
2346
2347 dreq_msg = (struct cm_dreq_msg *) mad_recv_wc->recv_buf.mad;
2348 drep_msg = (struct cm_drep_msg *) msg->mad;
2349
2350 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, dreq_msg->hdr.tid);
2351 drep_msg->remote_comm_id = dreq_msg->local_comm_id;
2352 drep_msg->local_comm_id = dreq_msg->remote_comm_id;
2353
2354 ret = ib_post_send_mad(msg, NULL);
2355 if (ret)
2356 cm_free_msg(msg);
2357
2358 return ret;
2359}
2360
Hal Rosenstocka9770492005-07-27 11:45:40 -07002361static int cm_dreq_handler(struct cm_work *work)
2362{
2363 struct cm_id_private *cm_id_priv;
2364 struct cm_dreq_msg *dreq_msg;
2365 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002366 int ret;
2367
2368 dreq_msg = (struct cm_dreq_msg *)work->mad_recv_wc->recv_buf.mad;
2369 cm_id_priv = cm_acquire_id(dreq_msg->remote_comm_id,
2370 dreq_msg->local_comm_id);
Sean Hefty82a9c162006-10-04 11:37:25 -07002371 if (!cm_id_priv) {
Sean Hefty9af57b72007-07-16 21:49:35 -07002372 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2373 counter[CM_DREQ_COUNTER]);
Sean Hefty82a9c162006-10-04 11:37:25 -07002374 cm_issue_drep(work->port, work->mad_recv_wc);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002375 return -EINVAL;
Sean Hefty82a9c162006-10-04 11:37:25 -07002376 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07002377
2378 work->cm_event.private_data = &dreq_msg->private_data;
2379
Sean Hefty24be6e82007-06-18 11:09:36 -07002380 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002381 if (cm_id_priv->local_qpn != cm_dreq_get_remote_qpn(dreq_msg))
2382 goto unlock;
2383
2384 switch (cm_id_priv->id.state) {
2385 case IB_CM_REP_SENT:
2386 case IB_CM_DREQ_SENT:
Sean Hefty34816ad2005-10-25 10:51:39 -07002387 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002388 break;
2389 case IB_CM_ESTABLISHED:
Sean Hefty8d8ac862011-03-03 23:31:06 +00002390 if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT ||
2391 cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
2392 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
2393 break;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002394 case IB_CM_MRA_REP_RCVD:
2395 break;
2396 case IB_CM_TIMEWAIT:
Sean Hefty9af57b72007-07-16 21:49:35 -07002397 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2398 counter[CM_DREQ_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002399 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2400 goto unlock;
2401
2402 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
2403 cm_id_priv->private_data,
2404 cm_id_priv->private_data_len);
Sean Hefty24be6e82007-06-18 11:09:36 -07002405 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002406
Sean Hefty34816ad2005-10-25 10:51:39 -07002407 if (ib_post_send_mad(msg, NULL))
Hal Rosenstocka9770492005-07-27 11:45:40 -07002408 cm_free_msg(msg);
2409 goto deref;
Sean Hefty9af57b72007-07-16 21:49:35 -07002410 case IB_CM_DREQ_RCVD:
2411 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2412 counter[CM_DREQ_COUNTER]);
2413 goto unlock;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002414 default:
2415 goto unlock;
2416 }
2417 cm_id_priv->id.state = IB_CM_DREQ_RCVD;
2418 cm_id_priv->tid = dreq_msg->hdr.tid;
2419 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2420 if (!ret)
2421 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002422 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002423
2424 if (ret)
2425 cm_process_work(cm_id_priv, work);
2426 else
2427 cm_deref_id(cm_id_priv);
2428 return 0;
2429
Sean Hefty24be6e82007-06-18 11:09:36 -07002430unlock: spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002431deref: cm_deref_id(cm_id_priv);
2432 return -EINVAL;
2433}
2434
2435static int cm_drep_handler(struct cm_work *work)
2436{
2437 struct cm_id_private *cm_id_priv;
2438 struct cm_drep_msg *drep_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002439 int ret;
2440
2441 drep_msg = (struct cm_drep_msg *)work->mad_recv_wc->recv_buf.mad;
2442 cm_id_priv = cm_acquire_id(drep_msg->remote_comm_id,
2443 drep_msg->local_comm_id);
2444 if (!cm_id_priv)
2445 return -EINVAL;
2446
2447 work->cm_event.private_data = &drep_msg->private_data;
2448
Sean Hefty24be6e82007-06-18 11:09:36 -07002449 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002450 if (cm_id_priv->id.state != IB_CM_DREQ_SENT &&
2451 cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
Sean Hefty24be6e82007-06-18 11:09:36 -07002452 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002453 goto out;
2454 }
2455 cm_enter_timewait(cm_id_priv);
2456
Sean Hefty34816ad2005-10-25 10:51:39 -07002457 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002458 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2459 if (!ret)
2460 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002461 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002462
2463 if (ret)
2464 cm_process_work(cm_id_priv, work);
2465 else
2466 cm_deref_id(cm_id_priv);
2467 return 0;
2468out:
2469 cm_deref_id(cm_id_priv);
2470 return -EINVAL;
2471}
2472
2473int ib_send_cm_rej(struct ib_cm_id *cm_id,
2474 enum ib_cm_rej_reason reason,
2475 void *ari,
2476 u8 ari_length,
2477 const void *private_data,
2478 u8 private_data_len)
2479{
2480 struct cm_id_private *cm_id_priv;
2481 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002482 unsigned long flags;
2483 int ret;
2484
2485 if ((private_data && private_data_len > IB_CM_REJ_PRIVATE_DATA_SIZE) ||
2486 (ari && ari_length > IB_CM_REJ_ARI_LENGTH))
2487 return -EINVAL;
2488
2489 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2490
2491 spin_lock_irqsave(&cm_id_priv->lock, flags);
2492 switch (cm_id->state) {
2493 case IB_CM_REQ_SENT:
2494 case IB_CM_MRA_REQ_RCVD:
2495 case IB_CM_REQ_RCVD:
2496 case IB_CM_MRA_REQ_SENT:
2497 case IB_CM_REP_RCVD:
2498 case IB_CM_MRA_REP_SENT:
2499 ret = cm_alloc_msg(cm_id_priv, &msg);
2500 if (!ret)
2501 cm_format_rej((struct cm_rej_msg *) msg->mad,
2502 cm_id_priv, reason, ari, ari_length,
2503 private_data, private_data_len);
2504
2505 cm_reset_to_idle(cm_id_priv);
2506 break;
2507 case IB_CM_REP_SENT:
2508 case IB_CM_MRA_REP_RCVD:
2509 ret = cm_alloc_msg(cm_id_priv, &msg);
2510 if (!ret)
2511 cm_format_rej((struct cm_rej_msg *) msg->mad,
2512 cm_id_priv, reason, ari, ari_length,
2513 private_data, private_data_len);
2514
2515 cm_enter_timewait(cm_id_priv);
2516 break;
2517 default:
2518 ret = -EINVAL;
2519 goto out;
2520 }
2521
2522 if (ret)
2523 goto out;
2524
Sean Hefty34816ad2005-10-25 10:51:39 -07002525 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002526 if (ret)
2527 cm_free_msg(msg);
2528
2529out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2530 return ret;
2531}
2532EXPORT_SYMBOL(ib_send_cm_rej);
2533
2534static void cm_format_rej_event(struct cm_work *work)
2535{
2536 struct cm_rej_msg *rej_msg;
2537 struct ib_cm_rej_event_param *param;
2538
2539 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2540 param = &work->cm_event.param.rej_rcvd;
2541 param->ari = rej_msg->ari;
2542 param->ari_length = cm_rej_get_reject_info_len(rej_msg);
Sean Hefty97f52eb2005-08-13 21:05:57 -07002543 param->reason = __be16_to_cpu(rej_msg->reason);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002544 work->cm_event.private_data = &rej_msg->private_data;
2545}
2546
2547static struct cm_id_private * cm_acquire_rejected_id(struct cm_rej_msg *rej_msg)
2548{
2549 struct cm_timewait_info *timewait_info;
2550 struct cm_id_private *cm_id_priv;
Sean Hefty97f52eb2005-08-13 21:05:57 -07002551 __be32 remote_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002552
2553 remote_id = rej_msg->local_comm_id;
2554
Sean Hefty97f52eb2005-08-13 21:05:57 -07002555 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_TIMEOUT) {
Sean Hefty24be6e82007-06-18 11:09:36 -07002556 spin_lock_irq(&cm.lock);
Sean Hefty97f52eb2005-08-13 21:05:57 -07002557 timewait_info = cm_find_remote_id( *((__be64 *) rej_msg->ari),
Hal Rosenstocka9770492005-07-27 11:45:40 -07002558 remote_id);
2559 if (!timewait_info) {
Sean Hefty24be6e82007-06-18 11:09:36 -07002560 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002561 return NULL;
2562 }
Sean Heftyf06d2652006-08-28 15:15:18 -07002563 cm_id_priv = idr_find(&cm.local_id_table, (__force int)
2564 (timewait_info->work.local_id ^
2565 cm.random_id_operand));
Hal Rosenstocka9770492005-07-27 11:45:40 -07002566 if (cm_id_priv) {
2567 if (cm_id_priv->id.remote_id == remote_id)
2568 atomic_inc(&cm_id_priv->refcount);
2569 else
2570 cm_id_priv = NULL;
2571 }
Sean Hefty24be6e82007-06-18 11:09:36 -07002572 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002573 } else if (cm_rej_get_msg_rejected(rej_msg) == CM_MSG_RESPONSE_REQ)
2574 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, 0);
2575 else
2576 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, remote_id);
2577
2578 return cm_id_priv;
2579}
2580
2581static int cm_rej_handler(struct cm_work *work)
2582{
2583 struct cm_id_private *cm_id_priv;
2584 struct cm_rej_msg *rej_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002585 int ret;
2586
2587 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2588 cm_id_priv = cm_acquire_rejected_id(rej_msg);
2589 if (!cm_id_priv)
2590 return -EINVAL;
2591
2592 cm_format_rej_event(work);
2593
Sean Hefty24be6e82007-06-18 11:09:36 -07002594 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002595 switch (cm_id_priv->id.state) {
2596 case IB_CM_REQ_SENT:
2597 case IB_CM_MRA_REQ_RCVD:
2598 case IB_CM_REP_SENT:
2599 case IB_CM_MRA_REP_RCVD:
Sean Hefty34816ad2005-10-25 10:51:39 -07002600 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002601 /* fall through */
2602 case IB_CM_REQ_RCVD:
2603 case IB_CM_MRA_REQ_SENT:
Sean Hefty97f52eb2005-08-13 21:05:57 -07002604 if (__be16_to_cpu(rej_msg->reason) == IB_CM_REJ_STALE_CONN)
Hal Rosenstocka9770492005-07-27 11:45:40 -07002605 cm_enter_timewait(cm_id_priv);
2606 else
2607 cm_reset_to_idle(cm_id_priv);
2608 break;
2609 case IB_CM_DREQ_SENT:
Sean Hefty34816ad2005-10-25 10:51:39 -07002610 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002611 /* fall through */
2612 case IB_CM_REP_RCVD:
2613 case IB_CM_MRA_REP_SENT:
Hal Rosenstocka9770492005-07-27 11:45:40 -07002614 cm_enter_timewait(cm_id_priv);
2615 break;
Sean Hefty8d8ac862011-03-03 23:31:06 +00002616 case IB_CM_ESTABLISHED:
2617 if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT ||
2618 cm_id_priv->id.lap_state == IB_CM_LAP_SENT) {
2619 if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT)
2620 ib_cancel_mad(cm_id_priv->av.port->mad_agent,
2621 cm_id_priv->msg);
2622 cm_enter_timewait(cm_id_priv);
2623 break;
2624 }
2625 /* fall through */
Hal Rosenstocka9770492005-07-27 11:45:40 -07002626 default:
Sean Hefty24be6e82007-06-18 11:09:36 -07002627 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002628 ret = -EINVAL;
2629 goto out;
2630 }
2631
2632 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2633 if (!ret)
2634 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002635 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002636
2637 if (ret)
2638 cm_process_work(cm_id_priv, work);
2639 else
2640 cm_deref_id(cm_id_priv);
2641 return 0;
2642out:
2643 cm_deref_id(cm_id_priv);
2644 return -EINVAL;
2645}
2646
2647int ib_send_cm_mra(struct ib_cm_id *cm_id,
2648 u8 service_timeout,
2649 const void *private_data,
2650 u8 private_data_len)
2651{
2652 struct cm_id_private *cm_id_priv;
2653 struct ib_mad_send_buf *msg;
Sean Heftyde98b692007-08-01 13:49:53 -07002654 enum ib_cm_state cm_state;
2655 enum ib_cm_lap_state lap_state;
2656 enum cm_msg_response msg_response;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002657 void *data;
2658 unsigned long flags;
2659 int ret;
2660
2661 if (private_data && private_data_len > IB_CM_MRA_PRIVATE_DATA_SIZE)
2662 return -EINVAL;
2663
2664 data = cm_copy_private_data(private_data, private_data_len);
2665 if (IS_ERR(data))
2666 return PTR_ERR(data);
2667
2668 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2669
2670 spin_lock_irqsave(&cm_id_priv->lock, flags);
2671 switch(cm_id_priv->id.state) {
2672 case IB_CM_REQ_RCVD:
Sean Heftyde98b692007-08-01 13:49:53 -07002673 cm_state = IB_CM_MRA_REQ_SENT;
2674 lap_state = cm_id->lap_state;
2675 msg_response = CM_MSG_RESPONSE_REQ;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002676 break;
2677 case IB_CM_REP_RCVD:
Sean Heftyde98b692007-08-01 13:49:53 -07002678 cm_state = IB_CM_MRA_REP_SENT;
2679 lap_state = cm_id->lap_state;
2680 msg_response = CM_MSG_RESPONSE_REP;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002681 break;
2682 case IB_CM_ESTABLISHED:
Sean Hefty50a025c2010-07-21 23:36:52 +00002683 if (cm_id->lap_state == IB_CM_LAP_RCVD) {
2684 cm_state = cm_id->state;
2685 lap_state = IB_CM_MRA_LAP_SENT;
2686 msg_response = CM_MSG_RESPONSE_OTHER;
2687 break;
2688 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07002689 default:
2690 ret = -EINVAL;
2691 goto error1;
2692 }
Sean Heftyde98b692007-08-01 13:49:53 -07002693
2694 if (!(service_timeout & IB_CM_MRA_FLAG_DELAY)) {
2695 ret = cm_alloc_msg(cm_id_priv, &msg);
2696 if (ret)
2697 goto error1;
2698
2699 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2700 msg_response, service_timeout,
2701 private_data, private_data_len);
2702 ret = ib_post_send_mad(msg, NULL);
2703 if (ret)
2704 goto error2;
2705 }
2706
2707 cm_id->state = cm_state;
2708 cm_id->lap_state = lap_state;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002709 cm_id_priv->service_timeout = service_timeout;
2710 cm_set_private_data(cm_id_priv, data, private_data_len);
2711 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2712 return 0;
2713
2714error1: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2715 kfree(data);
2716 return ret;
2717
2718error2: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2719 kfree(data);
2720 cm_free_msg(msg);
2721 return ret;
2722}
2723EXPORT_SYMBOL(ib_send_cm_mra);
2724
2725static struct cm_id_private * cm_acquire_mraed_id(struct cm_mra_msg *mra_msg)
2726{
2727 switch (cm_mra_get_msg_mraed(mra_msg)) {
2728 case CM_MSG_RESPONSE_REQ:
2729 return cm_acquire_id(mra_msg->remote_comm_id, 0);
2730 case CM_MSG_RESPONSE_REP:
2731 case CM_MSG_RESPONSE_OTHER:
2732 return cm_acquire_id(mra_msg->remote_comm_id,
2733 mra_msg->local_comm_id);
2734 default:
2735 return NULL;
2736 }
2737}
2738
2739static int cm_mra_handler(struct cm_work *work)
2740{
2741 struct cm_id_private *cm_id_priv;
2742 struct cm_mra_msg *mra_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002743 int timeout, ret;
2744
2745 mra_msg = (struct cm_mra_msg *)work->mad_recv_wc->recv_buf.mad;
2746 cm_id_priv = cm_acquire_mraed_id(mra_msg);
2747 if (!cm_id_priv)
2748 return -EINVAL;
2749
2750 work->cm_event.private_data = &mra_msg->private_data;
2751 work->cm_event.param.mra_rcvd.service_timeout =
2752 cm_mra_get_service_timeout(mra_msg);
2753 timeout = cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg)) +
Sean Hefty1d846122007-06-18 11:09:37 -07002754 cm_convert_to_ms(cm_id_priv->av.timeout);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002755
Sean Hefty24be6e82007-06-18 11:09:36 -07002756 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002757 switch (cm_id_priv->id.state) {
2758 case IB_CM_REQ_SENT:
2759 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REQ ||
2760 ib_modify_mad(cm_id_priv->av.port->mad_agent,
Sean Hefty34816ad2005-10-25 10:51:39 -07002761 cm_id_priv->msg, timeout))
Hal Rosenstocka9770492005-07-27 11:45:40 -07002762 goto out;
2763 cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD;
2764 break;
2765 case IB_CM_REP_SENT:
2766 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REP ||
2767 ib_modify_mad(cm_id_priv->av.port->mad_agent,
Sean Hefty34816ad2005-10-25 10:51:39 -07002768 cm_id_priv->msg, timeout))
Hal Rosenstocka9770492005-07-27 11:45:40 -07002769 goto out;
2770 cm_id_priv->id.state = IB_CM_MRA_REP_RCVD;
2771 break;
2772 case IB_CM_ESTABLISHED:
2773 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_OTHER ||
2774 cm_id_priv->id.lap_state != IB_CM_LAP_SENT ||
2775 ib_modify_mad(cm_id_priv->av.port->mad_agent,
Sean Hefty9af57b72007-07-16 21:49:35 -07002776 cm_id_priv->msg, timeout)) {
2777 if (cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD)
2778 atomic_long_inc(&work->port->
2779 counter_group[CM_RECV_DUPLICATES].
2780 counter[CM_MRA_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002781 goto out;
Sean Hefty9af57b72007-07-16 21:49:35 -07002782 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07002783 cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD;
2784 break;
Sean Hefty9af57b72007-07-16 21:49:35 -07002785 case IB_CM_MRA_REQ_RCVD:
2786 case IB_CM_MRA_REP_RCVD:
2787 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2788 counter[CM_MRA_COUNTER]);
2789 /* fall through */
Hal Rosenstocka9770492005-07-27 11:45:40 -07002790 default:
2791 goto out;
2792 }
2793
2794 cm_id_priv->msg->context[1] = (void *) (unsigned long)
2795 cm_id_priv->id.state;
2796 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2797 if (!ret)
2798 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002799 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002800
2801 if (ret)
2802 cm_process_work(cm_id_priv, work);
2803 else
2804 cm_deref_id(cm_id_priv);
2805 return 0;
2806out:
Sean Hefty24be6e82007-06-18 11:09:36 -07002807 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002808 cm_deref_id(cm_id_priv);
2809 return -EINVAL;
2810}
2811
2812static void cm_format_lap(struct cm_lap_msg *lap_msg,
2813 struct cm_id_private *cm_id_priv,
2814 struct ib_sa_path_rec *alternate_path,
2815 const void *private_data,
2816 u8 private_data_len)
2817{
2818 cm_format_mad_hdr(&lap_msg->hdr, CM_LAP_ATTR_ID,
2819 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_LAP));
2820 lap_msg->local_comm_id = cm_id_priv->id.local_id;
2821 lap_msg->remote_comm_id = cm_id_priv->id.remote_id;
2822 cm_lap_set_remote_qpn(lap_msg, cm_id_priv->remote_qpn);
2823 /* todo: need remote CM response timeout */
2824 cm_lap_set_remote_resp_timeout(lap_msg, 0x1F);
2825 lap_msg->alt_local_lid = alternate_path->slid;
2826 lap_msg->alt_remote_lid = alternate_path->dlid;
2827 lap_msg->alt_local_gid = alternate_path->sgid;
2828 lap_msg->alt_remote_gid = alternate_path->dgid;
2829 cm_lap_set_flow_label(lap_msg, alternate_path->flow_label);
2830 cm_lap_set_traffic_class(lap_msg, alternate_path->traffic_class);
2831 lap_msg->alt_hop_limit = alternate_path->hop_limit;
2832 cm_lap_set_packet_rate(lap_msg, alternate_path->rate);
2833 cm_lap_set_sl(lap_msg, alternate_path->sl);
2834 cm_lap_set_subnet_local(lap_msg, 1); /* local only... */
2835 cm_lap_set_local_ack_timeout(lap_msg,
Sean Hefty1d846122007-06-18 11:09:37 -07002836 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay,
2837 alternate_path->packet_life_time));
Hal Rosenstocka9770492005-07-27 11:45:40 -07002838
2839 if (private_data && private_data_len)
2840 memcpy(lap_msg->private_data, private_data, private_data_len);
2841}
2842
2843int ib_send_cm_lap(struct ib_cm_id *cm_id,
2844 struct ib_sa_path_rec *alternate_path,
2845 const void *private_data,
2846 u8 private_data_len)
2847{
2848 struct cm_id_private *cm_id_priv;
2849 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002850 unsigned long flags;
2851 int ret;
2852
2853 if (private_data && private_data_len > IB_CM_LAP_PRIVATE_DATA_SIZE)
2854 return -EINVAL;
2855
2856 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2857 spin_lock_irqsave(&cm_id_priv->lock, flags);
2858 if (cm_id->state != IB_CM_ESTABLISHED ||
Sean Heftye1444b52006-11-28 14:57:13 -08002859 (cm_id->lap_state != IB_CM_LAP_UNINIT &&
2860 cm_id->lap_state != IB_CM_LAP_IDLE)) {
Hal Rosenstocka9770492005-07-27 11:45:40 -07002861 ret = -EINVAL;
2862 goto out;
2863 }
2864
Mark Bloch9db0ff52016-10-27 16:36:27 +03002865 ret = cm_init_av_by_path(alternate_path, &cm_id_priv->alt_av,
2866 cm_id_priv);
Sean Heftye1444b52006-11-28 14:57:13 -08002867 if (ret)
2868 goto out;
Sean Hefty1d846122007-06-18 11:09:37 -07002869 cm_id_priv->alt_av.timeout =
2870 cm_ack_timeout(cm_id_priv->target_ack_delay,
2871 cm_id_priv->alt_av.timeout - 1);
Sean Heftye1444b52006-11-28 14:57:13 -08002872
Hal Rosenstocka9770492005-07-27 11:45:40 -07002873 ret = cm_alloc_msg(cm_id_priv, &msg);
2874 if (ret)
2875 goto out;
2876
2877 cm_format_lap((struct cm_lap_msg *) msg->mad, cm_id_priv,
2878 alternate_path, private_data, private_data_len);
Sean Hefty34816ad2005-10-25 10:51:39 -07002879 msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002880 msg->context[1] = (void *) (unsigned long) IB_CM_ESTABLISHED;
2881
Sean Hefty34816ad2005-10-25 10:51:39 -07002882 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002883 if (ret) {
2884 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2885 cm_free_msg(msg);
2886 return ret;
2887 }
2888
2889 cm_id->lap_state = IB_CM_LAP_SENT;
2890 cm_id_priv->msg = msg;
2891
2892out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2893 return ret;
2894}
2895EXPORT_SYMBOL(ib_send_cm_lap);
2896
Sean Heftye1444b52006-11-28 14:57:13 -08002897static void cm_format_path_from_lap(struct cm_id_private *cm_id_priv,
2898 struct ib_sa_path_rec *path,
Hal Rosenstocka9770492005-07-27 11:45:40 -07002899 struct cm_lap_msg *lap_msg)
2900{
2901 memset(path, 0, sizeof *path);
2902 path->dgid = lap_msg->alt_local_gid;
2903 path->sgid = lap_msg->alt_remote_gid;
2904 path->dlid = lap_msg->alt_local_lid;
2905 path->slid = lap_msg->alt_remote_lid;
2906 path->flow_label = cm_lap_get_flow_label(lap_msg);
2907 path->hop_limit = lap_msg->alt_hop_limit;
2908 path->traffic_class = cm_lap_get_traffic_class(lap_msg);
2909 path->reversible = 1;
Sean Heftye1444b52006-11-28 14:57:13 -08002910 path->pkey = cm_id_priv->pkey;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002911 path->sl = cm_lap_get_sl(lap_msg);
2912 path->mtu_selector = IB_SA_EQ;
Sean Heftye1444b52006-11-28 14:57:13 -08002913 path->mtu = cm_id_priv->path_mtu;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002914 path->rate_selector = IB_SA_EQ;
2915 path->rate = cm_lap_get_packet_rate(lap_msg);
2916 path->packet_life_time_selector = IB_SA_EQ;
2917 path->packet_life_time = cm_lap_get_local_ack_timeout(lap_msg);
2918 path->packet_life_time -= (path->packet_life_time > 0);
2919}
2920
2921static int cm_lap_handler(struct cm_work *work)
2922{
2923 struct cm_id_private *cm_id_priv;
2924 struct cm_lap_msg *lap_msg;
2925 struct ib_cm_lap_event_param *param;
2926 struct ib_mad_send_buf *msg = NULL;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002927 int ret;
2928
2929 /* todo: verify LAP request and send reject APR if invalid. */
2930 lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad;
2931 cm_id_priv = cm_acquire_id(lap_msg->remote_comm_id,
2932 lap_msg->local_comm_id);
2933 if (!cm_id_priv)
2934 return -EINVAL;
2935
2936 param = &work->cm_event.param.lap_rcvd;
2937 param->alternate_path = &work->path[0];
Sean Heftye1444b52006-11-28 14:57:13 -08002938 cm_format_path_from_lap(cm_id_priv, param->alternate_path, lap_msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002939 work->cm_event.private_data = &lap_msg->private_data;
2940
Sean Hefty24be6e82007-06-18 11:09:36 -07002941 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002942 if (cm_id_priv->id.state != IB_CM_ESTABLISHED)
2943 goto unlock;
2944
2945 switch (cm_id_priv->id.lap_state) {
Sean Heftye1444b52006-11-28 14:57:13 -08002946 case IB_CM_LAP_UNINIT:
Hal Rosenstocka9770492005-07-27 11:45:40 -07002947 case IB_CM_LAP_IDLE:
2948 break;
2949 case IB_CM_MRA_LAP_SENT:
Sean Hefty9af57b72007-07-16 21:49:35 -07002950 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2951 counter[CM_LAP_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002952 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2953 goto unlock;
2954
2955 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2956 CM_MSG_RESPONSE_OTHER,
2957 cm_id_priv->service_timeout,
2958 cm_id_priv->private_data,
2959 cm_id_priv->private_data_len);
Sean Hefty24be6e82007-06-18 11:09:36 -07002960 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002961
Sean Hefty34816ad2005-10-25 10:51:39 -07002962 if (ib_post_send_mad(msg, NULL))
Hal Rosenstocka9770492005-07-27 11:45:40 -07002963 cm_free_msg(msg);
2964 goto deref;
Sean Hefty9af57b72007-07-16 21:49:35 -07002965 case IB_CM_LAP_RCVD:
2966 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
2967 counter[CM_LAP_COUNTER]);
2968 goto unlock;
Hal Rosenstocka9770492005-07-27 11:45:40 -07002969 default:
2970 goto unlock;
2971 }
2972
2973 cm_id_priv->id.lap_state = IB_CM_LAP_RCVD;
2974 cm_id_priv->tid = lap_msg->hdr.tid;
Sean Heftye1444b52006-11-28 14:57:13 -08002975 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
2976 work->mad_recv_wc->recv_buf.grh,
2977 &cm_id_priv->av);
Mark Bloch9db0ff52016-10-27 16:36:27 +03002978 cm_init_av_by_path(param->alternate_path, &cm_id_priv->alt_av,
2979 cm_id_priv);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002980 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2981 if (!ret)
2982 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07002983 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002984
2985 if (ret)
2986 cm_process_work(cm_id_priv, work);
2987 else
2988 cm_deref_id(cm_id_priv);
2989 return 0;
2990
Sean Hefty24be6e82007-06-18 11:09:36 -07002991unlock: spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07002992deref: cm_deref_id(cm_id_priv);
2993 return -EINVAL;
2994}
2995
2996static void cm_format_apr(struct cm_apr_msg *apr_msg,
2997 struct cm_id_private *cm_id_priv,
2998 enum ib_cm_apr_status status,
2999 void *info,
3000 u8 info_length,
3001 const void *private_data,
3002 u8 private_data_len)
3003{
3004 cm_format_mad_hdr(&apr_msg->hdr, CM_APR_ATTR_ID, cm_id_priv->tid);
3005 apr_msg->local_comm_id = cm_id_priv->id.local_id;
3006 apr_msg->remote_comm_id = cm_id_priv->id.remote_id;
3007 apr_msg->ap_status = (u8) status;
3008
3009 if (info && info_length) {
3010 apr_msg->info_length = info_length;
3011 memcpy(apr_msg->info, info, info_length);
3012 }
3013
3014 if (private_data && private_data_len)
3015 memcpy(apr_msg->private_data, private_data, private_data_len);
3016}
3017
3018int ib_send_cm_apr(struct ib_cm_id *cm_id,
3019 enum ib_cm_apr_status status,
3020 void *info,
3021 u8 info_length,
3022 const void *private_data,
3023 u8 private_data_len)
3024{
3025 struct cm_id_private *cm_id_priv;
3026 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003027 unsigned long flags;
3028 int ret;
3029
3030 if ((private_data && private_data_len > IB_CM_APR_PRIVATE_DATA_SIZE) ||
3031 (info && info_length > IB_CM_APR_INFO_LENGTH))
3032 return -EINVAL;
3033
3034 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3035 spin_lock_irqsave(&cm_id_priv->lock, flags);
3036 if (cm_id->state != IB_CM_ESTABLISHED ||
3037 (cm_id->lap_state != IB_CM_LAP_RCVD &&
3038 cm_id->lap_state != IB_CM_MRA_LAP_SENT)) {
3039 ret = -EINVAL;
3040 goto out;
3041 }
3042
3043 ret = cm_alloc_msg(cm_id_priv, &msg);
3044 if (ret)
3045 goto out;
3046
3047 cm_format_apr((struct cm_apr_msg *) msg->mad, cm_id_priv, status,
3048 info, info_length, private_data, private_data_len);
Sean Hefty34816ad2005-10-25 10:51:39 -07003049 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003050 if (ret) {
3051 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3052 cm_free_msg(msg);
3053 return ret;
3054 }
3055
3056 cm_id->lap_state = IB_CM_LAP_IDLE;
3057out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3058 return ret;
3059}
3060EXPORT_SYMBOL(ib_send_cm_apr);
3061
3062static int cm_apr_handler(struct cm_work *work)
3063{
3064 struct cm_id_private *cm_id_priv;
3065 struct cm_apr_msg *apr_msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003066 int ret;
3067
3068 apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad;
3069 cm_id_priv = cm_acquire_id(apr_msg->remote_comm_id,
3070 apr_msg->local_comm_id);
3071 if (!cm_id_priv)
3072 return -EINVAL; /* Unmatched reply. */
3073
3074 work->cm_event.param.apr_rcvd.ap_status = apr_msg->ap_status;
3075 work->cm_event.param.apr_rcvd.apr_info = &apr_msg->info;
3076 work->cm_event.param.apr_rcvd.info_len = apr_msg->info_length;
3077 work->cm_event.private_data = &apr_msg->private_data;
3078
Sean Hefty24be6e82007-06-18 11:09:36 -07003079 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003080 if (cm_id_priv->id.state != IB_CM_ESTABLISHED ||
3081 (cm_id_priv->id.lap_state != IB_CM_LAP_SENT &&
3082 cm_id_priv->id.lap_state != IB_CM_MRA_LAP_RCVD)) {
Sean Hefty24be6e82007-06-18 11:09:36 -07003083 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003084 goto out;
3085 }
3086 cm_id_priv->id.lap_state = IB_CM_LAP_IDLE;
Sean Hefty34816ad2005-10-25 10:51:39 -07003087 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003088 cm_id_priv->msg = NULL;
3089
3090 ret = atomic_inc_and_test(&cm_id_priv->work_count);
3091 if (!ret)
3092 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty24be6e82007-06-18 11:09:36 -07003093 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003094
3095 if (ret)
3096 cm_process_work(cm_id_priv, work);
3097 else
3098 cm_deref_id(cm_id_priv);
3099 return 0;
3100out:
3101 cm_deref_id(cm_id_priv);
3102 return -EINVAL;
3103}
3104
3105static int cm_timewait_handler(struct cm_work *work)
3106{
3107 struct cm_timewait_info *timewait_info;
3108 struct cm_id_private *cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003109 int ret;
3110
3111 timewait_info = (struct cm_timewait_info *)work;
Sean Hefty85753292006-10-04 11:29:59 -07003112 spin_lock_irq(&cm.lock);
3113 list_del(&timewait_info->list);
3114 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003115
3116 cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
3117 timewait_info->work.remote_id);
3118 if (!cm_id_priv)
3119 return -EINVAL;
3120
Sean Hefty85753292006-10-04 11:29:59 -07003121 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003122 if (cm_id_priv->id.state != IB_CM_TIMEWAIT ||
3123 cm_id_priv->remote_qpn != timewait_info->remote_qpn) {
Sean Hefty85753292006-10-04 11:29:59 -07003124 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003125 goto out;
3126 }
3127 cm_id_priv->id.state = IB_CM_IDLE;
3128 ret = atomic_inc_and_test(&cm_id_priv->work_count);
3129 if (!ret)
3130 list_add_tail(&work->list, &cm_id_priv->work_list);
Sean Hefty85753292006-10-04 11:29:59 -07003131 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003132
3133 if (ret)
3134 cm_process_work(cm_id_priv, work);
3135 else
3136 cm_deref_id(cm_id_priv);
3137 return 0;
3138out:
3139 cm_deref_id(cm_id_priv);
3140 return -EINVAL;
3141}
3142
3143static void cm_format_sidr_req(struct cm_sidr_req_msg *sidr_req_msg,
3144 struct cm_id_private *cm_id_priv,
3145 struct ib_cm_sidr_req_param *param)
3146{
3147 cm_format_mad_hdr(&sidr_req_msg->hdr, CM_SIDR_REQ_ATTR_ID,
3148 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_SIDR));
3149 sidr_req_msg->request_id = cm_id_priv->id.local_id;
Roland Dreiera88f4882008-04-16 21:01:07 -07003150 sidr_req_msg->pkey = param->path->pkey;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003151 sidr_req_msg->service_id = param->service_id;
3152
3153 if (param->private_data && param->private_data_len)
3154 memcpy(sidr_req_msg->private_data, param->private_data,
3155 param->private_data_len);
3156}
3157
3158int ib_send_cm_sidr_req(struct ib_cm_id *cm_id,
3159 struct ib_cm_sidr_req_param *param)
3160{
3161 struct cm_id_private *cm_id_priv;
3162 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003163 unsigned long flags;
3164 int ret;
3165
3166 if (!param->path || (param->private_data &&
3167 param->private_data_len > IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE))
3168 return -EINVAL;
3169
3170 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
Mark Bloch9db0ff52016-10-27 16:36:27 +03003171 ret = cm_init_av_by_path(param->path, &cm_id_priv->av, cm_id_priv);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003172 if (ret)
3173 goto out;
3174
3175 cm_id->service_id = param->service_id;
Harvey Harrison9c3da092009-01-17 17:11:57 -08003176 cm_id->service_mask = ~cpu_to_be64(0);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003177 cm_id_priv->timeout_ms = param->timeout_ms;
3178 cm_id_priv->max_cm_retries = param->max_cm_retries;
3179 ret = cm_alloc_msg(cm_id_priv, &msg);
3180 if (ret)
3181 goto out;
3182
3183 cm_format_sidr_req((struct cm_sidr_req_msg *) msg->mad, cm_id_priv,
3184 param);
Sean Hefty34816ad2005-10-25 10:51:39 -07003185 msg->timeout_ms = cm_id_priv->timeout_ms;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003186 msg->context[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT;
3187
3188 spin_lock_irqsave(&cm_id_priv->lock, flags);
3189 if (cm_id->state == IB_CM_IDLE)
Sean Hefty34816ad2005-10-25 10:51:39 -07003190 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003191 else
3192 ret = -EINVAL;
3193
3194 if (ret) {
3195 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3196 cm_free_msg(msg);
3197 goto out;
3198 }
3199 cm_id->state = IB_CM_SIDR_REQ_SENT;
3200 cm_id_priv->msg = msg;
3201 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3202out:
3203 return ret;
3204}
3205EXPORT_SYMBOL(ib_send_cm_sidr_req);
3206
3207static void cm_format_sidr_req_event(struct cm_work *work,
3208 struct ib_cm_id *listen_id)
3209{
3210 struct cm_sidr_req_msg *sidr_req_msg;
3211 struct ib_cm_sidr_req_event_param *param;
3212
3213 sidr_req_msg = (struct cm_sidr_req_msg *)
3214 work->mad_recv_wc->recv_buf.mad;
3215 param = &work->cm_event.param.sidr_req_rcvd;
Sean Hefty97f52eb2005-08-13 21:05:57 -07003216 param->pkey = __be16_to_cpu(sidr_req_msg->pkey);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003217 param->listen_id = listen_id;
Haggai Eran15865e7d2015-07-30 17:50:17 +03003218 param->service_id = sidr_req_msg->service_id;
Haggai Eran24cad9a2015-07-30 17:50:21 +03003219 param->bth_pkey = cm_get_bth_pkey(work);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003220 param->port = work->port->port_num;
3221 work->cm_event.private_data = &sidr_req_msg->private_data;
3222}
3223
3224static int cm_sidr_req_handler(struct cm_work *work)
3225{
3226 struct ib_cm_id *cm_id;
3227 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
3228 struct cm_sidr_req_msg *sidr_req_msg;
3229 struct ib_wc *wc;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003230
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003231 cm_id = ib_create_cm_id(work->port->cm_dev->ib_device, NULL, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003232 if (IS_ERR(cm_id))
3233 return PTR_ERR(cm_id);
3234 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3235
3236 /* Record SGID/SLID and request ID for lookup. */
3237 sidr_req_msg = (struct cm_sidr_req_msg *)
3238 work->mad_recv_wc->recv_buf.mad;
3239 wc = work->mad_recv_wc->wc;
Sean Hefty97f52eb2005-08-13 21:05:57 -07003240 cm_id_priv->av.dgid.global.subnet_prefix = cpu_to_be64(wc->slid);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003241 cm_id_priv->av.dgid.global.interface_id = 0;
3242 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
Sean Heftyca222c62006-06-17 20:37:40 -07003243 work->mad_recv_wc->recv_buf.grh,
Hal Rosenstocka9770492005-07-27 11:45:40 -07003244 &cm_id_priv->av);
3245 cm_id_priv->id.remote_id = sidr_req_msg->request_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003246 cm_id_priv->tid = sidr_req_msg->hdr.tid;
3247 atomic_inc(&cm_id_priv->work_count);
3248
Sean Hefty24be6e82007-06-18 11:09:36 -07003249 spin_lock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003250 cur_cm_id_priv = cm_insert_remote_sidr(cm_id_priv);
3251 if (cur_cm_id_priv) {
Sean Hefty24be6e82007-06-18 11:09:36 -07003252 spin_unlock_irq(&cm.lock);
Sean Hefty9af57b72007-07-16 21:49:35 -07003253 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES].
3254 counter[CM_SIDR_REQ_COUNTER]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003255 goto out; /* Duplicate message. */
3256 }
Sean Hefty29c27312007-07-02 14:36:19 -07003257 cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD;
Sean Hefty07d357d2005-10-17 15:37:43 -07003258 cur_cm_id_priv = cm_find_listen(cm_id->device,
Haggai Eran73fec7f2015-07-30 17:50:26 +03003259 sidr_req_msg->service_id);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003260 if (!cur_cm_id_priv) {
Sean Hefty24be6e82007-06-18 11:09:36 -07003261 spin_unlock_irq(&cm.lock);
Sean Hefty6164c8c2007-07-02 16:14:14 -07003262 cm_reject_sidr_req(cm_id_priv, IB_SIDR_UNSUPPORTED);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003263 goto out; /* No match. */
3264 }
3265 atomic_inc(&cur_cm_id_priv->refcount);
Sean Hefty29963432011-02-23 08:17:40 -08003266 atomic_inc(&cm_id_priv->refcount);
Sean Hefty24be6e82007-06-18 11:09:36 -07003267 spin_unlock_irq(&cm.lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003268
3269 cm_id_priv->id.cm_handler = cur_cm_id_priv->id.cm_handler;
3270 cm_id_priv->id.context = cur_cm_id_priv->id.context;
3271 cm_id_priv->id.service_id = sidr_req_msg->service_id;
Harvey Harrison9c3da092009-01-17 17:11:57 -08003272 cm_id_priv->id.service_mask = ~cpu_to_be64(0);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003273
3274 cm_format_sidr_req_event(work, &cur_cm_id_priv->id);
3275 cm_process_work(cm_id_priv, work);
3276 cm_deref_id(cur_cm_id_priv);
3277 return 0;
3278out:
3279 ib_destroy_cm_id(&cm_id_priv->id);
3280 return -EINVAL;
3281}
3282
3283static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg,
3284 struct cm_id_private *cm_id_priv,
3285 struct ib_cm_sidr_rep_param *param)
3286{
3287 cm_format_mad_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID,
3288 cm_id_priv->tid);
3289 sidr_rep_msg->request_id = cm_id_priv->id.remote_id;
3290 sidr_rep_msg->status = param->status;
3291 cm_sidr_rep_set_qpn(sidr_rep_msg, cpu_to_be32(param->qp_num));
3292 sidr_rep_msg->service_id = cm_id_priv->id.service_id;
3293 sidr_rep_msg->qkey = cpu_to_be32(param->qkey);
3294
3295 if (param->info && param->info_length)
3296 memcpy(sidr_rep_msg->info, param->info, param->info_length);
3297
3298 if (param->private_data && param->private_data_len)
3299 memcpy(sidr_rep_msg->private_data, param->private_data,
3300 param->private_data_len);
3301}
3302
3303int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id,
3304 struct ib_cm_sidr_rep_param *param)
3305{
3306 struct cm_id_private *cm_id_priv;
3307 struct ib_mad_send_buf *msg;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003308 unsigned long flags;
3309 int ret;
3310
3311 if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) ||
3312 (param->private_data &&
3313 param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE))
3314 return -EINVAL;
3315
3316 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3317 spin_lock_irqsave(&cm_id_priv->lock, flags);
3318 if (cm_id->state != IB_CM_SIDR_REQ_RCVD) {
3319 ret = -EINVAL;
3320 goto error;
3321 }
3322
3323 ret = cm_alloc_msg(cm_id_priv, &msg);
3324 if (ret)
3325 goto error;
3326
3327 cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv,
3328 param);
Sean Hefty34816ad2005-10-25 10:51:39 -07003329 ret = ib_post_send_mad(msg, NULL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003330 if (ret) {
3331 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3332 cm_free_msg(msg);
3333 return ret;
3334 }
3335 cm_id->state = IB_CM_IDLE;
3336 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3337
3338 spin_lock_irqsave(&cm.lock, flags);
Doron Tsur0ca81a22015-10-11 15:58:17 +03003339 if (!RB_EMPTY_NODE(&cm_id_priv->sidr_id_node)) {
3340 rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
3341 RB_CLEAR_NODE(&cm_id_priv->sidr_id_node);
3342 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07003343 spin_unlock_irqrestore(&cm.lock, flags);
3344 return 0;
3345
3346error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3347 return ret;
3348}
3349EXPORT_SYMBOL(ib_send_cm_sidr_rep);
3350
3351static void cm_format_sidr_rep_event(struct cm_work *work)
3352{
3353 struct cm_sidr_rep_msg *sidr_rep_msg;
3354 struct ib_cm_sidr_rep_event_param *param;
3355
3356 sidr_rep_msg = (struct cm_sidr_rep_msg *)
3357 work->mad_recv_wc->recv_buf.mad;
3358 param = &work->cm_event.param.sidr_rep_rcvd;
3359 param->status = sidr_rep_msg->status;
3360 param->qkey = be32_to_cpu(sidr_rep_msg->qkey);
3361 param->qpn = be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg));
3362 param->info = &sidr_rep_msg->info;
3363 param->info_len = sidr_rep_msg->info_length;
3364 work->cm_event.private_data = &sidr_rep_msg->private_data;
3365}
3366
3367static int cm_sidr_rep_handler(struct cm_work *work)
3368{
3369 struct cm_sidr_rep_msg *sidr_rep_msg;
3370 struct cm_id_private *cm_id_priv;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003371
3372 sidr_rep_msg = (struct cm_sidr_rep_msg *)
3373 work->mad_recv_wc->recv_buf.mad;
3374 cm_id_priv = cm_acquire_id(sidr_rep_msg->request_id, 0);
3375 if (!cm_id_priv)
3376 return -EINVAL; /* Unmatched reply. */
3377
Sean Hefty24be6e82007-06-18 11:09:36 -07003378 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003379 if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) {
Sean Hefty24be6e82007-06-18 11:09:36 -07003380 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003381 goto out;
3382 }
3383 cm_id_priv->id.state = IB_CM_IDLE;
Sean Hefty34816ad2005-10-25 10:51:39 -07003384 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg);
Sean Hefty24be6e82007-06-18 11:09:36 -07003385 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003386
3387 cm_format_sidr_rep_event(work);
3388 cm_process_work(cm_id_priv, work);
3389 return 0;
3390out:
3391 cm_deref_id(cm_id_priv);
3392 return -EINVAL;
3393}
3394
3395static void cm_process_send_error(struct ib_mad_send_buf *msg,
3396 enum ib_wc_status wc_status)
3397{
3398 struct cm_id_private *cm_id_priv;
3399 struct ib_cm_event cm_event;
3400 enum ib_cm_state state;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003401 int ret;
3402
3403 memset(&cm_event, 0, sizeof cm_event);
3404 cm_id_priv = msg->context[0];
3405
3406 /* Discard old sends or ones without a response. */
Sean Hefty24be6e82007-06-18 11:09:36 -07003407 spin_lock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003408 state = (enum ib_cm_state) (unsigned long) msg->context[1];
3409 if (msg != cm_id_priv->msg || state != cm_id_priv->id.state)
3410 goto discard;
3411
Moni Shoua498683c2017-01-05 16:16:35 +02003412 pr_debug_ratelimited("CM: failed sending MAD in state %d. (%s)\n",
3413 state, ib_wc_status_msg(wc_status));
Hal Rosenstocka9770492005-07-27 11:45:40 -07003414 switch (state) {
3415 case IB_CM_REQ_SENT:
3416 case IB_CM_MRA_REQ_RCVD:
3417 cm_reset_to_idle(cm_id_priv);
3418 cm_event.event = IB_CM_REQ_ERROR;
3419 break;
3420 case IB_CM_REP_SENT:
3421 case IB_CM_MRA_REP_RCVD:
3422 cm_reset_to_idle(cm_id_priv);
3423 cm_event.event = IB_CM_REP_ERROR;
3424 break;
3425 case IB_CM_DREQ_SENT:
3426 cm_enter_timewait(cm_id_priv);
3427 cm_event.event = IB_CM_DREQ_ERROR;
3428 break;
3429 case IB_CM_SIDR_REQ_SENT:
3430 cm_id_priv->id.state = IB_CM_IDLE;
3431 cm_event.event = IB_CM_SIDR_REQ_ERROR;
3432 break;
3433 default:
3434 goto discard;
3435 }
Sean Hefty24be6e82007-06-18 11:09:36 -07003436 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003437 cm_event.param.send_status = wc_status;
3438
3439 /* No other events can occur on the cm_id at this point. */
3440 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event);
3441 cm_free_msg(msg);
3442 if (ret)
3443 ib_destroy_cm_id(&cm_id_priv->id);
3444 return;
3445discard:
Sean Hefty24be6e82007-06-18 11:09:36 -07003446 spin_unlock_irq(&cm_id_priv->lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003447 cm_free_msg(msg);
3448}
3449
3450static void cm_send_handler(struct ib_mad_agent *mad_agent,
3451 struct ib_mad_send_wc *mad_send_wc)
3452{
Sean Hefty34816ad2005-10-25 10:51:39 -07003453 struct ib_mad_send_buf *msg = mad_send_wc->send_buf;
Sean Hefty9af57b72007-07-16 21:49:35 -07003454 struct cm_port *port;
3455 u16 attr_index;
3456
3457 port = mad_agent->context;
3458 attr_index = be16_to_cpu(((struct ib_mad_hdr *)
3459 msg->mad)->attr_id) - CM_ATTR_ID_OFFSET;
3460
3461 /*
3462 * If the send was in response to a received message (context[0] is not
3463 * set to a cm_id), and is not a REJ, then it is a send that was
3464 * manually retried.
3465 */
3466 if (!msg->context[0] && (attr_index != CM_REJ_COUNTER))
3467 msg->retries = 1;
3468
3469 atomic_long_add(1 + msg->retries,
3470 &port->counter_group[CM_XMIT].counter[attr_index]);
3471 if (msg->retries)
3472 atomic_long_add(msg->retries,
3473 &port->counter_group[CM_XMIT_RETRIES].
3474 counter[attr_index]);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003475
3476 switch (mad_send_wc->status) {
3477 case IB_WC_SUCCESS:
3478 case IB_WC_WR_FLUSH_ERR:
3479 cm_free_msg(msg);
3480 break;
3481 default:
3482 if (msg->context[0] && msg->context[1])
3483 cm_process_send_error(msg, mad_send_wc->status);
3484 else
3485 cm_free_msg(msg);
3486 break;
3487 }
3488}
3489
David Howellsc4028952006-11-22 14:57:56 +00003490static void cm_work_handler(struct work_struct *_work)
Hal Rosenstocka9770492005-07-27 11:45:40 -07003491{
David Howellsc4028952006-11-22 14:57:56 +00003492 struct cm_work *work = container_of(_work, struct cm_work, work.work);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003493 int ret;
3494
3495 switch (work->cm_event.event) {
3496 case IB_CM_REQ_RECEIVED:
3497 ret = cm_req_handler(work);
3498 break;
3499 case IB_CM_MRA_RECEIVED:
3500 ret = cm_mra_handler(work);
3501 break;
3502 case IB_CM_REJ_RECEIVED:
3503 ret = cm_rej_handler(work);
3504 break;
3505 case IB_CM_REP_RECEIVED:
3506 ret = cm_rep_handler(work);
3507 break;
3508 case IB_CM_RTU_RECEIVED:
3509 ret = cm_rtu_handler(work);
3510 break;
3511 case IB_CM_USER_ESTABLISHED:
3512 ret = cm_establish_handler(work);
3513 break;
3514 case IB_CM_DREQ_RECEIVED:
3515 ret = cm_dreq_handler(work);
3516 break;
3517 case IB_CM_DREP_RECEIVED:
3518 ret = cm_drep_handler(work);
3519 break;
3520 case IB_CM_SIDR_REQ_RECEIVED:
3521 ret = cm_sidr_req_handler(work);
3522 break;
3523 case IB_CM_SIDR_REP_RECEIVED:
3524 ret = cm_sidr_rep_handler(work);
3525 break;
3526 case IB_CM_LAP_RECEIVED:
3527 ret = cm_lap_handler(work);
3528 break;
3529 case IB_CM_APR_RECEIVED:
3530 ret = cm_apr_handler(work);
3531 break;
3532 case IB_CM_TIMEWAIT_EXIT:
3533 ret = cm_timewait_handler(work);
3534 break;
3535 default:
3536 ret = -EINVAL;
3537 break;
3538 }
3539 if (ret)
3540 cm_free_work(work);
3541}
3542
Sean Heftye1444b52006-11-28 14:57:13 -08003543static int cm_establish(struct ib_cm_id *cm_id)
Hal Rosenstocka9770492005-07-27 11:45:40 -07003544{
3545 struct cm_id_private *cm_id_priv;
3546 struct cm_work *work;
3547 unsigned long flags;
3548 int ret = 0;
Erez Shitritbe4b4992015-06-25 17:13:22 +03003549 struct cm_device *cm_dev;
3550
3551 cm_dev = ib_get_client_data(cm_id->device, &cm_client);
3552 if (!cm_dev)
3553 return -ENODEV;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003554
3555 work = kmalloc(sizeof *work, GFP_ATOMIC);
3556 if (!work)
3557 return -ENOMEM;
3558
3559 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3560 spin_lock_irqsave(&cm_id_priv->lock, flags);
3561 switch (cm_id->state)
3562 {
3563 case IB_CM_REP_SENT:
3564 case IB_CM_MRA_REP_RCVD:
3565 cm_id->state = IB_CM_ESTABLISHED;
3566 break;
3567 case IB_CM_ESTABLISHED:
3568 ret = -EISCONN;
3569 break;
3570 default:
3571 ret = -EINVAL;
3572 break;
3573 }
3574 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3575
3576 if (ret) {
3577 kfree(work);
3578 goto out;
3579 }
3580
3581 /*
3582 * The CM worker thread may try to destroy the cm_id before it
3583 * can execute this work item. To prevent potential deadlock,
3584 * we need to find the cm_id once we're in the context of the
3585 * worker thread, rather than holding a reference on it.
3586 */
David Howellsc4028952006-11-22 14:57:56 +00003587 INIT_DELAYED_WORK(&work->work, cm_work_handler);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003588 work->local_id = cm_id->local_id;
3589 work->remote_id = cm_id->remote_id;
3590 work->mad_recv_wc = NULL;
3591 work->cm_event.event = IB_CM_USER_ESTABLISHED;
Erez Shitritbe4b4992015-06-25 17:13:22 +03003592
3593 /* Check if the device started its remove_one */
Bart Van Assche943f44d2016-03-25 08:33:16 -07003594 spin_lock_irqsave(&cm.lock, flags);
Erez Shitritbe4b4992015-06-25 17:13:22 +03003595 if (!cm_dev->going_down) {
3596 queue_delayed_work(cm.wq, &work->work, 0);
3597 } else {
3598 kfree(work);
3599 ret = -ENODEV;
3600 }
Bart Van Assche943f44d2016-03-25 08:33:16 -07003601 spin_unlock_irqrestore(&cm.lock, flags);
Erez Shitritbe4b4992015-06-25 17:13:22 +03003602
Hal Rosenstocka9770492005-07-27 11:45:40 -07003603out:
3604 return ret;
3605}
Sean Heftye1444b52006-11-28 14:57:13 -08003606
3607static int cm_migrate(struct ib_cm_id *cm_id)
3608{
3609 struct cm_id_private *cm_id_priv;
Mark Bloch9db0ff52016-10-27 16:36:27 +03003610 struct cm_av tmp_av;
Sean Heftye1444b52006-11-28 14:57:13 -08003611 unsigned long flags;
Mark Bloch9db0ff52016-10-27 16:36:27 +03003612 int tmp_send_port_not_ready;
Sean Heftye1444b52006-11-28 14:57:13 -08003613 int ret = 0;
3614
3615 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3616 spin_lock_irqsave(&cm_id_priv->lock, flags);
3617 if (cm_id->state == IB_CM_ESTABLISHED &&
3618 (cm_id->lap_state == IB_CM_LAP_UNINIT ||
3619 cm_id->lap_state == IB_CM_LAP_IDLE)) {
3620 cm_id->lap_state = IB_CM_LAP_IDLE;
Mark Bloch9db0ff52016-10-27 16:36:27 +03003621 /* Swap address vector */
3622 tmp_av = cm_id_priv->av;
Sean Heftye1444b52006-11-28 14:57:13 -08003623 cm_id_priv->av = cm_id_priv->alt_av;
Mark Bloch9db0ff52016-10-27 16:36:27 +03003624 cm_id_priv->alt_av = tmp_av;
3625 /* Swap port send ready state */
3626 tmp_send_port_not_ready = cm_id_priv->prim_send_port_not_ready;
3627 cm_id_priv->prim_send_port_not_ready = cm_id_priv->altr_send_port_not_ready;
3628 cm_id_priv->altr_send_port_not_ready = tmp_send_port_not_ready;
Sean Heftye1444b52006-11-28 14:57:13 -08003629 } else
3630 ret = -EINVAL;
3631 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3632
3633 return ret;
3634}
3635
3636int ib_cm_notify(struct ib_cm_id *cm_id, enum ib_event_type event)
3637{
3638 int ret;
3639
3640 switch (event) {
3641 case IB_EVENT_COMM_EST:
3642 ret = cm_establish(cm_id);
3643 break;
3644 case IB_EVENT_PATH_MIG:
3645 ret = cm_migrate(cm_id);
3646 break;
3647 default:
3648 ret = -EINVAL;
3649 }
3650 return ret;
3651}
3652EXPORT_SYMBOL(ib_cm_notify);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003653
3654static void cm_recv_handler(struct ib_mad_agent *mad_agent,
Christoph Hellwigca281262016-01-04 14:15:58 +01003655 struct ib_mad_send_buf *send_buf,
Hal Rosenstocka9770492005-07-27 11:45:40 -07003656 struct ib_mad_recv_wc *mad_recv_wc)
3657{
Sean Hefty9af57b72007-07-16 21:49:35 -07003658 struct cm_port *port = mad_agent->context;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003659 struct cm_work *work;
3660 enum ib_cm_event_type event;
Sean Hefty9af57b72007-07-16 21:49:35 -07003661 u16 attr_id;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003662 int paths = 0;
Erez Shitritbe4b4992015-06-25 17:13:22 +03003663 int going_down = 0;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003664
3665 switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) {
3666 case CM_REQ_ATTR_ID:
3667 paths = 1 + (((struct cm_req_msg *) mad_recv_wc->recv_buf.mad)->
3668 alt_local_lid != 0);
3669 event = IB_CM_REQ_RECEIVED;
3670 break;
3671 case CM_MRA_ATTR_ID:
3672 event = IB_CM_MRA_RECEIVED;
3673 break;
3674 case CM_REJ_ATTR_ID:
3675 event = IB_CM_REJ_RECEIVED;
3676 break;
3677 case CM_REP_ATTR_ID:
3678 event = IB_CM_REP_RECEIVED;
3679 break;
3680 case CM_RTU_ATTR_ID:
3681 event = IB_CM_RTU_RECEIVED;
3682 break;
3683 case CM_DREQ_ATTR_ID:
3684 event = IB_CM_DREQ_RECEIVED;
3685 break;
3686 case CM_DREP_ATTR_ID:
3687 event = IB_CM_DREP_RECEIVED;
3688 break;
3689 case CM_SIDR_REQ_ATTR_ID:
3690 event = IB_CM_SIDR_REQ_RECEIVED;
3691 break;
3692 case CM_SIDR_REP_ATTR_ID:
3693 event = IB_CM_SIDR_REP_RECEIVED;
3694 break;
3695 case CM_LAP_ATTR_ID:
3696 paths = 1;
3697 event = IB_CM_LAP_RECEIVED;
3698 break;
3699 case CM_APR_ATTR_ID:
3700 event = IB_CM_APR_RECEIVED;
3701 break;
3702 default:
3703 ib_free_recv_mad(mad_recv_wc);
3704 return;
3705 }
3706
Sean Hefty9af57b72007-07-16 21:49:35 -07003707 attr_id = be16_to_cpu(mad_recv_wc->recv_buf.mad->mad_hdr.attr_id);
3708 atomic_long_inc(&port->counter_group[CM_RECV].
3709 counter[attr_id - CM_ATTR_ID_OFFSET]);
3710
Hal Rosenstocka9770492005-07-27 11:45:40 -07003711 work = kmalloc(sizeof *work + sizeof(struct ib_sa_path_rec) * paths,
3712 GFP_KERNEL);
3713 if (!work) {
3714 ib_free_recv_mad(mad_recv_wc);
3715 return;
3716 }
3717
David Howellsc4028952006-11-22 14:57:56 +00003718 INIT_DELAYED_WORK(&work->work, cm_work_handler);
Hal Rosenstocka9770492005-07-27 11:45:40 -07003719 work->cm_event.event = event;
3720 work->mad_recv_wc = mad_recv_wc;
Sean Hefty9af57b72007-07-16 21:49:35 -07003721 work->port = port;
Erez Shitritbe4b4992015-06-25 17:13:22 +03003722
3723 /* Check if the device started its remove_one */
3724 spin_lock_irq(&cm.lock);
3725 if (!port->cm_dev->going_down)
3726 queue_delayed_work(cm.wq, &work->work, 0);
3727 else
3728 going_down = 1;
3729 spin_unlock_irq(&cm.lock);
3730
3731 if (going_down) {
3732 kfree(work);
3733 ib_free_recv_mad(mad_recv_wc);
3734 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07003735}
3736
3737static int cm_init_qp_init_attr(struct cm_id_private *cm_id_priv,
3738 struct ib_qp_attr *qp_attr,
3739 int *qp_attr_mask)
3740{
3741 unsigned long flags;
3742 int ret;
3743
3744 spin_lock_irqsave(&cm_id_priv->lock, flags);
3745 switch (cm_id_priv->id.state) {
3746 case IB_CM_REQ_SENT:
3747 case IB_CM_MRA_REQ_RCVD:
3748 case IB_CM_REQ_RCVD:
3749 case IB_CM_MRA_REQ_SENT:
3750 case IB_CM_REP_RCVD:
3751 case IB_CM_MRA_REP_SENT:
3752 case IB_CM_REP_SENT:
3753 case IB_CM_MRA_REP_RCVD:
3754 case IB_CM_ESTABLISHED:
3755 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS |
3756 IB_QP_PKEY_INDEX | IB_QP_PORT;
Dotan Barake31353ea2006-10-24 13:35:27 -07003757 qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003758 if (cm_id_priv->responder_resources)
Sean Heftyc1f250c2006-08-28 11:55:52 -07003759 qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ |
3760 IB_ACCESS_REMOTE_ATOMIC;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003761 qp_attr->pkey_index = cm_id_priv->av.pkey_index;
3762 qp_attr->port_num = cm_id_priv->av.port->port_num;
3763 ret = 0;
3764 break;
3765 default:
3766 ret = -EINVAL;
3767 break;
3768 }
3769 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3770 return ret;
3771}
3772
3773static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv,
3774 struct ib_qp_attr *qp_attr,
3775 int *qp_attr_mask)
3776{
3777 unsigned long flags;
3778 int ret;
3779
3780 spin_lock_irqsave(&cm_id_priv->lock, flags);
3781 switch (cm_id_priv->id.state) {
3782 case IB_CM_REQ_RCVD:
3783 case IB_CM_MRA_REQ_SENT:
3784 case IB_CM_REP_RCVD:
3785 case IB_CM_MRA_REP_SENT:
3786 case IB_CM_REP_SENT:
3787 case IB_CM_MRA_REP_RCVD:
3788 case IB_CM_ESTABLISHED:
3789 *qp_attr_mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU |
Sean Heftyae7971a2005-10-24 12:33:56 -07003790 IB_QP_DEST_QPN | IB_QP_RQ_PSN;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003791 qp_attr->ah_attr = cm_id_priv->av.ah_attr;
3792 qp_attr->path_mtu = cm_id_priv->path_mtu;
3793 qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn);
3794 qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn);
Sean Heftyd26a3602011-05-13 10:46:20 -07003795 if (cm_id_priv->qp_type == IB_QPT_RC ||
3796 cm_id_priv->qp_type == IB_QPT_XRC_TGT) {
Sean Heftyae7971a2005-10-24 12:33:56 -07003797 *qp_attr_mask |= IB_QP_MAX_DEST_RD_ATOMIC |
3798 IB_QP_MIN_RNR_TIMER;
3799 qp_attr->max_dest_rd_atomic =
3800 cm_id_priv->responder_resources;
3801 qp_attr->min_rnr_timer = 0;
3802 }
Hal Rosenstocka9770492005-07-27 11:45:40 -07003803 if (cm_id_priv->alt_av.ah_attr.dlid) {
3804 *qp_attr_mask |= IB_QP_ALT_PATH;
Sean Hefty0d8fdfd2006-06-30 14:10:14 -07003805 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
Sean Heftye1444b52006-11-28 14:57:13 -08003806 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
Sean Hefty1d846122007-06-18 11:09:37 -07003807 qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003808 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3809 }
3810 ret = 0;
3811 break;
3812 default:
3813 ret = -EINVAL;
3814 break;
3815 }
3816 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3817 return ret;
3818}
3819
3820static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv,
3821 struct ib_qp_attr *qp_attr,
3822 int *qp_attr_mask)
3823{
3824 unsigned long flags;
3825 int ret;
3826
3827 spin_lock_irqsave(&cm_id_priv->lock, flags);
3828 switch (cm_id_priv->id.state) {
Sean Hefty0fe313b2006-11-30 16:37:15 -08003829 /* Allow transition to RTS before sending REP */
3830 case IB_CM_REQ_RCVD:
3831 case IB_CM_MRA_REQ_SENT:
3832
Hal Rosenstocka9770492005-07-27 11:45:40 -07003833 case IB_CM_REP_RCVD:
3834 case IB_CM_MRA_REP_SENT:
3835 case IB_CM_REP_SENT:
3836 case IB_CM_MRA_REP_RCVD:
3837 case IB_CM_ESTABLISHED:
Sean Heftye1444b52006-11-28 14:57:13 -08003838 if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT) {
3839 *qp_attr_mask = IB_QP_STATE | IB_QP_SQ_PSN;
3840 qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn);
Sean Heftyd26a3602011-05-13 10:46:20 -07003841 switch (cm_id_priv->qp_type) {
3842 case IB_QPT_RC:
3843 case IB_QPT_XRC_INI:
3844 *qp_attr_mask |= IB_QP_RETRY_CNT | IB_QP_RNR_RETRY |
Sean Heftye1444b52006-11-28 14:57:13 -08003845 IB_QP_MAX_QP_RD_ATOMIC;
Sean Heftye1444b52006-11-28 14:57:13 -08003846 qp_attr->retry_cnt = cm_id_priv->retry_count;
3847 qp_attr->rnr_retry = cm_id_priv->rnr_retry_count;
Sean Heftyd26a3602011-05-13 10:46:20 -07003848 qp_attr->max_rd_atomic = cm_id_priv->initiator_depth;
3849 /* fall through */
3850 case IB_QPT_XRC_TGT:
3851 *qp_attr_mask |= IB_QP_TIMEOUT;
3852 qp_attr->timeout = cm_id_priv->av.timeout;
3853 break;
3854 default:
3855 break;
Sean Heftye1444b52006-11-28 14:57:13 -08003856 }
3857 if (cm_id_priv->alt_av.ah_attr.dlid) {
3858 *qp_attr_mask |= IB_QP_PATH_MIG_STATE;
3859 qp_attr->path_mig_state = IB_MIG_REARM;
3860 }
3861 } else {
3862 *qp_attr_mask = IB_QP_ALT_PATH | IB_QP_PATH_MIG_STATE;
3863 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num;
3864 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index;
Sean Hefty1d846122007-06-18 11:09:37 -07003865 qp_attr->alt_timeout = cm_id_priv->alt_av.timeout;
Sean Heftye1444b52006-11-28 14:57:13 -08003866 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
Hal Rosenstocka9770492005-07-27 11:45:40 -07003867 qp_attr->path_mig_state = IB_MIG_REARM;
3868 }
3869 ret = 0;
3870 break;
3871 default:
3872 ret = -EINVAL;
3873 break;
3874 }
3875 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3876 return ret;
3877}
3878
3879int ib_cm_init_qp_attr(struct ib_cm_id *cm_id,
3880 struct ib_qp_attr *qp_attr,
3881 int *qp_attr_mask)
3882{
3883 struct cm_id_private *cm_id_priv;
3884 int ret;
3885
3886 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3887 switch (qp_attr->qp_state) {
3888 case IB_QPS_INIT:
3889 ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask);
3890 break;
3891 case IB_QPS_RTR:
3892 ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask);
3893 break;
3894 case IB_QPS_RTS:
3895 ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask);
3896 break;
3897 default:
3898 ret = -EINVAL;
3899 break;
3900 }
3901 return ret;
3902}
3903EXPORT_SYMBOL(ib_cm_init_qp_attr);
3904
Sean Hefty9af57b72007-07-16 21:49:35 -07003905static ssize_t cm_show_counter(struct kobject *obj, struct attribute *attr,
3906 char *buf)
3907{
3908 struct cm_counter_group *group;
3909 struct cm_counter_attribute *cm_attr;
3910
3911 group = container_of(obj, struct cm_counter_group, obj);
3912 cm_attr = container_of(attr, struct cm_counter_attribute, attr);
3913
3914 return sprintf(buf, "%ld\n",
3915 atomic_long_read(&group->counter[cm_attr->index]));
3916}
3917
Emese Revfy52cf25d2010-01-19 02:58:23 +01003918static const struct sysfs_ops cm_counter_ops = {
Sean Hefty9af57b72007-07-16 21:49:35 -07003919 .show = cm_show_counter
3920};
3921
3922static struct kobj_type cm_counter_obj_type = {
3923 .sysfs_ops = &cm_counter_ops,
3924 .default_attrs = cm_counter_default_attrs
3925};
3926
3927static void cm_release_port_obj(struct kobject *obj)
3928{
3929 struct cm_port *cm_port;
3930
Sean Hefty9af57b72007-07-16 21:49:35 -07003931 cm_port = container_of(obj, struct cm_port, port_obj);
3932 kfree(cm_port);
3933}
3934
3935static struct kobj_type cm_port_obj_type = {
3936 .release = cm_release_port_obj
3937};
3938
Al Viro2c9ede52011-07-23 20:24:48 -04003939static char *cm_devnode(struct device *dev, umode_t *mode)
Roland Dreierc3af0982011-05-23 11:21:47 -07003940{
Goldwyn Rodriguesb2bc4782011-07-04 09:26:57 -07003941 if (mode)
3942 *mode = 0666;
Roland Dreierc3af0982011-05-23 11:21:47 -07003943 return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
3944}
3945
Sean Hefty9af57b72007-07-16 21:49:35 -07003946struct class cm_class = {
Roland Dreierc3af0982011-05-23 11:21:47 -07003947 .owner = THIS_MODULE,
Sean Hefty9af57b72007-07-16 21:49:35 -07003948 .name = "infiniband_cm",
Roland Dreierc3af0982011-05-23 11:21:47 -07003949 .devnode = cm_devnode,
Sean Hefty9af57b72007-07-16 21:49:35 -07003950};
3951EXPORT_SYMBOL(cm_class);
3952
Sean Hefty9af57b72007-07-16 21:49:35 -07003953static int cm_create_port_fs(struct cm_port *port)
3954{
3955 int i, ret;
3956
3957 ret = kobject_init_and_add(&port->port_obj, &cm_port_obj_type,
Greg Kroah-Hartman110cf372008-05-27 10:17:53 +03003958 &port->cm_dev->device->kobj,
Sean Hefty9af57b72007-07-16 21:49:35 -07003959 "%d", port->port_num);
3960 if (ret) {
3961 kfree(port);
3962 return ret;
3963 }
3964
3965 for (i = 0; i < CM_COUNTER_GROUPS; i++) {
3966 ret = kobject_init_and_add(&port->counter_group[i].obj,
3967 &cm_counter_obj_type,
Roland Dreier7c7a9bc2008-02-12 14:38:27 -08003968 &port->port_obj,
Sean Hefty9af57b72007-07-16 21:49:35 -07003969 "%s", counter_group_names[i]);
3970 if (ret)
3971 goto error;
3972 }
3973
3974 return 0;
3975
3976error:
3977 while (i--)
Roland Dreier7c7a9bc2008-02-12 14:38:27 -08003978 kobject_put(&port->counter_group[i].obj);
3979 kobject_put(&port->port_obj);
Sean Hefty9af57b72007-07-16 21:49:35 -07003980 return ret;
3981
3982}
3983
3984static void cm_remove_port_fs(struct cm_port *port)
3985{
3986 int i;
3987
3988 for (i = 0; i < CM_COUNTER_GROUPS; i++)
Roland Dreier7c7a9bc2008-02-12 14:38:27 -08003989 kobject_put(&port->counter_group[i].obj);
Sean Hefty9af57b72007-07-16 21:49:35 -07003990
Roland Dreier7c7a9bc2008-02-12 14:38:27 -08003991 kobject_put(&port->port_obj);
Sean Hefty9af57b72007-07-16 21:49:35 -07003992}
3993
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03003994static void cm_add_one(struct ib_device *ib_device)
Hal Rosenstocka9770492005-07-27 11:45:40 -07003995{
3996 struct cm_device *cm_dev;
3997 struct cm_port *port;
3998 struct ib_mad_reg_req reg_req = {
3999 .mgmt_class = IB_MGMT_CLASS_CM,
Ira Weiny0f29b462014-08-08 19:00:55 -04004000 .mgmt_class_version = IB_CM_CLASS_VERSION,
Hal Rosenstocka9770492005-07-27 11:45:40 -07004001 };
4002 struct ib_port_modify port_modify = {
4003 .set_port_cap_mask = IB_PORT_CM_SUP
4004 };
4005 unsigned long flags;
4006 int ret;
Michael Wang091e6a42015-05-05 14:50:21 +02004007 int count = 0;
Hal Rosenstocka9770492005-07-27 11:45:40 -07004008 u8 i;
4009
Sean Hefty9af57b72007-07-16 21:49:35 -07004010 cm_dev = kzalloc(sizeof(*cm_dev) + sizeof(*port) *
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03004011 ib_device->phys_port_cnt, GFP_KERNEL);
Hal Rosenstocka9770492005-07-27 11:45:40 -07004012 if (!cm_dev)
4013 return;
4014
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03004015 cm_dev->ib_device = ib_device;
Or Gerlitz86bee4c2015-12-18 10:59:45 +02004016 cm_dev->ack_delay = ib_device->attrs.local_ca_ack_delay;
Erez Shitritbe4b4992015-06-25 17:13:22 +03004017 cm_dev->going_down = 0;
Greg Kroah-Hartman91bd4182008-07-21 20:03:34 -07004018 cm_dev->device = device_create(&cm_class, &ib_device->dev,
4019 MKDEV(0, 0), NULL,
4020 "%s", ib_device->name);
Jani Nikula3e340c02010-03-11 16:17:28 +00004021 if (IS_ERR(cm_dev->device)) {
Sean Hefty9af57b72007-07-16 21:49:35 -07004022 kfree(cm_dev);
4023 return;
4024 }
4025
Hal Rosenstocka9770492005-07-27 11:45:40 -07004026 set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask);
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03004027 for (i = 1; i <= ib_device->phys_port_cnt; i++) {
Michael Wang72219cea2015-05-05 14:50:34 +02004028 if (!rdma_cap_ib_cm(ib_device, i))
Michael Wang091e6a42015-05-05 14:50:21 +02004029 continue;
4030
Sean Hefty9af57b72007-07-16 21:49:35 -07004031 port = kzalloc(sizeof *port, GFP_KERNEL);
4032 if (!port)
4033 goto error1;
4034
4035 cm_dev->port[i-1] = port;
Hal Rosenstocka9770492005-07-27 11:45:40 -07004036 port->cm_dev = cm_dev;
4037 port->port_num = i;
Sean Hefty9af57b72007-07-16 21:49:35 -07004038
Mark Bloch9db0ff52016-10-27 16:36:27 +03004039 INIT_LIST_HEAD(&port->cm_priv_prim_list);
4040 INIT_LIST_HEAD(&port->cm_priv_altr_list);
4041
Sean Hefty9af57b72007-07-16 21:49:35 -07004042 ret = cm_create_port_fs(port);
4043 if (ret)
4044 goto error1;
4045
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03004046 port->mad_agent = ib_register_mad_agent(ib_device, i,
Hal Rosenstocka9770492005-07-27 11:45:40 -07004047 IB_QPT_GSI,
4048 &reg_req,
4049 0,
4050 cm_send_handler,
4051 cm_recv_handler,
Ira Weiny0f29b462014-08-08 19:00:55 -04004052 port,
4053 0);
Hal Rosenstocka9770492005-07-27 11:45:40 -07004054 if (IS_ERR(port->mad_agent))
Sean Hefty9af57b72007-07-16 21:49:35 -07004055 goto error2;
Hal Rosenstocka9770492005-07-27 11:45:40 -07004056
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03004057 ret = ib_modify_port(ib_device, i, 0, &port_modify);
Hal Rosenstocka9770492005-07-27 11:45:40 -07004058 if (ret)
Sean Hefty9af57b72007-07-16 21:49:35 -07004059 goto error3;
Michael Wang091e6a42015-05-05 14:50:21 +02004060
4061 count++;
Hal Rosenstocka9770492005-07-27 11:45:40 -07004062 }
Michael Wang091e6a42015-05-05 14:50:21 +02004063
4064 if (!count)
4065 goto free;
4066
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03004067 ib_set_client_data(ib_device, &cm_client, cm_dev);
Hal Rosenstocka9770492005-07-27 11:45:40 -07004068
4069 write_lock_irqsave(&cm.device_lock, flags);
4070 list_add_tail(&cm_dev->list, &cm.device_list);
4071 write_unlock_irqrestore(&cm.device_lock, flags);
4072 return;
4073
Sean Hefty9af57b72007-07-16 21:49:35 -07004074error3:
Sean Heftycf311cd2006-01-10 07:39:34 -08004075 ib_unregister_mad_agent(port->mad_agent);
Sean Hefty9af57b72007-07-16 21:49:35 -07004076error2:
4077 cm_remove_port_fs(port);
Sean Heftycf311cd2006-01-10 07:39:34 -08004078error1:
Hal Rosenstocka9770492005-07-27 11:45:40 -07004079 port_modify.set_port_cap_mask = 0;
4080 port_modify.clr_port_cap_mask = IB_PORT_CM_SUP;
4081 while (--i) {
Michael Wang72219cea2015-05-05 14:50:34 +02004082 if (!rdma_cap_ib_cm(ib_device, i))
Michael Wang091e6a42015-05-05 14:50:21 +02004083 continue;
4084
Sean Hefty9af57b72007-07-16 21:49:35 -07004085 port = cm_dev->port[i-1];
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03004086 ib_modify_port(ib_device, port->port_num, 0, &port_modify);
Hal Rosenstocka9770492005-07-27 11:45:40 -07004087 ib_unregister_mad_agent(port->mad_agent);
Sean Hefty9af57b72007-07-16 21:49:35 -07004088 cm_remove_port_fs(port);
Hal Rosenstocka9770492005-07-27 11:45:40 -07004089 }
Michael Wang091e6a42015-05-05 14:50:21 +02004090free:
Greg Kroah-Hartman110cf372008-05-27 10:17:53 +03004091 device_unregister(cm_dev->device);
Hefty, Seana7e80ce2008-09-30 10:36:54 -07004092 kfree(cm_dev);
Hal Rosenstocka9770492005-07-27 11:45:40 -07004093}
4094
Haggai Eran7c1eb452015-07-30 17:50:14 +03004095static void cm_remove_one(struct ib_device *ib_device, void *client_data)
Hal Rosenstocka9770492005-07-27 11:45:40 -07004096{
Haggai Eran7c1eb452015-07-30 17:50:14 +03004097 struct cm_device *cm_dev = client_data;
Hal Rosenstocka9770492005-07-27 11:45:40 -07004098 struct cm_port *port;
Mark Bloch9db0ff52016-10-27 16:36:27 +03004099 struct cm_id_private *cm_id_priv;
4100 struct ib_mad_agent *cur_mad_agent;
Hal Rosenstocka9770492005-07-27 11:45:40 -07004101 struct ib_port_modify port_modify = {
4102 .clr_port_cap_mask = IB_PORT_CM_SUP
4103 };
4104 unsigned long flags;
4105 int i;
4106
Hal Rosenstocka9770492005-07-27 11:45:40 -07004107 if (!cm_dev)
4108 return;
4109
4110 write_lock_irqsave(&cm.device_lock, flags);
4111 list_del(&cm_dev->list);
4112 write_unlock_irqrestore(&cm.device_lock, flags);
4113
Erez Shitritbe4b4992015-06-25 17:13:22 +03004114 spin_lock_irq(&cm.lock);
4115 cm_dev->going_down = 1;
4116 spin_unlock_irq(&cm.lock);
4117
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03004118 for (i = 1; i <= ib_device->phys_port_cnt; i++) {
Michael Wang72219cea2015-05-05 14:50:34 +02004119 if (!rdma_cap_ib_cm(ib_device, i))
Michael Wang091e6a42015-05-05 14:50:21 +02004120 continue;
4121
Sean Hefty9af57b72007-07-16 21:49:35 -07004122 port = cm_dev->port[i-1];
Greg Kroah-Hartmand4c41962008-05-27 10:17:53 +03004123 ib_modify_port(ib_device, port->port_num, 0, &port_modify);
Mark Bloch9db0ff52016-10-27 16:36:27 +03004124 /* Mark all the cm_id's as not valid */
4125 spin_lock_irq(&cm.lock);
4126 list_for_each_entry(cm_id_priv, &port->cm_priv_altr_list, altr_list)
4127 cm_id_priv->altr_send_port_not_ready = 1;
4128 list_for_each_entry(cm_id_priv, &port->cm_priv_prim_list, prim_list)
4129 cm_id_priv->prim_send_port_not_ready = 1;
4130 spin_unlock_irq(&cm.lock);
Erez Shitritbe4b4992015-06-25 17:13:22 +03004131 /*
4132 * We flush the queue here after the going_down set, this
4133 * verify that no new works will be queued in the recv handler,
4134 * after that we can call the unregister_mad_agent
4135 */
Sean Hefty84ba2842008-02-22 10:40:45 -08004136 flush_workqueue(cm.wq);
Mark Bloch9db0ff52016-10-27 16:36:27 +03004137 spin_lock_irq(&cm.state_lock);
4138 cur_mad_agent = port->mad_agent;
4139 port->mad_agent = NULL;
4140 spin_unlock_irq(&cm.state_lock);
4141 ib_unregister_mad_agent(cur_mad_agent);
Sean Hefty9af57b72007-07-16 21:49:35 -07004142 cm_remove_port_fs(port);
Hal Rosenstocka9770492005-07-27 11:45:40 -07004143 }
Mark Bloch9db0ff52016-10-27 16:36:27 +03004144
Greg Kroah-Hartman110cf372008-05-27 10:17:53 +03004145 device_unregister(cm_dev->device);
Hefty, Seana7e80ce2008-09-30 10:36:54 -07004146 kfree(cm_dev);
Hal Rosenstocka9770492005-07-27 11:45:40 -07004147}
4148
4149static int __init ib_cm_init(void)
4150{
4151 int ret;
4152
4153 memset(&cm, 0, sizeof cm);
4154 INIT_LIST_HEAD(&cm.device_list);
4155 rwlock_init(&cm.device_lock);
4156 spin_lock_init(&cm.lock);
Mark Bloch9db0ff52016-10-27 16:36:27 +03004157 spin_lock_init(&cm.state_lock);
Hal Rosenstocka9770492005-07-27 11:45:40 -07004158 cm.listen_service_table = RB_ROOT;
Harvey Harrison9c3da092009-01-17 17:11:57 -08004159 cm.listen_service_id = be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID);
Hal Rosenstocka9770492005-07-27 11:45:40 -07004160 cm.remote_id_table = RB_ROOT;
4161 cm.remote_qp_table = RB_ROOT;
4162 cm.remote_sidr_table = RB_ROOT;
4163 idr_init(&cm.local_id_table);
Sean Heftyf06d2652006-08-28 15:15:18 -07004164 get_random_bytes(&cm.random_id_operand, sizeof cm.random_id_operand);
Sean Hefty85753292006-10-04 11:29:59 -07004165 INIT_LIST_HEAD(&cm.timewait_list);
Hal Rosenstocka9770492005-07-27 11:45:40 -07004166
Sean Hefty9af57b72007-07-16 21:49:35 -07004167 ret = class_register(&cm_class);
Dotan Barak87d4abd2012-07-11 15:39:31 +00004168 if (ret) {
Sean Hefty9af57b72007-07-16 21:49:35 -07004169 ret = -ENOMEM;
4170 goto error1;
4171 }
4172
Dotan Barak87d4abd2012-07-11 15:39:31 +00004173 cm.wq = create_workqueue("ib_cm");
4174 if (!cm.wq) {
4175 ret = -ENOMEM;
4176 goto error2;
4177 }
4178
Hal Rosenstocka9770492005-07-27 11:45:40 -07004179 ret = ib_register_client(&cm_client);
4180 if (ret)
Dotan Barak87d4abd2012-07-11 15:39:31 +00004181 goto error3;
Hal Rosenstocka9770492005-07-27 11:45:40 -07004182
4183 return 0;
Dotan Barak87d4abd2012-07-11 15:39:31 +00004184error3:
Hal Rosenstocka9770492005-07-27 11:45:40 -07004185 destroy_workqueue(cm.wq);
Dotan Barak87d4abd2012-07-11 15:39:31 +00004186error2:
Sean Hefty9af57b72007-07-16 21:49:35 -07004187 class_unregister(&cm_class);
Dotan Barak87d4abd2012-07-11 15:39:31 +00004188error1:
4189 idr_destroy(&cm.local_id_table);
Hal Rosenstocka9770492005-07-27 11:45:40 -07004190 return ret;
4191}
4192
4193static void __exit ib_cm_cleanup(void)
4194{
Sean Hefty85753292006-10-04 11:29:59 -07004195 struct cm_timewait_info *timewait_info, *tmp;
4196
4197 spin_lock_irq(&cm.lock);
4198 list_for_each_entry(timewait_info, &cm.timewait_list, list)
4199 cancel_delayed_work(&timewait_info->work.work);
4200 spin_unlock_irq(&cm.lock);
4201
Sean Hefty84ba2842008-02-22 10:40:45 -08004202 ib_unregister_client(&cm_client);
Hal Rosenstocka9770492005-07-27 11:45:40 -07004203 destroy_workqueue(cm.wq);
Sean Hefty85753292006-10-04 11:29:59 -07004204
4205 list_for_each_entry_safe(timewait_info, tmp, &cm.timewait_list, list) {
4206 list_del(&timewait_info->list);
4207 kfree(timewait_info);
4208 }
4209
Sean Hefty9af57b72007-07-16 21:49:35 -07004210 class_unregister(&cm_class);
Roland Dreier5d7edb32005-10-24 10:53:25 -07004211 idr_destroy(&cm.local_id_table);
Hal Rosenstocka9770492005-07-27 11:45:40 -07004212}
4213
4214module_init(ib_cm_init);
4215module_exit(ib_cm_cleanup);
4216