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/err.h>
37 #include <linux/random.h>
38 #include <linux/spinlock.h>
39 #include <linux/slab.h>
40 #include <linux/dma-mapping.h>
41 #include <linux/kref.h>
42 #include <linux/idr.h>
43 #include <linux/workqueue.h>
45 #include <rdma/ib_pack.h>
46 #include <rdma/ib_cache.h>
49 MODULE_AUTHOR("Roland Dreier");
50 MODULE_DESCRIPTION("InfiniBand subnet administration query support");
51 MODULE_LICENSE("Dual BSD/GPL");
61 struct ib_mad_agent *agent;
62 struct ib_mad_agent *notice_agent;
63 struct ib_sa_sm_ah *sm_ah;
64 struct work_struct update_task;
67 struct ib_device *device;
71 int start_port, end_port;
72 struct ib_event_handler event_handler;
73 struct ib_sa_port port[0];
77 void (*callback)(struct ib_sa_query *, int, struct ib_sa_mad *);
78 void (*release)(struct ib_sa_query *);
79 struct ib_sa_client *client;
80 struct ib_sa_port *port;
81 struct ib_mad_send_buf *mad_buf;
82 struct ib_sa_sm_ah *sm_ah;
86 struct ib_sa_service_query {
87 void (*callback)(int, struct ib_sa_service_rec *, void *);
89 struct ib_sa_query sa_query;
92 struct ib_sa_path_query {
93 void (*callback)(int, struct ib_sa_path_rec *, void *);
95 struct ib_sa_query sa_query;
98 struct ib_sa_mcmember_query {
99 void (*callback)(int, struct ib_sa_mcmember_rec *, void *);
101 struct ib_sa_query sa_query;
104 struct ib_sa_inform_query {
105 void (*callback)(int, struct ib_sa_inform *, void *);
107 struct ib_sa_query sa_query;
110 static void ib_sa_add_one(struct ib_device *device);
111 static void ib_sa_remove_one(struct ib_device *device);
113 static struct ib_client sa_client = {
115 .add = ib_sa_add_one,
116 .remove = ib_sa_remove_one
119 static spinlock_t idr_lock;
120 static DEFINE_IDR(query_idr);
122 static spinlock_t tid_lock;
125 #define PATH_REC_FIELD(field) \
126 .struct_offset_bytes = offsetof(struct ib_sa_path_rec, field), \
127 .struct_size_bytes = sizeof ((struct ib_sa_path_rec *) 0)->field, \
128 .field_name = "sa_path_rec:" #field
130 static const struct ib_field path_rec_table[] = {
131 { PATH_REC_FIELD(service_id),
135 { PATH_REC_FIELD(dgid),
139 { PATH_REC_FIELD(sgid),
143 { PATH_REC_FIELD(dlid),
147 { PATH_REC_FIELD(slid),
151 { PATH_REC_FIELD(raw_traffic),
159 { PATH_REC_FIELD(flow_label),
163 { PATH_REC_FIELD(hop_limit),
167 { PATH_REC_FIELD(traffic_class),
171 { PATH_REC_FIELD(reversible),
175 { PATH_REC_FIELD(numb_path),
179 { PATH_REC_FIELD(pkey),
183 { PATH_REC_FIELD(qos_class),
187 { PATH_REC_FIELD(sl),
191 { PATH_REC_FIELD(mtu_selector),
195 { PATH_REC_FIELD(mtu),
199 { PATH_REC_FIELD(rate_selector),
203 { PATH_REC_FIELD(rate),
207 { PATH_REC_FIELD(packet_life_time_selector),
211 { PATH_REC_FIELD(packet_life_time),
215 { PATH_REC_FIELD(preference),
225 #define MCMEMBER_REC_FIELD(field) \
226 .struct_offset_bytes = offsetof(struct ib_sa_mcmember_rec, field), \
227 .struct_size_bytes = sizeof ((struct ib_sa_mcmember_rec *) 0)->field, \
228 .field_name = "sa_mcmember_rec:" #field
230 static const struct ib_field mcmember_rec_table[] = {
231 { MCMEMBER_REC_FIELD(mgid),
235 { MCMEMBER_REC_FIELD(port_gid),
239 { MCMEMBER_REC_FIELD(qkey),
243 { MCMEMBER_REC_FIELD(mlid),
247 { MCMEMBER_REC_FIELD(mtu_selector),
251 { MCMEMBER_REC_FIELD(mtu),
255 { MCMEMBER_REC_FIELD(traffic_class),
259 { MCMEMBER_REC_FIELD(pkey),
263 { MCMEMBER_REC_FIELD(rate_selector),
267 { MCMEMBER_REC_FIELD(rate),
271 { MCMEMBER_REC_FIELD(packet_life_time_selector),
275 { MCMEMBER_REC_FIELD(packet_life_time),
279 { MCMEMBER_REC_FIELD(sl),
283 { MCMEMBER_REC_FIELD(flow_label),
287 { MCMEMBER_REC_FIELD(hop_limit),
291 { MCMEMBER_REC_FIELD(scope),
295 { MCMEMBER_REC_FIELD(join_state),
299 { MCMEMBER_REC_FIELD(proxy_join),
309 #define SERVICE_REC_FIELD(field) \
310 .struct_offset_bytes = offsetof(struct ib_sa_service_rec, field), \
311 .struct_size_bytes = sizeof ((struct ib_sa_service_rec *) 0)->field, \
312 .field_name = "sa_service_rec:" #field
314 static const struct ib_field service_rec_table[] = {
315 { SERVICE_REC_FIELD(id),
319 { SERVICE_REC_FIELD(gid),
323 { SERVICE_REC_FIELD(pkey),
327 { SERVICE_REC_FIELD(lease),
331 { SERVICE_REC_FIELD(key),
335 { SERVICE_REC_FIELD(name),
339 { SERVICE_REC_FIELD(data8),
343 { SERVICE_REC_FIELD(data16),
347 { SERVICE_REC_FIELD(data32),
351 { SERVICE_REC_FIELD(data64),
357 #define INFORM_FIELD(field) \
358 .struct_offset_bytes = offsetof(struct ib_sa_inform, field), \
359 .struct_size_bytes = sizeof ((struct ib_sa_inform *) 0)->field, \
360 .field_name = "sa_inform:" #field
362 static const struct ib_field inform_table[] = {
367 { INFORM_FIELD(lid_range_begin),
371 { INFORM_FIELD(lid_range_end),
379 { INFORM_FIELD(is_generic),
383 { INFORM_FIELD(subscribe),
387 { INFORM_FIELD(type),
391 { INFORM_FIELD(trap.generic.trap_num),
395 { INFORM_FIELD(trap.generic.qpn),
403 { INFORM_FIELD(trap.generic.resp_time),
411 { INFORM_FIELD(trap.generic.producer_type),
417 #define NOTICE_FIELD(field) \
418 .struct_offset_bytes = offsetof(struct ib_sa_notice, field), \
419 .struct_size_bytes = sizeof ((struct ib_sa_notice *) 0)->field, \
420 .field_name = "sa_notice:" #field
422 static const struct ib_field notice_table[] = {
423 { NOTICE_FIELD(is_generic),
427 { NOTICE_FIELD(type),
431 { NOTICE_FIELD(trap.generic.producer_type),
435 { NOTICE_FIELD(trap.generic.trap_num),
439 { NOTICE_FIELD(issuer_lid),
443 { NOTICE_FIELD(notice_toggle),
447 { NOTICE_FIELD(notice_count),
451 { NOTICE_FIELD(data_details),
455 { NOTICE_FIELD(issuer_gid),
461 int ib_sa_check_selector(ib_sa_comp_mask comp_mask,
462 ib_sa_comp_mask selector_mask,
463 ib_sa_comp_mask value_mask,
464 u8 selector, u8 src_value, u8 dst_value)
468 if (!(comp_mask & selector_mask) || !(comp_mask & value_mask))
473 err = (src_value <= dst_value);
476 err = (src_value >= dst_value);
479 err = (src_value != dst_value);
489 int ib_sa_pack_attr(void *dst, void *src, int attr_id)
492 case IB_SA_ATTR_PATH_REC:
493 ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table), src, dst);
501 int ib_sa_unpack_attr(void *dst, void *src, int attr_id)
504 case IB_SA_ATTR_PATH_REC:
505 ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table), src, dst);
513 static void free_sm_ah(struct kref *kref)
515 struct ib_sa_sm_ah *sm_ah = container_of(kref, struct ib_sa_sm_ah, ref);
517 ib_destroy_ah(sm_ah->ah);
521 static void update_sm_ah(struct work_struct *work)
523 struct ib_sa_port *port =
524 container_of(work, struct ib_sa_port, update_task);
525 struct ib_sa_sm_ah *new_ah;
526 struct ib_port_attr port_attr;
527 struct ib_ah_attr ah_attr;
529 if (ib_query_port(port->agent->device, port->port_num, &port_attr)) {
530 printk(KERN_WARNING "Couldn't query port\n");
534 new_ah = kmalloc(sizeof *new_ah, GFP_KERNEL);
536 printk(KERN_WARNING "Couldn't allocate new SM AH\n");
540 kref_init(&new_ah->ref);
541 new_ah->src_path_mask = (1 << port_attr.lmc) - 1;
543 new_ah->pkey_index = 0;
544 if (ib_find_pkey(port->agent->device, port->port_num,
545 IB_DEFAULT_PKEY_FULL, &new_ah->pkey_index))
546 printk(KERN_ERR "Couldn't find index for default PKey\n");
548 memset(&ah_attr, 0, sizeof ah_attr);
549 ah_attr.dlid = port_attr.sm_lid;
550 ah_attr.sl = port_attr.sm_sl;
551 ah_attr.port_num = port->port_num;
553 new_ah->ah = ib_create_ah(port->agent->qp->pd, &ah_attr);
554 if (IS_ERR(new_ah->ah)) {
555 printk(KERN_WARNING "Couldn't create new SM AH\n");
560 spin_lock_irq(&port->ah_lock);
562 kref_put(&port->sm_ah->ref, free_sm_ah);
563 port->sm_ah = new_ah;
564 spin_unlock_irq(&port->ah_lock);
568 static void ib_sa_event(struct ib_event_handler *handler, struct ib_event *event)
570 if (event->event == IB_EVENT_PORT_ERR ||
571 event->event == IB_EVENT_PORT_ACTIVE ||
572 event->event == IB_EVENT_LID_CHANGE ||
573 event->event == IB_EVENT_PKEY_CHANGE ||
574 event->event == IB_EVENT_SM_CHANGE ||
575 event->event == IB_EVENT_CLIENT_REREGISTER) {
577 struct ib_sa_device *sa_dev =
578 container_of(handler, typeof(*sa_dev), event_handler);
579 struct ib_sa_port *port =
580 &sa_dev->port[event->element.port_num - sa_dev->start_port];
582 if (rdma_port_get_link_layer(handler->device, port->port_num) != IB_LINK_LAYER_INFINIBAND)
585 spin_lock_irqsave(&port->ah_lock, flags);
587 kref_put(&port->sm_ah->ref, free_sm_ah);
589 spin_unlock_irqrestore(&port->ah_lock, flags);
591 schedule_work(&sa_dev->port[event->element.port_num -
592 sa_dev->start_port].update_task);
596 void ib_sa_register_client(struct ib_sa_client *client)
598 atomic_set(&client->users, 1);
599 init_completion(&client->comp);
601 EXPORT_SYMBOL(ib_sa_register_client);
603 void ib_sa_unregister_client(struct ib_sa_client *client)
605 ib_sa_client_put(client);
606 wait_for_completion(&client->comp);
608 EXPORT_SYMBOL(ib_sa_unregister_client);
611 * ib_sa_cancel_query - try to cancel an SA query
612 * @id:ID of query to cancel
613 * @query:query pointer to cancel
615 * Try to cancel an SA query. If the id and query don't match up or
616 * the query has already completed, nothing is done. Otherwise the
617 * query is canceled and will complete with a status of -EINTR.
619 void ib_sa_cancel_query(int id, struct ib_sa_query *query)
622 struct ib_mad_agent *agent;
623 struct ib_mad_send_buf *mad_buf;
625 spin_lock_irqsave(&idr_lock, flags);
626 if (idr_find(&query_idr, id) != query) {
627 spin_unlock_irqrestore(&idr_lock, flags);
630 agent = query->port->agent;
631 mad_buf = query->mad_buf;
632 spin_unlock_irqrestore(&idr_lock, flags);
634 ib_cancel_mad(agent, mad_buf);
636 EXPORT_SYMBOL(ib_sa_cancel_query);
638 static u8 get_src_path_mask(struct ib_device *device, u8 port_num)
640 struct ib_sa_device *sa_dev;
641 struct ib_sa_port *port;
645 sa_dev = ib_get_client_data(device, &sa_client);
649 port = &sa_dev->port[port_num - sa_dev->start_port];
650 spin_lock_irqsave(&port->ah_lock, flags);
651 src_path_mask = port->sm_ah ? port->sm_ah->src_path_mask : 0x7f;
652 spin_unlock_irqrestore(&port->ah_lock, flags);
654 return src_path_mask;
657 int ib_init_ah_from_path(struct ib_device *device, u8 port_num,
658 struct ib_sa_path_rec *rec, struct ib_ah_attr *ah_attr)
664 memset(ah_attr, 0, sizeof *ah_attr);
665 ah_attr->dlid = be16_to_cpu(rec->dlid);
666 ah_attr->sl = rec->sl;
667 ah_attr->src_path_bits = be16_to_cpu(rec->slid) &
668 get_src_path_mask(device, port_num);
669 ah_attr->port_num = port_num;
670 ah_attr->static_rate = rec->rate;
672 force_grh = rdma_port_get_link_layer(device, port_num) == IB_LINK_LAYER_ETHERNET;
674 if (rec->hop_limit > 1 || force_grh) {
675 ah_attr->ah_flags = IB_AH_GRH;
676 ah_attr->grh.dgid = rec->dgid;
678 ret = ib_find_cached_gid(device, &rec->sgid, &port_num,
683 ah_attr->grh.sgid_index = gid_index;
684 ah_attr->grh.flow_label = be32_to_cpu(rec->flow_label);
685 ah_attr->grh.hop_limit = rec->hop_limit;
686 ah_attr->grh.traffic_class = rec->traffic_class;
690 EXPORT_SYMBOL(ib_init_ah_from_path);
692 static int alloc_mad(struct ib_sa_query *query, gfp_t gfp_mask)
696 spin_lock_irqsave(&query->port->ah_lock, flags);
697 if (!query->port->sm_ah) {
698 spin_unlock_irqrestore(&query->port->ah_lock, flags);
701 kref_get(&query->port->sm_ah->ref);
702 query->sm_ah = query->port->sm_ah;
703 spin_unlock_irqrestore(&query->port->ah_lock, flags);
705 query->mad_buf = ib_create_send_mad(query->port->agent, 1,
706 query->sm_ah->pkey_index,
707 0, IB_MGMT_SA_HDR, IB_MGMT_SA_DATA,
709 if (IS_ERR(query->mad_buf)) {
710 kref_put(&query->sm_ah->ref, free_sm_ah);
714 query->mad_buf->ah = query->sm_ah->ah;
719 static void free_mad(struct ib_sa_query *query)
721 ib_free_send_mad(query->mad_buf);
722 kref_put(&query->sm_ah->ref, free_sm_ah);
725 static void init_mad(struct ib_sa_mad *mad, struct ib_mad_agent *agent)
729 memset(mad, 0, sizeof *mad);
731 mad->mad_hdr.base_version = IB_MGMT_BASE_VERSION;
732 mad->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
733 mad->mad_hdr.class_version = IB_SA_CLASS_VERSION;
735 spin_lock_irqsave(&tid_lock, flags);
737 cpu_to_be64(((u64) agent->hi_tid) << 32 | tid++);
738 spin_unlock_irqrestore(&tid_lock, flags);
741 static int send_mad(struct ib_sa_query *query, int timeout_ms, gfp_t gfp_mask)
747 if (!idr_pre_get(&query_idr, gfp_mask))
749 spin_lock_irqsave(&idr_lock, flags);
750 ret = idr_get_new(&query_idr, query, &id);
751 spin_unlock_irqrestore(&idr_lock, flags);
757 query->mad_buf->timeout_ms = timeout_ms;
758 query->mad_buf->context[0] = query;
761 ret = ib_post_send_mad(query->mad_buf, NULL);
763 spin_lock_irqsave(&idr_lock, flags);
764 idr_remove(&query_idr, id);
765 spin_unlock_irqrestore(&idr_lock, flags);
769 * It's not safe to dereference query any more, because the
770 * send may already have completed and freed the query in
773 return ret ? ret : id;
776 void ib_sa_unpack_path(void *attribute, struct ib_sa_path_rec *rec)
778 ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table), attribute, rec);
780 EXPORT_SYMBOL(ib_sa_unpack_path);
782 static void ib_sa_path_rec_callback(struct ib_sa_query *sa_query,
784 struct ib_sa_mad *mad)
786 struct ib_sa_path_query *query =
787 container_of(sa_query, struct ib_sa_path_query, sa_query);
790 struct ib_sa_path_rec rec;
792 ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table),
794 query->callback(status, &rec, query->context);
796 query->callback(status, NULL, query->context);
799 static void ib_sa_path_rec_release(struct ib_sa_query *sa_query)
801 kfree(container_of(sa_query, struct ib_sa_path_query, sa_query));
804 int ib_sa_path_rec_query(struct ib_sa_client *client,
805 struct ib_device *device, u8 port_num,
806 struct ib_sa_path_rec *rec,
807 ib_sa_comp_mask comp_mask,
808 int timeout_ms, gfp_t gfp_mask,
809 void (*callback)(int status,
810 struct ib_sa_path_rec *resp,
813 struct ib_sa_query **sa_query)
815 struct ib_sa_path_query *query;
816 struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
817 struct ib_sa_port *port;
818 struct ib_mad_agent *agent;
819 struct ib_sa_mad *mad;
825 port = &sa_dev->port[port_num - sa_dev->start_port];
828 query = kmalloc(sizeof *query, gfp_mask);
832 query->sa_query.port = port;
833 ret = alloc_mad(&query->sa_query, gfp_mask);
837 ib_sa_client_get(client);
838 query->sa_query.client = client;
839 query->callback = callback;
840 query->context = context;
842 mad = query->sa_query.mad_buf->mad;
843 init_mad(mad, agent);
845 query->sa_query.callback = callback ? ib_sa_path_rec_callback : NULL;
846 query->sa_query.release = ib_sa_path_rec_release;
847 mad->mad_hdr.method = IB_MGMT_METHOD_GET;
848 mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_PATH_REC);
849 mad->sa_hdr.comp_mask = comp_mask;
851 ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table), rec, mad->data);
853 *sa_query = &query->sa_query;
855 ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
863 ib_sa_client_put(query->sa_query.client);
864 free_mad(&query->sa_query);
871 static void ib_sa_service_rec_callback(struct ib_sa_query *sa_query,
873 struct ib_sa_mad *mad)
875 struct ib_sa_service_query *query =
876 container_of(sa_query, struct ib_sa_service_query, sa_query);
879 struct ib_sa_service_rec rec;
881 ib_unpack(service_rec_table, ARRAY_SIZE(service_rec_table),
883 query->callback(status, &rec, query->context);
885 query->callback(status, NULL, query->context);
888 static void ib_sa_service_rec_release(struct ib_sa_query *sa_query)
890 kfree(container_of(sa_query, struct ib_sa_service_query, sa_query));
894 * ib_sa_service_rec_query - Start Service Record operation
896 * @device:device to send request on
897 * @port_num: port number to send request on
898 * @method:SA method - should be get, set, or delete
899 * @rec:Service Record to send in request
900 * @comp_mask:component mask to send in request
901 * @timeout_ms:time to wait for response
902 * @gfp_mask:GFP mask to use for internal allocations
903 * @callback:function called when request completes, times out or is
905 * @context:opaque user context passed to callback
906 * @sa_query:request context, used to cancel request
908 * Send a Service Record set/get/delete to the SA to register,
909 * unregister or query a service record.
910 * The callback function will be called when the request completes (or
911 * fails); status is 0 for a successful response, -EINTR if the query
912 * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
913 * occurred sending the query. The resp parameter of the callback is
914 * only valid if status is 0.
916 * If the return value of ib_sa_service_rec_query() is negative, it is an
917 * error code. Otherwise it is a request ID that can be used to cancel
920 int ib_sa_service_rec_query(struct ib_sa_client *client,
921 struct ib_device *device, u8 port_num, u8 method,
922 struct ib_sa_service_rec *rec,
923 ib_sa_comp_mask comp_mask,
924 int timeout_ms, gfp_t gfp_mask,
925 void (*callback)(int status,
926 struct ib_sa_service_rec *resp,
929 struct ib_sa_query **sa_query)
931 struct ib_sa_service_query *query;
932 struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
933 struct ib_sa_port *port;
934 struct ib_mad_agent *agent;
935 struct ib_sa_mad *mad;
941 port = &sa_dev->port[port_num - sa_dev->start_port];
944 if (method != IB_MGMT_METHOD_GET &&
945 method != IB_MGMT_METHOD_SET &&
946 method != IB_SA_METHOD_DELETE)
949 query = kmalloc(sizeof *query, gfp_mask);
953 query->sa_query.port = port;
954 ret = alloc_mad(&query->sa_query, gfp_mask);
958 ib_sa_client_get(client);
959 query->sa_query.client = client;
960 query->callback = callback;
961 query->context = context;
963 mad = query->sa_query.mad_buf->mad;
964 init_mad(mad, agent);
966 query->sa_query.callback = callback ? ib_sa_service_rec_callback : NULL;
967 query->sa_query.release = ib_sa_service_rec_release;
968 mad->mad_hdr.method = method;
969 mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_SERVICE_REC);
970 mad->sa_hdr.comp_mask = comp_mask;
972 ib_pack(service_rec_table, ARRAY_SIZE(service_rec_table),
975 *sa_query = &query->sa_query;
977 ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
985 ib_sa_client_put(query->sa_query.client);
986 free_mad(&query->sa_query);
992 EXPORT_SYMBOL(ib_sa_service_rec_query);
994 static void ib_sa_mcmember_rec_callback(struct ib_sa_query *sa_query,
996 struct ib_sa_mad *mad)
998 struct ib_sa_mcmember_query *query =
999 container_of(sa_query, struct ib_sa_mcmember_query, sa_query);
1002 struct ib_sa_mcmember_rec rec;
1004 ib_unpack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
1006 query->callback(status, &rec, query->context);
1008 query->callback(status, NULL, query->context);
1011 static void ib_sa_mcmember_rec_release(struct ib_sa_query *sa_query)
1013 kfree(container_of(sa_query, struct ib_sa_mcmember_query, sa_query));
1016 int ib_sa_mcmember_rec_query(struct ib_sa_client *client,
1017 struct ib_device *device, u8 port_num,
1019 struct ib_sa_mcmember_rec *rec,
1020 ib_sa_comp_mask comp_mask,
1021 int timeout_ms, gfp_t gfp_mask,
1022 void (*callback)(int status,
1023 struct ib_sa_mcmember_rec *resp,
1026 struct ib_sa_query **sa_query)
1028 struct ib_sa_mcmember_query *query;
1029 struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
1030 struct ib_sa_port *port;
1031 struct ib_mad_agent *agent;
1032 struct ib_sa_mad *mad;
1038 port = &sa_dev->port[port_num - sa_dev->start_port];
1039 agent = port->agent;
1041 query = kmalloc(sizeof *query, gfp_mask);
1045 query->sa_query.port = port;
1046 ret = alloc_mad(&query->sa_query, gfp_mask);
1050 ib_sa_client_get(client);
1051 query->sa_query.client = client;
1052 query->callback = callback;
1053 query->context = context;
1055 mad = query->sa_query.mad_buf->mad;
1056 init_mad(mad, agent);
1058 query->sa_query.callback = callback ? ib_sa_mcmember_rec_callback : NULL;
1059 query->sa_query.release = ib_sa_mcmember_rec_release;
1060 mad->mad_hdr.method = method;
1061 mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC);
1062 mad->sa_hdr.comp_mask = comp_mask;
1064 ib_pack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
1067 *sa_query = &query->sa_query;
1069 ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
1077 ib_sa_client_put(query->sa_query.client);
1078 free_mad(&query->sa_query);
1085 static void ib_sa_inform_callback(struct ib_sa_query *sa_query,
1087 struct ib_sa_mad *mad)
1089 struct ib_sa_inform_query *query =
1090 container_of(sa_query, struct ib_sa_inform_query, sa_query);
1093 struct ib_sa_inform rec;
1095 ib_unpack(inform_table, ARRAY_SIZE(inform_table),
1097 query->callback(status, &rec, query->context);
1099 query->callback(status, NULL, query->context);
1102 static void ib_sa_inform_release(struct ib_sa_query *sa_query)
1104 kfree(container_of(sa_query, struct ib_sa_inform_query, sa_query));
1107 int ib_sa_guid_info_rec_query(struct ib_sa_client *client,
1108 struct ib_device *device, u8 port_num,
1109 struct ib_sa_guidinfo_rec *rec,
1110 ib_sa_comp_mask comp_mask, u8 method,
1111 int timeout_ms, gfp_t gfp_mask,
1112 void (*callback)(int status,
1113 struct ib_sa_guidinfo_rec *resp,
1116 struct ib_sa_query **sa_query)
1119 // called originally from mad.c under mlx4_ib_init_sriov()
1120 // which calls mlx4_ib_init_alias_guid_service() in alias_GUID.c
1121 // which goes down to this function
1123 printk("ERROR: function should be called only in SRIOV flow!!!");
1129 * ib_sa_informinfo_query - Start an InformInfo registration.
1131 * @device:device to send query on
1132 * @port_num: port number to send query on
1133 * @rec:Inform record to send in query
1134 * @timeout_ms:time to wait for response
1135 * @gfp_mask:GFP mask to use for internal allocations
1136 * @callback:function called when notice handler registration completes,
1137 * times out or is canceled
1138 * @context:opaque user context passed to callback
1139 * @sa_query:query context, used to cancel query
1141 * This function sends inform info to register with SA to receive
1142 * in-service notice.
1143 * The callback function will be called when the query completes (or
1144 * fails); status is 0 for a successful response, -EINTR if the query
1145 * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
1146 * occurred sending the query. The resp parameter of the callback is
1147 * only valid if status is 0.
1149 * If the return value of ib_sa_inform_query() is negative, it is an
1150 * error code. Otherwise it is a query ID that can be used to cancel
1153 int ib_sa_informinfo_query(struct ib_sa_client *client,
1154 struct ib_device *device, u8 port_num,
1155 struct ib_sa_inform *rec,
1156 int timeout_ms, gfp_t gfp_mask,
1157 void (*callback)(int status,
1158 struct ib_sa_inform *resp,
1161 struct ib_sa_query **sa_query)
1163 struct ib_sa_inform_query *query;
1164 struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
1165 struct ib_sa_port *port;
1166 struct ib_mad_agent *agent;
1167 struct ib_sa_mad *mad;
1173 port = &sa_dev->port[port_num - sa_dev->start_port];
1174 agent = port->agent;
1176 query = kmalloc(sizeof *query, gfp_mask);
1180 query->sa_query.port = port;
1181 ret = alloc_mad(&query->sa_query, gfp_mask);
1185 ib_sa_client_get(client);
1186 query->sa_query.client = client;
1187 query->callback = callback;
1188 query->context = context;
1190 mad = query->sa_query.mad_buf->mad;
1191 init_mad(mad, agent);
1193 query->sa_query.callback = callback ? ib_sa_inform_callback : NULL;
1194 query->sa_query.release = ib_sa_inform_release;
1195 query->sa_query.port = port;
1196 mad->mad_hdr.method = IB_MGMT_METHOD_SET;
1197 mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_INFORM_INFO);
1199 ib_pack(inform_table, ARRAY_SIZE(inform_table), rec, mad->data);
1201 *sa_query = &query->sa_query;
1202 ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
1210 ib_sa_client_put(query->sa_query.client);
1211 free_mad(&query->sa_query);
1217 static void ib_sa_notice_resp(struct ib_sa_port *port,
1218 struct ib_mad_recv_wc *mad_recv_wc)
1220 struct ib_mad_send_buf *mad_buf;
1221 struct ib_sa_mad *mad;
1223 unsigned long flags;
1225 mad_buf = ib_create_send_mad(port->notice_agent, 1, 0, 0,
1226 IB_MGMT_SA_HDR, IB_MGMT_SA_DATA,
1228 if (IS_ERR(mad_buf))
1232 memcpy(mad, mad_recv_wc->recv_buf.mad, sizeof *mad);
1233 mad->mad_hdr.method = IB_MGMT_METHOD_REPORT_RESP;
1235 spin_lock_irqsave(&port->ah_lock, flags);
1237 spin_unlock_irqrestore(&port->ah_lock, flags);
1238 ib_free_send_mad(mad_buf);
1241 kref_get(&port->sm_ah->ref);
1242 mad_buf->context[0] = &port->sm_ah->ref;
1243 mad_buf->ah = port->sm_ah->ah;
1244 spin_unlock_irqrestore(&port->ah_lock, flags);
1246 ret = ib_post_send_mad(mad_buf, NULL);
1252 kref_put(mad_buf->context[0], free_sm_ah);
1253 ib_free_send_mad(mad_buf);
1256 static void send_handler(struct ib_mad_agent *agent,
1257 struct ib_mad_send_wc *mad_send_wc)
1259 struct ib_sa_query *query = mad_send_wc->send_buf->context[0];
1260 unsigned long flags;
1262 if (query->callback)
1263 switch (mad_send_wc->status) {
1265 /* No callback -- already got recv */
1267 case IB_WC_RESP_TIMEOUT_ERR:
1268 query->callback(query, -ETIMEDOUT, NULL);
1270 case IB_WC_WR_FLUSH_ERR:
1271 query->callback(query, -EINTR, NULL);
1274 query->callback(query, -EIO, NULL);
1278 spin_lock_irqsave(&idr_lock, flags);
1279 idr_remove(&query_idr, query->id);
1280 spin_unlock_irqrestore(&idr_lock, flags);
1283 ib_sa_client_put(query->client);
1284 query->release(query);
1287 static void recv_handler(struct ib_mad_agent *mad_agent,
1288 struct ib_mad_recv_wc *mad_recv_wc)
1290 struct ib_sa_query *query;
1291 struct ib_mad_send_buf *mad_buf;
1293 mad_buf = (void *) (unsigned long) mad_recv_wc->wc->wr_id;
1294 query = mad_buf->context[0];
1296 if (query->callback) {
1297 if (mad_recv_wc->wc->status == IB_WC_SUCCESS)
1298 query->callback(query,
1299 mad_recv_wc->recv_buf.mad->mad_hdr.status ?
1301 (struct ib_sa_mad *) mad_recv_wc->recv_buf.mad);
1303 query->callback(query, -EIO, NULL);
1306 ib_free_recv_mad(mad_recv_wc);
1309 static void notice_resp_handler(struct ib_mad_agent *agent,
1310 struct ib_mad_send_wc *mad_send_wc)
1312 kref_put(mad_send_wc->send_buf->context[0], free_sm_ah);
1313 ib_free_send_mad(mad_send_wc->send_buf);
1316 static void notice_handler(struct ib_mad_agent *mad_agent,
1317 struct ib_mad_recv_wc *mad_recv_wc)
1319 struct ib_sa_port *port;
1320 struct ib_sa_mad *mad;
1321 struct ib_sa_notice notice;
1323 port = mad_agent->context;
1324 mad = (struct ib_sa_mad *) mad_recv_wc->recv_buf.mad;
1325 ib_unpack(notice_table, ARRAY_SIZE(notice_table), mad->data, ¬ice);
1327 if (!notice_dispatch(port->device, port->port_num, ¬ice))
1328 ib_sa_notice_resp(port, mad_recv_wc);
1329 ib_free_recv_mad(mad_recv_wc);
1332 static void ib_sa_add_one(struct ib_device *device)
1334 struct ib_sa_device *sa_dev;
1335 struct ib_mad_reg_req reg_req = {
1336 .mgmt_class = IB_MGMT_CLASS_SUBN_ADM,
1337 .mgmt_class_version = 2
1341 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1344 if (device->node_type == RDMA_NODE_IB_SWITCH)
1348 e = device->phys_port_cnt;
1351 sa_dev = kzalloc(sizeof *sa_dev +
1352 (e - s + 1) * sizeof (struct ib_sa_port),
1357 sa_dev->start_port = s;
1358 sa_dev->end_port = e;
1360 for (i = 0; i <= e - s; ++i) {
1361 spin_lock_init(&sa_dev->port[i].ah_lock);
1362 if (rdma_port_get_link_layer(device, i + 1) != IB_LINK_LAYER_INFINIBAND)
1365 sa_dev->port[i].sm_ah = NULL;
1366 sa_dev->port[i].port_num = i + s;
1368 sa_dev->port[i].agent =
1369 ib_register_mad_agent(device, i + s, IB_QPT_GSI,
1370 NULL, 0, send_handler,
1371 recv_handler, sa_dev);
1372 if (IS_ERR(sa_dev->port[i].agent))
1375 sa_dev->port[i].device = device;
1376 set_bit(IB_MGMT_METHOD_REPORT, reg_req.method_mask);
1377 sa_dev->port[i].notice_agent =
1378 ib_register_mad_agent(device, i + s, IB_QPT_GSI,
1379 ®_req, 0, notice_resp_handler,
1380 notice_handler, &sa_dev->port[i]);
1382 if (IS_ERR(sa_dev->port[i].notice_agent))
1385 INIT_WORK(&sa_dev->port[i].update_task, update_sm_ah);
1388 ib_set_client_data(device, &sa_client, sa_dev);
1391 * We register our event handler after everything is set up,
1392 * and then update our cached info after the event handler is
1393 * registered to avoid any problems if a port changes state
1394 * during our initialization.
1397 INIT_IB_EVENT_HANDLER(&sa_dev->event_handler, device, ib_sa_event);
1398 if (ib_register_event_handler(&sa_dev->event_handler))
1401 for (i = 0; i <= e - s; ++i)
1402 if (rdma_port_get_link_layer(device, i + 1) == IB_LINK_LAYER_INFINIBAND)
1403 update_sm_ah(&sa_dev->port[i].update_task);
1409 if (rdma_port_get_link_layer(device, i + 1) == IB_LINK_LAYER_INFINIBAND) {
1410 if (!IS_ERR(sa_dev->port[i].notice_agent))
1411 ib_unregister_mad_agent(sa_dev->port[i].notice_agent);
1412 if (!IS_ERR(sa_dev->port[i].agent))
1413 ib_unregister_mad_agent(sa_dev->port[i].agent);
1421 static void ib_sa_remove_one(struct ib_device *device)
1423 struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
1429 ib_unregister_event_handler(&sa_dev->event_handler);
1431 flush_scheduled_work();
1433 for (i = 0; i <= sa_dev->end_port - sa_dev->start_port; ++i) {
1434 if (rdma_port_get_link_layer(device, i + 1) == IB_LINK_LAYER_INFINIBAND) {
1435 ib_unregister_mad_agent(sa_dev->port[i].notice_agent);
1436 ib_unregister_mad_agent(sa_dev->port[i].agent);
1437 if (sa_dev->port[i].sm_ah)
1438 kref_put(&sa_dev->port[i].sm_ah->ref, free_sm_ah);
1446 static int __init ib_sa_init(void)
1450 spin_lock_init(&idr_lock);
1451 spin_lock_init(&tid_lock);
1453 get_random_bytes(&tid, sizeof tid);
1455 ret = ib_register_client(&sa_client);
1457 printk(KERN_ERR "Couldn't register ib_sa client\n");
1463 printk(KERN_ERR "Couldn't initialize multicast handling\n");
1467 ret = notice_init();
1469 printk(KERN_ERR "Couldn't initialize notice handling\n");
1475 printk(KERN_ERR "Couldn't initialize local SA\n");
1485 ib_unregister_client(&sa_client);
1490 static void __exit ib_sa_cleanup(void)
1495 ib_unregister_client(&sa_client);
1496 idr_destroy(&query_idr);
1499 module_init_order(ib_sa_init, SI_ORDER_SECOND);
1500 module_exit(ib_sa_cleanup);