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.
6 * Copyright (c) 2014 Intel Corporation. All rights reserved.
8 * This software is available to you under a choice of one of two
9 * licenses. You may choose to be licensed under the terms of the GNU
10 * General Public License (GPL) Version 2, available from the file
11 * COPYING in the main directory of this source tree, or the
12 * OpenIB.org BSD license below:
14 * Redistribution and use in source and binary forms, with or
15 * without modification, are permitted provided that the following
18 * - Redistributions of source code must retain the above
19 * copyright notice, this list of conditions and the following
22 * - Redistributions in binary form must reproduce the above
23 * copyright notice, this list of conditions and the following
24 * disclaimer in the documentation and/or other materials
25 * provided with the distribution.
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
31 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
32 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
33 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40 #include <linux/dma-mapping.h>
41 #include <linux/slab.h>
42 #include <linux/module.h>
43 #include <linux/security.h>
44 #include <rdma/ib_cache.h>
47 #include "core_priv.h"
53 static int mad_sendq_size
= IB_MAD_QP_SEND_SIZE
;
54 static int mad_recvq_size
= IB_MAD_QP_RECV_SIZE
;
56 module_param_named(send_queue_size
, mad_sendq_size
, int, 0444);
57 MODULE_PARM_DESC(send_queue_size
, "Size of send queue in number of work requests");
58 module_param_named(recv_queue_size
, mad_recvq_size
, int, 0444);
59 MODULE_PARM_DESC(recv_queue_size
, "Size of receive queue in number of work requests");
61 static struct list_head ib_mad_port_list
;
62 static atomic_t ib_mad_client_id
= ATOMIC_INIT(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
,
73 const struct ib_mad_hdr
*mad
);
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
);
84 static bool ib_mad_send_error(struct ib_mad_port_private
*port_priv
,
86 static void ib_mad_send_done(struct ib_cq
*cq
, struct ib_wc
*wc
);
89 * Returns a ib_mad_port_private structure or NULL for a device/port
90 * Assumes ib_mad_port_list_lock is being held
92 static inline struct ib_mad_port_private
*
93 __ib_get_mad_port(struct ib_device
*device
, int port_num
)
95 struct ib_mad_port_private
*entry
;
97 list_for_each_entry(entry
, &ib_mad_port_list
, port_list
) {
98 if (entry
->device
== device
&& entry
->port_num
== port_num
)
105 * Wrapper function to return a ib_mad_port_private structure or NULL
108 static inline struct ib_mad_port_private
*
109 ib_get_mad_port(struct ib_device
*device
, int port_num
)
111 struct ib_mad_port_private
*entry
;
114 spin_lock_irqsave(&ib_mad_port_list_lock
, flags
);
115 entry
= __ib_get_mad_port(device
, port_num
);
116 spin_unlock_irqrestore(&ib_mad_port_list_lock
, flags
);
121 static inline u8
convert_mgmt_class(u8 mgmt_class
)
123 /* Alias IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE to 0 */
124 return mgmt_class
== IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
?
128 static int get_spl_qp_index(enum ib_qp_type qp_type
)
141 static int vendor_class_index(u8 mgmt_class
)
143 return mgmt_class
- IB_MGMT_CLASS_VENDOR_RANGE2_START
;
146 static int is_vendor_class(u8 mgmt_class
)
148 if ((mgmt_class
< IB_MGMT_CLASS_VENDOR_RANGE2_START
) ||
149 (mgmt_class
> IB_MGMT_CLASS_VENDOR_RANGE2_END
))
154 static int is_vendor_oui(char *oui
)
156 if (oui
[0] || oui
[1] || oui
[2])
161 static int is_vendor_method_in_use(
162 struct ib_mad_mgmt_vendor_class
*vendor_class
,
163 struct ib_mad_reg_req
*mad_reg_req
)
165 struct ib_mad_mgmt_method_table
*method
;
168 for (i
= 0; i
< MAX_MGMT_OUI
; i
++) {
169 if (!memcmp(vendor_class
->oui
[i
], mad_reg_req
->oui
, 3)) {
170 method
= vendor_class
->method_table
[i
];
172 if (method_in_use(&method
, mad_reg_req
))
182 int ib_response_mad(const struct ib_mad_hdr
*hdr
)
184 return ((hdr
->method
& IB_MGMT_METHOD_RESP
) ||
185 (hdr
->method
== IB_MGMT_METHOD_TRAP_REPRESS
) ||
186 ((hdr
->mgmt_class
== IB_MGMT_CLASS_BM
) &&
187 (hdr
->attr_mod
& IB_BM_ATTR_MOD_RESP
)));
189 EXPORT_SYMBOL(ib_response_mad
);
192 * ib_register_mad_agent - Register to send/receive MADs
194 struct ib_mad_agent
*ib_register_mad_agent(struct ib_device
*device
,
196 enum ib_qp_type qp_type
,
197 struct ib_mad_reg_req
*mad_reg_req
,
199 ib_mad_send_handler send_handler
,
200 ib_mad_recv_handler recv_handler
,
202 u32 registration_flags
)
204 struct ib_mad_port_private
*port_priv
;
205 struct ib_mad_agent
*ret
= ERR_PTR(-EINVAL
);
206 struct ib_mad_agent_private
*mad_agent_priv
;
207 struct ib_mad_reg_req
*reg_req
= NULL
;
208 struct ib_mad_mgmt_class_table
*class;
209 struct ib_mad_mgmt_vendor_class_table
*vendor
;
210 struct ib_mad_mgmt_vendor_class
*vendor_class
;
211 struct ib_mad_mgmt_method_table
*method
;
214 u8 mgmt_class
, vclass
;
216 /* Validate parameters */
217 qpn
= get_spl_qp_index(qp_type
);
219 dev_notice(&device
->dev
,
220 "ib_register_mad_agent: invalid QP Type %d\n",
225 if (rmpp_version
&& rmpp_version
!= IB_MGMT_RMPP_VERSION
) {
226 dev_notice(&device
->dev
,
227 "ib_register_mad_agent: invalid RMPP Version %u\n",
232 /* Validate MAD registration request if supplied */
234 if (mad_reg_req
->mgmt_class_version
>= MAX_MGMT_VERSION
) {
235 dev_notice(&device
->dev
,
236 "ib_register_mad_agent: invalid Class Version %u\n",
237 mad_reg_req
->mgmt_class_version
);
241 dev_notice(&device
->dev
,
242 "ib_register_mad_agent: no recv_handler\n");
245 if (mad_reg_req
->mgmt_class
>= MAX_MGMT_CLASS
) {
247 * IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE is the only
248 * one in this range currently allowed
250 if (mad_reg_req
->mgmt_class
!=
251 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
) {
252 dev_notice(&device
->dev
,
253 "ib_register_mad_agent: Invalid Mgmt Class 0x%x\n",
254 mad_reg_req
->mgmt_class
);
257 } else if (mad_reg_req
->mgmt_class
== 0) {
259 * Class 0 is reserved in IBA and is used for
260 * aliasing of IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
262 dev_notice(&device
->dev
,
263 "ib_register_mad_agent: Invalid Mgmt Class 0\n");
265 } else if (is_vendor_class(mad_reg_req
->mgmt_class
)) {
267 * If class is in "new" vendor range,
268 * ensure supplied OUI is not zero
270 if (!is_vendor_oui(mad_reg_req
->oui
)) {
271 dev_notice(&device
->dev
,
272 "ib_register_mad_agent: No OUI specified for class 0x%x\n",
273 mad_reg_req
->mgmt_class
);
277 /* Make sure class supplied is consistent with RMPP */
278 if (!ib_is_mad_class_rmpp(mad_reg_req
->mgmt_class
)) {
280 dev_notice(&device
->dev
,
281 "ib_register_mad_agent: RMPP version for non-RMPP class 0x%x\n",
282 mad_reg_req
->mgmt_class
);
287 /* Make sure class supplied is consistent with QP type */
288 if (qp_type
== IB_QPT_SMI
) {
289 if ((mad_reg_req
->mgmt_class
!=
290 IB_MGMT_CLASS_SUBN_LID_ROUTED
) &&
291 (mad_reg_req
->mgmt_class
!=
292 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
)) {
293 dev_notice(&device
->dev
,
294 "ib_register_mad_agent: Invalid SM QP type: class 0x%x\n",
295 mad_reg_req
->mgmt_class
);
299 if ((mad_reg_req
->mgmt_class
==
300 IB_MGMT_CLASS_SUBN_LID_ROUTED
) ||
301 (mad_reg_req
->mgmt_class
==
302 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
)) {
303 dev_notice(&device
->dev
,
304 "ib_register_mad_agent: Invalid GS QP type: class 0x%x\n",
305 mad_reg_req
->mgmt_class
);
310 /* No registration request supplied */
313 if (registration_flags
& IB_MAD_USER_RMPP
)
317 /* Validate device and port */
318 port_priv
= ib_get_mad_port(device
, port_num
);
320 dev_notice(&device
->dev
,
321 "ib_register_mad_agent: Invalid port %d\n",
323 ret
= ERR_PTR(-ENODEV
);
327 /* Verify the QP requested is supported. For example, Ethernet devices
328 * will not have QP0 */
329 if (!port_priv
->qp_info
[qpn
].qp
) {
330 dev_notice(&device
->dev
,
331 "ib_register_mad_agent: QP %d not supported\n", qpn
);
332 ret
= ERR_PTR(-EPROTONOSUPPORT
);
336 /* Allocate structures */
337 mad_agent_priv
= kzalloc(sizeof *mad_agent_priv
, GFP_KERNEL
);
338 if (!mad_agent_priv
) {
339 ret
= ERR_PTR(-ENOMEM
);
344 reg_req
= kmemdup(mad_reg_req
, sizeof *reg_req
, GFP_KERNEL
);
346 ret
= ERR_PTR(-ENOMEM
);
351 /* Now, fill in the various structures */
352 mad_agent_priv
->qp_info
= &port_priv
->qp_info
[qpn
];
353 mad_agent_priv
->reg_req
= reg_req
;
354 mad_agent_priv
->agent
.rmpp_version
= rmpp_version
;
355 mad_agent_priv
->agent
.device
= device
;
356 mad_agent_priv
->agent
.recv_handler
= recv_handler
;
357 mad_agent_priv
->agent
.send_handler
= send_handler
;
358 mad_agent_priv
->agent
.context
= context
;
359 mad_agent_priv
->agent
.qp
= port_priv
->qp_info
[qpn
].qp
;
360 mad_agent_priv
->agent
.port_num
= port_num
;
361 mad_agent_priv
->agent
.flags
= registration_flags
;
362 spin_lock_init(&mad_agent_priv
->lock
);
363 INIT_LIST_HEAD(&mad_agent_priv
->send_list
);
364 INIT_LIST_HEAD(&mad_agent_priv
->wait_list
);
365 INIT_LIST_HEAD(&mad_agent_priv
->done_list
);
366 INIT_LIST_HEAD(&mad_agent_priv
->rmpp_list
);
367 INIT_DELAYED_WORK(&mad_agent_priv
->timed_work
, timeout_sends
);
368 INIT_LIST_HEAD(&mad_agent_priv
->local_list
);
369 INIT_WORK(&mad_agent_priv
->local_work
, local_completions
);
370 atomic_set(&mad_agent_priv
->refcount
, 1);
371 init_completion(&mad_agent_priv
->comp
);
373 ret2
= ib_mad_agent_security_setup(&mad_agent_priv
->agent
, qp_type
);
379 spin_lock_irqsave(&port_priv
->reg_lock
, flags
);
380 mad_agent_priv
->agent
.hi_tid
= atomic_inc_return(&ib_mad_client_id
);
383 * Make sure MAD registration (if supplied)
384 * is non overlapping with any existing ones
387 mgmt_class
= convert_mgmt_class(mad_reg_req
->mgmt_class
);
388 if (!is_vendor_class(mgmt_class
)) {
389 class = port_priv
->version
[mad_reg_req
->
390 mgmt_class_version
].class;
392 method
= class->method_table
[mgmt_class
];
394 if (method_in_use(&method
,
399 ret2
= add_nonoui_reg_req(mad_reg_req
, mad_agent_priv
,
402 /* "New" vendor class range */
403 vendor
= port_priv
->version
[mad_reg_req
->
404 mgmt_class_version
].vendor
;
406 vclass
= vendor_class_index(mgmt_class
);
407 vendor_class
= vendor
->vendor_class
[vclass
];
409 if (is_vendor_method_in_use(
415 ret2
= add_oui_reg_req(mad_reg_req
, mad_agent_priv
);
423 /* Add mad agent into port's agent list */
424 list_add_tail(&mad_agent_priv
->agent_list
, &port_priv
->agent_list
);
425 spin_unlock_irqrestore(&port_priv
->reg_lock
, flags
);
427 return &mad_agent_priv
->agent
;
429 spin_unlock_irqrestore(&port_priv
->reg_lock
, flags
);
430 ib_mad_agent_security_cleanup(&mad_agent_priv
->agent
);
434 kfree(mad_agent_priv
);
438 EXPORT_SYMBOL(ib_register_mad_agent
);
440 static inline int is_snooping_sends(int mad_snoop_flags
)
442 return (mad_snoop_flags
&
443 (/*IB_MAD_SNOOP_POSTED_SENDS |
444 IB_MAD_SNOOP_RMPP_SENDS |*/
445 IB_MAD_SNOOP_SEND_COMPLETIONS
/*|
446 IB_MAD_SNOOP_RMPP_SEND_COMPLETIONS*/));
449 static inline int is_snooping_recvs(int mad_snoop_flags
)
451 return (mad_snoop_flags
&
452 (IB_MAD_SNOOP_RECVS
/*|
453 IB_MAD_SNOOP_RMPP_RECVS*/));
456 static int register_snoop_agent(struct ib_mad_qp_info
*qp_info
,
457 struct ib_mad_snoop_private
*mad_snoop_priv
)
459 struct ib_mad_snoop_private
**new_snoop_table
;
463 spin_lock_irqsave(&qp_info
->snoop_lock
, flags
);
464 /* Check for empty slot in array. */
465 for (i
= 0; i
< qp_info
->snoop_table_size
; i
++)
466 if (!qp_info
->snoop_table
[i
])
469 if (i
== qp_info
->snoop_table_size
) {
471 new_snoop_table
= krealloc(qp_info
->snoop_table
,
472 sizeof mad_snoop_priv
*
473 (qp_info
->snoop_table_size
+ 1),
475 if (!new_snoop_table
) {
480 qp_info
->snoop_table
= new_snoop_table
;
481 qp_info
->snoop_table_size
++;
483 qp_info
->snoop_table
[i
] = mad_snoop_priv
;
484 atomic_inc(&qp_info
->snoop_count
);
486 spin_unlock_irqrestore(&qp_info
->snoop_lock
, flags
);
490 struct ib_mad_agent
*ib_register_mad_snoop(struct ib_device
*device
,
492 enum ib_qp_type qp_type
,
494 ib_mad_snoop_handler snoop_handler
,
495 ib_mad_recv_handler recv_handler
,
498 struct ib_mad_port_private
*port_priv
;
499 struct ib_mad_agent
*ret
;
500 struct ib_mad_snoop_private
*mad_snoop_priv
;
504 /* Validate parameters */
505 if ((is_snooping_sends(mad_snoop_flags
) && !snoop_handler
) ||
506 (is_snooping_recvs(mad_snoop_flags
) && !recv_handler
)) {
507 ret
= ERR_PTR(-EINVAL
);
510 qpn
= get_spl_qp_index(qp_type
);
512 ret
= ERR_PTR(-EINVAL
);
515 port_priv
= ib_get_mad_port(device
, port_num
);
517 ret
= ERR_PTR(-ENODEV
);
520 /* Allocate structures */
521 mad_snoop_priv
= kzalloc(sizeof *mad_snoop_priv
, GFP_KERNEL
);
522 if (!mad_snoop_priv
) {
523 ret
= ERR_PTR(-ENOMEM
);
527 /* Now, fill in the various structures */
528 mad_snoop_priv
->qp_info
= &port_priv
->qp_info
[qpn
];
529 mad_snoop_priv
->agent
.device
= device
;
530 mad_snoop_priv
->agent
.recv_handler
= recv_handler
;
531 mad_snoop_priv
->agent
.snoop_handler
= snoop_handler
;
532 mad_snoop_priv
->agent
.context
= context
;
533 mad_snoop_priv
->agent
.qp
= port_priv
->qp_info
[qpn
].qp
;
534 mad_snoop_priv
->agent
.port_num
= port_num
;
535 mad_snoop_priv
->mad_snoop_flags
= mad_snoop_flags
;
536 init_completion(&mad_snoop_priv
->comp
);
538 err
= ib_mad_agent_security_setup(&mad_snoop_priv
->agent
, qp_type
);
544 mad_snoop_priv
->snoop_index
= register_snoop_agent(
545 &port_priv
->qp_info
[qpn
],
547 if (mad_snoop_priv
->snoop_index
< 0) {
548 ret
= ERR_PTR(mad_snoop_priv
->snoop_index
);
552 atomic_set(&mad_snoop_priv
->refcount
, 1);
553 return &mad_snoop_priv
->agent
;
555 ib_mad_agent_security_cleanup(&mad_snoop_priv
->agent
);
557 kfree(mad_snoop_priv
);
561 EXPORT_SYMBOL(ib_register_mad_snoop
);
563 static inline void deref_mad_agent(struct ib_mad_agent_private
*mad_agent_priv
)
565 if (atomic_dec_and_test(&mad_agent_priv
->refcount
))
566 complete(&mad_agent_priv
->comp
);
569 static inline void deref_snoop_agent(struct ib_mad_snoop_private
*mad_snoop_priv
)
571 if (atomic_dec_and_test(&mad_snoop_priv
->refcount
))
572 complete(&mad_snoop_priv
->comp
);
575 static void unregister_mad_agent(struct ib_mad_agent_private
*mad_agent_priv
)
577 struct ib_mad_port_private
*port_priv
;
580 /* Note that we could still be handling received MADs */
583 * Canceling all sends results in dropping received response
584 * MADs, preventing us from queuing additional work
586 cancel_mads(mad_agent_priv
);
587 port_priv
= mad_agent_priv
->qp_info
->port_priv
;
588 cancel_delayed_work(&mad_agent_priv
->timed_work
);
590 spin_lock_irqsave(&port_priv
->reg_lock
, flags
);
591 remove_mad_reg_req(mad_agent_priv
);
592 list_del(&mad_agent_priv
->agent_list
);
593 spin_unlock_irqrestore(&port_priv
->reg_lock
, flags
);
595 flush_workqueue(port_priv
->wq
);
596 ib_cancel_rmpp_recvs(mad_agent_priv
);
598 deref_mad_agent(mad_agent_priv
);
599 wait_for_completion(&mad_agent_priv
->comp
);
601 ib_mad_agent_security_cleanup(&mad_agent_priv
->agent
);
603 kfree(mad_agent_priv
->reg_req
);
604 kfree(mad_agent_priv
);
607 static void unregister_mad_snoop(struct ib_mad_snoop_private
*mad_snoop_priv
)
609 struct ib_mad_qp_info
*qp_info
;
612 qp_info
= mad_snoop_priv
->qp_info
;
613 spin_lock_irqsave(&qp_info
->snoop_lock
, flags
);
614 qp_info
->snoop_table
[mad_snoop_priv
->snoop_index
] = NULL
;
615 atomic_dec(&qp_info
->snoop_count
);
616 spin_unlock_irqrestore(&qp_info
->snoop_lock
, flags
);
618 deref_snoop_agent(mad_snoop_priv
);
619 wait_for_completion(&mad_snoop_priv
->comp
);
621 ib_mad_agent_security_cleanup(&mad_snoop_priv
->agent
);
623 kfree(mad_snoop_priv
);
627 * ib_unregister_mad_agent - Unregisters a client from using MAD services
629 void ib_unregister_mad_agent(struct ib_mad_agent
*mad_agent
)
631 struct ib_mad_agent_private
*mad_agent_priv
;
632 struct ib_mad_snoop_private
*mad_snoop_priv
;
634 /* If the TID is zero, the agent can only snoop. */
635 if (mad_agent
->hi_tid
) {
636 mad_agent_priv
= container_of(mad_agent
,
637 struct ib_mad_agent_private
,
639 unregister_mad_agent(mad_agent_priv
);
641 mad_snoop_priv
= container_of(mad_agent
,
642 struct ib_mad_snoop_private
,
644 unregister_mad_snoop(mad_snoop_priv
);
647 EXPORT_SYMBOL(ib_unregister_mad_agent
);
649 static void dequeue_mad(struct ib_mad_list_head
*mad_list
)
651 struct ib_mad_queue
*mad_queue
;
654 mad_queue
= mad_list
->mad_queue
;
655 spin_lock_irqsave(&mad_queue
->lock
, flags
);
656 list_del(&mad_list
->list
);
658 spin_unlock_irqrestore(&mad_queue
->lock
, flags
);
661 static void snoop_send(struct ib_mad_qp_info
*qp_info
,
662 struct ib_mad_send_buf
*send_buf
,
663 struct ib_mad_send_wc
*mad_send_wc
,
666 struct ib_mad_snoop_private
*mad_snoop_priv
;
670 spin_lock_irqsave(&qp_info
->snoop_lock
, flags
);
671 for (i
= 0; i
< qp_info
->snoop_table_size
; i
++) {
672 mad_snoop_priv
= qp_info
->snoop_table
[i
];
673 if (!mad_snoop_priv
||
674 !(mad_snoop_priv
->mad_snoop_flags
& mad_snoop_flags
))
677 atomic_inc(&mad_snoop_priv
->refcount
);
678 spin_unlock_irqrestore(&qp_info
->snoop_lock
, flags
);
679 mad_snoop_priv
->agent
.snoop_handler(&mad_snoop_priv
->agent
,
680 send_buf
, mad_send_wc
);
681 deref_snoop_agent(mad_snoop_priv
);
682 spin_lock_irqsave(&qp_info
->snoop_lock
, flags
);
684 spin_unlock_irqrestore(&qp_info
->snoop_lock
, flags
);
687 static void snoop_recv(struct ib_mad_qp_info
*qp_info
,
688 struct ib_mad_recv_wc
*mad_recv_wc
,
691 struct ib_mad_snoop_private
*mad_snoop_priv
;
695 spin_lock_irqsave(&qp_info
->snoop_lock
, flags
);
696 for (i
= 0; i
< qp_info
->snoop_table_size
; i
++) {
697 mad_snoop_priv
= qp_info
->snoop_table
[i
];
698 if (!mad_snoop_priv
||
699 !(mad_snoop_priv
->mad_snoop_flags
& mad_snoop_flags
))
702 atomic_inc(&mad_snoop_priv
->refcount
);
703 spin_unlock_irqrestore(&qp_info
->snoop_lock
, flags
);
704 mad_snoop_priv
->agent
.recv_handler(&mad_snoop_priv
->agent
, NULL
,
706 deref_snoop_agent(mad_snoop_priv
);
707 spin_lock_irqsave(&qp_info
->snoop_lock
, flags
);
709 spin_unlock_irqrestore(&qp_info
->snoop_lock
, flags
);
712 static void build_smp_wc(struct ib_qp
*qp
, struct ib_cqe
*cqe
, u16 slid
,
713 u16 pkey_index
, u8 port_num
, struct ib_wc
*wc
)
715 memset(wc
, 0, sizeof *wc
);
717 wc
->status
= IB_WC_SUCCESS
;
718 wc
->opcode
= IB_WC_RECV
;
719 wc
->pkey_index
= pkey_index
;
720 wc
->byte_len
= sizeof(struct ib_mad
) + sizeof(struct ib_grh
);
725 wc
->dlid_path_bits
= 0;
726 wc
->port_num
= port_num
;
729 static size_t mad_priv_size(const struct ib_mad_private
*mp
)
731 return sizeof(struct ib_mad_private
) + mp
->mad_size
;
734 static struct ib_mad_private
*alloc_mad_private(size_t mad_size
, gfp_t flags
)
736 size_t size
= sizeof(struct ib_mad_private
) + mad_size
;
737 struct ib_mad_private
*ret
= kzalloc(size
, flags
);
740 ret
->mad_size
= mad_size
;
745 static size_t port_mad_size(const struct ib_mad_port_private
*port_priv
)
747 return rdma_max_mad_size(port_priv
->device
, port_priv
->port_num
);
750 static size_t mad_priv_dma_size(const struct ib_mad_private
*mp
)
752 return sizeof(struct ib_grh
) + mp
->mad_size
;
756 * Return 0 if SMP is to be sent
757 * Return 1 if SMP was consumed locally (whether or not solicited)
758 * Return < 0 if error
760 static int handle_outgoing_dr_smp(struct ib_mad_agent_private
*mad_agent_priv
,
761 struct ib_mad_send_wr_private
*mad_send_wr
)
764 struct ib_smp
*smp
= mad_send_wr
->send_buf
.mad
;
765 struct opa_smp
*opa_smp
= (struct opa_smp
*)smp
;
767 struct ib_mad_local_private
*local
;
768 struct ib_mad_private
*mad_priv
;
769 struct ib_mad_port_private
*port_priv
;
770 struct ib_mad_agent_private
*recv_mad_agent
= NULL
;
771 struct ib_device
*device
= mad_agent_priv
->agent
.device
;
774 struct ib_ud_wr
*send_wr
= &mad_send_wr
->send_wr
;
775 size_t mad_size
= port_mad_size(mad_agent_priv
->qp_info
->port_priv
);
776 u16 out_mad_pkey_index
= 0;
778 bool opa
= rdma_cap_opa_mad(mad_agent_priv
->qp_info
->port_priv
->device
,
779 mad_agent_priv
->qp_info
->port_priv
->port_num
);
781 if (rdma_cap_ib_switch(device
) &&
782 smp
->mgmt_class
== IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
)
783 port_num
= send_wr
->port_num
;
785 port_num
= mad_agent_priv
->agent
.port_num
;
788 * Directed route handling starts if the initial LID routed part of
789 * a request or the ending LID routed part of a response is empty.
790 * If we are at the start of the LID routed part, don't update the
791 * hop_ptr or hop_cnt. See section 14.2.2, Vol 1 IB spec.
793 if (opa
&& smp
->class_version
== OPA_SM_CLASS_VERSION
) {
796 if ((opa_get_smp_direction(opa_smp
)
797 ? opa_smp
->route
.dr
.dr_dlid
: opa_smp
->route
.dr
.dr_slid
) ==
798 OPA_LID_PERMISSIVE
&&
799 opa_smi_handle_dr_smp_send(opa_smp
,
800 rdma_cap_ib_switch(device
),
801 port_num
) == IB_SMI_DISCARD
) {
803 dev_err(&device
->dev
, "OPA Invalid directed route\n");
806 opa_drslid
= be32_to_cpu(opa_smp
->route
.dr
.dr_slid
);
807 if (opa_drslid
!= be32_to_cpu(OPA_LID_PERMISSIVE
) &&
808 opa_drslid
& 0xffff0000) {
810 dev_err(&device
->dev
, "OPA Invalid dr_slid 0x%x\n",
814 drslid
= (u16
)(opa_drslid
& 0x0000ffff);
816 /* Check to post send on QP or process locally */
817 if (opa_smi_check_local_smp(opa_smp
, device
) == IB_SMI_DISCARD
&&
818 opa_smi_check_local_returning_smp(opa_smp
, device
) == IB_SMI_DISCARD
)
821 if ((ib_get_smp_direction(smp
) ? smp
->dr_dlid
: smp
->dr_slid
) ==
823 smi_handle_dr_smp_send(smp
, rdma_cap_ib_switch(device
), port_num
) ==
826 dev_err(&device
->dev
, "Invalid directed route\n");
829 drslid
= be16_to_cpu(smp
->dr_slid
);
831 /* Check to post send on QP or process locally */
832 if (smi_check_local_smp(smp
, device
) == IB_SMI_DISCARD
&&
833 smi_check_local_returning_smp(smp
, device
) == IB_SMI_DISCARD
)
837 local
= kmalloc(sizeof *local
, GFP_ATOMIC
);
842 local
->mad_priv
= NULL
;
843 local
->recv_mad_agent
= NULL
;
844 mad_priv
= alloc_mad_private(mad_size
, GFP_ATOMIC
);
851 build_smp_wc(mad_agent_priv
->agent
.qp
,
852 send_wr
->wr
.wr_cqe
, drslid
,
854 send_wr
->port_num
, &mad_wc
);
856 if (opa
&& smp
->base_version
== OPA_MGMT_BASE_VERSION
) {
857 mad_wc
.byte_len
= mad_send_wr
->send_buf
.hdr_len
858 + mad_send_wr
->send_buf
.data_len
859 + sizeof(struct ib_grh
);
862 /* No GRH for DR SMP */
863 ret
= device
->process_mad(device
, 0, port_num
, &mad_wc
, NULL
,
864 (const struct ib_mad_hdr
*)smp
, mad_size
,
865 (struct ib_mad_hdr
*)mad_priv
->mad
,
866 &mad_size
, &out_mad_pkey_index
);
869 case IB_MAD_RESULT_SUCCESS
| IB_MAD_RESULT_REPLY
:
870 if (ib_response_mad((const struct ib_mad_hdr
*)mad_priv
->mad
) &&
871 mad_agent_priv
->agent
.recv_handler
) {
872 local
->mad_priv
= mad_priv
;
873 local
->recv_mad_agent
= mad_agent_priv
;
875 * Reference MAD agent until receive
876 * side of local completion handled
878 atomic_inc(&mad_agent_priv
->refcount
);
882 case IB_MAD_RESULT_SUCCESS
| IB_MAD_RESULT_CONSUMED
:
885 case IB_MAD_RESULT_SUCCESS
:
886 /* Treat like an incoming receive MAD */
887 port_priv
= ib_get_mad_port(mad_agent_priv
->agent
.device
,
888 mad_agent_priv
->agent
.port_num
);
890 memcpy(mad_priv
->mad
, smp
, mad_priv
->mad_size
);
891 recv_mad_agent
= find_mad_agent(port_priv
,
892 (const struct ib_mad_hdr
*)mad_priv
->mad
);
894 if (!port_priv
|| !recv_mad_agent
) {
896 * No receiving agent so drop packet and
897 * generate send completion.
902 local
->mad_priv
= mad_priv
;
903 local
->recv_mad_agent
= recv_mad_agent
;
912 local
->mad_send_wr
= mad_send_wr
;
914 local
->mad_send_wr
->send_wr
.pkey_index
= out_mad_pkey_index
;
915 local
->return_wc_byte_len
= mad_size
;
917 /* Reference MAD agent until send side of local completion handled */
918 atomic_inc(&mad_agent_priv
->refcount
);
919 /* Queue local completion to local list */
920 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
921 list_add_tail(&local
->completion_list
, &mad_agent_priv
->local_list
);
922 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
923 queue_work(mad_agent_priv
->qp_info
->port_priv
->wq
,
924 &mad_agent_priv
->local_work
);
930 static int get_pad_size(int hdr_len
, int data_len
, size_t mad_size
)
934 seg_size
= mad_size
- hdr_len
;
935 if (data_len
&& seg_size
) {
936 pad
= seg_size
- data_len
% seg_size
;
937 return pad
== seg_size
? 0 : pad
;
942 static void free_send_rmpp_list(struct ib_mad_send_wr_private
*mad_send_wr
)
944 struct ib_rmpp_segment
*s
, *t
;
946 list_for_each_entry_safe(s
, t
, &mad_send_wr
->rmpp_list
, list
) {
952 static int alloc_send_rmpp_list(struct ib_mad_send_wr_private
*send_wr
,
953 size_t mad_size
, gfp_t gfp_mask
)
955 struct ib_mad_send_buf
*send_buf
= &send_wr
->send_buf
;
956 struct ib_rmpp_mad
*rmpp_mad
= send_buf
->mad
;
957 struct ib_rmpp_segment
*seg
= NULL
;
958 int left
, seg_size
, pad
;
960 send_buf
->seg_size
= mad_size
- send_buf
->hdr_len
;
961 send_buf
->seg_rmpp_size
= mad_size
- IB_MGMT_RMPP_HDR
;
962 seg_size
= send_buf
->seg_size
;
965 /* Allocate data segments. */
966 for (left
= send_buf
->data_len
+ pad
; left
> 0; left
-= seg_size
) {
967 seg
= kmalloc(sizeof (*seg
) + seg_size
, gfp_mask
);
969 free_send_rmpp_list(send_wr
);
972 seg
->num
= ++send_buf
->seg_count
;
973 list_add_tail(&seg
->list
, &send_wr
->rmpp_list
);
976 /* Zero any padding */
978 memset(seg
->data
+ seg_size
- pad
, 0, pad
);
980 rmpp_mad
->rmpp_hdr
.rmpp_version
= send_wr
->mad_agent_priv
->
982 rmpp_mad
->rmpp_hdr
.rmpp_type
= IB_MGMT_RMPP_TYPE_DATA
;
983 ib_set_rmpp_flags(&rmpp_mad
->rmpp_hdr
, IB_MGMT_RMPP_FLAG_ACTIVE
);
985 send_wr
->cur_seg
= container_of(send_wr
->rmpp_list
.next
,
986 struct ib_rmpp_segment
, list
);
987 send_wr
->last_ack_seg
= send_wr
->cur_seg
;
991 int ib_mad_kernel_rmpp_agent(const struct ib_mad_agent
*agent
)
993 return agent
->rmpp_version
&& !(agent
->flags
& IB_MAD_USER_RMPP
);
995 EXPORT_SYMBOL(ib_mad_kernel_rmpp_agent
);
997 struct ib_mad_send_buf
* ib_create_send_mad(struct ib_mad_agent
*mad_agent
,
998 u32 remote_qpn
, u16 pkey_index
,
1000 int hdr_len
, int data_len
,
1004 struct ib_mad_agent_private
*mad_agent_priv
;
1005 struct ib_mad_send_wr_private
*mad_send_wr
;
1006 int pad
, message_size
, ret
, size
;
1011 mad_agent_priv
= container_of(mad_agent
, struct ib_mad_agent_private
,
1014 opa
= rdma_cap_opa_mad(mad_agent
->device
, mad_agent
->port_num
);
1016 if (opa
&& base_version
== OPA_MGMT_BASE_VERSION
)
1017 mad_size
= sizeof(struct opa_mad
);
1019 mad_size
= sizeof(struct ib_mad
);
1021 pad
= get_pad_size(hdr_len
, data_len
, mad_size
);
1022 message_size
= hdr_len
+ data_len
+ pad
;
1024 if (ib_mad_kernel_rmpp_agent(mad_agent
)) {
1025 if (!rmpp_active
&& message_size
> mad_size
)
1026 return ERR_PTR(-EINVAL
);
1028 if (rmpp_active
|| message_size
> mad_size
)
1029 return ERR_PTR(-EINVAL
);
1031 size
= rmpp_active
? hdr_len
: mad_size
;
1032 buf
= kzalloc(sizeof *mad_send_wr
+ size
, gfp_mask
);
1034 return ERR_PTR(-ENOMEM
);
1036 mad_send_wr
= buf
+ size
;
1037 INIT_LIST_HEAD(&mad_send_wr
->rmpp_list
);
1038 mad_send_wr
->send_buf
.mad
= buf
;
1039 mad_send_wr
->send_buf
.hdr_len
= hdr_len
;
1040 mad_send_wr
->send_buf
.data_len
= data_len
;
1041 mad_send_wr
->pad
= pad
;
1043 mad_send_wr
->mad_agent_priv
= mad_agent_priv
;
1044 mad_send_wr
->sg_list
[0].length
= hdr_len
;
1045 mad_send_wr
->sg_list
[0].lkey
= mad_agent
->qp
->pd
->local_dma_lkey
;
1047 /* OPA MADs don't have to be the full 2048 bytes */
1048 if (opa
&& base_version
== OPA_MGMT_BASE_VERSION
&&
1049 data_len
< mad_size
- hdr_len
)
1050 mad_send_wr
->sg_list
[1].length
= data_len
;
1052 mad_send_wr
->sg_list
[1].length
= mad_size
- hdr_len
;
1054 mad_send_wr
->sg_list
[1].lkey
= mad_agent
->qp
->pd
->local_dma_lkey
;
1056 mad_send_wr
->mad_list
.cqe
.done
= ib_mad_send_done
;
1058 mad_send_wr
->send_wr
.wr
.wr_cqe
= &mad_send_wr
->mad_list
.cqe
;
1059 mad_send_wr
->send_wr
.wr
.sg_list
= mad_send_wr
->sg_list
;
1060 mad_send_wr
->send_wr
.wr
.num_sge
= 2;
1061 mad_send_wr
->send_wr
.wr
.opcode
= IB_WR_SEND
;
1062 mad_send_wr
->send_wr
.wr
.send_flags
= IB_SEND_SIGNALED
;
1063 mad_send_wr
->send_wr
.remote_qpn
= remote_qpn
;
1064 mad_send_wr
->send_wr
.remote_qkey
= IB_QP_SET_QKEY
;
1065 mad_send_wr
->send_wr
.pkey_index
= pkey_index
;
1068 ret
= alloc_send_rmpp_list(mad_send_wr
, mad_size
, gfp_mask
);
1071 return ERR_PTR(ret
);
1075 mad_send_wr
->send_buf
.mad_agent
= mad_agent
;
1076 atomic_inc(&mad_agent_priv
->refcount
);
1077 return &mad_send_wr
->send_buf
;
1079 EXPORT_SYMBOL(ib_create_send_mad
);
1081 int ib_get_mad_data_offset(u8 mgmt_class
)
1083 if (mgmt_class
== IB_MGMT_CLASS_SUBN_ADM
)
1084 return IB_MGMT_SA_HDR
;
1085 else if ((mgmt_class
== IB_MGMT_CLASS_DEVICE_MGMT
) ||
1086 (mgmt_class
== IB_MGMT_CLASS_DEVICE_ADM
) ||
1087 (mgmt_class
== IB_MGMT_CLASS_BIS
))
1088 return IB_MGMT_DEVICE_HDR
;
1089 else if ((mgmt_class
>= IB_MGMT_CLASS_VENDOR_RANGE2_START
) &&
1090 (mgmt_class
<= IB_MGMT_CLASS_VENDOR_RANGE2_END
))
1091 return IB_MGMT_VENDOR_HDR
;
1093 return IB_MGMT_MAD_HDR
;
1095 EXPORT_SYMBOL(ib_get_mad_data_offset
);
1097 int ib_is_mad_class_rmpp(u8 mgmt_class
)
1099 if ((mgmt_class
== IB_MGMT_CLASS_SUBN_ADM
) ||
1100 (mgmt_class
== IB_MGMT_CLASS_DEVICE_MGMT
) ||
1101 (mgmt_class
== IB_MGMT_CLASS_DEVICE_ADM
) ||
1102 (mgmt_class
== IB_MGMT_CLASS_BIS
) ||
1103 ((mgmt_class
>= IB_MGMT_CLASS_VENDOR_RANGE2_START
) &&
1104 (mgmt_class
<= IB_MGMT_CLASS_VENDOR_RANGE2_END
)))
1108 EXPORT_SYMBOL(ib_is_mad_class_rmpp
);
1110 void *ib_get_rmpp_segment(struct ib_mad_send_buf
*send_buf
, int seg_num
)
1112 struct ib_mad_send_wr_private
*mad_send_wr
;
1113 struct list_head
*list
;
1115 mad_send_wr
= container_of(send_buf
, struct ib_mad_send_wr_private
,
1117 list
= &mad_send_wr
->cur_seg
->list
;
1119 if (mad_send_wr
->cur_seg
->num
< seg_num
) {
1120 list_for_each_entry(mad_send_wr
->cur_seg
, list
, list
)
1121 if (mad_send_wr
->cur_seg
->num
== seg_num
)
1123 } else if (mad_send_wr
->cur_seg
->num
> seg_num
) {
1124 list_for_each_entry_reverse(mad_send_wr
->cur_seg
, list
, list
)
1125 if (mad_send_wr
->cur_seg
->num
== seg_num
)
1128 return mad_send_wr
->cur_seg
->data
;
1130 EXPORT_SYMBOL(ib_get_rmpp_segment
);
1132 static inline void *ib_get_payload(struct ib_mad_send_wr_private
*mad_send_wr
)
1134 if (mad_send_wr
->send_buf
.seg_count
)
1135 return ib_get_rmpp_segment(&mad_send_wr
->send_buf
,
1136 mad_send_wr
->seg_num
);
1138 return mad_send_wr
->send_buf
.mad
+
1139 mad_send_wr
->send_buf
.hdr_len
;
1142 void ib_free_send_mad(struct ib_mad_send_buf
*send_buf
)
1144 struct ib_mad_agent_private
*mad_agent_priv
;
1145 struct ib_mad_send_wr_private
*mad_send_wr
;
1147 mad_agent_priv
= container_of(send_buf
->mad_agent
,
1148 struct ib_mad_agent_private
, agent
);
1149 mad_send_wr
= container_of(send_buf
, struct ib_mad_send_wr_private
,
1152 free_send_rmpp_list(mad_send_wr
);
1153 kfree(send_buf
->mad
);
1154 deref_mad_agent(mad_agent_priv
);
1156 EXPORT_SYMBOL(ib_free_send_mad
);
1158 int ib_send_mad(struct ib_mad_send_wr_private
*mad_send_wr
)
1160 struct ib_mad_qp_info
*qp_info
;
1161 struct list_head
*list
;
1162 struct ib_send_wr
*bad_send_wr
;
1163 struct ib_mad_agent
*mad_agent
;
1165 unsigned long flags
;
1168 /* Set WR ID to find mad_send_wr upon completion */
1169 qp_info
= mad_send_wr
->mad_agent_priv
->qp_info
;
1170 mad_send_wr
->mad_list
.mad_queue
= &qp_info
->send_queue
;
1171 mad_send_wr
->mad_list
.cqe
.done
= ib_mad_send_done
;
1172 mad_send_wr
->send_wr
.wr
.wr_cqe
= &mad_send_wr
->mad_list
.cqe
;
1174 mad_agent
= mad_send_wr
->send_buf
.mad_agent
;
1175 sge
= mad_send_wr
->sg_list
;
1176 sge
[0].addr
= ib_dma_map_single(mad_agent
->device
,
1177 mad_send_wr
->send_buf
.mad
,
1180 if (unlikely(ib_dma_mapping_error(mad_agent
->device
, sge
[0].addr
)))
1183 mad_send_wr
->header_mapping
= sge
[0].addr
;
1185 sge
[1].addr
= ib_dma_map_single(mad_agent
->device
,
1186 ib_get_payload(mad_send_wr
),
1189 if (unlikely(ib_dma_mapping_error(mad_agent
->device
, sge
[1].addr
))) {
1190 ib_dma_unmap_single(mad_agent
->device
,
1191 mad_send_wr
->header_mapping
,
1192 sge
[0].length
, DMA_TO_DEVICE
);
1195 mad_send_wr
->payload_mapping
= sge
[1].addr
;
1197 spin_lock_irqsave(&qp_info
->send_queue
.lock
, flags
);
1198 if (qp_info
->send_queue
.count
< qp_info
->send_queue
.max_active
) {
1199 ret
= ib_post_send(mad_agent
->qp
, &mad_send_wr
->send_wr
.wr
,
1201 list
= &qp_info
->send_queue
.list
;
1204 list
= &qp_info
->overflow_list
;
1208 qp_info
->send_queue
.count
++;
1209 list_add_tail(&mad_send_wr
->mad_list
.list
, list
);
1211 spin_unlock_irqrestore(&qp_info
->send_queue
.lock
, flags
);
1213 ib_dma_unmap_single(mad_agent
->device
,
1214 mad_send_wr
->header_mapping
,
1215 sge
[0].length
, DMA_TO_DEVICE
);
1216 ib_dma_unmap_single(mad_agent
->device
,
1217 mad_send_wr
->payload_mapping
,
1218 sge
[1].length
, DMA_TO_DEVICE
);
1224 * ib_post_send_mad - Posts MAD(s) to the send queue of the QP associated
1225 * with the registered client
1227 int ib_post_send_mad(struct ib_mad_send_buf
*send_buf
,
1228 struct ib_mad_send_buf
**bad_send_buf
)
1230 struct ib_mad_agent_private
*mad_agent_priv
;
1231 struct ib_mad_send_buf
*next_send_buf
;
1232 struct ib_mad_send_wr_private
*mad_send_wr
;
1233 unsigned long flags
;
1236 /* Walk list of send WRs and post each on send list */
1237 for (; send_buf
; send_buf
= next_send_buf
) {
1238 mad_send_wr
= container_of(send_buf
,
1239 struct ib_mad_send_wr_private
,
1241 mad_agent_priv
= mad_send_wr
->mad_agent_priv
;
1243 ret
= ib_mad_enforce_security(mad_agent_priv
,
1244 mad_send_wr
->send_wr
.pkey_index
);
1248 if (!send_buf
->mad_agent
->send_handler
||
1249 (send_buf
->timeout_ms
&&
1250 !send_buf
->mad_agent
->recv_handler
)) {
1255 if (!ib_is_mad_class_rmpp(((struct ib_mad_hdr
*) send_buf
->mad
)->mgmt_class
)) {
1256 if (mad_agent_priv
->agent
.rmpp_version
) {
1263 * Save pointer to next work request to post in case the
1264 * current one completes, and the user modifies the work
1265 * request associated with the completion
1267 next_send_buf
= send_buf
->next
;
1268 mad_send_wr
->send_wr
.ah
= send_buf
->ah
;
1270 if (((struct ib_mad_hdr
*) send_buf
->mad
)->mgmt_class
==
1271 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
) {
1272 ret
= handle_outgoing_dr_smp(mad_agent_priv
,
1274 if (ret
< 0) /* error */
1276 else if (ret
== 1) /* locally consumed */
1280 mad_send_wr
->tid
= ((struct ib_mad_hdr
*) send_buf
->mad
)->tid
;
1281 /* Timeout will be updated after send completes */
1282 mad_send_wr
->timeout
= msecs_to_jiffies(send_buf
->timeout_ms
);
1283 mad_send_wr
->max_retries
= send_buf
->retries
;
1284 mad_send_wr
->retries_left
= send_buf
->retries
;
1285 send_buf
->retries
= 0;
1286 /* Reference for work request to QP + response */
1287 mad_send_wr
->refcount
= 1 + (mad_send_wr
->timeout
> 0);
1288 mad_send_wr
->status
= IB_WC_SUCCESS
;
1290 /* Reference MAD agent until send completes */
1291 atomic_inc(&mad_agent_priv
->refcount
);
1292 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
1293 list_add_tail(&mad_send_wr
->agent_list
,
1294 &mad_agent_priv
->send_list
);
1295 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
1297 if (ib_mad_kernel_rmpp_agent(&mad_agent_priv
->agent
)) {
1298 ret
= ib_send_rmpp_mad(mad_send_wr
);
1299 if (ret
>= 0 && ret
!= IB_RMPP_RESULT_CONSUMED
)
1300 ret
= ib_send_mad(mad_send_wr
);
1302 ret
= ib_send_mad(mad_send_wr
);
1304 /* Fail send request */
1305 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
1306 list_del(&mad_send_wr
->agent_list
);
1307 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
1308 atomic_dec(&mad_agent_priv
->refcount
);
1315 *bad_send_buf
= send_buf
;
1318 EXPORT_SYMBOL(ib_post_send_mad
);
1321 * ib_free_recv_mad - Returns data buffers used to receive
1322 * a MAD to the access layer
1324 void ib_free_recv_mad(struct ib_mad_recv_wc
*mad_recv_wc
)
1326 struct ib_mad_recv_buf
*mad_recv_buf
, *temp_recv_buf
;
1327 struct ib_mad_private_header
*mad_priv_hdr
;
1328 struct ib_mad_private
*priv
;
1329 struct list_head free_list
;
1331 INIT_LIST_HEAD(&free_list
);
1332 list_splice_init(&mad_recv_wc
->rmpp_list
, &free_list
);
1334 list_for_each_entry_safe(mad_recv_buf
, temp_recv_buf
,
1336 mad_recv_wc
= container_of(mad_recv_buf
, struct ib_mad_recv_wc
,
1338 mad_priv_hdr
= container_of(mad_recv_wc
,
1339 struct ib_mad_private_header
,
1341 priv
= container_of(mad_priv_hdr
, struct ib_mad_private
,
1346 EXPORT_SYMBOL(ib_free_recv_mad
);
1348 struct ib_mad_agent
*ib_redirect_mad_qp(struct ib_qp
*qp
,
1350 ib_mad_send_handler send_handler
,
1351 ib_mad_recv_handler recv_handler
,
1354 return ERR_PTR(-EINVAL
); /* XXX: for now */
1356 EXPORT_SYMBOL(ib_redirect_mad_qp
);
1358 int ib_process_mad_wc(struct ib_mad_agent
*mad_agent
,
1361 dev_err(&mad_agent
->device
->dev
,
1362 "ib_process_mad_wc() not implemented yet\n");
1365 EXPORT_SYMBOL(ib_process_mad_wc
);
1367 static int method_in_use(struct ib_mad_mgmt_method_table
**method
,
1368 struct ib_mad_reg_req
*mad_reg_req
)
1372 for_each_set_bit(i
, mad_reg_req
->method_mask
, IB_MGMT_MAX_METHODS
) {
1373 if ((*method
)->agent
[i
]) {
1374 pr_err("Method %d already in use\n", i
);
1381 static int allocate_method_table(struct ib_mad_mgmt_method_table
**method
)
1383 /* Allocate management method table */
1384 *method
= kzalloc(sizeof **method
, GFP_ATOMIC
);
1385 return (*method
) ? 0 : (-ENOMEM
);
1389 * Check to see if there are any methods still in use
1391 static int check_method_table(struct ib_mad_mgmt_method_table
*method
)
1395 for (i
= 0; i
< IB_MGMT_MAX_METHODS
; i
++)
1396 if (method
->agent
[i
])
1402 * Check to see if there are any method tables for this class still in use
1404 static int check_class_table(struct ib_mad_mgmt_class_table
*class)
1408 for (i
= 0; i
< MAX_MGMT_CLASS
; i
++)
1409 if (class->method_table
[i
])
1414 static int check_vendor_class(struct ib_mad_mgmt_vendor_class
*vendor_class
)
1418 for (i
= 0; i
< MAX_MGMT_OUI
; i
++)
1419 if (vendor_class
->method_table
[i
])
1424 static int find_vendor_oui(struct ib_mad_mgmt_vendor_class
*vendor_class
,
1429 for (i
= 0; i
< MAX_MGMT_OUI
; i
++)
1430 /* Is there matching OUI for this vendor class ? */
1431 if (!memcmp(vendor_class
->oui
[i
], oui
, 3))
1437 static int check_vendor_table(struct ib_mad_mgmt_vendor_class_table
*vendor
)
1441 for (i
= 0; i
< MAX_MGMT_VENDOR_RANGE2
; i
++)
1442 if (vendor
->vendor_class
[i
])
1448 static void remove_methods_mad_agent(struct ib_mad_mgmt_method_table
*method
,
1449 struct ib_mad_agent_private
*agent
)
1453 /* Remove any methods for this mad agent */
1454 for (i
= 0; i
< IB_MGMT_MAX_METHODS
; i
++) {
1455 if (method
->agent
[i
] == agent
) {
1456 method
->agent
[i
] = NULL
;
1461 static int add_nonoui_reg_req(struct ib_mad_reg_req
*mad_reg_req
,
1462 struct ib_mad_agent_private
*agent_priv
,
1465 struct ib_mad_port_private
*port_priv
;
1466 struct ib_mad_mgmt_class_table
**class;
1467 struct ib_mad_mgmt_method_table
**method
;
1470 port_priv
= agent_priv
->qp_info
->port_priv
;
1471 class = &port_priv
->version
[mad_reg_req
->mgmt_class_version
].class;
1473 /* Allocate management class table for "new" class version */
1474 *class = kzalloc(sizeof **class, GFP_ATOMIC
);
1480 /* Allocate method table for this management class */
1481 method
= &(*class)->method_table
[mgmt_class
];
1482 if ((ret
= allocate_method_table(method
)))
1485 method
= &(*class)->method_table
[mgmt_class
];
1487 /* Allocate method table for this management class */
1488 if ((ret
= allocate_method_table(method
)))
1493 /* Now, make sure methods are not already in use */
1494 if (method_in_use(method
, mad_reg_req
))
1497 /* Finally, add in methods being registered */
1498 for_each_set_bit(i
, mad_reg_req
->method_mask
, IB_MGMT_MAX_METHODS
)
1499 (*method
)->agent
[i
] = agent_priv
;
1504 /* Remove any methods for this mad agent */
1505 remove_methods_mad_agent(*method
, agent_priv
);
1506 /* Now, check to see if there are any methods in use */
1507 if (!check_method_table(*method
)) {
1508 /* If not, release management method table */
1521 static int add_oui_reg_req(struct ib_mad_reg_req
*mad_reg_req
,
1522 struct ib_mad_agent_private
*agent_priv
)
1524 struct ib_mad_port_private
*port_priv
;
1525 struct ib_mad_mgmt_vendor_class_table
**vendor_table
;
1526 struct ib_mad_mgmt_vendor_class_table
*vendor
= NULL
;
1527 struct ib_mad_mgmt_vendor_class
*vendor_class
= NULL
;
1528 struct ib_mad_mgmt_method_table
**method
;
1529 int i
, ret
= -ENOMEM
;
1532 /* "New" vendor (with OUI) class */
1533 vclass
= vendor_class_index(mad_reg_req
->mgmt_class
);
1534 port_priv
= agent_priv
->qp_info
->port_priv
;
1535 vendor_table
= &port_priv
->version
[
1536 mad_reg_req
->mgmt_class_version
].vendor
;
1537 if (!*vendor_table
) {
1538 /* Allocate mgmt vendor class table for "new" class version */
1539 vendor
= kzalloc(sizeof *vendor
, GFP_ATOMIC
);
1543 *vendor_table
= vendor
;
1545 if (!(*vendor_table
)->vendor_class
[vclass
]) {
1546 /* Allocate table for this management vendor class */
1547 vendor_class
= kzalloc(sizeof *vendor_class
, GFP_ATOMIC
);
1551 (*vendor_table
)->vendor_class
[vclass
] = vendor_class
;
1553 for (i
= 0; i
< MAX_MGMT_OUI
; i
++) {
1554 /* Is there matching OUI for this vendor class ? */
1555 if (!memcmp((*vendor_table
)->vendor_class
[vclass
]->oui
[i
],
1556 mad_reg_req
->oui
, 3)) {
1557 method
= &(*vendor_table
)->vendor_class
[
1558 vclass
]->method_table
[i
];
1564 for (i
= 0; i
< MAX_MGMT_OUI
; i
++) {
1565 /* OUI slot available ? */
1566 if (!is_vendor_oui((*vendor_table
)->vendor_class
[
1568 method
= &(*vendor_table
)->vendor_class
[
1569 vclass
]->method_table
[i
];
1570 /* Allocate method table for this OUI */
1572 ret
= allocate_method_table(method
);
1576 memcpy((*vendor_table
)->vendor_class
[vclass
]->oui
[i
],
1577 mad_reg_req
->oui
, 3);
1581 dev_err(&agent_priv
->agent
.device
->dev
, "All OUI slots in use\n");
1585 /* Now, make sure methods are not already in use */
1586 if (method_in_use(method
, mad_reg_req
))
1589 /* Finally, add in methods being registered */
1590 for_each_set_bit(i
, mad_reg_req
->method_mask
, IB_MGMT_MAX_METHODS
)
1591 (*method
)->agent
[i
] = agent_priv
;
1596 /* Remove any methods for this mad agent */
1597 remove_methods_mad_agent(*method
, agent_priv
);
1598 /* Now, check to see if there are any methods in use */
1599 if (!check_method_table(*method
)) {
1600 /* If not, release management method table */
1607 (*vendor_table
)->vendor_class
[vclass
] = NULL
;
1608 kfree(vendor_class
);
1612 *vendor_table
= NULL
;
1619 static void remove_mad_reg_req(struct ib_mad_agent_private
*agent_priv
)
1621 struct ib_mad_port_private
*port_priv
;
1622 struct ib_mad_mgmt_class_table
*class;
1623 struct ib_mad_mgmt_method_table
*method
;
1624 struct ib_mad_mgmt_vendor_class_table
*vendor
;
1625 struct ib_mad_mgmt_vendor_class
*vendor_class
;
1630 * Was MAD registration request supplied
1631 * with original registration ?
1633 if (!agent_priv
->reg_req
) {
1637 port_priv
= agent_priv
->qp_info
->port_priv
;
1638 mgmt_class
= convert_mgmt_class(agent_priv
->reg_req
->mgmt_class
);
1639 class = port_priv
->version
[
1640 agent_priv
->reg_req
->mgmt_class_version
].class;
1644 method
= class->method_table
[mgmt_class
];
1646 /* Remove any methods for this mad agent */
1647 remove_methods_mad_agent(method
, agent_priv
);
1648 /* Now, check to see if there are any methods still in use */
1649 if (!check_method_table(method
)) {
1650 /* If not, release management method table */
1652 class->method_table
[mgmt_class
] = NULL
;
1653 /* Any management classes left ? */
1654 if (!check_class_table(class)) {
1655 /* If not, release management class table */
1658 agent_priv
->reg_req
->
1659 mgmt_class_version
].class = NULL
;
1665 if (!is_vendor_class(mgmt_class
))
1668 /* normalize mgmt_class to vendor range 2 */
1669 mgmt_class
= vendor_class_index(agent_priv
->reg_req
->mgmt_class
);
1670 vendor
= port_priv
->version
[
1671 agent_priv
->reg_req
->mgmt_class_version
].vendor
;
1676 vendor_class
= vendor
->vendor_class
[mgmt_class
];
1678 index
= find_vendor_oui(vendor_class
, agent_priv
->reg_req
->oui
);
1681 method
= vendor_class
->method_table
[index
];
1683 /* Remove any methods for this mad agent */
1684 remove_methods_mad_agent(method
, agent_priv
);
1686 * Now, check to see if there are
1687 * any methods still in use
1689 if (!check_method_table(method
)) {
1690 /* If not, release management method table */
1692 vendor_class
->method_table
[index
] = NULL
;
1693 memset(vendor_class
->oui
[index
], 0, 3);
1694 /* Any OUIs left ? */
1695 if (!check_vendor_class(vendor_class
)) {
1696 /* If not, release vendor class table */
1697 kfree(vendor_class
);
1698 vendor
->vendor_class
[mgmt_class
] = NULL
;
1699 /* Any other vendor classes left ? */
1700 if (!check_vendor_table(vendor
)) {
1703 agent_priv
->reg_req
->
1704 mgmt_class_version
].
1716 static struct ib_mad_agent_private
*
1717 find_mad_agent(struct ib_mad_port_private
*port_priv
,
1718 const struct ib_mad_hdr
*mad_hdr
)
1720 struct ib_mad_agent_private
*mad_agent
= NULL
;
1721 unsigned long flags
;
1723 spin_lock_irqsave(&port_priv
->reg_lock
, flags
);
1724 if (ib_response_mad(mad_hdr
)) {
1726 struct ib_mad_agent_private
*entry
;
1729 * Routing is based on high 32 bits of transaction ID
1732 hi_tid
= be64_to_cpu(mad_hdr
->tid
) >> 32;
1733 list_for_each_entry(entry
, &port_priv
->agent_list
, agent_list
) {
1734 if (entry
->agent
.hi_tid
== hi_tid
) {
1740 struct ib_mad_mgmt_class_table
*class;
1741 struct ib_mad_mgmt_method_table
*method
;
1742 struct ib_mad_mgmt_vendor_class_table
*vendor
;
1743 struct ib_mad_mgmt_vendor_class
*vendor_class
;
1744 const struct ib_vendor_mad
*vendor_mad
;
1748 * Routing is based on version, class, and method
1749 * For "newer" vendor MADs, also based on OUI
1751 if (mad_hdr
->class_version
>= MAX_MGMT_VERSION
)
1753 if (!is_vendor_class(mad_hdr
->mgmt_class
)) {
1754 class = port_priv
->version
[
1755 mad_hdr
->class_version
].class;
1758 if (convert_mgmt_class(mad_hdr
->mgmt_class
) >=
1759 ARRAY_SIZE(class->method_table
))
1761 method
= class->method_table
[convert_mgmt_class(
1762 mad_hdr
->mgmt_class
)];
1764 mad_agent
= method
->agent
[mad_hdr
->method
&
1765 ~IB_MGMT_METHOD_RESP
];
1767 vendor
= port_priv
->version
[
1768 mad_hdr
->class_version
].vendor
;
1771 vendor_class
= vendor
->vendor_class
[vendor_class_index(
1772 mad_hdr
->mgmt_class
)];
1775 /* Find matching OUI */
1776 vendor_mad
= (const struct ib_vendor_mad
*)mad_hdr
;
1777 index
= find_vendor_oui(vendor_class
, vendor_mad
->oui
);
1780 method
= vendor_class
->method_table
[index
];
1782 mad_agent
= method
->agent
[mad_hdr
->method
&
1783 ~IB_MGMT_METHOD_RESP
];
1789 if (mad_agent
->agent
.recv_handler
)
1790 atomic_inc(&mad_agent
->refcount
);
1792 dev_notice(&port_priv
->device
->dev
,
1793 "No receive handler for client %p on port %d\n",
1794 &mad_agent
->agent
, port_priv
->port_num
);
1799 spin_unlock_irqrestore(&port_priv
->reg_lock
, flags
);
1804 static int validate_mad(const struct ib_mad_hdr
*mad_hdr
,
1805 const struct ib_mad_qp_info
*qp_info
,
1809 u32 qp_num
= qp_info
->qp
->qp_num
;
1811 /* Make sure MAD base version is understood */
1812 if (mad_hdr
->base_version
!= IB_MGMT_BASE_VERSION
&&
1813 (!opa
|| mad_hdr
->base_version
!= OPA_MGMT_BASE_VERSION
)) {
1814 pr_err("MAD received with unsupported base version %d %s\n",
1815 mad_hdr
->base_version
, opa
? "(opa)" : "");
1819 /* Filter SMI packets sent to other than QP0 */
1820 if ((mad_hdr
->mgmt_class
== IB_MGMT_CLASS_SUBN_LID_ROUTED
) ||
1821 (mad_hdr
->mgmt_class
== IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
)) {
1825 /* CM attributes other than ClassPortInfo only use Send method */
1826 if ((mad_hdr
->mgmt_class
== IB_MGMT_CLASS_CM
) &&
1827 (mad_hdr
->attr_id
!= IB_MGMT_CLASSPORTINFO_ATTR_ID
) &&
1828 (mad_hdr
->method
!= IB_MGMT_METHOD_SEND
))
1830 /* Filter GSI packets sent to QP0 */
1839 static int is_rmpp_data_mad(const struct ib_mad_agent_private
*mad_agent_priv
,
1840 const struct ib_mad_hdr
*mad_hdr
)
1842 struct ib_rmpp_mad
*rmpp_mad
;
1844 rmpp_mad
= (struct ib_rmpp_mad
*)mad_hdr
;
1845 return !mad_agent_priv
->agent
.rmpp_version
||
1846 !ib_mad_kernel_rmpp_agent(&mad_agent_priv
->agent
) ||
1847 !(ib_get_rmpp_flags(&rmpp_mad
->rmpp_hdr
) &
1848 IB_MGMT_RMPP_FLAG_ACTIVE
) ||
1849 (rmpp_mad
->rmpp_hdr
.rmpp_type
== IB_MGMT_RMPP_TYPE_DATA
);
1852 static inline int rcv_has_same_class(const struct ib_mad_send_wr_private
*wr
,
1853 const struct ib_mad_recv_wc
*rwc
)
1855 return ((struct ib_mad_hdr
*)(wr
->send_buf
.mad
))->mgmt_class
==
1856 rwc
->recv_buf
.mad
->mad_hdr
.mgmt_class
;
1859 static inline int rcv_has_same_gid(const struct ib_mad_agent_private
*mad_agent_priv
,
1860 const struct ib_mad_send_wr_private
*wr
,
1861 const struct ib_mad_recv_wc
*rwc
)
1863 struct rdma_ah_attr attr
;
1864 u8 send_resp
, rcv_resp
;
1866 struct ib_device
*device
= mad_agent_priv
->agent
.device
;
1867 u8 port_num
= mad_agent_priv
->agent
.port_num
;
1871 send_resp
= ib_response_mad((struct ib_mad_hdr
*)wr
->send_buf
.mad
);
1872 rcv_resp
= ib_response_mad(&rwc
->recv_buf
.mad
->mad_hdr
);
1874 if (send_resp
== rcv_resp
)
1875 /* both requests, or both responses. GIDs different */
1878 if (rdma_query_ah(wr
->send_buf
.ah
, &attr
))
1879 /* Assume not equal, to avoid false positives. */
1882 has_grh
= !!(rdma_ah_get_ah_flags(&attr
) & IB_AH_GRH
);
1883 if (has_grh
!= !!(rwc
->wc
->wc_flags
& IB_WC_GRH
))
1884 /* one has GID, other does not. Assume different */
1887 if (!send_resp
&& rcv_resp
) {
1888 /* is request/response. */
1890 if (ib_get_cached_lmc(device
, port_num
, &lmc
))
1892 return (!lmc
|| !((rdma_ah_get_path_bits(&attr
) ^
1893 rwc
->wc
->dlid_path_bits
) &
1896 const struct ib_global_route
*grh
=
1897 rdma_ah_read_grh(&attr
);
1899 if (ib_get_cached_gid(device
, port_num
,
1900 grh
->sgid_index
, &sgid
, NULL
))
1902 return !memcmp(sgid
.raw
, rwc
->recv_buf
.grh
->dgid
.raw
,
1908 return rdma_ah_get_dlid(&attr
) == rwc
->wc
->slid
;
1910 return !memcmp(rdma_ah_read_grh(&attr
)->dgid
.raw
,
1911 rwc
->recv_buf
.grh
->sgid
.raw
,
1915 static inline int is_direct(u8
class)
1917 return (class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
);
1920 struct ib_mad_send_wr_private
*
1921 ib_find_send_mad(const struct ib_mad_agent_private
*mad_agent_priv
,
1922 const struct ib_mad_recv_wc
*wc
)
1924 struct ib_mad_send_wr_private
*wr
;
1925 const struct ib_mad_hdr
*mad_hdr
;
1927 mad_hdr
= &wc
->recv_buf
.mad
->mad_hdr
;
1929 list_for_each_entry(wr
, &mad_agent_priv
->wait_list
, agent_list
) {
1930 if ((wr
->tid
== mad_hdr
->tid
) &&
1931 rcv_has_same_class(wr
, wc
) &&
1933 * Don't check GID for direct routed MADs.
1934 * These might have permissive LIDs.
1936 (is_direct(mad_hdr
->mgmt_class
) ||
1937 rcv_has_same_gid(mad_agent_priv
, wr
, wc
)))
1938 return (wr
->status
== IB_WC_SUCCESS
) ? wr
: NULL
;
1942 * It's possible to receive the response before we've
1943 * been notified that the send has completed
1945 list_for_each_entry(wr
, &mad_agent_priv
->send_list
, agent_list
) {
1946 if (is_rmpp_data_mad(mad_agent_priv
, wr
->send_buf
.mad
) &&
1947 wr
->tid
== mad_hdr
->tid
&&
1949 rcv_has_same_class(wr
, wc
) &&
1951 * Don't check GID for direct routed MADs.
1952 * These might have permissive LIDs.
1954 (is_direct(mad_hdr
->mgmt_class
) ||
1955 rcv_has_same_gid(mad_agent_priv
, wr
, wc
)))
1956 /* Verify request has not been canceled */
1957 return (wr
->status
== IB_WC_SUCCESS
) ? wr
: NULL
;
1962 void ib_mark_mad_done(struct ib_mad_send_wr_private
*mad_send_wr
)
1964 mad_send_wr
->timeout
= 0;
1965 if (mad_send_wr
->refcount
== 1)
1966 list_move_tail(&mad_send_wr
->agent_list
,
1967 &mad_send_wr
->mad_agent_priv
->done_list
);
1970 static void ib_mad_complete_recv(struct ib_mad_agent_private
*mad_agent_priv
,
1971 struct ib_mad_recv_wc
*mad_recv_wc
)
1973 struct ib_mad_send_wr_private
*mad_send_wr
;
1974 struct ib_mad_send_wc mad_send_wc
;
1975 unsigned long flags
;
1978 INIT_LIST_HEAD(&mad_recv_wc
->rmpp_list
);
1979 ret
= ib_mad_enforce_security(mad_agent_priv
,
1980 mad_recv_wc
->wc
->pkey_index
);
1982 ib_free_recv_mad(mad_recv_wc
);
1983 deref_mad_agent(mad_agent_priv
);
1987 list_add(&mad_recv_wc
->recv_buf
.list
, &mad_recv_wc
->rmpp_list
);
1988 if (ib_mad_kernel_rmpp_agent(&mad_agent_priv
->agent
)) {
1989 mad_recv_wc
= ib_process_rmpp_recv_wc(mad_agent_priv
,
1992 deref_mad_agent(mad_agent_priv
);
1997 /* Complete corresponding request */
1998 if (ib_response_mad(&mad_recv_wc
->recv_buf
.mad
->mad_hdr
)) {
1999 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2000 mad_send_wr
= ib_find_send_mad(mad_agent_priv
, mad_recv_wc
);
2002 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2003 if (!ib_mad_kernel_rmpp_agent(&mad_agent_priv
->agent
)
2004 && ib_is_mad_class_rmpp(mad_recv_wc
->recv_buf
.mad
->mad_hdr
.mgmt_class
)
2005 && (ib_get_rmpp_flags(&((struct ib_rmpp_mad
*)mad_recv_wc
->recv_buf
.mad
)->rmpp_hdr
)
2006 & IB_MGMT_RMPP_FLAG_ACTIVE
)) {
2007 /* user rmpp is in effect
2008 * and this is an active RMPP MAD
2010 mad_agent_priv
->agent
.recv_handler(
2011 &mad_agent_priv
->agent
, NULL
,
2013 atomic_dec(&mad_agent_priv
->refcount
);
2015 /* not user rmpp, revert to normal behavior and
2017 ib_free_recv_mad(mad_recv_wc
);
2018 deref_mad_agent(mad_agent_priv
);
2022 ib_mark_mad_done(mad_send_wr
);
2023 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2025 /* Defined behavior is to complete response before request */
2026 mad_agent_priv
->agent
.recv_handler(
2027 &mad_agent_priv
->agent
,
2028 &mad_send_wr
->send_buf
,
2030 atomic_dec(&mad_agent_priv
->refcount
);
2032 mad_send_wc
.status
= IB_WC_SUCCESS
;
2033 mad_send_wc
.vendor_err
= 0;
2034 mad_send_wc
.send_buf
= &mad_send_wr
->send_buf
;
2035 ib_mad_complete_send_wr(mad_send_wr
, &mad_send_wc
);
2038 mad_agent_priv
->agent
.recv_handler(&mad_agent_priv
->agent
, NULL
,
2040 deref_mad_agent(mad_agent_priv
);
2046 static enum smi_action
handle_ib_smi(const struct ib_mad_port_private
*port_priv
,
2047 const struct ib_mad_qp_info
*qp_info
,
2048 const struct ib_wc
*wc
,
2050 struct ib_mad_private
*recv
,
2051 struct ib_mad_private
*response
)
2053 enum smi_forward_action retsmi
;
2054 struct ib_smp
*smp
= (struct ib_smp
*)recv
->mad
;
2056 if (smi_handle_dr_smp_recv(smp
,
2057 rdma_cap_ib_switch(port_priv
->device
),
2059 port_priv
->device
->phys_port_cnt
) ==
2061 return IB_SMI_DISCARD
;
2063 retsmi
= smi_check_forward_dr_smp(smp
);
2064 if (retsmi
== IB_SMI_LOCAL
)
2065 return IB_SMI_HANDLE
;
2067 if (retsmi
== IB_SMI_SEND
) { /* don't forward */
2068 if (smi_handle_dr_smp_send(smp
,
2069 rdma_cap_ib_switch(port_priv
->device
),
2070 port_num
) == IB_SMI_DISCARD
)
2071 return IB_SMI_DISCARD
;
2073 if (smi_check_local_smp(smp
, port_priv
->device
) == IB_SMI_DISCARD
)
2074 return IB_SMI_DISCARD
;
2075 } else if (rdma_cap_ib_switch(port_priv
->device
)) {
2076 /* forward case for switches */
2077 memcpy(response
, recv
, mad_priv_size(response
));
2078 response
->header
.recv_wc
.wc
= &response
->header
.wc
;
2079 response
->header
.recv_wc
.recv_buf
.mad
= (struct ib_mad
*)response
->mad
;
2080 response
->header
.recv_wc
.recv_buf
.grh
= &response
->grh
;
2082 agent_send_response((const struct ib_mad_hdr
*)response
->mad
,
2085 smi_get_fwd_port(smp
),
2086 qp_info
->qp
->qp_num
,
2090 return IB_SMI_DISCARD
;
2092 return IB_SMI_HANDLE
;
2095 static bool generate_unmatched_resp(const struct ib_mad_private
*recv
,
2096 struct ib_mad_private
*response
,
2097 size_t *resp_len
, bool opa
)
2099 const struct ib_mad_hdr
*recv_hdr
= (const struct ib_mad_hdr
*)recv
->mad
;
2100 struct ib_mad_hdr
*resp_hdr
= (struct ib_mad_hdr
*)response
->mad
;
2102 if (recv_hdr
->method
== IB_MGMT_METHOD_GET
||
2103 recv_hdr
->method
== IB_MGMT_METHOD_SET
) {
2104 memcpy(response
, recv
, mad_priv_size(response
));
2105 response
->header
.recv_wc
.wc
= &response
->header
.wc
;
2106 response
->header
.recv_wc
.recv_buf
.mad
= (struct ib_mad
*)response
->mad
;
2107 response
->header
.recv_wc
.recv_buf
.grh
= &response
->grh
;
2108 resp_hdr
->method
= IB_MGMT_METHOD_GET_RESP
;
2109 resp_hdr
->status
= cpu_to_be16(IB_MGMT_MAD_STATUS_UNSUPPORTED_METHOD_ATTRIB
);
2110 if (recv_hdr
->mgmt_class
== IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
)
2111 resp_hdr
->status
|= IB_SMP_DIRECTION
;
2113 if (opa
&& recv_hdr
->base_version
== OPA_MGMT_BASE_VERSION
) {
2114 if (recv_hdr
->mgmt_class
==
2115 IB_MGMT_CLASS_SUBN_LID_ROUTED
||
2116 recv_hdr
->mgmt_class
==
2117 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
)
2118 *resp_len
= opa_get_smp_header_size(
2119 (struct opa_smp
*)recv
->mad
);
2121 *resp_len
= sizeof(struct ib_mad_hdr
);
2130 static enum smi_action
2131 handle_opa_smi(struct ib_mad_port_private
*port_priv
,
2132 struct ib_mad_qp_info
*qp_info
,
2135 struct ib_mad_private
*recv
,
2136 struct ib_mad_private
*response
)
2138 enum smi_forward_action retsmi
;
2139 struct opa_smp
*smp
= (struct opa_smp
*)recv
->mad
;
2141 if (opa_smi_handle_dr_smp_recv(smp
,
2142 rdma_cap_ib_switch(port_priv
->device
),
2144 port_priv
->device
->phys_port_cnt
) ==
2146 return IB_SMI_DISCARD
;
2148 retsmi
= opa_smi_check_forward_dr_smp(smp
);
2149 if (retsmi
== IB_SMI_LOCAL
)
2150 return IB_SMI_HANDLE
;
2152 if (retsmi
== IB_SMI_SEND
) { /* don't forward */
2153 if (opa_smi_handle_dr_smp_send(smp
,
2154 rdma_cap_ib_switch(port_priv
->device
),
2155 port_num
) == IB_SMI_DISCARD
)
2156 return IB_SMI_DISCARD
;
2158 if (opa_smi_check_local_smp(smp
, port_priv
->device
) ==
2160 return IB_SMI_DISCARD
;
2162 } else if (rdma_cap_ib_switch(port_priv
->device
)) {
2163 /* forward case for switches */
2164 memcpy(response
, recv
, mad_priv_size(response
));
2165 response
->header
.recv_wc
.wc
= &response
->header
.wc
;
2166 response
->header
.recv_wc
.recv_buf
.opa_mad
=
2167 (struct opa_mad
*)response
->mad
;
2168 response
->header
.recv_wc
.recv_buf
.grh
= &response
->grh
;
2170 agent_send_response((const struct ib_mad_hdr
*)response
->mad
,
2173 opa_smi_get_fwd_port(smp
),
2174 qp_info
->qp
->qp_num
,
2175 recv
->header
.wc
.byte_len
,
2178 return IB_SMI_DISCARD
;
2181 return IB_SMI_HANDLE
;
2184 static enum smi_action
2185 handle_smi(struct ib_mad_port_private
*port_priv
,
2186 struct ib_mad_qp_info
*qp_info
,
2189 struct ib_mad_private
*recv
,
2190 struct ib_mad_private
*response
,
2193 struct ib_mad_hdr
*mad_hdr
= (struct ib_mad_hdr
*)recv
->mad
;
2195 if (opa
&& mad_hdr
->base_version
== OPA_MGMT_BASE_VERSION
&&
2196 mad_hdr
->class_version
== OPA_SM_CLASS_VERSION
)
2197 return handle_opa_smi(port_priv
, qp_info
, wc
, port_num
, recv
,
2200 return handle_ib_smi(port_priv
, qp_info
, wc
, port_num
, recv
, response
);
2203 static void ib_mad_recv_done(struct ib_cq
*cq
, struct ib_wc
*wc
)
2205 struct ib_mad_port_private
*port_priv
= cq
->cq_context
;
2206 struct ib_mad_list_head
*mad_list
=
2207 container_of(wc
->wr_cqe
, struct ib_mad_list_head
, cqe
);
2208 struct ib_mad_qp_info
*qp_info
;
2209 struct ib_mad_private_header
*mad_priv_hdr
;
2210 struct ib_mad_private
*recv
, *response
= NULL
;
2211 struct ib_mad_agent_private
*mad_agent
;
2213 int ret
= IB_MAD_RESULT_SUCCESS
;
2215 u16 resp_mad_pkey_index
= 0;
2218 if (list_empty_careful(&port_priv
->port_list
))
2221 if (wc
->status
!= IB_WC_SUCCESS
) {
2223 * Receive errors indicate that the QP has entered the error
2224 * state - error handling/shutdown code will cleanup
2229 qp_info
= mad_list
->mad_queue
->qp_info
;
2230 dequeue_mad(mad_list
);
2232 opa
= rdma_cap_opa_mad(qp_info
->port_priv
->device
,
2233 qp_info
->port_priv
->port_num
);
2235 mad_priv_hdr
= container_of(mad_list
, struct ib_mad_private_header
,
2237 recv
= container_of(mad_priv_hdr
, struct ib_mad_private
, header
);
2238 ib_dma_unmap_single(port_priv
->device
,
2239 recv
->header
.mapping
,
2240 mad_priv_dma_size(recv
),
2243 /* Setup MAD receive work completion from "normal" work completion */
2244 recv
->header
.wc
= *wc
;
2245 recv
->header
.recv_wc
.wc
= &recv
->header
.wc
;
2247 if (opa
&& ((struct ib_mad_hdr
*)(recv
->mad
))->base_version
== OPA_MGMT_BASE_VERSION
) {
2248 recv
->header
.recv_wc
.mad_len
= wc
->byte_len
- sizeof(struct ib_grh
);
2249 recv
->header
.recv_wc
.mad_seg_size
= sizeof(struct opa_mad
);
2251 recv
->header
.recv_wc
.mad_len
= sizeof(struct ib_mad
);
2252 recv
->header
.recv_wc
.mad_seg_size
= sizeof(struct ib_mad
);
2255 recv
->header
.recv_wc
.recv_buf
.mad
= (struct ib_mad
*)recv
->mad
;
2256 recv
->header
.recv_wc
.recv_buf
.grh
= &recv
->grh
;
2258 if (atomic_read(&qp_info
->snoop_count
))
2259 snoop_recv(qp_info
, &recv
->header
.recv_wc
, IB_MAD_SNOOP_RECVS
);
2262 if (!validate_mad((const struct ib_mad_hdr
*)recv
->mad
, qp_info
, opa
))
2265 mad_size
= recv
->mad_size
;
2266 response
= alloc_mad_private(mad_size
, GFP_KERNEL
);
2270 if (rdma_cap_ib_switch(port_priv
->device
))
2271 port_num
= wc
->port_num
;
2273 port_num
= port_priv
->port_num
;
2275 if (((struct ib_mad_hdr
*)recv
->mad
)->mgmt_class
==
2276 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
) {
2277 if (handle_smi(port_priv
, qp_info
, wc
, port_num
, recv
,
2283 /* Give driver "right of first refusal" on incoming MAD */
2284 if (port_priv
->device
->process_mad
) {
2285 ret
= port_priv
->device
->process_mad(port_priv
->device
, 0,
2286 port_priv
->port_num
,
2288 (const struct ib_mad_hdr
*)recv
->mad
,
2290 (struct ib_mad_hdr
*)response
->mad
,
2291 &mad_size
, &resp_mad_pkey_index
);
2294 wc
->pkey_index
= resp_mad_pkey_index
;
2296 if (ret
& IB_MAD_RESULT_SUCCESS
) {
2297 if (ret
& IB_MAD_RESULT_CONSUMED
)
2299 if (ret
& IB_MAD_RESULT_REPLY
) {
2300 agent_send_response((const struct ib_mad_hdr
*)response
->mad
,
2304 qp_info
->qp
->qp_num
,
2311 mad_agent
= find_mad_agent(port_priv
, (const struct ib_mad_hdr
*)recv
->mad
);
2313 ib_mad_complete_recv(mad_agent
, &recv
->header
.recv_wc
);
2315 * recv is freed up in error cases in ib_mad_complete_recv
2316 * or via recv_handler in ib_mad_complete_recv()
2319 } else if ((ret
& IB_MAD_RESULT_SUCCESS
) &&
2320 generate_unmatched_resp(recv
, response
, &mad_size
, opa
)) {
2321 agent_send_response((const struct ib_mad_hdr
*)response
->mad
, &recv
->grh
, wc
,
2322 port_priv
->device
, port_num
,
2323 qp_info
->qp
->qp_num
, mad_size
, opa
);
2327 /* Post another receive request for this QP */
2329 ib_mad_post_receive_mads(qp_info
, response
);
2332 ib_mad_post_receive_mads(qp_info
, recv
);
2335 static void adjust_timeout(struct ib_mad_agent_private
*mad_agent_priv
)
2337 struct ib_mad_send_wr_private
*mad_send_wr
;
2338 unsigned long delay
;
2340 if (list_empty(&mad_agent_priv
->wait_list
)) {
2341 cancel_delayed_work(&mad_agent_priv
->timed_work
);
2343 mad_send_wr
= list_entry(mad_agent_priv
->wait_list
.next
,
2344 struct ib_mad_send_wr_private
,
2347 if (time_after(mad_agent_priv
->timeout
,
2348 mad_send_wr
->timeout
)) {
2349 mad_agent_priv
->timeout
= mad_send_wr
->timeout
;
2350 delay
= mad_send_wr
->timeout
- jiffies
;
2351 if ((long)delay
<= 0)
2353 mod_delayed_work(mad_agent_priv
->qp_info
->port_priv
->wq
,
2354 &mad_agent_priv
->timed_work
, delay
);
2359 static void wait_for_response(struct ib_mad_send_wr_private
*mad_send_wr
)
2361 struct ib_mad_agent_private
*mad_agent_priv
;
2362 struct ib_mad_send_wr_private
*temp_mad_send_wr
;
2363 struct list_head
*list_item
;
2364 unsigned long delay
;
2366 mad_agent_priv
= mad_send_wr
->mad_agent_priv
;
2367 list_del(&mad_send_wr
->agent_list
);
2369 delay
= mad_send_wr
->timeout
;
2370 mad_send_wr
->timeout
+= jiffies
;
2373 list_for_each_prev(list_item
, &mad_agent_priv
->wait_list
) {
2374 temp_mad_send_wr
= list_entry(list_item
,
2375 struct ib_mad_send_wr_private
,
2377 if (time_after(mad_send_wr
->timeout
,
2378 temp_mad_send_wr
->timeout
))
2383 list_item
= &mad_agent_priv
->wait_list
;
2384 list_add(&mad_send_wr
->agent_list
, list_item
);
2386 /* Reschedule a work item if we have a shorter timeout */
2387 if (mad_agent_priv
->wait_list
.next
== &mad_send_wr
->agent_list
)
2388 mod_delayed_work(mad_agent_priv
->qp_info
->port_priv
->wq
,
2389 &mad_agent_priv
->timed_work
, delay
);
2392 void ib_reset_mad_timeout(struct ib_mad_send_wr_private
*mad_send_wr
,
2395 mad_send_wr
->timeout
= msecs_to_jiffies(timeout_ms
);
2396 wait_for_response(mad_send_wr
);
2400 * Process a send work completion
2402 void ib_mad_complete_send_wr(struct ib_mad_send_wr_private
*mad_send_wr
,
2403 struct ib_mad_send_wc
*mad_send_wc
)
2405 struct ib_mad_agent_private
*mad_agent_priv
;
2406 unsigned long flags
;
2409 mad_agent_priv
= mad_send_wr
->mad_agent_priv
;
2410 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2411 if (ib_mad_kernel_rmpp_agent(&mad_agent_priv
->agent
)) {
2412 ret
= ib_process_rmpp_send_wc(mad_send_wr
, mad_send_wc
);
2413 if (ret
== IB_RMPP_RESULT_CONSUMED
)
2416 ret
= IB_RMPP_RESULT_UNHANDLED
;
2418 if (mad_send_wc
->status
!= IB_WC_SUCCESS
&&
2419 mad_send_wr
->status
== IB_WC_SUCCESS
) {
2420 mad_send_wr
->status
= mad_send_wc
->status
;
2421 mad_send_wr
->refcount
-= (mad_send_wr
->timeout
> 0);
2424 if (--mad_send_wr
->refcount
> 0) {
2425 if (mad_send_wr
->refcount
== 1 && mad_send_wr
->timeout
&&
2426 mad_send_wr
->status
== IB_WC_SUCCESS
) {
2427 wait_for_response(mad_send_wr
);
2432 /* Remove send from MAD agent and notify client of completion */
2433 list_del(&mad_send_wr
->agent_list
);
2434 adjust_timeout(mad_agent_priv
);
2435 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2437 if (mad_send_wr
->status
!= IB_WC_SUCCESS
)
2438 mad_send_wc
->status
= mad_send_wr
->status
;
2439 if (ret
== IB_RMPP_RESULT_INTERNAL
)
2440 ib_rmpp_send_handler(mad_send_wc
);
2442 mad_agent_priv
->agent
.send_handler(&mad_agent_priv
->agent
,
2445 /* Release reference on agent taken when sending */
2446 deref_mad_agent(mad_agent_priv
);
2449 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2452 static void ib_mad_send_done(struct ib_cq
*cq
, struct ib_wc
*wc
)
2454 struct ib_mad_port_private
*port_priv
= cq
->cq_context
;
2455 struct ib_mad_list_head
*mad_list
=
2456 container_of(wc
->wr_cqe
, struct ib_mad_list_head
, cqe
);
2457 struct ib_mad_send_wr_private
*mad_send_wr
, *queued_send_wr
;
2458 struct ib_mad_qp_info
*qp_info
;
2459 struct ib_mad_queue
*send_queue
;
2460 struct ib_send_wr
*bad_send_wr
;
2461 struct ib_mad_send_wc mad_send_wc
;
2462 unsigned long flags
;
2465 if (list_empty_careful(&port_priv
->port_list
))
2468 if (wc
->status
!= IB_WC_SUCCESS
) {
2469 if (!ib_mad_send_error(port_priv
, wc
))
2473 mad_send_wr
= container_of(mad_list
, struct ib_mad_send_wr_private
,
2475 send_queue
= mad_list
->mad_queue
;
2476 qp_info
= send_queue
->qp_info
;
2479 ib_dma_unmap_single(mad_send_wr
->send_buf
.mad_agent
->device
,
2480 mad_send_wr
->header_mapping
,
2481 mad_send_wr
->sg_list
[0].length
, DMA_TO_DEVICE
);
2482 ib_dma_unmap_single(mad_send_wr
->send_buf
.mad_agent
->device
,
2483 mad_send_wr
->payload_mapping
,
2484 mad_send_wr
->sg_list
[1].length
, DMA_TO_DEVICE
);
2485 queued_send_wr
= NULL
;
2486 spin_lock_irqsave(&send_queue
->lock
, flags
);
2487 list_del(&mad_list
->list
);
2489 /* Move queued send to the send queue */
2490 if (send_queue
->count
-- > send_queue
->max_active
) {
2491 mad_list
= container_of(qp_info
->overflow_list
.next
,
2492 struct ib_mad_list_head
, list
);
2493 queued_send_wr
= container_of(mad_list
,
2494 struct ib_mad_send_wr_private
,
2496 list_move_tail(&mad_list
->list
, &send_queue
->list
);
2498 spin_unlock_irqrestore(&send_queue
->lock
, flags
);
2500 mad_send_wc
.send_buf
= &mad_send_wr
->send_buf
;
2501 mad_send_wc
.status
= wc
->status
;
2502 mad_send_wc
.vendor_err
= wc
->vendor_err
;
2503 if (atomic_read(&qp_info
->snoop_count
))
2504 snoop_send(qp_info
, &mad_send_wr
->send_buf
, &mad_send_wc
,
2505 IB_MAD_SNOOP_SEND_COMPLETIONS
);
2506 ib_mad_complete_send_wr(mad_send_wr
, &mad_send_wc
);
2508 if (queued_send_wr
) {
2509 ret
= ib_post_send(qp_info
->qp
, &queued_send_wr
->send_wr
.wr
,
2512 dev_err(&port_priv
->device
->dev
,
2513 "ib_post_send failed: %d\n", ret
);
2514 mad_send_wr
= queued_send_wr
;
2515 wc
->status
= IB_WC_LOC_QP_OP_ERR
;
2521 static void mark_sends_for_retry(struct ib_mad_qp_info
*qp_info
)
2523 struct ib_mad_send_wr_private
*mad_send_wr
;
2524 struct ib_mad_list_head
*mad_list
;
2525 unsigned long flags
;
2527 spin_lock_irqsave(&qp_info
->send_queue
.lock
, flags
);
2528 list_for_each_entry(mad_list
, &qp_info
->send_queue
.list
, list
) {
2529 mad_send_wr
= container_of(mad_list
,
2530 struct ib_mad_send_wr_private
,
2532 mad_send_wr
->retry
= 1;
2534 spin_unlock_irqrestore(&qp_info
->send_queue
.lock
, flags
);
2537 static bool ib_mad_send_error(struct ib_mad_port_private
*port_priv
,
2540 struct ib_mad_list_head
*mad_list
=
2541 container_of(wc
->wr_cqe
, struct ib_mad_list_head
, cqe
);
2542 struct ib_mad_qp_info
*qp_info
= mad_list
->mad_queue
->qp_info
;
2543 struct ib_mad_send_wr_private
*mad_send_wr
;
2547 * Send errors will transition the QP to SQE - move
2548 * QP to RTS and repost flushed work requests
2550 mad_send_wr
= container_of(mad_list
, struct ib_mad_send_wr_private
,
2552 if (wc
->status
== IB_WC_WR_FLUSH_ERR
) {
2553 if (mad_send_wr
->retry
) {
2555 struct ib_send_wr
*bad_send_wr
;
2557 mad_send_wr
->retry
= 0;
2558 ret
= ib_post_send(qp_info
->qp
, &mad_send_wr
->send_wr
.wr
,
2564 struct ib_qp_attr
*attr
;
2566 /* Transition QP to RTS and fail offending send */
2567 attr
= kmalloc(sizeof *attr
, GFP_KERNEL
);
2569 attr
->qp_state
= IB_QPS_RTS
;
2570 attr
->cur_qp_state
= IB_QPS_SQE
;
2571 ret
= ib_modify_qp(qp_info
->qp
, attr
,
2572 IB_QP_STATE
| IB_QP_CUR_STATE
);
2575 dev_err(&port_priv
->device
->dev
,
2576 "%s - ib_modify_qp to RTS: %d\n",
2579 mark_sends_for_retry(qp_info
);
2586 static void cancel_mads(struct ib_mad_agent_private
*mad_agent_priv
)
2588 unsigned long flags
;
2589 struct ib_mad_send_wr_private
*mad_send_wr
, *temp_mad_send_wr
;
2590 struct ib_mad_send_wc mad_send_wc
;
2591 struct list_head cancel_list
;
2593 INIT_LIST_HEAD(&cancel_list
);
2595 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2596 list_for_each_entry_safe(mad_send_wr
, temp_mad_send_wr
,
2597 &mad_agent_priv
->send_list
, agent_list
) {
2598 if (mad_send_wr
->status
== IB_WC_SUCCESS
) {
2599 mad_send_wr
->status
= IB_WC_WR_FLUSH_ERR
;
2600 mad_send_wr
->refcount
-= (mad_send_wr
->timeout
> 0);
2604 /* Empty wait list to prevent receives from finding a request */
2605 list_splice_init(&mad_agent_priv
->wait_list
, &cancel_list
);
2606 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2608 /* Report all cancelled requests */
2609 mad_send_wc
.status
= IB_WC_WR_FLUSH_ERR
;
2610 mad_send_wc
.vendor_err
= 0;
2612 list_for_each_entry_safe(mad_send_wr
, temp_mad_send_wr
,
2613 &cancel_list
, agent_list
) {
2614 mad_send_wc
.send_buf
= &mad_send_wr
->send_buf
;
2615 list_del(&mad_send_wr
->agent_list
);
2616 mad_agent_priv
->agent
.send_handler(&mad_agent_priv
->agent
,
2618 atomic_dec(&mad_agent_priv
->refcount
);
2622 static struct ib_mad_send_wr_private
*
2623 find_send_wr(struct ib_mad_agent_private
*mad_agent_priv
,
2624 struct ib_mad_send_buf
*send_buf
)
2626 struct ib_mad_send_wr_private
*mad_send_wr
;
2628 list_for_each_entry(mad_send_wr
, &mad_agent_priv
->wait_list
,
2630 if (&mad_send_wr
->send_buf
== send_buf
)
2634 list_for_each_entry(mad_send_wr
, &mad_agent_priv
->send_list
,
2636 if (is_rmpp_data_mad(mad_agent_priv
,
2637 mad_send_wr
->send_buf
.mad
) &&
2638 &mad_send_wr
->send_buf
== send_buf
)
2644 int ib_modify_mad(struct ib_mad_agent
*mad_agent
,
2645 struct ib_mad_send_buf
*send_buf
, u32 timeout_ms
)
2647 struct ib_mad_agent_private
*mad_agent_priv
;
2648 struct ib_mad_send_wr_private
*mad_send_wr
;
2649 unsigned long flags
;
2652 mad_agent_priv
= container_of(mad_agent
, struct ib_mad_agent_private
,
2654 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2655 mad_send_wr
= find_send_wr(mad_agent_priv
, send_buf
);
2656 if (!mad_send_wr
|| mad_send_wr
->status
!= IB_WC_SUCCESS
) {
2657 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2661 active
= (!mad_send_wr
->timeout
|| mad_send_wr
->refcount
> 1);
2663 mad_send_wr
->status
= IB_WC_WR_FLUSH_ERR
;
2664 mad_send_wr
->refcount
-= (mad_send_wr
->timeout
> 0);
2667 mad_send_wr
->send_buf
.timeout_ms
= timeout_ms
;
2669 mad_send_wr
->timeout
= msecs_to_jiffies(timeout_ms
);
2671 ib_reset_mad_timeout(mad_send_wr
, timeout_ms
);
2673 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2676 EXPORT_SYMBOL(ib_modify_mad
);
2678 void ib_cancel_mad(struct ib_mad_agent
*mad_agent
,
2679 struct ib_mad_send_buf
*send_buf
)
2681 ib_modify_mad(mad_agent
, send_buf
, 0);
2683 EXPORT_SYMBOL(ib_cancel_mad
);
2685 static void local_completions(struct work_struct
*work
)
2687 struct ib_mad_agent_private
*mad_agent_priv
;
2688 struct ib_mad_local_private
*local
;
2689 struct ib_mad_agent_private
*recv_mad_agent
;
2690 unsigned long flags
;
2693 struct ib_mad_send_wc mad_send_wc
;
2697 container_of(work
, struct ib_mad_agent_private
, local_work
);
2699 opa
= rdma_cap_opa_mad(mad_agent_priv
->qp_info
->port_priv
->device
,
2700 mad_agent_priv
->qp_info
->port_priv
->port_num
);
2702 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2703 while (!list_empty(&mad_agent_priv
->local_list
)) {
2704 local
= list_entry(mad_agent_priv
->local_list
.next
,
2705 struct ib_mad_local_private
,
2707 list_del(&local
->completion_list
);
2708 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2710 if (local
->mad_priv
) {
2712 recv_mad_agent
= local
->recv_mad_agent
;
2713 if (!recv_mad_agent
) {
2714 dev_err(&mad_agent_priv
->agent
.device
->dev
,
2715 "No receive MAD agent for local completion\n");
2717 goto local_send_completion
;
2721 * Defined behavior is to complete response
2724 build_smp_wc(recv_mad_agent
->agent
.qp
,
2725 local
->mad_send_wr
->send_wr
.wr
.wr_cqe
,
2726 be16_to_cpu(IB_LID_PERMISSIVE
),
2727 local
->mad_send_wr
->send_wr
.pkey_index
,
2728 recv_mad_agent
->agent
.port_num
, &wc
);
2730 local
->mad_priv
->header
.recv_wc
.wc
= &wc
;
2732 base_version
= ((struct ib_mad_hdr
*)(local
->mad_priv
->mad
))->base_version
;
2733 if (opa
&& base_version
== OPA_MGMT_BASE_VERSION
) {
2734 local
->mad_priv
->header
.recv_wc
.mad_len
= local
->return_wc_byte_len
;
2735 local
->mad_priv
->header
.recv_wc
.mad_seg_size
= sizeof(struct opa_mad
);
2737 local
->mad_priv
->header
.recv_wc
.mad_len
= sizeof(struct ib_mad
);
2738 local
->mad_priv
->header
.recv_wc
.mad_seg_size
= sizeof(struct ib_mad
);
2741 INIT_LIST_HEAD(&local
->mad_priv
->header
.recv_wc
.rmpp_list
);
2742 list_add(&local
->mad_priv
->header
.recv_wc
.recv_buf
.list
,
2743 &local
->mad_priv
->header
.recv_wc
.rmpp_list
);
2744 local
->mad_priv
->header
.recv_wc
.recv_buf
.grh
= NULL
;
2745 local
->mad_priv
->header
.recv_wc
.recv_buf
.mad
=
2746 (struct ib_mad
*)local
->mad_priv
->mad
;
2747 if (atomic_read(&recv_mad_agent
->qp_info
->snoop_count
))
2748 snoop_recv(recv_mad_agent
->qp_info
,
2749 &local
->mad_priv
->header
.recv_wc
,
2750 IB_MAD_SNOOP_RECVS
);
2751 recv_mad_agent
->agent
.recv_handler(
2752 &recv_mad_agent
->agent
,
2753 &local
->mad_send_wr
->send_buf
,
2754 &local
->mad_priv
->header
.recv_wc
);
2755 spin_lock_irqsave(&recv_mad_agent
->lock
, flags
);
2756 atomic_dec(&recv_mad_agent
->refcount
);
2757 spin_unlock_irqrestore(&recv_mad_agent
->lock
, flags
);
2760 local_send_completion
:
2762 mad_send_wc
.status
= IB_WC_SUCCESS
;
2763 mad_send_wc
.vendor_err
= 0;
2764 mad_send_wc
.send_buf
= &local
->mad_send_wr
->send_buf
;
2765 if (atomic_read(&mad_agent_priv
->qp_info
->snoop_count
))
2766 snoop_send(mad_agent_priv
->qp_info
,
2767 &local
->mad_send_wr
->send_buf
,
2768 &mad_send_wc
, IB_MAD_SNOOP_SEND_COMPLETIONS
);
2769 mad_agent_priv
->agent
.send_handler(&mad_agent_priv
->agent
,
2772 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2773 atomic_dec(&mad_agent_priv
->refcount
);
2775 kfree(local
->mad_priv
);
2778 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2781 static int retry_send(struct ib_mad_send_wr_private
*mad_send_wr
)
2785 if (!mad_send_wr
->retries_left
)
2788 mad_send_wr
->retries_left
--;
2789 mad_send_wr
->send_buf
.retries
++;
2791 mad_send_wr
->timeout
= msecs_to_jiffies(mad_send_wr
->send_buf
.timeout_ms
);
2793 if (ib_mad_kernel_rmpp_agent(&mad_send_wr
->mad_agent_priv
->agent
)) {
2794 ret
= ib_retry_rmpp(mad_send_wr
);
2796 case IB_RMPP_RESULT_UNHANDLED
:
2797 ret
= ib_send_mad(mad_send_wr
);
2799 case IB_RMPP_RESULT_CONSUMED
:
2807 ret
= ib_send_mad(mad_send_wr
);
2810 mad_send_wr
->refcount
++;
2811 list_add_tail(&mad_send_wr
->agent_list
,
2812 &mad_send_wr
->mad_agent_priv
->send_list
);
2817 static void timeout_sends(struct work_struct
*work
)
2819 struct ib_mad_agent_private
*mad_agent_priv
;
2820 struct ib_mad_send_wr_private
*mad_send_wr
;
2821 struct ib_mad_send_wc mad_send_wc
;
2822 unsigned long flags
, delay
;
2824 mad_agent_priv
= container_of(work
, struct ib_mad_agent_private
,
2826 mad_send_wc
.vendor_err
= 0;
2828 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2829 while (!list_empty(&mad_agent_priv
->wait_list
)) {
2830 mad_send_wr
= list_entry(mad_agent_priv
->wait_list
.next
,
2831 struct ib_mad_send_wr_private
,
2834 if (time_after(mad_send_wr
->timeout
, jiffies
)) {
2835 delay
= mad_send_wr
->timeout
- jiffies
;
2836 if ((long)delay
<= 0)
2838 queue_delayed_work(mad_agent_priv
->qp_info
->
2840 &mad_agent_priv
->timed_work
, delay
);
2844 list_del(&mad_send_wr
->agent_list
);
2845 if (mad_send_wr
->status
== IB_WC_SUCCESS
&&
2846 !retry_send(mad_send_wr
))
2849 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2851 if (mad_send_wr
->status
== IB_WC_SUCCESS
)
2852 mad_send_wc
.status
= IB_WC_RESP_TIMEOUT_ERR
;
2854 mad_send_wc
.status
= mad_send_wr
->status
;
2855 mad_send_wc
.send_buf
= &mad_send_wr
->send_buf
;
2856 mad_agent_priv
->agent
.send_handler(&mad_agent_priv
->agent
,
2859 atomic_dec(&mad_agent_priv
->refcount
);
2860 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2862 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2866 * Allocate receive MADs and post receive WRs for them
2868 static int ib_mad_post_receive_mads(struct ib_mad_qp_info
*qp_info
,
2869 struct ib_mad_private
*mad
)
2871 unsigned long flags
;
2873 struct ib_mad_private
*mad_priv
;
2874 struct ib_sge sg_list
;
2875 struct ib_recv_wr recv_wr
, *bad_recv_wr
;
2876 struct ib_mad_queue
*recv_queue
= &qp_info
->recv_queue
;
2878 /* Initialize common scatter list fields */
2879 sg_list
.lkey
= qp_info
->port_priv
->pd
->local_dma_lkey
;
2881 /* Initialize common receive WR fields */
2882 recv_wr
.next
= NULL
;
2883 recv_wr
.sg_list
= &sg_list
;
2884 recv_wr
.num_sge
= 1;
2887 /* Allocate and map receive buffer */
2892 mad_priv
= alloc_mad_private(port_mad_size(qp_info
->port_priv
),
2899 sg_list
.length
= mad_priv_dma_size(mad_priv
);
2900 sg_list
.addr
= ib_dma_map_single(qp_info
->port_priv
->device
,
2902 mad_priv_dma_size(mad_priv
),
2904 if (unlikely(ib_dma_mapping_error(qp_info
->port_priv
->device
,
2909 mad_priv
->header
.mapping
= sg_list
.addr
;
2910 mad_priv
->header
.mad_list
.mad_queue
= recv_queue
;
2911 mad_priv
->header
.mad_list
.cqe
.done
= ib_mad_recv_done
;
2912 recv_wr
.wr_cqe
= &mad_priv
->header
.mad_list
.cqe
;
2914 /* Post receive WR */
2915 spin_lock_irqsave(&recv_queue
->lock
, flags
);
2916 post
= (++recv_queue
->count
< recv_queue
->max_active
);
2917 list_add_tail(&mad_priv
->header
.mad_list
.list
, &recv_queue
->list
);
2918 spin_unlock_irqrestore(&recv_queue
->lock
, flags
);
2919 ret
= ib_post_recv(qp_info
->qp
, &recv_wr
, &bad_recv_wr
);
2921 spin_lock_irqsave(&recv_queue
->lock
, flags
);
2922 list_del(&mad_priv
->header
.mad_list
.list
);
2923 recv_queue
->count
--;
2924 spin_unlock_irqrestore(&recv_queue
->lock
, flags
);
2925 ib_dma_unmap_single(qp_info
->port_priv
->device
,
2926 mad_priv
->header
.mapping
,
2927 mad_priv_dma_size(mad_priv
),
2930 dev_err(&qp_info
->port_priv
->device
->dev
,
2931 "ib_post_recv failed: %d\n", ret
);
2940 * Return all the posted receive MADs
2942 static void cleanup_recv_queue(struct ib_mad_qp_info
*qp_info
)
2944 struct ib_mad_private_header
*mad_priv_hdr
;
2945 struct ib_mad_private
*recv
;
2946 struct ib_mad_list_head
*mad_list
;
2951 while (!list_empty(&qp_info
->recv_queue
.list
)) {
2953 mad_list
= list_entry(qp_info
->recv_queue
.list
.next
,
2954 struct ib_mad_list_head
, list
);
2955 mad_priv_hdr
= container_of(mad_list
,
2956 struct ib_mad_private_header
,
2958 recv
= container_of(mad_priv_hdr
, struct ib_mad_private
,
2961 /* Remove from posted receive MAD list */
2962 list_del(&mad_list
->list
);
2964 ib_dma_unmap_single(qp_info
->port_priv
->device
,
2965 recv
->header
.mapping
,
2966 mad_priv_dma_size(recv
),
2971 qp_info
->recv_queue
.count
= 0;
2977 static int ib_mad_port_start(struct ib_mad_port_private
*port_priv
)
2980 struct ib_qp_attr
*attr
;
2984 attr
= kmalloc(sizeof *attr
, GFP_KERNEL
);
2988 ret
= ib_find_pkey(port_priv
->device
, port_priv
->port_num
,
2989 IB_DEFAULT_PKEY_FULL
, &pkey_index
);
2993 for (i
= 0; i
< IB_MAD_QPS_CORE
; i
++) {
2994 qp
= port_priv
->qp_info
[i
].qp
;
2999 * PKey index for QP1 is irrelevant but
3000 * one is needed for the Reset to Init transition
3002 attr
->qp_state
= IB_QPS_INIT
;
3003 attr
->pkey_index
= pkey_index
;
3004 attr
->qkey
= (qp
->qp_num
== 0) ? 0 : IB_QP1_QKEY
;
3005 ret
= ib_modify_qp(qp
, attr
, IB_QP_STATE
|
3006 IB_QP_PKEY_INDEX
| IB_QP_QKEY
);
3008 dev_err(&port_priv
->device
->dev
,
3009 "Couldn't change QP%d state to INIT: %d\n",
3014 attr
->qp_state
= IB_QPS_RTR
;
3015 ret
= ib_modify_qp(qp
, attr
, IB_QP_STATE
);
3017 dev_err(&port_priv
->device
->dev
,
3018 "Couldn't change QP%d state to RTR: %d\n",
3023 attr
->qp_state
= IB_QPS_RTS
;
3024 attr
->sq_psn
= IB_MAD_SEND_Q_PSN
;
3025 ret
= ib_modify_qp(qp
, attr
, IB_QP_STATE
| IB_QP_SQ_PSN
);
3027 dev_err(&port_priv
->device
->dev
,
3028 "Couldn't change QP%d state to RTS: %d\n",
3034 ret
= ib_req_notify_cq(port_priv
->cq
, IB_CQ_NEXT_COMP
);
3036 dev_err(&port_priv
->device
->dev
,
3037 "Failed to request completion notification: %d\n",
3042 for (i
= 0; i
< IB_MAD_QPS_CORE
; i
++) {
3043 if (!port_priv
->qp_info
[i
].qp
)
3046 ret
= ib_mad_post_receive_mads(&port_priv
->qp_info
[i
], NULL
);
3048 dev_err(&port_priv
->device
->dev
,
3049 "Couldn't post receive WRs\n");
3058 static void qp_event_handler(struct ib_event
*event
, void *qp_context
)
3060 struct ib_mad_qp_info
*qp_info
= qp_context
;
3062 /* It's worse than that! He's dead, Jim! */
3063 dev_err(&qp_info
->port_priv
->device
->dev
,
3064 "Fatal error (%d) on MAD QP (%d)\n",
3065 event
->event
, qp_info
->qp
->qp_num
);
3068 static void init_mad_queue(struct ib_mad_qp_info
*qp_info
,
3069 struct ib_mad_queue
*mad_queue
)
3071 mad_queue
->qp_info
= qp_info
;
3072 mad_queue
->count
= 0;
3073 spin_lock_init(&mad_queue
->lock
);
3074 INIT_LIST_HEAD(&mad_queue
->list
);
3077 static void init_mad_qp(struct ib_mad_port_private
*port_priv
,
3078 struct ib_mad_qp_info
*qp_info
)
3080 qp_info
->port_priv
= port_priv
;
3081 init_mad_queue(qp_info
, &qp_info
->send_queue
);
3082 init_mad_queue(qp_info
, &qp_info
->recv_queue
);
3083 INIT_LIST_HEAD(&qp_info
->overflow_list
);
3084 spin_lock_init(&qp_info
->snoop_lock
);
3085 qp_info
->snoop_table
= NULL
;
3086 qp_info
->snoop_table_size
= 0;
3087 atomic_set(&qp_info
->snoop_count
, 0);
3090 static int create_mad_qp(struct ib_mad_qp_info
*qp_info
,
3091 enum ib_qp_type qp_type
)
3093 struct ib_qp_init_attr qp_init_attr
;
3096 memset(&qp_init_attr
, 0, sizeof qp_init_attr
);
3097 qp_init_attr
.send_cq
= qp_info
->port_priv
->cq
;
3098 qp_init_attr
.recv_cq
= qp_info
->port_priv
->cq
;
3099 qp_init_attr
.sq_sig_type
= IB_SIGNAL_ALL_WR
;
3100 qp_init_attr
.cap
.max_send_wr
= mad_sendq_size
;
3101 qp_init_attr
.cap
.max_recv_wr
= mad_recvq_size
;
3102 qp_init_attr
.cap
.max_send_sge
= IB_MAD_SEND_REQ_MAX_SG
;
3103 qp_init_attr
.cap
.max_recv_sge
= IB_MAD_RECV_REQ_MAX_SG
;
3104 qp_init_attr
.qp_type
= qp_type
;
3105 qp_init_attr
.port_num
= qp_info
->port_priv
->port_num
;
3106 qp_init_attr
.qp_context
= qp_info
;
3107 qp_init_attr
.event_handler
= qp_event_handler
;
3108 qp_info
->qp
= ib_create_qp(qp_info
->port_priv
->pd
, &qp_init_attr
);
3109 if (IS_ERR(qp_info
->qp
)) {
3110 dev_err(&qp_info
->port_priv
->device
->dev
,
3111 "Couldn't create ib_mad QP%d\n",
3112 get_spl_qp_index(qp_type
));
3113 ret
= PTR_ERR(qp_info
->qp
);
3116 /* Use minimum queue sizes unless the CQ is resized */
3117 qp_info
->send_queue
.max_active
= mad_sendq_size
;
3118 qp_info
->recv_queue
.max_active
= mad_recvq_size
;
3125 static void destroy_mad_qp(struct ib_mad_qp_info
*qp_info
)
3130 ib_destroy_qp(qp_info
->qp
);
3131 kfree(qp_info
->snoop_table
);
3136 * Create the QP, PD, MR, and CQ if needed
3138 static int ib_mad_port_open(struct ib_device
*device
,
3142 struct ib_mad_port_private
*port_priv
;
3143 unsigned long flags
;
3144 char name
[sizeof "ib_mad123"];
3147 if (WARN_ON(rdma_max_mad_size(device
, port_num
) < IB_MGMT_MAD_SIZE
))
3150 if (WARN_ON(rdma_cap_opa_mad(device
, port_num
) &&
3151 rdma_max_mad_size(device
, port_num
) < OPA_MGMT_MAD_SIZE
))
3154 /* Create new device info */
3155 port_priv
= kzalloc(sizeof *port_priv
, GFP_KERNEL
);
3159 port_priv
->device
= device
;
3160 port_priv
->port_num
= port_num
;
3161 spin_lock_init(&port_priv
->reg_lock
);
3162 INIT_LIST_HEAD(&port_priv
->agent_list
);
3163 init_mad_qp(port_priv
, &port_priv
->qp_info
[0]);
3164 init_mad_qp(port_priv
, &port_priv
->qp_info
[1]);
3166 cq_size
= mad_sendq_size
+ mad_recvq_size
;
3167 has_smi
= rdma_cap_ib_smi(device
, port_num
);
3171 port_priv
->cq
= ib_alloc_cq(port_priv
->device
, port_priv
, cq_size
, 0,
3173 if (IS_ERR(port_priv
->cq
)) {
3174 dev_err(&device
->dev
, "Couldn't create ib_mad CQ\n");
3175 ret
= PTR_ERR(port_priv
->cq
);
3179 port_priv
->pd
= ib_alloc_pd(device
, 0);
3180 if (IS_ERR(port_priv
->pd
)) {
3181 dev_err(&device
->dev
, "Couldn't create ib_mad PD\n");
3182 ret
= PTR_ERR(port_priv
->pd
);
3187 ret
= create_mad_qp(&port_priv
->qp_info
[0], IB_QPT_SMI
);
3191 ret
= create_mad_qp(&port_priv
->qp_info
[1], IB_QPT_GSI
);
3195 snprintf(name
, sizeof name
, "ib_mad%d", port_num
);
3196 port_priv
->wq
= alloc_ordered_workqueue(name
, WQ_MEM_RECLAIM
);
3197 if (!port_priv
->wq
) {
3202 spin_lock_irqsave(&ib_mad_port_list_lock
, flags
);
3203 list_add_tail(&port_priv
->port_list
, &ib_mad_port_list
);
3204 spin_unlock_irqrestore(&ib_mad_port_list_lock
, flags
);
3206 ret
= ib_mad_port_start(port_priv
);
3208 dev_err(&device
->dev
, "Couldn't start port\n");
3215 spin_lock_irqsave(&ib_mad_port_list_lock
, flags
);
3216 list_del_init(&port_priv
->port_list
);
3217 spin_unlock_irqrestore(&ib_mad_port_list_lock
, flags
);
3219 destroy_workqueue(port_priv
->wq
);
3221 destroy_mad_qp(&port_priv
->qp_info
[1]);
3223 destroy_mad_qp(&port_priv
->qp_info
[0]);
3225 ib_dealloc_pd(port_priv
->pd
);
3227 ib_free_cq(port_priv
->cq
);
3228 cleanup_recv_queue(&port_priv
->qp_info
[1]);
3229 cleanup_recv_queue(&port_priv
->qp_info
[0]);
3238 * If there are no classes using the port, free the port
3239 * resources (CQ, MR, PD, QP) and remove the port's info structure
3241 static int ib_mad_port_close(struct ib_device
*device
, int port_num
)
3243 struct ib_mad_port_private
*port_priv
;
3244 unsigned long flags
;
3246 spin_lock_irqsave(&ib_mad_port_list_lock
, flags
);
3247 port_priv
= __ib_get_mad_port(device
, port_num
);
3248 if (port_priv
== NULL
) {
3249 spin_unlock_irqrestore(&ib_mad_port_list_lock
, flags
);
3250 dev_err(&device
->dev
, "Port %d not found\n", port_num
);
3253 list_del_init(&port_priv
->port_list
);
3254 spin_unlock_irqrestore(&ib_mad_port_list_lock
, flags
);
3256 destroy_workqueue(port_priv
->wq
);
3257 destroy_mad_qp(&port_priv
->qp_info
[1]);
3258 destroy_mad_qp(&port_priv
->qp_info
[0]);
3259 ib_dealloc_pd(port_priv
->pd
);
3260 ib_free_cq(port_priv
->cq
);
3261 cleanup_recv_queue(&port_priv
->qp_info
[1]);
3262 cleanup_recv_queue(&port_priv
->qp_info
[0]);
3263 /* XXX: Handle deallocation of MAD registration tables */
3270 static void ib_mad_init_device(struct ib_device
*device
)
3274 start
= rdma_start_port(device
);
3276 for (i
= start
; i
<= rdma_end_port(device
); i
++) {
3277 if (!rdma_cap_ib_mad(device
, i
))
3280 if (ib_mad_port_open(device
, i
)) {
3281 dev_err(&device
->dev
, "Couldn't open port %d\n", i
);
3284 if (ib_agent_port_open(device
, i
)) {
3285 dev_err(&device
->dev
,
3286 "Couldn't open port %d for agents\n", i
);
3293 if (ib_mad_port_close(device
, i
))
3294 dev_err(&device
->dev
, "Couldn't close port %d\n", i
);
3297 while (--i
>= start
) {
3298 if (!rdma_cap_ib_mad(device
, i
))
3301 if (ib_agent_port_close(device
, i
))
3302 dev_err(&device
->dev
,
3303 "Couldn't close port %d for agents\n", i
);
3304 if (ib_mad_port_close(device
, i
))
3305 dev_err(&device
->dev
, "Couldn't close port %d\n", i
);
3309 static void ib_mad_remove_device(struct ib_device
*device
, void *client_data
)
3313 for (i
= rdma_start_port(device
); i
<= rdma_end_port(device
); i
++) {
3314 if (!rdma_cap_ib_mad(device
, i
))
3317 if (ib_agent_port_close(device
, i
))
3318 dev_err(&device
->dev
,
3319 "Couldn't close port %d for agents\n", i
);
3320 if (ib_mad_port_close(device
, i
))
3321 dev_err(&device
->dev
, "Couldn't close port %d\n", i
);
3325 static struct ib_client mad_client
= {
3327 .add
= ib_mad_init_device
,
3328 .remove
= ib_mad_remove_device
3331 int ib_mad_init(void)
3333 mad_recvq_size
= min(mad_recvq_size
, IB_MAD_QP_MAX_SIZE
);
3334 mad_recvq_size
= max(mad_recvq_size
, IB_MAD_QP_MIN_SIZE
);
3336 mad_sendq_size
= min(mad_sendq_size
, IB_MAD_QP_MAX_SIZE
);
3337 mad_sendq_size
= max(mad_sendq_size
, IB_MAD_QP_MIN_SIZE
);
3339 INIT_LIST_HEAD(&ib_mad_port_list
);
3341 if (ib_register_client(&mad_client
)) {
3342 pr_err("Couldn't register ib_mad client\n");
3349 void ib_mad_cleanup(void)
3351 ib_unregister_client(&mad_client
);