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.
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
39 #include <linux/dma-mapping.h>
40 #include <linux/slab.h>
41 #include <linux/module.h>
42 #include <rdma/ib_cache.h>
49 MODULE_LICENSE("Dual BSD/GPL");
50 MODULE_DESCRIPTION("kernel IB MAD API");
51 MODULE_AUTHOR("Hal Rosenstock");
52 MODULE_AUTHOR("Sean Hefty");
54 static int mad_sendq_size = IB_MAD_QP_SEND_SIZE;
55 static int mad_recvq_size = IB_MAD_QP_RECV_SIZE;
57 module_param_named(send_queue_size, mad_sendq_size, int, 0444);
58 MODULE_PARM_DESC(send_queue_size, "Size of send queue in number of work requests");
59 module_param_named(recv_queue_size, mad_recvq_size, int, 0444);
60 MODULE_PARM_DESC(recv_queue_size, "Size of receive queue in number of work requests");
62 static struct kmem_cache *ib_mad_cache;
64 static struct list_head ib_mad_port_list;
65 static u32 ib_mad_client_id = 0;
68 static DEFINE_SPINLOCK(ib_mad_port_list_lock);
70 /* Forward declarations */
71 static int method_in_use(struct ib_mad_mgmt_method_table **method,
72 struct ib_mad_reg_req *mad_reg_req);
73 static void remove_mad_reg_req(struct ib_mad_agent_private *priv);
74 static struct ib_mad_agent_private *find_mad_agent(
75 struct ib_mad_port_private *port_priv,
77 static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
78 struct ib_mad_private *mad);
79 static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv);
80 static void timeout_sends(struct work_struct *work);
81 static void local_completions(struct work_struct *work);
82 static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
83 struct ib_mad_agent_private *agent_priv,
85 static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
86 struct ib_mad_agent_private *agent_priv);
89 * Returns a ib_mad_port_private structure or NULL for a device/port
90 * Assumes ib_mad_port_list_lock is being held
92 static inline struct ib_mad_port_private *
93 __ib_get_mad_port(struct ib_device *device, int port_num)
95 struct ib_mad_port_private *entry;
97 list_for_each_entry(entry, &ib_mad_port_list, port_list) {
98 if (entry->device == device && entry->port_num == port_num)
105 * Wrapper function to return a ib_mad_port_private structure or NULL
108 static inline struct ib_mad_port_private *
109 ib_get_mad_port(struct ib_device *device, int port_num)
111 struct ib_mad_port_private *entry;
114 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
115 entry = __ib_get_mad_port(device, port_num);
116 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
121 static inline u8 convert_mgmt_class(u8 mgmt_class)
123 /* Alias IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE to 0 */
124 return mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE ?
128 static int get_spl_qp_index(enum ib_qp_type qp_type)
141 static int vendor_class_index(u8 mgmt_class)
143 return mgmt_class - IB_MGMT_CLASS_VENDOR_RANGE2_START;
146 static int is_vendor_class(u8 mgmt_class)
148 if ((mgmt_class < IB_MGMT_CLASS_VENDOR_RANGE2_START) ||
149 (mgmt_class > IB_MGMT_CLASS_VENDOR_RANGE2_END))
154 static int is_vendor_oui(char *oui)
156 if (oui[0] || oui[1] || oui[2])
161 static int is_vendor_method_in_use(
162 struct ib_mad_mgmt_vendor_class *vendor_class,
163 struct ib_mad_reg_req *mad_reg_req)
165 struct ib_mad_mgmt_method_table *method;
168 for (i = 0; i < MAX_MGMT_OUI; i++) {
169 if (!memcmp(vendor_class->oui[i], mad_reg_req->oui, 3)) {
170 method = vendor_class->method_table[i];
172 if (method_in_use(&method, mad_reg_req))
182 int ib_response_mad(struct ib_mad *mad)
184 return ((mad->mad_hdr.method & IB_MGMT_METHOD_RESP) ||
185 (mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS) ||
186 ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_BM) &&
187 (mad->mad_hdr.attr_mod & IB_BM_ATTR_MOD_RESP)));
189 EXPORT_SYMBOL(ib_response_mad);
192 * ib_register_mad_agent - Register to send/receive MADs
194 struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
196 enum ib_qp_type qp_type,
197 struct ib_mad_reg_req *mad_reg_req,
199 ib_mad_send_handler send_handler,
200 ib_mad_recv_handler recv_handler,
203 struct ib_mad_port_private *port_priv;
204 struct ib_mad_agent *ret = ERR_PTR(-EINVAL);
205 struct ib_mad_agent_private *mad_agent_priv;
206 struct ib_mad_reg_req *reg_req = NULL;
207 struct ib_mad_mgmt_class_table *class;
208 struct ib_mad_mgmt_vendor_class_table *vendor;
209 struct ib_mad_mgmt_vendor_class *vendor_class;
210 struct ib_mad_mgmt_method_table *method;
213 u8 mgmt_class, vclass;
215 /* Validate parameters */
216 qpn = get_spl_qp_index(qp_type);
218 dev_notice(&device->dev,
219 "ib_register_mad_agent: invalid QP Type %d\n",
224 if (rmpp_version && rmpp_version != IB_MGMT_RMPP_VERSION) {
225 dev_notice(&device->dev,
226 "ib_register_mad_agent: invalid RMPP Version %u\n",
231 /* Validate MAD registration request if supplied */
233 if (mad_reg_req->mgmt_class_version >= MAX_MGMT_VERSION) {
234 dev_notice(&device->dev,
235 "ib_register_mad_agent: invalid Class Version %u\n",
236 mad_reg_req->mgmt_class_version);
240 dev_notice(&device->dev,
241 "ib_register_mad_agent: no recv_handler\n");
244 if (mad_reg_req->mgmt_class >= MAX_MGMT_CLASS) {
246 * IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE is the only
247 * one in this range currently allowed
249 if (mad_reg_req->mgmt_class !=
250 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
251 dev_notice(&device->dev,
252 "ib_register_mad_agent: Invalid Mgmt Class 0x%x\n",
253 mad_reg_req->mgmt_class);
256 } else if (mad_reg_req->mgmt_class == 0) {
258 * Class 0 is reserved in IBA and is used for
259 * aliasing of IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
261 dev_notice(&device->dev,
262 "ib_register_mad_agent: Invalid Mgmt Class 0\n");
264 } else if (is_vendor_class(mad_reg_req->mgmt_class)) {
266 * If class is in "new" vendor range,
267 * ensure supplied OUI is not zero
269 if (!is_vendor_oui(mad_reg_req->oui)) {
270 dev_notice(&device->dev,
271 "ib_register_mad_agent: No OUI specified for class 0x%x\n",
272 mad_reg_req->mgmt_class);
276 /* Make sure class supplied is consistent with RMPP */
277 if (!ib_is_mad_class_rmpp(mad_reg_req->mgmt_class)) {
279 dev_notice(&device->dev,
280 "ib_register_mad_agent: RMPP version for non-RMPP class 0x%x\n",
281 mad_reg_req->mgmt_class);
285 /* Make sure class supplied is consistent with QP type */
286 if (qp_type == IB_QPT_SMI) {
287 if ((mad_reg_req->mgmt_class !=
288 IB_MGMT_CLASS_SUBN_LID_ROUTED) &&
289 (mad_reg_req->mgmt_class !=
290 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) {
291 dev_notice(&device->dev,
292 "ib_register_mad_agent: Invalid SM QP type: class 0x%x\n",
293 mad_reg_req->mgmt_class);
297 if ((mad_reg_req->mgmt_class ==
298 IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
299 (mad_reg_req->mgmt_class ==
300 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) {
301 dev_notice(&device->dev,
302 "ib_register_mad_agent: Invalid GS QP type: class 0x%x\n",
303 mad_reg_req->mgmt_class);
308 /* No registration request supplied */
313 /* Validate device and port */
314 port_priv = ib_get_mad_port(device, port_num);
316 dev_notice(&device->dev, "ib_register_mad_agent: Invalid port\n");
317 ret = ERR_PTR(-ENODEV);
321 /* Verify the QP requested is supported. For example, Ethernet devices
322 * will not have QP0 */
323 if (!port_priv->qp_info[qpn].qp) {
324 dev_notice(&device->dev,
325 "ib_register_mad_agent: QP %d not supported\n", qpn);
326 ret = ERR_PTR(-EPROTONOSUPPORT);
330 /* Allocate structures */
331 mad_agent_priv = kzalloc(sizeof *mad_agent_priv, GFP_KERNEL);
332 if (!mad_agent_priv) {
333 ret = ERR_PTR(-ENOMEM);
337 mad_agent_priv->agent.mr = ib_get_dma_mr(port_priv->qp_info[qpn].qp->pd,
338 IB_ACCESS_LOCAL_WRITE);
339 if (IS_ERR(mad_agent_priv->agent.mr)) {
340 ret = ERR_PTR(-ENOMEM);
345 reg_req = kmemdup(mad_reg_req, sizeof *reg_req, GFP_KERNEL);
347 ret = ERR_PTR(-ENOMEM);
352 /* Now, fill in the various structures */
353 mad_agent_priv->qp_info = &port_priv->qp_info[qpn];
354 mad_agent_priv->reg_req = reg_req;
355 mad_agent_priv->agent.rmpp_version = rmpp_version;
356 mad_agent_priv->agent.device = device;
357 mad_agent_priv->agent.recv_handler = recv_handler;
358 mad_agent_priv->agent.send_handler = send_handler;
359 mad_agent_priv->agent.context = context;
360 mad_agent_priv->agent.qp = port_priv->qp_info[qpn].qp;
361 mad_agent_priv->agent.port_num = port_num;
362 spin_lock_init(&mad_agent_priv->lock);
363 INIT_LIST_HEAD(&mad_agent_priv->send_list);
364 INIT_LIST_HEAD(&mad_agent_priv->wait_list);
365 INIT_LIST_HEAD(&mad_agent_priv->done_list);
366 INIT_LIST_HEAD(&mad_agent_priv->rmpp_list);
367 INIT_DELAYED_WORK(&mad_agent_priv->timed_work, timeout_sends);
368 INIT_LIST_HEAD(&mad_agent_priv->local_list);
369 INIT_WORK(&mad_agent_priv->local_work, local_completions);
370 atomic_set(&mad_agent_priv->refcount, 1);
371 init_completion(&mad_agent_priv->comp);
373 spin_lock_irqsave(&port_priv->reg_lock, flags);
374 mad_agent_priv->agent.hi_tid = ++ib_mad_client_id;
377 * Make sure MAD registration (if supplied)
378 * is non overlapping with any existing ones
381 mgmt_class = convert_mgmt_class(mad_reg_req->mgmt_class);
382 if (!is_vendor_class(mgmt_class)) {
383 class = port_priv->version[mad_reg_req->
384 mgmt_class_version].class;
386 method = class->method_table[mgmt_class];
388 if (method_in_use(&method,
393 ret2 = add_nonoui_reg_req(mad_reg_req, mad_agent_priv,
396 /* "New" vendor class range */
397 vendor = port_priv->version[mad_reg_req->
398 mgmt_class_version].vendor;
400 vclass = vendor_class_index(mgmt_class);
401 vendor_class = vendor->vendor_class[vclass];
403 if (is_vendor_method_in_use(
409 ret2 = add_oui_reg_req(mad_reg_req, mad_agent_priv);
417 /* Add mad agent into port's agent list */
418 list_add_tail(&mad_agent_priv->agent_list, &port_priv->agent_list);
419 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
421 return &mad_agent_priv->agent;
424 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
427 ib_dereg_mr(mad_agent_priv->agent.mr);
429 kfree(mad_agent_priv);
433 EXPORT_SYMBOL(ib_register_mad_agent);
435 static inline int is_snooping_sends(int mad_snoop_flags)
437 return (mad_snoop_flags &
438 (/*IB_MAD_SNOOP_POSTED_SENDS |
439 IB_MAD_SNOOP_RMPP_SENDS |*/
440 IB_MAD_SNOOP_SEND_COMPLETIONS /*|
441 IB_MAD_SNOOP_RMPP_SEND_COMPLETIONS*/));
444 static inline int is_snooping_recvs(int mad_snoop_flags)
446 return (mad_snoop_flags &
447 (IB_MAD_SNOOP_RECVS /*|
448 IB_MAD_SNOOP_RMPP_RECVS*/));
451 static int register_snoop_agent(struct ib_mad_qp_info *qp_info,
452 struct ib_mad_snoop_private *mad_snoop_priv)
454 struct ib_mad_snoop_private **new_snoop_table;
458 spin_lock_irqsave(&qp_info->snoop_lock, flags);
459 /* Check for empty slot in array. */
460 for (i = 0; i < qp_info->snoop_table_size; i++)
461 if (!qp_info->snoop_table[i])
464 if (i == qp_info->snoop_table_size) {
466 new_snoop_table = krealloc(qp_info->snoop_table,
467 sizeof mad_snoop_priv *
468 (qp_info->snoop_table_size + 1),
470 if (!new_snoop_table) {
475 qp_info->snoop_table = new_snoop_table;
476 qp_info->snoop_table_size++;
478 qp_info->snoop_table[i] = mad_snoop_priv;
479 atomic_inc(&qp_info->snoop_count);
481 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
485 struct ib_mad_agent *ib_register_mad_snoop(struct ib_device *device,
487 enum ib_qp_type qp_type,
489 ib_mad_snoop_handler snoop_handler,
490 ib_mad_recv_handler recv_handler,
493 struct ib_mad_port_private *port_priv;
494 struct ib_mad_agent *ret;
495 struct ib_mad_snoop_private *mad_snoop_priv;
498 /* Validate parameters */
499 if ((is_snooping_sends(mad_snoop_flags) && !snoop_handler) ||
500 (is_snooping_recvs(mad_snoop_flags) && !recv_handler)) {
501 ret = ERR_PTR(-EINVAL);
504 qpn = get_spl_qp_index(qp_type);
506 ret = ERR_PTR(-EINVAL);
509 port_priv = ib_get_mad_port(device, port_num);
511 ret = ERR_PTR(-ENODEV);
514 /* Allocate structures */
515 mad_snoop_priv = kzalloc(sizeof *mad_snoop_priv, GFP_KERNEL);
516 if (!mad_snoop_priv) {
517 ret = ERR_PTR(-ENOMEM);
521 /* Now, fill in the various structures */
522 mad_snoop_priv->qp_info = &port_priv->qp_info[qpn];
523 mad_snoop_priv->agent.device = device;
524 mad_snoop_priv->agent.recv_handler = recv_handler;
525 mad_snoop_priv->agent.snoop_handler = snoop_handler;
526 mad_snoop_priv->agent.context = context;
527 mad_snoop_priv->agent.qp = port_priv->qp_info[qpn].qp;
528 mad_snoop_priv->agent.port_num = port_num;
529 mad_snoop_priv->mad_snoop_flags = mad_snoop_flags;
530 init_completion(&mad_snoop_priv->comp);
531 mad_snoop_priv->snoop_index = register_snoop_agent(
532 &port_priv->qp_info[qpn],
534 if (mad_snoop_priv->snoop_index < 0) {
535 ret = ERR_PTR(mad_snoop_priv->snoop_index);
539 atomic_set(&mad_snoop_priv->refcount, 1);
540 return &mad_snoop_priv->agent;
543 kfree(mad_snoop_priv);
547 EXPORT_SYMBOL(ib_register_mad_snoop);
549 static inline void deref_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
551 if (atomic_dec_and_test(&mad_agent_priv->refcount))
552 complete(&mad_agent_priv->comp);
555 static inline void deref_snoop_agent(struct ib_mad_snoop_private *mad_snoop_priv)
557 if (atomic_dec_and_test(&mad_snoop_priv->refcount))
558 complete(&mad_snoop_priv->comp);
561 static void unregister_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
563 struct ib_mad_port_private *port_priv;
566 /* Note that we could still be handling received MADs */
569 * Canceling all sends results in dropping received response
570 * MADs, preventing us from queuing additional work
572 cancel_mads(mad_agent_priv);
573 port_priv = mad_agent_priv->qp_info->port_priv;
574 cancel_delayed_work(&mad_agent_priv->timed_work);
576 spin_lock_irqsave(&port_priv->reg_lock, flags);
577 remove_mad_reg_req(mad_agent_priv);
578 list_del(&mad_agent_priv->agent_list);
579 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
581 flush_workqueue(port_priv->wq);
582 ib_cancel_rmpp_recvs(mad_agent_priv);
584 deref_mad_agent(mad_agent_priv);
585 wait_for_completion(&mad_agent_priv->comp);
587 kfree(mad_agent_priv->reg_req);
588 ib_dereg_mr(mad_agent_priv->agent.mr);
589 kfree(mad_agent_priv);
592 static void unregister_mad_snoop(struct ib_mad_snoop_private *mad_snoop_priv)
594 struct ib_mad_qp_info *qp_info;
597 qp_info = mad_snoop_priv->qp_info;
598 spin_lock_irqsave(&qp_info->snoop_lock, flags);
599 qp_info->snoop_table[mad_snoop_priv->snoop_index] = NULL;
600 atomic_dec(&qp_info->snoop_count);
601 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
603 deref_snoop_agent(mad_snoop_priv);
604 wait_for_completion(&mad_snoop_priv->comp);
606 kfree(mad_snoop_priv);
610 * ib_unregister_mad_agent - Unregisters a client from using MAD services
612 int ib_unregister_mad_agent(struct ib_mad_agent *mad_agent)
614 struct ib_mad_agent_private *mad_agent_priv;
615 struct ib_mad_snoop_private *mad_snoop_priv;
617 /* If the TID is zero, the agent can only snoop. */
618 if (mad_agent->hi_tid) {
619 mad_agent_priv = container_of(mad_agent,
620 struct ib_mad_agent_private,
622 unregister_mad_agent(mad_agent_priv);
624 mad_snoop_priv = container_of(mad_agent,
625 struct ib_mad_snoop_private,
627 unregister_mad_snoop(mad_snoop_priv);
631 EXPORT_SYMBOL(ib_unregister_mad_agent);
633 static void dequeue_mad(struct ib_mad_list_head *mad_list)
635 struct ib_mad_queue *mad_queue;
638 BUG_ON(!mad_list->mad_queue);
639 mad_queue = mad_list->mad_queue;
640 spin_lock_irqsave(&mad_queue->lock, flags);
641 list_del(&mad_list->list);
643 spin_unlock_irqrestore(&mad_queue->lock, flags);
646 static void snoop_send(struct ib_mad_qp_info *qp_info,
647 struct ib_mad_send_buf *send_buf,
648 struct ib_mad_send_wc *mad_send_wc,
651 struct ib_mad_snoop_private *mad_snoop_priv;
655 spin_lock_irqsave(&qp_info->snoop_lock, flags);
656 for (i = 0; i < qp_info->snoop_table_size; i++) {
657 mad_snoop_priv = qp_info->snoop_table[i];
658 if (!mad_snoop_priv ||
659 !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
662 atomic_inc(&mad_snoop_priv->refcount);
663 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
664 mad_snoop_priv->agent.snoop_handler(&mad_snoop_priv->agent,
665 send_buf, mad_send_wc);
666 deref_snoop_agent(mad_snoop_priv);
667 spin_lock_irqsave(&qp_info->snoop_lock, flags);
669 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
672 static void snoop_recv(struct ib_mad_qp_info *qp_info,
673 struct ib_mad_recv_wc *mad_recv_wc,
676 struct ib_mad_snoop_private *mad_snoop_priv;
680 spin_lock_irqsave(&qp_info->snoop_lock, flags);
681 for (i = 0; i < qp_info->snoop_table_size; i++) {
682 mad_snoop_priv = qp_info->snoop_table[i];
683 if (!mad_snoop_priv ||
684 !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
687 atomic_inc(&mad_snoop_priv->refcount);
688 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
689 mad_snoop_priv->agent.recv_handler(&mad_snoop_priv->agent,
691 deref_snoop_agent(mad_snoop_priv);
692 spin_lock_irqsave(&qp_info->snoop_lock, flags);
694 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
697 static void build_smp_wc(struct ib_qp *qp,
698 u64 wr_id, u16 slid, u16 pkey_index, u8 port_num,
701 memset(wc, 0, sizeof *wc);
703 wc->status = IB_WC_SUCCESS;
704 wc->opcode = IB_WC_RECV;
705 wc->pkey_index = pkey_index;
706 wc->byte_len = sizeof(struct ib_mad) + sizeof(struct ib_grh);
711 wc->dlid_path_bits = 0;
712 wc->port_num = port_num;
716 * Return 0 if SMP is to be sent
717 * Return 1 if SMP was consumed locally (whether or not solicited)
718 * Return < 0 if error
720 static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
721 struct ib_mad_send_wr_private *mad_send_wr)
724 struct ib_smp *smp = mad_send_wr->send_buf.mad;
726 struct ib_mad_local_private *local;
727 struct ib_mad_private *mad_priv;
728 struct ib_mad_port_private *port_priv;
729 struct ib_mad_agent_private *recv_mad_agent = NULL;
730 struct ib_device *device = mad_agent_priv->agent.device;
733 struct ib_send_wr *send_wr = &mad_send_wr->send_wr;
735 if (device->node_type == RDMA_NODE_IB_SWITCH &&
736 smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
737 port_num = send_wr->wr.ud.port_num;
739 port_num = mad_agent_priv->agent.port_num;
742 * Directed route handling starts if the initial LID routed part of
743 * a request or the ending LID routed part of a response is empty.
744 * If we are at the start of the LID routed part, don't update the
745 * hop_ptr or hop_cnt. See section 14.2.2, Vol 1 IB spec.
747 if ((ib_get_smp_direction(smp) ? smp->dr_dlid : smp->dr_slid) ==
749 smi_handle_dr_smp_send(smp, device->node_type, port_num) ==
752 dev_err(&device->dev, "Invalid directed route\n");
756 /* Check to post send on QP or process locally */
757 if (smi_check_local_smp(smp, device) == IB_SMI_DISCARD &&
758 smi_check_local_returning_smp(smp, device) == IB_SMI_DISCARD)
761 local = kmalloc(sizeof *local, GFP_ATOMIC);
764 dev_err(&device->dev, "No memory for ib_mad_local_private\n");
767 local->mad_priv = NULL;
768 local->recv_mad_agent = NULL;
769 mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_ATOMIC);
772 dev_err(&device->dev, "No memory for local response MAD\n");
777 build_smp_wc(mad_agent_priv->agent.qp,
778 send_wr->wr_id, be16_to_cpu(smp->dr_slid),
779 send_wr->wr.ud.pkey_index,
780 send_wr->wr.ud.port_num, &mad_wc);
782 /* No GRH for DR SMP */
783 ret = device->process_mad(device, 0, port_num, &mad_wc, NULL,
784 (struct ib_mad *)smp,
785 (struct ib_mad *)&mad_priv->mad);
788 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY:
789 if (ib_response_mad(&mad_priv->mad.mad) &&
790 mad_agent_priv->agent.recv_handler) {
791 local->mad_priv = mad_priv;
792 local->recv_mad_agent = mad_agent_priv;
794 * Reference MAD agent until receive
795 * side of local completion handled
797 atomic_inc(&mad_agent_priv->refcount);
799 kmem_cache_free(ib_mad_cache, mad_priv);
801 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED:
802 kmem_cache_free(ib_mad_cache, mad_priv);
804 case IB_MAD_RESULT_SUCCESS:
805 /* Treat like an incoming receive MAD */
806 port_priv = ib_get_mad_port(mad_agent_priv->agent.device,
807 mad_agent_priv->agent.port_num);
809 memcpy(&mad_priv->mad.mad, smp, sizeof(struct ib_mad));
810 recv_mad_agent = find_mad_agent(port_priv,
813 if (!port_priv || !recv_mad_agent) {
815 * No receiving agent so drop packet and
816 * generate send completion.
818 kmem_cache_free(ib_mad_cache, mad_priv);
821 local->mad_priv = mad_priv;
822 local->recv_mad_agent = recv_mad_agent;
825 kmem_cache_free(ib_mad_cache, mad_priv);
831 local->mad_send_wr = mad_send_wr;
832 /* Reference MAD agent until send side of local completion handled */
833 atomic_inc(&mad_agent_priv->refcount);
834 /* Queue local completion to local list */
835 spin_lock_irqsave(&mad_agent_priv->lock, flags);
836 list_add_tail(&local->completion_list, &mad_agent_priv->local_list);
837 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
838 queue_work(mad_agent_priv->qp_info->port_priv->wq,
839 &mad_agent_priv->local_work);
845 static int get_pad_size(int hdr_len, int data_len)
849 seg_size = sizeof(struct ib_mad) - hdr_len;
850 if (data_len && seg_size) {
851 pad = seg_size - data_len % seg_size;
852 return pad == seg_size ? 0 : pad;
857 static void free_send_rmpp_list(struct ib_mad_send_wr_private *mad_send_wr)
859 struct ib_rmpp_segment *s, *t;
861 list_for_each_entry_safe(s, t, &mad_send_wr->rmpp_list, list) {
867 static int alloc_send_rmpp_list(struct ib_mad_send_wr_private *send_wr,
870 struct ib_mad_send_buf *send_buf = &send_wr->send_buf;
871 struct ib_rmpp_mad *rmpp_mad = send_buf->mad;
872 struct ib_rmpp_segment *seg = NULL;
873 int left, seg_size, pad;
875 send_buf->seg_size = sizeof (struct ib_mad) - send_buf->hdr_len;
876 seg_size = send_buf->seg_size;
879 /* Allocate data segments. */
880 for (left = send_buf->data_len + pad; left > 0; left -= seg_size) {
881 seg = kmalloc(sizeof (*seg) + seg_size, gfp_mask);
883 dev_err(&send_buf->mad_agent->device->dev,
884 "alloc_send_rmpp_segs: RMPP mem alloc failed for len %zd, gfp %#x\n",
885 sizeof (*seg) + seg_size, gfp_mask);
886 free_send_rmpp_list(send_wr);
889 seg->num = ++send_buf->seg_count;
890 list_add_tail(&seg->list, &send_wr->rmpp_list);
893 /* Zero any padding */
895 memset(seg->data + seg_size - pad, 0, pad);
897 rmpp_mad->rmpp_hdr.rmpp_version = send_wr->mad_agent_priv->
899 rmpp_mad->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_DATA;
900 ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
902 send_wr->cur_seg = container_of(send_wr->rmpp_list.next,
903 struct ib_rmpp_segment, list);
904 send_wr->last_ack_seg = send_wr->cur_seg;
908 struct ib_mad_send_buf * ib_create_send_mad(struct ib_mad_agent *mad_agent,
909 u32 remote_qpn, u16 pkey_index,
911 int hdr_len, int data_len,
914 struct ib_mad_agent_private *mad_agent_priv;
915 struct ib_mad_send_wr_private *mad_send_wr;
916 int pad, message_size, ret, size;
919 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
921 pad = get_pad_size(hdr_len, data_len);
922 message_size = hdr_len + data_len + pad;
924 if ((!mad_agent->rmpp_version &&
925 (rmpp_active || message_size > sizeof(struct ib_mad))) ||
926 (!rmpp_active && message_size > sizeof(struct ib_mad)))
927 return ERR_PTR(-EINVAL);
929 size = rmpp_active ? hdr_len : sizeof(struct ib_mad);
930 buf = kzalloc(sizeof *mad_send_wr + size, gfp_mask);
932 return ERR_PTR(-ENOMEM);
934 mad_send_wr = buf + size;
935 INIT_LIST_HEAD(&mad_send_wr->rmpp_list);
936 mad_send_wr->send_buf.mad = buf;
937 mad_send_wr->send_buf.hdr_len = hdr_len;
938 mad_send_wr->send_buf.data_len = data_len;
939 mad_send_wr->pad = pad;
941 mad_send_wr->mad_agent_priv = mad_agent_priv;
942 mad_send_wr->sg_list[0].length = hdr_len;
943 mad_send_wr->sg_list[0].lkey = mad_agent->mr->lkey;
944 mad_send_wr->sg_list[1].length = sizeof(struct ib_mad) - hdr_len;
945 mad_send_wr->sg_list[1].lkey = mad_agent->mr->lkey;
947 mad_send_wr->send_wr.wr_id = (unsigned long) mad_send_wr;
948 mad_send_wr->send_wr.sg_list = mad_send_wr->sg_list;
949 mad_send_wr->send_wr.num_sge = 2;
950 mad_send_wr->send_wr.opcode = IB_WR_SEND;
951 mad_send_wr->send_wr.send_flags = IB_SEND_SIGNALED;
952 mad_send_wr->send_wr.wr.ud.remote_qpn = remote_qpn;
953 mad_send_wr->send_wr.wr.ud.remote_qkey = IB_QP_SET_QKEY;
954 mad_send_wr->send_wr.wr.ud.pkey_index = pkey_index;
957 ret = alloc_send_rmpp_list(mad_send_wr, gfp_mask);
964 mad_send_wr->send_buf.mad_agent = mad_agent;
965 atomic_inc(&mad_agent_priv->refcount);
966 return &mad_send_wr->send_buf;
968 EXPORT_SYMBOL(ib_create_send_mad);
970 int ib_get_mad_data_offset(u8 mgmt_class)
972 if (mgmt_class == IB_MGMT_CLASS_SUBN_ADM)
973 return IB_MGMT_SA_HDR;
974 else if ((mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
975 (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
976 (mgmt_class == IB_MGMT_CLASS_BIS))
977 return IB_MGMT_DEVICE_HDR;
978 else if ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
979 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END))
980 return IB_MGMT_VENDOR_HDR;
982 return IB_MGMT_MAD_HDR;
984 EXPORT_SYMBOL(ib_get_mad_data_offset);
986 int ib_is_mad_class_rmpp(u8 mgmt_class)
988 if ((mgmt_class == IB_MGMT_CLASS_SUBN_ADM) ||
989 (mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
990 (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
991 (mgmt_class == IB_MGMT_CLASS_BIS) ||
992 ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
993 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END)))
997 EXPORT_SYMBOL(ib_is_mad_class_rmpp);
999 void *ib_get_rmpp_segment(struct ib_mad_send_buf *send_buf, int seg_num)
1001 struct ib_mad_send_wr_private *mad_send_wr;
1002 struct list_head *list;
1004 mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
1006 list = &mad_send_wr->cur_seg->list;
1008 if (mad_send_wr->cur_seg->num < seg_num) {
1009 list_for_each_entry(mad_send_wr->cur_seg, list, list)
1010 if (mad_send_wr->cur_seg->num == seg_num)
1012 } else if (mad_send_wr->cur_seg->num > seg_num) {
1013 list_for_each_entry_reverse(mad_send_wr->cur_seg, list, list)
1014 if (mad_send_wr->cur_seg->num == seg_num)
1017 return mad_send_wr->cur_seg->data;
1019 EXPORT_SYMBOL(ib_get_rmpp_segment);
1021 static inline void *ib_get_payload(struct ib_mad_send_wr_private *mad_send_wr)
1023 if (mad_send_wr->send_buf.seg_count)
1024 return ib_get_rmpp_segment(&mad_send_wr->send_buf,
1025 mad_send_wr->seg_num);
1027 return mad_send_wr->send_buf.mad +
1028 mad_send_wr->send_buf.hdr_len;
1031 void ib_free_send_mad(struct ib_mad_send_buf *send_buf)
1033 struct ib_mad_agent_private *mad_agent_priv;
1034 struct ib_mad_send_wr_private *mad_send_wr;
1036 mad_agent_priv = container_of(send_buf->mad_agent,
1037 struct ib_mad_agent_private, agent);
1038 mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
1041 free_send_rmpp_list(mad_send_wr);
1042 kfree(send_buf->mad);
1043 deref_mad_agent(mad_agent_priv);
1045 EXPORT_SYMBOL(ib_free_send_mad);
1047 int ib_send_mad(struct ib_mad_send_wr_private *mad_send_wr)
1049 struct ib_mad_qp_info *qp_info;
1050 struct list_head *list;
1051 struct ib_send_wr *bad_send_wr;
1052 struct ib_mad_agent *mad_agent;
1054 unsigned long flags;
1057 /* Set WR ID to find mad_send_wr upon completion */
1058 qp_info = mad_send_wr->mad_agent_priv->qp_info;
1059 mad_send_wr->send_wr.wr_id = (unsigned long)&mad_send_wr->mad_list;
1060 mad_send_wr->mad_list.mad_queue = &qp_info->send_queue;
1062 mad_agent = mad_send_wr->send_buf.mad_agent;
1063 sge = mad_send_wr->sg_list;
1064 sge[0].addr = ib_dma_map_single(mad_agent->device,
1065 mad_send_wr->send_buf.mad,
1068 if (unlikely(ib_dma_mapping_error(mad_agent->device, sge[0].addr)))
1071 mad_send_wr->header_mapping = sge[0].addr;
1073 sge[1].addr = ib_dma_map_single(mad_agent->device,
1074 ib_get_payload(mad_send_wr),
1077 if (unlikely(ib_dma_mapping_error(mad_agent->device, sge[1].addr))) {
1078 ib_dma_unmap_single(mad_agent->device,
1079 mad_send_wr->header_mapping,
1080 sge[0].length, DMA_TO_DEVICE);
1083 mad_send_wr->payload_mapping = sge[1].addr;
1085 spin_lock_irqsave(&qp_info->send_queue.lock, flags);
1086 if (qp_info->send_queue.count < qp_info->send_queue.max_active) {
1087 ret = ib_post_send(mad_agent->qp, &mad_send_wr->send_wr,
1089 list = &qp_info->send_queue.list;
1092 list = &qp_info->overflow_list;
1096 qp_info->send_queue.count++;
1097 list_add_tail(&mad_send_wr->mad_list.list, list);
1099 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
1101 ib_dma_unmap_single(mad_agent->device,
1102 mad_send_wr->header_mapping,
1103 sge[0].length, DMA_TO_DEVICE);
1104 ib_dma_unmap_single(mad_agent->device,
1105 mad_send_wr->payload_mapping,
1106 sge[1].length, DMA_TO_DEVICE);
1112 * ib_post_send_mad - Posts MAD(s) to the send queue of the QP associated
1113 * with the registered client
1115 int ib_post_send_mad(struct ib_mad_send_buf *send_buf,
1116 struct ib_mad_send_buf **bad_send_buf)
1118 struct ib_mad_agent_private *mad_agent_priv;
1119 struct ib_mad_send_buf *next_send_buf;
1120 struct ib_mad_send_wr_private *mad_send_wr;
1121 unsigned long flags;
1124 /* Walk list of send WRs and post each on send list */
1125 for (; send_buf; send_buf = next_send_buf) {
1127 mad_send_wr = container_of(send_buf,
1128 struct ib_mad_send_wr_private,
1130 mad_agent_priv = mad_send_wr->mad_agent_priv;
1132 if (!send_buf->mad_agent->send_handler ||
1133 (send_buf->timeout_ms &&
1134 !send_buf->mad_agent->recv_handler)) {
1139 if (!ib_is_mad_class_rmpp(((struct ib_mad_hdr *) send_buf->mad)->mgmt_class)) {
1140 if (mad_agent_priv->agent.rmpp_version) {
1147 * Save pointer to next work request to post in case the
1148 * current one completes, and the user modifies the work
1149 * request associated with the completion
1151 next_send_buf = send_buf->next;
1152 mad_send_wr->send_wr.wr.ud.ah = send_buf->ah;
1154 if (((struct ib_mad_hdr *) send_buf->mad)->mgmt_class ==
1155 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
1156 ret = handle_outgoing_dr_smp(mad_agent_priv,
1158 if (ret < 0) /* error */
1160 else if (ret == 1) /* locally consumed */
1164 mad_send_wr->tid = ((struct ib_mad_hdr *) send_buf->mad)->tid;
1165 /* Timeout will be updated after send completes */
1166 mad_send_wr->timeout = msecs_to_jiffies(send_buf->timeout_ms);
1167 mad_send_wr->max_retries = send_buf->retries;
1168 mad_send_wr->retries_left = send_buf->retries;
1169 send_buf->retries = 0;
1170 /* Reference for work request to QP + response */
1171 mad_send_wr->refcount = 1 + (mad_send_wr->timeout > 0);
1172 mad_send_wr->status = IB_WC_SUCCESS;
1174 /* Reference MAD agent until send completes */
1175 atomic_inc(&mad_agent_priv->refcount);
1176 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1177 list_add_tail(&mad_send_wr->agent_list,
1178 &mad_agent_priv->send_list);
1179 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1181 if (mad_agent_priv->agent.rmpp_version) {
1182 ret = ib_send_rmpp_mad(mad_send_wr);
1183 if (ret >= 0 && ret != IB_RMPP_RESULT_CONSUMED)
1184 ret = ib_send_mad(mad_send_wr);
1186 ret = ib_send_mad(mad_send_wr);
1188 /* Fail send request */
1189 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1190 list_del(&mad_send_wr->agent_list);
1191 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1192 atomic_dec(&mad_agent_priv->refcount);
1199 *bad_send_buf = send_buf;
1202 EXPORT_SYMBOL(ib_post_send_mad);
1205 * ib_free_recv_mad - Returns data buffers used to receive
1206 * a MAD to the access layer
1208 void ib_free_recv_mad(struct ib_mad_recv_wc *mad_recv_wc)
1210 struct ib_mad_recv_buf *mad_recv_buf, *temp_recv_buf;
1211 struct ib_mad_private_header *mad_priv_hdr;
1212 struct ib_mad_private *priv;
1213 struct list_head free_list;
1215 INIT_LIST_HEAD(&free_list);
1216 list_splice_init(&mad_recv_wc->rmpp_list, &free_list);
1218 list_for_each_entry_safe(mad_recv_buf, temp_recv_buf,
1220 mad_recv_wc = container_of(mad_recv_buf, struct ib_mad_recv_wc,
1222 mad_priv_hdr = container_of(mad_recv_wc,
1223 struct ib_mad_private_header,
1225 priv = container_of(mad_priv_hdr, struct ib_mad_private,
1227 kmem_cache_free(ib_mad_cache, priv);
1230 EXPORT_SYMBOL(ib_free_recv_mad);
1232 struct ib_mad_agent *ib_redirect_mad_qp(struct ib_qp *qp,
1234 ib_mad_send_handler send_handler,
1235 ib_mad_recv_handler recv_handler,
1238 return ERR_PTR(-EINVAL); /* XXX: for now */
1240 EXPORT_SYMBOL(ib_redirect_mad_qp);
1242 int ib_process_mad_wc(struct ib_mad_agent *mad_agent,
1245 dev_err(&mad_agent->device->dev,
1246 "ib_process_mad_wc() not implemented yet\n");
1249 EXPORT_SYMBOL(ib_process_mad_wc);
1251 static int method_in_use(struct ib_mad_mgmt_method_table **method,
1252 struct ib_mad_reg_req *mad_reg_req)
1256 for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS) {
1257 if ((*method)->agent[i]) {
1258 pr_err("Method %d already in use\n", i);
1265 static int allocate_method_table(struct ib_mad_mgmt_method_table **method)
1267 /* Allocate management method table */
1268 *method = kzalloc(sizeof **method, GFP_ATOMIC);
1270 pr_err("No memory for ib_mad_mgmt_method_table\n");
1278 * Check to see if there are any methods still in use
1280 static int check_method_table(struct ib_mad_mgmt_method_table *method)
1284 for (i = 0; i < IB_MGMT_MAX_METHODS; i++)
1285 if (method->agent[i])
1291 * Check to see if there are any method tables for this class still in use
1293 static int check_class_table(struct ib_mad_mgmt_class_table *class)
1297 for (i = 0; i < MAX_MGMT_CLASS; i++)
1298 if (class->method_table[i])
1303 static int check_vendor_class(struct ib_mad_mgmt_vendor_class *vendor_class)
1307 for (i = 0; i < MAX_MGMT_OUI; i++)
1308 if (vendor_class->method_table[i])
1313 static int find_vendor_oui(struct ib_mad_mgmt_vendor_class *vendor_class,
1318 for (i = 0; i < MAX_MGMT_OUI; i++)
1319 /* Is there matching OUI for this vendor class ? */
1320 if (!memcmp(vendor_class->oui[i], oui, 3))
1326 static int check_vendor_table(struct ib_mad_mgmt_vendor_class_table *vendor)
1330 for (i = 0; i < MAX_MGMT_VENDOR_RANGE2; i++)
1331 if (vendor->vendor_class[i])
1337 static void remove_methods_mad_agent(struct ib_mad_mgmt_method_table *method,
1338 struct ib_mad_agent_private *agent)
1342 /* Remove any methods for this mad agent */
1343 for (i = 0; i < IB_MGMT_MAX_METHODS; i++) {
1344 if (method->agent[i] == agent) {
1345 method->agent[i] = NULL;
1350 static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1351 struct ib_mad_agent_private *agent_priv,
1354 struct ib_mad_port_private *port_priv;
1355 struct ib_mad_mgmt_class_table **class;
1356 struct ib_mad_mgmt_method_table **method;
1359 port_priv = agent_priv->qp_info->port_priv;
1360 class = &port_priv->version[mad_reg_req->mgmt_class_version].class;
1362 /* Allocate management class table for "new" class version */
1363 *class = kzalloc(sizeof **class, GFP_ATOMIC);
1365 dev_err(&agent_priv->agent.device->dev,
1366 "No memory for ib_mad_mgmt_class_table\n");
1371 /* Allocate method table for this management class */
1372 method = &(*class)->method_table[mgmt_class];
1373 if ((ret = allocate_method_table(method)))
1376 method = &(*class)->method_table[mgmt_class];
1378 /* Allocate method table for this management class */
1379 if ((ret = allocate_method_table(method)))
1384 /* Now, make sure methods are not already in use */
1385 if (method_in_use(method, mad_reg_req))
1388 /* Finally, add in methods being registered */
1389 for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS)
1390 (*method)->agent[i] = agent_priv;
1395 /* Remove any methods for this mad agent */
1396 remove_methods_mad_agent(*method, agent_priv);
1397 /* Now, check to see if there are any methods in use */
1398 if (!check_method_table(*method)) {
1399 /* If not, release management method table */
1412 static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1413 struct ib_mad_agent_private *agent_priv)
1415 struct ib_mad_port_private *port_priv;
1416 struct ib_mad_mgmt_vendor_class_table **vendor_table;
1417 struct ib_mad_mgmt_vendor_class_table *vendor = NULL;
1418 struct ib_mad_mgmt_vendor_class *vendor_class = NULL;
1419 struct ib_mad_mgmt_method_table **method;
1420 int i, ret = -ENOMEM;
1423 /* "New" vendor (with OUI) class */
1424 vclass = vendor_class_index(mad_reg_req->mgmt_class);
1425 port_priv = agent_priv->qp_info->port_priv;
1426 vendor_table = &port_priv->version[
1427 mad_reg_req->mgmt_class_version].vendor;
1428 if (!*vendor_table) {
1429 /* Allocate mgmt vendor class table for "new" class version */
1430 vendor = kzalloc(sizeof *vendor, GFP_ATOMIC);
1432 dev_err(&agent_priv->agent.device->dev,
1433 "No memory for ib_mad_mgmt_vendor_class_table\n");
1437 *vendor_table = vendor;
1439 if (!(*vendor_table)->vendor_class[vclass]) {
1440 /* Allocate table for this management vendor class */
1441 vendor_class = kzalloc(sizeof *vendor_class, GFP_ATOMIC);
1442 if (!vendor_class) {
1443 dev_err(&agent_priv->agent.device->dev,
1444 "No memory for ib_mad_mgmt_vendor_class\n");
1448 (*vendor_table)->vendor_class[vclass] = vendor_class;
1450 for (i = 0; i < MAX_MGMT_OUI; i++) {
1451 /* Is there matching OUI for this vendor class ? */
1452 if (!memcmp((*vendor_table)->vendor_class[vclass]->oui[i],
1453 mad_reg_req->oui, 3)) {
1454 method = &(*vendor_table)->vendor_class[
1455 vclass]->method_table[i];
1460 for (i = 0; i < MAX_MGMT_OUI; i++) {
1461 /* OUI slot available ? */
1462 if (!is_vendor_oui((*vendor_table)->vendor_class[
1464 method = &(*vendor_table)->vendor_class[
1465 vclass]->method_table[i];
1467 /* Allocate method table for this OUI */
1468 if ((ret = allocate_method_table(method)))
1470 memcpy((*vendor_table)->vendor_class[vclass]->oui[i],
1471 mad_reg_req->oui, 3);
1475 dev_err(&agent_priv->agent.device->dev, "All OUI slots in use\n");
1479 /* Now, make sure methods are not already in use */
1480 if (method_in_use(method, mad_reg_req))
1483 /* Finally, add in methods being registered */
1484 for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS)
1485 (*method)->agent[i] = agent_priv;
1490 /* Remove any methods for this mad agent */
1491 remove_methods_mad_agent(*method, agent_priv);
1492 /* Now, check to see if there are any methods in use */
1493 if (!check_method_table(*method)) {
1494 /* If not, release management method table */
1501 (*vendor_table)->vendor_class[vclass] = NULL;
1502 kfree(vendor_class);
1506 *vendor_table = NULL;
1513 static void remove_mad_reg_req(struct ib_mad_agent_private *agent_priv)
1515 struct ib_mad_port_private *port_priv;
1516 struct ib_mad_mgmt_class_table *class;
1517 struct ib_mad_mgmt_method_table *method;
1518 struct ib_mad_mgmt_vendor_class_table *vendor;
1519 struct ib_mad_mgmt_vendor_class *vendor_class;
1524 * Was MAD registration request supplied
1525 * with original registration ?
1527 if (!agent_priv->reg_req) {
1531 port_priv = agent_priv->qp_info->port_priv;
1532 mgmt_class = convert_mgmt_class(agent_priv->reg_req->mgmt_class);
1533 class = port_priv->version[
1534 agent_priv->reg_req->mgmt_class_version].class;
1538 method = class->method_table[mgmt_class];
1540 /* Remove any methods for this mad agent */
1541 remove_methods_mad_agent(method, agent_priv);
1542 /* Now, check to see if there are any methods still in use */
1543 if (!check_method_table(method)) {
1544 /* If not, release management method table */
1546 class->method_table[mgmt_class] = NULL;
1547 /* Any management classes left ? */
1548 if (!check_class_table(class)) {
1549 /* If not, release management class table */
1552 agent_priv->reg_req->
1553 mgmt_class_version].class = NULL;
1559 if (!is_vendor_class(mgmt_class))
1562 /* normalize mgmt_class to vendor range 2 */
1563 mgmt_class = vendor_class_index(agent_priv->reg_req->mgmt_class);
1564 vendor = port_priv->version[
1565 agent_priv->reg_req->mgmt_class_version].vendor;
1570 vendor_class = vendor->vendor_class[mgmt_class];
1572 index = find_vendor_oui(vendor_class, agent_priv->reg_req->oui);
1575 method = vendor_class->method_table[index];
1577 /* Remove any methods for this mad agent */
1578 remove_methods_mad_agent(method, agent_priv);
1580 * Now, check to see if there are
1581 * any methods still in use
1583 if (!check_method_table(method)) {
1584 /* If not, release management method table */
1586 vendor_class->method_table[index] = NULL;
1587 memset(vendor_class->oui[index], 0, 3);
1588 /* Any OUIs left ? */
1589 if (!check_vendor_class(vendor_class)) {
1590 /* If not, release vendor class table */
1591 kfree(vendor_class);
1592 vendor->vendor_class[mgmt_class] = NULL;
1593 /* Any other vendor classes left ? */
1594 if (!check_vendor_table(vendor)) {
1597 agent_priv->reg_req->
1598 mgmt_class_version].
1610 static struct ib_mad_agent_private *
1611 find_mad_agent(struct ib_mad_port_private *port_priv,
1614 struct ib_mad_agent_private *mad_agent = NULL;
1615 unsigned long flags;
1617 spin_lock_irqsave(&port_priv->reg_lock, flags);
1618 if (ib_response_mad(mad)) {
1620 struct ib_mad_agent_private *entry;
1623 * Routing is based on high 32 bits of transaction ID
1626 hi_tid = be64_to_cpu(mad->mad_hdr.tid) >> 32;
1627 list_for_each_entry(entry, &port_priv->agent_list, agent_list) {
1628 if (entry->agent.hi_tid == hi_tid) {
1634 struct ib_mad_mgmt_class_table *class;
1635 struct ib_mad_mgmt_method_table *method;
1636 struct ib_mad_mgmt_vendor_class_table *vendor;
1637 struct ib_mad_mgmt_vendor_class *vendor_class;
1638 struct ib_vendor_mad *vendor_mad;
1642 * Routing is based on version, class, and method
1643 * For "newer" vendor MADs, also based on OUI
1645 if (mad->mad_hdr.class_version >= MAX_MGMT_VERSION)
1647 if (!is_vendor_class(mad->mad_hdr.mgmt_class)) {
1648 class = port_priv->version[
1649 mad->mad_hdr.class_version].class;
1652 if (convert_mgmt_class(mad->mad_hdr.mgmt_class) >=
1653 IB_MGMT_MAX_METHODS)
1655 method = class->method_table[convert_mgmt_class(
1656 mad->mad_hdr.mgmt_class)];
1658 mad_agent = method->agent[mad->mad_hdr.method &
1659 ~IB_MGMT_METHOD_RESP];
1661 vendor = port_priv->version[
1662 mad->mad_hdr.class_version].vendor;
1665 vendor_class = vendor->vendor_class[vendor_class_index(
1666 mad->mad_hdr.mgmt_class)];
1669 /* Find matching OUI */
1670 vendor_mad = (struct ib_vendor_mad *)mad;
1671 index = find_vendor_oui(vendor_class, vendor_mad->oui);
1674 method = vendor_class->method_table[index];
1676 mad_agent = method->agent[mad->mad_hdr.method &
1677 ~IB_MGMT_METHOD_RESP];
1683 if (mad_agent->agent.recv_handler)
1684 atomic_inc(&mad_agent->refcount);
1686 dev_notice(&port_priv->device->dev,
1687 "No receive handler for client %p on port %d\n",
1688 &mad_agent->agent, port_priv->port_num);
1693 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
1698 static int validate_mad(struct ib_mad *mad, u32 qp_num)
1702 /* Make sure MAD base version is understood */
1703 if (mad->mad_hdr.base_version != IB_MGMT_BASE_VERSION) {
1704 pr_err("MAD received with unsupported base version %d\n",
1705 mad->mad_hdr.base_version);
1709 /* Filter SMI packets sent to other than QP0 */
1710 if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
1711 (mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) {
1715 /* Filter GSI packets sent to QP0 */
1724 static int is_data_mad(struct ib_mad_agent_private *mad_agent_priv,
1725 struct ib_mad_hdr *mad_hdr)
1727 struct ib_rmpp_mad *rmpp_mad;
1729 rmpp_mad = (struct ib_rmpp_mad *)mad_hdr;
1730 return !mad_agent_priv->agent.rmpp_version ||
1731 !(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
1732 IB_MGMT_RMPP_FLAG_ACTIVE) ||
1733 (rmpp_mad->rmpp_hdr.rmpp_type == IB_MGMT_RMPP_TYPE_DATA);
1736 static inline int rcv_has_same_class(struct ib_mad_send_wr_private *wr,
1737 struct ib_mad_recv_wc *rwc)
1739 return ((struct ib_mad *)(wr->send_buf.mad))->mad_hdr.mgmt_class ==
1740 rwc->recv_buf.mad->mad_hdr.mgmt_class;
1743 static inline int rcv_has_same_gid(struct ib_mad_agent_private *mad_agent_priv,
1744 struct ib_mad_send_wr_private *wr,
1745 struct ib_mad_recv_wc *rwc )
1747 struct ib_ah_attr attr;
1748 u8 send_resp, rcv_resp;
1750 struct ib_device *device = mad_agent_priv->agent.device;
1751 u8 port_num = mad_agent_priv->agent.port_num;
1754 send_resp = ib_response_mad((struct ib_mad *)wr->send_buf.mad);
1755 rcv_resp = ib_response_mad(rwc->recv_buf.mad);
1757 if (send_resp == rcv_resp)
1758 /* both requests, or both responses. GIDs different */
1761 if (ib_query_ah(wr->send_buf.ah, &attr))
1762 /* Assume not equal, to avoid false positives. */
1765 if (!!(attr.ah_flags & IB_AH_GRH) !=
1766 !!(rwc->wc->wc_flags & IB_WC_GRH))
1767 /* one has GID, other does not. Assume different */
1770 if (!send_resp && rcv_resp) {
1771 /* is request/response. */
1772 if (!(attr.ah_flags & IB_AH_GRH)) {
1773 if (ib_get_cached_lmc(device, port_num, &lmc))
1775 return (!lmc || !((attr.src_path_bits ^
1776 rwc->wc->dlid_path_bits) &
1779 if (ib_get_cached_gid(device, port_num,
1780 attr.grh.sgid_index, &sgid))
1782 return !memcmp(sgid.raw, rwc->recv_buf.grh->dgid.raw,
1787 if (!(attr.ah_flags & IB_AH_GRH))
1788 return attr.dlid == rwc->wc->slid;
1790 return !memcmp(attr.grh.dgid.raw, rwc->recv_buf.grh->sgid.raw,
1794 static inline int is_direct(u8 class)
1796 return (class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE);
1799 struct ib_mad_send_wr_private*
1800 ib_find_send_mad(struct ib_mad_agent_private *mad_agent_priv,
1801 struct ib_mad_recv_wc *wc)
1803 struct ib_mad_send_wr_private *wr;
1806 mad = (struct ib_mad *)wc->recv_buf.mad;
1808 list_for_each_entry(wr, &mad_agent_priv->wait_list, agent_list) {
1809 if ((wr->tid == mad->mad_hdr.tid) &&
1810 rcv_has_same_class(wr, wc) &&
1812 * Don't check GID for direct routed MADs.
1813 * These might have permissive LIDs.
1815 (is_direct(wc->recv_buf.mad->mad_hdr.mgmt_class) ||
1816 rcv_has_same_gid(mad_agent_priv, wr, wc)))
1817 return (wr->status == IB_WC_SUCCESS) ? wr : NULL;
1821 * It's possible to receive the response before we've
1822 * been notified that the send has completed
1824 list_for_each_entry(wr, &mad_agent_priv->send_list, agent_list) {
1825 if (is_data_mad(mad_agent_priv, wr->send_buf.mad) &&
1826 wr->tid == mad->mad_hdr.tid &&
1828 rcv_has_same_class(wr, wc) &&
1830 * Don't check GID for direct routed MADs.
1831 * These might have permissive LIDs.
1833 (is_direct(wc->recv_buf.mad->mad_hdr.mgmt_class) ||
1834 rcv_has_same_gid(mad_agent_priv, wr, wc)))
1835 /* Verify request has not been canceled */
1836 return (wr->status == IB_WC_SUCCESS) ? wr : NULL;
1841 void ib_mark_mad_done(struct ib_mad_send_wr_private *mad_send_wr)
1843 mad_send_wr->timeout = 0;
1844 if (mad_send_wr->refcount == 1)
1845 list_move_tail(&mad_send_wr->agent_list,
1846 &mad_send_wr->mad_agent_priv->done_list);
1849 static void ib_mad_complete_recv(struct ib_mad_agent_private *mad_agent_priv,
1850 struct ib_mad_recv_wc *mad_recv_wc)
1852 struct ib_mad_send_wr_private *mad_send_wr;
1853 struct ib_mad_send_wc mad_send_wc;
1854 unsigned long flags;
1856 INIT_LIST_HEAD(&mad_recv_wc->rmpp_list);
1857 list_add(&mad_recv_wc->recv_buf.list, &mad_recv_wc->rmpp_list);
1858 if (mad_agent_priv->agent.rmpp_version) {
1859 mad_recv_wc = ib_process_rmpp_recv_wc(mad_agent_priv,
1862 deref_mad_agent(mad_agent_priv);
1867 /* Complete corresponding request */
1868 if (ib_response_mad(mad_recv_wc->recv_buf.mad)) {
1869 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1870 mad_send_wr = ib_find_send_mad(mad_agent_priv, mad_recv_wc);
1872 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1873 ib_free_recv_mad(mad_recv_wc);
1874 deref_mad_agent(mad_agent_priv);
1877 ib_mark_mad_done(mad_send_wr);
1878 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1880 /* Defined behavior is to complete response before request */
1881 mad_recv_wc->wc->wr_id = (unsigned long) &mad_send_wr->send_buf;
1882 mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent,
1884 atomic_dec(&mad_agent_priv->refcount);
1886 mad_send_wc.status = IB_WC_SUCCESS;
1887 mad_send_wc.vendor_err = 0;
1888 mad_send_wc.send_buf = &mad_send_wr->send_buf;
1889 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
1891 mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent,
1893 deref_mad_agent(mad_agent_priv);
1897 static bool generate_unmatched_resp(struct ib_mad_private *recv,
1898 struct ib_mad_private *response)
1900 if (recv->mad.mad.mad_hdr.method == IB_MGMT_METHOD_GET ||
1901 recv->mad.mad.mad_hdr.method == IB_MGMT_METHOD_SET) {
1902 memcpy(response, recv, sizeof *response);
1903 response->header.recv_wc.wc = &response->header.wc;
1904 response->header.recv_wc.recv_buf.mad = &response->mad.mad;
1905 response->header.recv_wc.recv_buf.grh = &response->grh;
1906 response->mad.mad.mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
1907 response->mad.mad.mad_hdr.status =
1908 cpu_to_be16(IB_MGMT_MAD_STATUS_UNSUPPORTED_METHOD_ATTRIB);
1909 if (recv->mad.mad.mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
1910 response->mad.mad.mad_hdr.status |= IB_SMP_DIRECTION;
1917 static void ib_mad_recv_done_handler(struct ib_mad_port_private *port_priv,
1920 struct ib_mad_qp_info *qp_info;
1921 struct ib_mad_private_header *mad_priv_hdr;
1922 struct ib_mad_private *recv, *response = NULL;
1923 struct ib_mad_list_head *mad_list;
1924 struct ib_mad_agent_private *mad_agent;
1926 int ret = IB_MAD_RESULT_SUCCESS;
1928 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
1929 qp_info = mad_list->mad_queue->qp_info;
1930 dequeue_mad(mad_list);
1932 mad_priv_hdr = container_of(mad_list, struct ib_mad_private_header,
1934 recv = container_of(mad_priv_hdr, struct ib_mad_private, header);
1935 ib_dma_unmap_single(port_priv->device,
1936 recv->header.mapping,
1937 sizeof(struct ib_mad_private) -
1938 sizeof(struct ib_mad_private_header),
1941 /* Setup MAD receive work completion from "normal" work completion */
1942 recv->header.wc = *wc;
1943 recv->header.recv_wc.wc = &recv->header.wc;
1944 recv->header.recv_wc.mad_len = sizeof(struct ib_mad);
1945 recv->header.recv_wc.recv_buf.mad = &recv->mad.mad;
1946 recv->header.recv_wc.recv_buf.grh = &recv->grh;
1948 if (atomic_read(&qp_info->snoop_count))
1949 snoop_recv(qp_info, &recv->header.recv_wc, IB_MAD_SNOOP_RECVS);
1952 if (!validate_mad(&recv->mad.mad, qp_info->qp->qp_num))
1955 response = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL);
1957 dev_err(&port_priv->device->dev,
1958 "ib_mad_recv_done_handler no memory for response buffer\n");
1962 if (port_priv->device->node_type == RDMA_NODE_IB_SWITCH)
1963 port_num = wc->port_num;
1965 port_num = port_priv->port_num;
1967 if (recv->mad.mad.mad_hdr.mgmt_class ==
1968 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
1969 enum smi_forward_action retsmi;
1971 if (smi_handle_dr_smp_recv(&recv->mad.smp,
1972 port_priv->device->node_type,
1974 port_priv->device->phys_port_cnt) ==
1978 retsmi = smi_check_forward_dr_smp(&recv->mad.smp);
1979 if (retsmi == IB_SMI_LOCAL)
1982 if (retsmi == IB_SMI_SEND) { /* don't forward */
1983 if (smi_handle_dr_smp_send(&recv->mad.smp,
1984 port_priv->device->node_type,
1985 port_num) == IB_SMI_DISCARD)
1988 if (smi_check_local_smp(&recv->mad.smp, port_priv->device) == IB_SMI_DISCARD)
1990 } else if (port_priv->device->node_type == RDMA_NODE_IB_SWITCH) {
1991 /* forward case for switches */
1992 memcpy(response, recv, sizeof(*response));
1993 response->header.recv_wc.wc = &response->header.wc;
1994 response->header.recv_wc.recv_buf.mad = &response->mad.mad;
1995 response->header.recv_wc.recv_buf.grh = &response->grh;
1997 agent_send_response(&response->mad.mad,
2000 smi_get_fwd_port(&recv->mad.smp),
2001 qp_info->qp->qp_num);
2008 /* Give driver "right of first refusal" on incoming MAD */
2009 if (port_priv->device->process_mad) {
2010 ret = port_priv->device->process_mad(port_priv->device, 0,
2011 port_priv->port_num,
2014 &response->mad.mad);
2015 if (ret & IB_MAD_RESULT_SUCCESS) {
2016 if (ret & IB_MAD_RESULT_CONSUMED)
2018 if (ret & IB_MAD_RESULT_REPLY) {
2019 agent_send_response(&response->mad.mad,
2023 qp_info->qp->qp_num);
2029 mad_agent = find_mad_agent(port_priv, &recv->mad.mad);
2031 ib_mad_complete_recv(mad_agent, &recv->header.recv_wc);
2033 * recv is freed up in error cases in ib_mad_complete_recv
2034 * or via recv_handler in ib_mad_complete_recv()
2037 } else if ((ret & IB_MAD_RESULT_SUCCESS) &&
2038 generate_unmatched_resp(recv, response)) {
2039 agent_send_response(&response->mad.mad, &recv->grh, wc,
2040 port_priv->device, port_num, qp_info->qp->qp_num);
2044 /* Post another receive request for this QP */
2046 ib_mad_post_receive_mads(qp_info, response);
2048 kmem_cache_free(ib_mad_cache, recv);
2050 ib_mad_post_receive_mads(qp_info, recv);
2053 static void adjust_timeout(struct ib_mad_agent_private *mad_agent_priv)
2055 struct ib_mad_send_wr_private *mad_send_wr;
2056 unsigned long delay;
2058 if (list_empty(&mad_agent_priv->wait_list)) {
2059 cancel_delayed_work(&mad_agent_priv->timed_work);
2061 mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
2062 struct ib_mad_send_wr_private,
2065 if (time_after(mad_agent_priv->timeout,
2066 mad_send_wr->timeout)) {
2067 mad_agent_priv->timeout = mad_send_wr->timeout;
2068 delay = mad_send_wr->timeout - jiffies;
2069 if ((long)delay <= 0)
2071 mod_delayed_work(mad_agent_priv->qp_info->port_priv->wq,
2072 &mad_agent_priv->timed_work, delay);
2077 static void wait_for_response(struct ib_mad_send_wr_private *mad_send_wr)
2079 struct ib_mad_agent_private *mad_agent_priv;
2080 struct ib_mad_send_wr_private *temp_mad_send_wr;
2081 struct list_head *list_item;
2082 unsigned long delay;
2084 mad_agent_priv = mad_send_wr->mad_agent_priv;
2085 list_del(&mad_send_wr->agent_list);
2087 delay = mad_send_wr->timeout;
2088 mad_send_wr->timeout += jiffies;
2091 list_for_each_prev(list_item, &mad_agent_priv->wait_list) {
2092 temp_mad_send_wr = list_entry(list_item,
2093 struct ib_mad_send_wr_private,
2095 if (time_after(mad_send_wr->timeout,
2096 temp_mad_send_wr->timeout))
2101 list_item = &mad_agent_priv->wait_list;
2102 list_add(&mad_send_wr->agent_list, list_item);
2104 /* Reschedule a work item if we have a shorter timeout */
2105 if (mad_agent_priv->wait_list.next == &mad_send_wr->agent_list)
2106 mod_delayed_work(mad_agent_priv->qp_info->port_priv->wq,
2107 &mad_agent_priv->timed_work, delay);
2110 void ib_reset_mad_timeout(struct ib_mad_send_wr_private *mad_send_wr,
2113 mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
2114 wait_for_response(mad_send_wr);
2118 * Process a send work completion
2120 void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr,
2121 struct ib_mad_send_wc *mad_send_wc)
2123 struct ib_mad_agent_private *mad_agent_priv;
2124 unsigned long flags;
2127 mad_agent_priv = mad_send_wr->mad_agent_priv;
2128 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2129 if (mad_agent_priv->agent.rmpp_version) {
2130 ret = ib_process_rmpp_send_wc(mad_send_wr, mad_send_wc);
2131 if (ret == IB_RMPP_RESULT_CONSUMED)
2134 ret = IB_RMPP_RESULT_UNHANDLED;
2136 if (mad_send_wc->status != IB_WC_SUCCESS &&
2137 mad_send_wr->status == IB_WC_SUCCESS) {
2138 mad_send_wr->status = mad_send_wc->status;
2139 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2142 if (--mad_send_wr->refcount > 0) {
2143 if (mad_send_wr->refcount == 1 && mad_send_wr->timeout &&
2144 mad_send_wr->status == IB_WC_SUCCESS) {
2145 wait_for_response(mad_send_wr);
2150 /* Remove send from MAD agent and notify client of completion */
2151 list_del(&mad_send_wr->agent_list);
2152 adjust_timeout(mad_agent_priv);
2153 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2155 if (mad_send_wr->status != IB_WC_SUCCESS )
2156 mad_send_wc->status = mad_send_wr->status;
2157 if (ret == IB_RMPP_RESULT_INTERNAL)
2158 ib_rmpp_send_handler(mad_send_wc);
2160 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2163 /* Release reference on agent taken when sending */
2164 deref_mad_agent(mad_agent_priv);
2167 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2170 static void ib_mad_send_done_handler(struct ib_mad_port_private *port_priv,
2173 struct ib_mad_send_wr_private *mad_send_wr, *queued_send_wr;
2174 struct ib_mad_list_head *mad_list;
2175 struct ib_mad_qp_info *qp_info;
2176 struct ib_mad_queue *send_queue;
2177 struct ib_send_wr *bad_send_wr;
2178 struct ib_mad_send_wc mad_send_wc;
2179 unsigned long flags;
2182 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
2183 mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
2185 send_queue = mad_list->mad_queue;
2186 qp_info = send_queue->qp_info;
2189 ib_dma_unmap_single(mad_send_wr->send_buf.mad_agent->device,
2190 mad_send_wr->header_mapping,
2191 mad_send_wr->sg_list[0].length, DMA_TO_DEVICE);
2192 ib_dma_unmap_single(mad_send_wr->send_buf.mad_agent->device,
2193 mad_send_wr->payload_mapping,
2194 mad_send_wr->sg_list[1].length, DMA_TO_DEVICE);
2195 queued_send_wr = NULL;
2196 spin_lock_irqsave(&send_queue->lock, flags);
2197 list_del(&mad_list->list);
2199 /* Move queued send to the send queue */
2200 if (send_queue->count-- > send_queue->max_active) {
2201 mad_list = container_of(qp_info->overflow_list.next,
2202 struct ib_mad_list_head, list);
2203 queued_send_wr = container_of(mad_list,
2204 struct ib_mad_send_wr_private,
2206 list_move_tail(&mad_list->list, &send_queue->list);
2208 spin_unlock_irqrestore(&send_queue->lock, flags);
2210 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2211 mad_send_wc.status = wc->status;
2212 mad_send_wc.vendor_err = wc->vendor_err;
2213 if (atomic_read(&qp_info->snoop_count))
2214 snoop_send(qp_info, &mad_send_wr->send_buf, &mad_send_wc,
2215 IB_MAD_SNOOP_SEND_COMPLETIONS);
2216 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
2218 if (queued_send_wr) {
2219 ret = ib_post_send(qp_info->qp, &queued_send_wr->send_wr,
2222 dev_err(&port_priv->device->dev,
2223 "ib_post_send failed: %d\n", ret);
2224 mad_send_wr = queued_send_wr;
2225 wc->status = IB_WC_LOC_QP_OP_ERR;
2231 static void mark_sends_for_retry(struct ib_mad_qp_info *qp_info)
2233 struct ib_mad_send_wr_private *mad_send_wr;
2234 struct ib_mad_list_head *mad_list;
2235 unsigned long flags;
2237 spin_lock_irqsave(&qp_info->send_queue.lock, flags);
2238 list_for_each_entry(mad_list, &qp_info->send_queue.list, list) {
2239 mad_send_wr = container_of(mad_list,
2240 struct ib_mad_send_wr_private,
2242 mad_send_wr->retry = 1;
2244 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
2247 static void mad_error_handler(struct ib_mad_port_private *port_priv,
2250 struct ib_mad_list_head *mad_list;
2251 struct ib_mad_qp_info *qp_info;
2252 struct ib_mad_send_wr_private *mad_send_wr;
2255 /* Determine if failure was a send or receive */
2256 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
2257 qp_info = mad_list->mad_queue->qp_info;
2258 if (mad_list->mad_queue == &qp_info->recv_queue)
2260 * Receive errors indicate that the QP has entered the error
2261 * state - error handling/shutdown code will cleanup
2266 * Send errors will transition the QP to SQE - move
2267 * QP to RTS and repost flushed work requests
2269 mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
2271 if (wc->status == IB_WC_WR_FLUSH_ERR) {
2272 if (mad_send_wr->retry) {
2274 struct ib_send_wr *bad_send_wr;
2276 mad_send_wr->retry = 0;
2277 ret = ib_post_send(qp_info->qp, &mad_send_wr->send_wr,
2280 ib_mad_send_done_handler(port_priv, wc);
2282 ib_mad_send_done_handler(port_priv, wc);
2284 struct ib_qp_attr *attr;
2286 /* Transition QP to RTS and fail offending send */
2287 attr = kmalloc(sizeof *attr, GFP_KERNEL);
2289 attr->qp_state = IB_QPS_RTS;
2290 attr->cur_qp_state = IB_QPS_SQE;
2291 ret = ib_modify_qp(qp_info->qp, attr,
2292 IB_QP_STATE | IB_QP_CUR_STATE);
2295 dev_err(&port_priv->device->dev,
2296 "mad_error_handler - ib_modify_qp to RTS : %d\n",
2299 mark_sends_for_retry(qp_info);
2301 ib_mad_send_done_handler(port_priv, wc);
2306 * IB MAD completion callback
2308 static void ib_mad_completion_handler(struct work_struct *work)
2310 struct ib_mad_port_private *port_priv;
2313 port_priv = container_of(work, struct ib_mad_port_private, work);
2314 ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
2316 while (ib_poll_cq(port_priv->cq, 1, &wc) == 1) {
2317 if (wc.status == IB_WC_SUCCESS) {
2318 switch (wc.opcode) {
2320 ib_mad_send_done_handler(port_priv, &wc);
2323 ib_mad_recv_done_handler(port_priv, &wc);
2330 mad_error_handler(port_priv, &wc);
2334 static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv)
2336 unsigned long flags;
2337 struct ib_mad_send_wr_private *mad_send_wr, *temp_mad_send_wr;
2338 struct ib_mad_send_wc mad_send_wc;
2339 struct list_head cancel_list;
2341 INIT_LIST_HEAD(&cancel_list);
2343 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2344 list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
2345 &mad_agent_priv->send_list, agent_list) {
2346 if (mad_send_wr->status == IB_WC_SUCCESS) {
2347 mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
2348 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2352 /* Empty wait list to prevent receives from finding a request */
2353 list_splice_init(&mad_agent_priv->wait_list, &cancel_list);
2354 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2356 /* Report all cancelled requests */
2357 mad_send_wc.status = IB_WC_WR_FLUSH_ERR;
2358 mad_send_wc.vendor_err = 0;
2360 list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
2361 &cancel_list, agent_list) {
2362 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2363 list_del(&mad_send_wr->agent_list);
2364 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2366 atomic_dec(&mad_agent_priv->refcount);
2370 static struct ib_mad_send_wr_private*
2371 find_send_wr(struct ib_mad_agent_private *mad_agent_priv,
2372 struct ib_mad_send_buf *send_buf)
2374 struct ib_mad_send_wr_private *mad_send_wr;
2376 list_for_each_entry(mad_send_wr, &mad_agent_priv->wait_list,
2378 if (&mad_send_wr->send_buf == send_buf)
2382 list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list,
2384 if (is_data_mad(mad_agent_priv, mad_send_wr->send_buf.mad) &&
2385 &mad_send_wr->send_buf == send_buf)
2391 int ib_modify_mad(struct ib_mad_agent *mad_agent,
2392 struct ib_mad_send_buf *send_buf, u32 timeout_ms)
2394 struct ib_mad_agent_private *mad_agent_priv;
2395 struct ib_mad_send_wr_private *mad_send_wr;
2396 unsigned long flags;
2399 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
2401 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2402 mad_send_wr = find_send_wr(mad_agent_priv, send_buf);
2403 if (!mad_send_wr || mad_send_wr->status != IB_WC_SUCCESS) {
2404 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2408 active = (!mad_send_wr->timeout || mad_send_wr->refcount > 1);
2410 mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
2411 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2414 mad_send_wr->send_buf.timeout_ms = timeout_ms;
2416 mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
2418 ib_reset_mad_timeout(mad_send_wr, timeout_ms);
2420 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2423 EXPORT_SYMBOL(ib_modify_mad);
2425 void ib_cancel_mad(struct ib_mad_agent *mad_agent,
2426 struct ib_mad_send_buf *send_buf)
2428 ib_modify_mad(mad_agent, send_buf, 0);
2430 EXPORT_SYMBOL(ib_cancel_mad);
2432 static void local_completions(struct work_struct *work)
2434 struct ib_mad_agent_private *mad_agent_priv;
2435 struct ib_mad_local_private *local;
2436 struct ib_mad_agent_private *recv_mad_agent;
2437 unsigned long flags;
2440 struct ib_mad_send_wc mad_send_wc;
2443 container_of(work, struct ib_mad_agent_private, local_work);
2445 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2446 while (!list_empty(&mad_agent_priv->local_list)) {
2447 local = list_entry(mad_agent_priv->local_list.next,
2448 struct ib_mad_local_private,
2450 list_del(&local->completion_list);
2451 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2453 if (local->mad_priv) {
2454 recv_mad_agent = local->recv_mad_agent;
2455 if (!recv_mad_agent) {
2456 dev_err(&mad_agent_priv->agent.device->dev,
2457 "No receive MAD agent for local completion\n");
2459 goto local_send_completion;
2463 * Defined behavior is to complete response
2466 build_smp_wc(recv_mad_agent->agent.qp,
2467 (unsigned long) local->mad_send_wr,
2468 be16_to_cpu(IB_LID_PERMISSIVE),
2469 0, recv_mad_agent->agent.port_num, &wc);
2471 local->mad_priv->header.recv_wc.wc = &wc;
2472 local->mad_priv->header.recv_wc.mad_len =
2473 sizeof(struct ib_mad);
2474 INIT_LIST_HEAD(&local->mad_priv->header.recv_wc.rmpp_list);
2475 list_add(&local->mad_priv->header.recv_wc.recv_buf.list,
2476 &local->mad_priv->header.recv_wc.rmpp_list);
2477 local->mad_priv->header.recv_wc.recv_buf.grh = NULL;
2478 local->mad_priv->header.recv_wc.recv_buf.mad =
2479 &local->mad_priv->mad.mad;
2480 if (atomic_read(&recv_mad_agent->qp_info->snoop_count))
2481 snoop_recv(recv_mad_agent->qp_info,
2482 &local->mad_priv->header.recv_wc,
2483 IB_MAD_SNOOP_RECVS);
2484 recv_mad_agent->agent.recv_handler(
2485 &recv_mad_agent->agent,
2486 &local->mad_priv->header.recv_wc);
2487 spin_lock_irqsave(&recv_mad_agent->lock, flags);
2488 atomic_dec(&recv_mad_agent->refcount);
2489 spin_unlock_irqrestore(&recv_mad_agent->lock, flags);
2492 local_send_completion:
2494 mad_send_wc.status = IB_WC_SUCCESS;
2495 mad_send_wc.vendor_err = 0;
2496 mad_send_wc.send_buf = &local->mad_send_wr->send_buf;
2497 if (atomic_read(&mad_agent_priv->qp_info->snoop_count))
2498 snoop_send(mad_agent_priv->qp_info,
2499 &local->mad_send_wr->send_buf,
2500 &mad_send_wc, IB_MAD_SNOOP_SEND_COMPLETIONS);
2501 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2504 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2505 atomic_dec(&mad_agent_priv->refcount);
2507 kmem_cache_free(ib_mad_cache, local->mad_priv);
2510 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2513 static int retry_send(struct ib_mad_send_wr_private *mad_send_wr)
2517 if (!mad_send_wr->retries_left)
2520 mad_send_wr->retries_left--;
2521 mad_send_wr->send_buf.retries++;
2523 mad_send_wr->timeout = msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms);
2525 if (mad_send_wr->mad_agent_priv->agent.rmpp_version) {
2526 ret = ib_retry_rmpp(mad_send_wr);
2528 case IB_RMPP_RESULT_UNHANDLED:
2529 ret = ib_send_mad(mad_send_wr);
2531 case IB_RMPP_RESULT_CONSUMED:
2539 ret = ib_send_mad(mad_send_wr);
2542 mad_send_wr->refcount++;
2543 list_add_tail(&mad_send_wr->agent_list,
2544 &mad_send_wr->mad_agent_priv->send_list);
2549 static void timeout_sends(struct work_struct *work)
2551 struct ib_mad_agent_private *mad_agent_priv;
2552 struct ib_mad_send_wr_private *mad_send_wr;
2553 struct ib_mad_send_wc mad_send_wc;
2554 unsigned long flags, delay;
2556 mad_agent_priv = container_of(work, struct ib_mad_agent_private,
2558 mad_send_wc.vendor_err = 0;
2560 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2561 while (!list_empty(&mad_agent_priv->wait_list)) {
2562 mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
2563 struct ib_mad_send_wr_private,
2566 if (time_after(mad_send_wr->timeout, jiffies)) {
2567 delay = mad_send_wr->timeout - jiffies;
2568 if ((long)delay <= 0)
2570 queue_delayed_work(mad_agent_priv->qp_info->
2572 &mad_agent_priv->timed_work, delay);
2576 list_del(&mad_send_wr->agent_list);
2577 if (mad_send_wr->status == IB_WC_SUCCESS &&
2578 !retry_send(mad_send_wr))
2581 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2583 if (mad_send_wr->status == IB_WC_SUCCESS)
2584 mad_send_wc.status = IB_WC_RESP_TIMEOUT_ERR;
2586 mad_send_wc.status = mad_send_wr->status;
2587 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2588 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2591 atomic_dec(&mad_agent_priv->refcount);
2592 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2594 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2597 static void ib_mad_thread_completion_handler(struct ib_cq *cq, void *arg)
2599 struct ib_mad_port_private *port_priv = cq->cq_context;
2600 unsigned long flags;
2602 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2603 if (!list_empty(&port_priv->port_list))
2604 queue_work(port_priv->wq, &port_priv->work);
2605 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2609 * Allocate receive MADs and post receive WRs for them
2611 static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
2612 struct ib_mad_private *mad)
2614 unsigned long flags;
2616 struct ib_mad_private *mad_priv;
2617 struct ib_sge sg_list;
2618 struct ib_recv_wr recv_wr, *bad_recv_wr;
2619 struct ib_mad_queue *recv_queue = &qp_info->recv_queue;
2621 /* Initialize common scatter list fields */
2622 sg_list.length = sizeof *mad_priv - sizeof mad_priv->header;
2623 sg_list.lkey = (*qp_info->port_priv->mr).lkey;
2625 /* Initialize common receive WR fields */
2626 recv_wr.next = NULL;
2627 recv_wr.sg_list = &sg_list;
2628 recv_wr.num_sge = 1;
2631 /* Allocate and map receive buffer */
2636 mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL);
2638 dev_err(&qp_info->port_priv->device->dev,
2639 "No memory for receive buffer\n");
2644 sg_list.addr = ib_dma_map_single(qp_info->port_priv->device,
2647 sizeof mad_priv->header,
2649 if (unlikely(ib_dma_mapping_error(qp_info->port_priv->device,
2654 mad_priv->header.mapping = sg_list.addr;
2655 recv_wr.wr_id = (unsigned long)&mad_priv->header.mad_list;
2656 mad_priv->header.mad_list.mad_queue = recv_queue;
2658 /* Post receive WR */
2659 spin_lock_irqsave(&recv_queue->lock, flags);
2660 post = (++recv_queue->count < recv_queue->max_active);
2661 list_add_tail(&mad_priv->header.mad_list.list, &recv_queue->list);
2662 spin_unlock_irqrestore(&recv_queue->lock, flags);
2663 ret = ib_post_recv(qp_info->qp, &recv_wr, &bad_recv_wr);
2665 spin_lock_irqsave(&recv_queue->lock, flags);
2666 list_del(&mad_priv->header.mad_list.list);
2667 recv_queue->count--;
2668 spin_unlock_irqrestore(&recv_queue->lock, flags);
2669 ib_dma_unmap_single(qp_info->port_priv->device,
2670 mad_priv->header.mapping,
2672 sizeof mad_priv->header,
2674 kmem_cache_free(ib_mad_cache, mad_priv);
2675 dev_err(&qp_info->port_priv->device->dev,
2676 "ib_post_recv failed: %d\n", ret);
2685 * Return all the posted receive MADs
2687 static void cleanup_recv_queue(struct ib_mad_qp_info *qp_info)
2689 struct ib_mad_private_header *mad_priv_hdr;
2690 struct ib_mad_private *recv;
2691 struct ib_mad_list_head *mad_list;
2696 while (!list_empty(&qp_info->recv_queue.list)) {
2698 mad_list = list_entry(qp_info->recv_queue.list.next,
2699 struct ib_mad_list_head, list);
2700 mad_priv_hdr = container_of(mad_list,
2701 struct ib_mad_private_header,
2703 recv = container_of(mad_priv_hdr, struct ib_mad_private,
2706 /* Remove from posted receive MAD list */
2707 list_del(&mad_list->list);
2709 ib_dma_unmap_single(qp_info->port_priv->device,
2710 recv->header.mapping,
2711 sizeof(struct ib_mad_private) -
2712 sizeof(struct ib_mad_private_header),
2714 kmem_cache_free(ib_mad_cache, recv);
2717 qp_info->recv_queue.count = 0;
2723 static int ib_mad_port_start(struct ib_mad_port_private *port_priv)
2726 struct ib_qp_attr *attr;
2730 attr = kmalloc(sizeof *attr, GFP_KERNEL);
2732 dev_err(&port_priv->device->dev,
2733 "Couldn't kmalloc ib_qp_attr\n");
2737 ret = ib_find_pkey(port_priv->device, port_priv->port_num,
2738 IB_DEFAULT_PKEY_FULL, &pkey_index);
2742 for (i = 0; i < IB_MAD_QPS_CORE; i++) {
2743 qp = port_priv->qp_info[i].qp;
2748 * PKey index for QP1 is irrelevant but
2749 * one is needed for the Reset to Init transition
2751 attr->qp_state = IB_QPS_INIT;
2752 attr->pkey_index = pkey_index;
2753 attr->qkey = (qp->qp_num == 0) ? 0 : IB_QP1_QKEY;
2754 ret = ib_modify_qp(qp, attr, IB_QP_STATE |
2755 IB_QP_PKEY_INDEX | IB_QP_QKEY);
2757 dev_err(&port_priv->device->dev,
2758 "Couldn't change QP%d state to INIT: %d\n",
2763 attr->qp_state = IB_QPS_RTR;
2764 ret = ib_modify_qp(qp, attr, IB_QP_STATE);
2766 dev_err(&port_priv->device->dev,
2767 "Couldn't change QP%d state to RTR: %d\n",
2772 attr->qp_state = IB_QPS_RTS;
2773 attr->sq_psn = IB_MAD_SEND_Q_PSN;
2774 ret = ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_SQ_PSN);
2776 dev_err(&port_priv->device->dev,
2777 "Couldn't change QP%d state to RTS: %d\n",
2783 ret = ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
2785 dev_err(&port_priv->device->dev,
2786 "Failed to request completion notification: %d\n",
2791 for (i = 0; i < IB_MAD_QPS_CORE; i++) {
2792 if (!port_priv->qp_info[i].qp)
2795 ret = ib_mad_post_receive_mads(&port_priv->qp_info[i], NULL);
2797 dev_err(&port_priv->device->dev,
2798 "Couldn't post receive WRs\n");
2807 static void qp_event_handler(struct ib_event *event, void *qp_context)
2809 struct ib_mad_qp_info *qp_info = qp_context;
2811 /* It's worse than that! He's dead, Jim! */
2812 dev_err(&qp_info->port_priv->device->dev,
2813 "Fatal error (%d) on MAD QP (%d)\n",
2814 event->event, qp_info->qp->qp_num);
2817 static void init_mad_queue(struct ib_mad_qp_info *qp_info,
2818 struct ib_mad_queue *mad_queue)
2820 mad_queue->qp_info = qp_info;
2821 mad_queue->count = 0;
2822 spin_lock_init(&mad_queue->lock);
2823 INIT_LIST_HEAD(&mad_queue->list);
2826 static void init_mad_qp(struct ib_mad_port_private *port_priv,
2827 struct ib_mad_qp_info *qp_info)
2829 qp_info->port_priv = port_priv;
2830 init_mad_queue(qp_info, &qp_info->send_queue);
2831 init_mad_queue(qp_info, &qp_info->recv_queue);
2832 INIT_LIST_HEAD(&qp_info->overflow_list);
2833 spin_lock_init(&qp_info->snoop_lock);
2834 qp_info->snoop_table = NULL;
2835 qp_info->snoop_table_size = 0;
2836 atomic_set(&qp_info->snoop_count, 0);
2839 static int create_mad_qp(struct ib_mad_qp_info *qp_info,
2840 enum ib_qp_type qp_type)
2842 struct ib_qp_init_attr qp_init_attr;
2845 memset(&qp_init_attr, 0, sizeof qp_init_attr);
2846 qp_init_attr.send_cq = qp_info->port_priv->cq;
2847 qp_init_attr.recv_cq = qp_info->port_priv->cq;
2848 qp_init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
2849 qp_init_attr.cap.max_send_wr = mad_sendq_size;
2850 qp_init_attr.cap.max_recv_wr = mad_recvq_size;
2851 qp_init_attr.cap.max_send_sge = IB_MAD_SEND_REQ_MAX_SG;
2852 qp_init_attr.cap.max_recv_sge = IB_MAD_RECV_REQ_MAX_SG;
2853 qp_init_attr.qp_type = qp_type;
2854 qp_init_attr.port_num = qp_info->port_priv->port_num;
2855 qp_init_attr.qp_context = qp_info;
2856 qp_init_attr.event_handler = qp_event_handler;
2857 qp_info->qp = ib_create_qp(qp_info->port_priv->pd, &qp_init_attr);
2858 if (IS_ERR(qp_info->qp)) {
2859 dev_err(&qp_info->port_priv->device->dev,
2860 "Couldn't create ib_mad QP%d\n",
2861 get_spl_qp_index(qp_type));
2862 ret = PTR_ERR(qp_info->qp);
2865 /* Use minimum queue sizes unless the CQ is resized */
2866 qp_info->send_queue.max_active = mad_sendq_size;
2867 qp_info->recv_queue.max_active = mad_recvq_size;
2874 static void destroy_mad_qp(struct ib_mad_qp_info *qp_info)
2879 ib_destroy_qp(qp_info->qp);
2880 kfree(qp_info->snoop_table);
2885 * Create the QP, PD, MR, and CQ if needed
2887 static int ib_mad_port_open(struct ib_device *device,
2891 struct ib_mad_port_private *port_priv;
2892 unsigned long flags;
2893 char name[sizeof "ib_mad123"];
2896 /* Create new device info */
2897 port_priv = kzalloc(sizeof *port_priv, GFP_KERNEL);
2899 dev_err(&device->dev, "No memory for ib_mad_port_private\n");
2903 port_priv->device = device;
2904 port_priv->port_num = port_num;
2905 spin_lock_init(&port_priv->reg_lock);
2906 INIT_LIST_HEAD(&port_priv->agent_list);
2907 init_mad_qp(port_priv, &port_priv->qp_info[0]);
2908 init_mad_qp(port_priv, &port_priv->qp_info[1]);
2910 cq_size = mad_sendq_size + mad_recvq_size;
2911 has_smi = rdma_port_get_link_layer(device, port_num) == IB_LINK_LAYER_INFINIBAND;
2915 port_priv->cq = ib_create_cq(port_priv->device,
2916 ib_mad_thread_completion_handler,
2917 NULL, port_priv, cq_size, 0);
2918 if (IS_ERR(port_priv->cq)) {
2919 dev_err(&device->dev, "Couldn't create ib_mad CQ\n");
2920 ret = PTR_ERR(port_priv->cq);
2924 port_priv->pd = ib_alloc_pd(device);
2925 if (IS_ERR(port_priv->pd)) {
2926 dev_err(&device->dev, "Couldn't create ib_mad PD\n");
2927 ret = PTR_ERR(port_priv->pd);
2931 port_priv->mr = ib_get_dma_mr(port_priv->pd, IB_ACCESS_LOCAL_WRITE);
2932 if (IS_ERR(port_priv->mr)) {
2933 dev_err(&device->dev, "Couldn't get ib_mad DMA MR\n");
2934 ret = PTR_ERR(port_priv->mr);
2939 ret = create_mad_qp(&port_priv->qp_info[0], IB_QPT_SMI);
2943 ret = create_mad_qp(&port_priv->qp_info[1], IB_QPT_GSI);
2947 snprintf(name, sizeof name, "ib_mad%d", port_num);
2948 port_priv->wq = create_singlethread_workqueue(name);
2949 if (!port_priv->wq) {
2953 INIT_WORK(&port_priv->work, ib_mad_completion_handler);
2955 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2956 list_add_tail(&port_priv->port_list, &ib_mad_port_list);
2957 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2959 ret = ib_mad_port_start(port_priv);
2961 dev_err(&device->dev, "Couldn't start port\n");
2968 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2969 list_del_init(&port_priv->port_list);
2970 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2972 destroy_workqueue(port_priv->wq);
2974 destroy_mad_qp(&port_priv->qp_info[1]);
2976 destroy_mad_qp(&port_priv->qp_info[0]);
2978 ib_dereg_mr(port_priv->mr);
2980 ib_dealloc_pd(port_priv->pd);
2982 ib_destroy_cq(port_priv->cq);
2983 cleanup_recv_queue(&port_priv->qp_info[1]);
2984 cleanup_recv_queue(&port_priv->qp_info[0]);
2993 * If there are no classes using the port, free the port
2994 * resources (CQ, MR, PD, QP) and remove the port's info structure
2996 static int ib_mad_port_close(struct ib_device *device, int port_num)
2998 struct ib_mad_port_private *port_priv;
2999 unsigned long flags;
3001 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
3002 port_priv = __ib_get_mad_port(device, port_num);
3003 if (port_priv == NULL) {
3004 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
3005 dev_err(&device->dev, "Port %d not found\n", port_num);
3008 list_del_init(&port_priv->port_list);
3009 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
3011 destroy_workqueue(port_priv->wq);
3012 destroy_mad_qp(&port_priv->qp_info[1]);
3013 destroy_mad_qp(&port_priv->qp_info[0]);
3014 ib_dereg_mr(port_priv->mr);
3015 ib_dealloc_pd(port_priv->pd);
3016 ib_destroy_cq(port_priv->cq);
3017 cleanup_recv_queue(&port_priv->qp_info[1]);
3018 cleanup_recv_queue(&port_priv->qp_info[0]);
3019 /* XXX: Handle deallocation of MAD registration tables */
3026 static void ib_mad_init_device(struct ib_device *device)
3030 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
3033 if (device->node_type == RDMA_NODE_IB_SWITCH) {
3038 end = device->phys_port_cnt;
3041 for (i = start; i <= end; i++) {
3042 if (ib_mad_port_open(device, i)) {
3043 dev_err(&device->dev, "Couldn't open port %d\n", i);
3046 if (ib_agent_port_open(device, i)) {
3047 dev_err(&device->dev,
3048 "Couldn't open port %d for agents\n", i);
3055 if (ib_mad_port_close(device, i))
3056 dev_err(&device->dev, "Couldn't close port %d\n", i);
3061 while (i >= start) {
3062 if (ib_agent_port_close(device, i))
3063 dev_err(&device->dev,
3064 "Couldn't close port %d for agents\n", i);
3065 if (ib_mad_port_close(device, i))
3066 dev_err(&device->dev, "Couldn't close port %d\n", i);
3071 static void ib_mad_remove_device(struct ib_device *device)
3073 int i, num_ports, cur_port;
3075 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
3078 if (device->node_type == RDMA_NODE_IB_SWITCH) {
3082 num_ports = device->phys_port_cnt;
3085 for (i = 0; i < num_ports; i++, cur_port++) {
3086 if (ib_agent_port_close(device, cur_port))
3087 dev_err(&device->dev,
3088 "Couldn't close port %d for agents\n",
3090 if (ib_mad_port_close(device, cur_port))
3091 dev_err(&device->dev, "Couldn't close port %d\n",
3096 static struct ib_client mad_client = {
3098 .add = ib_mad_init_device,
3099 .remove = ib_mad_remove_device
3102 static int __init ib_mad_init_module(void)
3106 mad_recvq_size = min(mad_recvq_size, IB_MAD_QP_MAX_SIZE);
3107 mad_recvq_size = max(mad_recvq_size, IB_MAD_QP_MIN_SIZE);
3109 mad_sendq_size = min(mad_sendq_size, IB_MAD_QP_MAX_SIZE);
3110 mad_sendq_size = max(mad_sendq_size, IB_MAD_QP_MIN_SIZE);
3112 ib_mad_cache = kmem_cache_create("ib_mad",
3113 sizeof(struct ib_mad_private),
3117 if (!ib_mad_cache) {
3118 pr_err("Couldn't create ib_mad cache\n");
3123 INIT_LIST_HEAD(&ib_mad_port_list);
3125 if (ib_register_client(&mad_client)) {
3126 pr_err("Couldn't register ib_mad client\n");
3134 kmem_cache_destroy(ib_mad_cache);
3139 static void __exit ib_mad_cleanup_module(void)
3141 ib_unregister_client(&mad_client);
3142 kmem_cache_destroy(ib_mad_cache);
3145 module_init(ib_mad_init_module);
3146 module_exit(ib_mad_cleanup_module);