blob: ecd1a3057c61bf41e9f09c5be5727de78866620d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Roland Dreier2a1d9b72005-08-10 23:03:10 -07002 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
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 *
37 * $Id: agent.c 1389 2004-12-27 22:56:47Z roland $
38 */
39
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080040#include <linux/slab.h>
41#include <linux/string.h>
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "agent.h"
Sean Hefty34816ad2005-10-25 10:51:39 -070044#include "smi.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Sean Hefty34816ad2005-10-25 10:51:39 -070046#define SPFX "ib_agent: "
47
48struct ib_agent_port_private {
49 struct list_head port_list;
50 struct ib_mad_agent *agent[2];
51};
52
53static DEFINE_SPINLOCK(ib_agent_port_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static LIST_HEAD(ib_agent_port_list);
55
Sean Hefty34816ad2005-10-25 10:51:39 -070056static struct ib_agent_port_private *
57__ib_get_agent_port(struct ib_device *device, int port_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 struct ib_agent_port_private *entry;
60
Sean Hefty34816ad2005-10-25 10:51:39 -070061 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 }
66 return NULL;
67}
68
Sean Hefty34816ad2005-10-25 10:51:39 -070069static struct ib_agent_port_private *
70ib_get_agent_port(struct ib_device *device, int port_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 struct ib_agent_port_private *entry;
73 unsigned long flags;
74
75 spin_lock_irqsave(&ib_agent_port_list_lock, flags);
Sean Hefty34816ad2005-10-25 10:51:39 -070076 entry = __ib_get_agent_port(device, port_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return entry;
79}
80
Sean Hefty34816ad2005-10-25 10:51:39 -070081int 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
85 struct ib_agent_port_private *port_priv;
Sean Hefty34816ad2005-10-25 10:51:39 -070086 struct ib_mad_agent *agent;
87 struct ib_mad_send_buf *send_buf;
88 struct ib_ah *ah;
89 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Sean Hefty34816ad2005-10-25 10:51:39 -070091 port_priv = ib_get_agent_port(device, port_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 if (!port_priv) {
Sean Hefty34816ad2005-10-25 10:51:39 -070093 printk(KERN_ERR SPFX "Unable to find port agent\n");
94 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 }
96
Sean Hefty34816ad2005-10-25 10:51:39 -070097 agent = port_priv->agent[qpn];
98 ah = ib_create_ah_from_wc(agent->qp->pd, wc, grh, port_num);
99 if (IS_ERR(ah)) {
100 ret = PTR_ERR(ah);
101 printk(KERN_ERR SPFX "ib_create_ah_from_wc error:%d\n", ret);
102 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
104
Sean Hefty34816ad2005-10-25 10:51:39 -0700105 send_buf = ib_create_send_mad(agent, wc->src_qp, wc->pkey_index, 0,
106 IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
107 GFP_KERNEL);
108 if (IS_ERR(send_buf)) {
109 ret = PTR_ERR(send_buf);
110 printk(KERN_ERR SPFX "ib_create_send_mad error:%d\n", ret);
111 goto err1;
112 }
113
114 memcpy(send_buf->mad, mad, sizeof *mad);
115 send_buf->ah = ah;
116 if ((ret = ib_post_send_mad(send_buf, NULL))) {
117 printk(KERN_ERR SPFX "ib_post_send_mad error:%d\n", ret);
118 goto err2;
119 }
120 return 0;
121err2:
122 ib_free_send_mad(send_buf);
123err1:
124 ib_destroy_ah(ah);
125 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
128static void agent_send_handler(struct ib_mad_agent *mad_agent,
129 struct ib_mad_send_wc *mad_send_wc)
130{
Sean Hefty34816ad2005-10-25 10:51:39 -0700131 ib_destroy_ah(mad_send_wc->send_buf->ah);
132 ib_free_send_mad(mad_send_wc->send_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
135int ib_agent_port_open(struct ib_device *device, int port_num)
136{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 struct ib_agent_port_private *port_priv;
138 unsigned long flags;
Sean Hefty34816ad2005-10-25 10:51:39 -0700139 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 /* Create new device info */
Roland Dreierde6eb662005-11-02 07:23:14 -0800142 port_priv = kzalloc(sizeof *port_priv, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 if (!port_priv) {
144 printk(KERN_ERR SPFX "No memory for ib_agent_port_private\n");
145 ret = -ENOMEM;
146 goto error1;
147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Sean Hefty34816ad2005-10-25 10:51:39 -0700149 /* Obtain send only MAD agent for SMI QP */
150 port_priv->agent[0] = ib_register_mad_agent(device, port_num,
151 IB_QPT_SMI, NULL, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 &agent_send_handler,
Sean Hefty34816ad2005-10-25 10:51:39 -0700153 NULL, NULL);
154 if (IS_ERR(port_priv->agent[0])) {
155 ret = PTR_ERR(port_priv->agent[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 goto error2;
157 }
158
Sean Hefty34816ad2005-10-25 10:51:39 -0700159 /* Obtain send only MAD agent for GSI QP */
160 port_priv->agent[1] = ib_register_mad_agent(device, port_num,
161 IB_QPT_GSI, NULL, 0,
162 &agent_send_handler,
163 NULL, NULL);
164 if (IS_ERR(port_priv->agent[1])) {
165 ret = PTR_ERR(port_priv->agent[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 goto error3;
167 }
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 spin_lock_irqsave(&ib_agent_port_list_lock, flags);
170 list_add_tail(&port_priv->port_list, &ib_agent_port_list);
171 spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
172
173 return 0;
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175error3:
Sean Hefty34816ad2005-10-25 10:51:39 -0700176 ib_unregister_mad_agent(port_priv->agent[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177error2:
178 kfree(port_priv);
179error1:
180 return ret;
181}
182
183int ib_agent_port_close(struct ib_device *device, int port_num)
184{
185 struct ib_agent_port_private *port_priv;
186 unsigned long flags;
187
188 spin_lock_irqsave(&ib_agent_port_list_lock, flags);
Sean Hefty34816ad2005-10-25 10:51:39 -0700189 port_priv = __ib_get_agent_port(device, port_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (port_priv == NULL) {
191 spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
192 printk(KERN_ERR SPFX "Port %d not found\n", port_num);
193 return -ENODEV;
194 }
195 list_del(&port_priv->port_list);
196 spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
197
Sean Hefty34816ad2005-10-25 10:51:39 -0700198 ib_unregister_mad_agent(port_priv->agent[1]);
199 ib_unregister_mad_agent(port_priv->agent[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 kfree(port_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return 0;
202}