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 <rdma/ib_cache.h>
44 MODULE_LICENSE("Dual BSD/GPL");
45 MODULE_DESCRIPTION("kernel IB MAD API");
46 MODULE_AUTHOR("Hal Rosenstock");
47 MODULE_AUTHOR("Sean Hefty");
49 int mad_sendq_size = IB_MAD_QP_SEND_SIZE;
50 int mad_recvq_size = IB_MAD_QP_RECV_SIZE;
52 module_param_named(send_queue_size, mad_sendq_size, int, 0444);
53 MODULE_PARM_DESC(send_queue_size, "Size of send queue in number of work requests");
54 module_param_named(recv_queue_size, mad_recvq_size, int, 0444);
55 MODULE_PARM_DESC(recv_queue_size, "Size of receive queue in number of work requests");
57 static struct kmem_cache *ib_mad_cache;
59 static struct list_head ib_mad_port_list;
60 static u32 ib_mad_client_id = 0;
63 static spinlock_t 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 = kmalloc(sizeof *reg_req, GFP_KERNEL);
296 ret = ERR_PTR(-ENOMEM);
299 /* Make a copy of the MAD registration request */
300 memcpy(reg_req, mad_reg_req, sizeof *reg_req);
303 /* Now, fill in the various structures */
304 mad_agent_priv->qp_info = &port_priv->qp_info[qpn];
305 mad_agent_priv->reg_req = reg_req;
306 mad_agent_priv->agent.rmpp_version = rmpp_version;
307 mad_agent_priv->agent.device = device;
308 mad_agent_priv->agent.recv_handler = recv_handler;
309 mad_agent_priv->agent.send_handler = send_handler;
310 mad_agent_priv->agent.context = context;
311 mad_agent_priv->agent.qp = port_priv->qp_info[qpn].qp;
312 mad_agent_priv->agent.port_num = port_num;
313 spin_lock_init(&mad_agent_priv->lock);
314 INIT_LIST_HEAD(&mad_agent_priv->send_list);
315 INIT_LIST_HEAD(&mad_agent_priv->wait_list);
316 INIT_LIST_HEAD(&mad_agent_priv->done_list);
317 INIT_LIST_HEAD(&mad_agent_priv->rmpp_list);
318 INIT_DELAYED_WORK(&mad_agent_priv->timed_work, timeout_sends);
319 INIT_LIST_HEAD(&mad_agent_priv->local_list);
320 INIT_WORK(&mad_agent_priv->local_work, local_completions);
321 atomic_set(&mad_agent_priv->refcount, 1);
322 init_completion(&mad_agent_priv->comp);
324 spin_lock_irqsave(&port_priv->reg_lock, flags);
325 mad_agent_priv->agent.hi_tid = ++ib_mad_client_id;
328 * Make sure MAD registration (if supplied)
329 * is non overlapping with any existing ones
332 mgmt_class = convert_mgmt_class(mad_reg_req->mgmt_class);
333 if (!is_vendor_class(mgmt_class)) {
334 class = port_priv->version[mad_reg_req->
335 mgmt_class_version].class;
337 method = class->method_table[mgmt_class];
339 if (method_in_use(&method,
344 ret2 = add_nonoui_reg_req(mad_reg_req, mad_agent_priv,
347 /* "New" vendor class range */
348 vendor = port_priv->version[mad_reg_req->
349 mgmt_class_version].vendor;
351 vclass = vendor_class_index(mgmt_class);
352 vendor_class = vendor->vendor_class[vclass];
354 if (is_vendor_method_in_use(
360 ret2 = add_oui_reg_req(mad_reg_req, mad_agent_priv);
368 /* Add mad agent into port's agent list */
369 list_add_tail(&mad_agent_priv->agent_list, &port_priv->agent_list);
370 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
372 return &mad_agent_priv->agent;
375 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
378 ib_dereg_mr(mad_agent_priv->agent.mr);
380 kfree(mad_agent_priv);
384 EXPORT_SYMBOL(ib_register_mad_agent);
386 static inline int is_snooping_sends(int mad_snoop_flags)
388 return (mad_snoop_flags &
389 (/*IB_MAD_SNOOP_POSTED_SENDS |
390 IB_MAD_SNOOP_RMPP_SENDS |*/
391 IB_MAD_SNOOP_SEND_COMPLETIONS /*|
392 IB_MAD_SNOOP_RMPP_SEND_COMPLETIONS*/));
395 static inline int is_snooping_recvs(int mad_snoop_flags)
397 return (mad_snoop_flags &
398 (IB_MAD_SNOOP_RECVS /*|
399 IB_MAD_SNOOP_RMPP_RECVS*/));
402 static int register_snoop_agent(struct ib_mad_qp_info *qp_info,
403 struct ib_mad_snoop_private *mad_snoop_priv)
405 struct ib_mad_snoop_private **new_snoop_table;
409 spin_lock_irqsave(&qp_info->snoop_lock, flags);
410 /* Check for empty slot in array. */
411 for (i = 0; i < qp_info->snoop_table_size; i++)
412 if (!qp_info->snoop_table[i])
415 if (i == qp_info->snoop_table_size) {
417 new_snoop_table = krealloc(qp_info->snoop_table,
418 sizeof mad_snoop_priv *
419 (qp_info->snoop_table_size + 1),
421 if (!new_snoop_table) {
426 qp_info->snoop_table = new_snoop_table;
427 qp_info->snoop_table_size++;
429 qp_info->snoop_table[i] = mad_snoop_priv;
430 atomic_inc(&qp_info->snoop_count);
432 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
436 struct ib_mad_agent *ib_register_mad_snoop(struct ib_device *device,
438 enum ib_qp_type qp_type,
440 ib_mad_snoop_handler snoop_handler,
441 ib_mad_recv_handler recv_handler,
444 struct ib_mad_port_private *port_priv;
445 struct ib_mad_agent *ret;
446 struct ib_mad_snoop_private *mad_snoop_priv;
449 /* Validate parameters */
450 if ((is_snooping_sends(mad_snoop_flags) && !snoop_handler) ||
451 (is_snooping_recvs(mad_snoop_flags) && !recv_handler)) {
452 ret = ERR_PTR(-EINVAL);
455 qpn = get_spl_qp_index(qp_type);
457 ret = ERR_PTR(-EINVAL);
460 port_priv = ib_get_mad_port(device, port_num);
462 ret = ERR_PTR(-ENODEV);
465 /* Allocate structures */
466 mad_snoop_priv = kzalloc(sizeof *mad_snoop_priv, GFP_KERNEL);
467 if (!mad_snoop_priv) {
468 ret = ERR_PTR(-ENOMEM);
472 /* Now, fill in the various structures */
473 mad_snoop_priv->qp_info = &port_priv->qp_info[qpn];
474 mad_snoop_priv->agent.device = device;
475 mad_snoop_priv->agent.recv_handler = recv_handler;
476 mad_snoop_priv->agent.snoop_handler = snoop_handler;
477 mad_snoop_priv->agent.context = context;
478 mad_snoop_priv->agent.qp = port_priv->qp_info[qpn].qp;
479 mad_snoop_priv->agent.port_num = port_num;
480 mad_snoop_priv->mad_snoop_flags = mad_snoop_flags;
481 init_completion(&mad_snoop_priv->comp);
482 mad_snoop_priv->snoop_index = register_snoop_agent(
483 &port_priv->qp_info[qpn],
485 if (mad_snoop_priv->snoop_index < 0) {
486 ret = ERR_PTR(mad_snoop_priv->snoop_index);
490 atomic_set(&mad_snoop_priv->refcount, 1);
491 return &mad_snoop_priv->agent;
494 kfree(mad_snoop_priv);
498 EXPORT_SYMBOL(ib_register_mad_snoop);
500 static inline void deref_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
502 if (atomic_dec_and_test(&mad_agent_priv->refcount))
503 complete(&mad_agent_priv->comp);
506 static inline void deref_snoop_agent(struct ib_mad_snoop_private *mad_snoop_priv)
508 if (atomic_dec_and_test(&mad_snoop_priv->refcount))
509 complete(&mad_snoop_priv->comp);
512 static void unregister_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
514 struct ib_mad_port_private *port_priv;
517 /* Note that we could still be handling received MADs */
520 * Canceling all sends results in dropping received response
521 * MADs, preventing us from queuing additional work
523 cancel_mads(mad_agent_priv);
524 port_priv = mad_agent_priv->qp_info->port_priv;
525 cancel_delayed_work(&mad_agent_priv->timed_work);
527 spin_lock_irqsave(&port_priv->reg_lock, flags);
528 remove_mad_reg_req(mad_agent_priv);
529 list_del(&mad_agent_priv->agent_list);
530 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
532 flush_workqueue(port_priv->wq);
533 ib_cancel_rmpp_recvs(mad_agent_priv);
535 deref_mad_agent(mad_agent_priv);
536 wait_for_completion(&mad_agent_priv->comp);
538 kfree(mad_agent_priv->reg_req);
539 ib_dereg_mr(mad_agent_priv->agent.mr);
540 kfree(mad_agent_priv);
543 static void unregister_mad_snoop(struct ib_mad_snoop_private *mad_snoop_priv)
545 struct ib_mad_qp_info *qp_info;
548 qp_info = mad_snoop_priv->qp_info;
549 spin_lock_irqsave(&qp_info->snoop_lock, flags);
550 qp_info->snoop_table[mad_snoop_priv->snoop_index] = NULL;
551 atomic_dec(&qp_info->snoop_count);
552 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
554 deref_snoop_agent(mad_snoop_priv);
555 wait_for_completion(&mad_snoop_priv->comp);
557 kfree(mad_snoop_priv);
561 * ib_unregister_mad_agent - Unregisters a client from using MAD services
563 int ib_unregister_mad_agent(struct ib_mad_agent *mad_agent)
565 struct ib_mad_agent_private *mad_agent_priv;
566 struct ib_mad_snoop_private *mad_snoop_priv;
568 /* If the TID is zero, the agent can only snoop. */
569 if (mad_agent->hi_tid) {
570 mad_agent_priv = container_of(mad_agent,
571 struct ib_mad_agent_private,
573 unregister_mad_agent(mad_agent_priv);
575 mad_snoop_priv = container_of(mad_agent,
576 struct ib_mad_snoop_private,
578 unregister_mad_snoop(mad_snoop_priv);
582 EXPORT_SYMBOL(ib_unregister_mad_agent);
584 static void dequeue_mad(struct ib_mad_list_head *mad_list)
586 struct ib_mad_queue *mad_queue;
589 BUG_ON(!mad_list->mad_queue);
590 mad_queue = mad_list->mad_queue;
591 spin_lock_irqsave(&mad_queue->lock, flags);
592 list_del(&mad_list->list);
594 spin_unlock_irqrestore(&mad_queue->lock, flags);
597 static void snoop_send(struct ib_mad_qp_info *qp_info,
598 struct ib_mad_send_buf *send_buf,
599 struct ib_mad_send_wc *mad_send_wc,
602 struct ib_mad_snoop_private *mad_snoop_priv;
606 spin_lock_irqsave(&qp_info->snoop_lock, flags);
607 for (i = 0; i < qp_info->snoop_table_size; i++) {
608 mad_snoop_priv = qp_info->snoop_table[i];
609 if (!mad_snoop_priv ||
610 !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
613 atomic_inc(&mad_snoop_priv->refcount);
614 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
615 mad_snoop_priv->agent.snoop_handler(&mad_snoop_priv->agent,
616 send_buf, mad_send_wc);
617 deref_snoop_agent(mad_snoop_priv);
618 spin_lock_irqsave(&qp_info->snoop_lock, flags);
620 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
623 static void snoop_recv(struct ib_mad_qp_info *qp_info,
624 struct ib_mad_recv_wc *mad_recv_wc,
627 struct ib_mad_snoop_private *mad_snoop_priv;
631 spin_lock_irqsave(&qp_info->snoop_lock, flags);
632 for (i = 0; i < qp_info->snoop_table_size; i++) {
633 mad_snoop_priv = qp_info->snoop_table[i];
634 if (!mad_snoop_priv ||
635 !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
638 atomic_inc(&mad_snoop_priv->refcount);
639 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
640 mad_snoop_priv->agent.recv_handler(&mad_snoop_priv->agent,
642 deref_snoop_agent(mad_snoop_priv);
643 spin_lock_irqsave(&qp_info->snoop_lock, flags);
645 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
648 static void build_smp_wc(struct ib_qp *qp,
649 u64 wr_id, u16 slid, u16 pkey_index, u8 port_num,
652 memset(wc, 0, sizeof *wc);
654 wc->status = IB_WC_SUCCESS;
655 wc->opcode = IB_WC_RECV;
656 wc->pkey_index = pkey_index;
657 wc->byte_len = sizeof(struct ib_mad) + sizeof(struct ib_grh);
662 wc->dlid_path_bits = 0;
663 wc->port_num = port_num;
667 * Return 0 if SMP is to be sent
668 * Return 1 if SMP was consumed locally (whether or not solicited)
669 * Return < 0 if error
671 static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
672 struct ib_mad_send_wr_private *mad_send_wr)
675 struct ib_smp *smp = mad_send_wr->send_buf.mad;
677 struct ib_mad_local_private *local;
678 struct ib_mad_private *mad_priv;
679 struct ib_mad_port_private *port_priv;
680 struct ib_mad_agent_private *recv_mad_agent = NULL;
681 struct ib_device *device = mad_agent_priv->agent.device;
684 struct ib_send_wr *send_wr = &mad_send_wr->send_wr;
686 if (device->node_type == RDMA_NODE_IB_SWITCH &&
687 smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
688 port_num = send_wr->wr.ud.port_num;
690 port_num = mad_agent_priv->agent.port_num;
693 * Directed route handling starts if the initial LID routed part of
694 * a request or the ending LID routed part of a response is empty.
695 * If we are at the start of the LID routed part, don't update the
696 * hop_ptr or hop_cnt. See section 14.2.2, Vol 1 IB spec.
698 if ((ib_get_smp_direction(smp) ? smp->dr_dlid : smp->dr_slid) ==
700 smi_handle_dr_smp_send(smp, device->node_type, port_num) ==
703 printk(KERN_ERR PFX "Invalid directed route\n");
707 /* Check to post send on QP or process locally */
708 if (smi_check_local_smp(smp, device) == IB_SMI_DISCARD &&
709 smi_check_local_returning_smp(smp, device) == IB_SMI_DISCARD)
712 local = kmalloc(sizeof *local, GFP_ATOMIC);
715 printk(KERN_ERR PFX "No memory for ib_mad_local_private\n");
718 local->mad_priv = NULL;
719 local->recv_mad_agent = NULL;
720 mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_ATOMIC);
723 printk(KERN_ERR PFX "No memory for local response MAD\n");
728 build_smp_wc(mad_agent_priv->agent.qp,
729 send_wr->wr_id, be16_to_cpu(smp->dr_slid),
730 send_wr->wr.ud.pkey_index,
731 send_wr->wr.ud.port_num, &mad_wc);
733 /* No GRH for DR SMP */
734 ret = device->process_mad(device, 0, port_num, &mad_wc, NULL,
735 (struct ib_mad *)smp,
736 (struct ib_mad *)&mad_priv->mad);
739 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY:
740 if (ib_response_mad(&mad_priv->mad.mad) &&
741 mad_agent_priv->agent.recv_handler) {
742 local->mad_priv = mad_priv;
743 local->recv_mad_agent = mad_agent_priv;
745 * Reference MAD agent until receive
746 * side of local completion handled
748 atomic_inc(&mad_agent_priv->refcount);
750 kmem_cache_free(ib_mad_cache, mad_priv);
752 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED:
753 kmem_cache_free(ib_mad_cache, mad_priv);
755 case IB_MAD_RESULT_SUCCESS:
756 /* Treat like an incoming receive MAD */
757 port_priv = ib_get_mad_port(mad_agent_priv->agent.device,
758 mad_agent_priv->agent.port_num);
760 memcpy(&mad_priv->mad.mad, smp, sizeof(struct ib_mad));
761 recv_mad_agent = find_mad_agent(port_priv,
764 if (!port_priv || !recv_mad_agent) {
766 * No receiving agent so drop packet and
767 * generate send completion.
769 kmem_cache_free(ib_mad_cache, mad_priv);
772 local->mad_priv = mad_priv;
773 local->recv_mad_agent = recv_mad_agent;
776 kmem_cache_free(ib_mad_cache, mad_priv);
782 local->mad_send_wr = mad_send_wr;
783 /* Reference MAD agent until send side of local completion handled */
784 atomic_inc(&mad_agent_priv->refcount);
785 /* Queue local completion to local list */
786 spin_lock_irqsave(&mad_agent_priv->lock, flags);
787 list_add_tail(&local->completion_list, &mad_agent_priv->local_list);
788 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
789 queue_work(mad_agent_priv->qp_info->port_priv->wq,
790 &mad_agent_priv->local_work);
796 static int get_pad_size(int hdr_len, int data_len)
800 seg_size = sizeof(struct ib_mad) - hdr_len;
801 if (data_len && seg_size) {
802 pad = seg_size - data_len % seg_size;
803 return pad == seg_size ? 0 : pad;
808 static void free_send_rmpp_list(struct ib_mad_send_wr_private *mad_send_wr)
810 struct ib_rmpp_segment *s, *t;
812 list_for_each_entry_safe(s, t, &mad_send_wr->rmpp_list, list) {
818 static int alloc_send_rmpp_list(struct ib_mad_send_wr_private *send_wr,
821 struct ib_mad_send_buf *send_buf = &send_wr->send_buf;
822 struct ib_rmpp_mad *rmpp_mad = send_buf->mad;
823 struct ib_rmpp_segment *seg = NULL;
824 int left, seg_size, pad;
826 send_buf->seg_size = sizeof (struct ib_mad) - send_buf->hdr_len;
827 seg_size = send_buf->seg_size;
830 /* Allocate data segments. */
831 for (left = send_buf->data_len + pad; left > 0; left -= seg_size) {
832 seg = kmalloc(sizeof (*seg) + seg_size, gfp_mask);
834 printk(KERN_ERR "alloc_send_rmpp_segs: RMPP mem "
835 "alloc failed for len %zd, gfp %#x\n",
836 sizeof (*seg) + seg_size, gfp_mask);
837 free_send_rmpp_list(send_wr);
840 seg->num = ++send_buf->seg_count;
841 list_add_tail(&seg->list, &send_wr->rmpp_list);
844 /* Zero any padding */
846 memset(seg->data + seg_size - pad, 0, pad);
848 rmpp_mad->rmpp_hdr.rmpp_version = send_wr->mad_agent_priv->
850 rmpp_mad->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_DATA;
851 ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
853 send_wr->cur_seg = container_of(send_wr->rmpp_list.next,
854 struct ib_rmpp_segment, list);
855 send_wr->last_ack_seg = send_wr->cur_seg;
859 struct ib_mad_send_buf * ib_create_send_mad(struct ib_mad_agent *mad_agent,
860 u32 remote_qpn, u16 pkey_index,
862 int hdr_len, int data_len,
865 struct ib_mad_agent_private *mad_agent_priv;
866 struct ib_mad_send_wr_private *mad_send_wr;
867 int pad, message_size, ret, size;
870 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
872 pad = get_pad_size(hdr_len, data_len);
873 message_size = hdr_len + data_len + pad;
875 if ((!mad_agent->rmpp_version &&
876 (rmpp_active || message_size > sizeof(struct ib_mad))) ||
877 (!rmpp_active && message_size > sizeof(struct ib_mad)))
878 return ERR_PTR(-EINVAL);
880 size = rmpp_active ? hdr_len : sizeof(struct ib_mad);
881 buf = kzalloc(sizeof *mad_send_wr + size, gfp_mask);
883 return ERR_PTR(-ENOMEM);
885 mad_send_wr = buf + size;
886 INIT_LIST_HEAD(&mad_send_wr->rmpp_list);
887 mad_send_wr->send_buf.mad = buf;
888 mad_send_wr->send_buf.hdr_len = hdr_len;
889 mad_send_wr->send_buf.data_len = data_len;
890 mad_send_wr->pad = pad;
892 mad_send_wr->mad_agent_priv = mad_agent_priv;
893 mad_send_wr->sg_list[0].length = hdr_len;
894 mad_send_wr->sg_list[0].lkey = mad_agent->mr->lkey;
895 mad_send_wr->sg_list[1].length = sizeof(struct ib_mad) - hdr_len;
896 mad_send_wr->sg_list[1].lkey = mad_agent->mr->lkey;
898 mad_send_wr->send_wr.wr_id = (unsigned long) mad_send_wr;
899 mad_send_wr->send_wr.sg_list = mad_send_wr->sg_list;
900 mad_send_wr->send_wr.num_sge = 2;
901 mad_send_wr->send_wr.opcode = IB_WR_SEND;
902 mad_send_wr->send_wr.send_flags = IB_SEND_SIGNALED;
903 mad_send_wr->send_wr.wr.ud.remote_qpn = remote_qpn;
904 mad_send_wr->send_wr.wr.ud.remote_qkey = IB_QP_SET_QKEY;
905 mad_send_wr->send_wr.wr.ud.pkey_index = pkey_index;
908 ret = alloc_send_rmpp_list(mad_send_wr, gfp_mask);
915 mad_send_wr->send_buf.mad_agent = mad_agent;
916 atomic_inc(&mad_agent_priv->refcount);
917 return &mad_send_wr->send_buf;
919 EXPORT_SYMBOL(ib_create_send_mad);
921 int ib_get_mad_data_offset(u8 mgmt_class)
923 if (mgmt_class == IB_MGMT_CLASS_SUBN_ADM)
924 return IB_MGMT_SA_HDR;
925 else if ((mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
926 (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
927 (mgmt_class == IB_MGMT_CLASS_BIS))
928 return IB_MGMT_DEVICE_HDR;
929 else if ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
930 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END))
931 return IB_MGMT_VENDOR_HDR;
933 return IB_MGMT_MAD_HDR;
935 EXPORT_SYMBOL(ib_get_mad_data_offset);
937 int ib_is_mad_class_rmpp(u8 mgmt_class)
939 if ((mgmt_class == IB_MGMT_CLASS_SUBN_ADM) ||
940 (mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
941 (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
942 (mgmt_class == IB_MGMT_CLASS_BIS) ||
943 ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
944 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END)))
948 EXPORT_SYMBOL(ib_is_mad_class_rmpp);
950 void *ib_get_rmpp_segment(struct ib_mad_send_buf *send_buf, int seg_num)
952 struct ib_mad_send_wr_private *mad_send_wr;
953 struct list_head *list;
955 mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
957 list = &mad_send_wr->cur_seg->list;
959 if (mad_send_wr->cur_seg->num < seg_num) {
960 list_for_each_entry(mad_send_wr->cur_seg, list, list)
961 if (mad_send_wr->cur_seg->num == seg_num)
963 } else if (mad_send_wr->cur_seg->num > seg_num) {
964 list_for_each_entry_reverse(mad_send_wr->cur_seg, list, list)
965 if (mad_send_wr->cur_seg->num == seg_num)
968 return mad_send_wr->cur_seg->data;
970 EXPORT_SYMBOL(ib_get_rmpp_segment);
972 static inline void *ib_get_payload(struct ib_mad_send_wr_private *mad_send_wr)
974 if (mad_send_wr->send_buf.seg_count)
975 return ib_get_rmpp_segment(&mad_send_wr->send_buf,
976 mad_send_wr->seg_num);
978 return mad_send_wr->send_buf.mad +
979 mad_send_wr->send_buf.hdr_len;
982 void ib_free_send_mad(struct ib_mad_send_buf *send_buf)
984 struct ib_mad_agent_private *mad_agent_priv;
985 struct ib_mad_send_wr_private *mad_send_wr;
987 mad_agent_priv = container_of(send_buf->mad_agent,
988 struct ib_mad_agent_private, agent);
989 mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
992 free_send_rmpp_list(mad_send_wr);
993 kfree(send_buf->mad);
994 deref_mad_agent(mad_agent_priv);
996 EXPORT_SYMBOL(ib_free_send_mad);
998 int ib_send_mad(struct ib_mad_send_wr_private *mad_send_wr)
1000 struct ib_mad_qp_info *qp_info;
1001 struct list_head *list;
1002 struct ib_send_wr *bad_send_wr;
1003 struct ib_mad_agent *mad_agent;
1005 unsigned long flags;
1008 /* Set WR ID to find mad_send_wr upon completion */
1009 qp_info = mad_send_wr->mad_agent_priv->qp_info;
1010 mad_send_wr->send_wr.wr_id = (unsigned long)&mad_send_wr->mad_list;
1011 mad_send_wr->mad_list.mad_queue = &qp_info->send_queue;
1013 mad_agent = mad_send_wr->send_buf.mad_agent;
1014 sge = mad_send_wr->sg_list;
1015 sge[0].addr = ib_dma_map_single(mad_agent->device,
1016 mad_send_wr->send_buf.mad,
1019 mad_send_wr->header_mapping = sge[0].addr;
1021 sge[1].addr = ib_dma_map_single(mad_agent->device,
1022 ib_get_payload(mad_send_wr),
1025 mad_send_wr->payload_mapping = sge[1].addr;
1027 spin_lock_irqsave(&qp_info->send_queue.lock, flags);
1028 if (qp_info->send_queue.count < qp_info->send_queue.max_active) {
1029 ret = ib_post_send(mad_agent->qp, &mad_send_wr->send_wr,
1031 list = &qp_info->send_queue.list;
1034 list = &qp_info->overflow_list;
1038 qp_info->send_queue.count++;
1039 list_add_tail(&mad_send_wr->mad_list.list, list);
1041 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
1043 ib_dma_unmap_single(mad_agent->device,
1044 mad_send_wr->header_mapping,
1045 sge[0].length, DMA_TO_DEVICE);
1046 ib_dma_unmap_single(mad_agent->device,
1047 mad_send_wr->payload_mapping,
1048 sge[1].length, DMA_TO_DEVICE);
1054 * ib_post_send_mad - Posts MAD(s) to the send queue of the QP associated
1055 * with the registered client
1057 int ib_post_send_mad(struct ib_mad_send_buf *send_buf,
1058 struct ib_mad_send_buf **bad_send_buf)
1060 struct ib_mad_agent_private *mad_agent_priv;
1061 struct ib_mad_send_buf *next_send_buf;
1062 struct ib_mad_send_wr_private *mad_send_wr;
1063 unsigned long flags;
1066 /* Walk list of send WRs and post each on send list */
1067 for (; send_buf; send_buf = next_send_buf) {
1069 mad_send_wr = container_of(send_buf,
1070 struct ib_mad_send_wr_private,
1072 mad_agent_priv = mad_send_wr->mad_agent_priv;
1074 if (!send_buf->mad_agent->send_handler ||
1075 (send_buf->timeout_ms &&
1076 !send_buf->mad_agent->recv_handler)) {
1081 if (!ib_is_mad_class_rmpp(((struct ib_mad_hdr *) send_buf->mad)->mgmt_class)) {
1082 if (mad_agent_priv->agent.rmpp_version) {
1089 * Save pointer to next work request to post in case the
1090 * current one completes, and the user modifies the work
1091 * request associated with the completion
1093 next_send_buf = send_buf->next;
1094 mad_send_wr->send_wr.wr.ud.ah = send_buf->ah;
1096 if (((struct ib_mad_hdr *) send_buf->mad)->mgmt_class ==
1097 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
1098 ret = handle_outgoing_dr_smp(mad_agent_priv,
1100 if (ret < 0) /* error */
1102 else if (ret == 1) /* locally consumed */
1106 mad_send_wr->tid = ((struct ib_mad_hdr *) send_buf->mad)->tid;
1107 /* Timeout will be updated after send completes */
1108 mad_send_wr->timeout = msecs_to_jiffies(send_buf->timeout_ms);
1109 mad_send_wr->max_retries = send_buf->retries;
1110 mad_send_wr->retries_left = send_buf->retries;
1111 send_buf->retries = 0;
1112 /* Reference for work request to QP + response */
1113 mad_send_wr->refcount = 1 + (mad_send_wr->timeout > 0);
1114 mad_send_wr->status = IB_WC_SUCCESS;
1116 /* Reference MAD agent until send completes */
1117 atomic_inc(&mad_agent_priv->refcount);
1118 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1119 list_add_tail(&mad_send_wr->agent_list,
1120 &mad_agent_priv->send_list);
1121 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1123 if (mad_agent_priv->agent.rmpp_version) {
1124 ret = ib_send_rmpp_mad(mad_send_wr);
1125 if (ret >= 0 && ret != IB_RMPP_RESULT_CONSUMED)
1126 ret = ib_send_mad(mad_send_wr);
1128 ret = ib_send_mad(mad_send_wr);
1130 /* Fail send request */
1131 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1132 list_del(&mad_send_wr->agent_list);
1133 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1134 atomic_dec(&mad_agent_priv->refcount);
1141 *bad_send_buf = send_buf;
1144 EXPORT_SYMBOL(ib_post_send_mad);
1147 * ib_free_recv_mad - Returns data buffers used to receive
1148 * a MAD to the access layer
1150 void ib_free_recv_mad(struct ib_mad_recv_wc *mad_recv_wc)
1152 struct ib_mad_recv_buf *mad_recv_buf, *temp_recv_buf;
1153 struct ib_mad_private_header *mad_priv_hdr;
1154 struct ib_mad_private *priv;
1155 struct list_head free_list;
1157 INIT_LIST_HEAD(&free_list);
1158 list_splice_init(&mad_recv_wc->rmpp_list, &free_list);
1160 list_for_each_entry_safe(mad_recv_buf, temp_recv_buf,
1162 mad_recv_wc = container_of(mad_recv_buf, struct ib_mad_recv_wc,
1164 mad_priv_hdr = container_of(mad_recv_wc,
1165 struct ib_mad_private_header,
1167 priv = container_of(mad_priv_hdr, struct ib_mad_private,
1169 kmem_cache_free(ib_mad_cache, priv);
1172 EXPORT_SYMBOL(ib_free_recv_mad);
1174 struct ib_mad_agent *ib_redirect_mad_qp(struct ib_qp *qp,
1176 ib_mad_send_handler send_handler,
1177 ib_mad_recv_handler recv_handler,
1180 return ERR_PTR(-EINVAL); /* XXX: for now */
1182 EXPORT_SYMBOL(ib_redirect_mad_qp);
1184 int ib_process_mad_wc(struct ib_mad_agent *mad_agent,
1187 printk(KERN_ERR PFX "ib_process_mad_wc() not implemented yet\n");
1190 EXPORT_SYMBOL(ib_process_mad_wc);
1192 static int method_in_use(struct ib_mad_mgmt_method_table **method,
1193 struct ib_mad_reg_req *mad_reg_req)
1197 for (i = find_first_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS);
1198 i < IB_MGMT_MAX_METHODS;
1199 i = find_next_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS,
1201 if ((*method)->agent[i]) {
1202 printk(KERN_ERR PFX "Method %d already in use\n", i);
1209 static int allocate_method_table(struct ib_mad_mgmt_method_table **method)
1211 /* Allocate management method table */
1212 *method = kzalloc(sizeof **method, GFP_ATOMIC);
1214 printk(KERN_ERR PFX "No memory for "
1215 "ib_mad_mgmt_method_table\n");
1223 * Check to see if there are any methods still in use
1225 static int check_method_table(struct ib_mad_mgmt_method_table *method)
1229 for (i = 0; i < IB_MGMT_MAX_METHODS; i++)
1230 if (method->agent[i])
1236 * Check to see if there are any method tables for this class still in use
1238 static int check_class_table(struct ib_mad_mgmt_class_table *class)
1242 for (i = 0; i < MAX_MGMT_CLASS; i++)
1243 if (class->method_table[i])
1248 static int check_vendor_class(struct ib_mad_mgmt_vendor_class *vendor_class)
1252 for (i = 0; i < MAX_MGMT_OUI; i++)
1253 if (vendor_class->method_table[i])
1258 static int find_vendor_oui(struct ib_mad_mgmt_vendor_class *vendor_class,
1263 for (i = 0; i < MAX_MGMT_OUI; i++)
1264 /* Is there matching OUI for this vendor class ? */
1265 if (!memcmp(vendor_class->oui[i], oui, 3))
1271 static int check_vendor_table(struct ib_mad_mgmt_vendor_class_table *vendor)
1275 for (i = 0; i < MAX_MGMT_VENDOR_RANGE2; i++)
1276 if (vendor->vendor_class[i])
1282 static void remove_methods_mad_agent(struct ib_mad_mgmt_method_table *method,
1283 struct ib_mad_agent_private *agent)
1287 /* Remove any methods for this mad agent */
1288 for (i = 0; i < IB_MGMT_MAX_METHODS; i++) {
1289 if (method->agent[i] == agent) {
1290 method->agent[i] = NULL;
1295 static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1296 struct ib_mad_agent_private *agent_priv,
1299 struct ib_mad_port_private *port_priv;
1300 struct ib_mad_mgmt_class_table **class;
1301 struct ib_mad_mgmt_method_table **method;
1304 port_priv = agent_priv->qp_info->port_priv;
1305 class = &port_priv->version[mad_reg_req->mgmt_class_version].class;
1307 /* Allocate management class table for "new" class version */
1308 *class = kzalloc(sizeof **class, GFP_ATOMIC);
1310 printk(KERN_ERR PFX "No memory for "
1311 "ib_mad_mgmt_class_table\n");
1316 /* Allocate method table for this management class */
1317 method = &(*class)->method_table[mgmt_class];
1318 if ((ret = allocate_method_table(method)))
1321 method = &(*class)->method_table[mgmt_class];
1323 /* Allocate method table for this management class */
1324 if ((ret = allocate_method_table(method)))
1329 /* Now, make sure methods are not already in use */
1330 if (method_in_use(method, mad_reg_req))
1333 /* Finally, add in methods being registered */
1334 for (i = find_first_bit(mad_reg_req->method_mask,
1335 IB_MGMT_MAX_METHODS);
1336 i < IB_MGMT_MAX_METHODS;
1337 i = find_next_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS,
1339 (*method)->agent[i] = agent_priv;
1344 /* Remove any methods for this mad agent */
1345 remove_methods_mad_agent(*method, agent_priv);
1346 /* Now, check to see if there are any methods in use */
1347 if (!check_method_table(*method)) {
1348 /* If not, release management method table */
1361 static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1362 struct ib_mad_agent_private *agent_priv)
1364 struct ib_mad_port_private *port_priv;
1365 struct ib_mad_mgmt_vendor_class_table **vendor_table;
1366 struct ib_mad_mgmt_vendor_class_table *vendor = NULL;
1367 struct ib_mad_mgmt_vendor_class *vendor_class = NULL;
1368 struct ib_mad_mgmt_method_table **method;
1369 int i, ret = -ENOMEM;
1372 /* "New" vendor (with OUI) class */
1373 vclass = vendor_class_index(mad_reg_req->mgmt_class);
1374 port_priv = agent_priv->qp_info->port_priv;
1375 vendor_table = &port_priv->version[
1376 mad_reg_req->mgmt_class_version].vendor;
1377 if (!*vendor_table) {
1378 /* Allocate mgmt vendor class table for "new" class version */
1379 vendor = kzalloc(sizeof *vendor, GFP_ATOMIC);
1381 printk(KERN_ERR PFX "No memory for "
1382 "ib_mad_mgmt_vendor_class_table\n");
1386 *vendor_table = vendor;
1388 if (!(*vendor_table)->vendor_class[vclass]) {
1389 /* Allocate table for this management vendor class */
1390 vendor_class = kzalloc(sizeof *vendor_class, GFP_ATOMIC);
1391 if (!vendor_class) {
1392 printk(KERN_ERR PFX "No memory for "
1393 "ib_mad_mgmt_vendor_class\n");
1397 (*vendor_table)->vendor_class[vclass] = vendor_class;
1399 for (i = 0; i < MAX_MGMT_OUI; i++) {
1400 /* Is there matching OUI for this vendor class ? */
1401 if (!memcmp((*vendor_table)->vendor_class[vclass]->oui[i],
1402 mad_reg_req->oui, 3)) {
1403 method = &(*vendor_table)->vendor_class[
1404 vclass]->method_table[i];
1409 for (i = 0; i < MAX_MGMT_OUI; i++) {
1410 /* OUI slot available ? */
1411 if (!is_vendor_oui((*vendor_table)->vendor_class[
1413 method = &(*vendor_table)->vendor_class[
1414 vclass]->method_table[i];
1416 /* Allocate method table for this OUI */
1417 if ((ret = allocate_method_table(method)))
1419 memcpy((*vendor_table)->vendor_class[vclass]->oui[i],
1420 mad_reg_req->oui, 3);
1424 printk(KERN_ERR PFX "All OUI slots in use\n");
1428 /* Now, make sure methods are not already in use */
1429 if (method_in_use(method, mad_reg_req))
1432 /* Finally, add in methods being registered */
1433 for (i = find_first_bit(mad_reg_req->method_mask,
1434 IB_MGMT_MAX_METHODS);
1435 i < IB_MGMT_MAX_METHODS;
1436 i = find_next_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS,
1438 (*method)->agent[i] = agent_priv;
1443 /* Remove any methods for this mad agent */
1444 remove_methods_mad_agent(*method, agent_priv);
1445 /* Now, check to see if there are any methods in use */
1446 if (!check_method_table(*method)) {
1447 /* If not, release management method table */
1454 (*vendor_table)->vendor_class[vclass] = NULL;
1455 kfree(vendor_class);
1459 *vendor_table = NULL;
1466 static void remove_mad_reg_req(struct ib_mad_agent_private *agent_priv)
1468 struct ib_mad_port_private *port_priv;
1469 struct ib_mad_mgmt_class_table *class;
1470 struct ib_mad_mgmt_method_table *method;
1471 struct ib_mad_mgmt_vendor_class_table *vendor;
1472 struct ib_mad_mgmt_vendor_class *vendor_class;
1477 * Was MAD registration request supplied
1478 * with original registration ?
1480 if (!agent_priv->reg_req) {
1484 port_priv = agent_priv->qp_info->port_priv;
1485 mgmt_class = convert_mgmt_class(agent_priv->reg_req->mgmt_class);
1486 class = port_priv->version[
1487 agent_priv->reg_req->mgmt_class_version].class;
1491 method = class->method_table[mgmt_class];
1493 /* Remove any methods for this mad agent */
1494 remove_methods_mad_agent(method, agent_priv);
1495 /* Now, check to see if there are any methods still in use */
1496 if (!check_method_table(method)) {
1497 /* If not, release management method table */
1499 class->method_table[mgmt_class] = NULL;
1500 /* Any management classes left ? */
1501 if (!check_class_table(class)) {
1502 /* If not, release management class table */
1505 agent_priv->reg_req->
1506 mgmt_class_version].class = NULL;
1512 if (!is_vendor_class(mgmt_class))
1515 /* normalize mgmt_class to vendor range 2 */
1516 mgmt_class = vendor_class_index(agent_priv->reg_req->mgmt_class);
1517 vendor = port_priv->version[
1518 agent_priv->reg_req->mgmt_class_version].vendor;
1523 vendor_class = vendor->vendor_class[mgmt_class];
1525 index = find_vendor_oui(vendor_class, agent_priv->reg_req->oui);
1528 method = vendor_class->method_table[index];
1530 /* Remove any methods for this mad agent */
1531 remove_methods_mad_agent(method, agent_priv);
1533 * Now, check to see if there are
1534 * any methods still in use
1536 if (!check_method_table(method)) {
1537 /* If not, release management method table */
1539 vendor_class->method_table[index] = NULL;
1540 memset(vendor_class->oui[index], 0, 3);
1541 /* Any OUIs left ? */
1542 if (!check_vendor_class(vendor_class)) {
1543 /* If not, release vendor class table */
1544 kfree(vendor_class);
1545 vendor->vendor_class[mgmt_class] = NULL;
1546 /* Any other vendor classes left ? */
1547 if (!check_vendor_table(vendor)) {
1550 agent_priv->reg_req->
1551 mgmt_class_version].
1563 static struct ib_mad_agent_private *
1564 find_mad_agent(struct ib_mad_port_private *port_priv,
1567 struct ib_mad_agent_private *mad_agent = NULL;
1568 unsigned long flags;
1570 spin_lock_irqsave(&port_priv->reg_lock, flags);
1571 if (ib_response_mad(mad)) {
1573 struct ib_mad_agent_private *entry;
1576 * Routing is based on high 32 bits of transaction ID
1579 hi_tid = be64_to_cpu(mad->mad_hdr.tid) >> 32;
1580 list_for_each_entry(entry, &port_priv->agent_list, agent_list) {
1581 if (entry->agent.hi_tid == hi_tid) {
1587 struct ib_mad_mgmt_class_table *class;
1588 struct ib_mad_mgmt_method_table *method;
1589 struct ib_mad_mgmt_vendor_class_table *vendor;
1590 struct ib_mad_mgmt_vendor_class *vendor_class;
1591 struct ib_vendor_mad *vendor_mad;
1595 * Routing is based on version, class, and method
1596 * For "newer" vendor MADs, also based on OUI
1598 if (mad->mad_hdr.class_version >= MAX_MGMT_VERSION)
1600 if (!is_vendor_class(mad->mad_hdr.mgmt_class)) {
1601 class = port_priv->version[
1602 mad->mad_hdr.class_version].class;
1605 method = class->method_table[convert_mgmt_class(
1606 mad->mad_hdr.mgmt_class)];
1608 mad_agent = method->agent[mad->mad_hdr.method &
1609 ~IB_MGMT_METHOD_RESP];
1611 vendor = port_priv->version[
1612 mad->mad_hdr.class_version].vendor;
1615 vendor_class = vendor->vendor_class[vendor_class_index(
1616 mad->mad_hdr.mgmt_class)];
1619 /* Find matching OUI */
1620 vendor_mad = (struct ib_vendor_mad *)mad;
1621 index = find_vendor_oui(vendor_class, vendor_mad->oui);
1624 method = vendor_class->method_table[index];
1626 mad_agent = method->agent[mad->mad_hdr.method &
1627 ~IB_MGMT_METHOD_RESP];
1633 if (mad_agent->agent.recv_handler)
1634 atomic_inc(&mad_agent->refcount);
1636 printk(KERN_NOTICE PFX "No receive handler for client "
1638 &mad_agent->agent, port_priv->port_num);
1643 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
1648 static int validate_mad(struct ib_mad *mad, u32 qp_num)
1652 /* Make sure MAD base version is understood */
1653 if (mad->mad_hdr.base_version != IB_MGMT_BASE_VERSION) {
1654 printk(KERN_ERR PFX "MAD received with unsupported base "
1655 "version %d\n", mad->mad_hdr.base_version);
1659 /* Filter SMI packets sent to other than QP0 */
1660 if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
1661 (mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) {
1665 /* Filter GSI packets sent to QP0 */
1674 static int is_data_mad(struct ib_mad_agent_private *mad_agent_priv,
1675 struct ib_mad_hdr *mad_hdr)
1677 struct ib_rmpp_mad *rmpp_mad;
1679 rmpp_mad = (struct ib_rmpp_mad *)mad_hdr;
1680 return !mad_agent_priv->agent.rmpp_version ||
1681 !(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
1682 IB_MGMT_RMPP_FLAG_ACTIVE) ||
1683 (rmpp_mad->rmpp_hdr.rmpp_type == IB_MGMT_RMPP_TYPE_DATA);
1686 static inline int rcv_has_same_class(struct ib_mad_send_wr_private *wr,
1687 struct ib_mad_recv_wc *rwc)
1689 return ((struct ib_mad *)(wr->send_buf.mad))->mad_hdr.mgmt_class ==
1690 rwc->recv_buf.mad->mad_hdr.mgmt_class;
1693 static inline int rcv_has_same_gid(struct ib_mad_agent_private *mad_agent_priv,
1694 struct ib_mad_send_wr_private *wr,
1695 struct ib_mad_recv_wc *rwc )
1697 struct ib_ah_attr attr;
1698 u8 send_resp, rcv_resp;
1700 struct ib_device *device = mad_agent_priv->agent.device;
1701 u8 port_num = mad_agent_priv->agent.port_num;
1704 send_resp = ib_response_mad((struct ib_mad *)wr->send_buf.mad);
1705 rcv_resp = ib_response_mad(rwc->recv_buf.mad);
1707 if (send_resp == rcv_resp)
1708 /* both requests, or both responses. GIDs different */
1711 if (ib_query_ah(wr->send_buf.ah, &attr))
1712 /* Assume not equal, to avoid false positives. */
1715 if (!!(attr.ah_flags & IB_AH_GRH) !=
1716 !!(rwc->wc->wc_flags & IB_WC_GRH))
1717 /* one has GID, other does not. Assume different */
1720 if (!send_resp && rcv_resp) {
1721 /* is request/response. */
1722 if (!(attr.ah_flags & IB_AH_GRH)) {
1723 if (ib_get_cached_lmc(device, port_num, &lmc))
1725 return (!lmc || !((attr.src_path_bits ^
1726 rwc->wc->dlid_path_bits) &
1729 if (ib_get_cached_gid(device, port_num,
1730 attr.grh.sgid_index, &sgid))
1732 return !memcmp(sgid.raw, rwc->recv_buf.grh->dgid.raw,
1737 if (!(attr.ah_flags & IB_AH_GRH))
1738 return attr.dlid == rwc->wc->slid;
1740 return !memcmp(attr.grh.dgid.raw, rwc->recv_buf.grh->sgid.raw,
1744 static inline int is_direct(u8 class)
1746 return (class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE);
1749 struct ib_mad_send_wr_private*
1750 ib_find_send_mad(struct ib_mad_agent_private *mad_agent_priv,
1751 struct ib_mad_recv_wc *wc)
1753 struct ib_mad_send_wr_private *wr;
1756 mad = (struct ib_mad *)wc->recv_buf.mad;
1758 list_for_each_entry(wr, &mad_agent_priv->wait_list, agent_list) {
1759 if ((wr->tid == mad->mad_hdr.tid) &&
1760 rcv_has_same_class(wr, wc) &&
1762 * Don't check GID for direct routed MADs.
1763 * These might have permissive LIDs.
1765 (is_direct(wc->recv_buf.mad->mad_hdr.mgmt_class) ||
1766 rcv_has_same_gid(mad_agent_priv, wr, wc)))
1767 return (wr->status == IB_WC_SUCCESS) ? wr : NULL;
1771 * It's possible to receive the response before we've
1772 * been notified that the send has completed
1774 list_for_each_entry(wr, &mad_agent_priv->send_list, agent_list) {
1775 if (is_data_mad(mad_agent_priv, wr->send_buf.mad) &&
1776 wr->tid == mad->mad_hdr.tid &&
1778 rcv_has_same_class(wr, wc) &&
1780 * Don't check GID for direct routed MADs.
1781 * These might have permissive LIDs.
1783 (is_direct(wc->recv_buf.mad->mad_hdr.mgmt_class) ||
1784 rcv_has_same_gid(mad_agent_priv, wr, wc)))
1785 /* Verify request has not been canceled */
1786 return (wr->status == IB_WC_SUCCESS) ? wr : NULL;
1791 void ib_mark_mad_done(struct ib_mad_send_wr_private *mad_send_wr)
1793 mad_send_wr->timeout = 0;
1794 if (mad_send_wr->refcount == 1)
1795 list_move_tail(&mad_send_wr->agent_list,
1796 &mad_send_wr->mad_agent_priv->done_list);
1799 static void ib_mad_complete_recv(struct ib_mad_agent_private *mad_agent_priv,
1800 struct ib_mad_recv_wc *mad_recv_wc)
1802 struct ib_mad_send_wr_private *mad_send_wr;
1803 struct ib_mad_send_wc mad_send_wc;
1804 unsigned long flags;
1806 INIT_LIST_HEAD(&mad_recv_wc->rmpp_list);
1807 list_add(&mad_recv_wc->recv_buf.list, &mad_recv_wc->rmpp_list);
1808 if (mad_agent_priv->agent.rmpp_version) {
1809 mad_recv_wc = ib_process_rmpp_recv_wc(mad_agent_priv,
1812 deref_mad_agent(mad_agent_priv);
1817 /* Complete corresponding request */
1818 if (ib_response_mad(mad_recv_wc->recv_buf.mad)) {
1819 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1820 mad_send_wr = ib_find_send_mad(mad_agent_priv, mad_recv_wc);
1822 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1823 ib_free_recv_mad(mad_recv_wc);
1824 deref_mad_agent(mad_agent_priv);
1827 ib_mark_mad_done(mad_send_wr);
1828 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1830 /* Defined behavior is to complete response before request */
1831 mad_recv_wc->wc->wr_id = (unsigned long) &mad_send_wr->send_buf;
1832 mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent,
1834 atomic_dec(&mad_agent_priv->refcount);
1836 mad_send_wc.status = IB_WC_SUCCESS;
1837 mad_send_wc.vendor_err = 0;
1838 mad_send_wc.send_buf = &mad_send_wr->send_buf;
1839 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
1841 mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent,
1843 deref_mad_agent(mad_agent_priv);
1847 static void ib_mad_recv_done_handler(struct ib_mad_port_private *port_priv,
1850 struct ib_mad_qp_info *qp_info;
1851 struct ib_mad_private_header *mad_priv_hdr;
1852 struct ib_mad_private *recv, *response = NULL;
1853 struct ib_mad_list_head *mad_list;
1854 struct ib_mad_agent_private *mad_agent;
1857 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
1858 qp_info = mad_list->mad_queue->qp_info;
1859 dequeue_mad(mad_list);
1861 mad_priv_hdr = container_of(mad_list, struct ib_mad_private_header,
1863 recv = container_of(mad_priv_hdr, struct ib_mad_private, header);
1864 ib_dma_unmap_single(port_priv->device,
1865 recv->header.mapping,
1866 sizeof(struct ib_mad_private) -
1867 sizeof(struct ib_mad_private_header),
1870 /* Setup MAD receive work completion from "normal" work completion */
1871 recv->header.wc = *wc;
1872 recv->header.recv_wc.wc = &recv->header.wc;
1873 recv->header.recv_wc.mad_len = sizeof(struct ib_mad);
1874 recv->header.recv_wc.recv_buf.mad = &recv->mad.mad;
1875 recv->header.recv_wc.recv_buf.grh = &recv->grh;
1877 if (atomic_read(&qp_info->snoop_count))
1878 snoop_recv(qp_info, &recv->header.recv_wc, IB_MAD_SNOOP_RECVS);
1881 if (!validate_mad(&recv->mad.mad, qp_info->qp->qp_num))
1884 response = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL);
1886 printk(KERN_ERR PFX "ib_mad_recv_done_handler no memory "
1887 "for response buffer\n");
1891 if (port_priv->device->node_type == RDMA_NODE_IB_SWITCH)
1892 port_num = wc->port_num;
1894 port_num = port_priv->port_num;
1896 if (recv->mad.mad.mad_hdr.mgmt_class ==
1897 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
1898 enum smi_forward_action retsmi;
1900 if (smi_handle_dr_smp_recv(&recv->mad.smp,
1901 port_priv->device->node_type,
1903 port_priv->device->phys_port_cnt) ==
1907 retsmi = smi_check_forward_dr_smp(&recv->mad.smp);
1908 if (retsmi == IB_SMI_LOCAL)
1911 if (retsmi == IB_SMI_SEND) { /* don't forward */
1912 if (smi_handle_dr_smp_send(&recv->mad.smp,
1913 port_priv->device->node_type,
1914 port_num) == IB_SMI_DISCARD)
1917 if (smi_check_local_smp(&recv->mad.smp, port_priv->device) == IB_SMI_DISCARD)
1919 } else if (port_priv->device->node_type == RDMA_NODE_IB_SWITCH) {
1920 /* forward case for switches */
1921 memcpy(response, recv, sizeof(*response));
1922 response->header.recv_wc.wc = &response->header.wc;
1923 response->header.recv_wc.recv_buf.mad = &response->mad.mad;
1924 response->header.recv_wc.recv_buf.grh = &response->grh;
1926 agent_send_response(&response->mad.mad,
1929 smi_get_fwd_port(&recv->mad.smp),
1930 qp_info->qp->qp_num);
1937 /* Give driver "right of first refusal" on incoming MAD */
1938 if (port_priv->device->process_mad) {
1941 ret = port_priv->device->process_mad(port_priv->device, 0,
1942 port_priv->port_num,
1945 &response->mad.mad);
1946 if (ret & IB_MAD_RESULT_SUCCESS) {
1947 if (ret & IB_MAD_RESULT_CONSUMED)
1949 if (ret & IB_MAD_RESULT_REPLY) {
1950 agent_send_response(&response->mad.mad,
1954 qp_info->qp->qp_num);
1960 mad_agent = find_mad_agent(port_priv, &recv->mad.mad);
1962 ib_mad_complete_recv(mad_agent, &recv->header.recv_wc);
1964 * recv is freed up in error cases in ib_mad_complete_recv
1965 * or via recv_handler in ib_mad_complete_recv()
1971 /* Post another receive request for this QP */
1973 ib_mad_post_receive_mads(qp_info, response);
1975 kmem_cache_free(ib_mad_cache, recv);
1977 ib_mad_post_receive_mads(qp_info, recv);
1980 static void adjust_timeout(struct ib_mad_agent_private *mad_agent_priv)
1982 struct ib_mad_send_wr_private *mad_send_wr;
1983 unsigned long delay;
1985 if (list_empty(&mad_agent_priv->wait_list)) {
1986 __cancel_delayed_work(&mad_agent_priv->timed_work);
1988 mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
1989 struct ib_mad_send_wr_private,
1992 if (time_after(mad_agent_priv->timeout,
1993 mad_send_wr->timeout)) {
1994 mad_agent_priv->timeout = mad_send_wr->timeout;
1995 __cancel_delayed_work(&mad_agent_priv->timed_work);
1996 delay = mad_send_wr->timeout - jiffies;
1997 if ((long)delay <= 0)
1999 queue_delayed_work(mad_agent_priv->qp_info->
2001 &mad_agent_priv->timed_work, delay);
2006 static void wait_for_response(struct ib_mad_send_wr_private *mad_send_wr)
2008 struct ib_mad_agent_private *mad_agent_priv;
2009 struct ib_mad_send_wr_private *temp_mad_send_wr;
2010 struct list_head *list_item;
2011 unsigned long delay;
2013 mad_agent_priv = mad_send_wr->mad_agent_priv;
2014 list_del(&mad_send_wr->agent_list);
2016 delay = mad_send_wr->timeout;
2017 mad_send_wr->timeout += jiffies;
2020 list_for_each_prev(list_item, &mad_agent_priv->wait_list) {
2021 temp_mad_send_wr = list_entry(list_item,
2022 struct ib_mad_send_wr_private,
2024 if (time_after(mad_send_wr->timeout,
2025 temp_mad_send_wr->timeout))
2030 list_item = &mad_agent_priv->wait_list;
2031 list_add(&mad_send_wr->agent_list, list_item);
2033 /* Reschedule a work item if we have a shorter timeout */
2034 if (mad_agent_priv->wait_list.next == &mad_send_wr->agent_list) {
2035 __cancel_delayed_work(&mad_agent_priv->timed_work);
2036 queue_delayed_work(mad_agent_priv->qp_info->port_priv->wq,
2037 &mad_agent_priv->timed_work, delay);
2041 void ib_reset_mad_timeout(struct ib_mad_send_wr_private *mad_send_wr,
2044 mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
2045 wait_for_response(mad_send_wr);
2049 * Process a send work completion
2051 void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr,
2052 struct ib_mad_send_wc *mad_send_wc)
2054 struct ib_mad_agent_private *mad_agent_priv;
2055 unsigned long flags;
2058 mad_agent_priv = mad_send_wr->mad_agent_priv;
2059 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2060 if (mad_agent_priv->agent.rmpp_version) {
2061 ret = ib_process_rmpp_send_wc(mad_send_wr, mad_send_wc);
2062 if (ret == IB_RMPP_RESULT_CONSUMED)
2065 ret = IB_RMPP_RESULT_UNHANDLED;
2067 if (mad_send_wc->status != IB_WC_SUCCESS &&
2068 mad_send_wr->status == IB_WC_SUCCESS) {
2069 mad_send_wr->status = mad_send_wc->status;
2070 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2073 if (--mad_send_wr->refcount > 0) {
2074 if (mad_send_wr->refcount == 1 && mad_send_wr->timeout &&
2075 mad_send_wr->status == IB_WC_SUCCESS) {
2076 wait_for_response(mad_send_wr);
2081 /* Remove send from MAD agent and notify client of completion */
2082 list_del(&mad_send_wr->agent_list);
2083 adjust_timeout(mad_agent_priv);
2084 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2086 if (mad_send_wr->status != IB_WC_SUCCESS )
2087 mad_send_wc->status = mad_send_wr->status;
2088 if (ret == IB_RMPP_RESULT_INTERNAL)
2089 ib_rmpp_send_handler(mad_send_wc);
2091 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2094 /* Release reference on agent taken when sending */
2095 deref_mad_agent(mad_agent_priv);
2098 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2101 static void ib_mad_send_done_handler(struct ib_mad_port_private *port_priv,
2104 struct ib_mad_send_wr_private *mad_send_wr, *queued_send_wr;
2105 struct ib_mad_list_head *mad_list;
2106 struct ib_mad_qp_info *qp_info;
2107 struct ib_mad_queue *send_queue;
2108 struct ib_send_wr *bad_send_wr;
2109 struct ib_mad_send_wc mad_send_wc;
2110 unsigned long flags;
2113 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
2114 mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
2116 send_queue = mad_list->mad_queue;
2117 qp_info = send_queue->qp_info;
2120 ib_dma_unmap_single(mad_send_wr->send_buf.mad_agent->device,
2121 mad_send_wr->header_mapping,
2122 mad_send_wr->sg_list[0].length, DMA_TO_DEVICE);
2123 ib_dma_unmap_single(mad_send_wr->send_buf.mad_agent->device,
2124 mad_send_wr->payload_mapping,
2125 mad_send_wr->sg_list[1].length, DMA_TO_DEVICE);
2126 queued_send_wr = NULL;
2127 spin_lock_irqsave(&send_queue->lock, flags);
2128 list_del(&mad_list->list);
2130 /* Move queued send to the send queue */
2131 if (send_queue->count-- > send_queue->max_active) {
2132 mad_list = container_of(qp_info->overflow_list.next,
2133 struct ib_mad_list_head, list);
2134 queued_send_wr = container_of(mad_list,
2135 struct ib_mad_send_wr_private,
2137 list_move_tail(&mad_list->list, &send_queue->list);
2139 spin_unlock_irqrestore(&send_queue->lock, flags);
2141 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2142 mad_send_wc.status = wc->status;
2143 mad_send_wc.vendor_err = wc->vendor_err;
2144 if (atomic_read(&qp_info->snoop_count))
2145 snoop_send(qp_info, &mad_send_wr->send_buf, &mad_send_wc,
2146 IB_MAD_SNOOP_SEND_COMPLETIONS);
2147 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
2149 if (queued_send_wr) {
2150 ret = ib_post_send(qp_info->qp, &queued_send_wr->send_wr,
2153 printk(KERN_ERR PFX "ib_post_send failed: %d\n", ret);
2154 mad_send_wr = queued_send_wr;
2155 wc->status = IB_WC_LOC_QP_OP_ERR;
2161 static void mark_sends_for_retry(struct ib_mad_qp_info *qp_info)
2163 struct ib_mad_send_wr_private *mad_send_wr;
2164 struct ib_mad_list_head *mad_list;
2165 unsigned long flags;
2167 spin_lock_irqsave(&qp_info->send_queue.lock, flags);
2168 list_for_each_entry(mad_list, &qp_info->send_queue.list, list) {
2169 mad_send_wr = container_of(mad_list,
2170 struct ib_mad_send_wr_private,
2172 mad_send_wr->retry = 1;
2174 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
2177 static void mad_error_handler(struct ib_mad_port_private *port_priv,
2180 struct ib_mad_list_head *mad_list;
2181 struct ib_mad_qp_info *qp_info;
2182 struct ib_mad_send_wr_private *mad_send_wr;
2185 /* Determine if failure was a send or receive */
2186 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
2187 qp_info = mad_list->mad_queue->qp_info;
2188 if (mad_list->mad_queue == &qp_info->recv_queue)
2190 * Receive errors indicate that the QP has entered the error
2191 * state - error handling/shutdown code will cleanup
2196 * Send errors will transition the QP to SQE - move
2197 * QP to RTS and repost flushed work requests
2199 mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
2201 if (wc->status == IB_WC_WR_FLUSH_ERR) {
2202 if (mad_send_wr->retry) {
2204 struct ib_send_wr *bad_send_wr;
2206 mad_send_wr->retry = 0;
2207 ret = ib_post_send(qp_info->qp, &mad_send_wr->send_wr,
2210 ib_mad_send_done_handler(port_priv, wc);
2212 ib_mad_send_done_handler(port_priv, wc);
2214 struct ib_qp_attr *attr;
2216 /* Transition QP to RTS and fail offending send */
2217 attr = kmalloc(sizeof *attr, GFP_KERNEL);
2219 attr->qp_state = IB_QPS_RTS;
2220 attr->cur_qp_state = IB_QPS_SQE;
2221 ret = ib_modify_qp(qp_info->qp, attr,
2222 IB_QP_STATE | IB_QP_CUR_STATE);
2225 printk(KERN_ERR PFX "mad_error_handler - "
2226 "ib_modify_qp to RTS : %d\n", ret);
2228 mark_sends_for_retry(qp_info);
2230 ib_mad_send_done_handler(port_priv, wc);
2235 * IB MAD completion callback
2237 static void ib_mad_completion_handler(struct work_struct *work)
2239 struct ib_mad_port_private *port_priv;
2242 port_priv = container_of(work, struct ib_mad_port_private, work);
2243 ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
2245 while (ib_poll_cq(port_priv->cq, 1, &wc) == 1) {
2246 if (wc.status == IB_WC_SUCCESS) {
2247 switch (wc.opcode) {
2249 ib_mad_send_done_handler(port_priv, &wc);
2252 ib_mad_recv_done_handler(port_priv, &wc);
2259 mad_error_handler(port_priv, &wc);
2263 static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv)
2265 unsigned long flags;
2266 struct ib_mad_send_wr_private *mad_send_wr, *temp_mad_send_wr;
2267 struct ib_mad_send_wc mad_send_wc;
2268 struct list_head cancel_list;
2270 INIT_LIST_HEAD(&cancel_list);
2272 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2273 list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
2274 &mad_agent_priv->send_list, agent_list) {
2275 if (mad_send_wr->status == IB_WC_SUCCESS) {
2276 mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
2277 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2281 /* Empty wait list to prevent receives from finding a request */
2282 list_splice_init(&mad_agent_priv->wait_list, &cancel_list);
2283 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2285 /* Report all cancelled requests */
2286 mad_send_wc.status = IB_WC_WR_FLUSH_ERR;
2287 mad_send_wc.vendor_err = 0;
2289 list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
2290 &cancel_list, agent_list) {
2291 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2292 list_del(&mad_send_wr->agent_list);
2293 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2295 atomic_dec(&mad_agent_priv->refcount);
2299 static struct ib_mad_send_wr_private*
2300 find_send_wr(struct ib_mad_agent_private *mad_agent_priv,
2301 struct ib_mad_send_buf *send_buf)
2303 struct ib_mad_send_wr_private *mad_send_wr;
2305 list_for_each_entry(mad_send_wr, &mad_agent_priv->wait_list,
2307 if (&mad_send_wr->send_buf == send_buf)
2311 list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list,
2313 if (is_data_mad(mad_agent_priv, mad_send_wr->send_buf.mad) &&
2314 &mad_send_wr->send_buf == send_buf)
2320 int ib_modify_mad(struct ib_mad_agent *mad_agent,
2321 struct ib_mad_send_buf *send_buf, u32 timeout_ms)
2323 struct ib_mad_agent_private *mad_agent_priv;
2324 struct ib_mad_send_wr_private *mad_send_wr;
2325 unsigned long flags;
2328 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
2330 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2331 mad_send_wr = find_send_wr(mad_agent_priv, send_buf);
2332 if (!mad_send_wr || mad_send_wr->status != IB_WC_SUCCESS) {
2333 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2337 active = (!mad_send_wr->timeout || mad_send_wr->refcount > 1);
2339 mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
2340 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2343 mad_send_wr->send_buf.timeout_ms = timeout_ms;
2345 mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
2347 ib_reset_mad_timeout(mad_send_wr, timeout_ms);
2349 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2352 EXPORT_SYMBOL(ib_modify_mad);
2354 void ib_cancel_mad(struct ib_mad_agent *mad_agent,
2355 struct ib_mad_send_buf *send_buf)
2357 ib_modify_mad(mad_agent, send_buf, 0);
2359 EXPORT_SYMBOL(ib_cancel_mad);
2361 static void local_completions(struct work_struct *work)
2363 struct ib_mad_agent_private *mad_agent_priv;
2364 struct ib_mad_local_private *local;
2365 struct ib_mad_agent_private *recv_mad_agent;
2366 unsigned long flags;
2369 struct ib_mad_send_wc mad_send_wc;
2372 container_of(work, struct ib_mad_agent_private, local_work);
2374 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2375 while (!list_empty(&mad_agent_priv->local_list)) {
2376 local = list_entry(mad_agent_priv->local_list.next,
2377 struct ib_mad_local_private,
2379 list_del(&local->completion_list);
2380 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2382 if (local->mad_priv) {
2383 recv_mad_agent = local->recv_mad_agent;
2384 if (!recv_mad_agent) {
2385 printk(KERN_ERR PFX "No receive MAD agent for local completion\n");
2387 goto local_send_completion;
2391 * Defined behavior is to complete response
2394 build_smp_wc(recv_mad_agent->agent.qp,
2395 (unsigned long) local->mad_send_wr,
2396 be16_to_cpu(IB_LID_PERMISSIVE),
2397 0, recv_mad_agent->agent.port_num, &wc);
2399 local->mad_priv->header.recv_wc.wc = &wc;
2400 local->mad_priv->header.recv_wc.mad_len =
2401 sizeof(struct ib_mad);
2402 INIT_LIST_HEAD(&local->mad_priv->header.recv_wc.rmpp_list);
2403 list_add(&local->mad_priv->header.recv_wc.recv_buf.list,
2404 &local->mad_priv->header.recv_wc.rmpp_list);
2405 local->mad_priv->header.recv_wc.recv_buf.grh = NULL;
2406 local->mad_priv->header.recv_wc.recv_buf.mad =
2407 &local->mad_priv->mad.mad;
2408 if (atomic_read(&recv_mad_agent->qp_info->snoop_count))
2409 snoop_recv(recv_mad_agent->qp_info,
2410 &local->mad_priv->header.recv_wc,
2411 IB_MAD_SNOOP_RECVS);
2412 recv_mad_agent->agent.recv_handler(
2413 &recv_mad_agent->agent,
2414 &local->mad_priv->header.recv_wc);
2415 spin_lock_irqsave(&recv_mad_agent->lock, flags);
2416 atomic_dec(&recv_mad_agent->refcount);
2417 spin_unlock_irqrestore(&recv_mad_agent->lock, flags);
2420 local_send_completion:
2422 mad_send_wc.status = IB_WC_SUCCESS;
2423 mad_send_wc.vendor_err = 0;
2424 mad_send_wc.send_buf = &local->mad_send_wr->send_buf;
2425 if (atomic_read(&mad_agent_priv->qp_info->snoop_count))
2426 snoop_send(mad_agent_priv->qp_info,
2427 &local->mad_send_wr->send_buf,
2428 &mad_send_wc, IB_MAD_SNOOP_SEND_COMPLETIONS);
2429 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2432 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2433 atomic_dec(&mad_agent_priv->refcount);
2435 kmem_cache_free(ib_mad_cache, local->mad_priv);
2438 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2441 static int retry_send(struct ib_mad_send_wr_private *mad_send_wr)
2445 if (!mad_send_wr->retries_left)
2448 mad_send_wr->retries_left--;
2449 mad_send_wr->send_buf.retries++;
2451 mad_send_wr->timeout = msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms);
2453 if (mad_send_wr->mad_agent_priv->agent.rmpp_version) {
2454 ret = ib_retry_rmpp(mad_send_wr);
2456 case IB_RMPP_RESULT_UNHANDLED:
2457 ret = ib_send_mad(mad_send_wr);
2459 case IB_RMPP_RESULT_CONSUMED:
2467 ret = ib_send_mad(mad_send_wr);
2470 mad_send_wr->refcount++;
2471 list_add_tail(&mad_send_wr->agent_list,
2472 &mad_send_wr->mad_agent_priv->send_list);
2477 static void timeout_sends(struct work_struct *work)
2479 struct ib_mad_agent_private *mad_agent_priv;
2480 struct ib_mad_send_wr_private *mad_send_wr;
2481 struct ib_mad_send_wc mad_send_wc;
2482 unsigned long flags, delay;
2484 mad_agent_priv = container_of(work, struct ib_mad_agent_private,
2486 mad_send_wc.vendor_err = 0;
2488 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2489 while (!list_empty(&mad_agent_priv->wait_list)) {
2490 mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
2491 struct ib_mad_send_wr_private,
2494 if (time_after(mad_send_wr->timeout, jiffies)) {
2495 delay = mad_send_wr->timeout - jiffies;
2496 if ((long)delay <= 0)
2498 queue_delayed_work(mad_agent_priv->qp_info->
2500 &mad_agent_priv->timed_work, delay);
2504 list_del(&mad_send_wr->agent_list);
2505 if (mad_send_wr->status == IB_WC_SUCCESS &&
2506 !retry_send(mad_send_wr))
2509 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2511 if (mad_send_wr->status == IB_WC_SUCCESS)
2512 mad_send_wc.status = IB_WC_RESP_TIMEOUT_ERR;
2514 mad_send_wc.status = mad_send_wr->status;
2515 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2516 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2519 atomic_dec(&mad_agent_priv->refcount);
2520 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2522 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2525 static void ib_mad_thread_completion_handler(struct ib_cq *cq, void *arg)
2527 struct ib_mad_port_private *port_priv = cq->cq_context;
2528 unsigned long flags;
2530 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2531 if (!list_empty(&port_priv->port_list))
2532 queue_work(port_priv->wq, &port_priv->work);
2533 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2537 * Allocate receive MADs and post receive WRs for them
2539 static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
2540 struct ib_mad_private *mad)
2542 unsigned long flags;
2544 struct ib_mad_private *mad_priv;
2545 struct ib_sge sg_list;
2546 struct ib_recv_wr recv_wr, *bad_recv_wr;
2547 struct ib_mad_queue *recv_queue = &qp_info->recv_queue;
2549 /* Initialize common scatter list fields */
2550 sg_list.length = sizeof *mad_priv - sizeof mad_priv->header;
2551 sg_list.lkey = (*qp_info->port_priv->mr).lkey;
2553 /* Initialize common receive WR fields */
2554 recv_wr.next = NULL;
2555 recv_wr.sg_list = &sg_list;
2556 recv_wr.num_sge = 1;
2559 /* Allocate and map receive buffer */
2564 mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL);
2566 printk(KERN_ERR PFX "No memory for receive buffer\n");
2571 sg_list.addr = ib_dma_map_single(qp_info->port_priv->device,
2574 sizeof mad_priv->header,
2576 mad_priv->header.mapping = sg_list.addr;
2577 recv_wr.wr_id = (unsigned long)&mad_priv->header.mad_list;
2578 mad_priv->header.mad_list.mad_queue = recv_queue;
2580 /* Post receive WR */
2581 spin_lock_irqsave(&recv_queue->lock, flags);
2582 post = (++recv_queue->count < recv_queue->max_active);
2583 list_add_tail(&mad_priv->header.mad_list.list, &recv_queue->list);
2584 spin_unlock_irqrestore(&recv_queue->lock, flags);
2585 ret = ib_post_recv(qp_info->qp, &recv_wr, &bad_recv_wr);
2587 spin_lock_irqsave(&recv_queue->lock, flags);
2588 list_del(&mad_priv->header.mad_list.list);
2589 recv_queue->count--;
2590 spin_unlock_irqrestore(&recv_queue->lock, flags);
2591 ib_dma_unmap_single(qp_info->port_priv->device,
2592 mad_priv->header.mapping,
2594 sizeof mad_priv->header,
2596 kmem_cache_free(ib_mad_cache, mad_priv);
2597 printk(KERN_ERR PFX "ib_post_recv failed: %d\n", ret);
2606 * Return all the posted receive MADs
2608 static void cleanup_recv_queue(struct ib_mad_qp_info *qp_info)
2610 struct ib_mad_private_header *mad_priv_hdr;
2611 struct ib_mad_private *recv;
2612 struct ib_mad_list_head *mad_list;
2614 while (!list_empty(&qp_info->recv_queue.list)) {
2616 mad_list = list_entry(qp_info->recv_queue.list.next,
2617 struct ib_mad_list_head, list);
2618 mad_priv_hdr = container_of(mad_list,
2619 struct ib_mad_private_header,
2621 recv = container_of(mad_priv_hdr, struct ib_mad_private,
2624 /* Remove from posted receive MAD list */
2625 list_del(&mad_list->list);
2627 ib_dma_unmap_single(qp_info->port_priv->device,
2628 recv->header.mapping,
2629 sizeof(struct ib_mad_private) -
2630 sizeof(struct ib_mad_private_header),
2632 kmem_cache_free(ib_mad_cache, recv);
2635 qp_info->recv_queue.count = 0;
2641 static int ib_mad_port_start(struct ib_mad_port_private *port_priv)
2644 struct ib_qp_attr *attr;
2647 attr = kmalloc(sizeof *attr, GFP_KERNEL);
2649 printk(KERN_ERR PFX "Couldn't kmalloc ib_qp_attr\n");
2653 for (i = 0; i < IB_MAD_QPS_CORE; i++) {
2654 qp = port_priv->qp_info[i].qp;
2656 * PKey index for QP1 is irrelevant but
2657 * one is needed for the Reset to Init transition
2659 attr->qp_state = IB_QPS_INIT;
2660 attr->pkey_index = 0;
2661 attr->qkey = (qp->qp_num == 0) ? 0 : IB_QP1_QKEY;
2662 ret = ib_modify_qp(qp, attr, IB_QP_STATE |
2663 IB_QP_PKEY_INDEX | IB_QP_QKEY);
2665 printk(KERN_ERR PFX "Couldn't change QP%d state to "
2666 "INIT: %d\n", i, ret);
2670 attr->qp_state = IB_QPS_RTR;
2671 ret = ib_modify_qp(qp, attr, IB_QP_STATE);
2673 printk(KERN_ERR PFX "Couldn't change QP%d state to "
2674 "RTR: %d\n", i, ret);
2678 attr->qp_state = IB_QPS_RTS;
2679 attr->sq_psn = IB_MAD_SEND_Q_PSN;
2680 ret = ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_SQ_PSN);
2682 printk(KERN_ERR PFX "Couldn't change QP%d state to "
2683 "RTS: %d\n", i, ret);
2688 ret = ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
2690 printk(KERN_ERR PFX "Failed to request completion "
2691 "notification: %d\n", ret);
2695 for (i = 0; i < IB_MAD_QPS_CORE; i++) {
2696 ret = ib_mad_post_receive_mads(&port_priv->qp_info[i], NULL);
2698 printk(KERN_ERR PFX "Couldn't post receive WRs\n");
2707 static void qp_event_handler(struct ib_event *event, void *qp_context)
2709 struct ib_mad_qp_info *qp_info = qp_context;
2711 /* It's worse than that! He's dead, Jim! */
2712 printk(KERN_ERR PFX "Fatal error (%d) on MAD QP (%d)\n",
2713 event->event, qp_info->qp->qp_num);
2716 static void init_mad_queue(struct ib_mad_qp_info *qp_info,
2717 struct ib_mad_queue *mad_queue)
2719 mad_queue->qp_info = qp_info;
2720 mad_queue->count = 0;
2721 spin_lock_init(&mad_queue->lock);
2722 INIT_LIST_HEAD(&mad_queue->list);
2725 static void init_mad_qp(struct ib_mad_port_private *port_priv,
2726 struct ib_mad_qp_info *qp_info)
2728 qp_info->port_priv = port_priv;
2729 init_mad_queue(qp_info, &qp_info->send_queue);
2730 init_mad_queue(qp_info, &qp_info->recv_queue);
2731 INIT_LIST_HEAD(&qp_info->overflow_list);
2732 spin_lock_init(&qp_info->snoop_lock);
2733 qp_info->snoop_table = NULL;
2734 qp_info->snoop_table_size = 0;
2735 atomic_set(&qp_info->snoop_count, 0);
2738 static int create_mad_qp(struct ib_mad_qp_info *qp_info,
2739 enum ib_qp_type qp_type)
2741 struct ib_qp_init_attr qp_init_attr;
2744 memset(&qp_init_attr, 0, sizeof qp_init_attr);
2745 qp_init_attr.send_cq = qp_info->port_priv->cq;
2746 qp_init_attr.recv_cq = qp_info->port_priv->cq;
2747 qp_init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
2748 qp_init_attr.cap.max_send_wr = mad_sendq_size;
2749 qp_init_attr.cap.max_recv_wr = mad_recvq_size;
2750 qp_init_attr.cap.max_send_sge = IB_MAD_SEND_REQ_MAX_SG;
2751 qp_init_attr.cap.max_recv_sge = IB_MAD_RECV_REQ_MAX_SG;
2752 qp_init_attr.qp_type = qp_type;
2753 qp_init_attr.port_num = qp_info->port_priv->port_num;
2754 qp_init_attr.qp_context = qp_info;
2755 qp_init_attr.event_handler = qp_event_handler;
2756 qp_info->qp = ib_create_qp(qp_info->port_priv->pd, &qp_init_attr);
2757 if (IS_ERR(qp_info->qp)) {
2758 printk(KERN_ERR PFX "Couldn't create ib_mad QP%d\n",
2759 get_spl_qp_index(qp_type));
2760 ret = PTR_ERR(qp_info->qp);
2763 /* Use minimum queue sizes unless the CQ is resized */
2764 qp_info->send_queue.max_active = mad_sendq_size;
2765 qp_info->recv_queue.max_active = mad_recvq_size;
2772 static void destroy_mad_qp(struct ib_mad_qp_info *qp_info)
2774 ib_destroy_qp(qp_info->qp);
2775 kfree(qp_info->snoop_table);
2780 * Create the QP, PD, MR, and CQ if needed
2782 static int ib_mad_port_open(struct ib_device *device,
2786 struct ib_mad_port_private *port_priv;
2787 unsigned long flags;
2788 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) * 2;
2805 port_priv->cq = ib_create_cq(port_priv->device,
2806 ib_mad_thread_completion_handler,
2807 NULL, port_priv, cq_size, 0);
2808 if (IS_ERR(port_priv->cq)) {
2809 printk(KERN_ERR PFX "Couldn't create ib_mad CQ\n");
2810 ret = PTR_ERR(port_priv->cq);
2814 port_priv->pd = ib_alloc_pd(device);
2815 if (IS_ERR(port_priv->pd)) {
2816 printk(KERN_ERR PFX "Couldn't create ib_mad PD\n");
2817 ret = PTR_ERR(port_priv->pd);
2821 port_priv->mr = ib_get_dma_mr(port_priv->pd, IB_ACCESS_LOCAL_WRITE);
2822 if (IS_ERR(port_priv->mr)) {
2823 printk(KERN_ERR PFX "Couldn't get ib_mad DMA MR\n");
2824 ret = PTR_ERR(port_priv->mr);
2828 ret = create_mad_qp(&port_priv->qp_info[0], IB_QPT_SMI);
2831 ret = create_mad_qp(&port_priv->qp_info[1], IB_QPT_GSI);
2835 snprintf(name, sizeof name, "ib_mad%d", port_num);
2836 port_priv->wq = create_singlethread_workqueue(name);
2837 if (!port_priv->wq) {
2841 INIT_WORK(&port_priv->work, ib_mad_completion_handler);
2843 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2844 list_add_tail(&port_priv->port_list, &ib_mad_port_list);
2845 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2847 ret = ib_mad_port_start(port_priv);
2849 printk(KERN_ERR PFX "Couldn't start port\n");
2856 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2857 list_del_init(&port_priv->port_list);
2858 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2860 destroy_workqueue(port_priv->wq);
2862 destroy_mad_qp(&port_priv->qp_info[1]);
2864 destroy_mad_qp(&port_priv->qp_info[0]);
2866 ib_dereg_mr(port_priv->mr);
2868 ib_dealloc_pd(port_priv->pd);
2870 ib_destroy_cq(port_priv->cq);
2871 cleanup_recv_queue(&port_priv->qp_info[1]);
2872 cleanup_recv_queue(&port_priv->qp_info[0]);
2881 * If there are no classes using the port, free the port
2882 * resources (CQ, MR, PD, QP) and remove the port's info structure
2884 static int ib_mad_port_close(struct ib_device *device, int port_num)
2886 struct ib_mad_port_private *port_priv;
2887 unsigned long flags;
2889 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2890 port_priv = __ib_get_mad_port(device, port_num);
2891 if (port_priv == NULL) {
2892 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2893 printk(KERN_ERR PFX "Port %d not found\n", port_num);
2896 list_del_init(&port_priv->port_list);
2897 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2899 destroy_workqueue(port_priv->wq);
2900 destroy_mad_qp(&port_priv->qp_info[1]);
2901 destroy_mad_qp(&port_priv->qp_info[0]);
2902 ib_dereg_mr(port_priv->mr);
2903 ib_dealloc_pd(port_priv->pd);
2904 ib_destroy_cq(port_priv->cq);
2905 cleanup_recv_queue(&port_priv->qp_info[1]);
2906 cleanup_recv_queue(&port_priv->qp_info[0]);
2907 /* XXX: Handle deallocation of MAD registration tables */
2914 static void ib_mad_init_device(struct ib_device *device)
2918 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
2921 if (device->node_type == RDMA_NODE_IB_SWITCH) {
2926 end = device->phys_port_cnt;
2929 for (i = start; i <= end; i++) {
2930 if (ib_mad_port_open(device, i)) {
2931 printk(KERN_ERR PFX "Couldn't open %s port %d\n",
2935 if (ib_agent_port_open(device, i)) {
2936 printk(KERN_ERR PFX "Couldn't open %s port %d "
2945 if (ib_mad_port_close(device, i))
2946 printk(KERN_ERR PFX "Couldn't close %s port %d\n",
2952 while (i >= start) {
2953 if (ib_agent_port_close(device, i))
2954 printk(KERN_ERR PFX "Couldn't close %s port %d "
2957 if (ib_mad_port_close(device, i))
2958 printk(KERN_ERR PFX "Couldn't close %s port %d\n",
2964 static void ib_mad_remove_device(struct ib_device *device)
2966 int i, num_ports, cur_port;
2968 if (device->node_type == RDMA_NODE_IB_SWITCH) {
2972 num_ports = device->phys_port_cnt;
2975 for (i = 0; i < num_ports; i++, cur_port++) {
2976 if (ib_agent_port_close(device, cur_port))
2977 printk(KERN_ERR PFX "Couldn't close %s port %d "
2979 device->name, cur_port);
2980 if (ib_mad_port_close(device, cur_port))
2981 printk(KERN_ERR PFX "Couldn't close %s port %d\n",
2982 device->name, cur_port);
2986 static struct ib_client mad_client = {
2988 .add = ib_mad_init_device,
2989 .remove = ib_mad_remove_device
2992 static int __init ib_mad_init_module(void)
2996 mad_recvq_size = min(mad_recvq_size, IB_MAD_QP_MAX_SIZE);
2997 mad_recvq_size = max(mad_recvq_size, IB_MAD_QP_MIN_SIZE);
2999 mad_sendq_size = min(mad_sendq_size, IB_MAD_QP_MAX_SIZE);
3000 mad_sendq_size = max(mad_sendq_size, IB_MAD_QP_MIN_SIZE);
3002 spin_lock_init(&ib_mad_port_list_lock);
3004 ib_mad_cache = kmem_cache_create("ib_mad",
3005 sizeof(struct ib_mad_private),
3009 if (!ib_mad_cache) {
3010 printk(KERN_ERR PFX "Couldn't create ib_mad cache\n");
3015 INIT_LIST_HEAD(&ib_mad_port_list);
3017 if (ib_register_client(&mad_client)) {
3018 printk(KERN_ERR PFX "Couldn't register ib_mad client\n");
3026 kmem_cache_destroy(ib_mad_cache);
3031 static void __exit ib_mad_cleanup_module(void)
3033 ib_unregister_client(&mad_client);
3034 kmem_cache_destroy(ib_mad_cache);
3037 module_init(ib_mad_init_module);
3038 module_exit(ib_mad_cleanup_module);