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
36 #include <linux/dma-mapping.h>
37 #include <linux/slab.h>
38 #include <rdma/ib_cache.h>
45 MODULE_LICENSE("Dual BSD/GPL");
46 MODULE_DESCRIPTION("kernel IB MAD API");
47 MODULE_AUTHOR("Hal Rosenstock");
48 MODULE_AUTHOR("Sean Hefty");
50 static int mad_sendq_size = IB_MAD_QP_SEND_SIZE;
51 static int mad_recvq_size = IB_MAD_QP_RECV_SIZE;
53 module_param_named(send_queue_size, mad_sendq_size, int, 0444);
54 MODULE_PARM_DESC(send_queue_size, "Size of send queue in number of work requests");
55 module_param_named(recv_queue_size, mad_recvq_size, int, 0444);
56 MODULE_PARM_DESC(recv_queue_size, "Size of receive queue in number of work requests");
58 static struct kmem_cache *ib_mad_cache;
60 static struct list_head ib_mad_port_list;
61 static u32 ib_mad_client_id = 0;
64 static DEFINE_SPINLOCK(ib_mad_port_list_lock);
66 /* Forward declarations */
67 static int method_in_use(struct ib_mad_mgmt_method_table **method,
68 struct ib_mad_reg_req *mad_reg_req);
69 static void remove_mad_reg_req(struct ib_mad_agent_private *priv);
70 static struct ib_mad_agent_private *find_mad_agent(
71 struct ib_mad_port_private *port_priv,
73 static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
74 struct ib_mad_private *mad);
75 static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv);
76 static void timeout_sends(struct work_struct *work);
77 static void local_completions(struct work_struct *work);
78 static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
79 struct ib_mad_agent_private *agent_priv,
81 static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
82 struct ib_mad_agent_private *agent_priv);
85 * Returns a ib_mad_port_private structure or NULL for a device/port
86 * Assumes ib_mad_port_list_lock is being held
88 static inline struct ib_mad_port_private *
89 __ib_get_mad_port(struct ib_device *device, int port_num)
91 struct ib_mad_port_private *entry;
93 list_for_each_entry(entry, &ib_mad_port_list, port_list) {
94 if (entry->device == device && entry->port_num == port_num)
101 * Wrapper function to return a ib_mad_port_private structure or NULL
104 static inline struct ib_mad_port_private *
105 ib_get_mad_port(struct ib_device *device, int port_num)
107 struct ib_mad_port_private *entry;
110 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
111 entry = __ib_get_mad_port(device, port_num);
112 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
117 static inline u8 convert_mgmt_class(u8 mgmt_class)
119 /* Alias IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE to 0 */
120 return mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE ?
124 static int get_spl_qp_index(enum ib_qp_type qp_type)
137 static int vendor_class_index(u8 mgmt_class)
139 return mgmt_class - IB_MGMT_CLASS_VENDOR_RANGE2_START;
142 static int is_vendor_class(u8 mgmt_class)
144 if ((mgmt_class < IB_MGMT_CLASS_VENDOR_RANGE2_START) ||
145 (mgmt_class > IB_MGMT_CLASS_VENDOR_RANGE2_END))
150 static int is_vendor_oui(char *oui)
152 if (oui[0] || oui[1] || oui[2])
157 static int is_vendor_method_in_use(
158 struct ib_mad_mgmt_vendor_class *vendor_class,
159 struct ib_mad_reg_req *mad_reg_req)
161 struct ib_mad_mgmt_method_table *method;
164 for (i = 0; i < MAX_MGMT_OUI; i++) {
165 if (!memcmp(vendor_class->oui[i], mad_reg_req->oui, 3)) {
166 method = vendor_class->method_table[i];
168 if (method_in_use(&method, mad_reg_req))
178 int ib_response_mad(struct ib_mad *mad)
180 return ((mad->mad_hdr.method & IB_MGMT_METHOD_RESP) ||
181 (mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS) ||
182 ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_BM) &&
183 (mad->mad_hdr.attr_mod & IB_BM_ATTR_MOD_RESP)));
185 EXPORT_SYMBOL(ib_response_mad);
188 * ib_register_mad_agent - Register to send/receive MADs
190 struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
192 enum ib_qp_type qp_type,
193 struct ib_mad_reg_req *mad_reg_req,
195 ib_mad_send_handler send_handler,
196 ib_mad_recv_handler recv_handler,
199 struct ib_mad_port_private *port_priv;
200 struct ib_mad_agent *ret = ERR_PTR(-EINVAL);
201 struct ib_mad_agent_private *mad_agent_priv;
202 struct ib_mad_reg_req *reg_req = NULL;
203 struct ib_mad_mgmt_class_table *class;
204 struct ib_mad_mgmt_vendor_class_table *vendor;
205 struct ib_mad_mgmt_vendor_class *vendor_class;
206 struct ib_mad_mgmt_method_table *method;
209 u8 mgmt_class, vclass;
211 /* Validate parameters */
212 qpn = get_spl_qp_index(qp_type);
216 if (rmpp_version && rmpp_version != IB_MGMT_RMPP_VERSION)
219 /* Validate MAD registration request if supplied */
221 if (mad_reg_req->mgmt_class_version >= MAX_MGMT_VERSION)
225 if (mad_reg_req->mgmt_class >= MAX_MGMT_CLASS) {
227 * IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE is the only
228 * one in this range currently allowed
230 if (mad_reg_req->mgmt_class !=
231 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
233 } else if (mad_reg_req->mgmt_class == 0) {
235 * Class 0 is reserved in IBA and is used for
236 * aliasing of IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
239 } else if (is_vendor_class(mad_reg_req->mgmt_class)) {
241 * If class is in "new" vendor range,
242 * ensure supplied OUI is not zero
244 if (!is_vendor_oui(mad_reg_req->oui))
247 /* Make sure class supplied is consistent with RMPP */
248 if (!ib_is_mad_class_rmpp(mad_reg_req->mgmt_class)) {
252 /* Make sure class supplied is consistent with QP type */
253 if (qp_type == IB_QPT_SMI) {
254 if ((mad_reg_req->mgmt_class !=
255 IB_MGMT_CLASS_SUBN_LID_ROUTED) &&
256 (mad_reg_req->mgmt_class !=
257 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE))
260 if ((mad_reg_req->mgmt_class ==
261 IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
262 (mad_reg_req->mgmt_class ==
263 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE))
267 /* No registration request supplied */
272 /* Validate device and port */
273 port_priv = ib_get_mad_port(device, port_num);
275 ret = ERR_PTR(-ENODEV);
279 /* Allocate structures */
280 mad_agent_priv = kzalloc(sizeof *mad_agent_priv, GFP_KERNEL);
281 if (!mad_agent_priv) {
282 ret = ERR_PTR(-ENOMEM);
286 mad_agent_priv->agent.mr = ib_get_dma_mr(port_priv->qp_info[qpn].qp->pd,
287 IB_ACCESS_LOCAL_WRITE);
288 if (IS_ERR(mad_agent_priv->agent.mr)) {
289 ret = ERR_PTR(-ENOMEM);
294 reg_req = kmemdup(mad_reg_req, sizeof *reg_req, GFP_KERNEL);
296 ret = ERR_PTR(-ENOMEM);
301 /* Now, fill in the various structures */
302 mad_agent_priv->qp_info = &port_priv->qp_info[qpn];
303 mad_agent_priv->reg_req = reg_req;
304 mad_agent_priv->agent.rmpp_version = rmpp_version;
305 mad_agent_priv->agent.device = device;
306 mad_agent_priv->agent.recv_handler = recv_handler;
307 mad_agent_priv->agent.send_handler = send_handler;
308 mad_agent_priv->agent.context = context;
309 mad_agent_priv->agent.qp = port_priv->qp_info[qpn].qp;
310 mad_agent_priv->agent.port_num = port_num;
311 spin_lock_init(&mad_agent_priv->lock);
312 INIT_LIST_HEAD(&mad_agent_priv->send_list);
313 INIT_LIST_HEAD(&mad_agent_priv->wait_list);
314 INIT_LIST_HEAD(&mad_agent_priv->done_list);
315 INIT_LIST_HEAD(&mad_agent_priv->rmpp_list);
316 INIT_DELAYED_WORK(&mad_agent_priv->timed_work, timeout_sends);
317 INIT_LIST_HEAD(&mad_agent_priv->local_list);
318 INIT_WORK(&mad_agent_priv->local_work, local_completions);
319 atomic_set(&mad_agent_priv->refcount, 1);
320 init_completion(&mad_agent_priv->comp);
322 spin_lock_irqsave(&port_priv->reg_lock, flags);
323 mad_agent_priv->agent.hi_tid = ++ib_mad_client_id;
326 * Make sure MAD registration (if supplied)
327 * is non overlapping with any existing ones
330 mgmt_class = convert_mgmt_class(mad_reg_req->mgmt_class);
331 if (!is_vendor_class(mgmt_class)) {
332 class = port_priv->version[mad_reg_req->
333 mgmt_class_version].class;
335 method = class->method_table[mgmt_class];
337 if (method_in_use(&method,
342 ret2 = add_nonoui_reg_req(mad_reg_req, mad_agent_priv,
345 /* "New" vendor class range */
346 vendor = port_priv->version[mad_reg_req->
347 mgmt_class_version].vendor;
349 vclass = vendor_class_index(mgmt_class);
350 vendor_class = vendor->vendor_class[vclass];
352 if (is_vendor_method_in_use(
358 ret2 = add_oui_reg_req(mad_reg_req, mad_agent_priv);
366 /* Add mad agent into port's agent list */
367 list_add_tail(&mad_agent_priv->agent_list, &port_priv->agent_list);
368 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
370 return &mad_agent_priv->agent;
373 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
376 ib_dereg_mr(mad_agent_priv->agent.mr);
378 kfree(mad_agent_priv);
382 EXPORT_SYMBOL(ib_register_mad_agent);
384 static inline int is_snooping_sends(int mad_snoop_flags)
386 return (mad_snoop_flags &
387 (/*IB_MAD_SNOOP_POSTED_SENDS |
388 IB_MAD_SNOOP_RMPP_SENDS |*/
389 IB_MAD_SNOOP_SEND_COMPLETIONS /*|
390 IB_MAD_SNOOP_RMPP_SEND_COMPLETIONS*/));
393 static inline int is_snooping_recvs(int mad_snoop_flags)
395 return (mad_snoop_flags &
396 (IB_MAD_SNOOP_RECVS /*|
397 IB_MAD_SNOOP_RMPP_RECVS*/));
400 static int register_snoop_agent(struct ib_mad_qp_info *qp_info,
401 struct ib_mad_snoop_private *mad_snoop_priv)
403 struct ib_mad_snoop_private **new_snoop_table;
407 spin_lock_irqsave(&qp_info->snoop_lock, flags);
408 /* Check for empty slot in array. */
409 for (i = 0; i < qp_info->snoop_table_size; i++)
410 if (!qp_info->snoop_table[i])
413 if (i == qp_info->snoop_table_size) {
415 new_snoop_table = krealloc(qp_info->snoop_table,
416 sizeof mad_snoop_priv *
417 (qp_info->snoop_table_size + 1),
419 if (!new_snoop_table) {
424 qp_info->snoop_table = new_snoop_table;
425 qp_info->snoop_table_size++;
427 qp_info->snoop_table[i] = mad_snoop_priv;
428 atomic_inc(&qp_info->snoop_count);
430 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
434 struct ib_mad_agent *ib_register_mad_snoop(struct ib_device *device,
436 enum ib_qp_type qp_type,
438 ib_mad_snoop_handler snoop_handler,
439 ib_mad_recv_handler recv_handler,
442 struct ib_mad_port_private *port_priv;
443 struct ib_mad_agent *ret;
444 struct ib_mad_snoop_private *mad_snoop_priv;
447 /* Validate parameters */
448 if ((is_snooping_sends(mad_snoop_flags) && !snoop_handler) ||
449 (is_snooping_recvs(mad_snoop_flags) && !recv_handler)) {
450 ret = ERR_PTR(-EINVAL);
453 qpn = get_spl_qp_index(qp_type);
455 ret = ERR_PTR(-EINVAL);
458 port_priv = ib_get_mad_port(device, port_num);
460 ret = ERR_PTR(-ENODEV);
463 /* Allocate structures */
464 mad_snoop_priv = kzalloc(sizeof *mad_snoop_priv, GFP_KERNEL);
465 if (!mad_snoop_priv) {
466 ret = ERR_PTR(-ENOMEM);
470 /* Now, fill in the various structures */
471 mad_snoop_priv->qp_info = &port_priv->qp_info[qpn];
472 mad_snoop_priv->agent.device = device;
473 mad_snoop_priv->agent.recv_handler = recv_handler;
474 mad_snoop_priv->agent.snoop_handler = snoop_handler;
475 mad_snoop_priv->agent.context = context;
476 mad_snoop_priv->agent.qp = port_priv->qp_info[qpn].qp;
477 mad_snoop_priv->agent.port_num = port_num;
478 mad_snoop_priv->mad_snoop_flags = mad_snoop_flags;
479 init_completion(&mad_snoop_priv->comp);
480 mad_snoop_priv->snoop_index = register_snoop_agent(
481 &port_priv->qp_info[qpn],
483 if (mad_snoop_priv->snoop_index < 0) {
484 ret = ERR_PTR(mad_snoop_priv->snoop_index);
488 atomic_set(&mad_snoop_priv->refcount, 1);
489 return &mad_snoop_priv->agent;
492 kfree(mad_snoop_priv);
496 EXPORT_SYMBOL(ib_register_mad_snoop);
498 static inline void deref_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
500 if (atomic_dec_and_test(&mad_agent_priv->refcount))
501 complete(&mad_agent_priv->comp);
504 static inline void deref_snoop_agent(struct ib_mad_snoop_private *mad_snoop_priv)
506 if (atomic_dec_and_test(&mad_snoop_priv->refcount))
507 complete(&mad_snoop_priv->comp);
510 static void unregister_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
512 struct ib_mad_port_private *port_priv;
515 /* Note that we could still be handling received MADs */
518 * Canceling all sends results in dropping received response
519 * MADs, preventing us from queuing additional work
521 cancel_mads(mad_agent_priv);
522 port_priv = mad_agent_priv->qp_info->port_priv;
523 cancel_delayed_work(&mad_agent_priv->timed_work);
525 spin_lock_irqsave(&port_priv->reg_lock, flags);
526 remove_mad_reg_req(mad_agent_priv);
527 list_del(&mad_agent_priv->agent_list);
528 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
530 flush_workqueue(port_priv->wq);
531 ib_cancel_rmpp_recvs(mad_agent_priv);
533 deref_mad_agent(mad_agent_priv);
534 wait_for_completion(&mad_agent_priv->comp);
536 kfree(mad_agent_priv->reg_req);
537 ib_dereg_mr(mad_agent_priv->agent.mr);
538 kfree(mad_agent_priv);
541 static void unregister_mad_snoop(struct ib_mad_snoop_private *mad_snoop_priv)
543 struct ib_mad_qp_info *qp_info;
546 qp_info = mad_snoop_priv->qp_info;
547 spin_lock_irqsave(&qp_info->snoop_lock, flags);
548 qp_info->snoop_table[mad_snoop_priv->snoop_index] = NULL;
549 atomic_dec(&qp_info->snoop_count);
550 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
552 deref_snoop_agent(mad_snoop_priv);
553 wait_for_completion(&mad_snoop_priv->comp);
555 kfree(mad_snoop_priv);
559 * ib_unregister_mad_agent - Unregisters a client from using MAD services
561 int ib_unregister_mad_agent(struct ib_mad_agent *mad_agent)
563 struct ib_mad_agent_private *mad_agent_priv;
564 struct ib_mad_snoop_private *mad_snoop_priv;
566 /* If the TID is zero, the agent can only snoop. */
567 if (mad_agent->hi_tid) {
568 mad_agent_priv = container_of(mad_agent,
569 struct ib_mad_agent_private,
571 unregister_mad_agent(mad_agent_priv);
573 mad_snoop_priv = container_of(mad_agent,
574 struct ib_mad_snoop_private,
576 unregister_mad_snoop(mad_snoop_priv);
580 EXPORT_SYMBOL(ib_unregister_mad_agent);
582 static void dequeue_mad(struct ib_mad_list_head *mad_list)
584 struct ib_mad_queue *mad_queue;
587 BUG_ON(!mad_list->mad_queue);
588 mad_queue = mad_list->mad_queue;
589 spin_lock_irqsave(&mad_queue->lock, flags);
590 list_del(&mad_list->list);
592 spin_unlock_irqrestore(&mad_queue->lock, flags);
595 static void snoop_send(struct ib_mad_qp_info *qp_info,
596 struct ib_mad_send_buf *send_buf,
597 struct ib_mad_send_wc *mad_send_wc,
600 struct ib_mad_snoop_private *mad_snoop_priv;
604 spin_lock_irqsave(&qp_info->snoop_lock, flags);
605 for (i = 0; i < qp_info->snoop_table_size; i++) {
606 mad_snoop_priv = qp_info->snoop_table[i];
607 if (!mad_snoop_priv ||
608 !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
611 atomic_inc(&mad_snoop_priv->refcount);
612 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
613 mad_snoop_priv->agent.snoop_handler(&mad_snoop_priv->agent,
614 send_buf, mad_send_wc);
615 deref_snoop_agent(mad_snoop_priv);
616 spin_lock_irqsave(&qp_info->snoop_lock, flags);
618 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
621 static void snoop_recv(struct ib_mad_qp_info *qp_info,
622 struct ib_mad_recv_wc *mad_recv_wc,
625 struct ib_mad_snoop_private *mad_snoop_priv;
629 spin_lock_irqsave(&qp_info->snoop_lock, flags);
630 for (i = 0; i < qp_info->snoop_table_size; i++) {
631 mad_snoop_priv = qp_info->snoop_table[i];
632 if (!mad_snoop_priv ||
633 !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
636 atomic_inc(&mad_snoop_priv->refcount);
637 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
638 mad_snoop_priv->agent.recv_handler(&mad_snoop_priv->agent,
640 deref_snoop_agent(mad_snoop_priv);
641 spin_lock_irqsave(&qp_info->snoop_lock, flags);
643 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
646 static void build_smp_wc(struct ib_qp *qp,
647 u64 wr_id, u16 slid, u16 pkey_index, u8 port_num,
650 memset(wc, 0, sizeof *wc);
652 wc->status = IB_WC_SUCCESS;
653 wc->opcode = IB_WC_RECV;
654 wc->pkey_index = pkey_index;
655 wc->byte_len = sizeof(struct ib_mad) + sizeof(struct ib_grh);
660 wc->dlid_path_bits = 0;
661 wc->port_num = port_num;
665 * Return 0 if SMP is to be sent
666 * Return 1 if SMP was consumed locally (whether or not solicited)
667 * Return < 0 if error
669 static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
670 struct ib_mad_send_wr_private *mad_send_wr)
673 struct ib_smp *smp = mad_send_wr->send_buf.mad;
675 struct ib_mad_local_private *local;
676 struct ib_mad_private *mad_priv;
677 struct ib_mad_port_private *port_priv;
678 struct ib_mad_agent_private *recv_mad_agent = NULL;
679 struct ib_device *device = mad_agent_priv->agent.device;
682 struct ib_send_wr *send_wr = &mad_send_wr->send_wr;
684 if (device->node_type == RDMA_NODE_IB_SWITCH &&
685 smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
686 port_num = send_wr->wr.ud.port_num;
688 port_num = mad_agent_priv->agent.port_num;
691 * Directed route handling starts if the initial LID routed part of
692 * a request or the ending LID routed part of a response is empty.
693 * If we are at the start of the LID routed part, don't update the
694 * hop_ptr or hop_cnt. See section 14.2.2, Vol 1 IB spec.
696 if ((ib_get_smp_direction(smp) ? smp->dr_dlid : smp->dr_slid) ==
698 smi_handle_dr_smp_send(smp, device->node_type, port_num) ==
701 printk(KERN_ERR PFX "Invalid directed route\n");
705 /* Check to post send on QP or process locally */
706 if (smi_check_local_smp(smp, device) == IB_SMI_DISCARD &&
707 smi_check_local_returning_smp(smp, device) == IB_SMI_DISCARD)
710 local = kmalloc(sizeof *local, GFP_ATOMIC);
713 printk(KERN_ERR PFX "No memory for ib_mad_local_private\n");
716 local->mad_priv = NULL;
717 local->recv_mad_agent = NULL;
718 mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_ATOMIC);
721 printk(KERN_ERR PFX "No memory for local response MAD\n");
726 build_smp_wc(mad_agent_priv->agent.qp,
727 send_wr->wr_id, be16_to_cpu(smp->dr_slid),
728 send_wr->wr.ud.pkey_index,
729 send_wr->wr.ud.port_num, &mad_wc);
731 /* No GRH for DR SMP */
732 ret = device->process_mad(device, 0, port_num, &mad_wc, NULL,
733 (struct ib_mad *)smp,
734 (struct ib_mad *)&mad_priv->mad);
737 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY:
738 if (ib_response_mad(&mad_priv->mad.mad) &&
739 mad_agent_priv->agent.recv_handler) {
740 local->mad_priv = mad_priv;
741 local->recv_mad_agent = mad_agent_priv;
743 * Reference MAD agent until receive
744 * side of local completion handled
746 atomic_inc(&mad_agent_priv->refcount);
748 kmem_cache_free(ib_mad_cache, mad_priv);
750 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED:
751 kmem_cache_free(ib_mad_cache, mad_priv);
753 case IB_MAD_RESULT_SUCCESS:
754 /* Treat like an incoming receive MAD */
755 port_priv = ib_get_mad_port(mad_agent_priv->agent.device,
756 mad_agent_priv->agent.port_num);
758 memcpy(&mad_priv->mad.mad, smp, sizeof(struct ib_mad));
759 recv_mad_agent = find_mad_agent(port_priv,
762 if (!port_priv || !recv_mad_agent) {
764 * No receiving agent so drop packet and
765 * generate send completion.
767 kmem_cache_free(ib_mad_cache, mad_priv);
770 local->mad_priv = mad_priv;
771 local->recv_mad_agent = recv_mad_agent;
774 kmem_cache_free(ib_mad_cache, mad_priv);
780 local->mad_send_wr = mad_send_wr;
781 /* Reference MAD agent until send side of local completion handled */
782 atomic_inc(&mad_agent_priv->refcount);
783 /* Queue local completion to local list */
784 spin_lock_irqsave(&mad_agent_priv->lock, flags);
785 list_add_tail(&local->completion_list, &mad_agent_priv->local_list);
786 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
787 queue_work(mad_agent_priv->qp_info->port_priv->wq,
788 &mad_agent_priv->local_work);
794 static int get_pad_size(int hdr_len, int data_len)
798 seg_size = sizeof(struct ib_mad) - hdr_len;
799 if (data_len && seg_size) {
800 pad = seg_size - data_len % seg_size;
801 return pad == seg_size ? 0 : pad;
806 static void free_send_rmpp_list(struct ib_mad_send_wr_private *mad_send_wr)
808 struct ib_rmpp_segment *s, *t;
810 list_for_each_entry_safe(s, t, &mad_send_wr->rmpp_list, list) {
816 static int alloc_send_rmpp_list(struct ib_mad_send_wr_private *send_wr,
819 struct ib_mad_send_buf *send_buf = &send_wr->send_buf;
820 struct ib_rmpp_mad *rmpp_mad = send_buf->mad;
821 struct ib_rmpp_segment *seg = NULL;
822 int left, seg_size, pad;
824 send_buf->seg_size = sizeof (struct ib_mad) - send_buf->hdr_len;
825 seg_size = send_buf->seg_size;
828 /* Allocate data segments. */
829 for (left = send_buf->data_len + pad; left > 0; left -= seg_size) {
830 seg = kmalloc(sizeof (*seg) + seg_size, gfp_mask);
832 printk(KERN_ERR "alloc_send_rmpp_segs: RMPP mem "
833 "alloc failed for len %zd, gfp %#x\n",
834 sizeof (*seg) + seg_size, gfp_mask);
835 free_send_rmpp_list(send_wr);
838 seg->num = ++send_buf->seg_count;
839 list_add_tail(&seg->list, &send_wr->rmpp_list);
842 /* Zero any padding */
844 memset(seg->data + seg_size - pad, 0, pad);
846 rmpp_mad->rmpp_hdr.rmpp_version = send_wr->mad_agent_priv->
848 rmpp_mad->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_DATA;
849 ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
851 send_wr->cur_seg = container_of(send_wr->rmpp_list.next,
852 struct ib_rmpp_segment, list);
853 send_wr->last_ack_seg = send_wr->cur_seg;
857 struct ib_mad_send_buf * ib_create_send_mad(struct ib_mad_agent *mad_agent,
858 u32 remote_qpn, u16 pkey_index,
860 int hdr_len, int data_len,
863 struct ib_mad_agent_private *mad_agent_priv;
864 struct ib_mad_send_wr_private *mad_send_wr;
865 int pad, message_size, ret, size;
868 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
870 pad = get_pad_size(hdr_len, data_len);
871 message_size = hdr_len + data_len + pad;
873 if ((!mad_agent->rmpp_version &&
874 (rmpp_active || message_size > sizeof(struct ib_mad))) ||
875 (!rmpp_active && message_size > sizeof(struct ib_mad)))
876 return ERR_PTR(-EINVAL);
878 size = rmpp_active ? hdr_len : sizeof(struct ib_mad);
879 buf = kzalloc(sizeof *mad_send_wr + size, gfp_mask);
881 return ERR_PTR(-ENOMEM);
883 mad_send_wr = buf + size;
884 INIT_LIST_HEAD(&mad_send_wr->rmpp_list);
885 mad_send_wr->send_buf.mad = buf;
886 mad_send_wr->send_buf.hdr_len = hdr_len;
887 mad_send_wr->send_buf.data_len = data_len;
888 mad_send_wr->pad = pad;
890 mad_send_wr->mad_agent_priv = mad_agent_priv;
891 mad_send_wr->sg_list[0].length = hdr_len;
892 mad_send_wr->sg_list[0].lkey = mad_agent->mr->lkey;
893 mad_send_wr->sg_list[1].length = sizeof(struct ib_mad) - hdr_len;
894 mad_send_wr->sg_list[1].lkey = mad_agent->mr->lkey;
896 mad_send_wr->send_wr.wr_id = (unsigned long) mad_send_wr;
897 mad_send_wr->send_wr.sg_list = mad_send_wr->sg_list;
898 mad_send_wr->send_wr.num_sge = 2;
899 mad_send_wr->send_wr.opcode = IB_WR_SEND;
900 mad_send_wr->send_wr.send_flags = IB_SEND_SIGNALED;
901 mad_send_wr->send_wr.wr.ud.remote_qpn = remote_qpn;
902 mad_send_wr->send_wr.wr.ud.remote_qkey = IB_QP_SET_QKEY;
903 mad_send_wr->send_wr.wr.ud.pkey_index = pkey_index;
906 ret = alloc_send_rmpp_list(mad_send_wr, gfp_mask);
913 mad_send_wr->send_buf.mad_agent = mad_agent;
914 atomic_inc(&mad_agent_priv->refcount);
915 return &mad_send_wr->send_buf;
917 EXPORT_SYMBOL(ib_create_send_mad);
919 int ib_get_mad_data_offset(u8 mgmt_class)
921 if (mgmt_class == IB_MGMT_CLASS_SUBN_ADM)
922 return IB_MGMT_SA_HDR;
923 else if ((mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
924 (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
925 (mgmt_class == IB_MGMT_CLASS_BIS))
926 return IB_MGMT_DEVICE_HDR;
927 else if ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
928 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END))
929 return IB_MGMT_VENDOR_HDR;
931 return IB_MGMT_MAD_HDR;
933 EXPORT_SYMBOL(ib_get_mad_data_offset);
935 int ib_is_mad_class_rmpp(u8 mgmt_class)
937 if ((mgmt_class == IB_MGMT_CLASS_SUBN_ADM) ||
938 (mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
939 (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
940 (mgmt_class == IB_MGMT_CLASS_BIS) ||
941 ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
942 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END)))
946 EXPORT_SYMBOL(ib_is_mad_class_rmpp);
948 void *ib_get_rmpp_segment(struct ib_mad_send_buf *send_buf, int seg_num)
950 struct ib_mad_send_wr_private *mad_send_wr;
951 struct list_head *list;
953 mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
955 list = &mad_send_wr->cur_seg->list;
957 if (mad_send_wr->cur_seg->num < seg_num) {
958 list_for_each_entry(mad_send_wr->cur_seg, list, list)
959 if (mad_send_wr->cur_seg->num == seg_num)
961 } else if (mad_send_wr->cur_seg->num > seg_num) {
962 list_for_each_entry_reverse(mad_send_wr->cur_seg, list, list)
963 if (mad_send_wr->cur_seg->num == seg_num)
966 return mad_send_wr->cur_seg->data;
968 EXPORT_SYMBOL(ib_get_rmpp_segment);
970 static inline void *ib_get_payload(struct ib_mad_send_wr_private *mad_send_wr)
972 if (mad_send_wr->send_buf.seg_count)
973 return ib_get_rmpp_segment(&mad_send_wr->send_buf,
974 mad_send_wr->seg_num);
976 return mad_send_wr->send_buf.mad +
977 mad_send_wr->send_buf.hdr_len;
980 void ib_free_send_mad(struct ib_mad_send_buf *send_buf)
982 struct ib_mad_agent_private *mad_agent_priv;
983 struct ib_mad_send_wr_private *mad_send_wr;
985 mad_agent_priv = container_of(send_buf->mad_agent,
986 struct ib_mad_agent_private, agent);
987 mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
990 free_send_rmpp_list(mad_send_wr);
991 kfree(send_buf->mad);
992 deref_mad_agent(mad_agent_priv);
994 EXPORT_SYMBOL(ib_free_send_mad);
996 int ib_send_mad(struct ib_mad_send_wr_private *mad_send_wr)
998 struct ib_mad_qp_info *qp_info;
999 struct list_head *list;
1000 struct ib_send_wr *bad_send_wr;
1001 struct ib_mad_agent *mad_agent;
1003 unsigned long flags;
1006 /* Set WR ID to find mad_send_wr upon completion */
1007 qp_info = mad_send_wr->mad_agent_priv->qp_info;
1008 mad_send_wr->send_wr.wr_id = (unsigned long)&mad_send_wr->mad_list;
1009 mad_send_wr->mad_list.mad_queue = &qp_info->send_queue;
1011 mad_agent = mad_send_wr->send_buf.mad_agent;
1012 sge = mad_send_wr->sg_list;
1013 sge[0].addr = ib_dma_map_single(mad_agent->device,
1014 mad_send_wr->send_buf.mad,
1017 mad_send_wr->header_mapping = sge[0].addr;
1019 sge[1].addr = ib_dma_map_single(mad_agent->device,
1020 ib_get_payload(mad_send_wr),
1023 mad_send_wr->payload_mapping = sge[1].addr;
1025 spin_lock_irqsave(&qp_info->send_queue.lock, flags);
1026 if (qp_info->send_queue.count < qp_info->send_queue.max_active) {
1027 ret = ib_post_send(mad_agent->qp, &mad_send_wr->send_wr,
1029 list = &qp_info->send_queue.list;
1032 list = &qp_info->overflow_list;
1036 qp_info->send_queue.count++;
1037 list_add_tail(&mad_send_wr->mad_list.list, list);
1039 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
1041 ib_dma_unmap_single(mad_agent->device,
1042 mad_send_wr->header_mapping,
1043 sge[0].length, DMA_TO_DEVICE);
1044 ib_dma_unmap_single(mad_agent->device,
1045 mad_send_wr->payload_mapping,
1046 sge[1].length, DMA_TO_DEVICE);
1052 * ib_post_send_mad - Posts MAD(s) to the send queue of the QP associated
1053 * with the registered client
1055 int ib_post_send_mad(struct ib_mad_send_buf *send_buf,
1056 struct ib_mad_send_buf **bad_send_buf)
1058 struct ib_mad_agent_private *mad_agent_priv;
1059 struct ib_mad_send_buf *next_send_buf;
1060 struct ib_mad_send_wr_private *mad_send_wr;
1061 unsigned long flags;
1064 /* Walk list of send WRs and post each on send list */
1065 for (; send_buf; send_buf = next_send_buf) {
1067 mad_send_wr = container_of(send_buf,
1068 struct ib_mad_send_wr_private,
1070 mad_agent_priv = mad_send_wr->mad_agent_priv;
1072 if (!send_buf->mad_agent->send_handler ||
1073 (send_buf->timeout_ms &&
1074 !send_buf->mad_agent->recv_handler)) {
1079 if (!ib_is_mad_class_rmpp(((struct ib_mad_hdr *) send_buf->mad)->mgmt_class)) {
1080 if (mad_agent_priv->agent.rmpp_version) {
1087 * Save pointer to next work request to post in case the
1088 * current one completes, and the user modifies the work
1089 * request associated with the completion
1091 next_send_buf = send_buf->next;
1092 mad_send_wr->send_wr.wr.ud.ah = send_buf->ah;
1094 if (((struct ib_mad_hdr *) send_buf->mad)->mgmt_class ==
1095 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
1096 ret = handle_outgoing_dr_smp(mad_agent_priv,
1098 if (ret < 0) /* error */
1100 else if (ret == 1) /* locally consumed */
1104 mad_send_wr->tid = ((struct ib_mad_hdr *) send_buf->mad)->tid;
1105 /* Timeout will be updated after send completes */
1106 mad_send_wr->timeout = msecs_to_jiffies(send_buf->timeout_ms);
1107 mad_send_wr->max_retries = send_buf->retries;
1108 mad_send_wr->retries_left = send_buf->retries;
1109 send_buf->retries = 0;
1110 /* Reference for work request to QP + response */
1111 mad_send_wr->refcount = 1 + (mad_send_wr->timeout > 0);
1112 mad_send_wr->status = IB_WC_SUCCESS;
1114 /* Reference MAD agent until send completes */
1115 atomic_inc(&mad_agent_priv->refcount);
1116 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1117 list_add_tail(&mad_send_wr->agent_list,
1118 &mad_agent_priv->send_list);
1119 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1121 if (mad_agent_priv->agent.rmpp_version) {
1122 ret = ib_send_rmpp_mad(mad_send_wr);
1123 if (ret >= 0 && ret != IB_RMPP_RESULT_CONSUMED)
1124 ret = ib_send_mad(mad_send_wr);
1126 ret = ib_send_mad(mad_send_wr);
1128 /* Fail send request */
1129 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1130 list_del(&mad_send_wr->agent_list);
1131 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1132 atomic_dec(&mad_agent_priv->refcount);
1139 *bad_send_buf = send_buf;
1142 EXPORT_SYMBOL(ib_post_send_mad);
1145 * ib_free_recv_mad - Returns data buffers used to receive
1146 * a MAD to the access layer
1148 void ib_free_recv_mad(struct ib_mad_recv_wc *mad_recv_wc)
1150 struct ib_mad_recv_buf *mad_recv_buf, *temp_recv_buf;
1151 struct ib_mad_private_header *mad_priv_hdr;
1152 struct ib_mad_private *priv;
1153 struct list_head free_list;
1155 INIT_LIST_HEAD(&free_list);
1156 list_splice_init(&mad_recv_wc->rmpp_list, &free_list);
1158 list_for_each_entry_safe(mad_recv_buf, temp_recv_buf,
1160 mad_recv_wc = container_of(mad_recv_buf, struct ib_mad_recv_wc,
1162 mad_priv_hdr = container_of(mad_recv_wc,
1163 struct ib_mad_private_header,
1165 priv = container_of(mad_priv_hdr, struct ib_mad_private,
1167 kmem_cache_free(ib_mad_cache, priv);
1170 EXPORT_SYMBOL(ib_free_recv_mad);
1172 struct ib_mad_agent *ib_redirect_mad_qp(struct ib_qp *qp,
1174 ib_mad_send_handler send_handler,
1175 ib_mad_recv_handler recv_handler,
1178 return ERR_PTR(-EINVAL); /* XXX: for now */
1180 EXPORT_SYMBOL(ib_redirect_mad_qp);
1182 int ib_process_mad_wc(struct ib_mad_agent *mad_agent,
1185 printk(KERN_ERR PFX "ib_process_mad_wc() not implemented yet\n");
1188 EXPORT_SYMBOL(ib_process_mad_wc);
1190 static int method_in_use(struct ib_mad_mgmt_method_table **method,
1191 struct ib_mad_reg_req *mad_reg_req)
1195 for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS) {
1196 if ((*method)->agent[i]) {
1197 printk(KERN_ERR PFX "Method %d already in use\n", i);
1204 static int allocate_method_table(struct ib_mad_mgmt_method_table **method)
1206 /* Allocate management method table */
1207 *method = kzalloc(sizeof **method, GFP_ATOMIC);
1209 printk(KERN_ERR PFX "No memory for "
1210 "ib_mad_mgmt_method_table\n");
1218 * Check to see if there are any methods still in use
1220 static int check_method_table(struct ib_mad_mgmt_method_table *method)
1224 for (i = 0; i < IB_MGMT_MAX_METHODS; i++)
1225 if (method->agent[i])
1231 * Check to see if there are any method tables for this class still in use
1233 static int check_class_table(struct ib_mad_mgmt_class_table *class)
1237 for (i = 0; i < MAX_MGMT_CLASS; i++)
1238 if (class->method_table[i])
1243 static int check_vendor_class(struct ib_mad_mgmt_vendor_class *vendor_class)
1247 for (i = 0; i < MAX_MGMT_OUI; i++)
1248 if (vendor_class->method_table[i])
1253 static int find_vendor_oui(struct ib_mad_mgmt_vendor_class *vendor_class,
1258 for (i = 0; i < MAX_MGMT_OUI; i++)
1259 /* Is there matching OUI for this vendor class ? */
1260 if (!memcmp(vendor_class->oui[i], oui, 3))
1266 static int check_vendor_table(struct ib_mad_mgmt_vendor_class_table *vendor)
1270 for (i = 0; i < MAX_MGMT_VENDOR_RANGE2; i++)
1271 if (vendor->vendor_class[i])
1277 static void remove_methods_mad_agent(struct ib_mad_mgmt_method_table *method,
1278 struct ib_mad_agent_private *agent)
1282 /* Remove any methods for this mad agent */
1283 for (i = 0; i < IB_MGMT_MAX_METHODS; i++) {
1284 if (method->agent[i] == agent) {
1285 method->agent[i] = NULL;
1290 static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1291 struct ib_mad_agent_private *agent_priv,
1294 struct ib_mad_port_private *port_priv;
1295 struct ib_mad_mgmt_class_table **class;
1296 struct ib_mad_mgmt_method_table **method;
1299 port_priv = agent_priv->qp_info->port_priv;
1300 class = &port_priv->version[mad_reg_req->mgmt_class_version].class;
1302 /* Allocate management class table for "new" class version */
1303 *class = kzalloc(sizeof **class, GFP_ATOMIC);
1305 printk(KERN_ERR PFX "No memory for "
1306 "ib_mad_mgmt_class_table\n");
1311 /* Allocate method table for this management class */
1312 method = &(*class)->method_table[mgmt_class];
1313 if ((ret = allocate_method_table(method)))
1316 method = &(*class)->method_table[mgmt_class];
1318 /* Allocate method table for this management class */
1319 if ((ret = allocate_method_table(method)))
1324 /* Now, make sure methods are not already in use */
1325 if (method_in_use(method, mad_reg_req))
1328 /* Finally, add in methods being registered */
1329 for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS)
1330 (*method)->agent[i] = agent_priv;
1335 /* Remove any methods for this mad agent */
1336 remove_methods_mad_agent(*method, agent_priv);
1337 /* Now, check to see if there are any methods in use */
1338 if (!check_method_table(*method)) {
1339 /* If not, release management method table */
1352 static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1353 struct ib_mad_agent_private *agent_priv)
1355 struct ib_mad_port_private *port_priv;
1356 struct ib_mad_mgmt_vendor_class_table **vendor_table;
1357 struct ib_mad_mgmt_vendor_class_table *vendor = NULL;
1358 struct ib_mad_mgmt_vendor_class *vendor_class = NULL;
1359 struct ib_mad_mgmt_method_table **method;
1360 int i, ret = -ENOMEM;
1363 /* "New" vendor (with OUI) class */
1364 vclass = vendor_class_index(mad_reg_req->mgmt_class);
1365 port_priv = agent_priv->qp_info->port_priv;
1366 vendor_table = &port_priv->version[
1367 mad_reg_req->mgmt_class_version].vendor;
1368 if (!*vendor_table) {
1369 /* Allocate mgmt vendor class table for "new" class version */
1370 vendor = kzalloc(sizeof *vendor, GFP_ATOMIC);
1372 printk(KERN_ERR PFX "No memory for "
1373 "ib_mad_mgmt_vendor_class_table\n");
1377 *vendor_table = vendor;
1379 if (!(*vendor_table)->vendor_class[vclass]) {
1380 /* Allocate table for this management vendor class */
1381 vendor_class = kzalloc(sizeof *vendor_class, GFP_ATOMIC);
1382 if (!vendor_class) {
1383 printk(KERN_ERR PFX "No memory for "
1384 "ib_mad_mgmt_vendor_class\n");
1388 (*vendor_table)->vendor_class[vclass] = vendor_class;
1390 for (i = 0; i < MAX_MGMT_OUI; i++) {
1391 /* Is there matching OUI for this vendor class ? */
1392 if (!memcmp((*vendor_table)->vendor_class[vclass]->oui[i],
1393 mad_reg_req->oui, 3)) {
1394 method = &(*vendor_table)->vendor_class[
1395 vclass]->method_table[i];
1400 for (i = 0; i < MAX_MGMT_OUI; i++) {
1401 /* OUI slot available ? */
1402 if (!is_vendor_oui((*vendor_table)->vendor_class[
1404 method = &(*vendor_table)->vendor_class[
1405 vclass]->method_table[i];
1407 /* Allocate method table for this OUI */
1408 if ((ret = allocate_method_table(method)))
1410 memcpy((*vendor_table)->vendor_class[vclass]->oui[i],
1411 mad_reg_req->oui, 3);
1415 printk(KERN_ERR PFX "All OUI slots in use\n");
1419 /* Now, make sure methods are not already in use */
1420 if (method_in_use(method, mad_reg_req))
1423 /* Finally, add in methods being registered */
1424 for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS)
1425 (*method)->agent[i] = agent_priv;
1430 /* Remove any methods for this mad agent */
1431 remove_methods_mad_agent(*method, agent_priv);
1432 /* Now, check to see if there are any methods in use */
1433 if (!check_method_table(*method)) {
1434 /* If not, release management method table */
1441 (*vendor_table)->vendor_class[vclass] = NULL;
1442 kfree(vendor_class);
1446 *vendor_table = NULL;
1453 static void remove_mad_reg_req(struct ib_mad_agent_private *agent_priv)
1455 struct ib_mad_port_private *port_priv;
1456 struct ib_mad_mgmt_class_table *class;
1457 struct ib_mad_mgmt_method_table *method;
1458 struct ib_mad_mgmt_vendor_class_table *vendor;
1459 struct ib_mad_mgmt_vendor_class *vendor_class;
1464 * Was MAD registration request supplied
1465 * with original registration ?
1467 if (!agent_priv->reg_req) {
1471 port_priv = agent_priv->qp_info->port_priv;
1472 mgmt_class = convert_mgmt_class(agent_priv->reg_req->mgmt_class);
1473 class = port_priv->version[
1474 agent_priv->reg_req->mgmt_class_version].class;
1478 method = class->method_table[mgmt_class];
1480 /* Remove any methods for this mad agent */
1481 remove_methods_mad_agent(method, agent_priv);
1482 /* Now, check to see if there are any methods still in use */
1483 if (!check_method_table(method)) {
1484 /* If not, release management method table */
1486 class->method_table[mgmt_class] = NULL;
1487 /* Any management classes left ? */
1488 if (!check_class_table(class)) {
1489 /* If not, release management class table */
1492 agent_priv->reg_req->
1493 mgmt_class_version].class = NULL;
1499 if (!is_vendor_class(mgmt_class))
1502 /* normalize mgmt_class to vendor range 2 */
1503 mgmt_class = vendor_class_index(agent_priv->reg_req->mgmt_class);
1504 vendor = port_priv->version[
1505 agent_priv->reg_req->mgmt_class_version].vendor;
1510 vendor_class = vendor->vendor_class[mgmt_class];
1512 index = find_vendor_oui(vendor_class, agent_priv->reg_req->oui);
1515 method = vendor_class->method_table[index];
1517 /* Remove any methods for this mad agent */
1518 remove_methods_mad_agent(method, agent_priv);
1520 * Now, check to see if there are
1521 * any methods still in use
1523 if (!check_method_table(method)) {
1524 /* If not, release management method table */
1526 vendor_class->method_table[index] = NULL;
1527 memset(vendor_class->oui[index], 0, 3);
1528 /* Any OUIs left ? */
1529 if (!check_vendor_class(vendor_class)) {
1530 /* If not, release vendor class table */
1531 kfree(vendor_class);
1532 vendor->vendor_class[mgmt_class] = NULL;
1533 /* Any other vendor classes left ? */
1534 if (!check_vendor_table(vendor)) {
1537 agent_priv->reg_req->
1538 mgmt_class_version].
1550 static struct ib_mad_agent_private *
1551 find_mad_agent(struct ib_mad_port_private *port_priv,
1554 struct ib_mad_agent_private *mad_agent = NULL;
1555 unsigned long flags;
1557 spin_lock_irqsave(&port_priv->reg_lock, flags);
1558 if (ib_response_mad(mad)) {
1560 struct ib_mad_agent_private *entry;
1563 * Routing is based on high 32 bits of transaction ID
1566 hi_tid = be64_to_cpu(mad->mad_hdr.tid) >> 32;
1567 list_for_each_entry(entry, &port_priv->agent_list, agent_list) {
1568 if (entry->agent.hi_tid == hi_tid) {
1574 struct ib_mad_mgmt_class_table *class;
1575 struct ib_mad_mgmt_method_table *method;
1576 struct ib_mad_mgmt_vendor_class_table *vendor;
1577 struct ib_mad_mgmt_vendor_class *vendor_class;
1578 struct ib_vendor_mad *vendor_mad;
1582 * Routing is based on version, class, and method
1583 * For "newer" vendor MADs, also based on OUI
1585 if (mad->mad_hdr.class_version >= MAX_MGMT_VERSION)
1587 if (!is_vendor_class(mad->mad_hdr.mgmt_class)) {
1588 class = port_priv->version[
1589 mad->mad_hdr.class_version].class;
1592 method = class->method_table[convert_mgmt_class(
1593 mad->mad_hdr.mgmt_class)];
1595 mad_agent = method->agent[mad->mad_hdr.method &
1596 ~IB_MGMT_METHOD_RESP];
1598 vendor = port_priv->version[
1599 mad->mad_hdr.class_version].vendor;
1602 vendor_class = vendor->vendor_class[vendor_class_index(
1603 mad->mad_hdr.mgmt_class)];
1606 /* Find matching OUI */
1607 vendor_mad = (struct ib_vendor_mad *)mad;
1608 index = find_vendor_oui(vendor_class, vendor_mad->oui);
1611 method = vendor_class->method_table[index];
1613 mad_agent = method->agent[mad->mad_hdr.method &
1614 ~IB_MGMT_METHOD_RESP];
1620 if (mad_agent->agent.recv_handler)
1621 atomic_inc(&mad_agent->refcount);
1623 printk(KERN_NOTICE PFX "No receive handler for client "
1625 &mad_agent->agent, port_priv->port_num);
1630 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
1635 static int validate_mad(struct ib_mad *mad, u32 qp_num)
1639 /* Make sure MAD base version is understood */
1640 if (mad->mad_hdr.base_version != IB_MGMT_BASE_VERSION) {
1641 printk(KERN_ERR PFX "MAD received with unsupported base "
1642 "version %d\n", mad->mad_hdr.base_version);
1646 /* Filter SMI packets sent to other than QP0 */
1647 if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
1648 (mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) {
1652 /* Filter GSI packets sent to QP0 */
1661 static int is_data_mad(struct ib_mad_agent_private *mad_agent_priv,
1662 struct ib_mad_hdr *mad_hdr)
1664 struct ib_rmpp_mad *rmpp_mad;
1666 rmpp_mad = (struct ib_rmpp_mad *)mad_hdr;
1667 return !mad_agent_priv->agent.rmpp_version ||
1668 !(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
1669 IB_MGMT_RMPP_FLAG_ACTIVE) ||
1670 (rmpp_mad->rmpp_hdr.rmpp_type == IB_MGMT_RMPP_TYPE_DATA);
1673 static inline int rcv_has_same_class(struct ib_mad_send_wr_private *wr,
1674 struct ib_mad_recv_wc *rwc)
1676 return ((struct ib_mad *)(wr->send_buf.mad))->mad_hdr.mgmt_class ==
1677 rwc->recv_buf.mad->mad_hdr.mgmt_class;
1680 static inline int rcv_has_same_gid(struct ib_mad_agent_private *mad_agent_priv,
1681 struct ib_mad_send_wr_private *wr,
1682 struct ib_mad_recv_wc *rwc )
1684 struct ib_ah_attr attr;
1685 u8 send_resp, rcv_resp;
1687 struct ib_device *device = mad_agent_priv->agent.device;
1688 u8 port_num = mad_agent_priv->agent.port_num;
1691 send_resp = ib_response_mad((struct ib_mad *)wr->send_buf.mad);
1692 rcv_resp = ib_response_mad(rwc->recv_buf.mad);
1694 if (send_resp == rcv_resp)
1695 /* both requests, or both responses. GIDs different */
1698 if (ib_query_ah(wr->send_buf.ah, &attr))
1699 /* Assume not equal, to avoid false positives. */
1702 if (!!(attr.ah_flags & IB_AH_GRH) !=
1703 !!(rwc->wc->wc_flags & IB_WC_GRH))
1704 /* one has GID, other does not. Assume different */
1707 if (!send_resp && rcv_resp) {
1708 /* is request/response. */
1709 if (!(attr.ah_flags & IB_AH_GRH)) {
1710 if (ib_get_cached_lmc(device, port_num, &lmc))
1712 return (!lmc || !((attr.src_path_bits ^
1713 rwc->wc->dlid_path_bits) &
1716 if (ib_get_cached_gid(device, port_num,
1717 attr.grh.sgid_index, &sgid))
1719 return !memcmp(sgid.raw, rwc->recv_buf.grh->dgid.raw,
1724 if (!(attr.ah_flags & IB_AH_GRH))
1725 return attr.dlid == rwc->wc->slid;
1727 return !memcmp(attr.grh.dgid.raw, rwc->recv_buf.grh->sgid.raw,
1731 static inline int is_direct(u8 class)
1733 return (class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE);
1736 struct ib_mad_send_wr_private*
1737 ib_find_send_mad(struct ib_mad_agent_private *mad_agent_priv,
1738 struct ib_mad_recv_wc *wc)
1740 struct ib_mad_send_wr_private *wr;
1743 mad = (struct ib_mad *)wc->recv_buf.mad;
1745 list_for_each_entry(wr, &mad_agent_priv->wait_list, agent_list) {
1746 if ((wr->tid == mad->mad_hdr.tid) &&
1747 rcv_has_same_class(wr, wc) &&
1749 * Don't check GID for direct routed MADs.
1750 * These might have permissive LIDs.
1752 (is_direct(wc->recv_buf.mad->mad_hdr.mgmt_class) ||
1753 rcv_has_same_gid(mad_agent_priv, wr, wc)))
1754 return (wr->status == IB_WC_SUCCESS) ? wr : NULL;
1758 * It's possible to receive the response before we've
1759 * been notified that the send has completed
1761 list_for_each_entry(wr, &mad_agent_priv->send_list, agent_list) {
1762 if (is_data_mad(mad_agent_priv, wr->send_buf.mad) &&
1763 wr->tid == mad->mad_hdr.tid &&
1765 rcv_has_same_class(wr, wc) &&
1767 * Don't check GID for direct routed MADs.
1768 * These might have permissive LIDs.
1770 (is_direct(wc->recv_buf.mad->mad_hdr.mgmt_class) ||
1771 rcv_has_same_gid(mad_agent_priv, wr, wc)))
1772 /* Verify request has not been canceled */
1773 return (wr->status == IB_WC_SUCCESS) ? wr : NULL;
1778 void ib_mark_mad_done(struct ib_mad_send_wr_private *mad_send_wr)
1780 mad_send_wr->timeout = 0;
1781 if (mad_send_wr->refcount == 1)
1782 list_move_tail(&mad_send_wr->agent_list,
1783 &mad_send_wr->mad_agent_priv->done_list);
1786 static void ib_mad_complete_recv(struct ib_mad_agent_private *mad_agent_priv,
1787 struct ib_mad_recv_wc *mad_recv_wc)
1789 struct ib_mad_send_wr_private *mad_send_wr;
1790 struct ib_mad_send_wc mad_send_wc;
1791 unsigned long flags;
1793 INIT_LIST_HEAD(&mad_recv_wc->rmpp_list);
1794 list_add(&mad_recv_wc->recv_buf.list, &mad_recv_wc->rmpp_list);
1795 if (mad_agent_priv->agent.rmpp_version) {
1796 mad_recv_wc = ib_process_rmpp_recv_wc(mad_agent_priv,
1799 deref_mad_agent(mad_agent_priv);
1804 /* Complete corresponding request */
1805 if (ib_response_mad(mad_recv_wc->recv_buf.mad)) {
1806 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1807 mad_send_wr = ib_find_send_mad(mad_agent_priv, mad_recv_wc);
1809 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1810 ib_free_recv_mad(mad_recv_wc);
1811 deref_mad_agent(mad_agent_priv);
1814 ib_mark_mad_done(mad_send_wr);
1815 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1817 /* Defined behavior is to complete response before request */
1818 mad_recv_wc->wc->wr_id = (unsigned long) &mad_send_wr->send_buf;
1819 mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent,
1821 atomic_dec(&mad_agent_priv->refcount);
1823 mad_send_wc.status = IB_WC_SUCCESS;
1824 mad_send_wc.vendor_err = 0;
1825 mad_send_wc.send_buf = &mad_send_wr->send_buf;
1826 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
1828 mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent,
1830 deref_mad_agent(mad_agent_priv);
1834 static void ib_mad_recv_done_handler(struct ib_mad_port_private *port_priv,
1837 struct ib_mad_qp_info *qp_info;
1838 struct ib_mad_private_header *mad_priv_hdr;
1839 struct ib_mad_private *recv, *response = NULL;
1840 struct ib_mad_list_head *mad_list;
1841 struct ib_mad_agent_private *mad_agent;
1844 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
1845 qp_info = mad_list->mad_queue->qp_info;
1846 dequeue_mad(mad_list);
1848 mad_priv_hdr = container_of(mad_list, struct ib_mad_private_header,
1850 recv = container_of(mad_priv_hdr, struct ib_mad_private, header);
1851 ib_dma_unmap_single(port_priv->device,
1852 recv->header.mapping,
1853 sizeof(struct ib_mad_private) -
1854 sizeof(struct ib_mad_private_header),
1857 /* Setup MAD receive work completion from "normal" work completion */
1858 recv->header.wc = *wc;
1859 recv->header.recv_wc.wc = &recv->header.wc;
1860 recv->header.recv_wc.mad_len = sizeof(struct ib_mad);
1861 recv->header.recv_wc.recv_buf.mad = &recv->mad.mad;
1862 recv->header.recv_wc.recv_buf.grh = &recv->grh;
1864 if (atomic_read(&qp_info->snoop_count))
1865 snoop_recv(qp_info, &recv->header.recv_wc, IB_MAD_SNOOP_RECVS);
1868 if (!validate_mad(&recv->mad.mad, qp_info->qp->qp_num))
1871 response = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL);
1873 printk(KERN_ERR PFX "ib_mad_recv_done_handler no memory "
1874 "for response buffer\n");
1878 if (port_priv->device->node_type == RDMA_NODE_IB_SWITCH)
1879 port_num = wc->port_num;
1881 port_num = port_priv->port_num;
1883 if (recv->mad.mad.mad_hdr.mgmt_class ==
1884 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
1885 enum smi_forward_action retsmi;
1887 if (smi_handle_dr_smp_recv(&recv->mad.smp,
1888 port_priv->device->node_type,
1890 port_priv->device->phys_port_cnt) ==
1894 retsmi = smi_check_forward_dr_smp(&recv->mad.smp);
1895 if (retsmi == IB_SMI_LOCAL)
1898 if (retsmi == IB_SMI_SEND) { /* don't forward */
1899 if (smi_handle_dr_smp_send(&recv->mad.smp,
1900 port_priv->device->node_type,
1901 port_num) == IB_SMI_DISCARD)
1904 if (smi_check_local_smp(&recv->mad.smp, port_priv->device) == IB_SMI_DISCARD)
1906 } else if (port_priv->device->node_type == RDMA_NODE_IB_SWITCH) {
1907 /* forward case for switches */
1908 memcpy(response, recv, sizeof(*response));
1909 response->header.recv_wc.wc = &response->header.wc;
1910 response->header.recv_wc.recv_buf.mad = &response->mad.mad;
1911 response->header.recv_wc.recv_buf.grh = &response->grh;
1913 agent_send_response(&response->mad.mad,
1916 smi_get_fwd_port(&recv->mad.smp),
1917 qp_info->qp->qp_num);
1924 /* Give driver "right of first refusal" on incoming MAD */
1925 if (port_priv->device->process_mad) {
1928 ret = port_priv->device->process_mad(port_priv->device, 0,
1929 port_priv->port_num,
1932 &response->mad.mad);
1933 if (ret & IB_MAD_RESULT_SUCCESS) {
1934 if (ret & IB_MAD_RESULT_CONSUMED)
1936 if (ret & IB_MAD_RESULT_REPLY) {
1937 agent_send_response(&response->mad.mad,
1941 qp_info->qp->qp_num);
1947 mad_agent = find_mad_agent(port_priv, &recv->mad.mad);
1949 ib_mad_complete_recv(mad_agent, &recv->header.recv_wc);
1951 * recv is freed up in error cases in ib_mad_complete_recv
1952 * or via recv_handler in ib_mad_complete_recv()
1958 /* Post another receive request for this QP */
1960 ib_mad_post_receive_mads(qp_info, response);
1962 kmem_cache_free(ib_mad_cache, recv);
1964 ib_mad_post_receive_mads(qp_info, recv);
1967 static void adjust_timeout(struct ib_mad_agent_private *mad_agent_priv)
1969 struct ib_mad_send_wr_private *mad_send_wr;
1970 unsigned long delay;
1972 if (list_empty(&mad_agent_priv->wait_list)) {
1973 __cancel_delayed_work(&mad_agent_priv->timed_work);
1975 mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
1976 struct ib_mad_send_wr_private,
1979 if (time_after(mad_agent_priv->timeout,
1980 mad_send_wr->timeout)) {
1981 mad_agent_priv->timeout = mad_send_wr->timeout;
1982 __cancel_delayed_work(&mad_agent_priv->timed_work);
1983 delay = mad_send_wr->timeout - jiffies;
1984 if ((long)delay <= 0)
1986 queue_delayed_work(mad_agent_priv->qp_info->
1988 &mad_agent_priv->timed_work, delay);
1993 static void wait_for_response(struct ib_mad_send_wr_private *mad_send_wr)
1995 struct ib_mad_agent_private *mad_agent_priv;
1996 struct ib_mad_send_wr_private *temp_mad_send_wr;
1997 struct list_head *list_item;
1998 unsigned long delay;
2000 mad_agent_priv = mad_send_wr->mad_agent_priv;
2001 list_del(&mad_send_wr->agent_list);
2003 delay = mad_send_wr->timeout;
2004 mad_send_wr->timeout += jiffies;
2007 list_for_each_prev(list_item, &mad_agent_priv->wait_list) {
2008 temp_mad_send_wr = list_entry(list_item,
2009 struct ib_mad_send_wr_private,
2011 if (time_after(mad_send_wr->timeout,
2012 temp_mad_send_wr->timeout))
2017 list_item = &mad_agent_priv->wait_list;
2018 list_add(&mad_send_wr->agent_list, list_item);
2020 /* Reschedule a work item if we have a shorter timeout */
2021 if (mad_agent_priv->wait_list.next == &mad_send_wr->agent_list) {
2022 __cancel_delayed_work(&mad_agent_priv->timed_work);
2023 queue_delayed_work(mad_agent_priv->qp_info->port_priv->wq,
2024 &mad_agent_priv->timed_work, delay);
2028 void ib_reset_mad_timeout(struct ib_mad_send_wr_private *mad_send_wr,
2031 mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
2032 wait_for_response(mad_send_wr);
2036 * Process a send work completion
2038 void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr,
2039 struct ib_mad_send_wc *mad_send_wc)
2041 struct ib_mad_agent_private *mad_agent_priv;
2042 unsigned long flags;
2045 mad_agent_priv = mad_send_wr->mad_agent_priv;
2046 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2047 if (mad_agent_priv->agent.rmpp_version) {
2048 ret = ib_process_rmpp_send_wc(mad_send_wr, mad_send_wc);
2049 if (ret == IB_RMPP_RESULT_CONSUMED)
2052 ret = IB_RMPP_RESULT_UNHANDLED;
2054 if (mad_send_wc->status != IB_WC_SUCCESS &&
2055 mad_send_wr->status == IB_WC_SUCCESS) {
2056 mad_send_wr->status = mad_send_wc->status;
2057 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2060 if (--mad_send_wr->refcount > 0) {
2061 if (mad_send_wr->refcount == 1 && mad_send_wr->timeout &&
2062 mad_send_wr->status == IB_WC_SUCCESS) {
2063 wait_for_response(mad_send_wr);
2068 /* Remove send from MAD agent and notify client of completion */
2069 list_del(&mad_send_wr->agent_list);
2070 adjust_timeout(mad_agent_priv);
2071 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2073 if (mad_send_wr->status != IB_WC_SUCCESS )
2074 mad_send_wc->status = mad_send_wr->status;
2075 if (ret == IB_RMPP_RESULT_INTERNAL)
2076 ib_rmpp_send_handler(mad_send_wc);
2078 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2081 /* Release reference on agent taken when sending */
2082 deref_mad_agent(mad_agent_priv);
2085 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2088 static void ib_mad_send_done_handler(struct ib_mad_port_private *port_priv,
2091 struct ib_mad_send_wr_private *mad_send_wr, *queued_send_wr;
2092 struct ib_mad_list_head *mad_list;
2093 struct ib_mad_qp_info *qp_info;
2094 struct ib_mad_queue *send_queue;
2095 struct ib_send_wr *bad_send_wr;
2096 struct ib_mad_send_wc mad_send_wc;
2097 unsigned long flags;
2100 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
2101 mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
2103 send_queue = mad_list->mad_queue;
2104 qp_info = send_queue->qp_info;
2107 ib_dma_unmap_single(mad_send_wr->send_buf.mad_agent->device,
2108 mad_send_wr->header_mapping,
2109 mad_send_wr->sg_list[0].length, DMA_TO_DEVICE);
2110 ib_dma_unmap_single(mad_send_wr->send_buf.mad_agent->device,
2111 mad_send_wr->payload_mapping,
2112 mad_send_wr->sg_list[1].length, DMA_TO_DEVICE);
2113 queued_send_wr = NULL;
2114 spin_lock_irqsave(&send_queue->lock, flags);
2115 list_del(&mad_list->list);
2117 /* Move queued send to the send queue */
2118 if (send_queue->count-- > send_queue->max_active) {
2119 mad_list = container_of(qp_info->overflow_list.next,
2120 struct ib_mad_list_head, list);
2121 queued_send_wr = container_of(mad_list,
2122 struct ib_mad_send_wr_private,
2124 list_move_tail(&mad_list->list, &send_queue->list);
2126 spin_unlock_irqrestore(&send_queue->lock, flags);
2128 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2129 mad_send_wc.status = wc->status;
2130 mad_send_wc.vendor_err = wc->vendor_err;
2131 if (atomic_read(&qp_info->snoop_count))
2132 snoop_send(qp_info, &mad_send_wr->send_buf, &mad_send_wc,
2133 IB_MAD_SNOOP_SEND_COMPLETIONS);
2134 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
2136 if (queued_send_wr) {
2137 ret = ib_post_send(qp_info->qp, &queued_send_wr->send_wr,
2140 printk(KERN_ERR PFX "ib_post_send failed: %d\n", ret);
2141 mad_send_wr = queued_send_wr;
2142 wc->status = IB_WC_LOC_QP_OP_ERR;
2148 static void mark_sends_for_retry(struct ib_mad_qp_info *qp_info)
2150 struct ib_mad_send_wr_private *mad_send_wr;
2151 struct ib_mad_list_head *mad_list;
2152 unsigned long flags;
2154 spin_lock_irqsave(&qp_info->send_queue.lock, flags);
2155 list_for_each_entry(mad_list, &qp_info->send_queue.list, list) {
2156 mad_send_wr = container_of(mad_list,
2157 struct ib_mad_send_wr_private,
2159 mad_send_wr->retry = 1;
2161 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
2164 static void mad_error_handler(struct ib_mad_port_private *port_priv,
2167 struct ib_mad_list_head *mad_list;
2168 struct ib_mad_qp_info *qp_info;
2169 struct ib_mad_send_wr_private *mad_send_wr;
2172 /* Determine if failure was a send or receive */
2173 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
2174 qp_info = mad_list->mad_queue->qp_info;
2175 if (mad_list->mad_queue == &qp_info->recv_queue)
2177 * Receive errors indicate that the QP has entered the error
2178 * state - error handling/shutdown code will cleanup
2183 * Send errors will transition the QP to SQE - move
2184 * QP to RTS and repost flushed work requests
2186 mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
2188 if (wc->status == IB_WC_WR_FLUSH_ERR) {
2189 if (mad_send_wr->retry) {
2191 struct ib_send_wr *bad_send_wr;
2193 mad_send_wr->retry = 0;
2194 ret = ib_post_send(qp_info->qp, &mad_send_wr->send_wr,
2197 ib_mad_send_done_handler(port_priv, wc);
2199 ib_mad_send_done_handler(port_priv, wc);
2201 struct ib_qp_attr *attr;
2203 /* Transition QP to RTS and fail offending send */
2204 attr = kmalloc(sizeof *attr, GFP_KERNEL);
2206 attr->qp_state = IB_QPS_RTS;
2207 attr->cur_qp_state = IB_QPS_SQE;
2208 ret = ib_modify_qp(qp_info->qp, attr,
2209 IB_QP_STATE | IB_QP_CUR_STATE);
2212 printk(KERN_ERR PFX "mad_error_handler - "
2213 "ib_modify_qp to RTS : %d\n", ret);
2215 mark_sends_for_retry(qp_info);
2217 ib_mad_send_done_handler(port_priv, wc);
2222 * IB MAD completion callback
2224 static void ib_mad_completion_handler(struct work_struct *work)
2226 struct ib_mad_port_private *port_priv;
2229 port_priv = container_of(work, struct ib_mad_port_private, work);
2230 ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
2232 while (ib_poll_cq(port_priv->cq, 1, &wc) == 1) {
2233 if (wc.status == IB_WC_SUCCESS) {
2234 switch (wc.opcode) {
2236 ib_mad_send_done_handler(port_priv, &wc);
2239 ib_mad_recv_done_handler(port_priv, &wc);
2246 mad_error_handler(port_priv, &wc);
2250 static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv)
2252 unsigned long flags;
2253 struct ib_mad_send_wr_private *mad_send_wr, *temp_mad_send_wr;
2254 struct ib_mad_send_wc mad_send_wc;
2255 struct list_head cancel_list;
2257 INIT_LIST_HEAD(&cancel_list);
2259 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2260 list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
2261 &mad_agent_priv->send_list, agent_list) {
2262 if (mad_send_wr->status == IB_WC_SUCCESS) {
2263 mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
2264 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2268 /* Empty wait list to prevent receives from finding a request */
2269 list_splice_init(&mad_agent_priv->wait_list, &cancel_list);
2270 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2272 /* Report all cancelled requests */
2273 mad_send_wc.status = IB_WC_WR_FLUSH_ERR;
2274 mad_send_wc.vendor_err = 0;
2276 list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
2277 &cancel_list, agent_list) {
2278 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2279 list_del(&mad_send_wr->agent_list);
2280 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2282 atomic_dec(&mad_agent_priv->refcount);
2286 static struct ib_mad_send_wr_private*
2287 find_send_wr(struct ib_mad_agent_private *mad_agent_priv,
2288 struct ib_mad_send_buf *send_buf)
2290 struct ib_mad_send_wr_private *mad_send_wr;
2292 list_for_each_entry(mad_send_wr, &mad_agent_priv->wait_list,
2294 if (&mad_send_wr->send_buf == send_buf)
2298 list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list,
2300 if (is_data_mad(mad_agent_priv, mad_send_wr->send_buf.mad) &&
2301 &mad_send_wr->send_buf == send_buf)
2307 int ib_modify_mad(struct ib_mad_agent *mad_agent,
2308 struct ib_mad_send_buf *send_buf, u32 timeout_ms)
2310 struct ib_mad_agent_private *mad_agent_priv;
2311 struct ib_mad_send_wr_private *mad_send_wr;
2312 unsigned long flags;
2315 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
2317 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2318 mad_send_wr = find_send_wr(mad_agent_priv, send_buf);
2319 if (!mad_send_wr || mad_send_wr->status != IB_WC_SUCCESS) {
2320 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2324 active = (!mad_send_wr->timeout || mad_send_wr->refcount > 1);
2326 mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
2327 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2330 mad_send_wr->send_buf.timeout_ms = timeout_ms;
2332 mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
2334 ib_reset_mad_timeout(mad_send_wr, timeout_ms);
2336 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2339 EXPORT_SYMBOL(ib_modify_mad);
2341 void ib_cancel_mad(struct ib_mad_agent *mad_agent,
2342 struct ib_mad_send_buf *send_buf)
2344 ib_modify_mad(mad_agent, send_buf, 0);
2346 EXPORT_SYMBOL(ib_cancel_mad);
2348 static void local_completions(struct work_struct *work)
2350 struct ib_mad_agent_private *mad_agent_priv;
2351 struct ib_mad_local_private *local;
2352 struct ib_mad_agent_private *recv_mad_agent;
2353 unsigned long flags;
2356 struct ib_mad_send_wc mad_send_wc;
2359 container_of(work, struct ib_mad_agent_private, local_work);
2361 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2362 while (!list_empty(&mad_agent_priv->local_list)) {
2363 local = list_entry(mad_agent_priv->local_list.next,
2364 struct ib_mad_local_private,
2366 list_del(&local->completion_list);
2367 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2369 if (local->mad_priv) {
2370 recv_mad_agent = local->recv_mad_agent;
2371 if (!recv_mad_agent) {
2372 printk(KERN_ERR PFX "No receive MAD agent for local completion\n");
2374 goto local_send_completion;
2378 * Defined behavior is to complete response
2381 build_smp_wc(recv_mad_agent->agent.qp,
2382 (unsigned long) local->mad_send_wr,
2383 be16_to_cpu(IB_LID_PERMISSIVE),
2384 0, recv_mad_agent->agent.port_num, &wc);
2386 local->mad_priv->header.recv_wc.wc = &wc;
2387 local->mad_priv->header.recv_wc.mad_len =
2388 sizeof(struct ib_mad);
2389 INIT_LIST_HEAD(&local->mad_priv->header.recv_wc.rmpp_list);
2390 list_add(&local->mad_priv->header.recv_wc.recv_buf.list,
2391 &local->mad_priv->header.recv_wc.rmpp_list);
2392 local->mad_priv->header.recv_wc.recv_buf.grh = NULL;
2393 local->mad_priv->header.recv_wc.recv_buf.mad =
2394 &local->mad_priv->mad.mad;
2395 if (atomic_read(&recv_mad_agent->qp_info->snoop_count))
2396 snoop_recv(recv_mad_agent->qp_info,
2397 &local->mad_priv->header.recv_wc,
2398 IB_MAD_SNOOP_RECVS);
2399 recv_mad_agent->agent.recv_handler(
2400 &recv_mad_agent->agent,
2401 &local->mad_priv->header.recv_wc);
2402 spin_lock_irqsave(&recv_mad_agent->lock, flags);
2403 atomic_dec(&recv_mad_agent->refcount);
2404 spin_unlock_irqrestore(&recv_mad_agent->lock, flags);
2407 local_send_completion:
2409 mad_send_wc.status = IB_WC_SUCCESS;
2410 mad_send_wc.vendor_err = 0;
2411 mad_send_wc.send_buf = &local->mad_send_wr->send_buf;
2412 if (atomic_read(&mad_agent_priv->qp_info->snoop_count))
2413 snoop_send(mad_agent_priv->qp_info,
2414 &local->mad_send_wr->send_buf,
2415 &mad_send_wc, IB_MAD_SNOOP_SEND_COMPLETIONS);
2416 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2419 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2420 atomic_dec(&mad_agent_priv->refcount);
2422 kmem_cache_free(ib_mad_cache, local->mad_priv);
2425 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2428 static int retry_send(struct ib_mad_send_wr_private *mad_send_wr)
2432 if (!mad_send_wr->retries_left)
2435 mad_send_wr->retries_left--;
2436 mad_send_wr->send_buf.retries++;
2438 mad_send_wr->timeout = msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms);
2440 if (mad_send_wr->mad_agent_priv->agent.rmpp_version) {
2441 ret = ib_retry_rmpp(mad_send_wr);
2443 case IB_RMPP_RESULT_UNHANDLED:
2444 ret = ib_send_mad(mad_send_wr);
2446 case IB_RMPP_RESULT_CONSUMED:
2454 ret = ib_send_mad(mad_send_wr);
2457 mad_send_wr->refcount++;
2458 list_add_tail(&mad_send_wr->agent_list,
2459 &mad_send_wr->mad_agent_priv->send_list);
2464 static void timeout_sends(struct work_struct *work)
2466 struct ib_mad_agent_private *mad_agent_priv;
2467 struct ib_mad_send_wr_private *mad_send_wr;
2468 struct ib_mad_send_wc mad_send_wc;
2469 unsigned long flags, delay;
2471 mad_agent_priv = container_of(work, struct ib_mad_agent_private,
2473 mad_send_wc.vendor_err = 0;
2475 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2476 while (!list_empty(&mad_agent_priv->wait_list)) {
2477 mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
2478 struct ib_mad_send_wr_private,
2481 if (time_after(mad_send_wr->timeout, jiffies)) {
2482 delay = mad_send_wr->timeout - jiffies;
2483 if ((long)delay <= 0)
2485 queue_delayed_work(mad_agent_priv->qp_info->
2487 &mad_agent_priv->timed_work, delay);
2491 list_del(&mad_send_wr->agent_list);
2492 if (mad_send_wr->status == IB_WC_SUCCESS &&
2493 !retry_send(mad_send_wr))
2496 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2498 if (mad_send_wr->status == IB_WC_SUCCESS)
2499 mad_send_wc.status = IB_WC_RESP_TIMEOUT_ERR;
2501 mad_send_wc.status = mad_send_wr->status;
2502 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2503 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2506 atomic_dec(&mad_agent_priv->refcount);
2507 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2509 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2512 static void ib_mad_thread_completion_handler(struct ib_cq *cq, void *arg)
2514 struct ib_mad_port_private *port_priv = cq->cq_context;
2515 unsigned long flags;
2517 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2518 if (!list_empty(&port_priv->port_list))
2519 queue_work(port_priv->wq, &port_priv->work);
2520 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2524 * Allocate receive MADs and post receive WRs for them
2526 static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
2527 struct ib_mad_private *mad)
2529 unsigned long flags;
2531 struct ib_mad_private *mad_priv;
2532 struct ib_sge sg_list;
2533 struct ib_recv_wr recv_wr, *bad_recv_wr;
2534 struct ib_mad_queue *recv_queue = &qp_info->recv_queue;
2536 /* Initialize common scatter list fields */
2537 sg_list.length = sizeof *mad_priv - sizeof mad_priv->header;
2538 sg_list.lkey = (*qp_info->port_priv->mr).lkey;
2540 /* Initialize common receive WR fields */
2541 recv_wr.next = NULL;
2542 recv_wr.sg_list = &sg_list;
2543 recv_wr.num_sge = 1;
2546 /* Allocate and map receive buffer */
2551 mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL);
2553 printk(KERN_ERR PFX "No memory for receive buffer\n");
2558 sg_list.addr = ib_dma_map_single(qp_info->port_priv->device,
2561 sizeof mad_priv->header,
2563 mad_priv->header.mapping = sg_list.addr;
2564 recv_wr.wr_id = (unsigned long)&mad_priv->header.mad_list;
2565 mad_priv->header.mad_list.mad_queue = recv_queue;
2567 /* Post receive WR */
2568 spin_lock_irqsave(&recv_queue->lock, flags);
2569 post = (++recv_queue->count < recv_queue->max_active);
2570 list_add_tail(&mad_priv->header.mad_list.list, &recv_queue->list);
2571 spin_unlock_irqrestore(&recv_queue->lock, flags);
2572 ret = ib_post_recv(qp_info->qp, &recv_wr, &bad_recv_wr);
2574 spin_lock_irqsave(&recv_queue->lock, flags);
2575 list_del(&mad_priv->header.mad_list.list);
2576 recv_queue->count--;
2577 spin_unlock_irqrestore(&recv_queue->lock, flags);
2578 ib_dma_unmap_single(qp_info->port_priv->device,
2579 mad_priv->header.mapping,
2581 sizeof mad_priv->header,
2583 kmem_cache_free(ib_mad_cache, mad_priv);
2584 printk(KERN_ERR PFX "ib_post_recv failed: %d\n", ret);
2593 * Return all the posted receive MADs
2595 static void cleanup_recv_queue(struct ib_mad_qp_info *qp_info)
2597 struct ib_mad_private_header *mad_priv_hdr;
2598 struct ib_mad_private *recv;
2599 struct ib_mad_list_head *mad_list;
2604 while (!list_empty(&qp_info->recv_queue.list)) {
2606 mad_list = list_entry(qp_info->recv_queue.list.next,
2607 struct ib_mad_list_head, list);
2608 mad_priv_hdr = container_of(mad_list,
2609 struct ib_mad_private_header,
2611 recv = container_of(mad_priv_hdr, struct ib_mad_private,
2614 /* Remove from posted receive MAD list */
2615 list_del(&mad_list->list);
2617 ib_dma_unmap_single(qp_info->port_priv->device,
2618 recv->header.mapping,
2619 sizeof(struct ib_mad_private) -
2620 sizeof(struct ib_mad_private_header),
2622 kmem_cache_free(ib_mad_cache, recv);
2625 qp_info->recv_queue.count = 0;
2631 static int ib_mad_port_start(struct ib_mad_port_private *port_priv)
2634 struct ib_qp_attr *attr;
2637 attr = kmalloc(sizeof *attr, GFP_KERNEL);
2639 printk(KERN_ERR PFX "Couldn't kmalloc ib_qp_attr\n");
2643 for (i = 0; i < IB_MAD_QPS_CORE; i++) {
2644 qp = port_priv->qp_info[i].qp;
2649 * PKey index for QP1 is irrelevant but
2650 * one is needed for the Reset to Init transition
2652 attr->qp_state = IB_QPS_INIT;
2653 attr->pkey_index = 0;
2654 attr->qkey = (qp->qp_num == 0) ? 0 : IB_QP1_QKEY;
2655 ret = ib_modify_qp(qp, attr, IB_QP_STATE |
2656 IB_QP_PKEY_INDEX | IB_QP_QKEY);
2658 printk(KERN_ERR PFX "Couldn't change QP%d state to "
2659 "INIT: %d\n", i, ret);
2663 attr->qp_state = IB_QPS_RTR;
2664 ret = ib_modify_qp(qp, attr, IB_QP_STATE);
2666 printk(KERN_ERR PFX "Couldn't change QP%d state to "
2667 "RTR: %d\n", i, ret);
2671 attr->qp_state = IB_QPS_RTS;
2672 attr->sq_psn = IB_MAD_SEND_Q_PSN;
2673 ret = ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_SQ_PSN);
2675 printk(KERN_ERR PFX "Couldn't change QP%d state to "
2676 "RTS: %d\n", i, ret);
2681 ret = ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
2683 printk(KERN_ERR PFX "Failed to request completion "
2684 "notification: %d\n", ret);
2688 for (i = 0; i < IB_MAD_QPS_CORE; i++) {
2689 if (!port_priv->qp_info[i].qp)
2692 ret = ib_mad_post_receive_mads(&port_priv->qp_info[i], NULL);
2694 printk(KERN_ERR PFX "Couldn't post receive WRs\n");
2703 static void qp_event_handler(struct ib_event *event, void *qp_context)
2705 struct ib_mad_qp_info *qp_info = qp_context;
2707 /* It's worse than that! He's dead, Jim! */
2708 printk(KERN_ERR PFX "Fatal error (%d) on MAD QP (%d)\n",
2709 event->event, qp_info->qp->qp_num);
2712 static void init_mad_queue(struct ib_mad_qp_info *qp_info,
2713 struct ib_mad_queue *mad_queue)
2715 mad_queue->qp_info = qp_info;
2716 mad_queue->count = 0;
2717 spin_lock_init(&mad_queue->lock);
2718 INIT_LIST_HEAD(&mad_queue->list);
2721 static void init_mad_qp(struct ib_mad_port_private *port_priv,
2722 struct ib_mad_qp_info *qp_info)
2724 qp_info->port_priv = port_priv;
2725 init_mad_queue(qp_info, &qp_info->send_queue);
2726 init_mad_queue(qp_info, &qp_info->recv_queue);
2727 INIT_LIST_HEAD(&qp_info->overflow_list);
2728 spin_lock_init(&qp_info->snoop_lock);
2729 qp_info->snoop_table = NULL;
2730 qp_info->snoop_table_size = 0;
2731 atomic_set(&qp_info->snoop_count, 0);
2734 static int create_mad_qp(struct ib_mad_qp_info *qp_info,
2735 enum ib_qp_type qp_type)
2737 struct ib_qp_init_attr qp_init_attr;
2740 memset(&qp_init_attr, 0, sizeof qp_init_attr);
2741 qp_init_attr.send_cq = qp_info->port_priv->cq;
2742 qp_init_attr.recv_cq = qp_info->port_priv->cq;
2743 qp_init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
2744 qp_init_attr.cap.max_send_wr = mad_sendq_size;
2745 qp_init_attr.cap.max_recv_wr = mad_recvq_size;
2746 qp_init_attr.cap.max_send_sge = IB_MAD_SEND_REQ_MAX_SG;
2747 qp_init_attr.cap.max_recv_sge = IB_MAD_RECV_REQ_MAX_SG;
2748 qp_init_attr.qp_type = qp_type;
2749 qp_init_attr.port_num = qp_info->port_priv->port_num;
2750 qp_init_attr.qp_context = qp_info;
2751 qp_init_attr.event_handler = qp_event_handler;
2752 qp_info->qp = ib_create_qp(qp_info->port_priv->pd, &qp_init_attr);
2753 if (IS_ERR(qp_info->qp)) {
2754 printk(KERN_ERR PFX "Couldn't create ib_mad QP%d\n",
2755 get_spl_qp_index(qp_type));
2756 ret = PTR_ERR(qp_info->qp);
2759 /* Use minimum queue sizes unless the CQ is resized */
2760 qp_info->send_queue.max_active = mad_sendq_size;
2761 qp_info->recv_queue.max_active = mad_recvq_size;
2768 static void destroy_mad_qp(struct ib_mad_qp_info *qp_info)
2773 ib_destroy_qp(qp_info->qp);
2774 kfree(qp_info->snoop_table);
2779 * Create the QP, PD, MR, and CQ if needed
2781 static int ib_mad_port_open(struct ib_device *device,
2785 struct ib_mad_port_private *port_priv;
2786 unsigned long flags;
2787 char name[sizeof "ib_mad123"];
2790 /* Create new device info */
2791 port_priv = kzalloc(sizeof *port_priv, GFP_KERNEL);
2793 printk(KERN_ERR PFX "No memory for ib_mad_port_private\n");
2797 port_priv->device = device;
2798 port_priv->port_num = port_num;
2799 spin_lock_init(&port_priv->reg_lock);
2800 INIT_LIST_HEAD(&port_priv->agent_list);
2801 init_mad_qp(port_priv, &port_priv->qp_info[0]);
2802 init_mad_qp(port_priv, &port_priv->qp_info[1]);
2804 cq_size = mad_sendq_size + mad_recvq_size;
2805 has_smi = rdma_port_get_link_layer(device, port_num) == IB_LINK_LAYER_INFINIBAND;
2809 port_priv->cq = ib_create_cq(port_priv->device,
2810 ib_mad_thread_completion_handler,
2811 NULL, port_priv, cq_size, 0);
2812 if (IS_ERR(port_priv->cq)) {
2813 printk(KERN_ERR PFX "Couldn't create ib_mad CQ\n");
2814 ret = PTR_ERR(port_priv->cq);
2818 port_priv->pd = ib_alloc_pd(device);
2819 if (IS_ERR(port_priv->pd)) {
2820 printk(KERN_ERR PFX "Couldn't create ib_mad PD\n");
2821 ret = PTR_ERR(port_priv->pd);
2825 port_priv->mr = ib_get_dma_mr(port_priv->pd, IB_ACCESS_LOCAL_WRITE);
2826 if (IS_ERR(port_priv->mr)) {
2827 printk(KERN_ERR PFX "Couldn't get ib_mad DMA MR\n");
2828 ret = PTR_ERR(port_priv->mr);
2833 ret = create_mad_qp(&port_priv->qp_info[0], IB_QPT_SMI);
2837 ret = create_mad_qp(&port_priv->qp_info[1], IB_QPT_GSI);
2841 snprintf(name, sizeof name, "ib_mad%d", port_num);
2842 port_priv->wq = create_singlethread_workqueue(name);
2843 if (!port_priv->wq) {
2847 INIT_WORK(&port_priv->work, ib_mad_completion_handler);
2849 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2850 list_add_tail(&port_priv->port_list, &ib_mad_port_list);
2851 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2853 ret = ib_mad_port_start(port_priv);
2855 printk(KERN_ERR PFX "Couldn't start port\n");
2862 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2863 list_del_init(&port_priv->port_list);
2864 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2866 destroy_workqueue(port_priv->wq);
2868 destroy_mad_qp(&port_priv->qp_info[1]);
2870 destroy_mad_qp(&port_priv->qp_info[0]);
2872 ib_dereg_mr(port_priv->mr);
2874 ib_dealloc_pd(port_priv->pd);
2876 ib_destroy_cq(port_priv->cq);
2877 cleanup_recv_queue(&port_priv->qp_info[1]);
2878 cleanup_recv_queue(&port_priv->qp_info[0]);
2887 * If there are no classes using the port, free the port
2888 * resources (CQ, MR, PD, QP) and remove the port's info structure
2890 static int ib_mad_port_close(struct ib_device *device, int port_num)
2892 struct ib_mad_port_private *port_priv;
2893 unsigned long flags;
2895 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2896 port_priv = __ib_get_mad_port(device, port_num);
2897 if (port_priv == NULL) {
2898 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2899 printk(KERN_ERR PFX "Port %d not found\n", port_num);
2902 list_del_init(&port_priv->port_list);
2903 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2905 destroy_workqueue(port_priv->wq);
2906 destroy_mad_qp(&port_priv->qp_info[1]);
2907 destroy_mad_qp(&port_priv->qp_info[0]);
2908 ib_dereg_mr(port_priv->mr);
2909 ib_dealloc_pd(port_priv->pd);
2910 ib_destroy_cq(port_priv->cq);
2911 cleanup_recv_queue(&port_priv->qp_info[1]);
2912 cleanup_recv_queue(&port_priv->qp_info[0]);
2913 /* XXX: Handle deallocation of MAD registration tables */
2920 static void ib_mad_init_device(struct ib_device *device)
2924 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
2927 if (device->node_type == RDMA_NODE_IB_SWITCH) {
2932 end = device->phys_port_cnt;
2935 for (i = start; i <= end; i++) {
2936 if (ib_mad_port_open(device, i)) {
2937 printk(KERN_ERR PFX "Couldn't open %s port %d\n",
2941 if (ib_agent_port_open(device, i)) {
2942 printk(KERN_ERR PFX "Couldn't open %s port %d "
2951 if (ib_mad_port_close(device, i))
2952 printk(KERN_ERR PFX "Couldn't close %s port %d\n",
2958 while (i >= start) {
2959 if (ib_agent_port_close(device, i))
2960 printk(KERN_ERR PFX "Couldn't close %s port %d "
2963 if (ib_mad_port_close(device, i))
2964 printk(KERN_ERR PFX "Couldn't close %s port %d\n",
2970 static void ib_mad_remove_device(struct ib_device *device)
2972 int i, num_ports, cur_port;
2974 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
2977 if (device->node_type == RDMA_NODE_IB_SWITCH) {
2981 num_ports = device->phys_port_cnt;
2984 for (i = 0; i < num_ports; i++, cur_port++) {
2985 if (ib_agent_port_close(device, cur_port))
2986 printk(KERN_ERR PFX "Couldn't close %s port %d "
2988 device->name, cur_port);
2989 if (ib_mad_port_close(device, cur_port))
2990 printk(KERN_ERR PFX "Couldn't close %s port %d\n",
2991 device->name, cur_port);
2995 static struct ib_client mad_client = {
2997 .add = ib_mad_init_device,
2998 .remove = ib_mad_remove_device
3001 static int __init ib_mad_init_module(void)
3005 mad_recvq_size = min(mad_recvq_size, IB_MAD_QP_MAX_SIZE);
3006 mad_recvq_size = max(mad_recvq_size, IB_MAD_QP_MIN_SIZE);
3008 mad_sendq_size = min(mad_sendq_size, IB_MAD_QP_MAX_SIZE);
3009 mad_sendq_size = max(mad_sendq_size, IB_MAD_QP_MIN_SIZE);
3011 ib_mad_cache = kmem_cache_create("ib_mad",
3012 sizeof(struct ib_mad_private),
3016 if (!ib_mad_cache) {
3017 printk(KERN_ERR PFX "Couldn't create ib_mad cache\n");
3022 INIT_LIST_HEAD(&ib_mad_port_list);
3024 if (ib_register_client(&mad_client)) {
3025 printk(KERN_ERR PFX "Couldn't register ib_mad client\n");
3033 kmem_cache_destroy(ib_mad_cache);
3038 static void __exit ib_mad_cleanup_module(void)
3040 ib_unregister_client(&mad_client);
3041 kmem_cache_destroy(ib_mad_cache);
3044 module_init(ib_mad_init_module);
3045 module_exit(ib_mad_cleanup_module);