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 <rdma/ib_cache.h>
51 MODULE_LICENSE("Dual BSD/GPL");
52 MODULE_DESCRIPTION("kernel IB MAD API");
53 MODULE_AUTHOR("Hal Rosenstock");
54 MODULE_AUTHOR("Sean Hefty");
56 static int mad_sendq_size
= IB_MAD_QP_SEND_SIZE
;
57 static int mad_recvq_size
= IB_MAD_QP_RECV_SIZE
;
59 module_param_named(send_queue_size
, mad_sendq_size
, int, 0444);
60 MODULE_PARM_DESC(send_queue_size
, "Size of send queue in number of work requests");
61 module_param_named(recv_queue_size
, mad_recvq_size
, int, 0444);
62 MODULE_PARM_DESC(recv_queue_size
, "Size of receive queue in number of work requests");
64 static struct list_head ib_mad_port_list
;
65 static u32 ib_mad_client_id
= 0;
68 static DEFINE_SPINLOCK(ib_mad_port_list_lock
);
70 /* Forward declarations */
71 static int method_in_use(struct ib_mad_mgmt_method_table
**method
,
72 struct ib_mad_reg_req
*mad_reg_req
);
73 static void remove_mad_reg_req(struct ib_mad_agent_private
*priv
);
74 static struct ib_mad_agent_private
*find_mad_agent(
75 struct ib_mad_port_private
*port_priv
,
76 const struct ib_mad_hdr
*mad
);
77 static int ib_mad_post_receive_mads(struct ib_mad_qp_info
*qp_info
,
78 struct ib_mad_private
*mad
);
79 static void cancel_mads(struct ib_mad_agent_private
*mad_agent_priv
);
80 static void timeout_sends(struct work_struct
*work
);
81 static void local_completions(struct work_struct
*work
);
82 static int add_nonoui_reg_req(struct ib_mad_reg_req
*mad_reg_req
,
83 struct ib_mad_agent_private
*agent_priv
,
85 static int add_oui_reg_req(struct ib_mad_reg_req
*mad_reg_req
,
86 struct ib_mad_agent_private
*agent_priv
);
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
, "ib_register_mad_agent: Invalid port\n");
321 ret
= ERR_PTR(-ENODEV
);
325 /* Verify the QP requested is supported. For example, Ethernet devices
326 * will not have QP0 */
327 if (!port_priv
->qp_info
[qpn
].qp
) {
328 dev_notice(&device
->dev
,
329 "ib_register_mad_agent: QP %d not supported\n", qpn
);
330 ret
= ERR_PTR(-EPROTONOSUPPORT
);
334 /* Allocate structures */
335 mad_agent_priv
= kzalloc(sizeof *mad_agent_priv
, GFP_KERNEL
);
336 if (!mad_agent_priv
) {
337 ret
= ERR_PTR(-ENOMEM
);
341 mad_agent_priv
->agent
.mr
= ib_get_dma_mr(port_priv
->qp_info
[qpn
].qp
->pd
,
342 IB_ACCESS_LOCAL_WRITE
);
343 if (IS_ERR(mad_agent_priv
->agent
.mr
)) {
344 ret
= ERR_PTR(-ENOMEM
);
349 reg_req
= kmemdup(mad_reg_req
, sizeof *reg_req
, GFP_KERNEL
);
351 ret
= ERR_PTR(-ENOMEM
);
356 /* Now, fill in the various structures */
357 mad_agent_priv
->qp_info
= &port_priv
->qp_info
[qpn
];
358 mad_agent_priv
->reg_req
= reg_req
;
359 mad_agent_priv
->agent
.rmpp_version
= rmpp_version
;
360 mad_agent_priv
->agent
.device
= device
;
361 mad_agent_priv
->agent
.recv_handler
= recv_handler
;
362 mad_agent_priv
->agent
.send_handler
= send_handler
;
363 mad_agent_priv
->agent
.context
= context
;
364 mad_agent_priv
->agent
.qp
= port_priv
->qp_info
[qpn
].qp
;
365 mad_agent_priv
->agent
.port_num
= port_num
;
366 mad_agent_priv
->agent
.flags
= registration_flags
;
367 spin_lock_init(&mad_agent_priv
->lock
);
368 INIT_LIST_HEAD(&mad_agent_priv
->send_list
);
369 INIT_LIST_HEAD(&mad_agent_priv
->wait_list
);
370 INIT_LIST_HEAD(&mad_agent_priv
->done_list
);
371 INIT_LIST_HEAD(&mad_agent_priv
->rmpp_list
);
372 INIT_DELAYED_WORK(&mad_agent_priv
->timed_work
, timeout_sends
);
373 INIT_LIST_HEAD(&mad_agent_priv
->local_list
);
374 INIT_WORK(&mad_agent_priv
->local_work
, local_completions
);
375 atomic_set(&mad_agent_priv
->refcount
, 1);
376 init_completion(&mad_agent_priv
->comp
);
378 spin_lock_irqsave(&port_priv
->reg_lock
, flags
);
379 mad_agent_priv
->agent
.hi_tid
= ++ib_mad_client_id
;
382 * Make sure MAD registration (if supplied)
383 * is non overlapping with any existing ones
386 mgmt_class
= convert_mgmt_class(mad_reg_req
->mgmt_class
);
387 if (!is_vendor_class(mgmt_class
)) {
388 class = port_priv
->version
[mad_reg_req
->
389 mgmt_class_version
].class;
391 method
= class->method_table
[mgmt_class
];
393 if (method_in_use(&method
,
398 ret2
= add_nonoui_reg_req(mad_reg_req
, mad_agent_priv
,
401 /* "New" vendor class range */
402 vendor
= port_priv
->version
[mad_reg_req
->
403 mgmt_class_version
].vendor
;
405 vclass
= vendor_class_index(mgmt_class
);
406 vendor_class
= vendor
->vendor_class
[vclass
];
408 if (is_vendor_method_in_use(
414 ret2
= add_oui_reg_req(mad_reg_req
, mad_agent_priv
);
422 /* Add mad agent into port's agent list */
423 list_add_tail(&mad_agent_priv
->agent_list
, &port_priv
->agent_list
);
424 spin_unlock_irqrestore(&port_priv
->reg_lock
, flags
);
426 return &mad_agent_priv
->agent
;
429 spin_unlock_irqrestore(&port_priv
->reg_lock
, flags
);
432 ib_dereg_mr(mad_agent_priv
->agent
.mr
);
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
;
503 /* Validate parameters */
504 if ((is_snooping_sends(mad_snoop_flags
) && !snoop_handler
) ||
505 (is_snooping_recvs(mad_snoop_flags
) && !recv_handler
)) {
506 ret
= ERR_PTR(-EINVAL
);
509 qpn
= get_spl_qp_index(qp_type
);
511 ret
= ERR_PTR(-EINVAL
);
514 port_priv
= ib_get_mad_port(device
, port_num
);
516 ret
= ERR_PTR(-ENODEV
);
519 /* Allocate structures */
520 mad_snoop_priv
= kzalloc(sizeof *mad_snoop_priv
, GFP_KERNEL
);
521 if (!mad_snoop_priv
) {
522 ret
= ERR_PTR(-ENOMEM
);
526 /* Now, fill in the various structures */
527 mad_snoop_priv
->qp_info
= &port_priv
->qp_info
[qpn
];
528 mad_snoop_priv
->agent
.device
= device
;
529 mad_snoop_priv
->agent
.recv_handler
= recv_handler
;
530 mad_snoop_priv
->agent
.snoop_handler
= snoop_handler
;
531 mad_snoop_priv
->agent
.context
= context
;
532 mad_snoop_priv
->agent
.qp
= port_priv
->qp_info
[qpn
].qp
;
533 mad_snoop_priv
->agent
.port_num
= port_num
;
534 mad_snoop_priv
->mad_snoop_flags
= mad_snoop_flags
;
535 init_completion(&mad_snoop_priv
->comp
);
536 mad_snoop_priv
->snoop_index
= register_snoop_agent(
537 &port_priv
->qp_info
[qpn
],
539 if (mad_snoop_priv
->snoop_index
< 0) {
540 ret
= ERR_PTR(mad_snoop_priv
->snoop_index
);
544 atomic_set(&mad_snoop_priv
->refcount
, 1);
545 return &mad_snoop_priv
->agent
;
548 kfree(mad_snoop_priv
);
552 EXPORT_SYMBOL(ib_register_mad_snoop
);
554 static inline void deref_mad_agent(struct ib_mad_agent_private
*mad_agent_priv
)
556 if (atomic_dec_and_test(&mad_agent_priv
->refcount
))
557 complete(&mad_agent_priv
->comp
);
560 static inline void deref_snoop_agent(struct ib_mad_snoop_private
*mad_snoop_priv
)
562 if (atomic_dec_and_test(&mad_snoop_priv
->refcount
))
563 complete(&mad_snoop_priv
->comp
);
566 static void unregister_mad_agent(struct ib_mad_agent_private
*mad_agent_priv
)
568 struct ib_mad_port_private
*port_priv
;
571 /* Note that we could still be handling received MADs */
574 * Canceling all sends results in dropping received response
575 * MADs, preventing us from queuing additional work
577 cancel_mads(mad_agent_priv
);
578 port_priv
= mad_agent_priv
->qp_info
->port_priv
;
579 cancel_delayed_work(&mad_agent_priv
->timed_work
);
581 spin_lock_irqsave(&port_priv
->reg_lock
, flags
);
582 remove_mad_reg_req(mad_agent_priv
);
583 list_del(&mad_agent_priv
->agent_list
);
584 spin_unlock_irqrestore(&port_priv
->reg_lock
, flags
);
586 flush_workqueue(port_priv
->wq
);
587 ib_cancel_rmpp_recvs(mad_agent_priv
);
589 deref_mad_agent(mad_agent_priv
);
590 wait_for_completion(&mad_agent_priv
->comp
);
592 kfree(mad_agent_priv
->reg_req
);
593 ib_dereg_mr(mad_agent_priv
->agent
.mr
);
594 kfree(mad_agent_priv
);
597 static void unregister_mad_snoop(struct ib_mad_snoop_private
*mad_snoop_priv
)
599 struct ib_mad_qp_info
*qp_info
;
602 qp_info
= mad_snoop_priv
->qp_info
;
603 spin_lock_irqsave(&qp_info
->snoop_lock
, flags
);
604 qp_info
->snoop_table
[mad_snoop_priv
->snoop_index
] = NULL
;
605 atomic_dec(&qp_info
->snoop_count
);
606 spin_unlock_irqrestore(&qp_info
->snoop_lock
, flags
);
608 deref_snoop_agent(mad_snoop_priv
);
609 wait_for_completion(&mad_snoop_priv
->comp
);
611 kfree(mad_snoop_priv
);
615 * ib_unregister_mad_agent - Unregisters a client from using MAD services
617 int ib_unregister_mad_agent(struct ib_mad_agent
*mad_agent
)
619 struct ib_mad_agent_private
*mad_agent_priv
;
620 struct ib_mad_snoop_private
*mad_snoop_priv
;
622 /* If the TID is zero, the agent can only snoop. */
623 if (mad_agent
->hi_tid
) {
624 mad_agent_priv
= container_of(mad_agent
,
625 struct ib_mad_agent_private
,
627 unregister_mad_agent(mad_agent_priv
);
629 mad_snoop_priv
= container_of(mad_agent
,
630 struct ib_mad_snoop_private
,
632 unregister_mad_snoop(mad_snoop_priv
);
636 EXPORT_SYMBOL(ib_unregister_mad_agent
);
638 static void dequeue_mad(struct ib_mad_list_head
*mad_list
)
640 struct ib_mad_queue
*mad_queue
;
643 BUG_ON(!mad_list
->mad_queue
);
644 mad_queue
= mad_list
->mad_queue
;
645 spin_lock_irqsave(&mad_queue
->lock
, flags
);
646 list_del(&mad_list
->list
);
648 spin_unlock_irqrestore(&mad_queue
->lock
, flags
);
651 static void snoop_send(struct ib_mad_qp_info
*qp_info
,
652 struct ib_mad_send_buf
*send_buf
,
653 struct ib_mad_send_wc
*mad_send_wc
,
656 struct ib_mad_snoop_private
*mad_snoop_priv
;
660 spin_lock_irqsave(&qp_info
->snoop_lock
, flags
);
661 for (i
= 0; i
< qp_info
->snoop_table_size
; i
++) {
662 mad_snoop_priv
= qp_info
->snoop_table
[i
];
663 if (!mad_snoop_priv
||
664 !(mad_snoop_priv
->mad_snoop_flags
& mad_snoop_flags
))
667 atomic_inc(&mad_snoop_priv
->refcount
);
668 spin_unlock_irqrestore(&qp_info
->snoop_lock
, flags
);
669 mad_snoop_priv
->agent
.snoop_handler(&mad_snoop_priv
->agent
,
670 send_buf
, mad_send_wc
);
671 deref_snoop_agent(mad_snoop_priv
);
672 spin_lock_irqsave(&qp_info
->snoop_lock
, flags
);
674 spin_unlock_irqrestore(&qp_info
->snoop_lock
, flags
);
677 static void snoop_recv(struct ib_mad_qp_info
*qp_info
,
678 struct ib_mad_recv_wc
*mad_recv_wc
,
681 struct ib_mad_snoop_private
*mad_snoop_priv
;
685 spin_lock_irqsave(&qp_info
->snoop_lock
, flags
);
686 for (i
= 0; i
< qp_info
->snoop_table_size
; i
++) {
687 mad_snoop_priv
= qp_info
->snoop_table
[i
];
688 if (!mad_snoop_priv
||
689 !(mad_snoop_priv
->mad_snoop_flags
& mad_snoop_flags
))
692 atomic_inc(&mad_snoop_priv
->refcount
);
693 spin_unlock_irqrestore(&qp_info
->snoop_lock
, flags
);
694 mad_snoop_priv
->agent
.recv_handler(&mad_snoop_priv
->agent
,
696 deref_snoop_agent(mad_snoop_priv
);
697 spin_lock_irqsave(&qp_info
->snoop_lock
, flags
);
699 spin_unlock_irqrestore(&qp_info
->snoop_lock
, flags
);
702 static void build_smp_wc(struct ib_qp
*qp
,
703 u64 wr_id
, u16 slid
, u16 pkey_index
, u8 port_num
,
706 memset(wc
, 0, sizeof *wc
);
708 wc
->status
= IB_WC_SUCCESS
;
709 wc
->opcode
= IB_WC_RECV
;
710 wc
->pkey_index
= pkey_index
;
711 wc
->byte_len
= sizeof(struct ib_mad
) + sizeof(struct ib_grh
);
716 wc
->dlid_path_bits
= 0;
717 wc
->port_num
= port_num
;
720 static size_t mad_priv_size(const struct ib_mad_private
*mp
)
722 return sizeof(struct ib_mad_private
) + mp
->mad_size
;
725 static struct ib_mad_private
*alloc_mad_private(size_t mad_size
, gfp_t flags
)
727 size_t size
= sizeof(struct ib_mad_private
) + mad_size
;
728 struct ib_mad_private
*ret
= kzalloc(size
, flags
);
731 ret
->mad_size
= mad_size
;
736 static size_t port_mad_size(const struct ib_mad_port_private
*port_priv
)
738 return rdma_max_mad_size(port_priv
->device
, port_priv
->port_num
);
741 static size_t mad_priv_dma_size(const struct ib_mad_private
*mp
)
743 return sizeof(struct ib_grh
) + mp
->mad_size
;
747 * Return 0 if SMP is to be sent
748 * Return 1 if SMP was consumed locally (whether or not solicited)
749 * Return < 0 if error
751 static int handle_outgoing_dr_smp(struct ib_mad_agent_private
*mad_agent_priv
,
752 struct ib_mad_send_wr_private
*mad_send_wr
)
755 struct ib_smp
*smp
= mad_send_wr
->send_buf
.mad
;
756 struct opa_smp
*opa_smp
= (struct opa_smp
*)smp
;
758 struct ib_mad_local_private
*local
;
759 struct ib_mad_private
*mad_priv
;
760 struct ib_mad_port_private
*port_priv
;
761 struct ib_mad_agent_private
*recv_mad_agent
= NULL
;
762 struct ib_device
*device
= mad_agent_priv
->agent
.device
;
765 struct ib_send_wr
*send_wr
= &mad_send_wr
->send_wr
;
766 size_t mad_size
= port_mad_size(mad_agent_priv
->qp_info
->port_priv
);
767 u16 out_mad_pkey_index
= 0;
769 bool opa
= rdma_cap_opa_mad(mad_agent_priv
->qp_info
->port_priv
->device
,
770 mad_agent_priv
->qp_info
->port_priv
->port_num
);
772 if (rdma_cap_ib_switch(device
) &&
773 smp
->mgmt_class
== IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
)
774 port_num
= send_wr
->wr
.ud
.port_num
;
776 port_num
= mad_agent_priv
->agent
.port_num
;
779 * Directed route handling starts if the initial LID routed part of
780 * a request or the ending LID routed part of a response is empty.
781 * If we are at the start of the LID routed part, don't update the
782 * hop_ptr or hop_cnt. See section 14.2.2, Vol 1 IB spec.
784 if (opa
&& smp
->class_version
== OPA_SMP_CLASS_VERSION
) {
787 if ((opa_get_smp_direction(opa_smp
)
788 ? opa_smp
->route
.dr
.dr_dlid
: opa_smp
->route
.dr
.dr_slid
) ==
789 OPA_LID_PERMISSIVE
&&
790 opa_smi_handle_dr_smp_send(opa_smp
,
791 rdma_cap_ib_switch(device
),
792 port_num
) == IB_SMI_DISCARD
) {
794 dev_err(&device
->dev
, "OPA Invalid directed route\n");
797 opa_drslid
= be32_to_cpu(opa_smp
->route
.dr
.dr_slid
);
798 if (opa_drslid
!= be32_to_cpu(OPA_LID_PERMISSIVE
) &&
799 opa_drslid
& 0xffff0000) {
801 dev_err(&device
->dev
, "OPA Invalid dr_slid 0x%x\n",
805 drslid
= (u16
)(opa_drslid
& 0x0000ffff);
807 /* Check to post send on QP or process locally */
808 if (opa_smi_check_local_smp(opa_smp
, device
) == IB_SMI_DISCARD
&&
809 opa_smi_check_local_returning_smp(opa_smp
, device
) == IB_SMI_DISCARD
)
812 if ((ib_get_smp_direction(smp
) ? smp
->dr_dlid
: smp
->dr_slid
) ==
814 smi_handle_dr_smp_send(smp
, rdma_cap_ib_switch(device
), port_num
) ==
817 dev_err(&device
->dev
, "Invalid directed route\n");
820 drslid
= be16_to_cpu(smp
->dr_slid
);
822 /* Check to post send on QP or process locally */
823 if (smi_check_local_smp(smp
, device
) == IB_SMI_DISCARD
&&
824 smi_check_local_returning_smp(smp
, device
) == IB_SMI_DISCARD
)
828 local
= kmalloc(sizeof *local
, GFP_ATOMIC
);
831 dev_err(&device
->dev
, "No memory for ib_mad_local_private\n");
834 local
->mad_priv
= NULL
;
835 local
->recv_mad_agent
= NULL
;
836 mad_priv
= alloc_mad_private(mad_size
, GFP_ATOMIC
);
839 dev_err(&device
->dev
, "No memory for local response MAD\n");
844 build_smp_wc(mad_agent_priv
->agent
.qp
,
845 send_wr
->wr_id
, drslid
,
846 send_wr
->wr
.ud
.pkey_index
,
847 send_wr
->wr
.ud
.port_num
, &mad_wc
);
849 if (opa
&& smp
->base_version
== OPA_MGMT_BASE_VERSION
) {
850 mad_wc
.byte_len
= mad_send_wr
->send_buf
.hdr_len
851 + mad_send_wr
->send_buf
.data_len
852 + sizeof(struct ib_grh
);
855 /* No GRH for DR SMP */
856 ret
= device
->process_mad(device
, 0, port_num
, &mad_wc
, NULL
,
857 (const struct ib_mad_hdr
*)smp
, mad_size
,
858 (struct ib_mad_hdr
*)mad_priv
->mad
,
859 &mad_size
, &out_mad_pkey_index
);
862 case IB_MAD_RESULT_SUCCESS
| IB_MAD_RESULT_REPLY
:
863 if (ib_response_mad((const struct ib_mad_hdr
*)mad_priv
->mad
) &&
864 mad_agent_priv
->agent
.recv_handler
) {
865 local
->mad_priv
= mad_priv
;
866 local
->recv_mad_agent
= mad_agent_priv
;
868 * Reference MAD agent until receive
869 * side of local completion handled
871 atomic_inc(&mad_agent_priv
->refcount
);
875 case IB_MAD_RESULT_SUCCESS
| IB_MAD_RESULT_CONSUMED
:
878 case IB_MAD_RESULT_SUCCESS
:
879 /* Treat like an incoming receive MAD */
880 port_priv
= ib_get_mad_port(mad_agent_priv
->agent
.device
,
881 mad_agent_priv
->agent
.port_num
);
883 memcpy(mad_priv
->mad
, smp
, mad_priv
->mad_size
);
884 recv_mad_agent
= find_mad_agent(port_priv
,
885 (const struct ib_mad_hdr
*)mad_priv
->mad
);
887 if (!port_priv
|| !recv_mad_agent
) {
889 * No receiving agent so drop packet and
890 * generate send completion.
895 local
->mad_priv
= mad_priv
;
896 local
->recv_mad_agent
= recv_mad_agent
;
905 local
->mad_send_wr
= mad_send_wr
;
907 local
->mad_send_wr
->send_wr
.wr
.ud
.pkey_index
= out_mad_pkey_index
;
908 local
->return_wc_byte_len
= mad_size
;
910 /* Reference MAD agent until send side of local completion handled */
911 atomic_inc(&mad_agent_priv
->refcount
);
912 /* Queue local completion to local list */
913 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
914 list_add_tail(&local
->completion_list
, &mad_agent_priv
->local_list
);
915 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
916 queue_work(mad_agent_priv
->qp_info
->port_priv
->wq
,
917 &mad_agent_priv
->local_work
);
923 static int get_pad_size(int hdr_len
, int data_len
, size_t mad_size
)
927 seg_size
= mad_size
- hdr_len
;
928 if (data_len
&& seg_size
) {
929 pad
= seg_size
- data_len
% seg_size
;
930 return pad
== seg_size
? 0 : pad
;
935 static void free_send_rmpp_list(struct ib_mad_send_wr_private
*mad_send_wr
)
937 struct ib_rmpp_segment
*s
, *t
;
939 list_for_each_entry_safe(s
, t
, &mad_send_wr
->rmpp_list
, list
) {
945 static int alloc_send_rmpp_list(struct ib_mad_send_wr_private
*send_wr
,
946 size_t mad_size
, gfp_t gfp_mask
)
948 struct ib_mad_send_buf
*send_buf
= &send_wr
->send_buf
;
949 struct ib_rmpp_mad
*rmpp_mad
= send_buf
->mad
;
950 struct ib_rmpp_segment
*seg
= NULL
;
951 int left
, seg_size
, pad
;
953 send_buf
->seg_size
= mad_size
- send_buf
->hdr_len
;
954 send_buf
->seg_rmpp_size
= mad_size
- IB_MGMT_RMPP_HDR
;
955 seg_size
= send_buf
->seg_size
;
958 /* Allocate data segments. */
959 for (left
= send_buf
->data_len
+ pad
; left
> 0; left
-= seg_size
) {
960 seg
= kmalloc(sizeof (*seg
) + seg_size
, gfp_mask
);
962 dev_err(&send_buf
->mad_agent
->device
->dev
,
963 "alloc_send_rmpp_segs: RMPP mem alloc failed for len %zd, gfp %#x\n",
964 sizeof (*seg
) + seg_size
, gfp_mask
);
965 free_send_rmpp_list(send_wr
);
968 seg
->num
= ++send_buf
->seg_count
;
969 list_add_tail(&seg
->list
, &send_wr
->rmpp_list
);
972 /* Zero any padding */
974 memset(seg
->data
+ seg_size
- pad
, 0, pad
);
976 rmpp_mad
->rmpp_hdr
.rmpp_version
= send_wr
->mad_agent_priv
->
978 rmpp_mad
->rmpp_hdr
.rmpp_type
= IB_MGMT_RMPP_TYPE_DATA
;
979 ib_set_rmpp_flags(&rmpp_mad
->rmpp_hdr
, IB_MGMT_RMPP_FLAG_ACTIVE
);
981 send_wr
->cur_seg
= container_of(send_wr
->rmpp_list
.next
,
982 struct ib_rmpp_segment
, list
);
983 send_wr
->last_ack_seg
= send_wr
->cur_seg
;
987 int ib_mad_kernel_rmpp_agent(const struct ib_mad_agent
*agent
)
989 return agent
->rmpp_version
&& !(agent
->flags
& IB_MAD_USER_RMPP
);
991 EXPORT_SYMBOL(ib_mad_kernel_rmpp_agent
);
993 struct ib_mad_send_buf
* ib_create_send_mad(struct ib_mad_agent
*mad_agent
,
994 u32 remote_qpn
, u16 pkey_index
,
996 int hdr_len
, int data_len
,
1000 struct ib_mad_agent_private
*mad_agent_priv
;
1001 struct ib_mad_send_wr_private
*mad_send_wr
;
1002 int pad
, message_size
, ret
, size
;
1007 mad_agent_priv
= container_of(mad_agent
, struct ib_mad_agent_private
,
1010 opa
= rdma_cap_opa_mad(mad_agent
->device
, mad_agent
->port_num
);
1012 if (opa
&& base_version
== OPA_MGMT_BASE_VERSION
)
1013 mad_size
= sizeof(struct opa_mad
);
1015 mad_size
= sizeof(struct ib_mad
);
1017 pad
= get_pad_size(hdr_len
, data_len
, mad_size
);
1018 message_size
= hdr_len
+ data_len
+ pad
;
1020 if (ib_mad_kernel_rmpp_agent(mad_agent
)) {
1021 if (!rmpp_active
&& message_size
> mad_size
)
1022 return ERR_PTR(-EINVAL
);
1024 if (rmpp_active
|| message_size
> mad_size
)
1025 return ERR_PTR(-EINVAL
);
1027 size
= rmpp_active
? hdr_len
: mad_size
;
1028 buf
= kzalloc(sizeof *mad_send_wr
+ size
, gfp_mask
);
1030 return ERR_PTR(-ENOMEM
);
1032 mad_send_wr
= buf
+ size
;
1033 INIT_LIST_HEAD(&mad_send_wr
->rmpp_list
);
1034 mad_send_wr
->send_buf
.mad
= buf
;
1035 mad_send_wr
->send_buf
.hdr_len
= hdr_len
;
1036 mad_send_wr
->send_buf
.data_len
= data_len
;
1037 mad_send_wr
->pad
= pad
;
1039 mad_send_wr
->mad_agent_priv
= mad_agent_priv
;
1040 mad_send_wr
->sg_list
[0].length
= hdr_len
;
1041 mad_send_wr
->sg_list
[0].lkey
= mad_agent
->mr
->lkey
;
1043 /* OPA MADs don't have to be the full 2048 bytes */
1044 if (opa
&& base_version
== OPA_MGMT_BASE_VERSION
&&
1045 data_len
< mad_size
- hdr_len
)
1046 mad_send_wr
->sg_list
[1].length
= data_len
;
1048 mad_send_wr
->sg_list
[1].length
= mad_size
- hdr_len
;
1050 mad_send_wr
->sg_list
[1].lkey
= mad_agent
->mr
->lkey
;
1052 mad_send_wr
->send_wr
.wr_id
= (unsigned long) mad_send_wr
;
1053 mad_send_wr
->send_wr
.sg_list
= mad_send_wr
->sg_list
;
1054 mad_send_wr
->send_wr
.num_sge
= 2;
1055 mad_send_wr
->send_wr
.opcode
= IB_WR_SEND
;
1056 mad_send_wr
->send_wr
.send_flags
= IB_SEND_SIGNALED
;
1057 mad_send_wr
->send_wr
.wr
.ud
.remote_qpn
= remote_qpn
;
1058 mad_send_wr
->send_wr
.wr
.ud
.remote_qkey
= IB_QP_SET_QKEY
;
1059 mad_send_wr
->send_wr
.wr
.ud
.pkey_index
= pkey_index
;
1062 ret
= alloc_send_rmpp_list(mad_send_wr
, mad_size
, gfp_mask
);
1065 return ERR_PTR(ret
);
1069 mad_send_wr
->send_buf
.mad_agent
= mad_agent
;
1070 atomic_inc(&mad_agent_priv
->refcount
);
1071 return &mad_send_wr
->send_buf
;
1073 EXPORT_SYMBOL(ib_create_send_mad
);
1075 int ib_get_mad_data_offset(u8 mgmt_class
)
1077 if (mgmt_class
== IB_MGMT_CLASS_SUBN_ADM
)
1078 return IB_MGMT_SA_HDR
;
1079 else if ((mgmt_class
== IB_MGMT_CLASS_DEVICE_MGMT
) ||
1080 (mgmt_class
== IB_MGMT_CLASS_DEVICE_ADM
) ||
1081 (mgmt_class
== IB_MGMT_CLASS_BIS
))
1082 return IB_MGMT_DEVICE_HDR
;
1083 else if ((mgmt_class
>= IB_MGMT_CLASS_VENDOR_RANGE2_START
) &&
1084 (mgmt_class
<= IB_MGMT_CLASS_VENDOR_RANGE2_END
))
1085 return IB_MGMT_VENDOR_HDR
;
1087 return IB_MGMT_MAD_HDR
;
1089 EXPORT_SYMBOL(ib_get_mad_data_offset
);
1091 int ib_is_mad_class_rmpp(u8 mgmt_class
)
1093 if ((mgmt_class
== IB_MGMT_CLASS_SUBN_ADM
) ||
1094 (mgmt_class
== IB_MGMT_CLASS_DEVICE_MGMT
) ||
1095 (mgmt_class
== IB_MGMT_CLASS_DEVICE_ADM
) ||
1096 (mgmt_class
== IB_MGMT_CLASS_BIS
) ||
1097 ((mgmt_class
>= IB_MGMT_CLASS_VENDOR_RANGE2_START
) &&
1098 (mgmt_class
<= IB_MGMT_CLASS_VENDOR_RANGE2_END
)))
1102 EXPORT_SYMBOL(ib_is_mad_class_rmpp
);
1104 void *ib_get_rmpp_segment(struct ib_mad_send_buf
*send_buf
, int seg_num
)
1106 struct ib_mad_send_wr_private
*mad_send_wr
;
1107 struct list_head
*list
;
1109 mad_send_wr
= container_of(send_buf
, struct ib_mad_send_wr_private
,
1111 list
= &mad_send_wr
->cur_seg
->list
;
1113 if (mad_send_wr
->cur_seg
->num
< seg_num
) {
1114 list_for_each_entry(mad_send_wr
->cur_seg
, list
, list
)
1115 if (mad_send_wr
->cur_seg
->num
== seg_num
)
1117 } else if (mad_send_wr
->cur_seg
->num
> seg_num
) {
1118 list_for_each_entry_reverse(mad_send_wr
->cur_seg
, list
, list
)
1119 if (mad_send_wr
->cur_seg
->num
== seg_num
)
1122 return mad_send_wr
->cur_seg
->data
;
1124 EXPORT_SYMBOL(ib_get_rmpp_segment
);
1126 static inline void *ib_get_payload(struct ib_mad_send_wr_private
*mad_send_wr
)
1128 if (mad_send_wr
->send_buf
.seg_count
)
1129 return ib_get_rmpp_segment(&mad_send_wr
->send_buf
,
1130 mad_send_wr
->seg_num
);
1132 return mad_send_wr
->send_buf
.mad
+
1133 mad_send_wr
->send_buf
.hdr_len
;
1136 void ib_free_send_mad(struct ib_mad_send_buf
*send_buf
)
1138 struct ib_mad_agent_private
*mad_agent_priv
;
1139 struct ib_mad_send_wr_private
*mad_send_wr
;
1141 mad_agent_priv
= container_of(send_buf
->mad_agent
,
1142 struct ib_mad_agent_private
, agent
);
1143 mad_send_wr
= container_of(send_buf
, struct ib_mad_send_wr_private
,
1146 free_send_rmpp_list(mad_send_wr
);
1147 kfree(send_buf
->mad
);
1148 deref_mad_agent(mad_agent_priv
);
1150 EXPORT_SYMBOL(ib_free_send_mad
);
1152 int ib_send_mad(struct ib_mad_send_wr_private
*mad_send_wr
)
1154 struct ib_mad_qp_info
*qp_info
;
1155 struct list_head
*list
;
1156 struct ib_send_wr
*bad_send_wr
;
1157 struct ib_mad_agent
*mad_agent
;
1159 unsigned long flags
;
1162 /* Set WR ID to find mad_send_wr upon completion */
1163 qp_info
= mad_send_wr
->mad_agent_priv
->qp_info
;
1164 mad_send_wr
->send_wr
.wr_id
= (unsigned long)&mad_send_wr
->mad_list
;
1165 mad_send_wr
->mad_list
.mad_queue
= &qp_info
->send_queue
;
1167 mad_agent
= mad_send_wr
->send_buf
.mad_agent
;
1168 sge
= mad_send_wr
->sg_list
;
1169 sge
[0].addr
= ib_dma_map_single(mad_agent
->device
,
1170 mad_send_wr
->send_buf
.mad
,
1173 if (unlikely(ib_dma_mapping_error(mad_agent
->device
, sge
[0].addr
)))
1176 mad_send_wr
->header_mapping
= sge
[0].addr
;
1178 sge
[1].addr
= ib_dma_map_single(mad_agent
->device
,
1179 ib_get_payload(mad_send_wr
),
1182 if (unlikely(ib_dma_mapping_error(mad_agent
->device
, sge
[1].addr
))) {
1183 ib_dma_unmap_single(mad_agent
->device
,
1184 mad_send_wr
->header_mapping
,
1185 sge
[0].length
, DMA_TO_DEVICE
);
1188 mad_send_wr
->payload_mapping
= sge
[1].addr
;
1190 spin_lock_irqsave(&qp_info
->send_queue
.lock
, flags
);
1191 if (qp_info
->send_queue
.count
< qp_info
->send_queue
.max_active
) {
1192 ret
= ib_post_send(mad_agent
->qp
, &mad_send_wr
->send_wr
,
1194 list
= &qp_info
->send_queue
.list
;
1197 list
= &qp_info
->overflow_list
;
1201 qp_info
->send_queue
.count
++;
1202 list_add_tail(&mad_send_wr
->mad_list
.list
, list
);
1204 spin_unlock_irqrestore(&qp_info
->send_queue
.lock
, flags
);
1206 ib_dma_unmap_single(mad_agent
->device
,
1207 mad_send_wr
->header_mapping
,
1208 sge
[0].length
, DMA_TO_DEVICE
);
1209 ib_dma_unmap_single(mad_agent
->device
,
1210 mad_send_wr
->payload_mapping
,
1211 sge
[1].length
, DMA_TO_DEVICE
);
1217 * ib_post_send_mad - Posts MAD(s) to the send queue of the QP associated
1218 * with the registered client
1220 int ib_post_send_mad(struct ib_mad_send_buf
*send_buf
,
1221 struct ib_mad_send_buf
**bad_send_buf
)
1223 struct ib_mad_agent_private
*mad_agent_priv
;
1224 struct ib_mad_send_buf
*next_send_buf
;
1225 struct ib_mad_send_wr_private
*mad_send_wr
;
1226 unsigned long flags
;
1229 /* Walk list of send WRs and post each on send list */
1230 for (; send_buf
; send_buf
= next_send_buf
) {
1232 mad_send_wr
= container_of(send_buf
,
1233 struct ib_mad_send_wr_private
,
1235 mad_agent_priv
= mad_send_wr
->mad_agent_priv
;
1237 if (!send_buf
->mad_agent
->send_handler
||
1238 (send_buf
->timeout_ms
&&
1239 !send_buf
->mad_agent
->recv_handler
)) {
1244 if (!ib_is_mad_class_rmpp(((struct ib_mad_hdr
*) send_buf
->mad
)->mgmt_class
)) {
1245 if (mad_agent_priv
->agent
.rmpp_version
) {
1252 * Save pointer to next work request to post in case the
1253 * current one completes, and the user modifies the work
1254 * request associated with the completion
1256 next_send_buf
= send_buf
->next
;
1257 mad_send_wr
->send_wr
.wr
.ud
.ah
= send_buf
->ah
;
1259 if (((struct ib_mad_hdr
*) send_buf
->mad
)->mgmt_class
==
1260 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
) {
1261 ret
= handle_outgoing_dr_smp(mad_agent_priv
,
1263 if (ret
< 0) /* error */
1265 else if (ret
== 1) /* locally consumed */
1269 mad_send_wr
->tid
= ((struct ib_mad_hdr
*) send_buf
->mad
)->tid
;
1270 /* Timeout will be updated after send completes */
1271 mad_send_wr
->timeout
= msecs_to_jiffies(send_buf
->timeout_ms
);
1272 mad_send_wr
->max_retries
= send_buf
->retries
;
1273 mad_send_wr
->retries_left
= send_buf
->retries
;
1274 send_buf
->retries
= 0;
1275 /* Reference for work request to QP + response */
1276 mad_send_wr
->refcount
= 1 + (mad_send_wr
->timeout
> 0);
1277 mad_send_wr
->status
= IB_WC_SUCCESS
;
1279 /* Reference MAD agent until send completes */
1280 atomic_inc(&mad_agent_priv
->refcount
);
1281 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
1282 list_add_tail(&mad_send_wr
->agent_list
,
1283 &mad_agent_priv
->send_list
);
1284 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
1286 if (ib_mad_kernel_rmpp_agent(&mad_agent_priv
->agent
)) {
1287 ret
= ib_send_rmpp_mad(mad_send_wr
);
1288 if (ret
>= 0 && ret
!= IB_RMPP_RESULT_CONSUMED
)
1289 ret
= ib_send_mad(mad_send_wr
);
1291 ret
= ib_send_mad(mad_send_wr
);
1293 /* Fail send request */
1294 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
1295 list_del(&mad_send_wr
->agent_list
);
1296 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
1297 atomic_dec(&mad_agent_priv
->refcount
);
1304 *bad_send_buf
= send_buf
;
1307 EXPORT_SYMBOL(ib_post_send_mad
);
1310 * ib_free_recv_mad - Returns data buffers used to receive
1311 * a MAD to the access layer
1313 void ib_free_recv_mad(struct ib_mad_recv_wc
*mad_recv_wc
)
1315 struct ib_mad_recv_buf
*mad_recv_buf
, *temp_recv_buf
;
1316 struct ib_mad_private_header
*mad_priv_hdr
;
1317 struct ib_mad_private
*priv
;
1318 struct list_head free_list
;
1320 INIT_LIST_HEAD(&free_list
);
1321 list_splice_init(&mad_recv_wc
->rmpp_list
, &free_list
);
1323 list_for_each_entry_safe(mad_recv_buf
, temp_recv_buf
,
1325 mad_recv_wc
= container_of(mad_recv_buf
, struct ib_mad_recv_wc
,
1327 mad_priv_hdr
= container_of(mad_recv_wc
,
1328 struct ib_mad_private_header
,
1330 priv
= container_of(mad_priv_hdr
, struct ib_mad_private
,
1335 EXPORT_SYMBOL(ib_free_recv_mad
);
1337 struct ib_mad_agent
*ib_redirect_mad_qp(struct ib_qp
*qp
,
1339 ib_mad_send_handler send_handler
,
1340 ib_mad_recv_handler recv_handler
,
1343 return ERR_PTR(-EINVAL
); /* XXX: for now */
1345 EXPORT_SYMBOL(ib_redirect_mad_qp
);
1347 int ib_process_mad_wc(struct ib_mad_agent
*mad_agent
,
1350 dev_err(&mad_agent
->device
->dev
,
1351 "ib_process_mad_wc() not implemented yet\n");
1354 EXPORT_SYMBOL(ib_process_mad_wc
);
1356 static int method_in_use(struct ib_mad_mgmt_method_table
**method
,
1357 struct ib_mad_reg_req
*mad_reg_req
)
1361 for_each_set_bit(i
, mad_reg_req
->method_mask
, IB_MGMT_MAX_METHODS
) {
1362 if ((*method
)->agent
[i
]) {
1363 pr_err("Method %d already in use\n", i
);
1370 static int allocate_method_table(struct ib_mad_mgmt_method_table
**method
)
1372 /* Allocate management method table */
1373 *method
= kzalloc(sizeof **method
, GFP_ATOMIC
);
1375 pr_err("No memory for ib_mad_mgmt_method_table\n");
1383 * Check to see if there are any methods still in use
1385 static int check_method_table(struct ib_mad_mgmt_method_table
*method
)
1389 for (i
= 0; i
< IB_MGMT_MAX_METHODS
; i
++)
1390 if (method
->agent
[i
])
1396 * Check to see if there are any method tables for this class still in use
1398 static int check_class_table(struct ib_mad_mgmt_class_table
*class)
1402 for (i
= 0; i
< MAX_MGMT_CLASS
; i
++)
1403 if (class->method_table
[i
])
1408 static int check_vendor_class(struct ib_mad_mgmt_vendor_class
*vendor_class
)
1412 for (i
= 0; i
< MAX_MGMT_OUI
; i
++)
1413 if (vendor_class
->method_table
[i
])
1418 static int find_vendor_oui(struct ib_mad_mgmt_vendor_class
*vendor_class
,
1423 for (i
= 0; i
< MAX_MGMT_OUI
; i
++)
1424 /* Is there matching OUI for this vendor class ? */
1425 if (!memcmp(vendor_class
->oui
[i
], oui
, 3))
1431 static int check_vendor_table(struct ib_mad_mgmt_vendor_class_table
*vendor
)
1435 for (i
= 0; i
< MAX_MGMT_VENDOR_RANGE2
; i
++)
1436 if (vendor
->vendor_class
[i
])
1442 static void remove_methods_mad_agent(struct ib_mad_mgmt_method_table
*method
,
1443 struct ib_mad_agent_private
*agent
)
1447 /* Remove any methods for this mad agent */
1448 for (i
= 0; i
< IB_MGMT_MAX_METHODS
; i
++) {
1449 if (method
->agent
[i
] == agent
) {
1450 method
->agent
[i
] = NULL
;
1455 static int add_nonoui_reg_req(struct ib_mad_reg_req
*mad_reg_req
,
1456 struct ib_mad_agent_private
*agent_priv
,
1459 struct ib_mad_port_private
*port_priv
;
1460 struct ib_mad_mgmt_class_table
**class;
1461 struct ib_mad_mgmt_method_table
**method
;
1464 port_priv
= agent_priv
->qp_info
->port_priv
;
1465 class = &port_priv
->version
[mad_reg_req
->mgmt_class_version
].class;
1467 /* Allocate management class table for "new" class version */
1468 *class = kzalloc(sizeof **class, GFP_ATOMIC
);
1470 dev_err(&agent_priv
->agent
.device
->dev
,
1471 "No memory for ib_mad_mgmt_class_table\n");
1476 /* Allocate method table for this management class */
1477 method
= &(*class)->method_table
[mgmt_class
];
1478 if ((ret
= allocate_method_table(method
)))
1481 method
= &(*class)->method_table
[mgmt_class
];
1483 /* Allocate method table for this management class */
1484 if ((ret
= allocate_method_table(method
)))
1489 /* Now, make sure methods are not already in use */
1490 if (method_in_use(method
, mad_reg_req
))
1493 /* Finally, add in methods being registered */
1494 for_each_set_bit(i
, mad_reg_req
->method_mask
, IB_MGMT_MAX_METHODS
)
1495 (*method
)->agent
[i
] = agent_priv
;
1500 /* Remove any methods for this mad agent */
1501 remove_methods_mad_agent(*method
, agent_priv
);
1502 /* Now, check to see if there are any methods in use */
1503 if (!check_method_table(*method
)) {
1504 /* If not, release management method table */
1517 static int add_oui_reg_req(struct ib_mad_reg_req
*mad_reg_req
,
1518 struct ib_mad_agent_private
*agent_priv
)
1520 struct ib_mad_port_private
*port_priv
;
1521 struct ib_mad_mgmt_vendor_class_table
**vendor_table
;
1522 struct ib_mad_mgmt_vendor_class_table
*vendor
= NULL
;
1523 struct ib_mad_mgmt_vendor_class
*vendor_class
= NULL
;
1524 struct ib_mad_mgmt_method_table
**method
;
1525 int i
, ret
= -ENOMEM
;
1528 /* "New" vendor (with OUI) class */
1529 vclass
= vendor_class_index(mad_reg_req
->mgmt_class
);
1530 port_priv
= agent_priv
->qp_info
->port_priv
;
1531 vendor_table
= &port_priv
->version
[
1532 mad_reg_req
->mgmt_class_version
].vendor
;
1533 if (!*vendor_table
) {
1534 /* Allocate mgmt vendor class table for "new" class version */
1535 vendor
= kzalloc(sizeof *vendor
, GFP_ATOMIC
);
1537 dev_err(&agent_priv
->agent
.device
->dev
,
1538 "No memory for ib_mad_mgmt_vendor_class_table\n");
1542 *vendor_table
= vendor
;
1544 if (!(*vendor_table
)->vendor_class
[vclass
]) {
1545 /* Allocate table for this management vendor class */
1546 vendor_class
= kzalloc(sizeof *vendor_class
, GFP_ATOMIC
);
1547 if (!vendor_class
) {
1548 dev_err(&agent_priv
->agent
.device
->dev
,
1549 "No memory for ib_mad_mgmt_vendor_class\n");
1553 (*vendor_table
)->vendor_class
[vclass
] = vendor_class
;
1555 for (i
= 0; i
< MAX_MGMT_OUI
; i
++) {
1556 /* Is there matching OUI for this vendor class ? */
1557 if (!memcmp((*vendor_table
)->vendor_class
[vclass
]->oui
[i
],
1558 mad_reg_req
->oui
, 3)) {
1559 method
= &(*vendor_table
)->vendor_class
[
1560 vclass
]->method_table
[i
];
1565 for (i
= 0; i
< MAX_MGMT_OUI
; i
++) {
1566 /* OUI slot available ? */
1567 if (!is_vendor_oui((*vendor_table
)->vendor_class
[
1569 method
= &(*vendor_table
)->vendor_class
[
1570 vclass
]->method_table
[i
];
1572 /* Allocate method table for this OUI */
1573 if ((ret
= allocate_method_table(method
)))
1575 memcpy((*vendor_table
)->vendor_class
[vclass
]->oui
[i
],
1576 mad_reg_req
->oui
, 3);
1580 dev_err(&agent_priv
->agent
.device
->dev
, "All OUI slots in use\n");
1584 /* Now, make sure methods are not already in use */
1585 if (method_in_use(method
, mad_reg_req
))
1588 /* Finally, add in methods being registered */
1589 for_each_set_bit(i
, mad_reg_req
->method_mask
, IB_MGMT_MAX_METHODS
)
1590 (*method
)->agent
[i
] = agent_priv
;
1595 /* Remove any methods for this mad agent */
1596 remove_methods_mad_agent(*method
, agent_priv
);
1597 /* Now, check to see if there are any methods in use */
1598 if (!check_method_table(*method
)) {
1599 /* If not, release management method table */
1606 (*vendor_table
)->vendor_class
[vclass
] = NULL
;
1607 kfree(vendor_class
);
1611 *vendor_table
= NULL
;
1618 static void remove_mad_reg_req(struct ib_mad_agent_private
*agent_priv
)
1620 struct ib_mad_port_private
*port_priv
;
1621 struct ib_mad_mgmt_class_table
*class;
1622 struct ib_mad_mgmt_method_table
*method
;
1623 struct ib_mad_mgmt_vendor_class_table
*vendor
;
1624 struct ib_mad_mgmt_vendor_class
*vendor_class
;
1629 * Was MAD registration request supplied
1630 * with original registration ?
1632 if (!agent_priv
->reg_req
) {
1636 port_priv
= agent_priv
->qp_info
->port_priv
;
1637 mgmt_class
= convert_mgmt_class(agent_priv
->reg_req
->mgmt_class
);
1638 class = port_priv
->version
[
1639 agent_priv
->reg_req
->mgmt_class_version
].class;
1643 method
= class->method_table
[mgmt_class
];
1645 /* Remove any methods for this mad agent */
1646 remove_methods_mad_agent(method
, agent_priv
);
1647 /* Now, check to see if there are any methods still in use */
1648 if (!check_method_table(method
)) {
1649 /* If not, release management method table */
1651 class->method_table
[mgmt_class
] = NULL
;
1652 /* Any management classes left ? */
1653 if (!check_class_table(class)) {
1654 /* If not, release management class table */
1657 agent_priv
->reg_req
->
1658 mgmt_class_version
].class = NULL
;
1664 if (!is_vendor_class(mgmt_class
))
1667 /* normalize mgmt_class to vendor range 2 */
1668 mgmt_class
= vendor_class_index(agent_priv
->reg_req
->mgmt_class
);
1669 vendor
= port_priv
->version
[
1670 agent_priv
->reg_req
->mgmt_class_version
].vendor
;
1675 vendor_class
= vendor
->vendor_class
[mgmt_class
];
1677 index
= find_vendor_oui(vendor_class
, agent_priv
->reg_req
->oui
);
1680 method
= vendor_class
->method_table
[index
];
1682 /* Remove any methods for this mad agent */
1683 remove_methods_mad_agent(method
, agent_priv
);
1685 * Now, check to see if there are
1686 * any methods still in use
1688 if (!check_method_table(method
)) {
1689 /* If not, release management method table */
1691 vendor_class
->method_table
[index
] = NULL
;
1692 memset(vendor_class
->oui
[index
], 0, 3);
1693 /* Any OUIs left ? */
1694 if (!check_vendor_class(vendor_class
)) {
1695 /* If not, release vendor class table */
1696 kfree(vendor_class
);
1697 vendor
->vendor_class
[mgmt_class
] = NULL
;
1698 /* Any other vendor classes left ? */
1699 if (!check_vendor_table(vendor
)) {
1702 agent_priv
->reg_req
->
1703 mgmt_class_version
].
1715 static struct ib_mad_agent_private
*
1716 find_mad_agent(struct ib_mad_port_private
*port_priv
,
1717 const struct ib_mad_hdr
*mad_hdr
)
1719 struct ib_mad_agent_private
*mad_agent
= NULL
;
1720 unsigned long flags
;
1722 spin_lock_irqsave(&port_priv
->reg_lock
, flags
);
1723 if (ib_response_mad(mad_hdr
)) {
1725 struct ib_mad_agent_private
*entry
;
1728 * Routing is based on high 32 bits of transaction ID
1731 hi_tid
= be64_to_cpu(mad_hdr
->tid
) >> 32;
1732 list_for_each_entry(entry
, &port_priv
->agent_list
, agent_list
) {
1733 if (entry
->agent
.hi_tid
== hi_tid
) {
1739 struct ib_mad_mgmt_class_table
*class;
1740 struct ib_mad_mgmt_method_table
*method
;
1741 struct ib_mad_mgmt_vendor_class_table
*vendor
;
1742 struct ib_mad_mgmt_vendor_class
*vendor_class
;
1743 const struct ib_vendor_mad
*vendor_mad
;
1747 * Routing is based on version, class, and method
1748 * For "newer" vendor MADs, also based on OUI
1750 if (mad_hdr
->class_version
>= MAX_MGMT_VERSION
)
1752 if (!is_vendor_class(mad_hdr
->mgmt_class
)) {
1753 class = port_priv
->version
[
1754 mad_hdr
->class_version
].class;
1757 if (convert_mgmt_class(mad_hdr
->mgmt_class
) >=
1758 IB_MGMT_MAX_METHODS
)
1760 method
= class->method_table
[convert_mgmt_class(
1761 mad_hdr
->mgmt_class
)];
1763 mad_agent
= method
->agent
[mad_hdr
->method
&
1764 ~IB_MGMT_METHOD_RESP
];
1766 vendor
= port_priv
->version
[
1767 mad_hdr
->class_version
].vendor
;
1770 vendor_class
= vendor
->vendor_class
[vendor_class_index(
1771 mad_hdr
->mgmt_class
)];
1774 /* Find matching OUI */
1775 vendor_mad
= (const struct ib_vendor_mad
*)mad_hdr
;
1776 index
= find_vendor_oui(vendor_class
, vendor_mad
->oui
);
1779 method
= vendor_class
->method_table
[index
];
1781 mad_agent
= method
->agent
[mad_hdr
->method
&
1782 ~IB_MGMT_METHOD_RESP
];
1788 if (mad_agent
->agent
.recv_handler
)
1789 atomic_inc(&mad_agent
->refcount
);
1791 dev_notice(&port_priv
->device
->dev
,
1792 "No receive handler for client %p on port %d\n",
1793 &mad_agent
->agent
, port_priv
->port_num
);
1798 spin_unlock_irqrestore(&port_priv
->reg_lock
, flags
);
1803 static int validate_mad(const struct ib_mad_hdr
*mad_hdr
,
1804 const struct ib_mad_qp_info
*qp_info
,
1808 u32 qp_num
= qp_info
->qp
->qp_num
;
1810 /* Make sure MAD base version is understood */
1811 if (mad_hdr
->base_version
!= IB_MGMT_BASE_VERSION
&&
1812 (!opa
|| mad_hdr
->base_version
!= OPA_MGMT_BASE_VERSION
)) {
1813 pr_err("MAD received with unsupported base version %d %s\n",
1814 mad_hdr
->base_version
, opa
? "(opa)" : "");
1818 /* Filter SMI packets sent to other than QP0 */
1819 if ((mad_hdr
->mgmt_class
== IB_MGMT_CLASS_SUBN_LID_ROUTED
) ||
1820 (mad_hdr
->mgmt_class
== IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
)) {
1824 /* Filter GSI packets sent to QP0 */
1833 static int is_rmpp_data_mad(const struct ib_mad_agent_private
*mad_agent_priv
,
1834 const struct ib_mad_hdr
*mad_hdr
)
1836 struct ib_rmpp_mad
*rmpp_mad
;
1838 rmpp_mad
= (struct ib_rmpp_mad
*)mad_hdr
;
1839 return !mad_agent_priv
->agent
.rmpp_version
||
1840 !ib_mad_kernel_rmpp_agent(&mad_agent_priv
->agent
) ||
1841 !(ib_get_rmpp_flags(&rmpp_mad
->rmpp_hdr
) &
1842 IB_MGMT_RMPP_FLAG_ACTIVE
) ||
1843 (rmpp_mad
->rmpp_hdr
.rmpp_type
== IB_MGMT_RMPP_TYPE_DATA
);
1846 static inline int rcv_has_same_class(const struct ib_mad_send_wr_private
*wr
,
1847 const struct ib_mad_recv_wc
*rwc
)
1849 return ((struct ib_mad_hdr
*)(wr
->send_buf
.mad
))->mgmt_class
==
1850 rwc
->recv_buf
.mad
->mad_hdr
.mgmt_class
;
1853 static inline int rcv_has_same_gid(const struct ib_mad_agent_private
*mad_agent_priv
,
1854 const struct ib_mad_send_wr_private
*wr
,
1855 const struct ib_mad_recv_wc
*rwc
)
1857 struct ib_ah_attr attr
;
1858 u8 send_resp
, rcv_resp
;
1860 struct ib_device
*device
= mad_agent_priv
->agent
.device
;
1861 u8 port_num
= mad_agent_priv
->agent
.port_num
;
1864 send_resp
= ib_response_mad((struct ib_mad_hdr
*)wr
->send_buf
.mad
);
1865 rcv_resp
= ib_response_mad(&rwc
->recv_buf
.mad
->mad_hdr
);
1867 if (send_resp
== rcv_resp
)
1868 /* both requests, or both responses. GIDs different */
1871 if (ib_query_ah(wr
->send_buf
.ah
, &attr
))
1872 /* Assume not equal, to avoid false positives. */
1875 if (!!(attr
.ah_flags
& IB_AH_GRH
) !=
1876 !!(rwc
->wc
->wc_flags
& IB_WC_GRH
))
1877 /* one has GID, other does not. Assume different */
1880 if (!send_resp
&& rcv_resp
) {
1881 /* is request/response. */
1882 if (!(attr
.ah_flags
& IB_AH_GRH
)) {
1883 if (ib_get_cached_lmc(device
, port_num
, &lmc
))
1885 return (!lmc
|| !((attr
.src_path_bits
^
1886 rwc
->wc
->dlid_path_bits
) &
1889 if (ib_get_cached_gid(device
, port_num
,
1890 attr
.grh
.sgid_index
, &sgid
))
1892 return !memcmp(sgid
.raw
, rwc
->recv_buf
.grh
->dgid
.raw
,
1897 if (!(attr
.ah_flags
& IB_AH_GRH
))
1898 return attr
.dlid
== rwc
->wc
->slid
;
1900 return !memcmp(attr
.grh
.dgid
.raw
, rwc
->recv_buf
.grh
->sgid
.raw
,
1904 static inline int is_direct(u8
class)
1906 return (class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
);
1909 struct ib_mad_send_wr_private
*
1910 ib_find_send_mad(const struct ib_mad_agent_private
*mad_agent_priv
,
1911 const struct ib_mad_recv_wc
*wc
)
1913 struct ib_mad_send_wr_private
*wr
;
1914 const struct ib_mad_hdr
*mad_hdr
;
1916 mad_hdr
= &wc
->recv_buf
.mad
->mad_hdr
;
1918 list_for_each_entry(wr
, &mad_agent_priv
->wait_list
, agent_list
) {
1919 if ((wr
->tid
== mad_hdr
->tid
) &&
1920 rcv_has_same_class(wr
, wc
) &&
1922 * Don't check GID for direct routed MADs.
1923 * These might have permissive LIDs.
1925 (is_direct(mad_hdr
->mgmt_class
) ||
1926 rcv_has_same_gid(mad_agent_priv
, wr
, wc
)))
1927 return (wr
->status
== IB_WC_SUCCESS
) ? wr
: NULL
;
1931 * It's possible to receive the response before we've
1932 * been notified that the send has completed
1934 list_for_each_entry(wr
, &mad_agent_priv
->send_list
, agent_list
) {
1935 if (is_rmpp_data_mad(mad_agent_priv
, wr
->send_buf
.mad
) &&
1936 wr
->tid
== mad_hdr
->tid
&&
1938 rcv_has_same_class(wr
, wc
) &&
1940 * Don't check GID for direct routed MADs.
1941 * These might have permissive LIDs.
1943 (is_direct(mad_hdr
->mgmt_class
) ||
1944 rcv_has_same_gid(mad_agent_priv
, wr
, wc
)))
1945 /* Verify request has not been canceled */
1946 return (wr
->status
== IB_WC_SUCCESS
) ? wr
: NULL
;
1951 void ib_mark_mad_done(struct ib_mad_send_wr_private
*mad_send_wr
)
1953 mad_send_wr
->timeout
= 0;
1954 if (mad_send_wr
->refcount
== 1)
1955 list_move_tail(&mad_send_wr
->agent_list
,
1956 &mad_send_wr
->mad_agent_priv
->done_list
);
1959 static void ib_mad_complete_recv(struct ib_mad_agent_private
*mad_agent_priv
,
1960 struct ib_mad_recv_wc
*mad_recv_wc
)
1962 struct ib_mad_send_wr_private
*mad_send_wr
;
1963 struct ib_mad_send_wc mad_send_wc
;
1964 unsigned long flags
;
1966 INIT_LIST_HEAD(&mad_recv_wc
->rmpp_list
);
1967 list_add(&mad_recv_wc
->recv_buf
.list
, &mad_recv_wc
->rmpp_list
);
1968 if (ib_mad_kernel_rmpp_agent(&mad_agent_priv
->agent
)) {
1969 mad_recv_wc
= ib_process_rmpp_recv_wc(mad_agent_priv
,
1972 deref_mad_agent(mad_agent_priv
);
1977 /* Complete corresponding request */
1978 if (ib_response_mad(&mad_recv_wc
->recv_buf
.mad
->mad_hdr
)) {
1979 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
1980 mad_send_wr
= ib_find_send_mad(mad_agent_priv
, mad_recv_wc
);
1982 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
1983 if (!ib_mad_kernel_rmpp_agent(&mad_agent_priv
->agent
)
1984 && ib_is_mad_class_rmpp(mad_recv_wc
->recv_buf
.mad
->mad_hdr
.mgmt_class
)
1985 && (ib_get_rmpp_flags(&((struct ib_rmpp_mad
*)mad_recv_wc
->recv_buf
.mad
)->rmpp_hdr
)
1986 & IB_MGMT_RMPP_FLAG_ACTIVE
)) {
1987 /* user rmpp is in effect
1988 * and this is an active RMPP MAD
1990 mad_recv_wc
->wc
->wr_id
= 0;
1991 mad_agent_priv
->agent
.recv_handler(&mad_agent_priv
->agent
,
1993 atomic_dec(&mad_agent_priv
->refcount
);
1995 /* not user rmpp, revert to normal behavior and
1997 ib_free_recv_mad(mad_recv_wc
);
1998 deref_mad_agent(mad_agent_priv
);
2002 ib_mark_mad_done(mad_send_wr
);
2003 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2005 /* Defined behavior is to complete response before request */
2006 mad_recv_wc
->wc
->wr_id
= (unsigned long) &mad_send_wr
->send_buf
;
2007 mad_agent_priv
->agent
.recv_handler(&mad_agent_priv
->agent
,
2009 atomic_dec(&mad_agent_priv
->refcount
);
2011 mad_send_wc
.status
= IB_WC_SUCCESS
;
2012 mad_send_wc
.vendor_err
= 0;
2013 mad_send_wc
.send_buf
= &mad_send_wr
->send_buf
;
2014 ib_mad_complete_send_wr(mad_send_wr
, &mad_send_wc
);
2017 mad_agent_priv
->agent
.recv_handler(&mad_agent_priv
->agent
,
2019 deref_mad_agent(mad_agent_priv
);
2023 static enum smi_action
handle_ib_smi(const struct ib_mad_port_private
*port_priv
,
2024 const struct ib_mad_qp_info
*qp_info
,
2025 const struct ib_wc
*wc
,
2027 struct ib_mad_private
*recv
,
2028 struct ib_mad_private
*response
)
2030 enum smi_forward_action retsmi
;
2031 struct ib_smp
*smp
= (struct ib_smp
*)recv
->mad
;
2033 if (smi_handle_dr_smp_recv(smp
,
2034 rdma_cap_ib_switch(port_priv
->device
),
2036 port_priv
->device
->phys_port_cnt
) ==
2038 return IB_SMI_DISCARD
;
2040 retsmi
= smi_check_forward_dr_smp(smp
);
2041 if (retsmi
== IB_SMI_LOCAL
)
2042 return IB_SMI_HANDLE
;
2044 if (retsmi
== IB_SMI_SEND
) { /* don't forward */
2045 if (smi_handle_dr_smp_send(smp
,
2046 rdma_cap_ib_switch(port_priv
->device
),
2047 port_num
) == IB_SMI_DISCARD
)
2048 return IB_SMI_DISCARD
;
2050 if (smi_check_local_smp(smp
, port_priv
->device
) == IB_SMI_DISCARD
)
2051 return IB_SMI_DISCARD
;
2052 } else if (rdma_cap_ib_switch(port_priv
->device
)) {
2053 /* forward case for switches */
2054 memcpy(response
, recv
, mad_priv_size(response
));
2055 response
->header
.recv_wc
.wc
= &response
->header
.wc
;
2056 response
->header
.recv_wc
.recv_buf
.mad
= (struct ib_mad
*)response
->mad
;
2057 response
->header
.recv_wc
.recv_buf
.grh
= &response
->grh
;
2059 agent_send_response((const struct ib_mad_hdr
*)response
->mad
,
2062 smi_get_fwd_port(smp
),
2063 qp_info
->qp
->qp_num
,
2067 return IB_SMI_DISCARD
;
2069 return IB_SMI_HANDLE
;
2072 static bool generate_unmatched_resp(const struct ib_mad_private
*recv
,
2073 struct ib_mad_private
*response
,
2074 size_t *resp_len
, bool opa
)
2076 const struct ib_mad_hdr
*recv_hdr
= (const struct ib_mad_hdr
*)recv
->mad
;
2077 struct ib_mad_hdr
*resp_hdr
= (struct ib_mad_hdr
*)response
->mad
;
2079 if (recv_hdr
->method
== IB_MGMT_METHOD_GET
||
2080 recv_hdr
->method
== IB_MGMT_METHOD_SET
) {
2081 memcpy(response
, recv
, mad_priv_size(response
));
2082 response
->header
.recv_wc
.wc
= &response
->header
.wc
;
2083 response
->header
.recv_wc
.recv_buf
.mad
= (struct ib_mad
*)response
->mad
;
2084 response
->header
.recv_wc
.recv_buf
.grh
= &response
->grh
;
2085 resp_hdr
->method
= IB_MGMT_METHOD_GET_RESP
;
2086 resp_hdr
->status
= cpu_to_be16(IB_MGMT_MAD_STATUS_UNSUPPORTED_METHOD_ATTRIB
);
2087 if (recv_hdr
->mgmt_class
== IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
)
2088 resp_hdr
->status
|= IB_SMP_DIRECTION
;
2090 if (opa
&& recv_hdr
->base_version
== OPA_MGMT_BASE_VERSION
) {
2091 if (recv_hdr
->mgmt_class
==
2092 IB_MGMT_CLASS_SUBN_LID_ROUTED
||
2093 recv_hdr
->mgmt_class
==
2094 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
)
2095 *resp_len
= opa_get_smp_header_size(
2096 (struct opa_smp
*)recv
->mad
);
2098 *resp_len
= sizeof(struct ib_mad_hdr
);
2107 static enum smi_action
2108 handle_opa_smi(struct ib_mad_port_private
*port_priv
,
2109 struct ib_mad_qp_info
*qp_info
,
2112 struct ib_mad_private
*recv
,
2113 struct ib_mad_private
*response
)
2115 enum smi_forward_action retsmi
;
2116 struct opa_smp
*smp
= (struct opa_smp
*)recv
->mad
;
2118 if (opa_smi_handle_dr_smp_recv(smp
,
2119 rdma_cap_ib_switch(port_priv
->device
),
2121 port_priv
->device
->phys_port_cnt
) ==
2123 return IB_SMI_DISCARD
;
2125 retsmi
= opa_smi_check_forward_dr_smp(smp
);
2126 if (retsmi
== IB_SMI_LOCAL
)
2127 return IB_SMI_HANDLE
;
2129 if (retsmi
== IB_SMI_SEND
) { /* don't forward */
2130 if (opa_smi_handle_dr_smp_send(smp
,
2131 rdma_cap_ib_switch(port_priv
->device
),
2132 port_num
) == IB_SMI_DISCARD
)
2133 return IB_SMI_DISCARD
;
2135 if (opa_smi_check_local_smp(smp
, port_priv
->device
) ==
2137 return IB_SMI_DISCARD
;
2139 } else if (rdma_cap_ib_switch(port_priv
->device
)) {
2140 /* forward case for switches */
2141 memcpy(response
, recv
, mad_priv_size(response
));
2142 response
->header
.recv_wc
.wc
= &response
->header
.wc
;
2143 response
->header
.recv_wc
.recv_buf
.opa_mad
=
2144 (struct opa_mad
*)response
->mad
;
2145 response
->header
.recv_wc
.recv_buf
.grh
= &response
->grh
;
2147 agent_send_response((const struct ib_mad_hdr
*)response
->mad
,
2150 opa_smi_get_fwd_port(smp
),
2151 qp_info
->qp
->qp_num
,
2152 recv
->header
.wc
.byte_len
,
2155 return IB_SMI_DISCARD
;
2158 return IB_SMI_HANDLE
;
2161 static enum smi_action
2162 handle_smi(struct ib_mad_port_private
*port_priv
,
2163 struct ib_mad_qp_info
*qp_info
,
2166 struct ib_mad_private
*recv
,
2167 struct ib_mad_private
*response
,
2170 struct ib_mad_hdr
*mad_hdr
= (struct ib_mad_hdr
*)recv
->mad
;
2172 if (opa
&& mad_hdr
->base_version
== OPA_MGMT_BASE_VERSION
&&
2173 mad_hdr
->class_version
== OPA_SMI_CLASS_VERSION
)
2174 return handle_opa_smi(port_priv
, qp_info
, wc
, port_num
, recv
,
2177 return handle_ib_smi(port_priv
, qp_info
, wc
, port_num
, recv
, response
);
2180 static void ib_mad_recv_done_handler(struct ib_mad_port_private
*port_priv
,
2183 struct ib_mad_qp_info
*qp_info
;
2184 struct ib_mad_private_header
*mad_priv_hdr
;
2185 struct ib_mad_private
*recv
, *response
= NULL
;
2186 struct ib_mad_list_head
*mad_list
;
2187 struct ib_mad_agent_private
*mad_agent
;
2189 int ret
= IB_MAD_RESULT_SUCCESS
;
2191 u16 resp_mad_pkey_index
= 0;
2194 mad_list
= (struct ib_mad_list_head
*)(unsigned long)wc
->wr_id
;
2195 qp_info
= mad_list
->mad_queue
->qp_info
;
2196 dequeue_mad(mad_list
);
2198 opa
= rdma_cap_opa_mad(qp_info
->port_priv
->device
,
2199 qp_info
->port_priv
->port_num
);
2201 mad_priv_hdr
= container_of(mad_list
, struct ib_mad_private_header
,
2203 recv
= container_of(mad_priv_hdr
, struct ib_mad_private
, header
);
2204 ib_dma_unmap_single(port_priv
->device
,
2205 recv
->header
.mapping
,
2206 mad_priv_dma_size(recv
),
2209 /* Setup MAD receive work completion from "normal" work completion */
2210 recv
->header
.wc
= *wc
;
2211 recv
->header
.recv_wc
.wc
= &recv
->header
.wc
;
2213 if (opa
&& ((struct ib_mad_hdr
*)(recv
->mad
))->base_version
== OPA_MGMT_BASE_VERSION
) {
2214 recv
->header
.recv_wc
.mad_len
= wc
->byte_len
- sizeof(struct ib_grh
);
2215 recv
->header
.recv_wc
.mad_seg_size
= sizeof(struct opa_mad
);
2217 recv
->header
.recv_wc
.mad_len
= sizeof(struct ib_mad
);
2218 recv
->header
.recv_wc
.mad_seg_size
= sizeof(struct ib_mad
);
2221 recv
->header
.recv_wc
.recv_buf
.mad
= (struct ib_mad
*)recv
->mad
;
2222 recv
->header
.recv_wc
.recv_buf
.grh
= &recv
->grh
;
2224 if (atomic_read(&qp_info
->snoop_count
))
2225 snoop_recv(qp_info
, &recv
->header
.recv_wc
, IB_MAD_SNOOP_RECVS
);
2228 if (!validate_mad((const struct ib_mad_hdr
*)recv
->mad
, qp_info
, opa
))
2231 mad_size
= recv
->mad_size
;
2232 response
= alloc_mad_private(mad_size
, GFP_KERNEL
);
2234 dev_err(&port_priv
->device
->dev
,
2235 "ib_mad_recv_done_handler no memory for response buffer\n");
2239 if (rdma_cap_ib_switch(port_priv
->device
))
2240 port_num
= wc
->port_num
;
2242 port_num
= port_priv
->port_num
;
2244 if (((struct ib_mad_hdr
*)recv
->mad
)->mgmt_class
==
2245 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
) {
2246 if (handle_smi(port_priv
, qp_info
, wc
, port_num
, recv
,
2252 /* Give driver "right of first refusal" on incoming MAD */
2253 if (port_priv
->device
->process_mad
) {
2254 ret
= port_priv
->device
->process_mad(port_priv
->device
, 0,
2255 port_priv
->port_num
,
2257 (const struct ib_mad_hdr
*)recv
->mad
,
2259 (struct ib_mad_hdr
*)response
->mad
,
2260 &mad_size
, &resp_mad_pkey_index
);
2263 wc
->pkey_index
= resp_mad_pkey_index
;
2265 if (ret
& IB_MAD_RESULT_SUCCESS
) {
2266 if (ret
& IB_MAD_RESULT_CONSUMED
)
2268 if (ret
& IB_MAD_RESULT_REPLY
) {
2269 agent_send_response((const struct ib_mad_hdr
*)response
->mad
,
2273 qp_info
->qp
->qp_num
,
2280 mad_agent
= find_mad_agent(port_priv
, (const struct ib_mad_hdr
*)recv
->mad
);
2282 ib_mad_complete_recv(mad_agent
, &recv
->header
.recv_wc
);
2284 * recv is freed up in error cases in ib_mad_complete_recv
2285 * or via recv_handler in ib_mad_complete_recv()
2288 } else if ((ret
& IB_MAD_RESULT_SUCCESS
) &&
2289 generate_unmatched_resp(recv
, response
, &mad_size
, opa
)) {
2290 agent_send_response((const struct ib_mad_hdr
*)response
->mad
, &recv
->grh
, wc
,
2291 port_priv
->device
, port_num
,
2292 qp_info
->qp
->qp_num
, mad_size
, opa
);
2296 /* Post another receive request for this QP */
2298 ib_mad_post_receive_mads(qp_info
, response
);
2301 ib_mad_post_receive_mads(qp_info
, recv
);
2304 static void adjust_timeout(struct ib_mad_agent_private
*mad_agent_priv
)
2306 struct ib_mad_send_wr_private
*mad_send_wr
;
2307 unsigned long delay
;
2309 if (list_empty(&mad_agent_priv
->wait_list
)) {
2310 cancel_delayed_work(&mad_agent_priv
->timed_work
);
2312 mad_send_wr
= list_entry(mad_agent_priv
->wait_list
.next
,
2313 struct ib_mad_send_wr_private
,
2316 if (time_after(mad_agent_priv
->timeout
,
2317 mad_send_wr
->timeout
)) {
2318 mad_agent_priv
->timeout
= mad_send_wr
->timeout
;
2319 delay
= mad_send_wr
->timeout
- jiffies
;
2320 if ((long)delay
<= 0)
2322 mod_delayed_work(mad_agent_priv
->qp_info
->port_priv
->wq
,
2323 &mad_agent_priv
->timed_work
, delay
);
2328 static void wait_for_response(struct ib_mad_send_wr_private
*mad_send_wr
)
2330 struct ib_mad_agent_private
*mad_agent_priv
;
2331 struct ib_mad_send_wr_private
*temp_mad_send_wr
;
2332 struct list_head
*list_item
;
2333 unsigned long delay
;
2335 mad_agent_priv
= mad_send_wr
->mad_agent_priv
;
2336 list_del(&mad_send_wr
->agent_list
);
2338 delay
= mad_send_wr
->timeout
;
2339 mad_send_wr
->timeout
+= jiffies
;
2342 list_for_each_prev(list_item
, &mad_agent_priv
->wait_list
) {
2343 temp_mad_send_wr
= list_entry(list_item
,
2344 struct ib_mad_send_wr_private
,
2346 if (time_after(mad_send_wr
->timeout
,
2347 temp_mad_send_wr
->timeout
))
2352 list_item
= &mad_agent_priv
->wait_list
;
2353 list_add(&mad_send_wr
->agent_list
, list_item
);
2355 /* Reschedule a work item if we have a shorter timeout */
2356 if (mad_agent_priv
->wait_list
.next
== &mad_send_wr
->agent_list
)
2357 mod_delayed_work(mad_agent_priv
->qp_info
->port_priv
->wq
,
2358 &mad_agent_priv
->timed_work
, delay
);
2361 void ib_reset_mad_timeout(struct ib_mad_send_wr_private
*mad_send_wr
,
2364 mad_send_wr
->timeout
= msecs_to_jiffies(timeout_ms
);
2365 wait_for_response(mad_send_wr
);
2369 * Process a send work completion
2371 void ib_mad_complete_send_wr(struct ib_mad_send_wr_private
*mad_send_wr
,
2372 struct ib_mad_send_wc
*mad_send_wc
)
2374 struct ib_mad_agent_private
*mad_agent_priv
;
2375 unsigned long flags
;
2378 mad_agent_priv
= mad_send_wr
->mad_agent_priv
;
2379 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2380 if (ib_mad_kernel_rmpp_agent(&mad_agent_priv
->agent
)) {
2381 ret
= ib_process_rmpp_send_wc(mad_send_wr
, mad_send_wc
);
2382 if (ret
== IB_RMPP_RESULT_CONSUMED
)
2385 ret
= IB_RMPP_RESULT_UNHANDLED
;
2387 if (mad_send_wc
->status
!= IB_WC_SUCCESS
&&
2388 mad_send_wr
->status
== IB_WC_SUCCESS
) {
2389 mad_send_wr
->status
= mad_send_wc
->status
;
2390 mad_send_wr
->refcount
-= (mad_send_wr
->timeout
> 0);
2393 if (--mad_send_wr
->refcount
> 0) {
2394 if (mad_send_wr
->refcount
== 1 && mad_send_wr
->timeout
&&
2395 mad_send_wr
->status
== IB_WC_SUCCESS
) {
2396 wait_for_response(mad_send_wr
);
2401 /* Remove send from MAD agent and notify client of completion */
2402 list_del(&mad_send_wr
->agent_list
);
2403 adjust_timeout(mad_agent_priv
);
2404 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2406 if (mad_send_wr
->status
!= IB_WC_SUCCESS
)
2407 mad_send_wc
->status
= mad_send_wr
->status
;
2408 if (ret
== IB_RMPP_RESULT_INTERNAL
)
2409 ib_rmpp_send_handler(mad_send_wc
);
2411 mad_agent_priv
->agent
.send_handler(&mad_agent_priv
->agent
,
2414 /* Release reference on agent taken when sending */
2415 deref_mad_agent(mad_agent_priv
);
2418 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2421 static void ib_mad_send_done_handler(struct ib_mad_port_private
*port_priv
,
2424 struct ib_mad_send_wr_private
*mad_send_wr
, *queued_send_wr
;
2425 struct ib_mad_list_head
*mad_list
;
2426 struct ib_mad_qp_info
*qp_info
;
2427 struct ib_mad_queue
*send_queue
;
2428 struct ib_send_wr
*bad_send_wr
;
2429 struct ib_mad_send_wc mad_send_wc
;
2430 unsigned long flags
;
2433 mad_list
= (struct ib_mad_list_head
*)(unsigned long)wc
->wr_id
;
2434 mad_send_wr
= container_of(mad_list
, struct ib_mad_send_wr_private
,
2436 send_queue
= mad_list
->mad_queue
;
2437 qp_info
= send_queue
->qp_info
;
2440 ib_dma_unmap_single(mad_send_wr
->send_buf
.mad_agent
->device
,
2441 mad_send_wr
->header_mapping
,
2442 mad_send_wr
->sg_list
[0].length
, DMA_TO_DEVICE
);
2443 ib_dma_unmap_single(mad_send_wr
->send_buf
.mad_agent
->device
,
2444 mad_send_wr
->payload_mapping
,
2445 mad_send_wr
->sg_list
[1].length
, DMA_TO_DEVICE
);
2446 queued_send_wr
= NULL
;
2447 spin_lock_irqsave(&send_queue
->lock
, flags
);
2448 list_del(&mad_list
->list
);
2450 /* Move queued send to the send queue */
2451 if (send_queue
->count
-- > send_queue
->max_active
) {
2452 mad_list
= container_of(qp_info
->overflow_list
.next
,
2453 struct ib_mad_list_head
, list
);
2454 queued_send_wr
= container_of(mad_list
,
2455 struct ib_mad_send_wr_private
,
2457 list_move_tail(&mad_list
->list
, &send_queue
->list
);
2459 spin_unlock_irqrestore(&send_queue
->lock
, flags
);
2461 mad_send_wc
.send_buf
= &mad_send_wr
->send_buf
;
2462 mad_send_wc
.status
= wc
->status
;
2463 mad_send_wc
.vendor_err
= wc
->vendor_err
;
2464 if (atomic_read(&qp_info
->snoop_count
))
2465 snoop_send(qp_info
, &mad_send_wr
->send_buf
, &mad_send_wc
,
2466 IB_MAD_SNOOP_SEND_COMPLETIONS
);
2467 ib_mad_complete_send_wr(mad_send_wr
, &mad_send_wc
);
2469 if (queued_send_wr
) {
2470 ret
= ib_post_send(qp_info
->qp
, &queued_send_wr
->send_wr
,
2473 dev_err(&port_priv
->device
->dev
,
2474 "ib_post_send failed: %d\n", ret
);
2475 mad_send_wr
= queued_send_wr
;
2476 wc
->status
= IB_WC_LOC_QP_OP_ERR
;
2482 static void mark_sends_for_retry(struct ib_mad_qp_info
*qp_info
)
2484 struct ib_mad_send_wr_private
*mad_send_wr
;
2485 struct ib_mad_list_head
*mad_list
;
2486 unsigned long flags
;
2488 spin_lock_irqsave(&qp_info
->send_queue
.lock
, flags
);
2489 list_for_each_entry(mad_list
, &qp_info
->send_queue
.list
, list
) {
2490 mad_send_wr
= container_of(mad_list
,
2491 struct ib_mad_send_wr_private
,
2493 mad_send_wr
->retry
= 1;
2495 spin_unlock_irqrestore(&qp_info
->send_queue
.lock
, flags
);
2498 static void mad_error_handler(struct ib_mad_port_private
*port_priv
,
2501 struct ib_mad_list_head
*mad_list
;
2502 struct ib_mad_qp_info
*qp_info
;
2503 struct ib_mad_send_wr_private
*mad_send_wr
;
2506 /* Determine if failure was a send or receive */
2507 mad_list
= (struct ib_mad_list_head
*)(unsigned long)wc
->wr_id
;
2508 qp_info
= mad_list
->mad_queue
->qp_info
;
2509 if (mad_list
->mad_queue
== &qp_info
->recv_queue
)
2511 * Receive errors indicate that the QP has entered the error
2512 * state - error handling/shutdown code will cleanup
2517 * Send errors will transition the QP to SQE - move
2518 * QP to RTS and repost flushed work requests
2520 mad_send_wr
= container_of(mad_list
, struct ib_mad_send_wr_private
,
2522 if (wc
->status
== IB_WC_WR_FLUSH_ERR
) {
2523 if (mad_send_wr
->retry
) {
2525 struct ib_send_wr
*bad_send_wr
;
2527 mad_send_wr
->retry
= 0;
2528 ret
= ib_post_send(qp_info
->qp
, &mad_send_wr
->send_wr
,
2531 ib_mad_send_done_handler(port_priv
, wc
);
2533 ib_mad_send_done_handler(port_priv
, wc
);
2535 struct ib_qp_attr
*attr
;
2537 /* Transition QP to RTS and fail offending send */
2538 attr
= kmalloc(sizeof *attr
, GFP_KERNEL
);
2540 attr
->qp_state
= IB_QPS_RTS
;
2541 attr
->cur_qp_state
= IB_QPS_SQE
;
2542 ret
= ib_modify_qp(qp_info
->qp
, attr
,
2543 IB_QP_STATE
| IB_QP_CUR_STATE
);
2546 dev_err(&port_priv
->device
->dev
,
2547 "mad_error_handler - ib_modify_qp to RTS : %d\n",
2550 mark_sends_for_retry(qp_info
);
2552 ib_mad_send_done_handler(port_priv
, wc
);
2557 * IB MAD completion callback
2559 static void ib_mad_completion_handler(struct work_struct
*work
)
2561 struct ib_mad_port_private
*port_priv
;
2564 port_priv
= container_of(work
, struct ib_mad_port_private
, work
);
2565 ib_req_notify_cq(port_priv
->cq
, IB_CQ_NEXT_COMP
);
2567 while (ib_poll_cq(port_priv
->cq
, 1, &wc
) == 1) {
2568 if (wc
.status
== IB_WC_SUCCESS
) {
2569 switch (wc
.opcode
) {
2571 ib_mad_send_done_handler(port_priv
, &wc
);
2574 ib_mad_recv_done_handler(port_priv
, &wc
);
2581 mad_error_handler(port_priv
, &wc
);
2585 static void cancel_mads(struct ib_mad_agent_private
*mad_agent_priv
)
2587 unsigned long flags
;
2588 struct ib_mad_send_wr_private
*mad_send_wr
, *temp_mad_send_wr
;
2589 struct ib_mad_send_wc mad_send_wc
;
2590 struct list_head cancel_list
;
2592 INIT_LIST_HEAD(&cancel_list
);
2594 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2595 list_for_each_entry_safe(mad_send_wr
, temp_mad_send_wr
,
2596 &mad_agent_priv
->send_list
, agent_list
) {
2597 if (mad_send_wr
->status
== IB_WC_SUCCESS
) {
2598 mad_send_wr
->status
= IB_WC_WR_FLUSH_ERR
;
2599 mad_send_wr
->refcount
-= (mad_send_wr
->timeout
> 0);
2603 /* Empty wait list to prevent receives from finding a request */
2604 list_splice_init(&mad_agent_priv
->wait_list
, &cancel_list
);
2605 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2607 /* Report all cancelled requests */
2608 mad_send_wc
.status
= IB_WC_WR_FLUSH_ERR
;
2609 mad_send_wc
.vendor_err
= 0;
2611 list_for_each_entry_safe(mad_send_wr
, temp_mad_send_wr
,
2612 &cancel_list
, agent_list
) {
2613 mad_send_wc
.send_buf
= &mad_send_wr
->send_buf
;
2614 list_del(&mad_send_wr
->agent_list
);
2615 mad_agent_priv
->agent
.send_handler(&mad_agent_priv
->agent
,
2617 atomic_dec(&mad_agent_priv
->refcount
);
2621 static struct ib_mad_send_wr_private
*
2622 find_send_wr(struct ib_mad_agent_private
*mad_agent_priv
,
2623 struct ib_mad_send_buf
*send_buf
)
2625 struct ib_mad_send_wr_private
*mad_send_wr
;
2627 list_for_each_entry(mad_send_wr
, &mad_agent_priv
->wait_list
,
2629 if (&mad_send_wr
->send_buf
== send_buf
)
2633 list_for_each_entry(mad_send_wr
, &mad_agent_priv
->send_list
,
2635 if (is_rmpp_data_mad(mad_agent_priv
,
2636 mad_send_wr
->send_buf
.mad
) &&
2637 &mad_send_wr
->send_buf
== send_buf
)
2643 int ib_modify_mad(struct ib_mad_agent
*mad_agent
,
2644 struct ib_mad_send_buf
*send_buf
, u32 timeout_ms
)
2646 struct ib_mad_agent_private
*mad_agent_priv
;
2647 struct ib_mad_send_wr_private
*mad_send_wr
;
2648 unsigned long flags
;
2651 mad_agent_priv
= container_of(mad_agent
, struct ib_mad_agent_private
,
2653 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2654 mad_send_wr
= find_send_wr(mad_agent_priv
, send_buf
);
2655 if (!mad_send_wr
|| mad_send_wr
->status
!= IB_WC_SUCCESS
) {
2656 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2660 active
= (!mad_send_wr
->timeout
|| mad_send_wr
->refcount
> 1);
2662 mad_send_wr
->status
= IB_WC_WR_FLUSH_ERR
;
2663 mad_send_wr
->refcount
-= (mad_send_wr
->timeout
> 0);
2666 mad_send_wr
->send_buf
.timeout_ms
= timeout_ms
;
2668 mad_send_wr
->timeout
= msecs_to_jiffies(timeout_ms
);
2670 ib_reset_mad_timeout(mad_send_wr
, timeout_ms
);
2672 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2675 EXPORT_SYMBOL(ib_modify_mad
);
2677 void ib_cancel_mad(struct ib_mad_agent
*mad_agent
,
2678 struct ib_mad_send_buf
*send_buf
)
2680 ib_modify_mad(mad_agent
, send_buf
, 0);
2682 EXPORT_SYMBOL(ib_cancel_mad
);
2684 static void local_completions(struct work_struct
*work
)
2686 struct ib_mad_agent_private
*mad_agent_priv
;
2687 struct ib_mad_local_private
*local
;
2688 struct ib_mad_agent_private
*recv_mad_agent
;
2689 unsigned long flags
;
2692 struct ib_mad_send_wc mad_send_wc
;
2696 container_of(work
, struct ib_mad_agent_private
, local_work
);
2698 opa
= rdma_cap_opa_mad(mad_agent_priv
->qp_info
->port_priv
->device
,
2699 mad_agent_priv
->qp_info
->port_priv
->port_num
);
2701 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2702 while (!list_empty(&mad_agent_priv
->local_list
)) {
2703 local
= list_entry(mad_agent_priv
->local_list
.next
,
2704 struct ib_mad_local_private
,
2706 list_del(&local
->completion_list
);
2707 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2709 if (local
->mad_priv
) {
2711 recv_mad_agent
= local
->recv_mad_agent
;
2712 if (!recv_mad_agent
) {
2713 dev_err(&mad_agent_priv
->agent
.device
->dev
,
2714 "No receive MAD agent for local completion\n");
2716 goto local_send_completion
;
2720 * Defined behavior is to complete response
2723 build_smp_wc(recv_mad_agent
->agent
.qp
,
2724 (unsigned long) local
->mad_send_wr
,
2725 be16_to_cpu(IB_LID_PERMISSIVE
),
2726 local
->mad_send_wr
->send_wr
.wr
.ud
.pkey_index
,
2727 recv_mad_agent
->agent
.port_num
, &wc
);
2729 local
->mad_priv
->header
.recv_wc
.wc
= &wc
;
2731 base_version
= ((struct ib_mad_hdr
*)(local
->mad_priv
->mad
))->base_version
;
2732 if (opa
&& base_version
== OPA_MGMT_BASE_VERSION
) {
2733 local
->mad_priv
->header
.recv_wc
.mad_len
= local
->return_wc_byte_len
;
2734 local
->mad_priv
->header
.recv_wc
.mad_seg_size
= sizeof(struct opa_mad
);
2736 local
->mad_priv
->header
.recv_wc
.mad_len
= sizeof(struct ib_mad
);
2737 local
->mad_priv
->header
.recv_wc
.mad_seg_size
= sizeof(struct ib_mad
);
2740 INIT_LIST_HEAD(&local
->mad_priv
->header
.recv_wc
.rmpp_list
);
2741 list_add(&local
->mad_priv
->header
.recv_wc
.recv_buf
.list
,
2742 &local
->mad_priv
->header
.recv_wc
.rmpp_list
);
2743 local
->mad_priv
->header
.recv_wc
.recv_buf
.grh
= NULL
;
2744 local
->mad_priv
->header
.recv_wc
.recv_buf
.mad
=
2745 (struct ib_mad
*)local
->mad_priv
->mad
;
2746 if (atomic_read(&recv_mad_agent
->qp_info
->snoop_count
))
2747 snoop_recv(recv_mad_agent
->qp_info
,
2748 &local
->mad_priv
->header
.recv_wc
,
2749 IB_MAD_SNOOP_RECVS
);
2750 recv_mad_agent
->agent
.recv_handler(
2751 &recv_mad_agent
->agent
,
2752 &local
->mad_priv
->header
.recv_wc
);
2753 spin_lock_irqsave(&recv_mad_agent
->lock
, flags
);
2754 atomic_dec(&recv_mad_agent
->refcount
);
2755 spin_unlock_irqrestore(&recv_mad_agent
->lock
, flags
);
2758 local_send_completion
:
2760 mad_send_wc
.status
= IB_WC_SUCCESS
;
2761 mad_send_wc
.vendor_err
= 0;
2762 mad_send_wc
.send_buf
= &local
->mad_send_wr
->send_buf
;
2763 if (atomic_read(&mad_agent_priv
->qp_info
->snoop_count
))
2764 snoop_send(mad_agent_priv
->qp_info
,
2765 &local
->mad_send_wr
->send_buf
,
2766 &mad_send_wc
, IB_MAD_SNOOP_SEND_COMPLETIONS
);
2767 mad_agent_priv
->agent
.send_handler(&mad_agent_priv
->agent
,
2770 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2771 atomic_dec(&mad_agent_priv
->refcount
);
2773 kfree(local
->mad_priv
);
2776 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2779 static int retry_send(struct ib_mad_send_wr_private
*mad_send_wr
)
2783 if (!mad_send_wr
->retries_left
)
2786 mad_send_wr
->retries_left
--;
2787 mad_send_wr
->send_buf
.retries
++;
2789 mad_send_wr
->timeout
= msecs_to_jiffies(mad_send_wr
->send_buf
.timeout_ms
);
2791 if (ib_mad_kernel_rmpp_agent(&mad_send_wr
->mad_agent_priv
->agent
)) {
2792 ret
= ib_retry_rmpp(mad_send_wr
);
2794 case IB_RMPP_RESULT_UNHANDLED
:
2795 ret
= ib_send_mad(mad_send_wr
);
2797 case IB_RMPP_RESULT_CONSUMED
:
2805 ret
= ib_send_mad(mad_send_wr
);
2808 mad_send_wr
->refcount
++;
2809 list_add_tail(&mad_send_wr
->agent_list
,
2810 &mad_send_wr
->mad_agent_priv
->send_list
);
2815 static void timeout_sends(struct work_struct
*work
)
2817 struct ib_mad_agent_private
*mad_agent_priv
;
2818 struct ib_mad_send_wr_private
*mad_send_wr
;
2819 struct ib_mad_send_wc mad_send_wc
;
2820 unsigned long flags
, delay
;
2822 mad_agent_priv
= container_of(work
, struct ib_mad_agent_private
,
2824 mad_send_wc
.vendor_err
= 0;
2826 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2827 while (!list_empty(&mad_agent_priv
->wait_list
)) {
2828 mad_send_wr
= list_entry(mad_agent_priv
->wait_list
.next
,
2829 struct ib_mad_send_wr_private
,
2832 if (time_after(mad_send_wr
->timeout
, jiffies
)) {
2833 delay
= mad_send_wr
->timeout
- jiffies
;
2834 if ((long)delay
<= 0)
2836 queue_delayed_work(mad_agent_priv
->qp_info
->
2838 &mad_agent_priv
->timed_work
, delay
);
2842 list_del(&mad_send_wr
->agent_list
);
2843 if (mad_send_wr
->status
== IB_WC_SUCCESS
&&
2844 !retry_send(mad_send_wr
))
2847 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2849 if (mad_send_wr
->status
== IB_WC_SUCCESS
)
2850 mad_send_wc
.status
= IB_WC_RESP_TIMEOUT_ERR
;
2852 mad_send_wc
.status
= mad_send_wr
->status
;
2853 mad_send_wc
.send_buf
= &mad_send_wr
->send_buf
;
2854 mad_agent_priv
->agent
.send_handler(&mad_agent_priv
->agent
,
2857 atomic_dec(&mad_agent_priv
->refcount
);
2858 spin_lock_irqsave(&mad_agent_priv
->lock
, flags
);
2860 spin_unlock_irqrestore(&mad_agent_priv
->lock
, flags
);
2863 static void ib_mad_thread_completion_handler(struct ib_cq
*cq
, void *arg
)
2865 struct ib_mad_port_private
*port_priv
= cq
->cq_context
;
2866 unsigned long flags
;
2868 spin_lock_irqsave(&ib_mad_port_list_lock
, flags
);
2869 if (!list_empty(&port_priv
->port_list
))
2870 queue_work(port_priv
->wq
, &port_priv
->work
);
2871 spin_unlock_irqrestore(&ib_mad_port_list_lock
, flags
);
2875 * Allocate receive MADs and post receive WRs for them
2877 static int ib_mad_post_receive_mads(struct ib_mad_qp_info
*qp_info
,
2878 struct ib_mad_private
*mad
)
2880 unsigned long flags
;
2882 struct ib_mad_private
*mad_priv
;
2883 struct ib_sge sg_list
;
2884 struct ib_recv_wr recv_wr
, *bad_recv_wr
;
2885 struct ib_mad_queue
*recv_queue
= &qp_info
->recv_queue
;
2887 /* Initialize common scatter list fields */
2888 sg_list
.lkey
= (*qp_info
->port_priv
->mr
).lkey
;
2890 /* Initialize common receive WR fields */
2891 recv_wr
.next
= NULL
;
2892 recv_wr
.sg_list
= &sg_list
;
2893 recv_wr
.num_sge
= 1;
2896 /* Allocate and map receive buffer */
2901 mad_priv
= alloc_mad_private(port_mad_size(qp_info
->port_priv
),
2904 dev_err(&qp_info
->port_priv
->device
->dev
,
2905 "No memory for receive buffer\n");
2910 sg_list
.length
= mad_priv_dma_size(mad_priv
);
2911 sg_list
.addr
= ib_dma_map_single(qp_info
->port_priv
->device
,
2913 mad_priv_dma_size(mad_priv
),
2915 if (unlikely(ib_dma_mapping_error(qp_info
->port_priv
->device
,
2920 mad_priv
->header
.mapping
= sg_list
.addr
;
2921 recv_wr
.wr_id
= (unsigned long)&mad_priv
->header
.mad_list
;
2922 mad_priv
->header
.mad_list
.mad_queue
= recv_queue
;
2924 /* Post receive WR */
2925 spin_lock_irqsave(&recv_queue
->lock
, flags
);
2926 post
= (++recv_queue
->count
< recv_queue
->max_active
);
2927 list_add_tail(&mad_priv
->header
.mad_list
.list
, &recv_queue
->list
);
2928 spin_unlock_irqrestore(&recv_queue
->lock
, flags
);
2929 ret
= ib_post_recv(qp_info
->qp
, &recv_wr
, &bad_recv_wr
);
2931 spin_lock_irqsave(&recv_queue
->lock
, flags
);
2932 list_del(&mad_priv
->header
.mad_list
.list
);
2933 recv_queue
->count
--;
2934 spin_unlock_irqrestore(&recv_queue
->lock
, flags
);
2935 ib_dma_unmap_single(qp_info
->port_priv
->device
,
2936 mad_priv
->header
.mapping
,
2937 mad_priv_dma_size(mad_priv
),
2940 dev_err(&qp_info
->port_priv
->device
->dev
,
2941 "ib_post_recv failed: %d\n", ret
);
2950 * Return all the posted receive MADs
2952 static void cleanup_recv_queue(struct ib_mad_qp_info
*qp_info
)
2954 struct ib_mad_private_header
*mad_priv_hdr
;
2955 struct ib_mad_private
*recv
;
2956 struct ib_mad_list_head
*mad_list
;
2961 while (!list_empty(&qp_info
->recv_queue
.list
)) {
2963 mad_list
= list_entry(qp_info
->recv_queue
.list
.next
,
2964 struct ib_mad_list_head
, list
);
2965 mad_priv_hdr
= container_of(mad_list
,
2966 struct ib_mad_private_header
,
2968 recv
= container_of(mad_priv_hdr
, struct ib_mad_private
,
2971 /* Remove from posted receive MAD list */
2972 list_del(&mad_list
->list
);
2974 ib_dma_unmap_single(qp_info
->port_priv
->device
,
2975 recv
->header
.mapping
,
2976 mad_priv_dma_size(recv
),
2981 qp_info
->recv_queue
.count
= 0;
2987 static int ib_mad_port_start(struct ib_mad_port_private
*port_priv
)
2990 struct ib_qp_attr
*attr
;
2994 attr
= kmalloc(sizeof *attr
, GFP_KERNEL
);
2996 dev_err(&port_priv
->device
->dev
,
2997 "Couldn't kmalloc ib_qp_attr\n");
3001 ret
= ib_find_pkey(port_priv
->device
, port_priv
->port_num
,
3002 IB_DEFAULT_PKEY_FULL
, &pkey_index
);
3006 for (i
= 0; i
< IB_MAD_QPS_CORE
; i
++) {
3007 qp
= port_priv
->qp_info
[i
].qp
;
3012 * PKey index for QP1 is irrelevant but
3013 * one is needed for the Reset to Init transition
3015 attr
->qp_state
= IB_QPS_INIT
;
3016 attr
->pkey_index
= pkey_index
;
3017 attr
->qkey
= (qp
->qp_num
== 0) ? 0 : IB_QP1_QKEY
;
3018 ret
= ib_modify_qp(qp
, attr
, IB_QP_STATE
|
3019 IB_QP_PKEY_INDEX
| IB_QP_QKEY
);
3021 dev_err(&port_priv
->device
->dev
,
3022 "Couldn't change QP%d state to INIT: %d\n",
3027 attr
->qp_state
= IB_QPS_RTR
;
3028 ret
= ib_modify_qp(qp
, attr
, IB_QP_STATE
);
3030 dev_err(&port_priv
->device
->dev
,
3031 "Couldn't change QP%d state to RTR: %d\n",
3036 attr
->qp_state
= IB_QPS_RTS
;
3037 attr
->sq_psn
= IB_MAD_SEND_Q_PSN
;
3038 ret
= ib_modify_qp(qp
, attr
, IB_QP_STATE
| IB_QP_SQ_PSN
);
3040 dev_err(&port_priv
->device
->dev
,
3041 "Couldn't change QP%d state to RTS: %d\n",
3047 ret
= ib_req_notify_cq(port_priv
->cq
, IB_CQ_NEXT_COMP
);
3049 dev_err(&port_priv
->device
->dev
,
3050 "Failed to request completion notification: %d\n",
3055 for (i
= 0; i
< IB_MAD_QPS_CORE
; i
++) {
3056 if (!port_priv
->qp_info
[i
].qp
)
3059 ret
= ib_mad_post_receive_mads(&port_priv
->qp_info
[i
], NULL
);
3061 dev_err(&port_priv
->device
->dev
,
3062 "Couldn't post receive WRs\n");
3071 static void qp_event_handler(struct ib_event
*event
, void *qp_context
)
3073 struct ib_mad_qp_info
*qp_info
= qp_context
;
3075 /* It's worse than that! He's dead, Jim! */
3076 dev_err(&qp_info
->port_priv
->device
->dev
,
3077 "Fatal error (%d) on MAD QP (%d)\n",
3078 event
->event
, qp_info
->qp
->qp_num
);
3081 static void init_mad_queue(struct ib_mad_qp_info
*qp_info
,
3082 struct ib_mad_queue
*mad_queue
)
3084 mad_queue
->qp_info
= qp_info
;
3085 mad_queue
->count
= 0;
3086 spin_lock_init(&mad_queue
->lock
);
3087 INIT_LIST_HEAD(&mad_queue
->list
);
3090 static void init_mad_qp(struct ib_mad_port_private
*port_priv
,
3091 struct ib_mad_qp_info
*qp_info
)
3093 qp_info
->port_priv
= port_priv
;
3094 init_mad_queue(qp_info
, &qp_info
->send_queue
);
3095 init_mad_queue(qp_info
, &qp_info
->recv_queue
);
3096 INIT_LIST_HEAD(&qp_info
->overflow_list
);
3097 spin_lock_init(&qp_info
->snoop_lock
);
3098 qp_info
->snoop_table
= NULL
;
3099 qp_info
->snoop_table_size
= 0;
3100 atomic_set(&qp_info
->snoop_count
, 0);
3103 static int create_mad_qp(struct ib_mad_qp_info
*qp_info
,
3104 enum ib_qp_type qp_type
)
3106 struct ib_qp_init_attr qp_init_attr
;
3109 memset(&qp_init_attr
, 0, sizeof qp_init_attr
);
3110 qp_init_attr
.send_cq
= qp_info
->port_priv
->cq
;
3111 qp_init_attr
.recv_cq
= qp_info
->port_priv
->cq
;
3112 qp_init_attr
.sq_sig_type
= IB_SIGNAL_ALL_WR
;
3113 qp_init_attr
.cap
.max_send_wr
= mad_sendq_size
;
3114 qp_init_attr
.cap
.max_recv_wr
= mad_recvq_size
;
3115 qp_init_attr
.cap
.max_send_sge
= IB_MAD_SEND_REQ_MAX_SG
;
3116 qp_init_attr
.cap
.max_recv_sge
= IB_MAD_RECV_REQ_MAX_SG
;
3117 qp_init_attr
.qp_type
= qp_type
;
3118 qp_init_attr
.port_num
= qp_info
->port_priv
->port_num
;
3119 qp_init_attr
.qp_context
= qp_info
;
3120 qp_init_attr
.event_handler
= qp_event_handler
;
3121 qp_info
->qp
= ib_create_qp(qp_info
->port_priv
->pd
, &qp_init_attr
);
3122 if (IS_ERR(qp_info
->qp
)) {
3123 dev_err(&qp_info
->port_priv
->device
->dev
,
3124 "Couldn't create ib_mad QP%d\n",
3125 get_spl_qp_index(qp_type
));
3126 ret
= PTR_ERR(qp_info
->qp
);
3129 /* Use minimum queue sizes unless the CQ is resized */
3130 qp_info
->send_queue
.max_active
= mad_sendq_size
;
3131 qp_info
->recv_queue
.max_active
= mad_recvq_size
;
3138 static void destroy_mad_qp(struct ib_mad_qp_info
*qp_info
)
3143 ib_destroy_qp(qp_info
->qp
);
3144 kfree(qp_info
->snoop_table
);
3149 * Create the QP, PD, MR, and CQ if needed
3151 static int ib_mad_port_open(struct ib_device
*device
,
3155 struct ib_mad_port_private
*port_priv
;
3156 unsigned long flags
;
3157 char name
[sizeof "ib_mad123"];
3159 struct ib_cq_init_attr cq_attr
= {};
3161 if (WARN_ON(rdma_max_mad_size(device
, port_num
) < IB_MGMT_MAD_SIZE
))
3164 if (WARN_ON(rdma_cap_opa_mad(device
, port_num
) &&
3165 rdma_max_mad_size(device
, port_num
) < OPA_MGMT_MAD_SIZE
))
3168 /* Create new device info */
3169 port_priv
= kzalloc(sizeof *port_priv
, GFP_KERNEL
);
3171 dev_err(&device
->dev
, "No memory for ib_mad_port_private\n");
3175 port_priv
->device
= device
;
3176 port_priv
->port_num
= port_num
;
3177 spin_lock_init(&port_priv
->reg_lock
);
3178 INIT_LIST_HEAD(&port_priv
->agent_list
);
3179 init_mad_qp(port_priv
, &port_priv
->qp_info
[0]);
3180 init_mad_qp(port_priv
, &port_priv
->qp_info
[1]);
3182 cq_size
= mad_sendq_size
+ mad_recvq_size
;
3183 has_smi
= rdma_cap_ib_smi(device
, port_num
);
3187 cq_attr
.cqe
= cq_size
;
3188 port_priv
->cq
= ib_create_cq(port_priv
->device
,
3189 ib_mad_thread_completion_handler
,
3190 NULL
, port_priv
, &cq_attr
);
3191 if (IS_ERR(port_priv
->cq
)) {
3192 dev_err(&device
->dev
, "Couldn't create ib_mad CQ\n");
3193 ret
= PTR_ERR(port_priv
->cq
);
3197 port_priv
->pd
= ib_alloc_pd(device
);
3198 if (IS_ERR(port_priv
->pd
)) {
3199 dev_err(&device
->dev
, "Couldn't create ib_mad PD\n");
3200 ret
= PTR_ERR(port_priv
->pd
);
3204 port_priv
->mr
= ib_get_dma_mr(port_priv
->pd
, IB_ACCESS_LOCAL_WRITE
);
3205 if (IS_ERR(port_priv
->mr
)) {
3206 dev_err(&device
->dev
, "Couldn't get ib_mad DMA MR\n");
3207 ret
= PTR_ERR(port_priv
->mr
);
3212 ret
= create_mad_qp(&port_priv
->qp_info
[0], IB_QPT_SMI
);
3216 ret
= create_mad_qp(&port_priv
->qp_info
[1], IB_QPT_GSI
);
3220 snprintf(name
, sizeof name
, "ib_mad%d", port_num
);
3221 port_priv
->wq
= create_singlethread_workqueue(name
);
3222 if (!port_priv
->wq
) {
3226 INIT_WORK(&port_priv
->work
, ib_mad_completion_handler
);
3228 spin_lock_irqsave(&ib_mad_port_list_lock
, flags
);
3229 list_add_tail(&port_priv
->port_list
, &ib_mad_port_list
);
3230 spin_unlock_irqrestore(&ib_mad_port_list_lock
, flags
);
3232 ret
= ib_mad_port_start(port_priv
);
3234 dev_err(&device
->dev
, "Couldn't start port\n");
3241 spin_lock_irqsave(&ib_mad_port_list_lock
, flags
);
3242 list_del_init(&port_priv
->port_list
);
3243 spin_unlock_irqrestore(&ib_mad_port_list_lock
, flags
);
3245 destroy_workqueue(port_priv
->wq
);
3247 destroy_mad_qp(&port_priv
->qp_info
[1]);
3249 destroy_mad_qp(&port_priv
->qp_info
[0]);
3251 ib_dereg_mr(port_priv
->mr
);
3253 ib_dealloc_pd(port_priv
->pd
);
3255 ib_destroy_cq(port_priv
->cq
);
3256 cleanup_recv_queue(&port_priv
->qp_info
[1]);
3257 cleanup_recv_queue(&port_priv
->qp_info
[0]);
3266 * If there are no classes using the port, free the port
3267 * resources (CQ, MR, PD, QP) and remove the port's info structure
3269 static int ib_mad_port_close(struct ib_device
*device
, int port_num
)
3271 struct ib_mad_port_private
*port_priv
;
3272 unsigned long flags
;
3274 spin_lock_irqsave(&ib_mad_port_list_lock
, flags
);
3275 port_priv
= __ib_get_mad_port(device
, port_num
);
3276 if (port_priv
== NULL
) {
3277 spin_unlock_irqrestore(&ib_mad_port_list_lock
, flags
);
3278 dev_err(&device
->dev
, "Port %d not found\n", port_num
);
3281 list_del_init(&port_priv
->port_list
);
3282 spin_unlock_irqrestore(&ib_mad_port_list_lock
, flags
);
3284 destroy_workqueue(port_priv
->wq
);
3285 destroy_mad_qp(&port_priv
->qp_info
[1]);
3286 destroy_mad_qp(&port_priv
->qp_info
[0]);
3287 ib_dereg_mr(port_priv
->mr
);
3288 ib_dealloc_pd(port_priv
->pd
);
3289 ib_destroy_cq(port_priv
->cq
);
3290 cleanup_recv_queue(&port_priv
->qp_info
[1]);
3291 cleanup_recv_queue(&port_priv
->qp_info
[0]);
3292 /* XXX: Handle deallocation of MAD registration tables */
3299 static void ib_mad_init_device(struct ib_device
*device
)
3303 start
= rdma_start_port(device
);
3305 for (i
= start
; i
<= rdma_end_port(device
); i
++) {
3306 if (!rdma_cap_ib_mad(device
, i
))
3309 if (ib_mad_port_open(device
, i
)) {
3310 dev_err(&device
->dev
, "Couldn't open port %d\n", i
);
3313 if (ib_agent_port_open(device
, i
)) {
3314 dev_err(&device
->dev
,
3315 "Couldn't open port %d for agents\n", i
);
3322 if (ib_mad_port_close(device
, i
))
3323 dev_err(&device
->dev
, "Couldn't close port %d\n", i
);
3326 while (--i
>= start
) {
3327 if (!rdma_cap_ib_mad(device
, i
))
3330 if (ib_agent_port_close(device
, i
))
3331 dev_err(&device
->dev
,
3332 "Couldn't close port %d for agents\n", i
);
3333 if (ib_mad_port_close(device
, i
))
3334 dev_err(&device
->dev
, "Couldn't close port %d\n", i
);
3338 static void ib_mad_remove_device(struct ib_device
*device
)
3342 for (i
= rdma_start_port(device
); i
<= rdma_end_port(device
); i
++) {
3343 if (!rdma_cap_ib_mad(device
, i
))
3346 if (ib_agent_port_close(device
, i
))
3347 dev_err(&device
->dev
,
3348 "Couldn't close port %d for agents\n", i
);
3349 if (ib_mad_port_close(device
, i
))
3350 dev_err(&device
->dev
, "Couldn't close port %d\n", i
);
3354 static struct ib_client mad_client
= {
3356 .add
= ib_mad_init_device
,
3357 .remove
= ib_mad_remove_device
3360 static int __init
ib_mad_init_module(void)
3362 mad_recvq_size
= min(mad_recvq_size
, IB_MAD_QP_MAX_SIZE
);
3363 mad_recvq_size
= max(mad_recvq_size
, IB_MAD_QP_MIN_SIZE
);
3365 mad_sendq_size
= min(mad_sendq_size
, IB_MAD_QP_MAX_SIZE
);
3366 mad_sendq_size
= max(mad_sendq_size
, IB_MAD_QP_MIN_SIZE
);
3368 INIT_LIST_HEAD(&ib_mad_port_list
);
3370 if (ib_register_client(&mad_client
)) {
3371 pr_err("Couldn't register ib_mad client\n");
3378 static void __exit
ib_mad_cleanup_module(void)
3380 ib_unregister_client(&mad_client
);
3383 module_init(ib_mad_init_module
);
3384 module_exit(ib_mad_cleanup_module
);