2 * Copyright (c) 2004-2007 Voltaire, Inc. All rights reserved.
3 * Copyright (c) 2005 Intel Corporation. All rights reserved.
4 * Copyright (c) 2005 Mellanox Technologies Ltd. All rights reserved.
5 * Copyright (c) 2009 HNR Consulting. All rights reserved.
6 * Copyright (c) 2014,2018 Intel Corporation. All rights reserved.
8 * This software is available to you under a choice of one of two
9 * licenses. You may choose to be licensed under the terms of the GNU
10 * General Public License (GPL) Version 2, available from the file
11 * COPYING in the main directory of this source tree, or the
12 * OpenIB.org BSD license below:
14 * Redistribution and use in source and binary forms, with or
15 * without modification, are permitted provided that the following
18 * - Redistributions of source code must retain the above
19 * copyright notice, this list of conditions and the following
22 * - Redistributions in binary form must reproduce the above
23 * copyright notice, this list of conditions and the following
24 * disclaimer in the documentation and/or other materials
25 * provided with the distribution.
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
31 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
32 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
33 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40 #include <linux/dma-mapping.h>
41 #include <linux/slab.h>
42 #include <linux/module.h>
43 #include <linux/security.h>
44 #include <linux/xarray.h>
45 #include <rdma/ib_cache.h>
48 #include "core_priv.h"
54 #define CREATE_TRACE_POINTS
55 #include <trace/events/ib_mad.h>
57 #ifdef CONFIG_TRACEPOINTS
58 static void create_mad_addr_info(struct ib_mad_send_wr_private *mad_send_wr,
59 struct ib_mad_qp_info *qp_info,
60 struct trace_event_raw_ib_mad_send_template *entry)
63 struct ib_device *dev = qp_info->port_priv->device;
64 u32 pnum = qp_info->port_priv->port_num;
65 struct ib_ud_wr *wr = &mad_send_wr->send_wr;
66 struct rdma_ah_attr attr = {};
68 rdma_query_ah(wr->ah, &attr);
70 /* These are common */
72 ib_query_pkey(dev, pnum, wr->pkey_index, &pkey);
74 entry->rqpn = wr->remote_qpn;
75 entry->rqkey = wr->remote_qkey;
76 entry->dlid = rdma_ah_get_dlid(&attr);
80 static int mad_sendq_size = IB_MAD_QP_SEND_SIZE;
81 static int mad_recvq_size = IB_MAD_QP_RECV_SIZE;
83 module_param_named(send_queue_size, mad_sendq_size, int, 0444);
84 MODULE_PARM_DESC(send_queue_size, "Size of send queue in number of work requests");
85 module_param_named(recv_queue_size, mad_recvq_size, int, 0444);
86 MODULE_PARM_DESC(recv_queue_size, "Size of receive queue in number of work requests");
88 static DEFINE_XARRAY_ALLOC1(ib_mad_clients);
89 static u32 ib_mad_client_next;
90 static struct list_head ib_mad_port_list;
93 static DEFINE_SPINLOCK(ib_mad_port_list_lock);
95 /* Forward declarations */
96 static int method_in_use(struct ib_mad_mgmt_method_table **method,
97 struct ib_mad_reg_req *mad_reg_req);
98 static void remove_mad_reg_req(struct ib_mad_agent_private *priv);
99 static struct ib_mad_agent_private *find_mad_agent(
100 struct ib_mad_port_private *port_priv,
101 const struct ib_mad_hdr *mad);
102 static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
103 struct ib_mad_private *mad);
104 static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv);
105 static void timeout_sends(struct work_struct *work);
106 static void local_completions(struct work_struct *work);
107 static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
108 struct ib_mad_agent_private *agent_priv,
110 static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
111 struct ib_mad_agent_private *agent_priv);
112 static bool ib_mad_send_error(struct ib_mad_port_private *port_priv,
114 static void ib_mad_send_done(struct ib_cq *cq, struct ib_wc *wc);
117 * Returns a ib_mad_port_private structure or NULL for a device/port
118 * Assumes ib_mad_port_list_lock is being held
120 static inline struct ib_mad_port_private *
121 __ib_get_mad_port(struct ib_device *device, u32 port_num)
123 struct ib_mad_port_private *entry;
125 list_for_each_entry(entry, &ib_mad_port_list, port_list) {
126 if (entry->device == device && entry->port_num == port_num)
133 * Wrapper function to return a ib_mad_port_private structure or NULL
136 static inline struct ib_mad_port_private *
137 ib_get_mad_port(struct ib_device *device, u32 port_num)
139 struct ib_mad_port_private *entry;
142 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
143 entry = __ib_get_mad_port(device, port_num);
144 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
149 static inline u8 convert_mgmt_class(u8 mgmt_class)
151 /* Alias IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE to 0 */
152 return mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE ?
156 static int get_spl_qp_index(enum ib_qp_type qp_type)
168 static int vendor_class_index(u8 mgmt_class)
170 return mgmt_class - IB_MGMT_CLASS_VENDOR_RANGE2_START;
173 static int is_vendor_class(u8 mgmt_class)
175 if ((mgmt_class < IB_MGMT_CLASS_VENDOR_RANGE2_START) ||
176 (mgmt_class > IB_MGMT_CLASS_VENDOR_RANGE2_END))
181 static int is_vendor_oui(char *oui)
183 if (oui[0] || oui[1] || oui[2])
188 static int is_vendor_method_in_use(
189 struct ib_mad_mgmt_vendor_class *vendor_class,
190 struct ib_mad_reg_req *mad_reg_req)
192 struct ib_mad_mgmt_method_table *method;
195 for (i = 0; i < MAX_MGMT_OUI; i++) {
196 if (!memcmp(vendor_class->oui[i], mad_reg_req->oui, 3)) {
197 method = vendor_class->method_table[i];
199 if (method_in_use(&method, mad_reg_req))
209 int ib_response_mad(const struct ib_mad_hdr *hdr)
211 return ((hdr->method & IB_MGMT_METHOD_RESP) ||
212 (hdr->method == IB_MGMT_METHOD_TRAP_REPRESS) ||
213 ((hdr->mgmt_class == IB_MGMT_CLASS_BM) &&
214 (hdr->attr_mod & IB_BM_ATTR_MOD_RESP)));
216 EXPORT_SYMBOL(ib_response_mad);
219 * ib_register_mad_agent - Register to send/receive MADs
221 * Context: Process context.
223 struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
225 enum ib_qp_type qp_type,
226 struct ib_mad_reg_req *mad_reg_req,
228 ib_mad_send_handler send_handler,
229 ib_mad_recv_handler recv_handler,
231 u32 registration_flags)
233 struct ib_mad_port_private *port_priv;
234 struct ib_mad_agent *ret = ERR_PTR(-EINVAL);
235 struct ib_mad_agent_private *mad_agent_priv;
236 struct ib_mad_reg_req *reg_req = NULL;
237 struct ib_mad_mgmt_class_table *class;
238 struct ib_mad_mgmt_vendor_class_table *vendor;
239 struct ib_mad_mgmt_vendor_class *vendor_class;
240 struct ib_mad_mgmt_method_table *method;
242 u8 mgmt_class, vclass;
244 if ((qp_type == IB_QPT_SMI && !rdma_cap_ib_smi(device, port_num)) ||
245 (qp_type == IB_QPT_GSI && !rdma_cap_ib_cm(device, port_num)))
246 return ERR_PTR(-EPROTONOSUPPORT);
248 /* Validate parameters */
249 qpn = get_spl_qp_index(qp_type);
251 dev_dbg_ratelimited(&device->dev, "%s: invalid QP Type %d\n",
256 if (rmpp_version && rmpp_version != IB_MGMT_RMPP_VERSION) {
257 dev_dbg_ratelimited(&device->dev,
258 "%s: invalid RMPP Version %u\n",
259 __func__, rmpp_version);
263 /* Validate MAD registration request if supplied */
265 if (mad_reg_req->mgmt_class_version >= MAX_MGMT_VERSION) {
266 dev_dbg_ratelimited(&device->dev,
267 "%s: invalid Class Version %u\n",
269 mad_reg_req->mgmt_class_version);
273 dev_dbg_ratelimited(&device->dev,
274 "%s: no recv_handler\n", __func__);
277 if (mad_reg_req->mgmt_class >= MAX_MGMT_CLASS) {
279 * IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE is the only
280 * one in this range currently allowed
282 if (mad_reg_req->mgmt_class !=
283 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
284 dev_dbg_ratelimited(&device->dev,
285 "%s: Invalid Mgmt Class 0x%x\n",
286 __func__, mad_reg_req->mgmt_class);
289 } else if (mad_reg_req->mgmt_class == 0) {
291 * Class 0 is reserved in IBA and is used for
292 * aliasing of IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
294 dev_dbg_ratelimited(&device->dev,
295 "%s: Invalid Mgmt Class 0\n",
298 } else if (is_vendor_class(mad_reg_req->mgmt_class)) {
300 * If class is in "new" vendor range,
301 * ensure supplied OUI is not zero
303 if (!is_vendor_oui(mad_reg_req->oui)) {
304 dev_dbg_ratelimited(&device->dev,
305 "%s: No OUI specified for class 0x%x\n",
307 mad_reg_req->mgmt_class);
311 /* Make sure class supplied is consistent with RMPP */
312 if (!ib_is_mad_class_rmpp(mad_reg_req->mgmt_class)) {
314 dev_dbg_ratelimited(&device->dev,
315 "%s: RMPP version for non-RMPP class 0x%x\n",
316 __func__, mad_reg_req->mgmt_class);
321 /* Make sure class supplied is consistent with QP type */
322 if (qp_type == IB_QPT_SMI) {
323 if ((mad_reg_req->mgmt_class !=
324 IB_MGMT_CLASS_SUBN_LID_ROUTED) &&
325 (mad_reg_req->mgmt_class !=
326 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) {
327 dev_dbg_ratelimited(&device->dev,
328 "%s: Invalid SM QP type: class 0x%x\n",
329 __func__, mad_reg_req->mgmt_class);
333 if ((mad_reg_req->mgmt_class ==
334 IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
335 (mad_reg_req->mgmt_class ==
336 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) {
337 dev_dbg_ratelimited(&device->dev,
338 "%s: Invalid GS QP type: class 0x%x\n",
339 __func__, mad_reg_req->mgmt_class);
344 /* No registration request supplied */
347 if (registration_flags & IB_MAD_USER_RMPP)
351 /* Validate device and port */
352 port_priv = ib_get_mad_port(device, port_num);
354 dev_dbg_ratelimited(&device->dev, "%s: Invalid port %d\n",
356 ret = ERR_PTR(-ENODEV);
360 /* Verify the QP requested is supported. For example, Ethernet devices
363 if (!port_priv->qp_info[qpn].qp) {
364 dev_dbg_ratelimited(&device->dev, "%s: QP %d not supported\n",
366 ret = ERR_PTR(-EPROTONOSUPPORT);
370 /* Allocate structures */
371 mad_agent_priv = kzalloc(sizeof *mad_agent_priv, GFP_KERNEL);
372 if (!mad_agent_priv) {
373 ret = ERR_PTR(-ENOMEM);
378 reg_req = kmemdup(mad_reg_req, sizeof *reg_req, GFP_KERNEL);
380 ret = ERR_PTR(-ENOMEM);
385 /* Now, fill in the various structures */
386 mad_agent_priv->qp_info = &port_priv->qp_info[qpn];
387 mad_agent_priv->reg_req = reg_req;
388 mad_agent_priv->agent.rmpp_version = rmpp_version;
389 mad_agent_priv->agent.device = device;
390 mad_agent_priv->agent.recv_handler = recv_handler;
391 mad_agent_priv->agent.send_handler = send_handler;
392 mad_agent_priv->agent.context = context;
393 mad_agent_priv->agent.qp = port_priv->qp_info[qpn].qp;
394 mad_agent_priv->agent.port_num = port_num;
395 mad_agent_priv->agent.flags = registration_flags;
396 spin_lock_init(&mad_agent_priv->lock);
397 INIT_LIST_HEAD(&mad_agent_priv->send_list);
398 INIT_LIST_HEAD(&mad_agent_priv->wait_list);
399 INIT_LIST_HEAD(&mad_agent_priv->done_list);
400 INIT_LIST_HEAD(&mad_agent_priv->rmpp_list);
401 INIT_DELAYED_WORK(&mad_agent_priv->timed_work, timeout_sends);
402 INIT_LIST_HEAD(&mad_agent_priv->local_list);
403 INIT_WORK(&mad_agent_priv->local_work, local_completions);
404 refcount_set(&mad_agent_priv->refcount, 1);
405 init_completion(&mad_agent_priv->comp);
407 ret2 = ib_mad_agent_security_setup(&mad_agent_priv->agent, qp_type);
414 * The mlx4 driver uses the top byte to distinguish which virtual
415 * function generated the MAD, so we must avoid using it.
417 ret2 = xa_alloc_cyclic(&ib_mad_clients, &mad_agent_priv->agent.hi_tid,
418 mad_agent_priv, XA_LIMIT(0, (1 << 24) - 1),
419 &ib_mad_client_next, GFP_KERNEL);
426 * Make sure MAD registration (if supplied)
427 * is non overlapping with any existing ones
429 spin_lock_irq(&port_priv->reg_lock);
431 mgmt_class = convert_mgmt_class(mad_reg_req->mgmt_class);
432 if (!is_vendor_class(mgmt_class)) {
433 class = port_priv->version[mad_reg_req->
434 mgmt_class_version].class;
436 method = class->method_table[mgmt_class];
438 if (method_in_use(&method,
443 ret2 = add_nonoui_reg_req(mad_reg_req, mad_agent_priv,
446 /* "New" vendor class range */
447 vendor = port_priv->version[mad_reg_req->
448 mgmt_class_version].vendor;
450 vclass = vendor_class_index(mgmt_class);
451 vendor_class = vendor->vendor_class[vclass];
453 if (is_vendor_method_in_use(
459 ret2 = add_oui_reg_req(mad_reg_req, mad_agent_priv);
466 spin_unlock_irq(&port_priv->reg_lock);
468 trace_ib_mad_create_agent(mad_agent_priv);
469 return &mad_agent_priv->agent;
471 spin_unlock_irq(&port_priv->reg_lock);
472 xa_erase(&ib_mad_clients, mad_agent_priv->agent.hi_tid);
474 ib_mad_agent_security_cleanup(&mad_agent_priv->agent);
478 kfree(mad_agent_priv);
482 EXPORT_SYMBOL(ib_register_mad_agent);
484 static inline void deref_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
486 if (refcount_dec_and_test(&mad_agent_priv->refcount))
487 complete(&mad_agent_priv->comp);
490 static void unregister_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
492 struct ib_mad_port_private *port_priv;
494 /* Note that we could still be handling received MADs */
495 trace_ib_mad_unregister_agent(mad_agent_priv);
498 * Canceling all sends results in dropping received response
499 * MADs, preventing us from queuing additional work
501 cancel_mads(mad_agent_priv);
502 port_priv = mad_agent_priv->qp_info->port_priv;
503 cancel_delayed_work(&mad_agent_priv->timed_work);
505 spin_lock_irq(&port_priv->reg_lock);
506 remove_mad_reg_req(mad_agent_priv);
507 spin_unlock_irq(&port_priv->reg_lock);
508 xa_erase(&ib_mad_clients, mad_agent_priv->agent.hi_tid);
510 flush_workqueue(port_priv->wq);
512 deref_mad_agent(mad_agent_priv);
513 wait_for_completion(&mad_agent_priv->comp);
514 ib_cancel_rmpp_recvs(mad_agent_priv);
516 ib_mad_agent_security_cleanup(&mad_agent_priv->agent);
518 kfree(mad_agent_priv->reg_req);
519 kfree_rcu(mad_agent_priv, rcu);
523 * ib_unregister_mad_agent - Unregisters a client from using MAD services
525 * Context: Process context.
527 void ib_unregister_mad_agent(struct ib_mad_agent *mad_agent)
529 struct ib_mad_agent_private *mad_agent_priv;
531 mad_agent_priv = container_of(mad_agent,
532 struct ib_mad_agent_private,
534 unregister_mad_agent(mad_agent_priv);
536 EXPORT_SYMBOL(ib_unregister_mad_agent);
538 static void dequeue_mad(struct ib_mad_list_head *mad_list)
540 struct ib_mad_queue *mad_queue;
543 mad_queue = mad_list->mad_queue;
544 spin_lock_irqsave(&mad_queue->lock, flags);
545 list_del(&mad_list->list);
547 spin_unlock_irqrestore(&mad_queue->lock, flags);
550 static void build_smp_wc(struct ib_qp *qp, struct ib_cqe *cqe, u16 slid,
551 u16 pkey_index, u32 port_num, struct ib_wc *wc)
553 memset(wc, 0, sizeof *wc);
555 wc->status = IB_WC_SUCCESS;
556 wc->opcode = IB_WC_RECV;
557 wc->pkey_index = pkey_index;
558 wc->byte_len = sizeof(struct ib_mad) + sizeof(struct ib_grh);
563 wc->dlid_path_bits = 0;
564 wc->port_num = port_num;
567 static size_t mad_priv_size(const struct ib_mad_private *mp)
569 return sizeof(struct ib_mad_private) + mp->mad_size;
572 static struct ib_mad_private *alloc_mad_private(size_t mad_size, gfp_t flags)
574 size_t size = sizeof(struct ib_mad_private) + mad_size;
575 struct ib_mad_private *ret = kzalloc(size, flags);
578 ret->mad_size = mad_size;
583 static size_t port_mad_size(const struct ib_mad_port_private *port_priv)
585 return rdma_max_mad_size(port_priv->device, port_priv->port_num);
588 static size_t mad_priv_dma_size(const struct ib_mad_private *mp)
590 return sizeof(struct ib_grh) + mp->mad_size;
594 * Return 0 if SMP is to be sent
595 * Return 1 if SMP was consumed locally (whether or not solicited)
596 * Return < 0 if error
598 static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
599 struct ib_mad_send_wr_private *mad_send_wr)
602 struct ib_smp *smp = mad_send_wr->send_buf.mad;
603 struct opa_smp *opa_smp = (struct opa_smp *)smp;
605 struct ib_mad_local_private *local;
606 struct ib_mad_private *mad_priv;
607 struct ib_mad_port_private *port_priv;
608 struct ib_mad_agent_private *recv_mad_agent = NULL;
609 struct ib_device *device = mad_agent_priv->agent.device;
612 struct ib_ud_wr *send_wr = &mad_send_wr->send_wr;
613 size_t mad_size = port_mad_size(mad_agent_priv->qp_info->port_priv);
614 u16 out_mad_pkey_index = 0;
616 bool opa = rdma_cap_opa_mad(mad_agent_priv->qp_info->port_priv->device,
617 mad_agent_priv->qp_info->port_priv->port_num);
619 if (rdma_cap_ib_switch(device) &&
620 smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
621 port_num = send_wr->port_num;
623 port_num = mad_agent_priv->agent.port_num;
626 * Directed route handling starts if the initial LID routed part of
627 * a request or the ending LID routed part of a response is empty.
628 * If we are at the start of the LID routed part, don't update the
629 * hop_ptr or hop_cnt. See section 14.2.2, Vol 1 IB spec.
631 if (opa && smp->class_version == OPA_SM_CLASS_VERSION) {
634 trace_ib_mad_handle_out_opa_smi(opa_smp);
636 if ((opa_get_smp_direction(opa_smp)
637 ? opa_smp->route.dr.dr_dlid : opa_smp->route.dr.dr_slid) ==
638 OPA_LID_PERMISSIVE &&
639 opa_smi_handle_dr_smp_send(opa_smp,
640 rdma_cap_ib_switch(device),
641 port_num) == IB_SMI_DISCARD) {
643 dev_err(&device->dev, "OPA Invalid directed route\n");
646 opa_drslid = be32_to_cpu(opa_smp->route.dr.dr_slid);
647 if (opa_drslid != be32_to_cpu(OPA_LID_PERMISSIVE) &&
648 opa_drslid & 0xffff0000) {
650 dev_err(&device->dev, "OPA Invalid dr_slid 0x%x\n",
654 drslid = (u16)(opa_drslid & 0x0000ffff);
656 /* Check to post send on QP or process locally */
657 if (opa_smi_check_local_smp(opa_smp, device) == IB_SMI_DISCARD &&
658 opa_smi_check_local_returning_smp(opa_smp, device) == IB_SMI_DISCARD)
661 trace_ib_mad_handle_out_ib_smi(smp);
663 if ((ib_get_smp_direction(smp) ? smp->dr_dlid : smp->dr_slid) ==
665 smi_handle_dr_smp_send(smp, rdma_cap_ib_switch(device), port_num) ==
668 dev_err(&device->dev, "Invalid directed route\n");
671 drslid = be16_to_cpu(smp->dr_slid);
673 /* Check to post send on QP or process locally */
674 if (smi_check_local_smp(smp, device) == IB_SMI_DISCARD &&
675 smi_check_local_returning_smp(smp, device) == IB_SMI_DISCARD)
679 local = kmalloc(sizeof *local, GFP_ATOMIC);
684 local->mad_priv = NULL;
685 local->recv_mad_agent = NULL;
686 mad_priv = alloc_mad_private(mad_size, GFP_ATOMIC);
693 build_smp_wc(mad_agent_priv->agent.qp,
694 send_wr->wr.wr_cqe, drslid,
696 send_wr->port_num, &mad_wc);
698 if (opa && smp->base_version == OPA_MGMT_BASE_VERSION) {
699 mad_wc.byte_len = mad_send_wr->send_buf.hdr_len
700 + mad_send_wr->send_buf.data_len
701 + sizeof(struct ib_grh);
704 /* No GRH for DR SMP */
705 ret = device->ops.process_mad(device, 0, port_num, &mad_wc, NULL,
706 (const struct ib_mad *)smp,
707 (struct ib_mad *)mad_priv->mad, &mad_size,
708 &out_mad_pkey_index);
710 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY:
711 if (ib_response_mad((const struct ib_mad_hdr *)mad_priv->mad) &&
712 mad_agent_priv->agent.recv_handler) {
713 local->mad_priv = mad_priv;
714 local->recv_mad_agent = mad_agent_priv;
716 * Reference MAD agent until receive
717 * side of local completion handled
719 refcount_inc(&mad_agent_priv->refcount);
723 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED:
726 case IB_MAD_RESULT_SUCCESS:
727 /* Treat like an incoming receive MAD */
728 port_priv = ib_get_mad_port(mad_agent_priv->agent.device,
729 mad_agent_priv->agent.port_num);
731 memcpy(mad_priv->mad, smp, mad_priv->mad_size);
732 recv_mad_agent = find_mad_agent(port_priv,
733 (const struct ib_mad_hdr *)mad_priv->mad);
735 if (!port_priv || !recv_mad_agent) {
737 * No receiving agent so drop packet and
738 * generate send completion.
743 local->mad_priv = mad_priv;
744 local->recv_mad_agent = recv_mad_agent;
753 local->mad_send_wr = mad_send_wr;
755 local->mad_send_wr->send_wr.pkey_index = out_mad_pkey_index;
756 local->return_wc_byte_len = mad_size;
758 /* Reference MAD agent until send side of local completion handled */
759 refcount_inc(&mad_agent_priv->refcount);
760 /* Queue local completion to local list */
761 spin_lock_irqsave(&mad_agent_priv->lock, flags);
762 list_add_tail(&local->completion_list, &mad_agent_priv->local_list);
763 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
764 queue_work(mad_agent_priv->qp_info->port_priv->wq,
765 &mad_agent_priv->local_work);
771 static int get_pad_size(int hdr_len, int data_len, size_t mad_size)
775 seg_size = mad_size - hdr_len;
776 if (data_len && seg_size) {
777 pad = seg_size - data_len % seg_size;
778 return pad == seg_size ? 0 : pad;
783 static void free_send_rmpp_list(struct ib_mad_send_wr_private *mad_send_wr)
785 struct ib_rmpp_segment *s, *t;
787 list_for_each_entry_safe(s, t, &mad_send_wr->rmpp_list, list) {
793 static int alloc_send_rmpp_list(struct ib_mad_send_wr_private *send_wr,
794 size_t mad_size, gfp_t gfp_mask)
796 struct ib_mad_send_buf *send_buf = &send_wr->send_buf;
797 struct ib_rmpp_mad *rmpp_mad = send_buf->mad;
798 struct ib_rmpp_segment *seg = NULL;
799 int left, seg_size, pad;
801 send_buf->seg_size = mad_size - send_buf->hdr_len;
802 send_buf->seg_rmpp_size = mad_size - IB_MGMT_RMPP_HDR;
803 seg_size = send_buf->seg_size;
806 /* Allocate data segments. */
807 for (left = send_buf->data_len + pad; left > 0; left -= seg_size) {
808 seg = kmalloc(sizeof(*seg) + seg_size, gfp_mask);
810 free_send_rmpp_list(send_wr);
813 seg->num = ++send_buf->seg_count;
814 list_add_tail(&seg->list, &send_wr->rmpp_list);
817 /* Zero any padding */
819 memset(seg->data + seg_size - pad, 0, pad);
821 rmpp_mad->rmpp_hdr.rmpp_version = send_wr->mad_agent_priv->
823 rmpp_mad->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_DATA;
824 ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
826 send_wr->cur_seg = container_of(send_wr->rmpp_list.next,
827 struct ib_rmpp_segment, list);
828 send_wr->last_ack_seg = send_wr->cur_seg;
832 int ib_mad_kernel_rmpp_agent(const struct ib_mad_agent *agent)
834 return agent->rmpp_version && !(agent->flags & IB_MAD_USER_RMPP);
836 EXPORT_SYMBOL(ib_mad_kernel_rmpp_agent);
838 struct ib_mad_send_buf *ib_create_send_mad(struct ib_mad_agent *mad_agent,
839 u32 remote_qpn, u16 pkey_index,
840 int rmpp_active, int hdr_len,
841 int data_len, gfp_t gfp_mask,
844 struct ib_mad_agent_private *mad_agent_priv;
845 struct ib_mad_send_wr_private *mad_send_wr;
846 int pad, message_size, ret, size;
851 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
854 opa = rdma_cap_opa_mad(mad_agent->device, mad_agent->port_num);
856 if (opa && base_version == OPA_MGMT_BASE_VERSION)
857 mad_size = sizeof(struct opa_mad);
859 mad_size = sizeof(struct ib_mad);
861 pad = get_pad_size(hdr_len, data_len, mad_size);
862 message_size = hdr_len + data_len + pad;
864 if (ib_mad_kernel_rmpp_agent(mad_agent)) {
865 if (!rmpp_active && message_size > mad_size)
866 return ERR_PTR(-EINVAL);
868 if (rmpp_active || message_size > mad_size)
869 return ERR_PTR(-EINVAL);
871 size = rmpp_active ? hdr_len : mad_size;
872 buf = kzalloc(sizeof *mad_send_wr + size, gfp_mask);
874 return ERR_PTR(-ENOMEM);
876 mad_send_wr = buf + size;
877 INIT_LIST_HEAD(&mad_send_wr->rmpp_list);
878 mad_send_wr->send_buf.mad = buf;
879 mad_send_wr->send_buf.hdr_len = hdr_len;
880 mad_send_wr->send_buf.data_len = data_len;
881 mad_send_wr->pad = pad;
883 mad_send_wr->mad_agent_priv = mad_agent_priv;
884 mad_send_wr->sg_list[0].length = hdr_len;
885 mad_send_wr->sg_list[0].lkey = mad_agent->qp->pd->local_dma_lkey;
887 /* OPA MADs don't have to be the full 2048 bytes */
888 if (opa && base_version == OPA_MGMT_BASE_VERSION &&
889 data_len < mad_size - hdr_len)
890 mad_send_wr->sg_list[1].length = data_len;
892 mad_send_wr->sg_list[1].length = mad_size - hdr_len;
894 mad_send_wr->sg_list[1].lkey = mad_agent->qp->pd->local_dma_lkey;
896 mad_send_wr->mad_list.cqe.done = ib_mad_send_done;
898 mad_send_wr->send_wr.wr.wr_cqe = &mad_send_wr->mad_list.cqe;
899 mad_send_wr->send_wr.wr.sg_list = mad_send_wr->sg_list;
900 mad_send_wr->send_wr.wr.num_sge = 2;
901 mad_send_wr->send_wr.wr.opcode = IB_WR_SEND;
902 mad_send_wr->send_wr.wr.send_flags = IB_SEND_SIGNALED;
903 mad_send_wr->send_wr.remote_qpn = remote_qpn;
904 mad_send_wr->send_wr.remote_qkey = IB_QP_SET_QKEY;
905 mad_send_wr->send_wr.pkey_index = pkey_index;
908 ret = alloc_send_rmpp_list(mad_send_wr, mad_size, gfp_mask);
915 mad_send_wr->send_buf.mad_agent = mad_agent;
916 refcount_inc(&mad_agent_priv->refcount);
917 return &mad_send_wr->send_buf;
919 EXPORT_SYMBOL(ib_create_send_mad);
921 int ib_get_mad_data_offset(u8 mgmt_class)
923 if (mgmt_class == IB_MGMT_CLASS_SUBN_ADM)
924 return IB_MGMT_SA_HDR;
925 else if ((mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
926 (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
927 (mgmt_class == IB_MGMT_CLASS_BIS))
928 return IB_MGMT_DEVICE_HDR;
929 else if ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
930 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END))
931 return IB_MGMT_VENDOR_HDR;
933 return IB_MGMT_MAD_HDR;
935 EXPORT_SYMBOL(ib_get_mad_data_offset);
937 int ib_is_mad_class_rmpp(u8 mgmt_class)
939 if ((mgmt_class == IB_MGMT_CLASS_SUBN_ADM) ||
940 (mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
941 (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
942 (mgmt_class == IB_MGMT_CLASS_BIS) ||
943 ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
944 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END)))
948 EXPORT_SYMBOL(ib_is_mad_class_rmpp);
950 void *ib_get_rmpp_segment(struct ib_mad_send_buf *send_buf, int seg_num)
952 struct ib_mad_send_wr_private *mad_send_wr;
953 struct list_head *list;
955 mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
957 list = &mad_send_wr->cur_seg->list;
959 if (mad_send_wr->cur_seg->num < seg_num) {
960 list_for_each_entry(mad_send_wr->cur_seg, list, list)
961 if (mad_send_wr->cur_seg->num == seg_num)
963 } else if (mad_send_wr->cur_seg->num > seg_num) {
964 list_for_each_entry_reverse(mad_send_wr->cur_seg, list, list)
965 if (mad_send_wr->cur_seg->num == seg_num)
968 return mad_send_wr->cur_seg->data;
970 EXPORT_SYMBOL(ib_get_rmpp_segment);
972 static inline void *ib_get_payload(struct ib_mad_send_wr_private *mad_send_wr)
974 if (mad_send_wr->send_buf.seg_count)
975 return ib_get_rmpp_segment(&mad_send_wr->send_buf,
976 mad_send_wr->seg_num);
978 return mad_send_wr->send_buf.mad +
979 mad_send_wr->send_buf.hdr_len;
982 void ib_free_send_mad(struct ib_mad_send_buf *send_buf)
984 struct ib_mad_agent_private *mad_agent_priv;
985 struct ib_mad_send_wr_private *mad_send_wr;
987 mad_agent_priv = container_of(send_buf->mad_agent,
988 struct ib_mad_agent_private, agent);
989 mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
992 free_send_rmpp_list(mad_send_wr);
993 kfree(send_buf->mad);
994 deref_mad_agent(mad_agent_priv);
996 EXPORT_SYMBOL(ib_free_send_mad);
998 int ib_send_mad(struct ib_mad_send_wr_private *mad_send_wr)
1000 struct ib_mad_qp_info *qp_info;
1001 struct list_head *list;
1002 struct ib_mad_agent *mad_agent;
1004 unsigned long flags;
1007 /* Set WR ID to find mad_send_wr upon completion */
1008 qp_info = mad_send_wr->mad_agent_priv->qp_info;
1009 mad_send_wr->mad_list.mad_queue = &qp_info->send_queue;
1010 mad_send_wr->mad_list.cqe.done = ib_mad_send_done;
1011 mad_send_wr->send_wr.wr.wr_cqe = &mad_send_wr->mad_list.cqe;
1013 mad_agent = mad_send_wr->send_buf.mad_agent;
1014 sge = mad_send_wr->sg_list;
1015 sge[0].addr = ib_dma_map_single(mad_agent->device,
1016 mad_send_wr->send_buf.mad,
1019 if (unlikely(ib_dma_mapping_error(mad_agent->device, sge[0].addr)))
1022 mad_send_wr->header_mapping = sge[0].addr;
1024 sge[1].addr = ib_dma_map_single(mad_agent->device,
1025 ib_get_payload(mad_send_wr),
1028 if (unlikely(ib_dma_mapping_error(mad_agent->device, sge[1].addr))) {
1029 ib_dma_unmap_single(mad_agent->device,
1030 mad_send_wr->header_mapping,
1031 sge[0].length, DMA_TO_DEVICE);
1034 mad_send_wr->payload_mapping = sge[1].addr;
1036 spin_lock_irqsave(&qp_info->send_queue.lock, flags);
1037 if (qp_info->send_queue.count < qp_info->send_queue.max_active) {
1038 trace_ib_mad_ib_send_mad(mad_send_wr, qp_info);
1039 ret = ib_post_send(mad_agent->qp, &mad_send_wr->send_wr.wr,
1041 list = &qp_info->send_queue.list;
1044 list = &qp_info->overflow_list;
1048 qp_info->send_queue.count++;
1049 list_add_tail(&mad_send_wr->mad_list.list, list);
1051 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
1053 ib_dma_unmap_single(mad_agent->device,
1054 mad_send_wr->header_mapping,
1055 sge[0].length, DMA_TO_DEVICE);
1056 ib_dma_unmap_single(mad_agent->device,
1057 mad_send_wr->payload_mapping,
1058 sge[1].length, DMA_TO_DEVICE);
1064 * ib_post_send_mad - Posts MAD(s) to the send queue of the QP associated
1065 * with the registered client
1067 int ib_post_send_mad(struct ib_mad_send_buf *send_buf,
1068 struct ib_mad_send_buf **bad_send_buf)
1070 struct ib_mad_agent_private *mad_agent_priv;
1071 struct ib_mad_send_buf *next_send_buf;
1072 struct ib_mad_send_wr_private *mad_send_wr;
1073 unsigned long flags;
1076 /* Walk list of send WRs and post each on send list */
1077 for (; send_buf; send_buf = next_send_buf) {
1078 mad_send_wr = container_of(send_buf,
1079 struct ib_mad_send_wr_private,
1081 mad_agent_priv = mad_send_wr->mad_agent_priv;
1083 ret = ib_mad_enforce_security(mad_agent_priv,
1084 mad_send_wr->send_wr.pkey_index);
1088 if (!send_buf->mad_agent->send_handler ||
1089 (send_buf->timeout_ms &&
1090 !send_buf->mad_agent->recv_handler)) {
1095 if (!ib_is_mad_class_rmpp(((struct ib_mad_hdr *) send_buf->mad)->mgmt_class)) {
1096 if (mad_agent_priv->agent.rmpp_version) {
1103 * Save pointer to next work request to post in case the
1104 * current one completes, and the user modifies the work
1105 * request associated with the completion
1107 next_send_buf = send_buf->next;
1108 mad_send_wr->send_wr.ah = send_buf->ah;
1110 if (((struct ib_mad_hdr *) send_buf->mad)->mgmt_class ==
1111 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
1112 ret = handle_outgoing_dr_smp(mad_agent_priv,
1114 if (ret < 0) /* error */
1116 else if (ret == 1) /* locally consumed */
1120 mad_send_wr->tid = ((struct ib_mad_hdr *) send_buf->mad)->tid;
1121 /* Timeout will be updated after send completes */
1122 mad_send_wr->timeout = msecs_to_jiffies(send_buf->timeout_ms);
1123 mad_send_wr->max_retries = send_buf->retries;
1124 mad_send_wr->retries_left = send_buf->retries;
1125 send_buf->retries = 0;
1126 /* Reference for work request to QP + response */
1127 mad_send_wr->refcount = 1 + (mad_send_wr->timeout > 0);
1128 mad_send_wr->status = IB_WC_SUCCESS;
1130 /* Reference MAD agent until send completes */
1131 refcount_inc(&mad_agent_priv->refcount);
1132 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1133 list_add_tail(&mad_send_wr->agent_list,
1134 &mad_agent_priv->send_list);
1135 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1137 if (ib_mad_kernel_rmpp_agent(&mad_agent_priv->agent)) {
1138 ret = ib_send_rmpp_mad(mad_send_wr);
1139 if (ret >= 0 && ret != IB_RMPP_RESULT_CONSUMED)
1140 ret = ib_send_mad(mad_send_wr);
1142 ret = ib_send_mad(mad_send_wr);
1144 /* Fail send request */
1145 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1146 list_del(&mad_send_wr->agent_list);
1147 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1148 deref_mad_agent(mad_agent_priv);
1155 *bad_send_buf = send_buf;
1158 EXPORT_SYMBOL(ib_post_send_mad);
1161 * ib_free_recv_mad - Returns data buffers used to receive
1162 * a MAD to the access layer
1164 void ib_free_recv_mad(struct ib_mad_recv_wc *mad_recv_wc)
1166 struct ib_mad_recv_buf *mad_recv_buf, *temp_recv_buf;
1167 struct ib_mad_private_header *mad_priv_hdr;
1168 struct ib_mad_private *priv;
1169 struct list_head free_list;
1171 INIT_LIST_HEAD(&free_list);
1172 list_splice_init(&mad_recv_wc->rmpp_list, &free_list);
1174 list_for_each_entry_safe(mad_recv_buf, temp_recv_buf,
1176 mad_recv_wc = container_of(mad_recv_buf, struct ib_mad_recv_wc,
1178 mad_priv_hdr = container_of(mad_recv_wc,
1179 struct ib_mad_private_header,
1181 priv = container_of(mad_priv_hdr, struct ib_mad_private,
1186 EXPORT_SYMBOL(ib_free_recv_mad);
1188 static int method_in_use(struct ib_mad_mgmt_method_table **method,
1189 struct ib_mad_reg_req *mad_reg_req)
1193 for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS) {
1194 if ((*method)->agent[i]) {
1195 pr_err("Method %d already in use\n", i);
1202 static int allocate_method_table(struct ib_mad_mgmt_method_table **method)
1204 /* Allocate management method table */
1205 *method = kzalloc(sizeof **method, GFP_ATOMIC);
1206 return (*method) ? 0 : (-ENOMEM);
1210 * Check to see if there are any methods still in use
1212 static int check_method_table(struct ib_mad_mgmt_method_table *method)
1216 for (i = 0; i < IB_MGMT_MAX_METHODS; i++)
1217 if (method->agent[i])
1223 * Check to see if there are any method tables for this class still in use
1225 static int check_class_table(struct ib_mad_mgmt_class_table *class)
1229 for (i = 0; i < MAX_MGMT_CLASS; i++)
1230 if (class->method_table[i])
1235 static int check_vendor_class(struct ib_mad_mgmt_vendor_class *vendor_class)
1239 for (i = 0; i < MAX_MGMT_OUI; i++)
1240 if (vendor_class->method_table[i])
1245 static int find_vendor_oui(struct ib_mad_mgmt_vendor_class *vendor_class,
1250 for (i = 0; i < MAX_MGMT_OUI; i++)
1251 /* Is there matching OUI for this vendor class ? */
1252 if (!memcmp(vendor_class->oui[i], oui, 3))
1258 static int check_vendor_table(struct ib_mad_mgmt_vendor_class_table *vendor)
1262 for (i = 0; i < MAX_MGMT_VENDOR_RANGE2; i++)
1263 if (vendor->vendor_class[i])
1269 static void remove_methods_mad_agent(struct ib_mad_mgmt_method_table *method,
1270 struct ib_mad_agent_private *agent)
1274 /* Remove any methods for this mad agent */
1275 for (i = 0; i < IB_MGMT_MAX_METHODS; i++)
1276 if (method->agent[i] == agent)
1277 method->agent[i] = NULL;
1280 static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1281 struct ib_mad_agent_private *agent_priv,
1284 struct ib_mad_port_private *port_priv;
1285 struct ib_mad_mgmt_class_table **class;
1286 struct ib_mad_mgmt_method_table **method;
1289 port_priv = agent_priv->qp_info->port_priv;
1290 class = &port_priv->version[mad_reg_req->mgmt_class_version].class;
1292 /* Allocate management class table for "new" class version */
1293 *class = kzalloc(sizeof **class, GFP_ATOMIC);
1299 /* Allocate method table for this management class */
1300 method = &(*class)->method_table[mgmt_class];
1301 if ((ret = allocate_method_table(method)))
1304 method = &(*class)->method_table[mgmt_class];
1306 /* Allocate method table for this management class */
1307 if ((ret = allocate_method_table(method)))
1312 /* Now, make sure methods are not already in use */
1313 if (method_in_use(method, mad_reg_req))
1316 /* Finally, add in methods being registered */
1317 for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS)
1318 (*method)->agent[i] = agent_priv;
1323 /* Remove any methods for this mad agent */
1324 remove_methods_mad_agent(*method, agent_priv);
1325 /* Now, check to see if there are any methods in use */
1326 if (!check_method_table(*method)) {
1327 /* If not, release management method table */
1340 static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1341 struct ib_mad_agent_private *agent_priv)
1343 struct ib_mad_port_private *port_priv;
1344 struct ib_mad_mgmt_vendor_class_table **vendor_table;
1345 struct ib_mad_mgmt_vendor_class_table *vendor = NULL;
1346 struct ib_mad_mgmt_vendor_class *vendor_class = NULL;
1347 struct ib_mad_mgmt_method_table **method;
1348 int i, ret = -ENOMEM;
1351 /* "New" vendor (with OUI) class */
1352 vclass = vendor_class_index(mad_reg_req->mgmt_class);
1353 port_priv = agent_priv->qp_info->port_priv;
1354 vendor_table = &port_priv->version[
1355 mad_reg_req->mgmt_class_version].vendor;
1356 if (!*vendor_table) {
1357 /* Allocate mgmt vendor class table for "new" class version */
1358 vendor = kzalloc(sizeof *vendor, GFP_ATOMIC);
1362 *vendor_table = vendor;
1364 if (!(*vendor_table)->vendor_class[vclass]) {
1365 /* Allocate table for this management vendor class */
1366 vendor_class = kzalloc(sizeof *vendor_class, GFP_ATOMIC);
1370 (*vendor_table)->vendor_class[vclass] = vendor_class;
1372 for (i = 0; i < MAX_MGMT_OUI; i++) {
1373 /* Is there matching OUI for this vendor class ? */
1374 if (!memcmp((*vendor_table)->vendor_class[vclass]->oui[i],
1375 mad_reg_req->oui, 3)) {
1376 method = &(*vendor_table)->vendor_class[
1377 vclass]->method_table[i];
1383 for (i = 0; i < MAX_MGMT_OUI; i++) {
1384 /* OUI slot available ? */
1385 if (!is_vendor_oui((*vendor_table)->vendor_class[
1387 method = &(*vendor_table)->vendor_class[
1388 vclass]->method_table[i];
1389 /* Allocate method table for this OUI */
1391 ret = allocate_method_table(method);
1395 memcpy((*vendor_table)->vendor_class[vclass]->oui[i],
1396 mad_reg_req->oui, 3);
1400 dev_err(&agent_priv->agent.device->dev, "All OUI slots in use\n");
1404 /* Now, make sure methods are not already in use */
1405 if (method_in_use(method, mad_reg_req))
1408 /* Finally, add in methods being registered */
1409 for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS)
1410 (*method)->agent[i] = agent_priv;
1415 /* Remove any methods for this mad agent */
1416 remove_methods_mad_agent(*method, agent_priv);
1417 /* Now, check to see if there are any methods in use */
1418 if (!check_method_table(*method)) {
1419 /* If not, release management method table */
1426 (*vendor_table)->vendor_class[vclass] = NULL;
1427 kfree(vendor_class);
1431 *vendor_table = NULL;
1438 static void remove_mad_reg_req(struct ib_mad_agent_private *agent_priv)
1440 struct ib_mad_port_private *port_priv;
1441 struct ib_mad_mgmt_class_table *class;
1442 struct ib_mad_mgmt_method_table *method;
1443 struct ib_mad_mgmt_vendor_class_table *vendor;
1444 struct ib_mad_mgmt_vendor_class *vendor_class;
1449 * Was MAD registration request supplied
1450 * with original registration ?
1452 if (!agent_priv->reg_req)
1455 port_priv = agent_priv->qp_info->port_priv;
1456 mgmt_class = convert_mgmt_class(agent_priv->reg_req->mgmt_class);
1457 class = port_priv->version[
1458 agent_priv->reg_req->mgmt_class_version].class;
1462 method = class->method_table[mgmt_class];
1464 /* Remove any methods for this mad agent */
1465 remove_methods_mad_agent(method, agent_priv);
1466 /* Now, check to see if there are any methods still in use */
1467 if (!check_method_table(method)) {
1468 /* If not, release management method table */
1470 class->method_table[mgmt_class] = NULL;
1471 /* Any management classes left ? */
1472 if (!check_class_table(class)) {
1473 /* If not, release management class table */
1476 agent_priv->reg_req->
1477 mgmt_class_version].class = NULL;
1483 if (!is_vendor_class(mgmt_class))
1486 /* normalize mgmt_class to vendor range 2 */
1487 mgmt_class = vendor_class_index(agent_priv->reg_req->mgmt_class);
1488 vendor = port_priv->version[
1489 agent_priv->reg_req->mgmt_class_version].vendor;
1494 vendor_class = vendor->vendor_class[mgmt_class];
1496 index = find_vendor_oui(vendor_class, agent_priv->reg_req->oui);
1499 method = vendor_class->method_table[index];
1501 /* Remove any methods for this mad agent */
1502 remove_methods_mad_agent(method, agent_priv);
1504 * Now, check to see if there are
1505 * any methods still in use
1507 if (!check_method_table(method)) {
1508 /* If not, release management method table */
1510 vendor_class->method_table[index] = NULL;
1511 memset(vendor_class->oui[index], 0, 3);
1512 /* Any OUIs left ? */
1513 if (!check_vendor_class(vendor_class)) {
1514 /* If not, release vendor class table */
1515 kfree(vendor_class);
1516 vendor->vendor_class[mgmt_class] = NULL;
1517 /* Any other vendor classes left ? */
1518 if (!check_vendor_table(vendor)) {
1521 agent_priv->reg_req->
1522 mgmt_class_version].
1534 static struct ib_mad_agent_private *
1535 find_mad_agent(struct ib_mad_port_private *port_priv,
1536 const struct ib_mad_hdr *mad_hdr)
1538 struct ib_mad_agent_private *mad_agent = NULL;
1539 unsigned long flags;
1541 if (ib_response_mad(mad_hdr)) {
1545 * Routing is based on high 32 bits of transaction ID
1548 hi_tid = be64_to_cpu(mad_hdr->tid) >> 32;
1550 mad_agent = xa_load(&ib_mad_clients, hi_tid);
1551 if (mad_agent && !refcount_inc_not_zero(&mad_agent->refcount))
1555 struct ib_mad_mgmt_class_table *class;
1556 struct ib_mad_mgmt_method_table *method;
1557 struct ib_mad_mgmt_vendor_class_table *vendor;
1558 struct ib_mad_mgmt_vendor_class *vendor_class;
1559 const struct ib_vendor_mad *vendor_mad;
1562 spin_lock_irqsave(&port_priv->reg_lock, flags);
1564 * Routing is based on version, class, and method
1565 * For "newer" vendor MADs, also based on OUI
1567 if (mad_hdr->class_version >= MAX_MGMT_VERSION)
1569 if (!is_vendor_class(mad_hdr->mgmt_class)) {
1570 class = port_priv->version[
1571 mad_hdr->class_version].class;
1574 if (convert_mgmt_class(mad_hdr->mgmt_class) >=
1575 ARRAY_SIZE(class->method_table))
1577 method = class->method_table[convert_mgmt_class(
1578 mad_hdr->mgmt_class)];
1580 mad_agent = method->agent[mad_hdr->method &
1581 ~IB_MGMT_METHOD_RESP];
1583 vendor = port_priv->version[
1584 mad_hdr->class_version].vendor;
1587 vendor_class = vendor->vendor_class[vendor_class_index(
1588 mad_hdr->mgmt_class)];
1591 /* Find matching OUI */
1592 vendor_mad = (const struct ib_vendor_mad *)mad_hdr;
1593 index = find_vendor_oui(vendor_class, vendor_mad->oui);
1596 method = vendor_class->method_table[index];
1598 mad_agent = method->agent[mad_hdr->method &
1599 ~IB_MGMT_METHOD_RESP];
1603 refcount_inc(&mad_agent->refcount);
1605 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
1608 if (mad_agent && !mad_agent->agent.recv_handler) {
1609 dev_notice(&port_priv->device->dev,
1610 "No receive handler for client %p on port %u\n",
1611 &mad_agent->agent, port_priv->port_num);
1612 deref_mad_agent(mad_agent);
1619 static int validate_mad(const struct ib_mad_hdr *mad_hdr,
1620 const struct ib_mad_qp_info *qp_info,
1624 u32 qp_num = qp_info->qp->qp_num;
1626 /* Make sure MAD base version is understood */
1627 if (mad_hdr->base_version != IB_MGMT_BASE_VERSION &&
1628 (!opa || mad_hdr->base_version != OPA_MGMT_BASE_VERSION)) {
1629 pr_err("MAD received with unsupported base version %d %s\n",
1630 mad_hdr->base_version, opa ? "(opa)" : "");
1634 /* Filter SMI packets sent to other than QP0 */
1635 if ((mad_hdr->mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
1636 (mad_hdr->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) {
1640 /* CM attributes other than ClassPortInfo only use Send method */
1641 if ((mad_hdr->mgmt_class == IB_MGMT_CLASS_CM) &&
1642 (mad_hdr->attr_id != IB_MGMT_CLASSPORTINFO_ATTR_ID) &&
1643 (mad_hdr->method != IB_MGMT_METHOD_SEND))
1645 /* Filter GSI packets sent to QP0 */
1654 static int is_rmpp_data_mad(const struct ib_mad_agent_private *mad_agent_priv,
1655 const struct ib_mad_hdr *mad_hdr)
1657 struct ib_rmpp_mad *rmpp_mad;
1659 rmpp_mad = (struct ib_rmpp_mad *)mad_hdr;
1660 return !mad_agent_priv->agent.rmpp_version ||
1661 !ib_mad_kernel_rmpp_agent(&mad_agent_priv->agent) ||
1662 !(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
1663 IB_MGMT_RMPP_FLAG_ACTIVE) ||
1664 (rmpp_mad->rmpp_hdr.rmpp_type == IB_MGMT_RMPP_TYPE_DATA);
1667 static inline int rcv_has_same_class(const struct ib_mad_send_wr_private *wr,
1668 const struct ib_mad_recv_wc *rwc)
1670 return ((struct ib_mad_hdr *)(wr->send_buf.mad))->mgmt_class ==
1671 rwc->recv_buf.mad->mad_hdr.mgmt_class;
1675 rcv_has_same_gid(const struct ib_mad_agent_private *mad_agent_priv,
1676 const struct ib_mad_send_wr_private *wr,
1677 const struct ib_mad_recv_wc *rwc)
1679 struct rdma_ah_attr attr;
1680 u8 send_resp, rcv_resp;
1682 struct ib_device *device = mad_agent_priv->agent.device;
1683 u32 port_num = mad_agent_priv->agent.port_num;
1687 send_resp = ib_response_mad((struct ib_mad_hdr *)wr->send_buf.mad);
1688 rcv_resp = ib_response_mad(&rwc->recv_buf.mad->mad_hdr);
1690 if (send_resp == rcv_resp)
1691 /* both requests, or both responses. GIDs different */
1694 if (rdma_query_ah(wr->send_buf.ah, &attr))
1695 /* Assume not equal, to avoid false positives. */
1698 has_grh = !!(rdma_ah_get_ah_flags(&attr) & IB_AH_GRH);
1699 if (has_grh != !!(rwc->wc->wc_flags & IB_WC_GRH))
1700 /* one has GID, other does not. Assume different */
1703 if (!send_resp && rcv_resp) {
1704 /* is request/response. */
1706 if (ib_get_cached_lmc(device, port_num, &lmc))
1708 return (!lmc || !((rdma_ah_get_path_bits(&attr) ^
1709 rwc->wc->dlid_path_bits) &
1712 const struct ib_global_route *grh =
1713 rdma_ah_read_grh(&attr);
1715 if (rdma_query_gid(device, port_num,
1716 grh->sgid_index, &sgid))
1718 return !memcmp(sgid.raw, rwc->recv_buf.grh->dgid.raw,
1724 return rdma_ah_get_dlid(&attr) == rwc->wc->slid;
1726 return !memcmp(rdma_ah_read_grh(&attr)->dgid.raw,
1727 rwc->recv_buf.grh->sgid.raw,
1731 static inline int is_direct(u8 class)
1733 return (class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE);
1736 struct ib_mad_send_wr_private*
1737 ib_find_send_mad(const struct ib_mad_agent_private *mad_agent_priv,
1738 const struct ib_mad_recv_wc *wc)
1740 struct ib_mad_send_wr_private *wr;
1741 const struct ib_mad_hdr *mad_hdr;
1743 mad_hdr = &wc->recv_buf.mad->mad_hdr;
1745 list_for_each_entry(wr, &mad_agent_priv->wait_list, agent_list) {
1746 if ((wr->tid == mad_hdr->tid) &&
1747 rcv_has_same_class(wr, wc) &&
1749 * Don't check GID for direct routed MADs.
1750 * These might have permissive LIDs.
1752 (is_direct(mad_hdr->mgmt_class) ||
1753 rcv_has_same_gid(mad_agent_priv, wr, wc)))
1754 return (wr->status == IB_WC_SUCCESS) ? wr : NULL;
1758 * It's possible to receive the response before we've
1759 * been notified that the send has completed
1761 list_for_each_entry(wr, &mad_agent_priv->send_list, agent_list) {
1762 if (is_rmpp_data_mad(mad_agent_priv, wr->send_buf.mad) &&
1763 wr->tid == mad_hdr->tid &&
1765 rcv_has_same_class(wr, wc) &&
1767 * Don't check GID for direct routed MADs.
1768 * These might have permissive LIDs.
1770 (is_direct(mad_hdr->mgmt_class) ||
1771 rcv_has_same_gid(mad_agent_priv, wr, wc)))
1772 /* Verify request has not been canceled */
1773 return (wr->status == IB_WC_SUCCESS) ? wr : NULL;
1778 void ib_mark_mad_done(struct ib_mad_send_wr_private *mad_send_wr)
1780 mad_send_wr->timeout = 0;
1781 if (mad_send_wr->refcount == 1)
1782 list_move_tail(&mad_send_wr->agent_list,
1783 &mad_send_wr->mad_agent_priv->done_list);
1786 static void ib_mad_complete_recv(struct ib_mad_agent_private *mad_agent_priv,
1787 struct ib_mad_recv_wc *mad_recv_wc)
1789 struct ib_mad_send_wr_private *mad_send_wr;
1790 struct ib_mad_send_wc mad_send_wc;
1791 unsigned long flags;
1794 INIT_LIST_HEAD(&mad_recv_wc->rmpp_list);
1795 ret = ib_mad_enforce_security(mad_agent_priv,
1796 mad_recv_wc->wc->pkey_index);
1798 ib_free_recv_mad(mad_recv_wc);
1799 deref_mad_agent(mad_agent_priv);
1803 list_add(&mad_recv_wc->recv_buf.list, &mad_recv_wc->rmpp_list);
1804 if (ib_mad_kernel_rmpp_agent(&mad_agent_priv->agent)) {
1805 mad_recv_wc = ib_process_rmpp_recv_wc(mad_agent_priv,
1808 deref_mad_agent(mad_agent_priv);
1813 /* Complete corresponding request */
1814 if (ib_response_mad(&mad_recv_wc->recv_buf.mad->mad_hdr)) {
1815 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1816 mad_send_wr = ib_find_send_mad(mad_agent_priv, mad_recv_wc);
1818 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1819 if (!ib_mad_kernel_rmpp_agent(&mad_agent_priv->agent)
1820 && ib_is_mad_class_rmpp(mad_recv_wc->recv_buf.mad->mad_hdr.mgmt_class)
1821 && (ib_get_rmpp_flags(&((struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad)->rmpp_hdr)
1822 & IB_MGMT_RMPP_FLAG_ACTIVE)) {
1823 /* user rmpp is in effect
1824 * and this is an active RMPP MAD
1826 mad_agent_priv->agent.recv_handler(
1827 &mad_agent_priv->agent, NULL,
1829 deref_mad_agent(mad_agent_priv);
1831 /* not user rmpp, revert to normal behavior and
1834 ib_free_recv_mad(mad_recv_wc);
1835 deref_mad_agent(mad_agent_priv);
1839 ib_mark_mad_done(mad_send_wr);
1840 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1842 /* Defined behavior is to complete response before request */
1843 mad_agent_priv->agent.recv_handler(
1844 &mad_agent_priv->agent,
1845 &mad_send_wr->send_buf,
1847 deref_mad_agent(mad_agent_priv);
1849 mad_send_wc.status = IB_WC_SUCCESS;
1850 mad_send_wc.vendor_err = 0;
1851 mad_send_wc.send_buf = &mad_send_wr->send_buf;
1852 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
1855 mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent, NULL,
1857 deref_mad_agent(mad_agent_priv);
1861 static enum smi_action handle_ib_smi(const struct ib_mad_port_private *port_priv,
1862 const struct ib_mad_qp_info *qp_info,
1863 const struct ib_wc *wc,
1865 struct ib_mad_private *recv,
1866 struct ib_mad_private *response)
1868 enum smi_forward_action retsmi;
1869 struct ib_smp *smp = (struct ib_smp *)recv->mad;
1871 trace_ib_mad_handle_ib_smi(smp);
1873 if (smi_handle_dr_smp_recv(smp,
1874 rdma_cap_ib_switch(port_priv->device),
1876 port_priv->device->phys_port_cnt) ==
1878 return IB_SMI_DISCARD;
1880 retsmi = smi_check_forward_dr_smp(smp);
1881 if (retsmi == IB_SMI_LOCAL)
1882 return IB_SMI_HANDLE;
1884 if (retsmi == IB_SMI_SEND) { /* don't forward */
1885 if (smi_handle_dr_smp_send(smp,
1886 rdma_cap_ib_switch(port_priv->device),
1887 port_num) == IB_SMI_DISCARD)
1888 return IB_SMI_DISCARD;
1890 if (smi_check_local_smp(smp, port_priv->device) == IB_SMI_DISCARD)
1891 return IB_SMI_DISCARD;
1892 } else if (rdma_cap_ib_switch(port_priv->device)) {
1893 /* forward case for switches */
1894 memcpy(response, recv, mad_priv_size(response));
1895 response->header.recv_wc.wc = &response->header.wc;
1896 response->header.recv_wc.recv_buf.mad = (struct ib_mad *)response->mad;
1897 response->header.recv_wc.recv_buf.grh = &response->grh;
1899 agent_send_response((const struct ib_mad_hdr *)response->mad,
1902 smi_get_fwd_port(smp),
1903 qp_info->qp->qp_num,
1907 return IB_SMI_DISCARD;
1909 return IB_SMI_HANDLE;
1912 static bool generate_unmatched_resp(const struct ib_mad_private *recv,
1913 struct ib_mad_private *response,
1914 size_t *resp_len, bool opa)
1916 const struct ib_mad_hdr *recv_hdr = (const struct ib_mad_hdr *)recv->mad;
1917 struct ib_mad_hdr *resp_hdr = (struct ib_mad_hdr *)response->mad;
1919 if (recv_hdr->method == IB_MGMT_METHOD_GET ||
1920 recv_hdr->method == IB_MGMT_METHOD_SET) {
1921 memcpy(response, recv, mad_priv_size(response));
1922 response->header.recv_wc.wc = &response->header.wc;
1923 response->header.recv_wc.recv_buf.mad = (struct ib_mad *)response->mad;
1924 response->header.recv_wc.recv_buf.grh = &response->grh;
1925 resp_hdr->method = IB_MGMT_METHOD_GET_RESP;
1926 resp_hdr->status = cpu_to_be16(IB_MGMT_MAD_STATUS_UNSUPPORTED_METHOD_ATTRIB);
1927 if (recv_hdr->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
1928 resp_hdr->status |= IB_SMP_DIRECTION;
1930 if (opa && recv_hdr->base_version == OPA_MGMT_BASE_VERSION) {
1931 if (recv_hdr->mgmt_class ==
1932 IB_MGMT_CLASS_SUBN_LID_ROUTED ||
1933 recv_hdr->mgmt_class ==
1934 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
1935 *resp_len = opa_get_smp_header_size(
1936 (struct opa_smp *)recv->mad);
1938 *resp_len = sizeof(struct ib_mad_hdr);
1947 static enum smi_action
1948 handle_opa_smi(struct ib_mad_port_private *port_priv,
1949 struct ib_mad_qp_info *qp_info,
1952 struct ib_mad_private *recv,
1953 struct ib_mad_private *response)
1955 enum smi_forward_action retsmi;
1956 struct opa_smp *smp = (struct opa_smp *)recv->mad;
1958 trace_ib_mad_handle_opa_smi(smp);
1960 if (opa_smi_handle_dr_smp_recv(smp,
1961 rdma_cap_ib_switch(port_priv->device),
1963 port_priv->device->phys_port_cnt) ==
1965 return IB_SMI_DISCARD;
1967 retsmi = opa_smi_check_forward_dr_smp(smp);
1968 if (retsmi == IB_SMI_LOCAL)
1969 return IB_SMI_HANDLE;
1971 if (retsmi == IB_SMI_SEND) { /* don't forward */
1972 if (opa_smi_handle_dr_smp_send(smp,
1973 rdma_cap_ib_switch(port_priv->device),
1974 port_num) == IB_SMI_DISCARD)
1975 return IB_SMI_DISCARD;
1977 if (opa_smi_check_local_smp(smp, port_priv->device) ==
1979 return IB_SMI_DISCARD;
1981 } else if (rdma_cap_ib_switch(port_priv->device)) {
1982 /* forward case for switches */
1983 memcpy(response, recv, mad_priv_size(response));
1984 response->header.recv_wc.wc = &response->header.wc;
1985 response->header.recv_wc.recv_buf.opa_mad =
1986 (struct opa_mad *)response->mad;
1987 response->header.recv_wc.recv_buf.grh = &response->grh;
1989 agent_send_response((const struct ib_mad_hdr *)response->mad,
1992 opa_smi_get_fwd_port(smp),
1993 qp_info->qp->qp_num,
1994 recv->header.wc.byte_len,
1997 return IB_SMI_DISCARD;
2000 return IB_SMI_HANDLE;
2003 static enum smi_action
2004 handle_smi(struct ib_mad_port_private *port_priv,
2005 struct ib_mad_qp_info *qp_info,
2008 struct ib_mad_private *recv,
2009 struct ib_mad_private *response,
2012 struct ib_mad_hdr *mad_hdr = (struct ib_mad_hdr *)recv->mad;
2014 if (opa && mad_hdr->base_version == OPA_MGMT_BASE_VERSION &&
2015 mad_hdr->class_version == OPA_SM_CLASS_VERSION)
2016 return handle_opa_smi(port_priv, qp_info, wc, port_num, recv,
2019 return handle_ib_smi(port_priv, qp_info, wc, port_num, recv, response);
2022 static void ib_mad_recv_done(struct ib_cq *cq, struct ib_wc *wc)
2024 struct ib_mad_port_private *port_priv = cq->cq_context;
2025 struct ib_mad_list_head *mad_list =
2026 container_of(wc->wr_cqe, struct ib_mad_list_head, cqe);
2027 struct ib_mad_qp_info *qp_info;
2028 struct ib_mad_private_header *mad_priv_hdr;
2029 struct ib_mad_private *recv, *response = NULL;
2030 struct ib_mad_agent_private *mad_agent;
2032 int ret = IB_MAD_RESULT_SUCCESS;
2034 u16 resp_mad_pkey_index = 0;
2037 if (list_empty_careful(&port_priv->port_list))
2040 if (wc->status != IB_WC_SUCCESS) {
2042 * Receive errors indicate that the QP has entered the error
2043 * state - error handling/shutdown code will cleanup
2048 qp_info = mad_list->mad_queue->qp_info;
2049 dequeue_mad(mad_list);
2051 opa = rdma_cap_opa_mad(qp_info->port_priv->device,
2052 qp_info->port_priv->port_num);
2054 mad_priv_hdr = container_of(mad_list, struct ib_mad_private_header,
2056 recv = container_of(mad_priv_hdr, struct ib_mad_private, header);
2057 ib_dma_unmap_single(port_priv->device,
2058 recv->header.mapping,
2059 mad_priv_dma_size(recv),
2062 /* Setup MAD receive work completion from "normal" work completion */
2063 recv->header.wc = *wc;
2064 recv->header.recv_wc.wc = &recv->header.wc;
2066 if (opa && ((struct ib_mad_hdr *)(recv->mad))->base_version == OPA_MGMT_BASE_VERSION) {
2067 recv->header.recv_wc.mad_len = wc->byte_len - sizeof(struct ib_grh);
2068 recv->header.recv_wc.mad_seg_size = sizeof(struct opa_mad);
2070 recv->header.recv_wc.mad_len = sizeof(struct ib_mad);
2071 recv->header.recv_wc.mad_seg_size = sizeof(struct ib_mad);
2074 recv->header.recv_wc.recv_buf.mad = (struct ib_mad *)recv->mad;
2075 recv->header.recv_wc.recv_buf.grh = &recv->grh;
2078 if (!validate_mad((const struct ib_mad_hdr *)recv->mad, qp_info, opa))
2081 trace_ib_mad_recv_done_handler(qp_info, wc,
2082 (struct ib_mad_hdr *)recv->mad);
2084 mad_size = recv->mad_size;
2085 response = alloc_mad_private(mad_size, GFP_KERNEL);
2089 if (rdma_cap_ib_switch(port_priv->device))
2090 port_num = wc->port_num;
2092 port_num = port_priv->port_num;
2094 if (((struct ib_mad_hdr *)recv->mad)->mgmt_class ==
2095 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
2096 if (handle_smi(port_priv, qp_info, wc, port_num, recv,
2102 /* Give driver "right of first refusal" on incoming MAD */
2103 if (port_priv->device->ops.process_mad) {
2104 ret = port_priv->device->ops.process_mad(
2105 port_priv->device, 0, port_priv->port_num, wc,
2106 &recv->grh, (const struct ib_mad *)recv->mad,
2107 (struct ib_mad *)response->mad, &mad_size,
2108 &resp_mad_pkey_index);
2111 wc->pkey_index = resp_mad_pkey_index;
2113 if (ret & IB_MAD_RESULT_SUCCESS) {
2114 if (ret & IB_MAD_RESULT_CONSUMED)
2116 if (ret & IB_MAD_RESULT_REPLY) {
2117 agent_send_response((const struct ib_mad_hdr *)response->mad,
2121 qp_info->qp->qp_num,
2128 mad_agent = find_mad_agent(port_priv, (const struct ib_mad_hdr *)recv->mad);
2130 trace_ib_mad_recv_done_agent(mad_agent);
2131 ib_mad_complete_recv(mad_agent, &recv->header.recv_wc);
2133 * recv is freed up in error cases in ib_mad_complete_recv
2134 * or via recv_handler in ib_mad_complete_recv()
2137 } else if ((ret & IB_MAD_RESULT_SUCCESS) &&
2138 generate_unmatched_resp(recv, response, &mad_size, opa)) {
2139 agent_send_response((const struct ib_mad_hdr *)response->mad, &recv->grh, wc,
2140 port_priv->device, port_num,
2141 qp_info->qp->qp_num, mad_size, opa);
2145 /* Post another receive request for this QP */
2147 ib_mad_post_receive_mads(qp_info, response);
2150 ib_mad_post_receive_mads(qp_info, recv);
2153 static void adjust_timeout(struct ib_mad_agent_private *mad_agent_priv)
2155 struct ib_mad_send_wr_private *mad_send_wr;
2156 unsigned long delay;
2158 if (list_empty(&mad_agent_priv->wait_list)) {
2159 cancel_delayed_work(&mad_agent_priv->timed_work);
2161 mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
2162 struct ib_mad_send_wr_private,
2165 if (time_after(mad_agent_priv->timeout,
2166 mad_send_wr->timeout)) {
2167 mad_agent_priv->timeout = mad_send_wr->timeout;
2168 delay = mad_send_wr->timeout - jiffies;
2169 if ((long)delay <= 0)
2171 mod_delayed_work(mad_agent_priv->qp_info->port_priv->wq,
2172 &mad_agent_priv->timed_work, delay);
2177 static void wait_for_response(struct ib_mad_send_wr_private *mad_send_wr)
2179 struct ib_mad_agent_private *mad_agent_priv;
2180 struct ib_mad_send_wr_private *temp_mad_send_wr;
2181 struct list_head *list_item;
2182 unsigned long delay;
2184 mad_agent_priv = mad_send_wr->mad_agent_priv;
2185 list_del(&mad_send_wr->agent_list);
2187 delay = mad_send_wr->timeout;
2188 mad_send_wr->timeout += jiffies;
2191 list_for_each_prev(list_item, &mad_agent_priv->wait_list) {
2192 temp_mad_send_wr = list_entry(list_item,
2193 struct ib_mad_send_wr_private,
2195 if (time_after(mad_send_wr->timeout,
2196 temp_mad_send_wr->timeout))
2200 list_item = &mad_agent_priv->wait_list;
2203 list_add(&mad_send_wr->agent_list, list_item);
2205 /* Reschedule a work item if we have a shorter timeout */
2206 if (mad_agent_priv->wait_list.next == &mad_send_wr->agent_list)
2207 mod_delayed_work(mad_agent_priv->qp_info->port_priv->wq,
2208 &mad_agent_priv->timed_work, delay);
2211 void ib_reset_mad_timeout(struct ib_mad_send_wr_private *mad_send_wr,
2212 unsigned long timeout_ms)
2214 mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
2215 wait_for_response(mad_send_wr);
2219 * Process a send work completion
2221 void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr,
2222 struct ib_mad_send_wc *mad_send_wc)
2224 struct ib_mad_agent_private *mad_agent_priv;
2225 unsigned long flags;
2228 mad_agent_priv = mad_send_wr->mad_agent_priv;
2229 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2230 if (ib_mad_kernel_rmpp_agent(&mad_agent_priv->agent)) {
2231 ret = ib_process_rmpp_send_wc(mad_send_wr, mad_send_wc);
2232 if (ret == IB_RMPP_RESULT_CONSUMED)
2235 ret = IB_RMPP_RESULT_UNHANDLED;
2237 if (mad_send_wc->status != IB_WC_SUCCESS &&
2238 mad_send_wr->status == IB_WC_SUCCESS) {
2239 mad_send_wr->status = mad_send_wc->status;
2240 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2243 if (--mad_send_wr->refcount > 0) {
2244 if (mad_send_wr->refcount == 1 && mad_send_wr->timeout &&
2245 mad_send_wr->status == IB_WC_SUCCESS) {
2246 wait_for_response(mad_send_wr);
2251 /* Remove send from MAD agent and notify client of completion */
2252 list_del(&mad_send_wr->agent_list);
2253 adjust_timeout(mad_agent_priv);
2254 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2256 if (mad_send_wr->status != IB_WC_SUCCESS)
2257 mad_send_wc->status = mad_send_wr->status;
2258 if (ret == IB_RMPP_RESULT_INTERNAL)
2259 ib_rmpp_send_handler(mad_send_wc);
2261 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2264 /* Release reference on agent taken when sending */
2265 deref_mad_agent(mad_agent_priv);
2268 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2271 static void ib_mad_send_done(struct ib_cq *cq, struct ib_wc *wc)
2273 struct ib_mad_port_private *port_priv = cq->cq_context;
2274 struct ib_mad_list_head *mad_list =
2275 container_of(wc->wr_cqe, struct ib_mad_list_head, cqe);
2276 struct ib_mad_send_wr_private *mad_send_wr, *queued_send_wr;
2277 struct ib_mad_qp_info *qp_info;
2278 struct ib_mad_queue *send_queue;
2279 struct ib_mad_send_wc mad_send_wc;
2280 unsigned long flags;
2283 if (list_empty_careful(&port_priv->port_list))
2286 if (wc->status != IB_WC_SUCCESS) {
2287 if (!ib_mad_send_error(port_priv, wc))
2291 mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
2293 send_queue = mad_list->mad_queue;
2294 qp_info = send_queue->qp_info;
2296 trace_ib_mad_send_done_agent(mad_send_wr->mad_agent_priv);
2297 trace_ib_mad_send_done_handler(mad_send_wr, wc);
2300 ib_dma_unmap_single(mad_send_wr->send_buf.mad_agent->device,
2301 mad_send_wr->header_mapping,
2302 mad_send_wr->sg_list[0].length, DMA_TO_DEVICE);
2303 ib_dma_unmap_single(mad_send_wr->send_buf.mad_agent->device,
2304 mad_send_wr->payload_mapping,
2305 mad_send_wr->sg_list[1].length, DMA_TO_DEVICE);
2306 queued_send_wr = NULL;
2307 spin_lock_irqsave(&send_queue->lock, flags);
2308 list_del(&mad_list->list);
2310 /* Move queued send to the send queue */
2311 if (send_queue->count-- > send_queue->max_active) {
2312 mad_list = container_of(qp_info->overflow_list.next,
2313 struct ib_mad_list_head, list);
2314 queued_send_wr = container_of(mad_list,
2315 struct ib_mad_send_wr_private,
2317 list_move_tail(&mad_list->list, &send_queue->list);
2319 spin_unlock_irqrestore(&send_queue->lock, flags);
2321 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2322 mad_send_wc.status = wc->status;
2323 mad_send_wc.vendor_err = wc->vendor_err;
2324 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
2326 if (queued_send_wr) {
2327 trace_ib_mad_send_done_resend(queued_send_wr, qp_info);
2328 ret = ib_post_send(qp_info->qp, &queued_send_wr->send_wr.wr,
2331 dev_err(&port_priv->device->dev,
2332 "ib_post_send failed: %d\n", ret);
2333 mad_send_wr = queued_send_wr;
2334 wc->status = IB_WC_LOC_QP_OP_ERR;
2340 static void mark_sends_for_retry(struct ib_mad_qp_info *qp_info)
2342 struct ib_mad_send_wr_private *mad_send_wr;
2343 struct ib_mad_list_head *mad_list;
2344 unsigned long flags;
2346 spin_lock_irqsave(&qp_info->send_queue.lock, flags);
2347 list_for_each_entry(mad_list, &qp_info->send_queue.list, list) {
2348 mad_send_wr = container_of(mad_list,
2349 struct ib_mad_send_wr_private,
2351 mad_send_wr->retry = 1;
2353 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
2356 static bool ib_mad_send_error(struct ib_mad_port_private *port_priv,
2359 struct ib_mad_list_head *mad_list =
2360 container_of(wc->wr_cqe, struct ib_mad_list_head, cqe);
2361 struct ib_mad_qp_info *qp_info = mad_list->mad_queue->qp_info;
2362 struct ib_mad_send_wr_private *mad_send_wr;
2366 * Send errors will transition the QP to SQE - move
2367 * QP to RTS and repost flushed work requests
2369 mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
2371 if (wc->status == IB_WC_WR_FLUSH_ERR) {
2372 if (mad_send_wr->retry) {
2374 mad_send_wr->retry = 0;
2375 trace_ib_mad_error_handler(mad_send_wr, qp_info);
2376 ret = ib_post_send(qp_info->qp, &mad_send_wr->send_wr.wr,
2382 struct ib_qp_attr *attr;
2384 /* Transition QP to RTS and fail offending send */
2385 attr = kmalloc(sizeof *attr, GFP_KERNEL);
2387 attr->qp_state = IB_QPS_RTS;
2388 attr->cur_qp_state = IB_QPS_SQE;
2389 ret = ib_modify_qp(qp_info->qp, attr,
2390 IB_QP_STATE | IB_QP_CUR_STATE);
2393 dev_err(&port_priv->device->dev,
2394 "%s - ib_modify_qp to RTS: %d\n",
2397 mark_sends_for_retry(qp_info);
2404 static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv)
2406 unsigned long flags;
2407 struct ib_mad_send_wr_private *mad_send_wr, *temp_mad_send_wr;
2408 struct ib_mad_send_wc mad_send_wc;
2409 struct list_head cancel_list;
2411 INIT_LIST_HEAD(&cancel_list);
2413 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2414 list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
2415 &mad_agent_priv->send_list, agent_list) {
2416 if (mad_send_wr->status == IB_WC_SUCCESS) {
2417 mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
2418 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2422 /* Empty wait list to prevent receives from finding a request */
2423 list_splice_init(&mad_agent_priv->wait_list, &cancel_list);
2424 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2426 /* Report all cancelled requests */
2427 mad_send_wc.status = IB_WC_WR_FLUSH_ERR;
2428 mad_send_wc.vendor_err = 0;
2430 list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
2431 &cancel_list, agent_list) {
2432 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2433 list_del(&mad_send_wr->agent_list);
2434 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2436 deref_mad_agent(mad_agent_priv);
2440 static struct ib_mad_send_wr_private*
2441 find_send_wr(struct ib_mad_agent_private *mad_agent_priv,
2442 struct ib_mad_send_buf *send_buf)
2444 struct ib_mad_send_wr_private *mad_send_wr;
2446 list_for_each_entry(mad_send_wr, &mad_agent_priv->wait_list,
2448 if (&mad_send_wr->send_buf == send_buf)
2452 list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list,
2454 if (is_rmpp_data_mad(mad_agent_priv,
2455 mad_send_wr->send_buf.mad) &&
2456 &mad_send_wr->send_buf == send_buf)
2462 int ib_modify_mad(struct ib_mad_agent *mad_agent,
2463 struct ib_mad_send_buf *send_buf, u32 timeout_ms)
2465 struct ib_mad_agent_private *mad_agent_priv;
2466 struct ib_mad_send_wr_private *mad_send_wr;
2467 unsigned long flags;
2470 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
2472 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2473 mad_send_wr = find_send_wr(mad_agent_priv, send_buf);
2474 if (!mad_send_wr || mad_send_wr->status != IB_WC_SUCCESS) {
2475 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2479 active = (!mad_send_wr->timeout || mad_send_wr->refcount > 1);
2481 mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
2482 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2485 mad_send_wr->send_buf.timeout_ms = timeout_ms;
2487 mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
2489 ib_reset_mad_timeout(mad_send_wr, timeout_ms);
2491 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2494 EXPORT_SYMBOL(ib_modify_mad);
2496 void ib_cancel_mad(struct ib_mad_agent *mad_agent,
2497 struct ib_mad_send_buf *send_buf)
2499 ib_modify_mad(mad_agent, send_buf, 0);
2501 EXPORT_SYMBOL(ib_cancel_mad);
2503 static void local_completions(struct work_struct *work)
2505 struct ib_mad_agent_private *mad_agent_priv;
2506 struct ib_mad_local_private *local;
2507 struct ib_mad_agent_private *recv_mad_agent;
2508 unsigned long flags;
2511 struct ib_mad_send_wc mad_send_wc;
2515 container_of(work, struct ib_mad_agent_private, local_work);
2517 opa = rdma_cap_opa_mad(mad_agent_priv->qp_info->port_priv->device,
2518 mad_agent_priv->qp_info->port_priv->port_num);
2520 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2521 while (!list_empty(&mad_agent_priv->local_list)) {
2522 local = list_entry(mad_agent_priv->local_list.next,
2523 struct ib_mad_local_private,
2525 list_del(&local->completion_list);
2526 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2528 if (local->mad_priv) {
2530 recv_mad_agent = local->recv_mad_agent;
2531 if (!recv_mad_agent) {
2532 dev_err(&mad_agent_priv->agent.device->dev,
2533 "No receive MAD agent for local completion\n");
2535 goto local_send_completion;
2539 * Defined behavior is to complete response
2542 build_smp_wc(recv_mad_agent->agent.qp,
2543 local->mad_send_wr->send_wr.wr.wr_cqe,
2544 be16_to_cpu(IB_LID_PERMISSIVE),
2545 local->mad_send_wr->send_wr.pkey_index,
2546 recv_mad_agent->agent.port_num, &wc);
2548 local->mad_priv->header.recv_wc.wc = &wc;
2550 base_version = ((struct ib_mad_hdr *)(local->mad_priv->mad))->base_version;
2551 if (opa && base_version == OPA_MGMT_BASE_VERSION) {
2552 local->mad_priv->header.recv_wc.mad_len = local->return_wc_byte_len;
2553 local->mad_priv->header.recv_wc.mad_seg_size = sizeof(struct opa_mad);
2555 local->mad_priv->header.recv_wc.mad_len = sizeof(struct ib_mad);
2556 local->mad_priv->header.recv_wc.mad_seg_size = sizeof(struct ib_mad);
2559 INIT_LIST_HEAD(&local->mad_priv->header.recv_wc.rmpp_list);
2560 list_add(&local->mad_priv->header.recv_wc.recv_buf.list,
2561 &local->mad_priv->header.recv_wc.rmpp_list);
2562 local->mad_priv->header.recv_wc.recv_buf.grh = NULL;
2563 local->mad_priv->header.recv_wc.recv_buf.mad =
2564 (struct ib_mad *)local->mad_priv->mad;
2565 recv_mad_agent->agent.recv_handler(
2566 &recv_mad_agent->agent,
2567 &local->mad_send_wr->send_buf,
2568 &local->mad_priv->header.recv_wc);
2569 spin_lock_irqsave(&recv_mad_agent->lock, flags);
2570 deref_mad_agent(recv_mad_agent);
2571 spin_unlock_irqrestore(&recv_mad_agent->lock, flags);
2574 local_send_completion:
2576 mad_send_wc.status = IB_WC_SUCCESS;
2577 mad_send_wc.vendor_err = 0;
2578 mad_send_wc.send_buf = &local->mad_send_wr->send_buf;
2579 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2582 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2583 deref_mad_agent(mad_agent_priv);
2585 kfree(local->mad_priv);
2588 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2591 static int retry_send(struct ib_mad_send_wr_private *mad_send_wr)
2595 if (!mad_send_wr->retries_left)
2598 mad_send_wr->retries_left--;
2599 mad_send_wr->send_buf.retries++;
2601 mad_send_wr->timeout = msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms);
2603 if (ib_mad_kernel_rmpp_agent(&mad_send_wr->mad_agent_priv->agent)) {
2604 ret = ib_retry_rmpp(mad_send_wr);
2606 case IB_RMPP_RESULT_UNHANDLED:
2607 ret = ib_send_mad(mad_send_wr);
2609 case IB_RMPP_RESULT_CONSUMED:
2617 ret = ib_send_mad(mad_send_wr);
2620 mad_send_wr->refcount++;
2621 list_add_tail(&mad_send_wr->agent_list,
2622 &mad_send_wr->mad_agent_priv->send_list);
2627 static void timeout_sends(struct work_struct *work)
2629 struct ib_mad_agent_private *mad_agent_priv;
2630 struct ib_mad_send_wr_private *mad_send_wr;
2631 struct ib_mad_send_wc mad_send_wc;
2632 unsigned long flags, delay;
2634 mad_agent_priv = container_of(work, struct ib_mad_agent_private,
2636 mad_send_wc.vendor_err = 0;
2638 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2639 while (!list_empty(&mad_agent_priv->wait_list)) {
2640 mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
2641 struct ib_mad_send_wr_private,
2644 if (time_after(mad_send_wr->timeout, jiffies)) {
2645 delay = mad_send_wr->timeout - jiffies;
2646 if ((long)delay <= 0)
2648 queue_delayed_work(mad_agent_priv->qp_info->
2650 &mad_agent_priv->timed_work, delay);
2654 list_del(&mad_send_wr->agent_list);
2655 if (mad_send_wr->status == IB_WC_SUCCESS &&
2656 !retry_send(mad_send_wr))
2659 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2661 if (mad_send_wr->status == IB_WC_SUCCESS)
2662 mad_send_wc.status = IB_WC_RESP_TIMEOUT_ERR;
2664 mad_send_wc.status = mad_send_wr->status;
2665 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2666 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2669 deref_mad_agent(mad_agent_priv);
2670 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2672 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2676 * Allocate receive MADs and post receive WRs for them
2678 static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
2679 struct ib_mad_private *mad)
2681 unsigned long flags;
2683 struct ib_mad_private *mad_priv;
2684 struct ib_sge sg_list;
2685 struct ib_recv_wr recv_wr;
2686 struct ib_mad_queue *recv_queue = &qp_info->recv_queue;
2688 /* Initialize common scatter list fields */
2689 sg_list.lkey = qp_info->port_priv->pd->local_dma_lkey;
2691 /* Initialize common receive WR fields */
2692 recv_wr.next = NULL;
2693 recv_wr.sg_list = &sg_list;
2694 recv_wr.num_sge = 1;
2697 /* Allocate and map receive buffer */
2702 mad_priv = alloc_mad_private(port_mad_size(qp_info->port_priv),
2709 sg_list.length = mad_priv_dma_size(mad_priv);
2710 sg_list.addr = ib_dma_map_single(qp_info->port_priv->device,
2712 mad_priv_dma_size(mad_priv),
2714 if (unlikely(ib_dma_mapping_error(qp_info->port_priv->device,
2720 mad_priv->header.mapping = sg_list.addr;
2721 mad_priv->header.mad_list.mad_queue = recv_queue;
2722 mad_priv->header.mad_list.cqe.done = ib_mad_recv_done;
2723 recv_wr.wr_cqe = &mad_priv->header.mad_list.cqe;
2725 /* Post receive WR */
2726 spin_lock_irqsave(&recv_queue->lock, flags);
2727 post = (++recv_queue->count < recv_queue->max_active);
2728 list_add_tail(&mad_priv->header.mad_list.list, &recv_queue->list);
2729 spin_unlock_irqrestore(&recv_queue->lock, flags);
2730 ret = ib_post_recv(qp_info->qp, &recv_wr, NULL);
2732 spin_lock_irqsave(&recv_queue->lock, flags);
2733 list_del(&mad_priv->header.mad_list.list);
2734 recv_queue->count--;
2735 spin_unlock_irqrestore(&recv_queue->lock, flags);
2736 ib_dma_unmap_single(qp_info->port_priv->device,
2737 mad_priv->header.mapping,
2738 mad_priv_dma_size(mad_priv),
2741 dev_err(&qp_info->port_priv->device->dev,
2742 "ib_post_recv failed: %d\n", ret);
2751 * Return all the posted receive MADs
2753 static void cleanup_recv_queue(struct ib_mad_qp_info *qp_info)
2755 struct ib_mad_private_header *mad_priv_hdr;
2756 struct ib_mad_private *recv;
2757 struct ib_mad_list_head *mad_list;
2762 while (!list_empty(&qp_info->recv_queue.list)) {
2764 mad_list = list_entry(qp_info->recv_queue.list.next,
2765 struct ib_mad_list_head, list);
2766 mad_priv_hdr = container_of(mad_list,
2767 struct ib_mad_private_header,
2769 recv = container_of(mad_priv_hdr, struct ib_mad_private,
2772 /* Remove from posted receive MAD list */
2773 list_del(&mad_list->list);
2775 ib_dma_unmap_single(qp_info->port_priv->device,
2776 recv->header.mapping,
2777 mad_priv_dma_size(recv),
2782 qp_info->recv_queue.count = 0;
2788 static int ib_mad_port_start(struct ib_mad_port_private *port_priv)
2791 struct ib_qp_attr *attr;
2795 attr = kmalloc(sizeof *attr, GFP_KERNEL);
2799 ret = ib_find_pkey(port_priv->device, port_priv->port_num,
2800 IB_DEFAULT_PKEY_FULL, &pkey_index);
2804 for (i = 0; i < IB_MAD_QPS_CORE; i++) {
2805 qp = port_priv->qp_info[i].qp;
2810 * PKey index for QP1 is irrelevant but
2811 * one is needed for the Reset to Init transition
2813 attr->qp_state = IB_QPS_INIT;
2814 attr->pkey_index = pkey_index;
2815 attr->qkey = (qp->qp_num == 0) ? 0 : IB_QP1_QKEY;
2816 ret = ib_modify_qp(qp, attr, IB_QP_STATE |
2817 IB_QP_PKEY_INDEX | IB_QP_QKEY);
2819 dev_err(&port_priv->device->dev,
2820 "Couldn't change QP%d state to INIT: %d\n",
2825 attr->qp_state = IB_QPS_RTR;
2826 ret = ib_modify_qp(qp, attr, IB_QP_STATE);
2828 dev_err(&port_priv->device->dev,
2829 "Couldn't change QP%d state to RTR: %d\n",
2834 attr->qp_state = IB_QPS_RTS;
2835 attr->sq_psn = IB_MAD_SEND_Q_PSN;
2836 ret = ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_SQ_PSN);
2838 dev_err(&port_priv->device->dev,
2839 "Couldn't change QP%d state to RTS: %d\n",
2845 ret = ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
2847 dev_err(&port_priv->device->dev,
2848 "Failed to request completion notification: %d\n",
2853 for (i = 0; i < IB_MAD_QPS_CORE; i++) {
2854 if (!port_priv->qp_info[i].qp)
2857 ret = ib_mad_post_receive_mads(&port_priv->qp_info[i], NULL);
2859 dev_err(&port_priv->device->dev,
2860 "Couldn't post receive WRs\n");
2869 static void qp_event_handler(struct ib_event *event, void *qp_context)
2871 struct ib_mad_qp_info *qp_info = qp_context;
2873 /* It's worse than that! He's dead, Jim! */
2874 dev_err(&qp_info->port_priv->device->dev,
2875 "Fatal error (%d) on MAD QP (%d)\n",
2876 event->event, qp_info->qp->qp_num);
2879 static void init_mad_queue(struct ib_mad_qp_info *qp_info,
2880 struct ib_mad_queue *mad_queue)
2882 mad_queue->qp_info = qp_info;
2883 mad_queue->count = 0;
2884 spin_lock_init(&mad_queue->lock);
2885 INIT_LIST_HEAD(&mad_queue->list);
2888 static void init_mad_qp(struct ib_mad_port_private *port_priv,
2889 struct ib_mad_qp_info *qp_info)
2891 qp_info->port_priv = port_priv;
2892 init_mad_queue(qp_info, &qp_info->send_queue);
2893 init_mad_queue(qp_info, &qp_info->recv_queue);
2894 INIT_LIST_HEAD(&qp_info->overflow_list);
2897 static int create_mad_qp(struct ib_mad_qp_info *qp_info,
2898 enum ib_qp_type qp_type)
2900 struct ib_qp_init_attr qp_init_attr;
2903 memset(&qp_init_attr, 0, sizeof qp_init_attr);
2904 qp_init_attr.send_cq = qp_info->port_priv->cq;
2905 qp_init_attr.recv_cq = qp_info->port_priv->cq;
2906 qp_init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
2907 qp_init_attr.cap.max_send_wr = mad_sendq_size;
2908 qp_init_attr.cap.max_recv_wr = mad_recvq_size;
2909 qp_init_attr.cap.max_send_sge = IB_MAD_SEND_REQ_MAX_SG;
2910 qp_init_attr.cap.max_recv_sge = IB_MAD_RECV_REQ_MAX_SG;
2911 qp_init_attr.qp_type = qp_type;
2912 qp_init_attr.port_num = qp_info->port_priv->port_num;
2913 qp_init_attr.qp_context = qp_info;
2914 qp_init_attr.event_handler = qp_event_handler;
2915 qp_info->qp = ib_create_qp(qp_info->port_priv->pd, &qp_init_attr);
2916 if (IS_ERR(qp_info->qp)) {
2917 dev_err(&qp_info->port_priv->device->dev,
2918 "Couldn't create ib_mad QP%d\n",
2919 get_spl_qp_index(qp_type));
2920 ret = PTR_ERR(qp_info->qp);
2923 /* Use minimum queue sizes unless the CQ is resized */
2924 qp_info->send_queue.max_active = mad_sendq_size;
2925 qp_info->recv_queue.max_active = mad_recvq_size;
2932 static void destroy_mad_qp(struct ib_mad_qp_info *qp_info)
2937 ib_destroy_qp(qp_info->qp);
2942 * Create the QP, PD, MR, and CQ if needed
2944 static int ib_mad_port_open(struct ib_device *device,
2948 struct ib_mad_port_private *port_priv;
2949 unsigned long flags;
2950 char name[sizeof "ib_mad123"];
2953 if (WARN_ON(rdma_max_mad_size(device, port_num) < IB_MGMT_MAD_SIZE))
2956 if (WARN_ON(rdma_cap_opa_mad(device, port_num) &&
2957 rdma_max_mad_size(device, port_num) < OPA_MGMT_MAD_SIZE))
2960 /* Create new device info */
2961 port_priv = kzalloc(sizeof *port_priv, GFP_KERNEL);
2965 port_priv->device = device;
2966 port_priv->port_num = port_num;
2967 spin_lock_init(&port_priv->reg_lock);
2968 init_mad_qp(port_priv, &port_priv->qp_info[0]);
2969 init_mad_qp(port_priv, &port_priv->qp_info[1]);
2971 cq_size = mad_sendq_size + mad_recvq_size;
2972 has_smi = rdma_cap_ib_smi(device, port_num);
2976 port_priv->pd = ib_alloc_pd(device, 0);
2977 if (IS_ERR(port_priv->pd)) {
2978 dev_err(&device->dev, "Couldn't create ib_mad PD\n");
2979 ret = PTR_ERR(port_priv->pd);
2983 port_priv->cq = ib_alloc_cq(port_priv->device, port_priv, cq_size, 0,
2984 IB_POLL_UNBOUND_WORKQUEUE);
2985 if (IS_ERR(port_priv->cq)) {
2986 dev_err(&device->dev, "Couldn't create ib_mad CQ\n");
2987 ret = PTR_ERR(port_priv->cq);
2992 ret = create_mad_qp(&port_priv->qp_info[0], IB_QPT_SMI);
2996 ret = create_mad_qp(&port_priv->qp_info[1], IB_QPT_GSI);
3000 snprintf(name, sizeof(name), "ib_mad%u", port_num);
3001 port_priv->wq = alloc_ordered_workqueue(name, WQ_MEM_RECLAIM);
3002 if (!port_priv->wq) {
3007 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
3008 list_add_tail(&port_priv->port_list, &ib_mad_port_list);
3009 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
3011 ret = ib_mad_port_start(port_priv);
3013 dev_err(&device->dev, "Couldn't start port\n");
3020 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
3021 list_del_init(&port_priv->port_list);
3022 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
3024 destroy_workqueue(port_priv->wq);
3026 destroy_mad_qp(&port_priv->qp_info[1]);
3028 destroy_mad_qp(&port_priv->qp_info[0]);
3030 ib_free_cq(port_priv->cq);
3031 cleanup_recv_queue(&port_priv->qp_info[1]);
3032 cleanup_recv_queue(&port_priv->qp_info[0]);
3034 ib_dealloc_pd(port_priv->pd);
3043 * If there are no classes using the port, free the port
3044 * resources (CQ, MR, PD, QP) and remove the port's info structure
3046 static int ib_mad_port_close(struct ib_device *device, u32 port_num)
3048 struct ib_mad_port_private *port_priv;
3049 unsigned long flags;
3051 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
3052 port_priv = __ib_get_mad_port(device, port_num);
3053 if (port_priv == NULL) {
3054 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
3055 dev_err(&device->dev, "Port %u not found\n", port_num);
3058 list_del_init(&port_priv->port_list);
3059 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
3061 destroy_workqueue(port_priv->wq);
3062 destroy_mad_qp(&port_priv->qp_info[1]);
3063 destroy_mad_qp(&port_priv->qp_info[0]);
3064 ib_free_cq(port_priv->cq);
3065 ib_dealloc_pd(port_priv->pd);
3066 cleanup_recv_queue(&port_priv->qp_info[1]);
3067 cleanup_recv_queue(&port_priv->qp_info[0]);
3068 /* XXX: Handle deallocation of MAD registration tables */
3075 static int ib_mad_init_device(struct ib_device *device)
3078 unsigned int count = 0;
3081 start = rdma_start_port(device);
3083 for (i = start; i <= rdma_end_port(device); i++) {
3084 if (!rdma_cap_ib_mad(device, i))
3087 ret = ib_mad_port_open(device, i);
3089 dev_err(&device->dev, "Couldn't open port %d\n", i);
3092 ret = ib_agent_port_open(device, i);
3094 dev_err(&device->dev,
3095 "Couldn't open port %d for agents\n", i);
3106 if (ib_mad_port_close(device, i))
3107 dev_err(&device->dev, "Couldn't close port %d\n", i);
3110 while (--i >= start) {
3111 if (!rdma_cap_ib_mad(device, i))
3114 if (ib_agent_port_close(device, i))
3115 dev_err(&device->dev,
3116 "Couldn't close port %d for agents\n", i);
3117 if (ib_mad_port_close(device, i))
3118 dev_err(&device->dev, "Couldn't close port %d\n", i);
3123 static void ib_mad_remove_device(struct ib_device *device, void *client_data)
3127 rdma_for_each_port (device, i) {
3128 if (!rdma_cap_ib_mad(device, i))
3131 if (ib_agent_port_close(device, i))
3132 dev_err(&device->dev,
3133 "Couldn't close port %d for agents\n", i);
3134 if (ib_mad_port_close(device, i))
3135 dev_err(&device->dev, "Couldn't close port %d\n", i);
3139 static struct ib_client mad_client = {
3141 .add = ib_mad_init_device,
3142 .remove = ib_mad_remove_device
3145 int ib_mad_init(void)
3147 mad_recvq_size = min(mad_recvq_size, IB_MAD_QP_MAX_SIZE);
3148 mad_recvq_size = max(mad_recvq_size, IB_MAD_QP_MIN_SIZE);
3150 mad_sendq_size = min(mad_sendq_size, IB_MAD_QP_MAX_SIZE);
3151 mad_sendq_size = max(mad_sendq_size, IB_MAD_QP_MIN_SIZE);
3153 INIT_LIST_HEAD(&ib_mad_port_list);
3155 if (ib_register_client(&mad_client)) {
3156 pr_err("Couldn't register ib_mad client\n");
3163 void ib_mad_cleanup(void)
3165 ib_unregister_client(&mad_client);