dm writecache: add cond_resched to loop in persistent_memory_claim()
[linux/fpc-iii.git] / drivers / infiniband / core / mad.c
blob049c9cdc10dec094d651f78f26888abc03195faa
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,2018 Intel Corporation. All rights reserved.
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:
14 * Redistribution and use in source and binary forms, with or
15 * without modification, are permitted provided that the following
16 * conditions are met:
18 * - Redistributions of source code must retain the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer.
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.
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.
38 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40 #include <linux/dma-mapping.h>
41 #include <linux/slab.h>
42 #include <linux/module.h>
43 #include <linux/security.h>
44 #include <linux/xarray.h>
45 #include <rdma/ib_cache.h>
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"
54 #define CREATE_TRACE_POINTS
55 #include <trace/events/ib_mad.h>
57 #ifdef CONFIG_TRACEPOINTS
58 static void create_mad_addr_info(struct ib_mad_send_wr_private *mad_send_wr,
59 struct ib_mad_qp_info *qp_info,
60 struct trace_event_raw_ib_mad_send_template *entry)
62 u16 pkey;
63 struct ib_device *dev = qp_info->port_priv->device;
64 u8 pnum = qp_info->port_priv->port_num;
65 struct ib_ud_wr *wr = &mad_send_wr->send_wr;
66 struct rdma_ah_attr attr = {};
68 rdma_query_ah(wr->ah, &attr);
70 /* These are common */
71 entry->sl = attr.sl;
72 ib_query_pkey(dev, pnum, wr->pkey_index, &pkey);
73 entry->pkey = pkey;
74 entry->rqpn = wr->remote_qpn;
75 entry->rqkey = wr->remote_qkey;
76 entry->dlid = rdma_ah_get_dlid(&attr);
78 #endif
80 static int mad_sendq_size = IB_MAD_QP_SEND_SIZE;
81 static int mad_recvq_size = IB_MAD_QP_RECV_SIZE;
83 module_param_named(send_queue_size, mad_sendq_size, int, 0444);
84 MODULE_PARM_DESC(send_queue_size, "Size of send queue in number of work requests");
85 module_param_named(recv_queue_size, mad_recvq_size, int, 0444);
86 MODULE_PARM_DESC(recv_queue_size, "Size of receive queue in number of work requests");
88 /* Client ID 0 is used for snoop-only clients */
89 static DEFINE_XARRAY_ALLOC1(ib_mad_clients);
90 static u32 ib_mad_client_next;
91 static struct list_head ib_mad_port_list;
93 /* Port list lock */
94 static DEFINE_SPINLOCK(ib_mad_port_list_lock);
96 /* Forward declarations */
97 static int method_in_use(struct ib_mad_mgmt_method_table **method,
98 struct ib_mad_reg_req *mad_reg_req);
99 static void remove_mad_reg_req(struct ib_mad_agent_private *priv);
100 static struct ib_mad_agent_private *find_mad_agent(
101 struct ib_mad_port_private *port_priv,
102 const struct ib_mad_hdr *mad);
103 static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
104 struct ib_mad_private *mad);
105 static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv);
106 static void timeout_sends(struct work_struct *work);
107 static void local_completions(struct work_struct *work);
108 static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
109 struct ib_mad_agent_private *agent_priv,
110 u8 mgmt_class);
111 static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
112 struct ib_mad_agent_private *agent_priv);
113 static bool ib_mad_send_error(struct ib_mad_port_private *port_priv,
114 struct ib_wc *wc);
115 static void ib_mad_send_done(struct ib_cq *cq, struct ib_wc *wc);
118 * Returns a ib_mad_port_private structure or NULL for a device/port
119 * Assumes ib_mad_port_list_lock is being held
121 static inline struct ib_mad_port_private *
122 __ib_get_mad_port(struct ib_device *device, int port_num)
124 struct ib_mad_port_private *entry;
126 list_for_each_entry(entry, &ib_mad_port_list, port_list) {
127 if (entry->device == device && entry->port_num == port_num)
128 return entry;
130 return NULL;
134 * Wrapper function to return a ib_mad_port_private structure or NULL
135 * for a device/port
137 static inline struct ib_mad_port_private *
138 ib_get_mad_port(struct ib_device *device, int port_num)
140 struct ib_mad_port_private *entry;
141 unsigned long flags;
143 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
144 entry = __ib_get_mad_port(device, port_num);
145 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
147 return entry;
150 static inline u8 convert_mgmt_class(u8 mgmt_class)
152 /* Alias IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE to 0 */
153 return mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE ?
154 0 : mgmt_class;
157 static int get_spl_qp_index(enum ib_qp_type qp_type)
159 switch (qp_type)
161 case IB_QPT_SMI:
162 return 0;
163 case IB_QPT_GSI:
164 return 1;
165 default:
166 return -1;
170 static int vendor_class_index(u8 mgmt_class)
172 return mgmt_class - IB_MGMT_CLASS_VENDOR_RANGE2_START;
175 static int is_vendor_class(u8 mgmt_class)
177 if ((mgmt_class < IB_MGMT_CLASS_VENDOR_RANGE2_START) ||
178 (mgmt_class > IB_MGMT_CLASS_VENDOR_RANGE2_END))
179 return 0;
180 return 1;
183 static int is_vendor_oui(char *oui)
185 if (oui[0] || oui[1] || oui[2])
186 return 1;
187 return 0;
190 static int is_vendor_method_in_use(
191 struct ib_mad_mgmt_vendor_class *vendor_class,
192 struct ib_mad_reg_req *mad_reg_req)
194 struct ib_mad_mgmt_method_table *method;
195 int i;
197 for (i = 0; i < MAX_MGMT_OUI; i++) {
198 if (!memcmp(vendor_class->oui[i], mad_reg_req->oui, 3)) {
199 method = vendor_class->method_table[i];
200 if (method) {
201 if (method_in_use(&method, mad_reg_req))
202 return 1;
203 else
204 break;
208 return 0;
211 int ib_response_mad(const struct ib_mad_hdr *hdr)
213 return ((hdr->method & IB_MGMT_METHOD_RESP) ||
214 (hdr->method == IB_MGMT_METHOD_TRAP_REPRESS) ||
215 ((hdr->mgmt_class == IB_MGMT_CLASS_BM) &&
216 (hdr->attr_mod & IB_BM_ATTR_MOD_RESP)));
218 EXPORT_SYMBOL(ib_response_mad);
221 * ib_register_mad_agent - Register to send/receive MADs
223 * Context: Process context.
225 struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
226 u8 port_num,
227 enum ib_qp_type qp_type,
228 struct ib_mad_reg_req *mad_reg_req,
229 u8 rmpp_version,
230 ib_mad_send_handler send_handler,
231 ib_mad_recv_handler recv_handler,
232 void *context,
233 u32 registration_flags)
235 struct ib_mad_port_private *port_priv;
236 struct ib_mad_agent *ret = ERR_PTR(-EINVAL);
237 struct ib_mad_agent_private *mad_agent_priv;
238 struct ib_mad_reg_req *reg_req = NULL;
239 struct ib_mad_mgmt_class_table *class;
240 struct ib_mad_mgmt_vendor_class_table *vendor;
241 struct ib_mad_mgmt_vendor_class *vendor_class;
242 struct ib_mad_mgmt_method_table *method;
243 int ret2, qpn;
244 u8 mgmt_class, vclass;
246 if ((qp_type == IB_QPT_SMI && !rdma_cap_ib_smi(device, port_num)) ||
247 (qp_type == IB_QPT_GSI && !rdma_cap_ib_cm(device, port_num)))
248 return ERR_PTR(-EPROTONOSUPPORT);
250 /* Validate parameters */
251 qpn = get_spl_qp_index(qp_type);
252 if (qpn == -1) {
253 dev_dbg_ratelimited(&device->dev, "%s: invalid QP Type %d\n",
254 __func__, qp_type);
255 goto error1;
258 if (rmpp_version && rmpp_version != IB_MGMT_RMPP_VERSION) {
259 dev_dbg_ratelimited(&device->dev,
260 "%s: invalid RMPP Version %u\n",
261 __func__, rmpp_version);
262 goto error1;
265 /* Validate MAD registration request if supplied */
266 if (mad_reg_req) {
267 if (mad_reg_req->mgmt_class_version >= MAX_MGMT_VERSION) {
268 dev_dbg_ratelimited(&device->dev,
269 "%s: invalid Class Version %u\n",
270 __func__,
271 mad_reg_req->mgmt_class_version);
272 goto error1;
274 if (!recv_handler) {
275 dev_dbg_ratelimited(&device->dev,
276 "%s: no recv_handler\n", __func__);
277 goto error1;
279 if (mad_reg_req->mgmt_class >= MAX_MGMT_CLASS) {
281 * IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE is the only
282 * one in this range currently allowed
284 if (mad_reg_req->mgmt_class !=
285 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
286 dev_dbg_ratelimited(&device->dev,
287 "%s: Invalid Mgmt Class 0x%x\n",
288 __func__, mad_reg_req->mgmt_class);
289 goto error1;
291 } else if (mad_reg_req->mgmt_class == 0) {
293 * Class 0 is reserved in IBA and is used for
294 * aliasing of IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
296 dev_dbg_ratelimited(&device->dev,
297 "%s: Invalid Mgmt Class 0\n",
298 __func__);
299 goto error1;
300 } else if (is_vendor_class(mad_reg_req->mgmt_class)) {
302 * If class is in "new" vendor range,
303 * ensure supplied OUI is not zero
305 if (!is_vendor_oui(mad_reg_req->oui)) {
306 dev_dbg_ratelimited(&device->dev,
307 "%s: No OUI specified for class 0x%x\n",
308 __func__,
309 mad_reg_req->mgmt_class);
310 goto error1;
313 /* Make sure class supplied is consistent with RMPP */
314 if (!ib_is_mad_class_rmpp(mad_reg_req->mgmt_class)) {
315 if (rmpp_version) {
316 dev_dbg_ratelimited(&device->dev,
317 "%s: RMPP version for non-RMPP class 0x%x\n",
318 __func__, mad_reg_req->mgmt_class);
319 goto error1;
323 /* Make sure class supplied is consistent with QP type */
324 if (qp_type == IB_QPT_SMI) {
325 if ((mad_reg_req->mgmt_class !=
326 IB_MGMT_CLASS_SUBN_LID_ROUTED) &&
327 (mad_reg_req->mgmt_class !=
328 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) {
329 dev_dbg_ratelimited(&device->dev,
330 "%s: Invalid SM QP type: class 0x%x\n",
331 __func__, mad_reg_req->mgmt_class);
332 goto error1;
334 } else {
335 if ((mad_reg_req->mgmt_class ==
336 IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
337 (mad_reg_req->mgmt_class ==
338 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) {
339 dev_dbg_ratelimited(&device->dev,
340 "%s: Invalid GS QP type: class 0x%x\n",
341 __func__, mad_reg_req->mgmt_class);
342 goto error1;
345 } else {
346 /* No registration request supplied */
347 if (!send_handler)
348 goto error1;
349 if (registration_flags & IB_MAD_USER_RMPP)
350 goto error1;
353 /* Validate device and port */
354 port_priv = ib_get_mad_port(device, port_num);
355 if (!port_priv) {
356 dev_dbg_ratelimited(&device->dev, "%s: Invalid port %d\n",
357 __func__, port_num);
358 ret = ERR_PTR(-ENODEV);
359 goto error1;
362 /* Verify the QP requested is supported. For example, Ethernet devices
363 * will not have QP0.
365 if (!port_priv->qp_info[qpn].qp) {
366 dev_dbg_ratelimited(&device->dev, "%s: QP %d not supported\n",
367 __func__, qpn);
368 ret = ERR_PTR(-EPROTONOSUPPORT);
369 goto error1;
372 /* Allocate structures */
373 mad_agent_priv = kzalloc(sizeof *mad_agent_priv, GFP_KERNEL);
374 if (!mad_agent_priv) {
375 ret = ERR_PTR(-ENOMEM);
376 goto error1;
379 if (mad_reg_req) {
380 reg_req = kmemdup(mad_reg_req, sizeof *reg_req, GFP_KERNEL);
381 if (!reg_req) {
382 ret = ERR_PTR(-ENOMEM);
383 goto error3;
387 /* Now, fill in the various structures */
388 mad_agent_priv->qp_info = &port_priv->qp_info[qpn];
389 mad_agent_priv->reg_req = reg_req;
390 mad_agent_priv->agent.rmpp_version = rmpp_version;
391 mad_agent_priv->agent.device = device;
392 mad_agent_priv->agent.recv_handler = recv_handler;
393 mad_agent_priv->agent.send_handler = send_handler;
394 mad_agent_priv->agent.context = context;
395 mad_agent_priv->agent.qp = port_priv->qp_info[qpn].qp;
396 mad_agent_priv->agent.port_num = port_num;
397 mad_agent_priv->agent.flags = registration_flags;
398 spin_lock_init(&mad_agent_priv->lock);
399 INIT_LIST_HEAD(&mad_agent_priv->send_list);
400 INIT_LIST_HEAD(&mad_agent_priv->wait_list);
401 INIT_LIST_HEAD(&mad_agent_priv->done_list);
402 INIT_LIST_HEAD(&mad_agent_priv->rmpp_list);
403 INIT_DELAYED_WORK(&mad_agent_priv->timed_work, timeout_sends);
404 INIT_LIST_HEAD(&mad_agent_priv->local_list);
405 INIT_WORK(&mad_agent_priv->local_work, local_completions);
406 atomic_set(&mad_agent_priv->refcount, 1);
407 init_completion(&mad_agent_priv->comp);
409 ret2 = ib_mad_agent_security_setup(&mad_agent_priv->agent, qp_type);
410 if (ret2) {
411 ret = ERR_PTR(ret2);
412 goto error4;
416 * The mlx4 driver uses the top byte to distinguish which virtual
417 * function generated the MAD, so we must avoid using it.
419 ret2 = xa_alloc_cyclic(&ib_mad_clients, &mad_agent_priv->agent.hi_tid,
420 mad_agent_priv, XA_LIMIT(0, (1 << 24) - 1),
421 &ib_mad_client_next, GFP_KERNEL);
422 if (ret2 < 0) {
423 ret = ERR_PTR(ret2);
424 goto error5;
428 * Make sure MAD registration (if supplied)
429 * is non overlapping with any existing ones
431 spin_lock_irq(&port_priv->reg_lock);
432 if (mad_reg_req) {
433 mgmt_class = convert_mgmt_class(mad_reg_req->mgmt_class);
434 if (!is_vendor_class(mgmt_class)) {
435 class = port_priv->version[mad_reg_req->
436 mgmt_class_version].class;
437 if (class) {
438 method = class->method_table[mgmt_class];
439 if (method) {
440 if (method_in_use(&method,
441 mad_reg_req))
442 goto error6;
445 ret2 = add_nonoui_reg_req(mad_reg_req, mad_agent_priv,
446 mgmt_class);
447 } else {
448 /* "New" vendor class range */
449 vendor = port_priv->version[mad_reg_req->
450 mgmt_class_version].vendor;
451 if (vendor) {
452 vclass = vendor_class_index(mgmt_class);
453 vendor_class = vendor->vendor_class[vclass];
454 if (vendor_class) {
455 if (is_vendor_method_in_use(
456 vendor_class,
457 mad_reg_req))
458 goto error6;
461 ret2 = add_oui_reg_req(mad_reg_req, mad_agent_priv);
463 if (ret2) {
464 ret = ERR_PTR(ret2);
465 goto error6;
468 spin_unlock_irq(&port_priv->reg_lock);
470 trace_ib_mad_create_agent(mad_agent_priv);
471 return &mad_agent_priv->agent;
472 error6:
473 spin_unlock_irq(&port_priv->reg_lock);
474 xa_erase(&ib_mad_clients, mad_agent_priv->agent.hi_tid);
475 error5:
476 ib_mad_agent_security_cleanup(&mad_agent_priv->agent);
477 error4:
478 kfree(reg_req);
479 error3:
480 kfree(mad_agent_priv);
481 error1:
482 return ret;
484 EXPORT_SYMBOL(ib_register_mad_agent);
486 static inline int is_snooping_sends(int mad_snoop_flags)
488 return (mad_snoop_flags &
489 (/*IB_MAD_SNOOP_POSTED_SENDS |
490 IB_MAD_SNOOP_RMPP_SENDS |*/
491 IB_MAD_SNOOP_SEND_COMPLETIONS /*|
492 IB_MAD_SNOOP_RMPP_SEND_COMPLETIONS*/));
495 static inline int is_snooping_recvs(int mad_snoop_flags)
497 return (mad_snoop_flags &
498 (IB_MAD_SNOOP_RECVS /*|
499 IB_MAD_SNOOP_RMPP_RECVS*/));
502 static int register_snoop_agent(struct ib_mad_qp_info *qp_info,
503 struct ib_mad_snoop_private *mad_snoop_priv)
505 struct ib_mad_snoop_private **new_snoop_table;
506 unsigned long flags;
507 int i;
509 spin_lock_irqsave(&qp_info->snoop_lock, flags);
510 /* Check for empty slot in array. */
511 for (i = 0; i < qp_info->snoop_table_size; i++)
512 if (!qp_info->snoop_table[i])
513 break;
515 if (i == qp_info->snoop_table_size) {
516 /* Grow table. */
517 new_snoop_table = krealloc(qp_info->snoop_table,
518 sizeof mad_snoop_priv *
519 (qp_info->snoop_table_size + 1),
520 GFP_ATOMIC);
521 if (!new_snoop_table) {
522 i = -ENOMEM;
523 goto out;
526 qp_info->snoop_table = new_snoop_table;
527 qp_info->snoop_table_size++;
529 qp_info->snoop_table[i] = mad_snoop_priv;
530 atomic_inc(&qp_info->snoop_count);
531 out:
532 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
533 return i;
536 struct ib_mad_agent *ib_register_mad_snoop(struct ib_device *device,
537 u8 port_num,
538 enum ib_qp_type qp_type,
539 int mad_snoop_flags,
540 ib_mad_snoop_handler snoop_handler,
541 ib_mad_recv_handler recv_handler,
542 void *context)
544 struct ib_mad_port_private *port_priv;
545 struct ib_mad_agent *ret;
546 struct ib_mad_snoop_private *mad_snoop_priv;
547 int qpn;
548 int err;
550 /* Validate parameters */
551 if ((is_snooping_sends(mad_snoop_flags) && !snoop_handler) ||
552 (is_snooping_recvs(mad_snoop_flags) && !recv_handler)) {
553 ret = ERR_PTR(-EINVAL);
554 goto error1;
556 qpn = get_spl_qp_index(qp_type);
557 if (qpn == -1) {
558 ret = ERR_PTR(-EINVAL);
559 goto error1;
561 port_priv = ib_get_mad_port(device, port_num);
562 if (!port_priv) {
563 ret = ERR_PTR(-ENODEV);
564 goto error1;
566 /* Allocate structures */
567 mad_snoop_priv = kzalloc(sizeof *mad_snoop_priv, GFP_KERNEL);
568 if (!mad_snoop_priv) {
569 ret = ERR_PTR(-ENOMEM);
570 goto error1;
573 /* Now, fill in the various structures */
574 mad_snoop_priv->qp_info = &port_priv->qp_info[qpn];
575 mad_snoop_priv->agent.device = device;
576 mad_snoop_priv->agent.recv_handler = recv_handler;
577 mad_snoop_priv->agent.snoop_handler = snoop_handler;
578 mad_snoop_priv->agent.context = context;
579 mad_snoop_priv->agent.qp = port_priv->qp_info[qpn].qp;
580 mad_snoop_priv->agent.port_num = port_num;
581 mad_snoop_priv->mad_snoop_flags = mad_snoop_flags;
582 init_completion(&mad_snoop_priv->comp);
584 err = ib_mad_agent_security_setup(&mad_snoop_priv->agent, qp_type);
585 if (err) {
586 ret = ERR_PTR(err);
587 goto error2;
590 mad_snoop_priv->snoop_index = register_snoop_agent(
591 &port_priv->qp_info[qpn],
592 mad_snoop_priv);
593 if (mad_snoop_priv->snoop_index < 0) {
594 ret = ERR_PTR(mad_snoop_priv->snoop_index);
595 goto error3;
598 atomic_set(&mad_snoop_priv->refcount, 1);
599 return &mad_snoop_priv->agent;
600 error3:
601 ib_mad_agent_security_cleanup(&mad_snoop_priv->agent);
602 error2:
603 kfree(mad_snoop_priv);
604 error1:
605 return ret;
607 EXPORT_SYMBOL(ib_register_mad_snoop);
609 static inline void deref_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
611 if (atomic_dec_and_test(&mad_agent_priv->refcount))
612 complete(&mad_agent_priv->comp);
615 static inline void deref_snoop_agent(struct ib_mad_snoop_private *mad_snoop_priv)
617 if (atomic_dec_and_test(&mad_snoop_priv->refcount))
618 complete(&mad_snoop_priv->comp);
621 static void unregister_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
623 struct ib_mad_port_private *port_priv;
625 /* Note that we could still be handling received MADs */
626 trace_ib_mad_unregister_agent(mad_agent_priv);
629 * Canceling all sends results in dropping received response
630 * MADs, preventing us from queuing additional work
632 cancel_mads(mad_agent_priv);
633 port_priv = mad_agent_priv->qp_info->port_priv;
634 cancel_delayed_work(&mad_agent_priv->timed_work);
636 spin_lock_irq(&port_priv->reg_lock);
637 remove_mad_reg_req(mad_agent_priv);
638 spin_unlock_irq(&port_priv->reg_lock);
639 xa_erase(&ib_mad_clients, mad_agent_priv->agent.hi_tid);
641 flush_workqueue(port_priv->wq);
643 deref_mad_agent(mad_agent_priv);
644 wait_for_completion(&mad_agent_priv->comp);
645 ib_cancel_rmpp_recvs(mad_agent_priv);
647 ib_mad_agent_security_cleanup(&mad_agent_priv->agent);
649 kfree(mad_agent_priv->reg_req);
650 kfree_rcu(mad_agent_priv, rcu);
653 static void unregister_mad_snoop(struct ib_mad_snoop_private *mad_snoop_priv)
655 struct ib_mad_qp_info *qp_info;
656 unsigned long flags;
658 qp_info = mad_snoop_priv->qp_info;
659 spin_lock_irqsave(&qp_info->snoop_lock, flags);
660 qp_info->snoop_table[mad_snoop_priv->snoop_index] = NULL;
661 atomic_dec(&qp_info->snoop_count);
662 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
664 deref_snoop_agent(mad_snoop_priv);
665 wait_for_completion(&mad_snoop_priv->comp);
667 ib_mad_agent_security_cleanup(&mad_snoop_priv->agent);
669 kfree(mad_snoop_priv);
673 * ib_unregister_mad_agent - Unregisters a client from using MAD services
675 * Context: Process context.
677 void ib_unregister_mad_agent(struct ib_mad_agent *mad_agent)
679 struct ib_mad_agent_private *mad_agent_priv;
680 struct ib_mad_snoop_private *mad_snoop_priv;
682 /* If the TID is zero, the agent can only snoop. */
683 if (mad_agent->hi_tid) {
684 mad_agent_priv = container_of(mad_agent,
685 struct ib_mad_agent_private,
686 agent);
687 unregister_mad_agent(mad_agent_priv);
688 } else {
689 mad_snoop_priv = container_of(mad_agent,
690 struct ib_mad_snoop_private,
691 agent);
692 unregister_mad_snoop(mad_snoop_priv);
695 EXPORT_SYMBOL(ib_unregister_mad_agent);
697 static void dequeue_mad(struct ib_mad_list_head *mad_list)
699 struct ib_mad_queue *mad_queue;
700 unsigned long flags;
702 mad_queue = mad_list->mad_queue;
703 spin_lock_irqsave(&mad_queue->lock, flags);
704 list_del(&mad_list->list);
705 mad_queue->count--;
706 spin_unlock_irqrestore(&mad_queue->lock, flags);
709 static void snoop_send(struct ib_mad_qp_info *qp_info,
710 struct ib_mad_send_buf *send_buf,
711 struct ib_mad_send_wc *mad_send_wc,
712 int mad_snoop_flags)
714 struct ib_mad_snoop_private *mad_snoop_priv;
715 unsigned long flags;
716 int i;
718 spin_lock_irqsave(&qp_info->snoop_lock, flags);
719 for (i = 0; i < qp_info->snoop_table_size; i++) {
720 mad_snoop_priv = qp_info->snoop_table[i];
721 if (!mad_snoop_priv ||
722 !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
723 continue;
725 atomic_inc(&mad_snoop_priv->refcount);
726 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
727 mad_snoop_priv->agent.snoop_handler(&mad_snoop_priv->agent,
728 send_buf, mad_send_wc);
729 deref_snoop_agent(mad_snoop_priv);
730 spin_lock_irqsave(&qp_info->snoop_lock, flags);
732 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
735 static void snoop_recv(struct ib_mad_qp_info *qp_info,
736 struct ib_mad_recv_wc *mad_recv_wc,
737 int mad_snoop_flags)
739 struct ib_mad_snoop_private *mad_snoop_priv;
740 unsigned long flags;
741 int i;
743 spin_lock_irqsave(&qp_info->snoop_lock, flags);
744 for (i = 0; i < qp_info->snoop_table_size; i++) {
745 mad_snoop_priv = qp_info->snoop_table[i];
746 if (!mad_snoop_priv ||
747 !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
748 continue;
750 atomic_inc(&mad_snoop_priv->refcount);
751 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
752 mad_snoop_priv->agent.recv_handler(&mad_snoop_priv->agent, NULL,
753 mad_recv_wc);
754 deref_snoop_agent(mad_snoop_priv);
755 spin_lock_irqsave(&qp_info->snoop_lock, flags);
757 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
760 static void build_smp_wc(struct ib_qp *qp, struct ib_cqe *cqe, u16 slid,
761 u16 pkey_index, u8 port_num, struct ib_wc *wc)
763 memset(wc, 0, sizeof *wc);
764 wc->wr_cqe = cqe;
765 wc->status = IB_WC_SUCCESS;
766 wc->opcode = IB_WC_RECV;
767 wc->pkey_index = pkey_index;
768 wc->byte_len = sizeof(struct ib_mad) + sizeof(struct ib_grh);
769 wc->src_qp = IB_QP0;
770 wc->qp = qp;
771 wc->slid = slid;
772 wc->sl = 0;
773 wc->dlid_path_bits = 0;
774 wc->port_num = port_num;
777 static size_t mad_priv_size(const struct ib_mad_private *mp)
779 return sizeof(struct ib_mad_private) + mp->mad_size;
782 static struct ib_mad_private *alloc_mad_private(size_t mad_size, gfp_t flags)
784 size_t size = sizeof(struct ib_mad_private) + mad_size;
785 struct ib_mad_private *ret = kzalloc(size, flags);
787 if (ret)
788 ret->mad_size = mad_size;
790 return ret;
793 static size_t port_mad_size(const struct ib_mad_port_private *port_priv)
795 return rdma_max_mad_size(port_priv->device, port_priv->port_num);
798 static size_t mad_priv_dma_size(const struct ib_mad_private *mp)
800 return sizeof(struct ib_grh) + mp->mad_size;
804 * Return 0 if SMP is to be sent
805 * Return 1 if SMP was consumed locally (whether or not solicited)
806 * Return < 0 if error
808 static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
809 struct ib_mad_send_wr_private *mad_send_wr)
811 int ret = 0;
812 struct ib_smp *smp = mad_send_wr->send_buf.mad;
813 struct opa_smp *opa_smp = (struct opa_smp *)smp;
814 unsigned long flags;
815 struct ib_mad_local_private *local;
816 struct ib_mad_private *mad_priv;
817 struct ib_mad_port_private *port_priv;
818 struct ib_mad_agent_private *recv_mad_agent = NULL;
819 struct ib_device *device = mad_agent_priv->agent.device;
820 u8 port_num;
821 struct ib_wc mad_wc;
822 struct ib_ud_wr *send_wr = &mad_send_wr->send_wr;
823 size_t mad_size = port_mad_size(mad_agent_priv->qp_info->port_priv);
824 u16 out_mad_pkey_index = 0;
825 u16 drslid;
826 bool opa = rdma_cap_opa_mad(mad_agent_priv->qp_info->port_priv->device,
827 mad_agent_priv->qp_info->port_priv->port_num);
829 if (rdma_cap_ib_switch(device) &&
830 smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
831 port_num = send_wr->port_num;
832 else
833 port_num = mad_agent_priv->agent.port_num;
836 * Directed route handling starts if the initial LID routed part of
837 * a request or the ending LID routed part of a response is empty.
838 * If we are at the start of the LID routed part, don't update the
839 * hop_ptr or hop_cnt. See section 14.2.2, Vol 1 IB spec.
841 if (opa && smp->class_version == OPA_SM_CLASS_VERSION) {
842 u32 opa_drslid;
844 trace_ib_mad_handle_out_opa_smi(opa_smp);
846 if ((opa_get_smp_direction(opa_smp)
847 ? opa_smp->route.dr.dr_dlid : opa_smp->route.dr.dr_slid) ==
848 OPA_LID_PERMISSIVE &&
849 opa_smi_handle_dr_smp_send(opa_smp,
850 rdma_cap_ib_switch(device),
851 port_num) == IB_SMI_DISCARD) {
852 ret = -EINVAL;
853 dev_err(&device->dev, "OPA Invalid directed route\n");
854 goto out;
856 opa_drslid = be32_to_cpu(opa_smp->route.dr.dr_slid);
857 if (opa_drslid != be32_to_cpu(OPA_LID_PERMISSIVE) &&
858 opa_drslid & 0xffff0000) {
859 ret = -EINVAL;
860 dev_err(&device->dev, "OPA Invalid dr_slid 0x%x\n",
861 opa_drslid);
862 goto out;
864 drslid = (u16)(opa_drslid & 0x0000ffff);
866 /* Check to post send on QP or process locally */
867 if (opa_smi_check_local_smp(opa_smp, device) == IB_SMI_DISCARD &&
868 opa_smi_check_local_returning_smp(opa_smp, device) == IB_SMI_DISCARD)
869 goto out;
870 } else {
871 trace_ib_mad_handle_out_ib_smi(smp);
873 if ((ib_get_smp_direction(smp) ? smp->dr_dlid : smp->dr_slid) ==
874 IB_LID_PERMISSIVE &&
875 smi_handle_dr_smp_send(smp, rdma_cap_ib_switch(device), port_num) ==
876 IB_SMI_DISCARD) {
877 ret = -EINVAL;
878 dev_err(&device->dev, "Invalid directed route\n");
879 goto out;
881 drslid = be16_to_cpu(smp->dr_slid);
883 /* Check to post send on QP or process locally */
884 if (smi_check_local_smp(smp, device) == IB_SMI_DISCARD &&
885 smi_check_local_returning_smp(smp, device) == IB_SMI_DISCARD)
886 goto out;
889 local = kmalloc(sizeof *local, GFP_ATOMIC);
890 if (!local) {
891 ret = -ENOMEM;
892 goto out;
894 local->mad_priv = NULL;
895 local->recv_mad_agent = NULL;
896 mad_priv = alloc_mad_private(mad_size, GFP_ATOMIC);
897 if (!mad_priv) {
898 ret = -ENOMEM;
899 kfree(local);
900 goto out;
903 build_smp_wc(mad_agent_priv->agent.qp,
904 send_wr->wr.wr_cqe, drslid,
905 send_wr->pkey_index,
906 send_wr->port_num, &mad_wc);
908 if (opa && smp->base_version == OPA_MGMT_BASE_VERSION) {
909 mad_wc.byte_len = mad_send_wr->send_buf.hdr_len
910 + mad_send_wr->send_buf.data_len
911 + sizeof(struct ib_grh);
914 /* No GRH for DR SMP */
915 ret = device->ops.process_mad(device, 0, port_num, &mad_wc, NULL,
916 (const struct ib_mad *)smp,
917 (struct ib_mad *)mad_priv->mad, &mad_size,
918 &out_mad_pkey_index);
919 switch (ret)
921 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY:
922 if (ib_response_mad((const struct ib_mad_hdr *)mad_priv->mad) &&
923 mad_agent_priv->agent.recv_handler) {
924 local->mad_priv = mad_priv;
925 local->recv_mad_agent = mad_agent_priv;
927 * Reference MAD agent until receive
928 * side of local completion handled
930 atomic_inc(&mad_agent_priv->refcount);
931 } else
932 kfree(mad_priv);
933 break;
934 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED:
935 kfree(mad_priv);
936 break;
937 case IB_MAD_RESULT_SUCCESS:
938 /* Treat like an incoming receive MAD */
939 port_priv = ib_get_mad_port(mad_agent_priv->agent.device,
940 mad_agent_priv->agent.port_num);
941 if (port_priv) {
942 memcpy(mad_priv->mad, smp, mad_priv->mad_size);
943 recv_mad_agent = find_mad_agent(port_priv,
944 (const struct ib_mad_hdr *)mad_priv->mad);
946 if (!port_priv || !recv_mad_agent) {
948 * No receiving agent so drop packet and
949 * generate send completion.
951 kfree(mad_priv);
952 break;
954 local->mad_priv = mad_priv;
955 local->recv_mad_agent = recv_mad_agent;
956 break;
957 default:
958 kfree(mad_priv);
959 kfree(local);
960 ret = -EINVAL;
961 goto out;
964 local->mad_send_wr = mad_send_wr;
965 if (opa) {
966 local->mad_send_wr->send_wr.pkey_index = out_mad_pkey_index;
967 local->return_wc_byte_len = mad_size;
969 /* Reference MAD agent until send side of local completion handled */
970 atomic_inc(&mad_agent_priv->refcount);
971 /* Queue local completion to local list */
972 spin_lock_irqsave(&mad_agent_priv->lock, flags);
973 list_add_tail(&local->completion_list, &mad_agent_priv->local_list);
974 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
975 queue_work(mad_agent_priv->qp_info->port_priv->wq,
976 &mad_agent_priv->local_work);
977 ret = 1;
978 out:
979 return ret;
982 static int get_pad_size(int hdr_len, int data_len, size_t mad_size)
984 int seg_size, pad;
986 seg_size = mad_size - hdr_len;
987 if (data_len && seg_size) {
988 pad = seg_size - data_len % seg_size;
989 return pad == seg_size ? 0 : pad;
990 } else
991 return seg_size;
994 static void free_send_rmpp_list(struct ib_mad_send_wr_private *mad_send_wr)
996 struct ib_rmpp_segment *s, *t;
998 list_for_each_entry_safe(s, t, &mad_send_wr->rmpp_list, list) {
999 list_del(&s->list);
1000 kfree(s);
1004 static int alloc_send_rmpp_list(struct ib_mad_send_wr_private *send_wr,
1005 size_t mad_size, gfp_t gfp_mask)
1007 struct ib_mad_send_buf *send_buf = &send_wr->send_buf;
1008 struct ib_rmpp_mad *rmpp_mad = send_buf->mad;
1009 struct ib_rmpp_segment *seg = NULL;
1010 int left, seg_size, pad;
1012 send_buf->seg_size = mad_size - send_buf->hdr_len;
1013 send_buf->seg_rmpp_size = mad_size - IB_MGMT_RMPP_HDR;
1014 seg_size = send_buf->seg_size;
1015 pad = send_wr->pad;
1017 /* Allocate data segments. */
1018 for (left = send_buf->data_len + pad; left > 0; left -= seg_size) {
1019 seg = kmalloc(sizeof (*seg) + seg_size, gfp_mask);
1020 if (!seg) {
1021 free_send_rmpp_list(send_wr);
1022 return -ENOMEM;
1024 seg->num = ++send_buf->seg_count;
1025 list_add_tail(&seg->list, &send_wr->rmpp_list);
1028 /* Zero any padding */
1029 if (pad)
1030 memset(seg->data + seg_size - pad, 0, pad);
1032 rmpp_mad->rmpp_hdr.rmpp_version = send_wr->mad_agent_priv->
1033 agent.rmpp_version;
1034 rmpp_mad->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_DATA;
1035 ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
1037 send_wr->cur_seg = container_of(send_wr->rmpp_list.next,
1038 struct ib_rmpp_segment, list);
1039 send_wr->last_ack_seg = send_wr->cur_seg;
1040 return 0;
1043 int ib_mad_kernel_rmpp_agent(const struct ib_mad_agent *agent)
1045 return agent->rmpp_version && !(agent->flags & IB_MAD_USER_RMPP);
1047 EXPORT_SYMBOL(ib_mad_kernel_rmpp_agent);
1049 struct ib_mad_send_buf * ib_create_send_mad(struct ib_mad_agent *mad_agent,
1050 u32 remote_qpn, u16 pkey_index,
1051 int rmpp_active,
1052 int hdr_len, int data_len,
1053 gfp_t gfp_mask,
1054 u8 base_version)
1056 struct ib_mad_agent_private *mad_agent_priv;
1057 struct ib_mad_send_wr_private *mad_send_wr;
1058 int pad, message_size, ret, size;
1059 void *buf;
1060 size_t mad_size;
1061 bool opa;
1063 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
1064 agent);
1066 opa = rdma_cap_opa_mad(mad_agent->device, mad_agent->port_num);
1068 if (opa && base_version == OPA_MGMT_BASE_VERSION)
1069 mad_size = sizeof(struct opa_mad);
1070 else
1071 mad_size = sizeof(struct ib_mad);
1073 pad = get_pad_size(hdr_len, data_len, mad_size);
1074 message_size = hdr_len + data_len + pad;
1076 if (ib_mad_kernel_rmpp_agent(mad_agent)) {
1077 if (!rmpp_active && message_size > mad_size)
1078 return ERR_PTR(-EINVAL);
1079 } else
1080 if (rmpp_active || message_size > mad_size)
1081 return ERR_PTR(-EINVAL);
1083 size = rmpp_active ? hdr_len : mad_size;
1084 buf = kzalloc(sizeof *mad_send_wr + size, gfp_mask);
1085 if (!buf)
1086 return ERR_PTR(-ENOMEM);
1088 mad_send_wr = buf + size;
1089 INIT_LIST_HEAD(&mad_send_wr->rmpp_list);
1090 mad_send_wr->send_buf.mad = buf;
1091 mad_send_wr->send_buf.hdr_len = hdr_len;
1092 mad_send_wr->send_buf.data_len = data_len;
1093 mad_send_wr->pad = pad;
1095 mad_send_wr->mad_agent_priv = mad_agent_priv;
1096 mad_send_wr->sg_list[0].length = hdr_len;
1097 mad_send_wr->sg_list[0].lkey = mad_agent->qp->pd->local_dma_lkey;
1099 /* OPA MADs don't have to be the full 2048 bytes */
1100 if (opa && base_version == OPA_MGMT_BASE_VERSION &&
1101 data_len < mad_size - hdr_len)
1102 mad_send_wr->sg_list[1].length = data_len;
1103 else
1104 mad_send_wr->sg_list[1].length = mad_size - hdr_len;
1106 mad_send_wr->sg_list[1].lkey = mad_agent->qp->pd->local_dma_lkey;
1108 mad_send_wr->mad_list.cqe.done = ib_mad_send_done;
1110 mad_send_wr->send_wr.wr.wr_cqe = &mad_send_wr->mad_list.cqe;
1111 mad_send_wr->send_wr.wr.sg_list = mad_send_wr->sg_list;
1112 mad_send_wr->send_wr.wr.num_sge = 2;
1113 mad_send_wr->send_wr.wr.opcode = IB_WR_SEND;
1114 mad_send_wr->send_wr.wr.send_flags = IB_SEND_SIGNALED;
1115 mad_send_wr->send_wr.remote_qpn = remote_qpn;
1116 mad_send_wr->send_wr.remote_qkey = IB_QP_SET_QKEY;
1117 mad_send_wr->send_wr.pkey_index = pkey_index;
1119 if (rmpp_active) {
1120 ret = alloc_send_rmpp_list(mad_send_wr, mad_size, gfp_mask);
1121 if (ret) {
1122 kfree(buf);
1123 return ERR_PTR(ret);
1127 mad_send_wr->send_buf.mad_agent = mad_agent;
1128 atomic_inc(&mad_agent_priv->refcount);
1129 return &mad_send_wr->send_buf;
1131 EXPORT_SYMBOL(ib_create_send_mad);
1133 int ib_get_mad_data_offset(u8 mgmt_class)
1135 if (mgmt_class == IB_MGMT_CLASS_SUBN_ADM)
1136 return IB_MGMT_SA_HDR;
1137 else if ((mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
1138 (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
1139 (mgmt_class == IB_MGMT_CLASS_BIS))
1140 return IB_MGMT_DEVICE_HDR;
1141 else if ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
1142 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END))
1143 return IB_MGMT_VENDOR_HDR;
1144 else
1145 return IB_MGMT_MAD_HDR;
1147 EXPORT_SYMBOL(ib_get_mad_data_offset);
1149 int ib_is_mad_class_rmpp(u8 mgmt_class)
1151 if ((mgmt_class == IB_MGMT_CLASS_SUBN_ADM) ||
1152 (mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
1153 (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
1154 (mgmt_class == IB_MGMT_CLASS_BIS) ||
1155 ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
1156 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END)))
1157 return 1;
1158 return 0;
1160 EXPORT_SYMBOL(ib_is_mad_class_rmpp);
1162 void *ib_get_rmpp_segment(struct ib_mad_send_buf *send_buf, int seg_num)
1164 struct ib_mad_send_wr_private *mad_send_wr;
1165 struct list_head *list;
1167 mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
1168 send_buf);
1169 list = &mad_send_wr->cur_seg->list;
1171 if (mad_send_wr->cur_seg->num < seg_num) {
1172 list_for_each_entry(mad_send_wr->cur_seg, list, list)
1173 if (mad_send_wr->cur_seg->num == seg_num)
1174 break;
1175 } else if (mad_send_wr->cur_seg->num > seg_num) {
1176 list_for_each_entry_reverse(mad_send_wr->cur_seg, list, list)
1177 if (mad_send_wr->cur_seg->num == seg_num)
1178 break;
1180 return mad_send_wr->cur_seg->data;
1182 EXPORT_SYMBOL(ib_get_rmpp_segment);
1184 static inline void *ib_get_payload(struct ib_mad_send_wr_private *mad_send_wr)
1186 if (mad_send_wr->send_buf.seg_count)
1187 return ib_get_rmpp_segment(&mad_send_wr->send_buf,
1188 mad_send_wr->seg_num);
1189 else
1190 return mad_send_wr->send_buf.mad +
1191 mad_send_wr->send_buf.hdr_len;
1194 void ib_free_send_mad(struct ib_mad_send_buf *send_buf)
1196 struct ib_mad_agent_private *mad_agent_priv;
1197 struct ib_mad_send_wr_private *mad_send_wr;
1199 mad_agent_priv = container_of(send_buf->mad_agent,
1200 struct ib_mad_agent_private, agent);
1201 mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
1202 send_buf);
1204 free_send_rmpp_list(mad_send_wr);
1205 kfree(send_buf->mad);
1206 deref_mad_agent(mad_agent_priv);
1208 EXPORT_SYMBOL(ib_free_send_mad);
1210 int ib_send_mad(struct ib_mad_send_wr_private *mad_send_wr)
1212 struct ib_mad_qp_info *qp_info;
1213 struct list_head *list;
1214 struct ib_mad_agent *mad_agent;
1215 struct ib_sge *sge;
1216 unsigned long flags;
1217 int ret;
1219 /* Set WR ID to find mad_send_wr upon completion */
1220 qp_info = mad_send_wr->mad_agent_priv->qp_info;
1221 mad_send_wr->mad_list.mad_queue = &qp_info->send_queue;
1222 mad_send_wr->mad_list.cqe.done = ib_mad_send_done;
1223 mad_send_wr->send_wr.wr.wr_cqe = &mad_send_wr->mad_list.cqe;
1225 mad_agent = mad_send_wr->send_buf.mad_agent;
1226 sge = mad_send_wr->sg_list;
1227 sge[0].addr = ib_dma_map_single(mad_agent->device,
1228 mad_send_wr->send_buf.mad,
1229 sge[0].length,
1230 DMA_TO_DEVICE);
1231 if (unlikely(ib_dma_mapping_error(mad_agent->device, sge[0].addr)))
1232 return -ENOMEM;
1234 mad_send_wr->header_mapping = sge[0].addr;
1236 sge[1].addr = ib_dma_map_single(mad_agent->device,
1237 ib_get_payload(mad_send_wr),
1238 sge[1].length,
1239 DMA_TO_DEVICE);
1240 if (unlikely(ib_dma_mapping_error(mad_agent->device, sge[1].addr))) {
1241 ib_dma_unmap_single(mad_agent->device,
1242 mad_send_wr->header_mapping,
1243 sge[0].length, DMA_TO_DEVICE);
1244 return -ENOMEM;
1246 mad_send_wr->payload_mapping = sge[1].addr;
1248 spin_lock_irqsave(&qp_info->send_queue.lock, flags);
1249 if (qp_info->send_queue.count < qp_info->send_queue.max_active) {
1250 trace_ib_mad_ib_send_mad(mad_send_wr, qp_info);
1251 ret = ib_post_send(mad_agent->qp, &mad_send_wr->send_wr.wr,
1252 NULL);
1253 list = &qp_info->send_queue.list;
1254 } else {
1255 ret = 0;
1256 list = &qp_info->overflow_list;
1259 if (!ret) {
1260 qp_info->send_queue.count++;
1261 list_add_tail(&mad_send_wr->mad_list.list, list);
1263 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
1264 if (ret) {
1265 ib_dma_unmap_single(mad_agent->device,
1266 mad_send_wr->header_mapping,
1267 sge[0].length, DMA_TO_DEVICE);
1268 ib_dma_unmap_single(mad_agent->device,
1269 mad_send_wr->payload_mapping,
1270 sge[1].length, DMA_TO_DEVICE);
1272 return ret;
1276 * ib_post_send_mad - Posts MAD(s) to the send queue of the QP associated
1277 * with the registered client
1279 int ib_post_send_mad(struct ib_mad_send_buf *send_buf,
1280 struct ib_mad_send_buf **bad_send_buf)
1282 struct ib_mad_agent_private *mad_agent_priv;
1283 struct ib_mad_send_buf *next_send_buf;
1284 struct ib_mad_send_wr_private *mad_send_wr;
1285 unsigned long flags;
1286 int ret = -EINVAL;
1288 /* Walk list of send WRs and post each on send list */
1289 for (; send_buf; send_buf = next_send_buf) {
1290 mad_send_wr = container_of(send_buf,
1291 struct ib_mad_send_wr_private,
1292 send_buf);
1293 mad_agent_priv = mad_send_wr->mad_agent_priv;
1295 ret = ib_mad_enforce_security(mad_agent_priv,
1296 mad_send_wr->send_wr.pkey_index);
1297 if (ret)
1298 goto error;
1300 if (!send_buf->mad_agent->send_handler ||
1301 (send_buf->timeout_ms &&
1302 !send_buf->mad_agent->recv_handler)) {
1303 ret = -EINVAL;
1304 goto error;
1307 if (!ib_is_mad_class_rmpp(((struct ib_mad_hdr *) send_buf->mad)->mgmt_class)) {
1308 if (mad_agent_priv->agent.rmpp_version) {
1309 ret = -EINVAL;
1310 goto error;
1315 * Save pointer to next work request to post in case the
1316 * current one completes, and the user modifies the work
1317 * request associated with the completion
1319 next_send_buf = send_buf->next;
1320 mad_send_wr->send_wr.ah = send_buf->ah;
1322 if (((struct ib_mad_hdr *) send_buf->mad)->mgmt_class ==
1323 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
1324 ret = handle_outgoing_dr_smp(mad_agent_priv,
1325 mad_send_wr);
1326 if (ret < 0) /* error */
1327 goto error;
1328 else if (ret == 1) /* locally consumed */
1329 continue;
1332 mad_send_wr->tid = ((struct ib_mad_hdr *) send_buf->mad)->tid;
1333 /* Timeout will be updated after send completes */
1334 mad_send_wr->timeout = msecs_to_jiffies(send_buf->timeout_ms);
1335 mad_send_wr->max_retries = send_buf->retries;
1336 mad_send_wr->retries_left = send_buf->retries;
1337 send_buf->retries = 0;
1338 /* Reference for work request to QP + response */
1339 mad_send_wr->refcount = 1 + (mad_send_wr->timeout > 0);
1340 mad_send_wr->status = IB_WC_SUCCESS;
1342 /* Reference MAD agent until send completes */
1343 atomic_inc(&mad_agent_priv->refcount);
1344 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1345 list_add_tail(&mad_send_wr->agent_list,
1346 &mad_agent_priv->send_list);
1347 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1349 if (ib_mad_kernel_rmpp_agent(&mad_agent_priv->agent)) {
1350 ret = ib_send_rmpp_mad(mad_send_wr);
1351 if (ret >= 0 && ret != IB_RMPP_RESULT_CONSUMED)
1352 ret = ib_send_mad(mad_send_wr);
1353 } else
1354 ret = ib_send_mad(mad_send_wr);
1355 if (ret < 0) {
1356 /* Fail send request */
1357 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1358 list_del(&mad_send_wr->agent_list);
1359 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1360 atomic_dec(&mad_agent_priv->refcount);
1361 goto error;
1364 return 0;
1365 error:
1366 if (bad_send_buf)
1367 *bad_send_buf = send_buf;
1368 return ret;
1370 EXPORT_SYMBOL(ib_post_send_mad);
1373 * ib_free_recv_mad - Returns data buffers used to receive
1374 * a MAD to the access layer
1376 void ib_free_recv_mad(struct ib_mad_recv_wc *mad_recv_wc)
1378 struct ib_mad_recv_buf *mad_recv_buf, *temp_recv_buf;
1379 struct ib_mad_private_header *mad_priv_hdr;
1380 struct ib_mad_private *priv;
1381 struct list_head free_list;
1383 INIT_LIST_HEAD(&free_list);
1384 list_splice_init(&mad_recv_wc->rmpp_list, &free_list);
1386 list_for_each_entry_safe(mad_recv_buf, temp_recv_buf,
1387 &free_list, list) {
1388 mad_recv_wc = container_of(mad_recv_buf, struct ib_mad_recv_wc,
1389 recv_buf);
1390 mad_priv_hdr = container_of(mad_recv_wc,
1391 struct ib_mad_private_header,
1392 recv_wc);
1393 priv = container_of(mad_priv_hdr, struct ib_mad_private,
1394 header);
1395 kfree(priv);
1398 EXPORT_SYMBOL(ib_free_recv_mad);
1400 static int method_in_use(struct ib_mad_mgmt_method_table **method,
1401 struct ib_mad_reg_req *mad_reg_req)
1403 int i;
1405 for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS) {
1406 if ((*method)->agent[i]) {
1407 pr_err("Method %d already in use\n", i);
1408 return -EINVAL;
1411 return 0;
1414 static int allocate_method_table(struct ib_mad_mgmt_method_table **method)
1416 /* Allocate management method table */
1417 *method = kzalloc(sizeof **method, GFP_ATOMIC);
1418 return (*method) ? 0 : (-ENOMEM);
1422 * Check to see if there are any methods still in use
1424 static int check_method_table(struct ib_mad_mgmt_method_table *method)
1426 int i;
1428 for (i = 0; i < IB_MGMT_MAX_METHODS; i++)
1429 if (method->agent[i])
1430 return 1;
1431 return 0;
1435 * Check to see if there are any method tables for this class still in use
1437 static int check_class_table(struct ib_mad_mgmt_class_table *class)
1439 int i;
1441 for (i = 0; i < MAX_MGMT_CLASS; i++)
1442 if (class->method_table[i])
1443 return 1;
1444 return 0;
1447 static int check_vendor_class(struct ib_mad_mgmt_vendor_class *vendor_class)
1449 int i;
1451 for (i = 0; i < MAX_MGMT_OUI; i++)
1452 if (vendor_class->method_table[i])
1453 return 1;
1454 return 0;
1457 static int find_vendor_oui(struct ib_mad_mgmt_vendor_class *vendor_class,
1458 const char *oui)
1460 int i;
1462 for (i = 0; i < MAX_MGMT_OUI; i++)
1463 /* Is there matching OUI for this vendor class ? */
1464 if (!memcmp(vendor_class->oui[i], oui, 3))
1465 return i;
1467 return -1;
1470 static int check_vendor_table(struct ib_mad_mgmt_vendor_class_table *vendor)
1472 int i;
1474 for (i = 0; i < MAX_MGMT_VENDOR_RANGE2; i++)
1475 if (vendor->vendor_class[i])
1476 return 1;
1478 return 0;
1481 static void remove_methods_mad_agent(struct ib_mad_mgmt_method_table *method,
1482 struct ib_mad_agent_private *agent)
1484 int i;
1486 /* Remove any methods for this mad agent */
1487 for (i = 0; i < IB_MGMT_MAX_METHODS; i++) {
1488 if (method->agent[i] == agent) {
1489 method->agent[i] = NULL;
1494 static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1495 struct ib_mad_agent_private *agent_priv,
1496 u8 mgmt_class)
1498 struct ib_mad_port_private *port_priv;
1499 struct ib_mad_mgmt_class_table **class;
1500 struct ib_mad_mgmt_method_table **method;
1501 int i, ret;
1503 port_priv = agent_priv->qp_info->port_priv;
1504 class = &port_priv->version[mad_reg_req->mgmt_class_version].class;
1505 if (!*class) {
1506 /* Allocate management class table for "new" class version */
1507 *class = kzalloc(sizeof **class, GFP_ATOMIC);
1508 if (!*class) {
1509 ret = -ENOMEM;
1510 goto error1;
1513 /* Allocate method table for this management class */
1514 method = &(*class)->method_table[mgmt_class];
1515 if ((ret = allocate_method_table(method)))
1516 goto error2;
1517 } else {
1518 method = &(*class)->method_table[mgmt_class];
1519 if (!*method) {
1520 /* Allocate method table for this management class */
1521 if ((ret = allocate_method_table(method)))
1522 goto error1;
1526 /* Now, make sure methods are not already in use */
1527 if (method_in_use(method, mad_reg_req))
1528 goto error3;
1530 /* Finally, add in methods being registered */
1531 for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS)
1532 (*method)->agent[i] = agent_priv;
1534 return 0;
1536 error3:
1537 /* Remove any methods for this mad agent */
1538 remove_methods_mad_agent(*method, agent_priv);
1539 /* Now, check to see if there are any methods in use */
1540 if (!check_method_table(*method)) {
1541 /* If not, release management method table */
1542 kfree(*method);
1543 *method = NULL;
1545 ret = -EINVAL;
1546 goto error1;
1547 error2:
1548 kfree(*class);
1549 *class = NULL;
1550 error1:
1551 return ret;
1554 static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1555 struct ib_mad_agent_private *agent_priv)
1557 struct ib_mad_port_private *port_priv;
1558 struct ib_mad_mgmt_vendor_class_table **vendor_table;
1559 struct ib_mad_mgmt_vendor_class_table *vendor = NULL;
1560 struct ib_mad_mgmt_vendor_class *vendor_class = NULL;
1561 struct ib_mad_mgmt_method_table **method;
1562 int i, ret = -ENOMEM;
1563 u8 vclass;
1565 /* "New" vendor (with OUI) class */
1566 vclass = vendor_class_index(mad_reg_req->mgmt_class);
1567 port_priv = agent_priv->qp_info->port_priv;
1568 vendor_table = &port_priv->version[
1569 mad_reg_req->mgmt_class_version].vendor;
1570 if (!*vendor_table) {
1571 /* Allocate mgmt vendor class table for "new" class version */
1572 vendor = kzalloc(sizeof *vendor, GFP_ATOMIC);
1573 if (!vendor)
1574 goto error1;
1576 *vendor_table = vendor;
1578 if (!(*vendor_table)->vendor_class[vclass]) {
1579 /* Allocate table for this management vendor class */
1580 vendor_class = kzalloc(sizeof *vendor_class, GFP_ATOMIC);
1581 if (!vendor_class)
1582 goto error2;
1584 (*vendor_table)->vendor_class[vclass] = vendor_class;
1586 for (i = 0; i < MAX_MGMT_OUI; i++) {
1587 /* Is there matching OUI for this vendor class ? */
1588 if (!memcmp((*vendor_table)->vendor_class[vclass]->oui[i],
1589 mad_reg_req->oui, 3)) {
1590 method = &(*vendor_table)->vendor_class[
1591 vclass]->method_table[i];
1592 if (!*method)
1593 goto error3;
1594 goto check_in_use;
1597 for (i = 0; i < MAX_MGMT_OUI; i++) {
1598 /* OUI slot available ? */
1599 if (!is_vendor_oui((*vendor_table)->vendor_class[
1600 vclass]->oui[i])) {
1601 method = &(*vendor_table)->vendor_class[
1602 vclass]->method_table[i];
1603 /* Allocate method table for this OUI */
1604 if (!*method) {
1605 ret = allocate_method_table(method);
1606 if (ret)
1607 goto error3;
1609 memcpy((*vendor_table)->vendor_class[vclass]->oui[i],
1610 mad_reg_req->oui, 3);
1611 goto check_in_use;
1614 dev_err(&agent_priv->agent.device->dev, "All OUI slots in use\n");
1615 goto error3;
1617 check_in_use:
1618 /* Now, make sure methods are not already in use */
1619 if (method_in_use(method, mad_reg_req))
1620 goto error4;
1622 /* Finally, add in methods being registered */
1623 for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS)
1624 (*method)->agent[i] = agent_priv;
1626 return 0;
1628 error4:
1629 /* Remove any methods for this mad agent */
1630 remove_methods_mad_agent(*method, agent_priv);
1631 /* Now, check to see if there are any methods in use */
1632 if (!check_method_table(*method)) {
1633 /* If not, release management method table */
1634 kfree(*method);
1635 *method = NULL;
1637 ret = -EINVAL;
1638 error3:
1639 if (vendor_class) {
1640 (*vendor_table)->vendor_class[vclass] = NULL;
1641 kfree(vendor_class);
1643 error2:
1644 if (vendor) {
1645 *vendor_table = NULL;
1646 kfree(vendor);
1648 error1:
1649 return ret;
1652 static void remove_mad_reg_req(struct ib_mad_agent_private *agent_priv)
1654 struct ib_mad_port_private *port_priv;
1655 struct ib_mad_mgmt_class_table *class;
1656 struct ib_mad_mgmt_method_table *method;
1657 struct ib_mad_mgmt_vendor_class_table *vendor;
1658 struct ib_mad_mgmt_vendor_class *vendor_class;
1659 int index;
1660 u8 mgmt_class;
1663 * Was MAD registration request supplied
1664 * with original registration ?
1666 if (!agent_priv->reg_req) {
1667 goto out;
1670 port_priv = agent_priv->qp_info->port_priv;
1671 mgmt_class = convert_mgmt_class(agent_priv->reg_req->mgmt_class);
1672 class = port_priv->version[
1673 agent_priv->reg_req->mgmt_class_version].class;
1674 if (!class)
1675 goto vendor_check;
1677 method = class->method_table[mgmt_class];
1678 if (method) {
1679 /* Remove any methods for this mad agent */
1680 remove_methods_mad_agent(method, agent_priv);
1681 /* Now, check to see if there are any methods still in use */
1682 if (!check_method_table(method)) {
1683 /* If not, release management method table */
1684 kfree(method);
1685 class->method_table[mgmt_class] = NULL;
1686 /* Any management classes left ? */
1687 if (!check_class_table(class)) {
1688 /* If not, release management class table */
1689 kfree(class);
1690 port_priv->version[
1691 agent_priv->reg_req->
1692 mgmt_class_version].class = NULL;
1697 vendor_check:
1698 if (!is_vendor_class(mgmt_class))
1699 goto out;
1701 /* normalize mgmt_class to vendor range 2 */
1702 mgmt_class = vendor_class_index(agent_priv->reg_req->mgmt_class);
1703 vendor = port_priv->version[
1704 agent_priv->reg_req->mgmt_class_version].vendor;
1706 if (!vendor)
1707 goto out;
1709 vendor_class = vendor->vendor_class[mgmt_class];
1710 if (vendor_class) {
1711 index = find_vendor_oui(vendor_class, agent_priv->reg_req->oui);
1712 if (index < 0)
1713 goto out;
1714 method = vendor_class->method_table[index];
1715 if (method) {
1716 /* Remove any methods for this mad agent */
1717 remove_methods_mad_agent(method, agent_priv);
1719 * Now, check to see if there are
1720 * any methods still in use
1722 if (!check_method_table(method)) {
1723 /* If not, release management method table */
1724 kfree(method);
1725 vendor_class->method_table[index] = NULL;
1726 memset(vendor_class->oui[index], 0, 3);
1727 /* Any OUIs left ? */
1728 if (!check_vendor_class(vendor_class)) {
1729 /* If not, release vendor class table */
1730 kfree(vendor_class);
1731 vendor->vendor_class[mgmt_class] = NULL;
1732 /* Any other vendor classes left ? */
1733 if (!check_vendor_table(vendor)) {
1734 kfree(vendor);
1735 port_priv->version[
1736 agent_priv->reg_req->
1737 mgmt_class_version].
1738 vendor = NULL;
1745 out:
1746 return;
1749 static struct ib_mad_agent_private *
1750 find_mad_agent(struct ib_mad_port_private *port_priv,
1751 const struct ib_mad_hdr *mad_hdr)
1753 struct ib_mad_agent_private *mad_agent = NULL;
1754 unsigned long flags;
1756 if (ib_response_mad(mad_hdr)) {
1757 u32 hi_tid;
1760 * Routing is based on high 32 bits of transaction ID
1761 * of MAD.
1763 hi_tid = be64_to_cpu(mad_hdr->tid) >> 32;
1764 rcu_read_lock();
1765 mad_agent = xa_load(&ib_mad_clients, hi_tid);
1766 if (mad_agent && !atomic_inc_not_zero(&mad_agent->refcount))
1767 mad_agent = NULL;
1768 rcu_read_unlock();
1769 } else {
1770 struct ib_mad_mgmt_class_table *class;
1771 struct ib_mad_mgmt_method_table *method;
1772 struct ib_mad_mgmt_vendor_class_table *vendor;
1773 struct ib_mad_mgmt_vendor_class *vendor_class;
1774 const struct ib_vendor_mad *vendor_mad;
1775 int index;
1777 spin_lock_irqsave(&port_priv->reg_lock, flags);
1779 * Routing is based on version, class, and method
1780 * For "newer" vendor MADs, also based on OUI
1782 if (mad_hdr->class_version >= MAX_MGMT_VERSION)
1783 goto out;
1784 if (!is_vendor_class(mad_hdr->mgmt_class)) {
1785 class = port_priv->version[
1786 mad_hdr->class_version].class;
1787 if (!class)
1788 goto out;
1789 if (convert_mgmt_class(mad_hdr->mgmt_class) >=
1790 ARRAY_SIZE(class->method_table))
1791 goto out;
1792 method = class->method_table[convert_mgmt_class(
1793 mad_hdr->mgmt_class)];
1794 if (method)
1795 mad_agent = method->agent[mad_hdr->method &
1796 ~IB_MGMT_METHOD_RESP];
1797 } else {
1798 vendor = port_priv->version[
1799 mad_hdr->class_version].vendor;
1800 if (!vendor)
1801 goto out;
1802 vendor_class = vendor->vendor_class[vendor_class_index(
1803 mad_hdr->mgmt_class)];
1804 if (!vendor_class)
1805 goto out;
1806 /* Find matching OUI */
1807 vendor_mad = (const struct ib_vendor_mad *)mad_hdr;
1808 index = find_vendor_oui(vendor_class, vendor_mad->oui);
1809 if (index == -1)
1810 goto out;
1811 method = vendor_class->method_table[index];
1812 if (method) {
1813 mad_agent = method->agent[mad_hdr->method &
1814 ~IB_MGMT_METHOD_RESP];
1817 if (mad_agent)
1818 atomic_inc(&mad_agent->refcount);
1819 out:
1820 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
1823 if (mad_agent && !mad_agent->agent.recv_handler) {
1824 dev_notice(&port_priv->device->dev,
1825 "No receive handler for client %p on port %d\n",
1826 &mad_agent->agent, port_priv->port_num);
1827 deref_mad_agent(mad_agent);
1828 mad_agent = NULL;
1831 return mad_agent;
1834 static int validate_mad(const struct ib_mad_hdr *mad_hdr,
1835 const struct ib_mad_qp_info *qp_info,
1836 bool opa)
1838 int valid = 0;
1839 u32 qp_num = qp_info->qp->qp_num;
1841 /* Make sure MAD base version is understood */
1842 if (mad_hdr->base_version != IB_MGMT_BASE_VERSION &&
1843 (!opa || mad_hdr->base_version != OPA_MGMT_BASE_VERSION)) {
1844 pr_err("MAD received with unsupported base version %d %s\n",
1845 mad_hdr->base_version, opa ? "(opa)" : "");
1846 goto out;
1849 /* Filter SMI packets sent to other than QP0 */
1850 if ((mad_hdr->mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
1851 (mad_hdr->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) {
1852 if (qp_num == 0)
1853 valid = 1;
1854 } else {
1855 /* CM attributes other than ClassPortInfo only use Send method */
1856 if ((mad_hdr->mgmt_class == IB_MGMT_CLASS_CM) &&
1857 (mad_hdr->attr_id != IB_MGMT_CLASSPORTINFO_ATTR_ID) &&
1858 (mad_hdr->method != IB_MGMT_METHOD_SEND))
1859 goto out;
1860 /* Filter GSI packets sent to QP0 */
1861 if (qp_num != 0)
1862 valid = 1;
1865 out:
1866 return valid;
1869 static int is_rmpp_data_mad(const struct ib_mad_agent_private *mad_agent_priv,
1870 const struct ib_mad_hdr *mad_hdr)
1872 struct ib_rmpp_mad *rmpp_mad;
1874 rmpp_mad = (struct ib_rmpp_mad *)mad_hdr;
1875 return !mad_agent_priv->agent.rmpp_version ||
1876 !ib_mad_kernel_rmpp_agent(&mad_agent_priv->agent) ||
1877 !(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
1878 IB_MGMT_RMPP_FLAG_ACTIVE) ||
1879 (rmpp_mad->rmpp_hdr.rmpp_type == IB_MGMT_RMPP_TYPE_DATA);
1882 static inline int rcv_has_same_class(const struct ib_mad_send_wr_private *wr,
1883 const struct ib_mad_recv_wc *rwc)
1885 return ((struct ib_mad_hdr *)(wr->send_buf.mad))->mgmt_class ==
1886 rwc->recv_buf.mad->mad_hdr.mgmt_class;
1889 static inline int rcv_has_same_gid(const struct ib_mad_agent_private *mad_agent_priv,
1890 const struct ib_mad_send_wr_private *wr,
1891 const struct ib_mad_recv_wc *rwc )
1893 struct rdma_ah_attr attr;
1894 u8 send_resp, rcv_resp;
1895 union ib_gid sgid;
1896 struct ib_device *device = mad_agent_priv->agent.device;
1897 u8 port_num = mad_agent_priv->agent.port_num;
1898 u8 lmc;
1899 bool has_grh;
1901 send_resp = ib_response_mad((struct ib_mad_hdr *)wr->send_buf.mad);
1902 rcv_resp = ib_response_mad(&rwc->recv_buf.mad->mad_hdr);
1904 if (send_resp == rcv_resp)
1905 /* both requests, or both responses. GIDs different */
1906 return 0;
1908 if (rdma_query_ah(wr->send_buf.ah, &attr))
1909 /* Assume not equal, to avoid false positives. */
1910 return 0;
1912 has_grh = !!(rdma_ah_get_ah_flags(&attr) & IB_AH_GRH);
1913 if (has_grh != !!(rwc->wc->wc_flags & IB_WC_GRH))
1914 /* one has GID, other does not. Assume different */
1915 return 0;
1917 if (!send_resp && rcv_resp) {
1918 /* is request/response. */
1919 if (!has_grh) {
1920 if (ib_get_cached_lmc(device, port_num, &lmc))
1921 return 0;
1922 return (!lmc || !((rdma_ah_get_path_bits(&attr) ^
1923 rwc->wc->dlid_path_bits) &
1924 ((1 << lmc) - 1)));
1925 } else {
1926 const struct ib_global_route *grh =
1927 rdma_ah_read_grh(&attr);
1929 if (rdma_query_gid(device, port_num,
1930 grh->sgid_index, &sgid))
1931 return 0;
1932 return !memcmp(sgid.raw, rwc->recv_buf.grh->dgid.raw,
1933 16);
1937 if (!has_grh)
1938 return rdma_ah_get_dlid(&attr) == rwc->wc->slid;
1939 else
1940 return !memcmp(rdma_ah_read_grh(&attr)->dgid.raw,
1941 rwc->recv_buf.grh->sgid.raw,
1942 16);
1945 static inline int is_direct(u8 class)
1947 return (class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE);
1950 struct ib_mad_send_wr_private*
1951 ib_find_send_mad(const struct ib_mad_agent_private *mad_agent_priv,
1952 const struct ib_mad_recv_wc *wc)
1954 struct ib_mad_send_wr_private *wr;
1955 const struct ib_mad_hdr *mad_hdr;
1957 mad_hdr = &wc->recv_buf.mad->mad_hdr;
1959 list_for_each_entry(wr, &mad_agent_priv->wait_list, agent_list) {
1960 if ((wr->tid == mad_hdr->tid) &&
1961 rcv_has_same_class(wr, wc) &&
1963 * Don't check GID for direct routed MADs.
1964 * These might have permissive LIDs.
1966 (is_direct(mad_hdr->mgmt_class) ||
1967 rcv_has_same_gid(mad_agent_priv, wr, wc)))
1968 return (wr->status == IB_WC_SUCCESS) ? wr : NULL;
1972 * It's possible to receive the response before we've
1973 * been notified that the send has completed
1975 list_for_each_entry(wr, &mad_agent_priv->send_list, agent_list) {
1976 if (is_rmpp_data_mad(mad_agent_priv, wr->send_buf.mad) &&
1977 wr->tid == mad_hdr->tid &&
1978 wr->timeout &&
1979 rcv_has_same_class(wr, wc) &&
1981 * Don't check GID for direct routed MADs.
1982 * These might have permissive LIDs.
1984 (is_direct(mad_hdr->mgmt_class) ||
1985 rcv_has_same_gid(mad_agent_priv, wr, wc)))
1986 /* Verify request has not been canceled */
1987 return (wr->status == IB_WC_SUCCESS) ? wr : NULL;
1989 return NULL;
1992 void ib_mark_mad_done(struct ib_mad_send_wr_private *mad_send_wr)
1994 mad_send_wr->timeout = 0;
1995 if (mad_send_wr->refcount == 1)
1996 list_move_tail(&mad_send_wr->agent_list,
1997 &mad_send_wr->mad_agent_priv->done_list);
2000 static void ib_mad_complete_recv(struct ib_mad_agent_private *mad_agent_priv,
2001 struct ib_mad_recv_wc *mad_recv_wc)
2003 struct ib_mad_send_wr_private *mad_send_wr;
2004 struct ib_mad_send_wc mad_send_wc;
2005 unsigned long flags;
2006 int ret;
2008 INIT_LIST_HEAD(&mad_recv_wc->rmpp_list);
2009 ret = ib_mad_enforce_security(mad_agent_priv,
2010 mad_recv_wc->wc->pkey_index);
2011 if (ret) {
2012 ib_free_recv_mad(mad_recv_wc);
2013 deref_mad_agent(mad_agent_priv);
2014 return;
2017 list_add(&mad_recv_wc->recv_buf.list, &mad_recv_wc->rmpp_list);
2018 if (ib_mad_kernel_rmpp_agent(&mad_agent_priv->agent)) {
2019 mad_recv_wc = ib_process_rmpp_recv_wc(mad_agent_priv,
2020 mad_recv_wc);
2021 if (!mad_recv_wc) {
2022 deref_mad_agent(mad_agent_priv);
2023 return;
2027 /* Complete corresponding request */
2028 if (ib_response_mad(&mad_recv_wc->recv_buf.mad->mad_hdr)) {
2029 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2030 mad_send_wr = ib_find_send_mad(mad_agent_priv, mad_recv_wc);
2031 if (!mad_send_wr) {
2032 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2033 if (!ib_mad_kernel_rmpp_agent(&mad_agent_priv->agent)
2034 && ib_is_mad_class_rmpp(mad_recv_wc->recv_buf.mad->mad_hdr.mgmt_class)
2035 && (ib_get_rmpp_flags(&((struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad)->rmpp_hdr)
2036 & IB_MGMT_RMPP_FLAG_ACTIVE)) {
2037 /* user rmpp is in effect
2038 * and this is an active RMPP MAD
2040 mad_agent_priv->agent.recv_handler(
2041 &mad_agent_priv->agent, NULL,
2042 mad_recv_wc);
2043 atomic_dec(&mad_agent_priv->refcount);
2044 } else {
2045 /* not user rmpp, revert to normal behavior and
2046 * drop the mad */
2047 ib_free_recv_mad(mad_recv_wc);
2048 deref_mad_agent(mad_agent_priv);
2049 return;
2051 } else {
2052 ib_mark_mad_done(mad_send_wr);
2053 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2055 /* Defined behavior is to complete response before request */
2056 mad_agent_priv->agent.recv_handler(
2057 &mad_agent_priv->agent,
2058 &mad_send_wr->send_buf,
2059 mad_recv_wc);
2060 atomic_dec(&mad_agent_priv->refcount);
2062 mad_send_wc.status = IB_WC_SUCCESS;
2063 mad_send_wc.vendor_err = 0;
2064 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2065 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
2067 } else {
2068 mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent, NULL,
2069 mad_recv_wc);
2070 deref_mad_agent(mad_agent_priv);
2073 return;
2076 static enum smi_action handle_ib_smi(const struct ib_mad_port_private *port_priv,
2077 const struct ib_mad_qp_info *qp_info,
2078 const struct ib_wc *wc,
2079 int port_num,
2080 struct ib_mad_private *recv,
2081 struct ib_mad_private *response)
2083 enum smi_forward_action retsmi;
2084 struct ib_smp *smp = (struct ib_smp *)recv->mad;
2086 trace_ib_mad_handle_ib_smi(smp);
2088 if (smi_handle_dr_smp_recv(smp,
2089 rdma_cap_ib_switch(port_priv->device),
2090 port_num,
2091 port_priv->device->phys_port_cnt) ==
2092 IB_SMI_DISCARD)
2093 return IB_SMI_DISCARD;
2095 retsmi = smi_check_forward_dr_smp(smp);
2096 if (retsmi == IB_SMI_LOCAL)
2097 return IB_SMI_HANDLE;
2099 if (retsmi == IB_SMI_SEND) { /* don't forward */
2100 if (smi_handle_dr_smp_send(smp,
2101 rdma_cap_ib_switch(port_priv->device),
2102 port_num) == IB_SMI_DISCARD)
2103 return IB_SMI_DISCARD;
2105 if (smi_check_local_smp(smp, port_priv->device) == IB_SMI_DISCARD)
2106 return IB_SMI_DISCARD;
2107 } else if (rdma_cap_ib_switch(port_priv->device)) {
2108 /* forward case for switches */
2109 memcpy(response, recv, mad_priv_size(response));
2110 response->header.recv_wc.wc = &response->header.wc;
2111 response->header.recv_wc.recv_buf.mad = (struct ib_mad *)response->mad;
2112 response->header.recv_wc.recv_buf.grh = &response->grh;
2114 agent_send_response((const struct ib_mad_hdr *)response->mad,
2115 &response->grh, wc,
2116 port_priv->device,
2117 smi_get_fwd_port(smp),
2118 qp_info->qp->qp_num,
2119 response->mad_size,
2120 false);
2122 return IB_SMI_DISCARD;
2124 return IB_SMI_HANDLE;
2127 static bool generate_unmatched_resp(const struct ib_mad_private *recv,
2128 struct ib_mad_private *response,
2129 size_t *resp_len, bool opa)
2131 const struct ib_mad_hdr *recv_hdr = (const struct ib_mad_hdr *)recv->mad;
2132 struct ib_mad_hdr *resp_hdr = (struct ib_mad_hdr *)response->mad;
2134 if (recv_hdr->method == IB_MGMT_METHOD_GET ||
2135 recv_hdr->method == IB_MGMT_METHOD_SET) {
2136 memcpy(response, recv, mad_priv_size(response));
2137 response->header.recv_wc.wc = &response->header.wc;
2138 response->header.recv_wc.recv_buf.mad = (struct ib_mad *)response->mad;
2139 response->header.recv_wc.recv_buf.grh = &response->grh;
2140 resp_hdr->method = IB_MGMT_METHOD_GET_RESP;
2141 resp_hdr->status = cpu_to_be16(IB_MGMT_MAD_STATUS_UNSUPPORTED_METHOD_ATTRIB);
2142 if (recv_hdr->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
2143 resp_hdr->status |= IB_SMP_DIRECTION;
2145 if (opa && recv_hdr->base_version == OPA_MGMT_BASE_VERSION) {
2146 if (recv_hdr->mgmt_class ==
2147 IB_MGMT_CLASS_SUBN_LID_ROUTED ||
2148 recv_hdr->mgmt_class ==
2149 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
2150 *resp_len = opa_get_smp_header_size(
2151 (struct opa_smp *)recv->mad);
2152 else
2153 *resp_len = sizeof(struct ib_mad_hdr);
2156 return true;
2157 } else {
2158 return false;
2162 static enum smi_action
2163 handle_opa_smi(struct ib_mad_port_private *port_priv,
2164 struct ib_mad_qp_info *qp_info,
2165 struct ib_wc *wc,
2166 int port_num,
2167 struct ib_mad_private *recv,
2168 struct ib_mad_private *response)
2170 enum smi_forward_action retsmi;
2171 struct opa_smp *smp = (struct opa_smp *)recv->mad;
2173 trace_ib_mad_handle_opa_smi(smp);
2175 if (opa_smi_handle_dr_smp_recv(smp,
2176 rdma_cap_ib_switch(port_priv->device),
2177 port_num,
2178 port_priv->device->phys_port_cnt) ==
2179 IB_SMI_DISCARD)
2180 return IB_SMI_DISCARD;
2182 retsmi = opa_smi_check_forward_dr_smp(smp);
2183 if (retsmi == IB_SMI_LOCAL)
2184 return IB_SMI_HANDLE;
2186 if (retsmi == IB_SMI_SEND) { /* don't forward */
2187 if (opa_smi_handle_dr_smp_send(smp,
2188 rdma_cap_ib_switch(port_priv->device),
2189 port_num) == IB_SMI_DISCARD)
2190 return IB_SMI_DISCARD;
2192 if (opa_smi_check_local_smp(smp, port_priv->device) ==
2193 IB_SMI_DISCARD)
2194 return IB_SMI_DISCARD;
2196 } else if (rdma_cap_ib_switch(port_priv->device)) {
2197 /* forward case for switches */
2198 memcpy(response, recv, mad_priv_size(response));
2199 response->header.recv_wc.wc = &response->header.wc;
2200 response->header.recv_wc.recv_buf.opa_mad =
2201 (struct opa_mad *)response->mad;
2202 response->header.recv_wc.recv_buf.grh = &response->grh;
2204 agent_send_response((const struct ib_mad_hdr *)response->mad,
2205 &response->grh, wc,
2206 port_priv->device,
2207 opa_smi_get_fwd_port(smp),
2208 qp_info->qp->qp_num,
2209 recv->header.wc.byte_len,
2210 true);
2212 return IB_SMI_DISCARD;
2215 return IB_SMI_HANDLE;
2218 static enum smi_action
2219 handle_smi(struct ib_mad_port_private *port_priv,
2220 struct ib_mad_qp_info *qp_info,
2221 struct ib_wc *wc,
2222 int port_num,
2223 struct ib_mad_private *recv,
2224 struct ib_mad_private *response,
2225 bool opa)
2227 struct ib_mad_hdr *mad_hdr = (struct ib_mad_hdr *)recv->mad;
2229 if (opa && mad_hdr->base_version == OPA_MGMT_BASE_VERSION &&
2230 mad_hdr->class_version == OPA_SM_CLASS_VERSION)
2231 return handle_opa_smi(port_priv, qp_info, wc, port_num, recv,
2232 response);
2234 return handle_ib_smi(port_priv, qp_info, wc, port_num, recv, response);
2237 static void ib_mad_recv_done(struct ib_cq *cq, struct ib_wc *wc)
2239 struct ib_mad_port_private *port_priv = cq->cq_context;
2240 struct ib_mad_list_head *mad_list =
2241 container_of(wc->wr_cqe, struct ib_mad_list_head, cqe);
2242 struct ib_mad_qp_info *qp_info;
2243 struct ib_mad_private_header *mad_priv_hdr;
2244 struct ib_mad_private *recv, *response = NULL;
2245 struct ib_mad_agent_private *mad_agent;
2246 int port_num;
2247 int ret = IB_MAD_RESULT_SUCCESS;
2248 size_t mad_size;
2249 u16 resp_mad_pkey_index = 0;
2250 bool opa;
2252 if (list_empty_careful(&port_priv->port_list))
2253 return;
2255 if (wc->status != IB_WC_SUCCESS) {
2257 * Receive errors indicate that the QP has entered the error
2258 * state - error handling/shutdown code will cleanup
2260 return;
2263 qp_info = mad_list->mad_queue->qp_info;
2264 dequeue_mad(mad_list);
2266 opa = rdma_cap_opa_mad(qp_info->port_priv->device,
2267 qp_info->port_priv->port_num);
2269 mad_priv_hdr = container_of(mad_list, struct ib_mad_private_header,
2270 mad_list);
2271 recv = container_of(mad_priv_hdr, struct ib_mad_private, header);
2272 ib_dma_unmap_single(port_priv->device,
2273 recv->header.mapping,
2274 mad_priv_dma_size(recv),
2275 DMA_FROM_DEVICE);
2277 /* Setup MAD receive work completion from "normal" work completion */
2278 recv->header.wc = *wc;
2279 recv->header.recv_wc.wc = &recv->header.wc;
2281 if (opa && ((struct ib_mad_hdr *)(recv->mad))->base_version == OPA_MGMT_BASE_VERSION) {
2282 recv->header.recv_wc.mad_len = wc->byte_len - sizeof(struct ib_grh);
2283 recv->header.recv_wc.mad_seg_size = sizeof(struct opa_mad);
2284 } else {
2285 recv->header.recv_wc.mad_len = sizeof(struct ib_mad);
2286 recv->header.recv_wc.mad_seg_size = sizeof(struct ib_mad);
2289 recv->header.recv_wc.recv_buf.mad = (struct ib_mad *)recv->mad;
2290 recv->header.recv_wc.recv_buf.grh = &recv->grh;
2292 if (atomic_read(&qp_info->snoop_count))
2293 snoop_recv(qp_info, &recv->header.recv_wc, IB_MAD_SNOOP_RECVS);
2295 /* Validate MAD */
2296 if (!validate_mad((const struct ib_mad_hdr *)recv->mad, qp_info, opa))
2297 goto out;
2299 trace_ib_mad_recv_done_handler(qp_info, wc,
2300 (struct ib_mad_hdr *)recv->mad);
2302 mad_size = recv->mad_size;
2303 response = alloc_mad_private(mad_size, GFP_KERNEL);
2304 if (!response)
2305 goto out;
2307 if (rdma_cap_ib_switch(port_priv->device))
2308 port_num = wc->port_num;
2309 else
2310 port_num = port_priv->port_num;
2312 if (((struct ib_mad_hdr *)recv->mad)->mgmt_class ==
2313 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
2314 if (handle_smi(port_priv, qp_info, wc, port_num, recv,
2315 response, opa)
2316 == IB_SMI_DISCARD)
2317 goto out;
2320 /* Give driver "right of first refusal" on incoming MAD */
2321 if (port_priv->device->ops.process_mad) {
2322 ret = port_priv->device->ops.process_mad(
2323 port_priv->device, 0, port_priv->port_num, wc,
2324 &recv->grh, (const struct ib_mad *)recv->mad,
2325 (struct ib_mad *)response->mad, &mad_size,
2326 &resp_mad_pkey_index);
2328 if (opa)
2329 wc->pkey_index = resp_mad_pkey_index;
2331 if (ret & IB_MAD_RESULT_SUCCESS) {
2332 if (ret & IB_MAD_RESULT_CONSUMED)
2333 goto out;
2334 if (ret & IB_MAD_RESULT_REPLY) {
2335 agent_send_response((const struct ib_mad_hdr *)response->mad,
2336 &recv->grh, wc,
2337 port_priv->device,
2338 port_num,
2339 qp_info->qp->qp_num,
2340 mad_size, opa);
2341 goto out;
2346 mad_agent = find_mad_agent(port_priv, (const struct ib_mad_hdr *)recv->mad);
2347 if (mad_agent) {
2348 trace_ib_mad_recv_done_agent(mad_agent);
2349 ib_mad_complete_recv(mad_agent, &recv->header.recv_wc);
2351 * recv is freed up in error cases in ib_mad_complete_recv
2352 * or via recv_handler in ib_mad_complete_recv()
2354 recv = NULL;
2355 } else if ((ret & IB_MAD_RESULT_SUCCESS) &&
2356 generate_unmatched_resp(recv, response, &mad_size, opa)) {
2357 agent_send_response((const struct ib_mad_hdr *)response->mad, &recv->grh, wc,
2358 port_priv->device, port_num,
2359 qp_info->qp->qp_num, mad_size, opa);
2362 out:
2363 /* Post another receive request for this QP */
2364 if (response) {
2365 ib_mad_post_receive_mads(qp_info, response);
2366 kfree(recv);
2367 } else
2368 ib_mad_post_receive_mads(qp_info, recv);
2371 static void adjust_timeout(struct ib_mad_agent_private *mad_agent_priv)
2373 struct ib_mad_send_wr_private *mad_send_wr;
2374 unsigned long delay;
2376 if (list_empty(&mad_agent_priv->wait_list)) {
2377 cancel_delayed_work(&mad_agent_priv->timed_work);
2378 } else {
2379 mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
2380 struct ib_mad_send_wr_private,
2381 agent_list);
2383 if (time_after(mad_agent_priv->timeout,
2384 mad_send_wr->timeout)) {
2385 mad_agent_priv->timeout = mad_send_wr->timeout;
2386 delay = mad_send_wr->timeout - jiffies;
2387 if ((long)delay <= 0)
2388 delay = 1;
2389 mod_delayed_work(mad_agent_priv->qp_info->port_priv->wq,
2390 &mad_agent_priv->timed_work, delay);
2395 static void wait_for_response(struct ib_mad_send_wr_private *mad_send_wr)
2397 struct ib_mad_agent_private *mad_agent_priv;
2398 struct ib_mad_send_wr_private *temp_mad_send_wr;
2399 struct list_head *list_item;
2400 unsigned long delay;
2402 mad_agent_priv = mad_send_wr->mad_agent_priv;
2403 list_del(&mad_send_wr->agent_list);
2405 delay = mad_send_wr->timeout;
2406 mad_send_wr->timeout += jiffies;
2408 if (delay) {
2409 list_for_each_prev(list_item, &mad_agent_priv->wait_list) {
2410 temp_mad_send_wr = list_entry(list_item,
2411 struct ib_mad_send_wr_private,
2412 agent_list);
2413 if (time_after(mad_send_wr->timeout,
2414 temp_mad_send_wr->timeout))
2415 break;
2418 else
2419 list_item = &mad_agent_priv->wait_list;
2420 list_add(&mad_send_wr->agent_list, list_item);
2422 /* Reschedule a work item if we have a shorter timeout */
2423 if (mad_agent_priv->wait_list.next == &mad_send_wr->agent_list)
2424 mod_delayed_work(mad_agent_priv->qp_info->port_priv->wq,
2425 &mad_agent_priv->timed_work, delay);
2428 void ib_reset_mad_timeout(struct ib_mad_send_wr_private *mad_send_wr,
2429 unsigned long timeout_ms)
2431 mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
2432 wait_for_response(mad_send_wr);
2436 * Process a send work completion
2438 void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr,
2439 struct ib_mad_send_wc *mad_send_wc)
2441 struct ib_mad_agent_private *mad_agent_priv;
2442 unsigned long flags;
2443 int ret;
2445 mad_agent_priv = mad_send_wr->mad_agent_priv;
2446 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2447 if (ib_mad_kernel_rmpp_agent(&mad_agent_priv->agent)) {
2448 ret = ib_process_rmpp_send_wc(mad_send_wr, mad_send_wc);
2449 if (ret == IB_RMPP_RESULT_CONSUMED)
2450 goto done;
2451 } else
2452 ret = IB_RMPP_RESULT_UNHANDLED;
2454 if (mad_send_wc->status != IB_WC_SUCCESS &&
2455 mad_send_wr->status == IB_WC_SUCCESS) {
2456 mad_send_wr->status = mad_send_wc->status;
2457 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2460 if (--mad_send_wr->refcount > 0) {
2461 if (mad_send_wr->refcount == 1 && mad_send_wr->timeout &&
2462 mad_send_wr->status == IB_WC_SUCCESS) {
2463 wait_for_response(mad_send_wr);
2465 goto done;
2468 /* Remove send from MAD agent and notify client of completion */
2469 list_del(&mad_send_wr->agent_list);
2470 adjust_timeout(mad_agent_priv);
2471 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2473 if (mad_send_wr->status != IB_WC_SUCCESS )
2474 mad_send_wc->status = mad_send_wr->status;
2475 if (ret == IB_RMPP_RESULT_INTERNAL)
2476 ib_rmpp_send_handler(mad_send_wc);
2477 else
2478 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2479 mad_send_wc);
2481 /* Release reference on agent taken when sending */
2482 deref_mad_agent(mad_agent_priv);
2483 return;
2484 done:
2485 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2488 static void ib_mad_send_done(struct ib_cq *cq, struct ib_wc *wc)
2490 struct ib_mad_port_private *port_priv = cq->cq_context;
2491 struct ib_mad_list_head *mad_list =
2492 container_of(wc->wr_cqe, struct ib_mad_list_head, cqe);
2493 struct ib_mad_send_wr_private *mad_send_wr, *queued_send_wr;
2494 struct ib_mad_qp_info *qp_info;
2495 struct ib_mad_queue *send_queue;
2496 struct ib_mad_send_wc mad_send_wc;
2497 unsigned long flags;
2498 int ret;
2500 if (list_empty_careful(&port_priv->port_list))
2501 return;
2503 if (wc->status != IB_WC_SUCCESS) {
2504 if (!ib_mad_send_error(port_priv, wc))
2505 return;
2508 mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
2509 mad_list);
2510 send_queue = mad_list->mad_queue;
2511 qp_info = send_queue->qp_info;
2513 trace_ib_mad_send_done_agent(mad_send_wr->mad_agent_priv);
2514 trace_ib_mad_send_done_handler(mad_send_wr, wc);
2516 retry:
2517 ib_dma_unmap_single(mad_send_wr->send_buf.mad_agent->device,
2518 mad_send_wr->header_mapping,
2519 mad_send_wr->sg_list[0].length, DMA_TO_DEVICE);
2520 ib_dma_unmap_single(mad_send_wr->send_buf.mad_agent->device,
2521 mad_send_wr->payload_mapping,
2522 mad_send_wr->sg_list[1].length, DMA_TO_DEVICE);
2523 queued_send_wr = NULL;
2524 spin_lock_irqsave(&send_queue->lock, flags);
2525 list_del(&mad_list->list);
2527 /* Move queued send to the send queue */
2528 if (send_queue->count-- > send_queue->max_active) {
2529 mad_list = container_of(qp_info->overflow_list.next,
2530 struct ib_mad_list_head, list);
2531 queued_send_wr = container_of(mad_list,
2532 struct ib_mad_send_wr_private,
2533 mad_list);
2534 list_move_tail(&mad_list->list, &send_queue->list);
2536 spin_unlock_irqrestore(&send_queue->lock, flags);
2538 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2539 mad_send_wc.status = wc->status;
2540 mad_send_wc.vendor_err = wc->vendor_err;
2541 if (atomic_read(&qp_info->snoop_count))
2542 snoop_send(qp_info, &mad_send_wr->send_buf, &mad_send_wc,
2543 IB_MAD_SNOOP_SEND_COMPLETIONS);
2544 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
2546 if (queued_send_wr) {
2547 trace_ib_mad_send_done_resend(queued_send_wr, qp_info);
2548 ret = ib_post_send(qp_info->qp, &queued_send_wr->send_wr.wr,
2549 NULL);
2550 if (ret) {
2551 dev_err(&port_priv->device->dev,
2552 "ib_post_send failed: %d\n", ret);
2553 mad_send_wr = queued_send_wr;
2554 wc->status = IB_WC_LOC_QP_OP_ERR;
2555 goto retry;
2560 static void mark_sends_for_retry(struct ib_mad_qp_info *qp_info)
2562 struct ib_mad_send_wr_private *mad_send_wr;
2563 struct ib_mad_list_head *mad_list;
2564 unsigned long flags;
2566 spin_lock_irqsave(&qp_info->send_queue.lock, flags);
2567 list_for_each_entry(mad_list, &qp_info->send_queue.list, list) {
2568 mad_send_wr = container_of(mad_list,
2569 struct ib_mad_send_wr_private,
2570 mad_list);
2571 mad_send_wr->retry = 1;
2573 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
2576 static bool ib_mad_send_error(struct ib_mad_port_private *port_priv,
2577 struct ib_wc *wc)
2579 struct ib_mad_list_head *mad_list =
2580 container_of(wc->wr_cqe, struct ib_mad_list_head, cqe);
2581 struct ib_mad_qp_info *qp_info = mad_list->mad_queue->qp_info;
2582 struct ib_mad_send_wr_private *mad_send_wr;
2583 int ret;
2586 * Send errors will transition the QP to SQE - move
2587 * QP to RTS and repost flushed work requests
2589 mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
2590 mad_list);
2591 if (wc->status == IB_WC_WR_FLUSH_ERR) {
2592 if (mad_send_wr->retry) {
2593 /* Repost send */
2594 mad_send_wr->retry = 0;
2595 trace_ib_mad_error_handler(mad_send_wr, qp_info);
2596 ret = ib_post_send(qp_info->qp, &mad_send_wr->send_wr.wr,
2597 NULL);
2598 if (!ret)
2599 return false;
2601 } else {
2602 struct ib_qp_attr *attr;
2604 /* Transition QP to RTS and fail offending send */
2605 attr = kmalloc(sizeof *attr, GFP_KERNEL);
2606 if (attr) {
2607 attr->qp_state = IB_QPS_RTS;
2608 attr->cur_qp_state = IB_QPS_SQE;
2609 ret = ib_modify_qp(qp_info->qp, attr,
2610 IB_QP_STATE | IB_QP_CUR_STATE);
2611 kfree(attr);
2612 if (ret)
2613 dev_err(&port_priv->device->dev,
2614 "%s - ib_modify_qp to RTS: %d\n",
2615 __func__, ret);
2616 else
2617 mark_sends_for_retry(qp_info);
2621 return true;
2624 static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv)
2626 unsigned long flags;
2627 struct ib_mad_send_wr_private *mad_send_wr, *temp_mad_send_wr;
2628 struct ib_mad_send_wc mad_send_wc;
2629 struct list_head cancel_list;
2631 INIT_LIST_HEAD(&cancel_list);
2633 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2634 list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
2635 &mad_agent_priv->send_list, agent_list) {
2636 if (mad_send_wr->status == IB_WC_SUCCESS) {
2637 mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
2638 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2642 /* Empty wait list to prevent receives from finding a request */
2643 list_splice_init(&mad_agent_priv->wait_list, &cancel_list);
2644 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2646 /* Report all cancelled requests */
2647 mad_send_wc.status = IB_WC_WR_FLUSH_ERR;
2648 mad_send_wc.vendor_err = 0;
2650 list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
2651 &cancel_list, agent_list) {
2652 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2653 list_del(&mad_send_wr->agent_list);
2654 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2655 &mad_send_wc);
2656 atomic_dec(&mad_agent_priv->refcount);
2660 static struct ib_mad_send_wr_private*
2661 find_send_wr(struct ib_mad_agent_private *mad_agent_priv,
2662 struct ib_mad_send_buf *send_buf)
2664 struct ib_mad_send_wr_private *mad_send_wr;
2666 list_for_each_entry(mad_send_wr, &mad_agent_priv->wait_list,
2667 agent_list) {
2668 if (&mad_send_wr->send_buf == send_buf)
2669 return mad_send_wr;
2672 list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list,
2673 agent_list) {
2674 if (is_rmpp_data_mad(mad_agent_priv,
2675 mad_send_wr->send_buf.mad) &&
2676 &mad_send_wr->send_buf == send_buf)
2677 return mad_send_wr;
2679 return NULL;
2682 int ib_modify_mad(struct ib_mad_agent *mad_agent,
2683 struct ib_mad_send_buf *send_buf, u32 timeout_ms)
2685 struct ib_mad_agent_private *mad_agent_priv;
2686 struct ib_mad_send_wr_private *mad_send_wr;
2687 unsigned long flags;
2688 int active;
2690 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
2691 agent);
2692 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2693 mad_send_wr = find_send_wr(mad_agent_priv, send_buf);
2694 if (!mad_send_wr || mad_send_wr->status != IB_WC_SUCCESS) {
2695 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2696 return -EINVAL;
2699 active = (!mad_send_wr->timeout || mad_send_wr->refcount > 1);
2700 if (!timeout_ms) {
2701 mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
2702 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2705 mad_send_wr->send_buf.timeout_ms = timeout_ms;
2706 if (active)
2707 mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
2708 else
2709 ib_reset_mad_timeout(mad_send_wr, timeout_ms);
2711 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2712 return 0;
2714 EXPORT_SYMBOL(ib_modify_mad);
2716 void ib_cancel_mad(struct ib_mad_agent *mad_agent,
2717 struct ib_mad_send_buf *send_buf)
2719 ib_modify_mad(mad_agent, send_buf, 0);
2721 EXPORT_SYMBOL(ib_cancel_mad);
2723 static void local_completions(struct work_struct *work)
2725 struct ib_mad_agent_private *mad_agent_priv;
2726 struct ib_mad_local_private *local;
2727 struct ib_mad_agent_private *recv_mad_agent;
2728 unsigned long flags;
2729 int free_mad;
2730 struct ib_wc wc;
2731 struct ib_mad_send_wc mad_send_wc;
2732 bool opa;
2734 mad_agent_priv =
2735 container_of(work, struct ib_mad_agent_private, local_work);
2737 opa = rdma_cap_opa_mad(mad_agent_priv->qp_info->port_priv->device,
2738 mad_agent_priv->qp_info->port_priv->port_num);
2740 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2741 while (!list_empty(&mad_agent_priv->local_list)) {
2742 local = list_entry(mad_agent_priv->local_list.next,
2743 struct ib_mad_local_private,
2744 completion_list);
2745 list_del(&local->completion_list);
2746 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2747 free_mad = 0;
2748 if (local->mad_priv) {
2749 u8 base_version;
2750 recv_mad_agent = local->recv_mad_agent;
2751 if (!recv_mad_agent) {
2752 dev_err(&mad_agent_priv->agent.device->dev,
2753 "No receive MAD agent for local completion\n");
2754 free_mad = 1;
2755 goto local_send_completion;
2759 * Defined behavior is to complete response
2760 * before request
2762 build_smp_wc(recv_mad_agent->agent.qp,
2763 local->mad_send_wr->send_wr.wr.wr_cqe,
2764 be16_to_cpu(IB_LID_PERMISSIVE),
2765 local->mad_send_wr->send_wr.pkey_index,
2766 recv_mad_agent->agent.port_num, &wc);
2768 local->mad_priv->header.recv_wc.wc = &wc;
2770 base_version = ((struct ib_mad_hdr *)(local->mad_priv->mad))->base_version;
2771 if (opa && base_version == OPA_MGMT_BASE_VERSION) {
2772 local->mad_priv->header.recv_wc.mad_len = local->return_wc_byte_len;
2773 local->mad_priv->header.recv_wc.mad_seg_size = sizeof(struct opa_mad);
2774 } else {
2775 local->mad_priv->header.recv_wc.mad_len = sizeof(struct ib_mad);
2776 local->mad_priv->header.recv_wc.mad_seg_size = sizeof(struct ib_mad);
2779 INIT_LIST_HEAD(&local->mad_priv->header.recv_wc.rmpp_list);
2780 list_add(&local->mad_priv->header.recv_wc.recv_buf.list,
2781 &local->mad_priv->header.recv_wc.rmpp_list);
2782 local->mad_priv->header.recv_wc.recv_buf.grh = NULL;
2783 local->mad_priv->header.recv_wc.recv_buf.mad =
2784 (struct ib_mad *)local->mad_priv->mad;
2785 if (atomic_read(&recv_mad_agent->qp_info->snoop_count))
2786 snoop_recv(recv_mad_agent->qp_info,
2787 &local->mad_priv->header.recv_wc,
2788 IB_MAD_SNOOP_RECVS);
2789 recv_mad_agent->agent.recv_handler(
2790 &recv_mad_agent->agent,
2791 &local->mad_send_wr->send_buf,
2792 &local->mad_priv->header.recv_wc);
2793 spin_lock_irqsave(&recv_mad_agent->lock, flags);
2794 atomic_dec(&recv_mad_agent->refcount);
2795 spin_unlock_irqrestore(&recv_mad_agent->lock, flags);
2798 local_send_completion:
2799 /* Complete send */
2800 mad_send_wc.status = IB_WC_SUCCESS;
2801 mad_send_wc.vendor_err = 0;
2802 mad_send_wc.send_buf = &local->mad_send_wr->send_buf;
2803 if (atomic_read(&mad_agent_priv->qp_info->snoop_count))
2804 snoop_send(mad_agent_priv->qp_info,
2805 &local->mad_send_wr->send_buf,
2806 &mad_send_wc, IB_MAD_SNOOP_SEND_COMPLETIONS);
2807 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2808 &mad_send_wc);
2810 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2811 atomic_dec(&mad_agent_priv->refcount);
2812 if (free_mad)
2813 kfree(local->mad_priv);
2814 kfree(local);
2816 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2819 static int retry_send(struct ib_mad_send_wr_private *mad_send_wr)
2821 int ret;
2823 if (!mad_send_wr->retries_left)
2824 return -ETIMEDOUT;
2826 mad_send_wr->retries_left--;
2827 mad_send_wr->send_buf.retries++;
2829 mad_send_wr->timeout = msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms);
2831 if (ib_mad_kernel_rmpp_agent(&mad_send_wr->mad_agent_priv->agent)) {
2832 ret = ib_retry_rmpp(mad_send_wr);
2833 switch (ret) {
2834 case IB_RMPP_RESULT_UNHANDLED:
2835 ret = ib_send_mad(mad_send_wr);
2836 break;
2837 case IB_RMPP_RESULT_CONSUMED:
2838 ret = 0;
2839 break;
2840 default:
2841 ret = -ECOMM;
2842 break;
2844 } else
2845 ret = ib_send_mad(mad_send_wr);
2847 if (!ret) {
2848 mad_send_wr->refcount++;
2849 list_add_tail(&mad_send_wr->agent_list,
2850 &mad_send_wr->mad_agent_priv->send_list);
2852 return ret;
2855 static void timeout_sends(struct work_struct *work)
2857 struct ib_mad_agent_private *mad_agent_priv;
2858 struct ib_mad_send_wr_private *mad_send_wr;
2859 struct ib_mad_send_wc mad_send_wc;
2860 unsigned long flags, delay;
2862 mad_agent_priv = container_of(work, struct ib_mad_agent_private,
2863 timed_work.work);
2864 mad_send_wc.vendor_err = 0;
2866 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2867 while (!list_empty(&mad_agent_priv->wait_list)) {
2868 mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
2869 struct ib_mad_send_wr_private,
2870 agent_list);
2872 if (time_after(mad_send_wr->timeout, jiffies)) {
2873 delay = mad_send_wr->timeout - jiffies;
2874 if ((long)delay <= 0)
2875 delay = 1;
2876 queue_delayed_work(mad_agent_priv->qp_info->
2877 port_priv->wq,
2878 &mad_agent_priv->timed_work, delay);
2879 break;
2882 list_del(&mad_send_wr->agent_list);
2883 if (mad_send_wr->status == IB_WC_SUCCESS &&
2884 !retry_send(mad_send_wr))
2885 continue;
2887 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2889 if (mad_send_wr->status == IB_WC_SUCCESS)
2890 mad_send_wc.status = IB_WC_RESP_TIMEOUT_ERR;
2891 else
2892 mad_send_wc.status = mad_send_wr->status;
2893 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2894 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2895 &mad_send_wc);
2897 atomic_dec(&mad_agent_priv->refcount);
2898 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2900 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2904 * Allocate receive MADs and post receive WRs for them
2906 static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
2907 struct ib_mad_private *mad)
2909 unsigned long flags;
2910 int post, ret;
2911 struct ib_mad_private *mad_priv;
2912 struct ib_sge sg_list;
2913 struct ib_recv_wr recv_wr;
2914 struct ib_mad_queue *recv_queue = &qp_info->recv_queue;
2916 /* Initialize common scatter list fields */
2917 sg_list.lkey = qp_info->port_priv->pd->local_dma_lkey;
2919 /* Initialize common receive WR fields */
2920 recv_wr.next = NULL;
2921 recv_wr.sg_list = &sg_list;
2922 recv_wr.num_sge = 1;
2924 do {
2925 /* Allocate and map receive buffer */
2926 if (mad) {
2927 mad_priv = mad;
2928 mad = NULL;
2929 } else {
2930 mad_priv = alloc_mad_private(port_mad_size(qp_info->port_priv),
2931 GFP_ATOMIC);
2932 if (!mad_priv) {
2933 ret = -ENOMEM;
2934 break;
2937 sg_list.length = mad_priv_dma_size(mad_priv);
2938 sg_list.addr = ib_dma_map_single(qp_info->port_priv->device,
2939 &mad_priv->grh,
2940 mad_priv_dma_size(mad_priv),
2941 DMA_FROM_DEVICE);
2942 if (unlikely(ib_dma_mapping_error(qp_info->port_priv->device,
2943 sg_list.addr))) {
2944 kfree(mad_priv);
2945 ret = -ENOMEM;
2946 break;
2948 mad_priv->header.mapping = sg_list.addr;
2949 mad_priv->header.mad_list.mad_queue = recv_queue;
2950 mad_priv->header.mad_list.cqe.done = ib_mad_recv_done;
2951 recv_wr.wr_cqe = &mad_priv->header.mad_list.cqe;
2953 /* Post receive WR */
2954 spin_lock_irqsave(&recv_queue->lock, flags);
2955 post = (++recv_queue->count < recv_queue->max_active);
2956 list_add_tail(&mad_priv->header.mad_list.list, &recv_queue->list);
2957 spin_unlock_irqrestore(&recv_queue->lock, flags);
2958 ret = ib_post_recv(qp_info->qp, &recv_wr, NULL);
2959 if (ret) {
2960 spin_lock_irqsave(&recv_queue->lock, flags);
2961 list_del(&mad_priv->header.mad_list.list);
2962 recv_queue->count--;
2963 spin_unlock_irqrestore(&recv_queue->lock, flags);
2964 ib_dma_unmap_single(qp_info->port_priv->device,
2965 mad_priv->header.mapping,
2966 mad_priv_dma_size(mad_priv),
2967 DMA_FROM_DEVICE);
2968 kfree(mad_priv);
2969 dev_err(&qp_info->port_priv->device->dev,
2970 "ib_post_recv failed: %d\n", ret);
2971 break;
2973 } while (post);
2975 return ret;
2979 * Return all the posted receive MADs
2981 static void cleanup_recv_queue(struct ib_mad_qp_info *qp_info)
2983 struct ib_mad_private_header *mad_priv_hdr;
2984 struct ib_mad_private *recv;
2985 struct ib_mad_list_head *mad_list;
2987 if (!qp_info->qp)
2988 return;
2990 while (!list_empty(&qp_info->recv_queue.list)) {
2992 mad_list = list_entry(qp_info->recv_queue.list.next,
2993 struct ib_mad_list_head, list);
2994 mad_priv_hdr = container_of(mad_list,
2995 struct ib_mad_private_header,
2996 mad_list);
2997 recv = container_of(mad_priv_hdr, struct ib_mad_private,
2998 header);
3000 /* Remove from posted receive MAD list */
3001 list_del(&mad_list->list);
3003 ib_dma_unmap_single(qp_info->port_priv->device,
3004 recv->header.mapping,
3005 mad_priv_dma_size(recv),
3006 DMA_FROM_DEVICE);
3007 kfree(recv);
3010 qp_info->recv_queue.count = 0;
3014 * Start the port
3016 static int ib_mad_port_start(struct ib_mad_port_private *port_priv)
3018 int ret, i;
3019 struct ib_qp_attr *attr;
3020 struct ib_qp *qp;
3021 u16 pkey_index;
3023 attr = kmalloc(sizeof *attr, GFP_KERNEL);
3024 if (!attr)
3025 return -ENOMEM;
3027 ret = ib_find_pkey(port_priv->device, port_priv->port_num,
3028 IB_DEFAULT_PKEY_FULL, &pkey_index);
3029 if (ret)
3030 pkey_index = 0;
3032 for (i = 0; i < IB_MAD_QPS_CORE; i++) {
3033 qp = port_priv->qp_info[i].qp;
3034 if (!qp)
3035 continue;
3038 * PKey index for QP1 is irrelevant but
3039 * one is needed for the Reset to Init transition
3041 attr->qp_state = IB_QPS_INIT;
3042 attr->pkey_index = pkey_index;
3043 attr->qkey = (qp->qp_num == 0) ? 0 : IB_QP1_QKEY;
3044 ret = ib_modify_qp(qp, attr, IB_QP_STATE |
3045 IB_QP_PKEY_INDEX | IB_QP_QKEY);
3046 if (ret) {
3047 dev_err(&port_priv->device->dev,
3048 "Couldn't change QP%d state to INIT: %d\n",
3049 i, ret);
3050 goto out;
3053 attr->qp_state = IB_QPS_RTR;
3054 ret = ib_modify_qp(qp, attr, IB_QP_STATE);
3055 if (ret) {
3056 dev_err(&port_priv->device->dev,
3057 "Couldn't change QP%d state to RTR: %d\n",
3058 i, ret);
3059 goto out;
3062 attr->qp_state = IB_QPS_RTS;
3063 attr->sq_psn = IB_MAD_SEND_Q_PSN;
3064 ret = ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_SQ_PSN);
3065 if (ret) {
3066 dev_err(&port_priv->device->dev,
3067 "Couldn't change QP%d state to RTS: %d\n",
3068 i, ret);
3069 goto out;
3073 ret = ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
3074 if (ret) {
3075 dev_err(&port_priv->device->dev,
3076 "Failed to request completion notification: %d\n",
3077 ret);
3078 goto out;
3081 for (i = 0; i < IB_MAD_QPS_CORE; i++) {
3082 if (!port_priv->qp_info[i].qp)
3083 continue;
3085 ret = ib_mad_post_receive_mads(&port_priv->qp_info[i], NULL);
3086 if (ret) {
3087 dev_err(&port_priv->device->dev,
3088 "Couldn't post receive WRs\n");
3089 goto out;
3092 out:
3093 kfree(attr);
3094 return ret;
3097 static void qp_event_handler(struct ib_event *event, void *qp_context)
3099 struct ib_mad_qp_info *qp_info = qp_context;
3101 /* It's worse than that! He's dead, Jim! */
3102 dev_err(&qp_info->port_priv->device->dev,
3103 "Fatal error (%d) on MAD QP (%d)\n",
3104 event->event, qp_info->qp->qp_num);
3107 static void init_mad_queue(struct ib_mad_qp_info *qp_info,
3108 struct ib_mad_queue *mad_queue)
3110 mad_queue->qp_info = qp_info;
3111 mad_queue->count = 0;
3112 spin_lock_init(&mad_queue->lock);
3113 INIT_LIST_HEAD(&mad_queue->list);
3116 static void init_mad_qp(struct ib_mad_port_private *port_priv,
3117 struct ib_mad_qp_info *qp_info)
3119 qp_info->port_priv = port_priv;
3120 init_mad_queue(qp_info, &qp_info->send_queue);
3121 init_mad_queue(qp_info, &qp_info->recv_queue);
3122 INIT_LIST_HEAD(&qp_info->overflow_list);
3123 spin_lock_init(&qp_info->snoop_lock);
3124 qp_info->snoop_table = NULL;
3125 qp_info->snoop_table_size = 0;
3126 atomic_set(&qp_info->snoop_count, 0);
3129 static int create_mad_qp(struct ib_mad_qp_info *qp_info,
3130 enum ib_qp_type qp_type)
3132 struct ib_qp_init_attr qp_init_attr;
3133 int ret;
3135 memset(&qp_init_attr, 0, sizeof qp_init_attr);
3136 qp_init_attr.send_cq = qp_info->port_priv->cq;
3137 qp_init_attr.recv_cq = qp_info->port_priv->cq;
3138 qp_init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
3139 qp_init_attr.cap.max_send_wr = mad_sendq_size;
3140 qp_init_attr.cap.max_recv_wr = mad_recvq_size;
3141 qp_init_attr.cap.max_send_sge = IB_MAD_SEND_REQ_MAX_SG;
3142 qp_init_attr.cap.max_recv_sge = IB_MAD_RECV_REQ_MAX_SG;
3143 qp_init_attr.qp_type = qp_type;
3144 qp_init_attr.port_num = qp_info->port_priv->port_num;
3145 qp_init_attr.qp_context = qp_info;
3146 qp_init_attr.event_handler = qp_event_handler;
3147 qp_info->qp = ib_create_qp(qp_info->port_priv->pd, &qp_init_attr);
3148 if (IS_ERR(qp_info->qp)) {
3149 dev_err(&qp_info->port_priv->device->dev,
3150 "Couldn't create ib_mad QP%d\n",
3151 get_spl_qp_index(qp_type));
3152 ret = PTR_ERR(qp_info->qp);
3153 goto error;
3155 /* Use minimum queue sizes unless the CQ is resized */
3156 qp_info->send_queue.max_active = mad_sendq_size;
3157 qp_info->recv_queue.max_active = mad_recvq_size;
3158 return 0;
3160 error:
3161 return ret;
3164 static void destroy_mad_qp(struct ib_mad_qp_info *qp_info)
3166 if (!qp_info->qp)
3167 return;
3169 ib_destroy_qp(qp_info->qp);
3170 kfree(qp_info->snoop_table);
3174 * Open the port
3175 * Create the QP, PD, MR, and CQ if needed
3177 static int ib_mad_port_open(struct ib_device *device,
3178 int port_num)
3180 int ret, cq_size;
3181 struct ib_mad_port_private *port_priv;
3182 unsigned long flags;
3183 char name[sizeof "ib_mad123"];
3184 int has_smi;
3186 if (WARN_ON(rdma_max_mad_size(device, port_num) < IB_MGMT_MAD_SIZE))
3187 return -EFAULT;
3189 if (WARN_ON(rdma_cap_opa_mad(device, port_num) &&
3190 rdma_max_mad_size(device, port_num) < OPA_MGMT_MAD_SIZE))
3191 return -EFAULT;
3193 /* Create new device info */
3194 port_priv = kzalloc(sizeof *port_priv, GFP_KERNEL);
3195 if (!port_priv)
3196 return -ENOMEM;
3198 port_priv->device = device;
3199 port_priv->port_num = port_num;
3200 spin_lock_init(&port_priv->reg_lock);
3201 init_mad_qp(port_priv, &port_priv->qp_info[0]);
3202 init_mad_qp(port_priv, &port_priv->qp_info[1]);
3204 cq_size = mad_sendq_size + mad_recvq_size;
3205 has_smi = rdma_cap_ib_smi(device, port_num);
3206 if (has_smi)
3207 cq_size *= 2;
3209 port_priv->pd = ib_alloc_pd(device, 0);
3210 if (IS_ERR(port_priv->pd)) {
3211 dev_err(&device->dev, "Couldn't create ib_mad PD\n");
3212 ret = PTR_ERR(port_priv->pd);
3213 goto error3;
3216 port_priv->cq = ib_alloc_cq(port_priv->device, port_priv, cq_size, 0,
3217 IB_POLL_UNBOUND_WORKQUEUE);
3218 if (IS_ERR(port_priv->cq)) {
3219 dev_err(&device->dev, "Couldn't create ib_mad CQ\n");
3220 ret = PTR_ERR(port_priv->cq);
3221 goto error4;
3224 if (has_smi) {
3225 ret = create_mad_qp(&port_priv->qp_info[0], IB_QPT_SMI);
3226 if (ret)
3227 goto error6;
3229 ret = create_mad_qp(&port_priv->qp_info[1], IB_QPT_GSI);
3230 if (ret)
3231 goto error7;
3233 snprintf(name, sizeof name, "ib_mad%d", port_num);
3234 port_priv->wq = alloc_ordered_workqueue(name, WQ_MEM_RECLAIM);
3235 if (!port_priv->wq) {
3236 ret = -ENOMEM;
3237 goto error8;
3240 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
3241 list_add_tail(&port_priv->port_list, &ib_mad_port_list);
3242 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
3244 ret = ib_mad_port_start(port_priv);
3245 if (ret) {
3246 dev_err(&device->dev, "Couldn't start port\n");
3247 goto error9;
3250 return 0;
3252 error9:
3253 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
3254 list_del_init(&port_priv->port_list);
3255 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
3257 destroy_workqueue(port_priv->wq);
3258 error8:
3259 destroy_mad_qp(&port_priv->qp_info[1]);
3260 error7:
3261 destroy_mad_qp(&port_priv->qp_info[0]);
3262 error6:
3263 ib_free_cq(port_priv->cq);
3264 cleanup_recv_queue(&port_priv->qp_info[1]);
3265 cleanup_recv_queue(&port_priv->qp_info[0]);
3266 error4:
3267 ib_dealloc_pd(port_priv->pd);
3268 error3:
3269 kfree(port_priv);
3271 return ret;
3275 * Close the port
3276 * If there are no classes using the port, free the port
3277 * resources (CQ, MR, PD, QP) and remove the port's info structure
3279 static int ib_mad_port_close(struct ib_device *device, int port_num)
3281 struct ib_mad_port_private *port_priv;
3282 unsigned long flags;
3284 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
3285 port_priv = __ib_get_mad_port(device, port_num);
3286 if (port_priv == NULL) {
3287 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
3288 dev_err(&device->dev, "Port %d not found\n", port_num);
3289 return -ENODEV;
3291 list_del_init(&port_priv->port_list);
3292 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
3294 destroy_workqueue(port_priv->wq);
3295 destroy_mad_qp(&port_priv->qp_info[1]);
3296 destroy_mad_qp(&port_priv->qp_info[0]);
3297 ib_free_cq(port_priv->cq);
3298 ib_dealloc_pd(port_priv->pd);
3299 cleanup_recv_queue(&port_priv->qp_info[1]);
3300 cleanup_recv_queue(&port_priv->qp_info[0]);
3301 /* XXX: Handle deallocation of MAD registration tables */
3303 kfree(port_priv);
3305 return 0;
3308 static void ib_mad_init_device(struct ib_device *device)
3310 int start, i;
3312 start = rdma_start_port(device);
3314 for (i = start; i <= rdma_end_port(device); i++) {
3315 if (!rdma_cap_ib_mad(device, i))
3316 continue;
3318 if (ib_mad_port_open(device, i)) {
3319 dev_err(&device->dev, "Couldn't open port %d\n", i);
3320 goto error;
3322 if (ib_agent_port_open(device, i)) {
3323 dev_err(&device->dev,
3324 "Couldn't open port %d for agents\n", i);
3325 goto error_agent;
3328 return;
3330 error_agent:
3331 if (ib_mad_port_close(device, i))
3332 dev_err(&device->dev, "Couldn't close port %d\n", i);
3334 error:
3335 while (--i >= start) {
3336 if (!rdma_cap_ib_mad(device, i))
3337 continue;
3339 if (ib_agent_port_close(device, i))
3340 dev_err(&device->dev,
3341 "Couldn't close port %d for agents\n", i);
3342 if (ib_mad_port_close(device, i))
3343 dev_err(&device->dev, "Couldn't close port %d\n", i);
3347 static void ib_mad_remove_device(struct ib_device *device, void *client_data)
3349 unsigned int i;
3351 rdma_for_each_port (device, i) {
3352 if (!rdma_cap_ib_mad(device, i))
3353 continue;
3355 if (ib_agent_port_close(device, i))
3356 dev_err(&device->dev,
3357 "Couldn't close port %d for agents\n", i);
3358 if (ib_mad_port_close(device, i))
3359 dev_err(&device->dev, "Couldn't close port %d\n", i);
3363 static struct ib_client mad_client = {
3364 .name = "mad",
3365 .add = ib_mad_init_device,
3366 .remove = ib_mad_remove_device
3369 int ib_mad_init(void)
3371 mad_recvq_size = min(mad_recvq_size, IB_MAD_QP_MAX_SIZE);
3372 mad_recvq_size = max(mad_recvq_size, IB_MAD_QP_MIN_SIZE);
3374 mad_sendq_size = min(mad_sendq_size, IB_MAD_QP_MAX_SIZE);
3375 mad_sendq_size = max(mad_sendq_size, IB_MAD_QP_MIN_SIZE);
3377 INIT_LIST_HEAD(&ib_mad_port_list);
3379 if (ib_register_client(&mad_client)) {
3380 pr_err("Couldn't register ib_mad client\n");
3381 return -EINVAL;
3384 return 0;
3387 void ib_mad_cleanup(void)
3389 ib_unregister_client(&mad_client);