IB/srp: Change target_mutex to a spinlock
[deliverable/linux.git] / drivers / infiniband / core / mad.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved.
fa619a77
HR
3 * Copyright (c) 2005 Intel Corporation. All rights reserved.
4 * Copyright (c) 2005 Mellanox Technologies Ltd. All rights reserved.
1da177e4
LT
5 *
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING 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 *
f36e1793 34 * $Id: mad.c 5596 2006-03-03 01:00:07Z sean.hefty $
1da177e4 35 */
1da177e4 36#include <linux/dma-mapping.h>
1da177e4
LT
37
38#include "mad_priv.h"
fa619a77 39#include "mad_rmpp.h"
1da177e4
LT
40#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;
fa619a77 50
1da177e4
LT
51static 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,
4a0754fa 64 struct ib_mad *mad);
1da177e4
LT
65static 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);
1da177e4 68static void timeout_sends(void *data);
1da177e4 69static void local_completions(void *data);
1da177e4
LT
70static 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
fa619a77
HR
199 if (rmpp_version && rmpp_version != IB_MGMT_RMPP_VERSION)
200 goto error1;
1da177e4
LT
201
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 }
618a3c03 230 /* Make sure class supplied is consistent with RMPP */
64cb9c6a 231 if (!ib_is_mad_class_rmpp(mad_reg_req->mgmt_class)) {
618a3c03
HR
232 if (rmpp_version)
233 goto error1;
234 }
1da177e4
LT
235 /* Make sure class supplied is consistent with QP type */
236 if (qp_type == IB_QPT_SMI) {
237 if ((mad_reg_req->mgmt_class !=
238 IB_MGMT_CLASS_SUBN_LID_ROUTED) &&
239 (mad_reg_req->mgmt_class !=
240 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE))
241 goto error1;
242 } else {
243 if ((mad_reg_req->mgmt_class ==
244 IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
245 (mad_reg_req->mgmt_class ==
246 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE))
247 goto error1;
248 }
249 } else {
250 /* No registration request supplied */
251 if (!send_handler)
252 goto error1;
253 }
254
255 /* Validate device and port */
256 port_priv = ib_get_mad_port(device, port_num);
257 if (!port_priv) {
258 ret = ERR_PTR(-ENODEV);
259 goto error1;
260 }
261
262 /* Allocate structures */
de6eb66b 263 mad_agent_priv = kzalloc(sizeof *mad_agent_priv, GFP_KERNEL);
1da177e4
LT
264 if (!mad_agent_priv) {
265 ret = ERR_PTR(-ENOMEM);
266 goto error1;
267 }
b82cab6b
HR
268
269 mad_agent_priv->agent.mr = ib_get_dma_mr(port_priv->qp_info[qpn].qp->pd,
270 IB_ACCESS_LOCAL_WRITE);
271 if (IS_ERR(mad_agent_priv->agent.mr)) {
272 ret = ERR_PTR(-ENOMEM);
273 goto error2;
274 }
1da177e4
LT
275
276 if (mad_reg_req) {
277 reg_req = kmalloc(sizeof *reg_req, GFP_KERNEL);
278 if (!reg_req) {
279 ret = ERR_PTR(-ENOMEM);
b82cab6b 280 goto error3;
1da177e4
LT
281 }
282 /* Make a copy of the MAD registration request */
283 memcpy(reg_req, mad_reg_req, sizeof *reg_req);
284 }
285
286 /* Now, fill in the various structures */
1da177e4
LT
287 mad_agent_priv->qp_info = &port_priv->qp_info[qpn];
288 mad_agent_priv->reg_req = reg_req;
fa619a77 289 mad_agent_priv->agent.rmpp_version = rmpp_version;
1da177e4
LT
290 mad_agent_priv->agent.device = device;
291 mad_agent_priv->agent.recv_handler = recv_handler;
292 mad_agent_priv->agent.send_handler = send_handler;
293 mad_agent_priv->agent.context = context;
294 mad_agent_priv->agent.qp = port_priv->qp_info[qpn].qp;
295 mad_agent_priv->agent.port_num = port_num;
296
297 spin_lock_irqsave(&port_priv->reg_lock, flags);
298 mad_agent_priv->agent.hi_tid = ++ib_mad_client_id;
299
300 /*
301 * Make sure MAD registration (if supplied)
302 * is non overlapping with any existing ones
303 */
304 if (mad_reg_req) {
305 mgmt_class = convert_mgmt_class(mad_reg_req->mgmt_class);
306 if (!is_vendor_class(mgmt_class)) {
307 class = port_priv->version[mad_reg_req->
308 mgmt_class_version].class;
309 if (class) {
310 method = class->method_table[mgmt_class];
311 if (method) {
312 if (method_in_use(&method,
313 mad_reg_req))
b82cab6b 314 goto error4;
1da177e4
LT
315 }
316 }
317 ret2 = add_nonoui_reg_req(mad_reg_req, mad_agent_priv,
318 mgmt_class);
319 } else {
320 /* "New" vendor class range */
321 vendor = port_priv->version[mad_reg_req->
322 mgmt_class_version].vendor;
323 if (vendor) {
324 vclass = vendor_class_index(mgmt_class);
325 vendor_class = vendor->vendor_class[vclass];
326 if (vendor_class) {
327 if (is_vendor_method_in_use(
328 vendor_class,
329 mad_reg_req))
b82cab6b 330 goto error4;
1da177e4
LT
331 }
332 }
333 ret2 = add_oui_reg_req(mad_reg_req, mad_agent_priv);
334 }
335 if (ret2) {
336 ret = ERR_PTR(ret2);
b82cab6b 337 goto error4;
1da177e4
LT
338 }
339 }
340
341 /* Add mad agent into port's agent list */
342 list_add_tail(&mad_agent_priv->agent_list, &port_priv->agent_list);
343 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
344
345 spin_lock_init(&mad_agent_priv->lock);
346 INIT_LIST_HEAD(&mad_agent_priv->send_list);
347 INIT_LIST_HEAD(&mad_agent_priv->wait_list);
6a0c435e 348 INIT_LIST_HEAD(&mad_agent_priv->done_list);
fa619a77 349 INIT_LIST_HEAD(&mad_agent_priv->rmpp_list);
1da177e4
LT
350 INIT_WORK(&mad_agent_priv->timed_work, timeout_sends, mad_agent_priv);
351 INIT_LIST_HEAD(&mad_agent_priv->local_list);
352 INIT_WORK(&mad_agent_priv->local_work, local_completions,
353 mad_agent_priv);
1da177e4 354 atomic_set(&mad_agent_priv->refcount, 1);
1b52fa98 355 init_completion(&mad_agent_priv->comp);
1da177e4
LT
356
357 return &mad_agent_priv->agent;
358
b82cab6b 359error4:
1da177e4
LT
360 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
361 kfree(reg_req);
b82cab6b 362error3:
b82cab6b 363 ib_dereg_mr(mad_agent_priv->agent.mr);
2012a116
AB
364error2:
365 kfree(mad_agent_priv);
1da177e4
LT
366error1:
367 return ret;
368}
369EXPORT_SYMBOL(ib_register_mad_agent);
370
371static inline int is_snooping_sends(int mad_snoop_flags)
372{
373 return (mad_snoop_flags &
374 (/*IB_MAD_SNOOP_POSTED_SENDS |
375 IB_MAD_SNOOP_RMPP_SENDS |*/
376 IB_MAD_SNOOP_SEND_COMPLETIONS /*|
377 IB_MAD_SNOOP_RMPP_SEND_COMPLETIONS*/));
378}
379
380static inline int is_snooping_recvs(int mad_snoop_flags)
381{
382 return (mad_snoop_flags &
383 (IB_MAD_SNOOP_RECVS /*|
384 IB_MAD_SNOOP_RMPP_RECVS*/));
385}
386
387static int register_snoop_agent(struct ib_mad_qp_info *qp_info,
388 struct ib_mad_snoop_private *mad_snoop_priv)
389{
390 struct ib_mad_snoop_private **new_snoop_table;
391 unsigned long flags;
392 int i;
393
394 spin_lock_irqsave(&qp_info->snoop_lock, flags);
395 /* Check for empty slot in array. */
396 for (i = 0; i < qp_info->snoop_table_size; i++)
397 if (!qp_info->snoop_table[i])
398 break;
399
400 if (i == qp_info->snoop_table_size) {
401 /* Grow table. */
402 new_snoop_table = kmalloc(sizeof mad_snoop_priv *
403 qp_info->snoop_table_size + 1,
404 GFP_ATOMIC);
405 if (!new_snoop_table) {
406 i = -ENOMEM;
407 goto out;
408 }
409 if (qp_info->snoop_table) {
410 memcpy(new_snoop_table, qp_info->snoop_table,
411 sizeof mad_snoop_priv *
412 qp_info->snoop_table_size);
413 kfree(qp_info->snoop_table);
414 }
415 qp_info->snoop_table = new_snoop_table;
416 qp_info->snoop_table_size++;
417 }
418 qp_info->snoop_table[i] = mad_snoop_priv;
419 atomic_inc(&qp_info->snoop_count);
420out:
421 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
422 return i;
423}
424
425struct ib_mad_agent *ib_register_mad_snoop(struct ib_device *device,
426 u8 port_num,
427 enum ib_qp_type qp_type,
428 int mad_snoop_flags,
429 ib_mad_snoop_handler snoop_handler,
430 ib_mad_recv_handler recv_handler,
431 void *context)
432{
433 struct ib_mad_port_private *port_priv;
434 struct ib_mad_agent *ret;
435 struct ib_mad_snoop_private *mad_snoop_priv;
436 int qpn;
437
438 /* Validate parameters */
439 if ((is_snooping_sends(mad_snoop_flags) && !snoop_handler) ||
440 (is_snooping_recvs(mad_snoop_flags) && !recv_handler)) {
441 ret = ERR_PTR(-EINVAL);
442 goto error1;
443 }
444 qpn = get_spl_qp_index(qp_type);
445 if (qpn == -1) {
446 ret = ERR_PTR(-EINVAL);
447 goto error1;
448 }
449 port_priv = ib_get_mad_port(device, port_num);
450 if (!port_priv) {
451 ret = ERR_PTR(-ENODEV);
452 goto error1;
453 }
454 /* Allocate structures */
de6eb66b 455 mad_snoop_priv = kzalloc(sizeof *mad_snoop_priv, GFP_KERNEL);
1da177e4
LT
456 if (!mad_snoop_priv) {
457 ret = ERR_PTR(-ENOMEM);
458 goto error1;
459 }
460
461 /* Now, fill in the various structures */
1da177e4
LT
462 mad_snoop_priv->qp_info = &port_priv->qp_info[qpn];
463 mad_snoop_priv->agent.device = device;
464 mad_snoop_priv->agent.recv_handler = recv_handler;
465 mad_snoop_priv->agent.snoop_handler = snoop_handler;
466 mad_snoop_priv->agent.context = context;
467 mad_snoop_priv->agent.qp = port_priv->qp_info[qpn].qp;
468 mad_snoop_priv->agent.port_num = port_num;
469 mad_snoop_priv->mad_snoop_flags = mad_snoop_flags;
1b52fa98 470 init_completion(&mad_snoop_priv->comp);
1da177e4
LT
471 mad_snoop_priv->snoop_index = register_snoop_agent(
472 &port_priv->qp_info[qpn],
473 mad_snoop_priv);
474 if (mad_snoop_priv->snoop_index < 0) {
475 ret = ERR_PTR(mad_snoop_priv->snoop_index);
476 goto error2;
477 }
478
479 atomic_set(&mad_snoop_priv->refcount, 1);
480 return &mad_snoop_priv->agent;
481
482error2:
483 kfree(mad_snoop_priv);
484error1:
485 return ret;
486}
487EXPORT_SYMBOL(ib_register_mad_snoop);
488
1b52fa98
SH
489static inline void deref_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
490{
491 if (atomic_dec_and_test(&mad_agent_priv->refcount))
492 complete(&mad_agent_priv->comp);
493}
494
495static inline void deref_snoop_agent(struct ib_mad_snoop_private *mad_snoop_priv)
496{
497 if (atomic_dec_and_test(&mad_snoop_priv->refcount))
498 complete(&mad_snoop_priv->comp);
499}
500
1da177e4
LT
501static void unregister_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
502{
503 struct ib_mad_port_private *port_priv;
504 unsigned long flags;
505
506 /* Note that we could still be handling received MADs */
507
508 /*
509 * Canceling all sends results in dropping received response
510 * MADs, preventing us from queuing additional work
511 */
512 cancel_mads(mad_agent_priv);
1da177e4 513 port_priv = mad_agent_priv->qp_info->port_priv;
1da177e4 514 cancel_delayed_work(&mad_agent_priv->timed_work);
1da177e4
LT
515
516 spin_lock_irqsave(&port_priv->reg_lock, flags);
517 remove_mad_reg_req(mad_agent_priv);
518 list_del(&mad_agent_priv->agent_list);
519 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
520
b82cab6b 521 flush_workqueue(port_priv->wq);
fa619a77 522 ib_cancel_rmpp_recvs(mad_agent_priv);
1da177e4 523
1b52fa98
SH
524 deref_mad_agent(mad_agent_priv);
525 wait_for_completion(&mad_agent_priv->comp);
1da177e4 526
6044ec88 527 kfree(mad_agent_priv->reg_req);
b82cab6b 528 ib_dereg_mr(mad_agent_priv->agent.mr);
1da177e4
LT
529 kfree(mad_agent_priv);
530}
531
532static void unregister_mad_snoop(struct ib_mad_snoop_private *mad_snoop_priv)
533{
534 struct ib_mad_qp_info *qp_info;
535 unsigned long flags;
536
537 qp_info = mad_snoop_priv->qp_info;
538 spin_lock_irqsave(&qp_info->snoop_lock, flags);
539 qp_info->snoop_table[mad_snoop_priv->snoop_index] = NULL;
540 atomic_dec(&qp_info->snoop_count);
541 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
542
1b52fa98
SH
543 deref_snoop_agent(mad_snoop_priv);
544 wait_for_completion(&mad_snoop_priv->comp);
1da177e4
LT
545
546 kfree(mad_snoop_priv);
547}
548
549/*
550 * ib_unregister_mad_agent - Unregisters a client from using MAD services
551 */
552int ib_unregister_mad_agent(struct ib_mad_agent *mad_agent)
553{
554 struct ib_mad_agent_private *mad_agent_priv;
555 struct ib_mad_snoop_private *mad_snoop_priv;
556
557 /* If the TID is zero, the agent can only snoop. */
558 if (mad_agent->hi_tid) {
559 mad_agent_priv = container_of(mad_agent,
560 struct ib_mad_agent_private,
561 agent);
562 unregister_mad_agent(mad_agent_priv);
563 } else {
564 mad_snoop_priv = container_of(mad_agent,
565 struct ib_mad_snoop_private,
566 agent);
567 unregister_mad_snoop(mad_snoop_priv);
568 }
569 return 0;
570}
571EXPORT_SYMBOL(ib_unregister_mad_agent);
572
4a0754fa
HR
573static inline int response_mad(struct ib_mad *mad)
574{
575 /* Trap represses are responses although response bit is reset */
576 return ((mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS) ||
577 (mad->mad_hdr.method & IB_MGMT_METHOD_RESP));
578}
579
1da177e4
LT
580static void dequeue_mad(struct ib_mad_list_head *mad_list)
581{
582 struct ib_mad_queue *mad_queue;
583 unsigned long flags;
584
585 BUG_ON(!mad_list->mad_queue);
586 mad_queue = mad_list->mad_queue;
587 spin_lock_irqsave(&mad_queue->lock, flags);
588 list_del(&mad_list->list);
589 mad_queue->count--;
590 spin_unlock_irqrestore(&mad_queue->lock, flags);
591}
592
593static void snoop_send(struct ib_mad_qp_info *qp_info,
34816ad9 594 struct ib_mad_send_buf *send_buf,
1da177e4
LT
595 struct ib_mad_send_wc *mad_send_wc,
596 int mad_snoop_flags)
597{
598 struct ib_mad_snoop_private *mad_snoop_priv;
599 unsigned long flags;
600 int i;
601
602 spin_lock_irqsave(&qp_info->snoop_lock, flags);
603 for (i = 0; i < qp_info->snoop_table_size; i++) {
604 mad_snoop_priv = qp_info->snoop_table[i];
605 if (!mad_snoop_priv ||
606 !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
607 continue;
608
609 atomic_inc(&mad_snoop_priv->refcount);
610 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
611 mad_snoop_priv->agent.snoop_handler(&mad_snoop_priv->agent,
34816ad9 612 send_buf, mad_send_wc);
1b52fa98 613 deref_snoop_agent(mad_snoop_priv);
1da177e4
LT
614 spin_lock_irqsave(&qp_info->snoop_lock, flags);
615 }
616 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
617}
618
619static void snoop_recv(struct ib_mad_qp_info *qp_info,
620 struct ib_mad_recv_wc *mad_recv_wc,
621 int mad_snoop_flags)
622{
623 struct ib_mad_snoop_private *mad_snoop_priv;
624 unsigned long flags;
625 int i;
626
627 spin_lock_irqsave(&qp_info->snoop_lock, flags);
628 for (i = 0; i < qp_info->snoop_table_size; i++) {
629 mad_snoop_priv = qp_info->snoop_table[i];
630 if (!mad_snoop_priv ||
631 !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
632 continue;
633
634 atomic_inc(&mad_snoop_priv->refcount);
635 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
636 mad_snoop_priv->agent.recv_handler(&mad_snoop_priv->agent,
637 mad_recv_wc);
1b52fa98 638 deref_snoop_agent(mad_snoop_priv);
1da177e4
LT
639 spin_lock_irqsave(&qp_info->snoop_lock, flags);
640 }
641 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
642}
643
644static void build_smp_wc(u64 wr_id, u16 slid, u16 pkey_index, u8 port_num,
645 struct ib_wc *wc)
646{
647 memset(wc, 0, sizeof *wc);
648 wc->wr_id = wr_id;
649 wc->status = IB_WC_SUCCESS;
650 wc->opcode = IB_WC_RECV;
651 wc->pkey_index = pkey_index;
652 wc->byte_len = sizeof(struct ib_mad) + sizeof(struct ib_grh);
653 wc->src_qp = IB_QP0;
654 wc->qp_num = IB_QP0;
655 wc->slid = slid;
656 wc->sl = 0;
657 wc->dlid_path_bits = 0;
658 wc->port_num = port_num;
659}
660
661/*
662 * Return 0 if SMP is to be sent
663 * Return 1 if SMP was consumed locally (whether or not solicited)
664 * Return < 0 if error
665 */
666static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
34816ad9 667 struct ib_mad_send_wr_private *mad_send_wr)
1da177e4 668{
4a0754fa 669 int ret;
34816ad9 670 struct ib_smp *smp = mad_send_wr->send_buf.mad;
1da177e4
LT
671 unsigned long flags;
672 struct ib_mad_local_private *local;
673 struct ib_mad_private *mad_priv;
674 struct ib_mad_port_private *port_priv;
675 struct ib_mad_agent_private *recv_mad_agent = NULL;
676 struct ib_device *device = mad_agent_priv->agent.device;
677 u8 port_num = mad_agent_priv->agent.port_num;
678 struct ib_wc mad_wc;
34816ad9 679 struct ib_send_wr *send_wr = &mad_send_wr->send_wr;
1da177e4 680
8cf3f04f
RC
681 /*
682 * Directed route handling starts if the initial LID routed part of
683 * a request or the ending LID routed part of a response is empty.
684 * If we are at the start of the LID routed part, don't update the
685 * hop_ptr or hop_cnt. See section 14.2.2, Vol 1 IB spec.
686 */
687 if ((ib_get_smp_direction(smp) ? smp->dr_dlid : smp->dr_slid) ==
688 IB_LID_PERMISSIVE &&
689 !smi_handle_dr_smp_send(smp, device->node_type, port_num)) {
1da177e4
LT
690 ret = -EINVAL;
691 printk(KERN_ERR PFX "Invalid directed route\n");
692 goto out;
693 }
694 /* Check to post send on QP or process locally */
5e9f71a1
RC
695 ret = smi_check_local_smp(smp, device);
696 if (!ret)
1da177e4
LT
697 goto out;
698
699 local = kmalloc(sizeof *local, GFP_ATOMIC);
700 if (!local) {
701 ret = -ENOMEM;
702 printk(KERN_ERR PFX "No memory for ib_mad_local_private\n");
703 goto out;
704 }
705 local->mad_priv = NULL;
706 local->recv_mad_agent = NULL;
707 mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_ATOMIC);
708 if (!mad_priv) {
709 ret = -ENOMEM;
710 printk(KERN_ERR PFX "No memory for local response MAD\n");
711 kfree(local);
712 goto out;
713 }
714
97f52eb4
SH
715 build_smp_wc(send_wr->wr_id, be16_to_cpu(smp->dr_slid),
716 send_wr->wr.ud.pkey_index,
1da177e4
LT
717 send_wr->wr.ud.port_num, &mad_wc);
718
719 /* No GRH for DR SMP */
720 ret = device->process_mad(device, 0, port_num, &mad_wc, NULL,
721 (struct ib_mad *)smp,
722 (struct ib_mad *)&mad_priv->mad);
723 switch (ret)
724 {
725 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY:
4a0754fa 726 if (response_mad(&mad_priv->mad.mad) &&
1da177e4
LT
727 mad_agent_priv->agent.recv_handler) {
728 local->mad_priv = mad_priv;
729 local->recv_mad_agent = mad_agent_priv;
730 /*
731 * Reference MAD agent until receive
732 * side of local completion handled
733 */
734 atomic_inc(&mad_agent_priv->refcount);
735 } else
736 kmem_cache_free(ib_mad_cache, mad_priv);
737 break;
738 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED:
739 kmem_cache_free(ib_mad_cache, mad_priv);
740 break;
741 case IB_MAD_RESULT_SUCCESS:
742 /* Treat like an incoming receive MAD */
1da177e4
LT
743 port_priv = ib_get_mad_port(mad_agent_priv->agent.device,
744 mad_agent_priv->agent.port_num);
745 if (port_priv) {
746 mad_priv->mad.mad.mad_hdr.tid =
747 ((struct ib_mad *)smp)->mad_hdr.tid;
748 recv_mad_agent = find_mad_agent(port_priv,
4a0754fa 749 &mad_priv->mad.mad);
1da177e4
LT
750 }
751 if (!port_priv || !recv_mad_agent) {
752 kmem_cache_free(ib_mad_cache, mad_priv);
753 kfree(local);
754 ret = 0;
755 goto out;
756 }
757 local->mad_priv = mad_priv;
758 local->recv_mad_agent = recv_mad_agent;
759 break;
760 default:
761 kmem_cache_free(ib_mad_cache, mad_priv);
762 kfree(local);
763 ret = -EINVAL;
764 goto out;
765 }
766
34816ad9 767 local->mad_send_wr = mad_send_wr;
1da177e4
LT
768 /* Reference MAD agent until send side of local completion handled */
769 atomic_inc(&mad_agent_priv->refcount);
770 /* Queue local completion to local list */
771 spin_lock_irqsave(&mad_agent_priv->lock, flags);
772 list_add_tail(&local->completion_list, &mad_agent_priv->local_list);
773 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
774 queue_work(mad_agent_priv->qp_info->port_priv->wq,
b82cab6b 775 &mad_agent_priv->local_work);
1da177e4
LT
776 ret = 1;
777out:
778 return ret;
779}
780
f36e1793 781static int get_pad_size(int hdr_len, int data_len)
824c8ae7
HR
782{
783 int seg_size, pad;
784
785 seg_size = sizeof(struct ib_mad) - hdr_len;
786 if (data_len && seg_size) {
787 pad = seg_size - data_len % seg_size;
f36e1793 788 return pad == seg_size ? 0 : pad;
824c8ae7 789 } else
f36e1793
JM
790 return seg_size;
791}
792
793static void free_send_rmpp_list(struct ib_mad_send_wr_private *mad_send_wr)
794{
795 struct ib_rmpp_segment *s, *t;
796
797 list_for_each_entry_safe(s, t, &mad_send_wr->rmpp_list, list) {
798 list_del(&s->list);
799 kfree(s);
800 }
801}
802
803static int alloc_send_rmpp_list(struct ib_mad_send_wr_private *send_wr,
804 gfp_t gfp_mask)
805{
806 struct ib_mad_send_buf *send_buf = &send_wr->send_buf;
807 struct ib_rmpp_mad *rmpp_mad = send_buf->mad;
808 struct ib_rmpp_segment *seg = NULL;
809 int left, seg_size, pad;
810
811 send_buf->seg_size = sizeof (struct ib_mad) - send_buf->hdr_len;
812 seg_size = send_buf->seg_size;
813 pad = send_wr->pad;
814
815 /* Allocate data segments. */
816 for (left = send_buf->data_len + pad; left > 0; left -= seg_size) {
817 seg = kmalloc(sizeof (*seg) + seg_size, gfp_mask);
818 if (!seg) {
819 printk(KERN_ERR "alloc_send_rmpp_segs: RMPP mem "
820 "alloc failed for len %zd, gfp %#x\n",
821 sizeof (*seg) + seg_size, gfp_mask);
822 free_send_rmpp_list(send_wr);
823 return -ENOMEM;
824 }
825 seg->num = ++send_buf->seg_count;
826 list_add_tail(&seg->list, &send_wr->rmpp_list);
827 }
828
829 /* Zero any padding */
830 if (pad)
831 memset(seg->data + seg_size - pad, 0, pad);
832
833 rmpp_mad->rmpp_hdr.rmpp_version = send_wr->mad_agent_priv->
834 agent.rmpp_version;
835 rmpp_mad->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_DATA;
836 ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
837
838 send_wr->cur_seg = container_of(send_wr->rmpp_list.next,
839 struct ib_rmpp_segment, list);
840 send_wr->last_ack_seg = send_wr->cur_seg;
841 return 0;
824c8ae7
HR
842}
843
844struct ib_mad_send_buf * ib_create_send_mad(struct ib_mad_agent *mad_agent,
845 u32 remote_qpn, u16 pkey_index,
34816ad9 846 int rmpp_active,
824c8ae7 847 int hdr_len, int data_len,
dd0fc66f 848 gfp_t gfp_mask)
824c8ae7
HR
849{
850 struct ib_mad_agent_private *mad_agent_priv;
34816ad9 851 struct ib_mad_send_wr_private *mad_send_wr;
f36e1793 852 int pad, message_size, ret, size;
824c8ae7
HR
853 void *buf;
854
34816ad9
SH
855 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
856 agent);
f36e1793
JM
857 pad = get_pad_size(hdr_len, data_len);
858 message_size = hdr_len + data_len + pad;
824c8ae7 859
fa619a77 860 if ((!mad_agent->rmpp_version &&
f36e1793
JM
861 (rmpp_active || message_size > sizeof(struct ib_mad))) ||
862 (!rmpp_active && message_size > sizeof(struct ib_mad)))
fa619a77
HR
863 return ERR_PTR(-EINVAL);
864
f36e1793
JM
865 size = rmpp_active ? hdr_len : sizeof(struct ib_mad);
866 buf = kzalloc(sizeof *mad_send_wr + size, gfp_mask);
824c8ae7
HR
867 if (!buf)
868 return ERR_PTR(-ENOMEM);
34816ad9 869
f36e1793
JM
870 mad_send_wr = buf + size;
871 INIT_LIST_HEAD(&mad_send_wr->rmpp_list);
34816ad9 872 mad_send_wr->send_buf.mad = buf;
f36e1793
JM
873 mad_send_wr->send_buf.hdr_len = hdr_len;
874 mad_send_wr->send_buf.data_len = data_len;
875 mad_send_wr->pad = pad;
34816ad9
SH
876
877 mad_send_wr->mad_agent_priv = mad_agent_priv;
f36e1793 878 mad_send_wr->sg_list[0].length = hdr_len;
34816ad9 879 mad_send_wr->sg_list[0].lkey = mad_agent->mr->lkey;
f36e1793
JM
880 mad_send_wr->sg_list[1].length = sizeof(struct ib_mad) - hdr_len;
881 mad_send_wr->sg_list[1].lkey = mad_agent->mr->lkey;
34816ad9
SH
882
883 mad_send_wr->send_wr.wr_id = (unsigned long) mad_send_wr;
884 mad_send_wr->send_wr.sg_list = mad_send_wr->sg_list;
f36e1793 885 mad_send_wr->send_wr.num_sge = 2;
34816ad9
SH
886 mad_send_wr->send_wr.opcode = IB_WR_SEND;
887 mad_send_wr->send_wr.send_flags = IB_SEND_SIGNALED;
888 mad_send_wr->send_wr.wr.ud.remote_qpn = remote_qpn;
889 mad_send_wr->send_wr.wr.ud.remote_qkey = IB_QP_SET_QKEY;
890 mad_send_wr->send_wr.wr.ud.pkey_index = pkey_index;
fa619a77
HR
891
892 if (rmpp_active) {
f36e1793
JM
893 ret = alloc_send_rmpp_list(mad_send_wr, gfp_mask);
894 if (ret) {
895 kfree(buf);
896 return ERR_PTR(ret);
897 }
fa619a77
HR
898 }
899
34816ad9 900 mad_send_wr->send_buf.mad_agent = mad_agent;
824c8ae7 901 atomic_inc(&mad_agent_priv->refcount);
34816ad9 902 return &mad_send_wr->send_buf;
824c8ae7
HR
903}
904EXPORT_SYMBOL(ib_create_send_mad);
905
618a3c03
HR
906int ib_get_mad_data_offset(u8 mgmt_class)
907{
908 if (mgmt_class == IB_MGMT_CLASS_SUBN_ADM)
909 return IB_MGMT_SA_HDR;
910 else if ((mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
911 (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
912 (mgmt_class == IB_MGMT_CLASS_BIS))
913 return IB_MGMT_DEVICE_HDR;
914 else if ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
915 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END))
916 return IB_MGMT_VENDOR_HDR;
917 else
918 return IB_MGMT_MAD_HDR;
919}
920EXPORT_SYMBOL(ib_get_mad_data_offset);
921
922int ib_is_mad_class_rmpp(u8 mgmt_class)
923{
924 if ((mgmt_class == IB_MGMT_CLASS_SUBN_ADM) ||
925 (mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
926 (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
927 (mgmt_class == IB_MGMT_CLASS_BIS) ||
928 ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
929 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END)))
930 return 1;
931 return 0;
932}
933EXPORT_SYMBOL(ib_is_mad_class_rmpp);
934
f36e1793
JM
935void *ib_get_rmpp_segment(struct ib_mad_send_buf *send_buf, int seg_num)
936{
937 struct ib_mad_send_wr_private *mad_send_wr;
938 struct list_head *list;
939
940 mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
941 send_buf);
942 list = &mad_send_wr->cur_seg->list;
943
944 if (mad_send_wr->cur_seg->num < seg_num) {
945 list_for_each_entry(mad_send_wr->cur_seg, list, list)
946 if (mad_send_wr->cur_seg->num == seg_num)
947 break;
948 } else if (mad_send_wr->cur_seg->num > seg_num) {
949 list_for_each_entry_reverse(mad_send_wr->cur_seg, list, list)
950 if (mad_send_wr->cur_seg->num == seg_num)
951 break;
952 }
953 return mad_send_wr->cur_seg->data;
954}
955EXPORT_SYMBOL(ib_get_rmpp_segment);
956
957static inline void *ib_get_payload(struct ib_mad_send_wr_private *mad_send_wr)
958{
959 if (mad_send_wr->send_buf.seg_count)
960 return ib_get_rmpp_segment(&mad_send_wr->send_buf,
961 mad_send_wr->seg_num);
962 else
963 return mad_send_wr->send_buf.mad +
964 mad_send_wr->send_buf.hdr_len;
965}
966
824c8ae7
HR
967void ib_free_send_mad(struct ib_mad_send_buf *send_buf)
968{
969 struct ib_mad_agent_private *mad_agent_priv;
f36e1793 970 struct ib_mad_send_wr_private *mad_send_wr;
824c8ae7
HR
971
972 mad_agent_priv = container_of(send_buf->mad_agent,
973 struct ib_mad_agent_private, agent);
f36e1793
JM
974 mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
975 send_buf);
824c8ae7 976
f36e1793
JM
977 free_send_rmpp_list(mad_send_wr);
978 kfree(send_buf->mad);
1b52fa98 979 deref_mad_agent(mad_agent_priv);
824c8ae7
HR
980}
981EXPORT_SYMBOL(ib_free_send_mad);
982
fa619a77 983int ib_send_mad(struct ib_mad_send_wr_private *mad_send_wr)
1da177e4
LT
984{
985 struct ib_mad_qp_info *qp_info;
cabe3cbc 986 struct list_head *list;
34816ad9
SH
987 struct ib_send_wr *bad_send_wr;
988 struct ib_mad_agent *mad_agent;
989 struct ib_sge *sge;
1da177e4
LT
990 unsigned long flags;
991 int ret;
992
f8197a4e 993 /* Set WR ID to find mad_send_wr upon completion */
d760ce8f 994 qp_info = mad_send_wr->mad_agent_priv->qp_info;
1da177e4
LT
995 mad_send_wr->send_wr.wr_id = (unsigned long)&mad_send_wr->mad_list;
996 mad_send_wr->mad_list.mad_queue = &qp_info->send_queue;
997
34816ad9
SH
998 mad_agent = mad_send_wr->send_buf.mad_agent;
999 sge = mad_send_wr->sg_list;
f36e1793
JM
1000 sge[0].addr = dma_map_single(mad_agent->device->dma_device,
1001 mad_send_wr->send_buf.mad,
1002 sge[0].length,
1003 DMA_TO_DEVICE);
1004 pci_unmap_addr_set(mad_send_wr, header_mapping, sge[0].addr);
1005
1006 sge[1].addr = dma_map_single(mad_agent->device->dma_device,
1007 ib_get_payload(mad_send_wr),
1008 sge[1].length,
1009 DMA_TO_DEVICE);
1010 pci_unmap_addr_set(mad_send_wr, payload_mapping, sge[1].addr);
34816ad9 1011
1da177e4 1012 spin_lock_irqsave(&qp_info->send_queue.lock, flags);
cabe3cbc 1013 if (qp_info->send_queue.count < qp_info->send_queue.max_active) {
34816ad9
SH
1014 ret = ib_post_send(mad_agent->qp, &mad_send_wr->send_wr,
1015 &bad_send_wr);
cabe3cbc 1016 list = &qp_info->send_queue.list;
1da177e4 1017 } else {
1da177e4 1018 ret = 0;
cabe3cbc 1019 list = &qp_info->overflow_list;
1da177e4 1020 }
cabe3cbc
HR
1021
1022 if (!ret) {
1023 qp_info->send_queue.count++;
1024 list_add_tail(&mad_send_wr->mad_list.list, list);
1025 }
1026 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
f36e1793 1027 if (ret) {
34816ad9 1028 dma_unmap_single(mad_agent->device->dma_device,
f36e1793
JM
1029 pci_unmap_addr(mad_send_wr, header_mapping),
1030 sge[0].length, DMA_TO_DEVICE);
1031 dma_unmap_single(mad_agent->device->dma_device,
1032 pci_unmap_addr(mad_send_wr, payload_mapping),
1033 sge[1].length, DMA_TO_DEVICE);
1034 }
1da177e4
LT
1035 return ret;
1036}
1037
1038/*
1039 * ib_post_send_mad - Posts MAD(s) to the send queue of the QP associated
1040 * with the registered client
1041 */
34816ad9
SH
1042int ib_post_send_mad(struct ib_mad_send_buf *send_buf,
1043 struct ib_mad_send_buf **bad_send_buf)
1da177e4 1044{
1da177e4 1045 struct ib_mad_agent_private *mad_agent_priv;
34816ad9
SH
1046 struct ib_mad_send_buf *next_send_buf;
1047 struct ib_mad_send_wr_private *mad_send_wr;
1048 unsigned long flags;
1049 int ret = -EINVAL;
1da177e4
LT
1050
1051 /* Walk list of send WRs and post each on send list */
34816ad9 1052 for (; send_buf; send_buf = next_send_buf) {
1da177e4 1053
34816ad9
SH
1054 mad_send_wr = container_of(send_buf,
1055 struct ib_mad_send_wr_private,
1056 send_buf);
1057 mad_agent_priv = mad_send_wr->mad_agent_priv;
1da177e4 1058
34816ad9
SH
1059 if (!send_buf->mad_agent->send_handler ||
1060 (send_buf->timeout_ms &&
1061 !send_buf->mad_agent->recv_handler)) {
1062 ret = -EINVAL;
1063 goto error;
1da177e4
LT
1064 }
1065
618a3c03
HR
1066 if (!ib_is_mad_class_rmpp(((struct ib_mad_hdr *) send_buf->mad)->mgmt_class)) {
1067 if (mad_agent_priv->agent.rmpp_version) {
1068 ret = -EINVAL;
1069 goto error;
1070 }
1071 }
1072
1da177e4
LT
1073 /*
1074 * Save pointer to next work request to post in case the
1075 * current one completes, and the user modifies the work
1076 * request associated with the completion
1077 */
34816ad9
SH
1078 next_send_buf = send_buf->next;
1079 mad_send_wr->send_wr.wr.ud.ah = send_buf->ah;
1da177e4 1080
34816ad9
SH
1081 if (((struct ib_mad_hdr *) send_buf->mad)->mgmt_class ==
1082 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
1083 ret = handle_outgoing_dr_smp(mad_agent_priv,
1084 mad_send_wr);
1da177e4 1085 if (ret < 0) /* error */
34816ad9 1086 goto error;
1da177e4 1087 else if (ret == 1) /* locally consumed */
34816ad9 1088 continue;
1da177e4
LT
1089 }
1090
34816ad9 1091 mad_send_wr->tid = ((struct ib_mad_hdr *) send_buf->mad)->tid;
1da177e4 1092 /* Timeout will be updated after send completes */
34816ad9
SH
1093 mad_send_wr->timeout = msecs_to_jiffies(send_buf->timeout_ms);
1094 mad_send_wr->retries = send_buf->retries;
1095 /* Reference for work request to QP + response */
1da177e4
LT
1096 mad_send_wr->refcount = 1 + (mad_send_wr->timeout > 0);
1097 mad_send_wr->status = IB_WC_SUCCESS;
1098
1099 /* Reference MAD agent until send completes */
1100 atomic_inc(&mad_agent_priv->refcount);
1101 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1102 list_add_tail(&mad_send_wr->agent_list,
1103 &mad_agent_priv->send_list);
1104 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1105
fa619a77
HR
1106 if (mad_agent_priv->agent.rmpp_version) {
1107 ret = ib_send_rmpp_mad(mad_send_wr);
1108 if (ret >= 0 && ret != IB_RMPP_RESULT_CONSUMED)
1109 ret = ib_send_mad(mad_send_wr);
1110 } else
1111 ret = ib_send_mad(mad_send_wr);
1112 if (ret < 0) {
1da177e4
LT
1113 /* Fail send request */
1114 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1115 list_del(&mad_send_wr->agent_list);
1116 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1117 atomic_dec(&mad_agent_priv->refcount);
34816ad9 1118 goto error;
1da177e4 1119 }
1da177e4
LT
1120 }
1121 return 0;
34816ad9
SH
1122error:
1123 if (bad_send_buf)
1124 *bad_send_buf = send_buf;
1da177e4
LT
1125 return ret;
1126}
1127EXPORT_SYMBOL(ib_post_send_mad);
1128
1129/*
1130 * ib_free_recv_mad - Returns data buffers used to receive
1131 * a MAD to the access layer
1132 */
1133void ib_free_recv_mad(struct ib_mad_recv_wc *mad_recv_wc)
1134{
fa619a77 1135 struct ib_mad_recv_buf *mad_recv_buf, *temp_recv_buf;
1da177e4
LT
1136 struct ib_mad_private_header *mad_priv_hdr;
1137 struct ib_mad_private *priv;
fa619a77 1138 struct list_head free_list;
1da177e4 1139
fa619a77
HR
1140 INIT_LIST_HEAD(&free_list);
1141 list_splice_init(&mad_recv_wc->rmpp_list, &free_list);
1da177e4 1142
fa619a77
HR
1143 list_for_each_entry_safe(mad_recv_buf, temp_recv_buf,
1144 &free_list, list) {
1145 mad_recv_wc = container_of(mad_recv_buf, struct ib_mad_recv_wc,
1146 recv_buf);
1da177e4
LT
1147 mad_priv_hdr = container_of(mad_recv_wc,
1148 struct ib_mad_private_header,
1149 recv_wc);
1150 priv = container_of(mad_priv_hdr, struct ib_mad_private,
1151 header);
fa619a77 1152 kmem_cache_free(ib_mad_cache, priv);
1da177e4 1153 }
1da177e4
LT
1154}
1155EXPORT_SYMBOL(ib_free_recv_mad);
1156
1da177e4
LT
1157struct ib_mad_agent *ib_redirect_mad_qp(struct ib_qp *qp,
1158 u8 rmpp_version,
1159 ib_mad_send_handler send_handler,
1160 ib_mad_recv_handler recv_handler,
1161 void *context)
1162{
1163 return ERR_PTR(-EINVAL); /* XXX: for now */
1164}
1165EXPORT_SYMBOL(ib_redirect_mad_qp);
1166
1167int ib_process_mad_wc(struct ib_mad_agent *mad_agent,
1168 struct ib_wc *wc)
1169{
1170 printk(KERN_ERR PFX "ib_process_mad_wc() not implemented yet\n");
1171 return 0;
1172}
1173EXPORT_SYMBOL(ib_process_mad_wc);
1174
1175static int method_in_use(struct ib_mad_mgmt_method_table **method,
1176 struct ib_mad_reg_req *mad_reg_req)
1177{
1178 int i;
1179
1180 for (i = find_first_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS);
1181 i < IB_MGMT_MAX_METHODS;
1182 i = find_next_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS,
1183 1+i)) {
1184 if ((*method)->agent[i]) {
1185 printk(KERN_ERR PFX "Method %d already in use\n", i);
1186 return -EINVAL;
1187 }
1188 }
1189 return 0;
1190}
1191
1192static int allocate_method_table(struct ib_mad_mgmt_method_table **method)
1193{
1194 /* Allocate management method table */
de6eb66b 1195 *method = kzalloc(sizeof **method, GFP_ATOMIC);
1da177e4
LT
1196 if (!*method) {
1197 printk(KERN_ERR PFX "No memory for "
1198 "ib_mad_mgmt_method_table\n");
1199 return -ENOMEM;
1200 }
1da177e4
LT
1201
1202 return 0;
1203}
1204
1205/*
1206 * Check to see if there are any methods still in use
1207 */
1208static int check_method_table(struct ib_mad_mgmt_method_table *method)
1209{
1210 int i;
1211
1212 for (i = 0; i < IB_MGMT_MAX_METHODS; i++)
1213 if (method->agent[i])
1214 return 1;
1215 return 0;
1216}
1217
1218/*
1219 * Check to see if there are any method tables for this class still in use
1220 */
1221static int check_class_table(struct ib_mad_mgmt_class_table *class)
1222{
1223 int i;
1224
1225 for (i = 0; i < MAX_MGMT_CLASS; i++)
1226 if (class->method_table[i])
1227 return 1;
1228 return 0;
1229}
1230
1231static int check_vendor_class(struct ib_mad_mgmt_vendor_class *vendor_class)
1232{
1233 int i;
1234
1235 for (i = 0; i < MAX_MGMT_OUI; i++)
1236 if (vendor_class->method_table[i])
1237 return 1;
1238 return 0;
1239}
1240
1241static int find_vendor_oui(struct ib_mad_mgmt_vendor_class *vendor_class,
1242 char *oui)
1243{
1244 int i;
1245
1246 for (i = 0; i < MAX_MGMT_OUI; i++)
1247 /* Is there matching OUI for this vendor class ? */
1248 if (!memcmp(vendor_class->oui[i], oui, 3))
1249 return i;
1250
1251 return -1;
1252}
1253
1254static int check_vendor_table(struct ib_mad_mgmt_vendor_class_table *vendor)
1255{
1256 int i;
1257
1258 for (i = 0; i < MAX_MGMT_VENDOR_RANGE2; i++)
1259 if (vendor->vendor_class[i])
1260 return 1;
1261
1262 return 0;
1263}
1264
1265static void remove_methods_mad_agent(struct ib_mad_mgmt_method_table *method,
1266 struct ib_mad_agent_private *agent)
1267{
1268 int i;
1269
1270 /* Remove any methods for this mad agent */
1271 for (i = 0; i < IB_MGMT_MAX_METHODS; i++) {
1272 if (method->agent[i] == agent) {
1273 method->agent[i] = NULL;
1274 }
1275 }
1276}
1277
1278static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1279 struct ib_mad_agent_private *agent_priv,
1280 u8 mgmt_class)
1281{
1282 struct ib_mad_port_private *port_priv;
1283 struct ib_mad_mgmt_class_table **class;
1284 struct ib_mad_mgmt_method_table **method;
1285 int i, ret;
1286
1287 port_priv = agent_priv->qp_info->port_priv;
1288 class = &port_priv->version[mad_reg_req->mgmt_class_version].class;
1289 if (!*class) {
1290 /* Allocate management class table for "new" class version */
de6eb66b 1291 *class = kzalloc(sizeof **class, GFP_ATOMIC);
1da177e4
LT
1292 if (!*class) {
1293 printk(KERN_ERR PFX "No memory for "
1294 "ib_mad_mgmt_class_table\n");
1295 ret = -ENOMEM;
1296 goto error1;
1297 }
de6eb66b 1298
1da177e4
LT
1299 /* Allocate method table for this management class */
1300 method = &(*class)->method_table[mgmt_class];
1301 if ((ret = allocate_method_table(method)))
1302 goto error2;
1303 } else {
1304 method = &(*class)->method_table[mgmt_class];
1305 if (!*method) {
1306 /* Allocate method table for this management class */
1307 if ((ret = allocate_method_table(method)))
1308 goto error1;
1309 }
1310 }
1311
1312 /* Now, make sure methods are not already in use */
1313 if (method_in_use(method, mad_reg_req))
1314 goto error3;
1315
1316 /* Finally, add in methods being registered */
1317 for (i = find_first_bit(mad_reg_req->method_mask,
1318 IB_MGMT_MAX_METHODS);
1319 i < IB_MGMT_MAX_METHODS;
1320 i = find_next_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS,
1321 1+i)) {
1322 (*method)->agent[i] = agent_priv;
1323 }
1324 return 0;
1325
1326error3:
1327 /* Remove any methods for this mad agent */
1328 remove_methods_mad_agent(*method, agent_priv);
1329 /* Now, check to see if there are any methods in use */
1330 if (!check_method_table(*method)) {
1331 /* If not, release management method table */
1332 kfree(*method);
1333 *method = NULL;
1334 }
1335 ret = -EINVAL;
1336 goto error1;
1337error2:
1338 kfree(*class);
1339 *class = NULL;
1340error1:
1341 return ret;
1342}
1343
1344static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1345 struct ib_mad_agent_private *agent_priv)
1346{
1347 struct ib_mad_port_private *port_priv;
1348 struct ib_mad_mgmt_vendor_class_table **vendor_table;
1349 struct ib_mad_mgmt_vendor_class_table *vendor = NULL;
1350 struct ib_mad_mgmt_vendor_class *vendor_class = NULL;
1351 struct ib_mad_mgmt_method_table **method;
1352 int i, ret = -ENOMEM;
1353 u8 vclass;
1354
1355 /* "New" vendor (with OUI) class */
1356 vclass = vendor_class_index(mad_reg_req->mgmt_class);
1357 port_priv = agent_priv->qp_info->port_priv;
1358 vendor_table = &port_priv->version[
1359 mad_reg_req->mgmt_class_version].vendor;
1360 if (!*vendor_table) {
1361 /* Allocate mgmt vendor class table for "new" class version */
de6eb66b 1362 vendor = kzalloc(sizeof *vendor, GFP_ATOMIC);
1da177e4
LT
1363 if (!vendor) {
1364 printk(KERN_ERR PFX "No memory for "
1365 "ib_mad_mgmt_vendor_class_table\n");
1366 goto error1;
1367 }
de6eb66b 1368
1da177e4
LT
1369 *vendor_table = vendor;
1370 }
1371 if (!(*vendor_table)->vendor_class[vclass]) {
1372 /* Allocate table for this management vendor class */
de6eb66b 1373 vendor_class = kzalloc(sizeof *vendor_class, GFP_ATOMIC);
1da177e4
LT
1374 if (!vendor_class) {
1375 printk(KERN_ERR PFX "No memory for "
1376 "ib_mad_mgmt_vendor_class\n");
1377 goto error2;
1378 }
de6eb66b 1379
1da177e4
LT
1380 (*vendor_table)->vendor_class[vclass] = vendor_class;
1381 }
1382 for (i = 0; i < MAX_MGMT_OUI; i++) {
1383 /* Is there matching OUI for this vendor class ? */
1384 if (!memcmp((*vendor_table)->vendor_class[vclass]->oui[i],
1385 mad_reg_req->oui, 3)) {
1386 method = &(*vendor_table)->vendor_class[
1387 vclass]->method_table[i];
1388 BUG_ON(!*method);
1389 goto check_in_use;
1390 }
1391 }
1392 for (i = 0; i < MAX_MGMT_OUI; i++) {
1393 /* OUI slot available ? */
1394 if (!is_vendor_oui((*vendor_table)->vendor_class[
1395 vclass]->oui[i])) {
1396 method = &(*vendor_table)->vendor_class[
1397 vclass]->method_table[i];
1398 BUG_ON(*method);
1399 /* Allocate method table for this OUI */
1400 if ((ret = allocate_method_table(method)))
1401 goto error3;
1402 memcpy((*vendor_table)->vendor_class[vclass]->oui[i],
1403 mad_reg_req->oui, 3);
1404 goto check_in_use;
1405 }
1406 }
1407 printk(KERN_ERR PFX "All OUI slots in use\n");
1408 goto error3;
1409
1410check_in_use:
1411 /* Now, make sure methods are not already in use */
1412 if (method_in_use(method, mad_reg_req))
1413 goto error4;
1414
1415 /* Finally, add in methods being registered */
1416 for (i = find_first_bit(mad_reg_req->method_mask,
1417 IB_MGMT_MAX_METHODS);
1418 i < IB_MGMT_MAX_METHODS;
1419 i = find_next_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS,
1420 1+i)) {
1421 (*method)->agent[i] = agent_priv;
1422 }
1423 return 0;
1424
1425error4:
1426 /* Remove any methods for this mad agent */
1427 remove_methods_mad_agent(*method, agent_priv);
1428 /* Now, check to see if there are any methods in use */
1429 if (!check_method_table(*method)) {
1430 /* If not, release management method table */
1431 kfree(*method);
1432 *method = NULL;
1433 }
1434 ret = -EINVAL;
1435error3:
1436 if (vendor_class) {
1437 (*vendor_table)->vendor_class[vclass] = NULL;
1438 kfree(vendor_class);
1439 }
1440error2:
1441 if (vendor) {
1442 *vendor_table = NULL;
1443 kfree(vendor);
1444 }
1445error1:
1446 return ret;
1447}
1448
1449static void remove_mad_reg_req(struct ib_mad_agent_private *agent_priv)
1450{
1451 struct ib_mad_port_private *port_priv;
1452 struct ib_mad_mgmt_class_table *class;
1453 struct ib_mad_mgmt_method_table *method;
1454 struct ib_mad_mgmt_vendor_class_table *vendor;
1455 struct ib_mad_mgmt_vendor_class *vendor_class;
1456 int index;
1457 u8 mgmt_class;
1458
1459 /*
1460 * Was MAD registration request supplied
1461 * with original registration ?
1462 */
1463 if (!agent_priv->reg_req) {
1464 goto out;
1465 }
1466
1467 port_priv = agent_priv->qp_info->port_priv;
1468 mgmt_class = convert_mgmt_class(agent_priv->reg_req->mgmt_class);
1469 class = port_priv->version[
1470 agent_priv->reg_req->mgmt_class_version].class;
1471 if (!class)
1472 goto vendor_check;
1473
1474 method = class->method_table[mgmt_class];
1475 if (method) {
1476 /* Remove any methods for this mad agent */
1477 remove_methods_mad_agent(method, agent_priv);
1478 /* Now, check to see if there are any methods still in use */
1479 if (!check_method_table(method)) {
1480 /* If not, release management method table */
1481 kfree(method);
1482 class->method_table[mgmt_class] = NULL;
1483 /* Any management classes left ? */
1484 if (!check_class_table(class)) {
1485 /* If not, release management class table */
1486 kfree(class);
1487 port_priv->version[
1488 agent_priv->reg_req->
1489 mgmt_class_version].class = NULL;
1490 }
1491 }
1492 }
1493
1494vendor_check:
1495 if (!is_vendor_class(mgmt_class))
1496 goto out;
1497
1498 /* normalize mgmt_class to vendor range 2 */
1499 mgmt_class = vendor_class_index(agent_priv->reg_req->mgmt_class);
1500 vendor = port_priv->version[
1501 agent_priv->reg_req->mgmt_class_version].vendor;
1502
1503 if (!vendor)
1504 goto out;
1505
1506 vendor_class = vendor->vendor_class[mgmt_class];
1507 if (vendor_class) {
1508 index = find_vendor_oui(vendor_class, agent_priv->reg_req->oui);
1509 if (index < 0)
1510 goto out;
1511 method = vendor_class->method_table[index];
1512 if (method) {
1513 /* Remove any methods for this mad agent */
1514 remove_methods_mad_agent(method, agent_priv);
1515 /*
1516 * Now, check to see if there are
1517 * any methods still in use
1518 */
1519 if (!check_method_table(method)) {
1520 /* If not, release management method table */
1521 kfree(method);
1522 vendor_class->method_table[index] = NULL;
1523 memset(vendor_class->oui[index], 0, 3);
1524 /* Any OUIs left ? */
1525 if (!check_vendor_class(vendor_class)) {
1526 /* If not, release vendor class table */
1527 kfree(vendor_class);
1528 vendor->vendor_class[mgmt_class] = NULL;
1529 /* Any other vendor classes left ? */
1530 if (!check_vendor_table(vendor)) {
1531 kfree(vendor);
1532 port_priv->version[
1533 agent_priv->reg_req->
1534 mgmt_class_version].
1535 vendor = NULL;
1536 }
1537 }
1538 }
1539 }
1540 }
1541
1542out:
1543 return;
1544}
1545
1da177e4
LT
1546static struct ib_mad_agent_private *
1547find_mad_agent(struct ib_mad_port_private *port_priv,
4a0754fa 1548 struct ib_mad *mad)
1da177e4
LT
1549{
1550 struct ib_mad_agent_private *mad_agent = NULL;
1551 unsigned long flags;
1552
1553 spin_lock_irqsave(&port_priv->reg_lock, flags);
4a0754fa 1554 if (response_mad(mad)) {
1da177e4
LT
1555 u32 hi_tid;
1556 struct ib_mad_agent_private *entry;
1557
1558 /*
1559 * Routing is based on high 32 bits of transaction ID
1560 * of MAD.
1561 */
1562 hi_tid = be64_to_cpu(mad->mad_hdr.tid) >> 32;
34816ad9 1563 list_for_each_entry(entry, &port_priv->agent_list, agent_list) {
1da177e4
LT
1564 if (entry->agent.hi_tid == hi_tid) {
1565 mad_agent = entry;
1566 break;
1567 }
1568 }
1569 } else {
1570 struct ib_mad_mgmt_class_table *class;
1571 struct ib_mad_mgmt_method_table *method;
1572 struct ib_mad_mgmt_vendor_class_table *vendor;
1573 struct ib_mad_mgmt_vendor_class *vendor_class;
1574 struct ib_vendor_mad *vendor_mad;
1575 int index;
1576
1577 /*
1578 * Routing is based on version, class, and method
1579 * For "newer" vendor MADs, also based on OUI
1580 */
1581 if (mad->mad_hdr.class_version >= MAX_MGMT_VERSION)
1582 goto out;
1583 if (!is_vendor_class(mad->mad_hdr.mgmt_class)) {
1584 class = port_priv->version[
1585 mad->mad_hdr.class_version].class;
1586 if (!class)
1587 goto out;
1588 method = class->method_table[convert_mgmt_class(
1589 mad->mad_hdr.mgmt_class)];
1590 if (method)
1591 mad_agent = method->agent[mad->mad_hdr.method &
1592 ~IB_MGMT_METHOD_RESP];
1593 } else {
1594 vendor = port_priv->version[
1595 mad->mad_hdr.class_version].vendor;
1596 if (!vendor)
1597 goto out;
1598 vendor_class = vendor->vendor_class[vendor_class_index(
1599 mad->mad_hdr.mgmt_class)];
1600 if (!vendor_class)
1601 goto out;
1602 /* Find matching OUI */
1603 vendor_mad = (struct ib_vendor_mad *)mad;
1604 index = find_vendor_oui(vendor_class, vendor_mad->oui);
1605 if (index == -1)
1606 goto out;
1607 method = vendor_class->method_table[index];
1608 if (method) {
1609 mad_agent = method->agent[mad->mad_hdr.method &
1610 ~IB_MGMT_METHOD_RESP];
1611 }
1612 }
1613 }
1614
1615 if (mad_agent) {
1616 if (mad_agent->agent.recv_handler)
1617 atomic_inc(&mad_agent->refcount);
1618 else {
1619 printk(KERN_NOTICE PFX "No receive handler for client "
1620 "%p on port %d\n",
1621 &mad_agent->agent, port_priv->port_num);
1622 mad_agent = NULL;
1623 }
1624 }
1625out:
1626 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
1627
1628 return mad_agent;
1629}
1630
1631static int validate_mad(struct ib_mad *mad, u32 qp_num)
1632{
1633 int valid = 0;
1634
1635 /* Make sure MAD base version is understood */
1636 if (mad->mad_hdr.base_version != IB_MGMT_BASE_VERSION) {
1637 printk(KERN_ERR PFX "MAD received with unsupported base "
1638 "version %d\n", mad->mad_hdr.base_version);
1639 goto out;
1640 }
1641
1642 /* Filter SMI packets sent to other than QP0 */
1643 if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
1644 (mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) {
1645 if (qp_num == 0)
1646 valid = 1;
1647 } else {
1648 /* Filter GSI packets sent to QP0 */
1649 if (qp_num != 0)
1650 valid = 1;
1651 }
1652
1653out:
1654 return valid;
1655}
1656
fa619a77
HR
1657static int is_data_mad(struct ib_mad_agent_private *mad_agent_priv,
1658 struct ib_mad_hdr *mad_hdr)
1659{
1660 struct ib_rmpp_mad *rmpp_mad;
1661
1662 rmpp_mad = (struct ib_rmpp_mad *)mad_hdr;
1663 return !mad_agent_priv->agent.rmpp_version ||
1664 !(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
1665 IB_MGMT_RMPP_FLAG_ACTIVE) ||
1666 (rmpp_mad->rmpp_hdr.rmpp_type == IB_MGMT_RMPP_TYPE_DATA);
1667}
1668
fa9656bb
JM
1669static inline int rcv_has_same_class(struct ib_mad_send_wr_private *wr,
1670 struct ib_mad_recv_wc *rwc)
1671{
1672 return ((struct ib_mad *)(wr->send_buf.mad))->mad_hdr.mgmt_class ==
1673 rwc->recv_buf.mad->mad_hdr.mgmt_class;
1674}
1675
1676static inline int rcv_has_same_gid(struct ib_mad_send_wr_private *wr,
1677 struct ib_mad_recv_wc *rwc )
1678{
1679 struct ib_ah_attr attr;
1680 u8 send_resp, rcv_resp;
1681
1682 send_resp = ((struct ib_mad *)(wr->send_buf.mad))->
1683 mad_hdr.method & IB_MGMT_METHOD_RESP;
1684 rcv_resp = rwc->recv_buf.mad->mad_hdr.method & IB_MGMT_METHOD_RESP;
1685
1686 if (!send_resp && rcv_resp)
1687 /* is request/response. GID/LIDs are both local (same). */
1688 return 1;
1689
1690 if (send_resp == rcv_resp)
1691 /* both requests, or both responses. GIDs different */
1692 return 0;
1693
1694 if (ib_query_ah(wr->send_buf.ah, &attr))
1695 /* Assume not equal, to avoid false positives. */
1696 return 0;
1697
1698 if (!(attr.ah_flags & IB_AH_GRH) && !(rwc->wc->wc_flags & IB_WC_GRH))
1699 return attr.dlid == rwc->wc->slid;
1700 else if ((attr.ah_flags & IB_AH_GRH) &&
1701 (rwc->wc->wc_flags & IB_WC_GRH))
1702 return memcmp(attr.grh.dgid.raw,
1703 rwc->recv_buf.grh->sgid.raw, 16) == 0;
1704 else
1705 /* one has GID, other does not. Assume different */
1706 return 0;
1707}
fa619a77 1708struct ib_mad_send_wr_private*
fa9656bb
JM
1709ib_find_send_mad(struct ib_mad_agent_private *mad_agent_priv,
1710 struct ib_mad_recv_wc *mad_recv_wc)
1da177e4
LT
1711{
1712 struct ib_mad_send_wr_private *mad_send_wr;
fa9656bb
JM
1713 struct ib_mad *mad;
1714
1715 mad = (struct ib_mad *)mad_recv_wc->recv_buf.mad;
1da177e4
LT
1716
1717 list_for_each_entry(mad_send_wr, &mad_agent_priv->wait_list,
1718 agent_list) {
fa9656bb
JM
1719 if ((mad_send_wr->tid == mad->mad_hdr.tid) &&
1720 rcv_has_same_class(mad_send_wr, mad_recv_wc) &&
1721 rcv_has_same_gid(mad_send_wr, mad_recv_wc))
1da177e4
LT
1722 return mad_send_wr;
1723 }
1724
1725 /*
1726 * It's possible to receive the response before we've
1727 * been notified that the send has completed
1728 */
1729 list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list,
1730 agent_list) {
34816ad9 1731 if (is_data_mad(mad_agent_priv, mad_send_wr->send_buf.mad) &&
fa9656bb
JM
1732 mad_send_wr->tid == mad->mad_hdr.tid &&
1733 mad_send_wr->timeout &&
1734 rcv_has_same_class(mad_send_wr, mad_recv_wc) &&
1735 rcv_has_same_gid(mad_send_wr, mad_recv_wc)) {
1da177e4
LT
1736 /* Verify request has not been canceled */
1737 return (mad_send_wr->status == IB_WC_SUCCESS) ?
1738 mad_send_wr : NULL;
1739 }
1740 }
1741 return NULL;
1742}
1743
fa619a77 1744void ib_mark_mad_done(struct ib_mad_send_wr_private *mad_send_wr)
6a0c435e
HR
1745{
1746 mad_send_wr->timeout = 0;
1747 if (mad_send_wr->refcount == 1) {
1748 list_del(&mad_send_wr->agent_list);
1749 list_add_tail(&mad_send_wr->agent_list,
1750 &mad_send_wr->mad_agent_priv->done_list);
1751 }
1752}
1753
1da177e4 1754static void ib_mad_complete_recv(struct ib_mad_agent_private *mad_agent_priv,
4a0754fa 1755 struct ib_mad_recv_wc *mad_recv_wc)
1da177e4
LT
1756{
1757 struct ib_mad_send_wr_private *mad_send_wr;
1758 struct ib_mad_send_wc mad_send_wc;
1759 unsigned long flags;
1760
fa619a77
HR
1761 INIT_LIST_HEAD(&mad_recv_wc->rmpp_list);
1762 list_add(&mad_recv_wc->recv_buf.list, &mad_recv_wc->rmpp_list);
1763 if (mad_agent_priv->agent.rmpp_version) {
1764 mad_recv_wc = ib_process_rmpp_recv_wc(mad_agent_priv,
1765 mad_recv_wc);
1766 if (!mad_recv_wc) {
1b52fa98 1767 deref_mad_agent(mad_agent_priv);
fa619a77
HR
1768 return;
1769 }
1770 }
1771
1da177e4 1772 /* Complete corresponding request */
4a0754fa 1773 if (response_mad(mad_recv_wc->recv_buf.mad)) {
1da177e4 1774 spin_lock_irqsave(&mad_agent_priv->lock, flags);
fa9656bb 1775 mad_send_wr = ib_find_send_mad(mad_agent_priv, mad_recv_wc);
1da177e4
LT
1776 if (!mad_send_wr) {
1777 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
4a0754fa 1778 ib_free_recv_mad(mad_recv_wc);
1b52fa98 1779 deref_mad_agent(mad_agent_priv);
1da177e4
LT
1780 return;
1781 }
fa619a77 1782 ib_mark_mad_done(mad_send_wr);
1da177e4
LT
1783 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1784
1785 /* Defined behavior is to complete response before request */
34816ad9 1786 mad_recv_wc->wc->wr_id = (unsigned long) &mad_send_wr->send_buf;
4a0754fa
HR
1787 mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent,
1788 mad_recv_wc);
1da177e4
LT
1789 atomic_dec(&mad_agent_priv->refcount);
1790
1791 mad_send_wc.status = IB_WC_SUCCESS;
1792 mad_send_wc.vendor_err = 0;
34816ad9 1793 mad_send_wc.send_buf = &mad_send_wr->send_buf;
1da177e4
LT
1794 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
1795 } else {
4a0754fa
HR
1796 mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent,
1797 mad_recv_wc);
1b52fa98 1798 deref_mad_agent(mad_agent_priv);
1da177e4
LT
1799 }
1800}
1801
1802static void ib_mad_recv_done_handler(struct ib_mad_port_private *port_priv,
1803 struct ib_wc *wc)
1804{
1805 struct ib_mad_qp_info *qp_info;
1806 struct ib_mad_private_header *mad_priv_hdr;
1807 struct ib_mad_private *recv, *response;
1808 struct ib_mad_list_head *mad_list;
1809 struct ib_mad_agent_private *mad_agent;
1da177e4
LT
1810
1811 response = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL);
1812 if (!response)
1813 printk(KERN_ERR PFX "ib_mad_recv_done_handler no memory "
1814 "for response buffer\n");
1815
1816 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
1817 qp_info = mad_list->mad_queue->qp_info;
1818 dequeue_mad(mad_list);
1819
1820 mad_priv_hdr = container_of(mad_list, struct ib_mad_private_header,
1821 mad_list);
1822 recv = container_of(mad_priv_hdr, struct ib_mad_private, header);
1823 dma_unmap_single(port_priv->device->dma_device,
1824 pci_unmap_addr(&recv->header, mapping),
1825 sizeof(struct ib_mad_private) -
1826 sizeof(struct ib_mad_private_header),
1827 DMA_FROM_DEVICE);
1828
1829 /* Setup MAD receive work completion from "normal" work completion */
24239aff
SH
1830 recv->header.wc = *wc;
1831 recv->header.recv_wc.wc = &recv->header.wc;
1da177e4
LT
1832 recv->header.recv_wc.mad_len = sizeof(struct ib_mad);
1833 recv->header.recv_wc.recv_buf.mad = &recv->mad.mad;
1834 recv->header.recv_wc.recv_buf.grh = &recv->grh;
1835
1836 if (atomic_read(&qp_info->snoop_count))
1837 snoop_recv(qp_info, &recv->header.recv_wc, IB_MAD_SNOOP_RECVS);
1838
1839 /* Validate MAD */
1840 if (!validate_mad(&recv->mad.mad, qp_info->qp->qp_num))
1841 goto out;
1842
1843 if (recv->mad.mad.mad_hdr.mgmt_class ==
1844 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
1845 if (!smi_handle_dr_smp_recv(&recv->mad.smp,
1846 port_priv->device->node_type,
1847 port_priv->port_num,
1848 port_priv->device->phys_port_cnt))
1849 goto out;
1850 if (!smi_check_forward_dr_smp(&recv->mad.smp))
1851 goto local;
1852 if (!smi_handle_dr_smp_send(&recv->mad.smp,
1853 port_priv->device->node_type,
1854 port_priv->port_num))
1855 goto out;
5e9f71a1 1856 if (!smi_check_local_smp(&recv->mad.smp, port_priv->device))
1da177e4
LT
1857 goto out;
1858 }
1859
1860local:
1861 /* Give driver "right of first refusal" on incoming MAD */
1862 if (port_priv->device->process_mad) {
1863 int ret;
1864
1865 if (!response) {
1866 printk(KERN_ERR PFX "No memory for response MAD\n");
1867 /*
1868 * Is it better to assume that
1869 * it wouldn't be processed ?
1870 */
1871 goto out;
1872 }
1873
1874 ret = port_priv->device->process_mad(port_priv->device, 0,
1875 port_priv->port_num,
1876 wc, &recv->grh,
1877 &recv->mad.mad,
1878 &response->mad.mad);
1879 if (ret & IB_MAD_RESULT_SUCCESS) {
1880 if (ret & IB_MAD_RESULT_CONSUMED)
1881 goto out;
1882 if (ret & IB_MAD_RESULT_REPLY) {
34816ad9
SH
1883 agent_send_response(&response->mad.mad,
1884 &recv->grh, wc,
1885 port_priv->device,
1886 port_priv->port_num,
1887 qp_info->qp->qp_num);
1da177e4
LT
1888 goto out;
1889 }
1890 }
1891 }
1892
4a0754fa 1893 mad_agent = find_mad_agent(port_priv, &recv->mad.mad);
1da177e4 1894 if (mad_agent) {
4a0754fa 1895 ib_mad_complete_recv(mad_agent, &recv->header.recv_wc);
1da177e4
LT
1896 /*
1897 * recv is freed up in error cases in ib_mad_complete_recv
1898 * or via recv_handler in ib_mad_complete_recv()
1899 */
1900 recv = NULL;
1901 }
1902
1903out:
1904 /* Post another receive request for this QP */
1905 if (response) {
1906 ib_mad_post_receive_mads(qp_info, response);
1907 if (recv)
1908 kmem_cache_free(ib_mad_cache, recv);
1909 } else
1910 ib_mad_post_receive_mads(qp_info, recv);
1911}
1912
1913static void adjust_timeout(struct ib_mad_agent_private *mad_agent_priv)
1914{
1915 struct ib_mad_send_wr_private *mad_send_wr;
1916 unsigned long delay;
1917
1918 if (list_empty(&mad_agent_priv->wait_list)) {
1919 cancel_delayed_work(&mad_agent_priv->timed_work);
1920 } else {
1921 mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
1922 struct ib_mad_send_wr_private,
1923 agent_list);
1924
1925 if (time_after(mad_agent_priv->timeout,
1926 mad_send_wr->timeout)) {
1927 mad_agent_priv->timeout = mad_send_wr->timeout;
1928 cancel_delayed_work(&mad_agent_priv->timed_work);
1929 delay = mad_send_wr->timeout - jiffies;
1930 if ((long)delay <= 0)
1931 delay = 1;
1932 queue_delayed_work(mad_agent_priv->qp_info->
1933 port_priv->wq,
1934 &mad_agent_priv->timed_work, delay);
1935 }
1936 }
1937}
1938
d760ce8f 1939static void wait_for_response(struct ib_mad_send_wr_private *mad_send_wr)
1da177e4 1940{
d760ce8f 1941 struct ib_mad_agent_private *mad_agent_priv;
1da177e4
LT
1942 struct ib_mad_send_wr_private *temp_mad_send_wr;
1943 struct list_head *list_item;
1944 unsigned long delay;
1945
d760ce8f 1946 mad_agent_priv = mad_send_wr->mad_agent_priv;
1da177e4
LT
1947 list_del(&mad_send_wr->agent_list);
1948
1949 delay = mad_send_wr->timeout;
1950 mad_send_wr->timeout += jiffies;
1951
29bb33dd
HR
1952 if (delay) {
1953 list_for_each_prev(list_item, &mad_agent_priv->wait_list) {
1954 temp_mad_send_wr = list_entry(list_item,
1955 struct ib_mad_send_wr_private,
1956 agent_list);
1957 if (time_after(mad_send_wr->timeout,
1958 temp_mad_send_wr->timeout))
1959 break;
1960 }
1da177e4 1961 }
29bb33dd
HR
1962 else
1963 list_item = &mad_agent_priv->wait_list;
1da177e4
LT
1964 list_add(&mad_send_wr->agent_list, list_item);
1965
1966 /* Reschedule a work item if we have a shorter timeout */
1967 if (mad_agent_priv->wait_list.next == &mad_send_wr->agent_list) {
1968 cancel_delayed_work(&mad_agent_priv->timed_work);
1969 queue_delayed_work(mad_agent_priv->qp_info->port_priv->wq,
1970 &mad_agent_priv->timed_work, delay);
1971 }
1972}
1973
03b61ad2
HR
1974void ib_reset_mad_timeout(struct ib_mad_send_wr_private *mad_send_wr,
1975 int timeout_ms)
1976{
1977 mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
1978 wait_for_response(mad_send_wr);
1979}
1980
1da177e4
LT
1981/*
1982 * Process a send work completion
1983 */
fa619a77
HR
1984void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr,
1985 struct ib_mad_send_wc *mad_send_wc)
1da177e4
LT
1986{
1987 struct ib_mad_agent_private *mad_agent_priv;
1988 unsigned long flags;
fa619a77 1989 int ret;
1da177e4 1990
d760ce8f 1991 mad_agent_priv = mad_send_wr->mad_agent_priv;
1da177e4 1992 spin_lock_irqsave(&mad_agent_priv->lock, flags);
fa619a77
HR
1993 if (mad_agent_priv->agent.rmpp_version) {
1994 ret = ib_process_rmpp_send_wc(mad_send_wr, mad_send_wc);
1995 if (ret == IB_RMPP_RESULT_CONSUMED)
1996 goto done;
1997 } else
1998 ret = IB_RMPP_RESULT_UNHANDLED;
1999
1da177e4
LT
2000 if (mad_send_wc->status != IB_WC_SUCCESS &&
2001 mad_send_wr->status == IB_WC_SUCCESS) {
2002 mad_send_wr->status = mad_send_wc->status;
2003 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2004 }
2005
2006 if (--mad_send_wr->refcount > 0) {
2007 if (mad_send_wr->refcount == 1 && mad_send_wr->timeout &&
2008 mad_send_wr->status == IB_WC_SUCCESS) {
d760ce8f 2009 wait_for_response(mad_send_wr);
1da177e4 2010 }
fa619a77 2011 goto done;
1da177e4
LT
2012 }
2013
2014 /* Remove send from MAD agent and notify client of completion */
2015 list_del(&mad_send_wr->agent_list);
2016 adjust_timeout(mad_agent_priv);
2017 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2018
2019 if (mad_send_wr->status != IB_WC_SUCCESS )
2020 mad_send_wc->status = mad_send_wr->status;
34816ad9
SH
2021 if (ret == IB_RMPP_RESULT_INTERNAL)
2022 ib_rmpp_send_handler(mad_send_wc);
2023 else
fa619a77
HR
2024 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2025 mad_send_wc);
1da177e4
LT
2026
2027 /* Release reference on agent taken when sending */
1b52fa98 2028 deref_mad_agent(mad_agent_priv);
fa619a77
HR
2029 return;
2030done:
2031 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1da177e4
LT
2032}
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;
34816ad9 2042 struct ib_mad_send_wc mad_send_wc;
1da177e4
LT
2043 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:
34816ad9 2053 dma_unmap_single(mad_send_wr->send_buf.mad_agent->device->dma_device,
f36e1793 2054 pci_unmap_addr(mad_send_wr, header_mapping),
34816ad9 2055 mad_send_wr->sg_list[0].length, DMA_TO_DEVICE);
f36e1793
JM
2056 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);
1da177e4
LT
2059 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
34816ad9
SH
2075 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;
1da177e4 2078 if (atomic_read(&qp_info->snoop_count))
34816ad9 2079 snoop_send(qp_info, &mad_send_wr->send_buf, &mad_send_wc,
1da177e4 2080 IB_MAD_SNOOP_SEND_COMPLETIONS);
34816ad9 2081 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
1da177e4
LT
2082
2083 if (queued_send_wr) {
2084 ret = ib_post_send(qp_info->qp, &queued_send_wr->send_wr,
34816ad9 2085 &bad_send_wr);
1da177e4
LT
2086 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);
2c153b93
HR
2217 /* Empty local completion list as well */
2218 list_splice_init(&mad_agent_priv->local_list, &cancel_list);
1da177e4
LT
2219 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) {
34816ad9
SH
2227 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2228 list_del(&mad_send_wr->agent_list);
1da177e4
LT
2229 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2230 &mad_send_wc);
1da177e4
LT
2231 atomic_dec(&mad_agent_priv->refcount);
2232 }
2233}
2234
2235static struct ib_mad_send_wr_private*
34816ad9
SH
2236find_send_wr(struct ib_mad_agent_private *mad_agent_priv,
2237 struct ib_mad_send_buf *send_buf)
1da177e4
LT
2238{
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) {
34816ad9 2243 if (&mad_send_wr->send_buf == send_buf)
1da177e4
LT
2244 return mad_send_wr;
2245 }
2246
2247 list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list,
2248 agent_list) {
34816ad9
SH
2249 if (is_data_mad(mad_agent_priv, mad_send_wr->send_buf.mad) &&
2250 &mad_send_wr->send_buf == send_buf)
1da177e4
LT
2251 return mad_send_wr;
2252 }
2253 return NULL;
2254}
2255
34816ad9
SH
2256int ib_modify_mad(struct ib_mad_agent *mad_agent,
2257 struct ib_mad_send_buf *send_buf, u32 timeout_ms)
1da177e4
LT
2258{
2259 struct ib_mad_agent_private *mad_agent_priv;
2260 struct ib_mad_send_wr_private *mad_send_wr;
2261 unsigned long flags;
cabe3cbc 2262 int active;
1da177e4
LT
2263
2264 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
2265 agent);
2266 spin_lock_irqsave(&mad_agent_priv->lock, flags);
34816ad9 2267 mad_send_wr = find_send_wr(mad_agent_priv, send_buf);
03b61ad2 2268 if (!mad_send_wr || mad_send_wr->status != IB_WC_SUCCESS) {
1da177e4 2269 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
03b61ad2 2270 return -EINVAL;
1da177e4
LT
2271 }
2272
cabe3cbc 2273 active = (!mad_send_wr->timeout || mad_send_wr->refcount > 1);
03b61ad2 2274 if (!timeout_ms) {
1da177e4 2275 mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
03b61ad2 2276 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
1da177e4
LT
2277 }
2278
34816ad9 2279 mad_send_wr->send_buf.timeout_ms = timeout_ms;
cabe3cbc 2280 if (active)
03b61ad2
HR
2281 mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
2282 else
2283 ib_reset_mad_timeout(mad_send_wr, timeout_ms);
2284
1da177e4 2285 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
03b61ad2
HR
2286 return 0;
2287}
2288EXPORT_SYMBOL(ib_modify_mad);
1da177e4 2289
34816ad9
SH
2290void ib_cancel_mad(struct ib_mad_agent *mad_agent,
2291 struct ib_mad_send_buf *send_buf)
03b61ad2 2292{
34816ad9 2293 ib_modify_mad(mad_agent, send_buf, 0);
1da177e4
LT
2294}
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;
2c153b93 2303 int recv = 0;
1da177e4
LT
2304 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);
37289efe 2314 list_del(&local->completion_list);
1da177e4
LT
2315 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2316 if (local->mad_priv) {
2317 recv_mad_agent = local->recv_mad_agent;
2318 if (!recv_mad_agent) {
2319 printk(KERN_ERR PFX "No receive MAD agent for local completion\n");
1da177e4
LT
2320 goto local_send_completion;
2321 }
2322
2c153b93 2323 recv = 1;
1da177e4
LT
2324 /*
2325 * Defined behavior is to complete response
2326 * before request
2327 */
34816ad9 2328 build_smp_wc((unsigned long) local->mad_send_wr,
97f52eb4 2329 be16_to_cpu(IB_LID_PERMISSIVE),
34816ad9 2330 0, recv_mad_agent->agent.port_num, &wc);
1da177e4
LT
2331
2332 local->mad_priv->header.recv_wc.wc = &wc;
2333 local->mad_priv->header.recv_wc.mad_len =
2334 sizeof(struct ib_mad);
fa619a77
HR
2335 INIT_LIST_HEAD(&local->mad_priv->header.recv_wc.rmpp_list);
2336 list_add(&local->mad_priv->header.recv_wc.recv_buf.list,
2337 &local->mad_priv->header.recv_wc.rmpp_list);
1da177e4
LT
2338 local->mad_priv->header.recv_wc.recv_buf.grh = NULL;
2339 local->mad_priv->header.recv_wc.recv_buf.mad =
2340 &local->mad_priv->mad.mad;
2341 if (atomic_read(&recv_mad_agent->qp_info->snoop_count))
2342 snoop_recv(recv_mad_agent->qp_info,
2343 &local->mad_priv->header.recv_wc,
2344 IB_MAD_SNOOP_RECVS);
2345 recv_mad_agent->agent.recv_handler(
2346 &recv_mad_agent->agent,
2347 &local->mad_priv->header.recv_wc);
2348 spin_lock_irqsave(&recv_mad_agent->lock, flags);
2349 atomic_dec(&recv_mad_agent->refcount);
2350 spin_unlock_irqrestore(&recv_mad_agent->lock, flags);
2351 }
2352
2353local_send_completion:
2354 /* Complete send */
2355 mad_send_wc.status = IB_WC_SUCCESS;
2356 mad_send_wc.vendor_err = 0;
34816ad9 2357 mad_send_wc.send_buf = &local->mad_send_wr->send_buf;
1da177e4 2358 if (atomic_read(&mad_agent_priv->qp_info->snoop_count))
34816ad9
SH
2359 snoop_send(mad_agent_priv->qp_info,
2360 &local->mad_send_wr->send_buf,
2361 &mad_send_wc, IB_MAD_SNOOP_SEND_COMPLETIONS);
1da177e4
LT
2362 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2363 &mad_send_wc);
2364
2365 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1da177e4 2366 atomic_dec(&mad_agent_priv->refcount);
2c153b93
HR
2367 if (!recv)
2368 kmem_cache_free(ib_mad_cache, local->mad_priv);
1da177e4
LT
2369 kfree(local);
2370 }
2371 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2372}
2373
f75b7a52
HR
2374static 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
34816ad9 2381 mad_send_wr->timeout = msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms);
f75b7a52 2382
fa619a77
HR
2383 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);
f75b7a52
HR
2398
2399 if (!ret) {
2400 mad_send_wr->refcount++;
f75b7a52
HR
2401 list_add_tail(&mad_send_wr->agent_list,
2402 &mad_send_wr->mad_agent_priv->send_list);
2403 }
2404 return ret;
2405}
2406
1da177e4
LT
2407static 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;
1da177e4
LT
2415 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
dbf9227b 2433 list_del(&mad_send_wr->agent_list);
29bb33dd
HR
2434 if (mad_send_wr->status == IB_WC_SUCCESS &&
2435 !retry_send(mad_send_wr))
f75b7a52
HR
2436 continue;
2437
1da177e4
LT
2438 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2439
03b61ad2
HR
2440 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;
34816ad9 2444 mad_send_wc.send_buf = &mad_send_wr->send_buf;
1da177e4
LT
2445 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2446 &mad_send_wc);
2447
1da177e4
LT
2448 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
5dd2ce12 2454static void ib_mad_thread_completion_handler(struct ib_cq *cq, void *arg)
1da177e4
LT
2455{
2456 struct ib_mad_port_private *port_priv = cq->cq_context;
dc05980d 2457 unsigned long flags;
1da177e4 2458
dc05980d
MT
2459 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);
1da177e4
LT
2463}
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->
618a3c03
HR
2501 device->dma_device,
2502 &mad_priv->grh,
2503 sizeof *mad_priv -
2504 sizeof mad_priv->header,
2505 DMA_FROM_DEVICE);
1da177e4
LT
2506 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
1da177e4
LT
2558 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);
6044ec88 2706 kfree(qp_info->snoop_table);
1da177e4
LT
2707}
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
1da177e4 2721 /* Create new device info */
de6eb66b 2722 port_priv = kzalloc(sizeof *port_priv, GFP_KERNEL);
1da177e4
LT
2723 if (!port_priv) {
2724 printk(KERN_ERR PFX "No memory for ib_mad_port_private\n");
2725 return -ENOMEM;
2726 }
de6eb66b 2727
1da177e4
LT
2728 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,
5dd2ce12 2737 ib_mad_thread_completion_handler,
1da177e4
LT
2738 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
dc05980d
MT
2774 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
1da177e4
LT
2778 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
1da177e4
LT
2784 return 0;
2785
2786error9:
dc05980d
MT
2787 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
1da177e4
LT
2791 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 }
dc05980d 2827 list_del_init(&port_priv->port_list);
1da177e4
LT
2828 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2829
1da177e4
LT
2830 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{
4ab6fb7e 2847 int start, end, i;
1da177e4
LT
2848
2849 if (device->node_type == IB_NODE_SWITCH) {
4ab6fb7e
RD
2850 start = 0;
2851 end = 0;
1da177e4 2852 } else {
4ab6fb7e
RD
2853 start = 1;
2854 end = device->phys_port_cnt;
1da177e4 2855 }
4ab6fb7e
RD
2856
2857 for (i = start; i <= end; i++) {
2858 if (ib_mad_port_open(device, i)) {
1da177e4 2859 printk(KERN_ERR PFX "Couldn't open %s port %d\n",
4ab6fb7e
RD
2860 device->name, i);
2861 goto error;
1da177e4 2862 }
4ab6fb7e 2863 if (ib_agent_port_open(device, i)) {
1da177e4
LT
2864 printk(KERN_ERR PFX "Couldn't open %s port %d "
2865 "for agents\n",
4ab6fb7e
RD
2866 device->name, i);
2867 goto error_agent;
1da177e4
LT
2868 }
2869 }
f68bcc2d 2870 return;
1da177e4 2871
4ab6fb7e
RD
2872error_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))
1da177e4
LT
2882 printk(KERN_ERR PFX "Couldn't close %s port %d "
2883 "for agents\n",
4ab6fb7e
RD
2884 device->name, i);
2885 if (ib_mad_port_close(device, i))
1da177e4 2886 printk(KERN_ERR PFX "Couldn't close %s port %d\n",
4ab6fb7e 2887 device->name, i);
1da177e4
LT
2888 i--;
2889 }
1da177e4
LT
2890}
2891
2892static void ib_mad_remove_device(struct ib_device *device)
2893{
f68bcc2d 2894 int i, num_ports, cur_port;
1da177e4
LT
2895
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++) {
f68bcc2d 2904 if (ib_agent_port_close(device, cur_port))
1da177e4
LT
2905 printk(KERN_ERR PFX "Couldn't close %s port %d "
2906 "for agents\n",
2907 device->name, cur_port);
f68bcc2d 2908 if (ib_mad_port_close(device, cur_port))
1da177e4
LT
2909 printk(KERN_ERR PFX "Couldn't close %s port %d\n",
2910 device->name, cur_port);
1da177e4
LT
2911 }
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);
1da177e4
LT
2925
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);
fa619a77 2965
This page took 0.255616 seconds and 5 git commands to generate.