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