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
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/err.h>
38 #include <linux/random.h>
39 #include <linux/spinlock.h>
40 #include <linux/slab.h>
41 #include <linux/dma-mapping.h>
42 #include <linux/kref.h>
43 #include <linux/idr.h>
44 #include <linux/workqueue.h>
46 #include <rdma/ib_pack.h>
47 #include <rdma/ib_cache.h>
50 MODULE_AUTHOR("Roland Dreier");
51 MODULE_DESCRIPTION("InfiniBand subnet administration query support");
52 MODULE_LICENSE("Dual BSD/GPL");
62 struct ib_mad_agent
*agent
;
63 struct ib_sa_sm_ah
*sm_ah
;
64 struct work_struct update_task
;
70 int start_port
, end_port
;
71 struct ib_event_handler event_handler
;
72 struct ib_sa_port port
[0];
76 void (*callback
)(struct ib_sa_query
*, int, struct ib_sa_mad
*);
77 void (*release
)(struct ib_sa_query
*);
78 struct ib_sa_client
*client
;
79 struct ib_sa_port
*port
;
80 struct ib_mad_send_buf
*mad_buf
;
81 struct ib_sa_sm_ah
*sm_ah
;
85 struct ib_sa_service_query
{
86 void (*callback
)(int, struct ib_sa_service_rec
*, void *);
88 struct ib_sa_query sa_query
;
91 struct ib_sa_path_query
{
92 void (*callback
)(int, struct ib_sa_path_rec
*, void *);
94 struct ib_sa_query sa_query
;
97 struct ib_sa_mcmember_query
{
98 void (*callback
)(int, struct ib_sa_mcmember_rec
*, void *);
100 struct ib_sa_query sa_query
;
103 static void ib_sa_add_one(struct ib_device
*device
);
104 static void ib_sa_remove_one(struct ib_device
*device
);
106 static struct ib_client sa_client
= {
108 .add
= ib_sa_add_one
,
109 .remove
= ib_sa_remove_one
112 static DEFINE_SPINLOCK(idr_lock
);
113 static DEFINE_IDR(query_idr
);
115 static DEFINE_SPINLOCK(tid_lock
);
118 #define PATH_REC_FIELD(field) \
119 .struct_offset_bytes = offsetof(struct ib_sa_path_rec, field), \
120 .struct_size_bytes = sizeof ((struct ib_sa_path_rec *) 0)->field, \
121 .field_name = "sa_path_rec:" #field
123 static const struct ib_field path_rec_table
[] = {
124 { PATH_REC_FIELD(service_id
),
128 { PATH_REC_FIELD(dgid
),
132 { PATH_REC_FIELD(sgid
),
136 { PATH_REC_FIELD(dlid
),
140 { PATH_REC_FIELD(slid
),
144 { PATH_REC_FIELD(raw_traffic
),
152 { PATH_REC_FIELD(flow_label
),
156 { PATH_REC_FIELD(hop_limit
),
160 { PATH_REC_FIELD(traffic_class
),
164 { PATH_REC_FIELD(reversible
),
168 { PATH_REC_FIELD(numb_path
),
172 { PATH_REC_FIELD(pkey
),
176 { PATH_REC_FIELD(qos_class
),
180 { PATH_REC_FIELD(sl
),
184 { PATH_REC_FIELD(mtu_selector
),
188 { PATH_REC_FIELD(mtu
),
192 { PATH_REC_FIELD(rate_selector
),
196 { PATH_REC_FIELD(rate
),
200 { PATH_REC_FIELD(packet_life_time_selector
),
204 { PATH_REC_FIELD(packet_life_time
),
208 { PATH_REC_FIELD(preference
),
218 #define MCMEMBER_REC_FIELD(field) \
219 .struct_offset_bytes = offsetof(struct ib_sa_mcmember_rec, field), \
220 .struct_size_bytes = sizeof ((struct ib_sa_mcmember_rec *) 0)->field, \
221 .field_name = "sa_mcmember_rec:" #field
223 static const struct ib_field mcmember_rec_table
[] = {
224 { MCMEMBER_REC_FIELD(mgid
),
228 { MCMEMBER_REC_FIELD(port_gid
),
232 { MCMEMBER_REC_FIELD(qkey
),
236 { MCMEMBER_REC_FIELD(mlid
),
240 { MCMEMBER_REC_FIELD(mtu_selector
),
244 { MCMEMBER_REC_FIELD(mtu
),
248 { MCMEMBER_REC_FIELD(traffic_class
),
252 { MCMEMBER_REC_FIELD(pkey
),
256 { MCMEMBER_REC_FIELD(rate_selector
),
260 { MCMEMBER_REC_FIELD(rate
),
264 { MCMEMBER_REC_FIELD(packet_life_time_selector
),
268 { MCMEMBER_REC_FIELD(packet_life_time
),
272 { MCMEMBER_REC_FIELD(sl
),
276 { MCMEMBER_REC_FIELD(flow_label
),
280 { MCMEMBER_REC_FIELD(hop_limit
),
284 { MCMEMBER_REC_FIELD(scope
),
288 { MCMEMBER_REC_FIELD(join_state
),
292 { MCMEMBER_REC_FIELD(proxy_join
),
302 #define SERVICE_REC_FIELD(field) \
303 .struct_offset_bytes = offsetof(struct ib_sa_service_rec, field), \
304 .struct_size_bytes = sizeof ((struct ib_sa_service_rec *) 0)->field, \
305 .field_name = "sa_service_rec:" #field
307 static const struct ib_field service_rec_table
[] = {
308 { SERVICE_REC_FIELD(id
),
312 { SERVICE_REC_FIELD(gid
),
316 { SERVICE_REC_FIELD(pkey
),
320 { SERVICE_REC_FIELD(lease
),
324 { SERVICE_REC_FIELD(key
),
328 { SERVICE_REC_FIELD(name
),
332 { SERVICE_REC_FIELD(data8
),
336 { SERVICE_REC_FIELD(data16
),
340 { SERVICE_REC_FIELD(data32
),
344 { SERVICE_REC_FIELD(data64
),
350 static void free_sm_ah(struct kref
*kref
)
352 struct ib_sa_sm_ah
*sm_ah
= container_of(kref
, struct ib_sa_sm_ah
, ref
);
354 ib_destroy_ah(sm_ah
->ah
);
358 static void update_sm_ah(struct work_struct
*work
)
360 struct ib_sa_port
*port
=
361 container_of(work
, struct ib_sa_port
, update_task
);
362 struct ib_sa_sm_ah
*new_ah
;
363 struct ib_port_attr port_attr
;
364 struct ib_ah_attr ah_attr
;
366 if (ib_query_port(port
->agent
->device
, port
->port_num
, &port_attr
)) {
367 printk(KERN_WARNING
"Couldn't query port\n");
371 new_ah
= kmalloc(sizeof *new_ah
, GFP_KERNEL
);
373 printk(KERN_WARNING
"Couldn't allocate new SM AH\n");
377 kref_init(&new_ah
->ref
);
378 new_ah
->src_path_mask
= (1 << port_attr
.lmc
) - 1;
380 new_ah
->pkey_index
= 0;
381 if (ib_find_pkey(port
->agent
->device
, port
->port_num
,
382 IB_DEFAULT_PKEY_FULL
, &new_ah
->pkey_index
))
383 printk(KERN_ERR
"Couldn't find index for default PKey\n");
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
);
399 kref_put(&port
->sm_ah
->ref
, free_sm_ah
);
400 port
->sm_ah
= new_ah
;
401 spin_unlock_irq(&port
->ah_lock
);
405 static void ib_sa_event(struct ib_event_handler
*handler
, struct ib_event
*event
)
407 if (event
->event
== IB_EVENT_PORT_ERR
||
408 event
->event
== IB_EVENT_PORT_ACTIVE
||
409 event
->event
== IB_EVENT_LID_CHANGE
||
410 event
->event
== IB_EVENT_PKEY_CHANGE
||
411 event
->event
== IB_EVENT_SM_CHANGE
||
412 event
->event
== IB_EVENT_CLIENT_REREGISTER
) {
414 struct ib_sa_device
*sa_dev
=
415 container_of(handler
, typeof(*sa_dev
), event_handler
);
416 struct ib_sa_port
*port
=
417 &sa_dev
->port
[event
->element
.port_num
- sa_dev
->start_port
];
419 if (rdma_port_get_link_layer(handler
->device
, port
->port_num
) != IB_LINK_LAYER_INFINIBAND
)
422 spin_lock_irqsave(&port
->ah_lock
, flags
);
424 kref_put(&port
->sm_ah
->ref
, free_sm_ah
);
426 spin_unlock_irqrestore(&port
->ah_lock
, flags
);
428 queue_work(ib_wq
, &sa_dev
->port
[event
->element
.port_num
-
429 sa_dev
->start_port
].update_task
);
433 void ib_sa_register_client(struct ib_sa_client
*client
)
435 atomic_set(&client
->users
, 1);
436 init_completion(&client
->comp
);
438 EXPORT_SYMBOL(ib_sa_register_client
);
440 void ib_sa_unregister_client(struct ib_sa_client
*client
)
442 ib_sa_client_put(client
);
443 wait_for_completion(&client
->comp
);
445 EXPORT_SYMBOL(ib_sa_unregister_client
);
448 * ib_sa_cancel_query - try to cancel an SA query
449 * @id:ID of query to cancel
450 * @query:query pointer to cancel
452 * Try to cancel an SA query. If the id and query don't match up or
453 * the query has already completed, nothing is done. Otherwise the
454 * query is canceled and will complete with a status of -EINTR.
456 void ib_sa_cancel_query(int id
, struct ib_sa_query
*query
)
459 struct ib_mad_agent
*agent
;
460 struct ib_mad_send_buf
*mad_buf
;
462 spin_lock_irqsave(&idr_lock
, flags
);
463 if (idr_find(&query_idr
, id
) != query
) {
464 spin_unlock_irqrestore(&idr_lock
, flags
);
467 agent
= query
->port
->agent
;
468 mad_buf
= query
->mad_buf
;
469 spin_unlock_irqrestore(&idr_lock
, flags
);
471 ib_cancel_mad(agent
, mad_buf
);
473 EXPORT_SYMBOL(ib_sa_cancel_query
);
475 static u8
get_src_path_mask(struct ib_device
*device
, u8 port_num
)
477 struct ib_sa_device
*sa_dev
;
478 struct ib_sa_port
*port
;
482 sa_dev
= ib_get_client_data(device
, &sa_client
);
486 port
= &sa_dev
->port
[port_num
- sa_dev
->start_port
];
487 spin_lock_irqsave(&port
->ah_lock
, flags
);
488 src_path_mask
= port
->sm_ah
? port
->sm_ah
->src_path_mask
: 0x7f;
489 spin_unlock_irqrestore(&port
->ah_lock
, flags
);
491 return src_path_mask
;
494 int ib_init_ah_from_path(struct ib_device
*device
, u8 port_num
,
495 struct ib_sa_path_rec
*rec
, struct ib_ah_attr
*ah_attr
)
501 memset(ah_attr
, 0, sizeof *ah_attr
);
502 ah_attr
->dlid
= be16_to_cpu(rec
->dlid
);
503 ah_attr
->sl
= rec
->sl
;
504 ah_attr
->src_path_bits
= be16_to_cpu(rec
->slid
) &
505 get_src_path_mask(device
, port_num
);
506 ah_attr
->port_num
= port_num
;
507 ah_attr
->static_rate
= rec
->rate
;
509 force_grh
= rdma_port_get_link_layer(device
, port_num
) == IB_LINK_LAYER_ETHERNET
;
511 if (rec
->hop_limit
> 1 || force_grh
) {
512 ah_attr
->ah_flags
= IB_AH_GRH
;
513 ah_attr
->grh
.dgid
= rec
->dgid
;
515 ret
= ib_find_cached_gid(device
, &rec
->sgid
, &port_num
,
520 ah_attr
->grh
.sgid_index
= gid_index
;
521 ah_attr
->grh
.flow_label
= be32_to_cpu(rec
->flow_label
);
522 ah_attr
->grh
.hop_limit
= rec
->hop_limit
;
523 ah_attr
->grh
.traffic_class
= rec
->traffic_class
;
527 EXPORT_SYMBOL(ib_init_ah_from_path
);
529 static int alloc_mad(struct ib_sa_query
*query
, gfp_t gfp_mask
)
533 spin_lock_irqsave(&query
->port
->ah_lock
, flags
);
534 if (!query
->port
->sm_ah
) {
535 spin_unlock_irqrestore(&query
->port
->ah_lock
, flags
);
538 kref_get(&query
->port
->sm_ah
->ref
);
539 query
->sm_ah
= query
->port
->sm_ah
;
540 spin_unlock_irqrestore(&query
->port
->ah_lock
, flags
);
542 query
->mad_buf
= ib_create_send_mad(query
->port
->agent
, 1,
543 query
->sm_ah
->pkey_index
,
544 0, IB_MGMT_SA_HDR
, IB_MGMT_SA_DATA
,
546 if (IS_ERR(query
->mad_buf
)) {
547 kref_put(&query
->sm_ah
->ref
, free_sm_ah
);
551 query
->mad_buf
->ah
= query
->sm_ah
->ah
;
556 static void free_mad(struct ib_sa_query
*query
)
558 ib_free_send_mad(query
->mad_buf
);
559 kref_put(&query
->sm_ah
->ref
, free_sm_ah
);
562 static void init_mad(struct ib_sa_mad
*mad
, struct ib_mad_agent
*agent
)
566 memset(mad
, 0, sizeof *mad
);
568 mad
->mad_hdr
.base_version
= IB_MGMT_BASE_VERSION
;
569 mad
->mad_hdr
.mgmt_class
= IB_MGMT_CLASS_SUBN_ADM
;
570 mad
->mad_hdr
.class_version
= IB_SA_CLASS_VERSION
;
572 spin_lock_irqsave(&tid_lock
, flags
);
574 cpu_to_be64(((u64
) agent
->hi_tid
) << 32 | tid
++);
575 spin_unlock_irqrestore(&tid_lock
, flags
);
578 static int send_mad(struct ib_sa_query
*query
, int timeout_ms
, gfp_t gfp_mask
)
584 if (!idr_pre_get(&query_idr
, gfp_mask
))
586 spin_lock_irqsave(&idr_lock
, flags
);
587 ret
= idr_get_new(&query_idr
, query
, &id
);
588 spin_unlock_irqrestore(&idr_lock
, flags
);
594 query
->mad_buf
->timeout_ms
= timeout_ms
;
595 query
->mad_buf
->context
[0] = query
;
598 ret
= ib_post_send_mad(query
->mad_buf
, NULL
);
600 spin_lock_irqsave(&idr_lock
, flags
);
601 idr_remove(&query_idr
, id
);
602 spin_unlock_irqrestore(&idr_lock
, flags
);
606 * It's not safe to dereference query any more, because the
607 * send may already have completed and freed the query in
610 return ret
? ret
: id
;
613 void ib_sa_unpack_path(void *attribute
, struct ib_sa_path_rec
*rec
)
615 ib_unpack(path_rec_table
, ARRAY_SIZE(path_rec_table
), attribute
, rec
);
617 EXPORT_SYMBOL(ib_sa_unpack_path
);
619 static void ib_sa_path_rec_callback(struct ib_sa_query
*sa_query
,
621 struct ib_sa_mad
*mad
)
623 struct ib_sa_path_query
*query
=
624 container_of(sa_query
, struct ib_sa_path_query
, sa_query
);
627 struct ib_sa_path_rec rec
;
629 ib_unpack(path_rec_table
, ARRAY_SIZE(path_rec_table
),
631 query
->callback(status
, &rec
, query
->context
);
633 query
->callback(status
, NULL
, query
->context
);
636 static void ib_sa_path_rec_release(struct ib_sa_query
*sa_query
)
638 kfree(container_of(sa_query
, struct ib_sa_path_query
, sa_query
));
642 * ib_sa_path_rec_get - Start a Path get query
644 * @device:device to send query on
645 * @port_num: port number to send query on
646 * @rec:Path Record to send in query
647 * @comp_mask:component mask to send in query
648 * @timeout_ms:time to wait for response
649 * @gfp_mask:GFP mask to use for internal allocations
650 * @callback:function called when query completes, times out or is
652 * @context:opaque user context passed to callback
653 * @sa_query:query context, used to cancel query
655 * Send a Path Record Get query to the SA to look up a path. The
656 * callback function will be called when the query completes (or
657 * fails); status is 0 for a successful response, -EINTR if the query
658 * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
659 * occurred sending the query. The resp parameter of the callback is
660 * only valid if status is 0.
662 * If the return value of ib_sa_path_rec_get() is negative, it is an
663 * error code. Otherwise it is a query ID that can be used to cancel
666 int ib_sa_path_rec_get(struct ib_sa_client
*client
,
667 struct ib_device
*device
, u8 port_num
,
668 struct ib_sa_path_rec
*rec
,
669 ib_sa_comp_mask comp_mask
,
670 int timeout_ms
, gfp_t gfp_mask
,
671 void (*callback
)(int status
,
672 struct ib_sa_path_rec
*resp
,
675 struct ib_sa_query
**sa_query
)
677 struct ib_sa_path_query
*query
;
678 struct ib_sa_device
*sa_dev
= ib_get_client_data(device
, &sa_client
);
679 struct ib_sa_port
*port
;
680 struct ib_mad_agent
*agent
;
681 struct ib_sa_mad
*mad
;
687 port
= &sa_dev
->port
[port_num
- sa_dev
->start_port
];
690 query
= kmalloc(sizeof *query
, gfp_mask
);
694 query
->sa_query
.port
= port
;
695 ret
= alloc_mad(&query
->sa_query
, gfp_mask
);
699 ib_sa_client_get(client
);
700 query
->sa_query
.client
= client
;
701 query
->callback
= callback
;
702 query
->context
= context
;
704 mad
= query
->sa_query
.mad_buf
->mad
;
705 init_mad(mad
, agent
);
707 query
->sa_query
.callback
= callback
? ib_sa_path_rec_callback
: NULL
;
708 query
->sa_query
.release
= ib_sa_path_rec_release
;
709 mad
->mad_hdr
.method
= IB_MGMT_METHOD_GET
;
710 mad
->mad_hdr
.attr_id
= cpu_to_be16(IB_SA_ATTR_PATH_REC
);
711 mad
->sa_hdr
.comp_mask
= comp_mask
;
713 ib_pack(path_rec_table
, ARRAY_SIZE(path_rec_table
), rec
, mad
->data
);
715 *sa_query
= &query
->sa_query
;
717 ret
= send_mad(&query
->sa_query
, timeout_ms
, gfp_mask
);
725 ib_sa_client_put(query
->sa_query
.client
);
726 free_mad(&query
->sa_query
);
732 EXPORT_SYMBOL(ib_sa_path_rec_get
);
734 static void ib_sa_service_rec_callback(struct ib_sa_query
*sa_query
,
736 struct ib_sa_mad
*mad
)
738 struct ib_sa_service_query
*query
=
739 container_of(sa_query
, struct ib_sa_service_query
, sa_query
);
742 struct ib_sa_service_rec rec
;
744 ib_unpack(service_rec_table
, ARRAY_SIZE(service_rec_table
),
746 query
->callback(status
, &rec
, query
->context
);
748 query
->callback(status
, NULL
, query
->context
);
751 static void ib_sa_service_rec_release(struct ib_sa_query
*sa_query
)
753 kfree(container_of(sa_query
, struct ib_sa_service_query
, sa_query
));
757 * ib_sa_service_rec_query - Start Service Record operation
759 * @device:device to send request on
760 * @port_num: port number to send request on
761 * @method:SA method - should be get, set, or delete
762 * @rec:Service Record to send in request
763 * @comp_mask:component mask to send in request
764 * @timeout_ms:time to wait for response
765 * @gfp_mask:GFP mask to use for internal allocations
766 * @callback:function called when request completes, times out or is
768 * @context:opaque user context passed to callback
769 * @sa_query:request context, used to cancel request
771 * Send a Service Record set/get/delete to the SA to register,
772 * unregister or query a service record.
773 * The callback function will be called when the request completes (or
774 * fails); status is 0 for a successful response, -EINTR if the query
775 * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
776 * occurred sending the query. The resp parameter of the callback is
777 * only valid if status is 0.
779 * If the return value of ib_sa_service_rec_query() is negative, it is an
780 * error code. Otherwise it is a request ID that can be used to cancel
783 int ib_sa_service_rec_query(struct ib_sa_client
*client
,
784 struct ib_device
*device
, u8 port_num
, u8 method
,
785 struct ib_sa_service_rec
*rec
,
786 ib_sa_comp_mask comp_mask
,
787 int timeout_ms
, gfp_t gfp_mask
,
788 void (*callback
)(int status
,
789 struct ib_sa_service_rec
*resp
,
792 struct ib_sa_query
**sa_query
)
794 struct ib_sa_service_query
*query
;
795 struct ib_sa_device
*sa_dev
= ib_get_client_data(device
, &sa_client
);
796 struct ib_sa_port
*port
;
797 struct ib_mad_agent
*agent
;
798 struct ib_sa_mad
*mad
;
804 port
= &sa_dev
->port
[port_num
- sa_dev
->start_port
];
807 if (method
!= IB_MGMT_METHOD_GET
&&
808 method
!= IB_MGMT_METHOD_SET
&&
809 method
!= IB_SA_METHOD_DELETE
)
812 query
= kmalloc(sizeof *query
, gfp_mask
);
816 query
->sa_query
.port
= port
;
817 ret
= alloc_mad(&query
->sa_query
, gfp_mask
);
821 ib_sa_client_get(client
);
822 query
->sa_query
.client
= client
;
823 query
->callback
= callback
;
824 query
->context
= context
;
826 mad
= query
->sa_query
.mad_buf
->mad
;
827 init_mad(mad
, agent
);
829 query
->sa_query
.callback
= callback
? ib_sa_service_rec_callback
: NULL
;
830 query
->sa_query
.release
= ib_sa_service_rec_release
;
831 mad
->mad_hdr
.method
= method
;
832 mad
->mad_hdr
.attr_id
= cpu_to_be16(IB_SA_ATTR_SERVICE_REC
);
833 mad
->sa_hdr
.comp_mask
= comp_mask
;
835 ib_pack(service_rec_table
, ARRAY_SIZE(service_rec_table
),
838 *sa_query
= &query
->sa_query
;
840 ret
= send_mad(&query
->sa_query
, timeout_ms
, gfp_mask
);
848 ib_sa_client_put(query
->sa_query
.client
);
849 free_mad(&query
->sa_query
);
855 EXPORT_SYMBOL(ib_sa_service_rec_query
);
857 static void ib_sa_mcmember_rec_callback(struct ib_sa_query
*sa_query
,
859 struct ib_sa_mad
*mad
)
861 struct ib_sa_mcmember_query
*query
=
862 container_of(sa_query
, struct ib_sa_mcmember_query
, sa_query
);
865 struct ib_sa_mcmember_rec rec
;
867 ib_unpack(mcmember_rec_table
, ARRAY_SIZE(mcmember_rec_table
),
869 query
->callback(status
, &rec
, query
->context
);
871 query
->callback(status
, NULL
, query
->context
);
874 static void ib_sa_mcmember_rec_release(struct ib_sa_query
*sa_query
)
876 kfree(container_of(sa_query
, struct ib_sa_mcmember_query
, sa_query
));
879 int ib_sa_mcmember_rec_query(struct ib_sa_client
*client
,
880 struct ib_device
*device
, u8 port_num
,
882 struct ib_sa_mcmember_rec
*rec
,
883 ib_sa_comp_mask comp_mask
,
884 int timeout_ms
, gfp_t gfp_mask
,
885 void (*callback
)(int status
,
886 struct ib_sa_mcmember_rec
*resp
,
889 struct ib_sa_query
**sa_query
)
891 struct ib_sa_mcmember_query
*query
;
892 struct ib_sa_device
*sa_dev
= ib_get_client_data(device
, &sa_client
);
893 struct ib_sa_port
*port
;
894 struct ib_mad_agent
*agent
;
895 struct ib_sa_mad
*mad
;
901 port
= &sa_dev
->port
[port_num
- sa_dev
->start_port
];
904 query
= kmalloc(sizeof *query
, gfp_mask
);
908 query
->sa_query
.port
= port
;
909 ret
= alloc_mad(&query
->sa_query
, gfp_mask
);
913 ib_sa_client_get(client
);
914 query
->sa_query
.client
= client
;
915 query
->callback
= callback
;
916 query
->context
= context
;
918 mad
= query
->sa_query
.mad_buf
->mad
;
919 init_mad(mad
, agent
);
921 query
->sa_query
.callback
= callback
? ib_sa_mcmember_rec_callback
: NULL
;
922 query
->sa_query
.release
= ib_sa_mcmember_rec_release
;
923 mad
->mad_hdr
.method
= method
;
924 mad
->mad_hdr
.attr_id
= cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC
);
925 mad
->sa_hdr
.comp_mask
= comp_mask
;
927 ib_pack(mcmember_rec_table
, ARRAY_SIZE(mcmember_rec_table
),
930 *sa_query
= &query
->sa_query
;
932 ret
= send_mad(&query
->sa_query
, timeout_ms
, gfp_mask
);
940 ib_sa_client_put(query
->sa_query
.client
);
941 free_mad(&query
->sa_query
);
948 static void send_handler(struct ib_mad_agent
*agent
,
949 struct ib_mad_send_wc
*mad_send_wc
)
951 struct ib_sa_query
*query
= mad_send_wc
->send_buf
->context
[0];
955 switch (mad_send_wc
->status
) {
957 /* No callback -- already got recv */
959 case IB_WC_RESP_TIMEOUT_ERR
:
960 query
->callback(query
, -ETIMEDOUT
, NULL
);
962 case IB_WC_WR_FLUSH_ERR
:
963 query
->callback(query
, -EINTR
, NULL
);
966 query
->callback(query
, -EIO
, NULL
);
970 spin_lock_irqsave(&idr_lock
, flags
);
971 idr_remove(&query_idr
, query
->id
);
972 spin_unlock_irqrestore(&idr_lock
, flags
);
975 ib_sa_client_put(query
->client
);
976 query
->release(query
);
979 static void recv_handler(struct ib_mad_agent
*mad_agent
,
980 struct ib_mad_recv_wc
*mad_recv_wc
)
982 struct ib_sa_query
*query
;
983 struct ib_mad_send_buf
*mad_buf
;
985 mad_buf
= (void *) (unsigned long) mad_recv_wc
->wc
->wr_id
;
986 query
= mad_buf
->context
[0];
988 if (query
->callback
) {
989 if (mad_recv_wc
->wc
->status
== IB_WC_SUCCESS
)
990 query
->callback(query
,
991 mad_recv_wc
->recv_buf
.mad
->mad_hdr
.status
?
993 (struct ib_sa_mad
*) mad_recv_wc
->recv_buf
.mad
);
995 query
->callback(query
, -EIO
, NULL
);
998 ib_free_recv_mad(mad_recv_wc
);
1001 static void ib_sa_add_one(struct ib_device
*device
)
1003 struct ib_sa_device
*sa_dev
;
1006 if (rdma_node_get_transport(device
->node_type
) != RDMA_TRANSPORT_IB
)
1009 if (device
->node_type
== RDMA_NODE_IB_SWITCH
)
1013 e
= device
->phys_port_cnt
;
1016 sa_dev
= kzalloc(sizeof *sa_dev
+
1017 (e
- s
+ 1) * sizeof (struct ib_sa_port
),
1022 sa_dev
->start_port
= s
;
1023 sa_dev
->end_port
= e
;
1025 for (i
= 0; i
<= e
- s
; ++i
) {
1026 spin_lock_init(&sa_dev
->port
[i
].ah_lock
);
1027 if (rdma_port_get_link_layer(device
, i
+ 1) != IB_LINK_LAYER_INFINIBAND
)
1030 sa_dev
->port
[i
].sm_ah
= NULL
;
1031 sa_dev
->port
[i
].port_num
= i
+ s
;
1033 sa_dev
->port
[i
].agent
=
1034 ib_register_mad_agent(device
, i
+ s
, IB_QPT_GSI
,
1035 NULL
, 0, send_handler
,
1036 recv_handler
, sa_dev
);
1037 if (IS_ERR(sa_dev
->port
[i
].agent
))
1040 INIT_WORK(&sa_dev
->port
[i
].update_task
, update_sm_ah
);
1043 ib_set_client_data(device
, &sa_client
, sa_dev
);
1046 * We register our event handler after everything is set up,
1047 * and then update our cached info after the event handler is
1048 * registered to avoid any problems if a port changes state
1049 * during our initialization.
1052 INIT_IB_EVENT_HANDLER(&sa_dev
->event_handler
, device
, ib_sa_event
);
1053 if (ib_register_event_handler(&sa_dev
->event_handler
))
1056 for (i
= 0; i
<= e
- s
; ++i
)
1057 if (rdma_port_get_link_layer(device
, i
+ 1) == IB_LINK_LAYER_INFINIBAND
)
1058 update_sm_ah(&sa_dev
->port
[i
].update_task
);
1064 if (rdma_port_get_link_layer(device
, i
+ 1) == IB_LINK_LAYER_INFINIBAND
)
1065 ib_unregister_mad_agent(sa_dev
->port
[i
].agent
);
1072 static void ib_sa_remove_one(struct ib_device
*device
)
1074 struct ib_sa_device
*sa_dev
= ib_get_client_data(device
, &sa_client
);
1080 ib_unregister_event_handler(&sa_dev
->event_handler
);
1082 flush_workqueue(ib_wq
);
1084 for (i
= 0; i
<= sa_dev
->end_port
- sa_dev
->start_port
; ++i
) {
1085 if (rdma_port_get_link_layer(device
, i
+ 1) == IB_LINK_LAYER_INFINIBAND
) {
1086 ib_unregister_mad_agent(sa_dev
->port
[i
].agent
);
1087 if (sa_dev
->port
[i
].sm_ah
)
1088 kref_put(&sa_dev
->port
[i
].sm_ah
->ref
, free_sm_ah
);
1096 static int __init
ib_sa_init(void)
1100 get_random_bytes(&tid
, sizeof tid
);
1102 ret
= ib_register_client(&sa_client
);
1104 printk(KERN_ERR
"Couldn't register ib_sa client\n");
1110 printk(KERN_ERR
"Couldn't initialize multicast handling\n");
1116 ib_unregister_client(&sa_client
);
1121 static void __exit
ib_sa_cleanup(void)
1124 ib_unregister_client(&sa_client
);
1125 idr_destroy(&query_idr
);
1128 module_init(ib_sa_init
);
1129 module_exit(ib_sa_cleanup
);