2 * Copyright (c) 2004, 2005 Mellanox Technologies Ltd. All rights reserved.
3 * Copyright (c) 2004, 2005 Infinicon Corporation. All rights reserved.
4 * Copyright (c) 2004, 2005 Intel Corporation. All rights reserved.
5 * Copyright (c) 2004, 2005 Topspin Corporation. All rights reserved.
6 * Copyright (c) 2004, 2005 Voltaire Corporation. All rights reserved.
7 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
9 * This software is available to you under a choice of one of two
10 * licenses. You may choose to be licensed under the terms of the GNU
11 * General Public License (GPL) Version 2, available from the file
12 * COPYING in the main directory of this source tree, or the
13 * OpenIB.org BSD license below:
15 * Redistribution and use in source and binary forms, with or
16 * without modification, are permitted provided that the following
19 * - Redistributions of source code must retain the above
20 * copyright notice, this list of conditions and the following
23 * - Redistributions in binary form must reproduce the above
24 * copyright notice, this list of conditions and the following
25 * disclaimer in the documentation and/or other materials
26 * provided with the distribution.
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
32 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
33 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37 * $Id: agent.c 1389 2004-12-27 22:56:47Z roland $
43 #define SPFX "ib_agent: "
45 struct ib_agent_port_private {
46 struct list_head port_list;
47 struct ib_mad_agent *agent[2];
50 static DEFINE_SPINLOCK(ib_agent_port_list_lock);
51 static LIST_HEAD(ib_agent_port_list);
53 static struct ib_agent_port_private *
54 __ib_get_agent_port(struct ib_device *device, int port_num)
56 struct ib_agent_port_private *entry;
58 list_for_each_entry(entry, &ib_agent_port_list, port_list) {
59 if (entry->agent[0]->device == device &&
60 entry->agent[0]->port_num == port_num)
66 static struct ib_agent_port_private *
67 ib_get_agent_port(struct ib_device *device, int port_num)
69 struct ib_agent_port_private *entry;
72 spin_lock_irqsave(&ib_agent_port_list_lock, flags);
73 entry = __ib_get_agent_port(device, port_num);
74 spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
78 int smi_check_local_dr_smp(struct ib_smp *smp,
79 struct ib_device *device,
82 struct ib_agent_port_private *port_priv;
84 if (smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
87 port_priv = ib_get_agent_port(device, port_num);
89 printk(KERN_DEBUG SPFX "smi_check_local_dr_smp %s port %d "
90 "not open\n", device->name, port_num);
94 return smi_check_local_smp(port_priv->agent[0], smp);
97 int agent_send_response(struct ib_mad *mad, struct ib_grh *grh,
98 struct ib_wc *wc, struct ib_device *device,
99 int port_num, int qpn)
101 struct ib_agent_port_private *port_priv;
102 struct ib_mad_agent *agent;
103 struct ib_mad_send_buf *send_buf;
107 port_priv = ib_get_agent_port(device, port_num);
109 printk(KERN_ERR SPFX "Unable to find port agent\n");
113 agent = port_priv->agent[qpn];
114 ah = ib_create_ah_from_wc(agent->qp->pd, wc, grh, port_num);
117 printk(KERN_ERR SPFX "ib_create_ah_from_wc error:%d\n", ret);
121 send_buf = ib_create_send_mad(agent, wc->src_qp, wc->pkey_index, 0,
122 IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
124 if (IS_ERR(send_buf)) {
125 ret = PTR_ERR(send_buf);
126 printk(KERN_ERR SPFX "ib_create_send_mad error:%d\n", ret);
130 memcpy(send_buf->mad, mad, sizeof *mad);
132 if ((ret = ib_post_send_mad(send_buf, NULL))) {
133 printk(KERN_ERR SPFX "ib_post_send_mad error:%d\n", ret);
138 ib_free_send_mad(send_buf);
144 static void agent_send_handler(struct ib_mad_agent *mad_agent,
145 struct ib_mad_send_wc *mad_send_wc)
147 ib_destroy_ah(mad_send_wc->send_buf->ah);
148 ib_free_send_mad(mad_send_wc->send_buf);
151 int ib_agent_port_open(struct ib_device *device, int port_num)
153 struct ib_agent_port_private *port_priv;
157 /* Create new device info */
158 port_priv = kmalloc(sizeof *port_priv, GFP_KERNEL);
160 printk(KERN_ERR SPFX "No memory for ib_agent_port_private\n");
164 memset(port_priv, 0, sizeof *port_priv);
166 /* Obtain send only MAD agent for SMI QP */
167 port_priv->agent[0] = ib_register_mad_agent(device, port_num,
171 if (IS_ERR(port_priv->agent[0])) {
172 ret = PTR_ERR(port_priv->agent[0]);
176 /* Obtain send only MAD agent for GSI QP */
177 port_priv->agent[1] = ib_register_mad_agent(device, port_num,
181 if (IS_ERR(port_priv->agent[1])) {
182 ret = PTR_ERR(port_priv->agent[1]);
186 spin_lock_irqsave(&ib_agent_port_list_lock, flags);
187 list_add_tail(&port_priv->port_list, &ib_agent_port_list);
188 spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
193 ib_unregister_mad_agent(port_priv->agent[0]);
200 int ib_agent_port_close(struct ib_device *device, int port_num)
202 struct ib_agent_port_private *port_priv;
205 spin_lock_irqsave(&ib_agent_port_list_lock, flags);
206 port_priv = __ib_get_agent_port(device, port_num);
207 if (port_priv == NULL) {
208 spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
209 printk(KERN_ERR SPFX "Port %d not found\n", port_num);
212 list_del(&port_priv->port_list);
213 spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
215 ib_unregister_mad_agent(port_priv->agent[1]);
216 ib_unregister_mad_agent(port_priv->agent[0]);