Merge branch 'drm-patches' of ssh://master.kernel.org/pub/scm/linux/kernel/git/airlie...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / infiniband / core / agent.c
CommitLineData
1da177e4 1/*
2a1d9b7f
RD
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.
1bae4dbf 6 * Copyright (c) 2004-2007 Voltaire Corporation. All rights reserved.
2a1d9b7f 7 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
1da177e4
LT
8 *
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:
14 *
15 * Redistribution and use in source and binary forms, with or
16 * without modification, are permitted provided that the following
17 * conditions are met:
18 *
19 * - Redistributions of source code must retain the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer.
22 *
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.
27 *
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
35 * SOFTWARE.
36 *
1da177e4
LT
37 */
38
8c65b4a6
TS
39#include <linux/slab.h>
40#include <linux/string.h>
41
34816ad9
SH
42#include "agent.h"
43#include "smi.h"
1bae4dbf 44#include "mad_priv.h"
1da177e4 45
34816ad9 46#define SPFX "ib_agent: "
1da177e4 47
34816ad9
SH
48struct ib_agent_port_private {
49 struct list_head port_list;
50 struct ib_mad_agent *agent[2];
51};
1da177e4 52
34816ad9 53static DEFINE_SPINLOCK(ib_agent_port_list_lock);
1da177e4
LT
54static LIST_HEAD(ib_agent_port_list);
55
34816ad9
SH
56static struct ib_agent_port_private *
57__ib_get_agent_port(struct ib_device *device, int port_num)
1da177e4
LT
58{
59 struct ib_agent_port_private *entry;
60
34816ad9
SH
61 list_for_each_entry(entry, &ib_agent_port_list, port_list) {
62 if (entry->agent[0]->device == device &&
63 entry->agent[0]->port_num == port_num)
64 return entry;
1da177e4
LT
65 }
66 return NULL;
67}
68
34816ad9
SH
69static struct ib_agent_port_private *
70ib_get_agent_port(struct ib_device *device, int port_num)
1da177e4
LT
71{
72 struct ib_agent_port_private *entry;
73 unsigned long flags;
74
75 spin_lock_irqsave(&ib_agent_port_list_lock, flags);
34816ad9 76 entry = __ib_get_agent_port(device, port_num);
1da177e4 77 spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
1da177e4
LT
78 return entry;
79}
80
34816ad9
SH
81int agent_send_response(struct ib_mad *mad, struct ib_grh *grh,
82 struct ib_wc *wc, struct ib_device *device,
83 int port_num, int qpn)
1da177e4 84{
34816ad9
SH
85 struct ib_agent_port_private *port_priv;
86 struct ib_mad_agent *agent;
87 struct ib_mad_send_buf *send_buf;
88 struct ib_ah *ah;
89 int ret;
1bae4dbf
HR
90 struct ib_mad_send_wr_private *mad_send_wr;
91
92 if (device->node_type == RDMA_NODE_IB_SWITCH)
93 port_priv = ib_get_agent_port(device, 0);
94 else
95 port_priv = ib_get_agent_port(device, port_num);
1da177e4 96
34816ad9
SH
97 if (!port_priv) {
98 printk(KERN_ERR SPFX "Unable to find port agent\n");
99 return -ENODEV;
1da177e4
LT
100 }
101
34816ad9
SH
102 agent = port_priv->agent[qpn];
103 ah = ib_create_ah_from_wc(agent->qp->pd, wc, grh, port_num);
104 if (IS_ERR(ah)) {
105 ret = PTR_ERR(ah);
106 printk(KERN_ERR SPFX "ib_create_ah_from_wc error:%d\n", ret);
107 return ret;
1da177e4
LT
108 }
109
34816ad9
SH
110 send_buf = ib_create_send_mad(agent, wc->src_qp, wc->pkey_index, 0,
111 IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
112 GFP_KERNEL);
113 if (IS_ERR(send_buf)) {
114 ret = PTR_ERR(send_buf);
115 printk(KERN_ERR SPFX "ib_create_send_mad error:%d\n", ret);
116 goto err1;
1da177e4 117 }
1da177e4 118
34816ad9
SH
119 memcpy(send_buf->mad, mad, sizeof *mad);
120 send_buf->ah = ah;
1bae4dbf
HR
121
122 if (device->node_type == RDMA_NODE_IB_SWITCH) {
123 mad_send_wr = container_of(send_buf,
124 struct ib_mad_send_wr_private,
125 send_buf);
126 mad_send_wr->send_wr.wr.ud.port_num = port_num;
127 }
128
34816ad9
SH
129 if ((ret = ib_post_send_mad(send_buf, NULL))) {
130 printk(KERN_ERR SPFX "ib_post_send_mad error:%d\n", ret);
131 goto err2;
1da177e4 132 }
34816ad9
SH
133 return 0;
134err2:
135 ib_free_send_mad(send_buf);
136err1:
137 ib_destroy_ah(ah);
1da177e4
LT
138 return ret;
139}
140
1da177e4
LT
141static void agent_send_handler(struct ib_mad_agent *mad_agent,
142 struct ib_mad_send_wc *mad_send_wc)
143{
34816ad9
SH
144 ib_destroy_ah(mad_send_wc->send_buf->ah);
145 ib_free_send_mad(mad_send_wc->send_buf);
1da177e4
LT
146}
147
148int ib_agent_port_open(struct ib_device *device, int port_num)
149{
1da177e4
LT
150 struct ib_agent_port_private *port_priv;
151 unsigned long flags;
34816ad9 152 int ret;
1da177e4
LT
153
154 /* Create new device info */
de6eb66b 155 port_priv = kzalloc(sizeof *port_priv, GFP_KERNEL);
1da177e4
LT
156 if (!port_priv) {
157 printk(KERN_ERR SPFX "No memory for ib_agent_port_private\n");
158 ret = -ENOMEM;
159 goto error1;
160 }
1da177e4 161
34816ad9
SH
162 /* Obtain send only MAD agent for SMI QP */
163 port_priv->agent[0] = ib_register_mad_agent(device, port_num,
164 IB_QPT_SMI, NULL, 0,
1da177e4 165 &agent_send_handler,
34816ad9
SH
166 NULL, NULL);
167 if (IS_ERR(port_priv->agent[0])) {
168 ret = PTR_ERR(port_priv->agent[0]);
1da177e4
LT
169 goto error2;
170 }
171
34816ad9
SH
172 /* Obtain send only MAD agent for GSI QP */
173 port_priv->agent[1] = ib_register_mad_agent(device, port_num,
174 IB_QPT_GSI, NULL, 0,
175 &agent_send_handler,
176 NULL, NULL);
177 if (IS_ERR(port_priv->agent[1])) {
178 ret = PTR_ERR(port_priv->agent[1]);
1da177e4
LT
179 goto error3;
180 }
181
1da177e4
LT
182 spin_lock_irqsave(&ib_agent_port_list_lock, flags);
183 list_add_tail(&port_priv->port_list, &ib_agent_port_list);
184 spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
185
186 return 0;
187
1da177e4 188error3:
34816ad9 189 ib_unregister_mad_agent(port_priv->agent[0]);
1da177e4
LT
190error2:
191 kfree(port_priv);
192error1:
193 return ret;
194}
195
196int ib_agent_port_close(struct ib_device *device, int port_num)
197{
198 struct ib_agent_port_private *port_priv;
199 unsigned long flags;
200
201 spin_lock_irqsave(&ib_agent_port_list_lock, flags);
34816ad9 202 port_priv = __ib_get_agent_port(device, port_num);
1da177e4
LT
203 if (port_priv == NULL) {
204 spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
205 printk(KERN_ERR SPFX "Port %d not found\n", port_num);
206 return -ENODEV;
207 }
208 list_del(&port_priv->port_list);
209 spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
210
34816ad9
SH
211 ib_unregister_mad_agent(port_priv->agent[1]);
212 ib_unregister_mad_agent(port_priv->agent[0]);
1da177e4 213 kfree(port_priv);
1da177e4
LT
214 return 0;
215}