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>
45 #include <uapi/linux/if_ether.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_guidinfo_query
{
98 void (*callback
)(int, struct ib_sa_guidinfo_rec
*, void *);
100 struct ib_sa_query sa_query
;
103 struct ib_sa_mcmember_query
{
104 void (*callback
)(int, struct ib_sa_mcmember_rec
*, void *);
106 struct ib_sa_query sa_query
;
109 static void ib_sa_add_one(struct ib_device
*device
);
110 static void ib_sa_remove_one(struct ib_device
*device
);
112 static struct ib_client sa_client
= {
114 .add
= ib_sa_add_one
,
115 .remove
= ib_sa_remove_one
118 static DEFINE_SPINLOCK(idr_lock
);
119 static DEFINE_IDR(query_idr
);
121 static DEFINE_SPINLOCK(tid_lock
);
124 #define PATH_REC_FIELD(field) \
125 .struct_offset_bytes = offsetof(struct ib_sa_path_rec, field), \
126 .struct_size_bytes = sizeof ((struct ib_sa_path_rec *) 0)->field, \
127 .field_name = "sa_path_rec:" #field
129 static const struct ib_field path_rec_table
[] = {
130 { PATH_REC_FIELD(service_id
),
134 { PATH_REC_FIELD(dgid
),
138 { PATH_REC_FIELD(sgid
),
142 { PATH_REC_FIELD(dlid
),
146 { PATH_REC_FIELD(slid
),
150 { PATH_REC_FIELD(raw_traffic
),
158 { PATH_REC_FIELD(flow_label
),
162 { PATH_REC_FIELD(hop_limit
),
166 { PATH_REC_FIELD(traffic_class
),
170 { PATH_REC_FIELD(reversible
),
174 { PATH_REC_FIELD(numb_path
),
178 { PATH_REC_FIELD(pkey
),
182 { PATH_REC_FIELD(qos_class
),
186 { PATH_REC_FIELD(sl
),
190 { PATH_REC_FIELD(mtu_selector
),
194 { PATH_REC_FIELD(mtu
),
198 { PATH_REC_FIELD(rate_selector
),
202 { PATH_REC_FIELD(rate
),
206 { PATH_REC_FIELD(packet_life_time_selector
),
210 { PATH_REC_FIELD(packet_life_time
),
214 { PATH_REC_FIELD(preference
),
224 #define MCMEMBER_REC_FIELD(field) \
225 .struct_offset_bytes = offsetof(struct ib_sa_mcmember_rec, field), \
226 .struct_size_bytes = sizeof ((struct ib_sa_mcmember_rec *) 0)->field, \
227 .field_name = "sa_mcmember_rec:" #field
229 static const struct ib_field mcmember_rec_table
[] = {
230 { MCMEMBER_REC_FIELD(mgid
),
234 { MCMEMBER_REC_FIELD(port_gid
),
238 { MCMEMBER_REC_FIELD(qkey
),
242 { MCMEMBER_REC_FIELD(mlid
),
246 { MCMEMBER_REC_FIELD(mtu_selector
),
250 { MCMEMBER_REC_FIELD(mtu
),
254 { MCMEMBER_REC_FIELD(traffic_class
),
258 { MCMEMBER_REC_FIELD(pkey
),
262 { MCMEMBER_REC_FIELD(rate_selector
),
266 { MCMEMBER_REC_FIELD(rate
),
270 { MCMEMBER_REC_FIELD(packet_life_time_selector
),
274 { MCMEMBER_REC_FIELD(packet_life_time
),
278 { MCMEMBER_REC_FIELD(sl
),
282 { MCMEMBER_REC_FIELD(flow_label
),
286 { MCMEMBER_REC_FIELD(hop_limit
),
290 { MCMEMBER_REC_FIELD(scope
),
294 { MCMEMBER_REC_FIELD(join_state
),
298 { MCMEMBER_REC_FIELD(proxy_join
),
308 #define SERVICE_REC_FIELD(field) \
309 .struct_offset_bytes = offsetof(struct ib_sa_service_rec, field), \
310 .struct_size_bytes = sizeof ((struct ib_sa_service_rec *) 0)->field, \
311 .field_name = "sa_service_rec:" #field
313 static const struct ib_field service_rec_table
[] = {
314 { SERVICE_REC_FIELD(id
),
318 { SERVICE_REC_FIELD(gid
),
322 { SERVICE_REC_FIELD(pkey
),
326 { SERVICE_REC_FIELD(lease
),
330 { SERVICE_REC_FIELD(key
),
334 { SERVICE_REC_FIELD(name
),
338 { SERVICE_REC_FIELD(data8
),
342 { SERVICE_REC_FIELD(data16
),
346 { SERVICE_REC_FIELD(data32
),
350 { SERVICE_REC_FIELD(data64
),
356 #define GUIDINFO_REC_FIELD(field) \
357 .struct_offset_bytes = offsetof(struct ib_sa_guidinfo_rec, field), \
358 .struct_size_bytes = sizeof((struct ib_sa_guidinfo_rec *) 0)->field, \
359 .field_name = "sa_guidinfo_rec:" #field
361 static const struct ib_field guidinfo_rec_table
[] = {
362 { GUIDINFO_REC_FIELD(lid
),
366 { GUIDINFO_REC_FIELD(block_num
),
370 { GUIDINFO_REC_FIELD(res1
),
374 { GUIDINFO_REC_FIELD(res2
),
378 { GUIDINFO_REC_FIELD(guid_info_list
),
384 static void free_sm_ah(struct kref
*kref
)
386 struct ib_sa_sm_ah
*sm_ah
= container_of(kref
, struct ib_sa_sm_ah
, ref
);
388 ib_destroy_ah(sm_ah
->ah
);
392 static void update_sm_ah(struct work_struct
*work
)
394 struct ib_sa_port
*port
=
395 container_of(work
, struct ib_sa_port
, update_task
);
396 struct ib_sa_sm_ah
*new_ah
;
397 struct ib_port_attr port_attr
;
398 struct ib_ah_attr ah_attr
;
400 if (ib_query_port(port
->agent
->device
, port
->port_num
, &port_attr
)) {
401 printk(KERN_WARNING
"Couldn't query port\n");
405 new_ah
= kmalloc(sizeof *new_ah
, GFP_KERNEL
);
407 printk(KERN_WARNING
"Couldn't allocate new SM AH\n");
411 kref_init(&new_ah
->ref
);
412 new_ah
->src_path_mask
= (1 << port_attr
.lmc
) - 1;
414 new_ah
->pkey_index
= 0;
415 if (ib_find_pkey(port
->agent
->device
, port
->port_num
,
416 IB_DEFAULT_PKEY_FULL
, &new_ah
->pkey_index
))
417 printk(KERN_ERR
"Couldn't find index for default PKey\n");
419 memset(&ah_attr
, 0, sizeof ah_attr
);
420 ah_attr
.dlid
= port_attr
.sm_lid
;
421 ah_attr
.sl
= port_attr
.sm_sl
;
422 ah_attr
.port_num
= port
->port_num
;
424 new_ah
->ah
= ib_create_ah(port
->agent
->qp
->pd
, &ah_attr
);
425 if (IS_ERR(new_ah
->ah
)) {
426 printk(KERN_WARNING
"Couldn't create new SM AH\n");
431 spin_lock_irq(&port
->ah_lock
);
433 kref_put(&port
->sm_ah
->ref
, free_sm_ah
);
434 port
->sm_ah
= new_ah
;
435 spin_unlock_irq(&port
->ah_lock
);
439 static void ib_sa_event(struct ib_event_handler
*handler
, struct ib_event
*event
)
441 if (event
->event
== IB_EVENT_PORT_ERR
||
442 event
->event
== IB_EVENT_PORT_ACTIVE
||
443 event
->event
== IB_EVENT_LID_CHANGE
||
444 event
->event
== IB_EVENT_PKEY_CHANGE
||
445 event
->event
== IB_EVENT_SM_CHANGE
||
446 event
->event
== IB_EVENT_CLIENT_REREGISTER
) {
448 struct ib_sa_device
*sa_dev
=
449 container_of(handler
, typeof(*sa_dev
), event_handler
);
450 struct ib_sa_port
*port
=
451 &sa_dev
->port
[event
->element
.port_num
- sa_dev
->start_port
];
453 if (!rdma_cap_ib_sa(handler
->device
, port
->port_num
))
456 spin_lock_irqsave(&port
->ah_lock
, flags
);
458 kref_put(&port
->sm_ah
->ref
, free_sm_ah
);
460 spin_unlock_irqrestore(&port
->ah_lock
, flags
);
462 queue_work(ib_wq
, &sa_dev
->port
[event
->element
.port_num
-
463 sa_dev
->start_port
].update_task
);
467 void ib_sa_register_client(struct ib_sa_client
*client
)
469 atomic_set(&client
->users
, 1);
470 init_completion(&client
->comp
);
472 EXPORT_SYMBOL(ib_sa_register_client
);
474 void ib_sa_unregister_client(struct ib_sa_client
*client
)
476 ib_sa_client_put(client
);
477 wait_for_completion(&client
->comp
);
479 EXPORT_SYMBOL(ib_sa_unregister_client
);
482 * ib_sa_cancel_query - try to cancel an SA query
483 * @id:ID of query to cancel
484 * @query:query pointer to cancel
486 * Try to cancel an SA query. If the id and query don't match up or
487 * the query has already completed, nothing is done. Otherwise the
488 * query is canceled and will complete with a status of -EINTR.
490 void ib_sa_cancel_query(int id
, struct ib_sa_query
*query
)
493 struct ib_mad_agent
*agent
;
494 struct ib_mad_send_buf
*mad_buf
;
496 spin_lock_irqsave(&idr_lock
, flags
);
497 if (idr_find(&query_idr
, id
) != query
) {
498 spin_unlock_irqrestore(&idr_lock
, flags
);
501 agent
= query
->port
->agent
;
502 mad_buf
= query
->mad_buf
;
503 spin_unlock_irqrestore(&idr_lock
, flags
);
505 ib_cancel_mad(agent
, mad_buf
);
507 EXPORT_SYMBOL(ib_sa_cancel_query
);
509 static u8
get_src_path_mask(struct ib_device
*device
, u8 port_num
)
511 struct ib_sa_device
*sa_dev
;
512 struct ib_sa_port
*port
;
516 sa_dev
= ib_get_client_data(device
, &sa_client
);
520 port
= &sa_dev
->port
[port_num
- sa_dev
->start_port
];
521 spin_lock_irqsave(&port
->ah_lock
, flags
);
522 src_path_mask
= port
->sm_ah
? port
->sm_ah
->src_path_mask
: 0x7f;
523 spin_unlock_irqrestore(&port
->ah_lock
, flags
);
525 return src_path_mask
;
528 int ib_init_ah_from_path(struct ib_device
*device
, u8 port_num
,
529 struct ib_sa_path_rec
*rec
, struct ib_ah_attr
*ah_attr
)
535 memset(ah_attr
, 0, sizeof *ah_attr
);
536 ah_attr
->dlid
= be16_to_cpu(rec
->dlid
);
537 ah_attr
->sl
= rec
->sl
;
538 ah_attr
->src_path_bits
= be16_to_cpu(rec
->slid
) &
539 get_src_path_mask(device
, port_num
);
540 ah_attr
->port_num
= port_num
;
541 ah_attr
->static_rate
= rec
->rate
;
543 force_grh
= rdma_cap_eth_ah(device
, port_num
);
545 if (rec
->hop_limit
> 1 || force_grh
) {
546 ah_attr
->ah_flags
= IB_AH_GRH
;
547 ah_attr
->grh
.dgid
= rec
->dgid
;
549 ret
= ib_find_cached_gid(device
, &rec
->sgid
, &port_num
,
554 ah_attr
->grh
.sgid_index
= gid_index
;
555 ah_attr
->grh
.flow_label
= be32_to_cpu(rec
->flow_label
);
556 ah_attr
->grh
.hop_limit
= rec
->hop_limit
;
557 ah_attr
->grh
.traffic_class
= rec
->traffic_class
;
560 memcpy(ah_attr
->dmac
, rec
->dmac
, ETH_ALEN
);
561 ah_attr
->vlan_id
= rec
->vlan_id
;
563 ah_attr
->vlan_id
= 0xffff;
568 EXPORT_SYMBOL(ib_init_ah_from_path
);
570 static int alloc_mad(struct ib_sa_query
*query
, gfp_t gfp_mask
)
574 spin_lock_irqsave(&query
->port
->ah_lock
, flags
);
575 if (!query
->port
->sm_ah
) {
576 spin_unlock_irqrestore(&query
->port
->ah_lock
, flags
);
579 kref_get(&query
->port
->sm_ah
->ref
);
580 query
->sm_ah
= query
->port
->sm_ah
;
581 spin_unlock_irqrestore(&query
->port
->ah_lock
, flags
);
583 query
->mad_buf
= ib_create_send_mad(query
->port
->agent
, 1,
584 query
->sm_ah
->pkey_index
,
585 0, IB_MGMT_SA_HDR
, IB_MGMT_SA_DATA
,
587 IB_MGMT_BASE_VERSION
);
588 if (IS_ERR(query
->mad_buf
)) {
589 kref_put(&query
->sm_ah
->ref
, free_sm_ah
);
593 query
->mad_buf
->ah
= query
->sm_ah
->ah
;
598 static void free_mad(struct ib_sa_query
*query
)
600 ib_free_send_mad(query
->mad_buf
);
601 kref_put(&query
->sm_ah
->ref
, free_sm_ah
);
604 static void init_mad(struct ib_sa_mad
*mad
, struct ib_mad_agent
*agent
)
608 memset(mad
, 0, sizeof *mad
);
610 mad
->mad_hdr
.base_version
= IB_MGMT_BASE_VERSION
;
611 mad
->mad_hdr
.mgmt_class
= IB_MGMT_CLASS_SUBN_ADM
;
612 mad
->mad_hdr
.class_version
= IB_SA_CLASS_VERSION
;
614 spin_lock_irqsave(&tid_lock
, flags
);
616 cpu_to_be64(((u64
) agent
->hi_tid
) << 32 | tid
++);
617 spin_unlock_irqrestore(&tid_lock
, flags
);
620 static int send_mad(struct ib_sa_query
*query
, int timeout_ms
, gfp_t gfp_mask
)
622 bool preload
= !!(gfp_mask
& __GFP_WAIT
);
627 idr_preload(gfp_mask
);
628 spin_lock_irqsave(&idr_lock
, flags
);
630 id
= idr_alloc(&query_idr
, query
, 0, 0, GFP_NOWAIT
);
632 spin_unlock_irqrestore(&idr_lock
, flags
);
638 query
->mad_buf
->timeout_ms
= timeout_ms
;
639 query
->mad_buf
->context
[0] = query
;
642 ret
= ib_post_send_mad(query
->mad_buf
, NULL
);
644 spin_lock_irqsave(&idr_lock
, flags
);
645 idr_remove(&query_idr
, id
);
646 spin_unlock_irqrestore(&idr_lock
, flags
);
650 * It's not safe to dereference query any more, because the
651 * send may already have completed and freed the query in
654 return ret
? ret
: id
;
657 void ib_sa_unpack_path(void *attribute
, struct ib_sa_path_rec
*rec
)
659 ib_unpack(path_rec_table
, ARRAY_SIZE(path_rec_table
), attribute
, rec
);
661 EXPORT_SYMBOL(ib_sa_unpack_path
);
663 void ib_sa_pack_path(struct ib_sa_path_rec
*rec
, void *attribute
)
665 ib_pack(path_rec_table
, ARRAY_SIZE(path_rec_table
), rec
, attribute
);
667 EXPORT_SYMBOL(ib_sa_pack_path
);
669 static void ib_sa_path_rec_callback(struct ib_sa_query
*sa_query
,
671 struct ib_sa_mad
*mad
)
673 struct ib_sa_path_query
*query
=
674 container_of(sa_query
, struct ib_sa_path_query
, sa_query
);
677 struct ib_sa_path_rec rec
;
679 ib_unpack(path_rec_table
, ARRAY_SIZE(path_rec_table
),
681 rec
.vlan_id
= 0xffff;
682 memset(rec
.dmac
, 0, ETH_ALEN
);
683 memset(rec
.smac
, 0, ETH_ALEN
);
684 query
->callback(status
, &rec
, query
->context
);
686 query
->callback(status
, NULL
, query
->context
);
689 static void ib_sa_path_rec_release(struct ib_sa_query
*sa_query
)
691 kfree(container_of(sa_query
, struct ib_sa_path_query
, sa_query
));
695 * ib_sa_path_rec_get - Start a Path get query
697 * @device:device to send query on
698 * @port_num: port number to send query on
699 * @rec:Path Record to send in query
700 * @comp_mask:component mask to send in query
701 * @timeout_ms:time to wait for response
702 * @gfp_mask:GFP mask to use for internal allocations
703 * @callback:function called when query completes, times out or is
705 * @context:opaque user context passed to callback
706 * @sa_query:query context, used to cancel query
708 * Send a Path Record Get query to the SA to look up a path. The
709 * callback function will be called when the query completes (or
710 * fails); status is 0 for a successful response, -EINTR if the query
711 * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
712 * occurred sending the query. The resp parameter of the callback is
713 * only valid if status is 0.
715 * If the return value of ib_sa_path_rec_get() is negative, it is an
716 * error code. Otherwise it is a query ID that can be used to cancel
719 int ib_sa_path_rec_get(struct ib_sa_client
*client
,
720 struct ib_device
*device
, u8 port_num
,
721 struct ib_sa_path_rec
*rec
,
722 ib_sa_comp_mask comp_mask
,
723 int timeout_ms
, gfp_t gfp_mask
,
724 void (*callback
)(int status
,
725 struct ib_sa_path_rec
*resp
,
728 struct ib_sa_query
**sa_query
)
730 struct ib_sa_path_query
*query
;
731 struct ib_sa_device
*sa_dev
= ib_get_client_data(device
, &sa_client
);
732 struct ib_sa_port
*port
;
733 struct ib_mad_agent
*agent
;
734 struct ib_sa_mad
*mad
;
740 port
= &sa_dev
->port
[port_num
- sa_dev
->start_port
];
743 query
= kmalloc(sizeof *query
, gfp_mask
);
747 query
->sa_query
.port
= port
;
748 ret
= alloc_mad(&query
->sa_query
, gfp_mask
);
752 ib_sa_client_get(client
);
753 query
->sa_query
.client
= client
;
754 query
->callback
= callback
;
755 query
->context
= context
;
757 mad
= query
->sa_query
.mad_buf
->mad
;
758 init_mad(mad
, agent
);
760 query
->sa_query
.callback
= callback
? ib_sa_path_rec_callback
: NULL
;
761 query
->sa_query
.release
= ib_sa_path_rec_release
;
762 mad
->mad_hdr
.method
= IB_MGMT_METHOD_GET
;
763 mad
->mad_hdr
.attr_id
= cpu_to_be16(IB_SA_ATTR_PATH_REC
);
764 mad
->sa_hdr
.comp_mask
= comp_mask
;
766 ib_pack(path_rec_table
, ARRAY_SIZE(path_rec_table
), rec
, mad
->data
);
768 *sa_query
= &query
->sa_query
;
770 ret
= send_mad(&query
->sa_query
, timeout_ms
, gfp_mask
);
778 ib_sa_client_put(query
->sa_query
.client
);
779 free_mad(&query
->sa_query
);
785 EXPORT_SYMBOL(ib_sa_path_rec_get
);
787 static void ib_sa_service_rec_callback(struct ib_sa_query
*sa_query
,
789 struct ib_sa_mad
*mad
)
791 struct ib_sa_service_query
*query
=
792 container_of(sa_query
, struct ib_sa_service_query
, sa_query
);
795 struct ib_sa_service_rec rec
;
797 ib_unpack(service_rec_table
, ARRAY_SIZE(service_rec_table
),
799 query
->callback(status
, &rec
, query
->context
);
801 query
->callback(status
, NULL
, query
->context
);
804 static void ib_sa_service_rec_release(struct ib_sa_query
*sa_query
)
806 kfree(container_of(sa_query
, struct ib_sa_service_query
, sa_query
));
810 * ib_sa_service_rec_query - Start Service Record operation
812 * @device:device to send request on
813 * @port_num: port number to send request on
814 * @method:SA method - should be get, set, or delete
815 * @rec:Service Record to send in request
816 * @comp_mask:component mask to send in request
817 * @timeout_ms:time to wait for response
818 * @gfp_mask:GFP mask to use for internal allocations
819 * @callback:function called when request completes, times out or is
821 * @context:opaque user context passed to callback
822 * @sa_query:request context, used to cancel request
824 * Send a Service Record set/get/delete to the SA to register,
825 * unregister or query a service record.
826 * The callback function will be called when the request completes (or
827 * fails); status is 0 for a successful response, -EINTR if the query
828 * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
829 * occurred sending the query. The resp parameter of the callback is
830 * only valid if status is 0.
832 * If the return value of ib_sa_service_rec_query() is negative, it is an
833 * error code. Otherwise it is a request ID that can be used to cancel
836 int ib_sa_service_rec_query(struct ib_sa_client
*client
,
837 struct ib_device
*device
, u8 port_num
, u8 method
,
838 struct ib_sa_service_rec
*rec
,
839 ib_sa_comp_mask comp_mask
,
840 int timeout_ms
, gfp_t gfp_mask
,
841 void (*callback
)(int status
,
842 struct ib_sa_service_rec
*resp
,
845 struct ib_sa_query
**sa_query
)
847 struct ib_sa_service_query
*query
;
848 struct ib_sa_device
*sa_dev
= ib_get_client_data(device
, &sa_client
);
849 struct ib_sa_port
*port
;
850 struct ib_mad_agent
*agent
;
851 struct ib_sa_mad
*mad
;
857 port
= &sa_dev
->port
[port_num
- sa_dev
->start_port
];
860 if (method
!= IB_MGMT_METHOD_GET
&&
861 method
!= IB_MGMT_METHOD_SET
&&
862 method
!= IB_SA_METHOD_DELETE
)
865 query
= kmalloc(sizeof *query
, gfp_mask
);
869 query
->sa_query
.port
= port
;
870 ret
= alloc_mad(&query
->sa_query
, gfp_mask
);
874 ib_sa_client_get(client
);
875 query
->sa_query
.client
= client
;
876 query
->callback
= callback
;
877 query
->context
= context
;
879 mad
= query
->sa_query
.mad_buf
->mad
;
880 init_mad(mad
, agent
);
882 query
->sa_query
.callback
= callback
? ib_sa_service_rec_callback
: NULL
;
883 query
->sa_query
.release
= ib_sa_service_rec_release
;
884 mad
->mad_hdr
.method
= method
;
885 mad
->mad_hdr
.attr_id
= cpu_to_be16(IB_SA_ATTR_SERVICE_REC
);
886 mad
->sa_hdr
.comp_mask
= comp_mask
;
888 ib_pack(service_rec_table
, ARRAY_SIZE(service_rec_table
),
891 *sa_query
= &query
->sa_query
;
893 ret
= send_mad(&query
->sa_query
, timeout_ms
, gfp_mask
);
901 ib_sa_client_put(query
->sa_query
.client
);
902 free_mad(&query
->sa_query
);
908 EXPORT_SYMBOL(ib_sa_service_rec_query
);
910 static void ib_sa_mcmember_rec_callback(struct ib_sa_query
*sa_query
,
912 struct ib_sa_mad
*mad
)
914 struct ib_sa_mcmember_query
*query
=
915 container_of(sa_query
, struct ib_sa_mcmember_query
, sa_query
);
918 struct ib_sa_mcmember_rec rec
;
920 ib_unpack(mcmember_rec_table
, ARRAY_SIZE(mcmember_rec_table
),
922 query
->callback(status
, &rec
, query
->context
);
924 query
->callback(status
, NULL
, query
->context
);
927 static void ib_sa_mcmember_rec_release(struct ib_sa_query
*sa_query
)
929 kfree(container_of(sa_query
, struct ib_sa_mcmember_query
, sa_query
));
932 int ib_sa_mcmember_rec_query(struct ib_sa_client
*client
,
933 struct ib_device
*device
, u8 port_num
,
935 struct ib_sa_mcmember_rec
*rec
,
936 ib_sa_comp_mask comp_mask
,
937 int timeout_ms
, gfp_t gfp_mask
,
938 void (*callback
)(int status
,
939 struct ib_sa_mcmember_rec
*resp
,
942 struct ib_sa_query
**sa_query
)
944 struct ib_sa_mcmember_query
*query
;
945 struct ib_sa_device
*sa_dev
= ib_get_client_data(device
, &sa_client
);
946 struct ib_sa_port
*port
;
947 struct ib_mad_agent
*agent
;
948 struct ib_sa_mad
*mad
;
954 port
= &sa_dev
->port
[port_num
- sa_dev
->start_port
];
957 query
= kmalloc(sizeof *query
, gfp_mask
);
961 query
->sa_query
.port
= port
;
962 ret
= alloc_mad(&query
->sa_query
, gfp_mask
);
966 ib_sa_client_get(client
);
967 query
->sa_query
.client
= client
;
968 query
->callback
= callback
;
969 query
->context
= context
;
971 mad
= query
->sa_query
.mad_buf
->mad
;
972 init_mad(mad
, agent
);
974 query
->sa_query
.callback
= callback
? ib_sa_mcmember_rec_callback
: NULL
;
975 query
->sa_query
.release
= ib_sa_mcmember_rec_release
;
976 mad
->mad_hdr
.method
= method
;
977 mad
->mad_hdr
.attr_id
= cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC
);
978 mad
->sa_hdr
.comp_mask
= comp_mask
;
980 ib_pack(mcmember_rec_table
, ARRAY_SIZE(mcmember_rec_table
),
983 *sa_query
= &query
->sa_query
;
985 ret
= send_mad(&query
->sa_query
, timeout_ms
, gfp_mask
);
993 ib_sa_client_put(query
->sa_query
.client
);
994 free_mad(&query
->sa_query
);
1001 /* Support GuidInfoRecord */
1002 static void ib_sa_guidinfo_rec_callback(struct ib_sa_query
*sa_query
,
1004 struct ib_sa_mad
*mad
)
1006 struct ib_sa_guidinfo_query
*query
=
1007 container_of(sa_query
, struct ib_sa_guidinfo_query
, sa_query
);
1010 struct ib_sa_guidinfo_rec rec
;
1012 ib_unpack(guidinfo_rec_table
, ARRAY_SIZE(guidinfo_rec_table
),
1014 query
->callback(status
, &rec
, query
->context
);
1016 query
->callback(status
, NULL
, query
->context
);
1019 static void ib_sa_guidinfo_rec_release(struct ib_sa_query
*sa_query
)
1021 kfree(container_of(sa_query
, struct ib_sa_guidinfo_query
, sa_query
));
1024 int ib_sa_guid_info_rec_query(struct ib_sa_client
*client
,
1025 struct ib_device
*device
, u8 port_num
,
1026 struct ib_sa_guidinfo_rec
*rec
,
1027 ib_sa_comp_mask comp_mask
, u8 method
,
1028 int timeout_ms
, gfp_t gfp_mask
,
1029 void (*callback
)(int status
,
1030 struct ib_sa_guidinfo_rec
*resp
,
1033 struct ib_sa_query
**sa_query
)
1035 struct ib_sa_guidinfo_query
*query
;
1036 struct ib_sa_device
*sa_dev
= ib_get_client_data(device
, &sa_client
);
1037 struct ib_sa_port
*port
;
1038 struct ib_mad_agent
*agent
;
1039 struct ib_sa_mad
*mad
;
1045 if (method
!= IB_MGMT_METHOD_GET
&&
1046 method
!= IB_MGMT_METHOD_SET
&&
1047 method
!= IB_SA_METHOD_DELETE
) {
1051 port
= &sa_dev
->port
[port_num
- sa_dev
->start_port
];
1052 agent
= port
->agent
;
1054 query
= kmalloc(sizeof *query
, gfp_mask
);
1058 query
->sa_query
.port
= port
;
1059 ret
= alloc_mad(&query
->sa_query
, gfp_mask
);
1063 ib_sa_client_get(client
);
1064 query
->sa_query
.client
= client
;
1065 query
->callback
= callback
;
1066 query
->context
= context
;
1068 mad
= query
->sa_query
.mad_buf
->mad
;
1069 init_mad(mad
, agent
);
1071 query
->sa_query
.callback
= callback
? ib_sa_guidinfo_rec_callback
: NULL
;
1072 query
->sa_query
.release
= ib_sa_guidinfo_rec_release
;
1074 mad
->mad_hdr
.method
= method
;
1075 mad
->mad_hdr
.attr_id
= cpu_to_be16(IB_SA_ATTR_GUID_INFO_REC
);
1076 mad
->sa_hdr
.comp_mask
= comp_mask
;
1078 ib_pack(guidinfo_rec_table
, ARRAY_SIZE(guidinfo_rec_table
), rec
,
1081 *sa_query
= &query
->sa_query
;
1083 ret
= send_mad(&query
->sa_query
, timeout_ms
, gfp_mask
);
1091 ib_sa_client_put(query
->sa_query
.client
);
1092 free_mad(&query
->sa_query
);
1098 EXPORT_SYMBOL(ib_sa_guid_info_rec_query
);
1100 static void send_handler(struct ib_mad_agent
*agent
,
1101 struct ib_mad_send_wc
*mad_send_wc
)
1103 struct ib_sa_query
*query
= mad_send_wc
->send_buf
->context
[0];
1104 unsigned long flags
;
1106 if (query
->callback
)
1107 switch (mad_send_wc
->status
) {
1109 /* No callback -- already got recv */
1111 case IB_WC_RESP_TIMEOUT_ERR
:
1112 query
->callback(query
, -ETIMEDOUT
, NULL
);
1114 case IB_WC_WR_FLUSH_ERR
:
1115 query
->callback(query
, -EINTR
, NULL
);
1118 query
->callback(query
, -EIO
, NULL
);
1122 spin_lock_irqsave(&idr_lock
, flags
);
1123 idr_remove(&query_idr
, query
->id
);
1124 spin_unlock_irqrestore(&idr_lock
, flags
);
1127 ib_sa_client_put(query
->client
);
1128 query
->release(query
);
1131 static void recv_handler(struct ib_mad_agent
*mad_agent
,
1132 struct ib_mad_recv_wc
*mad_recv_wc
)
1134 struct ib_sa_query
*query
;
1135 struct ib_mad_send_buf
*mad_buf
;
1137 mad_buf
= (void *) (unsigned long) mad_recv_wc
->wc
->wr_id
;
1138 query
= mad_buf
->context
[0];
1140 if (query
->callback
) {
1141 if (mad_recv_wc
->wc
->status
== IB_WC_SUCCESS
)
1142 query
->callback(query
,
1143 mad_recv_wc
->recv_buf
.mad
->mad_hdr
.status
?
1145 (struct ib_sa_mad
*) mad_recv_wc
->recv_buf
.mad
);
1147 query
->callback(query
, -EIO
, NULL
);
1150 ib_free_recv_mad(mad_recv_wc
);
1153 static void ib_sa_add_one(struct ib_device
*device
)
1155 struct ib_sa_device
*sa_dev
;
1159 s
= rdma_start_port(device
);
1160 e
= rdma_end_port(device
);
1162 sa_dev
= kzalloc(sizeof *sa_dev
+
1163 (e
- s
+ 1) * sizeof (struct ib_sa_port
),
1168 sa_dev
->start_port
= s
;
1169 sa_dev
->end_port
= e
;
1171 for (i
= 0; i
<= e
- s
; ++i
) {
1172 spin_lock_init(&sa_dev
->port
[i
].ah_lock
);
1173 if (!rdma_cap_ib_sa(device
, i
+ 1))
1176 sa_dev
->port
[i
].sm_ah
= NULL
;
1177 sa_dev
->port
[i
].port_num
= i
+ s
;
1179 sa_dev
->port
[i
].agent
=
1180 ib_register_mad_agent(device
, i
+ s
, IB_QPT_GSI
,
1181 NULL
, 0, send_handler
,
1182 recv_handler
, sa_dev
, 0);
1183 if (IS_ERR(sa_dev
->port
[i
].agent
))
1186 INIT_WORK(&sa_dev
->port
[i
].update_task
, update_sm_ah
);
1194 ib_set_client_data(device
, &sa_client
, sa_dev
);
1197 * We register our event handler after everything is set up,
1198 * and then update our cached info after the event handler is
1199 * registered to avoid any problems if a port changes state
1200 * during our initialization.
1203 INIT_IB_EVENT_HANDLER(&sa_dev
->event_handler
, device
, ib_sa_event
);
1204 if (ib_register_event_handler(&sa_dev
->event_handler
))
1207 for (i
= 0; i
<= e
- s
; ++i
) {
1208 if (rdma_cap_ib_sa(device
, i
+ 1))
1209 update_sm_ah(&sa_dev
->port
[i
].update_task
);
1216 if (rdma_cap_ib_sa(device
, i
+ 1))
1217 ib_unregister_mad_agent(sa_dev
->port
[i
].agent
);
1224 static void ib_sa_remove_one(struct ib_device
*device
)
1226 struct ib_sa_device
*sa_dev
= ib_get_client_data(device
, &sa_client
);
1232 ib_unregister_event_handler(&sa_dev
->event_handler
);
1234 flush_workqueue(ib_wq
);
1236 for (i
= 0; i
<= sa_dev
->end_port
- sa_dev
->start_port
; ++i
) {
1237 if (rdma_cap_ib_sa(device
, i
+ 1)) {
1238 ib_unregister_mad_agent(sa_dev
->port
[i
].agent
);
1239 if (sa_dev
->port
[i
].sm_ah
)
1240 kref_put(&sa_dev
->port
[i
].sm_ah
->ref
, free_sm_ah
);
1248 static int __init
ib_sa_init(void)
1252 get_random_bytes(&tid
, sizeof tid
);
1254 ret
= ib_register_client(&sa_client
);
1256 printk(KERN_ERR
"Couldn't register ib_sa client\n");
1262 printk(KERN_ERR
"Couldn't initialize multicast handling\n");
1268 ib_unregister_client(&sa_client
);
1273 static void __exit
ib_sa_cleanup(void)
1276 ib_unregister_client(&sa_client
);
1277 idr_destroy(&query_idr
);
1280 module_init(ib_sa_init
);
1281 module_exit(ib_sa_cleanup
);