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");
64 struct ib_mad_agent
*agent
;
65 struct ib_sa_sm_ah
*sm_ah
;
66 struct work_struct update_task
;
72 int start_port
, end_port
;
73 struct ib_event_handler event_handler
;
74 struct ib_sa_port port
[0];
78 void (*callback
)(struct ib_sa_query
*, int, struct ib_sa_mad
*);
79 void (*release
)(struct ib_sa_query
*);
80 struct ib_sa_client
*client
;
81 struct ib_sa_port
*port
;
82 struct ib_mad_send_buf
*mad_buf
;
83 struct ib_sa_sm_ah
*sm_ah
;
87 struct ib_sa_service_query
{
88 void (*callback
)(int, struct ib_sa_service_rec
*, void *);
90 struct ib_sa_query sa_query
;
93 struct ib_sa_path_query
{
94 void (*callback
)(int, struct ib_sa_path_rec
*, void *);
96 struct ib_sa_query sa_query
;
99 struct ib_sa_mcmember_query
{
100 void (*callback
)(int, struct ib_sa_mcmember_rec
*, void *);
102 struct ib_sa_query sa_query
;
105 static void ib_sa_add_one(struct ib_device
*device
);
106 static void ib_sa_remove_one(struct ib_device
*device
);
108 static struct ib_client sa_client
= {
110 .add
= ib_sa_add_one
,
111 .remove
= ib_sa_remove_one
114 static spinlock_t idr_lock
;
115 static DEFINE_IDR(query_idr
);
117 static spinlock_t tid_lock
;
120 #define PATH_REC_FIELD(field) \
121 .struct_offset_bytes = offsetof(struct ib_sa_path_rec, field), \
122 .struct_size_bytes = sizeof ((struct ib_sa_path_rec *) 0)->field, \
123 .field_name = "sa_path_rec:" #field
125 static const struct ib_field path_rec_table
[] = {
126 { PATH_REC_FIELD(service_id
),
130 { PATH_REC_FIELD(dgid
),
134 { PATH_REC_FIELD(sgid
),
138 { PATH_REC_FIELD(dlid
),
142 { PATH_REC_FIELD(slid
),
146 { PATH_REC_FIELD(raw_traffic
),
154 { PATH_REC_FIELD(flow_label
),
158 { PATH_REC_FIELD(hop_limit
),
162 { PATH_REC_FIELD(traffic_class
),
166 { PATH_REC_FIELD(reversible
),
170 { PATH_REC_FIELD(numb_path
),
174 { PATH_REC_FIELD(pkey
),
178 { PATH_REC_FIELD(qos_class
),
182 { PATH_REC_FIELD(sl
),
186 { PATH_REC_FIELD(mtu_selector
),
190 { PATH_REC_FIELD(mtu
),
194 { PATH_REC_FIELD(rate_selector
),
198 { PATH_REC_FIELD(rate
),
202 { PATH_REC_FIELD(packet_life_time_selector
),
206 { PATH_REC_FIELD(packet_life_time
),
210 { PATH_REC_FIELD(preference
),
220 #define MCMEMBER_REC_FIELD(field) \
221 .struct_offset_bytes = offsetof(struct ib_sa_mcmember_rec, field), \
222 .struct_size_bytes = sizeof ((struct ib_sa_mcmember_rec *) 0)->field, \
223 .field_name = "sa_mcmember_rec:" #field
225 static const struct ib_field mcmember_rec_table
[] = {
226 { MCMEMBER_REC_FIELD(mgid
),
230 { MCMEMBER_REC_FIELD(port_gid
),
234 { MCMEMBER_REC_FIELD(qkey
),
238 { MCMEMBER_REC_FIELD(mlid
),
242 { MCMEMBER_REC_FIELD(mtu_selector
),
246 { MCMEMBER_REC_FIELD(mtu
),
250 { MCMEMBER_REC_FIELD(traffic_class
),
254 { MCMEMBER_REC_FIELD(pkey
),
258 { MCMEMBER_REC_FIELD(rate_selector
),
262 { MCMEMBER_REC_FIELD(rate
),
266 { MCMEMBER_REC_FIELD(packet_life_time_selector
),
270 { MCMEMBER_REC_FIELD(packet_life_time
),
274 { MCMEMBER_REC_FIELD(sl
),
278 { MCMEMBER_REC_FIELD(flow_label
),
282 { MCMEMBER_REC_FIELD(hop_limit
),
286 { MCMEMBER_REC_FIELD(scope
),
290 { MCMEMBER_REC_FIELD(join_state
),
294 { MCMEMBER_REC_FIELD(proxy_join
),
304 #define SERVICE_REC_FIELD(field) \
305 .struct_offset_bytes = offsetof(struct ib_sa_service_rec, field), \
306 .struct_size_bytes = sizeof ((struct ib_sa_service_rec *) 0)->field, \
307 .field_name = "sa_service_rec:" #field
309 static const struct ib_field service_rec_table
[] = {
310 { SERVICE_REC_FIELD(id
),
314 { SERVICE_REC_FIELD(gid
),
318 { SERVICE_REC_FIELD(pkey
),
322 { SERVICE_REC_FIELD(lease
),
326 { SERVICE_REC_FIELD(key
),
330 { SERVICE_REC_FIELD(name
),
334 { SERVICE_REC_FIELD(data8
),
338 { SERVICE_REC_FIELD(data16
),
342 { SERVICE_REC_FIELD(data32
),
346 { SERVICE_REC_FIELD(data64
),
352 static void free_sm_ah(struct kref
*kref
)
354 struct ib_sa_sm_ah
*sm_ah
= container_of(kref
, struct ib_sa_sm_ah
, ref
);
356 ib_destroy_ah(sm_ah
->ah
);
360 static void update_sm_ah(struct work_struct
*work
)
362 struct ib_sa_port
*port
=
363 container_of(work
, struct ib_sa_port
, update_task
);
364 struct ib_sa_sm_ah
*new_ah
, *old_ah
;
365 struct ib_port_attr port_attr
;
366 struct ib_ah_attr ah_attr
;
368 if (ib_query_port(port
->agent
->device
, port
->port_num
, &port_attr
)) {
369 printk(KERN_WARNING
"Couldn't query port\n");
373 new_ah
= kmalloc(sizeof *new_ah
, GFP_KERNEL
);
375 printk(KERN_WARNING
"Couldn't allocate new SM AH\n");
379 kref_init(&new_ah
->ref
);
380 new_ah
->src_path_mask
= (1 << port_attr
.lmc
) - 1;
382 new_ah
->pkey_index
= 0;
383 if (ib_find_pkey(port
->agent
->device
, port
->port_num
,
384 IB_DEFAULT_PKEY_FULL
, &new_ah
->pkey_index
))
385 printk(KERN_ERR
"Couldn't find index for default PKey\n");
387 memset(&ah_attr
, 0, sizeof ah_attr
);
388 ah_attr
.dlid
= port_attr
.sm_lid
;
389 ah_attr
.sl
= port_attr
.sm_sl
;
390 ah_attr
.port_num
= port
->port_num
;
392 new_ah
->ah
= ib_create_ah(port
->agent
->qp
->pd
, &ah_attr
);
393 if (IS_ERR(new_ah
->ah
)) {
394 printk(KERN_WARNING
"Couldn't create new SM AH\n");
399 spin_lock_irq(&port
->ah_lock
);
400 old_ah
= port
->sm_ah
;
401 port
->sm_ah
= new_ah
;
402 spin_unlock_irq(&port
->ah_lock
);
405 kref_put(&old_ah
->ref
, free_sm_ah
);
408 static void ib_sa_event(struct ib_event_handler
*handler
, struct ib_event
*event
)
410 if (event
->event
== IB_EVENT_PORT_ERR
||
411 event
->event
== IB_EVENT_PORT_ACTIVE
||
412 event
->event
== IB_EVENT_LID_CHANGE
||
413 event
->event
== IB_EVENT_PKEY_CHANGE
||
414 event
->event
== IB_EVENT_SM_CHANGE
||
415 event
->event
== IB_EVENT_CLIENT_REREGISTER
) {
416 struct ib_sa_device
*sa_dev
;
417 sa_dev
= container_of(handler
, typeof(*sa_dev
), event_handler
);
419 schedule_work(&sa_dev
->port
[event
->element
.port_num
-
420 sa_dev
->start_port
].update_task
);
424 void ib_sa_register_client(struct ib_sa_client
*client
)
426 atomic_set(&client
->users
, 1);
427 init_completion(&client
->comp
);
429 EXPORT_SYMBOL(ib_sa_register_client
);
431 void ib_sa_unregister_client(struct ib_sa_client
*client
)
433 ib_sa_client_put(client
);
434 wait_for_completion(&client
->comp
);
436 EXPORT_SYMBOL(ib_sa_unregister_client
);
439 * ib_sa_cancel_query - try to cancel an SA query
440 * @id:ID of query to cancel
441 * @query:query pointer to cancel
443 * Try to cancel an SA query. If the id and query don't match up or
444 * the query has already completed, nothing is done. Otherwise the
445 * query is canceled and will complete with a status of -EINTR.
447 void ib_sa_cancel_query(int id
, struct ib_sa_query
*query
)
450 struct ib_mad_agent
*agent
;
451 struct ib_mad_send_buf
*mad_buf
;
453 spin_lock_irqsave(&idr_lock
, flags
);
454 if (idr_find(&query_idr
, id
) != query
) {
455 spin_unlock_irqrestore(&idr_lock
, flags
);
458 agent
= query
->port
->agent
;
459 mad_buf
= query
->mad_buf
;
460 spin_unlock_irqrestore(&idr_lock
, flags
);
462 ib_cancel_mad(agent
, mad_buf
);
464 EXPORT_SYMBOL(ib_sa_cancel_query
);
466 static u8
get_src_path_mask(struct ib_device
*device
, u8 port_num
)
468 struct ib_sa_device
*sa_dev
;
469 struct ib_sa_port
*port
;
473 sa_dev
= ib_get_client_data(device
, &sa_client
);
477 port
= &sa_dev
->port
[port_num
- sa_dev
->start_port
];
478 spin_lock_irqsave(&port
->ah_lock
, flags
);
479 src_path_mask
= port
->sm_ah
? port
->sm_ah
->src_path_mask
: 0x7f;
480 spin_unlock_irqrestore(&port
->ah_lock
, flags
);
482 return src_path_mask
;
485 int ib_init_ah_from_path(struct ib_device
*device
, u8 port_num
,
486 struct ib_sa_path_rec
*rec
, struct ib_ah_attr
*ah_attr
)
491 memset(ah_attr
, 0, sizeof *ah_attr
);
492 ah_attr
->dlid
= be16_to_cpu(rec
->dlid
);
493 ah_attr
->sl
= rec
->sl
;
494 ah_attr
->src_path_bits
= be16_to_cpu(rec
->slid
) &
495 get_src_path_mask(device
, port_num
);
496 ah_attr
->port_num
= port_num
;
497 ah_attr
->static_rate
= rec
->rate
;
499 if (rec
->hop_limit
> 1) {
500 ah_attr
->ah_flags
= IB_AH_GRH
;
501 ah_attr
->grh
.dgid
= rec
->dgid
;
503 ret
= ib_find_cached_gid(device
, &rec
->sgid
, &port_num
,
508 ah_attr
->grh
.sgid_index
= gid_index
;
509 ah_attr
->grh
.flow_label
= be32_to_cpu(rec
->flow_label
);
510 ah_attr
->grh
.hop_limit
= rec
->hop_limit
;
511 ah_attr
->grh
.traffic_class
= rec
->traffic_class
;
515 EXPORT_SYMBOL(ib_init_ah_from_path
);
517 static int alloc_mad(struct ib_sa_query
*query
, gfp_t gfp_mask
)
521 spin_lock_irqsave(&query
->port
->ah_lock
, flags
);
522 kref_get(&query
->port
->sm_ah
->ref
);
523 query
->sm_ah
= query
->port
->sm_ah
;
524 spin_unlock_irqrestore(&query
->port
->ah_lock
, flags
);
526 query
->mad_buf
= ib_create_send_mad(query
->port
->agent
, 1,
527 query
->sm_ah
->pkey_index
,
528 0, IB_MGMT_SA_HDR
, IB_MGMT_SA_DATA
,
530 if (IS_ERR(query
->mad_buf
)) {
531 kref_put(&query
->sm_ah
->ref
, free_sm_ah
);
535 query
->mad_buf
->ah
= query
->sm_ah
->ah
;
540 static void free_mad(struct ib_sa_query
*query
)
542 ib_free_send_mad(query
->mad_buf
);
543 kref_put(&query
->sm_ah
->ref
, free_sm_ah
);
546 static void init_mad(struct ib_sa_mad
*mad
, struct ib_mad_agent
*agent
)
550 memset(mad
, 0, sizeof *mad
);
552 mad
->mad_hdr
.base_version
= IB_MGMT_BASE_VERSION
;
553 mad
->mad_hdr
.mgmt_class
= IB_MGMT_CLASS_SUBN_ADM
;
554 mad
->mad_hdr
.class_version
= IB_SA_CLASS_VERSION
;
556 spin_lock_irqsave(&tid_lock
, flags
);
558 cpu_to_be64(((u64
) agent
->hi_tid
) << 32 | tid
++);
559 spin_unlock_irqrestore(&tid_lock
, flags
);
562 static int send_mad(struct ib_sa_query
*query
, int timeout_ms
, gfp_t gfp_mask
)
568 if (!idr_pre_get(&query_idr
, gfp_mask
))
570 spin_lock_irqsave(&idr_lock
, flags
);
571 ret
= idr_get_new(&query_idr
, query
, &id
);
572 spin_unlock_irqrestore(&idr_lock
, flags
);
578 query
->mad_buf
->timeout_ms
= timeout_ms
;
579 query
->mad_buf
->context
[0] = query
;
582 ret
= ib_post_send_mad(query
->mad_buf
, NULL
);
584 spin_lock_irqsave(&idr_lock
, flags
);
585 idr_remove(&query_idr
, id
);
586 spin_unlock_irqrestore(&idr_lock
, flags
);
590 * It's not safe to dereference query any more, because the
591 * send may already have completed and freed the query in
594 return ret
? ret
: id
;
597 static void ib_sa_path_rec_callback(struct ib_sa_query
*sa_query
,
599 struct ib_sa_mad
*mad
)
601 struct ib_sa_path_query
*query
=
602 container_of(sa_query
, struct ib_sa_path_query
, sa_query
);
605 struct ib_sa_path_rec rec
;
607 ib_unpack(path_rec_table
, ARRAY_SIZE(path_rec_table
),
609 query
->callback(status
, &rec
, query
->context
);
611 query
->callback(status
, NULL
, query
->context
);
614 static void ib_sa_path_rec_release(struct ib_sa_query
*sa_query
)
616 kfree(container_of(sa_query
, struct ib_sa_path_query
, sa_query
));
620 * ib_sa_path_rec_get - Start a Path get query
622 * @device:device to send query on
623 * @port_num: port number to send query on
624 * @rec:Path Record to send in query
625 * @comp_mask:component mask to send in query
626 * @timeout_ms:time to wait for response
627 * @gfp_mask:GFP mask to use for internal allocations
628 * @callback:function called when query completes, times out or is
630 * @context:opaque user context passed to callback
631 * @sa_query:query context, used to cancel query
633 * Send a Path Record Get query to the SA to look up a path. The
634 * callback function will be called when the query completes (or
635 * fails); status is 0 for a successful response, -EINTR if the query
636 * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
637 * occurred sending the query. The resp parameter of the callback is
638 * only valid if status is 0.
640 * If the return value of ib_sa_path_rec_get() is negative, it is an
641 * error code. Otherwise it is a query ID that can be used to cancel
644 int ib_sa_path_rec_get(struct ib_sa_client
*client
,
645 struct ib_device
*device
, u8 port_num
,
646 struct ib_sa_path_rec
*rec
,
647 ib_sa_comp_mask comp_mask
,
648 int timeout_ms
, gfp_t gfp_mask
,
649 void (*callback
)(int status
,
650 struct ib_sa_path_rec
*resp
,
653 struct ib_sa_query
**sa_query
)
655 struct ib_sa_path_query
*query
;
656 struct ib_sa_device
*sa_dev
= ib_get_client_data(device
, &sa_client
);
657 struct ib_sa_port
*port
;
658 struct ib_mad_agent
*agent
;
659 struct ib_sa_mad
*mad
;
665 port
= &sa_dev
->port
[port_num
- sa_dev
->start_port
];
668 query
= kmalloc(sizeof *query
, gfp_mask
);
672 query
->sa_query
.port
= port
;
673 ret
= alloc_mad(&query
->sa_query
, gfp_mask
);
677 ib_sa_client_get(client
);
678 query
->sa_query
.client
= client
;
679 query
->callback
= callback
;
680 query
->context
= context
;
682 mad
= query
->sa_query
.mad_buf
->mad
;
683 init_mad(mad
, agent
);
685 query
->sa_query
.callback
= callback
? ib_sa_path_rec_callback
: NULL
;
686 query
->sa_query
.release
= ib_sa_path_rec_release
;
687 mad
->mad_hdr
.method
= IB_MGMT_METHOD_GET
;
688 mad
->mad_hdr
.attr_id
= cpu_to_be16(IB_SA_ATTR_PATH_REC
);
689 mad
->sa_hdr
.comp_mask
= comp_mask
;
691 ib_pack(path_rec_table
, ARRAY_SIZE(path_rec_table
), rec
, mad
->data
);
693 *sa_query
= &query
->sa_query
;
695 ret
= send_mad(&query
->sa_query
, timeout_ms
, gfp_mask
);
703 ib_sa_client_put(query
->sa_query
.client
);
704 free_mad(&query
->sa_query
);
710 EXPORT_SYMBOL(ib_sa_path_rec_get
);
712 static void ib_sa_service_rec_callback(struct ib_sa_query
*sa_query
,
714 struct ib_sa_mad
*mad
)
716 struct ib_sa_service_query
*query
=
717 container_of(sa_query
, struct ib_sa_service_query
, sa_query
);
720 struct ib_sa_service_rec rec
;
722 ib_unpack(service_rec_table
, ARRAY_SIZE(service_rec_table
),
724 query
->callback(status
, &rec
, query
->context
);
726 query
->callback(status
, NULL
, query
->context
);
729 static void ib_sa_service_rec_release(struct ib_sa_query
*sa_query
)
731 kfree(container_of(sa_query
, struct ib_sa_service_query
, sa_query
));
735 * ib_sa_service_rec_query - Start Service Record operation
737 * @device:device to send request on
738 * @port_num: port number to send request on
739 * @method:SA method - should be get, set, or delete
740 * @rec:Service Record to send in request
741 * @comp_mask:component mask to send in request
742 * @timeout_ms:time to wait for response
743 * @gfp_mask:GFP mask to use for internal allocations
744 * @callback:function called when request completes, times out or is
746 * @context:opaque user context passed to callback
747 * @sa_query:request context, used to cancel request
749 * Send a Service Record set/get/delete to the SA to register,
750 * unregister or query a service record.
751 * The callback function will be called when the request completes (or
752 * fails); status is 0 for a successful response, -EINTR if the query
753 * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
754 * occurred sending the query. The resp parameter of the callback is
755 * only valid if status is 0.
757 * If the return value of ib_sa_service_rec_query() is negative, it is an
758 * error code. Otherwise it is a request ID that can be used to cancel
761 int ib_sa_service_rec_query(struct ib_sa_client
*client
,
762 struct ib_device
*device
, u8 port_num
, u8 method
,
763 struct ib_sa_service_rec
*rec
,
764 ib_sa_comp_mask comp_mask
,
765 int timeout_ms
, gfp_t gfp_mask
,
766 void (*callback
)(int status
,
767 struct ib_sa_service_rec
*resp
,
770 struct ib_sa_query
**sa_query
)
772 struct ib_sa_service_query
*query
;
773 struct ib_sa_device
*sa_dev
= ib_get_client_data(device
, &sa_client
);
774 struct ib_sa_port
*port
;
775 struct ib_mad_agent
*agent
;
776 struct ib_sa_mad
*mad
;
782 port
= &sa_dev
->port
[port_num
- sa_dev
->start_port
];
785 if (method
!= IB_MGMT_METHOD_GET
&&
786 method
!= IB_MGMT_METHOD_SET
&&
787 method
!= IB_SA_METHOD_DELETE
)
790 query
= kmalloc(sizeof *query
, gfp_mask
);
794 query
->sa_query
.port
= port
;
795 ret
= alloc_mad(&query
->sa_query
, gfp_mask
);
799 ib_sa_client_get(client
);
800 query
->sa_query
.client
= client
;
801 query
->callback
= callback
;
802 query
->context
= context
;
804 mad
= query
->sa_query
.mad_buf
->mad
;
805 init_mad(mad
, agent
);
807 query
->sa_query
.callback
= callback
? ib_sa_service_rec_callback
: NULL
;
808 query
->sa_query
.release
= ib_sa_service_rec_release
;
809 mad
->mad_hdr
.method
= method
;
810 mad
->mad_hdr
.attr_id
= cpu_to_be16(IB_SA_ATTR_SERVICE_REC
);
811 mad
->sa_hdr
.comp_mask
= comp_mask
;
813 ib_pack(service_rec_table
, ARRAY_SIZE(service_rec_table
),
816 *sa_query
= &query
->sa_query
;
818 ret
= send_mad(&query
->sa_query
, timeout_ms
, gfp_mask
);
826 ib_sa_client_put(query
->sa_query
.client
);
827 free_mad(&query
->sa_query
);
833 EXPORT_SYMBOL(ib_sa_service_rec_query
);
835 static void ib_sa_mcmember_rec_callback(struct ib_sa_query
*sa_query
,
837 struct ib_sa_mad
*mad
)
839 struct ib_sa_mcmember_query
*query
=
840 container_of(sa_query
, struct ib_sa_mcmember_query
, sa_query
);
843 struct ib_sa_mcmember_rec rec
;
845 ib_unpack(mcmember_rec_table
, ARRAY_SIZE(mcmember_rec_table
),
847 query
->callback(status
, &rec
, query
->context
);
849 query
->callback(status
, NULL
, query
->context
);
852 static void ib_sa_mcmember_rec_release(struct ib_sa_query
*sa_query
)
854 kfree(container_of(sa_query
, struct ib_sa_mcmember_query
, sa_query
));
857 int ib_sa_mcmember_rec_query(struct ib_sa_client
*client
,
858 struct ib_device
*device
, u8 port_num
,
860 struct ib_sa_mcmember_rec
*rec
,
861 ib_sa_comp_mask comp_mask
,
862 int timeout_ms
, gfp_t gfp_mask
,
863 void (*callback
)(int status
,
864 struct ib_sa_mcmember_rec
*resp
,
867 struct ib_sa_query
**sa_query
)
869 struct ib_sa_mcmember_query
*query
;
870 struct ib_sa_device
*sa_dev
= ib_get_client_data(device
, &sa_client
);
871 struct ib_sa_port
*port
;
872 struct ib_mad_agent
*agent
;
873 struct ib_sa_mad
*mad
;
879 port
= &sa_dev
->port
[port_num
- sa_dev
->start_port
];
882 query
= kmalloc(sizeof *query
, gfp_mask
);
886 query
->sa_query
.port
= port
;
887 ret
= alloc_mad(&query
->sa_query
, gfp_mask
);
891 ib_sa_client_get(client
);
892 query
->sa_query
.client
= client
;
893 query
->callback
= callback
;
894 query
->context
= context
;
896 mad
= query
->sa_query
.mad_buf
->mad
;
897 init_mad(mad
, agent
);
899 query
->sa_query
.callback
= callback
? ib_sa_mcmember_rec_callback
: NULL
;
900 query
->sa_query
.release
= ib_sa_mcmember_rec_release
;
901 mad
->mad_hdr
.method
= method
;
902 mad
->mad_hdr
.attr_id
= cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC
);
903 mad
->sa_hdr
.comp_mask
= comp_mask
;
905 ib_pack(mcmember_rec_table
, ARRAY_SIZE(mcmember_rec_table
),
908 *sa_query
= &query
->sa_query
;
910 ret
= send_mad(&query
->sa_query
, timeout_ms
, gfp_mask
);
918 ib_sa_client_put(query
->sa_query
.client
);
919 free_mad(&query
->sa_query
);
926 static void send_handler(struct ib_mad_agent
*agent
,
927 struct ib_mad_send_wc
*mad_send_wc
)
929 struct ib_sa_query
*query
= mad_send_wc
->send_buf
->context
[0];
933 switch (mad_send_wc
->status
) {
935 /* No callback -- already got recv */
937 case IB_WC_RESP_TIMEOUT_ERR
:
938 query
->callback(query
, -ETIMEDOUT
, NULL
);
940 case IB_WC_WR_FLUSH_ERR
:
941 query
->callback(query
, -EINTR
, NULL
);
944 query
->callback(query
, -EIO
, NULL
);
948 spin_lock_irqsave(&idr_lock
, flags
);
949 idr_remove(&query_idr
, query
->id
);
950 spin_unlock_irqrestore(&idr_lock
, flags
);
953 ib_sa_client_put(query
->client
);
954 query
->release(query
);
957 static void recv_handler(struct ib_mad_agent
*mad_agent
,
958 struct ib_mad_recv_wc
*mad_recv_wc
)
960 struct ib_sa_query
*query
;
961 struct ib_mad_send_buf
*mad_buf
;
963 mad_buf
= (void *) (unsigned long) mad_recv_wc
->wc
->wr_id
;
964 query
= mad_buf
->context
[0];
966 if (query
->callback
) {
967 if (mad_recv_wc
->wc
->status
== IB_WC_SUCCESS
)
968 query
->callback(query
,
969 mad_recv_wc
->recv_buf
.mad
->mad_hdr
.status
?
971 (struct ib_sa_mad
*) mad_recv_wc
->recv_buf
.mad
);
973 query
->callback(query
, -EIO
, NULL
);
976 ib_free_recv_mad(mad_recv_wc
);
979 static void ib_sa_add_one(struct ib_device
*device
)
981 struct ib_sa_device
*sa_dev
;
984 if (rdma_node_get_transport(device
->node_type
) != RDMA_TRANSPORT_IB
)
987 if (device
->node_type
== RDMA_NODE_IB_SWITCH
)
991 e
= device
->phys_port_cnt
;
994 sa_dev
= kmalloc(sizeof *sa_dev
+
995 (e
- s
+ 1) * sizeof (struct ib_sa_port
),
1000 sa_dev
->start_port
= s
;
1001 sa_dev
->end_port
= e
;
1003 for (i
= 0; i
<= e
- s
; ++i
) {
1004 sa_dev
->port
[i
].sm_ah
= NULL
;
1005 sa_dev
->port
[i
].port_num
= i
+ s
;
1006 spin_lock_init(&sa_dev
->port
[i
].ah_lock
);
1008 sa_dev
->port
[i
].agent
=
1009 ib_register_mad_agent(device
, i
+ s
, IB_QPT_GSI
,
1010 NULL
, 0, send_handler
,
1011 recv_handler
, sa_dev
);
1012 if (IS_ERR(sa_dev
->port
[i
].agent
))
1015 INIT_WORK(&sa_dev
->port
[i
].update_task
, update_sm_ah
);
1018 ib_set_client_data(device
, &sa_client
, sa_dev
);
1021 * We register our event handler after everything is set up,
1022 * and then update our cached info after the event handler is
1023 * registered to avoid any problems if a port changes state
1024 * during our initialization.
1027 INIT_IB_EVENT_HANDLER(&sa_dev
->event_handler
, device
, ib_sa_event
);
1028 if (ib_register_event_handler(&sa_dev
->event_handler
))
1031 for (i
= 0; i
<= e
- s
; ++i
)
1032 update_sm_ah(&sa_dev
->port
[i
].update_task
);
1038 ib_unregister_mad_agent(sa_dev
->port
[i
].agent
);
1045 static void ib_sa_remove_one(struct ib_device
*device
)
1047 struct ib_sa_device
*sa_dev
= ib_get_client_data(device
, &sa_client
);
1053 ib_unregister_event_handler(&sa_dev
->event_handler
);
1055 flush_scheduled_work();
1057 for (i
= 0; i
<= sa_dev
->end_port
- sa_dev
->start_port
; ++i
) {
1058 ib_unregister_mad_agent(sa_dev
->port
[i
].agent
);
1059 kref_put(&sa_dev
->port
[i
].sm_ah
->ref
, free_sm_ah
);
1065 static int __init
ib_sa_init(void)
1069 spin_lock_init(&idr_lock
);
1070 spin_lock_init(&tid_lock
);
1072 get_random_bytes(&tid
, sizeof tid
);
1074 ret
= ib_register_client(&sa_client
);
1076 printk(KERN_ERR
"Couldn't register ib_sa client\n");
1082 printk(KERN_ERR
"Couldn't initialize multicast handling\n");
1088 ib_unregister_client(&sa_client
);
1093 static void __exit
ib_sa_cleanup(void)
1096 ib_unregister_client(&sa_client
);
1097 idr_destroy(&query_idr
);
1100 module_init(ib_sa_init
);
1101 module_exit(ib_sa_cleanup
);