2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Voltaire, Inc. All rights reserved.
4 * Copyright (c) 2006 Intel Corporation. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 * $Id: sa_query.c 2811 2005-07-06 18:11:43Z halr $
37 #include <linux/module.h>
38 #include <linux/init.h>
39 #include <linux/err.h>
40 #include <linux/random.h>
41 #include <linux/spinlock.h>
42 #include <linux/slab.h>
43 #include <linux/dma-mapping.h>
44 #include <linux/kref.h>
45 #include <linux/idr.h>
46 #include <linux/workqueue.h>
48 #include <rdma/ib_pack.h>
49 #include <rdma/ib_cache.h>
52 MODULE_AUTHOR("Roland Dreier");
53 MODULE_DESCRIPTION("InfiniBand subnet administration query support");
54 MODULE_LICENSE("Dual BSD/GPL");
63 struct ib_mad_agent
*agent
;
64 struct ib_sa_sm_ah
*sm_ah
;
65 struct work_struct update_task
;
71 int start_port
, end_port
;
72 struct ib_event_handler event_handler
;
73 struct ib_sa_port port
[0];
77 void (*callback
)(struct ib_sa_query
*, int, struct ib_sa_mad
*);
78 void (*release
)(struct ib_sa_query
*);
79 struct ib_sa_client
*client
;
80 struct ib_sa_port
*port
;
81 struct ib_mad_send_buf
*mad_buf
;
82 struct ib_sa_sm_ah
*sm_ah
;
86 struct ib_sa_service_query
{
87 void (*callback
)(int, struct ib_sa_service_rec
*, void *);
89 struct ib_sa_query sa_query
;
92 struct ib_sa_path_query
{
93 void (*callback
)(int, struct ib_sa_path_rec
*, void *);
95 struct ib_sa_query sa_query
;
98 struct ib_sa_mcmember_query
{
99 void (*callback
)(int, struct ib_sa_mcmember_rec
*, void *);
101 struct ib_sa_query sa_query
;
104 static void ib_sa_add_one(struct ib_device
*device
);
105 static void ib_sa_remove_one(struct ib_device
*device
);
107 static struct ib_client sa_client
= {
109 .add
= ib_sa_add_one
,
110 .remove
= ib_sa_remove_one
113 static spinlock_t idr_lock
;
114 static DEFINE_IDR(query_idr
);
116 static spinlock_t tid_lock
;
119 #define PATH_REC_FIELD(field) \
120 .struct_offset_bytes = offsetof(struct ib_sa_path_rec, field), \
121 .struct_size_bytes = sizeof ((struct ib_sa_path_rec *) 0)->field, \
122 .field_name = "sa_path_rec:" #field
124 static const struct ib_field path_rec_table
[] = {
133 { PATH_REC_FIELD(dgid
),
137 { PATH_REC_FIELD(sgid
),
141 { PATH_REC_FIELD(dlid
),
145 { PATH_REC_FIELD(slid
),
149 { PATH_REC_FIELD(raw_traffic
),
157 { PATH_REC_FIELD(flow_label
),
161 { PATH_REC_FIELD(hop_limit
),
165 { PATH_REC_FIELD(traffic_class
),
169 { PATH_REC_FIELD(reversible
),
173 { PATH_REC_FIELD(numb_path
),
177 { PATH_REC_FIELD(pkey
),
185 { PATH_REC_FIELD(sl
),
189 { PATH_REC_FIELD(mtu_selector
),
193 { PATH_REC_FIELD(mtu
),
197 { PATH_REC_FIELD(rate_selector
),
201 { PATH_REC_FIELD(rate
),
205 { PATH_REC_FIELD(packet_life_time_selector
),
209 { PATH_REC_FIELD(packet_life_time
),
213 { PATH_REC_FIELD(preference
),
223 #define MCMEMBER_REC_FIELD(field) \
224 .struct_offset_bytes = offsetof(struct ib_sa_mcmember_rec, field), \
225 .struct_size_bytes = sizeof ((struct ib_sa_mcmember_rec *) 0)->field, \
226 .field_name = "sa_mcmember_rec:" #field
228 static const struct ib_field mcmember_rec_table
[] = {
229 { MCMEMBER_REC_FIELD(mgid
),
233 { MCMEMBER_REC_FIELD(port_gid
),
237 { MCMEMBER_REC_FIELD(qkey
),
241 { MCMEMBER_REC_FIELD(mlid
),
245 { MCMEMBER_REC_FIELD(mtu_selector
),
249 { MCMEMBER_REC_FIELD(mtu
),
253 { MCMEMBER_REC_FIELD(traffic_class
),
257 { MCMEMBER_REC_FIELD(pkey
),
261 { MCMEMBER_REC_FIELD(rate_selector
),
265 { MCMEMBER_REC_FIELD(rate
),
269 { MCMEMBER_REC_FIELD(packet_life_time_selector
),
273 { MCMEMBER_REC_FIELD(packet_life_time
),
277 { MCMEMBER_REC_FIELD(sl
),
281 { MCMEMBER_REC_FIELD(flow_label
),
285 { MCMEMBER_REC_FIELD(hop_limit
),
289 { MCMEMBER_REC_FIELD(scope
),
293 { MCMEMBER_REC_FIELD(join_state
),
297 { MCMEMBER_REC_FIELD(proxy_join
),
307 #define SERVICE_REC_FIELD(field) \
308 .struct_offset_bytes = offsetof(struct ib_sa_service_rec, field), \
309 .struct_size_bytes = sizeof ((struct ib_sa_service_rec *) 0)->field, \
310 .field_name = "sa_service_rec:" #field
312 static const struct ib_field service_rec_table
[] = {
313 { SERVICE_REC_FIELD(id
),
317 { SERVICE_REC_FIELD(gid
),
321 { SERVICE_REC_FIELD(pkey
),
325 { SERVICE_REC_FIELD(lease
),
329 { SERVICE_REC_FIELD(key
),
333 { SERVICE_REC_FIELD(name
),
337 { SERVICE_REC_FIELD(data8
),
341 { SERVICE_REC_FIELD(data16
),
345 { SERVICE_REC_FIELD(data32
),
349 { SERVICE_REC_FIELD(data64
),
355 static void free_sm_ah(struct kref
*kref
)
357 struct ib_sa_sm_ah
*sm_ah
= container_of(kref
, struct ib_sa_sm_ah
, ref
);
359 ib_destroy_ah(sm_ah
->ah
);
363 static void update_sm_ah(struct work_struct
*work
)
365 struct ib_sa_port
*port
=
366 container_of(work
, struct ib_sa_port
, update_task
);
367 struct ib_sa_sm_ah
*new_ah
, *old_ah
;
368 struct ib_port_attr port_attr
;
369 struct ib_ah_attr ah_attr
;
371 if (ib_query_port(port
->agent
->device
, port
->port_num
, &port_attr
)) {
372 printk(KERN_WARNING
"Couldn't query port\n");
376 new_ah
= kmalloc(sizeof *new_ah
, GFP_KERNEL
);
378 printk(KERN_WARNING
"Couldn't allocate new SM AH\n");
382 kref_init(&new_ah
->ref
);
383 new_ah
->src_path_mask
= (1 << port_attr
.lmc
) - 1;
385 memset(&ah_attr
, 0, sizeof ah_attr
);
386 ah_attr
.dlid
= port_attr
.sm_lid
;
387 ah_attr
.sl
= port_attr
.sm_sl
;
388 ah_attr
.port_num
= port
->port_num
;
390 new_ah
->ah
= ib_create_ah(port
->agent
->qp
->pd
, &ah_attr
);
391 if (IS_ERR(new_ah
->ah
)) {
392 printk(KERN_WARNING
"Couldn't create new SM AH\n");
397 spin_lock_irq(&port
->ah_lock
);
398 old_ah
= port
->sm_ah
;
399 port
->sm_ah
= new_ah
;
400 spin_unlock_irq(&port
->ah_lock
);
403 kref_put(&old_ah
->ref
, free_sm_ah
);
406 static void ib_sa_event(struct ib_event_handler
*handler
, struct ib_event
*event
)
408 if (event
->event
== IB_EVENT_PORT_ERR
||
409 event
->event
== IB_EVENT_PORT_ACTIVE
||
410 event
->event
== IB_EVENT_LID_CHANGE
||
411 event
->event
== IB_EVENT_PKEY_CHANGE
||
412 event
->event
== IB_EVENT_SM_CHANGE
||
413 event
->event
== IB_EVENT_CLIENT_REREGISTER
) {
414 struct ib_sa_device
*sa_dev
;
415 sa_dev
= container_of(handler
, typeof(*sa_dev
), event_handler
);
417 schedule_work(&sa_dev
->port
[event
->element
.port_num
-
418 sa_dev
->start_port
].update_task
);
422 void ib_sa_register_client(struct ib_sa_client
*client
)
424 atomic_set(&client
->users
, 1);
425 init_completion(&client
->comp
);
427 EXPORT_SYMBOL(ib_sa_register_client
);
429 void ib_sa_unregister_client(struct ib_sa_client
*client
)
431 ib_sa_client_put(client
);
432 wait_for_completion(&client
->comp
);
434 EXPORT_SYMBOL(ib_sa_unregister_client
);
437 * ib_sa_cancel_query - try to cancel an SA query
438 * @id:ID of query to cancel
439 * @query:query pointer to cancel
441 * Try to cancel an SA query. If the id and query don't match up or
442 * the query has already completed, nothing is done. Otherwise the
443 * query is canceled and will complete with a status of -EINTR.
445 void ib_sa_cancel_query(int id
, struct ib_sa_query
*query
)
448 struct ib_mad_agent
*agent
;
449 struct ib_mad_send_buf
*mad_buf
;
451 spin_lock_irqsave(&idr_lock
, flags
);
452 if (idr_find(&query_idr
, id
) != query
) {
453 spin_unlock_irqrestore(&idr_lock
, flags
);
456 agent
= query
->port
->agent
;
457 mad_buf
= query
->mad_buf
;
458 spin_unlock_irqrestore(&idr_lock
, flags
);
460 ib_cancel_mad(agent
, mad_buf
);
462 EXPORT_SYMBOL(ib_sa_cancel_query
);
464 static u8
get_src_path_mask(struct ib_device
*device
, u8 port_num
)
466 struct ib_sa_device
*sa_dev
;
467 struct ib_sa_port
*port
;
471 sa_dev
= ib_get_client_data(device
, &sa_client
);
475 port
= &sa_dev
->port
[port_num
- sa_dev
->start_port
];
476 spin_lock_irqsave(&port
->ah_lock
, flags
);
477 src_path_mask
= port
->sm_ah
? port
->sm_ah
->src_path_mask
: 0x7f;
478 spin_unlock_irqrestore(&port
->ah_lock
, flags
);
480 return src_path_mask
;
483 int ib_init_ah_from_path(struct ib_device
*device
, u8 port_num
,
484 struct ib_sa_path_rec
*rec
, struct ib_ah_attr
*ah_attr
)
489 memset(ah_attr
, 0, sizeof *ah_attr
);
490 ah_attr
->dlid
= be16_to_cpu(rec
->dlid
);
491 ah_attr
->sl
= rec
->sl
;
492 ah_attr
->src_path_bits
= be16_to_cpu(rec
->slid
) &
493 get_src_path_mask(device
, port_num
);
494 ah_attr
->port_num
= port_num
;
495 ah_attr
->static_rate
= rec
->rate
;
497 if (rec
->hop_limit
> 1) {
498 ah_attr
->ah_flags
= IB_AH_GRH
;
499 ah_attr
->grh
.dgid
= rec
->dgid
;
501 ret
= ib_find_cached_gid(device
, &rec
->sgid
, &port_num
,
506 ah_attr
->grh
.sgid_index
= gid_index
;
507 ah_attr
->grh
.flow_label
= be32_to_cpu(rec
->flow_label
);
508 ah_attr
->grh
.hop_limit
= rec
->hop_limit
;
509 ah_attr
->grh
.traffic_class
= rec
->traffic_class
;
513 EXPORT_SYMBOL(ib_init_ah_from_path
);
515 static void init_mad(struct ib_sa_mad
*mad
, struct ib_mad_agent
*agent
)
519 memset(mad
, 0, sizeof *mad
);
521 mad
->mad_hdr
.base_version
= IB_MGMT_BASE_VERSION
;
522 mad
->mad_hdr
.mgmt_class
= IB_MGMT_CLASS_SUBN_ADM
;
523 mad
->mad_hdr
.class_version
= IB_SA_CLASS_VERSION
;
525 spin_lock_irqsave(&tid_lock
, flags
);
527 cpu_to_be64(((u64
) agent
->hi_tid
) << 32 | tid
++);
528 spin_unlock_irqrestore(&tid_lock
, flags
);
531 static int send_mad(struct ib_sa_query
*query
, int timeout_ms
, gfp_t gfp_mask
)
537 if (!idr_pre_get(&query_idr
, gfp_mask
))
539 spin_lock_irqsave(&idr_lock
, flags
);
540 ret
= idr_get_new(&query_idr
, query
, &id
);
541 spin_unlock_irqrestore(&idr_lock
, flags
);
547 query
->mad_buf
->timeout_ms
= timeout_ms
;
548 query
->mad_buf
->context
[0] = query
;
551 spin_lock_irqsave(&query
->port
->ah_lock
, flags
);
552 kref_get(&query
->port
->sm_ah
->ref
);
553 query
->sm_ah
= query
->port
->sm_ah
;
554 spin_unlock_irqrestore(&query
->port
->ah_lock
, flags
);
556 query
->mad_buf
->ah
= query
->sm_ah
->ah
;
558 ret
= ib_post_send_mad(query
->mad_buf
, NULL
);
560 spin_lock_irqsave(&idr_lock
, flags
);
561 idr_remove(&query_idr
, id
);
562 spin_unlock_irqrestore(&idr_lock
, flags
);
564 kref_put(&query
->sm_ah
->ref
, free_sm_ah
);
568 * It's not safe to dereference query any more, because the
569 * send may already have completed and freed the query in
572 return ret
? ret
: id
;
575 static void ib_sa_path_rec_callback(struct ib_sa_query
*sa_query
,
577 struct ib_sa_mad
*mad
)
579 struct ib_sa_path_query
*query
=
580 container_of(sa_query
, struct ib_sa_path_query
, sa_query
);
583 struct ib_sa_path_rec rec
;
585 ib_unpack(path_rec_table
, ARRAY_SIZE(path_rec_table
),
587 query
->callback(status
, &rec
, query
->context
);
589 query
->callback(status
, NULL
, query
->context
);
592 static void ib_sa_path_rec_release(struct ib_sa_query
*sa_query
)
594 kfree(container_of(sa_query
, struct ib_sa_path_query
, sa_query
));
598 * ib_sa_path_rec_get - Start a Path get query
600 * @device:device to send query on
601 * @port_num: port number to send query on
602 * @rec:Path Record to send in query
603 * @comp_mask:component mask to send in query
604 * @timeout_ms:time to wait for response
605 * @gfp_mask:GFP mask to use for internal allocations
606 * @callback:function called when query completes, times out or is
608 * @context:opaque user context passed to callback
609 * @sa_query:query context, used to cancel query
611 * Send a Path Record Get query to the SA to look up a path. The
612 * callback function will be called when the query completes (or
613 * fails); status is 0 for a successful response, -EINTR if the query
614 * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
615 * occurred sending the query. The resp parameter of the callback is
616 * only valid if status is 0.
618 * If the return value of ib_sa_path_rec_get() is negative, it is an
619 * error code. Otherwise it is a query ID that can be used to cancel
622 int ib_sa_path_rec_get(struct ib_sa_client
*client
,
623 struct ib_device
*device
, u8 port_num
,
624 struct ib_sa_path_rec
*rec
,
625 ib_sa_comp_mask comp_mask
,
626 int timeout_ms
, gfp_t gfp_mask
,
627 void (*callback
)(int status
,
628 struct ib_sa_path_rec
*resp
,
631 struct ib_sa_query
**sa_query
)
633 struct ib_sa_path_query
*query
;
634 struct ib_sa_device
*sa_dev
= ib_get_client_data(device
, &sa_client
);
635 struct ib_sa_port
*port
;
636 struct ib_mad_agent
*agent
;
637 struct ib_sa_mad
*mad
;
643 port
= &sa_dev
->port
[port_num
- sa_dev
->start_port
];
646 query
= kmalloc(sizeof *query
, gfp_mask
);
650 query
->sa_query
.mad_buf
= ib_create_send_mad(agent
, 1, 0,
652 IB_MGMT_SA_DATA
, gfp_mask
);
653 if (!query
->sa_query
.mad_buf
) {
658 ib_sa_client_get(client
);
659 query
->sa_query
.client
= client
;
660 query
->callback
= callback
;
661 query
->context
= context
;
663 mad
= query
->sa_query
.mad_buf
->mad
;
664 init_mad(mad
, agent
);
666 query
->sa_query
.callback
= callback
? ib_sa_path_rec_callback
: NULL
;
667 query
->sa_query
.release
= ib_sa_path_rec_release
;
668 query
->sa_query
.port
= port
;
669 mad
->mad_hdr
.method
= IB_MGMT_METHOD_GET
;
670 mad
->mad_hdr
.attr_id
= cpu_to_be16(IB_SA_ATTR_PATH_REC
);
671 mad
->sa_hdr
.comp_mask
= comp_mask
;
673 ib_pack(path_rec_table
, ARRAY_SIZE(path_rec_table
), rec
, mad
->data
);
675 *sa_query
= &query
->sa_query
;
677 ret
= send_mad(&query
->sa_query
, timeout_ms
, gfp_mask
);
685 ib_sa_client_put(query
->sa_query
.client
);
686 ib_free_send_mad(query
->sa_query
.mad_buf
);
692 EXPORT_SYMBOL(ib_sa_path_rec_get
);
694 static void ib_sa_service_rec_callback(struct ib_sa_query
*sa_query
,
696 struct ib_sa_mad
*mad
)
698 struct ib_sa_service_query
*query
=
699 container_of(sa_query
, struct ib_sa_service_query
, sa_query
);
702 struct ib_sa_service_rec rec
;
704 ib_unpack(service_rec_table
, ARRAY_SIZE(service_rec_table
),
706 query
->callback(status
, &rec
, query
->context
);
708 query
->callback(status
, NULL
, query
->context
);
711 static void ib_sa_service_rec_release(struct ib_sa_query
*sa_query
)
713 kfree(container_of(sa_query
, struct ib_sa_service_query
, sa_query
));
717 * ib_sa_service_rec_query - Start Service Record operation
719 * @device:device to send request on
720 * @port_num: port number to send request on
721 * @method:SA method - should be get, set, or delete
722 * @rec:Service Record to send in request
723 * @comp_mask:component mask to send in request
724 * @timeout_ms:time to wait for response
725 * @gfp_mask:GFP mask to use for internal allocations
726 * @callback:function called when request completes, times out or is
728 * @context:opaque user context passed to callback
729 * @sa_query:request context, used to cancel request
731 * Send a Service Record set/get/delete to the SA to register,
732 * unregister or query a service record.
733 * The callback function will be called when the request completes (or
734 * fails); status is 0 for a successful response, -EINTR if the query
735 * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
736 * occurred sending the query. The resp parameter of the callback is
737 * only valid if status is 0.
739 * If the return value of ib_sa_service_rec_query() is negative, it is an
740 * error code. Otherwise it is a request ID that can be used to cancel
743 int ib_sa_service_rec_query(struct ib_sa_client
*client
,
744 struct ib_device
*device
, u8 port_num
, u8 method
,
745 struct ib_sa_service_rec
*rec
,
746 ib_sa_comp_mask comp_mask
,
747 int timeout_ms
, gfp_t gfp_mask
,
748 void (*callback
)(int status
,
749 struct ib_sa_service_rec
*resp
,
752 struct ib_sa_query
**sa_query
)
754 struct ib_sa_service_query
*query
;
755 struct ib_sa_device
*sa_dev
= ib_get_client_data(device
, &sa_client
);
756 struct ib_sa_port
*port
;
757 struct ib_mad_agent
*agent
;
758 struct ib_sa_mad
*mad
;
764 port
= &sa_dev
->port
[port_num
- sa_dev
->start_port
];
767 if (method
!= IB_MGMT_METHOD_GET
&&
768 method
!= IB_MGMT_METHOD_SET
&&
769 method
!= IB_SA_METHOD_DELETE
)
772 query
= kmalloc(sizeof *query
, gfp_mask
);
776 query
->sa_query
.mad_buf
= ib_create_send_mad(agent
, 1, 0,
778 IB_MGMT_SA_DATA
, gfp_mask
);
779 if (!query
->sa_query
.mad_buf
) {
784 ib_sa_client_get(client
);
785 query
->sa_query
.client
= client
;
786 query
->callback
= callback
;
787 query
->context
= context
;
789 mad
= query
->sa_query
.mad_buf
->mad
;
790 init_mad(mad
, agent
);
792 query
->sa_query
.callback
= callback
? ib_sa_service_rec_callback
: NULL
;
793 query
->sa_query
.release
= ib_sa_service_rec_release
;
794 query
->sa_query
.port
= port
;
795 mad
->mad_hdr
.method
= method
;
796 mad
->mad_hdr
.attr_id
= cpu_to_be16(IB_SA_ATTR_SERVICE_REC
);
797 mad
->sa_hdr
.comp_mask
= comp_mask
;
799 ib_pack(service_rec_table
, ARRAY_SIZE(service_rec_table
),
802 *sa_query
= &query
->sa_query
;
804 ret
= send_mad(&query
->sa_query
, timeout_ms
, gfp_mask
);
812 ib_sa_client_put(query
->sa_query
.client
);
813 ib_free_send_mad(query
->sa_query
.mad_buf
);
819 EXPORT_SYMBOL(ib_sa_service_rec_query
);
821 static void ib_sa_mcmember_rec_callback(struct ib_sa_query
*sa_query
,
823 struct ib_sa_mad
*mad
)
825 struct ib_sa_mcmember_query
*query
=
826 container_of(sa_query
, struct ib_sa_mcmember_query
, sa_query
);
829 struct ib_sa_mcmember_rec rec
;
831 ib_unpack(mcmember_rec_table
, ARRAY_SIZE(mcmember_rec_table
),
833 query
->callback(status
, &rec
, query
->context
);
835 query
->callback(status
, NULL
, query
->context
);
838 static void ib_sa_mcmember_rec_release(struct ib_sa_query
*sa_query
)
840 kfree(container_of(sa_query
, struct ib_sa_mcmember_query
, sa_query
));
843 int ib_sa_mcmember_rec_query(struct ib_sa_client
*client
,
844 struct ib_device
*device
, u8 port_num
,
846 struct ib_sa_mcmember_rec
*rec
,
847 ib_sa_comp_mask comp_mask
,
848 int timeout_ms
, gfp_t gfp_mask
,
849 void (*callback
)(int status
,
850 struct ib_sa_mcmember_rec
*resp
,
853 struct ib_sa_query
**sa_query
)
855 struct ib_sa_mcmember_query
*query
;
856 struct ib_sa_device
*sa_dev
= ib_get_client_data(device
, &sa_client
);
857 struct ib_sa_port
*port
;
858 struct ib_mad_agent
*agent
;
859 struct ib_sa_mad
*mad
;
865 port
= &sa_dev
->port
[port_num
- sa_dev
->start_port
];
868 query
= kmalloc(sizeof *query
, gfp_mask
);
872 query
->sa_query
.mad_buf
= ib_create_send_mad(agent
, 1, 0,
874 IB_MGMT_SA_DATA
, gfp_mask
);
875 if (!query
->sa_query
.mad_buf
) {
880 ib_sa_client_get(client
);
881 query
->sa_query
.client
= client
;
882 query
->callback
= callback
;
883 query
->context
= context
;
885 mad
= query
->sa_query
.mad_buf
->mad
;
886 init_mad(mad
, agent
);
888 query
->sa_query
.callback
= callback
? ib_sa_mcmember_rec_callback
: NULL
;
889 query
->sa_query
.release
= ib_sa_mcmember_rec_release
;
890 query
->sa_query
.port
= port
;
891 mad
->mad_hdr
.method
= method
;
892 mad
->mad_hdr
.attr_id
= cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC
);
893 mad
->sa_hdr
.comp_mask
= comp_mask
;
895 ib_pack(mcmember_rec_table
, ARRAY_SIZE(mcmember_rec_table
),
898 *sa_query
= &query
->sa_query
;
900 ret
= send_mad(&query
->sa_query
, timeout_ms
, gfp_mask
);
908 ib_sa_client_put(query
->sa_query
.client
);
909 ib_free_send_mad(query
->sa_query
.mad_buf
);
916 static void send_handler(struct ib_mad_agent
*agent
,
917 struct ib_mad_send_wc
*mad_send_wc
)
919 struct ib_sa_query
*query
= mad_send_wc
->send_buf
->context
[0];
923 switch (mad_send_wc
->status
) {
925 /* No callback -- already got recv */
927 case IB_WC_RESP_TIMEOUT_ERR
:
928 query
->callback(query
, -ETIMEDOUT
, NULL
);
930 case IB_WC_WR_FLUSH_ERR
:
931 query
->callback(query
, -EINTR
, NULL
);
934 query
->callback(query
, -EIO
, NULL
);
938 spin_lock_irqsave(&idr_lock
, flags
);
939 idr_remove(&query_idr
, query
->id
);
940 spin_unlock_irqrestore(&idr_lock
, flags
);
942 ib_free_send_mad(mad_send_wc
->send_buf
);
943 kref_put(&query
->sm_ah
->ref
, free_sm_ah
);
944 ib_sa_client_put(query
->client
);
945 query
->release(query
);
948 static void recv_handler(struct ib_mad_agent
*mad_agent
,
949 struct ib_mad_recv_wc
*mad_recv_wc
)
951 struct ib_sa_query
*query
;
952 struct ib_mad_send_buf
*mad_buf
;
954 mad_buf
= (void *) (unsigned long) mad_recv_wc
->wc
->wr_id
;
955 query
= mad_buf
->context
[0];
957 if (query
->callback
) {
958 if (mad_recv_wc
->wc
->status
== IB_WC_SUCCESS
)
959 query
->callback(query
,
960 mad_recv_wc
->recv_buf
.mad
->mad_hdr
.status
?
962 (struct ib_sa_mad
*) mad_recv_wc
->recv_buf
.mad
);
964 query
->callback(query
, -EIO
, NULL
);
967 ib_free_recv_mad(mad_recv_wc
);
970 static void ib_sa_add_one(struct ib_device
*device
)
972 struct ib_sa_device
*sa_dev
;
975 if (rdma_node_get_transport(device
->node_type
) != RDMA_TRANSPORT_IB
)
978 if (device
->node_type
== RDMA_NODE_IB_SWITCH
)
982 e
= device
->phys_port_cnt
;
985 sa_dev
= kmalloc(sizeof *sa_dev
+
986 (e
- s
+ 1) * sizeof (struct ib_sa_port
),
991 sa_dev
->start_port
= s
;
992 sa_dev
->end_port
= e
;
994 for (i
= 0; i
<= e
- s
; ++i
) {
995 sa_dev
->port
[i
].sm_ah
= NULL
;
996 sa_dev
->port
[i
].port_num
= i
+ s
;
997 spin_lock_init(&sa_dev
->port
[i
].ah_lock
);
999 sa_dev
->port
[i
].agent
=
1000 ib_register_mad_agent(device
, i
+ s
, IB_QPT_GSI
,
1001 NULL
, 0, send_handler
,
1002 recv_handler
, sa_dev
);
1003 if (IS_ERR(sa_dev
->port
[i
].agent
))
1006 INIT_WORK(&sa_dev
->port
[i
].update_task
, update_sm_ah
);
1009 ib_set_client_data(device
, &sa_client
, sa_dev
);
1012 * We register our event handler after everything is set up,
1013 * and then update our cached info after the event handler is
1014 * registered to avoid any problems if a port changes state
1015 * during our initialization.
1018 INIT_IB_EVENT_HANDLER(&sa_dev
->event_handler
, device
, ib_sa_event
);
1019 if (ib_register_event_handler(&sa_dev
->event_handler
))
1022 for (i
= 0; i
<= e
- s
; ++i
)
1023 update_sm_ah(&sa_dev
->port
[i
].update_task
);
1029 ib_unregister_mad_agent(sa_dev
->port
[i
].agent
);
1036 static void ib_sa_remove_one(struct ib_device
*device
)
1038 struct ib_sa_device
*sa_dev
= ib_get_client_data(device
, &sa_client
);
1044 ib_unregister_event_handler(&sa_dev
->event_handler
);
1046 flush_scheduled_work();
1048 for (i
= 0; i
<= sa_dev
->end_port
- sa_dev
->start_port
; ++i
) {
1049 ib_unregister_mad_agent(sa_dev
->port
[i
].agent
);
1050 kref_put(&sa_dev
->port
[i
].sm_ah
->ref
, free_sm_ah
);
1056 static int __init
ib_sa_init(void)
1060 spin_lock_init(&idr_lock
);
1061 spin_lock_init(&tid_lock
);
1063 get_random_bytes(&tid
, sizeof tid
);
1065 ret
= ib_register_client(&sa_client
);
1067 printk(KERN_ERR
"Couldn't register ib_sa client\n");
1073 printk(KERN_ERR
"Couldn't initialize multicast handling\n");
1079 ib_unregister_client(&sa_client
);
1084 static void __exit
ib_sa_cleanup(void)
1087 ib_unregister_client(&sa_client
);
1088 idr_destroy(&query_idr
);
1091 module_init(ib_sa_init
);
1092 module_exit(ib_sa_cleanup
);