IB/mad: pass ib_mad_send_buf explicitly to the recv_handler
[deliverable/linux.git] / drivers / infiniband / core / mad.c
CommitLineData
1da177e4 1/*
de493d47 2 * Copyright (c) 2004-2007 Voltaire, Inc. All rights reserved.
fa619a77
HR
3 * Copyright (c) 2005 Intel Corporation. All rights reserved.
4 * Copyright (c) 2005 Mellanox Technologies Ltd. All rights reserved.
b76aabc3 5 * Copyright (c) 2009 HNR Consulting. All rights reserved.
8e4349d1 6 * Copyright (c) 2014 Intel Corporation. All rights reserved.
1da177e4
LT
7 *
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 *
1da177e4 36 */
7ef5d4b0
IW
37
38#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
39
1da177e4 40#include <linux/dma-mapping.h>
5a0e3ad6 41#include <linux/slab.h>
e4dd23d7 42#include <linux/module.h>
9874e746 43#include <rdma/ib_cache.h>
1da177e4
LT
44
45#include "mad_priv.h"
fa619a77 46#include "mad_rmpp.h"
1da177e4 47#include "smi.h"
8e4349d1 48#include "opa_smi.h"
1da177e4
LT
49#include "agent.h"
50
51MODULE_LICENSE("Dual BSD/GPL");
52MODULE_DESCRIPTION("kernel IB MAD API");
53MODULE_AUTHOR("Hal Rosenstock");
54MODULE_AUTHOR("Sean Hefty");
55
16933955
RD
56static int mad_sendq_size = IB_MAD_QP_SEND_SIZE;
57static int mad_recvq_size = IB_MAD_QP_RECV_SIZE;
b76aabc3
HR
58
59module_param_named(send_queue_size, mad_sendq_size, int, 0444);
60MODULE_PARM_DESC(send_queue_size, "Size of send queue in number of work requests");
61module_param_named(recv_queue_size, mad_recvq_size, int, 0444);
62MODULE_PARM_DESC(recv_queue_size, "Size of receive queue in number of work requests");
63
0d6ed314
DL
64/*
65 * Define a limit on the number of completions which will be processed by the
66 * worker thread in a single work item. This ensures that other work items
67 * (potentially from other users) are processed fairly.
68 *
69 * The number of completions was derived from the default queue sizes above.
70 * We use a value which is double the larger of the 2 queues (receive @ 512)
71 * but keep it fixed such that an increase in that value does not introduce
72 * unfairness.
73 */
74#define MAD_COMPLETION_PROC_LIMIT 1024
75
1da177e4
LT
76static struct list_head ib_mad_port_list;
77static u32 ib_mad_client_id = 0;
78
79/* Port list lock */
6276e08a 80static DEFINE_SPINLOCK(ib_mad_port_list_lock);
1da177e4
LT
81
82/* Forward declarations */
83static int method_in_use(struct ib_mad_mgmt_method_table **method,
84 struct ib_mad_reg_req *mad_reg_req);
85static void remove_mad_reg_req(struct ib_mad_agent_private *priv);
86static struct ib_mad_agent_private *find_mad_agent(
87 struct ib_mad_port_private *port_priv,
d94bd266 88 const struct ib_mad_hdr *mad);
1da177e4
LT
89static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
90 struct ib_mad_private *mad);
91static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv);
c4028958
DH
92static void timeout_sends(struct work_struct *work);
93static void local_completions(struct work_struct *work);
1da177e4
LT
94static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
95 struct ib_mad_agent_private *agent_priv,
96 u8 mgmt_class);
97static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
98 struct ib_mad_agent_private *agent_priv);
99
100/*
101 * Returns a ib_mad_port_private structure or NULL for a device/port
102 * Assumes ib_mad_port_list_lock is being held
103 */
104static inline struct ib_mad_port_private *
105__ib_get_mad_port(struct ib_device *device, int port_num)
106{
107 struct ib_mad_port_private *entry;
108
109 list_for_each_entry(entry, &ib_mad_port_list, port_list) {
110 if (entry->device == device && entry->port_num == port_num)
111 return entry;
112 }
113 return NULL;
114}
115
116/*
117 * Wrapper function to return a ib_mad_port_private structure or NULL
118 * for a device/port
119 */
120static inline struct ib_mad_port_private *
121ib_get_mad_port(struct ib_device *device, int port_num)
122{
123 struct ib_mad_port_private *entry;
124 unsigned long flags;
125
126 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
127 entry = __ib_get_mad_port(device, port_num);
128 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
129
130 return entry;
131}
132
133static inline u8 convert_mgmt_class(u8 mgmt_class)
134{
135 /* Alias IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE to 0 */
136 return mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE ?
137 0 : mgmt_class;
138}
139
140static int get_spl_qp_index(enum ib_qp_type qp_type)
141{
142 switch (qp_type)
143 {
144 case IB_QPT_SMI:
145 return 0;
146 case IB_QPT_GSI:
147 return 1;
148 default:
149 return -1;
150 }
151}
152
153static int vendor_class_index(u8 mgmt_class)
154{
155 return mgmt_class - IB_MGMT_CLASS_VENDOR_RANGE2_START;
156}
157
158static int is_vendor_class(u8 mgmt_class)
159{
160 if ((mgmt_class < IB_MGMT_CLASS_VENDOR_RANGE2_START) ||
161 (mgmt_class > IB_MGMT_CLASS_VENDOR_RANGE2_END))
162 return 0;
163 return 1;
164}
165
166static int is_vendor_oui(char *oui)
167{
168 if (oui[0] || oui[1] || oui[2])
169 return 1;
170 return 0;
171}
172
173static int is_vendor_method_in_use(
174 struct ib_mad_mgmt_vendor_class *vendor_class,
175 struct ib_mad_reg_req *mad_reg_req)
176{
177 struct ib_mad_mgmt_method_table *method;
178 int i;
179
180 for (i = 0; i < MAX_MGMT_OUI; i++) {
181 if (!memcmp(vendor_class->oui[i], mad_reg_req->oui, 3)) {
182 method = vendor_class->method_table[i];
183 if (method) {
184 if (method_in_use(&method, mad_reg_req))
185 return 1;
186 else
187 break;
188 }
189 }
190 }
191 return 0;
192}
193
96909308 194int ib_response_mad(const struct ib_mad_hdr *hdr)
2527e681 195{
96909308
IW
196 return ((hdr->method & IB_MGMT_METHOD_RESP) ||
197 (hdr->method == IB_MGMT_METHOD_TRAP_REPRESS) ||
198 ((hdr->mgmt_class == IB_MGMT_CLASS_BM) &&
199 (hdr->attr_mod & IB_BM_ATTR_MOD_RESP)));
2527e681
SH
200}
201EXPORT_SYMBOL(ib_response_mad);
202
1da177e4
LT
203/*
204 * ib_register_mad_agent - Register to send/receive MADs
205 */
206struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
207 u8 port_num,
208 enum ib_qp_type qp_type,
209 struct ib_mad_reg_req *mad_reg_req,
210 u8 rmpp_version,
211 ib_mad_send_handler send_handler,
212 ib_mad_recv_handler recv_handler,
0f29b46d
IW
213 void *context,
214 u32 registration_flags)
1da177e4
LT
215{
216 struct ib_mad_port_private *port_priv;
217 struct ib_mad_agent *ret = ERR_PTR(-EINVAL);
218 struct ib_mad_agent_private *mad_agent_priv;
219 struct ib_mad_reg_req *reg_req = NULL;
220 struct ib_mad_mgmt_class_table *class;
221 struct ib_mad_mgmt_vendor_class_table *vendor;
222 struct ib_mad_mgmt_vendor_class *vendor_class;
223 struct ib_mad_mgmt_method_table *method;
224 int ret2, qpn;
225 unsigned long flags;
226 u8 mgmt_class, vclass;
227
228 /* Validate parameters */
229 qpn = get_spl_qp_index(qp_type);
9ad13a42
IW
230 if (qpn == -1) {
231 dev_notice(&device->dev,
232 "ib_register_mad_agent: invalid QP Type %d\n",
233 qp_type);
1da177e4 234 goto error1;
9ad13a42 235 }
1da177e4 236
9ad13a42
IW
237 if (rmpp_version && rmpp_version != IB_MGMT_RMPP_VERSION) {
238 dev_notice(&device->dev,
239 "ib_register_mad_agent: invalid RMPP Version %u\n",
240 rmpp_version);
fa619a77 241 goto error1;
9ad13a42 242 }
1da177e4
LT
243
244 /* Validate MAD registration request if supplied */
245 if (mad_reg_req) {
9ad13a42
IW
246 if (mad_reg_req->mgmt_class_version >= MAX_MGMT_VERSION) {
247 dev_notice(&device->dev,
248 "ib_register_mad_agent: invalid Class Version %u\n",
249 mad_reg_req->mgmt_class_version);
1da177e4 250 goto error1;
9ad13a42
IW
251 }
252 if (!recv_handler) {
253 dev_notice(&device->dev,
254 "ib_register_mad_agent: no recv_handler\n");
1da177e4 255 goto error1;
9ad13a42 256 }
1da177e4
LT
257 if (mad_reg_req->mgmt_class >= MAX_MGMT_CLASS) {
258 /*
259 * IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE is the only
260 * one in this range currently allowed
261 */
262 if (mad_reg_req->mgmt_class !=
9ad13a42
IW
263 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
264 dev_notice(&device->dev,
265 "ib_register_mad_agent: Invalid Mgmt Class 0x%x\n",
266 mad_reg_req->mgmt_class);
1da177e4 267 goto error1;
9ad13a42 268 }
1da177e4
LT
269 } else if (mad_reg_req->mgmt_class == 0) {
270 /*
271 * Class 0 is reserved in IBA and is used for
272 * aliasing of IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
273 */
9ad13a42
IW
274 dev_notice(&device->dev,
275 "ib_register_mad_agent: Invalid Mgmt Class 0\n");
1da177e4
LT
276 goto error1;
277 } else if (is_vendor_class(mad_reg_req->mgmt_class)) {
278 /*
279 * If class is in "new" vendor range,
280 * ensure supplied OUI is not zero
281 */
9ad13a42
IW
282 if (!is_vendor_oui(mad_reg_req->oui)) {
283 dev_notice(&device->dev,
284 "ib_register_mad_agent: No OUI specified for class 0x%x\n",
285 mad_reg_req->mgmt_class);
1da177e4 286 goto error1;
9ad13a42 287 }
1da177e4 288 }
618a3c03 289 /* Make sure class supplied is consistent with RMPP */
64cb9c6a 290 if (!ib_is_mad_class_rmpp(mad_reg_req->mgmt_class)) {
9ad13a42
IW
291 if (rmpp_version) {
292 dev_notice(&device->dev,
293 "ib_register_mad_agent: RMPP version for non-RMPP class 0x%x\n",
294 mad_reg_req->mgmt_class);
618a3c03 295 goto error1;
9ad13a42 296 }
618a3c03 297 }
1471cb6c 298
1da177e4
LT
299 /* Make sure class supplied is consistent with QP type */
300 if (qp_type == IB_QPT_SMI) {
301 if ((mad_reg_req->mgmt_class !=
302 IB_MGMT_CLASS_SUBN_LID_ROUTED) &&
303 (mad_reg_req->mgmt_class !=
9ad13a42
IW
304 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) {
305 dev_notice(&device->dev,
306 "ib_register_mad_agent: Invalid SM QP type: class 0x%x\n",
307 mad_reg_req->mgmt_class);
1da177e4 308 goto error1;
9ad13a42 309 }
1da177e4
LT
310 } else {
311 if ((mad_reg_req->mgmt_class ==
312 IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
313 (mad_reg_req->mgmt_class ==
9ad13a42
IW
314 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) {
315 dev_notice(&device->dev,
316 "ib_register_mad_agent: Invalid GS QP type: class 0x%x\n",
317 mad_reg_req->mgmt_class);
1da177e4 318 goto error1;
9ad13a42 319 }
1da177e4
LT
320 }
321 } else {
322 /* No registration request supplied */
323 if (!send_handler)
324 goto error1;
1471cb6c
IW
325 if (registration_flags & IB_MAD_USER_RMPP)
326 goto error1;
1da177e4
LT
327 }
328
329 /* Validate device and port */
330 port_priv = ib_get_mad_port(device, port_num);
331 if (!port_priv) {
9ad13a42 332 dev_notice(&device->dev, "ib_register_mad_agent: Invalid port\n");
1da177e4
LT
333 ret = ERR_PTR(-ENODEV);
334 goto error1;
335 }
336
c8367c4c
IW
337 /* Verify the QP requested is supported. For example, Ethernet devices
338 * will not have QP0 */
339 if (!port_priv->qp_info[qpn].qp) {
9ad13a42
IW
340 dev_notice(&device->dev,
341 "ib_register_mad_agent: QP %d not supported\n", qpn);
c8367c4c
IW
342 ret = ERR_PTR(-EPROTONOSUPPORT);
343 goto error1;
344 }
345
1da177e4 346 /* Allocate structures */
de6eb66b 347 mad_agent_priv = kzalloc(sizeof *mad_agent_priv, GFP_KERNEL);
1da177e4
LT
348 if (!mad_agent_priv) {
349 ret = ERR_PTR(-ENOMEM);
350 goto error1;
351 }
b82cab6b 352
1da177e4 353 if (mad_reg_req) {
9893e742 354 reg_req = kmemdup(mad_reg_req, sizeof *reg_req, GFP_KERNEL);
1da177e4
LT
355 if (!reg_req) {
356 ret = ERR_PTR(-ENOMEM);
b82cab6b 357 goto error3;
1da177e4 358 }
1da177e4
LT
359 }
360
361 /* Now, fill in the various structures */
1da177e4
LT
362 mad_agent_priv->qp_info = &port_priv->qp_info[qpn];
363 mad_agent_priv->reg_req = reg_req;
fa619a77 364 mad_agent_priv->agent.rmpp_version = rmpp_version;
1da177e4
LT
365 mad_agent_priv->agent.device = device;
366 mad_agent_priv->agent.recv_handler = recv_handler;
367 mad_agent_priv->agent.send_handler = send_handler;
368 mad_agent_priv->agent.context = context;
369 mad_agent_priv->agent.qp = port_priv->qp_info[qpn].qp;
370 mad_agent_priv->agent.port_num = port_num;
0f29b46d 371 mad_agent_priv->agent.flags = registration_flags;
d9620a4c
RC
372 spin_lock_init(&mad_agent_priv->lock);
373 INIT_LIST_HEAD(&mad_agent_priv->send_list);
374 INIT_LIST_HEAD(&mad_agent_priv->wait_list);
375 INIT_LIST_HEAD(&mad_agent_priv->done_list);
376 INIT_LIST_HEAD(&mad_agent_priv->rmpp_list);
377 INIT_DELAYED_WORK(&mad_agent_priv->timed_work, timeout_sends);
378 INIT_LIST_HEAD(&mad_agent_priv->local_list);
379 INIT_WORK(&mad_agent_priv->local_work, local_completions);
380 atomic_set(&mad_agent_priv->refcount, 1);
381 init_completion(&mad_agent_priv->comp);
1da177e4
LT
382
383 spin_lock_irqsave(&port_priv->reg_lock, flags);
384 mad_agent_priv->agent.hi_tid = ++ib_mad_client_id;
385
386 /*
387 * Make sure MAD registration (if supplied)
388 * is non overlapping with any existing ones
389 */
390 if (mad_reg_req) {
391 mgmt_class = convert_mgmt_class(mad_reg_req->mgmt_class);
392 if (!is_vendor_class(mgmt_class)) {
393 class = port_priv->version[mad_reg_req->
394 mgmt_class_version].class;
395 if (class) {
396 method = class->method_table[mgmt_class];
397 if (method) {
398 if (method_in_use(&method,
399 mad_reg_req))
b82cab6b 400 goto error4;
1da177e4
LT
401 }
402 }
403 ret2 = add_nonoui_reg_req(mad_reg_req, mad_agent_priv,
404 mgmt_class);
405 } else {
406 /* "New" vendor class range */
407 vendor = port_priv->version[mad_reg_req->
408 mgmt_class_version].vendor;
409 if (vendor) {
410 vclass = vendor_class_index(mgmt_class);
411 vendor_class = vendor->vendor_class[vclass];
412 if (vendor_class) {
413 if (is_vendor_method_in_use(
414 vendor_class,
415 mad_reg_req))
b82cab6b 416 goto error4;
1da177e4
LT
417 }
418 }
419 ret2 = add_oui_reg_req(mad_reg_req, mad_agent_priv);
420 }
421 if (ret2) {
422 ret = ERR_PTR(ret2);
b82cab6b 423 goto error4;
1da177e4
LT
424 }
425 }
426
427 /* Add mad agent into port's agent list */
428 list_add_tail(&mad_agent_priv->agent_list, &port_priv->agent_list);
429 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
430
1da177e4
LT
431 return &mad_agent_priv->agent;
432
b82cab6b 433error4:
1da177e4
LT
434 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
435 kfree(reg_req);
b82cab6b 436error3:
2012a116 437 kfree(mad_agent_priv);
1da177e4
LT
438error1:
439 return ret;
440}
441EXPORT_SYMBOL(ib_register_mad_agent);
442
443static inline int is_snooping_sends(int mad_snoop_flags)
444{
445 return (mad_snoop_flags &
446 (/*IB_MAD_SNOOP_POSTED_SENDS |
447 IB_MAD_SNOOP_RMPP_SENDS |*/
448 IB_MAD_SNOOP_SEND_COMPLETIONS /*|
449 IB_MAD_SNOOP_RMPP_SEND_COMPLETIONS*/));
450}
451
452static inline int is_snooping_recvs(int mad_snoop_flags)
453{
454 return (mad_snoop_flags &
455 (IB_MAD_SNOOP_RECVS /*|
456 IB_MAD_SNOOP_RMPP_RECVS*/));
457}
458
459static int register_snoop_agent(struct ib_mad_qp_info *qp_info,
460 struct ib_mad_snoop_private *mad_snoop_priv)
461{
462 struct ib_mad_snoop_private **new_snoop_table;
463 unsigned long flags;
464 int i;
465
466 spin_lock_irqsave(&qp_info->snoop_lock, flags);
467 /* Check for empty slot in array. */
468 for (i = 0; i < qp_info->snoop_table_size; i++)
469 if (!qp_info->snoop_table[i])
470 break;
471
472 if (i == qp_info->snoop_table_size) {
473 /* Grow table. */
52805174
RD
474 new_snoop_table = krealloc(qp_info->snoop_table,
475 sizeof mad_snoop_priv *
476 (qp_info->snoop_table_size + 1),
477 GFP_ATOMIC);
1da177e4
LT
478 if (!new_snoop_table) {
479 i = -ENOMEM;
480 goto out;
481 }
52805174 482
1da177e4
LT
483 qp_info->snoop_table = new_snoop_table;
484 qp_info->snoop_table_size++;
485 }
486 qp_info->snoop_table[i] = mad_snoop_priv;
487 atomic_inc(&qp_info->snoop_count);
488out:
489 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
490 return i;
491}
492
493struct ib_mad_agent *ib_register_mad_snoop(struct ib_device *device,
494 u8 port_num,
495 enum ib_qp_type qp_type,
496 int mad_snoop_flags,
497 ib_mad_snoop_handler snoop_handler,
498 ib_mad_recv_handler recv_handler,
499 void *context)
500{
501 struct ib_mad_port_private *port_priv;
502 struct ib_mad_agent *ret;
503 struct ib_mad_snoop_private *mad_snoop_priv;
504 int qpn;
505
506 /* Validate parameters */
507 if ((is_snooping_sends(mad_snoop_flags) && !snoop_handler) ||
508 (is_snooping_recvs(mad_snoop_flags) && !recv_handler)) {
509 ret = ERR_PTR(-EINVAL);
510 goto error1;
511 }
512 qpn = get_spl_qp_index(qp_type);
513 if (qpn == -1) {
514 ret = ERR_PTR(-EINVAL);
515 goto error1;
516 }
517 port_priv = ib_get_mad_port(device, port_num);
518 if (!port_priv) {
519 ret = ERR_PTR(-ENODEV);
520 goto error1;
521 }
522 /* Allocate structures */
de6eb66b 523 mad_snoop_priv = kzalloc(sizeof *mad_snoop_priv, GFP_KERNEL);
1da177e4
LT
524 if (!mad_snoop_priv) {
525 ret = ERR_PTR(-ENOMEM);
526 goto error1;
527 }
528
529 /* Now, fill in the various structures */
1da177e4
LT
530 mad_snoop_priv->qp_info = &port_priv->qp_info[qpn];
531 mad_snoop_priv->agent.device = device;
532 mad_snoop_priv->agent.recv_handler = recv_handler;
533 mad_snoop_priv->agent.snoop_handler = snoop_handler;
534 mad_snoop_priv->agent.context = context;
535 mad_snoop_priv->agent.qp = port_priv->qp_info[qpn].qp;
536 mad_snoop_priv->agent.port_num = port_num;
537 mad_snoop_priv->mad_snoop_flags = mad_snoop_flags;
1b52fa98 538 init_completion(&mad_snoop_priv->comp);
1da177e4
LT
539 mad_snoop_priv->snoop_index = register_snoop_agent(
540 &port_priv->qp_info[qpn],
541 mad_snoop_priv);
542 if (mad_snoop_priv->snoop_index < 0) {
543 ret = ERR_PTR(mad_snoop_priv->snoop_index);
544 goto error2;
545 }
546
547 atomic_set(&mad_snoop_priv->refcount, 1);
548 return &mad_snoop_priv->agent;
549
550error2:
551 kfree(mad_snoop_priv);
552error1:
553 return ret;
554}
555EXPORT_SYMBOL(ib_register_mad_snoop);
556
1b52fa98
SH
557static inline void deref_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
558{
559 if (atomic_dec_and_test(&mad_agent_priv->refcount))
560 complete(&mad_agent_priv->comp);
561}
562
563static inline void deref_snoop_agent(struct ib_mad_snoop_private *mad_snoop_priv)
564{
565 if (atomic_dec_and_test(&mad_snoop_priv->refcount))
566 complete(&mad_snoop_priv->comp);
567}
568
1da177e4
LT
569static void unregister_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
570{
571 struct ib_mad_port_private *port_priv;
572 unsigned long flags;
573
574 /* Note that we could still be handling received MADs */
575
576 /*
577 * Canceling all sends results in dropping received response
578 * MADs, preventing us from queuing additional work
579 */
580 cancel_mads(mad_agent_priv);
1da177e4 581 port_priv = mad_agent_priv->qp_info->port_priv;
1da177e4 582 cancel_delayed_work(&mad_agent_priv->timed_work);
1da177e4
LT
583
584 spin_lock_irqsave(&port_priv->reg_lock, flags);
585 remove_mad_reg_req(mad_agent_priv);
586 list_del(&mad_agent_priv->agent_list);
587 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
588
b82cab6b 589 flush_workqueue(port_priv->wq);
fa619a77 590 ib_cancel_rmpp_recvs(mad_agent_priv);
1da177e4 591
1b52fa98
SH
592 deref_mad_agent(mad_agent_priv);
593 wait_for_completion(&mad_agent_priv->comp);
1da177e4 594
6044ec88 595 kfree(mad_agent_priv->reg_req);
1da177e4
LT
596 kfree(mad_agent_priv);
597}
598
599static void unregister_mad_snoop(struct ib_mad_snoop_private *mad_snoop_priv)
600{
601 struct ib_mad_qp_info *qp_info;
602 unsigned long flags;
603
604 qp_info = mad_snoop_priv->qp_info;
605 spin_lock_irqsave(&qp_info->snoop_lock, flags);
606 qp_info->snoop_table[mad_snoop_priv->snoop_index] = NULL;
607 atomic_dec(&qp_info->snoop_count);
608 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
609
1b52fa98
SH
610 deref_snoop_agent(mad_snoop_priv);
611 wait_for_completion(&mad_snoop_priv->comp);
1da177e4
LT
612
613 kfree(mad_snoop_priv);
614}
615
616/*
617 * ib_unregister_mad_agent - Unregisters a client from using MAD services
618 */
619int ib_unregister_mad_agent(struct ib_mad_agent *mad_agent)
620{
621 struct ib_mad_agent_private *mad_agent_priv;
622 struct ib_mad_snoop_private *mad_snoop_priv;
623
624 /* If the TID is zero, the agent can only snoop. */
625 if (mad_agent->hi_tid) {
626 mad_agent_priv = container_of(mad_agent,
627 struct ib_mad_agent_private,
628 agent);
629 unregister_mad_agent(mad_agent_priv);
630 } else {
631 mad_snoop_priv = container_of(mad_agent,
632 struct ib_mad_snoop_private,
633 agent);
634 unregister_mad_snoop(mad_snoop_priv);
635 }
636 return 0;
637}
638EXPORT_SYMBOL(ib_unregister_mad_agent);
639
640static void dequeue_mad(struct ib_mad_list_head *mad_list)
641{
642 struct ib_mad_queue *mad_queue;
643 unsigned long flags;
644
645 BUG_ON(!mad_list->mad_queue);
646 mad_queue = mad_list->mad_queue;
647 spin_lock_irqsave(&mad_queue->lock, flags);
648 list_del(&mad_list->list);
649 mad_queue->count--;
650 spin_unlock_irqrestore(&mad_queue->lock, flags);
651}
652
653static void snoop_send(struct ib_mad_qp_info *qp_info,
34816ad9 654 struct ib_mad_send_buf *send_buf,
1da177e4
LT
655 struct ib_mad_send_wc *mad_send_wc,
656 int mad_snoop_flags)
657{
658 struct ib_mad_snoop_private *mad_snoop_priv;
659 unsigned long flags;
660 int i;
661
662 spin_lock_irqsave(&qp_info->snoop_lock, flags);
663 for (i = 0; i < qp_info->snoop_table_size; i++) {
664 mad_snoop_priv = qp_info->snoop_table[i];
665 if (!mad_snoop_priv ||
666 !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
667 continue;
668
669 atomic_inc(&mad_snoop_priv->refcount);
670 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
671 mad_snoop_priv->agent.snoop_handler(&mad_snoop_priv->agent,
34816ad9 672 send_buf, mad_send_wc);
1b52fa98 673 deref_snoop_agent(mad_snoop_priv);
1da177e4
LT
674 spin_lock_irqsave(&qp_info->snoop_lock, flags);
675 }
676 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
677}
678
679static void snoop_recv(struct ib_mad_qp_info *qp_info,
680 struct ib_mad_recv_wc *mad_recv_wc,
681 int mad_snoop_flags)
682{
683 struct ib_mad_snoop_private *mad_snoop_priv;
684 unsigned long flags;
685 int i;
686
687 spin_lock_irqsave(&qp_info->snoop_lock, flags);
688 for (i = 0; i < qp_info->snoop_table_size; i++) {
689 mad_snoop_priv = qp_info->snoop_table[i];
690 if (!mad_snoop_priv ||
691 !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
692 continue;
693
694 atomic_inc(&mad_snoop_priv->refcount);
695 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
ca281265 696 mad_snoop_priv->agent.recv_handler(&mad_snoop_priv->agent, NULL,
1da177e4 697 mad_recv_wc);
1b52fa98 698 deref_snoop_agent(mad_snoop_priv);
1da177e4
LT
699 spin_lock_irqsave(&qp_info->snoop_lock, flags);
700 }
701 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
702}
703
062dbb69
MT
704static void build_smp_wc(struct ib_qp *qp,
705 u64 wr_id, u16 slid, u16 pkey_index, u8 port_num,
1da177e4
LT
706 struct ib_wc *wc)
707{
708 memset(wc, 0, sizeof *wc);
709 wc->wr_id = wr_id;
710 wc->status = IB_WC_SUCCESS;
711 wc->opcode = IB_WC_RECV;
712 wc->pkey_index = pkey_index;
713 wc->byte_len = sizeof(struct ib_mad) + sizeof(struct ib_grh);
714 wc->src_qp = IB_QP0;
062dbb69 715 wc->qp = qp;
1da177e4
LT
716 wc->slid = slid;
717 wc->sl = 0;
718 wc->dlid_path_bits = 0;
719 wc->port_num = port_num;
720}
721
c9082e51
IW
722static size_t mad_priv_size(const struct ib_mad_private *mp)
723{
724 return sizeof(struct ib_mad_private) + mp->mad_size;
725}
726
727static struct ib_mad_private *alloc_mad_private(size_t mad_size, gfp_t flags)
728{
729 size_t size = sizeof(struct ib_mad_private) + mad_size;
730 struct ib_mad_private *ret = kzalloc(size, flags);
731
732 if (ret)
733 ret->mad_size = mad_size;
734
735 return ret;
736}
737
738static size_t port_mad_size(const struct ib_mad_port_private *port_priv)
739{
740 return rdma_max_mad_size(port_priv->device, port_priv->port_num);
741}
742
743static size_t mad_priv_dma_size(const struct ib_mad_private *mp)
744{
745 return sizeof(struct ib_grh) + mp->mad_size;
746}
747
1da177e4
LT
748/*
749 * Return 0 if SMP is to be sent
750 * Return 1 if SMP was consumed locally (whether or not solicited)
751 * Return < 0 if error
752 */
753static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
34816ad9 754 struct ib_mad_send_wr_private *mad_send_wr)
1da177e4 755{
de493d47 756 int ret = 0;
34816ad9 757 struct ib_smp *smp = mad_send_wr->send_buf.mad;
8e4349d1 758 struct opa_smp *opa_smp = (struct opa_smp *)smp;
1da177e4
LT
759 unsigned long flags;
760 struct ib_mad_local_private *local;
761 struct ib_mad_private *mad_priv;
762 struct ib_mad_port_private *port_priv;
763 struct ib_mad_agent_private *recv_mad_agent = NULL;
764 struct ib_device *device = mad_agent_priv->agent.device;
1bae4dbf 765 u8 port_num;
1da177e4 766 struct ib_wc mad_wc;
e622f2f4 767 struct ib_ud_wr *send_wr = &mad_send_wr->send_wr;
c9082e51 768 size_t mad_size = port_mad_size(mad_agent_priv->qp_info->port_priv);
4cd7c947 769 u16 out_mad_pkey_index = 0;
8e4349d1
IW
770 u16 drslid;
771 bool opa = rdma_cap_opa_mad(mad_agent_priv->qp_info->port_priv->device,
772 mad_agent_priv->qp_info->port_priv->port_num);
1da177e4 773
4139032b 774 if (rdma_cap_ib_switch(device) &&
1bae4dbf 775 smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
e622f2f4 776 port_num = send_wr->port_num;
1bae4dbf
HR
777 else
778 port_num = mad_agent_priv->agent.port_num;
779
8cf3f04f
RC
780 /*
781 * Directed route handling starts if the initial LID routed part of
782 * a request or the ending LID routed part of a response is empty.
783 * If we are at the start of the LID routed part, don't update the
784 * hop_ptr or hop_cnt. See section 14.2.2, Vol 1 IB spec.
785 */
8e4349d1
IW
786 if (opa && smp->class_version == OPA_SMP_CLASS_VERSION) {
787 u32 opa_drslid;
788
789 if ((opa_get_smp_direction(opa_smp)
790 ? opa_smp->route.dr.dr_dlid : opa_smp->route.dr.dr_slid) ==
791 OPA_LID_PERMISSIVE &&
4139032b
HR
792 opa_smi_handle_dr_smp_send(opa_smp,
793 rdma_cap_ib_switch(device),
8e4349d1
IW
794 port_num) == IB_SMI_DISCARD) {
795 ret = -EINVAL;
796 dev_err(&device->dev, "OPA Invalid directed route\n");
797 goto out;
798 }
799 opa_drslid = be32_to_cpu(opa_smp->route.dr.dr_slid);
cd4cd565 800 if (opa_drslid != be32_to_cpu(OPA_LID_PERMISSIVE) &&
8e4349d1
IW
801 opa_drslid & 0xffff0000) {
802 ret = -EINVAL;
803 dev_err(&device->dev, "OPA Invalid dr_slid 0x%x\n",
804 opa_drslid);
805 goto out;
806 }
807 drslid = (u16)(opa_drslid & 0x0000ffff);
de493d47 808
8e4349d1
IW
809 /* Check to post send on QP or process locally */
810 if (opa_smi_check_local_smp(opa_smp, device) == IB_SMI_DISCARD &&
811 opa_smi_check_local_returning_smp(opa_smp, device) == IB_SMI_DISCARD)
812 goto out;
813 } else {
814 if ((ib_get_smp_direction(smp) ? smp->dr_dlid : smp->dr_slid) ==
815 IB_LID_PERMISSIVE &&
4139032b 816 smi_handle_dr_smp_send(smp, rdma_cap_ib_switch(device), port_num) ==
8e4349d1
IW
817 IB_SMI_DISCARD) {
818 ret = -EINVAL;
819 dev_err(&device->dev, "Invalid directed route\n");
820 goto out;
821 }
822 drslid = be16_to_cpu(smp->dr_slid);
823
824 /* Check to post send on QP or process locally */
825 if (smi_check_local_smp(smp, device) == IB_SMI_DISCARD &&
826 smi_check_local_returning_smp(smp, device) == IB_SMI_DISCARD)
827 goto out;
828 }
1da177e4
LT
829
830 local = kmalloc(sizeof *local, GFP_ATOMIC);
831 if (!local) {
832 ret = -ENOMEM;
7ef5d4b0 833 dev_err(&device->dev, "No memory for ib_mad_local_private\n");
1da177e4
LT
834 goto out;
835 }
836 local->mad_priv = NULL;
837 local->recv_mad_agent = NULL;
c9082e51 838 mad_priv = alloc_mad_private(mad_size, GFP_ATOMIC);
1da177e4
LT
839 if (!mad_priv) {
840 ret = -ENOMEM;
7ef5d4b0 841 dev_err(&device->dev, "No memory for local response MAD\n");
1da177e4
LT
842 kfree(local);
843 goto out;
844 }
845
062dbb69 846 build_smp_wc(mad_agent_priv->agent.qp,
e622f2f4
CH
847 send_wr->wr.wr_id, drslid,
848 send_wr->pkey_index,
849 send_wr->port_num, &mad_wc);
1da177e4 850
8e4349d1
IW
851 if (opa && smp->base_version == OPA_MGMT_BASE_VERSION) {
852 mad_wc.byte_len = mad_send_wr->send_buf.hdr_len
853 + mad_send_wr->send_buf.data_len
854 + sizeof(struct ib_grh);
855 }
856
1da177e4
LT
857 /* No GRH for DR SMP */
858 ret = device->process_mad(device, 0, port_num, &mad_wc, NULL,
4cd7c947
IW
859 (const struct ib_mad_hdr *)smp, mad_size,
860 (struct ib_mad_hdr *)mad_priv->mad,
861 &mad_size, &out_mad_pkey_index);
1da177e4
LT
862 switch (ret)
863 {
864 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY:
c9082e51 865 if (ib_response_mad((const struct ib_mad_hdr *)mad_priv->mad) &&
1da177e4
LT
866 mad_agent_priv->agent.recv_handler) {
867 local->mad_priv = mad_priv;
868 local->recv_mad_agent = mad_agent_priv;
869 /*
870 * Reference MAD agent until receive
871 * side of local completion handled
872 */
873 atomic_inc(&mad_agent_priv->refcount);
874 } else
c9082e51 875 kfree(mad_priv);
1da177e4
LT
876 break;
877 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED:
c9082e51 878 kfree(mad_priv);
4780c195 879 break;
1da177e4
LT
880 case IB_MAD_RESULT_SUCCESS:
881 /* Treat like an incoming receive MAD */
1da177e4
LT
882 port_priv = ib_get_mad_port(mad_agent_priv->agent.device,
883 mad_agent_priv->agent.port_num);
884 if (port_priv) {
c9082e51 885 memcpy(mad_priv->mad, smp, mad_priv->mad_size);
1da177e4 886 recv_mad_agent = find_mad_agent(port_priv,
c9082e51 887 (const struct ib_mad_hdr *)mad_priv->mad);
1da177e4
LT
888 }
889 if (!port_priv || !recv_mad_agent) {
4780c195
RC
890 /*
891 * No receiving agent so drop packet and
892 * generate send completion.
893 */
c9082e51 894 kfree(mad_priv);
4780c195 895 break;
1da177e4
LT
896 }
897 local->mad_priv = mad_priv;
898 local->recv_mad_agent = recv_mad_agent;
899 break;
900 default:
c9082e51 901 kfree(mad_priv);
1da177e4
LT
902 kfree(local);
903 ret = -EINVAL;
904 goto out;
905 }
906
34816ad9 907 local->mad_send_wr = mad_send_wr;
8e4349d1 908 if (opa) {
e622f2f4 909 local->mad_send_wr->send_wr.pkey_index = out_mad_pkey_index;
8e4349d1
IW
910 local->return_wc_byte_len = mad_size;
911 }
1da177e4
LT
912 /* Reference MAD agent until send side of local completion handled */
913 atomic_inc(&mad_agent_priv->refcount);
914 /* Queue local completion to local list */
915 spin_lock_irqsave(&mad_agent_priv->lock, flags);
916 list_add_tail(&local->completion_list, &mad_agent_priv->local_list);
917 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
918 queue_work(mad_agent_priv->qp_info->port_priv->wq,
b82cab6b 919 &mad_agent_priv->local_work);
1da177e4
LT
920 ret = 1;
921out:
922 return ret;
923}
924
548ead17 925static int get_pad_size(int hdr_len, int data_len, size_t mad_size)
824c8ae7
HR
926{
927 int seg_size, pad;
928
548ead17 929 seg_size = mad_size - hdr_len;
824c8ae7
HR
930 if (data_len && seg_size) {
931 pad = seg_size - data_len % seg_size;
f36e1793 932 return pad == seg_size ? 0 : pad;
824c8ae7 933 } else
f36e1793
JM
934 return seg_size;
935}
936
937static void free_send_rmpp_list(struct ib_mad_send_wr_private *mad_send_wr)
938{
939 struct ib_rmpp_segment *s, *t;
940
941 list_for_each_entry_safe(s, t, &mad_send_wr->rmpp_list, list) {
942 list_del(&s->list);
943 kfree(s);
944 }
945}
946
947static int alloc_send_rmpp_list(struct ib_mad_send_wr_private *send_wr,
548ead17 948 size_t mad_size, gfp_t gfp_mask)
f36e1793
JM
949{
950 struct ib_mad_send_buf *send_buf = &send_wr->send_buf;
951 struct ib_rmpp_mad *rmpp_mad = send_buf->mad;
952 struct ib_rmpp_segment *seg = NULL;
953 int left, seg_size, pad;
954
548ead17
IW
955 send_buf->seg_size = mad_size - send_buf->hdr_len;
956 send_buf->seg_rmpp_size = mad_size - IB_MGMT_RMPP_HDR;
f36e1793
JM
957 seg_size = send_buf->seg_size;
958 pad = send_wr->pad;
959
960 /* Allocate data segments. */
961 for (left = send_buf->data_len + pad; left > 0; left -= seg_size) {
962 seg = kmalloc(sizeof (*seg) + seg_size, gfp_mask);
963 if (!seg) {
7ef5d4b0
IW
964 dev_err(&send_buf->mad_agent->device->dev,
965 "alloc_send_rmpp_segs: RMPP mem alloc failed for len %zd, gfp %#x\n",
966 sizeof (*seg) + seg_size, gfp_mask);
f36e1793
JM
967 free_send_rmpp_list(send_wr);
968 return -ENOMEM;
969 }
970 seg->num = ++send_buf->seg_count;
971 list_add_tail(&seg->list, &send_wr->rmpp_list);
972 }
973
974 /* Zero any padding */
975 if (pad)
976 memset(seg->data + seg_size - pad, 0, pad);
977
978 rmpp_mad->rmpp_hdr.rmpp_version = send_wr->mad_agent_priv->
979 agent.rmpp_version;
980 rmpp_mad->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_DATA;
981 ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
982
983 send_wr->cur_seg = container_of(send_wr->rmpp_list.next,
984 struct ib_rmpp_segment, list);
985 send_wr->last_ack_seg = send_wr->cur_seg;
986 return 0;
824c8ae7
HR
987}
988
f766c58f 989int ib_mad_kernel_rmpp_agent(const struct ib_mad_agent *agent)
1471cb6c
IW
990{
991 return agent->rmpp_version && !(agent->flags & IB_MAD_USER_RMPP);
992}
993EXPORT_SYMBOL(ib_mad_kernel_rmpp_agent);
994
824c8ae7
HR
995struct ib_mad_send_buf * ib_create_send_mad(struct ib_mad_agent *mad_agent,
996 u32 remote_qpn, u16 pkey_index,
34816ad9 997 int rmpp_active,
824c8ae7 998 int hdr_len, int data_len,
da2dfaa3
IW
999 gfp_t gfp_mask,
1000 u8 base_version)
824c8ae7
HR
1001{
1002 struct ib_mad_agent_private *mad_agent_priv;
34816ad9 1003 struct ib_mad_send_wr_private *mad_send_wr;
f36e1793 1004 int pad, message_size, ret, size;
824c8ae7 1005 void *buf;
548ead17
IW
1006 size_t mad_size;
1007 bool opa;
824c8ae7 1008
34816ad9
SH
1009 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
1010 agent);
548ead17
IW
1011
1012 opa = rdma_cap_opa_mad(mad_agent->device, mad_agent->port_num);
1013
1014 if (opa && base_version == OPA_MGMT_BASE_VERSION)
1015 mad_size = sizeof(struct opa_mad);
1016 else
1017 mad_size = sizeof(struct ib_mad);
1018
1019 pad = get_pad_size(hdr_len, data_len, mad_size);
f36e1793 1020 message_size = hdr_len + data_len + pad;
824c8ae7 1021
1471cb6c 1022 if (ib_mad_kernel_rmpp_agent(mad_agent)) {
548ead17 1023 if (!rmpp_active && message_size > mad_size)
1471cb6c
IW
1024 return ERR_PTR(-EINVAL);
1025 } else
548ead17 1026 if (rmpp_active || message_size > mad_size)
1471cb6c 1027 return ERR_PTR(-EINVAL);
fa619a77 1028
548ead17 1029 size = rmpp_active ? hdr_len : mad_size;
f36e1793 1030 buf = kzalloc(sizeof *mad_send_wr + size, gfp_mask);
824c8ae7
HR
1031 if (!buf)
1032 return ERR_PTR(-ENOMEM);
34816ad9 1033
f36e1793
JM
1034 mad_send_wr = buf + size;
1035 INIT_LIST_HEAD(&mad_send_wr->rmpp_list);
34816ad9 1036 mad_send_wr->send_buf.mad = buf;
f36e1793
JM
1037 mad_send_wr->send_buf.hdr_len = hdr_len;
1038 mad_send_wr->send_buf.data_len = data_len;
1039 mad_send_wr->pad = pad;
34816ad9
SH
1040
1041 mad_send_wr->mad_agent_priv = mad_agent_priv;
f36e1793 1042 mad_send_wr->sg_list[0].length = hdr_len;
4be90bc6 1043 mad_send_wr->sg_list[0].lkey = mad_agent->qp->pd->local_dma_lkey;
548ead17
IW
1044
1045 /* OPA MADs don't have to be the full 2048 bytes */
1046 if (opa && base_version == OPA_MGMT_BASE_VERSION &&
1047 data_len < mad_size - hdr_len)
1048 mad_send_wr->sg_list[1].length = data_len;
1049 else
1050 mad_send_wr->sg_list[1].length = mad_size - hdr_len;
1051
4be90bc6 1052 mad_send_wr->sg_list[1].lkey = mad_agent->qp->pd->local_dma_lkey;
34816ad9 1053
e622f2f4
CH
1054 mad_send_wr->send_wr.wr.wr_id = (unsigned long) mad_send_wr;
1055 mad_send_wr->send_wr.wr.sg_list = mad_send_wr->sg_list;
1056 mad_send_wr->send_wr.wr.num_sge = 2;
1057 mad_send_wr->send_wr.wr.opcode = IB_WR_SEND;
1058 mad_send_wr->send_wr.wr.send_flags = IB_SEND_SIGNALED;
1059 mad_send_wr->send_wr.remote_qpn = remote_qpn;
1060 mad_send_wr->send_wr.remote_qkey = IB_QP_SET_QKEY;
1061 mad_send_wr->send_wr.pkey_index = pkey_index;
fa619a77
HR
1062
1063 if (rmpp_active) {
548ead17 1064 ret = alloc_send_rmpp_list(mad_send_wr, mad_size, gfp_mask);
f36e1793
JM
1065 if (ret) {
1066 kfree(buf);
1067 return ERR_PTR(ret);
1068 }
fa619a77
HR
1069 }
1070
34816ad9 1071 mad_send_wr->send_buf.mad_agent = mad_agent;
824c8ae7 1072 atomic_inc(&mad_agent_priv->refcount);
34816ad9 1073 return &mad_send_wr->send_buf;
824c8ae7
HR
1074}
1075EXPORT_SYMBOL(ib_create_send_mad);
1076
618a3c03
HR
1077int ib_get_mad_data_offset(u8 mgmt_class)
1078{
1079 if (mgmt_class == IB_MGMT_CLASS_SUBN_ADM)
1080 return IB_MGMT_SA_HDR;
1081 else if ((mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
1082 (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
1083 (mgmt_class == IB_MGMT_CLASS_BIS))
1084 return IB_MGMT_DEVICE_HDR;
1085 else if ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
1086 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END))
1087 return IB_MGMT_VENDOR_HDR;
1088 else
1089 return IB_MGMT_MAD_HDR;
1090}
1091EXPORT_SYMBOL(ib_get_mad_data_offset);
1092
1093int ib_is_mad_class_rmpp(u8 mgmt_class)
1094{
1095 if ((mgmt_class == IB_MGMT_CLASS_SUBN_ADM) ||
1096 (mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
1097 (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
1098 (mgmt_class == IB_MGMT_CLASS_BIS) ||
1099 ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
1100 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END)))
1101 return 1;
1102 return 0;
1103}
1104EXPORT_SYMBOL(ib_is_mad_class_rmpp);
1105
f36e1793
JM
1106void *ib_get_rmpp_segment(struct ib_mad_send_buf *send_buf, int seg_num)
1107{
1108 struct ib_mad_send_wr_private *mad_send_wr;
1109 struct list_head *list;
1110
1111 mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
1112 send_buf);
1113 list = &mad_send_wr->cur_seg->list;
1114
1115 if (mad_send_wr->cur_seg->num < seg_num) {
1116 list_for_each_entry(mad_send_wr->cur_seg, list, list)
1117 if (mad_send_wr->cur_seg->num == seg_num)
1118 break;
1119 } else if (mad_send_wr->cur_seg->num > seg_num) {
1120 list_for_each_entry_reverse(mad_send_wr->cur_seg, list, list)
1121 if (mad_send_wr->cur_seg->num == seg_num)
1122 break;
1123 }
1124 return mad_send_wr->cur_seg->data;
1125}
1126EXPORT_SYMBOL(ib_get_rmpp_segment);
1127
1128static inline void *ib_get_payload(struct ib_mad_send_wr_private *mad_send_wr)
1129{
1130 if (mad_send_wr->send_buf.seg_count)
1131 return ib_get_rmpp_segment(&mad_send_wr->send_buf,
1132 mad_send_wr->seg_num);
1133 else
1134 return mad_send_wr->send_buf.mad +
1135 mad_send_wr->send_buf.hdr_len;
1136}
1137
824c8ae7
HR
1138void ib_free_send_mad(struct ib_mad_send_buf *send_buf)
1139{
1140 struct ib_mad_agent_private *mad_agent_priv;
f36e1793 1141 struct ib_mad_send_wr_private *mad_send_wr;
824c8ae7
HR
1142
1143 mad_agent_priv = container_of(send_buf->mad_agent,
1144 struct ib_mad_agent_private, agent);
f36e1793
JM
1145 mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
1146 send_buf);
824c8ae7 1147
f36e1793
JM
1148 free_send_rmpp_list(mad_send_wr);
1149 kfree(send_buf->mad);
1b52fa98 1150 deref_mad_agent(mad_agent_priv);
824c8ae7
HR
1151}
1152EXPORT_SYMBOL(ib_free_send_mad);
1153
fa619a77 1154int ib_send_mad(struct ib_mad_send_wr_private *mad_send_wr)
1da177e4
LT
1155{
1156 struct ib_mad_qp_info *qp_info;
cabe3cbc 1157 struct list_head *list;
34816ad9
SH
1158 struct ib_send_wr *bad_send_wr;
1159 struct ib_mad_agent *mad_agent;
1160 struct ib_sge *sge;
1da177e4
LT
1161 unsigned long flags;
1162 int ret;
1163
f8197a4e 1164 /* Set WR ID to find mad_send_wr upon completion */
d760ce8f 1165 qp_info = mad_send_wr->mad_agent_priv->qp_info;
e622f2f4 1166 mad_send_wr->send_wr.wr.wr_id = (unsigned long)&mad_send_wr->mad_list;
1da177e4
LT
1167 mad_send_wr->mad_list.mad_queue = &qp_info->send_queue;
1168
34816ad9
SH
1169 mad_agent = mad_send_wr->send_buf.mad_agent;
1170 sge = mad_send_wr->sg_list;
1527106f
RC
1171 sge[0].addr = ib_dma_map_single(mad_agent->device,
1172 mad_send_wr->send_buf.mad,
1173 sge[0].length,
1174 DMA_TO_DEVICE);
2c34e68f
YB
1175 if (unlikely(ib_dma_mapping_error(mad_agent->device, sge[0].addr)))
1176 return -ENOMEM;
1177
1527106f
RC
1178 mad_send_wr->header_mapping = sge[0].addr;
1179
1180 sge[1].addr = ib_dma_map_single(mad_agent->device,
1181 ib_get_payload(mad_send_wr),
1182 sge[1].length,
1183 DMA_TO_DEVICE);
2c34e68f
YB
1184 if (unlikely(ib_dma_mapping_error(mad_agent->device, sge[1].addr))) {
1185 ib_dma_unmap_single(mad_agent->device,
1186 mad_send_wr->header_mapping,
1187 sge[0].length, DMA_TO_DEVICE);
1188 return -ENOMEM;
1189 }
1527106f 1190 mad_send_wr->payload_mapping = sge[1].addr;
34816ad9 1191
1da177e4 1192 spin_lock_irqsave(&qp_info->send_queue.lock, flags);
cabe3cbc 1193 if (qp_info->send_queue.count < qp_info->send_queue.max_active) {
e622f2f4 1194 ret = ib_post_send(mad_agent->qp, &mad_send_wr->send_wr.wr,
34816ad9 1195 &bad_send_wr);
cabe3cbc 1196 list = &qp_info->send_queue.list;
1da177e4 1197 } else {
1da177e4 1198 ret = 0;
cabe3cbc 1199 list = &qp_info->overflow_list;
1da177e4 1200 }
cabe3cbc
HR
1201
1202 if (!ret) {
1203 qp_info->send_queue.count++;
1204 list_add_tail(&mad_send_wr->mad_list.list, list);
1205 }
1206 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
f36e1793 1207 if (ret) {
1527106f
RC
1208 ib_dma_unmap_single(mad_agent->device,
1209 mad_send_wr->header_mapping,
1210 sge[0].length, DMA_TO_DEVICE);
1211 ib_dma_unmap_single(mad_agent->device,
1212 mad_send_wr->payload_mapping,
1213 sge[1].length, DMA_TO_DEVICE);
f36e1793 1214 }
1da177e4
LT
1215 return ret;
1216}
1217
1218/*
1219 * ib_post_send_mad - Posts MAD(s) to the send queue of the QP associated
1220 * with the registered client
1221 */
34816ad9
SH
1222int ib_post_send_mad(struct ib_mad_send_buf *send_buf,
1223 struct ib_mad_send_buf **bad_send_buf)
1da177e4 1224{
1da177e4 1225 struct ib_mad_agent_private *mad_agent_priv;
34816ad9
SH
1226 struct ib_mad_send_buf *next_send_buf;
1227 struct ib_mad_send_wr_private *mad_send_wr;
1228 unsigned long flags;
1229 int ret = -EINVAL;
1da177e4
LT
1230
1231 /* Walk list of send WRs and post each on send list */
34816ad9 1232 for (; send_buf; send_buf = next_send_buf) {
1da177e4 1233
34816ad9
SH
1234 mad_send_wr = container_of(send_buf,
1235 struct ib_mad_send_wr_private,
1236 send_buf);
1237 mad_agent_priv = mad_send_wr->mad_agent_priv;
1da177e4 1238
34816ad9
SH
1239 if (!send_buf->mad_agent->send_handler ||
1240 (send_buf->timeout_ms &&
1241 !send_buf->mad_agent->recv_handler)) {
1242 ret = -EINVAL;
1243 goto error;
1da177e4
LT
1244 }
1245
618a3c03
HR
1246 if (!ib_is_mad_class_rmpp(((struct ib_mad_hdr *) send_buf->mad)->mgmt_class)) {
1247 if (mad_agent_priv->agent.rmpp_version) {
1248 ret = -EINVAL;
1249 goto error;
1250 }
1251 }
1252
1da177e4
LT
1253 /*
1254 * Save pointer to next work request to post in case the
1255 * current one completes, and the user modifies the work
1256 * request associated with the completion
1257 */
34816ad9 1258 next_send_buf = send_buf->next;
e622f2f4 1259 mad_send_wr->send_wr.ah = send_buf->ah;
1da177e4 1260
34816ad9
SH
1261 if (((struct ib_mad_hdr *) send_buf->mad)->mgmt_class ==
1262 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
1263 ret = handle_outgoing_dr_smp(mad_agent_priv,
1264 mad_send_wr);
1da177e4 1265 if (ret < 0) /* error */
34816ad9 1266 goto error;
1da177e4 1267 else if (ret == 1) /* locally consumed */
34816ad9 1268 continue;
1da177e4
LT
1269 }
1270
34816ad9 1271 mad_send_wr->tid = ((struct ib_mad_hdr *) send_buf->mad)->tid;
1da177e4 1272 /* Timeout will be updated after send completes */
34816ad9 1273 mad_send_wr->timeout = msecs_to_jiffies(send_buf->timeout_ms);
4fc8cd49
SH
1274 mad_send_wr->max_retries = send_buf->retries;
1275 mad_send_wr->retries_left = send_buf->retries;
1276 send_buf->retries = 0;
34816ad9 1277 /* Reference for work request to QP + response */
1da177e4
LT
1278 mad_send_wr->refcount = 1 + (mad_send_wr->timeout > 0);
1279 mad_send_wr->status = IB_WC_SUCCESS;
1280
1281 /* Reference MAD agent until send completes */
1282 atomic_inc(&mad_agent_priv->refcount);
1283 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1284 list_add_tail(&mad_send_wr->agent_list,
1285 &mad_agent_priv->send_list);
1286 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1287
1471cb6c 1288 if (ib_mad_kernel_rmpp_agent(&mad_agent_priv->agent)) {
fa619a77
HR
1289 ret = ib_send_rmpp_mad(mad_send_wr);
1290 if (ret >= 0 && ret != IB_RMPP_RESULT_CONSUMED)
1291 ret = ib_send_mad(mad_send_wr);
1292 } else
1293 ret = ib_send_mad(mad_send_wr);
1294 if (ret < 0) {
1da177e4
LT
1295 /* Fail send request */
1296 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1297 list_del(&mad_send_wr->agent_list);
1298 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1299 atomic_dec(&mad_agent_priv->refcount);
34816ad9 1300 goto error;
1da177e4 1301 }
1da177e4
LT
1302 }
1303 return 0;
34816ad9
SH
1304error:
1305 if (bad_send_buf)
1306 *bad_send_buf = send_buf;
1da177e4
LT
1307 return ret;
1308}
1309EXPORT_SYMBOL(ib_post_send_mad);
1310
1311/*
1312 * ib_free_recv_mad - Returns data buffers used to receive
1313 * a MAD to the access layer
1314 */
1315void ib_free_recv_mad(struct ib_mad_recv_wc *mad_recv_wc)
1316{
fa619a77 1317 struct ib_mad_recv_buf *mad_recv_buf, *temp_recv_buf;
1da177e4
LT
1318 struct ib_mad_private_header *mad_priv_hdr;
1319 struct ib_mad_private *priv;
fa619a77 1320 struct list_head free_list;
1da177e4 1321
fa619a77
HR
1322 INIT_LIST_HEAD(&free_list);
1323 list_splice_init(&mad_recv_wc->rmpp_list, &free_list);
1da177e4 1324
fa619a77
HR
1325 list_for_each_entry_safe(mad_recv_buf, temp_recv_buf,
1326 &free_list, list) {
1327 mad_recv_wc = container_of(mad_recv_buf, struct ib_mad_recv_wc,
1328 recv_buf);
1da177e4
LT
1329 mad_priv_hdr = container_of(mad_recv_wc,
1330 struct ib_mad_private_header,
1331 recv_wc);
1332 priv = container_of(mad_priv_hdr, struct ib_mad_private,
1333 header);
c9082e51 1334 kfree(priv);
1da177e4 1335 }
1da177e4
LT
1336}
1337EXPORT_SYMBOL(ib_free_recv_mad);
1338
1da177e4
LT
1339struct ib_mad_agent *ib_redirect_mad_qp(struct ib_qp *qp,
1340 u8 rmpp_version,
1341 ib_mad_send_handler send_handler,
1342 ib_mad_recv_handler recv_handler,
1343 void *context)
1344{
1345 return ERR_PTR(-EINVAL); /* XXX: for now */
1346}
1347EXPORT_SYMBOL(ib_redirect_mad_qp);
1348
1349int ib_process_mad_wc(struct ib_mad_agent *mad_agent,
1350 struct ib_wc *wc)
1351{
7ef5d4b0
IW
1352 dev_err(&mad_agent->device->dev,
1353 "ib_process_mad_wc() not implemented yet\n");
1da177e4
LT
1354 return 0;
1355}
1356EXPORT_SYMBOL(ib_process_mad_wc);
1357
1358static int method_in_use(struct ib_mad_mgmt_method_table **method,
1359 struct ib_mad_reg_req *mad_reg_req)
1360{
1361 int i;
1362
19b629f5 1363 for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS) {
1da177e4 1364 if ((*method)->agent[i]) {
7ef5d4b0 1365 pr_err("Method %d already in use\n", i);
1da177e4
LT
1366 return -EINVAL;
1367 }
1368 }
1369 return 0;
1370}
1371
1372static int allocate_method_table(struct ib_mad_mgmt_method_table **method)
1373{
1374 /* Allocate management method table */
de6eb66b 1375 *method = kzalloc(sizeof **method, GFP_ATOMIC);
1da177e4 1376 if (!*method) {
7ef5d4b0 1377 pr_err("No memory for ib_mad_mgmt_method_table\n");
1da177e4
LT
1378 return -ENOMEM;
1379 }
1da177e4
LT
1380
1381 return 0;
1382}
1383
1384/*
1385 * Check to see if there are any methods still in use
1386 */
1387static int check_method_table(struct ib_mad_mgmt_method_table *method)
1388{
1389 int i;
1390
1391 for (i = 0; i < IB_MGMT_MAX_METHODS; i++)
1392 if (method->agent[i])
1393 return 1;
1394 return 0;
1395}
1396
1397/*
1398 * Check to see if there are any method tables for this class still in use
1399 */
1400static int check_class_table(struct ib_mad_mgmt_class_table *class)
1401{
1402 int i;
1403
1404 for (i = 0; i < MAX_MGMT_CLASS; i++)
1405 if (class->method_table[i])
1406 return 1;
1407 return 0;
1408}
1409
1410static int check_vendor_class(struct ib_mad_mgmt_vendor_class *vendor_class)
1411{
1412 int i;
1413
1414 for (i = 0; i < MAX_MGMT_OUI; i++)
1415 if (vendor_class->method_table[i])
1416 return 1;
1417 return 0;
1418}
1419
1420static int find_vendor_oui(struct ib_mad_mgmt_vendor_class *vendor_class,
d94bd266 1421 const char *oui)
1da177e4
LT
1422{
1423 int i;
1424
1425 for (i = 0; i < MAX_MGMT_OUI; i++)
3cd96564
RD
1426 /* Is there matching OUI for this vendor class ? */
1427 if (!memcmp(vendor_class->oui[i], oui, 3))
1da177e4
LT
1428 return i;
1429
1430 return -1;
1431}
1432
1433static int check_vendor_table(struct ib_mad_mgmt_vendor_class_table *vendor)
1434{
1435 int i;
1436
1437 for (i = 0; i < MAX_MGMT_VENDOR_RANGE2; i++)
1438 if (vendor->vendor_class[i])
1439 return 1;
1440
1441 return 0;
1442}
1443
1444static void remove_methods_mad_agent(struct ib_mad_mgmt_method_table *method,
1445 struct ib_mad_agent_private *agent)
1446{
1447 int i;
1448
1449 /* Remove any methods for this mad agent */
1450 for (i = 0; i < IB_MGMT_MAX_METHODS; i++) {
1451 if (method->agent[i] == agent) {
1452 method->agent[i] = NULL;
1453 }
1454 }
1455}
1456
1457static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1458 struct ib_mad_agent_private *agent_priv,
1459 u8 mgmt_class)
1460{
1461 struct ib_mad_port_private *port_priv;
1462 struct ib_mad_mgmt_class_table **class;
1463 struct ib_mad_mgmt_method_table **method;
1464 int i, ret;
1465
1466 port_priv = agent_priv->qp_info->port_priv;
1467 class = &port_priv->version[mad_reg_req->mgmt_class_version].class;
1468 if (!*class) {
1469 /* Allocate management class table for "new" class version */
de6eb66b 1470 *class = kzalloc(sizeof **class, GFP_ATOMIC);
1da177e4 1471 if (!*class) {
7ef5d4b0
IW
1472 dev_err(&agent_priv->agent.device->dev,
1473 "No memory for ib_mad_mgmt_class_table\n");
1da177e4
LT
1474 ret = -ENOMEM;
1475 goto error1;
1476 }
de6eb66b 1477
1da177e4
LT
1478 /* Allocate method table for this management class */
1479 method = &(*class)->method_table[mgmt_class];
1480 if ((ret = allocate_method_table(method)))
1481 goto error2;
1482 } else {
1483 method = &(*class)->method_table[mgmt_class];
1484 if (!*method) {
1485 /* Allocate method table for this management class */
1486 if ((ret = allocate_method_table(method)))
1487 goto error1;
1488 }
1489 }
1490
1491 /* Now, make sure methods are not already in use */
1492 if (method_in_use(method, mad_reg_req))
1493 goto error3;
1494
1495 /* Finally, add in methods being registered */
19b629f5 1496 for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS)
1da177e4 1497 (*method)->agent[i] = agent_priv;
19b629f5 1498
1da177e4
LT
1499 return 0;
1500
1501error3:
1502 /* Remove any methods for this mad agent */
1503 remove_methods_mad_agent(*method, agent_priv);
1504 /* Now, check to see if there are any methods in use */
1505 if (!check_method_table(*method)) {
1506 /* If not, release management method table */
1507 kfree(*method);
1508 *method = NULL;
1509 }
1510 ret = -EINVAL;
1511 goto error1;
1512error2:
1513 kfree(*class);
1514 *class = NULL;
1515error1:
1516 return ret;
1517}
1518
1519static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1520 struct ib_mad_agent_private *agent_priv)
1521{
1522 struct ib_mad_port_private *port_priv;
1523 struct ib_mad_mgmt_vendor_class_table **vendor_table;
1524 struct ib_mad_mgmt_vendor_class_table *vendor = NULL;
1525 struct ib_mad_mgmt_vendor_class *vendor_class = NULL;
1526 struct ib_mad_mgmt_method_table **method;
1527 int i, ret = -ENOMEM;
1528 u8 vclass;
1529
1530 /* "New" vendor (with OUI) class */
1531 vclass = vendor_class_index(mad_reg_req->mgmt_class);
1532 port_priv = agent_priv->qp_info->port_priv;
1533 vendor_table = &port_priv->version[
1534 mad_reg_req->mgmt_class_version].vendor;
1535 if (!*vendor_table) {
1536 /* Allocate mgmt vendor class table for "new" class version */
de6eb66b 1537 vendor = kzalloc(sizeof *vendor, GFP_ATOMIC);
1da177e4 1538 if (!vendor) {
7ef5d4b0
IW
1539 dev_err(&agent_priv->agent.device->dev,
1540 "No memory for ib_mad_mgmt_vendor_class_table\n");
1da177e4
LT
1541 goto error1;
1542 }
de6eb66b 1543
1da177e4
LT
1544 *vendor_table = vendor;
1545 }
1546 if (!(*vendor_table)->vendor_class[vclass]) {
1547 /* Allocate table for this management vendor class */
de6eb66b 1548 vendor_class = kzalloc(sizeof *vendor_class, GFP_ATOMIC);
1da177e4 1549 if (!vendor_class) {
7ef5d4b0
IW
1550 dev_err(&agent_priv->agent.device->dev,
1551 "No memory for ib_mad_mgmt_vendor_class\n");
1da177e4
LT
1552 goto error2;
1553 }
de6eb66b 1554
1da177e4
LT
1555 (*vendor_table)->vendor_class[vclass] = vendor_class;
1556 }
1557 for (i = 0; i < MAX_MGMT_OUI; i++) {
1558 /* Is there matching OUI for this vendor class ? */
1559 if (!memcmp((*vendor_table)->vendor_class[vclass]->oui[i],
1560 mad_reg_req->oui, 3)) {
1561 method = &(*vendor_table)->vendor_class[
1562 vclass]->method_table[i];
1563 BUG_ON(!*method);
1564 goto check_in_use;
1565 }
1566 }
1567 for (i = 0; i < MAX_MGMT_OUI; i++) {
1568 /* OUI slot available ? */
1569 if (!is_vendor_oui((*vendor_table)->vendor_class[
1570 vclass]->oui[i])) {
1571 method = &(*vendor_table)->vendor_class[
1572 vclass]->method_table[i];
1573 BUG_ON(*method);
1574 /* Allocate method table for this OUI */
1575 if ((ret = allocate_method_table(method)))
1576 goto error3;
1577 memcpy((*vendor_table)->vendor_class[vclass]->oui[i],
1578 mad_reg_req->oui, 3);
1579 goto check_in_use;
1580 }
1581 }
7ef5d4b0 1582 dev_err(&agent_priv->agent.device->dev, "All OUI slots in use\n");
1da177e4
LT
1583 goto error3;
1584
1585check_in_use:
1586 /* Now, make sure methods are not already in use */
1587 if (method_in_use(method, mad_reg_req))
1588 goto error4;
1589
1590 /* Finally, add in methods being registered */
19b629f5 1591 for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS)
1da177e4 1592 (*method)->agent[i] = agent_priv;
19b629f5 1593
1da177e4
LT
1594 return 0;
1595
1596error4:
1597 /* Remove any methods for this mad agent */
1598 remove_methods_mad_agent(*method, agent_priv);
1599 /* Now, check to see if there are any methods in use */
1600 if (!check_method_table(*method)) {
1601 /* If not, release management method table */
1602 kfree(*method);
1603 *method = NULL;
1604 }
1605 ret = -EINVAL;
1606error3:
1607 if (vendor_class) {
1608 (*vendor_table)->vendor_class[vclass] = NULL;
1609 kfree(vendor_class);
1610 }
1611error2:
1612 if (vendor) {
1613 *vendor_table = NULL;
1614 kfree(vendor);
1615 }
1616error1:
1617 return ret;
1618}
1619
1620static void remove_mad_reg_req(struct ib_mad_agent_private *agent_priv)
1621{
1622 struct ib_mad_port_private *port_priv;
1623 struct ib_mad_mgmt_class_table *class;
1624 struct ib_mad_mgmt_method_table *method;
1625 struct ib_mad_mgmt_vendor_class_table *vendor;
1626 struct ib_mad_mgmt_vendor_class *vendor_class;
1627 int index;
1628 u8 mgmt_class;
1629
1630 /*
1631 * Was MAD registration request supplied
1632 * with original registration ?
1633 */
1634 if (!agent_priv->reg_req) {
1635 goto out;
1636 }
1637
1638 port_priv = agent_priv->qp_info->port_priv;
1639 mgmt_class = convert_mgmt_class(agent_priv->reg_req->mgmt_class);
1640 class = port_priv->version[
1641 agent_priv->reg_req->mgmt_class_version].class;
1642 if (!class)
1643 goto vendor_check;
1644
1645 method = class->method_table[mgmt_class];
1646 if (method) {
1647 /* Remove any methods for this mad agent */
1648 remove_methods_mad_agent(method, agent_priv);
1649 /* Now, check to see if there are any methods still in use */
1650 if (!check_method_table(method)) {
1651 /* If not, release management method table */
1652 kfree(method);
1653 class->method_table[mgmt_class] = NULL;
1654 /* Any management classes left ? */
1655 if (!check_class_table(class)) {
1656 /* If not, release management class table */
1657 kfree(class);
1658 port_priv->version[
1659 agent_priv->reg_req->
1660 mgmt_class_version].class = NULL;
1661 }
1662 }
1663 }
1664
1665vendor_check:
1666 if (!is_vendor_class(mgmt_class))
1667 goto out;
1668
1669 /* normalize mgmt_class to vendor range 2 */
1670 mgmt_class = vendor_class_index(agent_priv->reg_req->mgmt_class);
1671 vendor = port_priv->version[
1672 agent_priv->reg_req->mgmt_class_version].vendor;
1673
1674 if (!vendor)
1675 goto out;
1676
1677 vendor_class = vendor->vendor_class[mgmt_class];
1678 if (vendor_class) {
1679 index = find_vendor_oui(vendor_class, agent_priv->reg_req->oui);
1680 if (index < 0)
1681 goto out;
1682 method = vendor_class->method_table[index];
1683 if (method) {
1684 /* Remove any methods for this mad agent */
1685 remove_methods_mad_agent(method, agent_priv);
1686 /*
1687 * Now, check to see if there are
1688 * any methods still in use
1689 */
1690 if (!check_method_table(method)) {
1691 /* If not, release management method table */
1692 kfree(method);
1693 vendor_class->method_table[index] = NULL;
1694 memset(vendor_class->oui[index], 0, 3);
1695 /* Any OUIs left ? */
1696 if (!check_vendor_class(vendor_class)) {
1697 /* If not, release vendor class table */
1698 kfree(vendor_class);
1699 vendor->vendor_class[mgmt_class] = NULL;
1700 /* Any other vendor classes left ? */
1701 if (!check_vendor_table(vendor)) {
1702 kfree(vendor);
1703 port_priv->version[
1704 agent_priv->reg_req->
1705 mgmt_class_version].
1706 vendor = NULL;
1707 }
1708 }
1709 }
1710 }
1711 }
1712
1713out:
1714 return;
1715}
1716
1da177e4
LT
1717static struct ib_mad_agent_private *
1718find_mad_agent(struct ib_mad_port_private *port_priv,
d94bd266 1719 const struct ib_mad_hdr *mad_hdr)
1da177e4
LT
1720{
1721 struct ib_mad_agent_private *mad_agent = NULL;
1722 unsigned long flags;
1723
1724 spin_lock_irqsave(&port_priv->reg_lock, flags);
d94bd266 1725 if (ib_response_mad(mad_hdr)) {
1da177e4
LT
1726 u32 hi_tid;
1727 struct ib_mad_agent_private *entry;
1728
1729 /*
1730 * Routing is based on high 32 bits of transaction ID
1731 * of MAD.
1732 */
d94bd266 1733 hi_tid = be64_to_cpu(mad_hdr->tid) >> 32;
34816ad9 1734 list_for_each_entry(entry, &port_priv->agent_list, agent_list) {
1da177e4
LT
1735 if (entry->agent.hi_tid == hi_tid) {
1736 mad_agent = entry;
1737 break;
1738 }
1739 }
1740 } else {
1741 struct ib_mad_mgmt_class_table *class;
1742 struct ib_mad_mgmt_method_table *method;
1743 struct ib_mad_mgmt_vendor_class_table *vendor;
1744 struct ib_mad_mgmt_vendor_class *vendor_class;
d94bd266 1745 const struct ib_vendor_mad *vendor_mad;
1da177e4
LT
1746 int index;
1747
1748 /*
1749 * Routing is based on version, class, and method
1750 * For "newer" vendor MADs, also based on OUI
1751 */
d94bd266 1752 if (mad_hdr->class_version >= MAX_MGMT_VERSION)
1da177e4 1753 goto out;
d94bd266 1754 if (!is_vendor_class(mad_hdr->mgmt_class)) {
1da177e4 1755 class = port_priv->version[
d94bd266 1756 mad_hdr->class_version].class;
1da177e4
LT
1757 if (!class)
1758 goto out;
d94bd266 1759 if (convert_mgmt_class(mad_hdr->mgmt_class) >=
b7ab0b19
HS
1760 IB_MGMT_MAX_METHODS)
1761 goto out;
1da177e4 1762 method = class->method_table[convert_mgmt_class(
d94bd266 1763 mad_hdr->mgmt_class)];
1da177e4 1764 if (method)
d94bd266 1765 mad_agent = method->agent[mad_hdr->method &
1da177e4
LT
1766 ~IB_MGMT_METHOD_RESP];
1767 } else {
1768 vendor = port_priv->version[
d94bd266 1769 mad_hdr->class_version].vendor;
1da177e4
LT
1770 if (!vendor)
1771 goto out;
1772 vendor_class = vendor->vendor_class[vendor_class_index(
d94bd266 1773 mad_hdr->mgmt_class)];
1da177e4
LT
1774 if (!vendor_class)
1775 goto out;
1776 /* Find matching OUI */
d94bd266 1777 vendor_mad = (const struct ib_vendor_mad *)mad_hdr;
1da177e4
LT
1778 index = find_vendor_oui(vendor_class, vendor_mad->oui);
1779 if (index == -1)
1780 goto out;
1781 method = vendor_class->method_table[index];
1782 if (method) {
d94bd266 1783 mad_agent = method->agent[mad_hdr->method &
1da177e4
LT
1784 ~IB_MGMT_METHOD_RESP];
1785 }
1786 }
1787 }
1788
1789 if (mad_agent) {
1790 if (mad_agent->agent.recv_handler)
1791 atomic_inc(&mad_agent->refcount);
1792 else {
7ef5d4b0
IW
1793 dev_notice(&port_priv->device->dev,
1794 "No receive handler for client %p on port %d\n",
1795 &mad_agent->agent, port_priv->port_num);
1da177e4
LT
1796 mad_agent = NULL;
1797 }
1798 }
1799out:
1800 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
1801
1802 return mad_agent;
1803}
1804
8e4349d1
IW
1805static int validate_mad(const struct ib_mad_hdr *mad_hdr,
1806 const struct ib_mad_qp_info *qp_info,
1807 bool opa)
1da177e4
LT
1808{
1809 int valid = 0;
8e4349d1 1810 u32 qp_num = qp_info->qp->qp_num;
1da177e4
LT
1811
1812 /* Make sure MAD base version is understood */
8e4349d1
IW
1813 if (mad_hdr->base_version != IB_MGMT_BASE_VERSION &&
1814 (!opa || mad_hdr->base_version != OPA_MGMT_BASE_VERSION)) {
1815 pr_err("MAD received with unsupported base version %d %s\n",
1816 mad_hdr->base_version, opa ? "(opa)" : "");
1da177e4
LT
1817 goto out;
1818 }
1819
1820 /* Filter SMI packets sent to other than QP0 */
77f60833
IW
1821 if ((mad_hdr->mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
1822 (mad_hdr->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) {
1da177e4
LT
1823 if (qp_num == 0)
1824 valid = 1;
1825 } else {
53370886
HR
1826 /* CM attributes other than ClassPortInfo only use Send method */
1827 if ((mad_hdr->mgmt_class == IB_MGMT_CLASS_CM) &&
1828 (mad_hdr->attr_id != IB_MGMT_CLASSPORTINFO_ATTR_ID) &&
1829 (mad_hdr->method != IB_MGMT_METHOD_SEND))
1830 goto out;
1da177e4
LT
1831 /* Filter GSI packets sent to QP0 */
1832 if (qp_num != 0)
1833 valid = 1;
1834 }
1835
1836out:
1837 return valid;
1838}
1839
f766c58f
IW
1840static int is_rmpp_data_mad(const struct ib_mad_agent_private *mad_agent_priv,
1841 const struct ib_mad_hdr *mad_hdr)
fa619a77
HR
1842{
1843 struct ib_rmpp_mad *rmpp_mad;
1844
1845 rmpp_mad = (struct ib_rmpp_mad *)mad_hdr;
1846 return !mad_agent_priv->agent.rmpp_version ||
1471cb6c 1847 !ib_mad_kernel_rmpp_agent(&mad_agent_priv->agent) ||
fa619a77
HR
1848 !(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
1849 IB_MGMT_RMPP_FLAG_ACTIVE) ||
1850 (rmpp_mad->rmpp_hdr.rmpp_type == IB_MGMT_RMPP_TYPE_DATA);
1851}
1852
8bf4b30c
IW
1853static inline int rcv_has_same_class(const struct ib_mad_send_wr_private *wr,
1854 const struct ib_mad_recv_wc *rwc)
fa9656bb 1855{
8bf4b30c 1856 return ((struct ib_mad_hdr *)(wr->send_buf.mad))->mgmt_class ==
fa9656bb
JM
1857 rwc->recv_buf.mad->mad_hdr.mgmt_class;
1858}
1859
f766c58f
IW
1860static inline int rcv_has_same_gid(const struct ib_mad_agent_private *mad_agent_priv,
1861 const struct ib_mad_send_wr_private *wr,
1862 const struct ib_mad_recv_wc *rwc )
fa9656bb
JM
1863{
1864 struct ib_ah_attr attr;
1865 u8 send_resp, rcv_resp;
9874e746
JM
1866 union ib_gid sgid;
1867 struct ib_device *device = mad_agent_priv->agent.device;
1868 u8 port_num = mad_agent_priv->agent.port_num;
1869 u8 lmc;
fa9656bb 1870
96909308
IW
1871 send_resp = ib_response_mad((struct ib_mad_hdr *)wr->send_buf.mad);
1872 rcv_resp = ib_response_mad(&rwc->recv_buf.mad->mad_hdr);
fa9656bb 1873
fa9656bb
JM
1874 if (send_resp == rcv_resp)
1875 /* both requests, or both responses. GIDs different */
1876 return 0;
1877
1878 if (ib_query_ah(wr->send_buf.ah, &attr))
1879 /* Assume not equal, to avoid false positives. */
1880 return 0;
1881
9874e746
JM
1882 if (!!(attr.ah_flags & IB_AH_GRH) !=
1883 !!(rwc->wc->wc_flags & IB_WC_GRH))
fa9656bb
JM
1884 /* one has GID, other does not. Assume different */
1885 return 0;
9874e746
JM
1886
1887 if (!send_resp && rcv_resp) {
1888 /* is request/response. */
1889 if (!(attr.ah_flags & IB_AH_GRH)) {
1890 if (ib_get_cached_lmc(device, port_num, &lmc))
1891 return 0;
1892 return (!lmc || !((attr.src_path_bits ^
1893 rwc->wc->dlid_path_bits) &
1894 ((1 << lmc) - 1)));
1895 } else {
1896 if (ib_get_cached_gid(device, port_num,
55ee3ab2 1897 attr.grh.sgid_index, &sgid, NULL))
9874e746
JM
1898 return 0;
1899 return !memcmp(sgid.raw, rwc->recv_buf.grh->dgid.raw,
1900 16);
1901 }
1902 }
1903
1904 if (!(attr.ah_flags & IB_AH_GRH))
1905 return attr.dlid == rwc->wc->slid;
1906 else
1907 return !memcmp(attr.grh.dgid.raw, rwc->recv_buf.grh->sgid.raw,
1908 16);
1909}
1910
1911static inline int is_direct(u8 class)
1912{
1913 return (class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE);
fa9656bb 1914}
9874e746 1915
fa619a77 1916struct ib_mad_send_wr_private*
f766c58f
IW
1917ib_find_send_mad(const struct ib_mad_agent_private *mad_agent_priv,
1918 const struct ib_mad_recv_wc *wc)
1da177e4 1919{
9874e746 1920 struct ib_mad_send_wr_private *wr;
83a1d228 1921 const struct ib_mad_hdr *mad_hdr;
fa9656bb 1922
83a1d228 1923 mad_hdr = &wc->recv_buf.mad->mad_hdr;
9874e746
JM
1924
1925 list_for_each_entry(wr, &mad_agent_priv->wait_list, agent_list) {
83a1d228 1926 if ((wr->tid == mad_hdr->tid) &&
9874e746
JM
1927 rcv_has_same_class(wr, wc) &&
1928 /*
1929 * Don't check GID for direct routed MADs.
1930 * These might have permissive LIDs.
1931 */
83a1d228 1932 (is_direct(mad_hdr->mgmt_class) ||
9874e746 1933 rcv_has_same_gid(mad_agent_priv, wr, wc)))
39798695 1934 return (wr->status == IB_WC_SUCCESS) ? wr : NULL;
1da177e4
LT
1935 }
1936
1937 /*
1938 * It's possible to receive the response before we've
1939 * been notified that the send has completed
1940 */
9874e746 1941 list_for_each_entry(wr, &mad_agent_priv->send_list, agent_list) {
c597eee5 1942 if (is_rmpp_data_mad(mad_agent_priv, wr->send_buf.mad) &&
83a1d228 1943 wr->tid == mad_hdr->tid &&
9874e746
JM
1944 wr->timeout &&
1945 rcv_has_same_class(wr, wc) &&
1946 /*
1947 * Don't check GID for direct routed MADs.
1948 * These might have permissive LIDs.
1949 */
83a1d228 1950 (is_direct(mad_hdr->mgmt_class) ||
9874e746 1951 rcv_has_same_gid(mad_agent_priv, wr, wc)))
1da177e4 1952 /* Verify request has not been canceled */
9874e746 1953 return (wr->status == IB_WC_SUCCESS) ? wr : NULL;
1da177e4
LT
1954 }
1955 return NULL;
1956}
1957
fa619a77 1958void ib_mark_mad_done(struct ib_mad_send_wr_private *mad_send_wr)
6a0c435e
HR
1959{
1960 mad_send_wr->timeout = 0;
179e0917
AM
1961 if (mad_send_wr->refcount == 1)
1962 list_move_tail(&mad_send_wr->agent_list,
6a0c435e 1963 &mad_send_wr->mad_agent_priv->done_list);
6a0c435e
HR
1964}
1965
1da177e4 1966static void ib_mad_complete_recv(struct ib_mad_agent_private *mad_agent_priv,
4a0754fa 1967 struct ib_mad_recv_wc *mad_recv_wc)
1da177e4
LT
1968{
1969 struct ib_mad_send_wr_private *mad_send_wr;
1970 struct ib_mad_send_wc mad_send_wc;
1971 unsigned long flags;
1972
fa619a77
HR
1973 INIT_LIST_HEAD(&mad_recv_wc->rmpp_list);
1974 list_add(&mad_recv_wc->recv_buf.list, &mad_recv_wc->rmpp_list);
1471cb6c 1975 if (ib_mad_kernel_rmpp_agent(&mad_agent_priv->agent)) {
fa619a77
HR
1976 mad_recv_wc = ib_process_rmpp_recv_wc(mad_agent_priv,
1977 mad_recv_wc);
1978 if (!mad_recv_wc) {
1b52fa98 1979 deref_mad_agent(mad_agent_priv);
fa619a77
HR
1980 return;
1981 }
1982 }
1983
1da177e4 1984 /* Complete corresponding request */
96909308 1985 if (ib_response_mad(&mad_recv_wc->recv_buf.mad->mad_hdr)) {
1da177e4 1986 spin_lock_irqsave(&mad_agent_priv->lock, flags);
fa9656bb 1987 mad_send_wr = ib_find_send_mad(mad_agent_priv, mad_recv_wc);
1da177e4
LT
1988 if (!mad_send_wr) {
1989 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1471cb6c
IW
1990 if (!ib_mad_kernel_rmpp_agent(&mad_agent_priv->agent)
1991 && ib_is_mad_class_rmpp(mad_recv_wc->recv_buf.mad->mad_hdr.mgmt_class)
1992 && (ib_get_rmpp_flags(&((struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad)->rmpp_hdr)
1993 & IB_MGMT_RMPP_FLAG_ACTIVE)) {
1994 /* user rmpp is in effect
1995 * and this is an active RMPP MAD
1996 */
ca281265
CH
1997 mad_agent_priv->agent.recv_handler(
1998 &mad_agent_priv->agent, NULL,
1999 mad_recv_wc);
1471cb6c
IW
2000 atomic_dec(&mad_agent_priv->refcount);
2001 } else {
2002 /* not user rmpp, revert to normal behavior and
2003 * drop the mad */
2004 ib_free_recv_mad(mad_recv_wc);
2005 deref_mad_agent(mad_agent_priv);
2006 return;
2007 }
2008 } else {
2009 ib_mark_mad_done(mad_send_wr);
2010 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1da177e4 2011
1471cb6c 2012 /* Defined behavior is to complete response before request */
ca281265
CH
2013 mad_agent_priv->agent.recv_handler(
2014 &mad_agent_priv->agent,
2015 &mad_send_wr->send_buf,
2016 mad_recv_wc);
1471cb6c 2017 atomic_dec(&mad_agent_priv->refcount);
1da177e4 2018
1471cb6c
IW
2019 mad_send_wc.status = IB_WC_SUCCESS;
2020 mad_send_wc.vendor_err = 0;
2021 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2022 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
2023 }
1da177e4 2024 } else {
ca281265 2025 mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent, NULL,
4a0754fa 2026 mad_recv_wc);
1b52fa98 2027 deref_mad_agent(mad_agent_priv);
1da177e4
LT
2028 }
2029}
2030
e11ae8aa
IW
2031static enum smi_action handle_ib_smi(const struct ib_mad_port_private *port_priv,
2032 const struct ib_mad_qp_info *qp_info,
2033 const struct ib_wc *wc,
2034 int port_num,
2035 struct ib_mad_private *recv,
2036 struct ib_mad_private *response)
2037{
2038 enum smi_forward_action retsmi;
c9082e51 2039 struct ib_smp *smp = (struct ib_smp *)recv->mad;
e11ae8aa 2040
c9082e51 2041 if (smi_handle_dr_smp_recv(smp,
4139032b 2042 rdma_cap_ib_switch(port_priv->device),
e11ae8aa
IW
2043 port_num,
2044 port_priv->device->phys_port_cnt) ==
2045 IB_SMI_DISCARD)
2046 return IB_SMI_DISCARD;
2047
c9082e51 2048 retsmi = smi_check_forward_dr_smp(smp);
e11ae8aa
IW
2049 if (retsmi == IB_SMI_LOCAL)
2050 return IB_SMI_HANDLE;
2051
2052 if (retsmi == IB_SMI_SEND) { /* don't forward */
c9082e51 2053 if (smi_handle_dr_smp_send(smp,
4139032b 2054 rdma_cap_ib_switch(port_priv->device),
e11ae8aa
IW
2055 port_num) == IB_SMI_DISCARD)
2056 return IB_SMI_DISCARD;
2057
c9082e51 2058 if (smi_check_local_smp(smp, port_priv->device) == IB_SMI_DISCARD)
e11ae8aa 2059 return IB_SMI_DISCARD;
4139032b 2060 } else if (rdma_cap_ib_switch(port_priv->device)) {
e11ae8aa 2061 /* forward case for switches */
c9082e51 2062 memcpy(response, recv, mad_priv_size(response));
e11ae8aa 2063 response->header.recv_wc.wc = &response->header.wc;
c9082e51 2064 response->header.recv_wc.recv_buf.mad = (struct ib_mad *)response->mad;
e11ae8aa
IW
2065 response->header.recv_wc.recv_buf.grh = &response->grh;
2066
c9082e51 2067 agent_send_response((const struct ib_mad_hdr *)response->mad,
e11ae8aa
IW
2068 &response->grh, wc,
2069 port_priv->device,
c9082e51
IW
2070 smi_get_fwd_port(smp),
2071 qp_info->qp->qp_num,
8e4349d1
IW
2072 response->mad_size,
2073 false);
e11ae8aa
IW
2074
2075 return IB_SMI_DISCARD;
2076 }
2077 return IB_SMI_HANDLE;
2078}
2079
c9082e51 2080static bool generate_unmatched_resp(const struct ib_mad_private *recv,
8e4349d1
IW
2081 struct ib_mad_private *response,
2082 size_t *resp_len, bool opa)
0b307043 2083{
c9082e51
IW
2084 const struct ib_mad_hdr *recv_hdr = (const struct ib_mad_hdr *)recv->mad;
2085 struct ib_mad_hdr *resp_hdr = (struct ib_mad_hdr *)response->mad;
2086
2087 if (recv_hdr->method == IB_MGMT_METHOD_GET ||
2088 recv_hdr->method == IB_MGMT_METHOD_SET) {
2089 memcpy(response, recv, mad_priv_size(response));
0b307043 2090 response->header.recv_wc.wc = &response->header.wc;
c9082e51 2091 response->header.recv_wc.recv_buf.mad = (struct ib_mad *)response->mad;
0b307043 2092 response->header.recv_wc.recv_buf.grh = &response->grh;
c9082e51
IW
2093 resp_hdr->method = IB_MGMT_METHOD_GET_RESP;
2094 resp_hdr->status = cpu_to_be16(IB_MGMT_MAD_STATUS_UNSUPPORTED_METHOD_ATTRIB);
2095 if (recv_hdr->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
2096 resp_hdr->status |= IB_SMP_DIRECTION;
0b307043 2097
8e4349d1
IW
2098 if (opa && recv_hdr->base_version == OPA_MGMT_BASE_VERSION) {
2099 if (recv_hdr->mgmt_class ==
2100 IB_MGMT_CLASS_SUBN_LID_ROUTED ||
2101 recv_hdr->mgmt_class ==
2102 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
2103 *resp_len = opa_get_smp_header_size(
2104 (struct opa_smp *)recv->mad);
2105 else
2106 *resp_len = sizeof(struct ib_mad_hdr);
2107 }
2108
0b307043
ST
2109 return true;
2110 } else {
2111 return false;
2112 }
2113}
8e4349d1
IW
2114
2115static enum smi_action
2116handle_opa_smi(struct ib_mad_port_private *port_priv,
2117 struct ib_mad_qp_info *qp_info,
2118 struct ib_wc *wc,
2119 int port_num,
2120 struct ib_mad_private *recv,
2121 struct ib_mad_private *response)
2122{
2123 enum smi_forward_action retsmi;
2124 struct opa_smp *smp = (struct opa_smp *)recv->mad;
2125
2126 if (opa_smi_handle_dr_smp_recv(smp,
4139032b 2127 rdma_cap_ib_switch(port_priv->device),
8e4349d1
IW
2128 port_num,
2129 port_priv->device->phys_port_cnt) ==
2130 IB_SMI_DISCARD)
2131 return IB_SMI_DISCARD;
2132
2133 retsmi = opa_smi_check_forward_dr_smp(smp);
2134 if (retsmi == IB_SMI_LOCAL)
2135 return IB_SMI_HANDLE;
2136
2137 if (retsmi == IB_SMI_SEND) { /* don't forward */
2138 if (opa_smi_handle_dr_smp_send(smp,
4139032b 2139 rdma_cap_ib_switch(port_priv->device),
8e4349d1
IW
2140 port_num) == IB_SMI_DISCARD)
2141 return IB_SMI_DISCARD;
2142
2143 if (opa_smi_check_local_smp(smp, port_priv->device) ==
2144 IB_SMI_DISCARD)
2145 return IB_SMI_DISCARD;
2146
4139032b 2147 } else if (rdma_cap_ib_switch(port_priv->device)) {
8e4349d1
IW
2148 /* forward case for switches */
2149 memcpy(response, recv, mad_priv_size(response));
2150 response->header.recv_wc.wc = &response->header.wc;
2151 response->header.recv_wc.recv_buf.opa_mad =
2152 (struct opa_mad *)response->mad;
2153 response->header.recv_wc.recv_buf.grh = &response->grh;
2154
2155 agent_send_response((const struct ib_mad_hdr *)response->mad,
2156 &response->grh, wc,
2157 port_priv->device,
2158 opa_smi_get_fwd_port(smp),
2159 qp_info->qp->qp_num,
2160 recv->header.wc.byte_len,
2161 true);
2162
2163 return IB_SMI_DISCARD;
2164 }
2165
2166 return IB_SMI_HANDLE;
2167}
2168
2169static enum smi_action
2170handle_smi(struct ib_mad_port_private *port_priv,
2171 struct ib_mad_qp_info *qp_info,
2172 struct ib_wc *wc,
2173 int port_num,
2174 struct ib_mad_private *recv,
2175 struct ib_mad_private *response,
2176 bool opa)
2177{
2178 struct ib_mad_hdr *mad_hdr = (struct ib_mad_hdr *)recv->mad;
2179
2180 if (opa && mad_hdr->base_version == OPA_MGMT_BASE_VERSION &&
2181 mad_hdr->class_version == OPA_SMI_CLASS_VERSION)
2182 return handle_opa_smi(port_priv, qp_info, wc, port_num, recv,
2183 response);
2184
2185 return handle_ib_smi(port_priv, qp_info, wc, port_num, recv, response);
2186}
2187
1da177e4
LT
2188static void ib_mad_recv_done_handler(struct ib_mad_port_private *port_priv,
2189 struct ib_wc *wc)
2190{
2191 struct ib_mad_qp_info *qp_info;
2192 struct ib_mad_private_header *mad_priv_hdr;
445d6807 2193 struct ib_mad_private *recv, *response = NULL;
1da177e4
LT
2194 struct ib_mad_list_head *mad_list;
2195 struct ib_mad_agent_private *mad_agent;
1bae4dbf 2196 int port_num;
a9e74323 2197 int ret = IB_MAD_RESULT_SUCCESS;
4cd7c947
IW
2198 size_t mad_size;
2199 u16 resp_mad_pkey_index = 0;
8e4349d1 2200 bool opa;
1da177e4 2201
1da177e4
LT
2202 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
2203 qp_info = mad_list->mad_queue->qp_info;
2204 dequeue_mad(mad_list);
2205
8e4349d1
IW
2206 opa = rdma_cap_opa_mad(qp_info->port_priv->device,
2207 qp_info->port_priv->port_num);
2208
1da177e4
LT
2209 mad_priv_hdr = container_of(mad_list, struct ib_mad_private_header,
2210 mad_list);
2211 recv = container_of(mad_priv_hdr, struct ib_mad_private, header);
1527106f
RC
2212 ib_dma_unmap_single(port_priv->device,
2213 recv->header.mapping,
c9082e51 2214 mad_priv_dma_size(recv),
1527106f 2215 DMA_FROM_DEVICE);
1da177e4
LT
2216
2217 /* Setup MAD receive work completion from "normal" work completion */
24239aff
SH
2218 recv->header.wc = *wc;
2219 recv->header.recv_wc.wc = &recv->header.wc;
8e4349d1
IW
2220
2221 if (opa && ((struct ib_mad_hdr *)(recv->mad))->base_version == OPA_MGMT_BASE_VERSION) {
2222 recv->header.recv_wc.mad_len = wc->byte_len - sizeof(struct ib_grh);
2223 recv->header.recv_wc.mad_seg_size = sizeof(struct opa_mad);
2224 } else {
2225 recv->header.recv_wc.mad_len = sizeof(struct ib_mad);
2226 recv->header.recv_wc.mad_seg_size = sizeof(struct ib_mad);
2227 }
2228
c9082e51 2229 recv->header.recv_wc.recv_buf.mad = (struct ib_mad *)recv->mad;
1da177e4
LT
2230 recv->header.recv_wc.recv_buf.grh = &recv->grh;
2231
2232 if (atomic_read(&qp_info->snoop_count))
2233 snoop_recv(qp_info, &recv->header.recv_wc, IB_MAD_SNOOP_RECVS);
2234
2235 /* Validate MAD */
8e4349d1 2236 if (!validate_mad((const struct ib_mad_hdr *)recv->mad, qp_info, opa))
1da177e4
LT
2237 goto out;
2238
4cd7c947
IW
2239 mad_size = recv->mad_size;
2240 response = alloc_mad_private(mad_size, GFP_KERNEL);
445d6807 2241 if (!response) {
7ef5d4b0
IW
2242 dev_err(&port_priv->device->dev,
2243 "ib_mad_recv_done_handler no memory for response buffer\n");
445d6807
HR
2244 goto out;
2245 }
2246
4139032b 2247 if (rdma_cap_ib_switch(port_priv->device))
1bae4dbf
HR
2248 port_num = wc->port_num;
2249 else
2250 port_num = port_priv->port_num;
2251
c9082e51 2252 if (((struct ib_mad_hdr *)recv->mad)->mgmt_class ==
1da177e4 2253 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
8e4349d1
IW
2254 if (handle_smi(port_priv, qp_info, wc, port_num, recv,
2255 response, opa)
e11ae8aa 2256 == IB_SMI_DISCARD)
1da177e4 2257 goto out;
1da177e4
LT
2258 }
2259
1da177e4
LT
2260 /* Give driver "right of first refusal" on incoming MAD */
2261 if (port_priv->device->process_mad) {
1da177e4
LT
2262 ret = port_priv->device->process_mad(port_priv->device, 0,
2263 port_priv->port_num,
2264 wc, &recv->grh,
4cd7c947
IW
2265 (const struct ib_mad_hdr *)recv->mad,
2266 recv->mad_size,
2267 (struct ib_mad_hdr *)response->mad,
2268 &mad_size, &resp_mad_pkey_index);
8e4349d1
IW
2269
2270 if (opa)
2271 wc->pkey_index = resp_mad_pkey_index;
2272
1da177e4
LT
2273 if (ret & IB_MAD_RESULT_SUCCESS) {
2274 if (ret & IB_MAD_RESULT_CONSUMED)
2275 goto out;
2276 if (ret & IB_MAD_RESULT_REPLY) {
c9082e51 2277 agent_send_response((const struct ib_mad_hdr *)response->mad,
34816ad9
SH
2278 &recv->grh, wc,
2279 port_priv->device,
1bae4dbf 2280 port_num,
c9082e51 2281 qp_info->qp->qp_num,
8e4349d1 2282 mad_size, opa);
1da177e4
LT
2283 goto out;
2284 }
2285 }
2286 }
2287
c9082e51 2288 mad_agent = find_mad_agent(port_priv, (const struct ib_mad_hdr *)recv->mad);
1da177e4 2289 if (mad_agent) {
4a0754fa 2290 ib_mad_complete_recv(mad_agent, &recv->header.recv_wc);
1da177e4
LT
2291 /*
2292 * recv is freed up in error cases in ib_mad_complete_recv
2293 * or via recv_handler in ib_mad_complete_recv()
2294 */
2295 recv = NULL;
a9e74323 2296 } else if ((ret & IB_MAD_RESULT_SUCCESS) &&
8e4349d1 2297 generate_unmatched_resp(recv, response, &mad_size, opa)) {
c9082e51
IW
2298 agent_send_response((const struct ib_mad_hdr *)response->mad, &recv->grh, wc,
2299 port_priv->device, port_num,
8e4349d1 2300 qp_info->qp->qp_num, mad_size, opa);
1da177e4
LT
2301 }
2302
2303out:
2304 /* Post another receive request for this QP */
2305 if (response) {
2306 ib_mad_post_receive_mads(qp_info, response);
c9082e51 2307 kfree(recv);
1da177e4
LT
2308 } else
2309 ib_mad_post_receive_mads(qp_info, recv);
2310}
2311
2312static void adjust_timeout(struct ib_mad_agent_private *mad_agent_priv)
2313{
2314 struct ib_mad_send_wr_private *mad_send_wr;
2315 unsigned long delay;
2316
2317 if (list_empty(&mad_agent_priv->wait_list)) {
136b5721 2318 cancel_delayed_work(&mad_agent_priv->timed_work);
1da177e4
LT
2319 } else {
2320 mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
2321 struct ib_mad_send_wr_private,
2322 agent_list);
2323
2324 if (time_after(mad_agent_priv->timeout,
2325 mad_send_wr->timeout)) {
2326 mad_agent_priv->timeout = mad_send_wr->timeout;
1da177e4
LT
2327 delay = mad_send_wr->timeout - jiffies;
2328 if ((long)delay <= 0)
2329 delay = 1;
e7c2f967
TH
2330 mod_delayed_work(mad_agent_priv->qp_info->port_priv->wq,
2331 &mad_agent_priv->timed_work, delay);
1da177e4
LT
2332 }
2333 }
2334}
2335
d760ce8f 2336static void wait_for_response(struct ib_mad_send_wr_private *mad_send_wr)
1da177e4 2337{
d760ce8f 2338 struct ib_mad_agent_private *mad_agent_priv;
1da177e4
LT
2339 struct ib_mad_send_wr_private *temp_mad_send_wr;
2340 struct list_head *list_item;
2341 unsigned long delay;
2342
d760ce8f 2343 mad_agent_priv = mad_send_wr->mad_agent_priv;
1da177e4
LT
2344 list_del(&mad_send_wr->agent_list);
2345
2346 delay = mad_send_wr->timeout;
2347 mad_send_wr->timeout += jiffies;
2348
29bb33dd
HR
2349 if (delay) {
2350 list_for_each_prev(list_item, &mad_agent_priv->wait_list) {
2351 temp_mad_send_wr = list_entry(list_item,
2352 struct ib_mad_send_wr_private,
2353 agent_list);
2354 if (time_after(mad_send_wr->timeout,
2355 temp_mad_send_wr->timeout))
2356 break;
2357 }
1da177e4 2358 }
29bb33dd
HR
2359 else
2360 list_item = &mad_agent_priv->wait_list;
1da177e4
LT
2361 list_add(&mad_send_wr->agent_list, list_item);
2362
2363 /* Reschedule a work item if we have a shorter timeout */
e7c2f967
TH
2364 if (mad_agent_priv->wait_list.next == &mad_send_wr->agent_list)
2365 mod_delayed_work(mad_agent_priv->qp_info->port_priv->wq,
2366 &mad_agent_priv->timed_work, delay);
1da177e4
LT
2367}
2368
03b61ad2
HR
2369void ib_reset_mad_timeout(struct ib_mad_send_wr_private *mad_send_wr,
2370 int timeout_ms)
2371{
2372 mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
2373 wait_for_response(mad_send_wr);
2374}
2375
1da177e4
LT
2376/*
2377 * Process a send work completion
2378 */
fa619a77
HR
2379void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr,
2380 struct ib_mad_send_wc *mad_send_wc)
1da177e4
LT
2381{
2382 struct ib_mad_agent_private *mad_agent_priv;
2383 unsigned long flags;
fa619a77 2384 int ret;
1da177e4 2385
d760ce8f 2386 mad_agent_priv = mad_send_wr->mad_agent_priv;
1da177e4 2387 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1471cb6c 2388 if (ib_mad_kernel_rmpp_agent(&mad_agent_priv->agent)) {
fa619a77
HR
2389 ret = ib_process_rmpp_send_wc(mad_send_wr, mad_send_wc);
2390 if (ret == IB_RMPP_RESULT_CONSUMED)
2391 goto done;
2392 } else
2393 ret = IB_RMPP_RESULT_UNHANDLED;
2394
1da177e4
LT
2395 if (mad_send_wc->status != IB_WC_SUCCESS &&
2396 mad_send_wr->status == IB_WC_SUCCESS) {
2397 mad_send_wr->status = mad_send_wc->status;
2398 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2399 }
2400
2401 if (--mad_send_wr->refcount > 0) {
2402 if (mad_send_wr->refcount == 1 && mad_send_wr->timeout &&
2403 mad_send_wr->status == IB_WC_SUCCESS) {
d760ce8f 2404 wait_for_response(mad_send_wr);
1da177e4 2405 }
fa619a77 2406 goto done;
1da177e4
LT
2407 }
2408
2409 /* Remove send from MAD agent and notify client of completion */
2410 list_del(&mad_send_wr->agent_list);
2411 adjust_timeout(mad_agent_priv);
2412 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2413
2414 if (mad_send_wr->status != IB_WC_SUCCESS )
2415 mad_send_wc->status = mad_send_wr->status;
34816ad9
SH
2416 if (ret == IB_RMPP_RESULT_INTERNAL)
2417 ib_rmpp_send_handler(mad_send_wc);
2418 else
fa619a77
HR
2419 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2420 mad_send_wc);
1da177e4
LT
2421
2422 /* Release reference on agent taken when sending */
1b52fa98 2423 deref_mad_agent(mad_agent_priv);
fa619a77
HR
2424 return;
2425done:
2426 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1da177e4
LT
2427}
2428
2429static void ib_mad_send_done_handler(struct ib_mad_port_private *port_priv,
2430 struct ib_wc *wc)
2431{
2432 struct ib_mad_send_wr_private *mad_send_wr, *queued_send_wr;
2433 struct ib_mad_list_head *mad_list;
2434 struct ib_mad_qp_info *qp_info;
2435 struct ib_mad_queue *send_queue;
2436 struct ib_send_wr *bad_send_wr;
34816ad9 2437 struct ib_mad_send_wc mad_send_wc;
1da177e4
LT
2438 unsigned long flags;
2439 int ret;
2440
2441 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
2442 mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
2443 mad_list);
2444 send_queue = mad_list->mad_queue;
2445 qp_info = send_queue->qp_info;
2446
2447retry:
1527106f
RC
2448 ib_dma_unmap_single(mad_send_wr->send_buf.mad_agent->device,
2449 mad_send_wr->header_mapping,
2450 mad_send_wr->sg_list[0].length, DMA_TO_DEVICE);
2451 ib_dma_unmap_single(mad_send_wr->send_buf.mad_agent->device,
2452 mad_send_wr->payload_mapping,
2453 mad_send_wr->sg_list[1].length, DMA_TO_DEVICE);
1da177e4
LT
2454 queued_send_wr = NULL;
2455 spin_lock_irqsave(&send_queue->lock, flags);
2456 list_del(&mad_list->list);
2457
2458 /* Move queued send to the send queue */
2459 if (send_queue->count-- > send_queue->max_active) {
2460 mad_list = container_of(qp_info->overflow_list.next,
2461 struct ib_mad_list_head, list);
2462 queued_send_wr = container_of(mad_list,
2463 struct ib_mad_send_wr_private,
2464 mad_list);
179e0917 2465 list_move_tail(&mad_list->list, &send_queue->list);
1da177e4
LT
2466 }
2467 spin_unlock_irqrestore(&send_queue->lock, flags);
2468
34816ad9
SH
2469 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2470 mad_send_wc.status = wc->status;
2471 mad_send_wc.vendor_err = wc->vendor_err;
1da177e4 2472 if (atomic_read(&qp_info->snoop_count))
34816ad9 2473 snoop_send(qp_info, &mad_send_wr->send_buf, &mad_send_wc,
1da177e4 2474 IB_MAD_SNOOP_SEND_COMPLETIONS);
34816ad9 2475 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
1da177e4
LT
2476
2477 if (queued_send_wr) {
e622f2f4 2478 ret = ib_post_send(qp_info->qp, &queued_send_wr->send_wr.wr,
34816ad9 2479 &bad_send_wr);
1da177e4 2480 if (ret) {
7ef5d4b0
IW
2481 dev_err(&port_priv->device->dev,
2482 "ib_post_send failed: %d\n", ret);
1da177e4
LT
2483 mad_send_wr = queued_send_wr;
2484 wc->status = IB_WC_LOC_QP_OP_ERR;
2485 goto retry;
2486 }
2487 }
2488}
2489
2490static void mark_sends_for_retry(struct ib_mad_qp_info *qp_info)
2491{
2492 struct ib_mad_send_wr_private *mad_send_wr;
2493 struct ib_mad_list_head *mad_list;
2494 unsigned long flags;
2495
2496 spin_lock_irqsave(&qp_info->send_queue.lock, flags);
2497 list_for_each_entry(mad_list, &qp_info->send_queue.list, list) {
2498 mad_send_wr = container_of(mad_list,
2499 struct ib_mad_send_wr_private,
2500 mad_list);
2501 mad_send_wr->retry = 1;
2502 }
2503 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
2504}
2505
2506static void mad_error_handler(struct ib_mad_port_private *port_priv,
2507 struct ib_wc *wc)
2508{
2509 struct ib_mad_list_head *mad_list;
2510 struct ib_mad_qp_info *qp_info;
2511 struct ib_mad_send_wr_private *mad_send_wr;
2512 int ret;
2513
2514 /* Determine if failure was a send or receive */
2515 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
2516 qp_info = mad_list->mad_queue->qp_info;
2517 if (mad_list->mad_queue == &qp_info->recv_queue)
2518 /*
2519 * Receive errors indicate that the QP has entered the error
2520 * state - error handling/shutdown code will cleanup
2521 */
2522 return;
2523
2524 /*
2525 * Send errors will transition the QP to SQE - move
2526 * QP to RTS and repost flushed work requests
2527 */
2528 mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
2529 mad_list);
2530 if (wc->status == IB_WC_WR_FLUSH_ERR) {
2531 if (mad_send_wr->retry) {
2532 /* Repost send */
2533 struct ib_send_wr *bad_send_wr;
2534
2535 mad_send_wr->retry = 0;
e622f2f4 2536 ret = ib_post_send(qp_info->qp, &mad_send_wr->send_wr.wr,
1da177e4
LT
2537 &bad_send_wr);
2538 if (ret)
2539 ib_mad_send_done_handler(port_priv, wc);
2540 } else
2541 ib_mad_send_done_handler(port_priv, wc);
2542 } else {
2543 struct ib_qp_attr *attr;
2544
2545 /* Transition QP to RTS and fail offending send */
2546 attr = kmalloc(sizeof *attr, GFP_KERNEL);
2547 if (attr) {
2548 attr->qp_state = IB_QPS_RTS;
2549 attr->cur_qp_state = IB_QPS_SQE;
2550 ret = ib_modify_qp(qp_info->qp, attr,
2551 IB_QP_STATE | IB_QP_CUR_STATE);
2552 kfree(attr);
2553 if (ret)
7ef5d4b0
IW
2554 dev_err(&port_priv->device->dev,
2555 "mad_error_handler - ib_modify_qp to RTS : %d\n",
2556 ret);
1da177e4
LT
2557 else
2558 mark_sends_for_retry(qp_info);
2559 }
2560 ib_mad_send_done_handler(port_priv, wc);
2561 }
2562}
2563
2564/*
2565 * IB MAD completion callback
2566 */
c4028958 2567static void ib_mad_completion_handler(struct work_struct *work)
1da177e4
LT
2568{
2569 struct ib_mad_port_private *port_priv;
2570 struct ib_wc wc;
0d6ed314 2571 int count = 0;
1da177e4 2572
c4028958 2573 port_priv = container_of(work, struct ib_mad_port_private, work);
1da177e4
LT
2574 ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
2575
2576 while (ib_poll_cq(port_priv->cq, 1, &wc) == 1) {
2577 if (wc.status == IB_WC_SUCCESS) {
2578 switch (wc.opcode) {
2579 case IB_WC_SEND:
2580 ib_mad_send_done_handler(port_priv, &wc);
2581 break;
2582 case IB_WC_RECV:
2583 ib_mad_recv_done_handler(port_priv, &wc);
2584 break;
2585 default:
2586 BUG_ON(1);
2587 break;
2588 }
2589 } else
2590 mad_error_handler(port_priv, &wc);
0d6ed314
DL
2591
2592 if (++count > MAD_COMPLETION_PROC_LIMIT) {
2593 queue_work(port_priv->wq, &port_priv->work);
2594 break;
2595 }
1da177e4
LT
2596 }
2597}
2598
2599static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv)
2600{
2601 unsigned long flags;
2602 struct ib_mad_send_wr_private *mad_send_wr, *temp_mad_send_wr;
2603 struct ib_mad_send_wc mad_send_wc;
2604 struct list_head cancel_list;
2605
2606 INIT_LIST_HEAD(&cancel_list);
2607
2608 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2609 list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
2610 &mad_agent_priv->send_list, agent_list) {
2611 if (mad_send_wr->status == IB_WC_SUCCESS) {
3cd96564 2612 mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
1da177e4
LT
2613 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2614 }
2615 }
2616
2617 /* Empty wait list to prevent receives from finding a request */
2618 list_splice_init(&mad_agent_priv->wait_list, &cancel_list);
2619 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2620
2621 /* Report all cancelled requests */
2622 mad_send_wc.status = IB_WC_WR_FLUSH_ERR;
2623 mad_send_wc.vendor_err = 0;
2624
2625 list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
2626 &cancel_list, agent_list) {
34816ad9
SH
2627 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2628 list_del(&mad_send_wr->agent_list);
1da177e4
LT
2629 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2630 &mad_send_wc);
1da177e4
LT
2631 atomic_dec(&mad_agent_priv->refcount);
2632 }
2633}
2634
2635static struct ib_mad_send_wr_private*
34816ad9
SH
2636find_send_wr(struct ib_mad_agent_private *mad_agent_priv,
2637 struct ib_mad_send_buf *send_buf)
1da177e4
LT
2638{
2639 struct ib_mad_send_wr_private *mad_send_wr;
2640
2641 list_for_each_entry(mad_send_wr, &mad_agent_priv->wait_list,
2642 agent_list) {
34816ad9 2643 if (&mad_send_wr->send_buf == send_buf)
1da177e4
LT
2644 return mad_send_wr;
2645 }
2646
2647 list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list,
2648 agent_list) {
c597eee5
IW
2649 if (is_rmpp_data_mad(mad_agent_priv,
2650 mad_send_wr->send_buf.mad) &&
34816ad9 2651 &mad_send_wr->send_buf == send_buf)
1da177e4
LT
2652 return mad_send_wr;
2653 }
2654 return NULL;
2655}
2656
34816ad9
SH
2657int ib_modify_mad(struct ib_mad_agent *mad_agent,
2658 struct ib_mad_send_buf *send_buf, u32 timeout_ms)
1da177e4
LT
2659{
2660 struct ib_mad_agent_private *mad_agent_priv;
2661 struct ib_mad_send_wr_private *mad_send_wr;
2662 unsigned long flags;
cabe3cbc 2663 int active;
1da177e4
LT
2664
2665 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
2666 agent);
2667 spin_lock_irqsave(&mad_agent_priv->lock, flags);
34816ad9 2668 mad_send_wr = find_send_wr(mad_agent_priv, send_buf);
03b61ad2 2669 if (!mad_send_wr || mad_send_wr->status != IB_WC_SUCCESS) {
1da177e4 2670 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
03b61ad2 2671 return -EINVAL;
1da177e4
LT
2672 }
2673
cabe3cbc 2674 active = (!mad_send_wr->timeout || mad_send_wr->refcount > 1);
03b61ad2 2675 if (!timeout_ms) {
1da177e4 2676 mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
03b61ad2 2677 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
1da177e4
LT
2678 }
2679
34816ad9 2680 mad_send_wr->send_buf.timeout_ms = timeout_ms;
cabe3cbc 2681 if (active)
03b61ad2
HR
2682 mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
2683 else
2684 ib_reset_mad_timeout(mad_send_wr, timeout_ms);
2685
1da177e4 2686 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
03b61ad2
HR
2687 return 0;
2688}
2689EXPORT_SYMBOL(ib_modify_mad);
1da177e4 2690
34816ad9
SH
2691void ib_cancel_mad(struct ib_mad_agent *mad_agent,
2692 struct ib_mad_send_buf *send_buf)
03b61ad2 2693{
34816ad9 2694 ib_modify_mad(mad_agent, send_buf, 0);
1da177e4
LT
2695}
2696EXPORT_SYMBOL(ib_cancel_mad);
2697
c4028958 2698static void local_completions(struct work_struct *work)
1da177e4
LT
2699{
2700 struct ib_mad_agent_private *mad_agent_priv;
2701 struct ib_mad_local_private *local;
2702 struct ib_mad_agent_private *recv_mad_agent;
2703 unsigned long flags;
1d9bc6d6 2704 int free_mad;
1da177e4
LT
2705 struct ib_wc wc;
2706 struct ib_mad_send_wc mad_send_wc;
8e4349d1 2707 bool opa;
1da177e4 2708
c4028958
DH
2709 mad_agent_priv =
2710 container_of(work, struct ib_mad_agent_private, local_work);
1da177e4 2711
8e4349d1
IW
2712 opa = rdma_cap_opa_mad(mad_agent_priv->qp_info->port_priv->device,
2713 mad_agent_priv->qp_info->port_priv->port_num);
2714
1da177e4
LT
2715 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2716 while (!list_empty(&mad_agent_priv->local_list)) {
2717 local = list_entry(mad_agent_priv->local_list.next,
2718 struct ib_mad_local_private,
2719 completion_list);
37289efe 2720 list_del(&local->completion_list);
1da177e4 2721 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1d9bc6d6 2722 free_mad = 0;
1da177e4 2723 if (local->mad_priv) {
8e4349d1 2724 u8 base_version;
1da177e4
LT
2725 recv_mad_agent = local->recv_mad_agent;
2726 if (!recv_mad_agent) {
7ef5d4b0
IW
2727 dev_err(&mad_agent_priv->agent.device->dev,
2728 "No receive MAD agent for local completion\n");
1d9bc6d6 2729 free_mad = 1;
1da177e4
LT
2730 goto local_send_completion;
2731 }
2732
2733 /*
2734 * Defined behavior is to complete response
2735 * before request
2736 */
062dbb69
MT
2737 build_smp_wc(recv_mad_agent->agent.qp,
2738 (unsigned long) local->mad_send_wr,
97f52eb4 2739 be16_to_cpu(IB_LID_PERMISSIVE),
e622f2f4 2740 local->mad_send_wr->send_wr.pkey_index,
8e4349d1 2741 recv_mad_agent->agent.port_num, &wc);
1da177e4
LT
2742
2743 local->mad_priv->header.recv_wc.wc = &wc;
8e4349d1
IW
2744
2745 base_version = ((struct ib_mad_hdr *)(local->mad_priv->mad))->base_version;
2746 if (opa && base_version == OPA_MGMT_BASE_VERSION) {
2747 local->mad_priv->header.recv_wc.mad_len = local->return_wc_byte_len;
2748 local->mad_priv->header.recv_wc.mad_seg_size = sizeof(struct opa_mad);
2749 } else {
2750 local->mad_priv->header.recv_wc.mad_len = sizeof(struct ib_mad);
2751 local->mad_priv->header.recv_wc.mad_seg_size = sizeof(struct ib_mad);
2752 }
2753
fa619a77
HR
2754 INIT_LIST_HEAD(&local->mad_priv->header.recv_wc.rmpp_list);
2755 list_add(&local->mad_priv->header.recv_wc.recv_buf.list,
2756 &local->mad_priv->header.recv_wc.rmpp_list);
1da177e4
LT
2757 local->mad_priv->header.recv_wc.recv_buf.grh = NULL;
2758 local->mad_priv->header.recv_wc.recv_buf.mad =
c9082e51 2759 (struct ib_mad *)local->mad_priv->mad;
1da177e4
LT
2760 if (atomic_read(&recv_mad_agent->qp_info->snoop_count))
2761 snoop_recv(recv_mad_agent->qp_info,
2762 &local->mad_priv->header.recv_wc,
2763 IB_MAD_SNOOP_RECVS);
2764 recv_mad_agent->agent.recv_handler(
2765 &recv_mad_agent->agent,
ca281265 2766 &local->mad_send_wr->send_buf,
1da177e4
LT
2767 &local->mad_priv->header.recv_wc);
2768 spin_lock_irqsave(&recv_mad_agent->lock, flags);
2769 atomic_dec(&recv_mad_agent->refcount);
2770 spin_unlock_irqrestore(&recv_mad_agent->lock, flags);
2771 }
2772
2773local_send_completion:
2774 /* Complete send */
2775 mad_send_wc.status = IB_WC_SUCCESS;
2776 mad_send_wc.vendor_err = 0;
34816ad9 2777 mad_send_wc.send_buf = &local->mad_send_wr->send_buf;
1da177e4 2778 if (atomic_read(&mad_agent_priv->qp_info->snoop_count))
34816ad9
SH
2779 snoop_send(mad_agent_priv->qp_info,
2780 &local->mad_send_wr->send_buf,
2781 &mad_send_wc, IB_MAD_SNOOP_SEND_COMPLETIONS);
1da177e4
LT
2782 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2783 &mad_send_wc);
2784
2785 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1da177e4 2786 atomic_dec(&mad_agent_priv->refcount);
1d9bc6d6 2787 if (free_mad)
c9082e51 2788 kfree(local->mad_priv);
1da177e4
LT
2789 kfree(local);
2790 }
2791 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2792}
2793
f75b7a52
HR
2794static int retry_send(struct ib_mad_send_wr_private *mad_send_wr)
2795{
2796 int ret;
2797
4fc8cd49 2798 if (!mad_send_wr->retries_left)
f75b7a52
HR
2799 return -ETIMEDOUT;
2800
4fc8cd49
SH
2801 mad_send_wr->retries_left--;
2802 mad_send_wr->send_buf.retries++;
2803
34816ad9 2804 mad_send_wr->timeout = msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms);
f75b7a52 2805
1471cb6c 2806 if (ib_mad_kernel_rmpp_agent(&mad_send_wr->mad_agent_priv->agent)) {
fa619a77
HR
2807 ret = ib_retry_rmpp(mad_send_wr);
2808 switch (ret) {
2809 case IB_RMPP_RESULT_UNHANDLED:
2810 ret = ib_send_mad(mad_send_wr);
2811 break;
2812 case IB_RMPP_RESULT_CONSUMED:
2813 ret = 0;
2814 break;
2815 default:
2816 ret = -ECOMM;
2817 break;
2818 }
2819 } else
2820 ret = ib_send_mad(mad_send_wr);
f75b7a52
HR
2821
2822 if (!ret) {
2823 mad_send_wr->refcount++;
f75b7a52
HR
2824 list_add_tail(&mad_send_wr->agent_list,
2825 &mad_send_wr->mad_agent_priv->send_list);
2826 }
2827 return ret;
2828}
2829
c4028958 2830static void timeout_sends(struct work_struct *work)
1da177e4
LT
2831{
2832 struct ib_mad_agent_private *mad_agent_priv;
2833 struct ib_mad_send_wr_private *mad_send_wr;
2834 struct ib_mad_send_wc mad_send_wc;
2835 unsigned long flags, delay;
2836
c4028958
DH
2837 mad_agent_priv = container_of(work, struct ib_mad_agent_private,
2838 timed_work.work);
1da177e4
LT
2839 mad_send_wc.vendor_err = 0;
2840
2841 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2842 while (!list_empty(&mad_agent_priv->wait_list)) {
2843 mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
2844 struct ib_mad_send_wr_private,
2845 agent_list);
2846
2847 if (time_after(mad_send_wr->timeout, jiffies)) {
2848 delay = mad_send_wr->timeout - jiffies;
2849 if ((long)delay <= 0)
2850 delay = 1;
2851 queue_delayed_work(mad_agent_priv->qp_info->
2852 port_priv->wq,
2853 &mad_agent_priv->timed_work, delay);
2854 break;
2855 }
2856
dbf9227b 2857 list_del(&mad_send_wr->agent_list);
29bb33dd
HR
2858 if (mad_send_wr->status == IB_WC_SUCCESS &&
2859 !retry_send(mad_send_wr))
f75b7a52
HR
2860 continue;
2861
1da177e4
LT
2862 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2863
03b61ad2
HR
2864 if (mad_send_wr->status == IB_WC_SUCCESS)
2865 mad_send_wc.status = IB_WC_RESP_TIMEOUT_ERR;
2866 else
2867 mad_send_wc.status = mad_send_wr->status;
34816ad9 2868 mad_send_wc.send_buf = &mad_send_wr->send_buf;
1da177e4
LT
2869 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2870 &mad_send_wc);
2871
1da177e4
LT
2872 atomic_dec(&mad_agent_priv->refcount);
2873 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2874 }
2875 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2876}
2877
5dd2ce12 2878static void ib_mad_thread_completion_handler(struct ib_cq *cq, void *arg)
1da177e4
LT
2879{
2880 struct ib_mad_port_private *port_priv = cq->cq_context;
dc05980d 2881 unsigned long flags;
1da177e4 2882
dc05980d
MT
2883 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2884 if (!list_empty(&port_priv->port_list))
2885 queue_work(port_priv->wq, &port_priv->work);
2886 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
1da177e4
LT
2887}
2888
2889/*
2890 * Allocate receive MADs and post receive WRs for them
2891 */
2892static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
2893 struct ib_mad_private *mad)
2894{
2895 unsigned long flags;
2896 int post, ret;
2897 struct ib_mad_private *mad_priv;
2898 struct ib_sge sg_list;
2899 struct ib_recv_wr recv_wr, *bad_recv_wr;
2900 struct ib_mad_queue *recv_queue = &qp_info->recv_queue;
2901
2902 /* Initialize common scatter list fields */
4be90bc6 2903 sg_list.lkey = qp_info->port_priv->pd->local_dma_lkey;
1da177e4
LT
2904
2905 /* Initialize common receive WR fields */
2906 recv_wr.next = NULL;
2907 recv_wr.sg_list = &sg_list;
2908 recv_wr.num_sge = 1;
2909
2910 do {
2911 /* Allocate and map receive buffer */
2912 if (mad) {
2913 mad_priv = mad;
2914 mad = NULL;
2915 } else {
c9082e51
IW
2916 mad_priv = alloc_mad_private(port_mad_size(qp_info->port_priv),
2917 GFP_ATOMIC);
1da177e4 2918 if (!mad_priv) {
7ef5d4b0
IW
2919 dev_err(&qp_info->port_priv->device->dev,
2920 "No memory for receive buffer\n");
1da177e4
LT
2921 ret = -ENOMEM;
2922 break;
2923 }
2924 }
c9082e51 2925 sg_list.length = mad_priv_dma_size(mad_priv);
1527106f
RC
2926 sg_list.addr = ib_dma_map_single(qp_info->port_priv->device,
2927 &mad_priv->grh,
c9082e51 2928 mad_priv_dma_size(mad_priv),
1527106f 2929 DMA_FROM_DEVICE);
2c34e68f
YB
2930 if (unlikely(ib_dma_mapping_error(qp_info->port_priv->device,
2931 sg_list.addr))) {
2932 ret = -ENOMEM;
2933 break;
2934 }
1527106f 2935 mad_priv->header.mapping = sg_list.addr;
1da177e4
LT
2936 recv_wr.wr_id = (unsigned long)&mad_priv->header.mad_list;
2937 mad_priv->header.mad_list.mad_queue = recv_queue;
2938
2939 /* Post receive WR */
2940 spin_lock_irqsave(&recv_queue->lock, flags);
2941 post = (++recv_queue->count < recv_queue->max_active);
2942 list_add_tail(&mad_priv->header.mad_list.list, &recv_queue->list);
2943 spin_unlock_irqrestore(&recv_queue->lock, flags);
2944 ret = ib_post_recv(qp_info->qp, &recv_wr, &bad_recv_wr);
2945 if (ret) {
2946 spin_lock_irqsave(&recv_queue->lock, flags);
2947 list_del(&mad_priv->header.mad_list.list);
2948 recv_queue->count--;
2949 spin_unlock_irqrestore(&recv_queue->lock, flags);
1527106f
RC
2950 ib_dma_unmap_single(qp_info->port_priv->device,
2951 mad_priv->header.mapping,
c9082e51 2952 mad_priv_dma_size(mad_priv),
1527106f 2953 DMA_FROM_DEVICE);
c9082e51 2954 kfree(mad_priv);
7ef5d4b0
IW
2955 dev_err(&qp_info->port_priv->device->dev,
2956 "ib_post_recv failed: %d\n", ret);
1da177e4
LT
2957 break;
2958 }
2959 } while (post);
2960
2961 return ret;
2962}
2963
2964/*
2965 * Return all the posted receive MADs
2966 */
2967static void cleanup_recv_queue(struct ib_mad_qp_info *qp_info)
2968{
2969 struct ib_mad_private_header *mad_priv_hdr;
2970 struct ib_mad_private *recv;
2971 struct ib_mad_list_head *mad_list;
2972
fac70d51
EC
2973 if (!qp_info->qp)
2974 return;
2975
1da177e4
LT
2976 while (!list_empty(&qp_info->recv_queue.list)) {
2977
2978 mad_list = list_entry(qp_info->recv_queue.list.next,
2979 struct ib_mad_list_head, list);
2980 mad_priv_hdr = container_of(mad_list,
2981 struct ib_mad_private_header,
2982 mad_list);
2983 recv = container_of(mad_priv_hdr, struct ib_mad_private,
2984 header);
2985
2986 /* Remove from posted receive MAD list */
2987 list_del(&mad_list->list);
2988
1527106f
RC
2989 ib_dma_unmap_single(qp_info->port_priv->device,
2990 recv->header.mapping,
c9082e51 2991 mad_priv_dma_size(recv),
1527106f 2992 DMA_FROM_DEVICE);
c9082e51 2993 kfree(recv);
1da177e4
LT
2994 }
2995
2996 qp_info->recv_queue.count = 0;
2997}
2998
2999/*
3000 * Start the port
3001 */
3002static int ib_mad_port_start(struct ib_mad_port_private *port_priv)
3003{
3004 int ret, i;
3005 struct ib_qp_attr *attr;
3006 struct ib_qp *qp;
ef5ed416 3007 u16 pkey_index;
1da177e4
LT
3008
3009 attr = kmalloc(sizeof *attr, GFP_KERNEL);
3cd96564 3010 if (!attr) {
7ef5d4b0
IW
3011 dev_err(&port_priv->device->dev,
3012 "Couldn't kmalloc ib_qp_attr\n");
1da177e4
LT
3013 return -ENOMEM;
3014 }
3015
ef5ed416
JM
3016 ret = ib_find_pkey(port_priv->device, port_priv->port_num,
3017 IB_DEFAULT_PKEY_FULL, &pkey_index);
3018 if (ret)
3019 pkey_index = 0;
3020
1da177e4
LT
3021 for (i = 0; i < IB_MAD_QPS_CORE; i++) {
3022 qp = port_priv->qp_info[i].qp;
fac70d51
EC
3023 if (!qp)
3024 continue;
3025
1da177e4
LT
3026 /*
3027 * PKey index for QP1 is irrelevant but
3028 * one is needed for the Reset to Init transition
3029 */
3030 attr->qp_state = IB_QPS_INIT;
ef5ed416 3031 attr->pkey_index = pkey_index;
1da177e4
LT
3032 attr->qkey = (qp->qp_num == 0) ? 0 : IB_QP1_QKEY;
3033 ret = ib_modify_qp(qp, attr, IB_QP_STATE |
3034 IB_QP_PKEY_INDEX | IB_QP_QKEY);
3035 if (ret) {
7ef5d4b0
IW
3036 dev_err(&port_priv->device->dev,
3037 "Couldn't change QP%d state to INIT: %d\n",
3038 i, ret);
1da177e4
LT
3039 goto out;
3040 }
3041
3042 attr->qp_state = IB_QPS_RTR;
3043 ret = ib_modify_qp(qp, attr, IB_QP_STATE);
3044 if (ret) {
7ef5d4b0
IW
3045 dev_err(&port_priv->device->dev,
3046 "Couldn't change QP%d state to RTR: %d\n",
3047 i, ret);
1da177e4
LT
3048 goto out;
3049 }
3050
3051 attr->qp_state = IB_QPS_RTS;
3052 attr->sq_psn = IB_MAD_SEND_Q_PSN;
3053 ret = ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_SQ_PSN);
3054 if (ret) {
7ef5d4b0
IW
3055 dev_err(&port_priv->device->dev,
3056 "Couldn't change QP%d state to RTS: %d\n",
3057 i, ret);
1da177e4
LT
3058 goto out;
3059 }
3060 }
3061
3062 ret = ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
3063 if (ret) {
7ef5d4b0
IW
3064 dev_err(&port_priv->device->dev,
3065 "Failed to request completion notification: %d\n",
3066 ret);
1da177e4
LT
3067 goto out;
3068 }
3069
3070 for (i = 0; i < IB_MAD_QPS_CORE; i++) {
fac70d51
EC
3071 if (!port_priv->qp_info[i].qp)
3072 continue;
3073
1da177e4
LT
3074 ret = ib_mad_post_receive_mads(&port_priv->qp_info[i], NULL);
3075 if (ret) {
7ef5d4b0
IW
3076 dev_err(&port_priv->device->dev,
3077 "Couldn't post receive WRs\n");
1da177e4
LT
3078 goto out;
3079 }
3080 }
3081out:
3082 kfree(attr);
3083 return ret;
3084}
3085
3086static void qp_event_handler(struct ib_event *event, void *qp_context)
3087{
3088 struct ib_mad_qp_info *qp_info = qp_context;
3089
3090 /* It's worse than that! He's dead, Jim! */
7ef5d4b0
IW
3091 dev_err(&qp_info->port_priv->device->dev,
3092 "Fatal error (%d) on MAD QP (%d)\n",
1da177e4
LT
3093 event->event, qp_info->qp->qp_num);
3094}
3095
3096static void init_mad_queue(struct ib_mad_qp_info *qp_info,
3097 struct ib_mad_queue *mad_queue)
3098{
3099 mad_queue->qp_info = qp_info;
3100 mad_queue->count = 0;
3101 spin_lock_init(&mad_queue->lock);
3102 INIT_LIST_HEAD(&mad_queue->list);
3103}
3104
3105static void init_mad_qp(struct ib_mad_port_private *port_priv,
3106 struct ib_mad_qp_info *qp_info)
3107{
3108 qp_info->port_priv = port_priv;
3109 init_mad_queue(qp_info, &qp_info->send_queue);
3110 init_mad_queue(qp_info, &qp_info->recv_queue);
3111 INIT_LIST_HEAD(&qp_info->overflow_list);
3112 spin_lock_init(&qp_info->snoop_lock);
3113 qp_info->snoop_table = NULL;
3114 qp_info->snoop_table_size = 0;
3115 atomic_set(&qp_info->snoop_count, 0);
3116}
3117
3118static int create_mad_qp(struct ib_mad_qp_info *qp_info,
3119 enum ib_qp_type qp_type)
3120{
3121 struct ib_qp_init_attr qp_init_attr;
3122 int ret;
3123
3124 memset(&qp_init_attr, 0, sizeof qp_init_attr);
3125 qp_init_attr.send_cq = qp_info->port_priv->cq;
3126 qp_init_attr.recv_cq = qp_info->port_priv->cq;
3127 qp_init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
b76aabc3
HR
3128 qp_init_attr.cap.max_send_wr = mad_sendq_size;
3129 qp_init_attr.cap.max_recv_wr = mad_recvq_size;
1da177e4
LT
3130 qp_init_attr.cap.max_send_sge = IB_MAD_SEND_REQ_MAX_SG;
3131 qp_init_attr.cap.max_recv_sge = IB_MAD_RECV_REQ_MAX_SG;
3132 qp_init_attr.qp_type = qp_type;
3133 qp_init_attr.port_num = qp_info->port_priv->port_num;
3134 qp_init_attr.qp_context = qp_info;
3135 qp_init_attr.event_handler = qp_event_handler;
3136 qp_info->qp = ib_create_qp(qp_info->port_priv->pd, &qp_init_attr);
3137 if (IS_ERR(qp_info->qp)) {
7ef5d4b0
IW
3138 dev_err(&qp_info->port_priv->device->dev,
3139 "Couldn't create ib_mad QP%d\n",
3140 get_spl_qp_index(qp_type));
1da177e4
LT
3141 ret = PTR_ERR(qp_info->qp);
3142 goto error;
3143 }
3144 /* Use minimum queue sizes unless the CQ is resized */
b76aabc3
HR
3145 qp_info->send_queue.max_active = mad_sendq_size;
3146 qp_info->recv_queue.max_active = mad_recvq_size;
1da177e4
LT
3147 return 0;
3148
3149error:
3150 return ret;
3151}
3152
3153static void destroy_mad_qp(struct ib_mad_qp_info *qp_info)
3154{
fac70d51
EC
3155 if (!qp_info->qp)
3156 return;
3157
1da177e4 3158 ib_destroy_qp(qp_info->qp);
6044ec88 3159 kfree(qp_info->snoop_table);
1da177e4
LT
3160}
3161
3162/*
3163 * Open the port
3164 * Create the QP, PD, MR, and CQ if needed
3165 */
3166static int ib_mad_port_open(struct ib_device *device,
3167 int port_num)
3168{
3169 int ret, cq_size;
3170 struct ib_mad_port_private *port_priv;
3171 unsigned long flags;
3172 char name[sizeof "ib_mad123"];
fac70d51 3173 int has_smi;
8e37210b 3174 struct ib_cq_init_attr cq_attr = {};
1da177e4 3175
337877a4
IW
3176 if (WARN_ON(rdma_max_mad_size(device, port_num) < IB_MGMT_MAD_SIZE))
3177 return -EFAULT;
3178
548ead17
IW
3179 if (WARN_ON(rdma_cap_opa_mad(device, port_num) &&
3180 rdma_max_mad_size(device, port_num) < OPA_MGMT_MAD_SIZE))
3181 return -EFAULT;
3182
1da177e4 3183 /* Create new device info */
de6eb66b 3184 port_priv = kzalloc(sizeof *port_priv, GFP_KERNEL);
1da177e4 3185 if (!port_priv) {
7ef5d4b0 3186 dev_err(&device->dev, "No memory for ib_mad_port_private\n");
1da177e4
LT
3187 return -ENOMEM;
3188 }
de6eb66b 3189
1da177e4
LT
3190 port_priv->device = device;
3191 port_priv->port_num = port_num;
3192 spin_lock_init(&port_priv->reg_lock);
3193 INIT_LIST_HEAD(&port_priv->agent_list);
3194 init_mad_qp(port_priv, &port_priv->qp_info[0]);
3195 init_mad_qp(port_priv, &port_priv->qp_info[1]);
3196
fac70d51 3197 cq_size = mad_sendq_size + mad_recvq_size;
29541e3a 3198 has_smi = rdma_cap_ib_smi(device, port_num);
fac70d51
EC
3199 if (has_smi)
3200 cq_size *= 2;
3201
8e37210b 3202 cq_attr.cqe = cq_size;
1da177e4 3203 port_priv->cq = ib_create_cq(port_priv->device,
5dd2ce12 3204 ib_mad_thread_completion_handler,
8e37210b 3205 NULL, port_priv, &cq_attr);
1da177e4 3206 if (IS_ERR(port_priv->cq)) {
7ef5d4b0 3207 dev_err(&device->dev, "Couldn't create ib_mad CQ\n");
1da177e4
LT
3208 ret = PTR_ERR(port_priv->cq);
3209 goto error3;
3210 }
3211
3212 port_priv->pd = ib_alloc_pd(device);
3213 if (IS_ERR(port_priv->pd)) {
7ef5d4b0 3214 dev_err(&device->dev, "Couldn't create ib_mad PD\n");
1da177e4
LT
3215 ret = PTR_ERR(port_priv->pd);
3216 goto error4;
3217 }
3218
fac70d51
EC
3219 if (has_smi) {
3220 ret = create_mad_qp(&port_priv->qp_info[0], IB_QPT_SMI);
3221 if (ret)
3222 goto error6;
3223 }
1da177e4
LT
3224 ret = create_mad_qp(&port_priv->qp_info[1], IB_QPT_GSI);
3225 if (ret)
3226 goto error7;
3227
3228 snprintf(name, sizeof name, "ib_mad%d", port_num);
3229 port_priv->wq = create_singlethread_workqueue(name);
3230 if (!port_priv->wq) {
3231 ret = -ENOMEM;
3232 goto error8;
3233 }
c4028958 3234 INIT_WORK(&port_priv->work, ib_mad_completion_handler);
1da177e4 3235
dc05980d
MT
3236 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
3237 list_add_tail(&port_priv->port_list, &ib_mad_port_list);
3238 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
3239
1da177e4
LT
3240 ret = ib_mad_port_start(port_priv);
3241 if (ret) {
7ef5d4b0 3242 dev_err(&device->dev, "Couldn't start port\n");
1da177e4
LT
3243 goto error9;
3244 }
3245
1da177e4
LT
3246 return 0;
3247
3248error9:
dc05980d
MT
3249 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
3250 list_del_init(&port_priv->port_list);
3251 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
3252
1da177e4
LT
3253 destroy_workqueue(port_priv->wq);
3254error8:
3255 destroy_mad_qp(&port_priv->qp_info[1]);
3256error7:
3257 destroy_mad_qp(&port_priv->qp_info[0]);
3258error6:
1da177e4
LT
3259 ib_dealloc_pd(port_priv->pd);
3260error4:
3261 ib_destroy_cq(port_priv->cq);
3262 cleanup_recv_queue(&port_priv->qp_info[1]);
3263 cleanup_recv_queue(&port_priv->qp_info[0]);
3264error3:
3265 kfree(port_priv);
3266
3267 return ret;
3268}
3269
3270/*
3271 * Close the port
3272 * If there are no classes using the port, free the port
3273 * resources (CQ, MR, PD, QP) and remove the port's info structure
3274 */
3275static int ib_mad_port_close(struct ib_device *device, int port_num)
3276{
3277 struct ib_mad_port_private *port_priv;
3278 unsigned long flags;
3279
3280 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
3281 port_priv = __ib_get_mad_port(device, port_num);
3282 if (port_priv == NULL) {
3283 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
7ef5d4b0 3284 dev_err(&device->dev, "Port %d not found\n", port_num);
1da177e4
LT
3285 return -ENODEV;
3286 }
dc05980d 3287 list_del_init(&port_priv->port_list);
1da177e4
LT
3288 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
3289
1da177e4
LT
3290 destroy_workqueue(port_priv->wq);
3291 destroy_mad_qp(&port_priv->qp_info[1]);
3292 destroy_mad_qp(&port_priv->qp_info[0]);
1da177e4
LT
3293 ib_dealloc_pd(port_priv->pd);
3294 ib_destroy_cq(port_priv->cq);
3295 cleanup_recv_queue(&port_priv->qp_info[1]);
3296 cleanup_recv_queue(&port_priv->qp_info[0]);
3297 /* XXX: Handle deallocation of MAD registration tables */
3298
3299 kfree(port_priv);
3300
3301 return 0;
3302}
3303
3304static void ib_mad_init_device(struct ib_device *device)
3305{
4139032b 3306 int start, i;
1da177e4 3307
4139032b 3308 start = rdma_start_port(device);
4ab6fb7e 3309
4139032b 3310 for (i = start; i <= rdma_end_port(device); i++) {
c757dea8 3311 if (!rdma_cap_ib_mad(device, i))
827f2a8b
MW
3312 continue;
3313
4ab6fb7e 3314 if (ib_mad_port_open(device, i)) {
7ef5d4b0 3315 dev_err(&device->dev, "Couldn't open port %d\n", i);
4ab6fb7e 3316 goto error;
1da177e4 3317 }
4ab6fb7e 3318 if (ib_agent_port_open(device, i)) {
7ef5d4b0
IW
3319 dev_err(&device->dev,
3320 "Couldn't open port %d for agents\n", i);
4ab6fb7e 3321 goto error_agent;
1da177e4
LT
3322 }
3323 }
f68bcc2d 3324 return;
1da177e4 3325
4ab6fb7e
RD
3326error_agent:
3327 if (ib_mad_port_close(device, i))
7ef5d4b0 3328 dev_err(&device->dev, "Couldn't close port %d\n", i);
4ab6fb7e
RD
3329
3330error:
827f2a8b 3331 while (--i >= start) {
c757dea8 3332 if (!rdma_cap_ib_mad(device, i))
827f2a8b 3333 continue;
4ab6fb7e 3334
4ab6fb7e 3335 if (ib_agent_port_close(device, i))
7ef5d4b0
IW
3336 dev_err(&device->dev,
3337 "Couldn't close port %d for agents\n", i);
4ab6fb7e 3338 if (ib_mad_port_close(device, i))
7ef5d4b0 3339 dev_err(&device->dev, "Couldn't close port %d\n", i);
1da177e4 3340 }
1da177e4
LT
3341}
3342
7c1eb45a 3343static void ib_mad_remove_device(struct ib_device *device, void *client_data)
1da177e4 3344{
4139032b 3345 int i;
827f2a8b 3346
4139032b 3347 for (i = rdma_start_port(device); i <= rdma_end_port(device); i++) {
c757dea8 3348 if (!rdma_cap_ib_mad(device, i))
827f2a8b
MW
3349 continue;
3350
3351 if (ib_agent_port_close(device, i))
7ef5d4b0 3352 dev_err(&device->dev,
827f2a8b
MW
3353 "Couldn't close port %d for agents\n", i);
3354 if (ib_mad_port_close(device, i))
3355 dev_err(&device->dev, "Couldn't close port %d\n", i);
1da177e4
LT
3356 }
3357}
3358
3359static struct ib_client mad_client = {
3360 .name = "mad",
3361 .add = ib_mad_init_device,
3362 .remove = ib_mad_remove_device
3363};
3364
3365static int __init ib_mad_init_module(void)
3366{
b76aabc3
HR
3367 mad_recvq_size = min(mad_recvq_size, IB_MAD_QP_MAX_SIZE);
3368 mad_recvq_size = max(mad_recvq_size, IB_MAD_QP_MIN_SIZE);
3369
3370 mad_sendq_size = min(mad_sendq_size, IB_MAD_QP_MAX_SIZE);
3371 mad_sendq_size = max(mad_sendq_size, IB_MAD_QP_MIN_SIZE);
3372
1da177e4
LT
3373 INIT_LIST_HEAD(&ib_mad_port_list);
3374
3375 if (ib_register_client(&mad_client)) {
7ef5d4b0 3376 pr_err("Couldn't register ib_mad client\n");
c9082e51 3377 return -EINVAL;
1da177e4
LT
3378 }
3379
3380 return 0;
1da177e4
LT
3381}
3382
3383static void __exit ib_mad_cleanup_module(void)
3384{
3385 ib_unregister_client(&mad_client);
1da177e4
LT
3386}
3387
3388module_init(ib_mad_init_module);
3389module_exit(ib_mad_cleanup_module);
This page took 1.043299 seconds and 5 git commands to generate.