2 * Copyright (c) 2004-2007 Voltaire, Inc. All rights reserved.
3 * Copyright (c) 2005 Intel Corporation. All rights reserved.
4 * Copyright (c) 2005 Mellanox Technologies Ltd. All rights reserved.
5 * Copyright (c) 2009 HNR Consulting. All rights reserved.
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 #include <linux/dma-mapping.h>
37 #include <linux/slab.h>
38 #include <linux/module.h>
39 #include <rdma/ib_cache.h>
46 MODULE_LICENSE("Dual BSD/GPL");
47 MODULE_DESCRIPTION("kernel IB MAD API");
48 MODULE_AUTHOR("Hal Rosenstock");
49 MODULE_AUTHOR("Sean Hefty");
51 static int mad_sendq_size
= IB_MAD_QP_SEND_SIZE
;
52 static int mad_recvq_size
= IB_MAD_QP_RECV_SIZE
;
54 module_param_named(send_queue_size
, mad_sendq_size
, int, 0444);
55 MODULE_PARM_DESC(send_queue_size
, "Size of send queue in number of work requests");
56 module_param_named(recv_queue_size
, mad_recvq_size
, int, 0444);
57 MODULE_PARM_DESC(recv_queue_size
, "Size of receive queue in number of work requests");
59 static struct kmem_cache
*ib_mad_cache
;
61 static struct list_head ib_mad_port_list
;
62 static u32 ib_mad_client_id
= 0;
65 static DEFINE_SPINLOCK(ib_mad_port_list_lock
);
67 /* Forward declarations */
68 static int method_in_use(struct ib_mad_mgmt_method_table
**method
,
69 struct ib_mad_reg_req
*mad_reg_req
);
70 static void remove_mad_reg_req(struct ib_mad_agent_private
*priv
);
71 static struct ib_mad_agent_private
*find_mad_agent(
72 struct ib_mad_port_private
*port_priv
,
74 static int ib_mad_post_receive_mads(struct ib_mad_qp_info
*qp_info
,
75 struct ib_mad_private
*mad
);
76 static void cancel_mads(struct ib_mad_agent_private
*mad_agent_priv
);
77 static void timeout_sends(struct work_struct
*work
);
78 static void local_completions(struct work_struct
*work
);
79 static int add_nonoui_reg_req(struct ib_mad_reg_req
*mad_reg_req
,
80 struct ib_mad_agent_private
*agent_priv
,
82 static int add_oui_reg_req(struct ib_mad_reg_req
*mad_reg_req
,
83 struct ib_mad_agent_private
*agent_priv
);
86 * Returns a ib_mad_port_private structure or NULL for a device/port
87 * Assumes ib_mad_port_list_lock is being held
89 static inline struct ib_mad_port_private
*
90 __ib_get_mad_port(struct ib_device
*device
, int port_num
)
92 struct ib_mad_port_private
*entry
;
94 list_for_each_entry(entry
, &ib_mad_port_list
, port_list
) {
95 if (entry
->device
== device
&& entry
->port_num
== port_num
)
102 * Wrapper function to return a ib_mad_port_private structure or NULL
105 static inline struct ib_mad_port_private
*
106 ib_get_mad_port(struct ib_device
*device
, int port_num
)
108 struct ib_mad_port_private
*entry
;
111 spin_lock_irqsave(&ib_mad_port_list_lock
, flags
);
112 entry
= __ib_get_mad_port(device
, port_num
);
113 spin_unlock_irqrestore(&ib_mad_port_list_lock
, flags
);
118 static inline u8
convert_mgmt_class(u8 mgmt_class
)
120 /* Alias IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE to 0 */
121 return mgmt_class
== IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
?
125 static int get_spl_qp_index(enum ib_qp_type qp_type
)
138 static int vendor_class_index(u8 mgmt_class
)
140 return mgmt_class
- IB_MGMT_CLASS_VENDOR_RANGE2_START
;
143 static int is_vendor_class(u8 mgmt_class
)
145 if ((mgmt_class
< IB_MGMT_CLASS_VENDOR_RANGE2_START
) ||
146 (mgmt_class
> IB_MGMT_CLASS_VENDOR_RANGE2_END
))
151 static int is_vendor_oui(char *oui
)
153 if (oui
[0] || oui
[1] || oui
[2])
158 static int is_vendor_method_in_use(
159 struct ib_mad_mgmt_vendor_class
*vendor_class
,
160 struct ib_mad_reg_req
*mad_reg_req
)
162 struct ib_mad_mgmt_method_table
*method
;
165 for (i
= 0; i
< MAX_MGMT_OUI
; i
++) {
166 if (!memcmp(vendor_class
->oui
[i
], mad_reg_req
->oui
, 3)) {
167 method
= vendor_class
->method_table
[i
];
169 if (method_in_use(&method
, mad_reg_req
))
179 int ib_response_mad(struct ib_mad
*mad
)
181 return ((mad
->mad_hdr
.method
& IB_MGMT_METHOD_RESP
) ||
182 (mad
->mad_hdr
.method
== IB_MGMT_METHOD_TRAP_REPRESS
) ||
183 ((mad
->mad_hdr
.mgmt_class
== IB_MGMT_CLASS_BM
) &&
184 (mad
->mad_hdr
.attr_mod
& IB_BM_ATTR_MOD_RESP
)));
186 EXPORT_SYMBOL(ib_response_mad
);
189 * ib_register_mad_agent - Register to send/receive MADs
191 struct ib_mad_agent
*ib_register_mad_agent(struct ib_device
*device
,
193 enum ib_qp_type qp_type
,
194 struct ib_mad_reg_req
*mad_reg_req
,
196 ib_mad_send_handler send_handler
,
197 ib_mad_recv_handler recv_handler
,
200 struct ib_mad_port_private
*port_priv
;
201 struct ib_mad_agent
*ret
= ERR_PTR(-EINVAL
);
202 struct ib_mad_agent_private
*mad_agent_priv
;
203 struct ib_mad_reg_req
*reg_req
= NULL
;
204 struct ib_mad_mgmt_class_table
*class;
205 struct ib_mad_mgmt_vendor_class_table
*vendor
;
206 struct ib_mad_mgmt_vendor_class
*vendor_class
;
207 struct ib_mad_mgmt_method_table
*method
;
210 u8 mgmt_class
, vclass
;
212 /* Validate parameters */
213 qpn
= get_spl_qp_index(qp_type
);
217 if (rmpp_version
&& rmpp_version
!= IB_MGMT_RMPP_VERSION
)
220 /* Validate MAD registration request if supplied */
222 if (mad_reg_req
->mgmt_class_version
>= MAX_MGMT_VERSION
)
226 if (mad_reg_req
->mgmt_class
>= MAX_MGMT_CLASS
) {
228 * IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE is the only
229 * one in this range currently allowed
231 if (mad_reg_req
->mgmt_class
!=
232 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
)
234 } else if (mad_reg_req
->mgmt_class
== 0) {
236 * Class 0 is reserved in IBA and is used for
237 * aliasing of IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
240 } else if (is_vendor_class(mad_reg_req
->mgmt_class
)) {
242 * If class is in "new" vendor range,
243 * ensure supplied OUI is not zero
245 if (!is_vendor_oui(mad_reg_req
->oui
))
248 /* Make sure class supplied is consistent with RMPP */
249 if (!ib_is_mad_class_rmpp(mad_reg_req
->mgmt_class
)) {
253 /* Make sure class supplied is consistent with QP type */
254 if (qp_type
== IB_QPT_SMI
) {
255 if ((mad_reg_req
->mgmt_class
!=
256 IB_MGMT_CLASS_SUBN_LID_ROUTED
) &&
257 (mad_reg_req
->mgmt_class
!=
258 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
))
261 if ((mad_reg_req
->mgmt_class
==
262 IB_MGMT_CLASS_SUBN_LID_ROUTED
) ||
263 (mad_reg_req
->mgmt_class
==
264 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
))
268 /* No registration request supplied */
273 /* Validate device and port */
274 port_priv
= ib_get_mad_port(device
, port_num
);
276 ret
= ERR_PTR(-ENODEV
);
280 /* Verify the QP requested is supported. For example, Ethernet devices
281 * will not have QP0 */
282 if (!port_priv
->qp_info
[qpn
].qp
) {
283 ret
= ERR_PTR(-EPROTONOSUPPORT
);
287 /* Allocate structures */
288 mad_agent_priv
= kzalloc(sizeof *mad_agent_priv
, GFP_KERNEL
);
289 if (!mad_agent_priv
) {
290 ret
= ERR_PTR(-ENOMEM
);
294 mad_agent_priv
->agent
.mr
= ib_get_dma_mr(port_priv
->qp_info
[qpn
].qp
->pd
,
295 IB_ACCESS_LOCAL_WRITE
);
296 if (IS_ERR(mad_agent_priv
->agent
.mr
)) {
297 ret
= ERR_PTR(-ENOMEM
);
302 reg_req
= kmemdup(mad_reg_req
, sizeof *reg_req
, GFP_KERNEL
);
304 ret
= ERR_PTR(-ENOMEM
);
309 /* Now, fill in the various structures */
310 mad_agent_priv
->qp_info
= &port_priv
->qp_info
[qpn
];
311 mad_agent_priv
->reg_req
= reg_req
;
312 mad_agent_priv
->agent
.rmpp_version
= rmpp_version
;
313 mad_agent_priv
->agent
.device
= device
;
314 mad_agent_priv
->agent
.recv_handler
= recv_handler
;
315 mad_agent_priv
->agent
.send_handler
= send_handler
;
316 mad_agent_priv
->agent
.context
= context
;
317 mad_agent_priv
->agent
.qp
= port_priv
->qp_info
[qpn
].qp
;
318 mad_agent_priv
->agent
.port_num
= port_num
;
319 spin_lock_init(&mad_agent_priv
->lock
);
320 INIT_LIST_HEAD(&mad_agent_priv
->send_list
);
321 INIT_LIST_HEAD(&mad_agent_priv
->wait_list
);
322 INIT_LIST_HEAD(&mad_agent_priv
->done_list
);
323 INIT_LIST_HEAD(&mad_agent_priv
->rmpp_list
);
324 INIT_DELAYED_WORK(&mad_agent_priv
->timed_work
, timeout_sends
);
325 INIT_LIST_HEAD(&mad_agent_priv
->local_list
);
326 INIT_WORK(&mad_agent_priv
->local_work
, local_completions
);
327 atomic_set(&mad_agent_priv
->refcount
, 1);
328 init_completion(&mad_agent_priv
->comp
);
330 spin_lock_irqsave(&port_priv
->reg_lock
, flags
);
331 mad_agent_priv
->agent
.hi_tid
= ++ib_mad_client_id
;
334 * Make sure MAD registration (if supplied)
335 * is non overlapping with any existing ones
338 mgmt_class
= convert_mgmt_class(mad_reg_req
->mgmt_class
);
339 if (!is_vendor_class(mgmt_class
)) {
340 class = port_priv
->version
[mad_reg_req
->
341 mgmt_class_version
].class;
343 method
= class->method_table
[mgmt_class
];
345 if (method_in_use(&method
,
350 ret2
= add_nonoui_reg_req(mad_reg_req
, mad_agent_priv
,
353 /* "New" vendor class range */
354 vendor
= port_priv
->version
[mad_reg_req
->
355 mgmt_class_version
].vendor
;
357 vclass
= vendor_class_index(mgmt_class
);
358 vendor_class
= vendor
->vendor_class
[vclass
];
360 if (is_vendor_method_in_use(
366 ret2
= add_oui_reg_req(mad_reg_req
, mad_agent_priv
);
374 /* Add mad agent into port's agent list */
375 list_add_tail(&mad_agent_priv
->agent_list
, &port_priv
->agent_list
);
376 spin_unlock_irqrestore(&port_priv
->reg_lock
, flags
);
378 return &mad_agent_priv
->agent
;
381 spin_unlock_irqrestore(&port_priv
->reg_lock
, flags
);
384 ib_dereg_mr(mad_agent_priv
->agent
.mr
);
386 kfree(mad_agent_priv
);
390 EXPORT_SYMBOL(ib_register_mad_agent
);
392 static inline int is_snooping_sends(int mad_snoop_flags
)
394 return (mad_snoop_flags
&
395 (/*IB_MAD_SNOOP_POSTED_SENDS |
396 IB_MAD_SNOOP_RMPP_SENDS |*/
397 IB_MAD_SNOOP_SEND_COMPLETIONS
/*|
398 IB_MAD_SNOOP_RMPP_SEND_COMPLETIONS*/));
401 static inline int is_snooping_recvs(int mad_snoop_flags
)
403 return (mad_snoop_flags
&
404 (IB_MAD_SNOOP_RECVS
/*|
405 IB_MAD_SNOOP_RMPP_RECVS*/));
408 static int register_snoop_agent(struct ib_mad_qp_info
*qp_info
,
409 struct ib_mad_snoop_private
*mad_snoop_priv
)
411 struct ib_mad_snoop_private
**new_snoop_table
;
415 spin_lock_irqsave(&qp_info
->snoop_lock
, flags
);
416 /* Check for empty slot in array. */
417 for (i
= 0; i
< qp_info
->snoop_table_size
; i
++)
418 if (!qp_info
->snoop_table
[i
])
421 if (i
== qp_info
->snoop_table_size
) {
423 new_snoop_table
= krealloc(qp_info
->snoop_table
,
424 sizeof mad_snoop_priv
*
425 (qp_info
->snoop_table_size
+ 1),
427 if (!new_snoop_table
) {
432 qp_info
->snoop_table
= new_snoop_table
;
433 qp_info
->snoop_table_size
++;
435 qp_info
->snoop_table
[i
] = mad_snoop_priv
;
436 atomic_inc(&qp_info
->snoop_count
);
438 spin_unlock_irqrestore(&qp_info
->snoop_lock
, flags
);
442 struct ib_mad_agent
*ib_register_mad_snoop(struct ib_device
*device
,
444 enum ib_qp_type qp_type
,
446 ib_mad_snoop_handler snoop_handler
,
447 ib_mad_recv_handler recv_handler
,
450 struct ib_mad_port_private
*port_priv
;
451 struct ib_mad_agent
*ret
;
452 struct ib_mad_snoop_private
*mad_snoop_priv
;
455 /* Validate parameters */
456 if ((is_snooping_sends(mad_snoop_flags
) && !snoop_handler
) ||
457 (is_snooping_recvs(mad_snoop_flags
) && !recv_handler
)) {
458 ret
= ERR_PTR(-EINVAL
);
461 qpn
= get_spl_qp_index(qp_type
);
463 ret
= ERR_PTR(-EINVAL
);
466 port_priv
= ib_get_mad_port(device
, port_num
);
468 ret
= ERR_PTR(-ENODEV
);
471 /* Allocate structures */
472 mad_snoop_priv
= kzalloc(sizeof *mad_snoop_priv
, GFP_KERNEL
);
473 if (!mad_snoop_priv
) {
474 ret
= ERR_PTR(-ENOMEM
);
478 /* Now, fill in the various structures */
479 mad_snoop_priv
->qp_info
= &port_priv
->qp_info
[qpn
];
480 mad_snoop_priv
->agent
.device
= device
;
481 mad_snoop_priv
->agent
.recv_handler
= recv_handler
;
482 mad_snoop_priv
->agent
.snoop_handler
= snoop_handler
;
483 mad_snoop_priv
->agent
.context
= context
;
484 mad_snoop_priv
->agent
.qp
= port_priv
->qp_info
[qpn
].qp
;
485 mad_snoop_priv
->agent
.port_num
= port_num
;
486 mad_snoop_priv
->mad_snoop_flags
= mad_snoop_flags
;
487 init_completion(&mad_snoop_priv
->comp
);
488 mad_snoop_priv
->snoop_index
= register_snoop_agent(
489 &port_priv
->qp_info
[qpn
],
491 if (mad_snoop_priv
->snoop_index
< 0) {
492 ret
= ERR_PTR(mad_snoop_priv
->snoop_index
);
496 atomic_set(&mad_snoop_priv
->refcount
, 1);
497 return &mad_snoop_priv
->agent
;
500 kfree(mad_snoop_priv
);
504 EXPORT_SYMBOL(ib_register_mad_snoop
);
506 static inline void deref_mad_agent(struct ib_mad_agent_private
*mad_agent_priv
)
508 if (atomic_dec_and_test(&mad_agent_priv
->refcount
))
509 complete(&mad_agent_priv
->comp
);
512 static inline void deref_snoop_agent(struct ib_mad_snoop_private
*mad_snoop_priv
)
514 if (atomic_dec_and_test(&mad_snoop_priv
->refcount
))
515 complete(&mad_snoop_priv
->comp
);
518 static void unregister_mad_agent(struct ib_mad_agent_private
*mad_agent_priv
)
520 struct ib_mad_port_private
*port_priv
;
523 /* Note that we could still be handling received MADs */
526 * Canceling all sends results in dropping received response
527 * MADs, preventing us from queuing additional work
529 cancel_mads(mad_agent_priv
);
530 port_priv
= mad_agent_priv
->qp_info
->port_priv
;
531 cancel_delayed_work(&mad_agent_priv
->timed_work
);
533 spin_lock_irqsave(&port_priv
->reg_lock
, flags
);
534 remove_mad_reg_req(mad_agent_priv
);
535 list_del(&mad_agent_priv
->agent_list
);
536 spin_unlock_irqrestore(&port_priv
->reg_lock
, flags
);
538 flush_workqueue(port_priv
->wq
);
539 ib_cancel_rmpp_recvs(mad_agent_priv
);
541 deref_mad_agent(mad_agent_priv
);
542 wait_for_completion(&mad_agent_priv
->comp
);
544 kfree(mad_agent_priv
->reg_req
);
545 ib_dereg_mr(mad_agent_priv
->agent
.mr
);
546 kfree(mad_agent_priv
);
549 static void unregister_mad_snoop(struct ib_mad_snoop_private
*mad_snoop_priv
)
551 struct ib_mad_qp_info
*qp_info
;
554 qp_info
= mad_snoop_priv
->qp_info
;
555 spin_lock_irqsave(&qp_info
->snoop_lock
, flags
);
556 qp_info
->snoop_table
[mad_snoop_priv
->snoop_index
] = NULL
;
557 atomic_dec(&qp_info
->snoop_count
);
558 spin_unlock_irqrestore(&qp_info
->snoop_lock
, flags
);
560 deref_snoop_agent(mad_snoop_priv
);
561 wait_for_completion(&mad_snoop_priv
->comp
);
563 kfree(mad_snoop_priv
);
567 * ib_unregister_mad_agent - Unregisters a client from using MAD services
569 int ib_unregister_mad_agent(struct ib_mad_agent
*mad_agent
)
571 struct ib_mad_agent_private
*mad_agent_priv
;
572 struct ib_mad_snoop_private
*mad_snoop_priv
;
574 /* If the TID is zero, the agent can only snoop. */
575 if (mad_agent
->hi_tid
) {
576 mad_agent_priv
= container_of(mad_agent
,
577 struct ib_mad_agent_private
,
579 unregister_mad_agent(mad_agent_priv
);
581 mad_snoop_priv
= container_of(mad_agent
,
582 struct ib_mad_snoop_private
,
584 unregister_mad_snoop(mad_snoop_priv
);
588 EXPORT_SYMBOL(ib_unregister_mad_agent
);
590 static void dequeue_mad(struct ib_mad_list_head
*mad_list
)
592 struct ib_mad_queue
*mad_queue
;
595 BUG_ON(!mad_list
->mad_queue
);
596 mad_queue
= mad_list
->mad_queue
;
597 spin_lock_irqsave(&mad_queue
->lock
, flags
);
598 list_del(&mad_list
->list
);
600 spin_unlock_irqrestore(&mad_queue
->lock
, flags
);
603 static void snoop_send(struct ib_mad_qp_info
*qp_info
,
604 struct ib_mad_send_buf
*send_buf
,
605 struct ib_mad_send_wc
*mad_send_wc
,
608 struct ib_mad_snoop_private
*mad_snoop_priv
;
612 spin_lock_irqsave(&qp_info
->snoop_lock
, flags
);
613 for (i
= 0; i
< qp_info
->snoop_table_size
; i
++) {
614 mad_snoop_priv
= qp_info
->snoop_table
[i
];
615 if (!mad_snoop_priv
||
616 !(mad_snoop_priv
->mad_snoop_flags
& mad_snoop_flags
))
619 atomic_inc(&mad_snoop_priv
->refcount
);
620 spin_unlock_irqrestore(&qp_info
->snoop_lock
, flags
);
621 mad_snoop_priv
->agent
.snoop_handler(&mad_snoop_priv
->agent
,
622 send_buf
, mad_send_wc
);
623 deref_snoop_agent(mad_snoop_priv
);
624 spin_lock_irqsave(&qp_info
->snoop_lock
, flags
);
626 spin_unlock_irqrestore(&qp_info
->snoop_lock
, flags
);
629 static void snoop_recv(struct ib_mad_qp_info
*qp_info
,
630 struct ib_mad_recv_wc
*mad_recv_wc
,
633 struct ib_mad_snoop_private
*mad_snoop_priv
;
637 spin_lock_irqsave(&qp_info
->snoop_lock
, flags
);
638 for (i
= 0; i
< qp_info
->snoop_table_size
; i
++) {
639 mad_snoop_priv
= qp_info
->snoop_table
[i
];
640 if (!mad_snoop_priv
||
641 !(mad_snoop_priv
->mad_snoop_flags
& mad_snoop_flags
))
644 atomic_inc(&mad_snoop_priv
->refcount
);
645 spin_unlock_irqrestore(&qp_info
->snoop_lock
, flags
);
646 mad_snoop_priv
->agent
.recv_handler(&mad_snoop_priv
->agent
,
648 deref_snoop_agent(mad_snoop_priv
);
649 spin_lock_irqsave(&qp_info
->snoop_lock
, flags
);
651 spin_unlock_irqrestore(&qp_info
->snoop_lock
, flags
);
654 static void build_smp_wc(struct ib_qp
*qp
,
655 u64 wr_id
, u16 slid
, u16 pkey_index
, u8 port_num
,
658 memset(wc
, 0, sizeof *wc
);
660 wc
->status
= IB_WC_SUCCESS
;
661 wc
->opcode
= IB_WC_RECV
;
662 wc
->pkey_index
= pkey_index
;
663 wc
->byte_len
= sizeof(struct ib_mad
) + sizeof(struct ib_grh
);
668 wc
->dlid_path_bits
= 0;
669 wc
->port_num
= port_num
;
673 * Return 0 if SMP is to be sent
674 * Return 1 if SMP was consumed locally (whether or not solicited)
675 * Return < 0 if error
677 static int handle_outgoing_dr_smp(struct ib_mad_agent_private
*mad_agent_priv
,
678 struct ib_mad_send_wr_private
*mad_send_wr
)
681 struct ib_smp
*smp
= mad_send_wr
->send_buf
.mad
;
683 struct ib_mad_local_private
*local
;
684 struct ib_mad_private
*mad_priv
;
685 struct ib_mad_port_private
*port_priv
;
686 struct ib_mad_agent_private
*recv_mad_agent
= NULL
;
687 struct ib_device
*device
= mad_agent_priv
->agent
.device
;
690 struct ib_send_wr
*send_wr
= &mad_send_wr
->send_wr
;
692 if (device
->node_type
== RDMA_NODE_IB_SWITCH
&&
693 smp
->mgmt_class
== IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
)
694 port_num
= send_wr
->wr
.ud
.port_num
;
696 port_num
= mad_agent_priv
->agent
.port_num
;
699 * Directed route handling starts if the initial LID routed part of
700 * a request or the ending LID routed part of a response is empty.
701 * If we are at the start of the LID routed part, don't update the
702 * hop_ptr or hop_cnt. See section 14.2.2, Vol 1 IB spec.
704 if ((ib_get_smp_direction(smp
) ? smp
->dr_dlid
: smp
->dr_slid
) ==
706 smi_handle_dr_smp_send(smp
, device
->node_type
, port_num
) ==
709 printk(KERN_ERR PFX
"Invalid directed route\n");
713 /* Check to post send on QP or process locally */
714 if (smi_check_local_smp(smp
, device
) == IB_SMI_DISCARD
&&
715 smi_check_local_returning_smp(smp
, device
) == IB_SMI_DISCARD
)
718 local
= kmalloc(sizeof *local
, GFP_ATOMIC
);
721 printk(KERN_ERR PFX
"No memory for ib_mad_local_private\n");
724 local
->mad_priv
= NULL
;
725 local
->recv_mad_agent
= NULL
;
726 mad_priv
= kmem_cache_alloc(ib_mad_cache
, GFP_ATOMIC
);
729 printk(KERN_ERR PFX
"No memory for local response MAD\n");
734 build_smp_wc(mad_agent_priv
->agent
.qp
,
735 send_wr
->wr_id
, be16_to_cpu(smp
->dr_slid
),
736 send_wr
->wr
.ud
.pkey_index
,
737 send_wr
->wr
.ud
.port_num
, &mad_wc
);
739 /* No GRH for DR SMP */
740 ret
= device
->process_mad(device
, 0, port_num
, &mad_wc
, NULL
,
741 (struct ib_mad
*)smp
,
742 (struct ib_mad
*)&mad_priv
->mad
);
745 case IB_MAD_RESULT_SUCCESS
| IB_MAD_RESULT_REPLY
:
746 if (ib_response_mad(&mad_priv
->mad
.mad
) &&
747 mad_agent_priv
->agent
.recv_handler
) {
748 local
->mad_priv
= mad_priv
;
749 local
->recv_mad_agent
= mad_agent_priv
;
751 * Reference MAD agent until receive
752 * side of local completion handled
754 atomic_inc(&mad_agent_priv
->refcount
);
756 kmem_cache_free(ib_mad_cache
, mad_priv
);
758 case IB_MAD_RESULT_SUCCESS
| IB_MAD_RESULT_CONSUMED
:
759 kmem_cache_free(ib_mad_cache
, mad_priv
);
761 case IB_MAD_RESULT_SUCCESS
:
762 /* Treat like an incoming receive MAD */
763 port_priv
= ib_get_mad_port(mad_agent_priv
->agent
.device
,
764 mad_agent_priv
->agent
.port_num
);
766 memcpy(&mad_priv
->mad
.mad
, smp
, sizeof(struct ib_mad
));
767 recv_mad_agent
= find_mad_agent(port_priv
,
770 if (!port_priv
|| !recv_mad_agent
) {
772 * No receiving agent so drop packet and
773 * generate send completion.
775 kmem_cache_free(ib_mad_cache
, mad_priv
);
778 local
->mad_priv
= mad_priv
;
779 local
->recv_mad_agent
= recv_mad_agent
;
782 kmem_cache_free(ib_mad_cache
, mad_priv
);
788 local
->mad_send_wr
= mad_send_wr
;
789 /* Reference MAD agent until send side of local completion handled */
790 atomic_inc(&mad_agent_priv
->refcount
);
791 /* Queue local completion to local list */
792 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
793 list_add_tail(&local
->completion_list
, &mad_agent_priv
->local_list
);
794 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
795 queue_work(mad_agent_priv
->qp_info
->port_priv
->wq
,
796 &mad_agent_priv
->local_work
);
802 static int get_pad_size(int hdr_len
, int data_len
)
806 seg_size
= sizeof(struct ib_mad
) - hdr_len
;
807 if (data_len
&& seg_size
) {
808 pad
= seg_size
- data_len
% seg_size
;
809 return pad
== seg_size
? 0 : pad
;
814 static void free_send_rmpp_list(struct ib_mad_send_wr_private
*mad_send_wr
)
816 struct ib_rmpp_segment
*s
, *t
;
818 list_for_each_entry_safe(s
, t
, &mad_send_wr
->rmpp_list
, list
) {
824 static int alloc_send_rmpp_list(struct ib_mad_send_wr_private
*send_wr
,
827 struct ib_mad_send_buf
*send_buf
= &send_wr
->send_buf
;
828 struct ib_rmpp_mad
*rmpp_mad
= send_buf
->mad
;
829 struct ib_rmpp_segment
*seg
= NULL
;
830 int left
, seg_size
, pad
;
832 send_buf
->seg_size
= sizeof (struct ib_mad
) - send_buf
->hdr_len
;
833 seg_size
= send_buf
->seg_size
;
836 /* Allocate data segments. */
837 for (left
= send_buf
->data_len
+ pad
; left
> 0; left
-= seg_size
) {
838 seg
= kmalloc(sizeof (*seg
) + seg_size
, gfp_mask
);
840 printk(KERN_ERR
"alloc_send_rmpp_segs: RMPP mem "
841 "alloc failed for len %zd, gfp %#x\n",
842 sizeof (*seg
) + seg_size
, gfp_mask
);
843 free_send_rmpp_list(send_wr
);
846 seg
->num
= ++send_buf
->seg_count
;
847 list_add_tail(&seg
->list
, &send_wr
->rmpp_list
);
850 /* Zero any padding */
852 memset(seg
->data
+ seg_size
- pad
, 0, pad
);
854 rmpp_mad
->rmpp_hdr
.rmpp_version
= send_wr
->mad_agent_priv
->
856 rmpp_mad
->rmpp_hdr
.rmpp_type
= IB_MGMT_RMPP_TYPE_DATA
;
857 ib_set_rmpp_flags(&rmpp_mad
->rmpp_hdr
, IB_MGMT_RMPP_FLAG_ACTIVE
);
859 send_wr
->cur_seg
= container_of(send_wr
->rmpp_list
.next
,
860 struct ib_rmpp_segment
, list
);
861 send_wr
->last_ack_seg
= send_wr
->cur_seg
;
865 struct ib_mad_send_buf
* ib_create_send_mad(struct ib_mad_agent
*mad_agent
,
866 u32 remote_qpn
, u16 pkey_index
,
868 int hdr_len
, int data_len
,
871 struct ib_mad_agent_private
*mad_agent_priv
;
872 struct ib_mad_send_wr_private
*mad_send_wr
;
873 int pad
, message_size
, ret
, size
;
876 mad_agent_priv
= container_of(mad_agent
, struct ib_mad_agent_private
,
878 pad
= get_pad_size(hdr_len
, data_len
);
879 message_size
= hdr_len
+ data_len
+ pad
;
881 if ((!mad_agent
->rmpp_version
&&
882 (rmpp_active
|| message_size
> sizeof(struct ib_mad
))) ||
883 (!rmpp_active
&& message_size
> sizeof(struct ib_mad
)))
884 return ERR_PTR(-EINVAL
);
886 size
= rmpp_active
? hdr_len
: sizeof(struct ib_mad
);
887 buf
= kzalloc(sizeof *mad_send_wr
+ size
, gfp_mask
);
889 return ERR_PTR(-ENOMEM
);
891 mad_send_wr
= buf
+ size
;
892 INIT_LIST_HEAD(&mad_send_wr
->rmpp_list
);
893 mad_send_wr
->send_buf
.mad
= buf
;
894 mad_send_wr
->send_buf
.hdr_len
= hdr_len
;
895 mad_send_wr
->send_buf
.data_len
= data_len
;
896 mad_send_wr
->pad
= pad
;
898 mad_send_wr
->mad_agent_priv
= mad_agent_priv
;
899 mad_send_wr
->sg_list
[0].length
= hdr_len
;
900 mad_send_wr
->sg_list
[0].lkey
= mad_agent
->mr
->lkey
;
901 mad_send_wr
->sg_list
[1].length
= sizeof(struct ib_mad
) - hdr_len
;
902 mad_send_wr
->sg_list
[1].lkey
= mad_agent
->mr
->lkey
;
904 mad_send_wr
->send_wr
.wr_id
= (unsigned long) mad_send_wr
;
905 mad_send_wr
->send_wr
.sg_list
= mad_send_wr
->sg_list
;
906 mad_send_wr
->send_wr
.num_sge
= 2;
907 mad_send_wr
->send_wr
.opcode
= IB_WR_SEND
;
908 mad_send_wr
->send_wr
.send_flags
= IB_SEND_SIGNALED
;
909 mad_send_wr
->send_wr
.wr
.ud
.remote_qpn
= remote_qpn
;
910 mad_send_wr
->send_wr
.wr
.ud
.remote_qkey
= IB_QP_SET_QKEY
;
911 mad_send_wr
->send_wr
.wr
.ud
.pkey_index
= pkey_index
;
914 ret
= alloc_send_rmpp_list(mad_send_wr
, gfp_mask
);
921 mad_send_wr
->send_buf
.mad_agent
= mad_agent
;
922 atomic_inc(&mad_agent_priv
->refcount
);
923 return &mad_send_wr
->send_buf
;
925 EXPORT_SYMBOL(ib_create_send_mad
);
927 int ib_get_mad_data_offset(u8 mgmt_class
)
929 if (mgmt_class
== IB_MGMT_CLASS_SUBN_ADM
)
930 return IB_MGMT_SA_HDR
;
931 else if ((mgmt_class
== IB_MGMT_CLASS_DEVICE_MGMT
) ||
932 (mgmt_class
== IB_MGMT_CLASS_DEVICE_ADM
) ||
933 (mgmt_class
== IB_MGMT_CLASS_BIS
))
934 return IB_MGMT_DEVICE_HDR
;
935 else if ((mgmt_class
>= IB_MGMT_CLASS_VENDOR_RANGE2_START
) &&
936 (mgmt_class
<= IB_MGMT_CLASS_VENDOR_RANGE2_END
))
937 return IB_MGMT_VENDOR_HDR
;
939 return IB_MGMT_MAD_HDR
;
941 EXPORT_SYMBOL(ib_get_mad_data_offset
);
943 int ib_is_mad_class_rmpp(u8 mgmt_class
)
945 if ((mgmt_class
== IB_MGMT_CLASS_SUBN_ADM
) ||
946 (mgmt_class
== IB_MGMT_CLASS_DEVICE_MGMT
) ||
947 (mgmt_class
== IB_MGMT_CLASS_DEVICE_ADM
) ||
948 (mgmt_class
== IB_MGMT_CLASS_BIS
) ||
949 ((mgmt_class
>= IB_MGMT_CLASS_VENDOR_RANGE2_START
) &&
950 (mgmt_class
<= IB_MGMT_CLASS_VENDOR_RANGE2_END
)))
954 EXPORT_SYMBOL(ib_is_mad_class_rmpp
);
956 void *ib_get_rmpp_segment(struct ib_mad_send_buf
*send_buf
, int seg_num
)
958 struct ib_mad_send_wr_private
*mad_send_wr
;
959 struct list_head
*list
;
961 mad_send_wr
= container_of(send_buf
, struct ib_mad_send_wr_private
,
963 list
= &mad_send_wr
->cur_seg
->list
;
965 if (mad_send_wr
->cur_seg
->num
< seg_num
) {
966 list_for_each_entry(mad_send_wr
->cur_seg
, list
, list
)
967 if (mad_send_wr
->cur_seg
->num
== seg_num
)
969 } else if (mad_send_wr
->cur_seg
->num
> seg_num
) {
970 list_for_each_entry_reverse(mad_send_wr
->cur_seg
, list
, list
)
971 if (mad_send_wr
->cur_seg
->num
== seg_num
)
974 return mad_send_wr
->cur_seg
->data
;
976 EXPORT_SYMBOL(ib_get_rmpp_segment
);
978 static inline void *ib_get_payload(struct ib_mad_send_wr_private
*mad_send_wr
)
980 if (mad_send_wr
->send_buf
.seg_count
)
981 return ib_get_rmpp_segment(&mad_send_wr
->send_buf
,
982 mad_send_wr
->seg_num
);
984 return mad_send_wr
->send_buf
.mad
+
985 mad_send_wr
->send_buf
.hdr_len
;
988 void ib_free_send_mad(struct ib_mad_send_buf
*send_buf
)
990 struct ib_mad_agent_private
*mad_agent_priv
;
991 struct ib_mad_send_wr_private
*mad_send_wr
;
993 mad_agent_priv
= container_of(send_buf
->mad_agent
,
994 struct ib_mad_agent_private
, agent
);
995 mad_send_wr
= container_of(send_buf
, struct ib_mad_send_wr_private
,
998 free_send_rmpp_list(mad_send_wr
);
999 kfree(send_buf
->mad
);
1000 deref_mad_agent(mad_agent_priv
);
1002 EXPORT_SYMBOL(ib_free_send_mad
);
1004 int ib_send_mad(struct ib_mad_send_wr_private
*mad_send_wr
)
1006 struct ib_mad_qp_info
*qp_info
;
1007 struct list_head
*list
;
1008 struct ib_send_wr
*bad_send_wr
;
1009 struct ib_mad_agent
*mad_agent
;
1011 unsigned long flags
;
1014 /* Set WR ID to find mad_send_wr upon completion */
1015 qp_info
= mad_send_wr
->mad_agent_priv
->qp_info
;
1016 mad_send_wr
->send_wr
.wr_id
= (unsigned long)&mad_send_wr
->mad_list
;
1017 mad_send_wr
->mad_list
.mad_queue
= &qp_info
->send_queue
;
1019 mad_agent
= mad_send_wr
->send_buf
.mad_agent
;
1020 sge
= mad_send_wr
->sg_list
;
1021 sge
[0].addr
= ib_dma_map_single(mad_agent
->device
,
1022 mad_send_wr
->send_buf
.mad
,
1025 mad_send_wr
->header_mapping
= sge
[0].addr
;
1027 sge
[1].addr
= ib_dma_map_single(mad_agent
->device
,
1028 ib_get_payload(mad_send_wr
),
1031 mad_send_wr
->payload_mapping
= sge
[1].addr
;
1033 spin_lock_irqsave(&qp_info
->send_queue
.lock
, flags
);
1034 if (qp_info
->send_queue
.count
< qp_info
->send_queue
.max_active
) {
1035 ret
= ib_post_send(mad_agent
->qp
, &mad_send_wr
->send_wr
,
1037 list
= &qp_info
->send_queue
.list
;
1040 list
= &qp_info
->overflow_list
;
1044 qp_info
->send_queue
.count
++;
1045 list_add_tail(&mad_send_wr
->mad_list
.list
, list
);
1047 spin_unlock_irqrestore(&qp_info
->send_queue
.lock
, flags
);
1049 ib_dma_unmap_single(mad_agent
->device
,
1050 mad_send_wr
->header_mapping
,
1051 sge
[0].length
, DMA_TO_DEVICE
);
1052 ib_dma_unmap_single(mad_agent
->device
,
1053 mad_send_wr
->payload_mapping
,
1054 sge
[1].length
, DMA_TO_DEVICE
);
1060 * ib_post_send_mad - Posts MAD(s) to the send queue of the QP associated
1061 * with the registered client
1063 int ib_post_send_mad(struct ib_mad_send_buf
*send_buf
,
1064 struct ib_mad_send_buf
**bad_send_buf
)
1066 struct ib_mad_agent_private
*mad_agent_priv
;
1067 struct ib_mad_send_buf
*next_send_buf
;
1068 struct ib_mad_send_wr_private
*mad_send_wr
;
1069 unsigned long flags
;
1072 /* Walk list of send WRs and post each on send list */
1073 for (; send_buf
; send_buf
= next_send_buf
) {
1075 mad_send_wr
= container_of(send_buf
,
1076 struct ib_mad_send_wr_private
,
1078 mad_agent_priv
= mad_send_wr
->mad_agent_priv
;
1080 if (!send_buf
->mad_agent
->send_handler
||
1081 (send_buf
->timeout_ms
&&
1082 !send_buf
->mad_agent
->recv_handler
)) {
1087 if (!ib_is_mad_class_rmpp(((struct ib_mad_hdr
*) send_buf
->mad
)->mgmt_class
)) {
1088 if (mad_agent_priv
->agent
.rmpp_version
) {
1095 * Save pointer to next work request to post in case the
1096 * current one completes, and the user modifies the work
1097 * request associated with the completion
1099 next_send_buf
= send_buf
->next
;
1100 mad_send_wr
->send_wr
.wr
.ud
.ah
= send_buf
->ah
;
1102 if (((struct ib_mad_hdr
*) send_buf
->mad
)->mgmt_class
==
1103 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
) {
1104 ret
= handle_outgoing_dr_smp(mad_agent_priv
,
1106 if (ret
< 0) /* error */
1108 else if (ret
== 1) /* locally consumed */
1112 mad_send_wr
->tid
= ((struct ib_mad_hdr
*) send_buf
->mad
)->tid
;
1113 /* Timeout will be updated after send completes */
1114 mad_send_wr
->timeout
= msecs_to_jiffies(send_buf
->timeout_ms
);
1115 mad_send_wr
->max_retries
= send_buf
->retries
;
1116 mad_send_wr
->retries_left
= send_buf
->retries
;
1117 send_buf
->retries
= 0;
1118 /* Reference for work request to QP + response */
1119 mad_send_wr
->refcount
= 1 + (mad_send_wr
->timeout
> 0);
1120 mad_send_wr
->status
= IB_WC_SUCCESS
;
1122 /* Reference MAD agent until send completes */
1123 atomic_inc(&mad_agent_priv
->refcount
);
1124 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
1125 list_add_tail(&mad_send_wr
->agent_list
,
1126 &mad_agent_priv
->send_list
);
1127 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
1129 if (mad_agent_priv
->agent
.rmpp_version
) {
1130 ret
= ib_send_rmpp_mad(mad_send_wr
);
1131 if (ret
>= 0 && ret
!= IB_RMPP_RESULT_CONSUMED
)
1132 ret
= ib_send_mad(mad_send_wr
);
1134 ret
= ib_send_mad(mad_send_wr
);
1136 /* Fail send request */
1137 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
1138 list_del(&mad_send_wr
->agent_list
);
1139 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
1140 atomic_dec(&mad_agent_priv
->refcount
);
1147 *bad_send_buf
= send_buf
;
1150 EXPORT_SYMBOL(ib_post_send_mad
);
1153 * ib_free_recv_mad - Returns data buffers used to receive
1154 * a MAD to the access layer
1156 void ib_free_recv_mad(struct ib_mad_recv_wc
*mad_recv_wc
)
1158 struct ib_mad_recv_buf
*mad_recv_buf
, *temp_recv_buf
;
1159 struct ib_mad_private_header
*mad_priv_hdr
;
1160 struct ib_mad_private
*priv
;
1161 struct list_head free_list
;
1163 INIT_LIST_HEAD(&free_list
);
1164 list_splice_init(&mad_recv_wc
->rmpp_list
, &free_list
);
1166 list_for_each_entry_safe(mad_recv_buf
, temp_recv_buf
,
1168 mad_recv_wc
= container_of(mad_recv_buf
, struct ib_mad_recv_wc
,
1170 mad_priv_hdr
= container_of(mad_recv_wc
,
1171 struct ib_mad_private_header
,
1173 priv
= container_of(mad_priv_hdr
, struct ib_mad_private
,
1175 kmem_cache_free(ib_mad_cache
, priv
);
1178 EXPORT_SYMBOL(ib_free_recv_mad
);
1180 struct ib_mad_agent
*ib_redirect_mad_qp(struct ib_qp
*qp
,
1182 ib_mad_send_handler send_handler
,
1183 ib_mad_recv_handler recv_handler
,
1186 return ERR_PTR(-EINVAL
); /* XXX: for now */
1188 EXPORT_SYMBOL(ib_redirect_mad_qp
);
1190 int ib_process_mad_wc(struct ib_mad_agent
*mad_agent
,
1193 printk(KERN_ERR PFX
"ib_process_mad_wc() not implemented yet\n");
1196 EXPORT_SYMBOL(ib_process_mad_wc
);
1198 static int method_in_use(struct ib_mad_mgmt_method_table
**method
,
1199 struct ib_mad_reg_req
*mad_reg_req
)
1203 for_each_set_bit(i
, mad_reg_req
->method_mask
, IB_MGMT_MAX_METHODS
) {
1204 if ((*method
)->agent
[i
]) {
1205 printk(KERN_ERR PFX
"Method %d already in use\n", i
);
1212 static int allocate_method_table(struct ib_mad_mgmt_method_table
**method
)
1214 /* Allocate management method table */
1215 *method
= kzalloc(sizeof **method
, GFP_ATOMIC
);
1217 printk(KERN_ERR PFX
"No memory for "
1218 "ib_mad_mgmt_method_table\n");
1226 * Check to see if there are any methods still in use
1228 static int check_method_table(struct ib_mad_mgmt_method_table
*method
)
1232 for (i
= 0; i
< IB_MGMT_MAX_METHODS
; i
++)
1233 if (method
->agent
[i
])
1239 * Check to see if there are any method tables for this class still in use
1241 static int check_class_table(struct ib_mad_mgmt_class_table
*class)
1245 for (i
= 0; i
< MAX_MGMT_CLASS
; i
++)
1246 if (class->method_table
[i
])
1251 static int check_vendor_class(struct ib_mad_mgmt_vendor_class
*vendor_class
)
1255 for (i
= 0; i
< MAX_MGMT_OUI
; i
++)
1256 if (vendor_class
->method_table
[i
])
1261 static int find_vendor_oui(struct ib_mad_mgmt_vendor_class
*vendor_class
,
1266 for (i
= 0; i
< MAX_MGMT_OUI
; i
++)
1267 /* Is there matching OUI for this vendor class ? */
1268 if (!memcmp(vendor_class
->oui
[i
], oui
, 3))
1274 static int check_vendor_table(struct ib_mad_mgmt_vendor_class_table
*vendor
)
1278 for (i
= 0; i
< MAX_MGMT_VENDOR_RANGE2
; i
++)
1279 if (vendor
->vendor_class
[i
])
1285 static void remove_methods_mad_agent(struct ib_mad_mgmt_method_table
*method
,
1286 struct ib_mad_agent_private
*agent
)
1290 /* Remove any methods for this mad agent */
1291 for (i
= 0; i
< IB_MGMT_MAX_METHODS
; i
++) {
1292 if (method
->agent
[i
] == agent
) {
1293 method
->agent
[i
] = NULL
;
1298 static int add_nonoui_reg_req(struct ib_mad_reg_req
*mad_reg_req
,
1299 struct ib_mad_agent_private
*agent_priv
,
1302 struct ib_mad_port_private
*port_priv
;
1303 struct ib_mad_mgmt_class_table
**class;
1304 struct ib_mad_mgmt_method_table
**method
;
1307 port_priv
= agent_priv
->qp_info
->port_priv
;
1308 class = &port_priv
->version
[mad_reg_req
->mgmt_class_version
].class;
1310 /* Allocate management class table for "new" class version */
1311 *class = kzalloc(sizeof **class, GFP_ATOMIC
);
1313 printk(KERN_ERR PFX
"No memory for "
1314 "ib_mad_mgmt_class_table\n");
1319 /* Allocate method table for this management class */
1320 method
= &(*class)->method_table
[mgmt_class
];
1321 if ((ret
= allocate_method_table(method
)))
1324 method
= &(*class)->method_table
[mgmt_class
];
1326 /* Allocate method table for this management class */
1327 if ((ret
= allocate_method_table(method
)))
1332 /* Now, make sure methods are not already in use */
1333 if (method_in_use(method
, mad_reg_req
))
1336 /* Finally, add in methods being registered */
1337 for_each_set_bit(i
, mad_reg_req
->method_mask
, IB_MGMT_MAX_METHODS
)
1338 (*method
)->agent
[i
] = agent_priv
;
1343 /* Remove any methods for this mad agent */
1344 remove_methods_mad_agent(*method
, agent_priv
);
1345 /* Now, check to see if there are any methods in use */
1346 if (!check_method_table(*method
)) {
1347 /* If not, release management method table */
1360 static int add_oui_reg_req(struct ib_mad_reg_req
*mad_reg_req
,
1361 struct ib_mad_agent_private
*agent_priv
)
1363 struct ib_mad_port_private
*port_priv
;
1364 struct ib_mad_mgmt_vendor_class_table
**vendor_table
;
1365 struct ib_mad_mgmt_vendor_class_table
*vendor
= NULL
;
1366 struct ib_mad_mgmt_vendor_class
*vendor_class
= NULL
;
1367 struct ib_mad_mgmt_method_table
**method
;
1368 int i
, ret
= -ENOMEM
;
1371 /* "New" vendor (with OUI) class */
1372 vclass
= vendor_class_index(mad_reg_req
->mgmt_class
);
1373 port_priv
= agent_priv
->qp_info
->port_priv
;
1374 vendor_table
= &port_priv
->version
[
1375 mad_reg_req
->mgmt_class_version
].vendor
;
1376 if (!*vendor_table
) {
1377 /* Allocate mgmt vendor class table for "new" class version */
1378 vendor
= kzalloc(sizeof *vendor
, GFP_ATOMIC
);
1380 printk(KERN_ERR PFX
"No memory for "
1381 "ib_mad_mgmt_vendor_class_table\n");
1385 *vendor_table
= vendor
;
1387 if (!(*vendor_table
)->vendor_class
[vclass
]) {
1388 /* Allocate table for this management vendor class */
1389 vendor_class
= kzalloc(sizeof *vendor_class
, GFP_ATOMIC
);
1390 if (!vendor_class
) {
1391 printk(KERN_ERR PFX
"No memory for "
1392 "ib_mad_mgmt_vendor_class\n");
1396 (*vendor_table
)->vendor_class
[vclass
] = vendor_class
;
1398 for (i
= 0; i
< MAX_MGMT_OUI
; i
++) {
1399 /* Is there matching OUI for this vendor class ? */
1400 if (!memcmp((*vendor_table
)->vendor_class
[vclass
]->oui
[i
],
1401 mad_reg_req
->oui
, 3)) {
1402 method
= &(*vendor_table
)->vendor_class
[
1403 vclass
]->method_table
[i
];
1408 for (i
= 0; i
< MAX_MGMT_OUI
; i
++) {
1409 /* OUI slot available ? */
1410 if (!is_vendor_oui((*vendor_table
)->vendor_class
[
1412 method
= &(*vendor_table
)->vendor_class
[
1413 vclass
]->method_table
[i
];
1415 /* Allocate method table for this OUI */
1416 if ((ret
= allocate_method_table(method
)))
1418 memcpy((*vendor_table
)->vendor_class
[vclass
]->oui
[i
],
1419 mad_reg_req
->oui
, 3);
1423 printk(KERN_ERR PFX
"All OUI slots in use\n");
1427 /* Now, make sure methods are not already in use */
1428 if (method_in_use(method
, mad_reg_req
))
1431 /* Finally, add in methods being registered */
1432 for_each_set_bit(i
, mad_reg_req
->method_mask
, IB_MGMT_MAX_METHODS
)
1433 (*method
)->agent
[i
] = agent_priv
;
1438 /* Remove any methods for this mad agent */
1439 remove_methods_mad_agent(*method
, agent_priv
);
1440 /* Now, check to see if there are any methods in use */
1441 if (!check_method_table(*method
)) {
1442 /* If not, release management method table */
1449 (*vendor_table
)->vendor_class
[vclass
] = NULL
;
1450 kfree(vendor_class
);
1454 *vendor_table
= NULL
;
1461 static void remove_mad_reg_req(struct ib_mad_agent_private
*agent_priv
)
1463 struct ib_mad_port_private
*port_priv
;
1464 struct ib_mad_mgmt_class_table
*class;
1465 struct ib_mad_mgmt_method_table
*method
;
1466 struct ib_mad_mgmt_vendor_class_table
*vendor
;
1467 struct ib_mad_mgmt_vendor_class
*vendor_class
;
1472 * Was MAD registration request supplied
1473 * with original registration ?
1475 if (!agent_priv
->reg_req
) {
1479 port_priv
= agent_priv
->qp_info
->port_priv
;
1480 mgmt_class
= convert_mgmt_class(agent_priv
->reg_req
->mgmt_class
);
1481 class = port_priv
->version
[
1482 agent_priv
->reg_req
->mgmt_class_version
].class;
1486 method
= class->method_table
[mgmt_class
];
1488 /* Remove any methods for this mad agent */
1489 remove_methods_mad_agent(method
, agent_priv
);
1490 /* Now, check to see if there are any methods still in use */
1491 if (!check_method_table(method
)) {
1492 /* If not, release management method table */
1494 class->method_table
[mgmt_class
] = NULL
;
1495 /* Any management classes left ? */
1496 if (!check_class_table(class)) {
1497 /* If not, release management class table */
1500 agent_priv
->reg_req
->
1501 mgmt_class_version
].class = NULL
;
1507 if (!is_vendor_class(mgmt_class
))
1510 /* normalize mgmt_class to vendor range 2 */
1511 mgmt_class
= vendor_class_index(agent_priv
->reg_req
->mgmt_class
);
1512 vendor
= port_priv
->version
[
1513 agent_priv
->reg_req
->mgmt_class_version
].vendor
;
1518 vendor_class
= vendor
->vendor_class
[mgmt_class
];
1520 index
= find_vendor_oui(vendor_class
, agent_priv
->reg_req
->oui
);
1523 method
= vendor_class
->method_table
[index
];
1525 /* Remove any methods for this mad agent */
1526 remove_methods_mad_agent(method
, agent_priv
);
1528 * Now, check to see if there are
1529 * any methods still in use
1531 if (!check_method_table(method
)) {
1532 /* If not, release management method table */
1534 vendor_class
->method_table
[index
] = NULL
;
1535 memset(vendor_class
->oui
[index
], 0, 3);
1536 /* Any OUIs left ? */
1537 if (!check_vendor_class(vendor_class
)) {
1538 /* If not, release vendor class table */
1539 kfree(vendor_class
);
1540 vendor
->vendor_class
[mgmt_class
] = NULL
;
1541 /* Any other vendor classes left ? */
1542 if (!check_vendor_table(vendor
)) {
1545 agent_priv
->reg_req
->
1546 mgmt_class_version
].
1558 static struct ib_mad_agent_private
*
1559 find_mad_agent(struct ib_mad_port_private
*port_priv
,
1562 struct ib_mad_agent_private
*mad_agent
= NULL
;
1563 unsigned long flags
;
1565 spin_lock_irqsave(&port_priv
->reg_lock
, flags
);
1566 if (ib_response_mad(mad
)) {
1568 struct ib_mad_agent_private
*entry
;
1571 * Routing is based on high 32 bits of transaction ID
1574 hi_tid
= be64_to_cpu(mad
->mad_hdr
.tid
) >> 32;
1575 list_for_each_entry(entry
, &port_priv
->agent_list
, agent_list
) {
1576 if (entry
->agent
.hi_tid
== hi_tid
) {
1582 struct ib_mad_mgmt_class_table
*class;
1583 struct ib_mad_mgmt_method_table
*method
;
1584 struct ib_mad_mgmt_vendor_class_table
*vendor
;
1585 struct ib_mad_mgmt_vendor_class
*vendor_class
;
1586 struct ib_vendor_mad
*vendor_mad
;
1590 * Routing is based on version, class, and method
1591 * For "newer" vendor MADs, also based on OUI
1593 if (mad
->mad_hdr
.class_version
>= MAX_MGMT_VERSION
)
1595 if (!is_vendor_class(mad
->mad_hdr
.mgmt_class
)) {
1596 class = port_priv
->version
[
1597 mad
->mad_hdr
.class_version
].class;
1600 method
= class->method_table
[convert_mgmt_class(
1601 mad
->mad_hdr
.mgmt_class
)];
1603 mad_agent
= method
->agent
[mad
->mad_hdr
.method
&
1604 ~IB_MGMT_METHOD_RESP
];
1606 vendor
= port_priv
->version
[
1607 mad
->mad_hdr
.class_version
].vendor
;
1610 vendor_class
= vendor
->vendor_class
[vendor_class_index(
1611 mad
->mad_hdr
.mgmt_class
)];
1614 /* Find matching OUI */
1615 vendor_mad
= (struct ib_vendor_mad
*)mad
;
1616 index
= find_vendor_oui(vendor_class
, vendor_mad
->oui
);
1619 method
= vendor_class
->method_table
[index
];
1621 mad_agent
= method
->agent
[mad
->mad_hdr
.method
&
1622 ~IB_MGMT_METHOD_RESP
];
1628 if (mad_agent
->agent
.recv_handler
)
1629 atomic_inc(&mad_agent
->refcount
);
1631 printk(KERN_NOTICE PFX
"No receive handler for client "
1633 &mad_agent
->agent
, port_priv
->port_num
);
1638 spin_unlock_irqrestore(&port_priv
->reg_lock
, flags
);
1643 static int validate_mad(struct ib_mad
*mad
, u32 qp_num
)
1647 /* Make sure MAD base version is understood */
1648 if (mad
->mad_hdr
.base_version
!= IB_MGMT_BASE_VERSION
) {
1649 printk(KERN_ERR PFX
"MAD received with unsupported base "
1650 "version %d\n", mad
->mad_hdr
.base_version
);
1654 /* Filter SMI packets sent to other than QP0 */
1655 if ((mad
->mad_hdr
.mgmt_class
== IB_MGMT_CLASS_SUBN_LID_ROUTED
) ||
1656 (mad
->mad_hdr
.mgmt_class
== IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
)) {
1660 /* Filter GSI packets sent to QP0 */
1669 static int is_data_mad(struct ib_mad_agent_private
*mad_agent_priv
,
1670 struct ib_mad_hdr
*mad_hdr
)
1672 struct ib_rmpp_mad
*rmpp_mad
;
1674 rmpp_mad
= (struct ib_rmpp_mad
*)mad_hdr
;
1675 return !mad_agent_priv
->agent
.rmpp_version
||
1676 !(ib_get_rmpp_flags(&rmpp_mad
->rmpp_hdr
) &
1677 IB_MGMT_RMPP_FLAG_ACTIVE
) ||
1678 (rmpp_mad
->rmpp_hdr
.rmpp_type
== IB_MGMT_RMPP_TYPE_DATA
);
1681 static inline int rcv_has_same_class(struct ib_mad_send_wr_private
*wr
,
1682 struct ib_mad_recv_wc
*rwc
)
1684 return ((struct ib_mad
*)(wr
->send_buf
.mad
))->mad_hdr
.mgmt_class
==
1685 rwc
->recv_buf
.mad
->mad_hdr
.mgmt_class
;
1688 static inline int rcv_has_same_gid(struct ib_mad_agent_private
*mad_agent_priv
,
1689 struct ib_mad_send_wr_private
*wr
,
1690 struct ib_mad_recv_wc
*rwc
)
1692 struct ib_ah_attr attr
;
1693 u8 send_resp
, rcv_resp
;
1695 struct ib_device
*device
= mad_agent_priv
->agent
.device
;
1696 u8 port_num
= mad_agent_priv
->agent
.port_num
;
1699 send_resp
= ib_response_mad((struct ib_mad
*)wr
->send_buf
.mad
);
1700 rcv_resp
= ib_response_mad(rwc
->recv_buf
.mad
);
1702 if (send_resp
== rcv_resp
)
1703 /* both requests, or both responses. GIDs different */
1706 if (ib_query_ah(wr
->send_buf
.ah
, &attr
))
1707 /* Assume not equal, to avoid false positives. */
1710 if (!!(attr
.ah_flags
& IB_AH_GRH
) !=
1711 !!(rwc
->wc
->wc_flags
& IB_WC_GRH
))
1712 /* one has GID, other does not. Assume different */
1715 if (!send_resp
&& rcv_resp
) {
1716 /* is request/response. */
1717 if (!(attr
.ah_flags
& IB_AH_GRH
)) {
1718 if (ib_get_cached_lmc(device
, port_num
, &lmc
))
1720 return (!lmc
|| !((attr
.src_path_bits
^
1721 rwc
->wc
->dlid_path_bits
) &
1724 if (ib_get_cached_gid(device
, port_num
,
1725 attr
.grh
.sgid_index
, &sgid
))
1727 return !memcmp(sgid
.raw
, rwc
->recv_buf
.grh
->dgid
.raw
,
1732 if (!(attr
.ah_flags
& IB_AH_GRH
))
1733 return attr
.dlid
== rwc
->wc
->slid
;
1735 return !memcmp(attr
.grh
.dgid
.raw
, rwc
->recv_buf
.grh
->sgid
.raw
,
1739 static inline int is_direct(u8
class)
1741 return (class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
);
1744 struct ib_mad_send_wr_private
*
1745 ib_find_send_mad(struct ib_mad_agent_private
*mad_agent_priv
,
1746 struct ib_mad_recv_wc
*wc
)
1748 struct ib_mad_send_wr_private
*wr
;
1751 mad
= (struct ib_mad
*)wc
->recv_buf
.mad
;
1753 list_for_each_entry(wr
, &mad_agent_priv
->wait_list
, agent_list
) {
1754 if ((wr
->tid
== mad
->mad_hdr
.tid
) &&
1755 rcv_has_same_class(wr
, wc
) &&
1757 * Don't check GID for direct routed MADs.
1758 * These might have permissive LIDs.
1760 (is_direct(wc
->recv_buf
.mad
->mad_hdr
.mgmt_class
) ||
1761 rcv_has_same_gid(mad_agent_priv
, wr
, wc
)))
1762 return (wr
->status
== IB_WC_SUCCESS
) ? wr
: NULL
;
1766 * It's possible to receive the response before we've
1767 * been notified that the send has completed
1769 list_for_each_entry(wr
, &mad_agent_priv
->send_list
, agent_list
) {
1770 if (is_data_mad(mad_agent_priv
, wr
->send_buf
.mad
) &&
1771 wr
->tid
== mad
->mad_hdr
.tid
&&
1773 rcv_has_same_class(wr
, wc
) &&
1775 * Don't check GID for direct routed MADs.
1776 * These might have permissive LIDs.
1778 (is_direct(wc
->recv_buf
.mad
->mad_hdr
.mgmt_class
) ||
1779 rcv_has_same_gid(mad_agent_priv
, wr
, wc
)))
1780 /* Verify request has not been canceled */
1781 return (wr
->status
== IB_WC_SUCCESS
) ? wr
: NULL
;
1786 void ib_mark_mad_done(struct ib_mad_send_wr_private
*mad_send_wr
)
1788 mad_send_wr
->timeout
= 0;
1789 if (mad_send_wr
->refcount
== 1)
1790 list_move_tail(&mad_send_wr
->agent_list
,
1791 &mad_send_wr
->mad_agent_priv
->done_list
);
1794 static void ib_mad_complete_recv(struct ib_mad_agent_private
*mad_agent_priv
,
1795 struct ib_mad_recv_wc
*mad_recv_wc
)
1797 struct ib_mad_send_wr_private
*mad_send_wr
;
1798 struct ib_mad_send_wc mad_send_wc
;
1799 unsigned long flags
;
1801 INIT_LIST_HEAD(&mad_recv_wc
->rmpp_list
);
1802 list_add(&mad_recv_wc
->recv_buf
.list
, &mad_recv_wc
->rmpp_list
);
1803 if (mad_agent_priv
->agent
.rmpp_version
) {
1804 mad_recv_wc
= ib_process_rmpp_recv_wc(mad_agent_priv
,
1807 deref_mad_agent(mad_agent_priv
);
1812 /* Complete corresponding request */
1813 if (ib_response_mad(mad_recv_wc
->recv_buf
.mad
)) {
1814 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
1815 mad_send_wr
= ib_find_send_mad(mad_agent_priv
, mad_recv_wc
);
1817 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
1818 ib_free_recv_mad(mad_recv_wc
);
1819 deref_mad_agent(mad_agent_priv
);
1822 ib_mark_mad_done(mad_send_wr
);
1823 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
1825 /* Defined behavior is to complete response before request */
1826 mad_recv_wc
->wc
->wr_id
= (unsigned long) &mad_send_wr
->send_buf
;
1827 mad_agent_priv
->agent
.recv_handler(&mad_agent_priv
->agent
,
1829 atomic_dec(&mad_agent_priv
->refcount
);
1831 mad_send_wc
.status
= IB_WC_SUCCESS
;
1832 mad_send_wc
.vendor_err
= 0;
1833 mad_send_wc
.send_buf
= &mad_send_wr
->send_buf
;
1834 ib_mad_complete_send_wr(mad_send_wr
, &mad_send_wc
);
1836 mad_agent_priv
->agent
.recv_handler(&mad_agent_priv
->agent
,
1838 deref_mad_agent(mad_agent_priv
);
1842 static void ib_mad_recv_done_handler(struct ib_mad_port_private
*port_priv
,
1845 struct ib_mad_qp_info
*qp_info
;
1846 struct ib_mad_private_header
*mad_priv_hdr
;
1847 struct ib_mad_private
*recv
, *response
= NULL
;
1848 struct ib_mad_list_head
*mad_list
;
1849 struct ib_mad_agent_private
*mad_agent
;
1852 mad_list
= (struct ib_mad_list_head
*)(unsigned long)wc
->wr_id
;
1853 qp_info
= mad_list
->mad_queue
->qp_info
;
1854 dequeue_mad(mad_list
);
1856 mad_priv_hdr
= container_of(mad_list
, struct ib_mad_private_header
,
1858 recv
= container_of(mad_priv_hdr
, struct ib_mad_private
, header
);
1859 ib_dma_unmap_single(port_priv
->device
,
1860 recv
->header
.mapping
,
1861 sizeof(struct ib_mad_private
) -
1862 sizeof(struct ib_mad_private_header
),
1865 /* Setup MAD receive work completion from "normal" work completion */
1866 recv
->header
.wc
= *wc
;
1867 recv
->header
.recv_wc
.wc
= &recv
->header
.wc
;
1868 recv
->header
.recv_wc
.mad_len
= sizeof(struct ib_mad
);
1869 recv
->header
.recv_wc
.recv_buf
.mad
= &recv
->mad
.mad
;
1870 recv
->header
.recv_wc
.recv_buf
.grh
= &recv
->grh
;
1872 if (atomic_read(&qp_info
->snoop_count
))
1873 snoop_recv(qp_info
, &recv
->header
.recv_wc
, IB_MAD_SNOOP_RECVS
);
1876 if (!validate_mad(&recv
->mad
.mad
, qp_info
->qp
->qp_num
))
1879 response
= kmem_cache_alloc(ib_mad_cache
, GFP_KERNEL
);
1881 printk(KERN_ERR PFX
"ib_mad_recv_done_handler no memory "
1882 "for response buffer\n");
1886 if (port_priv
->device
->node_type
== RDMA_NODE_IB_SWITCH
)
1887 port_num
= wc
->port_num
;
1889 port_num
= port_priv
->port_num
;
1891 if (recv
->mad
.mad
.mad_hdr
.mgmt_class
==
1892 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
) {
1893 enum smi_forward_action retsmi
;
1895 if (smi_handle_dr_smp_recv(&recv
->mad
.smp
,
1896 port_priv
->device
->node_type
,
1898 port_priv
->device
->phys_port_cnt
) ==
1902 retsmi
= smi_check_forward_dr_smp(&recv
->mad
.smp
);
1903 if (retsmi
== IB_SMI_LOCAL
)
1906 if (retsmi
== IB_SMI_SEND
) { /* don't forward */
1907 if (smi_handle_dr_smp_send(&recv
->mad
.smp
,
1908 port_priv
->device
->node_type
,
1909 port_num
) == IB_SMI_DISCARD
)
1912 if (smi_check_local_smp(&recv
->mad
.smp
, port_priv
->device
) == IB_SMI_DISCARD
)
1914 } else if (port_priv
->device
->node_type
== RDMA_NODE_IB_SWITCH
) {
1915 /* forward case for switches */
1916 memcpy(response
, recv
, sizeof(*response
));
1917 response
->header
.recv_wc
.wc
= &response
->header
.wc
;
1918 response
->header
.recv_wc
.recv_buf
.mad
= &response
->mad
.mad
;
1919 response
->header
.recv_wc
.recv_buf
.grh
= &response
->grh
;
1921 agent_send_response(&response
->mad
.mad
,
1924 smi_get_fwd_port(&recv
->mad
.smp
),
1925 qp_info
->qp
->qp_num
);
1932 /* Give driver "right of first refusal" on incoming MAD */
1933 if (port_priv
->device
->process_mad
) {
1936 ret
= port_priv
->device
->process_mad(port_priv
->device
, 0,
1937 port_priv
->port_num
,
1940 &response
->mad
.mad
);
1941 if (ret
& IB_MAD_RESULT_SUCCESS
) {
1942 if (ret
& IB_MAD_RESULT_CONSUMED
)
1944 if (ret
& IB_MAD_RESULT_REPLY
) {
1945 agent_send_response(&response
->mad
.mad
,
1949 qp_info
->qp
->qp_num
);
1955 mad_agent
= find_mad_agent(port_priv
, &recv
->mad
.mad
);
1957 ib_mad_complete_recv(mad_agent
, &recv
->header
.recv_wc
);
1959 * recv is freed up in error cases in ib_mad_complete_recv
1960 * or via recv_handler in ib_mad_complete_recv()
1966 /* Post another receive request for this QP */
1968 ib_mad_post_receive_mads(qp_info
, response
);
1970 kmem_cache_free(ib_mad_cache
, recv
);
1972 ib_mad_post_receive_mads(qp_info
, recv
);
1975 static void adjust_timeout(struct ib_mad_agent_private
*mad_agent_priv
)
1977 struct ib_mad_send_wr_private
*mad_send_wr
;
1978 unsigned long delay
;
1980 if (list_empty(&mad_agent_priv
->wait_list
)) {
1981 __cancel_delayed_work(&mad_agent_priv
->timed_work
);
1983 mad_send_wr
= list_entry(mad_agent_priv
->wait_list
.next
,
1984 struct ib_mad_send_wr_private
,
1987 if (time_after(mad_agent_priv
->timeout
,
1988 mad_send_wr
->timeout
)) {
1989 mad_agent_priv
->timeout
= mad_send_wr
->timeout
;
1990 __cancel_delayed_work(&mad_agent_priv
->timed_work
);
1991 delay
= mad_send_wr
->timeout
- jiffies
;
1992 if ((long)delay
<= 0)
1994 queue_delayed_work(mad_agent_priv
->qp_info
->
1996 &mad_agent_priv
->timed_work
, delay
);
2001 static void wait_for_response(struct ib_mad_send_wr_private
*mad_send_wr
)
2003 struct ib_mad_agent_private
*mad_agent_priv
;
2004 struct ib_mad_send_wr_private
*temp_mad_send_wr
;
2005 struct list_head
*list_item
;
2006 unsigned long delay
;
2008 mad_agent_priv
= mad_send_wr
->mad_agent_priv
;
2009 list_del(&mad_send_wr
->agent_list
);
2011 delay
= mad_send_wr
->timeout
;
2012 mad_send_wr
->timeout
+= jiffies
;
2015 list_for_each_prev(list_item
, &mad_agent_priv
->wait_list
) {
2016 temp_mad_send_wr
= list_entry(list_item
,
2017 struct ib_mad_send_wr_private
,
2019 if (time_after(mad_send_wr
->timeout
,
2020 temp_mad_send_wr
->timeout
))
2025 list_item
= &mad_agent_priv
->wait_list
;
2026 list_add(&mad_send_wr
->agent_list
, list_item
);
2028 /* Reschedule a work item if we have a shorter timeout */
2029 if (mad_agent_priv
->wait_list
.next
== &mad_send_wr
->agent_list
) {
2030 __cancel_delayed_work(&mad_agent_priv
->timed_work
);
2031 queue_delayed_work(mad_agent_priv
->qp_info
->port_priv
->wq
,
2032 &mad_agent_priv
->timed_work
, delay
);
2036 void ib_reset_mad_timeout(struct ib_mad_send_wr_private
*mad_send_wr
,
2039 mad_send_wr
->timeout
= msecs_to_jiffies(timeout_ms
);
2040 wait_for_response(mad_send_wr
);
2044 * Process a send work completion
2046 void ib_mad_complete_send_wr(struct ib_mad_send_wr_private
*mad_send_wr
,
2047 struct ib_mad_send_wc
*mad_send_wc
)
2049 struct ib_mad_agent_private
*mad_agent_priv
;
2050 unsigned long flags
;
2053 mad_agent_priv
= mad_send_wr
->mad_agent_priv
;
2054 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2055 if (mad_agent_priv
->agent
.rmpp_version
) {
2056 ret
= ib_process_rmpp_send_wc(mad_send_wr
, mad_send_wc
);
2057 if (ret
== IB_RMPP_RESULT_CONSUMED
)
2060 ret
= IB_RMPP_RESULT_UNHANDLED
;
2062 if (mad_send_wc
->status
!= IB_WC_SUCCESS
&&
2063 mad_send_wr
->status
== IB_WC_SUCCESS
) {
2064 mad_send_wr
->status
= mad_send_wc
->status
;
2065 mad_send_wr
->refcount
-= (mad_send_wr
->timeout
> 0);
2068 if (--mad_send_wr
->refcount
> 0) {
2069 if (mad_send_wr
->refcount
== 1 && mad_send_wr
->timeout
&&
2070 mad_send_wr
->status
== IB_WC_SUCCESS
) {
2071 wait_for_response(mad_send_wr
);
2076 /* Remove send from MAD agent and notify client of completion */
2077 list_del(&mad_send_wr
->agent_list
);
2078 adjust_timeout(mad_agent_priv
);
2079 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2081 if (mad_send_wr
->status
!= IB_WC_SUCCESS
)
2082 mad_send_wc
->status
= mad_send_wr
->status
;
2083 if (ret
== IB_RMPP_RESULT_INTERNAL
)
2084 ib_rmpp_send_handler(mad_send_wc
);
2086 mad_agent_priv
->agent
.send_handler(&mad_agent_priv
->agent
,
2089 /* Release reference on agent taken when sending */
2090 deref_mad_agent(mad_agent_priv
);
2093 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2096 static void ib_mad_send_done_handler(struct ib_mad_port_private
*port_priv
,
2099 struct ib_mad_send_wr_private
*mad_send_wr
, *queued_send_wr
;
2100 struct ib_mad_list_head
*mad_list
;
2101 struct ib_mad_qp_info
*qp_info
;
2102 struct ib_mad_queue
*send_queue
;
2103 struct ib_send_wr
*bad_send_wr
;
2104 struct ib_mad_send_wc mad_send_wc
;
2105 unsigned long flags
;
2108 mad_list
= (struct ib_mad_list_head
*)(unsigned long)wc
->wr_id
;
2109 mad_send_wr
= container_of(mad_list
, struct ib_mad_send_wr_private
,
2111 send_queue
= mad_list
->mad_queue
;
2112 qp_info
= send_queue
->qp_info
;
2115 ib_dma_unmap_single(mad_send_wr
->send_buf
.mad_agent
->device
,
2116 mad_send_wr
->header_mapping
,
2117 mad_send_wr
->sg_list
[0].length
, DMA_TO_DEVICE
);
2118 ib_dma_unmap_single(mad_send_wr
->send_buf
.mad_agent
->device
,
2119 mad_send_wr
->payload_mapping
,
2120 mad_send_wr
->sg_list
[1].length
, DMA_TO_DEVICE
);
2121 queued_send_wr
= NULL
;
2122 spin_lock_irqsave(&send_queue
->lock
, flags
);
2123 list_del(&mad_list
->list
);
2125 /* Move queued send to the send queue */
2126 if (send_queue
->count
-- > send_queue
->max_active
) {
2127 mad_list
= container_of(qp_info
->overflow_list
.next
,
2128 struct ib_mad_list_head
, list
);
2129 queued_send_wr
= container_of(mad_list
,
2130 struct ib_mad_send_wr_private
,
2132 list_move_tail(&mad_list
->list
, &send_queue
->list
);
2134 spin_unlock_irqrestore(&send_queue
->lock
, flags
);
2136 mad_send_wc
.send_buf
= &mad_send_wr
->send_buf
;
2137 mad_send_wc
.status
= wc
->status
;
2138 mad_send_wc
.vendor_err
= wc
->vendor_err
;
2139 if (atomic_read(&qp_info
->snoop_count
))
2140 snoop_send(qp_info
, &mad_send_wr
->send_buf
, &mad_send_wc
,
2141 IB_MAD_SNOOP_SEND_COMPLETIONS
);
2142 ib_mad_complete_send_wr(mad_send_wr
, &mad_send_wc
);
2144 if (queued_send_wr
) {
2145 ret
= ib_post_send(qp_info
->qp
, &queued_send_wr
->send_wr
,
2148 printk(KERN_ERR PFX
"ib_post_send failed: %d\n", ret
);
2149 mad_send_wr
= queued_send_wr
;
2150 wc
->status
= IB_WC_LOC_QP_OP_ERR
;
2156 static void mark_sends_for_retry(struct ib_mad_qp_info
*qp_info
)
2158 struct ib_mad_send_wr_private
*mad_send_wr
;
2159 struct ib_mad_list_head
*mad_list
;
2160 unsigned long flags
;
2162 spin_lock_irqsave(&qp_info
->send_queue
.lock
, flags
);
2163 list_for_each_entry(mad_list
, &qp_info
->send_queue
.list
, list
) {
2164 mad_send_wr
= container_of(mad_list
,
2165 struct ib_mad_send_wr_private
,
2167 mad_send_wr
->retry
= 1;
2169 spin_unlock_irqrestore(&qp_info
->send_queue
.lock
, flags
);
2172 static void mad_error_handler(struct ib_mad_port_private
*port_priv
,
2175 struct ib_mad_list_head
*mad_list
;
2176 struct ib_mad_qp_info
*qp_info
;
2177 struct ib_mad_send_wr_private
*mad_send_wr
;
2180 /* Determine if failure was a send or receive */
2181 mad_list
= (struct ib_mad_list_head
*)(unsigned long)wc
->wr_id
;
2182 qp_info
= mad_list
->mad_queue
->qp_info
;
2183 if (mad_list
->mad_queue
== &qp_info
->recv_queue
)
2185 * Receive errors indicate that the QP has entered the error
2186 * state - error handling/shutdown code will cleanup
2191 * Send errors will transition the QP to SQE - move
2192 * QP to RTS and repost flushed work requests
2194 mad_send_wr
= container_of(mad_list
, struct ib_mad_send_wr_private
,
2196 if (wc
->status
== IB_WC_WR_FLUSH_ERR
) {
2197 if (mad_send_wr
->retry
) {
2199 struct ib_send_wr
*bad_send_wr
;
2201 mad_send_wr
->retry
= 0;
2202 ret
= ib_post_send(qp_info
->qp
, &mad_send_wr
->send_wr
,
2205 ib_mad_send_done_handler(port_priv
, wc
);
2207 ib_mad_send_done_handler(port_priv
, wc
);
2209 struct ib_qp_attr
*attr
;
2211 /* Transition QP to RTS and fail offending send */
2212 attr
= kmalloc(sizeof *attr
, GFP_KERNEL
);
2214 attr
->qp_state
= IB_QPS_RTS
;
2215 attr
->cur_qp_state
= IB_QPS_SQE
;
2216 ret
= ib_modify_qp(qp_info
->qp
, attr
,
2217 IB_QP_STATE
| IB_QP_CUR_STATE
);
2220 printk(KERN_ERR PFX
"mad_error_handler - "
2221 "ib_modify_qp to RTS : %d\n", ret
);
2223 mark_sends_for_retry(qp_info
);
2225 ib_mad_send_done_handler(port_priv
, wc
);
2230 * IB MAD completion callback
2232 static void ib_mad_completion_handler(struct work_struct
*work
)
2234 struct ib_mad_port_private
*port_priv
;
2237 port_priv
= container_of(work
, struct ib_mad_port_private
, work
);
2238 ib_req_notify_cq(port_priv
->cq
, IB_CQ_NEXT_COMP
);
2240 while (ib_poll_cq(port_priv
->cq
, 1, &wc
) == 1) {
2241 if (wc
.status
== IB_WC_SUCCESS
) {
2242 switch (wc
.opcode
) {
2244 ib_mad_send_done_handler(port_priv
, &wc
);
2247 ib_mad_recv_done_handler(port_priv
, &wc
);
2254 mad_error_handler(port_priv
, &wc
);
2258 static void cancel_mads(struct ib_mad_agent_private
*mad_agent_priv
)
2260 unsigned long flags
;
2261 struct ib_mad_send_wr_private
*mad_send_wr
, *temp_mad_send_wr
;
2262 struct ib_mad_send_wc mad_send_wc
;
2263 struct list_head cancel_list
;
2265 INIT_LIST_HEAD(&cancel_list
);
2267 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2268 list_for_each_entry_safe(mad_send_wr
, temp_mad_send_wr
,
2269 &mad_agent_priv
->send_list
, agent_list
) {
2270 if (mad_send_wr
->status
== IB_WC_SUCCESS
) {
2271 mad_send_wr
->status
= IB_WC_WR_FLUSH_ERR
;
2272 mad_send_wr
->refcount
-= (mad_send_wr
->timeout
> 0);
2276 /* Empty wait list to prevent receives from finding a request */
2277 list_splice_init(&mad_agent_priv
->wait_list
, &cancel_list
);
2278 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2280 /* Report all cancelled requests */
2281 mad_send_wc
.status
= IB_WC_WR_FLUSH_ERR
;
2282 mad_send_wc
.vendor_err
= 0;
2284 list_for_each_entry_safe(mad_send_wr
, temp_mad_send_wr
,
2285 &cancel_list
, agent_list
) {
2286 mad_send_wc
.send_buf
= &mad_send_wr
->send_buf
;
2287 list_del(&mad_send_wr
->agent_list
);
2288 mad_agent_priv
->agent
.send_handler(&mad_agent_priv
->agent
,
2290 atomic_dec(&mad_agent_priv
->refcount
);
2294 static struct ib_mad_send_wr_private
*
2295 find_send_wr(struct ib_mad_agent_private
*mad_agent_priv
,
2296 struct ib_mad_send_buf
*send_buf
)
2298 struct ib_mad_send_wr_private
*mad_send_wr
;
2300 list_for_each_entry(mad_send_wr
, &mad_agent_priv
->wait_list
,
2302 if (&mad_send_wr
->send_buf
== send_buf
)
2306 list_for_each_entry(mad_send_wr
, &mad_agent_priv
->send_list
,
2308 if (is_data_mad(mad_agent_priv
, mad_send_wr
->send_buf
.mad
) &&
2309 &mad_send_wr
->send_buf
== send_buf
)
2315 int ib_modify_mad(struct ib_mad_agent
*mad_agent
,
2316 struct ib_mad_send_buf
*send_buf
, u32 timeout_ms
)
2318 struct ib_mad_agent_private
*mad_agent_priv
;
2319 struct ib_mad_send_wr_private
*mad_send_wr
;
2320 unsigned long flags
;
2323 mad_agent_priv
= container_of(mad_agent
, struct ib_mad_agent_private
,
2325 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2326 mad_send_wr
= find_send_wr(mad_agent_priv
, send_buf
);
2327 if (!mad_send_wr
|| mad_send_wr
->status
!= IB_WC_SUCCESS
) {
2328 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2332 active
= (!mad_send_wr
->timeout
|| mad_send_wr
->refcount
> 1);
2334 mad_send_wr
->status
= IB_WC_WR_FLUSH_ERR
;
2335 mad_send_wr
->refcount
-= (mad_send_wr
->timeout
> 0);
2338 mad_send_wr
->send_buf
.timeout_ms
= timeout_ms
;
2340 mad_send_wr
->timeout
= msecs_to_jiffies(timeout_ms
);
2342 ib_reset_mad_timeout(mad_send_wr
, timeout_ms
);
2344 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2347 EXPORT_SYMBOL(ib_modify_mad
);
2349 void ib_cancel_mad(struct ib_mad_agent
*mad_agent
,
2350 struct ib_mad_send_buf
*send_buf
)
2352 ib_modify_mad(mad_agent
, send_buf
, 0);
2354 EXPORT_SYMBOL(ib_cancel_mad
);
2356 static void local_completions(struct work_struct
*work
)
2358 struct ib_mad_agent_private
*mad_agent_priv
;
2359 struct ib_mad_local_private
*local
;
2360 struct ib_mad_agent_private
*recv_mad_agent
;
2361 unsigned long flags
;
2364 struct ib_mad_send_wc mad_send_wc
;
2367 container_of(work
, struct ib_mad_agent_private
, local_work
);
2369 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2370 while (!list_empty(&mad_agent_priv
->local_list
)) {
2371 local
= list_entry(mad_agent_priv
->local_list
.next
,
2372 struct ib_mad_local_private
,
2374 list_del(&local
->completion_list
);
2375 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2377 if (local
->mad_priv
) {
2378 recv_mad_agent
= local
->recv_mad_agent
;
2379 if (!recv_mad_agent
) {
2380 printk(KERN_ERR PFX
"No receive MAD agent for local completion\n");
2382 goto local_send_completion
;
2386 * Defined behavior is to complete response
2389 build_smp_wc(recv_mad_agent
->agent
.qp
,
2390 (unsigned long) local
->mad_send_wr
,
2391 be16_to_cpu(IB_LID_PERMISSIVE
),
2392 0, recv_mad_agent
->agent
.port_num
, &wc
);
2394 local
->mad_priv
->header
.recv_wc
.wc
= &wc
;
2395 local
->mad_priv
->header
.recv_wc
.mad_len
=
2396 sizeof(struct ib_mad
);
2397 INIT_LIST_HEAD(&local
->mad_priv
->header
.recv_wc
.rmpp_list
);
2398 list_add(&local
->mad_priv
->header
.recv_wc
.recv_buf
.list
,
2399 &local
->mad_priv
->header
.recv_wc
.rmpp_list
);
2400 local
->mad_priv
->header
.recv_wc
.recv_buf
.grh
= NULL
;
2401 local
->mad_priv
->header
.recv_wc
.recv_buf
.mad
=
2402 &local
->mad_priv
->mad
.mad
;
2403 if (atomic_read(&recv_mad_agent
->qp_info
->snoop_count
))
2404 snoop_recv(recv_mad_agent
->qp_info
,
2405 &local
->mad_priv
->header
.recv_wc
,
2406 IB_MAD_SNOOP_RECVS
);
2407 recv_mad_agent
->agent
.recv_handler(
2408 &recv_mad_agent
->agent
,
2409 &local
->mad_priv
->header
.recv_wc
);
2410 spin_lock_irqsave(&recv_mad_agent
->lock
, flags
);
2411 atomic_dec(&recv_mad_agent
->refcount
);
2412 spin_unlock_irqrestore(&recv_mad_agent
->lock
, flags
);
2415 local_send_completion
:
2417 mad_send_wc
.status
= IB_WC_SUCCESS
;
2418 mad_send_wc
.vendor_err
= 0;
2419 mad_send_wc
.send_buf
= &local
->mad_send_wr
->send_buf
;
2420 if (atomic_read(&mad_agent_priv
->qp_info
->snoop_count
))
2421 snoop_send(mad_agent_priv
->qp_info
,
2422 &local
->mad_send_wr
->send_buf
,
2423 &mad_send_wc
, IB_MAD_SNOOP_SEND_COMPLETIONS
);
2424 mad_agent_priv
->agent
.send_handler(&mad_agent_priv
->agent
,
2427 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2428 atomic_dec(&mad_agent_priv
->refcount
);
2430 kmem_cache_free(ib_mad_cache
, local
->mad_priv
);
2433 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2436 static int retry_send(struct ib_mad_send_wr_private
*mad_send_wr
)
2440 if (!mad_send_wr
->retries_left
)
2443 mad_send_wr
->retries_left
--;
2444 mad_send_wr
->send_buf
.retries
++;
2446 mad_send_wr
->timeout
= msecs_to_jiffies(mad_send_wr
->send_buf
.timeout_ms
);
2448 if (mad_send_wr
->mad_agent_priv
->agent
.rmpp_version
) {
2449 ret
= ib_retry_rmpp(mad_send_wr
);
2451 case IB_RMPP_RESULT_UNHANDLED
:
2452 ret
= ib_send_mad(mad_send_wr
);
2454 case IB_RMPP_RESULT_CONSUMED
:
2462 ret
= ib_send_mad(mad_send_wr
);
2465 mad_send_wr
->refcount
++;
2466 list_add_tail(&mad_send_wr
->agent_list
,
2467 &mad_send_wr
->mad_agent_priv
->send_list
);
2472 static void timeout_sends(struct work_struct
*work
)
2474 struct ib_mad_agent_private
*mad_agent_priv
;
2475 struct ib_mad_send_wr_private
*mad_send_wr
;
2476 struct ib_mad_send_wc mad_send_wc
;
2477 unsigned long flags
, delay
;
2479 mad_agent_priv
= container_of(work
, struct ib_mad_agent_private
,
2481 mad_send_wc
.vendor_err
= 0;
2483 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2484 while (!list_empty(&mad_agent_priv
->wait_list
)) {
2485 mad_send_wr
= list_entry(mad_agent_priv
->wait_list
.next
,
2486 struct ib_mad_send_wr_private
,
2489 if (time_after(mad_send_wr
->timeout
, jiffies
)) {
2490 delay
= mad_send_wr
->timeout
- jiffies
;
2491 if ((long)delay
<= 0)
2493 queue_delayed_work(mad_agent_priv
->qp_info
->
2495 &mad_agent_priv
->timed_work
, delay
);
2499 list_del(&mad_send_wr
->agent_list
);
2500 if (mad_send_wr
->status
== IB_WC_SUCCESS
&&
2501 !retry_send(mad_send_wr
))
2504 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2506 if (mad_send_wr
->status
== IB_WC_SUCCESS
)
2507 mad_send_wc
.status
= IB_WC_RESP_TIMEOUT_ERR
;
2509 mad_send_wc
.status
= mad_send_wr
->status
;
2510 mad_send_wc
.send_buf
= &mad_send_wr
->send_buf
;
2511 mad_agent_priv
->agent
.send_handler(&mad_agent_priv
->agent
,
2514 atomic_dec(&mad_agent_priv
->refcount
);
2515 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2517 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2520 static void ib_mad_thread_completion_handler(struct ib_cq
*cq
, void *arg
)
2522 struct ib_mad_port_private
*port_priv
= cq
->cq_context
;
2523 unsigned long flags
;
2525 spin_lock_irqsave(&ib_mad_port_list_lock
, flags
);
2526 if (!list_empty(&port_priv
->port_list
))
2527 queue_work(port_priv
->wq
, &port_priv
->work
);
2528 spin_unlock_irqrestore(&ib_mad_port_list_lock
, flags
);
2532 * Allocate receive MADs and post receive WRs for them
2534 static int ib_mad_post_receive_mads(struct ib_mad_qp_info
*qp_info
,
2535 struct ib_mad_private
*mad
)
2537 unsigned long flags
;
2539 struct ib_mad_private
*mad_priv
;
2540 struct ib_sge sg_list
;
2541 struct ib_recv_wr recv_wr
, *bad_recv_wr
;
2542 struct ib_mad_queue
*recv_queue
= &qp_info
->recv_queue
;
2544 /* Initialize common scatter list fields */
2545 sg_list
.length
= sizeof *mad_priv
- sizeof mad_priv
->header
;
2546 sg_list
.lkey
= (*qp_info
->port_priv
->mr
).lkey
;
2548 /* Initialize common receive WR fields */
2549 recv_wr
.next
= NULL
;
2550 recv_wr
.sg_list
= &sg_list
;
2551 recv_wr
.num_sge
= 1;
2554 /* Allocate and map receive buffer */
2559 mad_priv
= kmem_cache_alloc(ib_mad_cache
, GFP_KERNEL
);
2561 printk(KERN_ERR PFX
"No memory for receive buffer\n");
2566 sg_list
.addr
= ib_dma_map_single(qp_info
->port_priv
->device
,
2569 sizeof mad_priv
->header
,
2571 mad_priv
->header
.mapping
= sg_list
.addr
;
2572 recv_wr
.wr_id
= (unsigned long)&mad_priv
->header
.mad_list
;
2573 mad_priv
->header
.mad_list
.mad_queue
= recv_queue
;
2575 /* Post receive WR */
2576 spin_lock_irqsave(&recv_queue
->lock
, flags
);
2577 post
= (++recv_queue
->count
< recv_queue
->max_active
);
2578 list_add_tail(&mad_priv
->header
.mad_list
.list
, &recv_queue
->list
);
2579 spin_unlock_irqrestore(&recv_queue
->lock
, flags
);
2580 ret
= ib_post_recv(qp_info
->qp
, &recv_wr
, &bad_recv_wr
);
2582 spin_lock_irqsave(&recv_queue
->lock
, flags
);
2583 list_del(&mad_priv
->header
.mad_list
.list
);
2584 recv_queue
->count
--;
2585 spin_unlock_irqrestore(&recv_queue
->lock
, flags
);
2586 ib_dma_unmap_single(qp_info
->port_priv
->device
,
2587 mad_priv
->header
.mapping
,
2589 sizeof mad_priv
->header
,
2591 kmem_cache_free(ib_mad_cache
, mad_priv
);
2592 printk(KERN_ERR PFX
"ib_post_recv failed: %d\n", ret
);
2601 * Return all the posted receive MADs
2603 static void cleanup_recv_queue(struct ib_mad_qp_info
*qp_info
)
2605 struct ib_mad_private_header
*mad_priv_hdr
;
2606 struct ib_mad_private
*recv
;
2607 struct ib_mad_list_head
*mad_list
;
2612 while (!list_empty(&qp_info
->recv_queue
.list
)) {
2614 mad_list
= list_entry(qp_info
->recv_queue
.list
.next
,
2615 struct ib_mad_list_head
, list
);
2616 mad_priv_hdr
= container_of(mad_list
,
2617 struct ib_mad_private_header
,
2619 recv
= container_of(mad_priv_hdr
, struct ib_mad_private
,
2622 /* Remove from posted receive MAD list */
2623 list_del(&mad_list
->list
);
2625 ib_dma_unmap_single(qp_info
->port_priv
->device
,
2626 recv
->header
.mapping
,
2627 sizeof(struct ib_mad_private
) -
2628 sizeof(struct ib_mad_private_header
),
2630 kmem_cache_free(ib_mad_cache
, recv
);
2633 qp_info
->recv_queue
.count
= 0;
2639 static int ib_mad_port_start(struct ib_mad_port_private
*port_priv
)
2642 struct ib_qp_attr
*attr
;
2645 attr
= kmalloc(sizeof *attr
, GFP_KERNEL
);
2647 printk(KERN_ERR PFX
"Couldn't kmalloc ib_qp_attr\n");
2651 for (i
= 0; i
< IB_MAD_QPS_CORE
; i
++) {
2652 qp
= port_priv
->qp_info
[i
].qp
;
2657 * PKey index for QP1 is irrelevant but
2658 * one is needed for the Reset to Init transition
2660 attr
->qp_state
= IB_QPS_INIT
;
2661 attr
->pkey_index
= 0;
2662 attr
->qkey
= (qp
->qp_num
== 0) ? 0 : IB_QP1_QKEY
;
2663 ret
= ib_modify_qp(qp
, attr
, IB_QP_STATE
|
2664 IB_QP_PKEY_INDEX
| IB_QP_QKEY
);
2666 printk(KERN_ERR PFX
"Couldn't change QP%d state to "
2667 "INIT: %d\n", i
, ret
);
2671 attr
->qp_state
= IB_QPS_RTR
;
2672 ret
= ib_modify_qp(qp
, attr
, IB_QP_STATE
);
2674 printk(KERN_ERR PFX
"Couldn't change QP%d state to "
2675 "RTR: %d\n", i
, ret
);
2679 attr
->qp_state
= IB_QPS_RTS
;
2680 attr
->sq_psn
= IB_MAD_SEND_Q_PSN
;
2681 ret
= ib_modify_qp(qp
, attr
, IB_QP_STATE
| IB_QP_SQ_PSN
);
2683 printk(KERN_ERR PFX
"Couldn't change QP%d state to "
2684 "RTS: %d\n", i
, ret
);
2689 ret
= ib_req_notify_cq(port_priv
->cq
, IB_CQ_NEXT_COMP
);
2691 printk(KERN_ERR PFX
"Failed to request completion "
2692 "notification: %d\n", ret
);
2696 for (i
= 0; i
< IB_MAD_QPS_CORE
; i
++) {
2697 if (!port_priv
->qp_info
[i
].qp
)
2700 ret
= ib_mad_post_receive_mads(&port_priv
->qp_info
[i
], NULL
);
2702 printk(KERN_ERR PFX
"Couldn't post receive WRs\n");
2711 static void qp_event_handler(struct ib_event
*event
, void *qp_context
)
2713 struct ib_mad_qp_info
*qp_info
= qp_context
;
2715 /* It's worse than that! He's dead, Jim! */
2716 printk(KERN_ERR PFX
"Fatal error (%d) on MAD QP (%d)\n",
2717 event
->event
, qp_info
->qp
->qp_num
);
2720 static void init_mad_queue(struct ib_mad_qp_info
*qp_info
,
2721 struct ib_mad_queue
*mad_queue
)
2723 mad_queue
->qp_info
= qp_info
;
2724 mad_queue
->count
= 0;
2725 spin_lock_init(&mad_queue
->lock
);
2726 INIT_LIST_HEAD(&mad_queue
->list
);
2729 static void init_mad_qp(struct ib_mad_port_private
*port_priv
,
2730 struct ib_mad_qp_info
*qp_info
)
2732 qp_info
->port_priv
= port_priv
;
2733 init_mad_queue(qp_info
, &qp_info
->send_queue
);
2734 init_mad_queue(qp_info
, &qp_info
->recv_queue
);
2735 INIT_LIST_HEAD(&qp_info
->overflow_list
);
2736 spin_lock_init(&qp_info
->snoop_lock
);
2737 qp_info
->snoop_table
= NULL
;
2738 qp_info
->snoop_table_size
= 0;
2739 atomic_set(&qp_info
->snoop_count
, 0);
2742 static int create_mad_qp(struct ib_mad_qp_info
*qp_info
,
2743 enum ib_qp_type qp_type
)
2745 struct ib_qp_init_attr qp_init_attr
;
2748 memset(&qp_init_attr
, 0, sizeof qp_init_attr
);
2749 qp_init_attr
.send_cq
= qp_info
->port_priv
->cq
;
2750 qp_init_attr
.recv_cq
= qp_info
->port_priv
->cq
;
2751 qp_init_attr
.sq_sig_type
= IB_SIGNAL_ALL_WR
;
2752 qp_init_attr
.cap
.max_send_wr
= mad_sendq_size
;
2753 qp_init_attr
.cap
.max_recv_wr
= mad_recvq_size
;
2754 qp_init_attr
.cap
.max_send_sge
= IB_MAD_SEND_REQ_MAX_SG
;
2755 qp_init_attr
.cap
.max_recv_sge
= IB_MAD_RECV_REQ_MAX_SG
;
2756 qp_init_attr
.qp_type
= qp_type
;
2757 qp_init_attr
.port_num
= qp_info
->port_priv
->port_num
;
2758 qp_init_attr
.qp_context
= qp_info
;
2759 qp_init_attr
.event_handler
= qp_event_handler
;
2760 qp_info
->qp
= ib_create_qp(qp_info
->port_priv
->pd
, &qp_init_attr
);
2761 if (IS_ERR(qp_info
->qp
)) {
2762 printk(KERN_ERR PFX
"Couldn't create ib_mad QP%d\n",
2763 get_spl_qp_index(qp_type
));
2764 ret
= PTR_ERR(qp_info
->qp
);
2767 /* Use minimum queue sizes unless the CQ is resized */
2768 qp_info
->send_queue
.max_active
= mad_sendq_size
;
2769 qp_info
->recv_queue
.max_active
= mad_recvq_size
;
2776 static void destroy_mad_qp(struct ib_mad_qp_info
*qp_info
)
2781 ib_destroy_qp(qp_info
->qp
);
2782 kfree(qp_info
->snoop_table
);
2787 * Create the QP, PD, MR, and CQ if needed
2789 static int ib_mad_port_open(struct ib_device
*device
,
2793 struct ib_mad_port_private
*port_priv
;
2794 unsigned long flags
;
2795 char name
[sizeof "ib_mad123"];
2798 /* Create new device info */
2799 port_priv
= kzalloc(sizeof *port_priv
, GFP_KERNEL
);
2801 printk(KERN_ERR PFX
"No memory for ib_mad_port_private\n");
2805 port_priv
->device
= device
;
2806 port_priv
->port_num
= port_num
;
2807 spin_lock_init(&port_priv
->reg_lock
);
2808 INIT_LIST_HEAD(&port_priv
->agent_list
);
2809 init_mad_qp(port_priv
, &port_priv
->qp_info
[0]);
2810 init_mad_qp(port_priv
, &port_priv
->qp_info
[1]);
2812 cq_size
= mad_sendq_size
+ mad_recvq_size
;
2813 has_smi
= rdma_port_get_link_layer(device
, port_num
) == IB_LINK_LAYER_INFINIBAND
;
2817 port_priv
->cq
= ib_create_cq(port_priv
->device
,
2818 ib_mad_thread_completion_handler
,
2819 NULL
, port_priv
, cq_size
, 0);
2820 if (IS_ERR(port_priv
->cq
)) {
2821 printk(KERN_ERR PFX
"Couldn't create ib_mad CQ\n");
2822 ret
= PTR_ERR(port_priv
->cq
);
2826 port_priv
->pd
= ib_alloc_pd(device
);
2827 if (IS_ERR(port_priv
->pd
)) {
2828 printk(KERN_ERR PFX
"Couldn't create ib_mad PD\n");
2829 ret
= PTR_ERR(port_priv
->pd
);
2833 port_priv
->mr
= ib_get_dma_mr(port_priv
->pd
, IB_ACCESS_LOCAL_WRITE
);
2834 if (IS_ERR(port_priv
->mr
)) {
2835 printk(KERN_ERR PFX
"Couldn't get ib_mad DMA MR\n");
2836 ret
= PTR_ERR(port_priv
->mr
);
2841 ret
= create_mad_qp(&port_priv
->qp_info
[0], IB_QPT_SMI
);
2845 ret
= create_mad_qp(&port_priv
->qp_info
[1], IB_QPT_GSI
);
2849 snprintf(name
, sizeof name
, "ib_mad%d", port_num
);
2850 port_priv
->wq
= create_singlethread_workqueue(name
);
2851 if (!port_priv
->wq
) {
2855 INIT_WORK(&port_priv
->work
, ib_mad_completion_handler
);
2857 spin_lock_irqsave(&ib_mad_port_list_lock
, flags
);
2858 list_add_tail(&port_priv
->port_list
, &ib_mad_port_list
);
2859 spin_unlock_irqrestore(&ib_mad_port_list_lock
, flags
);
2861 ret
= ib_mad_port_start(port_priv
);
2863 printk(KERN_ERR PFX
"Couldn't start port\n");
2870 spin_lock_irqsave(&ib_mad_port_list_lock
, flags
);
2871 list_del_init(&port_priv
->port_list
);
2872 spin_unlock_irqrestore(&ib_mad_port_list_lock
, flags
);
2874 destroy_workqueue(port_priv
->wq
);
2876 destroy_mad_qp(&port_priv
->qp_info
[1]);
2878 destroy_mad_qp(&port_priv
->qp_info
[0]);
2880 ib_dereg_mr(port_priv
->mr
);
2882 ib_dealloc_pd(port_priv
->pd
);
2884 ib_destroy_cq(port_priv
->cq
);
2885 cleanup_recv_queue(&port_priv
->qp_info
[1]);
2886 cleanup_recv_queue(&port_priv
->qp_info
[0]);
2895 * If there are no classes using the port, free the port
2896 * resources (CQ, MR, PD, QP) and remove the port's info structure
2898 static int ib_mad_port_close(struct ib_device
*device
, int port_num
)
2900 struct ib_mad_port_private
*port_priv
;
2901 unsigned long flags
;
2903 spin_lock_irqsave(&ib_mad_port_list_lock
, flags
);
2904 port_priv
= __ib_get_mad_port(device
, port_num
);
2905 if (port_priv
== NULL
) {
2906 spin_unlock_irqrestore(&ib_mad_port_list_lock
, flags
);
2907 printk(KERN_ERR PFX
"Port %d not found\n", port_num
);
2910 list_del_init(&port_priv
->port_list
);
2911 spin_unlock_irqrestore(&ib_mad_port_list_lock
, flags
);
2913 destroy_workqueue(port_priv
->wq
);
2914 destroy_mad_qp(&port_priv
->qp_info
[1]);
2915 destroy_mad_qp(&port_priv
->qp_info
[0]);
2916 ib_dereg_mr(port_priv
->mr
);
2917 ib_dealloc_pd(port_priv
->pd
);
2918 ib_destroy_cq(port_priv
->cq
);
2919 cleanup_recv_queue(&port_priv
->qp_info
[1]);
2920 cleanup_recv_queue(&port_priv
->qp_info
[0]);
2921 /* XXX: Handle deallocation of MAD registration tables */
2928 static void ib_mad_init_device(struct ib_device
*device
)
2932 if (rdma_node_get_transport(device
->node_type
) != RDMA_TRANSPORT_IB
)
2935 if (device
->node_type
== RDMA_NODE_IB_SWITCH
) {
2940 end
= device
->phys_port_cnt
;
2943 for (i
= start
; i
<= end
; i
++) {
2944 if (ib_mad_port_open(device
, i
)) {
2945 printk(KERN_ERR PFX
"Couldn't open %s port %d\n",
2949 if (ib_agent_port_open(device
, i
)) {
2950 printk(KERN_ERR PFX
"Couldn't open %s port %d "
2959 if (ib_mad_port_close(device
, i
))
2960 printk(KERN_ERR PFX
"Couldn't close %s port %d\n",
2966 while (i
>= start
) {
2967 if (ib_agent_port_close(device
, i
))
2968 printk(KERN_ERR PFX
"Couldn't close %s port %d "
2971 if (ib_mad_port_close(device
, i
))
2972 printk(KERN_ERR PFX
"Couldn't close %s port %d\n",
2978 static void ib_mad_remove_device(struct ib_device
*device
)
2980 int i
, num_ports
, cur_port
;
2982 if (rdma_node_get_transport(device
->node_type
) != RDMA_TRANSPORT_IB
)
2985 if (device
->node_type
== RDMA_NODE_IB_SWITCH
) {
2989 num_ports
= device
->phys_port_cnt
;
2992 for (i
= 0; i
< num_ports
; i
++, cur_port
++) {
2993 if (ib_agent_port_close(device
, cur_port
))
2994 printk(KERN_ERR PFX
"Couldn't close %s port %d "
2996 device
->name
, cur_port
);
2997 if (ib_mad_port_close(device
, cur_port
))
2998 printk(KERN_ERR PFX
"Couldn't close %s port %d\n",
2999 device
->name
, cur_port
);
3003 static struct ib_client mad_client
= {
3005 .add
= ib_mad_init_device
,
3006 .remove
= ib_mad_remove_device
3009 static int __init
ib_mad_init_module(void)
3013 mad_recvq_size
= min(mad_recvq_size
, IB_MAD_QP_MAX_SIZE
);
3014 mad_recvq_size
= max(mad_recvq_size
, IB_MAD_QP_MIN_SIZE
);
3016 mad_sendq_size
= min(mad_sendq_size
, IB_MAD_QP_MAX_SIZE
);
3017 mad_sendq_size
= max(mad_sendq_size
, IB_MAD_QP_MIN_SIZE
);
3019 ib_mad_cache
= kmem_cache_create("ib_mad",
3020 sizeof(struct ib_mad_private
),
3024 if (!ib_mad_cache
) {
3025 printk(KERN_ERR PFX
"Couldn't create ib_mad cache\n");
3030 INIT_LIST_HEAD(&ib_mad_port_list
);
3032 if (ib_register_client(&mad_client
)) {
3033 printk(KERN_ERR PFX
"Couldn't register ib_mad client\n");
3041 kmem_cache_destroy(ib_mad_cache
);
3046 static void __exit
ib_mad_cleanup_module(void)
3048 ib_unregister_client(&mad_client
);
3049 kmem_cache_destroy(ib_mad_cache
);
3052 module_init(ib_mad_init_module
);
3053 module_exit(ib_mad_cleanup_module
);