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,2018 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 <linux/xarray.h>
45 #include <rdma/ib_cache.h>
48 #include "core_priv.h"
54 #define CREATE_TRACE_POINTS
55 #include <trace/events/ib_mad.h>
57 #ifdef CONFIG_TRACEPOINTS
58 static void create_mad_addr_info(struct ib_mad_send_wr_private
*mad_send_wr
,
59 struct ib_mad_qp_info
*qp_info
,
60 struct trace_event_raw_ib_mad_send_template
*entry
)
62 struct ib_ud_wr
*wr
= &mad_send_wr
->send_wr
;
63 struct rdma_ah_attr attr
= {};
65 rdma_query_ah(wr
->ah
, &attr
);
67 /* These are common */
69 entry
->rqpn
= wr
->remote_qpn
;
70 entry
->rqkey
= wr
->remote_qkey
;
71 entry
->dlid
= rdma_ah_get_dlid(&attr
);
75 static int mad_sendq_size
= IB_MAD_QP_SEND_SIZE
;
76 static int mad_recvq_size
= IB_MAD_QP_RECV_SIZE
;
78 module_param_named(send_queue_size
, mad_sendq_size
, int, 0444);
79 MODULE_PARM_DESC(send_queue_size
, "Size of send queue in number of work requests");
80 module_param_named(recv_queue_size
, mad_recvq_size
, int, 0444);
81 MODULE_PARM_DESC(recv_queue_size
, "Size of receive queue in number of work requests");
83 static DEFINE_XARRAY_ALLOC1(ib_mad_clients
);
84 static u32 ib_mad_client_next
;
85 static struct list_head ib_mad_port_list
;
88 static DEFINE_SPINLOCK(ib_mad_port_list_lock
);
90 /* Forward declarations */
91 static int method_in_use(struct ib_mad_mgmt_method_table
**method
,
92 struct ib_mad_reg_req
*mad_reg_req
);
93 static void remove_mad_reg_req(struct ib_mad_agent_private
*priv
);
94 static struct ib_mad_agent_private
*find_mad_agent(
95 struct ib_mad_port_private
*port_priv
,
96 const struct ib_mad_hdr
*mad
);
97 static int ib_mad_post_receive_mads(struct ib_mad_qp_info
*qp_info
,
98 struct ib_mad_private
*mad
);
99 static void cancel_mads(struct ib_mad_agent_private
*mad_agent_priv
);
100 static void timeout_sends(struct work_struct
*work
);
101 static void local_completions(struct work_struct
*work
);
102 static int add_nonoui_reg_req(struct ib_mad_reg_req
*mad_reg_req
,
103 struct ib_mad_agent_private
*agent_priv
,
105 static int add_oui_reg_req(struct ib_mad_reg_req
*mad_reg_req
,
106 struct ib_mad_agent_private
*agent_priv
);
107 static bool ib_mad_send_error(struct ib_mad_port_private
*port_priv
,
109 static void ib_mad_send_done(struct ib_cq
*cq
, struct ib_wc
*wc
);
112 * Returns a ib_mad_port_private structure or NULL for a device/port
113 * Assumes ib_mad_port_list_lock is being held
115 static inline struct ib_mad_port_private
*
116 __ib_get_mad_port(struct ib_device
*device
, u32 port_num
)
118 struct ib_mad_port_private
*entry
;
120 list_for_each_entry(entry
, &ib_mad_port_list
, port_list
) {
121 if (entry
->device
== device
&& entry
->port_num
== port_num
)
128 * Wrapper function to return a ib_mad_port_private structure or NULL
131 static inline struct ib_mad_port_private
*
132 ib_get_mad_port(struct ib_device
*device
, u32 port_num
)
134 struct ib_mad_port_private
*entry
;
137 spin_lock_irqsave(&ib_mad_port_list_lock
, flags
);
138 entry
= __ib_get_mad_port(device
, port_num
);
139 spin_unlock_irqrestore(&ib_mad_port_list_lock
, flags
);
144 static inline u8
convert_mgmt_class(u8 mgmt_class
)
146 /* Alias IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE to 0 */
147 return mgmt_class
== IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
?
151 static int get_spl_qp_index(enum ib_qp_type qp_type
)
163 static int vendor_class_index(u8 mgmt_class
)
165 return mgmt_class
- IB_MGMT_CLASS_VENDOR_RANGE2_START
;
168 static int is_vendor_class(u8 mgmt_class
)
170 if ((mgmt_class
< IB_MGMT_CLASS_VENDOR_RANGE2_START
) ||
171 (mgmt_class
> IB_MGMT_CLASS_VENDOR_RANGE2_END
))
176 static int is_vendor_oui(char *oui
)
178 if (oui
[0] || oui
[1] || oui
[2])
183 static int is_vendor_method_in_use(
184 struct ib_mad_mgmt_vendor_class
*vendor_class
,
185 struct ib_mad_reg_req
*mad_reg_req
)
187 struct ib_mad_mgmt_method_table
*method
;
190 for (i
= 0; i
< MAX_MGMT_OUI
; i
++) {
191 if (!memcmp(vendor_class
->oui
[i
], mad_reg_req
->oui
, 3)) {
192 method
= vendor_class
->method_table
[i
];
194 if (method_in_use(&method
, mad_reg_req
))
204 int ib_response_mad(const struct ib_mad_hdr
*hdr
)
206 return ((hdr
->method
& IB_MGMT_METHOD_RESP
) ||
207 (hdr
->method
== IB_MGMT_METHOD_TRAP_REPRESS
) ||
208 ((hdr
->mgmt_class
== IB_MGMT_CLASS_BM
) &&
209 (hdr
->attr_mod
& IB_BM_ATTR_MOD_RESP
)));
211 EXPORT_SYMBOL(ib_response_mad
);
214 * ib_register_mad_agent - Register to send/receive MADs
216 * Context: Process context.
218 struct ib_mad_agent
*ib_register_mad_agent(struct ib_device
*device
,
220 enum ib_qp_type qp_type
,
221 struct ib_mad_reg_req
*mad_reg_req
,
223 ib_mad_send_handler send_handler
,
224 ib_mad_recv_handler recv_handler
,
226 u32 registration_flags
)
228 struct ib_mad_port_private
*port_priv
;
229 struct ib_mad_agent
*ret
= ERR_PTR(-EINVAL
);
230 struct ib_mad_agent_private
*mad_agent_priv
;
231 struct ib_mad_reg_req
*reg_req
= NULL
;
232 struct ib_mad_mgmt_class_table
*class;
233 struct ib_mad_mgmt_vendor_class_table
*vendor
;
234 struct ib_mad_mgmt_vendor_class
*vendor_class
;
235 struct ib_mad_mgmt_method_table
*method
;
237 u8 mgmt_class
, vclass
;
239 if ((qp_type
== IB_QPT_SMI
&& !rdma_cap_ib_smi(device
, port_num
)) ||
240 (qp_type
== IB_QPT_GSI
&& !rdma_cap_ib_cm(device
, port_num
)))
241 return ERR_PTR(-EPROTONOSUPPORT
);
243 /* Validate parameters */
244 qpn
= get_spl_qp_index(qp_type
);
246 dev_dbg_ratelimited(&device
->dev
, "%s: invalid QP Type %d\n",
251 if (rmpp_version
&& rmpp_version
!= IB_MGMT_RMPP_VERSION
) {
252 dev_dbg_ratelimited(&device
->dev
,
253 "%s: invalid RMPP Version %u\n",
254 __func__
, rmpp_version
);
258 /* Validate MAD registration request if supplied */
260 if (mad_reg_req
->mgmt_class_version
>= MAX_MGMT_VERSION
) {
261 dev_dbg_ratelimited(&device
->dev
,
262 "%s: invalid Class Version %u\n",
264 mad_reg_req
->mgmt_class_version
);
268 dev_dbg_ratelimited(&device
->dev
,
269 "%s: no recv_handler\n", __func__
);
272 if (mad_reg_req
->mgmt_class
>= MAX_MGMT_CLASS
) {
274 * IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE is the only
275 * one in this range currently allowed
277 if (mad_reg_req
->mgmt_class
!=
278 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
) {
279 dev_dbg_ratelimited(&device
->dev
,
280 "%s: Invalid Mgmt Class 0x%x\n",
281 __func__
, mad_reg_req
->mgmt_class
);
284 } else if (mad_reg_req
->mgmt_class
== 0) {
286 * Class 0 is reserved in IBA and is used for
287 * aliasing of IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
289 dev_dbg_ratelimited(&device
->dev
,
290 "%s: Invalid Mgmt Class 0\n",
293 } else if (is_vendor_class(mad_reg_req
->mgmt_class
)) {
295 * If class is in "new" vendor range,
296 * ensure supplied OUI is not zero
298 if (!is_vendor_oui(mad_reg_req
->oui
)) {
299 dev_dbg_ratelimited(&device
->dev
,
300 "%s: No OUI specified for class 0x%x\n",
302 mad_reg_req
->mgmt_class
);
306 /* Make sure class supplied is consistent with RMPP */
307 if (!ib_is_mad_class_rmpp(mad_reg_req
->mgmt_class
)) {
309 dev_dbg_ratelimited(&device
->dev
,
310 "%s: RMPP version for non-RMPP class 0x%x\n",
311 __func__
, mad_reg_req
->mgmt_class
);
316 /* Make sure class supplied is consistent with QP type */
317 if (qp_type
== IB_QPT_SMI
) {
318 if ((mad_reg_req
->mgmt_class
!=
319 IB_MGMT_CLASS_SUBN_LID_ROUTED
) &&
320 (mad_reg_req
->mgmt_class
!=
321 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
)) {
322 dev_dbg_ratelimited(&device
->dev
,
323 "%s: Invalid SM QP type: class 0x%x\n",
324 __func__
, mad_reg_req
->mgmt_class
);
328 if ((mad_reg_req
->mgmt_class
==
329 IB_MGMT_CLASS_SUBN_LID_ROUTED
) ||
330 (mad_reg_req
->mgmt_class
==
331 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
)) {
332 dev_dbg_ratelimited(&device
->dev
,
333 "%s: Invalid GS QP type: class 0x%x\n",
334 __func__
, mad_reg_req
->mgmt_class
);
339 /* No registration request supplied */
342 if (registration_flags
& IB_MAD_USER_RMPP
)
346 /* Validate device and port */
347 port_priv
= ib_get_mad_port(device
, port_num
);
349 dev_dbg_ratelimited(&device
->dev
, "%s: Invalid port %u\n",
351 ret
= ERR_PTR(-ENODEV
);
355 /* Verify the QP requested is supported. For example, Ethernet devices
358 if (!port_priv
->qp_info
[qpn
].qp
) {
359 dev_dbg_ratelimited(&device
->dev
, "%s: QP %d not supported\n",
361 ret
= ERR_PTR(-EPROTONOSUPPORT
);
365 /* Allocate structures */
366 mad_agent_priv
= kzalloc(sizeof *mad_agent_priv
, GFP_KERNEL
);
367 if (!mad_agent_priv
) {
368 ret
= ERR_PTR(-ENOMEM
);
373 reg_req
= kmemdup(mad_reg_req
, sizeof *reg_req
, GFP_KERNEL
);
375 ret
= ERR_PTR(-ENOMEM
);
380 /* Now, fill in the various structures */
381 mad_agent_priv
->qp_info
= &port_priv
->qp_info
[qpn
];
382 mad_agent_priv
->reg_req
= reg_req
;
383 mad_agent_priv
->agent
.rmpp_version
= rmpp_version
;
384 mad_agent_priv
->agent
.device
= device
;
385 mad_agent_priv
->agent
.recv_handler
= recv_handler
;
386 mad_agent_priv
->agent
.send_handler
= send_handler
;
387 mad_agent_priv
->agent
.context
= context
;
388 mad_agent_priv
->agent
.qp
= port_priv
->qp_info
[qpn
].qp
;
389 mad_agent_priv
->agent
.port_num
= port_num
;
390 mad_agent_priv
->agent
.flags
= registration_flags
;
391 spin_lock_init(&mad_agent_priv
->lock
);
392 INIT_LIST_HEAD(&mad_agent_priv
->send_list
);
393 INIT_LIST_HEAD(&mad_agent_priv
->wait_list
);
394 INIT_LIST_HEAD(&mad_agent_priv
->done_list
);
395 INIT_LIST_HEAD(&mad_agent_priv
->rmpp_list
);
396 INIT_DELAYED_WORK(&mad_agent_priv
->timed_work
, timeout_sends
);
397 INIT_LIST_HEAD(&mad_agent_priv
->local_list
);
398 INIT_WORK(&mad_agent_priv
->local_work
, local_completions
);
399 refcount_set(&mad_agent_priv
->refcount
, 1);
400 init_completion(&mad_agent_priv
->comp
);
402 ret2
= ib_mad_agent_security_setup(&mad_agent_priv
->agent
, qp_type
);
409 * The mlx4 driver uses the top byte to distinguish which virtual
410 * function generated the MAD, so we must avoid using it.
412 ret2
= xa_alloc_cyclic(&ib_mad_clients
, &mad_agent_priv
->agent
.hi_tid
,
413 mad_agent_priv
, XA_LIMIT(0, (1 << 24) - 1),
414 &ib_mad_client_next
, GFP_KERNEL
);
421 * Make sure MAD registration (if supplied)
422 * is non overlapping with any existing ones
424 spin_lock_irq(&port_priv
->reg_lock
);
426 mgmt_class
= convert_mgmt_class(mad_reg_req
->mgmt_class
);
427 if (!is_vendor_class(mgmt_class
)) {
428 class = port_priv
->version
[mad_reg_req
->
429 mgmt_class_version
].class;
431 method
= class->method_table
[mgmt_class
];
433 if (method_in_use(&method
,
438 ret2
= add_nonoui_reg_req(mad_reg_req
, mad_agent_priv
,
441 /* "New" vendor class range */
442 vendor
= port_priv
->version
[mad_reg_req
->
443 mgmt_class_version
].vendor
;
445 vclass
= vendor_class_index(mgmt_class
);
446 vendor_class
= vendor
->vendor_class
[vclass
];
448 if (is_vendor_method_in_use(
454 ret2
= add_oui_reg_req(mad_reg_req
, mad_agent_priv
);
461 spin_unlock_irq(&port_priv
->reg_lock
);
463 trace_ib_mad_create_agent(mad_agent_priv
);
464 return &mad_agent_priv
->agent
;
466 spin_unlock_irq(&port_priv
->reg_lock
);
467 xa_erase(&ib_mad_clients
, mad_agent_priv
->agent
.hi_tid
);
469 ib_mad_agent_security_cleanup(&mad_agent_priv
->agent
);
473 kfree(mad_agent_priv
);
477 EXPORT_SYMBOL(ib_register_mad_agent
);
479 static inline void deref_mad_agent(struct ib_mad_agent_private
*mad_agent_priv
)
481 if (refcount_dec_and_test(&mad_agent_priv
->refcount
))
482 complete(&mad_agent_priv
->comp
);
485 static void unregister_mad_agent(struct ib_mad_agent_private
*mad_agent_priv
)
487 struct ib_mad_port_private
*port_priv
;
489 /* Note that we could still be handling received MADs */
490 trace_ib_mad_unregister_agent(mad_agent_priv
);
493 * Canceling all sends results in dropping received response
494 * MADs, preventing us from queuing additional work
496 cancel_mads(mad_agent_priv
);
497 port_priv
= mad_agent_priv
->qp_info
->port_priv
;
498 cancel_delayed_work(&mad_agent_priv
->timed_work
);
500 spin_lock_irq(&port_priv
->reg_lock
);
501 remove_mad_reg_req(mad_agent_priv
);
502 spin_unlock_irq(&port_priv
->reg_lock
);
503 xa_erase(&ib_mad_clients
, mad_agent_priv
->agent
.hi_tid
);
505 flush_workqueue(port_priv
->wq
);
507 deref_mad_agent(mad_agent_priv
);
508 wait_for_completion(&mad_agent_priv
->comp
);
509 ib_cancel_rmpp_recvs(mad_agent_priv
);
511 ib_mad_agent_security_cleanup(&mad_agent_priv
->agent
);
513 kfree(mad_agent_priv
->reg_req
);
514 kfree_rcu(mad_agent_priv
, rcu
);
518 * ib_unregister_mad_agent - Unregisters a client from using MAD services
520 * Context: Process context.
522 void ib_unregister_mad_agent(struct ib_mad_agent
*mad_agent
)
524 struct ib_mad_agent_private
*mad_agent_priv
;
526 mad_agent_priv
= container_of(mad_agent
,
527 struct ib_mad_agent_private
,
529 unregister_mad_agent(mad_agent_priv
);
531 EXPORT_SYMBOL(ib_unregister_mad_agent
);
533 static void dequeue_mad(struct ib_mad_list_head
*mad_list
)
535 struct ib_mad_queue
*mad_queue
;
538 mad_queue
= mad_list
->mad_queue
;
539 spin_lock_irqsave(&mad_queue
->lock
, flags
);
540 list_del(&mad_list
->list
);
542 spin_unlock_irqrestore(&mad_queue
->lock
, flags
);
545 static void build_smp_wc(struct ib_qp
*qp
, struct ib_cqe
*cqe
, u16 slid
,
546 u16 pkey_index
, u32 port_num
, struct ib_wc
*wc
)
548 memset(wc
, 0, sizeof *wc
);
550 wc
->status
= IB_WC_SUCCESS
;
551 wc
->opcode
= IB_WC_RECV
;
552 wc
->pkey_index
= pkey_index
;
553 wc
->byte_len
= sizeof(struct ib_mad
) + sizeof(struct ib_grh
);
558 wc
->dlid_path_bits
= 0;
559 wc
->port_num
= port_num
;
562 static size_t mad_priv_size(const struct ib_mad_private
*mp
)
564 return sizeof(struct ib_mad_private
) + mp
->mad_size
;
567 static struct ib_mad_private
*alloc_mad_private(size_t mad_size
, gfp_t flags
)
569 size_t size
= sizeof(struct ib_mad_private
) + mad_size
;
570 struct ib_mad_private
*ret
= kzalloc(size
, flags
);
573 ret
->mad_size
= mad_size
;
578 static size_t port_mad_size(const struct ib_mad_port_private
*port_priv
)
580 return rdma_max_mad_size(port_priv
->device
, port_priv
->port_num
);
583 static size_t mad_priv_dma_size(const struct ib_mad_private
*mp
)
585 return sizeof(struct ib_grh
) + mp
->mad_size
;
589 * Return 0 if SMP is to be sent
590 * Return 1 if SMP was consumed locally (whether or not solicited)
591 * Return < 0 if error
593 static int handle_outgoing_dr_smp(struct ib_mad_agent_private
*mad_agent_priv
,
594 struct ib_mad_send_wr_private
*mad_send_wr
)
597 struct ib_smp
*smp
= mad_send_wr
->send_buf
.mad
;
598 struct opa_smp
*opa_smp
= (struct opa_smp
*)smp
;
600 struct ib_mad_local_private
*local
;
601 struct ib_mad_private
*mad_priv
;
602 struct ib_mad_port_private
*port_priv
;
603 struct ib_mad_agent_private
*recv_mad_agent
= NULL
;
604 struct ib_device
*device
= mad_agent_priv
->agent
.device
;
607 struct ib_ud_wr
*send_wr
= &mad_send_wr
->send_wr
;
608 size_t mad_size
= port_mad_size(mad_agent_priv
->qp_info
->port_priv
);
609 u16 out_mad_pkey_index
= 0;
611 bool opa
= rdma_cap_opa_mad(mad_agent_priv
->qp_info
->port_priv
->device
,
612 mad_agent_priv
->qp_info
->port_priv
->port_num
);
614 if (rdma_cap_ib_switch(device
) &&
615 smp
->mgmt_class
== IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
)
616 port_num
= send_wr
->port_num
;
618 port_num
= mad_agent_priv
->agent
.port_num
;
621 * Directed route handling starts if the initial LID routed part of
622 * a request or the ending LID routed part of a response is empty.
623 * If we are at the start of the LID routed part, don't update the
624 * hop_ptr or hop_cnt. See section 14.2.2, Vol 1 IB spec.
626 if (opa
&& smp
->class_version
== OPA_SM_CLASS_VERSION
) {
629 trace_ib_mad_handle_out_opa_smi(opa_smp
);
631 if ((opa_get_smp_direction(opa_smp
)
632 ? opa_smp
->route
.dr
.dr_dlid
: opa_smp
->route
.dr
.dr_slid
) ==
633 OPA_LID_PERMISSIVE
&&
634 opa_smi_handle_dr_smp_send(opa_smp
,
635 rdma_cap_ib_switch(device
),
636 port_num
) == IB_SMI_DISCARD
) {
638 dev_err(&device
->dev
, "OPA Invalid directed route\n");
641 opa_drslid
= be32_to_cpu(opa_smp
->route
.dr
.dr_slid
);
642 if (opa_drslid
!= be32_to_cpu(OPA_LID_PERMISSIVE
) &&
643 opa_drslid
& 0xffff0000) {
645 dev_err(&device
->dev
, "OPA Invalid dr_slid 0x%x\n",
649 drslid
= (u16
)(opa_drslid
& 0x0000ffff);
651 /* Check to post send on QP or process locally */
652 if (opa_smi_check_local_smp(opa_smp
, device
) == IB_SMI_DISCARD
&&
653 opa_smi_check_local_returning_smp(opa_smp
, device
) == IB_SMI_DISCARD
)
656 trace_ib_mad_handle_out_ib_smi(smp
);
658 if ((ib_get_smp_direction(smp
) ? smp
->dr_dlid
: smp
->dr_slid
) ==
660 smi_handle_dr_smp_send(smp
, rdma_cap_ib_switch(device
), port_num
) ==
663 dev_err(&device
->dev
, "Invalid directed route\n");
666 drslid
= be16_to_cpu(smp
->dr_slid
);
668 /* Check to post send on QP or process locally */
669 if (smi_check_local_smp(smp
, device
) == IB_SMI_DISCARD
&&
670 smi_check_local_returning_smp(smp
, device
) == IB_SMI_DISCARD
)
674 local
= kmalloc(sizeof *local
, GFP_ATOMIC
);
679 local
->mad_priv
= NULL
;
680 local
->recv_mad_agent
= NULL
;
681 mad_priv
= alloc_mad_private(mad_size
, GFP_ATOMIC
);
688 build_smp_wc(mad_agent_priv
->agent
.qp
,
689 send_wr
->wr
.wr_cqe
, drslid
,
691 send_wr
->port_num
, &mad_wc
);
693 if (opa
&& smp
->base_version
== OPA_MGMT_BASE_VERSION
) {
694 mad_wc
.byte_len
= mad_send_wr
->send_buf
.hdr_len
695 + mad_send_wr
->send_buf
.data_len
696 + sizeof(struct ib_grh
);
699 /* No GRH for DR SMP */
700 ret
= device
->ops
.process_mad(device
, 0, port_num
, &mad_wc
, NULL
,
701 (const struct ib_mad
*)smp
,
702 (struct ib_mad
*)mad_priv
->mad
, &mad_size
,
703 &out_mad_pkey_index
);
705 case IB_MAD_RESULT_SUCCESS
| IB_MAD_RESULT_REPLY
:
706 if (ib_response_mad((const struct ib_mad_hdr
*)mad_priv
->mad
) &&
707 mad_agent_priv
->agent
.recv_handler
) {
708 local
->mad_priv
= mad_priv
;
709 local
->recv_mad_agent
= mad_agent_priv
;
711 * Reference MAD agent until receive
712 * side of local completion handled
714 refcount_inc(&mad_agent_priv
->refcount
);
718 case IB_MAD_RESULT_SUCCESS
| IB_MAD_RESULT_CONSUMED
:
721 case IB_MAD_RESULT_SUCCESS
:
722 /* Treat like an incoming receive MAD */
723 port_priv
= ib_get_mad_port(mad_agent_priv
->agent
.device
,
724 mad_agent_priv
->agent
.port_num
);
726 memcpy(mad_priv
->mad
, smp
, mad_priv
->mad_size
);
727 recv_mad_agent
= find_mad_agent(port_priv
,
728 (const struct ib_mad_hdr
*)mad_priv
->mad
);
730 if (!port_priv
|| !recv_mad_agent
) {
732 * No receiving agent so drop packet and
733 * generate send completion.
738 local
->mad_priv
= mad_priv
;
739 local
->recv_mad_agent
= recv_mad_agent
;
748 local
->mad_send_wr
= mad_send_wr
;
750 local
->mad_send_wr
->send_wr
.pkey_index
= out_mad_pkey_index
;
751 local
->return_wc_byte_len
= mad_size
;
753 /* Reference MAD agent until send side of local completion handled */
754 refcount_inc(&mad_agent_priv
->refcount
);
755 /* Queue local completion to local list */
756 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
757 list_add_tail(&local
->completion_list
, &mad_agent_priv
->local_list
);
758 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
759 queue_work(mad_agent_priv
->qp_info
->port_priv
->wq
,
760 &mad_agent_priv
->local_work
);
766 static int get_pad_size(int hdr_len
, int data_len
, size_t mad_size
)
770 seg_size
= mad_size
- hdr_len
;
771 if (data_len
&& seg_size
) {
772 pad
= seg_size
- data_len
% seg_size
;
773 return pad
== seg_size
? 0 : pad
;
778 static void free_send_rmpp_list(struct ib_mad_send_wr_private
*mad_send_wr
)
780 struct ib_rmpp_segment
*s
, *t
;
782 list_for_each_entry_safe(s
, t
, &mad_send_wr
->rmpp_list
, list
) {
788 static int alloc_send_rmpp_list(struct ib_mad_send_wr_private
*send_wr
,
789 size_t mad_size
, gfp_t gfp_mask
)
791 struct ib_mad_send_buf
*send_buf
= &send_wr
->send_buf
;
792 struct ib_rmpp_mad
*rmpp_mad
= send_buf
->mad
;
793 struct ib_rmpp_segment
*seg
= NULL
;
794 int left
, seg_size
, pad
;
796 send_buf
->seg_size
= mad_size
- send_buf
->hdr_len
;
797 send_buf
->seg_rmpp_size
= mad_size
- IB_MGMT_RMPP_HDR
;
798 seg_size
= send_buf
->seg_size
;
801 /* Allocate data segments. */
802 for (left
= send_buf
->data_len
+ pad
; left
> 0; left
-= seg_size
) {
803 seg
= kmalloc(sizeof(*seg
) + seg_size
, gfp_mask
);
805 free_send_rmpp_list(send_wr
);
808 seg
->num
= ++send_buf
->seg_count
;
809 list_add_tail(&seg
->list
, &send_wr
->rmpp_list
);
812 /* Zero any padding */
814 memset(seg
->data
+ seg_size
- pad
, 0, pad
);
816 rmpp_mad
->rmpp_hdr
.rmpp_version
= send_wr
->mad_agent_priv
->
818 rmpp_mad
->rmpp_hdr
.rmpp_type
= IB_MGMT_RMPP_TYPE_DATA
;
819 ib_set_rmpp_flags(&rmpp_mad
->rmpp_hdr
, IB_MGMT_RMPP_FLAG_ACTIVE
);
821 send_wr
->cur_seg
= container_of(send_wr
->rmpp_list
.next
,
822 struct ib_rmpp_segment
, list
);
823 send_wr
->last_ack_seg
= send_wr
->cur_seg
;
827 int ib_mad_kernel_rmpp_agent(const struct ib_mad_agent
*agent
)
829 return agent
->rmpp_version
&& !(agent
->flags
& IB_MAD_USER_RMPP
);
831 EXPORT_SYMBOL(ib_mad_kernel_rmpp_agent
);
833 struct ib_mad_send_buf
*ib_create_send_mad(struct ib_mad_agent
*mad_agent
,
834 u32 remote_qpn
, u16 pkey_index
,
835 int rmpp_active
, int hdr_len
,
836 int data_len
, gfp_t gfp_mask
,
839 struct ib_mad_agent_private
*mad_agent_priv
;
840 struct ib_mad_send_wr_private
*mad_send_wr
;
841 int pad
, message_size
, ret
, size
;
846 mad_agent_priv
= container_of(mad_agent
, struct ib_mad_agent_private
,
849 opa
= rdma_cap_opa_mad(mad_agent
->device
, mad_agent
->port_num
);
851 if (opa
&& base_version
== OPA_MGMT_BASE_VERSION
)
852 mad_size
= sizeof(struct opa_mad
);
854 mad_size
= sizeof(struct ib_mad
);
856 pad
= get_pad_size(hdr_len
, data_len
, mad_size
);
857 message_size
= hdr_len
+ data_len
+ pad
;
859 if (ib_mad_kernel_rmpp_agent(mad_agent
)) {
860 if (!rmpp_active
&& message_size
> mad_size
)
861 return ERR_PTR(-EINVAL
);
863 if (rmpp_active
|| message_size
> mad_size
)
864 return ERR_PTR(-EINVAL
);
866 size
= rmpp_active
? hdr_len
: mad_size
;
867 buf
= kzalloc(sizeof *mad_send_wr
+ size
, gfp_mask
);
869 return ERR_PTR(-ENOMEM
);
871 mad_send_wr
= buf
+ size
;
872 INIT_LIST_HEAD(&mad_send_wr
->rmpp_list
);
873 mad_send_wr
->send_buf
.mad
= buf
;
874 mad_send_wr
->send_buf
.hdr_len
= hdr_len
;
875 mad_send_wr
->send_buf
.data_len
= data_len
;
876 mad_send_wr
->pad
= pad
;
878 mad_send_wr
->mad_agent_priv
= mad_agent_priv
;
879 mad_send_wr
->sg_list
[0].length
= hdr_len
;
880 mad_send_wr
->sg_list
[0].lkey
= mad_agent
->qp
->pd
->local_dma_lkey
;
882 /* OPA MADs don't have to be the full 2048 bytes */
883 if (opa
&& base_version
== OPA_MGMT_BASE_VERSION
&&
884 data_len
< mad_size
- hdr_len
)
885 mad_send_wr
->sg_list
[1].length
= data_len
;
887 mad_send_wr
->sg_list
[1].length
= mad_size
- hdr_len
;
889 mad_send_wr
->sg_list
[1].lkey
= mad_agent
->qp
->pd
->local_dma_lkey
;
891 mad_send_wr
->mad_list
.cqe
.done
= ib_mad_send_done
;
893 mad_send_wr
->send_wr
.wr
.wr_cqe
= &mad_send_wr
->mad_list
.cqe
;
894 mad_send_wr
->send_wr
.wr
.sg_list
= mad_send_wr
->sg_list
;
895 mad_send_wr
->send_wr
.wr
.num_sge
= 2;
896 mad_send_wr
->send_wr
.wr
.opcode
= IB_WR_SEND
;
897 mad_send_wr
->send_wr
.wr
.send_flags
= IB_SEND_SIGNALED
;
898 mad_send_wr
->send_wr
.remote_qpn
= remote_qpn
;
899 mad_send_wr
->send_wr
.remote_qkey
= IB_QP_SET_QKEY
;
900 mad_send_wr
->send_wr
.pkey_index
= pkey_index
;
903 ret
= alloc_send_rmpp_list(mad_send_wr
, mad_size
, gfp_mask
);
910 mad_send_wr
->send_buf
.mad_agent
= mad_agent
;
911 refcount_inc(&mad_agent_priv
->refcount
);
912 return &mad_send_wr
->send_buf
;
914 EXPORT_SYMBOL(ib_create_send_mad
);
916 int ib_get_mad_data_offset(u8 mgmt_class
)
918 if (mgmt_class
== IB_MGMT_CLASS_SUBN_ADM
)
919 return IB_MGMT_SA_HDR
;
920 else if ((mgmt_class
== IB_MGMT_CLASS_DEVICE_MGMT
) ||
921 (mgmt_class
== IB_MGMT_CLASS_DEVICE_ADM
) ||
922 (mgmt_class
== IB_MGMT_CLASS_BIS
))
923 return IB_MGMT_DEVICE_HDR
;
924 else if ((mgmt_class
>= IB_MGMT_CLASS_VENDOR_RANGE2_START
) &&
925 (mgmt_class
<= IB_MGMT_CLASS_VENDOR_RANGE2_END
))
926 return IB_MGMT_VENDOR_HDR
;
928 return IB_MGMT_MAD_HDR
;
930 EXPORT_SYMBOL(ib_get_mad_data_offset
);
932 int ib_is_mad_class_rmpp(u8 mgmt_class
)
934 if ((mgmt_class
== IB_MGMT_CLASS_SUBN_ADM
) ||
935 (mgmt_class
== IB_MGMT_CLASS_DEVICE_MGMT
) ||
936 (mgmt_class
== IB_MGMT_CLASS_DEVICE_ADM
) ||
937 (mgmt_class
== IB_MGMT_CLASS_BIS
) ||
938 ((mgmt_class
>= IB_MGMT_CLASS_VENDOR_RANGE2_START
) &&
939 (mgmt_class
<= IB_MGMT_CLASS_VENDOR_RANGE2_END
)))
943 EXPORT_SYMBOL(ib_is_mad_class_rmpp
);
945 void *ib_get_rmpp_segment(struct ib_mad_send_buf
*send_buf
, int seg_num
)
947 struct ib_mad_send_wr_private
*mad_send_wr
;
948 struct list_head
*list
;
950 mad_send_wr
= container_of(send_buf
, struct ib_mad_send_wr_private
,
952 list
= &mad_send_wr
->cur_seg
->list
;
954 if (mad_send_wr
->cur_seg
->num
< seg_num
) {
955 list_for_each_entry(mad_send_wr
->cur_seg
, list
, list
)
956 if (mad_send_wr
->cur_seg
->num
== seg_num
)
958 } else if (mad_send_wr
->cur_seg
->num
> seg_num
) {
959 list_for_each_entry_reverse(mad_send_wr
->cur_seg
, list
, list
)
960 if (mad_send_wr
->cur_seg
->num
== seg_num
)
963 return mad_send_wr
->cur_seg
->data
;
965 EXPORT_SYMBOL(ib_get_rmpp_segment
);
967 static inline void *ib_get_payload(struct ib_mad_send_wr_private
*mad_send_wr
)
969 if (mad_send_wr
->send_buf
.seg_count
)
970 return ib_get_rmpp_segment(&mad_send_wr
->send_buf
,
971 mad_send_wr
->seg_num
);
973 return mad_send_wr
->send_buf
.mad
+
974 mad_send_wr
->send_buf
.hdr_len
;
977 void ib_free_send_mad(struct ib_mad_send_buf
*send_buf
)
979 struct ib_mad_agent_private
*mad_agent_priv
;
980 struct ib_mad_send_wr_private
*mad_send_wr
;
982 mad_agent_priv
= container_of(send_buf
->mad_agent
,
983 struct ib_mad_agent_private
, agent
);
984 mad_send_wr
= container_of(send_buf
, struct ib_mad_send_wr_private
,
987 free_send_rmpp_list(mad_send_wr
);
988 kfree(send_buf
->mad
);
989 deref_mad_agent(mad_agent_priv
);
991 EXPORT_SYMBOL(ib_free_send_mad
);
993 int ib_send_mad(struct ib_mad_send_wr_private
*mad_send_wr
)
995 struct ib_mad_qp_info
*qp_info
;
996 struct list_head
*list
;
997 struct ib_mad_agent
*mad_agent
;
1002 /* Set WR ID to find mad_send_wr upon completion */
1003 qp_info
= mad_send_wr
->mad_agent_priv
->qp_info
;
1004 mad_send_wr
->mad_list
.mad_queue
= &qp_info
->send_queue
;
1005 mad_send_wr
->mad_list
.cqe
.done
= ib_mad_send_done
;
1006 mad_send_wr
->send_wr
.wr
.wr_cqe
= &mad_send_wr
->mad_list
.cqe
;
1008 mad_agent
= mad_send_wr
->send_buf
.mad_agent
;
1009 sge
= mad_send_wr
->sg_list
;
1010 sge
[0].addr
= ib_dma_map_single(mad_agent
->device
,
1011 mad_send_wr
->send_buf
.mad
,
1014 if (unlikely(ib_dma_mapping_error(mad_agent
->device
, sge
[0].addr
)))
1017 mad_send_wr
->header_mapping
= sge
[0].addr
;
1019 sge
[1].addr
= ib_dma_map_single(mad_agent
->device
,
1020 ib_get_payload(mad_send_wr
),
1023 if (unlikely(ib_dma_mapping_error(mad_agent
->device
, sge
[1].addr
))) {
1024 ib_dma_unmap_single(mad_agent
->device
,
1025 mad_send_wr
->header_mapping
,
1026 sge
[0].length
, DMA_TO_DEVICE
);
1029 mad_send_wr
->payload_mapping
= sge
[1].addr
;
1031 spin_lock_irqsave(&qp_info
->send_queue
.lock
, flags
);
1032 if (qp_info
->send_queue
.count
< qp_info
->send_queue
.max_active
) {
1033 trace_ib_mad_ib_send_mad(mad_send_wr
, qp_info
);
1034 ret
= ib_post_send(mad_agent
->qp
, &mad_send_wr
->send_wr
.wr
,
1036 list
= &qp_info
->send_queue
.list
;
1039 list
= &qp_info
->overflow_list
;
1043 qp_info
->send_queue
.count
++;
1044 list_add_tail(&mad_send_wr
->mad_list
.list
, list
);
1046 spin_unlock_irqrestore(&qp_info
->send_queue
.lock
, flags
);
1048 ib_dma_unmap_single(mad_agent
->device
,
1049 mad_send_wr
->header_mapping
,
1050 sge
[0].length
, DMA_TO_DEVICE
);
1051 ib_dma_unmap_single(mad_agent
->device
,
1052 mad_send_wr
->payload_mapping
,
1053 sge
[1].length
, DMA_TO_DEVICE
);
1059 * ib_post_send_mad - Posts MAD(s) to the send queue of the QP associated
1060 * with the registered client
1062 int ib_post_send_mad(struct ib_mad_send_buf
*send_buf
,
1063 struct ib_mad_send_buf
**bad_send_buf
)
1065 struct ib_mad_agent_private
*mad_agent_priv
;
1066 struct ib_mad_send_buf
*next_send_buf
;
1067 struct ib_mad_send_wr_private
*mad_send_wr
;
1068 unsigned long flags
;
1071 /* Walk list of send WRs and post each on send list */
1072 for (; send_buf
; send_buf
= next_send_buf
) {
1073 mad_send_wr
= container_of(send_buf
,
1074 struct ib_mad_send_wr_private
,
1076 mad_agent_priv
= mad_send_wr
->mad_agent_priv
;
1078 ret
= ib_mad_enforce_security(mad_agent_priv
,
1079 mad_send_wr
->send_wr
.pkey_index
);
1083 if (!send_buf
->mad_agent
->send_handler
||
1084 (send_buf
->timeout_ms
&&
1085 !send_buf
->mad_agent
->recv_handler
)) {
1090 if (!ib_is_mad_class_rmpp(((struct ib_mad_hdr
*) send_buf
->mad
)->mgmt_class
)) {
1091 if (mad_agent_priv
->agent
.rmpp_version
) {
1098 * Save pointer to next work request to post in case the
1099 * current one completes, and the user modifies the work
1100 * request associated with the completion
1102 next_send_buf
= send_buf
->next
;
1103 mad_send_wr
->send_wr
.ah
= send_buf
->ah
;
1105 if (((struct ib_mad_hdr
*) send_buf
->mad
)->mgmt_class
==
1106 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
) {
1107 ret
= handle_outgoing_dr_smp(mad_agent_priv
,
1109 if (ret
< 0) /* error */
1111 else if (ret
== 1) /* locally consumed */
1115 mad_send_wr
->tid
= ((struct ib_mad_hdr
*) send_buf
->mad
)->tid
;
1116 /* Timeout will be updated after send completes */
1117 mad_send_wr
->timeout
= msecs_to_jiffies(send_buf
->timeout_ms
);
1118 mad_send_wr
->max_retries
= send_buf
->retries
;
1119 mad_send_wr
->retries_left
= send_buf
->retries
;
1120 send_buf
->retries
= 0;
1121 /* Reference for work request to QP + response */
1122 mad_send_wr
->refcount
= 1 + (mad_send_wr
->timeout
> 0);
1123 mad_send_wr
->status
= IB_WC_SUCCESS
;
1125 /* Reference MAD agent until send completes */
1126 refcount_inc(&mad_agent_priv
->refcount
);
1127 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
1128 list_add_tail(&mad_send_wr
->agent_list
,
1129 &mad_agent_priv
->send_list
);
1130 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
1132 if (ib_mad_kernel_rmpp_agent(&mad_agent_priv
->agent
)) {
1133 ret
= ib_send_rmpp_mad(mad_send_wr
);
1134 if (ret
>= 0 && ret
!= IB_RMPP_RESULT_CONSUMED
)
1135 ret
= ib_send_mad(mad_send_wr
);
1137 ret
= ib_send_mad(mad_send_wr
);
1139 /* Fail send request */
1140 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
1141 list_del(&mad_send_wr
->agent_list
);
1142 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
1143 deref_mad_agent(mad_agent_priv
);
1150 *bad_send_buf
= send_buf
;
1153 EXPORT_SYMBOL(ib_post_send_mad
);
1156 * ib_free_recv_mad - Returns data buffers used to receive
1157 * a MAD to the access layer
1159 void ib_free_recv_mad(struct ib_mad_recv_wc
*mad_recv_wc
)
1161 struct ib_mad_recv_buf
*mad_recv_buf
, *temp_recv_buf
;
1162 struct ib_mad_private_header
*mad_priv_hdr
;
1163 struct ib_mad_private
*priv
;
1164 struct list_head free_list
;
1166 INIT_LIST_HEAD(&free_list
);
1167 list_splice_init(&mad_recv_wc
->rmpp_list
, &free_list
);
1169 list_for_each_entry_safe(mad_recv_buf
, temp_recv_buf
,
1171 mad_recv_wc
= container_of(mad_recv_buf
, struct ib_mad_recv_wc
,
1173 mad_priv_hdr
= container_of(mad_recv_wc
,
1174 struct ib_mad_private_header
,
1176 priv
= container_of(mad_priv_hdr
, struct ib_mad_private
,
1181 EXPORT_SYMBOL(ib_free_recv_mad
);
1183 static int method_in_use(struct ib_mad_mgmt_method_table
**method
,
1184 struct ib_mad_reg_req
*mad_reg_req
)
1188 for_each_set_bit(i
, mad_reg_req
->method_mask
, IB_MGMT_MAX_METHODS
) {
1189 if ((*method
)->agent
[i
]) {
1190 pr_err("Method %d already in use\n", i
);
1197 static int allocate_method_table(struct ib_mad_mgmt_method_table
**method
)
1199 /* Allocate management method table */
1200 *method
= kzalloc(sizeof **method
, GFP_ATOMIC
);
1201 return (*method
) ? 0 : (-ENOMEM
);
1205 * Check to see if there are any methods still in use
1207 static int check_method_table(struct ib_mad_mgmt_method_table
*method
)
1211 for (i
= 0; i
< IB_MGMT_MAX_METHODS
; i
++)
1212 if (method
->agent
[i
])
1218 * Check to see if there are any method tables for this class still in use
1220 static int check_class_table(struct ib_mad_mgmt_class_table
*class)
1224 for (i
= 0; i
< MAX_MGMT_CLASS
; i
++)
1225 if (class->method_table
[i
])
1230 static int check_vendor_class(struct ib_mad_mgmt_vendor_class
*vendor_class
)
1234 for (i
= 0; i
< MAX_MGMT_OUI
; i
++)
1235 if (vendor_class
->method_table
[i
])
1240 static int find_vendor_oui(struct ib_mad_mgmt_vendor_class
*vendor_class
,
1245 for (i
= 0; i
< MAX_MGMT_OUI
; i
++)
1246 /* Is there matching OUI for this vendor class ? */
1247 if (!memcmp(vendor_class
->oui
[i
], oui
, 3))
1253 static int check_vendor_table(struct ib_mad_mgmt_vendor_class_table
*vendor
)
1257 for (i
= 0; i
< MAX_MGMT_VENDOR_RANGE2
; i
++)
1258 if (vendor
->vendor_class
[i
])
1264 static void remove_methods_mad_agent(struct ib_mad_mgmt_method_table
*method
,
1265 struct ib_mad_agent_private
*agent
)
1269 /* Remove any methods for this mad agent */
1270 for (i
= 0; i
< IB_MGMT_MAX_METHODS
; i
++)
1271 if (method
->agent
[i
] == agent
)
1272 method
->agent
[i
] = NULL
;
1275 static int add_nonoui_reg_req(struct ib_mad_reg_req
*mad_reg_req
,
1276 struct ib_mad_agent_private
*agent_priv
,
1279 struct ib_mad_port_private
*port_priv
;
1280 struct ib_mad_mgmt_class_table
**class;
1281 struct ib_mad_mgmt_method_table
**method
;
1284 port_priv
= agent_priv
->qp_info
->port_priv
;
1285 class = &port_priv
->version
[mad_reg_req
->mgmt_class_version
].class;
1287 /* Allocate management class table for "new" class version */
1288 *class = kzalloc(sizeof **class, GFP_ATOMIC
);
1294 /* Allocate method table for this management class */
1295 method
= &(*class)->method_table
[mgmt_class
];
1296 if ((ret
= allocate_method_table(method
)))
1299 method
= &(*class)->method_table
[mgmt_class
];
1301 /* Allocate method table for this management class */
1302 if ((ret
= allocate_method_table(method
)))
1307 /* Now, make sure methods are not already in use */
1308 if (method_in_use(method
, mad_reg_req
))
1311 /* Finally, add in methods being registered */
1312 for_each_set_bit(i
, mad_reg_req
->method_mask
, IB_MGMT_MAX_METHODS
)
1313 (*method
)->agent
[i
] = agent_priv
;
1318 /* Remove any methods for this mad agent */
1319 remove_methods_mad_agent(*method
, agent_priv
);
1320 /* Now, check to see if there are any methods in use */
1321 if (!check_method_table(*method
)) {
1322 /* If not, release management method table */
1335 static int add_oui_reg_req(struct ib_mad_reg_req
*mad_reg_req
,
1336 struct ib_mad_agent_private
*agent_priv
)
1338 struct ib_mad_port_private
*port_priv
;
1339 struct ib_mad_mgmt_vendor_class_table
**vendor_table
;
1340 struct ib_mad_mgmt_vendor_class_table
*vendor
= NULL
;
1341 struct ib_mad_mgmt_vendor_class
*vendor_class
= NULL
;
1342 struct ib_mad_mgmt_method_table
**method
;
1343 int i
, ret
= -ENOMEM
;
1346 /* "New" vendor (with OUI) class */
1347 vclass
= vendor_class_index(mad_reg_req
->mgmt_class
);
1348 port_priv
= agent_priv
->qp_info
->port_priv
;
1349 vendor_table
= &port_priv
->version
[
1350 mad_reg_req
->mgmt_class_version
].vendor
;
1351 if (!*vendor_table
) {
1352 /* Allocate mgmt vendor class table for "new" class version */
1353 vendor
= kzalloc(sizeof *vendor
, GFP_ATOMIC
);
1357 *vendor_table
= vendor
;
1359 if (!(*vendor_table
)->vendor_class
[vclass
]) {
1360 /* Allocate table for this management vendor class */
1361 vendor_class
= kzalloc(sizeof *vendor_class
, GFP_ATOMIC
);
1365 (*vendor_table
)->vendor_class
[vclass
] = vendor_class
;
1367 for (i
= 0; i
< MAX_MGMT_OUI
; i
++) {
1368 /* Is there matching OUI for this vendor class ? */
1369 if (!memcmp((*vendor_table
)->vendor_class
[vclass
]->oui
[i
],
1370 mad_reg_req
->oui
, 3)) {
1371 method
= &(*vendor_table
)->vendor_class
[
1372 vclass
]->method_table
[i
];
1378 for (i
= 0; i
< MAX_MGMT_OUI
; i
++) {
1379 /* OUI slot available ? */
1380 if (!is_vendor_oui((*vendor_table
)->vendor_class
[
1382 method
= &(*vendor_table
)->vendor_class
[
1383 vclass
]->method_table
[i
];
1384 /* Allocate method table for this OUI */
1386 ret
= allocate_method_table(method
);
1390 memcpy((*vendor_table
)->vendor_class
[vclass
]->oui
[i
],
1391 mad_reg_req
->oui
, 3);
1395 dev_err(&agent_priv
->agent
.device
->dev
, "All OUI slots in use\n");
1399 /* Now, make sure methods are not already in use */
1400 if (method_in_use(method
, mad_reg_req
))
1403 /* Finally, add in methods being registered */
1404 for_each_set_bit(i
, mad_reg_req
->method_mask
, IB_MGMT_MAX_METHODS
)
1405 (*method
)->agent
[i
] = agent_priv
;
1410 /* Remove any methods for this mad agent */
1411 remove_methods_mad_agent(*method
, agent_priv
);
1412 /* Now, check to see if there are any methods in use */
1413 if (!check_method_table(*method
)) {
1414 /* If not, release management method table */
1421 (*vendor_table
)->vendor_class
[vclass
] = NULL
;
1422 kfree(vendor_class
);
1426 *vendor_table
= NULL
;
1433 static void remove_mad_reg_req(struct ib_mad_agent_private
*agent_priv
)
1435 struct ib_mad_port_private
*port_priv
;
1436 struct ib_mad_mgmt_class_table
*class;
1437 struct ib_mad_mgmt_method_table
*method
;
1438 struct ib_mad_mgmt_vendor_class_table
*vendor
;
1439 struct ib_mad_mgmt_vendor_class
*vendor_class
;
1444 * Was MAD registration request supplied
1445 * with original registration ?
1447 if (!agent_priv
->reg_req
)
1450 port_priv
= agent_priv
->qp_info
->port_priv
;
1451 mgmt_class
= convert_mgmt_class(agent_priv
->reg_req
->mgmt_class
);
1452 class = port_priv
->version
[
1453 agent_priv
->reg_req
->mgmt_class_version
].class;
1457 method
= class->method_table
[mgmt_class
];
1459 /* Remove any methods for this mad agent */
1460 remove_methods_mad_agent(method
, agent_priv
);
1461 /* Now, check to see if there are any methods still in use */
1462 if (!check_method_table(method
)) {
1463 /* If not, release management method table */
1465 class->method_table
[mgmt_class
] = NULL
;
1466 /* Any management classes left ? */
1467 if (!check_class_table(class)) {
1468 /* If not, release management class table */
1471 agent_priv
->reg_req
->
1472 mgmt_class_version
].class = NULL
;
1478 if (!is_vendor_class(mgmt_class
))
1481 /* normalize mgmt_class to vendor range 2 */
1482 mgmt_class
= vendor_class_index(agent_priv
->reg_req
->mgmt_class
);
1483 vendor
= port_priv
->version
[
1484 agent_priv
->reg_req
->mgmt_class_version
].vendor
;
1489 vendor_class
= vendor
->vendor_class
[mgmt_class
];
1491 index
= find_vendor_oui(vendor_class
, agent_priv
->reg_req
->oui
);
1494 method
= vendor_class
->method_table
[index
];
1496 /* Remove any methods for this mad agent */
1497 remove_methods_mad_agent(method
, agent_priv
);
1499 * Now, check to see if there are
1500 * any methods still in use
1502 if (!check_method_table(method
)) {
1503 /* If not, release management method table */
1505 vendor_class
->method_table
[index
] = NULL
;
1506 memset(vendor_class
->oui
[index
], 0, 3);
1507 /* Any OUIs left ? */
1508 if (!check_vendor_class(vendor_class
)) {
1509 /* If not, release vendor class table */
1510 kfree(vendor_class
);
1511 vendor
->vendor_class
[mgmt_class
] = NULL
;
1512 /* Any other vendor classes left ? */
1513 if (!check_vendor_table(vendor
)) {
1516 agent_priv
->reg_req
->
1517 mgmt_class_version
].
1529 static struct ib_mad_agent_private
*
1530 find_mad_agent(struct ib_mad_port_private
*port_priv
,
1531 const struct ib_mad_hdr
*mad_hdr
)
1533 struct ib_mad_agent_private
*mad_agent
= NULL
;
1534 unsigned long flags
;
1536 if (ib_response_mad(mad_hdr
)) {
1540 * Routing is based on high 32 bits of transaction ID
1543 hi_tid
= be64_to_cpu(mad_hdr
->tid
) >> 32;
1545 mad_agent
= xa_load(&ib_mad_clients
, hi_tid
);
1546 if (mad_agent
&& !refcount_inc_not_zero(&mad_agent
->refcount
))
1550 struct ib_mad_mgmt_class_table
*class;
1551 struct ib_mad_mgmt_method_table
*method
;
1552 struct ib_mad_mgmt_vendor_class_table
*vendor
;
1553 struct ib_mad_mgmt_vendor_class
*vendor_class
;
1554 const struct ib_vendor_mad
*vendor_mad
;
1557 spin_lock_irqsave(&port_priv
->reg_lock
, flags
);
1559 * Routing is based on version, class, and method
1560 * For "newer" vendor MADs, also based on OUI
1562 if (mad_hdr
->class_version
>= MAX_MGMT_VERSION
)
1564 if (!is_vendor_class(mad_hdr
->mgmt_class
)) {
1565 class = port_priv
->version
[
1566 mad_hdr
->class_version
].class;
1569 if (convert_mgmt_class(mad_hdr
->mgmt_class
) >=
1570 ARRAY_SIZE(class->method_table
))
1572 method
= class->method_table
[convert_mgmt_class(
1573 mad_hdr
->mgmt_class
)];
1575 mad_agent
= method
->agent
[mad_hdr
->method
&
1576 ~IB_MGMT_METHOD_RESP
];
1578 vendor
= port_priv
->version
[
1579 mad_hdr
->class_version
].vendor
;
1582 vendor_class
= vendor
->vendor_class
[vendor_class_index(
1583 mad_hdr
->mgmt_class
)];
1586 /* Find matching OUI */
1587 vendor_mad
= (const struct ib_vendor_mad
*)mad_hdr
;
1588 index
= find_vendor_oui(vendor_class
, vendor_mad
->oui
);
1591 method
= vendor_class
->method_table
[index
];
1593 mad_agent
= method
->agent
[mad_hdr
->method
&
1594 ~IB_MGMT_METHOD_RESP
];
1598 refcount_inc(&mad_agent
->refcount
);
1600 spin_unlock_irqrestore(&port_priv
->reg_lock
, flags
);
1603 if (mad_agent
&& !mad_agent
->agent
.recv_handler
) {
1604 dev_notice(&port_priv
->device
->dev
,
1605 "No receive handler for client %p on port %u\n",
1606 &mad_agent
->agent
, port_priv
->port_num
);
1607 deref_mad_agent(mad_agent
);
1614 static int validate_mad(const struct ib_mad_hdr
*mad_hdr
,
1615 const struct ib_mad_qp_info
*qp_info
,
1619 u32 qp_num
= qp_info
->qp
->qp_num
;
1621 /* Make sure MAD base version is understood */
1622 if (mad_hdr
->base_version
!= IB_MGMT_BASE_VERSION
&&
1623 (!opa
|| mad_hdr
->base_version
!= OPA_MGMT_BASE_VERSION
)) {
1624 pr_err("MAD received with unsupported base version %u %s\n",
1625 mad_hdr
->base_version
, opa
? "(opa)" : "");
1629 /* Filter SMI packets sent to other than QP0 */
1630 if ((mad_hdr
->mgmt_class
== IB_MGMT_CLASS_SUBN_LID_ROUTED
) ||
1631 (mad_hdr
->mgmt_class
== IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
)) {
1635 /* CM attributes other than ClassPortInfo only use Send method */
1636 if ((mad_hdr
->mgmt_class
== IB_MGMT_CLASS_CM
) &&
1637 (mad_hdr
->attr_id
!= IB_MGMT_CLASSPORTINFO_ATTR_ID
) &&
1638 (mad_hdr
->method
!= IB_MGMT_METHOD_SEND
))
1640 /* Filter GSI packets sent to QP0 */
1649 static int is_rmpp_data_mad(const struct ib_mad_agent_private
*mad_agent_priv
,
1650 const struct ib_mad_hdr
*mad_hdr
)
1652 struct ib_rmpp_mad
*rmpp_mad
;
1654 rmpp_mad
= (struct ib_rmpp_mad
*)mad_hdr
;
1655 return !mad_agent_priv
->agent
.rmpp_version
||
1656 !ib_mad_kernel_rmpp_agent(&mad_agent_priv
->agent
) ||
1657 !(ib_get_rmpp_flags(&rmpp_mad
->rmpp_hdr
) &
1658 IB_MGMT_RMPP_FLAG_ACTIVE
) ||
1659 (rmpp_mad
->rmpp_hdr
.rmpp_type
== IB_MGMT_RMPP_TYPE_DATA
);
1662 static inline int rcv_has_same_class(const struct ib_mad_send_wr_private
*wr
,
1663 const struct ib_mad_recv_wc
*rwc
)
1665 return ((struct ib_mad_hdr
*)(wr
->send_buf
.mad
))->mgmt_class
==
1666 rwc
->recv_buf
.mad
->mad_hdr
.mgmt_class
;
1670 rcv_has_same_gid(const struct ib_mad_agent_private
*mad_agent_priv
,
1671 const struct ib_mad_send_wr_private
*wr
,
1672 const struct ib_mad_recv_wc
*rwc
)
1674 struct rdma_ah_attr attr
;
1675 u8 send_resp
, rcv_resp
;
1677 struct ib_device
*device
= mad_agent_priv
->agent
.device
;
1678 u32 port_num
= mad_agent_priv
->agent
.port_num
;
1682 send_resp
= ib_response_mad((struct ib_mad_hdr
*)wr
->send_buf
.mad
);
1683 rcv_resp
= ib_response_mad(&rwc
->recv_buf
.mad
->mad_hdr
);
1685 if (send_resp
== rcv_resp
)
1686 /* both requests, or both responses. GIDs different */
1689 if (rdma_query_ah(wr
->send_buf
.ah
, &attr
))
1690 /* Assume not equal, to avoid false positives. */
1693 has_grh
= !!(rdma_ah_get_ah_flags(&attr
) & IB_AH_GRH
);
1694 if (has_grh
!= !!(rwc
->wc
->wc_flags
& IB_WC_GRH
))
1695 /* one has GID, other does not. Assume different */
1698 if (!send_resp
&& rcv_resp
) {
1699 /* is request/response. */
1701 if (ib_get_cached_lmc(device
, port_num
, &lmc
))
1703 return (!lmc
|| !((rdma_ah_get_path_bits(&attr
) ^
1704 rwc
->wc
->dlid_path_bits
) &
1707 const struct ib_global_route
*grh
=
1708 rdma_ah_read_grh(&attr
);
1710 if (rdma_query_gid(device
, port_num
,
1711 grh
->sgid_index
, &sgid
))
1713 return !memcmp(sgid
.raw
, rwc
->recv_buf
.grh
->dgid
.raw
,
1719 return rdma_ah_get_dlid(&attr
) == rwc
->wc
->slid
;
1721 return !memcmp(rdma_ah_read_grh(&attr
)->dgid
.raw
,
1722 rwc
->recv_buf
.grh
->sgid
.raw
,
1726 static inline int is_direct(u8
class)
1728 return (class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
);
1731 struct ib_mad_send_wr_private
*
1732 ib_find_send_mad(const struct ib_mad_agent_private
*mad_agent_priv
,
1733 const struct ib_mad_recv_wc
*wc
)
1735 struct ib_mad_send_wr_private
*wr
;
1736 const struct ib_mad_hdr
*mad_hdr
;
1738 mad_hdr
= &wc
->recv_buf
.mad
->mad_hdr
;
1740 list_for_each_entry(wr
, &mad_agent_priv
->wait_list
, agent_list
) {
1741 if ((wr
->tid
== mad_hdr
->tid
) &&
1742 rcv_has_same_class(wr
, wc
) &&
1744 * Don't check GID for direct routed MADs.
1745 * These might have permissive LIDs.
1747 (is_direct(mad_hdr
->mgmt_class
) ||
1748 rcv_has_same_gid(mad_agent_priv
, wr
, wc
)))
1749 return (wr
->status
== IB_WC_SUCCESS
) ? wr
: NULL
;
1753 * It's possible to receive the response before we've
1754 * been notified that the send has completed
1756 list_for_each_entry(wr
, &mad_agent_priv
->send_list
, agent_list
) {
1757 if (is_rmpp_data_mad(mad_agent_priv
, wr
->send_buf
.mad
) &&
1758 wr
->tid
== mad_hdr
->tid
&&
1760 rcv_has_same_class(wr
, wc
) &&
1762 * Don't check GID for direct routed MADs.
1763 * These might have permissive LIDs.
1765 (is_direct(mad_hdr
->mgmt_class
) ||
1766 rcv_has_same_gid(mad_agent_priv
, wr
, wc
)))
1767 /* Verify request has not been canceled */
1768 return (wr
->status
== IB_WC_SUCCESS
) ? wr
: NULL
;
1773 void ib_mark_mad_done(struct ib_mad_send_wr_private
*mad_send_wr
)
1775 mad_send_wr
->timeout
= 0;
1776 if (mad_send_wr
->refcount
== 1)
1777 list_move_tail(&mad_send_wr
->agent_list
,
1778 &mad_send_wr
->mad_agent_priv
->done_list
);
1781 static void ib_mad_complete_recv(struct ib_mad_agent_private
*mad_agent_priv
,
1782 struct ib_mad_recv_wc
*mad_recv_wc
)
1784 struct ib_mad_send_wr_private
*mad_send_wr
;
1785 struct ib_mad_send_wc mad_send_wc
;
1786 unsigned long flags
;
1789 INIT_LIST_HEAD(&mad_recv_wc
->rmpp_list
);
1790 ret
= ib_mad_enforce_security(mad_agent_priv
,
1791 mad_recv_wc
->wc
->pkey_index
);
1793 ib_free_recv_mad(mad_recv_wc
);
1794 deref_mad_agent(mad_agent_priv
);
1798 list_add(&mad_recv_wc
->recv_buf
.list
, &mad_recv_wc
->rmpp_list
);
1799 if (ib_mad_kernel_rmpp_agent(&mad_agent_priv
->agent
)) {
1800 mad_recv_wc
= ib_process_rmpp_recv_wc(mad_agent_priv
,
1803 deref_mad_agent(mad_agent_priv
);
1808 /* Complete corresponding request */
1809 if (ib_response_mad(&mad_recv_wc
->recv_buf
.mad
->mad_hdr
)) {
1810 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
1811 mad_send_wr
= ib_find_send_mad(mad_agent_priv
, mad_recv_wc
);
1813 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
1814 if (!ib_mad_kernel_rmpp_agent(&mad_agent_priv
->agent
)
1815 && ib_is_mad_class_rmpp(mad_recv_wc
->recv_buf
.mad
->mad_hdr
.mgmt_class
)
1816 && (ib_get_rmpp_flags(&((struct ib_rmpp_mad
*)mad_recv_wc
->recv_buf
.mad
)->rmpp_hdr
)
1817 & IB_MGMT_RMPP_FLAG_ACTIVE
)) {
1818 /* user rmpp is in effect
1819 * and this is an active RMPP MAD
1821 mad_agent_priv
->agent
.recv_handler(
1822 &mad_agent_priv
->agent
, NULL
,
1824 deref_mad_agent(mad_agent_priv
);
1826 /* not user rmpp, revert to normal behavior and
1829 ib_free_recv_mad(mad_recv_wc
);
1830 deref_mad_agent(mad_agent_priv
);
1834 ib_mark_mad_done(mad_send_wr
);
1835 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
1837 /* Defined behavior is to complete response before request */
1838 mad_agent_priv
->agent
.recv_handler(
1839 &mad_agent_priv
->agent
,
1840 &mad_send_wr
->send_buf
,
1842 deref_mad_agent(mad_agent_priv
);
1844 mad_send_wc
.status
= IB_WC_SUCCESS
;
1845 mad_send_wc
.vendor_err
= 0;
1846 mad_send_wc
.send_buf
= &mad_send_wr
->send_buf
;
1847 ib_mad_complete_send_wr(mad_send_wr
, &mad_send_wc
);
1850 mad_agent_priv
->agent
.recv_handler(&mad_agent_priv
->agent
, NULL
,
1852 deref_mad_agent(mad_agent_priv
);
1856 static enum smi_action
handle_ib_smi(const struct ib_mad_port_private
*port_priv
,
1857 const struct ib_mad_qp_info
*qp_info
,
1858 const struct ib_wc
*wc
,
1860 struct ib_mad_private
*recv
,
1861 struct ib_mad_private
*response
)
1863 enum smi_forward_action retsmi
;
1864 struct ib_smp
*smp
= (struct ib_smp
*)recv
->mad
;
1866 trace_ib_mad_handle_ib_smi(smp
);
1868 if (smi_handle_dr_smp_recv(smp
,
1869 rdma_cap_ib_switch(port_priv
->device
),
1871 port_priv
->device
->phys_port_cnt
) ==
1873 return IB_SMI_DISCARD
;
1875 retsmi
= smi_check_forward_dr_smp(smp
);
1876 if (retsmi
== IB_SMI_LOCAL
)
1877 return IB_SMI_HANDLE
;
1879 if (retsmi
== IB_SMI_SEND
) { /* don't forward */
1880 if (smi_handle_dr_smp_send(smp
,
1881 rdma_cap_ib_switch(port_priv
->device
),
1882 port_num
) == IB_SMI_DISCARD
)
1883 return IB_SMI_DISCARD
;
1885 if (smi_check_local_smp(smp
, port_priv
->device
) == IB_SMI_DISCARD
)
1886 return IB_SMI_DISCARD
;
1887 } else if (rdma_cap_ib_switch(port_priv
->device
)) {
1888 /* forward case for switches */
1889 memcpy(response
, recv
, mad_priv_size(response
));
1890 response
->header
.recv_wc
.wc
= &response
->header
.wc
;
1891 response
->header
.recv_wc
.recv_buf
.mad
= (struct ib_mad
*)response
->mad
;
1892 response
->header
.recv_wc
.recv_buf
.grh
= &response
->grh
;
1894 agent_send_response((const struct ib_mad_hdr
*)response
->mad
,
1897 smi_get_fwd_port(smp
),
1898 qp_info
->qp
->qp_num
,
1902 return IB_SMI_DISCARD
;
1904 return IB_SMI_HANDLE
;
1907 static bool generate_unmatched_resp(const struct ib_mad_private
*recv
,
1908 struct ib_mad_private
*response
,
1909 size_t *resp_len
, bool opa
)
1911 const struct ib_mad_hdr
*recv_hdr
= (const struct ib_mad_hdr
*)recv
->mad
;
1912 struct ib_mad_hdr
*resp_hdr
= (struct ib_mad_hdr
*)response
->mad
;
1914 if (recv_hdr
->method
== IB_MGMT_METHOD_GET
||
1915 recv_hdr
->method
== IB_MGMT_METHOD_SET
) {
1916 memcpy(response
, recv
, mad_priv_size(response
));
1917 response
->header
.recv_wc
.wc
= &response
->header
.wc
;
1918 response
->header
.recv_wc
.recv_buf
.mad
= (struct ib_mad
*)response
->mad
;
1919 response
->header
.recv_wc
.recv_buf
.grh
= &response
->grh
;
1920 resp_hdr
->method
= IB_MGMT_METHOD_GET_RESP
;
1921 resp_hdr
->status
= cpu_to_be16(IB_MGMT_MAD_STATUS_UNSUPPORTED_METHOD_ATTRIB
);
1922 if (recv_hdr
->mgmt_class
== IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
)
1923 resp_hdr
->status
|= IB_SMP_DIRECTION
;
1925 if (opa
&& recv_hdr
->base_version
== OPA_MGMT_BASE_VERSION
) {
1926 if (recv_hdr
->mgmt_class
==
1927 IB_MGMT_CLASS_SUBN_LID_ROUTED
||
1928 recv_hdr
->mgmt_class
==
1929 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
)
1930 *resp_len
= opa_get_smp_header_size(
1931 (struct opa_smp
*)recv
->mad
);
1933 *resp_len
= sizeof(struct ib_mad_hdr
);
1942 static enum smi_action
1943 handle_opa_smi(struct ib_mad_port_private
*port_priv
,
1944 struct ib_mad_qp_info
*qp_info
,
1947 struct ib_mad_private
*recv
,
1948 struct ib_mad_private
*response
)
1950 enum smi_forward_action retsmi
;
1951 struct opa_smp
*smp
= (struct opa_smp
*)recv
->mad
;
1953 trace_ib_mad_handle_opa_smi(smp
);
1955 if (opa_smi_handle_dr_smp_recv(smp
,
1956 rdma_cap_ib_switch(port_priv
->device
),
1958 port_priv
->device
->phys_port_cnt
) ==
1960 return IB_SMI_DISCARD
;
1962 retsmi
= opa_smi_check_forward_dr_smp(smp
);
1963 if (retsmi
== IB_SMI_LOCAL
)
1964 return IB_SMI_HANDLE
;
1966 if (retsmi
== IB_SMI_SEND
) { /* don't forward */
1967 if (opa_smi_handle_dr_smp_send(smp
,
1968 rdma_cap_ib_switch(port_priv
->device
),
1969 port_num
) == IB_SMI_DISCARD
)
1970 return IB_SMI_DISCARD
;
1972 if (opa_smi_check_local_smp(smp
, port_priv
->device
) ==
1974 return IB_SMI_DISCARD
;
1976 } else if (rdma_cap_ib_switch(port_priv
->device
)) {
1977 /* forward case for switches */
1978 memcpy(response
, recv
, mad_priv_size(response
));
1979 response
->header
.recv_wc
.wc
= &response
->header
.wc
;
1980 response
->header
.recv_wc
.recv_buf
.opa_mad
=
1981 (struct opa_mad
*)response
->mad
;
1982 response
->header
.recv_wc
.recv_buf
.grh
= &response
->grh
;
1984 agent_send_response((const struct ib_mad_hdr
*)response
->mad
,
1987 opa_smi_get_fwd_port(smp
),
1988 qp_info
->qp
->qp_num
,
1989 recv
->header
.wc
.byte_len
,
1992 return IB_SMI_DISCARD
;
1995 return IB_SMI_HANDLE
;
1998 static enum smi_action
1999 handle_smi(struct ib_mad_port_private
*port_priv
,
2000 struct ib_mad_qp_info
*qp_info
,
2003 struct ib_mad_private
*recv
,
2004 struct ib_mad_private
*response
,
2007 struct ib_mad_hdr
*mad_hdr
= (struct ib_mad_hdr
*)recv
->mad
;
2009 if (opa
&& mad_hdr
->base_version
== OPA_MGMT_BASE_VERSION
&&
2010 mad_hdr
->class_version
== OPA_SM_CLASS_VERSION
)
2011 return handle_opa_smi(port_priv
, qp_info
, wc
, port_num
, recv
,
2014 return handle_ib_smi(port_priv
, qp_info
, wc
, port_num
, recv
, response
);
2017 static void ib_mad_recv_done(struct ib_cq
*cq
, struct ib_wc
*wc
)
2019 struct ib_mad_port_private
*port_priv
= cq
->cq_context
;
2020 struct ib_mad_list_head
*mad_list
=
2021 container_of(wc
->wr_cqe
, struct ib_mad_list_head
, cqe
);
2022 struct ib_mad_qp_info
*qp_info
;
2023 struct ib_mad_private_header
*mad_priv_hdr
;
2024 struct ib_mad_private
*recv
, *response
= NULL
;
2025 struct ib_mad_agent_private
*mad_agent
;
2027 int ret
= IB_MAD_RESULT_SUCCESS
;
2029 u16 resp_mad_pkey_index
= 0;
2032 if (list_empty_careful(&port_priv
->port_list
))
2035 if (wc
->status
!= IB_WC_SUCCESS
) {
2037 * Receive errors indicate that the QP has entered the error
2038 * state - error handling/shutdown code will cleanup
2043 qp_info
= mad_list
->mad_queue
->qp_info
;
2044 dequeue_mad(mad_list
);
2046 opa
= rdma_cap_opa_mad(qp_info
->port_priv
->device
,
2047 qp_info
->port_priv
->port_num
);
2049 mad_priv_hdr
= container_of(mad_list
, struct ib_mad_private_header
,
2051 recv
= container_of(mad_priv_hdr
, struct ib_mad_private
, header
);
2052 ib_dma_unmap_single(port_priv
->device
,
2053 recv
->header
.mapping
,
2054 mad_priv_dma_size(recv
),
2057 /* Setup MAD receive work completion from "normal" work completion */
2058 recv
->header
.wc
= *wc
;
2059 recv
->header
.recv_wc
.wc
= &recv
->header
.wc
;
2061 if (opa
&& ((struct ib_mad_hdr
*)(recv
->mad
))->base_version
== OPA_MGMT_BASE_VERSION
) {
2062 recv
->header
.recv_wc
.mad_len
= wc
->byte_len
- sizeof(struct ib_grh
);
2063 recv
->header
.recv_wc
.mad_seg_size
= sizeof(struct opa_mad
);
2065 recv
->header
.recv_wc
.mad_len
= sizeof(struct ib_mad
);
2066 recv
->header
.recv_wc
.mad_seg_size
= sizeof(struct ib_mad
);
2069 recv
->header
.recv_wc
.recv_buf
.mad
= (struct ib_mad
*)recv
->mad
;
2070 recv
->header
.recv_wc
.recv_buf
.grh
= &recv
->grh
;
2073 if (!validate_mad((const struct ib_mad_hdr
*)recv
->mad
, qp_info
, opa
))
2076 trace_ib_mad_recv_done_handler(qp_info
, wc
,
2077 (struct ib_mad_hdr
*)recv
->mad
);
2079 mad_size
= recv
->mad_size
;
2080 response
= alloc_mad_private(mad_size
, GFP_KERNEL
);
2084 if (rdma_cap_ib_switch(port_priv
->device
))
2085 port_num
= wc
->port_num
;
2087 port_num
= port_priv
->port_num
;
2089 if (((struct ib_mad_hdr
*)recv
->mad
)->mgmt_class
==
2090 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
) {
2091 if (handle_smi(port_priv
, qp_info
, wc
, port_num
, recv
,
2097 /* Give driver "right of first refusal" on incoming MAD */
2098 if (port_priv
->device
->ops
.process_mad
) {
2099 ret
= port_priv
->device
->ops
.process_mad(
2100 port_priv
->device
, 0, port_priv
->port_num
, wc
,
2101 &recv
->grh
, (const struct ib_mad
*)recv
->mad
,
2102 (struct ib_mad
*)response
->mad
, &mad_size
,
2103 &resp_mad_pkey_index
);
2106 wc
->pkey_index
= resp_mad_pkey_index
;
2108 if (ret
& IB_MAD_RESULT_SUCCESS
) {
2109 if (ret
& IB_MAD_RESULT_CONSUMED
)
2111 if (ret
& IB_MAD_RESULT_REPLY
) {
2112 agent_send_response((const struct ib_mad_hdr
*)response
->mad
,
2116 qp_info
->qp
->qp_num
,
2123 mad_agent
= find_mad_agent(port_priv
, (const struct ib_mad_hdr
*)recv
->mad
);
2125 trace_ib_mad_recv_done_agent(mad_agent
);
2126 ib_mad_complete_recv(mad_agent
, &recv
->header
.recv_wc
);
2128 * recv is freed up in error cases in ib_mad_complete_recv
2129 * or via recv_handler in ib_mad_complete_recv()
2132 } else if ((ret
& IB_MAD_RESULT_SUCCESS
) &&
2133 generate_unmatched_resp(recv
, response
, &mad_size
, opa
)) {
2134 agent_send_response((const struct ib_mad_hdr
*)response
->mad
, &recv
->grh
, wc
,
2135 port_priv
->device
, port_num
,
2136 qp_info
->qp
->qp_num
, mad_size
, opa
);
2140 /* Post another receive request for this QP */
2142 ib_mad_post_receive_mads(qp_info
, response
);
2145 ib_mad_post_receive_mads(qp_info
, recv
);
2148 static void adjust_timeout(struct ib_mad_agent_private
*mad_agent_priv
)
2150 struct ib_mad_send_wr_private
*mad_send_wr
;
2151 unsigned long delay
;
2153 if (list_empty(&mad_agent_priv
->wait_list
)) {
2154 cancel_delayed_work(&mad_agent_priv
->timed_work
);
2156 mad_send_wr
= list_entry(mad_agent_priv
->wait_list
.next
,
2157 struct ib_mad_send_wr_private
,
2160 if (time_after(mad_agent_priv
->timeout
,
2161 mad_send_wr
->timeout
)) {
2162 mad_agent_priv
->timeout
= mad_send_wr
->timeout
;
2163 delay
= mad_send_wr
->timeout
- jiffies
;
2164 if ((long)delay
<= 0)
2166 mod_delayed_work(mad_agent_priv
->qp_info
->port_priv
->wq
,
2167 &mad_agent_priv
->timed_work
, delay
);
2172 static void wait_for_response(struct ib_mad_send_wr_private
*mad_send_wr
)
2174 struct ib_mad_agent_private
*mad_agent_priv
;
2175 struct ib_mad_send_wr_private
*temp_mad_send_wr
;
2176 struct list_head
*list_item
;
2177 unsigned long delay
;
2179 mad_agent_priv
= mad_send_wr
->mad_agent_priv
;
2180 list_del(&mad_send_wr
->agent_list
);
2182 delay
= mad_send_wr
->timeout
;
2183 mad_send_wr
->timeout
+= jiffies
;
2186 list_for_each_prev(list_item
, &mad_agent_priv
->wait_list
) {
2187 temp_mad_send_wr
= list_entry(list_item
,
2188 struct ib_mad_send_wr_private
,
2190 if (time_after(mad_send_wr
->timeout
,
2191 temp_mad_send_wr
->timeout
))
2195 list_item
= &mad_agent_priv
->wait_list
;
2198 list_add(&mad_send_wr
->agent_list
, list_item
);
2200 /* Reschedule a work item if we have a shorter timeout */
2201 if (mad_agent_priv
->wait_list
.next
== &mad_send_wr
->agent_list
)
2202 mod_delayed_work(mad_agent_priv
->qp_info
->port_priv
->wq
,
2203 &mad_agent_priv
->timed_work
, delay
);
2206 void ib_reset_mad_timeout(struct ib_mad_send_wr_private
*mad_send_wr
,
2207 unsigned long timeout_ms
)
2209 mad_send_wr
->timeout
= msecs_to_jiffies(timeout_ms
);
2210 wait_for_response(mad_send_wr
);
2214 * Process a send work completion
2216 void ib_mad_complete_send_wr(struct ib_mad_send_wr_private
*mad_send_wr
,
2217 struct ib_mad_send_wc
*mad_send_wc
)
2219 struct ib_mad_agent_private
*mad_agent_priv
;
2220 unsigned long flags
;
2223 mad_agent_priv
= mad_send_wr
->mad_agent_priv
;
2224 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2225 if (ib_mad_kernel_rmpp_agent(&mad_agent_priv
->agent
)) {
2226 ret
= ib_process_rmpp_send_wc(mad_send_wr
, mad_send_wc
);
2227 if (ret
== IB_RMPP_RESULT_CONSUMED
)
2230 ret
= IB_RMPP_RESULT_UNHANDLED
;
2232 if (mad_send_wc
->status
!= IB_WC_SUCCESS
&&
2233 mad_send_wr
->status
== IB_WC_SUCCESS
) {
2234 mad_send_wr
->status
= mad_send_wc
->status
;
2235 mad_send_wr
->refcount
-= (mad_send_wr
->timeout
> 0);
2238 if (--mad_send_wr
->refcount
> 0) {
2239 if (mad_send_wr
->refcount
== 1 && mad_send_wr
->timeout
&&
2240 mad_send_wr
->status
== IB_WC_SUCCESS
) {
2241 wait_for_response(mad_send_wr
);
2246 /* Remove send from MAD agent and notify client of completion */
2247 list_del(&mad_send_wr
->agent_list
);
2248 adjust_timeout(mad_agent_priv
);
2249 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2251 if (mad_send_wr
->status
!= IB_WC_SUCCESS
)
2252 mad_send_wc
->status
= mad_send_wr
->status
;
2253 if (ret
== IB_RMPP_RESULT_INTERNAL
)
2254 ib_rmpp_send_handler(mad_send_wc
);
2256 mad_agent_priv
->agent
.send_handler(&mad_agent_priv
->agent
,
2259 /* Release reference on agent taken when sending */
2260 deref_mad_agent(mad_agent_priv
);
2263 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2266 static void ib_mad_send_done(struct ib_cq
*cq
, struct ib_wc
*wc
)
2268 struct ib_mad_port_private
*port_priv
= cq
->cq_context
;
2269 struct ib_mad_list_head
*mad_list
=
2270 container_of(wc
->wr_cqe
, struct ib_mad_list_head
, cqe
);
2271 struct ib_mad_send_wr_private
*mad_send_wr
, *queued_send_wr
;
2272 struct ib_mad_qp_info
*qp_info
;
2273 struct ib_mad_queue
*send_queue
;
2274 struct ib_mad_send_wc mad_send_wc
;
2275 unsigned long flags
;
2278 if (list_empty_careful(&port_priv
->port_list
))
2281 if (wc
->status
!= IB_WC_SUCCESS
) {
2282 if (!ib_mad_send_error(port_priv
, wc
))
2286 mad_send_wr
= container_of(mad_list
, struct ib_mad_send_wr_private
,
2288 send_queue
= mad_list
->mad_queue
;
2289 qp_info
= send_queue
->qp_info
;
2291 trace_ib_mad_send_done_agent(mad_send_wr
->mad_agent_priv
);
2292 trace_ib_mad_send_done_handler(mad_send_wr
, wc
);
2295 ib_dma_unmap_single(mad_send_wr
->send_buf
.mad_agent
->device
,
2296 mad_send_wr
->header_mapping
,
2297 mad_send_wr
->sg_list
[0].length
, DMA_TO_DEVICE
);
2298 ib_dma_unmap_single(mad_send_wr
->send_buf
.mad_agent
->device
,
2299 mad_send_wr
->payload_mapping
,
2300 mad_send_wr
->sg_list
[1].length
, DMA_TO_DEVICE
);
2301 queued_send_wr
= NULL
;
2302 spin_lock_irqsave(&send_queue
->lock
, flags
);
2303 list_del(&mad_list
->list
);
2305 /* Move queued send to the send queue */
2306 if (send_queue
->count
-- > send_queue
->max_active
) {
2307 mad_list
= container_of(qp_info
->overflow_list
.next
,
2308 struct ib_mad_list_head
, list
);
2309 queued_send_wr
= container_of(mad_list
,
2310 struct ib_mad_send_wr_private
,
2312 list_move_tail(&mad_list
->list
, &send_queue
->list
);
2314 spin_unlock_irqrestore(&send_queue
->lock
, flags
);
2316 mad_send_wc
.send_buf
= &mad_send_wr
->send_buf
;
2317 mad_send_wc
.status
= wc
->status
;
2318 mad_send_wc
.vendor_err
= wc
->vendor_err
;
2319 ib_mad_complete_send_wr(mad_send_wr
, &mad_send_wc
);
2321 if (queued_send_wr
) {
2322 trace_ib_mad_send_done_resend(queued_send_wr
, qp_info
);
2323 ret
= ib_post_send(qp_info
->qp
, &queued_send_wr
->send_wr
.wr
,
2326 dev_err(&port_priv
->device
->dev
,
2327 "ib_post_send failed: %d\n", ret
);
2328 mad_send_wr
= queued_send_wr
;
2329 wc
->status
= IB_WC_LOC_QP_OP_ERR
;
2335 static void mark_sends_for_retry(struct ib_mad_qp_info
*qp_info
)
2337 struct ib_mad_send_wr_private
*mad_send_wr
;
2338 struct ib_mad_list_head
*mad_list
;
2339 unsigned long flags
;
2341 spin_lock_irqsave(&qp_info
->send_queue
.lock
, flags
);
2342 list_for_each_entry(mad_list
, &qp_info
->send_queue
.list
, list
) {
2343 mad_send_wr
= container_of(mad_list
,
2344 struct ib_mad_send_wr_private
,
2346 mad_send_wr
->retry
= 1;
2348 spin_unlock_irqrestore(&qp_info
->send_queue
.lock
, flags
);
2351 static bool ib_mad_send_error(struct ib_mad_port_private
*port_priv
,
2354 struct ib_mad_list_head
*mad_list
=
2355 container_of(wc
->wr_cqe
, struct ib_mad_list_head
, cqe
);
2356 struct ib_mad_qp_info
*qp_info
= mad_list
->mad_queue
->qp_info
;
2357 struct ib_mad_send_wr_private
*mad_send_wr
;
2361 * Send errors will transition the QP to SQE - move
2362 * QP to RTS and repost flushed work requests
2364 mad_send_wr
= container_of(mad_list
, struct ib_mad_send_wr_private
,
2366 if (wc
->status
== IB_WC_WR_FLUSH_ERR
) {
2367 if (mad_send_wr
->retry
) {
2369 mad_send_wr
->retry
= 0;
2370 trace_ib_mad_error_handler(mad_send_wr
, qp_info
);
2371 ret
= ib_post_send(qp_info
->qp
, &mad_send_wr
->send_wr
.wr
,
2377 struct ib_qp_attr
*attr
;
2379 /* Transition QP to RTS and fail offending send */
2380 attr
= kmalloc(sizeof *attr
, GFP_KERNEL
);
2382 attr
->qp_state
= IB_QPS_RTS
;
2383 attr
->cur_qp_state
= IB_QPS_SQE
;
2384 ret
= ib_modify_qp(qp_info
->qp
, attr
,
2385 IB_QP_STATE
| IB_QP_CUR_STATE
);
2388 dev_err(&port_priv
->device
->dev
,
2389 "%s - ib_modify_qp to RTS: %d\n",
2392 mark_sends_for_retry(qp_info
);
2399 static void cancel_mads(struct ib_mad_agent_private
*mad_agent_priv
)
2401 unsigned long flags
;
2402 struct ib_mad_send_wr_private
*mad_send_wr
, *temp_mad_send_wr
;
2403 struct ib_mad_send_wc mad_send_wc
;
2404 struct list_head cancel_list
;
2406 INIT_LIST_HEAD(&cancel_list
);
2408 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2409 list_for_each_entry_safe(mad_send_wr
, temp_mad_send_wr
,
2410 &mad_agent_priv
->send_list
, agent_list
) {
2411 if (mad_send_wr
->status
== IB_WC_SUCCESS
) {
2412 mad_send_wr
->status
= IB_WC_WR_FLUSH_ERR
;
2413 mad_send_wr
->refcount
-= (mad_send_wr
->timeout
> 0);
2417 /* Empty wait list to prevent receives from finding a request */
2418 list_splice_init(&mad_agent_priv
->wait_list
, &cancel_list
);
2419 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2421 /* Report all cancelled requests */
2422 mad_send_wc
.status
= IB_WC_WR_FLUSH_ERR
;
2423 mad_send_wc
.vendor_err
= 0;
2425 list_for_each_entry_safe(mad_send_wr
, temp_mad_send_wr
,
2426 &cancel_list
, agent_list
) {
2427 mad_send_wc
.send_buf
= &mad_send_wr
->send_buf
;
2428 list_del(&mad_send_wr
->agent_list
);
2429 mad_agent_priv
->agent
.send_handler(&mad_agent_priv
->agent
,
2431 deref_mad_agent(mad_agent_priv
);
2435 static struct ib_mad_send_wr_private
*
2436 find_send_wr(struct ib_mad_agent_private
*mad_agent_priv
,
2437 struct ib_mad_send_buf
*send_buf
)
2439 struct ib_mad_send_wr_private
*mad_send_wr
;
2441 list_for_each_entry(mad_send_wr
, &mad_agent_priv
->wait_list
,
2443 if (&mad_send_wr
->send_buf
== send_buf
)
2447 list_for_each_entry(mad_send_wr
, &mad_agent_priv
->send_list
,
2449 if (is_rmpp_data_mad(mad_agent_priv
,
2450 mad_send_wr
->send_buf
.mad
) &&
2451 &mad_send_wr
->send_buf
== send_buf
)
2457 int ib_modify_mad(struct ib_mad_send_buf
*send_buf
, u32 timeout_ms
)
2459 struct ib_mad_agent_private
*mad_agent_priv
;
2460 struct ib_mad_send_wr_private
*mad_send_wr
;
2461 unsigned long flags
;
2467 mad_agent_priv
= container_of(send_buf
->mad_agent
,
2468 struct ib_mad_agent_private
, agent
);
2469 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2470 mad_send_wr
= find_send_wr(mad_agent_priv
, send_buf
);
2471 if (!mad_send_wr
|| mad_send_wr
->status
!= IB_WC_SUCCESS
) {
2472 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2476 active
= (!mad_send_wr
->timeout
|| mad_send_wr
->refcount
> 1);
2478 mad_send_wr
->status
= IB_WC_WR_FLUSH_ERR
;
2479 mad_send_wr
->refcount
-= (mad_send_wr
->timeout
> 0);
2482 mad_send_wr
->send_buf
.timeout_ms
= timeout_ms
;
2484 mad_send_wr
->timeout
= msecs_to_jiffies(timeout_ms
);
2486 ib_reset_mad_timeout(mad_send_wr
, timeout_ms
);
2488 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2491 EXPORT_SYMBOL(ib_modify_mad
);
2493 static void local_completions(struct work_struct
*work
)
2495 struct ib_mad_agent_private
*mad_agent_priv
;
2496 struct ib_mad_local_private
*local
;
2497 struct ib_mad_agent_private
*recv_mad_agent
;
2498 unsigned long flags
;
2501 struct ib_mad_send_wc mad_send_wc
;
2505 container_of(work
, struct ib_mad_agent_private
, local_work
);
2507 opa
= rdma_cap_opa_mad(mad_agent_priv
->qp_info
->port_priv
->device
,
2508 mad_agent_priv
->qp_info
->port_priv
->port_num
);
2510 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2511 while (!list_empty(&mad_agent_priv
->local_list
)) {
2512 local
= list_entry(mad_agent_priv
->local_list
.next
,
2513 struct ib_mad_local_private
,
2515 list_del(&local
->completion_list
);
2516 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2518 if (local
->mad_priv
) {
2520 recv_mad_agent
= local
->recv_mad_agent
;
2521 if (!recv_mad_agent
) {
2522 dev_err(&mad_agent_priv
->agent
.device
->dev
,
2523 "No receive MAD agent for local completion\n");
2525 goto local_send_completion
;
2529 * Defined behavior is to complete response
2532 build_smp_wc(recv_mad_agent
->agent
.qp
,
2533 local
->mad_send_wr
->send_wr
.wr
.wr_cqe
,
2534 be16_to_cpu(IB_LID_PERMISSIVE
),
2535 local
->mad_send_wr
->send_wr
.pkey_index
,
2536 recv_mad_agent
->agent
.port_num
, &wc
);
2538 local
->mad_priv
->header
.recv_wc
.wc
= &wc
;
2540 base_version
= ((struct ib_mad_hdr
*)(local
->mad_priv
->mad
))->base_version
;
2541 if (opa
&& base_version
== OPA_MGMT_BASE_VERSION
) {
2542 local
->mad_priv
->header
.recv_wc
.mad_len
= local
->return_wc_byte_len
;
2543 local
->mad_priv
->header
.recv_wc
.mad_seg_size
= sizeof(struct opa_mad
);
2545 local
->mad_priv
->header
.recv_wc
.mad_len
= sizeof(struct ib_mad
);
2546 local
->mad_priv
->header
.recv_wc
.mad_seg_size
= sizeof(struct ib_mad
);
2549 INIT_LIST_HEAD(&local
->mad_priv
->header
.recv_wc
.rmpp_list
);
2550 list_add(&local
->mad_priv
->header
.recv_wc
.recv_buf
.list
,
2551 &local
->mad_priv
->header
.recv_wc
.rmpp_list
);
2552 local
->mad_priv
->header
.recv_wc
.recv_buf
.grh
= NULL
;
2553 local
->mad_priv
->header
.recv_wc
.recv_buf
.mad
=
2554 (struct ib_mad
*)local
->mad_priv
->mad
;
2555 recv_mad_agent
->agent
.recv_handler(
2556 &recv_mad_agent
->agent
,
2557 &local
->mad_send_wr
->send_buf
,
2558 &local
->mad_priv
->header
.recv_wc
);
2559 spin_lock_irqsave(&recv_mad_agent
->lock
, flags
);
2560 deref_mad_agent(recv_mad_agent
);
2561 spin_unlock_irqrestore(&recv_mad_agent
->lock
, flags
);
2564 local_send_completion
:
2566 mad_send_wc
.status
= IB_WC_SUCCESS
;
2567 mad_send_wc
.vendor_err
= 0;
2568 mad_send_wc
.send_buf
= &local
->mad_send_wr
->send_buf
;
2569 mad_agent_priv
->agent
.send_handler(&mad_agent_priv
->agent
,
2572 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2573 deref_mad_agent(mad_agent_priv
);
2575 kfree(local
->mad_priv
);
2578 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2581 static int retry_send(struct ib_mad_send_wr_private
*mad_send_wr
)
2585 if (!mad_send_wr
->retries_left
)
2588 mad_send_wr
->retries_left
--;
2589 mad_send_wr
->send_buf
.retries
++;
2591 mad_send_wr
->timeout
= msecs_to_jiffies(mad_send_wr
->send_buf
.timeout_ms
);
2593 if (ib_mad_kernel_rmpp_agent(&mad_send_wr
->mad_agent_priv
->agent
)) {
2594 ret
= ib_retry_rmpp(mad_send_wr
);
2596 case IB_RMPP_RESULT_UNHANDLED
:
2597 ret
= ib_send_mad(mad_send_wr
);
2599 case IB_RMPP_RESULT_CONSUMED
:
2607 ret
= ib_send_mad(mad_send_wr
);
2610 mad_send_wr
->refcount
++;
2611 list_add_tail(&mad_send_wr
->agent_list
,
2612 &mad_send_wr
->mad_agent_priv
->send_list
);
2617 static void timeout_sends(struct work_struct
*work
)
2619 struct ib_mad_send_wr_private
*mad_send_wr
, *n
;
2620 struct ib_mad_agent_private
*mad_agent_priv
;
2621 struct ib_mad_send_wc mad_send_wc
;
2622 struct list_head local_list
;
2623 unsigned long flags
, delay
;
2625 mad_agent_priv
= container_of(work
, struct ib_mad_agent_private
,
2627 mad_send_wc
.vendor_err
= 0;
2628 INIT_LIST_HEAD(&local_list
);
2630 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2631 while (!list_empty(&mad_agent_priv
->wait_list
)) {
2632 mad_send_wr
= list_entry(mad_agent_priv
->wait_list
.next
,
2633 struct ib_mad_send_wr_private
,
2636 if (time_after(mad_send_wr
->timeout
, jiffies
)) {
2637 delay
= mad_send_wr
->timeout
- jiffies
;
2638 if ((long)delay
<= 0)
2640 queue_delayed_work(mad_agent_priv
->qp_info
->
2642 &mad_agent_priv
->timed_work
, delay
);
2646 list_del_init(&mad_send_wr
->agent_list
);
2647 if (mad_send_wr
->status
== IB_WC_SUCCESS
&&
2648 !retry_send(mad_send_wr
))
2651 list_add_tail(&mad_send_wr
->agent_list
, &local_list
);
2653 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2655 list_for_each_entry_safe(mad_send_wr
, n
, &local_list
, agent_list
) {
2656 if (mad_send_wr
->status
== IB_WC_SUCCESS
)
2657 mad_send_wc
.status
= IB_WC_RESP_TIMEOUT_ERR
;
2659 mad_send_wc
.status
= mad_send_wr
->status
;
2660 mad_send_wc
.send_buf
= &mad_send_wr
->send_buf
;
2661 mad_agent_priv
->agent
.send_handler(&mad_agent_priv
->agent
,
2663 deref_mad_agent(mad_agent_priv
);
2668 * Allocate receive MADs and post receive WRs for them
2670 static int ib_mad_post_receive_mads(struct ib_mad_qp_info
*qp_info
,
2671 struct ib_mad_private
*mad
)
2673 unsigned long flags
;
2675 struct ib_mad_private
*mad_priv
;
2676 struct ib_sge sg_list
;
2677 struct ib_recv_wr recv_wr
;
2678 struct ib_mad_queue
*recv_queue
= &qp_info
->recv_queue
;
2680 /* Initialize common scatter list fields */
2681 sg_list
.lkey
= qp_info
->port_priv
->pd
->local_dma_lkey
;
2683 /* Initialize common receive WR fields */
2684 recv_wr
.next
= NULL
;
2685 recv_wr
.sg_list
= &sg_list
;
2686 recv_wr
.num_sge
= 1;
2689 /* Allocate and map receive buffer */
2694 mad_priv
= alloc_mad_private(port_mad_size(qp_info
->port_priv
),
2701 sg_list
.length
= mad_priv_dma_size(mad_priv
);
2702 sg_list
.addr
= ib_dma_map_single(qp_info
->port_priv
->device
,
2704 mad_priv_dma_size(mad_priv
),
2706 if (unlikely(ib_dma_mapping_error(qp_info
->port_priv
->device
,
2712 mad_priv
->header
.mapping
= sg_list
.addr
;
2713 mad_priv
->header
.mad_list
.mad_queue
= recv_queue
;
2714 mad_priv
->header
.mad_list
.cqe
.done
= ib_mad_recv_done
;
2715 recv_wr
.wr_cqe
= &mad_priv
->header
.mad_list
.cqe
;
2717 /* Post receive WR */
2718 spin_lock_irqsave(&recv_queue
->lock
, flags
);
2719 post
= (++recv_queue
->count
< recv_queue
->max_active
);
2720 list_add_tail(&mad_priv
->header
.mad_list
.list
, &recv_queue
->list
);
2721 spin_unlock_irqrestore(&recv_queue
->lock
, flags
);
2722 ret
= ib_post_recv(qp_info
->qp
, &recv_wr
, NULL
);
2724 spin_lock_irqsave(&recv_queue
->lock
, flags
);
2725 list_del(&mad_priv
->header
.mad_list
.list
);
2726 recv_queue
->count
--;
2727 spin_unlock_irqrestore(&recv_queue
->lock
, flags
);
2728 ib_dma_unmap_single(qp_info
->port_priv
->device
,
2729 mad_priv
->header
.mapping
,
2730 mad_priv_dma_size(mad_priv
),
2733 dev_err(&qp_info
->port_priv
->device
->dev
,
2734 "ib_post_recv failed: %d\n", ret
);
2743 * Return all the posted receive MADs
2745 static void cleanup_recv_queue(struct ib_mad_qp_info
*qp_info
)
2747 struct ib_mad_private_header
*mad_priv_hdr
;
2748 struct ib_mad_private
*recv
;
2749 struct ib_mad_list_head
*mad_list
;
2754 while (!list_empty(&qp_info
->recv_queue
.list
)) {
2756 mad_list
= list_entry(qp_info
->recv_queue
.list
.next
,
2757 struct ib_mad_list_head
, list
);
2758 mad_priv_hdr
= container_of(mad_list
,
2759 struct ib_mad_private_header
,
2761 recv
= container_of(mad_priv_hdr
, struct ib_mad_private
,
2764 /* Remove from posted receive MAD list */
2765 list_del(&mad_list
->list
);
2767 ib_dma_unmap_single(qp_info
->port_priv
->device
,
2768 recv
->header
.mapping
,
2769 mad_priv_dma_size(recv
),
2774 qp_info
->recv_queue
.count
= 0;
2780 static int ib_mad_port_start(struct ib_mad_port_private
*port_priv
)
2783 struct ib_qp_attr
*attr
;
2787 attr
= kmalloc(sizeof *attr
, GFP_KERNEL
);
2791 ret
= ib_find_pkey(port_priv
->device
, port_priv
->port_num
,
2792 IB_DEFAULT_PKEY_FULL
, &pkey_index
);
2796 for (i
= 0; i
< IB_MAD_QPS_CORE
; i
++) {
2797 qp
= port_priv
->qp_info
[i
].qp
;
2802 * PKey index for QP1 is irrelevant but
2803 * one is needed for the Reset to Init transition
2805 attr
->qp_state
= IB_QPS_INIT
;
2806 attr
->pkey_index
= pkey_index
;
2807 attr
->qkey
= (qp
->qp_num
== 0) ? 0 : IB_QP1_QKEY
;
2808 ret
= ib_modify_qp(qp
, attr
, IB_QP_STATE
|
2809 IB_QP_PKEY_INDEX
| IB_QP_QKEY
);
2811 dev_err(&port_priv
->device
->dev
,
2812 "Couldn't change QP%d state to INIT: %d\n",
2817 attr
->qp_state
= IB_QPS_RTR
;
2818 ret
= ib_modify_qp(qp
, attr
, IB_QP_STATE
);
2820 dev_err(&port_priv
->device
->dev
,
2821 "Couldn't change QP%d state to RTR: %d\n",
2826 attr
->qp_state
= IB_QPS_RTS
;
2827 attr
->sq_psn
= IB_MAD_SEND_Q_PSN
;
2828 ret
= ib_modify_qp(qp
, attr
, IB_QP_STATE
| IB_QP_SQ_PSN
);
2830 dev_err(&port_priv
->device
->dev
,
2831 "Couldn't change QP%d state to RTS: %d\n",
2837 ret
= ib_req_notify_cq(port_priv
->cq
, IB_CQ_NEXT_COMP
);
2839 dev_err(&port_priv
->device
->dev
,
2840 "Failed to request completion notification: %d\n",
2845 for (i
= 0; i
< IB_MAD_QPS_CORE
; i
++) {
2846 if (!port_priv
->qp_info
[i
].qp
)
2849 ret
= ib_mad_post_receive_mads(&port_priv
->qp_info
[i
], NULL
);
2851 dev_err(&port_priv
->device
->dev
,
2852 "Couldn't post receive WRs\n");
2861 static void qp_event_handler(struct ib_event
*event
, void *qp_context
)
2863 struct ib_mad_qp_info
*qp_info
= qp_context
;
2865 /* It's worse than that! He's dead, Jim! */
2866 dev_err(&qp_info
->port_priv
->device
->dev
,
2867 "Fatal error (%d) on MAD QP (%u)\n",
2868 event
->event
, qp_info
->qp
->qp_num
);
2871 static void init_mad_queue(struct ib_mad_qp_info
*qp_info
,
2872 struct ib_mad_queue
*mad_queue
)
2874 mad_queue
->qp_info
= qp_info
;
2875 mad_queue
->count
= 0;
2876 spin_lock_init(&mad_queue
->lock
);
2877 INIT_LIST_HEAD(&mad_queue
->list
);
2880 static void init_mad_qp(struct ib_mad_port_private
*port_priv
,
2881 struct ib_mad_qp_info
*qp_info
)
2883 qp_info
->port_priv
= port_priv
;
2884 init_mad_queue(qp_info
, &qp_info
->send_queue
);
2885 init_mad_queue(qp_info
, &qp_info
->recv_queue
);
2886 INIT_LIST_HEAD(&qp_info
->overflow_list
);
2889 static int create_mad_qp(struct ib_mad_qp_info
*qp_info
,
2890 enum ib_qp_type qp_type
)
2892 struct ib_qp_init_attr qp_init_attr
;
2895 memset(&qp_init_attr
, 0, sizeof qp_init_attr
);
2896 qp_init_attr
.send_cq
= qp_info
->port_priv
->cq
;
2897 qp_init_attr
.recv_cq
= qp_info
->port_priv
->cq
;
2898 qp_init_attr
.sq_sig_type
= IB_SIGNAL_ALL_WR
;
2899 qp_init_attr
.cap
.max_send_wr
= mad_sendq_size
;
2900 qp_init_attr
.cap
.max_recv_wr
= mad_recvq_size
;
2901 qp_init_attr
.cap
.max_send_sge
= IB_MAD_SEND_REQ_MAX_SG
;
2902 qp_init_attr
.cap
.max_recv_sge
= IB_MAD_RECV_REQ_MAX_SG
;
2903 qp_init_attr
.qp_type
= qp_type
;
2904 qp_init_attr
.port_num
= qp_info
->port_priv
->port_num
;
2905 qp_init_attr
.qp_context
= qp_info
;
2906 qp_init_attr
.event_handler
= qp_event_handler
;
2907 qp_info
->qp
= ib_create_qp(qp_info
->port_priv
->pd
, &qp_init_attr
);
2908 if (IS_ERR(qp_info
->qp
)) {
2909 dev_err(&qp_info
->port_priv
->device
->dev
,
2910 "Couldn't create ib_mad QP%d\n",
2911 get_spl_qp_index(qp_type
));
2912 ret
= PTR_ERR(qp_info
->qp
);
2915 /* Use minimum queue sizes unless the CQ is resized */
2916 qp_info
->send_queue
.max_active
= mad_sendq_size
;
2917 qp_info
->recv_queue
.max_active
= mad_recvq_size
;
2924 static void destroy_mad_qp(struct ib_mad_qp_info
*qp_info
)
2929 ib_destroy_qp(qp_info
->qp
);
2934 * Create the QP, PD, MR, and CQ if needed
2936 static int ib_mad_port_open(struct ib_device
*device
,
2940 struct ib_mad_port_private
*port_priv
;
2941 unsigned long flags
;
2944 if (WARN_ON(rdma_max_mad_size(device
, port_num
) < IB_MGMT_MAD_SIZE
))
2947 if (WARN_ON(rdma_cap_opa_mad(device
, port_num
) &&
2948 rdma_max_mad_size(device
, port_num
) < OPA_MGMT_MAD_SIZE
))
2951 /* Create new device info */
2952 port_priv
= kzalloc(sizeof *port_priv
, GFP_KERNEL
);
2956 port_priv
->device
= device
;
2957 port_priv
->port_num
= port_num
;
2958 spin_lock_init(&port_priv
->reg_lock
);
2959 init_mad_qp(port_priv
, &port_priv
->qp_info
[0]);
2960 init_mad_qp(port_priv
, &port_priv
->qp_info
[1]);
2962 cq_size
= mad_sendq_size
+ mad_recvq_size
;
2963 has_smi
= rdma_cap_ib_smi(device
, port_num
);
2967 port_priv
->pd
= ib_alloc_pd(device
, 0);
2968 if (IS_ERR(port_priv
->pd
)) {
2969 dev_err(&device
->dev
, "Couldn't create ib_mad PD\n");
2970 ret
= PTR_ERR(port_priv
->pd
);
2974 port_priv
->cq
= ib_alloc_cq(port_priv
->device
, port_priv
, cq_size
, 0,
2975 IB_POLL_UNBOUND_WORKQUEUE
);
2976 if (IS_ERR(port_priv
->cq
)) {
2977 dev_err(&device
->dev
, "Couldn't create ib_mad CQ\n");
2978 ret
= PTR_ERR(port_priv
->cq
);
2983 ret
= create_mad_qp(&port_priv
->qp_info
[0], IB_QPT_SMI
);
2988 if (rdma_cap_ib_cm(device
, port_num
)) {
2989 ret
= create_mad_qp(&port_priv
->qp_info
[1], IB_QPT_GSI
);
2994 port_priv
->wq
= alloc_ordered_workqueue("ib_mad%u", WQ_MEM_RECLAIM
,
2996 if (!port_priv
->wq
) {
3001 spin_lock_irqsave(&ib_mad_port_list_lock
, flags
);
3002 list_add_tail(&port_priv
->port_list
, &ib_mad_port_list
);
3003 spin_unlock_irqrestore(&ib_mad_port_list_lock
, flags
);
3005 ret
= ib_mad_port_start(port_priv
);
3007 dev_err(&device
->dev
, "Couldn't start port\n");
3014 spin_lock_irqsave(&ib_mad_port_list_lock
, flags
);
3015 list_del_init(&port_priv
->port_list
);
3016 spin_unlock_irqrestore(&ib_mad_port_list_lock
, flags
);
3018 destroy_workqueue(port_priv
->wq
);
3020 destroy_mad_qp(&port_priv
->qp_info
[1]);
3022 destroy_mad_qp(&port_priv
->qp_info
[0]);
3024 ib_free_cq(port_priv
->cq
);
3025 cleanup_recv_queue(&port_priv
->qp_info
[1]);
3026 cleanup_recv_queue(&port_priv
->qp_info
[0]);
3028 ib_dealloc_pd(port_priv
->pd
);
3037 * If there are no classes using the port, free the port
3038 * resources (CQ, MR, PD, QP) and remove the port's info structure
3040 static int ib_mad_port_close(struct ib_device
*device
, u32 port_num
)
3042 struct ib_mad_port_private
*port_priv
;
3043 unsigned long flags
;
3045 spin_lock_irqsave(&ib_mad_port_list_lock
, flags
);
3046 port_priv
= __ib_get_mad_port(device
, port_num
);
3047 if (port_priv
== NULL
) {
3048 spin_unlock_irqrestore(&ib_mad_port_list_lock
, flags
);
3049 dev_err(&device
->dev
, "Port %u not found\n", port_num
);
3052 list_del_init(&port_priv
->port_list
);
3053 spin_unlock_irqrestore(&ib_mad_port_list_lock
, flags
);
3055 destroy_workqueue(port_priv
->wq
);
3056 destroy_mad_qp(&port_priv
->qp_info
[1]);
3057 destroy_mad_qp(&port_priv
->qp_info
[0]);
3058 ib_free_cq(port_priv
->cq
);
3059 ib_dealloc_pd(port_priv
->pd
);
3060 cleanup_recv_queue(&port_priv
->qp_info
[1]);
3061 cleanup_recv_queue(&port_priv
->qp_info
[0]);
3062 /* XXX: Handle deallocation of MAD registration tables */
3069 static int ib_mad_init_device(struct ib_device
*device
)
3072 unsigned int count
= 0;
3075 start
= rdma_start_port(device
);
3077 for (i
= start
; i
<= rdma_end_port(device
); i
++) {
3078 if (!rdma_cap_ib_mad(device
, i
))
3081 ret
= ib_mad_port_open(device
, i
);
3083 dev_err(&device
->dev
, "Couldn't open port %d\n", i
);
3086 ret
= ib_agent_port_open(device
, i
);
3088 dev_err(&device
->dev
,
3089 "Couldn't open port %d for agents\n", i
);
3100 if (ib_mad_port_close(device
, i
))
3101 dev_err(&device
->dev
, "Couldn't close port %d\n", i
);
3104 while (--i
>= start
) {
3105 if (!rdma_cap_ib_mad(device
, i
))
3108 if (ib_agent_port_close(device
, i
))
3109 dev_err(&device
->dev
,
3110 "Couldn't close port %d for agents\n", i
);
3111 if (ib_mad_port_close(device
, i
))
3112 dev_err(&device
->dev
, "Couldn't close port %d\n", i
);
3117 static void ib_mad_remove_device(struct ib_device
*device
, void *client_data
)
3121 rdma_for_each_port (device
, i
) {
3122 if (!rdma_cap_ib_mad(device
, i
))
3125 if (ib_agent_port_close(device
, i
))
3126 dev_err(&device
->dev
,
3127 "Couldn't close port %u for agents\n", i
);
3128 if (ib_mad_port_close(device
, i
))
3129 dev_err(&device
->dev
, "Couldn't close port %u\n", i
);
3133 static struct ib_client mad_client
= {
3135 .add
= ib_mad_init_device
,
3136 .remove
= ib_mad_remove_device
3139 int ib_mad_init(void)
3141 mad_recvq_size
= min(mad_recvq_size
, IB_MAD_QP_MAX_SIZE
);
3142 mad_recvq_size
= max(mad_recvq_size
, IB_MAD_QP_MIN_SIZE
);
3144 mad_sendq_size
= min(mad_sendq_size
, IB_MAD_QP_MAX_SIZE
);
3145 mad_sendq_size
= max(mad_sendq_size
, IB_MAD_QP_MIN_SIZE
);
3147 INIT_LIST_HEAD(&ib_mad_port_list
);
3149 if (ib_register_client(&mad_client
)) {
3150 pr_err("Couldn't register ib_mad client\n");
3157 void ib_mad_cleanup(void)
3159 ib_unregister_client(&mad_client
);