The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/ofed/drivers/infiniband/core/ib_sa_query.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
    3  *
    4  * Copyright (c) 2004 Topspin Communications.  All rights reserved.
    5  * Copyright (c) 2005 Voltaire, Inc.  All rights reserved.
    6  * Copyright (c) 2006 Intel Corporation.  All rights reserved.
    7  *
    8  * This software is available to you under a choice of one of two
    9  * licenses.  You may choose to be licensed under the terms of the GNU
   10  * General Public License (GPL) Version 2, available from the file
   11  * COPYING in the main directory of this source tree, or the
   12  * OpenIB.org BSD license below:
   13  *
   14  *     Redistribution and use in source and binary forms, with or
   15  *     without modification, are permitted provided that the following
   16  *     conditions are met:
   17  *
   18  *      - Redistributions of source code must retain the above
   19  *        copyright notice, this list of conditions and the following
   20  *        disclaimer.
   21  *
   22  *      - Redistributions in binary form must reproduce the above
   23  *        copyright notice, this list of conditions and the following
   24  *        disclaimer in the documentation and/or other materials
   25  *        provided with the distribution.
   26  *
   27  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   28  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   29  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
   30  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
   31  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
   32  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   33  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
   34  * SOFTWARE.
   35  */
   36 
   37 #include <sys/cdefs.h>
   38 __FBSDID("$FreeBSD$");
   39 
   40 #include <linux/module.h>
   41 #include <linux/err.h>
   42 #include <linux/random.h>
   43 #include <linux/spinlock.h>
   44 #include <linux/slab.h>
   45 #include <linux/dma-mapping.h>
   46 #include <linux/kref.h>
   47 #include <linux/idr.h>
   48 #include <linux/workqueue.h>
   49 #include <linux/etherdevice.h>
   50 #include <rdma/ib_pack.h>
   51 #include <rdma/ib_cache.h>
   52 #include <rdma/ib_user_sa.h>
   53 #include <rdma/ib_marshall.h>
   54 #include <rdma/ib_addr.h>
   55 #include "sa.h"
   56 #include "core_priv.h"
   57 
   58 #define IB_SA_LOCAL_SVC_TIMEOUT_MIN             100
   59 #define IB_SA_LOCAL_SVC_TIMEOUT_DEFAULT         2000
   60 #define IB_SA_LOCAL_SVC_TIMEOUT_MAX             200000
   61 
   62 struct ib_sa_sm_ah {
   63         struct ib_ah        *ah;
   64         struct kref          ref;
   65         u16                  pkey_index;
   66         u8                   src_path_mask;
   67 };
   68 
   69 struct ib_sa_classport_cache {
   70         bool valid;
   71         struct ib_class_port_info data;
   72 };
   73 
   74 struct ib_sa_port {
   75         struct ib_mad_agent *agent;
   76         struct ib_sa_sm_ah  *sm_ah;
   77         struct work_struct   update_task;
   78         struct ib_sa_classport_cache classport_info;
   79         spinlock_t                   classport_lock; /* protects class port info set */
   80         spinlock_t           ah_lock;
   81         u8                   port_num;
   82 };
   83 
   84 struct ib_sa_device {
   85         int                     start_port, end_port;
   86         struct ib_event_handler event_handler;
   87         struct ib_sa_port port[0];
   88 };
   89 
   90 struct ib_sa_query {
   91         void (*callback)(struct ib_sa_query *, int, struct ib_sa_mad *);
   92         void (*release)(struct ib_sa_query *);
   93         struct ib_sa_client    *client;
   94         struct ib_sa_port      *port;
   95         struct ib_mad_send_buf *mad_buf;
   96         struct ib_sa_sm_ah     *sm_ah;
   97         int                     id;
   98         u32                     flags;
   99         struct list_head        list; /* Local svc request list */
  100         u32                     seq; /* Local svc request sequence number */
  101         unsigned long           timeout; /* Local svc timeout */
  102         u8                      path_use; /* How will the pathrecord be used */
  103 };
  104 
  105 #define IB_SA_ENABLE_LOCAL_SERVICE      0x00000001
  106 #define IB_SA_CANCEL                    0x00000002
  107 
  108 struct ib_sa_service_query {
  109         void (*callback)(int, struct ib_sa_service_rec *, void *);
  110         void *context;
  111         struct ib_sa_query sa_query;
  112 };
  113 
  114 struct ib_sa_path_query {
  115         void (*callback)(int, struct ib_sa_path_rec *, void *);
  116         void *context;
  117         struct ib_sa_query sa_query;
  118 };
  119 
  120 struct ib_sa_guidinfo_query {
  121         void (*callback)(int, struct ib_sa_guidinfo_rec *, void *);
  122         void *context;
  123         struct ib_sa_query sa_query;
  124 };
  125 
  126 struct ib_sa_classport_info_query {
  127         void (*callback)(int, struct ib_class_port_info *, void *);
  128         void *context;
  129         struct ib_sa_query sa_query;
  130 };
  131 
  132 struct ib_sa_mcmember_query {
  133         void (*callback)(int, struct ib_sa_mcmember_rec *, void *);
  134         void *context;
  135         struct ib_sa_query sa_query;
  136 };
  137 
  138 static void ib_sa_add_one(struct ib_device *device);
  139 static void ib_sa_remove_one(struct ib_device *device, void *client_data);
  140 
  141 static struct ib_client sa_client = {
  142         .name   = "sa",
  143         .add    = ib_sa_add_one,
  144         .remove = ib_sa_remove_one
  145 };
  146 
  147 static DEFINE_SPINLOCK(idr_lock);
  148 static DEFINE_IDR(query_idr);
  149 
  150 static DEFINE_SPINLOCK(tid_lock);
  151 static u32 tid;
  152 
  153 #define PATH_REC_FIELD(field) \
  154         .struct_offset_bytes = offsetof(struct ib_sa_path_rec, field),          \
  155         .struct_size_bytes   = sizeof ((struct ib_sa_path_rec *) 0)->field,     \
  156         .field_name          = "sa_path_rec:" #field
  157 
  158 static const struct ib_field path_rec_table[] = {
  159         { PATH_REC_FIELD(service_id),
  160           .offset_words = 0,
  161           .offset_bits  = 0,
  162           .size_bits    = 64 },
  163         { PATH_REC_FIELD(dgid),
  164           .offset_words = 2,
  165           .offset_bits  = 0,
  166           .size_bits    = 128 },
  167         { PATH_REC_FIELD(sgid),
  168           .offset_words = 6,
  169           .offset_bits  = 0,
  170           .size_bits    = 128 },
  171         { PATH_REC_FIELD(dlid),
  172           .offset_words = 10,
  173           .offset_bits  = 0,
  174           .size_bits    = 16 },
  175         { PATH_REC_FIELD(slid),
  176           .offset_words = 10,
  177           .offset_bits  = 16,
  178           .size_bits    = 16 },
  179         { PATH_REC_FIELD(raw_traffic),
  180           .offset_words = 11,
  181           .offset_bits  = 0,
  182           .size_bits    = 1 },
  183         { RESERVED,
  184           .offset_words = 11,
  185           .offset_bits  = 1,
  186           .size_bits    = 3 },
  187         { PATH_REC_FIELD(flow_label),
  188           .offset_words = 11,
  189           .offset_bits  = 4,
  190           .size_bits    = 20 },
  191         { PATH_REC_FIELD(hop_limit),
  192           .offset_words = 11,
  193           .offset_bits  = 24,
  194           .size_bits    = 8 },
  195         { PATH_REC_FIELD(traffic_class),
  196           .offset_words = 12,
  197           .offset_bits  = 0,
  198           .size_bits    = 8 },
  199         { PATH_REC_FIELD(reversible),
  200           .offset_words = 12,
  201           .offset_bits  = 8,
  202           .size_bits    = 1 },
  203         { PATH_REC_FIELD(numb_path),
  204           .offset_words = 12,
  205           .offset_bits  = 9,
  206           .size_bits    = 7 },
  207         { PATH_REC_FIELD(pkey),
  208           .offset_words = 12,
  209           .offset_bits  = 16,
  210           .size_bits    = 16 },
  211         { PATH_REC_FIELD(qos_class),
  212           .offset_words = 13,
  213           .offset_bits  = 0,
  214           .size_bits    = 12 },
  215         { PATH_REC_FIELD(sl),
  216           .offset_words = 13,
  217           .offset_bits  = 12,
  218           .size_bits    = 4 },
  219         { PATH_REC_FIELD(mtu_selector),
  220           .offset_words = 13,
  221           .offset_bits  = 16,
  222           .size_bits    = 2 },
  223         { PATH_REC_FIELD(mtu),
  224           .offset_words = 13,
  225           .offset_bits  = 18,
  226           .size_bits    = 6 },
  227         { PATH_REC_FIELD(rate_selector),
  228           .offset_words = 13,
  229           .offset_bits  = 24,
  230           .size_bits    = 2 },
  231         { PATH_REC_FIELD(rate),
  232           .offset_words = 13,
  233           .offset_bits  = 26,
  234           .size_bits    = 6 },
  235         { PATH_REC_FIELD(packet_life_time_selector),
  236           .offset_words = 14,
  237           .offset_bits  = 0,
  238           .size_bits    = 2 },
  239         { PATH_REC_FIELD(packet_life_time),
  240           .offset_words = 14,
  241           .offset_bits  = 2,
  242           .size_bits    = 6 },
  243         { PATH_REC_FIELD(preference),
  244           .offset_words = 14,
  245           .offset_bits  = 8,
  246           .size_bits    = 8 },
  247         { RESERVED,
  248           .offset_words = 14,
  249           .offset_bits  = 16,
  250           .size_bits    = 48 },
  251 };
  252 
  253 #define MCMEMBER_REC_FIELD(field) \
  254         .struct_offset_bytes = offsetof(struct ib_sa_mcmember_rec, field),      \
  255         .struct_size_bytes   = sizeof ((struct ib_sa_mcmember_rec *) 0)->field, \
  256         .field_name          = "sa_mcmember_rec:" #field
  257 
  258 static const struct ib_field mcmember_rec_table[] = {
  259         { MCMEMBER_REC_FIELD(mgid),
  260           .offset_words = 0,
  261           .offset_bits  = 0,
  262           .size_bits    = 128 },
  263         { MCMEMBER_REC_FIELD(port_gid),
  264           .offset_words = 4,
  265           .offset_bits  = 0,
  266           .size_bits    = 128 },
  267         { MCMEMBER_REC_FIELD(qkey),
  268           .offset_words = 8,
  269           .offset_bits  = 0,
  270           .size_bits    = 32 },
  271         { MCMEMBER_REC_FIELD(mlid),
  272           .offset_words = 9,
  273           .offset_bits  = 0,
  274           .size_bits    = 16 },
  275         { MCMEMBER_REC_FIELD(mtu_selector),
  276           .offset_words = 9,
  277           .offset_bits  = 16,
  278           .size_bits    = 2 },
  279         { MCMEMBER_REC_FIELD(mtu),
  280           .offset_words = 9,
  281           .offset_bits  = 18,
  282           .size_bits    = 6 },
  283         { MCMEMBER_REC_FIELD(traffic_class),
  284           .offset_words = 9,
  285           .offset_bits  = 24,
  286           .size_bits    = 8 },
  287         { MCMEMBER_REC_FIELD(pkey),
  288           .offset_words = 10,
  289           .offset_bits  = 0,
  290           .size_bits    = 16 },
  291         { MCMEMBER_REC_FIELD(rate_selector),
  292           .offset_words = 10,
  293           .offset_bits  = 16,
  294           .size_bits    = 2 },
  295         { MCMEMBER_REC_FIELD(rate),
  296           .offset_words = 10,
  297           .offset_bits  = 18,
  298           .size_bits    = 6 },
  299         { MCMEMBER_REC_FIELD(packet_life_time_selector),
  300           .offset_words = 10,
  301           .offset_bits  = 24,
  302           .size_bits    = 2 },
  303         { MCMEMBER_REC_FIELD(packet_life_time),
  304           .offset_words = 10,
  305           .offset_bits  = 26,
  306           .size_bits    = 6 },
  307         { MCMEMBER_REC_FIELD(sl),
  308           .offset_words = 11,
  309           .offset_bits  = 0,
  310           .size_bits    = 4 },
  311         { MCMEMBER_REC_FIELD(flow_label),
  312           .offset_words = 11,
  313           .offset_bits  = 4,
  314           .size_bits    = 20 },
  315         { MCMEMBER_REC_FIELD(hop_limit),
  316           .offset_words = 11,
  317           .offset_bits  = 24,
  318           .size_bits    = 8 },
  319         { MCMEMBER_REC_FIELD(scope),
  320           .offset_words = 12,
  321           .offset_bits  = 0,
  322           .size_bits    = 4 },
  323         { MCMEMBER_REC_FIELD(join_state),
  324           .offset_words = 12,
  325           .offset_bits  = 4,
  326           .size_bits    = 4 },
  327         { MCMEMBER_REC_FIELD(proxy_join),
  328           .offset_words = 12,
  329           .offset_bits  = 8,
  330           .size_bits    = 1 },
  331         { RESERVED,
  332           .offset_words = 12,
  333           .offset_bits  = 9,
  334           .size_bits    = 23 },
  335 };
  336 
  337 #define SERVICE_REC_FIELD(field) \
  338         .struct_offset_bytes = offsetof(struct ib_sa_service_rec, field),       \
  339         .struct_size_bytes   = sizeof ((struct ib_sa_service_rec *) 0)->field,  \
  340         .field_name          = "sa_service_rec:" #field
  341 
  342 static const struct ib_field service_rec_table[] = {
  343         { SERVICE_REC_FIELD(id),
  344           .offset_words = 0,
  345           .offset_bits  = 0,
  346           .size_bits    = 64 },
  347         { SERVICE_REC_FIELD(gid),
  348           .offset_words = 2,
  349           .offset_bits  = 0,
  350           .size_bits    = 128 },
  351         { SERVICE_REC_FIELD(pkey),
  352           .offset_words = 6,
  353           .offset_bits  = 0,
  354           .size_bits    = 16 },
  355         { SERVICE_REC_FIELD(lease),
  356           .offset_words = 7,
  357           .offset_bits  = 0,
  358           .size_bits    = 32 },
  359         { SERVICE_REC_FIELD(key),
  360           .offset_words = 8,
  361           .offset_bits  = 0,
  362           .size_bits    = 128 },
  363         { SERVICE_REC_FIELD(name),
  364           .offset_words = 12,
  365           .offset_bits  = 0,
  366           .size_bits    = 64*8 },
  367         { SERVICE_REC_FIELD(data8),
  368           .offset_words = 28,
  369           .offset_bits  = 0,
  370           .size_bits    = 16*8 },
  371         { SERVICE_REC_FIELD(data16),
  372           .offset_words = 32,
  373           .offset_bits  = 0,
  374           .size_bits    = 8*16 },
  375         { SERVICE_REC_FIELD(data32),
  376           .offset_words = 36,
  377           .offset_bits  = 0,
  378           .size_bits    = 4*32 },
  379         { SERVICE_REC_FIELD(data64),
  380           .offset_words = 40,
  381           .offset_bits  = 0,
  382           .size_bits    = 2*64 },
  383 };
  384 
  385 #define CLASSPORTINFO_REC_FIELD(field) \
  386         .struct_offset_bytes = offsetof(struct ib_class_port_info, field),      \
  387         .struct_size_bytes   = sizeof((struct ib_class_port_info *)0)->field,   \
  388         .field_name          = "ib_class_port_info:" #field
  389 
  390 static const struct ib_field classport_info_rec_table[] = {
  391         { CLASSPORTINFO_REC_FIELD(base_version),
  392           .offset_words = 0,
  393           .offset_bits  = 0,
  394           .size_bits    = 8 },
  395         { CLASSPORTINFO_REC_FIELD(class_version),
  396           .offset_words = 0,
  397           .offset_bits  = 8,
  398           .size_bits    = 8 },
  399         { CLASSPORTINFO_REC_FIELD(capability_mask),
  400           .offset_words = 0,
  401           .offset_bits  = 16,
  402           .size_bits    = 16 },
  403         { CLASSPORTINFO_REC_FIELD(cap_mask2_resp_time),
  404           .offset_words = 1,
  405           .offset_bits  = 0,
  406           .size_bits    = 32 },
  407         { CLASSPORTINFO_REC_FIELD(redirect_gid),
  408           .offset_words = 2,
  409           .offset_bits  = 0,
  410           .size_bits    = 128 },
  411         { CLASSPORTINFO_REC_FIELD(redirect_tcslfl),
  412           .offset_words = 6,
  413           .offset_bits  = 0,
  414           .size_bits    = 32 },
  415         { CLASSPORTINFO_REC_FIELD(redirect_lid),
  416           .offset_words = 7,
  417           .offset_bits  = 0,
  418           .size_bits    = 16 },
  419         { CLASSPORTINFO_REC_FIELD(redirect_pkey),
  420           .offset_words = 7,
  421           .offset_bits  = 16,
  422           .size_bits    = 16 },
  423 
  424         { CLASSPORTINFO_REC_FIELD(redirect_qp),
  425           .offset_words = 8,
  426           .offset_bits  = 0,
  427           .size_bits    = 32 },
  428         { CLASSPORTINFO_REC_FIELD(redirect_qkey),
  429           .offset_words = 9,
  430           .offset_bits  = 0,
  431           .size_bits    = 32 },
  432 
  433         { CLASSPORTINFO_REC_FIELD(trap_gid),
  434           .offset_words = 10,
  435           .offset_bits  = 0,
  436           .size_bits    = 128 },
  437         { CLASSPORTINFO_REC_FIELD(trap_tcslfl),
  438           .offset_words = 14,
  439           .offset_bits  = 0,
  440           .size_bits    = 32 },
  441 
  442         { CLASSPORTINFO_REC_FIELD(trap_lid),
  443           .offset_words = 15,
  444           .offset_bits  = 0,
  445           .size_bits    = 16 },
  446         { CLASSPORTINFO_REC_FIELD(trap_pkey),
  447           .offset_words = 15,
  448           .offset_bits  = 16,
  449           .size_bits    = 16 },
  450 
  451         { CLASSPORTINFO_REC_FIELD(trap_hlqp),
  452           .offset_words = 16,
  453           .offset_bits  = 0,
  454           .size_bits    = 32 },
  455         { CLASSPORTINFO_REC_FIELD(trap_qkey),
  456           .offset_words = 17,
  457           .offset_bits  = 0,
  458           .size_bits    = 32 },
  459 };
  460 
  461 #define GUIDINFO_REC_FIELD(field) \
  462         .struct_offset_bytes = offsetof(struct ib_sa_guidinfo_rec, field),      \
  463         .struct_size_bytes   = sizeof((struct ib_sa_guidinfo_rec *) 0)->field,  \
  464         .field_name          = "sa_guidinfo_rec:" #field
  465 
  466 static const struct ib_field guidinfo_rec_table[] = {
  467         { GUIDINFO_REC_FIELD(lid),
  468           .offset_words = 0,
  469           .offset_bits  = 0,
  470           .size_bits    = 16 },
  471         { GUIDINFO_REC_FIELD(block_num),
  472           .offset_words = 0,
  473           .offset_bits  = 16,
  474           .size_bits    = 8 },
  475         { GUIDINFO_REC_FIELD(res1),
  476           .offset_words = 0,
  477           .offset_bits  = 24,
  478           .size_bits    = 8 },
  479         { GUIDINFO_REC_FIELD(res2),
  480           .offset_words = 1,
  481           .offset_bits  = 0,
  482           .size_bits    = 32 },
  483         { GUIDINFO_REC_FIELD(guid_info_list),
  484           .offset_words = 2,
  485           .offset_bits  = 0,
  486           .size_bits    = 512 },
  487 };
  488 
  489 static inline void ib_sa_disable_local_svc(struct ib_sa_query *query)
  490 {
  491         query->flags &= ~IB_SA_ENABLE_LOCAL_SERVICE;
  492 }
  493 
  494 static void free_sm_ah(struct kref *kref)
  495 {
  496         struct ib_sa_sm_ah *sm_ah = container_of(kref, struct ib_sa_sm_ah, ref);
  497 
  498         ib_destroy_ah(sm_ah->ah, 0);
  499         kfree(sm_ah);
  500 }
  501 
  502 static void update_sm_ah(struct work_struct *work)
  503 {
  504         struct ib_sa_port *port =
  505                 container_of(work, struct ib_sa_port, update_task);
  506         struct ib_sa_sm_ah *new_ah;
  507         struct ib_port_attr port_attr;
  508         struct ib_ah_attr   ah_attr;
  509 
  510         if (ib_query_port(port->agent->device, port->port_num, &port_attr)) {
  511                 pr_warn("Couldn't query port\n");
  512                 return;
  513         }
  514 
  515         new_ah = kmalloc(sizeof *new_ah, GFP_KERNEL);
  516         if (!new_ah) {
  517                 return;
  518         }
  519 
  520         kref_init(&new_ah->ref);
  521         new_ah->src_path_mask = (1 << port_attr.lmc) - 1;
  522 
  523         new_ah->pkey_index = 0;
  524         if (ib_find_pkey(port->agent->device, port->port_num,
  525                          IB_DEFAULT_PKEY_FULL, &new_ah->pkey_index))
  526                 pr_err("Couldn't find index for default PKey\n");
  527 
  528         memset(&ah_attr, 0, sizeof ah_attr);
  529         ah_attr.dlid     = port_attr.sm_lid;
  530         ah_attr.sl       = port_attr.sm_sl;
  531         ah_attr.port_num = port->port_num;
  532         if (port_attr.grh_required) {
  533                 ah_attr.ah_flags = IB_AH_GRH;
  534                 ah_attr.grh.dgid.global.subnet_prefix = cpu_to_be64(port_attr.subnet_prefix);
  535                 ah_attr.grh.dgid.global.interface_id = cpu_to_be64(IB_SA_WELL_KNOWN_GUID);
  536         }
  537 
  538         new_ah->ah = ib_create_ah(port->agent->qp->pd, &ah_attr, RDMA_CREATE_AH_SLEEPABLE);
  539         if (IS_ERR(new_ah->ah)) {
  540                 pr_warn("Couldn't create new SM AH\n");
  541                 kfree(new_ah);
  542                 return;
  543         }
  544 
  545         spin_lock_irq(&port->ah_lock);
  546         if (port->sm_ah)
  547                 kref_put(&port->sm_ah->ref, free_sm_ah);
  548         port->sm_ah = new_ah;
  549         spin_unlock_irq(&port->ah_lock);
  550 
  551 }
  552 
  553 static void ib_sa_event(struct ib_event_handler *handler, struct ib_event *event)
  554 {
  555         if (event->event == IB_EVENT_PORT_ERR    ||
  556             event->event == IB_EVENT_PORT_ACTIVE ||
  557             event->event == IB_EVENT_LID_CHANGE  ||
  558             event->event == IB_EVENT_PKEY_CHANGE ||
  559             event->event == IB_EVENT_SM_CHANGE   ||
  560             event->event == IB_EVENT_CLIENT_REREGISTER) {
  561                 unsigned long flags;
  562                 struct ib_sa_device *sa_dev =
  563                         container_of(handler, typeof(*sa_dev), event_handler);
  564                 struct ib_sa_port *port =
  565                         &sa_dev->port[event->element.port_num - sa_dev->start_port];
  566 
  567                 if (!rdma_cap_ib_sa(handler->device, port->port_num))
  568                         return;
  569 
  570                 spin_lock_irqsave(&port->ah_lock, flags);
  571                 if (port->sm_ah)
  572                         kref_put(&port->sm_ah->ref, free_sm_ah);
  573                 port->sm_ah = NULL;
  574                 spin_unlock_irqrestore(&port->ah_lock, flags);
  575 
  576                 if (event->event == IB_EVENT_SM_CHANGE ||
  577                     event->event == IB_EVENT_CLIENT_REREGISTER ||
  578                     event->event == IB_EVENT_LID_CHANGE) {
  579                         spin_lock_irqsave(&port->classport_lock, flags);
  580                         port->classport_info.valid = false;
  581                         spin_unlock_irqrestore(&port->classport_lock, flags);
  582                 }
  583                 queue_work(ib_wq, &sa_dev->port[event->element.port_num -
  584                                             sa_dev->start_port].update_task);
  585         }
  586 }
  587 
  588 void ib_sa_register_client(struct ib_sa_client *client)
  589 {
  590         atomic_set(&client->users, 1);
  591         init_completion(&client->comp);
  592 }
  593 EXPORT_SYMBOL(ib_sa_register_client);
  594 
  595 void ib_sa_unregister_client(struct ib_sa_client *client)
  596 {
  597         ib_sa_client_put(client);
  598         wait_for_completion(&client->comp);
  599 }
  600 EXPORT_SYMBOL(ib_sa_unregister_client);
  601 
  602 /**
  603  * ib_sa_cancel_query - try to cancel an SA query
  604  * @id:ID of query to cancel
  605  * @query:query pointer to cancel
  606  *
  607  * Try to cancel an SA query.  If the id and query don't match up or
  608  * the query has already completed, nothing is done.  Otherwise the
  609  * query is canceled and will complete with a status of -EINTR.
  610  */
  611 void ib_sa_cancel_query(int id, struct ib_sa_query *query)
  612 {
  613         unsigned long flags;
  614         struct ib_mad_agent *agent;
  615         struct ib_mad_send_buf *mad_buf;
  616 
  617         spin_lock_irqsave(&idr_lock, flags);
  618         if (idr_find(&query_idr, id) != query) {
  619                 spin_unlock_irqrestore(&idr_lock, flags);
  620                 return;
  621         }
  622         agent = query->port->agent;
  623         mad_buf = query->mad_buf;
  624         spin_unlock_irqrestore(&idr_lock, flags);
  625 
  626         ib_cancel_mad(agent, mad_buf);
  627 }
  628 EXPORT_SYMBOL(ib_sa_cancel_query);
  629 
  630 static u8 get_src_path_mask(struct ib_device *device, u8 port_num)
  631 {
  632         struct ib_sa_device *sa_dev;
  633         struct ib_sa_port   *port;
  634         unsigned long flags;
  635         u8 src_path_mask;
  636 
  637         sa_dev = ib_get_client_data(device, &sa_client);
  638         if (!sa_dev)
  639                 return 0x7f;
  640 
  641         port  = &sa_dev->port[port_num - sa_dev->start_port];
  642         spin_lock_irqsave(&port->ah_lock, flags);
  643         src_path_mask = port->sm_ah ? port->sm_ah->src_path_mask : 0x7f;
  644         spin_unlock_irqrestore(&port->ah_lock, flags);
  645 
  646         return src_path_mask;
  647 }
  648 
  649 int ib_init_ah_from_path(struct ib_device *device, u8 port_num,
  650                          struct ib_sa_path_rec *rec, struct ib_ah_attr *ah_attr)
  651 {
  652         int ret;
  653         u16 gid_index;
  654         int use_roce;
  655         struct ifnet *ndev = NULL;
  656 
  657         memset(ah_attr, 0, sizeof *ah_attr);
  658         ah_attr->dlid = be16_to_cpu(rec->dlid);
  659         ah_attr->sl = rec->sl;
  660         ah_attr->src_path_bits = be16_to_cpu(rec->slid) &
  661                                  get_src_path_mask(device, port_num);
  662         ah_attr->port_num = port_num;
  663         ah_attr->static_rate = rec->rate;
  664 
  665         use_roce = rdma_cap_eth_ah(device, port_num);
  666 
  667         if (use_roce) {
  668                 struct ifnet *idev;
  669                 struct ifnet *resolved_dev;
  670                 struct rdma_dev_addr dev_addr = {.bound_dev_if = rec->ifindex,
  671                                                  .net = rec->net ? rec->net :
  672                                                          &init_net};
  673                 union rdma_sockaddr sgid_addr, dgid_addr;
  674 
  675                 if (!device->get_netdev)
  676                         return -EOPNOTSUPP;
  677 
  678                 rdma_gid2ip(&sgid_addr._sockaddr, &rec->sgid);
  679                 rdma_gid2ip(&dgid_addr._sockaddr, &rec->dgid);
  680 
  681                 /* validate the route */
  682                 ret = rdma_resolve_ip_route(&sgid_addr._sockaddr,
  683                                             &dgid_addr._sockaddr, &dev_addr);
  684                 if (ret)
  685                         return ret;
  686 
  687                 if ((dev_addr.network == RDMA_NETWORK_IPV4 ||
  688                      dev_addr.network == RDMA_NETWORK_IPV6) &&
  689                     rec->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
  690                         return -EINVAL;
  691 
  692                 idev = device->get_netdev(device, port_num);
  693                 if (!idev)
  694                         return -ENODEV;
  695 
  696                 resolved_dev = dev_get_by_index(dev_addr.net,
  697                                                 dev_addr.bound_dev_if);
  698                 if (!resolved_dev) {
  699                         dev_put(idev);
  700                         return -ENODEV;
  701                 }
  702                 ndev = ib_get_ndev_from_path(rec);
  703                 if ((ndev && ndev != resolved_dev) ||
  704                     (resolved_dev != idev &&
  705                      rdma_vlan_dev_real_dev(resolved_dev) != idev))
  706                         ret = -EHOSTUNREACH;
  707                 dev_put(idev);
  708                 dev_put(resolved_dev);
  709                 if (ret) {
  710                         if (ndev)
  711                                 dev_put(ndev);
  712                         return ret;
  713                 }
  714         }
  715 
  716         if (rec->hop_limit > 0 || use_roce) {
  717                 ah_attr->ah_flags = IB_AH_GRH;
  718                 ah_attr->grh.dgid = rec->dgid;
  719 
  720                 ret = ib_find_cached_gid_by_port(device, &rec->sgid,
  721                                                  rec->gid_type, port_num, ndev,
  722                                                  &gid_index);
  723                 if (ret) {
  724                         if (ndev)
  725                                 dev_put(ndev);
  726                         return ret;
  727                 }
  728 
  729                 ah_attr->grh.sgid_index    = gid_index;
  730                 ah_attr->grh.flow_label    = be32_to_cpu(rec->flow_label);
  731                 ah_attr->grh.hop_limit     = rec->hop_limit;
  732                 ah_attr->grh.traffic_class = rec->traffic_class;
  733                 if (ndev)
  734                         dev_put(ndev);
  735         }
  736 
  737         if (use_roce)
  738                 memcpy(ah_attr->dmac, rec->dmac, ETH_ALEN);
  739 
  740         return 0;
  741 }
  742 EXPORT_SYMBOL(ib_init_ah_from_path);
  743 
  744 static int alloc_mad(struct ib_sa_query *query, gfp_t gfp_mask)
  745 {
  746         unsigned long flags;
  747 
  748         spin_lock_irqsave(&query->port->ah_lock, flags);
  749         if (!query->port->sm_ah) {
  750                 spin_unlock_irqrestore(&query->port->ah_lock, flags);
  751                 return -EAGAIN;
  752         }
  753         kref_get(&query->port->sm_ah->ref);
  754         query->sm_ah = query->port->sm_ah;
  755         spin_unlock_irqrestore(&query->port->ah_lock, flags);
  756 
  757         query->mad_buf = ib_create_send_mad(query->port->agent, 1,
  758                                             query->sm_ah->pkey_index,
  759                                             0, IB_MGMT_SA_HDR, IB_MGMT_SA_DATA,
  760                                             gfp_mask,
  761                                             IB_MGMT_BASE_VERSION);
  762         if (IS_ERR(query->mad_buf)) {
  763                 kref_put(&query->sm_ah->ref, free_sm_ah);
  764                 return -ENOMEM;
  765         }
  766 
  767         query->mad_buf->ah = query->sm_ah->ah;
  768 
  769         return 0;
  770 }
  771 
  772 static void free_mad(struct ib_sa_query *query)
  773 {
  774         ib_free_send_mad(query->mad_buf);
  775         kref_put(&query->sm_ah->ref, free_sm_ah);
  776 }
  777 
  778 static void init_mad(struct ib_sa_mad *mad, struct ib_mad_agent *agent)
  779 {
  780         unsigned long flags;
  781 
  782         memset(mad, 0, sizeof *mad);
  783 
  784         mad->mad_hdr.base_version  = IB_MGMT_BASE_VERSION;
  785         mad->mad_hdr.mgmt_class    = IB_MGMT_CLASS_SUBN_ADM;
  786         mad->mad_hdr.class_version = IB_SA_CLASS_VERSION;
  787 
  788         spin_lock_irqsave(&tid_lock, flags);
  789         mad->mad_hdr.tid           =
  790                 cpu_to_be64(((u64) agent->hi_tid) << 32 | tid++);
  791         spin_unlock_irqrestore(&tid_lock, flags);
  792 }
  793 
  794 static int send_mad(struct ib_sa_query *query, int timeout_ms, gfp_t gfp_mask)
  795 {
  796         bool preload = gfpflags_allow_blocking(gfp_mask);
  797         unsigned long flags;
  798         int ret, id;
  799 
  800         if (preload)
  801                 idr_preload(gfp_mask);
  802         spin_lock_irqsave(&idr_lock, flags);
  803 
  804         id = idr_alloc(&query_idr, query, 0, 0, GFP_NOWAIT);
  805 
  806         spin_unlock_irqrestore(&idr_lock, flags);
  807         if (preload)
  808                 idr_preload_end();
  809         if (id < 0)
  810                 return id;
  811 
  812         query->mad_buf->timeout_ms  = timeout_ms;
  813         query->mad_buf->context[0] = query;
  814         query->id = id;
  815 
  816         if (query->flags & IB_SA_ENABLE_LOCAL_SERVICE) {
  817                 ib_sa_disable_local_svc(query);
  818         }
  819 
  820         ret = ib_post_send_mad(query->mad_buf, NULL);
  821         if (ret) {
  822                 spin_lock_irqsave(&idr_lock, flags);
  823                 idr_remove(&query_idr, id);
  824                 spin_unlock_irqrestore(&idr_lock, flags);
  825         }
  826 
  827         /*
  828          * It's not safe to dereference query any more, because the
  829          * send may already have completed and freed the query in
  830          * another context.
  831          */
  832         return ret ? ret : id;
  833 }
  834 
  835 void ib_sa_unpack_path(void *attribute, struct ib_sa_path_rec *rec)
  836 {
  837         ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table), attribute, rec);
  838 }
  839 EXPORT_SYMBOL(ib_sa_unpack_path);
  840 
  841 void ib_sa_pack_path(struct ib_sa_path_rec *rec, void *attribute)
  842 {
  843         ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table), rec, attribute);
  844 }
  845 EXPORT_SYMBOL(ib_sa_pack_path);
  846 
  847 static void ib_sa_path_rec_callback(struct ib_sa_query *sa_query,
  848                                     int status,
  849                                     struct ib_sa_mad *mad)
  850 {
  851         struct ib_sa_path_query *query =
  852                 container_of(sa_query, struct ib_sa_path_query, sa_query);
  853 
  854         if (mad) {
  855                 struct ib_sa_path_rec rec;
  856 
  857                 ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table),
  858                           mad->data, &rec);
  859                 rec.net = NULL;
  860                 rec.ifindex = 0;
  861                 rec.gid_type = IB_GID_TYPE_IB;
  862                 eth_zero_addr(rec.dmac);
  863                 query->callback(status, &rec, query->context);
  864         } else
  865                 query->callback(status, NULL, query->context);
  866 }
  867 
  868 static void ib_sa_path_rec_release(struct ib_sa_query *sa_query)
  869 {
  870         kfree(container_of(sa_query, struct ib_sa_path_query, sa_query));
  871 }
  872 
  873 /**
  874  * ib_sa_path_rec_get - Start a Path get query
  875  * @client:SA client
  876  * @device:device to send query on
  877  * @port_num: port number to send query on
  878  * @rec:Path Record to send in query
  879  * @comp_mask:component mask to send in query
  880  * @timeout_ms:time to wait for response
  881  * @gfp_mask:GFP mask to use for internal allocations
  882  * @callback:function called when query completes, times out or is
  883  * canceled
  884  * @context:opaque user context passed to callback
  885  * @sa_query:query context, used to cancel query
  886  *
  887  * Send a Path Record Get query to the SA to look up a path.  The
  888  * callback function will be called when the query completes (or
  889  * fails); status is 0 for a successful response, -EINTR if the query
  890  * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
  891  * occurred sending the query.  The resp parameter of the callback is
  892  * only valid if status is 0.
  893  *
  894  * If the return value of ib_sa_path_rec_get() is negative, it is an
  895  * error code.  Otherwise it is a query ID that can be used to cancel
  896  * the query.
  897  */
  898 int ib_sa_path_rec_get(struct ib_sa_client *client,
  899                        struct ib_device *device, u8 port_num,
  900                        struct ib_sa_path_rec *rec,
  901                        ib_sa_comp_mask comp_mask,
  902                        int timeout_ms, gfp_t gfp_mask,
  903                        void (*callback)(int status,
  904                                         struct ib_sa_path_rec *resp,
  905                                         void *context),
  906                        void *context,
  907                        struct ib_sa_query **sa_query)
  908 {
  909         struct ib_sa_path_query *query;
  910         struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  911         struct ib_sa_port   *port;
  912         struct ib_mad_agent *agent;
  913         struct ib_sa_mad *mad;
  914         int ret;
  915 
  916         if (!sa_dev)
  917                 return -ENODEV;
  918 
  919         port  = &sa_dev->port[port_num - sa_dev->start_port];
  920         agent = port->agent;
  921 
  922         query = kzalloc(sizeof(*query), gfp_mask);
  923         if (!query)
  924                 return -ENOMEM;
  925 
  926         query->sa_query.port     = port;
  927         ret = alloc_mad(&query->sa_query, gfp_mask);
  928         if (ret)
  929                 goto err1;
  930 
  931         ib_sa_client_get(client);
  932         query->sa_query.client = client;
  933         query->callback        = callback;
  934         query->context         = context;
  935 
  936         mad = query->sa_query.mad_buf->mad;
  937         init_mad(mad, agent);
  938 
  939         query->sa_query.callback = callback ? ib_sa_path_rec_callback : NULL;
  940         query->sa_query.release  = ib_sa_path_rec_release;
  941         mad->mad_hdr.method      = IB_MGMT_METHOD_GET;
  942         mad->mad_hdr.attr_id     = cpu_to_be16(IB_SA_ATTR_PATH_REC);
  943         mad->sa_hdr.comp_mask    = comp_mask;
  944 
  945         ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table), rec, mad->data);
  946 
  947         *sa_query = &query->sa_query;
  948 
  949         query->sa_query.flags |= IB_SA_ENABLE_LOCAL_SERVICE;
  950         query->sa_query.mad_buf->context[1] = rec;
  951 
  952         ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  953         if (ret < 0)
  954                 goto err2;
  955 
  956         return ret;
  957 
  958 err2:
  959         *sa_query = NULL;
  960         ib_sa_client_put(query->sa_query.client);
  961         free_mad(&query->sa_query);
  962 
  963 err1:
  964         kfree(query);
  965         return ret;
  966 }
  967 EXPORT_SYMBOL(ib_sa_path_rec_get);
  968 
  969 static void ib_sa_service_rec_callback(struct ib_sa_query *sa_query,
  970                                     int status,
  971                                     struct ib_sa_mad *mad)
  972 {
  973         struct ib_sa_service_query *query =
  974                 container_of(sa_query, struct ib_sa_service_query, sa_query);
  975 
  976         if (mad) {
  977                 struct ib_sa_service_rec rec;
  978 
  979                 ib_unpack(service_rec_table, ARRAY_SIZE(service_rec_table),
  980                           mad->data, &rec);
  981                 query->callback(status, &rec, query->context);
  982         } else
  983                 query->callback(status, NULL, query->context);
  984 }
  985 
  986 static void ib_sa_service_rec_release(struct ib_sa_query *sa_query)
  987 {
  988         kfree(container_of(sa_query, struct ib_sa_service_query, sa_query));
  989 }
  990 
  991 /**
  992  * ib_sa_service_rec_query - Start Service Record operation
  993  * @client:SA client
  994  * @device:device to send request on
  995  * @port_num: port number to send request on
  996  * @method:SA method - should be get, set, or delete
  997  * @rec:Service Record to send in request
  998  * @comp_mask:component mask to send in request
  999  * @timeout_ms:time to wait for response
 1000  * @gfp_mask:GFP mask to use for internal allocations
 1001  * @callback:function called when request completes, times out or is
 1002  * canceled
 1003  * @context:opaque user context passed to callback
 1004  * @sa_query:request context, used to cancel request
 1005  *
 1006  * Send a Service Record set/get/delete to the SA to register,
 1007  * unregister or query a service record.
 1008  * The callback function will be called when the request completes (or
 1009  * fails); status is 0 for a successful response, -EINTR if the query
 1010  * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
 1011  * occurred sending the query.  The resp parameter of the callback is
 1012  * only valid if status is 0.
 1013  *
 1014  * If the return value of ib_sa_service_rec_query() is negative, it is an
 1015  * error code.  Otherwise it is a request ID that can be used to cancel
 1016  * the query.
 1017  */
 1018 int ib_sa_service_rec_query(struct ib_sa_client *client,
 1019                             struct ib_device *device, u8 port_num, u8 method,
 1020                             struct ib_sa_service_rec *rec,
 1021                             ib_sa_comp_mask comp_mask,
 1022                             int timeout_ms, gfp_t gfp_mask,
 1023                             void (*callback)(int status,
 1024                                              struct ib_sa_service_rec *resp,
 1025                                              void *context),
 1026                             void *context,
 1027                             struct ib_sa_query **sa_query)
 1028 {
 1029         struct ib_sa_service_query *query;
 1030         struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
 1031         struct ib_sa_port   *port;
 1032         struct ib_mad_agent *agent;
 1033         struct ib_sa_mad *mad;
 1034         int ret;
 1035 
 1036         if (!sa_dev)
 1037                 return -ENODEV;
 1038 
 1039         port  = &sa_dev->port[port_num - sa_dev->start_port];
 1040         agent = port->agent;
 1041 
 1042         if (method != IB_MGMT_METHOD_GET &&
 1043             method != IB_MGMT_METHOD_SET &&
 1044             method != IB_SA_METHOD_DELETE)
 1045                 return -EINVAL;
 1046 
 1047         query = kzalloc(sizeof(*query), gfp_mask);
 1048         if (!query)
 1049                 return -ENOMEM;
 1050 
 1051         query->sa_query.port     = port;
 1052         ret = alloc_mad(&query->sa_query, gfp_mask);
 1053         if (ret)
 1054                 goto err1;
 1055 
 1056         ib_sa_client_get(client);
 1057         query->sa_query.client = client;
 1058         query->callback        = callback;
 1059         query->context         = context;
 1060 
 1061         mad = query->sa_query.mad_buf->mad;
 1062         init_mad(mad, agent);
 1063 
 1064         query->sa_query.callback = callback ? ib_sa_service_rec_callback : NULL;
 1065         query->sa_query.release  = ib_sa_service_rec_release;
 1066         mad->mad_hdr.method      = method;
 1067         mad->mad_hdr.attr_id     = cpu_to_be16(IB_SA_ATTR_SERVICE_REC);
 1068         mad->sa_hdr.comp_mask    = comp_mask;
 1069 
 1070         ib_pack(service_rec_table, ARRAY_SIZE(service_rec_table),
 1071                 rec, mad->data);
 1072 
 1073         *sa_query = &query->sa_query;
 1074 
 1075         ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
 1076         if (ret < 0)
 1077                 goto err2;
 1078 
 1079         return ret;
 1080 
 1081 err2:
 1082         *sa_query = NULL;
 1083         ib_sa_client_put(query->sa_query.client);
 1084         free_mad(&query->sa_query);
 1085 
 1086 err1:
 1087         kfree(query);
 1088         return ret;
 1089 }
 1090 EXPORT_SYMBOL(ib_sa_service_rec_query);
 1091 
 1092 static void ib_sa_mcmember_rec_callback(struct ib_sa_query *sa_query,
 1093                                         int status,
 1094                                         struct ib_sa_mad *mad)
 1095 {
 1096         struct ib_sa_mcmember_query *query =
 1097                 container_of(sa_query, struct ib_sa_mcmember_query, sa_query);
 1098 
 1099         if (mad) {
 1100                 struct ib_sa_mcmember_rec rec;
 1101 
 1102                 ib_unpack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
 1103                           mad->data, &rec);
 1104                 query->callback(status, &rec, query->context);
 1105         } else
 1106                 query->callback(status, NULL, query->context);
 1107 }
 1108 
 1109 static void ib_sa_mcmember_rec_release(struct ib_sa_query *sa_query)
 1110 {
 1111         kfree(container_of(sa_query, struct ib_sa_mcmember_query, sa_query));
 1112 }
 1113 
 1114 int ib_sa_mcmember_rec_query(struct ib_sa_client *client,
 1115                              struct ib_device *device, u8 port_num,
 1116                              u8 method,
 1117                              struct ib_sa_mcmember_rec *rec,
 1118                              ib_sa_comp_mask comp_mask,
 1119                              int timeout_ms, gfp_t gfp_mask,
 1120                              void (*callback)(int status,
 1121                                               struct ib_sa_mcmember_rec *resp,
 1122                                               void *context),
 1123                              void *context,
 1124                              struct ib_sa_query **sa_query)
 1125 {
 1126         struct ib_sa_mcmember_query *query;
 1127         struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
 1128         struct ib_sa_port   *port;
 1129         struct ib_mad_agent *agent;
 1130         struct ib_sa_mad *mad;
 1131         int ret;
 1132 
 1133         if (!sa_dev)
 1134                 return -ENODEV;
 1135 
 1136         port  = &sa_dev->port[port_num - sa_dev->start_port];
 1137         agent = port->agent;
 1138 
 1139         query = kzalloc(sizeof(*query), gfp_mask);
 1140         if (!query)
 1141                 return -ENOMEM;
 1142 
 1143         query->sa_query.port     = port;
 1144         ret = alloc_mad(&query->sa_query, gfp_mask);
 1145         if (ret)
 1146                 goto err1;
 1147 
 1148         ib_sa_client_get(client);
 1149         query->sa_query.client = client;
 1150         query->callback        = callback;
 1151         query->context         = context;
 1152 
 1153         mad = query->sa_query.mad_buf->mad;
 1154         init_mad(mad, agent);
 1155 
 1156         query->sa_query.callback = callback ? ib_sa_mcmember_rec_callback : NULL;
 1157         query->sa_query.release  = ib_sa_mcmember_rec_release;
 1158         mad->mad_hdr.method      = method;
 1159         mad->mad_hdr.attr_id     = cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC);
 1160         mad->sa_hdr.comp_mask    = comp_mask;
 1161 
 1162         ib_pack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
 1163                 rec, mad->data);
 1164 
 1165         *sa_query = &query->sa_query;
 1166 
 1167         ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
 1168         if (ret < 0)
 1169                 goto err2;
 1170 
 1171         return ret;
 1172 
 1173 err2:
 1174         *sa_query = NULL;
 1175         ib_sa_client_put(query->sa_query.client);
 1176         free_mad(&query->sa_query);
 1177 
 1178 err1:
 1179         kfree(query);
 1180         return ret;
 1181 }
 1182 
 1183 /* Support GuidInfoRecord */
 1184 static void ib_sa_guidinfo_rec_callback(struct ib_sa_query *sa_query,
 1185                                         int status,
 1186                                         struct ib_sa_mad *mad)
 1187 {
 1188         struct ib_sa_guidinfo_query *query =
 1189                 container_of(sa_query, struct ib_sa_guidinfo_query, sa_query);
 1190 
 1191         if (mad) {
 1192                 struct ib_sa_guidinfo_rec rec;
 1193 
 1194                 ib_unpack(guidinfo_rec_table, ARRAY_SIZE(guidinfo_rec_table),
 1195                           mad->data, &rec);
 1196                 query->callback(status, &rec, query->context);
 1197         } else
 1198                 query->callback(status, NULL, query->context);
 1199 }
 1200 
 1201 static void ib_sa_guidinfo_rec_release(struct ib_sa_query *sa_query)
 1202 {
 1203         kfree(container_of(sa_query, struct ib_sa_guidinfo_query, sa_query));
 1204 }
 1205 
 1206 int ib_sa_guid_info_rec_query(struct ib_sa_client *client,
 1207                               struct ib_device *device, u8 port_num,
 1208                               struct ib_sa_guidinfo_rec *rec,
 1209                               ib_sa_comp_mask comp_mask, u8 method,
 1210                               int timeout_ms, gfp_t gfp_mask,
 1211                               void (*callback)(int status,
 1212                                                struct ib_sa_guidinfo_rec *resp,
 1213                                                void *context),
 1214                               void *context,
 1215                               struct ib_sa_query **sa_query)
 1216 {
 1217         struct ib_sa_guidinfo_query *query;
 1218         struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
 1219         struct ib_sa_port *port;
 1220         struct ib_mad_agent *agent;
 1221         struct ib_sa_mad *mad;
 1222         int ret;
 1223 
 1224         if (!sa_dev)
 1225                 return -ENODEV;
 1226 
 1227         if (method != IB_MGMT_METHOD_GET &&
 1228             method != IB_MGMT_METHOD_SET &&
 1229             method != IB_SA_METHOD_DELETE) {
 1230                 return -EINVAL;
 1231         }
 1232 
 1233         port  = &sa_dev->port[port_num - sa_dev->start_port];
 1234         agent = port->agent;
 1235 
 1236         query = kzalloc(sizeof(*query), gfp_mask);
 1237         if (!query)
 1238                 return -ENOMEM;
 1239 
 1240         query->sa_query.port = port;
 1241         ret = alloc_mad(&query->sa_query, gfp_mask);
 1242         if (ret)
 1243                 goto err1;
 1244 
 1245         ib_sa_client_get(client);
 1246         query->sa_query.client = client;
 1247         query->callback        = callback;
 1248         query->context         = context;
 1249 
 1250         mad = query->sa_query.mad_buf->mad;
 1251         init_mad(mad, agent);
 1252 
 1253         query->sa_query.callback = callback ? ib_sa_guidinfo_rec_callback : NULL;
 1254         query->sa_query.release  = ib_sa_guidinfo_rec_release;
 1255 
 1256         mad->mad_hdr.method      = method;
 1257         mad->mad_hdr.attr_id     = cpu_to_be16(IB_SA_ATTR_GUID_INFO_REC);
 1258         mad->sa_hdr.comp_mask    = comp_mask;
 1259 
 1260         ib_pack(guidinfo_rec_table, ARRAY_SIZE(guidinfo_rec_table), rec,
 1261                 mad->data);
 1262 
 1263         *sa_query = &query->sa_query;
 1264 
 1265         ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
 1266         if (ret < 0)
 1267                 goto err2;
 1268 
 1269         return ret;
 1270 
 1271 err2:
 1272         *sa_query = NULL;
 1273         ib_sa_client_put(query->sa_query.client);
 1274         free_mad(&query->sa_query);
 1275 
 1276 err1:
 1277         kfree(query);
 1278         return ret;
 1279 }
 1280 EXPORT_SYMBOL(ib_sa_guid_info_rec_query);
 1281 
 1282 /* Support get SA ClassPortInfo */
 1283 static void ib_sa_classport_info_rec_callback(struct ib_sa_query *sa_query,
 1284                                               int status,
 1285                                               struct ib_sa_mad *mad)
 1286 {
 1287         unsigned long flags;
 1288         struct ib_sa_classport_info_query *query =
 1289                 container_of(sa_query, struct ib_sa_classport_info_query, sa_query);
 1290 
 1291         if (mad) {
 1292                 struct ib_class_port_info rec;
 1293 
 1294                 ib_unpack(classport_info_rec_table,
 1295                           ARRAY_SIZE(classport_info_rec_table),
 1296                           mad->data, &rec);
 1297 
 1298                 spin_lock_irqsave(&sa_query->port->classport_lock, flags);
 1299                 if (!status && !sa_query->port->classport_info.valid) {
 1300                         memcpy(&sa_query->port->classport_info.data, &rec,
 1301                                sizeof(sa_query->port->classport_info.data));
 1302 
 1303                         sa_query->port->classport_info.valid = true;
 1304                 }
 1305                 spin_unlock_irqrestore(&sa_query->port->classport_lock, flags);
 1306 
 1307                 query->callback(status, &rec, query->context);
 1308         } else {
 1309                 query->callback(status, NULL, query->context);
 1310         }
 1311 }
 1312 
 1313 static void ib_sa_portclass_info_rec_release(struct ib_sa_query *sa_query)
 1314 {
 1315         kfree(container_of(sa_query, struct ib_sa_classport_info_query,
 1316                            sa_query));
 1317 }
 1318 
 1319 int ib_sa_classport_info_rec_query(struct ib_sa_client *client,
 1320                                    struct ib_device *device, u8 port_num,
 1321                                    int timeout_ms, gfp_t gfp_mask,
 1322                                    void (*callback)(int status,
 1323                                                     struct ib_class_port_info *resp,
 1324                                                     void *context),
 1325                                    void *context,
 1326                                    struct ib_sa_query **sa_query)
 1327 {
 1328         struct ib_sa_classport_info_query *query;
 1329         struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
 1330         struct ib_sa_port *port;
 1331         struct ib_mad_agent *agent;
 1332         struct ib_sa_mad *mad;
 1333         struct ib_class_port_info cached_class_port_info;
 1334         int ret;
 1335         unsigned long flags;
 1336 
 1337         if (!sa_dev)
 1338                 return -ENODEV;
 1339 
 1340         port  = &sa_dev->port[port_num - sa_dev->start_port];
 1341         agent = port->agent;
 1342 
 1343         /* Use cached ClassPortInfo attribute if valid instead of sending mad */
 1344         spin_lock_irqsave(&port->classport_lock, flags);
 1345         if (port->classport_info.valid && callback) {
 1346                 memcpy(&cached_class_port_info, &port->classport_info.data,
 1347                        sizeof(cached_class_port_info));
 1348                 spin_unlock_irqrestore(&port->classport_lock, flags);
 1349                 callback(0, &cached_class_port_info, context);
 1350                 return 0;
 1351         }
 1352         spin_unlock_irqrestore(&port->classport_lock, flags);
 1353 
 1354         query = kzalloc(sizeof(*query), gfp_mask);
 1355         if (!query)
 1356                 return -ENOMEM;
 1357 
 1358         query->sa_query.port = port;
 1359         ret = alloc_mad(&query->sa_query, gfp_mask);
 1360         if (ret)
 1361                 goto err1;
 1362 
 1363         ib_sa_client_get(client);
 1364         query->sa_query.client = client;
 1365         query->callback        = callback;
 1366         query->context         = context;
 1367 
 1368         mad = query->sa_query.mad_buf->mad;
 1369         init_mad(mad, agent);
 1370 
 1371         query->sa_query.callback = callback ? ib_sa_classport_info_rec_callback : NULL;
 1372 
 1373         query->sa_query.release  = ib_sa_portclass_info_rec_release;
 1374         /* support GET only */
 1375         mad->mad_hdr.method      = IB_MGMT_METHOD_GET;
 1376         mad->mad_hdr.attr_id     = cpu_to_be16(IB_SA_ATTR_CLASS_PORTINFO);
 1377         mad->sa_hdr.comp_mask    = 0;
 1378         *sa_query = &query->sa_query;
 1379 
 1380         ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
 1381         if (ret < 0)
 1382                 goto err2;
 1383 
 1384         return ret;
 1385 
 1386 err2:
 1387         *sa_query = NULL;
 1388         ib_sa_client_put(query->sa_query.client);
 1389         free_mad(&query->sa_query);
 1390 
 1391 err1:
 1392         kfree(query);
 1393         return ret;
 1394 }
 1395 EXPORT_SYMBOL(ib_sa_classport_info_rec_query);
 1396 
 1397 static void send_handler(struct ib_mad_agent *agent,
 1398                          struct ib_mad_send_wc *mad_send_wc)
 1399 {
 1400         struct ib_sa_query *query = mad_send_wc->send_buf->context[0];
 1401         unsigned long flags;
 1402 
 1403         if (query->callback)
 1404                 switch (mad_send_wc->status) {
 1405                 case IB_WC_SUCCESS:
 1406                         /* No callback -- already got recv */
 1407                         break;
 1408                 case IB_WC_RESP_TIMEOUT_ERR:
 1409                         query->callback(query, -ETIMEDOUT, NULL);
 1410                         break;
 1411                 case IB_WC_WR_FLUSH_ERR:
 1412                         query->callback(query, -EINTR, NULL);
 1413                         break;
 1414                 default:
 1415                         query->callback(query, -EIO, NULL);
 1416                         break;
 1417                 }
 1418 
 1419         spin_lock_irqsave(&idr_lock, flags);
 1420         idr_remove(&query_idr, query->id);
 1421         spin_unlock_irqrestore(&idr_lock, flags);
 1422 
 1423         free_mad(query);
 1424         ib_sa_client_put(query->client);
 1425         query->release(query);
 1426 }
 1427 
 1428 static void recv_handler(struct ib_mad_agent *mad_agent,
 1429                          struct ib_mad_send_buf *send_buf,
 1430                          struct ib_mad_recv_wc *mad_recv_wc)
 1431 {
 1432         struct ib_sa_query *query;
 1433 
 1434         if (!send_buf)
 1435                 return;
 1436 
 1437         query = send_buf->context[0];
 1438         if (query->callback) {
 1439                 if (mad_recv_wc->wc->status == IB_WC_SUCCESS)
 1440                         query->callback(query,
 1441                                         mad_recv_wc->recv_buf.mad->mad_hdr.status ?
 1442                                         -EINVAL : 0,
 1443                                         (struct ib_sa_mad *) mad_recv_wc->recv_buf.mad);
 1444                 else
 1445                         query->callback(query, -EIO, NULL);
 1446         }
 1447 
 1448         ib_free_recv_mad(mad_recv_wc);
 1449 }
 1450 
 1451 static void ib_sa_add_one(struct ib_device *device)
 1452 {
 1453         struct ib_sa_device *sa_dev;
 1454         int s, e, i;
 1455         int count = 0;
 1456 
 1457         s = rdma_start_port(device);
 1458         e = rdma_end_port(device);
 1459 
 1460         sa_dev = kzalloc(sizeof *sa_dev +
 1461                          (e - s + 1) * sizeof (struct ib_sa_port),
 1462                          GFP_KERNEL);
 1463         if (!sa_dev)
 1464                 return;
 1465 
 1466         sa_dev->start_port = s;
 1467         sa_dev->end_port   = e;
 1468 
 1469         for (i = 0; i <= e - s; ++i) {
 1470                 spin_lock_init(&sa_dev->port[i].ah_lock);
 1471                 if (!rdma_cap_ib_sa(device, i + 1))
 1472                         continue;
 1473 
 1474                 sa_dev->port[i].sm_ah    = NULL;
 1475                 sa_dev->port[i].port_num = i + s;
 1476 
 1477                 spin_lock_init(&sa_dev->port[i].classport_lock);
 1478                 sa_dev->port[i].classport_info.valid = false;
 1479 
 1480                 sa_dev->port[i].agent =
 1481                         ib_register_mad_agent(device, i + s, IB_QPT_GSI,
 1482                                               NULL, 0, send_handler,
 1483                                               recv_handler, sa_dev, 0);
 1484                 if (IS_ERR(sa_dev->port[i].agent))
 1485                         goto err;
 1486 
 1487                 INIT_WORK(&sa_dev->port[i].update_task, update_sm_ah);
 1488 
 1489                 count++;
 1490         }
 1491 
 1492         if (!count)
 1493                 goto free;
 1494 
 1495         ib_set_client_data(device, &sa_client, sa_dev);
 1496 
 1497         /*
 1498          * We register our event handler after everything is set up,
 1499          * and then update our cached info after the event handler is
 1500          * registered to avoid any problems if a port changes state
 1501          * during our initialization.
 1502          */
 1503 
 1504         INIT_IB_EVENT_HANDLER(&sa_dev->event_handler, device, ib_sa_event);
 1505         if (ib_register_event_handler(&sa_dev->event_handler))
 1506                 goto err;
 1507 
 1508         for (i = 0; i <= e - s; ++i) {
 1509                 if (rdma_cap_ib_sa(device, i + 1))
 1510                         update_sm_ah(&sa_dev->port[i].update_task);
 1511         }
 1512 
 1513         return;
 1514 
 1515 err:
 1516         while (--i >= 0) {
 1517                 if (rdma_cap_ib_sa(device, i + 1))
 1518                         ib_unregister_mad_agent(sa_dev->port[i].agent);
 1519         }
 1520 free:
 1521         kfree(sa_dev);
 1522         return;
 1523 }
 1524 
 1525 static void ib_sa_remove_one(struct ib_device *device, void *client_data)
 1526 {
 1527         struct ib_sa_device *sa_dev = client_data;
 1528         int i;
 1529 
 1530         if (!sa_dev)
 1531                 return;
 1532 
 1533         ib_unregister_event_handler(&sa_dev->event_handler);
 1534 
 1535         flush_workqueue(ib_wq);
 1536 
 1537         for (i = 0; i <= sa_dev->end_port - sa_dev->start_port; ++i) {
 1538                 if (rdma_cap_ib_sa(device, i + 1)) {
 1539                         ib_unregister_mad_agent(sa_dev->port[i].agent);
 1540                         if (sa_dev->port[i].sm_ah)
 1541                                 kref_put(&sa_dev->port[i].sm_ah->ref, free_sm_ah);
 1542                 }
 1543 
 1544         }
 1545 
 1546         kfree(sa_dev);
 1547 }
 1548 
 1549 int ib_sa_init(void)
 1550 {
 1551         int ret;
 1552 
 1553         get_random_bytes(&tid, sizeof tid);
 1554 
 1555         ret = ib_register_client(&sa_client);
 1556         if (ret) {
 1557                 pr_err("Couldn't register ib_sa client\n");
 1558                 goto err1;
 1559         }
 1560 
 1561         ret = mcast_init();
 1562         if (ret) {
 1563                 pr_err("Couldn't initialize multicast handling\n");
 1564                 goto err2;
 1565         }
 1566 
 1567         return 0;
 1568 
 1569 err2:
 1570         ib_unregister_client(&sa_client);
 1571 err1:
 1572         return ret;
 1573 }
 1574 
 1575 void ib_sa_cleanup(void)
 1576 {
 1577         mcast_cleanup();
 1578         ib_unregister_client(&sa_client);
 1579         idr_destroy(&query_idr);
 1580 }

Cache object: 255ad7bae5062d9e9ee6afaa5de24c06


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.