Path: blob/master/drivers/infiniband/hw/mlx4/mad.c
15112 views
/*1* Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.2*3* This software is available to you under a choice of one of two4* licenses. You may choose to be licensed under the terms of the GNU5* General Public License (GPL) Version 2, available from the file6* COPYING in the main directory of this source tree, or the7* OpenIB.org BSD license below:8*9* Redistribution and use in source and binary forms, with or10* without modification, are permitted provided that the following11* conditions are met:12*13* - Redistributions of source code must retain the above14* copyright notice, this list of conditions and the following15* disclaimer.16*17* - Redistributions in binary form must reproduce the above18* copyright notice, this list of conditions and the following19* disclaimer in the documentation and/or other materials20* provided with the distribution.21*22* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,23* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF24* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND25* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS26* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN27* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN28* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE29* SOFTWARE.30*/3132#include <rdma/ib_mad.h>33#include <rdma/ib_smi.h>3435#include <linux/mlx4/cmd.h>36#include <linux/gfp.h>3738#include "mlx4_ib.h"3940enum {41MLX4_IB_VENDOR_CLASS1 = 0x9,42MLX4_IB_VENDOR_CLASS2 = 0xa43};4445int mlx4_MAD_IFC(struct mlx4_ib_dev *dev, int ignore_mkey, int ignore_bkey,46int port, struct ib_wc *in_wc, struct ib_grh *in_grh,47void *in_mad, void *response_mad)48{49struct mlx4_cmd_mailbox *inmailbox, *outmailbox;50void *inbox;51int err;52u32 in_modifier = port;53u8 op_modifier = 0;5455inmailbox = mlx4_alloc_cmd_mailbox(dev->dev);56if (IS_ERR(inmailbox))57return PTR_ERR(inmailbox);58inbox = inmailbox->buf;5960outmailbox = mlx4_alloc_cmd_mailbox(dev->dev);61if (IS_ERR(outmailbox)) {62mlx4_free_cmd_mailbox(dev->dev, inmailbox);63return PTR_ERR(outmailbox);64}6566memcpy(inbox, in_mad, 256);6768/*69* Key check traps can't be generated unless we have in_wc to70* tell us where to send the trap.71*/72if (ignore_mkey || !in_wc)73op_modifier |= 0x1;74if (ignore_bkey || !in_wc)75op_modifier |= 0x2;7677if (in_wc) {78struct {79__be32 my_qpn;80u32 reserved1;81__be32 rqpn;82u8 sl;83u8 g_path;84u16 reserved2[2];85__be16 pkey;86u32 reserved3[11];87u8 grh[40];88} *ext_info;8990memset(inbox + 256, 0, 256);91ext_info = inbox + 256;9293ext_info->my_qpn = cpu_to_be32(in_wc->qp->qp_num);94ext_info->rqpn = cpu_to_be32(in_wc->src_qp);95ext_info->sl = in_wc->sl << 4;96ext_info->g_path = in_wc->dlid_path_bits |97(in_wc->wc_flags & IB_WC_GRH ? 0x80 : 0);98ext_info->pkey = cpu_to_be16(in_wc->pkey_index);99100if (in_grh)101memcpy(ext_info->grh, in_grh, 40);102103op_modifier |= 0x4;104105in_modifier |= in_wc->slid << 16;106}107108err = mlx4_cmd_box(dev->dev, inmailbox->dma, outmailbox->dma,109in_modifier, op_modifier,110MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C);111112if (!err)113memcpy(response_mad, outmailbox->buf, 256);114115mlx4_free_cmd_mailbox(dev->dev, inmailbox);116mlx4_free_cmd_mailbox(dev->dev, outmailbox);117118return err;119}120121static void update_sm_ah(struct mlx4_ib_dev *dev, u8 port_num, u16 lid, u8 sl)122{123struct ib_ah *new_ah;124struct ib_ah_attr ah_attr;125126if (!dev->send_agent[port_num - 1][0])127return;128129memset(&ah_attr, 0, sizeof ah_attr);130ah_attr.dlid = lid;131ah_attr.sl = sl;132ah_attr.port_num = port_num;133134new_ah = ib_create_ah(dev->send_agent[port_num - 1][0]->qp->pd,135&ah_attr);136if (IS_ERR(new_ah))137return;138139spin_lock(&dev->sm_lock);140if (dev->sm_ah[port_num - 1])141ib_destroy_ah(dev->sm_ah[port_num - 1]);142dev->sm_ah[port_num - 1] = new_ah;143spin_unlock(&dev->sm_lock);144}145146/*147* Snoop SM MADs for port info and P_Key table sets, so we can148* synthesize LID change and P_Key change events.149*/150static void smp_snoop(struct ib_device *ibdev, u8 port_num, struct ib_mad *mad,151u16 prev_lid)152{153struct ib_event event;154155if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||156mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&157mad->mad_hdr.method == IB_MGMT_METHOD_SET) {158if (mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO) {159struct ib_port_info *pinfo =160(struct ib_port_info *) ((struct ib_smp *) mad)->data;161u16 lid = be16_to_cpu(pinfo->lid);162163update_sm_ah(to_mdev(ibdev), port_num,164be16_to_cpu(pinfo->sm_lid),165pinfo->neighbormtu_mastersmsl & 0xf);166167event.device = ibdev;168event.element.port_num = port_num;169170if (pinfo->clientrereg_resv_subnetto & 0x80) {171event.event = IB_EVENT_CLIENT_REREGISTER;172ib_dispatch_event(&event);173}174175if (prev_lid != lid) {176event.event = IB_EVENT_LID_CHANGE;177ib_dispatch_event(&event);178}179}180181if (mad->mad_hdr.attr_id == IB_SMP_ATTR_PKEY_TABLE) {182event.device = ibdev;183event.event = IB_EVENT_PKEY_CHANGE;184event.element.port_num = port_num;185ib_dispatch_event(&event);186}187}188}189190static void node_desc_override(struct ib_device *dev,191struct ib_mad *mad)192{193if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||194mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&195mad->mad_hdr.method == IB_MGMT_METHOD_GET_RESP &&196mad->mad_hdr.attr_id == IB_SMP_ATTR_NODE_DESC) {197spin_lock(&to_mdev(dev)->sm_lock);198memcpy(((struct ib_smp *) mad)->data, dev->node_desc, 64);199spin_unlock(&to_mdev(dev)->sm_lock);200}201}202203static void forward_trap(struct mlx4_ib_dev *dev, u8 port_num, struct ib_mad *mad)204{205int qpn = mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED;206struct ib_mad_send_buf *send_buf;207struct ib_mad_agent *agent = dev->send_agent[port_num - 1][qpn];208int ret;209210if (agent) {211send_buf = ib_create_send_mad(agent, qpn, 0, 0, IB_MGMT_MAD_HDR,212IB_MGMT_MAD_DATA, GFP_ATOMIC);213if (IS_ERR(send_buf))214return;215/*216* We rely here on the fact that MLX QPs don't use the217* address handle after the send is posted (this is218* wrong following the IB spec strictly, but we know219* it's OK for our devices).220*/221spin_lock(&dev->sm_lock);222memcpy(send_buf->mad, mad, sizeof *mad);223if ((send_buf->ah = dev->sm_ah[port_num - 1]))224ret = ib_post_send_mad(send_buf, NULL);225else226ret = -EINVAL;227spin_unlock(&dev->sm_lock);228229if (ret)230ib_free_send_mad(send_buf);231}232}233234int mlx4_ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,235struct ib_wc *in_wc, struct ib_grh *in_grh,236struct ib_mad *in_mad, struct ib_mad *out_mad)237{238u16 slid, prev_lid = 0;239int err;240struct ib_port_attr pattr;241242slid = in_wc ? in_wc->slid : be16_to_cpu(IB_LID_PERMISSIVE);243244if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP && slid == 0) {245forward_trap(to_mdev(ibdev), port_num, in_mad);246return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;247}248249if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||250in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {251if (in_mad->mad_hdr.method != IB_MGMT_METHOD_GET &&252in_mad->mad_hdr.method != IB_MGMT_METHOD_SET &&253in_mad->mad_hdr.method != IB_MGMT_METHOD_TRAP_REPRESS)254return IB_MAD_RESULT_SUCCESS;255256/*257* Don't process SMInfo queries or vendor-specific258* MADs -- the SMA can't handle them.259*/260if (in_mad->mad_hdr.attr_id == IB_SMP_ATTR_SM_INFO ||261((in_mad->mad_hdr.attr_id & IB_SMP_ATTR_VENDOR_MASK) ==262IB_SMP_ATTR_VENDOR_MASK))263return IB_MAD_RESULT_SUCCESS;264} else if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT ||265in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS1 ||266in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS2 ||267in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_CONG_MGMT) {268if (in_mad->mad_hdr.method != IB_MGMT_METHOD_GET &&269in_mad->mad_hdr.method != IB_MGMT_METHOD_SET)270return IB_MAD_RESULT_SUCCESS;271} else272return IB_MAD_RESULT_SUCCESS;273274if ((in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||275in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&276in_mad->mad_hdr.method == IB_MGMT_METHOD_SET &&277in_mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO &&278!ib_query_port(ibdev, port_num, &pattr))279prev_lid = pattr.lid;280281err = mlx4_MAD_IFC(to_mdev(ibdev),282mad_flags & IB_MAD_IGNORE_MKEY,283mad_flags & IB_MAD_IGNORE_BKEY,284port_num, in_wc, in_grh, in_mad, out_mad);285if (err)286return IB_MAD_RESULT_FAILURE;287288if (!out_mad->mad_hdr.status) {289smp_snoop(ibdev, port_num, in_mad, prev_lid);290node_desc_override(ibdev, out_mad);291}292293/* set return bit in status of directed route responses */294if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)295out_mad->mad_hdr.status |= cpu_to_be16(1 << 15);296297if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS)298/* no response for trap repress */299return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;300301return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;302}303304static void send_handler(struct ib_mad_agent *agent,305struct ib_mad_send_wc *mad_send_wc)306{307ib_free_send_mad(mad_send_wc->send_buf);308}309310int mlx4_ib_mad_init(struct mlx4_ib_dev *dev)311{312struct ib_mad_agent *agent;313int p, q;314int ret;315enum rdma_link_layer ll;316317for (p = 0; p < dev->num_ports; ++p) {318ll = rdma_port_get_link_layer(&dev->ib_dev, p + 1);319for (q = 0; q <= 1; ++q) {320if (ll == IB_LINK_LAYER_INFINIBAND) {321agent = ib_register_mad_agent(&dev->ib_dev, p + 1,322q ? IB_QPT_GSI : IB_QPT_SMI,323NULL, 0, send_handler,324NULL, NULL);325if (IS_ERR(agent)) {326ret = PTR_ERR(agent);327goto err;328}329dev->send_agent[p][q] = agent;330} else331dev->send_agent[p][q] = NULL;332}333}334335return 0;336337err:338for (p = 0; p < dev->num_ports; ++p)339for (q = 0; q <= 1; ++q)340if (dev->send_agent[p][q])341ib_unregister_mad_agent(dev->send_agent[p][q]);342343return ret;344}345346void mlx4_ib_mad_cleanup(struct mlx4_ib_dev *dev)347{348struct ib_mad_agent *agent;349int p, q;350351for (p = 0; p < dev->num_ports; ++p) {352for (q = 0; q <= 1; ++q) {353agent = dev->send_agent[p][q];354if (agent) {355dev->send_agent[p][q] = NULL;356ib_unregister_mad_agent(agent);357}358}359360if (dev->sm_ah[p])361ib_destroy_ah(dev->sm_ah[p]);362}363}364365366