blob: 7522008fda86880f759128e694d9df0cda68c764 [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>
Jack Morgenstein9874e742006-06-17 20:37:34 -070037#include <rdma/ib_cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include "mad_priv.h"
Hal Rosenstockfa619a72005-07-27 11:45:37 -070040#include "mad_rmpp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "smi.h"
42#include "agent.h"
43
44MODULE_LICENSE("Dual BSD/GPL");
45MODULE_DESCRIPTION("kernel IB MAD API");
46MODULE_AUTHOR("Hal Rosenstock");
47MODULE_AUTHOR("Sean Hefty");
48
Hal Rosenstockb76aabc2009-09-07 08:28:48 -070049int mad_sendq_size = IB_MAD_QP_SEND_SIZE;
50int mad_recvq_size = IB_MAD_QP_RECV_SIZE;
51
52module_param_named(send_queue_size, mad_sendq_size, int, 0444);
53MODULE_PARM_DESC(send_queue_size, "Size of send queue in number of work requests");
54module_param_named(recv_queue_size, mad_recvq_size, int, 0444);
55MODULE_PARM_DESC(recv_queue_size, "Size of receive queue in number of work requests");
56
Roland Dreiere54f8182006-11-29 15:33:07 -080057static struct kmem_cache *ib_mad_cache;
Hal Rosenstockfa619a72005-07-27 11:45:37 -070058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static struct list_head ib_mad_port_list;
60static u32 ib_mad_client_id = 0;
61
62/* Port list lock */
Roland Dreier6276e082009-09-05 20:24:23 -070063static DEFINE_SPINLOCK(ib_mad_port_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/* Forward declarations */
66static int method_in_use(struct ib_mad_mgmt_method_table **method,
67 struct ib_mad_reg_req *mad_reg_req);
68static void remove_mad_reg_req(struct ib_mad_agent_private *priv);
69static struct ib_mad_agent_private *find_mad_agent(
70 struct ib_mad_port_private *port_priv,
Hal Rosenstock4a0754f2005-07-27 11:45:24 -070071 struct ib_mad *mad);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
73 struct ib_mad_private *mad);
74static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv);
David Howellsc4028952006-11-22 14:57:56 +000075static void timeout_sends(struct work_struct *work);
76static void local_completions(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
78 struct ib_mad_agent_private *agent_priv,
79 u8 mgmt_class);
80static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
81 struct ib_mad_agent_private *agent_priv);
82
83/*
84 * Returns a ib_mad_port_private structure or NULL for a device/port
85 * Assumes ib_mad_port_list_lock is being held
86 */
87static inline struct ib_mad_port_private *
88__ib_get_mad_port(struct ib_device *device, int port_num)
89{
90 struct ib_mad_port_private *entry;
91
92 list_for_each_entry(entry, &ib_mad_port_list, port_list) {
93 if (entry->device == device && entry->port_num == port_num)
94 return entry;
95 }
96 return NULL;
97}
98
99/*
100 * Wrapper function to return a ib_mad_port_private structure or NULL
101 * for a device/port
102 */
103static inline struct ib_mad_port_private *
104ib_get_mad_port(struct ib_device *device, int port_num)
105{
106 struct ib_mad_port_private *entry;
107 unsigned long flags;
108
109 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
110 entry = __ib_get_mad_port(device, port_num);
111 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
112
113 return entry;
114}
115
116static inline u8 convert_mgmt_class(u8 mgmt_class)
117{
118 /* Alias IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE to 0 */
119 return mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE ?
120 0 : mgmt_class;
121}
122
123static int get_spl_qp_index(enum ib_qp_type qp_type)
124{
125 switch (qp_type)
126 {
127 case IB_QPT_SMI:
128 return 0;
129 case IB_QPT_GSI:
130 return 1;
131 default:
132 return -1;
133 }
134}
135
136static int vendor_class_index(u8 mgmt_class)
137{
138 return mgmt_class - IB_MGMT_CLASS_VENDOR_RANGE2_START;
139}
140
141static int is_vendor_class(u8 mgmt_class)
142{
143 if ((mgmt_class < IB_MGMT_CLASS_VENDOR_RANGE2_START) ||
144 (mgmt_class > IB_MGMT_CLASS_VENDOR_RANGE2_END))
145 return 0;
146 return 1;
147}
148
149static int is_vendor_oui(char *oui)
150{
151 if (oui[0] || oui[1] || oui[2])
152 return 1;
153 return 0;
154}
155
156static int is_vendor_method_in_use(
157 struct ib_mad_mgmt_vendor_class *vendor_class,
158 struct ib_mad_reg_req *mad_reg_req)
159{
160 struct ib_mad_mgmt_method_table *method;
161 int i;
162
163 for (i = 0; i < MAX_MGMT_OUI; i++) {
164 if (!memcmp(vendor_class->oui[i], mad_reg_req->oui, 3)) {
165 method = vendor_class->method_table[i];
166 if (method) {
167 if (method_in_use(&method, mad_reg_req))
168 return 1;
169 else
170 break;
171 }
172 }
173 }
174 return 0;
175}
176
Sean Hefty2527e682006-07-20 11:25:50 +0300177int ib_response_mad(struct ib_mad *mad)
178{
179 return ((mad->mad_hdr.method & IB_MGMT_METHOD_RESP) ||
180 (mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS) ||
181 ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_BM) &&
182 (mad->mad_hdr.attr_mod & IB_BM_ATTR_MOD_RESP)));
183}
184EXPORT_SYMBOL(ib_response_mad);
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/*
187 * ib_register_mad_agent - Register to send/receive MADs
188 */
189struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
190 u8 port_num,
191 enum ib_qp_type qp_type,
192 struct ib_mad_reg_req *mad_reg_req,
193 u8 rmpp_version,
194 ib_mad_send_handler send_handler,
195 ib_mad_recv_handler recv_handler,
196 void *context)
197{
198 struct ib_mad_port_private *port_priv;
199 struct ib_mad_agent *ret = ERR_PTR(-EINVAL);
200 struct ib_mad_agent_private *mad_agent_priv;
201 struct ib_mad_reg_req *reg_req = NULL;
202 struct ib_mad_mgmt_class_table *class;
203 struct ib_mad_mgmt_vendor_class_table *vendor;
204 struct ib_mad_mgmt_vendor_class *vendor_class;
205 struct ib_mad_mgmt_method_table *method;
206 int ret2, qpn;
207 unsigned long flags;
208 u8 mgmt_class, vclass;
209
210 /* Validate parameters */
211 qpn = get_spl_qp_index(qp_type);
212 if (qpn == -1)
213 goto error1;
214
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700215 if (rmpp_version && rmpp_version != IB_MGMT_RMPP_VERSION)
216 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 /* Validate MAD registration request if supplied */
219 if (mad_reg_req) {
220 if (mad_reg_req->mgmt_class_version >= MAX_MGMT_VERSION)
221 goto error1;
222 if (!recv_handler)
223 goto error1;
224 if (mad_reg_req->mgmt_class >= MAX_MGMT_CLASS) {
225 /*
226 * IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE is the only
227 * one in this range currently allowed
228 */
229 if (mad_reg_req->mgmt_class !=
230 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
231 goto error1;
232 } else if (mad_reg_req->mgmt_class == 0) {
233 /*
234 * Class 0 is reserved in IBA and is used for
235 * aliasing of IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
236 */
237 goto error1;
238 } else if (is_vendor_class(mad_reg_req->mgmt_class)) {
239 /*
240 * If class is in "new" vendor range,
241 * ensure supplied OUI is not zero
242 */
243 if (!is_vendor_oui(mad_reg_req->oui))
244 goto error1;
245 }
Hal Rosenstock618a3c02006-03-28 16:40:04 -0800246 /* Make sure class supplied is consistent with RMPP */
Hal Rosenstock64cb9c62006-04-12 21:29:10 -0400247 if (!ib_is_mad_class_rmpp(mad_reg_req->mgmt_class)) {
Hal Rosenstock618a3c02006-03-28 16:40:04 -0800248 if (rmpp_version)
249 goto error1;
250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 /* Make sure class supplied is consistent with QP type */
252 if (qp_type == IB_QPT_SMI) {
253 if ((mad_reg_req->mgmt_class !=
254 IB_MGMT_CLASS_SUBN_LID_ROUTED) &&
255 (mad_reg_req->mgmt_class !=
256 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE))
257 goto error1;
258 } else {
259 if ((mad_reg_req->mgmt_class ==
260 IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
261 (mad_reg_req->mgmt_class ==
262 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE))
263 goto error1;
264 }
265 } else {
266 /* No registration request supplied */
267 if (!send_handler)
268 goto error1;
269 }
270
271 /* Validate device and port */
272 port_priv = ib_get_mad_port(device, port_num);
273 if (!port_priv) {
274 ret = ERR_PTR(-ENODEV);
275 goto error1;
276 }
277
278 /* Allocate structures */
Roland Dreierde6eb662005-11-02 07:23:14 -0800279 mad_agent_priv = kzalloc(sizeof *mad_agent_priv, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (!mad_agent_priv) {
281 ret = ERR_PTR(-ENOMEM);
282 goto error1;
283 }
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700284
285 mad_agent_priv->agent.mr = ib_get_dma_mr(port_priv->qp_info[qpn].qp->pd,
286 IB_ACCESS_LOCAL_WRITE);
287 if (IS_ERR(mad_agent_priv->agent.mr)) {
288 ret = ERR_PTR(-ENOMEM);
289 goto error2;
290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 if (mad_reg_req) {
293 reg_req = kmalloc(sizeof *reg_req, GFP_KERNEL);
294 if (!reg_req) {
295 ret = ERR_PTR(-ENOMEM);
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700296 goto error3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298 /* Make a copy of the MAD registration request */
299 memcpy(reg_req, mad_reg_req, sizeof *reg_req);
300 }
301
302 /* Now, fill in the various structures */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 mad_agent_priv->qp_info = &port_priv->qp_info[qpn];
304 mad_agent_priv->reg_req = reg_req;
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700305 mad_agent_priv->agent.rmpp_version = rmpp_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 mad_agent_priv->agent.device = device;
307 mad_agent_priv->agent.recv_handler = recv_handler;
308 mad_agent_priv->agent.send_handler = send_handler;
309 mad_agent_priv->agent.context = context;
310 mad_agent_priv->agent.qp = port_priv->qp_info[qpn].qp;
311 mad_agent_priv->agent.port_num = port_num;
Ralph Campbelld9620a42009-02-27 14:44:32 -0800312 spin_lock_init(&mad_agent_priv->lock);
313 INIT_LIST_HEAD(&mad_agent_priv->send_list);
314 INIT_LIST_HEAD(&mad_agent_priv->wait_list);
315 INIT_LIST_HEAD(&mad_agent_priv->done_list);
316 INIT_LIST_HEAD(&mad_agent_priv->rmpp_list);
317 INIT_DELAYED_WORK(&mad_agent_priv->timed_work, timeout_sends);
318 INIT_LIST_HEAD(&mad_agent_priv->local_list);
319 INIT_WORK(&mad_agent_priv->local_work, local_completions);
320 atomic_set(&mad_agent_priv->refcount, 1);
321 init_completion(&mad_agent_priv->comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 spin_lock_irqsave(&port_priv->reg_lock, flags);
324 mad_agent_priv->agent.hi_tid = ++ib_mad_client_id;
325
326 /*
327 * Make sure MAD registration (if supplied)
328 * is non overlapping with any existing ones
329 */
330 if (mad_reg_req) {
331 mgmt_class = convert_mgmt_class(mad_reg_req->mgmt_class);
332 if (!is_vendor_class(mgmt_class)) {
333 class = port_priv->version[mad_reg_req->
334 mgmt_class_version].class;
335 if (class) {
336 method = class->method_table[mgmt_class];
337 if (method) {
338 if (method_in_use(&method,
339 mad_reg_req))
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700340 goto error4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342 }
343 ret2 = add_nonoui_reg_req(mad_reg_req, mad_agent_priv,
344 mgmt_class);
345 } else {
346 /* "New" vendor class range */
347 vendor = port_priv->version[mad_reg_req->
348 mgmt_class_version].vendor;
349 if (vendor) {
350 vclass = vendor_class_index(mgmt_class);
351 vendor_class = vendor->vendor_class[vclass];
352 if (vendor_class) {
353 if (is_vendor_method_in_use(
354 vendor_class,
355 mad_reg_req))
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700356 goto error4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358 }
359 ret2 = add_oui_reg_req(mad_reg_req, mad_agent_priv);
360 }
361 if (ret2) {
362 ret = ERR_PTR(ret2);
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700363 goto error4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365 }
366
367 /* Add mad agent into port's agent list */
368 list_add_tail(&mad_agent_priv->agent_list, &port_priv->agent_list);
369 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return &mad_agent_priv->agent;
372
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700373error4:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
375 kfree(reg_req);
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700376error3:
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700377 ib_dereg_mr(mad_agent_priv->agent.mr);
Adrian Bunk2012a112005-11-27 00:37:36 +0100378error2:
379 kfree(mad_agent_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380error1:
381 return ret;
382}
383EXPORT_SYMBOL(ib_register_mad_agent);
384
385static inline int is_snooping_sends(int mad_snoop_flags)
386{
387 return (mad_snoop_flags &
388 (/*IB_MAD_SNOOP_POSTED_SENDS |
389 IB_MAD_SNOOP_RMPP_SENDS |*/
390 IB_MAD_SNOOP_SEND_COMPLETIONS /*|
391 IB_MAD_SNOOP_RMPP_SEND_COMPLETIONS*/));
392}
393
394static inline int is_snooping_recvs(int mad_snoop_flags)
395{
396 return (mad_snoop_flags &
397 (IB_MAD_SNOOP_RECVS /*|
398 IB_MAD_SNOOP_RMPP_RECVS*/));
399}
400
401static int register_snoop_agent(struct ib_mad_qp_info *qp_info,
402 struct ib_mad_snoop_private *mad_snoop_priv)
403{
404 struct ib_mad_snoop_private **new_snoop_table;
405 unsigned long flags;
406 int i;
407
408 spin_lock_irqsave(&qp_info->snoop_lock, flags);
409 /* Check for empty slot in array. */
410 for (i = 0; i < qp_info->snoop_table_size; i++)
411 if (!qp_info->snoop_table[i])
412 break;
413
414 if (i == qp_info->snoop_table_size) {
415 /* Grow table. */
Roland Dreier52805172008-10-14 14:05:36 -0700416 new_snoop_table = krealloc(qp_info->snoop_table,
417 sizeof mad_snoop_priv *
418 (qp_info->snoop_table_size + 1),
419 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (!new_snoop_table) {
421 i = -ENOMEM;
422 goto out;
423 }
Roland Dreier52805172008-10-14 14:05:36 -0700424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 qp_info->snoop_table = new_snoop_table;
426 qp_info->snoop_table_size++;
427 }
428 qp_info->snoop_table[i] = mad_snoop_priv;
429 atomic_inc(&qp_info->snoop_count);
430out:
431 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
432 return i;
433}
434
435struct ib_mad_agent *ib_register_mad_snoop(struct ib_device *device,
436 u8 port_num,
437 enum ib_qp_type qp_type,
438 int mad_snoop_flags,
439 ib_mad_snoop_handler snoop_handler,
440 ib_mad_recv_handler recv_handler,
441 void *context)
442{
443 struct ib_mad_port_private *port_priv;
444 struct ib_mad_agent *ret;
445 struct ib_mad_snoop_private *mad_snoop_priv;
446 int qpn;
447
448 /* Validate parameters */
449 if ((is_snooping_sends(mad_snoop_flags) && !snoop_handler) ||
450 (is_snooping_recvs(mad_snoop_flags) && !recv_handler)) {
451 ret = ERR_PTR(-EINVAL);
452 goto error1;
453 }
454 qpn = get_spl_qp_index(qp_type);
455 if (qpn == -1) {
456 ret = ERR_PTR(-EINVAL);
457 goto error1;
458 }
459 port_priv = ib_get_mad_port(device, port_num);
460 if (!port_priv) {
461 ret = ERR_PTR(-ENODEV);
462 goto error1;
463 }
464 /* Allocate structures */
Roland Dreierde6eb662005-11-02 07:23:14 -0800465 mad_snoop_priv = kzalloc(sizeof *mad_snoop_priv, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (!mad_snoop_priv) {
467 ret = ERR_PTR(-ENOMEM);
468 goto error1;
469 }
470
471 /* Now, fill in the various structures */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 mad_snoop_priv->qp_info = &port_priv->qp_info[qpn];
473 mad_snoop_priv->agent.device = device;
474 mad_snoop_priv->agent.recv_handler = recv_handler;
475 mad_snoop_priv->agent.snoop_handler = snoop_handler;
476 mad_snoop_priv->agent.context = context;
477 mad_snoop_priv->agent.qp = port_priv->qp_info[qpn].qp;
478 mad_snoop_priv->agent.port_num = port_num;
479 mad_snoop_priv->mad_snoop_flags = mad_snoop_flags;
Sean Hefty1b52fa982006-05-12 14:57:52 -0700480 init_completion(&mad_snoop_priv->comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 mad_snoop_priv->snoop_index = register_snoop_agent(
482 &port_priv->qp_info[qpn],
483 mad_snoop_priv);
484 if (mad_snoop_priv->snoop_index < 0) {
485 ret = ERR_PTR(mad_snoop_priv->snoop_index);
486 goto error2;
487 }
488
489 atomic_set(&mad_snoop_priv->refcount, 1);
490 return &mad_snoop_priv->agent;
491
492error2:
493 kfree(mad_snoop_priv);
494error1:
495 return ret;
496}
497EXPORT_SYMBOL(ib_register_mad_snoop);
498
Sean Hefty1b52fa982006-05-12 14:57:52 -0700499static inline void deref_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
500{
501 if (atomic_dec_and_test(&mad_agent_priv->refcount))
502 complete(&mad_agent_priv->comp);
503}
504
505static inline void deref_snoop_agent(struct ib_mad_snoop_private *mad_snoop_priv)
506{
507 if (atomic_dec_and_test(&mad_snoop_priv->refcount))
508 complete(&mad_snoop_priv->comp);
509}
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511static void unregister_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
512{
513 struct ib_mad_port_private *port_priv;
514 unsigned long flags;
515
516 /* Note that we could still be handling received MADs */
517
518 /*
519 * Canceling all sends results in dropping received response
520 * MADs, preventing us from queuing additional work
521 */
522 cancel_mads(mad_agent_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 port_priv = mad_agent_priv->qp_info->port_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 cancel_delayed_work(&mad_agent_priv->timed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 spin_lock_irqsave(&port_priv->reg_lock, flags);
527 remove_mad_reg_req(mad_agent_priv);
528 list_del(&mad_agent_priv->agent_list);
529 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
530
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700531 flush_workqueue(port_priv->wq);
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700532 ib_cancel_rmpp_recvs(mad_agent_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Sean Hefty1b52fa982006-05-12 14:57:52 -0700534 deref_mad_agent(mad_agent_priv);
535 wait_for_completion(&mad_agent_priv->comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Jesper Juhl6044ec82005-11-07 01:01:32 -0800537 kfree(mad_agent_priv->reg_req);
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700538 ib_dereg_mr(mad_agent_priv->agent.mr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 kfree(mad_agent_priv);
540}
541
542static void unregister_mad_snoop(struct ib_mad_snoop_private *mad_snoop_priv)
543{
544 struct ib_mad_qp_info *qp_info;
545 unsigned long flags;
546
547 qp_info = mad_snoop_priv->qp_info;
548 spin_lock_irqsave(&qp_info->snoop_lock, flags);
549 qp_info->snoop_table[mad_snoop_priv->snoop_index] = NULL;
550 atomic_dec(&qp_info->snoop_count);
551 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
552
Sean Hefty1b52fa982006-05-12 14:57:52 -0700553 deref_snoop_agent(mad_snoop_priv);
554 wait_for_completion(&mad_snoop_priv->comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 kfree(mad_snoop_priv);
557}
558
559/*
560 * ib_unregister_mad_agent - Unregisters a client from using MAD services
561 */
562int ib_unregister_mad_agent(struct ib_mad_agent *mad_agent)
563{
564 struct ib_mad_agent_private *mad_agent_priv;
565 struct ib_mad_snoop_private *mad_snoop_priv;
566
567 /* If the TID is zero, the agent can only snoop. */
568 if (mad_agent->hi_tid) {
569 mad_agent_priv = container_of(mad_agent,
570 struct ib_mad_agent_private,
571 agent);
572 unregister_mad_agent(mad_agent_priv);
573 } else {
574 mad_snoop_priv = container_of(mad_agent,
575 struct ib_mad_snoop_private,
576 agent);
577 unregister_mad_snoop(mad_snoop_priv);
578 }
579 return 0;
580}
581EXPORT_SYMBOL(ib_unregister_mad_agent);
582
583static void dequeue_mad(struct ib_mad_list_head *mad_list)
584{
585 struct ib_mad_queue *mad_queue;
586 unsigned long flags;
587
588 BUG_ON(!mad_list->mad_queue);
589 mad_queue = mad_list->mad_queue;
590 spin_lock_irqsave(&mad_queue->lock, flags);
591 list_del(&mad_list->list);
592 mad_queue->count--;
593 spin_unlock_irqrestore(&mad_queue->lock, flags);
594}
595
596static void snoop_send(struct ib_mad_qp_info *qp_info,
Sean Hefty34816ad2005-10-25 10:51:39 -0700597 struct ib_mad_send_buf *send_buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 struct ib_mad_send_wc *mad_send_wc,
599 int mad_snoop_flags)
600{
601 struct ib_mad_snoop_private *mad_snoop_priv;
602 unsigned long flags;
603 int i;
604
605 spin_lock_irqsave(&qp_info->snoop_lock, flags);
606 for (i = 0; i < qp_info->snoop_table_size; i++) {
607 mad_snoop_priv = qp_info->snoop_table[i];
608 if (!mad_snoop_priv ||
609 !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
610 continue;
611
612 atomic_inc(&mad_snoop_priv->refcount);
613 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
614 mad_snoop_priv->agent.snoop_handler(&mad_snoop_priv->agent,
Sean Hefty34816ad2005-10-25 10:51:39 -0700615 send_buf, mad_send_wc);
Sean Hefty1b52fa982006-05-12 14:57:52 -0700616 deref_snoop_agent(mad_snoop_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 spin_lock_irqsave(&qp_info->snoop_lock, flags);
618 }
619 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
620}
621
622static void snoop_recv(struct ib_mad_qp_info *qp_info,
623 struct ib_mad_recv_wc *mad_recv_wc,
624 int mad_snoop_flags)
625{
626 struct ib_mad_snoop_private *mad_snoop_priv;
627 unsigned long flags;
628 int i;
629
630 spin_lock_irqsave(&qp_info->snoop_lock, flags);
631 for (i = 0; i < qp_info->snoop_table_size; i++) {
632 mad_snoop_priv = qp_info->snoop_table[i];
633 if (!mad_snoop_priv ||
634 !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
635 continue;
636
637 atomic_inc(&mad_snoop_priv->refcount);
638 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
639 mad_snoop_priv->agent.recv_handler(&mad_snoop_priv->agent,
640 mad_recv_wc);
Sean Hefty1b52fa982006-05-12 14:57:52 -0700641 deref_snoop_agent(mad_snoop_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 spin_lock_irqsave(&qp_info->snoop_lock, flags);
643 }
644 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
645}
646
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +0200647static void build_smp_wc(struct ib_qp *qp,
648 u64 wr_id, u16 slid, u16 pkey_index, u8 port_num,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 struct ib_wc *wc)
650{
651 memset(wc, 0, sizeof *wc);
652 wc->wr_id = wr_id;
653 wc->status = IB_WC_SUCCESS;
654 wc->opcode = IB_WC_RECV;
655 wc->pkey_index = pkey_index;
656 wc->byte_len = sizeof(struct ib_mad) + sizeof(struct ib_grh);
657 wc->src_qp = IB_QP0;
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +0200658 wc->qp = qp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 wc->slid = slid;
660 wc->sl = 0;
661 wc->dlid_path_bits = 0;
662 wc->port_num = port_num;
663}
664
665/*
666 * Return 0 if SMP is to be sent
667 * Return 1 if SMP was consumed locally (whether or not solicited)
668 * Return < 0 if error
669 */
670static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
Sean Hefty34816ad2005-10-25 10:51:39 -0700671 struct ib_mad_send_wr_private *mad_send_wr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Hal Rosenstockde493d42007-04-02 11:24:07 -0400673 int ret = 0;
Sean Hefty34816ad2005-10-25 10:51:39 -0700674 struct ib_smp *smp = mad_send_wr->send_buf.mad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 unsigned long flags;
676 struct ib_mad_local_private *local;
677 struct ib_mad_private *mad_priv;
678 struct ib_mad_port_private *port_priv;
679 struct ib_mad_agent_private *recv_mad_agent = NULL;
680 struct ib_device *device = mad_agent_priv->agent.device;
Hal Rosenstock1bae4db2007-05-14 17:21:52 -0400681 u8 port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 struct ib_wc mad_wc;
Sean Hefty34816ad2005-10-25 10:51:39 -0700683 struct ib_send_wr *send_wr = &mad_send_wr->send_wr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Hal Rosenstock1bae4db2007-05-14 17:21:52 -0400685 if (device->node_type == RDMA_NODE_IB_SWITCH &&
686 smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
687 port_num = send_wr->wr.ud.port_num;
688 else
689 port_num = mad_agent_priv->agent.port_num;
690
Ralph Campbell8cf3f042006-02-03 14:28:48 -0800691 /*
692 * Directed route handling starts if the initial LID routed part of
693 * a request or the ending LID routed part of a response is empty.
694 * If we are at the start of the LID routed part, don't update the
695 * hop_ptr or hop_cnt. See section 14.2.2, Vol 1 IB spec.
696 */
697 if ((ib_get_smp_direction(smp) ? smp->dr_dlid : smp->dr_slid) ==
698 IB_LID_PERMISSIVE &&
Hal Rosenstockde493d42007-04-02 11:24:07 -0400699 smi_handle_dr_smp_send(smp, device->node_type, port_num) ==
700 IB_SMI_DISCARD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 ret = -EINVAL;
702 printk(KERN_ERR PFX "Invalid directed route\n");
703 goto out;
704 }
Hal Rosenstockde493d42007-04-02 11:24:07 -0400705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 /* Check to post send on QP or process locally */
Steve Welch727792d2007-10-23 15:06:10 -0700707 if (smi_check_local_smp(smp, device) == IB_SMI_DISCARD &&
708 smi_check_local_returning_smp(smp, device) == IB_SMI_DISCARD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 goto out;
710
711 local = kmalloc(sizeof *local, GFP_ATOMIC);
712 if (!local) {
713 ret = -ENOMEM;
714 printk(KERN_ERR PFX "No memory for ib_mad_local_private\n");
715 goto out;
716 }
717 local->mad_priv = NULL;
718 local->recv_mad_agent = NULL;
719 mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_ATOMIC);
720 if (!mad_priv) {
721 ret = -ENOMEM;
722 printk(KERN_ERR PFX "No memory for local response MAD\n");
723 kfree(local);
724 goto out;
725 }
726
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +0200727 build_smp_wc(mad_agent_priv->agent.qp,
728 send_wr->wr_id, be16_to_cpu(smp->dr_slid),
Sean Hefty97f52eb2005-08-13 21:05:57 -0700729 send_wr->wr.ud.pkey_index,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 send_wr->wr.ud.port_num, &mad_wc);
731
732 /* No GRH for DR SMP */
733 ret = device->process_mad(device, 0, port_num, &mad_wc, NULL,
734 (struct ib_mad *)smp,
735 (struct ib_mad *)&mad_priv->mad);
736 switch (ret)
737 {
738 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY:
Sean Hefty2527e682006-07-20 11:25:50 +0300739 if (ib_response_mad(&mad_priv->mad.mad) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 mad_agent_priv->agent.recv_handler) {
741 local->mad_priv = mad_priv;
742 local->recv_mad_agent = mad_agent_priv;
743 /*
744 * Reference MAD agent until receive
745 * side of local completion handled
746 */
747 atomic_inc(&mad_agent_priv->refcount);
748 } else
749 kmem_cache_free(ib_mad_cache, mad_priv);
750 break;
751 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED:
752 kmem_cache_free(ib_mad_cache, mad_priv);
Ralph Campbell4780c192009-03-03 14:22:17 -0800753 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 case IB_MAD_RESULT_SUCCESS:
755 /* Treat like an incoming receive MAD */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 port_priv = ib_get_mad_port(mad_agent_priv->agent.device,
757 mad_agent_priv->agent.port_num);
758 if (port_priv) {
Steve Welch727792d2007-10-23 15:06:10 -0700759 memcpy(&mad_priv->mad.mad, smp, sizeof(struct ib_mad));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 recv_mad_agent = find_mad_agent(port_priv,
Hal Rosenstock4a0754f2005-07-27 11:45:24 -0700761 &mad_priv->mad.mad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
763 if (!port_priv || !recv_mad_agent) {
Ralph Campbell4780c192009-03-03 14:22:17 -0800764 /*
765 * No receiving agent so drop packet and
766 * generate send completion.
767 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 kmem_cache_free(ib_mad_cache, mad_priv);
Ralph Campbell4780c192009-03-03 14:22:17 -0800769 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
771 local->mad_priv = mad_priv;
772 local->recv_mad_agent = recv_mad_agent;
773 break;
774 default:
775 kmem_cache_free(ib_mad_cache, mad_priv);
776 kfree(local);
777 ret = -EINVAL;
778 goto out;
779 }
780
Sean Hefty34816ad2005-10-25 10:51:39 -0700781 local->mad_send_wr = mad_send_wr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 /* Reference MAD agent until send side of local completion handled */
783 atomic_inc(&mad_agent_priv->refcount);
784 /* Queue local completion to local list */
785 spin_lock_irqsave(&mad_agent_priv->lock, flags);
786 list_add_tail(&local->completion_list, &mad_agent_priv->local_list);
787 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
788 queue_work(mad_agent_priv->qp_info->port_priv->wq,
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700789 &mad_agent_priv->local_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 ret = 1;
791out:
792 return ret;
793}
794
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800795static int get_pad_size(int hdr_len, int data_len)
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700796{
797 int seg_size, pad;
798
799 seg_size = sizeof(struct ib_mad) - hdr_len;
800 if (data_len && seg_size) {
801 pad = seg_size - data_len % seg_size;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800802 return pad == seg_size ? 0 : pad;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700803 } else
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800804 return seg_size;
805}
806
807static void free_send_rmpp_list(struct ib_mad_send_wr_private *mad_send_wr)
808{
809 struct ib_rmpp_segment *s, *t;
810
811 list_for_each_entry_safe(s, t, &mad_send_wr->rmpp_list, list) {
812 list_del(&s->list);
813 kfree(s);
814 }
815}
816
817static int alloc_send_rmpp_list(struct ib_mad_send_wr_private *send_wr,
818 gfp_t gfp_mask)
819{
820 struct ib_mad_send_buf *send_buf = &send_wr->send_buf;
821 struct ib_rmpp_mad *rmpp_mad = send_buf->mad;
822 struct ib_rmpp_segment *seg = NULL;
823 int left, seg_size, pad;
824
825 send_buf->seg_size = sizeof (struct ib_mad) - send_buf->hdr_len;
826 seg_size = send_buf->seg_size;
827 pad = send_wr->pad;
828
829 /* Allocate data segments. */
830 for (left = send_buf->data_len + pad; left > 0; left -= seg_size) {
831 seg = kmalloc(sizeof (*seg) + seg_size, gfp_mask);
832 if (!seg) {
833 printk(KERN_ERR "alloc_send_rmpp_segs: RMPP mem "
834 "alloc failed for len %zd, gfp %#x\n",
835 sizeof (*seg) + seg_size, gfp_mask);
836 free_send_rmpp_list(send_wr);
837 return -ENOMEM;
838 }
839 seg->num = ++send_buf->seg_count;
840 list_add_tail(&seg->list, &send_wr->rmpp_list);
841 }
842
843 /* Zero any padding */
844 if (pad)
845 memset(seg->data + seg_size - pad, 0, pad);
846
847 rmpp_mad->rmpp_hdr.rmpp_version = send_wr->mad_agent_priv->
848 agent.rmpp_version;
849 rmpp_mad->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_DATA;
850 ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
851
852 send_wr->cur_seg = container_of(send_wr->rmpp_list.next,
853 struct ib_rmpp_segment, list);
854 send_wr->last_ack_seg = send_wr->cur_seg;
855 return 0;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700856}
857
858struct ib_mad_send_buf * ib_create_send_mad(struct ib_mad_agent *mad_agent,
859 u32 remote_qpn, u16 pkey_index,
Sean Hefty34816ad2005-10-25 10:51:39 -0700860 int rmpp_active,
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700861 int hdr_len, int data_len,
Al Virodd0fc662005-10-07 07:46:04 +0100862 gfp_t gfp_mask)
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700863{
864 struct ib_mad_agent_private *mad_agent_priv;
Sean Hefty34816ad2005-10-25 10:51:39 -0700865 struct ib_mad_send_wr_private *mad_send_wr;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800866 int pad, message_size, ret, size;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700867 void *buf;
868
Sean Hefty34816ad2005-10-25 10:51:39 -0700869 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
870 agent);
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800871 pad = get_pad_size(hdr_len, data_len);
872 message_size = hdr_len + data_len + pad;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700873
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700874 if ((!mad_agent->rmpp_version &&
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800875 (rmpp_active || message_size > sizeof(struct ib_mad))) ||
876 (!rmpp_active && message_size > sizeof(struct ib_mad)))
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700877 return ERR_PTR(-EINVAL);
878
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800879 size = rmpp_active ? hdr_len : sizeof(struct ib_mad);
880 buf = kzalloc(sizeof *mad_send_wr + size, gfp_mask);
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700881 if (!buf)
882 return ERR_PTR(-ENOMEM);
883
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800884 mad_send_wr = buf + size;
885 INIT_LIST_HEAD(&mad_send_wr->rmpp_list);
Sean Hefty34816ad2005-10-25 10:51:39 -0700886 mad_send_wr->send_buf.mad = buf;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800887 mad_send_wr->send_buf.hdr_len = hdr_len;
888 mad_send_wr->send_buf.data_len = data_len;
889 mad_send_wr->pad = pad;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700890
Sean Hefty34816ad2005-10-25 10:51:39 -0700891 mad_send_wr->mad_agent_priv = mad_agent_priv;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800892 mad_send_wr->sg_list[0].length = hdr_len;
Sean Hefty34816ad2005-10-25 10:51:39 -0700893 mad_send_wr->sg_list[0].lkey = mad_agent->mr->lkey;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800894 mad_send_wr->sg_list[1].length = sizeof(struct ib_mad) - hdr_len;
895 mad_send_wr->sg_list[1].lkey = mad_agent->mr->lkey;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700896
Sean Hefty34816ad2005-10-25 10:51:39 -0700897 mad_send_wr->send_wr.wr_id = (unsigned long) mad_send_wr;
898 mad_send_wr->send_wr.sg_list = mad_send_wr->sg_list;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800899 mad_send_wr->send_wr.num_sge = 2;
Sean Hefty34816ad2005-10-25 10:51:39 -0700900 mad_send_wr->send_wr.opcode = IB_WR_SEND;
901 mad_send_wr->send_wr.send_flags = IB_SEND_SIGNALED;
902 mad_send_wr->send_wr.wr.ud.remote_qpn = remote_qpn;
903 mad_send_wr->send_wr.wr.ud.remote_qkey = IB_QP_SET_QKEY;
904 mad_send_wr->send_wr.wr.ud.pkey_index = pkey_index;
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700905
906 if (rmpp_active) {
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800907 ret = alloc_send_rmpp_list(mad_send_wr, gfp_mask);
908 if (ret) {
909 kfree(buf);
910 return ERR_PTR(ret);
911 }
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700912 }
913
Sean Hefty34816ad2005-10-25 10:51:39 -0700914 mad_send_wr->send_buf.mad_agent = mad_agent;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700915 atomic_inc(&mad_agent_priv->refcount);
Sean Hefty34816ad2005-10-25 10:51:39 -0700916 return &mad_send_wr->send_buf;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700917}
918EXPORT_SYMBOL(ib_create_send_mad);
919
Hal Rosenstock618a3c02006-03-28 16:40:04 -0800920int ib_get_mad_data_offset(u8 mgmt_class)
921{
922 if (mgmt_class == IB_MGMT_CLASS_SUBN_ADM)
923 return IB_MGMT_SA_HDR;
924 else if ((mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
925 (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
926 (mgmt_class == IB_MGMT_CLASS_BIS))
927 return IB_MGMT_DEVICE_HDR;
928 else if ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
929 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END))
930 return IB_MGMT_VENDOR_HDR;
931 else
932 return IB_MGMT_MAD_HDR;
933}
934EXPORT_SYMBOL(ib_get_mad_data_offset);
935
936int ib_is_mad_class_rmpp(u8 mgmt_class)
937{
938 if ((mgmt_class == IB_MGMT_CLASS_SUBN_ADM) ||
939 (mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
940 (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
941 (mgmt_class == IB_MGMT_CLASS_BIS) ||
942 ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
943 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END)))
944 return 1;
945 return 0;
946}
947EXPORT_SYMBOL(ib_is_mad_class_rmpp);
948
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800949void *ib_get_rmpp_segment(struct ib_mad_send_buf *send_buf, int seg_num)
950{
951 struct ib_mad_send_wr_private *mad_send_wr;
952 struct list_head *list;
953
954 mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
955 send_buf);
956 list = &mad_send_wr->cur_seg->list;
957
958 if (mad_send_wr->cur_seg->num < seg_num) {
959 list_for_each_entry(mad_send_wr->cur_seg, list, list)
960 if (mad_send_wr->cur_seg->num == seg_num)
961 break;
962 } else if (mad_send_wr->cur_seg->num > seg_num) {
963 list_for_each_entry_reverse(mad_send_wr->cur_seg, list, list)
964 if (mad_send_wr->cur_seg->num == seg_num)
965 break;
966 }
967 return mad_send_wr->cur_seg->data;
968}
969EXPORT_SYMBOL(ib_get_rmpp_segment);
970
971static inline void *ib_get_payload(struct ib_mad_send_wr_private *mad_send_wr)
972{
973 if (mad_send_wr->send_buf.seg_count)
974 return ib_get_rmpp_segment(&mad_send_wr->send_buf,
975 mad_send_wr->seg_num);
976 else
977 return mad_send_wr->send_buf.mad +
978 mad_send_wr->send_buf.hdr_len;
979}
980
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700981void ib_free_send_mad(struct ib_mad_send_buf *send_buf)
982{
983 struct ib_mad_agent_private *mad_agent_priv;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800984 struct ib_mad_send_wr_private *mad_send_wr;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700985
986 mad_agent_priv = container_of(send_buf->mad_agent,
987 struct ib_mad_agent_private, agent);
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800988 mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
989 send_buf);
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700990
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800991 free_send_rmpp_list(mad_send_wr);
992 kfree(send_buf->mad);
Sean Hefty1b52fa982006-05-12 14:57:52 -0700993 deref_mad_agent(mad_agent_priv);
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700994}
995EXPORT_SYMBOL(ib_free_send_mad);
996
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700997int ib_send_mad(struct ib_mad_send_wr_private *mad_send_wr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 struct ib_mad_qp_info *qp_info;
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07001000 struct list_head *list;
Sean Hefty34816ad2005-10-25 10:51:39 -07001001 struct ib_send_wr *bad_send_wr;
1002 struct ib_mad_agent *mad_agent;
1003 struct ib_sge *sge;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 unsigned long flags;
1005 int ret;
1006
Hal Rosenstockf8197a42005-07-27 11:45:24 -07001007 /* Set WR ID to find mad_send_wr upon completion */
Hal Rosenstockd760ce82005-07-27 11:45:25 -07001008 qp_info = mad_send_wr->mad_agent_priv->qp_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 mad_send_wr->send_wr.wr_id = (unsigned long)&mad_send_wr->mad_list;
1010 mad_send_wr->mad_list.mad_queue = &qp_info->send_queue;
1011
Sean Hefty34816ad2005-10-25 10:51:39 -07001012 mad_agent = mad_send_wr->send_buf.mad_agent;
1013 sge = mad_send_wr->sg_list;
Ralph Campbell15271062006-12-12 14:28:30 -08001014 sge[0].addr = ib_dma_map_single(mad_agent->device,
1015 mad_send_wr->send_buf.mad,
1016 sge[0].length,
1017 DMA_TO_DEVICE);
1018 mad_send_wr->header_mapping = sge[0].addr;
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001019
Ralph Campbell15271062006-12-12 14:28:30 -08001020 sge[1].addr = ib_dma_map_single(mad_agent->device,
1021 ib_get_payload(mad_send_wr),
1022 sge[1].length,
1023 DMA_TO_DEVICE);
1024 mad_send_wr->payload_mapping = sge[1].addr;
Sean Hefty34816ad2005-10-25 10:51:39 -07001025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 spin_lock_irqsave(&qp_info->send_queue.lock, flags);
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07001027 if (qp_info->send_queue.count < qp_info->send_queue.max_active) {
Sean Hefty34816ad2005-10-25 10:51:39 -07001028 ret = ib_post_send(mad_agent->qp, &mad_send_wr->send_wr,
1029 &bad_send_wr);
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07001030 list = &qp_info->send_queue.list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 ret = 0;
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07001033 list = &qp_info->overflow_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07001035
1036 if (!ret) {
1037 qp_info->send_queue.count++;
1038 list_add_tail(&mad_send_wr->mad_list.list, list);
1039 }
1040 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001041 if (ret) {
Ralph Campbell15271062006-12-12 14:28:30 -08001042 ib_dma_unmap_single(mad_agent->device,
1043 mad_send_wr->header_mapping,
1044 sge[0].length, DMA_TO_DEVICE);
1045 ib_dma_unmap_single(mad_agent->device,
1046 mad_send_wr->payload_mapping,
1047 sge[1].length, DMA_TO_DEVICE);
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return ret;
1050}
1051
1052/*
1053 * ib_post_send_mad - Posts MAD(s) to the send queue of the QP associated
1054 * with the registered client
1055 */
Sean Hefty34816ad2005-10-25 10:51:39 -07001056int ib_post_send_mad(struct ib_mad_send_buf *send_buf,
1057 struct ib_mad_send_buf **bad_send_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 struct ib_mad_agent_private *mad_agent_priv;
Sean Hefty34816ad2005-10-25 10:51:39 -07001060 struct ib_mad_send_buf *next_send_buf;
1061 struct ib_mad_send_wr_private *mad_send_wr;
1062 unsigned long flags;
1063 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 /* Walk list of send WRs and post each on send list */
Sean Hefty34816ad2005-10-25 10:51:39 -07001066 for (; send_buf; send_buf = next_send_buf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Sean Hefty34816ad2005-10-25 10:51:39 -07001068 mad_send_wr = container_of(send_buf,
1069 struct ib_mad_send_wr_private,
1070 send_buf);
1071 mad_agent_priv = mad_send_wr->mad_agent_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Sean Hefty34816ad2005-10-25 10:51:39 -07001073 if (!send_buf->mad_agent->send_handler ||
1074 (send_buf->timeout_ms &&
1075 !send_buf->mad_agent->recv_handler)) {
1076 ret = -EINVAL;
1077 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 }
1079
Hal Rosenstock618a3c02006-03-28 16:40:04 -08001080 if (!ib_is_mad_class_rmpp(((struct ib_mad_hdr *) send_buf->mad)->mgmt_class)) {
1081 if (mad_agent_priv->agent.rmpp_version) {
1082 ret = -EINVAL;
1083 goto error;
1084 }
1085 }
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 /*
1088 * Save pointer to next work request to post in case the
1089 * current one completes, and the user modifies the work
1090 * request associated with the completion
1091 */
Sean Hefty34816ad2005-10-25 10:51:39 -07001092 next_send_buf = send_buf->next;
1093 mad_send_wr->send_wr.wr.ud.ah = send_buf->ah;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Sean Hefty34816ad2005-10-25 10:51:39 -07001095 if (((struct ib_mad_hdr *) send_buf->mad)->mgmt_class ==
1096 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
1097 ret = handle_outgoing_dr_smp(mad_agent_priv,
1098 mad_send_wr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 if (ret < 0) /* error */
Sean Hefty34816ad2005-10-25 10:51:39 -07001100 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 else if (ret == 1) /* locally consumed */
Sean Hefty34816ad2005-10-25 10:51:39 -07001102 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
1104
Sean Hefty34816ad2005-10-25 10:51:39 -07001105 mad_send_wr->tid = ((struct ib_mad_hdr *) send_buf->mad)->tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 /* Timeout will be updated after send completes */
Sean Hefty34816ad2005-10-25 10:51:39 -07001107 mad_send_wr->timeout = msecs_to_jiffies(send_buf->timeout_ms);
Sean Hefty4fc8cd42007-11-27 00:11:04 -08001108 mad_send_wr->max_retries = send_buf->retries;
1109 mad_send_wr->retries_left = send_buf->retries;
1110 send_buf->retries = 0;
Sean Hefty34816ad2005-10-25 10:51:39 -07001111 /* Reference for work request to QP + response */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 mad_send_wr->refcount = 1 + (mad_send_wr->timeout > 0);
1113 mad_send_wr->status = IB_WC_SUCCESS;
1114
1115 /* Reference MAD agent until send completes */
1116 atomic_inc(&mad_agent_priv->refcount);
1117 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1118 list_add_tail(&mad_send_wr->agent_list,
1119 &mad_agent_priv->send_list);
1120 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1121
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001122 if (mad_agent_priv->agent.rmpp_version) {
1123 ret = ib_send_rmpp_mad(mad_send_wr);
1124 if (ret >= 0 && ret != IB_RMPP_RESULT_CONSUMED)
1125 ret = ib_send_mad(mad_send_wr);
1126 } else
1127 ret = ib_send_mad(mad_send_wr);
1128 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 /* Fail send request */
1130 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1131 list_del(&mad_send_wr->agent_list);
1132 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1133 atomic_dec(&mad_agent_priv->refcount);
Sean Hefty34816ad2005-10-25 10:51:39 -07001134 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 }
1137 return 0;
Sean Hefty34816ad2005-10-25 10:51:39 -07001138error:
1139 if (bad_send_buf)
1140 *bad_send_buf = send_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return ret;
1142}
1143EXPORT_SYMBOL(ib_post_send_mad);
1144
1145/*
1146 * ib_free_recv_mad - Returns data buffers used to receive
1147 * a MAD to the access layer
1148 */
1149void ib_free_recv_mad(struct ib_mad_recv_wc *mad_recv_wc)
1150{
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001151 struct ib_mad_recv_buf *mad_recv_buf, *temp_recv_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 struct ib_mad_private_header *mad_priv_hdr;
1153 struct ib_mad_private *priv;
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001154 struct list_head free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001156 INIT_LIST_HEAD(&free_list);
1157 list_splice_init(&mad_recv_wc->rmpp_list, &free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001159 list_for_each_entry_safe(mad_recv_buf, temp_recv_buf,
1160 &free_list, list) {
1161 mad_recv_wc = container_of(mad_recv_buf, struct ib_mad_recv_wc,
1162 recv_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 mad_priv_hdr = container_of(mad_recv_wc,
1164 struct ib_mad_private_header,
1165 recv_wc);
1166 priv = container_of(mad_priv_hdr, struct ib_mad_private,
1167 header);
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001168 kmem_cache_free(ib_mad_cache, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
1171EXPORT_SYMBOL(ib_free_recv_mad);
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173struct ib_mad_agent *ib_redirect_mad_qp(struct ib_qp *qp,
1174 u8 rmpp_version,
1175 ib_mad_send_handler send_handler,
1176 ib_mad_recv_handler recv_handler,
1177 void *context)
1178{
1179 return ERR_PTR(-EINVAL); /* XXX: for now */
1180}
1181EXPORT_SYMBOL(ib_redirect_mad_qp);
1182
1183int ib_process_mad_wc(struct ib_mad_agent *mad_agent,
1184 struct ib_wc *wc)
1185{
1186 printk(KERN_ERR PFX "ib_process_mad_wc() not implemented yet\n");
1187 return 0;
1188}
1189EXPORT_SYMBOL(ib_process_mad_wc);
1190
1191static int method_in_use(struct ib_mad_mgmt_method_table **method,
1192 struct ib_mad_reg_req *mad_reg_req)
1193{
1194 int i;
1195
1196 for (i = find_first_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS);
1197 i < IB_MGMT_MAX_METHODS;
1198 i = find_next_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS,
1199 1+i)) {
1200 if ((*method)->agent[i]) {
1201 printk(KERN_ERR PFX "Method %d already in use\n", i);
1202 return -EINVAL;
1203 }
1204 }
1205 return 0;
1206}
1207
1208static int allocate_method_table(struct ib_mad_mgmt_method_table **method)
1209{
1210 /* Allocate management method table */
Roland Dreierde6eb662005-11-02 07:23:14 -08001211 *method = kzalloc(sizeof **method, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 if (!*method) {
1213 printk(KERN_ERR PFX "No memory for "
1214 "ib_mad_mgmt_method_table\n");
1215 return -ENOMEM;
1216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 return 0;
1219}
1220
1221/*
1222 * Check to see if there are any methods still in use
1223 */
1224static int check_method_table(struct ib_mad_mgmt_method_table *method)
1225{
1226 int i;
1227
1228 for (i = 0; i < IB_MGMT_MAX_METHODS; i++)
1229 if (method->agent[i])
1230 return 1;
1231 return 0;
1232}
1233
1234/*
1235 * Check to see if there are any method tables for this class still in use
1236 */
1237static int check_class_table(struct ib_mad_mgmt_class_table *class)
1238{
1239 int i;
1240
1241 for (i = 0; i < MAX_MGMT_CLASS; i++)
1242 if (class->method_table[i])
1243 return 1;
1244 return 0;
1245}
1246
1247static int check_vendor_class(struct ib_mad_mgmt_vendor_class *vendor_class)
1248{
1249 int i;
1250
1251 for (i = 0; i < MAX_MGMT_OUI; i++)
1252 if (vendor_class->method_table[i])
1253 return 1;
1254 return 0;
1255}
1256
1257static int find_vendor_oui(struct ib_mad_mgmt_vendor_class *vendor_class,
1258 char *oui)
1259{
1260 int i;
1261
1262 for (i = 0; i < MAX_MGMT_OUI; i++)
Roland Dreier3cd96562006-09-22 15:22:46 -07001263 /* Is there matching OUI for this vendor class ? */
1264 if (!memcmp(vendor_class->oui[i], oui, 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 return i;
1266
1267 return -1;
1268}
1269
1270static int check_vendor_table(struct ib_mad_mgmt_vendor_class_table *vendor)
1271{
1272 int i;
1273
1274 for (i = 0; i < MAX_MGMT_VENDOR_RANGE2; i++)
1275 if (vendor->vendor_class[i])
1276 return 1;
1277
1278 return 0;
1279}
1280
1281static void remove_methods_mad_agent(struct ib_mad_mgmt_method_table *method,
1282 struct ib_mad_agent_private *agent)
1283{
1284 int i;
1285
1286 /* Remove any methods for this mad agent */
1287 for (i = 0; i < IB_MGMT_MAX_METHODS; i++) {
1288 if (method->agent[i] == agent) {
1289 method->agent[i] = NULL;
1290 }
1291 }
1292}
1293
1294static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1295 struct ib_mad_agent_private *agent_priv,
1296 u8 mgmt_class)
1297{
1298 struct ib_mad_port_private *port_priv;
1299 struct ib_mad_mgmt_class_table **class;
1300 struct ib_mad_mgmt_method_table **method;
1301 int i, ret;
1302
1303 port_priv = agent_priv->qp_info->port_priv;
1304 class = &port_priv->version[mad_reg_req->mgmt_class_version].class;
1305 if (!*class) {
1306 /* Allocate management class table for "new" class version */
Roland Dreierde6eb662005-11-02 07:23:14 -08001307 *class = kzalloc(sizeof **class, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 if (!*class) {
1309 printk(KERN_ERR PFX "No memory for "
1310 "ib_mad_mgmt_class_table\n");
1311 ret = -ENOMEM;
1312 goto error1;
1313 }
Roland Dreierde6eb662005-11-02 07:23:14 -08001314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 /* Allocate method table for this management class */
1316 method = &(*class)->method_table[mgmt_class];
1317 if ((ret = allocate_method_table(method)))
1318 goto error2;
1319 } else {
1320 method = &(*class)->method_table[mgmt_class];
1321 if (!*method) {
1322 /* Allocate method table for this management class */
1323 if ((ret = allocate_method_table(method)))
1324 goto error1;
1325 }
1326 }
1327
1328 /* Now, make sure methods are not already in use */
1329 if (method_in_use(method, mad_reg_req))
1330 goto error3;
1331
1332 /* Finally, add in methods being registered */
1333 for (i = find_first_bit(mad_reg_req->method_mask,
1334 IB_MGMT_MAX_METHODS);
1335 i < IB_MGMT_MAX_METHODS;
1336 i = find_next_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS,
1337 1+i)) {
1338 (*method)->agent[i] = agent_priv;
1339 }
1340 return 0;
1341
1342error3:
1343 /* Remove any methods for this mad agent */
1344 remove_methods_mad_agent(*method, agent_priv);
1345 /* Now, check to see if there are any methods in use */
1346 if (!check_method_table(*method)) {
1347 /* If not, release management method table */
1348 kfree(*method);
1349 *method = NULL;
1350 }
1351 ret = -EINVAL;
1352 goto error1;
1353error2:
1354 kfree(*class);
1355 *class = NULL;
1356error1:
1357 return ret;
1358}
1359
1360static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1361 struct ib_mad_agent_private *agent_priv)
1362{
1363 struct ib_mad_port_private *port_priv;
1364 struct ib_mad_mgmt_vendor_class_table **vendor_table;
1365 struct ib_mad_mgmt_vendor_class_table *vendor = NULL;
1366 struct ib_mad_mgmt_vendor_class *vendor_class = NULL;
1367 struct ib_mad_mgmt_method_table **method;
1368 int i, ret = -ENOMEM;
1369 u8 vclass;
1370
1371 /* "New" vendor (with OUI) class */
1372 vclass = vendor_class_index(mad_reg_req->mgmt_class);
1373 port_priv = agent_priv->qp_info->port_priv;
1374 vendor_table = &port_priv->version[
1375 mad_reg_req->mgmt_class_version].vendor;
1376 if (!*vendor_table) {
1377 /* Allocate mgmt vendor class table for "new" class version */
Roland Dreierde6eb662005-11-02 07:23:14 -08001378 vendor = kzalloc(sizeof *vendor, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 if (!vendor) {
1380 printk(KERN_ERR PFX "No memory for "
1381 "ib_mad_mgmt_vendor_class_table\n");
1382 goto error1;
1383 }
Roland Dreierde6eb662005-11-02 07:23:14 -08001384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 *vendor_table = vendor;
1386 }
1387 if (!(*vendor_table)->vendor_class[vclass]) {
1388 /* Allocate table for this management vendor class */
Roland Dreierde6eb662005-11-02 07:23:14 -08001389 vendor_class = kzalloc(sizeof *vendor_class, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 if (!vendor_class) {
1391 printk(KERN_ERR PFX "No memory for "
1392 "ib_mad_mgmt_vendor_class\n");
1393 goto error2;
1394 }
Roland Dreierde6eb662005-11-02 07:23:14 -08001395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 (*vendor_table)->vendor_class[vclass] = vendor_class;
1397 }
1398 for (i = 0; i < MAX_MGMT_OUI; i++) {
1399 /* Is there matching OUI for this vendor class ? */
1400 if (!memcmp((*vendor_table)->vendor_class[vclass]->oui[i],
1401 mad_reg_req->oui, 3)) {
1402 method = &(*vendor_table)->vendor_class[
1403 vclass]->method_table[i];
1404 BUG_ON(!*method);
1405 goto check_in_use;
1406 }
1407 }
1408 for (i = 0; i < MAX_MGMT_OUI; i++) {
1409 /* OUI slot available ? */
1410 if (!is_vendor_oui((*vendor_table)->vendor_class[
1411 vclass]->oui[i])) {
1412 method = &(*vendor_table)->vendor_class[
1413 vclass]->method_table[i];
1414 BUG_ON(*method);
1415 /* Allocate method table for this OUI */
1416 if ((ret = allocate_method_table(method)))
1417 goto error3;
1418 memcpy((*vendor_table)->vendor_class[vclass]->oui[i],
1419 mad_reg_req->oui, 3);
1420 goto check_in_use;
1421 }
1422 }
1423 printk(KERN_ERR PFX "All OUI slots in use\n");
1424 goto error3;
1425
1426check_in_use:
1427 /* Now, make sure methods are not already in use */
1428 if (method_in_use(method, mad_reg_req))
1429 goto error4;
1430
1431 /* Finally, add in methods being registered */
1432 for (i = find_first_bit(mad_reg_req->method_mask,
1433 IB_MGMT_MAX_METHODS);
1434 i < IB_MGMT_MAX_METHODS;
1435 i = find_next_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS,
1436 1+i)) {
1437 (*method)->agent[i] = agent_priv;
1438 }
1439 return 0;
1440
1441error4:
1442 /* Remove any methods for this mad agent */
1443 remove_methods_mad_agent(*method, agent_priv);
1444 /* Now, check to see if there are any methods in use */
1445 if (!check_method_table(*method)) {
1446 /* If not, release management method table */
1447 kfree(*method);
1448 *method = NULL;
1449 }
1450 ret = -EINVAL;
1451error3:
1452 if (vendor_class) {
1453 (*vendor_table)->vendor_class[vclass] = NULL;
1454 kfree(vendor_class);
1455 }
1456error2:
1457 if (vendor) {
1458 *vendor_table = NULL;
1459 kfree(vendor);
1460 }
1461error1:
1462 return ret;
1463}
1464
1465static void remove_mad_reg_req(struct ib_mad_agent_private *agent_priv)
1466{
1467 struct ib_mad_port_private *port_priv;
1468 struct ib_mad_mgmt_class_table *class;
1469 struct ib_mad_mgmt_method_table *method;
1470 struct ib_mad_mgmt_vendor_class_table *vendor;
1471 struct ib_mad_mgmt_vendor_class *vendor_class;
1472 int index;
1473 u8 mgmt_class;
1474
1475 /*
1476 * Was MAD registration request supplied
1477 * with original registration ?
1478 */
1479 if (!agent_priv->reg_req) {
1480 goto out;
1481 }
1482
1483 port_priv = agent_priv->qp_info->port_priv;
1484 mgmt_class = convert_mgmt_class(agent_priv->reg_req->mgmt_class);
1485 class = port_priv->version[
1486 agent_priv->reg_req->mgmt_class_version].class;
1487 if (!class)
1488 goto vendor_check;
1489
1490 method = class->method_table[mgmt_class];
1491 if (method) {
1492 /* Remove any methods for this mad agent */
1493 remove_methods_mad_agent(method, agent_priv);
1494 /* Now, check to see if there are any methods still in use */
1495 if (!check_method_table(method)) {
1496 /* If not, release management method table */
1497 kfree(method);
1498 class->method_table[mgmt_class] = NULL;
1499 /* Any management classes left ? */
1500 if (!check_class_table(class)) {
1501 /* If not, release management class table */
1502 kfree(class);
1503 port_priv->version[
1504 agent_priv->reg_req->
1505 mgmt_class_version].class = NULL;
1506 }
1507 }
1508 }
1509
1510vendor_check:
1511 if (!is_vendor_class(mgmt_class))
1512 goto out;
1513
1514 /* normalize mgmt_class to vendor range 2 */
1515 mgmt_class = vendor_class_index(agent_priv->reg_req->mgmt_class);
1516 vendor = port_priv->version[
1517 agent_priv->reg_req->mgmt_class_version].vendor;
1518
1519 if (!vendor)
1520 goto out;
1521
1522 vendor_class = vendor->vendor_class[mgmt_class];
1523 if (vendor_class) {
1524 index = find_vendor_oui(vendor_class, agent_priv->reg_req->oui);
1525 if (index < 0)
1526 goto out;
1527 method = vendor_class->method_table[index];
1528 if (method) {
1529 /* Remove any methods for this mad agent */
1530 remove_methods_mad_agent(method, agent_priv);
1531 /*
1532 * Now, check to see if there are
1533 * any methods still in use
1534 */
1535 if (!check_method_table(method)) {
1536 /* If not, release management method table */
1537 kfree(method);
1538 vendor_class->method_table[index] = NULL;
1539 memset(vendor_class->oui[index], 0, 3);
1540 /* Any OUIs left ? */
1541 if (!check_vendor_class(vendor_class)) {
1542 /* If not, release vendor class table */
1543 kfree(vendor_class);
1544 vendor->vendor_class[mgmt_class] = NULL;
1545 /* Any other vendor classes left ? */
1546 if (!check_vendor_table(vendor)) {
1547 kfree(vendor);
1548 port_priv->version[
1549 agent_priv->reg_req->
1550 mgmt_class_version].
1551 vendor = NULL;
1552 }
1553 }
1554 }
1555 }
1556 }
1557
1558out:
1559 return;
1560}
1561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562static struct ib_mad_agent_private *
1563find_mad_agent(struct ib_mad_port_private *port_priv,
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07001564 struct ib_mad *mad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
1566 struct ib_mad_agent_private *mad_agent = NULL;
1567 unsigned long flags;
1568
1569 spin_lock_irqsave(&port_priv->reg_lock, flags);
Sean Hefty2527e682006-07-20 11:25:50 +03001570 if (ib_response_mad(mad)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 u32 hi_tid;
1572 struct ib_mad_agent_private *entry;
1573
1574 /*
1575 * Routing is based on high 32 bits of transaction ID
1576 * of MAD.
1577 */
1578 hi_tid = be64_to_cpu(mad->mad_hdr.tid) >> 32;
Sean Hefty34816ad2005-10-25 10:51:39 -07001579 list_for_each_entry(entry, &port_priv->agent_list, agent_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 if (entry->agent.hi_tid == hi_tid) {
1581 mad_agent = entry;
1582 break;
1583 }
1584 }
1585 } else {
1586 struct ib_mad_mgmt_class_table *class;
1587 struct ib_mad_mgmt_method_table *method;
1588 struct ib_mad_mgmt_vendor_class_table *vendor;
1589 struct ib_mad_mgmt_vendor_class *vendor_class;
1590 struct ib_vendor_mad *vendor_mad;
1591 int index;
1592
1593 /*
1594 * Routing is based on version, class, and method
1595 * For "newer" vendor MADs, also based on OUI
1596 */
1597 if (mad->mad_hdr.class_version >= MAX_MGMT_VERSION)
1598 goto out;
1599 if (!is_vendor_class(mad->mad_hdr.mgmt_class)) {
1600 class = port_priv->version[
1601 mad->mad_hdr.class_version].class;
1602 if (!class)
1603 goto out;
1604 method = class->method_table[convert_mgmt_class(
1605 mad->mad_hdr.mgmt_class)];
1606 if (method)
1607 mad_agent = method->agent[mad->mad_hdr.method &
1608 ~IB_MGMT_METHOD_RESP];
1609 } else {
1610 vendor = port_priv->version[
1611 mad->mad_hdr.class_version].vendor;
1612 if (!vendor)
1613 goto out;
1614 vendor_class = vendor->vendor_class[vendor_class_index(
1615 mad->mad_hdr.mgmt_class)];
1616 if (!vendor_class)
1617 goto out;
1618 /* Find matching OUI */
1619 vendor_mad = (struct ib_vendor_mad *)mad;
1620 index = find_vendor_oui(vendor_class, vendor_mad->oui);
1621 if (index == -1)
1622 goto out;
1623 method = vendor_class->method_table[index];
1624 if (method) {
1625 mad_agent = method->agent[mad->mad_hdr.method &
1626 ~IB_MGMT_METHOD_RESP];
1627 }
1628 }
1629 }
1630
1631 if (mad_agent) {
1632 if (mad_agent->agent.recv_handler)
1633 atomic_inc(&mad_agent->refcount);
1634 else {
1635 printk(KERN_NOTICE PFX "No receive handler for client "
1636 "%p on port %d\n",
1637 &mad_agent->agent, port_priv->port_num);
1638 mad_agent = NULL;
1639 }
1640 }
1641out:
1642 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
1643
1644 return mad_agent;
1645}
1646
1647static int validate_mad(struct ib_mad *mad, u32 qp_num)
1648{
1649 int valid = 0;
1650
1651 /* Make sure MAD base version is understood */
1652 if (mad->mad_hdr.base_version != IB_MGMT_BASE_VERSION) {
1653 printk(KERN_ERR PFX "MAD received with unsupported base "
1654 "version %d\n", mad->mad_hdr.base_version);
1655 goto out;
1656 }
1657
1658 /* Filter SMI packets sent to other than QP0 */
1659 if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
1660 (mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) {
1661 if (qp_num == 0)
1662 valid = 1;
1663 } else {
1664 /* Filter GSI packets sent to QP0 */
1665 if (qp_num != 0)
1666 valid = 1;
1667 }
1668
1669out:
1670 return valid;
1671}
1672
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001673static int is_data_mad(struct ib_mad_agent_private *mad_agent_priv,
1674 struct ib_mad_hdr *mad_hdr)
1675{
1676 struct ib_rmpp_mad *rmpp_mad;
1677
1678 rmpp_mad = (struct ib_rmpp_mad *)mad_hdr;
1679 return !mad_agent_priv->agent.rmpp_version ||
1680 !(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
1681 IB_MGMT_RMPP_FLAG_ACTIVE) ||
1682 (rmpp_mad->rmpp_hdr.rmpp_type == IB_MGMT_RMPP_TYPE_DATA);
1683}
1684
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001685static inline int rcv_has_same_class(struct ib_mad_send_wr_private *wr,
1686 struct ib_mad_recv_wc *rwc)
1687{
1688 return ((struct ib_mad *)(wr->send_buf.mad))->mad_hdr.mgmt_class ==
1689 rwc->recv_buf.mad->mad_hdr.mgmt_class;
1690}
1691
Jack Morgenstein9874e742006-06-17 20:37:34 -07001692static inline int rcv_has_same_gid(struct ib_mad_agent_private *mad_agent_priv,
1693 struct ib_mad_send_wr_private *wr,
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001694 struct ib_mad_recv_wc *rwc )
1695{
1696 struct ib_ah_attr attr;
1697 u8 send_resp, rcv_resp;
Jack Morgenstein9874e742006-06-17 20:37:34 -07001698 union ib_gid sgid;
1699 struct ib_device *device = mad_agent_priv->agent.device;
1700 u8 port_num = mad_agent_priv->agent.port_num;
1701 u8 lmc;
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001702
Michael Brooks70972282008-09-20 20:06:16 -07001703 send_resp = ib_response_mad((struct ib_mad *)wr->send_buf.mad);
1704 rcv_resp = ib_response_mad(rwc->recv_buf.mad);
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001705
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001706 if (send_resp == rcv_resp)
1707 /* both requests, or both responses. GIDs different */
1708 return 0;
1709
1710 if (ib_query_ah(wr->send_buf.ah, &attr))
1711 /* Assume not equal, to avoid false positives. */
1712 return 0;
1713
Jack Morgenstein9874e742006-06-17 20:37:34 -07001714 if (!!(attr.ah_flags & IB_AH_GRH) !=
1715 !!(rwc->wc->wc_flags & IB_WC_GRH))
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001716 /* one has GID, other does not. Assume different */
1717 return 0;
Jack Morgenstein9874e742006-06-17 20:37:34 -07001718
1719 if (!send_resp && rcv_resp) {
1720 /* is request/response. */
1721 if (!(attr.ah_flags & IB_AH_GRH)) {
1722 if (ib_get_cached_lmc(device, port_num, &lmc))
1723 return 0;
1724 return (!lmc || !((attr.src_path_bits ^
1725 rwc->wc->dlid_path_bits) &
1726 ((1 << lmc) - 1)));
1727 } else {
1728 if (ib_get_cached_gid(device, port_num,
1729 attr.grh.sgid_index, &sgid))
1730 return 0;
1731 return !memcmp(sgid.raw, rwc->recv_buf.grh->dgid.raw,
1732 16);
1733 }
1734 }
1735
1736 if (!(attr.ah_flags & IB_AH_GRH))
1737 return attr.dlid == rwc->wc->slid;
1738 else
1739 return !memcmp(attr.grh.dgid.raw, rwc->recv_buf.grh->sgid.raw,
1740 16);
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001741}
Jack Morgenstein9874e742006-06-17 20:37:34 -07001742
1743static inline int is_direct(u8 class)
1744{
1745 return (class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE);
1746}
1747
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001748struct ib_mad_send_wr_private*
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001749ib_find_send_mad(struct ib_mad_agent_private *mad_agent_priv,
Jack Morgenstein9874e742006-06-17 20:37:34 -07001750 struct ib_mad_recv_wc *wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751{
Jack Morgenstein9874e742006-06-17 20:37:34 -07001752 struct ib_mad_send_wr_private *wr;
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001753 struct ib_mad *mad;
1754
Jack Morgenstein9874e742006-06-17 20:37:34 -07001755 mad = (struct ib_mad *)wc->recv_buf.mad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Jack Morgenstein9874e742006-06-17 20:37:34 -07001757 list_for_each_entry(wr, &mad_agent_priv->wait_list, agent_list) {
1758 if ((wr->tid == mad->mad_hdr.tid) &&
1759 rcv_has_same_class(wr, wc) &&
1760 /*
1761 * Don't check GID for direct routed MADs.
1762 * These might have permissive LIDs.
1763 */
1764 (is_direct(wc->recv_buf.mad->mad_hdr.mgmt_class) ||
1765 rcv_has_same_gid(mad_agent_priv, wr, wc)))
Roland Dreier39798692006-11-13 09:38:07 -08001766 return (wr->status == IB_WC_SUCCESS) ? wr : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 }
1768
1769 /*
1770 * It's possible to receive the response before we've
1771 * been notified that the send has completed
1772 */
Jack Morgenstein9874e742006-06-17 20:37:34 -07001773 list_for_each_entry(wr, &mad_agent_priv->send_list, agent_list) {
1774 if (is_data_mad(mad_agent_priv, wr->send_buf.mad) &&
1775 wr->tid == mad->mad_hdr.tid &&
1776 wr->timeout &&
1777 rcv_has_same_class(wr, wc) &&
1778 /*
1779 * Don't check GID for direct routed MADs.
1780 * These might have permissive LIDs.
1781 */
1782 (is_direct(wc->recv_buf.mad->mad_hdr.mgmt_class) ||
1783 rcv_has_same_gid(mad_agent_priv, wr, wc)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 /* Verify request has not been canceled */
Jack Morgenstein9874e742006-06-17 20:37:34 -07001785 return (wr->status == IB_WC_SUCCESS) ? wr : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 }
1787 return NULL;
1788}
1789
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001790void ib_mark_mad_done(struct ib_mad_send_wr_private *mad_send_wr)
Hal Rosenstock6a0c4352005-07-27 11:45:26 -07001791{
1792 mad_send_wr->timeout = 0;
Akinobu Mita179e0912006-06-26 00:24:41 -07001793 if (mad_send_wr->refcount == 1)
1794 list_move_tail(&mad_send_wr->agent_list,
Hal Rosenstock6a0c4352005-07-27 11:45:26 -07001795 &mad_send_wr->mad_agent_priv->done_list);
Hal Rosenstock6a0c4352005-07-27 11:45:26 -07001796}
1797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798static void ib_mad_complete_recv(struct ib_mad_agent_private *mad_agent_priv,
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07001799 struct ib_mad_recv_wc *mad_recv_wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
1801 struct ib_mad_send_wr_private *mad_send_wr;
1802 struct ib_mad_send_wc mad_send_wc;
1803 unsigned long flags;
1804
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001805 INIT_LIST_HEAD(&mad_recv_wc->rmpp_list);
1806 list_add(&mad_recv_wc->recv_buf.list, &mad_recv_wc->rmpp_list);
1807 if (mad_agent_priv->agent.rmpp_version) {
1808 mad_recv_wc = ib_process_rmpp_recv_wc(mad_agent_priv,
1809 mad_recv_wc);
1810 if (!mad_recv_wc) {
Sean Hefty1b52fa982006-05-12 14:57:52 -07001811 deref_mad_agent(mad_agent_priv);
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001812 return;
1813 }
1814 }
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 /* Complete corresponding request */
Sean Hefty2527e682006-07-20 11:25:50 +03001817 if (ib_response_mad(mad_recv_wc->recv_buf.mad)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 spin_lock_irqsave(&mad_agent_priv->lock, flags);
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001819 mad_send_wr = ib_find_send_mad(mad_agent_priv, mad_recv_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 if (!mad_send_wr) {
1821 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07001822 ib_free_recv_mad(mad_recv_wc);
Sean Hefty1b52fa982006-05-12 14:57:52 -07001823 deref_mad_agent(mad_agent_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 return;
1825 }
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001826 ib_mark_mad_done(mad_send_wr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1828
1829 /* Defined behavior is to complete response before request */
Sean Hefty34816ad2005-10-25 10:51:39 -07001830 mad_recv_wc->wc->wr_id = (unsigned long) &mad_send_wr->send_buf;
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07001831 mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent,
1832 mad_recv_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 atomic_dec(&mad_agent_priv->refcount);
1834
1835 mad_send_wc.status = IB_WC_SUCCESS;
1836 mad_send_wc.vendor_err = 0;
Sean Hefty34816ad2005-10-25 10:51:39 -07001837 mad_send_wc.send_buf = &mad_send_wr->send_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
1839 } else {
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07001840 mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent,
1841 mad_recv_wc);
Sean Hefty1b52fa982006-05-12 14:57:52 -07001842 deref_mad_agent(mad_agent_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 }
1844}
1845
1846static void ib_mad_recv_done_handler(struct ib_mad_port_private *port_priv,
1847 struct ib_wc *wc)
1848{
1849 struct ib_mad_qp_info *qp_info;
1850 struct ib_mad_private_header *mad_priv_hdr;
Hal Rosenstock445d6802007-08-03 10:45:17 -07001851 struct ib_mad_private *recv, *response = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 struct ib_mad_list_head *mad_list;
1853 struct ib_mad_agent_private *mad_agent;
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04001854 int port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
1857 qp_info = mad_list->mad_queue->qp_info;
1858 dequeue_mad(mad_list);
1859
1860 mad_priv_hdr = container_of(mad_list, struct ib_mad_private_header,
1861 mad_list);
1862 recv = container_of(mad_priv_hdr, struct ib_mad_private, header);
Ralph Campbell15271062006-12-12 14:28:30 -08001863 ib_dma_unmap_single(port_priv->device,
1864 recv->header.mapping,
1865 sizeof(struct ib_mad_private) -
1866 sizeof(struct ib_mad_private_header),
1867 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 /* Setup MAD receive work completion from "normal" work completion */
Sean Hefty24239af2005-04-16 15:26:08 -07001870 recv->header.wc = *wc;
1871 recv->header.recv_wc.wc = &recv->header.wc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 recv->header.recv_wc.mad_len = sizeof(struct ib_mad);
1873 recv->header.recv_wc.recv_buf.mad = &recv->mad.mad;
1874 recv->header.recv_wc.recv_buf.grh = &recv->grh;
1875
1876 if (atomic_read(&qp_info->snoop_count))
1877 snoop_recv(qp_info, &recv->header.recv_wc, IB_MAD_SNOOP_RECVS);
1878
1879 /* Validate MAD */
1880 if (!validate_mad(&recv->mad.mad, qp_info->qp->qp_num))
1881 goto out;
1882
Hal Rosenstock445d6802007-08-03 10:45:17 -07001883 response = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL);
1884 if (!response) {
1885 printk(KERN_ERR PFX "ib_mad_recv_done_handler no memory "
1886 "for response buffer\n");
1887 goto out;
1888 }
1889
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04001890 if (port_priv->device->node_type == RDMA_NODE_IB_SWITCH)
1891 port_num = wc->port_num;
1892 else
1893 port_num = port_priv->port_num;
1894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if (recv->mad.mad.mad_hdr.mgmt_class ==
1896 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04001897 enum smi_forward_action retsmi;
1898
Hal Rosenstockde493d42007-04-02 11:24:07 -04001899 if (smi_handle_dr_smp_recv(&recv->mad.smp,
1900 port_priv->device->node_type,
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04001901 port_num,
Hal Rosenstockde493d42007-04-02 11:24:07 -04001902 port_priv->device->phys_port_cnt) ==
1903 IB_SMI_DISCARD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 goto out;
Hal Rosenstockde493d42007-04-02 11:24:07 -04001905
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04001906 retsmi = smi_check_forward_dr_smp(&recv->mad.smp);
1907 if (retsmi == IB_SMI_LOCAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 goto local;
Hal Rosenstockde493d42007-04-02 11:24:07 -04001909
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04001910 if (retsmi == IB_SMI_SEND) { /* don't forward */
1911 if (smi_handle_dr_smp_send(&recv->mad.smp,
1912 port_priv->device->node_type,
1913 port_num) == IB_SMI_DISCARD)
1914 goto out;
Hal Rosenstockde493d42007-04-02 11:24:07 -04001915
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04001916 if (smi_check_local_smp(&recv->mad.smp, port_priv->device) == IB_SMI_DISCARD)
1917 goto out;
1918 } else if (port_priv->device->node_type == RDMA_NODE_IB_SWITCH) {
1919 /* forward case for switches */
1920 memcpy(response, recv, sizeof(*response));
1921 response->header.recv_wc.wc = &response->header.wc;
1922 response->header.recv_wc.recv_buf.mad = &response->mad.mad;
1923 response->header.recv_wc.recv_buf.grh = &response->grh;
1924
Hal Rosenstock86dfbec2007-08-03 10:45:17 -07001925 agent_send_response(&response->mad.mad,
1926 &response->grh, wc,
1927 port_priv->device,
1928 smi_get_fwd_port(&recv->mad.smp),
1929 qp_info->qp->qp_num);
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04001930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 goto out;
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04001932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 }
1934
1935local:
1936 /* Give driver "right of first refusal" on incoming MAD */
1937 if (port_priv->device->process_mad) {
1938 int ret;
1939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 ret = port_priv->device->process_mad(port_priv->device, 0,
1941 port_priv->port_num,
1942 wc, &recv->grh,
1943 &recv->mad.mad,
1944 &response->mad.mad);
1945 if (ret & IB_MAD_RESULT_SUCCESS) {
1946 if (ret & IB_MAD_RESULT_CONSUMED)
1947 goto out;
1948 if (ret & IB_MAD_RESULT_REPLY) {
Sean Hefty34816ad2005-10-25 10:51:39 -07001949 agent_send_response(&response->mad.mad,
1950 &recv->grh, wc,
1951 port_priv->device,
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04001952 port_num,
Sean Hefty34816ad2005-10-25 10:51:39 -07001953 qp_info->qp->qp_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 goto out;
1955 }
1956 }
1957 }
1958
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07001959 mad_agent = find_mad_agent(port_priv, &recv->mad.mad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 if (mad_agent) {
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07001961 ib_mad_complete_recv(mad_agent, &recv->header.recv_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 /*
1963 * recv is freed up in error cases in ib_mad_complete_recv
1964 * or via recv_handler in ib_mad_complete_recv()
1965 */
1966 recv = NULL;
1967 }
1968
1969out:
1970 /* Post another receive request for this QP */
1971 if (response) {
1972 ib_mad_post_receive_mads(qp_info, response);
1973 if (recv)
1974 kmem_cache_free(ib_mad_cache, recv);
1975 } else
1976 ib_mad_post_receive_mads(qp_info, recv);
1977}
1978
1979static void adjust_timeout(struct ib_mad_agent_private *mad_agent_priv)
1980{
1981 struct ib_mad_send_wr_private *mad_send_wr;
1982 unsigned long delay;
1983
1984 if (list_empty(&mad_agent_priv->wait_list)) {
Roland Dreier6b2eef82009-09-07 08:27:50 -07001985 __cancel_delayed_work(&mad_agent_priv->timed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 } else {
1987 mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
1988 struct ib_mad_send_wr_private,
1989 agent_list);
1990
1991 if (time_after(mad_agent_priv->timeout,
1992 mad_send_wr->timeout)) {
1993 mad_agent_priv->timeout = mad_send_wr->timeout;
Roland Dreier6b2eef82009-09-07 08:27:50 -07001994 __cancel_delayed_work(&mad_agent_priv->timed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 delay = mad_send_wr->timeout - jiffies;
1996 if ((long)delay <= 0)
1997 delay = 1;
1998 queue_delayed_work(mad_agent_priv->qp_info->
1999 port_priv->wq,
2000 &mad_agent_priv->timed_work, delay);
2001 }
2002 }
2003}
2004
Hal Rosenstockd760ce82005-07-27 11:45:25 -07002005static void wait_for_response(struct ib_mad_send_wr_private *mad_send_wr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006{
Hal Rosenstockd760ce82005-07-27 11:45:25 -07002007 struct ib_mad_agent_private *mad_agent_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 struct ib_mad_send_wr_private *temp_mad_send_wr;
2009 struct list_head *list_item;
2010 unsigned long delay;
2011
Hal Rosenstockd760ce82005-07-27 11:45:25 -07002012 mad_agent_priv = mad_send_wr->mad_agent_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 list_del(&mad_send_wr->agent_list);
2014
2015 delay = mad_send_wr->timeout;
2016 mad_send_wr->timeout += jiffies;
2017
Hal Rosenstock29bb33d2005-07-27 11:45:32 -07002018 if (delay) {
2019 list_for_each_prev(list_item, &mad_agent_priv->wait_list) {
2020 temp_mad_send_wr = list_entry(list_item,
2021 struct ib_mad_send_wr_private,
2022 agent_list);
2023 if (time_after(mad_send_wr->timeout,
2024 temp_mad_send_wr->timeout))
2025 break;
2026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 }
Hal Rosenstock29bb33d2005-07-27 11:45:32 -07002028 else
2029 list_item = &mad_agent_priv->wait_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 list_add(&mad_send_wr->agent_list, list_item);
2031
2032 /* Reschedule a work item if we have a shorter timeout */
2033 if (mad_agent_priv->wait_list.next == &mad_send_wr->agent_list) {
Roland Dreier6b2eef82009-09-07 08:27:50 -07002034 __cancel_delayed_work(&mad_agent_priv->timed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 queue_delayed_work(mad_agent_priv->qp_info->port_priv->wq,
2036 &mad_agent_priv->timed_work, delay);
2037 }
2038}
2039
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002040void ib_reset_mad_timeout(struct ib_mad_send_wr_private *mad_send_wr,
2041 int timeout_ms)
2042{
2043 mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
2044 wait_for_response(mad_send_wr);
2045}
2046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047/*
2048 * Process a send work completion
2049 */
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002050void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr,
2051 struct ib_mad_send_wc *mad_send_wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
2053 struct ib_mad_agent_private *mad_agent_priv;
2054 unsigned long flags;
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002055 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Hal Rosenstockd760ce82005-07-27 11:45:25 -07002057 mad_agent_priv = mad_send_wr->mad_agent_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 spin_lock_irqsave(&mad_agent_priv->lock, flags);
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002059 if (mad_agent_priv->agent.rmpp_version) {
2060 ret = ib_process_rmpp_send_wc(mad_send_wr, mad_send_wc);
2061 if (ret == IB_RMPP_RESULT_CONSUMED)
2062 goto done;
2063 } else
2064 ret = IB_RMPP_RESULT_UNHANDLED;
2065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 if (mad_send_wc->status != IB_WC_SUCCESS &&
2067 mad_send_wr->status == IB_WC_SUCCESS) {
2068 mad_send_wr->status = mad_send_wc->status;
2069 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2070 }
2071
2072 if (--mad_send_wr->refcount > 0) {
2073 if (mad_send_wr->refcount == 1 && mad_send_wr->timeout &&
2074 mad_send_wr->status == IB_WC_SUCCESS) {
Hal Rosenstockd760ce82005-07-27 11:45:25 -07002075 wait_for_response(mad_send_wr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 }
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002077 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 }
2079
2080 /* Remove send from MAD agent and notify client of completion */
2081 list_del(&mad_send_wr->agent_list);
2082 adjust_timeout(mad_agent_priv);
2083 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2084
2085 if (mad_send_wr->status != IB_WC_SUCCESS )
2086 mad_send_wc->status = mad_send_wr->status;
Sean Hefty34816ad2005-10-25 10:51:39 -07002087 if (ret == IB_RMPP_RESULT_INTERNAL)
2088 ib_rmpp_send_handler(mad_send_wc);
2089 else
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002090 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2091 mad_send_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
2093 /* Release reference on agent taken when sending */
Sean Hefty1b52fa982006-05-12 14:57:52 -07002094 deref_mad_agent(mad_agent_priv);
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002095 return;
2096done:
2097 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098}
2099
2100static void ib_mad_send_done_handler(struct ib_mad_port_private *port_priv,
2101 struct ib_wc *wc)
2102{
2103 struct ib_mad_send_wr_private *mad_send_wr, *queued_send_wr;
2104 struct ib_mad_list_head *mad_list;
2105 struct ib_mad_qp_info *qp_info;
2106 struct ib_mad_queue *send_queue;
2107 struct ib_send_wr *bad_send_wr;
Sean Hefty34816ad2005-10-25 10:51:39 -07002108 struct ib_mad_send_wc mad_send_wc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 unsigned long flags;
2110 int ret;
2111
2112 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
2113 mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
2114 mad_list);
2115 send_queue = mad_list->mad_queue;
2116 qp_info = send_queue->qp_info;
2117
2118retry:
Ralph Campbell15271062006-12-12 14:28:30 -08002119 ib_dma_unmap_single(mad_send_wr->send_buf.mad_agent->device,
2120 mad_send_wr->header_mapping,
2121 mad_send_wr->sg_list[0].length, DMA_TO_DEVICE);
2122 ib_dma_unmap_single(mad_send_wr->send_buf.mad_agent->device,
2123 mad_send_wr->payload_mapping,
2124 mad_send_wr->sg_list[1].length, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 queued_send_wr = NULL;
2126 spin_lock_irqsave(&send_queue->lock, flags);
2127 list_del(&mad_list->list);
2128
2129 /* Move queued send to the send queue */
2130 if (send_queue->count-- > send_queue->max_active) {
2131 mad_list = container_of(qp_info->overflow_list.next,
2132 struct ib_mad_list_head, list);
2133 queued_send_wr = container_of(mad_list,
2134 struct ib_mad_send_wr_private,
2135 mad_list);
Akinobu Mita179e0912006-06-26 00:24:41 -07002136 list_move_tail(&mad_list->list, &send_queue->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 }
2138 spin_unlock_irqrestore(&send_queue->lock, flags);
2139
Sean Hefty34816ad2005-10-25 10:51:39 -07002140 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2141 mad_send_wc.status = wc->status;
2142 mad_send_wc.vendor_err = wc->vendor_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 if (atomic_read(&qp_info->snoop_count))
Sean Hefty34816ad2005-10-25 10:51:39 -07002144 snoop_send(qp_info, &mad_send_wr->send_buf, &mad_send_wc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 IB_MAD_SNOOP_SEND_COMPLETIONS);
Sean Hefty34816ad2005-10-25 10:51:39 -07002146 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148 if (queued_send_wr) {
2149 ret = ib_post_send(qp_info->qp, &queued_send_wr->send_wr,
Sean Hefty34816ad2005-10-25 10:51:39 -07002150 &bad_send_wr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 if (ret) {
2152 printk(KERN_ERR PFX "ib_post_send failed: %d\n", ret);
2153 mad_send_wr = queued_send_wr;
2154 wc->status = IB_WC_LOC_QP_OP_ERR;
2155 goto retry;
2156 }
2157 }
2158}
2159
2160static void mark_sends_for_retry(struct ib_mad_qp_info *qp_info)
2161{
2162 struct ib_mad_send_wr_private *mad_send_wr;
2163 struct ib_mad_list_head *mad_list;
2164 unsigned long flags;
2165
2166 spin_lock_irqsave(&qp_info->send_queue.lock, flags);
2167 list_for_each_entry(mad_list, &qp_info->send_queue.list, list) {
2168 mad_send_wr = container_of(mad_list,
2169 struct ib_mad_send_wr_private,
2170 mad_list);
2171 mad_send_wr->retry = 1;
2172 }
2173 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
2174}
2175
2176static void mad_error_handler(struct ib_mad_port_private *port_priv,
2177 struct ib_wc *wc)
2178{
2179 struct ib_mad_list_head *mad_list;
2180 struct ib_mad_qp_info *qp_info;
2181 struct ib_mad_send_wr_private *mad_send_wr;
2182 int ret;
2183
2184 /* Determine if failure was a send or receive */
2185 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
2186 qp_info = mad_list->mad_queue->qp_info;
2187 if (mad_list->mad_queue == &qp_info->recv_queue)
2188 /*
2189 * Receive errors indicate that the QP has entered the error
2190 * state - error handling/shutdown code will cleanup
2191 */
2192 return;
2193
2194 /*
2195 * Send errors will transition the QP to SQE - move
2196 * QP to RTS and repost flushed work requests
2197 */
2198 mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
2199 mad_list);
2200 if (wc->status == IB_WC_WR_FLUSH_ERR) {
2201 if (mad_send_wr->retry) {
2202 /* Repost send */
2203 struct ib_send_wr *bad_send_wr;
2204
2205 mad_send_wr->retry = 0;
2206 ret = ib_post_send(qp_info->qp, &mad_send_wr->send_wr,
2207 &bad_send_wr);
2208 if (ret)
2209 ib_mad_send_done_handler(port_priv, wc);
2210 } else
2211 ib_mad_send_done_handler(port_priv, wc);
2212 } else {
2213 struct ib_qp_attr *attr;
2214
2215 /* Transition QP to RTS and fail offending send */
2216 attr = kmalloc(sizeof *attr, GFP_KERNEL);
2217 if (attr) {
2218 attr->qp_state = IB_QPS_RTS;
2219 attr->cur_qp_state = IB_QPS_SQE;
2220 ret = ib_modify_qp(qp_info->qp, attr,
2221 IB_QP_STATE | IB_QP_CUR_STATE);
2222 kfree(attr);
2223 if (ret)
2224 printk(KERN_ERR PFX "mad_error_handler - "
2225 "ib_modify_qp to RTS : %d\n", ret);
2226 else
2227 mark_sends_for_retry(qp_info);
2228 }
2229 ib_mad_send_done_handler(port_priv, wc);
2230 }
2231}
2232
2233/*
2234 * IB MAD completion callback
2235 */
David Howellsc4028952006-11-22 14:57:56 +00002236static void ib_mad_completion_handler(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237{
2238 struct ib_mad_port_private *port_priv;
2239 struct ib_wc wc;
2240
David Howellsc4028952006-11-22 14:57:56 +00002241 port_priv = container_of(work, struct ib_mad_port_private, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
2243
2244 while (ib_poll_cq(port_priv->cq, 1, &wc) == 1) {
2245 if (wc.status == IB_WC_SUCCESS) {
2246 switch (wc.opcode) {
2247 case IB_WC_SEND:
2248 ib_mad_send_done_handler(port_priv, &wc);
2249 break;
2250 case IB_WC_RECV:
2251 ib_mad_recv_done_handler(port_priv, &wc);
2252 break;
2253 default:
2254 BUG_ON(1);
2255 break;
2256 }
2257 } else
2258 mad_error_handler(port_priv, &wc);
2259 }
2260}
2261
2262static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv)
2263{
2264 unsigned long flags;
2265 struct ib_mad_send_wr_private *mad_send_wr, *temp_mad_send_wr;
2266 struct ib_mad_send_wc mad_send_wc;
2267 struct list_head cancel_list;
2268
2269 INIT_LIST_HEAD(&cancel_list);
2270
2271 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2272 list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
2273 &mad_agent_priv->send_list, agent_list) {
2274 if (mad_send_wr->status == IB_WC_SUCCESS) {
Roland Dreier3cd96562006-09-22 15:22:46 -07002275 mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2277 }
2278 }
2279
2280 /* Empty wait list to prevent receives from finding a request */
2281 list_splice_init(&mad_agent_priv->wait_list, &cancel_list);
2282 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2283
2284 /* Report all cancelled requests */
2285 mad_send_wc.status = IB_WC_WR_FLUSH_ERR;
2286 mad_send_wc.vendor_err = 0;
2287
2288 list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
2289 &cancel_list, agent_list) {
Sean Hefty34816ad2005-10-25 10:51:39 -07002290 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2291 list_del(&mad_send_wr->agent_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2293 &mad_send_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 atomic_dec(&mad_agent_priv->refcount);
2295 }
2296}
2297
2298static struct ib_mad_send_wr_private*
Sean Hefty34816ad2005-10-25 10:51:39 -07002299find_send_wr(struct ib_mad_agent_private *mad_agent_priv,
2300 struct ib_mad_send_buf *send_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301{
2302 struct ib_mad_send_wr_private *mad_send_wr;
2303
2304 list_for_each_entry(mad_send_wr, &mad_agent_priv->wait_list,
2305 agent_list) {
Sean Hefty34816ad2005-10-25 10:51:39 -07002306 if (&mad_send_wr->send_buf == send_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 return mad_send_wr;
2308 }
2309
2310 list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list,
2311 agent_list) {
Sean Hefty34816ad2005-10-25 10:51:39 -07002312 if (is_data_mad(mad_agent_priv, mad_send_wr->send_buf.mad) &&
2313 &mad_send_wr->send_buf == send_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 return mad_send_wr;
2315 }
2316 return NULL;
2317}
2318
Sean Hefty34816ad2005-10-25 10:51:39 -07002319int ib_modify_mad(struct ib_mad_agent *mad_agent,
2320 struct ib_mad_send_buf *send_buf, u32 timeout_ms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321{
2322 struct ib_mad_agent_private *mad_agent_priv;
2323 struct ib_mad_send_wr_private *mad_send_wr;
2324 unsigned long flags;
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07002325 int active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
2328 agent);
2329 spin_lock_irqsave(&mad_agent_priv->lock, flags);
Sean Hefty34816ad2005-10-25 10:51:39 -07002330 mad_send_wr = find_send_wr(mad_agent_priv, send_buf);
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002331 if (!mad_send_wr || mad_send_wr->status != IB_WC_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002333 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 }
2335
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07002336 active = (!mad_send_wr->timeout || mad_send_wr->refcount > 1);
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002337 if (!timeout_ms) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002339 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 }
2341
Sean Hefty34816ad2005-10-25 10:51:39 -07002342 mad_send_wr->send_buf.timeout_ms = timeout_ms;
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07002343 if (active)
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002344 mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
2345 else
2346 ib_reset_mad_timeout(mad_send_wr, timeout_ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002348 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2349 return 0;
2350}
2351EXPORT_SYMBOL(ib_modify_mad);
2352
Sean Hefty34816ad2005-10-25 10:51:39 -07002353void ib_cancel_mad(struct ib_mad_agent *mad_agent,
2354 struct ib_mad_send_buf *send_buf)
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002355{
Sean Hefty34816ad2005-10-25 10:51:39 -07002356 ib_modify_mad(mad_agent, send_buf, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357}
2358EXPORT_SYMBOL(ib_cancel_mad);
2359
David Howellsc4028952006-11-22 14:57:56 +00002360static void local_completions(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361{
2362 struct ib_mad_agent_private *mad_agent_priv;
2363 struct ib_mad_local_private *local;
2364 struct ib_mad_agent_private *recv_mad_agent;
2365 unsigned long flags;
Ralph Campbell1d9bc6d62009-02-27 10:34:30 -08002366 int free_mad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 struct ib_wc wc;
2368 struct ib_mad_send_wc mad_send_wc;
2369
David Howellsc4028952006-11-22 14:57:56 +00002370 mad_agent_priv =
2371 container_of(work, struct ib_mad_agent_private, local_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
2373 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2374 while (!list_empty(&mad_agent_priv->local_list)) {
2375 local = list_entry(mad_agent_priv->local_list.next,
2376 struct ib_mad_local_private,
2377 completion_list);
Michael S. Tsirkin37289ef2006-03-30 15:52:54 +02002378 list_del(&local->completion_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
Ralph Campbell1d9bc6d62009-02-27 10:34:30 -08002380 free_mad = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 if (local->mad_priv) {
2382 recv_mad_agent = local->recv_mad_agent;
2383 if (!recv_mad_agent) {
2384 printk(KERN_ERR PFX "No receive MAD agent for local completion\n");
Ralph Campbell1d9bc6d62009-02-27 10:34:30 -08002385 free_mad = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 goto local_send_completion;
2387 }
2388
2389 /*
2390 * Defined behavior is to complete response
2391 * before request
2392 */
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +02002393 build_smp_wc(recv_mad_agent->agent.qp,
2394 (unsigned long) local->mad_send_wr,
Sean Hefty97f52eb2005-08-13 21:05:57 -07002395 be16_to_cpu(IB_LID_PERMISSIVE),
Sean Hefty34816ad2005-10-25 10:51:39 -07002396 0, recv_mad_agent->agent.port_num, &wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
2398 local->mad_priv->header.recv_wc.wc = &wc;
2399 local->mad_priv->header.recv_wc.mad_len =
2400 sizeof(struct ib_mad);
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002401 INIT_LIST_HEAD(&local->mad_priv->header.recv_wc.rmpp_list);
2402 list_add(&local->mad_priv->header.recv_wc.recv_buf.list,
2403 &local->mad_priv->header.recv_wc.rmpp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 local->mad_priv->header.recv_wc.recv_buf.grh = NULL;
2405 local->mad_priv->header.recv_wc.recv_buf.mad =
2406 &local->mad_priv->mad.mad;
2407 if (atomic_read(&recv_mad_agent->qp_info->snoop_count))
2408 snoop_recv(recv_mad_agent->qp_info,
2409 &local->mad_priv->header.recv_wc,
2410 IB_MAD_SNOOP_RECVS);
2411 recv_mad_agent->agent.recv_handler(
2412 &recv_mad_agent->agent,
2413 &local->mad_priv->header.recv_wc);
2414 spin_lock_irqsave(&recv_mad_agent->lock, flags);
2415 atomic_dec(&recv_mad_agent->refcount);
2416 spin_unlock_irqrestore(&recv_mad_agent->lock, flags);
2417 }
2418
2419local_send_completion:
2420 /* Complete send */
2421 mad_send_wc.status = IB_WC_SUCCESS;
2422 mad_send_wc.vendor_err = 0;
Sean Hefty34816ad2005-10-25 10:51:39 -07002423 mad_send_wc.send_buf = &local->mad_send_wr->send_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 if (atomic_read(&mad_agent_priv->qp_info->snoop_count))
Sean Hefty34816ad2005-10-25 10:51:39 -07002425 snoop_send(mad_agent_priv->qp_info,
2426 &local->mad_send_wr->send_buf,
2427 &mad_send_wc, IB_MAD_SNOOP_SEND_COMPLETIONS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2429 &mad_send_wc);
2430
2431 spin_lock_irqsave(&mad_agent_priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 atomic_dec(&mad_agent_priv->refcount);
Ralph Campbell1d9bc6d62009-02-27 10:34:30 -08002433 if (free_mad)
Hal Rosenstock2c153b92005-07-27 11:45:31 -07002434 kmem_cache_free(ib_mad_cache, local->mad_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 kfree(local);
2436 }
2437 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2438}
2439
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002440static int retry_send(struct ib_mad_send_wr_private *mad_send_wr)
2441{
2442 int ret;
2443
Sean Hefty4fc8cd42007-11-27 00:11:04 -08002444 if (!mad_send_wr->retries_left)
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002445 return -ETIMEDOUT;
2446
Sean Hefty4fc8cd42007-11-27 00:11:04 -08002447 mad_send_wr->retries_left--;
2448 mad_send_wr->send_buf.retries++;
2449
Sean Hefty34816ad2005-10-25 10:51:39 -07002450 mad_send_wr->timeout = msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms);
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002451
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002452 if (mad_send_wr->mad_agent_priv->agent.rmpp_version) {
2453 ret = ib_retry_rmpp(mad_send_wr);
2454 switch (ret) {
2455 case IB_RMPP_RESULT_UNHANDLED:
2456 ret = ib_send_mad(mad_send_wr);
2457 break;
2458 case IB_RMPP_RESULT_CONSUMED:
2459 ret = 0;
2460 break;
2461 default:
2462 ret = -ECOMM;
2463 break;
2464 }
2465 } else
2466 ret = ib_send_mad(mad_send_wr);
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002467
2468 if (!ret) {
2469 mad_send_wr->refcount++;
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002470 list_add_tail(&mad_send_wr->agent_list,
2471 &mad_send_wr->mad_agent_priv->send_list);
2472 }
2473 return ret;
2474}
2475
David Howellsc4028952006-11-22 14:57:56 +00002476static void timeout_sends(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477{
2478 struct ib_mad_agent_private *mad_agent_priv;
2479 struct ib_mad_send_wr_private *mad_send_wr;
2480 struct ib_mad_send_wc mad_send_wc;
2481 unsigned long flags, delay;
2482
David Howellsc4028952006-11-22 14:57:56 +00002483 mad_agent_priv = container_of(work, struct ib_mad_agent_private,
2484 timed_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 mad_send_wc.vendor_err = 0;
2486
2487 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2488 while (!list_empty(&mad_agent_priv->wait_list)) {
2489 mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
2490 struct ib_mad_send_wr_private,
2491 agent_list);
2492
2493 if (time_after(mad_send_wr->timeout, jiffies)) {
2494 delay = mad_send_wr->timeout - jiffies;
2495 if ((long)delay <= 0)
2496 delay = 1;
2497 queue_delayed_work(mad_agent_priv->qp_info->
2498 port_priv->wq,
2499 &mad_agent_priv->timed_work, delay);
2500 break;
2501 }
2502
Hal Rosenstockdbf92272005-07-27 11:45:30 -07002503 list_del(&mad_send_wr->agent_list);
Hal Rosenstock29bb33d2005-07-27 11:45:32 -07002504 if (mad_send_wr->status == IB_WC_SUCCESS &&
2505 !retry_send(mad_send_wr))
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002506 continue;
2507
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2509
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002510 if (mad_send_wr->status == IB_WC_SUCCESS)
2511 mad_send_wc.status = IB_WC_RESP_TIMEOUT_ERR;
2512 else
2513 mad_send_wc.status = mad_send_wr->status;
Sean Hefty34816ad2005-10-25 10:51:39 -07002514 mad_send_wc.send_buf = &mad_send_wr->send_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2516 &mad_send_wc);
2517
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 atomic_dec(&mad_agent_priv->refcount);
2519 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2520 }
2521 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2522}
2523
Hal Rosenstock5dd2ce12005-08-15 14:16:36 -07002524static void ib_mad_thread_completion_handler(struct ib_cq *cq, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525{
2526 struct ib_mad_port_private *port_priv = cq->cq_context;
Michael S. Tsirkindc059802006-03-20 10:08:25 -08002527 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
Michael S. Tsirkindc059802006-03-20 10:08:25 -08002529 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2530 if (!list_empty(&port_priv->port_list))
2531 queue_work(port_priv->wq, &port_priv->work);
2532 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533}
2534
2535/*
2536 * Allocate receive MADs and post receive WRs for them
2537 */
2538static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
2539 struct ib_mad_private *mad)
2540{
2541 unsigned long flags;
2542 int post, ret;
2543 struct ib_mad_private *mad_priv;
2544 struct ib_sge sg_list;
2545 struct ib_recv_wr recv_wr, *bad_recv_wr;
2546 struct ib_mad_queue *recv_queue = &qp_info->recv_queue;
2547
2548 /* Initialize common scatter list fields */
2549 sg_list.length = sizeof *mad_priv - sizeof mad_priv->header;
2550 sg_list.lkey = (*qp_info->port_priv->mr).lkey;
2551
2552 /* Initialize common receive WR fields */
2553 recv_wr.next = NULL;
2554 recv_wr.sg_list = &sg_list;
2555 recv_wr.num_sge = 1;
2556
2557 do {
2558 /* Allocate and map receive buffer */
2559 if (mad) {
2560 mad_priv = mad;
2561 mad = NULL;
2562 } else {
2563 mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL);
2564 if (!mad_priv) {
2565 printk(KERN_ERR PFX "No memory for receive buffer\n");
2566 ret = -ENOMEM;
2567 break;
2568 }
2569 }
Ralph Campbell15271062006-12-12 14:28:30 -08002570 sg_list.addr = ib_dma_map_single(qp_info->port_priv->device,
2571 &mad_priv->grh,
2572 sizeof *mad_priv -
2573 sizeof mad_priv->header,
2574 DMA_FROM_DEVICE);
2575 mad_priv->header.mapping = sg_list.addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 recv_wr.wr_id = (unsigned long)&mad_priv->header.mad_list;
2577 mad_priv->header.mad_list.mad_queue = recv_queue;
2578
2579 /* Post receive WR */
2580 spin_lock_irqsave(&recv_queue->lock, flags);
2581 post = (++recv_queue->count < recv_queue->max_active);
2582 list_add_tail(&mad_priv->header.mad_list.list, &recv_queue->list);
2583 spin_unlock_irqrestore(&recv_queue->lock, flags);
2584 ret = ib_post_recv(qp_info->qp, &recv_wr, &bad_recv_wr);
2585 if (ret) {
2586 spin_lock_irqsave(&recv_queue->lock, flags);
2587 list_del(&mad_priv->header.mad_list.list);
2588 recv_queue->count--;
2589 spin_unlock_irqrestore(&recv_queue->lock, flags);
Ralph Campbell15271062006-12-12 14:28:30 -08002590 ib_dma_unmap_single(qp_info->port_priv->device,
2591 mad_priv->header.mapping,
2592 sizeof *mad_priv -
2593 sizeof mad_priv->header,
2594 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 kmem_cache_free(ib_mad_cache, mad_priv);
2596 printk(KERN_ERR PFX "ib_post_recv failed: %d\n", ret);
2597 break;
2598 }
2599 } while (post);
2600
2601 return ret;
2602}
2603
2604/*
2605 * Return all the posted receive MADs
2606 */
2607static void cleanup_recv_queue(struct ib_mad_qp_info *qp_info)
2608{
2609 struct ib_mad_private_header *mad_priv_hdr;
2610 struct ib_mad_private *recv;
2611 struct ib_mad_list_head *mad_list;
2612
2613 while (!list_empty(&qp_info->recv_queue.list)) {
2614
2615 mad_list = list_entry(qp_info->recv_queue.list.next,
2616 struct ib_mad_list_head, list);
2617 mad_priv_hdr = container_of(mad_list,
2618 struct ib_mad_private_header,
2619 mad_list);
2620 recv = container_of(mad_priv_hdr, struct ib_mad_private,
2621 header);
2622
2623 /* Remove from posted receive MAD list */
2624 list_del(&mad_list->list);
2625
Ralph Campbell15271062006-12-12 14:28:30 -08002626 ib_dma_unmap_single(qp_info->port_priv->device,
2627 recv->header.mapping,
2628 sizeof(struct ib_mad_private) -
2629 sizeof(struct ib_mad_private_header),
2630 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 kmem_cache_free(ib_mad_cache, recv);
2632 }
2633
2634 qp_info->recv_queue.count = 0;
2635}
2636
2637/*
2638 * Start the port
2639 */
2640static int ib_mad_port_start(struct ib_mad_port_private *port_priv)
2641{
2642 int ret, i;
2643 struct ib_qp_attr *attr;
2644 struct ib_qp *qp;
2645
2646 attr = kmalloc(sizeof *attr, GFP_KERNEL);
Roland Dreier3cd96562006-09-22 15:22:46 -07002647 if (!attr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 printk(KERN_ERR PFX "Couldn't kmalloc ib_qp_attr\n");
2649 return -ENOMEM;
2650 }
2651
2652 for (i = 0; i < IB_MAD_QPS_CORE; i++) {
2653 qp = port_priv->qp_info[i].qp;
2654 /*
2655 * PKey index for QP1 is irrelevant but
2656 * one is needed for the Reset to Init transition
2657 */
2658 attr->qp_state = IB_QPS_INIT;
2659 attr->pkey_index = 0;
2660 attr->qkey = (qp->qp_num == 0) ? 0 : IB_QP1_QKEY;
2661 ret = ib_modify_qp(qp, attr, IB_QP_STATE |
2662 IB_QP_PKEY_INDEX | IB_QP_QKEY);
2663 if (ret) {
2664 printk(KERN_ERR PFX "Couldn't change QP%d state to "
2665 "INIT: %d\n", i, ret);
2666 goto out;
2667 }
2668
2669 attr->qp_state = IB_QPS_RTR;
2670 ret = ib_modify_qp(qp, attr, IB_QP_STATE);
2671 if (ret) {
2672 printk(KERN_ERR PFX "Couldn't change QP%d state to "
2673 "RTR: %d\n", i, ret);
2674 goto out;
2675 }
2676
2677 attr->qp_state = IB_QPS_RTS;
2678 attr->sq_psn = IB_MAD_SEND_Q_PSN;
2679 ret = ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_SQ_PSN);
2680 if (ret) {
2681 printk(KERN_ERR PFX "Couldn't change QP%d state to "
2682 "RTS: %d\n", i, ret);
2683 goto out;
2684 }
2685 }
2686
2687 ret = ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
2688 if (ret) {
2689 printk(KERN_ERR PFX "Failed to request completion "
2690 "notification: %d\n", ret);
2691 goto out;
2692 }
2693
2694 for (i = 0; i < IB_MAD_QPS_CORE; i++) {
2695 ret = ib_mad_post_receive_mads(&port_priv->qp_info[i], NULL);
2696 if (ret) {
2697 printk(KERN_ERR PFX "Couldn't post receive WRs\n");
2698 goto out;
2699 }
2700 }
2701out:
2702 kfree(attr);
2703 return ret;
2704}
2705
2706static void qp_event_handler(struct ib_event *event, void *qp_context)
2707{
2708 struct ib_mad_qp_info *qp_info = qp_context;
2709
2710 /* It's worse than that! He's dead, Jim! */
2711 printk(KERN_ERR PFX "Fatal error (%d) on MAD QP (%d)\n",
2712 event->event, qp_info->qp->qp_num);
2713}
2714
2715static void init_mad_queue(struct ib_mad_qp_info *qp_info,
2716 struct ib_mad_queue *mad_queue)
2717{
2718 mad_queue->qp_info = qp_info;
2719 mad_queue->count = 0;
2720 spin_lock_init(&mad_queue->lock);
2721 INIT_LIST_HEAD(&mad_queue->list);
2722}
2723
2724static void init_mad_qp(struct ib_mad_port_private *port_priv,
2725 struct ib_mad_qp_info *qp_info)
2726{
2727 qp_info->port_priv = port_priv;
2728 init_mad_queue(qp_info, &qp_info->send_queue);
2729 init_mad_queue(qp_info, &qp_info->recv_queue);
2730 INIT_LIST_HEAD(&qp_info->overflow_list);
2731 spin_lock_init(&qp_info->snoop_lock);
2732 qp_info->snoop_table = NULL;
2733 qp_info->snoop_table_size = 0;
2734 atomic_set(&qp_info->snoop_count, 0);
2735}
2736
2737static int create_mad_qp(struct ib_mad_qp_info *qp_info,
2738 enum ib_qp_type qp_type)
2739{
2740 struct ib_qp_init_attr qp_init_attr;
2741 int ret;
2742
2743 memset(&qp_init_attr, 0, sizeof qp_init_attr);
2744 qp_init_attr.send_cq = qp_info->port_priv->cq;
2745 qp_init_attr.recv_cq = qp_info->port_priv->cq;
2746 qp_init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
Hal Rosenstockb76aabc2009-09-07 08:28:48 -07002747 qp_init_attr.cap.max_send_wr = mad_sendq_size;
2748 qp_init_attr.cap.max_recv_wr = mad_recvq_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 qp_init_attr.cap.max_send_sge = IB_MAD_SEND_REQ_MAX_SG;
2750 qp_init_attr.cap.max_recv_sge = IB_MAD_RECV_REQ_MAX_SG;
2751 qp_init_attr.qp_type = qp_type;
2752 qp_init_attr.port_num = qp_info->port_priv->port_num;
2753 qp_init_attr.qp_context = qp_info;
2754 qp_init_attr.event_handler = qp_event_handler;
2755 qp_info->qp = ib_create_qp(qp_info->port_priv->pd, &qp_init_attr);
2756 if (IS_ERR(qp_info->qp)) {
2757 printk(KERN_ERR PFX "Couldn't create ib_mad QP%d\n",
2758 get_spl_qp_index(qp_type));
2759 ret = PTR_ERR(qp_info->qp);
2760 goto error;
2761 }
2762 /* Use minimum queue sizes unless the CQ is resized */
Hal Rosenstockb76aabc2009-09-07 08:28:48 -07002763 qp_info->send_queue.max_active = mad_sendq_size;
2764 qp_info->recv_queue.max_active = mad_recvq_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 return 0;
2766
2767error:
2768 return ret;
2769}
2770
2771static void destroy_mad_qp(struct ib_mad_qp_info *qp_info)
2772{
2773 ib_destroy_qp(qp_info->qp);
Jesper Juhl6044ec82005-11-07 01:01:32 -08002774 kfree(qp_info->snoop_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775}
2776
2777/*
2778 * Open the port
2779 * Create the QP, PD, MR, and CQ if needed
2780 */
2781static int ib_mad_port_open(struct ib_device *device,
2782 int port_num)
2783{
2784 int ret, cq_size;
2785 struct ib_mad_port_private *port_priv;
2786 unsigned long flags;
2787 char name[sizeof "ib_mad123"];
2788
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 /* Create new device info */
Roland Dreierde6eb662005-11-02 07:23:14 -08002790 port_priv = kzalloc(sizeof *port_priv, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 if (!port_priv) {
2792 printk(KERN_ERR PFX "No memory for ib_mad_port_private\n");
2793 return -ENOMEM;
2794 }
Roland Dreierde6eb662005-11-02 07:23:14 -08002795
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 port_priv->device = device;
2797 port_priv->port_num = port_num;
2798 spin_lock_init(&port_priv->reg_lock);
2799 INIT_LIST_HEAD(&port_priv->agent_list);
2800 init_mad_qp(port_priv, &port_priv->qp_info[0]);
2801 init_mad_qp(port_priv, &port_priv->qp_info[1]);
2802
Hal Rosenstockb76aabc2009-09-07 08:28:48 -07002803 cq_size = (mad_sendq_size + mad_recvq_size) * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 port_priv->cq = ib_create_cq(port_priv->device,
Hal Rosenstock5dd2ce12005-08-15 14:16:36 -07002805 ib_mad_thread_completion_handler,
Michael S. Tsirkinf4fd0b22007-05-03 13:48:47 +03002806 NULL, port_priv, cq_size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 if (IS_ERR(port_priv->cq)) {
2808 printk(KERN_ERR PFX "Couldn't create ib_mad CQ\n");
2809 ret = PTR_ERR(port_priv->cq);
2810 goto error3;
2811 }
2812
2813 port_priv->pd = ib_alloc_pd(device);
2814 if (IS_ERR(port_priv->pd)) {
2815 printk(KERN_ERR PFX "Couldn't create ib_mad PD\n");
2816 ret = PTR_ERR(port_priv->pd);
2817 goto error4;
2818 }
2819
2820 port_priv->mr = ib_get_dma_mr(port_priv->pd, IB_ACCESS_LOCAL_WRITE);
2821 if (IS_ERR(port_priv->mr)) {
2822 printk(KERN_ERR PFX "Couldn't get ib_mad DMA MR\n");
2823 ret = PTR_ERR(port_priv->mr);
2824 goto error5;
2825 }
2826
2827 ret = create_mad_qp(&port_priv->qp_info[0], IB_QPT_SMI);
2828 if (ret)
2829 goto error6;
2830 ret = create_mad_qp(&port_priv->qp_info[1], IB_QPT_GSI);
2831 if (ret)
2832 goto error7;
2833
2834 snprintf(name, sizeof name, "ib_mad%d", port_num);
2835 port_priv->wq = create_singlethread_workqueue(name);
2836 if (!port_priv->wq) {
2837 ret = -ENOMEM;
2838 goto error8;
2839 }
David Howellsc4028952006-11-22 14:57:56 +00002840 INIT_WORK(&port_priv->work, ib_mad_completion_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
Michael S. Tsirkindc059802006-03-20 10:08:25 -08002842 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2843 list_add_tail(&port_priv->port_list, &ib_mad_port_list);
2844 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2845
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 ret = ib_mad_port_start(port_priv);
2847 if (ret) {
2848 printk(KERN_ERR PFX "Couldn't start port\n");
2849 goto error9;
2850 }
2851
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 return 0;
2853
2854error9:
Michael S. Tsirkindc059802006-03-20 10:08:25 -08002855 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2856 list_del_init(&port_priv->port_list);
2857 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2858
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 destroy_workqueue(port_priv->wq);
2860error8:
2861 destroy_mad_qp(&port_priv->qp_info[1]);
2862error7:
2863 destroy_mad_qp(&port_priv->qp_info[0]);
2864error6:
2865 ib_dereg_mr(port_priv->mr);
2866error5:
2867 ib_dealloc_pd(port_priv->pd);
2868error4:
2869 ib_destroy_cq(port_priv->cq);
2870 cleanup_recv_queue(&port_priv->qp_info[1]);
2871 cleanup_recv_queue(&port_priv->qp_info[0]);
2872error3:
2873 kfree(port_priv);
2874
2875 return ret;
2876}
2877
2878/*
2879 * Close the port
2880 * If there are no classes using the port, free the port
2881 * resources (CQ, MR, PD, QP) and remove the port's info structure
2882 */
2883static int ib_mad_port_close(struct ib_device *device, int port_num)
2884{
2885 struct ib_mad_port_private *port_priv;
2886 unsigned long flags;
2887
2888 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2889 port_priv = __ib_get_mad_port(device, port_num);
2890 if (port_priv == NULL) {
2891 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2892 printk(KERN_ERR PFX "Port %d not found\n", port_num);
2893 return -ENODEV;
2894 }
Michael S. Tsirkindc059802006-03-20 10:08:25 -08002895 list_del_init(&port_priv->port_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2897
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 destroy_workqueue(port_priv->wq);
2899 destroy_mad_qp(&port_priv->qp_info[1]);
2900 destroy_mad_qp(&port_priv->qp_info[0]);
2901 ib_dereg_mr(port_priv->mr);
2902 ib_dealloc_pd(port_priv->pd);
2903 ib_destroy_cq(port_priv->cq);
2904 cleanup_recv_queue(&port_priv->qp_info[1]);
2905 cleanup_recv_queue(&port_priv->qp_info[0]);
2906 /* XXX: Handle deallocation of MAD registration tables */
2907
2908 kfree(port_priv);
2909
2910 return 0;
2911}
2912
2913static void ib_mad_init_device(struct ib_device *device)
2914{
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002915 int start, end, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
Tom Tucker07ebafb2006-08-03 16:02:42 -05002917 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
2918 return;
2919
2920 if (device->node_type == RDMA_NODE_IB_SWITCH) {
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002921 start = 0;
2922 end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 } else {
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002924 start = 1;
2925 end = device->phys_port_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 }
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002927
2928 for (i = start; i <= end; i++) {
2929 if (ib_mad_port_open(device, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 printk(KERN_ERR PFX "Couldn't open %s port %d\n",
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002931 device->name, i);
2932 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 }
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002934 if (ib_agent_port_open(device, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 printk(KERN_ERR PFX "Couldn't open %s port %d "
2936 "for agents\n",
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002937 device->name, i);
2938 goto error_agent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 }
2940 }
Hal Rosenstockf68bcc22005-07-27 11:45:27 -07002941 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002943error_agent:
2944 if (ib_mad_port_close(device, i))
2945 printk(KERN_ERR PFX "Couldn't close %s port %d\n",
2946 device->name, i);
2947
2948error:
2949 i--;
2950
2951 while (i >= start) {
2952 if (ib_agent_port_close(device, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 printk(KERN_ERR PFX "Couldn't close %s port %d "
2954 "for agents\n",
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002955 device->name, i);
2956 if (ib_mad_port_close(device, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 printk(KERN_ERR PFX "Couldn't close %s port %d\n",
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002958 device->name, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 i--;
2960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961}
2962
2963static void ib_mad_remove_device(struct ib_device *device)
2964{
Hal Rosenstockf68bcc22005-07-27 11:45:27 -07002965 int i, num_ports, cur_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
Tom Tucker07ebafb2006-08-03 16:02:42 -05002967 if (device->node_type == RDMA_NODE_IB_SWITCH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 num_ports = 1;
2969 cur_port = 0;
2970 } else {
2971 num_ports = device->phys_port_cnt;
2972 cur_port = 1;
2973 }
2974 for (i = 0; i < num_ports; i++, cur_port++) {
Hal Rosenstockf68bcc22005-07-27 11:45:27 -07002975 if (ib_agent_port_close(device, cur_port))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 printk(KERN_ERR PFX "Couldn't close %s port %d "
2977 "for agents\n",
2978 device->name, cur_port);
Hal Rosenstockf68bcc22005-07-27 11:45:27 -07002979 if (ib_mad_port_close(device, cur_port))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 printk(KERN_ERR PFX "Couldn't close %s port %d\n",
2981 device->name, cur_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 }
2983}
2984
2985static struct ib_client mad_client = {
2986 .name = "mad",
2987 .add = ib_mad_init_device,
2988 .remove = ib_mad_remove_device
2989};
2990
2991static int __init ib_mad_init_module(void)
2992{
2993 int ret;
2994
Hal Rosenstockb76aabc2009-09-07 08:28:48 -07002995 mad_recvq_size = min(mad_recvq_size, IB_MAD_QP_MAX_SIZE);
2996 mad_recvq_size = max(mad_recvq_size, IB_MAD_QP_MIN_SIZE);
2997
2998 mad_sendq_size = min(mad_sendq_size, IB_MAD_QP_MAX_SIZE);
2999 mad_sendq_size = max(mad_sendq_size, IB_MAD_QP_MIN_SIZE);
3000
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 ib_mad_cache = kmem_cache_create("ib_mad",
3002 sizeof(struct ib_mad_private),
3003 0,
3004 SLAB_HWCACHE_ALIGN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 NULL);
3006 if (!ib_mad_cache) {
3007 printk(KERN_ERR PFX "Couldn't create ib_mad cache\n");
3008 ret = -ENOMEM;
3009 goto error1;
3010 }
3011
3012 INIT_LIST_HEAD(&ib_mad_port_list);
3013
3014 if (ib_register_client(&mad_client)) {
3015 printk(KERN_ERR PFX "Couldn't register ib_mad client\n");
3016 ret = -EINVAL;
3017 goto error2;
3018 }
3019
3020 return 0;
3021
3022error2:
3023 kmem_cache_destroy(ib_mad_cache);
3024error1:
3025 return ret;
3026}
3027
3028static void __exit ib_mad_cleanup_module(void)
3029{
3030 ib_unregister_client(&mad_client);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07003031 kmem_cache_destroy(ib_mad_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032}
3033
3034module_init(ib_mad_init_module);
3035module_exit(ib_mad_cleanup_module);