2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Voltaire, Inc. All rights reserved.
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 * $Id: sa_query.c 2811 2005-07-06 18:11:43Z halr $
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/err.h>
39 #include <linux/random.h>
40 #include <linux/spinlock.h>
41 #include <linux/slab.h>
42 #include <linux/pci.h>
43 #include <linux/dma-mapping.h>
44 #include <linux/kref.h>
45 #include <linux/idr.h>
47 #include <rdma/ib_pack.h>
48 #include <rdma/ib_sa.h>
50 MODULE_AUTHOR("Roland Dreier");
51 MODULE_DESCRIPTION("InfiniBand subnet administration query support");
52 MODULE_LICENSE("Dual BSD/GPL");
60 struct ib_mad_agent
*agent
;
61 struct ib_sa_sm_ah
*sm_ah
;
62 struct work_struct update_task
;
68 int start_port
, end_port
;
69 struct ib_event_handler event_handler
;
70 struct ib_sa_port port
[0];
74 void (*callback
)(struct ib_sa_query
*, int, struct ib_sa_mad
*);
75 void (*release
)(struct ib_sa_query
*);
76 struct ib_sa_port
*port
;
77 struct ib_sa_mad
*mad
;
78 struct ib_sa_sm_ah
*sm_ah
;
79 DECLARE_PCI_UNMAP_ADDR(mapping
)
83 struct ib_sa_service_query
{
84 void (*callback
)(int, struct ib_sa_service_rec
*, void *);
86 struct ib_sa_query sa_query
;
89 struct ib_sa_path_query
{
90 void (*callback
)(int, struct ib_sa_path_rec
*, void *);
92 struct ib_sa_query sa_query
;
95 struct ib_sa_mcmember_query
{
96 void (*callback
)(int, struct ib_sa_mcmember_rec
*, void *);
98 struct ib_sa_query sa_query
;
101 static void ib_sa_add_one(struct ib_device
*device
);
102 static void ib_sa_remove_one(struct ib_device
*device
);
104 static struct ib_client sa_client
= {
106 .add
= ib_sa_add_one
,
107 .remove
= ib_sa_remove_one
110 static spinlock_t idr_lock
;
111 static DEFINE_IDR(query_idr
);
113 static spinlock_t tid_lock
;
116 #define PATH_REC_FIELD(field) \
117 .struct_offset_bytes = offsetof(struct ib_sa_path_rec, field), \
118 .struct_size_bytes = sizeof ((struct ib_sa_path_rec *) 0)->field, \
119 .field_name = "sa_path_rec:" #field
121 static const struct ib_field path_rec_table
[] = {
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
),
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(void *port_ptr
)
362 struct ib_sa_port
*port
= port_ptr
;
363 struct ib_sa_sm_ah
*new_ah
, *old_ah
;
364 struct ib_port_attr port_attr
;
365 struct ib_ah_attr ah_attr
;
367 if (ib_query_port(port
->agent
->device
, port
->port_num
, &port_attr
)) {
368 printk(KERN_WARNING
"Couldn't query port\n");
372 new_ah
= kmalloc(sizeof *new_ah
, GFP_KERNEL
);
374 printk(KERN_WARNING
"Couldn't allocate new SM AH\n");
378 kref_init(&new_ah
->ref
);
380 memset(&ah_attr
, 0, sizeof ah_attr
);
381 ah_attr
.dlid
= port_attr
.sm_lid
;
382 ah_attr
.sl
= port_attr
.sm_sl
;
383 ah_attr
.port_num
= port
->port_num
;
385 new_ah
->ah
= ib_create_ah(port
->agent
->qp
->pd
, &ah_attr
);
386 if (IS_ERR(new_ah
->ah
)) {
387 printk(KERN_WARNING
"Couldn't create new SM AH\n");
392 spin_lock_irq(&port
->ah_lock
);
393 old_ah
= port
->sm_ah
;
394 port
->sm_ah
= new_ah
;
395 spin_unlock_irq(&port
->ah_lock
);
398 kref_put(&old_ah
->ref
, free_sm_ah
);
401 static void ib_sa_event(struct ib_event_handler
*handler
, struct ib_event
*event
)
403 if (event
->event
== IB_EVENT_PORT_ERR
||
404 event
->event
== IB_EVENT_PORT_ACTIVE
||
405 event
->event
== IB_EVENT_LID_CHANGE
||
406 event
->event
== IB_EVENT_PKEY_CHANGE
||
407 event
->event
== IB_EVENT_SM_CHANGE
) {
408 struct ib_sa_device
*sa_dev
;
409 sa_dev
= container_of(handler
, typeof(*sa_dev
), event_handler
);
411 schedule_work(&sa_dev
->port
[event
->element
.port_num
-
412 sa_dev
->start_port
].update_task
);
417 * ib_sa_cancel_query - try to cancel an SA query
418 * @id:ID of query to cancel
419 * @query:query pointer to cancel
421 * Try to cancel an SA query. If the id and query don't match up or
422 * the query has already completed, nothing is done. Otherwise the
423 * query is canceled and will complete with a status of -EINTR.
425 void ib_sa_cancel_query(int id
, struct ib_sa_query
*query
)
428 struct ib_mad_agent
*agent
;
430 spin_lock_irqsave(&idr_lock
, flags
);
431 if (idr_find(&query_idr
, id
) != query
) {
432 spin_unlock_irqrestore(&idr_lock
, flags
);
435 agent
= query
->port
->agent
;
436 spin_unlock_irqrestore(&idr_lock
, flags
);
438 ib_cancel_mad(agent
, id
);
440 EXPORT_SYMBOL(ib_sa_cancel_query
);
442 static void init_mad(struct ib_sa_mad
*mad
, struct ib_mad_agent
*agent
)
446 memset(mad
, 0, sizeof *mad
);
448 mad
->mad_hdr
.base_version
= IB_MGMT_BASE_VERSION
;
449 mad
->mad_hdr
.mgmt_class
= IB_MGMT_CLASS_SUBN_ADM
;
450 mad
->mad_hdr
.class_version
= IB_SA_CLASS_VERSION
;
452 spin_lock_irqsave(&tid_lock
, flags
);
454 cpu_to_be64(((u64
) agent
->hi_tid
) << 32 | tid
++);
455 spin_unlock_irqrestore(&tid_lock
, flags
);
458 static int send_mad(struct ib_sa_query
*query
, int timeout_ms
)
460 struct ib_sa_port
*port
= query
->port
;
463 struct ib_sge gather_list
;
464 struct ib_send_wr
*bad_wr
, wr
= {
465 .opcode
= IB_WR_SEND
,
466 .sg_list
= &gather_list
,
468 .send_flags
= IB_SEND_SIGNALED
,
471 .mad_hdr
= &query
->mad
->mad_hdr
,
473 .remote_qkey
= IB_QP1_QKEY
,
474 .timeout_ms
= timeout_ms
,
480 if (!idr_pre_get(&query_idr
, GFP_ATOMIC
))
482 spin_lock_irqsave(&idr_lock
, flags
);
483 ret
= idr_get_new(&query_idr
, query
, &query
->id
);
484 spin_unlock_irqrestore(&idr_lock
, flags
);
490 wr
.wr_id
= query
->id
;
492 spin_lock_irqsave(&port
->ah_lock
, flags
);
493 kref_get(&port
->sm_ah
->ref
);
494 query
->sm_ah
= port
->sm_ah
;
495 wr
.wr
.ud
.ah
= port
->sm_ah
->ah
;
496 spin_unlock_irqrestore(&port
->ah_lock
, flags
);
498 gather_list
.addr
= dma_map_single(port
->agent
->device
->dma_device
,
500 sizeof (struct ib_sa_mad
),
502 gather_list
.length
= sizeof (struct ib_sa_mad
);
503 gather_list
.lkey
= port
->agent
->mr
->lkey
;
504 pci_unmap_addr_set(query
, mapping
, gather_list
.addr
);
506 ret
= ib_post_send_mad(port
->agent
, &wr
, &bad_wr
);
508 dma_unmap_single(port
->agent
->device
->dma_device
,
509 pci_unmap_addr(query
, mapping
),
510 sizeof (struct ib_sa_mad
),
512 kref_put(&query
->sm_ah
->ref
, free_sm_ah
);
513 spin_lock_irqsave(&idr_lock
, flags
);
514 idr_remove(&query_idr
, query
->id
);
515 spin_unlock_irqrestore(&idr_lock
, flags
);
519 * It's not safe to dereference query any more, because the
520 * send may already have completed and freed the query in
521 * another context. So use wr.wr_id, which has a copy of the
524 return ret
? ret
: wr
.wr_id
;
527 static void ib_sa_path_rec_callback(struct ib_sa_query
*sa_query
,
529 struct ib_sa_mad
*mad
)
531 struct ib_sa_path_query
*query
=
532 container_of(sa_query
, struct ib_sa_path_query
, sa_query
);
535 struct ib_sa_path_rec rec
;
537 ib_unpack(path_rec_table
, ARRAY_SIZE(path_rec_table
),
539 query
->callback(status
, &rec
, query
->context
);
541 query
->callback(status
, NULL
, query
->context
);
544 static void ib_sa_path_rec_release(struct ib_sa_query
*sa_query
)
546 kfree(sa_query
->mad
);
547 kfree(container_of(sa_query
, struct ib_sa_path_query
, sa_query
));
551 * ib_sa_path_rec_get - Start a Path get query
552 * @device:device to send query on
553 * @port_num: port number to send query on
554 * @rec:Path Record to send in query
555 * @comp_mask:component mask to send in query
556 * @timeout_ms:time to wait for response
557 * @gfp_mask:GFP mask to use for internal allocations
558 * @callback:function called when query completes, times out or is
560 * @context:opaque user context passed to callback
561 * @sa_query:query context, used to cancel query
563 * Send a Path Record Get query to the SA to look up a path. The
564 * callback function will be called when the query completes (or
565 * fails); status is 0 for a successful response, -EINTR if the query
566 * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
567 * occurred sending the query. The resp parameter of the callback is
568 * only valid if status is 0.
570 * If the return value of ib_sa_path_rec_get() is negative, it is an
571 * error code. Otherwise it is a query ID that can be used to cancel
574 int ib_sa_path_rec_get(struct ib_device
*device
, u8 port_num
,
575 struct ib_sa_path_rec
*rec
,
576 ib_sa_comp_mask comp_mask
,
577 int timeout_ms
, unsigned int __nocast gfp_mask
,
578 void (*callback
)(int status
,
579 struct ib_sa_path_rec
*resp
,
582 struct ib_sa_query
**sa_query
)
584 struct ib_sa_path_query
*query
;
585 struct ib_sa_device
*sa_dev
= ib_get_client_data(device
, &sa_client
);
586 struct ib_sa_port
*port
= &sa_dev
->port
[port_num
- sa_dev
->start_port
];
587 struct ib_mad_agent
*agent
= port
->agent
;
590 query
= kmalloc(sizeof *query
, gfp_mask
);
593 query
->sa_query
.mad
= kmalloc(sizeof *query
->sa_query
.mad
, gfp_mask
);
594 if (!query
->sa_query
.mad
) {
599 query
->callback
= callback
;
600 query
->context
= context
;
602 init_mad(query
->sa_query
.mad
, agent
);
604 query
->sa_query
.callback
= callback
? ib_sa_path_rec_callback
: NULL
;
605 query
->sa_query
.release
= ib_sa_path_rec_release
;
606 query
->sa_query
.port
= port
;
607 query
->sa_query
.mad
->mad_hdr
.method
= IB_MGMT_METHOD_GET
;
608 query
->sa_query
.mad
->mad_hdr
.attr_id
= cpu_to_be16(IB_SA_ATTR_PATH_REC
);
609 query
->sa_query
.mad
->sa_hdr
.comp_mask
= comp_mask
;
611 ib_pack(path_rec_table
, ARRAY_SIZE(path_rec_table
),
612 rec
, query
->sa_query
.mad
->data
);
614 *sa_query
= &query
->sa_query
;
616 ret
= send_mad(&query
->sa_query
, timeout_ms
);
619 kfree(query
->sa_query
.mad
);
625 EXPORT_SYMBOL(ib_sa_path_rec_get
);
627 static void ib_sa_service_rec_callback(struct ib_sa_query
*sa_query
,
629 struct ib_sa_mad
*mad
)
631 struct ib_sa_service_query
*query
=
632 container_of(sa_query
, struct ib_sa_service_query
, sa_query
);
635 struct ib_sa_service_rec rec
;
637 ib_unpack(service_rec_table
, ARRAY_SIZE(service_rec_table
),
639 query
->callback(status
, &rec
, query
->context
);
641 query
->callback(status
, NULL
, query
->context
);
644 static void ib_sa_service_rec_release(struct ib_sa_query
*sa_query
)
646 kfree(sa_query
->mad
);
647 kfree(container_of(sa_query
, struct ib_sa_service_query
, sa_query
));
651 * ib_sa_service_rec_query - Start Service Record operation
652 * @device:device to send request on
653 * @port_num: port number to send request on
654 * @method:SA method - should be get, set, or delete
655 * @rec:Service Record to send in request
656 * @comp_mask:component mask to send in request
657 * @timeout_ms:time to wait for response
658 * @gfp_mask:GFP mask to use for internal allocations
659 * @callback:function called when request completes, times out or is
661 * @context:opaque user context passed to callback
662 * @sa_query:request context, used to cancel request
664 * Send a Service Record set/get/delete to the SA to register,
665 * unregister or query a service record.
666 * The callback function will be called when the request completes (or
667 * fails); status is 0 for a successful response, -EINTR if the query
668 * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
669 * occurred sending the query. The resp parameter of the callback is
670 * only valid if status is 0.
672 * If the return value of ib_sa_service_rec_query() is negative, it is an
673 * error code. Otherwise it is a request ID that can be used to cancel
676 int ib_sa_service_rec_query(struct ib_device
*device
, u8 port_num
, u8 method
,
677 struct ib_sa_service_rec
*rec
,
678 ib_sa_comp_mask comp_mask
,
679 int timeout_ms
, unsigned int __nocast gfp_mask
,
680 void (*callback
)(int status
,
681 struct ib_sa_service_rec
*resp
,
684 struct ib_sa_query
**sa_query
)
686 struct ib_sa_service_query
*query
;
687 struct ib_sa_device
*sa_dev
= ib_get_client_data(device
, &sa_client
);
688 struct ib_sa_port
*port
= &sa_dev
->port
[port_num
- sa_dev
->start_port
];
689 struct ib_mad_agent
*agent
= port
->agent
;
692 if (method
!= IB_MGMT_METHOD_GET
&&
693 method
!= IB_MGMT_METHOD_SET
&&
694 method
!= IB_SA_METHOD_DELETE
)
697 query
= kmalloc(sizeof *query
, gfp_mask
);
700 query
->sa_query
.mad
= kmalloc(sizeof *query
->sa_query
.mad
, gfp_mask
);
701 if (!query
->sa_query
.mad
) {
706 query
->callback
= callback
;
707 query
->context
= context
;
709 init_mad(query
->sa_query
.mad
, agent
);
711 query
->sa_query
.callback
= callback
? ib_sa_service_rec_callback
: NULL
;
712 query
->sa_query
.release
= ib_sa_service_rec_release
;
713 query
->sa_query
.port
= port
;
714 query
->sa_query
.mad
->mad_hdr
.method
= method
;
715 query
->sa_query
.mad
->mad_hdr
.attr_id
=
716 cpu_to_be16(IB_SA_ATTR_SERVICE_REC
);
717 query
->sa_query
.mad
->sa_hdr
.comp_mask
= comp_mask
;
719 ib_pack(service_rec_table
, ARRAY_SIZE(service_rec_table
),
720 rec
, query
->sa_query
.mad
->data
);
722 *sa_query
= &query
->sa_query
;
724 ret
= send_mad(&query
->sa_query
, timeout_ms
);
727 kfree(query
->sa_query
.mad
);
733 EXPORT_SYMBOL(ib_sa_service_rec_query
);
735 static void ib_sa_mcmember_rec_callback(struct ib_sa_query
*sa_query
,
737 struct ib_sa_mad
*mad
)
739 struct ib_sa_mcmember_query
*query
=
740 container_of(sa_query
, struct ib_sa_mcmember_query
, sa_query
);
743 struct ib_sa_mcmember_rec rec
;
745 ib_unpack(mcmember_rec_table
, ARRAY_SIZE(mcmember_rec_table
),
747 query
->callback(status
, &rec
, query
->context
);
749 query
->callback(status
, NULL
, query
->context
);
752 static void ib_sa_mcmember_rec_release(struct ib_sa_query
*sa_query
)
754 kfree(sa_query
->mad
);
755 kfree(container_of(sa_query
, struct ib_sa_mcmember_query
, sa_query
));
758 int ib_sa_mcmember_rec_query(struct ib_device
*device
, u8 port_num
,
760 struct ib_sa_mcmember_rec
*rec
,
761 ib_sa_comp_mask comp_mask
,
762 int timeout_ms
, unsigned int __nocast gfp_mask
,
763 void (*callback
)(int status
,
764 struct ib_sa_mcmember_rec
*resp
,
767 struct ib_sa_query
**sa_query
)
769 struct ib_sa_mcmember_query
*query
;
770 struct ib_sa_device
*sa_dev
= ib_get_client_data(device
, &sa_client
);
771 struct ib_sa_port
*port
= &sa_dev
->port
[port_num
- sa_dev
->start_port
];
772 struct ib_mad_agent
*agent
= port
->agent
;
775 query
= kmalloc(sizeof *query
, gfp_mask
);
778 query
->sa_query
.mad
= kmalloc(sizeof *query
->sa_query
.mad
, gfp_mask
);
779 if (!query
->sa_query
.mad
) {
784 query
->callback
= callback
;
785 query
->context
= context
;
787 init_mad(query
->sa_query
.mad
, agent
);
789 query
->sa_query
.callback
= callback
? ib_sa_mcmember_rec_callback
: NULL
;
790 query
->sa_query
.release
= ib_sa_mcmember_rec_release
;
791 query
->sa_query
.port
= port
;
792 query
->sa_query
.mad
->mad_hdr
.method
= method
;
793 query
->sa_query
.mad
->mad_hdr
.attr_id
= cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC
);
794 query
->sa_query
.mad
->sa_hdr
.comp_mask
= comp_mask
;
796 ib_pack(mcmember_rec_table
, ARRAY_SIZE(mcmember_rec_table
),
797 rec
, query
->sa_query
.mad
->data
);
799 *sa_query
= &query
->sa_query
;
801 ret
= send_mad(&query
->sa_query
, timeout_ms
);
804 kfree(query
->sa_query
.mad
);
810 EXPORT_SYMBOL(ib_sa_mcmember_rec_query
);
812 static void send_handler(struct ib_mad_agent
*agent
,
813 struct ib_mad_send_wc
*mad_send_wc
)
815 struct ib_sa_query
*query
;
818 spin_lock_irqsave(&idr_lock
, flags
);
819 query
= idr_find(&query_idr
, mad_send_wc
->wr_id
);
820 spin_unlock_irqrestore(&idr_lock
, flags
);
826 switch (mad_send_wc
->status
) {
828 /* No callback -- already got recv */
830 case IB_WC_RESP_TIMEOUT_ERR
:
831 query
->callback(query
, -ETIMEDOUT
, NULL
);
833 case IB_WC_WR_FLUSH_ERR
:
834 query
->callback(query
, -EINTR
, NULL
);
837 query
->callback(query
, -EIO
, NULL
);
841 dma_unmap_single(agent
->device
->dma_device
,
842 pci_unmap_addr(query
, mapping
),
843 sizeof (struct ib_sa_mad
),
845 kref_put(&query
->sm_ah
->ref
, free_sm_ah
);
847 query
->release(query
);
849 spin_lock_irqsave(&idr_lock
, flags
);
850 idr_remove(&query_idr
, mad_send_wc
->wr_id
);
851 spin_unlock_irqrestore(&idr_lock
, flags
);
854 static void recv_handler(struct ib_mad_agent
*mad_agent
,
855 struct ib_mad_recv_wc
*mad_recv_wc
)
857 struct ib_sa_query
*query
;
860 spin_lock_irqsave(&idr_lock
, flags
);
861 query
= idr_find(&query_idr
, mad_recv_wc
->wc
->wr_id
);
862 spin_unlock_irqrestore(&idr_lock
, flags
);
864 if (query
&& query
->callback
) {
865 if (mad_recv_wc
->wc
->status
== IB_WC_SUCCESS
)
866 query
->callback(query
,
867 mad_recv_wc
->recv_buf
.mad
->mad_hdr
.status
?
869 (struct ib_sa_mad
*) mad_recv_wc
->recv_buf
.mad
);
871 query
->callback(query
, -EIO
, NULL
);
874 ib_free_recv_mad(mad_recv_wc
);
877 static void ib_sa_add_one(struct ib_device
*device
)
879 struct ib_sa_device
*sa_dev
;
882 if (device
->node_type
== IB_NODE_SWITCH
)
886 e
= device
->phys_port_cnt
;
889 sa_dev
= kmalloc(sizeof *sa_dev
+
890 (e
- s
+ 1) * sizeof (struct ib_sa_port
),
895 sa_dev
->start_port
= s
;
896 sa_dev
->end_port
= e
;
898 for (i
= 0; i
<= e
- s
; ++i
) {
899 sa_dev
->port
[i
].sm_ah
= NULL
;
900 sa_dev
->port
[i
].port_num
= i
+ s
;
901 spin_lock_init(&sa_dev
->port
[i
].ah_lock
);
903 sa_dev
->port
[i
].agent
=
904 ib_register_mad_agent(device
, i
+ s
, IB_QPT_GSI
,
905 NULL
, 0, send_handler
,
906 recv_handler
, sa_dev
);
907 if (IS_ERR(sa_dev
->port
[i
].agent
))
910 INIT_WORK(&sa_dev
->port
[i
].update_task
,
911 update_sm_ah
, &sa_dev
->port
[i
]);
914 ib_set_client_data(device
, &sa_client
, sa_dev
);
917 * We register our event handler after everything is set up,
918 * and then update our cached info after the event handler is
919 * registered to avoid any problems if a port changes state
920 * during our initialization.
923 INIT_IB_EVENT_HANDLER(&sa_dev
->event_handler
, device
, ib_sa_event
);
924 if (ib_register_event_handler(&sa_dev
->event_handler
))
927 for (i
= 0; i
<= e
- s
; ++i
)
928 update_sm_ah(&sa_dev
->port
[i
]);
934 ib_unregister_mad_agent(sa_dev
->port
[i
].agent
);
941 static void ib_sa_remove_one(struct ib_device
*device
)
943 struct ib_sa_device
*sa_dev
= ib_get_client_data(device
, &sa_client
);
949 ib_unregister_event_handler(&sa_dev
->event_handler
);
951 for (i
= 0; i
<= sa_dev
->end_port
- sa_dev
->start_port
; ++i
) {
952 ib_unregister_mad_agent(sa_dev
->port
[i
].agent
);
953 kref_put(&sa_dev
->port
[i
].sm_ah
->ref
, free_sm_ah
);
959 static int __init
ib_sa_init(void)
963 spin_lock_init(&idr_lock
);
964 spin_lock_init(&tid_lock
);
966 get_random_bytes(&tid
, sizeof tid
);
968 ret
= ib_register_client(&sa_client
);
970 printk(KERN_ERR
"Couldn't register ib_sa client\n");
975 static void __exit
ib_sa_cleanup(void)
977 ib_unregister_client(&sa_client
);
980 module_init(ib_sa_init
);
981 module_exit(ib_sa_cleanup
);