blob: ba54c856b0e5b98881320694cba80d3b333258f3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2004, 2005 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33 *
Jack Morgensteinf36e1792006-03-03 21:54:13 -080034 * $Id: mad.c 5596 2006-03-03 01:00:07Z sean.hefty $
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include "mad_priv.h"
Hal Rosenstockfa619a72005-07-27 11:45:37 -070039#include "mad_rmpp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "smi.h"
41#include "agent.h"
42
43MODULE_LICENSE("Dual BSD/GPL");
44MODULE_DESCRIPTION("kernel IB MAD API");
45MODULE_AUTHOR("Hal Rosenstock");
46MODULE_AUTHOR("Sean Hefty");
47
48
49kmem_cache_t *ib_mad_cache;
Hal Rosenstockfa619a72005-07-27 11:45:37 -070050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static struct list_head ib_mad_port_list;
52static u32 ib_mad_client_id = 0;
53
54/* Port list lock */
55static spinlock_t ib_mad_port_list_lock;
56
57
58/* Forward declarations */
59static int method_in_use(struct ib_mad_mgmt_method_table **method,
60 struct ib_mad_reg_req *mad_reg_req);
61static void remove_mad_reg_req(struct ib_mad_agent_private *priv);
62static struct ib_mad_agent_private *find_mad_agent(
63 struct ib_mad_port_private *port_priv,
Hal Rosenstock4a0754f2005-07-27 11:45:24 -070064 struct ib_mad *mad);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
66 struct ib_mad_private *mad);
67static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static void timeout_sends(void *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static void local_completions(void *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
71 struct ib_mad_agent_private *agent_priv,
72 u8 mgmt_class);
73static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
74 struct ib_mad_agent_private *agent_priv);
75
76/*
77 * Returns a ib_mad_port_private structure or NULL for a device/port
78 * Assumes ib_mad_port_list_lock is being held
79 */
80static inline struct ib_mad_port_private *
81__ib_get_mad_port(struct ib_device *device, int port_num)
82{
83 struct ib_mad_port_private *entry;
84
85 list_for_each_entry(entry, &ib_mad_port_list, port_list) {
86 if (entry->device == device && entry->port_num == port_num)
87 return entry;
88 }
89 return NULL;
90}
91
92/*
93 * Wrapper function to return a ib_mad_port_private structure or NULL
94 * for a device/port
95 */
96static inline struct ib_mad_port_private *
97ib_get_mad_port(struct ib_device *device, int port_num)
98{
99 struct ib_mad_port_private *entry;
100 unsigned long flags;
101
102 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
103 entry = __ib_get_mad_port(device, port_num);
104 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
105
106 return entry;
107}
108
109static inline u8 convert_mgmt_class(u8 mgmt_class)
110{
111 /* Alias IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE to 0 */
112 return mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE ?
113 0 : mgmt_class;
114}
115
116static int get_spl_qp_index(enum ib_qp_type qp_type)
117{
118 switch (qp_type)
119 {
120 case IB_QPT_SMI:
121 return 0;
122 case IB_QPT_GSI:
123 return 1;
124 default:
125 return -1;
126 }
127}
128
129static int vendor_class_index(u8 mgmt_class)
130{
131 return mgmt_class - IB_MGMT_CLASS_VENDOR_RANGE2_START;
132}
133
134static int is_vendor_class(u8 mgmt_class)
135{
136 if ((mgmt_class < IB_MGMT_CLASS_VENDOR_RANGE2_START) ||
137 (mgmt_class > IB_MGMT_CLASS_VENDOR_RANGE2_END))
138 return 0;
139 return 1;
140}
141
142static int is_vendor_oui(char *oui)
143{
144 if (oui[0] || oui[1] || oui[2])
145 return 1;
146 return 0;
147}
148
149static int is_vendor_method_in_use(
150 struct ib_mad_mgmt_vendor_class *vendor_class,
151 struct ib_mad_reg_req *mad_reg_req)
152{
153 struct ib_mad_mgmt_method_table *method;
154 int i;
155
156 for (i = 0; i < MAX_MGMT_OUI; i++) {
157 if (!memcmp(vendor_class->oui[i], mad_reg_req->oui, 3)) {
158 method = vendor_class->method_table[i];
159 if (method) {
160 if (method_in_use(&method, mad_reg_req))
161 return 1;
162 else
163 break;
164 }
165 }
166 }
167 return 0;
168}
169
170/*
171 * ib_register_mad_agent - Register to send/receive MADs
172 */
173struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
174 u8 port_num,
175 enum ib_qp_type qp_type,
176 struct ib_mad_reg_req *mad_reg_req,
177 u8 rmpp_version,
178 ib_mad_send_handler send_handler,
179 ib_mad_recv_handler recv_handler,
180 void *context)
181{
182 struct ib_mad_port_private *port_priv;
183 struct ib_mad_agent *ret = ERR_PTR(-EINVAL);
184 struct ib_mad_agent_private *mad_agent_priv;
185 struct ib_mad_reg_req *reg_req = NULL;
186 struct ib_mad_mgmt_class_table *class;
187 struct ib_mad_mgmt_vendor_class_table *vendor;
188 struct ib_mad_mgmt_vendor_class *vendor_class;
189 struct ib_mad_mgmt_method_table *method;
190 int ret2, qpn;
191 unsigned long flags;
192 u8 mgmt_class, vclass;
193
194 /* Validate parameters */
195 qpn = get_spl_qp_index(qp_type);
196 if (qpn == -1)
197 goto error1;
198
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700199 if (rmpp_version && rmpp_version != IB_MGMT_RMPP_VERSION)
200 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 /* Validate MAD registration request if supplied */
203 if (mad_reg_req) {
204 if (mad_reg_req->mgmt_class_version >= MAX_MGMT_VERSION)
205 goto error1;
206 if (!recv_handler)
207 goto error1;
208 if (mad_reg_req->mgmt_class >= MAX_MGMT_CLASS) {
209 /*
210 * IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE is the only
211 * one in this range currently allowed
212 */
213 if (mad_reg_req->mgmt_class !=
214 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
215 goto error1;
216 } else if (mad_reg_req->mgmt_class == 0) {
217 /*
218 * Class 0 is reserved in IBA and is used for
219 * aliasing of IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
220 */
221 goto error1;
222 } else if (is_vendor_class(mad_reg_req->mgmt_class)) {
223 /*
224 * If class is in "new" vendor range,
225 * ensure supplied OUI is not zero
226 */
227 if (!is_vendor_oui(mad_reg_req->oui))
228 goto error1;
229 }
Hal Rosenstock618a3c02006-03-28 16:40:04 -0800230 /* Make sure class supplied is consistent with RMPP */
231 if (ib_is_mad_class_rmpp(mad_reg_req->mgmt_class)) {
232 if (!rmpp_version)
233 goto error1;
234 } else {
235 if (rmpp_version)
236 goto error1;
237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 /* Make sure class supplied is consistent with QP type */
239 if (qp_type == IB_QPT_SMI) {
240 if ((mad_reg_req->mgmt_class !=
241 IB_MGMT_CLASS_SUBN_LID_ROUTED) &&
242 (mad_reg_req->mgmt_class !=
243 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE))
244 goto error1;
245 } else {
246 if ((mad_reg_req->mgmt_class ==
247 IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
248 (mad_reg_req->mgmt_class ==
249 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE))
250 goto error1;
251 }
252 } else {
253 /* No registration request supplied */
254 if (!send_handler)
255 goto error1;
256 }
257
258 /* Validate device and port */
259 port_priv = ib_get_mad_port(device, port_num);
260 if (!port_priv) {
261 ret = ERR_PTR(-ENODEV);
262 goto error1;
263 }
264
265 /* Allocate structures */
Roland Dreierde6eb662005-11-02 07:23:14 -0800266 mad_agent_priv = kzalloc(sizeof *mad_agent_priv, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (!mad_agent_priv) {
268 ret = ERR_PTR(-ENOMEM);
269 goto error1;
270 }
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700271
272 mad_agent_priv->agent.mr = ib_get_dma_mr(port_priv->qp_info[qpn].qp->pd,
273 IB_ACCESS_LOCAL_WRITE);
274 if (IS_ERR(mad_agent_priv->agent.mr)) {
275 ret = ERR_PTR(-ENOMEM);
276 goto error2;
277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 if (mad_reg_req) {
280 reg_req = kmalloc(sizeof *reg_req, GFP_KERNEL);
281 if (!reg_req) {
282 ret = ERR_PTR(-ENOMEM);
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700283 goto error3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285 /* Make a copy of the MAD registration request */
286 memcpy(reg_req, mad_reg_req, sizeof *reg_req);
287 }
288
289 /* Now, fill in the various structures */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 mad_agent_priv->qp_info = &port_priv->qp_info[qpn];
291 mad_agent_priv->reg_req = reg_req;
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700292 mad_agent_priv->agent.rmpp_version = rmpp_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 mad_agent_priv->agent.device = device;
294 mad_agent_priv->agent.recv_handler = recv_handler;
295 mad_agent_priv->agent.send_handler = send_handler;
296 mad_agent_priv->agent.context = context;
297 mad_agent_priv->agent.qp = port_priv->qp_info[qpn].qp;
298 mad_agent_priv->agent.port_num = port_num;
299
300 spin_lock_irqsave(&port_priv->reg_lock, flags);
301 mad_agent_priv->agent.hi_tid = ++ib_mad_client_id;
302
303 /*
304 * Make sure MAD registration (if supplied)
305 * is non overlapping with any existing ones
306 */
307 if (mad_reg_req) {
308 mgmt_class = convert_mgmt_class(mad_reg_req->mgmt_class);
309 if (!is_vendor_class(mgmt_class)) {
310 class = port_priv->version[mad_reg_req->
311 mgmt_class_version].class;
312 if (class) {
313 method = class->method_table[mgmt_class];
314 if (method) {
315 if (method_in_use(&method,
316 mad_reg_req))
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700317 goto error4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
319 }
320 ret2 = add_nonoui_reg_req(mad_reg_req, mad_agent_priv,
321 mgmt_class);
322 } else {
323 /* "New" vendor class range */
324 vendor = port_priv->version[mad_reg_req->
325 mgmt_class_version].vendor;
326 if (vendor) {
327 vclass = vendor_class_index(mgmt_class);
328 vendor_class = vendor->vendor_class[vclass];
329 if (vendor_class) {
330 if (is_vendor_method_in_use(
331 vendor_class,
332 mad_reg_req))
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700333 goto error4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335 }
336 ret2 = add_oui_reg_req(mad_reg_req, mad_agent_priv);
337 }
338 if (ret2) {
339 ret = ERR_PTR(ret2);
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700340 goto error4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342 }
343
344 /* Add mad agent into port's agent list */
345 list_add_tail(&mad_agent_priv->agent_list, &port_priv->agent_list);
346 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
347
348 spin_lock_init(&mad_agent_priv->lock);
349 INIT_LIST_HEAD(&mad_agent_priv->send_list);
350 INIT_LIST_HEAD(&mad_agent_priv->wait_list);
Hal Rosenstock6a0c4352005-07-27 11:45:26 -0700351 INIT_LIST_HEAD(&mad_agent_priv->done_list);
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700352 INIT_LIST_HEAD(&mad_agent_priv->rmpp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 INIT_WORK(&mad_agent_priv->timed_work, timeout_sends, mad_agent_priv);
354 INIT_LIST_HEAD(&mad_agent_priv->local_list);
355 INIT_WORK(&mad_agent_priv->local_work, local_completions,
356 mad_agent_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 atomic_set(&mad_agent_priv->refcount, 1);
358 init_waitqueue_head(&mad_agent_priv->wait);
359
360 return &mad_agent_priv->agent;
361
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700362error4:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
364 kfree(reg_req);
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700365error3:
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700366 ib_dereg_mr(mad_agent_priv->agent.mr);
Adrian Bunk2012a112005-11-27 00:37:36 +0100367error2:
368 kfree(mad_agent_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369error1:
370 return ret;
371}
372EXPORT_SYMBOL(ib_register_mad_agent);
373
374static inline int is_snooping_sends(int mad_snoop_flags)
375{
376 return (mad_snoop_flags &
377 (/*IB_MAD_SNOOP_POSTED_SENDS |
378 IB_MAD_SNOOP_RMPP_SENDS |*/
379 IB_MAD_SNOOP_SEND_COMPLETIONS /*|
380 IB_MAD_SNOOP_RMPP_SEND_COMPLETIONS*/));
381}
382
383static inline int is_snooping_recvs(int mad_snoop_flags)
384{
385 return (mad_snoop_flags &
386 (IB_MAD_SNOOP_RECVS /*|
387 IB_MAD_SNOOP_RMPP_RECVS*/));
388}
389
390static int register_snoop_agent(struct ib_mad_qp_info *qp_info,
391 struct ib_mad_snoop_private *mad_snoop_priv)
392{
393 struct ib_mad_snoop_private **new_snoop_table;
394 unsigned long flags;
395 int i;
396
397 spin_lock_irqsave(&qp_info->snoop_lock, flags);
398 /* Check for empty slot in array. */
399 for (i = 0; i < qp_info->snoop_table_size; i++)
400 if (!qp_info->snoop_table[i])
401 break;
402
403 if (i == qp_info->snoop_table_size) {
404 /* Grow table. */
405 new_snoop_table = kmalloc(sizeof mad_snoop_priv *
406 qp_info->snoop_table_size + 1,
407 GFP_ATOMIC);
408 if (!new_snoop_table) {
409 i = -ENOMEM;
410 goto out;
411 }
412 if (qp_info->snoop_table) {
413 memcpy(new_snoop_table, qp_info->snoop_table,
414 sizeof mad_snoop_priv *
415 qp_info->snoop_table_size);
416 kfree(qp_info->snoop_table);
417 }
418 qp_info->snoop_table = new_snoop_table;
419 qp_info->snoop_table_size++;
420 }
421 qp_info->snoop_table[i] = mad_snoop_priv;
422 atomic_inc(&qp_info->snoop_count);
423out:
424 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
425 return i;
426}
427
428struct ib_mad_agent *ib_register_mad_snoop(struct ib_device *device,
429 u8 port_num,
430 enum ib_qp_type qp_type,
431 int mad_snoop_flags,
432 ib_mad_snoop_handler snoop_handler,
433 ib_mad_recv_handler recv_handler,
434 void *context)
435{
436 struct ib_mad_port_private *port_priv;
437 struct ib_mad_agent *ret;
438 struct ib_mad_snoop_private *mad_snoop_priv;
439 int qpn;
440
441 /* Validate parameters */
442 if ((is_snooping_sends(mad_snoop_flags) && !snoop_handler) ||
443 (is_snooping_recvs(mad_snoop_flags) && !recv_handler)) {
444 ret = ERR_PTR(-EINVAL);
445 goto error1;
446 }
447 qpn = get_spl_qp_index(qp_type);
448 if (qpn == -1) {
449 ret = ERR_PTR(-EINVAL);
450 goto error1;
451 }
452 port_priv = ib_get_mad_port(device, port_num);
453 if (!port_priv) {
454 ret = ERR_PTR(-ENODEV);
455 goto error1;
456 }
457 /* Allocate structures */
Roland Dreierde6eb662005-11-02 07:23:14 -0800458 mad_snoop_priv = kzalloc(sizeof *mad_snoop_priv, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (!mad_snoop_priv) {
460 ret = ERR_PTR(-ENOMEM);
461 goto error1;
462 }
463
464 /* Now, fill in the various structures */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 mad_snoop_priv->qp_info = &port_priv->qp_info[qpn];
466 mad_snoop_priv->agent.device = device;
467 mad_snoop_priv->agent.recv_handler = recv_handler;
468 mad_snoop_priv->agent.snoop_handler = snoop_handler;
469 mad_snoop_priv->agent.context = context;
470 mad_snoop_priv->agent.qp = port_priv->qp_info[qpn].qp;
471 mad_snoop_priv->agent.port_num = port_num;
472 mad_snoop_priv->mad_snoop_flags = mad_snoop_flags;
473 init_waitqueue_head(&mad_snoop_priv->wait);
474 mad_snoop_priv->snoop_index = register_snoop_agent(
475 &port_priv->qp_info[qpn],
476 mad_snoop_priv);
477 if (mad_snoop_priv->snoop_index < 0) {
478 ret = ERR_PTR(mad_snoop_priv->snoop_index);
479 goto error2;
480 }
481
482 atomic_set(&mad_snoop_priv->refcount, 1);
483 return &mad_snoop_priv->agent;
484
485error2:
486 kfree(mad_snoop_priv);
487error1:
488 return ret;
489}
490EXPORT_SYMBOL(ib_register_mad_snoop);
491
492static void unregister_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
493{
494 struct ib_mad_port_private *port_priv;
495 unsigned long flags;
496
497 /* Note that we could still be handling received MADs */
498
499 /*
500 * Canceling all sends results in dropping received response
501 * MADs, preventing us from queuing additional work
502 */
503 cancel_mads(mad_agent_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 port_priv = mad_agent_priv->qp_info->port_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 cancel_delayed_work(&mad_agent_priv->timed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 spin_lock_irqsave(&port_priv->reg_lock, flags);
508 remove_mad_reg_req(mad_agent_priv);
509 list_del(&mad_agent_priv->agent_list);
510 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
511
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700512 flush_workqueue(port_priv->wq);
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700513 ib_cancel_rmpp_recvs(mad_agent_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 atomic_dec(&mad_agent_priv->refcount);
516 wait_event(mad_agent_priv->wait,
517 !atomic_read(&mad_agent_priv->refcount));
518
Jesper Juhl6044ec82005-11-07 01:01:32 -0800519 kfree(mad_agent_priv->reg_req);
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700520 ib_dereg_mr(mad_agent_priv->agent.mr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 kfree(mad_agent_priv);
522}
523
524static void unregister_mad_snoop(struct ib_mad_snoop_private *mad_snoop_priv)
525{
526 struct ib_mad_qp_info *qp_info;
527 unsigned long flags;
528
529 qp_info = mad_snoop_priv->qp_info;
530 spin_lock_irqsave(&qp_info->snoop_lock, flags);
531 qp_info->snoop_table[mad_snoop_priv->snoop_index] = NULL;
532 atomic_dec(&qp_info->snoop_count);
533 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
534
535 atomic_dec(&mad_snoop_priv->refcount);
536 wait_event(mad_snoop_priv->wait,
537 !atomic_read(&mad_snoop_priv->refcount));
538
539 kfree(mad_snoop_priv);
540}
541
542/*
543 * ib_unregister_mad_agent - Unregisters a client from using MAD services
544 */
545int ib_unregister_mad_agent(struct ib_mad_agent *mad_agent)
546{
547 struct ib_mad_agent_private *mad_agent_priv;
548 struct ib_mad_snoop_private *mad_snoop_priv;
549
550 /* If the TID is zero, the agent can only snoop. */
551 if (mad_agent->hi_tid) {
552 mad_agent_priv = container_of(mad_agent,
553 struct ib_mad_agent_private,
554 agent);
555 unregister_mad_agent(mad_agent_priv);
556 } else {
557 mad_snoop_priv = container_of(mad_agent,
558 struct ib_mad_snoop_private,
559 agent);
560 unregister_mad_snoop(mad_snoop_priv);
561 }
562 return 0;
563}
564EXPORT_SYMBOL(ib_unregister_mad_agent);
565
Hal Rosenstock4a0754f2005-07-27 11:45:24 -0700566static inline int response_mad(struct ib_mad *mad)
567{
568 /* Trap represses are responses although response bit is reset */
569 return ((mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS) ||
570 (mad->mad_hdr.method & IB_MGMT_METHOD_RESP));
571}
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573static void dequeue_mad(struct ib_mad_list_head *mad_list)
574{
575 struct ib_mad_queue *mad_queue;
576 unsigned long flags;
577
578 BUG_ON(!mad_list->mad_queue);
579 mad_queue = mad_list->mad_queue;
580 spin_lock_irqsave(&mad_queue->lock, flags);
581 list_del(&mad_list->list);
582 mad_queue->count--;
583 spin_unlock_irqrestore(&mad_queue->lock, flags);
584}
585
586static void snoop_send(struct ib_mad_qp_info *qp_info,
Sean Hefty34816ad2005-10-25 10:51:39 -0700587 struct ib_mad_send_buf *send_buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 struct ib_mad_send_wc *mad_send_wc,
589 int mad_snoop_flags)
590{
591 struct ib_mad_snoop_private *mad_snoop_priv;
592 unsigned long flags;
593 int i;
594
595 spin_lock_irqsave(&qp_info->snoop_lock, flags);
596 for (i = 0; i < qp_info->snoop_table_size; i++) {
597 mad_snoop_priv = qp_info->snoop_table[i];
598 if (!mad_snoop_priv ||
599 !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
600 continue;
601
602 atomic_inc(&mad_snoop_priv->refcount);
603 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
604 mad_snoop_priv->agent.snoop_handler(&mad_snoop_priv->agent,
Sean Hefty34816ad2005-10-25 10:51:39 -0700605 send_buf, mad_send_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (atomic_dec_and_test(&mad_snoop_priv->refcount))
607 wake_up(&mad_snoop_priv->wait);
608 spin_lock_irqsave(&qp_info->snoop_lock, flags);
609 }
610 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
611}
612
613static void snoop_recv(struct ib_mad_qp_info *qp_info,
614 struct ib_mad_recv_wc *mad_recv_wc,
615 int mad_snoop_flags)
616{
617 struct ib_mad_snoop_private *mad_snoop_priv;
618 unsigned long flags;
619 int i;
620
621 spin_lock_irqsave(&qp_info->snoop_lock, flags);
622 for (i = 0; i < qp_info->snoop_table_size; i++) {
623 mad_snoop_priv = qp_info->snoop_table[i];
624 if (!mad_snoop_priv ||
625 !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
626 continue;
627
628 atomic_inc(&mad_snoop_priv->refcount);
629 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
630 mad_snoop_priv->agent.recv_handler(&mad_snoop_priv->agent,
631 mad_recv_wc);
632 if (atomic_dec_and_test(&mad_snoop_priv->refcount))
633 wake_up(&mad_snoop_priv->wait);
634 spin_lock_irqsave(&qp_info->snoop_lock, flags);
635 }
636 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
637}
638
639static void build_smp_wc(u64 wr_id, u16 slid, u16 pkey_index, u8 port_num,
640 struct ib_wc *wc)
641{
642 memset(wc, 0, sizeof *wc);
643 wc->wr_id = wr_id;
644 wc->status = IB_WC_SUCCESS;
645 wc->opcode = IB_WC_RECV;
646 wc->pkey_index = pkey_index;
647 wc->byte_len = sizeof(struct ib_mad) + sizeof(struct ib_grh);
648 wc->src_qp = IB_QP0;
649 wc->qp_num = IB_QP0;
650 wc->slid = slid;
651 wc->sl = 0;
652 wc->dlid_path_bits = 0;
653 wc->port_num = port_num;
654}
655
656/*
657 * Return 0 if SMP is to be sent
658 * Return 1 if SMP was consumed locally (whether or not solicited)
659 * Return < 0 if error
660 */
661static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
Sean Hefty34816ad2005-10-25 10:51:39 -0700662 struct ib_mad_send_wr_private *mad_send_wr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Hal Rosenstock4a0754f2005-07-27 11:45:24 -0700664 int ret;
Sean Hefty34816ad2005-10-25 10:51:39 -0700665 struct ib_smp *smp = mad_send_wr->send_buf.mad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 unsigned long flags;
667 struct ib_mad_local_private *local;
668 struct ib_mad_private *mad_priv;
669 struct ib_mad_port_private *port_priv;
670 struct ib_mad_agent_private *recv_mad_agent = NULL;
671 struct ib_device *device = mad_agent_priv->agent.device;
672 u8 port_num = mad_agent_priv->agent.port_num;
673 struct ib_wc mad_wc;
Sean Hefty34816ad2005-10-25 10:51:39 -0700674 struct ib_send_wr *send_wr = &mad_send_wr->send_wr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Ralph Campbell8cf3f042006-02-03 14:28:48 -0800676 /*
677 * Directed route handling starts if the initial LID routed part of
678 * a request or the ending LID routed part of a response is empty.
679 * If we are at the start of the LID routed part, don't update the
680 * hop_ptr or hop_cnt. See section 14.2.2, Vol 1 IB spec.
681 */
682 if ((ib_get_smp_direction(smp) ? smp->dr_dlid : smp->dr_slid) ==
683 IB_LID_PERMISSIVE &&
684 !smi_handle_dr_smp_send(smp, device->node_type, port_num)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 ret = -EINVAL;
686 printk(KERN_ERR PFX "Invalid directed route\n");
687 goto out;
688 }
689 /* Check to post send on QP or process locally */
Ralph Campbell5e9f71a2006-02-03 14:32:01 -0800690 ret = smi_check_local_smp(smp, device);
691 if (!ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 goto out;
693
694 local = kmalloc(sizeof *local, GFP_ATOMIC);
695 if (!local) {
696 ret = -ENOMEM;
697 printk(KERN_ERR PFX "No memory for ib_mad_local_private\n");
698 goto out;
699 }
700 local->mad_priv = NULL;
701 local->recv_mad_agent = NULL;
702 mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_ATOMIC);
703 if (!mad_priv) {
704 ret = -ENOMEM;
705 printk(KERN_ERR PFX "No memory for local response MAD\n");
706 kfree(local);
707 goto out;
708 }
709
Sean Hefty97f52eb2005-08-13 21:05:57 -0700710 build_smp_wc(send_wr->wr_id, be16_to_cpu(smp->dr_slid),
711 send_wr->wr.ud.pkey_index,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 send_wr->wr.ud.port_num, &mad_wc);
713
714 /* No GRH for DR SMP */
715 ret = device->process_mad(device, 0, port_num, &mad_wc, NULL,
716 (struct ib_mad *)smp,
717 (struct ib_mad *)&mad_priv->mad);
718 switch (ret)
719 {
720 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY:
Hal Rosenstock4a0754f2005-07-27 11:45:24 -0700721 if (response_mad(&mad_priv->mad.mad) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 mad_agent_priv->agent.recv_handler) {
723 local->mad_priv = mad_priv;
724 local->recv_mad_agent = mad_agent_priv;
725 /*
726 * Reference MAD agent until receive
727 * side of local completion handled
728 */
729 atomic_inc(&mad_agent_priv->refcount);
730 } else
731 kmem_cache_free(ib_mad_cache, mad_priv);
732 break;
733 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED:
734 kmem_cache_free(ib_mad_cache, mad_priv);
735 break;
736 case IB_MAD_RESULT_SUCCESS:
737 /* Treat like an incoming receive MAD */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 port_priv = ib_get_mad_port(mad_agent_priv->agent.device,
739 mad_agent_priv->agent.port_num);
740 if (port_priv) {
741 mad_priv->mad.mad.mad_hdr.tid =
742 ((struct ib_mad *)smp)->mad_hdr.tid;
743 recv_mad_agent = find_mad_agent(port_priv,
Hal Rosenstock4a0754f2005-07-27 11:45:24 -0700744 &mad_priv->mad.mad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746 if (!port_priv || !recv_mad_agent) {
747 kmem_cache_free(ib_mad_cache, mad_priv);
748 kfree(local);
749 ret = 0;
750 goto out;
751 }
752 local->mad_priv = mad_priv;
753 local->recv_mad_agent = recv_mad_agent;
754 break;
755 default:
756 kmem_cache_free(ib_mad_cache, mad_priv);
757 kfree(local);
758 ret = -EINVAL;
759 goto out;
760 }
761
Sean Hefty34816ad2005-10-25 10:51:39 -0700762 local->mad_send_wr = mad_send_wr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 /* Reference MAD agent until send side of local completion handled */
764 atomic_inc(&mad_agent_priv->refcount);
765 /* Queue local completion to local list */
766 spin_lock_irqsave(&mad_agent_priv->lock, flags);
767 list_add_tail(&local->completion_list, &mad_agent_priv->local_list);
768 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
769 queue_work(mad_agent_priv->qp_info->port_priv->wq,
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700770 &mad_agent_priv->local_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 ret = 1;
772out:
773 return ret;
774}
775
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800776static int get_pad_size(int hdr_len, int data_len)
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700777{
778 int seg_size, pad;
779
780 seg_size = sizeof(struct ib_mad) - hdr_len;
781 if (data_len && seg_size) {
782 pad = seg_size - data_len % seg_size;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800783 return pad == seg_size ? 0 : pad;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700784 } else
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800785 return seg_size;
786}
787
788static void free_send_rmpp_list(struct ib_mad_send_wr_private *mad_send_wr)
789{
790 struct ib_rmpp_segment *s, *t;
791
792 list_for_each_entry_safe(s, t, &mad_send_wr->rmpp_list, list) {
793 list_del(&s->list);
794 kfree(s);
795 }
796}
797
798static int alloc_send_rmpp_list(struct ib_mad_send_wr_private *send_wr,
799 gfp_t gfp_mask)
800{
801 struct ib_mad_send_buf *send_buf = &send_wr->send_buf;
802 struct ib_rmpp_mad *rmpp_mad = send_buf->mad;
803 struct ib_rmpp_segment *seg = NULL;
804 int left, seg_size, pad;
805
806 send_buf->seg_size = sizeof (struct ib_mad) - send_buf->hdr_len;
807 seg_size = send_buf->seg_size;
808 pad = send_wr->pad;
809
810 /* Allocate data segments. */
811 for (left = send_buf->data_len + pad; left > 0; left -= seg_size) {
812 seg = kmalloc(sizeof (*seg) + seg_size, gfp_mask);
813 if (!seg) {
814 printk(KERN_ERR "alloc_send_rmpp_segs: RMPP mem "
815 "alloc failed for len %zd, gfp %#x\n",
816 sizeof (*seg) + seg_size, gfp_mask);
817 free_send_rmpp_list(send_wr);
818 return -ENOMEM;
819 }
820 seg->num = ++send_buf->seg_count;
821 list_add_tail(&seg->list, &send_wr->rmpp_list);
822 }
823
824 /* Zero any padding */
825 if (pad)
826 memset(seg->data + seg_size - pad, 0, pad);
827
828 rmpp_mad->rmpp_hdr.rmpp_version = send_wr->mad_agent_priv->
829 agent.rmpp_version;
830 rmpp_mad->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_DATA;
831 ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
832
833 send_wr->cur_seg = container_of(send_wr->rmpp_list.next,
834 struct ib_rmpp_segment, list);
835 send_wr->last_ack_seg = send_wr->cur_seg;
836 return 0;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700837}
838
839struct ib_mad_send_buf * ib_create_send_mad(struct ib_mad_agent *mad_agent,
840 u32 remote_qpn, u16 pkey_index,
Sean Hefty34816ad2005-10-25 10:51:39 -0700841 int rmpp_active,
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700842 int hdr_len, int data_len,
Al Virodd0fc662005-10-07 07:46:04 +0100843 gfp_t gfp_mask)
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700844{
845 struct ib_mad_agent_private *mad_agent_priv;
Sean Hefty34816ad2005-10-25 10:51:39 -0700846 struct ib_mad_send_wr_private *mad_send_wr;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800847 int pad, message_size, ret, size;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700848 void *buf;
849
Sean Hefty34816ad2005-10-25 10:51:39 -0700850 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
851 agent);
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800852 pad = get_pad_size(hdr_len, data_len);
853 message_size = hdr_len + data_len + pad;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700854
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700855 if ((!mad_agent->rmpp_version &&
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800856 (rmpp_active || message_size > sizeof(struct ib_mad))) ||
857 (!rmpp_active && message_size > sizeof(struct ib_mad)))
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700858 return ERR_PTR(-EINVAL);
859
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800860 size = rmpp_active ? hdr_len : sizeof(struct ib_mad);
861 buf = kzalloc(sizeof *mad_send_wr + size, gfp_mask);
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700862 if (!buf)
863 return ERR_PTR(-ENOMEM);
864
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800865 mad_send_wr = buf + size;
866 INIT_LIST_HEAD(&mad_send_wr->rmpp_list);
Sean Hefty34816ad2005-10-25 10:51:39 -0700867 mad_send_wr->send_buf.mad = buf;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800868 mad_send_wr->send_buf.hdr_len = hdr_len;
869 mad_send_wr->send_buf.data_len = data_len;
870 mad_send_wr->pad = pad;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700871
Sean Hefty34816ad2005-10-25 10:51:39 -0700872 mad_send_wr->mad_agent_priv = mad_agent_priv;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800873 mad_send_wr->sg_list[0].length = hdr_len;
Sean Hefty34816ad2005-10-25 10:51:39 -0700874 mad_send_wr->sg_list[0].lkey = mad_agent->mr->lkey;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800875 mad_send_wr->sg_list[1].length = sizeof(struct ib_mad) - hdr_len;
876 mad_send_wr->sg_list[1].lkey = mad_agent->mr->lkey;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700877
Sean Hefty34816ad2005-10-25 10:51:39 -0700878 mad_send_wr->send_wr.wr_id = (unsigned long) mad_send_wr;
879 mad_send_wr->send_wr.sg_list = mad_send_wr->sg_list;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800880 mad_send_wr->send_wr.num_sge = 2;
Sean Hefty34816ad2005-10-25 10:51:39 -0700881 mad_send_wr->send_wr.opcode = IB_WR_SEND;
882 mad_send_wr->send_wr.send_flags = IB_SEND_SIGNALED;
883 mad_send_wr->send_wr.wr.ud.remote_qpn = remote_qpn;
884 mad_send_wr->send_wr.wr.ud.remote_qkey = IB_QP_SET_QKEY;
885 mad_send_wr->send_wr.wr.ud.pkey_index = pkey_index;
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700886
887 if (rmpp_active) {
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800888 ret = alloc_send_rmpp_list(mad_send_wr, gfp_mask);
889 if (ret) {
890 kfree(buf);
891 return ERR_PTR(ret);
892 }
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700893 }
894
Sean Hefty34816ad2005-10-25 10:51:39 -0700895 mad_send_wr->send_buf.mad_agent = mad_agent;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700896 atomic_inc(&mad_agent_priv->refcount);
Sean Hefty34816ad2005-10-25 10:51:39 -0700897 return &mad_send_wr->send_buf;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700898}
899EXPORT_SYMBOL(ib_create_send_mad);
900
Hal Rosenstock618a3c02006-03-28 16:40:04 -0800901int ib_get_mad_data_offset(u8 mgmt_class)
902{
903 if (mgmt_class == IB_MGMT_CLASS_SUBN_ADM)
904 return IB_MGMT_SA_HDR;
905 else if ((mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
906 (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
907 (mgmt_class == IB_MGMT_CLASS_BIS))
908 return IB_MGMT_DEVICE_HDR;
909 else if ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
910 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END))
911 return IB_MGMT_VENDOR_HDR;
912 else
913 return IB_MGMT_MAD_HDR;
914}
915EXPORT_SYMBOL(ib_get_mad_data_offset);
916
917int ib_is_mad_class_rmpp(u8 mgmt_class)
918{
919 if ((mgmt_class == IB_MGMT_CLASS_SUBN_ADM) ||
920 (mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
921 (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
922 (mgmt_class == IB_MGMT_CLASS_BIS) ||
923 ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
924 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END)))
925 return 1;
926 return 0;
927}
928EXPORT_SYMBOL(ib_is_mad_class_rmpp);
929
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800930void *ib_get_rmpp_segment(struct ib_mad_send_buf *send_buf, int seg_num)
931{
932 struct ib_mad_send_wr_private *mad_send_wr;
933 struct list_head *list;
934
935 mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
936 send_buf);
937 list = &mad_send_wr->cur_seg->list;
938
939 if (mad_send_wr->cur_seg->num < seg_num) {
940 list_for_each_entry(mad_send_wr->cur_seg, list, list)
941 if (mad_send_wr->cur_seg->num == seg_num)
942 break;
943 } else if (mad_send_wr->cur_seg->num > seg_num) {
944 list_for_each_entry_reverse(mad_send_wr->cur_seg, list, list)
945 if (mad_send_wr->cur_seg->num == seg_num)
946 break;
947 }
948 return mad_send_wr->cur_seg->data;
949}
950EXPORT_SYMBOL(ib_get_rmpp_segment);
951
952static inline void *ib_get_payload(struct ib_mad_send_wr_private *mad_send_wr)
953{
954 if (mad_send_wr->send_buf.seg_count)
955 return ib_get_rmpp_segment(&mad_send_wr->send_buf,
956 mad_send_wr->seg_num);
957 else
958 return mad_send_wr->send_buf.mad +
959 mad_send_wr->send_buf.hdr_len;
960}
961
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700962void ib_free_send_mad(struct ib_mad_send_buf *send_buf)
963{
964 struct ib_mad_agent_private *mad_agent_priv;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800965 struct ib_mad_send_wr_private *mad_send_wr;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700966
967 mad_agent_priv = container_of(send_buf->mad_agent,
968 struct ib_mad_agent_private, agent);
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800969 mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
970 send_buf);
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700971
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800972 free_send_rmpp_list(mad_send_wr);
973 kfree(send_buf->mad);
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700974 if (atomic_dec_and_test(&mad_agent_priv->refcount))
975 wake_up(&mad_agent_priv->wait);
976}
977EXPORT_SYMBOL(ib_free_send_mad);
978
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700979int ib_send_mad(struct ib_mad_send_wr_private *mad_send_wr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
981 struct ib_mad_qp_info *qp_info;
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -0700982 struct list_head *list;
Sean Hefty34816ad2005-10-25 10:51:39 -0700983 struct ib_send_wr *bad_send_wr;
984 struct ib_mad_agent *mad_agent;
985 struct ib_sge *sge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 unsigned long flags;
987 int ret;
988
Hal Rosenstockf8197a42005-07-27 11:45:24 -0700989 /* Set WR ID to find mad_send_wr upon completion */
Hal Rosenstockd760ce82005-07-27 11:45:25 -0700990 qp_info = mad_send_wr->mad_agent_priv->qp_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 mad_send_wr->send_wr.wr_id = (unsigned long)&mad_send_wr->mad_list;
992 mad_send_wr->mad_list.mad_queue = &qp_info->send_queue;
993
Sean Hefty34816ad2005-10-25 10:51:39 -0700994 mad_agent = mad_send_wr->send_buf.mad_agent;
995 sge = mad_send_wr->sg_list;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800996 sge[0].addr = dma_map_single(mad_agent->device->dma_device,
997 mad_send_wr->send_buf.mad,
998 sge[0].length,
999 DMA_TO_DEVICE);
1000 pci_unmap_addr_set(mad_send_wr, header_mapping, sge[0].addr);
1001
1002 sge[1].addr = dma_map_single(mad_agent->device->dma_device,
1003 ib_get_payload(mad_send_wr),
1004 sge[1].length,
1005 DMA_TO_DEVICE);
1006 pci_unmap_addr_set(mad_send_wr, payload_mapping, sge[1].addr);
Sean Hefty34816ad2005-10-25 10:51:39 -07001007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 spin_lock_irqsave(&qp_info->send_queue.lock, flags);
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07001009 if (qp_info->send_queue.count < qp_info->send_queue.max_active) {
Sean Hefty34816ad2005-10-25 10:51:39 -07001010 ret = ib_post_send(mad_agent->qp, &mad_send_wr->send_wr,
1011 &bad_send_wr);
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07001012 list = &qp_info->send_queue.list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 ret = 0;
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07001015 list = &qp_info->overflow_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07001017
1018 if (!ret) {
1019 qp_info->send_queue.count++;
1020 list_add_tail(&mad_send_wr->mad_list.list, list);
1021 }
1022 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001023 if (ret) {
Sean Hefty34816ad2005-10-25 10:51:39 -07001024 dma_unmap_single(mad_agent->device->dma_device,
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001025 pci_unmap_addr(mad_send_wr, header_mapping),
1026 sge[0].length, DMA_TO_DEVICE);
1027 dma_unmap_single(mad_agent->device->dma_device,
1028 pci_unmap_addr(mad_send_wr, payload_mapping),
1029 sge[1].length, DMA_TO_DEVICE);
1030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return ret;
1032}
1033
1034/*
1035 * ib_post_send_mad - Posts MAD(s) to the send queue of the QP associated
1036 * with the registered client
1037 */
Sean Hefty34816ad2005-10-25 10:51:39 -07001038int ib_post_send_mad(struct ib_mad_send_buf *send_buf,
1039 struct ib_mad_send_buf **bad_send_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 struct ib_mad_agent_private *mad_agent_priv;
Sean Hefty34816ad2005-10-25 10:51:39 -07001042 struct ib_mad_send_buf *next_send_buf;
1043 struct ib_mad_send_wr_private *mad_send_wr;
1044 unsigned long flags;
1045 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 /* Walk list of send WRs and post each on send list */
Sean Hefty34816ad2005-10-25 10:51:39 -07001048 for (; send_buf; send_buf = next_send_buf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Sean Hefty34816ad2005-10-25 10:51:39 -07001050 mad_send_wr = container_of(send_buf,
1051 struct ib_mad_send_wr_private,
1052 send_buf);
1053 mad_agent_priv = mad_send_wr->mad_agent_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Sean Hefty34816ad2005-10-25 10:51:39 -07001055 if (!send_buf->mad_agent->send_handler ||
1056 (send_buf->timeout_ms &&
1057 !send_buf->mad_agent->recv_handler)) {
1058 ret = -EINVAL;
1059 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 }
1061
Hal Rosenstock618a3c02006-03-28 16:40:04 -08001062 if (!ib_is_mad_class_rmpp(((struct ib_mad_hdr *) send_buf->mad)->mgmt_class)) {
1063 if (mad_agent_priv->agent.rmpp_version) {
1064 ret = -EINVAL;
1065 goto error;
1066 }
1067 }
1068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 /*
1070 * Save pointer to next work request to post in case the
1071 * current one completes, and the user modifies the work
1072 * request associated with the completion
1073 */
Sean Hefty34816ad2005-10-25 10:51:39 -07001074 next_send_buf = send_buf->next;
1075 mad_send_wr->send_wr.wr.ud.ah = send_buf->ah;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Sean Hefty34816ad2005-10-25 10:51:39 -07001077 if (((struct ib_mad_hdr *) send_buf->mad)->mgmt_class ==
1078 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
1079 ret = handle_outgoing_dr_smp(mad_agent_priv,
1080 mad_send_wr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (ret < 0) /* error */
Sean Hefty34816ad2005-10-25 10:51:39 -07001082 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 else if (ret == 1) /* locally consumed */
Sean Hefty34816ad2005-10-25 10:51:39 -07001084 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
1086
Sean Hefty34816ad2005-10-25 10:51:39 -07001087 mad_send_wr->tid = ((struct ib_mad_hdr *) send_buf->mad)->tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 /* Timeout will be updated after send completes */
Sean Hefty34816ad2005-10-25 10:51:39 -07001089 mad_send_wr->timeout = msecs_to_jiffies(send_buf->timeout_ms);
1090 mad_send_wr->retries = send_buf->retries;
1091 /* Reference for work request to QP + response */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 mad_send_wr->refcount = 1 + (mad_send_wr->timeout > 0);
1093 mad_send_wr->status = IB_WC_SUCCESS;
1094
1095 /* Reference MAD agent until send completes */
1096 atomic_inc(&mad_agent_priv->refcount);
1097 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1098 list_add_tail(&mad_send_wr->agent_list,
1099 &mad_agent_priv->send_list);
1100 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1101
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001102 if (mad_agent_priv->agent.rmpp_version) {
1103 ret = ib_send_rmpp_mad(mad_send_wr);
1104 if (ret >= 0 && ret != IB_RMPP_RESULT_CONSUMED)
1105 ret = ib_send_mad(mad_send_wr);
1106 } else
1107 ret = ib_send_mad(mad_send_wr);
1108 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 /* Fail send request */
1110 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1111 list_del(&mad_send_wr->agent_list);
1112 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1113 atomic_dec(&mad_agent_priv->refcount);
Sean Hefty34816ad2005-10-25 10:51:39 -07001114 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 }
1117 return 0;
Sean Hefty34816ad2005-10-25 10:51:39 -07001118error:
1119 if (bad_send_buf)
1120 *bad_send_buf = send_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 return ret;
1122}
1123EXPORT_SYMBOL(ib_post_send_mad);
1124
1125/*
1126 * ib_free_recv_mad - Returns data buffers used to receive
1127 * a MAD to the access layer
1128 */
1129void ib_free_recv_mad(struct ib_mad_recv_wc *mad_recv_wc)
1130{
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001131 struct ib_mad_recv_buf *mad_recv_buf, *temp_recv_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 struct ib_mad_private_header *mad_priv_hdr;
1133 struct ib_mad_private *priv;
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001134 struct list_head free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001136 INIT_LIST_HEAD(&free_list);
1137 list_splice_init(&mad_recv_wc->rmpp_list, &free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001139 list_for_each_entry_safe(mad_recv_buf, temp_recv_buf,
1140 &free_list, list) {
1141 mad_recv_wc = container_of(mad_recv_buf, struct ib_mad_recv_wc,
1142 recv_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 mad_priv_hdr = container_of(mad_recv_wc,
1144 struct ib_mad_private_header,
1145 recv_wc);
1146 priv = container_of(mad_priv_hdr, struct ib_mad_private,
1147 header);
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001148 kmem_cache_free(ib_mad_cache, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150}
1151EXPORT_SYMBOL(ib_free_recv_mad);
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153struct ib_mad_agent *ib_redirect_mad_qp(struct ib_qp *qp,
1154 u8 rmpp_version,
1155 ib_mad_send_handler send_handler,
1156 ib_mad_recv_handler recv_handler,
1157 void *context)
1158{
1159 return ERR_PTR(-EINVAL); /* XXX: for now */
1160}
1161EXPORT_SYMBOL(ib_redirect_mad_qp);
1162
1163int ib_process_mad_wc(struct ib_mad_agent *mad_agent,
1164 struct ib_wc *wc)
1165{
1166 printk(KERN_ERR PFX "ib_process_mad_wc() not implemented yet\n");
1167 return 0;
1168}
1169EXPORT_SYMBOL(ib_process_mad_wc);
1170
1171static int method_in_use(struct ib_mad_mgmt_method_table **method,
1172 struct ib_mad_reg_req *mad_reg_req)
1173{
1174 int i;
1175
1176 for (i = find_first_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS);
1177 i < IB_MGMT_MAX_METHODS;
1178 i = find_next_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS,
1179 1+i)) {
1180 if ((*method)->agent[i]) {
1181 printk(KERN_ERR PFX "Method %d already in use\n", i);
1182 return -EINVAL;
1183 }
1184 }
1185 return 0;
1186}
1187
1188static int allocate_method_table(struct ib_mad_mgmt_method_table **method)
1189{
1190 /* Allocate management method table */
Roland Dreierde6eb662005-11-02 07:23:14 -08001191 *method = kzalloc(sizeof **method, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 if (!*method) {
1193 printk(KERN_ERR PFX "No memory for "
1194 "ib_mad_mgmt_method_table\n");
1195 return -ENOMEM;
1196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 return 0;
1199}
1200
1201/*
1202 * Check to see if there are any methods still in use
1203 */
1204static int check_method_table(struct ib_mad_mgmt_method_table *method)
1205{
1206 int i;
1207
1208 for (i = 0; i < IB_MGMT_MAX_METHODS; i++)
1209 if (method->agent[i])
1210 return 1;
1211 return 0;
1212}
1213
1214/*
1215 * Check to see if there are any method tables for this class still in use
1216 */
1217static int check_class_table(struct ib_mad_mgmt_class_table *class)
1218{
1219 int i;
1220
1221 for (i = 0; i < MAX_MGMT_CLASS; i++)
1222 if (class->method_table[i])
1223 return 1;
1224 return 0;
1225}
1226
1227static int check_vendor_class(struct ib_mad_mgmt_vendor_class *vendor_class)
1228{
1229 int i;
1230
1231 for (i = 0; i < MAX_MGMT_OUI; i++)
1232 if (vendor_class->method_table[i])
1233 return 1;
1234 return 0;
1235}
1236
1237static int find_vendor_oui(struct ib_mad_mgmt_vendor_class *vendor_class,
1238 char *oui)
1239{
1240 int i;
1241
1242 for (i = 0; i < MAX_MGMT_OUI; i++)
1243 /* Is there matching OUI for this vendor class ? */
1244 if (!memcmp(vendor_class->oui[i], oui, 3))
1245 return i;
1246
1247 return -1;
1248}
1249
1250static int check_vendor_table(struct ib_mad_mgmt_vendor_class_table *vendor)
1251{
1252 int i;
1253
1254 for (i = 0; i < MAX_MGMT_VENDOR_RANGE2; i++)
1255 if (vendor->vendor_class[i])
1256 return 1;
1257
1258 return 0;
1259}
1260
1261static void remove_methods_mad_agent(struct ib_mad_mgmt_method_table *method,
1262 struct ib_mad_agent_private *agent)
1263{
1264 int i;
1265
1266 /* Remove any methods for this mad agent */
1267 for (i = 0; i < IB_MGMT_MAX_METHODS; i++) {
1268 if (method->agent[i] == agent) {
1269 method->agent[i] = NULL;
1270 }
1271 }
1272}
1273
1274static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1275 struct ib_mad_agent_private *agent_priv,
1276 u8 mgmt_class)
1277{
1278 struct ib_mad_port_private *port_priv;
1279 struct ib_mad_mgmt_class_table **class;
1280 struct ib_mad_mgmt_method_table **method;
1281 int i, ret;
1282
1283 port_priv = agent_priv->qp_info->port_priv;
1284 class = &port_priv->version[mad_reg_req->mgmt_class_version].class;
1285 if (!*class) {
1286 /* Allocate management class table for "new" class version */
Roland Dreierde6eb662005-11-02 07:23:14 -08001287 *class = kzalloc(sizeof **class, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 if (!*class) {
1289 printk(KERN_ERR PFX "No memory for "
1290 "ib_mad_mgmt_class_table\n");
1291 ret = -ENOMEM;
1292 goto error1;
1293 }
Roland Dreierde6eb662005-11-02 07:23:14 -08001294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 /* Allocate method table for this management class */
1296 method = &(*class)->method_table[mgmt_class];
1297 if ((ret = allocate_method_table(method)))
1298 goto error2;
1299 } else {
1300 method = &(*class)->method_table[mgmt_class];
1301 if (!*method) {
1302 /* Allocate method table for this management class */
1303 if ((ret = allocate_method_table(method)))
1304 goto error1;
1305 }
1306 }
1307
1308 /* Now, make sure methods are not already in use */
1309 if (method_in_use(method, mad_reg_req))
1310 goto error3;
1311
1312 /* Finally, add in methods being registered */
1313 for (i = find_first_bit(mad_reg_req->method_mask,
1314 IB_MGMT_MAX_METHODS);
1315 i < IB_MGMT_MAX_METHODS;
1316 i = find_next_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS,
1317 1+i)) {
1318 (*method)->agent[i] = agent_priv;
1319 }
1320 return 0;
1321
1322error3:
1323 /* Remove any methods for this mad agent */
1324 remove_methods_mad_agent(*method, agent_priv);
1325 /* Now, check to see if there are any methods in use */
1326 if (!check_method_table(*method)) {
1327 /* If not, release management method table */
1328 kfree(*method);
1329 *method = NULL;
1330 }
1331 ret = -EINVAL;
1332 goto error1;
1333error2:
1334 kfree(*class);
1335 *class = NULL;
1336error1:
1337 return ret;
1338}
1339
1340static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1341 struct ib_mad_agent_private *agent_priv)
1342{
1343 struct ib_mad_port_private *port_priv;
1344 struct ib_mad_mgmt_vendor_class_table **vendor_table;
1345 struct ib_mad_mgmt_vendor_class_table *vendor = NULL;
1346 struct ib_mad_mgmt_vendor_class *vendor_class = NULL;
1347 struct ib_mad_mgmt_method_table **method;
1348 int i, ret = -ENOMEM;
1349 u8 vclass;
1350
1351 /* "New" vendor (with OUI) class */
1352 vclass = vendor_class_index(mad_reg_req->mgmt_class);
1353 port_priv = agent_priv->qp_info->port_priv;
1354 vendor_table = &port_priv->version[
1355 mad_reg_req->mgmt_class_version].vendor;
1356 if (!*vendor_table) {
1357 /* Allocate mgmt vendor class table for "new" class version */
Roland Dreierde6eb662005-11-02 07:23:14 -08001358 vendor = kzalloc(sizeof *vendor, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 if (!vendor) {
1360 printk(KERN_ERR PFX "No memory for "
1361 "ib_mad_mgmt_vendor_class_table\n");
1362 goto error1;
1363 }
Roland Dreierde6eb662005-11-02 07:23:14 -08001364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 *vendor_table = vendor;
1366 }
1367 if (!(*vendor_table)->vendor_class[vclass]) {
1368 /* Allocate table for this management vendor class */
Roland Dreierde6eb662005-11-02 07:23:14 -08001369 vendor_class = kzalloc(sizeof *vendor_class, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 if (!vendor_class) {
1371 printk(KERN_ERR PFX "No memory for "
1372 "ib_mad_mgmt_vendor_class\n");
1373 goto error2;
1374 }
Roland Dreierde6eb662005-11-02 07:23:14 -08001375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 (*vendor_table)->vendor_class[vclass] = vendor_class;
1377 }
1378 for (i = 0; i < MAX_MGMT_OUI; i++) {
1379 /* Is there matching OUI for this vendor class ? */
1380 if (!memcmp((*vendor_table)->vendor_class[vclass]->oui[i],
1381 mad_reg_req->oui, 3)) {
1382 method = &(*vendor_table)->vendor_class[
1383 vclass]->method_table[i];
1384 BUG_ON(!*method);
1385 goto check_in_use;
1386 }
1387 }
1388 for (i = 0; i < MAX_MGMT_OUI; i++) {
1389 /* OUI slot available ? */
1390 if (!is_vendor_oui((*vendor_table)->vendor_class[
1391 vclass]->oui[i])) {
1392 method = &(*vendor_table)->vendor_class[
1393 vclass]->method_table[i];
1394 BUG_ON(*method);
1395 /* Allocate method table for this OUI */
1396 if ((ret = allocate_method_table(method)))
1397 goto error3;
1398 memcpy((*vendor_table)->vendor_class[vclass]->oui[i],
1399 mad_reg_req->oui, 3);
1400 goto check_in_use;
1401 }
1402 }
1403 printk(KERN_ERR PFX "All OUI slots in use\n");
1404 goto error3;
1405
1406check_in_use:
1407 /* Now, make sure methods are not already in use */
1408 if (method_in_use(method, mad_reg_req))
1409 goto error4;
1410
1411 /* Finally, add in methods being registered */
1412 for (i = find_first_bit(mad_reg_req->method_mask,
1413 IB_MGMT_MAX_METHODS);
1414 i < IB_MGMT_MAX_METHODS;
1415 i = find_next_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS,
1416 1+i)) {
1417 (*method)->agent[i] = agent_priv;
1418 }
1419 return 0;
1420
1421error4:
1422 /* Remove any methods for this mad agent */
1423 remove_methods_mad_agent(*method, agent_priv);
1424 /* Now, check to see if there are any methods in use */
1425 if (!check_method_table(*method)) {
1426 /* If not, release management method table */
1427 kfree(*method);
1428 *method = NULL;
1429 }
1430 ret = -EINVAL;
1431error3:
1432 if (vendor_class) {
1433 (*vendor_table)->vendor_class[vclass] = NULL;
1434 kfree(vendor_class);
1435 }
1436error2:
1437 if (vendor) {
1438 *vendor_table = NULL;
1439 kfree(vendor);
1440 }
1441error1:
1442 return ret;
1443}
1444
1445static void remove_mad_reg_req(struct ib_mad_agent_private *agent_priv)
1446{
1447 struct ib_mad_port_private *port_priv;
1448 struct ib_mad_mgmt_class_table *class;
1449 struct ib_mad_mgmt_method_table *method;
1450 struct ib_mad_mgmt_vendor_class_table *vendor;
1451 struct ib_mad_mgmt_vendor_class *vendor_class;
1452 int index;
1453 u8 mgmt_class;
1454
1455 /*
1456 * Was MAD registration request supplied
1457 * with original registration ?
1458 */
1459 if (!agent_priv->reg_req) {
1460 goto out;
1461 }
1462
1463 port_priv = agent_priv->qp_info->port_priv;
1464 mgmt_class = convert_mgmt_class(agent_priv->reg_req->mgmt_class);
1465 class = port_priv->version[
1466 agent_priv->reg_req->mgmt_class_version].class;
1467 if (!class)
1468 goto vendor_check;
1469
1470 method = class->method_table[mgmt_class];
1471 if (method) {
1472 /* Remove any methods for this mad agent */
1473 remove_methods_mad_agent(method, agent_priv);
1474 /* Now, check to see if there are any methods still in use */
1475 if (!check_method_table(method)) {
1476 /* If not, release management method table */
1477 kfree(method);
1478 class->method_table[mgmt_class] = NULL;
1479 /* Any management classes left ? */
1480 if (!check_class_table(class)) {
1481 /* If not, release management class table */
1482 kfree(class);
1483 port_priv->version[
1484 agent_priv->reg_req->
1485 mgmt_class_version].class = NULL;
1486 }
1487 }
1488 }
1489
1490vendor_check:
1491 if (!is_vendor_class(mgmt_class))
1492 goto out;
1493
1494 /* normalize mgmt_class to vendor range 2 */
1495 mgmt_class = vendor_class_index(agent_priv->reg_req->mgmt_class);
1496 vendor = port_priv->version[
1497 agent_priv->reg_req->mgmt_class_version].vendor;
1498
1499 if (!vendor)
1500 goto out;
1501
1502 vendor_class = vendor->vendor_class[mgmt_class];
1503 if (vendor_class) {
1504 index = find_vendor_oui(vendor_class, agent_priv->reg_req->oui);
1505 if (index < 0)
1506 goto out;
1507 method = vendor_class->method_table[index];
1508 if (method) {
1509 /* Remove any methods for this mad agent */
1510 remove_methods_mad_agent(method, agent_priv);
1511 /*
1512 * Now, check to see if there are
1513 * any methods still in use
1514 */
1515 if (!check_method_table(method)) {
1516 /* If not, release management method table */
1517 kfree(method);
1518 vendor_class->method_table[index] = NULL;
1519 memset(vendor_class->oui[index], 0, 3);
1520 /* Any OUIs left ? */
1521 if (!check_vendor_class(vendor_class)) {
1522 /* If not, release vendor class table */
1523 kfree(vendor_class);
1524 vendor->vendor_class[mgmt_class] = NULL;
1525 /* Any other vendor classes left ? */
1526 if (!check_vendor_table(vendor)) {
1527 kfree(vendor);
1528 port_priv->version[
1529 agent_priv->reg_req->
1530 mgmt_class_version].
1531 vendor = NULL;
1532 }
1533 }
1534 }
1535 }
1536 }
1537
1538out:
1539 return;
1540}
1541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542static struct ib_mad_agent_private *
1543find_mad_agent(struct ib_mad_port_private *port_priv,
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07001544 struct ib_mad *mad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
1546 struct ib_mad_agent_private *mad_agent = NULL;
1547 unsigned long flags;
1548
1549 spin_lock_irqsave(&port_priv->reg_lock, flags);
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07001550 if (response_mad(mad)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 u32 hi_tid;
1552 struct ib_mad_agent_private *entry;
1553
1554 /*
1555 * Routing is based on high 32 bits of transaction ID
1556 * of MAD.
1557 */
1558 hi_tid = be64_to_cpu(mad->mad_hdr.tid) >> 32;
Sean Hefty34816ad2005-10-25 10:51:39 -07001559 list_for_each_entry(entry, &port_priv->agent_list, agent_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 if (entry->agent.hi_tid == hi_tid) {
1561 mad_agent = entry;
1562 break;
1563 }
1564 }
1565 } else {
1566 struct ib_mad_mgmt_class_table *class;
1567 struct ib_mad_mgmt_method_table *method;
1568 struct ib_mad_mgmt_vendor_class_table *vendor;
1569 struct ib_mad_mgmt_vendor_class *vendor_class;
1570 struct ib_vendor_mad *vendor_mad;
1571 int index;
1572
1573 /*
1574 * Routing is based on version, class, and method
1575 * For "newer" vendor MADs, also based on OUI
1576 */
1577 if (mad->mad_hdr.class_version >= MAX_MGMT_VERSION)
1578 goto out;
1579 if (!is_vendor_class(mad->mad_hdr.mgmt_class)) {
1580 class = port_priv->version[
1581 mad->mad_hdr.class_version].class;
1582 if (!class)
1583 goto out;
1584 method = class->method_table[convert_mgmt_class(
1585 mad->mad_hdr.mgmt_class)];
1586 if (method)
1587 mad_agent = method->agent[mad->mad_hdr.method &
1588 ~IB_MGMT_METHOD_RESP];
1589 } else {
1590 vendor = port_priv->version[
1591 mad->mad_hdr.class_version].vendor;
1592 if (!vendor)
1593 goto out;
1594 vendor_class = vendor->vendor_class[vendor_class_index(
1595 mad->mad_hdr.mgmt_class)];
1596 if (!vendor_class)
1597 goto out;
1598 /* Find matching OUI */
1599 vendor_mad = (struct ib_vendor_mad *)mad;
1600 index = find_vendor_oui(vendor_class, vendor_mad->oui);
1601 if (index == -1)
1602 goto out;
1603 method = vendor_class->method_table[index];
1604 if (method) {
1605 mad_agent = method->agent[mad->mad_hdr.method &
1606 ~IB_MGMT_METHOD_RESP];
1607 }
1608 }
1609 }
1610
1611 if (mad_agent) {
1612 if (mad_agent->agent.recv_handler)
1613 atomic_inc(&mad_agent->refcount);
1614 else {
1615 printk(KERN_NOTICE PFX "No receive handler for client "
1616 "%p on port %d\n",
1617 &mad_agent->agent, port_priv->port_num);
1618 mad_agent = NULL;
1619 }
1620 }
1621out:
1622 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
1623
1624 return mad_agent;
1625}
1626
1627static int validate_mad(struct ib_mad *mad, u32 qp_num)
1628{
1629 int valid = 0;
1630
1631 /* Make sure MAD base version is understood */
1632 if (mad->mad_hdr.base_version != IB_MGMT_BASE_VERSION) {
1633 printk(KERN_ERR PFX "MAD received with unsupported base "
1634 "version %d\n", mad->mad_hdr.base_version);
1635 goto out;
1636 }
1637
1638 /* Filter SMI packets sent to other than QP0 */
1639 if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
1640 (mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) {
1641 if (qp_num == 0)
1642 valid = 1;
1643 } else {
1644 /* Filter GSI packets sent to QP0 */
1645 if (qp_num != 0)
1646 valid = 1;
1647 }
1648
1649out:
1650 return valid;
1651}
1652
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001653static int is_data_mad(struct ib_mad_agent_private *mad_agent_priv,
1654 struct ib_mad_hdr *mad_hdr)
1655{
1656 struct ib_rmpp_mad *rmpp_mad;
1657
1658 rmpp_mad = (struct ib_rmpp_mad *)mad_hdr;
1659 return !mad_agent_priv->agent.rmpp_version ||
1660 !(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
1661 IB_MGMT_RMPP_FLAG_ACTIVE) ||
1662 (rmpp_mad->rmpp_hdr.rmpp_type == IB_MGMT_RMPP_TYPE_DATA);
1663}
1664
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001665static inline int rcv_has_same_class(struct ib_mad_send_wr_private *wr,
1666 struct ib_mad_recv_wc *rwc)
1667{
1668 return ((struct ib_mad *)(wr->send_buf.mad))->mad_hdr.mgmt_class ==
1669 rwc->recv_buf.mad->mad_hdr.mgmt_class;
1670}
1671
1672static inline int rcv_has_same_gid(struct ib_mad_send_wr_private *wr,
1673 struct ib_mad_recv_wc *rwc )
1674{
1675 struct ib_ah_attr attr;
1676 u8 send_resp, rcv_resp;
1677
1678 send_resp = ((struct ib_mad *)(wr->send_buf.mad))->
1679 mad_hdr.method & IB_MGMT_METHOD_RESP;
1680 rcv_resp = rwc->recv_buf.mad->mad_hdr.method & IB_MGMT_METHOD_RESP;
1681
1682 if (!send_resp && rcv_resp)
1683 /* is request/response. GID/LIDs are both local (same). */
1684 return 1;
1685
1686 if (send_resp == rcv_resp)
1687 /* both requests, or both responses. GIDs different */
1688 return 0;
1689
1690 if (ib_query_ah(wr->send_buf.ah, &attr))
1691 /* Assume not equal, to avoid false positives. */
1692 return 0;
1693
1694 if (!(attr.ah_flags & IB_AH_GRH) && !(rwc->wc->wc_flags & IB_WC_GRH))
1695 return attr.dlid == rwc->wc->slid;
1696 else if ((attr.ah_flags & IB_AH_GRH) &&
1697 (rwc->wc->wc_flags & IB_WC_GRH))
1698 return memcmp(attr.grh.dgid.raw,
1699 rwc->recv_buf.grh->sgid.raw, 16) == 0;
1700 else
1701 /* one has GID, other does not. Assume different */
1702 return 0;
1703}
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001704struct ib_mad_send_wr_private*
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001705ib_find_send_mad(struct ib_mad_agent_private *mad_agent_priv,
1706 struct ib_mad_recv_wc *mad_recv_wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
1708 struct ib_mad_send_wr_private *mad_send_wr;
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001709 struct ib_mad *mad;
1710
1711 mad = (struct ib_mad *)mad_recv_wc->recv_buf.mad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
1713 list_for_each_entry(mad_send_wr, &mad_agent_priv->wait_list,
1714 agent_list) {
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001715 if ((mad_send_wr->tid == mad->mad_hdr.tid) &&
1716 rcv_has_same_class(mad_send_wr, mad_recv_wc) &&
1717 rcv_has_same_gid(mad_send_wr, mad_recv_wc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 return mad_send_wr;
1719 }
1720
1721 /*
1722 * It's possible to receive the response before we've
1723 * been notified that the send has completed
1724 */
1725 list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list,
1726 agent_list) {
Sean Hefty34816ad2005-10-25 10:51:39 -07001727 if (is_data_mad(mad_agent_priv, mad_send_wr->send_buf.mad) &&
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001728 mad_send_wr->tid == mad->mad_hdr.tid &&
1729 mad_send_wr->timeout &&
1730 rcv_has_same_class(mad_send_wr, mad_recv_wc) &&
1731 rcv_has_same_gid(mad_send_wr, mad_recv_wc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 /* Verify request has not been canceled */
1733 return (mad_send_wr->status == IB_WC_SUCCESS) ?
1734 mad_send_wr : NULL;
1735 }
1736 }
1737 return NULL;
1738}
1739
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001740void ib_mark_mad_done(struct ib_mad_send_wr_private *mad_send_wr)
Hal Rosenstock6a0c4352005-07-27 11:45:26 -07001741{
1742 mad_send_wr->timeout = 0;
1743 if (mad_send_wr->refcount == 1) {
1744 list_del(&mad_send_wr->agent_list);
1745 list_add_tail(&mad_send_wr->agent_list,
1746 &mad_send_wr->mad_agent_priv->done_list);
1747 }
1748}
1749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750static void ib_mad_complete_recv(struct ib_mad_agent_private *mad_agent_priv,
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07001751 struct ib_mad_recv_wc *mad_recv_wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752{
1753 struct ib_mad_send_wr_private *mad_send_wr;
1754 struct ib_mad_send_wc mad_send_wc;
1755 unsigned long flags;
1756
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001757 INIT_LIST_HEAD(&mad_recv_wc->rmpp_list);
1758 list_add(&mad_recv_wc->recv_buf.list, &mad_recv_wc->rmpp_list);
1759 if (mad_agent_priv->agent.rmpp_version) {
1760 mad_recv_wc = ib_process_rmpp_recv_wc(mad_agent_priv,
1761 mad_recv_wc);
1762 if (!mad_recv_wc) {
1763 if (atomic_dec_and_test(&mad_agent_priv->refcount))
1764 wake_up(&mad_agent_priv->wait);
1765 return;
1766 }
1767 }
1768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 /* Complete corresponding request */
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07001770 if (response_mad(mad_recv_wc->recv_buf.mad)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 spin_lock_irqsave(&mad_agent_priv->lock, flags);
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001772 mad_send_wr = ib_find_send_mad(mad_agent_priv, mad_recv_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 if (!mad_send_wr) {
1774 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07001775 ib_free_recv_mad(mad_recv_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 if (atomic_dec_and_test(&mad_agent_priv->refcount))
1777 wake_up(&mad_agent_priv->wait);
1778 return;
1779 }
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001780 ib_mark_mad_done(mad_send_wr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1782
1783 /* Defined behavior is to complete response before request */
Sean Hefty34816ad2005-10-25 10:51:39 -07001784 mad_recv_wc->wc->wr_id = (unsigned long) &mad_send_wr->send_buf;
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07001785 mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent,
1786 mad_recv_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 atomic_dec(&mad_agent_priv->refcount);
1788
1789 mad_send_wc.status = IB_WC_SUCCESS;
1790 mad_send_wc.vendor_err = 0;
Sean Hefty34816ad2005-10-25 10:51:39 -07001791 mad_send_wc.send_buf = &mad_send_wr->send_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
1793 } else {
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07001794 mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent,
1795 mad_recv_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 if (atomic_dec_and_test(&mad_agent_priv->refcount))
1797 wake_up(&mad_agent_priv->wait);
1798 }
1799}
1800
1801static void ib_mad_recv_done_handler(struct ib_mad_port_private *port_priv,
1802 struct ib_wc *wc)
1803{
1804 struct ib_mad_qp_info *qp_info;
1805 struct ib_mad_private_header *mad_priv_hdr;
1806 struct ib_mad_private *recv, *response;
1807 struct ib_mad_list_head *mad_list;
1808 struct ib_mad_agent_private *mad_agent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
1810 response = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL);
1811 if (!response)
1812 printk(KERN_ERR PFX "ib_mad_recv_done_handler no memory "
1813 "for response buffer\n");
1814
1815 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
1816 qp_info = mad_list->mad_queue->qp_info;
1817 dequeue_mad(mad_list);
1818
1819 mad_priv_hdr = container_of(mad_list, struct ib_mad_private_header,
1820 mad_list);
1821 recv = container_of(mad_priv_hdr, struct ib_mad_private, header);
1822 dma_unmap_single(port_priv->device->dma_device,
1823 pci_unmap_addr(&recv->header, mapping),
1824 sizeof(struct ib_mad_private) -
1825 sizeof(struct ib_mad_private_header),
1826 DMA_FROM_DEVICE);
1827
1828 /* Setup MAD receive work completion from "normal" work completion */
Sean Hefty24239af2005-04-16 15:26:08 -07001829 recv->header.wc = *wc;
1830 recv->header.recv_wc.wc = &recv->header.wc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 recv->header.recv_wc.mad_len = sizeof(struct ib_mad);
1832 recv->header.recv_wc.recv_buf.mad = &recv->mad.mad;
1833 recv->header.recv_wc.recv_buf.grh = &recv->grh;
1834
1835 if (atomic_read(&qp_info->snoop_count))
1836 snoop_recv(qp_info, &recv->header.recv_wc, IB_MAD_SNOOP_RECVS);
1837
1838 /* Validate MAD */
1839 if (!validate_mad(&recv->mad.mad, qp_info->qp->qp_num))
1840 goto out;
1841
1842 if (recv->mad.mad.mad_hdr.mgmt_class ==
1843 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
1844 if (!smi_handle_dr_smp_recv(&recv->mad.smp,
1845 port_priv->device->node_type,
1846 port_priv->port_num,
1847 port_priv->device->phys_port_cnt))
1848 goto out;
1849 if (!smi_check_forward_dr_smp(&recv->mad.smp))
1850 goto local;
1851 if (!smi_handle_dr_smp_send(&recv->mad.smp,
1852 port_priv->device->node_type,
1853 port_priv->port_num))
1854 goto out;
Ralph Campbell5e9f71a2006-02-03 14:32:01 -08001855 if (!smi_check_local_smp(&recv->mad.smp, port_priv->device))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 goto out;
1857 }
1858
1859local:
1860 /* Give driver "right of first refusal" on incoming MAD */
1861 if (port_priv->device->process_mad) {
1862 int ret;
1863
1864 if (!response) {
1865 printk(KERN_ERR PFX "No memory for response MAD\n");
1866 /*
1867 * Is it better to assume that
1868 * it wouldn't be processed ?
1869 */
1870 goto out;
1871 }
1872
1873 ret = port_priv->device->process_mad(port_priv->device, 0,
1874 port_priv->port_num,
1875 wc, &recv->grh,
1876 &recv->mad.mad,
1877 &response->mad.mad);
1878 if (ret & IB_MAD_RESULT_SUCCESS) {
1879 if (ret & IB_MAD_RESULT_CONSUMED)
1880 goto out;
1881 if (ret & IB_MAD_RESULT_REPLY) {
Sean Hefty34816ad2005-10-25 10:51:39 -07001882 agent_send_response(&response->mad.mad,
1883 &recv->grh, wc,
1884 port_priv->device,
1885 port_priv->port_num,
1886 qp_info->qp->qp_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 goto out;
1888 }
1889 }
1890 }
1891
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07001892 mad_agent = find_mad_agent(port_priv, &recv->mad.mad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 if (mad_agent) {
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07001894 ib_mad_complete_recv(mad_agent, &recv->header.recv_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 /*
1896 * recv is freed up in error cases in ib_mad_complete_recv
1897 * or via recv_handler in ib_mad_complete_recv()
1898 */
1899 recv = NULL;
1900 }
1901
1902out:
1903 /* Post another receive request for this QP */
1904 if (response) {
1905 ib_mad_post_receive_mads(qp_info, response);
1906 if (recv)
1907 kmem_cache_free(ib_mad_cache, recv);
1908 } else
1909 ib_mad_post_receive_mads(qp_info, recv);
1910}
1911
1912static void adjust_timeout(struct ib_mad_agent_private *mad_agent_priv)
1913{
1914 struct ib_mad_send_wr_private *mad_send_wr;
1915 unsigned long delay;
1916
1917 if (list_empty(&mad_agent_priv->wait_list)) {
1918 cancel_delayed_work(&mad_agent_priv->timed_work);
1919 } else {
1920 mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
1921 struct ib_mad_send_wr_private,
1922 agent_list);
1923
1924 if (time_after(mad_agent_priv->timeout,
1925 mad_send_wr->timeout)) {
1926 mad_agent_priv->timeout = mad_send_wr->timeout;
1927 cancel_delayed_work(&mad_agent_priv->timed_work);
1928 delay = mad_send_wr->timeout - jiffies;
1929 if ((long)delay <= 0)
1930 delay = 1;
1931 queue_delayed_work(mad_agent_priv->qp_info->
1932 port_priv->wq,
1933 &mad_agent_priv->timed_work, delay);
1934 }
1935 }
1936}
1937
Hal Rosenstockd760ce82005-07-27 11:45:25 -07001938static void wait_for_response(struct ib_mad_send_wr_private *mad_send_wr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
Hal Rosenstockd760ce82005-07-27 11:45:25 -07001940 struct ib_mad_agent_private *mad_agent_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 struct ib_mad_send_wr_private *temp_mad_send_wr;
1942 struct list_head *list_item;
1943 unsigned long delay;
1944
Hal Rosenstockd760ce82005-07-27 11:45:25 -07001945 mad_agent_priv = mad_send_wr->mad_agent_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 list_del(&mad_send_wr->agent_list);
1947
1948 delay = mad_send_wr->timeout;
1949 mad_send_wr->timeout += jiffies;
1950
Hal Rosenstock29bb33d2005-07-27 11:45:32 -07001951 if (delay) {
1952 list_for_each_prev(list_item, &mad_agent_priv->wait_list) {
1953 temp_mad_send_wr = list_entry(list_item,
1954 struct ib_mad_send_wr_private,
1955 agent_list);
1956 if (time_after(mad_send_wr->timeout,
1957 temp_mad_send_wr->timeout))
1958 break;
1959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 }
Hal Rosenstock29bb33d2005-07-27 11:45:32 -07001961 else
1962 list_item = &mad_agent_priv->wait_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 list_add(&mad_send_wr->agent_list, list_item);
1964
1965 /* Reschedule a work item if we have a shorter timeout */
1966 if (mad_agent_priv->wait_list.next == &mad_send_wr->agent_list) {
1967 cancel_delayed_work(&mad_agent_priv->timed_work);
1968 queue_delayed_work(mad_agent_priv->qp_info->port_priv->wq,
1969 &mad_agent_priv->timed_work, delay);
1970 }
1971}
1972
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07001973void ib_reset_mad_timeout(struct ib_mad_send_wr_private *mad_send_wr,
1974 int timeout_ms)
1975{
1976 mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
1977 wait_for_response(mad_send_wr);
1978}
1979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980/*
1981 * Process a send work completion
1982 */
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001983void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr,
1984 struct ib_mad_send_wc *mad_send_wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985{
1986 struct ib_mad_agent_private *mad_agent_priv;
1987 unsigned long flags;
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001988 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Hal Rosenstockd760ce82005-07-27 11:45:25 -07001990 mad_agent_priv = mad_send_wr->mad_agent_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 spin_lock_irqsave(&mad_agent_priv->lock, flags);
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001992 if (mad_agent_priv->agent.rmpp_version) {
1993 ret = ib_process_rmpp_send_wc(mad_send_wr, mad_send_wc);
1994 if (ret == IB_RMPP_RESULT_CONSUMED)
1995 goto done;
1996 } else
1997 ret = IB_RMPP_RESULT_UNHANDLED;
1998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 if (mad_send_wc->status != IB_WC_SUCCESS &&
2000 mad_send_wr->status == IB_WC_SUCCESS) {
2001 mad_send_wr->status = mad_send_wc->status;
2002 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2003 }
2004
2005 if (--mad_send_wr->refcount > 0) {
2006 if (mad_send_wr->refcount == 1 && mad_send_wr->timeout &&
2007 mad_send_wr->status == IB_WC_SUCCESS) {
Hal Rosenstockd760ce82005-07-27 11:45:25 -07002008 wait_for_response(mad_send_wr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 }
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002010 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 }
2012
2013 /* Remove send from MAD agent and notify client of completion */
2014 list_del(&mad_send_wr->agent_list);
2015 adjust_timeout(mad_agent_priv);
2016 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2017
2018 if (mad_send_wr->status != IB_WC_SUCCESS )
2019 mad_send_wc->status = mad_send_wr->status;
Sean Hefty34816ad2005-10-25 10:51:39 -07002020 if (ret == IB_RMPP_RESULT_INTERNAL)
2021 ib_rmpp_send_handler(mad_send_wc);
2022 else
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002023 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2024 mad_send_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
2026 /* Release reference on agent taken when sending */
2027 if (atomic_dec_and_test(&mad_agent_priv->refcount))
2028 wake_up(&mad_agent_priv->wait);
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002029 return;
2030done:
2031 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032}
2033
2034static void ib_mad_send_done_handler(struct ib_mad_port_private *port_priv,
2035 struct ib_wc *wc)
2036{
2037 struct ib_mad_send_wr_private *mad_send_wr, *queued_send_wr;
2038 struct ib_mad_list_head *mad_list;
2039 struct ib_mad_qp_info *qp_info;
2040 struct ib_mad_queue *send_queue;
2041 struct ib_send_wr *bad_send_wr;
Sean Hefty34816ad2005-10-25 10:51:39 -07002042 struct ib_mad_send_wc mad_send_wc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 unsigned long flags;
2044 int ret;
2045
2046 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
2047 mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
2048 mad_list);
2049 send_queue = mad_list->mad_queue;
2050 qp_info = send_queue->qp_info;
2051
2052retry:
Sean Hefty34816ad2005-10-25 10:51:39 -07002053 dma_unmap_single(mad_send_wr->send_buf.mad_agent->device->dma_device,
Jack Morgensteinf36e1792006-03-03 21:54:13 -08002054 pci_unmap_addr(mad_send_wr, header_mapping),
Sean Hefty34816ad2005-10-25 10:51:39 -07002055 mad_send_wr->sg_list[0].length, DMA_TO_DEVICE);
Jack Morgensteinf36e1792006-03-03 21:54:13 -08002056 dma_unmap_single(mad_send_wr->send_buf.mad_agent->device->dma_device,
2057 pci_unmap_addr(mad_send_wr, payload_mapping),
2058 mad_send_wr->sg_list[1].length, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 queued_send_wr = NULL;
2060 spin_lock_irqsave(&send_queue->lock, flags);
2061 list_del(&mad_list->list);
2062
2063 /* Move queued send to the send queue */
2064 if (send_queue->count-- > send_queue->max_active) {
2065 mad_list = container_of(qp_info->overflow_list.next,
2066 struct ib_mad_list_head, list);
2067 queued_send_wr = container_of(mad_list,
2068 struct ib_mad_send_wr_private,
2069 mad_list);
2070 list_del(&mad_list->list);
2071 list_add_tail(&mad_list->list, &send_queue->list);
2072 }
2073 spin_unlock_irqrestore(&send_queue->lock, flags);
2074
Sean Hefty34816ad2005-10-25 10:51:39 -07002075 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2076 mad_send_wc.status = wc->status;
2077 mad_send_wc.vendor_err = wc->vendor_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 if (atomic_read(&qp_info->snoop_count))
Sean Hefty34816ad2005-10-25 10:51:39 -07002079 snoop_send(qp_info, &mad_send_wr->send_buf, &mad_send_wc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 IB_MAD_SNOOP_SEND_COMPLETIONS);
Sean Hefty34816ad2005-10-25 10:51:39 -07002081 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
2083 if (queued_send_wr) {
2084 ret = ib_post_send(qp_info->qp, &queued_send_wr->send_wr,
Sean Hefty34816ad2005-10-25 10:51:39 -07002085 &bad_send_wr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 if (ret) {
2087 printk(KERN_ERR PFX "ib_post_send failed: %d\n", ret);
2088 mad_send_wr = queued_send_wr;
2089 wc->status = IB_WC_LOC_QP_OP_ERR;
2090 goto retry;
2091 }
2092 }
2093}
2094
2095static void mark_sends_for_retry(struct ib_mad_qp_info *qp_info)
2096{
2097 struct ib_mad_send_wr_private *mad_send_wr;
2098 struct ib_mad_list_head *mad_list;
2099 unsigned long flags;
2100
2101 spin_lock_irqsave(&qp_info->send_queue.lock, flags);
2102 list_for_each_entry(mad_list, &qp_info->send_queue.list, list) {
2103 mad_send_wr = container_of(mad_list,
2104 struct ib_mad_send_wr_private,
2105 mad_list);
2106 mad_send_wr->retry = 1;
2107 }
2108 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
2109}
2110
2111static void mad_error_handler(struct ib_mad_port_private *port_priv,
2112 struct ib_wc *wc)
2113{
2114 struct ib_mad_list_head *mad_list;
2115 struct ib_mad_qp_info *qp_info;
2116 struct ib_mad_send_wr_private *mad_send_wr;
2117 int ret;
2118
2119 /* Determine if failure was a send or receive */
2120 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
2121 qp_info = mad_list->mad_queue->qp_info;
2122 if (mad_list->mad_queue == &qp_info->recv_queue)
2123 /*
2124 * Receive errors indicate that the QP has entered the error
2125 * state - error handling/shutdown code will cleanup
2126 */
2127 return;
2128
2129 /*
2130 * Send errors will transition the QP to SQE - move
2131 * QP to RTS and repost flushed work requests
2132 */
2133 mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
2134 mad_list);
2135 if (wc->status == IB_WC_WR_FLUSH_ERR) {
2136 if (mad_send_wr->retry) {
2137 /* Repost send */
2138 struct ib_send_wr *bad_send_wr;
2139
2140 mad_send_wr->retry = 0;
2141 ret = ib_post_send(qp_info->qp, &mad_send_wr->send_wr,
2142 &bad_send_wr);
2143 if (ret)
2144 ib_mad_send_done_handler(port_priv, wc);
2145 } else
2146 ib_mad_send_done_handler(port_priv, wc);
2147 } else {
2148 struct ib_qp_attr *attr;
2149
2150 /* Transition QP to RTS and fail offending send */
2151 attr = kmalloc(sizeof *attr, GFP_KERNEL);
2152 if (attr) {
2153 attr->qp_state = IB_QPS_RTS;
2154 attr->cur_qp_state = IB_QPS_SQE;
2155 ret = ib_modify_qp(qp_info->qp, attr,
2156 IB_QP_STATE | IB_QP_CUR_STATE);
2157 kfree(attr);
2158 if (ret)
2159 printk(KERN_ERR PFX "mad_error_handler - "
2160 "ib_modify_qp to RTS : %d\n", ret);
2161 else
2162 mark_sends_for_retry(qp_info);
2163 }
2164 ib_mad_send_done_handler(port_priv, wc);
2165 }
2166}
2167
2168/*
2169 * IB MAD completion callback
2170 */
2171static void ib_mad_completion_handler(void *data)
2172{
2173 struct ib_mad_port_private *port_priv;
2174 struct ib_wc wc;
2175
2176 port_priv = (struct ib_mad_port_private *)data;
2177 ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
2178
2179 while (ib_poll_cq(port_priv->cq, 1, &wc) == 1) {
2180 if (wc.status == IB_WC_SUCCESS) {
2181 switch (wc.opcode) {
2182 case IB_WC_SEND:
2183 ib_mad_send_done_handler(port_priv, &wc);
2184 break;
2185 case IB_WC_RECV:
2186 ib_mad_recv_done_handler(port_priv, &wc);
2187 break;
2188 default:
2189 BUG_ON(1);
2190 break;
2191 }
2192 } else
2193 mad_error_handler(port_priv, &wc);
2194 }
2195}
2196
2197static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv)
2198{
2199 unsigned long flags;
2200 struct ib_mad_send_wr_private *mad_send_wr, *temp_mad_send_wr;
2201 struct ib_mad_send_wc mad_send_wc;
2202 struct list_head cancel_list;
2203
2204 INIT_LIST_HEAD(&cancel_list);
2205
2206 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2207 list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
2208 &mad_agent_priv->send_list, agent_list) {
2209 if (mad_send_wr->status == IB_WC_SUCCESS) {
2210 mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
2211 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2212 }
2213 }
2214
2215 /* Empty wait list to prevent receives from finding a request */
2216 list_splice_init(&mad_agent_priv->wait_list, &cancel_list);
Hal Rosenstock2c153b92005-07-27 11:45:31 -07002217 /* Empty local completion list as well */
2218 list_splice_init(&mad_agent_priv->local_list, &cancel_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2220
2221 /* Report all cancelled requests */
2222 mad_send_wc.status = IB_WC_WR_FLUSH_ERR;
2223 mad_send_wc.vendor_err = 0;
2224
2225 list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
2226 &cancel_list, agent_list) {
Sean Hefty34816ad2005-10-25 10:51:39 -07002227 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2228 list_del(&mad_send_wr->agent_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2230 &mad_send_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 atomic_dec(&mad_agent_priv->refcount);
2232 }
2233}
2234
2235static struct ib_mad_send_wr_private*
Sean Hefty34816ad2005-10-25 10:51:39 -07002236find_send_wr(struct ib_mad_agent_private *mad_agent_priv,
2237 struct ib_mad_send_buf *send_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238{
2239 struct ib_mad_send_wr_private *mad_send_wr;
2240
2241 list_for_each_entry(mad_send_wr, &mad_agent_priv->wait_list,
2242 agent_list) {
Sean Hefty34816ad2005-10-25 10:51:39 -07002243 if (&mad_send_wr->send_buf == send_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 return mad_send_wr;
2245 }
2246
2247 list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list,
2248 agent_list) {
Sean Hefty34816ad2005-10-25 10:51:39 -07002249 if (is_data_mad(mad_agent_priv, mad_send_wr->send_buf.mad) &&
2250 &mad_send_wr->send_buf == send_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 return mad_send_wr;
2252 }
2253 return NULL;
2254}
2255
Sean Hefty34816ad2005-10-25 10:51:39 -07002256int ib_modify_mad(struct ib_mad_agent *mad_agent,
2257 struct ib_mad_send_buf *send_buf, u32 timeout_ms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258{
2259 struct ib_mad_agent_private *mad_agent_priv;
2260 struct ib_mad_send_wr_private *mad_send_wr;
2261 unsigned long flags;
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07002262 int active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
2264 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
2265 agent);
2266 spin_lock_irqsave(&mad_agent_priv->lock, flags);
Sean Hefty34816ad2005-10-25 10:51:39 -07002267 mad_send_wr = find_send_wr(mad_agent_priv, send_buf);
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002268 if (!mad_send_wr || mad_send_wr->status != IB_WC_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002270 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 }
2272
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07002273 active = (!mad_send_wr->timeout || mad_send_wr->refcount > 1);
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002274 if (!timeout_ms) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002276 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 }
2278
Sean Hefty34816ad2005-10-25 10:51:39 -07002279 mad_send_wr->send_buf.timeout_ms = timeout_ms;
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07002280 if (active)
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002281 mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
2282 else
2283 ib_reset_mad_timeout(mad_send_wr, timeout_ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002285 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2286 return 0;
2287}
2288EXPORT_SYMBOL(ib_modify_mad);
2289
Sean Hefty34816ad2005-10-25 10:51:39 -07002290void ib_cancel_mad(struct ib_mad_agent *mad_agent,
2291 struct ib_mad_send_buf *send_buf)
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002292{
Sean Hefty34816ad2005-10-25 10:51:39 -07002293 ib_modify_mad(mad_agent, send_buf, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294}
2295EXPORT_SYMBOL(ib_cancel_mad);
2296
2297static void local_completions(void *data)
2298{
2299 struct ib_mad_agent_private *mad_agent_priv;
2300 struct ib_mad_local_private *local;
2301 struct ib_mad_agent_private *recv_mad_agent;
2302 unsigned long flags;
Hal Rosenstock2c153b92005-07-27 11:45:31 -07002303 int recv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 struct ib_wc wc;
2305 struct ib_mad_send_wc mad_send_wc;
2306
2307 mad_agent_priv = (struct ib_mad_agent_private *)data;
2308
2309 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2310 while (!list_empty(&mad_agent_priv->local_list)) {
2311 local = list_entry(mad_agent_priv->local_list.next,
2312 struct ib_mad_local_private,
2313 completion_list);
2314 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2315 if (local->mad_priv) {
2316 recv_mad_agent = local->recv_mad_agent;
2317 if (!recv_mad_agent) {
2318 printk(KERN_ERR PFX "No receive MAD agent for local completion\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 goto local_send_completion;
2320 }
2321
Hal Rosenstock2c153b92005-07-27 11:45:31 -07002322 recv = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 /*
2324 * Defined behavior is to complete response
2325 * before request
2326 */
Sean Hefty34816ad2005-10-25 10:51:39 -07002327 build_smp_wc((unsigned long) local->mad_send_wr,
Sean Hefty97f52eb2005-08-13 21:05:57 -07002328 be16_to_cpu(IB_LID_PERMISSIVE),
Sean Hefty34816ad2005-10-25 10:51:39 -07002329 0, recv_mad_agent->agent.port_num, &wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
2331 local->mad_priv->header.recv_wc.wc = &wc;
2332 local->mad_priv->header.recv_wc.mad_len =
2333 sizeof(struct ib_mad);
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002334 INIT_LIST_HEAD(&local->mad_priv->header.recv_wc.rmpp_list);
2335 list_add(&local->mad_priv->header.recv_wc.recv_buf.list,
2336 &local->mad_priv->header.recv_wc.rmpp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 local->mad_priv->header.recv_wc.recv_buf.grh = NULL;
2338 local->mad_priv->header.recv_wc.recv_buf.mad =
2339 &local->mad_priv->mad.mad;
2340 if (atomic_read(&recv_mad_agent->qp_info->snoop_count))
2341 snoop_recv(recv_mad_agent->qp_info,
2342 &local->mad_priv->header.recv_wc,
2343 IB_MAD_SNOOP_RECVS);
2344 recv_mad_agent->agent.recv_handler(
2345 &recv_mad_agent->agent,
2346 &local->mad_priv->header.recv_wc);
2347 spin_lock_irqsave(&recv_mad_agent->lock, flags);
2348 atomic_dec(&recv_mad_agent->refcount);
2349 spin_unlock_irqrestore(&recv_mad_agent->lock, flags);
2350 }
2351
2352local_send_completion:
2353 /* Complete send */
2354 mad_send_wc.status = IB_WC_SUCCESS;
2355 mad_send_wc.vendor_err = 0;
Sean Hefty34816ad2005-10-25 10:51:39 -07002356 mad_send_wc.send_buf = &local->mad_send_wr->send_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 if (atomic_read(&mad_agent_priv->qp_info->snoop_count))
Sean Hefty34816ad2005-10-25 10:51:39 -07002358 snoop_send(mad_agent_priv->qp_info,
2359 &local->mad_send_wr->send_buf,
2360 &mad_send_wc, IB_MAD_SNOOP_SEND_COMPLETIONS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2362 &mad_send_wc);
2363
2364 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2365 list_del(&local->completion_list);
2366 atomic_dec(&mad_agent_priv->refcount);
Hal Rosenstock2c153b92005-07-27 11:45:31 -07002367 if (!recv)
2368 kmem_cache_free(ib_mad_cache, local->mad_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 kfree(local);
2370 }
2371 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2372}
2373
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002374static int retry_send(struct ib_mad_send_wr_private *mad_send_wr)
2375{
2376 int ret;
2377
2378 if (!mad_send_wr->retries--)
2379 return -ETIMEDOUT;
2380
Sean Hefty34816ad2005-10-25 10:51:39 -07002381 mad_send_wr->timeout = msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms);
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002382
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002383 if (mad_send_wr->mad_agent_priv->agent.rmpp_version) {
2384 ret = ib_retry_rmpp(mad_send_wr);
2385 switch (ret) {
2386 case IB_RMPP_RESULT_UNHANDLED:
2387 ret = ib_send_mad(mad_send_wr);
2388 break;
2389 case IB_RMPP_RESULT_CONSUMED:
2390 ret = 0;
2391 break;
2392 default:
2393 ret = -ECOMM;
2394 break;
2395 }
2396 } else
2397 ret = ib_send_mad(mad_send_wr);
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002398
2399 if (!ret) {
2400 mad_send_wr->refcount++;
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002401 list_add_tail(&mad_send_wr->agent_list,
2402 &mad_send_wr->mad_agent_priv->send_list);
2403 }
2404 return ret;
2405}
2406
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407static void timeout_sends(void *data)
2408{
2409 struct ib_mad_agent_private *mad_agent_priv;
2410 struct ib_mad_send_wr_private *mad_send_wr;
2411 struct ib_mad_send_wc mad_send_wc;
2412 unsigned long flags, delay;
2413
2414 mad_agent_priv = (struct ib_mad_agent_private *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 mad_send_wc.vendor_err = 0;
2416
2417 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2418 while (!list_empty(&mad_agent_priv->wait_list)) {
2419 mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
2420 struct ib_mad_send_wr_private,
2421 agent_list);
2422
2423 if (time_after(mad_send_wr->timeout, jiffies)) {
2424 delay = mad_send_wr->timeout - jiffies;
2425 if ((long)delay <= 0)
2426 delay = 1;
2427 queue_delayed_work(mad_agent_priv->qp_info->
2428 port_priv->wq,
2429 &mad_agent_priv->timed_work, delay);
2430 break;
2431 }
2432
Hal Rosenstockdbf92272005-07-27 11:45:30 -07002433 list_del(&mad_send_wr->agent_list);
Hal Rosenstock29bb33d2005-07-27 11:45:32 -07002434 if (mad_send_wr->status == IB_WC_SUCCESS &&
2435 !retry_send(mad_send_wr))
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002436 continue;
2437
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2439
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002440 if (mad_send_wr->status == IB_WC_SUCCESS)
2441 mad_send_wc.status = IB_WC_RESP_TIMEOUT_ERR;
2442 else
2443 mad_send_wc.status = mad_send_wr->status;
Sean Hefty34816ad2005-10-25 10:51:39 -07002444 mad_send_wc.send_buf = &mad_send_wr->send_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2446 &mad_send_wc);
2447
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 atomic_dec(&mad_agent_priv->refcount);
2449 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2450 }
2451 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2452}
2453
Hal Rosenstock5dd2ce12005-08-15 14:16:36 -07002454static void ib_mad_thread_completion_handler(struct ib_cq *cq, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455{
2456 struct ib_mad_port_private *port_priv = cq->cq_context;
Michael S. Tsirkindc059802006-03-20 10:08:25 -08002457 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
Michael S. Tsirkindc059802006-03-20 10:08:25 -08002459 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2460 if (!list_empty(&port_priv->port_list))
2461 queue_work(port_priv->wq, &port_priv->work);
2462 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463}
2464
2465/*
2466 * Allocate receive MADs and post receive WRs for them
2467 */
2468static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
2469 struct ib_mad_private *mad)
2470{
2471 unsigned long flags;
2472 int post, ret;
2473 struct ib_mad_private *mad_priv;
2474 struct ib_sge sg_list;
2475 struct ib_recv_wr recv_wr, *bad_recv_wr;
2476 struct ib_mad_queue *recv_queue = &qp_info->recv_queue;
2477
2478 /* Initialize common scatter list fields */
2479 sg_list.length = sizeof *mad_priv - sizeof mad_priv->header;
2480 sg_list.lkey = (*qp_info->port_priv->mr).lkey;
2481
2482 /* Initialize common receive WR fields */
2483 recv_wr.next = NULL;
2484 recv_wr.sg_list = &sg_list;
2485 recv_wr.num_sge = 1;
2486
2487 do {
2488 /* Allocate and map receive buffer */
2489 if (mad) {
2490 mad_priv = mad;
2491 mad = NULL;
2492 } else {
2493 mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL);
2494 if (!mad_priv) {
2495 printk(KERN_ERR PFX "No memory for receive buffer\n");
2496 ret = -ENOMEM;
2497 break;
2498 }
2499 }
2500 sg_list.addr = dma_map_single(qp_info->port_priv->
Hal Rosenstock618a3c02006-03-28 16:40:04 -08002501 device->dma_device,
2502 &mad_priv->grh,
2503 sizeof *mad_priv -
2504 sizeof mad_priv->header,
2505 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 pci_unmap_addr_set(&mad_priv->header, mapping, sg_list.addr);
2507 recv_wr.wr_id = (unsigned long)&mad_priv->header.mad_list;
2508 mad_priv->header.mad_list.mad_queue = recv_queue;
2509
2510 /* Post receive WR */
2511 spin_lock_irqsave(&recv_queue->lock, flags);
2512 post = (++recv_queue->count < recv_queue->max_active);
2513 list_add_tail(&mad_priv->header.mad_list.list, &recv_queue->list);
2514 spin_unlock_irqrestore(&recv_queue->lock, flags);
2515 ret = ib_post_recv(qp_info->qp, &recv_wr, &bad_recv_wr);
2516 if (ret) {
2517 spin_lock_irqsave(&recv_queue->lock, flags);
2518 list_del(&mad_priv->header.mad_list.list);
2519 recv_queue->count--;
2520 spin_unlock_irqrestore(&recv_queue->lock, flags);
2521 dma_unmap_single(qp_info->port_priv->device->dma_device,
2522 pci_unmap_addr(&mad_priv->header,
2523 mapping),
2524 sizeof *mad_priv -
2525 sizeof mad_priv->header,
2526 DMA_FROM_DEVICE);
2527 kmem_cache_free(ib_mad_cache, mad_priv);
2528 printk(KERN_ERR PFX "ib_post_recv failed: %d\n", ret);
2529 break;
2530 }
2531 } while (post);
2532
2533 return ret;
2534}
2535
2536/*
2537 * Return all the posted receive MADs
2538 */
2539static void cleanup_recv_queue(struct ib_mad_qp_info *qp_info)
2540{
2541 struct ib_mad_private_header *mad_priv_hdr;
2542 struct ib_mad_private *recv;
2543 struct ib_mad_list_head *mad_list;
2544
2545 while (!list_empty(&qp_info->recv_queue.list)) {
2546
2547 mad_list = list_entry(qp_info->recv_queue.list.next,
2548 struct ib_mad_list_head, list);
2549 mad_priv_hdr = container_of(mad_list,
2550 struct ib_mad_private_header,
2551 mad_list);
2552 recv = container_of(mad_priv_hdr, struct ib_mad_private,
2553 header);
2554
2555 /* Remove from posted receive MAD list */
2556 list_del(&mad_list->list);
2557
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 dma_unmap_single(qp_info->port_priv->device->dma_device,
2559 pci_unmap_addr(&recv->header, mapping),
2560 sizeof(struct ib_mad_private) -
2561 sizeof(struct ib_mad_private_header),
2562 DMA_FROM_DEVICE);
2563 kmem_cache_free(ib_mad_cache, recv);
2564 }
2565
2566 qp_info->recv_queue.count = 0;
2567}
2568
2569/*
2570 * Start the port
2571 */
2572static int ib_mad_port_start(struct ib_mad_port_private *port_priv)
2573{
2574 int ret, i;
2575 struct ib_qp_attr *attr;
2576 struct ib_qp *qp;
2577
2578 attr = kmalloc(sizeof *attr, GFP_KERNEL);
2579 if (!attr) {
2580 printk(KERN_ERR PFX "Couldn't kmalloc ib_qp_attr\n");
2581 return -ENOMEM;
2582 }
2583
2584 for (i = 0; i < IB_MAD_QPS_CORE; i++) {
2585 qp = port_priv->qp_info[i].qp;
2586 /*
2587 * PKey index for QP1 is irrelevant but
2588 * one is needed for the Reset to Init transition
2589 */
2590 attr->qp_state = IB_QPS_INIT;
2591 attr->pkey_index = 0;
2592 attr->qkey = (qp->qp_num == 0) ? 0 : IB_QP1_QKEY;
2593 ret = ib_modify_qp(qp, attr, IB_QP_STATE |
2594 IB_QP_PKEY_INDEX | IB_QP_QKEY);
2595 if (ret) {
2596 printk(KERN_ERR PFX "Couldn't change QP%d state to "
2597 "INIT: %d\n", i, ret);
2598 goto out;
2599 }
2600
2601 attr->qp_state = IB_QPS_RTR;
2602 ret = ib_modify_qp(qp, attr, IB_QP_STATE);
2603 if (ret) {
2604 printk(KERN_ERR PFX "Couldn't change QP%d state to "
2605 "RTR: %d\n", i, ret);
2606 goto out;
2607 }
2608
2609 attr->qp_state = IB_QPS_RTS;
2610 attr->sq_psn = IB_MAD_SEND_Q_PSN;
2611 ret = ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_SQ_PSN);
2612 if (ret) {
2613 printk(KERN_ERR PFX "Couldn't change QP%d state to "
2614 "RTS: %d\n", i, ret);
2615 goto out;
2616 }
2617 }
2618
2619 ret = ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
2620 if (ret) {
2621 printk(KERN_ERR PFX "Failed to request completion "
2622 "notification: %d\n", ret);
2623 goto out;
2624 }
2625
2626 for (i = 0; i < IB_MAD_QPS_CORE; i++) {
2627 ret = ib_mad_post_receive_mads(&port_priv->qp_info[i], NULL);
2628 if (ret) {
2629 printk(KERN_ERR PFX "Couldn't post receive WRs\n");
2630 goto out;
2631 }
2632 }
2633out:
2634 kfree(attr);
2635 return ret;
2636}
2637
2638static void qp_event_handler(struct ib_event *event, void *qp_context)
2639{
2640 struct ib_mad_qp_info *qp_info = qp_context;
2641
2642 /* It's worse than that! He's dead, Jim! */
2643 printk(KERN_ERR PFX "Fatal error (%d) on MAD QP (%d)\n",
2644 event->event, qp_info->qp->qp_num);
2645}
2646
2647static void init_mad_queue(struct ib_mad_qp_info *qp_info,
2648 struct ib_mad_queue *mad_queue)
2649{
2650 mad_queue->qp_info = qp_info;
2651 mad_queue->count = 0;
2652 spin_lock_init(&mad_queue->lock);
2653 INIT_LIST_HEAD(&mad_queue->list);
2654}
2655
2656static void init_mad_qp(struct ib_mad_port_private *port_priv,
2657 struct ib_mad_qp_info *qp_info)
2658{
2659 qp_info->port_priv = port_priv;
2660 init_mad_queue(qp_info, &qp_info->send_queue);
2661 init_mad_queue(qp_info, &qp_info->recv_queue);
2662 INIT_LIST_HEAD(&qp_info->overflow_list);
2663 spin_lock_init(&qp_info->snoop_lock);
2664 qp_info->snoop_table = NULL;
2665 qp_info->snoop_table_size = 0;
2666 atomic_set(&qp_info->snoop_count, 0);
2667}
2668
2669static int create_mad_qp(struct ib_mad_qp_info *qp_info,
2670 enum ib_qp_type qp_type)
2671{
2672 struct ib_qp_init_attr qp_init_attr;
2673 int ret;
2674
2675 memset(&qp_init_attr, 0, sizeof qp_init_attr);
2676 qp_init_attr.send_cq = qp_info->port_priv->cq;
2677 qp_init_attr.recv_cq = qp_info->port_priv->cq;
2678 qp_init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
2679 qp_init_attr.cap.max_send_wr = IB_MAD_QP_SEND_SIZE;
2680 qp_init_attr.cap.max_recv_wr = IB_MAD_QP_RECV_SIZE;
2681 qp_init_attr.cap.max_send_sge = IB_MAD_SEND_REQ_MAX_SG;
2682 qp_init_attr.cap.max_recv_sge = IB_MAD_RECV_REQ_MAX_SG;
2683 qp_init_attr.qp_type = qp_type;
2684 qp_init_attr.port_num = qp_info->port_priv->port_num;
2685 qp_init_attr.qp_context = qp_info;
2686 qp_init_attr.event_handler = qp_event_handler;
2687 qp_info->qp = ib_create_qp(qp_info->port_priv->pd, &qp_init_attr);
2688 if (IS_ERR(qp_info->qp)) {
2689 printk(KERN_ERR PFX "Couldn't create ib_mad QP%d\n",
2690 get_spl_qp_index(qp_type));
2691 ret = PTR_ERR(qp_info->qp);
2692 goto error;
2693 }
2694 /* Use minimum queue sizes unless the CQ is resized */
2695 qp_info->send_queue.max_active = IB_MAD_QP_SEND_SIZE;
2696 qp_info->recv_queue.max_active = IB_MAD_QP_RECV_SIZE;
2697 return 0;
2698
2699error:
2700 return ret;
2701}
2702
2703static void destroy_mad_qp(struct ib_mad_qp_info *qp_info)
2704{
2705 ib_destroy_qp(qp_info->qp);
Jesper Juhl6044ec82005-11-07 01:01:32 -08002706 kfree(qp_info->snoop_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707}
2708
2709/*
2710 * Open the port
2711 * Create the QP, PD, MR, and CQ if needed
2712 */
2713static int ib_mad_port_open(struct ib_device *device,
2714 int port_num)
2715{
2716 int ret, cq_size;
2717 struct ib_mad_port_private *port_priv;
2718 unsigned long flags;
2719 char name[sizeof "ib_mad123"];
2720
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 /* Create new device info */
Roland Dreierde6eb662005-11-02 07:23:14 -08002722 port_priv = kzalloc(sizeof *port_priv, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 if (!port_priv) {
2724 printk(KERN_ERR PFX "No memory for ib_mad_port_private\n");
2725 return -ENOMEM;
2726 }
Roland Dreierde6eb662005-11-02 07:23:14 -08002727
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 port_priv->device = device;
2729 port_priv->port_num = port_num;
2730 spin_lock_init(&port_priv->reg_lock);
2731 INIT_LIST_HEAD(&port_priv->agent_list);
2732 init_mad_qp(port_priv, &port_priv->qp_info[0]);
2733 init_mad_qp(port_priv, &port_priv->qp_info[1]);
2734
2735 cq_size = (IB_MAD_QP_SEND_SIZE + IB_MAD_QP_RECV_SIZE) * 2;
2736 port_priv->cq = ib_create_cq(port_priv->device,
Hal Rosenstock5dd2ce12005-08-15 14:16:36 -07002737 ib_mad_thread_completion_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 NULL, port_priv, cq_size);
2739 if (IS_ERR(port_priv->cq)) {
2740 printk(KERN_ERR PFX "Couldn't create ib_mad CQ\n");
2741 ret = PTR_ERR(port_priv->cq);
2742 goto error3;
2743 }
2744
2745 port_priv->pd = ib_alloc_pd(device);
2746 if (IS_ERR(port_priv->pd)) {
2747 printk(KERN_ERR PFX "Couldn't create ib_mad PD\n");
2748 ret = PTR_ERR(port_priv->pd);
2749 goto error4;
2750 }
2751
2752 port_priv->mr = ib_get_dma_mr(port_priv->pd, IB_ACCESS_LOCAL_WRITE);
2753 if (IS_ERR(port_priv->mr)) {
2754 printk(KERN_ERR PFX "Couldn't get ib_mad DMA MR\n");
2755 ret = PTR_ERR(port_priv->mr);
2756 goto error5;
2757 }
2758
2759 ret = create_mad_qp(&port_priv->qp_info[0], IB_QPT_SMI);
2760 if (ret)
2761 goto error6;
2762 ret = create_mad_qp(&port_priv->qp_info[1], IB_QPT_GSI);
2763 if (ret)
2764 goto error7;
2765
2766 snprintf(name, sizeof name, "ib_mad%d", port_num);
2767 port_priv->wq = create_singlethread_workqueue(name);
2768 if (!port_priv->wq) {
2769 ret = -ENOMEM;
2770 goto error8;
2771 }
2772 INIT_WORK(&port_priv->work, ib_mad_completion_handler, port_priv);
2773
Michael S. Tsirkindc059802006-03-20 10:08:25 -08002774 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2775 list_add_tail(&port_priv->port_list, &ib_mad_port_list);
2776 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2777
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 ret = ib_mad_port_start(port_priv);
2779 if (ret) {
2780 printk(KERN_ERR PFX "Couldn't start port\n");
2781 goto error9;
2782 }
2783
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 return 0;
2785
2786error9:
Michael S. Tsirkindc059802006-03-20 10:08:25 -08002787 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2788 list_del_init(&port_priv->port_list);
2789 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2790
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 destroy_workqueue(port_priv->wq);
2792error8:
2793 destroy_mad_qp(&port_priv->qp_info[1]);
2794error7:
2795 destroy_mad_qp(&port_priv->qp_info[0]);
2796error6:
2797 ib_dereg_mr(port_priv->mr);
2798error5:
2799 ib_dealloc_pd(port_priv->pd);
2800error4:
2801 ib_destroy_cq(port_priv->cq);
2802 cleanup_recv_queue(&port_priv->qp_info[1]);
2803 cleanup_recv_queue(&port_priv->qp_info[0]);
2804error3:
2805 kfree(port_priv);
2806
2807 return ret;
2808}
2809
2810/*
2811 * Close the port
2812 * If there are no classes using the port, free the port
2813 * resources (CQ, MR, PD, QP) and remove the port's info structure
2814 */
2815static int ib_mad_port_close(struct ib_device *device, int port_num)
2816{
2817 struct ib_mad_port_private *port_priv;
2818 unsigned long flags;
2819
2820 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2821 port_priv = __ib_get_mad_port(device, port_num);
2822 if (port_priv == NULL) {
2823 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2824 printk(KERN_ERR PFX "Port %d not found\n", port_num);
2825 return -ENODEV;
2826 }
Michael S. Tsirkindc059802006-03-20 10:08:25 -08002827 list_del_init(&port_priv->port_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2829
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 destroy_workqueue(port_priv->wq);
2831 destroy_mad_qp(&port_priv->qp_info[1]);
2832 destroy_mad_qp(&port_priv->qp_info[0]);
2833 ib_dereg_mr(port_priv->mr);
2834 ib_dealloc_pd(port_priv->pd);
2835 ib_destroy_cq(port_priv->cq);
2836 cleanup_recv_queue(&port_priv->qp_info[1]);
2837 cleanup_recv_queue(&port_priv->qp_info[0]);
2838 /* XXX: Handle deallocation of MAD registration tables */
2839
2840 kfree(port_priv);
2841
2842 return 0;
2843}
2844
2845static void ib_mad_init_device(struct ib_device *device)
2846{
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002847 int start, end, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
2849 if (device->node_type == IB_NODE_SWITCH) {
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002850 start = 0;
2851 end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 } else {
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002853 start = 1;
2854 end = device->phys_port_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 }
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002856
2857 for (i = start; i <= end; i++) {
2858 if (ib_mad_port_open(device, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 printk(KERN_ERR PFX "Couldn't open %s port %d\n",
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002860 device->name, i);
2861 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 }
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002863 if (ib_agent_port_open(device, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 printk(KERN_ERR PFX "Couldn't open %s port %d "
2865 "for agents\n",
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002866 device->name, i);
2867 goto error_agent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 }
2869 }
Hal Rosenstockf68bcc22005-07-27 11:45:27 -07002870 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002872error_agent:
2873 if (ib_mad_port_close(device, i))
2874 printk(KERN_ERR PFX "Couldn't close %s port %d\n",
2875 device->name, i);
2876
2877error:
2878 i--;
2879
2880 while (i >= start) {
2881 if (ib_agent_port_close(device, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 printk(KERN_ERR PFX "Couldn't close %s port %d "
2883 "for agents\n",
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002884 device->name, i);
2885 if (ib_mad_port_close(device, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 printk(KERN_ERR PFX "Couldn't close %s port %d\n",
Roland Dreier4ab6fb72005-10-06 13:28:16 -07002887 device->name, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 i--;
2889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890}
2891
2892static void ib_mad_remove_device(struct ib_device *device)
2893{
Hal Rosenstockf68bcc22005-07-27 11:45:27 -07002894 int i, num_ports, cur_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
2896 if (device->node_type == IB_NODE_SWITCH) {
2897 num_ports = 1;
2898 cur_port = 0;
2899 } else {
2900 num_ports = device->phys_port_cnt;
2901 cur_port = 1;
2902 }
2903 for (i = 0; i < num_ports; i++, cur_port++) {
Hal Rosenstockf68bcc22005-07-27 11:45:27 -07002904 if (ib_agent_port_close(device, cur_port))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 printk(KERN_ERR PFX "Couldn't close %s port %d "
2906 "for agents\n",
2907 device->name, cur_port);
Hal Rosenstockf68bcc22005-07-27 11:45:27 -07002908 if (ib_mad_port_close(device, cur_port))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 printk(KERN_ERR PFX "Couldn't close %s port %d\n",
2910 device->name, cur_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 }
2912}
2913
2914static struct ib_client mad_client = {
2915 .name = "mad",
2916 .add = ib_mad_init_device,
2917 .remove = ib_mad_remove_device
2918};
2919
2920static int __init ib_mad_init_module(void)
2921{
2922 int ret;
2923
2924 spin_lock_init(&ib_mad_port_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
2926 ib_mad_cache = kmem_cache_create("ib_mad",
2927 sizeof(struct ib_mad_private),
2928 0,
2929 SLAB_HWCACHE_ALIGN,
2930 NULL,
2931 NULL);
2932 if (!ib_mad_cache) {
2933 printk(KERN_ERR PFX "Couldn't create ib_mad cache\n");
2934 ret = -ENOMEM;
2935 goto error1;
2936 }
2937
2938 INIT_LIST_HEAD(&ib_mad_port_list);
2939
2940 if (ib_register_client(&mad_client)) {
2941 printk(KERN_ERR PFX "Couldn't register ib_mad client\n");
2942 ret = -EINVAL;
2943 goto error2;
2944 }
2945
2946 return 0;
2947
2948error2:
2949 kmem_cache_destroy(ib_mad_cache);
2950error1:
2951 return ret;
2952}
2953
2954static void __exit ib_mad_cleanup_module(void)
2955{
2956 ib_unregister_client(&mad_client);
2957
2958 if (kmem_cache_destroy(ib_mad_cache)) {
2959 printk(KERN_DEBUG PFX "Failed to destroy ib_mad cache\n");
2960 }
2961}
2962
2963module_init(ib_mad_init_module);
2964module_exit(ib_mad_cleanup_module);
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002965