blob: 056389229ea7d03573e8dfa7b29520ed18a671fd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Hal Rosenstockde493d42007-04-02 11:24:07 -04002 * Copyright (c) 2004-2007 Voltaire, Inc. All rights reserved.
Hal Rosenstockfa619a72005-07-27 11:45:37 -07003 * Copyright (c) 2005 Intel Corporation. All rights reserved.
4 * Copyright (c) 2005 Mellanox Technologies Ltd. All rights reserved.
Hal Rosenstockb76aabc2009-09-07 08:28:48 -07005 * Copyright (c) 2009 HNR Consulting. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Jack Morgenstein9874e742006-06-17 20:37:34 -070038#include <rdma/ib_cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#include "mad_priv.h"
Hal Rosenstockfa619a72005-07-27 11:45:37 -070041#include "mad_rmpp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "smi.h"
43#include "agent.h"
44
45MODULE_LICENSE("Dual BSD/GPL");
46MODULE_DESCRIPTION("kernel IB MAD API");
47MODULE_AUTHOR("Hal Rosenstock");
48MODULE_AUTHOR("Sean Hefty");
49
Roland Dreier16933952010-05-23 21:39:31 -070050static int mad_sendq_size = IB_MAD_QP_SEND_SIZE;
51static int mad_recvq_size = IB_MAD_QP_RECV_SIZE;
Hal Rosenstockb76aabc2009-09-07 08:28:48 -070052
53module_param_named(send_queue_size, mad_sendq_size, int, 0444);
54MODULE_PARM_DESC(send_queue_size, "Size of send queue in number of work requests");
55module_param_named(recv_queue_size, mad_recvq_size, int, 0444);
56MODULE_PARM_DESC(recv_queue_size, "Size of receive queue in number of work requests");
57
Roland Dreiere54f8182006-11-29 15:33:07 -080058static struct kmem_cache *ib_mad_cache;
Hal Rosenstockfa619a72005-07-27 11:45:37 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static struct list_head ib_mad_port_list;
61static u32 ib_mad_client_id = 0;
62
63/* Port list lock */
Roland Dreier6276e082009-09-05 20:24:23 -070064static DEFINE_SPINLOCK(ib_mad_port_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/* Forward declarations */
67static int method_in_use(struct ib_mad_mgmt_method_table **method,
68 struct ib_mad_reg_req *mad_reg_req);
69static void remove_mad_reg_req(struct ib_mad_agent_private *priv);
70static struct ib_mad_agent_private *find_mad_agent(
71 struct ib_mad_port_private *port_priv,
Hal Rosenstock4a0754f2005-07-27 11:45:24 -070072 struct ib_mad *mad);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
74 struct ib_mad_private *mad);
75static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv);
David Howellsc4028952006-11-22 14:57:56 +000076static void timeout_sends(struct work_struct *work);
77static void local_completions(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
79 struct ib_mad_agent_private *agent_priv,
80 u8 mgmt_class);
81static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
82 struct ib_mad_agent_private *agent_priv);
83
84/*
85 * Returns a ib_mad_port_private structure or NULL for a device/port
86 * Assumes ib_mad_port_list_lock is being held
87 */
88static inline struct ib_mad_port_private *
89__ib_get_mad_port(struct ib_device *device, int port_num)
90{
91 struct ib_mad_port_private *entry;
92
93 list_for_each_entry(entry, &ib_mad_port_list, port_list) {
94 if (entry->device == device && entry->port_num == port_num)
95 return entry;
96 }
97 return NULL;
98}
99
100/*
101 * Wrapper function to return a ib_mad_port_private structure or NULL
102 * for a device/port
103 */
104static inline struct ib_mad_port_private *
105ib_get_mad_port(struct ib_device *device, int port_num)
106{
107 struct ib_mad_port_private *entry;
108 unsigned long flags;
109
110 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
111 entry = __ib_get_mad_port(device, port_num);
112 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
113
114 return entry;
115}
116
117static inline u8 convert_mgmt_class(u8 mgmt_class)
118{
119 /* Alias IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE to 0 */
120 return mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE ?
121 0 : mgmt_class;
122}
123
124static int get_spl_qp_index(enum ib_qp_type qp_type)
125{
126 switch (qp_type)
127 {
128 case IB_QPT_SMI:
129 return 0;
130 case IB_QPT_GSI:
131 return 1;
132 default:
133 return -1;
134 }
135}
136
137static int vendor_class_index(u8 mgmt_class)
138{
139 return mgmt_class - IB_MGMT_CLASS_VENDOR_RANGE2_START;
140}
141
142static int is_vendor_class(u8 mgmt_class)
143{
144 if ((mgmt_class < IB_MGMT_CLASS_VENDOR_RANGE2_START) ||
145 (mgmt_class > IB_MGMT_CLASS_VENDOR_RANGE2_END))
146 return 0;
147 return 1;
148}
149
150static int is_vendor_oui(char *oui)
151{
152 if (oui[0] || oui[1] || oui[2])
153 return 1;
154 return 0;
155}
156
157static int is_vendor_method_in_use(
158 struct ib_mad_mgmt_vendor_class *vendor_class,
159 struct ib_mad_reg_req *mad_reg_req)
160{
161 struct ib_mad_mgmt_method_table *method;
162 int i;
163
164 for (i = 0; i < MAX_MGMT_OUI; i++) {
165 if (!memcmp(vendor_class->oui[i], mad_reg_req->oui, 3)) {
166 method = vendor_class->method_table[i];
167 if (method) {
168 if (method_in_use(&method, mad_reg_req))
169 return 1;
170 else
171 break;
172 }
173 }
174 }
175 return 0;
176}
177
Sean Hefty2527e682006-07-20 11:25:50 +0300178int ib_response_mad(struct ib_mad *mad)
179{
180 return ((mad->mad_hdr.method & IB_MGMT_METHOD_RESP) ||
181 (mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS) ||
182 ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_BM) &&
183 (mad->mad_hdr.attr_mod & IB_BM_ATTR_MOD_RESP)));
184}
185EXPORT_SYMBOL(ib_response_mad);
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187/*
188 * ib_register_mad_agent - Register to send/receive MADs
189 */
190struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
191 u8 port_num,
192 enum ib_qp_type qp_type,
193 struct ib_mad_reg_req *mad_reg_req,
194 u8 rmpp_version,
195 ib_mad_send_handler send_handler,
196 ib_mad_recv_handler recv_handler,
197 void *context)
198{
199 struct ib_mad_port_private *port_priv;
200 struct ib_mad_agent *ret = ERR_PTR(-EINVAL);
201 struct ib_mad_agent_private *mad_agent_priv;
202 struct ib_mad_reg_req *reg_req = NULL;
203 struct ib_mad_mgmt_class_table *class;
204 struct ib_mad_mgmt_vendor_class_table *vendor;
205 struct ib_mad_mgmt_vendor_class *vendor_class;
206 struct ib_mad_mgmt_method_table *method;
207 int ret2, qpn;
208 unsigned long flags;
209 u8 mgmt_class, vclass;
210
211 /* Validate parameters */
212 qpn = get_spl_qp_index(qp_type);
213 if (qpn == -1)
214 goto error1;
215
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700216 if (rmpp_version && rmpp_version != IB_MGMT_RMPP_VERSION)
217 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 /* Validate MAD registration request if supplied */
220 if (mad_reg_req) {
221 if (mad_reg_req->mgmt_class_version >= MAX_MGMT_VERSION)
222 goto error1;
223 if (!recv_handler)
224 goto error1;
225 if (mad_reg_req->mgmt_class >= MAX_MGMT_CLASS) {
226 /*
227 * IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE is the only
228 * one in this range currently allowed
229 */
230 if (mad_reg_req->mgmt_class !=
231 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
232 goto error1;
233 } else if (mad_reg_req->mgmt_class == 0) {
234 /*
235 * Class 0 is reserved in IBA and is used for
236 * aliasing of IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
237 */
238 goto error1;
239 } else if (is_vendor_class(mad_reg_req->mgmt_class)) {
240 /*
241 * If class is in "new" vendor range,
242 * ensure supplied OUI is not zero
243 */
244 if (!is_vendor_oui(mad_reg_req->oui))
245 goto error1;
246 }
Hal Rosenstock618a3c02006-03-28 16:40:04 -0800247 /* Make sure class supplied is consistent with RMPP */
Hal Rosenstock64cb9c62006-04-12 21:29:10 -0400248 if (!ib_is_mad_class_rmpp(mad_reg_req->mgmt_class)) {
Hal Rosenstock618a3c02006-03-28 16:40:04 -0800249 if (rmpp_version)
250 goto error1;
251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 /* Make sure class supplied is consistent with QP type */
253 if (qp_type == IB_QPT_SMI) {
254 if ((mad_reg_req->mgmt_class !=
255 IB_MGMT_CLASS_SUBN_LID_ROUTED) &&
256 (mad_reg_req->mgmt_class !=
257 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE))
258 goto error1;
259 } else {
260 if ((mad_reg_req->mgmt_class ==
261 IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
262 (mad_reg_req->mgmt_class ==
263 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE))
264 goto error1;
265 }
266 } else {
267 /* No registration request supplied */
268 if (!send_handler)
269 goto error1;
270 }
271
272 /* Validate device and port */
273 port_priv = ib_get_mad_port(device, port_num);
274 if (!port_priv) {
275 ret = ERR_PTR(-ENODEV);
276 goto error1;
277 }
278
Ira Weinyc8367c42011-05-19 18:19:28 -0700279 /* Verify the QP requested is supported. For example, Ethernet devices
280 * will not have QP0 */
281 if (!port_priv->qp_info[qpn].qp) {
282 ret = ERR_PTR(-EPROTONOSUPPORT);
283 goto error1;
284 }
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 /* Allocate structures */
Roland Dreierde6eb662005-11-02 07:23:14 -0800287 mad_agent_priv = kzalloc(sizeof *mad_agent_priv, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (!mad_agent_priv) {
289 ret = ERR_PTR(-ENOMEM);
290 goto error1;
291 }
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700292
293 mad_agent_priv->agent.mr = ib_get_dma_mr(port_priv->qp_info[qpn].qp->pd,
294 IB_ACCESS_LOCAL_WRITE);
295 if (IS_ERR(mad_agent_priv->agent.mr)) {
296 ret = ERR_PTR(-ENOMEM);
297 goto error2;
298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 if (mad_reg_req) {
Julia Lawall9893e742010-05-15 23:22:38 +0200301 reg_req = kmemdup(mad_reg_req, sizeof *reg_req, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (!reg_req) {
303 ret = ERR_PTR(-ENOMEM);
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700304 goto error3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
307
308 /* Now, fill in the various structures */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 mad_agent_priv->qp_info = &port_priv->qp_info[qpn];
310 mad_agent_priv->reg_req = reg_req;
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700311 mad_agent_priv->agent.rmpp_version = rmpp_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 mad_agent_priv->agent.device = device;
313 mad_agent_priv->agent.recv_handler = recv_handler;
314 mad_agent_priv->agent.send_handler = send_handler;
315 mad_agent_priv->agent.context = context;
316 mad_agent_priv->agent.qp = port_priv->qp_info[qpn].qp;
317 mad_agent_priv->agent.port_num = port_num;
Ralph Campbelld9620a42009-02-27 14:44:32 -0800318 spin_lock_init(&mad_agent_priv->lock);
319 INIT_LIST_HEAD(&mad_agent_priv->send_list);
320 INIT_LIST_HEAD(&mad_agent_priv->wait_list);
321 INIT_LIST_HEAD(&mad_agent_priv->done_list);
322 INIT_LIST_HEAD(&mad_agent_priv->rmpp_list);
323 INIT_DELAYED_WORK(&mad_agent_priv->timed_work, timeout_sends);
324 INIT_LIST_HEAD(&mad_agent_priv->local_list);
325 INIT_WORK(&mad_agent_priv->local_work, local_completions);
326 atomic_set(&mad_agent_priv->refcount, 1);
327 init_completion(&mad_agent_priv->comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 spin_lock_irqsave(&port_priv->reg_lock, flags);
330 mad_agent_priv->agent.hi_tid = ++ib_mad_client_id;
331
332 /*
333 * Make sure MAD registration (if supplied)
334 * is non overlapping with any existing ones
335 */
336 if (mad_reg_req) {
337 mgmt_class = convert_mgmt_class(mad_reg_req->mgmt_class);
338 if (!is_vendor_class(mgmt_class)) {
339 class = port_priv->version[mad_reg_req->
340 mgmt_class_version].class;
341 if (class) {
342 method = class->method_table[mgmt_class];
343 if (method) {
344 if (method_in_use(&method,
345 mad_reg_req))
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700346 goto error4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348 }
349 ret2 = add_nonoui_reg_req(mad_reg_req, mad_agent_priv,
350 mgmt_class);
351 } else {
352 /* "New" vendor class range */
353 vendor = port_priv->version[mad_reg_req->
354 mgmt_class_version].vendor;
355 if (vendor) {
356 vclass = vendor_class_index(mgmt_class);
357 vendor_class = vendor->vendor_class[vclass];
358 if (vendor_class) {
359 if (is_vendor_method_in_use(
360 vendor_class,
361 mad_reg_req))
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700362 goto error4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
364 }
365 ret2 = add_oui_reg_req(mad_reg_req, mad_agent_priv);
366 }
367 if (ret2) {
368 ret = ERR_PTR(ret2);
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700369 goto error4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
371 }
372
373 /* Add mad agent into port's agent list */
374 list_add_tail(&mad_agent_priv->agent_list, &port_priv->agent_list);
375 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return &mad_agent_priv->agent;
378
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700379error4:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
381 kfree(reg_req);
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700382error3:
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700383 ib_dereg_mr(mad_agent_priv->agent.mr);
Adrian Bunk2012a112005-11-27 00:37:36 +0100384error2:
385 kfree(mad_agent_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386error1:
387 return ret;
388}
389EXPORT_SYMBOL(ib_register_mad_agent);
390
391static inline int is_snooping_sends(int mad_snoop_flags)
392{
393 return (mad_snoop_flags &
394 (/*IB_MAD_SNOOP_POSTED_SENDS |
395 IB_MAD_SNOOP_RMPP_SENDS |*/
396 IB_MAD_SNOOP_SEND_COMPLETIONS /*|
397 IB_MAD_SNOOP_RMPP_SEND_COMPLETIONS*/));
398}
399
400static inline int is_snooping_recvs(int mad_snoop_flags)
401{
402 return (mad_snoop_flags &
403 (IB_MAD_SNOOP_RECVS /*|
404 IB_MAD_SNOOP_RMPP_RECVS*/));
405}
406
407static int register_snoop_agent(struct ib_mad_qp_info *qp_info,
408 struct ib_mad_snoop_private *mad_snoop_priv)
409{
410 struct ib_mad_snoop_private **new_snoop_table;
411 unsigned long flags;
412 int i;
413
414 spin_lock_irqsave(&qp_info->snoop_lock, flags);
415 /* Check for empty slot in array. */
416 for (i = 0; i < qp_info->snoop_table_size; i++)
417 if (!qp_info->snoop_table[i])
418 break;
419
420 if (i == qp_info->snoop_table_size) {
421 /* Grow table. */
Roland Dreier528051742008-10-14 14:05:36 -0700422 new_snoop_table = krealloc(qp_info->snoop_table,
423 sizeof mad_snoop_priv *
424 (qp_info->snoop_table_size + 1),
425 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (!new_snoop_table) {
427 i = -ENOMEM;
428 goto out;
429 }
Roland Dreier528051742008-10-14 14:05:36 -0700430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 qp_info->snoop_table = new_snoop_table;
432 qp_info->snoop_table_size++;
433 }
434 qp_info->snoop_table[i] = mad_snoop_priv;
435 atomic_inc(&qp_info->snoop_count);
436out:
437 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
438 return i;
439}
440
441struct ib_mad_agent *ib_register_mad_snoop(struct ib_device *device,
442 u8 port_num,
443 enum ib_qp_type qp_type,
444 int mad_snoop_flags,
445 ib_mad_snoop_handler snoop_handler,
446 ib_mad_recv_handler recv_handler,
447 void *context)
448{
449 struct ib_mad_port_private *port_priv;
450 struct ib_mad_agent *ret;
451 struct ib_mad_snoop_private *mad_snoop_priv;
452 int qpn;
453
454 /* Validate parameters */
455 if ((is_snooping_sends(mad_snoop_flags) && !snoop_handler) ||
456 (is_snooping_recvs(mad_snoop_flags) && !recv_handler)) {
457 ret = ERR_PTR(-EINVAL);
458 goto error1;
459 }
460 qpn = get_spl_qp_index(qp_type);
461 if (qpn == -1) {
462 ret = ERR_PTR(-EINVAL);
463 goto error1;
464 }
465 port_priv = ib_get_mad_port(device, port_num);
466 if (!port_priv) {
467 ret = ERR_PTR(-ENODEV);
468 goto error1;
469 }
470 /* Allocate structures */
Roland Dreierde6eb662005-11-02 07:23:14 -0800471 mad_snoop_priv = kzalloc(sizeof *mad_snoop_priv, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (!mad_snoop_priv) {
473 ret = ERR_PTR(-ENOMEM);
474 goto error1;
475 }
476
477 /* Now, fill in the various structures */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 mad_snoop_priv->qp_info = &port_priv->qp_info[qpn];
479 mad_snoop_priv->agent.device = device;
480 mad_snoop_priv->agent.recv_handler = recv_handler;
481 mad_snoop_priv->agent.snoop_handler = snoop_handler;
482 mad_snoop_priv->agent.context = context;
483 mad_snoop_priv->agent.qp = port_priv->qp_info[qpn].qp;
484 mad_snoop_priv->agent.port_num = port_num;
485 mad_snoop_priv->mad_snoop_flags = mad_snoop_flags;
Sean Hefty1b52fa982006-05-12 14:57:52 -0700486 init_completion(&mad_snoop_priv->comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 mad_snoop_priv->snoop_index = register_snoop_agent(
488 &port_priv->qp_info[qpn],
489 mad_snoop_priv);
490 if (mad_snoop_priv->snoop_index < 0) {
491 ret = ERR_PTR(mad_snoop_priv->snoop_index);
492 goto error2;
493 }
494
495 atomic_set(&mad_snoop_priv->refcount, 1);
496 return &mad_snoop_priv->agent;
497
498error2:
499 kfree(mad_snoop_priv);
500error1:
501 return ret;
502}
503EXPORT_SYMBOL(ib_register_mad_snoop);
504
Sean Hefty1b52fa982006-05-12 14:57:52 -0700505static inline void deref_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
506{
507 if (atomic_dec_and_test(&mad_agent_priv->refcount))
508 complete(&mad_agent_priv->comp);
509}
510
511static inline void deref_snoop_agent(struct ib_mad_snoop_private *mad_snoop_priv)
512{
513 if (atomic_dec_and_test(&mad_snoop_priv->refcount))
514 complete(&mad_snoop_priv->comp);
515}
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517static void unregister_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
518{
519 struct ib_mad_port_private *port_priv;
520 unsigned long flags;
521
522 /* Note that we could still be handling received MADs */
523
524 /*
525 * Canceling all sends results in dropping received response
526 * MADs, preventing us from queuing additional work
527 */
528 cancel_mads(mad_agent_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 port_priv = mad_agent_priv->qp_info->port_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 cancel_delayed_work(&mad_agent_priv->timed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 spin_lock_irqsave(&port_priv->reg_lock, flags);
533 remove_mad_reg_req(mad_agent_priv);
534 list_del(&mad_agent_priv->agent_list);
535 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
536
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700537 flush_workqueue(port_priv->wq);
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700538 ib_cancel_rmpp_recvs(mad_agent_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Sean Hefty1b52fa982006-05-12 14:57:52 -0700540 deref_mad_agent(mad_agent_priv);
541 wait_for_completion(&mad_agent_priv->comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Jesper Juhl6044ec82005-11-07 01:01:32 -0800543 kfree(mad_agent_priv->reg_req);
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700544 ib_dereg_mr(mad_agent_priv->agent.mr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 kfree(mad_agent_priv);
546}
547
548static void unregister_mad_snoop(struct ib_mad_snoop_private *mad_snoop_priv)
549{
550 struct ib_mad_qp_info *qp_info;
551 unsigned long flags;
552
553 qp_info = mad_snoop_priv->qp_info;
554 spin_lock_irqsave(&qp_info->snoop_lock, flags);
555 qp_info->snoop_table[mad_snoop_priv->snoop_index] = NULL;
556 atomic_dec(&qp_info->snoop_count);
557 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
558
Sean Hefty1b52fa982006-05-12 14:57:52 -0700559 deref_snoop_agent(mad_snoop_priv);
560 wait_for_completion(&mad_snoop_priv->comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 kfree(mad_snoop_priv);
563}
564
565/*
566 * ib_unregister_mad_agent - Unregisters a client from using MAD services
567 */
568int ib_unregister_mad_agent(struct ib_mad_agent *mad_agent)
569{
570 struct ib_mad_agent_private *mad_agent_priv;
571 struct ib_mad_snoop_private *mad_snoop_priv;
572
573 /* If the TID is zero, the agent can only snoop. */
574 if (mad_agent->hi_tid) {
575 mad_agent_priv = container_of(mad_agent,
576 struct ib_mad_agent_private,
577 agent);
578 unregister_mad_agent(mad_agent_priv);
579 } else {
580 mad_snoop_priv = container_of(mad_agent,
581 struct ib_mad_snoop_private,
582 agent);
583 unregister_mad_snoop(mad_snoop_priv);
584 }
585 return 0;
586}
587EXPORT_SYMBOL(ib_unregister_mad_agent);
588
589static void dequeue_mad(struct ib_mad_list_head *mad_list)
590{
591 struct ib_mad_queue *mad_queue;
592 unsigned long flags;
593
594 BUG_ON(!mad_list->mad_queue);
595 mad_queue = mad_list->mad_queue;
596 spin_lock_irqsave(&mad_queue->lock, flags);
597 list_del(&mad_list->list);
598 mad_queue->count--;
599 spin_unlock_irqrestore(&mad_queue->lock, flags);
600}
601
602static void snoop_send(struct ib_mad_qp_info *qp_info,
Sean Hefty34816ad2005-10-25 10:51:39 -0700603 struct ib_mad_send_buf *send_buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 struct ib_mad_send_wc *mad_send_wc,
605 int mad_snoop_flags)
606{
607 struct ib_mad_snoop_private *mad_snoop_priv;
608 unsigned long flags;
609 int i;
610
611 spin_lock_irqsave(&qp_info->snoop_lock, flags);
612 for (i = 0; i < qp_info->snoop_table_size; i++) {
613 mad_snoop_priv = qp_info->snoop_table[i];
614 if (!mad_snoop_priv ||
615 !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
616 continue;
617
618 atomic_inc(&mad_snoop_priv->refcount);
619 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
620 mad_snoop_priv->agent.snoop_handler(&mad_snoop_priv->agent,
Sean Hefty34816ad2005-10-25 10:51:39 -0700621 send_buf, mad_send_wc);
Sean Hefty1b52fa982006-05-12 14:57:52 -0700622 deref_snoop_agent(mad_snoop_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 spin_lock_irqsave(&qp_info->snoop_lock, flags);
624 }
625 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
626}
627
628static void snoop_recv(struct ib_mad_qp_info *qp_info,
629 struct ib_mad_recv_wc *mad_recv_wc,
630 int mad_snoop_flags)
631{
632 struct ib_mad_snoop_private *mad_snoop_priv;
633 unsigned long flags;
634 int i;
635
636 spin_lock_irqsave(&qp_info->snoop_lock, flags);
637 for (i = 0; i < qp_info->snoop_table_size; i++) {
638 mad_snoop_priv = qp_info->snoop_table[i];
639 if (!mad_snoop_priv ||
640 !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
641 continue;
642
643 atomic_inc(&mad_snoop_priv->refcount);
644 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
645 mad_snoop_priv->agent.recv_handler(&mad_snoop_priv->agent,
646 mad_recv_wc);
Sean Hefty1b52fa982006-05-12 14:57:52 -0700647 deref_snoop_agent(mad_snoop_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 spin_lock_irqsave(&qp_info->snoop_lock, flags);
649 }
650 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
651}
652
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +0200653static void build_smp_wc(struct ib_qp *qp,
654 u64 wr_id, u16 slid, u16 pkey_index, u8 port_num,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 struct ib_wc *wc)
656{
657 memset(wc, 0, sizeof *wc);
658 wc->wr_id = wr_id;
659 wc->status = IB_WC_SUCCESS;
660 wc->opcode = IB_WC_RECV;
661 wc->pkey_index = pkey_index;
662 wc->byte_len = sizeof(struct ib_mad) + sizeof(struct ib_grh);
663 wc->src_qp = IB_QP0;
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +0200664 wc->qp = qp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 wc->slid = slid;
666 wc->sl = 0;
667 wc->dlid_path_bits = 0;
668 wc->port_num = port_num;
669}
670
671/*
672 * Return 0 if SMP is to be sent
673 * Return 1 if SMP was consumed locally (whether or not solicited)
674 * Return < 0 if error
675 */
676static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
Sean Hefty34816ad2005-10-25 10:51:39 -0700677 struct ib_mad_send_wr_private *mad_send_wr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Hal Rosenstockde493d42007-04-02 11:24:07 -0400679 int ret = 0;
Sean Hefty34816ad2005-10-25 10:51:39 -0700680 struct ib_smp *smp = mad_send_wr->send_buf.mad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 unsigned long flags;
682 struct ib_mad_local_private *local;
683 struct ib_mad_private *mad_priv;
684 struct ib_mad_port_private *port_priv;
685 struct ib_mad_agent_private *recv_mad_agent = NULL;
686 struct ib_device *device = mad_agent_priv->agent.device;
Hal Rosenstock1bae4db2007-05-14 17:21:52 -0400687 u8 port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 struct ib_wc mad_wc;
Sean Hefty34816ad2005-10-25 10:51:39 -0700689 struct ib_send_wr *send_wr = &mad_send_wr->send_wr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Hal Rosenstock1bae4db2007-05-14 17:21:52 -0400691 if (device->node_type == RDMA_NODE_IB_SWITCH &&
692 smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
693 port_num = send_wr->wr.ud.port_num;
694 else
695 port_num = mad_agent_priv->agent.port_num;
696
Ralph Campbell8cf3f042006-02-03 14:28:48 -0800697 /*
698 * Directed route handling starts if the initial LID routed part of
699 * a request or the ending LID routed part of a response is empty.
700 * If we are at the start of the LID routed part, don't update the
701 * hop_ptr or hop_cnt. See section 14.2.2, Vol 1 IB spec.
702 */
703 if ((ib_get_smp_direction(smp) ? smp->dr_dlid : smp->dr_slid) ==
704 IB_LID_PERMISSIVE &&
Hal Rosenstockde493d42007-04-02 11:24:07 -0400705 smi_handle_dr_smp_send(smp, device->node_type, port_num) ==
706 IB_SMI_DISCARD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 ret = -EINVAL;
708 printk(KERN_ERR PFX "Invalid directed route\n");
709 goto out;
710 }
Hal Rosenstockde493d42007-04-02 11:24:07 -0400711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /* Check to post send on QP or process locally */
Steve Welch727792d2007-10-23 15:06:10 -0700713 if (smi_check_local_smp(smp, device) == IB_SMI_DISCARD &&
714 smi_check_local_returning_smp(smp, device) == IB_SMI_DISCARD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 goto out;
716
717 local = kmalloc(sizeof *local, GFP_ATOMIC);
718 if (!local) {
719 ret = -ENOMEM;
720 printk(KERN_ERR PFX "No memory for ib_mad_local_private\n");
721 goto out;
722 }
723 local->mad_priv = NULL;
724 local->recv_mad_agent = NULL;
725 mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_ATOMIC);
726 if (!mad_priv) {
727 ret = -ENOMEM;
728 printk(KERN_ERR PFX "No memory for local response MAD\n");
729 kfree(local);
730 goto out;
731 }
732
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +0200733 build_smp_wc(mad_agent_priv->agent.qp,
734 send_wr->wr_id, be16_to_cpu(smp->dr_slid),
Sean Hefty97f52eb2005-08-13 21:05:57 -0700735 send_wr->wr.ud.pkey_index,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 send_wr->wr.ud.port_num, &mad_wc);
737
738 /* No GRH for DR SMP */
739 ret = device->process_mad(device, 0, port_num, &mad_wc, NULL,
740 (struct ib_mad *)smp,
741 (struct ib_mad *)&mad_priv->mad);
742 switch (ret)
743 {
744 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY:
Sean Hefty2527e682006-07-20 11:25:50 +0300745 if (ib_response_mad(&mad_priv->mad.mad) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 mad_agent_priv->agent.recv_handler) {
747 local->mad_priv = mad_priv;
748 local->recv_mad_agent = mad_agent_priv;
749 /*
750 * Reference MAD agent until receive
751 * side of local completion handled
752 */
753 atomic_inc(&mad_agent_priv->refcount);
754 } else
755 kmem_cache_free(ib_mad_cache, mad_priv);
756 break;
757 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED:
758 kmem_cache_free(ib_mad_cache, mad_priv);
Ralph Campbell4780c192009-03-03 14:22:17 -0800759 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 case IB_MAD_RESULT_SUCCESS:
761 /* Treat like an incoming receive MAD */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 port_priv = ib_get_mad_port(mad_agent_priv->agent.device,
763 mad_agent_priv->agent.port_num);
764 if (port_priv) {
Steve Welch727792d2007-10-23 15:06:10 -0700765 memcpy(&mad_priv->mad.mad, smp, sizeof(struct ib_mad));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 recv_mad_agent = find_mad_agent(port_priv,
Hal Rosenstock4a0754f2005-07-27 11:45:24 -0700767 &mad_priv->mad.mad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769 if (!port_priv || !recv_mad_agent) {
Ralph Campbell4780c192009-03-03 14:22:17 -0800770 /*
771 * No receiving agent so drop packet and
772 * generate send completion.
773 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 kmem_cache_free(ib_mad_cache, mad_priv);
Ralph Campbell4780c192009-03-03 14:22:17 -0800775 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777 local->mad_priv = mad_priv;
778 local->recv_mad_agent = recv_mad_agent;
779 break;
780 default:
781 kmem_cache_free(ib_mad_cache, mad_priv);
782 kfree(local);
783 ret = -EINVAL;
784 goto out;
785 }
786
Sean Hefty34816ad2005-10-25 10:51:39 -0700787 local->mad_send_wr = mad_send_wr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 /* Reference MAD agent until send side of local completion handled */
789 atomic_inc(&mad_agent_priv->refcount);
790 /* Queue local completion to local list */
791 spin_lock_irqsave(&mad_agent_priv->lock, flags);
792 list_add_tail(&local->completion_list, &mad_agent_priv->local_list);
793 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
794 queue_work(mad_agent_priv->qp_info->port_priv->wq,
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700795 &mad_agent_priv->local_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 ret = 1;
797out:
798 return ret;
799}
800
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800801static int get_pad_size(int hdr_len, int data_len)
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700802{
803 int seg_size, pad;
804
805 seg_size = sizeof(struct ib_mad) - hdr_len;
806 if (data_len && seg_size) {
807 pad = seg_size - data_len % seg_size;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800808 return pad == seg_size ? 0 : pad;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700809 } else
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800810 return seg_size;
811}
812
813static void free_send_rmpp_list(struct ib_mad_send_wr_private *mad_send_wr)
814{
815 struct ib_rmpp_segment *s, *t;
816
817 list_for_each_entry_safe(s, t, &mad_send_wr->rmpp_list, list) {
818 list_del(&s->list);
819 kfree(s);
820 }
821}
822
823static int alloc_send_rmpp_list(struct ib_mad_send_wr_private *send_wr,
824 gfp_t gfp_mask)
825{
826 struct ib_mad_send_buf *send_buf = &send_wr->send_buf;
827 struct ib_rmpp_mad *rmpp_mad = send_buf->mad;
828 struct ib_rmpp_segment *seg = NULL;
829 int left, seg_size, pad;
830
831 send_buf->seg_size = sizeof (struct ib_mad) - send_buf->hdr_len;
832 seg_size = send_buf->seg_size;
833 pad = send_wr->pad;
834
835 /* Allocate data segments. */
836 for (left = send_buf->data_len + pad; left > 0; left -= seg_size) {
837 seg = kmalloc(sizeof (*seg) + seg_size, gfp_mask);
838 if (!seg) {
839 printk(KERN_ERR "alloc_send_rmpp_segs: RMPP mem "
840 "alloc failed for len %zd, gfp %#x\n",
841 sizeof (*seg) + seg_size, gfp_mask);
842 free_send_rmpp_list(send_wr);
843 return -ENOMEM;
844 }
845 seg->num = ++send_buf->seg_count;
846 list_add_tail(&seg->list, &send_wr->rmpp_list);
847 }
848
849 /* Zero any padding */
850 if (pad)
851 memset(seg->data + seg_size - pad, 0, pad);
852
853 rmpp_mad->rmpp_hdr.rmpp_version = send_wr->mad_agent_priv->
854 agent.rmpp_version;
855 rmpp_mad->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_DATA;
856 ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
857
858 send_wr->cur_seg = container_of(send_wr->rmpp_list.next,
859 struct ib_rmpp_segment, list);
860 send_wr->last_ack_seg = send_wr->cur_seg;
861 return 0;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700862}
863
864struct ib_mad_send_buf * ib_create_send_mad(struct ib_mad_agent *mad_agent,
865 u32 remote_qpn, u16 pkey_index,
Sean Hefty34816ad2005-10-25 10:51:39 -0700866 int rmpp_active,
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700867 int hdr_len, int data_len,
Al Virodd0fc662005-10-07 07:46:04 +0100868 gfp_t gfp_mask)
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700869{
870 struct ib_mad_agent_private *mad_agent_priv;
Sean Hefty34816ad2005-10-25 10:51:39 -0700871 struct ib_mad_send_wr_private *mad_send_wr;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800872 int pad, message_size, ret, size;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700873 void *buf;
874
Sean Hefty34816ad2005-10-25 10:51:39 -0700875 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
876 agent);
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800877 pad = get_pad_size(hdr_len, data_len);
878 message_size = hdr_len + data_len + pad;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700879
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700880 if ((!mad_agent->rmpp_version &&
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800881 (rmpp_active || message_size > sizeof(struct ib_mad))) ||
882 (!rmpp_active && message_size > sizeof(struct ib_mad)))
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700883 return ERR_PTR(-EINVAL);
884
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800885 size = rmpp_active ? hdr_len : sizeof(struct ib_mad);
886 buf = kzalloc(sizeof *mad_send_wr + size, gfp_mask);
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700887 if (!buf)
888 return ERR_PTR(-ENOMEM);
889
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800890 mad_send_wr = buf + size;
891 INIT_LIST_HEAD(&mad_send_wr->rmpp_list);
Sean Hefty34816ad2005-10-25 10:51:39 -0700892 mad_send_wr->send_buf.mad = buf;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800893 mad_send_wr->send_buf.hdr_len = hdr_len;
894 mad_send_wr->send_buf.data_len = data_len;
895 mad_send_wr->pad = pad;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700896
Sean Hefty34816ad2005-10-25 10:51:39 -0700897 mad_send_wr->mad_agent_priv = mad_agent_priv;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800898 mad_send_wr->sg_list[0].length = hdr_len;
Sean Hefty34816ad2005-10-25 10:51:39 -0700899 mad_send_wr->sg_list[0].lkey = mad_agent->mr->lkey;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800900 mad_send_wr->sg_list[1].length = sizeof(struct ib_mad) - hdr_len;
901 mad_send_wr->sg_list[1].lkey = mad_agent->mr->lkey;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700902
Sean Hefty34816ad2005-10-25 10:51:39 -0700903 mad_send_wr->send_wr.wr_id = (unsigned long) mad_send_wr;
904 mad_send_wr->send_wr.sg_list = mad_send_wr->sg_list;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800905 mad_send_wr->send_wr.num_sge = 2;
Sean Hefty34816ad2005-10-25 10:51:39 -0700906 mad_send_wr->send_wr.opcode = IB_WR_SEND;
907 mad_send_wr->send_wr.send_flags = IB_SEND_SIGNALED;
908 mad_send_wr->send_wr.wr.ud.remote_qpn = remote_qpn;
909 mad_send_wr->send_wr.wr.ud.remote_qkey = IB_QP_SET_QKEY;
910 mad_send_wr->send_wr.wr.ud.pkey_index = pkey_index;
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700911
912 if (rmpp_active) {
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800913 ret = alloc_send_rmpp_list(mad_send_wr, gfp_mask);
914 if (ret) {
915 kfree(buf);
916 return ERR_PTR(ret);
917 }
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700918 }
919
Sean Hefty34816ad2005-10-25 10:51:39 -0700920 mad_send_wr->send_buf.mad_agent = mad_agent;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700921 atomic_inc(&mad_agent_priv->refcount);
Sean Hefty34816ad2005-10-25 10:51:39 -0700922 return &mad_send_wr->send_buf;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700923}
924EXPORT_SYMBOL(ib_create_send_mad);
925
Hal Rosenstock618a3c02006-03-28 16:40:04 -0800926int ib_get_mad_data_offset(u8 mgmt_class)
927{
928 if (mgmt_class == IB_MGMT_CLASS_SUBN_ADM)
929 return IB_MGMT_SA_HDR;
930 else if ((mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
931 (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
932 (mgmt_class == IB_MGMT_CLASS_BIS))
933 return IB_MGMT_DEVICE_HDR;
934 else if ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
935 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END))
936 return IB_MGMT_VENDOR_HDR;
937 else
938 return IB_MGMT_MAD_HDR;
939}
940EXPORT_SYMBOL(ib_get_mad_data_offset);
941
942int ib_is_mad_class_rmpp(u8 mgmt_class)
943{
944 if ((mgmt_class == IB_MGMT_CLASS_SUBN_ADM) ||
945 (mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
946 (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
947 (mgmt_class == IB_MGMT_CLASS_BIS) ||
948 ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
949 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END)))
950 return 1;
951 return 0;
952}
953EXPORT_SYMBOL(ib_is_mad_class_rmpp);
954
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800955void *ib_get_rmpp_segment(struct ib_mad_send_buf *send_buf, int seg_num)
956{
957 struct ib_mad_send_wr_private *mad_send_wr;
958 struct list_head *list;
959
960 mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
961 send_buf);
962 list = &mad_send_wr->cur_seg->list;
963
964 if (mad_send_wr->cur_seg->num < seg_num) {
965 list_for_each_entry(mad_send_wr->cur_seg, list, list)
966 if (mad_send_wr->cur_seg->num == seg_num)
967 break;
968 } else if (mad_send_wr->cur_seg->num > seg_num) {
969 list_for_each_entry_reverse(mad_send_wr->cur_seg, list, list)
970 if (mad_send_wr->cur_seg->num == seg_num)
971 break;
972 }
973 return mad_send_wr->cur_seg->data;
974}
975EXPORT_SYMBOL(ib_get_rmpp_segment);
976
977static inline void *ib_get_payload(struct ib_mad_send_wr_private *mad_send_wr)
978{
979 if (mad_send_wr->send_buf.seg_count)
980 return ib_get_rmpp_segment(&mad_send_wr->send_buf,
981 mad_send_wr->seg_num);
982 else
983 return mad_send_wr->send_buf.mad +
984 mad_send_wr->send_buf.hdr_len;
985}
986
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700987void ib_free_send_mad(struct ib_mad_send_buf *send_buf)
988{
989 struct ib_mad_agent_private *mad_agent_priv;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800990 struct ib_mad_send_wr_private *mad_send_wr;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700991
992 mad_agent_priv = container_of(send_buf->mad_agent,
993 struct ib_mad_agent_private, agent);
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800994 mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
995 send_buf);
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700996
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800997 free_send_rmpp_list(mad_send_wr);
998 kfree(send_buf->mad);
Sean Hefty1b52fa982006-05-12 14:57:52 -0700999 deref_mad_agent(mad_agent_priv);
Hal Rosenstock824c8ae2005-07-27 11:45:23 -07001000}
1001EXPORT_SYMBOL(ib_free_send_mad);
1002
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001003int ib_send_mad(struct ib_mad_send_wr_private *mad_send_wr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
1005 struct ib_mad_qp_info *qp_info;
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07001006 struct list_head *list;
Sean Hefty34816ad2005-10-25 10:51:39 -07001007 struct ib_send_wr *bad_send_wr;
1008 struct ib_mad_agent *mad_agent;
1009 struct ib_sge *sge;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 unsigned long flags;
1011 int ret;
1012
Hal Rosenstockf8197a42005-07-27 11:45:24 -07001013 /* Set WR ID to find mad_send_wr upon completion */
Hal Rosenstockd760ce82005-07-27 11:45:25 -07001014 qp_info = mad_send_wr->mad_agent_priv->qp_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 mad_send_wr->send_wr.wr_id = (unsigned long)&mad_send_wr->mad_list;
1016 mad_send_wr->mad_list.mad_queue = &qp_info->send_queue;
1017
Sean Hefty34816ad2005-10-25 10:51:39 -07001018 mad_agent = mad_send_wr->send_buf.mad_agent;
1019 sge = mad_send_wr->sg_list;
Ralph Campbell15271062006-12-12 14:28:30 -08001020 sge[0].addr = ib_dma_map_single(mad_agent->device,
1021 mad_send_wr->send_buf.mad,
1022 sge[0].length,
1023 DMA_TO_DEVICE);
1024 mad_send_wr->header_mapping = sge[0].addr;
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001025
Ralph Campbell15271062006-12-12 14:28:30 -08001026 sge[1].addr = ib_dma_map_single(mad_agent->device,
1027 ib_get_payload(mad_send_wr),
1028 sge[1].length,
1029 DMA_TO_DEVICE);
1030 mad_send_wr->payload_mapping = sge[1].addr;
Sean Hefty34816ad2005-10-25 10:51:39 -07001031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 spin_lock_irqsave(&qp_info->send_queue.lock, flags);
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07001033 if (qp_info->send_queue.count < qp_info->send_queue.max_active) {
Sean Hefty34816ad2005-10-25 10:51:39 -07001034 ret = ib_post_send(mad_agent->qp, &mad_send_wr->send_wr,
1035 &bad_send_wr);
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07001036 list = &qp_info->send_queue.list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 ret = 0;
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07001039 list = &qp_info->overflow_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07001041
1042 if (!ret) {
1043 qp_info->send_queue.count++;
1044 list_add_tail(&mad_send_wr->mad_list.list, list);
1045 }
1046 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001047 if (ret) {
Ralph Campbell15271062006-12-12 14:28:30 -08001048 ib_dma_unmap_single(mad_agent->device,
1049 mad_send_wr->header_mapping,
1050 sge[0].length, DMA_TO_DEVICE);
1051 ib_dma_unmap_single(mad_agent->device,
1052 mad_send_wr->payload_mapping,
1053 sge[1].length, DMA_TO_DEVICE);
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return ret;
1056}
1057
1058/*
1059 * ib_post_send_mad - Posts MAD(s) to the send queue of the QP associated
1060 * with the registered client
1061 */
Sean Hefty34816ad2005-10-25 10:51:39 -07001062int ib_post_send_mad(struct ib_mad_send_buf *send_buf,
1063 struct ib_mad_send_buf **bad_send_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 struct ib_mad_agent_private *mad_agent_priv;
Sean Hefty34816ad2005-10-25 10:51:39 -07001066 struct ib_mad_send_buf *next_send_buf;
1067 struct ib_mad_send_wr_private *mad_send_wr;
1068 unsigned long flags;
1069 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 /* Walk list of send WRs and post each on send list */
Sean Hefty34816ad2005-10-25 10:51:39 -07001072 for (; send_buf; send_buf = next_send_buf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Sean Hefty34816ad2005-10-25 10:51:39 -07001074 mad_send_wr = container_of(send_buf,
1075 struct ib_mad_send_wr_private,
1076 send_buf);
1077 mad_agent_priv = mad_send_wr->mad_agent_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Sean Hefty34816ad2005-10-25 10:51:39 -07001079 if (!send_buf->mad_agent->send_handler ||
1080 (send_buf->timeout_ms &&
1081 !send_buf->mad_agent->recv_handler)) {
1082 ret = -EINVAL;
1083 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
1085
Hal Rosenstock618a3c02006-03-28 16:40:04 -08001086 if (!ib_is_mad_class_rmpp(((struct ib_mad_hdr *) send_buf->mad)->mgmt_class)) {
1087 if (mad_agent_priv->agent.rmpp_version) {
1088 ret = -EINVAL;
1089 goto error;
1090 }
1091 }
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 /*
1094 * Save pointer to next work request to post in case the
1095 * current one completes, and the user modifies the work
1096 * request associated with the completion
1097 */
Sean Hefty34816ad2005-10-25 10:51:39 -07001098 next_send_buf = send_buf->next;
1099 mad_send_wr->send_wr.wr.ud.ah = send_buf->ah;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Sean Hefty34816ad2005-10-25 10:51:39 -07001101 if (((struct ib_mad_hdr *) send_buf->mad)->mgmt_class ==
1102 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
1103 ret = handle_outgoing_dr_smp(mad_agent_priv,
1104 mad_send_wr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 if (ret < 0) /* error */
Sean Hefty34816ad2005-10-25 10:51:39 -07001106 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 else if (ret == 1) /* locally consumed */
Sean Hefty34816ad2005-10-25 10:51:39 -07001108 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110
Sean Hefty34816ad2005-10-25 10:51:39 -07001111 mad_send_wr->tid = ((struct ib_mad_hdr *) send_buf->mad)->tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 /* Timeout will be updated after send completes */
Sean Hefty34816ad2005-10-25 10:51:39 -07001113 mad_send_wr->timeout = msecs_to_jiffies(send_buf->timeout_ms);
Sean Hefty4fc8cd42007-11-27 00:11:04 -08001114 mad_send_wr->max_retries = send_buf->retries;
1115 mad_send_wr->retries_left = send_buf->retries;
1116 send_buf->retries = 0;
Sean Hefty34816ad2005-10-25 10:51:39 -07001117 /* Reference for work request to QP + response */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 mad_send_wr->refcount = 1 + (mad_send_wr->timeout > 0);
1119 mad_send_wr->status = IB_WC_SUCCESS;
1120
1121 /* Reference MAD agent until send completes */
1122 atomic_inc(&mad_agent_priv->refcount);
1123 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1124 list_add_tail(&mad_send_wr->agent_list,
1125 &mad_agent_priv->send_list);
1126 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1127
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001128 if (mad_agent_priv->agent.rmpp_version) {
1129 ret = ib_send_rmpp_mad(mad_send_wr);
1130 if (ret >= 0 && ret != IB_RMPP_RESULT_CONSUMED)
1131 ret = ib_send_mad(mad_send_wr);
1132 } else
1133 ret = ib_send_mad(mad_send_wr);
1134 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 /* Fail send request */
1136 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1137 list_del(&mad_send_wr->agent_list);
1138 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1139 atomic_dec(&mad_agent_priv->refcount);
Sean Hefty34816ad2005-10-25 10:51:39 -07001140 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143 return 0;
Sean Hefty34816ad2005-10-25 10:51:39 -07001144error:
1145 if (bad_send_buf)
1146 *bad_send_buf = send_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return ret;
1148}
1149EXPORT_SYMBOL(ib_post_send_mad);
1150
1151/*
1152 * ib_free_recv_mad - Returns data buffers used to receive
1153 * a MAD to the access layer
1154 */
1155void ib_free_recv_mad(struct ib_mad_recv_wc *mad_recv_wc)
1156{
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001157 struct ib_mad_recv_buf *mad_recv_buf, *temp_recv_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 struct ib_mad_private_header *mad_priv_hdr;
1159 struct ib_mad_private *priv;
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001160 struct list_head free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001162 INIT_LIST_HEAD(&free_list);
1163 list_splice_init(&mad_recv_wc->rmpp_list, &free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001165 list_for_each_entry_safe(mad_recv_buf, temp_recv_buf,
1166 &free_list, list) {
1167 mad_recv_wc = container_of(mad_recv_buf, struct ib_mad_recv_wc,
1168 recv_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 mad_priv_hdr = container_of(mad_recv_wc,
1170 struct ib_mad_private_header,
1171 recv_wc);
1172 priv = container_of(mad_priv_hdr, struct ib_mad_private,
1173 header);
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001174 kmem_cache_free(ib_mad_cache, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
1177EXPORT_SYMBOL(ib_free_recv_mad);
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179struct ib_mad_agent *ib_redirect_mad_qp(struct ib_qp *qp,
1180 u8 rmpp_version,
1181 ib_mad_send_handler send_handler,
1182 ib_mad_recv_handler recv_handler,
1183 void *context)
1184{
1185 return ERR_PTR(-EINVAL); /* XXX: for now */
1186}
1187EXPORT_SYMBOL(ib_redirect_mad_qp);
1188
1189int ib_process_mad_wc(struct ib_mad_agent *mad_agent,
1190 struct ib_wc *wc)
1191{
1192 printk(KERN_ERR PFX "ib_process_mad_wc() not implemented yet\n");
1193 return 0;
1194}
1195EXPORT_SYMBOL(ib_process_mad_wc);
1196
1197static int method_in_use(struct ib_mad_mgmt_method_table **method,
1198 struct ib_mad_reg_req *mad_reg_req)
1199{
1200 int i;
1201
Akinobu Mita19b629f2010-03-05 13:41:38 -08001202 for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 if ((*method)->agent[i]) {
1204 printk(KERN_ERR PFX "Method %d already in use\n", i);
1205 return -EINVAL;
1206 }
1207 }
1208 return 0;
1209}
1210
1211static int allocate_method_table(struct ib_mad_mgmt_method_table **method)
1212{
1213 /* Allocate management method table */
Roland Dreierde6eb662005-11-02 07:23:14 -08001214 *method = kzalloc(sizeof **method, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (!*method) {
1216 printk(KERN_ERR PFX "No memory for "
1217 "ib_mad_mgmt_method_table\n");
1218 return -ENOMEM;
1219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 return 0;
1222}
1223
1224/*
1225 * Check to see if there are any methods still in use
1226 */
1227static int check_method_table(struct ib_mad_mgmt_method_table *method)
1228{
1229 int i;
1230
1231 for (i = 0; i < IB_MGMT_MAX_METHODS; i++)
1232 if (method->agent[i])
1233 return 1;
1234 return 0;
1235}
1236
1237/*
1238 * Check to see if there are any method tables for this class still in use
1239 */
1240static int check_class_table(struct ib_mad_mgmt_class_table *class)
1241{
1242 int i;
1243
1244 for (i = 0; i < MAX_MGMT_CLASS; i++)
1245 if (class->method_table[i])
1246 return 1;
1247 return 0;
1248}
1249
1250static int check_vendor_class(struct ib_mad_mgmt_vendor_class *vendor_class)
1251{
1252 int i;
1253
1254 for (i = 0; i < MAX_MGMT_OUI; i++)
1255 if (vendor_class->method_table[i])
1256 return 1;
1257 return 0;
1258}
1259
1260static int find_vendor_oui(struct ib_mad_mgmt_vendor_class *vendor_class,
1261 char *oui)
1262{
1263 int i;
1264
1265 for (i = 0; i < MAX_MGMT_OUI; i++)
Roland Dreier3cd96562006-09-22 15:22:46 -07001266 /* Is there matching OUI for this vendor class ? */
1267 if (!memcmp(vendor_class->oui[i], oui, 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 return i;
1269
1270 return -1;
1271}
1272
1273static int check_vendor_table(struct ib_mad_mgmt_vendor_class_table *vendor)
1274{
1275 int i;
1276
1277 for (i = 0; i < MAX_MGMT_VENDOR_RANGE2; i++)
1278 if (vendor->vendor_class[i])
1279 return 1;
1280
1281 return 0;
1282}
1283
1284static void remove_methods_mad_agent(struct ib_mad_mgmt_method_table *method,
1285 struct ib_mad_agent_private *agent)
1286{
1287 int i;
1288
1289 /* Remove any methods for this mad agent */
1290 for (i = 0; i < IB_MGMT_MAX_METHODS; i++) {
1291 if (method->agent[i] == agent) {
1292 method->agent[i] = NULL;
1293 }
1294 }
1295}
1296
1297static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1298 struct ib_mad_agent_private *agent_priv,
1299 u8 mgmt_class)
1300{
1301 struct ib_mad_port_private *port_priv;
1302 struct ib_mad_mgmt_class_table **class;
1303 struct ib_mad_mgmt_method_table **method;
1304 int i, ret;
1305
1306 port_priv = agent_priv->qp_info->port_priv;
1307 class = &port_priv->version[mad_reg_req->mgmt_class_version].class;
1308 if (!*class) {
1309 /* Allocate management class table for "new" class version */
Roland Dreierde6eb662005-11-02 07:23:14 -08001310 *class = kzalloc(sizeof **class, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (!*class) {
1312 printk(KERN_ERR PFX "No memory for "
1313 "ib_mad_mgmt_class_table\n");
1314 ret = -ENOMEM;
1315 goto error1;
1316 }
Roland Dreierde6eb662005-11-02 07:23:14 -08001317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 /* Allocate method table for this management class */
1319 method = &(*class)->method_table[mgmt_class];
1320 if ((ret = allocate_method_table(method)))
1321 goto error2;
1322 } else {
1323 method = &(*class)->method_table[mgmt_class];
1324 if (!*method) {
1325 /* Allocate method table for this management class */
1326 if ((ret = allocate_method_table(method)))
1327 goto error1;
1328 }
1329 }
1330
1331 /* Now, make sure methods are not already in use */
1332 if (method_in_use(method, mad_reg_req))
1333 goto error3;
1334
1335 /* Finally, add in methods being registered */
Akinobu Mita19b629f2010-03-05 13:41:38 -08001336 for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 (*method)->agent[i] = agent_priv;
Akinobu Mita19b629f2010-03-05 13:41:38 -08001338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 return 0;
1340
1341error3:
1342 /* Remove any methods for this mad agent */
1343 remove_methods_mad_agent(*method, agent_priv);
1344 /* Now, check to see if there are any methods in use */
1345 if (!check_method_table(*method)) {
1346 /* If not, release management method table */
1347 kfree(*method);
1348 *method = NULL;
1349 }
1350 ret = -EINVAL;
1351 goto error1;
1352error2:
1353 kfree(*class);
1354 *class = NULL;
1355error1:
1356 return ret;
1357}
1358
1359static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1360 struct ib_mad_agent_private *agent_priv)
1361{
1362 struct ib_mad_port_private *port_priv;
1363 struct ib_mad_mgmt_vendor_class_table **vendor_table;
1364 struct ib_mad_mgmt_vendor_class_table *vendor = NULL;
1365 struct ib_mad_mgmt_vendor_class *vendor_class = NULL;
1366 struct ib_mad_mgmt_method_table **method;
1367 int i, ret = -ENOMEM;
1368 u8 vclass;
1369
1370 /* "New" vendor (with OUI) class */
1371 vclass = vendor_class_index(mad_reg_req->mgmt_class);
1372 port_priv = agent_priv->qp_info->port_priv;
1373 vendor_table = &port_priv->version[
1374 mad_reg_req->mgmt_class_version].vendor;
1375 if (!*vendor_table) {
1376 /* Allocate mgmt vendor class table for "new" class version */
Roland Dreierde6eb662005-11-02 07:23:14 -08001377 vendor = kzalloc(sizeof *vendor, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if (!vendor) {
1379 printk(KERN_ERR PFX "No memory for "
1380 "ib_mad_mgmt_vendor_class_table\n");
1381 goto error1;
1382 }
Roland Dreierde6eb662005-11-02 07:23:14 -08001383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 *vendor_table = vendor;
1385 }
1386 if (!(*vendor_table)->vendor_class[vclass]) {
1387 /* Allocate table for this management vendor class */
Roland Dreierde6eb662005-11-02 07:23:14 -08001388 vendor_class = kzalloc(sizeof *vendor_class, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 if (!vendor_class) {
1390 printk(KERN_ERR PFX "No memory for "
1391 "ib_mad_mgmt_vendor_class\n");
1392 goto error2;
1393 }
Roland Dreierde6eb662005-11-02 07:23:14 -08001394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 (*vendor_table)->vendor_class[vclass] = vendor_class;
1396 }
1397 for (i = 0; i < MAX_MGMT_OUI; i++) {
1398 /* Is there matching OUI for this vendor class ? */
1399 if (!memcmp((*vendor_table)->vendor_class[vclass]->oui[i],
1400 mad_reg_req->oui, 3)) {
1401 method = &(*vendor_table)->vendor_class[
1402 vclass]->method_table[i];
1403 BUG_ON(!*method);
1404 goto check_in_use;
1405 }
1406 }
1407 for (i = 0; i < MAX_MGMT_OUI; i++) {
1408 /* OUI slot available ? */
1409 if (!is_vendor_oui((*vendor_table)->vendor_class[
1410 vclass]->oui[i])) {
1411 method = &(*vendor_table)->vendor_class[
1412 vclass]->method_table[i];
1413 BUG_ON(*method);
1414 /* Allocate method table for this OUI */
1415 if ((ret = allocate_method_table(method)))
1416 goto error3;
1417 memcpy((*vendor_table)->vendor_class[vclass]->oui[i],
1418 mad_reg_req->oui, 3);
1419 goto check_in_use;
1420 }
1421 }
1422 printk(KERN_ERR PFX "All OUI slots in use\n");
1423 goto error3;
1424
1425check_in_use:
1426 /* Now, make sure methods are not already in use */
1427 if (method_in_use(method, mad_reg_req))
1428 goto error4;
1429
1430 /* Finally, add in methods being registered */
Akinobu Mita19b629f2010-03-05 13:41:38 -08001431 for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 (*method)->agent[i] = agent_priv;
Akinobu Mita19b629f2010-03-05 13:41:38 -08001433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return 0;
1435
1436error4:
1437 /* Remove any methods for this mad agent */
1438 remove_methods_mad_agent(*method, agent_priv);
1439 /* Now, check to see if there are any methods in use */
1440 if (!check_method_table(*method)) {
1441 /* If not, release management method table */
1442 kfree(*method);
1443 *method = NULL;
1444 }
1445 ret = -EINVAL;
1446error3:
1447 if (vendor_class) {
1448 (*vendor_table)->vendor_class[vclass] = NULL;
1449 kfree(vendor_class);
1450 }
1451error2:
1452 if (vendor) {
1453 *vendor_table = NULL;
1454 kfree(vendor);
1455 }
1456error1:
1457 return ret;
1458}
1459
1460static void remove_mad_reg_req(struct ib_mad_agent_private *agent_priv)
1461{
1462 struct ib_mad_port_private *port_priv;
1463 struct ib_mad_mgmt_class_table *class;
1464 struct ib_mad_mgmt_method_table *method;
1465 struct ib_mad_mgmt_vendor_class_table *vendor;
1466 struct ib_mad_mgmt_vendor_class *vendor_class;
1467 int index;
1468 u8 mgmt_class;
1469
1470 /*
1471 * Was MAD registration request supplied
1472 * with original registration ?
1473 */
1474 if (!agent_priv->reg_req) {
1475 goto out;
1476 }
1477
1478 port_priv = agent_priv->qp_info->port_priv;
1479 mgmt_class = convert_mgmt_class(agent_priv->reg_req->mgmt_class);
1480 class = port_priv->version[
1481 agent_priv->reg_req->mgmt_class_version].class;
1482 if (!class)
1483 goto vendor_check;
1484
1485 method = class->method_table[mgmt_class];
1486 if (method) {
1487 /* Remove any methods for this mad agent */
1488 remove_methods_mad_agent(method, agent_priv);
1489 /* Now, check to see if there are any methods still in use */
1490 if (!check_method_table(method)) {
1491 /* If not, release management method table */
1492 kfree(method);
1493 class->method_table[mgmt_class] = NULL;
1494 /* Any management classes left ? */
1495 if (!check_class_table(class)) {
1496 /* If not, release management class table */
1497 kfree(class);
1498 port_priv->version[
1499 agent_priv->reg_req->
1500 mgmt_class_version].class = NULL;
1501 }
1502 }
1503 }
1504
1505vendor_check:
1506 if (!is_vendor_class(mgmt_class))
1507 goto out;
1508
1509 /* normalize mgmt_class to vendor range 2 */
1510 mgmt_class = vendor_class_index(agent_priv->reg_req->mgmt_class);
1511 vendor = port_priv->version[
1512 agent_priv->reg_req->mgmt_class_version].vendor;
1513
1514 if (!vendor)
1515 goto out;
1516
1517 vendor_class = vendor->vendor_class[mgmt_class];
1518 if (vendor_class) {
1519 index = find_vendor_oui(vendor_class, agent_priv->reg_req->oui);
1520 if (index < 0)
1521 goto out;
1522 method = vendor_class->method_table[index];
1523 if (method) {
1524 /* Remove any methods for this mad agent */
1525 remove_methods_mad_agent(method, agent_priv);
1526 /*
1527 * Now, check to see if there are
1528 * any methods still in use
1529 */
1530 if (!check_method_table(method)) {
1531 /* If not, release management method table */
1532 kfree(method);
1533 vendor_class->method_table[index] = NULL;
1534 memset(vendor_class->oui[index], 0, 3);
1535 /* Any OUIs left ? */
1536 if (!check_vendor_class(vendor_class)) {
1537 /* If not, release vendor class table */
1538 kfree(vendor_class);
1539 vendor->vendor_class[mgmt_class] = NULL;
1540 /* Any other vendor classes left ? */
1541 if (!check_vendor_table(vendor)) {
1542 kfree(vendor);
1543 port_priv->version[
1544 agent_priv->reg_req->
1545 mgmt_class_version].
1546 vendor = NULL;
1547 }
1548 }
1549 }
1550 }
1551 }
1552
1553out:
1554 return;
1555}
1556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557static struct ib_mad_agent_private *
1558find_mad_agent(struct ib_mad_port_private *port_priv,
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07001559 struct ib_mad *mad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
1561 struct ib_mad_agent_private *mad_agent = NULL;
1562 unsigned long flags;
1563
1564 spin_lock_irqsave(&port_priv->reg_lock, flags);
Sean Hefty2527e682006-07-20 11:25:50 +03001565 if (ib_response_mad(mad)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 u32 hi_tid;
1567 struct ib_mad_agent_private *entry;
1568
1569 /*
1570 * Routing is based on high 32 bits of transaction ID
1571 * of MAD.
1572 */
1573 hi_tid = be64_to_cpu(mad->mad_hdr.tid) >> 32;
Sean Hefty34816ad2005-10-25 10:51:39 -07001574 list_for_each_entry(entry, &port_priv->agent_list, agent_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 if (entry->agent.hi_tid == hi_tid) {
1576 mad_agent = entry;
1577 break;
1578 }
1579 }
1580 } else {
1581 struct ib_mad_mgmt_class_table *class;
1582 struct ib_mad_mgmt_method_table *method;
1583 struct ib_mad_mgmt_vendor_class_table *vendor;
1584 struct ib_mad_mgmt_vendor_class *vendor_class;
1585 struct ib_vendor_mad *vendor_mad;
1586 int index;
1587
1588 /*
1589 * Routing is based on version, class, and method
1590 * For "newer" vendor MADs, also based on OUI
1591 */
1592 if (mad->mad_hdr.class_version >= MAX_MGMT_VERSION)
1593 goto out;
1594 if (!is_vendor_class(mad->mad_hdr.mgmt_class)) {
1595 class = port_priv->version[
1596 mad->mad_hdr.class_version].class;
1597 if (!class)
1598 goto out;
Hefty, Seanb7ab0b12011-10-06 09:33:05 -07001599 if (convert_mgmt_class(mad->mad_hdr.mgmt_class) >=
1600 IB_MGMT_MAX_METHODS)
1601 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 method = class->method_table[convert_mgmt_class(
1603 mad->mad_hdr.mgmt_class)];
1604 if (method)
1605 mad_agent = method->agent[mad->mad_hdr.method &
1606 ~IB_MGMT_METHOD_RESP];
1607 } else {
1608 vendor = port_priv->version[
1609 mad->mad_hdr.class_version].vendor;
1610 if (!vendor)
1611 goto out;
1612 vendor_class = vendor->vendor_class[vendor_class_index(
1613 mad->mad_hdr.mgmt_class)];
1614 if (!vendor_class)
1615 goto out;
1616 /* Find matching OUI */
1617 vendor_mad = (struct ib_vendor_mad *)mad;
1618 index = find_vendor_oui(vendor_class, vendor_mad->oui);
1619 if (index == -1)
1620 goto out;
1621 method = vendor_class->method_table[index];
1622 if (method) {
1623 mad_agent = method->agent[mad->mad_hdr.method &
1624 ~IB_MGMT_METHOD_RESP];
1625 }
1626 }
1627 }
1628
1629 if (mad_agent) {
1630 if (mad_agent->agent.recv_handler)
1631 atomic_inc(&mad_agent->refcount);
1632 else {
1633 printk(KERN_NOTICE PFX "No receive handler for client "
1634 "%p on port %d\n",
1635 &mad_agent->agent, port_priv->port_num);
1636 mad_agent = NULL;
1637 }
1638 }
1639out:
1640 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
1641
1642 return mad_agent;
1643}
1644
1645static int validate_mad(struct ib_mad *mad, u32 qp_num)
1646{
1647 int valid = 0;
1648
1649 /* Make sure MAD base version is understood */
1650 if (mad->mad_hdr.base_version != IB_MGMT_BASE_VERSION) {
1651 printk(KERN_ERR PFX "MAD received with unsupported base "
1652 "version %d\n", mad->mad_hdr.base_version);
1653 goto out;
1654 }
1655
1656 /* Filter SMI packets sent to other than QP0 */
1657 if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
1658 (mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) {
1659 if (qp_num == 0)
1660 valid = 1;
1661 } else {
1662 /* Filter GSI packets sent to QP0 */
1663 if (qp_num != 0)
1664 valid = 1;
1665 }
1666
1667out:
1668 return valid;
1669}
1670
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001671static int is_data_mad(struct ib_mad_agent_private *mad_agent_priv,
1672 struct ib_mad_hdr *mad_hdr)
1673{
1674 struct ib_rmpp_mad *rmpp_mad;
1675
1676 rmpp_mad = (struct ib_rmpp_mad *)mad_hdr;
1677 return !mad_agent_priv->agent.rmpp_version ||
1678 !(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
1679 IB_MGMT_RMPP_FLAG_ACTIVE) ||
1680 (rmpp_mad->rmpp_hdr.rmpp_type == IB_MGMT_RMPP_TYPE_DATA);
1681}
1682
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001683static inline int rcv_has_same_class(struct ib_mad_send_wr_private *wr,
1684 struct ib_mad_recv_wc *rwc)
1685{
1686 return ((struct ib_mad *)(wr->send_buf.mad))->mad_hdr.mgmt_class ==
1687 rwc->recv_buf.mad->mad_hdr.mgmt_class;
1688}
1689
Jack Morgenstein9874e742006-06-17 20:37:34 -07001690static inline int rcv_has_same_gid(struct ib_mad_agent_private *mad_agent_priv,
1691 struct ib_mad_send_wr_private *wr,
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001692 struct ib_mad_recv_wc *rwc )
1693{
1694 struct ib_ah_attr attr;
1695 u8 send_resp, rcv_resp;
Jack Morgenstein9874e742006-06-17 20:37:34 -07001696 union ib_gid sgid;
1697 struct ib_device *device = mad_agent_priv->agent.device;
1698 u8 port_num = mad_agent_priv->agent.port_num;
1699 u8 lmc;
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001700
Michael Brooks70972282008-09-20 20:06:16 -07001701 send_resp = ib_response_mad((struct ib_mad *)wr->send_buf.mad);
1702 rcv_resp = ib_response_mad(rwc->recv_buf.mad);
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001703
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001704 if (send_resp == rcv_resp)
1705 /* both requests, or both responses. GIDs different */
1706 return 0;
1707
1708 if (ib_query_ah(wr->send_buf.ah, &attr))
1709 /* Assume not equal, to avoid false positives. */
1710 return 0;
1711
Jack Morgenstein9874e742006-06-17 20:37:34 -07001712 if (!!(attr.ah_flags & IB_AH_GRH) !=
1713 !!(rwc->wc->wc_flags & IB_WC_GRH))
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001714 /* one has GID, other does not. Assume different */
1715 return 0;
Jack Morgenstein9874e742006-06-17 20:37:34 -07001716
1717 if (!send_resp && rcv_resp) {
1718 /* is request/response. */
1719 if (!(attr.ah_flags & IB_AH_GRH)) {
1720 if (ib_get_cached_lmc(device, port_num, &lmc))
1721 return 0;
1722 return (!lmc || !((attr.src_path_bits ^
1723 rwc->wc->dlid_path_bits) &
1724 ((1 << lmc) - 1)));
1725 } else {
1726 if (ib_get_cached_gid(device, port_num,
1727 attr.grh.sgid_index, &sgid))
1728 return 0;
1729 return !memcmp(sgid.raw, rwc->recv_buf.grh->dgid.raw,
1730 16);
1731 }
1732 }
1733
1734 if (!(attr.ah_flags & IB_AH_GRH))
1735 return attr.dlid == rwc->wc->slid;
1736 else
1737 return !memcmp(attr.grh.dgid.raw, rwc->recv_buf.grh->sgid.raw,
1738 16);
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001739}
Jack Morgenstein9874e742006-06-17 20:37:34 -07001740
1741static inline int is_direct(u8 class)
1742{
1743 return (class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE);
1744}
1745
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001746struct ib_mad_send_wr_private*
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001747ib_find_send_mad(struct ib_mad_agent_private *mad_agent_priv,
Jack Morgenstein9874e742006-06-17 20:37:34 -07001748 struct ib_mad_recv_wc *wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
Jack Morgenstein9874e742006-06-17 20:37:34 -07001750 struct ib_mad_send_wr_private *wr;
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001751 struct ib_mad *mad;
1752
Jack Morgenstein9874e742006-06-17 20:37:34 -07001753 mad = (struct ib_mad *)wc->recv_buf.mad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Jack Morgenstein9874e742006-06-17 20:37:34 -07001755 list_for_each_entry(wr, &mad_agent_priv->wait_list, agent_list) {
1756 if ((wr->tid == mad->mad_hdr.tid) &&
1757 rcv_has_same_class(wr, wc) &&
1758 /*
1759 * Don't check GID for direct routed MADs.
1760 * These might have permissive LIDs.
1761 */
1762 (is_direct(wc->recv_buf.mad->mad_hdr.mgmt_class) ||
1763 rcv_has_same_gid(mad_agent_priv, wr, wc)))
Roland Dreier39798692006-11-13 09:38:07 -08001764 return (wr->status == IB_WC_SUCCESS) ? wr : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 }
1766
1767 /*
1768 * It's possible to receive the response before we've
1769 * been notified that the send has completed
1770 */
Jack Morgenstein9874e742006-06-17 20:37:34 -07001771 list_for_each_entry(wr, &mad_agent_priv->send_list, agent_list) {
1772 if (is_data_mad(mad_agent_priv, wr->send_buf.mad) &&
1773 wr->tid == mad->mad_hdr.tid &&
1774 wr->timeout &&
1775 rcv_has_same_class(wr, wc) &&
1776 /*
1777 * Don't check GID for direct routed MADs.
1778 * These might have permissive LIDs.
1779 */
1780 (is_direct(wc->recv_buf.mad->mad_hdr.mgmt_class) ||
1781 rcv_has_same_gid(mad_agent_priv, wr, wc)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 /* Verify request has not been canceled */
Jack Morgenstein9874e742006-06-17 20:37:34 -07001783 return (wr->status == IB_WC_SUCCESS) ? wr : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 }
1785 return NULL;
1786}
1787
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001788void ib_mark_mad_done(struct ib_mad_send_wr_private *mad_send_wr)
Hal Rosenstock6a0c4352005-07-27 11:45:26 -07001789{
1790 mad_send_wr->timeout = 0;
Akinobu Mita179e0912006-06-26 00:24:41 -07001791 if (mad_send_wr->refcount == 1)
1792 list_move_tail(&mad_send_wr->agent_list,
Hal Rosenstock6a0c4352005-07-27 11:45:26 -07001793 &mad_send_wr->mad_agent_priv->done_list);
Hal Rosenstock6a0c4352005-07-27 11:45:26 -07001794}
1795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796static void ib_mad_complete_recv(struct ib_mad_agent_private *mad_agent_priv,
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07001797 struct ib_mad_recv_wc *mad_recv_wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798{
1799 struct ib_mad_send_wr_private *mad_send_wr;
1800 struct ib_mad_send_wc mad_send_wc;
1801 unsigned long flags;
1802
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001803 INIT_LIST_HEAD(&mad_recv_wc->rmpp_list);
1804 list_add(&mad_recv_wc->recv_buf.list, &mad_recv_wc->rmpp_list);
1805 if (mad_agent_priv->agent.rmpp_version) {
1806 mad_recv_wc = ib_process_rmpp_recv_wc(mad_agent_priv,
1807 mad_recv_wc);
1808 if (!mad_recv_wc) {
Sean Hefty1b52fa982006-05-12 14:57:52 -07001809 deref_mad_agent(mad_agent_priv);
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001810 return;
1811 }
1812 }
1813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 /* Complete corresponding request */
Sean Hefty2527e682006-07-20 11:25:50 +03001815 if (ib_response_mad(mad_recv_wc->recv_buf.mad)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 spin_lock_irqsave(&mad_agent_priv->lock, flags);
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001817 mad_send_wr = ib_find_send_mad(mad_agent_priv, mad_recv_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 if (!mad_send_wr) {
1819 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07001820 ib_free_recv_mad(mad_recv_wc);
Sean Hefty1b52fa982006-05-12 14:57:52 -07001821 deref_mad_agent(mad_agent_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 return;
1823 }
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001824 ib_mark_mad_done(mad_send_wr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1826
1827 /* Defined behavior is to complete response before request */
Sean Hefty34816ad2005-10-25 10:51:39 -07001828 mad_recv_wc->wc->wr_id = (unsigned long) &mad_send_wr->send_buf;
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07001829 mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent,
1830 mad_recv_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 atomic_dec(&mad_agent_priv->refcount);
1832
1833 mad_send_wc.status = IB_WC_SUCCESS;
1834 mad_send_wc.vendor_err = 0;
Sean Hefty34816ad2005-10-25 10:51:39 -07001835 mad_send_wc.send_buf = &mad_send_wr->send_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
1837 } else {
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07001838 mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent,
1839 mad_recv_wc);
Sean Hefty1b52fa982006-05-12 14:57:52 -07001840 deref_mad_agent(mad_agent_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 }
1842}
1843
1844static void ib_mad_recv_done_handler(struct ib_mad_port_private *port_priv,
1845 struct ib_wc *wc)
1846{
1847 struct ib_mad_qp_info *qp_info;
1848 struct ib_mad_private_header *mad_priv_hdr;
Hal Rosenstock445d6802007-08-03 10:45:17 -07001849 struct ib_mad_private *recv, *response = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 struct ib_mad_list_head *mad_list;
1851 struct ib_mad_agent_private *mad_agent;
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04001852 int port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
1855 qp_info = mad_list->mad_queue->qp_info;
1856 dequeue_mad(mad_list);
1857
1858 mad_priv_hdr = container_of(mad_list, struct ib_mad_private_header,
1859 mad_list);
1860 recv = container_of(mad_priv_hdr, struct ib_mad_private, header);
Ralph Campbell15271062006-12-12 14:28:30 -08001861 ib_dma_unmap_single(port_priv->device,
1862 recv->header.mapping,
1863 sizeof(struct ib_mad_private) -
1864 sizeof(struct ib_mad_private_header),
1865 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867 /* Setup MAD receive work completion from "normal" work completion */
Sean Hefty24239af2005-04-16 15:26:08 -07001868 recv->header.wc = *wc;
1869 recv->header.recv_wc.wc = &recv->header.wc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 recv->header.recv_wc.mad_len = sizeof(struct ib_mad);
1871 recv->header.recv_wc.recv_buf.mad = &recv->mad.mad;
1872 recv->header.recv_wc.recv_buf.grh = &recv->grh;
1873
1874 if (atomic_read(&qp_info->snoop_count))
1875 snoop_recv(qp_info, &recv->header.recv_wc, IB_MAD_SNOOP_RECVS);
1876
1877 /* Validate MAD */
1878 if (!validate_mad(&recv->mad.mad, qp_info->qp->qp_num))
1879 goto out;
1880
Hal Rosenstock445d6802007-08-03 10:45:17 -07001881 response = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL);
1882 if (!response) {
1883 printk(KERN_ERR PFX "ib_mad_recv_done_handler no memory "
1884 "for response buffer\n");
1885 goto out;
1886 }
1887
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04001888 if (port_priv->device->node_type == RDMA_NODE_IB_SWITCH)
1889 port_num = wc->port_num;
1890 else
1891 port_num = port_priv->port_num;
1892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 if (recv->mad.mad.mad_hdr.mgmt_class ==
1894 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04001895 enum smi_forward_action retsmi;
1896
Hal Rosenstockde493d42007-04-02 11:24:07 -04001897 if (smi_handle_dr_smp_recv(&recv->mad.smp,
1898 port_priv->device->node_type,
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04001899 port_num,
Hal Rosenstockde493d42007-04-02 11:24:07 -04001900 port_priv->device->phys_port_cnt) ==
1901 IB_SMI_DISCARD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 goto out;
Hal Rosenstockde493d42007-04-02 11:24:07 -04001903
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04001904 retsmi = smi_check_forward_dr_smp(&recv->mad.smp);
1905 if (retsmi == IB_SMI_LOCAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 goto local;
Hal Rosenstockde493d42007-04-02 11:24:07 -04001907
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04001908 if (retsmi == IB_SMI_SEND) { /* don't forward */
1909 if (smi_handle_dr_smp_send(&recv->mad.smp,
1910 port_priv->device->node_type,
1911 port_num) == IB_SMI_DISCARD)
1912 goto out;
Hal Rosenstockde493d42007-04-02 11:24:07 -04001913
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04001914 if (smi_check_local_smp(&recv->mad.smp, port_priv->device) == IB_SMI_DISCARD)
1915 goto out;
1916 } else if (port_priv->device->node_type == RDMA_NODE_IB_SWITCH) {
1917 /* forward case for switches */
1918 memcpy(response, recv, sizeof(*response));
1919 response->header.recv_wc.wc = &response->header.wc;
1920 response->header.recv_wc.recv_buf.mad = &response->mad.mad;
1921 response->header.recv_wc.recv_buf.grh = &response->grh;
1922
Hal Rosenstock86dfbec2007-08-03 10:45:17 -07001923 agent_send_response(&response->mad.mad,
1924 &response->grh, wc,
1925 port_priv->device,
1926 smi_get_fwd_port(&recv->mad.smp),
1927 qp_info->qp->qp_num);
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04001928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 goto out;
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04001930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 }
1932
1933local:
1934 /* Give driver "right of first refusal" on incoming MAD */
1935 if (port_priv->device->process_mad) {
1936 int ret;
1937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 ret = port_priv->device->process_mad(port_priv->device, 0,
1939 port_priv->port_num,
1940 wc, &recv->grh,
1941 &recv->mad.mad,
1942 &response->mad.mad);
1943 if (ret & IB_MAD_RESULT_SUCCESS) {
1944 if (ret & IB_MAD_RESULT_CONSUMED)
1945 goto out;
1946 if (ret & IB_MAD_RESULT_REPLY) {
Sean Hefty34816ad2005-10-25 10:51:39 -07001947 agent_send_response(&response->mad.mad,
1948 &recv->grh, wc,
1949 port_priv->device,
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04001950 port_num,
Sean Hefty34816ad2005-10-25 10:51:39 -07001951 qp_info->qp->qp_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 goto out;
1953 }
1954 }
1955 }
1956
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07001957 mad_agent = find_mad_agent(port_priv, &recv->mad.mad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 if (mad_agent) {
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07001959 ib_mad_complete_recv(mad_agent, &recv->header.recv_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 /*
1961 * recv is freed up in error cases in ib_mad_complete_recv
1962 * or via recv_handler in ib_mad_complete_recv()
1963 */
1964 recv = NULL;
1965 }
1966
1967out:
1968 /* Post another receive request for this QP */
1969 if (response) {
1970 ib_mad_post_receive_mads(qp_info, response);
1971 if (recv)
1972 kmem_cache_free(ib_mad_cache, recv);
1973 } else
1974 ib_mad_post_receive_mads(qp_info, recv);
1975}
1976
1977static void adjust_timeout(struct ib_mad_agent_private *mad_agent_priv)
1978{
1979 struct ib_mad_send_wr_private *mad_send_wr;
1980 unsigned long delay;
1981
1982 if (list_empty(&mad_agent_priv->wait_list)) {
Roland Dreier6b2eef82009-09-07 08:27:50 -07001983 __cancel_delayed_work(&mad_agent_priv->timed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 } else {
1985 mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
1986 struct ib_mad_send_wr_private,
1987 agent_list);
1988
1989 if (time_after(mad_agent_priv->timeout,
1990 mad_send_wr->timeout)) {
1991 mad_agent_priv->timeout = mad_send_wr->timeout;
Roland Dreier6b2eef82009-09-07 08:27:50 -07001992 __cancel_delayed_work(&mad_agent_priv->timed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 delay = mad_send_wr->timeout - jiffies;
1994 if ((long)delay <= 0)
1995 delay = 1;
1996 queue_delayed_work(mad_agent_priv->qp_info->
1997 port_priv->wq,
1998 &mad_agent_priv->timed_work, delay);
1999 }
2000 }
2001}
2002
Hal Rosenstockd760ce82005-07-27 11:45:25 -07002003static void wait_for_response(struct ib_mad_send_wr_private *mad_send_wr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004{
Hal Rosenstockd760ce82005-07-27 11:45:25 -07002005 struct ib_mad_agent_private *mad_agent_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 struct ib_mad_send_wr_private *temp_mad_send_wr;
2007 struct list_head *list_item;
2008 unsigned long delay;
2009
Hal Rosenstockd760ce82005-07-27 11:45:25 -07002010 mad_agent_priv = mad_send_wr->mad_agent_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 list_del(&mad_send_wr->agent_list);
2012
2013 delay = mad_send_wr->timeout;
2014 mad_send_wr->timeout += jiffies;
2015
Hal Rosenstock29bb33d2005-07-27 11:45:32 -07002016 if (delay) {
2017 list_for_each_prev(list_item, &mad_agent_priv->wait_list) {
2018 temp_mad_send_wr = list_entry(list_item,
2019 struct ib_mad_send_wr_private,
2020 agent_list);
2021 if (time_after(mad_send_wr->timeout,
2022 temp_mad_send_wr->timeout))
2023 break;
2024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 }
Hal Rosenstock29bb33d2005-07-27 11:45:32 -07002026 else
2027 list_item = &mad_agent_priv->wait_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 list_add(&mad_send_wr->agent_list, list_item);
2029
2030 /* Reschedule a work item if we have a shorter timeout */
2031 if (mad_agent_priv->wait_list.next == &mad_send_wr->agent_list) {
Roland Dreier6b2eef82009-09-07 08:27:50 -07002032 __cancel_delayed_work(&mad_agent_priv->timed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 queue_delayed_work(mad_agent_priv->qp_info->port_priv->wq,
2034 &mad_agent_priv->timed_work, delay);
2035 }
2036}
2037
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002038void ib_reset_mad_timeout(struct ib_mad_send_wr_private *mad_send_wr,
2039 int timeout_ms)
2040{
2041 mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
2042 wait_for_response(mad_send_wr);
2043}
2044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045/*
2046 * Process a send work completion
2047 */
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002048void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr,
2049 struct ib_mad_send_wc *mad_send_wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
2051 struct ib_mad_agent_private *mad_agent_priv;
2052 unsigned long flags;
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002053 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Hal Rosenstockd760ce82005-07-27 11:45:25 -07002055 mad_agent_priv = mad_send_wr->mad_agent_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 spin_lock_irqsave(&mad_agent_priv->lock, flags);
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002057 if (mad_agent_priv->agent.rmpp_version) {
2058 ret = ib_process_rmpp_send_wc(mad_send_wr, mad_send_wc);
2059 if (ret == IB_RMPP_RESULT_CONSUMED)
2060 goto done;
2061 } else
2062 ret = IB_RMPP_RESULT_UNHANDLED;
2063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 if (mad_send_wc->status != IB_WC_SUCCESS &&
2065 mad_send_wr->status == IB_WC_SUCCESS) {
2066 mad_send_wr->status = mad_send_wc->status;
2067 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2068 }
2069
2070 if (--mad_send_wr->refcount > 0) {
2071 if (mad_send_wr->refcount == 1 && mad_send_wr->timeout &&
2072 mad_send_wr->status == IB_WC_SUCCESS) {
Hal Rosenstockd760ce82005-07-27 11:45:25 -07002073 wait_for_response(mad_send_wr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 }
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002075 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 }
2077
2078 /* Remove send from MAD agent and notify client of completion */
2079 list_del(&mad_send_wr->agent_list);
2080 adjust_timeout(mad_agent_priv);
2081 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2082
2083 if (mad_send_wr->status != IB_WC_SUCCESS )
2084 mad_send_wc->status = mad_send_wr->status;
Sean Hefty34816ad2005-10-25 10:51:39 -07002085 if (ret == IB_RMPP_RESULT_INTERNAL)
2086 ib_rmpp_send_handler(mad_send_wc);
2087 else
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002088 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2089 mad_send_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
2091 /* Release reference on agent taken when sending */
Sean Hefty1b52fa982006-05-12 14:57:52 -07002092 deref_mad_agent(mad_agent_priv);
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002093 return;
2094done:
2095 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096}
2097
2098static void ib_mad_send_done_handler(struct ib_mad_port_private *port_priv,
2099 struct ib_wc *wc)
2100{
2101 struct ib_mad_send_wr_private *mad_send_wr, *queued_send_wr;
2102 struct ib_mad_list_head *mad_list;
2103 struct ib_mad_qp_info *qp_info;
2104 struct ib_mad_queue *send_queue;
2105 struct ib_send_wr *bad_send_wr;
Sean Hefty34816ad2005-10-25 10:51:39 -07002106 struct ib_mad_send_wc mad_send_wc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 unsigned long flags;
2108 int ret;
2109
2110 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
2111 mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
2112 mad_list);
2113 send_queue = mad_list->mad_queue;
2114 qp_info = send_queue->qp_info;
2115
2116retry:
Ralph Campbell15271062006-12-12 14:28:30 -08002117 ib_dma_unmap_single(mad_send_wr->send_buf.mad_agent->device,
2118 mad_send_wr->header_mapping,
2119 mad_send_wr->sg_list[0].length, DMA_TO_DEVICE);
2120 ib_dma_unmap_single(mad_send_wr->send_buf.mad_agent->device,
2121 mad_send_wr->payload_mapping,
2122 mad_send_wr->sg_list[1].length, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 queued_send_wr = NULL;
2124 spin_lock_irqsave(&send_queue->lock, flags);
2125 list_del(&mad_list->list);
2126
2127 /* Move queued send to the send queue */
2128 if (send_queue->count-- > send_queue->max_active) {
2129 mad_list = container_of(qp_info->overflow_list.next,
2130 struct ib_mad_list_head, list);
2131 queued_send_wr = container_of(mad_list,
2132 struct ib_mad_send_wr_private,
2133 mad_list);
Akinobu Mita179e0912006-06-26 00:24:41 -07002134 list_move_tail(&mad_list->list, &send_queue->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 }
2136 spin_unlock_irqrestore(&send_queue->lock, flags);
2137
Sean Hefty34816ad2005-10-25 10:51:39 -07002138 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2139 mad_send_wc.status = wc->status;
2140 mad_send_wc.vendor_err = wc->vendor_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 if (atomic_read(&qp_info->snoop_count))
Sean Hefty34816ad2005-10-25 10:51:39 -07002142 snoop_send(qp_info, &mad_send_wr->send_buf, &mad_send_wc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 IB_MAD_SNOOP_SEND_COMPLETIONS);
Sean Hefty34816ad2005-10-25 10:51:39 -07002144 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
2146 if (queued_send_wr) {
2147 ret = ib_post_send(qp_info->qp, &queued_send_wr->send_wr,
Sean Hefty34816ad2005-10-25 10:51:39 -07002148 &bad_send_wr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 if (ret) {
2150 printk(KERN_ERR PFX "ib_post_send failed: %d\n", ret);
2151 mad_send_wr = queued_send_wr;
2152 wc->status = IB_WC_LOC_QP_OP_ERR;
2153 goto retry;
2154 }
2155 }
2156}
2157
2158static void mark_sends_for_retry(struct ib_mad_qp_info *qp_info)
2159{
2160 struct ib_mad_send_wr_private *mad_send_wr;
2161 struct ib_mad_list_head *mad_list;
2162 unsigned long flags;
2163
2164 spin_lock_irqsave(&qp_info->send_queue.lock, flags);
2165 list_for_each_entry(mad_list, &qp_info->send_queue.list, list) {
2166 mad_send_wr = container_of(mad_list,
2167 struct ib_mad_send_wr_private,
2168 mad_list);
2169 mad_send_wr->retry = 1;
2170 }
2171 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
2172}
2173
2174static void mad_error_handler(struct ib_mad_port_private *port_priv,
2175 struct ib_wc *wc)
2176{
2177 struct ib_mad_list_head *mad_list;
2178 struct ib_mad_qp_info *qp_info;
2179 struct ib_mad_send_wr_private *mad_send_wr;
2180 int ret;
2181
2182 /* Determine if failure was a send or receive */
2183 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
2184 qp_info = mad_list->mad_queue->qp_info;
2185 if (mad_list->mad_queue == &qp_info->recv_queue)
2186 /*
2187 * Receive errors indicate that the QP has entered the error
2188 * state - error handling/shutdown code will cleanup
2189 */
2190 return;
2191
2192 /*
2193 * Send errors will transition the QP to SQE - move
2194 * QP to RTS and repost flushed work requests
2195 */
2196 mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
2197 mad_list);
2198 if (wc->status == IB_WC_WR_FLUSH_ERR) {
2199 if (mad_send_wr->retry) {
2200 /* Repost send */
2201 struct ib_send_wr *bad_send_wr;
2202
2203 mad_send_wr->retry = 0;
2204 ret = ib_post_send(qp_info->qp, &mad_send_wr->send_wr,
2205 &bad_send_wr);
2206 if (ret)
2207 ib_mad_send_done_handler(port_priv, wc);
2208 } else
2209 ib_mad_send_done_handler(port_priv, wc);
2210 } else {
2211 struct ib_qp_attr *attr;
2212
2213 /* Transition QP to RTS and fail offending send */
2214 attr = kmalloc(sizeof *attr, GFP_KERNEL);
2215 if (attr) {
2216 attr->qp_state = IB_QPS_RTS;
2217 attr->cur_qp_state = IB_QPS_SQE;
2218 ret = ib_modify_qp(qp_info->qp, attr,
2219 IB_QP_STATE | IB_QP_CUR_STATE);
2220 kfree(attr);
2221 if (ret)
2222 printk(KERN_ERR PFX "mad_error_handler - "
2223 "ib_modify_qp to RTS : %d\n", ret);
2224 else
2225 mark_sends_for_retry(qp_info);
2226 }
2227 ib_mad_send_done_handler(port_priv, wc);
2228 }
2229}
2230
2231/*
2232 * IB MAD completion callback
2233 */
David Howellsc4028952006-11-22 14:57:56 +00002234static void ib_mad_completion_handler(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235{
2236 struct ib_mad_port_private *port_priv;
2237 struct ib_wc wc;
2238
David Howellsc4028952006-11-22 14:57:56 +00002239 port_priv = container_of(work, struct ib_mad_port_private, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
2241
2242 while (ib_poll_cq(port_priv->cq, 1, &wc) == 1) {
2243 if (wc.status == IB_WC_SUCCESS) {
2244 switch (wc.opcode) {
2245 case IB_WC_SEND:
2246 ib_mad_send_done_handler(port_priv, &wc);
2247 break;
2248 case IB_WC_RECV:
2249 ib_mad_recv_done_handler(port_priv, &wc);
2250 break;
2251 default:
2252 BUG_ON(1);
2253 break;
2254 }
2255 } else
2256 mad_error_handler(port_priv, &wc);
2257 }
2258}
2259
2260static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv)
2261{
2262 unsigned long flags;
2263 struct ib_mad_send_wr_private *mad_send_wr, *temp_mad_send_wr;
2264 struct ib_mad_send_wc mad_send_wc;
2265 struct list_head cancel_list;
2266
2267 INIT_LIST_HEAD(&cancel_list);
2268
2269 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2270 list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
2271 &mad_agent_priv->send_list, agent_list) {
2272 if (mad_send_wr->status == IB_WC_SUCCESS) {
Roland Dreier3cd96562006-09-22 15:22:46 -07002273 mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2275 }
2276 }
2277
2278 /* Empty wait list to prevent receives from finding a request */
2279 list_splice_init(&mad_agent_priv->wait_list, &cancel_list);
2280 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2281
2282 /* Report all cancelled requests */
2283 mad_send_wc.status = IB_WC_WR_FLUSH_ERR;
2284 mad_send_wc.vendor_err = 0;
2285
2286 list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
2287 &cancel_list, agent_list) {
Sean Hefty34816ad2005-10-25 10:51:39 -07002288 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2289 list_del(&mad_send_wr->agent_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2291 &mad_send_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 atomic_dec(&mad_agent_priv->refcount);
2293 }
2294}
2295
2296static struct ib_mad_send_wr_private*
Sean Hefty34816ad2005-10-25 10:51:39 -07002297find_send_wr(struct ib_mad_agent_private *mad_agent_priv,
2298 struct ib_mad_send_buf *send_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299{
2300 struct ib_mad_send_wr_private *mad_send_wr;
2301
2302 list_for_each_entry(mad_send_wr, &mad_agent_priv->wait_list,
2303 agent_list) {
Sean Hefty34816ad2005-10-25 10:51:39 -07002304 if (&mad_send_wr->send_buf == send_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 return mad_send_wr;
2306 }
2307
2308 list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list,
2309 agent_list) {
Sean Hefty34816ad2005-10-25 10:51:39 -07002310 if (is_data_mad(mad_agent_priv, mad_send_wr->send_buf.mad) &&
2311 &mad_send_wr->send_buf == send_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 return mad_send_wr;
2313 }
2314 return NULL;
2315}
2316
Sean Hefty34816ad2005-10-25 10:51:39 -07002317int ib_modify_mad(struct ib_mad_agent *mad_agent,
2318 struct ib_mad_send_buf *send_buf, u32 timeout_ms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319{
2320 struct ib_mad_agent_private *mad_agent_priv;
2321 struct ib_mad_send_wr_private *mad_send_wr;
2322 unsigned long flags;
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07002323 int active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
2325 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
2326 agent);
2327 spin_lock_irqsave(&mad_agent_priv->lock, flags);
Sean Hefty34816ad2005-10-25 10:51:39 -07002328 mad_send_wr = find_send_wr(mad_agent_priv, send_buf);
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002329 if (!mad_send_wr || mad_send_wr->status != IB_WC_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002331 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 }
2333
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07002334 active = (!mad_send_wr->timeout || mad_send_wr->refcount > 1);
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002335 if (!timeout_ms) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002337 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 }
2339
Sean Hefty34816ad2005-10-25 10:51:39 -07002340 mad_send_wr->send_buf.timeout_ms = timeout_ms;
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07002341 if (active)
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002342 mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
2343 else
2344 ib_reset_mad_timeout(mad_send_wr, timeout_ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002346 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2347 return 0;
2348}
2349EXPORT_SYMBOL(ib_modify_mad);
2350
Sean Hefty34816ad2005-10-25 10:51:39 -07002351void ib_cancel_mad(struct ib_mad_agent *mad_agent,
2352 struct ib_mad_send_buf *send_buf)
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002353{
Sean Hefty34816ad2005-10-25 10:51:39 -07002354 ib_modify_mad(mad_agent, send_buf, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355}
2356EXPORT_SYMBOL(ib_cancel_mad);
2357
David Howellsc4028952006-11-22 14:57:56 +00002358static void local_completions(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359{
2360 struct ib_mad_agent_private *mad_agent_priv;
2361 struct ib_mad_local_private *local;
2362 struct ib_mad_agent_private *recv_mad_agent;
2363 unsigned long flags;
Ralph Campbell1d9bc6d62009-02-27 10:34:30 -08002364 int free_mad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 struct ib_wc wc;
2366 struct ib_mad_send_wc mad_send_wc;
2367
David Howellsc4028952006-11-22 14:57:56 +00002368 mad_agent_priv =
2369 container_of(work, struct ib_mad_agent_private, local_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
2371 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2372 while (!list_empty(&mad_agent_priv->local_list)) {
2373 local = list_entry(mad_agent_priv->local_list.next,
2374 struct ib_mad_local_private,
2375 completion_list);
Michael S. Tsirkin37289ef2006-03-30 15:52:54 +02002376 list_del(&local->completion_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
Ralph Campbell1d9bc6d62009-02-27 10:34:30 -08002378 free_mad = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 if (local->mad_priv) {
2380 recv_mad_agent = local->recv_mad_agent;
2381 if (!recv_mad_agent) {
2382 printk(KERN_ERR PFX "No receive MAD agent for local completion\n");
Ralph Campbell1d9bc6d62009-02-27 10:34:30 -08002383 free_mad = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 goto local_send_completion;
2385 }
2386
2387 /*
2388 * Defined behavior is to complete response
2389 * before request
2390 */
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +02002391 build_smp_wc(recv_mad_agent->agent.qp,
2392 (unsigned long) local->mad_send_wr,
Sean Hefty97f52eb2005-08-13 21:05:57 -07002393 be16_to_cpu(IB_LID_PERMISSIVE),
Sean Hefty34816ad2005-10-25 10:51:39 -07002394 0, recv_mad_agent->agent.port_num, &wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
2396 local->mad_priv->header.recv_wc.wc = &wc;
2397 local->mad_priv->header.recv_wc.mad_len =
2398 sizeof(struct ib_mad);
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002399 INIT_LIST_HEAD(&local->mad_priv->header.recv_wc.rmpp_list);
2400 list_add(&local->mad_priv->header.recv_wc.recv_buf.list,
2401 &local->mad_priv->header.recv_wc.rmpp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 local->mad_priv->header.recv_wc.recv_buf.grh = NULL;
2403 local->mad_priv->header.recv_wc.recv_buf.mad =
2404 &local->mad_priv->mad.mad;
2405 if (atomic_read(&recv_mad_agent->qp_info->snoop_count))
2406 snoop_recv(recv_mad_agent->qp_info,
2407 &local->mad_priv->header.recv_wc,
2408 IB_MAD_SNOOP_RECVS);
2409 recv_mad_agent->agent.recv_handler(
2410 &recv_mad_agent->agent,
2411 &local->mad_priv->header.recv_wc);
2412 spin_lock_irqsave(&recv_mad_agent->lock, flags);
2413 atomic_dec(&recv_mad_agent->refcount);
2414 spin_unlock_irqrestore(&recv_mad_agent->lock, flags);
2415 }
2416
2417local_send_completion:
2418 /* Complete send */
2419 mad_send_wc.status = IB_WC_SUCCESS;
2420 mad_send_wc.vendor_err = 0;
Sean Hefty34816ad2005-10-25 10:51:39 -07002421 mad_send_wc.send_buf = &local->mad_send_wr->send_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 if (atomic_read(&mad_agent_priv->qp_info->snoop_count))
Sean Hefty34816ad2005-10-25 10:51:39 -07002423 snoop_send(mad_agent_priv->qp_info,
2424 &local->mad_send_wr->send_buf,
2425 &mad_send_wc, IB_MAD_SNOOP_SEND_COMPLETIONS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2427 &mad_send_wc);
2428
2429 spin_lock_irqsave(&mad_agent_priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 atomic_dec(&mad_agent_priv->refcount);
Ralph Campbell1d9bc6d62009-02-27 10:34:30 -08002431 if (free_mad)
Hal Rosenstock2c153b92005-07-27 11:45:31 -07002432 kmem_cache_free(ib_mad_cache, local->mad_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 kfree(local);
2434 }
2435 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2436}
2437
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002438static int retry_send(struct ib_mad_send_wr_private *mad_send_wr)
2439{
2440 int ret;
2441
Sean Hefty4fc8cd42007-11-27 00:11:04 -08002442 if (!mad_send_wr->retries_left)
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002443 return -ETIMEDOUT;
2444
Sean Hefty4fc8cd42007-11-27 00:11:04 -08002445 mad_send_wr->retries_left--;
2446 mad_send_wr->send_buf.retries++;
2447
Sean Hefty34816ad2005-10-25 10:51:39 -07002448 mad_send_wr->timeout = msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms);
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002449
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002450 if (mad_send_wr->mad_agent_priv->agent.rmpp_version) {
2451 ret = ib_retry_rmpp(mad_send_wr);
2452 switch (ret) {
2453 case IB_RMPP_RESULT_UNHANDLED:
2454 ret = ib_send_mad(mad_send_wr);
2455 break;
2456 case IB_RMPP_RESULT_CONSUMED:
2457 ret = 0;
2458 break;
2459 default:
2460 ret = -ECOMM;
2461 break;
2462 }
2463 } else
2464 ret = ib_send_mad(mad_send_wr);
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002465
2466 if (!ret) {
2467 mad_send_wr->refcount++;
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002468 list_add_tail(&mad_send_wr->agent_list,
2469 &mad_send_wr->mad_agent_priv->send_list);
2470 }
2471 return ret;
2472}
2473
David Howellsc4028952006-11-22 14:57:56 +00002474static void timeout_sends(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475{
2476 struct ib_mad_agent_private *mad_agent_priv;
2477 struct ib_mad_send_wr_private *mad_send_wr;
2478 struct ib_mad_send_wc mad_send_wc;
2479 unsigned long flags, delay;
2480
David Howellsc4028952006-11-22 14:57:56 +00002481 mad_agent_priv = container_of(work, struct ib_mad_agent_private,
2482 timed_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 mad_send_wc.vendor_err = 0;
2484
2485 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2486 while (!list_empty(&mad_agent_priv->wait_list)) {
2487 mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
2488 struct ib_mad_send_wr_private,
2489 agent_list);
2490
2491 if (time_after(mad_send_wr->timeout, jiffies)) {
2492 delay = mad_send_wr->timeout - jiffies;
2493 if ((long)delay <= 0)
2494 delay = 1;
2495 queue_delayed_work(mad_agent_priv->qp_info->
2496 port_priv->wq,
2497 &mad_agent_priv->timed_work, delay);
2498 break;
2499 }
2500
Hal Rosenstockdbf92272005-07-27 11:45:30 -07002501 list_del(&mad_send_wr->agent_list);
Hal Rosenstock29bb33d2005-07-27 11:45:32 -07002502 if (mad_send_wr->status == IB_WC_SUCCESS &&
2503 !retry_send(mad_send_wr))
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002504 continue;
2505
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2507
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002508 if (mad_send_wr->status == IB_WC_SUCCESS)
2509 mad_send_wc.status = IB_WC_RESP_TIMEOUT_ERR;
2510 else
2511 mad_send_wc.status = mad_send_wr->status;
Sean Hefty34816ad2005-10-25 10:51:39 -07002512 mad_send_wc.send_buf = &mad_send_wr->send_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2514 &mad_send_wc);
2515
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 atomic_dec(&mad_agent_priv->refcount);
2517 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2518 }
2519 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2520}
2521
Hal Rosenstock5dd2ce12005-08-15 14:16:36 -07002522static void ib_mad_thread_completion_handler(struct ib_cq *cq, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523{
2524 struct ib_mad_port_private *port_priv = cq->cq_context;
Michael S. Tsirkindc059802006-03-20 10:08:25 -08002525 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
Michael S. Tsirkindc059802006-03-20 10:08:25 -08002527 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2528 if (!list_empty(&port_priv->port_list))
2529 queue_work(port_priv->wq, &port_priv->work);
2530 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531}
2532
2533/*
2534 * Allocate receive MADs and post receive WRs for them
2535 */
2536static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
2537 struct ib_mad_private *mad)
2538{
2539 unsigned long flags;
2540 int post, ret;
2541 struct ib_mad_private *mad_priv;
2542 struct ib_sge sg_list;
2543 struct ib_recv_wr recv_wr, *bad_recv_wr;
2544 struct ib_mad_queue *recv_queue = &qp_info->recv_queue;
2545
2546 /* Initialize common scatter list fields */
2547 sg_list.length = sizeof *mad_priv - sizeof mad_priv->header;
2548 sg_list.lkey = (*qp_info->port_priv->mr).lkey;
2549
2550 /* Initialize common receive WR fields */
2551 recv_wr.next = NULL;
2552 recv_wr.sg_list = &sg_list;
2553 recv_wr.num_sge = 1;
2554
2555 do {
2556 /* Allocate and map receive buffer */
2557 if (mad) {
2558 mad_priv = mad;
2559 mad = NULL;
2560 } else {
2561 mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL);
2562 if (!mad_priv) {
2563 printk(KERN_ERR PFX "No memory for receive buffer\n");
2564 ret = -ENOMEM;
2565 break;
2566 }
2567 }
Ralph Campbell15271062006-12-12 14:28:30 -08002568 sg_list.addr = ib_dma_map_single(qp_info->port_priv->device,
2569 &mad_priv->grh,
2570 sizeof *mad_priv -
2571 sizeof mad_priv->header,
2572 DMA_FROM_DEVICE);
2573 mad_priv->header.mapping = sg_list.addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 recv_wr.wr_id = (unsigned long)&mad_priv->header.mad_list;
2575 mad_priv->header.mad_list.mad_queue = recv_queue;
2576
2577 /* Post receive WR */
2578 spin_lock_irqsave(&recv_queue->lock, flags);
2579 post = (++recv_queue->count < recv_queue->max_active);
2580 list_add_tail(&mad_priv->header.mad_list.list, &recv_queue->list);
2581 spin_unlock_irqrestore(&recv_queue->lock, flags);
2582 ret = ib_post_recv(qp_info->qp, &recv_wr, &bad_recv_wr);
2583 if (ret) {
2584 spin_lock_irqsave(&recv_queue->lock, flags);
2585 list_del(&mad_priv->header.mad_list.list);
2586 recv_queue->count--;
2587 spin_unlock_irqrestore(&recv_queue->lock, flags);
Ralph Campbell15271062006-12-12 14:28:30 -08002588 ib_dma_unmap_single(qp_info->port_priv->device,
2589 mad_priv->header.mapping,
2590 sizeof *mad_priv -
2591 sizeof mad_priv->header,
2592 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 kmem_cache_free(ib_mad_cache, mad_priv);
2594 printk(KERN_ERR PFX "ib_post_recv failed: %d\n", ret);
2595 break;
2596 }
2597 } while (post);
2598
2599 return ret;
2600}
2601
2602/*
2603 * Return all the posted receive MADs
2604 */
2605static void cleanup_recv_queue(struct ib_mad_qp_info *qp_info)
2606{
2607 struct ib_mad_private_header *mad_priv_hdr;
2608 struct ib_mad_private *recv;
2609 struct ib_mad_list_head *mad_list;
2610
Eli Cohenfac70d52010-09-27 17:51:11 -07002611 if (!qp_info->qp)
2612 return;
2613
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 while (!list_empty(&qp_info->recv_queue.list)) {
2615
2616 mad_list = list_entry(qp_info->recv_queue.list.next,
2617 struct ib_mad_list_head, list);
2618 mad_priv_hdr = container_of(mad_list,
2619 struct ib_mad_private_header,
2620 mad_list);
2621 recv = container_of(mad_priv_hdr, struct ib_mad_private,
2622 header);
2623
2624 /* Remove from posted receive MAD list */
2625 list_del(&mad_list->list);
2626
Ralph Campbell15271062006-12-12 14:28:30 -08002627 ib_dma_unmap_single(qp_info->port_priv->device,
2628 recv->header.mapping,
2629 sizeof(struct ib_mad_private) -
2630 sizeof(struct ib_mad_private_header),
2631 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 kmem_cache_free(ib_mad_cache, recv);
2633 }
2634
2635 qp_info->recv_queue.count = 0;
2636}
2637
2638/*
2639 * Start the port
2640 */
2641static int ib_mad_port_start(struct ib_mad_port_private *port_priv)
2642{
2643 int ret, i;
2644 struct ib_qp_attr *attr;
2645 struct ib_qp *qp;
2646
2647 attr = kmalloc(sizeof *attr, GFP_KERNEL);
Roland Dreier3cd96562006-09-22 15:22:46 -07002648 if (!attr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 printk(KERN_ERR PFX "Couldn't kmalloc ib_qp_attr\n");
2650 return -ENOMEM;
2651 }
2652
2653 for (i = 0; i < IB_MAD_QPS_CORE; i++) {
2654 qp = port_priv->qp_info[i].qp;
Eli Cohenfac70d52010-09-27 17:51:11 -07002655 if (!qp)
2656 continue;
2657
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 /*
2659 * PKey index for QP1 is irrelevant but
2660 * one is needed for the Reset to Init transition
2661 */
2662 attr->qp_state = IB_QPS_INIT;
2663 attr->pkey_index = 0;
2664 attr->qkey = (qp->qp_num == 0) ? 0 : IB_QP1_QKEY;
2665 ret = ib_modify_qp(qp, attr, IB_QP_STATE |
2666 IB_QP_PKEY_INDEX | IB_QP_QKEY);
2667 if (ret) {
2668 printk(KERN_ERR PFX "Couldn't change QP%d state to "
2669 "INIT: %d\n", i, ret);
2670 goto out;
2671 }
2672
2673 attr->qp_state = IB_QPS_RTR;
2674 ret = ib_modify_qp(qp, attr, IB_QP_STATE);
2675 if (ret) {
2676 printk(KERN_ERR PFX "Couldn't change QP%d state to "
2677 "RTR: %d\n", i, ret);
2678 goto out;
2679 }
2680
2681 attr->qp_state = IB_QPS_RTS;
2682 attr->sq_psn = IB_MAD_SEND_Q_PSN;
2683 ret = ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_SQ_PSN);
2684 if (ret) {
2685 printk(KERN_ERR PFX "Couldn't change QP%d state to "
2686 "RTS: %d\n", i, ret);
2687 goto out;
2688 }
2689 }
2690
2691 ret = ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
2692 if (ret) {
2693 printk(KERN_ERR PFX "Failed to request completion "
2694 "notification: %d\n", ret);
2695 goto out;
2696 }
2697
2698 for (i = 0; i < IB_MAD_QPS_CORE; i++) {
Eli Cohenfac70d52010-09-27 17:51:11 -07002699 if (!port_priv->qp_info[i].qp)
2700 continue;
2701
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 ret = ib_mad_post_receive_mads(&port_priv->qp_info[i], NULL);
2703 if (ret) {
2704 printk(KERN_ERR PFX "Couldn't post receive WRs\n");
2705 goto out;
2706 }
2707 }
2708out:
2709 kfree(attr);
2710 return ret;
2711}
2712
2713static void qp_event_handler(struct ib_event *event, void *qp_context)
2714{
2715 struct ib_mad_qp_info *qp_info = qp_context;
2716
2717 /* It's worse than that! He's dead, Jim! */
2718 printk(KERN_ERR PFX "Fatal error (%d) on MAD QP (%d)\n",
2719 event->event, qp_info->qp->qp_num);
2720}
2721
2722static void init_mad_queue(struct ib_mad_qp_info *qp_info,
2723 struct ib_mad_queue *mad_queue)
2724{
2725 mad_queue->qp_info = qp_info;
2726 mad_queue->count = 0;
2727 spin_lock_init(&mad_queue->lock);
2728 INIT_LIST_HEAD(&mad_queue->list);
2729}
2730
2731static void init_mad_qp(struct ib_mad_port_private *port_priv,
2732 struct ib_mad_qp_info *qp_info)
2733{
2734 qp_info->port_priv = port_priv;
2735 init_mad_queue(qp_info, &qp_info->send_queue);
2736 init_mad_queue(qp_info, &qp_info->recv_queue);
2737 INIT_LIST_HEAD(&qp_info->overflow_list);
2738 spin_lock_init(&qp_info->snoop_lock);
2739 qp_info->snoop_table = NULL;
2740 qp_info->snoop_table_size = 0;
2741 atomic_set(&qp_info->snoop_count, 0);
2742}
2743
2744static int create_mad_qp(struct ib_mad_qp_info *qp_info,
2745 enum ib_qp_type qp_type)
2746{
2747 struct ib_qp_init_attr qp_init_attr;
2748 int ret;
2749
2750 memset(&qp_init_attr, 0, sizeof qp_init_attr);
2751 qp_init_attr.send_cq = qp_info->port_priv->cq;
2752 qp_init_attr.recv_cq = qp_info->port_priv->cq;
2753 qp_init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
Hal Rosenstockb76aabc2009-09-07 08:28:48 -07002754 qp_init_attr.cap.max_send_wr = mad_sendq_size;
2755 qp_init_attr.cap.max_recv_wr = mad_recvq_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 qp_init_attr.cap.max_send_sge = IB_MAD_SEND_REQ_MAX_SG;
2757 qp_init_attr.cap.max_recv_sge = IB_MAD_RECV_REQ_MAX_SG;
2758 qp_init_attr.qp_type = qp_type;
2759 qp_init_attr.port_num = qp_info->port_priv->port_num;
2760 qp_init_attr.qp_context = qp_info;
2761 qp_init_attr.event_handler = qp_event_handler;
2762 qp_info->qp = ib_create_qp(qp_info->port_priv->pd, &qp_init_attr);
2763 if (IS_ERR(qp_info->qp)) {
2764 printk(KERN_ERR PFX "Couldn't create ib_mad QP%d\n",
2765 get_spl_qp_index(qp_type));
2766 ret = PTR_ERR(qp_info->qp);
2767 goto error;
2768 }
2769 /* Use minimum queue sizes unless the CQ is resized */
Hal Rosenstockb76aabc2009-09-07 08:28:48 -07002770 qp_info->send_queue.max_active = mad_sendq_size;
2771 qp_info->recv_queue.max_active = mad_recvq_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 return 0;
2773
2774error:
2775 return ret;
2776}
2777
2778static void destroy_mad_qp(struct ib_mad_qp_info *qp_info)
2779{
Eli Cohenfac70d52010-09-27 17:51:11 -07002780 if (!qp_info->qp)
2781 return;
2782
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 ib_destroy_qp(qp_info->qp);
Jesper Juhl6044ec82005-11-07 01:01:32 -08002784 kfree(qp_info->snoop_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785}
2786
2787/*
2788 * Open the port
2789 * Create the QP, PD, MR, and CQ if needed
2790 */
2791static int ib_mad_port_open(struct ib_device *device,
2792 int port_num)
2793{
2794 int ret, cq_size;
2795 struct ib_mad_port_private *port_priv;
2796 unsigned long flags;
2797 char name[sizeof "ib_mad123"];
Eli Cohenfac70d52010-09-27 17:51:11 -07002798 int has_smi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 /* Create new device info */
Roland Dreierde6eb662005-11-02 07:23:14 -08002801 port_priv = kzalloc(sizeof *port_priv, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 if (!port_priv) {
2803 printk(KERN_ERR PFX "No memory for ib_mad_port_private\n");
2804 return -ENOMEM;
2805 }
Roland Dreierde6eb662005-11-02 07:23:14 -08002806
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 port_priv->device = device;
2808 port_priv->port_num = port_num;
2809 spin_lock_init(&port_priv->reg_lock);
2810 INIT_LIST_HEAD(&port_priv->agent_list);
2811 init_mad_qp(port_priv, &port_priv->qp_info[0]);
2812 init_mad_qp(port_priv, &port_priv->qp_info[1]);
2813
Eli Cohenfac70d52010-09-27 17:51:11 -07002814 cq_size = mad_sendq_size + mad_recvq_size;
2815 has_smi = rdma_port_get_link_layer(device, port_num) == IB_LINK_LAYER_INFINIBAND;
2816 if (has_smi)
2817 cq_size *= 2;
2818
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 port_priv->cq = ib_create_cq(port_priv->device,
Hal Rosenstock5dd2ce12005-08-15 14:16:36 -07002820 ib_mad_thread_completion_handler,
Michael S. Tsirkinf4fd0b22007-05-03 13:48:47 +03002821 NULL, port_priv, cq_size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 if (IS_ERR(port_priv->cq)) {
2823 printk(KERN_ERR PFX "Couldn't create ib_mad CQ\n");
2824 ret = PTR_ERR(port_priv->cq);
2825 goto error3;
2826 }
2827
2828 port_priv->pd = ib_alloc_pd(device);
2829 if (IS_ERR(port_priv->pd)) {
2830 printk(KERN_ERR PFX "Couldn't create ib_mad PD\n");
2831 ret = PTR_ERR(port_priv->pd);
2832 goto error4;
2833 }
2834
2835 port_priv->mr = ib_get_dma_mr(port_priv->pd, IB_ACCESS_LOCAL_WRITE);
2836 if (IS_ERR(port_priv->mr)) {
2837 printk(KERN_ERR PFX "Couldn't get ib_mad DMA MR\n");
2838 ret = PTR_ERR(port_priv->mr);
2839 goto error5;
2840 }
2841
Eli Cohenfac70d52010-09-27 17:51:11 -07002842 if (has_smi) {
2843 ret = create_mad_qp(&port_priv->qp_info[0], IB_QPT_SMI);
2844 if (ret)
2845 goto error6;
2846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 ret = create_mad_qp(&port_priv->qp_info[1], IB_QPT_GSI);
2848 if (ret)
2849 goto error7;
2850
2851 snprintf(name, sizeof name, "ib_mad%d", port_num);
2852 port_priv->wq = create_singlethread_workqueue(name);
2853 if (!port_priv->wq) {
2854 ret = -ENOMEM;
2855 goto error8;
2856 }
David Howellsc4028952006-11-22 14:57:56 +00002857 INIT_WORK(&port_priv->work, ib_mad_completion_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
Michael S. Tsirkindc059802006-03-20 10:08:25 -08002859 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2860 list_add_tail(&port_priv->port_list, &ib_mad_port_list);
2861 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2862
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 ret = ib_mad_port_start(port_priv);
2864 if (ret) {
2865 printk(KERN_ERR PFX "Couldn't start port\n");
2866 goto error9;
2867 }
2868
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 return 0;
2870
2871error9:
Michael S. Tsirkindc059802006-03-20 10:08:25 -08002872 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2873 list_del_init(&port_priv->port_list);
2874 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2875
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 destroy_workqueue(port_priv->wq);
2877error8:
2878 destroy_mad_qp(&port_priv->qp_info[1]);
2879error7:
2880 destroy_mad_qp(&port_priv->qp_info[0]);
2881error6:
2882 ib_dereg_mr(port_priv->mr);
2883error5:
2884 ib_dealloc_pd(port_priv->pd);
2885error4:
2886 ib_destroy_cq(port_priv->cq);
2887 cleanup_recv_queue(&port_priv->qp_info[1]);
2888 cleanup_recv_queue(&port_priv->qp_info[0]);
2889error3:
2890 kfree(port_priv);
2891
2892 return ret;
2893}
2894
2895/*
2896 * Close the port
2897 * If there are no classes using the port, free the port
2898 * resources (CQ, MR, PD, QP) and remove the port's info structure
2899 */
2900static int ib_mad_port_close(struct ib_device *device, int port_num)
2901{
2902 struct ib_mad_port_private *port_priv;
2903 unsigned long flags;
2904
2905 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2906 port_priv = __ib_get_mad_port(device, port_num);
2907 if (port_priv == NULL) {
2908 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2909 printk(KERN_ERR PFX "Port %d not found\n", port_num);
2910 return -ENODEV;
2911 }
Michael S. Tsirkindc059802006-03-20 10:08:25 -08002912 list_del_init(&port_priv->port_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2914
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 destroy_workqueue(port_priv->wq);
2916 destroy_mad_qp(&port_priv->qp_info[1]);
2917 destroy_mad_qp(&port_priv->qp_info[0]);
2918 ib_dereg_mr(port_priv->mr);
2919 ib_dealloc_pd(port_priv->pd);
2920 ib_destroy_cq(port_priv->cq);
2921 cleanup_recv_queue(&port_priv->qp_info[1]);
2922 cleanup_recv_queue(&port_priv->qp_info[0]);
2923 /* XXX: Handle deallocation of MAD registration tables */
2924
2925 kfree(port_priv);
2926
2927 return 0;
2928}
2929
2930static void ib_mad_init_device(struct ib_device *device)
2931{
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002932 int start, end, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
Tom Tucker07ebafb2006-08-03 16:02:42 -05002934 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
2935 return;
2936
2937 if (device->node_type == RDMA_NODE_IB_SWITCH) {
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002938 start = 0;
2939 end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 } else {
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002941 start = 1;
2942 end = device->phys_port_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 }
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002944
2945 for (i = start; i <= end; i++) {
2946 if (ib_mad_port_open(device, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 printk(KERN_ERR PFX "Couldn't open %s port %d\n",
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002948 device->name, i);
2949 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 }
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002951 if (ib_agent_port_open(device, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 printk(KERN_ERR PFX "Couldn't open %s port %d "
2953 "for agents\n",
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002954 device->name, i);
2955 goto error_agent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 }
2957 }
Hal Rosenstockf68bcc22005-07-27 11:45:27 -07002958 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002960error_agent:
2961 if (ib_mad_port_close(device, i))
2962 printk(KERN_ERR PFX "Couldn't close %s port %d\n",
2963 device->name, i);
2964
2965error:
2966 i--;
2967
2968 while (i >= start) {
2969 if (ib_agent_port_close(device, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 printk(KERN_ERR PFX "Couldn't close %s port %d "
2971 "for agents\n",
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002972 device->name, i);
2973 if (ib_mad_port_close(device, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 printk(KERN_ERR PFX "Couldn't close %s port %d\n",
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002975 device->name, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 i--;
2977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978}
2979
2980static void ib_mad_remove_device(struct ib_device *device)
2981{
Hal Rosenstockf68bcc22005-07-27 11:45:27 -07002982 int i, num_ports, cur_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
Steve Wise070e1402010-03-04 18:18:18 +00002984 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
2985 return;
2986
Tom Tucker07ebafb2006-08-03 16:02:42 -05002987 if (device->node_type == RDMA_NODE_IB_SWITCH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 num_ports = 1;
2989 cur_port = 0;
2990 } else {
2991 num_ports = device->phys_port_cnt;
2992 cur_port = 1;
2993 }
2994 for (i = 0; i < num_ports; i++, cur_port++) {
Hal Rosenstockf68bcc22005-07-27 11:45:27 -07002995 if (ib_agent_port_close(device, cur_port))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 printk(KERN_ERR PFX "Couldn't close %s port %d "
2997 "for agents\n",
2998 device->name, cur_port);
Hal Rosenstockf68bcc22005-07-27 11:45:27 -07002999 if (ib_mad_port_close(device, cur_port))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 printk(KERN_ERR PFX "Couldn't close %s port %d\n",
3001 device->name, cur_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 }
3003}
3004
3005static struct ib_client mad_client = {
3006 .name = "mad",
3007 .add = ib_mad_init_device,
3008 .remove = ib_mad_remove_device
3009};
3010
3011static int __init ib_mad_init_module(void)
3012{
3013 int ret;
3014
Hal Rosenstockb76aabc2009-09-07 08:28:48 -07003015 mad_recvq_size = min(mad_recvq_size, IB_MAD_QP_MAX_SIZE);
3016 mad_recvq_size = max(mad_recvq_size, IB_MAD_QP_MIN_SIZE);
3017
3018 mad_sendq_size = min(mad_sendq_size, IB_MAD_QP_MAX_SIZE);
3019 mad_sendq_size = max(mad_sendq_size, IB_MAD_QP_MIN_SIZE);
3020
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 ib_mad_cache = kmem_cache_create("ib_mad",
3022 sizeof(struct ib_mad_private),
3023 0,
3024 SLAB_HWCACHE_ALIGN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 NULL);
3026 if (!ib_mad_cache) {
3027 printk(KERN_ERR PFX "Couldn't create ib_mad cache\n");
3028 ret = -ENOMEM;
3029 goto error1;
3030 }
3031
3032 INIT_LIST_HEAD(&ib_mad_port_list);
3033
3034 if (ib_register_client(&mad_client)) {
3035 printk(KERN_ERR PFX "Couldn't register ib_mad client\n");
3036 ret = -EINVAL;
3037 goto error2;
3038 }
3039
3040 return 0;
3041
3042error2:
3043 kmem_cache_destroy(ib_mad_cache);
3044error1:
3045 return ret;
3046}
3047
3048static void __exit ib_mad_cleanup_module(void)
3049{
3050 ib_unregister_client(&mad_client);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07003051 kmem_cache_destroy(ib_mad_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052}
3053
3054module_init(ib_mad_init_module);
3055module_exit(ib_mad_cleanup_module);