Path: blob/master/drivers/infiniband/hw/mthca/mthca_mad.c
15112 views
/*1* Copyright (c) 2004 Topspin Communications. All rights reserved.2* Copyright (c) 2005 Mellanox Technologies. All rights reserved.3* Copyright (c) 2004 Voltaire, Inc. All rights reserved.4*5* This software is available to you under a choice of one of two6* licenses. You may choose to be licensed under the terms of the GNU7* General Public License (GPL) Version 2, available from the file8* COPYING in the main directory of this source tree, or the9* OpenIB.org BSD license below:10*11* Redistribution and use in source and binary forms, with or12* without modification, are permitted provided that the following13* conditions are met:14*15* - Redistributions of source code must retain the above16* copyright notice, this list of conditions and the following17* disclaimer.18*19* - Redistributions in binary form must reproduce the above20* copyright notice, this list of conditions and the following21* disclaimer in the documentation and/or other materials22* provided with the distribution.23*24* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,25* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF26* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND27* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS28* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN29* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN30* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE31* SOFTWARE.32*/3334#include <linux/string.h>35#include <linux/slab.h>3637#include <rdma/ib_verbs.h>38#include <rdma/ib_mad.h>39#include <rdma/ib_smi.h>4041#include "mthca_dev.h"42#include "mthca_cmd.h"4344enum {45MTHCA_VENDOR_CLASS1 = 0x9,46MTHCA_VENDOR_CLASS2 = 0xa47};4849static int mthca_update_rate(struct mthca_dev *dev, u8 port_num)50{51struct ib_port_attr *tprops = NULL;52int ret;5354tprops = kmalloc(sizeof *tprops, GFP_KERNEL);55if (!tprops)56return -ENOMEM;5758ret = ib_query_port(&dev->ib_dev, port_num, tprops);59if (ret) {60printk(KERN_WARNING "ib_query_port failed (%d) for %s port %d\n",61ret, dev->ib_dev.name, port_num);62goto out;63}6465dev->rate[port_num - 1] = tprops->active_speed *66ib_width_enum_to_int(tprops->active_width);6768out:69kfree(tprops);70return ret;71}7273static void update_sm_ah(struct mthca_dev *dev,74u8 port_num, u16 lid, u8 sl)75{76struct ib_ah *new_ah;77struct ib_ah_attr ah_attr;78unsigned long flags;7980if (!dev->send_agent[port_num - 1][0])81return;8283memset(&ah_attr, 0, sizeof ah_attr);84ah_attr.dlid = lid;85ah_attr.sl = sl;86ah_attr.port_num = port_num;8788new_ah = ib_create_ah(dev->send_agent[port_num - 1][0]->qp->pd,89&ah_attr);90if (IS_ERR(new_ah))91return;9293spin_lock_irqsave(&dev->sm_lock, flags);94if (dev->sm_ah[port_num - 1])95ib_destroy_ah(dev->sm_ah[port_num - 1]);96dev->sm_ah[port_num - 1] = new_ah;97spin_unlock_irqrestore(&dev->sm_lock, flags);98}99100/*101* Snoop SM MADs for port info and P_Key table sets, so we can102* synthesize LID change and P_Key change events.103*/104static void smp_snoop(struct ib_device *ibdev,105u8 port_num,106struct ib_mad *mad,107u16 prev_lid)108{109struct ib_event event;110111if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||112mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&113mad->mad_hdr.method == IB_MGMT_METHOD_SET) {114if (mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO) {115struct ib_port_info *pinfo =116(struct ib_port_info *) ((struct ib_smp *) mad)->data;117u16 lid = be16_to_cpu(pinfo->lid);118119mthca_update_rate(to_mdev(ibdev), port_num);120update_sm_ah(to_mdev(ibdev), port_num,121be16_to_cpu(pinfo->sm_lid),122pinfo->neighbormtu_mastersmsl & 0xf);123124event.device = ibdev;125event.element.port_num = port_num;126127if (pinfo->clientrereg_resv_subnetto & 0x80) {128event.event = IB_EVENT_CLIENT_REREGISTER;129ib_dispatch_event(&event);130}131132if (prev_lid != lid) {133event.event = IB_EVENT_LID_CHANGE;134ib_dispatch_event(&event);135}136}137138if (mad->mad_hdr.attr_id == IB_SMP_ATTR_PKEY_TABLE) {139event.device = ibdev;140event.event = IB_EVENT_PKEY_CHANGE;141event.element.port_num = port_num;142ib_dispatch_event(&event);143}144}145}146147static void node_desc_override(struct ib_device *dev,148struct ib_mad *mad)149{150if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||151mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&152mad->mad_hdr.method == IB_MGMT_METHOD_GET_RESP &&153mad->mad_hdr.attr_id == IB_SMP_ATTR_NODE_DESC) {154mutex_lock(&to_mdev(dev)->cap_mask_mutex);155memcpy(((struct ib_smp *) mad)->data, dev->node_desc, 64);156mutex_unlock(&to_mdev(dev)->cap_mask_mutex);157}158}159160static void forward_trap(struct mthca_dev *dev,161u8 port_num,162struct ib_mad *mad)163{164int qpn = mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED;165struct ib_mad_send_buf *send_buf;166struct ib_mad_agent *agent = dev->send_agent[port_num - 1][qpn];167int ret;168unsigned long flags;169170if (agent) {171send_buf = ib_create_send_mad(agent, qpn, 0, 0, IB_MGMT_MAD_HDR,172IB_MGMT_MAD_DATA, GFP_ATOMIC);173if (IS_ERR(send_buf))174return;175/*176* We rely here on the fact that MLX QPs don't use the177* address handle after the send is posted (this is178* wrong following the IB spec strictly, but we know179* it's OK for our devices).180*/181spin_lock_irqsave(&dev->sm_lock, flags);182memcpy(send_buf->mad, mad, sizeof *mad);183if ((send_buf->ah = dev->sm_ah[port_num - 1]))184ret = ib_post_send_mad(send_buf, NULL);185else186ret = -EINVAL;187spin_unlock_irqrestore(&dev->sm_lock, flags);188189if (ret)190ib_free_send_mad(send_buf);191}192}193194int mthca_process_mad(struct ib_device *ibdev,195int mad_flags,196u8 port_num,197struct ib_wc *in_wc,198struct ib_grh *in_grh,199struct ib_mad *in_mad,200struct ib_mad *out_mad)201{202int err;203u8 status;204u16 slid = in_wc ? in_wc->slid : be16_to_cpu(IB_LID_PERMISSIVE);205u16 prev_lid = 0;206struct ib_port_attr pattr;207208/* Forward locally generated traps to the SM */209if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP &&210slid == 0) {211forward_trap(to_mdev(ibdev), port_num, in_mad);212return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;213}214215/*216* Only handle SM gets, sets and trap represses for SM class217*218* Only handle PMA and Mellanox vendor-specific class gets and219* sets for other classes.220*/221if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||222in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {223if (in_mad->mad_hdr.method != IB_MGMT_METHOD_GET &&224in_mad->mad_hdr.method != IB_MGMT_METHOD_SET &&225in_mad->mad_hdr.method != IB_MGMT_METHOD_TRAP_REPRESS)226return IB_MAD_RESULT_SUCCESS;227228/*229* Don't process SMInfo queries or vendor-specific230* MADs -- the SMA can't handle them.231*/232if (in_mad->mad_hdr.attr_id == IB_SMP_ATTR_SM_INFO ||233((in_mad->mad_hdr.attr_id & IB_SMP_ATTR_VENDOR_MASK) ==234IB_SMP_ATTR_VENDOR_MASK))235return IB_MAD_RESULT_SUCCESS;236} else if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT ||237in_mad->mad_hdr.mgmt_class == MTHCA_VENDOR_CLASS1 ||238in_mad->mad_hdr.mgmt_class == MTHCA_VENDOR_CLASS2) {239if (in_mad->mad_hdr.method != IB_MGMT_METHOD_GET &&240in_mad->mad_hdr.method != IB_MGMT_METHOD_SET)241return IB_MAD_RESULT_SUCCESS;242} else243return IB_MAD_RESULT_SUCCESS;244if ((in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||245in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&246in_mad->mad_hdr.method == IB_MGMT_METHOD_SET &&247in_mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO &&248!ib_query_port(ibdev, port_num, &pattr))249prev_lid = pattr.lid;250251err = mthca_MAD_IFC(to_mdev(ibdev),252mad_flags & IB_MAD_IGNORE_MKEY,253mad_flags & IB_MAD_IGNORE_BKEY,254port_num, in_wc, in_grh, in_mad, out_mad,255&status);256if (err) {257mthca_err(to_mdev(ibdev), "MAD_IFC failed\n");258return IB_MAD_RESULT_FAILURE;259}260if (status == MTHCA_CMD_STAT_BAD_PKT)261return IB_MAD_RESULT_SUCCESS;262if (status) {263mthca_err(to_mdev(ibdev), "MAD_IFC returned status %02x\n",264status);265return IB_MAD_RESULT_FAILURE;266}267268if (!out_mad->mad_hdr.status) {269smp_snoop(ibdev, port_num, in_mad, prev_lid);270node_desc_override(ibdev, out_mad);271}272273/* set return bit in status of directed route responses */274if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)275out_mad->mad_hdr.status |= cpu_to_be16(1 << 15);276277if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS)278/* no response for trap repress */279return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;280281return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;282}283284static void send_handler(struct ib_mad_agent *agent,285struct ib_mad_send_wc *mad_send_wc)286{287ib_free_send_mad(mad_send_wc->send_buf);288}289290int mthca_create_agents(struct mthca_dev *dev)291{292struct ib_mad_agent *agent;293int p, q;294int ret;295296spin_lock_init(&dev->sm_lock);297298for (p = 0; p < dev->limits.num_ports; ++p)299for (q = 0; q <= 1; ++q) {300agent = ib_register_mad_agent(&dev->ib_dev, p + 1,301q ? IB_QPT_GSI : IB_QPT_SMI,302NULL, 0, send_handler,303NULL, NULL);304if (IS_ERR(agent)) {305ret = PTR_ERR(agent);306goto err;307}308dev->send_agent[p][q] = agent;309}310311312for (p = 1; p <= dev->limits.num_ports; ++p) {313ret = mthca_update_rate(dev, p);314if (ret) {315mthca_err(dev, "Failed to obtain port %d rate."316" aborting.\n", p);317goto err;318}319}320321return 0;322323err:324for (p = 0; p < dev->limits.num_ports; ++p)325for (q = 0; q <= 1; ++q)326if (dev->send_agent[p][q])327ib_unregister_mad_agent(dev->send_agent[p][q]);328329return ret;330}331332void mthca_free_agents(struct mthca_dev *dev)333{334struct ib_mad_agent *agent;335int p, q;336337for (p = 0; p < dev->limits.num_ports; ++p) {338for (q = 0; q <= 1; ++q) {339agent = dev->send_agent[p][q];340dev->send_agent[p][q] = NULL;341ib_unregister_mad_agent(agent);342}343344if (dev->sm_ah[p])345ib_destroy_ah(dev->sm_ah[p]);346}347}348349350