blob: 6c420736ce9313a7db37f19b1357471328a548db [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.
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04006 * Copyright (c) 2004-2007 Voltaire Corporation. All rights reserved.
Roland Dreier2a1d9b72005-08-10 23:03:10 -07007 * 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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 */
38
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080039#include <linux/slab.h>
40#include <linux/string.h>
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "agent.h"
Sean Hefty34816ad2005-10-25 10:51:39 -070043#include "smi.h"
Hal Rosenstock1bae4db2007-05-14 17:21:52 -040044#include "mad_priv.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 *
Ira Weiny73cdaae2015-05-31 17:15:31 -040057__ib_get_agent_port(const 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) {
Eli Cohenfac70d52010-09-27 17:51:11 -070062 if (entry->agent[1]->device == device &&
63 entry->agent[1]->port_num == port_num)
Sean Hefty34816ad2005-10-25 10:51:39 -070064 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 *
Ira Weiny73cdaae2015-05-31 17:15:31 -040070ib_get_agent_port(const 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
Ira Weinyc9082e52015-06-06 14:38:30 -040081void agent_send_response(const struct ib_mad_hdr *mad_hdr, const struct ib_grh *grh,
Ira Weiny73cdaae2015-05-31 17:15:31 -040082 const struct ib_wc *wc, const struct ib_device *device,
Ira Weinyc9082e52015-06-06 14:38:30 -040083 int port_num, int qpn, size_t resp_mad_len)
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;
Hal Rosenstock1bae4db2007-05-14 17:21:52 -040089 struct ib_mad_send_wr_private *mad_send_wr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Hal Rosenstock1bae4db2007-05-14 17:21:52 -040091 if (device->node_type == RDMA_NODE_IB_SWITCH)
92 port_priv = ib_get_agent_port(device, 0);
93 else
94 port_priv = ib_get_agent_port(device, port_num);
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 if (!port_priv) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -040097 dev_err(&device->dev, "Unable to find port agent\n");
Hal Rosenstock8fc394b2007-08-03 10:45:17 -070098 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
100
Sean Hefty34816ad2005-10-25 10:51:39 -0700101 agent = port_priv->agent[qpn];
102 ah = ib_create_ah_from_wc(agent->qp->pd, wc, grh, port_num);
103 if (IS_ERR(ah)) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -0400104 dev_err(&device->dev, "ib_create_ah_from_wc error %ld\n",
Michael Heinz1eba8432011-02-11 21:44:30 +0000105 PTR_ERR(ah));
Hal Rosenstock8fc394b2007-08-03 10:45:17 -0700106 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
108
Sean Hefty34816ad2005-10-25 10:51:39 -0700109 send_buf = ib_create_send_mad(agent, wc->src_qp, wc->pkey_index, 0,
Ira Weinyc9082e52015-06-06 14:38:30 -0400110 IB_MGMT_MAD_HDR,
111 resp_mad_len - IB_MGMT_MAD_HDR,
Ira Weinyda2dfaa2015-06-06 14:38:28 -0400112 GFP_KERNEL,
113 IB_MGMT_BASE_VERSION);
Sean Hefty34816ad2005-10-25 10:51:39 -0700114 if (IS_ERR(send_buf)) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -0400115 dev_err(&device->dev, "ib_create_send_mad error\n");
Sean Hefty34816ad2005-10-25 10:51:39 -0700116 goto err1;
117 }
118
Ira Weinyc9082e52015-06-06 14:38:30 -0400119 memcpy(send_buf->mad, mad_hdr, resp_mad_len);
Sean Hefty34816ad2005-10-25 10:51:39 -0700120 send_buf->ah = ah;
Hal Rosenstock1bae4db2007-05-14 17:21:52 -0400121
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
Hal Rosenstock8fc394b2007-08-03 10:45:17 -0700129 if (ib_post_send_mad(send_buf, NULL)) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -0400130 dev_err(&device->dev, "ib_post_send_mad error\n");
Sean Hefty34816ad2005-10-25 10:51:39 -0700131 goto err2;
132 }
Hal Rosenstock8fc394b2007-08-03 10:45:17 -0700133 return;
Sean Hefty34816ad2005-10-25 10:51:39 -0700134err2:
135 ib_free_send_mad(send_buf);
136err1:
137 ib_destroy_ah(ah);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
140static void agent_send_handler(struct ib_mad_agent *mad_agent,
141 struct ib_mad_send_wc *mad_send_wc)
142{
Sean Hefty34816ad2005-10-25 10:51:39 -0700143 ib_destroy_ah(mad_send_wc->send_buf->ah);
144 ib_free_send_mad(mad_send_wc->send_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
147int ib_agent_port_open(struct ib_device *device, int port_num)
148{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 struct ib_agent_port_private *port_priv;
150 unsigned long flags;
Sean Hefty34816ad2005-10-25 10:51:39 -0700151 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 /* Create new device info */
Roland Dreierde6eb662005-11-02 07:23:14 -0800154 port_priv = kzalloc(sizeof *port_priv, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if (!port_priv) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -0400156 dev_err(&device->dev, "No memory for ib_agent_port_private\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 ret = -ENOMEM;
158 goto error1;
159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Michael Wang29541e32015-05-05 14:50:33 +0200161 if (rdma_cap_ib_smi(device, port_num)) {
Eli Cohenfac70d52010-09-27 17:51:11 -0700162 /* 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,
165 &agent_send_handler,
Ira Weiny0f29b462014-08-08 19:00:55 -0400166 NULL, NULL, 0);
Eli Cohenfac70d52010-09-27 17:51:11 -0700167 if (IS_ERR(port_priv->agent[0])) {
168 ret = PTR_ERR(port_priv->agent[0]);
169 goto error2;
170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172
Sean Hefty34816ad2005-10-25 10:51:39 -0700173 /* Obtain send only MAD agent for GSI QP */
174 port_priv->agent[1] = ib_register_mad_agent(device, port_num,
175 IB_QPT_GSI, NULL, 0,
176 &agent_send_handler,
Ira Weiny0f29b462014-08-08 19:00:55 -0400177 NULL, NULL, 0);
Sean Hefty34816ad2005-10-25 10:51:39 -0700178 if (IS_ERR(port_priv->agent[1])) {
179 ret = PTR_ERR(port_priv->agent[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 goto error3;
181 }
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 spin_lock_irqsave(&ib_agent_port_list_lock, flags);
184 list_add_tail(&port_priv->port_list, &ib_agent_port_list);
185 spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
186
187 return 0;
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189error3:
Eli Cohenfac70d52010-09-27 17:51:11 -0700190 if (port_priv->agent[0])
191 ib_unregister_mad_agent(port_priv->agent[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192error2:
193 kfree(port_priv);
194error1:
195 return ret;
196}
197
198int ib_agent_port_close(struct ib_device *device, int port_num)
199{
200 struct ib_agent_port_private *port_priv;
201 unsigned long flags;
202
203 spin_lock_irqsave(&ib_agent_port_list_lock, flags);
Sean Hefty34816ad2005-10-25 10:51:39 -0700204 port_priv = __ib_get_agent_port(device, port_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (port_priv == NULL) {
206 spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
Ira Weiny7ef5d4b2014-08-08 19:00:53 -0400207 dev_err(&device->dev, "Port %d not found\n", port_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return -ENODEV;
209 }
210 list_del(&port_priv->port_list);
211 spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
212
Sean Hefty34816ad2005-10-25 10:51:39 -0700213 ib_unregister_mad_agent(port_priv->agent[1]);
Eli Cohenfac70d52010-09-27 17:51:11 -0700214 if (port_priv->agent[0])
215 ib_unregister_mad_agent(port_priv->agent[0]);
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 kfree(port_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return 0;
219}