aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/infiniband/core
diff options
context:
space:
mode:
authorRoland Dreier <roland@eddore.topspincom.com>2005-07-27 22:12:56 -0400
committerRoland Dreier <roland@eddore.topspincom.com>2005-07-27 22:12:56 -0400
commit2868bd281fef21d1e73d6b7648a41efc3d75f10c (patch)
tree0ad821cfcc9e3f9e8b662d026bec6bb6d4ce69ac /drivers/infiniband/core
parent6d376756f2cf3478d5a4fdb8d18e958948366b9d (diff)
parent41c018b7ecb60b1c2c4d5dee0cd37d32a94c45af (diff)
Merge /scratch/Ksrc/linux-git/
Diffstat (limited to 'drivers/infiniband/core')
-rw-r--r--drivers/infiniband/core/Makefile9
-rw-r--r--drivers/infiniband/core/agent.c22
-rw-r--r--drivers/infiniband/core/agent_priv.h3
-rw-r--r--drivers/infiniband/core/cm.c3324
-rw-r--r--drivers/infiniband/core/cm_msgs.h819
-rw-r--r--drivers/infiniband/core/fmr_pool.c7
-rw-r--r--drivers/infiniband/core/mad.c600
-rw-r--r--drivers/infiniband/core/mad_priv.h33
-rw-r--r--drivers/infiniband/core/mad_rmpp.c765
-rw-r--r--drivers/infiniband/core/mad_rmpp.h58
-rw-r--r--drivers/infiniband/core/sa_query.c206
-rw-r--r--drivers/infiniband/core/ucm.c1393
-rw-r--r--drivers/infiniband/core/ucm.h89
-rw-r--r--drivers/infiniband/core/user_mad.c299
-rw-r--r--drivers/infiniband/core/verbs.c35
15 files changed, 7233 insertions, 429 deletions
diff --git a/drivers/infiniband/core/Makefile b/drivers/infiniband/core/Makefile
index e1a7cf3e8636..10be36731ed7 100644
--- a/drivers/infiniband/core/Makefile
+++ b/drivers/infiniband/core/Makefile
@@ -1,15 +1,20 @@
1EXTRA_CFLAGS += -Idrivers/infiniband/include 1EXTRA_CFLAGS += -Idrivers/infiniband/include
2 2
3obj-$(CONFIG_INFINIBAND) += ib_core.o ib_mad.o ib_sa.o ib_umad.o 3obj-$(CONFIG_INFINIBAND) += ib_core.o ib_mad.o ib_sa.o \
4 ib_cm.o ib_umad.o ib_ucm.o
4obj-$(CONFIG_INFINIBAND_USER_VERBS) += ib_uverbs.o 5obj-$(CONFIG_INFINIBAND_USER_VERBS) += ib_uverbs.o
5 6
6ib_core-y := packer.o ud_header.o verbs.o sysfs.o \ 7ib_core-y := packer.o ud_header.o verbs.o sysfs.o \
7 device.o fmr_pool.o cache.o 8 device.o fmr_pool.o cache.o
8 9
9ib_mad-y := mad.o smi.o agent.o 10ib_mad-y := mad.o smi.o agent.o mad_rmpp.o
10 11
11ib_sa-y := sa_query.o 12ib_sa-y := sa_query.o
12 13
14ib_cm-y := cm.o
15
13ib_umad-y := user_mad.o 16ib_umad-y := user_mad.o
14 17
18ib_ucm-y := ucm.o
19
15ib_uverbs-y := uverbs_main.o uverbs_cmd.o uverbs_mem.o 20ib_uverbs-y := uverbs_main.o uverbs_cmd.o uverbs_mem.o
diff --git a/drivers/infiniband/core/agent.c b/drivers/infiniband/core/agent.c
index 23d1957c4b29..729f0b0d983a 100644
--- a/drivers/infiniband/core/agent.c
+++ b/drivers/infiniband/core/agent.c
@@ -134,7 +134,7 @@ static int agent_mad_send(struct ib_mad_agent *mad_agent,
134 sizeof(mad_priv->mad), 134 sizeof(mad_priv->mad),
135 DMA_TO_DEVICE); 135 DMA_TO_DEVICE);
136 gather_list.length = sizeof(mad_priv->mad); 136 gather_list.length = sizeof(mad_priv->mad);
137 gather_list.lkey = (*port_priv->mr).lkey; 137 gather_list.lkey = mad_agent->mr->lkey;
138 138
139 send_wr.next = NULL; 139 send_wr.next = NULL;
140 send_wr.opcode = IB_WR_SEND; 140 send_wr.opcode = IB_WR_SEND;
@@ -156,10 +156,10 @@ static int agent_mad_send(struct ib_mad_agent *mad_agent,
156 /* Should sgid be looked up ? */ 156 /* Should sgid be looked up ? */
157 ah_attr.grh.sgid_index = 0; 157 ah_attr.grh.sgid_index = 0;
158 ah_attr.grh.hop_limit = grh->hop_limit; 158 ah_attr.grh.hop_limit = grh->hop_limit;
159 ah_attr.grh.flow_label = be32_to_cpup( 159 ah_attr.grh.flow_label = be32_to_cpu(
160 &grh->version_tclass_flow) & 0xfffff; 160 grh->version_tclass_flow) & 0xfffff;
161 ah_attr.grh.traffic_class = (be32_to_cpup( 161 ah_attr.grh.traffic_class = (be32_to_cpu(
162 &grh->version_tclass_flow) >> 20) & 0xff; 162 grh->version_tclass_flow) >> 20) & 0xff;
163 memcpy(ah_attr.grh.dgid.raw, 163 memcpy(ah_attr.grh.dgid.raw,
164 grh->sgid.raw, 164 grh->sgid.raw,
165 sizeof(ah_attr.grh.dgid)); 165 sizeof(ah_attr.grh.dgid));
@@ -322,22 +322,12 @@ int ib_agent_port_open(struct ib_device *device, int port_num)
322 goto error3; 322 goto error3;
323 } 323 }
324 324
325 port_priv->mr = ib_get_dma_mr(port_priv->smp_agent->qp->pd,
326 IB_ACCESS_LOCAL_WRITE);
327 if (IS_ERR(port_priv->mr)) {
328 printk(KERN_ERR SPFX "Couldn't get DMA MR\n");
329 ret = PTR_ERR(port_priv->mr);
330 goto error4;
331 }
332
333 spin_lock_irqsave(&ib_agent_port_list_lock, flags); 325 spin_lock_irqsave(&ib_agent_port_list_lock, flags);
334 list_add_tail(&port_priv->port_list, &ib_agent_port_list); 326 list_add_tail(&port_priv->port_list, &ib_agent_port_list);
335 spin_unlock_irqrestore(&ib_agent_port_list_lock, flags); 327 spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
336 328
337 return 0; 329 return 0;
338 330
339error4:
340 ib_unregister_mad_agent(port_priv->perf_mgmt_agent);
341error3: 331error3:
342 ib_unregister_mad_agent(port_priv->smp_agent); 332 ib_unregister_mad_agent(port_priv->smp_agent);
343error2: 333error2:
@@ -361,8 +351,6 @@ int ib_agent_port_close(struct ib_device *device, int port_num)
361 list_del(&port_priv->port_list); 351 list_del(&port_priv->port_list);
362 spin_unlock_irqrestore(&ib_agent_port_list_lock, flags); 352 spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
363 353
364 ib_dereg_mr(port_priv->mr);
365
366 ib_unregister_mad_agent(port_priv->perf_mgmt_agent); 354 ib_unregister_mad_agent(port_priv->perf_mgmt_agent);
367 ib_unregister_mad_agent(port_priv->smp_agent); 355 ib_unregister_mad_agent(port_priv->smp_agent);
368 kfree(port_priv); 356 kfree(port_priv);
diff --git a/drivers/infiniband/core/agent_priv.h b/drivers/infiniband/core/agent_priv.h
index 17a0cce5813c..17435af1e914 100644
--- a/drivers/infiniband/core/agent_priv.h
+++ b/drivers/infiniband/core/agent_priv.h
@@ -33,7 +33,7 @@
33 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 33 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 * SOFTWARE. 34 * SOFTWARE.
35 * 35 *
36 * $Id: agent_priv.h 1389 2004-12-27 22:56:47Z roland $ 36 * $Id: agent_priv.h 1640 2005-01-24 22:39:02Z halr $
37 */ 37 */
38 38
39#ifndef __IB_AGENT_PRIV_H__ 39#ifndef __IB_AGENT_PRIV_H__
@@ -57,7 +57,6 @@ struct ib_agent_port_private {
57 int port_num; 57 int port_num;
58 struct ib_mad_agent *smp_agent; /* SM class */ 58 struct ib_mad_agent *smp_agent; /* SM class */
59 struct ib_mad_agent *perf_mgmt_agent; /* PerfMgmt class */ 59 struct ib_mad_agent *perf_mgmt_agent; /* PerfMgmt class */
60 struct ib_mr *mr;
61}; 60};
62 61
63#endif /* __IB_AGENT_PRIV_H__ */ 62#endif /* __IB_AGENT_PRIV_H__ */
diff --git a/drivers/infiniband/core/cm.c b/drivers/infiniband/core/cm.c
new file mode 100644
index 000000000000..403ed125d8f4
--- /dev/null
+++ b/drivers/infiniband/core/cm.c
@@ -0,0 +1,3324 @@
1/*
2 * Copyright (c) 2004, 2005 Intel Corporation. All rights reserved.
3 * Copyright (c) 2004 Topspin Corporation. All rights reserved.
4 * Copyright (c) 2004, 2005 Voltaire Corporation. All rights reserved.
5 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
6 *
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
12 *
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
15 * conditions are met:
16 *
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer.
20 *
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 * SOFTWARE.
34 *
35 * $Id: cm.c 2821 2005-07-08 17:07:28Z sean.hefty $
36 */
37#include <linux/dma-mapping.h>
38#include <linux/err.h>
39#include <linux/idr.h>
40#include <linux/interrupt.h>
41#include <linux/pci.h>
42#include <linux/rbtree.h>
43#include <linux/spinlock.h>
44#include <linux/workqueue.h>
45
46#include <ib_cache.h>
47#include <ib_cm.h>
48#include "cm_msgs.h"
49
50MODULE_AUTHOR("Sean Hefty");
51MODULE_DESCRIPTION("InfiniBand CM");
52MODULE_LICENSE("Dual BSD/GPL");
53
54static void cm_add_one(struct ib_device *device);
55static void cm_remove_one(struct ib_device *device);
56
57static struct ib_client cm_client = {
58 .name = "cm",
59 .add = cm_add_one,
60 .remove = cm_remove_one
61};
62
63static struct ib_cm {
64 spinlock_t lock;
65 struct list_head device_list;
66 rwlock_t device_lock;
67 struct rb_root listen_service_table;
68 u64 listen_service_id;
69 /* struct rb_root peer_service_table; todo: fix peer to peer */
70 struct rb_root remote_qp_table;
71 struct rb_root remote_id_table;
72 struct rb_root remote_sidr_table;
73 struct idr local_id_table;
74 struct workqueue_struct *wq;
75} cm;
76
77struct cm_port {
78 struct cm_device *cm_dev;
79 struct ib_mad_agent *mad_agent;
80 u8 port_num;
81};
82
83struct cm_device {
84 struct list_head list;
85 struct ib_device *device;
86 u64 ca_guid;
87 struct cm_port port[0];
88};
89
90struct cm_av {
91 struct cm_port *port;
92 union ib_gid dgid;
93 struct ib_ah_attr ah_attr;
94 u16 pkey_index;
95 u8 packet_life_time;
96};
97
98struct cm_work {
99 struct work_struct work;
100 struct list_head list;
101 struct cm_port *port;
102 struct ib_mad_recv_wc *mad_recv_wc; /* Received MADs */
103 u32 local_id; /* Established / timewait */
104 u32 remote_id;
105 struct ib_cm_event cm_event;
106 struct ib_sa_path_rec path[0];
107};
108
109struct cm_timewait_info {
110 struct cm_work work; /* Must be first. */
111 struct rb_node remote_qp_node;
112 struct rb_node remote_id_node;
113 u64 remote_ca_guid;
114 u32 remote_qpn;
115 u8 inserted_remote_qp;
116 u8 inserted_remote_id;
117};
118
119struct cm_id_private {
120 struct ib_cm_id id;
121
122 struct rb_node service_node;
123 struct rb_node sidr_id_node;
124 spinlock_t lock;
125 wait_queue_head_t wait;
126 atomic_t refcount;
127
128 struct ib_mad_send_buf *msg;
129 struct cm_timewait_info *timewait_info;
130 /* todo: use alternate port on send failure */
131 struct cm_av av;
132 struct cm_av alt_av;
133
134 void *private_data;
135 u64 tid;
136 u32 local_qpn;
137 u32 remote_qpn;
138 u32 sq_psn;
139 u32 rq_psn;
140 int timeout_ms;
141 enum ib_mtu path_mtu;
142 u8 private_data_len;
143 u8 max_cm_retries;
144 u8 peer_to_peer;
145 u8 responder_resources;
146 u8 initiator_depth;
147 u8 local_ack_timeout;
148 u8 retry_count;
149 u8 rnr_retry_count;
150 u8 service_timeout;
151
152 struct list_head work_list;
153 atomic_t work_count;
154};
155
156static void cm_work_handler(void *data);
157
158static inline void cm_deref_id(struct cm_id_private *cm_id_priv)
159{
160 if (atomic_dec_and_test(&cm_id_priv->refcount))
161 wake_up(&cm_id_priv->wait);
162}
163
164static int cm_alloc_msg(struct cm_id_private *cm_id_priv,
165 struct ib_mad_send_buf **msg)
166{
167 struct ib_mad_agent *mad_agent;
168 struct ib_mad_send_buf *m;
169 struct ib_ah *ah;
170
171 mad_agent = cm_id_priv->av.port->mad_agent;
172 ah = ib_create_ah(mad_agent->qp->pd, &cm_id_priv->av.ah_attr);
173 if (IS_ERR(ah))
174 return PTR_ERR(ah);
175
176 m = ib_create_send_mad(mad_agent, 1, cm_id_priv->av.pkey_index,
177 ah, 0, sizeof(struct ib_mad_hdr),
178 sizeof(struct ib_mad)-sizeof(struct ib_mad_hdr),
179 GFP_ATOMIC);
180 if (IS_ERR(m)) {
181 ib_destroy_ah(ah);
182 return PTR_ERR(m);
183 }
184
185 /* Timeout set by caller if response is expected. */
186 m->send_wr.wr.ud.retries = cm_id_priv->max_cm_retries;
187
188 atomic_inc(&cm_id_priv->refcount);
189 m->context[0] = cm_id_priv;
190 *msg = m;
191 return 0;
192}
193
194static int cm_alloc_response_msg(struct cm_port *port,
195 struct ib_mad_recv_wc *mad_recv_wc,
196 struct ib_mad_send_buf **msg)
197{
198 struct ib_mad_send_buf *m;
199 struct ib_ah *ah;
200
201 ah = ib_create_ah_from_wc(port->mad_agent->qp->pd, mad_recv_wc->wc,
202 mad_recv_wc->recv_buf.grh, port->port_num);
203 if (IS_ERR(ah))
204 return PTR_ERR(ah);
205
206 m = ib_create_send_mad(port->mad_agent, 1, mad_recv_wc->wc->pkey_index,
207 ah, 0, sizeof(struct ib_mad_hdr),
208 sizeof(struct ib_mad)-sizeof(struct ib_mad_hdr),
209 GFP_ATOMIC);
210 if (IS_ERR(m)) {
211 ib_destroy_ah(ah);
212 return PTR_ERR(m);
213 }
214 *msg = m;
215 return 0;
216}
217
218static void cm_free_msg(struct ib_mad_send_buf *msg)
219{
220 ib_destroy_ah(msg->send_wr.wr.ud.ah);
221 if (msg->context[0])
222 cm_deref_id(msg->context[0]);
223 ib_free_send_mad(msg);
224}
225
226static void * cm_copy_private_data(const void *private_data,
227 u8 private_data_len)
228{
229 void *data;
230
231 if (!private_data || !private_data_len)
232 return NULL;
233
234 data = kmalloc(private_data_len, GFP_KERNEL);
235 if (!data)
236 return ERR_PTR(-ENOMEM);
237
238 memcpy(data, private_data, private_data_len);
239 return data;
240}
241
242static void cm_set_private_data(struct cm_id_private *cm_id_priv,
243 void *private_data, u8 private_data_len)
244{
245 if (cm_id_priv->private_data && cm_id_priv->private_data_len)
246 kfree(cm_id_priv->private_data);
247
248 cm_id_priv->private_data = private_data;
249 cm_id_priv->private_data_len = private_data_len;
250}
251
252static void cm_set_ah_attr(struct ib_ah_attr *ah_attr, u8 port_num,
253 u16 dlid, u8 sl, u16 src_path_bits)
254{
255 memset(ah_attr, 0, sizeof ah_attr);
256 ah_attr->dlid = be16_to_cpu(dlid);
257 ah_attr->sl = sl;
258 ah_attr->src_path_bits = src_path_bits;
259 ah_attr->port_num = port_num;
260}
261
262static void cm_init_av_for_response(struct cm_port *port,
263 struct ib_wc *wc, struct cm_av *av)
264{
265 av->port = port;
266 av->pkey_index = wc->pkey_index;
267 cm_set_ah_attr(&av->ah_attr, port->port_num, cpu_to_be16(wc->slid),
268 wc->sl, wc->dlid_path_bits);
269}
270
271static int cm_init_av_by_path(struct ib_sa_path_rec *path, struct cm_av *av)
272{
273 struct cm_device *cm_dev;
274 struct cm_port *port = NULL;
275 unsigned long flags;
276 int ret;
277 u8 p;
278
279 read_lock_irqsave(&cm.device_lock, flags);
280 list_for_each_entry(cm_dev, &cm.device_list, list) {
281 if (!ib_find_cached_gid(cm_dev->device, &path->sgid,
282 &p, NULL)) {
283 port = &cm_dev->port[p-1];
284 break;
285 }
286 }
287 read_unlock_irqrestore(&cm.device_lock, flags);
288
289 if (!port)
290 return -EINVAL;
291
292 ret = ib_find_cached_pkey(cm_dev->device, port->port_num,
293 be16_to_cpu(path->pkey), &av->pkey_index);
294 if (ret)
295 return ret;
296
297 av->port = port;
298 cm_set_ah_attr(&av->ah_attr, av->port->port_num, path->dlid,
299 path->sl, path->slid & 0x7F);
300 av->packet_life_time = path->packet_life_time;
301 return 0;
302}
303
304static int cm_alloc_id(struct cm_id_private *cm_id_priv)
305{
306 unsigned long flags;
307 int ret;
308
309 do {
310 spin_lock_irqsave(&cm.lock, flags);
311 ret = idr_get_new_above(&cm.local_id_table, cm_id_priv, 1,
312 (int *) &cm_id_priv->id.local_id);
313 spin_unlock_irqrestore(&cm.lock, flags);
314 } while( (ret == -EAGAIN) && idr_pre_get(&cm.local_id_table, GFP_KERNEL) );
315 return ret;
316}
317
318static void cm_free_id(u32 local_id)
319{
320 unsigned long flags;
321
322 spin_lock_irqsave(&cm.lock, flags);
323 idr_remove(&cm.local_id_table, (int) local_id);
324 spin_unlock_irqrestore(&cm.lock, flags);
325}
326
327static struct cm_id_private * cm_get_id(u32 local_id, u32 remote_id)
328{
329 struct cm_id_private *cm_id_priv;
330
331 cm_id_priv = idr_find(&cm.local_id_table, (int) local_id);
332 if (cm_id_priv) {
333 if (cm_id_priv->id.remote_id == remote_id)
334 atomic_inc(&cm_id_priv->refcount);
335 else
336 cm_id_priv = NULL;
337 }
338
339 return cm_id_priv;
340}
341
342static struct cm_id_private * cm_acquire_id(u32 local_id, u32 remote_id)
343{
344 struct cm_id_private *cm_id_priv;
345 unsigned long flags;
346
347 spin_lock_irqsave(&cm.lock, flags);
348 cm_id_priv = cm_get_id(local_id, remote_id);
349 spin_unlock_irqrestore(&cm.lock, flags);
350
351 return cm_id_priv;
352}
353
354static struct cm_id_private * cm_insert_listen(struct cm_id_private *cm_id_priv)
355{
356 struct rb_node **link = &cm.listen_service_table.rb_node;
357 struct rb_node *parent = NULL;
358 struct cm_id_private *cur_cm_id_priv;
359 u64 service_id = cm_id_priv->id.service_id;
360 u64 service_mask = cm_id_priv->id.service_mask;
361
362 while (*link) {
363 parent = *link;
364 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
365 service_node);
366 if ((cur_cm_id_priv->id.service_mask & service_id) ==
367 (service_mask & cur_cm_id_priv->id.service_id))
368 return cm_id_priv;
369 if (service_id < cur_cm_id_priv->id.service_id)
370 link = &(*link)->rb_left;
371 else
372 link = &(*link)->rb_right;
373 }
374 rb_link_node(&cm_id_priv->service_node, parent, link);
375 rb_insert_color(&cm_id_priv->service_node, &cm.listen_service_table);
376 return NULL;
377}
378
379static struct cm_id_private * cm_find_listen(u64 service_id)
380{
381 struct rb_node *node = cm.listen_service_table.rb_node;
382 struct cm_id_private *cm_id_priv;
383
384 while (node) {
385 cm_id_priv = rb_entry(node, struct cm_id_private, service_node);
386 if ((cm_id_priv->id.service_mask & service_id) ==
387 (cm_id_priv->id.service_mask & cm_id_priv->id.service_id))
388 return cm_id_priv;
389 if (service_id < cm_id_priv->id.service_id)
390 node = node->rb_left;
391 else
392 node = node->rb_right;
393 }
394 return NULL;
395}
396
397static struct cm_timewait_info * cm_insert_remote_id(struct cm_timewait_info
398 *timewait_info)
399{
400 struct rb_node **link = &cm.remote_id_table.rb_node;
401 struct rb_node *parent = NULL;
402 struct cm_timewait_info *cur_timewait_info;
403 u64 remote_ca_guid = timewait_info->remote_ca_guid;
404 u32 remote_id = timewait_info->work.remote_id;
405
406 while (*link) {
407 parent = *link;
408 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
409 remote_id_node);
410 if (remote_id < cur_timewait_info->work.remote_id)
411 link = &(*link)->rb_left;
412 else if (remote_id > cur_timewait_info->work.remote_id)
413 link = &(*link)->rb_right;
414 else if (remote_ca_guid < cur_timewait_info->remote_ca_guid)
415 link = &(*link)->rb_left;
416 else if (remote_ca_guid > cur_timewait_info->remote_ca_guid)
417 link = &(*link)->rb_right;
418 else
419 return cur_timewait_info;
420 }
421 timewait_info->inserted_remote_id = 1;
422 rb_link_node(&timewait_info->remote_id_node, parent, link);
423 rb_insert_color(&timewait_info->remote_id_node, &cm.remote_id_table);
424 return NULL;
425}
426
427static struct cm_timewait_info * cm_find_remote_id(u64 remote_ca_guid,
428 u32 remote_id)
429{
430 struct rb_node *node = cm.remote_id_table.rb_node;
431 struct cm_timewait_info *timewait_info;
432
433 while (node) {
434 timewait_info = rb_entry(node, struct cm_timewait_info,
435 remote_id_node);
436 if (remote_id < timewait_info->work.remote_id)
437 node = node->rb_left;
438 else if (remote_id > timewait_info->work.remote_id)
439 node = node->rb_right;
440 else if (remote_ca_guid < timewait_info->remote_ca_guid)
441 node = node->rb_left;
442 else if (remote_ca_guid > timewait_info->remote_ca_guid)
443 node = node->rb_right;
444 else
445 return timewait_info;
446 }
447 return NULL;
448}
449
450static struct cm_timewait_info * cm_insert_remote_qpn(struct cm_timewait_info
451 *timewait_info)
452{
453 struct rb_node **link = &cm.remote_qp_table.rb_node;
454 struct rb_node *parent = NULL;
455 struct cm_timewait_info *cur_timewait_info;
456 u64 remote_ca_guid = timewait_info->remote_ca_guid;
457 u32 remote_qpn = timewait_info->remote_qpn;
458
459 while (*link) {
460 parent = *link;
461 cur_timewait_info = rb_entry(parent, struct cm_timewait_info,
462 remote_qp_node);
463 if (remote_qpn < cur_timewait_info->remote_qpn)
464 link = &(*link)->rb_left;
465 else if (remote_qpn > cur_timewait_info->remote_qpn)
466 link = &(*link)->rb_right;
467 else if (remote_ca_guid < cur_timewait_info->remote_ca_guid)
468 link = &(*link)->rb_left;
469 else if (remote_ca_guid > cur_timewait_info->remote_ca_guid)
470 link = &(*link)->rb_right;
471 else
472 return cur_timewait_info;
473 }
474 timewait_info->inserted_remote_qp = 1;
475 rb_link_node(&timewait_info->remote_qp_node, parent, link);
476 rb_insert_color(&timewait_info->remote_qp_node, &cm.remote_qp_table);
477 return NULL;
478}
479
480static struct cm_id_private * cm_insert_remote_sidr(struct cm_id_private
481 *cm_id_priv)
482{
483 struct rb_node **link = &cm.remote_sidr_table.rb_node;
484 struct rb_node *parent = NULL;
485 struct cm_id_private *cur_cm_id_priv;
486 union ib_gid *port_gid = &cm_id_priv->av.dgid;
487 u32 remote_id = cm_id_priv->id.remote_id;
488
489 while (*link) {
490 parent = *link;
491 cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
492 sidr_id_node);
493 if (remote_id < cur_cm_id_priv->id.remote_id)
494 link = &(*link)->rb_left;
495 else if (remote_id > cur_cm_id_priv->id.remote_id)
496 link = &(*link)->rb_right;
497 else {
498 int cmp;
499 cmp = memcmp(port_gid, &cur_cm_id_priv->av.dgid,
500 sizeof *port_gid);
501 if (cmp < 0)
502 link = &(*link)->rb_left;
503 else if (cmp > 0)
504 link = &(*link)->rb_right;
505 else
506 return cur_cm_id_priv;
507 }
508 }
509 rb_link_node(&cm_id_priv->sidr_id_node, parent, link);
510 rb_insert_color(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
511 return NULL;
512}
513
514static void cm_reject_sidr_req(struct cm_id_private *cm_id_priv,
515 enum ib_cm_sidr_status status)
516{
517 struct ib_cm_sidr_rep_param param;
518
519 memset(&param, 0, sizeof param);
520 param.status = status;
521 ib_send_cm_sidr_rep(&cm_id_priv->id, &param);
522}
523
524struct ib_cm_id *ib_create_cm_id(ib_cm_handler cm_handler,
525 void *context)
526{
527 struct cm_id_private *cm_id_priv;
528 int ret;
529
530 cm_id_priv = kmalloc(sizeof *cm_id_priv, GFP_KERNEL);
531 if (!cm_id_priv)
532 return ERR_PTR(-ENOMEM);
533
534 memset(cm_id_priv, 0, sizeof *cm_id_priv);
535 cm_id_priv->id.state = IB_CM_IDLE;
536 cm_id_priv->id.cm_handler = cm_handler;
537 cm_id_priv->id.context = context;
538 ret = cm_alloc_id(cm_id_priv);
539 if (ret)
540 goto error;
541
542 spin_lock_init(&cm_id_priv->lock);
543 init_waitqueue_head(&cm_id_priv->wait);
544 INIT_LIST_HEAD(&cm_id_priv->work_list);
545 atomic_set(&cm_id_priv->work_count, -1);
546 atomic_set(&cm_id_priv->refcount, 1);
547 return &cm_id_priv->id;
548
549error:
550 kfree(cm_id_priv);
551 return ERR_PTR(-ENOMEM);
552}
553EXPORT_SYMBOL(ib_create_cm_id);
554
555static struct cm_work * cm_dequeue_work(struct cm_id_private *cm_id_priv)
556{
557 struct cm_work *work;
558
559 if (list_empty(&cm_id_priv->work_list))
560 return NULL;
561
562 work = list_entry(cm_id_priv->work_list.next, struct cm_work, list);
563 list_del(&work->list);
564 return work;
565}
566
567static void cm_free_work(struct cm_work *work)
568{
569 if (work->mad_recv_wc)
570 ib_free_recv_mad(work->mad_recv_wc);
571 kfree(work);
572}
573
574static inline int cm_convert_to_ms(int iba_time)
575{
576 /* approximate conversion to ms from 4.096us x 2^iba_time */
577 return 1 << max(iba_time - 8, 0);
578}
579
580static void cm_cleanup_timewait(struct cm_timewait_info *timewait_info)
581{
582 unsigned long flags;
583
584 if (!timewait_info->inserted_remote_id &&
585 !timewait_info->inserted_remote_qp)
586 return;
587
588 spin_lock_irqsave(&cm.lock, flags);
589 if (timewait_info->inserted_remote_id) {
590 rb_erase(&timewait_info->remote_id_node, &cm.remote_id_table);
591 timewait_info->inserted_remote_id = 0;
592 }
593
594 if (timewait_info->inserted_remote_qp) {
595 rb_erase(&timewait_info->remote_qp_node, &cm.remote_qp_table);
596 timewait_info->inserted_remote_qp = 0;
597 }
598 spin_unlock_irqrestore(&cm.lock, flags);
599}
600
601static struct cm_timewait_info * cm_create_timewait_info(u32 local_id)
602{
603 struct cm_timewait_info *timewait_info;
604
605 timewait_info = kmalloc(sizeof *timewait_info, GFP_KERNEL);
606 if (!timewait_info)
607 return ERR_PTR(-ENOMEM);
608 memset(timewait_info, 0, sizeof *timewait_info);
609
610 timewait_info->work.local_id = local_id;
611 INIT_WORK(&timewait_info->work.work, cm_work_handler,
612 &timewait_info->work);
613 timewait_info->work.cm_event.event = IB_CM_TIMEWAIT_EXIT;
614 return timewait_info;
615}
616
617static void cm_enter_timewait(struct cm_id_private *cm_id_priv)
618{
619 int wait_time;
620
621 /*
622 * The cm_id could be destroyed by the user before we exit timewait.
623 * To protect against this, we search for the cm_id after exiting
624 * timewait before notifying the user that we've exited timewait.
625 */
626 cm_id_priv->id.state = IB_CM_TIMEWAIT;
627 wait_time = cm_convert_to_ms(cm_id_priv->local_ack_timeout);
628 queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work,
629 msecs_to_jiffies(wait_time));
630 cm_id_priv->timewait_info = NULL;
631}
632
633static void cm_reset_to_idle(struct cm_id_private *cm_id_priv)
634{
635 cm_id_priv->id.state = IB_CM_IDLE;
636 if (cm_id_priv->timewait_info) {
637 cm_cleanup_timewait(cm_id_priv->timewait_info);
638 kfree(cm_id_priv->timewait_info);
639 cm_id_priv->timewait_info = NULL;
640 }
641}
642
643void ib_destroy_cm_id(struct ib_cm_id *cm_id)
644{
645 struct cm_id_private *cm_id_priv;
646 struct cm_work *work;
647 unsigned long flags;
648
649 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
650retest:
651 spin_lock_irqsave(&cm_id_priv->lock, flags);
652 switch (cm_id->state) {
653 case IB_CM_LISTEN:
654 cm_id->state = IB_CM_IDLE;
655 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
656 spin_lock_irqsave(&cm.lock, flags);
657 rb_erase(&cm_id_priv->service_node, &cm.listen_service_table);
658 spin_unlock_irqrestore(&cm.lock, flags);
659 break;
660 case IB_CM_SIDR_REQ_SENT:
661 cm_id->state = IB_CM_IDLE;
662 ib_cancel_mad(cm_id_priv->av.port->mad_agent,
663 (unsigned long) cm_id_priv->msg);
664 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
665 break;
666 case IB_CM_SIDR_REQ_RCVD:
667 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
668 cm_reject_sidr_req(cm_id_priv, IB_SIDR_REJECT);
669 break;
670 case IB_CM_REQ_SENT:
671 case IB_CM_MRA_REQ_RCVD:
672 case IB_CM_REP_SENT:
673 case IB_CM_MRA_REP_RCVD:
674 ib_cancel_mad(cm_id_priv->av.port->mad_agent,
675 (unsigned long) cm_id_priv->msg);
676 /* Fall through */
677 case IB_CM_REQ_RCVD:
678 case IB_CM_MRA_REQ_SENT:
679 case IB_CM_REP_RCVD:
680 case IB_CM_MRA_REP_SENT:
681 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
682 ib_send_cm_rej(cm_id, IB_CM_REJ_TIMEOUT,
683 &cm_id_priv->av.port->cm_dev->ca_guid,
684 sizeof cm_id_priv->av.port->cm_dev->ca_guid,
685 NULL, 0);
686 break;
687 case IB_CM_ESTABLISHED:
688 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
689 ib_send_cm_dreq(cm_id, NULL, 0);
690 goto retest;
691 case IB_CM_DREQ_SENT:
692 ib_cancel_mad(cm_id_priv->av.port->mad_agent,
693 (unsigned long) cm_id_priv->msg);
694 cm_enter_timewait(cm_id_priv);
695 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
696 break;
697 case IB_CM_DREQ_RCVD:
698 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
699 ib_send_cm_drep(cm_id, NULL, 0);
700 break;
701 default:
702 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
703 break;
704 }
705
706 cm_free_id(cm_id->local_id);
707 atomic_dec(&cm_id_priv->refcount);
708 wait_event(cm_id_priv->wait, !atomic_read(&cm_id_priv->refcount));
709 while ((work = cm_dequeue_work(cm_id_priv)) != NULL)
710 cm_free_work(work);
711 if (cm_id_priv->private_data && cm_id_priv->private_data_len)
712 kfree(cm_id_priv->private_data);
713 kfree(cm_id_priv);
714}
715EXPORT_SYMBOL(ib_destroy_cm_id);
716
717int ib_cm_listen(struct ib_cm_id *cm_id,
718 u64 service_id,
719 u64 service_mask)
720{
721 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
722 unsigned long flags;
723 int ret = 0;
724
725 service_mask = service_mask ? service_mask : ~0ULL;
726 service_id &= service_mask;
727 if ((service_id & IB_SERVICE_ID_AGN_MASK) == IB_CM_ASSIGN_SERVICE_ID &&
728 (service_id != IB_CM_ASSIGN_SERVICE_ID))
729 return -EINVAL;
730
731 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
732 BUG_ON(cm_id->state != IB_CM_IDLE);
733
734 cm_id->state = IB_CM_LISTEN;
735
736 spin_lock_irqsave(&cm.lock, flags);
737 if (service_id == IB_CM_ASSIGN_SERVICE_ID) {
738 cm_id->service_id = __cpu_to_be64(cm.listen_service_id++);
739 cm_id->service_mask = ~0ULL;
740 } else {
741 cm_id->service_id = service_id;
742 cm_id->service_mask = service_mask;
743 }
744 cur_cm_id_priv = cm_insert_listen(cm_id_priv);
745 spin_unlock_irqrestore(&cm.lock, flags);
746
747 if (cur_cm_id_priv) {
748 cm_id->state = IB_CM_IDLE;
749 ret = -EBUSY;
750 }
751 return ret;
752}
753EXPORT_SYMBOL(ib_cm_listen);
754
755static u64 cm_form_tid(struct cm_id_private *cm_id_priv,
756 enum cm_msg_sequence msg_seq)
757{
758 u64 hi_tid, low_tid;
759
760 hi_tid = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32;
761 low_tid = (u64) (cm_id_priv->id.local_id | (msg_seq << 30));
762 return cpu_to_be64(hi_tid | low_tid);
763}
764
765static void cm_format_mad_hdr(struct ib_mad_hdr *hdr,
766 enum cm_msg_attr_id attr_id, u64 tid)
767{
768 hdr->base_version = IB_MGMT_BASE_VERSION;
769 hdr->mgmt_class = IB_MGMT_CLASS_CM;
770 hdr->class_version = IB_CM_CLASS_VERSION;
771 hdr->method = IB_MGMT_METHOD_SEND;
772 hdr->attr_id = attr_id;
773 hdr->tid = tid;
774}
775
776static void cm_format_req(struct cm_req_msg *req_msg,
777 struct cm_id_private *cm_id_priv,
778 struct ib_cm_req_param *param)
779{
780 cm_format_mad_hdr(&req_msg->hdr, CM_REQ_ATTR_ID,
781 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_REQ));
782
783 req_msg->local_comm_id = cm_id_priv->id.local_id;
784 req_msg->service_id = param->service_id;
785 req_msg->local_ca_guid = cm_id_priv->av.port->cm_dev->ca_guid;
786 cm_req_set_local_qpn(req_msg, cpu_to_be32(param->qp_num));
787 cm_req_set_resp_res(req_msg, param->responder_resources);
788 cm_req_set_init_depth(req_msg, param->initiator_depth);
789 cm_req_set_remote_resp_timeout(req_msg,
790 param->remote_cm_response_timeout);
791 cm_req_set_qp_type(req_msg, param->qp_type);
792 cm_req_set_flow_ctrl(req_msg, param->flow_control);
793 cm_req_set_starting_psn(req_msg, cpu_to_be32(param->starting_psn));
794 cm_req_set_local_resp_timeout(req_msg,
795 param->local_cm_response_timeout);
796 cm_req_set_retry_count(req_msg, param->retry_count);
797 req_msg->pkey = param->primary_path->pkey;
798 cm_req_set_path_mtu(req_msg, param->primary_path->mtu);
799 cm_req_set_rnr_retry_count(req_msg, param->rnr_retry_count);
800 cm_req_set_max_cm_retries(req_msg, param->max_cm_retries);
801 cm_req_set_srq(req_msg, param->srq);
802
803 req_msg->primary_local_lid = param->primary_path->slid;
804 req_msg->primary_remote_lid = param->primary_path->dlid;
805 req_msg->primary_local_gid = param->primary_path->sgid;
806 req_msg->primary_remote_gid = param->primary_path->dgid;
807 cm_req_set_primary_flow_label(req_msg, param->primary_path->flow_label);
808 cm_req_set_primary_packet_rate(req_msg, param->primary_path->rate);
809 req_msg->primary_traffic_class = param->primary_path->traffic_class;
810 req_msg->primary_hop_limit = param->primary_path->hop_limit;
811 cm_req_set_primary_sl(req_msg, param->primary_path->sl);
812 cm_req_set_primary_subnet_local(req_msg, 1); /* local only... */
813 cm_req_set_primary_local_ack_timeout(req_msg,
814 min(31, param->primary_path->packet_life_time + 1));
815
816 if (param->alternate_path) {
817 req_msg->alt_local_lid = param->alternate_path->slid;
818 req_msg->alt_remote_lid = param->alternate_path->dlid;
819 req_msg->alt_local_gid = param->alternate_path->sgid;
820 req_msg->alt_remote_gid = param->alternate_path->dgid;
821 cm_req_set_alt_flow_label(req_msg,
822 param->alternate_path->flow_label);
823 cm_req_set_alt_packet_rate(req_msg, param->alternate_path->rate);
824 req_msg->alt_traffic_class = param->alternate_path->traffic_class;
825 req_msg->alt_hop_limit = param->alternate_path->hop_limit;
826 cm_req_set_alt_sl(req_msg, param->alternate_path->sl);
827 cm_req_set_alt_subnet_local(req_msg, 1); /* local only... */
828 cm_req_set_alt_local_ack_timeout(req_msg,
829 min(31, param->alternate_path->packet_life_time + 1));
830 }
831
832 if (param->private_data && param->private_data_len)
833 memcpy(req_msg->private_data, param->private_data,
834 param->private_data_len);
835}
836
837static inline int cm_validate_req_param(struct ib_cm_req_param *param)
838{
839 /* peer-to-peer not supported */
840 if (param->peer_to_peer)
841 return -EINVAL;
842
843 if (!param->primary_path)
844 return -EINVAL;
845
846 if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC)
847 return -EINVAL;
848
849 if (param->private_data &&
850 param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE)
851 return -EINVAL;
852
853 if (param->alternate_path &&
854 (param->alternate_path->pkey != param->primary_path->pkey ||
855 param->alternate_path->mtu != param->primary_path->mtu))
856 return -EINVAL;
857
858 return 0;
859}
860
861int ib_send_cm_req(struct ib_cm_id *cm_id,
862 struct ib_cm_req_param *param)
863{
864 struct cm_id_private *cm_id_priv;
865 struct ib_send_wr *bad_send_wr;
866 struct cm_req_msg *req_msg;
867 unsigned long flags;
868 int ret;
869
870 ret = cm_validate_req_param(param);
871 if (ret)
872 return ret;
873
874 /* Verify that we're not in timewait. */
875 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
876 spin_lock_irqsave(&cm_id_priv->lock, flags);
877 if (cm_id->state != IB_CM_IDLE) {
878 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
879 ret = -EINVAL;
880 goto out;
881 }
882 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
883
884 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
885 id.local_id);
886 if (IS_ERR(cm_id_priv->timewait_info))
887 goto out;
888
889 ret = cm_init_av_by_path(param->primary_path, &cm_id_priv->av);
890 if (ret)
891 goto error1;
892 if (param->alternate_path) {
893 ret = cm_init_av_by_path(param->alternate_path,
894 &cm_id_priv->alt_av);
895 if (ret)
896 goto error1;
897 }
898 cm_id->service_id = param->service_id;
899 cm_id->service_mask = ~0ULL;
900 cm_id_priv->timeout_ms = cm_convert_to_ms(
901 param->primary_path->packet_life_time) * 2 +
902 cm_convert_to_ms(
903 param->remote_cm_response_timeout);
904 cm_id_priv->max_cm_retries = param->max_cm_retries;
905 cm_id_priv->initiator_depth = param->initiator_depth;
906 cm_id_priv->responder_resources = param->responder_resources;
907 cm_id_priv->retry_count = param->retry_count;
908 cm_id_priv->path_mtu = param->primary_path->mtu;
909
910 ret = cm_alloc_msg(cm_id_priv, &cm_id_priv->msg);
911 if (ret)
912 goto error1;
913
914 req_msg = (struct cm_req_msg *) cm_id_priv->msg->mad;
915 cm_format_req(req_msg, cm_id_priv, param);
916 cm_id_priv->tid = req_msg->hdr.tid;
917 cm_id_priv->msg->send_wr.wr.ud.timeout_ms = cm_id_priv->timeout_ms;
918 cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT;
919
920 cm_id_priv->local_qpn = cm_req_get_local_qpn(req_msg);
921 cm_id_priv->rq_psn = cm_req_get_starting_psn(req_msg);
922 cm_id_priv->local_ack_timeout =
923 cm_req_get_primary_local_ack_timeout(req_msg);
924
925 spin_lock_irqsave(&cm_id_priv->lock, flags);
926 ret = ib_post_send_mad(cm_id_priv->av.port->mad_agent,
927 &cm_id_priv->msg->send_wr, &bad_send_wr);
928 if (ret) {
929 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
930 goto error2;
931 }
932 BUG_ON(cm_id->state != IB_CM_IDLE);
933 cm_id->state = IB_CM_REQ_SENT;
934 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
935 return 0;
936
937error2: cm_free_msg(cm_id_priv->msg);
938error1: kfree(cm_id_priv->timewait_info);
939out: return ret;
940}
941EXPORT_SYMBOL(ib_send_cm_req);
942
943static int cm_issue_rej(struct cm_port *port,
944 struct ib_mad_recv_wc *mad_recv_wc,
945 enum ib_cm_rej_reason reason,
946 enum cm_msg_response msg_rejected,
947 void *ari, u8 ari_length)
948{
949 struct ib_mad_send_buf *msg = NULL;
950 struct ib_send_wr *bad_send_wr;
951 struct cm_rej_msg *rej_msg, *rcv_msg;
952 int ret;
953
954 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg);
955 if (ret)
956 return ret;
957
958 /* We just need common CM header information. Cast to any message. */
959 rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad;
960 rej_msg = (struct cm_rej_msg *) msg->mad;
961
962 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid);
963 rej_msg->remote_comm_id = rcv_msg->local_comm_id;
964 rej_msg->local_comm_id = rcv_msg->remote_comm_id;
965 cm_rej_set_msg_rejected(rej_msg, msg_rejected);
966 rej_msg->reason = reason;
967
968 if (ari && ari_length) {
969 cm_rej_set_reject_info_len(rej_msg, ari_length);
970 memcpy(rej_msg->ari, ari, ari_length);
971 }
972
973 ret = ib_post_send_mad(port->mad_agent, &msg->send_wr, &bad_send_wr);
974 if (ret)
975 cm_free_msg(msg);
976
977 return ret;
978}
979
980static inline int cm_is_active_peer(u64 local_ca_guid, u64 remote_ca_guid,
981 u32 local_qpn, u32 remote_qpn)
982{
983 return (be64_to_cpu(local_ca_guid) > be64_to_cpu(remote_ca_guid) ||
984 ((local_ca_guid == remote_ca_guid) &&
985 (be32_to_cpu(local_qpn) > be32_to_cpu(remote_qpn))));
986}
987
988static inline void cm_format_paths_from_req(struct cm_req_msg *req_msg,
989 struct ib_sa_path_rec *primary_path,
990 struct ib_sa_path_rec *alt_path)
991{
992 memset(primary_path, 0, sizeof *primary_path);
993 primary_path->dgid = req_msg->primary_local_gid;
994 primary_path->sgid = req_msg->primary_remote_gid;
995 primary_path->dlid = req_msg->primary_local_lid;
996 primary_path->slid = req_msg->primary_remote_lid;
997 primary_path->flow_label = cm_req_get_primary_flow_label(req_msg);
998 primary_path->hop_limit = req_msg->primary_hop_limit;
999 primary_path->traffic_class = req_msg->primary_traffic_class;
1000 primary_path->reversible = 1;
1001 primary_path->pkey = req_msg->pkey;
1002 primary_path->sl = cm_req_get_primary_sl(req_msg);
1003 primary_path->mtu_selector = IB_SA_EQ;
1004 primary_path->mtu = cm_req_get_path_mtu(req_msg);
1005 primary_path->rate_selector = IB_SA_EQ;
1006 primary_path->rate = cm_req_get_primary_packet_rate(req_msg);
1007 primary_path->packet_life_time_selector = IB_SA_EQ;
1008 primary_path->packet_life_time =
1009 cm_req_get_primary_local_ack_timeout(req_msg);
1010 primary_path->packet_life_time -= (primary_path->packet_life_time > 0);
1011
1012 if (req_msg->alt_local_lid) {
1013 memset(alt_path, 0, sizeof *alt_path);
1014 alt_path->dgid = req_msg->alt_local_gid;
1015 alt_path->sgid = req_msg->alt_remote_gid;
1016 alt_path->dlid = req_msg->alt_local_lid;
1017 alt_path->slid = req_msg->alt_remote_lid;
1018 alt_path->flow_label = cm_req_get_alt_flow_label(req_msg);
1019 alt_path->hop_limit = req_msg->alt_hop_limit;
1020 alt_path->traffic_class = req_msg->alt_traffic_class;
1021 alt_path->reversible = 1;
1022 alt_path->pkey = req_msg->pkey;
1023 alt_path->sl = cm_req_get_alt_sl(req_msg);
1024 alt_path->mtu_selector = IB_SA_EQ;
1025 alt_path->mtu = cm_req_get_path_mtu(req_msg);
1026 alt_path->rate_selector = IB_SA_EQ;
1027 alt_path->rate = cm_req_get_alt_packet_rate(req_msg);
1028 alt_path->packet_life_time_selector = IB_SA_EQ;
1029 alt_path->packet_life_time =
1030 cm_req_get_alt_local_ack_timeout(req_msg);
1031 alt_path->packet_life_time -= (alt_path->packet_life_time > 0);
1032 }
1033}
1034
1035static void cm_format_req_event(struct cm_work *work,
1036 struct cm_id_private *cm_id_priv,
1037 struct ib_cm_id *listen_id)
1038{
1039 struct cm_req_msg *req_msg;
1040 struct ib_cm_req_event_param *param;
1041
1042 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1043 param = &work->cm_event.param.req_rcvd;
1044 param->listen_id = listen_id;
1045 param->device = cm_id_priv->av.port->mad_agent->device;
1046 param->port = cm_id_priv->av.port->port_num;
1047 param->primary_path = &work->path[0];
1048 if (req_msg->alt_local_lid)
1049 param->alternate_path = &work->path[1];
1050 else
1051 param->alternate_path = NULL;
1052 param->remote_ca_guid = req_msg->local_ca_guid;
1053 param->remote_qkey = be32_to_cpu(req_msg->local_qkey);
1054 param->remote_qpn = be32_to_cpu(cm_req_get_local_qpn(req_msg));
1055 param->qp_type = cm_req_get_qp_type(req_msg);
1056 param->starting_psn = be32_to_cpu(cm_req_get_starting_psn(req_msg));
1057 param->responder_resources = cm_req_get_init_depth(req_msg);
1058 param->initiator_depth = cm_req_get_resp_res(req_msg);
1059 param->local_cm_response_timeout =
1060 cm_req_get_remote_resp_timeout(req_msg);
1061 param->flow_control = cm_req_get_flow_ctrl(req_msg);
1062 param->remote_cm_response_timeout =
1063 cm_req_get_local_resp_timeout(req_msg);
1064 param->retry_count = cm_req_get_retry_count(req_msg);
1065 param->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1066 param->srq = cm_req_get_srq(req_msg);
1067 work->cm_event.private_data = &req_msg->private_data;
1068}
1069
1070static void cm_process_work(struct cm_id_private *cm_id_priv,
1071 struct cm_work *work)
1072{
1073 unsigned long flags;
1074 int ret;
1075
1076 /* We will typically only have the current event to report. */
1077 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event);
1078 cm_free_work(work);
1079
1080 while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) {
1081 spin_lock_irqsave(&cm_id_priv->lock, flags);
1082 work = cm_dequeue_work(cm_id_priv);
1083 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1084 BUG_ON(!work);
1085 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id,
1086 &work->cm_event);
1087 cm_free_work(work);
1088 }
1089 cm_deref_id(cm_id_priv);
1090 if (ret)
1091 ib_destroy_cm_id(&cm_id_priv->id);
1092}
1093
1094static void cm_format_mra(struct cm_mra_msg *mra_msg,
1095 struct cm_id_private *cm_id_priv,
1096 enum cm_msg_response msg_mraed, u8 service_timeout,
1097 const void *private_data, u8 private_data_len)
1098{
1099 cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid);
1100 cm_mra_set_msg_mraed(mra_msg, msg_mraed);
1101 mra_msg->local_comm_id = cm_id_priv->id.local_id;
1102 mra_msg->remote_comm_id = cm_id_priv->id.remote_id;
1103 cm_mra_set_service_timeout(mra_msg, service_timeout);
1104
1105 if (private_data && private_data_len)
1106 memcpy(mra_msg->private_data, private_data, private_data_len);
1107}
1108
1109static void cm_format_rej(struct cm_rej_msg *rej_msg,
1110 struct cm_id_private *cm_id_priv,
1111 enum ib_cm_rej_reason reason,
1112 void *ari,
1113 u8 ari_length,
1114 const void *private_data,
1115 u8 private_data_len)
1116{
1117 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid);
1118 rej_msg->remote_comm_id = cm_id_priv->id.remote_id;
1119
1120 switch(cm_id_priv->id.state) {
1121 case IB_CM_REQ_RCVD:
1122 rej_msg->local_comm_id = 0;
1123 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1124 break;
1125 case IB_CM_MRA_REQ_SENT:
1126 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1127 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REQ);
1128 break;
1129 case IB_CM_REP_RCVD:
1130 case IB_CM_MRA_REP_SENT:
1131 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1132 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_REP);
1133 break;
1134 default:
1135 rej_msg->local_comm_id = cm_id_priv->id.local_id;
1136 cm_rej_set_msg_rejected(rej_msg, CM_MSG_RESPONSE_OTHER);
1137 break;
1138 }
1139
1140 rej_msg->reason = reason;
1141 if (ari && ari_length) {
1142 cm_rej_set_reject_info_len(rej_msg, ari_length);
1143 memcpy(rej_msg->ari, ari, ari_length);
1144 }
1145
1146 if (private_data && private_data_len)
1147 memcpy(rej_msg->private_data, private_data, private_data_len);
1148}
1149
1150static void cm_dup_req_handler(struct cm_work *work,
1151 struct cm_id_private *cm_id_priv)
1152{
1153 struct ib_mad_send_buf *msg = NULL;
1154 struct ib_send_wr *bad_send_wr;
1155 unsigned long flags;
1156 int ret;
1157
1158 /* Quick state check to discard duplicate REQs. */
1159 if (cm_id_priv->id.state == IB_CM_REQ_RCVD)
1160 return;
1161
1162 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1163 if (ret)
1164 return;
1165
1166 spin_lock_irqsave(&cm_id_priv->lock, flags);
1167 switch (cm_id_priv->id.state) {
1168 case IB_CM_MRA_REQ_SENT:
1169 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1170 CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout,
1171 cm_id_priv->private_data,
1172 cm_id_priv->private_data_len);
1173 break;
1174 case IB_CM_TIMEWAIT:
1175 cm_format_rej((struct cm_rej_msg *) msg->mad, cm_id_priv,
1176 IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0);
1177 break;
1178 default:
1179 goto unlock;
1180 }
1181 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1182
1183 ret = ib_post_send_mad(cm_id_priv->av.port->mad_agent, &msg->send_wr,
1184 &bad_send_wr);
1185 if (ret)
1186 goto free;
1187 return;
1188
1189unlock: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1190free: cm_free_msg(msg);
1191}
1192
1193static struct cm_id_private * cm_match_req(struct cm_work *work,
1194 struct cm_id_private *cm_id_priv)
1195{
1196 struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv;
1197 struct cm_timewait_info *timewait_info;
1198 struct cm_req_msg *req_msg;
1199 unsigned long flags;
1200
1201 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1202
1203 /* Check for duplicate REQ and stale connections. */
1204 spin_lock_irqsave(&cm.lock, flags);
1205 timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info);
1206 if (!timewait_info)
1207 timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info);
1208
1209 if (timewait_info) {
1210 cur_cm_id_priv = cm_get_id(timewait_info->work.local_id,
1211 timewait_info->work.remote_id);
1212 spin_unlock_irqrestore(&cm.lock, flags);
1213 if (cur_cm_id_priv) {
1214 cm_dup_req_handler(work, cur_cm_id_priv);
1215 cm_deref_id(cur_cm_id_priv);
1216 } else
1217 cm_issue_rej(work->port, work->mad_recv_wc,
1218 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ,
1219 NULL, 0);
1220 goto error;
1221 }
1222
1223 /* Find matching listen request. */
1224 listen_cm_id_priv = cm_find_listen(req_msg->service_id);
1225 if (!listen_cm_id_priv) {
1226 spin_unlock_irqrestore(&cm.lock, flags);
1227 cm_issue_rej(work->port, work->mad_recv_wc,
1228 IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ,
1229 NULL, 0);
1230 goto error;
1231 }
1232 atomic_inc(&listen_cm_id_priv->refcount);
1233 atomic_inc(&cm_id_priv->refcount);
1234 cm_id_priv->id.state = IB_CM_REQ_RCVD;
1235 atomic_inc(&cm_id_priv->work_count);
1236 spin_unlock_irqrestore(&cm.lock, flags);
1237 return listen_cm_id_priv;
1238
1239error: cm_cleanup_timewait(cm_id_priv->timewait_info);
1240 return NULL;
1241}
1242
1243static int cm_req_handler(struct cm_work *work)
1244{
1245 struct ib_cm_id *cm_id;
1246 struct cm_id_private *cm_id_priv, *listen_cm_id_priv;
1247 struct cm_req_msg *req_msg;
1248 int ret;
1249
1250 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
1251
1252 cm_id = ib_create_cm_id(NULL, NULL);
1253 if (IS_ERR(cm_id))
1254 return PTR_ERR(cm_id);
1255
1256 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1257 cm_id_priv->id.remote_id = req_msg->local_comm_id;
1258 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
1259 &cm_id_priv->av);
1260 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv->
1261 id.local_id);
1262 if (IS_ERR(cm_id_priv->timewait_info)) {
1263 ret = PTR_ERR(cm_id_priv->timewait_info);
1264 goto error1;
1265 }
1266 cm_id_priv->timewait_info->work.remote_id = req_msg->local_comm_id;
1267 cm_id_priv->timewait_info->remote_ca_guid = req_msg->local_ca_guid;
1268 cm_id_priv->timewait_info->remote_qpn = cm_req_get_local_qpn(req_msg);
1269
1270 listen_cm_id_priv = cm_match_req(work, cm_id_priv);
1271 if (!listen_cm_id_priv) {
1272 ret = -EINVAL;
1273 goto error2;
1274 }
1275
1276 cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler;
1277 cm_id_priv->id.context = listen_cm_id_priv->id.context;
1278 cm_id_priv->id.service_id = req_msg->service_id;
1279 cm_id_priv->id.service_mask = ~0ULL;
1280
1281 cm_format_paths_from_req(req_msg, &work->path[0], &work->path[1]);
1282 ret = cm_init_av_by_path(&work->path[0], &cm_id_priv->av);
1283 if (ret)
1284 goto error3;
1285 if (req_msg->alt_local_lid) {
1286 ret = cm_init_av_by_path(&work->path[1], &cm_id_priv->alt_av);
1287 if (ret)
1288 goto error3;
1289 }
1290 cm_id_priv->tid = req_msg->hdr.tid;
1291 cm_id_priv->timeout_ms = cm_convert_to_ms(
1292 cm_req_get_local_resp_timeout(req_msg));
1293 cm_id_priv->max_cm_retries = cm_req_get_max_cm_retries(req_msg);
1294 cm_id_priv->remote_qpn = cm_req_get_local_qpn(req_msg);
1295 cm_id_priv->initiator_depth = cm_req_get_resp_res(req_msg);
1296 cm_id_priv->responder_resources = cm_req_get_init_depth(req_msg);
1297 cm_id_priv->path_mtu = cm_req_get_path_mtu(req_msg);
1298 cm_id_priv->sq_psn = cm_req_get_starting_psn(req_msg);
1299 cm_id_priv->local_ack_timeout =
1300 cm_req_get_primary_local_ack_timeout(req_msg);
1301 cm_id_priv->retry_count = cm_req_get_retry_count(req_msg);
1302 cm_id_priv->rnr_retry_count = cm_req_get_rnr_retry_count(req_msg);
1303
1304 cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id);
1305 cm_process_work(cm_id_priv, work);
1306 cm_deref_id(listen_cm_id_priv);
1307 return 0;
1308
1309error3: atomic_dec(&cm_id_priv->refcount);
1310 cm_deref_id(listen_cm_id_priv);
1311 cm_cleanup_timewait(cm_id_priv->timewait_info);
1312error2: kfree(cm_id_priv->timewait_info);
1313error1: ib_destroy_cm_id(&cm_id_priv->id);
1314 return ret;
1315}
1316
1317static void cm_format_rep(struct cm_rep_msg *rep_msg,
1318 struct cm_id_private *cm_id_priv,
1319 struct ib_cm_rep_param *param)
1320{
1321 cm_format_mad_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid);
1322 rep_msg->local_comm_id = cm_id_priv->id.local_id;
1323 rep_msg->remote_comm_id = cm_id_priv->id.remote_id;
1324 cm_rep_set_local_qpn(rep_msg, cpu_to_be32(param->qp_num));
1325 cm_rep_set_starting_psn(rep_msg, cpu_to_be32(param->starting_psn));
1326 rep_msg->resp_resources = param->responder_resources;
1327 rep_msg->initiator_depth = param->initiator_depth;
1328 cm_rep_set_target_ack_delay(rep_msg, param->target_ack_delay);
1329 cm_rep_set_failover(rep_msg, param->failover_accepted);
1330 cm_rep_set_flow_ctrl(rep_msg, param->flow_control);
1331 cm_rep_set_rnr_retry_count(rep_msg, param->rnr_retry_count);
1332 cm_rep_set_srq(rep_msg, param->srq);
1333 rep_msg->local_ca_guid = cm_id_priv->av.port->cm_dev->ca_guid;
1334
1335 if (param->private_data && param->private_data_len)
1336 memcpy(rep_msg->private_data, param->private_data,
1337 param->private_data_len);
1338}
1339
1340int ib_send_cm_rep(struct ib_cm_id *cm_id,
1341 struct ib_cm_rep_param *param)
1342{
1343 struct cm_id_private *cm_id_priv;
1344 struct ib_mad_send_buf *msg;
1345 struct cm_rep_msg *rep_msg;
1346 struct ib_send_wr *bad_send_wr;
1347 unsigned long flags;
1348 int ret;
1349
1350 if (param->private_data &&
1351 param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE)
1352 return -EINVAL;
1353
1354 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1355 spin_lock_irqsave(&cm_id_priv->lock, flags);
1356 if (cm_id->state != IB_CM_REQ_RCVD &&
1357 cm_id->state != IB_CM_MRA_REQ_SENT) {
1358 ret = -EINVAL;
1359 goto out;
1360 }
1361
1362 ret = cm_alloc_msg(cm_id_priv, &msg);
1363 if (ret)
1364 goto out;
1365
1366 rep_msg = (struct cm_rep_msg *) msg->mad;
1367 cm_format_rep(rep_msg, cm_id_priv, param);
1368 msg->send_wr.wr.ud.timeout_ms = cm_id_priv->timeout_ms;
1369 msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT;
1370
1371 ret = ib_post_send_mad(cm_id_priv->av.port->mad_agent,
1372 &msg->send_wr, &bad_send_wr);
1373 if (ret) {
1374 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1375 cm_free_msg(msg);
1376 return ret;
1377 }
1378
1379 cm_id->state = IB_CM_REP_SENT;
1380 cm_id_priv->msg = msg;
1381 cm_id_priv->initiator_depth = param->initiator_depth;
1382 cm_id_priv->responder_resources = param->responder_resources;
1383 cm_id_priv->rq_psn = cm_rep_get_starting_psn(rep_msg);
1384 cm_id_priv->local_qpn = cm_rep_get_local_qpn(rep_msg);
1385
1386out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1387 return ret;
1388}
1389EXPORT_SYMBOL(ib_send_cm_rep);
1390
1391static void cm_format_rtu(struct cm_rtu_msg *rtu_msg,
1392 struct cm_id_private *cm_id_priv,
1393 const void *private_data,
1394 u8 private_data_len)
1395{
1396 cm_format_mad_hdr(&rtu_msg->hdr, CM_RTU_ATTR_ID, cm_id_priv->tid);
1397 rtu_msg->local_comm_id = cm_id_priv->id.local_id;
1398 rtu_msg->remote_comm_id = cm_id_priv->id.remote_id;
1399
1400 if (private_data && private_data_len)
1401 memcpy(rtu_msg->private_data, private_data, private_data_len);
1402}
1403
1404int ib_send_cm_rtu(struct ib_cm_id *cm_id,
1405 const void *private_data,
1406 u8 private_data_len)
1407{
1408 struct cm_id_private *cm_id_priv;
1409 struct ib_mad_send_buf *msg;
1410 struct ib_send_wr *bad_send_wr;
1411 unsigned long flags;
1412 void *data;
1413 int ret;
1414
1415 if (private_data && private_data_len > IB_CM_RTU_PRIVATE_DATA_SIZE)
1416 return -EINVAL;
1417
1418 data = cm_copy_private_data(private_data, private_data_len);
1419 if (IS_ERR(data))
1420 return PTR_ERR(data);
1421
1422 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1423 spin_lock_irqsave(&cm_id_priv->lock, flags);
1424 if (cm_id->state != IB_CM_REP_RCVD &&
1425 cm_id->state != IB_CM_MRA_REP_SENT) {
1426 ret = -EINVAL;
1427 goto error;
1428 }
1429
1430 ret = cm_alloc_msg(cm_id_priv, &msg);
1431 if (ret)
1432 goto error;
1433
1434 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1435 private_data, private_data_len);
1436
1437 ret = ib_post_send_mad(cm_id_priv->av.port->mad_agent,
1438 &msg->send_wr, &bad_send_wr);
1439 if (ret) {
1440 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1441 cm_free_msg(msg);
1442 kfree(data);
1443 return ret;
1444 }
1445
1446 cm_id->state = IB_CM_ESTABLISHED;
1447 cm_set_private_data(cm_id_priv, data, private_data_len);
1448 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1449 return 0;
1450
1451error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1452 kfree(data);
1453 return ret;
1454}
1455EXPORT_SYMBOL(ib_send_cm_rtu);
1456
1457static void cm_format_rep_event(struct cm_work *work)
1458{
1459 struct cm_rep_msg *rep_msg;
1460 struct ib_cm_rep_event_param *param;
1461
1462 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1463 param = &work->cm_event.param.rep_rcvd;
1464 param->remote_ca_guid = rep_msg->local_ca_guid;
1465 param->remote_qkey = be32_to_cpu(rep_msg->local_qkey);
1466 param->remote_qpn = be32_to_cpu(cm_rep_get_local_qpn(rep_msg));
1467 param->starting_psn = be32_to_cpu(cm_rep_get_starting_psn(rep_msg));
1468 param->responder_resources = rep_msg->initiator_depth;
1469 param->initiator_depth = rep_msg->resp_resources;
1470 param->target_ack_delay = cm_rep_get_target_ack_delay(rep_msg);
1471 param->failover_accepted = cm_rep_get_failover(rep_msg);
1472 param->flow_control = cm_rep_get_flow_ctrl(rep_msg);
1473 param->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1474 param->srq = cm_rep_get_srq(rep_msg);
1475 work->cm_event.private_data = &rep_msg->private_data;
1476}
1477
1478static void cm_dup_rep_handler(struct cm_work *work)
1479{
1480 struct cm_id_private *cm_id_priv;
1481 struct cm_rep_msg *rep_msg;
1482 struct ib_mad_send_buf *msg = NULL;
1483 struct ib_send_wr *bad_send_wr;
1484 unsigned long flags;
1485 int ret;
1486
1487 rep_msg = (struct cm_rep_msg *) work->mad_recv_wc->recv_buf.mad;
1488 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id,
1489 rep_msg->local_comm_id);
1490 if (!cm_id_priv)
1491 return;
1492
1493 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg);
1494 if (ret)
1495 goto deref;
1496
1497 spin_lock_irqsave(&cm_id_priv->lock, flags);
1498 if (cm_id_priv->id.state == IB_CM_ESTABLISHED)
1499 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv,
1500 cm_id_priv->private_data,
1501 cm_id_priv->private_data_len);
1502 else if (cm_id_priv->id.state == IB_CM_MRA_REP_SENT)
1503 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
1504 CM_MSG_RESPONSE_REP, cm_id_priv->service_timeout,
1505 cm_id_priv->private_data,
1506 cm_id_priv->private_data_len);
1507 else
1508 goto unlock;
1509 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1510
1511 ret = ib_post_send_mad(cm_id_priv->av.port->mad_agent, &msg->send_wr,
1512 &bad_send_wr);
1513 if (ret)
1514 goto free;
1515 goto deref;
1516
1517unlock: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1518free: cm_free_msg(msg);
1519deref: cm_deref_id(cm_id_priv);
1520}
1521
1522static int cm_rep_handler(struct cm_work *work)
1523{
1524 struct cm_id_private *cm_id_priv;
1525 struct cm_rep_msg *rep_msg;
1526 unsigned long flags;
1527 int ret;
1528
1529 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad;
1530 cm_id_priv = cm_acquire_id(rep_msg->remote_comm_id, 0);
1531 if (!cm_id_priv) {
1532 cm_dup_rep_handler(work);
1533 return -EINVAL;
1534 }
1535
1536 cm_id_priv->timewait_info->work.remote_id = rep_msg->local_comm_id;
1537 cm_id_priv->timewait_info->remote_ca_guid = rep_msg->local_ca_guid;
1538 cm_id_priv->timewait_info->remote_qpn = cm_rep_get_local_qpn(rep_msg);
1539
1540 spin_lock_irqsave(&cm.lock, flags);
1541 /* Check for duplicate REP. */
1542 if (cm_insert_remote_id(cm_id_priv->timewait_info)) {
1543 spin_unlock_irqrestore(&cm.lock, flags);
1544 ret = -EINVAL;
1545 goto error;
1546 }
1547 /* Check for a stale connection. */
1548 if (cm_insert_remote_qpn(cm_id_priv->timewait_info)) {
1549 spin_unlock_irqrestore(&cm.lock, flags);
1550 cm_issue_rej(work->port, work->mad_recv_wc,
1551 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP,
1552 NULL, 0);
1553 ret = -EINVAL;
1554 goto error;
1555 }
1556 spin_unlock_irqrestore(&cm.lock, flags);
1557
1558 cm_format_rep_event(work);
1559
1560 spin_lock_irqsave(&cm_id_priv->lock, flags);
1561 switch (cm_id_priv->id.state) {
1562 case IB_CM_REQ_SENT:
1563 case IB_CM_MRA_REQ_RCVD:
1564 break;
1565 default:
1566 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1567 ret = -EINVAL;
1568 goto error;
1569 }
1570 cm_id_priv->id.state = IB_CM_REP_RCVD;
1571 cm_id_priv->id.remote_id = rep_msg->local_comm_id;
1572 cm_id_priv->remote_qpn = cm_rep_get_local_qpn(rep_msg);
1573 cm_id_priv->initiator_depth = rep_msg->resp_resources;
1574 cm_id_priv->responder_resources = rep_msg->initiator_depth;
1575 cm_id_priv->sq_psn = cm_rep_get_starting_psn(rep_msg);
1576 cm_id_priv->rnr_retry_count = cm_rep_get_rnr_retry_count(rep_msg);
1577
1578 /* todo: handle peer_to_peer */
1579
1580 ib_cancel_mad(cm_id_priv->av.port->mad_agent,
1581 (unsigned long) cm_id_priv->msg);
1582 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1583 if (!ret)
1584 list_add_tail(&work->list, &cm_id_priv->work_list);
1585 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1586
1587 if (ret)
1588 cm_process_work(cm_id_priv, work);
1589 else
1590 cm_deref_id(cm_id_priv);
1591 return 0;
1592
1593error: cm_cleanup_timewait(cm_id_priv->timewait_info);
1594 cm_deref_id(cm_id_priv);
1595 return ret;
1596}
1597
1598static int cm_establish_handler(struct cm_work *work)
1599{
1600 struct cm_id_private *cm_id_priv;
1601 unsigned long flags;
1602 int ret;
1603
1604 /* See comment in ib_cm_establish about lookup. */
1605 cm_id_priv = cm_acquire_id(work->local_id, work->remote_id);
1606 if (!cm_id_priv)
1607 return -EINVAL;
1608
1609 spin_lock_irqsave(&cm_id_priv->lock, flags);
1610 if (cm_id_priv->id.state != IB_CM_ESTABLISHED) {
1611 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1612 goto out;
1613 }
1614
1615 ib_cancel_mad(cm_id_priv->av.port->mad_agent,
1616 (unsigned long) cm_id_priv->msg);
1617 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1618 if (!ret)
1619 list_add_tail(&work->list, &cm_id_priv->work_list);
1620 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1621
1622 if (ret)
1623 cm_process_work(cm_id_priv, work);
1624 else
1625 cm_deref_id(cm_id_priv);
1626 return 0;
1627out:
1628 cm_deref_id(cm_id_priv);
1629 return -EINVAL;
1630}
1631
1632static int cm_rtu_handler(struct cm_work *work)
1633{
1634 struct cm_id_private *cm_id_priv;
1635 struct cm_rtu_msg *rtu_msg;
1636 unsigned long flags;
1637 int ret;
1638
1639 rtu_msg = (struct cm_rtu_msg *)work->mad_recv_wc->recv_buf.mad;
1640 cm_id_priv = cm_acquire_id(rtu_msg->remote_comm_id,
1641 rtu_msg->local_comm_id);
1642 if (!cm_id_priv)
1643 return -EINVAL;
1644
1645 work->cm_event.private_data = &rtu_msg->private_data;
1646
1647 spin_lock_irqsave(&cm_id_priv->lock, flags);
1648 if (cm_id_priv->id.state != IB_CM_REP_SENT &&
1649 cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) {
1650 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1651 goto out;
1652 }
1653 cm_id_priv->id.state = IB_CM_ESTABLISHED;
1654
1655 ib_cancel_mad(cm_id_priv->av.port->mad_agent,
1656 (unsigned long) cm_id_priv->msg);
1657 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1658 if (!ret)
1659 list_add_tail(&work->list, &cm_id_priv->work_list);
1660 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1661
1662 if (ret)
1663 cm_process_work(cm_id_priv, work);
1664 else
1665 cm_deref_id(cm_id_priv);
1666 return 0;
1667out:
1668 cm_deref_id(cm_id_priv);
1669 return -EINVAL;
1670}
1671
1672static void cm_format_dreq(struct cm_dreq_msg *dreq_msg,
1673 struct cm_id_private *cm_id_priv,
1674 const void *private_data,
1675 u8 private_data_len)
1676{
1677 cm_format_mad_hdr(&dreq_msg->hdr, CM_DREQ_ATTR_ID,
1678 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_DREQ));
1679 dreq_msg->local_comm_id = cm_id_priv->id.local_id;
1680 dreq_msg->remote_comm_id = cm_id_priv->id.remote_id;
1681 cm_dreq_set_remote_qpn(dreq_msg, cm_id_priv->remote_qpn);
1682
1683 if (private_data && private_data_len)
1684 memcpy(dreq_msg->private_data, private_data, private_data_len);
1685}
1686
1687int ib_send_cm_dreq(struct ib_cm_id *cm_id,
1688 const void *private_data,
1689 u8 private_data_len)
1690{
1691 struct cm_id_private *cm_id_priv;
1692 struct ib_mad_send_buf *msg;
1693 struct ib_send_wr *bad_send_wr;
1694 unsigned long flags;
1695 int ret;
1696
1697 if (private_data && private_data_len > IB_CM_DREQ_PRIVATE_DATA_SIZE)
1698 return -EINVAL;
1699
1700 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1701 spin_lock_irqsave(&cm_id_priv->lock, flags);
1702 if (cm_id->state != IB_CM_ESTABLISHED) {
1703 ret = -EINVAL;
1704 goto out;
1705 }
1706
1707 ret = cm_alloc_msg(cm_id_priv, &msg);
1708 if (ret) {
1709 cm_enter_timewait(cm_id_priv);
1710 goto out;
1711 }
1712
1713 cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv,
1714 private_data, private_data_len);
1715 msg->send_wr.wr.ud.timeout_ms = cm_id_priv->timeout_ms;
1716 msg->context[1] = (void *) (unsigned long) IB_CM_DREQ_SENT;
1717
1718 ret = ib_post_send_mad(cm_id_priv->av.port->mad_agent,
1719 &msg->send_wr, &bad_send_wr);
1720 if (ret) {
1721 cm_enter_timewait(cm_id_priv);
1722 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1723 cm_free_msg(msg);
1724 return ret;
1725 }
1726
1727 cm_id->state = IB_CM_DREQ_SENT;
1728 cm_id_priv->msg = msg;
1729out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1730 return ret;
1731}
1732EXPORT_SYMBOL(ib_send_cm_dreq);
1733
1734static void cm_format_drep(struct cm_drep_msg *drep_msg,
1735 struct cm_id_private *cm_id_priv,
1736 const void *private_data,
1737 u8 private_data_len)
1738{
1739 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, cm_id_priv->tid);
1740 drep_msg->local_comm_id = cm_id_priv->id.local_id;
1741 drep_msg->remote_comm_id = cm_id_priv->id.remote_id;
1742
1743 if (private_data && private_data_len)
1744 memcpy(drep_msg->private_data, private_data, private_data_len);
1745}
1746
1747int ib_send_cm_drep(struct ib_cm_id *cm_id,
1748 const void *private_data,
1749 u8 private_data_len)
1750{
1751 struct cm_id_private *cm_id_priv;
1752 struct ib_mad_send_buf *msg;
1753 struct ib_send_wr *bad_send_wr;
1754 unsigned long flags;
1755 void *data;
1756 int ret;
1757
1758 if (private_data && private_data_len > IB_CM_DREP_PRIVATE_DATA_SIZE)
1759 return -EINVAL;
1760
1761 data = cm_copy_private_data(private_data, private_data_len);
1762 if (IS_ERR(data))
1763 return PTR_ERR(data);
1764
1765 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1766 spin_lock_irqsave(&cm_id_priv->lock, flags);
1767 if (cm_id->state != IB_CM_DREQ_RCVD) {
1768 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1769 kfree(data);
1770 return -EINVAL;
1771 }
1772
1773 cm_set_private_data(cm_id_priv, data, private_data_len);
1774 cm_enter_timewait(cm_id_priv);
1775
1776 ret = cm_alloc_msg(cm_id_priv, &msg);
1777 if (ret)
1778 goto out;
1779
1780 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
1781 private_data, private_data_len);
1782
1783 ret = ib_post_send_mad(cm_id_priv->av.port->mad_agent, &msg->send_wr,
1784 &bad_send_wr);
1785 if (ret) {
1786 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1787 cm_free_msg(msg);
1788 return ret;
1789 }
1790
1791out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1792 return ret;
1793}
1794EXPORT_SYMBOL(ib_send_cm_drep);
1795
1796static int cm_dreq_handler(struct cm_work *work)
1797{
1798 struct cm_id_private *cm_id_priv;
1799 struct cm_dreq_msg *dreq_msg;
1800 struct ib_mad_send_buf *msg = NULL;
1801 struct ib_send_wr *bad_send_wr;
1802 unsigned long flags;
1803 int ret;
1804
1805 dreq_msg = (struct cm_dreq_msg *)work->mad_recv_wc->recv_buf.mad;
1806 cm_id_priv = cm_acquire_id(dreq_msg->remote_comm_id,
1807 dreq_msg->local_comm_id);
1808 if (!cm_id_priv)
1809 return -EINVAL;
1810
1811 work->cm_event.private_data = &dreq_msg->private_data;
1812
1813 spin_lock_irqsave(&cm_id_priv->lock, flags);
1814 if (cm_id_priv->local_qpn != cm_dreq_get_remote_qpn(dreq_msg))
1815 goto unlock;
1816
1817 switch (cm_id_priv->id.state) {
1818 case IB_CM_REP_SENT:
1819 case IB_CM_DREQ_SENT:
1820 ib_cancel_mad(cm_id_priv->av.port->mad_agent,
1821 (unsigned long) cm_id_priv->msg);
1822 break;
1823 case IB_CM_ESTABLISHED:
1824 case IB_CM_MRA_REP_RCVD:
1825 break;
1826 case IB_CM_TIMEWAIT:
1827 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
1828 goto unlock;
1829
1830 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv,
1831 cm_id_priv->private_data,
1832 cm_id_priv->private_data_len);
1833 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1834
1835 if (ib_post_send_mad(cm_id_priv->av.port->mad_agent,
1836 &msg->send_wr, &bad_send_wr))
1837 cm_free_msg(msg);
1838 goto deref;
1839 default:
1840 goto unlock;
1841 }
1842 cm_id_priv->id.state = IB_CM_DREQ_RCVD;
1843 cm_id_priv->tid = dreq_msg->hdr.tid;
1844 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1845 if (!ret)
1846 list_add_tail(&work->list, &cm_id_priv->work_list);
1847 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1848
1849 if (ret)
1850 cm_process_work(cm_id_priv, work);
1851 else
1852 cm_deref_id(cm_id_priv);
1853 return 0;
1854
1855unlock: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1856deref: cm_deref_id(cm_id_priv);
1857 return -EINVAL;
1858}
1859
1860static int cm_drep_handler(struct cm_work *work)
1861{
1862 struct cm_id_private *cm_id_priv;
1863 struct cm_drep_msg *drep_msg;
1864 unsigned long flags;
1865 int ret;
1866
1867 drep_msg = (struct cm_drep_msg *)work->mad_recv_wc->recv_buf.mad;
1868 cm_id_priv = cm_acquire_id(drep_msg->remote_comm_id,
1869 drep_msg->local_comm_id);
1870 if (!cm_id_priv)
1871 return -EINVAL;
1872
1873 work->cm_event.private_data = &drep_msg->private_data;
1874
1875 spin_lock_irqsave(&cm_id_priv->lock, flags);
1876 if (cm_id_priv->id.state != IB_CM_DREQ_SENT &&
1877 cm_id_priv->id.state != IB_CM_DREQ_RCVD) {
1878 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1879 goto out;
1880 }
1881 cm_enter_timewait(cm_id_priv);
1882
1883 ib_cancel_mad(cm_id_priv->av.port->mad_agent,
1884 (unsigned long) cm_id_priv->msg);
1885 ret = atomic_inc_and_test(&cm_id_priv->work_count);
1886 if (!ret)
1887 list_add_tail(&work->list, &cm_id_priv->work_list);
1888 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1889
1890 if (ret)
1891 cm_process_work(cm_id_priv, work);
1892 else
1893 cm_deref_id(cm_id_priv);
1894 return 0;
1895out:
1896 cm_deref_id(cm_id_priv);
1897 return -EINVAL;
1898}
1899
1900int ib_send_cm_rej(struct ib_cm_id *cm_id,
1901 enum ib_cm_rej_reason reason,
1902 void *ari,
1903 u8 ari_length,
1904 const void *private_data,
1905 u8 private_data_len)
1906{
1907 struct cm_id_private *cm_id_priv;
1908 struct ib_mad_send_buf *msg;
1909 struct ib_send_wr *bad_send_wr;
1910 unsigned long flags;
1911 int ret;
1912
1913 if ((private_data && private_data_len > IB_CM_REJ_PRIVATE_DATA_SIZE) ||
1914 (ari && ari_length > IB_CM_REJ_ARI_LENGTH))
1915 return -EINVAL;
1916
1917 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
1918
1919 spin_lock_irqsave(&cm_id_priv->lock, flags);
1920 switch (cm_id->state) {
1921 case IB_CM_REQ_SENT:
1922 case IB_CM_MRA_REQ_RCVD:
1923 case IB_CM_REQ_RCVD:
1924 case IB_CM_MRA_REQ_SENT:
1925 case IB_CM_REP_RCVD:
1926 case IB_CM_MRA_REP_SENT:
1927 ret = cm_alloc_msg(cm_id_priv, &msg);
1928 if (!ret)
1929 cm_format_rej((struct cm_rej_msg *) msg->mad,
1930 cm_id_priv, reason, ari, ari_length,
1931 private_data, private_data_len);
1932
1933 cm_reset_to_idle(cm_id_priv);
1934 break;
1935 case IB_CM_REP_SENT:
1936 case IB_CM_MRA_REP_RCVD:
1937 ret = cm_alloc_msg(cm_id_priv, &msg);
1938 if (!ret)
1939 cm_format_rej((struct cm_rej_msg *) msg->mad,
1940 cm_id_priv, reason, ari, ari_length,
1941 private_data, private_data_len);
1942
1943 cm_enter_timewait(cm_id_priv);
1944 break;
1945 default:
1946 ret = -EINVAL;
1947 goto out;
1948 }
1949
1950 if (ret)
1951 goto out;
1952
1953 ret = ib_post_send_mad(cm_id_priv->av.port->mad_agent,
1954 &msg->send_wr, &bad_send_wr);
1955 if (ret)
1956 cm_free_msg(msg);
1957
1958out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1959 return ret;
1960}
1961EXPORT_SYMBOL(ib_send_cm_rej);
1962
1963static void cm_format_rej_event(struct cm_work *work)
1964{
1965 struct cm_rej_msg *rej_msg;
1966 struct ib_cm_rej_event_param *param;
1967
1968 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
1969 param = &work->cm_event.param.rej_rcvd;
1970 param->ari = rej_msg->ari;
1971 param->ari_length = cm_rej_get_reject_info_len(rej_msg);
1972 param->reason = rej_msg->reason;
1973 work->cm_event.private_data = &rej_msg->private_data;
1974}
1975
1976static struct cm_id_private * cm_acquire_rejected_id(struct cm_rej_msg *rej_msg)
1977{
1978 struct cm_timewait_info *timewait_info;
1979 struct cm_id_private *cm_id_priv;
1980 unsigned long flags;
1981 u32 remote_id;
1982
1983 remote_id = rej_msg->local_comm_id;
1984
1985 if (rej_msg->reason == IB_CM_REJ_TIMEOUT) {
1986 spin_lock_irqsave(&cm.lock, flags);
1987 timewait_info = cm_find_remote_id( *((u64 *) rej_msg->ari),
1988 remote_id);
1989 if (!timewait_info) {
1990 spin_unlock_irqrestore(&cm.lock, flags);
1991 return NULL;
1992 }
1993 cm_id_priv = idr_find(&cm.local_id_table,
1994 (int) timewait_info->work.local_id);
1995 if (cm_id_priv) {
1996 if (cm_id_priv->id.remote_id == remote_id)
1997 atomic_inc(&cm_id_priv->refcount);
1998 else
1999 cm_id_priv = NULL;
2000 }
2001 spin_unlock_irqrestore(&cm.lock, flags);
2002 } else if (cm_rej_get_msg_rejected(rej_msg) == CM_MSG_RESPONSE_REQ)
2003 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, 0);
2004 else
2005 cm_id_priv = cm_acquire_id(rej_msg->remote_comm_id, remote_id);
2006
2007 return cm_id_priv;
2008}
2009
2010static int cm_rej_handler(struct cm_work *work)
2011{
2012 struct cm_id_private *cm_id_priv;
2013 struct cm_rej_msg *rej_msg;
2014 unsigned long flags;
2015 int ret;
2016
2017 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad;
2018 cm_id_priv = cm_acquire_rejected_id(rej_msg);
2019 if (!cm_id_priv)
2020 return -EINVAL;
2021
2022 cm_format_rej_event(work);
2023
2024 spin_lock_irqsave(&cm_id_priv->lock, flags);
2025 switch (cm_id_priv->id.state) {
2026 case IB_CM_REQ_SENT:
2027 case IB_CM_MRA_REQ_RCVD:
2028 case IB_CM_REP_SENT:
2029 case IB_CM_MRA_REP_RCVD:
2030 ib_cancel_mad(cm_id_priv->av.port->mad_agent,
2031 (unsigned long) cm_id_priv->msg);
2032 /* fall through */
2033 case IB_CM_REQ_RCVD:
2034 case IB_CM_MRA_REQ_SENT:
2035 if (rej_msg->reason == IB_CM_REJ_STALE_CONN)
2036 cm_enter_timewait(cm_id_priv);
2037 else
2038 cm_reset_to_idle(cm_id_priv);
2039 break;
2040 case IB_CM_DREQ_SENT:
2041 ib_cancel_mad(cm_id_priv->av.port->mad_agent,
2042 (unsigned long) cm_id_priv->msg);
2043 /* fall through */
2044 case IB_CM_REP_RCVD:
2045 case IB_CM_MRA_REP_SENT:
2046 case IB_CM_ESTABLISHED:
2047 cm_enter_timewait(cm_id_priv);
2048 break;
2049 default:
2050 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2051 ret = -EINVAL;
2052 goto out;
2053 }
2054
2055 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2056 if (!ret)
2057 list_add_tail(&work->list, &cm_id_priv->work_list);
2058 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2059
2060 if (ret)
2061 cm_process_work(cm_id_priv, work);
2062 else
2063 cm_deref_id(cm_id_priv);
2064 return 0;
2065out:
2066 cm_deref_id(cm_id_priv);
2067 return -EINVAL;
2068}
2069
2070int ib_send_cm_mra(struct ib_cm_id *cm_id,
2071 u8 service_timeout,
2072 const void *private_data,
2073 u8 private_data_len)
2074{
2075 struct cm_id_private *cm_id_priv;
2076 struct ib_mad_send_buf *msg;
2077 struct ib_send_wr *bad_send_wr;
2078 void *data;
2079 unsigned long flags;
2080 int ret;
2081
2082 if (private_data && private_data_len > IB_CM_MRA_PRIVATE_DATA_SIZE)
2083 return -EINVAL;
2084
2085 data = cm_copy_private_data(private_data, private_data_len);
2086 if (IS_ERR(data))
2087 return PTR_ERR(data);
2088
2089 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2090
2091 spin_lock_irqsave(&cm_id_priv->lock, flags);
2092 switch(cm_id_priv->id.state) {
2093 case IB_CM_REQ_RCVD:
2094 ret = cm_alloc_msg(cm_id_priv, &msg);
2095 if (ret)
2096 goto error1;
2097
2098 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2099 CM_MSG_RESPONSE_REQ, service_timeout,
2100 private_data, private_data_len);
2101 ret = ib_post_send_mad(cm_id_priv->av.port->mad_agent,
2102 &msg->send_wr, &bad_send_wr);
2103 if (ret)
2104 goto error2;
2105 cm_id->state = IB_CM_MRA_REQ_SENT;
2106 break;
2107 case IB_CM_REP_RCVD:
2108 ret = cm_alloc_msg(cm_id_priv, &msg);
2109 if (ret)
2110 goto error1;
2111
2112 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2113 CM_MSG_RESPONSE_REP, service_timeout,
2114 private_data, private_data_len);
2115 ret = ib_post_send_mad(cm_id_priv->av.port->mad_agent,
2116 &msg->send_wr, &bad_send_wr);
2117 if (ret)
2118 goto error2;
2119 cm_id->state = IB_CM_MRA_REP_SENT;
2120 break;
2121 case IB_CM_ESTABLISHED:
2122 ret = cm_alloc_msg(cm_id_priv, &msg);
2123 if (ret)
2124 goto error1;
2125
2126 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2127 CM_MSG_RESPONSE_OTHER, service_timeout,
2128 private_data, private_data_len);
2129 ret = ib_post_send_mad(cm_id_priv->av.port->mad_agent,
2130 &msg->send_wr, &bad_send_wr);
2131 if (ret)
2132 goto error2;
2133 cm_id->lap_state = IB_CM_MRA_LAP_SENT;
2134 break;
2135 default:
2136 ret = -EINVAL;
2137 goto error1;
2138 }
2139 cm_id_priv->service_timeout = service_timeout;
2140 cm_set_private_data(cm_id_priv, data, private_data_len);
2141 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2142 return 0;
2143
2144error1: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2145 kfree(data);
2146 return ret;
2147
2148error2: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2149 kfree(data);
2150 cm_free_msg(msg);
2151 return ret;
2152}
2153EXPORT_SYMBOL(ib_send_cm_mra);
2154
2155static struct cm_id_private * cm_acquire_mraed_id(struct cm_mra_msg *mra_msg)
2156{
2157 switch (cm_mra_get_msg_mraed(mra_msg)) {
2158 case CM_MSG_RESPONSE_REQ:
2159 return cm_acquire_id(mra_msg->remote_comm_id, 0);
2160 case CM_MSG_RESPONSE_REP:
2161 case CM_MSG_RESPONSE_OTHER:
2162 return cm_acquire_id(mra_msg->remote_comm_id,
2163 mra_msg->local_comm_id);
2164 default:
2165 return NULL;
2166 }
2167}
2168
2169static int cm_mra_handler(struct cm_work *work)
2170{
2171 struct cm_id_private *cm_id_priv;
2172 struct cm_mra_msg *mra_msg;
2173 unsigned long flags;
2174 int timeout, ret;
2175
2176 mra_msg = (struct cm_mra_msg *)work->mad_recv_wc->recv_buf.mad;
2177 cm_id_priv = cm_acquire_mraed_id(mra_msg);
2178 if (!cm_id_priv)
2179 return -EINVAL;
2180
2181 work->cm_event.private_data = &mra_msg->private_data;
2182 work->cm_event.param.mra_rcvd.service_timeout =
2183 cm_mra_get_service_timeout(mra_msg);
2184 timeout = cm_convert_to_ms(cm_mra_get_service_timeout(mra_msg)) +
2185 cm_convert_to_ms(cm_id_priv->av.packet_life_time);
2186
2187 spin_lock_irqsave(&cm_id_priv->lock, flags);
2188 switch (cm_id_priv->id.state) {
2189 case IB_CM_REQ_SENT:
2190 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REQ ||
2191 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2192 (unsigned long) cm_id_priv->msg, timeout))
2193 goto out;
2194 cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD;
2195 break;
2196 case IB_CM_REP_SENT:
2197 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_REP ||
2198 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2199 (unsigned long) cm_id_priv->msg, timeout))
2200 goto out;
2201 cm_id_priv->id.state = IB_CM_MRA_REP_RCVD;
2202 break;
2203 case IB_CM_ESTABLISHED:
2204 if (cm_mra_get_msg_mraed(mra_msg) != CM_MSG_RESPONSE_OTHER ||
2205 cm_id_priv->id.lap_state != IB_CM_LAP_SENT ||
2206 ib_modify_mad(cm_id_priv->av.port->mad_agent,
2207 (unsigned long) cm_id_priv->msg, timeout))
2208 goto out;
2209 cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD;
2210 break;
2211 default:
2212 goto out;
2213 }
2214
2215 cm_id_priv->msg->context[1] = (void *) (unsigned long)
2216 cm_id_priv->id.state;
2217 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2218 if (!ret)
2219 list_add_tail(&work->list, &cm_id_priv->work_list);
2220 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2221
2222 if (ret)
2223 cm_process_work(cm_id_priv, work);
2224 else
2225 cm_deref_id(cm_id_priv);
2226 return 0;
2227out:
2228 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2229 cm_deref_id(cm_id_priv);
2230 return -EINVAL;
2231}
2232
2233static void cm_format_lap(struct cm_lap_msg *lap_msg,
2234 struct cm_id_private *cm_id_priv,
2235 struct ib_sa_path_rec *alternate_path,
2236 const void *private_data,
2237 u8 private_data_len)
2238{
2239 cm_format_mad_hdr(&lap_msg->hdr, CM_LAP_ATTR_ID,
2240 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_LAP));
2241 lap_msg->local_comm_id = cm_id_priv->id.local_id;
2242 lap_msg->remote_comm_id = cm_id_priv->id.remote_id;
2243 cm_lap_set_remote_qpn(lap_msg, cm_id_priv->remote_qpn);
2244 /* todo: need remote CM response timeout */
2245 cm_lap_set_remote_resp_timeout(lap_msg, 0x1F);
2246 lap_msg->alt_local_lid = alternate_path->slid;
2247 lap_msg->alt_remote_lid = alternate_path->dlid;
2248 lap_msg->alt_local_gid = alternate_path->sgid;
2249 lap_msg->alt_remote_gid = alternate_path->dgid;
2250 cm_lap_set_flow_label(lap_msg, alternate_path->flow_label);
2251 cm_lap_set_traffic_class(lap_msg, alternate_path->traffic_class);
2252 lap_msg->alt_hop_limit = alternate_path->hop_limit;
2253 cm_lap_set_packet_rate(lap_msg, alternate_path->rate);
2254 cm_lap_set_sl(lap_msg, alternate_path->sl);
2255 cm_lap_set_subnet_local(lap_msg, 1); /* local only... */
2256 cm_lap_set_local_ack_timeout(lap_msg,
2257 min(31, alternate_path->packet_life_time + 1));
2258
2259 if (private_data && private_data_len)
2260 memcpy(lap_msg->private_data, private_data, private_data_len);
2261}
2262
2263int ib_send_cm_lap(struct ib_cm_id *cm_id,
2264 struct ib_sa_path_rec *alternate_path,
2265 const void *private_data,
2266 u8 private_data_len)
2267{
2268 struct cm_id_private *cm_id_priv;
2269 struct ib_mad_send_buf *msg;
2270 struct ib_send_wr *bad_send_wr;
2271 unsigned long flags;
2272 int ret;
2273
2274 if (private_data && private_data_len > IB_CM_LAP_PRIVATE_DATA_SIZE)
2275 return -EINVAL;
2276
2277 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2278 spin_lock_irqsave(&cm_id_priv->lock, flags);
2279 if (cm_id->state != IB_CM_ESTABLISHED ||
2280 cm_id->lap_state != IB_CM_LAP_IDLE) {
2281 ret = -EINVAL;
2282 goto out;
2283 }
2284
2285 ret = cm_alloc_msg(cm_id_priv, &msg);
2286 if (ret)
2287 goto out;
2288
2289 cm_format_lap((struct cm_lap_msg *) msg->mad, cm_id_priv,
2290 alternate_path, private_data, private_data_len);
2291 msg->send_wr.wr.ud.timeout_ms = cm_id_priv->timeout_ms;
2292 msg->context[1] = (void *) (unsigned long) IB_CM_ESTABLISHED;
2293
2294 ret = ib_post_send_mad(cm_id_priv->av.port->mad_agent,
2295 &msg->send_wr, &bad_send_wr);
2296 if (ret) {
2297 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2298 cm_free_msg(msg);
2299 return ret;
2300 }
2301
2302 cm_id->lap_state = IB_CM_LAP_SENT;
2303 cm_id_priv->msg = msg;
2304
2305out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2306 return ret;
2307}
2308EXPORT_SYMBOL(ib_send_cm_lap);
2309
2310static void cm_format_path_from_lap(struct ib_sa_path_rec *path,
2311 struct cm_lap_msg *lap_msg)
2312{
2313 memset(path, 0, sizeof *path);
2314 path->dgid = lap_msg->alt_local_gid;
2315 path->sgid = lap_msg->alt_remote_gid;
2316 path->dlid = lap_msg->alt_local_lid;
2317 path->slid = lap_msg->alt_remote_lid;
2318 path->flow_label = cm_lap_get_flow_label(lap_msg);
2319 path->hop_limit = lap_msg->alt_hop_limit;
2320 path->traffic_class = cm_lap_get_traffic_class(lap_msg);
2321 path->reversible = 1;
2322 /* pkey is same as in REQ */
2323 path->sl = cm_lap_get_sl(lap_msg);
2324 path->mtu_selector = IB_SA_EQ;
2325 /* mtu is same as in REQ */
2326 path->rate_selector = IB_SA_EQ;
2327 path->rate = cm_lap_get_packet_rate(lap_msg);
2328 path->packet_life_time_selector = IB_SA_EQ;
2329 path->packet_life_time = cm_lap_get_local_ack_timeout(lap_msg);
2330 path->packet_life_time -= (path->packet_life_time > 0);
2331}
2332
2333static int cm_lap_handler(struct cm_work *work)
2334{
2335 struct cm_id_private *cm_id_priv;
2336 struct cm_lap_msg *lap_msg;
2337 struct ib_cm_lap_event_param *param;
2338 struct ib_mad_send_buf *msg = NULL;
2339 struct ib_send_wr *bad_send_wr;
2340 unsigned long flags;
2341 int ret;
2342
2343 /* todo: verify LAP request and send reject APR if invalid. */
2344 lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad;
2345 cm_id_priv = cm_acquire_id(lap_msg->remote_comm_id,
2346 lap_msg->local_comm_id);
2347 if (!cm_id_priv)
2348 return -EINVAL;
2349
2350 param = &work->cm_event.param.lap_rcvd;
2351 param->alternate_path = &work->path[0];
2352 cm_format_path_from_lap(param->alternate_path, lap_msg);
2353 work->cm_event.private_data = &lap_msg->private_data;
2354
2355 spin_lock_irqsave(&cm_id_priv->lock, flags);
2356 if (cm_id_priv->id.state != IB_CM_ESTABLISHED)
2357 goto unlock;
2358
2359 switch (cm_id_priv->id.lap_state) {
2360 case IB_CM_LAP_IDLE:
2361 break;
2362 case IB_CM_MRA_LAP_SENT:
2363 if (cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg))
2364 goto unlock;
2365
2366 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv,
2367 CM_MSG_RESPONSE_OTHER,
2368 cm_id_priv->service_timeout,
2369 cm_id_priv->private_data,
2370 cm_id_priv->private_data_len);
2371 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2372
2373 if (ib_post_send_mad(cm_id_priv->av.port->mad_agent,
2374 &msg->send_wr, &bad_send_wr))
2375 cm_free_msg(msg);
2376 goto deref;
2377 default:
2378 goto unlock;
2379 }
2380
2381 cm_id_priv->id.lap_state = IB_CM_LAP_RCVD;
2382 cm_id_priv->tid = lap_msg->hdr.tid;
2383 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2384 if (!ret)
2385 list_add_tail(&work->list, &cm_id_priv->work_list);
2386 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2387
2388 if (ret)
2389 cm_process_work(cm_id_priv, work);
2390 else
2391 cm_deref_id(cm_id_priv);
2392 return 0;
2393
2394unlock: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2395deref: cm_deref_id(cm_id_priv);
2396 return -EINVAL;
2397}
2398
2399static void cm_format_apr(struct cm_apr_msg *apr_msg,
2400 struct cm_id_private *cm_id_priv,
2401 enum ib_cm_apr_status status,
2402 void *info,
2403 u8 info_length,
2404 const void *private_data,
2405 u8 private_data_len)
2406{
2407 cm_format_mad_hdr(&apr_msg->hdr, CM_APR_ATTR_ID, cm_id_priv->tid);
2408 apr_msg->local_comm_id = cm_id_priv->id.local_id;
2409 apr_msg->remote_comm_id = cm_id_priv->id.remote_id;
2410 apr_msg->ap_status = (u8) status;
2411
2412 if (info && info_length) {
2413 apr_msg->info_length = info_length;
2414 memcpy(apr_msg->info, info, info_length);
2415 }
2416
2417 if (private_data && private_data_len)
2418 memcpy(apr_msg->private_data, private_data, private_data_len);
2419}
2420
2421int ib_send_cm_apr(struct ib_cm_id *cm_id,
2422 enum ib_cm_apr_status status,
2423 void *info,
2424 u8 info_length,
2425 const void *private_data,
2426 u8 private_data_len)
2427{
2428 struct cm_id_private *cm_id_priv;
2429 struct ib_mad_send_buf *msg;
2430 struct ib_send_wr *bad_send_wr;
2431 unsigned long flags;
2432 int ret;
2433
2434 if ((private_data && private_data_len > IB_CM_APR_PRIVATE_DATA_SIZE) ||
2435 (info && info_length > IB_CM_APR_INFO_LENGTH))
2436 return -EINVAL;
2437
2438 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2439 spin_lock_irqsave(&cm_id_priv->lock, flags);
2440 if (cm_id->state != IB_CM_ESTABLISHED ||
2441 (cm_id->lap_state != IB_CM_LAP_RCVD &&
2442 cm_id->lap_state != IB_CM_MRA_LAP_SENT)) {
2443 ret = -EINVAL;
2444 goto out;
2445 }
2446
2447 ret = cm_alloc_msg(cm_id_priv, &msg);
2448 if (ret)
2449 goto out;
2450
2451 cm_format_apr((struct cm_apr_msg *) msg->mad, cm_id_priv, status,
2452 info, info_length, private_data, private_data_len);
2453 ret = ib_post_send_mad(cm_id_priv->av.port->mad_agent,
2454 &msg->send_wr, &bad_send_wr);
2455 if (ret) {
2456 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2457 cm_free_msg(msg);
2458 return ret;
2459 }
2460
2461 cm_id->lap_state = IB_CM_LAP_IDLE;
2462out: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2463 return ret;
2464}
2465EXPORT_SYMBOL(ib_send_cm_apr);
2466
2467static int cm_apr_handler(struct cm_work *work)
2468{
2469 struct cm_id_private *cm_id_priv;
2470 struct cm_apr_msg *apr_msg;
2471 unsigned long flags;
2472 int ret;
2473
2474 apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad;
2475 cm_id_priv = cm_acquire_id(apr_msg->remote_comm_id,
2476 apr_msg->local_comm_id);
2477 if (!cm_id_priv)
2478 return -EINVAL; /* Unmatched reply. */
2479
2480 work->cm_event.param.apr_rcvd.ap_status = apr_msg->ap_status;
2481 work->cm_event.param.apr_rcvd.apr_info = &apr_msg->info;
2482 work->cm_event.param.apr_rcvd.info_len = apr_msg->info_length;
2483 work->cm_event.private_data = &apr_msg->private_data;
2484
2485 spin_lock_irqsave(&cm_id_priv->lock, flags);
2486 if (cm_id_priv->id.state != IB_CM_ESTABLISHED ||
2487 (cm_id_priv->id.lap_state != IB_CM_LAP_SENT &&
2488 cm_id_priv->id.lap_state != IB_CM_MRA_LAP_RCVD)) {
2489 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2490 goto out;
2491 }
2492 cm_id_priv->id.lap_state = IB_CM_LAP_IDLE;
2493 ib_cancel_mad(cm_id_priv->av.port->mad_agent,
2494 (unsigned long) cm_id_priv->msg);
2495 cm_id_priv->msg = NULL;
2496
2497 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2498 if (!ret)
2499 list_add_tail(&work->list, &cm_id_priv->work_list);
2500 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2501
2502 if (ret)
2503 cm_process_work(cm_id_priv, work);
2504 else
2505 cm_deref_id(cm_id_priv);
2506 return 0;
2507out:
2508 cm_deref_id(cm_id_priv);
2509 return -EINVAL;
2510}
2511
2512static int cm_timewait_handler(struct cm_work *work)
2513{
2514 struct cm_timewait_info *timewait_info;
2515 struct cm_id_private *cm_id_priv;
2516 unsigned long flags;
2517 int ret;
2518
2519 timewait_info = (struct cm_timewait_info *)work;
2520 cm_cleanup_timewait(timewait_info);
2521
2522 cm_id_priv = cm_acquire_id(timewait_info->work.local_id,
2523 timewait_info->work.remote_id);
2524 if (!cm_id_priv)
2525 return -EINVAL;
2526
2527 spin_lock_irqsave(&cm_id_priv->lock, flags);
2528 if (cm_id_priv->id.state != IB_CM_TIMEWAIT ||
2529 cm_id_priv->remote_qpn != timewait_info->remote_qpn) {
2530 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2531 goto out;
2532 }
2533 cm_id_priv->id.state = IB_CM_IDLE;
2534 ret = atomic_inc_and_test(&cm_id_priv->work_count);
2535 if (!ret)
2536 list_add_tail(&work->list, &cm_id_priv->work_list);
2537 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2538
2539 if (ret)
2540 cm_process_work(cm_id_priv, work);
2541 else
2542 cm_deref_id(cm_id_priv);
2543 return 0;
2544out:
2545 cm_deref_id(cm_id_priv);
2546 return -EINVAL;
2547}
2548
2549static void cm_format_sidr_req(struct cm_sidr_req_msg *sidr_req_msg,
2550 struct cm_id_private *cm_id_priv,
2551 struct ib_cm_sidr_req_param *param)
2552{
2553 cm_format_mad_hdr(&sidr_req_msg->hdr, CM_SIDR_REQ_ATTR_ID,
2554 cm_form_tid(cm_id_priv, CM_MSG_SEQUENCE_SIDR));
2555 sidr_req_msg->request_id = cm_id_priv->id.local_id;
2556 sidr_req_msg->pkey = param->pkey;
2557 sidr_req_msg->service_id = param->service_id;
2558
2559 if (param->private_data && param->private_data_len)
2560 memcpy(sidr_req_msg->private_data, param->private_data,
2561 param->private_data_len);
2562}
2563
2564int ib_send_cm_sidr_req(struct ib_cm_id *cm_id,
2565 struct ib_cm_sidr_req_param *param)
2566{
2567 struct cm_id_private *cm_id_priv;
2568 struct ib_mad_send_buf *msg;
2569 struct ib_send_wr *bad_send_wr;
2570 unsigned long flags;
2571 int ret;
2572
2573 if (!param->path || (param->private_data &&
2574 param->private_data_len > IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE))
2575 return -EINVAL;
2576
2577 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2578 ret = cm_init_av_by_path(param->path, &cm_id_priv->av);
2579 if (ret)
2580 goto out;
2581
2582 cm_id->service_id = param->service_id;
2583 cm_id->service_mask = ~0ULL;
2584 cm_id_priv->timeout_ms = param->timeout_ms;
2585 cm_id_priv->max_cm_retries = param->max_cm_retries;
2586 ret = cm_alloc_msg(cm_id_priv, &msg);
2587 if (ret)
2588 goto out;
2589
2590 cm_format_sidr_req((struct cm_sidr_req_msg *) msg->mad, cm_id_priv,
2591 param);
2592 msg->send_wr.wr.ud.timeout_ms = cm_id_priv->timeout_ms;
2593 msg->context[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT;
2594
2595 spin_lock_irqsave(&cm_id_priv->lock, flags);
2596 if (cm_id->state == IB_CM_IDLE)
2597 ret = ib_post_send_mad(cm_id_priv->av.port->mad_agent,
2598 &msg->send_wr, &bad_send_wr);
2599 else
2600 ret = -EINVAL;
2601
2602 if (ret) {
2603 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2604 cm_free_msg(msg);
2605 goto out;
2606 }
2607 cm_id->state = IB_CM_SIDR_REQ_SENT;
2608 cm_id_priv->msg = msg;
2609 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2610out:
2611 return ret;
2612}
2613EXPORT_SYMBOL(ib_send_cm_sidr_req);
2614
2615static void cm_format_sidr_req_event(struct cm_work *work,
2616 struct ib_cm_id *listen_id)
2617{
2618 struct cm_sidr_req_msg *sidr_req_msg;
2619 struct ib_cm_sidr_req_event_param *param;
2620
2621 sidr_req_msg = (struct cm_sidr_req_msg *)
2622 work->mad_recv_wc->recv_buf.mad;
2623 param = &work->cm_event.param.sidr_req_rcvd;
2624 param->pkey = sidr_req_msg->pkey;
2625 param->listen_id = listen_id;
2626 param->device = work->port->mad_agent->device;
2627 param->port = work->port->port_num;
2628 work->cm_event.private_data = &sidr_req_msg->private_data;
2629}
2630
2631static int cm_sidr_req_handler(struct cm_work *work)
2632{
2633 struct ib_cm_id *cm_id;
2634 struct cm_id_private *cm_id_priv, *cur_cm_id_priv;
2635 struct cm_sidr_req_msg *sidr_req_msg;
2636 struct ib_wc *wc;
2637 unsigned long flags;
2638
2639 cm_id = ib_create_cm_id(NULL, NULL);
2640 if (IS_ERR(cm_id))
2641 return PTR_ERR(cm_id);
2642 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2643
2644 /* Record SGID/SLID and request ID for lookup. */
2645 sidr_req_msg = (struct cm_sidr_req_msg *)
2646 work->mad_recv_wc->recv_buf.mad;
2647 wc = work->mad_recv_wc->wc;
2648 cm_id_priv->av.dgid.global.subnet_prefix = wc->slid;
2649 cm_id_priv->av.dgid.global.interface_id = 0;
2650 cm_init_av_for_response(work->port, work->mad_recv_wc->wc,
2651 &cm_id_priv->av);
2652 cm_id_priv->id.remote_id = sidr_req_msg->request_id;
2653 cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD;
2654 cm_id_priv->tid = sidr_req_msg->hdr.tid;
2655 atomic_inc(&cm_id_priv->work_count);
2656
2657 spin_lock_irqsave(&cm.lock, flags);
2658 cur_cm_id_priv = cm_insert_remote_sidr(cm_id_priv);
2659 if (cur_cm_id_priv) {
2660 spin_unlock_irqrestore(&cm.lock, flags);
2661 goto out; /* Duplicate message. */
2662 }
2663 cur_cm_id_priv = cm_find_listen(sidr_req_msg->service_id);
2664 if (!cur_cm_id_priv) {
2665 rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
2666 spin_unlock_irqrestore(&cm.lock, flags);
2667 /* todo: reply with no match */
2668 goto out; /* No match. */
2669 }
2670 atomic_inc(&cur_cm_id_priv->refcount);
2671 spin_unlock_irqrestore(&cm.lock, flags);
2672
2673 cm_id_priv->id.cm_handler = cur_cm_id_priv->id.cm_handler;
2674 cm_id_priv->id.context = cur_cm_id_priv->id.context;
2675 cm_id_priv->id.service_id = sidr_req_msg->service_id;
2676 cm_id_priv->id.service_mask = ~0ULL;
2677
2678 cm_format_sidr_req_event(work, &cur_cm_id_priv->id);
2679 cm_process_work(cm_id_priv, work);
2680 cm_deref_id(cur_cm_id_priv);
2681 return 0;
2682out:
2683 ib_destroy_cm_id(&cm_id_priv->id);
2684 return -EINVAL;
2685}
2686
2687static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg,
2688 struct cm_id_private *cm_id_priv,
2689 struct ib_cm_sidr_rep_param *param)
2690{
2691 cm_format_mad_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID,
2692 cm_id_priv->tid);
2693 sidr_rep_msg->request_id = cm_id_priv->id.remote_id;
2694 sidr_rep_msg->status = param->status;
2695 cm_sidr_rep_set_qpn(sidr_rep_msg, cpu_to_be32(param->qp_num));
2696 sidr_rep_msg->service_id = cm_id_priv->id.service_id;
2697 sidr_rep_msg->qkey = cpu_to_be32(param->qkey);
2698
2699 if (param->info && param->info_length)
2700 memcpy(sidr_rep_msg->info, param->info, param->info_length);
2701
2702 if (param->private_data && param->private_data_len)
2703 memcpy(sidr_rep_msg->private_data, param->private_data,
2704 param->private_data_len);
2705}
2706
2707int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id,
2708 struct ib_cm_sidr_rep_param *param)
2709{
2710 struct cm_id_private *cm_id_priv;
2711 struct ib_mad_send_buf *msg;
2712 struct ib_send_wr *bad_send_wr;
2713 unsigned long flags;
2714 int ret;
2715
2716 if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) ||
2717 (param->private_data &&
2718 param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE))
2719 return -EINVAL;
2720
2721 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2722 spin_lock_irqsave(&cm_id_priv->lock, flags);
2723 if (cm_id->state != IB_CM_SIDR_REQ_RCVD) {
2724 ret = -EINVAL;
2725 goto error;
2726 }
2727
2728 ret = cm_alloc_msg(cm_id_priv, &msg);
2729 if (ret)
2730 goto error;
2731
2732 cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv,
2733 param);
2734 ret = ib_post_send_mad(cm_id_priv->av.port->mad_agent,
2735 &msg->send_wr, &bad_send_wr);
2736 if (ret) {
2737 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2738 cm_free_msg(msg);
2739 return ret;
2740 }
2741 cm_id->state = IB_CM_IDLE;
2742 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2743
2744 spin_lock_irqsave(&cm.lock, flags);
2745 rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
2746 spin_unlock_irqrestore(&cm.lock, flags);
2747 return 0;
2748
2749error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2750 return ret;
2751}
2752EXPORT_SYMBOL(ib_send_cm_sidr_rep);
2753
2754static void cm_format_sidr_rep_event(struct cm_work *work)
2755{
2756 struct cm_sidr_rep_msg *sidr_rep_msg;
2757 struct ib_cm_sidr_rep_event_param *param;
2758
2759 sidr_rep_msg = (struct cm_sidr_rep_msg *)
2760 work->mad_recv_wc->recv_buf.mad;
2761 param = &work->cm_event.param.sidr_rep_rcvd;
2762 param->status = sidr_rep_msg->status;
2763 param->qkey = be32_to_cpu(sidr_rep_msg->qkey);
2764 param->qpn = be32_to_cpu(cm_sidr_rep_get_qpn(sidr_rep_msg));
2765 param->info = &sidr_rep_msg->info;
2766 param->info_len = sidr_rep_msg->info_length;
2767 work->cm_event.private_data = &sidr_rep_msg->private_data;
2768}
2769
2770static int cm_sidr_rep_handler(struct cm_work *work)
2771{
2772 struct cm_sidr_rep_msg *sidr_rep_msg;
2773 struct cm_id_private *cm_id_priv;
2774 unsigned long flags;
2775
2776 sidr_rep_msg = (struct cm_sidr_rep_msg *)
2777 work->mad_recv_wc->recv_buf.mad;
2778 cm_id_priv = cm_acquire_id(sidr_rep_msg->request_id, 0);
2779 if (!cm_id_priv)
2780 return -EINVAL; /* Unmatched reply. */
2781
2782 spin_lock_irqsave(&cm_id_priv->lock, flags);
2783 if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) {
2784 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2785 goto out;
2786 }
2787 cm_id_priv->id.state = IB_CM_IDLE;
2788 ib_cancel_mad(cm_id_priv->av.port->mad_agent,
2789 (unsigned long) cm_id_priv->msg);
2790 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2791
2792 cm_format_sidr_rep_event(work);
2793 cm_process_work(cm_id_priv, work);
2794 return 0;
2795out:
2796 cm_deref_id(cm_id_priv);
2797 return -EINVAL;
2798}
2799
2800static void cm_process_send_error(struct ib_mad_send_buf *msg,
2801 enum ib_wc_status wc_status)
2802{
2803 struct cm_id_private *cm_id_priv;
2804 struct ib_cm_event cm_event;
2805 enum ib_cm_state state;
2806 unsigned long flags;
2807 int ret;
2808
2809 memset(&cm_event, 0, sizeof cm_event);
2810 cm_id_priv = msg->context[0];
2811
2812 /* Discard old sends or ones without a response. */
2813 spin_lock_irqsave(&cm_id_priv->lock, flags);
2814 state = (enum ib_cm_state) (unsigned long) msg->context[1];
2815 if (msg != cm_id_priv->msg || state != cm_id_priv->id.state)
2816 goto discard;
2817
2818 switch (state) {
2819 case IB_CM_REQ_SENT:
2820 case IB_CM_MRA_REQ_RCVD:
2821 cm_reset_to_idle(cm_id_priv);
2822 cm_event.event = IB_CM_REQ_ERROR;
2823 break;
2824 case IB_CM_REP_SENT:
2825 case IB_CM_MRA_REP_RCVD:
2826 cm_reset_to_idle(cm_id_priv);
2827 cm_event.event = IB_CM_REP_ERROR;
2828 break;
2829 case IB_CM_DREQ_SENT:
2830 cm_enter_timewait(cm_id_priv);
2831 cm_event.event = IB_CM_DREQ_ERROR;
2832 break;
2833 case IB_CM_SIDR_REQ_SENT:
2834 cm_id_priv->id.state = IB_CM_IDLE;
2835 cm_event.event = IB_CM_SIDR_REQ_ERROR;
2836 break;
2837 default:
2838 goto discard;
2839 }
2840 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2841 cm_event.param.send_status = wc_status;
2842
2843 /* No other events can occur on the cm_id at this point. */
2844 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event);
2845 cm_free_msg(msg);
2846 if (ret)
2847 ib_destroy_cm_id(&cm_id_priv->id);
2848 return;
2849discard:
2850 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2851 cm_free_msg(msg);
2852}
2853
2854static void cm_send_handler(struct ib_mad_agent *mad_agent,
2855 struct ib_mad_send_wc *mad_send_wc)
2856{
2857 struct ib_mad_send_buf *msg;
2858
2859 msg = (struct ib_mad_send_buf *)(unsigned long)mad_send_wc->wr_id;
2860
2861 switch (mad_send_wc->status) {
2862 case IB_WC_SUCCESS:
2863 case IB_WC_WR_FLUSH_ERR:
2864 cm_free_msg(msg);
2865 break;
2866 default:
2867 if (msg->context[0] && msg->context[1])
2868 cm_process_send_error(msg, mad_send_wc->status);
2869 else
2870 cm_free_msg(msg);
2871 break;
2872 }
2873}
2874
2875static void cm_work_handler(void *data)
2876{
2877 struct cm_work *work = data;
2878 int ret;
2879
2880 switch (work->cm_event.event) {
2881 case IB_CM_REQ_RECEIVED:
2882 ret = cm_req_handler(work);
2883 break;
2884 case IB_CM_MRA_RECEIVED:
2885 ret = cm_mra_handler(work);
2886 break;
2887 case IB_CM_REJ_RECEIVED:
2888 ret = cm_rej_handler(work);
2889 break;
2890 case IB_CM_REP_RECEIVED:
2891 ret = cm_rep_handler(work);
2892 break;
2893 case IB_CM_RTU_RECEIVED:
2894 ret = cm_rtu_handler(work);
2895 break;
2896 case IB_CM_USER_ESTABLISHED:
2897 ret = cm_establish_handler(work);
2898 break;
2899 case IB_CM_DREQ_RECEIVED:
2900 ret = cm_dreq_handler(work);
2901 break;
2902 case IB_CM_DREP_RECEIVED:
2903 ret = cm_drep_handler(work);
2904 break;
2905 case IB_CM_SIDR_REQ_RECEIVED:
2906 ret = cm_sidr_req_handler(work);
2907 break;
2908 case IB_CM_SIDR_REP_RECEIVED:
2909 ret = cm_sidr_rep_handler(work);
2910 break;
2911 case IB_CM_LAP_RECEIVED:
2912 ret = cm_lap_handler(work);
2913 break;
2914 case IB_CM_APR_RECEIVED:
2915 ret = cm_apr_handler(work);
2916 break;
2917 case IB_CM_TIMEWAIT_EXIT:
2918 ret = cm_timewait_handler(work);
2919 break;
2920 default:
2921 ret = -EINVAL;
2922 break;
2923 }
2924 if (ret)
2925 cm_free_work(work);
2926}
2927
2928int ib_cm_establish(struct ib_cm_id *cm_id)
2929{
2930 struct cm_id_private *cm_id_priv;
2931 struct cm_work *work;
2932 unsigned long flags;
2933 int ret = 0;
2934
2935 work = kmalloc(sizeof *work, GFP_ATOMIC);
2936 if (!work)
2937 return -ENOMEM;
2938
2939 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
2940 spin_lock_irqsave(&cm_id_priv->lock, flags);
2941 switch (cm_id->state)
2942 {
2943 case IB_CM_REP_SENT:
2944 case IB_CM_MRA_REP_RCVD:
2945 cm_id->state = IB_CM_ESTABLISHED;
2946 break;
2947 case IB_CM_ESTABLISHED:
2948 ret = -EISCONN;
2949 break;
2950 default:
2951 ret = -EINVAL;
2952 break;
2953 }
2954 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
2955
2956 if (ret) {
2957 kfree(work);
2958 goto out;
2959 }
2960
2961 /*
2962 * The CM worker thread may try to destroy the cm_id before it
2963 * can execute this work item. To prevent potential deadlock,
2964 * we need to find the cm_id once we're in the context of the
2965 * worker thread, rather than holding a reference on it.
2966 */
2967 INIT_WORK(&work->work, cm_work_handler, work);
2968 work->local_id = cm_id->local_id;
2969 work->remote_id = cm_id->remote_id;
2970 work->mad_recv_wc = NULL;
2971 work->cm_event.event = IB_CM_USER_ESTABLISHED;
2972 queue_work(cm.wq, &work->work);
2973out:
2974 return ret;
2975}
2976EXPORT_SYMBOL(ib_cm_establish);
2977
2978static void cm_recv_handler(struct ib_mad_agent *mad_agent,
2979 struct ib_mad_recv_wc *mad_recv_wc)
2980{
2981 struct cm_work *work;
2982 enum ib_cm_event_type event;
2983 int paths = 0;
2984
2985 switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) {
2986 case CM_REQ_ATTR_ID:
2987 paths = 1 + (((struct cm_req_msg *) mad_recv_wc->recv_buf.mad)->
2988 alt_local_lid != 0);
2989 event = IB_CM_REQ_RECEIVED;
2990 break;
2991 case CM_MRA_ATTR_ID:
2992 event = IB_CM_MRA_RECEIVED;
2993 break;
2994 case CM_REJ_ATTR_ID:
2995 event = IB_CM_REJ_RECEIVED;
2996 break;
2997 case CM_REP_ATTR_ID:
2998 event = IB_CM_REP_RECEIVED;
2999 break;
3000 case CM_RTU_ATTR_ID:
3001 event = IB_CM_RTU_RECEIVED;
3002 break;
3003 case CM_DREQ_ATTR_ID:
3004 event = IB_CM_DREQ_RECEIVED;
3005 break;
3006 case CM_DREP_ATTR_ID:
3007 event = IB_CM_DREP_RECEIVED;
3008 break;
3009 case CM_SIDR_REQ_ATTR_ID:
3010 event = IB_CM_SIDR_REQ_RECEIVED;
3011 break;
3012 case CM_SIDR_REP_ATTR_ID:
3013 event = IB_CM_SIDR_REP_RECEIVED;
3014 break;
3015 case CM_LAP_ATTR_ID:
3016 paths = 1;
3017 event = IB_CM_LAP_RECEIVED;
3018 break;
3019 case CM_APR_ATTR_ID:
3020 event = IB_CM_APR_RECEIVED;
3021 break;
3022 default:
3023 ib_free_recv_mad(mad_recv_wc);
3024 return;
3025 }
3026
3027 work = kmalloc(sizeof *work + sizeof(struct ib_sa_path_rec) * paths,
3028 GFP_KERNEL);
3029 if (!work) {
3030 ib_free_recv_mad(mad_recv_wc);
3031 return;
3032 }
3033
3034 INIT_WORK(&work->work, cm_work_handler, work);
3035 work->cm_event.event = event;
3036 work->mad_recv_wc = mad_recv_wc;
3037 work->port = (struct cm_port *)mad_agent->context;
3038 queue_work(cm.wq, &work->work);
3039}
3040
3041static int cm_init_qp_init_attr(struct cm_id_private *cm_id_priv,
3042 struct ib_qp_attr *qp_attr,
3043 int *qp_attr_mask)
3044{
3045 unsigned long flags;
3046 int ret;
3047
3048 spin_lock_irqsave(&cm_id_priv->lock, flags);
3049 switch (cm_id_priv->id.state) {
3050 case IB_CM_REQ_SENT:
3051 case IB_CM_MRA_REQ_RCVD:
3052 case IB_CM_REQ_RCVD:
3053 case IB_CM_MRA_REQ_SENT:
3054 case IB_CM_REP_RCVD:
3055 case IB_CM_MRA_REP_SENT:
3056 case IB_CM_REP_SENT:
3057 case IB_CM_MRA_REP_RCVD:
3058 case IB_CM_ESTABLISHED:
3059 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS |
3060 IB_QP_PKEY_INDEX | IB_QP_PORT;
3061 qp_attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE;
3062 if (cm_id_priv->responder_resources)
3063 qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_WRITE |
3064 IB_ACCESS_REMOTE_READ;
3065 qp_attr->pkey_index = cm_id_priv->av.pkey_index;
3066 qp_attr->port_num = cm_id_priv->av.port->port_num;
3067 ret = 0;
3068 break;
3069 default:
3070 ret = -EINVAL;
3071 break;
3072 }
3073 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3074 return ret;
3075}
3076
3077static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv,
3078 struct ib_qp_attr *qp_attr,
3079 int *qp_attr_mask)
3080{
3081 unsigned long flags;
3082 int ret;
3083
3084 spin_lock_irqsave(&cm_id_priv->lock, flags);
3085 switch (cm_id_priv->id.state) {
3086 case IB_CM_REQ_RCVD:
3087 case IB_CM_MRA_REQ_SENT:
3088 case IB_CM_REP_RCVD:
3089 case IB_CM_MRA_REP_SENT:
3090 case IB_CM_REP_SENT:
3091 case IB_CM_MRA_REP_RCVD:
3092 case IB_CM_ESTABLISHED:
3093 *qp_attr_mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU |
3094 IB_QP_DEST_QPN | IB_QP_RQ_PSN |
3095 IB_QP_MAX_DEST_RD_ATOMIC | IB_QP_MIN_RNR_TIMER;
3096 qp_attr->ah_attr = cm_id_priv->av.ah_attr;
3097 qp_attr->path_mtu = cm_id_priv->path_mtu;
3098 qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn);
3099 qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn);
3100 qp_attr->max_dest_rd_atomic = cm_id_priv->responder_resources;
3101 qp_attr->min_rnr_timer = 0;
3102 if (cm_id_priv->alt_av.ah_attr.dlid) {
3103 *qp_attr_mask |= IB_QP_ALT_PATH;
3104 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr;
3105 }
3106 ret = 0;
3107 break;
3108 default:
3109 ret = -EINVAL;
3110 break;
3111 }
3112 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3113 return ret;
3114}
3115
3116static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv,
3117 struct ib_qp_attr *qp_attr,
3118 int *qp_attr_mask)
3119{
3120 unsigned long flags;
3121 int ret;
3122
3123 spin_lock_irqsave(&cm_id_priv->lock, flags);
3124 switch (cm_id_priv->id.state) {
3125 case IB_CM_REP_RCVD:
3126 case IB_CM_MRA_REP_SENT:
3127 case IB_CM_REP_SENT:
3128 case IB_CM_MRA_REP_RCVD:
3129 case IB_CM_ESTABLISHED:
3130 *qp_attr_mask = IB_QP_STATE | IB_QP_TIMEOUT | IB_QP_RETRY_CNT |
3131 IB_QP_RNR_RETRY | IB_QP_SQ_PSN |
3132 IB_QP_MAX_QP_RD_ATOMIC;
3133 qp_attr->timeout = cm_id_priv->local_ack_timeout;
3134 qp_attr->retry_cnt = cm_id_priv->retry_count;
3135 qp_attr->rnr_retry = cm_id_priv->rnr_retry_count;
3136 qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn);
3137 qp_attr->max_rd_atomic = cm_id_priv->initiator_depth;
3138 if (cm_id_priv->alt_av.ah_attr.dlid) {
3139 *qp_attr_mask |= IB_QP_PATH_MIG_STATE;
3140 qp_attr->path_mig_state = IB_MIG_REARM;
3141 }
3142 ret = 0;
3143 break;
3144 default:
3145 ret = -EINVAL;
3146 break;
3147 }
3148 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3149 return ret;
3150}
3151
3152int ib_cm_init_qp_attr(struct ib_cm_id *cm_id,
3153 struct ib_qp_attr *qp_attr,
3154 int *qp_attr_mask)
3155{
3156 struct cm_id_private *cm_id_priv;
3157 int ret;
3158
3159 cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3160 switch (qp_attr->qp_state) {
3161 case IB_QPS_INIT:
3162 ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask);
3163 break;
3164 case IB_QPS_RTR:
3165 ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask);
3166 break;
3167 case IB_QPS_RTS:
3168 ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask);
3169 break;
3170 default:
3171 ret = -EINVAL;
3172 break;
3173 }
3174 return ret;
3175}
3176EXPORT_SYMBOL(ib_cm_init_qp_attr);
3177
3178static u64 cm_get_ca_guid(struct ib_device *device)
3179{
3180 struct ib_device_attr *device_attr;
3181 u64 guid;
3182 int ret;
3183
3184 device_attr = kmalloc(sizeof *device_attr, GFP_KERNEL);
3185 if (!device_attr)
3186 return 0;
3187
3188 ret = ib_query_device(device, device_attr);
3189 guid = ret ? 0 : device_attr->node_guid;
3190 kfree(device_attr);
3191 return guid;
3192}
3193
3194static void cm_add_one(struct ib_device *device)
3195{
3196 struct cm_device *cm_dev;
3197 struct cm_port *port;
3198 struct ib_mad_reg_req reg_req = {
3199 .mgmt_class = IB_MGMT_CLASS_CM,
3200 .mgmt_class_version = IB_CM_CLASS_VERSION
3201 };
3202 struct ib_port_modify port_modify = {
3203 .set_port_cap_mask = IB_PORT_CM_SUP
3204 };
3205 unsigned long flags;
3206 int ret;
3207 u8 i;
3208
3209 cm_dev = kmalloc(sizeof(*cm_dev) + sizeof(*port) *
3210 device->phys_port_cnt, GFP_KERNEL);
3211 if (!cm_dev)
3212 return;
3213
3214 cm_dev->device = device;
3215 cm_dev->ca_guid = cm_get_ca_guid(device);
3216 if (!cm_dev->ca_guid)
3217 goto error1;
3218
3219 set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask);
3220 for (i = 1; i <= device->phys_port_cnt; i++) {
3221 port = &cm_dev->port[i-1];
3222 port->cm_dev = cm_dev;
3223 port->port_num = i;
3224 port->mad_agent = ib_register_mad_agent(device, i,
3225 IB_QPT_GSI,
3226 &reg_req,
3227 0,
3228 cm_send_handler,
3229 cm_recv_handler,
3230 port);
3231 if (IS_ERR(port->mad_agent))
3232 goto error2;
3233
3234 ret = ib_modify_port(device, i, 0, &port_modify);
3235 if (ret)
3236 goto error3;
3237 }
3238 ib_set_client_data(device, &cm_client, cm_dev);
3239
3240 write_lock_irqsave(&cm.device_lock, flags);
3241 list_add_tail(&cm_dev->list, &cm.device_list);
3242 write_unlock_irqrestore(&cm.device_lock, flags);
3243 return;
3244
3245error3:
3246 ib_unregister_mad_agent(port->mad_agent);
3247error2:
3248 port_modify.set_port_cap_mask = 0;
3249 port_modify.clr_port_cap_mask = IB_PORT_CM_SUP;
3250 while (--i) {
3251 port = &cm_dev->port[i-1];
3252 ib_modify_port(device, port->port_num, 0, &port_modify);
3253 ib_unregister_mad_agent(port->mad_agent);
3254 }
3255error1:
3256 kfree(cm_dev);
3257}
3258
3259static void cm_remove_one(struct ib_device *device)
3260{
3261 struct cm_device *cm_dev;
3262 struct cm_port *port;
3263 struct ib_port_modify port_modify = {
3264 .clr_port_cap_mask = IB_PORT_CM_SUP
3265 };
3266 unsigned long flags;
3267 int i;
3268
3269 cm_dev = ib_get_client_data(device, &cm_client);
3270 if (!cm_dev)
3271 return;
3272
3273 write_lock_irqsave(&cm.device_lock, flags);
3274 list_del(&cm_dev->list);
3275 write_unlock_irqrestore(&cm.device_lock, flags);
3276
3277 for (i = 1; i <= device->phys_port_cnt; i++) {
3278 port = &cm_dev->port[i-1];
3279 ib_modify_port(device, port->port_num, 0, &port_modify);
3280 ib_unregister_mad_agent(port->mad_agent);
3281 }
3282 kfree(cm_dev);
3283}
3284
3285static int __init ib_cm_init(void)
3286{
3287 int ret;
3288
3289 memset(&cm, 0, sizeof cm);
3290 INIT_LIST_HEAD(&cm.device_list);
3291 rwlock_init(&cm.device_lock);
3292 spin_lock_init(&cm.lock);
3293 cm.listen_service_table = RB_ROOT;
3294 cm.listen_service_id = __constant_be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID);
3295 cm.remote_id_table = RB_ROOT;
3296 cm.remote_qp_table = RB_ROOT;
3297 cm.remote_sidr_table = RB_ROOT;
3298 idr_init(&cm.local_id_table);
3299 idr_pre_get(&cm.local_id_table, GFP_KERNEL);
3300
3301 cm.wq = create_workqueue("ib_cm");
3302 if (!cm.wq)
3303 return -ENOMEM;
3304
3305 ret = ib_register_client(&cm_client);
3306 if (ret)
3307 goto error;
3308
3309 return 0;
3310error:
3311 destroy_workqueue(cm.wq);
3312 return ret;
3313}
3314
3315static void __exit ib_cm_cleanup(void)
3316{
3317 flush_workqueue(cm.wq);
3318 destroy_workqueue(cm.wq);
3319 ib_unregister_client(&cm_client);
3320}
3321
3322module_init(ib_cm_init);
3323module_exit(ib_cm_cleanup);
3324
diff --git a/drivers/infiniband/core/cm_msgs.h b/drivers/infiniband/core/cm_msgs.h
new file mode 100644
index 000000000000..15a309a77b2b
--- /dev/null
+++ b/drivers/infiniband/core/cm_msgs.h
@@ -0,0 +1,819 @@
1/*
2 * Copyright (c) 2004 Intel Corporation. All rights reserved.
3 * Copyright (c) 2004 Topspin Corporation. All rights reserved.
4 * Copyright (c) 2004 Voltaire Corporation. All rights reserved.
5 *
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING the madirectory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 * Redistribution and use source and binary forms, with or
13 * withmodification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retathe above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
20 * - Redistributions binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer the documentation and/or other materials
23 * provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHWARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS THE
32 * SOFTWARE.
33 */
34#if !defined(CM_MSGS_H)
35#define CM_MSGS_H
36
37#include <ib_mad.h>
38
39/*
40 * Parameters to routines below should be in network-byte order, and values
41 * are returned in network-byte order.
42 */
43
44#define IB_CM_CLASS_VERSION 2 /* IB specification 1.2 */
45
46enum cm_msg_attr_id {
47 CM_REQ_ATTR_ID = __constant_htons(0x0010),
48 CM_MRA_ATTR_ID = __constant_htons(0x0011),
49 CM_REJ_ATTR_ID = __constant_htons(0x0012),
50 CM_REP_ATTR_ID = __constant_htons(0x0013),
51 CM_RTU_ATTR_ID = __constant_htons(0x0014),
52 CM_DREQ_ATTR_ID = __constant_htons(0x0015),
53 CM_DREP_ATTR_ID = __constant_htons(0x0016),
54 CM_SIDR_REQ_ATTR_ID = __constant_htons(0x0017),
55 CM_SIDR_REP_ATTR_ID = __constant_htons(0x0018),
56 CM_LAP_ATTR_ID = __constant_htons(0x0019),
57 CM_APR_ATTR_ID = __constant_htons(0x001A)
58};
59
60enum cm_msg_sequence {
61 CM_MSG_SEQUENCE_REQ,
62 CM_MSG_SEQUENCE_LAP,
63 CM_MSG_SEQUENCE_DREQ,
64 CM_MSG_SEQUENCE_SIDR
65};
66
67struct cm_req_msg {
68 struct ib_mad_hdr hdr;
69
70 u32 local_comm_id;
71 u32 rsvd4;
72 u64 service_id;
73 u64 local_ca_guid;
74 u32 rsvd24;
75 u32 local_qkey;
76 /* local QPN:24, responder resources:8 */
77 u32 offset32;
78 /* local EECN:24, initiator depth:8 */
79 u32 offset36;
80 /*
81 * remote EECN:24, remote CM response timeout:5,
82 * transport service type:2, end-to-end flow control:1
83 */
84 u32 offset40;
85 /* starting PSN:24, local CM response timeout:5, retry count:3 */
86 u32 offset44;
87 u16 pkey;
88 /* path MTU:4, RDC exists:1, RNR retry count:3. */
89 u8 offset50;
90 /* max CM Retries:4, SRQ:1, rsvd:3 */
91 u8 offset51;
92
93 u16 primary_local_lid;
94 u16 primary_remote_lid;
95 union ib_gid primary_local_gid;
96 union ib_gid primary_remote_gid;
97 /* flow label:20, rsvd:6, packet rate:6 */
98 u32 primary_offset88;
99 u8 primary_traffic_class;
100 u8 primary_hop_limit;
101 /* SL:4, subnet local:1, rsvd:3 */
102 u8 primary_offset94;
103 /* local ACK timeout:5, rsvd:3 */
104 u8 primary_offset95;
105
106 u16 alt_local_lid;
107 u16 alt_remote_lid;
108 union ib_gid alt_local_gid;
109 union ib_gid alt_remote_gid;
110 /* flow label:20, rsvd:6, packet rate:6 */
111 u32 alt_offset132;
112 u8 alt_traffic_class;
113 u8 alt_hop_limit;
114 /* SL:4, subnet local:1, rsvd:3 */
115 u8 alt_offset138;
116 /* local ACK timeout:5, rsvd:3 */
117 u8 alt_offset139;
118
119 u8 private_data[IB_CM_REQ_PRIVATE_DATA_SIZE];
120
121} __attribute__ ((packed));
122
123static inline u32 cm_req_get_local_qpn(struct cm_req_msg *req_msg)
124{
125 return cpu_to_be32(be32_to_cpu(req_msg->offset32) >> 8);
126}
127
128static inline void cm_req_set_local_qpn(struct cm_req_msg *req_msg, u32 qpn)
129{
130 req_msg->offset32 = cpu_to_be32((be32_to_cpu(qpn) << 8) |
131 (be32_to_cpu(req_msg->offset32) &
132 0x000000FF));
133}
134
135static inline u8 cm_req_get_resp_res(struct cm_req_msg *req_msg)
136{
137 return (u8) be32_to_cpu(req_msg->offset32);
138}
139
140static inline void cm_req_set_resp_res(struct cm_req_msg *req_msg, u8 resp_res)
141{
142 req_msg->offset32 = cpu_to_be32(resp_res |
143 (be32_to_cpu(req_msg->offset32) &
144 0xFFFFFF00));
145}
146
147static inline u8 cm_req_get_init_depth(struct cm_req_msg *req_msg)
148{
149 return (u8) be32_to_cpu(req_msg->offset36);
150}
151
152static inline void cm_req_set_init_depth(struct cm_req_msg *req_msg,
153 u8 init_depth)
154{
155 req_msg->offset36 = cpu_to_be32(init_depth |
156 (be32_to_cpu(req_msg->offset36) &
157 0xFFFFFF00));
158}
159
160static inline u8 cm_req_get_remote_resp_timeout(struct cm_req_msg *req_msg)
161{
162 return (u8) ((be32_to_cpu(req_msg->offset40) & 0xF8) >> 3);
163}
164
165static inline void cm_req_set_remote_resp_timeout(struct cm_req_msg *req_msg,
166 u8 resp_timeout)
167{
168 req_msg->offset40 = cpu_to_be32((resp_timeout << 3) |
169 (be32_to_cpu(req_msg->offset40) &
170 0xFFFFFF07));
171}
172
173static inline enum ib_qp_type cm_req_get_qp_type(struct cm_req_msg *req_msg)
174{
175 u8 transport_type = (u8) (be32_to_cpu(req_msg->offset40) & 0x06) >> 1;
176 switch(transport_type) {
177 case 0: return IB_QPT_RC;
178 case 1: return IB_QPT_UC;
179 default: return 0;
180 }
181}
182
183static inline void cm_req_set_qp_type(struct cm_req_msg *req_msg,
184 enum ib_qp_type qp_type)
185{
186 switch(qp_type) {
187 case IB_QPT_UC:
188 req_msg->offset40 = cpu_to_be32((be32_to_cpu(
189 req_msg->offset40) &
190 0xFFFFFFF9) | 0x2);
191 default:
192 req_msg->offset40 = cpu_to_be32(be32_to_cpu(
193 req_msg->offset40) &
194 0xFFFFFFF9);
195 }
196}
197
198static inline u8 cm_req_get_flow_ctrl(struct cm_req_msg *req_msg)
199{
200 return be32_to_cpu(req_msg->offset40) & 0x1;
201}
202
203static inline void cm_req_set_flow_ctrl(struct cm_req_msg *req_msg,
204 u8 flow_ctrl)
205{
206 req_msg->offset40 = cpu_to_be32((flow_ctrl & 0x1) |
207 (be32_to_cpu(req_msg->offset40) &
208 0xFFFFFFFE));
209}
210
211static inline u32 cm_req_get_starting_psn(struct cm_req_msg *req_msg)
212{
213 return cpu_to_be32(be32_to_cpu(req_msg->offset44) >> 8);
214}
215
216static inline void cm_req_set_starting_psn(struct cm_req_msg *req_msg,
217 u32 starting_psn)
218{
219 req_msg->offset44 = cpu_to_be32((be32_to_cpu(starting_psn) << 8) |
220 (be32_to_cpu(req_msg->offset44) & 0x000000FF));
221}
222
223static inline u8 cm_req_get_local_resp_timeout(struct cm_req_msg *req_msg)
224{
225 return (u8) ((be32_to_cpu(req_msg->offset44) & 0xF8) >> 3);
226}
227
228static inline void cm_req_set_local_resp_timeout(struct cm_req_msg *req_msg,
229 u8 resp_timeout)
230{
231 req_msg->offset44 = cpu_to_be32((resp_timeout << 3) |
232 (be32_to_cpu(req_msg->offset44) & 0xFFFFFF07));
233}
234
235static inline u8 cm_req_get_retry_count(struct cm_req_msg *req_msg)
236{
237 return (u8) (be32_to_cpu(req_msg->offset44) & 0x7);
238}
239
240static inline void cm_req_set_retry_count(struct cm_req_msg *req_msg,
241 u8 retry_count)
242{
243 req_msg->offset44 = cpu_to_be32((retry_count & 0x7) |
244 (be32_to_cpu(req_msg->offset44) & 0xFFFFFFF8));
245}
246
247static inline u8 cm_req_get_path_mtu(struct cm_req_msg *req_msg)
248{
249 return req_msg->offset50 >> 4;
250}
251
252static inline void cm_req_set_path_mtu(struct cm_req_msg *req_msg, u8 path_mtu)
253{
254 req_msg->offset50 = (u8) ((req_msg->offset50 & 0xF) | (path_mtu << 4));
255}
256
257static inline u8 cm_req_get_rnr_retry_count(struct cm_req_msg *req_msg)
258{
259 return req_msg->offset50 & 0x7;
260}
261
262static inline void cm_req_set_rnr_retry_count(struct cm_req_msg *req_msg,
263 u8 rnr_retry_count)
264{
265 req_msg->offset50 = (u8) ((req_msg->offset50 & 0xF8) |
266 (rnr_retry_count & 0x7));
267}
268
269static inline u8 cm_req_get_max_cm_retries(struct cm_req_msg *req_msg)
270{
271 return req_msg->offset51 >> 4;
272}
273
274static inline void cm_req_set_max_cm_retries(struct cm_req_msg *req_msg,
275 u8 retries)
276{
277 req_msg->offset51 = (u8) ((req_msg->offset51 & 0xF) | (retries << 4));
278}
279
280static inline u8 cm_req_get_srq(struct cm_req_msg *req_msg)
281{
282 return (req_msg->offset51 & 0x8) >> 3;
283}
284
285static inline void cm_req_set_srq(struct cm_req_msg *req_msg, u8 srq)
286{
287 req_msg->offset51 = (u8) ((req_msg->offset51 & 0xF7) |
288 ((srq & 0x1) << 3));
289}
290
291static inline u32 cm_req_get_primary_flow_label(struct cm_req_msg *req_msg)
292{
293 return cpu_to_be32((be32_to_cpu(req_msg->primary_offset88) >> 12));
294}
295
296static inline void cm_req_set_primary_flow_label(struct cm_req_msg *req_msg,
297 u32 flow_label)
298{
299 req_msg->primary_offset88 = cpu_to_be32(
300 (be32_to_cpu(req_msg->primary_offset88) &
301 0x00000FFF) |
302 (be32_to_cpu(flow_label) << 12));
303}
304
305static inline u8 cm_req_get_primary_packet_rate(struct cm_req_msg *req_msg)
306{
307 return (u8) (be32_to_cpu(req_msg->primary_offset88) & 0x3F);
308}
309
310static inline void cm_req_set_primary_packet_rate(struct cm_req_msg *req_msg,
311 u8 rate)
312{
313 req_msg->primary_offset88 = cpu_to_be32(
314 (be32_to_cpu(req_msg->primary_offset88) &
315 0xFFFFFFC0) | (rate & 0x3F));
316}
317
318static inline u8 cm_req_get_primary_sl(struct cm_req_msg *req_msg)
319{
320 return (u8) (req_msg->primary_offset94 >> 4);
321}
322
323static inline void cm_req_set_primary_sl(struct cm_req_msg *req_msg, u8 sl)
324{
325 req_msg->primary_offset94 = (u8) ((req_msg->primary_offset94 & 0x0F) |
326 (sl << 4));
327}
328
329static inline u8 cm_req_get_primary_subnet_local(struct cm_req_msg *req_msg)
330{
331 return (u8) ((req_msg->primary_offset94 & 0x08) >> 3);
332}
333
334static inline void cm_req_set_primary_subnet_local(struct cm_req_msg *req_msg,
335 u8 subnet_local)
336{
337 req_msg->primary_offset94 = (u8) ((req_msg->primary_offset94 & 0xF7) |
338 ((subnet_local & 0x1) << 3));
339}
340
341static inline u8 cm_req_get_primary_local_ack_timeout(struct cm_req_msg *req_msg)
342{
343 return (u8) (req_msg->primary_offset95 >> 3);
344}
345
346static inline void cm_req_set_primary_local_ack_timeout(struct cm_req_msg *req_msg,
347 u8 local_ack_timeout)
348{
349 req_msg->primary_offset95 = (u8) ((req_msg->primary_offset95 & 0x07) |
350 (local_ack_timeout << 3));
351}
352
353static inline u32 cm_req_get_alt_flow_label(struct cm_req_msg *req_msg)
354{
355 return cpu_to_be32((be32_to_cpu(req_msg->alt_offset132) >> 12));
356}
357
358static inline void cm_req_set_alt_flow_label(struct cm_req_msg *req_msg,
359 u32 flow_label)
360{
361 req_msg->alt_offset132 = cpu_to_be32(
362 (be32_to_cpu(req_msg->alt_offset132) &
363 0x00000FFF) |
364 (be32_to_cpu(flow_label) << 12));
365}
366
367static inline u8 cm_req_get_alt_packet_rate(struct cm_req_msg *req_msg)
368{
369 return (u8) (be32_to_cpu(req_msg->alt_offset132) & 0x3F);
370}
371
372static inline void cm_req_set_alt_packet_rate(struct cm_req_msg *req_msg,
373 u8 rate)
374{
375 req_msg->alt_offset132 = cpu_to_be32(
376 (be32_to_cpu(req_msg->alt_offset132) &
377 0xFFFFFFC0) | (rate & 0x3F));
378}
379
380static inline u8 cm_req_get_alt_sl(struct cm_req_msg *req_msg)
381{
382 return (u8) (req_msg->alt_offset138 >> 4);
383}
384
385static inline void cm_req_set_alt_sl(struct cm_req_msg *req_msg, u8 sl)
386{
387 req_msg->alt_offset138 = (u8) ((req_msg->alt_offset138 & 0x0F) |
388 (sl << 4));
389}
390
391static inline u8 cm_req_get_alt_subnet_local(struct cm_req_msg *req_msg)
392{
393 return (u8) ((req_msg->alt_offset138 & 0x08) >> 3);
394}
395
396static inline void cm_req_set_alt_subnet_local(struct cm_req_msg *req_msg,
397 u8 subnet_local)
398{
399 req_msg->alt_offset138 = (u8) ((req_msg->alt_offset138 & 0xF7) |
400 ((subnet_local & 0x1) << 3));
401}
402
403static inline u8 cm_req_get_alt_local_ack_timeout(struct cm_req_msg *req_msg)
404{
405 return (u8) (req_msg->alt_offset139 >> 3);
406}
407
408static inline void cm_req_set_alt_local_ack_timeout(struct cm_req_msg *req_msg,
409 u8 local_ack_timeout)
410{
411 req_msg->alt_offset139 = (u8) ((req_msg->alt_offset139 & 0x07) |
412 (local_ack_timeout << 3));
413}
414
415/* Message REJected or MRAed */
416enum cm_msg_response {
417 CM_MSG_RESPONSE_REQ = 0x0,
418 CM_MSG_RESPONSE_REP = 0x1,
419 CM_MSG_RESPONSE_OTHER = 0x2
420};
421
422 struct cm_mra_msg {
423 struct ib_mad_hdr hdr;
424
425 u32 local_comm_id;
426 u32 remote_comm_id;
427 /* message MRAed:2, rsvd:6 */
428 u8 offset8;
429 /* service timeout:5, rsvd:3 */
430 u8 offset9;
431
432 u8 private_data[IB_CM_MRA_PRIVATE_DATA_SIZE];
433
434} __attribute__ ((packed));
435
436static inline u8 cm_mra_get_msg_mraed(struct cm_mra_msg *mra_msg)
437{
438 return (u8) (mra_msg->offset8 >> 6);
439}
440
441static inline void cm_mra_set_msg_mraed(struct cm_mra_msg *mra_msg, u8 msg)
442{
443 mra_msg->offset8 = (u8) ((mra_msg->offset8 & 0x3F) | (msg << 6));
444}
445
446static inline u8 cm_mra_get_service_timeout(struct cm_mra_msg *mra_msg)
447{
448 return (u8) (mra_msg->offset9 >> 3);
449}
450
451static inline void cm_mra_set_service_timeout(struct cm_mra_msg *mra_msg,
452 u8 service_timeout)
453{
454 mra_msg->offset9 = (u8) ((mra_msg->offset9 & 0x07) |
455 (service_timeout << 3));
456}
457
458struct cm_rej_msg {
459 struct ib_mad_hdr hdr;
460
461 u32 local_comm_id;
462 u32 remote_comm_id;
463 /* message REJected:2, rsvd:6 */
464 u8 offset8;
465 /* reject info length:7, rsvd:1. */
466 u8 offset9;
467 u16 reason;
468 u8 ari[IB_CM_REJ_ARI_LENGTH];
469
470 u8 private_data[IB_CM_REJ_PRIVATE_DATA_SIZE];
471
472} __attribute__ ((packed));
473
474static inline u8 cm_rej_get_msg_rejected(struct cm_rej_msg *rej_msg)
475{
476 return (u8) (rej_msg->offset8 >> 6);
477}
478
479static inline void cm_rej_set_msg_rejected(struct cm_rej_msg *rej_msg, u8 msg)
480{
481 rej_msg->offset8 = (u8) ((rej_msg->offset8 & 0x3F) | (msg << 6));
482}
483
484static inline u8 cm_rej_get_reject_info_len(struct cm_rej_msg *rej_msg)
485{
486 return (u8) (rej_msg->offset9 >> 1);
487}
488
489static inline void cm_rej_set_reject_info_len(struct cm_rej_msg *rej_msg,
490 u8 len)
491{
492 rej_msg->offset9 = (u8) ((rej_msg->offset9 & 0x1) | (len << 1));
493}
494
495struct cm_rep_msg {
496 struct ib_mad_hdr hdr;
497
498 u32 local_comm_id;
499 u32 remote_comm_id;
500 u32 local_qkey;
501 /* local QPN:24, rsvd:8 */
502 u32 offset12;
503 /* local EECN:24, rsvd:8 */
504 u32 offset16;
505 /* starting PSN:24 rsvd:8 */
506 u32 offset20;
507 u8 resp_resources;
508 u8 initiator_depth;
509 /* target ACK delay:5, failover accepted:2, end-to-end flow control:1 */
510 u8 offset26;
511 /* RNR retry count:3, SRQ:1, rsvd:5 */
512 u8 offset27;
513 u64 local_ca_guid;
514
515 u8 private_data[IB_CM_REP_PRIVATE_DATA_SIZE];
516
517} __attribute__ ((packed));
518
519static inline u32 cm_rep_get_local_qpn(struct cm_rep_msg *rep_msg)
520{
521 return cpu_to_be32(be32_to_cpu(rep_msg->offset12) >> 8);
522}
523
524static inline void cm_rep_set_local_qpn(struct cm_rep_msg *rep_msg, u32 qpn)
525{
526 rep_msg->offset12 = cpu_to_be32((be32_to_cpu(qpn) << 8) |
527 (be32_to_cpu(rep_msg->offset12) & 0x000000FF));
528}
529
530static inline u32 cm_rep_get_starting_psn(struct cm_rep_msg *rep_msg)
531{
532 return cpu_to_be32(be32_to_cpu(rep_msg->offset20) >> 8);
533}
534
535static inline void cm_rep_set_starting_psn(struct cm_rep_msg *rep_msg,
536 u32 starting_psn)
537{
538 rep_msg->offset20 = cpu_to_be32((be32_to_cpu(starting_psn) << 8) |
539 (be32_to_cpu(rep_msg->offset20) & 0x000000FF));
540}
541
542static inline u8 cm_rep_get_target_ack_delay(struct cm_rep_msg *rep_msg)
543{
544 return (u8) (rep_msg->offset26 >> 3);
545}
546
547static inline void cm_rep_set_target_ack_delay(struct cm_rep_msg *rep_msg,
548 u8 target_ack_delay)
549{
550 rep_msg->offset26 = (u8) ((rep_msg->offset26 & 0x07) |
551 (target_ack_delay << 3));
552}
553
554static inline u8 cm_rep_get_failover(struct cm_rep_msg *rep_msg)
555{
556 return (u8) ((rep_msg->offset26 & 0x06) >> 1);
557}
558
559static inline void cm_rep_set_failover(struct cm_rep_msg *rep_msg, u8 failover)
560{
561 rep_msg->offset26 = (u8) ((rep_msg->offset26 & 0xF9) |
562 ((failover & 0x3) << 1));
563}
564
565static inline u8 cm_rep_get_flow_ctrl(struct cm_rep_msg *rep_msg)
566{
567 return (u8) (rep_msg->offset26 & 0x01);
568}
569
570static inline void cm_rep_set_flow_ctrl(struct cm_rep_msg *rep_msg,
571 u8 flow_ctrl)
572{
573 rep_msg->offset26 = (u8) ((rep_msg->offset26 & 0xFE) |
574 (flow_ctrl & 0x1));
575}
576
577static inline u8 cm_rep_get_rnr_retry_count(struct cm_rep_msg *rep_msg)
578{
579 return (u8) (rep_msg->offset27 >> 5);
580}
581
582static inline void cm_rep_set_rnr_retry_count(struct cm_rep_msg *rep_msg,
583 u8 rnr_retry_count)
584{
585 rep_msg->offset27 = (u8) ((rep_msg->offset27 & 0x1F) |
586 (rnr_retry_count << 5));
587}
588
589static inline u8 cm_rep_get_srq(struct cm_rep_msg *rep_msg)
590{
591 return (u8) ((rep_msg->offset27 >> 4) & 0x1);
592}
593
594static inline void cm_rep_set_srq(struct cm_rep_msg *rep_msg, u8 srq)
595{
596 rep_msg->offset27 = (u8) ((rep_msg->offset27 & 0xEF) |
597 ((srq & 0x1) << 4));
598}
599
600struct cm_rtu_msg {
601 struct ib_mad_hdr hdr;
602
603 u32 local_comm_id;
604 u32 remote_comm_id;
605
606 u8 private_data[IB_CM_RTU_PRIVATE_DATA_SIZE];
607
608} __attribute__ ((packed));
609
610struct cm_dreq_msg {
611 struct ib_mad_hdr hdr;
612
613 u32 local_comm_id;
614 u32 remote_comm_id;
615 /* remote QPN/EECN:24, rsvd:8 */
616 u32 offset8;
617
618 u8 private_data[IB_CM_DREQ_PRIVATE_DATA_SIZE];
619
620} __attribute__ ((packed));
621
622static inline u32 cm_dreq_get_remote_qpn(struct cm_dreq_msg *dreq_msg)
623{
624 return cpu_to_be32(be32_to_cpu(dreq_msg->offset8) >> 8);
625}
626
627static inline void cm_dreq_set_remote_qpn(struct cm_dreq_msg *dreq_msg, u32 qpn)
628{
629 dreq_msg->offset8 = cpu_to_be32((be32_to_cpu(qpn) << 8) |
630 (be32_to_cpu(dreq_msg->offset8) & 0x000000FF));
631}
632
633struct cm_drep_msg {
634 struct ib_mad_hdr hdr;
635
636 u32 local_comm_id;
637 u32 remote_comm_id;
638
639 u8 private_data[IB_CM_DREP_PRIVATE_DATA_SIZE];
640
641} __attribute__ ((packed));
642
643struct cm_lap_msg {
644 struct ib_mad_hdr hdr;
645
646 u32 local_comm_id;
647 u32 remote_comm_id;
648
649 u32 rsvd8;
650 /* remote QPN/EECN:24, remote CM response timeout:5, rsvd:3 */
651 u32 offset12;
652 u32 rsvd16;
653
654 u16 alt_local_lid;
655 u16 alt_remote_lid;
656 union ib_gid alt_local_gid;
657 union ib_gid alt_remote_gid;
658 /* flow label:20, rsvd:4, traffic class:8 */
659 u32 offset56;
660 u8 alt_hop_limit;
661 /* rsvd:2, packet rate:6 */
662 uint8_t offset61;
663 /* SL:4, subnet local:1, rsvd:3 */
664 uint8_t offset62;
665 /* local ACK timeout:5, rsvd:3 */
666 uint8_t offset63;
667
668 u8 private_data[IB_CM_LAP_PRIVATE_DATA_SIZE];
669} __attribute__ ((packed));
670
671static inline u32 cm_lap_get_remote_qpn(struct cm_lap_msg *lap_msg)
672{
673 return cpu_to_be32(be32_to_cpu(lap_msg->offset12) >> 8);
674}
675
676static inline void cm_lap_set_remote_qpn(struct cm_lap_msg *lap_msg, u32 qpn)
677{
678 lap_msg->offset12 = cpu_to_be32((be32_to_cpu(qpn) << 8) |
679 (be32_to_cpu(lap_msg->offset12) &
680 0x000000FF));
681}
682
683static inline u8 cm_lap_get_remote_resp_timeout(struct cm_lap_msg *lap_msg)
684{
685 return (u8) ((be32_to_cpu(lap_msg->offset12) & 0xF8) >> 3);
686}
687
688static inline void cm_lap_set_remote_resp_timeout(struct cm_lap_msg *lap_msg,
689 u8 resp_timeout)
690{
691 lap_msg->offset12 = cpu_to_be32((resp_timeout << 3) |
692 (be32_to_cpu(lap_msg->offset12) &
693 0xFFFFFF07));
694}
695
696static inline u32 cm_lap_get_flow_label(struct cm_lap_msg *lap_msg)
697{
698 return be32_to_cpu(lap_msg->offset56) >> 12;
699}
700
701static inline void cm_lap_set_flow_label(struct cm_lap_msg *lap_msg,
702 u32 flow_label)
703{
704 lap_msg->offset56 = cpu_to_be32((flow_label << 12) |
705 (be32_to_cpu(lap_msg->offset56) &
706 0x00000FFF));
707}
708
709static inline u8 cm_lap_get_traffic_class(struct cm_lap_msg *lap_msg)
710{
711 return (u8) be32_to_cpu(lap_msg->offset56);
712}
713
714static inline void cm_lap_set_traffic_class(struct cm_lap_msg *lap_msg,
715 u8 traffic_class)
716{
717 lap_msg->offset56 = cpu_to_be32(traffic_class |
718 (be32_to_cpu(lap_msg->offset56) &
719 0xFFFFFF00));
720}
721
722static inline u8 cm_lap_get_packet_rate(struct cm_lap_msg *lap_msg)
723{
724 return lap_msg->offset61 & 0x3F;
725}
726
727static inline void cm_lap_set_packet_rate(struct cm_lap_msg *lap_msg,
728 u8 packet_rate)
729{
730 lap_msg->offset61 = (packet_rate & 0x3F) | (lap_msg->offset61 & 0xC0);
731}
732
733static inline u8 cm_lap_get_sl(struct cm_lap_msg *lap_msg)
734{
735 return lap_msg->offset62 >> 4;
736}
737
738static inline void cm_lap_set_sl(struct cm_lap_msg *lap_msg, u8 sl)
739{
740 lap_msg->offset62 = (sl << 4) | (lap_msg->offset62 & 0x0F);
741}
742
743static inline u8 cm_lap_get_subnet_local(struct cm_lap_msg *lap_msg)
744{
745 return (lap_msg->offset62 >> 3) & 0x1;
746}
747
748static inline void cm_lap_set_subnet_local(struct cm_lap_msg *lap_msg,
749 u8 subnet_local)
750{
751 lap_msg->offset62 = ((subnet_local & 0x1) << 3) |
752 (lap_msg->offset61 & 0xF7);
753}
754static inline u8 cm_lap_get_local_ack_timeout(struct cm_lap_msg *lap_msg)
755{
756 return lap_msg->offset63 >> 3;
757}
758
759static inline void cm_lap_set_local_ack_timeout(struct cm_lap_msg *lap_msg,
760 u8 local_ack_timeout)
761{
762 lap_msg->offset63 = (local_ack_timeout << 3) |
763 (lap_msg->offset63 & 0x07);
764}
765
766struct cm_apr_msg {
767 struct ib_mad_hdr hdr;
768
769 u32 local_comm_id;
770 u32 remote_comm_id;
771
772 u8 info_length;
773 u8 ap_status;
774 u8 info[IB_CM_APR_INFO_LENGTH];
775
776 u8 private_data[IB_CM_APR_PRIVATE_DATA_SIZE];
777} __attribute__ ((packed));
778
779struct cm_sidr_req_msg {
780 struct ib_mad_hdr hdr;
781
782 u32 request_id;
783 u16 pkey;
784 u16 rsvd;
785 u64 service_id;
786
787 u8 private_data[IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE];
788} __attribute__ ((packed));
789
790struct cm_sidr_rep_msg {
791 struct ib_mad_hdr hdr;
792
793 u32 request_id;
794 u8 status;
795 u8 info_length;
796 u16 rsvd;
797 /* QPN:24, rsvd:8 */
798 u32 offset8;
799 u64 service_id;
800 u32 qkey;
801 u8 info[IB_CM_SIDR_REP_INFO_LENGTH];
802
803 u8 private_data[IB_CM_SIDR_REP_PRIVATE_DATA_SIZE];
804} __attribute__ ((packed));
805
806static inline u32 cm_sidr_rep_get_qpn(struct cm_sidr_rep_msg *sidr_rep_msg)
807{
808 return cpu_to_be32(be32_to_cpu(sidr_rep_msg->offset8) >> 8);
809}
810
811static inline void cm_sidr_rep_set_qpn(struct cm_sidr_rep_msg *sidr_rep_msg,
812 u32 qpn)
813{
814 sidr_rep_msg->offset8 = cpu_to_be32((be32_to_cpu(qpn) << 8) |
815 (be32_to_cpu(sidr_rep_msg->offset8) &
816 0x000000FF));
817}
818
819#endif /* CM_MSGS_H */
diff --git a/drivers/infiniband/core/fmr_pool.c b/drivers/infiniband/core/fmr_pool.c
index 328feae2a5be..7763b31abba7 100644
--- a/drivers/infiniband/core/fmr_pool.c
+++ b/drivers/infiniband/core/fmr_pool.c
@@ -1,5 +1,6 @@
1/* 1/*
2 * Copyright (c) 2004 Topspin Communications. All rights reserved. 2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
3 * 4 *
4 * This software is available to you under a choice of one of two 5 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU 6 * licenses. You may choose to be licensed under the terms of the GNU
@@ -29,7 +30,7 @@
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE. 31 * SOFTWARE.
31 * 32 *
32 * $Id: fmr_pool.c 1349 2004-12-16 21:09:43Z roland $ 33 * $Id: fmr_pool.c 2730 2005-06-28 16:43:03Z sean.hefty $
33 */ 34 */
34 35
35#include <linux/errno.h> 36#include <linux/errno.h>
@@ -329,7 +330,7 @@ EXPORT_SYMBOL(ib_create_fmr_pool);
329 * 330 *
330 * Destroy an FMR pool and free all associated resources. 331 * Destroy an FMR pool and free all associated resources.
331 */ 332 */
332int ib_destroy_fmr_pool(struct ib_fmr_pool *pool) 333void ib_destroy_fmr_pool(struct ib_fmr_pool *pool)
333{ 334{
334 struct ib_pool_fmr *fmr; 335 struct ib_pool_fmr *fmr;
335 struct ib_pool_fmr *tmp; 336 struct ib_pool_fmr *tmp;
@@ -352,8 +353,6 @@ int ib_destroy_fmr_pool(struct ib_fmr_pool *pool)
352 353
353 kfree(pool->cache_bucket); 354 kfree(pool->cache_bucket);
354 kfree(pool); 355 kfree(pool);
355
356 return 0;
357} 356}
358EXPORT_SYMBOL(ib_destroy_fmr_pool); 357EXPORT_SYMBOL(ib_destroy_fmr_pool);
359 358
diff --git a/drivers/infiniband/core/mad.c b/drivers/infiniband/core/mad.c
index 23628c622a50..b97e210ce9c8 100644
--- a/drivers/infiniband/core/mad.c
+++ b/drivers/infiniband/core/mad.c
@@ -1,5 +1,7 @@
1/* 1/*
2 * Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved. 2 * Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved.
3 * Copyright (c) 2005 Intel Corporation. All rights reserved.
4 * Copyright (c) 2005 Mellanox Technologies Ltd. All rights reserved.
3 * 5 *
4 * This software is available to you under a choice of one of two 6 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU 7 * licenses. You may choose to be licensed under the terms of the GNU
@@ -29,12 +31,12 @@
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE. 32 * SOFTWARE.
31 * 33 *
32 * $Id: mad.c 1389 2004-12-27 22:56:47Z roland $ 34 * $Id: mad.c 2817 2005-07-07 11:29:26Z halr $
33 */ 35 */
34
35#include <linux/dma-mapping.h> 36#include <linux/dma-mapping.h>
36 37
37#include "mad_priv.h" 38#include "mad_priv.h"
39#include "mad_rmpp.h"
38#include "smi.h" 40#include "smi.h"
39#include "agent.h" 41#include "agent.h"
40 42
@@ -45,6 +47,7 @@ MODULE_AUTHOR("Sean Hefty");
45 47
46 48
47kmem_cache_t *ib_mad_cache; 49kmem_cache_t *ib_mad_cache;
50
48static struct list_head ib_mad_port_list; 51static struct list_head ib_mad_port_list;
49static u32 ib_mad_client_id = 0; 52static u32 ib_mad_client_id = 0;
50 53
@@ -58,16 +61,12 @@ static int method_in_use(struct ib_mad_mgmt_method_table **method,
58static void remove_mad_reg_req(struct ib_mad_agent_private *priv); 61static void remove_mad_reg_req(struct ib_mad_agent_private *priv);
59static struct ib_mad_agent_private *find_mad_agent( 62static struct ib_mad_agent_private *find_mad_agent(
60 struct ib_mad_port_private *port_priv, 63 struct ib_mad_port_private *port_priv,
61 struct ib_mad *mad, int solicited); 64 struct ib_mad *mad);
62static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info, 65static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
63 struct ib_mad_private *mad); 66 struct ib_mad_private *mad);
64static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv); 67static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv);
65static void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr,
66 struct ib_mad_send_wc *mad_send_wc);
67static void timeout_sends(void *data); 68static void timeout_sends(void *data);
68static void cancel_sends(void *data);
69static void local_completions(void *data); 69static void local_completions(void *data);
70static int solicited_mad(struct ib_mad *mad);
71static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req, 70static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
72 struct ib_mad_agent_private *agent_priv, 71 struct ib_mad_agent_private *agent_priv,
73 u8 mgmt_class); 72 u8 mgmt_class);
@@ -197,8 +196,8 @@ struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
197 if (qpn == -1) 196 if (qpn == -1)
198 goto error1; 197 goto error1;
199 198
200 if (rmpp_version) 199 if (rmpp_version && rmpp_version != IB_MGMT_RMPP_VERSION)
201 goto error1; /* XXX: until RMPP implemented */ 200 goto error1;
202 201
203 /* Validate MAD registration request if supplied */ 202 /* Validate MAD registration request if supplied */
204 if (mad_reg_req) { 203 if (mad_reg_req) {
@@ -261,22 +260,29 @@ struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
261 ret = ERR_PTR(-ENOMEM); 260 ret = ERR_PTR(-ENOMEM);
262 goto error1; 261 goto error1;
263 } 262 }
263 memset(mad_agent_priv, 0, sizeof *mad_agent_priv);
264
265 mad_agent_priv->agent.mr = ib_get_dma_mr(port_priv->qp_info[qpn].qp->pd,
266 IB_ACCESS_LOCAL_WRITE);
267 if (IS_ERR(mad_agent_priv->agent.mr)) {
268 ret = ERR_PTR(-ENOMEM);
269 goto error2;
270 }
264 271
265 if (mad_reg_req) { 272 if (mad_reg_req) {
266 reg_req = kmalloc(sizeof *reg_req, GFP_KERNEL); 273 reg_req = kmalloc(sizeof *reg_req, GFP_KERNEL);
267 if (!reg_req) { 274 if (!reg_req) {
268 ret = ERR_PTR(-ENOMEM); 275 ret = ERR_PTR(-ENOMEM);
269 goto error2; 276 goto error3;
270 } 277 }
271 /* Make a copy of the MAD registration request */ 278 /* Make a copy of the MAD registration request */
272 memcpy(reg_req, mad_reg_req, sizeof *reg_req); 279 memcpy(reg_req, mad_reg_req, sizeof *reg_req);
273 } 280 }
274 281
275 /* Now, fill in the various structures */ 282 /* Now, fill in the various structures */
276 memset(mad_agent_priv, 0, sizeof *mad_agent_priv);
277 mad_agent_priv->qp_info = &port_priv->qp_info[qpn]; 283 mad_agent_priv->qp_info = &port_priv->qp_info[qpn];
278 mad_agent_priv->reg_req = reg_req; 284 mad_agent_priv->reg_req = reg_req;
279 mad_agent_priv->rmpp_version = rmpp_version; 285 mad_agent_priv->agent.rmpp_version = rmpp_version;
280 mad_agent_priv->agent.device = device; 286 mad_agent_priv->agent.device = device;
281 mad_agent_priv->agent.recv_handler = recv_handler; 287 mad_agent_priv->agent.recv_handler = recv_handler;
282 mad_agent_priv->agent.send_handler = send_handler; 288 mad_agent_priv->agent.send_handler = send_handler;
@@ -301,7 +307,7 @@ struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
301 if (method) { 307 if (method) {
302 if (method_in_use(&method, 308 if (method_in_use(&method,
303 mad_reg_req)) 309 mad_reg_req))
304 goto error3; 310 goto error4;
305 } 311 }
306 } 312 }
307 ret2 = add_nonoui_reg_req(mad_reg_req, mad_agent_priv, 313 ret2 = add_nonoui_reg_req(mad_reg_req, mad_agent_priv,
@@ -317,14 +323,14 @@ struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
317 if (is_vendor_method_in_use( 323 if (is_vendor_method_in_use(
318 vendor_class, 324 vendor_class,
319 mad_reg_req)) 325 mad_reg_req))
320 goto error3; 326 goto error4;
321 } 327 }
322 } 328 }
323 ret2 = add_oui_reg_req(mad_reg_req, mad_agent_priv); 329 ret2 = add_oui_reg_req(mad_reg_req, mad_agent_priv);
324 } 330 }
325 if (ret2) { 331 if (ret2) {
326 ret = ERR_PTR(ret2); 332 ret = ERR_PTR(ret2);
327 goto error3; 333 goto error4;
328 } 334 }
329 } 335 }
330 336
@@ -335,22 +341,24 @@ struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
335 spin_lock_init(&mad_agent_priv->lock); 341 spin_lock_init(&mad_agent_priv->lock);
336 INIT_LIST_HEAD(&mad_agent_priv->send_list); 342 INIT_LIST_HEAD(&mad_agent_priv->send_list);
337 INIT_LIST_HEAD(&mad_agent_priv->wait_list); 343 INIT_LIST_HEAD(&mad_agent_priv->wait_list);
344 INIT_LIST_HEAD(&mad_agent_priv->done_list);
345 INIT_LIST_HEAD(&mad_agent_priv->rmpp_list);
338 INIT_WORK(&mad_agent_priv->timed_work, timeout_sends, mad_agent_priv); 346 INIT_WORK(&mad_agent_priv->timed_work, timeout_sends, mad_agent_priv);
339 INIT_LIST_HEAD(&mad_agent_priv->local_list); 347 INIT_LIST_HEAD(&mad_agent_priv->local_list);
340 INIT_WORK(&mad_agent_priv->local_work, local_completions, 348 INIT_WORK(&mad_agent_priv->local_work, local_completions,
341 mad_agent_priv); 349 mad_agent_priv);
342 INIT_LIST_HEAD(&mad_agent_priv->canceled_list);
343 INIT_WORK(&mad_agent_priv->canceled_work, cancel_sends, mad_agent_priv);
344 atomic_set(&mad_agent_priv->refcount, 1); 350 atomic_set(&mad_agent_priv->refcount, 1);
345 init_waitqueue_head(&mad_agent_priv->wait); 351 init_waitqueue_head(&mad_agent_priv->wait);
346 352
347 return &mad_agent_priv->agent; 353 return &mad_agent_priv->agent;
348 354
349error3: 355error4:
350 spin_unlock_irqrestore(&port_priv->reg_lock, flags); 356 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
351 kfree(reg_req); 357 kfree(reg_req);
352error2: 358error3:
353 kfree(mad_agent_priv); 359 kfree(mad_agent_priv);
360error2:
361 ib_dereg_mr(mad_agent_priv->agent.mr);
354error1: 362error1:
355 return ret; 363 return ret;
356} 364}
@@ -487,18 +495,16 @@ static void unregister_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
487 * MADs, preventing us from queuing additional work 495 * MADs, preventing us from queuing additional work
488 */ 496 */
489 cancel_mads(mad_agent_priv); 497 cancel_mads(mad_agent_priv);
490
491 port_priv = mad_agent_priv->qp_info->port_priv; 498 port_priv = mad_agent_priv->qp_info->port_priv;
492
493 cancel_delayed_work(&mad_agent_priv->timed_work); 499 cancel_delayed_work(&mad_agent_priv->timed_work);
494 flush_workqueue(port_priv->wq);
495 500
496 spin_lock_irqsave(&port_priv->reg_lock, flags); 501 spin_lock_irqsave(&port_priv->reg_lock, flags);
497 remove_mad_reg_req(mad_agent_priv); 502 remove_mad_reg_req(mad_agent_priv);
498 list_del(&mad_agent_priv->agent_list); 503 list_del(&mad_agent_priv->agent_list);
499 spin_unlock_irqrestore(&port_priv->reg_lock, flags); 504 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
500 505
501 /* XXX: Cleanup pending RMPP receives for this agent */ 506 flush_workqueue(port_priv->wq);
507 ib_cancel_rmpp_recvs(mad_agent_priv);
502 508
503 atomic_dec(&mad_agent_priv->refcount); 509 atomic_dec(&mad_agent_priv->refcount);
504 wait_event(mad_agent_priv->wait, 510 wait_event(mad_agent_priv->wait,
@@ -506,6 +512,7 @@ static void unregister_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
506 512
507 if (mad_agent_priv->reg_req) 513 if (mad_agent_priv->reg_req)
508 kfree(mad_agent_priv->reg_req); 514 kfree(mad_agent_priv->reg_req);
515 ib_dereg_mr(mad_agent_priv->agent.mr);
509 kfree(mad_agent_priv); 516 kfree(mad_agent_priv);
510} 517}
511 518
@@ -551,6 +558,13 @@ int ib_unregister_mad_agent(struct ib_mad_agent *mad_agent)
551} 558}
552EXPORT_SYMBOL(ib_unregister_mad_agent); 559EXPORT_SYMBOL(ib_unregister_mad_agent);
553 560
561static inline int response_mad(struct ib_mad *mad)
562{
563 /* Trap represses are responses although response bit is reset */
564 return ((mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS) ||
565 (mad->mad_hdr.method & IB_MGMT_METHOD_RESP));
566}
567
554static void dequeue_mad(struct ib_mad_list_head *mad_list) 568static void dequeue_mad(struct ib_mad_list_head *mad_list)
555{ 569{
556 struct ib_mad_queue *mad_queue; 570 struct ib_mad_queue *mad_queue;
@@ -643,7 +657,7 @@ static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
643 struct ib_smp *smp, 657 struct ib_smp *smp,
644 struct ib_send_wr *send_wr) 658 struct ib_send_wr *send_wr)
645{ 659{
646 int ret, solicited; 660 int ret;
647 unsigned long flags; 661 unsigned long flags;
648 struct ib_mad_local_private *local; 662 struct ib_mad_local_private *local;
649 struct ib_mad_private *mad_priv; 663 struct ib_mad_private *mad_priv;
@@ -689,11 +703,7 @@ static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
689 switch (ret) 703 switch (ret)
690 { 704 {
691 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY: 705 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY:
692 /* 706 if (response_mad(&mad_priv->mad.mad) &&
693 * See if response is solicited and
694 * there is a recv handler
695 */
696 if (solicited_mad(&mad_priv->mad.mad) &&
697 mad_agent_priv->agent.recv_handler) { 707 mad_agent_priv->agent.recv_handler) {
698 local->mad_priv = mad_priv; 708 local->mad_priv = mad_priv;
699 local->recv_mad_agent = mad_agent_priv; 709 local->recv_mad_agent = mad_agent_priv;
@@ -710,15 +720,13 @@ static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
710 break; 720 break;
711 case IB_MAD_RESULT_SUCCESS: 721 case IB_MAD_RESULT_SUCCESS:
712 /* Treat like an incoming receive MAD */ 722 /* Treat like an incoming receive MAD */
713 solicited = solicited_mad(&mad_priv->mad.mad);
714 port_priv = ib_get_mad_port(mad_agent_priv->agent.device, 723 port_priv = ib_get_mad_port(mad_agent_priv->agent.device,
715 mad_agent_priv->agent.port_num); 724 mad_agent_priv->agent.port_num);
716 if (port_priv) { 725 if (port_priv) {
717 mad_priv->mad.mad.mad_hdr.tid = 726 mad_priv->mad.mad.mad_hdr.tid =
718 ((struct ib_mad *)smp)->mad_hdr.tid; 727 ((struct ib_mad *)smp)->mad_hdr.tid;
719 recv_mad_agent = find_mad_agent(port_priv, 728 recv_mad_agent = find_mad_agent(port_priv,
720 &mad_priv->mad.mad, 729 &mad_priv->mad.mad);
721 solicited);
722 } 730 }
723 if (!port_priv || !recv_mad_agent) { 731 if (!port_priv || !recv_mad_agent) {
724 kmem_cache_free(ib_mad_cache, mad_priv); 732 kmem_cache_free(ib_mad_cache, mad_priv);
@@ -750,43 +758,133 @@ static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
750 list_add_tail(&local->completion_list, &mad_agent_priv->local_list); 758 list_add_tail(&local->completion_list, &mad_agent_priv->local_list);
751 spin_unlock_irqrestore(&mad_agent_priv->lock, flags); 759 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
752 queue_work(mad_agent_priv->qp_info->port_priv->wq, 760 queue_work(mad_agent_priv->qp_info->port_priv->wq,
753 &mad_agent_priv->local_work); 761 &mad_agent_priv->local_work);
754 ret = 1; 762 ret = 1;
755out: 763out:
756 return ret; 764 return ret;
757} 765}
758 766
759static int ib_send_mad(struct ib_mad_agent_private *mad_agent_priv, 767static int get_buf_length(int hdr_len, int data_len)
760 struct ib_mad_send_wr_private *mad_send_wr) 768{
769 int seg_size, pad;
770
771 seg_size = sizeof(struct ib_mad) - hdr_len;
772 if (data_len && seg_size) {
773 pad = seg_size - data_len % seg_size;
774 if (pad == seg_size)
775 pad = 0;
776 } else
777 pad = seg_size;
778 return hdr_len + data_len + pad;
779}
780
781struct ib_mad_send_buf * ib_create_send_mad(struct ib_mad_agent *mad_agent,
782 u32 remote_qpn, u16 pkey_index,
783 struct ib_ah *ah, int rmpp_active,
784 int hdr_len, int data_len,
785 unsigned int __nocast gfp_mask)
786{
787 struct ib_mad_agent_private *mad_agent_priv;
788 struct ib_mad_send_buf *send_buf;
789 int buf_size;
790 void *buf;
791
792 mad_agent_priv = container_of(mad_agent,
793 struct ib_mad_agent_private, agent);
794 buf_size = get_buf_length(hdr_len, data_len);
795
796 if ((!mad_agent->rmpp_version &&
797 (rmpp_active || buf_size > sizeof(struct ib_mad))) ||
798 (!rmpp_active && buf_size > sizeof(struct ib_mad)))
799 return ERR_PTR(-EINVAL);
800
801 buf = kmalloc(sizeof *send_buf + buf_size, gfp_mask);
802 if (!buf)
803 return ERR_PTR(-ENOMEM);
804 memset(buf, 0, sizeof *send_buf + buf_size);
805
806 send_buf = buf + buf_size;
807 send_buf->mad = buf;
808
809 send_buf->sge.addr = dma_map_single(mad_agent->device->dma_device,
810 buf, buf_size, DMA_TO_DEVICE);
811 pci_unmap_addr_set(send_buf, mapping, send_buf->sge.addr);
812 send_buf->sge.length = buf_size;
813 send_buf->sge.lkey = mad_agent->mr->lkey;
814
815 send_buf->send_wr.wr_id = (unsigned long) send_buf;
816 send_buf->send_wr.sg_list = &send_buf->sge;
817 send_buf->send_wr.num_sge = 1;
818 send_buf->send_wr.opcode = IB_WR_SEND;
819 send_buf->send_wr.send_flags = IB_SEND_SIGNALED;
820 send_buf->send_wr.wr.ud.ah = ah;
821 send_buf->send_wr.wr.ud.mad_hdr = &send_buf->mad->mad_hdr;
822 send_buf->send_wr.wr.ud.remote_qpn = remote_qpn;
823 send_buf->send_wr.wr.ud.remote_qkey = IB_QP_SET_QKEY;
824 send_buf->send_wr.wr.ud.pkey_index = pkey_index;
825
826 if (rmpp_active) {
827 struct ib_rmpp_mad *rmpp_mad;
828 rmpp_mad = (struct ib_rmpp_mad *)send_buf->mad;
829 rmpp_mad->rmpp_hdr.paylen_newwin = cpu_to_be32(hdr_len -
830 offsetof(struct ib_rmpp_mad, data) + data_len);
831 rmpp_mad->rmpp_hdr.rmpp_version = mad_agent->rmpp_version;
832 rmpp_mad->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_DATA;
833 ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr,
834 IB_MGMT_RMPP_FLAG_ACTIVE);
835 }
836
837 send_buf->mad_agent = mad_agent;
838 atomic_inc(&mad_agent_priv->refcount);
839 return send_buf;
840}
841EXPORT_SYMBOL(ib_create_send_mad);
842
843void ib_free_send_mad(struct ib_mad_send_buf *send_buf)
844{
845 struct ib_mad_agent_private *mad_agent_priv;
846
847 mad_agent_priv = container_of(send_buf->mad_agent,
848 struct ib_mad_agent_private, agent);
849
850 dma_unmap_single(send_buf->mad_agent->device->dma_device,
851 pci_unmap_addr(send_buf, mapping),
852 send_buf->sge.length, DMA_TO_DEVICE);
853 kfree(send_buf->mad);
854
855 if (atomic_dec_and_test(&mad_agent_priv->refcount))
856 wake_up(&mad_agent_priv->wait);
857}
858EXPORT_SYMBOL(ib_free_send_mad);
859
860int ib_send_mad(struct ib_mad_send_wr_private *mad_send_wr)
761{ 861{
762 struct ib_mad_qp_info *qp_info; 862 struct ib_mad_qp_info *qp_info;
763 struct ib_send_wr *bad_send_wr; 863 struct ib_send_wr *bad_send_wr;
864 struct list_head *list;
764 unsigned long flags; 865 unsigned long flags;
765 int ret; 866 int ret;
766 867
767 /* Replace user's WR ID with our own to find WR upon completion */ 868 /* Set WR ID to find mad_send_wr upon completion */
768 qp_info = mad_agent_priv->qp_info; 869 qp_info = mad_send_wr->mad_agent_priv->qp_info;
769 mad_send_wr->wr_id = mad_send_wr->send_wr.wr_id;
770 mad_send_wr->send_wr.wr_id = (unsigned long)&mad_send_wr->mad_list; 870 mad_send_wr->send_wr.wr_id = (unsigned long)&mad_send_wr->mad_list;
771 mad_send_wr->mad_list.mad_queue = &qp_info->send_queue; 871 mad_send_wr->mad_list.mad_queue = &qp_info->send_queue;
772 872
773 spin_lock_irqsave(&qp_info->send_queue.lock, flags); 873 spin_lock_irqsave(&qp_info->send_queue.lock, flags);
774 if (qp_info->send_queue.count++ < qp_info->send_queue.max_active) { 874 if (qp_info->send_queue.count < qp_info->send_queue.max_active) {
775 list_add_tail(&mad_send_wr->mad_list.list, 875 ret = ib_post_send(mad_send_wr->mad_agent_priv->agent.qp,
776 &qp_info->send_queue.list);
777 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
778 ret = ib_post_send(mad_agent_priv->agent.qp,
779 &mad_send_wr->send_wr, &bad_send_wr); 876 &mad_send_wr->send_wr, &bad_send_wr);
780 if (ret) { 877 list = &qp_info->send_queue.list;
781 printk(KERN_ERR PFX "ib_post_send failed: %d\n", ret);
782 dequeue_mad(&mad_send_wr->mad_list);
783 }
784 } else { 878 } else {
785 list_add_tail(&mad_send_wr->mad_list.list,
786 &qp_info->overflow_list);
787 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
788 ret = 0; 879 ret = 0;
880 list = &qp_info->overflow_list;
789 } 881 }
882
883 if (!ret) {
884 qp_info->send_queue.count++;
885 list_add_tail(&mad_send_wr->mad_list.list, list);
886 }
887 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
790 return ret; 888 return ret;
791} 889}
792 890
@@ -860,18 +958,19 @@ int ib_post_send_mad(struct ib_mad_agent *mad_agent,
860 ret = -ENOMEM; 958 ret = -ENOMEM;
861 goto error2; 959 goto error2;
862 } 960 }
961 memset(mad_send_wr, 0, sizeof *mad_send_wr);
863 962
864 mad_send_wr->send_wr = *send_wr; 963 mad_send_wr->send_wr = *send_wr;
865 mad_send_wr->send_wr.sg_list = mad_send_wr->sg_list; 964 mad_send_wr->send_wr.sg_list = mad_send_wr->sg_list;
866 memcpy(mad_send_wr->sg_list, send_wr->sg_list, 965 memcpy(mad_send_wr->sg_list, send_wr->sg_list,
867 sizeof *send_wr->sg_list * send_wr->num_sge); 966 sizeof *send_wr->sg_list * send_wr->num_sge);
868 mad_send_wr->send_wr.next = NULL; 967 mad_send_wr->wr_id = send_wr->wr_id;
869 mad_send_wr->tid = send_wr->wr.ud.mad_hdr->tid; 968 mad_send_wr->tid = send_wr->wr.ud.mad_hdr->tid;
870 mad_send_wr->agent = mad_agent; 969 mad_send_wr->mad_agent_priv = mad_agent_priv;
871 /* Timeout will be updated after send completes */ 970 /* Timeout will be updated after send completes */
872 mad_send_wr->timeout = msecs_to_jiffies(send_wr->wr. 971 mad_send_wr->timeout = msecs_to_jiffies(send_wr->wr.
873 ud.timeout_ms); 972 ud.timeout_ms);
874 mad_send_wr->retry = 0; 973 mad_send_wr->retries = mad_send_wr->send_wr.wr.ud.retries;
875 /* One reference for each work request to QP + response */ 974 /* One reference for each work request to QP + response */
876 mad_send_wr->refcount = 1 + (mad_send_wr->timeout > 0); 975 mad_send_wr->refcount = 1 + (mad_send_wr->timeout > 0);
877 mad_send_wr->status = IB_WC_SUCCESS; 976 mad_send_wr->status = IB_WC_SUCCESS;
@@ -883,8 +982,13 @@ int ib_post_send_mad(struct ib_mad_agent *mad_agent,
883 &mad_agent_priv->send_list); 982 &mad_agent_priv->send_list);
884 spin_unlock_irqrestore(&mad_agent_priv->lock, flags); 983 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
885 984
886 ret = ib_send_mad(mad_agent_priv, mad_send_wr); 985 if (mad_agent_priv->agent.rmpp_version) {
887 if (ret) { 986 ret = ib_send_rmpp_mad(mad_send_wr);
987 if (ret >= 0 && ret != IB_RMPP_RESULT_CONSUMED)
988 ret = ib_send_mad(mad_send_wr);
989 } else
990 ret = ib_send_mad(mad_send_wr);
991 if (ret < 0) {
888 /* Fail send request */ 992 /* Fail send request */
889 spin_lock_irqsave(&mad_agent_priv->lock, flags); 993 spin_lock_irqsave(&mad_agent_priv->lock, flags);
890 list_del(&mad_send_wr->agent_list); 994 list_del(&mad_send_wr->agent_list);
@@ -910,41 +1014,28 @@ EXPORT_SYMBOL(ib_post_send_mad);
910 */ 1014 */
911void ib_free_recv_mad(struct ib_mad_recv_wc *mad_recv_wc) 1015void ib_free_recv_mad(struct ib_mad_recv_wc *mad_recv_wc)
912{ 1016{
913 struct ib_mad_recv_buf *entry; 1017 struct ib_mad_recv_buf *mad_recv_buf, *temp_recv_buf;
914 struct ib_mad_private_header *mad_priv_hdr; 1018 struct ib_mad_private_header *mad_priv_hdr;
915 struct ib_mad_private *priv; 1019 struct ib_mad_private *priv;
1020 struct list_head free_list;
916 1021
917 mad_priv_hdr = container_of(mad_recv_wc, 1022 INIT_LIST_HEAD(&free_list);
918 struct ib_mad_private_header, 1023 list_splice_init(&mad_recv_wc->rmpp_list, &free_list);
919 recv_wc);
920 priv = container_of(mad_priv_hdr, struct ib_mad_private, header);
921 1024
922 /* 1025 list_for_each_entry_safe(mad_recv_buf, temp_recv_buf,
923 * Walk receive buffer list associated with this WC 1026 &free_list, list) {
924 * No need to remove them from list of receive buffers 1027 mad_recv_wc = container_of(mad_recv_buf, struct ib_mad_recv_wc,
925 */ 1028 recv_buf);
926 list_for_each_entry(entry, &mad_recv_wc->recv_buf.list, list) {
927 /* Free previous receive buffer */
928 kmem_cache_free(ib_mad_cache, priv);
929 mad_priv_hdr = container_of(mad_recv_wc, 1029 mad_priv_hdr = container_of(mad_recv_wc,
930 struct ib_mad_private_header, 1030 struct ib_mad_private_header,
931 recv_wc); 1031 recv_wc);
932 priv = container_of(mad_priv_hdr, struct ib_mad_private, 1032 priv = container_of(mad_priv_hdr, struct ib_mad_private,
933 header); 1033 header);
1034 kmem_cache_free(ib_mad_cache, priv);
934 } 1035 }
935
936 /* Free last buffer */
937 kmem_cache_free(ib_mad_cache, priv);
938} 1036}
939EXPORT_SYMBOL(ib_free_recv_mad); 1037EXPORT_SYMBOL(ib_free_recv_mad);
940 1038
941void ib_coalesce_recv_mad(struct ib_mad_recv_wc *mad_recv_wc,
942 void *buf)
943{
944 printk(KERN_ERR PFX "ib_coalesce_recv_mad() not implemented yet\n");
945}
946EXPORT_SYMBOL(ib_coalesce_recv_mad);
947
948struct ib_mad_agent *ib_redirect_mad_qp(struct ib_qp *qp, 1039struct ib_mad_agent *ib_redirect_mad_qp(struct ib_qp *qp,
949 u8 rmpp_version, 1040 u8 rmpp_version,
950 ib_mad_send_handler send_handler, 1041 ib_mad_send_handler send_handler,
@@ -1338,42 +1429,15 @@ out:
1338 return; 1429 return;
1339} 1430}
1340 1431
1341static int response_mad(struct ib_mad *mad)
1342{
1343 /* Trap represses are responses although response bit is reset */
1344 return ((mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS) ||
1345 (mad->mad_hdr.method & IB_MGMT_METHOD_RESP));
1346}
1347
1348static int solicited_mad(struct ib_mad *mad)
1349{
1350 /* CM MADs are never solicited */
1351 if (mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_CM) {
1352 return 0;
1353 }
1354
1355 /* XXX: Determine whether MAD is using RMPP */
1356
1357 /* Not using RMPP */
1358 /* Is this MAD a response to a previous MAD ? */
1359 return response_mad(mad);
1360}
1361
1362static struct ib_mad_agent_private * 1432static struct ib_mad_agent_private *
1363find_mad_agent(struct ib_mad_port_private *port_priv, 1433find_mad_agent(struct ib_mad_port_private *port_priv,
1364 struct ib_mad *mad, 1434 struct ib_mad *mad)
1365 int solicited)
1366{ 1435{
1367 struct ib_mad_agent_private *mad_agent = NULL; 1436 struct ib_mad_agent_private *mad_agent = NULL;
1368 unsigned long flags; 1437 unsigned long flags;
1369 1438
1370 spin_lock_irqsave(&port_priv->reg_lock, flags); 1439 spin_lock_irqsave(&port_priv->reg_lock, flags);
1371 1440 if (response_mad(mad)) {
1372 /*
1373 * Whether MAD was solicited determines type of routing to
1374 * MAD client.
1375 */
1376 if (solicited) {
1377 u32 hi_tid; 1441 u32 hi_tid;
1378 struct ib_mad_agent_private *entry; 1442 struct ib_mad_agent_private *entry;
1379 1443
@@ -1477,21 +1541,20 @@ out:
1477 return valid; 1541 return valid;
1478} 1542}
1479 1543
1480/* 1544static int is_data_mad(struct ib_mad_agent_private *mad_agent_priv,
1481 * Return start of fully reassembled MAD, or NULL, if MAD isn't assembled yet 1545 struct ib_mad_hdr *mad_hdr)
1482 */
1483static struct ib_mad_private *
1484reassemble_recv(struct ib_mad_agent_private *mad_agent_priv,
1485 struct ib_mad_private *recv)
1486{ 1546{
1487 /* Until we have RMPP, all receives are reassembled!... */ 1547 struct ib_rmpp_mad *rmpp_mad;
1488 INIT_LIST_HEAD(&recv->header.recv_wc.recv_buf.list); 1548
1489 return recv; 1549 rmpp_mad = (struct ib_rmpp_mad *)mad_hdr;
1550 return !mad_agent_priv->agent.rmpp_version ||
1551 !(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
1552 IB_MGMT_RMPP_FLAG_ACTIVE) ||
1553 (rmpp_mad->rmpp_hdr.rmpp_type == IB_MGMT_RMPP_TYPE_DATA);
1490} 1554}
1491 1555
1492static struct ib_mad_send_wr_private* 1556struct ib_mad_send_wr_private*
1493find_send_req(struct ib_mad_agent_private *mad_agent_priv, 1557ib_find_send_mad(struct ib_mad_agent_private *mad_agent_priv, u64 tid)
1494 u64 tid)
1495{ 1558{
1496 struct ib_mad_send_wr_private *mad_send_wr; 1559 struct ib_mad_send_wr_private *mad_send_wr;
1497 1560
@@ -1507,7 +1570,9 @@ find_send_req(struct ib_mad_agent_private *mad_agent_priv,
1507 */ 1570 */
1508 list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list, 1571 list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list,
1509 agent_list) { 1572 agent_list) {
1510 if (mad_send_wr->tid == tid && mad_send_wr->timeout) { 1573 if (is_data_mad(mad_agent_priv,
1574 mad_send_wr->send_wr.wr.ud.mad_hdr) &&
1575 mad_send_wr->tid == tid && mad_send_wr->timeout) {
1511 /* Verify request has not been canceled */ 1576 /* Verify request has not been canceled */
1512 return (mad_send_wr->status == IB_WC_SUCCESS) ? 1577 return (mad_send_wr->status == IB_WC_SUCCESS) ?
1513 mad_send_wr : NULL; 1578 mad_send_wr : NULL;
@@ -1516,43 +1581,55 @@ find_send_req(struct ib_mad_agent_private *mad_agent_priv,
1516 return NULL; 1581 return NULL;
1517} 1582}
1518 1583
1584void ib_mark_mad_done(struct ib_mad_send_wr_private *mad_send_wr)
1585{
1586 mad_send_wr->timeout = 0;
1587 if (mad_send_wr->refcount == 1) {
1588 list_del(&mad_send_wr->agent_list);
1589 list_add_tail(&mad_send_wr->agent_list,
1590 &mad_send_wr->mad_agent_priv->done_list);
1591 }
1592}
1593
1519static void ib_mad_complete_recv(struct ib_mad_agent_private *mad_agent_priv, 1594static void ib_mad_complete_recv(struct ib_mad_agent_private *mad_agent_priv,
1520 struct ib_mad_private *recv, 1595 struct ib_mad_recv_wc *mad_recv_wc)
1521 int solicited)
1522{ 1596{
1523 struct ib_mad_send_wr_private *mad_send_wr; 1597 struct ib_mad_send_wr_private *mad_send_wr;
1524 struct ib_mad_send_wc mad_send_wc; 1598 struct ib_mad_send_wc mad_send_wc;
1525 unsigned long flags; 1599 unsigned long flags;
1526 1600 u64 tid;
1527 /* Fully reassemble receive before processing */ 1601
1528 recv = reassemble_recv(mad_agent_priv, recv); 1602 INIT_LIST_HEAD(&mad_recv_wc->rmpp_list);
1529 if (!recv) { 1603 list_add(&mad_recv_wc->recv_buf.list, &mad_recv_wc->rmpp_list);
1530 if (atomic_dec_and_test(&mad_agent_priv->refcount)) 1604 if (mad_agent_priv->agent.rmpp_version) {
1531 wake_up(&mad_agent_priv->wait); 1605 mad_recv_wc = ib_process_rmpp_recv_wc(mad_agent_priv,
1532 return; 1606 mad_recv_wc);
1607 if (!mad_recv_wc) {
1608 if (atomic_dec_and_test(&mad_agent_priv->refcount))
1609 wake_up(&mad_agent_priv->wait);
1610 return;
1611 }
1533 } 1612 }
1534 1613
1535 /* Complete corresponding request */ 1614 /* Complete corresponding request */
1536 if (solicited) { 1615 if (response_mad(mad_recv_wc->recv_buf.mad)) {
1616 tid = mad_recv_wc->recv_buf.mad->mad_hdr.tid;
1537 spin_lock_irqsave(&mad_agent_priv->lock, flags); 1617 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1538 mad_send_wr = find_send_req(mad_agent_priv, 1618 mad_send_wr = ib_find_send_mad(mad_agent_priv, tid);
1539 recv->mad.mad.mad_hdr.tid);
1540 if (!mad_send_wr) { 1619 if (!mad_send_wr) {
1541 spin_unlock_irqrestore(&mad_agent_priv->lock, flags); 1620 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1542 ib_free_recv_mad(&recv->header.recv_wc); 1621 ib_free_recv_mad(mad_recv_wc);
1543 if (atomic_dec_and_test(&mad_agent_priv->refcount)) 1622 if (atomic_dec_and_test(&mad_agent_priv->refcount))
1544 wake_up(&mad_agent_priv->wait); 1623 wake_up(&mad_agent_priv->wait);
1545 return; 1624 return;
1546 } 1625 }
1547 /* Timeout = 0 means that we won't wait for a response */ 1626 ib_mark_mad_done(mad_send_wr);
1548 mad_send_wr->timeout = 0;
1549 spin_unlock_irqrestore(&mad_agent_priv->lock, flags); 1627 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1550 1628
1551 /* Defined behavior is to complete response before request */ 1629 /* Defined behavior is to complete response before request */
1552 recv->header.recv_wc.wc->wr_id = mad_send_wr->wr_id; 1630 mad_recv_wc->wc->wr_id = mad_send_wr->wr_id;
1553 mad_agent_priv->agent.recv_handler( 1631 mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent,
1554 &mad_agent_priv->agent, 1632 mad_recv_wc);
1555 &recv->header.recv_wc);
1556 atomic_dec(&mad_agent_priv->refcount); 1633 atomic_dec(&mad_agent_priv->refcount);
1557 1634
1558 mad_send_wc.status = IB_WC_SUCCESS; 1635 mad_send_wc.status = IB_WC_SUCCESS;
@@ -1560,9 +1637,8 @@ static void ib_mad_complete_recv(struct ib_mad_agent_private *mad_agent_priv,
1560 mad_send_wc.wr_id = mad_send_wr->wr_id; 1637 mad_send_wc.wr_id = mad_send_wr->wr_id;
1561 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc); 1638 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
1562 } else { 1639 } else {
1563 mad_agent_priv->agent.recv_handler( 1640 mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent,
1564 &mad_agent_priv->agent, 1641 mad_recv_wc);
1565 &recv->header.recv_wc);
1566 if (atomic_dec_and_test(&mad_agent_priv->refcount)) 1642 if (atomic_dec_and_test(&mad_agent_priv->refcount))
1567 wake_up(&mad_agent_priv->wait); 1643 wake_up(&mad_agent_priv->wait);
1568 } 1644 }
@@ -1576,7 +1652,6 @@ static void ib_mad_recv_done_handler(struct ib_mad_port_private *port_priv,
1576 struct ib_mad_private *recv, *response; 1652 struct ib_mad_private *recv, *response;
1577 struct ib_mad_list_head *mad_list; 1653 struct ib_mad_list_head *mad_list;
1578 struct ib_mad_agent_private *mad_agent; 1654 struct ib_mad_agent_private *mad_agent;
1579 int solicited;
1580 1655
1581 response = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL); 1656 response = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL);
1582 if (!response) 1657 if (!response)
@@ -1662,11 +1737,9 @@ local:
1662 } 1737 }
1663 } 1738 }
1664 1739
1665 /* Determine corresponding MAD agent for incoming receive MAD */ 1740 mad_agent = find_mad_agent(port_priv, &recv->mad.mad);
1666 solicited = solicited_mad(&recv->mad.mad);
1667 mad_agent = find_mad_agent(port_priv, &recv->mad.mad, solicited);
1668 if (mad_agent) { 1741 if (mad_agent) {
1669 ib_mad_complete_recv(mad_agent, recv, solicited); 1742 ib_mad_complete_recv(mad_agent, &recv->header.recv_wc);
1670 /* 1743 /*
1671 * recv is freed up in error cases in ib_mad_complete_recv 1744 * recv is freed up in error cases in ib_mad_complete_recv
1672 * or via recv_handler in ib_mad_complete_recv() 1745 * or via recv_handler in ib_mad_complete_recv()
@@ -1710,26 +1783,31 @@ static void adjust_timeout(struct ib_mad_agent_private *mad_agent_priv)
1710 } 1783 }
1711} 1784}
1712 1785
1713static void wait_for_response(struct ib_mad_agent_private *mad_agent_priv, 1786static void wait_for_response(struct ib_mad_send_wr_private *mad_send_wr)
1714 struct ib_mad_send_wr_private *mad_send_wr )
1715{ 1787{
1788 struct ib_mad_agent_private *mad_agent_priv;
1716 struct ib_mad_send_wr_private *temp_mad_send_wr; 1789 struct ib_mad_send_wr_private *temp_mad_send_wr;
1717 struct list_head *list_item; 1790 struct list_head *list_item;
1718 unsigned long delay; 1791 unsigned long delay;
1719 1792
1793 mad_agent_priv = mad_send_wr->mad_agent_priv;
1720 list_del(&mad_send_wr->agent_list); 1794 list_del(&mad_send_wr->agent_list);
1721 1795
1722 delay = mad_send_wr->timeout; 1796 delay = mad_send_wr->timeout;
1723 mad_send_wr->timeout += jiffies; 1797 mad_send_wr->timeout += jiffies;
1724 1798
1725 list_for_each_prev(list_item, &mad_agent_priv->wait_list) { 1799 if (delay) {
1726 temp_mad_send_wr = list_entry(list_item, 1800 list_for_each_prev(list_item, &mad_agent_priv->wait_list) {
1727 struct ib_mad_send_wr_private, 1801 temp_mad_send_wr = list_entry(list_item,
1728 agent_list); 1802 struct ib_mad_send_wr_private,
1729 if (time_after(mad_send_wr->timeout, 1803 agent_list);
1730 temp_mad_send_wr->timeout)) 1804 if (time_after(mad_send_wr->timeout,
1731 break; 1805 temp_mad_send_wr->timeout))
1806 break;
1807 }
1732 } 1808 }
1809 else
1810 list_item = &mad_agent_priv->wait_list;
1733 list_add(&mad_send_wr->agent_list, list_item); 1811 list_add(&mad_send_wr->agent_list, list_item);
1734 1812
1735 /* Reschedule a work item if we have a shorter timeout */ 1813 /* Reschedule a work item if we have a shorter timeout */
@@ -1740,19 +1818,32 @@ static void wait_for_response(struct ib_mad_agent_private *mad_agent_priv,
1740 } 1818 }
1741} 1819}
1742 1820
1821void ib_reset_mad_timeout(struct ib_mad_send_wr_private *mad_send_wr,
1822 int timeout_ms)
1823{
1824 mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
1825 wait_for_response(mad_send_wr);
1826}
1827
1743/* 1828/*
1744 * Process a send work completion 1829 * Process a send work completion
1745 */ 1830 */
1746static void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr, 1831void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr,
1747 struct ib_mad_send_wc *mad_send_wc) 1832 struct ib_mad_send_wc *mad_send_wc)
1748{ 1833{
1749 struct ib_mad_agent_private *mad_agent_priv; 1834 struct ib_mad_agent_private *mad_agent_priv;
1750 unsigned long flags; 1835 unsigned long flags;
1836 int ret;
1751 1837
1752 mad_agent_priv = container_of(mad_send_wr->agent, 1838 mad_agent_priv = mad_send_wr->mad_agent_priv;
1753 struct ib_mad_agent_private, agent);
1754
1755 spin_lock_irqsave(&mad_agent_priv->lock, flags); 1839 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1840 if (mad_agent_priv->agent.rmpp_version) {
1841 ret = ib_process_rmpp_send_wc(mad_send_wr, mad_send_wc);
1842 if (ret == IB_RMPP_RESULT_CONSUMED)
1843 goto done;
1844 } else
1845 ret = IB_RMPP_RESULT_UNHANDLED;
1846
1756 if (mad_send_wc->status != IB_WC_SUCCESS && 1847 if (mad_send_wc->status != IB_WC_SUCCESS &&
1757 mad_send_wr->status == IB_WC_SUCCESS) { 1848 mad_send_wr->status == IB_WC_SUCCESS) {
1758 mad_send_wr->status = mad_send_wc->status; 1849 mad_send_wr->status = mad_send_wc->status;
@@ -1762,10 +1853,9 @@ static void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr,
1762 if (--mad_send_wr->refcount > 0) { 1853 if (--mad_send_wr->refcount > 0) {
1763 if (mad_send_wr->refcount == 1 && mad_send_wr->timeout && 1854 if (mad_send_wr->refcount == 1 && mad_send_wr->timeout &&
1764 mad_send_wr->status == IB_WC_SUCCESS) { 1855 mad_send_wr->status == IB_WC_SUCCESS) {
1765 wait_for_response(mad_agent_priv, mad_send_wr); 1856 wait_for_response(mad_send_wr);
1766 } 1857 }
1767 spin_unlock_irqrestore(&mad_agent_priv->lock, flags); 1858 goto done;
1768 return;
1769 } 1859 }
1770 1860
1771 /* Remove send from MAD agent and notify client of completion */ 1861 /* Remove send from MAD agent and notify client of completion */
@@ -1775,14 +1865,18 @@ static void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr,
1775 1865
1776 if (mad_send_wr->status != IB_WC_SUCCESS ) 1866 if (mad_send_wr->status != IB_WC_SUCCESS )
1777 mad_send_wc->status = mad_send_wr->status; 1867 mad_send_wc->status = mad_send_wr->status;
1778 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent, 1868 if (ret != IB_RMPP_RESULT_INTERNAL)
1779 mad_send_wc); 1869 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
1870 mad_send_wc);
1780 1871
1781 /* Release reference on agent taken when sending */ 1872 /* Release reference on agent taken when sending */
1782 if (atomic_dec_and_test(&mad_agent_priv->refcount)) 1873 if (atomic_dec_and_test(&mad_agent_priv->refcount))
1783 wake_up(&mad_agent_priv->wait); 1874 wake_up(&mad_agent_priv->wait);
1784 1875
1785 kfree(mad_send_wr); 1876 kfree(mad_send_wr);
1877 return;
1878done:
1879 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1786} 1880}
1787 1881
1788static void ib_mad_send_done_handler(struct ib_mad_port_private *port_priv, 1882static void ib_mad_send_done_handler(struct ib_mad_port_private *port_priv,
@@ -1961,6 +2055,8 @@ static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv)
1961 2055
1962 /* Empty wait list to prevent receives from finding a request */ 2056 /* Empty wait list to prevent receives from finding a request */
1963 list_splice_init(&mad_agent_priv->wait_list, &cancel_list); 2057 list_splice_init(&mad_agent_priv->wait_list, &cancel_list);
2058 /* Empty local completion list as well */
2059 list_splice_init(&mad_agent_priv->local_list, &cancel_list);
1964 spin_unlock_irqrestore(&mad_agent_priv->lock, flags); 2060 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1965 2061
1966 /* Report all cancelled requests */ 2062 /* Report all cancelled requests */
@@ -1980,8 +2076,7 @@ static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv)
1980} 2076}
1981 2077
1982static struct ib_mad_send_wr_private* 2078static struct ib_mad_send_wr_private*
1983find_send_by_wr_id(struct ib_mad_agent_private *mad_agent_priv, 2079find_send_by_wr_id(struct ib_mad_agent_private *mad_agent_priv, u64 wr_id)
1984 u64 wr_id)
1985{ 2080{
1986 struct ib_mad_send_wr_private *mad_send_wr; 2081 struct ib_mad_send_wr_private *mad_send_wr;
1987 2082
@@ -1993,79 +2088,50 @@ find_send_by_wr_id(struct ib_mad_agent_private *mad_agent_priv,
1993 2088
1994 list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list, 2089 list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list,
1995 agent_list) { 2090 agent_list) {
1996 if (mad_send_wr->wr_id == wr_id) 2091 if (is_data_mad(mad_agent_priv,
2092 mad_send_wr->send_wr.wr.ud.mad_hdr) &&
2093 mad_send_wr->wr_id == wr_id)
1997 return mad_send_wr; 2094 return mad_send_wr;
1998 } 2095 }
1999 return NULL; 2096 return NULL;
2000} 2097}
2001 2098
2002void cancel_sends(void *data) 2099int ib_modify_mad(struct ib_mad_agent *mad_agent, u64 wr_id, u32 timeout_ms)
2003{
2004 struct ib_mad_agent_private *mad_agent_priv;
2005 struct ib_mad_send_wr_private *mad_send_wr;
2006 struct ib_mad_send_wc mad_send_wc;
2007 unsigned long flags;
2008
2009 mad_agent_priv = data;
2010
2011 mad_send_wc.status = IB_WC_WR_FLUSH_ERR;
2012 mad_send_wc.vendor_err = 0;
2013
2014 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2015 while (!list_empty(&mad_agent_priv->canceled_list)) {
2016 mad_send_wr = list_entry(mad_agent_priv->canceled_list.next,
2017 struct ib_mad_send_wr_private,
2018 agent_list);
2019
2020 list_del(&mad_send_wr->agent_list);
2021 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2022
2023 mad_send_wc.wr_id = mad_send_wr->wr_id;
2024 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2025 &mad_send_wc);
2026
2027 kfree(mad_send_wr);
2028 if (atomic_dec_and_test(&mad_agent_priv->refcount))
2029 wake_up(&mad_agent_priv->wait);
2030 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2031 }
2032 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2033}
2034
2035void ib_cancel_mad(struct ib_mad_agent *mad_agent,
2036 u64 wr_id)
2037{ 2100{
2038 struct ib_mad_agent_private *mad_agent_priv; 2101 struct ib_mad_agent_private *mad_agent_priv;
2039 struct ib_mad_send_wr_private *mad_send_wr; 2102 struct ib_mad_send_wr_private *mad_send_wr;
2040 unsigned long flags; 2103 unsigned long flags;
2104 int active;
2041 2105
2042 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private, 2106 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
2043 agent); 2107 agent);
2044 spin_lock_irqsave(&mad_agent_priv->lock, flags); 2108 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2045 mad_send_wr = find_send_by_wr_id(mad_agent_priv, wr_id); 2109 mad_send_wr = find_send_by_wr_id(mad_agent_priv, wr_id);
2046 if (!mad_send_wr) { 2110 if (!mad_send_wr || mad_send_wr->status != IB_WC_SUCCESS) {
2047 spin_unlock_irqrestore(&mad_agent_priv->lock, flags); 2111 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2048 goto out; 2112 return -EINVAL;
2049 } 2113 }
2050 2114
2051 if (mad_send_wr->status == IB_WC_SUCCESS) 2115 active = (!mad_send_wr->timeout || mad_send_wr->refcount > 1);
2052 mad_send_wr->refcount -= (mad_send_wr->timeout > 0); 2116 if (!timeout_ms) {
2053
2054 if (mad_send_wr->refcount != 0) {
2055 mad_send_wr->status = IB_WC_WR_FLUSH_ERR; 2117 mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
2056 spin_unlock_irqrestore(&mad_agent_priv->lock, flags); 2118 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2057 goto out;
2058 } 2119 }
2059 2120
2060 list_del(&mad_send_wr->agent_list); 2121 mad_send_wr->send_wr.wr.ud.timeout_ms = timeout_ms;
2061 list_add_tail(&mad_send_wr->agent_list, &mad_agent_priv->canceled_list); 2122 if (active)
2062 adjust_timeout(mad_agent_priv); 2123 mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
2124 else
2125 ib_reset_mad_timeout(mad_send_wr, timeout_ms);
2126
2063 spin_unlock_irqrestore(&mad_agent_priv->lock, flags); 2127 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2128 return 0;
2129}
2130EXPORT_SYMBOL(ib_modify_mad);
2064 2131
2065 queue_work(mad_agent_priv->qp_info->port_priv->wq, 2132void ib_cancel_mad(struct ib_mad_agent *mad_agent, u64 wr_id)
2066 &mad_agent_priv->canceled_work); 2133{
2067out: 2134 ib_modify_mad(mad_agent, wr_id, 0);
2068 return;
2069} 2135}
2070EXPORT_SYMBOL(ib_cancel_mad); 2136EXPORT_SYMBOL(ib_cancel_mad);
2071 2137
@@ -2075,6 +2141,7 @@ static void local_completions(void *data)
2075 struct ib_mad_local_private *local; 2141 struct ib_mad_local_private *local;
2076 struct ib_mad_agent_private *recv_mad_agent; 2142 struct ib_mad_agent_private *recv_mad_agent;
2077 unsigned long flags; 2143 unsigned long flags;
2144 int recv = 0;
2078 struct ib_wc wc; 2145 struct ib_wc wc;
2079 struct ib_mad_send_wc mad_send_wc; 2146 struct ib_mad_send_wc mad_send_wc;
2080 2147
@@ -2090,10 +2157,10 @@ static void local_completions(void *data)
2090 recv_mad_agent = local->recv_mad_agent; 2157 recv_mad_agent = local->recv_mad_agent;
2091 if (!recv_mad_agent) { 2158 if (!recv_mad_agent) {
2092 printk(KERN_ERR PFX "No receive MAD agent for local completion\n"); 2159 printk(KERN_ERR PFX "No receive MAD agent for local completion\n");
2093 kmem_cache_free(ib_mad_cache, local->mad_priv);
2094 goto local_send_completion; 2160 goto local_send_completion;
2095 } 2161 }
2096 2162
2163 recv = 1;
2097 /* 2164 /*
2098 * Defined behavior is to complete response 2165 * Defined behavior is to complete response
2099 * before request 2166 * before request
@@ -2105,7 +2172,9 @@ static void local_completions(void *data)
2105 local->mad_priv->header.recv_wc.wc = &wc; 2172 local->mad_priv->header.recv_wc.wc = &wc;
2106 local->mad_priv->header.recv_wc.mad_len = 2173 local->mad_priv->header.recv_wc.mad_len =
2107 sizeof(struct ib_mad); 2174 sizeof(struct ib_mad);
2108 INIT_LIST_HEAD(&local->mad_priv->header.recv_wc.recv_buf.list); 2175 INIT_LIST_HEAD(&local->mad_priv->header.recv_wc.rmpp_list);
2176 list_add(&local->mad_priv->header.recv_wc.recv_buf.list,
2177 &local->mad_priv->header.recv_wc.rmpp_list);
2109 local->mad_priv->header.recv_wc.recv_buf.grh = NULL; 2178 local->mad_priv->header.recv_wc.recv_buf.grh = NULL;
2110 local->mad_priv->header.recv_wc.recv_buf.mad = 2179 local->mad_priv->header.recv_wc.recv_buf.mad =
2111 &local->mad_priv->mad.mad; 2180 &local->mad_priv->mad.mad;
@@ -2136,11 +2205,47 @@ local_send_completion:
2136 spin_lock_irqsave(&mad_agent_priv->lock, flags); 2205 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2137 list_del(&local->completion_list); 2206 list_del(&local->completion_list);
2138 atomic_dec(&mad_agent_priv->refcount); 2207 atomic_dec(&mad_agent_priv->refcount);
2208 if (!recv)
2209 kmem_cache_free(ib_mad_cache, local->mad_priv);
2139 kfree(local); 2210 kfree(local);
2140 } 2211 }
2141 spin_unlock_irqrestore(&mad_agent_priv->lock, flags); 2212 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2142} 2213}
2143 2214
2215static int retry_send(struct ib_mad_send_wr_private *mad_send_wr)
2216{
2217 int ret;
2218
2219 if (!mad_send_wr->retries--)
2220 return -ETIMEDOUT;
2221
2222 mad_send_wr->timeout = msecs_to_jiffies(mad_send_wr->send_wr.
2223 wr.ud.timeout_ms);
2224
2225 if (mad_send_wr->mad_agent_priv->agent.rmpp_version) {
2226 ret = ib_retry_rmpp(mad_send_wr);
2227 switch (ret) {
2228 case IB_RMPP_RESULT_UNHANDLED:
2229 ret = ib_send_mad(mad_send_wr);
2230 break;
2231 case IB_RMPP_RESULT_CONSUMED:
2232 ret = 0;
2233 break;
2234 default:
2235 ret = -ECOMM;
2236 break;
2237 }
2238 } else
2239 ret = ib_send_mad(mad_send_wr);
2240
2241 if (!ret) {
2242 mad_send_wr->refcount++;
2243 list_add_tail(&mad_send_wr->agent_list,
2244 &mad_send_wr->mad_agent_priv->send_list);
2245 }
2246 return ret;
2247}
2248
2144static void timeout_sends(void *data) 2249static void timeout_sends(void *data)
2145{ 2250{
2146 struct ib_mad_agent_private *mad_agent_priv; 2251 struct ib_mad_agent_private *mad_agent_priv;
@@ -2149,8 +2254,6 @@ static void timeout_sends(void *data)
2149 unsigned long flags, delay; 2254 unsigned long flags, delay;
2150 2255
2151 mad_agent_priv = (struct ib_mad_agent_private *)data; 2256 mad_agent_priv = (struct ib_mad_agent_private *)data;
2152
2153 mad_send_wc.status = IB_WC_RESP_TIMEOUT_ERR;
2154 mad_send_wc.vendor_err = 0; 2257 mad_send_wc.vendor_err = 0;
2155 2258
2156 spin_lock_irqsave(&mad_agent_priv->lock, flags); 2259 spin_lock_irqsave(&mad_agent_priv->lock, flags);
@@ -2170,8 +2273,16 @@ static void timeout_sends(void *data)
2170 } 2273 }
2171 2274
2172 list_del(&mad_send_wr->agent_list); 2275 list_del(&mad_send_wr->agent_list);
2276 if (mad_send_wr->status == IB_WC_SUCCESS &&
2277 !retry_send(mad_send_wr))
2278 continue;
2279
2173 spin_unlock_irqrestore(&mad_agent_priv->lock, flags); 2280 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2174 2281
2282 if (mad_send_wr->status == IB_WC_SUCCESS)
2283 mad_send_wc.status = IB_WC_RESP_TIMEOUT_ERR;
2284 else
2285 mad_send_wc.status = mad_send_wr->status;
2175 mad_send_wc.wr_id = mad_send_wr->wr_id; 2286 mad_send_wc.wr_id = mad_send_wr->wr_id;
2176 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent, 2287 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2177 &mad_send_wc); 2288 &mad_send_wc);
@@ -2447,14 +2558,6 @@ static int ib_mad_port_open(struct ib_device *device,
2447 unsigned long flags; 2558 unsigned long flags;
2448 char name[sizeof "ib_mad123"]; 2559 char name[sizeof "ib_mad123"];
2449 2560
2450 /* First, check if port already open at MAD layer */
2451 port_priv = ib_get_mad_port(device, port_num);
2452 if (port_priv) {
2453 printk(KERN_DEBUG PFX "%s port %d already open\n",
2454 device->name, port_num);
2455 return 0;
2456 }
2457
2458 /* Create new device info */ 2561 /* Create new device info */
2459 port_priv = kmalloc(sizeof *port_priv, GFP_KERNEL); 2562 port_priv = kmalloc(sizeof *port_priv, GFP_KERNEL);
2460 if (!port_priv) { 2563 if (!port_priv) {
@@ -2579,7 +2682,7 @@ static int ib_mad_port_close(struct ib_device *device, int port_num)
2579 2682
2580static void ib_mad_init_device(struct ib_device *device) 2683static void ib_mad_init_device(struct ib_device *device)
2581{ 2684{
2582 int ret, num_ports, cur_port, i, ret2; 2685 int num_ports, cur_port, i;
2583 2686
2584 if (device->node_type == IB_NODE_SWITCH) { 2687 if (device->node_type == IB_NODE_SWITCH) {
2585 num_ports = 1; 2688 num_ports = 1;
@@ -2589,47 +2692,37 @@ static void ib_mad_init_device(struct ib_device *device)
2589 cur_port = 1; 2692 cur_port = 1;
2590 } 2693 }
2591 for (i = 0; i < num_ports; i++, cur_port++) { 2694 for (i = 0; i < num_ports; i++, cur_port++) {
2592 ret = ib_mad_port_open(device, cur_port); 2695 if (ib_mad_port_open(device, cur_port)) {
2593 if (ret) {
2594 printk(KERN_ERR PFX "Couldn't open %s port %d\n", 2696 printk(KERN_ERR PFX "Couldn't open %s port %d\n",
2595 device->name, cur_port); 2697 device->name, cur_port);
2596 goto error_device_open; 2698 goto error_device_open;
2597 } 2699 }
2598 ret = ib_agent_port_open(device, cur_port); 2700 if (ib_agent_port_open(device, cur_port)) {
2599 if (ret) {
2600 printk(KERN_ERR PFX "Couldn't open %s port %d " 2701 printk(KERN_ERR PFX "Couldn't open %s port %d "
2601 "for agents\n", 2702 "for agents\n",
2602 device->name, cur_port); 2703 device->name, cur_port);
2603 goto error_device_open; 2704 goto error_device_open;
2604 } 2705 }
2605 } 2706 }
2606 2707 return;
2607 goto error_device_query;
2608 2708
2609error_device_open: 2709error_device_open:
2610 while (i > 0) { 2710 while (i > 0) {
2611 cur_port--; 2711 cur_port--;
2612 ret2 = ib_agent_port_close(device, cur_port); 2712 if (ib_agent_port_close(device, cur_port))
2613 if (ret2) {
2614 printk(KERN_ERR PFX "Couldn't close %s port %d " 2713 printk(KERN_ERR PFX "Couldn't close %s port %d "
2615 "for agents\n", 2714 "for agents\n",
2616 device->name, cur_port); 2715 device->name, cur_port);
2617 } 2716 if (ib_mad_port_close(device, cur_port))
2618 ret2 = ib_mad_port_close(device, cur_port);
2619 if (ret2) {
2620 printk(KERN_ERR PFX "Couldn't close %s port %d\n", 2717 printk(KERN_ERR PFX "Couldn't close %s port %d\n",
2621 device->name, cur_port); 2718 device->name, cur_port);
2622 }
2623 i--; 2719 i--;
2624 } 2720 }
2625
2626error_device_query:
2627 return;
2628} 2721}
2629 2722
2630static void ib_mad_remove_device(struct ib_device *device) 2723static void ib_mad_remove_device(struct ib_device *device)
2631{ 2724{
2632 int ret = 0, i, num_ports, cur_port, ret2; 2725 int i, num_ports, cur_port;
2633 2726
2634 if (device->node_type == IB_NODE_SWITCH) { 2727 if (device->node_type == IB_NODE_SWITCH) {
2635 num_ports = 1; 2728 num_ports = 1;
@@ -2639,21 +2732,13 @@ static void ib_mad_remove_device(struct ib_device *device)
2639 cur_port = 1; 2732 cur_port = 1;
2640 } 2733 }
2641 for (i = 0; i < num_ports; i++, cur_port++) { 2734 for (i = 0; i < num_ports; i++, cur_port++) {
2642 ret2 = ib_agent_port_close(device, cur_port); 2735 if (ib_agent_port_close(device, cur_port))
2643 if (ret2) {
2644 printk(KERN_ERR PFX "Couldn't close %s port %d " 2736 printk(KERN_ERR PFX "Couldn't close %s port %d "
2645 "for agents\n", 2737 "for agents\n",
2646 device->name, cur_port); 2738 device->name, cur_port);
2647 if (!ret) 2739 if (ib_mad_port_close(device, cur_port))
2648 ret = ret2;
2649 }
2650 ret2 = ib_mad_port_close(device, cur_port);
2651 if (ret2) {
2652 printk(KERN_ERR PFX "Couldn't close %s port %d\n", 2740 printk(KERN_ERR PFX "Couldn't close %s port %d\n",
2653 device->name, cur_port); 2741 device->name, cur_port);
2654 if (!ret)
2655 ret = ret2;
2656 }
2657 } 2742 }
2658} 2743}
2659 2744
@@ -2709,3 +2794,4 @@ static void __exit ib_mad_cleanup_module(void)
2709 2794
2710module_init(ib_mad_init_module); 2795module_init(ib_mad_init_module);
2711module_exit(ib_mad_cleanup_module); 2796module_exit(ib_mad_cleanup_module);
2797
diff --git a/drivers/infiniband/core/mad_priv.h b/drivers/infiniband/core/mad_priv.h
index 008cbcb94b15..568da10b05ab 100644
--- a/drivers/infiniband/core/mad_priv.h
+++ b/drivers/infiniband/core/mad_priv.h
@@ -1,5 +1,7 @@
1/* 1/*
2 * Copyright (c) 2004, 2005, Voltaire, Inc. All rights reserved. 2 * Copyright (c) 2004, 2005, Voltaire, Inc. All rights reserved.
3 * Copyright (c) 2005 Intel Corporation. All rights reserved.
4 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
3 * 5 *
4 * This software is available to you under a choice of one of two 6 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU 7 * licenses. You may choose to be licensed under the terms of the GNU
@@ -29,7 +31,7 @@
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE. 32 * SOFTWARE.
31 * 33 *
32 * $Id: mad_priv.h 1389 2004-12-27 22:56:47Z roland $ 34 * $Id: mad_priv.h 2730 2005-06-28 16:43:03Z sean.hefty $
33 */ 35 */
34 36
35#ifndef __IB_MAD_PRIV_H__ 37#ifndef __IB_MAD_PRIV_H__
@@ -92,16 +94,15 @@ struct ib_mad_agent_private {
92 spinlock_t lock; 94 spinlock_t lock;
93 struct list_head send_list; 95 struct list_head send_list;
94 struct list_head wait_list; 96 struct list_head wait_list;
97 struct list_head done_list;
95 struct work_struct timed_work; 98 struct work_struct timed_work;
96 unsigned long timeout; 99 unsigned long timeout;
97 struct list_head local_list; 100 struct list_head local_list;
98 struct work_struct local_work; 101 struct work_struct local_work;
99 struct list_head canceled_list; 102 struct list_head rmpp_list;
100 struct work_struct canceled_work;
101 103
102 atomic_t refcount; 104 atomic_t refcount;
103 wait_queue_head_t wait; 105 wait_queue_head_t wait;
104 u8 rmpp_version;
105}; 106};
106 107
107struct ib_mad_snoop_private { 108struct ib_mad_snoop_private {
@@ -116,15 +117,24 @@ struct ib_mad_snoop_private {
116struct ib_mad_send_wr_private { 117struct ib_mad_send_wr_private {
117 struct ib_mad_list_head mad_list; 118 struct ib_mad_list_head mad_list;
118 struct list_head agent_list; 119 struct list_head agent_list;
119 struct ib_mad_agent *agent; 120 struct ib_mad_agent_private *mad_agent_priv;
120 struct ib_send_wr send_wr; 121 struct ib_send_wr send_wr;
121 struct ib_sge sg_list[IB_MAD_SEND_REQ_MAX_SG]; 122 struct ib_sge sg_list[IB_MAD_SEND_REQ_MAX_SG];
122 u64 wr_id; /* client WR ID */ 123 u64 wr_id; /* client WR ID */
123 u64 tid; 124 u64 tid;
124 unsigned long timeout; 125 unsigned long timeout;
126 int retries;
125 int retry; 127 int retry;
126 int refcount; 128 int refcount;
127 enum ib_wc_status status; 129 enum ib_wc_status status;
130
131 /* RMPP control */
132 int last_ack;
133 int seg_num;
134 int newwin;
135 int total_seg;
136 int data_offset;
137 int pad;
128}; 138};
129 139
130struct ib_mad_local_private { 140struct ib_mad_local_private {
@@ -197,4 +207,17 @@ struct ib_mad_port_private {
197 207
198extern kmem_cache_t *ib_mad_cache; 208extern kmem_cache_t *ib_mad_cache;
199 209
210int ib_send_mad(struct ib_mad_send_wr_private *mad_send_wr);
211
212struct ib_mad_send_wr_private *
213ib_find_send_mad(struct ib_mad_agent_private *mad_agent_priv, u64 tid);
214
215void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr,
216 struct ib_mad_send_wc *mad_send_wc);
217
218void ib_mark_mad_done(struct ib_mad_send_wr_private *mad_send_wr);
219
220void ib_reset_mad_timeout(struct ib_mad_send_wr_private *mad_send_wr,
221 int timeout_ms);
222
200#endif /* __IB_MAD_PRIV_H__ */ 223#endif /* __IB_MAD_PRIV_H__ */
diff --git a/drivers/infiniband/core/mad_rmpp.c b/drivers/infiniband/core/mad_rmpp.c
new file mode 100644
index 000000000000..8f1eb80e421f
--- /dev/null
+++ b/drivers/infiniband/core/mad_rmpp.c
@@ -0,0 +1,765 @@
1/*
2 * Copyright (c) 2005 Intel Inc. All rights reserved.
3 * Copyright (c) 2005 Voltaire, Inc. All rights reserved.
4 *
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 *
33 * $Id: mad_rmpp.c 1921 2005-03-02 22:58:44Z sean.hefty $
34 */
35
36#include <linux/dma-mapping.h>
37
38#include "mad_priv.h"
39#include "mad_rmpp.h"
40
41enum rmpp_state {
42 RMPP_STATE_ACTIVE,
43 RMPP_STATE_TIMEOUT,
44 RMPP_STATE_COMPLETE
45};
46
47struct mad_rmpp_recv {
48 struct ib_mad_agent_private *agent;
49 struct list_head list;
50 struct work_struct timeout_work;
51 struct work_struct cleanup_work;
52 wait_queue_head_t wait;
53 enum rmpp_state state;
54 spinlock_t lock;
55 atomic_t refcount;
56
57 struct ib_ah *ah;
58 struct ib_mad_recv_wc *rmpp_wc;
59 struct ib_mad_recv_buf *cur_seg_buf;
60 int last_ack;
61 int seg_num;
62 int newwin;
63
64 u64 tid;
65 u32 src_qp;
66 u16 slid;
67 u8 mgmt_class;
68 u8 class_version;
69 u8 method;
70};
71
72static void destroy_rmpp_recv(struct mad_rmpp_recv *rmpp_recv)
73{
74 atomic_dec(&rmpp_recv->refcount);
75 wait_event(rmpp_recv->wait, !atomic_read(&rmpp_recv->refcount));
76 ib_destroy_ah(rmpp_recv->ah);
77 kfree(rmpp_recv);
78}
79
80void ib_cancel_rmpp_recvs(struct ib_mad_agent_private *agent)
81{
82 struct mad_rmpp_recv *rmpp_recv, *temp_rmpp_recv;
83 unsigned long flags;
84
85 spin_lock_irqsave(&agent->lock, flags);
86 list_for_each_entry(rmpp_recv, &agent->rmpp_list, list) {
87 cancel_delayed_work(&rmpp_recv->timeout_work);
88 cancel_delayed_work(&rmpp_recv->cleanup_work);
89 }
90 spin_unlock_irqrestore(&agent->lock, flags);
91
92 flush_workqueue(agent->qp_info->port_priv->wq);
93
94 list_for_each_entry_safe(rmpp_recv, temp_rmpp_recv,
95 &agent->rmpp_list, list) {
96 list_del(&rmpp_recv->list);
97 if (rmpp_recv->state != RMPP_STATE_COMPLETE)
98 ib_free_recv_mad(rmpp_recv->rmpp_wc);
99 destroy_rmpp_recv(rmpp_recv);
100 }
101}
102
103static void recv_timeout_handler(void *data)
104{
105 struct mad_rmpp_recv *rmpp_recv = data;
106 struct ib_mad_recv_wc *rmpp_wc;
107 unsigned long flags;
108
109 spin_lock_irqsave(&rmpp_recv->agent->lock, flags);
110 if (rmpp_recv->state != RMPP_STATE_ACTIVE) {
111 spin_unlock_irqrestore(&rmpp_recv->agent->lock, flags);
112 return;
113 }
114 rmpp_recv->state = RMPP_STATE_TIMEOUT;
115 list_del(&rmpp_recv->list);
116 spin_unlock_irqrestore(&rmpp_recv->agent->lock, flags);
117
118 /* TODO: send abort. */
119 rmpp_wc = rmpp_recv->rmpp_wc;
120 destroy_rmpp_recv(rmpp_recv);
121 ib_free_recv_mad(rmpp_wc);
122}
123
124static void recv_cleanup_handler(void *data)
125{
126 struct mad_rmpp_recv *rmpp_recv = data;
127 unsigned long flags;
128
129 spin_lock_irqsave(&rmpp_recv->agent->lock, flags);
130 list_del(&rmpp_recv->list);
131 spin_unlock_irqrestore(&rmpp_recv->agent->lock, flags);
132 destroy_rmpp_recv(rmpp_recv);
133}
134
135static struct mad_rmpp_recv *
136create_rmpp_recv(struct ib_mad_agent_private *agent,
137 struct ib_mad_recv_wc *mad_recv_wc)
138{
139 struct mad_rmpp_recv *rmpp_recv;
140 struct ib_mad_hdr *mad_hdr;
141
142 rmpp_recv = kmalloc(sizeof *rmpp_recv, GFP_KERNEL);
143 if (!rmpp_recv)
144 return NULL;
145
146 rmpp_recv->ah = ib_create_ah_from_wc(agent->agent.qp->pd,
147 mad_recv_wc->wc,
148 mad_recv_wc->recv_buf.grh,
149 agent->agent.port_num);
150 if (IS_ERR(rmpp_recv->ah))
151 goto error;
152
153 rmpp_recv->agent = agent;
154 init_waitqueue_head(&rmpp_recv->wait);
155 INIT_WORK(&rmpp_recv->timeout_work, recv_timeout_handler, rmpp_recv);
156 INIT_WORK(&rmpp_recv->cleanup_work, recv_cleanup_handler, rmpp_recv);
157 spin_lock_init(&rmpp_recv->lock);
158 rmpp_recv->state = RMPP_STATE_ACTIVE;
159 atomic_set(&rmpp_recv->refcount, 1);
160
161 rmpp_recv->rmpp_wc = mad_recv_wc;
162 rmpp_recv->cur_seg_buf = &mad_recv_wc->recv_buf;
163 rmpp_recv->newwin = 1;
164 rmpp_recv->seg_num = 1;
165 rmpp_recv->last_ack = 0;
166
167 mad_hdr = &mad_recv_wc->recv_buf.mad->mad_hdr;
168 rmpp_recv->tid = mad_hdr->tid;
169 rmpp_recv->src_qp = mad_recv_wc->wc->src_qp;
170 rmpp_recv->slid = mad_recv_wc->wc->slid;
171 rmpp_recv->mgmt_class = mad_hdr->mgmt_class;
172 rmpp_recv->class_version = mad_hdr->class_version;
173 rmpp_recv->method = mad_hdr->method;
174 return rmpp_recv;
175
176error: kfree(rmpp_recv);
177 return NULL;
178}
179
180static inline void deref_rmpp_recv(struct mad_rmpp_recv *rmpp_recv)
181{
182 if (atomic_dec_and_test(&rmpp_recv->refcount))
183 wake_up(&rmpp_recv->wait);
184}
185
186static struct mad_rmpp_recv *
187find_rmpp_recv(struct ib_mad_agent_private *agent,
188 struct ib_mad_recv_wc *mad_recv_wc)
189{
190 struct mad_rmpp_recv *rmpp_recv;
191 struct ib_mad_hdr *mad_hdr = &mad_recv_wc->recv_buf.mad->mad_hdr;
192
193 list_for_each_entry(rmpp_recv, &agent->rmpp_list, list) {
194 if (rmpp_recv->tid == mad_hdr->tid &&
195 rmpp_recv->src_qp == mad_recv_wc->wc->src_qp &&
196 rmpp_recv->slid == mad_recv_wc->wc->slid &&
197 rmpp_recv->mgmt_class == mad_hdr->mgmt_class &&
198 rmpp_recv->class_version == mad_hdr->class_version &&
199 rmpp_recv->method == mad_hdr->method)
200 return rmpp_recv;
201 }
202 return NULL;
203}
204
205static struct mad_rmpp_recv *
206acquire_rmpp_recv(struct ib_mad_agent_private *agent,
207 struct ib_mad_recv_wc *mad_recv_wc)
208{
209 struct mad_rmpp_recv *rmpp_recv;
210 unsigned long flags;
211
212 spin_lock_irqsave(&agent->lock, flags);
213 rmpp_recv = find_rmpp_recv(agent, mad_recv_wc);
214 if (rmpp_recv)
215 atomic_inc(&rmpp_recv->refcount);
216 spin_unlock_irqrestore(&agent->lock, flags);
217 return rmpp_recv;
218}
219
220static struct mad_rmpp_recv *
221insert_rmpp_recv(struct ib_mad_agent_private *agent,
222 struct mad_rmpp_recv *rmpp_recv)
223{
224 struct mad_rmpp_recv *cur_rmpp_recv;
225
226 cur_rmpp_recv = find_rmpp_recv(agent, rmpp_recv->rmpp_wc);
227 if (!cur_rmpp_recv)
228 list_add_tail(&rmpp_recv->list, &agent->rmpp_list);
229
230 return cur_rmpp_recv;
231}
232
233static int data_offset(u8 mgmt_class)
234{
235 if (mgmt_class == IB_MGMT_CLASS_SUBN_ADM)
236 return offsetof(struct ib_sa_mad, data);
237 else if ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
238 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END))
239 return offsetof(struct ib_vendor_mad, data);
240 else
241 return offsetof(struct ib_rmpp_mad, data);
242}
243
244static void format_ack(struct ib_rmpp_mad *ack,
245 struct ib_rmpp_mad *data,
246 struct mad_rmpp_recv *rmpp_recv)
247{
248 unsigned long flags;
249
250 memcpy(&ack->mad_hdr, &data->mad_hdr,
251 data_offset(data->mad_hdr.mgmt_class));
252
253 ack->mad_hdr.method ^= IB_MGMT_METHOD_RESP;
254 ack->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_ACK;
255 ib_set_rmpp_flags(&ack->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
256
257 spin_lock_irqsave(&rmpp_recv->lock, flags);
258 rmpp_recv->last_ack = rmpp_recv->seg_num;
259 ack->rmpp_hdr.seg_num = cpu_to_be32(rmpp_recv->seg_num);
260 ack->rmpp_hdr.paylen_newwin = cpu_to_be32(rmpp_recv->newwin);
261 spin_unlock_irqrestore(&rmpp_recv->lock, flags);
262}
263
264static void ack_recv(struct mad_rmpp_recv *rmpp_recv,
265 struct ib_mad_recv_wc *recv_wc)
266{
267 struct ib_mad_send_buf *msg;
268 struct ib_send_wr *bad_send_wr;
269 int hdr_len, ret;
270
271 hdr_len = sizeof(struct ib_mad_hdr) + sizeof(struct ib_rmpp_hdr);
272 msg = ib_create_send_mad(&rmpp_recv->agent->agent, recv_wc->wc->src_qp,
273 recv_wc->wc->pkey_index, rmpp_recv->ah, 1,
274 hdr_len, sizeof(struct ib_rmpp_mad) - hdr_len,
275 GFP_KERNEL);
276 if (!msg)
277 return;
278
279 format_ack((struct ib_rmpp_mad *) msg->mad,
280 (struct ib_rmpp_mad *) recv_wc->recv_buf.mad, rmpp_recv);
281 ret = ib_post_send_mad(&rmpp_recv->agent->agent, &msg->send_wr,
282 &bad_send_wr);
283 if (ret)
284 ib_free_send_mad(msg);
285}
286
287static inline int get_last_flag(struct ib_mad_recv_buf *seg)
288{
289 struct ib_rmpp_mad *rmpp_mad;
290
291 rmpp_mad = (struct ib_rmpp_mad *) seg->mad;
292 return ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) & IB_MGMT_RMPP_FLAG_LAST;
293}
294
295static inline int get_seg_num(struct ib_mad_recv_buf *seg)
296{
297 struct ib_rmpp_mad *rmpp_mad;
298
299 rmpp_mad = (struct ib_rmpp_mad *) seg->mad;
300 return be32_to_cpu(rmpp_mad->rmpp_hdr.seg_num);
301}
302
303static inline struct ib_mad_recv_buf * get_next_seg(struct list_head *rmpp_list,
304 struct ib_mad_recv_buf *seg)
305{
306 if (seg->list.next == rmpp_list)
307 return NULL;
308
309 return container_of(seg->list.next, struct ib_mad_recv_buf, list);
310}
311
312static inline int window_size(struct ib_mad_agent_private *agent)
313{
314 return max(agent->qp_info->recv_queue.max_active >> 3, 1);
315}
316
317static struct ib_mad_recv_buf * find_seg_location(struct list_head *rmpp_list,
318 int seg_num)
319{
320 struct ib_mad_recv_buf *seg_buf;
321 int cur_seg_num;
322
323 list_for_each_entry_reverse(seg_buf, rmpp_list, list) {
324 cur_seg_num = get_seg_num(seg_buf);
325 if (seg_num > cur_seg_num)
326 return seg_buf;
327 if (seg_num == cur_seg_num)
328 break;
329 }
330 return NULL;
331}
332
333static void update_seg_num(struct mad_rmpp_recv *rmpp_recv,
334 struct ib_mad_recv_buf *new_buf)
335{
336 struct list_head *rmpp_list = &rmpp_recv->rmpp_wc->rmpp_list;
337
338 while (new_buf && (get_seg_num(new_buf) == rmpp_recv->seg_num + 1)) {
339 rmpp_recv->cur_seg_buf = new_buf;
340 rmpp_recv->seg_num++;
341 new_buf = get_next_seg(rmpp_list, new_buf);
342 }
343}
344
345static inline int get_mad_len(struct mad_rmpp_recv *rmpp_recv)
346{
347 struct ib_rmpp_mad *rmpp_mad;
348 int hdr_size, data_size, pad;
349
350 rmpp_mad = (struct ib_rmpp_mad *)rmpp_recv->cur_seg_buf->mad;
351
352 hdr_size = data_offset(rmpp_mad->mad_hdr.mgmt_class);
353 data_size = sizeof(struct ib_rmpp_mad) - hdr_size;
354 pad = data_size - be32_to_cpu(rmpp_mad->rmpp_hdr.paylen_newwin);
355 if (pad > data_size || pad < 0)
356 pad = 0;
357
358 return hdr_size + rmpp_recv->seg_num * data_size - pad;
359}
360
361static struct ib_mad_recv_wc * complete_rmpp(struct mad_rmpp_recv *rmpp_recv)
362{
363 struct ib_mad_recv_wc *rmpp_wc;
364
365 ack_recv(rmpp_recv, rmpp_recv->rmpp_wc);
366 if (rmpp_recv->seg_num > 1)
367 cancel_delayed_work(&rmpp_recv->timeout_work);
368
369 rmpp_wc = rmpp_recv->rmpp_wc;
370 rmpp_wc->mad_len = get_mad_len(rmpp_recv);
371 /* 10 seconds until we can find the packet lifetime */
372 queue_delayed_work(rmpp_recv->agent->qp_info->port_priv->wq,
373 &rmpp_recv->cleanup_work, msecs_to_jiffies(10000));
374 return rmpp_wc;
375}
376
377void ib_coalesce_recv_mad(struct ib_mad_recv_wc *mad_recv_wc, void *buf)
378{
379 struct ib_mad_recv_buf *seg_buf;
380 struct ib_rmpp_mad *rmpp_mad;
381 void *data;
382 int size, len, offset;
383 u8 flags;
384
385 len = mad_recv_wc->mad_len;
386 if (len <= sizeof(struct ib_mad)) {
387 memcpy(buf, mad_recv_wc->recv_buf.mad, len);
388 return;
389 }
390
391 offset = data_offset(mad_recv_wc->recv_buf.mad->mad_hdr.mgmt_class);
392
393 list_for_each_entry(seg_buf, &mad_recv_wc->rmpp_list, list) {
394 rmpp_mad = (struct ib_rmpp_mad *)seg_buf->mad;
395 flags = ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr);
396
397 if (flags & IB_MGMT_RMPP_FLAG_FIRST) {
398 data = rmpp_mad;
399 size = sizeof(*rmpp_mad);
400 } else {
401 data = (void *) rmpp_mad + offset;
402 if (flags & IB_MGMT_RMPP_FLAG_LAST)
403 size = len;
404 else
405 size = sizeof(*rmpp_mad) - offset;
406 }
407
408 memcpy(buf, data, size);
409 len -= size;
410 buf += size;
411 }
412}
413EXPORT_SYMBOL(ib_coalesce_recv_mad);
414
415static struct ib_mad_recv_wc *
416continue_rmpp(struct ib_mad_agent_private *agent,
417 struct ib_mad_recv_wc *mad_recv_wc)
418{
419 struct mad_rmpp_recv *rmpp_recv;
420 struct ib_mad_recv_buf *prev_buf;
421 struct ib_mad_recv_wc *done_wc;
422 int seg_num;
423 unsigned long flags;
424
425 rmpp_recv = acquire_rmpp_recv(agent, mad_recv_wc);
426 if (!rmpp_recv)
427 goto drop1;
428
429 seg_num = get_seg_num(&mad_recv_wc->recv_buf);
430
431 spin_lock_irqsave(&rmpp_recv->lock, flags);
432 if ((rmpp_recv->state == RMPP_STATE_TIMEOUT) ||
433 (seg_num > rmpp_recv->newwin))
434 goto drop3;
435
436 if ((seg_num <= rmpp_recv->last_ack) ||
437 (rmpp_recv->state == RMPP_STATE_COMPLETE)) {
438 spin_unlock_irqrestore(&rmpp_recv->lock, flags);
439 ack_recv(rmpp_recv, mad_recv_wc);
440 goto drop2;
441 }
442
443 prev_buf = find_seg_location(&rmpp_recv->rmpp_wc->rmpp_list, seg_num);
444 if (!prev_buf)
445 goto drop3;
446
447 done_wc = NULL;
448 list_add(&mad_recv_wc->recv_buf.list, &prev_buf->list);
449 if (rmpp_recv->cur_seg_buf == prev_buf) {
450 update_seg_num(rmpp_recv, &mad_recv_wc->recv_buf);
451 if (get_last_flag(rmpp_recv->cur_seg_buf)) {
452 rmpp_recv->state = RMPP_STATE_COMPLETE;
453 spin_unlock_irqrestore(&rmpp_recv->lock, flags);
454 done_wc = complete_rmpp(rmpp_recv);
455 goto out;
456 } else if (rmpp_recv->seg_num == rmpp_recv->newwin) {
457 rmpp_recv->newwin += window_size(agent);
458 spin_unlock_irqrestore(&rmpp_recv->lock, flags);
459 ack_recv(rmpp_recv, mad_recv_wc);
460 goto out;
461 }
462 }
463 spin_unlock_irqrestore(&rmpp_recv->lock, flags);
464out:
465 deref_rmpp_recv(rmpp_recv);
466 return done_wc;
467
468drop3: spin_unlock_irqrestore(&rmpp_recv->lock, flags);
469drop2: deref_rmpp_recv(rmpp_recv);
470drop1: ib_free_recv_mad(mad_recv_wc);
471 return NULL;
472}
473
474static struct ib_mad_recv_wc *
475start_rmpp(struct ib_mad_agent_private *agent,
476 struct ib_mad_recv_wc *mad_recv_wc)
477{
478 struct mad_rmpp_recv *rmpp_recv;
479 unsigned long flags;
480
481 rmpp_recv = create_rmpp_recv(agent, mad_recv_wc);
482 if (!rmpp_recv) {
483 ib_free_recv_mad(mad_recv_wc);
484 return NULL;
485 }
486
487 spin_lock_irqsave(&agent->lock, flags);
488 if (insert_rmpp_recv(agent, rmpp_recv)) {
489 spin_unlock_irqrestore(&agent->lock, flags);
490 /* duplicate first MAD */
491 destroy_rmpp_recv(rmpp_recv);
492 return continue_rmpp(agent, mad_recv_wc);
493 }
494 atomic_inc(&rmpp_recv->refcount);
495
496 if (get_last_flag(&mad_recv_wc->recv_buf)) {
497 rmpp_recv->state = RMPP_STATE_COMPLETE;
498 spin_unlock_irqrestore(&agent->lock, flags);
499 complete_rmpp(rmpp_recv);
500 } else {
501 spin_unlock_irqrestore(&agent->lock, flags);
502 /* 40 seconds until we can find the packet lifetimes */
503 queue_delayed_work(agent->qp_info->port_priv->wq,
504 &rmpp_recv->timeout_work,
505 msecs_to_jiffies(40000));
506 rmpp_recv->newwin += window_size(agent);
507 ack_recv(rmpp_recv, mad_recv_wc);
508 mad_recv_wc = NULL;
509 }
510 deref_rmpp_recv(rmpp_recv);
511 return mad_recv_wc;
512}
513
514static inline u64 get_seg_addr(struct ib_mad_send_wr_private *mad_send_wr)
515{
516 return mad_send_wr->sg_list[0].addr + mad_send_wr->data_offset +
517 (sizeof(struct ib_rmpp_mad) - mad_send_wr->data_offset) *
518 (mad_send_wr->seg_num - 1);
519}
520
521static int send_next_seg(struct ib_mad_send_wr_private *mad_send_wr)
522{
523 struct ib_rmpp_mad *rmpp_mad;
524 int timeout;
525
526 rmpp_mad = (struct ib_rmpp_mad *)mad_send_wr->send_wr.wr.ud.mad_hdr;
527 ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
528 rmpp_mad->rmpp_hdr.seg_num = cpu_to_be32(mad_send_wr->seg_num);
529
530 if (mad_send_wr->seg_num == 1) {
531 rmpp_mad->rmpp_hdr.rmpp_rtime_flags |= IB_MGMT_RMPP_FLAG_FIRST;
532 rmpp_mad->rmpp_hdr.paylen_newwin =
533 cpu_to_be32(mad_send_wr->total_seg *
534 (sizeof(struct ib_rmpp_mad) -
535 offsetof(struct ib_rmpp_mad, data)));
536 mad_send_wr->sg_list[0].length = sizeof(struct ib_rmpp_mad);
537 } else {
538 mad_send_wr->send_wr.num_sge = 2;
539 mad_send_wr->sg_list[0].length = mad_send_wr->data_offset;
540 mad_send_wr->sg_list[1].addr = get_seg_addr(mad_send_wr);
541 mad_send_wr->sg_list[1].length = sizeof(struct ib_rmpp_mad) -
542 mad_send_wr->data_offset;
543 mad_send_wr->sg_list[1].lkey = mad_send_wr->sg_list[0].lkey;
544 }
545
546 if (mad_send_wr->seg_num == mad_send_wr->total_seg) {
547 rmpp_mad->rmpp_hdr.rmpp_rtime_flags |= IB_MGMT_RMPP_FLAG_LAST;
548 rmpp_mad->rmpp_hdr.paylen_newwin =
549 cpu_to_be32(sizeof(struct ib_rmpp_mad) -
550 offsetof(struct ib_rmpp_mad, data) -
551 mad_send_wr->pad);
552 }
553
554 /* 2 seconds for an ACK until we can find the packet lifetime */
555 timeout = mad_send_wr->send_wr.wr.ud.timeout_ms;
556 if (!timeout || timeout > 2000)
557 mad_send_wr->timeout = msecs_to_jiffies(2000);
558 mad_send_wr->seg_num++;
559 return ib_send_mad(mad_send_wr);
560}
561
562static void process_rmpp_ack(struct ib_mad_agent_private *agent,
563 struct ib_mad_recv_wc *mad_recv_wc)
564{
565 struct ib_mad_send_wr_private *mad_send_wr;
566 struct ib_rmpp_mad *rmpp_mad;
567 unsigned long flags;
568 int seg_num, newwin, ret;
569
570 rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
571 if (rmpp_mad->rmpp_hdr.rmpp_status)
572 return;
573
574 seg_num = be32_to_cpu(rmpp_mad->rmpp_hdr.seg_num);
575 newwin = be32_to_cpu(rmpp_mad->rmpp_hdr.paylen_newwin);
576
577 spin_lock_irqsave(&agent->lock, flags);
578 mad_send_wr = ib_find_send_mad(agent, rmpp_mad->mad_hdr.tid);
579 if (!mad_send_wr)
580 goto out; /* Unmatched ACK */
581
582 if ((mad_send_wr->last_ack == mad_send_wr->total_seg) ||
583 (!mad_send_wr->timeout) || (mad_send_wr->status != IB_WC_SUCCESS))
584 goto out; /* Send is already done */
585
586 if (seg_num > mad_send_wr->total_seg)
587 goto out; /* Bad ACK */
588
589 if (newwin < mad_send_wr->newwin || seg_num < mad_send_wr->last_ack)
590 goto out; /* Old ACK */
591
592 if (seg_num > mad_send_wr->last_ack) {
593 mad_send_wr->last_ack = seg_num;
594 mad_send_wr->retries = mad_send_wr->send_wr.wr.ud.retries;
595 }
596 mad_send_wr->newwin = newwin;
597 if (mad_send_wr->last_ack == mad_send_wr->total_seg) {
598 /* If no response is expected, the ACK completes the send */
599 if (!mad_send_wr->send_wr.wr.ud.timeout_ms) {
600 struct ib_mad_send_wc wc;
601
602 ib_mark_mad_done(mad_send_wr);
603 spin_unlock_irqrestore(&agent->lock, flags);
604
605 wc.status = IB_WC_SUCCESS;
606 wc.vendor_err = 0;
607 wc.wr_id = mad_send_wr->wr_id;
608 ib_mad_complete_send_wr(mad_send_wr, &wc);
609 return;
610 }
611 if (mad_send_wr->refcount == 1)
612 ib_reset_mad_timeout(mad_send_wr, mad_send_wr->
613 send_wr.wr.ud.timeout_ms);
614 } else if (mad_send_wr->refcount == 1 &&
615 mad_send_wr->seg_num < mad_send_wr->newwin &&
616 mad_send_wr->seg_num <= mad_send_wr->total_seg) {
617 /* Send failure will just result in a timeout/retry */
618 ret = send_next_seg(mad_send_wr);
619 if (ret)
620 goto out;
621
622 mad_send_wr->refcount++;
623 list_del(&mad_send_wr->agent_list);
624 list_add_tail(&mad_send_wr->agent_list,
625 &mad_send_wr->mad_agent_priv->send_list);
626 }
627out:
628 spin_unlock_irqrestore(&agent->lock, flags);
629}
630
631struct ib_mad_recv_wc *
632ib_process_rmpp_recv_wc(struct ib_mad_agent_private *agent,
633 struct ib_mad_recv_wc *mad_recv_wc)
634{
635 struct ib_rmpp_mad *rmpp_mad;
636
637 rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
638 if (!(rmpp_mad->rmpp_hdr.rmpp_rtime_flags & IB_MGMT_RMPP_FLAG_ACTIVE))
639 return mad_recv_wc;
640
641 if (rmpp_mad->rmpp_hdr.rmpp_version != IB_MGMT_RMPP_VERSION)
642 goto out;
643
644 switch (rmpp_mad->rmpp_hdr.rmpp_type) {
645 case IB_MGMT_RMPP_TYPE_DATA:
646 if (rmpp_mad->rmpp_hdr.seg_num == __constant_htonl(1))
647 return start_rmpp(agent, mad_recv_wc);
648 else
649 return continue_rmpp(agent, mad_recv_wc);
650 case IB_MGMT_RMPP_TYPE_ACK:
651 process_rmpp_ack(agent, mad_recv_wc);
652 break;
653 case IB_MGMT_RMPP_TYPE_STOP:
654 case IB_MGMT_RMPP_TYPE_ABORT:
655 /* TODO: process_rmpp_nack(agent, mad_recv_wc); */
656 break;
657 default:
658 break;
659 }
660out:
661 ib_free_recv_mad(mad_recv_wc);
662 return NULL;
663}
664
665int ib_send_rmpp_mad(struct ib_mad_send_wr_private *mad_send_wr)
666{
667 struct ib_rmpp_mad *rmpp_mad;
668 int i, total_len, ret;
669
670 rmpp_mad = (struct ib_rmpp_mad *)mad_send_wr->send_wr.wr.ud.mad_hdr;
671 if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
672 IB_MGMT_RMPP_FLAG_ACTIVE))
673 return IB_RMPP_RESULT_UNHANDLED;
674
675 if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_DATA)
676 return IB_RMPP_RESULT_INTERNAL;
677
678 if (mad_send_wr->send_wr.num_sge > 1)
679 return -EINVAL; /* TODO: support num_sge > 1 */
680
681 mad_send_wr->seg_num = 1;
682 mad_send_wr->newwin = 1;
683 mad_send_wr->data_offset = data_offset(rmpp_mad->mad_hdr.mgmt_class);
684
685 total_len = 0;
686 for (i = 0; i < mad_send_wr->send_wr.num_sge; i++)
687 total_len += mad_send_wr->send_wr.sg_list[i].length;
688
689 mad_send_wr->total_seg = (total_len - mad_send_wr->data_offset) /
690 (sizeof(struct ib_rmpp_mad) - mad_send_wr->data_offset);
691 mad_send_wr->pad = total_len - offsetof(struct ib_rmpp_mad, data) -
692 be32_to_cpu(rmpp_mad->rmpp_hdr.paylen_newwin);
693
694 /* We need to wait for the final ACK even if there isn't a response */
695 mad_send_wr->refcount += (mad_send_wr->timeout == 0);
696 ret = send_next_seg(mad_send_wr);
697 if (!ret)
698 return IB_RMPP_RESULT_CONSUMED;
699 return ret;
700}
701
702int ib_process_rmpp_send_wc(struct ib_mad_send_wr_private *mad_send_wr,
703 struct ib_mad_send_wc *mad_send_wc)
704{
705 struct ib_rmpp_mad *rmpp_mad;
706 struct ib_mad_send_buf *msg;
707 int ret;
708
709 rmpp_mad = (struct ib_rmpp_mad *)mad_send_wr->send_wr.wr.ud.mad_hdr;
710 if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
711 IB_MGMT_RMPP_FLAG_ACTIVE))
712 return IB_RMPP_RESULT_UNHANDLED; /* RMPP not active */
713
714 if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_DATA) {
715 msg = (struct ib_mad_send_buf *) (unsigned long)
716 mad_send_wc->wr_id;
717 ib_free_send_mad(msg);
718 return IB_RMPP_RESULT_INTERNAL; /* ACK, STOP, or ABORT */
719 }
720
721 if (mad_send_wc->status != IB_WC_SUCCESS ||
722 mad_send_wr->status != IB_WC_SUCCESS)
723 return IB_RMPP_RESULT_PROCESSED; /* Canceled or send error */
724
725 if (!mad_send_wr->timeout)
726 return IB_RMPP_RESULT_PROCESSED; /* Response received */
727
728 if (mad_send_wr->last_ack == mad_send_wr->total_seg) {
729 mad_send_wr->timeout =
730 msecs_to_jiffies(mad_send_wr->send_wr.wr.ud.timeout_ms);
731 return IB_RMPP_RESULT_PROCESSED; /* Send done */
732 }
733
734 if (mad_send_wr->seg_num > mad_send_wr->newwin ||
735 mad_send_wr->seg_num > mad_send_wr->total_seg)
736 return IB_RMPP_RESULT_PROCESSED; /* Wait for ACK */
737
738 ret = send_next_seg(mad_send_wr);
739 if (ret) {
740 mad_send_wc->status = IB_WC_GENERAL_ERR;
741 return IB_RMPP_RESULT_PROCESSED;
742 }
743 return IB_RMPP_RESULT_CONSUMED;
744}
745
746int ib_retry_rmpp(struct ib_mad_send_wr_private *mad_send_wr)
747{
748 struct ib_rmpp_mad *rmpp_mad;
749 int ret;
750
751 rmpp_mad = (struct ib_rmpp_mad *)mad_send_wr->send_wr.wr.ud.mad_hdr;
752 if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
753 IB_MGMT_RMPP_FLAG_ACTIVE))
754 return IB_RMPP_RESULT_UNHANDLED; /* RMPP not active */
755
756 if (mad_send_wr->last_ack == mad_send_wr->total_seg)
757 return IB_RMPP_RESULT_PROCESSED;
758
759 mad_send_wr->seg_num = mad_send_wr->last_ack + 1;
760 ret = send_next_seg(mad_send_wr);
761 if (ret)
762 return IB_RMPP_RESULT_PROCESSED;
763
764 return IB_RMPP_RESULT_CONSUMED;
765}
diff --git a/drivers/infiniband/core/mad_rmpp.h b/drivers/infiniband/core/mad_rmpp.h
new file mode 100644
index 000000000000..c4924dfb8e75
--- /dev/null
+++ b/drivers/infiniband/core/mad_rmpp.h
@@ -0,0 +1,58 @@
1/*
2 * Copyright (c) 2005 Intel Inc. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 *
32 * $Id: mad_rmpp.h 1921 2005-02-25 22:58:44Z sean.hefty $
33 */
34
35#ifndef __MAD_RMPP_H__
36#define __MAD_RMPP_H__
37
38enum {
39 IB_RMPP_RESULT_PROCESSED,
40 IB_RMPP_RESULT_CONSUMED,
41 IB_RMPP_RESULT_INTERNAL,
42 IB_RMPP_RESULT_UNHANDLED
43};
44
45int ib_send_rmpp_mad(struct ib_mad_send_wr_private *mad_send_wr);
46
47struct ib_mad_recv_wc *
48ib_process_rmpp_recv_wc(struct ib_mad_agent_private *agent,
49 struct ib_mad_recv_wc *mad_recv_wc);
50
51int ib_process_rmpp_send_wc(struct ib_mad_send_wr_private *mad_send_wr,
52 struct ib_mad_send_wc *mad_send_wc);
53
54void ib_cancel_rmpp_recvs(struct ib_mad_agent_private *agent);
55
56int ib_retry_rmpp(struct ib_mad_send_wr_private *mad_send_wr);
57
58#endif /* __MAD_RMPP_H__ */
diff --git a/drivers/infiniband/core/sa_query.c b/drivers/infiniband/core/sa_query.c
index 5a08e81fa827..795184931c83 100644
--- a/drivers/infiniband/core/sa_query.c
+++ b/drivers/infiniband/core/sa_query.c
@@ -1,5 +1,6 @@
1/* 1/*
2 * Copyright (c) 2004 Topspin Communications. All rights reserved. 2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Voltaire, Inc. All rights reserved.
3 * 4 *
4 * This software is available to you under a choice of one of two 5 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU 6 * licenses. You may choose to be licensed under the terms of the GNU
@@ -29,7 +30,7 @@
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE. 31 * SOFTWARE.
31 * 32 *
32 * $Id: sa_query.c 1389 2004-12-27 22:56:47Z roland $ 33 * $Id: sa_query.c 2811 2005-07-06 18:11:43Z halr $
33 */ 34 */
34 35
35#include <linux/module.h> 36#include <linux/module.h>
@@ -50,26 +51,6 @@ MODULE_AUTHOR("Roland Dreier");
50MODULE_DESCRIPTION("InfiniBand subnet administration query support"); 51MODULE_DESCRIPTION("InfiniBand subnet administration query support");
51MODULE_LICENSE("Dual BSD/GPL"); 52MODULE_LICENSE("Dual BSD/GPL");
52 53
53/*
54 * These two structures must be packed because they have 64-bit fields
55 * that are only 32-bit aligned. 64-bit architectures will lay them
56 * out wrong otherwise. (And unfortunately they are sent on the wire
57 * so we can't change the layout)
58 */
59struct ib_sa_hdr {
60 u64 sm_key;
61 u16 attr_offset;
62 u16 reserved;
63 ib_sa_comp_mask comp_mask;
64} __attribute__ ((packed));
65
66struct ib_sa_mad {
67 struct ib_mad_hdr mad_hdr;
68 struct ib_rmpp_hdr rmpp_hdr;
69 struct ib_sa_hdr sa_hdr;
70 u8 data[200];
71} __attribute__ ((packed));
72
73struct ib_sa_sm_ah { 54struct ib_sa_sm_ah {
74 struct ib_ah *ah; 55 struct ib_ah *ah;
75 struct kref ref; 56 struct kref ref;
@@ -77,7 +58,6 @@ struct ib_sa_sm_ah {
77 58
78struct ib_sa_port { 59struct ib_sa_port {
79 struct ib_mad_agent *agent; 60 struct ib_mad_agent *agent;
80 struct ib_mr *mr;
81 struct ib_sa_sm_ah *sm_ah; 61 struct ib_sa_sm_ah *sm_ah;
82 struct work_struct update_task; 62 struct work_struct update_task;
83 spinlock_t ah_lock; 63 spinlock_t ah_lock;
@@ -100,6 +80,12 @@ struct ib_sa_query {
100 int id; 80 int id;
101}; 81};
102 82
83struct ib_sa_service_query {
84 void (*callback)(int, struct ib_sa_service_rec *, void *);
85 void *context;
86 struct ib_sa_query sa_query;
87};
88
103struct ib_sa_path_query { 89struct ib_sa_path_query {
104 void (*callback)(int, struct ib_sa_path_rec *, void *); 90 void (*callback)(int, struct ib_sa_path_rec *, void *);
105 void *context; 91 void *context;
@@ -341,6 +327,54 @@ static const struct ib_field mcmember_rec_table[] = {
341 .size_bits = 23 }, 327 .size_bits = 23 },
342}; 328};
343 329
330#define SERVICE_REC_FIELD(field) \
331 .struct_offset_bytes = offsetof(struct ib_sa_service_rec, field), \
332 .struct_size_bytes = sizeof ((struct ib_sa_service_rec *) 0)->field, \
333 .field_name = "sa_service_rec:" #field
334
335static const struct ib_field service_rec_table[] = {
336 { SERVICE_REC_FIELD(id),
337 .offset_words = 0,
338 .offset_bits = 0,
339 .size_bits = 64 },
340 { SERVICE_REC_FIELD(gid),
341 .offset_words = 2,
342 .offset_bits = 0,
343 .size_bits = 128 },
344 { SERVICE_REC_FIELD(pkey),
345 .offset_words = 6,
346 .offset_bits = 0,
347 .size_bits = 16 },
348 { SERVICE_REC_FIELD(lease),
349 .offset_words = 7,
350 .offset_bits = 0,
351 .size_bits = 32 },
352 { SERVICE_REC_FIELD(key),
353 .offset_words = 8,
354 .offset_bits = 0,
355 .size_bits = 128 },
356 { SERVICE_REC_FIELD(name),
357 .offset_words = 12,
358 .offset_bits = 0,
359 .size_bits = 64*8 },
360 { SERVICE_REC_FIELD(data8),
361 .offset_words = 28,
362 .offset_bits = 0,
363 .size_bits = 16*8 },
364 { SERVICE_REC_FIELD(data16),
365 .offset_words = 32,
366 .offset_bits = 0,
367 .size_bits = 8*16 },
368 { SERVICE_REC_FIELD(data32),
369 .offset_words = 36,
370 .offset_bits = 0,
371 .size_bits = 4*32 },
372 { SERVICE_REC_FIELD(data64),
373 .offset_words = 40,
374 .offset_bits = 0,
375 .size_bits = 2*64 },
376};
377
344static void free_sm_ah(struct kref *kref) 378static void free_sm_ah(struct kref *kref)
345{ 379{
346 struct ib_sa_sm_ah *sm_ah = container_of(kref, struct ib_sa_sm_ah, ref); 380 struct ib_sa_sm_ah *sm_ah = container_of(kref, struct ib_sa_sm_ah, ref);
@@ -463,7 +497,7 @@ static int send_mad(struct ib_sa_query *query, int timeout_ms)
463 .mad_hdr = &query->mad->mad_hdr, 497 .mad_hdr = &query->mad->mad_hdr,
464 .remote_qpn = 1, 498 .remote_qpn = 1,
465 .remote_qkey = IB_QP1_QKEY, 499 .remote_qkey = IB_QP1_QKEY,
466 .timeout_ms = timeout_ms 500 .timeout_ms = timeout_ms,
467 } 501 }
468 } 502 }
469 }; 503 };
@@ -492,7 +526,7 @@ retry:
492 sizeof (struct ib_sa_mad), 526 sizeof (struct ib_sa_mad),
493 DMA_TO_DEVICE); 527 DMA_TO_DEVICE);
494 gather_list.length = sizeof (struct ib_sa_mad); 528 gather_list.length = sizeof (struct ib_sa_mad);
495 gather_list.lkey = port->mr->lkey; 529 gather_list.lkey = port->agent->mr->lkey;
496 pci_unmap_addr_set(query, mapping, gather_list.addr); 530 pci_unmap_addr_set(query, mapping, gather_list.addr);
497 531
498 ret = ib_post_send_mad(port->agent, &wr, &bad_wr); 532 ret = ib_post_send_mad(port->agent, &wr, &bad_wr);
@@ -566,7 +600,7 @@ static void ib_sa_path_rec_release(struct ib_sa_query *sa_query)
566int ib_sa_path_rec_get(struct ib_device *device, u8 port_num, 600int ib_sa_path_rec_get(struct ib_device *device, u8 port_num,
567 struct ib_sa_path_rec *rec, 601 struct ib_sa_path_rec *rec,
568 ib_sa_comp_mask comp_mask, 602 ib_sa_comp_mask comp_mask,
569 int timeout_ms, int gfp_mask, 603 int timeout_ms, unsigned int __nocast gfp_mask,
570 void (*callback)(int status, 604 void (*callback)(int status,
571 struct ib_sa_path_rec *resp, 605 struct ib_sa_path_rec *resp,
572 void *context), 606 void *context),
@@ -616,6 +650,114 @@ int ib_sa_path_rec_get(struct ib_device *device, u8 port_num,
616} 650}
617EXPORT_SYMBOL(ib_sa_path_rec_get); 651EXPORT_SYMBOL(ib_sa_path_rec_get);
618 652
653static void ib_sa_service_rec_callback(struct ib_sa_query *sa_query,
654 int status,
655 struct ib_sa_mad *mad)
656{
657 struct ib_sa_service_query *query =
658 container_of(sa_query, struct ib_sa_service_query, sa_query);
659
660 if (mad) {
661 struct ib_sa_service_rec rec;
662
663 ib_unpack(service_rec_table, ARRAY_SIZE(service_rec_table),
664 mad->data, &rec);
665 query->callback(status, &rec, query->context);
666 } else
667 query->callback(status, NULL, query->context);
668}
669
670static void ib_sa_service_rec_release(struct ib_sa_query *sa_query)
671{
672 kfree(sa_query->mad);
673 kfree(container_of(sa_query, struct ib_sa_service_query, sa_query));
674}
675
676/**
677 * ib_sa_service_rec_query - Start Service Record operation
678 * @device:device to send request on
679 * @port_num: port number to send request on
680 * @method:SA method - should be get, set, or delete
681 * @rec:Service Record to send in request
682 * @comp_mask:component mask to send in request
683 * @timeout_ms:time to wait for response
684 * @gfp_mask:GFP mask to use for internal allocations
685 * @callback:function called when request completes, times out or is
686 * canceled
687 * @context:opaque user context passed to callback
688 * @sa_query:request context, used to cancel request
689 *
690 * Send a Service Record set/get/delete to the SA to register,
691 * unregister or query a service record.
692 * The callback function will be called when the request completes (or
693 * fails); status is 0 for a successful response, -EINTR if the query
694 * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
695 * occurred sending the query. The resp parameter of the callback is
696 * only valid if status is 0.
697 *
698 * If the return value of ib_sa_service_rec_query() is negative, it is an
699 * error code. Otherwise it is a request ID that can be used to cancel
700 * the query.
701 */
702int ib_sa_service_rec_query(struct ib_device *device, u8 port_num, u8 method,
703 struct ib_sa_service_rec *rec,
704 ib_sa_comp_mask comp_mask,
705 int timeout_ms, unsigned int __nocast gfp_mask,
706 void (*callback)(int status,
707 struct ib_sa_service_rec *resp,
708 void *context),
709 void *context,
710 struct ib_sa_query **sa_query)
711{
712 struct ib_sa_service_query *query;
713 struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
714 struct ib_sa_port *port = &sa_dev->port[port_num - sa_dev->start_port];
715 struct ib_mad_agent *agent = port->agent;
716 int ret;
717
718 if (method != IB_MGMT_METHOD_GET &&
719 method != IB_MGMT_METHOD_SET &&
720 method != IB_SA_METHOD_DELETE)
721 return -EINVAL;
722
723 query = kmalloc(sizeof *query, gfp_mask);
724 if (!query)
725 return -ENOMEM;
726 query->sa_query.mad = kmalloc(sizeof *query->sa_query.mad, gfp_mask);
727 if (!query->sa_query.mad) {
728 kfree(query);
729 return -ENOMEM;
730 }
731
732 query->callback = callback;
733 query->context = context;
734
735 init_mad(query->sa_query.mad, agent);
736
737 query->sa_query.callback = callback ? ib_sa_service_rec_callback : NULL;
738 query->sa_query.release = ib_sa_service_rec_release;
739 query->sa_query.port = port;
740 query->sa_query.mad->mad_hdr.method = method;
741 query->sa_query.mad->mad_hdr.attr_id =
742 cpu_to_be16(IB_SA_ATTR_SERVICE_REC);
743 query->sa_query.mad->sa_hdr.comp_mask = comp_mask;
744
745 ib_pack(service_rec_table, ARRAY_SIZE(service_rec_table),
746 rec, query->sa_query.mad->data);
747
748 *sa_query = &query->sa_query;
749
750 ret = send_mad(&query->sa_query, timeout_ms);
751 if (ret < 0) {
752 *sa_query = NULL;
753 kfree(query->sa_query.mad);
754 kfree(query);
755 }
756
757 return ret;
758}
759EXPORT_SYMBOL(ib_sa_service_rec_query);
760
619static void ib_sa_mcmember_rec_callback(struct ib_sa_query *sa_query, 761static void ib_sa_mcmember_rec_callback(struct ib_sa_query *sa_query,
620 int status, 762 int status,
621 struct ib_sa_mad *mad) 763 struct ib_sa_mad *mad)
@@ -643,7 +785,7 @@ int ib_sa_mcmember_rec_query(struct ib_device *device, u8 port_num,
643 u8 method, 785 u8 method,
644 struct ib_sa_mcmember_rec *rec, 786 struct ib_sa_mcmember_rec *rec,
645 ib_sa_comp_mask comp_mask, 787 ib_sa_comp_mask comp_mask,
646 int timeout_ms, int gfp_mask, 788 int timeout_ms, unsigned int __nocast gfp_mask,
647 void (*callback)(int status, 789 void (*callback)(int status,
648 struct ib_sa_mcmember_rec *resp, 790 struct ib_sa_mcmember_rec *resp,
649 void *context), 791 void *context),
@@ -780,7 +922,6 @@ static void ib_sa_add_one(struct ib_device *device)
780 sa_dev->end_port = e; 922 sa_dev->end_port = e;
781 923
782 for (i = 0; i <= e - s; ++i) { 924 for (i = 0; i <= e - s; ++i) {
783 sa_dev->port[i].mr = NULL;
784 sa_dev->port[i].sm_ah = NULL; 925 sa_dev->port[i].sm_ah = NULL;
785 sa_dev->port[i].port_num = i + s; 926 sa_dev->port[i].port_num = i + s;
786 spin_lock_init(&sa_dev->port[i].ah_lock); 927 spin_lock_init(&sa_dev->port[i].ah_lock);
@@ -792,13 +933,6 @@ static void ib_sa_add_one(struct ib_device *device)
792 if (IS_ERR(sa_dev->port[i].agent)) 933 if (IS_ERR(sa_dev->port[i].agent))
793 goto err; 934 goto err;
794 935
795 sa_dev->port[i].mr = ib_get_dma_mr(sa_dev->port[i].agent->qp->pd,
796 IB_ACCESS_LOCAL_WRITE);
797 if (IS_ERR(sa_dev->port[i].mr)) {
798 ib_unregister_mad_agent(sa_dev->port[i].agent);
799 goto err;
800 }
801
802 INIT_WORK(&sa_dev->port[i].update_task, 936 INIT_WORK(&sa_dev->port[i].update_task,
803 update_sm_ah, &sa_dev->port[i]); 937 update_sm_ah, &sa_dev->port[i]);
804 } 938 }
@@ -822,10 +956,8 @@ static void ib_sa_add_one(struct ib_device *device)
822 return; 956 return;
823 957
824err: 958err:
825 while (--i >= 0) { 959 while (--i >= 0)
826 ib_dereg_mr(sa_dev->port[i].mr);
827 ib_unregister_mad_agent(sa_dev->port[i].agent); 960 ib_unregister_mad_agent(sa_dev->port[i].agent);
828 }
829 961
830 kfree(sa_dev); 962 kfree(sa_dev);
831 963
diff --git a/drivers/infiniband/core/ucm.c b/drivers/infiniband/core/ucm.c
new file mode 100644
index 000000000000..546ec61c407f
--- /dev/null
+++ b/drivers/infiniband/core/ucm.c
@@ -0,0 +1,1393 @@
1/*
2 * Copyright (c) 2005 Topspin Communications. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 *
32 * $Id: ucm.c 2594 2005-06-13 19:46:02Z libor $
33 */
34#include <linux/init.h>
35#include <linux/fs.h>
36#include <linux/module.h>
37#include <linux/device.h>
38#include <linux/err.h>
39#include <linux/poll.h>
40#include <linux/file.h>
41#include <linux/mount.h>
42#include <linux/cdev.h>
43
44#include <asm/uaccess.h>
45
46#include "ucm.h"
47
48MODULE_AUTHOR("Libor Michalek");
49MODULE_DESCRIPTION("InfiniBand userspace Connection Manager access");
50MODULE_LICENSE("Dual BSD/GPL");
51
52enum {
53 IB_UCM_MAJOR = 231,
54 IB_UCM_MINOR = 255
55};
56
57#define IB_UCM_DEV MKDEV(IB_UCM_MAJOR, IB_UCM_MINOR)
58
59static struct semaphore ctx_id_mutex;
60static struct idr ctx_id_table;
61static int ctx_id_rover = 0;
62
63static struct ib_ucm_context *ib_ucm_ctx_get(int id)
64{
65 struct ib_ucm_context *ctx;
66
67 down(&ctx_id_mutex);
68 ctx = idr_find(&ctx_id_table, id);
69 if (ctx)
70 ctx->ref++;
71 up(&ctx_id_mutex);
72
73 return ctx;
74}
75
76static void ib_ucm_ctx_put(struct ib_ucm_context *ctx)
77{
78 struct ib_ucm_event *uevent;
79
80 down(&ctx_id_mutex);
81
82 ctx->ref--;
83 if (!ctx->ref)
84 idr_remove(&ctx_id_table, ctx->id);
85
86 up(&ctx_id_mutex);
87
88 if (ctx->ref)
89 return;
90
91 down(&ctx->file->mutex);
92
93 list_del(&ctx->file_list);
94 while (!list_empty(&ctx->events)) {
95
96 uevent = list_entry(ctx->events.next,
97 struct ib_ucm_event, ctx_list);
98 list_del(&uevent->file_list);
99 list_del(&uevent->ctx_list);
100
101 /* clear incoming connections. */
102 if (uevent->cm_id)
103 ib_destroy_cm_id(uevent->cm_id);
104
105 kfree(uevent);
106 }
107
108 up(&ctx->file->mutex);
109
110 printk(KERN_ERR "UCM: Destroyed CM ID <%d>\n", ctx->id);
111
112 ib_destroy_cm_id(ctx->cm_id);
113 kfree(ctx);
114}
115
116static struct ib_ucm_context *ib_ucm_ctx_alloc(struct ib_ucm_file *file)
117{
118 struct ib_ucm_context *ctx;
119 int result;
120
121 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
122 if (!ctx)
123 return NULL;
124
125 ctx->ref = 1; /* user reference */
126 ctx->file = file;
127
128 INIT_LIST_HEAD(&ctx->events);
129 init_MUTEX(&ctx->mutex);
130
131 list_add_tail(&ctx->file_list, &file->ctxs);
132
133 ctx_id_rover = (ctx_id_rover + 1) & INT_MAX;
134retry:
135 result = idr_pre_get(&ctx_id_table, GFP_KERNEL);
136 if (!result)
137 goto error;
138
139 down(&ctx_id_mutex);
140 result = idr_get_new_above(&ctx_id_table, ctx, ctx_id_rover, &ctx->id);
141 up(&ctx_id_mutex);
142
143 if (result == -EAGAIN)
144 goto retry;
145 if (result)
146 goto error;
147
148 printk(KERN_ERR "UCM: Allocated CM ID <%d>\n", ctx->id);
149
150 return ctx;
151error:
152 list_del(&ctx->file_list);
153 kfree(ctx);
154
155 return NULL;
156}
157/*
158 * Event portion of the API, handle CM events
159 * and allow event polling.
160 */
161static void ib_ucm_event_path_get(struct ib_ucm_path_rec *upath,
162 struct ib_sa_path_rec *kpath)
163{
164 if (!kpath || !upath)
165 return;
166
167 memcpy(upath->dgid, kpath->dgid.raw, sizeof(union ib_gid));
168 memcpy(upath->sgid, kpath->sgid.raw, sizeof(union ib_gid));
169
170 upath->dlid = kpath->dlid;
171 upath->slid = kpath->slid;
172 upath->raw_traffic = kpath->raw_traffic;
173 upath->flow_label = kpath->flow_label;
174 upath->hop_limit = kpath->hop_limit;
175 upath->traffic_class = kpath->traffic_class;
176 upath->reversible = kpath->reversible;
177 upath->numb_path = kpath->numb_path;
178 upath->pkey = kpath->pkey;
179 upath->sl = kpath->sl;
180 upath->mtu_selector = kpath->mtu_selector;
181 upath->mtu = kpath->mtu;
182 upath->rate_selector = kpath->rate_selector;
183 upath->rate = kpath->rate;
184 upath->packet_life_time = kpath->packet_life_time;
185 upath->preference = kpath->preference;
186
187 upath->packet_life_time_selector =
188 kpath->packet_life_time_selector;
189}
190
191static void ib_ucm_event_req_get(struct ib_ucm_req_event_resp *ureq,
192 struct ib_cm_req_event_param *kreq)
193{
194 ureq->listen_id = (long)kreq->listen_id->context;
195
196 ureq->remote_ca_guid = kreq->remote_ca_guid;
197 ureq->remote_qkey = kreq->remote_qkey;
198 ureq->remote_qpn = kreq->remote_qpn;
199 ureq->qp_type = kreq->qp_type;
200 ureq->starting_psn = kreq->starting_psn;
201 ureq->responder_resources = kreq->responder_resources;
202 ureq->initiator_depth = kreq->initiator_depth;
203 ureq->local_cm_response_timeout = kreq->local_cm_response_timeout;
204 ureq->flow_control = kreq->flow_control;
205 ureq->remote_cm_response_timeout = kreq->remote_cm_response_timeout;
206 ureq->retry_count = kreq->retry_count;
207 ureq->rnr_retry_count = kreq->rnr_retry_count;
208 ureq->srq = kreq->srq;
209
210 ib_ucm_event_path_get(&ureq->primary_path, kreq->primary_path);
211 ib_ucm_event_path_get(&ureq->alternate_path, kreq->alternate_path);
212}
213
214static void ib_ucm_event_rep_get(struct ib_ucm_rep_event_resp *urep,
215 struct ib_cm_rep_event_param *krep)
216{
217 urep->remote_ca_guid = krep->remote_ca_guid;
218 urep->remote_qkey = krep->remote_qkey;
219 urep->remote_qpn = krep->remote_qpn;
220 urep->starting_psn = krep->starting_psn;
221 urep->responder_resources = krep->responder_resources;
222 urep->initiator_depth = krep->initiator_depth;
223 urep->target_ack_delay = krep->target_ack_delay;
224 urep->failover_accepted = krep->failover_accepted;
225 urep->flow_control = krep->flow_control;
226 urep->rnr_retry_count = krep->rnr_retry_count;
227 urep->srq = krep->srq;
228}
229
230static void ib_ucm_event_rej_get(struct ib_ucm_rej_event_resp *urej,
231 struct ib_cm_rej_event_param *krej)
232{
233 urej->reason = krej->reason;
234}
235
236static void ib_ucm_event_mra_get(struct ib_ucm_mra_event_resp *umra,
237 struct ib_cm_mra_event_param *kmra)
238{
239 umra->timeout = kmra->service_timeout;
240}
241
242static void ib_ucm_event_lap_get(struct ib_ucm_lap_event_resp *ulap,
243 struct ib_cm_lap_event_param *klap)
244{
245 ib_ucm_event_path_get(&ulap->path, klap->alternate_path);
246}
247
248static void ib_ucm_event_apr_get(struct ib_ucm_apr_event_resp *uapr,
249 struct ib_cm_apr_event_param *kapr)
250{
251 uapr->status = kapr->ap_status;
252}
253
254static void ib_ucm_event_sidr_req_get(struct ib_ucm_sidr_req_event_resp *ureq,
255 struct ib_cm_sidr_req_event_param *kreq)
256{
257 ureq->listen_id = (long)kreq->listen_id->context;
258 ureq->pkey = kreq->pkey;
259}
260
261static void ib_ucm_event_sidr_rep_get(struct ib_ucm_sidr_rep_event_resp *urep,
262 struct ib_cm_sidr_rep_event_param *krep)
263{
264 urep->status = krep->status;
265 urep->qkey = krep->qkey;
266 urep->qpn = krep->qpn;
267};
268
269static int ib_ucm_event_process(struct ib_cm_event *evt,
270 struct ib_ucm_event *uvt)
271{
272 void *info = NULL;
273 int result;
274
275 switch (evt->event) {
276 case IB_CM_REQ_RECEIVED:
277 ib_ucm_event_req_get(&uvt->resp.u.req_resp,
278 &evt->param.req_rcvd);
279 uvt->data_len = IB_CM_REQ_PRIVATE_DATA_SIZE;
280 uvt->resp.present |= (evt->param.req_rcvd.primary_path ?
281 IB_UCM_PRES_PRIMARY : 0);
282 uvt->resp.present |= (evt->param.req_rcvd.alternate_path ?
283 IB_UCM_PRES_ALTERNATE : 0);
284 break;
285 case IB_CM_REP_RECEIVED:
286 ib_ucm_event_rep_get(&uvt->resp.u.rep_resp,
287 &evt->param.rep_rcvd);
288 uvt->data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
289
290 break;
291 case IB_CM_RTU_RECEIVED:
292 uvt->data_len = IB_CM_RTU_PRIVATE_DATA_SIZE;
293 uvt->resp.u.send_status = evt->param.send_status;
294
295 break;
296 case IB_CM_DREQ_RECEIVED:
297 uvt->data_len = IB_CM_DREQ_PRIVATE_DATA_SIZE;
298 uvt->resp.u.send_status = evt->param.send_status;
299
300 break;
301 case IB_CM_DREP_RECEIVED:
302 uvt->data_len = IB_CM_DREP_PRIVATE_DATA_SIZE;
303 uvt->resp.u.send_status = evt->param.send_status;
304
305 break;
306 case IB_CM_MRA_RECEIVED:
307 ib_ucm_event_mra_get(&uvt->resp.u.mra_resp,
308 &evt->param.mra_rcvd);
309 uvt->data_len = IB_CM_MRA_PRIVATE_DATA_SIZE;
310
311 break;
312 case IB_CM_REJ_RECEIVED:
313 ib_ucm_event_rej_get(&uvt->resp.u.rej_resp,
314 &evt->param.rej_rcvd);
315 uvt->data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
316 uvt->info_len = evt->param.rej_rcvd.ari_length;
317 info = evt->param.rej_rcvd.ari;
318
319 break;
320 case IB_CM_LAP_RECEIVED:
321 ib_ucm_event_lap_get(&uvt->resp.u.lap_resp,
322 &evt->param.lap_rcvd);
323 uvt->data_len = IB_CM_LAP_PRIVATE_DATA_SIZE;
324 uvt->resp.present |= (evt->param.lap_rcvd.alternate_path ?
325 IB_UCM_PRES_ALTERNATE : 0);
326 break;
327 case IB_CM_APR_RECEIVED:
328 ib_ucm_event_apr_get(&uvt->resp.u.apr_resp,
329 &evt->param.apr_rcvd);
330 uvt->data_len = IB_CM_APR_PRIVATE_DATA_SIZE;
331 uvt->info_len = evt->param.apr_rcvd.info_len;
332 info = evt->param.apr_rcvd.apr_info;
333
334 break;
335 case IB_CM_SIDR_REQ_RECEIVED:
336 ib_ucm_event_sidr_req_get(&uvt->resp.u.sidr_req_resp,
337 &evt->param.sidr_req_rcvd);
338 uvt->data_len = IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE;
339
340 break;
341 case IB_CM_SIDR_REP_RECEIVED:
342 ib_ucm_event_sidr_rep_get(&uvt->resp.u.sidr_rep_resp,
343 &evt->param.sidr_rep_rcvd);
344 uvt->data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
345 uvt->info_len = evt->param.sidr_rep_rcvd.info_len;
346 info = evt->param.sidr_rep_rcvd.info;
347
348 break;
349 default:
350 uvt->resp.u.send_status = evt->param.send_status;
351
352 break;
353 }
354
355 if (uvt->data_len && evt->private_data) {
356
357 uvt->data = kmalloc(uvt->data_len, GFP_KERNEL);
358 if (!uvt->data) {
359 result = -ENOMEM;
360 goto error;
361 }
362
363 memcpy(uvt->data, evt->private_data, uvt->data_len);
364 uvt->resp.present |= IB_UCM_PRES_DATA;
365 }
366
367 if (uvt->info_len && info) {
368
369 uvt->info = kmalloc(uvt->info_len, GFP_KERNEL);
370 if (!uvt->info) {
371 result = -ENOMEM;
372 goto error;
373 }
374
375 memcpy(uvt->info, info, uvt->info_len);
376 uvt->resp.present |= IB_UCM_PRES_INFO;
377 }
378
379 return 0;
380error:
381 if (uvt->info)
382 kfree(uvt->info);
383 if (uvt->data)
384 kfree(uvt->data);
385 return result;
386}
387
388static int ib_ucm_event_handler(struct ib_cm_id *cm_id,
389 struct ib_cm_event *event)
390{
391 struct ib_ucm_event *uevent;
392 struct ib_ucm_context *ctx;
393 int result = 0;
394 int id;
395 /*
396 * lookup correct context based on event type.
397 */
398 switch (event->event) {
399 case IB_CM_REQ_RECEIVED:
400 id = (long)event->param.req_rcvd.listen_id->context;
401 break;
402 case IB_CM_SIDR_REQ_RECEIVED:
403 id = (long)event->param.sidr_req_rcvd.listen_id->context;
404 break;
405 default:
406 id = (long)cm_id->context;
407 break;
408 }
409
410 printk(KERN_ERR "UCM: Event. CM ID <%d> event <%d>\n",
411 id, event->event);
412
413 ctx = ib_ucm_ctx_get(id);
414 if (!ctx)
415 return -ENOENT;
416
417 if (event->event == IB_CM_REQ_RECEIVED ||
418 event->event == IB_CM_SIDR_REQ_RECEIVED)
419 id = IB_UCM_CM_ID_INVALID;
420
421 uevent = kmalloc(sizeof(*uevent), GFP_KERNEL);
422 if (!uevent) {
423 result = -ENOMEM;
424 goto done;
425 }
426
427 memset(uevent, 0, sizeof(*uevent));
428
429 uevent->resp.id = id;
430 uevent->resp.event = event->event;
431
432 result = ib_ucm_event_process(event, uevent);
433 if (result)
434 goto done;
435
436 uevent->ctx = ctx;
437 uevent->cm_id = ((event->event == IB_CM_REQ_RECEIVED ||
438 event->event == IB_CM_SIDR_REQ_RECEIVED ) ?
439 cm_id : NULL);
440
441 down(&ctx->file->mutex);
442
443 list_add_tail(&uevent->file_list, &ctx->file->events);
444 list_add_tail(&uevent->ctx_list, &ctx->events);
445
446 wake_up_interruptible(&ctx->file->poll_wait);
447
448 up(&ctx->file->mutex);
449done:
450 ctx->error = result;
451 ib_ucm_ctx_put(ctx); /* func reference */
452 return result;
453}
454
455static ssize_t ib_ucm_event(struct ib_ucm_file *file,
456 const char __user *inbuf,
457 int in_len, int out_len)
458{
459 struct ib_ucm_context *ctx;
460 struct ib_ucm_event_get cmd;
461 struct ib_ucm_event *uevent = NULL;
462 int result = 0;
463 DEFINE_WAIT(wait);
464
465 if (out_len < sizeof(struct ib_ucm_event_resp))
466 return -ENOSPC;
467
468 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
469 return -EFAULT;
470 /*
471 * wait
472 */
473 down(&file->mutex);
474
475 while (list_empty(&file->events)) {
476
477 if (file->filp->f_flags & O_NONBLOCK) {
478 result = -EAGAIN;
479 break;
480 }
481
482 if (signal_pending(current)) {
483 result = -ERESTARTSYS;
484 break;
485 }
486
487 prepare_to_wait(&file->poll_wait, &wait, TASK_INTERRUPTIBLE);
488
489 up(&file->mutex);
490 schedule();
491 down(&file->mutex);
492
493 finish_wait(&file->poll_wait, &wait);
494 }
495
496 if (result)
497 goto done;
498
499 uevent = list_entry(file->events.next, struct ib_ucm_event, file_list);
500
501 if (!uevent->cm_id)
502 goto user;
503
504 ctx = ib_ucm_ctx_alloc(file);
505 if (!ctx) {
506 result = -ENOMEM;
507 goto done;
508 }
509
510 ctx->cm_id = uevent->cm_id;
511 ctx->cm_id->cm_handler = ib_ucm_event_handler;
512 ctx->cm_id->context = (void *)(unsigned long)ctx->id;
513
514 uevent->resp.id = ctx->id;
515
516user:
517 if (copy_to_user((void __user *)(unsigned long)cmd.response,
518 &uevent->resp, sizeof(uevent->resp))) {
519 result = -EFAULT;
520 goto done;
521 }
522
523 if (uevent->data) {
524
525 if (cmd.data_len < uevent->data_len) {
526 result = -ENOMEM;
527 goto done;
528 }
529
530 if (copy_to_user((void __user *)(unsigned long)cmd.data,
531 uevent->data, uevent->data_len)) {
532 result = -EFAULT;
533 goto done;
534 }
535 }
536
537 if (uevent->info) {
538
539 if (cmd.info_len < uevent->info_len) {
540 result = -ENOMEM;
541 goto done;
542 }
543
544 if (copy_to_user((void __user *)(unsigned long)cmd.info,
545 uevent->info, uevent->info_len)) {
546 result = -EFAULT;
547 goto done;
548 }
549 }
550
551 list_del(&uevent->file_list);
552 list_del(&uevent->ctx_list);
553
554 if (uevent->data)
555 kfree(uevent->data);
556 if (uevent->info)
557 kfree(uevent->info);
558 kfree(uevent);
559done:
560 up(&file->mutex);
561 return result;
562}
563
564
565static ssize_t ib_ucm_create_id(struct ib_ucm_file *file,
566 const char __user *inbuf,
567 int in_len, int out_len)
568{
569 struct ib_ucm_create_id cmd;
570 struct ib_ucm_create_id_resp resp;
571 struct ib_ucm_context *ctx;
572 int result;
573
574 if (out_len < sizeof(resp))
575 return -ENOSPC;
576
577 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
578 return -EFAULT;
579
580 ctx = ib_ucm_ctx_alloc(file);
581 if (!ctx)
582 return -ENOMEM;
583
584 ctx->cm_id = ib_create_cm_id(ib_ucm_event_handler,
585 (void *)(unsigned long)ctx->id);
586 if (!ctx->cm_id) {
587 result = -ENOMEM;
588 goto err_cm;
589 }
590
591 resp.id = ctx->id;
592 if (copy_to_user((void __user *)(unsigned long)cmd.response,
593 &resp, sizeof(resp))) {
594 result = -EFAULT;
595 goto err_ret;
596 }
597
598 return 0;
599err_ret:
600 ib_destroy_cm_id(ctx->cm_id);
601err_cm:
602 ib_ucm_ctx_put(ctx); /* user reference */
603
604 return result;
605}
606
607static ssize_t ib_ucm_destroy_id(struct ib_ucm_file *file,
608 const char __user *inbuf,
609 int in_len, int out_len)
610{
611 struct ib_ucm_destroy_id cmd;
612 struct ib_ucm_context *ctx;
613
614 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
615 return -EFAULT;
616
617 ctx = ib_ucm_ctx_get(cmd.id);
618 if (!ctx)
619 return -ENOENT;
620
621 ib_ucm_ctx_put(ctx); /* user reference */
622 ib_ucm_ctx_put(ctx); /* func reference */
623
624 return 0;
625}
626
627static ssize_t ib_ucm_attr_id(struct ib_ucm_file *file,
628 const char __user *inbuf,
629 int in_len, int out_len)
630{
631 struct ib_ucm_attr_id_resp resp;
632 struct ib_ucm_attr_id cmd;
633 struct ib_ucm_context *ctx;
634 int result = 0;
635
636 if (out_len < sizeof(resp))
637 return -ENOSPC;
638
639 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
640 return -EFAULT;
641
642 ctx = ib_ucm_ctx_get(cmd.id);
643 if (!ctx)
644 return -ENOENT;
645
646 down(&ctx->file->mutex);
647 if (ctx->file != file) {
648 result = -EINVAL;
649 goto done;
650 }
651
652 resp.service_id = ctx->cm_id->service_id;
653 resp.service_mask = ctx->cm_id->service_mask;
654 resp.local_id = ctx->cm_id->local_id;
655 resp.remote_id = ctx->cm_id->remote_id;
656
657 if (copy_to_user((void __user *)(unsigned long)cmd.response,
658 &resp, sizeof(resp)))
659 result = -EFAULT;
660
661done:
662 up(&ctx->file->mutex);
663 ib_ucm_ctx_put(ctx); /* func reference */
664 return result;
665}
666
667static ssize_t ib_ucm_listen(struct ib_ucm_file *file,
668 const char __user *inbuf,
669 int in_len, int out_len)
670{
671 struct ib_ucm_listen cmd;
672 struct ib_ucm_context *ctx;
673 int result;
674
675 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
676 return -EFAULT;
677
678 ctx = ib_ucm_ctx_get(cmd.id);
679 if (!ctx)
680 return -ENOENT;
681
682 down(&ctx->file->mutex);
683 if (ctx->file != file)
684 result = -EINVAL;
685 else
686 result = ib_cm_listen(ctx->cm_id, cmd.service_id,
687 cmd.service_mask);
688
689 up(&ctx->file->mutex);
690 ib_ucm_ctx_put(ctx); /* func reference */
691 return result;
692}
693
694static ssize_t ib_ucm_establish(struct ib_ucm_file *file,
695 const char __user *inbuf,
696 int in_len, int out_len)
697{
698 struct ib_ucm_establish cmd;
699 struct ib_ucm_context *ctx;
700 int result;
701
702 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
703 return -EFAULT;
704
705 ctx = ib_ucm_ctx_get(cmd.id);
706 if (!ctx)
707 return -ENOENT;
708
709 down(&ctx->file->mutex);
710 if (ctx->file != file)
711 result = -EINVAL;
712 else
713 result = ib_cm_establish(ctx->cm_id);
714
715 up(&ctx->file->mutex);
716 ib_ucm_ctx_put(ctx); /* func reference */
717 return result;
718}
719
720static int ib_ucm_alloc_data(const void **dest, u64 src, u32 len)
721{
722 void *data;
723
724 *dest = NULL;
725
726 if (!len)
727 return 0;
728
729 data = kmalloc(len, GFP_KERNEL);
730 if (!data)
731 return -ENOMEM;
732
733 if (copy_from_user(data, (void __user *)(unsigned long)src, len)) {
734 kfree(data);
735 return -EFAULT;
736 }
737
738 *dest = data;
739 return 0;
740}
741
742static int ib_ucm_path_get(struct ib_sa_path_rec **path, u64 src)
743{
744 struct ib_ucm_path_rec ucm_path;
745 struct ib_sa_path_rec *sa_path;
746
747 *path = NULL;
748
749 if (!src)
750 return 0;
751
752 sa_path = kmalloc(sizeof(*sa_path), GFP_KERNEL);
753 if (!sa_path)
754 return -ENOMEM;
755
756 if (copy_from_user(&ucm_path, (void __user *)(unsigned long)src,
757 sizeof(ucm_path))) {
758
759 kfree(sa_path);
760 return -EFAULT;
761 }
762
763 memcpy(sa_path->dgid.raw, ucm_path.dgid, sizeof(union ib_gid));
764 memcpy(sa_path->sgid.raw, ucm_path.sgid, sizeof(union ib_gid));
765
766 sa_path->dlid = ucm_path.dlid;
767 sa_path->slid = ucm_path.slid;
768 sa_path->raw_traffic = ucm_path.raw_traffic;
769 sa_path->flow_label = ucm_path.flow_label;
770 sa_path->hop_limit = ucm_path.hop_limit;
771 sa_path->traffic_class = ucm_path.traffic_class;
772 sa_path->reversible = ucm_path.reversible;
773 sa_path->numb_path = ucm_path.numb_path;
774 sa_path->pkey = ucm_path.pkey;
775 sa_path->sl = ucm_path.sl;
776 sa_path->mtu_selector = ucm_path.mtu_selector;
777 sa_path->mtu = ucm_path.mtu;
778 sa_path->rate_selector = ucm_path.rate_selector;
779 sa_path->rate = ucm_path.rate;
780 sa_path->packet_life_time = ucm_path.packet_life_time;
781 sa_path->preference = ucm_path.preference;
782
783 sa_path->packet_life_time_selector =
784 ucm_path.packet_life_time_selector;
785
786 *path = sa_path;
787 return 0;
788}
789
790static ssize_t ib_ucm_send_req(struct ib_ucm_file *file,
791 const char __user *inbuf,
792 int in_len, int out_len)
793{
794 struct ib_cm_req_param param;
795 struct ib_ucm_context *ctx;
796 struct ib_ucm_req cmd;
797 int result;
798
799 param.private_data = NULL;
800 param.primary_path = NULL;
801 param.alternate_path = NULL;
802
803 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
804 return -EFAULT;
805
806 result = ib_ucm_alloc_data(&param.private_data, cmd.data, cmd.len);
807 if (result)
808 goto done;
809
810 result = ib_ucm_path_get(&param.primary_path, cmd.primary_path);
811 if (result)
812 goto done;
813
814 result = ib_ucm_path_get(&param.alternate_path, cmd.alternate_path);
815 if (result)
816 goto done;
817
818 param.private_data_len = cmd.len;
819 param.service_id = cmd.sid;
820 param.qp_num = cmd.qpn;
821 param.qp_type = cmd.qp_type;
822 param.starting_psn = cmd.psn;
823 param.peer_to_peer = cmd.peer_to_peer;
824 param.responder_resources = cmd.responder_resources;
825 param.initiator_depth = cmd.initiator_depth;
826 param.remote_cm_response_timeout = cmd.remote_cm_response_timeout;
827 param.flow_control = cmd.flow_control;
828 param.local_cm_response_timeout = cmd.local_cm_response_timeout;
829 param.retry_count = cmd.retry_count;
830 param.rnr_retry_count = cmd.rnr_retry_count;
831 param.max_cm_retries = cmd.max_cm_retries;
832 param.srq = cmd.srq;
833
834 ctx = ib_ucm_ctx_get(cmd.id);
835 if (!ctx) {
836 result = -ENOENT;
837 goto done;
838 }
839
840 down(&ctx->file->mutex);
841 if (ctx->file != file)
842 result = -EINVAL;
843 else
844 result = ib_send_cm_req(ctx->cm_id, &param);
845
846 up(&ctx->file->mutex);
847 ib_ucm_ctx_put(ctx); /* func reference */
848done:
849 if (param.private_data)
850 kfree(param.private_data);
851 if (param.primary_path)
852 kfree(param.primary_path);
853 if (param.alternate_path)
854 kfree(param.alternate_path);
855
856 return result;
857}
858
859static ssize_t ib_ucm_send_rep(struct ib_ucm_file *file,
860 const char __user *inbuf,
861 int in_len, int out_len)
862{
863 struct ib_cm_rep_param param;
864 struct ib_ucm_context *ctx;
865 struct ib_ucm_rep cmd;
866 int result;
867
868 param.private_data = NULL;
869
870 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
871 return -EFAULT;
872
873 result = ib_ucm_alloc_data(&param.private_data, cmd.data, cmd.len);
874 if (result)
875 return result;
876
877 param.qp_num = cmd.qpn;
878 param.starting_psn = cmd.psn;
879 param.private_data_len = cmd.len;
880 param.responder_resources = cmd.responder_resources;
881 param.initiator_depth = cmd.initiator_depth;
882 param.target_ack_delay = cmd.target_ack_delay;
883 param.failover_accepted = cmd.failover_accepted;
884 param.flow_control = cmd.flow_control;
885 param.rnr_retry_count = cmd.rnr_retry_count;
886 param.srq = cmd.srq;
887
888 ctx = ib_ucm_ctx_get(cmd.id);
889 if (!ctx) {
890 result = -ENOENT;
891 goto done;
892 }
893
894 down(&ctx->file->mutex);
895 if (ctx->file != file)
896 result = -EINVAL;
897 else
898 result = ib_send_cm_rep(ctx->cm_id, &param);
899
900 up(&ctx->file->mutex);
901 ib_ucm_ctx_put(ctx); /* func reference */
902done:
903 if (param.private_data)
904 kfree(param.private_data);
905
906 return result;
907}
908
909static ssize_t ib_ucm_send_private_data(struct ib_ucm_file *file,
910 const char __user *inbuf, int in_len,
911 int (*func)(struct ib_cm_id *cm_id,
912 const void *private_data,
913 u8 private_data_len))
914{
915 struct ib_ucm_private_data cmd;
916 struct ib_ucm_context *ctx;
917 const void *private_data = NULL;
918 int result;
919
920 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
921 return -EFAULT;
922
923 result = ib_ucm_alloc_data(&private_data, cmd.data, cmd.len);
924 if (result)
925 return result;
926
927 ctx = ib_ucm_ctx_get(cmd.id);
928 if (!ctx) {
929 result = -ENOENT;
930 goto done;
931 }
932
933 down(&ctx->file->mutex);
934 if (ctx->file != file)
935 result = -EINVAL;
936 else
937 result = func(ctx->cm_id, private_data, cmd.len);
938
939 up(&ctx->file->mutex);
940 ib_ucm_ctx_put(ctx); /* func reference */
941done:
942 if (private_data)
943 kfree(private_data);
944
945 return result;
946}
947
948static ssize_t ib_ucm_send_rtu(struct ib_ucm_file *file,
949 const char __user *inbuf,
950 int in_len, int out_len)
951{
952 return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_rtu);
953}
954
955static ssize_t ib_ucm_send_dreq(struct ib_ucm_file *file,
956 const char __user *inbuf,
957 int in_len, int out_len)
958{
959 return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_dreq);
960}
961
962static ssize_t ib_ucm_send_drep(struct ib_ucm_file *file,
963 const char __user *inbuf,
964 int in_len, int out_len)
965{
966 return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_drep);
967}
968
969static ssize_t ib_ucm_send_info(struct ib_ucm_file *file,
970 const char __user *inbuf, int in_len,
971 int (*func)(struct ib_cm_id *cm_id,
972 int status,
973 const void *info,
974 u8 info_len,
975 const void *data,
976 u8 data_len))
977{
978 struct ib_ucm_context *ctx;
979 struct ib_ucm_info cmd;
980 const void *data = NULL;
981 const void *info = NULL;
982 int result;
983
984 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
985 return -EFAULT;
986
987 result = ib_ucm_alloc_data(&data, cmd.data, cmd.data_len);
988 if (result)
989 goto done;
990
991 result = ib_ucm_alloc_data(&info, cmd.info, cmd.info_len);
992 if (result)
993 goto done;
994
995 ctx = ib_ucm_ctx_get(cmd.id);
996 if (!ctx) {
997 result = -ENOENT;
998 goto done;
999 }
1000
1001 down(&ctx->file->mutex);
1002 if (ctx->file != file)
1003 result = -EINVAL;
1004 else
1005 result = func(ctx->cm_id, cmd.status,
1006 info, cmd.info_len,
1007 data, cmd.data_len);
1008
1009 up(&ctx->file->mutex);
1010 ib_ucm_ctx_put(ctx); /* func reference */
1011done:
1012 if (data)
1013 kfree(data);
1014 if (info)
1015 kfree(info);
1016
1017 return result;
1018}
1019
1020static ssize_t ib_ucm_send_rej(struct ib_ucm_file *file,
1021 const char __user *inbuf,
1022 int in_len, int out_len)
1023{
1024 return ib_ucm_send_info(file, inbuf, in_len, (void *)ib_send_cm_rej);
1025}
1026
1027static ssize_t ib_ucm_send_apr(struct ib_ucm_file *file,
1028 const char __user *inbuf,
1029 int in_len, int out_len)
1030{
1031 return ib_ucm_send_info(file, inbuf, in_len, (void *)ib_send_cm_apr);
1032}
1033
1034static ssize_t ib_ucm_send_mra(struct ib_ucm_file *file,
1035 const char __user *inbuf,
1036 int in_len, int out_len)
1037{
1038 struct ib_ucm_context *ctx;
1039 struct ib_ucm_mra cmd;
1040 const void *data = NULL;
1041 int result;
1042
1043 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1044 return -EFAULT;
1045
1046 result = ib_ucm_alloc_data(&data, cmd.data, cmd.len);
1047 if (result)
1048 return result;
1049
1050 ctx = ib_ucm_ctx_get(cmd.id);
1051 if (!ctx) {
1052 result = -ENOENT;
1053 goto done;
1054 }
1055
1056 down(&ctx->file->mutex);
1057 if (ctx->file != file)
1058 result = -EINVAL;
1059 else
1060 result = ib_send_cm_mra(ctx->cm_id, cmd.timeout,
1061 data, cmd.len);
1062
1063 up(&ctx->file->mutex);
1064 ib_ucm_ctx_put(ctx); /* func reference */
1065done:
1066 if (data)
1067 kfree(data);
1068
1069 return result;
1070}
1071
1072static ssize_t ib_ucm_send_lap(struct ib_ucm_file *file,
1073 const char __user *inbuf,
1074 int in_len, int out_len)
1075{
1076 struct ib_ucm_context *ctx;
1077 struct ib_sa_path_rec *path = NULL;
1078 struct ib_ucm_lap cmd;
1079 const void *data = NULL;
1080 int result;
1081
1082 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1083 return -EFAULT;
1084
1085 result = ib_ucm_alloc_data(&data, cmd.data, cmd.len);
1086 if (result)
1087 goto done;
1088
1089 result = ib_ucm_path_get(&path, cmd.path);
1090 if (result)
1091 goto done;
1092
1093 ctx = ib_ucm_ctx_get(cmd.id);
1094 if (!ctx) {
1095 result = -ENOENT;
1096 goto done;
1097 }
1098
1099 down(&ctx->file->mutex);
1100 if (ctx->file != file)
1101 result = -EINVAL;
1102 else
1103 result = ib_send_cm_lap(ctx->cm_id, path, data, cmd.len);
1104
1105 up(&ctx->file->mutex);
1106 ib_ucm_ctx_put(ctx); /* func reference */
1107done:
1108 if (data)
1109 kfree(data);
1110 if (path)
1111 kfree(path);
1112
1113 return result;
1114}
1115
1116static ssize_t ib_ucm_send_sidr_req(struct ib_ucm_file *file,
1117 const char __user *inbuf,
1118 int in_len, int out_len)
1119{
1120 struct ib_cm_sidr_req_param param;
1121 struct ib_ucm_context *ctx;
1122 struct ib_ucm_sidr_req cmd;
1123 int result;
1124
1125 param.private_data = NULL;
1126 param.path = NULL;
1127
1128 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1129 return -EFAULT;
1130
1131 result = ib_ucm_alloc_data(&param.private_data, cmd.data, cmd.len);
1132 if (result)
1133 goto done;
1134
1135 result = ib_ucm_path_get(&param.path, cmd.path);
1136 if (result)
1137 goto done;
1138
1139 param.private_data_len = cmd.len;
1140 param.service_id = cmd.sid;
1141 param.timeout_ms = cmd.timeout;
1142 param.max_cm_retries = cmd.max_cm_retries;
1143 param.pkey = cmd.pkey;
1144
1145 ctx = ib_ucm_ctx_get(cmd.id);
1146 if (!ctx) {
1147 result = -ENOENT;
1148 goto done;
1149 }
1150
1151 down(&ctx->file->mutex);
1152 if (ctx->file != file)
1153 result = -EINVAL;
1154 else
1155 result = ib_send_cm_sidr_req(ctx->cm_id, &param);
1156
1157 up(&ctx->file->mutex);
1158 ib_ucm_ctx_put(ctx); /* func reference */
1159done:
1160 if (param.private_data)
1161 kfree(param.private_data);
1162 if (param.path)
1163 kfree(param.path);
1164
1165 return result;
1166}
1167
1168static ssize_t ib_ucm_send_sidr_rep(struct ib_ucm_file *file,
1169 const char __user *inbuf,
1170 int in_len, int out_len)
1171{
1172 struct ib_cm_sidr_rep_param param;
1173 struct ib_ucm_sidr_rep cmd;
1174 struct ib_ucm_context *ctx;
1175 int result;
1176
1177 param.info = NULL;
1178
1179 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1180 return -EFAULT;
1181
1182 result = ib_ucm_alloc_data(&param.private_data,
1183 cmd.data, cmd.data_len);
1184 if (result)
1185 goto done;
1186
1187 result = ib_ucm_alloc_data(&param.info, cmd.info, cmd.info_len);
1188 if (result)
1189 goto done;
1190
1191 param.qp_num = cmd.qpn;
1192 param.qkey = cmd.qkey;
1193 param.status = cmd.status;
1194 param.info_length = cmd.info_len;
1195 param.private_data_len = cmd.data_len;
1196
1197 ctx = ib_ucm_ctx_get(cmd.id);
1198 if (!ctx) {
1199 result = -ENOENT;
1200 goto done;
1201 }
1202
1203 down(&ctx->file->mutex);
1204 if (ctx->file != file)
1205 result = -EINVAL;
1206 else
1207 result = ib_send_cm_sidr_rep(ctx->cm_id, &param);
1208
1209 up(&ctx->file->mutex);
1210 ib_ucm_ctx_put(ctx); /* func reference */
1211done:
1212 if (param.private_data)
1213 kfree(param.private_data);
1214 if (param.info)
1215 kfree(param.info);
1216
1217 return result;
1218}
1219
1220static ssize_t (*ucm_cmd_table[])(struct ib_ucm_file *file,
1221 const char __user *inbuf,
1222 int in_len, int out_len) = {
1223 [IB_USER_CM_CMD_CREATE_ID] = ib_ucm_create_id,
1224 [IB_USER_CM_CMD_DESTROY_ID] = ib_ucm_destroy_id,
1225 [IB_USER_CM_CMD_ATTR_ID] = ib_ucm_attr_id,
1226 [IB_USER_CM_CMD_LISTEN] = ib_ucm_listen,
1227 [IB_USER_CM_CMD_ESTABLISH] = ib_ucm_establish,
1228 [IB_USER_CM_CMD_SEND_REQ] = ib_ucm_send_req,
1229 [IB_USER_CM_CMD_SEND_REP] = ib_ucm_send_rep,
1230 [IB_USER_CM_CMD_SEND_RTU] = ib_ucm_send_rtu,
1231 [IB_USER_CM_CMD_SEND_DREQ] = ib_ucm_send_dreq,
1232 [IB_USER_CM_CMD_SEND_DREP] = ib_ucm_send_drep,
1233 [IB_USER_CM_CMD_SEND_REJ] = ib_ucm_send_rej,
1234 [IB_USER_CM_CMD_SEND_MRA] = ib_ucm_send_mra,
1235 [IB_USER_CM_CMD_SEND_LAP] = ib_ucm_send_lap,
1236 [IB_USER_CM_CMD_SEND_APR] = ib_ucm_send_apr,
1237 [IB_USER_CM_CMD_SEND_SIDR_REQ] = ib_ucm_send_sidr_req,
1238 [IB_USER_CM_CMD_SEND_SIDR_REP] = ib_ucm_send_sidr_rep,
1239 [IB_USER_CM_CMD_EVENT] = ib_ucm_event,
1240};
1241
1242static ssize_t ib_ucm_write(struct file *filp, const char __user *buf,
1243 size_t len, loff_t *pos)
1244{
1245 struct ib_ucm_file *file = filp->private_data;
1246 struct ib_ucm_cmd_hdr hdr;
1247 ssize_t result;
1248
1249 if (len < sizeof(hdr))
1250 return -EINVAL;
1251
1252 if (copy_from_user(&hdr, buf, sizeof(hdr)))
1253 return -EFAULT;
1254
1255 printk(KERN_ERR "UCM: Write. cmd <%d> in <%d> out <%d> len <%Zu>\n",
1256 hdr.cmd, hdr.in, hdr.out, len);
1257
1258 if (hdr.cmd < 0 || hdr.cmd >= ARRAY_SIZE(ucm_cmd_table))
1259 return -EINVAL;
1260
1261 if (hdr.in + sizeof(hdr) > len)
1262 return -EINVAL;
1263
1264 result = ucm_cmd_table[hdr.cmd](file, buf + sizeof(hdr),
1265 hdr.in, hdr.out);
1266 if (!result)
1267 result = len;
1268
1269 return result;
1270}
1271
1272static unsigned int ib_ucm_poll(struct file *filp,
1273 struct poll_table_struct *wait)
1274{
1275 struct ib_ucm_file *file = filp->private_data;
1276 unsigned int mask = 0;
1277
1278 poll_wait(filp, &file->poll_wait, wait);
1279
1280 if (!list_empty(&file->events))
1281 mask = POLLIN | POLLRDNORM;
1282
1283 return mask;
1284}
1285
1286static int ib_ucm_open(struct inode *inode, struct file *filp)
1287{
1288 struct ib_ucm_file *file;
1289
1290 file = kmalloc(sizeof(*file), GFP_KERNEL);
1291 if (!file)
1292 return -ENOMEM;
1293
1294 INIT_LIST_HEAD(&file->events);
1295 INIT_LIST_HEAD(&file->ctxs);
1296 init_waitqueue_head(&file->poll_wait);
1297
1298 init_MUTEX(&file->mutex);
1299
1300 filp->private_data = file;
1301 file->filp = filp;
1302
1303 printk(KERN_ERR "UCM: Created struct\n");
1304
1305 return 0;
1306}
1307
1308static int ib_ucm_close(struct inode *inode, struct file *filp)
1309{
1310 struct ib_ucm_file *file = filp->private_data;
1311 struct ib_ucm_context *ctx;
1312
1313 down(&file->mutex);
1314
1315 while (!list_empty(&file->ctxs)) {
1316
1317 ctx = list_entry(file->ctxs.next,
1318 struct ib_ucm_context, file_list);
1319
1320 up(&ctx->file->mutex);
1321 ib_ucm_ctx_put(ctx); /* user reference */
1322 down(&file->mutex);
1323 }
1324
1325 up(&file->mutex);
1326
1327 kfree(file);
1328
1329 printk(KERN_ERR "UCM: Deleted struct\n");
1330 return 0;
1331}
1332
1333static struct file_operations ib_ucm_fops = {
1334 .owner = THIS_MODULE,
1335 .open = ib_ucm_open,
1336 .release = ib_ucm_close,
1337 .write = ib_ucm_write,
1338 .poll = ib_ucm_poll,
1339};
1340
1341
1342static struct class *ib_ucm_class;
1343static struct cdev ib_ucm_cdev;
1344
1345static int __init ib_ucm_init(void)
1346{
1347 int result;
1348
1349 result = register_chrdev_region(IB_UCM_DEV, 1, "infiniband_cm");
1350 if (result) {
1351 printk(KERN_ERR "UCM: Error <%d> registering dev\n", result);
1352 goto err_chr;
1353 }
1354
1355 cdev_init(&ib_ucm_cdev, &ib_ucm_fops);
1356
1357 result = cdev_add(&ib_ucm_cdev, IB_UCM_DEV, 1);
1358 if (result) {
1359 printk(KERN_ERR "UCM: Error <%d> adding cdev\n", result);
1360 goto err_cdev;
1361 }
1362
1363 ib_ucm_class = class_create(THIS_MODULE, "infiniband_cm");
1364 if (IS_ERR(ib_ucm_class)) {
1365 result = PTR_ERR(ib_ucm_class);
1366 printk(KERN_ERR "UCM: Error <%d> creating class\n", result);
1367 goto err_class;
1368 }
1369
1370 class_device_create(ib_ucm_class, IB_UCM_DEV, NULL, "ucm");
1371
1372 idr_init(&ctx_id_table);
1373 init_MUTEX(&ctx_id_mutex);
1374
1375 return 0;
1376err_class:
1377 cdev_del(&ib_ucm_cdev);
1378err_cdev:
1379 unregister_chrdev_region(IB_UCM_DEV, 1);
1380err_chr:
1381 return result;
1382}
1383
1384static void __exit ib_ucm_cleanup(void)
1385{
1386 class_device_destroy(ib_ucm_class, IB_UCM_DEV);
1387 class_destroy(ib_ucm_class);
1388 cdev_del(&ib_ucm_cdev);
1389 unregister_chrdev_region(IB_UCM_DEV, 1);
1390}
1391
1392module_init(ib_ucm_init);
1393module_exit(ib_ucm_cleanup);
diff --git a/drivers/infiniband/core/ucm.h b/drivers/infiniband/core/ucm.h
new file mode 100644
index 000000000000..6d36606151b2
--- /dev/null
+++ b/drivers/infiniband/core/ucm.h
@@ -0,0 +1,89 @@
1/*
2 * Copyright (c) 2005 Topspin Communications. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 *
32 * $Id: ucm.h 2208 2005-04-22 23:24:31Z libor $
33 */
34
35#ifndef UCM_H
36#define UCM_H
37
38#include <linux/fs.h>
39#include <linux/device.h>
40#include <linux/cdev.h>
41#include <linux/idr.h>
42
43#include <ib_cm.h>
44#include <ib_user_cm.h>
45
46#define IB_UCM_CM_ID_INVALID 0xffffffff
47
48struct ib_ucm_file {
49 struct semaphore mutex;
50 struct file *filp;
51 /*
52 * list of pending events
53 */
54 struct list_head ctxs; /* list of active connections */
55 struct list_head events; /* list of pending events */
56 wait_queue_head_t poll_wait;
57};
58
59struct ib_ucm_context {
60 int id;
61 int ref;
62 int error;
63
64 struct ib_ucm_file *file;
65 struct ib_cm_id *cm_id;
66 struct semaphore mutex;
67
68 struct list_head events; /* list of pending events. */
69 struct list_head file_list; /* member in file ctx list */
70};
71
72struct ib_ucm_event {
73 struct ib_ucm_context *ctx;
74 struct list_head file_list; /* member in file event list */
75 struct list_head ctx_list; /* member in ctx event list */
76
77 struct ib_ucm_event_resp resp;
78 void *data;
79 void *info;
80 int data_len;
81 int info_len;
82 /*
83 * new connection identifiers needs to be saved until
84 * userspace can get a handle on them.
85 */
86 struct ib_cm_id *cm_id;
87};
88
89#endif /* UCM_H */
diff --git a/drivers/infiniband/core/user_mad.c b/drivers/infiniband/core/user_mad.c
index 9d912d6877ff..2e38792df533 100644
--- a/drivers/infiniband/core/user_mad.c
+++ b/drivers/infiniband/core/user_mad.c
@@ -1,5 +1,7 @@
1/* 1/*
2 * Copyright (c) 2004 Topspin Communications. All rights reserved. 2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Voltaire, Inc. All rights reserved.
4 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
3 * 5 *
4 * This software is available to you under a choice of one of two 6 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU 7 * licenses. You may choose to be licensed under the terms of the GNU
@@ -29,7 +31,7 @@
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE. 32 * SOFTWARE.
31 * 33 *
32 * $Id: user_mad.c 1389 2004-12-27 22:56:47Z roland $ 34 * $Id: user_mad.c 2814 2005-07-06 19:14:09Z halr $
33 */ 35 */
34 36
35#include <linux/module.h> 37#include <linux/module.h>
@@ -94,10 +96,12 @@ struct ib_umad_file {
94}; 96};
95 97
96struct ib_umad_packet { 98struct ib_umad_packet {
97 struct ib_user_mad mad;
98 struct ib_ah *ah; 99 struct ib_ah *ah;
100 struct ib_mad_send_buf *msg;
99 struct list_head list; 101 struct list_head list;
102 int length;
100 DECLARE_PCI_UNMAP_ADDR(mapping) 103 DECLARE_PCI_UNMAP_ADDR(mapping)
104 struct ib_user_mad mad;
101}; 105};
102 106
103static const dev_t base_dev = MKDEV(IB_UMAD_MAJOR, IB_UMAD_MINOR_BASE); 107static const dev_t base_dev = MKDEV(IB_UMAD_MAJOR, IB_UMAD_MINOR_BASE);
@@ -114,10 +118,10 @@ static int queue_packet(struct ib_umad_file *file,
114 int ret = 1; 118 int ret = 1;
115 119
116 down_read(&file->agent_mutex); 120 down_read(&file->agent_mutex);
117 for (packet->mad.id = 0; 121 for (packet->mad.hdr.id = 0;
118 packet->mad.id < IB_UMAD_MAX_AGENTS; 122 packet->mad.hdr.id < IB_UMAD_MAX_AGENTS;
119 packet->mad.id++) 123 packet->mad.hdr.id++)
120 if (agent == file->agent[packet->mad.id]) { 124 if (agent == file->agent[packet->mad.hdr.id]) {
121 spin_lock_irq(&file->recv_lock); 125 spin_lock_irq(&file->recv_lock);
122 list_add_tail(&packet->list, &file->recv_list); 126 list_add_tail(&packet->list, &file->recv_list);
123 spin_unlock_irq(&file->recv_lock); 127 spin_unlock_irq(&file->recv_lock);
@@ -135,22 +139,30 @@ static void send_handler(struct ib_mad_agent *agent,
135 struct ib_mad_send_wc *send_wc) 139 struct ib_mad_send_wc *send_wc)
136{ 140{
137 struct ib_umad_file *file = agent->context; 141 struct ib_umad_file *file = agent->context;
138 struct ib_umad_packet *packet = 142 struct ib_umad_packet *timeout, *packet =
139 (void *) (unsigned long) send_wc->wr_id; 143 (void *) (unsigned long) send_wc->wr_id;
140 144
141 dma_unmap_single(agent->device->dma_device, 145 ib_destroy_ah(packet->msg->send_wr.wr.ud.ah);
142 pci_unmap_addr(packet, mapping), 146 ib_free_send_mad(packet->msg);
143 sizeof packet->mad.data,
144 DMA_TO_DEVICE);
145 ib_destroy_ah(packet->ah);
146 147
147 if (send_wc->status == IB_WC_RESP_TIMEOUT_ERR) { 148 if (send_wc->status == IB_WC_RESP_TIMEOUT_ERR) {
148 packet->mad.status = ETIMEDOUT; 149 timeout = kmalloc(sizeof *timeout + sizeof (struct ib_mad_hdr),
150 GFP_KERNEL);
151 if (!timeout)
152 goto out;
149 153
150 if (!queue_packet(file, agent, packet)) 154 memset(timeout, 0, sizeof *timeout + sizeof (struct ib_mad_hdr));
151 return;
152 }
153 155
156 timeout->length = sizeof (struct ib_mad_hdr);
157 timeout->mad.hdr.id = packet->mad.hdr.id;
158 timeout->mad.hdr.status = ETIMEDOUT;
159 memcpy(timeout->mad.data, packet->mad.data,
160 sizeof (struct ib_mad_hdr));
161
162 if (!queue_packet(file, agent, timeout))
163 return;
164 }
165out:
154 kfree(packet); 166 kfree(packet);
155} 167}
156 168
@@ -159,30 +171,35 @@ static void recv_handler(struct ib_mad_agent *agent,
159{ 171{
160 struct ib_umad_file *file = agent->context; 172 struct ib_umad_file *file = agent->context;
161 struct ib_umad_packet *packet; 173 struct ib_umad_packet *packet;
174 int length;
162 175
163 if (mad_recv_wc->wc->status != IB_WC_SUCCESS) 176 if (mad_recv_wc->wc->status != IB_WC_SUCCESS)
164 goto out; 177 goto out;
165 178
166 packet = kmalloc(sizeof *packet, GFP_KERNEL); 179 length = mad_recv_wc->mad_len;
180 packet = kmalloc(sizeof *packet + length, GFP_KERNEL);
167 if (!packet) 181 if (!packet)
168 goto out; 182 goto out;
169 183
170 memset(packet, 0, sizeof *packet); 184 memset(packet, 0, sizeof *packet + length);
185 packet->length = length;
186
187 ib_coalesce_recv_mad(mad_recv_wc, packet->mad.data);
171 188
172 memcpy(packet->mad.data, mad_recv_wc->recv_buf.mad, sizeof packet->mad.data); 189 packet->mad.hdr.status = 0;
173 packet->mad.status = 0; 190 packet->mad.hdr.length = length + sizeof (struct ib_user_mad);
174 packet->mad.qpn = cpu_to_be32(mad_recv_wc->wc->src_qp); 191 packet->mad.hdr.qpn = cpu_to_be32(mad_recv_wc->wc->src_qp);
175 packet->mad.lid = cpu_to_be16(mad_recv_wc->wc->slid); 192 packet->mad.hdr.lid = cpu_to_be16(mad_recv_wc->wc->slid);
176 packet->mad.sl = mad_recv_wc->wc->sl; 193 packet->mad.hdr.sl = mad_recv_wc->wc->sl;
177 packet->mad.path_bits = mad_recv_wc->wc->dlid_path_bits; 194 packet->mad.hdr.path_bits = mad_recv_wc->wc->dlid_path_bits;
178 packet->mad.grh_present = !!(mad_recv_wc->wc->wc_flags & IB_WC_GRH); 195 packet->mad.hdr.grh_present = !!(mad_recv_wc->wc->wc_flags & IB_WC_GRH);
179 if (packet->mad.grh_present) { 196 if (packet->mad.hdr.grh_present) {
180 /* XXX parse GRH */ 197 /* XXX parse GRH */
181 packet->mad.gid_index = 0; 198 packet->mad.hdr.gid_index = 0;
182 packet->mad.hop_limit = 0; 199 packet->mad.hdr.hop_limit = 0;
183 packet->mad.traffic_class = 0; 200 packet->mad.hdr.traffic_class = 0;
184 memset(packet->mad.gid, 0, 16); 201 memset(packet->mad.hdr.gid, 0, 16);
185 packet->mad.flow_label = 0; 202 packet->mad.hdr.flow_label = 0;
186 } 203 }
187 204
188 if (queue_packet(file, agent, packet)) 205 if (queue_packet(file, agent, packet))
@@ -199,7 +216,7 @@ static ssize_t ib_umad_read(struct file *filp, char __user *buf,
199 struct ib_umad_packet *packet; 216 struct ib_umad_packet *packet;
200 ssize_t ret; 217 ssize_t ret;
201 218
202 if (count < sizeof (struct ib_user_mad)) 219 if (count < sizeof (struct ib_user_mad) + sizeof (struct ib_mad))
203 return -EINVAL; 220 return -EINVAL;
204 221
205 spin_lock_irq(&file->recv_lock); 222 spin_lock_irq(&file->recv_lock);
@@ -222,12 +239,25 @@ static ssize_t ib_umad_read(struct file *filp, char __user *buf,
222 239
223 spin_unlock_irq(&file->recv_lock); 240 spin_unlock_irq(&file->recv_lock);
224 241
225 if (copy_to_user(buf, &packet->mad, sizeof packet->mad)) 242 if (count < packet->length + sizeof (struct ib_user_mad)) {
243 /* Return length needed (and first RMPP segment) if too small */
244 if (copy_to_user(buf, &packet->mad,
245 sizeof (struct ib_user_mad) + sizeof (struct ib_mad)))
246 ret = -EFAULT;
247 else
248 ret = -ENOSPC;
249 } else if (copy_to_user(buf, &packet->mad,
250 packet->length + sizeof (struct ib_user_mad)))
226 ret = -EFAULT; 251 ret = -EFAULT;
227 else 252 else
228 ret = sizeof packet->mad; 253 ret = packet->length + sizeof (struct ib_user_mad);
229 254 if (ret < 0) {
230 kfree(packet); 255 /* Requeue packet */
256 spin_lock_irq(&file->recv_lock);
257 list_add(&packet->list, &file->recv_list);
258 spin_unlock_irq(&file->recv_lock);
259 } else
260 kfree(packet);
231 return ret; 261 return ret;
232} 262}
233 263
@@ -238,69 +268,57 @@ static ssize_t ib_umad_write(struct file *filp, const char __user *buf,
238 struct ib_umad_packet *packet; 268 struct ib_umad_packet *packet;
239 struct ib_mad_agent *agent; 269 struct ib_mad_agent *agent;
240 struct ib_ah_attr ah_attr; 270 struct ib_ah_attr ah_attr;
241 struct ib_sge gather_list; 271 struct ib_send_wr *bad_wr;
242 struct ib_send_wr *bad_wr, wr = { 272 struct ib_rmpp_mad *rmpp_mad;
243 .opcode = IB_WR_SEND,
244 .sg_list = &gather_list,
245 .num_sge = 1,
246 .send_flags = IB_SEND_SIGNALED,
247 };
248 u8 method; 273 u8 method;
249 u64 *tid; 274 u64 *tid;
250 int ret; 275 int ret, length, hdr_len, data_len, rmpp_hdr_size;
276 int rmpp_active = 0;
251 277
252 if (count < sizeof (struct ib_user_mad)) 278 if (count < sizeof (struct ib_user_mad))
253 return -EINVAL; 279 return -EINVAL;
254 280
255 packet = kmalloc(sizeof *packet, GFP_KERNEL); 281 length = count - sizeof (struct ib_user_mad);
282 packet = kmalloc(sizeof *packet + sizeof(struct ib_mad_hdr) +
283 sizeof(struct ib_rmpp_hdr), GFP_KERNEL);
256 if (!packet) 284 if (!packet)
257 return -ENOMEM; 285 return -ENOMEM;
258 286
259 if (copy_from_user(&packet->mad, buf, sizeof packet->mad)) { 287 if (copy_from_user(&packet->mad, buf,
260 kfree(packet); 288 sizeof (struct ib_user_mad) +
261 return -EFAULT; 289 sizeof(struct ib_mad_hdr) +
290 sizeof(struct ib_rmpp_hdr))) {
291 ret = -EFAULT;
292 goto err;
262 } 293 }
263 294
264 if (packet->mad.id < 0 || packet->mad.id >= IB_UMAD_MAX_AGENTS) { 295 if (packet->mad.hdr.id < 0 ||
296 packet->mad.hdr.id >= IB_UMAD_MAX_AGENTS) {
265 ret = -EINVAL; 297 ret = -EINVAL;
266 goto err; 298 goto err;
267 } 299 }
268 300
301 packet->length = length;
302
269 down_read(&file->agent_mutex); 303 down_read(&file->agent_mutex);
270 304
271 agent = file->agent[packet->mad.id]; 305 agent = file->agent[packet->mad.hdr.id];
272 if (!agent) { 306 if (!agent) {
273 ret = -EINVAL; 307 ret = -EINVAL;
274 goto err_up; 308 goto err_up;
275 } 309 }
276 310
277 /*
278 * If userspace is generating a request that will generate a
279 * response, we need to make sure the high-order part of the
280 * transaction ID matches the agent being used to send the
281 * MAD.
282 */
283 method = ((struct ib_mad_hdr *) packet->mad.data)->method;
284
285 if (!(method & IB_MGMT_METHOD_RESP) &&
286 method != IB_MGMT_METHOD_TRAP_REPRESS &&
287 method != IB_MGMT_METHOD_SEND) {
288 tid = &((struct ib_mad_hdr *) packet->mad.data)->tid;
289 *tid = cpu_to_be64(((u64) agent->hi_tid) << 32 |
290 (be64_to_cpup(tid) & 0xffffffff));
291 }
292
293 memset(&ah_attr, 0, sizeof ah_attr); 311 memset(&ah_attr, 0, sizeof ah_attr);
294 ah_attr.dlid = be16_to_cpu(packet->mad.lid); 312 ah_attr.dlid = be16_to_cpu(packet->mad.hdr.lid);
295 ah_attr.sl = packet->mad.sl; 313 ah_attr.sl = packet->mad.hdr.sl;
296 ah_attr.src_path_bits = packet->mad.path_bits; 314 ah_attr.src_path_bits = packet->mad.hdr.path_bits;
297 ah_attr.port_num = file->port->port_num; 315 ah_attr.port_num = file->port->port_num;
298 if (packet->mad.grh_present) { 316 if (packet->mad.hdr.grh_present) {
299 ah_attr.ah_flags = IB_AH_GRH; 317 ah_attr.ah_flags = IB_AH_GRH;
300 memcpy(ah_attr.grh.dgid.raw, packet->mad.gid, 16); 318 memcpy(ah_attr.grh.dgid.raw, packet->mad.hdr.gid, 16);
301 ah_attr.grh.flow_label = packet->mad.flow_label; 319 ah_attr.grh.flow_label = packet->mad.hdr.flow_label;
302 ah_attr.grh.hop_limit = packet->mad.hop_limit; 320 ah_attr.grh.hop_limit = packet->mad.hdr.hop_limit;
303 ah_attr.grh.traffic_class = packet->mad.traffic_class; 321 ah_attr.grh.traffic_class = packet->mad.hdr.traffic_class;
304 } 322 }
305 323
306 packet->ah = ib_create_ah(agent->qp->pd, &ah_attr); 324 packet->ah = ib_create_ah(agent->qp->pd, &ah_attr);
@@ -309,34 +327,104 @@ static ssize_t ib_umad_write(struct file *filp, const char __user *buf,
309 goto err_up; 327 goto err_up;
310 } 328 }
311 329
312 gather_list.addr = dma_map_single(agent->device->dma_device, 330 rmpp_mad = (struct ib_rmpp_mad *) packet->mad.data;
313 packet->mad.data, 331 if (ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) & IB_MGMT_RMPP_FLAG_ACTIVE) {
314 sizeof packet->mad.data, 332 /* RMPP active */
315 DMA_TO_DEVICE); 333 if (!agent->rmpp_version) {
316 gather_list.length = sizeof packet->mad.data; 334 ret = -EINVAL;
317 gather_list.lkey = file->mr[packet->mad.id]->lkey; 335 goto err_ah;
318 pci_unmap_addr_set(packet, mapping, gather_list.addr); 336 }
337 /* Validate that management class can support RMPP */
338 if (rmpp_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_ADM) {
339 hdr_len = offsetof(struct ib_sa_mad, data);
340 data_len = length;
341 } else if ((rmpp_mad->mad_hdr.mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
342 (rmpp_mad->mad_hdr.mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END)) {
343 hdr_len = offsetof(struct ib_vendor_mad, data);
344 data_len = length - hdr_len;
345 } else {
346 ret = -EINVAL;
347 goto err_ah;
348 }
349 rmpp_active = 1;
350 } else {
351 if (length > sizeof(struct ib_mad)) {
352 ret = -EINVAL;
353 goto err_ah;
354 }
355 hdr_len = offsetof(struct ib_mad, data);
356 data_len = length - hdr_len;
357 }
358
359 packet->msg = ib_create_send_mad(agent,
360 be32_to_cpu(packet->mad.hdr.qpn),
361 0, packet->ah, rmpp_active,
362 hdr_len, data_len,
363 GFP_KERNEL);
364 if (IS_ERR(packet->msg)) {
365 ret = PTR_ERR(packet->msg);
366 goto err_ah;
367 }
319 368
320 wr.wr.ud.mad_hdr = (struct ib_mad_hdr *) packet->mad.data; 369 packet->msg->send_wr.wr.ud.timeout_ms = packet->mad.hdr.timeout_ms;
321 wr.wr.ud.ah = packet->ah; 370 packet->msg->send_wr.wr.ud.retries = packet->mad.hdr.retries;
322 wr.wr.ud.remote_qpn = be32_to_cpu(packet->mad.qpn);
323 wr.wr.ud.remote_qkey = be32_to_cpu(packet->mad.qkey);
324 wr.wr.ud.timeout_ms = packet->mad.timeout_ms;
325 371
326 wr.wr_id = (unsigned long) packet; 372 /* Override send WR WRID initialized in ib_create_send_mad */
373 packet->msg->send_wr.wr_id = (unsigned long) packet;
327 374
328 ret = ib_post_send_mad(agent, &wr, &bad_wr); 375 if (!rmpp_active) {
329 if (ret) { 376 /* Copy message from user into send buffer */
330 dma_unmap_single(agent->device->dma_device, 377 if (copy_from_user(packet->msg->mad,
331 pci_unmap_addr(packet, mapping), 378 buf + sizeof(struct ib_user_mad), length)) {
332 sizeof packet->mad.data, 379 ret = -EFAULT;
333 DMA_TO_DEVICE); 380 goto err_msg;
334 goto err_up; 381 }
382 } else {
383 rmpp_hdr_size = sizeof(struct ib_mad_hdr) +
384 sizeof(struct ib_rmpp_hdr);
385
386 /* Only copy MAD headers (RMPP header in place) */
387 memcpy(packet->msg->mad, packet->mad.data,
388 sizeof(struct ib_mad_hdr));
389
390 /* Now, copy rest of message from user into send buffer */
391 if (copy_from_user(((struct ib_rmpp_mad *) packet->msg->mad)->data,
392 buf + sizeof (struct ib_user_mad) + rmpp_hdr_size,
393 length - rmpp_hdr_size)) {
394 ret = -EFAULT;
395 goto err_msg;
396 }
397 }
398
399 /*
400 * If userspace is generating a request that will generate a
401 * response, we need to make sure the high-order part of the
402 * transaction ID matches the agent being used to send the
403 * MAD.
404 */
405 method = packet->msg->mad->mad_hdr.method;
406
407 if (!(method & IB_MGMT_METHOD_RESP) &&
408 method != IB_MGMT_METHOD_TRAP_REPRESS &&
409 method != IB_MGMT_METHOD_SEND) {
410 tid = &packet->msg->mad->mad_hdr.tid;
411 *tid = cpu_to_be64(((u64) agent->hi_tid) << 32 |
412 (be64_to_cpup(tid) & 0xffffffff));
335 } 413 }
336 414
415 ret = ib_post_send_mad(agent, &packet->msg->send_wr, &bad_wr);
416 if (ret)
417 goto err_msg;
418
337 up_read(&file->agent_mutex); 419 up_read(&file->agent_mutex);
338 420
339 return sizeof packet->mad; 421 return sizeof (struct ib_user_mad_hdr) + packet->length;
422
423err_msg:
424 ib_free_send_mad(packet->msg);
425
426err_ah:
427 ib_destroy_ah(packet->ah);
340 428
341err_up: 429err_up:
342 up_read(&file->agent_mutex); 430 up_read(&file->agent_mutex);
@@ -399,7 +487,8 @@ found:
399 agent = ib_register_mad_agent(file->port->ib_dev, file->port->port_num, 487 agent = ib_register_mad_agent(file->port->ib_dev, file->port->port_num,
400 ureq.qpn ? IB_QPT_GSI : IB_QPT_SMI, 488 ureq.qpn ? IB_QPT_GSI : IB_QPT_SMI,
401 ureq.mgmt_class ? &req : NULL, 489 ureq.mgmt_class ? &req : NULL,
402 0, send_handler, recv_handler, file); 490 ureq.rmpp_version,
491 send_handler, recv_handler, file);
403 if (IS_ERR(agent)) { 492 if (IS_ERR(agent)) {
404 ret = PTR_ERR(agent); 493 ret = PTR_ERR(agent);
405 goto out; 494 goto out;
@@ -460,8 +549,8 @@ out:
460 return ret; 549 return ret;
461} 550}
462 551
463static long ib_umad_ioctl(struct file *filp, 552static long ib_umad_ioctl(struct file *filp, unsigned int cmd,
464 unsigned int cmd, unsigned long arg) 553 unsigned long arg)
465{ 554{
466 switch (cmd) { 555 switch (cmd) {
467 case IB_USER_MAD_REGISTER_AGENT: 556 case IB_USER_MAD_REGISTER_AGENT:
@@ -517,14 +606,14 @@ static int ib_umad_close(struct inode *inode, struct file *filp)
517} 606}
518 607
519static struct file_operations umad_fops = { 608static struct file_operations umad_fops = {
520 .owner = THIS_MODULE, 609 .owner = THIS_MODULE,
521 .read = ib_umad_read, 610 .read = ib_umad_read,
522 .write = ib_umad_write, 611 .write = ib_umad_write,
523 .poll = ib_umad_poll, 612 .poll = ib_umad_poll,
524 .unlocked_ioctl = ib_umad_ioctl, 613 .unlocked_ioctl = ib_umad_ioctl,
525 .compat_ioctl = ib_umad_ioctl, 614 .compat_ioctl = ib_umad_ioctl,
526 .open = ib_umad_open, 615 .open = ib_umad_open,
527 .release = ib_umad_close 616 .release = ib_umad_close
528}; 617};
529 618
530static int ib_umad_sm_open(struct inode *inode, struct file *filp) 619static int ib_umad_sm_open(struct inode *inode, struct file *filp)
diff --git a/drivers/infiniband/core/verbs.c b/drivers/infiniband/core/verbs.c
index 2516f9646515..506fdf1f2a26 100644
--- a/drivers/infiniband/core/verbs.c
+++ b/drivers/infiniband/core/verbs.c
@@ -41,6 +41,7 @@
41#include <linux/err.h> 41#include <linux/err.h>
42 42
43#include <ib_verbs.h> 43#include <ib_verbs.h>
44#include <ib_cache.h>
44 45
45/* Protection domains */ 46/* Protection domains */
46 47
@@ -88,6 +89,40 @@ struct ib_ah *ib_create_ah(struct ib_pd *pd, struct ib_ah_attr *ah_attr)
88} 89}
89EXPORT_SYMBOL(ib_create_ah); 90EXPORT_SYMBOL(ib_create_ah);
90 91
92struct ib_ah *ib_create_ah_from_wc(struct ib_pd *pd, struct ib_wc *wc,
93 struct ib_grh *grh, u8 port_num)
94{
95 struct ib_ah_attr ah_attr;
96 u32 flow_class;
97 u16 gid_index;
98 int ret;
99
100 memset(&ah_attr, 0, sizeof ah_attr);
101 ah_attr.dlid = wc->slid;
102 ah_attr.sl = wc->sl;
103 ah_attr.src_path_bits = wc->dlid_path_bits;
104 ah_attr.port_num = port_num;
105
106 if (wc->wc_flags & IB_WC_GRH) {
107 ah_attr.ah_flags = IB_AH_GRH;
108 ah_attr.grh.dgid = grh->dgid;
109
110 ret = ib_find_cached_gid(pd->device, &grh->sgid, &port_num,
111 &gid_index);
112 if (ret)
113 return ERR_PTR(ret);
114
115 ah_attr.grh.sgid_index = (u8) gid_index;
116 flow_class = be32_to_cpu(grh->version_tclass_flow);
117 ah_attr.grh.flow_label = flow_class & 0xFFFFF;
118 ah_attr.grh.traffic_class = (flow_class >> 20) & 0xFF;
119 ah_attr.grh.hop_limit = grh->hop_limit;
120 }
121
122 return ib_create_ah(pd, &ah_attr);
123}
124EXPORT_SYMBOL(ib_create_ah_from_wc);
125
91int ib_modify_ah(struct ib_ah *ah, struct ib_ah_attr *ah_attr) 126int ib_modify_ah(struct ib_ah *ah, struct ib_ah_attr *ah_attr)
92{ 127{
93 return ah->device->modify_ah ? 128 return ah->device->modify_ah ?