blob: ef1304f151dc6fbaf9cf847a79557b1953f3e448 [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
279 /* Allocate structures */
Roland Dreierde6eb662005-11-02 07:23:14 -0800280 mad_agent_priv = kzalloc(sizeof *mad_agent_priv, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (!mad_agent_priv) {
282 ret = ERR_PTR(-ENOMEM);
283 goto error1;
284 }
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700285
286 mad_agent_priv->agent.mr = ib_get_dma_mr(port_priv->qp_info[qpn].qp->pd,
287 IB_ACCESS_LOCAL_WRITE);
288 if (IS_ERR(mad_agent_priv->agent.mr)) {
289 ret = ERR_PTR(-ENOMEM);
290 goto error2;
291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 if (mad_reg_req) {
Julia Lawall9893e742010-05-15 23:22:38 +0200294 reg_req = kmemdup(mad_reg_req, sizeof *reg_req, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (!reg_req) {
296 ret = ERR_PTR(-ENOMEM);
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700297 goto error3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300
301 /* Now, fill in the various structures */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 mad_agent_priv->qp_info = &port_priv->qp_info[qpn];
303 mad_agent_priv->reg_req = reg_req;
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700304 mad_agent_priv->agent.rmpp_version = rmpp_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 mad_agent_priv->agent.device = device;
306 mad_agent_priv->agent.recv_handler = recv_handler;
307 mad_agent_priv->agent.send_handler = send_handler;
308 mad_agent_priv->agent.context = context;
309 mad_agent_priv->agent.qp = port_priv->qp_info[qpn].qp;
310 mad_agent_priv->agent.port_num = port_num;
Ralph Campbelld9620a42009-02-27 14:44:32 -0800311 spin_lock_init(&mad_agent_priv->lock);
312 INIT_LIST_HEAD(&mad_agent_priv->send_list);
313 INIT_LIST_HEAD(&mad_agent_priv->wait_list);
314 INIT_LIST_HEAD(&mad_agent_priv->done_list);
315 INIT_LIST_HEAD(&mad_agent_priv->rmpp_list);
316 INIT_DELAYED_WORK(&mad_agent_priv->timed_work, timeout_sends);
317 INIT_LIST_HEAD(&mad_agent_priv->local_list);
318 INIT_WORK(&mad_agent_priv->local_work, local_completions);
319 atomic_set(&mad_agent_priv->refcount, 1);
320 init_completion(&mad_agent_priv->comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 spin_lock_irqsave(&port_priv->reg_lock, flags);
323 mad_agent_priv->agent.hi_tid = ++ib_mad_client_id;
324
325 /*
326 * Make sure MAD registration (if supplied)
327 * is non overlapping with any existing ones
328 */
329 if (mad_reg_req) {
330 mgmt_class = convert_mgmt_class(mad_reg_req->mgmt_class);
331 if (!is_vendor_class(mgmt_class)) {
332 class = port_priv->version[mad_reg_req->
333 mgmt_class_version].class;
334 if (class) {
335 method = class->method_table[mgmt_class];
336 if (method) {
337 if (method_in_use(&method,
338 mad_reg_req))
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700339 goto error4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341 }
342 ret2 = add_nonoui_reg_req(mad_reg_req, mad_agent_priv,
343 mgmt_class);
344 } else {
345 /* "New" vendor class range */
346 vendor = port_priv->version[mad_reg_req->
347 mgmt_class_version].vendor;
348 if (vendor) {
349 vclass = vendor_class_index(mgmt_class);
350 vendor_class = vendor->vendor_class[vclass];
351 if (vendor_class) {
352 if (is_vendor_method_in_use(
353 vendor_class,
354 mad_reg_req))
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700355 goto error4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
357 }
358 ret2 = add_oui_reg_req(mad_reg_req, mad_agent_priv);
359 }
360 if (ret2) {
361 ret = ERR_PTR(ret2);
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700362 goto error4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
364 }
365
366 /* Add mad agent into port's agent list */
367 list_add_tail(&mad_agent_priv->agent_list, &port_priv->agent_list);
368 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return &mad_agent_priv->agent;
371
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700372error4:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
374 kfree(reg_req);
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700375error3:
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700376 ib_dereg_mr(mad_agent_priv->agent.mr);
Adrian Bunk2012a112005-11-27 00:37:36 +0100377error2:
378 kfree(mad_agent_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379error1:
380 return ret;
381}
382EXPORT_SYMBOL(ib_register_mad_agent);
383
384static inline int is_snooping_sends(int mad_snoop_flags)
385{
386 return (mad_snoop_flags &
387 (/*IB_MAD_SNOOP_POSTED_SENDS |
388 IB_MAD_SNOOP_RMPP_SENDS |*/
389 IB_MAD_SNOOP_SEND_COMPLETIONS /*|
390 IB_MAD_SNOOP_RMPP_SEND_COMPLETIONS*/));
391}
392
393static inline int is_snooping_recvs(int mad_snoop_flags)
394{
395 return (mad_snoop_flags &
396 (IB_MAD_SNOOP_RECVS /*|
397 IB_MAD_SNOOP_RMPP_RECVS*/));
398}
399
400static int register_snoop_agent(struct ib_mad_qp_info *qp_info,
401 struct ib_mad_snoop_private *mad_snoop_priv)
402{
403 struct ib_mad_snoop_private **new_snoop_table;
404 unsigned long flags;
405 int i;
406
407 spin_lock_irqsave(&qp_info->snoop_lock, flags);
408 /* Check for empty slot in array. */
409 for (i = 0; i < qp_info->snoop_table_size; i++)
410 if (!qp_info->snoop_table[i])
411 break;
412
413 if (i == qp_info->snoop_table_size) {
414 /* Grow table. */
Roland Dreier528051742008-10-14 14:05:36 -0700415 new_snoop_table = krealloc(qp_info->snoop_table,
416 sizeof mad_snoop_priv *
417 (qp_info->snoop_table_size + 1),
418 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (!new_snoop_table) {
420 i = -ENOMEM;
421 goto out;
422 }
Roland Dreier528051742008-10-14 14:05:36 -0700423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 qp_info->snoop_table = new_snoop_table;
425 qp_info->snoop_table_size++;
426 }
427 qp_info->snoop_table[i] = mad_snoop_priv;
428 atomic_inc(&qp_info->snoop_count);
429out:
430 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
431 return i;
432}
433
434struct ib_mad_agent *ib_register_mad_snoop(struct ib_device *device,
435 u8 port_num,
436 enum ib_qp_type qp_type,
437 int mad_snoop_flags,
438 ib_mad_snoop_handler snoop_handler,
439 ib_mad_recv_handler recv_handler,
440 void *context)
441{
442 struct ib_mad_port_private *port_priv;
443 struct ib_mad_agent *ret;
444 struct ib_mad_snoop_private *mad_snoop_priv;
445 int qpn;
446
447 /* Validate parameters */
448 if ((is_snooping_sends(mad_snoop_flags) && !snoop_handler) ||
449 (is_snooping_recvs(mad_snoop_flags) && !recv_handler)) {
450 ret = ERR_PTR(-EINVAL);
451 goto error1;
452 }
453 qpn = get_spl_qp_index(qp_type);
454 if (qpn == -1) {
455 ret = ERR_PTR(-EINVAL);
456 goto error1;
457 }
458 port_priv = ib_get_mad_port(device, port_num);
459 if (!port_priv) {
460 ret = ERR_PTR(-ENODEV);
461 goto error1;
462 }
463 /* Allocate structures */
Roland Dreierde6eb662005-11-02 07:23:14 -0800464 mad_snoop_priv = kzalloc(sizeof *mad_snoop_priv, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (!mad_snoop_priv) {
466 ret = ERR_PTR(-ENOMEM);
467 goto error1;
468 }
469
470 /* Now, fill in the various structures */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 mad_snoop_priv->qp_info = &port_priv->qp_info[qpn];
472 mad_snoop_priv->agent.device = device;
473 mad_snoop_priv->agent.recv_handler = recv_handler;
474 mad_snoop_priv->agent.snoop_handler = snoop_handler;
475 mad_snoop_priv->agent.context = context;
476 mad_snoop_priv->agent.qp = port_priv->qp_info[qpn].qp;
477 mad_snoop_priv->agent.port_num = port_num;
478 mad_snoop_priv->mad_snoop_flags = mad_snoop_flags;
Sean Hefty1b52fa982006-05-12 14:57:52 -0700479 init_completion(&mad_snoop_priv->comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 mad_snoop_priv->snoop_index = register_snoop_agent(
481 &port_priv->qp_info[qpn],
482 mad_snoop_priv);
483 if (mad_snoop_priv->snoop_index < 0) {
484 ret = ERR_PTR(mad_snoop_priv->snoop_index);
485 goto error2;
486 }
487
488 atomic_set(&mad_snoop_priv->refcount, 1);
489 return &mad_snoop_priv->agent;
490
491error2:
492 kfree(mad_snoop_priv);
493error1:
494 return ret;
495}
496EXPORT_SYMBOL(ib_register_mad_snoop);
497
Sean Hefty1b52fa982006-05-12 14:57:52 -0700498static inline void deref_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
499{
500 if (atomic_dec_and_test(&mad_agent_priv->refcount))
501 complete(&mad_agent_priv->comp);
502}
503
504static inline void deref_snoop_agent(struct ib_mad_snoop_private *mad_snoop_priv)
505{
506 if (atomic_dec_and_test(&mad_snoop_priv->refcount))
507 complete(&mad_snoop_priv->comp);
508}
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510static void unregister_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
511{
512 struct ib_mad_port_private *port_priv;
513 unsigned long flags;
514
515 /* Note that we could still be handling received MADs */
516
517 /*
518 * Canceling all sends results in dropping received response
519 * MADs, preventing us from queuing additional work
520 */
521 cancel_mads(mad_agent_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 port_priv = mad_agent_priv->qp_info->port_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 cancel_delayed_work(&mad_agent_priv->timed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 spin_lock_irqsave(&port_priv->reg_lock, flags);
526 remove_mad_reg_req(mad_agent_priv);
527 list_del(&mad_agent_priv->agent_list);
528 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
529
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700530 flush_workqueue(port_priv->wq);
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700531 ib_cancel_rmpp_recvs(mad_agent_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Sean Hefty1b52fa982006-05-12 14:57:52 -0700533 deref_mad_agent(mad_agent_priv);
534 wait_for_completion(&mad_agent_priv->comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Jesper Juhl6044ec82005-11-07 01:01:32 -0800536 kfree(mad_agent_priv->reg_req);
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700537 ib_dereg_mr(mad_agent_priv->agent.mr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 kfree(mad_agent_priv);
539}
540
541static void unregister_mad_snoop(struct ib_mad_snoop_private *mad_snoop_priv)
542{
543 struct ib_mad_qp_info *qp_info;
544 unsigned long flags;
545
546 qp_info = mad_snoop_priv->qp_info;
547 spin_lock_irqsave(&qp_info->snoop_lock, flags);
548 qp_info->snoop_table[mad_snoop_priv->snoop_index] = NULL;
549 atomic_dec(&qp_info->snoop_count);
550 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
551
Sean Hefty1b52fa982006-05-12 14:57:52 -0700552 deref_snoop_agent(mad_snoop_priv);
553 wait_for_completion(&mad_snoop_priv->comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 kfree(mad_snoop_priv);
556}
557
558/*
559 * ib_unregister_mad_agent - Unregisters a client from using MAD services
560 */
561int ib_unregister_mad_agent(struct ib_mad_agent *mad_agent)
562{
563 struct ib_mad_agent_private *mad_agent_priv;
564 struct ib_mad_snoop_private *mad_snoop_priv;
565
566 /* If the TID is zero, the agent can only snoop. */
567 if (mad_agent->hi_tid) {
568 mad_agent_priv = container_of(mad_agent,
569 struct ib_mad_agent_private,
570 agent);
571 unregister_mad_agent(mad_agent_priv);
572 } else {
573 mad_snoop_priv = container_of(mad_agent,
574 struct ib_mad_snoop_private,
575 agent);
576 unregister_mad_snoop(mad_snoop_priv);
577 }
578 return 0;
579}
580EXPORT_SYMBOL(ib_unregister_mad_agent);
581
582static void dequeue_mad(struct ib_mad_list_head *mad_list)
583{
584 struct ib_mad_queue *mad_queue;
585 unsigned long flags;
586
587 BUG_ON(!mad_list->mad_queue);
588 mad_queue = mad_list->mad_queue;
589 spin_lock_irqsave(&mad_queue->lock, flags);
590 list_del(&mad_list->list);
591 mad_queue->count--;
592 spin_unlock_irqrestore(&mad_queue->lock, flags);
593}
594
595static void snoop_send(struct ib_mad_qp_info *qp_info,
Sean Hefty34816ad2005-10-25 10:51:39 -0700596 struct ib_mad_send_buf *send_buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 struct ib_mad_send_wc *mad_send_wc,
598 int mad_snoop_flags)
599{
600 struct ib_mad_snoop_private *mad_snoop_priv;
601 unsigned long flags;
602 int i;
603
604 spin_lock_irqsave(&qp_info->snoop_lock, flags);
605 for (i = 0; i < qp_info->snoop_table_size; i++) {
606 mad_snoop_priv = qp_info->snoop_table[i];
607 if (!mad_snoop_priv ||
608 !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
609 continue;
610
611 atomic_inc(&mad_snoop_priv->refcount);
612 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
613 mad_snoop_priv->agent.snoop_handler(&mad_snoop_priv->agent,
Sean Hefty34816ad2005-10-25 10:51:39 -0700614 send_buf, mad_send_wc);
Sean Hefty1b52fa982006-05-12 14:57:52 -0700615 deref_snoop_agent(mad_snoop_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 spin_lock_irqsave(&qp_info->snoop_lock, flags);
617 }
618 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
619}
620
621static void snoop_recv(struct ib_mad_qp_info *qp_info,
622 struct ib_mad_recv_wc *mad_recv_wc,
623 int mad_snoop_flags)
624{
625 struct ib_mad_snoop_private *mad_snoop_priv;
626 unsigned long flags;
627 int i;
628
629 spin_lock_irqsave(&qp_info->snoop_lock, flags);
630 for (i = 0; i < qp_info->snoop_table_size; i++) {
631 mad_snoop_priv = qp_info->snoop_table[i];
632 if (!mad_snoop_priv ||
633 !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
634 continue;
635
636 atomic_inc(&mad_snoop_priv->refcount);
637 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
638 mad_snoop_priv->agent.recv_handler(&mad_snoop_priv->agent,
639 mad_recv_wc);
Sean Hefty1b52fa982006-05-12 14:57:52 -0700640 deref_snoop_agent(mad_snoop_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 spin_lock_irqsave(&qp_info->snoop_lock, flags);
642 }
643 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
644}
645
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +0200646static void build_smp_wc(struct ib_qp *qp,
647 u64 wr_id, u16 slid, u16 pkey_index, u8 port_num,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 struct ib_wc *wc)
649{
650 memset(wc, 0, sizeof *wc);
651 wc->wr_id = wr_id;
652 wc->status = IB_WC_SUCCESS;
653 wc->opcode = IB_WC_RECV;
654 wc->pkey_index = pkey_index;
655 wc->byte_len = sizeof(struct ib_mad) + sizeof(struct ib_grh);
656 wc->src_qp = IB_QP0;
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +0200657 wc->qp = qp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 wc->slid = slid;
659 wc->sl = 0;
660 wc->dlid_path_bits = 0;
661 wc->port_num = port_num;
662}
663
664/*
665 * Return 0 if SMP is to be sent
666 * Return 1 if SMP was consumed locally (whether or not solicited)
667 * Return < 0 if error
668 */
669static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
Sean Hefty34816ad2005-10-25 10:51:39 -0700670 struct ib_mad_send_wr_private *mad_send_wr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Hal Rosenstockde493d42007-04-02 11:24:07 -0400672 int ret = 0;
Sean Hefty34816ad2005-10-25 10:51:39 -0700673 struct ib_smp *smp = mad_send_wr->send_buf.mad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 unsigned long flags;
675 struct ib_mad_local_private *local;
676 struct ib_mad_private *mad_priv;
677 struct ib_mad_port_private *port_priv;
678 struct ib_mad_agent_private *recv_mad_agent = NULL;
679 struct ib_device *device = mad_agent_priv->agent.device;
Hal Rosenstock1bae4db2007-05-14 17:21:52 -0400680 u8 port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 struct ib_wc mad_wc;
Sean Hefty34816ad2005-10-25 10:51:39 -0700682 struct ib_send_wr *send_wr = &mad_send_wr->send_wr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Hal Rosenstock1bae4db2007-05-14 17:21:52 -0400684 if (device->node_type == RDMA_NODE_IB_SWITCH &&
685 smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
686 port_num = send_wr->wr.ud.port_num;
687 else
688 port_num = mad_agent_priv->agent.port_num;
689
Ralph Campbell8cf3f042006-02-03 14:28:48 -0800690 /*
691 * Directed route handling starts if the initial LID routed part of
692 * a request or the ending LID routed part of a response is empty.
693 * If we are at the start of the LID routed part, don't update the
694 * hop_ptr or hop_cnt. See section 14.2.2, Vol 1 IB spec.
695 */
696 if ((ib_get_smp_direction(smp) ? smp->dr_dlid : smp->dr_slid) ==
697 IB_LID_PERMISSIVE &&
Hal Rosenstockde493d42007-04-02 11:24:07 -0400698 smi_handle_dr_smp_send(smp, device->node_type, port_num) ==
699 IB_SMI_DISCARD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 ret = -EINVAL;
701 printk(KERN_ERR PFX "Invalid directed route\n");
702 goto out;
703 }
Hal Rosenstockde493d42007-04-02 11:24:07 -0400704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 /* Check to post send on QP or process locally */
Steve Welch727792d2007-10-23 15:06:10 -0700706 if (smi_check_local_smp(smp, device) == IB_SMI_DISCARD &&
707 smi_check_local_returning_smp(smp, device) == IB_SMI_DISCARD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 goto out;
709
710 local = kmalloc(sizeof *local, GFP_ATOMIC);
711 if (!local) {
712 ret = -ENOMEM;
713 printk(KERN_ERR PFX "No memory for ib_mad_local_private\n");
714 goto out;
715 }
716 local->mad_priv = NULL;
717 local->recv_mad_agent = NULL;
718 mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_ATOMIC);
719 if (!mad_priv) {
720 ret = -ENOMEM;
721 printk(KERN_ERR PFX "No memory for local response MAD\n");
722 kfree(local);
723 goto out;
724 }
725
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +0200726 build_smp_wc(mad_agent_priv->agent.qp,
727 send_wr->wr_id, be16_to_cpu(smp->dr_slid),
Sean Hefty97f52eb2005-08-13 21:05:57 -0700728 send_wr->wr.ud.pkey_index,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 send_wr->wr.ud.port_num, &mad_wc);
730
731 /* No GRH for DR SMP */
732 ret = device->process_mad(device, 0, port_num, &mad_wc, NULL,
733 (struct ib_mad *)smp,
734 (struct ib_mad *)&mad_priv->mad);
735 switch (ret)
736 {
737 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY:
Sean Hefty2527e682006-07-20 11:25:50 +0300738 if (ib_response_mad(&mad_priv->mad.mad) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 mad_agent_priv->agent.recv_handler) {
740 local->mad_priv = mad_priv;
741 local->recv_mad_agent = mad_agent_priv;
742 /*
743 * Reference MAD agent until receive
744 * side of local completion handled
745 */
746 atomic_inc(&mad_agent_priv->refcount);
747 } else
748 kmem_cache_free(ib_mad_cache, mad_priv);
749 break;
750 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED:
751 kmem_cache_free(ib_mad_cache, mad_priv);
Ralph Campbell4780c192009-03-03 14:22:17 -0800752 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 case IB_MAD_RESULT_SUCCESS:
754 /* Treat like an incoming receive MAD */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 port_priv = ib_get_mad_port(mad_agent_priv->agent.device,
756 mad_agent_priv->agent.port_num);
757 if (port_priv) {
Steve Welch727792d2007-10-23 15:06:10 -0700758 memcpy(&mad_priv->mad.mad, smp, sizeof(struct ib_mad));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 recv_mad_agent = find_mad_agent(port_priv,
Hal Rosenstock4a0754f2005-07-27 11:45:24 -0700760 &mad_priv->mad.mad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
762 if (!port_priv || !recv_mad_agent) {
Ralph Campbell4780c192009-03-03 14:22:17 -0800763 /*
764 * No receiving agent so drop packet and
765 * generate send completion.
766 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 kmem_cache_free(ib_mad_cache, mad_priv);
Ralph Campbell4780c192009-03-03 14:22:17 -0800768 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
770 local->mad_priv = mad_priv;
771 local->recv_mad_agent = recv_mad_agent;
772 break;
773 default:
774 kmem_cache_free(ib_mad_cache, mad_priv);
775 kfree(local);
776 ret = -EINVAL;
777 goto out;
778 }
779
Sean Hefty34816ad2005-10-25 10:51:39 -0700780 local->mad_send_wr = mad_send_wr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 /* Reference MAD agent until send side of local completion handled */
782 atomic_inc(&mad_agent_priv->refcount);
783 /* Queue local completion to local list */
784 spin_lock_irqsave(&mad_agent_priv->lock, flags);
785 list_add_tail(&local->completion_list, &mad_agent_priv->local_list);
786 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
787 queue_work(mad_agent_priv->qp_info->port_priv->wq,
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700788 &mad_agent_priv->local_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 ret = 1;
790out:
791 return ret;
792}
793
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800794static int get_pad_size(int hdr_len, int data_len)
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700795{
796 int seg_size, pad;
797
798 seg_size = sizeof(struct ib_mad) - hdr_len;
799 if (data_len && seg_size) {
800 pad = seg_size - data_len % seg_size;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800801 return pad == seg_size ? 0 : pad;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700802 } else
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800803 return seg_size;
804}
805
806static void free_send_rmpp_list(struct ib_mad_send_wr_private *mad_send_wr)
807{
808 struct ib_rmpp_segment *s, *t;
809
810 list_for_each_entry_safe(s, t, &mad_send_wr->rmpp_list, list) {
811 list_del(&s->list);
812 kfree(s);
813 }
814}
815
816static int alloc_send_rmpp_list(struct ib_mad_send_wr_private *send_wr,
817 gfp_t gfp_mask)
818{
819 struct ib_mad_send_buf *send_buf = &send_wr->send_buf;
820 struct ib_rmpp_mad *rmpp_mad = send_buf->mad;
821 struct ib_rmpp_segment *seg = NULL;
822 int left, seg_size, pad;
823
824 send_buf->seg_size = sizeof (struct ib_mad) - send_buf->hdr_len;
825 seg_size = send_buf->seg_size;
826 pad = send_wr->pad;
827
828 /* Allocate data segments. */
829 for (left = send_buf->data_len + pad; left > 0; left -= seg_size) {
830 seg = kmalloc(sizeof (*seg) + seg_size, gfp_mask);
831 if (!seg) {
832 printk(KERN_ERR "alloc_send_rmpp_segs: RMPP mem "
833 "alloc failed for len %zd, gfp %#x\n",
834 sizeof (*seg) + seg_size, gfp_mask);
835 free_send_rmpp_list(send_wr);
836 return -ENOMEM;
837 }
838 seg->num = ++send_buf->seg_count;
839 list_add_tail(&seg->list, &send_wr->rmpp_list);
840 }
841
842 /* Zero any padding */
843 if (pad)
844 memset(seg->data + seg_size - pad, 0, pad);
845
846 rmpp_mad->rmpp_hdr.rmpp_version = send_wr->mad_agent_priv->
847 agent.rmpp_version;
848 rmpp_mad->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_DATA;
849 ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
850
851 send_wr->cur_seg = container_of(send_wr->rmpp_list.next,
852 struct ib_rmpp_segment, list);
853 send_wr->last_ack_seg = send_wr->cur_seg;
854 return 0;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700855}
856
857struct ib_mad_send_buf * ib_create_send_mad(struct ib_mad_agent *mad_agent,
858 u32 remote_qpn, u16 pkey_index,
Sean Hefty34816ad2005-10-25 10:51:39 -0700859 int rmpp_active,
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700860 int hdr_len, int data_len,
Al Virodd0fc662005-10-07 07:46:04 +0100861 gfp_t gfp_mask)
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700862{
863 struct ib_mad_agent_private *mad_agent_priv;
Sean Hefty34816ad2005-10-25 10:51:39 -0700864 struct ib_mad_send_wr_private *mad_send_wr;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800865 int pad, message_size, ret, size;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700866 void *buf;
867
Sean Hefty34816ad2005-10-25 10:51:39 -0700868 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
869 agent);
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800870 pad = get_pad_size(hdr_len, data_len);
871 message_size = hdr_len + data_len + pad;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700872
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700873 if ((!mad_agent->rmpp_version &&
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800874 (rmpp_active || message_size > sizeof(struct ib_mad))) ||
875 (!rmpp_active && message_size > sizeof(struct ib_mad)))
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700876 return ERR_PTR(-EINVAL);
877
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800878 size = rmpp_active ? hdr_len : sizeof(struct ib_mad);
879 buf = kzalloc(sizeof *mad_send_wr + size, gfp_mask);
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700880 if (!buf)
881 return ERR_PTR(-ENOMEM);
882
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800883 mad_send_wr = buf + size;
884 INIT_LIST_HEAD(&mad_send_wr->rmpp_list);
Sean Hefty34816ad2005-10-25 10:51:39 -0700885 mad_send_wr->send_buf.mad = buf;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800886 mad_send_wr->send_buf.hdr_len = hdr_len;
887 mad_send_wr->send_buf.data_len = data_len;
888 mad_send_wr->pad = pad;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700889
Sean Hefty34816ad2005-10-25 10:51:39 -0700890 mad_send_wr->mad_agent_priv = mad_agent_priv;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800891 mad_send_wr->sg_list[0].length = hdr_len;
Sean Hefty34816ad2005-10-25 10:51:39 -0700892 mad_send_wr->sg_list[0].lkey = mad_agent->mr->lkey;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800893 mad_send_wr->sg_list[1].length = sizeof(struct ib_mad) - hdr_len;
894 mad_send_wr->sg_list[1].lkey = mad_agent->mr->lkey;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700895
Sean Hefty34816ad2005-10-25 10:51:39 -0700896 mad_send_wr->send_wr.wr_id = (unsigned long) mad_send_wr;
897 mad_send_wr->send_wr.sg_list = mad_send_wr->sg_list;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800898 mad_send_wr->send_wr.num_sge = 2;
Sean Hefty34816ad2005-10-25 10:51:39 -0700899 mad_send_wr->send_wr.opcode = IB_WR_SEND;
900 mad_send_wr->send_wr.send_flags = IB_SEND_SIGNALED;
901 mad_send_wr->send_wr.wr.ud.remote_qpn = remote_qpn;
902 mad_send_wr->send_wr.wr.ud.remote_qkey = IB_QP_SET_QKEY;
903 mad_send_wr->send_wr.wr.ud.pkey_index = pkey_index;
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700904
905 if (rmpp_active) {
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800906 ret = alloc_send_rmpp_list(mad_send_wr, gfp_mask);
907 if (ret) {
908 kfree(buf);
909 return ERR_PTR(ret);
910 }
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700911 }
912
Sean Hefty34816ad2005-10-25 10:51:39 -0700913 mad_send_wr->send_buf.mad_agent = mad_agent;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700914 atomic_inc(&mad_agent_priv->refcount);
Sean Hefty34816ad2005-10-25 10:51:39 -0700915 return &mad_send_wr->send_buf;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700916}
917EXPORT_SYMBOL(ib_create_send_mad);
918
Hal Rosenstock618a3c02006-03-28 16:40:04 -0800919int ib_get_mad_data_offset(u8 mgmt_class)
920{
921 if (mgmt_class == IB_MGMT_CLASS_SUBN_ADM)
922 return IB_MGMT_SA_HDR;
923 else if ((mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
924 (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
925 (mgmt_class == IB_MGMT_CLASS_BIS))
926 return IB_MGMT_DEVICE_HDR;
927 else if ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
928 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END))
929 return IB_MGMT_VENDOR_HDR;
930 else
931 return IB_MGMT_MAD_HDR;
932}
933EXPORT_SYMBOL(ib_get_mad_data_offset);
934
935int ib_is_mad_class_rmpp(u8 mgmt_class)
936{
937 if ((mgmt_class == IB_MGMT_CLASS_SUBN_ADM) ||
938 (mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
939 (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
940 (mgmt_class == IB_MGMT_CLASS_BIS) ||
941 ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
942 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END)))
943 return 1;
944 return 0;
945}
946EXPORT_SYMBOL(ib_is_mad_class_rmpp);
947
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800948void *ib_get_rmpp_segment(struct ib_mad_send_buf *send_buf, int seg_num)
949{
950 struct ib_mad_send_wr_private *mad_send_wr;
951 struct list_head *list;
952
953 mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
954 send_buf);
955 list = &mad_send_wr->cur_seg->list;
956
957 if (mad_send_wr->cur_seg->num < seg_num) {
958 list_for_each_entry(mad_send_wr->cur_seg, list, list)
959 if (mad_send_wr->cur_seg->num == seg_num)
960 break;
961 } else if (mad_send_wr->cur_seg->num > seg_num) {
962 list_for_each_entry_reverse(mad_send_wr->cur_seg, list, list)
963 if (mad_send_wr->cur_seg->num == seg_num)
964 break;
965 }
966 return mad_send_wr->cur_seg->data;
967}
968EXPORT_SYMBOL(ib_get_rmpp_segment);
969
970static inline void *ib_get_payload(struct ib_mad_send_wr_private *mad_send_wr)
971{
972 if (mad_send_wr->send_buf.seg_count)
973 return ib_get_rmpp_segment(&mad_send_wr->send_buf,
974 mad_send_wr->seg_num);
975 else
976 return mad_send_wr->send_buf.mad +
977 mad_send_wr->send_buf.hdr_len;
978}
979
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700980void ib_free_send_mad(struct ib_mad_send_buf *send_buf)
981{
982 struct ib_mad_agent_private *mad_agent_priv;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800983 struct ib_mad_send_wr_private *mad_send_wr;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700984
985 mad_agent_priv = container_of(send_buf->mad_agent,
986 struct ib_mad_agent_private, agent);
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800987 mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
988 send_buf);
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700989
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800990 free_send_rmpp_list(mad_send_wr);
991 kfree(send_buf->mad);
Sean Hefty1b52fa982006-05-12 14:57:52 -0700992 deref_mad_agent(mad_agent_priv);
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700993}
994EXPORT_SYMBOL(ib_free_send_mad);
995
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700996int ib_send_mad(struct ib_mad_send_wr_private *mad_send_wr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
998 struct ib_mad_qp_info *qp_info;
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -0700999 struct list_head *list;
Sean Hefty34816ad2005-10-25 10:51:39 -07001000 struct ib_send_wr *bad_send_wr;
1001 struct ib_mad_agent *mad_agent;
1002 struct ib_sge *sge;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 unsigned long flags;
1004 int ret;
1005
Hal Rosenstockf8197a42005-07-27 11:45:24 -07001006 /* Set WR ID to find mad_send_wr upon completion */
Hal Rosenstockd760ce82005-07-27 11:45:25 -07001007 qp_info = mad_send_wr->mad_agent_priv->qp_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 mad_send_wr->send_wr.wr_id = (unsigned long)&mad_send_wr->mad_list;
1009 mad_send_wr->mad_list.mad_queue = &qp_info->send_queue;
1010
Sean Hefty34816ad2005-10-25 10:51:39 -07001011 mad_agent = mad_send_wr->send_buf.mad_agent;
1012 sge = mad_send_wr->sg_list;
Ralph Campbell15271062006-12-12 14:28:30 -08001013 sge[0].addr = ib_dma_map_single(mad_agent->device,
1014 mad_send_wr->send_buf.mad,
1015 sge[0].length,
1016 DMA_TO_DEVICE);
1017 mad_send_wr->header_mapping = sge[0].addr;
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001018
Ralph Campbell15271062006-12-12 14:28:30 -08001019 sge[1].addr = ib_dma_map_single(mad_agent->device,
1020 ib_get_payload(mad_send_wr),
1021 sge[1].length,
1022 DMA_TO_DEVICE);
1023 mad_send_wr->payload_mapping = sge[1].addr;
Sean Hefty34816ad2005-10-25 10:51:39 -07001024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 spin_lock_irqsave(&qp_info->send_queue.lock, flags);
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07001026 if (qp_info->send_queue.count < qp_info->send_queue.max_active) {
Sean Hefty34816ad2005-10-25 10:51:39 -07001027 ret = ib_post_send(mad_agent->qp, &mad_send_wr->send_wr,
1028 &bad_send_wr);
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07001029 list = &qp_info->send_queue.list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 ret = 0;
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07001032 list = &qp_info->overflow_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07001034
1035 if (!ret) {
1036 qp_info->send_queue.count++;
1037 list_add_tail(&mad_send_wr->mad_list.list, list);
1038 }
1039 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001040 if (ret) {
Ralph Campbell15271062006-12-12 14:28:30 -08001041 ib_dma_unmap_single(mad_agent->device,
1042 mad_send_wr->header_mapping,
1043 sge[0].length, DMA_TO_DEVICE);
1044 ib_dma_unmap_single(mad_agent->device,
1045 mad_send_wr->payload_mapping,
1046 sge[1].length, DMA_TO_DEVICE);
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 return ret;
1049}
1050
1051/*
1052 * ib_post_send_mad - Posts MAD(s) to the send queue of the QP associated
1053 * with the registered client
1054 */
Sean Hefty34816ad2005-10-25 10:51:39 -07001055int ib_post_send_mad(struct ib_mad_send_buf *send_buf,
1056 struct ib_mad_send_buf **bad_send_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 struct ib_mad_agent_private *mad_agent_priv;
Sean Hefty34816ad2005-10-25 10:51:39 -07001059 struct ib_mad_send_buf *next_send_buf;
1060 struct ib_mad_send_wr_private *mad_send_wr;
1061 unsigned long flags;
1062 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 /* Walk list of send WRs and post each on send list */
Sean Hefty34816ad2005-10-25 10:51:39 -07001065 for (; send_buf; send_buf = next_send_buf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Sean Hefty34816ad2005-10-25 10:51:39 -07001067 mad_send_wr = container_of(send_buf,
1068 struct ib_mad_send_wr_private,
1069 send_buf);
1070 mad_agent_priv = mad_send_wr->mad_agent_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Sean Hefty34816ad2005-10-25 10:51:39 -07001072 if (!send_buf->mad_agent->send_handler ||
1073 (send_buf->timeout_ms &&
1074 !send_buf->mad_agent->recv_handler)) {
1075 ret = -EINVAL;
1076 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
1078
Hal Rosenstock618a3c02006-03-28 16:40:04 -08001079 if (!ib_is_mad_class_rmpp(((struct ib_mad_hdr *) send_buf->mad)->mgmt_class)) {
1080 if (mad_agent_priv->agent.rmpp_version) {
1081 ret = -EINVAL;
1082 goto error;
1083 }
1084 }
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 /*
1087 * Save pointer to next work request to post in case the
1088 * current one completes, and the user modifies the work
1089 * request associated with the completion
1090 */
Sean Hefty34816ad2005-10-25 10:51:39 -07001091 next_send_buf = send_buf->next;
1092 mad_send_wr->send_wr.wr.ud.ah = send_buf->ah;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Sean Hefty34816ad2005-10-25 10:51:39 -07001094 if (((struct ib_mad_hdr *) send_buf->mad)->mgmt_class ==
1095 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
1096 ret = handle_outgoing_dr_smp(mad_agent_priv,
1097 mad_send_wr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (ret < 0) /* error */
Sean Hefty34816ad2005-10-25 10:51:39 -07001099 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 else if (ret == 1) /* locally consumed */
Sean Hefty34816ad2005-10-25 10:51:39 -07001101 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 }
1103
Sean Hefty34816ad2005-10-25 10:51:39 -07001104 mad_send_wr->tid = ((struct ib_mad_hdr *) send_buf->mad)->tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 /* Timeout will be updated after send completes */
Sean Hefty34816ad2005-10-25 10:51:39 -07001106 mad_send_wr->timeout = msecs_to_jiffies(send_buf->timeout_ms);
Sean Hefty4fc8cd42007-11-27 00:11:04 -08001107 mad_send_wr->max_retries = send_buf->retries;
1108 mad_send_wr->retries_left = send_buf->retries;
1109 send_buf->retries = 0;
Sean Hefty34816ad2005-10-25 10:51:39 -07001110 /* Reference for work request to QP + response */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 mad_send_wr->refcount = 1 + (mad_send_wr->timeout > 0);
1112 mad_send_wr->status = IB_WC_SUCCESS;
1113
1114 /* Reference MAD agent until send completes */
1115 atomic_inc(&mad_agent_priv->refcount);
1116 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1117 list_add_tail(&mad_send_wr->agent_list,
1118 &mad_agent_priv->send_list);
1119 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1120
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001121 if (mad_agent_priv->agent.rmpp_version) {
1122 ret = ib_send_rmpp_mad(mad_send_wr);
1123 if (ret >= 0 && ret != IB_RMPP_RESULT_CONSUMED)
1124 ret = ib_send_mad(mad_send_wr);
1125 } else
1126 ret = ib_send_mad(mad_send_wr);
1127 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 /* Fail send request */
1129 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1130 list_del(&mad_send_wr->agent_list);
1131 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1132 atomic_dec(&mad_agent_priv->refcount);
Sean Hefty34816ad2005-10-25 10:51:39 -07001133 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
1136 return 0;
Sean Hefty34816ad2005-10-25 10:51:39 -07001137error:
1138 if (bad_send_buf)
1139 *bad_send_buf = send_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 return ret;
1141}
1142EXPORT_SYMBOL(ib_post_send_mad);
1143
1144/*
1145 * ib_free_recv_mad - Returns data buffers used to receive
1146 * a MAD to the access layer
1147 */
1148void ib_free_recv_mad(struct ib_mad_recv_wc *mad_recv_wc)
1149{
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001150 struct ib_mad_recv_buf *mad_recv_buf, *temp_recv_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 struct ib_mad_private_header *mad_priv_hdr;
1152 struct ib_mad_private *priv;
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001153 struct list_head free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001155 INIT_LIST_HEAD(&free_list);
1156 list_splice_init(&mad_recv_wc->rmpp_list, &free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001158 list_for_each_entry_safe(mad_recv_buf, temp_recv_buf,
1159 &free_list, list) {
1160 mad_recv_wc = container_of(mad_recv_buf, struct ib_mad_recv_wc,
1161 recv_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 mad_priv_hdr = container_of(mad_recv_wc,
1163 struct ib_mad_private_header,
1164 recv_wc);
1165 priv = container_of(mad_priv_hdr, struct ib_mad_private,
1166 header);
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001167 kmem_cache_free(ib_mad_cache, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
1170EXPORT_SYMBOL(ib_free_recv_mad);
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172struct ib_mad_agent *ib_redirect_mad_qp(struct ib_qp *qp,
1173 u8 rmpp_version,
1174 ib_mad_send_handler send_handler,
1175 ib_mad_recv_handler recv_handler,
1176 void *context)
1177{
1178 return ERR_PTR(-EINVAL); /* XXX: for now */
1179}
1180EXPORT_SYMBOL(ib_redirect_mad_qp);
1181
1182int ib_process_mad_wc(struct ib_mad_agent *mad_agent,
1183 struct ib_wc *wc)
1184{
1185 printk(KERN_ERR PFX "ib_process_mad_wc() not implemented yet\n");
1186 return 0;
1187}
1188EXPORT_SYMBOL(ib_process_mad_wc);
1189
1190static int method_in_use(struct ib_mad_mgmt_method_table **method,
1191 struct ib_mad_reg_req *mad_reg_req)
1192{
1193 int i;
1194
Akinobu Mita19b629f2010-03-05 13:41:38 -08001195 for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 if ((*method)->agent[i]) {
1197 printk(KERN_ERR PFX "Method %d already in use\n", i);
1198 return -EINVAL;
1199 }
1200 }
1201 return 0;
1202}
1203
1204static int allocate_method_table(struct ib_mad_mgmt_method_table **method)
1205{
1206 /* Allocate management method table */
Roland Dreierde6eb662005-11-02 07:23:14 -08001207 *method = kzalloc(sizeof **method, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if (!*method) {
1209 printk(KERN_ERR PFX "No memory for "
1210 "ib_mad_mgmt_method_table\n");
1211 return -ENOMEM;
1212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 return 0;
1215}
1216
1217/*
1218 * Check to see if there are any methods still in use
1219 */
1220static int check_method_table(struct ib_mad_mgmt_method_table *method)
1221{
1222 int i;
1223
1224 for (i = 0; i < IB_MGMT_MAX_METHODS; i++)
1225 if (method->agent[i])
1226 return 1;
1227 return 0;
1228}
1229
1230/*
1231 * Check to see if there are any method tables for this class still in use
1232 */
1233static int check_class_table(struct ib_mad_mgmt_class_table *class)
1234{
1235 int i;
1236
1237 for (i = 0; i < MAX_MGMT_CLASS; i++)
1238 if (class->method_table[i])
1239 return 1;
1240 return 0;
1241}
1242
1243static int check_vendor_class(struct ib_mad_mgmt_vendor_class *vendor_class)
1244{
1245 int i;
1246
1247 for (i = 0; i < MAX_MGMT_OUI; i++)
1248 if (vendor_class->method_table[i])
1249 return 1;
1250 return 0;
1251}
1252
1253static int find_vendor_oui(struct ib_mad_mgmt_vendor_class *vendor_class,
1254 char *oui)
1255{
1256 int i;
1257
1258 for (i = 0; i < MAX_MGMT_OUI; i++)
Roland Dreier3cd96562006-09-22 15:22:46 -07001259 /* Is there matching OUI for this vendor class ? */
1260 if (!memcmp(vendor_class->oui[i], oui, 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return i;
1262
1263 return -1;
1264}
1265
1266static int check_vendor_table(struct ib_mad_mgmt_vendor_class_table *vendor)
1267{
1268 int i;
1269
1270 for (i = 0; i < MAX_MGMT_VENDOR_RANGE2; i++)
1271 if (vendor->vendor_class[i])
1272 return 1;
1273
1274 return 0;
1275}
1276
1277static void remove_methods_mad_agent(struct ib_mad_mgmt_method_table *method,
1278 struct ib_mad_agent_private *agent)
1279{
1280 int i;
1281
1282 /* Remove any methods for this mad agent */
1283 for (i = 0; i < IB_MGMT_MAX_METHODS; i++) {
1284 if (method->agent[i] == agent) {
1285 method->agent[i] = NULL;
1286 }
1287 }
1288}
1289
1290static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1291 struct ib_mad_agent_private *agent_priv,
1292 u8 mgmt_class)
1293{
1294 struct ib_mad_port_private *port_priv;
1295 struct ib_mad_mgmt_class_table **class;
1296 struct ib_mad_mgmt_method_table **method;
1297 int i, ret;
1298
1299 port_priv = agent_priv->qp_info->port_priv;
1300 class = &port_priv->version[mad_reg_req->mgmt_class_version].class;
1301 if (!*class) {
1302 /* Allocate management class table for "new" class version */
Roland Dreierde6eb662005-11-02 07:23:14 -08001303 *class = kzalloc(sizeof **class, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 if (!*class) {
1305 printk(KERN_ERR PFX "No memory for "
1306 "ib_mad_mgmt_class_table\n");
1307 ret = -ENOMEM;
1308 goto error1;
1309 }
Roland Dreierde6eb662005-11-02 07:23:14 -08001310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 /* Allocate method table for this management class */
1312 method = &(*class)->method_table[mgmt_class];
1313 if ((ret = allocate_method_table(method)))
1314 goto error2;
1315 } else {
1316 method = &(*class)->method_table[mgmt_class];
1317 if (!*method) {
1318 /* Allocate method table for this management class */
1319 if ((ret = allocate_method_table(method)))
1320 goto error1;
1321 }
1322 }
1323
1324 /* Now, make sure methods are not already in use */
1325 if (method_in_use(method, mad_reg_req))
1326 goto error3;
1327
1328 /* Finally, add in methods being registered */
Akinobu Mita19b629f2010-03-05 13:41:38 -08001329 for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 (*method)->agent[i] = agent_priv;
Akinobu Mita19b629f2010-03-05 13:41:38 -08001331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 return 0;
1333
1334error3:
1335 /* Remove any methods for this mad agent */
1336 remove_methods_mad_agent(*method, agent_priv);
1337 /* Now, check to see if there are any methods in use */
1338 if (!check_method_table(*method)) {
1339 /* If not, release management method table */
1340 kfree(*method);
1341 *method = NULL;
1342 }
1343 ret = -EINVAL;
1344 goto error1;
1345error2:
1346 kfree(*class);
1347 *class = NULL;
1348error1:
1349 return ret;
1350}
1351
1352static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1353 struct ib_mad_agent_private *agent_priv)
1354{
1355 struct ib_mad_port_private *port_priv;
1356 struct ib_mad_mgmt_vendor_class_table **vendor_table;
1357 struct ib_mad_mgmt_vendor_class_table *vendor = NULL;
1358 struct ib_mad_mgmt_vendor_class *vendor_class = NULL;
1359 struct ib_mad_mgmt_method_table **method;
1360 int i, ret = -ENOMEM;
1361 u8 vclass;
1362
1363 /* "New" vendor (with OUI) class */
1364 vclass = vendor_class_index(mad_reg_req->mgmt_class);
1365 port_priv = agent_priv->qp_info->port_priv;
1366 vendor_table = &port_priv->version[
1367 mad_reg_req->mgmt_class_version].vendor;
1368 if (!*vendor_table) {
1369 /* Allocate mgmt vendor class table for "new" class version */
Roland Dreierde6eb662005-11-02 07:23:14 -08001370 vendor = kzalloc(sizeof *vendor, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 if (!vendor) {
1372 printk(KERN_ERR PFX "No memory for "
1373 "ib_mad_mgmt_vendor_class_table\n");
1374 goto error1;
1375 }
Roland Dreierde6eb662005-11-02 07:23:14 -08001376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 *vendor_table = vendor;
1378 }
1379 if (!(*vendor_table)->vendor_class[vclass]) {
1380 /* Allocate table for this management vendor class */
Roland Dreierde6eb662005-11-02 07:23:14 -08001381 vendor_class = kzalloc(sizeof *vendor_class, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 if (!vendor_class) {
1383 printk(KERN_ERR PFX "No memory for "
1384 "ib_mad_mgmt_vendor_class\n");
1385 goto error2;
1386 }
Roland Dreierde6eb662005-11-02 07:23:14 -08001387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 (*vendor_table)->vendor_class[vclass] = vendor_class;
1389 }
1390 for (i = 0; i < MAX_MGMT_OUI; i++) {
1391 /* Is there matching OUI for this vendor class ? */
1392 if (!memcmp((*vendor_table)->vendor_class[vclass]->oui[i],
1393 mad_reg_req->oui, 3)) {
1394 method = &(*vendor_table)->vendor_class[
1395 vclass]->method_table[i];
1396 BUG_ON(!*method);
1397 goto check_in_use;
1398 }
1399 }
1400 for (i = 0; i < MAX_MGMT_OUI; i++) {
1401 /* OUI slot available ? */
1402 if (!is_vendor_oui((*vendor_table)->vendor_class[
1403 vclass]->oui[i])) {
1404 method = &(*vendor_table)->vendor_class[
1405 vclass]->method_table[i];
1406 BUG_ON(*method);
1407 /* Allocate method table for this OUI */
1408 if ((ret = allocate_method_table(method)))
1409 goto error3;
1410 memcpy((*vendor_table)->vendor_class[vclass]->oui[i],
1411 mad_reg_req->oui, 3);
1412 goto check_in_use;
1413 }
1414 }
1415 printk(KERN_ERR PFX "All OUI slots in use\n");
1416 goto error3;
1417
1418check_in_use:
1419 /* Now, make sure methods are not already in use */
1420 if (method_in_use(method, mad_reg_req))
1421 goto error4;
1422
1423 /* Finally, add in methods being registered */
Akinobu Mita19b629f2010-03-05 13:41:38 -08001424 for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 (*method)->agent[i] = agent_priv;
Akinobu Mita19b629f2010-03-05 13:41:38 -08001426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 return 0;
1428
1429error4:
1430 /* Remove any methods for this mad agent */
1431 remove_methods_mad_agent(*method, agent_priv);
1432 /* Now, check to see if there are any methods in use */
1433 if (!check_method_table(*method)) {
1434 /* If not, release management method table */
1435 kfree(*method);
1436 *method = NULL;
1437 }
1438 ret = -EINVAL;
1439error3:
1440 if (vendor_class) {
1441 (*vendor_table)->vendor_class[vclass] = NULL;
1442 kfree(vendor_class);
1443 }
1444error2:
1445 if (vendor) {
1446 *vendor_table = NULL;
1447 kfree(vendor);
1448 }
1449error1:
1450 return ret;
1451}
1452
1453static void remove_mad_reg_req(struct ib_mad_agent_private *agent_priv)
1454{
1455 struct ib_mad_port_private *port_priv;
1456 struct ib_mad_mgmt_class_table *class;
1457 struct ib_mad_mgmt_method_table *method;
1458 struct ib_mad_mgmt_vendor_class_table *vendor;
1459 struct ib_mad_mgmt_vendor_class *vendor_class;
1460 int index;
1461 u8 mgmt_class;
1462
1463 /*
1464 * Was MAD registration request supplied
1465 * with original registration ?
1466 */
1467 if (!agent_priv->reg_req) {
1468 goto out;
1469 }
1470
1471 port_priv = agent_priv->qp_info->port_priv;
1472 mgmt_class = convert_mgmt_class(agent_priv->reg_req->mgmt_class);
1473 class = port_priv->version[
1474 agent_priv->reg_req->mgmt_class_version].class;
1475 if (!class)
1476 goto vendor_check;
1477
1478 method = class->method_table[mgmt_class];
1479 if (method) {
1480 /* Remove any methods for this mad agent */
1481 remove_methods_mad_agent(method, agent_priv);
1482 /* Now, check to see if there are any methods still in use */
1483 if (!check_method_table(method)) {
1484 /* If not, release management method table */
1485 kfree(method);
1486 class->method_table[mgmt_class] = NULL;
1487 /* Any management classes left ? */
1488 if (!check_class_table(class)) {
1489 /* If not, release management class table */
1490 kfree(class);
1491 port_priv->version[
1492 agent_priv->reg_req->
1493 mgmt_class_version].class = NULL;
1494 }
1495 }
1496 }
1497
1498vendor_check:
1499 if (!is_vendor_class(mgmt_class))
1500 goto out;
1501
1502 /* normalize mgmt_class to vendor range 2 */
1503 mgmt_class = vendor_class_index(agent_priv->reg_req->mgmt_class);
1504 vendor = port_priv->version[
1505 agent_priv->reg_req->mgmt_class_version].vendor;
1506
1507 if (!vendor)
1508 goto out;
1509
1510 vendor_class = vendor->vendor_class[mgmt_class];
1511 if (vendor_class) {
1512 index = find_vendor_oui(vendor_class, agent_priv->reg_req->oui);
1513 if (index < 0)
1514 goto out;
1515 method = vendor_class->method_table[index];
1516 if (method) {
1517 /* Remove any methods for this mad agent */
1518 remove_methods_mad_agent(method, agent_priv);
1519 /*
1520 * Now, check to see if there are
1521 * any methods still in use
1522 */
1523 if (!check_method_table(method)) {
1524 /* If not, release management method table */
1525 kfree(method);
1526 vendor_class->method_table[index] = NULL;
1527 memset(vendor_class->oui[index], 0, 3);
1528 /* Any OUIs left ? */
1529 if (!check_vendor_class(vendor_class)) {
1530 /* If not, release vendor class table */
1531 kfree(vendor_class);
1532 vendor->vendor_class[mgmt_class] = NULL;
1533 /* Any other vendor classes left ? */
1534 if (!check_vendor_table(vendor)) {
1535 kfree(vendor);
1536 port_priv->version[
1537 agent_priv->reg_req->
1538 mgmt_class_version].
1539 vendor = NULL;
1540 }
1541 }
1542 }
1543 }
1544 }
1545
1546out:
1547 return;
1548}
1549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550static struct ib_mad_agent_private *
1551find_mad_agent(struct ib_mad_port_private *port_priv,
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07001552 struct ib_mad *mad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
1554 struct ib_mad_agent_private *mad_agent = NULL;
1555 unsigned long flags;
1556
1557 spin_lock_irqsave(&port_priv->reg_lock, flags);
Sean Hefty2527e682006-07-20 11:25:50 +03001558 if (ib_response_mad(mad)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 u32 hi_tid;
1560 struct ib_mad_agent_private *entry;
1561
1562 /*
1563 * Routing is based on high 32 bits of transaction ID
1564 * of MAD.
1565 */
1566 hi_tid = be64_to_cpu(mad->mad_hdr.tid) >> 32;
Sean Hefty34816ad2005-10-25 10:51:39 -07001567 list_for_each_entry(entry, &port_priv->agent_list, agent_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if (entry->agent.hi_tid == hi_tid) {
1569 mad_agent = entry;
1570 break;
1571 }
1572 }
1573 } else {
1574 struct ib_mad_mgmt_class_table *class;
1575 struct ib_mad_mgmt_method_table *method;
1576 struct ib_mad_mgmt_vendor_class_table *vendor;
1577 struct ib_mad_mgmt_vendor_class *vendor_class;
1578 struct ib_vendor_mad *vendor_mad;
1579 int index;
1580
1581 /*
1582 * Routing is based on version, class, and method
1583 * For "newer" vendor MADs, also based on OUI
1584 */
1585 if (mad->mad_hdr.class_version >= MAX_MGMT_VERSION)
1586 goto out;
1587 if (!is_vendor_class(mad->mad_hdr.mgmt_class)) {
1588 class = port_priv->version[
1589 mad->mad_hdr.class_version].class;
1590 if (!class)
1591 goto out;
1592 method = class->method_table[convert_mgmt_class(
1593 mad->mad_hdr.mgmt_class)];
1594 if (method)
1595 mad_agent = method->agent[mad->mad_hdr.method &
1596 ~IB_MGMT_METHOD_RESP];
1597 } else {
1598 vendor = port_priv->version[
1599 mad->mad_hdr.class_version].vendor;
1600 if (!vendor)
1601 goto out;
1602 vendor_class = vendor->vendor_class[vendor_class_index(
1603 mad->mad_hdr.mgmt_class)];
1604 if (!vendor_class)
1605 goto out;
1606 /* Find matching OUI */
1607 vendor_mad = (struct ib_vendor_mad *)mad;
1608 index = find_vendor_oui(vendor_class, vendor_mad->oui);
1609 if (index == -1)
1610 goto out;
1611 method = vendor_class->method_table[index];
1612 if (method) {
1613 mad_agent = method->agent[mad->mad_hdr.method &
1614 ~IB_MGMT_METHOD_RESP];
1615 }
1616 }
1617 }
1618
1619 if (mad_agent) {
1620 if (mad_agent->agent.recv_handler)
1621 atomic_inc(&mad_agent->refcount);
1622 else {
1623 printk(KERN_NOTICE PFX "No receive handler for client "
1624 "%p on port %d\n",
1625 &mad_agent->agent, port_priv->port_num);
1626 mad_agent = NULL;
1627 }
1628 }
1629out:
1630 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
1631
1632 return mad_agent;
1633}
1634
1635static int validate_mad(struct ib_mad *mad, u32 qp_num)
1636{
1637 int valid = 0;
1638
1639 /* Make sure MAD base version is understood */
1640 if (mad->mad_hdr.base_version != IB_MGMT_BASE_VERSION) {
1641 printk(KERN_ERR PFX "MAD received with unsupported base "
1642 "version %d\n", mad->mad_hdr.base_version);
1643 goto out;
1644 }
1645
1646 /* Filter SMI packets sent to other than QP0 */
1647 if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
1648 (mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) {
1649 if (qp_num == 0)
1650 valid = 1;
1651 } else {
1652 /* Filter GSI packets sent to QP0 */
1653 if (qp_num != 0)
1654 valid = 1;
1655 }
1656
1657out:
1658 return valid;
1659}
1660
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001661static int is_data_mad(struct ib_mad_agent_private *mad_agent_priv,
1662 struct ib_mad_hdr *mad_hdr)
1663{
1664 struct ib_rmpp_mad *rmpp_mad;
1665
1666 rmpp_mad = (struct ib_rmpp_mad *)mad_hdr;
1667 return !mad_agent_priv->agent.rmpp_version ||
1668 !(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
1669 IB_MGMT_RMPP_FLAG_ACTIVE) ||
1670 (rmpp_mad->rmpp_hdr.rmpp_type == IB_MGMT_RMPP_TYPE_DATA);
1671}
1672
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001673static inline int rcv_has_same_class(struct ib_mad_send_wr_private *wr,
1674 struct ib_mad_recv_wc *rwc)
1675{
1676 return ((struct ib_mad *)(wr->send_buf.mad))->mad_hdr.mgmt_class ==
1677 rwc->recv_buf.mad->mad_hdr.mgmt_class;
1678}
1679
Jack Morgenstein9874e742006-06-17 20:37:34 -07001680static inline int rcv_has_same_gid(struct ib_mad_agent_private *mad_agent_priv,
1681 struct ib_mad_send_wr_private *wr,
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001682 struct ib_mad_recv_wc *rwc )
1683{
1684 struct ib_ah_attr attr;
1685 u8 send_resp, rcv_resp;
Jack Morgenstein9874e742006-06-17 20:37:34 -07001686 union ib_gid sgid;
1687 struct ib_device *device = mad_agent_priv->agent.device;
1688 u8 port_num = mad_agent_priv->agent.port_num;
1689 u8 lmc;
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001690
Michael Brooks70972282008-09-20 20:06:16 -07001691 send_resp = ib_response_mad((struct ib_mad *)wr->send_buf.mad);
1692 rcv_resp = ib_response_mad(rwc->recv_buf.mad);
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001693
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001694 if (send_resp == rcv_resp)
1695 /* both requests, or both responses. GIDs different */
1696 return 0;
1697
1698 if (ib_query_ah(wr->send_buf.ah, &attr))
1699 /* Assume not equal, to avoid false positives. */
1700 return 0;
1701
Jack Morgenstein9874e742006-06-17 20:37:34 -07001702 if (!!(attr.ah_flags & IB_AH_GRH) !=
1703 !!(rwc->wc->wc_flags & IB_WC_GRH))
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001704 /* one has GID, other does not. Assume different */
1705 return 0;
Jack Morgenstein9874e742006-06-17 20:37:34 -07001706
1707 if (!send_resp && rcv_resp) {
1708 /* is request/response. */
1709 if (!(attr.ah_flags & IB_AH_GRH)) {
1710 if (ib_get_cached_lmc(device, port_num, &lmc))
1711 return 0;
1712 return (!lmc || !((attr.src_path_bits ^
1713 rwc->wc->dlid_path_bits) &
1714 ((1 << lmc) - 1)));
1715 } else {
1716 if (ib_get_cached_gid(device, port_num,
1717 attr.grh.sgid_index, &sgid))
1718 return 0;
1719 return !memcmp(sgid.raw, rwc->recv_buf.grh->dgid.raw,
1720 16);
1721 }
1722 }
1723
1724 if (!(attr.ah_flags & IB_AH_GRH))
1725 return attr.dlid == rwc->wc->slid;
1726 else
1727 return !memcmp(attr.grh.dgid.raw, rwc->recv_buf.grh->sgid.raw,
1728 16);
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001729}
Jack Morgenstein9874e742006-06-17 20:37:34 -07001730
1731static inline int is_direct(u8 class)
1732{
1733 return (class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE);
1734}
1735
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001736struct ib_mad_send_wr_private*
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001737ib_find_send_mad(struct ib_mad_agent_private *mad_agent_priv,
Jack Morgenstein9874e742006-06-17 20:37:34 -07001738 struct ib_mad_recv_wc *wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
Jack Morgenstein9874e742006-06-17 20:37:34 -07001740 struct ib_mad_send_wr_private *wr;
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001741 struct ib_mad *mad;
1742
Jack Morgenstein9874e742006-06-17 20:37:34 -07001743 mad = (struct ib_mad *)wc->recv_buf.mad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Jack Morgenstein9874e742006-06-17 20:37:34 -07001745 list_for_each_entry(wr, &mad_agent_priv->wait_list, agent_list) {
1746 if ((wr->tid == mad->mad_hdr.tid) &&
1747 rcv_has_same_class(wr, wc) &&
1748 /*
1749 * Don't check GID for direct routed MADs.
1750 * These might have permissive LIDs.
1751 */
1752 (is_direct(wc->recv_buf.mad->mad_hdr.mgmt_class) ||
1753 rcv_has_same_gid(mad_agent_priv, wr, wc)))
Roland Dreier39798692006-11-13 09:38:07 -08001754 return (wr->status == IB_WC_SUCCESS) ? wr : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 }
1756
1757 /*
1758 * It's possible to receive the response before we've
1759 * been notified that the send has completed
1760 */
Jack Morgenstein9874e742006-06-17 20:37:34 -07001761 list_for_each_entry(wr, &mad_agent_priv->send_list, agent_list) {
1762 if (is_data_mad(mad_agent_priv, wr->send_buf.mad) &&
1763 wr->tid == mad->mad_hdr.tid &&
1764 wr->timeout &&
1765 rcv_has_same_class(wr, wc) &&
1766 /*
1767 * Don't check GID for direct routed MADs.
1768 * These might have permissive LIDs.
1769 */
1770 (is_direct(wc->recv_buf.mad->mad_hdr.mgmt_class) ||
1771 rcv_has_same_gid(mad_agent_priv, wr, wc)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 /* Verify request has not been canceled */
Jack Morgenstein9874e742006-06-17 20:37:34 -07001773 return (wr->status == IB_WC_SUCCESS) ? wr : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 }
1775 return NULL;
1776}
1777
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001778void ib_mark_mad_done(struct ib_mad_send_wr_private *mad_send_wr)
Hal Rosenstock6a0c4352005-07-27 11:45:26 -07001779{
1780 mad_send_wr->timeout = 0;
Akinobu Mita179e0912006-06-26 00:24:41 -07001781 if (mad_send_wr->refcount == 1)
1782 list_move_tail(&mad_send_wr->agent_list,
Hal Rosenstock6a0c4352005-07-27 11:45:26 -07001783 &mad_send_wr->mad_agent_priv->done_list);
Hal Rosenstock6a0c4352005-07-27 11:45:26 -07001784}
1785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786static void ib_mad_complete_recv(struct ib_mad_agent_private *mad_agent_priv,
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07001787 struct ib_mad_recv_wc *mad_recv_wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
1789 struct ib_mad_send_wr_private *mad_send_wr;
1790 struct ib_mad_send_wc mad_send_wc;
1791 unsigned long flags;
1792
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001793 INIT_LIST_HEAD(&mad_recv_wc->rmpp_list);
1794 list_add(&mad_recv_wc->recv_buf.list, &mad_recv_wc->rmpp_list);
1795 if (mad_agent_priv->agent.rmpp_version) {
1796 mad_recv_wc = ib_process_rmpp_recv_wc(mad_agent_priv,
1797 mad_recv_wc);
1798 if (!mad_recv_wc) {
Sean Hefty1b52fa982006-05-12 14:57:52 -07001799 deref_mad_agent(mad_agent_priv);
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001800 return;
1801 }
1802 }
1803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 /* Complete corresponding request */
Sean Hefty2527e682006-07-20 11:25:50 +03001805 if (ib_response_mad(mad_recv_wc->recv_buf.mad)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 spin_lock_irqsave(&mad_agent_priv->lock, flags);
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001807 mad_send_wr = ib_find_send_mad(mad_agent_priv, mad_recv_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 if (!mad_send_wr) {
1809 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07001810 ib_free_recv_mad(mad_recv_wc);
Sean Hefty1b52fa982006-05-12 14:57:52 -07001811 deref_mad_agent(mad_agent_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 return;
1813 }
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001814 ib_mark_mad_done(mad_send_wr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1816
1817 /* Defined behavior is to complete response before request */
Sean Hefty34816ad2005-10-25 10:51:39 -07001818 mad_recv_wc->wc->wr_id = (unsigned long) &mad_send_wr->send_buf;
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07001819 mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent,
1820 mad_recv_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 atomic_dec(&mad_agent_priv->refcount);
1822
1823 mad_send_wc.status = IB_WC_SUCCESS;
1824 mad_send_wc.vendor_err = 0;
Sean Hefty34816ad2005-10-25 10:51:39 -07001825 mad_send_wc.send_buf = &mad_send_wr->send_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
1827 } else {
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07001828 mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent,
1829 mad_recv_wc);
Sean Hefty1b52fa982006-05-12 14:57:52 -07001830 deref_mad_agent(mad_agent_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 }
1832}
1833
1834static void ib_mad_recv_done_handler(struct ib_mad_port_private *port_priv,
1835 struct ib_wc *wc)
1836{
1837 struct ib_mad_qp_info *qp_info;
1838 struct ib_mad_private_header *mad_priv_hdr;
Hal Rosenstock445d6802007-08-03 10:45:17 -07001839 struct ib_mad_private *recv, *response = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 struct ib_mad_list_head *mad_list;
1841 struct ib_mad_agent_private *mad_agent;
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04001842 int port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
1845 qp_info = mad_list->mad_queue->qp_info;
1846 dequeue_mad(mad_list);
1847
1848 mad_priv_hdr = container_of(mad_list, struct ib_mad_private_header,
1849 mad_list);
1850 recv = container_of(mad_priv_hdr, struct ib_mad_private, header);
Ralph Campbell15271062006-12-12 14:28:30 -08001851 ib_dma_unmap_single(port_priv->device,
1852 recv->header.mapping,
1853 sizeof(struct ib_mad_private) -
1854 sizeof(struct ib_mad_private_header),
1855 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 /* Setup MAD receive work completion from "normal" work completion */
Sean Hefty24239af2005-04-16 15:26:08 -07001858 recv->header.wc = *wc;
1859 recv->header.recv_wc.wc = &recv->header.wc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 recv->header.recv_wc.mad_len = sizeof(struct ib_mad);
1861 recv->header.recv_wc.recv_buf.mad = &recv->mad.mad;
1862 recv->header.recv_wc.recv_buf.grh = &recv->grh;
1863
1864 if (atomic_read(&qp_info->snoop_count))
1865 snoop_recv(qp_info, &recv->header.recv_wc, IB_MAD_SNOOP_RECVS);
1866
1867 /* Validate MAD */
1868 if (!validate_mad(&recv->mad.mad, qp_info->qp->qp_num))
1869 goto out;
1870
Hal Rosenstock445d6802007-08-03 10:45:17 -07001871 response = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL);
1872 if (!response) {
1873 printk(KERN_ERR PFX "ib_mad_recv_done_handler no memory "
1874 "for response buffer\n");
1875 goto out;
1876 }
1877
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04001878 if (port_priv->device->node_type == RDMA_NODE_IB_SWITCH)
1879 port_num = wc->port_num;
1880 else
1881 port_num = port_priv->port_num;
1882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 if (recv->mad.mad.mad_hdr.mgmt_class ==
1884 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04001885 enum smi_forward_action retsmi;
1886
Hal Rosenstockde493d42007-04-02 11:24:07 -04001887 if (smi_handle_dr_smp_recv(&recv->mad.smp,
1888 port_priv->device->node_type,
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04001889 port_num,
Hal Rosenstockde493d42007-04-02 11:24:07 -04001890 port_priv->device->phys_port_cnt) ==
1891 IB_SMI_DISCARD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 goto out;
Hal Rosenstockde493d42007-04-02 11:24:07 -04001893
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04001894 retsmi = smi_check_forward_dr_smp(&recv->mad.smp);
1895 if (retsmi == IB_SMI_LOCAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 goto local;
Hal Rosenstockde493d42007-04-02 11:24:07 -04001897
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04001898 if (retsmi == IB_SMI_SEND) { /* don't forward */
1899 if (smi_handle_dr_smp_send(&recv->mad.smp,
1900 port_priv->device->node_type,
1901 port_num) == IB_SMI_DISCARD)
1902 goto out;
Hal Rosenstockde493d42007-04-02 11:24:07 -04001903
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04001904 if (smi_check_local_smp(&recv->mad.smp, port_priv->device) == IB_SMI_DISCARD)
1905 goto out;
1906 } else if (port_priv->device->node_type == RDMA_NODE_IB_SWITCH) {
1907 /* forward case for switches */
1908 memcpy(response, recv, sizeof(*response));
1909 response->header.recv_wc.wc = &response->header.wc;
1910 response->header.recv_wc.recv_buf.mad = &response->mad.mad;
1911 response->header.recv_wc.recv_buf.grh = &response->grh;
1912
Hal Rosenstock86dfbec2007-08-03 10:45:17 -07001913 agent_send_response(&response->mad.mad,
1914 &response->grh, wc,
1915 port_priv->device,
1916 smi_get_fwd_port(&recv->mad.smp),
1917 qp_info->qp->qp_num);
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04001918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 goto out;
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04001920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 }
1922
1923local:
1924 /* Give driver "right of first refusal" on incoming MAD */
1925 if (port_priv->device->process_mad) {
1926 int ret;
1927
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 ret = port_priv->device->process_mad(port_priv->device, 0,
1929 port_priv->port_num,
1930 wc, &recv->grh,
1931 &recv->mad.mad,
1932 &response->mad.mad);
1933 if (ret & IB_MAD_RESULT_SUCCESS) {
1934 if (ret & IB_MAD_RESULT_CONSUMED)
1935 goto out;
1936 if (ret & IB_MAD_RESULT_REPLY) {
Sean Hefty34816ad2005-10-25 10:51:39 -07001937 agent_send_response(&response->mad.mad,
1938 &recv->grh, wc,
1939 port_priv->device,
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04001940 port_num,
Sean Hefty34816ad2005-10-25 10:51:39 -07001941 qp_info->qp->qp_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 goto out;
1943 }
1944 }
1945 }
1946
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07001947 mad_agent = find_mad_agent(port_priv, &recv->mad.mad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 if (mad_agent) {
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07001949 ib_mad_complete_recv(mad_agent, &recv->header.recv_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 /*
1951 * recv is freed up in error cases in ib_mad_complete_recv
1952 * or via recv_handler in ib_mad_complete_recv()
1953 */
1954 recv = NULL;
1955 }
1956
1957out:
1958 /* Post another receive request for this QP */
1959 if (response) {
1960 ib_mad_post_receive_mads(qp_info, response);
1961 if (recv)
1962 kmem_cache_free(ib_mad_cache, recv);
1963 } else
1964 ib_mad_post_receive_mads(qp_info, recv);
1965}
1966
1967static void adjust_timeout(struct ib_mad_agent_private *mad_agent_priv)
1968{
1969 struct ib_mad_send_wr_private *mad_send_wr;
1970 unsigned long delay;
1971
1972 if (list_empty(&mad_agent_priv->wait_list)) {
Roland Dreier6b2eef82009-09-07 08:27:50 -07001973 __cancel_delayed_work(&mad_agent_priv->timed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 } else {
1975 mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
1976 struct ib_mad_send_wr_private,
1977 agent_list);
1978
1979 if (time_after(mad_agent_priv->timeout,
1980 mad_send_wr->timeout)) {
1981 mad_agent_priv->timeout = mad_send_wr->timeout;
Roland Dreier6b2eef82009-09-07 08:27:50 -07001982 __cancel_delayed_work(&mad_agent_priv->timed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 delay = mad_send_wr->timeout - jiffies;
1984 if ((long)delay <= 0)
1985 delay = 1;
1986 queue_delayed_work(mad_agent_priv->qp_info->
1987 port_priv->wq,
1988 &mad_agent_priv->timed_work, delay);
1989 }
1990 }
1991}
1992
Hal Rosenstockd760ce82005-07-27 11:45:25 -07001993static void wait_for_response(struct ib_mad_send_wr_private *mad_send_wr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
Hal Rosenstockd760ce82005-07-27 11:45:25 -07001995 struct ib_mad_agent_private *mad_agent_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 struct ib_mad_send_wr_private *temp_mad_send_wr;
1997 struct list_head *list_item;
1998 unsigned long delay;
1999
Hal Rosenstockd760ce82005-07-27 11:45:25 -07002000 mad_agent_priv = mad_send_wr->mad_agent_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 list_del(&mad_send_wr->agent_list);
2002
2003 delay = mad_send_wr->timeout;
2004 mad_send_wr->timeout += jiffies;
2005
Hal Rosenstock29bb33d2005-07-27 11:45:32 -07002006 if (delay) {
2007 list_for_each_prev(list_item, &mad_agent_priv->wait_list) {
2008 temp_mad_send_wr = list_entry(list_item,
2009 struct ib_mad_send_wr_private,
2010 agent_list);
2011 if (time_after(mad_send_wr->timeout,
2012 temp_mad_send_wr->timeout))
2013 break;
2014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 }
Hal Rosenstock29bb33d2005-07-27 11:45:32 -07002016 else
2017 list_item = &mad_agent_priv->wait_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 list_add(&mad_send_wr->agent_list, list_item);
2019
2020 /* Reschedule a work item if we have a shorter timeout */
2021 if (mad_agent_priv->wait_list.next == &mad_send_wr->agent_list) {
Roland Dreier6b2eef82009-09-07 08:27:50 -07002022 __cancel_delayed_work(&mad_agent_priv->timed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 queue_delayed_work(mad_agent_priv->qp_info->port_priv->wq,
2024 &mad_agent_priv->timed_work, delay);
2025 }
2026}
2027
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002028void ib_reset_mad_timeout(struct ib_mad_send_wr_private *mad_send_wr,
2029 int timeout_ms)
2030{
2031 mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
2032 wait_for_response(mad_send_wr);
2033}
2034
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035/*
2036 * Process a send work completion
2037 */
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002038void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr,
2039 struct ib_mad_send_wc *mad_send_wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
2041 struct ib_mad_agent_private *mad_agent_priv;
2042 unsigned long flags;
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002043 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Hal Rosenstockd760ce82005-07-27 11:45:25 -07002045 mad_agent_priv = mad_send_wr->mad_agent_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 spin_lock_irqsave(&mad_agent_priv->lock, flags);
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002047 if (mad_agent_priv->agent.rmpp_version) {
2048 ret = ib_process_rmpp_send_wc(mad_send_wr, mad_send_wc);
2049 if (ret == IB_RMPP_RESULT_CONSUMED)
2050 goto done;
2051 } else
2052 ret = IB_RMPP_RESULT_UNHANDLED;
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 if (mad_send_wc->status != IB_WC_SUCCESS &&
2055 mad_send_wr->status == IB_WC_SUCCESS) {
2056 mad_send_wr->status = mad_send_wc->status;
2057 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2058 }
2059
2060 if (--mad_send_wr->refcount > 0) {
2061 if (mad_send_wr->refcount == 1 && mad_send_wr->timeout &&
2062 mad_send_wr->status == IB_WC_SUCCESS) {
Hal Rosenstockd760ce82005-07-27 11:45:25 -07002063 wait_for_response(mad_send_wr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 }
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002065 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 }
2067
2068 /* Remove send from MAD agent and notify client of completion */
2069 list_del(&mad_send_wr->agent_list);
2070 adjust_timeout(mad_agent_priv);
2071 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2072
2073 if (mad_send_wr->status != IB_WC_SUCCESS )
2074 mad_send_wc->status = mad_send_wr->status;
Sean Hefty34816ad2005-10-25 10:51:39 -07002075 if (ret == IB_RMPP_RESULT_INTERNAL)
2076 ib_rmpp_send_handler(mad_send_wc);
2077 else
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002078 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2079 mad_send_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
2081 /* Release reference on agent taken when sending */
Sean Hefty1b52fa982006-05-12 14:57:52 -07002082 deref_mad_agent(mad_agent_priv);
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002083 return;
2084done:
2085 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086}
2087
2088static void ib_mad_send_done_handler(struct ib_mad_port_private *port_priv,
2089 struct ib_wc *wc)
2090{
2091 struct ib_mad_send_wr_private *mad_send_wr, *queued_send_wr;
2092 struct ib_mad_list_head *mad_list;
2093 struct ib_mad_qp_info *qp_info;
2094 struct ib_mad_queue *send_queue;
2095 struct ib_send_wr *bad_send_wr;
Sean Hefty34816ad2005-10-25 10:51:39 -07002096 struct ib_mad_send_wc mad_send_wc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 unsigned long flags;
2098 int ret;
2099
2100 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
2101 mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
2102 mad_list);
2103 send_queue = mad_list->mad_queue;
2104 qp_info = send_queue->qp_info;
2105
2106retry:
Ralph Campbell15271062006-12-12 14:28:30 -08002107 ib_dma_unmap_single(mad_send_wr->send_buf.mad_agent->device,
2108 mad_send_wr->header_mapping,
2109 mad_send_wr->sg_list[0].length, DMA_TO_DEVICE);
2110 ib_dma_unmap_single(mad_send_wr->send_buf.mad_agent->device,
2111 mad_send_wr->payload_mapping,
2112 mad_send_wr->sg_list[1].length, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 queued_send_wr = NULL;
2114 spin_lock_irqsave(&send_queue->lock, flags);
2115 list_del(&mad_list->list);
2116
2117 /* Move queued send to the send queue */
2118 if (send_queue->count-- > send_queue->max_active) {
2119 mad_list = container_of(qp_info->overflow_list.next,
2120 struct ib_mad_list_head, list);
2121 queued_send_wr = container_of(mad_list,
2122 struct ib_mad_send_wr_private,
2123 mad_list);
Akinobu Mita179e0912006-06-26 00:24:41 -07002124 list_move_tail(&mad_list->list, &send_queue->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
2126 spin_unlock_irqrestore(&send_queue->lock, flags);
2127
Sean Hefty34816ad2005-10-25 10:51:39 -07002128 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2129 mad_send_wc.status = wc->status;
2130 mad_send_wc.vendor_err = wc->vendor_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if (atomic_read(&qp_info->snoop_count))
Sean Hefty34816ad2005-10-25 10:51:39 -07002132 snoop_send(qp_info, &mad_send_wr->send_buf, &mad_send_wc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 IB_MAD_SNOOP_SEND_COMPLETIONS);
Sean Hefty34816ad2005-10-25 10:51:39 -07002134 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 if (queued_send_wr) {
2137 ret = ib_post_send(qp_info->qp, &queued_send_wr->send_wr,
Sean Hefty34816ad2005-10-25 10:51:39 -07002138 &bad_send_wr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 if (ret) {
2140 printk(KERN_ERR PFX "ib_post_send failed: %d\n", ret);
2141 mad_send_wr = queued_send_wr;
2142 wc->status = IB_WC_LOC_QP_OP_ERR;
2143 goto retry;
2144 }
2145 }
2146}
2147
2148static void mark_sends_for_retry(struct ib_mad_qp_info *qp_info)
2149{
2150 struct ib_mad_send_wr_private *mad_send_wr;
2151 struct ib_mad_list_head *mad_list;
2152 unsigned long flags;
2153
2154 spin_lock_irqsave(&qp_info->send_queue.lock, flags);
2155 list_for_each_entry(mad_list, &qp_info->send_queue.list, list) {
2156 mad_send_wr = container_of(mad_list,
2157 struct ib_mad_send_wr_private,
2158 mad_list);
2159 mad_send_wr->retry = 1;
2160 }
2161 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
2162}
2163
2164static void mad_error_handler(struct ib_mad_port_private *port_priv,
2165 struct ib_wc *wc)
2166{
2167 struct ib_mad_list_head *mad_list;
2168 struct ib_mad_qp_info *qp_info;
2169 struct ib_mad_send_wr_private *mad_send_wr;
2170 int ret;
2171
2172 /* Determine if failure was a send or receive */
2173 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
2174 qp_info = mad_list->mad_queue->qp_info;
2175 if (mad_list->mad_queue == &qp_info->recv_queue)
2176 /*
2177 * Receive errors indicate that the QP has entered the error
2178 * state - error handling/shutdown code will cleanup
2179 */
2180 return;
2181
2182 /*
2183 * Send errors will transition the QP to SQE - move
2184 * QP to RTS and repost flushed work requests
2185 */
2186 mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
2187 mad_list);
2188 if (wc->status == IB_WC_WR_FLUSH_ERR) {
2189 if (mad_send_wr->retry) {
2190 /* Repost send */
2191 struct ib_send_wr *bad_send_wr;
2192
2193 mad_send_wr->retry = 0;
2194 ret = ib_post_send(qp_info->qp, &mad_send_wr->send_wr,
2195 &bad_send_wr);
2196 if (ret)
2197 ib_mad_send_done_handler(port_priv, wc);
2198 } else
2199 ib_mad_send_done_handler(port_priv, wc);
2200 } else {
2201 struct ib_qp_attr *attr;
2202
2203 /* Transition QP to RTS and fail offending send */
2204 attr = kmalloc(sizeof *attr, GFP_KERNEL);
2205 if (attr) {
2206 attr->qp_state = IB_QPS_RTS;
2207 attr->cur_qp_state = IB_QPS_SQE;
2208 ret = ib_modify_qp(qp_info->qp, attr,
2209 IB_QP_STATE | IB_QP_CUR_STATE);
2210 kfree(attr);
2211 if (ret)
2212 printk(KERN_ERR PFX "mad_error_handler - "
2213 "ib_modify_qp to RTS : %d\n", ret);
2214 else
2215 mark_sends_for_retry(qp_info);
2216 }
2217 ib_mad_send_done_handler(port_priv, wc);
2218 }
2219}
2220
2221/*
2222 * IB MAD completion callback
2223 */
David Howellsc4028952006-11-22 14:57:56 +00002224static void ib_mad_completion_handler(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225{
2226 struct ib_mad_port_private *port_priv;
2227 struct ib_wc wc;
2228
David Howellsc4028952006-11-22 14:57:56 +00002229 port_priv = container_of(work, struct ib_mad_port_private, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
2231
2232 while (ib_poll_cq(port_priv->cq, 1, &wc) == 1) {
2233 if (wc.status == IB_WC_SUCCESS) {
2234 switch (wc.opcode) {
2235 case IB_WC_SEND:
2236 ib_mad_send_done_handler(port_priv, &wc);
2237 break;
2238 case IB_WC_RECV:
2239 ib_mad_recv_done_handler(port_priv, &wc);
2240 break;
2241 default:
2242 BUG_ON(1);
2243 break;
2244 }
2245 } else
2246 mad_error_handler(port_priv, &wc);
2247 }
2248}
2249
2250static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv)
2251{
2252 unsigned long flags;
2253 struct ib_mad_send_wr_private *mad_send_wr, *temp_mad_send_wr;
2254 struct ib_mad_send_wc mad_send_wc;
2255 struct list_head cancel_list;
2256
2257 INIT_LIST_HEAD(&cancel_list);
2258
2259 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2260 list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
2261 &mad_agent_priv->send_list, agent_list) {
2262 if (mad_send_wr->status == IB_WC_SUCCESS) {
Roland Dreier3cd96562006-09-22 15:22:46 -07002263 mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2265 }
2266 }
2267
2268 /* Empty wait list to prevent receives from finding a request */
2269 list_splice_init(&mad_agent_priv->wait_list, &cancel_list);
2270 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2271
2272 /* Report all cancelled requests */
2273 mad_send_wc.status = IB_WC_WR_FLUSH_ERR;
2274 mad_send_wc.vendor_err = 0;
2275
2276 list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
2277 &cancel_list, agent_list) {
Sean Hefty34816ad2005-10-25 10:51:39 -07002278 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2279 list_del(&mad_send_wr->agent_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2281 &mad_send_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 atomic_dec(&mad_agent_priv->refcount);
2283 }
2284}
2285
2286static struct ib_mad_send_wr_private*
Sean Hefty34816ad2005-10-25 10:51:39 -07002287find_send_wr(struct ib_mad_agent_private *mad_agent_priv,
2288 struct ib_mad_send_buf *send_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289{
2290 struct ib_mad_send_wr_private *mad_send_wr;
2291
2292 list_for_each_entry(mad_send_wr, &mad_agent_priv->wait_list,
2293 agent_list) {
Sean Hefty34816ad2005-10-25 10:51:39 -07002294 if (&mad_send_wr->send_buf == send_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 return mad_send_wr;
2296 }
2297
2298 list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list,
2299 agent_list) {
Sean Hefty34816ad2005-10-25 10:51:39 -07002300 if (is_data_mad(mad_agent_priv, mad_send_wr->send_buf.mad) &&
2301 &mad_send_wr->send_buf == send_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 return mad_send_wr;
2303 }
2304 return NULL;
2305}
2306
Sean Hefty34816ad2005-10-25 10:51:39 -07002307int ib_modify_mad(struct ib_mad_agent *mad_agent,
2308 struct ib_mad_send_buf *send_buf, u32 timeout_ms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309{
2310 struct ib_mad_agent_private *mad_agent_priv;
2311 struct ib_mad_send_wr_private *mad_send_wr;
2312 unsigned long flags;
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07002313 int active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
2315 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
2316 agent);
2317 spin_lock_irqsave(&mad_agent_priv->lock, flags);
Sean Hefty34816ad2005-10-25 10:51:39 -07002318 mad_send_wr = find_send_wr(mad_agent_priv, send_buf);
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002319 if (!mad_send_wr || mad_send_wr->status != IB_WC_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002321 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 }
2323
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07002324 active = (!mad_send_wr->timeout || mad_send_wr->refcount > 1);
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002325 if (!timeout_ms) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002327 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 }
2329
Sean Hefty34816ad2005-10-25 10:51:39 -07002330 mad_send_wr->send_buf.timeout_ms = timeout_ms;
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07002331 if (active)
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002332 mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
2333 else
2334 ib_reset_mad_timeout(mad_send_wr, timeout_ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002336 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2337 return 0;
2338}
2339EXPORT_SYMBOL(ib_modify_mad);
2340
Sean Hefty34816ad2005-10-25 10:51:39 -07002341void ib_cancel_mad(struct ib_mad_agent *mad_agent,
2342 struct ib_mad_send_buf *send_buf)
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002343{
Sean Hefty34816ad2005-10-25 10:51:39 -07002344 ib_modify_mad(mad_agent, send_buf, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345}
2346EXPORT_SYMBOL(ib_cancel_mad);
2347
David Howellsc4028952006-11-22 14:57:56 +00002348static void local_completions(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349{
2350 struct ib_mad_agent_private *mad_agent_priv;
2351 struct ib_mad_local_private *local;
2352 struct ib_mad_agent_private *recv_mad_agent;
2353 unsigned long flags;
Ralph Campbell1d9bc6d62009-02-27 10:34:30 -08002354 int free_mad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 struct ib_wc wc;
2356 struct ib_mad_send_wc mad_send_wc;
2357
David Howellsc4028952006-11-22 14:57:56 +00002358 mad_agent_priv =
2359 container_of(work, struct ib_mad_agent_private, local_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
2361 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2362 while (!list_empty(&mad_agent_priv->local_list)) {
2363 local = list_entry(mad_agent_priv->local_list.next,
2364 struct ib_mad_local_private,
2365 completion_list);
Michael S. Tsirkin37289ef2006-03-30 15:52:54 +02002366 list_del(&local->completion_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
Ralph Campbell1d9bc6d62009-02-27 10:34:30 -08002368 free_mad = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 if (local->mad_priv) {
2370 recv_mad_agent = local->recv_mad_agent;
2371 if (!recv_mad_agent) {
2372 printk(KERN_ERR PFX "No receive MAD agent for local completion\n");
Ralph Campbell1d9bc6d62009-02-27 10:34:30 -08002373 free_mad = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 goto local_send_completion;
2375 }
2376
2377 /*
2378 * Defined behavior is to complete response
2379 * before request
2380 */
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +02002381 build_smp_wc(recv_mad_agent->agent.qp,
2382 (unsigned long) local->mad_send_wr,
Sean Hefty97f52eb2005-08-13 21:05:57 -07002383 be16_to_cpu(IB_LID_PERMISSIVE),
Sean Hefty34816ad2005-10-25 10:51:39 -07002384 0, recv_mad_agent->agent.port_num, &wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
2386 local->mad_priv->header.recv_wc.wc = &wc;
2387 local->mad_priv->header.recv_wc.mad_len =
2388 sizeof(struct ib_mad);
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002389 INIT_LIST_HEAD(&local->mad_priv->header.recv_wc.rmpp_list);
2390 list_add(&local->mad_priv->header.recv_wc.recv_buf.list,
2391 &local->mad_priv->header.recv_wc.rmpp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 local->mad_priv->header.recv_wc.recv_buf.grh = NULL;
2393 local->mad_priv->header.recv_wc.recv_buf.mad =
2394 &local->mad_priv->mad.mad;
2395 if (atomic_read(&recv_mad_agent->qp_info->snoop_count))
2396 snoop_recv(recv_mad_agent->qp_info,
2397 &local->mad_priv->header.recv_wc,
2398 IB_MAD_SNOOP_RECVS);
2399 recv_mad_agent->agent.recv_handler(
2400 &recv_mad_agent->agent,
2401 &local->mad_priv->header.recv_wc);
2402 spin_lock_irqsave(&recv_mad_agent->lock, flags);
2403 atomic_dec(&recv_mad_agent->refcount);
2404 spin_unlock_irqrestore(&recv_mad_agent->lock, flags);
2405 }
2406
2407local_send_completion:
2408 /* Complete send */
2409 mad_send_wc.status = IB_WC_SUCCESS;
2410 mad_send_wc.vendor_err = 0;
Sean Hefty34816ad2005-10-25 10:51:39 -07002411 mad_send_wc.send_buf = &local->mad_send_wr->send_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 if (atomic_read(&mad_agent_priv->qp_info->snoop_count))
Sean Hefty34816ad2005-10-25 10:51:39 -07002413 snoop_send(mad_agent_priv->qp_info,
2414 &local->mad_send_wr->send_buf,
2415 &mad_send_wc, IB_MAD_SNOOP_SEND_COMPLETIONS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2417 &mad_send_wc);
2418
2419 spin_lock_irqsave(&mad_agent_priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 atomic_dec(&mad_agent_priv->refcount);
Ralph Campbell1d9bc6d62009-02-27 10:34:30 -08002421 if (free_mad)
Hal Rosenstock2c153b92005-07-27 11:45:31 -07002422 kmem_cache_free(ib_mad_cache, local->mad_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 kfree(local);
2424 }
2425 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2426}
2427
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002428static int retry_send(struct ib_mad_send_wr_private *mad_send_wr)
2429{
2430 int ret;
2431
Sean Hefty4fc8cd42007-11-27 00:11:04 -08002432 if (!mad_send_wr->retries_left)
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002433 return -ETIMEDOUT;
2434
Sean Hefty4fc8cd42007-11-27 00:11:04 -08002435 mad_send_wr->retries_left--;
2436 mad_send_wr->send_buf.retries++;
2437
Sean Hefty34816ad2005-10-25 10:51:39 -07002438 mad_send_wr->timeout = msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms);
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002439
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002440 if (mad_send_wr->mad_agent_priv->agent.rmpp_version) {
2441 ret = ib_retry_rmpp(mad_send_wr);
2442 switch (ret) {
2443 case IB_RMPP_RESULT_UNHANDLED:
2444 ret = ib_send_mad(mad_send_wr);
2445 break;
2446 case IB_RMPP_RESULT_CONSUMED:
2447 ret = 0;
2448 break;
2449 default:
2450 ret = -ECOMM;
2451 break;
2452 }
2453 } else
2454 ret = ib_send_mad(mad_send_wr);
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002455
2456 if (!ret) {
2457 mad_send_wr->refcount++;
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002458 list_add_tail(&mad_send_wr->agent_list,
2459 &mad_send_wr->mad_agent_priv->send_list);
2460 }
2461 return ret;
2462}
2463
David Howellsc4028952006-11-22 14:57:56 +00002464static void timeout_sends(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465{
2466 struct ib_mad_agent_private *mad_agent_priv;
2467 struct ib_mad_send_wr_private *mad_send_wr;
2468 struct ib_mad_send_wc mad_send_wc;
2469 unsigned long flags, delay;
2470
David Howellsc4028952006-11-22 14:57:56 +00002471 mad_agent_priv = container_of(work, struct ib_mad_agent_private,
2472 timed_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 mad_send_wc.vendor_err = 0;
2474
2475 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2476 while (!list_empty(&mad_agent_priv->wait_list)) {
2477 mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
2478 struct ib_mad_send_wr_private,
2479 agent_list);
2480
2481 if (time_after(mad_send_wr->timeout, jiffies)) {
2482 delay = mad_send_wr->timeout - jiffies;
2483 if ((long)delay <= 0)
2484 delay = 1;
2485 queue_delayed_work(mad_agent_priv->qp_info->
2486 port_priv->wq,
2487 &mad_agent_priv->timed_work, delay);
2488 break;
2489 }
2490
Hal Rosenstockdbf92272005-07-27 11:45:30 -07002491 list_del(&mad_send_wr->agent_list);
Hal Rosenstock29bb33d2005-07-27 11:45:32 -07002492 if (mad_send_wr->status == IB_WC_SUCCESS &&
2493 !retry_send(mad_send_wr))
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002494 continue;
2495
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2497
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002498 if (mad_send_wr->status == IB_WC_SUCCESS)
2499 mad_send_wc.status = IB_WC_RESP_TIMEOUT_ERR;
2500 else
2501 mad_send_wc.status = mad_send_wr->status;
Sean Hefty34816ad2005-10-25 10:51:39 -07002502 mad_send_wc.send_buf = &mad_send_wr->send_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2504 &mad_send_wc);
2505
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 atomic_dec(&mad_agent_priv->refcount);
2507 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2508 }
2509 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2510}
2511
Hal Rosenstock5dd2ce12005-08-15 14:16:36 -07002512static void ib_mad_thread_completion_handler(struct ib_cq *cq, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513{
2514 struct ib_mad_port_private *port_priv = cq->cq_context;
Michael S. Tsirkindc059802006-03-20 10:08:25 -08002515 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
Michael S. Tsirkindc059802006-03-20 10:08:25 -08002517 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2518 if (!list_empty(&port_priv->port_list))
2519 queue_work(port_priv->wq, &port_priv->work);
2520 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521}
2522
2523/*
2524 * Allocate receive MADs and post receive WRs for them
2525 */
2526static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
2527 struct ib_mad_private *mad)
2528{
2529 unsigned long flags;
2530 int post, ret;
2531 struct ib_mad_private *mad_priv;
2532 struct ib_sge sg_list;
2533 struct ib_recv_wr recv_wr, *bad_recv_wr;
2534 struct ib_mad_queue *recv_queue = &qp_info->recv_queue;
2535
2536 /* Initialize common scatter list fields */
2537 sg_list.length = sizeof *mad_priv - sizeof mad_priv->header;
2538 sg_list.lkey = (*qp_info->port_priv->mr).lkey;
2539
2540 /* Initialize common receive WR fields */
2541 recv_wr.next = NULL;
2542 recv_wr.sg_list = &sg_list;
2543 recv_wr.num_sge = 1;
2544
2545 do {
2546 /* Allocate and map receive buffer */
2547 if (mad) {
2548 mad_priv = mad;
2549 mad = NULL;
2550 } else {
2551 mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL);
2552 if (!mad_priv) {
2553 printk(KERN_ERR PFX "No memory for receive buffer\n");
2554 ret = -ENOMEM;
2555 break;
2556 }
2557 }
Ralph Campbell15271062006-12-12 14:28:30 -08002558 sg_list.addr = ib_dma_map_single(qp_info->port_priv->device,
2559 &mad_priv->grh,
2560 sizeof *mad_priv -
2561 sizeof mad_priv->header,
2562 DMA_FROM_DEVICE);
2563 mad_priv->header.mapping = sg_list.addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 recv_wr.wr_id = (unsigned long)&mad_priv->header.mad_list;
2565 mad_priv->header.mad_list.mad_queue = recv_queue;
2566
2567 /* Post receive WR */
2568 spin_lock_irqsave(&recv_queue->lock, flags);
2569 post = (++recv_queue->count < recv_queue->max_active);
2570 list_add_tail(&mad_priv->header.mad_list.list, &recv_queue->list);
2571 spin_unlock_irqrestore(&recv_queue->lock, flags);
2572 ret = ib_post_recv(qp_info->qp, &recv_wr, &bad_recv_wr);
2573 if (ret) {
2574 spin_lock_irqsave(&recv_queue->lock, flags);
2575 list_del(&mad_priv->header.mad_list.list);
2576 recv_queue->count--;
2577 spin_unlock_irqrestore(&recv_queue->lock, flags);
Ralph Campbell15271062006-12-12 14:28:30 -08002578 ib_dma_unmap_single(qp_info->port_priv->device,
2579 mad_priv->header.mapping,
2580 sizeof *mad_priv -
2581 sizeof mad_priv->header,
2582 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 kmem_cache_free(ib_mad_cache, mad_priv);
2584 printk(KERN_ERR PFX "ib_post_recv failed: %d\n", ret);
2585 break;
2586 }
2587 } while (post);
2588
2589 return ret;
2590}
2591
2592/*
2593 * Return all the posted receive MADs
2594 */
2595static void cleanup_recv_queue(struct ib_mad_qp_info *qp_info)
2596{
2597 struct ib_mad_private_header *mad_priv_hdr;
2598 struct ib_mad_private *recv;
2599 struct ib_mad_list_head *mad_list;
2600
2601 while (!list_empty(&qp_info->recv_queue.list)) {
2602
2603 mad_list = list_entry(qp_info->recv_queue.list.next,
2604 struct ib_mad_list_head, list);
2605 mad_priv_hdr = container_of(mad_list,
2606 struct ib_mad_private_header,
2607 mad_list);
2608 recv = container_of(mad_priv_hdr, struct ib_mad_private,
2609 header);
2610
2611 /* Remove from posted receive MAD list */
2612 list_del(&mad_list->list);
2613
Ralph Campbell15271062006-12-12 14:28:30 -08002614 ib_dma_unmap_single(qp_info->port_priv->device,
2615 recv->header.mapping,
2616 sizeof(struct ib_mad_private) -
2617 sizeof(struct ib_mad_private_header),
2618 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 kmem_cache_free(ib_mad_cache, recv);
2620 }
2621
2622 qp_info->recv_queue.count = 0;
2623}
2624
2625/*
2626 * Start the port
2627 */
2628static int ib_mad_port_start(struct ib_mad_port_private *port_priv)
2629{
2630 int ret, i;
2631 struct ib_qp_attr *attr;
2632 struct ib_qp *qp;
2633
2634 attr = kmalloc(sizeof *attr, GFP_KERNEL);
Roland Dreier3cd96562006-09-22 15:22:46 -07002635 if (!attr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 printk(KERN_ERR PFX "Couldn't kmalloc ib_qp_attr\n");
2637 return -ENOMEM;
2638 }
2639
2640 for (i = 0; i < IB_MAD_QPS_CORE; i++) {
2641 qp = port_priv->qp_info[i].qp;
2642 /*
2643 * PKey index for QP1 is irrelevant but
2644 * one is needed for the Reset to Init transition
2645 */
2646 attr->qp_state = IB_QPS_INIT;
2647 attr->pkey_index = 0;
2648 attr->qkey = (qp->qp_num == 0) ? 0 : IB_QP1_QKEY;
2649 ret = ib_modify_qp(qp, attr, IB_QP_STATE |
2650 IB_QP_PKEY_INDEX | IB_QP_QKEY);
2651 if (ret) {
2652 printk(KERN_ERR PFX "Couldn't change QP%d state to "
2653 "INIT: %d\n", i, ret);
2654 goto out;
2655 }
2656
2657 attr->qp_state = IB_QPS_RTR;
2658 ret = ib_modify_qp(qp, attr, IB_QP_STATE);
2659 if (ret) {
2660 printk(KERN_ERR PFX "Couldn't change QP%d state to "
2661 "RTR: %d\n", i, ret);
2662 goto out;
2663 }
2664
2665 attr->qp_state = IB_QPS_RTS;
2666 attr->sq_psn = IB_MAD_SEND_Q_PSN;
2667 ret = ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_SQ_PSN);
2668 if (ret) {
2669 printk(KERN_ERR PFX "Couldn't change QP%d state to "
2670 "RTS: %d\n", i, ret);
2671 goto out;
2672 }
2673 }
2674
2675 ret = ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
2676 if (ret) {
2677 printk(KERN_ERR PFX "Failed to request completion "
2678 "notification: %d\n", ret);
2679 goto out;
2680 }
2681
2682 for (i = 0; i < IB_MAD_QPS_CORE; i++) {
2683 ret = ib_mad_post_receive_mads(&port_priv->qp_info[i], NULL);
2684 if (ret) {
2685 printk(KERN_ERR PFX "Couldn't post receive WRs\n");
2686 goto out;
2687 }
2688 }
2689out:
2690 kfree(attr);
2691 return ret;
2692}
2693
2694static void qp_event_handler(struct ib_event *event, void *qp_context)
2695{
2696 struct ib_mad_qp_info *qp_info = qp_context;
2697
2698 /* It's worse than that! He's dead, Jim! */
2699 printk(KERN_ERR PFX "Fatal error (%d) on MAD QP (%d)\n",
2700 event->event, qp_info->qp->qp_num);
2701}
2702
2703static void init_mad_queue(struct ib_mad_qp_info *qp_info,
2704 struct ib_mad_queue *mad_queue)
2705{
2706 mad_queue->qp_info = qp_info;
2707 mad_queue->count = 0;
2708 spin_lock_init(&mad_queue->lock);
2709 INIT_LIST_HEAD(&mad_queue->list);
2710}
2711
2712static void init_mad_qp(struct ib_mad_port_private *port_priv,
2713 struct ib_mad_qp_info *qp_info)
2714{
2715 qp_info->port_priv = port_priv;
2716 init_mad_queue(qp_info, &qp_info->send_queue);
2717 init_mad_queue(qp_info, &qp_info->recv_queue);
2718 INIT_LIST_HEAD(&qp_info->overflow_list);
2719 spin_lock_init(&qp_info->snoop_lock);
2720 qp_info->snoop_table = NULL;
2721 qp_info->snoop_table_size = 0;
2722 atomic_set(&qp_info->snoop_count, 0);
2723}
2724
2725static int create_mad_qp(struct ib_mad_qp_info *qp_info,
2726 enum ib_qp_type qp_type)
2727{
2728 struct ib_qp_init_attr qp_init_attr;
2729 int ret;
2730
2731 memset(&qp_init_attr, 0, sizeof qp_init_attr);
2732 qp_init_attr.send_cq = qp_info->port_priv->cq;
2733 qp_init_attr.recv_cq = qp_info->port_priv->cq;
2734 qp_init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
Hal Rosenstockb76aabc2009-09-07 08:28:48 -07002735 qp_init_attr.cap.max_send_wr = mad_sendq_size;
2736 qp_init_attr.cap.max_recv_wr = mad_recvq_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 qp_init_attr.cap.max_send_sge = IB_MAD_SEND_REQ_MAX_SG;
2738 qp_init_attr.cap.max_recv_sge = IB_MAD_RECV_REQ_MAX_SG;
2739 qp_init_attr.qp_type = qp_type;
2740 qp_init_attr.port_num = qp_info->port_priv->port_num;
2741 qp_init_attr.qp_context = qp_info;
2742 qp_init_attr.event_handler = qp_event_handler;
2743 qp_info->qp = ib_create_qp(qp_info->port_priv->pd, &qp_init_attr);
2744 if (IS_ERR(qp_info->qp)) {
2745 printk(KERN_ERR PFX "Couldn't create ib_mad QP%d\n",
2746 get_spl_qp_index(qp_type));
2747 ret = PTR_ERR(qp_info->qp);
2748 goto error;
2749 }
2750 /* Use minimum queue sizes unless the CQ is resized */
Hal Rosenstockb76aabc2009-09-07 08:28:48 -07002751 qp_info->send_queue.max_active = mad_sendq_size;
2752 qp_info->recv_queue.max_active = mad_recvq_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 return 0;
2754
2755error:
2756 return ret;
2757}
2758
2759static void destroy_mad_qp(struct ib_mad_qp_info *qp_info)
2760{
2761 ib_destroy_qp(qp_info->qp);
Jesper Juhl6044ec82005-11-07 01:01:32 -08002762 kfree(qp_info->snoop_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763}
2764
2765/*
2766 * Open the port
2767 * Create the QP, PD, MR, and CQ if needed
2768 */
2769static int ib_mad_port_open(struct ib_device *device,
2770 int port_num)
2771{
2772 int ret, cq_size;
2773 struct ib_mad_port_private *port_priv;
2774 unsigned long flags;
2775 char name[sizeof "ib_mad123"];
2776
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 /* Create new device info */
Roland Dreierde6eb662005-11-02 07:23:14 -08002778 port_priv = kzalloc(sizeof *port_priv, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 if (!port_priv) {
2780 printk(KERN_ERR PFX "No memory for ib_mad_port_private\n");
2781 return -ENOMEM;
2782 }
Roland Dreierde6eb662005-11-02 07:23:14 -08002783
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 port_priv->device = device;
2785 port_priv->port_num = port_num;
2786 spin_lock_init(&port_priv->reg_lock);
2787 INIT_LIST_HEAD(&port_priv->agent_list);
2788 init_mad_qp(port_priv, &port_priv->qp_info[0]);
2789 init_mad_qp(port_priv, &port_priv->qp_info[1]);
2790
Hal Rosenstockb76aabc2009-09-07 08:28:48 -07002791 cq_size = (mad_sendq_size + mad_recvq_size) * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 port_priv->cq = ib_create_cq(port_priv->device,
Hal Rosenstock5dd2ce12005-08-15 14:16:36 -07002793 ib_mad_thread_completion_handler,
Michael S. Tsirkinf4fd0b22007-05-03 13:48:47 +03002794 NULL, port_priv, cq_size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 if (IS_ERR(port_priv->cq)) {
2796 printk(KERN_ERR PFX "Couldn't create ib_mad CQ\n");
2797 ret = PTR_ERR(port_priv->cq);
2798 goto error3;
2799 }
2800
2801 port_priv->pd = ib_alloc_pd(device);
2802 if (IS_ERR(port_priv->pd)) {
2803 printk(KERN_ERR PFX "Couldn't create ib_mad PD\n");
2804 ret = PTR_ERR(port_priv->pd);
2805 goto error4;
2806 }
2807
2808 port_priv->mr = ib_get_dma_mr(port_priv->pd, IB_ACCESS_LOCAL_WRITE);
2809 if (IS_ERR(port_priv->mr)) {
2810 printk(KERN_ERR PFX "Couldn't get ib_mad DMA MR\n");
2811 ret = PTR_ERR(port_priv->mr);
2812 goto error5;
2813 }
2814
2815 ret = create_mad_qp(&port_priv->qp_info[0], IB_QPT_SMI);
2816 if (ret)
2817 goto error6;
2818 ret = create_mad_qp(&port_priv->qp_info[1], IB_QPT_GSI);
2819 if (ret)
2820 goto error7;
2821
2822 snprintf(name, sizeof name, "ib_mad%d", port_num);
2823 port_priv->wq = create_singlethread_workqueue(name);
2824 if (!port_priv->wq) {
2825 ret = -ENOMEM;
2826 goto error8;
2827 }
David Howellsc4028952006-11-22 14:57:56 +00002828 INIT_WORK(&port_priv->work, ib_mad_completion_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829
Michael S. Tsirkindc059802006-03-20 10:08:25 -08002830 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2831 list_add_tail(&port_priv->port_list, &ib_mad_port_list);
2832 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2833
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 ret = ib_mad_port_start(port_priv);
2835 if (ret) {
2836 printk(KERN_ERR PFX "Couldn't start port\n");
2837 goto error9;
2838 }
2839
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 return 0;
2841
2842error9:
Michael S. Tsirkindc059802006-03-20 10:08:25 -08002843 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2844 list_del_init(&port_priv->port_list);
2845 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2846
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 destroy_workqueue(port_priv->wq);
2848error8:
2849 destroy_mad_qp(&port_priv->qp_info[1]);
2850error7:
2851 destroy_mad_qp(&port_priv->qp_info[0]);
2852error6:
2853 ib_dereg_mr(port_priv->mr);
2854error5:
2855 ib_dealloc_pd(port_priv->pd);
2856error4:
2857 ib_destroy_cq(port_priv->cq);
2858 cleanup_recv_queue(&port_priv->qp_info[1]);
2859 cleanup_recv_queue(&port_priv->qp_info[0]);
2860error3:
2861 kfree(port_priv);
2862
2863 return ret;
2864}
2865
2866/*
2867 * Close the port
2868 * If there are no classes using the port, free the port
2869 * resources (CQ, MR, PD, QP) and remove the port's info structure
2870 */
2871static int ib_mad_port_close(struct ib_device *device, int port_num)
2872{
2873 struct ib_mad_port_private *port_priv;
2874 unsigned long flags;
2875
2876 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2877 port_priv = __ib_get_mad_port(device, port_num);
2878 if (port_priv == NULL) {
2879 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2880 printk(KERN_ERR PFX "Port %d not found\n", port_num);
2881 return -ENODEV;
2882 }
Michael S. Tsirkindc059802006-03-20 10:08:25 -08002883 list_del_init(&port_priv->port_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2885
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 destroy_workqueue(port_priv->wq);
2887 destroy_mad_qp(&port_priv->qp_info[1]);
2888 destroy_mad_qp(&port_priv->qp_info[0]);
2889 ib_dereg_mr(port_priv->mr);
2890 ib_dealloc_pd(port_priv->pd);
2891 ib_destroy_cq(port_priv->cq);
2892 cleanup_recv_queue(&port_priv->qp_info[1]);
2893 cleanup_recv_queue(&port_priv->qp_info[0]);
2894 /* XXX: Handle deallocation of MAD registration tables */
2895
2896 kfree(port_priv);
2897
2898 return 0;
2899}
2900
2901static void ib_mad_init_device(struct ib_device *device)
2902{
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002903 int start, end, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
Tom Tucker07ebafb2006-08-03 16:02:42 -05002905 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
2906 return;
2907
2908 if (device->node_type == RDMA_NODE_IB_SWITCH) {
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002909 start = 0;
2910 end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 } else {
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002912 start = 1;
2913 end = device->phys_port_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 }
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002915
2916 for (i = start; i <= end; i++) {
2917 if (ib_mad_port_open(device, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 printk(KERN_ERR PFX "Couldn't open %s port %d\n",
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002919 device->name, i);
2920 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 }
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002922 if (ib_agent_port_open(device, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 printk(KERN_ERR PFX "Couldn't open %s port %d "
2924 "for agents\n",
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002925 device->name, i);
2926 goto error_agent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 }
2928 }
Hal Rosenstockf68bcc22005-07-27 11:45:27 -07002929 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002931error_agent:
2932 if (ib_mad_port_close(device, i))
2933 printk(KERN_ERR PFX "Couldn't close %s port %d\n",
2934 device->name, i);
2935
2936error:
2937 i--;
2938
2939 while (i >= start) {
2940 if (ib_agent_port_close(device, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 printk(KERN_ERR PFX "Couldn't close %s port %d "
2942 "for agents\n",
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002943 device->name, i);
2944 if (ib_mad_port_close(device, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 printk(KERN_ERR PFX "Couldn't close %s port %d\n",
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002946 device->name, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 i--;
2948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949}
2950
2951static void ib_mad_remove_device(struct ib_device *device)
2952{
Hal Rosenstockf68bcc22005-07-27 11:45:27 -07002953 int i, num_ports, cur_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954
Steve Wise070e1402010-03-04 18:18:18 +00002955 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
2956 return;
2957
Tom Tucker07ebafb2006-08-03 16:02:42 -05002958 if (device->node_type == RDMA_NODE_IB_SWITCH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 num_ports = 1;
2960 cur_port = 0;
2961 } else {
2962 num_ports = device->phys_port_cnt;
2963 cur_port = 1;
2964 }
2965 for (i = 0; i < num_ports; i++, cur_port++) {
Hal Rosenstockf68bcc22005-07-27 11:45:27 -07002966 if (ib_agent_port_close(device, cur_port))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 printk(KERN_ERR PFX "Couldn't close %s port %d "
2968 "for agents\n",
2969 device->name, cur_port);
Hal Rosenstockf68bcc22005-07-27 11:45:27 -07002970 if (ib_mad_port_close(device, cur_port))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 printk(KERN_ERR PFX "Couldn't close %s port %d\n",
2972 device->name, cur_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 }
2974}
2975
2976static struct ib_client mad_client = {
2977 .name = "mad",
2978 .add = ib_mad_init_device,
2979 .remove = ib_mad_remove_device
2980};
2981
2982static int __init ib_mad_init_module(void)
2983{
2984 int ret;
2985
Hal Rosenstockb76aabc2009-09-07 08:28:48 -07002986 mad_recvq_size = min(mad_recvq_size, IB_MAD_QP_MAX_SIZE);
2987 mad_recvq_size = max(mad_recvq_size, IB_MAD_QP_MIN_SIZE);
2988
2989 mad_sendq_size = min(mad_sendq_size, IB_MAD_QP_MAX_SIZE);
2990 mad_sendq_size = max(mad_sendq_size, IB_MAD_QP_MIN_SIZE);
2991
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 ib_mad_cache = kmem_cache_create("ib_mad",
2993 sizeof(struct ib_mad_private),
2994 0,
2995 SLAB_HWCACHE_ALIGN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 NULL);
2997 if (!ib_mad_cache) {
2998 printk(KERN_ERR PFX "Couldn't create ib_mad cache\n");
2999 ret = -ENOMEM;
3000 goto error1;
3001 }
3002
3003 INIT_LIST_HEAD(&ib_mad_port_list);
3004
3005 if (ib_register_client(&mad_client)) {
3006 printk(KERN_ERR PFX "Couldn't register ib_mad client\n");
3007 ret = -EINVAL;
3008 goto error2;
3009 }
3010
3011 return 0;
3012
3013error2:
3014 kmem_cache_destroy(ib_mad_cache);
3015error1:
3016 return ret;
3017}
3018
3019static void __exit ib_mad_cleanup_module(void)
3020{
3021 ib_unregister_client(&mad_client);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07003022 kmem_cache_destroy(ib_mad_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023}
3024
3025module_init(ib_mad_init_module);
3026module_exit(ib_mad_cleanup_module);