IB/mad: Support alternate Base Versions when creating MADs
[firefly-linux-kernel-4.4.55.git] / drivers / infiniband / core / agent.c
1 /*
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-2007 Voltaire Corporation.  All rights reserved.
7  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
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  *
37  */
38
39 #include <linux/slab.h>
40 #include <linux/string.h>
41
42 #include "agent.h"
43 #include "smi.h"
44 #include "mad_priv.h"
45
46 #define SPFX "ib_agent: "
47
48 struct ib_agent_port_private {
49         struct list_head port_list;
50         struct ib_mad_agent *agent[2];
51 };
52
53 static DEFINE_SPINLOCK(ib_agent_port_list_lock);
54 static LIST_HEAD(ib_agent_port_list);
55
56 static struct ib_agent_port_private *
57 __ib_get_agent_port(const struct ib_device *device, int port_num)
58 {
59         struct ib_agent_port_private *entry;
60
61         list_for_each_entry(entry, &ib_agent_port_list, port_list) {
62                 if (entry->agent[1]->device == device &&
63                     entry->agent[1]->port_num == port_num)
64                         return entry;
65         }
66         return NULL;
67 }
68
69 static struct ib_agent_port_private *
70 ib_get_agent_port(const struct ib_device *device, int port_num)
71 {
72         struct ib_agent_port_private *entry;
73         unsigned long flags;
74
75         spin_lock_irqsave(&ib_agent_port_list_lock, flags);
76         entry = __ib_get_agent_port(device, port_num);
77         spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
78         return entry;
79 }
80
81 void agent_send_response(const struct ib_mad *mad, const struct ib_grh *grh,
82                          const struct ib_wc *wc, const struct ib_device *device,
83                          int port_num, int qpn)
84 {
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         struct ib_mad_send_wr_private *mad_send_wr;
90
91         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
96         if (!port_priv) {
97                 dev_err(&device->dev, "Unable to find port agent\n");
98                 return;
99         }
100
101         agent = port_priv->agent[qpn];
102         ah = ib_create_ah_from_wc(agent->qp->pd, wc, grh, port_num);
103         if (IS_ERR(ah)) {
104                 dev_err(&device->dev, "ib_create_ah_from_wc error %ld\n",
105                         PTR_ERR(ah));
106                 return;
107         }
108
109         send_buf = ib_create_send_mad(agent, wc->src_qp, wc->pkey_index, 0,
110                                       IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
111                                       GFP_KERNEL,
112                                       IB_MGMT_BASE_VERSION);
113         if (IS_ERR(send_buf)) {
114                 dev_err(&device->dev, "ib_create_send_mad error\n");
115                 goto err1;
116         }
117
118         memcpy(send_buf->mad, mad, sizeof *mad);
119         send_buf->ah = ah;
120
121         if (device->node_type == RDMA_NODE_IB_SWITCH) {
122                 mad_send_wr = container_of(send_buf,
123                                            struct ib_mad_send_wr_private,
124                                            send_buf);
125                 mad_send_wr->send_wr.wr.ud.port_num = port_num;
126         }
127
128         if (ib_post_send_mad(send_buf, NULL)) {
129                 dev_err(&device->dev, "ib_post_send_mad error\n");
130                 goto err2;
131         }
132         return;
133 err2:
134         ib_free_send_mad(send_buf);
135 err1:
136         ib_destroy_ah(ah);
137 }
138
139 static void agent_send_handler(struct ib_mad_agent *mad_agent,
140                                struct ib_mad_send_wc *mad_send_wc)
141 {
142         ib_destroy_ah(mad_send_wc->send_buf->ah);
143         ib_free_send_mad(mad_send_wc->send_buf);
144 }
145
146 int ib_agent_port_open(struct ib_device *device, int port_num)
147 {
148         struct ib_agent_port_private *port_priv;
149         unsigned long flags;
150         int ret;
151
152         /* Create new device info */
153         port_priv = kzalloc(sizeof *port_priv, GFP_KERNEL);
154         if (!port_priv) {
155                 dev_err(&device->dev, "No memory for ib_agent_port_private\n");
156                 ret = -ENOMEM;
157                 goto error1;
158         }
159
160         if (rdma_cap_ib_smi(device, port_num)) {
161                 /* Obtain send only MAD agent for SMI QP */
162                 port_priv->agent[0] = ib_register_mad_agent(device, port_num,
163                                                             IB_QPT_SMI, NULL, 0,
164                                                             &agent_send_handler,
165                                                             NULL, NULL, 0);
166                 if (IS_ERR(port_priv->agent[0])) {
167                         ret = PTR_ERR(port_priv->agent[0]);
168                         goto error2;
169                 }
170         }
171
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, 0);
177         if (IS_ERR(port_priv->agent[1])) {
178                 ret = PTR_ERR(port_priv->agent[1]);
179                 goto error3;
180         }
181
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
188 error3:
189         if (port_priv->agent[0])
190                 ib_unregister_mad_agent(port_priv->agent[0]);
191 error2:
192         kfree(port_priv);
193 error1:
194         return ret;
195 }
196
197 int ib_agent_port_close(struct ib_device *device, int port_num)
198 {
199         struct ib_agent_port_private *port_priv;
200         unsigned long flags;
201
202         spin_lock_irqsave(&ib_agent_port_list_lock, flags);
203         port_priv = __ib_get_agent_port(device, port_num);
204         if (port_priv == NULL) {
205                 spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
206                 dev_err(&device->dev, "Port %d not found\n", port_num);
207                 return -ENODEV;
208         }
209         list_del(&port_priv->port_list);
210         spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
211
212         ib_unregister_mad_agent(port_priv->agent[1]);
213         if (port_priv->agent[0])
214                 ib_unregister_mad_agent(port_priv->agent[0]);
215
216         kfree(port_priv);
217         return 0;
218 }