blob: c50596f7f98a52f2cdc6afacac3d98d9a7d348fb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Hal Rosenstockde493d42007-04-02 11:24:07 -04002 * Copyright (c) 2004-2007 Voltaire, Inc. All rights reserved.
Hal Rosenstockfa619a72005-07-27 11:45:37 -07003 * Copyright (c) 2005 Intel Corporation. All rights reserved.
4 * Copyright (c) 2005 Mellanox Technologies Ltd. All rights reserved.
Hal Rosenstockb76aabc2009-09-07 08:28:48 -07005 * Copyright (c) 2009 HNR Consulting. All rights reserved.
Ira Weiny8e4349d2015-06-10 16:16:48 -04006 * Copyright (c) 2014 Intel Corporation. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This software is available to you under a choice of one of two
9 * licenses. You may choose to be licensed under the terms of the GNU
10 * General Public License (GPL) Version 2, available from the file
11 * COPYING in the main directory of this source tree, or the
12 * OpenIB.org BSD license below:
13 *
14 * Redistribution and use in source and binary forms, with or
15 * without modification, are permitted provided that the following
16 * conditions are met:
17 *
18 * - Redistributions of source code must retain the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer.
21 *
22 * - Redistributions in binary form must reproduce the above
23 * copyright notice, this list of conditions and the following
24 * disclaimer in the documentation and/or other materials
25 * provided with the distribution.
26 *
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
31 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
32 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
33 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 * SOFTWARE.
35 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 */
Ira Weiny7ef5d4b2014-08-08 19:00:53 -040037
38#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/slab.h>
Paul Gortmakere4dd23d2011-05-27 15:35:46 -040042#include <linux/module.h>
Daniel Jurgens47a2b332017-05-19 15:48:54 +030043#include <linux/security.h>
Jack Morgenstein9874e742006-06-17 20:37:34 -070044#include <rdma/ib_cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include "mad_priv.h"
Daniel Jurgens47a2b332017-05-19 15:48:54 +030047#include "core_priv.h"
Hal Rosenstockfa619a72005-07-27 11:45:37 -070048#include "mad_rmpp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include "smi.h"
Ira Weiny8e4349d2015-06-10 16:16:48 -040050#include "opa_smi.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include "agent.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Roland Dreier16933952010-05-23 21:39:31 -070053static int mad_sendq_size = IB_MAD_QP_SEND_SIZE;
54static int mad_recvq_size = IB_MAD_QP_RECV_SIZE;
Hal Rosenstockb76aabc2009-09-07 08:28:48 -070055
56module_param_named(send_queue_size, mad_sendq_size, int, 0444);
57MODULE_PARM_DESC(send_queue_size, "Size of send queue in number of work requests");
58module_param_named(recv_queue_size, mad_recvq_size, int, 0444);
59MODULE_PARM_DESC(recv_queue_size, "Size of receive queue in number of work requests");
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static struct list_head ib_mad_port_list;
62static u32 ib_mad_client_id = 0;
63
64/* Port list lock */
Roland Dreier6276e082009-09-05 20:24:23 -070065static DEFINE_SPINLOCK(ib_mad_port_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/* Forward declarations */
68static int method_in_use(struct ib_mad_mgmt_method_table **method,
69 struct ib_mad_reg_req *mad_reg_req);
70static void remove_mad_reg_req(struct ib_mad_agent_private *priv);
71static struct ib_mad_agent_private *find_mad_agent(
72 struct ib_mad_port_private *port_priv,
Ira Weinyd94bd262015-06-06 14:38:22 -040073 const struct ib_mad_hdr *mad);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
75 struct ib_mad_private *mad);
76static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv);
David Howellsc4028952006-11-22 14:57:56 +000077static void timeout_sends(struct work_struct *work);
78static void local_completions(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
80 struct ib_mad_agent_private *agent_priv,
81 u8 mgmt_class);
82static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
83 struct ib_mad_agent_private *agent_priv);
Christoph Hellwigd53e11f2016-01-05 22:46:12 -080084static bool ib_mad_send_error(struct ib_mad_port_private *port_priv,
85 struct ib_wc *wc);
86static void ib_mad_send_done(struct ib_cq *cq, struct ib_wc *wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88/*
89 * Returns a ib_mad_port_private structure or NULL for a device/port
90 * Assumes ib_mad_port_list_lock is being held
91 */
92static inline struct ib_mad_port_private *
93__ib_get_mad_port(struct ib_device *device, int port_num)
94{
95 struct ib_mad_port_private *entry;
96
97 list_for_each_entry(entry, &ib_mad_port_list, port_list) {
98 if (entry->device == device && entry->port_num == port_num)
99 return entry;
100 }
101 return NULL;
102}
103
104/*
105 * Wrapper function to return a ib_mad_port_private structure or NULL
106 * for a device/port
107 */
108static inline struct ib_mad_port_private *
109ib_get_mad_port(struct ib_device *device, int port_num)
110{
111 struct ib_mad_port_private *entry;
112 unsigned long flags;
113
114 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
115 entry = __ib_get_mad_port(device, port_num);
116 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
117
118 return entry;
119}
120
121static inline u8 convert_mgmt_class(u8 mgmt_class)
122{
123 /* Alias IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE to 0 */
124 return mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE ?
125 0 : mgmt_class;
126}
127
128static int get_spl_qp_index(enum ib_qp_type qp_type)
129{
130 switch (qp_type)
131 {
132 case IB_QPT_SMI:
133 return 0;
134 case IB_QPT_GSI:
135 return 1;
136 default:
137 return -1;
138 }
139}
140
141static int vendor_class_index(u8 mgmt_class)
142{
143 return mgmt_class - IB_MGMT_CLASS_VENDOR_RANGE2_START;
144}
145
146static int is_vendor_class(u8 mgmt_class)
147{
148 if ((mgmt_class < IB_MGMT_CLASS_VENDOR_RANGE2_START) ||
149 (mgmt_class > IB_MGMT_CLASS_VENDOR_RANGE2_END))
150 return 0;
151 return 1;
152}
153
154static int is_vendor_oui(char *oui)
155{
156 if (oui[0] || oui[1] || oui[2])
157 return 1;
158 return 0;
159}
160
161static int is_vendor_method_in_use(
162 struct ib_mad_mgmt_vendor_class *vendor_class,
163 struct ib_mad_reg_req *mad_reg_req)
164{
165 struct ib_mad_mgmt_method_table *method;
166 int i;
167
168 for (i = 0; i < MAX_MGMT_OUI; i++) {
169 if (!memcmp(vendor_class->oui[i], mad_reg_req->oui, 3)) {
170 method = vendor_class->method_table[i];
171 if (method) {
172 if (method_in_use(&method, mad_reg_req))
173 return 1;
174 else
175 break;
176 }
177 }
178 }
179 return 0;
180}
181
Ira Weiny96909302015-05-08 14:27:22 -0400182int ib_response_mad(const struct ib_mad_hdr *hdr)
Sean Hefty2527e682006-07-20 11:25:50 +0300183{
Ira Weiny96909302015-05-08 14:27:22 -0400184 return ((hdr->method & IB_MGMT_METHOD_RESP) ||
185 (hdr->method == IB_MGMT_METHOD_TRAP_REPRESS) ||
186 ((hdr->mgmt_class == IB_MGMT_CLASS_BM) &&
187 (hdr->attr_mod & IB_BM_ATTR_MOD_RESP)));
Sean Hefty2527e682006-07-20 11:25:50 +0300188}
189EXPORT_SYMBOL(ib_response_mad);
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/*
192 * ib_register_mad_agent - Register to send/receive MADs
193 */
194struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
195 u8 port_num,
196 enum ib_qp_type qp_type,
197 struct ib_mad_reg_req *mad_reg_req,
198 u8 rmpp_version,
199 ib_mad_send_handler send_handler,
200 ib_mad_recv_handler recv_handler,
Ira Weiny0f29b462014-08-08 19:00:55 -0400201 void *context,
202 u32 registration_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 struct ib_mad_port_private *port_priv;
205 struct ib_mad_agent *ret = ERR_PTR(-EINVAL);
206 struct ib_mad_agent_private *mad_agent_priv;
207 struct ib_mad_reg_req *reg_req = NULL;
208 struct ib_mad_mgmt_class_table *class;
209 struct ib_mad_mgmt_vendor_class_table *vendor;
210 struct ib_mad_mgmt_vendor_class *vendor_class;
211 struct ib_mad_mgmt_method_table *method;
212 int ret2, qpn;
213 unsigned long flags;
214 u8 mgmt_class, vclass;
215
216 /* Validate parameters */
217 qpn = get_spl_qp_index(qp_type);
Ira Weiny9ad13a42014-08-08 19:00:54 -0400218 if (qpn == -1) {
219 dev_notice(&device->dev,
220 "ib_register_mad_agent: invalid QP Type %d\n",
221 qp_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 goto error1;
Ira Weiny9ad13a42014-08-08 19:00:54 -0400223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Ira Weiny9ad13a42014-08-08 19:00:54 -0400225 if (rmpp_version && rmpp_version != IB_MGMT_RMPP_VERSION) {
226 dev_notice(&device->dev,
227 "ib_register_mad_agent: invalid RMPP Version %u\n",
228 rmpp_version);
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700229 goto error1;
Ira Weiny9ad13a42014-08-08 19:00:54 -0400230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 /* Validate MAD registration request if supplied */
233 if (mad_reg_req) {
Ira Weiny9ad13a42014-08-08 19:00:54 -0400234 if (mad_reg_req->mgmt_class_version >= MAX_MGMT_VERSION) {
235 dev_notice(&device->dev,
236 "ib_register_mad_agent: invalid Class Version %u\n",
237 mad_reg_req->mgmt_class_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 goto error1;
Ira Weiny9ad13a42014-08-08 19:00:54 -0400239 }
240 if (!recv_handler) {
241 dev_notice(&device->dev,
242 "ib_register_mad_agent: no recv_handler\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 goto error1;
Ira Weiny9ad13a42014-08-08 19:00:54 -0400244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 if (mad_reg_req->mgmt_class >= MAX_MGMT_CLASS) {
246 /*
247 * IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE is the only
248 * one in this range currently allowed
249 */
250 if (mad_reg_req->mgmt_class !=
Ira Weiny9ad13a42014-08-08 19:00:54 -0400251 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
252 dev_notice(&device->dev,
253 "ib_register_mad_agent: Invalid Mgmt Class 0x%x\n",
254 mad_reg_req->mgmt_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 goto error1;
Ira Weiny9ad13a42014-08-08 19:00:54 -0400256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 } else if (mad_reg_req->mgmt_class == 0) {
258 /*
259 * Class 0 is reserved in IBA and is used for
260 * aliasing of IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
261 */
Ira Weiny9ad13a42014-08-08 19:00:54 -0400262 dev_notice(&device->dev,
263 "ib_register_mad_agent: Invalid Mgmt Class 0\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 goto error1;
265 } else if (is_vendor_class(mad_reg_req->mgmt_class)) {
266 /*
267 * If class is in "new" vendor range,
268 * ensure supplied OUI is not zero
269 */
Ira Weiny9ad13a42014-08-08 19:00:54 -0400270 if (!is_vendor_oui(mad_reg_req->oui)) {
271 dev_notice(&device->dev,
272 "ib_register_mad_agent: No OUI specified for class 0x%x\n",
273 mad_reg_req->mgmt_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 goto error1;
Ira Weiny9ad13a42014-08-08 19:00:54 -0400275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
Hal Rosenstock618a3c02006-03-28 16:40:04 -0800277 /* Make sure class supplied is consistent with RMPP */
Hal Rosenstock64cb9c62006-04-12 21:29:10 -0400278 if (!ib_is_mad_class_rmpp(mad_reg_req->mgmt_class)) {
Ira Weiny9ad13a42014-08-08 19:00:54 -0400279 if (rmpp_version) {
280 dev_notice(&device->dev,
281 "ib_register_mad_agent: RMPP version for non-RMPP class 0x%x\n",
282 mad_reg_req->mgmt_class);
Hal Rosenstock618a3c02006-03-28 16:40:04 -0800283 goto error1;
Ira Weiny9ad13a42014-08-08 19:00:54 -0400284 }
Hal Rosenstock618a3c02006-03-28 16:40:04 -0800285 }
Ira Weiny1471cb62014-08-08 19:00:56 -0400286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 /* Make sure class supplied is consistent with QP type */
288 if (qp_type == IB_QPT_SMI) {
289 if ((mad_reg_req->mgmt_class !=
290 IB_MGMT_CLASS_SUBN_LID_ROUTED) &&
291 (mad_reg_req->mgmt_class !=
Ira Weiny9ad13a42014-08-08 19:00:54 -0400292 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) {
293 dev_notice(&device->dev,
294 "ib_register_mad_agent: Invalid SM QP type: class 0x%x\n",
295 mad_reg_req->mgmt_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 goto error1;
Ira Weiny9ad13a42014-08-08 19:00:54 -0400297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 } else {
299 if ((mad_reg_req->mgmt_class ==
300 IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
301 (mad_reg_req->mgmt_class ==
Ira Weiny9ad13a42014-08-08 19:00:54 -0400302 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) {
303 dev_notice(&device->dev,
304 "ib_register_mad_agent: Invalid GS QP type: class 0x%x\n",
305 mad_reg_req->mgmt_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 goto error1;
Ira Weiny9ad13a42014-08-08 19:00:54 -0400307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
309 } else {
310 /* No registration request supplied */
311 if (!send_handler)
312 goto error1;
Ira Weiny1471cb62014-08-08 19:00:56 -0400313 if (registration_flags & IB_MAD_USER_RMPP)
314 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
316
317 /* Validate device and port */
318 port_priv = ib_get_mad_port(device, port_num);
319 if (!port_priv) {
Yuval Shaiaf57e8ca2017-01-19 16:48:31 +0200320 dev_notice(&device->dev,
321 "ib_register_mad_agent: Invalid port %d\n",
322 port_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 ret = ERR_PTR(-ENODEV);
324 goto error1;
325 }
326
Ira Weinyc8367c42011-05-19 18:19:28 -0700327 /* Verify the QP requested is supported. For example, Ethernet devices
328 * will not have QP0 */
329 if (!port_priv->qp_info[qpn].qp) {
Ira Weiny9ad13a42014-08-08 19:00:54 -0400330 dev_notice(&device->dev,
331 "ib_register_mad_agent: QP %d not supported\n", qpn);
Ira Weinyc8367c42011-05-19 18:19:28 -0700332 ret = ERR_PTR(-EPROTONOSUPPORT);
333 goto error1;
334 }
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 /* Allocate structures */
Roland Dreierde6eb662005-11-02 07:23:14 -0800337 mad_agent_priv = kzalloc(sizeof *mad_agent_priv, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (!mad_agent_priv) {
339 ret = ERR_PTR(-ENOMEM);
340 goto error1;
341 }
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (mad_reg_req) {
Julia Lawall9893e742010-05-15 23:22:38 +0200344 reg_req = kmemdup(mad_reg_req, sizeof *reg_req, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (!reg_req) {
346 ret = ERR_PTR(-ENOMEM);
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700347 goto error3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350
351 /* Now, fill in the various structures */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 mad_agent_priv->qp_info = &port_priv->qp_info[qpn];
353 mad_agent_priv->reg_req = reg_req;
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700354 mad_agent_priv->agent.rmpp_version = rmpp_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 mad_agent_priv->agent.device = device;
356 mad_agent_priv->agent.recv_handler = recv_handler;
357 mad_agent_priv->agent.send_handler = send_handler;
358 mad_agent_priv->agent.context = context;
359 mad_agent_priv->agent.qp = port_priv->qp_info[qpn].qp;
360 mad_agent_priv->agent.port_num = port_num;
Ira Weiny0f29b462014-08-08 19:00:55 -0400361 mad_agent_priv->agent.flags = registration_flags;
Ralph Campbelld9620a42009-02-27 14:44:32 -0800362 spin_lock_init(&mad_agent_priv->lock);
363 INIT_LIST_HEAD(&mad_agent_priv->send_list);
364 INIT_LIST_HEAD(&mad_agent_priv->wait_list);
365 INIT_LIST_HEAD(&mad_agent_priv->done_list);
366 INIT_LIST_HEAD(&mad_agent_priv->rmpp_list);
367 INIT_DELAYED_WORK(&mad_agent_priv->timed_work, timeout_sends);
368 INIT_LIST_HEAD(&mad_agent_priv->local_list);
369 INIT_WORK(&mad_agent_priv->local_work, local_completions);
370 atomic_set(&mad_agent_priv->refcount, 1);
371 init_completion(&mad_agent_priv->comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Daniel Jurgens47a2b332017-05-19 15:48:54 +0300373 ret2 = ib_mad_agent_security_setup(&mad_agent_priv->agent, qp_type);
374 if (ret2) {
375 ret = ERR_PTR(ret2);
376 goto error4;
377 }
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 spin_lock_irqsave(&port_priv->reg_lock, flags);
380 mad_agent_priv->agent.hi_tid = ++ib_mad_client_id;
381
382 /*
383 * Make sure MAD registration (if supplied)
384 * is non overlapping with any existing ones
385 */
386 if (mad_reg_req) {
387 mgmt_class = convert_mgmt_class(mad_reg_req->mgmt_class);
388 if (!is_vendor_class(mgmt_class)) {
389 class = port_priv->version[mad_reg_req->
390 mgmt_class_version].class;
391 if (class) {
392 method = class->method_table[mgmt_class];
393 if (method) {
394 if (method_in_use(&method,
395 mad_reg_req))
Daniel Jurgens47a2b332017-05-19 15:48:54 +0300396 goto error5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398 }
399 ret2 = add_nonoui_reg_req(mad_reg_req, mad_agent_priv,
400 mgmt_class);
401 } else {
402 /* "New" vendor class range */
403 vendor = port_priv->version[mad_reg_req->
404 mgmt_class_version].vendor;
405 if (vendor) {
406 vclass = vendor_class_index(mgmt_class);
407 vendor_class = vendor->vendor_class[vclass];
408 if (vendor_class) {
409 if (is_vendor_method_in_use(
410 vendor_class,
411 mad_reg_req))
Daniel Jurgens47a2b332017-05-19 15:48:54 +0300412 goto error5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414 }
415 ret2 = add_oui_reg_req(mad_reg_req, mad_agent_priv);
416 }
417 if (ret2) {
418 ret = ERR_PTR(ret2);
Daniel Jurgens47a2b332017-05-19 15:48:54 +0300419 goto error5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421 }
422
423 /* Add mad agent into port's agent list */
424 list_add_tail(&mad_agent_priv->agent_list, &port_priv->agent_list);
425 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return &mad_agent_priv->agent;
Daniel Jurgens47a2b332017-05-19 15:48:54 +0300428error5:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
Daniel Jurgens47a2b332017-05-19 15:48:54 +0300430 ib_mad_agent_security_cleanup(&mad_agent_priv->agent);
431error4:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 kfree(reg_req);
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700433error3:
Adrian Bunk2012a112005-11-27 00:37:36 +0100434 kfree(mad_agent_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435error1:
436 return ret;
437}
438EXPORT_SYMBOL(ib_register_mad_agent);
439
440static inline int is_snooping_sends(int mad_snoop_flags)
441{
442 return (mad_snoop_flags &
443 (/*IB_MAD_SNOOP_POSTED_SENDS |
444 IB_MAD_SNOOP_RMPP_SENDS |*/
445 IB_MAD_SNOOP_SEND_COMPLETIONS /*|
446 IB_MAD_SNOOP_RMPP_SEND_COMPLETIONS*/));
447}
448
449static inline int is_snooping_recvs(int mad_snoop_flags)
450{
451 return (mad_snoop_flags &
452 (IB_MAD_SNOOP_RECVS /*|
453 IB_MAD_SNOOP_RMPP_RECVS*/));
454}
455
456static int register_snoop_agent(struct ib_mad_qp_info *qp_info,
457 struct ib_mad_snoop_private *mad_snoop_priv)
458{
459 struct ib_mad_snoop_private **new_snoop_table;
460 unsigned long flags;
461 int i;
462
463 spin_lock_irqsave(&qp_info->snoop_lock, flags);
464 /* Check for empty slot in array. */
465 for (i = 0; i < qp_info->snoop_table_size; i++)
466 if (!qp_info->snoop_table[i])
467 break;
468
469 if (i == qp_info->snoop_table_size) {
470 /* Grow table. */
Roland Dreier528051742008-10-14 14:05:36 -0700471 new_snoop_table = krealloc(qp_info->snoop_table,
472 sizeof mad_snoop_priv *
473 (qp_info->snoop_table_size + 1),
474 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (!new_snoop_table) {
476 i = -ENOMEM;
477 goto out;
478 }
Roland Dreier528051742008-10-14 14:05:36 -0700479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 qp_info->snoop_table = new_snoop_table;
481 qp_info->snoop_table_size++;
482 }
483 qp_info->snoop_table[i] = mad_snoop_priv;
484 atomic_inc(&qp_info->snoop_count);
485out:
486 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
487 return i;
488}
489
490struct ib_mad_agent *ib_register_mad_snoop(struct ib_device *device,
491 u8 port_num,
492 enum ib_qp_type qp_type,
493 int mad_snoop_flags,
494 ib_mad_snoop_handler snoop_handler,
495 ib_mad_recv_handler recv_handler,
496 void *context)
497{
498 struct ib_mad_port_private *port_priv;
499 struct ib_mad_agent *ret;
500 struct ib_mad_snoop_private *mad_snoop_priv;
501 int qpn;
Daniel Jurgens47a2b332017-05-19 15:48:54 +0300502 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 /* Validate parameters */
505 if ((is_snooping_sends(mad_snoop_flags) && !snoop_handler) ||
506 (is_snooping_recvs(mad_snoop_flags) && !recv_handler)) {
507 ret = ERR_PTR(-EINVAL);
508 goto error1;
509 }
510 qpn = get_spl_qp_index(qp_type);
511 if (qpn == -1) {
512 ret = ERR_PTR(-EINVAL);
513 goto error1;
514 }
515 port_priv = ib_get_mad_port(device, port_num);
516 if (!port_priv) {
517 ret = ERR_PTR(-ENODEV);
518 goto error1;
519 }
520 /* Allocate structures */
Roland Dreierde6eb662005-11-02 07:23:14 -0800521 mad_snoop_priv = kzalloc(sizeof *mad_snoop_priv, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (!mad_snoop_priv) {
523 ret = ERR_PTR(-ENOMEM);
524 goto error1;
525 }
526
527 /* Now, fill in the various structures */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 mad_snoop_priv->qp_info = &port_priv->qp_info[qpn];
529 mad_snoop_priv->agent.device = device;
530 mad_snoop_priv->agent.recv_handler = recv_handler;
531 mad_snoop_priv->agent.snoop_handler = snoop_handler;
532 mad_snoop_priv->agent.context = context;
533 mad_snoop_priv->agent.qp = port_priv->qp_info[qpn].qp;
534 mad_snoop_priv->agent.port_num = port_num;
535 mad_snoop_priv->mad_snoop_flags = mad_snoop_flags;
Sean Hefty1b52fa982006-05-12 14:57:52 -0700536 init_completion(&mad_snoop_priv->comp);
Daniel Jurgens47a2b332017-05-19 15:48:54 +0300537
538 err = ib_mad_agent_security_setup(&mad_snoop_priv->agent, qp_type);
539 if (err) {
540 ret = ERR_PTR(err);
541 goto error2;
542 }
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 mad_snoop_priv->snoop_index = register_snoop_agent(
545 &port_priv->qp_info[qpn],
546 mad_snoop_priv);
547 if (mad_snoop_priv->snoop_index < 0) {
548 ret = ERR_PTR(mad_snoop_priv->snoop_index);
Daniel Jurgens47a2b332017-05-19 15:48:54 +0300549 goto error3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
551
552 atomic_set(&mad_snoop_priv->refcount, 1);
553 return &mad_snoop_priv->agent;
Daniel Jurgens47a2b332017-05-19 15:48:54 +0300554error3:
555 ib_mad_agent_security_cleanup(&mad_snoop_priv->agent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556error2:
557 kfree(mad_snoop_priv);
558error1:
559 return ret;
560}
561EXPORT_SYMBOL(ib_register_mad_snoop);
562
Sean Hefty1b52fa982006-05-12 14:57:52 -0700563static inline void deref_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
564{
565 if (atomic_dec_and_test(&mad_agent_priv->refcount))
566 complete(&mad_agent_priv->comp);
567}
568
569static inline void deref_snoop_agent(struct ib_mad_snoop_private *mad_snoop_priv)
570{
571 if (atomic_dec_and_test(&mad_snoop_priv->refcount))
572 complete(&mad_snoop_priv->comp);
573}
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575static void unregister_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
576{
577 struct ib_mad_port_private *port_priv;
578 unsigned long flags;
579
580 /* Note that we could still be handling received MADs */
581
582 /*
583 * Canceling all sends results in dropping received response
584 * MADs, preventing us from queuing additional work
585 */
586 cancel_mads(mad_agent_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 port_priv = mad_agent_priv->qp_info->port_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 cancel_delayed_work(&mad_agent_priv->timed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 spin_lock_irqsave(&port_priv->reg_lock, flags);
591 remove_mad_reg_req(mad_agent_priv);
592 list_del(&mad_agent_priv->agent_list);
593 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
594
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700595 flush_workqueue(port_priv->wq);
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700596 ib_cancel_rmpp_recvs(mad_agent_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Sean Hefty1b52fa982006-05-12 14:57:52 -0700598 deref_mad_agent(mad_agent_priv);
599 wait_for_completion(&mad_agent_priv->comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Daniel Jurgens47a2b332017-05-19 15:48:54 +0300601 ib_mad_agent_security_cleanup(&mad_agent_priv->agent);
602
Jesper Juhl6044ec82005-11-07 01:01:32 -0800603 kfree(mad_agent_priv->reg_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 kfree(mad_agent_priv);
605}
606
607static void unregister_mad_snoop(struct ib_mad_snoop_private *mad_snoop_priv)
608{
609 struct ib_mad_qp_info *qp_info;
610 unsigned long flags;
611
612 qp_info = mad_snoop_priv->qp_info;
613 spin_lock_irqsave(&qp_info->snoop_lock, flags);
614 qp_info->snoop_table[mad_snoop_priv->snoop_index] = NULL;
615 atomic_dec(&qp_info->snoop_count);
616 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
617
Sean Hefty1b52fa982006-05-12 14:57:52 -0700618 deref_snoop_agent(mad_snoop_priv);
619 wait_for_completion(&mad_snoop_priv->comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Daniel Jurgens47a2b332017-05-19 15:48:54 +0300621 ib_mad_agent_security_cleanup(&mad_snoop_priv->agent);
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 kfree(mad_snoop_priv);
624}
625
626/*
627 * ib_unregister_mad_agent - Unregisters a client from using MAD services
628 */
Zhu Yanjun8d2216b2017-03-31 23:42:55 -0400629void ib_unregister_mad_agent(struct ib_mad_agent *mad_agent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
631 struct ib_mad_agent_private *mad_agent_priv;
632 struct ib_mad_snoop_private *mad_snoop_priv;
633
634 /* If the TID is zero, the agent can only snoop. */
635 if (mad_agent->hi_tid) {
636 mad_agent_priv = container_of(mad_agent,
637 struct ib_mad_agent_private,
638 agent);
639 unregister_mad_agent(mad_agent_priv);
640 } else {
641 mad_snoop_priv = container_of(mad_agent,
642 struct ib_mad_snoop_private,
643 agent);
644 unregister_mad_snoop(mad_snoop_priv);
645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647EXPORT_SYMBOL(ib_unregister_mad_agent);
648
649static void dequeue_mad(struct ib_mad_list_head *mad_list)
650{
651 struct ib_mad_queue *mad_queue;
652 unsigned long flags;
653
654 BUG_ON(!mad_list->mad_queue);
655 mad_queue = mad_list->mad_queue;
656 spin_lock_irqsave(&mad_queue->lock, flags);
657 list_del(&mad_list->list);
658 mad_queue->count--;
659 spin_unlock_irqrestore(&mad_queue->lock, flags);
660}
661
662static void snoop_send(struct ib_mad_qp_info *qp_info,
Sean Hefty34816ad2005-10-25 10:51:39 -0700663 struct ib_mad_send_buf *send_buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 struct ib_mad_send_wc *mad_send_wc,
665 int mad_snoop_flags)
666{
667 struct ib_mad_snoop_private *mad_snoop_priv;
668 unsigned long flags;
669 int i;
670
671 spin_lock_irqsave(&qp_info->snoop_lock, flags);
672 for (i = 0; i < qp_info->snoop_table_size; i++) {
673 mad_snoop_priv = qp_info->snoop_table[i];
674 if (!mad_snoop_priv ||
675 !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
676 continue;
677
678 atomic_inc(&mad_snoop_priv->refcount);
679 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
680 mad_snoop_priv->agent.snoop_handler(&mad_snoop_priv->agent,
Sean Hefty34816ad2005-10-25 10:51:39 -0700681 send_buf, mad_send_wc);
Sean Hefty1b52fa982006-05-12 14:57:52 -0700682 deref_snoop_agent(mad_snoop_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 spin_lock_irqsave(&qp_info->snoop_lock, flags);
684 }
685 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
686}
687
688static void snoop_recv(struct ib_mad_qp_info *qp_info,
689 struct ib_mad_recv_wc *mad_recv_wc,
690 int mad_snoop_flags)
691{
692 struct ib_mad_snoop_private *mad_snoop_priv;
693 unsigned long flags;
694 int i;
695
696 spin_lock_irqsave(&qp_info->snoop_lock, flags);
697 for (i = 0; i < qp_info->snoop_table_size; i++) {
698 mad_snoop_priv = qp_info->snoop_table[i];
699 if (!mad_snoop_priv ||
700 !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
701 continue;
702
703 atomic_inc(&mad_snoop_priv->refcount);
704 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
Christoph Hellwigca281262016-01-04 14:15:58 +0100705 mad_snoop_priv->agent.recv_handler(&mad_snoop_priv->agent, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 mad_recv_wc);
Sean Hefty1b52fa982006-05-12 14:57:52 -0700707 deref_snoop_agent(mad_snoop_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 spin_lock_irqsave(&qp_info->snoop_lock, flags);
709 }
710 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
711}
712
Christoph Hellwigd53e11f2016-01-05 22:46:12 -0800713static void build_smp_wc(struct ib_qp *qp, struct ib_cqe *cqe, u16 slid,
714 u16 pkey_index, u8 port_num, struct ib_wc *wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
716 memset(wc, 0, sizeof *wc);
Christoph Hellwigd53e11f2016-01-05 22:46:12 -0800717 wc->wr_cqe = cqe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 wc->status = IB_WC_SUCCESS;
719 wc->opcode = IB_WC_RECV;
720 wc->pkey_index = pkey_index;
721 wc->byte_len = sizeof(struct ib_mad) + sizeof(struct ib_grh);
722 wc->src_qp = IB_QP0;
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +0200723 wc->qp = qp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 wc->slid = slid;
725 wc->sl = 0;
726 wc->dlid_path_bits = 0;
727 wc->port_num = port_num;
728}
729
Ira Weinyc9082e52015-06-06 14:38:30 -0400730static size_t mad_priv_size(const struct ib_mad_private *mp)
731{
732 return sizeof(struct ib_mad_private) + mp->mad_size;
733}
734
735static struct ib_mad_private *alloc_mad_private(size_t mad_size, gfp_t flags)
736{
737 size_t size = sizeof(struct ib_mad_private) + mad_size;
738 struct ib_mad_private *ret = kzalloc(size, flags);
739
740 if (ret)
741 ret->mad_size = mad_size;
742
743 return ret;
744}
745
746static size_t port_mad_size(const struct ib_mad_port_private *port_priv)
747{
748 return rdma_max_mad_size(port_priv->device, port_priv->port_num);
749}
750
751static size_t mad_priv_dma_size(const struct ib_mad_private *mp)
752{
753 return sizeof(struct ib_grh) + mp->mad_size;
754}
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756/*
757 * Return 0 if SMP is to be sent
758 * Return 1 if SMP was consumed locally (whether or not solicited)
759 * Return < 0 if error
760 */
761static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
Sean Hefty34816ad2005-10-25 10:51:39 -0700762 struct ib_mad_send_wr_private *mad_send_wr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Hal Rosenstockde493d42007-04-02 11:24:07 -0400764 int ret = 0;
Sean Hefty34816ad2005-10-25 10:51:39 -0700765 struct ib_smp *smp = mad_send_wr->send_buf.mad;
Ira Weiny8e4349d2015-06-10 16:16:48 -0400766 struct opa_smp *opa_smp = (struct opa_smp *)smp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 unsigned long flags;
768 struct ib_mad_local_private *local;
769 struct ib_mad_private *mad_priv;
770 struct ib_mad_port_private *port_priv;
771 struct ib_mad_agent_private *recv_mad_agent = NULL;
772 struct ib_device *device = mad_agent_priv->agent.device;
Hal Rosenstock1bae4db2007-05-14 17:21:52 -0400773 u8 port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 struct ib_wc mad_wc;
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100775 struct ib_ud_wr *send_wr = &mad_send_wr->send_wr;
Ira Weinyc9082e52015-06-06 14:38:30 -0400776 size_t mad_size = port_mad_size(mad_agent_priv->qp_info->port_priv);
Ira Weiny4cd7c942015-06-06 14:38:31 -0400777 u16 out_mad_pkey_index = 0;
Ira Weiny8e4349d2015-06-10 16:16:48 -0400778 u16 drslid;
779 bool opa = rdma_cap_opa_mad(mad_agent_priv->qp_info->port_priv->device,
780 mad_agent_priv->qp_info->port_priv->port_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Hal Rosenstock41390322015-06-29 09:57:00 -0400782 if (rdma_cap_ib_switch(device) &&
Hal Rosenstock1bae4db2007-05-14 17:21:52 -0400783 smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100784 port_num = send_wr->port_num;
Hal Rosenstock1bae4db2007-05-14 17:21:52 -0400785 else
786 port_num = mad_agent_priv->agent.port_num;
787
Ralph Campbell8cf3f042006-02-03 14:28:48 -0800788 /*
789 * Directed route handling starts if the initial LID routed part of
790 * a request or the ending LID routed part of a response is empty.
791 * If we are at the start of the LID routed part, don't update the
792 * hop_ptr or hop_cnt. See section 14.2.2, Vol 1 IB spec.
793 */
Hal Rosenstock9fa240b2016-10-18 13:20:29 -0400794 if (opa && smp->class_version == OPA_SM_CLASS_VERSION) {
Ira Weiny8e4349d2015-06-10 16:16:48 -0400795 u32 opa_drslid;
Hal Rosenstockde493d42007-04-02 11:24:07 -0400796
Ira Weiny8e4349d2015-06-10 16:16:48 -0400797 if ((opa_get_smp_direction(opa_smp)
798 ? opa_smp->route.dr.dr_dlid : opa_smp->route.dr.dr_slid) ==
799 OPA_LID_PERMISSIVE &&
Hal Rosenstock41390322015-06-29 09:57:00 -0400800 opa_smi_handle_dr_smp_send(opa_smp,
801 rdma_cap_ib_switch(device),
Ira Weiny8e4349d2015-06-10 16:16:48 -0400802 port_num) == IB_SMI_DISCARD) {
803 ret = -EINVAL;
804 dev_err(&device->dev, "OPA Invalid directed route\n");
805 goto out;
806 }
807 opa_drslid = be32_to_cpu(opa_smp->route.dr.dr_slid);
Ira Weinycd4cd562015-06-25 12:04:49 -0400808 if (opa_drslid != be32_to_cpu(OPA_LID_PERMISSIVE) &&
Ira Weiny8e4349d2015-06-10 16:16:48 -0400809 opa_drslid & 0xffff0000) {
810 ret = -EINVAL;
811 dev_err(&device->dev, "OPA Invalid dr_slid 0x%x\n",
812 opa_drslid);
813 goto out;
814 }
815 drslid = (u16)(opa_drslid & 0x0000ffff);
816
817 /* Check to post send on QP or process locally */
818 if (opa_smi_check_local_smp(opa_smp, device) == IB_SMI_DISCARD &&
819 opa_smi_check_local_returning_smp(opa_smp, device) == IB_SMI_DISCARD)
820 goto out;
821 } else {
822 if ((ib_get_smp_direction(smp) ? smp->dr_dlid : smp->dr_slid) ==
823 IB_LID_PERMISSIVE &&
Hal Rosenstock41390322015-06-29 09:57:00 -0400824 smi_handle_dr_smp_send(smp, rdma_cap_ib_switch(device), port_num) ==
Ira Weiny8e4349d2015-06-10 16:16:48 -0400825 IB_SMI_DISCARD) {
826 ret = -EINVAL;
827 dev_err(&device->dev, "Invalid directed route\n");
828 goto out;
829 }
830 drslid = be16_to_cpu(smp->dr_slid);
831
832 /* Check to post send on QP or process locally */
833 if (smi_check_local_smp(smp, device) == IB_SMI_DISCARD &&
834 smi_check_local_returning_smp(smp, device) == IB_SMI_DISCARD)
835 goto out;
836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 local = kmalloc(sizeof *local, GFP_ATOMIC);
839 if (!local) {
840 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 goto out;
842 }
843 local->mad_priv = NULL;
844 local->recv_mad_agent = NULL;
Ira Weinyc9082e52015-06-06 14:38:30 -0400845 mad_priv = alloc_mad_private(mad_size, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (!mad_priv) {
847 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 kfree(local);
849 goto out;
850 }
851
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +0200852 build_smp_wc(mad_agent_priv->agent.qp,
Christoph Hellwigd53e11f2016-01-05 22:46:12 -0800853 send_wr->wr.wr_cqe, drslid,
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100854 send_wr->pkey_index,
855 send_wr->port_num, &mad_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Ira Weiny8e4349d2015-06-10 16:16:48 -0400857 if (opa && smp->base_version == OPA_MGMT_BASE_VERSION) {
858 mad_wc.byte_len = mad_send_wr->send_buf.hdr_len
859 + mad_send_wr->send_buf.data_len
860 + sizeof(struct ib_grh);
861 }
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 /* No GRH for DR SMP */
864 ret = device->process_mad(device, 0, port_num, &mad_wc, NULL,
Ira Weiny4cd7c942015-06-06 14:38:31 -0400865 (const struct ib_mad_hdr *)smp, mad_size,
866 (struct ib_mad_hdr *)mad_priv->mad,
867 &mad_size, &out_mad_pkey_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 switch (ret)
869 {
870 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY:
Ira Weinyc9082e52015-06-06 14:38:30 -0400871 if (ib_response_mad((const struct ib_mad_hdr *)mad_priv->mad) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 mad_agent_priv->agent.recv_handler) {
873 local->mad_priv = mad_priv;
874 local->recv_mad_agent = mad_agent_priv;
875 /*
876 * Reference MAD agent until receive
877 * side of local completion handled
878 */
879 atomic_inc(&mad_agent_priv->refcount);
880 } else
Ira Weinyc9082e52015-06-06 14:38:30 -0400881 kfree(mad_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 break;
883 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED:
Ira Weinyc9082e52015-06-06 14:38:30 -0400884 kfree(mad_priv);
Ralph Campbell4780c192009-03-03 14:22:17 -0800885 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 case IB_MAD_RESULT_SUCCESS:
887 /* Treat like an incoming receive MAD */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 port_priv = ib_get_mad_port(mad_agent_priv->agent.device,
889 mad_agent_priv->agent.port_num);
890 if (port_priv) {
Ira Weinyc9082e52015-06-06 14:38:30 -0400891 memcpy(mad_priv->mad, smp, mad_priv->mad_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 recv_mad_agent = find_mad_agent(port_priv,
Ira Weinyc9082e52015-06-06 14:38:30 -0400893 (const struct ib_mad_hdr *)mad_priv->mad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
895 if (!port_priv || !recv_mad_agent) {
Ralph Campbell4780c192009-03-03 14:22:17 -0800896 /*
897 * No receiving agent so drop packet and
898 * generate send completion.
899 */
Ira Weinyc9082e52015-06-06 14:38:30 -0400900 kfree(mad_priv);
Ralph Campbell4780c192009-03-03 14:22:17 -0800901 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903 local->mad_priv = mad_priv;
904 local->recv_mad_agent = recv_mad_agent;
905 break;
906 default:
Ira Weinyc9082e52015-06-06 14:38:30 -0400907 kfree(mad_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 kfree(local);
909 ret = -EINVAL;
910 goto out;
911 }
912
Sean Hefty34816ad2005-10-25 10:51:39 -0700913 local->mad_send_wr = mad_send_wr;
Ira Weiny8e4349d2015-06-10 16:16:48 -0400914 if (opa) {
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100915 local->mad_send_wr->send_wr.pkey_index = out_mad_pkey_index;
Ira Weiny8e4349d2015-06-10 16:16:48 -0400916 local->return_wc_byte_len = mad_size;
917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 /* Reference MAD agent until send side of local completion handled */
919 atomic_inc(&mad_agent_priv->refcount);
920 /* Queue local completion to local list */
921 spin_lock_irqsave(&mad_agent_priv->lock, flags);
922 list_add_tail(&local->completion_list, &mad_agent_priv->local_list);
923 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
924 queue_work(mad_agent_priv->qp_info->port_priv->wq,
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700925 &mad_agent_priv->local_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 ret = 1;
927out:
928 return ret;
929}
930
Ira Weiny548ead12015-06-06 14:38:33 -0400931static int get_pad_size(int hdr_len, int data_len, size_t mad_size)
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700932{
933 int seg_size, pad;
934
Ira Weiny548ead12015-06-06 14:38:33 -0400935 seg_size = mad_size - hdr_len;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700936 if (data_len && seg_size) {
937 pad = seg_size - data_len % seg_size;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800938 return pad == seg_size ? 0 : pad;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700939 } else
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800940 return seg_size;
941}
942
943static void free_send_rmpp_list(struct ib_mad_send_wr_private *mad_send_wr)
944{
945 struct ib_rmpp_segment *s, *t;
946
947 list_for_each_entry_safe(s, t, &mad_send_wr->rmpp_list, list) {
948 list_del(&s->list);
949 kfree(s);
950 }
951}
952
953static int alloc_send_rmpp_list(struct ib_mad_send_wr_private *send_wr,
Ira Weiny548ead12015-06-06 14:38:33 -0400954 size_t mad_size, gfp_t gfp_mask)
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800955{
956 struct ib_mad_send_buf *send_buf = &send_wr->send_buf;
957 struct ib_rmpp_mad *rmpp_mad = send_buf->mad;
958 struct ib_rmpp_segment *seg = NULL;
959 int left, seg_size, pad;
960
Ira Weiny548ead12015-06-06 14:38:33 -0400961 send_buf->seg_size = mad_size - send_buf->hdr_len;
962 send_buf->seg_rmpp_size = mad_size - IB_MGMT_RMPP_HDR;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800963 seg_size = send_buf->seg_size;
964 pad = send_wr->pad;
965
966 /* Allocate data segments. */
967 for (left = send_buf->data_len + pad; left > 0; left -= seg_size) {
968 seg = kmalloc(sizeof (*seg) + seg_size, gfp_mask);
969 if (!seg) {
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800970 free_send_rmpp_list(send_wr);
971 return -ENOMEM;
972 }
973 seg->num = ++send_buf->seg_count;
974 list_add_tail(&seg->list, &send_wr->rmpp_list);
975 }
976
977 /* Zero any padding */
978 if (pad)
979 memset(seg->data + seg_size - pad, 0, pad);
980
981 rmpp_mad->rmpp_hdr.rmpp_version = send_wr->mad_agent_priv->
982 agent.rmpp_version;
983 rmpp_mad->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_DATA;
984 ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
985
986 send_wr->cur_seg = container_of(send_wr->rmpp_list.next,
987 struct ib_rmpp_segment, list);
988 send_wr->last_ack_seg = send_wr->cur_seg;
989 return 0;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700990}
991
Ira Weinyf766c582015-05-08 14:27:24 -0400992int ib_mad_kernel_rmpp_agent(const struct ib_mad_agent *agent)
Ira Weiny1471cb62014-08-08 19:00:56 -0400993{
994 return agent->rmpp_version && !(agent->flags & IB_MAD_USER_RMPP);
995}
996EXPORT_SYMBOL(ib_mad_kernel_rmpp_agent);
997
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700998struct ib_mad_send_buf * ib_create_send_mad(struct ib_mad_agent *mad_agent,
999 u32 remote_qpn, u16 pkey_index,
Sean Hefty34816ad2005-10-25 10:51:39 -07001000 int rmpp_active,
Hal Rosenstock824c8ae2005-07-27 11:45:23 -07001001 int hdr_len, int data_len,
Ira Weinyda2dfaa2015-06-06 14:38:28 -04001002 gfp_t gfp_mask,
1003 u8 base_version)
Hal Rosenstock824c8ae2005-07-27 11:45:23 -07001004{
1005 struct ib_mad_agent_private *mad_agent_priv;
Sean Hefty34816ad2005-10-25 10:51:39 -07001006 struct ib_mad_send_wr_private *mad_send_wr;
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001007 int pad, message_size, ret, size;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -07001008 void *buf;
Ira Weiny548ead12015-06-06 14:38:33 -04001009 size_t mad_size;
1010 bool opa;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -07001011
Sean Hefty34816ad2005-10-25 10:51:39 -07001012 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
1013 agent);
Ira Weiny548ead12015-06-06 14:38:33 -04001014
1015 opa = rdma_cap_opa_mad(mad_agent->device, mad_agent->port_num);
1016
1017 if (opa && base_version == OPA_MGMT_BASE_VERSION)
1018 mad_size = sizeof(struct opa_mad);
1019 else
1020 mad_size = sizeof(struct ib_mad);
1021
1022 pad = get_pad_size(hdr_len, data_len, mad_size);
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001023 message_size = hdr_len + data_len + pad;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -07001024
Ira Weiny1471cb62014-08-08 19:00:56 -04001025 if (ib_mad_kernel_rmpp_agent(mad_agent)) {
Ira Weiny548ead12015-06-06 14:38:33 -04001026 if (!rmpp_active && message_size > mad_size)
Ira Weiny1471cb62014-08-08 19:00:56 -04001027 return ERR_PTR(-EINVAL);
1028 } else
Ira Weiny548ead12015-06-06 14:38:33 -04001029 if (rmpp_active || message_size > mad_size)
Ira Weiny1471cb62014-08-08 19:00:56 -04001030 return ERR_PTR(-EINVAL);
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001031
Ira Weiny548ead12015-06-06 14:38:33 -04001032 size = rmpp_active ? hdr_len : mad_size;
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001033 buf = kzalloc(sizeof *mad_send_wr + size, gfp_mask);
Hal Rosenstock824c8ae2005-07-27 11:45:23 -07001034 if (!buf)
1035 return ERR_PTR(-ENOMEM);
1036
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001037 mad_send_wr = buf + size;
1038 INIT_LIST_HEAD(&mad_send_wr->rmpp_list);
Sean Hefty34816ad2005-10-25 10:51:39 -07001039 mad_send_wr->send_buf.mad = buf;
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001040 mad_send_wr->send_buf.hdr_len = hdr_len;
1041 mad_send_wr->send_buf.data_len = data_len;
1042 mad_send_wr->pad = pad;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -07001043
Sean Hefty34816ad2005-10-25 10:51:39 -07001044 mad_send_wr->mad_agent_priv = mad_agent_priv;
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001045 mad_send_wr->sg_list[0].length = hdr_len;
Jason Gunthorpe4be90bc2015-07-30 17:22:16 -06001046 mad_send_wr->sg_list[0].lkey = mad_agent->qp->pd->local_dma_lkey;
Ira Weiny548ead12015-06-06 14:38:33 -04001047
1048 /* OPA MADs don't have to be the full 2048 bytes */
1049 if (opa && base_version == OPA_MGMT_BASE_VERSION &&
1050 data_len < mad_size - hdr_len)
1051 mad_send_wr->sg_list[1].length = data_len;
1052 else
1053 mad_send_wr->sg_list[1].length = mad_size - hdr_len;
1054
Jason Gunthorpe4be90bc2015-07-30 17:22:16 -06001055 mad_send_wr->sg_list[1].lkey = mad_agent->qp->pd->local_dma_lkey;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -07001056
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08001057 mad_send_wr->mad_list.cqe.done = ib_mad_send_done;
1058
1059 mad_send_wr->send_wr.wr.wr_cqe = &mad_send_wr->mad_list.cqe;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001060 mad_send_wr->send_wr.wr.sg_list = mad_send_wr->sg_list;
1061 mad_send_wr->send_wr.wr.num_sge = 2;
1062 mad_send_wr->send_wr.wr.opcode = IB_WR_SEND;
1063 mad_send_wr->send_wr.wr.send_flags = IB_SEND_SIGNALED;
1064 mad_send_wr->send_wr.remote_qpn = remote_qpn;
1065 mad_send_wr->send_wr.remote_qkey = IB_QP_SET_QKEY;
1066 mad_send_wr->send_wr.pkey_index = pkey_index;
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001067
1068 if (rmpp_active) {
Ira Weiny548ead12015-06-06 14:38:33 -04001069 ret = alloc_send_rmpp_list(mad_send_wr, mad_size, gfp_mask);
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001070 if (ret) {
1071 kfree(buf);
1072 return ERR_PTR(ret);
1073 }
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001074 }
1075
Sean Hefty34816ad2005-10-25 10:51:39 -07001076 mad_send_wr->send_buf.mad_agent = mad_agent;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -07001077 atomic_inc(&mad_agent_priv->refcount);
Sean Hefty34816ad2005-10-25 10:51:39 -07001078 return &mad_send_wr->send_buf;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -07001079}
1080EXPORT_SYMBOL(ib_create_send_mad);
1081
Hal Rosenstock618a3c02006-03-28 16:40:04 -08001082int ib_get_mad_data_offset(u8 mgmt_class)
1083{
1084 if (mgmt_class == IB_MGMT_CLASS_SUBN_ADM)
1085 return IB_MGMT_SA_HDR;
1086 else if ((mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
1087 (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
1088 (mgmt_class == IB_MGMT_CLASS_BIS))
1089 return IB_MGMT_DEVICE_HDR;
1090 else if ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
1091 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END))
1092 return IB_MGMT_VENDOR_HDR;
1093 else
1094 return IB_MGMT_MAD_HDR;
1095}
1096EXPORT_SYMBOL(ib_get_mad_data_offset);
1097
1098int ib_is_mad_class_rmpp(u8 mgmt_class)
1099{
1100 if ((mgmt_class == IB_MGMT_CLASS_SUBN_ADM) ||
1101 (mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
1102 (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
1103 (mgmt_class == IB_MGMT_CLASS_BIS) ||
1104 ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
1105 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END)))
1106 return 1;
1107 return 0;
1108}
1109EXPORT_SYMBOL(ib_is_mad_class_rmpp);
1110
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001111void *ib_get_rmpp_segment(struct ib_mad_send_buf *send_buf, int seg_num)
1112{
1113 struct ib_mad_send_wr_private *mad_send_wr;
1114 struct list_head *list;
1115
1116 mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
1117 send_buf);
1118 list = &mad_send_wr->cur_seg->list;
1119
1120 if (mad_send_wr->cur_seg->num < seg_num) {
1121 list_for_each_entry(mad_send_wr->cur_seg, list, list)
1122 if (mad_send_wr->cur_seg->num == seg_num)
1123 break;
1124 } else if (mad_send_wr->cur_seg->num > seg_num) {
1125 list_for_each_entry_reverse(mad_send_wr->cur_seg, list, list)
1126 if (mad_send_wr->cur_seg->num == seg_num)
1127 break;
1128 }
1129 return mad_send_wr->cur_seg->data;
1130}
1131EXPORT_SYMBOL(ib_get_rmpp_segment);
1132
1133static inline void *ib_get_payload(struct ib_mad_send_wr_private *mad_send_wr)
1134{
1135 if (mad_send_wr->send_buf.seg_count)
1136 return ib_get_rmpp_segment(&mad_send_wr->send_buf,
1137 mad_send_wr->seg_num);
1138 else
1139 return mad_send_wr->send_buf.mad +
1140 mad_send_wr->send_buf.hdr_len;
1141}
1142
Hal Rosenstock824c8ae2005-07-27 11:45:23 -07001143void ib_free_send_mad(struct ib_mad_send_buf *send_buf)
1144{
1145 struct ib_mad_agent_private *mad_agent_priv;
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001146 struct ib_mad_send_wr_private *mad_send_wr;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -07001147
1148 mad_agent_priv = container_of(send_buf->mad_agent,
1149 struct ib_mad_agent_private, agent);
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001150 mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
1151 send_buf);
Hal Rosenstock824c8ae2005-07-27 11:45:23 -07001152
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001153 free_send_rmpp_list(mad_send_wr);
1154 kfree(send_buf->mad);
Sean Hefty1b52fa982006-05-12 14:57:52 -07001155 deref_mad_agent(mad_agent_priv);
Hal Rosenstock824c8ae2005-07-27 11:45:23 -07001156}
1157EXPORT_SYMBOL(ib_free_send_mad);
1158
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001159int ib_send_mad(struct ib_mad_send_wr_private *mad_send_wr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
1161 struct ib_mad_qp_info *qp_info;
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07001162 struct list_head *list;
Sean Hefty34816ad2005-10-25 10:51:39 -07001163 struct ib_send_wr *bad_send_wr;
1164 struct ib_mad_agent *mad_agent;
1165 struct ib_sge *sge;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 unsigned long flags;
1167 int ret;
1168
Hal Rosenstockf8197a42005-07-27 11:45:24 -07001169 /* Set WR ID to find mad_send_wr upon completion */
Hal Rosenstockd760ce82005-07-27 11:45:25 -07001170 qp_info = mad_send_wr->mad_agent_priv->qp_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 mad_send_wr->mad_list.mad_queue = &qp_info->send_queue;
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08001172 mad_send_wr->mad_list.cqe.done = ib_mad_send_done;
1173 mad_send_wr->send_wr.wr.wr_cqe = &mad_send_wr->mad_list.cqe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Sean Hefty34816ad2005-10-25 10:51:39 -07001175 mad_agent = mad_send_wr->send_buf.mad_agent;
1176 sge = mad_send_wr->sg_list;
Ralph Campbell15271062006-12-12 14:28:30 -08001177 sge[0].addr = ib_dma_map_single(mad_agent->device,
1178 mad_send_wr->send_buf.mad,
1179 sge[0].length,
1180 DMA_TO_DEVICE);
Yan Burman2c34e682014-03-11 14:41:47 +02001181 if (unlikely(ib_dma_mapping_error(mad_agent->device, sge[0].addr)))
1182 return -ENOMEM;
1183
Ralph Campbell15271062006-12-12 14:28:30 -08001184 mad_send_wr->header_mapping = sge[0].addr;
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001185
Ralph Campbell15271062006-12-12 14:28:30 -08001186 sge[1].addr = ib_dma_map_single(mad_agent->device,
1187 ib_get_payload(mad_send_wr),
1188 sge[1].length,
1189 DMA_TO_DEVICE);
Yan Burman2c34e682014-03-11 14:41:47 +02001190 if (unlikely(ib_dma_mapping_error(mad_agent->device, sge[1].addr))) {
1191 ib_dma_unmap_single(mad_agent->device,
1192 mad_send_wr->header_mapping,
1193 sge[0].length, DMA_TO_DEVICE);
1194 return -ENOMEM;
1195 }
Ralph Campbell15271062006-12-12 14:28:30 -08001196 mad_send_wr->payload_mapping = sge[1].addr;
Sean Hefty34816ad2005-10-25 10:51:39 -07001197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 spin_lock_irqsave(&qp_info->send_queue.lock, flags);
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07001199 if (qp_info->send_queue.count < qp_info->send_queue.max_active) {
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001200 ret = ib_post_send(mad_agent->qp, &mad_send_wr->send_wr.wr,
Sean Hefty34816ad2005-10-25 10:51:39 -07001201 &bad_send_wr);
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07001202 list = &qp_info->send_queue.list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 ret = 0;
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07001205 list = &qp_info->overflow_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07001207
1208 if (!ret) {
1209 qp_info->send_queue.count++;
1210 list_add_tail(&mad_send_wr->mad_list.list, list);
1211 }
1212 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001213 if (ret) {
Ralph Campbell15271062006-12-12 14:28:30 -08001214 ib_dma_unmap_single(mad_agent->device,
1215 mad_send_wr->header_mapping,
1216 sge[0].length, DMA_TO_DEVICE);
1217 ib_dma_unmap_single(mad_agent->device,
1218 mad_send_wr->payload_mapping,
1219 sge[1].length, DMA_TO_DEVICE);
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 return ret;
1222}
1223
1224/*
1225 * ib_post_send_mad - Posts MAD(s) to the send queue of the QP associated
1226 * with the registered client
1227 */
Sean Hefty34816ad2005-10-25 10:51:39 -07001228int ib_post_send_mad(struct ib_mad_send_buf *send_buf,
1229 struct ib_mad_send_buf **bad_send_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 struct ib_mad_agent_private *mad_agent_priv;
Sean Hefty34816ad2005-10-25 10:51:39 -07001232 struct ib_mad_send_buf *next_send_buf;
1233 struct ib_mad_send_wr_private *mad_send_wr;
1234 unsigned long flags;
1235 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 /* Walk list of send WRs and post each on send list */
Sean Hefty34816ad2005-10-25 10:51:39 -07001238 for (; send_buf; send_buf = next_send_buf) {
Sean Hefty34816ad2005-10-25 10:51:39 -07001239 mad_send_wr = container_of(send_buf,
1240 struct ib_mad_send_wr_private,
1241 send_buf);
1242 mad_agent_priv = mad_send_wr->mad_agent_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Daniel Jurgens47a2b332017-05-19 15:48:54 +03001244 ret = ib_mad_enforce_security(mad_agent_priv,
1245 mad_send_wr->send_wr.pkey_index);
1246 if (ret)
1247 goto error;
1248
Sean Hefty34816ad2005-10-25 10:51:39 -07001249 if (!send_buf->mad_agent->send_handler ||
1250 (send_buf->timeout_ms &&
1251 !send_buf->mad_agent->recv_handler)) {
1252 ret = -EINVAL;
1253 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
1255
Hal Rosenstock618a3c02006-03-28 16:40:04 -08001256 if (!ib_is_mad_class_rmpp(((struct ib_mad_hdr *) send_buf->mad)->mgmt_class)) {
1257 if (mad_agent_priv->agent.rmpp_version) {
1258 ret = -EINVAL;
1259 goto error;
1260 }
1261 }
1262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 /*
1264 * Save pointer to next work request to post in case the
1265 * current one completes, and the user modifies the work
1266 * request associated with the completion
1267 */
Sean Hefty34816ad2005-10-25 10:51:39 -07001268 next_send_buf = send_buf->next;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001269 mad_send_wr->send_wr.ah = send_buf->ah;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Sean Hefty34816ad2005-10-25 10:51:39 -07001271 if (((struct ib_mad_hdr *) send_buf->mad)->mgmt_class ==
1272 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
1273 ret = handle_outgoing_dr_smp(mad_agent_priv,
1274 mad_send_wr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if (ret < 0) /* error */
Sean Hefty34816ad2005-10-25 10:51:39 -07001276 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 else if (ret == 1) /* locally consumed */
Sean Hefty34816ad2005-10-25 10:51:39 -07001278 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
1280
Sean Hefty34816ad2005-10-25 10:51:39 -07001281 mad_send_wr->tid = ((struct ib_mad_hdr *) send_buf->mad)->tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 /* Timeout will be updated after send completes */
Sean Hefty34816ad2005-10-25 10:51:39 -07001283 mad_send_wr->timeout = msecs_to_jiffies(send_buf->timeout_ms);
Sean Hefty4fc8cd42007-11-27 00:11:04 -08001284 mad_send_wr->max_retries = send_buf->retries;
1285 mad_send_wr->retries_left = send_buf->retries;
1286 send_buf->retries = 0;
Sean Hefty34816ad2005-10-25 10:51:39 -07001287 /* Reference for work request to QP + response */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 mad_send_wr->refcount = 1 + (mad_send_wr->timeout > 0);
1289 mad_send_wr->status = IB_WC_SUCCESS;
1290
1291 /* Reference MAD agent until send completes */
1292 atomic_inc(&mad_agent_priv->refcount);
1293 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1294 list_add_tail(&mad_send_wr->agent_list,
1295 &mad_agent_priv->send_list);
1296 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1297
Ira Weiny1471cb62014-08-08 19:00:56 -04001298 if (ib_mad_kernel_rmpp_agent(&mad_agent_priv->agent)) {
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001299 ret = ib_send_rmpp_mad(mad_send_wr);
1300 if (ret >= 0 && ret != IB_RMPP_RESULT_CONSUMED)
1301 ret = ib_send_mad(mad_send_wr);
1302 } else
1303 ret = ib_send_mad(mad_send_wr);
1304 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 /* Fail send request */
1306 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1307 list_del(&mad_send_wr->agent_list);
1308 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1309 atomic_dec(&mad_agent_priv->refcount);
Sean Hefty34816ad2005-10-25 10:51:39 -07001310 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
1313 return 0;
Sean Hefty34816ad2005-10-25 10:51:39 -07001314error:
1315 if (bad_send_buf)
1316 *bad_send_buf = send_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 return ret;
1318}
1319EXPORT_SYMBOL(ib_post_send_mad);
1320
1321/*
1322 * ib_free_recv_mad - Returns data buffers used to receive
1323 * a MAD to the access layer
1324 */
1325void ib_free_recv_mad(struct ib_mad_recv_wc *mad_recv_wc)
1326{
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001327 struct ib_mad_recv_buf *mad_recv_buf, *temp_recv_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 struct ib_mad_private_header *mad_priv_hdr;
1329 struct ib_mad_private *priv;
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001330 struct list_head free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001332 INIT_LIST_HEAD(&free_list);
1333 list_splice_init(&mad_recv_wc->rmpp_list, &free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001335 list_for_each_entry_safe(mad_recv_buf, temp_recv_buf,
1336 &free_list, list) {
1337 mad_recv_wc = container_of(mad_recv_buf, struct ib_mad_recv_wc,
1338 recv_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 mad_priv_hdr = container_of(mad_recv_wc,
1340 struct ib_mad_private_header,
1341 recv_wc);
1342 priv = container_of(mad_priv_hdr, struct ib_mad_private,
1343 header);
Ira Weinyc9082e52015-06-06 14:38:30 -04001344 kfree(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346}
1347EXPORT_SYMBOL(ib_free_recv_mad);
1348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349struct ib_mad_agent *ib_redirect_mad_qp(struct ib_qp *qp,
1350 u8 rmpp_version,
1351 ib_mad_send_handler send_handler,
1352 ib_mad_recv_handler recv_handler,
1353 void *context)
1354{
1355 return ERR_PTR(-EINVAL); /* XXX: for now */
1356}
1357EXPORT_SYMBOL(ib_redirect_mad_qp);
1358
1359int ib_process_mad_wc(struct ib_mad_agent *mad_agent,
1360 struct ib_wc *wc)
1361{
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04001362 dev_err(&mad_agent->device->dev,
1363 "ib_process_mad_wc() not implemented yet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 return 0;
1365}
1366EXPORT_SYMBOL(ib_process_mad_wc);
1367
1368static int method_in_use(struct ib_mad_mgmt_method_table **method,
1369 struct ib_mad_reg_req *mad_reg_req)
1370{
1371 int i;
1372
Akinobu Mita19b629f2010-03-05 13:41:38 -08001373 for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 if ((*method)->agent[i]) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04001375 pr_err("Method %d already in use\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 return -EINVAL;
1377 }
1378 }
1379 return 0;
1380}
1381
1382static int allocate_method_table(struct ib_mad_mgmt_method_table **method)
1383{
1384 /* Allocate management method table */
Roland Dreierde6eb662005-11-02 07:23:14 -08001385 *method = kzalloc(sizeof **method, GFP_ATOMIC);
Leon Romanovsky27162432016-11-03 16:44:09 +02001386 return (*method) ? 0 : (-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
1388
1389/*
1390 * Check to see if there are any methods still in use
1391 */
1392static int check_method_table(struct ib_mad_mgmt_method_table *method)
1393{
1394 int i;
1395
1396 for (i = 0; i < IB_MGMT_MAX_METHODS; i++)
1397 if (method->agent[i])
1398 return 1;
1399 return 0;
1400}
1401
1402/*
1403 * Check to see if there are any method tables for this class still in use
1404 */
1405static int check_class_table(struct ib_mad_mgmt_class_table *class)
1406{
1407 int i;
1408
1409 for (i = 0; i < MAX_MGMT_CLASS; i++)
1410 if (class->method_table[i])
1411 return 1;
1412 return 0;
1413}
1414
1415static int check_vendor_class(struct ib_mad_mgmt_vendor_class *vendor_class)
1416{
1417 int i;
1418
1419 for (i = 0; i < MAX_MGMT_OUI; i++)
1420 if (vendor_class->method_table[i])
1421 return 1;
1422 return 0;
1423}
1424
1425static int find_vendor_oui(struct ib_mad_mgmt_vendor_class *vendor_class,
Ira Weinyd94bd262015-06-06 14:38:22 -04001426 const char *oui)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
1428 int i;
1429
1430 for (i = 0; i < MAX_MGMT_OUI; i++)
Roland Dreier3cd96562006-09-22 15:22:46 -07001431 /* Is there matching OUI for this vendor class ? */
1432 if (!memcmp(vendor_class->oui[i], oui, 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 return i;
1434
1435 return -1;
1436}
1437
1438static int check_vendor_table(struct ib_mad_mgmt_vendor_class_table *vendor)
1439{
1440 int i;
1441
1442 for (i = 0; i < MAX_MGMT_VENDOR_RANGE2; i++)
1443 if (vendor->vendor_class[i])
1444 return 1;
1445
1446 return 0;
1447}
1448
1449static void remove_methods_mad_agent(struct ib_mad_mgmt_method_table *method,
1450 struct ib_mad_agent_private *agent)
1451{
1452 int i;
1453
1454 /* Remove any methods for this mad agent */
1455 for (i = 0; i < IB_MGMT_MAX_METHODS; i++) {
1456 if (method->agent[i] == agent) {
1457 method->agent[i] = NULL;
1458 }
1459 }
1460}
1461
1462static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1463 struct ib_mad_agent_private *agent_priv,
1464 u8 mgmt_class)
1465{
1466 struct ib_mad_port_private *port_priv;
1467 struct ib_mad_mgmt_class_table **class;
1468 struct ib_mad_mgmt_method_table **method;
1469 int i, ret;
1470
1471 port_priv = agent_priv->qp_info->port_priv;
1472 class = &port_priv->version[mad_reg_req->mgmt_class_version].class;
1473 if (!*class) {
1474 /* Allocate management class table for "new" class version */
Roland Dreierde6eb662005-11-02 07:23:14 -08001475 *class = kzalloc(sizeof **class, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 if (!*class) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 ret = -ENOMEM;
1478 goto error1;
1479 }
Roland Dreierde6eb662005-11-02 07:23:14 -08001480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 /* Allocate method table for this management class */
1482 method = &(*class)->method_table[mgmt_class];
1483 if ((ret = allocate_method_table(method)))
1484 goto error2;
1485 } else {
1486 method = &(*class)->method_table[mgmt_class];
1487 if (!*method) {
1488 /* Allocate method table for this management class */
1489 if ((ret = allocate_method_table(method)))
1490 goto error1;
1491 }
1492 }
1493
1494 /* Now, make sure methods are not already in use */
1495 if (method_in_use(method, mad_reg_req))
1496 goto error3;
1497
1498 /* Finally, add in methods being registered */
Akinobu Mita19b629f2010-03-05 13:41:38 -08001499 for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 (*method)->agent[i] = agent_priv;
Akinobu Mita19b629f2010-03-05 13:41:38 -08001501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 return 0;
1503
1504error3:
1505 /* Remove any methods for this mad agent */
1506 remove_methods_mad_agent(*method, agent_priv);
1507 /* Now, check to see if there are any methods in use */
1508 if (!check_method_table(*method)) {
1509 /* If not, release management method table */
1510 kfree(*method);
1511 *method = NULL;
1512 }
1513 ret = -EINVAL;
1514 goto error1;
1515error2:
1516 kfree(*class);
1517 *class = NULL;
1518error1:
1519 return ret;
1520}
1521
1522static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1523 struct ib_mad_agent_private *agent_priv)
1524{
1525 struct ib_mad_port_private *port_priv;
1526 struct ib_mad_mgmt_vendor_class_table **vendor_table;
1527 struct ib_mad_mgmt_vendor_class_table *vendor = NULL;
1528 struct ib_mad_mgmt_vendor_class *vendor_class = NULL;
1529 struct ib_mad_mgmt_method_table **method;
1530 int i, ret = -ENOMEM;
1531 u8 vclass;
1532
1533 /* "New" vendor (with OUI) class */
1534 vclass = vendor_class_index(mad_reg_req->mgmt_class);
1535 port_priv = agent_priv->qp_info->port_priv;
1536 vendor_table = &port_priv->version[
1537 mad_reg_req->mgmt_class_version].vendor;
1538 if (!*vendor_table) {
1539 /* Allocate mgmt vendor class table for "new" class version */
Roland Dreierde6eb662005-11-02 07:23:14 -08001540 vendor = kzalloc(sizeof *vendor, GFP_ATOMIC);
Leon Romanovsky27162432016-11-03 16:44:09 +02001541 if (!vendor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 goto error1;
Roland Dreierde6eb662005-11-02 07:23:14 -08001543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 *vendor_table = vendor;
1545 }
1546 if (!(*vendor_table)->vendor_class[vclass]) {
1547 /* Allocate table for this management vendor class */
Roland Dreierde6eb662005-11-02 07:23:14 -08001548 vendor_class = kzalloc(sizeof *vendor_class, GFP_ATOMIC);
Leon Romanovsky27162432016-11-03 16:44:09 +02001549 if (!vendor_class)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 goto error2;
Roland Dreierde6eb662005-11-02 07:23:14 -08001551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 (*vendor_table)->vendor_class[vclass] = vendor_class;
1553 }
1554 for (i = 0; i < MAX_MGMT_OUI; i++) {
1555 /* Is there matching OUI for this vendor class ? */
1556 if (!memcmp((*vendor_table)->vendor_class[vclass]->oui[i],
1557 mad_reg_req->oui, 3)) {
1558 method = &(*vendor_table)->vendor_class[
1559 vclass]->method_table[i];
1560 BUG_ON(!*method);
1561 goto check_in_use;
1562 }
1563 }
1564 for (i = 0; i < MAX_MGMT_OUI; i++) {
1565 /* OUI slot available ? */
1566 if (!is_vendor_oui((*vendor_table)->vendor_class[
1567 vclass]->oui[i])) {
1568 method = &(*vendor_table)->vendor_class[
1569 vclass]->method_table[i];
1570 BUG_ON(*method);
1571 /* Allocate method table for this OUI */
1572 if ((ret = allocate_method_table(method)))
1573 goto error3;
1574 memcpy((*vendor_table)->vendor_class[vclass]->oui[i],
1575 mad_reg_req->oui, 3);
1576 goto check_in_use;
1577 }
1578 }
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04001579 dev_err(&agent_priv->agent.device->dev, "All OUI slots in use\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 goto error3;
1581
1582check_in_use:
1583 /* Now, make sure methods are not already in use */
1584 if (method_in_use(method, mad_reg_req))
1585 goto error4;
1586
1587 /* Finally, add in methods being registered */
Akinobu Mita19b629f2010-03-05 13:41:38 -08001588 for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 (*method)->agent[i] = agent_priv;
Akinobu Mita19b629f2010-03-05 13:41:38 -08001590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 return 0;
1592
1593error4:
1594 /* Remove any methods for this mad agent */
1595 remove_methods_mad_agent(*method, agent_priv);
1596 /* Now, check to see if there are any methods in use */
1597 if (!check_method_table(*method)) {
1598 /* If not, release management method table */
1599 kfree(*method);
1600 *method = NULL;
1601 }
1602 ret = -EINVAL;
1603error3:
1604 if (vendor_class) {
1605 (*vendor_table)->vendor_class[vclass] = NULL;
1606 kfree(vendor_class);
1607 }
1608error2:
1609 if (vendor) {
1610 *vendor_table = NULL;
1611 kfree(vendor);
1612 }
1613error1:
1614 return ret;
1615}
1616
1617static void remove_mad_reg_req(struct ib_mad_agent_private *agent_priv)
1618{
1619 struct ib_mad_port_private *port_priv;
1620 struct ib_mad_mgmt_class_table *class;
1621 struct ib_mad_mgmt_method_table *method;
1622 struct ib_mad_mgmt_vendor_class_table *vendor;
1623 struct ib_mad_mgmt_vendor_class *vendor_class;
1624 int index;
1625 u8 mgmt_class;
1626
1627 /*
1628 * Was MAD registration request supplied
1629 * with original registration ?
1630 */
1631 if (!agent_priv->reg_req) {
1632 goto out;
1633 }
1634
1635 port_priv = agent_priv->qp_info->port_priv;
1636 mgmt_class = convert_mgmt_class(agent_priv->reg_req->mgmt_class);
1637 class = port_priv->version[
1638 agent_priv->reg_req->mgmt_class_version].class;
1639 if (!class)
1640 goto vendor_check;
1641
1642 method = class->method_table[mgmt_class];
1643 if (method) {
1644 /* Remove any methods for this mad agent */
1645 remove_methods_mad_agent(method, agent_priv);
1646 /* Now, check to see if there are any methods still in use */
1647 if (!check_method_table(method)) {
1648 /* If not, release management method table */
Bart Van Assche2190d102016-06-03 12:08:44 -07001649 kfree(method);
1650 class->method_table[mgmt_class] = NULL;
1651 /* Any management classes left ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 if (!check_class_table(class)) {
1653 /* If not, release management class table */
1654 kfree(class);
1655 port_priv->version[
1656 agent_priv->reg_req->
1657 mgmt_class_version].class = NULL;
1658 }
1659 }
1660 }
1661
1662vendor_check:
1663 if (!is_vendor_class(mgmt_class))
1664 goto out;
1665
1666 /* normalize mgmt_class to vendor range 2 */
1667 mgmt_class = vendor_class_index(agent_priv->reg_req->mgmt_class);
1668 vendor = port_priv->version[
1669 agent_priv->reg_req->mgmt_class_version].vendor;
1670
1671 if (!vendor)
1672 goto out;
1673
1674 vendor_class = vendor->vendor_class[mgmt_class];
1675 if (vendor_class) {
1676 index = find_vendor_oui(vendor_class, agent_priv->reg_req->oui);
1677 if (index < 0)
1678 goto out;
1679 method = vendor_class->method_table[index];
1680 if (method) {
1681 /* Remove any methods for this mad agent */
1682 remove_methods_mad_agent(method, agent_priv);
1683 /*
1684 * Now, check to see if there are
1685 * any methods still in use
1686 */
1687 if (!check_method_table(method)) {
1688 /* If not, release management method table */
1689 kfree(method);
1690 vendor_class->method_table[index] = NULL;
1691 memset(vendor_class->oui[index], 0, 3);
1692 /* Any OUIs left ? */
1693 if (!check_vendor_class(vendor_class)) {
1694 /* If not, release vendor class table */
1695 kfree(vendor_class);
1696 vendor->vendor_class[mgmt_class] = NULL;
1697 /* Any other vendor classes left ? */
1698 if (!check_vendor_table(vendor)) {
1699 kfree(vendor);
1700 port_priv->version[
1701 agent_priv->reg_req->
1702 mgmt_class_version].
1703 vendor = NULL;
1704 }
1705 }
1706 }
1707 }
1708 }
1709
1710out:
1711 return;
1712}
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714static struct ib_mad_agent_private *
1715find_mad_agent(struct ib_mad_port_private *port_priv,
Ira Weinyd94bd262015-06-06 14:38:22 -04001716 const struct ib_mad_hdr *mad_hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717{
1718 struct ib_mad_agent_private *mad_agent = NULL;
1719 unsigned long flags;
1720
1721 spin_lock_irqsave(&port_priv->reg_lock, flags);
Ira Weinyd94bd262015-06-06 14:38:22 -04001722 if (ib_response_mad(mad_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 u32 hi_tid;
1724 struct ib_mad_agent_private *entry;
1725
1726 /*
1727 * Routing is based on high 32 bits of transaction ID
1728 * of MAD.
1729 */
Ira Weinyd94bd262015-06-06 14:38:22 -04001730 hi_tid = be64_to_cpu(mad_hdr->tid) >> 32;
Sean Hefty34816ad2005-10-25 10:51:39 -07001731 list_for_each_entry(entry, &port_priv->agent_list, agent_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 if (entry->agent.hi_tid == hi_tid) {
1733 mad_agent = entry;
1734 break;
1735 }
1736 }
1737 } else {
1738 struct ib_mad_mgmt_class_table *class;
1739 struct ib_mad_mgmt_method_table *method;
1740 struct ib_mad_mgmt_vendor_class_table *vendor;
1741 struct ib_mad_mgmt_vendor_class *vendor_class;
Ira Weinyd94bd262015-06-06 14:38:22 -04001742 const struct ib_vendor_mad *vendor_mad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 int index;
1744
1745 /*
1746 * Routing is based on version, class, and method
1747 * For "newer" vendor MADs, also based on OUI
1748 */
Ira Weinyd94bd262015-06-06 14:38:22 -04001749 if (mad_hdr->class_version >= MAX_MGMT_VERSION)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 goto out;
Ira Weinyd94bd262015-06-06 14:38:22 -04001751 if (!is_vendor_class(mad_hdr->mgmt_class)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 class = port_priv->version[
Ira Weinyd94bd262015-06-06 14:38:22 -04001753 mad_hdr->class_version].class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 if (!class)
1755 goto out;
Ira Weinyd94bd262015-06-06 14:38:22 -04001756 if (convert_mgmt_class(mad_hdr->mgmt_class) >=
Bart Van Assche2fe2f372016-11-21 10:21:17 -08001757 ARRAY_SIZE(class->method_table))
Hefty, Seanb7ab0b12011-10-06 09:33:05 -07001758 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 method = class->method_table[convert_mgmt_class(
Ira Weinyd94bd262015-06-06 14:38:22 -04001760 mad_hdr->mgmt_class)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 if (method)
Ira Weinyd94bd262015-06-06 14:38:22 -04001762 mad_agent = method->agent[mad_hdr->method &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 ~IB_MGMT_METHOD_RESP];
1764 } else {
1765 vendor = port_priv->version[
Ira Weinyd94bd262015-06-06 14:38:22 -04001766 mad_hdr->class_version].vendor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 if (!vendor)
1768 goto out;
1769 vendor_class = vendor->vendor_class[vendor_class_index(
Ira Weinyd94bd262015-06-06 14:38:22 -04001770 mad_hdr->mgmt_class)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 if (!vendor_class)
1772 goto out;
1773 /* Find matching OUI */
Ira Weinyd94bd262015-06-06 14:38:22 -04001774 vendor_mad = (const struct ib_vendor_mad *)mad_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 index = find_vendor_oui(vendor_class, vendor_mad->oui);
1776 if (index == -1)
1777 goto out;
1778 method = vendor_class->method_table[index];
1779 if (method) {
Ira Weinyd94bd262015-06-06 14:38:22 -04001780 mad_agent = method->agent[mad_hdr->method &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 ~IB_MGMT_METHOD_RESP];
1782 }
1783 }
1784 }
1785
1786 if (mad_agent) {
1787 if (mad_agent->agent.recv_handler)
1788 atomic_inc(&mad_agent->refcount);
1789 else {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04001790 dev_notice(&port_priv->device->dev,
1791 "No receive handler for client %p on port %d\n",
1792 &mad_agent->agent, port_priv->port_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 mad_agent = NULL;
1794 }
1795 }
1796out:
1797 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
1798
1799 return mad_agent;
1800}
1801
Ira Weiny8e4349d2015-06-10 16:16:48 -04001802static int validate_mad(const struct ib_mad_hdr *mad_hdr,
1803 const struct ib_mad_qp_info *qp_info,
1804 bool opa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
1806 int valid = 0;
Ira Weiny8e4349d2015-06-10 16:16:48 -04001807 u32 qp_num = qp_info->qp->qp_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 /* Make sure MAD base version is understood */
Ira Weiny8e4349d2015-06-10 16:16:48 -04001810 if (mad_hdr->base_version != IB_MGMT_BASE_VERSION &&
1811 (!opa || mad_hdr->base_version != OPA_MGMT_BASE_VERSION)) {
1812 pr_err("MAD received with unsupported base version %d %s\n",
1813 mad_hdr->base_version, opa ? "(opa)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 goto out;
1815 }
1816
1817 /* Filter SMI packets sent to other than QP0 */
Ira Weiny77f60832015-05-08 14:27:21 -04001818 if ((mad_hdr->mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
1819 (mad_hdr->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 if (qp_num == 0)
1821 valid = 1;
1822 } else {
Hal Rosenstock53370882015-11-13 15:22:22 -05001823 /* CM attributes other than ClassPortInfo only use Send method */
1824 if ((mad_hdr->mgmt_class == IB_MGMT_CLASS_CM) &&
1825 (mad_hdr->attr_id != IB_MGMT_CLASSPORTINFO_ATTR_ID) &&
1826 (mad_hdr->method != IB_MGMT_METHOD_SEND))
1827 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 /* Filter GSI packets sent to QP0 */
1829 if (qp_num != 0)
1830 valid = 1;
1831 }
1832
1833out:
1834 return valid;
1835}
1836
Ira Weinyf766c582015-05-08 14:27:24 -04001837static int is_rmpp_data_mad(const struct ib_mad_agent_private *mad_agent_priv,
1838 const struct ib_mad_hdr *mad_hdr)
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001839{
1840 struct ib_rmpp_mad *rmpp_mad;
1841
1842 rmpp_mad = (struct ib_rmpp_mad *)mad_hdr;
1843 return !mad_agent_priv->agent.rmpp_version ||
Ira Weiny1471cb62014-08-08 19:00:56 -04001844 !ib_mad_kernel_rmpp_agent(&mad_agent_priv->agent) ||
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001845 !(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
1846 IB_MGMT_RMPP_FLAG_ACTIVE) ||
1847 (rmpp_mad->rmpp_hdr.rmpp_type == IB_MGMT_RMPP_TYPE_DATA);
1848}
1849
Ira Weiny8bf4b302015-05-08 14:27:23 -04001850static inline int rcv_has_same_class(const struct ib_mad_send_wr_private *wr,
1851 const struct ib_mad_recv_wc *rwc)
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001852{
Ira Weiny8bf4b302015-05-08 14:27:23 -04001853 return ((struct ib_mad_hdr *)(wr->send_buf.mad))->mgmt_class ==
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001854 rwc->recv_buf.mad->mad_hdr.mgmt_class;
1855}
1856
Ira Weinyf766c582015-05-08 14:27:24 -04001857static inline int rcv_has_same_gid(const struct ib_mad_agent_private *mad_agent_priv,
1858 const struct ib_mad_send_wr_private *wr,
1859 const struct ib_mad_recv_wc *rwc )
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001860{
Dasaratharaman Chandramouli90898852017-04-29 14:41:18 -04001861 struct rdma_ah_attr attr;
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001862 u8 send_resp, rcv_resp;
Jack Morgenstein9874e742006-06-17 20:37:34 -07001863 union ib_gid sgid;
1864 struct ib_device *device = mad_agent_priv->agent.device;
1865 u8 port_num = mad_agent_priv->agent.port_num;
1866 u8 lmc;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04001867 bool has_grh;
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001868
Ira Weiny96909302015-05-08 14:27:22 -04001869 send_resp = ib_response_mad((struct ib_mad_hdr *)wr->send_buf.mad);
1870 rcv_resp = ib_response_mad(&rwc->recv_buf.mad->mad_hdr);
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001871
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001872 if (send_resp == rcv_resp)
1873 /* both requests, or both responses. GIDs different */
1874 return 0;
1875
Dasaratharaman Chandramoulibfbfd662017-04-29 14:41:21 -04001876 if (rdma_query_ah(wr->send_buf.ah, &attr))
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001877 /* Assume not equal, to avoid false positives. */
1878 return 0;
1879
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04001880 has_grh = !!(rdma_ah_get_ah_flags(&attr) & IB_AH_GRH);
1881 if (has_grh != !!(rwc->wc->wc_flags & IB_WC_GRH))
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001882 /* one has GID, other does not. Assume different */
1883 return 0;
Jack Morgenstein9874e742006-06-17 20:37:34 -07001884
1885 if (!send_resp && rcv_resp) {
1886 /* is request/response. */
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04001887 if (!has_grh) {
Jack Morgenstein9874e742006-06-17 20:37:34 -07001888 if (ib_get_cached_lmc(device, port_num, &lmc))
1889 return 0;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04001890 return (!lmc || !((rdma_ah_get_path_bits(&attr) ^
Jack Morgenstein9874e742006-06-17 20:37:34 -07001891 rwc->wc->dlid_path_bits) &
1892 ((1 << lmc) - 1)));
1893 } else {
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04001894 const struct ib_global_route *grh =
1895 rdma_ah_read_grh(&attr);
1896
Jack Morgenstein9874e742006-06-17 20:37:34 -07001897 if (ib_get_cached_gid(device, port_num,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04001898 grh->sgid_index, &sgid, NULL))
Jack Morgenstein9874e742006-06-17 20:37:34 -07001899 return 0;
1900 return !memcmp(sgid.raw, rwc->recv_buf.grh->dgid.raw,
1901 16);
1902 }
1903 }
1904
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04001905 if (!has_grh)
1906 return rdma_ah_get_dlid(&attr) == rwc->wc->slid;
Jack Morgenstein9874e742006-06-17 20:37:34 -07001907 else
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04001908 return !memcmp(rdma_ah_read_grh(&attr)->dgid.raw,
1909 rwc->recv_buf.grh->sgid.raw,
Jack Morgenstein9874e742006-06-17 20:37:34 -07001910 16);
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001911}
Jack Morgenstein9874e742006-06-17 20:37:34 -07001912
1913static inline int is_direct(u8 class)
1914{
1915 return (class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE);
1916}
1917
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001918struct ib_mad_send_wr_private*
Ira Weinyf766c582015-05-08 14:27:24 -04001919ib_find_send_mad(const struct ib_mad_agent_private *mad_agent_priv,
1920 const struct ib_mad_recv_wc *wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
Jack Morgenstein9874e742006-06-17 20:37:34 -07001922 struct ib_mad_send_wr_private *wr;
Ira Weiny83a1d222015-06-06 14:38:23 -04001923 const struct ib_mad_hdr *mad_hdr;
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001924
Ira Weiny83a1d222015-06-06 14:38:23 -04001925 mad_hdr = &wc->recv_buf.mad->mad_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Jack Morgenstein9874e742006-06-17 20:37:34 -07001927 list_for_each_entry(wr, &mad_agent_priv->wait_list, agent_list) {
Ira Weiny83a1d222015-06-06 14:38:23 -04001928 if ((wr->tid == mad_hdr->tid) &&
Jack Morgenstein9874e742006-06-17 20:37:34 -07001929 rcv_has_same_class(wr, wc) &&
1930 /*
1931 * Don't check GID for direct routed MADs.
1932 * These might have permissive LIDs.
1933 */
Ira Weiny83a1d222015-06-06 14:38:23 -04001934 (is_direct(mad_hdr->mgmt_class) ||
Jack Morgenstein9874e742006-06-17 20:37:34 -07001935 rcv_has_same_gid(mad_agent_priv, wr, wc)))
Roland Dreier39798692006-11-13 09:38:07 -08001936 return (wr->status == IB_WC_SUCCESS) ? wr : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 }
1938
1939 /*
1940 * It's possible to receive the response before we've
1941 * been notified that the send has completed
1942 */
Jack Morgenstein9874e742006-06-17 20:37:34 -07001943 list_for_each_entry(wr, &mad_agent_priv->send_list, agent_list) {
Ira Weinyc597eee2015-05-08 13:10:03 -04001944 if (is_rmpp_data_mad(mad_agent_priv, wr->send_buf.mad) &&
Ira Weiny83a1d222015-06-06 14:38:23 -04001945 wr->tid == mad_hdr->tid &&
Jack Morgenstein9874e742006-06-17 20:37:34 -07001946 wr->timeout &&
1947 rcv_has_same_class(wr, wc) &&
1948 /*
1949 * Don't check GID for direct routed MADs.
1950 * These might have permissive LIDs.
1951 */
Ira Weiny83a1d222015-06-06 14:38:23 -04001952 (is_direct(mad_hdr->mgmt_class) ||
Jack Morgenstein9874e742006-06-17 20:37:34 -07001953 rcv_has_same_gid(mad_agent_priv, wr, wc)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 /* Verify request has not been canceled */
Jack Morgenstein9874e742006-06-17 20:37:34 -07001955 return (wr->status == IB_WC_SUCCESS) ? wr : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 }
1957 return NULL;
1958}
1959
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001960void ib_mark_mad_done(struct ib_mad_send_wr_private *mad_send_wr)
Hal Rosenstock6a0c4352005-07-27 11:45:26 -07001961{
1962 mad_send_wr->timeout = 0;
Akinobu Mita179e0912006-06-26 00:24:41 -07001963 if (mad_send_wr->refcount == 1)
1964 list_move_tail(&mad_send_wr->agent_list,
Hal Rosenstock6a0c4352005-07-27 11:45:26 -07001965 &mad_send_wr->mad_agent_priv->done_list);
Hal Rosenstock6a0c4352005-07-27 11:45:26 -07001966}
1967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968static void ib_mad_complete_recv(struct ib_mad_agent_private *mad_agent_priv,
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07001969 struct ib_mad_recv_wc *mad_recv_wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
1971 struct ib_mad_send_wr_private *mad_send_wr;
1972 struct ib_mad_send_wc mad_send_wc;
1973 unsigned long flags;
Daniel Jurgens47a2b332017-05-19 15:48:54 +03001974 int ret;
1975
Parav Pandit89548bc2017-10-31 10:33:18 -05001976 INIT_LIST_HEAD(&mad_recv_wc->rmpp_list);
Daniel Jurgens47a2b332017-05-19 15:48:54 +03001977 ret = ib_mad_enforce_security(mad_agent_priv,
1978 mad_recv_wc->wc->pkey_index);
1979 if (ret) {
1980 ib_free_recv_mad(mad_recv_wc);
1981 deref_mad_agent(mad_agent_priv);
Parav Pandit89548bc2017-10-31 10:33:18 -05001982 return;
Daniel Jurgens47a2b332017-05-19 15:48:54 +03001983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001985 list_add(&mad_recv_wc->recv_buf.list, &mad_recv_wc->rmpp_list);
Ira Weiny1471cb62014-08-08 19:00:56 -04001986 if (ib_mad_kernel_rmpp_agent(&mad_agent_priv->agent)) {
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001987 mad_recv_wc = ib_process_rmpp_recv_wc(mad_agent_priv,
1988 mad_recv_wc);
1989 if (!mad_recv_wc) {
Sean Hefty1b52fa982006-05-12 14:57:52 -07001990 deref_mad_agent(mad_agent_priv);
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001991 return;
1992 }
1993 }
1994
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 /* Complete corresponding request */
Ira Weiny96909302015-05-08 14:27:22 -04001996 if (ib_response_mad(&mad_recv_wc->recv_buf.mad->mad_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 spin_lock_irqsave(&mad_agent_priv->lock, flags);
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001998 mad_send_wr = ib_find_send_mad(mad_agent_priv, mad_recv_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 if (!mad_send_wr) {
2000 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
Ira Weiny1471cb62014-08-08 19:00:56 -04002001 if (!ib_mad_kernel_rmpp_agent(&mad_agent_priv->agent)
2002 && ib_is_mad_class_rmpp(mad_recv_wc->recv_buf.mad->mad_hdr.mgmt_class)
2003 && (ib_get_rmpp_flags(&((struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad)->rmpp_hdr)
2004 & IB_MGMT_RMPP_FLAG_ACTIVE)) {
2005 /* user rmpp is in effect
2006 * and this is an active RMPP MAD
2007 */
Christoph Hellwigca281262016-01-04 14:15:58 +01002008 mad_agent_priv->agent.recv_handler(
2009 &mad_agent_priv->agent, NULL,
2010 mad_recv_wc);
Ira Weiny1471cb62014-08-08 19:00:56 -04002011 atomic_dec(&mad_agent_priv->refcount);
2012 } else {
2013 /* not user rmpp, revert to normal behavior and
2014 * drop the mad */
2015 ib_free_recv_mad(mad_recv_wc);
2016 deref_mad_agent(mad_agent_priv);
2017 return;
2018 }
2019 } else {
2020 ib_mark_mad_done(mad_send_wr);
2021 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2022
2023 /* Defined behavior is to complete response before request */
Christoph Hellwigca281262016-01-04 14:15:58 +01002024 mad_agent_priv->agent.recv_handler(
2025 &mad_agent_priv->agent,
2026 &mad_send_wr->send_buf,
2027 mad_recv_wc);
Ira Weiny1471cb62014-08-08 19:00:56 -04002028 atomic_dec(&mad_agent_priv->refcount);
2029
2030 mad_send_wc.status = IB_WC_SUCCESS;
2031 mad_send_wc.vendor_err = 0;
2032 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2033 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 } else {
Christoph Hellwigca281262016-01-04 14:15:58 +01002036 mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent, NULL,
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07002037 mad_recv_wc);
Sean Hefty1b52fa982006-05-12 14:57:52 -07002038 deref_mad_agent(mad_agent_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 }
Daniel Jurgens47a2b332017-05-19 15:48:54 +03002040
2041 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042}
2043
Ira Weinye11ae8a2015-06-06 14:38:24 -04002044static enum smi_action handle_ib_smi(const struct ib_mad_port_private *port_priv,
2045 const struct ib_mad_qp_info *qp_info,
2046 const struct ib_wc *wc,
2047 int port_num,
2048 struct ib_mad_private *recv,
2049 struct ib_mad_private *response)
2050{
2051 enum smi_forward_action retsmi;
Ira Weinyc9082e52015-06-06 14:38:30 -04002052 struct ib_smp *smp = (struct ib_smp *)recv->mad;
Ira Weinye11ae8a2015-06-06 14:38:24 -04002053
Ira Weinyc9082e52015-06-06 14:38:30 -04002054 if (smi_handle_dr_smp_recv(smp,
Hal Rosenstock41390322015-06-29 09:57:00 -04002055 rdma_cap_ib_switch(port_priv->device),
Ira Weinye11ae8a2015-06-06 14:38:24 -04002056 port_num,
2057 port_priv->device->phys_port_cnt) ==
2058 IB_SMI_DISCARD)
2059 return IB_SMI_DISCARD;
2060
Ira Weinyc9082e52015-06-06 14:38:30 -04002061 retsmi = smi_check_forward_dr_smp(smp);
Ira Weinye11ae8a2015-06-06 14:38:24 -04002062 if (retsmi == IB_SMI_LOCAL)
2063 return IB_SMI_HANDLE;
2064
2065 if (retsmi == IB_SMI_SEND) { /* don't forward */
Ira Weinyc9082e52015-06-06 14:38:30 -04002066 if (smi_handle_dr_smp_send(smp,
Hal Rosenstock41390322015-06-29 09:57:00 -04002067 rdma_cap_ib_switch(port_priv->device),
Ira Weinye11ae8a2015-06-06 14:38:24 -04002068 port_num) == IB_SMI_DISCARD)
2069 return IB_SMI_DISCARD;
2070
Ira Weinyc9082e52015-06-06 14:38:30 -04002071 if (smi_check_local_smp(smp, port_priv->device) == IB_SMI_DISCARD)
Ira Weinye11ae8a2015-06-06 14:38:24 -04002072 return IB_SMI_DISCARD;
Hal Rosenstock41390322015-06-29 09:57:00 -04002073 } else if (rdma_cap_ib_switch(port_priv->device)) {
Ira Weinye11ae8a2015-06-06 14:38:24 -04002074 /* forward case for switches */
Ira Weinyc9082e52015-06-06 14:38:30 -04002075 memcpy(response, recv, mad_priv_size(response));
Ira Weinye11ae8a2015-06-06 14:38:24 -04002076 response->header.recv_wc.wc = &response->header.wc;
Ira Weinyc9082e52015-06-06 14:38:30 -04002077 response->header.recv_wc.recv_buf.mad = (struct ib_mad *)response->mad;
Ira Weinye11ae8a2015-06-06 14:38:24 -04002078 response->header.recv_wc.recv_buf.grh = &response->grh;
2079
Ira Weinyc9082e52015-06-06 14:38:30 -04002080 agent_send_response((const struct ib_mad_hdr *)response->mad,
Ira Weinye11ae8a2015-06-06 14:38:24 -04002081 &response->grh, wc,
2082 port_priv->device,
Ira Weinyc9082e52015-06-06 14:38:30 -04002083 smi_get_fwd_port(smp),
2084 qp_info->qp->qp_num,
Ira Weiny8e4349d2015-06-10 16:16:48 -04002085 response->mad_size,
2086 false);
Ira Weinye11ae8a2015-06-06 14:38:24 -04002087
2088 return IB_SMI_DISCARD;
2089 }
2090 return IB_SMI_HANDLE;
2091}
2092
Ira Weinyc9082e52015-06-06 14:38:30 -04002093static bool generate_unmatched_resp(const struct ib_mad_private *recv,
Ira Weiny8e4349d2015-06-10 16:16:48 -04002094 struct ib_mad_private *response,
2095 size_t *resp_len, bool opa)
Swapna Thete0b307042012-02-25 17:47:32 -08002096{
Ira Weinyc9082e52015-06-06 14:38:30 -04002097 const struct ib_mad_hdr *recv_hdr = (const struct ib_mad_hdr *)recv->mad;
2098 struct ib_mad_hdr *resp_hdr = (struct ib_mad_hdr *)response->mad;
2099
2100 if (recv_hdr->method == IB_MGMT_METHOD_GET ||
2101 recv_hdr->method == IB_MGMT_METHOD_SET) {
2102 memcpy(response, recv, mad_priv_size(response));
Swapna Thete0b307042012-02-25 17:47:32 -08002103 response->header.recv_wc.wc = &response->header.wc;
Ira Weinyc9082e52015-06-06 14:38:30 -04002104 response->header.recv_wc.recv_buf.mad = (struct ib_mad *)response->mad;
Swapna Thete0b307042012-02-25 17:47:32 -08002105 response->header.recv_wc.recv_buf.grh = &response->grh;
Ira Weinyc9082e52015-06-06 14:38:30 -04002106 resp_hdr->method = IB_MGMT_METHOD_GET_RESP;
2107 resp_hdr->status = cpu_to_be16(IB_MGMT_MAD_STATUS_UNSUPPORTED_METHOD_ATTRIB);
2108 if (recv_hdr->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
2109 resp_hdr->status |= IB_SMP_DIRECTION;
Swapna Thete0b307042012-02-25 17:47:32 -08002110
Ira Weiny8e4349d2015-06-10 16:16:48 -04002111 if (opa && recv_hdr->base_version == OPA_MGMT_BASE_VERSION) {
2112 if (recv_hdr->mgmt_class ==
2113 IB_MGMT_CLASS_SUBN_LID_ROUTED ||
2114 recv_hdr->mgmt_class ==
2115 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
2116 *resp_len = opa_get_smp_header_size(
2117 (struct opa_smp *)recv->mad);
2118 else
2119 *resp_len = sizeof(struct ib_mad_hdr);
2120 }
2121
Swapna Thete0b307042012-02-25 17:47:32 -08002122 return true;
2123 } else {
2124 return false;
2125 }
2126}
Ira Weiny8e4349d2015-06-10 16:16:48 -04002127
2128static enum smi_action
2129handle_opa_smi(struct ib_mad_port_private *port_priv,
2130 struct ib_mad_qp_info *qp_info,
2131 struct ib_wc *wc,
2132 int port_num,
2133 struct ib_mad_private *recv,
2134 struct ib_mad_private *response)
2135{
2136 enum smi_forward_action retsmi;
2137 struct opa_smp *smp = (struct opa_smp *)recv->mad;
2138
2139 if (opa_smi_handle_dr_smp_recv(smp,
Hal Rosenstock41390322015-06-29 09:57:00 -04002140 rdma_cap_ib_switch(port_priv->device),
Ira Weiny8e4349d2015-06-10 16:16:48 -04002141 port_num,
2142 port_priv->device->phys_port_cnt) ==
2143 IB_SMI_DISCARD)
2144 return IB_SMI_DISCARD;
2145
2146 retsmi = opa_smi_check_forward_dr_smp(smp);
2147 if (retsmi == IB_SMI_LOCAL)
2148 return IB_SMI_HANDLE;
2149
2150 if (retsmi == IB_SMI_SEND) { /* don't forward */
2151 if (opa_smi_handle_dr_smp_send(smp,
Hal Rosenstock41390322015-06-29 09:57:00 -04002152 rdma_cap_ib_switch(port_priv->device),
Ira Weiny8e4349d2015-06-10 16:16:48 -04002153 port_num) == IB_SMI_DISCARD)
2154 return IB_SMI_DISCARD;
2155
2156 if (opa_smi_check_local_smp(smp, port_priv->device) ==
2157 IB_SMI_DISCARD)
2158 return IB_SMI_DISCARD;
2159
Hal Rosenstock41390322015-06-29 09:57:00 -04002160 } else if (rdma_cap_ib_switch(port_priv->device)) {
Ira Weiny8e4349d2015-06-10 16:16:48 -04002161 /* forward case for switches */
2162 memcpy(response, recv, mad_priv_size(response));
2163 response->header.recv_wc.wc = &response->header.wc;
2164 response->header.recv_wc.recv_buf.opa_mad =
2165 (struct opa_mad *)response->mad;
2166 response->header.recv_wc.recv_buf.grh = &response->grh;
2167
2168 agent_send_response((const struct ib_mad_hdr *)response->mad,
2169 &response->grh, wc,
2170 port_priv->device,
2171 opa_smi_get_fwd_port(smp),
2172 qp_info->qp->qp_num,
2173 recv->header.wc.byte_len,
2174 true);
2175
2176 return IB_SMI_DISCARD;
2177 }
2178
2179 return IB_SMI_HANDLE;
2180}
2181
2182static enum smi_action
2183handle_smi(struct ib_mad_port_private *port_priv,
2184 struct ib_mad_qp_info *qp_info,
2185 struct ib_wc *wc,
2186 int port_num,
2187 struct ib_mad_private *recv,
2188 struct ib_mad_private *response,
2189 bool opa)
2190{
2191 struct ib_mad_hdr *mad_hdr = (struct ib_mad_hdr *)recv->mad;
2192
2193 if (opa && mad_hdr->base_version == OPA_MGMT_BASE_VERSION &&
Hal Rosenstock9fa240b2016-10-18 13:20:29 -04002194 mad_hdr->class_version == OPA_SM_CLASS_VERSION)
Ira Weiny8e4349d2015-06-10 16:16:48 -04002195 return handle_opa_smi(port_priv, qp_info, wc, port_num, recv,
2196 response);
2197
2198 return handle_ib_smi(port_priv, qp_info, wc, port_num, recv, response);
2199}
2200
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08002201static void ib_mad_recv_done(struct ib_cq *cq, struct ib_wc *wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08002203 struct ib_mad_port_private *port_priv = cq->cq_context;
2204 struct ib_mad_list_head *mad_list =
2205 container_of(wc->wr_cqe, struct ib_mad_list_head, cqe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 struct ib_mad_qp_info *qp_info;
2207 struct ib_mad_private_header *mad_priv_hdr;
Hal Rosenstock445d6802007-08-03 10:45:17 -07002208 struct ib_mad_private *recv, *response = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 struct ib_mad_agent_private *mad_agent;
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04002210 int port_num;
Jack Morgensteina9e74322012-04-24 16:08:57 -07002211 int ret = IB_MAD_RESULT_SUCCESS;
Ira Weiny4cd7c942015-06-06 14:38:31 -04002212 size_t mad_size;
2213 u16 resp_mad_pkey_index = 0;
Ira Weiny8e4349d2015-06-10 16:16:48 -04002214 bool opa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08002216 if (list_empty_careful(&port_priv->port_list))
2217 return;
2218
2219 if (wc->status != IB_WC_SUCCESS) {
2220 /*
2221 * Receive errors indicate that the QP has entered the error
2222 * state - error handling/shutdown code will cleanup
2223 */
2224 return;
2225 }
2226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 qp_info = mad_list->mad_queue->qp_info;
2228 dequeue_mad(mad_list);
2229
Ira Weiny8e4349d2015-06-10 16:16:48 -04002230 opa = rdma_cap_opa_mad(qp_info->port_priv->device,
2231 qp_info->port_priv->port_num);
2232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 mad_priv_hdr = container_of(mad_list, struct ib_mad_private_header,
2234 mad_list);
2235 recv = container_of(mad_priv_hdr, struct ib_mad_private, header);
Ralph Campbell15271062006-12-12 14:28:30 -08002236 ib_dma_unmap_single(port_priv->device,
2237 recv->header.mapping,
Ira Weinyc9082e52015-06-06 14:38:30 -04002238 mad_priv_dma_size(recv),
Ralph Campbell15271062006-12-12 14:28:30 -08002239 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
2241 /* Setup MAD receive work completion from "normal" work completion */
Sean Hefty24239af2005-04-16 15:26:08 -07002242 recv->header.wc = *wc;
2243 recv->header.recv_wc.wc = &recv->header.wc;
Ira Weiny8e4349d2015-06-10 16:16:48 -04002244
2245 if (opa && ((struct ib_mad_hdr *)(recv->mad))->base_version == OPA_MGMT_BASE_VERSION) {
2246 recv->header.recv_wc.mad_len = wc->byte_len - sizeof(struct ib_grh);
2247 recv->header.recv_wc.mad_seg_size = sizeof(struct opa_mad);
2248 } else {
2249 recv->header.recv_wc.mad_len = sizeof(struct ib_mad);
2250 recv->header.recv_wc.mad_seg_size = sizeof(struct ib_mad);
2251 }
2252
Ira Weinyc9082e52015-06-06 14:38:30 -04002253 recv->header.recv_wc.recv_buf.mad = (struct ib_mad *)recv->mad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 recv->header.recv_wc.recv_buf.grh = &recv->grh;
2255
2256 if (atomic_read(&qp_info->snoop_count))
2257 snoop_recv(qp_info, &recv->header.recv_wc, IB_MAD_SNOOP_RECVS);
2258
2259 /* Validate MAD */
Ira Weiny8e4349d2015-06-10 16:16:48 -04002260 if (!validate_mad((const struct ib_mad_hdr *)recv->mad, qp_info, opa))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 goto out;
2262
Ira Weiny4cd7c942015-06-06 14:38:31 -04002263 mad_size = recv->mad_size;
2264 response = alloc_mad_private(mad_size, GFP_KERNEL);
Leon Romanovsky27162432016-11-03 16:44:09 +02002265 if (!response)
Hal Rosenstock445d6802007-08-03 10:45:17 -07002266 goto out;
Hal Rosenstock445d6802007-08-03 10:45:17 -07002267
Hal Rosenstock41390322015-06-29 09:57:00 -04002268 if (rdma_cap_ib_switch(port_priv->device))
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04002269 port_num = wc->port_num;
2270 else
2271 port_num = port_priv->port_num;
2272
Ira Weinyc9082e52015-06-06 14:38:30 -04002273 if (((struct ib_mad_hdr *)recv->mad)->mgmt_class ==
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
Ira Weiny8e4349d2015-06-10 16:16:48 -04002275 if (handle_smi(port_priv, qp_info, wc, port_num, recv,
2276 response, opa)
Ira Weinye11ae8a2015-06-06 14:38:24 -04002277 == IB_SMI_DISCARD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 }
2280
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 /* Give driver "right of first refusal" on incoming MAD */
2282 if (port_priv->device->process_mad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 ret = port_priv->device->process_mad(port_priv->device, 0,
2284 port_priv->port_num,
2285 wc, &recv->grh,
Ira Weiny4cd7c942015-06-06 14:38:31 -04002286 (const struct ib_mad_hdr *)recv->mad,
2287 recv->mad_size,
2288 (struct ib_mad_hdr *)response->mad,
2289 &mad_size, &resp_mad_pkey_index);
Ira Weiny8e4349d2015-06-10 16:16:48 -04002290
2291 if (opa)
2292 wc->pkey_index = resp_mad_pkey_index;
2293
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 if (ret & IB_MAD_RESULT_SUCCESS) {
2295 if (ret & IB_MAD_RESULT_CONSUMED)
2296 goto out;
2297 if (ret & IB_MAD_RESULT_REPLY) {
Ira Weinyc9082e52015-06-06 14:38:30 -04002298 agent_send_response((const struct ib_mad_hdr *)response->mad,
Sean Hefty34816ad2005-10-25 10:51:39 -07002299 &recv->grh, wc,
2300 port_priv->device,
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04002301 port_num,
Ira Weinyc9082e52015-06-06 14:38:30 -04002302 qp_info->qp->qp_num,
Ira Weiny8e4349d2015-06-10 16:16:48 -04002303 mad_size, opa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 goto out;
2305 }
2306 }
2307 }
2308
Ira Weinyc9082e52015-06-06 14:38:30 -04002309 mad_agent = find_mad_agent(port_priv, (const struct ib_mad_hdr *)recv->mad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 if (mad_agent) {
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07002311 ib_mad_complete_recv(mad_agent, &recv->header.recv_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 /*
2313 * recv is freed up in error cases in ib_mad_complete_recv
2314 * or via recv_handler in ib_mad_complete_recv()
2315 */
2316 recv = NULL;
Jack Morgensteina9e74322012-04-24 16:08:57 -07002317 } else if ((ret & IB_MAD_RESULT_SUCCESS) &&
Ira Weiny8e4349d2015-06-10 16:16:48 -04002318 generate_unmatched_resp(recv, response, &mad_size, opa)) {
Ira Weinyc9082e52015-06-06 14:38:30 -04002319 agent_send_response((const struct ib_mad_hdr *)response->mad, &recv->grh, wc,
2320 port_priv->device, port_num,
Ira Weiny8e4349d2015-06-10 16:16:48 -04002321 qp_info->qp->qp_num, mad_size, opa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 }
2323
2324out:
2325 /* Post another receive request for this QP */
2326 if (response) {
2327 ib_mad_post_receive_mads(qp_info, response);
Ira Weinyc9082e52015-06-06 14:38:30 -04002328 kfree(recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 } else
2330 ib_mad_post_receive_mads(qp_info, recv);
2331}
2332
2333static void adjust_timeout(struct ib_mad_agent_private *mad_agent_priv)
2334{
2335 struct ib_mad_send_wr_private *mad_send_wr;
2336 unsigned long delay;
2337
2338 if (list_empty(&mad_agent_priv->wait_list)) {
Tejun Heo136b5722012-08-21 13:18:24 -07002339 cancel_delayed_work(&mad_agent_priv->timed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 } else {
2341 mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
2342 struct ib_mad_send_wr_private,
2343 agent_list);
2344
2345 if (time_after(mad_agent_priv->timeout,
2346 mad_send_wr->timeout)) {
2347 mad_agent_priv->timeout = mad_send_wr->timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 delay = mad_send_wr->timeout - jiffies;
2349 if ((long)delay <= 0)
2350 delay = 1;
Tejun Heoe7c2f962012-08-21 13:18:24 -07002351 mod_delayed_work(mad_agent_priv->qp_info->port_priv->wq,
2352 &mad_agent_priv->timed_work, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 }
2354 }
2355}
2356
Hal Rosenstockd760ce82005-07-27 11:45:25 -07002357static void wait_for_response(struct ib_mad_send_wr_private *mad_send_wr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358{
Hal Rosenstockd760ce82005-07-27 11:45:25 -07002359 struct ib_mad_agent_private *mad_agent_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 struct ib_mad_send_wr_private *temp_mad_send_wr;
2361 struct list_head *list_item;
2362 unsigned long delay;
2363
Hal Rosenstockd760ce82005-07-27 11:45:25 -07002364 mad_agent_priv = mad_send_wr->mad_agent_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 list_del(&mad_send_wr->agent_list);
2366
2367 delay = mad_send_wr->timeout;
2368 mad_send_wr->timeout += jiffies;
2369
Hal Rosenstock29bb33d2005-07-27 11:45:32 -07002370 if (delay) {
2371 list_for_each_prev(list_item, &mad_agent_priv->wait_list) {
2372 temp_mad_send_wr = list_entry(list_item,
2373 struct ib_mad_send_wr_private,
2374 agent_list);
2375 if (time_after(mad_send_wr->timeout,
2376 temp_mad_send_wr->timeout))
2377 break;
2378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 }
Hal Rosenstock29bb33d2005-07-27 11:45:32 -07002380 else
2381 list_item = &mad_agent_priv->wait_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 list_add(&mad_send_wr->agent_list, list_item);
2383
2384 /* Reschedule a work item if we have a shorter timeout */
Tejun Heoe7c2f962012-08-21 13:18:24 -07002385 if (mad_agent_priv->wait_list.next == &mad_send_wr->agent_list)
2386 mod_delayed_work(mad_agent_priv->qp_info->port_priv->wq,
2387 &mad_agent_priv->timed_work, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388}
2389
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002390void ib_reset_mad_timeout(struct ib_mad_send_wr_private *mad_send_wr,
2391 int timeout_ms)
2392{
2393 mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
2394 wait_for_response(mad_send_wr);
2395}
2396
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397/*
2398 * Process a send work completion
2399 */
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002400void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr,
2401 struct ib_mad_send_wc *mad_send_wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402{
2403 struct ib_mad_agent_private *mad_agent_priv;
2404 unsigned long flags;
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002405 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
Hal Rosenstockd760ce82005-07-27 11:45:25 -07002407 mad_agent_priv = mad_send_wr->mad_agent_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 spin_lock_irqsave(&mad_agent_priv->lock, flags);
Ira Weiny1471cb62014-08-08 19:00:56 -04002409 if (ib_mad_kernel_rmpp_agent(&mad_agent_priv->agent)) {
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002410 ret = ib_process_rmpp_send_wc(mad_send_wr, mad_send_wc);
2411 if (ret == IB_RMPP_RESULT_CONSUMED)
2412 goto done;
2413 } else
2414 ret = IB_RMPP_RESULT_UNHANDLED;
2415
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 if (mad_send_wc->status != IB_WC_SUCCESS &&
2417 mad_send_wr->status == IB_WC_SUCCESS) {
2418 mad_send_wr->status = mad_send_wc->status;
2419 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2420 }
2421
2422 if (--mad_send_wr->refcount > 0) {
2423 if (mad_send_wr->refcount == 1 && mad_send_wr->timeout &&
2424 mad_send_wr->status == IB_WC_SUCCESS) {
Hal Rosenstockd760ce82005-07-27 11:45:25 -07002425 wait_for_response(mad_send_wr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 }
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002427 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 }
2429
2430 /* Remove send from MAD agent and notify client of completion */
2431 list_del(&mad_send_wr->agent_list);
2432 adjust_timeout(mad_agent_priv);
2433 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2434
2435 if (mad_send_wr->status != IB_WC_SUCCESS )
2436 mad_send_wc->status = mad_send_wr->status;
Sean Hefty34816ad2005-10-25 10:51:39 -07002437 if (ret == IB_RMPP_RESULT_INTERNAL)
2438 ib_rmpp_send_handler(mad_send_wc);
2439 else
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002440 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2441 mad_send_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
2443 /* Release reference on agent taken when sending */
Sean Hefty1b52fa982006-05-12 14:57:52 -07002444 deref_mad_agent(mad_agent_priv);
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002445 return;
2446done:
2447 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448}
2449
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08002450static void ib_mad_send_done(struct ib_cq *cq, struct ib_wc *wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451{
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08002452 struct ib_mad_port_private *port_priv = cq->cq_context;
2453 struct ib_mad_list_head *mad_list =
2454 container_of(wc->wr_cqe, struct ib_mad_list_head, cqe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 struct ib_mad_send_wr_private *mad_send_wr, *queued_send_wr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 struct ib_mad_qp_info *qp_info;
2457 struct ib_mad_queue *send_queue;
2458 struct ib_send_wr *bad_send_wr;
Sean Hefty34816ad2005-10-25 10:51:39 -07002459 struct ib_mad_send_wc mad_send_wc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 unsigned long flags;
2461 int ret;
2462
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08002463 if (list_empty_careful(&port_priv->port_list))
2464 return;
2465
2466 if (wc->status != IB_WC_SUCCESS) {
2467 if (!ib_mad_send_error(port_priv, wc))
2468 return;
2469 }
2470
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
2472 mad_list);
2473 send_queue = mad_list->mad_queue;
2474 qp_info = send_queue->qp_info;
2475
2476retry:
Ralph Campbell15271062006-12-12 14:28:30 -08002477 ib_dma_unmap_single(mad_send_wr->send_buf.mad_agent->device,
2478 mad_send_wr->header_mapping,
2479 mad_send_wr->sg_list[0].length, DMA_TO_DEVICE);
2480 ib_dma_unmap_single(mad_send_wr->send_buf.mad_agent->device,
2481 mad_send_wr->payload_mapping,
2482 mad_send_wr->sg_list[1].length, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 queued_send_wr = NULL;
2484 spin_lock_irqsave(&send_queue->lock, flags);
2485 list_del(&mad_list->list);
2486
2487 /* Move queued send to the send queue */
2488 if (send_queue->count-- > send_queue->max_active) {
2489 mad_list = container_of(qp_info->overflow_list.next,
2490 struct ib_mad_list_head, list);
2491 queued_send_wr = container_of(mad_list,
2492 struct ib_mad_send_wr_private,
2493 mad_list);
Akinobu Mita179e0912006-06-26 00:24:41 -07002494 list_move_tail(&mad_list->list, &send_queue->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 }
2496 spin_unlock_irqrestore(&send_queue->lock, flags);
2497
Sean Hefty34816ad2005-10-25 10:51:39 -07002498 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2499 mad_send_wc.status = wc->status;
2500 mad_send_wc.vendor_err = wc->vendor_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 if (atomic_read(&qp_info->snoop_count))
Sean Hefty34816ad2005-10-25 10:51:39 -07002502 snoop_send(qp_info, &mad_send_wr->send_buf, &mad_send_wc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 IB_MAD_SNOOP_SEND_COMPLETIONS);
Sean Hefty34816ad2005-10-25 10:51:39 -07002504 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505
2506 if (queued_send_wr) {
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002507 ret = ib_post_send(qp_info->qp, &queued_send_wr->send_wr.wr,
Sean Hefty34816ad2005-10-25 10:51:39 -07002508 &bad_send_wr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 if (ret) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04002510 dev_err(&port_priv->device->dev,
2511 "ib_post_send failed: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 mad_send_wr = queued_send_wr;
2513 wc->status = IB_WC_LOC_QP_OP_ERR;
2514 goto retry;
2515 }
2516 }
2517}
2518
2519static void mark_sends_for_retry(struct ib_mad_qp_info *qp_info)
2520{
2521 struct ib_mad_send_wr_private *mad_send_wr;
2522 struct ib_mad_list_head *mad_list;
2523 unsigned long flags;
2524
2525 spin_lock_irqsave(&qp_info->send_queue.lock, flags);
2526 list_for_each_entry(mad_list, &qp_info->send_queue.list, list) {
2527 mad_send_wr = container_of(mad_list,
2528 struct ib_mad_send_wr_private,
2529 mad_list);
2530 mad_send_wr->retry = 1;
2531 }
2532 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
2533}
2534
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08002535static bool ib_mad_send_error(struct ib_mad_port_private *port_priv,
2536 struct ib_wc *wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537{
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08002538 struct ib_mad_list_head *mad_list =
2539 container_of(wc->wr_cqe, struct ib_mad_list_head, cqe);
2540 struct ib_mad_qp_info *qp_info = mad_list->mad_queue->qp_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 struct ib_mad_send_wr_private *mad_send_wr;
2542 int ret;
2543
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 /*
2545 * Send errors will transition the QP to SQE - move
2546 * QP to RTS and repost flushed work requests
2547 */
2548 mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
2549 mad_list);
2550 if (wc->status == IB_WC_WR_FLUSH_ERR) {
2551 if (mad_send_wr->retry) {
2552 /* Repost send */
2553 struct ib_send_wr *bad_send_wr;
2554
2555 mad_send_wr->retry = 0;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002556 ret = ib_post_send(qp_info->qp, &mad_send_wr->send_wr.wr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 &bad_send_wr);
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08002558 if (!ret)
2559 return false;
2560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 } else {
2562 struct ib_qp_attr *attr;
2563
2564 /* Transition QP to RTS and fail offending send */
2565 attr = kmalloc(sizeof *attr, GFP_KERNEL);
2566 if (attr) {
2567 attr->qp_state = IB_QPS_RTS;
2568 attr->cur_qp_state = IB_QPS_SQE;
2569 ret = ib_modify_qp(qp_info->qp, attr,
2570 IB_QP_STATE | IB_QP_CUR_STATE);
2571 kfree(attr);
2572 if (ret)
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04002573 dev_err(&port_priv->device->dev,
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08002574 "%s - ib_modify_qp to RTS: %d\n",
2575 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 else
2577 mark_sends_for_retry(qp_info);
2578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08002581 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582}
2583
2584static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv)
2585{
2586 unsigned long flags;
2587 struct ib_mad_send_wr_private *mad_send_wr, *temp_mad_send_wr;
2588 struct ib_mad_send_wc mad_send_wc;
2589 struct list_head cancel_list;
2590
2591 INIT_LIST_HEAD(&cancel_list);
2592
2593 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2594 list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
2595 &mad_agent_priv->send_list, agent_list) {
2596 if (mad_send_wr->status == IB_WC_SUCCESS) {
Roland Dreier3cd96562006-09-22 15:22:46 -07002597 mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2599 }
2600 }
2601
2602 /* Empty wait list to prevent receives from finding a request */
2603 list_splice_init(&mad_agent_priv->wait_list, &cancel_list);
2604 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2605
2606 /* Report all cancelled requests */
2607 mad_send_wc.status = IB_WC_WR_FLUSH_ERR;
2608 mad_send_wc.vendor_err = 0;
2609
2610 list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
2611 &cancel_list, agent_list) {
Sean Hefty34816ad2005-10-25 10:51:39 -07002612 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2613 list_del(&mad_send_wr->agent_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2615 &mad_send_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 atomic_dec(&mad_agent_priv->refcount);
2617 }
2618}
2619
2620static struct ib_mad_send_wr_private*
Sean Hefty34816ad2005-10-25 10:51:39 -07002621find_send_wr(struct ib_mad_agent_private *mad_agent_priv,
2622 struct ib_mad_send_buf *send_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623{
2624 struct ib_mad_send_wr_private *mad_send_wr;
2625
2626 list_for_each_entry(mad_send_wr, &mad_agent_priv->wait_list,
2627 agent_list) {
Sean Hefty34816ad2005-10-25 10:51:39 -07002628 if (&mad_send_wr->send_buf == send_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 return mad_send_wr;
2630 }
2631
2632 list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list,
2633 agent_list) {
Ira Weinyc597eee2015-05-08 13:10:03 -04002634 if (is_rmpp_data_mad(mad_agent_priv,
2635 mad_send_wr->send_buf.mad) &&
Sean Hefty34816ad2005-10-25 10:51:39 -07002636 &mad_send_wr->send_buf == send_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 return mad_send_wr;
2638 }
2639 return NULL;
2640}
2641
Sean Hefty34816ad2005-10-25 10:51:39 -07002642int ib_modify_mad(struct ib_mad_agent *mad_agent,
2643 struct ib_mad_send_buf *send_buf, u32 timeout_ms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644{
2645 struct ib_mad_agent_private *mad_agent_priv;
2646 struct ib_mad_send_wr_private *mad_send_wr;
2647 unsigned long flags;
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07002648 int active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
2650 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
2651 agent);
2652 spin_lock_irqsave(&mad_agent_priv->lock, flags);
Sean Hefty34816ad2005-10-25 10:51:39 -07002653 mad_send_wr = find_send_wr(mad_agent_priv, send_buf);
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002654 if (!mad_send_wr || mad_send_wr->status != IB_WC_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002656 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 }
2658
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07002659 active = (!mad_send_wr->timeout || mad_send_wr->refcount > 1);
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002660 if (!timeout_ms) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002662 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 }
2664
Sean Hefty34816ad2005-10-25 10:51:39 -07002665 mad_send_wr->send_buf.timeout_ms = timeout_ms;
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07002666 if (active)
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002667 mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
2668 else
2669 ib_reset_mad_timeout(mad_send_wr, timeout_ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002671 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2672 return 0;
2673}
2674EXPORT_SYMBOL(ib_modify_mad);
2675
Sean Hefty34816ad2005-10-25 10:51:39 -07002676void ib_cancel_mad(struct ib_mad_agent *mad_agent,
2677 struct ib_mad_send_buf *send_buf)
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002678{
Sean Hefty34816ad2005-10-25 10:51:39 -07002679 ib_modify_mad(mad_agent, send_buf, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680}
2681EXPORT_SYMBOL(ib_cancel_mad);
2682
David Howellsc4028952006-11-22 14:57:56 +00002683static void local_completions(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684{
2685 struct ib_mad_agent_private *mad_agent_priv;
2686 struct ib_mad_local_private *local;
2687 struct ib_mad_agent_private *recv_mad_agent;
2688 unsigned long flags;
Ralph Campbell1d9bc6d62009-02-27 10:34:30 -08002689 int free_mad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 struct ib_wc wc;
2691 struct ib_mad_send_wc mad_send_wc;
Ira Weiny8e4349d2015-06-10 16:16:48 -04002692 bool opa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693
David Howellsc4028952006-11-22 14:57:56 +00002694 mad_agent_priv =
2695 container_of(work, struct ib_mad_agent_private, local_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
Ira Weiny8e4349d2015-06-10 16:16:48 -04002697 opa = rdma_cap_opa_mad(mad_agent_priv->qp_info->port_priv->device,
2698 mad_agent_priv->qp_info->port_priv->port_num);
2699
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2701 while (!list_empty(&mad_agent_priv->local_list)) {
2702 local = list_entry(mad_agent_priv->local_list.next,
2703 struct ib_mad_local_private,
2704 completion_list);
Michael S. Tsirkin37289ef2006-03-30 15:52:54 +02002705 list_del(&local->completion_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
Ralph Campbell1d9bc6d62009-02-27 10:34:30 -08002707 free_mad = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 if (local->mad_priv) {
Ira Weiny8e4349d2015-06-10 16:16:48 -04002709 u8 base_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 recv_mad_agent = local->recv_mad_agent;
2711 if (!recv_mad_agent) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04002712 dev_err(&mad_agent_priv->agent.device->dev,
2713 "No receive MAD agent for local completion\n");
Ralph Campbell1d9bc6d62009-02-27 10:34:30 -08002714 free_mad = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 goto local_send_completion;
2716 }
2717
2718 /*
2719 * Defined behavior is to complete response
2720 * before request
2721 */
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +02002722 build_smp_wc(recv_mad_agent->agent.qp,
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08002723 local->mad_send_wr->send_wr.wr.wr_cqe,
Sean Hefty97f52eb2005-08-13 21:05:57 -07002724 be16_to_cpu(IB_LID_PERMISSIVE),
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002725 local->mad_send_wr->send_wr.pkey_index,
Ira Weiny8e4349d2015-06-10 16:16:48 -04002726 recv_mad_agent->agent.port_num, &wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
2728 local->mad_priv->header.recv_wc.wc = &wc;
Ira Weiny8e4349d2015-06-10 16:16:48 -04002729
2730 base_version = ((struct ib_mad_hdr *)(local->mad_priv->mad))->base_version;
2731 if (opa && base_version == OPA_MGMT_BASE_VERSION) {
2732 local->mad_priv->header.recv_wc.mad_len = local->return_wc_byte_len;
2733 local->mad_priv->header.recv_wc.mad_seg_size = sizeof(struct opa_mad);
2734 } else {
2735 local->mad_priv->header.recv_wc.mad_len = sizeof(struct ib_mad);
2736 local->mad_priv->header.recv_wc.mad_seg_size = sizeof(struct ib_mad);
2737 }
2738
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002739 INIT_LIST_HEAD(&local->mad_priv->header.recv_wc.rmpp_list);
2740 list_add(&local->mad_priv->header.recv_wc.recv_buf.list,
2741 &local->mad_priv->header.recv_wc.rmpp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 local->mad_priv->header.recv_wc.recv_buf.grh = NULL;
2743 local->mad_priv->header.recv_wc.recv_buf.mad =
Ira Weinyc9082e52015-06-06 14:38:30 -04002744 (struct ib_mad *)local->mad_priv->mad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 if (atomic_read(&recv_mad_agent->qp_info->snoop_count))
2746 snoop_recv(recv_mad_agent->qp_info,
2747 &local->mad_priv->header.recv_wc,
2748 IB_MAD_SNOOP_RECVS);
2749 recv_mad_agent->agent.recv_handler(
2750 &recv_mad_agent->agent,
Christoph Hellwigca281262016-01-04 14:15:58 +01002751 &local->mad_send_wr->send_buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 &local->mad_priv->header.recv_wc);
2753 spin_lock_irqsave(&recv_mad_agent->lock, flags);
2754 atomic_dec(&recv_mad_agent->refcount);
2755 spin_unlock_irqrestore(&recv_mad_agent->lock, flags);
2756 }
2757
2758local_send_completion:
2759 /* Complete send */
2760 mad_send_wc.status = IB_WC_SUCCESS;
2761 mad_send_wc.vendor_err = 0;
Sean Hefty34816ad2005-10-25 10:51:39 -07002762 mad_send_wc.send_buf = &local->mad_send_wr->send_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 if (atomic_read(&mad_agent_priv->qp_info->snoop_count))
Sean Hefty34816ad2005-10-25 10:51:39 -07002764 snoop_send(mad_agent_priv->qp_info,
2765 &local->mad_send_wr->send_buf,
2766 &mad_send_wc, IB_MAD_SNOOP_SEND_COMPLETIONS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2768 &mad_send_wc);
2769
2770 spin_lock_irqsave(&mad_agent_priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 atomic_dec(&mad_agent_priv->refcount);
Ralph Campbell1d9bc6d62009-02-27 10:34:30 -08002772 if (free_mad)
Ira Weinyc9082e52015-06-06 14:38:30 -04002773 kfree(local->mad_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 kfree(local);
2775 }
2776 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2777}
2778
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002779static int retry_send(struct ib_mad_send_wr_private *mad_send_wr)
2780{
2781 int ret;
2782
Sean Hefty4fc8cd42007-11-27 00:11:04 -08002783 if (!mad_send_wr->retries_left)
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002784 return -ETIMEDOUT;
2785
Sean Hefty4fc8cd42007-11-27 00:11:04 -08002786 mad_send_wr->retries_left--;
2787 mad_send_wr->send_buf.retries++;
2788
Sean Hefty34816ad2005-10-25 10:51:39 -07002789 mad_send_wr->timeout = msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms);
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002790
Ira Weiny1471cb62014-08-08 19:00:56 -04002791 if (ib_mad_kernel_rmpp_agent(&mad_send_wr->mad_agent_priv->agent)) {
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002792 ret = ib_retry_rmpp(mad_send_wr);
2793 switch (ret) {
2794 case IB_RMPP_RESULT_UNHANDLED:
2795 ret = ib_send_mad(mad_send_wr);
2796 break;
2797 case IB_RMPP_RESULT_CONSUMED:
2798 ret = 0;
2799 break;
2800 default:
2801 ret = -ECOMM;
2802 break;
2803 }
2804 } else
2805 ret = ib_send_mad(mad_send_wr);
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002806
2807 if (!ret) {
2808 mad_send_wr->refcount++;
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002809 list_add_tail(&mad_send_wr->agent_list,
2810 &mad_send_wr->mad_agent_priv->send_list);
2811 }
2812 return ret;
2813}
2814
David Howellsc4028952006-11-22 14:57:56 +00002815static void timeout_sends(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816{
2817 struct ib_mad_agent_private *mad_agent_priv;
2818 struct ib_mad_send_wr_private *mad_send_wr;
2819 struct ib_mad_send_wc mad_send_wc;
2820 unsigned long flags, delay;
2821
David Howellsc4028952006-11-22 14:57:56 +00002822 mad_agent_priv = container_of(work, struct ib_mad_agent_private,
2823 timed_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 mad_send_wc.vendor_err = 0;
2825
2826 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2827 while (!list_empty(&mad_agent_priv->wait_list)) {
2828 mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
2829 struct ib_mad_send_wr_private,
2830 agent_list);
2831
2832 if (time_after(mad_send_wr->timeout, jiffies)) {
2833 delay = mad_send_wr->timeout - jiffies;
2834 if ((long)delay <= 0)
2835 delay = 1;
2836 queue_delayed_work(mad_agent_priv->qp_info->
2837 port_priv->wq,
2838 &mad_agent_priv->timed_work, delay);
2839 break;
2840 }
2841
Hal Rosenstockdbf92272005-07-27 11:45:30 -07002842 list_del(&mad_send_wr->agent_list);
Hal Rosenstock29bb33d2005-07-27 11:45:32 -07002843 if (mad_send_wr->status == IB_WC_SUCCESS &&
2844 !retry_send(mad_send_wr))
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002845 continue;
2846
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2848
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002849 if (mad_send_wr->status == IB_WC_SUCCESS)
2850 mad_send_wc.status = IB_WC_RESP_TIMEOUT_ERR;
2851 else
2852 mad_send_wc.status = mad_send_wr->status;
Sean Hefty34816ad2005-10-25 10:51:39 -07002853 mad_send_wc.send_buf = &mad_send_wr->send_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2855 &mad_send_wc);
2856
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 atomic_dec(&mad_agent_priv->refcount);
2858 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2859 }
2860 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2861}
2862
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863/*
2864 * Allocate receive MADs and post receive WRs for them
2865 */
2866static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
2867 struct ib_mad_private *mad)
2868{
2869 unsigned long flags;
2870 int post, ret;
2871 struct ib_mad_private *mad_priv;
2872 struct ib_sge sg_list;
2873 struct ib_recv_wr recv_wr, *bad_recv_wr;
2874 struct ib_mad_queue *recv_queue = &qp_info->recv_queue;
2875
2876 /* Initialize common scatter list fields */
Jason Gunthorpe4be90bc2015-07-30 17:22:16 -06002877 sg_list.lkey = qp_info->port_priv->pd->local_dma_lkey;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878
2879 /* Initialize common receive WR fields */
2880 recv_wr.next = NULL;
2881 recv_wr.sg_list = &sg_list;
2882 recv_wr.num_sge = 1;
2883
2884 do {
2885 /* Allocate and map receive buffer */
2886 if (mad) {
2887 mad_priv = mad;
2888 mad = NULL;
2889 } else {
Ira Weinyc9082e52015-06-06 14:38:30 -04002890 mad_priv = alloc_mad_private(port_mad_size(qp_info->port_priv),
2891 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 if (!mad_priv) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 ret = -ENOMEM;
2894 break;
2895 }
2896 }
Ira Weinyc9082e52015-06-06 14:38:30 -04002897 sg_list.length = mad_priv_dma_size(mad_priv);
Ralph Campbell15271062006-12-12 14:28:30 -08002898 sg_list.addr = ib_dma_map_single(qp_info->port_priv->device,
2899 &mad_priv->grh,
Ira Weinyc9082e52015-06-06 14:38:30 -04002900 mad_priv_dma_size(mad_priv),
Ralph Campbell15271062006-12-12 14:28:30 -08002901 DMA_FROM_DEVICE);
Yan Burman2c34e682014-03-11 14:41:47 +02002902 if (unlikely(ib_dma_mapping_error(qp_info->port_priv->device,
2903 sg_list.addr))) {
2904 ret = -ENOMEM;
2905 break;
2906 }
Ralph Campbell15271062006-12-12 14:28:30 -08002907 mad_priv->header.mapping = sg_list.addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 mad_priv->header.mad_list.mad_queue = recv_queue;
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08002909 mad_priv->header.mad_list.cqe.done = ib_mad_recv_done;
2910 recv_wr.wr_cqe = &mad_priv->header.mad_list.cqe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
2912 /* Post receive WR */
2913 spin_lock_irqsave(&recv_queue->lock, flags);
2914 post = (++recv_queue->count < recv_queue->max_active);
2915 list_add_tail(&mad_priv->header.mad_list.list, &recv_queue->list);
2916 spin_unlock_irqrestore(&recv_queue->lock, flags);
2917 ret = ib_post_recv(qp_info->qp, &recv_wr, &bad_recv_wr);
2918 if (ret) {
2919 spin_lock_irqsave(&recv_queue->lock, flags);
2920 list_del(&mad_priv->header.mad_list.list);
2921 recv_queue->count--;
2922 spin_unlock_irqrestore(&recv_queue->lock, flags);
Ralph Campbell15271062006-12-12 14:28:30 -08002923 ib_dma_unmap_single(qp_info->port_priv->device,
2924 mad_priv->header.mapping,
Ira Weinyc9082e52015-06-06 14:38:30 -04002925 mad_priv_dma_size(mad_priv),
Ralph Campbell15271062006-12-12 14:28:30 -08002926 DMA_FROM_DEVICE);
Ira Weinyc9082e52015-06-06 14:38:30 -04002927 kfree(mad_priv);
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04002928 dev_err(&qp_info->port_priv->device->dev,
2929 "ib_post_recv failed: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 break;
2931 }
2932 } while (post);
2933
2934 return ret;
2935}
2936
2937/*
2938 * Return all the posted receive MADs
2939 */
2940static void cleanup_recv_queue(struct ib_mad_qp_info *qp_info)
2941{
2942 struct ib_mad_private_header *mad_priv_hdr;
2943 struct ib_mad_private *recv;
2944 struct ib_mad_list_head *mad_list;
2945
Eli Cohenfac70d52010-09-27 17:51:11 -07002946 if (!qp_info->qp)
2947 return;
2948
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 while (!list_empty(&qp_info->recv_queue.list)) {
2950
2951 mad_list = list_entry(qp_info->recv_queue.list.next,
2952 struct ib_mad_list_head, list);
2953 mad_priv_hdr = container_of(mad_list,
2954 struct ib_mad_private_header,
2955 mad_list);
2956 recv = container_of(mad_priv_hdr, struct ib_mad_private,
2957 header);
2958
2959 /* Remove from posted receive MAD list */
2960 list_del(&mad_list->list);
2961
Ralph Campbell15271062006-12-12 14:28:30 -08002962 ib_dma_unmap_single(qp_info->port_priv->device,
2963 recv->header.mapping,
Ira Weinyc9082e52015-06-06 14:38:30 -04002964 mad_priv_dma_size(recv),
Ralph Campbell15271062006-12-12 14:28:30 -08002965 DMA_FROM_DEVICE);
Ira Weinyc9082e52015-06-06 14:38:30 -04002966 kfree(recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 }
2968
2969 qp_info->recv_queue.count = 0;
2970}
2971
2972/*
2973 * Start the port
2974 */
2975static int ib_mad_port_start(struct ib_mad_port_private *port_priv)
2976{
2977 int ret, i;
2978 struct ib_qp_attr *attr;
2979 struct ib_qp *qp;
Jack Morgensteinef5ed412013-07-18 14:02:29 +03002980 u16 pkey_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
2982 attr = kmalloc(sizeof *attr, GFP_KERNEL);
Leon Romanovsky27162432016-11-03 16:44:09 +02002983 if (!attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
Jack Morgensteinef5ed412013-07-18 14:02:29 +03002986 ret = ib_find_pkey(port_priv->device, port_priv->port_num,
2987 IB_DEFAULT_PKEY_FULL, &pkey_index);
2988 if (ret)
2989 pkey_index = 0;
2990
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 for (i = 0; i < IB_MAD_QPS_CORE; i++) {
2992 qp = port_priv->qp_info[i].qp;
Eli Cohenfac70d52010-09-27 17:51:11 -07002993 if (!qp)
2994 continue;
2995
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 /*
2997 * PKey index for QP1 is irrelevant but
2998 * one is needed for the Reset to Init transition
2999 */
3000 attr->qp_state = IB_QPS_INIT;
Jack Morgensteinef5ed412013-07-18 14:02:29 +03003001 attr->pkey_index = pkey_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 attr->qkey = (qp->qp_num == 0) ? 0 : IB_QP1_QKEY;
3003 ret = ib_modify_qp(qp, attr, IB_QP_STATE |
3004 IB_QP_PKEY_INDEX | IB_QP_QKEY);
3005 if (ret) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04003006 dev_err(&port_priv->device->dev,
3007 "Couldn't change QP%d state to INIT: %d\n",
3008 i, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 goto out;
3010 }
3011
3012 attr->qp_state = IB_QPS_RTR;
3013 ret = ib_modify_qp(qp, attr, IB_QP_STATE);
3014 if (ret) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04003015 dev_err(&port_priv->device->dev,
3016 "Couldn't change QP%d state to RTR: %d\n",
3017 i, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 goto out;
3019 }
3020
3021 attr->qp_state = IB_QPS_RTS;
3022 attr->sq_psn = IB_MAD_SEND_Q_PSN;
3023 ret = ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_SQ_PSN);
3024 if (ret) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04003025 dev_err(&port_priv->device->dev,
3026 "Couldn't change QP%d state to RTS: %d\n",
3027 i, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 goto out;
3029 }
3030 }
3031
3032 ret = ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
3033 if (ret) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04003034 dev_err(&port_priv->device->dev,
3035 "Failed to request completion notification: %d\n",
3036 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 goto out;
3038 }
3039
3040 for (i = 0; i < IB_MAD_QPS_CORE; i++) {
Eli Cohenfac70d52010-09-27 17:51:11 -07003041 if (!port_priv->qp_info[i].qp)
3042 continue;
3043
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 ret = ib_mad_post_receive_mads(&port_priv->qp_info[i], NULL);
3045 if (ret) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04003046 dev_err(&port_priv->device->dev,
3047 "Couldn't post receive WRs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 goto out;
3049 }
3050 }
3051out:
3052 kfree(attr);
3053 return ret;
3054}
3055
3056static void qp_event_handler(struct ib_event *event, void *qp_context)
3057{
3058 struct ib_mad_qp_info *qp_info = qp_context;
3059
3060 /* It's worse than that! He's dead, Jim! */
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04003061 dev_err(&qp_info->port_priv->device->dev,
3062 "Fatal error (%d) on MAD QP (%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 event->event, qp_info->qp->qp_num);
3064}
3065
3066static void init_mad_queue(struct ib_mad_qp_info *qp_info,
3067 struct ib_mad_queue *mad_queue)
3068{
3069 mad_queue->qp_info = qp_info;
3070 mad_queue->count = 0;
3071 spin_lock_init(&mad_queue->lock);
3072 INIT_LIST_HEAD(&mad_queue->list);
3073}
3074
3075static void init_mad_qp(struct ib_mad_port_private *port_priv,
3076 struct ib_mad_qp_info *qp_info)
3077{
3078 qp_info->port_priv = port_priv;
3079 init_mad_queue(qp_info, &qp_info->send_queue);
3080 init_mad_queue(qp_info, &qp_info->recv_queue);
3081 INIT_LIST_HEAD(&qp_info->overflow_list);
3082 spin_lock_init(&qp_info->snoop_lock);
3083 qp_info->snoop_table = NULL;
3084 qp_info->snoop_table_size = 0;
3085 atomic_set(&qp_info->snoop_count, 0);
3086}
3087
3088static int create_mad_qp(struct ib_mad_qp_info *qp_info,
3089 enum ib_qp_type qp_type)
3090{
3091 struct ib_qp_init_attr qp_init_attr;
3092 int ret;
3093
3094 memset(&qp_init_attr, 0, sizeof qp_init_attr);
3095 qp_init_attr.send_cq = qp_info->port_priv->cq;
3096 qp_init_attr.recv_cq = qp_info->port_priv->cq;
3097 qp_init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
Hal Rosenstockb76aabc2009-09-07 08:28:48 -07003098 qp_init_attr.cap.max_send_wr = mad_sendq_size;
3099 qp_init_attr.cap.max_recv_wr = mad_recvq_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 qp_init_attr.cap.max_send_sge = IB_MAD_SEND_REQ_MAX_SG;
3101 qp_init_attr.cap.max_recv_sge = IB_MAD_RECV_REQ_MAX_SG;
3102 qp_init_attr.qp_type = qp_type;
3103 qp_init_attr.port_num = qp_info->port_priv->port_num;
3104 qp_init_attr.qp_context = qp_info;
3105 qp_init_attr.event_handler = qp_event_handler;
3106 qp_info->qp = ib_create_qp(qp_info->port_priv->pd, &qp_init_attr);
3107 if (IS_ERR(qp_info->qp)) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04003108 dev_err(&qp_info->port_priv->device->dev,
3109 "Couldn't create ib_mad QP%d\n",
3110 get_spl_qp_index(qp_type));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 ret = PTR_ERR(qp_info->qp);
3112 goto error;
3113 }
3114 /* Use minimum queue sizes unless the CQ is resized */
Hal Rosenstockb76aabc2009-09-07 08:28:48 -07003115 qp_info->send_queue.max_active = mad_sendq_size;
3116 qp_info->recv_queue.max_active = mad_recvq_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 return 0;
3118
3119error:
3120 return ret;
3121}
3122
3123static void destroy_mad_qp(struct ib_mad_qp_info *qp_info)
3124{
Eli Cohenfac70d52010-09-27 17:51:11 -07003125 if (!qp_info->qp)
3126 return;
3127
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 ib_destroy_qp(qp_info->qp);
Jesper Juhl6044ec82005-11-07 01:01:32 -08003129 kfree(qp_info->snoop_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130}
3131
3132/*
3133 * Open the port
3134 * Create the QP, PD, MR, and CQ if needed
3135 */
3136static int ib_mad_port_open(struct ib_device *device,
3137 int port_num)
3138{
3139 int ret, cq_size;
3140 struct ib_mad_port_private *port_priv;
3141 unsigned long flags;
3142 char name[sizeof "ib_mad123"];
Eli Cohenfac70d52010-09-27 17:51:11 -07003143 int has_smi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
Ira Weiny337877a2015-06-06 14:38:29 -04003145 if (WARN_ON(rdma_max_mad_size(device, port_num) < IB_MGMT_MAD_SIZE))
3146 return -EFAULT;
3147
Ira Weiny548ead12015-06-06 14:38:33 -04003148 if (WARN_ON(rdma_cap_opa_mad(device, port_num) &&
3149 rdma_max_mad_size(device, port_num) < OPA_MGMT_MAD_SIZE))
3150 return -EFAULT;
3151
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 /* Create new device info */
Roland Dreierde6eb662005-11-02 07:23:14 -08003153 port_priv = kzalloc(sizeof *port_priv, GFP_KERNEL);
Leon Romanovsky27162432016-11-03 16:44:09 +02003154 if (!port_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 return -ENOMEM;
Roland Dreierde6eb662005-11-02 07:23:14 -08003156
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 port_priv->device = device;
3158 port_priv->port_num = port_num;
3159 spin_lock_init(&port_priv->reg_lock);
3160 INIT_LIST_HEAD(&port_priv->agent_list);
3161 init_mad_qp(port_priv, &port_priv->qp_info[0]);
3162 init_mad_qp(port_priv, &port_priv->qp_info[1]);
3163
Eli Cohenfac70d52010-09-27 17:51:11 -07003164 cq_size = mad_sendq_size + mad_recvq_size;
Michael Wang29541e32015-05-05 14:50:33 +02003165 has_smi = rdma_cap_ib_smi(device, port_num);
Eli Cohenfac70d52010-09-27 17:51:11 -07003166 if (has_smi)
3167 cq_size *= 2;
3168
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08003169 port_priv->cq = ib_alloc_cq(port_priv->device, port_priv, cq_size, 0,
3170 IB_POLL_WORKQUEUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 if (IS_ERR(port_priv->cq)) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04003172 dev_err(&device->dev, "Couldn't create ib_mad CQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 ret = PTR_ERR(port_priv->cq);
3174 goto error3;
3175 }
3176
Christoph Hellwiged082d32016-09-05 12:56:17 +02003177 port_priv->pd = ib_alloc_pd(device, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 if (IS_ERR(port_priv->pd)) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04003179 dev_err(&device->dev, "Couldn't create ib_mad PD\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 ret = PTR_ERR(port_priv->pd);
3181 goto error4;
3182 }
3183
Eli Cohenfac70d52010-09-27 17:51:11 -07003184 if (has_smi) {
3185 ret = create_mad_qp(&port_priv->qp_info[0], IB_QPT_SMI);
3186 if (ret)
3187 goto error6;
3188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 ret = create_mad_qp(&port_priv->qp_info[1], IB_QPT_GSI);
3190 if (ret)
3191 goto error7;
3192
3193 snprintf(name, sizeof name, "ib_mad%d", port_num);
Bhaktipriya Shridhar1c99e292016-08-15 23:28:07 +05303194 port_priv->wq = alloc_ordered_workqueue(name, WQ_MEM_RECLAIM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 if (!port_priv->wq) {
3196 ret = -ENOMEM;
3197 goto error8;
3198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
Michael S. Tsirkindc059802006-03-20 10:08:25 -08003200 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
3201 list_add_tail(&port_priv->port_list, &ib_mad_port_list);
3202 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
3203
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 ret = ib_mad_port_start(port_priv);
3205 if (ret) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04003206 dev_err(&device->dev, "Couldn't start port\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 goto error9;
3208 }
3209
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 return 0;
3211
3212error9:
Michael S. Tsirkindc059802006-03-20 10:08:25 -08003213 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
3214 list_del_init(&port_priv->port_list);
3215 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
3216
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 destroy_workqueue(port_priv->wq);
3218error8:
3219 destroy_mad_qp(&port_priv->qp_info[1]);
3220error7:
3221 destroy_mad_qp(&port_priv->qp_info[0]);
3222error6:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 ib_dealloc_pd(port_priv->pd);
3224error4:
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08003225 ib_free_cq(port_priv->cq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 cleanup_recv_queue(&port_priv->qp_info[1]);
3227 cleanup_recv_queue(&port_priv->qp_info[0]);
3228error3:
3229 kfree(port_priv);
3230
3231 return ret;
3232}
3233
3234/*
3235 * Close the port
3236 * If there are no classes using the port, free the port
3237 * resources (CQ, MR, PD, QP) and remove the port's info structure
3238 */
3239static int ib_mad_port_close(struct ib_device *device, int port_num)
3240{
3241 struct ib_mad_port_private *port_priv;
3242 unsigned long flags;
3243
3244 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
3245 port_priv = __ib_get_mad_port(device, port_num);
3246 if (port_priv == NULL) {
3247 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04003248 dev_err(&device->dev, "Port %d not found\n", port_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 return -ENODEV;
3250 }
Michael S. Tsirkindc059802006-03-20 10:08:25 -08003251 list_del_init(&port_priv->port_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
3253
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 destroy_workqueue(port_priv->wq);
3255 destroy_mad_qp(&port_priv->qp_info[1]);
3256 destroy_mad_qp(&port_priv->qp_info[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 ib_dealloc_pd(port_priv->pd);
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08003258 ib_free_cq(port_priv->cq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 cleanup_recv_queue(&port_priv->qp_info[1]);
3260 cleanup_recv_queue(&port_priv->qp_info[0]);
3261 /* XXX: Handle deallocation of MAD registration tables */
3262
3263 kfree(port_priv);
3264
3265 return 0;
3266}
3267
3268static void ib_mad_init_device(struct ib_device *device)
3269{
Hal Rosenstock41390322015-06-29 09:57:00 -04003270 int start, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
Hal Rosenstock41390322015-06-29 09:57:00 -04003272 start = rdma_start_port(device);
Roland Dreier4ab6fb72005-10-06 13:28:16 -07003273
Hal Rosenstock41390322015-06-29 09:57:00 -04003274 for (i = start; i <= rdma_end_port(device); i++) {
Michael Wangc757dea2015-05-05 14:50:32 +02003275 if (!rdma_cap_ib_mad(device, i))
Michael Wang827f2a82015-05-05 14:50:20 +02003276 continue;
3277
Roland Dreier4ab6fb72005-10-06 13:28:16 -07003278 if (ib_mad_port_open(device, i)) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04003279 dev_err(&device->dev, "Couldn't open port %d\n", i);
Roland Dreier4ab6fb72005-10-06 13:28:16 -07003280 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 }
Roland Dreier4ab6fb72005-10-06 13:28:16 -07003282 if (ib_agent_port_open(device, i)) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04003283 dev_err(&device->dev,
3284 "Couldn't open port %d for agents\n", i);
Roland Dreier4ab6fb72005-10-06 13:28:16 -07003285 goto error_agent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 }
3287 }
Hal Rosenstockf68bcc22005-07-27 11:45:27 -07003288 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
Roland Dreier4ab6fb72005-10-06 13:28:16 -07003290error_agent:
3291 if (ib_mad_port_close(device, i))
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04003292 dev_err(&device->dev, "Couldn't close port %d\n", i);
Roland Dreier4ab6fb72005-10-06 13:28:16 -07003293
3294error:
Michael Wang827f2a82015-05-05 14:50:20 +02003295 while (--i >= start) {
Michael Wangc757dea2015-05-05 14:50:32 +02003296 if (!rdma_cap_ib_mad(device, i))
Michael Wang827f2a82015-05-05 14:50:20 +02003297 continue;
Roland Dreier4ab6fb72005-10-06 13:28:16 -07003298
Roland Dreier4ab6fb72005-10-06 13:28:16 -07003299 if (ib_agent_port_close(device, i))
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04003300 dev_err(&device->dev,
3301 "Couldn't close port %d for agents\n", i);
Roland Dreier4ab6fb72005-10-06 13:28:16 -07003302 if (ib_mad_port_close(device, i))
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04003303 dev_err(&device->dev, "Couldn't close port %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305}
3306
Haggai Eran7c1eb452015-07-30 17:50:14 +03003307static void ib_mad_remove_device(struct ib_device *device, void *client_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308{
Hal Rosenstock41390322015-06-29 09:57:00 -04003309 int i;
Steve Wise070e1402010-03-04 18:18:18 +00003310
Hal Rosenstock41390322015-06-29 09:57:00 -04003311 for (i = rdma_start_port(device); i <= rdma_end_port(device); i++) {
Michael Wangc757dea2015-05-05 14:50:32 +02003312 if (!rdma_cap_ib_mad(device, i))
Michael Wang827f2a82015-05-05 14:50:20 +02003313 continue;
3314
3315 if (ib_agent_port_close(device, i))
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04003316 dev_err(&device->dev,
Michael Wang827f2a82015-05-05 14:50:20 +02003317 "Couldn't close port %d for agents\n", i);
3318 if (ib_mad_port_close(device, i))
3319 dev_err(&device->dev, "Couldn't close port %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 }
3321}
3322
3323static struct ib_client mad_client = {
3324 .name = "mad",
3325 .add = ib_mad_init_device,
3326 .remove = ib_mad_remove_device
3327};
3328
Mark Bloch4c2cb422016-05-19 17:12:32 +03003329int ib_mad_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330{
Hal Rosenstockb76aabc2009-09-07 08:28:48 -07003331 mad_recvq_size = min(mad_recvq_size, IB_MAD_QP_MAX_SIZE);
3332 mad_recvq_size = max(mad_recvq_size, IB_MAD_QP_MIN_SIZE);
3333
3334 mad_sendq_size = min(mad_sendq_size, IB_MAD_QP_MAX_SIZE);
3335 mad_sendq_size = max(mad_sendq_size, IB_MAD_QP_MIN_SIZE);
3336
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 INIT_LIST_HEAD(&ib_mad_port_list);
3338
3339 if (ib_register_client(&mad_client)) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04003340 pr_err("Couldn't register ib_mad client\n");
Ira Weinyc9082e52015-06-06 14:38:30 -04003341 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 }
3343
3344 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345}
3346
Mark Bloch4c2cb422016-05-19 17:12:32 +03003347void ib_mad_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348{
3349 ib_unregister_client(&mad_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350}