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>
41 MODULE_LICENSE("Dual BSD/GPL");
42 MODULE_DESCRIPTION("kernel IB MAD API");
43 MODULE_AUTHOR("Hal Rosenstock");
44 MODULE_AUTHOR("Sean Hefty");
47 kmem_cache_t *ib_mad_cache;
48 static struct list_head ib_mad_port_list;
49 static u32 ib_mad_client_id = 0;
52 static spinlock_t ib_mad_port_list_lock;
55 /* Forward declarations */
56 static int method_in_use(struct ib_mad_mgmt_method_table **method,
57 struct ib_mad_reg_req *mad_reg_req);
58 static void remove_mad_reg_req(struct ib_mad_agent_private *priv);
59 static struct ib_mad_agent_private *find_mad_agent(
60 struct ib_mad_port_private *port_priv,
62 static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
63 struct ib_mad_private *mad);
64 static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv);
65 static void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr,
66 struct ib_mad_send_wc *mad_send_wc);
67 static void timeout_sends(void *data);
68 static void cancel_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))
171 * ib_register_mad_agent - Register to send/receive MADs
173 struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
175 enum ib_qp_type qp_type,
176 struct ib_mad_reg_req *mad_reg_req,
178 ib_mad_send_handler send_handler,
179 ib_mad_recv_handler recv_handler,
182 struct ib_mad_port_private *port_priv;
183 struct ib_mad_agent *ret = ERR_PTR(-EINVAL);
184 struct ib_mad_agent_private *mad_agent_priv;
185 struct ib_mad_reg_req *reg_req = NULL;
186 struct ib_mad_mgmt_class_table *class;
187 struct ib_mad_mgmt_vendor_class_table *vendor;
188 struct ib_mad_mgmt_vendor_class *vendor_class;
189 struct ib_mad_mgmt_method_table *method;
192 u8 mgmt_class, vclass;
194 /* Validate parameters */
195 qpn = get_spl_qp_index(qp_type);
200 goto error1; /* XXX: until RMPP implemented */
202 /* Validate MAD registration request if supplied */
204 if (mad_reg_req->mgmt_class_version >= MAX_MGMT_VERSION)
208 if (mad_reg_req->mgmt_class >= MAX_MGMT_CLASS) {
210 * IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE is the only
211 * one in this range currently allowed
213 if (mad_reg_req->mgmt_class !=
214 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
216 } else if (mad_reg_req->mgmt_class == 0) {
218 * Class 0 is reserved in IBA and is used for
219 * aliasing of IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
222 } else if (is_vendor_class(mad_reg_req->mgmt_class)) {
224 * If class is in "new" vendor range,
225 * ensure supplied OUI is not zero
227 if (!is_vendor_oui(mad_reg_req->oui))
230 /* Make sure class supplied is consistent with QP type */
231 if (qp_type == IB_QPT_SMI) {
232 if ((mad_reg_req->mgmt_class !=
233 IB_MGMT_CLASS_SUBN_LID_ROUTED) &&
234 (mad_reg_req->mgmt_class !=
235 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE))
238 if ((mad_reg_req->mgmt_class ==
239 IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
240 (mad_reg_req->mgmt_class ==
241 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE))
245 /* No registration request supplied */
250 /* Validate device and port */
251 port_priv = ib_get_mad_port(device, port_num);
253 ret = ERR_PTR(-ENODEV);
257 /* Allocate structures */
258 mad_agent_priv = kmalloc(sizeof *mad_agent_priv, GFP_KERNEL);
259 if (!mad_agent_priv) {
260 ret = ERR_PTR(-ENOMEM);
263 memset(mad_agent_priv, 0, sizeof *mad_agent_priv);
265 mad_agent_priv->agent.mr = ib_get_dma_mr(port_priv->qp_info[qpn].qp->pd,
266 IB_ACCESS_LOCAL_WRITE);
267 if (IS_ERR(mad_agent_priv->agent.mr)) {
268 ret = ERR_PTR(-ENOMEM);
273 reg_req = kmalloc(sizeof *reg_req, GFP_KERNEL);
275 ret = ERR_PTR(-ENOMEM);
278 /* Make a copy of the MAD registration request */
279 memcpy(reg_req, mad_reg_req, sizeof *reg_req);
282 /* Now, fill in the various structures */
283 mad_agent_priv->qp_info = &port_priv->qp_info[qpn];
284 mad_agent_priv->reg_req = reg_req;
285 mad_agent_priv->rmpp_version = rmpp_version;
286 mad_agent_priv->agent.device = device;
287 mad_agent_priv->agent.recv_handler = recv_handler;
288 mad_agent_priv->agent.send_handler = send_handler;
289 mad_agent_priv->agent.context = context;
290 mad_agent_priv->agent.qp = port_priv->qp_info[qpn].qp;
291 mad_agent_priv->agent.port_num = port_num;
293 spin_lock_irqsave(&port_priv->reg_lock, flags);
294 mad_agent_priv->agent.hi_tid = ++ib_mad_client_id;
297 * Make sure MAD registration (if supplied)
298 * is non overlapping with any existing ones
301 mgmt_class = convert_mgmt_class(mad_reg_req->mgmt_class);
302 if (!is_vendor_class(mgmt_class)) {
303 class = port_priv->version[mad_reg_req->
304 mgmt_class_version].class;
306 method = class->method_table[mgmt_class];
308 if (method_in_use(&method,
313 ret2 = add_nonoui_reg_req(mad_reg_req, mad_agent_priv,
316 /* "New" vendor class range */
317 vendor = port_priv->version[mad_reg_req->
318 mgmt_class_version].vendor;
320 vclass = vendor_class_index(mgmt_class);
321 vendor_class = vendor->vendor_class[vclass];
323 if (is_vendor_method_in_use(
329 ret2 = add_oui_reg_req(mad_reg_req, mad_agent_priv);
337 /* Add mad agent into port's agent list */
338 list_add_tail(&mad_agent_priv->agent_list, &port_priv->agent_list);
339 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
341 spin_lock_init(&mad_agent_priv->lock);
342 INIT_LIST_HEAD(&mad_agent_priv->send_list);
343 INIT_LIST_HEAD(&mad_agent_priv->wait_list);
344 INIT_LIST_HEAD(&mad_agent_priv->done_list);
345 INIT_WORK(&mad_agent_priv->timed_work, timeout_sends, mad_agent_priv);
346 INIT_LIST_HEAD(&mad_agent_priv->local_list);
347 INIT_WORK(&mad_agent_priv->local_work, local_completions,
349 INIT_LIST_HEAD(&mad_agent_priv->canceled_list);
350 INIT_WORK(&mad_agent_priv->canceled_work, cancel_sends, mad_agent_priv);
351 atomic_set(&mad_agent_priv->refcount, 1);
352 init_waitqueue_head(&mad_agent_priv->wait);
354 return &mad_agent_priv->agent;
357 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
360 kfree(mad_agent_priv);
362 ib_dereg_mr(mad_agent_priv->agent.mr);
366 EXPORT_SYMBOL(ib_register_mad_agent);
368 static inline int is_snooping_sends(int mad_snoop_flags)
370 return (mad_snoop_flags &
371 (/*IB_MAD_SNOOP_POSTED_SENDS |
372 IB_MAD_SNOOP_RMPP_SENDS |*/
373 IB_MAD_SNOOP_SEND_COMPLETIONS /*|
374 IB_MAD_SNOOP_RMPP_SEND_COMPLETIONS*/));
377 static inline int is_snooping_recvs(int mad_snoop_flags)
379 return (mad_snoop_flags &
380 (IB_MAD_SNOOP_RECVS /*|
381 IB_MAD_SNOOP_RMPP_RECVS*/));
384 static int register_snoop_agent(struct ib_mad_qp_info *qp_info,
385 struct ib_mad_snoop_private *mad_snoop_priv)
387 struct ib_mad_snoop_private **new_snoop_table;
391 spin_lock_irqsave(&qp_info->snoop_lock, flags);
392 /* Check for empty slot in array. */
393 for (i = 0; i < qp_info->snoop_table_size; i++)
394 if (!qp_info->snoop_table[i])
397 if (i == qp_info->snoop_table_size) {
399 new_snoop_table = kmalloc(sizeof mad_snoop_priv *
400 qp_info->snoop_table_size + 1,
402 if (!new_snoop_table) {
406 if (qp_info->snoop_table) {
407 memcpy(new_snoop_table, qp_info->snoop_table,
408 sizeof mad_snoop_priv *
409 qp_info->snoop_table_size);
410 kfree(qp_info->snoop_table);
412 qp_info->snoop_table = new_snoop_table;
413 qp_info->snoop_table_size++;
415 qp_info->snoop_table[i] = mad_snoop_priv;
416 atomic_inc(&qp_info->snoop_count);
418 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
422 struct ib_mad_agent *ib_register_mad_snoop(struct ib_device *device,
424 enum ib_qp_type qp_type,
426 ib_mad_snoop_handler snoop_handler,
427 ib_mad_recv_handler recv_handler,
430 struct ib_mad_port_private *port_priv;
431 struct ib_mad_agent *ret;
432 struct ib_mad_snoop_private *mad_snoop_priv;
435 /* Validate parameters */
436 if ((is_snooping_sends(mad_snoop_flags) && !snoop_handler) ||
437 (is_snooping_recvs(mad_snoop_flags) && !recv_handler)) {
438 ret = ERR_PTR(-EINVAL);
441 qpn = get_spl_qp_index(qp_type);
443 ret = ERR_PTR(-EINVAL);
446 port_priv = ib_get_mad_port(device, port_num);
448 ret = ERR_PTR(-ENODEV);
451 /* Allocate structures */
452 mad_snoop_priv = kmalloc(sizeof *mad_snoop_priv, GFP_KERNEL);
453 if (!mad_snoop_priv) {
454 ret = ERR_PTR(-ENOMEM);
458 /* Now, fill in the various structures */
459 memset(mad_snoop_priv, 0, sizeof *mad_snoop_priv);
460 mad_snoop_priv->qp_info = &port_priv->qp_info[qpn];
461 mad_snoop_priv->agent.device = device;
462 mad_snoop_priv->agent.recv_handler = recv_handler;
463 mad_snoop_priv->agent.snoop_handler = snoop_handler;
464 mad_snoop_priv->agent.context = context;
465 mad_snoop_priv->agent.qp = port_priv->qp_info[qpn].qp;
466 mad_snoop_priv->agent.port_num = port_num;
467 mad_snoop_priv->mad_snoop_flags = mad_snoop_flags;
468 init_waitqueue_head(&mad_snoop_priv->wait);
469 mad_snoop_priv->snoop_index = register_snoop_agent(
470 &port_priv->qp_info[qpn],
472 if (mad_snoop_priv->snoop_index < 0) {
473 ret = ERR_PTR(mad_snoop_priv->snoop_index);
477 atomic_set(&mad_snoop_priv->refcount, 1);
478 return &mad_snoop_priv->agent;
481 kfree(mad_snoop_priv);
485 EXPORT_SYMBOL(ib_register_mad_snoop);
487 static void unregister_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
489 struct ib_mad_port_private *port_priv;
492 /* Note that we could still be handling received MADs */
495 * Canceling all sends results in dropping received response
496 * MADs, preventing us from queuing additional work
498 cancel_mads(mad_agent_priv);
499 port_priv = mad_agent_priv->qp_info->port_priv;
500 cancel_delayed_work(&mad_agent_priv->timed_work);
502 spin_lock_irqsave(&port_priv->reg_lock, flags);
503 remove_mad_reg_req(mad_agent_priv);
504 list_del(&mad_agent_priv->agent_list);
505 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
507 flush_workqueue(port_priv->wq);
509 atomic_dec(&mad_agent_priv->refcount);
510 wait_event(mad_agent_priv->wait,
511 !atomic_read(&mad_agent_priv->refcount));
513 if (mad_agent_priv->reg_req)
514 kfree(mad_agent_priv->reg_req);
515 ib_dereg_mr(mad_agent_priv->agent.mr);
516 kfree(mad_agent_priv);
519 static void unregister_mad_snoop(struct ib_mad_snoop_private *mad_snoop_priv)
521 struct ib_mad_qp_info *qp_info;
524 qp_info = mad_snoop_priv->qp_info;
525 spin_lock_irqsave(&qp_info->snoop_lock, flags);
526 qp_info->snoop_table[mad_snoop_priv->snoop_index] = NULL;
527 atomic_dec(&qp_info->snoop_count);
528 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
530 atomic_dec(&mad_snoop_priv->refcount);
531 wait_event(mad_snoop_priv->wait,
532 !atomic_read(&mad_snoop_priv->refcount));
534 kfree(mad_snoop_priv);
538 * ib_unregister_mad_agent - Unregisters a client from using MAD services
540 int ib_unregister_mad_agent(struct ib_mad_agent *mad_agent)
542 struct ib_mad_agent_private *mad_agent_priv;
543 struct ib_mad_snoop_private *mad_snoop_priv;
545 /* If the TID is zero, the agent can only snoop. */
546 if (mad_agent->hi_tid) {
547 mad_agent_priv = container_of(mad_agent,
548 struct ib_mad_agent_private,
550 unregister_mad_agent(mad_agent_priv);
552 mad_snoop_priv = container_of(mad_agent,
553 struct ib_mad_snoop_private,
555 unregister_mad_snoop(mad_snoop_priv);
559 EXPORT_SYMBOL(ib_unregister_mad_agent);
561 static inline int response_mad(struct ib_mad *mad)
563 /* Trap represses are responses although response bit is reset */
564 return ((mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS) ||
565 (mad->mad_hdr.method & IB_MGMT_METHOD_RESP));
568 static void dequeue_mad(struct ib_mad_list_head *mad_list)
570 struct ib_mad_queue *mad_queue;
573 BUG_ON(!mad_list->mad_queue);
574 mad_queue = mad_list->mad_queue;
575 spin_lock_irqsave(&mad_queue->lock, flags);
576 list_del(&mad_list->list);
578 spin_unlock_irqrestore(&mad_queue->lock, flags);
581 static void snoop_send(struct ib_mad_qp_info *qp_info,
582 struct ib_send_wr *send_wr,
583 struct ib_mad_send_wc *mad_send_wc,
586 struct ib_mad_snoop_private *mad_snoop_priv;
590 spin_lock_irqsave(&qp_info->snoop_lock, flags);
591 for (i = 0; i < qp_info->snoop_table_size; i++) {
592 mad_snoop_priv = qp_info->snoop_table[i];
593 if (!mad_snoop_priv ||
594 !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
597 atomic_inc(&mad_snoop_priv->refcount);
598 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
599 mad_snoop_priv->agent.snoop_handler(&mad_snoop_priv->agent,
600 send_wr, mad_send_wc);
601 if (atomic_dec_and_test(&mad_snoop_priv->refcount))
602 wake_up(&mad_snoop_priv->wait);
603 spin_lock_irqsave(&qp_info->snoop_lock, flags);
605 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
608 static void snoop_recv(struct ib_mad_qp_info *qp_info,
609 struct ib_mad_recv_wc *mad_recv_wc,
612 struct ib_mad_snoop_private *mad_snoop_priv;
616 spin_lock_irqsave(&qp_info->snoop_lock, flags);
617 for (i = 0; i < qp_info->snoop_table_size; i++) {
618 mad_snoop_priv = qp_info->snoop_table[i];
619 if (!mad_snoop_priv ||
620 !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
623 atomic_inc(&mad_snoop_priv->refcount);
624 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
625 mad_snoop_priv->agent.recv_handler(&mad_snoop_priv->agent,
627 if (atomic_dec_and_test(&mad_snoop_priv->refcount))
628 wake_up(&mad_snoop_priv->wait);
629 spin_lock_irqsave(&qp_info->snoop_lock, flags);
631 spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
634 static void build_smp_wc(u64 wr_id, u16 slid, u16 pkey_index, u8 port_num,
637 memset(wc, 0, sizeof *wc);
639 wc->status = IB_WC_SUCCESS;
640 wc->opcode = IB_WC_RECV;
641 wc->pkey_index = pkey_index;
642 wc->byte_len = sizeof(struct ib_mad) + sizeof(struct ib_grh);
647 wc->dlid_path_bits = 0;
648 wc->port_num = port_num;
652 * Return 0 if SMP is to be sent
653 * Return 1 if SMP was consumed locally (whether or not solicited)
654 * Return < 0 if error
656 static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
658 struct ib_send_wr *send_wr)
662 struct ib_mad_local_private *local;
663 struct ib_mad_private *mad_priv;
664 struct ib_mad_port_private *port_priv;
665 struct ib_mad_agent_private *recv_mad_agent = NULL;
666 struct ib_device *device = mad_agent_priv->agent.device;
667 u8 port_num = mad_agent_priv->agent.port_num;
670 if (!smi_handle_dr_smp_send(smp, device->node_type, port_num)) {
672 printk(KERN_ERR PFX "Invalid directed route\n");
675 /* Check to post send on QP or process locally */
676 ret = smi_check_local_dr_smp(smp, device, port_num);
677 if (!ret || !device->process_mad)
680 local = kmalloc(sizeof *local, GFP_ATOMIC);
683 printk(KERN_ERR PFX "No memory for ib_mad_local_private\n");
686 local->mad_priv = NULL;
687 local->recv_mad_agent = NULL;
688 mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_ATOMIC);
691 printk(KERN_ERR PFX "No memory for local response MAD\n");
696 build_smp_wc(send_wr->wr_id, smp->dr_slid, send_wr->wr.ud.pkey_index,
697 send_wr->wr.ud.port_num, &mad_wc);
699 /* No GRH for DR SMP */
700 ret = device->process_mad(device, 0, port_num, &mad_wc, NULL,
701 (struct ib_mad *)smp,
702 (struct ib_mad *)&mad_priv->mad);
705 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY:
706 if (response_mad(&mad_priv->mad.mad) &&
707 mad_agent_priv->agent.recv_handler) {
708 local->mad_priv = mad_priv;
709 local->recv_mad_agent = mad_agent_priv;
711 * Reference MAD agent until receive
712 * side of local completion handled
714 atomic_inc(&mad_agent_priv->refcount);
716 kmem_cache_free(ib_mad_cache, mad_priv);
718 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED:
719 kmem_cache_free(ib_mad_cache, mad_priv);
721 case IB_MAD_RESULT_SUCCESS:
722 /* Treat like an incoming receive MAD */
723 port_priv = ib_get_mad_port(mad_agent_priv->agent.device,
724 mad_agent_priv->agent.port_num);
726 mad_priv->mad.mad.mad_hdr.tid =
727 ((struct ib_mad *)smp)->mad_hdr.tid;
728 recv_mad_agent = find_mad_agent(port_priv,
731 if (!port_priv || !recv_mad_agent) {
732 kmem_cache_free(ib_mad_cache, mad_priv);
737 local->mad_priv = mad_priv;
738 local->recv_mad_agent = recv_mad_agent;
741 kmem_cache_free(ib_mad_cache, mad_priv);
747 local->send_wr = *send_wr;
748 local->send_wr.sg_list = local->sg_list;
749 memcpy(local->sg_list, send_wr->sg_list,
750 sizeof *send_wr->sg_list * send_wr->num_sge);
751 local->send_wr.next = NULL;
752 local->tid = send_wr->wr.ud.mad_hdr->tid;
753 local->wr_id = send_wr->wr_id;
754 /* Reference MAD agent until send side of local completion handled */
755 atomic_inc(&mad_agent_priv->refcount);
756 /* Queue local completion to local list */
757 spin_lock_irqsave(&mad_agent_priv->lock, flags);
758 list_add_tail(&local->completion_list, &mad_agent_priv->local_list);
759 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
760 queue_work(mad_agent_priv->qp_info->port_priv->wq,
761 &mad_agent_priv->local_work);
767 static int get_buf_length(int hdr_len, int data_len)
771 seg_size = sizeof(struct ib_mad) - hdr_len;
772 if (data_len && seg_size) {
773 pad = seg_size - data_len % seg_size;
778 return hdr_len + data_len + pad;
781 struct ib_mad_send_buf * ib_create_send_mad(struct ib_mad_agent *mad_agent,
782 u32 remote_qpn, u16 pkey_index,
784 int hdr_len, int data_len,
785 unsigned int __nocast gfp_mask)
787 struct ib_mad_agent_private *mad_agent_priv;
788 struct ib_mad_send_buf *send_buf;
792 mad_agent_priv = container_of(mad_agent,
793 struct ib_mad_agent_private, agent);
794 buf_size = get_buf_length(hdr_len, data_len);
796 buf = kmalloc(sizeof *send_buf + buf_size, gfp_mask);
798 return ERR_PTR(-ENOMEM);
800 send_buf = buf + buf_size;
801 memset(send_buf, 0, sizeof *send_buf);
804 send_buf->sge.addr = dma_map_single(mad_agent->device->dma_device,
805 buf, buf_size, DMA_TO_DEVICE);
806 pci_unmap_addr_set(send_buf, mapping, send_buf->sge.addr);
807 send_buf->sge.length = buf_size;
808 send_buf->sge.lkey = mad_agent->mr->lkey;
810 send_buf->send_wr.wr_id = (unsigned long) send_buf;
811 send_buf->send_wr.sg_list = &send_buf->sge;
812 send_buf->send_wr.num_sge = 1;
813 send_buf->send_wr.opcode = IB_WR_SEND;
814 send_buf->send_wr.send_flags = IB_SEND_SIGNALED;
815 send_buf->send_wr.wr.ud.ah = ah;
816 send_buf->send_wr.wr.ud.mad_hdr = &send_buf->mad->mad_hdr;
817 send_buf->send_wr.wr.ud.remote_qpn = remote_qpn;
818 send_buf->send_wr.wr.ud.remote_qkey = IB_QP_SET_QKEY;
819 send_buf->send_wr.wr.ud.pkey_index = pkey_index;
820 send_buf->mad_agent = mad_agent;
821 atomic_inc(&mad_agent_priv->refcount);
824 EXPORT_SYMBOL(ib_create_send_mad);
826 void ib_free_send_mad(struct ib_mad_send_buf *send_buf)
828 struct ib_mad_agent_private *mad_agent_priv;
830 mad_agent_priv = container_of(send_buf->mad_agent,
831 struct ib_mad_agent_private, agent);
833 dma_unmap_single(send_buf->mad_agent->device->dma_device,
834 pci_unmap_addr(send_buf, mapping),
835 send_buf->sge.length, DMA_TO_DEVICE);
836 kfree(send_buf->mad);
838 if (atomic_dec_and_test(&mad_agent_priv->refcount))
839 wake_up(&mad_agent_priv->wait);
841 EXPORT_SYMBOL(ib_free_send_mad);
843 static int ib_send_mad(struct ib_mad_send_wr_private *mad_send_wr)
845 struct ib_mad_qp_info *qp_info;
846 struct ib_send_wr *bad_send_wr;
850 /* Set WR ID to find mad_send_wr upon completion */
851 qp_info = mad_send_wr->mad_agent_priv->qp_info;
852 mad_send_wr->send_wr.wr_id = (unsigned long)&mad_send_wr->mad_list;
853 mad_send_wr->mad_list.mad_queue = &qp_info->send_queue;
855 spin_lock_irqsave(&qp_info->send_queue.lock, flags);
856 if (qp_info->send_queue.count++ < qp_info->send_queue.max_active) {
857 list_add_tail(&mad_send_wr->mad_list.list,
858 &qp_info->send_queue.list);
859 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
860 ret = ib_post_send(mad_send_wr->mad_agent_priv->agent.qp,
861 &mad_send_wr->send_wr, &bad_send_wr);
863 printk(KERN_ERR PFX "ib_post_send failed: %d\n", ret);
864 dequeue_mad(&mad_send_wr->mad_list);
867 list_add_tail(&mad_send_wr->mad_list.list,
868 &qp_info->overflow_list);
869 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
876 * ib_post_send_mad - Posts MAD(s) to the send queue of the QP associated
877 * with the registered client
879 int ib_post_send_mad(struct ib_mad_agent *mad_agent,
880 struct ib_send_wr *send_wr,
881 struct ib_send_wr **bad_send_wr)
884 struct ib_mad_agent_private *mad_agent_priv;
886 /* Validate supplied parameters */
890 if (!mad_agent || !send_wr)
893 if (!mad_agent->send_handler)
896 mad_agent_priv = container_of(mad_agent,
897 struct ib_mad_agent_private,
900 /* Walk list of send WRs and post each on send list */
903 struct ib_send_wr *next_send_wr;
904 struct ib_mad_send_wr_private *mad_send_wr;
907 /* Validate more parameters */
908 if (send_wr->num_sge > IB_MAD_SEND_REQ_MAX_SG)
911 if (send_wr->wr.ud.timeout_ms && !mad_agent->recv_handler)
914 if (!send_wr->wr.ud.mad_hdr) {
915 printk(KERN_ERR PFX "MAD header must be supplied "
916 "in WR %p\n", send_wr);
921 * Save pointer to next work request to post in case the
922 * current one completes, and the user modifies the work
923 * request associated with the completion
925 next_send_wr = (struct ib_send_wr *)send_wr->next;
927 smp = (struct ib_smp *)send_wr->wr.ud.mad_hdr;
928 if (smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
929 ret = handle_outgoing_dr_smp(mad_agent_priv, smp,
931 if (ret < 0) /* error */
933 else if (ret == 1) /* locally consumed */
937 /* Allocate MAD send WR tracking structure */
938 mad_send_wr = kmalloc(sizeof *mad_send_wr, GFP_ATOMIC);
940 printk(KERN_ERR PFX "No memory for "
941 "ib_mad_send_wr_private\n");
946 mad_send_wr->send_wr = *send_wr;
947 mad_send_wr->send_wr.sg_list = mad_send_wr->sg_list;
948 memcpy(mad_send_wr->sg_list, send_wr->sg_list,
949 sizeof *send_wr->sg_list * send_wr->num_sge);
950 mad_send_wr->wr_id = mad_send_wr->send_wr.wr_id;
951 mad_send_wr->send_wr.next = NULL;
952 mad_send_wr->tid = send_wr->wr.ud.mad_hdr->tid;
953 mad_send_wr->mad_agent_priv = mad_agent_priv;
954 /* Timeout will be updated after send completes */
955 mad_send_wr->timeout = msecs_to_jiffies(send_wr->wr.
957 mad_send_wr->retry = 0;
958 /* One reference for each work request to QP + response */
959 mad_send_wr->refcount = 1 + (mad_send_wr->timeout > 0);
960 mad_send_wr->status = IB_WC_SUCCESS;
962 /* Reference MAD agent until send completes */
963 atomic_inc(&mad_agent_priv->refcount);
964 spin_lock_irqsave(&mad_agent_priv->lock, flags);
965 list_add_tail(&mad_send_wr->agent_list,
966 &mad_agent_priv->send_list);
967 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
969 ret = ib_send_mad(mad_send_wr);
971 /* Fail send request */
972 spin_lock_irqsave(&mad_agent_priv->lock, flags);
973 list_del(&mad_send_wr->agent_list);
974 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
975 atomic_dec(&mad_agent_priv->refcount);
979 send_wr = next_send_wr;
984 *bad_send_wr = send_wr;
988 EXPORT_SYMBOL(ib_post_send_mad);
991 * ib_free_recv_mad - Returns data buffers used to receive
992 * a MAD to the access layer
994 void ib_free_recv_mad(struct ib_mad_recv_wc *mad_recv_wc)
996 struct ib_mad_recv_buf *entry;
997 struct ib_mad_private_header *mad_priv_hdr;
998 struct ib_mad_private *priv;
1000 mad_priv_hdr = container_of(mad_recv_wc,
1001 struct ib_mad_private_header,
1003 priv = container_of(mad_priv_hdr, struct ib_mad_private, header);
1006 * Walk receive buffer list associated with this WC
1007 * No need to remove them from list of receive buffers
1009 list_for_each_entry(entry, &mad_recv_wc->recv_buf.list, list) {
1010 /* Free previous receive buffer */
1011 kmem_cache_free(ib_mad_cache, priv);
1012 mad_priv_hdr = container_of(mad_recv_wc,
1013 struct ib_mad_private_header,
1015 priv = container_of(mad_priv_hdr, struct ib_mad_private,
1019 /* Free last buffer */
1020 kmem_cache_free(ib_mad_cache, priv);
1022 EXPORT_SYMBOL(ib_free_recv_mad);
1024 void ib_coalesce_recv_mad(struct ib_mad_recv_wc *mad_recv_wc,
1027 printk(KERN_ERR PFX "ib_coalesce_recv_mad() not implemented yet\n");
1029 EXPORT_SYMBOL(ib_coalesce_recv_mad);
1031 struct ib_mad_agent *ib_redirect_mad_qp(struct ib_qp *qp,
1033 ib_mad_send_handler send_handler,
1034 ib_mad_recv_handler recv_handler,
1037 return ERR_PTR(-EINVAL); /* XXX: for now */
1039 EXPORT_SYMBOL(ib_redirect_mad_qp);
1041 int ib_process_mad_wc(struct ib_mad_agent *mad_agent,
1044 printk(KERN_ERR PFX "ib_process_mad_wc() not implemented yet\n");
1047 EXPORT_SYMBOL(ib_process_mad_wc);
1049 static int method_in_use(struct ib_mad_mgmt_method_table **method,
1050 struct ib_mad_reg_req *mad_reg_req)
1054 for (i = find_first_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS);
1055 i < IB_MGMT_MAX_METHODS;
1056 i = find_next_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS,
1058 if ((*method)->agent[i]) {
1059 printk(KERN_ERR PFX "Method %d already in use\n", i);
1066 static int allocate_method_table(struct ib_mad_mgmt_method_table **method)
1068 /* Allocate management method table */
1069 *method = kmalloc(sizeof **method, GFP_ATOMIC);
1071 printk(KERN_ERR PFX "No memory for "
1072 "ib_mad_mgmt_method_table\n");
1075 /* Clear management method table */
1076 memset(*method, 0, sizeof **method);
1082 * Check to see if there are any methods still in use
1084 static int check_method_table(struct ib_mad_mgmt_method_table *method)
1088 for (i = 0; i < IB_MGMT_MAX_METHODS; i++)
1089 if (method->agent[i])
1095 * Check to see if there are any method tables for this class still in use
1097 static int check_class_table(struct ib_mad_mgmt_class_table *class)
1101 for (i = 0; i < MAX_MGMT_CLASS; i++)
1102 if (class->method_table[i])
1107 static int check_vendor_class(struct ib_mad_mgmt_vendor_class *vendor_class)
1111 for (i = 0; i < MAX_MGMT_OUI; i++)
1112 if (vendor_class->method_table[i])
1117 static int find_vendor_oui(struct ib_mad_mgmt_vendor_class *vendor_class,
1122 for (i = 0; i < MAX_MGMT_OUI; i++)
1123 /* Is there matching OUI for this vendor class ? */
1124 if (!memcmp(vendor_class->oui[i], oui, 3))
1130 static int check_vendor_table(struct ib_mad_mgmt_vendor_class_table *vendor)
1134 for (i = 0; i < MAX_MGMT_VENDOR_RANGE2; i++)
1135 if (vendor->vendor_class[i])
1141 static void remove_methods_mad_agent(struct ib_mad_mgmt_method_table *method,
1142 struct ib_mad_agent_private *agent)
1146 /* Remove any methods for this mad agent */
1147 for (i = 0; i < IB_MGMT_MAX_METHODS; i++) {
1148 if (method->agent[i] == agent) {
1149 method->agent[i] = NULL;
1154 static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1155 struct ib_mad_agent_private *agent_priv,
1158 struct ib_mad_port_private *port_priv;
1159 struct ib_mad_mgmt_class_table **class;
1160 struct ib_mad_mgmt_method_table **method;
1163 port_priv = agent_priv->qp_info->port_priv;
1164 class = &port_priv->version[mad_reg_req->mgmt_class_version].class;
1166 /* Allocate management class table for "new" class version */
1167 *class = kmalloc(sizeof **class, GFP_ATOMIC);
1169 printk(KERN_ERR PFX "No memory for "
1170 "ib_mad_mgmt_class_table\n");
1174 /* Clear management class table */
1175 memset(*class, 0, sizeof(**class));
1176 /* Allocate method table for this management class */
1177 method = &(*class)->method_table[mgmt_class];
1178 if ((ret = allocate_method_table(method)))
1181 method = &(*class)->method_table[mgmt_class];
1183 /* Allocate method table for this management class */
1184 if ((ret = allocate_method_table(method)))
1189 /* Now, make sure methods are not already in use */
1190 if (method_in_use(method, mad_reg_req))
1193 /* Finally, add in methods being registered */
1194 for (i = find_first_bit(mad_reg_req->method_mask,
1195 IB_MGMT_MAX_METHODS);
1196 i < IB_MGMT_MAX_METHODS;
1197 i = find_next_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS,
1199 (*method)->agent[i] = agent_priv;
1204 /* Remove any methods for this mad agent */
1205 remove_methods_mad_agent(*method, agent_priv);
1206 /* Now, check to see if there are any methods in use */
1207 if (!check_method_table(*method)) {
1208 /* If not, release management method table */
1221 static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1222 struct ib_mad_agent_private *agent_priv)
1224 struct ib_mad_port_private *port_priv;
1225 struct ib_mad_mgmt_vendor_class_table **vendor_table;
1226 struct ib_mad_mgmt_vendor_class_table *vendor = NULL;
1227 struct ib_mad_mgmt_vendor_class *vendor_class = NULL;
1228 struct ib_mad_mgmt_method_table **method;
1229 int i, ret = -ENOMEM;
1232 /* "New" vendor (with OUI) class */
1233 vclass = vendor_class_index(mad_reg_req->mgmt_class);
1234 port_priv = agent_priv->qp_info->port_priv;
1235 vendor_table = &port_priv->version[
1236 mad_reg_req->mgmt_class_version].vendor;
1237 if (!*vendor_table) {
1238 /* Allocate mgmt vendor class table for "new" class version */
1239 vendor = kmalloc(sizeof *vendor, GFP_ATOMIC);
1241 printk(KERN_ERR PFX "No memory for "
1242 "ib_mad_mgmt_vendor_class_table\n");
1245 /* Clear management vendor class table */
1246 memset(vendor, 0, sizeof(*vendor));
1247 *vendor_table = vendor;
1249 if (!(*vendor_table)->vendor_class[vclass]) {
1250 /* Allocate table for this management vendor class */
1251 vendor_class = kmalloc(sizeof *vendor_class, GFP_ATOMIC);
1252 if (!vendor_class) {
1253 printk(KERN_ERR PFX "No memory for "
1254 "ib_mad_mgmt_vendor_class\n");
1257 memset(vendor_class, 0, sizeof(*vendor_class));
1258 (*vendor_table)->vendor_class[vclass] = vendor_class;
1260 for (i = 0; i < MAX_MGMT_OUI; i++) {
1261 /* Is there matching OUI for this vendor class ? */
1262 if (!memcmp((*vendor_table)->vendor_class[vclass]->oui[i],
1263 mad_reg_req->oui, 3)) {
1264 method = &(*vendor_table)->vendor_class[
1265 vclass]->method_table[i];
1270 for (i = 0; i < MAX_MGMT_OUI; i++) {
1271 /* OUI slot available ? */
1272 if (!is_vendor_oui((*vendor_table)->vendor_class[
1274 method = &(*vendor_table)->vendor_class[
1275 vclass]->method_table[i];
1277 /* Allocate method table for this OUI */
1278 if ((ret = allocate_method_table(method)))
1280 memcpy((*vendor_table)->vendor_class[vclass]->oui[i],
1281 mad_reg_req->oui, 3);
1285 printk(KERN_ERR PFX "All OUI slots in use\n");
1289 /* Now, make sure methods are not already in use */
1290 if (method_in_use(method, mad_reg_req))
1293 /* Finally, add in methods being registered */
1294 for (i = find_first_bit(mad_reg_req->method_mask,
1295 IB_MGMT_MAX_METHODS);
1296 i < IB_MGMT_MAX_METHODS;
1297 i = find_next_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS,
1299 (*method)->agent[i] = agent_priv;
1304 /* Remove any methods for this mad agent */
1305 remove_methods_mad_agent(*method, agent_priv);
1306 /* Now, check to see if there are any methods in use */
1307 if (!check_method_table(*method)) {
1308 /* If not, release management method table */
1315 (*vendor_table)->vendor_class[vclass] = NULL;
1316 kfree(vendor_class);
1320 *vendor_table = NULL;
1327 static void remove_mad_reg_req(struct ib_mad_agent_private *agent_priv)
1329 struct ib_mad_port_private *port_priv;
1330 struct ib_mad_mgmt_class_table *class;
1331 struct ib_mad_mgmt_method_table *method;
1332 struct ib_mad_mgmt_vendor_class_table *vendor;
1333 struct ib_mad_mgmt_vendor_class *vendor_class;
1338 * Was MAD registration request supplied
1339 * with original registration ?
1341 if (!agent_priv->reg_req) {
1345 port_priv = agent_priv->qp_info->port_priv;
1346 mgmt_class = convert_mgmt_class(agent_priv->reg_req->mgmt_class);
1347 class = port_priv->version[
1348 agent_priv->reg_req->mgmt_class_version].class;
1352 method = class->method_table[mgmt_class];
1354 /* Remove any methods for this mad agent */
1355 remove_methods_mad_agent(method, agent_priv);
1356 /* Now, check to see if there are any methods still in use */
1357 if (!check_method_table(method)) {
1358 /* If not, release management method table */
1360 class->method_table[mgmt_class] = NULL;
1361 /* Any management classes left ? */
1362 if (!check_class_table(class)) {
1363 /* If not, release management class table */
1366 agent_priv->reg_req->
1367 mgmt_class_version].class = NULL;
1373 if (!is_vendor_class(mgmt_class))
1376 /* normalize mgmt_class to vendor range 2 */
1377 mgmt_class = vendor_class_index(agent_priv->reg_req->mgmt_class);
1378 vendor = port_priv->version[
1379 agent_priv->reg_req->mgmt_class_version].vendor;
1384 vendor_class = vendor->vendor_class[mgmt_class];
1386 index = find_vendor_oui(vendor_class, agent_priv->reg_req->oui);
1389 method = vendor_class->method_table[index];
1391 /* Remove any methods for this mad agent */
1392 remove_methods_mad_agent(method, agent_priv);
1394 * Now, check to see if there are
1395 * any methods still in use
1397 if (!check_method_table(method)) {
1398 /* If not, release management method table */
1400 vendor_class->method_table[index] = NULL;
1401 memset(vendor_class->oui[index], 0, 3);
1402 /* Any OUIs left ? */
1403 if (!check_vendor_class(vendor_class)) {
1404 /* If not, release vendor class table */
1405 kfree(vendor_class);
1406 vendor->vendor_class[mgmt_class] = NULL;
1407 /* Any other vendor classes left ? */
1408 if (!check_vendor_table(vendor)) {
1411 agent_priv->reg_req->
1412 mgmt_class_version].
1424 static struct ib_mad_agent_private *
1425 find_mad_agent(struct ib_mad_port_private *port_priv,
1428 struct ib_mad_agent_private *mad_agent = NULL;
1429 unsigned long flags;
1431 spin_lock_irqsave(&port_priv->reg_lock, flags);
1432 if (response_mad(mad)) {
1434 struct ib_mad_agent_private *entry;
1437 * Routing is based on high 32 bits of transaction ID
1440 hi_tid = be64_to_cpu(mad->mad_hdr.tid) >> 32;
1441 list_for_each_entry(entry, &port_priv->agent_list,
1443 if (entry->agent.hi_tid == hi_tid) {
1449 struct ib_mad_mgmt_class_table *class;
1450 struct ib_mad_mgmt_method_table *method;
1451 struct ib_mad_mgmt_vendor_class_table *vendor;
1452 struct ib_mad_mgmt_vendor_class *vendor_class;
1453 struct ib_vendor_mad *vendor_mad;
1457 * Routing is based on version, class, and method
1458 * For "newer" vendor MADs, also based on OUI
1460 if (mad->mad_hdr.class_version >= MAX_MGMT_VERSION)
1462 if (!is_vendor_class(mad->mad_hdr.mgmt_class)) {
1463 class = port_priv->version[
1464 mad->mad_hdr.class_version].class;
1467 method = class->method_table[convert_mgmt_class(
1468 mad->mad_hdr.mgmt_class)];
1470 mad_agent = method->agent[mad->mad_hdr.method &
1471 ~IB_MGMT_METHOD_RESP];
1473 vendor = port_priv->version[
1474 mad->mad_hdr.class_version].vendor;
1477 vendor_class = vendor->vendor_class[vendor_class_index(
1478 mad->mad_hdr.mgmt_class)];
1481 /* Find matching OUI */
1482 vendor_mad = (struct ib_vendor_mad *)mad;
1483 index = find_vendor_oui(vendor_class, vendor_mad->oui);
1486 method = vendor_class->method_table[index];
1488 mad_agent = method->agent[mad->mad_hdr.method &
1489 ~IB_MGMT_METHOD_RESP];
1495 if (mad_agent->agent.recv_handler)
1496 atomic_inc(&mad_agent->refcount);
1498 printk(KERN_NOTICE PFX "No receive handler for client "
1500 &mad_agent->agent, port_priv->port_num);
1505 spin_unlock_irqrestore(&port_priv->reg_lock, flags);
1510 static int validate_mad(struct ib_mad *mad, u32 qp_num)
1514 /* Make sure MAD base version is understood */
1515 if (mad->mad_hdr.base_version != IB_MGMT_BASE_VERSION) {
1516 printk(KERN_ERR PFX "MAD received with unsupported base "
1517 "version %d\n", mad->mad_hdr.base_version);
1521 /* Filter SMI packets sent to other than QP0 */
1522 if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
1523 (mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) {
1527 /* Filter GSI packets sent to QP0 */
1536 static struct ib_mad_send_wr_private*
1537 find_send_req(struct ib_mad_agent_private *mad_agent_priv,
1540 struct ib_mad_send_wr_private *mad_send_wr;
1542 list_for_each_entry(mad_send_wr, &mad_agent_priv->wait_list,
1544 if (mad_send_wr->tid == tid)
1549 * It's possible to receive the response before we've
1550 * been notified that the send has completed
1552 list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list,
1554 if (mad_send_wr->tid == tid && mad_send_wr->timeout) {
1555 /* Verify request has not been canceled */
1556 return (mad_send_wr->status == IB_WC_SUCCESS) ?
1563 static void ib_mark_req_done(struct ib_mad_send_wr_private *mad_send_wr)
1565 mad_send_wr->timeout = 0;
1566 if (mad_send_wr->refcount == 1) {
1567 list_del(&mad_send_wr->agent_list);
1568 list_add_tail(&mad_send_wr->agent_list,
1569 &mad_send_wr->mad_agent_priv->done_list);
1573 static void ib_mad_complete_recv(struct ib_mad_agent_private *mad_agent_priv,
1574 struct ib_mad_recv_wc *mad_recv_wc)
1576 struct ib_mad_send_wr_private *mad_send_wr;
1577 struct ib_mad_send_wc mad_send_wc;
1578 unsigned long flags;
1581 INIT_LIST_HEAD(&mad_recv_wc->recv_buf.list);
1582 /* Complete corresponding request */
1583 if (response_mad(mad_recv_wc->recv_buf.mad)) {
1584 tid = mad_recv_wc->recv_buf.mad->mad_hdr.tid;
1585 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1586 mad_send_wr = find_send_req(mad_agent_priv, tid);
1588 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1589 ib_free_recv_mad(mad_recv_wc);
1590 if (atomic_dec_and_test(&mad_agent_priv->refcount))
1591 wake_up(&mad_agent_priv->wait);
1594 ib_mark_req_done(mad_send_wr);
1595 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1597 /* Defined behavior is to complete response before request */
1598 mad_recv_wc->wc->wr_id = mad_send_wr->wr_id;
1599 mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent,
1601 atomic_dec(&mad_agent_priv->refcount);
1603 mad_send_wc.status = IB_WC_SUCCESS;
1604 mad_send_wc.vendor_err = 0;
1605 mad_send_wc.wr_id = mad_send_wr->wr_id;
1606 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
1608 mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent,
1610 if (atomic_dec_and_test(&mad_agent_priv->refcount))
1611 wake_up(&mad_agent_priv->wait);
1615 static void ib_mad_recv_done_handler(struct ib_mad_port_private *port_priv,
1618 struct ib_mad_qp_info *qp_info;
1619 struct ib_mad_private_header *mad_priv_hdr;
1620 struct ib_mad_private *recv, *response;
1621 struct ib_mad_list_head *mad_list;
1622 struct ib_mad_agent_private *mad_agent;
1624 response = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL);
1626 printk(KERN_ERR PFX "ib_mad_recv_done_handler no memory "
1627 "for response buffer\n");
1629 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
1630 qp_info = mad_list->mad_queue->qp_info;
1631 dequeue_mad(mad_list);
1633 mad_priv_hdr = container_of(mad_list, struct ib_mad_private_header,
1635 recv = container_of(mad_priv_hdr, struct ib_mad_private, header);
1636 dma_unmap_single(port_priv->device->dma_device,
1637 pci_unmap_addr(&recv->header, mapping),
1638 sizeof(struct ib_mad_private) -
1639 sizeof(struct ib_mad_private_header),
1642 /* Setup MAD receive work completion from "normal" work completion */
1643 recv->header.wc = *wc;
1644 recv->header.recv_wc.wc = &recv->header.wc;
1645 recv->header.recv_wc.mad_len = sizeof(struct ib_mad);
1646 recv->header.recv_wc.recv_buf.mad = &recv->mad.mad;
1647 recv->header.recv_wc.recv_buf.grh = &recv->grh;
1649 if (atomic_read(&qp_info->snoop_count))
1650 snoop_recv(qp_info, &recv->header.recv_wc, IB_MAD_SNOOP_RECVS);
1653 if (!validate_mad(&recv->mad.mad, qp_info->qp->qp_num))
1656 if (recv->mad.mad.mad_hdr.mgmt_class ==
1657 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
1658 if (!smi_handle_dr_smp_recv(&recv->mad.smp,
1659 port_priv->device->node_type,
1660 port_priv->port_num,
1661 port_priv->device->phys_port_cnt))
1663 if (!smi_check_forward_dr_smp(&recv->mad.smp))
1665 if (!smi_handle_dr_smp_send(&recv->mad.smp,
1666 port_priv->device->node_type,
1667 port_priv->port_num))
1669 if (!smi_check_local_dr_smp(&recv->mad.smp,
1671 port_priv->port_num))
1676 /* Give driver "right of first refusal" on incoming MAD */
1677 if (port_priv->device->process_mad) {
1681 printk(KERN_ERR PFX "No memory for response MAD\n");
1683 * Is it better to assume that
1684 * it wouldn't be processed ?
1689 ret = port_priv->device->process_mad(port_priv->device, 0,
1690 port_priv->port_num,
1693 &response->mad.mad);
1694 if (ret & IB_MAD_RESULT_SUCCESS) {
1695 if (ret & IB_MAD_RESULT_CONSUMED)
1697 if (ret & IB_MAD_RESULT_REPLY) {
1699 if (!agent_send(response, &recv->grh, wc,
1701 port_priv->port_num))
1708 mad_agent = find_mad_agent(port_priv, &recv->mad.mad);
1710 ib_mad_complete_recv(mad_agent, &recv->header.recv_wc);
1712 * recv is freed up in error cases in ib_mad_complete_recv
1713 * or via recv_handler in ib_mad_complete_recv()
1719 /* Post another receive request for this QP */
1721 ib_mad_post_receive_mads(qp_info, response);
1723 kmem_cache_free(ib_mad_cache, recv);
1725 ib_mad_post_receive_mads(qp_info, recv);
1728 static void adjust_timeout(struct ib_mad_agent_private *mad_agent_priv)
1730 struct ib_mad_send_wr_private *mad_send_wr;
1731 unsigned long delay;
1733 if (list_empty(&mad_agent_priv->wait_list)) {
1734 cancel_delayed_work(&mad_agent_priv->timed_work);
1736 mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
1737 struct ib_mad_send_wr_private,
1740 if (time_after(mad_agent_priv->timeout,
1741 mad_send_wr->timeout)) {
1742 mad_agent_priv->timeout = mad_send_wr->timeout;
1743 cancel_delayed_work(&mad_agent_priv->timed_work);
1744 delay = mad_send_wr->timeout - jiffies;
1745 if ((long)delay <= 0)
1747 queue_delayed_work(mad_agent_priv->qp_info->
1749 &mad_agent_priv->timed_work, delay);
1754 static void wait_for_response(struct ib_mad_send_wr_private *mad_send_wr)
1756 struct ib_mad_agent_private *mad_agent_priv;
1757 struct ib_mad_send_wr_private *temp_mad_send_wr;
1758 struct list_head *list_item;
1759 unsigned long delay;
1761 mad_agent_priv = mad_send_wr->mad_agent_priv;
1762 list_del(&mad_send_wr->agent_list);
1764 delay = mad_send_wr->timeout;
1765 mad_send_wr->timeout += jiffies;
1767 list_for_each_prev(list_item, &mad_agent_priv->wait_list) {
1768 temp_mad_send_wr = list_entry(list_item,
1769 struct ib_mad_send_wr_private,
1771 if (time_after(mad_send_wr->timeout,
1772 temp_mad_send_wr->timeout))
1775 list_add(&mad_send_wr->agent_list, list_item);
1777 /* Reschedule a work item if we have a shorter timeout */
1778 if (mad_agent_priv->wait_list.next == &mad_send_wr->agent_list) {
1779 cancel_delayed_work(&mad_agent_priv->timed_work);
1780 queue_delayed_work(mad_agent_priv->qp_info->port_priv->wq,
1781 &mad_agent_priv->timed_work, delay);
1786 * Process a send work completion
1788 static void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr,
1789 struct ib_mad_send_wc *mad_send_wc)
1791 struct ib_mad_agent_private *mad_agent_priv;
1792 unsigned long flags;
1794 mad_agent_priv = mad_send_wr->mad_agent_priv;
1795 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1796 if (mad_send_wc->status != IB_WC_SUCCESS &&
1797 mad_send_wr->status == IB_WC_SUCCESS) {
1798 mad_send_wr->status = mad_send_wc->status;
1799 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
1802 if (--mad_send_wr->refcount > 0) {
1803 if (mad_send_wr->refcount == 1 && mad_send_wr->timeout &&
1804 mad_send_wr->status == IB_WC_SUCCESS) {
1805 wait_for_response(mad_send_wr);
1807 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1811 /* Remove send from MAD agent and notify client of completion */
1812 list_del(&mad_send_wr->agent_list);
1813 adjust_timeout(mad_agent_priv);
1814 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1816 if (mad_send_wr->status != IB_WC_SUCCESS )
1817 mad_send_wc->status = mad_send_wr->status;
1818 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
1821 /* Release reference on agent taken when sending */
1822 if (atomic_dec_and_test(&mad_agent_priv->refcount))
1823 wake_up(&mad_agent_priv->wait);
1828 static void ib_mad_send_done_handler(struct ib_mad_port_private *port_priv,
1831 struct ib_mad_send_wr_private *mad_send_wr, *queued_send_wr;
1832 struct ib_mad_list_head *mad_list;
1833 struct ib_mad_qp_info *qp_info;
1834 struct ib_mad_queue *send_queue;
1835 struct ib_send_wr *bad_send_wr;
1836 unsigned long flags;
1839 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
1840 mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
1842 send_queue = mad_list->mad_queue;
1843 qp_info = send_queue->qp_info;
1846 queued_send_wr = NULL;
1847 spin_lock_irqsave(&send_queue->lock, flags);
1848 list_del(&mad_list->list);
1850 /* Move queued send to the send queue */
1851 if (send_queue->count-- > send_queue->max_active) {
1852 mad_list = container_of(qp_info->overflow_list.next,
1853 struct ib_mad_list_head, list);
1854 queued_send_wr = container_of(mad_list,
1855 struct ib_mad_send_wr_private,
1857 list_del(&mad_list->list);
1858 list_add_tail(&mad_list->list, &send_queue->list);
1860 spin_unlock_irqrestore(&send_queue->lock, flags);
1862 /* Restore client wr_id in WC and complete send */
1863 wc->wr_id = mad_send_wr->wr_id;
1864 if (atomic_read(&qp_info->snoop_count))
1865 snoop_send(qp_info, &mad_send_wr->send_wr,
1866 (struct ib_mad_send_wc *)wc,
1867 IB_MAD_SNOOP_SEND_COMPLETIONS);
1868 ib_mad_complete_send_wr(mad_send_wr, (struct ib_mad_send_wc *)wc);
1870 if (queued_send_wr) {
1871 ret = ib_post_send(qp_info->qp, &queued_send_wr->send_wr,
1874 printk(KERN_ERR PFX "ib_post_send failed: %d\n", ret);
1875 mad_send_wr = queued_send_wr;
1876 wc->status = IB_WC_LOC_QP_OP_ERR;
1882 static void mark_sends_for_retry(struct ib_mad_qp_info *qp_info)
1884 struct ib_mad_send_wr_private *mad_send_wr;
1885 struct ib_mad_list_head *mad_list;
1886 unsigned long flags;
1888 spin_lock_irqsave(&qp_info->send_queue.lock, flags);
1889 list_for_each_entry(mad_list, &qp_info->send_queue.list, list) {
1890 mad_send_wr = container_of(mad_list,
1891 struct ib_mad_send_wr_private,
1893 mad_send_wr->retry = 1;
1895 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
1898 static void mad_error_handler(struct ib_mad_port_private *port_priv,
1901 struct ib_mad_list_head *mad_list;
1902 struct ib_mad_qp_info *qp_info;
1903 struct ib_mad_send_wr_private *mad_send_wr;
1906 /* Determine if failure was a send or receive */
1907 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
1908 qp_info = mad_list->mad_queue->qp_info;
1909 if (mad_list->mad_queue == &qp_info->recv_queue)
1911 * Receive errors indicate that the QP has entered the error
1912 * state - error handling/shutdown code will cleanup
1917 * Send errors will transition the QP to SQE - move
1918 * QP to RTS and repost flushed work requests
1920 mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
1922 if (wc->status == IB_WC_WR_FLUSH_ERR) {
1923 if (mad_send_wr->retry) {
1925 struct ib_send_wr *bad_send_wr;
1927 mad_send_wr->retry = 0;
1928 ret = ib_post_send(qp_info->qp, &mad_send_wr->send_wr,
1931 ib_mad_send_done_handler(port_priv, wc);
1933 ib_mad_send_done_handler(port_priv, wc);
1935 struct ib_qp_attr *attr;
1937 /* Transition QP to RTS and fail offending send */
1938 attr = kmalloc(sizeof *attr, GFP_KERNEL);
1940 attr->qp_state = IB_QPS_RTS;
1941 attr->cur_qp_state = IB_QPS_SQE;
1942 ret = ib_modify_qp(qp_info->qp, attr,
1943 IB_QP_STATE | IB_QP_CUR_STATE);
1946 printk(KERN_ERR PFX "mad_error_handler - "
1947 "ib_modify_qp to RTS : %d\n", ret);
1949 mark_sends_for_retry(qp_info);
1951 ib_mad_send_done_handler(port_priv, wc);
1956 * IB MAD completion callback
1958 static void ib_mad_completion_handler(void *data)
1960 struct ib_mad_port_private *port_priv;
1963 port_priv = (struct ib_mad_port_private *)data;
1964 ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
1966 while (ib_poll_cq(port_priv->cq, 1, &wc) == 1) {
1967 if (wc.status == IB_WC_SUCCESS) {
1968 switch (wc.opcode) {
1970 ib_mad_send_done_handler(port_priv, &wc);
1973 ib_mad_recv_done_handler(port_priv, &wc);
1980 mad_error_handler(port_priv, &wc);
1984 static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv)
1986 unsigned long flags;
1987 struct ib_mad_send_wr_private *mad_send_wr, *temp_mad_send_wr;
1988 struct ib_mad_send_wc mad_send_wc;
1989 struct list_head cancel_list;
1991 INIT_LIST_HEAD(&cancel_list);
1993 spin_lock_irqsave(&mad_agent_priv->lock, flags);
1994 list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
1995 &mad_agent_priv->send_list, agent_list) {
1996 if (mad_send_wr->status == IB_WC_SUCCESS) {
1997 mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
1998 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2002 /* Empty wait list to prevent receives from finding a request */
2003 list_splice_init(&mad_agent_priv->wait_list, &cancel_list);
2004 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2006 /* Report all cancelled requests */
2007 mad_send_wc.status = IB_WC_WR_FLUSH_ERR;
2008 mad_send_wc.vendor_err = 0;
2010 list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
2011 &cancel_list, agent_list) {
2012 mad_send_wc.wr_id = mad_send_wr->wr_id;
2013 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2016 list_del(&mad_send_wr->agent_list);
2018 atomic_dec(&mad_agent_priv->refcount);
2022 static struct ib_mad_send_wr_private*
2023 find_send_by_wr_id(struct ib_mad_agent_private *mad_agent_priv,
2026 struct ib_mad_send_wr_private *mad_send_wr;
2028 list_for_each_entry(mad_send_wr, &mad_agent_priv->wait_list,
2030 if (mad_send_wr->wr_id == wr_id)
2034 list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list,
2036 if (mad_send_wr->wr_id == wr_id)
2042 void cancel_sends(void *data)
2044 struct ib_mad_agent_private *mad_agent_priv;
2045 struct ib_mad_send_wr_private *mad_send_wr;
2046 struct ib_mad_send_wc mad_send_wc;
2047 unsigned long flags;
2049 mad_agent_priv = data;
2051 mad_send_wc.status = IB_WC_WR_FLUSH_ERR;
2052 mad_send_wc.vendor_err = 0;
2054 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2055 while (!list_empty(&mad_agent_priv->canceled_list)) {
2056 mad_send_wr = list_entry(mad_agent_priv->canceled_list.next,
2057 struct ib_mad_send_wr_private,
2060 list_del(&mad_send_wr->agent_list);
2061 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2063 mad_send_wc.wr_id = mad_send_wr->wr_id;
2064 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2068 if (atomic_dec_and_test(&mad_agent_priv->refcount))
2069 wake_up(&mad_agent_priv->wait);
2070 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2072 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2075 void ib_cancel_mad(struct ib_mad_agent *mad_agent,
2078 struct ib_mad_agent_private *mad_agent_priv;
2079 struct ib_mad_send_wr_private *mad_send_wr;
2080 unsigned long flags;
2082 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
2084 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2085 mad_send_wr = find_send_by_wr_id(mad_agent_priv, wr_id);
2087 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2091 if (mad_send_wr->status == IB_WC_SUCCESS)
2092 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2094 if (mad_send_wr->refcount != 0) {
2095 mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
2096 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2100 list_del(&mad_send_wr->agent_list);
2101 list_add_tail(&mad_send_wr->agent_list, &mad_agent_priv->canceled_list);
2102 adjust_timeout(mad_agent_priv);
2103 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2105 queue_work(mad_agent_priv->qp_info->port_priv->wq,
2106 &mad_agent_priv->canceled_work);
2110 EXPORT_SYMBOL(ib_cancel_mad);
2112 static void local_completions(void *data)
2114 struct ib_mad_agent_private *mad_agent_priv;
2115 struct ib_mad_local_private *local;
2116 struct ib_mad_agent_private *recv_mad_agent;
2117 unsigned long flags;
2119 struct ib_mad_send_wc mad_send_wc;
2121 mad_agent_priv = (struct ib_mad_agent_private *)data;
2123 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2124 while (!list_empty(&mad_agent_priv->local_list)) {
2125 local = list_entry(mad_agent_priv->local_list.next,
2126 struct ib_mad_local_private,
2128 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2129 if (local->mad_priv) {
2130 recv_mad_agent = local->recv_mad_agent;
2131 if (!recv_mad_agent) {
2132 printk(KERN_ERR PFX "No receive MAD agent for local completion\n");
2133 kmem_cache_free(ib_mad_cache, local->mad_priv);
2134 goto local_send_completion;
2138 * Defined behavior is to complete response
2141 build_smp_wc(local->wr_id, IB_LID_PERMISSIVE,
2143 recv_mad_agent->agent.port_num, &wc);
2145 local->mad_priv->header.recv_wc.wc = &wc;
2146 local->mad_priv->header.recv_wc.mad_len =
2147 sizeof(struct ib_mad);
2148 INIT_LIST_HEAD(&local->mad_priv->header.recv_wc.recv_buf.list);
2149 local->mad_priv->header.recv_wc.recv_buf.grh = NULL;
2150 local->mad_priv->header.recv_wc.recv_buf.mad =
2151 &local->mad_priv->mad.mad;
2152 if (atomic_read(&recv_mad_agent->qp_info->snoop_count))
2153 snoop_recv(recv_mad_agent->qp_info,
2154 &local->mad_priv->header.recv_wc,
2155 IB_MAD_SNOOP_RECVS);
2156 recv_mad_agent->agent.recv_handler(
2157 &recv_mad_agent->agent,
2158 &local->mad_priv->header.recv_wc);
2159 spin_lock_irqsave(&recv_mad_agent->lock, flags);
2160 atomic_dec(&recv_mad_agent->refcount);
2161 spin_unlock_irqrestore(&recv_mad_agent->lock, flags);
2164 local_send_completion:
2166 mad_send_wc.status = IB_WC_SUCCESS;
2167 mad_send_wc.vendor_err = 0;
2168 mad_send_wc.wr_id = local->wr_id;
2169 if (atomic_read(&mad_agent_priv->qp_info->snoop_count))
2170 snoop_send(mad_agent_priv->qp_info, &local->send_wr,
2172 IB_MAD_SNOOP_SEND_COMPLETIONS);
2173 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2176 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2177 list_del(&local->completion_list);
2178 atomic_dec(&mad_agent_priv->refcount);
2181 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2184 static void timeout_sends(void *data)
2186 struct ib_mad_agent_private *mad_agent_priv;
2187 struct ib_mad_send_wr_private *mad_send_wr;
2188 struct ib_mad_send_wc mad_send_wc;
2189 unsigned long flags, delay;
2191 mad_agent_priv = (struct ib_mad_agent_private *)data;
2193 mad_send_wc.status = IB_WC_RESP_TIMEOUT_ERR;
2194 mad_send_wc.vendor_err = 0;
2196 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2197 while (!list_empty(&mad_agent_priv->wait_list)) {
2198 mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
2199 struct ib_mad_send_wr_private,
2202 if (time_after(mad_send_wr->timeout, jiffies)) {
2203 delay = mad_send_wr->timeout - jiffies;
2204 if ((long)delay <= 0)
2206 queue_delayed_work(mad_agent_priv->qp_info->
2208 &mad_agent_priv->timed_work, delay);
2212 list_del(&mad_send_wr->agent_list);
2213 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2215 mad_send_wc.wr_id = mad_send_wr->wr_id;
2216 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2220 atomic_dec(&mad_agent_priv->refcount);
2221 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2223 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2226 static void ib_mad_thread_completion_handler(struct ib_cq *cq)
2228 struct ib_mad_port_private *port_priv = cq->cq_context;
2230 queue_work(port_priv->wq, &port_priv->work);
2234 * Allocate receive MADs and post receive WRs for them
2236 static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
2237 struct ib_mad_private *mad)
2239 unsigned long flags;
2241 struct ib_mad_private *mad_priv;
2242 struct ib_sge sg_list;
2243 struct ib_recv_wr recv_wr, *bad_recv_wr;
2244 struct ib_mad_queue *recv_queue = &qp_info->recv_queue;
2246 /* Initialize common scatter list fields */
2247 sg_list.length = sizeof *mad_priv - sizeof mad_priv->header;
2248 sg_list.lkey = (*qp_info->port_priv->mr).lkey;
2250 /* Initialize common receive WR fields */
2251 recv_wr.next = NULL;
2252 recv_wr.sg_list = &sg_list;
2253 recv_wr.num_sge = 1;
2256 /* Allocate and map receive buffer */
2261 mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL);
2263 printk(KERN_ERR PFX "No memory for receive buffer\n");
2268 sg_list.addr = dma_map_single(qp_info->port_priv->
2272 sizeof mad_priv->header,
2274 pci_unmap_addr_set(&mad_priv->header, mapping, sg_list.addr);
2275 recv_wr.wr_id = (unsigned long)&mad_priv->header.mad_list;
2276 mad_priv->header.mad_list.mad_queue = recv_queue;
2278 /* Post receive WR */
2279 spin_lock_irqsave(&recv_queue->lock, flags);
2280 post = (++recv_queue->count < recv_queue->max_active);
2281 list_add_tail(&mad_priv->header.mad_list.list, &recv_queue->list);
2282 spin_unlock_irqrestore(&recv_queue->lock, flags);
2283 ret = ib_post_recv(qp_info->qp, &recv_wr, &bad_recv_wr);
2285 spin_lock_irqsave(&recv_queue->lock, flags);
2286 list_del(&mad_priv->header.mad_list.list);
2287 recv_queue->count--;
2288 spin_unlock_irqrestore(&recv_queue->lock, flags);
2289 dma_unmap_single(qp_info->port_priv->device->dma_device,
2290 pci_unmap_addr(&mad_priv->header,
2293 sizeof mad_priv->header,
2295 kmem_cache_free(ib_mad_cache, mad_priv);
2296 printk(KERN_ERR PFX "ib_post_recv failed: %d\n", ret);
2305 * Return all the posted receive MADs
2307 static void cleanup_recv_queue(struct ib_mad_qp_info *qp_info)
2309 struct ib_mad_private_header *mad_priv_hdr;
2310 struct ib_mad_private *recv;
2311 struct ib_mad_list_head *mad_list;
2313 while (!list_empty(&qp_info->recv_queue.list)) {
2315 mad_list = list_entry(qp_info->recv_queue.list.next,
2316 struct ib_mad_list_head, list);
2317 mad_priv_hdr = container_of(mad_list,
2318 struct ib_mad_private_header,
2320 recv = container_of(mad_priv_hdr, struct ib_mad_private,
2323 /* Remove from posted receive MAD list */
2324 list_del(&mad_list->list);
2326 dma_unmap_single(qp_info->port_priv->device->dma_device,
2327 pci_unmap_addr(&recv->header, mapping),
2328 sizeof(struct ib_mad_private) -
2329 sizeof(struct ib_mad_private_header),
2331 kmem_cache_free(ib_mad_cache, recv);
2334 qp_info->recv_queue.count = 0;
2340 static int ib_mad_port_start(struct ib_mad_port_private *port_priv)
2343 struct ib_qp_attr *attr;
2346 attr = kmalloc(sizeof *attr, GFP_KERNEL);
2348 printk(KERN_ERR PFX "Couldn't kmalloc ib_qp_attr\n");
2352 for (i = 0; i < IB_MAD_QPS_CORE; i++) {
2353 qp = port_priv->qp_info[i].qp;
2355 * PKey index for QP1 is irrelevant but
2356 * one is needed for the Reset to Init transition
2358 attr->qp_state = IB_QPS_INIT;
2359 attr->pkey_index = 0;
2360 attr->qkey = (qp->qp_num == 0) ? 0 : IB_QP1_QKEY;
2361 ret = ib_modify_qp(qp, attr, IB_QP_STATE |
2362 IB_QP_PKEY_INDEX | IB_QP_QKEY);
2364 printk(KERN_ERR PFX "Couldn't change QP%d state to "
2365 "INIT: %d\n", i, ret);
2369 attr->qp_state = IB_QPS_RTR;
2370 ret = ib_modify_qp(qp, attr, IB_QP_STATE);
2372 printk(KERN_ERR PFX "Couldn't change QP%d state to "
2373 "RTR: %d\n", i, ret);
2377 attr->qp_state = IB_QPS_RTS;
2378 attr->sq_psn = IB_MAD_SEND_Q_PSN;
2379 ret = ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_SQ_PSN);
2381 printk(KERN_ERR PFX "Couldn't change QP%d state to "
2382 "RTS: %d\n", i, ret);
2387 ret = ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
2389 printk(KERN_ERR PFX "Failed to request completion "
2390 "notification: %d\n", ret);
2394 for (i = 0; i < IB_MAD_QPS_CORE; i++) {
2395 ret = ib_mad_post_receive_mads(&port_priv->qp_info[i], NULL);
2397 printk(KERN_ERR PFX "Couldn't post receive WRs\n");
2406 static void qp_event_handler(struct ib_event *event, void *qp_context)
2408 struct ib_mad_qp_info *qp_info = qp_context;
2410 /* It's worse than that! He's dead, Jim! */
2411 printk(KERN_ERR PFX "Fatal error (%d) on MAD QP (%d)\n",
2412 event->event, qp_info->qp->qp_num);
2415 static void init_mad_queue(struct ib_mad_qp_info *qp_info,
2416 struct ib_mad_queue *mad_queue)
2418 mad_queue->qp_info = qp_info;
2419 mad_queue->count = 0;
2420 spin_lock_init(&mad_queue->lock);
2421 INIT_LIST_HEAD(&mad_queue->list);
2424 static void init_mad_qp(struct ib_mad_port_private *port_priv,
2425 struct ib_mad_qp_info *qp_info)
2427 qp_info->port_priv = port_priv;
2428 init_mad_queue(qp_info, &qp_info->send_queue);
2429 init_mad_queue(qp_info, &qp_info->recv_queue);
2430 INIT_LIST_HEAD(&qp_info->overflow_list);
2431 spin_lock_init(&qp_info->snoop_lock);
2432 qp_info->snoop_table = NULL;
2433 qp_info->snoop_table_size = 0;
2434 atomic_set(&qp_info->snoop_count, 0);
2437 static int create_mad_qp(struct ib_mad_qp_info *qp_info,
2438 enum ib_qp_type qp_type)
2440 struct ib_qp_init_attr qp_init_attr;
2443 memset(&qp_init_attr, 0, sizeof qp_init_attr);
2444 qp_init_attr.send_cq = qp_info->port_priv->cq;
2445 qp_init_attr.recv_cq = qp_info->port_priv->cq;
2446 qp_init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
2447 qp_init_attr.cap.max_send_wr = IB_MAD_QP_SEND_SIZE;
2448 qp_init_attr.cap.max_recv_wr = IB_MAD_QP_RECV_SIZE;
2449 qp_init_attr.cap.max_send_sge = IB_MAD_SEND_REQ_MAX_SG;
2450 qp_init_attr.cap.max_recv_sge = IB_MAD_RECV_REQ_MAX_SG;
2451 qp_init_attr.qp_type = qp_type;
2452 qp_init_attr.port_num = qp_info->port_priv->port_num;
2453 qp_init_attr.qp_context = qp_info;
2454 qp_init_attr.event_handler = qp_event_handler;
2455 qp_info->qp = ib_create_qp(qp_info->port_priv->pd, &qp_init_attr);
2456 if (IS_ERR(qp_info->qp)) {
2457 printk(KERN_ERR PFX "Couldn't create ib_mad QP%d\n",
2458 get_spl_qp_index(qp_type));
2459 ret = PTR_ERR(qp_info->qp);
2462 /* Use minimum queue sizes unless the CQ is resized */
2463 qp_info->send_queue.max_active = IB_MAD_QP_SEND_SIZE;
2464 qp_info->recv_queue.max_active = IB_MAD_QP_RECV_SIZE;
2471 static void destroy_mad_qp(struct ib_mad_qp_info *qp_info)
2473 ib_destroy_qp(qp_info->qp);
2474 if (qp_info->snoop_table)
2475 kfree(qp_info->snoop_table);
2480 * Create the QP, PD, MR, and CQ if needed
2482 static int ib_mad_port_open(struct ib_device *device,
2486 struct ib_mad_port_private *port_priv;
2487 unsigned long flags;
2488 char name[sizeof "ib_mad123"];
2490 /* Create new device info */
2491 port_priv = kmalloc(sizeof *port_priv, GFP_KERNEL);
2493 printk(KERN_ERR PFX "No memory for ib_mad_port_private\n");
2496 memset(port_priv, 0, sizeof *port_priv);
2497 port_priv->device = device;
2498 port_priv->port_num = port_num;
2499 spin_lock_init(&port_priv->reg_lock);
2500 INIT_LIST_HEAD(&port_priv->agent_list);
2501 init_mad_qp(port_priv, &port_priv->qp_info[0]);
2502 init_mad_qp(port_priv, &port_priv->qp_info[1]);
2504 cq_size = (IB_MAD_QP_SEND_SIZE + IB_MAD_QP_RECV_SIZE) * 2;
2505 port_priv->cq = ib_create_cq(port_priv->device,
2507 ib_mad_thread_completion_handler,
2508 NULL, port_priv, cq_size);
2509 if (IS_ERR(port_priv->cq)) {
2510 printk(KERN_ERR PFX "Couldn't create ib_mad CQ\n");
2511 ret = PTR_ERR(port_priv->cq);
2515 port_priv->pd = ib_alloc_pd(device);
2516 if (IS_ERR(port_priv->pd)) {
2517 printk(KERN_ERR PFX "Couldn't create ib_mad PD\n");
2518 ret = PTR_ERR(port_priv->pd);
2522 port_priv->mr = ib_get_dma_mr(port_priv->pd, IB_ACCESS_LOCAL_WRITE);
2523 if (IS_ERR(port_priv->mr)) {
2524 printk(KERN_ERR PFX "Couldn't get ib_mad DMA MR\n");
2525 ret = PTR_ERR(port_priv->mr);
2529 ret = create_mad_qp(&port_priv->qp_info[0], IB_QPT_SMI);
2532 ret = create_mad_qp(&port_priv->qp_info[1], IB_QPT_GSI);
2536 snprintf(name, sizeof name, "ib_mad%d", port_num);
2537 port_priv->wq = create_singlethread_workqueue(name);
2538 if (!port_priv->wq) {
2542 INIT_WORK(&port_priv->work, ib_mad_completion_handler, port_priv);
2544 ret = ib_mad_port_start(port_priv);
2546 printk(KERN_ERR PFX "Couldn't start port\n");
2550 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2551 list_add_tail(&port_priv->port_list, &ib_mad_port_list);
2552 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2556 destroy_workqueue(port_priv->wq);
2558 destroy_mad_qp(&port_priv->qp_info[1]);
2560 destroy_mad_qp(&port_priv->qp_info[0]);
2562 ib_dereg_mr(port_priv->mr);
2564 ib_dealloc_pd(port_priv->pd);
2566 ib_destroy_cq(port_priv->cq);
2567 cleanup_recv_queue(&port_priv->qp_info[1]);
2568 cleanup_recv_queue(&port_priv->qp_info[0]);
2577 * If there are no classes using the port, free the port
2578 * resources (CQ, MR, PD, QP) and remove the port's info structure
2580 static int ib_mad_port_close(struct ib_device *device, int port_num)
2582 struct ib_mad_port_private *port_priv;
2583 unsigned long flags;
2585 spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2586 port_priv = __ib_get_mad_port(device, port_num);
2587 if (port_priv == NULL) {
2588 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2589 printk(KERN_ERR PFX "Port %d not found\n", port_num);
2592 list_del(&port_priv->port_list);
2593 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2595 /* Stop processing completions. */
2596 flush_workqueue(port_priv->wq);
2597 destroy_workqueue(port_priv->wq);
2598 destroy_mad_qp(&port_priv->qp_info[1]);
2599 destroy_mad_qp(&port_priv->qp_info[0]);
2600 ib_dereg_mr(port_priv->mr);
2601 ib_dealloc_pd(port_priv->pd);
2602 ib_destroy_cq(port_priv->cq);
2603 cleanup_recv_queue(&port_priv->qp_info[1]);
2604 cleanup_recv_queue(&port_priv->qp_info[0]);
2605 /* XXX: Handle deallocation of MAD registration tables */
2612 static void ib_mad_init_device(struct ib_device *device)
2614 int num_ports, cur_port, i;
2616 if (device->node_type == IB_NODE_SWITCH) {
2620 num_ports = device->phys_port_cnt;
2623 for (i = 0; i < num_ports; i++, cur_port++) {
2624 if (ib_mad_port_open(device, cur_port)) {
2625 printk(KERN_ERR PFX "Couldn't open %s port %d\n",
2626 device->name, cur_port);
2627 goto error_device_open;
2629 if (ib_agent_port_open(device, cur_port)) {
2630 printk(KERN_ERR PFX "Couldn't open %s port %d "
2632 device->name, cur_port);
2633 goto error_device_open;
2641 if (ib_agent_port_close(device, cur_port))
2642 printk(KERN_ERR PFX "Couldn't close %s port %d "
2644 device->name, cur_port);
2645 if (ib_mad_port_close(device, cur_port))
2646 printk(KERN_ERR PFX "Couldn't close %s port %d\n",
2647 device->name, cur_port);
2652 static void ib_mad_remove_device(struct ib_device *device)
2654 int i, num_ports, cur_port;
2656 if (device->node_type == IB_NODE_SWITCH) {
2660 num_ports = device->phys_port_cnt;
2663 for (i = 0; i < num_ports; i++, cur_port++) {
2664 if (ib_agent_port_close(device, cur_port))
2665 printk(KERN_ERR PFX "Couldn't close %s port %d "
2667 device->name, cur_port);
2668 if (ib_mad_port_close(device, cur_port))
2669 printk(KERN_ERR PFX "Couldn't close %s port %d\n",
2670 device->name, cur_port);
2674 static struct ib_client mad_client = {
2676 .add = ib_mad_init_device,
2677 .remove = ib_mad_remove_device
2680 static int __init ib_mad_init_module(void)
2684 spin_lock_init(&ib_mad_port_list_lock);
2685 spin_lock_init(&ib_agent_port_list_lock);
2687 ib_mad_cache = kmem_cache_create("ib_mad",
2688 sizeof(struct ib_mad_private),
2693 if (!ib_mad_cache) {
2694 printk(KERN_ERR PFX "Couldn't create ib_mad cache\n");
2699 INIT_LIST_HEAD(&ib_mad_port_list);
2701 if (ib_register_client(&mad_client)) {
2702 printk(KERN_ERR PFX "Couldn't register ib_mad client\n");
2710 kmem_cache_destroy(ib_mad_cache);
2715 static void __exit ib_mad_cleanup_module(void)
2717 ib_unregister_client(&mad_client);
2719 if (kmem_cache_destroy(ib_mad_cache)) {
2720 printk(KERN_DEBUG PFX "Failed to destroy ib_mad cache\n");
2724 module_init(ib_mad_init_module);
2725 module_exit(ib_mad_cleanup_module);