2 * Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved.
3 * Copyright (c) 2005 Intel Corporation. All rights reserved.
4 * Copyright (c) 2005 Mellanox Technologies Ltd. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 * $Id: mad.c 5596 2006-03-03 01:00:07Z sean.hefty $
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 static kmem_cache_t *ib_mad_cache;
51 static struct list_head ib_mad_port_list;
52 static u32 ib_mad_client_id = 0;
55 static spinlock_t ib_mad_port_list_lock;
58 /* Forward declarations */
59 static int method_in_use(struct ib_mad_mgmt_method_table **method,
60 struct ib_mad_reg_req *mad_reg_req);
61 static void remove_mad_reg_req(struct ib_mad_agent_private *priv);
62 static struct ib_mad_agent_private *find_mad_agent(
63 struct ib_mad_port_private *port_priv,
65 static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
66 struct ib_mad_private *mad);
67 static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv);
68 static void timeout_sends(void *data);
69 static void local_completions(void *data);
70 static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
71 struct ib_mad_agent_private *agent_priv,
73 static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
74 struct ib_mad_agent_private *agent_priv);
77 * Returns a ib_mad_port_private structure or NULL for a device/port
78 * Assumes ib_mad_port_list_lock is being held
80 static inline struct ib_mad_port_private *
81 __ib_get_mad_port(struct ib_device *device, int port_num)
83 struct ib_mad_port_private *entry;
85 list_for_each_entry(entry, &ib_mad_port_list, port_list) {
86 if (entry->device == device && entry->port_num == port_num)
93 * Wrapper function to return a ib_mad_port_private structure or NULL
96 static inline struct ib_mad_port_private *
97 ib_get_mad_port(struct ib_device *device, int port_num)
99 struct ib_mad_port_private *entry;
102 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
103 entry = __ib_get_mad_port(device, port_num);
104 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
109 static inline u8 convert_mgmt_class(u8 mgmt_class)
111 /* Alias IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE to 0 */
112 return mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE ?
116 static int get_spl_qp_index(enum ib_qp_type qp_type)
129 static int vendor_class_index(u8 mgmt_class)
131 return mgmt_class - IB_MGMT_CLASS_VENDOR_RANGE2_START;
134 static int is_vendor_class(u8 mgmt_class)
136 if ((mgmt_class < IB_MGMT_CLASS_VENDOR_RANGE2_START) ||
137 (mgmt_class > IB_MGMT_CLASS_VENDOR_RANGE2_END))
142 static int is_vendor_oui(char *oui)
144 if (oui[0] || oui[1] || oui[2])
149 static int is_vendor_method_in_use(
150 struct ib_mad_mgmt_vendor_class *vendor_class,
151 struct ib_mad_reg_req *mad_reg_req)
153 struct ib_mad_mgmt_method_table *method;
156 for (i = 0; i < MAX_MGMT_OUI; i++) {
157 if (!memcmp(vendor_class->oui[i], mad_reg_req->oui, 3)) {
158 method = vendor_class->method_table[i];
160 if (method_in_use(&method, mad_reg_req))
170 int ib_response_mad(struct ib_mad *mad)
172 return ((mad->mad_hdr.method & IB_MGMT_METHOD_RESP) ||
173 (mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS) ||
174 ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_BM) &&
175 (mad->mad_hdr.attr_mod & IB_BM_ATTR_MOD_RESP)));
177 EXPORT_SYMBOL(ib_response_mad);
180 * ib_register_mad_agent - Register to send/receive MADs
182 struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
184 enum ib_qp_type qp_type,
185 struct ib_mad_reg_req *mad_reg_req,
187 ib_mad_send_handler send_handler,
188 ib_mad_recv_handler recv_handler,
191 struct ib_mad_port_private *port_priv;
192 struct ib_mad_agent *ret = ERR_PTR(-EINVAL);
193 struct ib_mad_agent_private *mad_agent_priv;
194 struct ib_mad_reg_req *reg_req = NULL;
195 struct ib_mad_mgmt_class_table *class;
196 struct ib_mad_mgmt_vendor_class_table *vendor;
197 struct ib_mad_mgmt_vendor_class *vendor_class;
198 struct ib_mad_mgmt_method_table *method;
201 u8 mgmt_class, vclass;
203 /* Validate parameters */
204 qpn = get_spl_qp_index(qp_type);
208 if (rmpp_version && rmpp_version != IB_MGMT_RMPP_VERSION)
211 /* Validate MAD registration request if supplied */
213 if (mad_reg_req->mgmt_class_version >= MAX_MGMT_VERSION)
217 if (mad_reg_req->mgmt_class >= MAX_MGMT_CLASS) {
219 * IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE is the only
220 * one in this range currently allowed
222 if (mad_reg_req->mgmt_class !=
223 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
225 } else if (mad_reg_req->mgmt_class == 0) {
227 * Class 0 is reserved in IBA and is used for
228 * aliasing of IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
231 } else if (is_vendor_class(mad_reg_req->mgmt_class)) {
233 * If class is in "new" vendor range,
234 * ensure supplied OUI is not zero
236 if (!is_vendor_oui(mad_reg_req->oui))
239 /* Make sure class supplied is consistent with RMPP */
240 if (!ib_is_mad_class_rmpp(mad_reg_req->mgmt_class)) {
244 /* Make sure class supplied is consistent with QP type */
245 if (qp_type == IB_QPT_SMI) {
246 if ((mad_reg_req->mgmt_class !=
247 IB_MGMT_CLASS_SUBN_LID_ROUTED) &&
248 (mad_reg_req->mgmt_class !=
249 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE))
252 if ((mad_reg_req->mgmt_class ==
253 IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
254 (mad_reg_req->mgmt_class ==
255 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE))
259 /* No registration request supplied */
264 /* Validate device and port */
265 port_priv = ib_get_mad_port(device, port_num);
267 ret = ERR_PTR(-ENODEV);
271 /* Allocate structures */
272 mad_agent_priv = kzalloc(sizeof *mad_agent_priv, GFP_KERNEL);
273 if (!mad_agent_priv) {
274 ret = ERR_PTR(-ENOMEM);
278 mad_agent_priv->agent.mr = ib_get_dma_mr(port_priv->qp_info[qpn].qp->pd,
279 IB_ACCESS_LOCAL_WRITE);
280 if (IS_ERR(mad_agent_priv->agent.mr)) {
281 ret = ERR_PTR(-ENOMEM);
286 reg_req = kmalloc(sizeof *reg_req, GFP_KERNEL);
288 ret = ERR_PTR(-ENOMEM);
291 /* Make a copy of the MAD registration request */
292 memcpy(reg_req, mad_reg_req, sizeof *reg_req);
295 /* Now, fill in the various structures */
296 mad_agent_priv->qp_info = &port_priv->qp_info[qpn];
297 mad_agent_priv->reg_req = reg_req;
298 mad_agent_priv->agent.rmpp_version = rmpp_version;
299 mad_agent_priv->agent.device = device;
300 mad_agent_priv->agent.recv_handler = recv_handler;
301 mad_agent_priv->agent.send_handler = send_handler;
302 mad_agent_priv->agent.context = context;
303 mad_agent_priv->agent.qp = port_priv->qp_info[qpn].qp;
304 mad_agent_priv->agent.port_num = port_num;
306 spin_lock_irqsave(&port_priv->reg_lock, flags);
307 mad_agent_priv->agent.hi_tid = ++ib_mad_client_id;
310 * Make sure MAD registration (if supplied)
311 * is non overlapping with any existing ones
314 mgmt_class = convert_mgmt_class(mad_reg_req->mgmt_class);
315 if (!is_vendor_class(mgmt_class)) {
316 class = port_priv->version[mad_reg_req->
317 mgmt_class_version].class;
319 method = class->method_table[mgmt_class];
321 if (method_in_use(&method,
326 ret2 = add_nonoui_reg_req(mad_reg_req, mad_agent_priv,
329 /* "New" vendor class range */
330 vendor = port_priv->version[mad_reg_req->
331 mgmt_class_version].vendor;
333 vclass = vendor_class_index(mgmt_class);
334 vendor_class = vendor->vendor_class[vclass];
336 if (is_vendor_method_in_use(
342 ret2 = add_oui_reg_req(mad_reg_req, mad_agent_priv);
350 /* Add mad agent into port's agent list */
351 list_add_tail(&mad_agent_priv->agent_list, &port_priv->agent_list);
352 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
354 spin_lock_init(&mad_agent_priv->lock);
355 INIT_LIST_HEAD(&mad_agent_priv->send_list);
356 INIT_LIST_HEAD(&mad_agent_priv->wait_list);
357 INIT_LIST_HEAD(&mad_agent_priv->done_list);
358 INIT_LIST_HEAD(&mad_agent_priv->rmpp_list);
359 INIT_WORK(&mad_agent_priv->timed_work, timeout_sends, mad_agent_priv);
360 INIT_LIST_HEAD(&mad_agent_priv->local_list);
361 INIT_WORK(&mad_agent_priv->local_work, local_completions,
363 atomic_set(&mad_agent_priv->refcount, 1);
364 init_completion(&mad_agent_priv->comp);
366 return &mad_agent_priv->agent;
369 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
372 ib_dereg_mr(mad_agent_priv->agent.mr);
374 kfree(mad_agent_priv);
378 EXPORT_SYMBOL(ib_register_mad_agent);
380 static inline int is_snooping_sends(int mad_snoop_flags)
382 return (mad_snoop_flags &
383 (/*IB_MAD_SNOOP_POSTED_SENDS |
384 IB_MAD_SNOOP_RMPP_SENDS |*/
385 IB_MAD_SNOOP_SEND_COMPLETIONS /*|
386 IB_MAD_SNOOP_RMPP_SEND_COMPLETIONS*/));
389 static inline int is_snooping_recvs(int mad_snoop_flags)
391 return (mad_snoop_flags &
392 (IB_MAD_SNOOP_RECVS /*|
393 IB_MAD_SNOOP_RMPP_RECVS*/));
396 static int register_snoop_agent(struct ib_mad_qp_info *qp_info,
397 struct ib_mad_snoop_private *mad_snoop_priv)
399 struct ib_mad_snoop_private **new_snoop_table;
403 spin_lock_irqsave(&qp_info->snoop_lock, flags);
404 /* Check for empty slot in array. */
405 for (i = 0; i < qp_info->snoop_table_size; i++)
406 if (!qp_info->snoop_table[i])
409 if (i == qp_info->snoop_table_size) {
411 new_snoop_table = kmalloc(sizeof mad_snoop_priv *
412 qp_info->snoop_table_size + 1,
414 if (!new_snoop_table) {
418 if (qp_info->snoop_table) {
419 memcpy(new_snoop_table, qp_info->snoop_table,
420 sizeof mad_snoop_priv *
421 qp_info->snoop_table_size);
422 kfree(qp_info->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(u64 wr_id, u16 slid, u16 pkey_index, u8 port_num,
649 memset(wc, 0, sizeof *wc);
651 wc->status = IB_WC_SUCCESS;
652 wc->opcode = IB_WC_RECV;
653 wc->pkey_index = pkey_index;
654 wc->byte_len = sizeof(struct ib_mad) + sizeof(struct ib_grh);
659 wc->dlid_path_bits = 0;
660 wc->port_num = port_num;
664 * Return 0 if SMP is to be sent
665 * Return 1 if SMP was consumed locally (whether or not solicited)
666 * Return < 0 if error
668 static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
669 struct ib_mad_send_wr_private *mad_send_wr)
672 struct ib_smp *smp = mad_send_wr->send_buf.mad;
674 struct ib_mad_local_private *local;
675 struct ib_mad_private *mad_priv;
676 struct ib_mad_port_private *port_priv;
677 struct ib_mad_agent_private *recv_mad_agent = NULL;
678 struct ib_device *device = mad_agent_priv->agent.device;
679 u8 port_num = mad_agent_priv->agent.port_num;
681 struct ib_send_wr *send_wr = &mad_send_wr->send_wr;
684 * Directed route handling starts if the initial LID routed part of
685 * a request or the ending LID routed part of a response is empty.
686 * If we are at the start of the LID routed part, don't update the
687 * hop_ptr or hop_cnt. See section 14.2.2, Vol 1 IB spec.
689 if ((ib_get_smp_direction(smp) ? smp->dr_dlid : smp->dr_slid) ==
691 !smi_handle_dr_smp_send(smp, device->node_type, port_num)) {
693 printk(KERN_ERR PFX "Invalid directed route\n");
696 /* Check to post send on QP or process locally */
697 ret = smi_check_local_smp(smp, device);
701 local = kmalloc(sizeof *local, GFP_ATOMIC);
704 printk(KERN_ERR PFX "No memory for ib_mad_local_private\n");
707 local->mad_priv = NULL;
708 local->recv_mad_agent = NULL;
709 mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_ATOMIC);
712 printk(KERN_ERR PFX "No memory for local response MAD\n");
717 build_smp_wc(send_wr->wr_id, be16_to_cpu(smp->dr_slid),
718 send_wr->wr.ud.pkey_index,
719 send_wr->wr.ud.port_num, &mad_wc);
721 /* No GRH for DR SMP */
722 ret = device->process_mad(device, 0, port_num, &mad_wc, NULL,
723 (struct ib_mad *)smp,
724 (struct ib_mad *)&mad_priv->mad);
727 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY:
728 if (ib_response_mad(&mad_priv->mad.mad) &&
729 mad_agent_priv->agent.recv_handler) {
730 local->mad_priv = mad_priv;
731 local->recv_mad_agent = mad_agent_priv;
733 * Reference MAD agent until receive
734 * side of local completion handled
736 atomic_inc(&mad_agent_priv->refcount);
738 kmem_cache_free(ib_mad_cache, mad_priv);
740 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED:
741 kmem_cache_free(ib_mad_cache, mad_priv);
743 case IB_MAD_RESULT_SUCCESS:
744 /* Treat like an incoming receive MAD */
745 port_priv = ib_get_mad_port(mad_agent_priv->agent.device,
746 mad_agent_priv->agent.port_num);
748 mad_priv->mad.mad.mad_hdr.tid =
749 ((struct ib_mad *)smp)->mad_hdr.tid;
750 recv_mad_agent = find_mad_agent(port_priv,
753 if (!port_priv || !recv_mad_agent) {
754 kmem_cache_free(ib_mad_cache, mad_priv);
759 local->mad_priv = mad_priv;
760 local->recv_mad_agent = recv_mad_agent;
763 kmem_cache_free(ib_mad_cache, mad_priv);
769 local->mad_send_wr = mad_send_wr;
770 /* Reference MAD agent until send side of local completion handled */
771 atomic_inc(&mad_agent_priv->refcount);
772 /* Queue local completion to local list */
773 spin_lock_irqsave(&mad_agent_priv->lock, flags);
774 list_add_tail(&local->completion_list, &mad_agent_priv->local_list);
775 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
776 queue_work(mad_agent_priv->qp_info->port_priv->wq,
777 &mad_agent_priv->local_work);
783 static int get_pad_size(int hdr_len, int data_len)
787 seg_size = sizeof(struct ib_mad) - hdr_len;
788 if (data_len && seg_size) {
789 pad = seg_size - data_len % seg_size;
790 return pad == seg_size ? 0 : pad;
795 static void free_send_rmpp_list(struct ib_mad_send_wr_private *mad_send_wr)
797 struct ib_rmpp_segment *s, *t;
799 list_for_each_entry_safe(s, t, &mad_send_wr->rmpp_list, list) {
805 static int alloc_send_rmpp_list(struct ib_mad_send_wr_private *send_wr,
808 struct ib_mad_send_buf *send_buf = &send_wr->send_buf;
809 struct ib_rmpp_mad *rmpp_mad = send_buf->mad;
810 struct ib_rmpp_segment *seg = NULL;
811 int left, seg_size, pad;
813 send_buf->seg_size = sizeof (struct ib_mad) - send_buf->hdr_len;
814 seg_size = send_buf->seg_size;
817 /* Allocate data segments. */
818 for (left = send_buf->data_len + pad; left > 0; left -= seg_size) {
819 seg = kmalloc(sizeof (*seg) + seg_size, gfp_mask);
821 printk(KERN_ERR "alloc_send_rmpp_segs: RMPP mem "
822 "alloc failed for len %zd, gfp %#x\n",
823 sizeof (*seg) + seg_size, gfp_mask);
824 free_send_rmpp_list(send_wr);
827 seg->num = ++send_buf->seg_count;
828 list_add_tail(&seg->list, &send_wr->rmpp_list);
831 /* Zero any padding */
833 memset(seg->data + seg_size - pad, 0, pad);
835 rmpp_mad->rmpp_hdr.rmpp_version = send_wr->mad_agent_priv->
837 rmpp_mad->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_DATA;
838 ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
840 send_wr->cur_seg = container_of(send_wr->rmpp_list.next,
841 struct ib_rmpp_segment, list);
842 send_wr->last_ack_seg = send_wr->cur_seg;
846 struct ib_mad_send_buf * ib_create_send_mad(struct ib_mad_agent *mad_agent,
847 u32 remote_qpn, u16 pkey_index,
849 int hdr_len, int data_len,
852 struct ib_mad_agent_private *mad_agent_priv;
853 struct ib_mad_send_wr_private *mad_send_wr;
854 int pad, message_size, ret, size;
857 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
859 pad = get_pad_size(hdr_len, data_len);
860 message_size = hdr_len + data_len + pad;
862 if ((!mad_agent->rmpp_version &&
863 (rmpp_active || message_size > sizeof(struct ib_mad))) ||
864 (!rmpp_active && message_size > sizeof(struct ib_mad)))
865 return ERR_PTR(-EINVAL);
867 size = rmpp_active ? hdr_len : sizeof(struct ib_mad);
868 buf = kzalloc(sizeof *mad_send_wr + size, gfp_mask);
870 return ERR_PTR(-ENOMEM);
872 mad_send_wr = buf + size;
873 INIT_LIST_HEAD(&mad_send_wr->rmpp_list);
874 mad_send_wr->send_buf.mad = buf;
875 mad_send_wr->send_buf.hdr_len = hdr_len;
876 mad_send_wr->send_buf.data_len = data_len;
877 mad_send_wr->pad = pad;
879 mad_send_wr->mad_agent_priv = mad_agent_priv;
880 mad_send_wr->sg_list[0].length = hdr_len;
881 mad_send_wr->sg_list[0].lkey = mad_agent->mr->lkey;
882 mad_send_wr->sg_list[1].length = sizeof(struct ib_mad) - hdr_len;
883 mad_send_wr->sg_list[1].lkey = mad_agent->mr->lkey;
885 mad_send_wr->send_wr.wr_id = (unsigned long) mad_send_wr;
886 mad_send_wr->send_wr.sg_list = mad_send_wr->sg_list;
887 mad_send_wr->send_wr.num_sge = 2;
888 mad_send_wr->send_wr.opcode = IB_WR_SEND;
889 mad_send_wr->send_wr.send_flags = IB_SEND_SIGNALED;
890 mad_send_wr->send_wr.wr.ud.remote_qpn = remote_qpn;
891 mad_send_wr->send_wr.wr.ud.remote_qkey = IB_QP_SET_QKEY;
892 mad_send_wr->send_wr.wr.ud.pkey_index = pkey_index;
895 ret = alloc_send_rmpp_list(mad_send_wr, gfp_mask);
902 mad_send_wr->send_buf.mad_agent = mad_agent;
903 atomic_inc(&mad_agent_priv->refcount);
904 return &mad_send_wr->send_buf;
906 EXPORT_SYMBOL(ib_create_send_mad);
908 int ib_get_mad_data_offset(u8 mgmt_class)
910 if (mgmt_class == IB_MGMT_CLASS_SUBN_ADM)
911 return IB_MGMT_SA_HDR;
912 else if ((mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
913 (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
914 (mgmt_class == IB_MGMT_CLASS_BIS))
915 return IB_MGMT_DEVICE_HDR;
916 else if ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
917 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END))
918 return IB_MGMT_VENDOR_HDR;
920 return IB_MGMT_MAD_HDR;
922 EXPORT_SYMBOL(ib_get_mad_data_offset);
924 int ib_is_mad_class_rmpp(u8 mgmt_class)
926 if ((mgmt_class == IB_MGMT_CLASS_SUBN_ADM) ||
927 (mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
928 (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
929 (mgmt_class == IB_MGMT_CLASS_BIS) ||
930 ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
931 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END)))
935 EXPORT_SYMBOL(ib_is_mad_class_rmpp);
937 void *ib_get_rmpp_segment(struct ib_mad_send_buf *send_buf, int seg_num)
939 struct ib_mad_send_wr_private *mad_send_wr;
940 struct list_head *list;
942 mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
944 list = &mad_send_wr->cur_seg->list;
946 if (mad_send_wr->cur_seg->num < seg_num) {
947 list_for_each_entry(mad_send_wr->cur_seg, list, list)
948 if (mad_send_wr->cur_seg->num == seg_num)
950 } else if (mad_send_wr->cur_seg->num > seg_num) {
951 list_for_each_entry_reverse(mad_send_wr->cur_seg, list, list)
952 if (mad_send_wr->cur_seg->num == seg_num)
955 return mad_send_wr->cur_seg->data;
957 EXPORT_SYMBOL(ib_get_rmpp_segment);
959 static inline void *ib_get_payload(struct ib_mad_send_wr_private *mad_send_wr)
961 if (mad_send_wr->send_buf.seg_count)
962 return ib_get_rmpp_segment(&mad_send_wr->send_buf,
963 mad_send_wr->seg_num);
965 return mad_send_wr->send_buf.mad +
966 mad_send_wr->send_buf.hdr_len;
969 void ib_free_send_mad(struct ib_mad_send_buf *send_buf)
971 struct ib_mad_agent_private *mad_agent_priv;
972 struct ib_mad_send_wr_private *mad_send_wr;
974 mad_agent_priv = container_of(send_buf->mad_agent,
975 struct ib_mad_agent_private, agent);
976 mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
979 free_send_rmpp_list(mad_send_wr);
980 kfree(send_buf->mad);
981 deref_mad_agent(mad_agent_priv);
983 EXPORT_SYMBOL(ib_free_send_mad);
985 int ib_send_mad(struct ib_mad_send_wr_private *mad_send_wr)
987 struct ib_mad_qp_info *qp_info;
988 struct list_head *list;
989 struct ib_send_wr *bad_send_wr;
990 struct ib_mad_agent *mad_agent;
995 /* Set WR ID to find mad_send_wr upon completion */
996 qp_info = mad_send_wr->mad_agent_priv->qp_info;
997 mad_send_wr->send_wr.wr_id = (unsigned long)&mad_send_wr->mad_list;
998 mad_send_wr->mad_list.mad_queue = &qp_info->send_queue;
1000 mad_agent = mad_send_wr->send_buf.mad_agent;
1001 sge = mad_send_wr->sg_list;
1002 sge[0].addr = dma_map_single(mad_agent->device->dma_device,
1003 mad_send_wr->send_buf.mad,
1006 pci_unmap_addr_set(mad_send_wr, header_mapping, sge[0].addr);
1008 sge[1].addr = dma_map_single(mad_agent->device->dma_device,
1009 ib_get_payload(mad_send_wr),
1012 pci_unmap_addr_set(mad_send_wr, payload_mapping, sge[1].addr);
1014 spin_lock_irqsave(&qp_info->send_queue.lock, flags);
1015 if (qp_info->send_queue.count < qp_info->send_queue.max_active) {
1016 ret = ib_post_send(mad_agent->qp, &mad_send_wr->send_wr,
1018 list = &qp_info->send_queue.list;
1021 list = &qp_info->overflow_list;
1025 qp_info->send_queue.count++;
1026 list_add_tail(&mad_send_wr->mad_list.list, list);
1028 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
1030 dma_unmap_single(mad_agent->device->dma_device,
1031 pci_unmap_addr(mad_send_wr, header_mapping),
1032 sge[0].length, DMA_TO_DEVICE);
1033 dma_unmap_single(mad_agent->device->dma_device,
1034 pci_unmap_addr(mad_send_wr, payload_mapping),
1035 sge[1].length, DMA_TO_DEVICE);
1041 * ib_post_send_mad - Posts MAD(s) to the send queue of the QP associated
1042 * with the registered client
1044 int ib_post_send_mad(struct ib_mad_send_buf *send_buf,
1045 struct ib_mad_send_buf **bad_send_buf)
1047 struct ib_mad_agent_private *mad_agent_priv;
1048 struct ib_mad_send_buf *next_send_buf;
1049 struct ib_mad_send_wr_private *mad_send_wr;
1050 unsigned long flags;
1053 /* Walk list of send WRs and post each on send list */
1054 for (; send_buf; send_buf = next_send_buf) {
1056 mad_send_wr = container_of(send_buf,
1057 struct ib_mad_send_wr_private,
1059 mad_agent_priv = mad_send_wr->mad_agent_priv;
1061 if (!send_buf->mad_agent->send_handler ||
1062 (send_buf->timeout_ms &&
1063 !send_buf->mad_agent->recv_handler)) {
1068 if (!ib_is_mad_class_rmpp(((struct ib_mad_hdr *) send_buf->mad)->mgmt_class)) {
1069 if (mad_agent_priv->agent.rmpp_version) {
1076 * Save pointer to next work request to post in case the
1077 * current one completes, and the user modifies the work
1078 * request associated with the completion
1080 next_send_buf = send_buf->next;
1081 mad_send_wr->send_wr.wr.ud.ah = send_buf->ah;
1083 if (((struct ib_mad_hdr *) send_buf->mad)->mgmt_class ==
1084 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
1085 ret = handle_outgoing_dr_smp(mad_agent_priv,
1087 if (ret < 0) /* error */
1089 else if (ret == 1) /* locally consumed */
1093 mad_send_wr->tid = ((struct ib_mad_hdr *) send_buf->mad)->tid;
1094 /* Timeout will be updated after send completes */
1095 mad_send_wr->timeout = msecs_to_jiffies(send_buf->timeout_ms);
1096 mad_send_wr->retries = send_buf->retries;
1097 /* Reference for work request to QP + response */
1098 mad_send_wr->refcount = 1 + (mad_send_wr->timeout > 0);
1099 mad_send_wr->status = IB_WC_SUCCESS;
1101 /* Reference MAD agent until send completes */
1102 atomic_inc(&mad_agent_priv->refcount);
1103 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1104 list_add_tail(&mad_send_wr->agent_list,
1105 &mad_agent_priv->send_list);
1106 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1108 if (mad_agent_priv->agent.rmpp_version) {
1109 ret = ib_send_rmpp_mad(mad_send_wr);
1110 if (ret >= 0 && ret != IB_RMPP_RESULT_CONSUMED)
1111 ret = ib_send_mad(mad_send_wr);
1113 ret = ib_send_mad(mad_send_wr);
1115 /* Fail send request */
1116 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1117 list_del(&mad_send_wr->agent_list);
1118 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1119 atomic_dec(&mad_agent_priv->refcount);
1126 *bad_send_buf = send_buf;
1129 EXPORT_SYMBOL(ib_post_send_mad);
1132 * ib_free_recv_mad - Returns data buffers used to receive
1133 * a MAD to the access layer
1135 void ib_free_recv_mad(struct ib_mad_recv_wc *mad_recv_wc)
1137 struct ib_mad_recv_buf *mad_recv_buf, *temp_recv_buf;
1138 struct ib_mad_private_header *mad_priv_hdr;
1139 struct ib_mad_private *priv;
1140 struct list_head free_list;
1142 INIT_LIST_HEAD(&free_list);
1143 list_splice_init(&mad_recv_wc->rmpp_list, &free_list);
1145 list_for_each_entry_safe(mad_recv_buf, temp_recv_buf,
1147 mad_recv_wc = container_of(mad_recv_buf, struct ib_mad_recv_wc,
1149 mad_priv_hdr = container_of(mad_recv_wc,
1150 struct ib_mad_private_header,
1152 priv = container_of(mad_priv_hdr, struct ib_mad_private,
1154 kmem_cache_free(ib_mad_cache, priv);
1157 EXPORT_SYMBOL(ib_free_recv_mad);
1159 struct ib_mad_agent *ib_redirect_mad_qp(struct ib_qp *qp,
1161 ib_mad_send_handler send_handler,
1162 ib_mad_recv_handler recv_handler,
1165 return ERR_PTR(-EINVAL); /* XXX: for now */
1167 EXPORT_SYMBOL(ib_redirect_mad_qp);
1169 int ib_process_mad_wc(struct ib_mad_agent *mad_agent,
1172 printk(KERN_ERR PFX "ib_process_mad_wc() not implemented yet\n");
1175 EXPORT_SYMBOL(ib_process_mad_wc);
1177 static int method_in_use(struct ib_mad_mgmt_method_table **method,
1178 struct ib_mad_reg_req *mad_reg_req)
1182 for (i = find_first_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS);
1183 i < IB_MGMT_MAX_METHODS;
1184 i = find_next_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS,
1186 if ((*method)->agent[i]) {
1187 printk(KERN_ERR PFX "Method %d already in use\n", i);
1194 static int allocate_method_table(struct ib_mad_mgmt_method_table **method)
1196 /* Allocate management method table */
1197 *method = kzalloc(sizeof **method, GFP_ATOMIC);
1199 printk(KERN_ERR PFX "No memory for "
1200 "ib_mad_mgmt_method_table\n");
1208 * Check to see if there are any methods still in use
1210 static int check_method_table(struct ib_mad_mgmt_method_table *method)
1214 for (i = 0; i < IB_MGMT_MAX_METHODS; i++)
1215 if (method->agent[i])
1221 * Check to see if there are any method tables for this class still in use
1223 static int check_class_table(struct ib_mad_mgmt_class_table *class)
1227 for (i = 0; i < MAX_MGMT_CLASS; i++)
1228 if (class->method_table[i])
1233 static int check_vendor_class(struct ib_mad_mgmt_vendor_class *vendor_class)
1237 for (i = 0; i < MAX_MGMT_OUI; i++)
1238 if (vendor_class->method_table[i])
1243 static int find_vendor_oui(struct ib_mad_mgmt_vendor_class *vendor_class,
1248 for (i = 0; i < MAX_MGMT_OUI; i++)
1249 /* Is there matching OUI for this vendor class ? */
1250 if (!memcmp(vendor_class->oui[i], oui, 3))
1256 static int check_vendor_table(struct ib_mad_mgmt_vendor_class_table *vendor)
1260 for (i = 0; i < MAX_MGMT_VENDOR_RANGE2; i++)
1261 if (vendor->vendor_class[i])
1267 static void remove_methods_mad_agent(struct ib_mad_mgmt_method_table *method,
1268 struct ib_mad_agent_private *agent)
1272 /* Remove any methods for this mad agent */
1273 for (i = 0; i < IB_MGMT_MAX_METHODS; i++) {
1274 if (method->agent[i] == agent) {
1275 method->agent[i] = NULL;
1280 static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1281 struct ib_mad_agent_private *agent_priv,
1284 struct ib_mad_port_private *port_priv;
1285 struct ib_mad_mgmt_class_table **class;
1286 struct ib_mad_mgmt_method_table **method;
1289 port_priv = agent_priv->qp_info->port_priv;
1290 class = &port_priv->version[mad_reg_req->mgmt_class_version].class;
1292 /* Allocate management class table for "new" class version */
1293 *class = kzalloc(sizeof **class, GFP_ATOMIC);
1295 printk(KERN_ERR PFX "No memory for "
1296 "ib_mad_mgmt_class_table\n");
1301 /* Allocate method table for this management class */
1302 method = &(*class)->method_table[mgmt_class];
1303 if ((ret = allocate_method_table(method)))
1306 method = &(*class)->method_table[mgmt_class];
1308 /* Allocate method table for this management class */
1309 if ((ret = allocate_method_table(method)))
1314 /* Now, make sure methods are not already in use */
1315 if (method_in_use(method, mad_reg_req))
1318 /* Finally, add in methods being registered */
1319 for (i = find_first_bit(mad_reg_req->method_mask,
1320 IB_MGMT_MAX_METHODS);
1321 i < IB_MGMT_MAX_METHODS;
1322 i = find_next_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS,
1324 (*method)->agent[i] = agent_priv;
1329 /* Remove any methods for this mad agent */
1330 remove_methods_mad_agent(*method, agent_priv);
1331 /* Now, check to see if there are any methods in use */
1332 if (!check_method_table(*method)) {
1333 /* If not, release management method table */
1346 static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1347 struct ib_mad_agent_private *agent_priv)
1349 struct ib_mad_port_private *port_priv;
1350 struct ib_mad_mgmt_vendor_class_table **vendor_table;
1351 struct ib_mad_mgmt_vendor_class_table *vendor = NULL;
1352 struct ib_mad_mgmt_vendor_class *vendor_class = NULL;
1353 struct ib_mad_mgmt_method_table **method;
1354 int i, ret = -ENOMEM;
1357 /* "New" vendor (with OUI) class */
1358 vclass = vendor_class_index(mad_reg_req->mgmt_class);
1359 port_priv = agent_priv->qp_info->port_priv;
1360 vendor_table = &port_priv->version[
1361 mad_reg_req->mgmt_class_version].vendor;
1362 if (!*vendor_table) {
1363 /* Allocate mgmt vendor class table for "new" class version */
1364 vendor = kzalloc(sizeof *vendor, GFP_ATOMIC);
1366 printk(KERN_ERR PFX "No memory for "
1367 "ib_mad_mgmt_vendor_class_table\n");
1371 *vendor_table = vendor;
1373 if (!(*vendor_table)->vendor_class[vclass]) {
1374 /* Allocate table for this management vendor class */
1375 vendor_class = kzalloc(sizeof *vendor_class, GFP_ATOMIC);
1376 if (!vendor_class) {
1377 printk(KERN_ERR PFX "No memory for "
1378 "ib_mad_mgmt_vendor_class\n");
1382 (*vendor_table)->vendor_class[vclass] = vendor_class;
1384 for (i = 0; i < MAX_MGMT_OUI; i++) {
1385 /* Is there matching OUI for this vendor class ? */
1386 if (!memcmp((*vendor_table)->vendor_class[vclass]->oui[i],
1387 mad_reg_req->oui, 3)) {
1388 method = &(*vendor_table)->vendor_class[
1389 vclass]->method_table[i];
1394 for (i = 0; i < MAX_MGMT_OUI; i++) {
1395 /* OUI slot available ? */
1396 if (!is_vendor_oui((*vendor_table)->vendor_class[
1398 method = &(*vendor_table)->vendor_class[
1399 vclass]->method_table[i];
1401 /* Allocate method table for this OUI */
1402 if ((ret = allocate_method_table(method)))
1404 memcpy((*vendor_table)->vendor_class[vclass]->oui[i],
1405 mad_reg_req->oui, 3);
1409 printk(KERN_ERR PFX "All OUI slots in use\n");
1413 /* Now, make sure methods are not already in use */
1414 if (method_in_use(method, mad_reg_req))
1417 /* Finally, add in methods being registered */
1418 for (i = find_first_bit(mad_reg_req->method_mask,
1419 IB_MGMT_MAX_METHODS);
1420 i < IB_MGMT_MAX_METHODS;
1421 i = find_next_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS,
1423 (*method)->agent[i] = agent_priv;
1428 /* Remove any methods for this mad agent */
1429 remove_methods_mad_agent(*method, agent_priv);
1430 /* Now, check to see if there are any methods in use */
1431 if (!check_method_table(*method)) {
1432 /* If not, release management method table */
1439 (*vendor_table)->vendor_class[vclass] = NULL;
1440 kfree(vendor_class);
1444 *vendor_table = NULL;
1451 static void remove_mad_reg_req(struct ib_mad_agent_private *agent_priv)
1453 struct ib_mad_port_private *port_priv;
1454 struct ib_mad_mgmt_class_table *class;
1455 struct ib_mad_mgmt_method_table *method;
1456 struct ib_mad_mgmt_vendor_class_table *vendor;
1457 struct ib_mad_mgmt_vendor_class *vendor_class;
1462 * Was MAD registration request supplied
1463 * with original registration ?
1465 if (!agent_priv->reg_req) {
1469 port_priv = agent_priv->qp_info->port_priv;
1470 mgmt_class = convert_mgmt_class(agent_priv->reg_req->mgmt_class);
1471 class = port_priv->version[
1472 agent_priv->reg_req->mgmt_class_version].class;
1476 method = class->method_table[mgmt_class];
1478 /* Remove any methods for this mad agent */
1479 remove_methods_mad_agent(method, agent_priv);
1480 /* Now, check to see if there are any methods still in use */
1481 if (!check_method_table(method)) {
1482 /* If not, release management method table */
1484 class->method_table[mgmt_class] = NULL;
1485 /* Any management classes left ? */
1486 if (!check_class_table(class)) {
1487 /* If not, release management class table */
1490 agent_priv->reg_req->
1491 mgmt_class_version].class = NULL;
1497 if (!is_vendor_class(mgmt_class))
1500 /* normalize mgmt_class to vendor range 2 */
1501 mgmt_class = vendor_class_index(agent_priv->reg_req->mgmt_class);
1502 vendor = port_priv->version[
1503 agent_priv->reg_req->mgmt_class_version].vendor;
1508 vendor_class = vendor->vendor_class[mgmt_class];
1510 index = find_vendor_oui(vendor_class, agent_priv->reg_req->oui);
1513 method = vendor_class->method_table[index];
1515 /* Remove any methods for this mad agent */
1516 remove_methods_mad_agent(method, agent_priv);
1518 * Now, check to see if there are
1519 * any methods still in use
1521 if (!check_method_table(method)) {
1522 /* If not, release management method table */
1524 vendor_class->method_table[index] = NULL;
1525 memset(vendor_class->oui[index], 0, 3);
1526 /* Any OUIs left ? */
1527 if (!check_vendor_class(vendor_class)) {
1528 /* If not, release vendor class table */
1529 kfree(vendor_class);
1530 vendor->vendor_class[mgmt_class] = NULL;
1531 /* Any other vendor classes left ? */
1532 if (!check_vendor_table(vendor)) {
1535 agent_priv->reg_req->
1536 mgmt_class_version].
1548 static struct ib_mad_agent_private *
1549 find_mad_agent(struct ib_mad_port_private *port_priv,
1552 struct ib_mad_agent_private *mad_agent = NULL;
1553 unsigned long flags;
1555 spin_lock_irqsave(&port_priv->reg_lock, flags);
1556 if (ib_response_mad(mad)) {
1558 struct ib_mad_agent_private *entry;
1561 * Routing is based on high 32 bits of transaction ID
1564 hi_tid = be64_to_cpu(mad->mad_hdr.tid) >> 32;
1565 list_for_each_entry(entry, &port_priv->agent_list, agent_list) {
1566 if (entry->agent.hi_tid == hi_tid) {
1572 struct ib_mad_mgmt_class_table *class;
1573 struct ib_mad_mgmt_method_table *method;
1574 struct ib_mad_mgmt_vendor_class_table *vendor;
1575 struct ib_mad_mgmt_vendor_class *vendor_class;
1576 struct ib_vendor_mad *vendor_mad;
1580 * Routing is based on version, class, and method
1581 * For "newer" vendor MADs, also based on OUI
1583 if (mad->mad_hdr.class_version >= MAX_MGMT_VERSION)
1585 if (!is_vendor_class(mad->mad_hdr.mgmt_class)) {
1586 class = port_priv->version[
1587 mad->mad_hdr.class_version].class;
1590 method = class->method_table[convert_mgmt_class(
1591 mad->mad_hdr.mgmt_class)];
1593 mad_agent = method->agent[mad->mad_hdr.method &
1594 ~IB_MGMT_METHOD_RESP];
1596 vendor = port_priv->version[
1597 mad->mad_hdr.class_version].vendor;
1600 vendor_class = vendor->vendor_class[vendor_class_index(
1601 mad->mad_hdr.mgmt_class)];
1604 /* Find matching OUI */
1605 vendor_mad = (struct ib_vendor_mad *)mad;
1606 index = find_vendor_oui(vendor_class, vendor_mad->oui);
1609 method = vendor_class->method_table[index];
1611 mad_agent = method->agent[mad->mad_hdr.method &
1612 ~IB_MGMT_METHOD_RESP];
1618 if (mad_agent->agent.recv_handler)
1619 atomic_inc(&mad_agent->refcount);
1621 printk(KERN_NOTICE PFX "No receive handler for client "
1623 &mad_agent->agent, port_priv->port_num);
1628 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
1633 static int validate_mad(struct ib_mad *mad, u32 qp_num)
1637 /* Make sure MAD base version is understood */
1638 if (mad->mad_hdr.base_version != IB_MGMT_BASE_VERSION) {
1639 printk(KERN_ERR PFX "MAD received with unsupported base "
1640 "version %d\n", mad->mad_hdr.base_version);
1644 /* Filter SMI packets sent to other than QP0 */
1645 if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
1646 (mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) {
1650 /* Filter GSI packets sent to QP0 */
1659 static int is_data_mad(struct ib_mad_agent_private *mad_agent_priv,
1660 struct ib_mad_hdr *mad_hdr)
1662 struct ib_rmpp_mad *rmpp_mad;
1664 rmpp_mad = (struct ib_rmpp_mad *)mad_hdr;
1665 return !mad_agent_priv->agent.rmpp_version ||
1666 !(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
1667 IB_MGMT_RMPP_FLAG_ACTIVE) ||
1668 (rmpp_mad->rmpp_hdr.rmpp_type == IB_MGMT_RMPP_TYPE_DATA);
1671 static inline int rcv_has_same_class(struct ib_mad_send_wr_private *wr,
1672 struct ib_mad_recv_wc *rwc)
1674 return ((struct ib_mad *)(wr->send_buf.mad))->mad_hdr.mgmt_class ==
1675 rwc->recv_buf.mad->mad_hdr.mgmt_class;
1678 static inline int rcv_has_same_gid(struct ib_mad_agent_private *mad_agent_priv,
1679 struct ib_mad_send_wr_private *wr,
1680 struct ib_mad_recv_wc *rwc )
1682 struct ib_ah_attr attr;
1683 u8 send_resp, rcv_resp;
1685 struct ib_device *device = mad_agent_priv->agent.device;
1686 u8 port_num = mad_agent_priv->agent.port_num;
1689 send_resp = ((struct ib_mad *)(wr->send_buf.mad))->
1690 mad_hdr.method & IB_MGMT_METHOD_RESP;
1691 rcv_resp = rwc->recv_buf.mad->mad_hdr.method & IB_MGMT_METHOD_RESP;
1693 if (send_resp == rcv_resp)
1694 /* both requests, or both responses. GIDs different */
1697 if (ib_query_ah(wr->send_buf.ah, &attr))
1698 /* Assume not equal, to avoid false positives. */
1701 if (!!(attr.ah_flags & IB_AH_GRH) !=
1702 !!(rwc->wc->wc_flags & IB_WC_GRH))
1703 /* one has GID, other does not. Assume different */
1706 if (!send_resp && rcv_resp) {
1707 /* is request/response. */
1708 if (!(attr.ah_flags & IB_AH_GRH)) {
1709 if (ib_get_cached_lmc(device, port_num, &lmc))
1711 return (!lmc || !((attr.src_path_bits ^
1712 rwc->wc->dlid_path_bits) &
1715 if (ib_get_cached_gid(device, port_num,
1716 attr.grh.sgid_index, &sgid))
1718 return !memcmp(sgid.raw, rwc->recv_buf.grh->dgid.raw,
1723 if (!(attr.ah_flags & IB_AH_GRH))
1724 return attr.dlid == rwc->wc->slid;
1726 return !memcmp(attr.grh.dgid.raw, rwc->recv_buf.grh->sgid.raw,
1730 static inline int is_direct(u8 class)
1732 return (class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE);
1735 struct ib_mad_send_wr_private*
1736 ib_find_send_mad(struct ib_mad_agent_private *mad_agent_priv,
1737 struct ib_mad_recv_wc *wc)
1739 struct ib_mad_send_wr_private *wr;
1742 mad = (struct ib_mad *)wc->recv_buf.mad;
1744 list_for_each_entry(wr, &mad_agent_priv->wait_list, agent_list) {
1745 if ((wr->tid == mad->mad_hdr.tid) &&
1746 rcv_has_same_class(wr, wc) &&
1748 * Don't check GID for direct routed MADs.
1749 * These might have permissive LIDs.
1751 (is_direct(wc->recv_buf.mad->mad_hdr.mgmt_class) ||
1752 rcv_has_same_gid(mad_agent_priv, wr, wc)))
1757 * It's possible to receive the response before we've
1758 * been notified that the send has completed
1760 list_for_each_entry(wr, &mad_agent_priv->send_list, agent_list) {
1761 if (is_data_mad(mad_agent_priv, wr->send_buf.mad) &&
1762 wr->tid == mad->mad_hdr.tid &&
1764 rcv_has_same_class(wr, wc) &&
1766 * Don't check GID for direct routed MADs.
1767 * These might have permissive LIDs.
1769 (is_direct(wc->recv_buf.mad->mad_hdr.mgmt_class) ||
1770 rcv_has_same_gid(mad_agent_priv, wr, wc)))
1771 /* Verify request has not been canceled */
1772 return (wr->status == IB_WC_SUCCESS) ? wr : NULL;
1777 void ib_mark_mad_done(struct ib_mad_send_wr_private *mad_send_wr)
1779 mad_send_wr->timeout = 0;
1780 if (mad_send_wr->refcount == 1)
1781 list_move_tail(&mad_send_wr->agent_list,
1782 &mad_send_wr->mad_agent_priv->done_list);
1785 static void ib_mad_complete_recv(struct ib_mad_agent_private *mad_agent_priv,
1786 struct ib_mad_recv_wc *mad_recv_wc)
1788 struct ib_mad_send_wr_private *mad_send_wr;
1789 struct ib_mad_send_wc mad_send_wc;
1790 unsigned long flags;
1792 INIT_LIST_HEAD(&mad_recv_wc->rmpp_list);
1793 list_add(&mad_recv_wc->recv_buf.list, &mad_recv_wc->rmpp_list);
1794 if (mad_agent_priv->agent.rmpp_version) {
1795 mad_recv_wc = ib_process_rmpp_recv_wc(mad_agent_priv,
1798 deref_mad_agent(mad_agent_priv);
1803 /* Complete corresponding request */
1804 if (ib_response_mad(mad_recv_wc->recv_buf.mad)) {
1805 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1806 mad_send_wr = ib_find_send_mad(mad_agent_priv, mad_recv_wc);
1808 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1809 ib_free_recv_mad(mad_recv_wc);
1810 deref_mad_agent(mad_agent_priv);
1813 ib_mark_mad_done(mad_send_wr);
1814 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1816 /* Defined behavior is to complete response before request */
1817 mad_recv_wc->wc->wr_id = (unsigned long) &mad_send_wr->send_buf;
1818 mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent,
1820 atomic_dec(&mad_agent_priv->refcount);
1822 mad_send_wc.status = IB_WC_SUCCESS;
1823 mad_send_wc.vendor_err = 0;
1824 mad_send_wc.send_buf = &mad_send_wr->send_buf;
1825 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
1827 mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent,
1829 deref_mad_agent(mad_agent_priv);
1833 static void ib_mad_recv_done_handler(struct ib_mad_port_private *port_priv,
1836 struct ib_mad_qp_info *qp_info;
1837 struct ib_mad_private_header *mad_priv_hdr;
1838 struct ib_mad_private *recv, *response;
1839 struct ib_mad_list_head *mad_list;
1840 struct ib_mad_agent_private *mad_agent;
1842 response = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL);
1844 printk(KERN_ERR PFX "ib_mad_recv_done_handler no memory "
1845 "for response buffer\n");
1847 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
1848 qp_info = mad_list->mad_queue->qp_info;
1849 dequeue_mad(mad_list);
1851 mad_priv_hdr = container_of(mad_list, struct ib_mad_private_header,
1853 recv = container_of(mad_priv_hdr, struct ib_mad_private, header);
1854 dma_unmap_single(port_priv->device->dma_device,
1855 pci_unmap_addr(&recv->header, mapping),
1856 sizeof(struct ib_mad_private) -
1857 sizeof(struct ib_mad_private_header),
1860 /* Setup MAD receive work completion from "normal" work completion */
1861 recv->header.wc = *wc;
1862 recv->header.recv_wc.wc = &recv->header.wc;
1863 recv->header.recv_wc.mad_len = sizeof(struct ib_mad);
1864 recv->header.recv_wc.recv_buf.mad = &recv->mad.mad;
1865 recv->header.recv_wc.recv_buf.grh = &recv->grh;
1867 if (atomic_read(&qp_info->snoop_count))
1868 snoop_recv(qp_info, &recv->header.recv_wc, IB_MAD_SNOOP_RECVS);
1871 if (!validate_mad(&recv->mad.mad, qp_info->qp->qp_num))
1874 if (recv->mad.mad.mad_hdr.mgmt_class ==
1875 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
1876 if (!smi_handle_dr_smp_recv(&recv->mad.smp,
1877 port_priv->device->node_type,
1878 port_priv->port_num,
1879 port_priv->device->phys_port_cnt))
1881 if (!smi_check_forward_dr_smp(&recv->mad.smp))
1883 if (!smi_handle_dr_smp_send(&recv->mad.smp,
1884 port_priv->device->node_type,
1885 port_priv->port_num))
1887 if (!smi_check_local_smp(&recv->mad.smp, port_priv->device))
1892 /* Give driver "right of first refusal" on incoming MAD */
1893 if (port_priv->device->process_mad) {
1897 printk(KERN_ERR PFX "No memory for response MAD\n");
1899 * Is it better to assume that
1900 * it wouldn't be processed ?
1905 ret = port_priv->device->process_mad(port_priv->device, 0,
1906 port_priv->port_num,
1909 &response->mad.mad);
1910 if (ret & IB_MAD_RESULT_SUCCESS) {
1911 if (ret & IB_MAD_RESULT_CONSUMED)
1913 if (ret & IB_MAD_RESULT_REPLY) {
1914 agent_send_response(&response->mad.mad,
1917 port_priv->port_num,
1918 qp_info->qp->qp_num);
1924 mad_agent = find_mad_agent(port_priv, &recv->mad.mad);
1926 ib_mad_complete_recv(mad_agent, &recv->header.recv_wc);
1928 * recv is freed up in error cases in ib_mad_complete_recv
1929 * or via recv_handler in ib_mad_complete_recv()
1935 /* Post another receive request for this QP */
1937 ib_mad_post_receive_mads(qp_info, response);
1939 kmem_cache_free(ib_mad_cache, recv);
1941 ib_mad_post_receive_mads(qp_info, recv);
1944 static void adjust_timeout(struct ib_mad_agent_private *mad_agent_priv)
1946 struct ib_mad_send_wr_private *mad_send_wr;
1947 unsigned long delay;
1949 if (list_empty(&mad_agent_priv->wait_list)) {
1950 cancel_delayed_work(&mad_agent_priv->timed_work);
1952 mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
1953 struct ib_mad_send_wr_private,
1956 if (time_after(mad_agent_priv->timeout,
1957 mad_send_wr->timeout)) {
1958 mad_agent_priv->timeout = mad_send_wr->timeout;
1959 cancel_delayed_work(&mad_agent_priv->timed_work);
1960 delay = mad_send_wr->timeout - jiffies;
1961 if ((long)delay <= 0)
1963 queue_delayed_work(mad_agent_priv->qp_info->
1965 &mad_agent_priv->timed_work, delay);
1970 static void wait_for_response(struct ib_mad_send_wr_private *mad_send_wr)
1972 struct ib_mad_agent_private *mad_agent_priv;
1973 struct ib_mad_send_wr_private *temp_mad_send_wr;
1974 struct list_head *list_item;
1975 unsigned long delay;
1977 mad_agent_priv = mad_send_wr->mad_agent_priv;
1978 list_del(&mad_send_wr->agent_list);
1980 delay = mad_send_wr->timeout;
1981 mad_send_wr->timeout += jiffies;
1984 list_for_each_prev(list_item, &mad_agent_priv->wait_list) {
1985 temp_mad_send_wr = list_entry(list_item,
1986 struct ib_mad_send_wr_private,
1988 if (time_after(mad_send_wr->timeout,
1989 temp_mad_send_wr->timeout))
1994 list_item = &mad_agent_priv->wait_list;
1995 list_add(&mad_send_wr->agent_list, list_item);
1997 /* Reschedule a work item if we have a shorter timeout */
1998 if (mad_agent_priv->wait_list.next == &mad_send_wr->agent_list) {
1999 cancel_delayed_work(&mad_agent_priv->timed_work);
2000 queue_delayed_work(mad_agent_priv->qp_info->port_priv->wq,
2001 &mad_agent_priv->timed_work, delay);
2005 void ib_reset_mad_timeout(struct ib_mad_send_wr_private *mad_send_wr,
2008 mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
2009 wait_for_response(mad_send_wr);
2013 * Process a send work completion
2015 void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr,
2016 struct ib_mad_send_wc *mad_send_wc)
2018 struct ib_mad_agent_private *mad_agent_priv;
2019 unsigned long flags;
2022 mad_agent_priv = mad_send_wr->mad_agent_priv;
2023 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2024 if (mad_agent_priv->agent.rmpp_version) {
2025 ret = ib_process_rmpp_send_wc(mad_send_wr, mad_send_wc);
2026 if (ret == IB_RMPP_RESULT_CONSUMED)
2029 ret = IB_RMPP_RESULT_UNHANDLED;
2031 if (mad_send_wc->status != IB_WC_SUCCESS &&
2032 mad_send_wr->status == IB_WC_SUCCESS) {
2033 mad_send_wr->status = mad_send_wc->status;
2034 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2037 if (--mad_send_wr->refcount > 0) {
2038 if (mad_send_wr->refcount == 1 && mad_send_wr->timeout &&
2039 mad_send_wr->status == IB_WC_SUCCESS) {
2040 wait_for_response(mad_send_wr);
2045 /* Remove send from MAD agent and notify client of completion */
2046 list_del(&mad_send_wr->agent_list);
2047 adjust_timeout(mad_agent_priv);
2048 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2050 if (mad_send_wr->status != IB_WC_SUCCESS )
2051 mad_send_wc->status = mad_send_wr->status;
2052 if (ret == IB_RMPP_RESULT_INTERNAL)
2053 ib_rmpp_send_handler(mad_send_wc);
2055 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2058 /* Release reference on agent taken when sending */
2059 deref_mad_agent(mad_agent_priv);
2062 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2065 static void ib_mad_send_done_handler(struct ib_mad_port_private *port_priv,
2068 struct ib_mad_send_wr_private *mad_send_wr, *queued_send_wr;
2069 struct ib_mad_list_head *mad_list;
2070 struct ib_mad_qp_info *qp_info;
2071 struct ib_mad_queue *send_queue;
2072 struct ib_send_wr *bad_send_wr;
2073 struct ib_mad_send_wc mad_send_wc;
2074 unsigned long flags;
2077 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
2078 mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
2080 send_queue = mad_list->mad_queue;
2081 qp_info = send_queue->qp_info;
2084 dma_unmap_single(mad_send_wr->send_buf.mad_agent->device->dma_device,
2085 pci_unmap_addr(mad_send_wr, header_mapping),
2086 mad_send_wr->sg_list[0].length, DMA_TO_DEVICE);
2087 dma_unmap_single(mad_send_wr->send_buf.mad_agent->device->dma_device,
2088 pci_unmap_addr(mad_send_wr, payload_mapping),
2089 mad_send_wr->sg_list[1].length, DMA_TO_DEVICE);
2090 queued_send_wr = NULL;
2091 spin_lock_irqsave(&send_queue->lock, flags);
2092 list_del(&mad_list->list);
2094 /* Move queued send to the send queue */
2095 if (send_queue->count-- > send_queue->max_active) {
2096 mad_list = container_of(qp_info->overflow_list.next,
2097 struct ib_mad_list_head, list);
2098 queued_send_wr = container_of(mad_list,
2099 struct ib_mad_send_wr_private,
2101 list_move_tail(&mad_list->list, &send_queue->list);
2103 spin_unlock_irqrestore(&send_queue->lock, flags);
2105 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2106 mad_send_wc.status = wc->status;
2107 mad_send_wc.vendor_err = wc->vendor_err;
2108 if (atomic_read(&qp_info->snoop_count))
2109 snoop_send(qp_info, &mad_send_wr->send_buf, &mad_send_wc,
2110 IB_MAD_SNOOP_SEND_COMPLETIONS);
2111 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
2113 if (queued_send_wr) {
2114 ret = ib_post_send(qp_info->qp, &queued_send_wr->send_wr,
2117 printk(KERN_ERR PFX "ib_post_send failed: %d\n", ret);
2118 mad_send_wr = queued_send_wr;
2119 wc->status = IB_WC_LOC_QP_OP_ERR;
2125 static void mark_sends_for_retry(struct ib_mad_qp_info *qp_info)
2127 struct ib_mad_send_wr_private *mad_send_wr;
2128 struct ib_mad_list_head *mad_list;
2129 unsigned long flags;
2131 spin_lock_irqsave(&qp_info->send_queue.lock, flags);
2132 list_for_each_entry(mad_list, &qp_info->send_queue.list, list) {
2133 mad_send_wr = container_of(mad_list,
2134 struct ib_mad_send_wr_private,
2136 mad_send_wr->retry = 1;
2138 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
2141 static void mad_error_handler(struct ib_mad_port_private *port_priv,
2144 struct ib_mad_list_head *mad_list;
2145 struct ib_mad_qp_info *qp_info;
2146 struct ib_mad_send_wr_private *mad_send_wr;
2149 /* Determine if failure was a send or receive */
2150 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
2151 qp_info = mad_list->mad_queue->qp_info;
2152 if (mad_list->mad_queue == &qp_info->recv_queue)
2154 * Receive errors indicate that the QP has entered the error
2155 * state - error handling/shutdown code will cleanup
2160 * Send errors will transition the QP to SQE - move
2161 * QP to RTS and repost flushed work requests
2163 mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
2165 if (wc->status == IB_WC_WR_FLUSH_ERR) {
2166 if (mad_send_wr->retry) {
2168 struct ib_send_wr *bad_send_wr;
2170 mad_send_wr->retry = 0;
2171 ret = ib_post_send(qp_info->qp, &mad_send_wr->send_wr,
2174 ib_mad_send_done_handler(port_priv, wc);
2176 ib_mad_send_done_handler(port_priv, wc);
2178 struct ib_qp_attr *attr;
2180 /* Transition QP to RTS and fail offending send */
2181 attr = kmalloc(sizeof *attr, GFP_KERNEL);
2183 attr->qp_state = IB_QPS_RTS;
2184 attr->cur_qp_state = IB_QPS_SQE;
2185 ret = ib_modify_qp(qp_info->qp, attr,
2186 IB_QP_STATE | IB_QP_CUR_STATE);
2189 printk(KERN_ERR PFX "mad_error_handler - "
2190 "ib_modify_qp to RTS : %d\n", ret);
2192 mark_sends_for_retry(qp_info);
2194 ib_mad_send_done_handler(port_priv, wc);
2199 * IB MAD completion callback
2201 static void ib_mad_completion_handler(void *data)
2203 struct ib_mad_port_private *port_priv;
2206 port_priv = (struct ib_mad_port_private *)data;
2207 ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
2209 while (ib_poll_cq(port_priv->cq, 1, &wc) == 1) {
2210 if (wc.status == IB_WC_SUCCESS) {
2211 switch (wc.opcode) {
2213 ib_mad_send_done_handler(port_priv, &wc);
2216 ib_mad_recv_done_handler(port_priv, &wc);
2223 mad_error_handler(port_priv, &wc);
2227 static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv)
2229 unsigned long flags;
2230 struct ib_mad_send_wr_private *mad_send_wr, *temp_mad_send_wr;
2231 struct ib_mad_send_wc mad_send_wc;
2232 struct list_head cancel_list;
2234 INIT_LIST_HEAD(&cancel_list);
2236 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2237 list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
2238 &mad_agent_priv->send_list, agent_list) {
2239 if (mad_send_wr->status == IB_WC_SUCCESS) {
2240 mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
2241 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2245 /* Empty wait list to prevent receives from finding a request */
2246 list_splice_init(&mad_agent_priv->wait_list, &cancel_list);
2247 /* Empty local completion list as well */
2248 list_splice_init(&mad_agent_priv->local_list, &cancel_list);
2249 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2251 /* Report all cancelled requests */
2252 mad_send_wc.status = IB_WC_WR_FLUSH_ERR;
2253 mad_send_wc.vendor_err = 0;
2255 list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
2256 &cancel_list, agent_list) {
2257 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2258 list_del(&mad_send_wr->agent_list);
2259 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2261 atomic_dec(&mad_agent_priv->refcount);
2265 static struct ib_mad_send_wr_private*
2266 find_send_wr(struct ib_mad_agent_private *mad_agent_priv,
2267 struct ib_mad_send_buf *send_buf)
2269 struct ib_mad_send_wr_private *mad_send_wr;
2271 list_for_each_entry(mad_send_wr, &mad_agent_priv->wait_list,
2273 if (&mad_send_wr->send_buf == send_buf)
2277 list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list,
2279 if (is_data_mad(mad_agent_priv, mad_send_wr->send_buf.mad) &&
2280 &mad_send_wr->send_buf == send_buf)
2286 int ib_modify_mad(struct ib_mad_agent *mad_agent,
2287 struct ib_mad_send_buf *send_buf, u32 timeout_ms)
2289 struct ib_mad_agent_private *mad_agent_priv;
2290 struct ib_mad_send_wr_private *mad_send_wr;
2291 unsigned long flags;
2294 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
2296 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2297 mad_send_wr = find_send_wr(mad_agent_priv, send_buf);
2298 if (!mad_send_wr || mad_send_wr->status != IB_WC_SUCCESS) {
2299 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2303 active = (!mad_send_wr->timeout || mad_send_wr->refcount > 1);
2305 mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
2306 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2309 mad_send_wr->send_buf.timeout_ms = timeout_ms;
2311 mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
2313 ib_reset_mad_timeout(mad_send_wr, timeout_ms);
2315 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2318 EXPORT_SYMBOL(ib_modify_mad);
2320 void ib_cancel_mad(struct ib_mad_agent *mad_agent,
2321 struct ib_mad_send_buf *send_buf)
2323 ib_modify_mad(mad_agent, send_buf, 0);
2325 EXPORT_SYMBOL(ib_cancel_mad);
2327 static void local_completions(void *data)
2329 struct ib_mad_agent_private *mad_agent_priv;
2330 struct ib_mad_local_private *local;
2331 struct ib_mad_agent_private *recv_mad_agent;
2332 unsigned long flags;
2335 struct ib_mad_send_wc mad_send_wc;
2337 mad_agent_priv = (struct ib_mad_agent_private *)data;
2339 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2340 while (!list_empty(&mad_agent_priv->local_list)) {
2341 local = list_entry(mad_agent_priv->local_list.next,
2342 struct ib_mad_local_private,
2344 list_del(&local->completion_list);
2345 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2346 if (local->mad_priv) {
2347 recv_mad_agent = local->recv_mad_agent;
2348 if (!recv_mad_agent) {
2349 printk(KERN_ERR PFX "No receive MAD agent for local completion\n");
2350 goto local_send_completion;
2355 * Defined behavior is to complete response
2358 build_smp_wc((unsigned long) local->mad_send_wr,
2359 be16_to_cpu(IB_LID_PERMISSIVE),
2360 0, recv_mad_agent->agent.port_num, &wc);
2362 local->mad_priv->header.recv_wc.wc = &wc;
2363 local->mad_priv->header.recv_wc.mad_len =
2364 sizeof(struct ib_mad);
2365 INIT_LIST_HEAD(&local->mad_priv->header.recv_wc.rmpp_list);
2366 list_add(&local->mad_priv->header.recv_wc.recv_buf.list,
2367 &local->mad_priv->header.recv_wc.rmpp_list);
2368 local->mad_priv->header.recv_wc.recv_buf.grh = NULL;
2369 local->mad_priv->header.recv_wc.recv_buf.mad =
2370 &local->mad_priv->mad.mad;
2371 if (atomic_read(&recv_mad_agent->qp_info->snoop_count))
2372 snoop_recv(recv_mad_agent->qp_info,
2373 &local->mad_priv->header.recv_wc,
2374 IB_MAD_SNOOP_RECVS);
2375 recv_mad_agent->agent.recv_handler(
2376 &recv_mad_agent->agent,
2377 &local->mad_priv->header.recv_wc);
2378 spin_lock_irqsave(&recv_mad_agent->lock, flags);
2379 atomic_dec(&recv_mad_agent->refcount);
2380 spin_unlock_irqrestore(&recv_mad_agent->lock, flags);
2383 local_send_completion:
2385 mad_send_wc.status = IB_WC_SUCCESS;
2386 mad_send_wc.vendor_err = 0;
2387 mad_send_wc.send_buf = &local->mad_send_wr->send_buf;
2388 if (atomic_read(&mad_agent_priv->qp_info->snoop_count))
2389 snoop_send(mad_agent_priv->qp_info,
2390 &local->mad_send_wr->send_buf,
2391 &mad_send_wc, IB_MAD_SNOOP_SEND_COMPLETIONS);
2392 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2395 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2396 atomic_dec(&mad_agent_priv->refcount);
2398 kmem_cache_free(ib_mad_cache, local->mad_priv);
2401 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2404 static int retry_send(struct ib_mad_send_wr_private *mad_send_wr)
2408 if (!mad_send_wr->retries--)
2411 mad_send_wr->timeout = msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms);
2413 if (mad_send_wr->mad_agent_priv->agent.rmpp_version) {
2414 ret = ib_retry_rmpp(mad_send_wr);
2416 case IB_RMPP_RESULT_UNHANDLED:
2417 ret = ib_send_mad(mad_send_wr);
2419 case IB_RMPP_RESULT_CONSUMED:
2427 ret = ib_send_mad(mad_send_wr);
2430 mad_send_wr->refcount++;
2431 list_add_tail(&mad_send_wr->agent_list,
2432 &mad_send_wr->mad_agent_priv->send_list);
2437 static void timeout_sends(void *data)
2439 struct ib_mad_agent_private *mad_agent_priv;
2440 struct ib_mad_send_wr_private *mad_send_wr;
2441 struct ib_mad_send_wc mad_send_wc;
2442 unsigned long flags, delay;
2444 mad_agent_priv = (struct ib_mad_agent_private *)data;
2445 mad_send_wc.vendor_err = 0;
2447 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2448 while (!list_empty(&mad_agent_priv->wait_list)) {
2449 mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
2450 struct ib_mad_send_wr_private,
2453 if (time_after(mad_send_wr->timeout, jiffies)) {
2454 delay = mad_send_wr->timeout - jiffies;
2455 if ((long)delay <= 0)
2457 queue_delayed_work(mad_agent_priv->qp_info->
2459 &mad_agent_priv->timed_work, delay);
2463 list_del(&mad_send_wr->agent_list);
2464 if (mad_send_wr->status == IB_WC_SUCCESS &&
2465 !retry_send(mad_send_wr))
2468 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2470 if (mad_send_wr->status == IB_WC_SUCCESS)
2471 mad_send_wc.status = IB_WC_RESP_TIMEOUT_ERR;
2473 mad_send_wc.status = mad_send_wr->status;
2474 mad_send_wc.send_buf = &mad_send_wr->send_buf;
2475 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2478 atomic_dec(&mad_agent_priv->refcount);
2479 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2481 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2484 static void ib_mad_thread_completion_handler(struct ib_cq *cq, void *arg)
2486 struct ib_mad_port_private *port_priv = cq->cq_context;
2487 unsigned long flags;
2489 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2490 if (!list_empty(&port_priv->port_list))
2491 queue_work(port_priv->wq, &port_priv->work);
2492 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2496 * Allocate receive MADs and post receive WRs for them
2498 static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
2499 struct ib_mad_private *mad)
2501 unsigned long flags;
2503 struct ib_mad_private *mad_priv;
2504 struct ib_sge sg_list;
2505 struct ib_recv_wr recv_wr, *bad_recv_wr;
2506 struct ib_mad_queue *recv_queue = &qp_info->recv_queue;
2508 /* Initialize common scatter list fields */
2509 sg_list.length = sizeof *mad_priv - sizeof mad_priv->header;
2510 sg_list.lkey = (*qp_info->port_priv->mr).lkey;
2512 /* Initialize common receive WR fields */
2513 recv_wr.next = NULL;
2514 recv_wr.sg_list = &sg_list;
2515 recv_wr.num_sge = 1;
2518 /* Allocate and map receive buffer */
2523 mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL);
2525 printk(KERN_ERR PFX "No memory for receive buffer\n");
2530 sg_list.addr = dma_map_single(qp_info->port_priv->
2534 sizeof mad_priv->header,
2536 pci_unmap_addr_set(&mad_priv->header, mapping, sg_list.addr);
2537 recv_wr.wr_id = (unsigned long)&mad_priv->header.mad_list;
2538 mad_priv->header.mad_list.mad_queue = recv_queue;
2540 /* Post receive WR */
2541 spin_lock_irqsave(&recv_queue->lock, flags);
2542 post = (++recv_queue->count < recv_queue->max_active);
2543 list_add_tail(&mad_priv->header.mad_list.list, &recv_queue->list);
2544 spin_unlock_irqrestore(&recv_queue->lock, flags);
2545 ret = ib_post_recv(qp_info->qp, &recv_wr, &bad_recv_wr);
2547 spin_lock_irqsave(&recv_queue->lock, flags);
2548 list_del(&mad_priv->header.mad_list.list);
2549 recv_queue->count--;
2550 spin_unlock_irqrestore(&recv_queue->lock, flags);
2551 dma_unmap_single(qp_info->port_priv->device->dma_device,
2552 pci_unmap_addr(&mad_priv->header,
2555 sizeof mad_priv->header,
2557 kmem_cache_free(ib_mad_cache, mad_priv);
2558 printk(KERN_ERR PFX "ib_post_recv failed: %d\n", ret);
2567 * Return all the posted receive MADs
2569 static void cleanup_recv_queue(struct ib_mad_qp_info *qp_info)
2571 struct ib_mad_private_header *mad_priv_hdr;
2572 struct ib_mad_private *recv;
2573 struct ib_mad_list_head *mad_list;
2575 while (!list_empty(&qp_info->recv_queue.list)) {
2577 mad_list = list_entry(qp_info->recv_queue.list.next,
2578 struct ib_mad_list_head, list);
2579 mad_priv_hdr = container_of(mad_list,
2580 struct ib_mad_private_header,
2582 recv = container_of(mad_priv_hdr, struct ib_mad_private,
2585 /* Remove from posted receive MAD list */
2586 list_del(&mad_list->list);
2588 dma_unmap_single(qp_info->port_priv->device->dma_device,
2589 pci_unmap_addr(&recv->header, mapping),
2590 sizeof(struct ib_mad_private) -
2591 sizeof(struct ib_mad_private_header),
2593 kmem_cache_free(ib_mad_cache, recv);
2596 qp_info->recv_queue.count = 0;
2602 static int ib_mad_port_start(struct ib_mad_port_private *port_priv)
2605 struct ib_qp_attr *attr;
2608 attr = kmalloc(sizeof *attr, GFP_KERNEL);
2610 printk(KERN_ERR PFX "Couldn't kmalloc ib_qp_attr\n");
2614 for (i = 0; i < IB_MAD_QPS_CORE; i++) {
2615 qp = port_priv->qp_info[i].qp;
2617 * PKey index for QP1 is irrelevant but
2618 * one is needed for the Reset to Init transition
2620 attr->qp_state = IB_QPS_INIT;
2621 attr->pkey_index = 0;
2622 attr->qkey = (qp->qp_num == 0) ? 0 : IB_QP1_QKEY;
2623 ret = ib_modify_qp(qp, attr, IB_QP_STATE |
2624 IB_QP_PKEY_INDEX | IB_QP_QKEY);
2626 printk(KERN_ERR PFX "Couldn't change QP%d state to "
2627 "INIT: %d\n", i, ret);
2631 attr->qp_state = IB_QPS_RTR;
2632 ret = ib_modify_qp(qp, attr, IB_QP_STATE);
2634 printk(KERN_ERR PFX "Couldn't change QP%d state to "
2635 "RTR: %d\n", i, ret);
2639 attr->qp_state = IB_QPS_RTS;
2640 attr->sq_psn = IB_MAD_SEND_Q_PSN;
2641 ret = ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_SQ_PSN);
2643 printk(KERN_ERR PFX "Couldn't change QP%d state to "
2644 "RTS: %d\n", i, ret);
2649 ret = ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
2651 printk(KERN_ERR PFX "Failed to request completion "
2652 "notification: %d\n", ret);
2656 for (i = 0; i < IB_MAD_QPS_CORE; i++) {
2657 ret = ib_mad_post_receive_mads(&port_priv->qp_info[i], NULL);
2659 printk(KERN_ERR PFX "Couldn't post receive WRs\n");
2668 static void qp_event_handler(struct ib_event *event, void *qp_context)
2670 struct ib_mad_qp_info *qp_info = qp_context;
2672 /* It's worse than that! He's dead, Jim! */
2673 printk(KERN_ERR PFX "Fatal error (%d) on MAD QP (%d)\n",
2674 event->event, qp_info->qp->qp_num);
2677 static void init_mad_queue(struct ib_mad_qp_info *qp_info,
2678 struct ib_mad_queue *mad_queue)
2680 mad_queue->qp_info = qp_info;
2681 mad_queue->count = 0;
2682 spin_lock_init(&mad_queue->lock);
2683 INIT_LIST_HEAD(&mad_queue->list);
2686 static void init_mad_qp(struct ib_mad_port_private *port_priv,
2687 struct ib_mad_qp_info *qp_info)
2689 qp_info->port_priv = port_priv;
2690 init_mad_queue(qp_info, &qp_info->send_queue);
2691 init_mad_queue(qp_info, &qp_info->recv_queue);
2692 INIT_LIST_HEAD(&qp_info->overflow_list);
2693 spin_lock_init(&qp_info->snoop_lock);
2694 qp_info->snoop_table = NULL;
2695 qp_info->snoop_table_size = 0;
2696 atomic_set(&qp_info->snoop_count, 0);
2699 static int create_mad_qp(struct ib_mad_qp_info *qp_info,
2700 enum ib_qp_type qp_type)
2702 struct ib_qp_init_attr qp_init_attr;
2705 memset(&qp_init_attr, 0, sizeof qp_init_attr);
2706 qp_init_attr.send_cq = qp_info->port_priv->cq;
2707 qp_init_attr.recv_cq = qp_info->port_priv->cq;
2708 qp_init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
2709 qp_init_attr.cap.max_send_wr = IB_MAD_QP_SEND_SIZE;
2710 qp_init_attr.cap.max_recv_wr = IB_MAD_QP_RECV_SIZE;
2711 qp_init_attr.cap.max_send_sge = IB_MAD_SEND_REQ_MAX_SG;
2712 qp_init_attr.cap.max_recv_sge = IB_MAD_RECV_REQ_MAX_SG;
2713 qp_init_attr.qp_type = qp_type;
2714 qp_init_attr.port_num = qp_info->port_priv->port_num;
2715 qp_init_attr.qp_context = qp_info;
2716 qp_init_attr.event_handler = qp_event_handler;
2717 qp_info->qp = ib_create_qp(qp_info->port_priv->pd, &qp_init_attr);
2718 if (IS_ERR(qp_info->qp)) {
2719 printk(KERN_ERR PFX "Couldn't create ib_mad QP%d\n",
2720 get_spl_qp_index(qp_type));
2721 ret = PTR_ERR(qp_info->qp);
2724 /* Use minimum queue sizes unless the CQ is resized */
2725 qp_info->send_queue.max_active = IB_MAD_QP_SEND_SIZE;
2726 qp_info->recv_queue.max_active = IB_MAD_QP_RECV_SIZE;
2733 static void destroy_mad_qp(struct ib_mad_qp_info *qp_info)
2735 ib_destroy_qp(qp_info->qp);
2736 kfree(qp_info->snoop_table);
2741 * Create the QP, PD, MR, and CQ if needed
2743 static int ib_mad_port_open(struct ib_device *device,
2747 struct ib_mad_port_private *port_priv;
2748 unsigned long flags;
2749 char name[sizeof "ib_mad123"];
2751 /* Create new device info */
2752 port_priv = kzalloc(sizeof *port_priv, GFP_KERNEL);
2754 printk(KERN_ERR PFX "No memory for ib_mad_port_private\n");
2758 port_priv->device = device;
2759 port_priv->port_num = port_num;
2760 spin_lock_init(&port_priv->reg_lock);
2761 INIT_LIST_HEAD(&port_priv->agent_list);
2762 init_mad_qp(port_priv, &port_priv->qp_info[0]);
2763 init_mad_qp(port_priv, &port_priv->qp_info[1]);
2765 cq_size = (IB_MAD_QP_SEND_SIZE + IB_MAD_QP_RECV_SIZE) * 2;
2766 port_priv->cq = ib_create_cq(port_priv->device,
2767 ib_mad_thread_completion_handler,
2768 NULL, port_priv, cq_size);
2769 if (IS_ERR(port_priv->cq)) {
2770 printk(KERN_ERR PFX "Couldn't create ib_mad CQ\n");
2771 ret = PTR_ERR(port_priv->cq);
2775 port_priv->pd = ib_alloc_pd(device);
2776 if (IS_ERR(port_priv->pd)) {
2777 printk(KERN_ERR PFX "Couldn't create ib_mad PD\n");
2778 ret = PTR_ERR(port_priv->pd);
2782 port_priv->mr = ib_get_dma_mr(port_priv->pd, IB_ACCESS_LOCAL_WRITE);
2783 if (IS_ERR(port_priv->mr)) {
2784 printk(KERN_ERR PFX "Couldn't get ib_mad DMA MR\n");
2785 ret = PTR_ERR(port_priv->mr);
2789 ret = create_mad_qp(&port_priv->qp_info[0], IB_QPT_SMI);
2792 ret = create_mad_qp(&port_priv->qp_info[1], IB_QPT_GSI);
2796 snprintf(name, sizeof name, "ib_mad%d", port_num);
2797 port_priv->wq = create_singlethread_workqueue(name);
2798 if (!port_priv->wq) {
2802 INIT_WORK(&port_priv->work, ib_mad_completion_handler, port_priv);
2804 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2805 list_add_tail(&port_priv->port_list, &ib_mad_port_list);
2806 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2808 ret = ib_mad_port_start(port_priv);
2810 printk(KERN_ERR PFX "Couldn't start port\n");
2817 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2818 list_del_init(&port_priv->port_list);
2819 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2821 destroy_workqueue(port_priv->wq);
2823 destroy_mad_qp(&port_priv->qp_info[1]);
2825 destroy_mad_qp(&port_priv->qp_info[0]);
2827 ib_dereg_mr(port_priv->mr);
2829 ib_dealloc_pd(port_priv->pd);
2831 ib_destroy_cq(port_priv->cq);
2832 cleanup_recv_queue(&port_priv->qp_info[1]);
2833 cleanup_recv_queue(&port_priv->qp_info[0]);
2842 * If there are no classes using the port, free the port
2843 * resources (CQ, MR, PD, QP) and remove the port's info structure
2845 static int ib_mad_port_close(struct ib_device *device, int port_num)
2847 struct ib_mad_port_private *port_priv;
2848 unsigned long flags;
2850 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2851 port_priv = __ib_get_mad_port(device, port_num);
2852 if (port_priv == NULL) {
2853 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2854 printk(KERN_ERR PFX "Port %d not found\n", port_num);
2857 list_del_init(&port_priv->port_list);
2858 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2860 destroy_workqueue(port_priv->wq);
2861 destroy_mad_qp(&port_priv->qp_info[1]);
2862 destroy_mad_qp(&port_priv->qp_info[0]);
2863 ib_dereg_mr(port_priv->mr);
2864 ib_dealloc_pd(port_priv->pd);
2865 ib_destroy_cq(port_priv->cq);
2866 cleanup_recv_queue(&port_priv->qp_info[1]);
2867 cleanup_recv_queue(&port_priv->qp_info[0]);
2868 /* XXX: Handle deallocation of MAD registration tables */
2875 static void ib_mad_init_device(struct ib_device *device)
2879 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
2882 if (device->node_type == RDMA_NODE_IB_SWITCH) {
2887 end = device->phys_port_cnt;
2890 for (i = start; i <= end; i++) {
2891 if (ib_mad_port_open(device, i)) {
2892 printk(KERN_ERR PFX "Couldn't open %s port %d\n",
2896 if (ib_agent_port_open(device, i)) {
2897 printk(KERN_ERR PFX "Couldn't open %s port %d "
2906 if (ib_mad_port_close(device, i))
2907 printk(KERN_ERR PFX "Couldn't close %s port %d\n",
2913 while (i >= start) {
2914 if (ib_agent_port_close(device, i))
2915 printk(KERN_ERR PFX "Couldn't close %s port %d "
2918 if (ib_mad_port_close(device, i))
2919 printk(KERN_ERR PFX "Couldn't close %s port %d\n",
2925 static void ib_mad_remove_device(struct ib_device *device)
2927 int i, num_ports, cur_port;
2929 if (device->node_type == RDMA_NODE_IB_SWITCH) {
2933 num_ports = device->phys_port_cnt;
2936 for (i = 0; i < num_ports; i++, cur_port++) {
2937 if (ib_agent_port_close(device, cur_port))
2938 printk(KERN_ERR PFX "Couldn't close %s port %d "
2940 device->name, cur_port);
2941 if (ib_mad_port_close(device, cur_port))
2942 printk(KERN_ERR PFX "Couldn't close %s port %d\n",
2943 device->name, cur_port);
2947 static struct ib_client mad_client = {
2949 .add = ib_mad_init_device,
2950 .remove = ib_mad_remove_device
2953 static int __init ib_mad_init_module(void)
2957 spin_lock_init(&ib_mad_port_list_lock);
2959 ib_mad_cache = kmem_cache_create("ib_mad",
2960 sizeof(struct ib_mad_private),
2965 if (!ib_mad_cache) {
2966 printk(KERN_ERR PFX "Couldn't create ib_mad cache\n");
2971 INIT_LIST_HEAD(&ib_mad_port_list);
2973 if (ib_register_client(&mad_client)) {
2974 printk(KERN_ERR PFX "Couldn't register ib_mad client\n");
2982 kmem_cache_destroy(ib_mad_cache);
2987 static void __exit ib_mad_cleanup_module(void)
2989 ib_unregister_client(&mad_client);
2991 if (kmem_cache_destroy(ib_mad_cache)) {
2992 printk(KERN_DEBUG PFX "Failed to destroy ib_mad cache\n");
2996 module_init(ib_mad_init_module);
2997 module_exit(ib_mad_cleanup_module);