blob: dadce12e9056925924abd55f1f2c5679d792ade5 [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>
Jack Morgenstein9874e742006-06-17 20:37:34 -070043#include <rdma/ib_cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include "mad_priv.h"
Hal Rosenstockfa619a72005-07-27 11:45:37 -070046#include "mad_rmpp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include "smi.h"
Ira Weiny8e4349d2015-06-10 16:16:48 -040048#include "opa_smi.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include "agent.h"
Mark Bloch4c2cb422016-05-19 17:12:32 +030050#include "core_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Roland Dreier16933952010-05-23 21:39:31 -070052static int mad_sendq_size = IB_MAD_QP_SEND_SIZE;
53static int mad_recvq_size = IB_MAD_QP_RECV_SIZE;
Hal Rosenstockb76aabc2009-09-07 08:28:48 -070054
55module_param_named(send_queue_size, mad_sendq_size, int, 0444);
56MODULE_PARM_DESC(send_queue_size, "Size of send queue in number of work requests");
57module_param_named(recv_queue_size, mad_recvq_size, int, 0444);
58MODULE_PARM_DESC(recv_queue_size, "Size of receive queue in number of work requests");
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static struct list_head ib_mad_port_list;
61static u32 ib_mad_client_id = 0;
62
63/* Port list lock */
Roland Dreier6276e082009-09-05 20:24:23 -070064static DEFINE_SPINLOCK(ib_mad_port_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/* Forward declarations */
67static int method_in_use(struct ib_mad_mgmt_method_table **method,
68 struct ib_mad_reg_req *mad_reg_req);
69static void remove_mad_reg_req(struct ib_mad_agent_private *priv);
70static struct ib_mad_agent_private *find_mad_agent(
71 struct ib_mad_port_private *port_priv,
Ira Weinyd94bd262015-06-06 14:38:22 -040072 const struct ib_mad_hdr *mad);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
74 struct ib_mad_private *mad);
75static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv);
David Howellsc4028952006-11-22 14:57:56 +000076static void timeout_sends(struct work_struct *work);
77static void local_completions(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
79 struct ib_mad_agent_private *agent_priv,
80 u8 mgmt_class);
81static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
82 struct ib_mad_agent_private *agent_priv);
Christoph Hellwigd53e11f2016-01-05 22:46:12 -080083static bool ib_mad_send_error(struct ib_mad_port_private *port_priv,
84 struct ib_wc *wc);
85static void ib_mad_send_done(struct ib_cq *cq, struct ib_wc *wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/*
88 * Returns a ib_mad_port_private structure or NULL for a device/port
89 * Assumes ib_mad_port_list_lock is being held
90 */
91static inline struct ib_mad_port_private *
92__ib_get_mad_port(struct ib_device *device, int port_num)
93{
94 struct ib_mad_port_private *entry;
95
96 list_for_each_entry(entry, &ib_mad_port_list, port_list) {
97 if (entry->device == device && entry->port_num == port_num)
98 return entry;
99 }
100 return NULL;
101}
102
103/*
104 * Wrapper function to return a ib_mad_port_private structure or NULL
105 * for a device/port
106 */
107static inline struct ib_mad_port_private *
108ib_get_mad_port(struct ib_device *device, int port_num)
109{
110 struct ib_mad_port_private *entry;
111 unsigned long flags;
112
113 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
114 entry = __ib_get_mad_port(device, port_num);
115 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
116
117 return entry;
118}
119
120static inline u8 convert_mgmt_class(u8 mgmt_class)
121{
122 /* Alias IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE to 0 */
123 return mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE ?
124 0 : mgmt_class;
125}
126
127static int get_spl_qp_index(enum ib_qp_type qp_type)
128{
129 switch (qp_type)
130 {
131 case IB_QPT_SMI:
132 return 0;
133 case IB_QPT_GSI:
134 return 1;
135 default:
136 return -1;
137 }
138}
139
140static int vendor_class_index(u8 mgmt_class)
141{
142 return mgmt_class - IB_MGMT_CLASS_VENDOR_RANGE2_START;
143}
144
145static int is_vendor_class(u8 mgmt_class)
146{
147 if ((mgmt_class < IB_MGMT_CLASS_VENDOR_RANGE2_START) ||
148 (mgmt_class > IB_MGMT_CLASS_VENDOR_RANGE2_END))
149 return 0;
150 return 1;
151}
152
153static int is_vendor_oui(char *oui)
154{
155 if (oui[0] || oui[1] || oui[2])
156 return 1;
157 return 0;
158}
159
160static int is_vendor_method_in_use(
161 struct ib_mad_mgmt_vendor_class *vendor_class,
162 struct ib_mad_reg_req *mad_reg_req)
163{
164 struct ib_mad_mgmt_method_table *method;
165 int i;
166
167 for (i = 0; i < MAX_MGMT_OUI; i++) {
168 if (!memcmp(vendor_class->oui[i], mad_reg_req->oui, 3)) {
169 method = vendor_class->method_table[i];
170 if (method) {
171 if (method_in_use(&method, mad_reg_req))
172 return 1;
173 else
174 break;
175 }
176 }
177 }
178 return 0;
179}
180
Ira Weiny96909302015-05-08 14:27:22 -0400181int ib_response_mad(const struct ib_mad_hdr *hdr)
Sean Hefty2527e682006-07-20 11:25:50 +0300182{
Ira Weiny96909302015-05-08 14:27:22 -0400183 return ((hdr->method & IB_MGMT_METHOD_RESP) ||
184 (hdr->method == IB_MGMT_METHOD_TRAP_REPRESS) ||
185 ((hdr->mgmt_class == IB_MGMT_CLASS_BM) &&
186 (hdr->attr_mod & IB_BM_ATTR_MOD_RESP)));
Sean Hefty2527e682006-07-20 11:25:50 +0300187}
188EXPORT_SYMBOL(ib_response_mad);
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/*
191 * ib_register_mad_agent - Register to send/receive MADs
192 */
193struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
194 u8 port_num,
195 enum ib_qp_type qp_type,
196 struct ib_mad_reg_req *mad_reg_req,
197 u8 rmpp_version,
198 ib_mad_send_handler send_handler,
199 ib_mad_recv_handler recv_handler,
Ira Weiny0f29b462014-08-08 19:00:55 -0400200 void *context,
201 u32 registration_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 struct ib_mad_port_private *port_priv;
204 struct ib_mad_agent *ret = ERR_PTR(-EINVAL);
205 struct ib_mad_agent_private *mad_agent_priv;
206 struct ib_mad_reg_req *reg_req = NULL;
207 struct ib_mad_mgmt_class_table *class;
208 struct ib_mad_mgmt_vendor_class_table *vendor;
209 struct ib_mad_mgmt_vendor_class *vendor_class;
210 struct ib_mad_mgmt_method_table *method;
211 int ret2, qpn;
212 unsigned long flags;
213 u8 mgmt_class, vclass;
214
215 /* Validate parameters */
216 qpn = get_spl_qp_index(qp_type);
Ira Weiny9ad13a42014-08-08 19:00:54 -0400217 if (qpn == -1) {
218 dev_notice(&device->dev,
219 "ib_register_mad_agent: invalid QP Type %d\n",
220 qp_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 goto error1;
Ira Weiny9ad13a42014-08-08 19:00:54 -0400222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Ira Weiny9ad13a42014-08-08 19:00:54 -0400224 if (rmpp_version && rmpp_version != IB_MGMT_RMPP_VERSION) {
225 dev_notice(&device->dev,
226 "ib_register_mad_agent: invalid RMPP Version %u\n",
227 rmpp_version);
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700228 goto error1;
Ira Weiny9ad13a42014-08-08 19:00:54 -0400229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 /* Validate MAD registration request if supplied */
232 if (mad_reg_req) {
Ira Weiny9ad13a42014-08-08 19:00:54 -0400233 if (mad_reg_req->mgmt_class_version >= MAX_MGMT_VERSION) {
234 dev_notice(&device->dev,
235 "ib_register_mad_agent: invalid Class Version %u\n",
236 mad_reg_req->mgmt_class_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 goto error1;
Ira Weiny9ad13a42014-08-08 19:00:54 -0400238 }
239 if (!recv_handler) {
240 dev_notice(&device->dev,
241 "ib_register_mad_agent: no recv_handler\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 goto error1;
Ira Weiny9ad13a42014-08-08 19:00:54 -0400243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if (mad_reg_req->mgmt_class >= MAX_MGMT_CLASS) {
245 /*
246 * IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE is the only
247 * one in this range currently allowed
248 */
249 if (mad_reg_req->mgmt_class !=
Ira Weiny9ad13a42014-08-08 19:00:54 -0400250 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
251 dev_notice(&device->dev,
252 "ib_register_mad_agent: Invalid Mgmt Class 0x%x\n",
253 mad_reg_req->mgmt_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 goto error1;
Ira Weiny9ad13a42014-08-08 19:00:54 -0400255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 } else if (mad_reg_req->mgmt_class == 0) {
257 /*
258 * Class 0 is reserved in IBA and is used for
259 * aliasing of IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
260 */
Ira Weiny9ad13a42014-08-08 19:00:54 -0400261 dev_notice(&device->dev,
262 "ib_register_mad_agent: Invalid Mgmt Class 0\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 goto error1;
264 } else if (is_vendor_class(mad_reg_req->mgmt_class)) {
265 /*
266 * If class is in "new" vendor range,
267 * ensure supplied OUI is not zero
268 */
Ira Weiny9ad13a42014-08-08 19:00:54 -0400269 if (!is_vendor_oui(mad_reg_req->oui)) {
270 dev_notice(&device->dev,
271 "ib_register_mad_agent: No OUI specified for class 0x%x\n",
272 mad_reg_req->mgmt_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 goto error1;
Ira Weiny9ad13a42014-08-08 19:00:54 -0400274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
Hal Rosenstock618a3c02006-03-28 16:40:04 -0800276 /* Make sure class supplied is consistent with RMPP */
Hal Rosenstock64cb9c62006-04-12 21:29:10 -0400277 if (!ib_is_mad_class_rmpp(mad_reg_req->mgmt_class)) {
Ira Weiny9ad13a42014-08-08 19:00:54 -0400278 if (rmpp_version) {
279 dev_notice(&device->dev,
280 "ib_register_mad_agent: RMPP version for non-RMPP class 0x%x\n",
281 mad_reg_req->mgmt_class);
Hal Rosenstock618a3c02006-03-28 16:40:04 -0800282 goto error1;
Ira Weiny9ad13a42014-08-08 19:00:54 -0400283 }
Hal Rosenstock618a3c02006-03-28 16:40:04 -0800284 }
Ira Weiny1471cb62014-08-08 19:00:56 -0400285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 /* Make sure class supplied is consistent with QP type */
287 if (qp_type == IB_QPT_SMI) {
288 if ((mad_reg_req->mgmt_class !=
289 IB_MGMT_CLASS_SUBN_LID_ROUTED) &&
290 (mad_reg_req->mgmt_class !=
Ira Weiny9ad13a42014-08-08 19:00:54 -0400291 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) {
292 dev_notice(&device->dev,
293 "ib_register_mad_agent: Invalid SM QP type: class 0x%x\n",
294 mad_reg_req->mgmt_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 goto error1;
Ira Weiny9ad13a42014-08-08 19:00:54 -0400296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 } else {
298 if ((mad_reg_req->mgmt_class ==
299 IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
300 (mad_reg_req->mgmt_class ==
Ira Weiny9ad13a42014-08-08 19:00:54 -0400301 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) {
302 dev_notice(&device->dev,
303 "ib_register_mad_agent: Invalid GS QP type: class 0x%x\n",
304 mad_reg_req->mgmt_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 goto error1;
Ira Weiny9ad13a42014-08-08 19:00:54 -0400306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308 } else {
309 /* No registration request supplied */
310 if (!send_handler)
311 goto error1;
Ira Weiny1471cb62014-08-08 19:00:56 -0400312 if (registration_flags & IB_MAD_USER_RMPP)
313 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
315
316 /* Validate device and port */
317 port_priv = ib_get_mad_port(device, port_num);
318 if (!port_priv) {
Ira Weiny9ad13a42014-08-08 19:00:54 -0400319 dev_notice(&device->dev, "ib_register_mad_agent: Invalid port\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 ret = ERR_PTR(-ENODEV);
321 goto error1;
322 }
323
Ira Weinyc8367c42011-05-19 18:19:28 -0700324 /* Verify the QP requested is supported. For example, Ethernet devices
325 * will not have QP0 */
326 if (!port_priv->qp_info[qpn].qp) {
Ira Weiny9ad13a42014-08-08 19:00:54 -0400327 dev_notice(&device->dev,
328 "ib_register_mad_agent: QP %d not supported\n", qpn);
Ira Weinyc8367c42011-05-19 18:19:28 -0700329 ret = ERR_PTR(-EPROTONOSUPPORT);
330 goto error1;
331 }
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 /* Allocate structures */
Roland Dreierde6eb662005-11-02 07:23:14 -0800334 mad_agent_priv = kzalloc(sizeof *mad_agent_priv, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (!mad_agent_priv) {
336 ret = ERR_PTR(-ENOMEM);
337 goto error1;
338 }
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (mad_reg_req) {
Julia Lawall9893e742010-05-15 23:22:38 +0200341 reg_req = kmemdup(mad_reg_req, sizeof *reg_req, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (!reg_req) {
343 ret = ERR_PTR(-ENOMEM);
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700344 goto error3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347
348 /* Now, fill in the various structures */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 mad_agent_priv->qp_info = &port_priv->qp_info[qpn];
350 mad_agent_priv->reg_req = reg_req;
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700351 mad_agent_priv->agent.rmpp_version = rmpp_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 mad_agent_priv->agent.device = device;
353 mad_agent_priv->agent.recv_handler = recv_handler;
354 mad_agent_priv->agent.send_handler = send_handler;
355 mad_agent_priv->agent.context = context;
356 mad_agent_priv->agent.qp = port_priv->qp_info[qpn].qp;
357 mad_agent_priv->agent.port_num = port_num;
Ira Weiny0f29b462014-08-08 19:00:55 -0400358 mad_agent_priv->agent.flags = registration_flags;
Ralph Campbelld9620a42009-02-27 14:44:32 -0800359 spin_lock_init(&mad_agent_priv->lock);
360 INIT_LIST_HEAD(&mad_agent_priv->send_list);
361 INIT_LIST_HEAD(&mad_agent_priv->wait_list);
362 INIT_LIST_HEAD(&mad_agent_priv->done_list);
363 INIT_LIST_HEAD(&mad_agent_priv->rmpp_list);
364 INIT_DELAYED_WORK(&mad_agent_priv->timed_work, timeout_sends);
365 INIT_LIST_HEAD(&mad_agent_priv->local_list);
366 INIT_WORK(&mad_agent_priv->local_work, local_completions);
367 atomic_set(&mad_agent_priv->refcount, 1);
368 init_completion(&mad_agent_priv->comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 spin_lock_irqsave(&port_priv->reg_lock, flags);
371 mad_agent_priv->agent.hi_tid = ++ib_mad_client_id;
372
373 /*
374 * Make sure MAD registration (if supplied)
375 * is non overlapping with any existing ones
376 */
377 if (mad_reg_req) {
378 mgmt_class = convert_mgmt_class(mad_reg_req->mgmt_class);
379 if (!is_vendor_class(mgmt_class)) {
380 class = port_priv->version[mad_reg_req->
381 mgmt_class_version].class;
382 if (class) {
383 method = class->method_table[mgmt_class];
384 if (method) {
385 if (method_in_use(&method,
386 mad_reg_req))
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700387 goto error4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389 }
390 ret2 = add_nonoui_reg_req(mad_reg_req, mad_agent_priv,
391 mgmt_class);
392 } else {
393 /* "New" vendor class range */
394 vendor = port_priv->version[mad_reg_req->
395 mgmt_class_version].vendor;
396 if (vendor) {
397 vclass = vendor_class_index(mgmt_class);
398 vendor_class = vendor->vendor_class[vclass];
399 if (vendor_class) {
400 if (is_vendor_method_in_use(
401 vendor_class,
402 mad_reg_req))
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700403 goto error4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405 }
406 ret2 = add_oui_reg_req(mad_reg_req, mad_agent_priv);
407 }
408 if (ret2) {
409 ret = ERR_PTR(ret2);
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700410 goto error4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412 }
413
414 /* Add mad agent into port's agent list */
415 list_add_tail(&mad_agent_priv->agent_list, &port_priv->agent_list);
416 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return &mad_agent_priv->agent;
419
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700420error4:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
422 kfree(reg_req);
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700423error3:
Adrian Bunk2012a112005-11-27 00:37:36 +0100424 kfree(mad_agent_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425error1:
426 return ret;
427}
428EXPORT_SYMBOL(ib_register_mad_agent);
429
430static inline int is_snooping_sends(int mad_snoop_flags)
431{
432 return (mad_snoop_flags &
433 (/*IB_MAD_SNOOP_POSTED_SENDS |
434 IB_MAD_SNOOP_RMPP_SENDS |*/
435 IB_MAD_SNOOP_SEND_COMPLETIONS /*|
436 IB_MAD_SNOOP_RMPP_SEND_COMPLETIONS*/));
437}
438
439static inline int is_snooping_recvs(int mad_snoop_flags)
440{
441 return (mad_snoop_flags &
442 (IB_MAD_SNOOP_RECVS /*|
443 IB_MAD_SNOOP_RMPP_RECVS*/));
444}
445
446static int register_snoop_agent(struct ib_mad_qp_info *qp_info,
447 struct ib_mad_snoop_private *mad_snoop_priv)
448{
449 struct ib_mad_snoop_private **new_snoop_table;
450 unsigned long flags;
451 int i;
452
453 spin_lock_irqsave(&qp_info->snoop_lock, flags);
454 /* Check for empty slot in array. */
455 for (i = 0; i < qp_info->snoop_table_size; i++)
456 if (!qp_info->snoop_table[i])
457 break;
458
459 if (i == qp_info->snoop_table_size) {
460 /* Grow table. */
Roland Dreier528051742008-10-14 14:05:36 -0700461 new_snoop_table = krealloc(qp_info->snoop_table,
462 sizeof mad_snoop_priv *
463 (qp_info->snoop_table_size + 1),
464 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (!new_snoop_table) {
466 i = -ENOMEM;
467 goto out;
468 }
Roland Dreier528051742008-10-14 14:05:36 -0700469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 qp_info->snoop_table = new_snoop_table;
471 qp_info->snoop_table_size++;
472 }
473 qp_info->snoop_table[i] = mad_snoop_priv;
474 atomic_inc(&qp_info->snoop_count);
475out:
476 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
477 return i;
478}
479
480struct ib_mad_agent *ib_register_mad_snoop(struct ib_device *device,
481 u8 port_num,
482 enum ib_qp_type qp_type,
483 int mad_snoop_flags,
484 ib_mad_snoop_handler snoop_handler,
485 ib_mad_recv_handler recv_handler,
486 void *context)
487{
488 struct ib_mad_port_private *port_priv;
489 struct ib_mad_agent *ret;
490 struct ib_mad_snoop_private *mad_snoop_priv;
491 int qpn;
492
493 /* Validate parameters */
494 if ((is_snooping_sends(mad_snoop_flags) && !snoop_handler) ||
495 (is_snooping_recvs(mad_snoop_flags) && !recv_handler)) {
496 ret = ERR_PTR(-EINVAL);
497 goto error1;
498 }
499 qpn = get_spl_qp_index(qp_type);
500 if (qpn == -1) {
501 ret = ERR_PTR(-EINVAL);
502 goto error1;
503 }
504 port_priv = ib_get_mad_port(device, port_num);
505 if (!port_priv) {
506 ret = ERR_PTR(-ENODEV);
507 goto error1;
508 }
509 /* Allocate structures */
Roland Dreierde6eb662005-11-02 07:23:14 -0800510 mad_snoop_priv = kzalloc(sizeof *mad_snoop_priv, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (!mad_snoop_priv) {
512 ret = ERR_PTR(-ENOMEM);
513 goto error1;
514 }
515
516 /* Now, fill in the various structures */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 mad_snoop_priv->qp_info = &port_priv->qp_info[qpn];
518 mad_snoop_priv->agent.device = device;
519 mad_snoop_priv->agent.recv_handler = recv_handler;
520 mad_snoop_priv->agent.snoop_handler = snoop_handler;
521 mad_snoop_priv->agent.context = context;
522 mad_snoop_priv->agent.qp = port_priv->qp_info[qpn].qp;
523 mad_snoop_priv->agent.port_num = port_num;
524 mad_snoop_priv->mad_snoop_flags = mad_snoop_flags;
Sean Hefty1b52fa982006-05-12 14:57:52 -0700525 init_completion(&mad_snoop_priv->comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 mad_snoop_priv->snoop_index = register_snoop_agent(
527 &port_priv->qp_info[qpn],
528 mad_snoop_priv);
529 if (mad_snoop_priv->snoop_index < 0) {
530 ret = ERR_PTR(mad_snoop_priv->snoop_index);
531 goto error2;
532 }
533
534 atomic_set(&mad_snoop_priv->refcount, 1);
535 return &mad_snoop_priv->agent;
536
537error2:
538 kfree(mad_snoop_priv);
539error1:
540 return ret;
541}
542EXPORT_SYMBOL(ib_register_mad_snoop);
543
Sean Hefty1b52fa982006-05-12 14:57:52 -0700544static inline void deref_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
545{
546 if (atomic_dec_and_test(&mad_agent_priv->refcount))
547 complete(&mad_agent_priv->comp);
548}
549
550static inline void deref_snoop_agent(struct ib_mad_snoop_private *mad_snoop_priv)
551{
552 if (atomic_dec_and_test(&mad_snoop_priv->refcount))
553 complete(&mad_snoop_priv->comp);
554}
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556static void unregister_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
557{
558 struct ib_mad_port_private *port_priv;
559 unsigned long flags;
560
561 /* Note that we could still be handling received MADs */
562
563 /*
564 * Canceling all sends results in dropping received response
565 * MADs, preventing us from queuing additional work
566 */
567 cancel_mads(mad_agent_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 port_priv = mad_agent_priv->qp_info->port_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 cancel_delayed_work(&mad_agent_priv->timed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 spin_lock_irqsave(&port_priv->reg_lock, flags);
572 remove_mad_reg_req(mad_agent_priv);
573 list_del(&mad_agent_priv->agent_list);
574 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
575
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700576 flush_workqueue(port_priv->wq);
Hal Rosenstockfa619a72005-07-27 11:45:37 -0700577 ib_cancel_rmpp_recvs(mad_agent_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Sean Hefty1b52fa982006-05-12 14:57:52 -0700579 deref_mad_agent(mad_agent_priv);
580 wait_for_completion(&mad_agent_priv->comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Jesper Juhl6044ec82005-11-07 01:01:32 -0800582 kfree(mad_agent_priv->reg_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 kfree(mad_agent_priv);
584}
585
586static void unregister_mad_snoop(struct ib_mad_snoop_private *mad_snoop_priv)
587{
588 struct ib_mad_qp_info *qp_info;
589 unsigned long flags;
590
591 qp_info = mad_snoop_priv->qp_info;
592 spin_lock_irqsave(&qp_info->snoop_lock, flags);
593 qp_info->snoop_table[mad_snoop_priv->snoop_index] = NULL;
594 atomic_dec(&qp_info->snoop_count);
595 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
596
Sean Hefty1b52fa982006-05-12 14:57:52 -0700597 deref_snoop_agent(mad_snoop_priv);
598 wait_for_completion(&mad_snoop_priv->comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 kfree(mad_snoop_priv);
601}
602
603/*
604 * ib_unregister_mad_agent - Unregisters a client from using MAD services
605 */
606int ib_unregister_mad_agent(struct ib_mad_agent *mad_agent)
607{
608 struct ib_mad_agent_private *mad_agent_priv;
609 struct ib_mad_snoop_private *mad_snoop_priv;
610
611 /* If the TID is zero, the agent can only snoop. */
612 if (mad_agent->hi_tid) {
613 mad_agent_priv = container_of(mad_agent,
614 struct ib_mad_agent_private,
615 agent);
616 unregister_mad_agent(mad_agent_priv);
617 } else {
618 mad_snoop_priv = container_of(mad_agent,
619 struct ib_mad_snoop_private,
620 agent);
621 unregister_mad_snoop(mad_snoop_priv);
622 }
623 return 0;
624}
625EXPORT_SYMBOL(ib_unregister_mad_agent);
626
627static void dequeue_mad(struct ib_mad_list_head *mad_list)
628{
629 struct ib_mad_queue *mad_queue;
630 unsigned long flags;
631
632 BUG_ON(!mad_list->mad_queue);
633 mad_queue = mad_list->mad_queue;
634 spin_lock_irqsave(&mad_queue->lock, flags);
635 list_del(&mad_list->list);
636 mad_queue->count--;
637 spin_unlock_irqrestore(&mad_queue->lock, flags);
638}
639
640static void snoop_send(struct ib_mad_qp_info *qp_info,
Sean Hefty34816ad2005-10-25 10:51:39 -0700641 struct ib_mad_send_buf *send_buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 struct ib_mad_send_wc *mad_send_wc,
643 int mad_snoop_flags)
644{
645 struct ib_mad_snoop_private *mad_snoop_priv;
646 unsigned long flags;
647 int i;
648
649 spin_lock_irqsave(&qp_info->snoop_lock, flags);
650 for (i = 0; i < qp_info->snoop_table_size; i++) {
651 mad_snoop_priv = qp_info->snoop_table[i];
652 if (!mad_snoop_priv ||
653 !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
654 continue;
655
656 atomic_inc(&mad_snoop_priv->refcount);
657 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
658 mad_snoop_priv->agent.snoop_handler(&mad_snoop_priv->agent,
Sean Hefty34816ad2005-10-25 10:51:39 -0700659 send_buf, mad_send_wc);
Sean Hefty1b52fa982006-05-12 14:57:52 -0700660 deref_snoop_agent(mad_snoop_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 spin_lock_irqsave(&qp_info->snoop_lock, flags);
662 }
663 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
664}
665
666static void snoop_recv(struct ib_mad_qp_info *qp_info,
667 struct ib_mad_recv_wc *mad_recv_wc,
668 int mad_snoop_flags)
669{
670 struct ib_mad_snoop_private *mad_snoop_priv;
671 unsigned long flags;
672 int i;
673
674 spin_lock_irqsave(&qp_info->snoop_lock, flags);
675 for (i = 0; i < qp_info->snoop_table_size; i++) {
676 mad_snoop_priv = qp_info->snoop_table[i];
677 if (!mad_snoop_priv ||
678 !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
679 continue;
680
681 atomic_inc(&mad_snoop_priv->refcount);
682 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
Christoph Hellwigca281262016-01-04 14:15:58 +0100683 mad_snoop_priv->agent.recv_handler(&mad_snoop_priv->agent, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 mad_recv_wc);
Sean Hefty1b52fa982006-05-12 14:57:52 -0700685 deref_snoop_agent(mad_snoop_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 spin_lock_irqsave(&qp_info->snoop_lock, flags);
687 }
688 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
689}
690
Christoph Hellwigd53e11f2016-01-05 22:46:12 -0800691static void build_smp_wc(struct ib_qp *qp, struct ib_cqe *cqe, u16 slid,
692 u16 pkey_index, u8 port_num, struct ib_wc *wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
694 memset(wc, 0, sizeof *wc);
Christoph Hellwigd53e11f2016-01-05 22:46:12 -0800695 wc->wr_cqe = cqe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 wc->status = IB_WC_SUCCESS;
697 wc->opcode = IB_WC_RECV;
698 wc->pkey_index = pkey_index;
699 wc->byte_len = sizeof(struct ib_mad) + sizeof(struct ib_grh);
700 wc->src_qp = IB_QP0;
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +0200701 wc->qp = qp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 wc->slid = slid;
703 wc->sl = 0;
704 wc->dlid_path_bits = 0;
705 wc->port_num = port_num;
706}
707
Ira Weinyc9082e52015-06-06 14:38:30 -0400708static size_t mad_priv_size(const struct ib_mad_private *mp)
709{
710 return sizeof(struct ib_mad_private) + mp->mad_size;
711}
712
713static struct ib_mad_private *alloc_mad_private(size_t mad_size, gfp_t flags)
714{
715 size_t size = sizeof(struct ib_mad_private) + mad_size;
716 struct ib_mad_private *ret = kzalloc(size, flags);
717
718 if (ret)
719 ret->mad_size = mad_size;
720
721 return ret;
722}
723
724static size_t port_mad_size(const struct ib_mad_port_private *port_priv)
725{
726 return rdma_max_mad_size(port_priv->device, port_priv->port_num);
727}
728
729static size_t mad_priv_dma_size(const struct ib_mad_private *mp)
730{
731 return sizeof(struct ib_grh) + mp->mad_size;
732}
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734/*
735 * Return 0 if SMP is to be sent
736 * Return 1 if SMP was consumed locally (whether or not solicited)
737 * Return < 0 if error
738 */
739static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
Sean Hefty34816ad2005-10-25 10:51:39 -0700740 struct ib_mad_send_wr_private *mad_send_wr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Hal Rosenstockde493d42007-04-02 11:24:07 -0400742 int ret = 0;
Sean Hefty34816ad2005-10-25 10:51:39 -0700743 struct ib_smp *smp = mad_send_wr->send_buf.mad;
Ira Weiny8e4349d2015-06-10 16:16:48 -0400744 struct opa_smp *opa_smp = (struct opa_smp *)smp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 unsigned long flags;
746 struct ib_mad_local_private *local;
747 struct ib_mad_private *mad_priv;
748 struct ib_mad_port_private *port_priv;
749 struct ib_mad_agent_private *recv_mad_agent = NULL;
750 struct ib_device *device = mad_agent_priv->agent.device;
Hal Rosenstock1bae4db2007-05-14 17:21:52 -0400751 u8 port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 struct ib_wc mad_wc;
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100753 struct ib_ud_wr *send_wr = &mad_send_wr->send_wr;
Ira Weinyc9082e52015-06-06 14:38:30 -0400754 size_t mad_size = port_mad_size(mad_agent_priv->qp_info->port_priv);
Ira Weiny4cd7c942015-06-06 14:38:31 -0400755 u16 out_mad_pkey_index = 0;
Ira Weiny8e4349d2015-06-10 16:16:48 -0400756 u16 drslid;
757 bool opa = rdma_cap_opa_mad(mad_agent_priv->qp_info->port_priv->device,
758 mad_agent_priv->qp_info->port_priv->port_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Hal Rosenstock41390322015-06-29 09:57:00 -0400760 if (rdma_cap_ib_switch(device) &&
Hal Rosenstock1bae4db2007-05-14 17:21:52 -0400761 smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100762 port_num = send_wr->port_num;
Hal Rosenstock1bae4db2007-05-14 17:21:52 -0400763 else
764 port_num = mad_agent_priv->agent.port_num;
765
Ralph Campbell8cf3f042006-02-03 14:28:48 -0800766 /*
767 * Directed route handling starts if the initial LID routed part of
768 * a request or the ending LID routed part of a response is empty.
769 * If we are at the start of the LID routed part, don't update the
770 * hop_ptr or hop_cnt. See section 14.2.2, Vol 1 IB spec.
771 */
Ira Weiny8e4349d2015-06-10 16:16:48 -0400772 if (opa && smp->class_version == OPA_SMP_CLASS_VERSION) {
773 u32 opa_drslid;
Hal Rosenstockde493d42007-04-02 11:24:07 -0400774
Ira Weiny8e4349d2015-06-10 16:16:48 -0400775 if ((opa_get_smp_direction(opa_smp)
776 ? opa_smp->route.dr.dr_dlid : opa_smp->route.dr.dr_slid) ==
777 OPA_LID_PERMISSIVE &&
Hal Rosenstock41390322015-06-29 09:57:00 -0400778 opa_smi_handle_dr_smp_send(opa_smp,
779 rdma_cap_ib_switch(device),
Ira Weiny8e4349d2015-06-10 16:16:48 -0400780 port_num) == IB_SMI_DISCARD) {
781 ret = -EINVAL;
782 dev_err(&device->dev, "OPA Invalid directed route\n");
783 goto out;
784 }
785 opa_drslid = be32_to_cpu(opa_smp->route.dr.dr_slid);
Ira Weinycd4cd562015-06-25 12:04:49 -0400786 if (opa_drslid != be32_to_cpu(OPA_LID_PERMISSIVE) &&
Ira Weiny8e4349d2015-06-10 16:16:48 -0400787 opa_drslid & 0xffff0000) {
788 ret = -EINVAL;
789 dev_err(&device->dev, "OPA Invalid dr_slid 0x%x\n",
790 opa_drslid);
791 goto out;
792 }
793 drslid = (u16)(opa_drslid & 0x0000ffff);
794
795 /* Check to post send on QP or process locally */
796 if (opa_smi_check_local_smp(opa_smp, device) == IB_SMI_DISCARD &&
797 opa_smi_check_local_returning_smp(opa_smp, device) == IB_SMI_DISCARD)
798 goto out;
799 } else {
800 if ((ib_get_smp_direction(smp) ? smp->dr_dlid : smp->dr_slid) ==
801 IB_LID_PERMISSIVE &&
Hal Rosenstock41390322015-06-29 09:57:00 -0400802 smi_handle_dr_smp_send(smp, rdma_cap_ib_switch(device), port_num) ==
Ira Weiny8e4349d2015-06-10 16:16:48 -0400803 IB_SMI_DISCARD) {
804 ret = -EINVAL;
805 dev_err(&device->dev, "Invalid directed route\n");
806 goto out;
807 }
808 drslid = be16_to_cpu(smp->dr_slid);
809
810 /* Check to post send on QP or process locally */
811 if (smi_check_local_smp(smp, device) == IB_SMI_DISCARD &&
812 smi_check_local_returning_smp(smp, device) == IB_SMI_DISCARD)
813 goto out;
814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 local = kmalloc(sizeof *local, GFP_ATOMIC);
817 if (!local) {
818 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 goto out;
820 }
821 local->mad_priv = NULL;
822 local->recv_mad_agent = NULL;
Ira Weinyc9082e52015-06-06 14:38:30 -0400823 mad_priv = alloc_mad_private(mad_size, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (!mad_priv) {
825 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 kfree(local);
827 goto out;
828 }
829
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +0200830 build_smp_wc(mad_agent_priv->agent.qp,
Christoph Hellwigd53e11f2016-01-05 22:46:12 -0800831 send_wr->wr.wr_cqe, drslid,
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100832 send_wr->pkey_index,
833 send_wr->port_num, &mad_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Ira Weiny8e4349d2015-06-10 16:16:48 -0400835 if (opa && smp->base_version == OPA_MGMT_BASE_VERSION) {
836 mad_wc.byte_len = mad_send_wr->send_buf.hdr_len
837 + mad_send_wr->send_buf.data_len
838 + sizeof(struct ib_grh);
839 }
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 /* No GRH for DR SMP */
842 ret = device->process_mad(device, 0, port_num, &mad_wc, NULL,
Ira Weiny4cd7c942015-06-06 14:38:31 -0400843 (const struct ib_mad_hdr *)smp, mad_size,
844 (struct ib_mad_hdr *)mad_priv->mad,
845 &mad_size, &out_mad_pkey_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 switch (ret)
847 {
848 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY:
Ira Weinyc9082e52015-06-06 14:38:30 -0400849 if (ib_response_mad((const struct ib_mad_hdr *)mad_priv->mad) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 mad_agent_priv->agent.recv_handler) {
851 local->mad_priv = mad_priv;
852 local->recv_mad_agent = mad_agent_priv;
853 /*
854 * Reference MAD agent until receive
855 * side of local completion handled
856 */
857 atomic_inc(&mad_agent_priv->refcount);
858 } else
Ira Weinyc9082e52015-06-06 14:38:30 -0400859 kfree(mad_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 break;
861 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED:
Ira Weinyc9082e52015-06-06 14:38:30 -0400862 kfree(mad_priv);
Ralph Campbell4780c192009-03-03 14:22:17 -0800863 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 case IB_MAD_RESULT_SUCCESS:
865 /* Treat like an incoming receive MAD */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 port_priv = ib_get_mad_port(mad_agent_priv->agent.device,
867 mad_agent_priv->agent.port_num);
868 if (port_priv) {
Ira Weinyc9082e52015-06-06 14:38:30 -0400869 memcpy(mad_priv->mad, smp, mad_priv->mad_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 recv_mad_agent = find_mad_agent(port_priv,
Ira Weinyc9082e52015-06-06 14:38:30 -0400871 (const struct ib_mad_hdr *)mad_priv->mad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
873 if (!port_priv || !recv_mad_agent) {
Ralph Campbell4780c192009-03-03 14:22:17 -0800874 /*
875 * No receiving agent so drop packet and
876 * generate send completion.
877 */
Ira Weinyc9082e52015-06-06 14:38:30 -0400878 kfree(mad_priv);
Ralph Campbell4780c192009-03-03 14:22:17 -0800879 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
881 local->mad_priv = mad_priv;
882 local->recv_mad_agent = recv_mad_agent;
883 break;
884 default:
Ira Weinyc9082e52015-06-06 14:38:30 -0400885 kfree(mad_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 kfree(local);
887 ret = -EINVAL;
888 goto out;
889 }
890
Sean Hefty34816ad2005-10-25 10:51:39 -0700891 local->mad_send_wr = mad_send_wr;
Ira Weiny8e4349d2015-06-10 16:16:48 -0400892 if (opa) {
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100893 local->mad_send_wr->send_wr.pkey_index = out_mad_pkey_index;
Ira Weiny8e4349d2015-06-10 16:16:48 -0400894 local->return_wc_byte_len = mad_size;
895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 /* Reference MAD agent until send side of local completion handled */
897 atomic_inc(&mad_agent_priv->refcount);
898 /* Queue local completion to local list */
899 spin_lock_irqsave(&mad_agent_priv->lock, flags);
900 list_add_tail(&local->completion_list, &mad_agent_priv->local_list);
901 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
902 queue_work(mad_agent_priv->qp_info->port_priv->wq,
Hal Rosenstockb82cab62005-07-27 11:45:22 -0700903 &mad_agent_priv->local_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 ret = 1;
905out:
906 return ret;
907}
908
Ira Weiny548ead12015-06-06 14:38:33 -0400909static int get_pad_size(int hdr_len, int data_len, size_t mad_size)
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700910{
911 int seg_size, pad;
912
Ira Weiny548ead12015-06-06 14:38:33 -0400913 seg_size = mad_size - hdr_len;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700914 if (data_len && seg_size) {
915 pad = seg_size - data_len % seg_size;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800916 return pad == seg_size ? 0 : pad;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700917 } else
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800918 return seg_size;
919}
920
921static void free_send_rmpp_list(struct ib_mad_send_wr_private *mad_send_wr)
922{
923 struct ib_rmpp_segment *s, *t;
924
925 list_for_each_entry_safe(s, t, &mad_send_wr->rmpp_list, list) {
926 list_del(&s->list);
927 kfree(s);
928 }
929}
930
931static int alloc_send_rmpp_list(struct ib_mad_send_wr_private *send_wr,
Ira Weiny548ead12015-06-06 14:38:33 -0400932 size_t mad_size, gfp_t gfp_mask)
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800933{
934 struct ib_mad_send_buf *send_buf = &send_wr->send_buf;
935 struct ib_rmpp_mad *rmpp_mad = send_buf->mad;
936 struct ib_rmpp_segment *seg = NULL;
937 int left, seg_size, pad;
938
Ira Weiny548ead12015-06-06 14:38:33 -0400939 send_buf->seg_size = mad_size - send_buf->hdr_len;
940 send_buf->seg_rmpp_size = mad_size - IB_MGMT_RMPP_HDR;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800941 seg_size = send_buf->seg_size;
942 pad = send_wr->pad;
943
944 /* Allocate data segments. */
945 for (left = send_buf->data_len + pad; left > 0; left -= seg_size) {
946 seg = kmalloc(sizeof (*seg) + seg_size, gfp_mask);
947 if (!seg) {
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800948 free_send_rmpp_list(send_wr);
949 return -ENOMEM;
950 }
951 seg->num = ++send_buf->seg_count;
952 list_add_tail(&seg->list, &send_wr->rmpp_list);
953 }
954
955 /* Zero any padding */
956 if (pad)
957 memset(seg->data + seg_size - pad, 0, pad);
958
959 rmpp_mad->rmpp_hdr.rmpp_version = send_wr->mad_agent_priv->
960 agent.rmpp_version;
961 rmpp_mad->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_DATA;
962 ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
963
964 send_wr->cur_seg = container_of(send_wr->rmpp_list.next,
965 struct ib_rmpp_segment, list);
966 send_wr->last_ack_seg = send_wr->cur_seg;
967 return 0;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700968}
969
Ira Weinyf766c582015-05-08 14:27:24 -0400970int ib_mad_kernel_rmpp_agent(const struct ib_mad_agent *agent)
Ira Weiny1471cb62014-08-08 19:00:56 -0400971{
972 return agent->rmpp_version && !(agent->flags & IB_MAD_USER_RMPP);
973}
974EXPORT_SYMBOL(ib_mad_kernel_rmpp_agent);
975
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700976struct ib_mad_send_buf * ib_create_send_mad(struct ib_mad_agent *mad_agent,
977 u32 remote_qpn, u16 pkey_index,
Sean Hefty34816ad2005-10-25 10:51:39 -0700978 int rmpp_active,
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700979 int hdr_len, int data_len,
Ira Weinyda2dfaa2015-06-06 14:38:28 -0400980 gfp_t gfp_mask,
981 u8 base_version)
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700982{
983 struct ib_mad_agent_private *mad_agent_priv;
Sean Hefty34816ad2005-10-25 10:51:39 -0700984 struct ib_mad_send_wr_private *mad_send_wr;
Jack Morgensteinf36e1792006-03-03 21:54:13 -0800985 int pad, message_size, ret, size;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700986 void *buf;
Ira Weiny548ead12015-06-06 14:38:33 -0400987 size_t mad_size;
988 bool opa;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -0700989
Sean Hefty34816ad2005-10-25 10:51:39 -0700990 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
991 agent);
Ira Weiny548ead12015-06-06 14:38:33 -0400992
993 opa = rdma_cap_opa_mad(mad_agent->device, mad_agent->port_num);
994
995 if (opa && base_version == OPA_MGMT_BASE_VERSION)
996 mad_size = sizeof(struct opa_mad);
997 else
998 mad_size = sizeof(struct ib_mad);
999
1000 pad = get_pad_size(hdr_len, data_len, mad_size);
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001001 message_size = hdr_len + data_len + pad;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -07001002
Ira Weiny1471cb62014-08-08 19:00:56 -04001003 if (ib_mad_kernel_rmpp_agent(mad_agent)) {
Ira Weiny548ead12015-06-06 14:38:33 -04001004 if (!rmpp_active && message_size > mad_size)
Ira Weiny1471cb62014-08-08 19:00:56 -04001005 return ERR_PTR(-EINVAL);
1006 } else
Ira Weiny548ead12015-06-06 14:38:33 -04001007 if (rmpp_active || message_size > mad_size)
Ira Weiny1471cb62014-08-08 19:00:56 -04001008 return ERR_PTR(-EINVAL);
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001009
Ira Weiny548ead12015-06-06 14:38:33 -04001010 size = rmpp_active ? hdr_len : mad_size;
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001011 buf = kzalloc(sizeof *mad_send_wr + size, gfp_mask);
Hal Rosenstock824c8ae2005-07-27 11:45:23 -07001012 if (!buf)
1013 return ERR_PTR(-ENOMEM);
1014
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001015 mad_send_wr = buf + size;
1016 INIT_LIST_HEAD(&mad_send_wr->rmpp_list);
Sean Hefty34816ad2005-10-25 10:51:39 -07001017 mad_send_wr->send_buf.mad = buf;
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001018 mad_send_wr->send_buf.hdr_len = hdr_len;
1019 mad_send_wr->send_buf.data_len = data_len;
1020 mad_send_wr->pad = pad;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -07001021
Sean Hefty34816ad2005-10-25 10:51:39 -07001022 mad_send_wr->mad_agent_priv = mad_agent_priv;
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001023 mad_send_wr->sg_list[0].length = hdr_len;
Jason Gunthorpe4be90bc2015-07-30 17:22:16 -06001024 mad_send_wr->sg_list[0].lkey = mad_agent->qp->pd->local_dma_lkey;
Ira Weiny548ead12015-06-06 14:38:33 -04001025
1026 /* OPA MADs don't have to be the full 2048 bytes */
1027 if (opa && base_version == OPA_MGMT_BASE_VERSION &&
1028 data_len < mad_size - hdr_len)
1029 mad_send_wr->sg_list[1].length = data_len;
1030 else
1031 mad_send_wr->sg_list[1].length = mad_size - hdr_len;
1032
Jason Gunthorpe4be90bc2015-07-30 17:22:16 -06001033 mad_send_wr->sg_list[1].lkey = mad_agent->qp->pd->local_dma_lkey;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -07001034
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08001035 mad_send_wr->mad_list.cqe.done = ib_mad_send_done;
1036
1037 mad_send_wr->send_wr.wr.wr_cqe = &mad_send_wr->mad_list.cqe;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001038 mad_send_wr->send_wr.wr.sg_list = mad_send_wr->sg_list;
1039 mad_send_wr->send_wr.wr.num_sge = 2;
1040 mad_send_wr->send_wr.wr.opcode = IB_WR_SEND;
1041 mad_send_wr->send_wr.wr.send_flags = IB_SEND_SIGNALED;
1042 mad_send_wr->send_wr.remote_qpn = remote_qpn;
1043 mad_send_wr->send_wr.remote_qkey = IB_QP_SET_QKEY;
1044 mad_send_wr->send_wr.pkey_index = pkey_index;
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001045
1046 if (rmpp_active) {
Ira Weiny548ead12015-06-06 14:38:33 -04001047 ret = alloc_send_rmpp_list(mad_send_wr, mad_size, gfp_mask);
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001048 if (ret) {
1049 kfree(buf);
1050 return ERR_PTR(ret);
1051 }
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001052 }
1053
Sean Hefty34816ad2005-10-25 10:51:39 -07001054 mad_send_wr->send_buf.mad_agent = mad_agent;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -07001055 atomic_inc(&mad_agent_priv->refcount);
Sean Hefty34816ad2005-10-25 10:51:39 -07001056 return &mad_send_wr->send_buf;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -07001057}
1058EXPORT_SYMBOL(ib_create_send_mad);
1059
Hal Rosenstock618a3c02006-03-28 16:40:04 -08001060int ib_get_mad_data_offset(u8 mgmt_class)
1061{
1062 if (mgmt_class == IB_MGMT_CLASS_SUBN_ADM)
1063 return IB_MGMT_SA_HDR;
1064 else if ((mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
1065 (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
1066 (mgmt_class == IB_MGMT_CLASS_BIS))
1067 return IB_MGMT_DEVICE_HDR;
1068 else if ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
1069 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END))
1070 return IB_MGMT_VENDOR_HDR;
1071 else
1072 return IB_MGMT_MAD_HDR;
1073}
1074EXPORT_SYMBOL(ib_get_mad_data_offset);
1075
1076int ib_is_mad_class_rmpp(u8 mgmt_class)
1077{
1078 if ((mgmt_class == IB_MGMT_CLASS_SUBN_ADM) ||
1079 (mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
1080 (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
1081 (mgmt_class == IB_MGMT_CLASS_BIS) ||
1082 ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
1083 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END)))
1084 return 1;
1085 return 0;
1086}
1087EXPORT_SYMBOL(ib_is_mad_class_rmpp);
1088
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001089void *ib_get_rmpp_segment(struct ib_mad_send_buf *send_buf, int seg_num)
1090{
1091 struct ib_mad_send_wr_private *mad_send_wr;
1092 struct list_head *list;
1093
1094 mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
1095 send_buf);
1096 list = &mad_send_wr->cur_seg->list;
1097
1098 if (mad_send_wr->cur_seg->num < seg_num) {
1099 list_for_each_entry(mad_send_wr->cur_seg, list, list)
1100 if (mad_send_wr->cur_seg->num == seg_num)
1101 break;
1102 } else if (mad_send_wr->cur_seg->num > seg_num) {
1103 list_for_each_entry_reverse(mad_send_wr->cur_seg, list, list)
1104 if (mad_send_wr->cur_seg->num == seg_num)
1105 break;
1106 }
1107 return mad_send_wr->cur_seg->data;
1108}
1109EXPORT_SYMBOL(ib_get_rmpp_segment);
1110
1111static inline void *ib_get_payload(struct ib_mad_send_wr_private *mad_send_wr)
1112{
1113 if (mad_send_wr->send_buf.seg_count)
1114 return ib_get_rmpp_segment(&mad_send_wr->send_buf,
1115 mad_send_wr->seg_num);
1116 else
1117 return mad_send_wr->send_buf.mad +
1118 mad_send_wr->send_buf.hdr_len;
1119}
1120
Hal Rosenstock824c8ae2005-07-27 11:45:23 -07001121void ib_free_send_mad(struct ib_mad_send_buf *send_buf)
1122{
1123 struct ib_mad_agent_private *mad_agent_priv;
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001124 struct ib_mad_send_wr_private *mad_send_wr;
Hal Rosenstock824c8ae2005-07-27 11:45:23 -07001125
1126 mad_agent_priv = container_of(send_buf->mad_agent,
1127 struct ib_mad_agent_private, agent);
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001128 mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
1129 send_buf);
Hal Rosenstock824c8ae2005-07-27 11:45:23 -07001130
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001131 free_send_rmpp_list(mad_send_wr);
1132 kfree(send_buf->mad);
Sean Hefty1b52fa982006-05-12 14:57:52 -07001133 deref_mad_agent(mad_agent_priv);
Hal Rosenstock824c8ae2005-07-27 11:45:23 -07001134}
1135EXPORT_SYMBOL(ib_free_send_mad);
1136
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001137int ib_send_mad(struct ib_mad_send_wr_private *mad_send_wr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
1139 struct ib_mad_qp_info *qp_info;
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07001140 struct list_head *list;
Sean Hefty34816ad2005-10-25 10:51:39 -07001141 struct ib_send_wr *bad_send_wr;
1142 struct ib_mad_agent *mad_agent;
1143 struct ib_sge *sge;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 unsigned long flags;
1145 int ret;
1146
Hal Rosenstockf8197a42005-07-27 11:45:24 -07001147 /* Set WR ID to find mad_send_wr upon completion */
Hal Rosenstockd760ce82005-07-27 11:45:25 -07001148 qp_info = mad_send_wr->mad_agent_priv->qp_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 mad_send_wr->mad_list.mad_queue = &qp_info->send_queue;
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08001150 mad_send_wr->mad_list.cqe.done = ib_mad_send_done;
1151 mad_send_wr->send_wr.wr.wr_cqe = &mad_send_wr->mad_list.cqe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Sean Hefty34816ad2005-10-25 10:51:39 -07001153 mad_agent = mad_send_wr->send_buf.mad_agent;
1154 sge = mad_send_wr->sg_list;
Ralph Campbell15271062006-12-12 14:28:30 -08001155 sge[0].addr = ib_dma_map_single(mad_agent->device,
1156 mad_send_wr->send_buf.mad,
1157 sge[0].length,
1158 DMA_TO_DEVICE);
Yan Burman2c34e682014-03-11 14:41:47 +02001159 if (unlikely(ib_dma_mapping_error(mad_agent->device, sge[0].addr)))
1160 return -ENOMEM;
1161
Ralph Campbell15271062006-12-12 14:28:30 -08001162 mad_send_wr->header_mapping = sge[0].addr;
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001163
Ralph Campbell15271062006-12-12 14:28:30 -08001164 sge[1].addr = ib_dma_map_single(mad_agent->device,
1165 ib_get_payload(mad_send_wr),
1166 sge[1].length,
1167 DMA_TO_DEVICE);
Yan Burman2c34e682014-03-11 14:41:47 +02001168 if (unlikely(ib_dma_mapping_error(mad_agent->device, sge[1].addr))) {
1169 ib_dma_unmap_single(mad_agent->device,
1170 mad_send_wr->header_mapping,
1171 sge[0].length, DMA_TO_DEVICE);
1172 return -ENOMEM;
1173 }
Ralph Campbell15271062006-12-12 14:28:30 -08001174 mad_send_wr->payload_mapping = sge[1].addr;
Sean Hefty34816ad2005-10-25 10:51:39 -07001175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 spin_lock_irqsave(&qp_info->send_queue.lock, flags);
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07001177 if (qp_info->send_queue.count < qp_info->send_queue.max_active) {
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001178 ret = ib_post_send(mad_agent->qp, &mad_send_wr->send_wr.wr,
Sean Hefty34816ad2005-10-25 10:51:39 -07001179 &bad_send_wr);
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07001180 list = &qp_info->send_queue.list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 ret = 0;
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07001183 list = &qp_info->overflow_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07001185
1186 if (!ret) {
1187 qp_info->send_queue.count++;
1188 list_add_tail(&mad_send_wr->mad_list.list, list);
1189 }
1190 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001191 if (ret) {
Ralph Campbell15271062006-12-12 14:28:30 -08001192 ib_dma_unmap_single(mad_agent->device,
1193 mad_send_wr->header_mapping,
1194 sge[0].length, DMA_TO_DEVICE);
1195 ib_dma_unmap_single(mad_agent->device,
1196 mad_send_wr->payload_mapping,
1197 sge[1].length, DMA_TO_DEVICE);
Jack Morgensteinf36e1792006-03-03 21:54:13 -08001198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 return ret;
1200}
1201
1202/*
1203 * ib_post_send_mad - Posts MAD(s) to the send queue of the QP associated
1204 * with the registered client
1205 */
Sean Hefty34816ad2005-10-25 10:51:39 -07001206int ib_post_send_mad(struct ib_mad_send_buf *send_buf,
1207 struct ib_mad_send_buf **bad_send_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 struct ib_mad_agent_private *mad_agent_priv;
Sean Hefty34816ad2005-10-25 10:51:39 -07001210 struct ib_mad_send_buf *next_send_buf;
1211 struct ib_mad_send_wr_private *mad_send_wr;
1212 unsigned long flags;
1213 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 /* Walk list of send WRs and post each on send list */
Sean Hefty34816ad2005-10-25 10:51:39 -07001216 for (; send_buf; send_buf = next_send_buf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Sean Hefty34816ad2005-10-25 10:51:39 -07001218 mad_send_wr = container_of(send_buf,
1219 struct ib_mad_send_wr_private,
1220 send_buf);
1221 mad_agent_priv = mad_send_wr->mad_agent_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Sean Hefty34816ad2005-10-25 10:51:39 -07001223 if (!send_buf->mad_agent->send_handler ||
1224 (send_buf->timeout_ms &&
1225 !send_buf->mad_agent->recv_handler)) {
1226 ret = -EINVAL;
1227 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 }
1229
Hal Rosenstock618a3c02006-03-28 16:40:04 -08001230 if (!ib_is_mad_class_rmpp(((struct ib_mad_hdr *) send_buf->mad)->mgmt_class)) {
1231 if (mad_agent_priv->agent.rmpp_version) {
1232 ret = -EINVAL;
1233 goto error;
1234 }
1235 }
1236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 /*
1238 * Save pointer to next work request to post in case the
1239 * current one completes, and the user modifies the work
1240 * request associated with the completion
1241 */
Sean Hefty34816ad2005-10-25 10:51:39 -07001242 next_send_buf = send_buf->next;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001243 mad_send_wr->send_wr.ah = send_buf->ah;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Sean Hefty34816ad2005-10-25 10:51:39 -07001245 if (((struct ib_mad_hdr *) send_buf->mad)->mgmt_class ==
1246 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
1247 ret = handle_outgoing_dr_smp(mad_agent_priv,
1248 mad_send_wr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (ret < 0) /* error */
Sean Hefty34816ad2005-10-25 10:51:39 -07001250 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 else if (ret == 1) /* locally consumed */
Sean Hefty34816ad2005-10-25 10:51:39 -07001252 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
1254
Sean Hefty34816ad2005-10-25 10:51:39 -07001255 mad_send_wr->tid = ((struct ib_mad_hdr *) send_buf->mad)->tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 /* Timeout will be updated after send completes */
Sean Hefty34816ad2005-10-25 10:51:39 -07001257 mad_send_wr->timeout = msecs_to_jiffies(send_buf->timeout_ms);
Sean Hefty4fc8cd42007-11-27 00:11:04 -08001258 mad_send_wr->max_retries = send_buf->retries;
1259 mad_send_wr->retries_left = send_buf->retries;
1260 send_buf->retries = 0;
Sean Hefty34816ad2005-10-25 10:51:39 -07001261 /* Reference for work request to QP + response */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 mad_send_wr->refcount = 1 + (mad_send_wr->timeout > 0);
1263 mad_send_wr->status = IB_WC_SUCCESS;
1264
1265 /* Reference MAD agent until send completes */
1266 atomic_inc(&mad_agent_priv->refcount);
1267 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1268 list_add_tail(&mad_send_wr->agent_list,
1269 &mad_agent_priv->send_list);
1270 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1271
Ira Weiny1471cb62014-08-08 19:00:56 -04001272 if (ib_mad_kernel_rmpp_agent(&mad_agent_priv->agent)) {
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001273 ret = ib_send_rmpp_mad(mad_send_wr);
1274 if (ret >= 0 && ret != IB_RMPP_RESULT_CONSUMED)
1275 ret = ib_send_mad(mad_send_wr);
1276 } else
1277 ret = ib_send_mad(mad_send_wr);
1278 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 /* Fail send request */
1280 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1281 list_del(&mad_send_wr->agent_list);
1282 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1283 atomic_dec(&mad_agent_priv->refcount);
Sean Hefty34816ad2005-10-25 10:51:39 -07001284 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 }
1287 return 0;
Sean Hefty34816ad2005-10-25 10:51:39 -07001288error:
1289 if (bad_send_buf)
1290 *bad_send_buf = send_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 return ret;
1292}
1293EXPORT_SYMBOL(ib_post_send_mad);
1294
1295/*
1296 * ib_free_recv_mad - Returns data buffers used to receive
1297 * a MAD to the access layer
1298 */
1299void ib_free_recv_mad(struct ib_mad_recv_wc *mad_recv_wc)
1300{
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001301 struct ib_mad_recv_buf *mad_recv_buf, *temp_recv_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 struct ib_mad_private_header *mad_priv_hdr;
1303 struct ib_mad_private *priv;
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001304 struct list_head free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001306 INIT_LIST_HEAD(&free_list);
1307 list_splice_init(&mad_recv_wc->rmpp_list, &free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001309 list_for_each_entry_safe(mad_recv_buf, temp_recv_buf,
1310 &free_list, list) {
1311 mad_recv_wc = container_of(mad_recv_buf, struct ib_mad_recv_wc,
1312 recv_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 mad_priv_hdr = container_of(mad_recv_wc,
1314 struct ib_mad_private_header,
1315 recv_wc);
1316 priv = container_of(mad_priv_hdr, struct ib_mad_private,
1317 header);
Ira Weinyc9082e52015-06-06 14:38:30 -04001318 kfree(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320}
1321EXPORT_SYMBOL(ib_free_recv_mad);
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323struct ib_mad_agent *ib_redirect_mad_qp(struct ib_qp *qp,
1324 u8 rmpp_version,
1325 ib_mad_send_handler send_handler,
1326 ib_mad_recv_handler recv_handler,
1327 void *context)
1328{
1329 return ERR_PTR(-EINVAL); /* XXX: for now */
1330}
1331EXPORT_SYMBOL(ib_redirect_mad_qp);
1332
1333int ib_process_mad_wc(struct ib_mad_agent *mad_agent,
1334 struct ib_wc *wc)
1335{
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04001336 dev_err(&mad_agent->device->dev,
1337 "ib_process_mad_wc() not implemented yet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return 0;
1339}
1340EXPORT_SYMBOL(ib_process_mad_wc);
1341
1342static int method_in_use(struct ib_mad_mgmt_method_table **method,
1343 struct ib_mad_reg_req *mad_reg_req)
1344{
1345 int i;
1346
Akinobu Mita19b629f2010-03-05 13:41:38 -08001347 for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 if ((*method)->agent[i]) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04001349 pr_err("Method %d already in use\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return -EINVAL;
1351 }
1352 }
1353 return 0;
1354}
1355
1356static int allocate_method_table(struct ib_mad_mgmt_method_table **method)
1357{
1358 /* Allocate management method table */
Roland Dreierde6eb662005-11-02 07:23:14 -08001359 *method = kzalloc(sizeof **method, GFP_ATOMIC);
Leon Romanovsky27162432016-11-03 16:44:09 +02001360 return (*method) ? 0 : (-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
1362
1363/*
1364 * Check to see if there are any methods still in use
1365 */
1366static int check_method_table(struct ib_mad_mgmt_method_table *method)
1367{
1368 int i;
1369
1370 for (i = 0; i < IB_MGMT_MAX_METHODS; i++)
1371 if (method->agent[i])
1372 return 1;
1373 return 0;
1374}
1375
1376/*
1377 * Check to see if there are any method tables for this class still in use
1378 */
1379static int check_class_table(struct ib_mad_mgmt_class_table *class)
1380{
1381 int i;
1382
1383 for (i = 0; i < MAX_MGMT_CLASS; i++)
1384 if (class->method_table[i])
1385 return 1;
1386 return 0;
1387}
1388
1389static int check_vendor_class(struct ib_mad_mgmt_vendor_class *vendor_class)
1390{
1391 int i;
1392
1393 for (i = 0; i < MAX_MGMT_OUI; i++)
1394 if (vendor_class->method_table[i])
1395 return 1;
1396 return 0;
1397}
1398
1399static int find_vendor_oui(struct ib_mad_mgmt_vendor_class *vendor_class,
Ira Weinyd94bd262015-06-06 14:38:22 -04001400 const char *oui)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
1402 int i;
1403
1404 for (i = 0; i < MAX_MGMT_OUI; i++)
Roland Dreier3cd96562006-09-22 15:22:46 -07001405 /* Is there matching OUI for this vendor class ? */
1406 if (!memcmp(vendor_class->oui[i], oui, 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 return i;
1408
1409 return -1;
1410}
1411
1412static int check_vendor_table(struct ib_mad_mgmt_vendor_class_table *vendor)
1413{
1414 int i;
1415
1416 for (i = 0; i < MAX_MGMT_VENDOR_RANGE2; i++)
1417 if (vendor->vendor_class[i])
1418 return 1;
1419
1420 return 0;
1421}
1422
1423static void remove_methods_mad_agent(struct ib_mad_mgmt_method_table *method,
1424 struct ib_mad_agent_private *agent)
1425{
1426 int i;
1427
1428 /* Remove any methods for this mad agent */
1429 for (i = 0; i < IB_MGMT_MAX_METHODS; i++) {
1430 if (method->agent[i] == agent) {
1431 method->agent[i] = NULL;
1432 }
1433 }
1434}
1435
1436static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1437 struct ib_mad_agent_private *agent_priv,
1438 u8 mgmt_class)
1439{
1440 struct ib_mad_port_private *port_priv;
1441 struct ib_mad_mgmt_class_table **class;
1442 struct ib_mad_mgmt_method_table **method;
1443 int i, ret;
1444
1445 port_priv = agent_priv->qp_info->port_priv;
1446 class = &port_priv->version[mad_reg_req->mgmt_class_version].class;
1447 if (!*class) {
1448 /* Allocate management class table for "new" class version */
Roland Dreierde6eb662005-11-02 07:23:14 -08001449 *class = kzalloc(sizeof **class, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 if (!*class) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 ret = -ENOMEM;
1452 goto error1;
1453 }
Roland Dreierde6eb662005-11-02 07:23:14 -08001454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 /* Allocate method table for this management class */
1456 method = &(*class)->method_table[mgmt_class];
1457 if ((ret = allocate_method_table(method)))
1458 goto error2;
1459 } else {
1460 method = &(*class)->method_table[mgmt_class];
1461 if (!*method) {
1462 /* Allocate method table for this management class */
1463 if ((ret = allocate_method_table(method)))
1464 goto error1;
1465 }
1466 }
1467
1468 /* Now, make sure methods are not already in use */
1469 if (method_in_use(method, mad_reg_req))
1470 goto error3;
1471
1472 /* Finally, add in methods being registered */
Akinobu Mita19b629f2010-03-05 13:41:38 -08001473 for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 (*method)->agent[i] = agent_priv;
Akinobu Mita19b629f2010-03-05 13:41:38 -08001475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return 0;
1477
1478error3:
1479 /* Remove any methods for this mad agent */
1480 remove_methods_mad_agent(*method, agent_priv);
1481 /* Now, check to see if there are any methods in use */
1482 if (!check_method_table(*method)) {
1483 /* If not, release management method table */
1484 kfree(*method);
1485 *method = NULL;
1486 }
1487 ret = -EINVAL;
1488 goto error1;
1489error2:
1490 kfree(*class);
1491 *class = NULL;
1492error1:
1493 return ret;
1494}
1495
1496static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1497 struct ib_mad_agent_private *agent_priv)
1498{
1499 struct ib_mad_port_private *port_priv;
1500 struct ib_mad_mgmt_vendor_class_table **vendor_table;
1501 struct ib_mad_mgmt_vendor_class_table *vendor = NULL;
1502 struct ib_mad_mgmt_vendor_class *vendor_class = NULL;
1503 struct ib_mad_mgmt_method_table **method;
1504 int i, ret = -ENOMEM;
1505 u8 vclass;
1506
1507 /* "New" vendor (with OUI) class */
1508 vclass = vendor_class_index(mad_reg_req->mgmt_class);
1509 port_priv = agent_priv->qp_info->port_priv;
1510 vendor_table = &port_priv->version[
1511 mad_reg_req->mgmt_class_version].vendor;
1512 if (!*vendor_table) {
1513 /* Allocate mgmt vendor class table for "new" class version */
Roland Dreierde6eb662005-11-02 07:23:14 -08001514 vendor = kzalloc(sizeof *vendor, GFP_ATOMIC);
Leon Romanovsky27162432016-11-03 16:44:09 +02001515 if (!vendor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 goto error1;
Roland Dreierde6eb662005-11-02 07:23:14 -08001517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 *vendor_table = vendor;
1519 }
1520 if (!(*vendor_table)->vendor_class[vclass]) {
1521 /* Allocate table for this management vendor class */
Roland Dreierde6eb662005-11-02 07:23:14 -08001522 vendor_class = kzalloc(sizeof *vendor_class, GFP_ATOMIC);
Leon Romanovsky27162432016-11-03 16:44:09 +02001523 if (!vendor_class)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 goto error2;
Roland Dreierde6eb662005-11-02 07:23:14 -08001525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 (*vendor_table)->vendor_class[vclass] = vendor_class;
1527 }
1528 for (i = 0; i < MAX_MGMT_OUI; i++) {
1529 /* Is there matching OUI for this vendor class ? */
1530 if (!memcmp((*vendor_table)->vendor_class[vclass]->oui[i],
1531 mad_reg_req->oui, 3)) {
1532 method = &(*vendor_table)->vendor_class[
1533 vclass]->method_table[i];
1534 BUG_ON(!*method);
1535 goto check_in_use;
1536 }
1537 }
1538 for (i = 0; i < MAX_MGMT_OUI; i++) {
1539 /* OUI slot available ? */
1540 if (!is_vendor_oui((*vendor_table)->vendor_class[
1541 vclass]->oui[i])) {
1542 method = &(*vendor_table)->vendor_class[
1543 vclass]->method_table[i];
1544 BUG_ON(*method);
1545 /* Allocate method table for this OUI */
1546 if ((ret = allocate_method_table(method)))
1547 goto error3;
1548 memcpy((*vendor_table)->vendor_class[vclass]->oui[i],
1549 mad_reg_req->oui, 3);
1550 goto check_in_use;
1551 }
1552 }
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04001553 dev_err(&agent_priv->agent.device->dev, "All OUI slots in use\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 goto error3;
1555
1556check_in_use:
1557 /* Now, make sure methods are not already in use */
1558 if (method_in_use(method, mad_reg_req))
1559 goto error4;
1560
1561 /* Finally, add in methods being registered */
Akinobu Mita19b629f2010-03-05 13:41:38 -08001562 for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 (*method)->agent[i] = agent_priv;
Akinobu Mita19b629f2010-03-05 13:41:38 -08001564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 return 0;
1566
1567error4:
1568 /* Remove any methods for this mad agent */
1569 remove_methods_mad_agent(*method, agent_priv);
1570 /* Now, check to see if there are any methods in use */
1571 if (!check_method_table(*method)) {
1572 /* If not, release management method table */
1573 kfree(*method);
1574 *method = NULL;
1575 }
1576 ret = -EINVAL;
1577error3:
1578 if (vendor_class) {
1579 (*vendor_table)->vendor_class[vclass] = NULL;
1580 kfree(vendor_class);
1581 }
1582error2:
1583 if (vendor) {
1584 *vendor_table = NULL;
1585 kfree(vendor);
1586 }
1587error1:
1588 return ret;
1589}
1590
1591static void remove_mad_reg_req(struct ib_mad_agent_private *agent_priv)
1592{
1593 struct ib_mad_port_private *port_priv;
1594 struct ib_mad_mgmt_class_table *class;
1595 struct ib_mad_mgmt_method_table *method;
1596 struct ib_mad_mgmt_vendor_class_table *vendor;
1597 struct ib_mad_mgmt_vendor_class *vendor_class;
1598 int index;
1599 u8 mgmt_class;
1600
1601 /*
1602 * Was MAD registration request supplied
1603 * with original registration ?
1604 */
1605 if (!agent_priv->reg_req) {
1606 goto out;
1607 }
1608
1609 port_priv = agent_priv->qp_info->port_priv;
1610 mgmt_class = convert_mgmt_class(agent_priv->reg_req->mgmt_class);
1611 class = port_priv->version[
1612 agent_priv->reg_req->mgmt_class_version].class;
1613 if (!class)
1614 goto vendor_check;
1615
1616 method = class->method_table[mgmt_class];
1617 if (method) {
1618 /* Remove any methods for this mad agent */
1619 remove_methods_mad_agent(method, agent_priv);
1620 /* Now, check to see if there are any methods still in use */
1621 if (!check_method_table(method)) {
1622 /* If not, release management method table */
Bart Van Assche2190d102016-06-03 12:08:44 -07001623 kfree(method);
1624 class->method_table[mgmt_class] = NULL;
1625 /* Any management classes left ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 if (!check_class_table(class)) {
1627 /* If not, release management class table */
1628 kfree(class);
1629 port_priv->version[
1630 agent_priv->reg_req->
1631 mgmt_class_version].class = NULL;
1632 }
1633 }
1634 }
1635
1636vendor_check:
1637 if (!is_vendor_class(mgmt_class))
1638 goto out;
1639
1640 /* normalize mgmt_class to vendor range 2 */
1641 mgmt_class = vendor_class_index(agent_priv->reg_req->mgmt_class);
1642 vendor = port_priv->version[
1643 agent_priv->reg_req->mgmt_class_version].vendor;
1644
1645 if (!vendor)
1646 goto out;
1647
1648 vendor_class = vendor->vendor_class[mgmt_class];
1649 if (vendor_class) {
1650 index = find_vendor_oui(vendor_class, agent_priv->reg_req->oui);
1651 if (index < 0)
1652 goto out;
1653 method = vendor_class->method_table[index];
1654 if (method) {
1655 /* Remove any methods for this mad agent */
1656 remove_methods_mad_agent(method, agent_priv);
1657 /*
1658 * Now, check to see if there are
1659 * any methods still in use
1660 */
1661 if (!check_method_table(method)) {
1662 /* If not, release management method table */
1663 kfree(method);
1664 vendor_class->method_table[index] = NULL;
1665 memset(vendor_class->oui[index], 0, 3);
1666 /* Any OUIs left ? */
1667 if (!check_vendor_class(vendor_class)) {
1668 /* If not, release vendor class table */
1669 kfree(vendor_class);
1670 vendor->vendor_class[mgmt_class] = NULL;
1671 /* Any other vendor classes left ? */
1672 if (!check_vendor_table(vendor)) {
1673 kfree(vendor);
1674 port_priv->version[
1675 agent_priv->reg_req->
1676 mgmt_class_version].
1677 vendor = NULL;
1678 }
1679 }
1680 }
1681 }
1682 }
1683
1684out:
1685 return;
1686}
1687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688static struct ib_mad_agent_private *
1689find_mad_agent(struct ib_mad_port_private *port_priv,
Ira Weinyd94bd262015-06-06 14:38:22 -04001690 const struct ib_mad_hdr *mad_hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
1692 struct ib_mad_agent_private *mad_agent = NULL;
1693 unsigned long flags;
1694
1695 spin_lock_irqsave(&port_priv->reg_lock, flags);
Ira Weinyd94bd262015-06-06 14:38:22 -04001696 if (ib_response_mad(mad_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 u32 hi_tid;
1698 struct ib_mad_agent_private *entry;
1699
1700 /*
1701 * Routing is based on high 32 bits of transaction ID
1702 * of MAD.
1703 */
Ira Weinyd94bd262015-06-06 14:38:22 -04001704 hi_tid = be64_to_cpu(mad_hdr->tid) >> 32;
Sean Hefty34816ad2005-10-25 10:51:39 -07001705 list_for_each_entry(entry, &port_priv->agent_list, agent_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 if (entry->agent.hi_tid == hi_tid) {
1707 mad_agent = entry;
1708 break;
1709 }
1710 }
1711 } else {
1712 struct ib_mad_mgmt_class_table *class;
1713 struct ib_mad_mgmt_method_table *method;
1714 struct ib_mad_mgmt_vendor_class_table *vendor;
1715 struct ib_mad_mgmt_vendor_class *vendor_class;
Ira Weinyd94bd262015-06-06 14:38:22 -04001716 const struct ib_vendor_mad *vendor_mad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 int index;
1718
1719 /*
1720 * Routing is based on version, class, and method
1721 * For "newer" vendor MADs, also based on OUI
1722 */
Ira Weinyd94bd262015-06-06 14:38:22 -04001723 if (mad_hdr->class_version >= MAX_MGMT_VERSION)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 goto out;
Ira Weinyd94bd262015-06-06 14:38:22 -04001725 if (!is_vendor_class(mad_hdr->mgmt_class)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 class = port_priv->version[
Ira Weinyd94bd262015-06-06 14:38:22 -04001727 mad_hdr->class_version].class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 if (!class)
1729 goto out;
Ira Weinyd94bd262015-06-06 14:38:22 -04001730 if (convert_mgmt_class(mad_hdr->mgmt_class) >=
Hefty, Seanb7ab0b12011-10-06 09:33:05 -07001731 IB_MGMT_MAX_METHODS)
1732 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 method = class->method_table[convert_mgmt_class(
Ira Weinyd94bd262015-06-06 14:38:22 -04001734 mad_hdr->mgmt_class)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 if (method)
Ira Weinyd94bd262015-06-06 14:38:22 -04001736 mad_agent = method->agent[mad_hdr->method &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 ~IB_MGMT_METHOD_RESP];
1738 } else {
1739 vendor = port_priv->version[
Ira Weinyd94bd262015-06-06 14:38:22 -04001740 mad_hdr->class_version].vendor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 if (!vendor)
1742 goto out;
1743 vendor_class = vendor->vendor_class[vendor_class_index(
Ira Weinyd94bd262015-06-06 14:38:22 -04001744 mad_hdr->mgmt_class)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 if (!vendor_class)
1746 goto out;
1747 /* Find matching OUI */
Ira Weinyd94bd262015-06-06 14:38:22 -04001748 vendor_mad = (const struct ib_vendor_mad *)mad_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 index = find_vendor_oui(vendor_class, vendor_mad->oui);
1750 if (index == -1)
1751 goto out;
1752 method = vendor_class->method_table[index];
1753 if (method) {
Ira Weinyd94bd262015-06-06 14:38:22 -04001754 mad_agent = method->agent[mad_hdr->method &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 ~IB_MGMT_METHOD_RESP];
1756 }
1757 }
1758 }
1759
1760 if (mad_agent) {
1761 if (mad_agent->agent.recv_handler)
1762 atomic_inc(&mad_agent->refcount);
1763 else {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04001764 dev_notice(&port_priv->device->dev,
1765 "No receive handler for client %p on port %d\n",
1766 &mad_agent->agent, port_priv->port_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 mad_agent = NULL;
1768 }
1769 }
1770out:
1771 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
1772
1773 return mad_agent;
1774}
1775
Ira Weiny8e4349d2015-06-10 16:16:48 -04001776static int validate_mad(const struct ib_mad_hdr *mad_hdr,
1777 const struct ib_mad_qp_info *qp_info,
1778 bool opa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
1780 int valid = 0;
Ira Weiny8e4349d2015-06-10 16:16:48 -04001781 u32 qp_num = qp_info->qp->qp_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
1783 /* Make sure MAD base version is understood */
Ira Weiny8e4349d2015-06-10 16:16:48 -04001784 if (mad_hdr->base_version != IB_MGMT_BASE_VERSION &&
1785 (!opa || mad_hdr->base_version != OPA_MGMT_BASE_VERSION)) {
1786 pr_err("MAD received with unsupported base version %d %s\n",
1787 mad_hdr->base_version, opa ? "(opa)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 goto out;
1789 }
1790
1791 /* Filter SMI packets sent to other than QP0 */
Ira Weiny77f60832015-05-08 14:27:21 -04001792 if ((mad_hdr->mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
1793 (mad_hdr->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 if (qp_num == 0)
1795 valid = 1;
1796 } else {
Hal Rosenstock53370882015-11-13 15:22:22 -05001797 /* CM attributes other than ClassPortInfo only use Send method */
1798 if ((mad_hdr->mgmt_class == IB_MGMT_CLASS_CM) &&
1799 (mad_hdr->attr_id != IB_MGMT_CLASSPORTINFO_ATTR_ID) &&
1800 (mad_hdr->method != IB_MGMT_METHOD_SEND))
1801 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 /* Filter GSI packets sent to QP0 */
1803 if (qp_num != 0)
1804 valid = 1;
1805 }
1806
1807out:
1808 return valid;
1809}
1810
Ira Weinyf766c582015-05-08 14:27:24 -04001811static int is_rmpp_data_mad(const struct ib_mad_agent_private *mad_agent_priv,
1812 const struct ib_mad_hdr *mad_hdr)
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001813{
1814 struct ib_rmpp_mad *rmpp_mad;
1815
1816 rmpp_mad = (struct ib_rmpp_mad *)mad_hdr;
1817 return !mad_agent_priv->agent.rmpp_version ||
Ira Weiny1471cb62014-08-08 19:00:56 -04001818 !ib_mad_kernel_rmpp_agent(&mad_agent_priv->agent) ||
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001819 !(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
1820 IB_MGMT_RMPP_FLAG_ACTIVE) ||
1821 (rmpp_mad->rmpp_hdr.rmpp_type == IB_MGMT_RMPP_TYPE_DATA);
1822}
1823
Ira Weiny8bf4b302015-05-08 14:27:23 -04001824static inline int rcv_has_same_class(const struct ib_mad_send_wr_private *wr,
1825 const struct ib_mad_recv_wc *rwc)
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001826{
Ira Weiny8bf4b302015-05-08 14:27:23 -04001827 return ((struct ib_mad_hdr *)(wr->send_buf.mad))->mgmt_class ==
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001828 rwc->recv_buf.mad->mad_hdr.mgmt_class;
1829}
1830
Ira Weinyf766c582015-05-08 14:27:24 -04001831static inline int rcv_has_same_gid(const struct ib_mad_agent_private *mad_agent_priv,
1832 const struct ib_mad_send_wr_private *wr,
1833 const struct ib_mad_recv_wc *rwc )
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001834{
1835 struct ib_ah_attr attr;
1836 u8 send_resp, rcv_resp;
Jack Morgenstein9874e742006-06-17 20:37:34 -07001837 union ib_gid sgid;
1838 struct ib_device *device = mad_agent_priv->agent.device;
1839 u8 port_num = mad_agent_priv->agent.port_num;
1840 u8 lmc;
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001841
Ira Weiny96909302015-05-08 14:27:22 -04001842 send_resp = ib_response_mad((struct ib_mad_hdr *)wr->send_buf.mad);
1843 rcv_resp = ib_response_mad(&rwc->recv_buf.mad->mad_hdr);
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001844
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001845 if (send_resp == rcv_resp)
1846 /* both requests, or both responses. GIDs different */
1847 return 0;
1848
1849 if (ib_query_ah(wr->send_buf.ah, &attr))
1850 /* Assume not equal, to avoid false positives. */
1851 return 0;
1852
Jack Morgenstein9874e742006-06-17 20:37:34 -07001853 if (!!(attr.ah_flags & IB_AH_GRH) !=
1854 !!(rwc->wc->wc_flags & IB_WC_GRH))
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001855 /* one has GID, other does not. Assume different */
1856 return 0;
Jack Morgenstein9874e742006-06-17 20:37:34 -07001857
1858 if (!send_resp && rcv_resp) {
1859 /* is request/response. */
1860 if (!(attr.ah_flags & IB_AH_GRH)) {
1861 if (ib_get_cached_lmc(device, port_num, &lmc))
1862 return 0;
1863 return (!lmc || !((attr.src_path_bits ^
1864 rwc->wc->dlid_path_bits) &
1865 ((1 << lmc) - 1)));
1866 } else {
1867 if (ib_get_cached_gid(device, port_num,
Matan Barak55ee3ab2015-10-15 18:38:45 +03001868 attr.grh.sgid_index, &sgid, NULL))
Jack Morgenstein9874e742006-06-17 20:37:34 -07001869 return 0;
1870 return !memcmp(sgid.raw, rwc->recv_buf.grh->dgid.raw,
1871 16);
1872 }
1873 }
1874
1875 if (!(attr.ah_flags & IB_AH_GRH))
1876 return attr.dlid == rwc->wc->slid;
1877 else
1878 return !memcmp(attr.grh.dgid.raw, rwc->recv_buf.grh->sgid.raw,
1879 16);
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001880}
Jack Morgenstein9874e742006-06-17 20:37:34 -07001881
1882static inline int is_direct(u8 class)
1883{
1884 return (class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE);
1885}
1886
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001887struct ib_mad_send_wr_private*
Ira Weinyf766c582015-05-08 14:27:24 -04001888ib_find_send_mad(const struct ib_mad_agent_private *mad_agent_priv,
1889 const struct ib_mad_recv_wc *wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890{
Jack Morgenstein9874e742006-06-17 20:37:34 -07001891 struct ib_mad_send_wr_private *wr;
Ira Weiny83a1d222015-06-06 14:38:23 -04001892 const struct ib_mad_hdr *mad_hdr;
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001893
Ira Weiny83a1d222015-06-06 14:38:23 -04001894 mad_hdr = &wc->recv_buf.mad->mad_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Jack Morgenstein9874e742006-06-17 20:37:34 -07001896 list_for_each_entry(wr, &mad_agent_priv->wait_list, agent_list) {
Ira Weiny83a1d222015-06-06 14:38:23 -04001897 if ((wr->tid == mad_hdr->tid) &&
Jack Morgenstein9874e742006-06-17 20:37:34 -07001898 rcv_has_same_class(wr, wc) &&
1899 /*
1900 * Don't check GID for direct routed MADs.
1901 * These might have permissive LIDs.
1902 */
Ira Weiny83a1d222015-06-06 14:38:23 -04001903 (is_direct(mad_hdr->mgmt_class) ||
Jack Morgenstein9874e742006-06-17 20:37:34 -07001904 rcv_has_same_gid(mad_agent_priv, wr, wc)))
Roland Dreier39798692006-11-13 09:38:07 -08001905 return (wr->status == IB_WC_SUCCESS) ? wr : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 }
1907
1908 /*
1909 * It's possible to receive the response before we've
1910 * been notified that the send has completed
1911 */
Jack Morgenstein9874e742006-06-17 20:37:34 -07001912 list_for_each_entry(wr, &mad_agent_priv->send_list, agent_list) {
Ira Weinyc597eee2015-05-08 13:10:03 -04001913 if (is_rmpp_data_mad(mad_agent_priv, wr->send_buf.mad) &&
Ira Weiny83a1d222015-06-06 14:38:23 -04001914 wr->tid == mad_hdr->tid &&
Jack Morgenstein9874e742006-06-17 20:37:34 -07001915 wr->timeout &&
1916 rcv_has_same_class(wr, wc) &&
1917 /*
1918 * Don't check GID for direct routed MADs.
1919 * These might have permissive LIDs.
1920 */
Ira Weiny83a1d222015-06-06 14:38:23 -04001921 (is_direct(mad_hdr->mgmt_class) ||
Jack Morgenstein9874e742006-06-17 20:37:34 -07001922 rcv_has_same_gid(mad_agent_priv, wr, wc)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 /* Verify request has not been canceled */
Jack Morgenstein9874e742006-06-17 20:37:34 -07001924 return (wr->status == IB_WC_SUCCESS) ? wr : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 }
1926 return NULL;
1927}
1928
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001929void ib_mark_mad_done(struct ib_mad_send_wr_private *mad_send_wr)
Hal Rosenstock6a0c4352005-07-27 11:45:26 -07001930{
1931 mad_send_wr->timeout = 0;
Akinobu Mita179e0912006-06-26 00:24:41 -07001932 if (mad_send_wr->refcount == 1)
1933 list_move_tail(&mad_send_wr->agent_list,
Hal Rosenstock6a0c4352005-07-27 11:45:26 -07001934 &mad_send_wr->mad_agent_priv->done_list);
Hal Rosenstock6a0c4352005-07-27 11:45:26 -07001935}
1936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937static void ib_mad_complete_recv(struct ib_mad_agent_private *mad_agent_priv,
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07001938 struct ib_mad_recv_wc *mad_recv_wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
1940 struct ib_mad_send_wr_private *mad_send_wr;
1941 struct ib_mad_send_wc mad_send_wc;
1942 unsigned long flags;
1943
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001944 INIT_LIST_HEAD(&mad_recv_wc->rmpp_list);
1945 list_add(&mad_recv_wc->recv_buf.list, &mad_recv_wc->rmpp_list);
Ira Weiny1471cb62014-08-08 19:00:56 -04001946 if (ib_mad_kernel_rmpp_agent(&mad_agent_priv->agent)) {
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001947 mad_recv_wc = ib_process_rmpp_recv_wc(mad_agent_priv,
1948 mad_recv_wc);
1949 if (!mad_recv_wc) {
Sean Hefty1b52fa982006-05-12 14:57:52 -07001950 deref_mad_agent(mad_agent_priv);
Hal Rosenstockfa619a72005-07-27 11:45:37 -07001951 return;
1952 }
1953 }
1954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 /* Complete corresponding request */
Ira Weiny96909302015-05-08 14:27:22 -04001956 if (ib_response_mad(&mad_recv_wc->recv_buf.mad->mad_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 spin_lock_irqsave(&mad_agent_priv->lock, flags);
Jack Morgensteinfa9656b2006-03-28 16:39:07 -08001958 mad_send_wr = ib_find_send_mad(mad_agent_priv, mad_recv_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 if (!mad_send_wr) {
1960 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
Ira Weiny1471cb62014-08-08 19:00:56 -04001961 if (!ib_mad_kernel_rmpp_agent(&mad_agent_priv->agent)
1962 && ib_is_mad_class_rmpp(mad_recv_wc->recv_buf.mad->mad_hdr.mgmt_class)
1963 && (ib_get_rmpp_flags(&((struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad)->rmpp_hdr)
1964 & IB_MGMT_RMPP_FLAG_ACTIVE)) {
1965 /* user rmpp is in effect
1966 * and this is an active RMPP MAD
1967 */
Christoph Hellwigca281262016-01-04 14:15:58 +01001968 mad_agent_priv->agent.recv_handler(
1969 &mad_agent_priv->agent, NULL,
1970 mad_recv_wc);
Ira Weiny1471cb62014-08-08 19:00:56 -04001971 atomic_dec(&mad_agent_priv->refcount);
1972 } else {
1973 /* not user rmpp, revert to normal behavior and
1974 * drop the mad */
1975 ib_free_recv_mad(mad_recv_wc);
1976 deref_mad_agent(mad_agent_priv);
1977 return;
1978 }
1979 } else {
1980 ib_mark_mad_done(mad_send_wr);
1981 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1982
1983 /* Defined behavior is to complete response before request */
Christoph Hellwigca281262016-01-04 14:15:58 +01001984 mad_agent_priv->agent.recv_handler(
1985 &mad_agent_priv->agent,
1986 &mad_send_wr->send_buf,
1987 mad_recv_wc);
Ira Weiny1471cb62014-08-08 19:00:56 -04001988 atomic_dec(&mad_agent_priv->refcount);
1989
1990 mad_send_wc.status = IB_WC_SUCCESS;
1991 mad_send_wc.vendor_err = 0;
1992 mad_send_wc.send_buf = &mad_send_wr->send_buf;
1993 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 } else {
Christoph Hellwigca281262016-01-04 14:15:58 +01001996 mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent, NULL,
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07001997 mad_recv_wc);
Sean Hefty1b52fa982006-05-12 14:57:52 -07001998 deref_mad_agent(mad_agent_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 }
2000}
2001
Ira Weinye11ae8a2015-06-06 14:38:24 -04002002static enum smi_action handle_ib_smi(const struct ib_mad_port_private *port_priv,
2003 const struct ib_mad_qp_info *qp_info,
2004 const struct ib_wc *wc,
2005 int port_num,
2006 struct ib_mad_private *recv,
2007 struct ib_mad_private *response)
2008{
2009 enum smi_forward_action retsmi;
Ira Weinyc9082e52015-06-06 14:38:30 -04002010 struct ib_smp *smp = (struct ib_smp *)recv->mad;
Ira Weinye11ae8a2015-06-06 14:38:24 -04002011
Ira Weinyc9082e52015-06-06 14:38:30 -04002012 if (smi_handle_dr_smp_recv(smp,
Hal Rosenstock41390322015-06-29 09:57:00 -04002013 rdma_cap_ib_switch(port_priv->device),
Ira Weinye11ae8a2015-06-06 14:38:24 -04002014 port_num,
2015 port_priv->device->phys_port_cnt) ==
2016 IB_SMI_DISCARD)
2017 return IB_SMI_DISCARD;
2018
Ira Weinyc9082e52015-06-06 14:38:30 -04002019 retsmi = smi_check_forward_dr_smp(smp);
Ira Weinye11ae8a2015-06-06 14:38:24 -04002020 if (retsmi == IB_SMI_LOCAL)
2021 return IB_SMI_HANDLE;
2022
2023 if (retsmi == IB_SMI_SEND) { /* don't forward */
Ira Weinyc9082e52015-06-06 14:38:30 -04002024 if (smi_handle_dr_smp_send(smp,
Hal Rosenstock41390322015-06-29 09:57:00 -04002025 rdma_cap_ib_switch(port_priv->device),
Ira Weinye11ae8a2015-06-06 14:38:24 -04002026 port_num) == IB_SMI_DISCARD)
2027 return IB_SMI_DISCARD;
2028
Ira Weinyc9082e52015-06-06 14:38:30 -04002029 if (smi_check_local_smp(smp, port_priv->device) == IB_SMI_DISCARD)
Ira Weinye11ae8a2015-06-06 14:38:24 -04002030 return IB_SMI_DISCARD;
Hal Rosenstock41390322015-06-29 09:57:00 -04002031 } else if (rdma_cap_ib_switch(port_priv->device)) {
Ira Weinye11ae8a2015-06-06 14:38:24 -04002032 /* forward case for switches */
Ira Weinyc9082e52015-06-06 14:38:30 -04002033 memcpy(response, recv, mad_priv_size(response));
Ira Weinye11ae8a2015-06-06 14:38:24 -04002034 response->header.recv_wc.wc = &response->header.wc;
Ira Weinyc9082e52015-06-06 14:38:30 -04002035 response->header.recv_wc.recv_buf.mad = (struct ib_mad *)response->mad;
Ira Weinye11ae8a2015-06-06 14:38:24 -04002036 response->header.recv_wc.recv_buf.grh = &response->grh;
2037
Ira Weinyc9082e52015-06-06 14:38:30 -04002038 agent_send_response((const struct ib_mad_hdr *)response->mad,
Ira Weinye11ae8a2015-06-06 14:38:24 -04002039 &response->grh, wc,
2040 port_priv->device,
Ira Weinyc9082e52015-06-06 14:38:30 -04002041 smi_get_fwd_port(smp),
2042 qp_info->qp->qp_num,
Ira Weiny8e4349d2015-06-10 16:16:48 -04002043 response->mad_size,
2044 false);
Ira Weinye11ae8a2015-06-06 14:38:24 -04002045
2046 return IB_SMI_DISCARD;
2047 }
2048 return IB_SMI_HANDLE;
2049}
2050
Ira Weinyc9082e52015-06-06 14:38:30 -04002051static bool generate_unmatched_resp(const struct ib_mad_private *recv,
Ira Weiny8e4349d2015-06-10 16:16:48 -04002052 struct ib_mad_private *response,
2053 size_t *resp_len, bool opa)
Swapna Thete0b307042012-02-25 17:47:32 -08002054{
Ira Weinyc9082e52015-06-06 14:38:30 -04002055 const struct ib_mad_hdr *recv_hdr = (const struct ib_mad_hdr *)recv->mad;
2056 struct ib_mad_hdr *resp_hdr = (struct ib_mad_hdr *)response->mad;
2057
2058 if (recv_hdr->method == IB_MGMT_METHOD_GET ||
2059 recv_hdr->method == IB_MGMT_METHOD_SET) {
2060 memcpy(response, recv, mad_priv_size(response));
Swapna Thete0b307042012-02-25 17:47:32 -08002061 response->header.recv_wc.wc = &response->header.wc;
Ira Weinyc9082e52015-06-06 14:38:30 -04002062 response->header.recv_wc.recv_buf.mad = (struct ib_mad *)response->mad;
Swapna Thete0b307042012-02-25 17:47:32 -08002063 response->header.recv_wc.recv_buf.grh = &response->grh;
Ira Weinyc9082e52015-06-06 14:38:30 -04002064 resp_hdr->method = IB_MGMT_METHOD_GET_RESP;
2065 resp_hdr->status = cpu_to_be16(IB_MGMT_MAD_STATUS_UNSUPPORTED_METHOD_ATTRIB);
2066 if (recv_hdr->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
2067 resp_hdr->status |= IB_SMP_DIRECTION;
Swapna Thete0b307042012-02-25 17:47:32 -08002068
Ira Weiny8e4349d2015-06-10 16:16:48 -04002069 if (opa && recv_hdr->base_version == OPA_MGMT_BASE_VERSION) {
2070 if (recv_hdr->mgmt_class ==
2071 IB_MGMT_CLASS_SUBN_LID_ROUTED ||
2072 recv_hdr->mgmt_class ==
2073 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
2074 *resp_len = opa_get_smp_header_size(
2075 (struct opa_smp *)recv->mad);
2076 else
2077 *resp_len = sizeof(struct ib_mad_hdr);
2078 }
2079
Swapna Thete0b307042012-02-25 17:47:32 -08002080 return true;
2081 } else {
2082 return false;
2083 }
2084}
Ira Weiny8e4349d2015-06-10 16:16:48 -04002085
2086static enum smi_action
2087handle_opa_smi(struct ib_mad_port_private *port_priv,
2088 struct ib_mad_qp_info *qp_info,
2089 struct ib_wc *wc,
2090 int port_num,
2091 struct ib_mad_private *recv,
2092 struct ib_mad_private *response)
2093{
2094 enum smi_forward_action retsmi;
2095 struct opa_smp *smp = (struct opa_smp *)recv->mad;
2096
2097 if (opa_smi_handle_dr_smp_recv(smp,
Hal Rosenstock41390322015-06-29 09:57:00 -04002098 rdma_cap_ib_switch(port_priv->device),
Ira Weiny8e4349d2015-06-10 16:16:48 -04002099 port_num,
2100 port_priv->device->phys_port_cnt) ==
2101 IB_SMI_DISCARD)
2102 return IB_SMI_DISCARD;
2103
2104 retsmi = opa_smi_check_forward_dr_smp(smp);
2105 if (retsmi == IB_SMI_LOCAL)
2106 return IB_SMI_HANDLE;
2107
2108 if (retsmi == IB_SMI_SEND) { /* don't forward */
2109 if (opa_smi_handle_dr_smp_send(smp,
Hal Rosenstock41390322015-06-29 09:57:00 -04002110 rdma_cap_ib_switch(port_priv->device),
Ira Weiny8e4349d2015-06-10 16:16:48 -04002111 port_num) == IB_SMI_DISCARD)
2112 return IB_SMI_DISCARD;
2113
2114 if (opa_smi_check_local_smp(smp, port_priv->device) ==
2115 IB_SMI_DISCARD)
2116 return IB_SMI_DISCARD;
2117
Hal Rosenstock41390322015-06-29 09:57:00 -04002118 } else if (rdma_cap_ib_switch(port_priv->device)) {
Ira Weiny8e4349d2015-06-10 16:16:48 -04002119 /* forward case for switches */
2120 memcpy(response, recv, mad_priv_size(response));
2121 response->header.recv_wc.wc = &response->header.wc;
2122 response->header.recv_wc.recv_buf.opa_mad =
2123 (struct opa_mad *)response->mad;
2124 response->header.recv_wc.recv_buf.grh = &response->grh;
2125
2126 agent_send_response((const struct ib_mad_hdr *)response->mad,
2127 &response->grh, wc,
2128 port_priv->device,
2129 opa_smi_get_fwd_port(smp),
2130 qp_info->qp->qp_num,
2131 recv->header.wc.byte_len,
2132 true);
2133
2134 return IB_SMI_DISCARD;
2135 }
2136
2137 return IB_SMI_HANDLE;
2138}
2139
2140static enum smi_action
2141handle_smi(struct ib_mad_port_private *port_priv,
2142 struct ib_mad_qp_info *qp_info,
2143 struct ib_wc *wc,
2144 int port_num,
2145 struct ib_mad_private *recv,
2146 struct ib_mad_private *response,
2147 bool opa)
2148{
2149 struct ib_mad_hdr *mad_hdr = (struct ib_mad_hdr *)recv->mad;
2150
2151 if (opa && mad_hdr->base_version == OPA_MGMT_BASE_VERSION &&
2152 mad_hdr->class_version == OPA_SMI_CLASS_VERSION)
2153 return handle_opa_smi(port_priv, qp_info, wc, port_num, recv,
2154 response);
2155
2156 return handle_ib_smi(port_priv, qp_info, wc, port_num, recv, response);
2157}
2158
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08002159static void ib_mad_recv_done(struct ib_cq *cq, struct ib_wc *wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08002161 struct ib_mad_port_private *port_priv = cq->cq_context;
2162 struct ib_mad_list_head *mad_list =
2163 container_of(wc->wr_cqe, struct ib_mad_list_head, cqe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 struct ib_mad_qp_info *qp_info;
2165 struct ib_mad_private_header *mad_priv_hdr;
Hal Rosenstock445d6802007-08-03 10:45:17 -07002166 struct ib_mad_private *recv, *response = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 struct ib_mad_agent_private *mad_agent;
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04002168 int port_num;
Jack Morgensteina9e74322012-04-24 16:08:57 -07002169 int ret = IB_MAD_RESULT_SUCCESS;
Ira Weiny4cd7c942015-06-06 14:38:31 -04002170 size_t mad_size;
2171 u16 resp_mad_pkey_index = 0;
Ira Weiny8e4349d2015-06-10 16:16:48 -04002172 bool opa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08002174 if (list_empty_careful(&port_priv->port_list))
2175 return;
2176
2177 if (wc->status != IB_WC_SUCCESS) {
2178 /*
2179 * Receive errors indicate that the QP has entered the error
2180 * state - error handling/shutdown code will cleanup
2181 */
2182 return;
2183 }
2184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 qp_info = mad_list->mad_queue->qp_info;
2186 dequeue_mad(mad_list);
2187
Ira Weiny8e4349d2015-06-10 16:16:48 -04002188 opa = rdma_cap_opa_mad(qp_info->port_priv->device,
2189 qp_info->port_priv->port_num);
2190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 mad_priv_hdr = container_of(mad_list, struct ib_mad_private_header,
2192 mad_list);
2193 recv = container_of(mad_priv_hdr, struct ib_mad_private, header);
Ralph Campbell15271062006-12-12 14:28:30 -08002194 ib_dma_unmap_single(port_priv->device,
2195 recv->header.mapping,
Ira Weinyc9082e52015-06-06 14:38:30 -04002196 mad_priv_dma_size(recv),
Ralph Campbell15271062006-12-12 14:28:30 -08002197 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
2199 /* Setup MAD receive work completion from "normal" work completion */
Sean Hefty24239af2005-04-16 15:26:08 -07002200 recv->header.wc = *wc;
2201 recv->header.recv_wc.wc = &recv->header.wc;
Ira Weiny8e4349d2015-06-10 16:16:48 -04002202
2203 if (opa && ((struct ib_mad_hdr *)(recv->mad))->base_version == OPA_MGMT_BASE_VERSION) {
2204 recv->header.recv_wc.mad_len = wc->byte_len - sizeof(struct ib_grh);
2205 recv->header.recv_wc.mad_seg_size = sizeof(struct opa_mad);
2206 } else {
2207 recv->header.recv_wc.mad_len = sizeof(struct ib_mad);
2208 recv->header.recv_wc.mad_seg_size = sizeof(struct ib_mad);
2209 }
2210
Ira Weinyc9082e52015-06-06 14:38:30 -04002211 recv->header.recv_wc.recv_buf.mad = (struct ib_mad *)recv->mad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 recv->header.recv_wc.recv_buf.grh = &recv->grh;
2213
2214 if (atomic_read(&qp_info->snoop_count))
2215 snoop_recv(qp_info, &recv->header.recv_wc, IB_MAD_SNOOP_RECVS);
2216
2217 /* Validate MAD */
Ira Weiny8e4349d2015-06-10 16:16:48 -04002218 if (!validate_mad((const struct ib_mad_hdr *)recv->mad, qp_info, opa))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 goto out;
2220
Ira Weiny4cd7c942015-06-06 14:38:31 -04002221 mad_size = recv->mad_size;
2222 response = alloc_mad_private(mad_size, GFP_KERNEL);
Leon Romanovsky27162432016-11-03 16:44:09 +02002223 if (!response)
Hal Rosenstock445d6802007-08-03 10:45:17 -07002224 goto out;
Hal Rosenstock445d6802007-08-03 10:45:17 -07002225
Hal Rosenstock41390322015-06-29 09:57:00 -04002226 if (rdma_cap_ib_switch(port_priv->device))
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04002227 port_num = wc->port_num;
2228 else
2229 port_num = port_priv->port_num;
2230
Ira Weinyc9082e52015-06-06 14:38:30 -04002231 if (((struct ib_mad_hdr *)recv->mad)->mgmt_class ==
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
Ira Weiny8e4349d2015-06-10 16:16:48 -04002233 if (handle_smi(port_priv, qp_info, wc, port_num, recv,
2234 response, opa)
Ira Weinye11ae8a2015-06-06 14:38:24 -04002235 == IB_SMI_DISCARD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 }
2238
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 /* Give driver "right of first refusal" on incoming MAD */
2240 if (port_priv->device->process_mad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 ret = port_priv->device->process_mad(port_priv->device, 0,
2242 port_priv->port_num,
2243 wc, &recv->grh,
Ira Weiny4cd7c942015-06-06 14:38:31 -04002244 (const struct ib_mad_hdr *)recv->mad,
2245 recv->mad_size,
2246 (struct ib_mad_hdr *)response->mad,
2247 &mad_size, &resp_mad_pkey_index);
Ira Weiny8e4349d2015-06-10 16:16:48 -04002248
2249 if (opa)
2250 wc->pkey_index = resp_mad_pkey_index;
2251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 if (ret & IB_MAD_RESULT_SUCCESS) {
2253 if (ret & IB_MAD_RESULT_CONSUMED)
2254 goto out;
2255 if (ret & IB_MAD_RESULT_REPLY) {
Ira Weinyc9082e52015-06-06 14:38:30 -04002256 agent_send_response((const struct ib_mad_hdr *)response->mad,
Sean Hefty34816ad2005-10-25 10:51:39 -07002257 &recv->grh, wc,
2258 port_priv->device,
Hal Rosenstock1bae4db2007-05-14 17:21:52 -04002259 port_num,
Ira Weinyc9082e52015-06-06 14:38:30 -04002260 qp_info->qp->qp_num,
Ira Weiny8e4349d2015-06-10 16:16:48 -04002261 mad_size, opa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 goto out;
2263 }
2264 }
2265 }
2266
Ira Weinyc9082e52015-06-06 14:38:30 -04002267 mad_agent = find_mad_agent(port_priv, (const struct ib_mad_hdr *)recv->mad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 if (mad_agent) {
Hal Rosenstock4a0754f2005-07-27 11:45:24 -07002269 ib_mad_complete_recv(mad_agent, &recv->header.recv_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 /*
2271 * recv is freed up in error cases in ib_mad_complete_recv
2272 * or via recv_handler in ib_mad_complete_recv()
2273 */
2274 recv = NULL;
Jack Morgensteina9e74322012-04-24 16:08:57 -07002275 } else if ((ret & IB_MAD_RESULT_SUCCESS) &&
Ira Weiny8e4349d2015-06-10 16:16:48 -04002276 generate_unmatched_resp(recv, response, &mad_size, opa)) {
Ira Weinyc9082e52015-06-06 14:38:30 -04002277 agent_send_response((const struct ib_mad_hdr *)response->mad, &recv->grh, wc,
2278 port_priv->device, port_num,
Ira Weiny8e4349d2015-06-10 16:16:48 -04002279 qp_info->qp->qp_num, mad_size, opa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 }
2281
2282out:
2283 /* Post another receive request for this QP */
2284 if (response) {
2285 ib_mad_post_receive_mads(qp_info, response);
Ira Weinyc9082e52015-06-06 14:38:30 -04002286 kfree(recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 } else
2288 ib_mad_post_receive_mads(qp_info, recv);
2289}
2290
2291static void adjust_timeout(struct ib_mad_agent_private *mad_agent_priv)
2292{
2293 struct ib_mad_send_wr_private *mad_send_wr;
2294 unsigned long delay;
2295
2296 if (list_empty(&mad_agent_priv->wait_list)) {
Tejun Heo136b5722012-08-21 13:18:24 -07002297 cancel_delayed_work(&mad_agent_priv->timed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 } else {
2299 mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
2300 struct ib_mad_send_wr_private,
2301 agent_list);
2302
2303 if (time_after(mad_agent_priv->timeout,
2304 mad_send_wr->timeout)) {
2305 mad_agent_priv->timeout = mad_send_wr->timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 delay = mad_send_wr->timeout - jiffies;
2307 if ((long)delay <= 0)
2308 delay = 1;
Tejun Heoe7c2f962012-08-21 13:18:24 -07002309 mod_delayed_work(mad_agent_priv->qp_info->port_priv->wq,
2310 &mad_agent_priv->timed_work, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 }
2312 }
2313}
2314
Hal Rosenstockd760ce82005-07-27 11:45:25 -07002315static void wait_for_response(struct ib_mad_send_wr_private *mad_send_wr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316{
Hal Rosenstockd760ce82005-07-27 11:45:25 -07002317 struct ib_mad_agent_private *mad_agent_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 struct ib_mad_send_wr_private *temp_mad_send_wr;
2319 struct list_head *list_item;
2320 unsigned long delay;
2321
Hal Rosenstockd760ce82005-07-27 11:45:25 -07002322 mad_agent_priv = mad_send_wr->mad_agent_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 list_del(&mad_send_wr->agent_list);
2324
2325 delay = mad_send_wr->timeout;
2326 mad_send_wr->timeout += jiffies;
2327
Hal Rosenstock29bb33d2005-07-27 11:45:32 -07002328 if (delay) {
2329 list_for_each_prev(list_item, &mad_agent_priv->wait_list) {
2330 temp_mad_send_wr = list_entry(list_item,
2331 struct ib_mad_send_wr_private,
2332 agent_list);
2333 if (time_after(mad_send_wr->timeout,
2334 temp_mad_send_wr->timeout))
2335 break;
2336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 }
Hal Rosenstock29bb33d2005-07-27 11:45:32 -07002338 else
2339 list_item = &mad_agent_priv->wait_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 list_add(&mad_send_wr->agent_list, list_item);
2341
2342 /* Reschedule a work item if we have a shorter timeout */
Tejun Heoe7c2f962012-08-21 13:18:24 -07002343 if (mad_agent_priv->wait_list.next == &mad_send_wr->agent_list)
2344 mod_delayed_work(mad_agent_priv->qp_info->port_priv->wq,
2345 &mad_agent_priv->timed_work, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346}
2347
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002348void ib_reset_mad_timeout(struct ib_mad_send_wr_private *mad_send_wr,
2349 int timeout_ms)
2350{
2351 mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
2352 wait_for_response(mad_send_wr);
2353}
2354
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355/*
2356 * Process a send work completion
2357 */
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002358void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr,
2359 struct ib_mad_send_wc *mad_send_wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360{
2361 struct ib_mad_agent_private *mad_agent_priv;
2362 unsigned long flags;
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002363 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Hal Rosenstockd760ce82005-07-27 11:45:25 -07002365 mad_agent_priv = mad_send_wr->mad_agent_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 spin_lock_irqsave(&mad_agent_priv->lock, flags);
Ira Weiny1471cb62014-08-08 19:00:56 -04002367 if (ib_mad_kernel_rmpp_agent(&mad_agent_priv->agent)) {
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002368 ret = ib_process_rmpp_send_wc(mad_send_wr, mad_send_wc);
2369 if (ret == IB_RMPP_RESULT_CONSUMED)
2370 goto done;
2371 } else
2372 ret = IB_RMPP_RESULT_UNHANDLED;
2373
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 if (mad_send_wc->status != IB_WC_SUCCESS &&
2375 mad_send_wr->status == IB_WC_SUCCESS) {
2376 mad_send_wr->status = mad_send_wc->status;
2377 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2378 }
2379
2380 if (--mad_send_wr->refcount > 0) {
2381 if (mad_send_wr->refcount == 1 && mad_send_wr->timeout &&
2382 mad_send_wr->status == IB_WC_SUCCESS) {
Hal Rosenstockd760ce82005-07-27 11:45:25 -07002383 wait_for_response(mad_send_wr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 }
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002385 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 }
2387
2388 /* Remove send from MAD agent and notify client of completion */
2389 list_del(&mad_send_wr->agent_list);
2390 adjust_timeout(mad_agent_priv);
2391 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2392
2393 if (mad_send_wr->status != IB_WC_SUCCESS )
2394 mad_send_wc->status = mad_send_wr->status;
Sean Hefty34816ad2005-10-25 10:51:39 -07002395 if (ret == IB_RMPP_RESULT_INTERNAL)
2396 ib_rmpp_send_handler(mad_send_wc);
2397 else
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002398 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2399 mad_send_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
2401 /* Release reference on agent taken when sending */
Sean Hefty1b52fa982006-05-12 14:57:52 -07002402 deref_mad_agent(mad_agent_priv);
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002403 return;
2404done:
2405 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406}
2407
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08002408static void ib_mad_send_done(struct ib_cq *cq, struct ib_wc *wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409{
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08002410 struct ib_mad_port_private *port_priv = cq->cq_context;
2411 struct ib_mad_list_head *mad_list =
2412 container_of(wc->wr_cqe, struct ib_mad_list_head, cqe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 struct ib_mad_send_wr_private *mad_send_wr, *queued_send_wr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 struct ib_mad_qp_info *qp_info;
2415 struct ib_mad_queue *send_queue;
2416 struct ib_send_wr *bad_send_wr;
Sean Hefty34816ad2005-10-25 10:51:39 -07002417 struct ib_mad_send_wc mad_send_wc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 unsigned long flags;
2419 int ret;
2420
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08002421 if (list_empty_careful(&port_priv->port_list))
2422 return;
2423
2424 if (wc->status != IB_WC_SUCCESS) {
2425 if (!ib_mad_send_error(port_priv, wc))
2426 return;
2427 }
2428
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
2430 mad_list);
2431 send_queue = mad_list->mad_queue;
2432 qp_info = send_queue->qp_info;
2433
2434retry:
Ralph Campbell15271062006-12-12 14:28:30 -08002435 ib_dma_unmap_single(mad_send_wr->send_buf.mad_agent->device,
2436 mad_send_wr->header_mapping,
2437 mad_send_wr->sg_list[0].length, DMA_TO_DEVICE);
2438 ib_dma_unmap_single(mad_send_wr->send_buf.mad_agent->device,
2439 mad_send_wr->payload_mapping,
2440 mad_send_wr->sg_list[1].length, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 queued_send_wr = NULL;
2442 spin_lock_irqsave(&send_queue->lock, flags);
2443 list_del(&mad_list->list);
2444
2445 /* Move queued send to the send queue */
2446 if (send_queue->count-- > send_queue->max_active) {
2447 mad_list = container_of(qp_info->overflow_list.next,
2448 struct ib_mad_list_head, list);
2449 queued_send_wr = container_of(mad_list,
2450 struct ib_mad_send_wr_private,
2451 mad_list);
Akinobu Mita179e0912006-06-26 00:24:41 -07002452 list_move_tail(&mad_list->list, &send_queue->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 }
2454 spin_unlock_irqrestore(&send_queue->lock, flags);
2455
Sean Hefty34816ad2005-10-25 10:51:39 -07002456 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2457 mad_send_wc.status = wc->status;
2458 mad_send_wc.vendor_err = wc->vendor_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 if (atomic_read(&qp_info->snoop_count))
Sean Hefty34816ad2005-10-25 10:51:39 -07002460 snoop_send(qp_info, &mad_send_wr->send_buf, &mad_send_wc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 IB_MAD_SNOOP_SEND_COMPLETIONS);
Sean Hefty34816ad2005-10-25 10:51:39 -07002462 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
2464 if (queued_send_wr) {
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002465 ret = ib_post_send(qp_info->qp, &queued_send_wr->send_wr.wr,
Sean Hefty34816ad2005-10-25 10:51:39 -07002466 &bad_send_wr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 if (ret) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04002468 dev_err(&port_priv->device->dev,
2469 "ib_post_send failed: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 mad_send_wr = queued_send_wr;
2471 wc->status = IB_WC_LOC_QP_OP_ERR;
2472 goto retry;
2473 }
2474 }
2475}
2476
2477static void mark_sends_for_retry(struct ib_mad_qp_info *qp_info)
2478{
2479 struct ib_mad_send_wr_private *mad_send_wr;
2480 struct ib_mad_list_head *mad_list;
2481 unsigned long flags;
2482
2483 spin_lock_irqsave(&qp_info->send_queue.lock, flags);
2484 list_for_each_entry(mad_list, &qp_info->send_queue.list, list) {
2485 mad_send_wr = container_of(mad_list,
2486 struct ib_mad_send_wr_private,
2487 mad_list);
2488 mad_send_wr->retry = 1;
2489 }
2490 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
2491}
2492
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08002493static bool ib_mad_send_error(struct ib_mad_port_private *port_priv,
2494 struct ib_wc *wc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495{
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08002496 struct ib_mad_list_head *mad_list =
2497 container_of(wc->wr_cqe, struct ib_mad_list_head, cqe);
2498 struct ib_mad_qp_info *qp_info = mad_list->mad_queue->qp_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 struct ib_mad_send_wr_private *mad_send_wr;
2500 int ret;
2501
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 /*
2503 * Send errors will transition the QP to SQE - move
2504 * QP to RTS and repost flushed work requests
2505 */
2506 mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
2507 mad_list);
2508 if (wc->status == IB_WC_WR_FLUSH_ERR) {
2509 if (mad_send_wr->retry) {
2510 /* Repost send */
2511 struct ib_send_wr *bad_send_wr;
2512
2513 mad_send_wr->retry = 0;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002514 ret = ib_post_send(qp_info->qp, &mad_send_wr->send_wr.wr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 &bad_send_wr);
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08002516 if (!ret)
2517 return false;
2518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 } else {
2520 struct ib_qp_attr *attr;
2521
2522 /* Transition QP to RTS and fail offending send */
2523 attr = kmalloc(sizeof *attr, GFP_KERNEL);
2524 if (attr) {
2525 attr->qp_state = IB_QPS_RTS;
2526 attr->cur_qp_state = IB_QPS_SQE;
2527 ret = ib_modify_qp(qp_info->qp, attr,
2528 IB_QP_STATE | IB_QP_CUR_STATE);
2529 kfree(attr);
2530 if (ret)
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04002531 dev_err(&port_priv->device->dev,
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08002532 "%s - ib_modify_qp to RTS: %d\n",
2533 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 else
2535 mark_sends_for_retry(qp_info);
2536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08002539 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540}
2541
2542static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv)
2543{
2544 unsigned long flags;
2545 struct ib_mad_send_wr_private *mad_send_wr, *temp_mad_send_wr;
2546 struct ib_mad_send_wc mad_send_wc;
2547 struct list_head cancel_list;
2548
2549 INIT_LIST_HEAD(&cancel_list);
2550
2551 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2552 list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
2553 &mad_agent_priv->send_list, agent_list) {
2554 if (mad_send_wr->status == IB_WC_SUCCESS) {
Roland Dreier3cd96562006-09-22 15:22:46 -07002555 mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2557 }
2558 }
2559
2560 /* Empty wait list to prevent receives from finding a request */
2561 list_splice_init(&mad_agent_priv->wait_list, &cancel_list);
2562 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2563
2564 /* Report all cancelled requests */
2565 mad_send_wc.status = IB_WC_WR_FLUSH_ERR;
2566 mad_send_wc.vendor_err = 0;
2567
2568 list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
2569 &cancel_list, agent_list) {
Sean Hefty34816ad2005-10-25 10:51:39 -07002570 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2571 list_del(&mad_send_wr->agent_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2573 &mad_send_wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 atomic_dec(&mad_agent_priv->refcount);
2575 }
2576}
2577
2578static struct ib_mad_send_wr_private*
Sean Hefty34816ad2005-10-25 10:51:39 -07002579find_send_wr(struct ib_mad_agent_private *mad_agent_priv,
2580 struct ib_mad_send_buf *send_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581{
2582 struct ib_mad_send_wr_private *mad_send_wr;
2583
2584 list_for_each_entry(mad_send_wr, &mad_agent_priv->wait_list,
2585 agent_list) {
Sean Hefty34816ad2005-10-25 10:51:39 -07002586 if (&mad_send_wr->send_buf == send_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 return mad_send_wr;
2588 }
2589
2590 list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list,
2591 agent_list) {
Ira Weinyc597eee2015-05-08 13:10:03 -04002592 if (is_rmpp_data_mad(mad_agent_priv,
2593 mad_send_wr->send_buf.mad) &&
Sean Hefty34816ad2005-10-25 10:51:39 -07002594 &mad_send_wr->send_buf == send_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 return mad_send_wr;
2596 }
2597 return NULL;
2598}
2599
Sean Hefty34816ad2005-10-25 10:51:39 -07002600int ib_modify_mad(struct ib_mad_agent *mad_agent,
2601 struct ib_mad_send_buf *send_buf, u32 timeout_ms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602{
2603 struct ib_mad_agent_private *mad_agent_priv;
2604 struct ib_mad_send_wr_private *mad_send_wr;
2605 unsigned long flags;
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07002606 int active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
2608 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
2609 agent);
2610 spin_lock_irqsave(&mad_agent_priv->lock, flags);
Sean Hefty34816ad2005-10-25 10:51:39 -07002611 mad_send_wr = find_send_wr(mad_agent_priv, send_buf);
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002612 if (!mad_send_wr || mad_send_wr->status != IB_WC_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002614 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 }
2616
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07002617 active = (!mad_send_wr->timeout || mad_send_wr->refcount > 1);
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002618 if (!timeout_ms) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002620 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 }
2622
Sean Hefty34816ad2005-10-25 10:51:39 -07002623 mad_send_wr->send_buf.timeout_ms = timeout_ms;
Hal Rosenstockcabe3cb2005-07-27 11:45:33 -07002624 if (active)
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002625 mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
2626 else
2627 ib_reset_mad_timeout(mad_send_wr, timeout_ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002629 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2630 return 0;
2631}
2632EXPORT_SYMBOL(ib_modify_mad);
2633
Sean Hefty34816ad2005-10-25 10:51:39 -07002634void ib_cancel_mad(struct ib_mad_agent *mad_agent,
2635 struct ib_mad_send_buf *send_buf)
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002636{
Sean Hefty34816ad2005-10-25 10:51:39 -07002637 ib_modify_mad(mad_agent, send_buf, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638}
2639EXPORT_SYMBOL(ib_cancel_mad);
2640
David Howellsc4028952006-11-22 14:57:56 +00002641static void local_completions(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642{
2643 struct ib_mad_agent_private *mad_agent_priv;
2644 struct ib_mad_local_private *local;
2645 struct ib_mad_agent_private *recv_mad_agent;
2646 unsigned long flags;
Ralph Campbell1d9bc6d62009-02-27 10:34:30 -08002647 int free_mad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 struct ib_wc wc;
2649 struct ib_mad_send_wc mad_send_wc;
Ira Weiny8e4349d2015-06-10 16:16:48 -04002650 bool opa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
David Howellsc4028952006-11-22 14:57:56 +00002652 mad_agent_priv =
2653 container_of(work, struct ib_mad_agent_private, local_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
Ira Weiny8e4349d2015-06-10 16:16:48 -04002655 opa = rdma_cap_opa_mad(mad_agent_priv->qp_info->port_priv->device,
2656 mad_agent_priv->qp_info->port_priv->port_num);
2657
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2659 while (!list_empty(&mad_agent_priv->local_list)) {
2660 local = list_entry(mad_agent_priv->local_list.next,
2661 struct ib_mad_local_private,
2662 completion_list);
Michael S. Tsirkin37289ef2006-03-30 15:52:54 +02002663 list_del(&local->completion_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
Ralph Campbell1d9bc6d62009-02-27 10:34:30 -08002665 free_mad = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 if (local->mad_priv) {
Ira Weiny8e4349d2015-06-10 16:16:48 -04002667 u8 base_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 recv_mad_agent = local->recv_mad_agent;
2669 if (!recv_mad_agent) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04002670 dev_err(&mad_agent_priv->agent.device->dev,
2671 "No receive MAD agent for local completion\n");
Ralph Campbell1d9bc6d62009-02-27 10:34:30 -08002672 free_mad = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 goto local_send_completion;
2674 }
2675
2676 /*
2677 * Defined behavior is to complete response
2678 * before request
2679 */
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +02002680 build_smp_wc(recv_mad_agent->agent.qp,
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08002681 local->mad_send_wr->send_wr.wr.wr_cqe,
Sean Hefty97f52eb2005-08-13 21:05:57 -07002682 be16_to_cpu(IB_LID_PERMISSIVE),
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002683 local->mad_send_wr->send_wr.pkey_index,
Ira Weiny8e4349d2015-06-10 16:16:48 -04002684 recv_mad_agent->agent.port_num, &wc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
2686 local->mad_priv->header.recv_wc.wc = &wc;
Ira Weiny8e4349d2015-06-10 16:16:48 -04002687
2688 base_version = ((struct ib_mad_hdr *)(local->mad_priv->mad))->base_version;
2689 if (opa && base_version == OPA_MGMT_BASE_VERSION) {
2690 local->mad_priv->header.recv_wc.mad_len = local->return_wc_byte_len;
2691 local->mad_priv->header.recv_wc.mad_seg_size = sizeof(struct opa_mad);
2692 } else {
2693 local->mad_priv->header.recv_wc.mad_len = sizeof(struct ib_mad);
2694 local->mad_priv->header.recv_wc.mad_seg_size = sizeof(struct ib_mad);
2695 }
2696
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002697 INIT_LIST_HEAD(&local->mad_priv->header.recv_wc.rmpp_list);
2698 list_add(&local->mad_priv->header.recv_wc.recv_buf.list,
2699 &local->mad_priv->header.recv_wc.rmpp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 local->mad_priv->header.recv_wc.recv_buf.grh = NULL;
2701 local->mad_priv->header.recv_wc.recv_buf.mad =
Ira Weinyc9082e52015-06-06 14:38:30 -04002702 (struct ib_mad *)local->mad_priv->mad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 if (atomic_read(&recv_mad_agent->qp_info->snoop_count))
2704 snoop_recv(recv_mad_agent->qp_info,
2705 &local->mad_priv->header.recv_wc,
2706 IB_MAD_SNOOP_RECVS);
2707 recv_mad_agent->agent.recv_handler(
2708 &recv_mad_agent->agent,
Christoph Hellwigca281262016-01-04 14:15:58 +01002709 &local->mad_send_wr->send_buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 &local->mad_priv->header.recv_wc);
2711 spin_lock_irqsave(&recv_mad_agent->lock, flags);
2712 atomic_dec(&recv_mad_agent->refcount);
2713 spin_unlock_irqrestore(&recv_mad_agent->lock, flags);
2714 }
2715
2716local_send_completion:
2717 /* Complete send */
2718 mad_send_wc.status = IB_WC_SUCCESS;
2719 mad_send_wc.vendor_err = 0;
Sean Hefty34816ad2005-10-25 10:51:39 -07002720 mad_send_wc.send_buf = &local->mad_send_wr->send_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 if (atomic_read(&mad_agent_priv->qp_info->snoop_count))
Sean Hefty34816ad2005-10-25 10:51:39 -07002722 snoop_send(mad_agent_priv->qp_info,
2723 &local->mad_send_wr->send_buf,
2724 &mad_send_wc, IB_MAD_SNOOP_SEND_COMPLETIONS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2726 &mad_send_wc);
2727
2728 spin_lock_irqsave(&mad_agent_priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 atomic_dec(&mad_agent_priv->refcount);
Ralph Campbell1d9bc6d62009-02-27 10:34:30 -08002730 if (free_mad)
Ira Weinyc9082e52015-06-06 14:38:30 -04002731 kfree(local->mad_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 kfree(local);
2733 }
2734 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2735}
2736
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002737static int retry_send(struct ib_mad_send_wr_private *mad_send_wr)
2738{
2739 int ret;
2740
Sean Hefty4fc8cd42007-11-27 00:11:04 -08002741 if (!mad_send_wr->retries_left)
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002742 return -ETIMEDOUT;
2743
Sean Hefty4fc8cd42007-11-27 00:11:04 -08002744 mad_send_wr->retries_left--;
2745 mad_send_wr->send_buf.retries++;
2746
Sean Hefty34816ad2005-10-25 10:51:39 -07002747 mad_send_wr->timeout = msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms);
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002748
Ira Weiny1471cb62014-08-08 19:00:56 -04002749 if (ib_mad_kernel_rmpp_agent(&mad_send_wr->mad_agent_priv->agent)) {
Hal Rosenstockfa619a72005-07-27 11:45:37 -07002750 ret = ib_retry_rmpp(mad_send_wr);
2751 switch (ret) {
2752 case IB_RMPP_RESULT_UNHANDLED:
2753 ret = ib_send_mad(mad_send_wr);
2754 break;
2755 case IB_RMPP_RESULT_CONSUMED:
2756 ret = 0;
2757 break;
2758 default:
2759 ret = -ECOMM;
2760 break;
2761 }
2762 } else
2763 ret = ib_send_mad(mad_send_wr);
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002764
2765 if (!ret) {
2766 mad_send_wr->refcount++;
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002767 list_add_tail(&mad_send_wr->agent_list,
2768 &mad_send_wr->mad_agent_priv->send_list);
2769 }
2770 return ret;
2771}
2772
David Howellsc4028952006-11-22 14:57:56 +00002773static void timeout_sends(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774{
2775 struct ib_mad_agent_private *mad_agent_priv;
2776 struct ib_mad_send_wr_private *mad_send_wr;
2777 struct ib_mad_send_wc mad_send_wc;
2778 unsigned long flags, delay;
2779
David Howellsc4028952006-11-22 14:57:56 +00002780 mad_agent_priv = container_of(work, struct ib_mad_agent_private,
2781 timed_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 mad_send_wc.vendor_err = 0;
2783
2784 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2785 while (!list_empty(&mad_agent_priv->wait_list)) {
2786 mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
2787 struct ib_mad_send_wr_private,
2788 agent_list);
2789
2790 if (time_after(mad_send_wr->timeout, jiffies)) {
2791 delay = mad_send_wr->timeout - jiffies;
2792 if ((long)delay <= 0)
2793 delay = 1;
2794 queue_delayed_work(mad_agent_priv->qp_info->
2795 port_priv->wq,
2796 &mad_agent_priv->timed_work, delay);
2797 break;
2798 }
2799
Hal Rosenstockdbf92272005-07-27 11:45:30 -07002800 list_del(&mad_send_wr->agent_list);
Hal Rosenstock29bb33d2005-07-27 11:45:32 -07002801 if (mad_send_wr->status == IB_WC_SUCCESS &&
2802 !retry_send(mad_send_wr))
Hal Rosenstockf75b7a52005-07-27 11:45:29 -07002803 continue;
2804
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2806
Hal Rosenstock03b61ad2005-07-27 11:45:32 -07002807 if (mad_send_wr->status == IB_WC_SUCCESS)
2808 mad_send_wc.status = IB_WC_RESP_TIMEOUT_ERR;
2809 else
2810 mad_send_wc.status = mad_send_wr->status;
Sean Hefty34816ad2005-10-25 10:51:39 -07002811 mad_send_wc.send_buf = &mad_send_wr->send_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2813 &mad_send_wc);
2814
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 atomic_dec(&mad_agent_priv->refcount);
2816 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2817 }
2818 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2819}
2820
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821/*
2822 * Allocate receive MADs and post receive WRs for them
2823 */
2824static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
2825 struct ib_mad_private *mad)
2826{
2827 unsigned long flags;
2828 int post, ret;
2829 struct ib_mad_private *mad_priv;
2830 struct ib_sge sg_list;
2831 struct ib_recv_wr recv_wr, *bad_recv_wr;
2832 struct ib_mad_queue *recv_queue = &qp_info->recv_queue;
2833
2834 /* Initialize common scatter list fields */
Jason Gunthorpe4be90bc2015-07-30 17:22:16 -06002835 sg_list.lkey = qp_info->port_priv->pd->local_dma_lkey;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
2837 /* Initialize common receive WR fields */
2838 recv_wr.next = NULL;
2839 recv_wr.sg_list = &sg_list;
2840 recv_wr.num_sge = 1;
2841
2842 do {
2843 /* Allocate and map receive buffer */
2844 if (mad) {
2845 mad_priv = mad;
2846 mad = NULL;
2847 } else {
Ira Weinyc9082e52015-06-06 14:38:30 -04002848 mad_priv = alloc_mad_private(port_mad_size(qp_info->port_priv),
2849 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 if (!mad_priv) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 ret = -ENOMEM;
2852 break;
2853 }
2854 }
Ira Weinyc9082e52015-06-06 14:38:30 -04002855 sg_list.length = mad_priv_dma_size(mad_priv);
Ralph Campbell15271062006-12-12 14:28:30 -08002856 sg_list.addr = ib_dma_map_single(qp_info->port_priv->device,
2857 &mad_priv->grh,
Ira Weinyc9082e52015-06-06 14:38:30 -04002858 mad_priv_dma_size(mad_priv),
Ralph Campbell15271062006-12-12 14:28:30 -08002859 DMA_FROM_DEVICE);
Yan Burman2c34e682014-03-11 14:41:47 +02002860 if (unlikely(ib_dma_mapping_error(qp_info->port_priv->device,
2861 sg_list.addr))) {
2862 ret = -ENOMEM;
2863 break;
2864 }
Ralph Campbell15271062006-12-12 14:28:30 -08002865 mad_priv->header.mapping = sg_list.addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 mad_priv->header.mad_list.mad_queue = recv_queue;
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08002867 mad_priv->header.mad_list.cqe.done = ib_mad_recv_done;
2868 recv_wr.wr_cqe = &mad_priv->header.mad_list.cqe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
2870 /* Post receive WR */
2871 spin_lock_irqsave(&recv_queue->lock, flags);
2872 post = (++recv_queue->count < recv_queue->max_active);
2873 list_add_tail(&mad_priv->header.mad_list.list, &recv_queue->list);
2874 spin_unlock_irqrestore(&recv_queue->lock, flags);
2875 ret = ib_post_recv(qp_info->qp, &recv_wr, &bad_recv_wr);
2876 if (ret) {
2877 spin_lock_irqsave(&recv_queue->lock, flags);
2878 list_del(&mad_priv->header.mad_list.list);
2879 recv_queue->count--;
2880 spin_unlock_irqrestore(&recv_queue->lock, flags);
Ralph Campbell15271062006-12-12 14:28:30 -08002881 ib_dma_unmap_single(qp_info->port_priv->device,
2882 mad_priv->header.mapping,
Ira Weinyc9082e52015-06-06 14:38:30 -04002883 mad_priv_dma_size(mad_priv),
Ralph Campbell15271062006-12-12 14:28:30 -08002884 DMA_FROM_DEVICE);
Ira Weinyc9082e52015-06-06 14:38:30 -04002885 kfree(mad_priv);
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04002886 dev_err(&qp_info->port_priv->device->dev,
2887 "ib_post_recv failed: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 break;
2889 }
2890 } while (post);
2891
2892 return ret;
2893}
2894
2895/*
2896 * Return all the posted receive MADs
2897 */
2898static void cleanup_recv_queue(struct ib_mad_qp_info *qp_info)
2899{
2900 struct ib_mad_private_header *mad_priv_hdr;
2901 struct ib_mad_private *recv;
2902 struct ib_mad_list_head *mad_list;
2903
Eli Cohenfac70d52010-09-27 17:51:11 -07002904 if (!qp_info->qp)
2905 return;
2906
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 while (!list_empty(&qp_info->recv_queue.list)) {
2908
2909 mad_list = list_entry(qp_info->recv_queue.list.next,
2910 struct ib_mad_list_head, list);
2911 mad_priv_hdr = container_of(mad_list,
2912 struct ib_mad_private_header,
2913 mad_list);
2914 recv = container_of(mad_priv_hdr, struct ib_mad_private,
2915 header);
2916
2917 /* Remove from posted receive MAD list */
2918 list_del(&mad_list->list);
2919
Ralph Campbell15271062006-12-12 14:28:30 -08002920 ib_dma_unmap_single(qp_info->port_priv->device,
2921 recv->header.mapping,
Ira Weinyc9082e52015-06-06 14:38:30 -04002922 mad_priv_dma_size(recv),
Ralph Campbell15271062006-12-12 14:28:30 -08002923 DMA_FROM_DEVICE);
Ira Weinyc9082e52015-06-06 14:38:30 -04002924 kfree(recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 }
2926
2927 qp_info->recv_queue.count = 0;
2928}
2929
2930/*
2931 * Start the port
2932 */
2933static int ib_mad_port_start(struct ib_mad_port_private *port_priv)
2934{
2935 int ret, i;
2936 struct ib_qp_attr *attr;
2937 struct ib_qp *qp;
Jack Morgensteinef5ed412013-07-18 14:02:29 +03002938 u16 pkey_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
2940 attr = kmalloc(sizeof *attr, GFP_KERNEL);
Leon Romanovsky27162432016-11-03 16:44:09 +02002941 if (!attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
Jack Morgensteinef5ed412013-07-18 14:02:29 +03002944 ret = ib_find_pkey(port_priv->device, port_priv->port_num,
2945 IB_DEFAULT_PKEY_FULL, &pkey_index);
2946 if (ret)
2947 pkey_index = 0;
2948
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 for (i = 0; i < IB_MAD_QPS_CORE; i++) {
2950 qp = port_priv->qp_info[i].qp;
Eli Cohenfac70d52010-09-27 17:51:11 -07002951 if (!qp)
2952 continue;
2953
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 /*
2955 * PKey index for QP1 is irrelevant but
2956 * one is needed for the Reset to Init transition
2957 */
2958 attr->qp_state = IB_QPS_INIT;
Jack Morgensteinef5ed412013-07-18 14:02:29 +03002959 attr->pkey_index = pkey_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 attr->qkey = (qp->qp_num == 0) ? 0 : IB_QP1_QKEY;
2961 ret = ib_modify_qp(qp, attr, IB_QP_STATE |
2962 IB_QP_PKEY_INDEX | IB_QP_QKEY);
2963 if (ret) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04002964 dev_err(&port_priv->device->dev,
2965 "Couldn't change QP%d state to INIT: %d\n",
2966 i, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 goto out;
2968 }
2969
2970 attr->qp_state = IB_QPS_RTR;
2971 ret = ib_modify_qp(qp, attr, IB_QP_STATE);
2972 if (ret) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04002973 dev_err(&port_priv->device->dev,
2974 "Couldn't change QP%d state to RTR: %d\n",
2975 i, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 goto out;
2977 }
2978
2979 attr->qp_state = IB_QPS_RTS;
2980 attr->sq_psn = IB_MAD_SEND_Q_PSN;
2981 ret = ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_SQ_PSN);
2982 if (ret) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04002983 dev_err(&port_priv->device->dev,
2984 "Couldn't change QP%d state to RTS: %d\n",
2985 i, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 goto out;
2987 }
2988 }
2989
2990 ret = ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
2991 if (ret) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04002992 dev_err(&port_priv->device->dev,
2993 "Failed to request completion notification: %d\n",
2994 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 goto out;
2996 }
2997
2998 for (i = 0; i < IB_MAD_QPS_CORE; i++) {
Eli Cohenfac70d52010-09-27 17:51:11 -07002999 if (!port_priv->qp_info[i].qp)
3000 continue;
3001
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 ret = ib_mad_post_receive_mads(&port_priv->qp_info[i], NULL);
3003 if (ret) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04003004 dev_err(&port_priv->device->dev,
3005 "Couldn't post receive WRs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 goto out;
3007 }
3008 }
3009out:
3010 kfree(attr);
3011 return ret;
3012}
3013
3014static void qp_event_handler(struct ib_event *event, void *qp_context)
3015{
3016 struct ib_mad_qp_info *qp_info = qp_context;
3017
3018 /* It's worse than that! He's dead, Jim! */
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04003019 dev_err(&qp_info->port_priv->device->dev,
3020 "Fatal error (%d) on MAD QP (%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 event->event, qp_info->qp->qp_num);
3022}
3023
3024static void init_mad_queue(struct ib_mad_qp_info *qp_info,
3025 struct ib_mad_queue *mad_queue)
3026{
3027 mad_queue->qp_info = qp_info;
3028 mad_queue->count = 0;
3029 spin_lock_init(&mad_queue->lock);
3030 INIT_LIST_HEAD(&mad_queue->list);
3031}
3032
3033static void init_mad_qp(struct ib_mad_port_private *port_priv,
3034 struct ib_mad_qp_info *qp_info)
3035{
3036 qp_info->port_priv = port_priv;
3037 init_mad_queue(qp_info, &qp_info->send_queue);
3038 init_mad_queue(qp_info, &qp_info->recv_queue);
3039 INIT_LIST_HEAD(&qp_info->overflow_list);
3040 spin_lock_init(&qp_info->snoop_lock);
3041 qp_info->snoop_table = NULL;
3042 qp_info->snoop_table_size = 0;
3043 atomic_set(&qp_info->snoop_count, 0);
3044}
3045
3046static int create_mad_qp(struct ib_mad_qp_info *qp_info,
3047 enum ib_qp_type qp_type)
3048{
3049 struct ib_qp_init_attr qp_init_attr;
3050 int ret;
3051
3052 memset(&qp_init_attr, 0, sizeof qp_init_attr);
3053 qp_init_attr.send_cq = qp_info->port_priv->cq;
3054 qp_init_attr.recv_cq = qp_info->port_priv->cq;
3055 qp_init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
Hal Rosenstockb76aabc2009-09-07 08:28:48 -07003056 qp_init_attr.cap.max_send_wr = mad_sendq_size;
3057 qp_init_attr.cap.max_recv_wr = mad_recvq_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 qp_init_attr.cap.max_send_sge = IB_MAD_SEND_REQ_MAX_SG;
3059 qp_init_attr.cap.max_recv_sge = IB_MAD_RECV_REQ_MAX_SG;
3060 qp_init_attr.qp_type = qp_type;
3061 qp_init_attr.port_num = qp_info->port_priv->port_num;
3062 qp_init_attr.qp_context = qp_info;
3063 qp_init_attr.event_handler = qp_event_handler;
3064 qp_info->qp = ib_create_qp(qp_info->port_priv->pd, &qp_init_attr);
3065 if (IS_ERR(qp_info->qp)) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04003066 dev_err(&qp_info->port_priv->device->dev,
3067 "Couldn't create ib_mad QP%d\n",
3068 get_spl_qp_index(qp_type));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 ret = PTR_ERR(qp_info->qp);
3070 goto error;
3071 }
3072 /* Use minimum queue sizes unless the CQ is resized */
Hal Rosenstockb76aabc2009-09-07 08:28:48 -07003073 qp_info->send_queue.max_active = mad_sendq_size;
3074 qp_info->recv_queue.max_active = mad_recvq_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 return 0;
3076
3077error:
3078 return ret;
3079}
3080
3081static void destroy_mad_qp(struct ib_mad_qp_info *qp_info)
3082{
Eli Cohenfac70d52010-09-27 17:51:11 -07003083 if (!qp_info->qp)
3084 return;
3085
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 ib_destroy_qp(qp_info->qp);
Jesper Juhl6044ec82005-11-07 01:01:32 -08003087 kfree(qp_info->snoop_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088}
3089
3090/*
3091 * Open the port
3092 * Create the QP, PD, MR, and CQ if needed
3093 */
3094static int ib_mad_port_open(struct ib_device *device,
3095 int port_num)
3096{
3097 int ret, cq_size;
3098 struct ib_mad_port_private *port_priv;
3099 unsigned long flags;
3100 char name[sizeof "ib_mad123"];
Eli Cohenfac70d52010-09-27 17:51:11 -07003101 int has_smi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102
Ira Weiny337877a2015-06-06 14:38:29 -04003103 if (WARN_ON(rdma_max_mad_size(device, port_num) < IB_MGMT_MAD_SIZE))
3104 return -EFAULT;
3105
Ira Weiny548ead12015-06-06 14:38:33 -04003106 if (WARN_ON(rdma_cap_opa_mad(device, port_num) &&
3107 rdma_max_mad_size(device, port_num) < OPA_MGMT_MAD_SIZE))
3108 return -EFAULT;
3109
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 /* Create new device info */
Roland Dreierde6eb662005-11-02 07:23:14 -08003111 port_priv = kzalloc(sizeof *port_priv, GFP_KERNEL);
Leon Romanovsky27162432016-11-03 16:44:09 +02003112 if (!port_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 return -ENOMEM;
Roland Dreierde6eb662005-11-02 07:23:14 -08003114
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 port_priv->device = device;
3116 port_priv->port_num = port_num;
3117 spin_lock_init(&port_priv->reg_lock);
3118 INIT_LIST_HEAD(&port_priv->agent_list);
3119 init_mad_qp(port_priv, &port_priv->qp_info[0]);
3120 init_mad_qp(port_priv, &port_priv->qp_info[1]);
3121
Eli Cohenfac70d52010-09-27 17:51:11 -07003122 cq_size = mad_sendq_size + mad_recvq_size;
Michael Wang29541e32015-05-05 14:50:33 +02003123 has_smi = rdma_cap_ib_smi(device, port_num);
Eli Cohenfac70d52010-09-27 17:51:11 -07003124 if (has_smi)
3125 cq_size *= 2;
3126
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08003127 port_priv->cq = ib_alloc_cq(port_priv->device, port_priv, cq_size, 0,
3128 IB_POLL_WORKQUEUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 if (IS_ERR(port_priv->cq)) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04003130 dev_err(&device->dev, "Couldn't create ib_mad CQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 ret = PTR_ERR(port_priv->cq);
3132 goto error3;
3133 }
3134
Christoph Hellwiged082d32016-09-05 12:56:17 +02003135 port_priv->pd = ib_alloc_pd(device, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 if (IS_ERR(port_priv->pd)) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04003137 dev_err(&device->dev, "Couldn't create ib_mad PD\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 ret = PTR_ERR(port_priv->pd);
3139 goto error4;
3140 }
3141
Eli Cohenfac70d52010-09-27 17:51:11 -07003142 if (has_smi) {
3143 ret = create_mad_qp(&port_priv->qp_info[0], IB_QPT_SMI);
3144 if (ret)
3145 goto error6;
3146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 ret = create_mad_qp(&port_priv->qp_info[1], IB_QPT_GSI);
3148 if (ret)
3149 goto error7;
3150
3151 snprintf(name, sizeof name, "ib_mad%d", port_num);
Bhaktipriya Shridhar1c99e292016-08-15 23:28:07 +05303152 port_priv->wq = alloc_ordered_workqueue(name, WQ_MEM_RECLAIM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 if (!port_priv->wq) {
3154 ret = -ENOMEM;
3155 goto error8;
3156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
Michael S. Tsirkindc059802006-03-20 10:08:25 -08003158 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
3159 list_add_tail(&port_priv->port_list, &ib_mad_port_list);
3160 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
3161
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 ret = ib_mad_port_start(port_priv);
3163 if (ret) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04003164 dev_err(&device->dev, "Couldn't start port\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 goto error9;
3166 }
3167
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 return 0;
3169
3170error9:
Michael S. Tsirkindc059802006-03-20 10:08:25 -08003171 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
3172 list_del_init(&port_priv->port_list);
3173 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
3174
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 destroy_workqueue(port_priv->wq);
3176error8:
3177 destroy_mad_qp(&port_priv->qp_info[1]);
3178error7:
3179 destroy_mad_qp(&port_priv->qp_info[0]);
3180error6:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 ib_dealloc_pd(port_priv->pd);
3182error4:
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08003183 ib_free_cq(port_priv->cq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 cleanup_recv_queue(&port_priv->qp_info[1]);
3185 cleanup_recv_queue(&port_priv->qp_info[0]);
3186error3:
3187 kfree(port_priv);
3188
3189 return ret;
3190}
3191
3192/*
3193 * Close the port
3194 * If there are no classes using the port, free the port
3195 * resources (CQ, MR, PD, QP) and remove the port's info structure
3196 */
3197static int ib_mad_port_close(struct ib_device *device, int port_num)
3198{
3199 struct ib_mad_port_private *port_priv;
3200 unsigned long flags;
3201
3202 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
3203 port_priv = __ib_get_mad_port(device, port_num);
3204 if (port_priv == NULL) {
3205 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04003206 dev_err(&device->dev, "Port %d not found\n", port_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 return -ENODEV;
3208 }
Michael S. Tsirkindc059802006-03-20 10:08:25 -08003209 list_del_init(&port_priv->port_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
3211
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 destroy_workqueue(port_priv->wq);
3213 destroy_mad_qp(&port_priv->qp_info[1]);
3214 destroy_mad_qp(&port_priv->qp_info[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 ib_dealloc_pd(port_priv->pd);
Christoph Hellwigd53e11f2016-01-05 22:46:12 -08003216 ib_free_cq(port_priv->cq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 cleanup_recv_queue(&port_priv->qp_info[1]);
3218 cleanup_recv_queue(&port_priv->qp_info[0]);
3219 /* XXX: Handle deallocation of MAD registration tables */
3220
3221 kfree(port_priv);
3222
3223 return 0;
3224}
3225
3226static void ib_mad_init_device(struct ib_device *device)
3227{
Hal Rosenstock41390322015-06-29 09:57:00 -04003228 int start, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
Hal Rosenstock41390322015-06-29 09:57:00 -04003230 start = rdma_start_port(device);
Roland Dreier4ab6fb72005-10-06 13:28:16 -07003231
Hal Rosenstock41390322015-06-29 09:57:00 -04003232 for (i = start; i <= rdma_end_port(device); i++) {
Michael Wangc757dea2015-05-05 14:50:32 +02003233 if (!rdma_cap_ib_mad(device, i))
Michael Wang827f2a82015-05-05 14:50:20 +02003234 continue;
3235
Roland Dreier4ab6fb72005-10-06 13:28:16 -07003236 if (ib_mad_port_open(device, i)) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04003237 dev_err(&device->dev, "Couldn't open port %d\n", i);
Roland Dreier4ab6fb72005-10-06 13:28:16 -07003238 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 }
Roland Dreier4ab6fb72005-10-06 13:28:16 -07003240 if (ib_agent_port_open(device, i)) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04003241 dev_err(&device->dev,
3242 "Couldn't open port %d for agents\n", i);
Roland Dreier4ab6fb72005-10-06 13:28:16 -07003243 goto error_agent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 }
3245 }
Hal Rosenstockf68bcc22005-07-27 11:45:27 -07003246 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247
Roland Dreier4ab6fb72005-10-06 13:28:16 -07003248error_agent:
3249 if (ib_mad_port_close(device, i))
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04003250 dev_err(&device->dev, "Couldn't close port %d\n", i);
Roland Dreier4ab6fb72005-10-06 13:28:16 -07003251
3252error:
Michael Wang827f2a82015-05-05 14:50:20 +02003253 while (--i >= start) {
Michael Wangc757dea2015-05-05 14:50:32 +02003254 if (!rdma_cap_ib_mad(device, i))
Michael Wang827f2a82015-05-05 14:50:20 +02003255 continue;
Roland Dreier4ab6fb72005-10-06 13:28:16 -07003256
Roland Dreier4ab6fb72005-10-06 13:28:16 -07003257 if (ib_agent_port_close(device, i))
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04003258 dev_err(&device->dev,
3259 "Couldn't close port %d for agents\n", i);
Roland Dreier4ab6fb72005-10-06 13:28:16 -07003260 if (ib_mad_port_close(device, i))
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04003261 dev_err(&device->dev, "Couldn't close port %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263}
3264
Haggai Eran7c1eb452015-07-30 17:50:14 +03003265static void ib_mad_remove_device(struct ib_device *device, void *client_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266{
Hal Rosenstock41390322015-06-29 09:57:00 -04003267 int i;
Steve Wise070e1402010-03-04 18:18:18 +00003268
Hal Rosenstock41390322015-06-29 09:57:00 -04003269 for (i = rdma_start_port(device); i <= rdma_end_port(device); i++) {
Michael Wangc757dea2015-05-05 14:50:32 +02003270 if (!rdma_cap_ib_mad(device, i))
Michael Wang827f2a82015-05-05 14:50:20 +02003271 continue;
3272
3273 if (ib_agent_port_close(device, i))
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04003274 dev_err(&device->dev,
Michael Wang827f2a82015-05-05 14:50:20 +02003275 "Couldn't close port %d for agents\n", i);
3276 if (ib_mad_port_close(device, i))
3277 dev_err(&device->dev, "Couldn't close port %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 }
3279}
3280
3281static struct ib_client mad_client = {
3282 .name = "mad",
3283 .add = ib_mad_init_device,
3284 .remove = ib_mad_remove_device
3285};
3286
Mark Bloch4c2cb422016-05-19 17:12:32 +03003287int ib_mad_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288{
Hal Rosenstockb76aabc2009-09-07 08:28:48 -07003289 mad_recvq_size = min(mad_recvq_size, IB_MAD_QP_MAX_SIZE);
3290 mad_recvq_size = max(mad_recvq_size, IB_MAD_QP_MIN_SIZE);
3291
3292 mad_sendq_size = min(mad_sendq_size, IB_MAD_QP_MAX_SIZE);
3293 mad_sendq_size = max(mad_sendq_size, IB_MAD_QP_MIN_SIZE);
3294
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 INIT_LIST_HEAD(&ib_mad_port_list);
3296
3297 if (ib_register_client(&mad_client)) {
Ira Weiny7ef5d4b2014-08-08 19:00:53 -04003298 pr_err("Couldn't register ib_mad client\n");
Ira Weinyc9082e52015-06-06 14:38:30 -04003299 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 }
3301
3302 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303}
3304
Mark Bloch4c2cb422016-05-19 17:12:32 +03003305void ib_mad_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306{
3307 ib_unregister_client(&mad_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308}