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;
117 IB_SA_ATTR_CLASS_PORTINFO = 0x01,
118 IB_SA_ATTR_NOTICE = 0x02,
119 IB_SA_ATTR_INFORM_INFO = 0x03,
120 IB_SA_ATTR_NODE_REC = 0x11,
121 IB_SA_ATTR_PORT_INFO_REC = 0x12,
122 IB_SA_ATTR_SL2VL_REC = 0x13,
123 IB_SA_ATTR_SWITCH_REC = 0x14,
124 IB_SA_ATTR_LINEAR_FDB_REC = 0x15,
125 IB_SA_ATTR_RANDOM_FDB_REC = 0x16,
126 IB_SA_ATTR_MCAST_FDB_REC = 0x17,
127 IB_SA_ATTR_SM_INFO_REC = 0x18,
128 IB_SA_ATTR_LINK_REC = 0x20,
129 IB_SA_ATTR_GUID_INFO_REC = 0x30,
130 IB_SA_ATTR_SERVICE_REC = 0x31,
131 IB_SA_ATTR_PARTITION_REC = 0x33,
132 IB_SA_ATTR_RANGE_REC = 0x34,
133 IB_SA_ATTR_PATH_REC = 0x35,
134 IB_SA_ATTR_VL_ARB_REC = 0x36,
135 IB_SA_ATTR_MC_GROUP_REC = 0x37,
136 IB_SA_ATTR_MC_MEMBER_REC = 0x38,
137 IB_SA_ATTR_TRACE_REC = 0x39,
138 IB_SA_ATTR_MULTI_PATH_REC = 0x3a,
139 IB_SA_ATTR_SERVICE_ASSOC_REC = 0x3b
142 #define PATH_REC_FIELD(field) \
143 .struct_offset_bytes = offsetof(struct ib_sa_path_rec, field), \
144 .struct_size_bytes = sizeof ((struct ib_sa_path_rec *) 0)->field, \
145 .field_name = "sa_path_rec:" #field
147 static const struct ib_field path_rec_table[] = {
156 { PATH_REC_FIELD(dgid),
160 { PATH_REC_FIELD(sgid),
164 { PATH_REC_FIELD(dlid),
168 { PATH_REC_FIELD(slid),
172 { PATH_REC_FIELD(raw_traffic),
180 { PATH_REC_FIELD(flow_label),
184 { PATH_REC_FIELD(hop_limit),
188 { PATH_REC_FIELD(traffic_class),
192 { PATH_REC_FIELD(reversible),
196 { PATH_REC_FIELD(numb_path),
200 { PATH_REC_FIELD(pkey),
208 { PATH_REC_FIELD(sl),
212 { PATH_REC_FIELD(mtu_selector),
216 { PATH_REC_FIELD(mtu),
220 { PATH_REC_FIELD(rate_selector),
224 { PATH_REC_FIELD(rate),
228 { PATH_REC_FIELD(packet_life_time_selector),
232 { PATH_REC_FIELD(packet_life_time),
236 { PATH_REC_FIELD(preference),
246 #define MCMEMBER_REC_FIELD(field) \
247 .struct_offset_bytes = offsetof(struct ib_sa_mcmember_rec, field), \
248 .struct_size_bytes = sizeof ((struct ib_sa_mcmember_rec *) 0)->field, \
249 .field_name = "sa_mcmember_rec:" #field
251 static const struct ib_field mcmember_rec_table[] = {
252 { MCMEMBER_REC_FIELD(mgid),
256 { MCMEMBER_REC_FIELD(port_gid),
260 { MCMEMBER_REC_FIELD(qkey),
264 { MCMEMBER_REC_FIELD(mlid),
268 { MCMEMBER_REC_FIELD(mtu_selector),
272 { MCMEMBER_REC_FIELD(mtu),
276 { MCMEMBER_REC_FIELD(traffic_class),
280 { MCMEMBER_REC_FIELD(pkey),
284 { MCMEMBER_REC_FIELD(rate_selector),
288 { MCMEMBER_REC_FIELD(rate),
292 { MCMEMBER_REC_FIELD(packet_life_time_selector),
296 { MCMEMBER_REC_FIELD(packet_life_time),
300 { MCMEMBER_REC_FIELD(sl),
304 { MCMEMBER_REC_FIELD(flow_label),
308 { MCMEMBER_REC_FIELD(hop_limit),
312 { MCMEMBER_REC_FIELD(scope),
316 { MCMEMBER_REC_FIELD(join_state),
320 { MCMEMBER_REC_FIELD(proxy_join),
330 #define SERVICE_REC_FIELD(field) \
331 .struct_offset_bytes = offsetof(struct ib_sa_service_rec, field), \
332 .struct_size_bytes = sizeof ((struct ib_sa_service_rec *) 0)->field, \
333 .field_name = "sa_service_rec:" #field
335 static const struct ib_field service_rec_table[] = {
336 { SERVICE_REC_FIELD(id),
340 { SERVICE_REC_FIELD(gid),
344 { SERVICE_REC_FIELD(pkey),
348 { SERVICE_REC_FIELD(lease),
352 { SERVICE_REC_FIELD(key),
356 { SERVICE_REC_FIELD(name),
360 { SERVICE_REC_FIELD(data8),
364 { SERVICE_REC_FIELD(data16),
368 { SERVICE_REC_FIELD(data32),
372 { SERVICE_REC_FIELD(data64),
378 static void free_sm_ah(struct kref *kref)
380 struct ib_sa_sm_ah *sm_ah = container_of(kref, struct ib_sa_sm_ah, ref);
382 ib_destroy_ah(sm_ah->ah);
386 static void update_sm_ah(void *port_ptr)
388 struct ib_sa_port *port = port_ptr;
389 struct ib_sa_sm_ah *new_ah, *old_ah;
390 struct ib_port_attr port_attr;
391 struct ib_ah_attr ah_attr;
393 if (ib_query_port(port->agent->device, port->port_num, &port_attr)) {
394 printk(KERN_WARNING "Couldn't query port\n");
398 new_ah = kmalloc(sizeof *new_ah, GFP_KERNEL);
400 printk(KERN_WARNING "Couldn't allocate new SM AH\n");
404 kref_init(&new_ah->ref);
406 memset(&ah_attr, 0, sizeof ah_attr);
407 ah_attr.dlid = port_attr.sm_lid;
408 ah_attr.sl = port_attr.sm_sl;
409 ah_attr.port_num = port->port_num;
411 new_ah->ah = ib_create_ah(port->agent->qp->pd, &ah_attr);
412 if (IS_ERR(new_ah->ah)) {
413 printk(KERN_WARNING "Couldn't create new SM AH\n");
418 spin_lock_irq(&port->ah_lock);
419 old_ah = port->sm_ah;
420 port->sm_ah = new_ah;
421 spin_unlock_irq(&port->ah_lock);
424 kref_put(&old_ah->ref, free_sm_ah);
427 static void ib_sa_event(struct ib_event_handler *handler, struct ib_event *event)
429 if (event->event == IB_EVENT_PORT_ERR ||
430 event->event == IB_EVENT_PORT_ACTIVE ||
431 event->event == IB_EVENT_LID_CHANGE ||
432 event->event == IB_EVENT_PKEY_CHANGE ||
433 event->event == IB_EVENT_SM_CHANGE) {
434 struct ib_sa_device *sa_dev =
435 ib_get_client_data(event->device, &sa_client);
437 schedule_work(&sa_dev->port[event->element.port_num -
438 sa_dev->start_port].update_task);
443 * ib_sa_cancel_query - try to cancel an SA query
444 * @id:ID of query to cancel
445 * @query:query pointer to cancel
447 * Try to cancel an SA query. If the id and query don't match up or
448 * the query has already completed, nothing is done. Otherwise the
449 * query is canceled and will complete with a status of -EINTR.
451 void ib_sa_cancel_query(int id, struct ib_sa_query *query)
454 struct ib_mad_agent *agent;
456 spin_lock_irqsave(&idr_lock, flags);
457 if (idr_find(&query_idr, id) != query) {
458 spin_unlock_irqrestore(&idr_lock, flags);
461 agent = query->port->agent;
462 spin_unlock_irqrestore(&idr_lock, flags);
464 ib_cancel_mad(agent, id);
466 EXPORT_SYMBOL(ib_sa_cancel_query);
468 static void init_mad(struct ib_sa_mad *mad, struct ib_mad_agent *agent)
472 memset(mad, 0, sizeof *mad);
474 mad->mad_hdr.base_version = IB_MGMT_BASE_VERSION;
475 mad->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
476 mad->mad_hdr.class_version = IB_SA_CLASS_VERSION;
478 spin_lock_irqsave(&tid_lock, flags);
480 cpu_to_be64(((u64) agent->hi_tid) << 32 | tid++);
481 spin_unlock_irqrestore(&tid_lock, flags);
484 static int send_mad(struct ib_sa_query *query, int timeout_ms)
486 struct ib_sa_port *port = query->port;
489 struct ib_sge gather_list;
490 struct ib_send_wr *bad_wr, wr = {
491 .opcode = IB_WR_SEND,
492 .sg_list = &gather_list,
494 .send_flags = IB_SEND_SIGNALED,
497 .mad_hdr = &query->mad->mad_hdr,
499 .remote_qkey = IB_QP1_QKEY,
500 .timeout_ms = timeout_ms,
506 if (!idr_pre_get(&query_idr, GFP_ATOMIC))
508 spin_lock_irqsave(&idr_lock, flags);
509 ret = idr_get_new(&query_idr, query, &query->id);
510 spin_unlock_irqrestore(&idr_lock, flags);
516 wr.wr_id = query->id;
518 spin_lock_irqsave(&port->ah_lock, flags);
519 kref_get(&port->sm_ah->ref);
520 query->sm_ah = port->sm_ah;
521 wr.wr.ud.ah = port->sm_ah->ah;
522 spin_unlock_irqrestore(&port->ah_lock, flags);
524 gather_list.addr = dma_map_single(port->agent->device->dma_device,
526 sizeof (struct ib_sa_mad),
528 gather_list.length = sizeof (struct ib_sa_mad);
529 gather_list.lkey = port->agent->mr->lkey;
530 pci_unmap_addr_set(query, mapping, gather_list.addr);
532 ret = ib_post_send_mad(port->agent, &wr, &bad_wr);
534 dma_unmap_single(port->agent->device->dma_device,
535 pci_unmap_addr(query, mapping),
536 sizeof (struct ib_sa_mad),
538 kref_put(&query->sm_ah->ref, free_sm_ah);
539 spin_lock_irqsave(&idr_lock, flags);
540 idr_remove(&query_idr, query->id);
541 spin_unlock_irqrestore(&idr_lock, flags);
545 * It's not safe to dereference query any more, because the
546 * send may already have completed and freed the query in
547 * another context. So use wr.wr_id, which has a copy of the
550 return ret ? ret : wr.wr_id;
553 static void ib_sa_path_rec_callback(struct ib_sa_query *sa_query,
555 struct ib_sa_mad *mad)
557 struct ib_sa_path_query *query =
558 container_of(sa_query, struct ib_sa_path_query, sa_query);
561 struct ib_sa_path_rec rec;
563 ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table),
565 query->callback(status, &rec, query->context);
567 query->callback(status, NULL, query->context);
570 static void ib_sa_path_rec_release(struct ib_sa_query *sa_query)
572 kfree(sa_query->mad);
573 kfree(container_of(sa_query, struct ib_sa_path_query, sa_query));
577 * ib_sa_path_rec_get - Start a Path get query
578 * @device:device to send query on
579 * @port_num: port number to send query on
580 * @rec:Path Record to send in query
581 * @comp_mask:component mask to send in query
582 * @timeout_ms:time to wait for response
583 * @gfp_mask:GFP mask to use for internal allocations
584 * @callback:function called when query completes, times out or is
586 * @context:opaque user context passed to callback
587 * @sa_query:query context, used to cancel query
589 * Send a Path Record Get query to the SA to look up a path. The
590 * callback function will be called when the query completes (or
591 * fails); status is 0 for a successful response, -EINTR if the query
592 * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
593 * occurred sending the query. The resp parameter of the callback is
594 * only valid if status is 0.
596 * If the return value of ib_sa_path_rec_get() is negative, it is an
597 * error code. Otherwise it is a query ID that can be used to cancel
600 int ib_sa_path_rec_get(struct ib_device *device, u8 port_num,
601 struct ib_sa_path_rec *rec,
602 ib_sa_comp_mask comp_mask,
603 int timeout_ms, unsigned int __nocast gfp_mask,
604 void (*callback)(int status,
605 struct ib_sa_path_rec *resp,
608 struct ib_sa_query **sa_query)
610 struct ib_sa_path_query *query;
611 struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
612 struct ib_sa_port *port = &sa_dev->port[port_num - sa_dev->start_port];
613 struct ib_mad_agent *agent = port->agent;
616 query = kmalloc(sizeof *query, gfp_mask);
619 query->sa_query.mad = kmalloc(sizeof *query->sa_query.mad, gfp_mask);
620 if (!query->sa_query.mad) {
625 query->callback = callback;
626 query->context = context;
628 init_mad(query->sa_query.mad, agent);
630 query->sa_query.callback = callback ? ib_sa_path_rec_callback : NULL;
631 query->sa_query.release = ib_sa_path_rec_release;
632 query->sa_query.port = port;
633 query->sa_query.mad->mad_hdr.method = IB_MGMT_METHOD_GET;
634 query->sa_query.mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_PATH_REC);
635 query->sa_query.mad->sa_hdr.comp_mask = comp_mask;
637 ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table),
638 rec, query->sa_query.mad->data);
640 *sa_query = &query->sa_query;
642 ret = send_mad(&query->sa_query, timeout_ms);
645 kfree(query->sa_query.mad);
651 EXPORT_SYMBOL(ib_sa_path_rec_get);
653 static void ib_sa_service_rec_callback(struct ib_sa_query *sa_query,
655 struct ib_sa_mad *mad)
657 struct ib_sa_service_query *query =
658 container_of(sa_query, struct ib_sa_service_query, sa_query);
661 struct ib_sa_service_rec rec;
663 ib_unpack(service_rec_table, ARRAY_SIZE(service_rec_table),
665 query->callback(status, &rec, query->context);
667 query->callback(status, NULL, query->context);
670 static void ib_sa_service_rec_release(struct ib_sa_query *sa_query)
672 kfree(sa_query->mad);
673 kfree(container_of(sa_query, struct ib_sa_service_query, sa_query));
677 * ib_sa_service_rec_query - Start Service Record operation
678 * @device:device to send request on
679 * @port_num: port number to send request on
680 * @method:SA method - should be get, set, or delete
681 * @rec:Service Record to send in request
682 * @comp_mask:component mask to send in request
683 * @timeout_ms:time to wait for response
684 * @gfp_mask:GFP mask to use for internal allocations
685 * @callback:function called when request completes, times out or is
687 * @context:opaque user context passed to callback
688 * @sa_query:request context, used to cancel request
690 * Send a Service Record set/get/delete to the SA to register,
691 * unregister or query a service record.
692 * The callback function will be called when the request completes (or
693 * fails); status is 0 for a successful response, -EINTR if the query
694 * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
695 * occurred sending the query. The resp parameter of the callback is
696 * only valid if status is 0.
698 * If the return value of ib_sa_service_rec_query() is negative, it is an
699 * error code. Otherwise it is a request ID that can be used to cancel
702 int ib_sa_service_rec_query(struct ib_device *device, u8 port_num, u8 method,
703 struct ib_sa_service_rec *rec,
704 ib_sa_comp_mask comp_mask,
705 int timeout_ms, unsigned int __nocast gfp_mask,
706 void (*callback)(int status,
707 struct ib_sa_service_rec *resp,
710 struct ib_sa_query **sa_query)
712 struct ib_sa_service_query *query;
713 struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
714 struct ib_sa_port *port = &sa_dev->port[port_num - sa_dev->start_port];
715 struct ib_mad_agent *agent = port->agent;
718 if (method != IB_MGMT_METHOD_GET &&
719 method != IB_MGMT_METHOD_SET &&
720 method != IB_SA_METHOD_DELETE)
723 query = kmalloc(sizeof *query, gfp_mask);
726 query->sa_query.mad = kmalloc(sizeof *query->sa_query.mad, gfp_mask);
727 if (!query->sa_query.mad) {
732 query->callback = callback;
733 query->context = context;
735 init_mad(query->sa_query.mad, agent);
737 query->sa_query.callback = callback ? ib_sa_service_rec_callback : NULL;
738 query->sa_query.release = ib_sa_service_rec_release;
739 query->sa_query.port = port;
740 query->sa_query.mad->mad_hdr.method = method;
741 query->sa_query.mad->mad_hdr.attr_id =
742 cpu_to_be16(IB_SA_ATTR_SERVICE_REC);
743 query->sa_query.mad->sa_hdr.comp_mask = comp_mask;
745 ib_pack(service_rec_table, ARRAY_SIZE(service_rec_table),
746 rec, query->sa_query.mad->data);
748 *sa_query = &query->sa_query;
750 ret = send_mad(&query->sa_query, timeout_ms);
753 kfree(query->sa_query.mad);
759 EXPORT_SYMBOL(ib_sa_service_rec_query);
761 static void ib_sa_mcmember_rec_callback(struct ib_sa_query *sa_query,
763 struct ib_sa_mad *mad)
765 struct ib_sa_mcmember_query *query =
766 container_of(sa_query, struct ib_sa_mcmember_query, sa_query);
769 struct ib_sa_mcmember_rec rec;
771 ib_unpack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
773 query->callback(status, &rec, query->context);
775 query->callback(status, NULL, query->context);
778 static void ib_sa_mcmember_rec_release(struct ib_sa_query *sa_query)
780 kfree(sa_query->mad);
781 kfree(container_of(sa_query, struct ib_sa_mcmember_query, sa_query));
784 int ib_sa_mcmember_rec_query(struct ib_device *device, u8 port_num,
786 struct ib_sa_mcmember_rec *rec,
787 ib_sa_comp_mask comp_mask,
788 int timeout_ms, unsigned int __nocast gfp_mask,
789 void (*callback)(int status,
790 struct ib_sa_mcmember_rec *resp,
793 struct ib_sa_query **sa_query)
795 struct ib_sa_mcmember_query *query;
796 struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
797 struct ib_sa_port *port = &sa_dev->port[port_num - sa_dev->start_port];
798 struct ib_mad_agent *agent = port->agent;
801 query = kmalloc(sizeof *query, gfp_mask);
804 query->sa_query.mad = kmalloc(sizeof *query->sa_query.mad, gfp_mask);
805 if (!query->sa_query.mad) {
810 query->callback = callback;
811 query->context = context;
813 init_mad(query->sa_query.mad, agent);
815 query->sa_query.callback = callback ? ib_sa_mcmember_rec_callback : NULL;
816 query->sa_query.release = ib_sa_mcmember_rec_release;
817 query->sa_query.port = port;
818 query->sa_query.mad->mad_hdr.method = method;
819 query->sa_query.mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC);
820 query->sa_query.mad->sa_hdr.comp_mask = comp_mask;
822 ib_pack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
823 rec, query->sa_query.mad->data);
825 *sa_query = &query->sa_query;
827 ret = send_mad(&query->sa_query, timeout_ms);
830 kfree(query->sa_query.mad);
836 EXPORT_SYMBOL(ib_sa_mcmember_rec_query);
838 static void send_handler(struct ib_mad_agent *agent,
839 struct ib_mad_send_wc *mad_send_wc)
841 struct ib_sa_query *query;
844 spin_lock_irqsave(&idr_lock, flags);
845 query = idr_find(&query_idr, mad_send_wc->wr_id);
846 spin_unlock_irqrestore(&idr_lock, flags);
852 switch (mad_send_wc->status) {
854 /* No callback -- already got recv */
856 case IB_WC_RESP_TIMEOUT_ERR:
857 query->callback(query, -ETIMEDOUT, NULL);
859 case IB_WC_WR_FLUSH_ERR:
860 query->callback(query, -EINTR, NULL);
863 query->callback(query, -EIO, NULL);
867 dma_unmap_single(agent->device->dma_device,
868 pci_unmap_addr(query, mapping),
869 sizeof (struct ib_sa_mad),
871 kref_put(&query->sm_ah->ref, free_sm_ah);
873 query->release(query);
875 spin_lock_irqsave(&idr_lock, flags);
876 idr_remove(&query_idr, mad_send_wc->wr_id);
877 spin_unlock_irqrestore(&idr_lock, flags);
880 static void recv_handler(struct ib_mad_agent *mad_agent,
881 struct ib_mad_recv_wc *mad_recv_wc)
883 struct ib_sa_query *query;
886 spin_lock_irqsave(&idr_lock, flags);
887 query = idr_find(&query_idr, mad_recv_wc->wc->wr_id);
888 spin_unlock_irqrestore(&idr_lock, flags);
890 if (query && query->callback) {
891 if (mad_recv_wc->wc->status == IB_WC_SUCCESS)
892 query->callback(query,
893 mad_recv_wc->recv_buf.mad->mad_hdr.status ?
895 (struct ib_sa_mad *) mad_recv_wc->recv_buf.mad);
897 query->callback(query, -EIO, NULL);
900 ib_free_recv_mad(mad_recv_wc);
903 static void ib_sa_add_one(struct ib_device *device)
905 struct ib_sa_device *sa_dev;
908 if (device->node_type == IB_NODE_SWITCH)
912 e = device->phys_port_cnt;
915 sa_dev = kmalloc(sizeof *sa_dev +
916 (e - s + 1) * sizeof (struct ib_sa_port),
921 sa_dev->start_port = s;
922 sa_dev->end_port = e;
924 for (i = 0; i <= e - s; ++i) {
925 sa_dev->port[i].sm_ah = NULL;
926 sa_dev->port[i].port_num = i + s;
927 spin_lock_init(&sa_dev->port[i].ah_lock);
929 sa_dev->port[i].agent =
930 ib_register_mad_agent(device, i + s, IB_QPT_GSI,
931 NULL, 0, send_handler,
932 recv_handler, sa_dev);
933 if (IS_ERR(sa_dev->port[i].agent))
936 INIT_WORK(&sa_dev->port[i].update_task,
937 update_sm_ah, &sa_dev->port[i]);
940 ib_set_client_data(device, &sa_client, sa_dev);
943 * We register our event handler after everything is set up,
944 * and then update our cached info after the event handler is
945 * registered to avoid any problems if a port changes state
946 * during our initialization.
949 INIT_IB_EVENT_HANDLER(&sa_dev->event_handler, device, ib_sa_event);
950 if (ib_register_event_handler(&sa_dev->event_handler))
953 for (i = 0; i <= e - s; ++i)
954 update_sm_ah(&sa_dev->port[i]);
960 ib_unregister_mad_agent(sa_dev->port[i].agent);
967 static void ib_sa_remove_one(struct ib_device *device)
969 struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
975 ib_unregister_event_handler(&sa_dev->event_handler);
977 for (i = 0; i <= sa_dev->end_port - sa_dev->start_port; ++i) {
978 ib_unregister_mad_agent(sa_dev->port[i].agent);
979 kref_put(&sa_dev->port[i].sm_ah->ref, free_sm_ah);
985 static int __init ib_sa_init(void)
989 spin_lock_init(&idr_lock);
990 spin_lock_init(&tid_lock);
992 get_random_bytes(&tid, sizeof tid);
994 ret = ib_register_client(&sa_client);
996 printk(KERN_ERR "Couldn't register ib_sa client\n");
1001 static void __exit ib_sa_cleanup(void)
1003 ib_unregister_client(&sa_client);
1006 module_init(ib_sa_init);
1007 module_exit(ib_sa_cleanup);