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 <rdma/ib_cache.h>
44 MODULE_LICENSE("Dual BSD/GPL");
45 MODULE_DESCRIPTION("kernel IB MAD API");
46 MODULE_AUTHOR("Hal Rosenstock");
47 MODULE_AUTHOR("Sean Hefty");
49 int mad_sendq_size
= IB_MAD_QP_SEND_SIZE
;
50 int mad_recvq_size
= IB_MAD_QP_RECV_SIZE
;
52 module_param_named(send_queue_size
, mad_sendq_size
, int, 0444);
53 MODULE_PARM_DESC(send_queue_size
, "Size of send queue in number of work requests");
54 module_param_named(recv_queue_size
, mad_recvq_size
, int, 0444);
55 MODULE_PARM_DESC(recv_queue_size
, "Size of receive queue in number of work requests");
57 static struct kmem_cache
*ib_mad_cache
;
59 static struct list_head ib_mad_port_list
;
60 static u32 ib_mad_client_id
= 0;
63 static DEFINE_SPINLOCK(ib_mad_port_list_lock
);
65 /* Forward declarations */
66 static int method_in_use(struct ib_mad_mgmt_method_table
**method
,
67 struct ib_mad_reg_req
*mad_reg_req
);
68 static void remove_mad_reg_req(struct ib_mad_agent_private
*priv
);
69 static struct ib_mad_agent_private
*find_mad_agent(
70 struct ib_mad_port_private
*port_priv
,
72 static int ib_mad_post_receive_mads(struct ib_mad_qp_info
*qp_info
,
73 struct ib_mad_private
*mad
);
74 static void cancel_mads(struct ib_mad_agent_private
*mad_agent_priv
);
75 static void timeout_sends(struct work_struct
*work
);
76 static void local_completions(struct work_struct
*work
);
77 static int add_nonoui_reg_req(struct ib_mad_reg_req
*mad_reg_req
,
78 struct ib_mad_agent_private
*agent_priv
,
80 static int add_oui_reg_req(struct ib_mad_reg_req
*mad_reg_req
,
81 struct ib_mad_agent_private
*agent_priv
);
84 * Returns a ib_mad_port_private structure or NULL for a device/port
85 * Assumes ib_mad_port_list_lock is being held
87 static inline struct ib_mad_port_private
*
88 __ib_get_mad_port(struct ib_device
*device
, int port_num
)
90 struct ib_mad_port_private
*entry
;
92 list_for_each_entry(entry
, &ib_mad_port_list
, port_list
) {
93 if (entry
->device
== device
&& entry
->port_num
== port_num
)
100 * Wrapper function to return a ib_mad_port_private structure or NULL
103 static inline struct ib_mad_port_private
*
104 ib_get_mad_port(struct ib_device
*device
, int port_num
)
106 struct ib_mad_port_private
*entry
;
109 spin_lock_irqsave(&ib_mad_port_list_lock
, flags
);
110 entry
= __ib_get_mad_port(device
, port_num
);
111 spin_unlock_irqrestore(&ib_mad_port_list_lock
, flags
);
116 static inline u8
convert_mgmt_class(u8 mgmt_class
)
118 /* Alias IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE to 0 */
119 return mgmt_class
== IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
?
123 static int get_spl_qp_index(enum ib_qp_type qp_type
)
136 static int vendor_class_index(u8 mgmt_class
)
138 return mgmt_class
- IB_MGMT_CLASS_VENDOR_RANGE2_START
;
141 static int is_vendor_class(u8 mgmt_class
)
143 if ((mgmt_class
< IB_MGMT_CLASS_VENDOR_RANGE2_START
) ||
144 (mgmt_class
> IB_MGMT_CLASS_VENDOR_RANGE2_END
))
149 static int is_vendor_oui(char *oui
)
151 if (oui
[0] || oui
[1] || oui
[2])
156 static int is_vendor_method_in_use(
157 struct ib_mad_mgmt_vendor_class
*vendor_class
,
158 struct ib_mad_reg_req
*mad_reg_req
)
160 struct ib_mad_mgmt_method_table
*method
;
163 for (i
= 0; i
< MAX_MGMT_OUI
; i
++) {
164 if (!memcmp(vendor_class
->oui
[i
], mad_reg_req
->oui
, 3)) {
165 method
= vendor_class
->method_table
[i
];
167 if (method_in_use(&method
, mad_reg_req
))
177 int ib_response_mad(struct ib_mad
*mad
)
179 return ((mad
->mad_hdr
.method
& IB_MGMT_METHOD_RESP
) ||
180 (mad
->mad_hdr
.method
== IB_MGMT_METHOD_TRAP_REPRESS
) ||
181 ((mad
->mad_hdr
.mgmt_class
== IB_MGMT_CLASS_BM
) &&
182 (mad
->mad_hdr
.attr_mod
& IB_BM_ATTR_MOD_RESP
)));
184 EXPORT_SYMBOL(ib_response_mad
);
187 * ib_register_mad_agent - Register to send/receive MADs
189 struct ib_mad_agent
*ib_register_mad_agent(struct ib_device
*device
,
191 enum ib_qp_type qp_type
,
192 struct ib_mad_reg_req
*mad_reg_req
,
194 ib_mad_send_handler send_handler
,
195 ib_mad_recv_handler recv_handler
,
198 struct ib_mad_port_private
*port_priv
;
199 struct ib_mad_agent
*ret
= ERR_PTR(-EINVAL
);
200 struct ib_mad_agent_private
*mad_agent_priv
;
201 struct ib_mad_reg_req
*reg_req
= NULL
;
202 struct ib_mad_mgmt_class_table
*class;
203 struct ib_mad_mgmt_vendor_class_table
*vendor
;
204 struct ib_mad_mgmt_vendor_class
*vendor_class
;
205 struct ib_mad_mgmt_method_table
*method
;
208 u8 mgmt_class
, vclass
;
210 /* Validate parameters */
211 qpn
= get_spl_qp_index(qp_type
);
215 if (rmpp_version
&& rmpp_version
!= IB_MGMT_RMPP_VERSION
)
218 /* Validate MAD registration request if supplied */
220 if (mad_reg_req
->mgmt_class_version
>= MAX_MGMT_VERSION
)
224 if (mad_reg_req
->mgmt_class
>= MAX_MGMT_CLASS
) {
226 * IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE is the only
227 * one in this range currently allowed
229 if (mad_reg_req
->mgmt_class
!=
230 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
)
232 } else if (mad_reg_req
->mgmt_class
== 0) {
234 * Class 0 is reserved in IBA and is used for
235 * aliasing of IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
238 } else if (is_vendor_class(mad_reg_req
->mgmt_class
)) {
240 * If class is in "new" vendor range,
241 * ensure supplied OUI is not zero
243 if (!is_vendor_oui(mad_reg_req
->oui
))
246 /* Make sure class supplied is consistent with RMPP */
247 if (!ib_is_mad_class_rmpp(mad_reg_req
->mgmt_class
)) {
251 /* Make sure class supplied is consistent with QP type */
252 if (qp_type
== IB_QPT_SMI
) {
253 if ((mad_reg_req
->mgmt_class
!=
254 IB_MGMT_CLASS_SUBN_LID_ROUTED
) &&
255 (mad_reg_req
->mgmt_class
!=
256 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
))
259 if ((mad_reg_req
->mgmt_class
==
260 IB_MGMT_CLASS_SUBN_LID_ROUTED
) ||
261 (mad_reg_req
->mgmt_class
==
262 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
))
266 /* No registration request supplied */
271 /* Validate device and port */
272 port_priv
= ib_get_mad_port(device
, port_num
);
274 ret
= ERR_PTR(-ENODEV
);
278 /* Allocate structures */
279 mad_agent_priv
= kzalloc(sizeof *mad_agent_priv
, GFP_KERNEL
);
280 if (!mad_agent_priv
) {
281 ret
= ERR_PTR(-ENOMEM
);
285 mad_agent_priv
->agent
.mr
= ib_get_dma_mr(port_priv
->qp_info
[qpn
].qp
->pd
,
286 IB_ACCESS_LOCAL_WRITE
);
287 if (IS_ERR(mad_agent_priv
->agent
.mr
)) {
288 ret
= ERR_PTR(-ENOMEM
);
293 reg_req
= kmalloc(sizeof *reg_req
, GFP_KERNEL
);
295 ret
= ERR_PTR(-ENOMEM
);
298 /* Make a copy of the MAD registration request */
299 memcpy(reg_req
, mad_reg_req
, sizeof *reg_req
);
302 /* Now, fill in the various structures */
303 mad_agent_priv
->qp_info
= &port_priv
->qp_info
[qpn
];
304 mad_agent_priv
->reg_req
= reg_req
;
305 mad_agent_priv
->agent
.rmpp_version
= rmpp_version
;
306 mad_agent_priv
->agent
.device
= device
;
307 mad_agent_priv
->agent
.recv_handler
= recv_handler
;
308 mad_agent_priv
->agent
.send_handler
= send_handler
;
309 mad_agent_priv
->agent
.context
= context
;
310 mad_agent_priv
->agent
.qp
= port_priv
->qp_info
[qpn
].qp
;
311 mad_agent_priv
->agent
.port_num
= port_num
;
312 spin_lock_init(&mad_agent_priv
->lock
);
313 INIT_LIST_HEAD(&mad_agent_priv
->send_list
);
314 INIT_LIST_HEAD(&mad_agent_priv
->wait_list
);
315 INIT_LIST_HEAD(&mad_agent_priv
->done_list
);
316 INIT_LIST_HEAD(&mad_agent_priv
->rmpp_list
);
317 INIT_DELAYED_WORK(&mad_agent_priv
->timed_work
, timeout_sends
);
318 INIT_LIST_HEAD(&mad_agent_priv
->local_list
);
319 INIT_WORK(&mad_agent_priv
->local_work
, local_completions
);
320 atomic_set(&mad_agent_priv
->refcount
, 1);
321 init_completion(&mad_agent_priv
->comp
);
323 spin_lock_irqsave(&port_priv
->reg_lock
, flags
);
324 mad_agent_priv
->agent
.hi_tid
= ++ib_mad_client_id
;
327 * Make sure MAD registration (if supplied)
328 * is non overlapping with any existing ones
331 mgmt_class
= convert_mgmt_class(mad_reg_req
->mgmt_class
);
332 if (!is_vendor_class(mgmt_class
)) {
333 class = port_priv
->version
[mad_reg_req
->
334 mgmt_class_version
].class;
336 method
= class->method_table
[mgmt_class
];
338 if (method_in_use(&method
,
343 ret2
= add_nonoui_reg_req(mad_reg_req
, mad_agent_priv
,
346 /* "New" vendor class range */
347 vendor
= port_priv
->version
[mad_reg_req
->
348 mgmt_class_version
].vendor
;
350 vclass
= vendor_class_index(mgmt_class
);
351 vendor_class
= vendor
->vendor_class
[vclass
];
353 if (is_vendor_method_in_use(
359 ret2
= add_oui_reg_req(mad_reg_req
, mad_agent_priv
);
367 /* Add mad agent into port's agent list */
368 list_add_tail(&mad_agent_priv
->agent_list
, &port_priv
->agent_list
);
369 spin_unlock_irqrestore(&port_priv
->reg_lock
, flags
);
371 return &mad_agent_priv
->agent
;
374 spin_unlock_irqrestore(&port_priv
->reg_lock
, flags
);
377 ib_dereg_mr(mad_agent_priv
->agent
.mr
);
379 kfree(mad_agent_priv
);
383 EXPORT_SYMBOL(ib_register_mad_agent
);
385 static inline int is_snooping_sends(int mad_snoop_flags
)
387 return (mad_snoop_flags
&
388 (/*IB_MAD_SNOOP_POSTED_SENDS |
389 IB_MAD_SNOOP_RMPP_SENDS |*/
390 IB_MAD_SNOOP_SEND_COMPLETIONS
/*|
391 IB_MAD_SNOOP_RMPP_SEND_COMPLETIONS*/));
394 static inline int is_snooping_recvs(int mad_snoop_flags
)
396 return (mad_snoop_flags
&
397 (IB_MAD_SNOOP_RECVS
/*|
398 IB_MAD_SNOOP_RMPP_RECVS*/));
401 static int register_snoop_agent(struct ib_mad_qp_info
*qp_info
,
402 struct ib_mad_snoop_private
*mad_snoop_priv
)
404 struct ib_mad_snoop_private
**new_snoop_table
;
408 spin_lock_irqsave(&qp_info
->snoop_lock
, flags
);
409 /* Check for empty slot in array. */
410 for (i
= 0; i
< qp_info
->snoop_table_size
; i
++)
411 if (!qp_info
->snoop_table
[i
])
414 if (i
== qp_info
->snoop_table_size
) {
416 new_snoop_table
= krealloc(qp_info
->snoop_table
,
417 sizeof mad_snoop_priv
*
418 (qp_info
->snoop_table_size
+ 1),
420 if (!new_snoop_table
) {
425 qp_info
->snoop_table
= new_snoop_table
;
426 qp_info
->snoop_table_size
++;
428 qp_info
->snoop_table
[i
] = mad_snoop_priv
;
429 atomic_inc(&qp_info
->snoop_count
);
431 spin_unlock_irqrestore(&qp_info
->snoop_lock
, flags
);
435 struct ib_mad_agent
*ib_register_mad_snoop(struct ib_device
*device
,
437 enum ib_qp_type qp_type
,
439 ib_mad_snoop_handler snoop_handler
,
440 ib_mad_recv_handler recv_handler
,
443 struct ib_mad_port_private
*port_priv
;
444 struct ib_mad_agent
*ret
;
445 struct ib_mad_snoop_private
*mad_snoop_priv
;
448 /* Validate parameters */
449 if ((is_snooping_sends(mad_snoop_flags
) && !snoop_handler
) ||
450 (is_snooping_recvs(mad_snoop_flags
) && !recv_handler
)) {
451 ret
= ERR_PTR(-EINVAL
);
454 qpn
= get_spl_qp_index(qp_type
);
456 ret
= ERR_PTR(-EINVAL
);
459 port_priv
= ib_get_mad_port(device
, port_num
);
461 ret
= ERR_PTR(-ENODEV
);
464 /* Allocate structures */
465 mad_snoop_priv
= kzalloc(sizeof *mad_snoop_priv
, GFP_KERNEL
);
466 if (!mad_snoop_priv
) {
467 ret
= ERR_PTR(-ENOMEM
);
471 /* Now, fill in the various structures */
472 mad_snoop_priv
->qp_info
= &port_priv
->qp_info
[qpn
];
473 mad_snoop_priv
->agent
.device
= device
;
474 mad_snoop_priv
->agent
.recv_handler
= recv_handler
;
475 mad_snoop_priv
->agent
.snoop_handler
= snoop_handler
;
476 mad_snoop_priv
->agent
.context
= context
;
477 mad_snoop_priv
->agent
.qp
= port_priv
->qp_info
[qpn
].qp
;
478 mad_snoop_priv
->agent
.port_num
= port_num
;
479 mad_snoop_priv
->mad_snoop_flags
= mad_snoop_flags
;
480 init_completion(&mad_snoop_priv
->comp
);
481 mad_snoop_priv
->snoop_index
= register_snoop_agent(
482 &port_priv
->qp_info
[qpn
],
484 if (mad_snoop_priv
->snoop_index
< 0) {
485 ret
= ERR_PTR(mad_snoop_priv
->snoop_index
);
489 atomic_set(&mad_snoop_priv
->refcount
, 1);
490 return &mad_snoop_priv
->agent
;
493 kfree(mad_snoop_priv
);
497 EXPORT_SYMBOL(ib_register_mad_snoop
);
499 static inline void deref_mad_agent(struct ib_mad_agent_private
*mad_agent_priv
)
501 if (atomic_dec_and_test(&mad_agent_priv
->refcount
))
502 complete(&mad_agent_priv
->comp
);
505 static inline void deref_snoop_agent(struct ib_mad_snoop_private
*mad_snoop_priv
)
507 if (atomic_dec_and_test(&mad_snoop_priv
->refcount
))
508 complete(&mad_snoop_priv
->comp
);
511 static void unregister_mad_agent(struct ib_mad_agent_private
*mad_agent_priv
)
513 struct ib_mad_port_private
*port_priv
;
516 /* Note that we could still be handling received MADs */
519 * Canceling all sends results in dropping received response
520 * MADs, preventing us from queuing additional work
522 cancel_mads(mad_agent_priv
);
523 port_priv
= mad_agent_priv
->qp_info
->port_priv
;
524 cancel_delayed_work(&mad_agent_priv
->timed_work
);
526 spin_lock_irqsave(&port_priv
->reg_lock
, flags
);
527 remove_mad_reg_req(mad_agent_priv
);
528 list_del(&mad_agent_priv
->agent_list
);
529 spin_unlock_irqrestore(&port_priv
->reg_lock
, flags
);
531 flush_workqueue(port_priv
->wq
);
532 ib_cancel_rmpp_recvs(mad_agent_priv
);
534 deref_mad_agent(mad_agent_priv
);
535 wait_for_completion(&mad_agent_priv
->comp
);
537 kfree(mad_agent_priv
->reg_req
);
538 ib_dereg_mr(mad_agent_priv
->agent
.mr
);
539 kfree(mad_agent_priv
);
542 static void unregister_mad_snoop(struct ib_mad_snoop_private
*mad_snoop_priv
)
544 struct ib_mad_qp_info
*qp_info
;
547 qp_info
= mad_snoop_priv
->qp_info
;
548 spin_lock_irqsave(&qp_info
->snoop_lock
, flags
);
549 qp_info
->snoop_table
[mad_snoop_priv
->snoop_index
] = NULL
;
550 atomic_dec(&qp_info
->snoop_count
);
551 spin_unlock_irqrestore(&qp_info
->snoop_lock
, flags
);
553 deref_snoop_agent(mad_snoop_priv
);
554 wait_for_completion(&mad_snoop_priv
->comp
);
556 kfree(mad_snoop_priv
);
560 * ib_unregister_mad_agent - Unregisters a client from using MAD services
562 int ib_unregister_mad_agent(struct ib_mad_agent
*mad_agent
)
564 struct ib_mad_agent_private
*mad_agent_priv
;
565 struct ib_mad_snoop_private
*mad_snoop_priv
;
567 /* If the TID is zero, the agent can only snoop. */
568 if (mad_agent
->hi_tid
) {
569 mad_agent_priv
= container_of(mad_agent
,
570 struct ib_mad_agent_private
,
572 unregister_mad_agent(mad_agent_priv
);
574 mad_snoop_priv
= container_of(mad_agent
,
575 struct ib_mad_snoop_private
,
577 unregister_mad_snoop(mad_snoop_priv
);
581 EXPORT_SYMBOL(ib_unregister_mad_agent
);
583 static void dequeue_mad(struct ib_mad_list_head
*mad_list
)
585 struct ib_mad_queue
*mad_queue
;
588 BUG_ON(!mad_list
->mad_queue
);
589 mad_queue
= mad_list
->mad_queue
;
590 spin_lock_irqsave(&mad_queue
->lock
, flags
);
591 list_del(&mad_list
->list
);
593 spin_unlock_irqrestore(&mad_queue
->lock
, flags
);
596 static void snoop_send(struct ib_mad_qp_info
*qp_info
,
597 struct ib_mad_send_buf
*send_buf
,
598 struct ib_mad_send_wc
*mad_send_wc
,
601 struct ib_mad_snoop_private
*mad_snoop_priv
;
605 spin_lock_irqsave(&qp_info
->snoop_lock
, flags
);
606 for (i
= 0; i
< qp_info
->snoop_table_size
; i
++) {
607 mad_snoop_priv
= qp_info
->snoop_table
[i
];
608 if (!mad_snoop_priv
||
609 !(mad_snoop_priv
->mad_snoop_flags
& mad_snoop_flags
))
612 atomic_inc(&mad_snoop_priv
->refcount
);
613 spin_unlock_irqrestore(&qp_info
->snoop_lock
, flags
);
614 mad_snoop_priv
->agent
.snoop_handler(&mad_snoop_priv
->agent
,
615 send_buf
, mad_send_wc
);
616 deref_snoop_agent(mad_snoop_priv
);
617 spin_lock_irqsave(&qp_info
->snoop_lock
, flags
);
619 spin_unlock_irqrestore(&qp_info
->snoop_lock
, flags
);
622 static void snoop_recv(struct ib_mad_qp_info
*qp_info
,
623 struct ib_mad_recv_wc
*mad_recv_wc
,
626 struct ib_mad_snoop_private
*mad_snoop_priv
;
630 spin_lock_irqsave(&qp_info
->snoop_lock
, flags
);
631 for (i
= 0; i
< qp_info
->snoop_table_size
; i
++) {
632 mad_snoop_priv
= qp_info
->snoop_table
[i
];
633 if (!mad_snoop_priv
||
634 !(mad_snoop_priv
->mad_snoop_flags
& mad_snoop_flags
))
637 atomic_inc(&mad_snoop_priv
->refcount
);
638 spin_unlock_irqrestore(&qp_info
->snoop_lock
, flags
);
639 mad_snoop_priv
->agent
.recv_handler(&mad_snoop_priv
->agent
,
641 deref_snoop_agent(mad_snoop_priv
);
642 spin_lock_irqsave(&qp_info
->snoop_lock
, flags
);
644 spin_unlock_irqrestore(&qp_info
->snoop_lock
, flags
);
647 static void build_smp_wc(struct ib_qp
*qp
,
648 u64 wr_id
, u16 slid
, u16 pkey_index
, u8 port_num
,
651 memset(wc
, 0, sizeof *wc
);
653 wc
->status
= IB_WC_SUCCESS
;
654 wc
->opcode
= IB_WC_RECV
;
655 wc
->pkey_index
= pkey_index
;
656 wc
->byte_len
= sizeof(struct ib_mad
) + sizeof(struct ib_grh
);
661 wc
->dlid_path_bits
= 0;
662 wc
->port_num
= port_num
;
666 * Return 0 if SMP is to be sent
667 * Return 1 if SMP was consumed locally (whether or not solicited)
668 * Return < 0 if error
670 static int handle_outgoing_dr_smp(struct ib_mad_agent_private
*mad_agent_priv
,
671 struct ib_mad_send_wr_private
*mad_send_wr
)
674 struct ib_smp
*smp
= mad_send_wr
->send_buf
.mad
;
676 struct ib_mad_local_private
*local
;
677 struct ib_mad_private
*mad_priv
;
678 struct ib_mad_port_private
*port_priv
;
679 struct ib_mad_agent_private
*recv_mad_agent
= NULL
;
680 struct ib_device
*device
= mad_agent_priv
->agent
.device
;
683 struct ib_send_wr
*send_wr
= &mad_send_wr
->send_wr
;
685 if (device
->node_type
== RDMA_NODE_IB_SWITCH
&&
686 smp
->mgmt_class
== IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
)
687 port_num
= send_wr
->wr
.ud
.port_num
;
689 port_num
= mad_agent_priv
->agent
.port_num
;
692 * Directed route handling starts if the initial LID routed part of
693 * a request or the ending LID routed part of a response is empty.
694 * If we are at the start of the LID routed part, don't update the
695 * hop_ptr or hop_cnt. See section 14.2.2, Vol 1 IB spec.
697 if ((ib_get_smp_direction(smp
) ? smp
->dr_dlid
: smp
->dr_slid
) ==
699 smi_handle_dr_smp_send(smp
, device
->node_type
, port_num
) ==
702 printk(KERN_ERR PFX
"Invalid directed route\n");
706 /* Check to post send on QP or process locally */
707 if (smi_check_local_smp(smp
, device
) == IB_SMI_DISCARD
&&
708 smi_check_local_returning_smp(smp
, device
) == IB_SMI_DISCARD
)
711 local
= kmalloc(sizeof *local
, GFP_ATOMIC
);
714 printk(KERN_ERR PFX
"No memory for ib_mad_local_private\n");
717 local
->mad_priv
= NULL
;
718 local
->recv_mad_agent
= NULL
;
719 mad_priv
= kmem_cache_alloc(ib_mad_cache
, GFP_ATOMIC
);
722 printk(KERN_ERR PFX
"No memory for local response MAD\n");
727 build_smp_wc(mad_agent_priv
->agent
.qp
,
728 send_wr
->wr_id
, be16_to_cpu(smp
->dr_slid
),
729 send_wr
->wr
.ud
.pkey_index
,
730 send_wr
->wr
.ud
.port_num
, &mad_wc
);
732 /* No GRH for DR SMP */
733 ret
= device
->process_mad(device
, 0, port_num
, &mad_wc
, NULL
,
734 (struct ib_mad
*)smp
,
735 (struct ib_mad
*)&mad_priv
->mad
);
738 case IB_MAD_RESULT_SUCCESS
| IB_MAD_RESULT_REPLY
:
739 if (ib_response_mad(&mad_priv
->mad
.mad
) &&
740 mad_agent_priv
->agent
.recv_handler
) {
741 local
->mad_priv
= mad_priv
;
742 local
->recv_mad_agent
= mad_agent_priv
;
744 * Reference MAD agent until receive
745 * side of local completion handled
747 atomic_inc(&mad_agent_priv
->refcount
);
749 kmem_cache_free(ib_mad_cache
, mad_priv
);
751 case IB_MAD_RESULT_SUCCESS
| IB_MAD_RESULT_CONSUMED
:
752 kmem_cache_free(ib_mad_cache
, mad_priv
);
754 case IB_MAD_RESULT_SUCCESS
:
755 /* Treat like an incoming receive MAD */
756 port_priv
= ib_get_mad_port(mad_agent_priv
->agent
.device
,
757 mad_agent_priv
->agent
.port_num
);
759 memcpy(&mad_priv
->mad
.mad
, smp
, sizeof(struct ib_mad
));
760 recv_mad_agent
= find_mad_agent(port_priv
,
763 if (!port_priv
|| !recv_mad_agent
) {
765 * No receiving agent so drop packet and
766 * generate send completion.
768 kmem_cache_free(ib_mad_cache
, mad_priv
);
771 local
->mad_priv
= mad_priv
;
772 local
->recv_mad_agent
= recv_mad_agent
;
775 kmem_cache_free(ib_mad_cache
, mad_priv
);
781 local
->mad_send_wr
= mad_send_wr
;
782 /* Reference MAD agent until send side of local completion handled */
783 atomic_inc(&mad_agent_priv
->refcount
);
784 /* Queue local completion to local list */
785 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
786 list_add_tail(&local
->completion_list
, &mad_agent_priv
->local_list
);
787 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
788 queue_work(mad_agent_priv
->qp_info
->port_priv
->wq
,
789 &mad_agent_priv
->local_work
);
795 static int get_pad_size(int hdr_len
, int data_len
)
799 seg_size
= sizeof(struct ib_mad
) - hdr_len
;
800 if (data_len
&& seg_size
) {
801 pad
= seg_size
- data_len
% seg_size
;
802 return pad
== seg_size
? 0 : pad
;
807 static void free_send_rmpp_list(struct ib_mad_send_wr_private
*mad_send_wr
)
809 struct ib_rmpp_segment
*s
, *t
;
811 list_for_each_entry_safe(s
, t
, &mad_send_wr
->rmpp_list
, list
) {
817 static int alloc_send_rmpp_list(struct ib_mad_send_wr_private
*send_wr
,
820 struct ib_mad_send_buf
*send_buf
= &send_wr
->send_buf
;
821 struct ib_rmpp_mad
*rmpp_mad
= send_buf
->mad
;
822 struct ib_rmpp_segment
*seg
= NULL
;
823 int left
, seg_size
, pad
;
825 send_buf
->seg_size
= sizeof (struct ib_mad
) - send_buf
->hdr_len
;
826 seg_size
= send_buf
->seg_size
;
829 /* Allocate data segments. */
830 for (left
= send_buf
->data_len
+ pad
; left
> 0; left
-= seg_size
) {
831 seg
= kmalloc(sizeof (*seg
) + seg_size
, gfp_mask
);
833 printk(KERN_ERR
"alloc_send_rmpp_segs: RMPP mem "
834 "alloc failed for len %zd, gfp %#x\n",
835 sizeof (*seg
) + seg_size
, gfp_mask
);
836 free_send_rmpp_list(send_wr
);
839 seg
->num
= ++send_buf
->seg_count
;
840 list_add_tail(&seg
->list
, &send_wr
->rmpp_list
);
843 /* Zero any padding */
845 memset(seg
->data
+ seg_size
- pad
, 0, pad
);
847 rmpp_mad
->rmpp_hdr
.rmpp_version
= send_wr
->mad_agent_priv
->
849 rmpp_mad
->rmpp_hdr
.rmpp_type
= IB_MGMT_RMPP_TYPE_DATA
;
850 ib_set_rmpp_flags(&rmpp_mad
->rmpp_hdr
, IB_MGMT_RMPP_FLAG_ACTIVE
);
852 send_wr
->cur_seg
= container_of(send_wr
->rmpp_list
.next
,
853 struct ib_rmpp_segment
, list
);
854 send_wr
->last_ack_seg
= send_wr
->cur_seg
;
858 struct ib_mad_send_buf
* ib_create_send_mad(struct ib_mad_agent
*mad_agent
,
859 u32 remote_qpn
, u16 pkey_index
,
861 int hdr_len
, int data_len
,
864 struct ib_mad_agent_private
*mad_agent_priv
;
865 struct ib_mad_send_wr_private
*mad_send_wr
;
866 int pad
, message_size
, ret
, size
;
869 mad_agent_priv
= container_of(mad_agent
, struct ib_mad_agent_private
,
871 pad
= get_pad_size(hdr_len
, data_len
);
872 message_size
= hdr_len
+ data_len
+ pad
;
874 if ((!mad_agent
->rmpp_version
&&
875 (rmpp_active
|| message_size
> sizeof(struct ib_mad
))) ||
876 (!rmpp_active
&& message_size
> sizeof(struct ib_mad
)))
877 return ERR_PTR(-EINVAL
);
879 size
= rmpp_active
? hdr_len
: sizeof(struct ib_mad
);
880 buf
= kzalloc(sizeof *mad_send_wr
+ size
, gfp_mask
);
882 return ERR_PTR(-ENOMEM
);
884 mad_send_wr
= buf
+ size
;
885 INIT_LIST_HEAD(&mad_send_wr
->rmpp_list
);
886 mad_send_wr
->send_buf
.mad
= buf
;
887 mad_send_wr
->send_buf
.hdr_len
= hdr_len
;
888 mad_send_wr
->send_buf
.data_len
= data_len
;
889 mad_send_wr
->pad
= pad
;
891 mad_send_wr
->mad_agent_priv
= mad_agent_priv
;
892 mad_send_wr
->sg_list
[0].length
= hdr_len
;
893 mad_send_wr
->sg_list
[0].lkey
= mad_agent
->mr
->lkey
;
894 mad_send_wr
->sg_list
[1].length
= sizeof(struct ib_mad
) - hdr_len
;
895 mad_send_wr
->sg_list
[1].lkey
= mad_agent
->mr
->lkey
;
897 mad_send_wr
->send_wr
.wr_id
= (unsigned long) mad_send_wr
;
898 mad_send_wr
->send_wr
.sg_list
= mad_send_wr
->sg_list
;
899 mad_send_wr
->send_wr
.num_sge
= 2;
900 mad_send_wr
->send_wr
.opcode
= IB_WR_SEND
;
901 mad_send_wr
->send_wr
.send_flags
= IB_SEND_SIGNALED
;
902 mad_send_wr
->send_wr
.wr
.ud
.remote_qpn
= remote_qpn
;
903 mad_send_wr
->send_wr
.wr
.ud
.remote_qkey
= IB_QP_SET_QKEY
;
904 mad_send_wr
->send_wr
.wr
.ud
.pkey_index
= pkey_index
;
907 ret
= alloc_send_rmpp_list(mad_send_wr
, gfp_mask
);
914 mad_send_wr
->send_buf
.mad_agent
= mad_agent
;
915 atomic_inc(&mad_agent_priv
->refcount
);
916 return &mad_send_wr
->send_buf
;
918 EXPORT_SYMBOL(ib_create_send_mad
);
920 int ib_get_mad_data_offset(u8 mgmt_class
)
922 if (mgmt_class
== IB_MGMT_CLASS_SUBN_ADM
)
923 return IB_MGMT_SA_HDR
;
924 else if ((mgmt_class
== IB_MGMT_CLASS_DEVICE_MGMT
) ||
925 (mgmt_class
== IB_MGMT_CLASS_DEVICE_ADM
) ||
926 (mgmt_class
== IB_MGMT_CLASS_BIS
))
927 return IB_MGMT_DEVICE_HDR
;
928 else if ((mgmt_class
>= IB_MGMT_CLASS_VENDOR_RANGE2_START
) &&
929 (mgmt_class
<= IB_MGMT_CLASS_VENDOR_RANGE2_END
))
930 return IB_MGMT_VENDOR_HDR
;
932 return IB_MGMT_MAD_HDR
;
934 EXPORT_SYMBOL(ib_get_mad_data_offset
);
936 int ib_is_mad_class_rmpp(u8 mgmt_class
)
938 if ((mgmt_class
== IB_MGMT_CLASS_SUBN_ADM
) ||
939 (mgmt_class
== IB_MGMT_CLASS_DEVICE_MGMT
) ||
940 (mgmt_class
== IB_MGMT_CLASS_DEVICE_ADM
) ||
941 (mgmt_class
== IB_MGMT_CLASS_BIS
) ||
942 ((mgmt_class
>= IB_MGMT_CLASS_VENDOR_RANGE2_START
) &&
943 (mgmt_class
<= IB_MGMT_CLASS_VENDOR_RANGE2_END
)))
947 EXPORT_SYMBOL(ib_is_mad_class_rmpp
);
949 void *ib_get_rmpp_segment(struct ib_mad_send_buf
*send_buf
, int seg_num
)
951 struct ib_mad_send_wr_private
*mad_send_wr
;
952 struct list_head
*list
;
954 mad_send_wr
= container_of(send_buf
, struct ib_mad_send_wr_private
,
956 list
= &mad_send_wr
->cur_seg
->list
;
958 if (mad_send_wr
->cur_seg
->num
< seg_num
) {
959 list_for_each_entry(mad_send_wr
->cur_seg
, list
, list
)
960 if (mad_send_wr
->cur_seg
->num
== seg_num
)
962 } else if (mad_send_wr
->cur_seg
->num
> seg_num
) {
963 list_for_each_entry_reverse(mad_send_wr
->cur_seg
, list
, list
)
964 if (mad_send_wr
->cur_seg
->num
== seg_num
)
967 return mad_send_wr
->cur_seg
->data
;
969 EXPORT_SYMBOL(ib_get_rmpp_segment
);
971 static inline void *ib_get_payload(struct ib_mad_send_wr_private
*mad_send_wr
)
973 if (mad_send_wr
->send_buf
.seg_count
)
974 return ib_get_rmpp_segment(&mad_send_wr
->send_buf
,
975 mad_send_wr
->seg_num
);
977 return mad_send_wr
->send_buf
.mad
+
978 mad_send_wr
->send_buf
.hdr_len
;
981 void ib_free_send_mad(struct ib_mad_send_buf
*send_buf
)
983 struct ib_mad_agent_private
*mad_agent_priv
;
984 struct ib_mad_send_wr_private
*mad_send_wr
;
986 mad_agent_priv
= container_of(send_buf
->mad_agent
,
987 struct ib_mad_agent_private
, agent
);
988 mad_send_wr
= container_of(send_buf
, struct ib_mad_send_wr_private
,
991 free_send_rmpp_list(mad_send_wr
);
992 kfree(send_buf
->mad
);
993 deref_mad_agent(mad_agent_priv
);
995 EXPORT_SYMBOL(ib_free_send_mad
);
997 int ib_send_mad(struct ib_mad_send_wr_private
*mad_send_wr
)
999 struct ib_mad_qp_info
*qp_info
;
1000 struct list_head
*list
;
1001 struct ib_send_wr
*bad_send_wr
;
1002 struct ib_mad_agent
*mad_agent
;
1004 unsigned long flags
;
1007 /* Set WR ID to find mad_send_wr upon completion */
1008 qp_info
= mad_send_wr
->mad_agent_priv
->qp_info
;
1009 mad_send_wr
->send_wr
.wr_id
= (unsigned long)&mad_send_wr
->mad_list
;
1010 mad_send_wr
->mad_list
.mad_queue
= &qp_info
->send_queue
;
1012 mad_agent
= mad_send_wr
->send_buf
.mad_agent
;
1013 sge
= mad_send_wr
->sg_list
;
1014 sge
[0].addr
= ib_dma_map_single(mad_agent
->device
,
1015 mad_send_wr
->send_buf
.mad
,
1018 mad_send_wr
->header_mapping
= sge
[0].addr
;
1020 sge
[1].addr
= ib_dma_map_single(mad_agent
->device
,
1021 ib_get_payload(mad_send_wr
),
1024 mad_send_wr
->payload_mapping
= sge
[1].addr
;
1026 spin_lock_irqsave(&qp_info
->send_queue
.lock
, flags
);
1027 if (qp_info
->send_queue
.count
< qp_info
->send_queue
.max_active
) {
1028 ret
= ib_post_send(mad_agent
->qp
, &mad_send_wr
->send_wr
,
1030 list
= &qp_info
->send_queue
.list
;
1033 list
= &qp_info
->overflow_list
;
1037 qp_info
->send_queue
.count
++;
1038 list_add_tail(&mad_send_wr
->mad_list
.list
, list
);
1040 spin_unlock_irqrestore(&qp_info
->send_queue
.lock
, flags
);
1042 ib_dma_unmap_single(mad_agent
->device
,
1043 mad_send_wr
->header_mapping
,
1044 sge
[0].length
, DMA_TO_DEVICE
);
1045 ib_dma_unmap_single(mad_agent
->device
,
1046 mad_send_wr
->payload_mapping
,
1047 sge
[1].length
, DMA_TO_DEVICE
);
1053 * ib_post_send_mad - Posts MAD(s) to the send queue of the QP associated
1054 * with the registered client
1056 int ib_post_send_mad(struct ib_mad_send_buf
*send_buf
,
1057 struct ib_mad_send_buf
**bad_send_buf
)
1059 struct ib_mad_agent_private
*mad_agent_priv
;
1060 struct ib_mad_send_buf
*next_send_buf
;
1061 struct ib_mad_send_wr_private
*mad_send_wr
;
1062 unsigned long flags
;
1065 /* Walk list of send WRs and post each on send list */
1066 for (; send_buf
; send_buf
= next_send_buf
) {
1068 mad_send_wr
= container_of(send_buf
,
1069 struct ib_mad_send_wr_private
,
1071 mad_agent_priv
= mad_send_wr
->mad_agent_priv
;
1073 if (!send_buf
->mad_agent
->send_handler
||
1074 (send_buf
->timeout_ms
&&
1075 !send_buf
->mad_agent
->recv_handler
)) {
1080 if (!ib_is_mad_class_rmpp(((struct ib_mad_hdr
*) send_buf
->mad
)->mgmt_class
)) {
1081 if (mad_agent_priv
->agent
.rmpp_version
) {
1088 * Save pointer to next work request to post in case the
1089 * current one completes, and the user modifies the work
1090 * request associated with the completion
1092 next_send_buf
= send_buf
->next
;
1093 mad_send_wr
->send_wr
.wr
.ud
.ah
= send_buf
->ah
;
1095 if (((struct ib_mad_hdr
*) send_buf
->mad
)->mgmt_class
==
1096 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
) {
1097 ret
= handle_outgoing_dr_smp(mad_agent_priv
,
1099 if (ret
< 0) /* error */
1101 else if (ret
== 1) /* locally consumed */
1105 mad_send_wr
->tid
= ((struct ib_mad_hdr
*) send_buf
->mad
)->tid
;
1106 /* Timeout will be updated after send completes */
1107 mad_send_wr
->timeout
= msecs_to_jiffies(send_buf
->timeout_ms
);
1108 mad_send_wr
->max_retries
= send_buf
->retries
;
1109 mad_send_wr
->retries_left
= send_buf
->retries
;
1110 send_buf
->retries
= 0;
1111 /* Reference for work request to QP + response */
1112 mad_send_wr
->refcount
= 1 + (mad_send_wr
->timeout
> 0);
1113 mad_send_wr
->status
= IB_WC_SUCCESS
;
1115 /* Reference MAD agent until send completes */
1116 atomic_inc(&mad_agent_priv
->refcount
);
1117 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
1118 list_add_tail(&mad_send_wr
->agent_list
,
1119 &mad_agent_priv
->send_list
);
1120 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
1122 if (mad_agent_priv
->agent
.rmpp_version
) {
1123 ret
= ib_send_rmpp_mad(mad_send_wr
);
1124 if (ret
>= 0 && ret
!= IB_RMPP_RESULT_CONSUMED
)
1125 ret
= ib_send_mad(mad_send_wr
);
1127 ret
= ib_send_mad(mad_send_wr
);
1129 /* Fail send request */
1130 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
1131 list_del(&mad_send_wr
->agent_list
);
1132 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
1133 atomic_dec(&mad_agent_priv
->refcount
);
1140 *bad_send_buf
= send_buf
;
1143 EXPORT_SYMBOL(ib_post_send_mad
);
1146 * ib_free_recv_mad - Returns data buffers used to receive
1147 * a MAD to the access layer
1149 void ib_free_recv_mad(struct ib_mad_recv_wc
*mad_recv_wc
)
1151 struct ib_mad_recv_buf
*mad_recv_buf
, *temp_recv_buf
;
1152 struct ib_mad_private_header
*mad_priv_hdr
;
1153 struct ib_mad_private
*priv
;
1154 struct list_head free_list
;
1156 INIT_LIST_HEAD(&free_list
);
1157 list_splice_init(&mad_recv_wc
->rmpp_list
, &free_list
);
1159 list_for_each_entry_safe(mad_recv_buf
, temp_recv_buf
,
1161 mad_recv_wc
= container_of(mad_recv_buf
, struct ib_mad_recv_wc
,
1163 mad_priv_hdr
= container_of(mad_recv_wc
,
1164 struct ib_mad_private_header
,
1166 priv
= container_of(mad_priv_hdr
, struct ib_mad_private
,
1168 kmem_cache_free(ib_mad_cache
, priv
);
1171 EXPORT_SYMBOL(ib_free_recv_mad
);
1173 struct ib_mad_agent
*ib_redirect_mad_qp(struct ib_qp
*qp
,
1175 ib_mad_send_handler send_handler
,
1176 ib_mad_recv_handler recv_handler
,
1179 return ERR_PTR(-EINVAL
); /* XXX: for now */
1181 EXPORT_SYMBOL(ib_redirect_mad_qp
);
1183 int ib_process_mad_wc(struct ib_mad_agent
*mad_agent
,
1186 printk(KERN_ERR PFX
"ib_process_mad_wc() not implemented yet\n");
1189 EXPORT_SYMBOL(ib_process_mad_wc
);
1191 static int method_in_use(struct ib_mad_mgmt_method_table
**method
,
1192 struct ib_mad_reg_req
*mad_reg_req
)
1196 for (i
= find_first_bit(mad_reg_req
->method_mask
, IB_MGMT_MAX_METHODS
);
1197 i
< IB_MGMT_MAX_METHODS
;
1198 i
= find_next_bit(mad_reg_req
->method_mask
, IB_MGMT_MAX_METHODS
,
1200 if ((*method
)->agent
[i
]) {
1201 printk(KERN_ERR PFX
"Method %d already in use\n", i
);
1208 static int allocate_method_table(struct ib_mad_mgmt_method_table
**method
)
1210 /* Allocate management method table */
1211 *method
= kzalloc(sizeof **method
, GFP_ATOMIC
);
1213 printk(KERN_ERR PFX
"No memory for "
1214 "ib_mad_mgmt_method_table\n");
1222 * Check to see if there are any methods still in use
1224 static int check_method_table(struct ib_mad_mgmt_method_table
*method
)
1228 for (i
= 0; i
< IB_MGMT_MAX_METHODS
; i
++)
1229 if (method
->agent
[i
])
1235 * Check to see if there are any method tables for this class still in use
1237 static int check_class_table(struct ib_mad_mgmt_class_table
*class)
1241 for (i
= 0; i
< MAX_MGMT_CLASS
; i
++)
1242 if (class->method_table
[i
])
1247 static int check_vendor_class(struct ib_mad_mgmt_vendor_class
*vendor_class
)
1251 for (i
= 0; i
< MAX_MGMT_OUI
; i
++)
1252 if (vendor_class
->method_table
[i
])
1257 static int find_vendor_oui(struct ib_mad_mgmt_vendor_class
*vendor_class
,
1262 for (i
= 0; i
< MAX_MGMT_OUI
; i
++)
1263 /* Is there matching OUI for this vendor class ? */
1264 if (!memcmp(vendor_class
->oui
[i
], oui
, 3))
1270 static int check_vendor_table(struct ib_mad_mgmt_vendor_class_table
*vendor
)
1274 for (i
= 0; i
< MAX_MGMT_VENDOR_RANGE2
; i
++)
1275 if (vendor
->vendor_class
[i
])
1281 static void remove_methods_mad_agent(struct ib_mad_mgmt_method_table
*method
,
1282 struct ib_mad_agent_private
*agent
)
1286 /* Remove any methods for this mad agent */
1287 for (i
= 0; i
< IB_MGMT_MAX_METHODS
; i
++) {
1288 if (method
->agent
[i
] == agent
) {
1289 method
->agent
[i
] = NULL
;
1294 static int add_nonoui_reg_req(struct ib_mad_reg_req
*mad_reg_req
,
1295 struct ib_mad_agent_private
*agent_priv
,
1298 struct ib_mad_port_private
*port_priv
;
1299 struct ib_mad_mgmt_class_table
**class;
1300 struct ib_mad_mgmt_method_table
**method
;
1303 port_priv
= agent_priv
->qp_info
->port_priv
;
1304 class = &port_priv
->version
[mad_reg_req
->mgmt_class_version
].class;
1306 /* Allocate management class table for "new" class version */
1307 *class = kzalloc(sizeof **class, GFP_ATOMIC
);
1309 printk(KERN_ERR PFX
"No memory for "
1310 "ib_mad_mgmt_class_table\n");
1315 /* Allocate method table for this management class */
1316 method
= &(*class)->method_table
[mgmt_class
];
1317 if ((ret
= allocate_method_table(method
)))
1320 method
= &(*class)->method_table
[mgmt_class
];
1322 /* Allocate method table for this management class */
1323 if ((ret
= allocate_method_table(method
)))
1328 /* Now, make sure methods are not already in use */
1329 if (method_in_use(method
, mad_reg_req
))
1332 /* Finally, add in methods being registered */
1333 for (i
= find_first_bit(mad_reg_req
->method_mask
,
1334 IB_MGMT_MAX_METHODS
);
1335 i
< IB_MGMT_MAX_METHODS
;
1336 i
= find_next_bit(mad_reg_req
->method_mask
, IB_MGMT_MAX_METHODS
,
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 (i
= find_first_bit(mad_reg_req
->method_mask
,
1433 IB_MGMT_MAX_METHODS
);
1434 i
< IB_MGMT_MAX_METHODS
;
1435 i
= find_next_bit(mad_reg_req
->method_mask
, IB_MGMT_MAX_METHODS
,
1437 (*method
)->agent
[i
] = agent_priv
;
1442 /* Remove any methods for this mad agent */
1443 remove_methods_mad_agent(*method
, agent_priv
);
1444 /* Now, check to see if there are any methods in use */
1445 if (!check_method_table(*method
)) {
1446 /* If not, release management method table */
1453 (*vendor_table
)->vendor_class
[vclass
] = NULL
;
1454 kfree(vendor_class
);
1458 *vendor_table
= NULL
;
1465 static void remove_mad_reg_req(struct ib_mad_agent_private
*agent_priv
)
1467 struct ib_mad_port_private
*port_priv
;
1468 struct ib_mad_mgmt_class_table
*class;
1469 struct ib_mad_mgmt_method_table
*method
;
1470 struct ib_mad_mgmt_vendor_class_table
*vendor
;
1471 struct ib_mad_mgmt_vendor_class
*vendor_class
;
1476 * Was MAD registration request supplied
1477 * with original registration ?
1479 if (!agent_priv
->reg_req
) {
1483 port_priv
= agent_priv
->qp_info
->port_priv
;
1484 mgmt_class
= convert_mgmt_class(agent_priv
->reg_req
->mgmt_class
);
1485 class = port_priv
->version
[
1486 agent_priv
->reg_req
->mgmt_class_version
].class;
1490 method
= class->method_table
[mgmt_class
];
1492 /* Remove any methods for this mad agent */
1493 remove_methods_mad_agent(method
, agent_priv
);
1494 /* Now, check to see if there are any methods still in use */
1495 if (!check_method_table(method
)) {
1496 /* If not, release management method table */
1498 class->method_table
[mgmt_class
] = NULL
;
1499 /* Any management classes left ? */
1500 if (!check_class_table(class)) {
1501 /* If not, release management class table */
1504 agent_priv
->reg_req
->
1505 mgmt_class_version
].class = NULL
;
1511 if (!is_vendor_class(mgmt_class
))
1514 /* normalize mgmt_class to vendor range 2 */
1515 mgmt_class
= vendor_class_index(agent_priv
->reg_req
->mgmt_class
);
1516 vendor
= port_priv
->version
[
1517 agent_priv
->reg_req
->mgmt_class_version
].vendor
;
1522 vendor_class
= vendor
->vendor_class
[mgmt_class
];
1524 index
= find_vendor_oui(vendor_class
, agent_priv
->reg_req
->oui
);
1527 method
= vendor_class
->method_table
[index
];
1529 /* Remove any methods for this mad agent */
1530 remove_methods_mad_agent(method
, agent_priv
);
1532 * Now, check to see if there are
1533 * any methods still in use
1535 if (!check_method_table(method
)) {
1536 /* If not, release management method table */
1538 vendor_class
->method_table
[index
] = NULL
;
1539 memset(vendor_class
->oui
[index
], 0, 3);
1540 /* Any OUIs left ? */
1541 if (!check_vendor_class(vendor_class
)) {
1542 /* If not, release vendor class table */
1543 kfree(vendor_class
);
1544 vendor
->vendor_class
[mgmt_class
] = NULL
;
1545 /* Any other vendor classes left ? */
1546 if (!check_vendor_table(vendor
)) {
1549 agent_priv
->reg_req
->
1550 mgmt_class_version
].
1562 static struct ib_mad_agent_private
*
1563 find_mad_agent(struct ib_mad_port_private
*port_priv
,
1566 struct ib_mad_agent_private
*mad_agent
= NULL
;
1567 unsigned long flags
;
1569 spin_lock_irqsave(&port_priv
->reg_lock
, flags
);
1570 if (ib_response_mad(mad
)) {
1572 struct ib_mad_agent_private
*entry
;
1575 * Routing is based on high 32 bits of transaction ID
1578 hi_tid
= be64_to_cpu(mad
->mad_hdr
.tid
) >> 32;
1579 list_for_each_entry(entry
, &port_priv
->agent_list
, agent_list
) {
1580 if (entry
->agent
.hi_tid
== hi_tid
) {
1586 struct ib_mad_mgmt_class_table
*class;
1587 struct ib_mad_mgmt_method_table
*method
;
1588 struct ib_mad_mgmt_vendor_class_table
*vendor
;
1589 struct ib_mad_mgmt_vendor_class
*vendor_class
;
1590 struct ib_vendor_mad
*vendor_mad
;
1594 * Routing is based on version, class, and method
1595 * For "newer" vendor MADs, also based on OUI
1597 if (mad
->mad_hdr
.class_version
>= MAX_MGMT_VERSION
)
1599 if (!is_vendor_class(mad
->mad_hdr
.mgmt_class
)) {
1600 class = port_priv
->version
[
1601 mad
->mad_hdr
.class_version
].class;
1604 method
= class->method_table
[convert_mgmt_class(
1605 mad
->mad_hdr
.mgmt_class
)];
1607 mad_agent
= method
->agent
[mad
->mad_hdr
.method
&
1608 ~IB_MGMT_METHOD_RESP
];
1610 vendor
= port_priv
->version
[
1611 mad
->mad_hdr
.class_version
].vendor
;
1614 vendor_class
= vendor
->vendor_class
[vendor_class_index(
1615 mad
->mad_hdr
.mgmt_class
)];
1618 /* Find matching OUI */
1619 vendor_mad
= (struct ib_vendor_mad
*)mad
;
1620 index
= find_vendor_oui(vendor_class
, vendor_mad
->oui
);
1623 method
= vendor_class
->method_table
[index
];
1625 mad_agent
= method
->agent
[mad
->mad_hdr
.method
&
1626 ~IB_MGMT_METHOD_RESP
];
1632 if (mad_agent
->agent
.recv_handler
)
1633 atomic_inc(&mad_agent
->refcount
);
1635 printk(KERN_NOTICE PFX
"No receive handler for client "
1637 &mad_agent
->agent
, port_priv
->port_num
);
1642 spin_unlock_irqrestore(&port_priv
->reg_lock
, flags
);
1647 static int validate_mad(struct ib_mad
*mad
, u32 qp_num
)
1651 /* Make sure MAD base version is understood */
1652 if (mad
->mad_hdr
.base_version
!= IB_MGMT_BASE_VERSION
) {
1653 printk(KERN_ERR PFX
"MAD received with unsupported base "
1654 "version %d\n", mad
->mad_hdr
.base_version
);
1658 /* Filter SMI packets sent to other than QP0 */
1659 if ((mad
->mad_hdr
.mgmt_class
== IB_MGMT_CLASS_SUBN_LID_ROUTED
) ||
1660 (mad
->mad_hdr
.mgmt_class
== IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
)) {
1664 /* Filter GSI packets sent to QP0 */
1673 static int is_data_mad(struct ib_mad_agent_private
*mad_agent_priv
,
1674 struct ib_mad_hdr
*mad_hdr
)
1676 struct ib_rmpp_mad
*rmpp_mad
;
1678 rmpp_mad
= (struct ib_rmpp_mad
*)mad_hdr
;
1679 return !mad_agent_priv
->agent
.rmpp_version
||
1680 !(ib_get_rmpp_flags(&rmpp_mad
->rmpp_hdr
) &
1681 IB_MGMT_RMPP_FLAG_ACTIVE
) ||
1682 (rmpp_mad
->rmpp_hdr
.rmpp_type
== IB_MGMT_RMPP_TYPE_DATA
);
1685 static inline int rcv_has_same_class(struct ib_mad_send_wr_private
*wr
,
1686 struct ib_mad_recv_wc
*rwc
)
1688 return ((struct ib_mad
*)(wr
->send_buf
.mad
))->mad_hdr
.mgmt_class
==
1689 rwc
->recv_buf
.mad
->mad_hdr
.mgmt_class
;
1692 static inline int rcv_has_same_gid(struct ib_mad_agent_private
*mad_agent_priv
,
1693 struct ib_mad_send_wr_private
*wr
,
1694 struct ib_mad_recv_wc
*rwc
)
1696 struct ib_ah_attr attr
;
1697 u8 send_resp
, rcv_resp
;
1699 struct ib_device
*device
= mad_agent_priv
->agent
.device
;
1700 u8 port_num
= mad_agent_priv
->agent
.port_num
;
1703 send_resp
= ib_response_mad((struct ib_mad
*)wr
->send_buf
.mad
);
1704 rcv_resp
= ib_response_mad(rwc
->recv_buf
.mad
);
1706 if (send_resp
== rcv_resp
)
1707 /* both requests, or both responses. GIDs different */
1710 if (ib_query_ah(wr
->send_buf
.ah
, &attr
))
1711 /* Assume not equal, to avoid false positives. */
1714 if (!!(attr
.ah_flags
& IB_AH_GRH
) !=
1715 !!(rwc
->wc
->wc_flags
& IB_WC_GRH
))
1716 /* one has GID, other does not. Assume different */
1719 if (!send_resp
&& rcv_resp
) {
1720 /* is request/response. */
1721 if (!(attr
.ah_flags
& IB_AH_GRH
)) {
1722 if (ib_get_cached_lmc(device
, port_num
, &lmc
))
1724 return (!lmc
|| !((attr
.src_path_bits
^
1725 rwc
->wc
->dlid_path_bits
) &
1728 if (ib_get_cached_gid(device
, port_num
,
1729 attr
.grh
.sgid_index
, &sgid
))
1731 return !memcmp(sgid
.raw
, rwc
->recv_buf
.grh
->dgid
.raw
,
1736 if (!(attr
.ah_flags
& IB_AH_GRH
))
1737 return attr
.dlid
== rwc
->wc
->slid
;
1739 return !memcmp(attr
.grh
.dgid
.raw
, rwc
->recv_buf
.grh
->sgid
.raw
,
1743 static inline int is_direct(u8
class)
1745 return (class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
);
1748 struct ib_mad_send_wr_private
*
1749 ib_find_send_mad(struct ib_mad_agent_private
*mad_agent_priv
,
1750 struct ib_mad_recv_wc
*wc
)
1752 struct ib_mad_send_wr_private
*wr
;
1755 mad
= (struct ib_mad
*)wc
->recv_buf
.mad
;
1757 list_for_each_entry(wr
, &mad_agent_priv
->wait_list
, agent_list
) {
1758 if ((wr
->tid
== mad
->mad_hdr
.tid
) &&
1759 rcv_has_same_class(wr
, wc
) &&
1761 * Don't check GID for direct routed MADs.
1762 * These might have permissive LIDs.
1764 (is_direct(wc
->recv_buf
.mad
->mad_hdr
.mgmt_class
) ||
1765 rcv_has_same_gid(mad_agent_priv
, wr
, wc
)))
1766 return (wr
->status
== IB_WC_SUCCESS
) ? wr
: NULL
;
1770 * It's possible to receive the response before we've
1771 * been notified that the send has completed
1773 list_for_each_entry(wr
, &mad_agent_priv
->send_list
, agent_list
) {
1774 if (is_data_mad(mad_agent_priv
, wr
->send_buf
.mad
) &&
1775 wr
->tid
== mad
->mad_hdr
.tid
&&
1777 rcv_has_same_class(wr
, wc
) &&
1779 * Don't check GID for direct routed MADs.
1780 * These might have permissive LIDs.
1782 (is_direct(wc
->recv_buf
.mad
->mad_hdr
.mgmt_class
) ||
1783 rcv_has_same_gid(mad_agent_priv
, wr
, wc
)))
1784 /* Verify request has not been canceled */
1785 return (wr
->status
== IB_WC_SUCCESS
) ? wr
: NULL
;
1790 void ib_mark_mad_done(struct ib_mad_send_wr_private
*mad_send_wr
)
1792 mad_send_wr
->timeout
= 0;
1793 if (mad_send_wr
->refcount
== 1)
1794 list_move_tail(&mad_send_wr
->agent_list
,
1795 &mad_send_wr
->mad_agent_priv
->done_list
);
1798 static void ib_mad_complete_recv(struct ib_mad_agent_private
*mad_agent_priv
,
1799 struct ib_mad_recv_wc
*mad_recv_wc
)
1801 struct ib_mad_send_wr_private
*mad_send_wr
;
1802 struct ib_mad_send_wc mad_send_wc
;
1803 unsigned long flags
;
1805 INIT_LIST_HEAD(&mad_recv_wc
->rmpp_list
);
1806 list_add(&mad_recv_wc
->recv_buf
.list
, &mad_recv_wc
->rmpp_list
);
1807 if (mad_agent_priv
->agent
.rmpp_version
) {
1808 mad_recv_wc
= ib_process_rmpp_recv_wc(mad_agent_priv
,
1811 deref_mad_agent(mad_agent_priv
);
1816 /* Complete corresponding request */
1817 if (ib_response_mad(mad_recv_wc
->recv_buf
.mad
)) {
1818 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
1819 mad_send_wr
= ib_find_send_mad(mad_agent_priv
, mad_recv_wc
);
1821 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
1822 ib_free_recv_mad(mad_recv_wc
);
1823 deref_mad_agent(mad_agent_priv
);
1826 ib_mark_mad_done(mad_send_wr
);
1827 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
1829 /* Defined behavior is to complete response before request */
1830 mad_recv_wc
->wc
->wr_id
= (unsigned long) &mad_send_wr
->send_buf
;
1831 mad_agent_priv
->agent
.recv_handler(&mad_agent_priv
->agent
,
1833 atomic_dec(&mad_agent_priv
->refcount
);
1835 mad_send_wc
.status
= IB_WC_SUCCESS
;
1836 mad_send_wc
.vendor_err
= 0;
1837 mad_send_wc
.send_buf
= &mad_send_wr
->send_buf
;
1838 ib_mad_complete_send_wr(mad_send_wr
, &mad_send_wc
);
1840 mad_agent_priv
->agent
.recv_handler(&mad_agent_priv
->agent
,
1842 deref_mad_agent(mad_agent_priv
);
1846 static void ib_mad_recv_done_handler(struct ib_mad_port_private
*port_priv
,
1849 struct ib_mad_qp_info
*qp_info
;
1850 struct ib_mad_private_header
*mad_priv_hdr
;
1851 struct ib_mad_private
*recv
, *response
= NULL
;
1852 struct ib_mad_list_head
*mad_list
;
1853 struct ib_mad_agent_private
*mad_agent
;
1856 mad_list
= (struct ib_mad_list_head
*)(unsigned long)wc
->wr_id
;
1857 qp_info
= mad_list
->mad_queue
->qp_info
;
1858 dequeue_mad(mad_list
);
1860 mad_priv_hdr
= container_of(mad_list
, struct ib_mad_private_header
,
1862 recv
= container_of(mad_priv_hdr
, struct ib_mad_private
, header
);
1863 ib_dma_unmap_single(port_priv
->device
,
1864 recv
->header
.mapping
,
1865 sizeof(struct ib_mad_private
) -
1866 sizeof(struct ib_mad_private_header
),
1869 /* Setup MAD receive work completion from "normal" work completion */
1870 recv
->header
.wc
= *wc
;
1871 recv
->header
.recv_wc
.wc
= &recv
->header
.wc
;
1872 recv
->header
.recv_wc
.mad_len
= sizeof(struct ib_mad
);
1873 recv
->header
.recv_wc
.recv_buf
.mad
= &recv
->mad
.mad
;
1874 recv
->header
.recv_wc
.recv_buf
.grh
= &recv
->grh
;
1876 if (atomic_read(&qp_info
->snoop_count
))
1877 snoop_recv(qp_info
, &recv
->header
.recv_wc
, IB_MAD_SNOOP_RECVS
);
1880 if (!validate_mad(&recv
->mad
.mad
, qp_info
->qp
->qp_num
))
1883 response
= kmem_cache_alloc(ib_mad_cache
, GFP_KERNEL
);
1885 printk(KERN_ERR PFX
"ib_mad_recv_done_handler no memory "
1886 "for response buffer\n");
1890 if (port_priv
->device
->node_type
== RDMA_NODE_IB_SWITCH
)
1891 port_num
= wc
->port_num
;
1893 port_num
= port_priv
->port_num
;
1895 if (recv
->mad
.mad
.mad_hdr
.mgmt_class
==
1896 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
) {
1897 enum smi_forward_action retsmi
;
1899 if (smi_handle_dr_smp_recv(&recv
->mad
.smp
,
1900 port_priv
->device
->node_type
,
1902 port_priv
->device
->phys_port_cnt
) ==
1906 retsmi
= smi_check_forward_dr_smp(&recv
->mad
.smp
);
1907 if (retsmi
== IB_SMI_LOCAL
)
1910 if (retsmi
== IB_SMI_SEND
) { /* don't forward */
1911 if (smi_handle_dr_smp_send(&recv
->mad
.smp
,
1912 port_priv
->device
->node_type
,
1913 port_num
) == IB_SMI_DISCARD
)
1916 if (smi_check_local_smp(&recv
->mad
.smp
, port_priv
->device
) == IB_SMI_DISCARD
)
1918 } else if (port_priv
->device
->node_type
== RDMA_NODE_IB_SWITCH
) {
1919 /* forward case for switches */
1920 memcpy(response
, recv
, sizeof(*response
));
1921 response
->header
.recv_wc
.wc
= &response
->header
.wc
;
1922 response
->header
.recv_wc
.recv_buf
.mad
= &response
->mad
.mad
;
1923 response
->header
.recv_wc
.recv_buf
.grh
= &response
->grh
;
1925 agent_send_response(&response
->mad
.mad
,
1928 smi_get_fwd_port(&recv
->mad
.smp
),
1929 qp_info
->qp
->qp_num
);
1936 /* Give driver "right of first refusal" on incoming MAD */
1937 if (port_priv
->device
->process_mad
) {
1940 ret
= port_priv
->device
->process_mad(port_priv
->device
, 0,
1941 port_priv
->port_num
,
1944 &response
->mad
.mad
);
1945 if (ret
& IB_MAD_RESULT_SUCCESS
) {
1946 if (ret
& IB_MAD_RESULT_CONSUMED
)
1948 if (ret
& IB_MAD_RESULT_REPLY
) {
1949 agent_send_response(&response
->mad
.mad
,
1953 qp_info
->qp
->qp_num
);
1959 mad_agent
= find_mad_agent(port_priv
, &recv
->mad
.mad
);
1961 ib_mad_complete_recv(mad_agent
, &recv
->header
.recv_wc
);
1963 * recv is freed up in error cases in ib_mad_complete_recv
1964 * or via recv_handler in ib_mad_complete_recv()
1970 /* Post another receive request for this QP */
1972 ib_mad_post_receive_mads(qp_info
, response
);
1974 kmem_cache_free(ib_mad_cache
, recv
);
1976 ib_mad_post_receive_mads(qp_info
, recv
);
1979 static void adjust_timeout(struct ib_mad_agent_private
*mad_agent_priv
)
1981 struct ib_mad_send_wr_private
*mad_send_wr
;
1982 unsigned long delay
;
1984 if (list_empty(&mad_agent_priv
->wait_list
)) {
1985 __cancel_delayed_work(&mad_agent_priv
->timed_work
);
1987 mad_send_wr
= list_entry(mad_agent_priv
->wait_list
.next
,
1988 struct ib_mad_send_wr_private
,
1991 if (time_after(mad_agent_priv
->timeout
,
1992 mad_send_wr
->timeout
)) {
1993 mad_agent_priv
->timeout
= mad_send_wr
->timeout
;
1994 __cancel_delayed_work(&mad_agent_priv
->timed_work
);
1995 delay
= mad_send_wr
->timeout
- jiffies
;
1996 if ((long)delay
<= 0)
1998 queue_delayed_work(mad_agent_priv
->qp_info
->
2000 &mad_agent_priv
->timed_work
, delay
);
2005 static void wait_for_response(struct ib_mad_send_wr_private
*mad_send_wr
)
2007 struct ib_mad_agent_private
*mad_agent_priv
;
2008 struct ib_mad_send_wr_private
*temp_mad_send_wr
;
2009 struct list_head
*list_item
;
2010 unsigned long delay
;
2012 mad_agent_priv
= mad_send_wr
->mad_agent_priv
;
2013 list_del(&mad_send_wr
->agent_list
);
2015 delay
= mad_send_wr
->timeout
;
2016 mad_send_wr
->timeout
+= jiffies
;
2019 list_for_each_prev(list_item
, &mad_agent_priv
->wait_list
) {
2020 temp_mad_send_wr
= list_entry(list_item
,
2021 struct ib_mad_send_wr_private
,
2023 if (time_after(mad_send_wr
->timeout
,
2024 temp_mad_send_wr
->timeout
))
2029 list_item
= &mad_agent_priv
->wait_list
;
2030 list_add(&mad_send_wr
->agent_list
, list_item
);
2032 /* Reschedule a work item if we have a shorter timeout */
2033 if (mad_agent_priv
->wait_list
.next
== &mad_send_wr
->agent_list
) {
2034 __cancel_delayed_work(&mad_agent_priv
->timed_work
);
2035 queue_delayed_work(mad_agent_priv
->qp_info
->port_priv
->wq
,
2036 &mad_agent_priv
->timed_work
, delay
);
2040 void ib_reset_mad_timeout(struct ib_mad_send_wr_private
*mad_send_wr
,
2043 mad_send_wr
->timeout
= msecs_to_jiffies(timeout_ms
);
2044 wait_for_response(mad_send_wr
);
2048 * Process a send work completion
2050 void ib_mad_complete_send_wr(struct ib_mad_send_wr_private
*mad_send_wr
,
2051 struct ib_mad_send_wc
*mad_send_wc
)
2053 struct ib_mad_agent_private
*mad_agent_priv
;
2054 unsigned long flags
;
2057 mad_agent_priv
= mad_send_wr
->mad_agent_priv
;
2058 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2059 if (mad_agent_priv
->agent
.rmpp_version
) {
2060 ret
= ib_process_rmpp_send_wc(mad_send_wr
, mad_send_wc
);
2061 if (ret
== IB_RMPP_RESULT_CONSUMED
)
2064 ret
= IB_RMPP_RESULT_UNHANDLED
;
2066 if (mad_send_wc
->status
!= IB_WC_SUCCESS
&&
2067 mad_send_wr
->status
== IB_WC_SUCCESS
) {
2068 mad_send_wr
->status
= mad_send_wc
->status
;
2069 mad_send_wr
->refcount
-= (mad_send_wr
->timeout
> 0);
2072 if (--mad_send_wr
->refcount
> 0) {
2073 if (mad_send_wr
->refcount
== 1 && mad_send_wr
->timeout
&&
2074 mad_send_wr
->status
== IB_WC_SUCCESS
) {
2075 wait_for_response(mad_send_wr
);
2080 /* Remove send from MAD agent and notify client of completion */
2081 list_del(&mad_send_wr
->agent_list
);
2082 adjust_timeout(mad_agent_priv
);
2083 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2085 if (mad_send_wr
->status
!= IB_WC_SUCCESS
)
2086 mad_send_wc
->status
= mad_send_wr
->status
;
2087 if (ret
== IB_RMPP_RESULT_INTERNAL
)
2088 ib_rmpp_send_handler(mad_send_wc
);
2090 mad_agent_priv
->agent
.send_handler(&mad_agent_priv
->agent
,
2093 /* Release reference on agent taken when sending */
2094 deref_mad_agent(mad_agent_priv
);
2097 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2100 static void ib_mad_send_done_handler(struct ib_mad_port_private
*port_priv
,
2103 struct ib_mad_send_wr_private
*mad_send_wr
, *queued_send_wr
;
2104 struct ib_mad_list_head
*mad_list
;
2105 struct ib_mad_qp_info
*qp_info
;
2106 struct ib_mad_queue
*send_queue
;
2107 struct ib_send_wr
*bad_send_wr
;
2108 struct ib_mad_send_wc mad_send_wc
;
2109 unsigned long flags
;
2112 mad_list
= (struct ib_mad_list_head
*)(unsigned long)wc
->wr_id
;
2113 mad_send_wr
= container_of(mad_list
, struct ib_mad_send_wr_private
,
2115 send_queue
= mad_list
->mad_queue
;
2116 qp_info
= send_queue
->qp_info
;
2119 ib_dma_unmap_single(mad_send_wr
->send_buf
.mad_agent
->device
,
2120 mad_send_wr
->header_mapping
,
2121 mad_send_wr
->sg_list
[0].length
, DMA_TO_DEVICE
);
2122 ib_dma_unmap_single(mad_send_wr
->send_buf
.mad_agent
->device
,
2123 mad_send_wr
->payload_mapping
,
2124 mad_send_wr
->sg_list
[1].length
, DMA_TO_DEVICE
);
2125 queued_send_wr
= NULL
;
2126 spin_lock_irqsave(&send_queue
->lock
, flags
);
2127 list_del(&mad_list
->list
);
2129 /* Move queued send to the send queue */
2130 if (send_queue
->count
-- > send_queue
->max_active
) {
2131 mad_list
= container_of(qp_info
->overflow_list
.next
,
2132 struct ib_mad_list_head
, list
);
2133 queued_send_wr
= container_of(mad_list
,
2134 struct ib_mad_send_wr_private
,
2136 list_move_tail(&mad_list
->list
, &send_queue
->list
);
2138 spin_unlock_irqrestore(&send_queue
->lock
, flags
);
2140 mad_send_wc
.send_buf
= &mad_send_wr
->send_buf
;
2141 mad_send_wc
.status
= wc
->status
;
2142 mad_send_wc
.vendor_err
= wc
->vendor_err
;
2143 if (atomic_read(&qp_info
->snoop_count
))
2144 snoop_send(qp_info
, &mad_send_wr
->send_buf
, &mad_send_wc
,
2145 IB_MAD_SNOOP_SEND_COMPLETIONS
);
2146 ib_mad_complete_send_wr(mad_send_wr
, &mad_send_wc
);
2148 if (queued_send_wr
) {
2149 ret
= ib_post_send(qp_info
->qp
, &queued_send_wr
->send_wr
,
2152 printk(KERN_ERR PFX
"ib_post_send failed: %d\n", ret
);
2153 mad_send_wr
= queued_send_wr
;
2154 wc
->status
= IB_WC_LOC_QP_OP_ERR
;
2160 static void mark_sends_for_retry(struct ib_mad_qp_info
*qp_info
)
2162 struct ib_mad_send_wr_private
*mad_send_wr
;
2163 struct ib_mad_list_head
*mad_list
;
2164 unsigned long flags
;
2166 spin_lock_irqsave(&qp_info
->send_queue
.lock
, flags
);
2167 list_for_each_entry(mad_list
, &qp_info
->send_queue
.list
, list
) {
2168 mad_send_wr
= container_of(mad_list
,
2169 struct ib_mad_send_wr_private
,
2171 mad_send_wr
->retry
= 1;
2173 spin_unlock_irqrestore(&qp_info
->send_queue
.lock
, flags
);
2176 static void mad_error_handler(struct ib_mad_port_private
*port_priv
,
2179 struct ib_mad_list_head
*mad_list
;
2180 struct ib_mad_qp_info
*qp_info
;
2181 struct ib_mad_send_wr_private
*mad_send_wr
;
2184 /* Determine if failure was a send or receive */
2185 mad_list
= (struct ib_mad_list_head
*)(unsigned long)wc
->wr_id
;
2186 qp_info
= mad_list
->mad_queue
->qp_info
;
2187 if (mad_list
->mad_queue
== &qp_info
->recv_queue
)
2189 * Receive errors indicate that the QP has entered the error
2190 * state - error handling/shutdown code will cleanup
2195 * Send errors will transition the QP to SQE - move
2196 * QP to RTS and repost flushed work requests
2198 mad_send_wr
= container_of(mad_list
, struct ib_mad_send_wr_private
,
2200 if (wc
->status
== IB_WC_WR_FLUSH_ERR
) {
2201 if (mad_send_wr
->retry
) {
2203 struct ib_send_wr
*bad_send_wr
;
2205 mad_send_wr
->retry
= 0;
2206 ret
= ib_post_send(qp_info
->qp
, &mad_send_wr
->send_wr
,
2209 ib_mad_send_done_handler(port_priv
, wc
);
2211 ib_mad_send_done_handler(port_priv
, wc
);
2213 struct ib_qp_attr
*attr
;
2215 /* Transition QP to RTS and fail offending send */
2216 attr
= kmalloc(sizeof *attr
, GFP_KERNEL
);
2218 attr
->qp_state
= IB_QPS_RTS
;
2219 attr
->cur_qp_state
= IB_QPS_SQE
;
2220 ret
= ib_modify_qp(qp_info
->qp
, attr
,
2221 IB_QP_STATE
| IB_QP_CUR_STATE
);
2224 printk(KERN_ERR PFX
"mad_error_handler - "
2225 "ib_modify_qp to RTS : %d\n", ret
);
2227 mark_sends_for_retry(qp_info
);
2229 ib_mad_send_done_handler(port_priv
, wc
);
2234 * IB MAD completion callback
2236 static void ib_mad_completion_handler(struct work_struct
*work
)
2238 struct ib_mad_port_private
*port_priv
;
2241 port_priv
= container_of(work
, struct ib_mad_port_private
, work
);
2242 ib_req_notify_cq(port_priv
->cq
, IB_CQ_NEXT_COMP
);
2244 while (ib_poll_cq(port_priv
->cq
, 1, &wc
) == 1) {
2245 if (wc
.status
== IB_WC_SUCCESS
) {
2246 switch (wc
.opcode
) {
2248 ib_mad_send_done_handler(port_priv
, &wc
);
2251 ib_mad_recv_done_handler(port_priv
, &wc
);
2258 mad_error_handler(port_priv
, &wc
);
2262 static void cancel_mads(struct ib_mad_agent_private
*mad_agent_priv
)
2264 unsigned long flags
;
2265 struct ib_mad_send_wr_private
*mad_send_wr
, *temp_mad_send_wr
;
2266 struct ib_mad_send_wc mad_send_wc
;
2267 struct list_head cancel_list
;
2269 INIT_LIST_HEAD(&cancel_list
);
2271 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2272 list_for_each_entry_safe(mad_send_wr
, temp_mad_send_wr
,
2273 &mad_agent_priv
->send_list
, agent_list
) {
2274 if (mad_send_wr
->status
== IB_WC_SUCCESS
) {
2275 mad_send_wr
->status
= IB_WC_WR_FLUSH_ERR
;
2276 mad_send_wr
->refcount
-= (mad_send_wr
->timeout
> 0);
2280 /* Empty wait list to prevent receives from finding a request */
2281 list_splice_init(&mad_agent_priv
->wait_list
, &cancel_list
);
2282 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2284 /* Report all cancelled requests */
2285 mad_send_wc
.status
= IB_WC_WR_FLUSH_ERR
;
2286 mad_send_wc
.vendor_err
= 0;
2288 list_for_each_entry_safe(mad_send_wr
, temp_mad_send_wr
,
2289 &cancel_list
, agent_list
) {
2290 mad_send_wc
.send_buf
= &mad_send_wr
->send_buf
;
2291 list_del(&mad_send_wr
->agent_list
);
2292 mad_agent_priv
->agent
.send_handler(&mad_agent_priv
->agent
,
2294 atomic_dec(&mad_agent_priv
->refcount
);
2298 static struct ib_mad_send_wr_private
*
2299 find_send_wr(struct ib_mad_agent_private
*mad_agent_priv
,
2300 struct ib_mad_send_buf
*send_buf
)
2302 struct ib_mad_send_wr_private
*mad_send_wr
;
2304 list_for_each_entry(mad_send_wr
, &mad_agent_priv
->wait_list
,
2306 if (&mad_send_wr
->send_buf
== send_buf
)
2310 list_for_each_entry(mad_send_wr
, &mad_agent_priv
->send_list
,
2312 if (is_data_mad(mad_agent_priv
, mad_send_wr
->send_buf
.mad
) &&
2313 &mad_send_wr
->send_buf
== send_buf
)
2319 int ib_modify_mad(struct ib_mad_agent
*mad_agent
,
2320 struct ib_mad_send_buf
*send_buf
, u32 timeout_ms
)
2322 struct ib_mad_agent_private
*mad_agent_priv
;
2323 struct ib_mad_send_wr_private
*mad_send_wr
;
2324 unsigned long flags
;
2327 mad_agent_priv
= container_of(mad_agent
, struct ib_mad_agent_private
,
2329 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2330 mad_send_wr
= find_send_wr(mad_agent_priv
, send_buf
);
2331 if (!mad_send_wr
|| mad_send_wr
->status
!= IB_WC_SUCCESS
) {
2332 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2336 active
= (!mad_send_wr
->timeout
|| mad_send_wr
->refcount
> 1);
2338 mad_send_wr
->status
= IB_WC_WR_FLUSH_ERR
;
2339 mad_send_wr
->refcount
-= (mad_send_wr
->timeout
> 0);
2342 mad_send_wr
->send_buf
.timeout_ms
= timeout_ms
;
2344 mad_send_wr
->timeout
= msecs_to_jiffies(timeout_ms
);
2346 ib_reset_mad_timeout(mad_send_wr
, timeout_ms
);
2348 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2351 EXPORT_SYMBOL(ib_modify_mad
);
2353 void ib_cancel_mad(struct ib_mad_agent
*mad_agent
,
2354 struct ib_mad_send_buf
*send_buf
)
2356 ib_modify_mad(mad_agent
, send_buf
, 0);
2358 EXPORT_SYMBOL(ib_cancel_mad
);
2360 static void local_completions(struct work_struct
*work
)
2362 struct ib_mad_agent_private
*mad_agent_priv
;
2363 struct ib_mad_local_private
*local
;
2364 struct ib_mad_agent_private
*recv_mad_agent
;
2365 unsigned long flags
;
2368 struct ib_mad_send_wc mad_send_wc
;
2371 container_of(work
, struct ib_mad_agent_private
, local_work
);
2373 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2374 while (!list_empty(&mad_agent_priv
->local_list
)) {
2375 local
= list_entry(mad_agent_priv
->local_list
.next
,
2376 struct ib_mad_local_private
,
2378 list_del(&local
->completion_list
);
2379 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2381 if (local
->mad_priv
) {
2382 recv_mad_agent
= local
->recv_mad_agent
;
2383 if (!recv_mad_agent
) {
2384 printk(KERN_ERR PFX
"No receive MAD agent for local completion\n");
2386 goto local_send_completion
;
2390 * Defined behavior is to complete response
2393 build_smp_wc(recv_mad_agent
->agent
.qp
,
2394 (unsigned long) local
->mad_send_wr
,
2395 be16_to_cpu(IB_LID_PERMISSIVE
),
2396 0, recv_mad_agent
->agent
.port_num
, &wc
);
2398 local
->mad_priv
->header
.recv_wc
.wc
= &wc
;
2399 local
->mad_priv
->header
.recv_wc
.mad_len
=
2400 sizeof(struct ib_mad
);
2401 INIT_LIST_HEAD(&local
->mad_priv
->header
.recv_wc
.rmpp_list
);
2402 list_add(&local
->mad_priv
->header
.recv_wc
.recv_buf
.list
,
2403 &local
->mad_priv
->header
.recv_wc
.rmpp_list
);
2404 local
->mad_priv
->header
.recv_wc
.recv_buf
.grh
= NULL
;
2405 local
->mad_priv
->header
.recv_wc
.recv_buf
.mad
=
2406 &local
->mad_priv
->mad
.mad
;
2407 if (atomic_read(&recv_mad_agent
->qp_info
->snoop_count
))
2408 snoop_recv(recv_mad_agent
->qp_info
,
2409 &local
->mad_priv
->header
.recv_wc
,
2410 IB_MAD_SNOOP_RECVS
);
2411 recv_mad_agent
->agent
.recv_handler(
2412 &recv_mad_agent
->agent
,
2413 &local
->mad_priv
->header
.recv_wc
);
2414 spin_lock_irqsave(&recv_mad_agent
->lock
, flags
);
2415 atomic_dec(&recv_mad_agent
->refcount
);
2416 spin_unlock_irqrestore(&recv_mad_agent
->lock
, flags
);
2419 local_send_completion
:
2421 mad_send_wc
.status
= IB_WC_SUCCESS
;
2422 mad_send_wc
.vendor_err
= 0;
2423 mad_send_wc
.send_buf
= &local
->mad_send_wr
->send_buf
;
2424 if (atomic_read(&mad_agent_priv
->qp_info
->snoop_count
))
2425 snoop_send(mad_agent_priv
->qp_info
,
2426 &local
->mad_send_wr
->send_buf
,
2427 &mad_send_wc
, IB_MAD_SNOOP_SEND_COMPLETIONS
);
2428 mad_agent_priv
->agent
.send_handler(&mad_agent_priv
->agent
,
2431 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2432 atomic_dec(&mad_agent_priv
->refcount
);
2434 kmem_cache_free(ib_mad_cache
, local
->mad_priv
);
2437 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2440 static int retry_send(struct ib_mad_send_wr_private
*mad_send_wr
)
2444 if (!mad_send_wr
->retries_left
)
2447 mad_send_wr
->retries_left
--;
2448 mad_send_wr
->send_buf
.retries
++;
2450 mad_send_wr
->timeout
= msecs_to_jiffies(mad_send_wr
->send_buf
.timeout_ms
);
2452 if (mad_send_wr
->mad_agent_priv
->agent
.rmpp_version
) {
2453 ret
= ib_retry_rmpp(mad_send_wr
);
2455 case IB_RMPP_RESULT_UNHANDLED
:
2456 ret
= ib_send_mad(mad_send_wr
);
2458 case IB_RMPP_RESULT_CONSUMED
:
2466 ret
= ib_send_mad(mad_send_wr
);
2469 mad_send_wr
->refcount
++;
2470 list_add_tail(&mad_send_wr
->agent_list
,
2471 &mad_send_wr
->mad_agent_priv
->send_list
);
2476 static void timeout_sends(struct work_struct
*work
)
2478 struct ib_mad_agent_private
*mad_agent_priv
;
2479 struct ib_mad_send_wr_private
*mad_send_wr
;
2480 struct ib_mad_send_wc mad_send_wc
;
2481 unsigned long flags
, delay
;
2483 mad_agent_priv
= container_of(work
, struct ib_mad_agent_private
,
2485 mad_send_wc
.vendor_err
= 0;
2487 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2488 while (!list_empty(&mad_agent_priv
->wait_list
)) {
2489 mad_send_wr
= list_entry(mad_agent_priv
->wait_list
.next
,
2490 struct ib_mad_send_wr_private
,
2493 if (time_after(mad_send_wr
->timeout
, jiffies
)) {
2494 delay
= mad_send_wr
->timeout
- jiffies
;
2495 if ((long)delay
<= 0)
2497 queue_delayed_work(mad_agent_priv
->qp_info
->
2499 &mad_agent_priv
->timed_work
, delay
);
2503 list_del(&mad_send_wr
->agent_list
);
2504 if (mad_send_wr
->status
== IB_WC_SUCCESS
&&
2505 !retry_send(mad_send_wr
))
2508 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2510 if (mad_send_wr
->status
== IB_WC_SUCCESS
)
2511 mad_send_wc
.status
= IB_WC_RESP_TIMEOUT_ERR
;
2513 mad_send_wc
.status
= mad_send_wr
->status
;
2514 mad_send_wc
.send_buf
= &mad_send_wr
->send_buf
;
2515 mad_agent_priv
->agent
.send_handler(&mad_agent_priv
->agent
,
2518 atomic_dec(&mad_agent_priv
->refcount
);
2519 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2521 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2524 static void ib_mad_thread_completion_handler(struct ib_cq
*cq
, void *arg
)
2526 struct ib_mad_port_private
*port_priv
= cq
->cq_context
;
2527 unsigned long flags
;
2529 spin_lock_irqsave(&ib_mad_port_list_lock
, flags
);
2530 if (!list_empty(&port_priv
->port_list
))
2531 queue_work(port_priv
->wq
, &port_priv
->work
);
2532 spin_unlock_irqrestore(&ib_mad_port_list_lock
, flags
);
2536 * Allocate receive MADs and post receive WRs for them
2538 static int ib_mad_post_receive_mads(struct ib_mad_qp_info
*qp_info
,
2539 struct ib_mad_private
*mad
)
2541 unsigned long flags
;
2543 struct ib_mad_private
*mad_priv
;
2544 struct ib_sge sg_list
;
2545 struct ib_recv_wr recv_wr
, *bad_recv_wr
;
2546 struct ib_mad_queue
*recv_queue
= &qp_info
->recv_queue
;
2548 /* Initialize common scatter list fields */
2549 sg_list
.length
= sizeof *mad_priv
- sizeof mad_priv
->header
;
2550 sg_list
.lkey
= (*qp_info
->port_priv
->mr
).lkey
;
2552 /* Initialize common receive WR fields */
2553 recv_wr
.next
= NULL
;
2554 recv_wr
.sg_list
= &sg_list
;
2555 recv_wr
.num_sge
= 1;
2558 /* Allocate and map receive buffer */
2563 mad_priv
= kmem_cache_alloc(ib_mad_cache
, GFP_KERNEL
);
2565 printk(KERN_ERR PFX
"No memory for receive buffer\n");
2570 sg_list
.addr
= ib_dma_map_single(qp_info
->port_priv
->device
,
2573 sizeof mad_priv
->header
,
2575 mad_priv
->header
.mapping
= sg_list
.addr
;
2576 recv_wr
.wr_id
= (unsigned long)&mad_priv
->header
.mad_list
;
2577 mad_priv
->header
.mad_list
.mad_queue
= recv_queue
;
2579 /* Post receive WR */
2580 spin_lock_irqsave(&recv_queue
->lock
, flags
);
2581 post
= (++recv_queue
->count
< recv_queue
->max_active
);
2582 list_add_tail(&mad_priv
->header
.mad_list
.list
, &recv_queue
->list
);
2583 spin_unlock_irqrestore(&recv_queue
->lock
, flags
);
2584 ret
= ib_post_recv(qp_info
->qp
, &recv_wr
, &bad_recv_wr
);
2586 spin_lock_irqsave(&recv_queue
->lock
, flags
);
2587 list_del(&mad_priv
->header
.mad_list
.list
);
2588 recv_queue
->count
--;
2589 spin_unlock_irqrestore(&recv_queue
->lock
, flags
);
2590 ib_dma_unmap_single(qp_info
->port_priv
->device
,
2591 mad_priv
->header
.mapping
,
2593 sizeof mad_priv
->header
,
2595 kmem_cache_free(ib_mad_cache
, mad_priv
);
2596 printk(KERN_ERR PFX
"ib_post_recv failed: %d\n", ret
);
2605 * Return all the posted receive MADs
2607 static void cleanup_recv_queue(struct ib_mad_qp_info
*qp_info
)
2609 struct ib_mad_private_header
*mad_priv_hdr
;
2610 struct ib_mad_private
*recv
;
2611 struct ib_mad_list_head
*mad_list
;
2613 while (!list_empty(&qp_info
->recv_queue
.list
)) {
2615 mad_list
= list_entry(qp_info
->recv_queue
.list
.next
,
2616 struct ib_mad_list_head
, list
);
2617 mad_priv_hdr
= container_of(mad_list
,
2618 struct ib_mad_private_header
,
2620 recv
= container_of(mad_priv_hdr
, struct ib_mad_private
,
2623 /* Remove from posted receive MAD list */
2624 list_del(&mad_list
->list
);
2626 ib_dma_unmap_single(qp_info
->port_priv
->device
,
2627 recv
->header
.mapping
,
2628 sizeof(struct ib_mad_private
) -
2629 sizeof(struct ib_mad_private_header
),
2631 kmem_cache_free(ib_mad_cache
, recv
);
2634 qp_info
->recv_queue
.count
= 0;
2640 static int ib_mad_port_start(struct ib_mad_port_private
*port_priv
)
2643 struct ib_qp_attr
*attr
;
2646 attr
= kmalloc(sizeof *attr
, GFP_KERNEL
);
2648 printk(KERN_ERR PFX
"Couldn't kmalloc ib_qp_attr\n");
2652 for (i
= 0; i
< IB_MAD_QPS_CORE
; i
++) {
2653 qp
= port_priv
->qp_info
[i
].qp
;
2655 * PKey index for QP1 is irrelevant but
2656 * one is needed for the Reset to Init transition
2658 attr
->qp_state
= IB_QPS_INIT
;
2659 attr
->pkey_index
= 0;
2660 attr
->qkey
= (qp
->qp_num
== 0) ? 0 : IB_QP1_QKEY
;
2661 ret
= ib_modify_qp(qp
, attr
, IB_QP_STATE
|
2662 IB_QP_PKEY_INDEX
| IB_QP_QKEY
);
2664 printk(KERN_ERR PFX
"Couldn't change QP%d state to "
2665 "INIT: %d\n", i
, ret
);
2669 attr
->qp_state
= IB_QPS_RTR
;
2670 ret
= ib_modify_qp(qp
, attr
, IB_QP_STATE
);
2672 printk(KERN_ERR PFX
"Couldn't change QP%d state to "
2673 "RTR: %d\n", i
, ret
);
2677 attr
->qp_state
= IB_QPS_RTS
;
2678 attr
->sq_psn
= IB_MAD_SEND_Q_PSN
;
2679 ret
= ib_modify_qp(qp
, attr
, IB_QP_STATE
| IB_QP_SQ_PSN
);
2681 printk(KERN_ERR PFX
"Couldn't change QP%d state to "
2682 "RTS: %d\n", i
, ret
);
2687 ret
= ib_req_notify_cq(port_priv
->cq
, IB_CQ_NEXT_COMP
);
2689 printk(KERN_ERR PFX
"Failed to request completion "
2690 "notification: %d\n", ret
);
2694 for (i
= 0; i
< IB_MAD_QPS_CORE
; i
++) {
2695 ret
= ib_mad_post_receive_mads(&port_priv
->qp_info
[i
], NULL
);
2697 printk(KERN_ERR PFX
"Couldn't post receive WRs\n");
2706 static void qp_event_handler(struct ib_event
*event
, void *qp_context
)
2708 struct ib_mad_qp_info
*qp_info
= qp_context
;
2710 /* It's worse than that! He's dead, Jim! */
2711 printk(KERN_ERR PFX
"Fatal error (%d) on MAD QP (%d)\n",
2712 event
->event
, qp_info
->qp
->qp_num
);
2715 static void init_mad_queue(struct ib_mad_qp_info
*qp_info
,
2716 struct ib_mad_queue
*mad_queue
)
2718 mad_queue
->qp_info
= qp_info
;
2719 mad_queue
->count
= 0;
2720 spin_lock_init(&mad_queue
->lock
);
2721 INIT_LIST_HEAD(&mad_queue
->list
);
2724 static void init_mad_qp(struct ib_mad_port_private
*port_priv
,
2725 struct ib_mad_qp_info
*qp_info
)
2727 qp_info
->port_priv
= port_priv
;
2728 init_mad_queue(qp_info
, &qp_info
->send_queue
);
2729 init_mad_queue(qp_info
, &qp_info
->recv_queue
);
2730 INIT_LIST_HEAD(&qp_info
->overflow_list
);
2731 spin_lock_init(&qp_info
->snoop_lock
);
2732 qp_info
->snoop_table
= NULL
;
2733 qp_info
->snoop_table_size
= 0;
2734 atomic_set(&qp_info
->snoop_count
, 0);
2737 static int create_mad_qp(struct ib_mad_qp_info
*qp_info
,
2738 enum ib_qp_type qp_type
)
2740 struct ib_qp_init_attr qp_init_attr
;
2743 memset(&qp_init_attr
, 0, sizeof qp_init_attr
);
2744 qp_init_attr
.send_cq
= qp_info
->port_priv
->cq
;
2745 qp_init_attr
.recv_cq
= qp_info
->port_priv
->cq
;
2746 qp_init_attr
.sq_sig_type
= IB_SIGNAL_ALL_WR
;
2747 qp_init_attr
.cap
.max_send_wr
= mad_sendq_size
;
2748 qp_init_attr
.cap
.max_recv_wr
= mad_recvq_size
;
2749 qp_init_attr
.cap
.max_send_sge
= IB_MAD_SEND_REQ_MAX_SG
;
2750 qp_init_attr
.cap
.max_recv_sge
= IB_MAD_RECV_REQ_MAX_SG
;
2751 qp_init_attr
.qp_type
= qp_type
;
2752 qp_init_attr
.port_num
= qp_info
->port_priv
->port_num
;
2753 qp_init_attr
.qp_context
= qp_info
;
2754 qp_init_attr
.event_handler
= qp_event_handler
;
2755 qp_info
->qp
= ib_create_qp(qp_info
->port_priv
->pd
, &qp_init_attr
);
2756 if (IS_ERR(qp_info
->qp
)) {
2757 printk(KERN_ERR PFX
"Couldn't create ib_mad QP%d\n",
2758 get_spl_qp_index(qp_type
));
2759 ret
= PTR_ERR(qp_info
->qp
);
2762 /* Use minimum queue sizes unless the CQ is resized */
2763 qp_info
->send_queue
.max_active
= mad_sendq_size
;
2764 qp_info
->recv_queue
.max_active
= mad_recvq_size
;
2771 static void destroy_mad_qp(struct ib_mad_qp_info
*qp_info
)
2773 ib_destroy_qp(qp_info
->qp
);
2774 kfree(qp_info
->snoop_table
);
2779 * Create the QP, PD, MR, and CQ if needed
2781 static int ib_mad_port_open(struct ib_device
*device
,
2785 struct ib_mad_port_private
*port_priv
;
2786 unsigned long flags
;
2787 char name
[sizeof "ib_mad123"];
2789 /* Create new device info */
2790 port_priv
= kzalloc(sizeof *port_priv
, GFP_KERNEL
);
2792 printk(KERN_ERR PFX
"No memory for ib_mad_port_private\n");
2796 port_priv
->device
= device
;
2797 port_priv
->port_num
= port_num
;
2798 spin_lock_init(&port_priv
->reg_lock
);
2799 INIT_LIST_HEAD(&port_priv
->agent_list
);
2800 init_mad_qp(port_priv
, &port_priv
->qp_info
[0]);
2801 init_mad_qp(port_priv
, &port_priv
->qp_info
[1]);
2803 cq_size
= (mad_sendq_size
+ mad_recvq_size
) * 2;
2804 port_priv
->cq
= ib_create_cq(port_priv
->device
,
2805 ib_mad_thread_completion_handler
,
2806 NULL
, port_priv
, cq_size
, 0);
2807 if (IS_ERR(port_priv
->cq
)) {
2808 printk(KERN_ERR PFX
"Couldn't create ib_mad CQ\n");
2809 ret
= PTR_ERR(port_priv
->cq
);
2813 port_priv
->pd
= ib_alloc_pd(device
);
2814 if (IS_ERR(port_priv
->pd
)) {
2815 printk(KERN_ERR PFX
"Couldn't create ib_mad PD\n");
2816 ret
= PTR_ERR(port_priv
->pd
);
2820 port_priv
->mr
= ib_get_dma_mr(port_priv
->pd
, IB_ACCESS_LOCAL_WRITE
);
2821 if (IS_ERR(port_priv
->mr
)) {
2822 printk(KERN_ERR PFX
"Couldn't get ib_mad DMA MR\n");
2823 ret
= PTR_ERR(port_priv
->mr
);
2827 ret
= create_mad_qp(&port_priv
->qp_info
[0], IB_QPT_SMI
);
2830 ret
= create_mad_qp(&port_priv
->qp_info
[1], IB_QPT_GSI
);
2834 snprintf(name
, sizeof name
, "ib_mad%d", port_num
);
2835 port_priv
->wq
= create_singlethread_workqueue(name
);
2836 if (!port_priv
->wq
) {
2840 INIT_WORK(&port_priv
->work
, ib_mad_completion_handler
);
2842 spin_lock_irqsave(&ib_mad_port_list_lock
, flags
);
2843 list_add_tail(&port_priv
->port_list
, &ib_mad_port_list
);
2844 spin_unlock_irqrestore(&ib_mad_port_list_lock
, flags
);
2846 ret
= ib_mad_port_start(port_priv
);
2848 printk(KERN_ERR PFX
"Couldn't start port\n");
2855 spin_lock_irqsave(&ib_mad_port_list_lock
, flags
);
2856 list_del_init(&port_priv
->port_list
);
2857 spin_unlock_irqrestore(&ib_mad_port_list_lock
, flags
);
2859 destroy_workqueue(port_priv
->wq
);
2861 destroy_mad_qp(&port_priv
->qp_info
[1]);
2863 destroy_mad_qp(&port_priv
->qp_info
[0]);
2865 ib_dereg_mr(port_priv
->mr
);
2867 ib_dealloc_pd(port_priv
->pd
);
2869 ib_destroy_cq(port_priv
->cq
);
2870 cleanup_recv_queue(&port_priv
->qp_info
[1]);
2871 cleanup_recv_queue(&port_priv
->qp_info
[0]);
2880 * If there are no classes using the port, free the port
2881 * resources (CQ, MR, PD, QP) and remove the port's info structure
2883 static int ib_mad_port_close(struct ib_device
*device
, int port_num
)
2885 struct ib_mad_port_private
*port_priv
;
2886 unsigned long flags
;
2888 spin_lock_irqsave(&ib_mad_port_list_lock
, flags
);
2889 port_priv
= __ib_get_mad_port(device
, port_num
);
2890 if (port_priv
== NULL
) {
2891 spin_unlock_irqrestore(&ib_mad_port_list_lock
, flags
);
2892 printk(KERN_ERR PFX
"Port %d not found\n", port_num
);
2895 list_del_init(&port_priv
->port_list
);
2896 spin_unlock_irqrestore(&ib_mad_port_list_lock
, flags
);
2898 destroy_workqueue(port_priv
->wq
);
2899 destroy_mad_qp(&port_priv
->qp_info
[1]);
2900 destroy_mad_qp(&port_priv
->qp_info
[0]);
2901 ib_dereg_mr(port_priv
->mr
);
2902 ib_dealloc_pd(port_priv
->pd
);
2903 ib_destroy_cq(port_priv
->cq
);
2904 cleanup_recv_queue(&port_priv
->qp_info
[1]);
2905 cleanup_recv_queue(&port_priv
->qp_info
[0]);
2906 /* XXX: Handle deallocation of MAD registration tables */
2913 static void ib_mad_init_device(struct ib_device
*device
)
2917 if (rdma_node_get_transport(device
->node_type
) != RDMA_TRANSPORT_IB
)
2920 if (device
->node_type
== RDMA_NODE_IB_SWITCH
) {
2925 end
= device
->phys_port_cnt
;
2928 for (i
= start
; i
<= end
; i
++) {
2929 if (ib_mad_port_open(device
, i
)) {
2930 printk(KERN_ERR PFX
"Couldn't open %s port %d\n",
2934 if (ib_agent_port_open(device
, i
)) {
2935 printk(KERN_ERR PFX
"Couldn't open %s port %d "
2944 if (ib_mad_port_close(device
, i
))
2945 printk(KERN_ERR PFX
"Couldn't close %s port %d\n",
2951 while (i
>= start
) {
2952 if (ib_agent_port_close(device
, i
))
2953 printk(KERN_ERR PFX
"Couldn't close %s port %d "
2956 if (ib_mad_port_close(device
, i
))
2957 printk(KERN_ERR PFX
"Couldn't close %s port %d\n",
2963 static void ib_mad_remove_device(struct ib_device
*device
)
2965 int i
, num_ports
, cur_port
;
2967 if (device
->node_type
== RDMA_NODE_IB_SWITCH
) {
2971 num_ports
= device
->phys_port_cnt
;
2974 for (i
= 0; i
< num_ports
; i
++, cur_port
++) {
2975 if (ib_agent_port_close(device
, cur_port
))
2976 printk(KERN_ERR PFX
"Couldn't close %s port %d "
2978 device
->name
, cur_port
);
2979 if (ib_mad_port_close(device
, cur_port
))
2980 printk(KERN_ERR PFX
"Couldn't close %s port %d\n",
2981 device
->name
, cur_port
);
2985 static struct ib_client mad_client
= {
2987 .add
= ib_mad_init_device
,
2988 .remove
= ib_mad_remove_device
2991 static int __init
ib_mad_init_module(void)
2995 mad_recvq_size
= min(mad_recvq_size
, IB_MAD_QP_MAX_SIZE
);
2996 mad_recvq_size
= max(mad_recvq_size
, IB_MAD_QP_MIN_SIZE
);
2998 mad_sendq_size
= min(mad_sendq_size
, IB_MAD_QP_MAX_SIZE
);
2999 mad_sendq_size
= max(mad_sendq_size
, IB_MAD_QP_MIN_SIZE
);
3001 ib_mad_cache
= kmem_cache_create("ib_mad",
3002 sizeof(struct ib_mad_private
),
3006 if (!ib_mad_cache
) {
3007 printk(KERN_ERR PFX
"Couldn't create ib_mad cache\n");
3012 INIT_LIST_HEAD(&ib_mad_port_list
);
3014 if (ib_register_client(&mad_client
)) {
3015 printk(KERN_ERR PFX
"Couldn't register ib_mad client\n");
3023 kmem_cache_destroy(ib_mad_cache
);
3028 static void __exit
ib_mad_cleanup_module(void)
3030 ib_unregister_client(&mad_client
);
3031 kmem_cache_destroy(ib_mad_cache
);
3034 module_init(ib_mad_init_module
);
3035 module_exit(ib_mad_cleanup_module
);