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>
46 #include <linux/workqueue.h>
48 #include <rdma/ib_pack.h>
49 #include <rdma/ib_sa.h>
50 #include <rdma/ib_cache.h>
52 MODULE_AUTHOR("Roland Dreier");
53 MODULE_DESCRIPTION("InfiniBand subnet administration query support");
54 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_port *port;
79 struct ib_mad_send_buf *mad_buf;
80 struct ib_sa_sm_ah *sm_ah;
84 struct ib_sa_service_query {
85 void (*callback)(int, struct ib_sa_service_rec *, void *);
87 struct ib_sa_query sa_query;
90 struct ib_sa_path_query {
91 void (*callback)(int, struct ib_sa_path_rec *, void *);
93 struct ib_sa_query sa_query;
96 struct ib_sa_mcmember_query {
97 void (*callback)(int, struct ib_sa_mcmember_rec *, void *);
99 struct ib_sa_query sa_query;
102 static void ib_sa_add_one(struct ib_device *device);
103 static void ib_sa_remove_one(struct ib_device *device);
105 static struct ib_client sa_client = {
107 .add = ib_sa_add_one,
108 .remove = ib_sa_remove_one
111 static spinlock_t idr_lock;
112 static DEFINE_IDR(query_idr);
114 static spinlock_t tid_lock;
117 #define PATH_REC_FIELD(field) \
118 .struct_offset_bytes = offsetof(struct ib_sa_path_rec, field), \
119 .struct_size_bytes = sizeof ((struct ib_sa_path_rec *) 0)->field, \
120 .field_name = "sa_path_rec:" #field
122 static const struct ib_field path_rec_table[] = {
131 { PATH_REC_FIELD(dgid),
135 { PATH_REC_FIELD(sgid),
139 { PATH_REC_FIELD(dlid),
143 { PATH_REC_FIELD(slid),
147 { PATH_REC_FIELD(raw_traffic),
155 { PATH_REC_FIELD(flow_label),
159 { PATH_REC_FIELD(hop_limit),
163 { PATH_REC_FIELD(traffic_class),
167 { PATH_REC_FIELD(reversible),
171 { PATH_REC_FIELD(numb_path),
175 { PATH_REC_FIELD(pkey),
183 { PATH_REC_FIELD(sl),
187 { PATH_REC_FIELD(mtu_selector),
191 { PATH_REC_FIELD(mtu),
195 { PATH_REC_FIELD(rate_selector),
199 { PATH_REC_FIELD(rate),
203 { PATH_REC_FIELD(packet_life_time_selector),
207 { PATH_REC_FIELD(packet_life_time),
211 { PATH_REC_FIELD(preference),
221 #define MCMEMBER_REC_FIELD(field) \
222 .struct_offset_bytes = offsetof(struct ib_sa_mcmember_rec, field), \
223 .struct_size_bytes = sizeof ((struct ib_sa_mcmember_rec *) 0)->field, \
224 .field_name = "sa_mcmember_rec:" #field
226 static const struct ib_field mcmember_rec_table[] = {
227 { MCMEMBER_REC_FIELD(mgid),
231 { MCMEMBER_REC_FIELD(port_gid),
235 { MCMEMBER_REC_FIELD(qkey),
239 { MCMEMBER_REC_FIELD(mlid),
243 { MCMEMBER_REC_FIELD(mtu_selector),
247 { MCMEMBER_REC_FIELD(mtu),
251 { MCMEMBER_REC_FIELD(traffic_class),
255 { MCMEMBER_REC_FIELD(pkey),
259 { MCMEMBER_REC_FIELD(rate_selector),
263 { MCMEMBER_REC_FIELD(rate),
267 { MCMEMBER_REC_FIELD(packet_life_time_selector),
271 { MCMEMBER_REC_FIELD(packet_life_time),
275 { MCMEMBER_REC_FIELD(sl),
279 { MCMEMBER_REC_FIELD(flow_label),
283 { MCMEMBER_REC_FIELD(hop_limit),
287 { MCMEMBER_REC_FIELD(scope),
291 { MCMEMBER_REC_FIELD(join_state),
295 { MCMEMBER_REC_FIELD(proxy_join),
305 #define SERVICE_REC_FIELD(field) \
306 .struct_offset_bytes = offsetof(struct ib_sa_service_rec, field), \
307 .struct_size_bytes = sizeof ((struct ib_sa_service_rec *) 0)->field, \
308 .field_name = "sa_service_rec:" #field
310 static const struct ib_field service_rec_table[] = {
311 { SERVICE_REC_FIELD(id),
315 { SERVICE_REC_FIELD(gid),
319 { SERVICE_REC_FIELD(pkey),
323 { SERVICE_REC_FIELD(lease),
327 { SERVICE_REC_FIELD(key),
331 { SERVICE_REC_FIELD(name),
335 { SERVICE_REC_FIELD(data8),
339 { SERVICE_REC_FIELD(data16),
343 { SERVICE_REC_FIELD(data32),
347 { SERVICE_REC_FIELD(data64),
353 static void free_sm_ah(struct kref *kref)
355 struct ib_sa_sm_ah *sm_ah = container_of(kref, struct ib_sa_sm_ah, ref);
357 ib_destroy_ah(sm_ah->ah);
361 static void update_sm_ah(void *port_ptr)
363 struct ib_sa_port *port = port_ptr;
364 struct ib_sa_sm_ah *new_ah, *old_ah;
365 struct ib_port_attr port_attr;
366 struct ib_ah_attr ah_attr;
368 if (ib_query_port(port->agent->device, port->port_num, &port_attr)) {
369 printk(KERN_WARNING "Couldn't query port\n");
373 new_ah = kmalloc(sizeof *new_ah, GFP_KERNEL);
375 printk(KERN_WARNING "Couldn't allocate new SM AH\n");
379 kref_init(&new_ah->ref);
381 memset(&ah_attr, 0, sizeof ah_attr);
382 ah_attr.dlid = port_attr.sm_lid;
383 ah_attr.sl = port_attr.sm_sl;
384 ah_attr.port_num = port->port_num;
386 new_ah->ah = ib_create_ah(port->agent->qp->pd, &ah_attr);
387 if (IS_ERR(new_ah->ah)) {
388 printk(KERN_WARNING "Couldn't create new SM AH\n");
393 spin_lock_irq(&port->ah_lock);
394 old_ah = port->sm_ah;
395 port->sm_ah = new_ah;
396 spin_unlock_irq(&port->ah_lock);
399 kref_put(&old_ah->ref, free_sm_ah);
402 static void ib_sa_event(struct ib_event_handler *handler, struct ib_event *event)
404 if (event->event == IB_EVENT_PORT_ERR ||
405 event->event == IB_EVENT_PORT_ACTIVE ||
406 event->event == IB_EVENT_LID_CHANGE ||
407 event->event == IB_EVENT_PKEY_CHANGE ||
408 event->event == IB_EVENT_SM_CHANGE ||
409 event->event == IB_EVENT_CLIENT_REREGISTER) {
410 struct ib_sa_device *sa_dev;
411 sa_dev = container_of(handler, typeof(*sa_dev), event_handler);
413 schedule_work(&sa_dev->port[event->element.port_num -
414 sa_dev->start_port].update_task);
419 * ib_sa_cancel_query - try to cancel an SA query
420 * @id:ID of query to cancel
421 * @query:query pointer to cancel
423 * Try to cancel an SA query. If the id and query don't match up or
424 * the query has already completed, nothing is done. Otherwise the
425 * query is canceled and will complete with a status of -EINTR.
427 void ib_sa_cancel_query(int id, struct ib_sa_query *query)
430 struct ib_mad_agent *agent;
431 struct ib_mad_send_buf *mad_buf;
433 spin_lock_irqsave(&idr_lock, flags);
434 if (idr_find(&query_idr, id) != query) {
435 spin_unlock_irqrestore(&idr_lock, flags);
438 agent = query->port->agent;
439 mad_buf = query->mad_buf;
440 spin_unlock_irqrestore(&idr_lock, flags);
442 ib_cancel_mad(agent, mad_buf);
444 EXPORT_SYMBOL(ib_sa_cancel_query);
446 int ib_init_ah_from_path(struct ib_device *device, u8 port_num,
447 struct ib_sa_path_rec *rec, struct ib_ah_attr *ah_attr)
452 memset(ah_attr, 0, sizeof *ah_attr);
453 ah_attr->dlid = be16_to_cpu(rec->dlid);
454 ah_attr->sl = rec->sl;
455 ah_attr->src_path_bits = be16_to_cpu(rec->slid) & 0x7f;
456 ah_attr->port_num = port_num;
458 if (rec->hop_limit > 1) {
459 ah_attr->ah_flags = IB_AH_GRH;
460 ah_attr->grh.dgid = rec->dgid;
462 ret = ib_find_cached_gid(device, &rec->sgid, &port_num,
467 ah_attr->grh.sgid_index = gid_index;
468 ah_attr->grh.flow_label = be32_to_cpu(rec->flow_label);
469 ah_attr->grh.hop_limit = rec->hop_limit;
470 ah_attr->grh.traffic_class = rec->traffic_class;
474 EXPORT_SYMBOL(ib_init_ah_from_path);
476 static void init_mad(struct ib_sa_mad *mad, struct ib_mad_agent *agent)
480 memset(mad, 0, sizeof *mad);
482 mad->mad_hdr.base_version = IB_MGMT_BASE_VERSION;
483 mad->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
484 mad->mad_hdr.class_version = IB_SA_CLASS_VERSION;
486 spin_lock_irqsave(&tid_lock, flags);
488 cpu_to_be64(((u64) agent->hi_tid) << 32 | tid++);
489 spin_unlock_irqrestore(&tid_lock, flags);
492 static int send_mad(struct ib_sa_query *query, int timeout_ms, gfp_t gfp_mask)
498 if (!idr_pre_get(&query_idr, gfp_mask))
500 spin_lock_irqsave(&idr_lock, flags);
501 ret = idr_get_new(&query_idr, query, &id);
502 spin_unlock_irqrestore(&idr_lock, flags);
508 query->mad_buf->timeout_ms = timeout_ms;
509 query->mad_buf->context[0] = query;
512 spin_lock_irqsave(&query->port->ah_lock, flags);
513 kref_get(&query->port->sm_ah->ref);
514 query->sm_ah = query->port->sm_ah;
515 spin_unlock_irqrestore(&query->port->ah_lock, flags);
517 query->mad_buf->ah = query->sm_ah->ah;
519 ret = ib_post_send_mad(query->mad_buf, NULL);
521 spin_lock_irqsave(&idr_lock, flags);
522 idr_remove(&query_idr, id);
523 spin_unlock_irqrestore(&idr_lock, flags);
525 kref_put(&query->sm_ah->ref, free_sm_ah);
529 * It's not safe to dereference query any more, because the
530 * send may already have completed and freed the query in
533 return ret ? ret : id;
536 static void ib_sa_path_rec_callback(struct ib_sa_query *sa_query,
538 struct ib_sa_mad *mad)
540 struct ib_sa_path_query *query =
541 container_of(sa_query, struct ib_sa_path_query, sa_query);
544 struct ib_sa_path_rec rec;
546 ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table),
548 query->callback(status, &rec, query->context);
550 query->callback(status, NULL, query->context);
553 static void ib_sa_path_rec_release(struct ib_sa_query *sa_query)
555 kfree(container_of(sa_query, struct ib_sa_path_query, sa_query));
559 * ib_sa_path_rec_get - Start a Path get query
560 * @device:device to send query on
561 * @port_num: port number to send query on
562 * @rec:Path Record to send in query
563 * @comp_mask:component mask to send in query
564 * @timeout_ms:time to wait for response
565 * @gfp_mask:GFP mask to use for internal allocations
566 * @callback:function called when query completes, times out or is
568 * @context:opaque user context passed to callback
569 * @sa_query:query context, used to cancel query
571 * Send a Path Record Get query to the SA to look up a path. The
572 * callback function will be called when the query completes (or
573 * fails); status is 0 for a successful response, -EINTR if the query
574 * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
575 * occurred sending the query. The resp parameter of the callback is
576 * only valid if status is 0.
578 * If the return value of ib_sa_path_rec_get() is negative, it is an
579 * error code. Otherwise it is a query ID that can be used to cancel
582 int ib_sa_path_rec_get(struct ib_device *device, u8 port_num,
583 struct ib_sa_path_rec *rec,
584 ib_sa_comp_mask comp_mask,
585 int timeout_ms, gfp_t gfp_mask,
586 void (*callback)(int status,
587 struct ib_sa_path_rec *resp,
590 struct ib_sa_query **sa_query)
592 struct ib_sa_path_query *query;
593 struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
594 struct ib_sa_port *port;
595 struct ib_mad_agent *agent;
596 struct ib_sa_mad *mad;
602 port = &sa_dev->port[port_num - sa_dev->start_port];
605 query = kmalloc(sizeof *query, gfp_mask);
609 query->sa_query.mad_buf = ib_create_send_mad(agent, 1, 0,
611 IB_MGMT_SA_DATA, gfp_mask);
612 if (!query->sa_query.mad_buf) {
617 query->callback = callback;
618 query->context = context;
620 mad = query->sa_query.mad_buf->mad;
621 init_mad(mad, agent);
623 query->sa_query.callback = callback ? ib_sa_path_rec_callback : NULL;
624 query->sa_query.release = ib_sa_path_rec_release;
625 query->sa_query.port = port;
626 mad->mad_hdr.method = IB_MGMT_METHOD_GET;
627 mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_PATH_REC);
628 mad->sa_hdr.comp_mask = comp_mask;
630 ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table), rec, mad->data);
632 *sa_query = &query->sa_query;
634 ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
642 ib_free_send_mad(query->sa_query.mad_buf);
648 EXPORT_SYMBOL(ib_sa_path_rec_get);
650 static void ib_sa_service_rec_callback(struct ib_sa_query *sa_query,
652 struct ib_sa_mad *mad)
654 struct ib_sa_service_query *query =
655 container_of(sa_query, struct ib_sa_service_query, sa_query);
658 struct ib_sa_service_rec rec;
660 ib_unpack(service_rec_table, ARRAY_SIZE(service_rec_table),
662 query->callback(status, &rec, query->context);
664 query->callback(status, NULL, query->context);
667 static void ib_sa_service_rec_release(struct ib_sa_query *sa_query)
669 kfree(container_of(sa_query, struct ib_sa_service_query, sa_query));
673 * ib_sa_service_rec_query - Start Service Record operation
674 * @device:device to send request on
675 * @port_num: port number to send request on
676 * @method:SA method - should be get, set, or delete
677 * @rec:Service Record to send in request
678 * @comp_mask:component mask to send in request
679 * @timeout_ms:time to wait for response
680 * @gfp_mask:GFP mask to use for internal allocations
681 * @callback:function called when request completes, times out or is
683 * @context:opaque user context passed to callback
684 * @sa_query:request context, used to cancel request
686 * Send a Service Record set/get/delete to the SA to register,
687 * unregister or query a service record.
688 * The callback function will be called when the request completes (or
689 * fails); status is 0 for a successful response, -EINTR if the query
690 * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
691 * occurred sending the query. The resp parameter of the callback is
692 * only valid if status is 0.
694 * If the return value of ib_sa_service_rec_query() is negative, it is an
695 * error code. Otherwise it is a request ID that can be used to cancel
698 int ib_sa_service_rec_query(struct ib_device *device, u8 port_num, u8 method,
699 struct ib_sa_service_rec *rec,
700 ib_sa_comp_mask comp_mask,
701 int timeout_ms, gfp_t gfp_mask,
702 void (*callback)(int status,
703 struct ib_sa_service_rec *resp,
706 struct ib_sa_query **sa_query)
708 struct ib_sa_service_query *query;
709 struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
710 struct ib_sa_port *port;
711 struct ib_mad_agent *agent;
712 struct ib_sa_mad *mad;
718 port = &sa_dev->port[port_num - sa_dev->start_port];
721 if (method != IB_MGMT_METHOD_GET &&
722 method != IB_MGMT_METHOD_SET &&
723 method != IB_SA_METHOD_DELETE)
726 query = kmalloc(sizeof *query, gfp_mask);
730 query->sa_query.mad_buf = ib_create_send_mad(agent, 1, 0,
732 IB_MGMT_SA_DATA, gfp_mask);
733 if (!query->sa_query.mad_buf) {
738 query->callback = callback;
739 query->context = context;
741 mad = query->sa_query.mad_buf->mad;
742 init_mad(mad, agent);
744 query->sa_query.callback = callback ? ib_sa_service_rec_callback : NULL;
745 query->sa_query.release = ib_sa_service_rec_release;
746 query->sa_query.port = port;
747 mad->mad_hdr.method = method;
748 mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_SERVICE_REC);
749 mad->sa_hdr.comp_mask = comp_mask;
751 ib_pack(service_rec_table, ARRAY_SIZE(service_rec_table),
754 *sa_query = &query->sa_query;
756 ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
764 ib_free_send_mad(query->sa_query.mad_buf);
770 EXPORT_SYMBOL(ib_sa_service_rec_query);
772 static void ib_sa_mcmember_rec_callback(struct ib_sa_query *sa_query,
774 struct ib_sa_mad *mad)
776 struct ib_sa_mcmember_query *query =
777 container_of(sa_query, struct ib_sa_mcmember_query, sa_query);
780 struct ib_sa_mcmember_rec rec;
782 ib_unpack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
784 query->callback(status, &rec, query->context);
786 query->callback(status, NULL, query->context);
789 static void ib_sa_mcmember_rec_release(struct ib_sa_query *sa_query)
791 kfree(container_of(sa_query, struct ib_sa_mcmember_query, sa_query));
794 int ib_sa_mcmember_rec_query(struct ib_device *device, u8 port_num,
796 struct ib_sa_mcmember_rec *rec,
797 ib_sa_comp_mask comp_mask,
798 int timeout_ms, gfp_t gfp_mask,
799 void (*callback)(int status,
800 struct ib_sa_mcmember_rec *resp,
803 struct ib_sa_query **sa_query)
805 struct ib_sa_mcmember_query *query;
806 struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
807 struct ib_sa_port *port;
808 struct ib_mad_agent *agent;
809 struct ib_sa_mad *mad;
815 port = &sa_dev->port[port_num - sa_dev->start_port];
818 query = kmalloc(sizeof *query, gfp_mask);
822 query->sa_query.mad_buf = ib_create_send_mad(agent, 1, 0,
824 IB_MGMT_SA_DATA, gfp_mask);
825 if (!query->sa_query.mad_buf) {
830 query->callback = callback;
831 query->context = context;
833 mad = query->sa_query.mad_buf->mad;
834 init_mad(mad, agent);
836 query->sa_query.callback = callback ? ib_sa_mcmember_rec_callback : NULL;
837 query->sa_query.release = ib_sa_mcmember_rec_release;
838 query->sa_query.port = port;
839 mad->mad_hdr.method = method;
840 mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC);
841 mad->sa_hdr.comp_mask = comp_mask;
843 ib_pack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
846 *sa_query = &query->sa_query;
848 ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
856 ib_free_send_mad(query->sa_query.mad_buf);
862 EXPORT_SYMBOL(ib_sa_mcmember_rec_query);
864 static void send_handler(struct ib_mad_agent *agent,
865 struct ib_mad_send_wc *mad_send_wc)
867 struct ib_sa_query *query = mad_send_wc->send_buf->context[0];
871 switch (mad_send_wc->status) {
873 /* No callback -- already got recv */
875 case IB_WC_RESP_TIMEOUT_ERR:
876 query->callback(query, -ETIMEDOUT, NULL);
878 case IB_WC_WR_FLUSH_ERR:
879 query->callback(query, -EINTR, NULL);
882 query->callback(query, -EIO, NULL);
886 spin_lock_irqsave(&idr_lock, flags);
887 idr_remove(&query_idr, query->id);
888 spin_unlock_irqrestore(&idr_lock, flags);
890 ib_free_send_mad(mad_send_wc->send_buf);
891 kref_put(&query->sm_ah->ref, free_sm_ah);
892 query->release(query);
895 static void recv_handler(struct ib_mad_agent *mad_agent,
896 struct ib_mad_recv_wc *mad_recv_wc)
898 struct ib_sa_query *query;
899 struct ib_mad_send_buf *mad_buf;
901 mad_buf = (void *) (unsigned long) mad_recv_wc->wc->wr_id;
902 query = mad_buf->context[0];
904 if (query->callback) {
905 if (mad_recv_wc->wc->status == IB_WC_SUCCESS)
906 query->callback(query,
907 mad_recv_wc->recv_buf.mad->mad_hdr.status ?
909 (struct ib_sa_mad *) mad_recv_wc->recv_buf.mad);
911 query->callback(query, -EIO, NULL);
914 ib_free_recv_mad(mad_recv_wc);
917 static void ib_sa_add_one(struct ib_device *device)
919 struct ib_sa_device *sa_dev;
922 if (device->node_type == IB_NODE_SWITCH)
926 e = device->phys_port_cnt;
929 sa_dev = kmalloc(sizeof *sa_dev +
930 (e - s + 1) * sizeof (struct ib_sa_port),
935 sa_dev->start_port = s;
936 sa_dev->end_port = e;
938 for (i = 0; i <= e - s; ++i) {
939 sa_dev->port[i].sm_ah = NULL;
940 sa_dev->port[i].port_num = i + s;
941 spin_lock_init(&sa_dev->port[i].ah_lock);
943 sa_dev->port[i].agent =
944 ib_register_mad_agent(device, i + s, IB_QPT_GSI,
945 NULL, 0, send_handler,
946 recv_handler, sa_dev);
947 if (IS_ERR(sa_dev->port[i].agent))
950 INIT_WORK(&sa_dev->port[i].update_task,
951 update_sm_ah, &sa_dev->port[i]);
954 ib_set_client_data(device, &sa_client, sa_dev);
957 * We register our event handler after everything is set up,
958 * and then update our cached info after the event handler is
959 * registered to avoid any problems if a port changes state
960 * during our initialization.
963 INIT_IB_EVENT_HANDLER(&sa_dev->event_handler, device, ib_sa_event);
964 if (ib_register_event_handler(&sa_dev->event_handler))
967 for (i = 0; i <= e - s; ++i)
968 update_sm_ah(&sa_dev->port[i]);
974 ib_unregister_mad_agent(sa_dev->port[i].agent);
981 static void ib_sa_remove_one(struct ib_device *device)
983 struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
989 ib_unregister_event_handler(&sa_dev->event_handler);
991 flush_scheduled_work();
993 for (i = 0; i <= sa_dev->end_port - sa_dev->start_port; ++i) {
994 ib_unregister_mad_agent(sa_dev->port[i].agent);
995 kref_put(&sa_dev->port[i].sm_ah->ref, free_sm_ah);
1001 static int __init ib_sa_init(void)
1005 spin_lock_init(&idr_lock);
1006 spin_lock_init(&tid_lock);
1008 get_random_bytes(&tid, sizeof tid);
1010 ret = ib_register_client(&sa_client);
1012 printk(KERN_ERR "Couldn't register ib_sa client\n");
1017 static void __exit ib_sa_cleanup(void)
1019 ib_unregister_client(&sa_client);
1020 idr_destroy(&query_idr);
1023 module_init(ib_sa_init);
1024 module_exit(ib_sa_cleanup);