2 * Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * $Id: mad.c 1389 2004-12-27 22:56:47Z roland $
35 #include <linux/dma-mapping.h>
36 #include <linux/interrupt.h>
45 MODULE_LICENSE("Dual BSD/GPL");
46 MODULE_DESCRIPTION("kernel IB MAD API");
47 MODULE_AUTHOR("Hal Rosenstock");
48 MODULE_AUTHOR("Sean Hefty");
51 kmem_cache_t *ib_mad_cache;
52 static struct list_head ib_mad_port_list;
53 static u32 ib_mad_client_id = 0;
56 static spinlock_t ib_mad_port_list_lock;
59 /* Forward declarations */
60 static int method_in_use(struct ib_mad_mgmt_method_table **method,
61 struct ib_mad_reg_req *mad_reg_req);
62 static void remove_mad_reg_req(struct ib_mad_agent_private *priv);
63 static struct ib_mad_agent_private *find_mad_agent(
64 struct ib_mad_port_private *port_priv,
65 struct ib_mad *mad, int solicited);
66 static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
67 struct ib_mad_private *mad);
68 static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv);
69 static void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr,
70 struct ib_mad_send_wc *mad_send_wc);
71 static void timeout_sends(void *data);
72 static void local_completions(void *data);
73 static int solicited_mad(struct ib_mad *mad);
74 static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
75 struct ib_mad_agent_private *agent_priv,
77 static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
78 struct ib_mad_agent_private *agent_priv);
81 * Returns a ib_mad_port_private structure or NULL for a device/port
82 * Assumes ib_mad_port_list_lock is being held
84 static inline struct ib_mad_port_private *
85 __ib_get_mad_port(struct ib_device *device, int port_num)
87 struct ib_mad_port_private *entry;
89 list_for_each_entry(entry, &ib_mad_port_list, port_list) {
90 if (entry->device == device && entry->port_num == port_num)
97 * Wrapper function to return a ib_mad_port_private structure or NULL
100 static inline struct ib_mad_port_private *
101 ib_get_mad_port(struct ib_device *device, int port_num)
103 struct ib_mad_port_private *entry;
106 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
107 entry = __ib_get_mad_port(device, port_num);
108 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
113 static inline u8 convert_mgmt_class(u8 mgmt_class)
115 /* Alias IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE to 0 */
116 return mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE ?
120 static int get_spl_qp_index(enum ib_qp_type qp_type)
133 static int vendor_class_index(u8 mgmt_class)
135 return mgmt_class - IB_MGMT_CLASS_VENDOR_RANGE2_START;
138 static int is_vendor_class(u8 mgmt_class)
140 if ((mgmt_class < IB_MGMT_CLASS_VENDOR_RANGE2_START) ||
141 (mgmt_class > IB_MGMT_CLASS_VENDOR_RANGE2_END))
146 static int is_vendor_oui(char *oui)
148 if (oui[0] || oui[1] || oui[2])
153 static int is_vendor_method_in_use(
154 struct ib_mad_mgmt_vendor_class *vendor_class,
155 struct ib_mad_reg_req *mad_reg_req)
157 struct ib_mad_mgmt_method_table *method;
160 for (i = 0; i < MAX_MGMT_OUI; i++) {
161 if (!memcmp(vendor_class->oui[i], mad_reg_req->oui, 3)) {
162 method = vendor_class->method_table[i];
164 if (method_in_use(&method, mad_reg_req))
175 * ib_register_mad_agent - Register to send/receive MADs
177 struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
179 enum ib_qp_type qp_type,
180 struct ib_mad_reg_req *mad_reg_req,
182 ib_mad_send_handler send_handler,
183 ib_mad_recv_handler recv_handler,
186 struct ib_mad_port_private *port_priv;
187 struct ib_mad_agent *ret = ERR_PTR(-EINVAL);
188 struct ib_mad_agent_private *mad_agent_priv;
189 struct ib_mad_reg_req *reg_req = NULL;
190 struct ib_mad_mgmt_class_table *class;
191 struct ib_mad_mgmt_vendor_class_table *vendor;
192 struct ib_mad_mgmt_vendor_class *vendor_class;
193 struct ib_mad_mgmt_method_table *method;
196 u8 mgmt_class, vclass;
198 /* Validate parameters */
199 qpn = get_spl_qp_index(qp_type);
204 goto error1; /* XXX: until RMPP implemented */
206 /* Validate MAD registration request if supplied */
208 if (mad_reg_req->mgmt_class_version >= MAX_MGMT_VERSION)
212 if (mad_reg_req->mgmt_class >= MAX_MGMT_CLASS) {
214 * IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE is the only
215 * one in this range currently allowed
217 if (mad_reg_req->mgmt_class !=
218 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
220 } else if (mad_reg_req->mgmt_class == 0) {
222 * Class 0 is reserved in IBA and is used for
223 * aliasing of IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
226 } else if (is_vendor_class(mad_reg_req->mgmt_class)) {
228 * If class is in "new" vendor range,
229 * ensure supplied OUI is not zero
231 if (!is_vendor_oui(mad_reg_req->oui))
234 /* Make sure class supplied is consistent with QP type */
235 if (qp_type == IB_QPT_SMI) {
236 if ((mad_reg_req->mgmt_class !=
237 IB_MGMT_CLASS_SUBN_LID_ROUTED) &&
238 (mad_reg_req->mgmt_class !=
239 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE))
242 if ((mad_reg_req->mgmt_class ==
243 IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
244 (mad_reg_req->mgmt_class ==
245 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE))
249 /* No registration request supplied */
254 /* Validate device and port */
255 port_priv = ib_get_mad_port(device, port_num);
257 ret = ERR_PTR(-ENODEV);
261 /* Allocate structures */
262 mad_agent_priv = kmalloc(sizeof *mad_agent_priv, GFP_KERNEL);
263 if (!mad_agent_priv) {
264 ret = ERR_PTR(-ENOMEM);
269 reg_req = kmalloc(sizeof *reg_req, GFP_KERNEL);
271 ret = ERR_PTR(-ENOMEM);
274 /* Make a copy of the MAD registration request */
275 memcpy(reg_req, mad_reg_req, sizeof *reg_req);
278 /* Now, fill in the various structures */
279 memset(mad_agent_priv, 0, sizeof *mad_agent_priv);
280 mad_agent_priv->qp_info = &port_priv->qp_info[qpn];
281 mad_agent_priv->reg_req = reg_req;
282 mad_agent_priv->rmpp_version = rmpp_version;
283 mad_agent_priv->agent.device = device;
284 mad_agent_priv->agent.recv_handler = recv_handler;
285 mad_agent_priv->agent.send_handler = send_handler;
286 mad_agent_priv->agent.context = context;
287 mad_agent_priv->agent.qp = port_priv->qp_info[qpn].qp;
288 mad_agent_priv->agent.port_num = port_num;
290 spin_lock_irqsave(&port_priv->reg_lock, flags);
291 mad_agent_priv->agent.hi_tid = ++ib_mad_client_id;
294 * Make sure MAD registration (if supplied)
295 * is non overlapping with any existing ones
298 mgmt_class = convert_mgmt_class(mad_reg_req->mgmt_class);
299 if (!is_vendor_class(mgmt_class)) {
300 class = port_priv->version[mad_reg_req->
301 mgmt_class_version].class;
303 method = class->method_table[mgmt_class];
305 if (method_in_use(&method,
310 ret2 = add_nonoui_reg_req(mad_reg_req, mad_agent_priv,
313 /* "New" vendor class range */
314 vendor = port_priv->version[mad_reg_req->
315 mgmt_class_version].vendor;
317 vclass = vendor_class_index(mgmt_class);
318 vendor_class = vendor->vendor_class[vclass];
320 if (is_vendor_method_in_use(
326 ret2 = add_oui_reg_req(mad_reg_req, mad_agent_priv);
334 /* Add mad agent into port's agent list */
335 list_add_tail(&mad_agent_priv->agent_list, &port_priv->agent_list);
336 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
338 spin_lock_init(&mad_agent_priv->lock);
339 INIT_LIST_HEAD(&mad_agent_priv->send_list);
340 INIT_LIST_HEAD(&mad_agent_priv->wait_list);
341 INIT_WORK(&mad_agent_priv->timed_work, timeout_sends, mad_agent_priv);
342 INIT_LIST_HEAD(&mad_agent_priv->local_list);
343 INIT_WORK(&mad_agent_priv->local_work, local_completions,
345 atomic_set(&mad_agent_priv->refcount, 1);
346 init_waitqueue_head(&mad_agent_priv->wait);
348 return &mad_agent_priv->agent;
351 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
354 kfree(mad_agent_priv);
358 EXPORT_SYMBOL(ib_register_mad_agent);
360 static inline int is_snooping_sends(int mad_snoop_flags)
362 return (mad_snoop_flags &
363 (/*IB_MAD_SNOOP_POSTED_SENDS |
364 IB_MAD_SNOOP_RMPP_SENDS |*/
365 IB_MAD_SNOOP_SEND_COMPLETIONS /*|
366 IB_MAD_SNOOP_RMPP_SEND_COMPLETIONS*/));
369 static inline int is_snooping_recvs(int mad_snoop_flags)
371 return (mad_snoop_flags &
372 (IB_MAD_SNOOP_RECVS /*|
373 IB_MAD_SNOOP_RMPP_RECVS*/));
376 static int register_snoop_agent(struct ib_mad_qp_info *qp_info,
377 struct ib_mad_snoop_private *mad_snoop_priv)
379 struct ib_mad_snoop_private **new_snoop_table;
383 spin_lock_irqsave(&qp_info->snoop_lock, flags);
384 /* Check for empty slot in array. */
385 for (i = 0; i < qp_info->snoop_table_size; i++)
386 if (!qp_info->snoop_table[i])
389 if (i == qp_info->snoop_table_size) {
391 new_snoop_table = kmalloc(sizeof mad_snoop_priv *
392 qp_info->snoop_table_size + 1,
394 if (!new_snoop_table) {
398 if (qp_info->snoop_table) {
399 memcpy(new_snoop_table, qp_info->snoop_table,
400 sizeof mad_snoop_priv *
401 qp_info->snoop_table_size);
402 kfree(qp_info->snoop_table);
404 qp_info->snoop_table = new_snoop_table;
405 qp_info->snoop_table_size++;
407 qp_info->snoop_table[i] = mad_snoop_priv;
408 atomic_inc(&qp_info->snoop_count);
410 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
414 struct ib_mad_agent *ib_register_mad_snoop(struct ib_device *device,
416 enum ib_qp_type qp_type,
418 ib_mad_snoop_handler snoop_handler,
419 ib_mad_recv_handler recv_handler,
422 struct ib_mad_port_private *port_priv;
423 struct ib_mad_agent *ret;
424 struct ib_mad_snoop_private *mad_snoop_priv;
427 /* Validate parameters */
428 if ((is_snooping_sends(mad_snoop_flags) && !snoop_handler) ||
429 (is_snooping_recvs(mad_snoop_flags) && !recv_handler)) {
430 ret = ERR_PTR(-EINVAL);
433 qpn = get_spl_qp_index(qp_type);
435 ret = ERR_PTR(-EINVAL);
438 port_priv = ib_get_mad_port(device, port_num);
440 ret = ERR_PTR(-ENODEV);
443 /* Allocate structures */
444 mad_snoop_priv = kmalloc(sizeof *mad_snoop_priv, GFP_KERNEL);
445 if (!mad_snoop_priv) {
446 ret = ERR_PTR(-ENOMEM);
450 /* Now, fill in the various structures */
451 memset(mad_snoop_priv, 0, sizeof *mad_snoop_priv);
452 mad_snoop_priv->qp_info = &port_priv->qp_info[qpn];
453 mad_snoop_priv->agent.device = device;
454 mad_snoop_priv->agent.recv_handler = recv_handler;
455 mad_snoop_priv->agent.snoop_handler = snoop_handler;
456 mad_snoop_priv->agent.context = context;
457 mad_snoop_priv->agent.qp = port_priv->qp_info[qpn].qp;
458 mad_snoop_priv->agent.port_num = port_num;
459 mad_snoop_priv->mad_snoop_flags = mad_snoop_flags;
460 init_waitqueue_head(&mad_snoop_priv->wait);
461 mad_snoop_priv->snoop_index = register_snoop_agent(
462 &port_priv->qp_info[qpn],
464 if (mad_snoop_priv->snoop_index < 0) {
465 ret = ERR_PTR(mad_snoop_priv->snoop_index);
469 atomic_set(&mad_snoop_priv->refcount, 1);
470 return &mad_snoop_priv->agent;
473 kfree(mad_snoop_priv);
477 EXPORT_SYMBOL(ib_register_mad_snoop);
479 static void unregister_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
481 struct ib_mad_port_private *port_priv;
484 /* Note that we could still be handling received MADs */
487 * Canceling all sends results in dropping received response
488 * MADs, preventing us from queuing additional work
490 cancel_mads(mad_agent_priv);
492 port_priv = mad_agent_priv->qp_info->port_priv;
493 cancel_delayed_work(&mad_agent_priv->timed_work);
494 flush_workqueue(port_priv->wq);
496 spin_lock_irqsave(&port_priv->reg_lock, flags);
497 remove_mad_reg_req(mad_agent_priv);
498 list_del(&mad_agent_priv->agent_list);
499 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
501 /* XXX: Cleanup pending RMPP receives for this agent */
503 atomic_dec(&mad_agent_priv->refcount);
504 wait_event(mad_agent_priv->wait,
505 !atomic_read(&mad_agent_priv->refcount));
507 if (mad_agent_priv->reg_req)
508 kfree(mad_agent_priv->reg_req);
509 kfree(mad_agent_priv);
512 static void unregister_mad_snoop(struct ib_mad_snoop_private *mad_snoop_priv)
514 struct ib_mad_qp_info *qp_info;
517 qp_info = mad_snoop_priv->qp_info;
518 spin_lock_irqsave(&qp_info->snoop_lock, flags);
519 qp_info->snoop_table[mad_snoop_priv->snoop_index] = NULL;
520 atomic_dec(&qp_info->snoop_count);
521 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
523 atomic_dec(&mad_snoop_priv->refcount);
524 wait_event(mad_snoop_priv->wait,
525 !atomic_read(&mad_snoop_priv->refcount));
527 kfree(mad_snoop_priv);
531 * ib_unregister_mad_agent - Unregisters a client from using MAD services
533 int ib_unregister_mad_agent(struct ib_mad_agent *mad_agent)
535 struct ib_mad_agent_private *mad_agent_priv;
536 struct ib_mad_snoop_private *mad_snoop_priv;
538 /* If the TID is zero, the agent can only snoop. */
539 if (mad_agent->hi_tid) {
540 mad_agent_priv = container_of(mad_agent,
541 struct ib_mad_agent_private,
543 unregister_mad_agent(mad_agent_priv);
545 mad_snoop_priv = container_of(mad_agent,
546 struct ib_mad_snoop_private,
548 unregister_mad_snoop(mad_snoop_priv);
552 EXPORT_SYMBOL(ib_unregister_mad_agent);
554 static void dequeue_mad(struct ib_mad_list_head *mad_list)
556 struct ib_mad_queue *mad_queue;
559 BUG_ON(!mad_list->mad_queue);
560 mad_queue = mad_list->mad_queue;
561 spin_lock_irqsave(&mad_queue->lock, flags);
562 list_del(&mad_list->list);
564 spin_unlock_irqrestore(&mad_queue->lock, flags);
567 static void snoop_send(struct ib_mad_qp_info *qp_info,
568 struct ib_send_wr *send_wr,
569 struct ib_mad_send_wc *mad_send_wc,
572 struct ib_mad_snoop_private *mad_snoop_priv;
576 spin_lock_irqsave(&qp_info->snoop_lock, flags);
577 for (i = 0; i < qp_info->snoop_table_size; i++) {
578 mad_snoop_priv = qp_info->snoop_table[i];
579 if (!mad_snoop_priv ||
580 !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
583 atomic_inc(&mad_snoop_priv->refcount);
584 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
585 mad_snoop_priv->agent.snoop_handler(&mad_snoop_priv->agent,
586 send_wr, mad_send_wc);
587 if (atomic_dec_and_test(&mad_snoop_priv->refcount))
588 wake_up(&mad_snoop_priv->wait);
589 spin_lock_irqsave(&qp_info->snoop_lock, flags);
591 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
594 static void snoop_recv(struct ib_mad_qp_info *qp_info,
595 struct ib_mad_recv_wc *mad_recv_wc,
598 struct ib_mad_snoop_private *mad_snoop_priv;
602 spin_lock_irqsave(&qp_info->snoop_lock, flags);
603 for (i = 0; i < qp_info->snoop_table_size; i++) {
604 mad_snoop_priv = qp_info->snoop_table[i];
605 if (!mad_snoop_priv ||
606 !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
609 atomic_inc(&mad_snoop_priv->refcount);
610 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
611 mad_snoop_priv->agent.recv_handler(&mad_snoop_priv->agent,
613 if (atomic_dec_and_test(&mad_snoop_priv->refcount))
614 wake_up(&mad_snoop_priv->wait);
615 spin_lock_irqsave(&qp_info->snoop_lock, flags);
617 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
620 static void build_smp_wc(u64 wr_id, u16 slid, u16 pkey_index, u8 port_num,
623 memset(wc, 0, sizeof *wc);
625 wc->status = IB_WC_SUCCESS;
626 wc->opcode = IB_WC_RECV;
627 wc->pkey_index = pkey_index;
628 wc->byte_len = sizeof(struct ib_mad) + sizeof(struct ib_grh);
633 wc->dlid_path_bits = 0;
634 wc->port_num = port_num;
638 * Return 0 if SMP is to be sent
639 * Return 1 if SMP was consumed locally (whether or not solicited)
640 * Return < 0 if error
642 static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
644 struct ib_send_wr *send_wr)
646 int ret, alloc_flags, solicited;
648 struct ib_mad_local_private *local;
649 struct ib_mad_private *mad_priv;
650 struct ib_mad_port_private *port_priv;
651 struct ib_mad_agent_private *recv_mad_agent = NULL;
652 struct ib_device *device = mad_agent_priv->agent.device;
653 u8 port_num = mad_agent_priv->agent.port_num;
656 if (!smi_handle_dr_smp_send(smp, device->node_type, port_num)) {
658 printk(KERN_ERR PFX "Invalid directed route\n");
661 /* Check to post send on QP or process locally */
662 ret = smi_check_local_dr_smp(smp, device, port_num);
663 if (!ret || !device->process_mad)
666 if (in_atomic() || irqs_disabled())
667 alloc_flags = GFP_ATOMIC;
669 alloc_flags = GFP_KERNEL;
670 local = kmalloc(sizeof *local, alloc_flags);
673 printk(KERN_ERR PFX "No memory for ib_mad_local_private\n");
676 local->mad_priv = NULL;
677 local->recv_mad_agent = NULL;
678 mad_priv = kmem_cache_alloc(ib_mad_cache, alloc_flags);
681 printk(KERN_ERR PFX "No memory for local response MAD\n");
686 build_smp_wc(send_wr->wr_id, smp->dr_slid, send_wr->wr.ud.pkey_index,
687 send_wr->wr.ud.port_num, &mad_wc);
689 /* No GRH for DR SMP */
690 ret = device->process_mad(device, 0, port_num, &mad_wc, NULL,
691 (struct ib_mad *)smp,
692 (struct ib_mad *)&mad_priv->mad);
695 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY:
697 * See if response is solicited and
698 * there is a recv handler
700 if (solicited_mad(&mad_priv->mad.mad) &&
701 mad_agent_priv->agent.recv_handler) {
702 local->mad_priv = mad_priv;
703 local->recv_mad_agent = mad_agent_priv;
705 * Reference MAD agent until receive
706 * side of local completion handled
708 atomic_inc(&mad_agent_priv->refcount);
710 kmem_cache_free(ib_mad_cache, mad_priv);
712 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED:
713 kmem_cache_free(ib_mad_cache, mad_priv);
715 case IB_MAD_RESULT_SUCCESS:
716 /* Treat like an incoming receive MAD */
717 solicited = solicited_mad(&mad_priv->mad.mad);
718 port_priv = ib_get_mad_port(mad_agent_priv->agent.device,
719 mad_agent_priv->agent.port_num);
721 mad_priv->mad.mad.mad_hdr.tid =
722 ((struct ib_mad *)smp)->mad_hdr.tid;
723 recv_mad_agent = find_mad_agent(port_priv,
727 if (!port_priv || !recv_mad_agent) {
728 kmem_cache_free(ib_mad_cache, mad_priv);
733 local->mad_priv = mad_priv;
734 local->recv_mad_agent = recv_mad_agent;
737 kmem_cache_free(ib_mad_cache, mad_priv);
743 local->send_wr = *send_wr;
744 local->send_wr.sg_list = local->sg_list;
745 memcpy(local->sg_list, send_wr->sg_list,
746 sizeof *send_wr->sg_list * send_wr->num_sge);
747 local->send_wr.next = NULL;
748 local->tid = send_wr->wr.ud.mad_hdr->tid;
749 local->wr_id = send_wr->wr_id;
750 /* Reference MAD agent until send side of local completion handled */
751 atomic_inc(&mad_agent_priv->refcount);
752 /* Queue local completion to local list */
753 spin_lock_irqsave(&mad_agent_priv->lock, flags);
754 list_add_tail(&local->completion_list, &mad_agent_priv->local_list);
755 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
756 queue_work(mad_agent_priv->qp_info->port_priv->wq,
757 &mad_agent_priv->local_work);
763 static int ib_send_mad(struct ib_mad_agent_private *mad_agent_priv,
764 struct ib_mad_send_wr_private *mad_send_wr)
766 struct ib_mad_qp_info *qp_info;
767 struct ib_send_wr *bad_send_wr;
771 /* Replace user's WR ID with our own to find WR upon completion */
772 qp_info = mad_agent_priv->qp_info;
773 mad_send_wr->wr_id = mad_send_wr->send_wr.wr_id;
774 mad_send_wr->send_wr.wr_id = (unsigned long)&mad_send_wr->mad_list;
775 mad_send_wr->mad_list.mad_queue = &qp_info->send_queue;
777 spin_lock_irqsave(&qp_info->send_queue.lock, flags);
778 if (qp_info->send_queue.count++ < qp_info->send_queue.max_active) {
779 list_add_tail(&mad_send_wr->mad_list.list,
780 &qp_info->send_queue.list);
781 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
782 ret = ib_post_send(mad_agent_priv->agent.qp,
783 &mad_send_wr->send_wr, &bad_send_wr);
785 printk(KERN_ERR PFX "ib_post_send failed: %d\n", ret);
786 dequeue_mad(&mad_send_wr->mad_list);
789 list_add_tail(&mad_send_wr->mad_list.list,
790 &qp_info->overflow_list);
791 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
798 * ib_post_send_mad - Posts MAD(s) to the send queue of the QP associated
799 * with the registered client
801 int ib_post_send_mad(struct ib_mad_agent *mad_agent,
802 struct ib_send_wr *send_wr,
803 struct ib_send_wr **bad_send_wr)
806 struct ib_mad_agent_private *mad_agent_priv;
808 /* Validate supplied parameters */
812 if (!mad_agent || !send_wr)
815 if (!mad_agent->send_handler)
818 mad_agent_priv = container_of(mad_agent,
819 struct ib_mad_agent_private,
822 /* Walk list of send WRs and post each on send list */
825 struct ib_send_wr *next_send_wr;
826 struct ib_mad_send_wr_private *mad_send_wr;
829 /* Validate more parameters */
830 if (send_wr->num_sge > IB_MAD_SEND_REQ_MAX_SG)
833 if (send_wr->wr.ud.timeout_ms && !mad_agent->recv_handler)
836 if (!send_wr->wr.ud.mad_hdr) {
837 printk(KERN_ERR PFX "MAD header must be supplied "
838 "in WR %p\n", send_wr);
843 * Save pointer to next work request to post in case the
844 * current one completes, and the user modifies the work
845 * request associated with the completion
847 next_send_wr = (struct ib_send_wr *)send_wr->next;
849 smp = (struct ib_smp *)send_wr->wr.ud.mad_hdr;
850 if (smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
851 ret = handle_outgoing_dr_smp(mad_agent_priv, smp,
853 if (ret < 0) /* error */
855 else if (ret == 1) /* locally consumed */
859 /* Allocate MAD send WR tracking structure */
860 mad_send_wr = kmalloc(sizeof *mad_send_wr,
861 (in_atomic() || irqs_disabled()) ?
862 GFP_ATOMIC : GFP_KERNEL);
864 printk(KERN_ERR PFX "No memory for "
865 "ib_mad_send_wr_private\n");
870 mad_send_wr->send_wr = *send_wr;
871 mad_send_wr->send_wr.sg_list = mad_send_wr->sg_list;
872 memcpy(mad_send_wr->sg_list, send_wr->sg_list,
873 sizeof *send_wr->sg_list * send_wr->num_sge);
874 mad_send_wr->send_wr.next = NULL;
875 mad_send_wr->tid = send_wr->wr.ud.mad_hdr->tid;
876 mad_send_wr->agent = mad_agent;
877 /* Timeout will be updated after send completes */
878 mad_send_wr->timeout = msecs_to_jiffies(send_wr->wr.
880 mad_send_wr->retry = 0;
881 /* One reference for each work request to QP + response */
882 mad_send_wr->refcount = 1 + (mad_send_wr->timeout > 0);
883 mad_send_wr->status = IB_WC_SUCCESS;
885 /* Reference MAD agent until send completes */
886 atomic_inc(&mad_agent_priv->refcount);
887 spin_lock_irqsave(&mad_agent_priv->lock, flags);
888 list_add_tail(&mad_send_wr->agent_list,
889 &mad_agent_priv->send_list);
890 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
892 ret = ib_send_mad(mad_agent_priv, mad_send_wr);
894 /* Fail send request */
895 spin_lock_irqsave(&mad_agent_priv->lock, flags);
896 list_del(&mad_send_wr->agent_list);
897 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
898 atomic_dec(&mad_agent_priv->refcount);
902 send_wr = next_send_wr;
907 *bad_send_wr = send_wr;
911 EXPORT_SYMBOL(ib_post_send_mad);
914 * ib_free_recv_mad - Returns data buffers used to receive
915 * a MAD to the access layer
917 void ib_free_recv_mad(struct ib_mad_recv_wc *mad_recv_wc)
919 struct ib_mad_recv_buf *entry;
920 struct ib_mad_private_header *mad_priv_hdr;
921 struct ib_mad_private *priv;
923 mad_priv_hdr = container_of(mad_recv_wc,
924 struct ib_mad_private_header,
926 priv = container_of(mad_priv_hdr, struct ib_mad_private, header);
929 * Walk receive buffer list associated with this WC
930 * No need to remove them from list of receive buffers
932 list_for_each_entry(entry, &mad_recv_wc->recv_buf.list, list) {
933 /* Free previous receive buffer */
934 kmem_cache_free(ib_mad_cache, priv);
935 mad_priv_hdr = container_of(mad_recv_wc,
936 struct ib_mad_private_header,
938 priv = container_of(mad_priv_hdr, struct ib_mad_private,
942 /* Free last buffer */
943 kmem_cache_free(ib_mad_cache, priv);
945 EXPORT_SYMBOL(ib_free_recv_mad);
947 void ib_coalesce_recv_mad(struct ib_mad_recv_wc *mad_recv_wc,
950 printk(KERN_ERR PFX "ib_coalesce_recv_mad() not implemented yet\n");
952 EXPORT_SYMBOL(ib_coalesce_recv_mad);
954 struct ib_mad_agent *ib_redirect_mad_qp(struct ib_qp *qp,
956 ib_mad_send_handler send_handler,
957 ib_mad_recv_handler recv_handler,
960 return ERR_PTR(-EINVAL); /* XXX: for now */
962 EXPORT_SYMBOL(ib_redirect_mad_qp);
964 int ib_process_mad_wc(struct ib_mad_agent *mad_agent,
967 printk(KERN_ERR PFX "ib_process_mad_wc() not implemented yet\n");
970 EXPORT_SYMBOL(ib_process_mad_wc);
972 static int method_in_use(struct ib_mad_mgmt_method_table **method,
973 struct ib_mad_reg_req *mad_reg_req)
977 for (i = find_first_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS);
978 i < IB_MGMT_MAX_METHODS;
979 i = find_next_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS,
981 if ((*method)->agent[i]) {
982 printk(KERN_ERR PFX "Method %d already in use\n", i);
989 static int allocate_method_table(struct ib_mad_mgmt_method_table **method)
991 /* Allocate management method table */
992 *method = kmalloc(sizeof **method, GFP_ATOMIC);
994 printk(KERN_ERR PFX "No memory for "
995 "ib_mad_mgmt_method_table\n");
998 /* Clear management method table */
999 memset(*method, 0, sizeof **method);
1005 * Check to see if there are any methods still in use
1007 static int check_method_table(struct ib_mad_mgmt_method_table *method)
1011 for (i = 0; i < IB_MGMT_MAX_METHODS; i++)
1012 if (method->agent[i])
1018 * Check to see if there are any method tables for this class still in use
1020 static int check_class_table(struct ib_mad_mgmt_class_table *class)
1024 for (i = 0; i < MAX_MGMT_CLASS; i++)
1025 if (class->method_table[i])
1030 static int check_vendor_class(struct ib_mad_mgmt_vendor_class *vendor_class)
1034 for (i = 0; i < MAX_MGMT_OUI; i++)
1035 if (vendor_class->method_table[i])
1040 static int find_vendor_oui(struct ib_mad_mgmt_vendor_class *vendor_class,
1045 for (i = 0; i < MAX_MGMT_OUI; i++)
1046 /* Is there matching OUI for this vendor class ? */
1047 if (!memcmp(vendor_class->oui[i], oui, 3))
1053 static int check_vendor_table(struct ib_mad_mgmt_vendor_class_table *vendor)
1057 for (i = 0; i < MAX_MGMT_VENDOR_RANGE2; i++)
1058 if (vendor->vendor_class[i])
1064 static void remove_methods_mad_agent(struct ib_mad_mgmt_method_table *method,
1065 struct ib_mad_agent_private *agent)
1069 /* Remove any methods for this mad agent */
1070 for (i = 0; i < IB_MGMT_MAX_METHODS; i++) {
1071 if (method->agent[i] == agent) {
1072 method->agent[i] = NULL;
1077 static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1078 struct ib_mad_agent_private *agent_priv,
1081 struct ib_mad_port_private *port_priv;
1082 struct ib_mad_mgmt_class_table **class;
1083 struct ib_mad_mgmt_method_table **method;
1086 port_priv = agent_priv->qp_info->port_priv;
1087 class = &port_priv->version[mad_reg_req->mgmt_class_version].class;
1089 /* Allocate management class table for "new" class version */
1090 *class = kmalloc(sizeof **class, GFP_ATOMIC);
1092 printk(KERN_ERR PFX "No memory for "
1093 "ib_mad_mgmt_class_table\n");
1097 /* Clear management class table */
1098 memset(*class, 0, sizeof(**class));
1099 /* Allocate method table for this management class */
1100 method = &(*class)->method_table[mgmt_class];
1101 if ((ret = allocate_method_table(method)))
1104 method = &(*class)->method_table[mgmt_class];
1106 /* Allocate method table for this management class */
1107 if ((ret = allocate_method_table(method)))
1112 /* Now, make sure methods are not already in use */
1113 if (method_in_use(method, mad_reg_req))
1116 /* Finally, add in methods being registered */
1117 for (i = find_first_bit(mad_reg_req->method_mask,
1118 IB_MGMT_MAX_METHODS);
1119 i < IB_MGMT_MAX_METHODS;
1120 i = find_next_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS,
1122 (*method)->agent[i] = agent_priv;
1127 /* Remove any methods for this mad agent */
1128 remove_methods_mad_agent(*method, agent_priv);
1129 /* Now, check to see if there are any methods in use */
1130 if (!check_method_table(*method)) {
1131 /* If not, release management method table */
1144 static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1145 struct ib_mad_agent_private *agent_priv)
1147 struct ib_mad_port_private *port_priv;
1148 struct ib_mad_mgmt_vendor_class_table **vendor_table;
1149 struct ib_mad_mgmt_vendor_class_table *vendor = NULL;
1150 struct ib_mad_mgmt_vendor_class *vendor_class = NULL;
1151 struct ib_mad_mgmt_method_table **method;
1152 int i, ret = -ENOMEM;
1155 /* "New" vendor (with OUI) class */
1156 vclass = vendor_class_index(mad_reg_req->mgmt_class);
1157 port_priv = agent_priv->qp_info->port_priv;
1158 vendor_table = &port_priv->version[
1159 mad_reg_req->mgmt_class_version].vendor;
1160 if (!*vendor_table) {
1161 /* Allocate mgmt vendor class table for "new" class version */
1162 vendor = kmalloc(sizeof *vendor, GFP_ATOMIC);
1164 printk(KERN_ERR PFX "No memory for "
1165 "ib_mad_mgmt_vendor_class_table\n");
1168 /* Clear management vendor class table */
1169 memset(vendor, 0, sizeof(*vendor));
1170 *vendor_table = vendor;
1172 if (!(*vendor_table)->vendor_class[vclass]) {
1173 /* Allocate table for this management vendor class */
1174 vendor_class = kmalloc(sizeof *vendor_class, GFP_ATOMIC);
1175 if (!vendor_class) {
1176 printk(KERN_ERR PFX "No memory for "
1177 "ib_mad_mgmt_vendor_class\n");
1180 memset(vendor_class, 0, sizeof(*vendor_class));
1181 (*vendor_table)->vendor_class[vclass] = vendor_class;
1183 for (i = 0; i < MAX_MGMT_OUI; i++) {
1184 /* Is there matching OUI for this vendor class ? */
1185 if (!memcmp((*vendor_table)->vendor_class[vclass]->oui[i],
1186 mad_reg_req->oui, 3)) {
1187 method = &(*vendor_table)->vendor_class[
1188 vclass]->method_table[i];
1193 for (i = 0; i < MAX_MGMT_OUI; i++) {
1194 /* OUI slot available ? */
1195 if (!is_vendor_oui((*vendor_table)->vendor_class[
1197 method = &(*vendor_table)->vendor_class[
1198 vclass]->method_table[i];
1200 /* Allocate method table for this OUI */
1201 if ((ret = allocate_method_table(method)))
1203 memcpy((*vendor_table)->vendor_class[vclass]->oui[i],
1204 mad_reg_req->oui, 3);
1208 printk(KERN_ERR PFX "All OUI slots in use\n");
1212 /* Now, make sure methods are not already in use */
1213 if (method_in_use(method, mad_reg_req))
1216 /* Finally, add in methods being registered */
1217 for (i = find_first_bit(mad_reg_req->method_mask,
1218 IB_MGMT_MAX_METHODS);
1219 i < IB_MGMT_MAX_METHODS;
1220 i = find_next_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS,
1222 (*method)->agent[i] = agent_priv;
1227 /* Remove any methods for this mad agent */
1228 remove_methods_mad_agent(*method, agent_priv);
1229 /* Now, check to see if there are any methods in use */
1230 if (!check_method_table(*method)) {
1231 /* If not, release management method table */
1238 (*vendor_table)->vendor_class[vclass] = NULL;
1239 kfree(vendor_class);
1243 *vendor_table = NULL;
1250 static void remove_mad_reg_req(struct ib_mad_agent_private *agent_priv)
1252 struct ib_mad_port_private *port_priv;
1253 struct ib_mad_mgmt_class_table *class;
1254 struct ib_mad_mgmt_method_table *method;
1255 struct ib_mad_mgmt_vendor_class_table *vendor;
1256 struct ib_mad_mgmt_vendor_class *vendor_class;
1261 * Was MAD registration request supplied
1262 * with original registration ?
1264 if (!agent_priv->reg_req) {
1268 port_priv = agent_priv->qp_info->port_priv;
1269 class = port_priv->version[
1270 agent_priv->reg_req->mgmt_class_version].class;
1274 mgmt_class = convert_mgmt_class(agent_priv->reg_req->mgmt_class);
1275 method = class->method_table[mgmt_class];
1277 /* Remove any methods for this mad agent */
1278 remove_methods_mad_agent(method, agent_priv);
1279 /* Now, check to see if there are any methods still in use */
1280 if (!check_method_table(method)) {
1281 /* If not, release management method table */
1283 class->method_table[mgmt_class] = NULL;
1284 /* Any management classes left ? */
1285 if (!check_class_table(class)) {
1286 /* If not, release management class table */
1289 agent_priv->reg_req->
1290 mgmt_class_version].class = NULL;
1296 vendor = port_priv->version[
1297 agent_priv->reg_req->mgmt_class_version].vendor;
1301 mgmt_class = vendor_class_index(agent_priv->reg_req->mgmt_class);
1302 vendor_class = vendor->vendor_class[mgmt_class];
1304 index = find_vendor_oui(vendor_class, agent_priv->reg_req->oui);
1307 method = vendor_class->method_table[index];
1309 /* Remove any methods for this mad agent */
1310 remove_methods_mad_agent(method, agent_priv);
1312 * Now, check to see if there are
1313 * any methods still in use
1315 if (!check_method_table(method)) {
1316 /* If not, release management method table */
1318 vendor_class->method_table[index] = NULL;
1319 memset(vendor_class->oui[index], 0, 3);
1320 /* Any OUIs left ? */
1321 if (!check_vendor_class(vendor_class)) {
1322 /* If not, release vendor class table */
1323 kfree(vendor_class);
1324 vendor->vendor_class[mgmt_class] = NULL;
1325 /* Any other vendor classes left ? */
1326 if (!check_vendor_table(vendor)) {
1329 agent_priv->reg_req->
1330 mgmt_class_version].
1342 static int response_mad(struct ib_mad *mad)
1344 /* Trap represses are responses although response bit is reset */
1345 return ((mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS) ||
1346 (mad->mad_hdr.method & IB_MGMT_METHOD_RESP));
1349 static int solicited_mad(struct ib_mad *mad)
1351 /* CM MADs are never solicited */
1352 if (mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_CM) {
1356 /* XXX: Determine whether MAD is using RMPP */
1358 /* Not using RMPP */
1359 /* Is this MAD a response to a previous MAD ? */
1360 return response_mad(mad);
1363 static struct ib_mad_agent_private *
1364 find_mad_agent(struct ib_mad_port_private *port_priv,
1368 struct ib_mad_agent_private *mad_agent = NULL;
1369 unsigned long flags;
1371 spin_lock_irqsave(&port_priv->reg_lock, flags);
1374 * Whether MAD was solicited determines type of routing to
1379 struct ib_mad_agent_private *entry;
1382 * Routing is based on high 32 bits of transaction ID
1385 hi_tid = be64_to_cpu(mad->mad_hdr.tid) >> 32;
1386 list_for_each_entry(entry, &port_priv->agent_list,
1388 if (entry->agent.hi_tid == hi_tid) {
1394 struct ib_mad_mgmt_class_table *class;
1395 struct ib_mad_mgmt_method_table *method;
1396 struct ib_mad_mgmt_vendor_class_table *vendor;
1397 struct ib_mad_mgmt_vendor_class *vendor_class;
1398 struct ib_vendor_mad *vendor_mad;
1402 * Routing is based on version, class, and method
1403 * For "newer" vendor MADs, also based on OUI
1405 if (mad->mad_hdr.class_version >= MAX_MGMT_VERSION)
1407 if (!is_vendor_class(mad->mad_hdr.mgmt_class)) {
1408 class = port_priv->version[
1409 mad->mad_hdr.class_version].class;
1412 method = class->method_table[convert_mgmt_class(
1413 mad->mad_hdr.mgmt_class)];
1415 mad_agent = method->agent[mad->mad_hdr.method &
1416 ~IB_MGMT_METHOD_RESP];
1418 vendor = port_priv->version[
1419 mad->mad_hdr.class_version].vendor;
1422 vendor_class = vendor->vendor_class[vendor_class_index(
1423 mad->mad_hdr.mgmt_class)];
1426 /* Find matching OUI */
1427 vendor_mad = (struct ib_vendor_mad *)mad;
1428 index = find_vendor_oui(vendor_class, vendor_mad->oui);
1431 method = vendor_class->method_table[index];
1433 mad_agent = method->agent[mad->mad_hdr.method &
1434 ~IB_MGMT_METHOD_RESP];
1440 if (mad_agent->agent.recv_handler)
1441 atomic_inc(&mad_agent->refcount);
1443 printk(KERN_NOTICE PFX "No receive handler for client "
1445 &mad_agent->agent, port_priv->port_num);
1450 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
1455 static int validate_mad(struct ib_mad *mad, u32 qp_num)
1459 /* Make sure MAD base version is understood */
1460 if (mad->mad_hdr.base_version != IB_MGMT_BASE_VERSION) {
1461 printk(KERN_ERR PFX "MAD received with unsupported base "
1462 "version %d\n", mad->mad_hdr.base_version);
1466 /* Filter SMI packets sent to other than QP0 */
1467 if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
1468 (mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) {
1472 /* Filter GSI packets sent to QP0 */
1482 * Return start of fully reassembled MAD, or NULL, if MAD isn't assembled yet
1484 static struct ib_mad_private *
1485 reassemble_recv(struct ib_mad_agent_private *mad_agent_priv,
1486 struct ib_mad_private *recv)
1488 /* Until we have RMPP, all receives are reassembled!... */
1489 INIT_LIST_HEAD(&recv->header.recv_wc.recv_buf.list);
1493 static struct ib_mad_send_wr_private*
1494 find_send_req(struct ib_mad_agent_private *mad_agent_priv,
1497 struct ib_mad_send_wr_private *mad_send_wr;
1499 list_for_each_entry(mad_send_wr, &mad_agent_priv->wait_list,
1501 if (mad_send_wr->tid == tid)
1506 * It's possible to receive the response before we've
1507 * been notified that the send has completed
1509 list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list,
1511 if (mad_send_wr->tid == tid && mad_send_wr->timeout) {
1512 /* Verify request has not been canceled */
1513 return (mad_send_wr->status == IB_WC_SUCCESS) ?
1520 static void ib_mad_complete_recv(struct ib_mad_agent_private *mad_agent_priv,
1521 struct ib_mad_private *recv,
1524 struct ib_mad_send_wr_private *mad_send_wr;
1525 struct ib_mad_send_wc mad_send_wc;
1526 unsigned long flags;
1528 /* Fully reassemble receive before processing */
1529 recv = reassemble_recv(mad_agent_priv, recv);
1531 if (atomic_dec_and_test(&mad_agent_priv->refcount))
1532 wake_up(&mad_agent_priv->wait);
1536 /* Complete corresponding request */
1538 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1539 mad_send_wr = find_send_req(mad_agent_priv,
1540 recv->mad.mad.mad_hdr.tid);
1542 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1543 ib_free_recv_mad(&recv->header.recv_wc);
1544 if (atomic_dec_and_test(&mad_agent_priv->refcount))
1545 wake_up(&mad_agent_priv->wait);
1548 /* Timeout = 0 means that we won't wait for a response */
1549 mad_send_wr->timeout = 0;
1550 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1552 /* Defined behavior is to complete response before request */
1553 recv->header.recv_wc.wc->wr_id = mad_send_wr->wr_id;
1554 mad_agent_priv->agent.recv_handler(
1555 &mad_agent_priv->agent,
1556 &recv->header.recv_wc);
1557 atomic_dec(&mad_agent_priv->refcount);
1559 mad_send_wc.status = IB_WC_SUCCESS;
1560 mad_send_wc.vendor_err = 0;
1561 mad_send_wc.wr_id = mad_send_wr->wr_id;
1562 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
1564 mad_agent_priv->agent.recv_handler(
1565 &mad_agent_priv->agent,
1566 &recv->header.recv_wc);
1567 if (atomic_dec_and_test(&mad_agent_priv->refcount))
1568 wake_up(&mad_agent_priv->wait);
1572 static void ib_mad_recv_done_handler(struct ib_mad_port_private *port_priv,
1575 struct ib_mad_qp_info *qp_info;
1576 struct ib_mad_private_header *mad_priv_hdr;
1577 struct ib_mad_private *recv, *response;
1578 struct ib_mad_list_head *mad_list;
1579 struct ib_mad_agent_private *mad_agent;
1582 response = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL);
1584 printk(KERN_ERR PFX "ib_mad_recv_done_handler no memory "
1585 "for response buffer\n");
1587 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
1588 qp_info = mad_list->mad_queue->qp_info;
1589 dequeue_mad(mad_list);
1591 mad_priv_hdr = container_of(mad_list, struct ib_mad_private_header,
1593 recv = container_of(mad_priv_hdr, struct ib_mad_private, header);
1594 dma_unmap_single(port_priv->device->dma_device,
1595 pci_unmap_addr(&recv->header, mapping),
1596 sizeof(struct ib_mad_private) -
1597 sizeof(struct ib_mad_private_header),
1600 /* Setup MAD receive work completion from "normal" work completion */
1601 recv->header.recv_wc.wc = wc;
1602 recv->header.recv_wc.mad_len = sizeof(struct ib_mad);
1603 recv->header.recv_wc.recv_buf.mad = &recv->mad.mad;
1604 recv->header.recv_wc.recv_buf.grh = &recv->grh;
1606 if (atomic_read(&qp_info->snoop_count))
1607 snoop_recv(qp_info, &recv->header.recv_wc, IB_MAD_SNOOP_RECVS);
1610 if (!validate_mad(&recv->mad.mad, qp_info->qp->qp_num))
1613 if (recv->mad.mad.mad_hdr.mgmt_class ==
1614 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
1615 if (!smi_handle_dr_smp_recv(&recv->mad.smp,
1616 port_priv->device->node_type,
1617 port_priv->port_num,
1618 port_priv->device->phys_port_cnt))
1620 if (!smi_check_forward_dr_smp(&recv->mad.smp))
1622 if (!smi_handle_dr_smp_send(&recv->mad.smp,
1623 port_priv->device->node_type,
1624 port_priv->port_num))
1626 if (!smi_check_local_dr_smp(&recv->mad.smp,
1628 port_priv->port_num))
1633 /* Give driver "right of first refusal" on incoming MAD */
1634 if (port_priv->device->process_mad) {
1638 printk(KERN_ERR PFX "No memory for response MAD\n");
1640 * Is it better to assume that
1641 * it wouldn't be processed ?
1646 ret = port_priv->device->process_mad(port_priv->device, 0,
1647 port_priv->port_num,
1650 &response->mad.mad);
1651 if (ret & IB_MAD_RESULT_SUCCESS) {
1652 if (ret & IB_MAD_RESULT_CONSUMED)
1654 if (ret & IB_MAD_RESULT_REPLY) {
1656 if (!agent_send(response, &recv->grh, wc,
1658 port_priv->port_num))
1665 /* Determine corresponding MAD agent for incoming receive MAD */
1666 solicited = solicited_mad(&recv->mad.mad);
1667 mad_agent = find_mad_agent(port_priv, &recv->mad.mad, solicited);
1669 ib_mad_complete_recv(mad_agent, recv, solicited);
1671 * recv is freed up in error cases in ib_mad_complete_recv
1672 * or via recv_handler in ib_mad_complete_recv()
1678 /* Post another receive request for this QP */
1680 ib_mad_post_receive_mads(qp_info, response);
1682 kmem_cache_free(ib_mad_cache, recv);
1684 ib_mad_post_receive_mads(qp_info, recv);
1687 static void adjust_timeout(struct ib_mad_agent_private *mad_agent_priv)
1689 struct ib_mad_send_wr_private *mad_send_wr;
1690 unsigned long delay;
1692 if (list_empty(&mad_agent_priv->wait_list)) {
1693 cancel_delayed_work(&mad_agent_priv->timed_work);
1695 mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
1696 struct ib_mad_send_wr_private,
1699 if (time_after(mad_agent_priv->timeout,
1700 mad_send_wr->timeout)) {
1701 mad_agent_priv->timeout = mad_send_wr->timeout;
1702 cancel_delayed_work(&mad_agent_priv->timed_work);
1703 delay = mad_send_wr->timeout - jiffies;
1704 if ((long)delay <= 0)
1706 queue_delayed_work(mad_agent_priv->qp_info->
1708 &mad_agent_priv->timed_work, delay);
1713 static void wait_for_response(struct ib_mad_agent_private *mad_agent_priv,
1714 struct ib_mad_send_wr_private *mad_send_wr )
1716 struct ib_mad_send_wr_private *temp_mad_send_wr;
1717 struct list_head *list_item;
1718 unsigned long delay;
1720 list_del(&mad_send_wr->agent_list);
1722 delay = mad_send_wr->timeout;
1723 mad_send_wr->timeout += jiffies;
1725 list_for_each_prev(list_item, &mad_agent_priv->wait_list) {
1726 temp_mad_send_wr = list_entry(list_item,
1727 struct ib_mad_send_wr_private,
1729 if (time_after(mad_send_wr->timeout,
1730 temp_mad_send_wr->timeout))
1733 list_add(&mad_send_wr->agent_list, list_item);
1735 /* Reschedule a work item if we have a shorter timeout */
1736 if (mad_agent_priv->wait_list.next == &mad_send_wr->agent_list) {
1737 cancel_delayed_work(&mad_agent_priv->timed_work);
1738 queue_delayed_work(mad_agent_priv->qp_info->port_priv->wq,
1739 &mad_agent_priv->timed_work, delay);
1744 * Process a send work completion
1746 static void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr,
1747 struct ib_mad_send_wc *mad_send_wc)
1749 struct ib_mad_agent_private *mad_agent_priv;
1750 unsigned long flags;
1752 mad_agent_priv = container_of(mad_send_wr->agent,
1753 struct ib_mad_agent_private, agent);
1755 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1756 if (mad_send_wc->status != IB_WC_SUCCESS &&
1757 mad_send_wr->status == IB_WC_SUCCESS) {
1758 mad_send_wr->status = mad_send_wc->status;
1759 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
1762 if (--mad_send_wr->refcount > 0) {
1763 if (mad_send_wr->refcount == 1 && mad_send_wr->timeout &&
1764 mad_send_wr->status == IB_WC_SUCCESS) {
1765 wait_for_response(mad_agent_priv, mad_send_wr);
1767 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1771 /* Remove send from MAD agent and notify client of completion */
1772 list_del(&mad_send_wr->agent_list);
1773 adjust_timeout(mad_agent_priv);
1774 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1776 if (mad_send_wr->status != IB_WC_SUCCESS )
1777 mad_send_wc->status = mad_send_wr->status;
1778 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
1781 /* Release reference on agent taken when sending */
1782 if (atomic_dec_and_test(&mad_agent_priv->refcount))
1783 wake_up(&mad_agent_priv->wait);
1788 static void ib_mad_send_done_handler(struct ib_mad_port_private *port_priv,
1791 struct ib_mad_send_wr_private *mad_send_wr, *queued_send_wr;
1792 struct ib_mad_list_head *mad_list;
1793 struct ib_mad_qp_info *qp_info;
1794 struct ib_mad_queue *send_queue;
1795 struct ib_send_wr *bad_send_wr;
1796 unsigned long flags;
1799 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
1800 mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
1802 send_queue = mad_list->mad_queue;
1803 qp_info = send_queue->qp_info;
1806 queued_send_wr = NULL;
1807 spin_lock_irqsave(&send_queue->lock, flags);
1808 list_del(&mad_list->list);
1810 /* Move queued send to the send queue */
1811 if (send_queue->count-- > send_queue->max_active) {
1812 mad_list = container_of(qp_info->overflow_list.next,
1813 struct ib_mad_list_head, list);
1814 queued_send_wr = container_of(mad_list,
1815 struct ib_mad_send_wr_private,
1817 list_del(&mad_list->list);
1818 list_add_tail(&mad_list->list, &send_queue->list);
1820 spin_unlock_irqrestore(&send_queue->lock, flags);
1822 /* Restore client wr_id in WC and complete send */
1823 wc->wr_id = mad_send_wr->wr_id;
1824 if (atomic_read(&qp_info->snoop_count))
1825 snoop_send(qp_info, &mad_send_wr->send_wr,
1826 (struct ib_mad_send_wc *)wc,
1827 IB_MAD_SNOOP_SEND_COMPLETIONS);
1828 ib_mad_complete_send_wr(mad_send_wr, (struct ib_mad_send_wc *)wc);
1830 if (queued_send_wr) {
1831 ret = ib_post_send(qp_info->qp, &queued_send_wr->send_wr,
1834 printk(KERN_ERR PFX "ib_post_send failed: %d\n", ret);
1835 mad_send_wr = queued_send_wr;
1836 wc->status = IB_WC_LOC_QP_OP_ERR;
1842 static void mark_sends_for_retry(struct ib_mad_qp_info *qp_info)
1844 struct ib_mad_send_wr_private *mad_send_wr;
1845 struct ib_mad_list_head *mad_list;
1846 unsigned long flags;
1848 spin_lock_irqsave(&qp_info->send_queue.lock, flags);
1849 list_for_each_entry(mad_list, &qp_info->send_queue.list, list) {
1850 mad_send_wr = container_of(mad_list,
1851 struct ib_mad_send_wr_private,
1853 mad_send_wr->retry = 1;
1855 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
1858 static void mad_error_handler(struct ib_mad_port_private *port_priv,
1861 struct ib_mad_list_head *mad_list;
1862 struct ib_mad_qp_info *qp_info;
1863 struct ib_mad_send_wr_private *mad_send_wr;
1866 /* Determine if failure was a send or receive */
1867 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
1868 qp_info = mad_list->mad_queue->qp_info;
1869 if (mad_list->mad_queue == &qp_info->recv_queue)
1871 * Receive errors indicate that the QP has entered the error
1872 * state - error handling/shutdown code will cleanup
1877 * Send errors will transition the QP to SQE - move
1878 * QP to RTS and repost flushed work requests
1880 mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
1882 if (wc->status == IB_WC_WR_FLUSH_ERR) {
1883 if (mad_send_wr->retry) {
1885 struct ib_send_wr *bad_send_wr;
1887 mad_send_wr->retry = 0;
1888 ret = ib_post_send(qp_info->qp, &mad_send_wr->send_wr,
1891 ib_mad_send_done_handler(port_priv, wc);
1893 ib_mad_send_done_handler(port_priv, wc);
1895 struct ib_qp_attr *attr;
1897 /* Transition QP to RTS and fail offending send */
1898 attr = kmalloc(sizeof *attr, GFP_KERNEL);
1900 attr->qp_state = IB_QPS_RTS;
1901 attr->cur_qp_state = IB_QPS_SQE;
1902 ret = ib_modify_qp(qp_info->qp, attr,
1903 IB_QP_STATE | IB_QP_CUR_STATE);
1906 printk(KERN_ERR PFX "mad_error_handler - "
1907 "ib_modify_qp to RTS : %d\n", ret);
1909 mark_sends_for_retry(qp_info);
1911 ib_mad_send_done_handler(port_priv, wc);
1916 * IB MAD completion callback
1918 static void ib_mad_completion_handler(void *data)
1920 struct ib_mad_port_private *port_priv;
1923 port_priv = (struct ib_mad_port_private *)data;
1924 ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
1926 while (ib_poll_cq(port_priv->cq, 1, &wc) == 1) {
1927 if (wc.status == IB_WC_SUCCESS) {
1928 switch (wc.opcode) {
1930 ib_mad_send_done_handler(port_priv, &wc);
1933 ib_mad_recv_done_handler(port_priv, &wc);
1940 mad_error_handler(port_priv, &wc);
1944 static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv)
1946 unsigned long flags;
1947 struct ib_mad_send_wr_private *mad_send_wr, *temp_mad_send_wr;
1948 struct ib_mad_send_wc mad_send_wc;
1949 struct list_head cancel_list;
1951 INIT_LIST_HEAD(&cancel_list);
1953 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1954 list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
1955 &mad_agent_priv->send_list, agent_list) {
1956 if (mad_send_wr->status == IB_WC_SUCCESS) {
1957 mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
1958 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
1962 /* Empty wait list to prevent receives from finding a request */
1963 list_splice_init(&mad_agent_priv->wait_list, &cancel_list);
1964 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1966 /* Report all cancelled requests */
1967 mad_send_wc.status = IB_WC_WR_FLUSH_ERR;
1968 mad_send_wc.vendor_err = 0;
1970 list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
1971 &cancel_list, agent_list) {
1972 mad_send_wc.wr_id = mad_send_wr->wr_id;
1973 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
1976 list_del(&mad_send_wr->agent_list);
1978 atomic_dec(&mad_agent_priv->refcount);
1982 static struct ib_mad_send_wr_private*
1983 find_send_by_wr_id(struct ib_mad_agent_private *mad_agent_priv,
1986 struct ib_mad_send_wr_private *mad_send_wr;
1988 list_for_each_entry(mad_send_wr, &mad_agent_priv->wait_list,
1990 if (mad_send_wr->wr_id == wr_id)
1994 list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list,
1996 if (mad_send_wr->wr_id == wr_id)
2002 void ib_cancel_mad(struct ib_mad_agent *mad_agent,
2005 struct ib_mad_agent_private *mad_agent_priv;
2006 struct ib_mad_send_wr_private *mad_send_wr;
2007 struct ib_mad_send_wc mad_send_wc;
2008 unsigned long flags;
2010 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
2012 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2013 mad_send_wr = find_send_by_wr_id(mad_agent_priv, wr_id);
2015 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2019 if (mad_send_wr->status == IB_WC_SUCCESS)
2020 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2022 if (mad_send_wr->refcount != 0) {
2023 mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
2024 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2028 list_del(&mad_send_wr->agent_list);
2029 adjust_timeout(mad_agent_priv);
2030 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2032 mad_send_wc.status = IB_WC_WR_FLUSH_ERR;
2033 mad_send_wc.vendor_err = 0;
2034 mad_send_wc.wr_id = mad_send_wr->wr_id;
2035 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2039 if (atomic_dec_and_test(&mad_agent_priv->refcount))
2040 wake_up(&mad_agent_priv->wait);
2045 EXPORT_SYMBOL(ib_cancel_mad);
2047 static void local_completions(void *data)
2049 struct ib_mad_agent_private *mad_agent_priv;
2050 struct ib_mad_local_private *local;
2051 struct ib_mad_agent_private *recv_mad_agent;
2052 unsigned long flags;
2054 struct ib_mad_send_wc mad_send_wc;
2056 mad_agent_priv = (struct ib_mad_agent_private *)data;
2058 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2059 while (!list_empty(&mad_agent_priv->local_list)) {
2060 local = list_entry(mad_agent_priv->local_list.next,
2061 struct ib_mad_local_private,
2063 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2064 if (local->mad_priv) {
2065 recv_mad_agent = local->recv_mad_agent;
2066 if (!recv_mad_agent) {
2067 printk(KERN_ERR PFX "No receive MAD agent for local completion\n");
2068 kmem_cache_free(ib_mad_cache, local->mad_priv);
2069 goto local_send_completion;
2073 * Defined behavior is to complete response
2076 build_smp_wc(local->wr_id, IB_LID_PERMISSIVE,
2078 recv_mad_agent->agent.port_num, &wc);
2080 local->mad_priv->header.recv_wc.wc = &wc;
2081 local->mad_priv->header.recv_wc.mad_len =
2082 sizeof(struct ib_mad);
2083 INIT_LIST_HEAD(&local->mad_priv->header.recv_wc.recv_buf.list);
2084 local->mad_priv->header.recv_wc.recv_buf.grh = NULL;
2085 local->mad_priv->header.recv_wc.recv_buf.mad =
2086 &local->mad_priv->mad.mad;
2087 if (atomic_read(&recv_mad_agent->qp_info->snoop_count))
2088 snoop_recv(recv_mad_agent->qp_info,
2089 &local->mad_priv->header.recv_wc,
2090 IB_MAD_SNOOP_RECVS);
2091 recv_mad_agent->agent.recv_handler(
2092 &recv_mad_agent->agent,
2093 &local->mad_priv->header.recv_wc);
2094 spin_lock_irqsave(&recv_mad_agent->lock, flags);
2095 atomic_dec(&recv_mad_agent->refcount);
2096 spin_unlock_irqrestore(&recv_mad_agent->lock, flags);
2099 local_send_completion:
2101 mad_send_wc.status = IB_WC_SUCCESS;
2102 mad_send_wc.vendor_err = 0;
2103 mad_send_wc.wr_id = local->wr_id;
2104 if (atomic_read(&mad_agent_priv->qp_info->snoop_count))
2105 snoop_send(mad_agent_priv->qp_info, &local->send_wr,
2107 IB_MAD_SNOOP_SEND_COMPLETIONS);
2108 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2111 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2112 list_del(&local->completion_list);
2113 atomic_dec(&mad_agent_priv->refcount);
2116 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2119 static void timeout_sends(void *data)
2121 struct ib_mad_agent_private *mad_agent_priv;
2122 struct ib_mad_send_wr_private *mad_send_wr;
2123 struct ib_mad_send_wc mad_send_wc;
2124 unsigned long flags, delay;
2126 mad_agent_priv = (struct ib_mad_agent_private *)data;
2128 mad_send_wc.status = IB_WC_RESP_TIMEOUT_ERR;
2129 mad_send_wc.vendor_err = 0;
2131 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2132 while (!list_empty(&mad_agent_priv->wait_list)) {
2133 mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
2134 struct ib_mad_send_wr_private,
2137 if (time_after(mad_send_wr->timeout, jiffies)) {
2138 delay = mad_send_wr->timeout - jiffies;
2139 if ((long)delay <= 0)
2141 queue_delayed_work(mad_agent_priv->qp_info->
2143 &mad_agent_priv->timed_work, delay);
2147 list_del(&mad_send_wr->agent_list);
2148 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2150 mad_send_wc.wr_id = mad_send_wr->wr_id;
2151 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2155 atomic_dec(&mad_agent_priv->refcount);
2156 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2158 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2161 static void ib_mad_thread_completion_handler(struct ib_cq *cq)
2163 struct ib_mad_port_private *port_priv = cq->cq_context;
2165 queue_work(port_priv->wq, &port_priv->work);
2169 * Allocate receive MADs and post receive WRs for them
2171 static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
2172 struct ib_mad_private *mad)
2174 unsigned long flags;
2176 struct ib_mad_private *mad_priv;
2177 struct ib_sge sg_list;
2178 struct ib_recv_wr recv_wr, *bad_recv_wr;
2179 struct ib_mad_queue *recv_queue = &qp_info->recv_queue;
2181 /* Initialize common scatter list fields */
2182 sg_list.length = sizeof *mad_priv - sizeof mad_priv->header;
2183 sg_list.lkey = (*qp_info->port_priv->mr).lkey;
2185 /* Initialize common receive WR fields */
2186 recv_wr.next = NULL;
2187 recv_wr.sg_list = &sg_list;
2188 recv_wr.num_sge = 1;
2189 recv_wr.recv_flags = IB_RECV_SIGNALED;
2192 /* Allocate and map receive buffer */
2197 mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL);
2199 printk(KERN_ERR PFX "No memory for receive buffer\n");
2204 sg_list.addr = dma_map_single(qp_info->port_priv->
2208 sizeof mad_priv->header,
2210 pci_unmap_addr_set(&mad_priv->header, mapping, sg_list.addr);
2211 recv_wr.wr_id = (unsigned long)&mad_priv->header.mad_list;
2212 mad_priv->header.mad_list.mad_queue = recv_queue;
2214 /* Post receive WR */
2215 spin_lock_irqsave(&recv_queue->lock, flags);
2216 post = (++recv_queue->count < recv_queue->max_active);
2217 list_add_tail(&mad_priv->header.mad_list.list, &recv_queue->list);
2218 spin_unlock_irqrestore(&recv_queue->lock, flags);
2219 ret = ib_post_recv(qp_info->qp, &recv_wr, &bad_recv_wr);
2221 spin_lock_irqsave(&recv_queue->lock, flags);
2222 list_del(&mad_priv->header.mad_list.list);
2223 recv_queue->count--;
2224 spin_unlock_irqrestore(&recv_queue->lock, flags);
2225 dma_unmap_single(qp_info->port_priv->device->dma_device,
2226 pci_unmap_addr(&mad_priv->header,
2229 sizeof mad_priv->header,
2231 kmem_cache_free(ib_mad_cache, mad_priv);
2232 printk(KERN_ERR PFX "ib_post_recv failed: %d\n", ret);
2241 * Return all the posted receive MADs
2243 static void cleanup_recv_queue(struct ib_mad_qp_info *qp_info)
2245 struct ib_mad_private_header *mad_priv_hdr;
2246 struct ib_mad_private *recv;
2247 struct ib_mad_list_head *mad_list;
2249 while (!list_empty(&qp_info->recv_queue.list)) {
2251 mad_list = list_entry(qp_info->recv_queue.list.next,
2252 struct ib_mad_list_head, list);
2253 mad_priv_hdr = container_of(mad_list,
2254 struct ib_mad_private_header,
2256 recv = container_of(mad_priv_hdr, struct ib_mad_private,
2259 /* Remove from posted receive MAD list */
2260 list_del(&mad_list->list);
2262 /* Undo PCI mapping */
2263 dma_unmap_single(qp_info->port_priv->device->dma_device,
2264 pci_unmap_addr(&recv->header, mapping),
2265 sizeof(struct ib_mad_private) -
2266 sizeof(struct ib_mad_private_header),
2268 kmem_cache_free(ib_mad_cache, recv);
2271 qp_info->recv_queue.count = 0;
2277 static int ib_mad_port_start(struct ib_mad_port_private *port_priv)
2280 struct ib_qp_attr *attr;
2283 attr = kmalloc(sizeof *attr, GFP_KERNEL);
2285 printk(KERN_ERR PFX "Couldn't kmalloc ib_qp_attr\n");
2289 for (i = 0; i < IB_MAD_QPS_CORE; i++) {
2290 qp = port_priv->qp_info[i].qp;
2292 * PKey index for QP1 is irrelevant but
2293 * one is needed for the Reset to Init transition
2295 attr->qp_state = IB_QPS_INIT;
2296 attr->pkey_index = 0;
2297 attr->qkey = (qp->qp_num == 0) ? 0 : IB_QP1_QKEY;
2298 ret = ib_modify_qp(qp, attr, IB_QP_STATE |
2299 IB_QP_PKEY_INDEX | IB_QP_QKEY);
2301 printk(KERN_ERR PFX "Couldn't change QP%d state to "
2302 "INIT: %d\n", i, ret);
2306 attr->qp_state = IB_QPS_RTR;
2307 ret = ib_modify_qp(qp, attr, IB_QP_STATE);
2309 printk(KERN_ERR PFX "Couldn't change QP%d state to "
2310 "RTR: %d\n", i, ret);
2314 attr->qp_state = IB_QPS_RTS;
2315 attr->sq_psn = IB_MAD_SEND_Q_PSN;
2316 ret = ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_SQ_PSN);
2318 printk(KERN_ERR PFX "Couldn't change QP%d state to "
2319 "RTS: %d\n", i, ret);
2324 ret = ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
2326 printk(KERN_ERR PFX "Failed to request completion "
2327 "notification: %d\n", ret);
2331 for (i = 0; i < IB_MAD_QPS_CORE; i++) {
2332 ret = ib_mad_post_receive_mads(&port_priv->qp_info[i], NULL);
2334 printk(KERN_ERR PFX "Couldn't post receive WRs\n");
2343 static void qp_event_handler(struct ib_event *event, void *qp_context)
2345 struct ib_mad_qp_info *qp_info = qp_context;
2347 /* It's worse than that! He's dead, Jim! */
2348 printk(KERN_ERR PFX "Fatal error (%d) on MAD QP (%d)\n",
2349 event->event, qp_info->qp->qp_num);
2352 static void init_mad_queue(struct ib_mad_qp_info *qp_info,
2353 struct ib_mad_queue *mad_queue)
2355 mad_queue->qp_info = qp_info;
2356 mad_queue->count = 0;
2357 spin_lock_init(&mad_queue->lock);
2358 INIT_LIST_HEAD(&mad_queue->list);
2361 static void init_mad_qp(struct ib_mad_port_private *port_priv,
2362 struct ib_mad_qp_info *qp_info)
2364 qp_info->port_priv = port_priv;
2365 init_mad_queue(qp_info, &qp_info->send_queue);
2366 init_mad_queue(qp_info, &qp_info->recv_queue);
2367 INIT_LIST_HEAD(&qp_info->overflow_list);
2368 spin_lock_init(&qp_info->snoop_lock);
2369 qp_info->snoop_table = NULL;
2370 qp_info->snoop_table_size = 0;
2371 atomic_set(&qp_info->snoop_count, 0);
2374 static int create_mad_qp(struct ib_mad_qp_info *qp_info,
2375 enum ib_qp_type qp_type)
2377 struct ib_qp_init_attr qp_init_attr;
2380 memset(&qp_init_attr, 0, sizeof qp_init_attr);
2381 qp_init_attr.send_cq = qp_info->port_priv->cq;
2382 qp_init_attr.recv_cq = qp_info->port_priv->cq;
2383 qp_init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
2384 qp_init_attr.rq_sig_type = IB_SIGNAL_ALL_WR;
2385 qp_init_attr.cap.max_send_wr = IB_MAD_QP_SEND_SIZE;
2386 qp_init_attr.cap.max_recv_wr = IB_MAD_QP_RECV_SIZE;
2387 qp_init_attr.cap.max_send_sge = IB_MAD_SEND_REQ_MAX_SG;
2388 qp_init_attr.cap.max_recv_sge = IB_MAD_RECV_REQ_MAX_SG;
2389 qp_init_attr.qp_type = qp_type;
2390 qp_init_attr.port_num = qp_info->port_priv->port_num;
2391 qp_init_attr.qp_context = qp_info;
2392 qp_init_attr.event_handler = qp_event_handler;
2393 qp_info->qp = ib_create_qp(qp_info->port_priv->pd, &qp_init_attr);
2394 if (IS_ERR(qp_info->qp)) {
2395 printk(KERN_ERR PFX "Couldn't create ib_mad QP%d\n",
2396 get_spl_qp_index(qp_type));
2397 ret = PTR_ERR(qp_info->qp);
2400 /* Use minimum queue sizes unless the CQ is resized */
2401 qp_info->send_queue.max_active = IB_MAD_QP_SEND_SIZE;
2402 qp_info->recv_queue.max_active = IB_MAD_QP_RECV_SIZE;
2409 static void destroy_mad_qp(struct ib_mad_qp_info *qp_info)
2411 ib_destroy_qp(qp_info->qp);
2412 if (qp_info->snoop_table)
2413 kfree(qp_info->snoop_table);
2418 * Create the QP, PD, MR, and CQ if needed
2420 static int ib_mad_port_open(struct ib_device *device,
2424 struct ib_mad_port_private *port_priv;
2425 unsigned long flags;
2426 char name[sizeof "ib_mad123"];
2428 /* First, check if port already open at MAD layer */
2429 port_priv = ib_get_mad_port(device, port_num);
2431 printk(KERN_DEBUG PFX "%s port %d already open\n",
2432 device->name, port_num);
2436 /* Create new device info */
2437 port_priv = kmalloc(sizeof *port_priv, GFP_KERNEL);
2439 printk(KERN_ERR PFX "No memory for ib_mad_port_private\n");
2442 memset(port_priv, 0, sizeof *port_priv);
2443 port_priv->device = device;
2444 port_priv->port_num = port_num;
2445 spin_lock_init(&port_priv->reg_lock);
2446 INIT_LIST_HEAD(&port_priv->agent_list);
2447 init_mad_qp(port_priv, &port_priv->qp_info[0]);
2448 init_mad_qp(port_priv, &port_priv->qp_info[1]);
2450 cq_size = (IB_MAD_QP_SEND_SIZE + IB_MAD_QP_RECV_SIZE) * 2;
2451 port_priv->cq = ib_create_cq(port_priv->device,
2453 ib_mad_thread_completion_handler,
2454 NULL, port_priv, cq_size);
2455 if (IS_ERR(port_priv->cq)) {
2456 printk(KERN_ERR PFX "Couldn't create ib_mad CQ\n");
2457 ret = PTR_ERR(port_priv->cq);
2461 port_priv->pd = ib_alloc_pd(device);
2462 if (IS_ERR(port_priv->pd)) {
2463 printk(KERN_ERR PFX "Couldn't create ib_mad PD\n");
2464 ret = PTR_ERR(port_priv->pd);
2468 port_priv->mr = ib_get_dma_mr(port_priv->pd, IB_ACCESS_LOCAL_WRITE);
2469 if (IS_ERR(port_priv->mr)) {
2470 printk(KERN_ERR PFX "Couldn't get ib_mad DMA MR\n");
2471 ret = PTR_ERR(port_priv->mr);
2475 ret = create_mad_qp(&port_priv->qp_info[0], IB_QPT_SMI);
2478 ret = create_mad_qp(&port_priv->qp_info[1], IB_QPT_GSI);
2482 snprintf(name, sizeof name, "ib_mad%d", port_num);
2483 port_priv->wq = create_singlethread_workqueue(name);
2484 if (!port_priv->wq) {
2488 INIT_WORK(&port_priv->work, ib_mad_completion_handler, port_priv);
2490 ret = ib_mad_port_start(port_priv);
2492 printk(KERN_ERR PFX "Couldn't start port\n");
2496 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2497 list_add_tail(&port_priv->port_list, &ib_mad_port_list);
2498 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2502 destroy_workqueue(port_priv->wq);
2504 destroy_mad_qp(&port_priv->qp_info[1]);
2506 destroy_mad_qp(&port_priv->qp_info[0]);
2508 ib_dereg_mr(port_priv->mr);
2510 ib_dealloc_pd(port_priv->pd);
2512 ib_destroy_cq(port_priv->cq);
2513 cleanup_recv_queue(&port_priv->qp_info[1]);
2514 cleanup_recv_queue(&port_priv->qp_info[0]);
2523 * If there are no classes using the port, free the port
2524 * resources (CQ, MR, PD, QP) and remove the port's info structure
2526 static int ib_mad_port_close(struct ib_device *device, int port_num)
2528 struct ib_mad_port_private *port_priv;
2529 unsigned long flags;
2531 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2532 port_priv = __ib_get_mad_port(device, port_num);
2533 if (port_priv == NULL) {
2534 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2535 printk(KERN_ERR PFX "Port %d not found\n", port_num);
2538 list_del(&port_priv->port_list);
2539 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2541 /* Stop processing completions. */
2542 flush_workqueue(port_priv->wq);
2543 destroy_workqueue(port_priv->wq);
2544 destroy_mad_qp(&port_priv->qp_info[1]);
2545 destroy_mad_qp(&port_priv->qp_info[0]);
2546 ib_dereg_mr(port_priv->mr);
2547 ib_dealloc_pd(port_priv->pd);
2548 ib_destroy_cq(port_priv->cq);
2549 cleanup_recv_queue(&port_priv->qp_info[1]);
2550 cleanup_recv_queue(&port_priv->qp_info[0]);
2551 /* XXX: Handle deallocation of MAD registration tables */
2558 static void ib_mad_init_device(struct ib_device *device)
2560 int ret, num_ports, cur_port, i, ret2;
2562 if (device->node_type == IB_NODE_SWITCH) {
2566 num_ports = device->phys_port_cnt;
2569 for (i = 0; i < num_ports; i++, cur_port++) {
2570 ret = ib_mad_port_open(device, cur_port);
2572 printk(KERN_ERR PFX "Couldn't open %s port %d\n",
2573 device->name, cur_port);
2574 goto error_device_open;
2576 ret = ib_agent_port_open(device, cur_port);
2578 printk(KERN_ERR PFX "Couldn't open %s port %d "
2580 device->name, cur_port);
2581 goto error_device_open;
2585 goto error_device_query;
2590 ret2 = ib_agent_port_close(device, cur_port);
2592 printk(KERN_ERR PFX "Couldn't close %s port %d "
2594 device->name, cur_port);
2596 ret2 = ib_mad_port_close(device, cur_port);
2598 printk(KERN_ERR PFX "Couldn't close %s port %d\n",
2599 device->name, cur_port);
2608 static void ib_mad_remove_device(struct ib_device *device)
2610 int ret = 0, i, num_ports, cur_port, ret2;
2612 if (device->node_type == IB_NODE_SWITCH) {
2616 num_ports = device->phys_port_cnt;
2619 for (i = 0; i < num_ports; i++, cur_port++) {
2620 ret2 = ib_agent_port_close(device, cur_port);
2622 printk(KERN_ERR PFX "Couldn't close %s port %d "
2624 device->name, cur_port);
2628 ret2 = ib_mad_port_close(device, cur_port);
2630 printk(KERN_ERR PFX "Couldn't close %s port %d\n",
2631 device->name, cur_port);
2638 static struct ib_client mad_client = {
2640 .add = ib_mad_init_device,
2641 .remove = ib_mad_remove_device
2644 static int __init ib_mad_init_module(void)
2648 spin_lock_init(&ib_mad_port_list_lock);
2649 spin_lock_init(&ib_agent_port_list_lock);
2651 ib_mad_cache = kmem_cache_create("ib_mad",
2652 sizeof(struct ib_mad_private),
2657 if (!ib_mad_cache) {
2658 printk(KERN_ERR PFX "Couldn't create ib_mad cache\n");
2663 INIT_LIST_HEAD(&ib_mad_port_list);
2665 if (ib_register_client(&mad_client)) {
2666 printk(KERN_ERR PFX "Couldn't register ib_mad client\n");
2674 kmem_cache_destroy(ib_mad_cache);
2679 static void __exit ib_mad_cleanup_module(void)
2681 ib_unregister_client(&mad_client);
2683 if (kmem_cache_destroy(ib_mad_cache)) {
2684 printk(KERN_DEBUG PFX "Failed to destroy ib_mad cache\n");
2688 module_init(ib_mad_init_module);
2689 module_exit(ib_mad_cleanup_module);